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/qmp/json-parser.h"
25 #include "qapi/qmp/json-streamer.h"
26 #include "qapi/qmp/qjson.h"
29 #define SOCKET_TIMEOUT 50
31 QTestState
*global_qtest
;
37 bool irq_level
[MAX_IRQ
];
39 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); \
51 static int qtest_query_target_endianness(QTestState
*s
);
53 static int init_socket(const char *socket_path
)
55 struct sockaddr_un addr
;
59 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
60 g_assert_no_errno(sock
);
62 addr
.sun_family
= AF_UNIX
;
63 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
64 qemu_set_cloexec(sock
);
67 ret
= bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
));
68 } while (ret
== -1 && errno
== EINTR
);
69 g_assert_no_errno(ret
);
70 ret
= listen(sock
, 1);
71 g_assert_no_errno(ret
);
76 static int socket_accept(int sock
)
78 struct sockaddr_un addr
;
81 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
84 setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
, (void *)&timeout
,
88 addrlen
= sizeof(addr
);
89 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
90 } while (ret
== -1 && errno
== EINTR
);
92 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
99 static void kill_qemu(QTestState
*s
)
101 if (s
->qemu_pid
!= -1) {
102 kill(s
->qemu_pid
, SIGTERM
);
103 waitpid(s
->qemu_pid
, NULL
, 0);
107 static void kill_qemu_hook_func(void *s
)
112 static void sigabrt_handler(int signo
)
114 g_hook_list_invoke(&abrt_hooks
, FALSE
);
117 static void setup_sigabrt_handler(void)
119 struct sigaction sigact
;
121 /* Catch SIGABRT to clean up on g_assert() failure */
122 sigact
= (struct sigaction
){
123 .sa_handler
= sigabrt_handler
,
124 .sa_flags
= SA_RESETHAND
,
126 sigemptyset(&sigact
.sa_mask
);
127 sigaction(SIGABRT
, &sigact
, &sigact_old
);
130 static void cleanup_sigabrt_handler(void)
132 sigaction(SIGABRT
, &sigact_old
, NULL
);
135 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
139 /* Only install SIGABRT handler once */
140 if (!abrt_hooks
.is_setup
) {
141 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
142 setup_sigabrt_handler();
145 hook
= g_hook_alloc(&abrt_hooks
);
147 hook
->data
= (void *)data
;
149 g_hook_prepend(&abrt_hooks
, hook
);
152 QTestState
*qtest_init(const char *extra_args
)
155 int sock
, qmpsock
, i
;
157 gchar
*qmp_socket_path
;
159 const char *qemu_binary
;
161 qemu_binary
= getenv("QTEST_QEMU_BINARY");
162 g_assert(qemu_binary
!= NULL
);
164 s
= g_malloc(sizeof(*s
));
166 socket_path
= g_strdup_printf("/tmp/qtest-%d.sock", getpid());
167 qmp_socket_path
= g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
169 sock
= init_socket(socket_path
);
170 qmpsock
= init_socket(qmp_socket_path
);
172 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
174 s
->qemu_pid
= fork();
175 if (s
->qemu_pid
== 0) {
176 setenv("QEMU_AUDIO_DRV", "none", true);
177 command
= g_strdup_printf("exec %s "
178 "-qtest unix:%s,nowait "
180 "-qmp unix:%s,nowait "
181 "-machine accel=qtest "
183 "%s", qemu_binary
, socket_path
,
184 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
187 execlp("/bin/sh", "sh", "-c", command
, NULL
);
191 s
->fd
= socket_accept(sock
);
193 s
->qmp_fd
= socket_accept(qmpsock
);
196 unlink(qmp_socket_path
);
198 g_free(qmp_socket_path
);
200 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
202 s
->rx
= g_string_new("");
203 for (i
= 0; i
< MAX_IRQ
; i
++) {
204 s
->irq_level
[i
] = false;
207 /* Read the QMP greeting and then do the handshake */
208 qtest_qmp_discard_response(s
, "");
209 qtest_qmp_discard_response(s
, "{ 'execute': 'qmp_capabilities' }");
211 if (getenv("QTEST_STOP")) {
212 kill(s
->qemu_pid
, SIGSTOP
);
215 /* ask endianness of the target */
217 s
->big_endian
= qtest_query_target_endianness(s
);
222 void qtest_quit(QTestState
*s
)
224 qtest_instances
= g_list_remove(qtest_instances
, s
);
225 g_hook_destroy_link(&abrt_hooks
, g_hook_find_data(&abrt_hooks
, TRUE
, s
));
227 /* Uninstall SIGABRT handler on last instance */
228 if (!qtest_instances
) {
229 cleanup_sigabrt_handler();
235 g_string_free(s
->rx
, true);
239 static void socket_send(int fd
, const char *buf
, size_t size
)
244 while (offset
< size
) {
247 len
= write(fd
, buf
+ offset
, size
- offset
);
248 if (len
== -1 && errno
== EINTR
) {
252 g_assert_no_errno(len
);
253 g_assert_cmpint(len
, >, 0);
259 static void socket_sendf(int fd
, const char *fmt
, va_list ap
)
261 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
262 size_t size
= strlen(str
);
264 socket_send(fd
, str
, size
);
268 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
273 socket_sendf(s
->fd
, fmt
, ap
);
277 static GString
*qtest_recv_line(QTestState
*s
)
283 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
287 len
= read(s
->fd
, buffer
, sizeof(buffer
));
288 if (len
== -1 && errno
== EINTR
) {
292 if (len
== -1 || len
== 0) {
293 fprintf(stderr
, "Broken pipe\n");
297 g_string_append_len(s
->rx
, buffer
, len
);
300 offset
= eol
- s
->rx
->str
;
301 line
= g_string_new_len(s
->rx
->str
, offset
);
302 g_string_erase(s
->rx
, 0, offset
+ 1);
307 static gchar
**qtest_rsp(QTestState
*s
, int expected_args
)
314 line
= qtest_recv_line(s
);
315 words
= g_strsplit(line
->str
, " ", 0);
316 g_string_free(line
, TRUE
);
318 if (strcmp(words
[0], "IRQ") == 0) {
321 g_assert(words
[1] != NULL
);
322 g_assert(words
[2] != NULL
);
324 irq
= strtoul(words
[2], NULL
, 0);
325 g_assert_cmpint(irq
, >=, 0);
326 g_assert_cmpint(irq
, <, MAX_IRQ
);
328 if (strcmp(words
[1], "raise") == 0) {
329 s
->irq_level
[irq
] = true;
331 s
->irq_level
[irq
] = false;
338 g_assert(words
[0] != NULL
);
339 g_assert_cmpstr(words
[0], ==, "OK");
342 for (i
= 0; i
< expected_args
; i
++) {
343 g_assert(words
[i
] != NULL
);
352 static int qtest_query_target_endianness(QTestState
*s
)
357 qtest_sendf(s
, "endianness\n");
358 args
= qtest_rsp(s
, 1);
359 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
360 big_endian
= strcmp(args
[1], "big") == 0;
367 JSONMessageParser parser
;
371 static void qmp_response(JSONMessageParser
*parser
, GQueue
*tokens
)
373 QMPResponseParser
*qmp
= container_of(parser
, QMPResponseParser
, parser
);
376 obj
= json_parser_parse(tokens
, NULL
);
378 fprintf(stderr
, "QMP JSON response parsing failed\n");
382 g_assert(qobject_type(obj
) == QTYPE_QDICT
);
383 g_assert(!qmp
->response
);
384 qmp
->response
= (QDict
*)obj
;
387 QDict
*qmp_fd_receive(int fd
)
389 QMPResponseParser qmp
;
390 bool log
= getenv("QTEST_LOG") != NULL
;
393 json_message_parser_init(&qmp
.parser
, qmp_response
);
394 while (!qmp
.response
) {
398 len
= read(fd
, &c
, 1);
399 if (len
== -1 && errno
== EINTR
) {
403 if (len
== -1 || len
== 0) {
404 fprintf(stderr
, "Broken pipe\n");
409 len
= write(2, &c
, 1);
411 json_message_parser_feed(&qmp
.parser
, &c
, 1);
413 json_message_parser_destroy(&qmp
.parser
);
418 QDict
*qtest_qmp_receive(QTestState
*s
)
420 return qmp_fd_receive(s
->qmp_fd
);
424 * Allow users to send a message without waiting for the reply,
425 * in the case that they choose to discard all replies up until
426 * a particular EVENT is received.
428 void qmp_fd_sendv(int fd
, const char *fmt
, va_list ap
)
433 /* Going through qobject ensures we escape strings properly.
434 * This seemingly unnecessary copy is required in case va_list
437 va_copy(ap_copy
, ap
);
438 qobj
= qobject_from_jsonv(fmt
, &ap_copy
);
441 /* No need to send anything for an empty QObject. */
443 int log
= getenv("QTEST_LOG") != NULL
;
444 QString
*qstr
= qobject_to_json(qobj
);
445 const char *str
= qstring_get_str(qstr
);
446 size_t size
= qstring_get_length(qstr
);
449 fprintf(stderr
, "%s", str
);
451 /* Send QMP request */
452 socket_send(fd
, str
, size
);
455 qobject_decref(qobj
);
459 void qtest_async_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
461 qmp_fd_sendv(s
->qmp_fd
, fmt
, ap
);
464 QDict
*qmp_fdv(int fd
, const char *fmt
, va_list ap
)
466 qmp_fd_sendv(fd
, fmt
, ap
);
468 return qmp_fd_receive(fd
);
471 QDict
*qtest_qmpv(QTestState
*s
, const char *fmt
, va_list ap
)
473 qtest_async_qmpv(s
, fmt
, ap
);
476 return qtest_qmp_receive(s
);
479 QDict
*qmp_fd(int fd
, const char *fmt
, ...)
485 response
= qmp_fdv(fd
, fmt
, ap
);
490 void qmp_fd_send(int fd
, const char *fmt
, ...)
495 qmp_fd_sendv(fd
, fmt
, ap
);
499 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
505 response
= qtest_qmpv(s
, fmt
, ap
);
510 void qtest_async_qmp(QTestState
*s
, const char *fmt
, ...)
515 qtest_async_qmpv(s
, fmt
, ap
);
519 void qtest_qmpv_discard_response(QTestState
*s
, const char *fmt
, va_list ap
)
521 QDict
*response
= qtest_qmpv(s
, fmt
, ap
);
525 void qtest_qmp_discard_response(QTestState
*s
, const char *fmt
, ...)
531 response
= qtest_qmpv(s
, fmt
, ap
);
536 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
541 response
= qtest_qmp_receive(s
);
542 if ((qdict_haskey(response
, "event")) &&
543 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
551 char *qtest_hmpv(QTestState
*s
, const char *fmt
, va_list ap
)
557 cmd
= g_strdup_vprintf(fmt
, ap
);
558 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
559 " 'arguments': {'command-line': %s}}",
561 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
568 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
574 ret
= qtest_hmpv(s
, fmt
, ap
);
579 const char *qtest_get_arch(void)
581 const char *qemu
= getenv("QTEST_QEMU_BINARY");
582 g_assert(qemu
!= NULL
);
583 const char *end
= strrchr(qemu
, '/');
585 return end
+ strlen("/qemu-system-");
588 bool qtest_get_irq(QTestState
*s
, int num
)
590 /* dummy operation in order to make sure irq is up to date */
593 return s
->irq_level
[num
];
596 static int64_t qtest_clock_rsp(QTestState
*s
)
600 words
= qtest_rsp(s
, 2);
601 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
606 int64_t qtest_clock_step_next(QTestState
*s
)
608 qtest_sendf(s
, "clock_step\n");
609 return qtest_clock_rsp(s
);
612 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
614 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
615 return qtest_clock_rsp(s
);
618 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
620 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
621 return qtest_clock_rsp(s
);
624 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
626 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
630 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
632 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
636 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
638 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
642 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
644 qtest_out(s
, "outb", addr
, value
);
647 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
649 qtest_out(s
, "outw", addr
, value
);
652 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
654 qtest_out(s
, "outl", addr
, value
);
657 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
662 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
663 args
= qtest_rsp(s
, 2);
664 value
= strtoul(args
[1], NULL
, 0);
670 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
672 return qtest_in(s
, "inb", addr
);
675 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
677 return qtest_in(s
, "inw", addr
);
680 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
682 return qtest_in(s
, "inl", addr
);
685 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
688 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
692 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
694 qtest_write(s
, "writeb", addr
, value
);
697 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
699 qtest_write(s
, "writew", addr
, value
);
702 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
704 qtest_write(s
, "writel", addr
, value
);
707 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
709 qtest_write(s
, "writeq", addr
, value
);
712 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
717 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
718 args
= qtest_rsp(s
, 2);
719 value
= strtoull(args
[1], NULL
, 0);
725 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
727 return qtest_read(s
, "readb", addr
);
730 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
732 return qtest_read(s
, "readw", addr
);
735 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
737 return qtest_read(s
, "readl", addr
);
740 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
742 return qtest_read(s
, "readq", addr
);
745 static int hex2nib(char ch
)
747 if (ch
>= '0' && ch
<= '9') {
749 } else if (ch
>= 'a' && ch
<= 'f') {
750 return 10 + (ch
- 'a');
751 } else if (ch
>= 'A' && ch
<= 'F') {
752 return 10 + (ch
- 'a');
758 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
764 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
765 args
= qtest_rsp(s
, 2);
767 for (i
= 0; i
< size
; i
++) {
768 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
769 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
775 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
776 uint32_t nargs
, uint64_t args
,
777 uint32_t nret
, uint64_t ret
)
779 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
780 name
, nargs
, args
, nret
, ret
);
785 void qtest_add_func(const char *str
, void (*fn
)(void))
787 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
788 g_test_add_func(path
, fn
);
792 void qtest_add_data_func_full(const char *str
, void *data
,
793 void (*fn
)(const void *),
794 GDestroyNotify data_free_func
)
796 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
797 #if GLIB_CHECK_VERSION(2, 34, 0)
798 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
799 #elif GLIB_CHECK_VERSION(2, 26, 0)
800 /* back-compat casts, remove this once we can require new-enough glib */
801 g_test_add_vtable(path
, 0, data
, NULL
,
802 (GTestFixtureFunc
)fn
, (GTestFixtureFunc
) data_free_func
);
804 /* back-compat casts, remove this once we can require new-enough glib */
805 g_test_add_vtable(path
, 0, data
, NULL
,
806 (void (*)(void)) fn
, (void (*)(void)) data_free_func
);
811 void qtest_add_data_func(const char *str
, const void *data
,
812 void (*fn
)(const void *))
814 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
815 g_test_add_data_func(path
, data
, fn
);
819 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
823 bdata
= g_base64_encode(data
, size
);
824 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
825 socket_send(s
->fd
, bdata
, strlen(bdata
));
826 socket_send(s
->fd
, "\n", 1);
831 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
836 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
837 args
= qtest_rsp(s
, 2);
839 g_base64_decode_inplace(args
[1], &len
);
841 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
843 len
= MIN(len
, size
);
846 memcpy(data
, args
[1], len
);
850 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
852 const uint8_t *ptr
= data
;
854 char *enc
= g_malloc(2 * size
+ 1);
856 for (i
= 0; i
< size
; i
++) {
857 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
860 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
865 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
867 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
871 QDict
*qmp(const char *fmt
, ...)
877 response
= qtest_qmpv(global_qtest
, fmt
, ap
);
882 void qmp_async(const char *fmt
, ...)
887 qtest_async_qmpv(global_qtest
, fmt
, ap
);
891 void qmp_discard_response(const char *fmt
, ...)
896 qtest_qmpv_discard_response(global_qtest
, fmt
, ap
);
899 char *hmp(const char *fmt
, ...)
905 ret
= qtest_hmpv(global_qtest
, fmt
, ap
);
910 bool qtest_big_endian(QTestState
*s
)
912 return s
->big_endian
;