arm: virt: Add an abstract ARM virt machine type
[qemu/ar7.git] / tests / libqtest.c
blobb12a9e4ca94d36fd9dffa030578bcbc1caf6452d
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 "qemu/osdep.h"
18 #include "libqtest.h"
20 #include <glib.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/un.h>
25 #include "qapi/qmp/json-parser.h"
26 #include "qapi/qmp/json-streamer.h"
27 #include "qapi/qmp/qjson.h"
29 #define MAX_IRQ 256
30 #define SOCKET_TIMEOUT 5
32 QTestState *global_qtest;
34 struct QTestState
36 int fd;
37 int qmp_fd;
38 bool irq_level[MAX_IRQ];
39 GString *rx;
40 pid_t qemu_pid; /* our child QEMU process */
43 static GHookList abrt_hooks;
44 static GList *qtest_instances;
45 static struct sigaction sigact_old;
47 #define g_assert_no_errno(ret) do { \
48 g_assert_cmpint(ret, !=, -1); \
49 } while (0)
51 static int init_socket(const char *socket_path)
53 struct sockaddr_un addr;
54 int sock;
55 int ret;
57 sock = socket(PF_UNIX, SOCK_STREAM, 0);
58 g_assert_no_errno(sock);
60 addr.sun_family = AF_UNIX;
61 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
62 qemu_set_cloexec(sock);
64 do {
65 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
66 } while (ret == -1 && errno == EINTR);
67 g_assert_no_errno(ret);
68 ret = listen(sock, 1);
69 g_assert_no_errno(ret);
71 return sock;
74 static int socket_accept(int sock)
76 struct sockaddr_un addr;
77 socklen_t addrlen;
78 int ret;
79 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
80 .tv_usec = 0 };
82 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
83 sizeof(timeout));
85 do {
86 addrlen = sizeof(addr);
87 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
88 } while (ret == -1 && errno == EINTR);
89 if (ret == -1) {
90 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
92 close(sock);
94 return ret;
97 static void kill_qemu(QTestState *s)
99 if (s->qemu_pid != -1) {
100 kill(s->qemu_pid, SIGTERM);
101 waitpid(s->qemu_pid, NULL, 0);
105 static void kill_qemu_hook_func(void *s)
107 kill_qemu(s);
110 static void sigabrt_handler(int signo)
112 g_hook_list_invoke(&abrt_hooks, FALSE);
115 static void setup_sigabrt_handler(void)
117 struct sigaction sigact;
119 /* Catch SIGABRT to clean up on g_assert() failure */
120 sigact = (struct sigaction){
121 .sa_handler = sigabrt_handler,
122 .sa_flags = SA_RESETHAND,
124 sigemptyset(&sigact.sa_mask);
125 sigaction(SIGABRT, &sigact, &sigact_old);
128 static void cleanup_sigabrt_handler(void)
130 sigaction(SIGABRT, &sigact_old, NULL);
133 void qtest_add_abrt_handler(GHookFunc fn, const void *data)
135 GHook *hook;
137 /* Only install SIGABRT handler once */
138 if (!abrt_hooks.is_setup) {
139 g_hook_list_init(&abrt_hooks, sizeof(GHook));
140 setup_sigabrt_handler();
143 hook = g_hook_alloc(&abrt_hooks);
144 hook->func = fn;
145 hook->data = (void *)data;
147 g_hook_prepend(&abrt_hooks, hook);
150 QTestState *qtest_init(const char *extra_args)
152 QTestState *s;
153 int sock, qmpsock, i;
154 gchar *socket_path;
155 gchar *qmp_socket_path;
156 gchar *command;
157 const char *qemu_binary;
159 qemu_binary = getenv("QTEST_QEMU_BINARY");
160 g_assert(qemu_binary != NULL);
162 s = g_malloc(sizeof(*s));
164 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
165 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
167 sock = init_socket(socket_path);
168 qmpsock = init_socket(qmp_socket_path);
170 qtest_add_abrt_handler(kill_qemu_hook_func, s);
172 s->qemu_pid = fork();
173 if (s->qemu_pid == 0) {
174 setenv("QEMU_AUDIO_DRV", "none", true);
175 command = g_strdup_printf("exec %s "
176 "-qtest unix:%s,nowait "
177 "-qtest-log %s "
178 "-qmp unix:%s,nowait "
179 "-machine accel=qtest "
180 "-display none "
181 "%s", qemu_binary, socket_path,
182 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
183 qmp_socket_path,
184 extra_args ?: "");
185 execlp("/bin/sh", "sh", "-c", command, NULL);
186 exit(1);
189 s->fd = socket_accept(sock);
190 if (s->fd >= 0) {
191 s->qmp_fd = socket_accept(qmpsock);
193 unlink(socket_path);
194 unlink(qmp_socket_path);
195 g_free(socket_path);
196 g_free(qmp_socket_path);
198 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
200 s->rx = g_string_new("");
201 for (i = 0; i < MAX_IRQ; i++) {
202 s->irq_level[i] = false;
205 /* Read the QMP greeting and then do the handshake */
206 qtest_qmp_discard_response(s, "");
207 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
209 if (getenv("QTEST_STOP")) {
210 kill(s->qemu_pid, SIGSTOP);
213 return s;
216 void qtest_quit(QTestState *s)
218 qtest_instances = g_list_remove(qtest_instances, s);
219 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
221 /* Uninstall SIGABRT handler on last instance */
222 if (!qtest_instances) {
223 cleanup_sigabrt_handler();
226 kill_qemu(s);
227 close(s->fd);
228 close(s->qmp_fd);
229 g_string_free(s->rx, true);
230 g_free(s);
233 static void socket_send(int fd, const char *buf, size_t size)
235 size_t offset;
237 offset = 0;
238 while (offset < size) {
239 ssize_t len;
241 len = write(fd, buf + offset, size - offset);
242 if (len == -1 && errno == EINTR) {
243 continue;
246 g_assert_no_errno(len);
247 g_assert_cmpint(len, >, 0);
249 offset += len;
253 static void socket_sendf(int fd, const char *fmt, va_list ap)
255 gchar *str = g_strdup_vprintf(fmt, ap);
256 size_t size = strlen(str);
258 socket_send(fd, str, size);
259 g_free(str);
262 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
264 va_list ap;
266 va_start(ap, fmt);
267 socket_sendf(s->fd, fmt, ap);
268 va_end(ap);
271 static GString *qtest_recv_line(QTestState *s)
273 GString *line;
274 size_t offset;
275 char *eol;
277 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
278 ssize_t len;
279 char buffer[1024];
281 len = read(s->fd, buffer, sizeof(buffer));
282 if (len == -1 && errno == EINTR) {
283 continue;
286 if (len == -1 || len == 0) {
287 fprintf(stderr, "Broken pipe\n");
288 exit(1);
291 g_string_append_len(s->rx, buffer, len);
294 offset = eol - s->rx->str;
295 line = g_string_new_len(s->rx->str, offset);
296 g_string_erase(s->rx, 0, offset + 1);
298 return line;
301 static gchar **qtest_rsp(QTestState *s, int expected_args)
303 GString *line;
304 gchar **words;
305 int i;
307 redo:
308 line = qtest_recv_line(s);
309 words = g_strsplit(line->str, " ", 0);
310 g_string_free(line, TRUE);
312 if (strcmp(words[0], "IRQ") == 0) {
313 int irq;
315 g_assert(words[1] != NULL);
316 g_assert(words[2] != NULL);
318 irq = strtoul(words[2], NULL, 0);
319 g_assert_cmpint(irq, >=, 0);
320 g_assert_cmpint(irq, <, MAX_IRQ);
322 if (strcmp(words[1], "raise") == 0) {
323 s->irq_level[irq] = true;
324 } else {
325 s->irq_level[irq] = false;
328 g_strfreev(words);
329 goto redo;
332 g_assert(words[0] != NULL);
333 g_assert_cmpstr(words[0], ==, "OK");
335 if (expected_args) {
336 for (i = 0; i < expected_args; i++) {
337 g_assert(words[i] != NULL);
339 } else {
340 g_strfreev(words);
343 return words;
346 typedef struct {
347 JSONMessageParser parser;
348 QDict *response;
349 } QMPResponseParser;
351 static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
353 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
354 QObject *obj;
356 obj = json_parser_parse(tokens, NULL);
357 if (!obj) {
358 fprintf(stderr, "QMP JSON response parsing failed\n");
359 exit(1);
362 g_assert(qobject_type(obj) == QTYPE_QDICT);
363 g_assert(!qmp->response);
364 qmp->response = (QDict *)obj;
367 QDict *qmp_fd_receive(int fd)
369 QMPResponseParser qmp;
370 bool log = getenv("QTEST_LOG") != NULL;
372 qmp.response = NULL;
373 json_message_parser_init(&qmp.parser, qmp_response);
374 while (!qmp.response) {
375 ssize_t len;
376 char c;
378 len = read(fd, &c, 1);
379 if (len == -1 && errno == EINTR) {
380 continue;
383 if (len == -1 || len == 0) {
384 fprintf(stderr, "Broken pipe\n");
385 exit(1);
388 if (log) {
389 len = write(2, &c, 1);
391 json_message_parser_feed(&qmp.parser, &c, 1);
393 json_message_parser_destroy(&qmp.parser);
395 return qmp.response;
398 QDict *qtest_qmp_receive(QTestState *s)
400 return qmp_fd_receive(s->qmp_fd);
404 * Allow users to send a message without waiting for the reply,
405 * in the case that they choose to discard all replies up until
406 * a particular EVENT is received.
408 void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
410 va_list ap_copy;
411 QObject *qobj;
413 /* Going through qobject ensures we escape strings properly.
414 * This seemingly unnecessary copy is required in case va_list
415 * is an array type.
417 va_copy(ap_copy, ap);
418 qobj = qobject_from_jsonv(fmt, &ap_copy);
419 va_end(ap_copy);
421 /* No need to send anything for an empty QObject. */
422 if (qobj) {
423 int log = getenv("QTEST_LOG") != NULL;
424 QString *qstr = qobject_to_json(qobj);
425 const char *str = qstring_get_str(qstr);
426 size_t size = qstring_get_length(qstr);
428 if (log) {
429 fprintf(stderr, "%s", str);
431 /* Send QMP request */
432 socket_send(fd, str, size);
434 QDECREF(qstr);
435 qobject_decref(qobj);
439 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
441 qmp_fd_sendv(s->qmp_fd, fmt, ap);
444 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
446 qmp_fd_sendv(fd, fmt, ap);
448 return qmp_fd_receive(fd);
451 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
453 qtest_async_qmpv(s, fmt, ap);
455 /* Receive reply */
456 return qtest_qmp_receive(s);
459 QDict *qmp_fd(int fd, const char *fmt, ...)
461 va_list ap;
462 QDict *response;
464 va_start(ap, fmt);
465 response = qmp_fdv(fd, fmt, ap);
466 va_end(ap);
467 return response;
470 void qmp_fd_send(int fd, const char *fmt, ...)
472 va_list ap;
474 va_start(ap, fmt);
475 qmp_fd_sendv(fd, fmt, ap);
476 va_end(ap);
479 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
481 va_list ap;
482 QDict *response;
484 va_start(ap, fmt);
485 response = qtest_qmpv(s, fmt, ap);
486 va_end(ap);
487 return response;
490 void qtest_async_qmp(QTestState *s, const char *fmt, ...)
492 va_list ap;
494 va_start(ap, fmt);
495 qtest_async_qmpv(s, fmt, ap);
496 va_end(ap);
499 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
501 QDict *response = qtest_qmpv(s, fmt, ap);
502 QDECREF(response);
505 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
507 va_list ap;
508 QDict *response;
510 va_start(ap, fmt);
511 response = qtest_qmpv(s, fmt, ap);
512 va_end(ap);
513 QDECREF(response);
516 void qtest_qmp_eventwait(QTestState *s, const char *event)
518 QDict *response;
520 for (;;) {
521 response = qtest_qmp_receive(s);
522 if ((qdict_haskey(response, "event")) &&
523 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
524 QDECREF(response);
525 break;
527 QDECREF(response);
531 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
533 char *cmd;
534 QDict *resp;
535 char *ret;
537 cmd = g_strdup_vprintf(fmt, ap);
538 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
539 " 'arguments': {'command-line': %s}}",
540 cmd);
541 ret = g_strdup(qdict_get_try_str(resp, "return"));
542 g_assert(ret);
543 QDECREF(resp);
544 g_free(cmd);
545 return ret;
548 char *qtest_hmp(QTestState *s, const char *fmt, ...)
550 va_list ap;
551 char *ret;
553 va_start(ap, fmt);
554 ret = qtest_hmpv(s, fmt, ap);
555 va_end(ap);
556 return ret;
559 const char *qtest_get_arch(void)
561 const char *qemu = getenv("QTEST_QEMU_BINARY");
562 g_assert(qemu != NULL);
563 const char *end = strrchr(qemu, '/');
565 return end + strlen("/qemu-system-");
568 bool qtest_get_irq(QTestState *s, int num)
570 /* dummy operation in order to make sure irq is up to date */
571 qtest_inb(s, 0);
573 return s->irq_level[num];
576 static int64_t qtest_clock_rsp(QTestState *s)
578 gchar **words;
579 int64_t clock;
580 words = qtest_rsp(s, 2);
581 clock = g_ascii_strtoll(words[1], NULL, 0);
582 g_strfreev(words);
583 return clock;
586 int64_t qtest_clock_step_next(QTestState *s)
588 qtest_sendf(s, "clock_step\n");
589 return qtest_clock_rsp(s);
592 int64_t qtest_clock_step(QTestState *s, int64_t step)
594 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
595 return qtest_clock_rsp(s);
598 int64_t qtest_clock_set(QTestState *s, int64_t val)
600 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
601 return qtest_clock_rsp(s);
604 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
606 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
607 qtest_rsp(s, 0);
610 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
612 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
613 qtest_rsp(s, 0);
616 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
618 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
619 qtest_rsp(s, 0);
622 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
624 qtest_out(s, "outb", addr, value);
627 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
629 qtest_out(s, "outw", addr, value);
632 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
634 qtest_out(s, "outl", addr, value);
637 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
639 gchar **args;
640 uint32_t value;
642 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
643 args = qtest_rsp(s, 2);
644 value = strtoul(args[1], NULL, 0);
645 g_strfreev(args);
647 return value;
650 uint8_t qtest_inb(QTestState *s, uint16_t addr)
652 return qtest_in(s, "inb", addr);
655 uint16_t qtest_inw(QTestState *s, uint16_t addr)
657 return qtest_in(s, "inw", addr);
660 uint32_t qtest_inl(QTestState *s, uint16_t addr)
662 return qtest_in(s, "inl", addr);
665 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
666 uint64_t value)
668 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
669 qtest_rsp(s, 0);
672 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
674 qtest_write(s, "writeb", addr, value);
677 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
679 qtest_write(s, "writew", addr, value);
682 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
684 qtest_write(s, "writel", addr, value);
687 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
689 qtest_write(s, "writeq", addr, value);
692 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
694 gchar **args;
695 uint64_t value;
697 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
698 args = qtest_rsp(s, 2);
699 value = strtoull(args[1], NULL, 0);
700 g_strfreev(args);
702 return value;
705 uint8_t qtest_readb(QTestState *s, uint64_t addr)
707 return qtest_read(s, "readb", addr);
710 uint16_t qtest_readw(QTestState *s, uint64_t addr)
712 return qtest_read(s, "readw", addr);
715 uint32_t qtest_readl(QTestState *s, uint64_t addr)
717 return qtest_read(s, "readl", addr);
720 uint64_t qtest_readq(QTestState *s, uint64_t addr)
722 return qtest_read(s, "readq", addr);
725 static int hex2nib(char ch)
727 if (ch >= '0' && ch <= '9') {
728 return ch - '0';
729 } else if (ch >= 'a' && ch <= 'f') {
730 return 10 + (ch - 'a');
731 } else if (ch >= 'A' && ch <= 'F') {
732 return 10 + (ch - 'a');
733 } else {
734 return -1;
738 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
740 uint8_t *ptr = data;
741 gchar **args;
742 size_t i;
744 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
745 args = qtest_rsp(s, 2);
747 for (i = 0; i < size; i++) {
748 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
749 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
752 g_strfreev(args);
755 void qtest_add_func(const char *str, void (*fn)(void))
757 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
758 g_test_add_func(path, fn);
759 g_free(path);
762 void qtest_add_data_func(const char *str, const void *data,
763 void (*fn)(const void *))
765 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
766 g_test_add_data_func(path, data, fn);
767 g_free(path);
770 void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
772 gchar *bdata;
774 bdata = g_base64_encode(data, size);
775 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
776 socket_send(s->fd, bdata, strlen(bdata));
777 socket_send(s->fd, "\n", 1);
778 qtest_rsp(s, 0);
779 g_free(bdata);
782 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
784 gchar **args;
785 size_t len;
787 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
788 args = qtest_rsp(s, 2);
790 g_base64_decode_inplace(args[1], &len);
791 if (size != len) {
792 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
793 size, len);
794 len = MIN(len, size);
797 memcpy(data, args[1], len);
798 g_strfreev(args);
801 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
803 const uint8_t *ptr = data;
804 size_t i;
805 char *enc = g_malloc(2 * size + 1);
807 for (i = 0; i < size; i++) {
808 sprintf(&enc[i * 2], "%02x", ptr[i]);
811 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
812 qtest_rsp(s, 0);
813 g_free(enc);
816 void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
818 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
819 qtest_rsp(s, 0);
822 QDict *qmp(const char *fmt, ...)
824 va_list ap;
825 QDict *response;
827 va_start(ap, fmt);
828 response = qtest_qmpv(global_qtest, fmt, ap);
829 va_end(ap);
830 return response;
833 void qmp_async(const char *fmt, ...)
835 va_list ap;
837 va_start(ap, fmt);
838 qtest_async_qmpv(global_qtest, fmt, ap);
839 va_end(ap);
842 void qmp_discard_response(const char *fmt, ...)
844 va_list ap;
846 va_start(ap, fmt);
847 qtest_qmpv_discard_response(global_qtest, fmt, ap);
848 va_end(ap);
850 char *hmp(const char *fmt, ...)
852 va_list ap;
853 char *ret;
855 va_start(ap, fmt);
856 ret = qtest_hmpv(global_qtest, fmt, ap);
857 va_end(ap);
858 return ret;
861 bool qtest_big_endian(void)
863 const char *arch = qtest_get_arch();
864 int i;
866 static const struct {
867 const char *arch;
868 bool big_endian;
869 } endianness[] = {
870 { "aarch64", false },
871 { "alpha", false },
872 { "arm", false },
873 { "cris", false },
874 { "i386", false },
875 { "lm32", true },
876 { "m68k", true },
877 { "microblaze", true },
878 { "microblazeel", false },
879 { "mips", true },
880 { "mips64", true },
881 { "mips64el", false },
882 { "mipsel", false },
883 { "moxie", true },
884 { "or32", true },
885 { "ppc", true },
886 { "ppc64", true },
887 { "ppcemb", true },
888 { "s390x", true },
889 { "sh4", false },
890 { "sh4eb", true },
891 { "sparc", true },
892 { "sparc64", true },
893 { "unicore32", false },
894 { "x86_64", false },
895 { "xtensa", false },
896 { "xtensaeb", true },
900 for (i = 0; endianness[i].arch; i++) {
901 if (strcmp(endianness[i].arch, arch) == 0) {
902 return endianness[i].big_endian;
906 return false;