4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
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.
20 #include <sys/types.h>
21 #include <sys/socket.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"
38 #define SOCKET_TIMEOUT 5
40 QTestState
*global_qtest
;
46 bool irq_level
[MAX_IRQ
];
48 pid_t qemu_pid
; /* our child QEMU process */
51 static GHookList abrt_hooks
;
52 static GList
*qtest_instances
;
53 static struct sigaction sigact_old
;
55 #define g_assert_no_errno(ret) do { \
56 g_assert_cmpint(ret, !=, -1); \
59 static int init_socket(const char *socket_path
)
61 struct sockaddr_un addr
;
65 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
66 g_assert_no_errno(sock
);
68 addr
.sun_family
= AF_UNIX
;
69 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
70 qemu_set_cloexec(sock
);
73 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
74 } while (ret
== -1 && errno
== EINTR
);
75 g_assert_no_errno(ret
);
76 ret
= listen(sock
, 1);
77 g_assert_no_errno(ret
);
82 static int socket_accept(int sock
)
84 struct sockaddr_un addr
;
87 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
90 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
94 addrlen
= sizeof(addr
);
95 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
96 } while (ret
== -1 && errno
== EINTR
);
98 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
105 static void kill_qemu(QTestState
*s
)
107 if (s
->qemu_pid
!= -1) {
108 kill(s
->qemu_pid
, SIGTERM
);
109 waitpid(s
->qemu_pid
, NULL
, 0);
113 static void sigabrt_handler(int signo
)
115 g_hook_list_invoke(&abrt_hooks
, FALSE
);
118 static void setup_sigabrt_handler(void)
120 struct sigaction sigact
;
122 /* Catch SIGABRT to clean up on g_assert() failure */
123 sigact
= (struct sigaction
){
124 .sa_handler
= sigabrt_handler
,
125 .sa_flags
= SA_RESETHAND
,
127 sigemptyset(&sigact
.sa_mask
);
128 sigaction(SIGABRT
, &sigact
, &sigact_old
);
131 static void cleanup_sigabrt_handler(void)
133 sigaction(SIGABRT
, &sigact_old
, NULL
);
136 void qtest_add_abrt_handler(void (*fn
), const void *data
)
140 /* Only install SIGABRT handler once */
141 if (!abrt_hooks
.is_setup
) {
142 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
143 setup_sigabrt_handler();
146 hook
= g_hook_alloc(&abrt_hooks
);
148 hook
->data
= (void *)data
;
150 g_hook_prepend(&abrt_hooks
, hook
);
153 QTestState
*qtest_init(const char *extra_args
)
156 int sock
, qmpsock
, i
;
158 gchar
*qmp_socket_path
;
160 const char *qemu_binary
;
162 qemu_binary
= getenv("QTEST_QEMU_BINARY");
163 g_assert(qemu_binary
!= NULL
);
165 s
= g_malloc(sizeof(*s
));
167 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
168 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
170 sock
= init_socket(socket_path
);
171 qmpsock
= init_socket(qmp_socket_path
);
173 qtest_add_abrt_handler(kill_qemu
, s
);
175 s
->qemu_pid
= fork();
176 if (s
->qemu_pid
== 0) {
177 setenv("QEMU_AUDIO_DRV", "none", true);
178 command
= g_strdup_printf("exec %s "
179 "-qtest unix:%s,nowait "
181 "-qmp unix:%s,nowait "
182 "-machine accel=qtest "
184 "%s", qemu_binary
, socket_path
,
185 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
188 execlp("/bin/sh", "sh", "-c", command
, NULL
);
192 s
->fd
= socket_accept(sock
);
194 s
->qmp_fd
= socket_accept(qmpsock
);
197 unlink(qmp_socket_path
);
199 g_free(qmp_socket_path
);
201 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
203 s
->rx
= g_string_new("");
204 for (i
= 0; i
< MAX_IRQ
; i
++) {
205 s
->irq_level
[i
] = false;
208 /* Read the QMP greeting and then do the handshake */
209 qtest_qmp_discard_response(s
, "");
210 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
212 if (getenv("QTEST_STOP")) {
213 kill(s
->qemu_pid
, SIGSTOP
);
219 void qtest_quit(QTestState
*s
)
221 qtest_instances
= g_list_remove(qtest_instances
, s
);
222 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
224 /* Uninstall SIGABRT handler on last instance */
225 if (!qtest_instances
) {
226 cleanup_sigabrt_handler();
232 g_string_free(s
->rx
, true);
236 static void socket_send(int fd
, const char *buf
, size_t size
)
241 while (offset
< size
) {
244 len
= write(fd
, buf
+ offset
, size
- offset
);
245 if (len
== -1 && errno
== EINTR
) {
249 g_assert_no_errno(len
);
250 g_assert_cmpint(len
, >, 0);
256 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
258 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
259 size_t size
= strlen(str
);
261 socket_send(fd
, str
, size
);
265 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
270 socket_sendf(s
->fd
, fmt
, ap
);
274 static GString
*qtest_recv_line(QTestState
*s
)
280 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
284 len
= read(s
->fd
, buffer
, sizeof(buffer
));
285 if (len
== -1 && errno
== EINTR
) {
289 if (len
== -1 || len
== 0) {
290 fprintf(stderr
, "Broken pipe\n");
294 g_string_append_len(s
->rx
, buffer
, len
);
297 offset
= eol
- s
->rx
->str
;
298 line
= g_string_new_len(s
->rx
->str
, offset
);
299 g_string_erase(s
->rx
, 0, offset
+ 1);
304 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
311 line
= qtest_recv_line(s
);
312 words
= g_strsplit(line
->str
, " ", 0);
313 g_string_free(line
, TRUE
);
315 if (strcmp(words
[0], "IRQ") == 0) {
318 g_assert(words
[1] != NULL
);
319 g_assert(words
[2] != NULL
);
321 irq
= strtoul(words
[2], NULL
, 0);
322 g_assert_cmpint(irq
, >=, 0);
323 g_assert_cmpint(irq
, <, MAX_IRQ
);
325 if (strcmp(words
[1], "raise") == 0) {
326 s
->irq_level
[irq
] = true;
328 s
->irq_level
[irq
] = false;
335 g_assert(words
[0] != NULL
);
336 g_assert_cmpstr(words
[0], ==, "OK");
339 for (i
= 0; i
< expected_args
; i
++) {
340 g_assert(words
[i
] != NULL
);
350 JSONMessageParser parser
;
354 static void qmp_response(JSONMessageParser
*parser
, QList
*tokens
)
356 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
359 obj
= json_parser_parse(tokens
, NULL
);
361 fprintf(stderr
, "QMP JSON response parsing failed\n");
365 g_assert(qobject_type(obj
) == QTYPE_QDICT
);
366 g_assert(!qmp
->response
);
367 qmp
->response
= (QDict
*)obj
;
370 QDict
*qmp_fd_receive(int fd
)
372 QMPResponseParser qmp
;
373 bool log
= getenv("QTEST_LOG") != NULL
;
376 json_message_parser_init(&qmp
.parser
, qmp_response
);
377 while (!qmp
.response
) {
381 len
= read(fd
, &c
, 1);
382 if (len
== -1 && errno
== EINTR
) {
386 if (len
== -1 || len
== 0) {
387 fprintf(stderr
, "Broken pipe\n");
392 len
= write(2, &c
, 1);
394 json_message_parser_feed(&qmp
.parser
, &c
, 1);
396 json_message_parser_destroy(&qmp
.parser
);
401 QDict
*qtest_qmp_receive(QTestState
*s
)
403 return qmp_fd_receive(s
->qmp_fd
);
407 * Allow users to send a message without waiting for the reply,
408 * in the case that they choose to discard all replies up until
409 * a particular EVENT is received.
411 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
416 /* Going through qobject ensures we escape strings properly.
417 * This seemingly unnecessary copy is required in case va_list
420 va_copy(ap_copy
, ap
);
421 qobj
= qobject_from_jsonv(fmt
, &ap_copy
);
424 /* No need to send anything for an empty QObject. */
426 int log
= getenv("QTEST_LOG") != NULL
;
427 QString
*qstr
= qobject_to_json(qobj
);
428 const char *str
= qstring_get_str(qstr
);
429 size_t size
= qstring_get_length(qstr
);
432 fprintf(stderr
, "%s", str
);
434 /* Send QMP request */
435 socket_send(fd
, str
, size
);
438 qobject_decref(qobj
);
442 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
444 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
447 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
449 qmp_fd_sendv(fd
, fmt
, ap
);
451 return qmp_fd_receive(fd
);
454 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
456 qtest_async_qmpv(s
, fmt
, ap
);
459 return qtest_qmp_receive(s
);
462 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
468 response
= qmp_fdv(fd
, fmt
, ap
);
473 void qmp_fd_send(int fd
, const char *fmt
, ...)
478 qmp_fd_sendv(fd
, fmt
, ap
);
482 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
488 response
= qtest_qmpv(s
, fmt
, ap
);
493 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
498 qtest_async_qmpv(s
, fmt
, ap
);
502 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
504 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
508 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
514 response
= qtest_qmpv(s
, fmt
, ap
);
519 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
524 response
= qtest_qmp_receive(s
);
525 if ((qdict_haskey(response
, "event")) &&
526 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
534 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
540 cmd
= g_strdup_vprintf(fmt
, ap
);
541 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
542 " 'arguments': {'command-line': %s}}",
544 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
551 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
557 ret
= qtest_hmpv(s
, fmt
, ap
);
562 const char *qtest_get_arch(void)
564 const char *qemu
= getenv("QTEST_QEMU_BINARY");
565 g_assert(qemu
!= NULL
);
566 const char *end
= strrchr(qemu
, '/');
568 return end
+ strlen("/qemu-system-");
571 bool qtest_get_irq(QTestState
*s
, int num
)
573 /* dummy operation in order to make sure irq is up to date */
576 return s
->irq_level
[num
];
579 static int64_t qtest_clock_rsp(QTestState
*s
)
583 words
= qtest_rsp(s
, 2);
584 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
589 int64_t qtest_clock_step_next(QTestState
*s
)
591 qtest_sendf(s
, "clock_step\n");
592 return qtest_clock_rsp(s
);
595 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
597 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
598 return qtest_clock_rsp(s
);
601 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
603 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
604 return qtest_clock_rsp(s
);
607 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
609 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
613 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
615 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
619 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
621 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
625 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
627 qtest_out(s
, "outb", addr
, value
);
630 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
632 qtest_out(s
, "outw", addr
, value
);
635 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
637 qtest_out(s
, "outl", addr
, value
);
640 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
645 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
646 args
= qtest_rsp(s
, 2);
647 value
= strtoul(args
[1], NULL
, 0);
653 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
655 return qtest_in(s
, "inb", addr
);
658 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
660 return qtest_in(s
, "inw", addr
);
663 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
665 return qtest_in(s
, "inl", addr
);
668 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
671 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
675 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
677 qtest_write(s
, "writeb", addr
, value
);
680 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
682 qtest_write(s
, "writew", addr
, value
);
685 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
687 qtest_write(s
, "writel", addr
, value
);
690 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
692 qtest_write(s
, "writeq", addr
, value
);
695 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
700 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
701 args
= qtest_rsp(s
, 2);
702 value
= strtoull(args
[1], NULL
, 0);
708 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
710 return qtest_read(s
, "readb", addr
);
713 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
715 return qtest_read(s
, "readw", addr
);
718 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
720 return qtest_read(s
, "readl", addr
);
723 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
725 return qtest_read(s
, "readq", addr
);
728 static int hex2nib(char ch
)
730 if (ch
>= '0' && ch
<= '9') {
732 } else if (ch
>= 'a' && ch
<= 'f') {
733 return 10 + (ch
- 'a');
734 } else if (ch
>= 'A' && ch
<= 'F') {
735 return 10 + (ch
- 'a');
741 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
747 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
748 args
= qtest_rsp(s
, 2);
750 for (i
= 0; i
< size
; i
++) {
751 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
752 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
758 void qtest_add_func(const char *str
, void (*fn
))
760 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
761 g_test_add_func(path
, fn
);
765 void qtest_add_data_func(const char *str
, const void *data
, void (*fn
))
767 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
768 g_test_add_data_func(path
, data
, fn
);
772 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
776 bdata
= g_base64_encode(data
, size
);
777 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
778 socket_send(s
->fd
, bdata
, strlen(bdata
));
779 socket_send(s
->fd
, "\n", 1);
784 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
789 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
790 args
= qtest_rsp(s
, 2);
792 g_base64_decode_inplace(args
[1], &len
);
794 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
796 len
= MIN(len
, size
);
799 memcpy(data
, args
[1], len
);
803 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
805 const uint8_t *ptr
= data
;
807 char *enc
= g_malloc(2 * size
+ 1);
809 for (i
= 0; i
< size
; i
++) {
810 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
813 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
818 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
820 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
824 QDict
*qmp(const char *fmt
, ...)
830 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
835 void qmp_async(const char *fmt
, ...)
840 qtest_async_qmpv(global_qtest
, fmt
, ap
);
844 void qmp_discard_response(const char *fmt
, ...)
849 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
852 char *hmp(const char *fmt
, ...)
858 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
863 bool qtest_big_endian(void)
865 const char *arch
= qtest_get_arch();
868 static const struct {
872 { "aarch64", false },
879 { "microblaze", true },
880 { "microblazeel", false },
883 { "mips64el", false },
895 { "unicore32", false },
898 { "xtensaeb", true },
902 for (i
= 0; endianness
[i
].arch
; i
++) {
903 if (strcmp(endianness
[i
].arch
, arch
) == 0) {
904 return endianness
[i
].big_endian
;