xref: /openbmc/qemu/tests/qtest/libqtest.c (revision 835fde4a)
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         abort();
911     }
912 
913     return end + 1;
914 }
915 
916 bool qtest_get_irq(QTestState *s, int num)
917 {
918     /* dummy operation in order to make sure irq is up to date */
919     qtest_inb(s, 0);
920 
921     return s->irq_level[num];
922 }
923 
924 void qtest_module_load(QTestState *s, const char *prefix, const char *libname)
925 {
926     qtest_sendf(s, "module_load %s %s\n", prefix, libname);
927     qtest_rsp(s);
928 }
929 
930 static int64_t qtest_clock_rsp(QTestState *s)
931 {
932     gchar **words;
933     int64_t clock;
934     words = qtest_rsp_args(s, 2);
935     clock = g_ascii_strtoll(words[1], NULL, 0);
936     g_strfreev(words);
937     return clock;
938 }
939 
940 int64_t qtest_clock_step_next(QTestState *s)
941 {
942     qtest_sendf(s, "clock_step\n");
943     return qtest_clock_rsp(s);
944 }
945 
946 int64_t qtest_clock_step(QTestState *s, int64_t step)
947 {
948     qtest_sendf(s, "clock_step %"PRIi64"\n", step);
949     return qtest_clock_rsp(s);
950 }
951 
952 int64_t qtest_clock_set(QTestState *s, int64_t val)
953 {
954     qtest_sendf(s, "clock_set %"PRIi64"\n", val);
955     return qtest_clock_rsp(s);
956 }
957 
958 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
959 {
960     qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
961     qtest_rsp(s);
962 }
963 
964 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
965 {
966     qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
967     qtest_rsp(s);
968 }
969 
970 void qtest_set_irq_in(QTestState *s, const char *qom_path, const char *name,
971                       int num, int level)
972 {
973     if (!name) {
974         name = "unnamed-gpio-in";
975     }
976     qtest_sendf(s, "set_irq_in %s %s %d %d\n", qom_path, name, num, level);
977     qtest_rsp(s);
978 }
979 
980 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
981 {
982     qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
983     qtest_rsp(s);
984 }
985 
986 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
987 {
988     qtest_out(s, "outb", addr, value);
989 }
990 
991 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
992 {
993     qtest_out(s, "outw", addr, value);
994 }
995 
996 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
997 {
998     qtest_out(s, "outl", addr, value);
999 }
1000 
1001 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
1002 {
1003     gchar **args;
1004     int ret;
1005     unsigned long value;
1006 
1007     qtest_sendf(s, "%s 0x%x\n", cmd, addr);
1008     args = qtest_rsp_args(s, 2);
1009     ret = qemu_strtoul(args[1], NULL, 0, &value);
1010     g_assert(!ret && value <= UINT32_MAX);
1011     g_strfreev(args);
1012 
1013     return value;
1014 }
1015 
1016 uint8_t qtest_inb(QTestState *s, uint16_t addr)
1017 {
1018     return qtest_in(s, "inb", addr);
1019 }
1020 
1021 uint16_t qtest_inw(QTestState *s, uint16_t addr)
1022 {
1023     return qtest_in(s, "inw", addr);
1024 }
1025 
1026 uint32_t qtest_inl(QTestState *s, uint16_t addr)
1027 {
1028     return qtest_in(s, "inl", addr);
1029 }
1030 
1031 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
1032                         uint64_t value)
1033 {
1034     qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
1035     qtest_rsp(s);
1036 }
1037 
1038 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
1039 {
1040     qtest_write(s, "writeb", addr, value);
1041 }
1042 
1043 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
1044 {
1045     qtest_write(s, "writew", addr, value);
1046 }
1047 
1048 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
1049 {
1050     qtest_write(s, "writel", addr, value);
1051 }
1052 
1053 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
1054 {
1055     qtest_write(s, "writeq", addr, value);
1056 }
1057 
1058 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
1059 {
1060     gchar **args;
1061     int ret;
1062     uint64_t value;
1063 
1064     qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
1065     args = qtest_rsp_args(s, 2);
1066     ret = qemu_strtou64(args[1], NULL, 0, &value);
1067     g_assert(!ret);
1068     g_strfreev(args);
1069 
1070     return value;
1071 }
1072 
1073 uint8_t qtest_readb(QTestState *s, uint64_t addr)
1074 {
1075     return qtest_read(s, "readb", addr);
1076 }
1077 
1078 uint16_t qtest_readw(QTestState *s, uint64_t addr)
1079 {
1080     return qtest_read(s, "readw", addr);
1081 }
1082 
1083 uint32_t qtest_readl(QTestState *s, uint64_t addr)
1084 {
1085     return qtest_read(s, "readl", addr);
1086 }
1087 
1088 uint64_t qtest_readq(QTestState *s, uint64_t addr)
1089 {
1090     return qtest_read(s, "readq", addr);
1091 }
1092 
1093 static int hex2nib(char ch)
1094 {
1095     if (ch >= '0' && ch <= '9') {
1096         return ch - '0';
1097     } else if (ch >= 'a' && ch <= 'f') {
1098         return 10 + (ch - 'a');
1099     } else if (ch >= 'A' && ch <= 'F') {
1100         return 10 + (ch - 'a');
1101     } else {
1102         return -1;
1103     }
1104 }
1105 
1106 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
1107 {
1108     uint8_t *ptr = data;
1109     gchar **args;
1110     size_t i;
1111 
1112     if (!size) {
1113         return;
1114     }
1115 
1116     qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
1117     args = qtest_rsp_args(s, 2);
1118 
1119     for (i = 0; i < size; i++) {
1120         ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
1121         ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
1122     }
1123 
1124     g_strfreev(args);
1125 }
1126 
1127 uint64_t qtest_rtas_call(QTestState *s, const char *name,
1128                          uint32_t nargs, uint64_t args,
1129                          uint32_t nret, uint64_t ret)
1130 {
1131     qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
1132                 name, nargs, args, nret, ret);
1133     qtest_rsp(s);
1134     return 0;
1135 }
1136 
1137 void qtest_add_func(const char *str, void (*fn)(void))
1138 {
1139     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1140     g_test_add_func(path, fn);
1141     g_free(path);
1142 }
1143 
1144 void qtest_add_data_func_full(const char *str, void *data,
1145                               void (*fn)(const void *),
1146                               GDestroyNotify data_free_func)
1147 {
1148     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1149     g_test_add_data_func_full(path, data, fn, data_free_func);
1150     g_free(path);
1151 }
1152 
1153 void qtest_add_data_func(const char *str, const void *data,
1154                          void (*fn)(const void *))
1155 {
1156     gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
1157     g_test_add_data_func(path, data, fn);
1158     g_free(path);
1159 }
1160 
1161 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
1162 {
1163     gchar *bdata;
1164 
1165     bdata = g_base64_encode(data, size);
1166     qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
1167     s->ops.send(s, bdata);
1168     s->ops.send(s, "\n");
1169     qtest_rsp(s);
1170     g_free(bdata);
1171 }
1172 
1173 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
1174 {
1175     gchar **args;
1176     size_t len;
1177 
1178     qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
1179     args = qtest_rsp_args(s, 2);
1180 
1181     g_base64_decode_inplace(args[1], &len);
1182     if (size != len) {
1183         fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
1184                 size, len);
1185         len = MIN(len, size);
1186     }
1187 
1188     memcpy(data, args[1], len);
1189     g_strfreev(args);
1190 }
1191 
1192 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
1193 {
1194     const uint8_t *ptr = data;
1195     size_t i;
1196     char *enc;
1197 
1198     if (!size) {
1199         return;
1200     }
1201 
1202     enc = g_malloc(2 * size + 1);
1203 
1204     for (i = 0; i < size; i++) {
1205         sprintf(&enc[i * 2], "%02x", ptr[i]);
1206     }
1207 
1208     qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
1209     qtest_rsp(s);
1210     g_free(enc);
1211 }
1212 
1213 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
1214 {
1215     qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
1216     qtest_rsp(s);
1217 }
1218 
1219 void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
1220 {
1221     va_list ap;
1222     QDict *response;
1223 
1224     va_start(ap, fmt);
1225     response = qtest_vqmp(qts, fmt, ap);
1226     va_end(ap);
1227 
1228     g_assert(response);
1229     if (!qdict_haskey(response, "return")) {
1230         GString *s = qobject_to_json_pretty(QOBJECT(response), true);
1231         g_test_message("%s", s->str);
1232         g_string_free(s, true);
1233     }
1234     g_assert(qdict_haskey(response, "return"));
1235     qobject_unref(response);
1236 }
1237 
1238 bool qtest_big_endian(QTestState *s)
1239 {
1240     return s->big_endian;
1241 }
1242 
1243 static bool qtest_check_machine_version(const char *mname, const char *basename,
1244                                         int major, int minor)
1245 {
1246     char *newname;
1247     bool is_equal;
1248 
1249     newname = g_strdup_printf("%s-%i.%i", basename, major, minor);
1250     is_equal = g_str_equal(mname, newname);
1251     g_free(newname);
1252 
1253     return is_equal;
1254 }
1255 
1256 static bool qtest_is_old_versioned_machine(const char *mname)
1257 {
1258     const char *dash = strrchr(mname, '-');
1259     const char *dot = strrchr(mname, '.');
1260     const char *chr;
1261     char *bname;
1262     const int major = QEMU_VERSION_MAJOR;
1263     const int minor = QEMU_VERSION_MINOR;
1264     bool res = false;
1265 
1266     if (dash && dot && dot > dash) {
1267         for (chr = dash + 1; *chr; chr++) {
1268             if (!qemu_isdigit(*chr) && *chr != '.') {
1269                 return false;
1270             }
1271         }
1272         /*
1273          * Now check if it is one of the latest versions. Check major + 1
1274          * and minor + 1 versions as well, since they might already exist
1275          * in the development branch.
1276          */
1277         bname = g_strdup(mname);
1278         bname[dash - mname] = 0;
1279         res = !qtest_check_machine_version(mname, bname, major + 1, 0) &&
1280               !qtest_check_machine_version(mname, bname, major, minor + 1) &&
1281               !qtest_check_machine_version(mname, bname, major, minor);
1282         g_free(bname);
1283     }
1284 
1285     return res;
1286 }
1287 
1288 void qtest_cb_for_every_machine(void (*cb)(const char *machine),
1289                                 bool skip_old_versioned)
1290 {
1291     QDict *response, *minfo;
1292     QList *list;
1293     const QListEntry *p;
1294     QObject *qobj;
1295     QString *qstr;
1296     const char *mname;
1297     QTestState *qts;
1298 
1299     qts = qtest_init("-machine none");
1300     response = qtest_qmp(qts, "{ 'execute': 'query-machines' }");
1301     g_assert(response);
1302     list = qdict_get_qlist(response, "return");
1303     g_assert(list);
1304 
1305     for (p = qlist_first(list); p; p = qlist_next(p)) {
1306         minfo = qobject_to(QDict, qlist_entry_obj(p));
1307         g_assert(minfo);
1308         qobj = qdict_get(minfo, "name");
1309         g_assert(qobj);
1310         qstr = qobject_to(QString, qobj);
1311         g_assert(qstr);
1312         mname = qstring_get_str(qstr);
1313         /* Ignore machines that cannot be used for qtests */
1314         if (!strncmp("xenfv", mname, 5) || g_str_equal("xenpv", mname)) {
1315             continue;
1316         }
1317         if (!skip_old_versioned || !qtest_is_old_versioned_machine(mname)) {
1318             cb(mname);
1319         }
1320     }
1321 
1322     qtest_quit(qts);
1323     qobject_unref(response);
1324 }
1325 
1326 /*
1327  * Generic hot-plugging test via the device_add QMP commands.
1328  */
1329 void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv,
1330                                 const QDict *arguments)
1331 {
1332     QDict *resp;
1333     QDict *args = arguments ? qdict_clone_shallow(arguments) : qdict_new();
1334 
1335     g_assert(!qdict_haskey(args, "driver"));
1336     qdict_put_str(args, "driver", drv);
1337     resp = qtest_qmp(qts, "{'execute': 'device_add', 'arguments': %p}", args);
1338     g_assert(resp);
1339     g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */
1340     g_assert(!qdict_haskey(resp, "error"));
1341     qobject_unref(resp);
1342 }
1343 
1344 void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
1345                           const char *fmt, ...)
1346 {
1347     QDict *args;
1348     va_list ap;
1349 
1350     va_start(ap, fmt);
1351     args = qdict_from_vjsonf_nofail(fmt, ap);
1352     va_end(ap);
1353 
1354     g_assert(!qdict_haskey(args, "id"));
1355     qdict_put_str(args, "id", id);
1356 
1357     qtest_qmp_device_add_qdict(qts, driver, args);
1358     qobject_unref(args);
1359 }
1360 
1361 
1362 /*
1363  * Generic hot-unplugging test via the device_del QMP command.
1364  * Device deletion will get one response and one event. For example:
1365  *
1366  * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1367  *
1368  * will get this one:
1369  *
1370  * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1371  *  "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1372  *  "path": "/machine/peripheral/scsi-hd"}}
1373  *
1374  * and this one:
1375  *
1376  * {"return": {}}
1377  */
1378 void qtest_qmp_device_del(QTestState *qts, const char *id)
1379 {
1380     QDict *rsp;
1381 
1382     rsp = qtest_qmp(qts, "{'execute': 'device_del', 'arguments': {'id': %s}}",
1383                     id);
1384 
1385     g_assert(qdict_haskey(rsp, "return"));
1386     qobject_unref(rsp);
1387     qtest_qmp_eventwait(qts, "DEVICE_DELETED");
1388 }
1389 
1390 bool qmp_rsp_is_err(QDict *rsp)
1391 {
1392     QDict *error = qdict_get_qdict(rsp, "error");
1393     qobject_unref(rsp);
1394     return !!error;
1395 }
1396 
1397 void qmp_expect_error_and_unref(QDict *rsp, const char *class)
1398 {
1399     QDict *error = qdict_get_qdict(rsp, "error");
1400 
1401     g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, class);
1402     g_assert_nonnull(qdict_get_try_str(error, "desc"));
1403     g_assert(!qdict_haskey(rsp, "return"));
1404 
1405     qobject_unref(rsp);
1406 }
1407 
1408 static void qtest_client_set_tx_handler(QTestState *s,
1409                     QTestSendFn send)
1410 {
1411     s->ops.send = send;
1412 }
1413 static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv)
1414 {
1415     s->ops.recv_line = recv;
1416 }
1417 /* A type-safe wrapper for s->send() */
1418 static void send_wrapper(QTestState *s, const char *buf)
1419 {
1420     s->ops.external_send(s, buf);
1421 }
1422 
1423 static GString *qtest_client_inproc_recv_line(QTestState *s)
1424 {
1425     GString *line;
1426     size_t offset;
1427     char *eol;
1428 
1429     eol = strchr(s->rx->str, '\n');
1430     offset = eol - s->rx->str;
1431     line = g_string_new_len(s->rx->str, offset);
1432     g_string_erase(s->rx, 0, offset + 1);
1433     return line;
1434 }
1435 
1436 QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch,
1437                     void (*send)(void*, const char*))
1438 {
1439     QTestState *qts;
1440     qts = g_new0(QTestState, 1);
1441     qts->pending_events = NULL;
1442     *s = qts; /* Expose qts early on, since the query endianness relies on it */
1443     qts->wstatus = 0;
1444     for (int i = 0; i < MAX_IRQ; i++) {
1445         qts->irq_level[i] = false;
1446     }
1447 
1448     qtest_client_set_rx_handler(qts, qtest_client_inproc_recv_line);
1449 
1450     /* send() may not have a matching protoype, so use a type-safe wrapper */
1451     qts->ops.external_send = send;
1452     qtest_client_set_tx_handler(qts, send_wrapper);
1453 
1454     qts->big_endian = qtest_query_target_endianness(qts);
1455 
1456     /*
1457      * Set a dummy path for QTEST_QEMU_BINARY. Doesn't need to exist, but this
1458      * way, qtest_get_arch works for inproc qtest.
1459      */
1460     gchar *bin_path = g_strconcat("/qemu-system-", arch, NULL);
1461     setenv("QTEST_QEMU_BINARY", bin_path, 0);
1462     g_free(bin_path);
1463 
1464     return qts;
1465 }
1466 
1467 void qtest_client_inproc_recv(void *opaque, const char *str)
1468 {
1469     QTestState *qts = *(QTestState **)opaque;
1470 
1471     if (!qts->rx) {
1472         qts->rx = g_string_new(NULL);
1473     }
1474     g_string_append(qts->rx, str);
1475     return;
1476 }
1477