cuda: add to bridge category
[qemu/ar7.git] / tests / libqtest.c
blobb6d700c606fad8e0e75d8d5e2213b6676538d90f
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
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.
17 #include "libqtest.h"
19 #include <glib.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/un.h>
24 #include <inttypes.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33 #include "qapi/qmp/json-parser.h"
34 #include "qapi/qmp/json-streamer.h"
35 #include "qapi/qmp/qjson.h"
37 #define MAX_IRQ 256
38 #define SOCKET_TIMEOUT 5
40 QTestState *global_qtest;
42 struct QTestState
44 int fd;
45 int qmp_fd;
46 bool irq_level[MAX_IRQ];
47 GString *rx;
48 pid_t qemu_pid; /* our child QEMU process */
51 static GList *qtest_instances;
52 static struct sigaction sigact_old;
54 #define g_assert_no_errno(ret) do { \
55 g_assert_cmpint(ret, !=, -1); \
56 } while (0)
58 static int init_socket(const char *socket_path)
60 struct sockaddr_un addr;
61 int sock;
62 int ret;
64 sock = socket(PF_UNIX, SOCK_STREAM, 0);
65 g_assert_no_errno(sock);
67 addr.sun_family = AF_UNIX;
68 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
69 qemu_set_cloexec(sock);
71 do {
72 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
73 } while (ret == -1 && errno == EINTR);
74 g_assert_no_errno(ret);
75 ret = listen(sock, 1);
76 g_assert_no_errno(ret);
78 return sock;
81 static int socket_accept(int sock)
83 struct sockaddr_un addr;
84 socklen_t addrlen;
85 int ret;
86 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
87 .tv_usec = 0 };
89 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
90 sizeof(timeout));
92 do {
93 addrlen = sizeof(addr);
94 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
95 } while (ret == -1 && errno == EINTR);
96 if (ret == -1) {
97 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
99 close(sock);
101 return ret;
104 static void kill_qemu(QTestState *s)
106 if (s->qemu_pid != -1) {
107 kill(s->qemu_pid, SIGTERM);
108 waitpid(s->qemu_pid, NULL, 0);
112 static void sigabrt_handler(int signo)
114 GList *elem;
115 for (elem = qtest_instances; elem; elem = elem->next) {
116 kill_qemu(elem->data);
120 static void setup_sigabrt_handler(void)
122 struct sigaction sigact;
124 /* Catch SIGABRT to clean up on g_assert() failure */
125 sigact = (struct sigaction){
126 .sa_handler = sigabrt_handler,
127 .sa_flags = SA_RESETHAND,
129 sigemptyset(&sigact.sa_mask);
130 sigaction(SIGABRT, &sigact, &sigact_old);
133 static void cleanup_sigabrt_handler(void)
135 sigaction(SIGABRT, &sigact_old, NULL);
138 QTestState *qtest_init(const char *extra_args)
140 QTestState *s;
141 int sock, qmpsock, i;
142 gchar *socket_path;
143 gchar *qmp_socket_path;
144 gchar *command;
145 const char *qemu_binary;
147 qemu_binary = getenv("QTEST_QEMU_BINARY");
148 g_assert(qemu_binary != NULL);
150 s = g_malloc(sizeof(*s));
152 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
153 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
155 sock = init_socket(socket_path);
156 qmpsock = init_socket(qmp_socket_path);
158 /* Only install SIGABRT handler once */
159 if (!qtest_instances) {
160 setup_sigabrt_handler();
163 qtest_instances = g_list_prepend(qtest_instances, s);
165 s->qemu_pid = fork();
166 if (s->qemu_pid == 0) {
167 setenv("QEMU_AUDIO_DRV", "none", true);
168 command = g_strdup_printf("exec %s "
169 "-qtest unix:%s,nowait "
170 "-qtest-log %s "
171 "-qmp unix:%s,nowait "
172 "-machine accel=qtest "
173 "-display none "
174 "%s", qemu_binary, socket_path,
175 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
176 qmp_socket_path,
177 extra_args ?: "");
178 execlp("/bin/sh", "sh", "-c", command, NULL);
179 exit(1);
182 s->fd = socket_accept(sock);
183 if (s->fd >= 0) {
184 s->qmp_fd = socket_accept(qmpsock);
186 unlink(socket_path);
187 unlink(qmp_socket_path);
188 g_free(socket_path);
189 g_free(qmp_socket_path);
191 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
193 s->rx = g_string_new("");
194 for (i = 0; i < MAX_IRQ; i++) {
195 s->irq_level[i] = false;
198 /* Read the QMP greeting and then do the handshake */
199 qtest_qmp_discard_response(s, "");
200 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
202 if (getenv("QTEST_STOP")) {
203 kill(s->qemu_pid, SIGSTOP);
206 return s;
209 void qtest_quit(QTestState *s)
211 /* Uninstall SIGABRT handler on last instance */
212 if (qtest_instances && !qtest_instances->next) {
213 cleanup_sigabrt_handler();
216 qtest_instances = g_list_remove(qtest_instances, s);
218 kill_qemu(s);
219 close(s->fd);
220 close(s->qmp_fd);
221 g_string_free(s->rx, true);
222 g_free(s);
225 static void socket_send(int fd, const char *buf, size_t size)
227 size_t offset;
229 offset = 0;
230 while (offset < size) {
231 ssize_t len;
233 len = write(fd, buf + offset, size - offset);
234 if (len == -1 && errno == EINTR) {
235 continue;
238 g_assert_no_errno(len);
239 g_assert_cmpint(len, >, 0);
241 offset += len;
245 static void socket_sendf(int fd, const char *fmt, va_list ap)
247 gchar *str = g_strdup_vprintf(fmt, ap);
248 size_t size = strlen(str);
250 socket_send(fd, str, size);
251 g_free(str);
254 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
256 va_list ap;
258 va_start(ap, fmt);
259 socket_sendf(s->fd, fmt, ap);
260 va_end(ap);
263 static GString *qtest_recv_line(QTestState *s)
265 GString *line;
266 size_t offset;
267 char *eol;
269 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
270 ssize_t len;
271 char buffer[1024];
273 len = read(s->fd, buffer, sizeof(buffer));
274 if (len == -1 && errno == EINTR) {
275 continue;
278 if (len == -1 || len == 0) {
279 fprintf(stderr, "Broken pipe\n");
280 exit(1);
283 g_string_append_len(s->rx, buffer, len);
286 offset = eol - s->rx->str;
287 line = g_string_new_len(s->rx->str, offset);
288 g_string_erase(s->rx, 0, offset + 1);
290 return line;
293 static gchar **qtest_rsp(QTestState *s, int expected_args)
295 GString *line;
296 gchar **words;
297 int i;
299 redo:
300 line = qtest_recv_line(s);
301 words = g_strsplit(line->str, " ", 0);
302 g_string_free(line, TRUE);
304 if (strcmp(words[0], "IRQ") == 0) {
305 int irq;
307 g_assert(words[1] != NULL);
308 g_assert(words[2] != NULL);
310 irq = strtoul(words[2], NULL, 0);
311 g_assert_cmpint(irq, >=, 0);
312 g_assert_cmpint(irq, <, MAX_IRQ);
314 if (strcmp(words[1], "raise") == 0) {
315 s->irq_level[irq] = true;
316 } else {
317 s->irq_level[irq] = false;
320 g_strfreev(words);
321 goto redo;
324 g_assert(words[0] != NULL);
325 g_assert_cmpstr(words[0], ==, "OK");
327 if (expected_args) {
328 for (i = 0; i < expected_args; i++) {
329 g_assert(words[i] != NULL);
331 } else {
332 g_strfreev(words);
335 return words;
338 typedef struct {
339 JSONMessageParser parser;
340 QDict *response;
341 } QMPResponseParser;
343 static void qmp_response(JSONMessageParser *parser, QList *tokens)
345 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
346 QObject *obj;
348 obj = json_parser_parse(tokens, NULL);
349 if (!obj) {
350 fprintf(stderr, "QMP JSON response parsing failed\n");
351 exit(1);
354 g_assert(qobject_type(obj) == QTYPE_QDICT);
355 g_assert(!qmp->response);
356 qmp->response = (QDict *)obj;
359 QDict *qmp_fd_receive(int fd)
361 QMPResponseParser qmp;
362 bool log = getenv("QTEST_LOG") != NULL;
364 qmp.response = NULL;
365 json_message_parser_init(&qmp.parser, qmp_response);
366 while (!qmp.response) {
367 ssize_t len;
368 char c;
370 len = read(fd, &c, 1);
371 if (len == -1 && errno == EINTR) {
372 continue;
375 if (len == -1 || len == 0) {
376 fprintf(stderr, "Broken pipe\n");
377 exit(1);
380 if (log) {
381 len = write(2, &c, 1);
383 json_message_parser_feed(&qmp.parser, &c, 1);
385 json_message_parser_destroy(&qmp.parser);
387 return qmp.response;
390 QDict *qtest_qmp_receive(QTestState *s)
392 return qmp_fd_receive(s->qmp_fd);
396 * Allow users to send a message without waiting for the reply,
397 * in the case that they choose to discard all replies up until
398 * a particular EVENT is received.
400 void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
402 va_list ap_copy;
403 QObject *qobj;
405 /* Going through qobject ensures we escape strings properly.
406 * This seemingly unnecessary copy is required in case va_list
407 * is an array type.
409 va_copy(ap_copy, ap);
410 qobj = qobject_from_jsonv(fmt, &ap_copy);
411 va_end(ap_copy);
413 /* No need to send anything for an empty QObject. */
414 if (qobj) {
415 int log = getenv("QTEST_LOG") != NULL;
416 QString *qstr = qobject_to_json(qobj);
417 const char *str = qstring_get_str(qstr);
418 size_t size = qstring_get_length(qstr);
420 if (log) {
421 fprintf(stderr, "%s", str);
423 /* Send QMP request */
424 socket_send(fd, str, size);
426 QDECREF(qstr);
427 qobject_decref(qobj);
431 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
433 qmp_fd_sendv(s->qmp_fd, fmt, ap);
436 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
438 qmp_fd_sendv(fd, fmt, ap);
440 return qmp_fd_receive(fd);
443 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
445 qtest_async_qmpv(s, fmt, ap);
447 /* Receive reply */
448 return qtest_qmp_receive(s);
451 QDict *qmp_fd(int fd, const char *fmt, ...)
453 va_list ap;
454 QDict *response;
456 va_start(ap, fmt);
457 response = qmp_fdv(fd, fmt, ap);
458 va_end(ap);
459 return response;
462 void qmp_fd_send(int fd, const char *fmt, ...)
464 va_list ap;
466 va_start(ap, fmt);
467 qmp_fd_sendv(fd, fmt, ap);
468 va_end(ap);
471 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
473 va_list ap;
474 QDict *response;
476 va_start(ap, fmt);
477 response = qtest_qmpv(s, fmt, ap);
478 va_end(ap);
479 return response;
482 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
484 va_list ap;
486 va_start(ap, fmt);
487 qtest_async_qmpv(s, fmt, ap);
488 va_end(ap);
491 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
493 QDict *response = qtest_qmpv(s, fmt, ap);
494 QDECREF(response);
497 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
499 va_list ap;
500 QDict *response;
502 va_start(ap, fmt);
503 response = qtest_qmpv(s, fmt, ap);
504 va_end(ap);
505 QDECREF(response);
508 void qtest_qmp_eventwait(QTestState *s, const char *event)
510 QDict *response;
512 for (;;) {
513 response = qtest_qmp_receive(s);
514 if ((qdict_haskey(response, "event")) &&
515 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
516 QDECREF(response);
517 break;
519 QDECREF(response);
523 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
525 char *cmd;
526 QDict *resp;
527 char *ret;
529 cmd = g_strdup_vprintf(fmt, ap);
530 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
531 " 'arguments': {'command-line': %s}}",
532 cmd);
533 ret = g_strdup(qdict_get_try_str(resp, "return"));
534 g_assert(ret);
535 QDECREF(resp);
536 g_free(cmd);
537 return ret;
540 char *qtest_hmp(QTestState *s, const char *fmt, ...)
542 va_list ap;
543 char *ret;
545 va_start(ap, fmt);
546 ret = qtest_hmpv(s, fmt, ap);
547 va_end(ap);
548 return ret;
551 const char *qtest_get_arch(void)
553 const char *qemu = getenv("QTEST_QEMU_BINARY");
554 g_assert(qemu != NULL);
555 const char *end = strrchr(qemu, '/');
557 return end + strlen("/qemu-system-");
560 bool qtest_get_irq(QTestState *s, int num)
562 /* dummy operation in order to make sure irq is up to date */
563 qtest_inb(s, 0);
565 return s->irq_level[num];
568 static int64_t qtest_clock_rsp(QTestState *s)
570 gchar **words;
571 int64_t clock;
572 words = qtest_rsp(s, 2);
573 clock = g_ascii_strtoll(words[1], NULL, 0);
574 g_strfreev(words);
575 return clock;
578 int64_t qtest_clock_step_next(QTestState *s)
580 qtest_sendf(s, "clock_step\n");
581 return qtest_clock_rsp(s);
584 int64_t qtest_clock_step(QTestState *s, int64_t step)
586 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
587 return qtest_clock_rsp(s);
590 int64_t qtest_clock_set(QTestState *s, int64_t val)
592 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
593 return qtest_clock_rsp(s);
596 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
598 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
599 qtest_rsp(s, 0);
602 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
604 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
605 qtest_rsp(s, 0);
608 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
610 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
611 qtest_rsp(s, 0);
614 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
616 qtest_out(s, "outb", addr, value);
619 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
621 qtest_out(s, "outw", addr, value);
624 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
626 qtest_out(s, "outl", addr, value);
629 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
631 gchar **args;
632 uint32_t value;
634 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
635 args = qtest_rsp(s, 2);
636 value = strtoul(args[1], NULL, 0);
637 g_strfreev(args);
639 return value;
642 uint8_t qtest_inb(QTestState *s, uint16_t addr)
644 return qtest_in(s, "inb", addr);
647 uint16_t qtest_inw(QTestState *s, uint16_t addr)
649 return qtest_in(s, "inw", addr);
652 uint32_t qtest_inl(QTestState *s, uint16_t addr)
654 return qtest_in(s, "inl", addr);
657 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
658 uint64_t value)
660 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
661 qtest_rsp(s, 0);
664 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
666 qtest_write(s, "writeb", addr, value);
669 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
671 qtest_write(s, "writew", addr, value);
674 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
676 qtest_write(s, "writel", addr, value);
679 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
681 qtest_write(s, "writeq", addr, value);
684 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
686 gchar **args;
687 uint64_t value;
689 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
690 args = qtest_rsp(s, 2);
691 value = strtoull(args[1], NULL, 0);
692 g_strfreev(args);
694 return value;
697 uint8_t qtest_readb(QTestState *s, uint64_t addr)
699 return qtest_read(s, "readb", addr);
702 uint16_t qtest_readw(QTestState *s, uint64_t addr)
704 return qtest_read(s, "readw", addr);
707 uint32_t qtest_readl(QTestState *s, uint64_t addr)
709 return qtest_read(s, "readl", addr);
712 uint64_t qtest_readq(QTestState *s, uint64_t addr)
714 return qtest_read(s, "readq", addr);
717 static int hex2nib(char ch)
719 if (ch >= '0' && ch <= '9') {
720 return ch - '0';
721 } else if (ch >= 'a' && ch <= 'f') {
722 return 10 + (ch - 'a');
723 } else if (ch >= 'A' && ch <= 'F') {
724 return 10 + (ch - 'a');
725 } else {
726 return -1;
730 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
732 uint8_t *ptr = data;
733 gchar **args;
734 size_t i;
736 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
737 args = qtest_rsp(s, 2);
739 for (i = 0; i < size; i++) {
740 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
741 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
744 g_strfreev(args);
747 void qtest_add_func(const char *str, void (*fn))
749 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
750 g_test_add_func(path, fn);
751 g_free(path);
754 void qtest_add_data_func(const char *str, const void *data, void (*fn))
756 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
757 g_test_add_data_func(path, data, fn);
758 g_free(path);
761 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
763 gchar *bdata;
765 bdata = g_base64_encode(data, size);
766 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
767 socket_send(s->fd, bdata, strlen(bdata));
768 socket_send(s->fd, "\n", 1);
769 qtest_rsp(s, 0);
770 g_free(bdata);
773 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
775 gchar **args;
776 size_t len;
778 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
779 args = qtest_rsp(s, 2);
781 g_base64_decode_inplace(args[1], &len);
782 if (size != len) {
783 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
784 size, len);
785 len = MIN(len, size);
788 memcpy(data, args[1], len);
789 g_strfreev(args);
792 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
794 const uint8_t *ptr = data;
795 size_t i;
796 char *enc = g_malloc(2 * size + 1);
798 for (i = 0; i < size; i++) {
799 sprintf(&enc[i * 2], "%02x", ptr[i]);
802 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
803 qtest_rsp(s, 0);
804 g_free(enc);
807 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
809 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
810 qtest_rsp(s, 0);
813 QDict *qmp(const char *fmt, ...)
815 va_list ap;
816 QDict *response;
818 va_start(ap, fmt);
819 response = qtest_qmpv(global_qtest, fmt, ap);
820 va_end(ap);
821 return response;
824 void qmp_async(const char *fmt, ...)
826 va_list ap;
828 va_start(ap, fmt);
829 qtest_async_qmpv(global_qtest, fmt, ap);
830 va_end(ap);
833 void qmp_discard_response(const char *fmt, ...)
835 va_list ap;
837 va_start(ap, fmt);
838 qtest_qmpv_discard_response(global_qtest, fmt, ap);
839 va_end(ap);
841 char *hmp(const char *fmt, ...)
843 va_list ap;
844 char *ret;
846 va_start(ap, fmt);
847 ret = qtest_hmpv(global_qtest, fmt, ap);
848 va_end(ap);
849 return ret;
852 bool qtest_big_endian(void)
854 const char *arch = qtest_get_arch();
855 int i;
857 static const struct {
858 const char *arch;
859 bool big_endian;
860 } endianness[] = {
861 { "aarch64", false },
862 { "alpha", false },
863 { "arm", false },
864 { "cris", false },
865 { "i386", false },
866 { "lm32", true },
867 { "m68k", true },
868 { "microblaze", true },
869 { "microblazeel", false },
870 { "mips", true },
871 { "mips64", true },
872 { "mips64el", false },
873 { "mipsel", false },
874 { "moxie", true },
875 { "or32", true },
876 { "ppc", true },
877 { "ppc64", true },
878 { "ppcemb", true },
879 { "s390x", true },
880 { "sh4", false },
881 { "sh4eb", true },
882 { "sparc", true },
883 { "sparc64", true },
884 { "unicore32", false },
885 { "x86_64", false },
886 { "xtensa", false },
887 { "xtensaeb", true },
891 for (i = 0; endianness[i].arch; i++) {
892 if (strcmp(endianness[i].arch, arch) == 0) {
893 return endianness[i].big_endian;
897 return false;