xref: /openbmc/qemu/tests/qtest/libqtest.c (revision 587adaca)
1 /*
2  * QTest
3  *
4  * Copyright IBM, Corp. 2012
5  * Copyright Red Hat, Inc. 2012
6  * Copyright SUSE LINUX Products GmbH 2013
7  *
8  * Authors:
9  *  Anthony Liguori   <aliguori@us.ibm.com>
10  *  Paolo Bonzini     <pbonzini@redhat.com>
11  *  Andreas Färber    <afaerber@suse.de>
12  *
13  * This work is licensed under the terms of the GNU GPL, version 2 or later.
14  * See the COPYING file in the top-level directory.
15  */
16 
17 #include "qemu/osdep.h"
18 
19 #include <sys/socket.h>
20 #include <sys/wait.h>
21 #include <sys/un.h>
22 
23 #include "libqos/libqtest.h"
24 #include "qemu-common.h"
25 #include "qemu/ctype.h"
26 #include "qemu/cutils.h"
27 #include "qapi/error.h"
28 #include "qapi/qmp/json-parser.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qjson.h"
31 #include "qapi/qmp/qlist.h"
32 #include "qapi/qmp/qstring.h"
33 
34 #define MAX_IRQ 256
35 #define SOCKET_TIMEOUT 50
36 #define SOCKET_MAX_FDS 16
37 
38 
39 typedef void (*QTestSendFn)(QTestState *s, const char *buf);
40 typedef void (*ExternalSendFn)(void *s, const char *buf);
41 typedef GString* (*QTestRecvFn)(QTestState *);
42 
43 typedef struct QTestClientTransportOps {
44     QTestSendFn     send;      /* for sending qtest commands */
45 
46     /*
47      * use external_send to send qtest command strings through functions which
48      * do not accept a QTestState as the first parameter.
49      */
50     ExternalSendFn  external_send;
51 
52     QTestRecvFn     recv_line; /* for receiving qtest command responses */
53 } QTestTransportOps;
54 
55 struct QTestState
56 {
57     int fd;
58     int qmp_fd;
59     pid_t qemu_pid;  /* our child QEMU process */
60     int wstatus;
61     int expected_status;
62     bool big_endian;
63     bool irq_level[MAX_IRQ];
64     GString *rx;
65     QTestTransportOps ops;
66     GList *pending_events;
67 };
68 
69 static GHookList abrt_hooks;
70 static struct sigaction sigact_old;
71 
72 static int qtest_query_target_endianness(QTestState *s);
73 
74 static void qtest_client_socket_send(QTestState*, const char *buf);
75 static void socket_send(int fd, const char *buf, size_t size);
76 
77 static GString *qtest_client_socket_recv_line(QTestState *);
78 
79 static void qtest_client_set_tx_handler(QTestState *s, QTestSendFn send);
80 static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv);
81 
82 static int init_socket(const char *socket_path)
83 {
84     int sock = qtest_socket_server(socket_path);
85     qemu_set_cloexec(sock);
86     return sock;
87 }
88 
89 static int socket_accept(int sock)
90 {
91     struct sockaddr_un addr;
92     socklen_t addrlen;
93     int ret;
94     struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
95                                .tv_usec = 0 };
96 
97     if (qemu_setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
98                         (void *)&timeout, sizeof(timeout))) {
99         fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
100                 __func__, strerror(errno));
101         close(sock);
102         return -1;
103     }
104 
105     do {
106         addrlen = sizeof(addr);
107         ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
108     } while (ret == -1 && errno == EINTR);
109     if (ret == -1) {
110         fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
111     }
112     close(sock);
113 
114     return ret;
115 }
116 
117 bool qtest_probe_child(QTestState *s)
118 {
119     pid_t pid = s->qemu_pid;
120 
121     if (pid != -1) {
122         pid = waitpid(pid, &s->wstatus, WNOHANG);
123         if (pid == 0) {
124             return true;
125         }
126         s->qemu_pid = -1;
127     }
128     return false;
129 }
130 
131 void qtest_set_expected_status(QTestState *s, int status)
132 {
133     s->expected_status = status;
134 }
135 
136 void qtest_kill_qemu(QTestState *s)
137 {
138     pid_t pid = s->qemu_pid;
139     int wstatus;
140 
141     /* Skip wait if qtest_probe_child already reaped.  */
142     if (pid != -1) {
143         kill(pid, SIGTERM);
144         TFR(pid = waitpid(s->qemu_pid, &s->wstatus, 0));
145         assert(pid == s->qemu_pid);
146         s->qemu_pid = -1;
147     }
148 
149     /*
150      * Check whether qemu exited with expected exit status; anything else is
151      * fishy and should be logged with as much detail as possible.
152      */
153     wstatus = s->wstatus;
154     if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != s->expected_status) {
155         fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
156                 "process but encountered exit status %d (expected %d)\n",
157                 __FILE__, __LINE__, WEXITSTATUS(wstatus), s->expected_status);
158         abort();
159     } else if (WIFSIGNALED(wstatus)) {
160         int sig = WTERMSIG(wstatus);
161         const char *signame = strsignal(sig) ?: "unknown ???";
162         const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : "";
163 
164         fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death "
165                 "from signal %d (%s)%s\n",
166                 __FILE__, __LINE__, sig, signame, dump);
167         abort();
168     }
169 }
170 
171 static void kill_qemu_hook_func(void *s)
172 {
173     qtest_kill_qemu(s);
174 }
175 
176 static void sigabrt_handler(int signo)
177 {
178     g_hook_list_invoke(&abrt_hooks, FALSE);
179 }
180 
181 static void setup_sigabrt_handler(void)
182 {
183     struct sigaction sigact;
184 
185     /* Catch SIGABRT to clean up on g_assert() failure */
186     sigact = (struct sigaction){
187         .sa_handler = sigabrt_handler,
188         .sa_flags = SA_RESETHAND,
189     };
190     sigemptyset(&sigact.sa_mask);
191     sigaction(SIGABRT, &sigact, &sigact_old);
192 }
193 
194 static void cleanup_sigabrt_handler(void)
195 {
196     sigaction(SIGABRT, &sigact_old, NULL);
197 }
198 
199 static bool hook_list_is_empty(GHookList *hook_list)
200 {
201     GHook *hook = g_hook_first_valid(hook_list, TRUE);
202 
203     if (!hook) {
204         return false;
205     }
206 
207     g_hook_unref(hook_list, hook);
208     return true;
209 }
210 
211 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
212 {
213     GHook *hook;
214 
215     if (!abrt_hooks.is_setup) {
216         g_hook_list_init(&abrt_hooks, sizeof(GHook));
217     }
218 
219     /* Only install SIGABRT handler once */
220     if (hook_list_is_empty(&abrt_hooks)) {
221         setup_sigabrt_handler();
222     }
223 
224     hook = g_hook_alloc(&abrt_hooks);
225     hook->func = fn;
226     hook->data = (void *)data;
227 
228     g_hook_prepend(&abrt_hooks, hook);
229 }
230 
231 void qtest_remove_abrt_handler(void *data)
232 {
233     GHook *hook = g_hook_find_data(&abrt_hooks, TRUE, data);
234     g_hook_destroy_link(&abrt_hooks, hook);
235 
236     /* Uninstall SIGABRT handler on last instance */
237     if (hook_list_is_empty(&abrt_hooks)) {
238         cleanup_sigabrt_handler();
239     }
240 }
241 
242 static const char *qtest_qemu_binary(void)
243 {
244     const char *qemu_bin;
245 
246     qemu_bin = getenv("QTEST_QEMU_BINARY");
247     if (!qemu_bin) {
248         fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
249         exit(1);
250     }
251 
252     return qemu_bin;
253 }
254 
255 QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
256 {
257     QTestState *s;
258     int sock, qmpsock, i;
259     gchar *socket_path;
260     gchar *qmp_socket_path;
261     gchar *command;
262     const char *qemu_binary = qtest_qemu_binary();
263 
264     s = g_new(QTestState, 1);
265 
266     socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
267     qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
268 
269     /* It's possible that if an earlier test run crashed it might
270      * have left a stale unix socket lying around. Delete any
271      * stale old socket to avoid spurious test failures with
272      * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
273      */
274     unlink(socket_path);
275     unlink(qmp_socket_path);
276 
277     sock = init_socket(socket_path);
278     qmpsock = init_socket(qmp_socket_path);
279 
280     qtest_client_set_rx_handler(s, qtest_client_socket_recv_line);
281     qtest_client_set_tx_handler(s, qtest_client_socket_send);
282 
283     qtest_add_abrt_handler(kill_qemu_hook_func, s);
284 
285     command = g_strdup_printf("exec %s "
286                               "-qtest unix:%s "
287                               "-qtest-log %s "
288                               "-chardev socket,path=%s,id=char0 "
289                               "-mon chardev=char0,mode=control "
290                               "-display none "
291                               "%s"
292                               " -accel qtest", qemu_binary, socket_path,
293                               getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
294                               qmp_socket_path,
295                               extra_args ?: "");
296 
297     g_test_message("starting QEMU: %s", command);
298 
299     s->pending_events = NULL;
300     s->wstatus = 0;
301     s->expected_status = 0;
302     s->qemu_pid = fork();
303     if (s->qemu_pid == 0) {
304         g_setenv("QEMU_AUDIO_DRV", "none", true);
305         execlp("/bin/sh", "sh", "-c", command, NULL);
306         exit(1);
307     }
308 
309     g_free(command);
310     s->fd = socket_accept(sock);
311     if (s->fd >= 0) {
312         s->qmp_fd = socket_accept(qmpsock);
313     }
314     unlink(socket_path);
315     unlink(qmp_socket_path);
316     g_free(socket_path);
317     g_free(qmp_socket_path);
318 
319     g_assert(s->fd >= 0 && s->qmp_fd >= 0);
320 
321     s->rx = g_string_new("");
322     for (i = 0; i < MAX_IRQ; i++) {
323         s->irq_level[i] = false;
324     }
325 
326     if (getenv("QTEST_STOP")) {
327         kill(s->qemu_pid, SIGSTOP);
328     }
329 
330     /* ask endianness of the target */
331 
332     s->big_endian = qtest_query_target_endianness(s);
333 
334     return s;
335 }
336 
337 QTestState *qtest_init(const char *extra_args)
338 {
339     QTestState *s = qtest_init_without_qmp_handshake(extra_args);
340     QDict *greeting;
341 
342     /* Read the QMP greeting and then do the handshake */
343     greeting = qtest_qmp_receive(s);
344     qobject_unref(greeting);
345     qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }"));
346 
347     return s;
348 }
349 
350 QTestState *qtest_vinitf(const char *fmt, va_list ap)
351 {
352     char *args = g_strdup_vprintf(fmt, ap);
353     QTestState *s;
354 
355     s = qtest_init(args);
356     g_free(args);
357     return s;
358 }
359 
360 QTestState *qtest_initf(const char *fmt, ...)
361 {
362     va_list ap;
363     QTestState *s;
364 
365     va_start(ap, fmt);
366     s = qtest_vinitf(fmt, ap);
367     va_end(ap);
368     return s;
369 }
370 
371 QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd)
372 {
373     int sock_fd_init;
374     char *sock_path, sock_dir[] = "/tmp/qtest-serial-XXXXXX";
375     QTestState *qts;
376 
377     g_assert_true(mkdtemp(sock_dir) != NULL);
378     sock_path = g_strdup_printf("%s/sock", sock_dir);
379 
380     sock_fd_init = init_socket(sock_path);
381 
382     qts = qtest_initf("-chardev socket,id=s0,path=%s -serial chardev:s0 %s",
383                       sock_path, extra_args);
384 
385     *sock_fd = socket_accept(sock_fd_init);
386 
387     unlink(sock_path);
388     g_free(sock_path);
389     rmdir(sock_dir);
390 
391     g_assert_true(*sock_fd >= 0);
392 
393     return qts;
394 }
395 
396 void qtest_quit(QTestState *s)
397 {
398     qtest_remove_abrt_handler(s);
399 
400     qtest_kill_qemu(s);
401     close(s->fd);
402     close(s->qmp_fd);
403     g_string_free(s->rx, true);
404 
405     for (GList *it = s->pending_events; it != NULL; it = it->next) {
406         qobject_unref((QDict *)it->data);
407     }
408 
409     g_list_free(s->pending_events);
410 
411     g_free(s);
412 }
413 
414 static void socket_send(int fd, const char *buf, size_t size)
415 {
416     size_t offset;
417 
418     offset = 0;
419     while (offset < size) {
420         ssize_t len;
421 
422         len = write(fd, buf + offset, size - offset);
423         if (len == -1 && errno == EINTR) {
424             continue;
425         }
426 
427         g_assert_cmpint(len, >, 0);
428 
429         offset += len;
430     }
431 }
432 
433 static void qtest_client_socket_send(QTestState *s, const char *buf)
434 {
435     socket_send(s->fd, buf, strlen(buf));
436 }
437 
438 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
439 {
440     va_list ap;
441 
442     va_start(ap, fmt);
443     gchar *str = g_strdup_vprintf(fmt, ap);
444     va_end(ap);
445 
446     s->ops.send(s, str);
447     g_free(str);
448 }
449 
450 /* Sends a message and file descriptors to the socket.
451  * It's needed for qmp-commands like getfd/add-fd */
452 static void socket_send_fds(int socket_fd, int *fds, size_t fds_num,
453                             const char *buf, size_t buf_size)
454 {
455     ssize_t ret;
456     struct msghdr msg = { 0 };
457     char control[CMSG_SPACE(sizeof(int) * SOCKET_MAX_FDS)] = { 0 };
458     size_t fdsize = sizeof(int) * fds_num;
459     struct cmsghdr *cmsg;
460     struct iovec iov = { .iov_base = (char *)buf, .iov_len = buf_size };
461 
462     msg.msg_iov = &iov;
463     msg.msg_iovlen = 1;
464 
465     if (fds && fds_num > 0) {
466         g_assert_cmpuint(fds_num, <, SOCKET_MAX_FDS);
467 
468         msg.msg_control = control;
469         msg.msg_controllen = CMSG_SPACE(fdsize);
470 
471         cmsg = CMSG_FIRSTHDR(&msg);
472         cmsg->cmsg_len = CMSG_LEN(fdsize);
473         cmsg->cmsg_level = SOL_SOCKET;
474         cmsg->cmsg_type = SCM_RIGHTS;
475         memcpy(CMSG_DATA(cmsg), fds, fdsize);
476     }
477 
478     do {
479         ret = sendmsg(socket_fd, &msg, 0);
480     } while (ret < 0 && errno == EINTR);
481     g_assert_cmpint(ret, >, 0);
482 }
483 
484 static GString *qtest_client_socket_recv_line(QTestState *s)
485 {
486     GString *line;
487     size_t offset;
488     char *eol;
489 
490     while ((eol = strchr(s->rx->str, '\n')) == NULL) {
491         ssize_t len;
492         char buffer[1024];
493 
494         len = read(s->fd, buffer, sizeof(buffer));
495         if (len == -1 && errno == EINTR) {
496             continue;
497         }
498 
499         if (len == -1 || len == 0) {
500             fprintf(stderr, "Broken pipe\n");
501             abort();
502         }
503 
504         g_string_append_len(s->rx, buffer, len);
505     }
506 
507     offset = eol - s->rx->str;
508     line = g_string_new_len(s->rx->str, offset);
509     g_string_erase(s->rx, 0, offset + 1);
510 
511     return line;
512 }
513 
514 static gchar **qtest_rsp_args(QTestState *s, int expected_args)
515 {
516     GString *line;
517     gchar **words;
518     int i;
519 
520 redo:
521     line = s->ops.recv_line(s);
522     words = g_strsplit(line->str, " ", 0);
523     g_string_free(line, TRUE);
524 
525     if (strcmp(words[0], "IRQ") == 0) {
526         long irq;
527         int ret;
528 
529         g_assert(words[1] != NULL);
530         g_assert(words[2] != NULL);
531 
532         ret = qemu_strtol(words[2], NULL, 0, &irq);
533         g_assert(!ret);
534         g_assert_cmpint(irq, >=, 0);
535         g_assert_cmpint(irq, <, MAX_IRQ);
536 
537         if (strcmp(words[1], "raise") == 0) {
538             s->irq_level[irq] = true;
539         } else {
540             s->irq_level[irq] = false;
541         }
542 
543         g_strfreev(words);
544         goto redo;
545     }
546 
547     g_assert(words[0] != NULL);
548     g_assert_cmpstr(words[0], ==, "OK");
549 
550     for (i = 0; i < expected_args; i++) {
551         g_assert(words[i] != NULL);
552     }
553 
554     return words;
555 }
556 
557 static void qtest_rsp(QTestState *s)
558 {
559     gchar **words = qtest_rsp_args(s, 0);
560 
561     g_strfreev(words);
562 }
563 
564 static int qtest_query_target_endianness(QTestState *s)
565 {
566     gchar **args;
567     int big_endian;
568 
569     qtest_sendf(s, "endianness\n");
570     args = qtest_rsp_args(s, 1);
571     g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
572     big_endian = strcmp(args[1], "big") == 0;
573     g_strfreev(args);
574 
575     return big_endian;
576 }
577 
578 typedef struct {
579     JSONMessageParser parser;
580     QDict *response;
581 } QMPResponseParser;
582 
583 static void qmp_response(void *opaque, QObject *obj, Error *err)
584 {
585     QMPResponseParser *qmp = opaque;
586 
587     assert(!obj != !err);
588 
589     if (err) {
590         error_prepend(&err, "QMP JSON response parsing failed: ");
591         error_report_err(err);
592         abort();
593     }
594 
595     g_assert(!qmp->response);
596     qmp->response = qobject_to(QDict, obj);
597     g_assert(qmp->response);
598 }
599 
600 QDict *qmp_fd_receive(int fd)
601 {
602     QMPResponseParser qmp;
603     bool log = getenv("QTEST_LOG") != NULL;
604 
605     qmp.response = NULL;
606     json_message_parser_init(&qmp.parser, qmp_response, &qmp, NULL);
607     while (!qmp.response) {
608         ssize_t len;
609         char c;
610 
611         len = read(fd, &c, 1);
612         if (len == -1 && errno == EINTR) {
613             continue;
614         }
615 
616         if (len == -1 || len == 0) {
617             fprintf(stderr, "Broken pipe\n");
618             abort();
619         }
620 
621         if (log) {
622             len = write(2, &c, 1);
623         }
624         json_message_parser_feed(&qmp.parser, &c, 1);
625     }
626     json_message_parser_destroy(&qmp.parser);
627 
628     return qmp.response;
629 }
630 
631 QDict *qtest_qmp_receive(QTestState *s)
632 {
633     while (true) {
634         QDict *response = qtest_qmp_receive_dict(s);
635 
636         if (!qdict_get_try_str(response, "event")) {
637             return response;
638         }
639         /* Stash the event for a later consumption */
640         s->pending_events = g_list_append(s->pending_events, response);
641     }
642 }
643 
644 QDict *qtest_qmp_receive_dict(QTestState *s)
645 {
646     return qmp_fd_receive(s->qmp_fd);
647 }
648 
649 int qtest_socket_server(const char *socket_path)
650 {
651     struct sockaddr_un addr;
652     int sock;
653     int ret;
654 
655     sock = socket(PF_UNIX, SOCK_STREAM, 0);
656     g_assert_cmpint(sock, !=, -1);
657 
658     addr.sun_family = AF_UNIX;
659     snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
660 
661     do {
662         ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
663     } while (ret == -1 && errno == EINTR);
664     g_assert_cmpint(ret, !=, -1);
665     ret = listen(sock, 1);
666     g_assert_cmpint(ret, !=, -1);
667 
668     return sock;
669 }
670 
671 /**
672  * Allow users to send a message without waiting for the reply,
673  * in the case that they choose to discard all replies up until
674  * a particular EVENT is received.
675  */
676 void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
677                       const char *fmt, va_list ap)
678 {
679     QObject *qobj;
680 
681     /* Going through qobject ensures we escape strings properly */
682     qobj = qobject_from_vjsonf_nofail(fmt, ap);
683 
684     /* No need to send anything for an empty QObject.  */
685     if (qobj) {
686         int log = getenv("QTEST_LOG") != NULL;
687         GString *str = qobject_to_json(qobj);
688 
689         /*
690          * BUG: QMP doesn't react to input until it sees a newline, an
691          * object, or an array.  Work-around: give it a newline.
692          */
693         g_string_append_c(str, '\n');
694 
695         if (log) {
696             fprintf(stderr, "%s", str->str);
697         }
698         /* Send QMP request */
699         if (fds && fds_num > 0) {
700             socket_send_fds(fd, fds, fds_num, str->str, str->len);
701         } else {
702             socket_send(fd, str->str, str->len);
703         }
704 
705         g_string_free(str, true);
706         qobject_unref(qobj);
707     }
708 }
709 
710 void qmp_fd_vsend(int fd, const char *fmt, va_list ap)
711 {
712     qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
713 }
714 
715 void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
716                          const char *fmt, va_list ap)
717 {
718     qmp_fd_vsend_fds(s->qmp_fd, fds, fds_num, fmt, ap);
719 }
720 
721 void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
722 {
723     qmp_fd_vsend_fds(s->qmp_fd, NULL, 0, fmt, ap);
724 }
725 
726 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
727 {
728     qmp_fd_vsend_fds(fd, NULL, 0, fmt, ap);
729 
730     return qmp_fd_receive(fd);
731 }
732 
733 QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
734                       const char *fmt, va_list ap)
735 {
736     qtest_qmp_vsend_fds(s, fds, fds_num, fmt, ap);
737 
738     /* Receive reply */
739     return qtest_qmp_receive(s);
740 }
741 
742 QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
743 {
744     qtest_qmp_vsend(s, fmt, ap);
745 
746     /* Receive reply */
747     return qtest_qmp_receive(s);
748 }
749 
750 QDict *qmp_fd(int fd, const char *fmt, ...)
751 {
752     va_list ap;
753     QDict *response;
754 
755     va_start(ap, fmt);
756     response = qmp_fdv(fd, fmt, ap);
757     va_end(ap);
758     return response;
759 }
760 
761 void qmp_fd_send(int fd, const char *fmt, ...)
762 {
763     va_list ap;
764 
765     va_start(ap, fmt);
766     qmp_fd_vsend(fd, fmt, ap);
767     va_end(ap);
768 }
769 
770 QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
771                      const char *fmt, ...)
772 {
773     va_list ap;
774     QDict *response;
775 
776     va_start(ap, fmt);
777     response = qtest_vqmp_fds(s, fds, fds_num, fmt, ap);
778     va_end(ap);
779     return response;
780 }
781 
782 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
783 {
784     va_list ap;
785     QDict *response;
786 
787     va_start(ap, fmt);
788     response = qtest_vqmp(s, fmt, ap);
789     va_end(ap);
790     return response;
791 }
792 
793 void qtest_qmp_send(QTestState *s, const char *fmt, ...)
794 {
795     va_list ap;
796 
797     va_start(ap, fmt);
798     qtest_qmp_vsend(s, fmt, ap);
799     va_end(ap);
800 }
801 
802 void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap)
803 {
804     bool log = getenv("QTEST_LOG") != NULL;
805     char *str = g_strdup_vprintf(fmt, ap);
806 
807     if (log) {
808         fprintf(stderr, "%s", str);
809     }
810     socket_send(fd, str, strlen(str));
811     g_free(str);
812 }
813 
814 void qmp_fd_send_raw(int fd, const char *fmt, ...)
815 {
816     va_list ap;
817 
818     va_start(ap, fmt);
819     qmp_fd_vsend_raw(fd, fmt, ap);
820     va_end(ap);
821 }
822 
823 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
824 {
825     va_list ap;
826 
827     va_start(ap, fmt);
828     qmp_fd_vsend_raw(s->qmp_fd, fmt, ap);
829     va_end(ap);
830 }
831 
832 QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
833 {
834     while (s->pending_events) {
835 
836         GList *first = s->pending_events;
837         QDict *response = (QDict *)first->data;
838 
839         s->pending_events = g_list_delete_link(s->pending_events, first);
840 
841         if (!strcmp(qdict_get_str(response, "event"), event)) {
842             return response;
843         }
844         qobject_unref(response);
845     }
846     return NULL;
847 }
848 
849 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
850 {
851     QDict *response = qtest_qmp_event_ref(s, event);
852 
853     if (response) {
854         return response;
855     }
856 
857     for (;;) {
858         response = qtest_qmp_receive_dict(s);
859         if ((qdict_haskey(response, "event")) &&
860             (strcmp(qdict_get_str(response, "event"), event) == 0)) {
861             return response;
862         }
863         qobject_unref(response);
864     }
865 }
866 
867 void qtest_qmp_eventwait(QTestState *s, const char *event)
868 {
869     QDict *response;
870 
871     response = qtest_qmp_eventwait_ref(s, event);
872     qobject_unref(response);
873 }
874 
875 char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
876 {
877     char *cmd;
878     QDict *resp;
879     char *ret;
880 
881     cmd = g_strdup_vprintf(fmt, ap);
882     resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
883                      " 'arguments': {'command-line': %s}}",
884                      cmd);
885     ret = g_strdup(qdict_get_try_str(resp, "return"));
886     g_assert(ret);
887     qobject_unref(resp);
888     g_free(cmd);
889     return ret;
890 }
891 
892 char *qtest_hmp(QTestState *s, const char *fmt, ...)
893 {
894     va_list ap;
895     char *ret;
896 
897     va_start(ap, fmt);
898     ret = qtest_vhmp(s, fmt, ap);
899     va_end(ap);
900     return ret;
901 }
902 
903 const char *qtest_get_arch(void)
904 {
905     const char *qemu = qtest_qemu_binary();
906     const char *end = strrchr(qemu, '-');
907 
908     if (!end) {
909         fprintf(stderr, "Can't determine architecture from binary name.\n");
910         exit(1);
911     }
912 
913     if (!strstr(qemu, "-system-")) {
914         fprintf(stderr, "QTEST_QEMU_BINARY must end with *-system-<arch> "
915                 "where 'arch' is the target\narchitecture (x86_64, aarch64, "
916                 "etc).\n");
917         exit(1);
918     }
919 
920     return end + 1;
921 }
922 
923 bool qtest_get_irq(QTestState *s, int num)
924 {
925     /* dummy operation in order to make sure irq is up to date */
926     qtest_inb(s, 0);
927 
928     return s->irq_level[num];
929 }
930 
931 void qtest_module_load(QTestState *s, const char *prefix, const char *libname)
932 {
933     qtest_sendf(s, "module_load %s %s\n", prefix, libname);
934     qtest_rsp(s);
935 }
936 
937 static int64_t qtest_clock_rsp(QTestState *s)
938 {
939     gchar **words;
940     int64_t clock;
941     words = qtest_rsp_args(s, 2);
942     clock = g_ascii_strtoll(words[1], NULL, 0);
943     g_strfreev(words);
944     return clock;
945 }
946 
947 int64_t qtest_clock_step_next(QTestState *s)
948 {
949     qtest_sendf(s, "clock_step\n");
950     return qtest_clock_rsp(s);
951 }
952 
953 int64_t qtest_clock_step(QTestState *s, int64_t step)
954 {
955     qtest_sendf(s, "clock_step %"PRIi64"\n", step);
956     return qtest_clock_rsp(s);
957 }
958 
959 int64_t qtest_clock_set(QTestState *s, int64_t val)
960 {
961     qtest_sendf(s, "clock_set %"PRIi64"\n", val);
962     return qtest_clock_rsp(s);
963 }
964 
965 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
966 {
967     qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
968     qtest_rsp(s);
969 }
970 
971 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
972 {
973     qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
974     qtest_rsp(s);
975 }
976 
977 void qtest_set_irq_in(QTestState *s, const char *qom_path, const char *name,
978                       int num, int level)
979 {
980     if (!name) {
981         name = "unnamed-gpio-in";
982     }
983     qtest_sendf(s, "set_irq_in %s %s %d %d\n", qom_path, name, num, level);
984     qtest_rsp(s);
985 }
986 
987 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
988 {
989     qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
990     qtest_rsp(s);
991 }
992 
993 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
994 {
995     qtest_out(s, "outb", addr, value);
996 }
997 
998 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
999 {
1000     qtest_out(s, "outw", addr, value);
1001 }
1002 
1003 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
1004 {
1005     qtest_out(s, "outl", addr, value);
1006 }
1007 
1008 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
1009 {
1010     gchar **args;
1011     int ret;
1012     unsigned long value;
1013 
1014     qtest_sendf(s, "%s 0x%x\n", cmd, addr);
1015     args = qtest_rsp_args(s, 2);
1016     ret = qemu_strtoul(args[1], NULL, 0, &value);
1017     g_assert(!ret && value <= UINT32_MAX);
1018     g_strfreev(args);
1019 
1020     return value;
1021 }
1022 
1023 uint8_t qtest_inb(QTestState *s, uint16_t addr)
1024 {
1025     return qtest_in(s, "inb", addr);
1026 }
1027 
1028 uint16_t qtest_inw(QTestState *s, uint16_t addr)
1029 {
1030     return qtest_in(s, "inw", addr);
1031 }
1032 
1033 uint32_t qtest_inl(QTestState *s, uint16_t addr)
1034 {
1035     return qtest_in(s, "inl", addr);
1036 }
1037 
1038 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
1039                         uint64_t value)
1040 {
1041     qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
1042     qtest_rsp(s);
1043 }
1044 
1045 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
1046 {
1047     qtest_write(s, "writeb", addr, value);
1048 }
1049 
1050 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
1051 {
1052     qtest_write(s, "writew", addr, value);
1053 }
1054 
1055 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
1056 {
1057     qtest_write(s, "writel", addr, value);
1058 }
1059 
1060 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
1061 {
1062     qtest_write(s, "writeq", addr, value);
1063 }
1064 
1065 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
1066 {
1067     gchar **args;
1068     int ret;
1069     uint64_t value;
1070 
1071     qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
1072     args = qtest_rsp_args(s, 2);
1073     ret = qemu_strtou64(args[1], NULL, 0, &value);
1074     g_assert(!ret);
1075     g_strfreev(args);
1076 
1077     return value;
1078 }
1079 
1080 uint8_t qtest_readb(QTestState *s, uint64_t addr)
1081 {
1082     return qtest_read(s, "readb", addr);
1083 }
1084 
1085 uint16_t qtest_readw(QTestState *s, uint64_t addr)
1086 {
1087     return qtest_read(s, "readw", addr);
1088 }
1089 
1090 uint32_t qtest_readl(QTestState *s, uint64_t addr)
1091 {
1092     return qtest_read(s, "readl", addr);
1093 }
1094 
1095 uint64_t qtest_readq(QTestState *s, uint64_t addr)
1096 {
1097     return qtest_read(s, "readq", addr);
1098 }
1099 
1100 static int hex2nib(char ch)
1101 {
1102     if (ch >= '0' && ch <= '9') {
1103         return ch - '0';
1104     } else if (ch >= 'a' && ch <= 'f') {
1105         return 10 + (ch - 'a');
1106     } else if (ch >= 'A' && ch <= 'F') {
1107         return 10 + (ch - 'a');
1108     } else {
1109         return -1;
1110     }
1111 }
1112 
1113 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
1114 {
1115     uint8_t *ptr = data;
1116     gchar **args;
1117     size_t i;
1118 
1119     if (!size) {
1120         return;
1121     }
1122 
1123     qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
1124     args = qtest_rsp_args(s, 2);
1125 
1126     for (i = 0; i < size; i++) {
1127         ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
1128         ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
1129     }
1130 
1131     g_strfreev(args);
1132 }
1133 
1134 uint64_t qtest_rtas_call(QTestState *s, const char *name,
1135                          uint32_t nargs, uint64_t args,
1136                          uint32_t nret, uint64_t ret)
1137 {
1138     qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
1139                 name, nargs, args, nret, ret);
1140     qtest_rsp(s);
1141     return 0;
1142 }
1143 
1144 void qtest_add_func(const char *str, void (*fn)(void))
1145 {
1146     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1147     g_test_add_func(path, fn);
1148     g_free(path);
1149 }
1150 
1151 void qtest_add_data_func_full(const char *str, void *data,
1152                               void (*fn)(const void *),
1153                               GDestroyNotify data_free_func)
1154 {
1155     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1156     g_test_add_data_func_full(path, data, fn, data_free_func);
1157     g_free(path);
1158 }
1159 
1160 void qtest_add_data_func(const char *str, const void *data,
1161                          void (*fn)(const void *))
1162 {
1163     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1164     g_test_add_data_func(path, data, fn);
1165     g_free(path);
1166 }
1167 
1168 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
1169 {
1170     gchar *bdata;
1171 
1172     bdata = g_base64_encode(data, size);
1173     qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
1174     s->ops.send(s, bdata);
1175     s->ops.send(s, "\n");
1176     qtest_rsp(s);
1177     g_free(bdata);
1178 }
1179 
1180 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
1181 {
1182     gchar **args;
1183     size_t len;
1184 
1185     qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
1186     args = qtest_rsp_args(s, 2);
1187 
1188     g_base64_decode_inplace(args[1], &len);
1189     if (size != len) {
1190         fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
1191                 size, len);
1192         len = MIN(len, size);
1193     }
1194 
1195     memcpy(data, args[1], len);
1196     g_strfreev(args);
1197 }
1198 
1199 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
1200 {
1201     const uint8_t *ptr = data;
1202     size_t i;
1203     char *enc;
1204 
1205     if (!size) {
1206         return;
1207     }
1208 
1209     enc = g_malloc(2 * size + 1);
1210 
1211     for (i = 0; i < size; i++) {
1212         sprintf(&enc[i * 2], "%02x", ptr[i]);
1213     }
1214 
1215     qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
1216     qtest_rsp(s);
1217     g_free(enc);
1218 }
1219 
1220 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
1221 {
1222     qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
1223     qtest_rsp(s);
1224 }
1225 
1226 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
1227 {
1228     va_list ap;
1229     QDict *response;
1230 
1231     va_start(ap, fmt);
1232     response = qtest_vqmp(qts, fmt, ap);
1233     va_end(ap);
1234 
1235     g_assert(response);
1236     if (!qdict_haskey(response, "return")) {
1237         GString *s = qobject_to_json_pretty(QOBJECT(response), true);
1238         g_test_message("%s", s->str);
1239         g_string_free(s, true);
1240     }
1241     g_assert(qdict_haskey(response, "return"));
1242     qobject_unref(response);
1243 }
1244 
1245 bool qtest_big_endian(QTestState *s)
1246 {
1247     return s->big_endian;
1248 }
1249 
1250 static bool qtest_check_machine_version(const char *mname, const char *basename,
1251                                         int major, int minor)
1252 {
1253     char *newname;
1254     bool is_equal;
1255 
1256     newname = g_strdup_printf("%s-%i.%i", basename, major, minor);
1257     is_equal = g_str_equal(mname, newname);
1258     g_free(newname);
1259 
1260     return is_equal;
1261 }
1262 
1263 static bool qtest_is_old_versioned_machine(const char *mname)
1264 {
1265     const char *dash = strrchr(mname, '-');
1266     const char *dot = strrchr(mname, '.');
1267     const char *chr;
1268     char *bname;
1269     const int major = QEMU_VERSION_MAJOR;
1270     const int minor = QEMU_VERSION_MINOR;
1271     bool res = false;
1272 
1273     if (dash && dot && dot > dash) {
1274         for (chr = dash + 1; *chr; chr++) {
1275             if (!qemu_isdigit(*chr) && *chr != '.') {
1276                 return false;
1277             }
1278         }
1279         /*
1280          * Now check if it is one of the latest versions. Check major + 1
1281          * and minor + 1 versions as well, since they might already exist
1282          * in the development branch.
1283          */
1284         bname = g_strdup(mname);
1285         bname[dash - mname] = 0;
1286         res = !qtest_check_machine_version(mname, bname, major + 1, 0) &&
1287               !qtest_check_machine_version(mname, bname, major, minor + 1) &&
1288               !qtest_check_machine_version(mname, bname, major, minor);
1289         g_free(bname);
1290     }
1291 
1292     return res;
1293 }
1294 
1295 void qtest_cb_for_every_machine(void (*cb)(const char *machine),
1296                                 bool skip_old_versioned)
1297 {
1298     QDict *response, *minfo;
1299     QList *list;
1300     const QListEntry *p;
1301     QObject *qobj;
1302     QString *qstr;
1303     const char *mname;
1304     QTestState *qts;
1305 
1306     qts = qtest_init("-machine none");
1307     response = qtest_qmp(qts, "{ 'execute': 'query-machines' }");
1308     g_assert(response);
1309     list = qdict_get_qlist(response, "return");
1310     g_assert(list);
1311 
1312     for (p = qlist_first(list); p; p = qlist_next(p)) {
1313         minfo = qobject_to(QDict, qlist_entry_obj(p));
1314         g_assert(minfo);
1315         qobj = qdict_get(minfo, "name");
1316         g_assert(qobj);
1317         qstr = qobject_to(QString, qobj);
1318         g_assert(qstr);
1319         mname = qstring_get_str(qstr);
1320         /* Ignore machines that cannot be used for qtests */
1321         if (!strncmp("xenfv", mname, 5) || g_str_equal("xenpv", mname)) {
1322             continue;
1323         }
1324         if (!skip_old_versioned || !qtest_is_old_versioned_machine(mname)) {
1325             cb(mname);
1326         }
1327     }
1328 
1329     qtest_quit(qts);
1330     qobject_unref(response);
1331 }
1332 
1333 /*
1334  * Generic hot-plugging test via the device_add QMP commands.
1335  */
1336 void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv,
1337                                 const QDict *arguments)
1338 {
1339     QDict *resp;
1340     QDict *args = arguments ? qdict_clone_shallow(arguments) : qdict_new();
1341 
1342     g_assert(!qdict_haskey(args, "driver"));
1343     qdict_put_str(args, "driver", drv);
1344     resp = qtest_qmp(qts, "{'execute': 'device_add', 'arguments': %p}", args);
1345     g_assert(resp);
1346     g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */
1347     g_assert(!qdict_haskey(resp, "error"));
1348     qobject_unref(resp);
1349 }
1350 
1351 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
1352                           const char *fmt, ...)
1353 {
1354     QDict *args;
1355     va_list ap;
1356 
1357     va_start(ap, fmt);
1358     args = qdict_from_vjsonf_nofail(fmt, ap);
1359     va_end(ap);
1360 
1361     g_assert(!qdict_haskey(args, "id"));
1362     qdict_put_str(args, "id", id);
1363 
1364     qtest_qmp_device_add_qdict(qts, driver, args);
1365     qobject_unref(args);
1366 }
1367 
1368 
1369 /*
1370  * Generic hot-unplugging test via the device_del QMP command.
1371  * Device deletion will get one response and one event. For example:
1372  *
1373  * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1374  *
1375  * will get this one:
1376  *
1377  * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1378  *  "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1379  *  "path": "/machine/peripheral/scsi-hd"}}
1380  *
1381  * and this one:
1382  *
1383  * {"return": {}}
1384  */
1385 void qtest_qmp_device_del(QTestState *qts, const char *id)
1386 {
1387     QDict *rsp;
1388 
1389     rsp = qtest_qmp(qts, "{'execute': 'device_del', 'arguments': {'id': %s}}",
1390                     id);
1391 
1392     g_assert(qdict_haskey(rsp, "return"));
1393     qobject_unref(rsp);
1394     qtest_qmp_eventwait(qts, "DEVICE_DELETED");
1395 }
1396 
1397 bool qmp_rsp_is_err(QDict *rsp)
1398 {
1399     QDict *error = qdict_get_qdict(rsp, "error");
1400     qobject_unref(rsp);
1401     return !!error;
1402 }
1403 
1404 void qmp_expect_error_and_unref(QDict *rsp, const char *class)
1405 {
1406     QDict *error = qdict_get_qdict(rsp, "error");
1407 
1408     g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class);
1409     g_assert_nonnull(qdict_get_try_str(error, "desc"));
1410     g_assert(!qdict_haskey(rsp, "return"));
1411 
1412     qobject_unref(rsp);
1413 }
1414 
1415 static void qtest_client_set_tx_handler(QTestState *s,
1416                     QTestSendFn send)
1417 {
1418     s->ops.send = send;
1419 }
1420 static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv)
1421 {
1422     s->ops.recv_line = recv;
1423 }
1424 /* A type-safe wrapper for s->send() */
1425 static void send_wrapper(QTestState *s, const char *buf)
1426 {
1427     s->ops.external_send(s, buf);
1428 }
1429 
1430 static GString *qtest_client_inproc_recv_line(QTestState *s)
1431 {
1432     GString *line;
1433     size_t offset;
1434     char *eol;
1435 
1436     eol = strchr(s->rx->str, '\n');
1437     offset = eol - s->rx->str;
1438     line = g_string_new_len(s->rx->str, offset);
1439     g_string_erase(s->rx, 0, offset + 1);
1440     return line;
1441 }
1442 
1443 QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch,
1444                     void (*send)(void*, const char*))
1445 {
1446     QTestState *qts;
1447     qts = g_new0(QTestState, 1);
1448     qts->pending_events = NULL;
1449     *s = qts; /* Expose qts early on, since the query endianness relies on it */
1450     qts->wstatus = 0;
1451     for (int i = 0; i < MAX_IRQ; i++) {
1452         qts->irq_level[i] = false;
1453     }
1454 
1455     qtest_client_set_rx_handler(qts, qtest_client_inproc_recv_line);
1456 
1457     /* send() may not have a matching protoype, so use a type-safe wrapper */
1458     qts->ops.external_send = send;
1459     qtest_client_set_tx_handler(qts, send_wrapper);
1460 
1461     qts->big_endian = qtest_query_target_endianness(qts);
1462 
1463     /*
1464      * Set a dummy path for QTEST_QEMU_BINARY. Doesn't need to exist, but this
1465      * way, qtest_get_arch works for inproc qtest.
1466      */
1467     gchar *bin_path = g_strconcat("/qemu-system-", arch, NULL);
1468     setenv("QTEST_QEMU_BINARY", bin_path, 0);
1469     g_free(bin_path);
1470 
1471     return qts;
1472 }
1473 
1474 void qtest_client_inproc_recv(void *opaque, const char *str)
1475 {
1476     QTestState *qts = *(QTestState **)opaque;
1477 
1478     if (!qts->rx) {
1479         qts->rx = g_string_new(NULL);
1480     }
1481     g_string_append(qts->rx, str);
1482     return;
1483 }
1484