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>
25 #include <sys/prctl.h>
26 #endif /* __linux__ */
28 #include <sys/procctl.h>
29 #endif /* __FreeBSD__ */
33 #include "qemu/ctype.h"
34 #include "qemu/cutils.h"
35 #include "qemu/sockets.h"
36 #include "qapi/qmp/qdict.h"
37 #include "qapi/qmp/qjson.h"
38 #include "qapi/qmp/qlist.h"
39 #include "qapi/qmp/qstring.h"
40 #include "qapi/qmp/qbool.h"
45 # define SOCKET_TIMEOUT 50
46 # define CMD_EXEC "exec "
47 # define DEV_STDERR "/dev/fd/2"
48 # define DEV_NULL "/dev/null"
50 # define SOCKET_TIMEOUT 50000
52 # define DEV_STDERR "2"
53 # define DEV_NULL "nul"
56 #define WAITPID_TIMEOUT 30
58 typedef void (*QTestSendFn
)(QTestState
*s
, const char *buf
);
59 typedef void (*ExternalSendFn
)(void *s
, const char *buf
);
60 typedef GString
* (*QTestRecvFn
)(QTestState
*);
62 typedef struct QTestClientTransportOps
{
63 QTestSendFn send
; /* for sending qtest commands */
66 * use external_send to send qtest command strings through functions which
67 * do not accept a QTestState as the first parameter.
69 ExternalSendFn external_send
;
71 QTestRecvFn recv_line
; /* for receiving qtest command responses */
78 pid_t qemu_pid
; /* our child QEMU process */
85 bool irq_level
[MAX_IRQ
];
87 QTestTransportOps ops
;
88 GList
*pending_events
;
89 QTestQMPEventCallback eventCB
;
93 static GHookList abrt_hooks
;
94 static void (*sighandler_old
)(int);
95 static bool silence_spawn_log
;
97 static int qtest_query_target_endianness(QTestState
*s
);
99 static void qtest_client_socket_send(QTestState
*, const char *buf
);
100 static void socket_send(int fd
, const char *buf
, size_t size
);
102 static GString
*qtest_client_socket_recv_line(QTestState
*);
104 static void qtest_client_set_tx_handler(QTestState
*s
, QTestSendFn send
);
105 static void qtest_client_set_rx_handler(QTestState
*s
, QTestRecvFn recv
);
107 static int init_socket(const char *socket_path
)
109 int sock
= qtest_socket_server(socket_path
);
110 qemu_set_cloexec(sock
);
114 static int socket_accept(int sock
)
116 struct sockaddr_un addr
;
120 * timeout unit of blocking receive calls is different among platforms.
121 * It's in seconds on non-Windows platforms but milliseconds on Windows.
124 struct timeval timeout
= { .tv_sec
= SOCKET_TIMEOUT
,
127 DWORD timeout
= SOCKET_TIMEOUT
;
130 if (setsockopt(sock
, SOL_SOCKET
, SO_RCVTIMEO
,
131 (void *)&timeout
, sizeof(timeout
))) {
132 fprintf(stderr
, "%s failed to set SO_RCVTIMEO: %s\n",
133 __func__
, strerror(errno
));
139 addrlen
= sizeof(addr
);
140 ret
= accept(sock
, (struct sockaddr
*)&addr
, &addrlen
);
141 } while (ret
== -1 && errno
== EINTR
);
143 fprintf(stderr
, "%s failed: %s\n", __func__
, strerror(errno
));
150 pid_t
qtest_pid(QTestState
*s
)
155 bool qtest_probe_child(QTestState
*s
)
157 pid_t pid
= s
->qemu_pid
;
161 pid
= waitpid(pid
, &s
->wstatus
, WNOHANG
);
166 GetExitCodeProcess((HANDLE
)pid
, &s
->exit_code
);
167 if (s
->exit_code
== STILL_ACTIVE
) {
170 CloseHandle((HANDLE
)pid
);
173 qtest_remove_abrt_handler(s
);
178 void qtest_set_expected_status(QTestState
*s
, int status
)
180 s
->expected_status
= status
;
183 static void qtest_check_status(QTestState
*s
)
185 assert(s
->qemu_pid
== -1);
188 * Check whether qemu exited with expected exit status; anything else is
189 * fishy and should be logged with as much detail as possible.
192 int wstatus
= s
->wstatus
;
193 if (WIFEXITED(wstatus
) && WEXITSTATUS(wstatus
) != s
->expected_status
) {
194 fprintf(stderr
, "%s:%d: kill_qemu() tried to terminate QEMU "
195 "process but encountered exit status %d (expected %d)\n",
196 __FILE__
, __LINE__
, WEXITSTATUS(wstatus
), s
->expected_status
);
198 } else if (WIFSIGNALED(wstatus
)) {
199 int sig
= WTERMSIG(wstatus
);
200 const char *signame
= strsignal(sig
) ?: "unknown ???";
201 const char *dump
= WCOREDUMP(wstatus
) ? " (core dumped)" : "";
203 fprintf(stderr
, "%s:%d: kill_qemu() detected QEMU death "
204 "from signal %d (%s)%s\n",
205 __FILE__
, __LINE__
, sig
, signame
, dump
);
209 if (s
->exit_code
!= s
->expected_status
) {
210 fprintf(stderr
, "%s:%d: kill_qemu() tried to terminate QEMU "
211 "process but encountered exit status %ld (expected %d)\n",
212 __FILE__
, __LINE__
, s
->exit_code
, s
->expected_status
);
218 void qtest_wait_qemu(QTestState
*s
)
220 if (s
->qemu_pid
!= -1) {
225 /* poll for a while until sending SIGKILL */
226 end
= g_get_monotonic_time() + WAITPID_TIMEOUT
* G_TIME_SPAN_SECOND
;
229 pid
= waitpid(s
->qemu_pid
, &s
->wstatus
, WNOHANG
);
233 g_usleep(100 * 1000);
234 } while (g_get_monotonic_time() < end
);
237 kill(s
->qemu_pid
, SIGKILL
);
238 pid
= RETRY_ON_EINTR(waitpid(s
->qemu_pid
, &s
->wstatus
, 0));
241 assert(pid
== s
->qemu_pid
);
245 ret
= WaitForSingleObject((HANDLE
)s
->qemu_pid
, INFINITE
);
246 assert(ret
== WAIT_OBJECT_0
);
247 GetExitCodeProcess((HANDLE
)s
->qemu_pid
, &s
->exit_code
);
248 CloseHandle((HANDLE
)s
->qemu_pid
);
252 qtest_remove_abrt_handler(s
);
254 qtest_check_status(s
);
257 void qtest_kill_qemu(QTestState
*s
)
259 /* Skip wait if qtest_probe_child() already reaped */
260 if (s
->qemu_pid
!= -1) {
262 kill(s
->qemu_pid
, SIGTERM
);
264 TerminateProcess((HANDLE
)s
->qemu_pid
, s
->expected_status
);
270 qtest_check_status(s
);
273 static void kill_qemu_hook_func(void *s
)
278 static void sigabrt_handler(int signo
)
280 g_hook_list_invoke(&abrt_hooks
, FALSE
);
283 static void setup_sigabrt_handler(void)
285 sighandler_old
= signal(SIGABRT
, sigabrt_handler
);
288 static void cleanup_sigabrt_handler(void)
290 signal(SIGABRT
, sighandler_old
);
293 static bool hook_list_is_empty(GHookList
*hook_list
)
295 GHook
*hook
= g_hook_first_valid(hook_list
, TRUE
);
301 g_hook_unref(hook_list
, hook
);
305 void qtest_add_abrt_handler(GHookFunc fn
, const void *data
)
309 if (!abrt_hooks
.is_setup
) {
310 g_hook_list_init(&abrt_hooks
, sizeof(GHook
));
313 /* Only install SIGABRT handler once */
314 if (hook_list_is_empty(&abrt_hooks
)) {
315 setup_sigabrt_handler();
318 hook
= g_hook_alloc(&abrt_hooks
);
320 hook
->data
= (void *)data
;
322 g_hook_prepend(&abrt_hooks
, hook
);
325 void qtest_remove_abrt_handler(void *data
)
327 GHook
*hook
= g_hook_find_data(&abrt_hooks
, TRUE
, data
);
333 g_hook_destroy_link(&abrt_hooks
, hook
);
335 /* Uninstall SIGABRT handler on last instance */
336 if (hook_list_is_empty(&abrt_hooks
)) {
337 cleanup_sigabrt_handler();
341 static const char *qtest_qemu_binary(const char *var
)
343 const char *qemu_bin
;
346 qemu_bin
= getenv(var
);
352 qemu_bin
= getenv("QTEST_QEMU_BINARY");
354 fprintf(stderr
, "Environment variable QTEST_QEMU_BINARY required\n");
362 static pid_t
qtest_create_process(char *cmd
)
365 PROCESS_INFORMATION pi
;
368 ZeroMemory(&si
, sizeof(si
));
370 ZeroMemory(&pi
, sizeof(pi
));
372 ret
= CreateProcess(NULL
, /* module name */
373 cmd
, /* command line */
374 NULL
, /* process handle not inheritable */
375 NULL
, /* thread handle not inheritable */
376 FALSE
, /* set handle inheritance to FALSE */
377 0, /* No creation flags */
378 NULL
, /* use parent's environment block */
379 NULL
, /* use parent's starting directory */
380 &si
, /* pointer to STARTUPINFO structure */
381 &pi
/* pointer to PROCESS_INFORMATION structure */
384 fprintf(stderr
, "%s:%d: unable to create a new process (%s)\n",
385 __FILE__
, __LINE__
, strerror(GetLastError()));
389 return (pid_t
)pi
.hProcess
;
393 static QTestState
*G_GNUC_PRINTF(2, 3) qtest_spawn_qemu(const char *qemu_bin
,
394 const char *fmt
, ...)
397 QTestState
*s
= g_new0(QTestState
, 1);
398 const char *trace
= g_getenv("QTEST_TRACE");
399 g_autofree
char *tracearg
= trace
?
400 g_strdup_printf("-trace %s ", trace
) : g_strdup("");
401 g_autoptr(GString
) command
= g_string_new("");
404 g_string_append_printf(command
, CMD_EXEC
"%s %s", qemu_bin
, tracearg
);
405 g_string_append_vprintf(command
, fmt
, ap
);
408 qtest_add_abrt_handler(kill_qemu_hook_func
, s
);
410 if (!silence_spawn_log
) {
411 g_test_message("starting QEMU: %s", command
->str
);
415 s
->qemu_pid
= fork();
416 if (s
->qemu_pid
== 0) {
419 * Although we register a ABRT handler to kill off QEMU
420 * when g_assert() triggers, we want an extra safety
421 * net. The QEMU process might be non-functional and
422 * thus not have responded to SIGTERM. The test script
423 * might also have crashed with SEGV, in which case the
424 * cleanup handlers won't ever run.
426 * This PR_SET_PDEATHSIG setup will ensure any remaining
427 * QEMU will get terminated with SIGKILL in these cases.
429 prctl(PR_SET_PDEATHSIG
, SIGKILL
, 0, 0, 0);
430 #endif /* __linux__ */
433 procctl(P_PID
, getpid(), PROC_PDEATHSIG_CTL
, &sig
);
434 #endif /* __FreeBSD__ */
435 execlp("/bin/sh", "sh", "-c", command
->str
, NULL
);
439 s
->qemu_pid
= qtest_create_process(command
->str
);
445 static QTestState
*qtest_init_internal(const char *qemu_bin
,
446 const char *extra_args
)
449 int sock
, qmpsock
, i
;
451 gchar
*qmp_socket_path
;
453 socket_path
= g_strdup_printf("%s/qtest-%d.sock",
454 g_get_tmp_dir(), getpid());
455 qmp_socket_path
= g_strdup_printf("%s/qtest-%d.qmp",
456 g_get_tmp_dir(), getpid());
459 * It's possible that if an earlier test run crashed it might
460 * have left a stale unix socket lying around. Delete any
461 * stale old socket to avoid spurious test failures with
462 * tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
465 unlink(qmp_socket_path
);
468 sock
= init_socket(socket_path
);
469 qmpsock
= init_socket(qmp_socket_path
);
471 s
= qtest_spawn_qemu(qemu_bin
,
474 "-chardev socket,path=%s,id=char0 "
475 "-mon chardev=char0,mode=control "
481 getenv("QTEST_LOG") ? DEV_STDERR
: DEV_NULL
,
485 qtest_client_set_rx_handler(s
, qtest_client_socket_recv_line
);
486 qtest_client_set_tx_handler(s
, qtest_client_socket_send
);
488 s
->fd
= socket_accept(sock
);
490 s
->qmp_fd
= socket_accept(qmpsock
);
493 unlink(qmp_socket_path
);
495 g_free(qmp_socket_path
);
497 g_assert(s
->fd
>= 0 && s
->qmp_fd
>= 0);
499 s
->rx
= g_string_new("");
500 for (i
= 0; i
< MAX_IRQ
; i
++) {
501 s
->irq_level
[i
] = false;
505 * Stopping QEMU for debugging is not supported on Windows.
507 * Using DebugActiveProcess() API can suspend the QEMU process,
508 * but gdb cannot attach to the process. Using the undocumented
509 * NtSuspendProcess() can suspend the QEMU process and gdb can
510 * attach to the process, but gdb cannot resume it.
513 if (getenv("QTEST_STOP")) {
514 kill(s
->qemu_pid
, SIGSTOP
);
520 QTestState
*qtest_init_without_qmp_handshake(const char *extra_args
)
522 return qtest_init_internal(qtest_qemu_binary(NULL
), extra_args
);
525 QTestState
*qtest_init_with_env_no_handshake(const char *var
,
526 const char *extra_args
)
528 return qtest_init_internal(qtest_qemu_binary(var
), extra_args
);
531 QTestState
*qtest_init_with_env(const char *var
, const char *extra_args
)
533 QTestState
*s
= qtest_init_internal(qtest_qemu_binary(var
), extra_args
);
536 /* ask endianness of the target */
538 s
->big_endian
= qtest_query_target_endianness(s
);
540 /* Read the QMP greeting and then do the handshake */
541 greeting
= qtest_qmp_receive(s
);
542 qobject_unref(greeting
);
543 qobject_unref(qtest_qmp(s
, "{ 'execute': 'qmp_capabilities' }"));
548 QTestState
*qtest_init(const char *extra_args
)
550 return qtest_init_with_env(NULL
, extra_args
);
553 QTestState
*qtest_vinitf(const char *fmt
, va_list ap
)
555 char *args
= g_strdup_vprintf(fmt
, ap
);
558 s
= qtest_init(args
);
563 QTestState
*qtest_initf(const char *fmt
, ...)
569 s
= qtest_vinitf(fmt
, ap
);
574 QTestState
*qtest_init_with_serial(const char *extra_args
, int *sock_fd
)
577 g_autofree
char *sock_dir
= NULL
;
581 sock_dir
= g_dir_make_tmp("qtest-serial-XXXXXX", NULL
);
582 g_assert_true(sock_dir
!= NULL
);
583 sock_path
= g_strdup_printf("%s/sock", sock_dir
);
586 sock_fd_init
= init_socket(sock_path
);
588 qts
= qtest_initf("-chardev socket,id=s0,path=%s -serial chardev:s0 %s",
589 sock_path
, extra_args
);
591 *sock_fd
= socket_accept(sock_fd_init
);
597 g_assert_true(*sock_fd
>= 0);
602 void qtest_quit(QTestState
*s
)
604 qtest_remove_abrt_handler(s
);
609 g_string_free(s
->rx
, true);
611 for (GList
*it
= s
->pending_events
; it
!= NULL
; it
= it
->next
) {
612 qobject_unref((QDict
*)it
->data
);
615 g_list_free(s
->pending_events
);
620 static void socket_send(int fd
, const char *buf
, size_t size
)
622 ssize_t res
= qemu_send_full(fd
, buf
, size
);
627 static void qtest_client_socket_send(QTestState
*s
, const char *buf
)
629 socket_send(s
->fd
, buf
, strlen(buf
));
632 static void G_GNUC_PRINTF(2, 3) qtest_sendf(QTestState
*s
, const char *fmt
, ...)
637 gchar
*str
= g_strdup_vprintf(fmt
, ap
);
644 static GString
*qtest_client_socket_recv_line(QTestState
*s
)
650 while ((eol
= strchr(s
->rx
->str
, '\n')) == NULL
) {
654 len
= recv(s
->fd
, buffer
, sizeof(buffer
), 0);
655 if (len
== -1 && errno
== EINTR
) {
659 if (len
== -1 || len
== 0) {
660 fprintf(stderr
, "Broken pipe\n");
664 g_string_append_len(s
->rx
, buffer
, len
);
667 offset
= eol
- s
->rx
->str
;
668 line
= g_string_new_len(s
->rx
->str
, offset
);
669 g_string_erase(s
->rx
, 0, offset
+ 1);
674 static gchar
**qtest_rsp_args(QTestState
*s
, int expected_args
)
681 line
= s
->ops
.recv_line(s
);
682 words
= g_strsplit(line
->str
, " ", 0);
683 g_string_free(line
, TRUE
);
685 if (strcmp(words
[0], "IRQ") == 0) {
689 g_assert(words
[1] != NULL
);
690 g_assert(words
[2] != NULL
);
692 ret
= qemu_strtol(words
[2], NULL
, 0, &irq
);
694 g_assert_cmpint(irq
, >=, 0);
695 g_assert_cmpint(irq
, <, MAX_IRQ
);
697 if (strcmp(words
[1], "raise") == 0) {
698 s
->irq_level
[irq
] = true;
700 s
->irq_level
[irq
] = false;
707 g_assert(words
[0] != NULL
);
708 g_assert_cmpstr(words
[0], ==, "OK");
710 for (i
= 0; i
< expected_args
; i
++) {
711 g_assert(words
[i
] != NULL
);
717 static void qtest_rsp(QTestState
*s
)
719 gchar
**words
= qtest_rsp_args(s
, 0);
724 static int qtest_query_target_endianness(QTestState
*s
)
729 qtest_sendf(s
, "endianness\n");
730 args
= qtest_rsp_args(s
, 1);
731 g_assert(strcmp(args
[1], "big") == 0 || strcmp(args
[1], "little") == 0);
732 big_endian
= strcmp(args
[1], "big") == 0;
738 QDict
*qtest_qmp_receive(QTestState
*s
)
741 QDict
*response
= qtest_qmp_receive_dict(s
);
743 if (!qdict_get_try_str(response
, "event")) {
748 !s
->eventCB(s
, qdict_get_str(response
, "event"),
749 response
, s
->eventData
)) {
750 /* Stash the event for a later consumption */
751 s
->pending_events
= g_list_append(s
->pending_events
, response
);
753 qobject_unref(response
);
758 QDict
*qtest_qmp_receive_dict(QTestState
*s
)
760 return qmp_fd_receive(s
->qmp_fd
);
763 int qtest_socket_server(const char *socket_path
)
765 struct sockaddr_un addr
;
769 sock
= socket(PF_UNIX
, SOCK_STREAM
, 0);
770 g_assert_cmpint(sock
, !=, -1);
772 addr
.sun_family
= AF_UNIX
;
773 snprintf(addr
.sun_path
, sizeof(addr
.sun_path
), "%s", socket_path
);
775 ret
= RETRY_ON_EINTR(bind(sock
, (struct sockaddr
*)&addr
, sizeof(addr
)));
776 g_assert_cmpint(ret
, !=, -1);
777 ret
= listen(sock
, 1);
778 g_assert_cmpint(ret
, !=, -1);
784 void qtest_qmp_vsend_fds(QTestState
*s
, int *fds
, size_t fds_num
,
785 const char *fmt
, va_list ap
)
787 qmp_fd_vsend_fds(s
->qmp_fd
, fds
, fds_num
, fmt
, ap
);
791 void qtest_qmp_vsend(QTestState
*s
, const char *fmt
, va_list ap
)
793 qmp_fd_vsend(s
->qmp_fd
, fmt
, ap
);
797 QDict
*qtest_vqmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
798 const char *fmt
, va_list ap
)
800 qtest_qmp_vsend_fds(s
, fds
, fds_num
, fmt
, ap
);
803 return qtest_qmp_receive(s
);
807 QDict
*qtest_vqmp(QTestState
*s
, const char *fmt
, va_list ap
)
809 qtest_qmp_vsend(s
, fmt
, ap
);
812 return qtest_qmp_receive(s
);
816 QDict
*qtest_qmp_fds(QTestState
*s
, int *fds
, size_t fds_num
,
817 const char *fmt
, ...)
823 response
= qtest_vqmp_fds(s
, fds
, fds_num
, fmt
, ap
);
829 QDict
*qtest_qmp(QTestState
*s
, const char *fmt
, ...)
835 response
= qtest_vqmp(s
, fmt
, ap
);
840 void qtest_qmp_send(QTestState
*s
, const char *fmt
, ...)
845 qtest_qmp_vsend(s
, fmt
, ap
);
849 void qtest_qmp_send_raw(QTestState
*s
, const char *fmt
, ...)
854 qmp_fd_vsend_raw(s
->qmp_fd
, fmt
, ap
);
858 void qtest_qmp_set_event_callback(QTestState
*s
,
859 QTestQMPEventCallback cb
, void *opaque
)
862 s
->eventData
= opaque
;
865 QDict
*qtest_qmp_event_ref(QTestState
*s
, const char *event
)
867 while (s
->pending_events
) {
869 GList
*first
= s
->pending_events
;
870 QDict
*response
= (QDict
*)first
->data
;
872 s
->pending_events
= g_list_delete_link(s
->pending_events
, first
);
874 if (!strcmp(qdict_get_str(response
, "event"), event
)) {
877 qobject_unref(response
);
882 QDict
*qtest_qmp_eventwait_ref(QTestState
*s
, const char *event
)
884 QDict
*response
= qtest_qmp_event_ref(s
, event
);
891 response
= qtest_qmp_receive_dict(s
);
892 if ((qdict_haskey(response
, "event")) &&
893 (strcmp(qdict_get_str(response
, "event"), event
) == 0)) {
896 qobject_unref(response
);
900 void qtest_qmp_eventwait(QTestState
*s
, const char *event
)
904 response
= qtest_qmp_eventwait_ref(s
, event
);
905 qobject_unref(response
);
908 char *qtest_vhmp(QTestState
*s
, const char *fmt
, va_list ap
)
914 cmd
= g_strdup_vprintf(fmt
, ap
);
915 resp
= qtest_qmp(s
, "{'execute': 'human-monitor-command',"
916 " 'arguments': {'command-line': %s}}",
918 ret
= g_strdup(qdict_get_try_str(resp
, "return"));
925 char *qtest_hmp(QTestState
*s
, const char *fmt
, ...)
931 ret
= qtest_vhmp(s
, fmt
, ap
);
936 const char *qtest_get_arch(void)
938 const char *qemu
= qtest_qemu_binary(NULL
);
939 const char *end
= strrchr(qemu
, '-');
942 fprintf(stderr
, "Can't determine architecture from binary name.\n");
946 if (!strstr(qemu
, "-system-")) {
947 fprintf(stderr
, "QTEST_QEMU_BINARY must end with *-system-<arch> "
948 "where 'arch' is the target\narchitecture (x86_64, aarch64, "
956 bool qtest_has_accel(const char *accel_name
)
958 if (g_str_equal(accel_name
, "tcg")) {
959 #if defined(CONFIG_TCG)
964 } else if (g_str_equal(accel_name
, "kvm")) {
966 const char *arch
= qtest_get_arch();
967 const char *targets
[] = { CONFIG_KVM_TARGETS
};
969 for (i
= 0; i
< ARRAY_SIZE(targets
); i
++) {
970 if (!strncmp(targets
[i
], arch
, strlen(arch
))) {
971 if (!access("/dev/kvm", R_OK
| W_OK
)) {
977 /* not implemented */
978 g_assert_not_reached();
983 bool qtest_get_irq(QTestState
*s
, int num
)
985 /* dummy operation in order to make sure irq is up to date */
988 return s
->irq_level
[num
];
991 void qtest_module_load(QTestState
*s
, const char *prefix
, const char *libname
)
993 qtest_sendf(s
, "module_load %s %s\n", prefix
, libname
);
997 static int64_t qtest_clock_rsp(QTestState
*s
)
1001 words
= qtest_rsp_args(s
, 2);
1002 clock
= g_ascii_strtoll(words
[1], NULL
, 0);
1007 int64_t qtest_clock_step_next(QTestState
*s
)
1009 qtest_sendf(s
, "clock_step\n");
1010 return qtest_clock_rsp(s
);
1013 int64_t qtest_clock_step(QTestState
*s
, int64_t step
)
1015 qtest_sendf(s
, "clock_step %"PRIi64
"\n", step
);
1016 return qtest_clock_rsp(s
);
1019 int64_t qtest_clock_set(QTestState
*s
, int64_t val
)
1021 qtest_sendf(s
, "clock_set %"PRIi64
"\n", val
);
1022 return qtest_clock_rsp(s
);
1025 void qtest_irq_intercept_out(QTestState
*s
, const char *qom_path
)
1027 qtest_sendf(s
, "irq_intercept_out %s\n", qom_path
);
1031 void qtest_irq_intercept_out_named(QTestState
*s
, const char *qom_path
, const char *name
)
1033 qtest_sendf(s
, "irq_intercept_out %s %s\n", qom_path
, name
);
1037 void qtest_irq_intercept_in(QTestState
*s
, const char *qom_path
)
1039 qtest_sendf(s
, "irq_intercept_in %s\n", qom_path
);
1043 void qtest_set_irq_in(QTestState
*s
, const char *qom_path
, const char *name
,
1047 name
= "unnamed-gpio-in";
1049 qtest_sendf(s
, "set_irq_in %s %s %d %d\n", qom_path
, name
, num
, level
);
1053 static void qtest_out(QTestState
*s
, const char *cmd
, uint16_t addr
, uint32_t value
)
1055 qtest_sendf(s
, "%s 0x%x 0x%x\n", cmd
, addr
, value
);
1059 void qtest_outb(QTestState
*s
, uint16_t addr
, uint8_t value
)
1061 qtest_out(s
, "outb", addr
, value
);
1064 void qtest_outw(QTestState
*s
, uint16_t addr
, uint16_t value
)
1066 qtest_out(s
, "outw", addr
, value
);
1069 void qtest_outl(QTestState
*s
, uint16_t addr
, uint32_t value
)
1071 qtest_out(s
, "outl", addr
, value
);
1074 static uint32_t qtest_in(QTestState
*s
, const char *cmd
, uint16_t addr
)
1078 unsigned long value
;
1080 qtest_sendf(s
, "%s 0x%x\n", cmd
, addr
);
1081 args
= qtest_rsp_args(s
, 2);
1082 ret
= qemu_strtoul(args
[1], NULL
, 0, &value
);
1083 g_assert(!ret
&& value
<= UINT32_MAX
);
1089 uint8_t qtest_inb(QTestState
*s
, uint16_t addr
)
1091 return qtest_in(s
, "inb", addr
);
1094 uint16_t qtest_inw(QTestState
*s
, uint16_t addr
)
1096 return qtest_in(s
, "inw", addr
);
1099 uint32_t qtest_inl(QTestState
*s
, uint16_t addr
)
1101 return qtest_in(s
, "inl", addr
);
1104 static void qtest_write(QTestState
*s
, const char *cmd
, uint64_t addr
,
1107 qtest_sendf(s
, "%s 0x%" PRIx64
" 0x%" PRIx64
"\n", cmd
, addr
, value
);
1111 void qtest_writeb(QTestState
*s
, uint64_t addr
, uint8_t value
)
1113 qtest_write(s
, "writeb", addr
, value
);
1116 void qtest_writew(QTestState
*s
, uint64_t addr
, uint16_t value
)
1118 qtest_write(s
, "writew", addr
, value
);
1121 void qtest_writel(QTestState
*s
, uint64_t addr
, uint32_t value
)
1123 qtest_write(s
, "writel", addr
, value
);
1126 void qtest_writeq(QTestState
*s
, uint64_t addr
, uint64_t value
)
1128 qtest_write(s
, "writeq", addr
, value
);
1131 static uint64_t qtest_read(QTestState
*s
, const char *cmd
, uint64_t addr
)
1137 qtest_sendf(s
, "%s 0x%" PRIx64
"\n", cmd
, addr
);
1138 args
= qtest_rsp_args(s
, 2);
1139 ret
= qemu_strtou64(args
[1], NULL
, 0, &value
);
1146 uint8_t qtest_readb(QTestState
*s
, uint64_t addr
)
1148 return qtest_read(s
, "readb", addr
);
1151 uint16_t qtest_readw(QTestState
*s
, uint64_t addr
)
1153 return qtest_read(s
, "readw", addr
);
1156 uint32_t qtest_readl(QTestState
*s
, uint64_t addr
)
1158 return qtest_read(s
, "readl", addr
);
1161 uint64_t qtest_readq(QTestState
*s
, uint64_t addr
)
1163 return qtest_read(s
, "readq", addr
);
1166 static int hex2nib(char ch
)
1168 if (ch
>= '0' && ch
<= '9') {
1170 } else if (ch
>= 'a' && ch
<= 'f') {
1171 return 10 + (ch
- 'a');
1172 } else if (ch
>= 'A' && ch
<= 'F') {
1173 return 10 + (ch
- 'a');
1179 void qtest_memread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
1181 uint8_t *ptr
= data
;
1189 qtest_sendf(s
, "read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
1190 args
= qtest_rsp_args(s
, 2);
1192 for (i
= 0; i
< size
; i
++) {
1193 ptr
[i
] = hex2nib(args
[1][2 + (i
* 2)]) << 4;
1194 ptr
[i
] |= hex2nib(args
[1][2 + (i
* 2) + 1]);
1200 uint64_t qtest_rtas_call(QTestState
*s
, const char *name
,
1201 uint32_t nargs
, uint64_t args
,
1202 uint32_t nret
, uint64_t ret
)
1204 qtest_sendf(s
, "rtas %s %u 0x%"PRIx64
" %u 0x%"PRIx64
"\n",
1205 name
, nargs
, args
, nret
, ret
);
1210 void qtest_add_func(const char *str
, void (*fn
)(void))
1212 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
1213 g_test_add_func(path
, fn
);
1217 void qtest_add_data_func_full(const char *str
, void *data
,
1218 void (*fn
)(const void *),
1219 GDestroyNotify data_free_func
)
1221 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
1222 g_test_add_data_func_full(path
, data
, fn
, data_free_func
);
1226 void qtest_add_data_func(const char *str
, const void *data
,
1227 void (*fn
)(const void *))
1229 gchar
*path
= g_strdup_printf("/%s/%s", qtest_get_arch(), str
);
1230 g_test_add_data_func(path
, data
, fn
);
1234 void qtest_bufwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
1238 bdata
= g_base64_encode(data
, size
);
1239 qtest_sendf(s
, "b64write 0x%" PRIx64
" 0x%zx ", addr
, size
);
1240 s
->ops
.send(s
, bdata
);
1241 s
->ops
.send(s
, "\n");
1246 void qtest_bufread(QTestState
*s
, uint64_t addr
, void *data
, size_t size
)
1251 qtest_sendf(s
, "b64read 0x%" PRIx64
" 0x%zx\n", addr
, size
);
1252 args
= qtest_rsp_args(s
, 2);
1254 g_base64_decode_inplace(args
[1], &len
);
1256 fprintf(stderr
, "bufread: asked for %zu bytes but decoded %zu\n",
1258 len
= MIN(len
, size
);
1261 memcpy(data
, args
[1], len
);
1265 void qtest_memwrite(QTestState
*s
, uint64_t addr
, const void *data
, size_t size
)
1267 const uint8_t *ptr
= data
;
1275 enc
= g_malloc(2 * size
+ 1);
1277 for (i
= 0; i
< size
; i
++) {
1278 sprintf(&enc
[i
* 2], "%02x", ptr
[i
]);
1281 qtest_sendf(s
, "write 0x%" PRIx64
" 0x%zx 0x%s\n", addr
, size
, enc
);
1286 void qtest_memset(QTestState
*s
, uint64_t addr
, uint8_t pattern
, size_t size
)
1288 qtest_sendf(s
, "memset 0x%" PRIx64
" 0x%zx 0x%02x\n", addr
, size
, pattern
);
1292 QDict
*qtest_vqmp_assert_failure_ref(QTestState
*qts
,
1293 const char *fmt
, va_list args
)
1298 response
= qtest_vqmp(qts
, fmt
, args
);
1301 if (!qdict_haskey(response
, "error")) {
1302 g_autoptr(GString
) s
= qobject_to_json_pretty(QOBJECT(response
), true);
1303 g_test_message("%s", s
->str
);
1305 g_assert(qdict_haskey(response
, "error"));
1306 g_assert(!qdict_haskey(response
, "return"));
1307 ret
= qdict_get_qdict(response
, "error");
1309 qobject_unref(response
);
1314 QDict
*qtest_vqmp_assert_success_ref(QTestState
*qts
,
1315 const char *fmt
, va_list args
)
1320 response
= qtest_vqmp(qts
, fmt
, args
);
1323 if (!qdict_haskey(response
, "return")) {
1324 g_autoptr(GString
) s
= qobject_to_json_pretty(QOBJECT(response
), true);
1325 g_test_message("%s", s
->str
);
1327 g_assert(qdict_haskey(response
, "return"));
1328 ret
= qdict_get_qdict(response
, "return");
1330 qobject_unref(response
);
1335 void qtest_vqmp_assert_success(QTestState
*qts
,
1336 const char *fmt
, va_list args
)
1340 response
= qtest_vqmp_assert_success_ref(qts
, fmt
, args
);
1342 qobject_unref(response
);
1346 QDict
*qtest_vqmp_fds_assert_success_ref(QTestState
*qts
, int *fds
, size_t nfds
,
1347 const char *fmt
, va_list args
)
1352 response
= qtest_vqmp_fds(qts
, fds
, nfds
, fmt
, args
);
1355 if (!qdict_haskey(response
, "return")) {
1356 g_autoptr(GString
) s
= qobject_to_json_pretty(QOBJECT(response
), true);
1357 g_test_message("%s", s
->str
);
1359 g_assert(qdict_haskey(response
, "return"));
1360 ret
= qdict_get_qdict(response
, "return");
1362 qobject_unref(response
);
1367 void qtest_vqmp_fds_assert_success(QTestState
*qts
, int *fds
, size_t nfds
,
1368 const char *fmt
, va_list args
)
1371 response
= qtest_vqmp_fds_assert_success_ref(qts
, fds
, nfds
, fmt
, args
);
1372 qobject_unref(response
);
1374 #endif /* !_WIN32 */
1376 QDict
*qtest_qmp_assert_failure_ref(QTestState
*qts
, const char *fmt
, ...)
1382 response
= qtest_vqmp_assert_failure_ref(qts
, fmt
, ap
);
1387 QDict
*qtest_qmp_assert_success_ref(QTestState
*qts
, const char *fmt
, ...)
1392 response
= qtest_vqmp_assert_success_ref(qts
, fmt
, ap
);
1397 void qtest_qmp_assert_success(QTestState
*qts
, const char *fmt
, ...)
1401 qtest_vqmp_assert_success(qts
, fmt
, ap
);
1406 QDict
*qtest_qmp_fds_assert_success_ref(QTestState
*qts
, int *fds
, size_t nfds
,
1407 const char *fmt
, ...)
1412 response
= qtest_vqmp_fds_assert_success_ref(qts
, fds
, nfds
, fmt
, ap
);
1417 void qtest_qmp_fds_assert_success(QTestState
*qts
, int *fds
, size_t nfds
,
1418 const char *fmt
, ...)
1422 qtest_vqmp_fds_assert_success(qts
, fds
, nfds
, fmt
, ap
);
1425 #endif /* !_WIN32 */
1427 bool qtest_big_endian(QTestState
*s
)
1429 return s
->big_endian
;
1432 static bool qtest_check_machine_version(const char *mname
, const char *basename
,
1433 int major
, int minor
)
1438 newname
= g_strdup_printf("%s-%i.%i", basename
, major
, minor
);
1439 is_equal
= g_str_equal(mname
, newname
);
1445 static bool qtest_is_old_versioned_machine(const char *mname
)
1447 const char *dash
= strrchr(mname
, '-');
1448 const char *dot
= strrchr(mname
, '.');
1451 const int major
= QEMU_VERSION_MAJOR
;
1452 const int minor
= QEMU_VERSION_MINOR
;
1455 if (dash
&& dot
&& dot
> dash
) {
1456 for (chr
= dash
+ 1; *chr
; chr
++) {
1457 if (!qemu_isdigit(*chr
) && *chr
!= '.') {
1462 * Now check if it is one of the latest versions. Check major + 1
1463 * and minor + 1 versions as well, since they might already exist
1464 * in the development branch.
1466 bname
= g_strdup(mname
);
1467 bname
[dash
- mname
] = 0;
1468 res
= !qtest_check_machine_version(mname
, bname
, major
+ 1, 0) &&
1469 !qtest_check_machine_version(mname
, bname
, major
, minor
+ 1) &&
1470 !qtest_check_machine_version(mname
, bname
, major
, minor
);
1488 static void qtest_free_machine_list(struct MachInfo
*machines
)
1491 for (int i
= 0; machines
[i
].name
!= NULL
; i
++) {
1492 g_free(machines
[i
].name
);
1493 g_free(machines
[i
].alias
);
1501 * Returns an array with pointers to the available machine names.
1502 * The terminating entry has the name set to NULL.
1504 static struct MachInfo
*qtest_get_machines(const char *var
)
1506 static struct MachInfo
*machines
;
1507 static char *qemu_var
;
1508 QDict
*response
, *minfo
;
1510 const QListEntry
*p
;
1516 if (g_strcmp0(qemu_var
, var
)) {
1517 qemu_var
= g_strdup(var
);
1519 /* new qemu, clear the cache */
1520 qtest_free_machine_list(machines
);
1528 silence_spawn_log
= !g_test_verbose();
1530 qts
= qtest_init_with_env(qemu_var
, "-machine none");
1531 response
= qtest_qmp(qts
, "{ 'execute': 'query-machines' }");
1533 list
= qdict_get_qlist(response
, "return");
1536 machines
= g_new(struct MachInfo
, qlist_size(list
) + 1);
1538 for (p
= qlist_first(list
), idx
= 0; p
; p
= qlist_next(p
), idx
++) {
1539 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1542 qobj
= qdict_get(minfo
, "name");
1544 qstr
= qobject_to(QString
, qobj
);
1546 machines
[idx
].name
= g_strdup(qstring_get_str(qstr
));
1548 qobj
= qdict_get(minfo
, "alias");
1549 if (qobj
) { /* The alias is optional */
1550 qstr
= qobject_to(QString
, qobj
);
1552 machines
[idx
].alias
= g_strdup(qstring_get_str(qstr
));
1554 machines
[idx
].alias
= NULL
;
1559 qobject_unref(response
);
1561 silence_spawn_log
= false;
1563 memset(&machines
[idx
], 0, sizeof(struct MachInfo
)); /* Terminating entry */
1567 static struct CpuModel
*qtest_get_cpu_models(void)
1569 static struct CpuModel
*cpus
;
1570 QDict
*response
, *minfo
;
1572 const QListEntry
*p
;
1583 silence_spawn_log
= !g_test_verbose();
1585 qts
= qtest_init_with_env(NULL
, "-machine none");
1586 response
= qtest_qmp(qts
, "{ 'execute': 'query-cpu-definitions' }");
1588 list
= qdict_get_qlist(response
, "return");
1591 cpus
= g_new0(struct CpuModel
, qlist_size(list
) + 1);
1593 for (p
= qlist_first(list
), idx
= 0; p
; p
= qlist_next(p
), idx
++) {
1594 minfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1597 qobj
= qdict_get(minfo
, "name");
1599 qstr
= qobject_to(QString
, qobj
);
1601 cpus
[idx
].name
= g_strdup(qstring_get_str(qstr
));
1603 qobj
= qdict_get(minfo
, "alias_of");
1604 if (qobj
) { /* old machines do not report aliases */
1605 qstr
= qobject_to(QString
, qobj
);
1607 cpus
[idx
].alias_of
= g_strdup(qstring_get_str(qstr
));
1609 cpus
[idx
].alias_of
= NULL
;
1612 qobj
= qdict_get(minfo
, "deprecated");
1613 qbool
= qobject_to(QBool
, qobj
);
1615 cpus
[idx
].deprecated
= qbool_get_bool(qbool
);
1619 qobject_unref(response
);
1621 silence_spawn_log
= false;
1626 bool qtest_has_cpu_model(const char *cpu
)
1628 struct CpuModel
*cpus
;
1631 cpus
= qtest_get_cpu_models();
1633 for (i
= 0; cpus
[i
].name
!= NULL
; i
++) {
1634 if (g_str_equal(cpu
, cpus
[i
].name
) ||
1635 (cpus
[i
].alias_of
&& g_str_equal(cpu
, cpus
[i
].alias_of
))) {
1643 void qtest_cb_for_every_machine(void (*cb
)(const char *machine
),
1644 bool skip_old_versioned
)
1646 struct MachInfo
*machines
;
1649 machines
= qtest_get_machines(NULL
);
1651 for (i
= 0; machines
[i
].name
!= NULL
; i
++) {
1652 /* Ignore machines that cannot be used for qtests */
1653 if (!strncmp("xenfv", machines
[i
].name
, 5) ||
1654 g_str_equal("xenpv", machines
[i
].name
) ||
1655 g_str_equal("xenpvh", machines
[i
].name
)) {
1658 if (!skip_old_versioned
||
1659 !qtest_is_old_versioned_machine(machines
[i
].name
)) {
1660 cb(machines
[i
].name
);
1665 char *qtest_resolve_machine_alias(const char *var
, const char *alias
)
1667 struct MachInfo
*machines
;
1670 machines
= qtest_get_machines(var
);
1672 for (i
= 0; machines
[i
].name
!= NULL
; i
++) {
1673 if (machines
[i
].alias
&& g_str_equal(alias
, machines
[i
].alias
)) {
1674 return g_strdup(machines
[i
].name
);
1681 bool qtest_has_machine_with_env(const char *var
, const char *machine
)
1683 struct MachInfo
*machines
;
1686 machines
= qtest_get_machines(var
);
1688 for (i
= 0; machines
[i
].name
!= NULL
; i
++) {
1689 if (g_str_equal(machine
, machines
[i
].name
) ||
1690 (machines
[i
].alias
&& g_str_equal(machine
, machines
[i
].alias
))) {
1698 bool qtest_has_machine(const char *machine
)
1700 return qtest_has_machine_with_env(NULL
, machine
);
1703 bool qtest_has_device(const char *device
)
1706 const QListEntry
*p
;
1715 QTestState
*qts
= qtest_init("-machine none");
1718 qdict_put_bool(args
, "abstract", false);
1719 qdict_put_str(args
, "implements", "device");
1721 resp
= qtest_qmp(qts
, "{'execute': 'qom-list-types', 'arguments': %p }",
1723 g_assert(qdict_haskey(resp
, "return"));
1724 list
= qdict_get_qlist(resp
, "return");
1726 qobject_unref(resp
);
1731 for (p
= qlist_first(list
), idx
= 0; p
; p
= qlist_next(p
), idx
++) {
1732 devinfo
= qobject_to(QDict
, qlist_entry_obj(p
));
1735 qobj
= qdict_get(devinfo
, "name");
1737 qstr
= qobject_to(QString
, qobj
);
1739 if (g_str_equal(qstring_get_str(qstr
), device
)) {
1748 * Generic hot-plugging test via the device_add QMP commands.
1750 void qtest_qmp_device_add_qdict(QTestState
*qts
, const char *drv
,
1751 const QDict
*arguments
)
1754 QDict
*args
= arguments
? qdict_clone_shallow(arguments
) : qdict_new();
1756 g_assert(!qdict_haskey(args
, "driver"));
1757 qdict_put_str(args
, "driver", drv
);
1758 resp
= qtest_qmp(qts
, "{'execute': 'device_add', 'arguments': %p}", args
);
1760 g_assert(!qdict_haskey(resp
, "event")); /* We don't expect any events */
1761 if (qdict_haskey(resp
, "error")) {
1762 fprintf(stderr
, "error: %s\n",
1763 qdict_get_str(qdict_get_qdict(resp
, "error"), "desc"));
1765 g_assert(!qdict_haskey(resp
, "error"));
1766 qobject_unref(resp
);
1769 void qtest_qmp_device_add(QTestState
*qts
, const char *driver
, const char *id
,
1770 const char *fmt
, ...)
1776 args
= qdict_from_vjsonf_nofail(fmt
, ap
);
1779 g_assert(!qdict_haskey(args
, "id"));
1780 qdict_put_str(args
, "id", id
);
1782 qtest_qmp_device_add_qdict(qts
, driver
, args
);
1783 qobject_unref(args
);
1786 void qtest_qmp_add_client(QTestState
*qts
, const char *protocol
, int fd
)
1791 WSAPROTOCOL_INFOW info
;
1792 g_autofree
char *info64
= NULL
;
1795 assert(fd_is_socket(fd
));
1796 s
= _get_osfhandle(fd
);
1797 if (WSADuplicateSocketW(s
, GetProcessId((HANDLE
)qts
->qemu_pid
), &info
) == SOCKET_ERROR
) {
1798 g_autofree
char *emsg
= g_win32_error_message(WSAGetLastError());
1799 g_error("WSADuplicateSocketW failed: %s", emsg
);
1801 info64
= g_base64_encode((guchar
*)&info
, sizeof(info
));
1802 resp
= qtest_qmp(qts
, "{'execute': 'get-win32-socket',"
1803 "'arguments': {'fdname': 'fdname', 'info': %s}}", info64
);
1805 resp
= qtest_qmp_fds(qts
, &fd
, 1, "{'execute': 'getfd',"
1806 "'arguments': {'fdname': 'fdname'}}");
1809 g_assert(!qdict_haskey(resp
, "event")); /* We don't expect any events */
1810 g_assert(!qdict_haskey(resp
, "error"));
1811 qobject_unref(resp
);
1814 qts
, "{'execute': 'add_client',"
1815 "'arguments': {'protocol': %s, 'fdname': 'fdname'}}", protocol
);
1817 g_assert(!qdict_haskey(resp
, "event")); /* We don't expect any events */
1818 g_assert(!qdict_haskey(resp
, "error"));
1819 qobject_unref(resp
);
1823 * Generic hot-unplugging test via the device_del QMP command.
1824 * Device deletion will get one response and one event. For example:
1826 * {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
1828 * will get this one:
1830 * {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
1831 * "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
1832 * "path": "/machine/peripheral/scsi-hd"}}
1838 void qtest_qmp_device_del_send(QTestState
*qts
, const char *id
)
1840 QDict
*rsp
= qtest_qmp(qts
, "{'execute': 'device_del', "
1841 "'arguments': {'id': %s}}", id
);
1843 g_assert(qdict_haskey(rsp
, "return"));
1844 g_assert(!qdict_haskey(rsp
, "error"));
1848 void qtest_qmp_device_del(QTestState
*qts
, const char *id
)
1850 qtest_qmp_device_del_send(qts
, id
);
1851 qtest_qmp_eventwait(qts
, "DEVICE_DELETED");
1854 static void qtest_client_set_tx_handler(QTestState
*s
,
1859 static void qtest_client_set_rx_handler(QTestState
*s
, QTestRecvFn recv
)
1861 s
->ops
.recv_line
= recv
;
1863 /* A type-safe wrapper for s->send() */
1864 static void send_wrapper(QTestState
*s
, const char *buf
)
1866 s
->ops
.external_send(s
, buf
);
1869 static GString
*qtest_client_inproc_recv_line(QTestState
*s
)
1875 eol
= strchr(s
->rx
->str
, '\n');
1876 offset
= eol
- s
->rx
->str
;
1877 line
= g_string_new_len(s
->rx
->str
, offset
);
1878 g_string_erase(s
->rx
, 0, offset
+ 1);
1882 QTestState
*qtest_inproc_init(QTestState
**s
, bool log
, const char* arch
,
1883 void (*send
)(void*, const char*))
1886 qts
= g_new0(QTestState
, 1);
1887 qts
->pending_events
= NULL
;
1888 *s
= qts
; /* Expose qts early on, since the query endianness relies on it */
1890 for (int i
= 0; i
< MAX_IRQ
; i
++) {
1891 qts
->irq_level
[i
] = false;
1894 qtest_client_set_rx_handler(qts
, qtest_client_inproc_recv_line
);
1896 /* send() may not have a matching prototype, so use a type-safe wrapper */
1897 qts
->ops
.external_send
= send
;
1898 qtest_client_set_tx_handler(qts
, send_wrapper
);
1900 qts
->big_endian
= qtest_query_target_endianness(qts
);
1903 * Set a dummy path for QTEST_QEMU_BINARY. Doesn't need to exist, but this
1904 * way, qtest_get_arch works for inproc qtest.
1906 gchar
*bin_path
= g_strconcat("/qemu-system-", arch
, NULL
);
1907 if (!g_setenv("QTEST_QEMU_BINARY", bin_path
, 0)) {
1909 "Could not set environment variable QTEST_QEMU_BINARY\n");
1917 void qtest_client_inproc_recv(void *opaque
, const char *str
)
1919 QTestState
*qts
= *(QTestState
**)opaque
;
1922 qts
->rx
= g_string_new(NULL
);
1924 g_string_append(qts
->rx
, str
);
1928 void qtest_qom_set_bool(QTestState
*s
, const char *path
, const char *property
,
1933 r
= qtest_qmp(s
, "{ 'execute': 'qom-set', 'arguments': "
1934 "{ 'path': %s, 'property': %s, 'value': %i } }",
1935 path
, property
, value
);
1939 bool qtest_qom_get_bool(QTestState
*s
, const char *path
, const char *property
)
1944 r
= qtest_qmp(s
, "{ 'execute': 'qom-get', 'arguments': "
1945 "{ 'path': %s, 'property': %s } }", path
, property
);
1946 b
= qdict_get_bool(r
, "return");
1952 bool have_qemu_img(void)
1955 const char *path
= getenv("QTEST_QEMU_IMG");
1960 rpath
= realpath(path
, NULL
);
1969 bool mkimg(const char *file
, const char *fmt
, unsigned size_mb
)
1975 char *qemu_img_path
;
1977 char *qemu_img_abs_path
;
1979 qemu_img_path
= getenv("QTEST_QEMU_IMG");
1980 if (!qemu_img_path
) {
1983 qemu_img_abs_path
= realpath(qemu_img_path
, NULL
);
1984 if (!qemu_img_abs_path
) {
1988 cli
= g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path
,
1989 fmt
, file
, size_mb
);
1990 ret
= g_spawn_command_line_sync(cli
, &out
, &out2
, &rc
, &err
);
1991 if (err
|| !g_spawn_check_exit_status(rc
, &err
)) {
1992 fprintf(stderr
, "%s\n", err
->message
);
1999 free(qemu_img_abs_path
);