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.
17 #include "qemu/osdep.h"
20 #include <sys/socket.h>
24 #include "qapi/error.h"
25 #include "qapi/qmp/json-parser.h"
26 #include "qapi/qmp/json-streamer.h"
27 #include "qapi/qmp/qjson.h"
30 #define SOCKET_TIMEOUT 50
32 QTestState
*global_qtest
;
38 bool irq_level
[MAX_IRQ
];
40 pid_t qemu_pid
; /* our child QEMU process */
44 static GHookList abrt_hooks
;
45 static GList
*qtest_instances
;
46 static struct sigaction sigact_old
;
48 #define g_assert_no_errno(ret) do { \
49 g_assert_cmpint(ret, !=, -1); \
52 static int qtest_query_target_endianness(QTestState
*s
);
54 static int init_socket(const char *socket_path
)
56 struct sockaddr_un addr
;
60 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
61 g_assert_no_errno(sock
);
63 addr
.sun_family
= AF_UNIX
;
64 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
65 qemu_set_cloexec(sock
);
68 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
69 } while (ret
== -1 && errno
== EINTR
);
70 g_assert_no_errno(ret
);
71 ret
= listen(sock
, 1);
72 g_assert_no_errno(ret
);
77 static int socket_accept(int sock
)
79 struct sockaddr_un addr
;
82 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
85 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
89 addrlen
= sizeof(addr
);
90 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
91 } while (ret
== -1 && errno
== EINTR
);
93 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
100 static void kill_qemu(QTestState
*s
)
102 if (s
->qemu_pid
!= -1) {
103 kill(s
->qemu_pid
, SIGTERM
);
104 waitpid(s
->qemu_pid
, NULL
, 0);
108 static void kill_qemu_hook_func(void *s
)
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(GHookFunc 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_without_qmp_handshake(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 /* It's possible that if an earlier test run crashed it might
171 * have left a stale unix socket lying around. Delete any
172 * stale old socket to avoid spurious test failures with
173 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
176 unlink(qmp_socket_path
);
178 sock
= init_socket(socket_path
);
179 qmpsock
= init_socket(qmp_socket_path
);
181 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
183 s
->qemu_pid
= fork();
184 if (s
->qemu_pid
== 0) {
185 setenv("QEMU_AUDIO_DRV", "none", true);
186 command
= g_strdup_printf("exec %s "
187 "-qtest unix:%s,nowait "
189 "-qmp unix:%s,nowait "
190 "-machine accel=qtest "
192 "%s", qemu_binary
, socket_path
,
193 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
196 execlp("/bin/sh", "sh", "-c", command
, NULL
);
200 s
->fd
= socket_accept(sock
);
202 s
->qmp_fd
= socket_accept(qmpsock
);
205 unlink(qmp_socket_path
);
207 g_free(qmp_socket_path
);
209 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
211 s
->rx
= g_string_new("");
212 for (i
= 0; i
< MAX_IRQ
; i
++) {
213 s
->irq_level
[i
] = false;
216 if (getenv("QTEST_STOP")) {
217 kill(s
->qemu_pid
, SIGSTOP
);
220 /* ask endianness of the target */
222 s
->big_endian
= qtest_query_target_endianness(s
);
227 QTestState
*qtest_init(const char *extra_args
)
229 QTestState
*s
= qtest_init_without_qmp_handshake(extra_args
);
231 /* Read the QMP greeting and then do the handshake */
232 qtest_qmp_discard_response(s
, "");
233 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
238 void qtest_quit(QTestState
*s
)
240 qtest_instances
= g_list_remove(qtest_instances
, s
);
241 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
243 /* Uninstall SIGABRT handler on last instance */
244 if (!qtest_instances
) {
245 cleanup_sigabrt_handler();
251 g_string_free(s
->rx
, true);
255 static void socket_send(int fd
, const char *buf
, size_t size
)
260 while (offset
< size
) {
263 len
= write(fd
, buf
+ offset
, size
- offset
);
264 if (len
== -1 && errno
== EINTR
) {
268 g_assert_no_errno(len
);
269 g_assert_cmpint(len
, >, 0);
275 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
277 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
278 size_t size
= strlen(str
);
280 socket_send(fd
, str
, size
);
284 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
289 socket_sendf(s
->fd
, fmt
, ap
);
293 static GString
*qtest_recv_line(QTestState
*s
)
299 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
303 len
= read(s
->fd
, buffer
, sizeof(buffer
));
304 if (len
== -1 && errno
== EINTR
) {
308 if (len
== -1 || len
== 0) {
309 fprintf(stderr
, "Broken pipe\n");
313 g_string_append_len(s
->rx
, buffer
, len
);
316 offset
= eol
- s
->rx
->str
;
317 line
= g_string_new_len(s
->rx
->str
, offset
);
318 g_string_erase(s
->rx
, 0, offset
+ 1);
323 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
330 line
= qtest_recv_line(s
);
331 words
= g_strsplit(line
->str
, " ", 0);
332 g_string_free(line
, TRUE
);
334 if (strcmp(words
[0], "IRQ") == 0) {
337 g_assert(words
[1] != NULL
);
338 g_assert(words
[2] != NULL
);
340 irq
= strtoul(words
[2], NULL
, 0);
341 g_assert_cmpint(irq
, >=, 0);
342 g_assert_cmpint(irq
, <, MAX_IRQ
);
344 if (strcmp(words
[1], "raise") == 0) {
345 s
->irq_level
[irq
] = true;
347 s
->irq_level
[irq
] = false;
354 g_assert(words
[0] != NULL
);
355 g_assert_cmpstr(words
[0], ==, "OK");
358 for (i
= 0; i
< expected_args
; i
++) {
359 g_assert(words
[i
] != NULL
);
368 static int qtest_query_target_endianness(QTestState
*s
)
373 qtest_sendf(s
, "endianness\n");
374 args
= qtest_rsp(s
, 1);
375 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
376 big_endian
= strcmp(args
[1], "big") == 0;
383 JSONMessageParser parser
;
387 static void qmp_response(JSONMessageParser
*parser
, GQueue
*tokens
)
389 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
392 obj
= json_parser_parse(tokens
, NULL
);
394 fprintf(stderr
, "QMP JSON response parsing failed\n");
398 g_assert(!qmp
->response
);
399 qmp
->response
= qobject_to_qdict(obj
);
400 g_assert(qmp
->response
);
403 QDict
*qmp_fd_receive(int fd
)
405 QMPResponseParser qmp
;
406 bool log
= getenv("QTEST_LOG") != NULL
;
409 json_message_parser_init(&qmp
.parser
, qmp_response
);
410 while (!qmp
.response
) {
414 len
= read(fd
, &c
, 1);
415 if (len
== -1 && errno
== EINTR
) {
419 if (len
== -1 || len
== 0) {
420 fprintf(stderr
, "Broken pipe\n");
425 len
= write(2, &c
, 1);
427 json_message_parser_feed(&qmp
.parser
, &c
, 1);
429 json_message_parser_destroy(&qmp
.parser
);
434 QDict
*qtest_qmp_receive(QTestState
*s
)
436 return qmp_fd_receive(s
->qmp_fd
);
440 * Allow users to send a message without waiting for the reply,
441 * in the case that they choose to discard all replies up until
442 * a particular EVENT is received.
444 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
449 /* qobject_from_jsonv() silently eats leading 0xff as invalid
450 * JSON, but we want to test sending them over the wire to force
452 if (*fmt
== '\377') {
453 socket_send(fd
, fmt
, 1);
457 /* Going through qobject ensures we escape strings properly.
458 * This seemingly unnecessary copy is required in case va_list
461 va_copy(ap_copy
, ap
);
462 qobj
= qobject_from_jsonv(fmt
, &ap_copy
, &error_abort
);
465 /* No need to send anything for an empty QObject. */
467 int log
= getenv("QTEST_LOG") != NULL
;
468 QString
*qstr
= qobject_to_json(qobj
);
472 * BUG: QMP doesn't react to input until it sees a newline, an
473 * object, or an array. Work-around: give it a newline.
475 qstring_append_chr(qstr
, '\n');
476 str
= qstring_get_str(qstr
);
479 fprintf(stderr
, "%s", str
);
481 /* Send QMP request */
482 socket_send(fd
, str
, qstring_get_length(qstr
));
485 qobject_decref(qobj
);
489 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
491 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
494 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
496 qmp_fd_sendv(fd
, fmt
, ap
);
498 return qmp_fd_receive(fd
);
501 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
503 qtest_async_qmpv(s
, fmt
, ap
);
506 return qtest_qmp_receive(s
);
509 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
515 response
= qmp_fdv(fd
, fmt
, ap
);
520 void qmp_fd_send(int fd
, const char *fmt
, ...)
525 qmp_fd_sendv(fd
, fmt
, ap
);
529 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
535 response
= qtest_qmpv(s
, fmt
, ap
);
540 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
545 qtest_async_qmpv(s
, fmt
, ap
);
549 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
551 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
555 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
561 response
= qtest_qmpv(s
, fmt
, ap
);
566 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
571 response
= qtest_qmp_receive(s
);
572 if ((qdict_haskey(response
, "event")) &&
573 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
580 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
584 response
= qtest_qmp_eventwait_ref(s
, event
);
588 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
594 cmd
= g_strdup_vprintf(fmt
, ap
);
595 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
596 " 'arguments': {'command-line': %s}}",
598 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
599 while (ret
== NULL
&& qdict_get_try_str(resp
, "event")) {
600 /* Ignore asynchronous QMP events */
602 resp
= qtest_qmp_receive(s
);
603 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
611 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
617 ret
= qtest_hmpv(s
, fmt
, ap
);
622 const char *qtest_get_arch(void)
624 const char *qemu
= getenv("QTEST_QEMU_BINARY");
625 g_assert(qemu
!= NULL
);
626 const char *end
= strrchr(qemu
, '/');
628 return end
+ strlen("/qemu-system-");
631 bool qtest_get_irq(QTestState
*s
, int num
)
633 /* dummy operation in order to make sure irq is up to date */
636 return s
->irq_level
[num
];
639 static int64_t qtest_clock_rsp(QTestState
*s
)
643 words
= qtest_rsp(s
, 2);
644 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
649 int64_t qtest_clock_step_next(QTestState
*s
)
651 qtest_sendf(s
, "clock_step\n");
652 return qtest_clock_rsp(s
);
655 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
657 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
658 return qtest_clock_rsp(s
);
661 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
663 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
664 return qtest_clock_rsp(s
);
667 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
669 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
673 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
675 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
679 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
681 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
685 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
687 qtest_out(s
, "outb", addr
, value
);
690 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
692 qtest_out(s
, "outw", addr
, value
);
695 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
697 qtest_out(s
, "outl", addr
, value
);
700 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
705 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
706 args
= qtest_rsp(s
, 2);
707 value
= strtoul(args
[1], NULL
, 0);
713 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
715 return qtest_in(s
, "inb", addr
);
718 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
720 return qtest_in(s
, "inw", addr
);
723 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
725 return qtest_in(s
, "inl", addr
);
728 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
731 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
735 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
737 qtest_write(s
, "writeb", addr
, value
);
740 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
742 qtest_write(s
, "writew", addr
, value
);
745 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
747 qtest_write(s
, "writel", addr
, value
);
750 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
752 qtest_write(s
, "writeq", addr
, value
);
755 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
760 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
761 args
= qtest_rsp(s
, 2);
762 value
= strtoull(args
[1], NULL
, 0);
768 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
770 return qtest_read(s
, "readb", addr
);
773 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
775 return qtest_read(s
, "readw", addr
);
778 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
780 return qtest_read(s
, "readl", addr
);
783 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
785 return qtest_read(s
, "readq", addr
);
788 static int hex2nib(char ch
)
790 if (ch
>= '0' && ch
<= '9') {
792 } else if (ch
>= 'a' && ch
<= 'f') {
793 return 10 + (ch
- 'a');
794 } else if (ch
>= 'A' && ch
<= 'F') {
795 return 10 + (ch
- 'a');
801 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
811 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
812 args
= qtest_rsp(s
, 2);
814 for (i
= 0; i
< size
; i
++) {
815 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
816 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
822 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
823 uint32_t nargs
, uint64_t args
,
824 uint32_t nret
, uint64_t ret
)
826 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
827 name
, nargs
, args
, nret
, ret
);
832 void qtest_add_func(const char *str
, void (*fn
)(void))
834 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
835 g_test_add_func(path
, fn
);
839 void qtest_add_data_func_full(const char *str
, void *data
,
840 void (*fn
)(const void *),
841 GDestroyNotify data_free_func
)
843 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
844 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
848 void qtest_add_data_func(const char *str
, const void *data
,
849 void (*fn
)(const void *))
851 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
852 g_test_add_data_func(path
, data
, fn
);
856 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
860 bdata
= g_base64_encode(data
, size
);
861 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
862 socket_send(s
->fd
, bdata
, strlen(bdata
));
863 socket_send(s
->fd
, "\n", 1);
868 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
873 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
874 args
= qtest_rsp(s
, 2);
876 g_base64_decode_inplace(args
[1], &len
);
878 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
880 len
= MIN(len
, size
);
883 memcpy(data
, args
[1], len
);
887 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
889 const uint8_t *ptr
= data
;
897 enc
= g_malloc(2 * size
+ 1);
899 for (i
= 0; i
< size
; i
++) {
900 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
903 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
908 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
910 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
914 QDict
*qmp(const char *fmt
, ...)
920 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
925 void qmp_async(const char *fmt
, ...)
930 qtest_async_qmpv(global_qtest
, fmt
, ap
);
934 void qmp_discard_response(const char *fmt
, ...)
939 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
942 char *hmp(const char *fmt
, ...)
948 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
953 bool qtest_big_endian(QTestState
*s
)
955 return s
->big_endian
;
958 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
))
960 QDict
*response
, *minfo
;
967 qtest_start("-machine none");
968 response
= qmp("{ 'execute': 'query-machines' }");
970 list
= qdict_get_qlist(response
, "return");
973 for (p
= qlist_first(list
); p
; p
= qlist_next(p
)) {
974 minfo
= qobject_to_qdict(qlist_entry_obj(p
));
976 qobj
= qdict_get(minfo
, "name");
978 qstr
= qobject_to_qstring(qobj
);
980 mname
= qstring_get_str(qstr
);