2012-03-28 15:42:05 +02:00
|
|
|
/*
|
|
|
|
* QTest
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2012
|
|
|
|
* Copyright Red Hat, Inc. 2012
|
2013-02-16 22:44:03 +01:00
|
|
|
* Copyright SUSE LINUX Products GmbH 2013
|
2012-03-28 15:42:05 +02:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
* Paolo Bonzini <pbonzini@redhat.com>
|
2013-02-16 22:44:03 +01:00
|
|
|
* Andreas Färber <afaerber@suse.de>
|
2012-03-28 15:42:05 +02:00
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
2018-02-01 12:18:38 +01:00
|
|
|
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifndef _WIN32
|
2012-03-28 15:42:05 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/un.h>
|
2022-10-28 06:57:28 +02:00
|
|
|
#endif /* _WIN32 */
|
tests/qtest: use prctl(PR_SET_PDEATHSIG) as fallback to kill QEMU
Although we register a ABRT handler to kill off QEMU when g_assert()
triggers, we want an extra safety net. The QEMU process might be
non-functional and thus not have responded to SIGTERM. The test script
might also have crashed with SEGV, in which case the cleanup handlers
won't ever run.
Using the Linux specific prctl(PR_SET_PDEATHSIG) syscall, we
can ensure that QEMU gets sent SIGKILL as soon as the controlling
qtest exits, if nothing else has correctly told it to quit.
Note, technically the death signal is sent when the *thread* that
called fork() exits. IOW, if you are calling qtest_init() in one
thread, letting that thread exit, and then expecting to run
qtest_quit() in a different thread, things are not going to work
out. Fortunately that is not a scenario that exists in qtests,
as pairs of qtest_init and qtest_quit are always called from the
same thread.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20220513154906.206715-3-berrange@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
2022-05-13 17:49:06 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#endif /* __linux__ */
|
2023-09-14 17:54:18 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/procctl.h>
|
|
|
|
#endif /* __FreeBSD__ */
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2022-03-30 11:39:05 +02:00
|
|
|
#include "libqtest.h"
|
2022-03-30 11:34:11 +02:00
|
|
|
#include "libqmp.h"
|
2019-05-23 16:35:06 +02:00
|
|
|
#include "qemu/ctype.h"
|
2017-09-11 19:19:50 +02:00
|
|
|
#include "qemu/cutils.h"
|
2022-10-28 06:57:27 +02:00
|
|
|
#include "qemu/sockets.h"
|
2018-02-01 12:18:39 +01:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2014-06-13 09:35:18 +02:00
|
|
|
#include "qapi/qmp/qjson.h"
|
2018-02-01 12:18:38 +01:00
|
|
|
#include "qapi/qmp/qlist.h"
|
2018-02-01 12:18:40 +01:00
|
|
|
#include "qapi/qmp/qstring.h"
|
2012-03-28 15:42:05 +02:00
|
|
|
|
|
|
|
#define MAX_IRQ 256
|
2020-02-20 05:11:02 +01:00
|
|
|
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifndef _WIN32
|
2022-10-28 06:57:34 +02:00
|
|
|
# define SOCKET_TIMEOUT 50
|
2022-10-28 06:57:28 +02:00
|
|
|
# define CMD_EXEC "exec "
|
|
|
|
# define DEV_STDERR "/dev/fd/2"
|
|
|
|
# define DEV_NULL "/dev/null"
|
|
|
|
#else
|
2022-10-28 06:57:34 +02:00
|
|
|
# define SOCKET_TIMEOUT 50000
|
2022-10-28 06:57:28 +02:00
|
|
|
# define CMD_EXEC ""
|
|
|
|
# define DEV_STDERR "2"
|
|
|
|
# define DEV_NULL "nul"
|
|
|
|
#endif
|
|
|
|
|
2023-01-12 15:34:13 +01:00
|
|
|
#define WAITPID_TIMEOUT 30
|
|
|
|
|
2020-02-20 05:11:02 +01:00
|
|
|
typedef void (*QTestSendFn)(QTestState *s, const char *buf);
|
2020-02-20 05:11:03 +01:00
|
|
|
typedef void (*ExternalSendFn)(void *s, const char *buf);
|
2020-02-20 05:11:02 +01:00
|
|
|
typedef GString* (*QTestRecvFn)(QTestState *);
|
|
|
|
|
|
|
|
typedef struct QTestClientTransportOps {
|
|
|
|
QTestSendFn send; /* for sending qtest commands */
|
2020-02-20 05:11:03 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* use external_send to send qtest command strings through functions which
|
|
|
|
* do not accept a QTestState as the first parameter.
|
|
|
|
*/
|
|
|
|
ExternalSendFn external_send;
|
|
|
|
|
2020-02-20 05:11:02 +01:00
|
|
|
QTestRecvFn recv_line; /* for receiving qtest command responses */
|
|
|
|
} QTestTransportOps;
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
struct QTestState
|
|
|
|
{
|
|
|
|
int fd;
|
2012-04-26 19:07:55 +02:00
|
|
|
int qmp_fd;
|
2014-02-17 14:54:17 +01:00
|
|
|
pid_t qemu_pid; /* our child QEMU process */
|
2018-12-17 04:58:16 +01:00
|
|
|
int wstatus;
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD exit_code;
|
|
|
|
#endif
|
2019-09-03 18:22:45 +02:00
|
|
|
int expected_status;
|
2016-10-07 12:14:27 +02:00
|
|
|
bool big_endian;
|
2018-12-17 04:58:16 +01:00
|
|
|
bool irq_level[MAX_IRQ];
|
|
|
|
GString *rx;
|
2020-02-20 05:11:02 +01:00
|
|
|
QTestTransportOps ops;
|
2020-10-06 14:38:53 +02:00
|
|
|
GList *pending_events;
|
2023-06-01 18:13:39 +02:00
|
|
|
QTestQMPEventCallback eventCB;
|
|
|
|
void *eventData;
|
2012-03-28 15:42:05 +02:00
|
|
|
};
|
|
|
|
|
2015-06-19 18:45:14 +02:00
|
|
|
static GHookList abrt_hooks;
|
2022-10-06 17:19:19 +02:00
|
|
|
static void (*sighandler_old)(int);
|
2014-03-27 15:09:50 +01:00
|
|
|
|
2016-10-07 12:14:27 +02:00
|
|
|
static int qtest_query_target_endianness(QTestState *s);
|
|
|
|
|
2020-02-20 05:11:02 +01:00
|
|
|
static void qtest_client_socket_send(QTestState*, const char *buf);
|
|
|
|
static void socket_send(int fd, const char *buf, size_t size);
|
|
|
|
|
|
|
|
static GString *qtest_client_socket_recv_line(QTestState *);
|
|
|
|
|
|
|
|
static void qtest_client_set_tx_handler(QTestState *s, QTestSendFn send);
|
|
|
|
static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv);
|
|
|
|
|
2012-04-26 19:07:55 +02:00
|
|
|
static int init_socket(const char *socket_path)
|
|
|
|
{
|
2021-02-23 15:46:43 +01:00
|
|
|
int sock = qtest_socket_server(socket_path);
|
2012-04-26 19:07:55 +02:00
|
|
|
qemu_set_cloexec(sock);
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int socket_accept(int sock)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
socklen_t addrlen;
|
|
|
|
int ret;
|
2022-10-28 06:57:34 +02:00
|
|
|
/*
|
2023-07-14 13:33:27 +02:00
|
|
|
* timeout unit of blocking receive calls is different among platforms.
|
2022-10-28 06:57:34 +02:00
|
|
|
* It's in seconds on non-Windows platforms but milliseconds on Windows.
|
|
|
|
*/
|
|
|
|
#ifndef _WIN32
|
2014-03-11 14:00:34 +01:00
|
|
|
struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
|
|
|
|
.tv_usec = 0 };
|
2022-10-28 06:57:34 +02:00
|
|
|
#else
|
|
|
|
DWORD timeout = SOCKET_TIMEOUT;
|
|
|
|
#endif
|
2014-03-11 14:00:34 +01:00
|
|
|
|
2022-02-18 22:34:50 +01:00
|
|
|
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
|
|
|
|
(void *)&timeout, sizeof(timeout))) {
|
2020-11-03 12:51:12 +01:00
|
|
|
fprintf(stderr, "%s failed to set SO_RCVTIMEO: %s\n",
|
|
|
|
__func__, strerror(errno));
|
2023-02-21 13:48:01 +01:00
|
|
|
close(sock);
|
2020-11-03 12:51:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2012-04-26 19:07:55 +02:00
|
|
|
|
|
|
|
do {
|
2014-04-17 19:21:12 +02:00
|
|
|
addrlen = sizeof(addr);
|
2012-04-26 19:07:55 +02:00
|
|
|
ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
|
|
|
|
} while (ret == -1 && errno == EINTR);
|
2014-04-17 18:38:25 +02:00
|
|
|
if (ret == -1) {
|
|
|
|
fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
|
|
|
|
}
|
2023-02-21 13:48:01 +01:00
|
|
|
close(sock);
|
2012-04-26 19:07:55 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-06 13:56:43 +02:00
|
|
|
pid_t qtest_pid(QTestState *s)
|
|
|
|
{
|
|
|
|
return s->qemu_pid;
|
|
|
|
}
|
|
|
|
|
2018-12-17 04:58:16 +01:00
|
|
|
bool qtest_probe_child(QTestState *s)
|
2014-02-17 16:33:35 +01:00
|
|
|
{
|
2018-12-17 04:58:16 +01:00
|
|
|
pid_t pid = s->qemu_pid;
|
|
|
|
|
|
|
|
if (pid != -1) {
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifndef _WIN32
|
2018-12-17 04:58:16 +01:00
|
|
|
pid = waitpid(pid, &s->wstatus, WNOHANG);
|
|
|
|
if (pid == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-10-28 06:57:28 +02:00
|
|
|
#else
|
|
|
|
GetExitCodeProcess((HANDLE)pid, &s->exit_code);
|
|
|
|
if (s->exit_code == STILL_ACTIVE) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
CloseHandle((HANDLE)pid);
|
|
|
|
#endif
|
2018-12-17 04:58:16 +01:00
|
|
|
s->qemu_pid = -1;
|
2023-01-13 12:01:20 +01:00
|
|
|
qtest_remove_abrt_handler(s);
|
2018-12-17 04:58:16 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-05-24 16:24:35 +02:00
|
|
|
|
2019-09-03 18:22:45 +02:00
|
|
|
void qtest_set_expected_status(QTestState *s, int status)
|
|
|
|
{
|
|
|
|
s->expected_status = status;
|
|
|
|
}
|
|
|
|
|
2022-10-28 06:57:31 +02:00
|
|
|
static void qtest_check_status(QTestState *s)
|
2018-12-17 04:58:16 +01:00
|
|
|
{
|
2023-01-13 12:01:20 +01:00
|
|
|
assert(s->qemu_pid == -1);
|
|
|
|
|
2018-12-17 04:58:16 +01:00
|
|
|
/*
|
2019-09-03 18:22:45 +02:00
|
|
|
* Check whether qemu exited with expected exit status; anything else is
|
2018-12-17 04:58:16 +01:00
|
|
|
* fishy and should be logged with as much detail as possible.
|
|
|
|
*/
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifndef _WIN32
|
2022-10-28 06:57:31 +02:00
|
|
|
int wstatus = s->wstatus;
|
2019-09-03 18:22:45 +02:00
|
|
|
if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != s->expected_status) {
|
|
|
|
fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
|
|
|
|
"process but encountered exit status %d (expected %d)\n",
|
|
|
|
__FILE__, __LINE__, WEXITSTATUS(wstatus), s->expected_status);
|
|
|
|
abort();
|
|
|
|
} else if (WIFSIGNALED(wstatus)) {
|
|
|
|
int sig = WTERMSIG(wstatus);
|
|
|
|
const char *signame = strsignal(sig) ?: "unknown ???";
|
|
|
|
const char *dump = WCOREDUMP(wstatus) ? " (core dumped)" : "";
|
|
|
|
|
|
|
|
fprintf(stderr, "%s:%d: kill_qemu() detected QEMU death "
|
|
|
|
"from signal %d (%s)%s\n",
|
|
|
|
__FILE__, __LINE__, sig, signame, dump);
|
2018-12-17 04:58:16 +01:00
|
|
|
abort();
|
2014-02-17 16:33:35 +01:00
|
|
|
}
|
2022-10-28 06:57:28 +02:00
|
|
|
#else
|
|
|
|
if (s->exit_code != s->expected_status) {
|
|
|
|
fprintf(stderr, "%s:%d: kill_qemu() tried to terminate QEMU "
|
|
|
|
"process but encountered exit status %ld (expected %d)\n",
|
|
|
|
__FILE__, __LINE__, s->exit_code, s->expected_status);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
2014-02-17 16:33:35 +01:00
|
|
|
}
|
|
|
|
|
2022-10-28 06:57:31 +02:00
|
|
|
void qtest_wait_qemu(QTestState *s)
|
|
|
|
{
|
2023-01-13 12:01:20 +01:00
|
|
|
if (s->qemu_pid != -1) {
|
2022-10-28 06:57:31 +02:00
|
|
|
#ifndef _WIN32
|
2023-01-13 12:01:20 +01:00
|
|
|
pid_t pid;
|
|
|
|
uint64_t end;
|
2023-01-12 15:34:13 +01:00
|
|
|
|
2023-01-13 12:01:20 +01:00
|
|
|
/* poll for a while until sending SIGKILL */
|
|
|
|
end = g_get_monotonic_time() + WAITPID_TIMEOUT * G_TIME_SPAN_SECOND;
|
2023-01-12 15:34:13 +01:00
|
|
|
|
2023-01-13 12:01:20 +01:00
|
|
|
do {
|
|
|
|
pid = waitpid(s->qemu_pid, &s->wstatus, WNOHANG);
|
|
|
|
if (pid != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
g_usleep(100 * 1000);
|
|
|
|
} while (g_get_monotonic_time() < end);
|
2023-01-12 15:34:13 +01:00
|
|
|
|
2023-01-13 12:01:20 +01:00
|
|
|
if (pid == 0) {
|
|
|
|
kill(s->qemu_pid, SIGKILL);
|
|
|
|
pid = RETRY_ON_EINTR(waitpid(s->qemu_pid, &s->wstatus, 0));
|
|
|
|
}
|
2022-10-28 06:57:31 +02:00
|
|
|
|
2023-01-13 12:01:20 +01:00
|
|
|
assert(pid == s->qemu_pid);
|
2022-10-28 06:57:31 +02:00
|
|
|
#else
|
2023-01-13 12:01:20 +01:00
|
|
|
DWORD ret;
|
2022-10-28 06:57:31 +02:00
|
|
|
|
2023-01-13 12:01:20 +01:00
|
|
|
ret = WaitForSingleObject((HANDLE)s->qemu_pid, INFINITE);
|
|
|
|
assert(ret == WAIT_OBJECT_0);
|
|
|
|
GetExitCodeProcess((HANDLE)s->qemu_pid, &s->exit_code);
|
|
|
|
CloseHandle((HANDLE)s->qemu_pid);
|
2022-10-28 06:57:31 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-13 12:01:20 +01:00
|
|
|
s->qemu_pid = -1;
|
|
|
|
qtest_remove_abrt_handler(s);
|
|
|
|
}
|
2022-10-28 06:57:31 +02:00
|
|
|
qtest_check_status(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_kill_qemu(QTestState *s)
|
|
|
|
{
|
|
|
|
/* Skip wait if qtest_probe_child() already reaped */
|
|
|
|
if (s->qemu_pid != -1) {
|
|
|
|
#ifndef _WIN32
|
|
|
|
kill(s->qemu_pid, SIGTERM);
|
|
|
|
#else
|
|
|
|
TerminateProcess((HANDLE)s->qemu_pid, s->expected_status);
|
|
|
|
#endif
|
|
|
|
qtest_wait_qemu(s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qtest_check_status(s);
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:20:33 +01:00
|
|
|
static void kill_qemu_hook_func(void *s)
|
|
|
|
{
|
2021-02-23 15:46:44 +01:00
|
|
|
qtest_kill_qemu(s);
|
2015-12-02 21:20:33 +01:00
|
|
|
}
|
|
|
|
|
2014-02-17 16:33:35 +01:00
|
|
|
static void sigabrt_handler(int signo)
|
|
|
|
{
|
2015-06-19 18:45:14 +02:00
|
|
|
g_hook_list_invoke(&abrt_hooks, FALSE);
|
2014-03-27 15:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void setup_sigabrt_handler(void)
|
|
|
|
{
|
2022-10-06 17:19:19 +02:00
|
|
|
sighandler_old = signal(SIGABRT, sigabrt_handler);
|
2014-03-27 15:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup_sigabrt_handler(void)
|
|
|
|
{
|
2022-10-06 17:19:19 +02:00
|
|
|
signal(SIGABRT, sighandler_old);
|
2014-02-17 16:33:35 +01:00
|
|
|
}
|
|
|
|
|
2021-02-23 15:46:45 +01:00
|
|
|
static bool hook_list_is_empty(GHookList *hook_list)
|
|
|
|
{
|
|
|
|
GHook *hook = g_hook_first_valid(hook_list, TRUE);
|
|
|
|
|
|
|
|
if (!hook) {
|
2022-05-13 17:49:05 +02:00
|
|
|
return true;
|
2021-02-23 15:46:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
g_hook_unref(hook_list, hook);
|
2022-05-13 17:49:05 +02:00
|
|
|
return false;
|
2021-02-23 15:46:45 +01:00
|
|
|
}
|
|
|
|
|
2015-12-02 21:20:33 +01:00
|
|
|
void qtest_add_abrt_handler(GHookFunc fn, const void *data)
|
2015-06-19 18:45:14 +02:00
|
|
|
{
|
|
|
|
GHook *hook;
|
|
|
|
|
|
|
|
if (!abrt_hooks.is_setup) {
|
|
|
|
g_hook_list_init(&abrt_hooks, sizeof(GHook));
|
|
|
|
}
|
2021-02-23 15:46:45 +01:00
|
|
|
|
|
|
|
/* Only install SIGABRT handler once */
|
|
|
|
if (hook_list_is_empty(&abrt_hooks)) {
|
|
|
|
setup_sigabrt_handler();
|
|
|
|
}
|
2015-06-19 18:45:14 +02:00
|
|
|
|
|
|
|
hook = g_hook_alloc(&abrt_hooks);
|
|
|
|
hook->func = fn;
|
|
|
|
hook->data = (void *)data;
|
|
|
|
|
|
|
|
g_hook_prepend(&abrt_hooks, hook);
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:46:45 +01:00
|
|
|
void qtest_remove_abrt_handler(void *data)
|
|
|
|
{
|
|
|
|
GHook *hook = g_hook_find_data(&abrt_hooks, TRUE, data);
|
2023-01-13 12:01:20 +01:00
|
|
|
|
|
|
|
if (!hook) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:46:45 +01:00
|
|
|
g_hook_destroy_link(&abrt_hooks, hook);
|
|
|
|
|
|
|
|
/* Uninstall SIGABRT handler on last instance */
|
|
|
|
if (hook_list_is_empty(&abrt_hooks)) {
|
|
|
|
cleanup_sigabrt_handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 21:27:30 +02:00
|
|
|
static const char *qtest_qemu_binary(const char *var)
|
2017-08-28 12:25:45 +02:00
|
|
|
{
|
|
|
|
const char *qemu_bin;
|
|
|
|
|
2023-10-18 21:27:30 +02:00
|
|
|
if (var) {
|
|
|
|
qemu_bin = getenv(var);
|
|
|
|
if (qemu_bin) {
|
|
|
|
return qemu_bin;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:25:45 +02:00
|
|
|
qemu_bin = getenv("QTEST_QEMU_BINARY");
|
|
|
|
if (!qemu_bin) {
|
|
|
|
fprintf(stderr, "Environment variable QTEST_QEMU_BINARY required\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return qemu_bin;
|
|
|
|
}
|
|
|
|
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
static pid_t qtest_create_process(char *cmd)
|
|
|
|
{
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
BOOL ret;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
|
|
|
ret = CreateProcess(NULL, /* module name */
|
|
|
|
cmd, /* command line */
|
|
|
|
NULL, /* process handle not inheritable */
|
|
|
|
NULL, /* thread handle not inheritable */
|
|
|
|
FALSE, /* set handle inheritance to FALSE */
|
|
|
|
0, /* No creation flags */
|
|
|
|
NULL, /* use parent's environment block */
|
|
|
|
NULL, /* use parent's starting directory */
|
|
|
|
&si, /* pointer to STARTUPINFO structure */
|
|
|
|
&pi /* pointer to PROCESS_INFORMATION structure */
|
|
|
|
);
|
|
|
|
if (ret == 0) {
|
|
|
|
fprintf(stderr, "%s:%d: unable to create a new process (%s)\n",
|
|
|
|
__FILE__, __LINE__, strerror(GetLastError()));
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
return (pid_t)pi.hProcess;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
2023-10-18 21:27:31 +02:00
|
|
|
static QTestState *G_GNUC_PRINTF(2, 3) qtest_spawn_qemu(const char *qemu_bin,
|
|
|
|
const char *fmt, ...)
|
2012-03-28 15:42:05 +02:00
|
|
|
{
|
2023-01-13 11:27:14 +01:00
|
|
|
va_list ap;
|
|
|
|
QTestState *s = g_new0(QTestState, 1);
|
2022-03-10 18:18:06 +01:00
|
|
|
const char *trace = g_getenv("QTEST_TRACE");
|
|
|
|
g_autofree char *tracearg = trace ?
|
|
|
|
g_strdup_printf("-trace %s ", trace) : g_strdup("");
|
2023-01-13 11:27:14 +01:00
|
|
|
g_autoptr(GString) command = g_string_new("");
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2023-01-13 11:27:14 +01:00
|
|
|
va_start(ap, fmt);
|
2023-10-18 21:27:31 +02:00
|
|
|
g_string_append_printf(command, CMD_EXEC "%s %s", qemu_bin, tracearg);
|
2023-01-13 11:27:14 +01:00
|
|
|
g_string_append_vprintf(command, fmt, ap);
|
|
|
|
va_end(ap);
|
2020-02-20 05:11:02 +01:00
|
|
|
|
2015-12-02 21:20:33 +01:00
|
|
|
qtest_add_abrt_handler(kill_qemu_hook_func, s);
|
2014-02-17 16:33:35 +01:00
|
|
|
|
2023-01-13 11:27:14 +01:00
|
|
|
g_test_message("starting QEMU: %s", command->str);
|
|
|
|
|
2022-10-28 06:57:28 +02:00
|
|
|
#ifndef _WIN32
|
2014-02-17 14:54:17 +01:00
|
|
|
s->qemu_pid = fork();
|
|
|
|
if (s->qemu_pid == 0) {
|
tests/qtest: use prctl(PR_SET_PDEATHSIG) as fallback to kill QEMU
Although we register a ABRT handler to kill off QEMU when g_assert()
triggers, we want an extra safety net. The QEMU process might be
non-functional and thus not have responded to SIGTERM. The test script
might also have crashed with SEGV, in which case the cleanup handlers
won't ever run.
Using the Linux specific prctl(PR_SET_PDEATHSIG) syscall, we
can ensure that QEMU gets sent SIGKILL as soon as the controlling
qtest exits, if nothing else has correctly told it to quit.
Note, technically the death signal is sent when the *thread* that
called fork() exits. IOW, if you are calling qtest_init() in one
thread, letting that thread exit, and then expecting to run
qtest_quit() in a different thread, things are not going to work
out. Fortunately that is not a scenario that exists in qtests,
as pairs of qtest_init and qtest_quit are always called from the
same thread.
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Message-Id: <20220513154906.206715-3-berrange@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
2022-05-13 17:49:06 +02:00
|
|
|
#ifdef __linux__
|
|
|
|
/*
|
|
|
|
* Although we register a ABRT handler to kill off QEMU
|
|
|
|
* when g_assert() triggers, we want an extra safety
|
|
|
|
* net. The QEMU process might be non-functional and
|
|
|
|
* thus not have responded to SIGTERM. The test script
|
|
|
|
* might also have crashed with SEGV, in which case the
|
|
|
|
* cleanup handlers won't ever run.
|
|
|
|
*
|
|
|
|
* This PR_SET_PDEATHSIG setup will ensure any remaining
|
|
|
|
* QEMU will get terminated with SIGKILL in these cases.
|
|
|
|
*/
|
|
|
|
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
|
|
|
|
#endif /* __linux__ */
|
2023-09-14 17:54:18 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
int sig = SIGKILL;
|
|
|
|
procctl(P_PID, getpid(), PROC_PDEATHSIG_CTL, &sig);
|
|
|
|
#endif /* __FreeBSD__ */
|
2023-01-13 11:27:14 +01:00
|
|
|
execlp("/bin/sh", "sh", "-c", command->str, NULL);
|
2013-04-16 16:45:15 +02:00
|
|
|
exit(1);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
2022-10-28 06:57:28 +02:00
|
|
|
#else
|
2023-01-13 11:27:14 +01:00
|
|
|
s->qemu_pid = qtest_create_process(command->str);
|
2022-10-28 06:57:28 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2023-01-13 11:27:14 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-10-18 21:27:31 +02:00
|
|
|
static QTestState *qtest_init_internal(const char *qemu_bin,
|
|
|
|
const char *extra_args)
|
2023-01-13 11:27:14 +01:00
|
|
|
{
|
|
|
|
QTestState *s;
|
|
|
|
int sock, qmpsock, i;
|
|
|
|
gchar *socket_path;
|
|
|
|
gchar *qmp_socket_path;
|
|
|
|
|
|
|
|
socket_path = g_strdup_printf("%s/qtest-%d.sock",
|
|
|
|
g_get_tmp_dir(), getpid());
|
|
|
|
qmp_socket_path = g_strdup_printf("%s/qtest-%d.qmp",
|
|
|
|
g_get_tmp_dir(), getpid());
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's possible that if an earlier test run crashed it might
|
|
|
|
* have left a stale unix socket lying around. Delete any
|
|
|
|
* stale old socket to avoid spurious test failures with
|
|
|
|
* tests/libqtest.c:70:init_socket: assertion failed (ret != -1): (-1 != -1)
|
|
|
|
*/
|
|
|
|
unlink(socket_path);
|
|
|
|
unlink(qmp_socket_path);
|
|
|
|
|
|
|
|
socket_init();
|
|
|
|
sock = init_socket(socket_path);
|
|
|
|
qmpsock = init_socket(qmp_socket_path);
|
|
|
|
|
2023-10-18 21:27:31 +02:00
|
|
|
s = qtest_spawn_qemu(qemu_bin,
|
|
|
|
"-qtest unix:%s "
|
2023-01-13 11:27:14 +01:00
|
|
|
"-qtest-log %s "
|
|
|
|
"-chardev socket,path=%s,id=char0 "
|
|
|
|
"-mon chardev=char0,mode=control "
|
|
|
|
"-display none "
|
2023-10-05 11:45:55 +02:00
|
|
|
"-audio none "
|
2023-01-13 11:27:14 +01:00
|
|
|
"%s"
|
|
|
|
" -accel qtest",
|
|
|
|
socket_path,
|
|
|
|
getenv("QTEST_LOG") ? DEV_STDERR : DEV_NULL,
|
|
|
|
qmp_socket_path,
|
|
|
|
extra_args ?: "");
|
|
|
|
|
|
|
|
qtest_client_set_rx_handler(s, qtest_client_socket_recv_line);
|
|
|
|
qtest_client_set_tx_handler(s, qtest_client_socket_send);
|
|
|
|
|
2012-04-26 19:07:55 +02:00
|
|
|
s->fd = socket_accept(sock);
|
2014-03-11 14:00:34 +01:00
|
|
|
if (s->fd >= 0) {
|
|
|
|
s->qmp_fd = socket_accept(qmpsock);
|
|
|
|
}
|
2013-11-21 11:37:25 +01:00
|
|
|
unlink(socket_path);
|
|
|
|
unlink(qmp_socket_path);
|
|
|
|
g_free(socket_path);
|
|
|
|
g_free(qmp_socket_path);
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2014-03-11 14:00:34 +01:00
|
|
|
g_assert(s->fd >= 0 && s->qmp_fd >= 0);
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
s->rx = g_string_new("");
|
|
|
|
for (i = 0; i < MAX_IRQ; i++) {
|
|
|
|
s->irq_level[i] = false;
|
|
|
|
}
|
|
|
|
|
2022-10-28 06:57:28 +02:00
|
|
|
/*
|
|
|
|
* Stopping QEMU for debugging is not supported on Windows.
|
|
|
|
*
|
|
|
|
* Using DebugActiveProcess() API can suspend the QEMU process,
|
|
|
|
* but gdb cannot attach to the process. Using the undocumented
|
|
|
|
* NtSuspendProcess() can suspend the QEMU process and gdb can
|
|
|
|
* attach to the process, but gdb cannot resume it.
|
|
|
|
*/
|
|
|
|
#ifndef _WIN32
|
2012-10-01 14:18:07 +02:00
|
|
|
if (getenv("QTEST_STOP")) {
|
2013-11-21 11:33:17 +01:00
|
|
|
kill(s->qemu_pid, SIGSTOP);
|
2012-10-01 14:18:07 +02:00
|
|
|
}
|
2022-10-28 06:57:28 +02:00
|
|
|
#endif
|
2012-10-01 14:18:07 +02:00
|
|
|
|
2016-10-07 12:14:27 +02:00
|
|
|
/* ask endianness of the target */
|
|
|
|
|
|
|
|
s->big_endian = qtest_query_target_endianness(s);
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-10-18 21:27:31 +02:00
|
|
|
QTestState *qtest_init_without_qmp_handshake(const char *extra_args)
|
|
|
|
{
|
|
|
|
return qtest_init_internal(qtest_qemu_binary(NULL), extra_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTestState *qtest_init_with_env(const char *var, const char *extra_args)
|
2017-03-03 13:32:23 +01:00
|
|
|
{
|
2023-10-18 21:27:31 +02:00
|
|
|
QTestState *s = qtest_init_internal(qtest_qemu_binary(var), extra_args);
|
2018-08-06 08:53:24 +02:00
|
|
|
QDict *greeting;
|
2017-03-03 13:32:23 +01:00
|
|
|
|
|
|
|
/* Read the QMP greeting and then do the handshake */
|
2020-10-06 14:38:53 +02:00
|
|
|
greeting = qtest_qmp_receive(s);
|
2018-08-06 08:53:24 +02:00
|
|
|
qobject_unref(greeting);
|
2018-08-06 08:53:25 +02:00
|
|
|
qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }"));
|
2017-03-03 13:32:23 +01:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2023-10-18 21:27:31 +02:00
|
|
|
QTestState *qtest_init(const char *extra_args)
|
|
|
|
{
|
|
|
|
return qtest_init_with_env(NULL, extra_args);
|
|
|
|
}
|
|
|
|
|
2018-08-06 08:53:43 +02:00
|
|
|
QTestState *qtest_vinitf(const char *fmt, va_list ap)
|
2017-10-18 16:20:27 +02:00
|
|
|
{
|
|
|
|
char *args = g_strdup_vprintf(fmt, ap);
|
|
|
|
QTestState *s;
|
|
|
|
|
2018-08-06 08:53:43 +02:00
|
|
|
s = qtest_init(args);
|
2017-10-18 16:20:27 +02:00
|
|
|
g_free(args);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-08-06 08:53:43 +02:00
|
|
|
QTestState *qtest_initf(const char *fmt, ...)
|
2017-10-18 16:20:27 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QTestState *s;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 08:53:43 +02:00
|
|
|
s = qtest_vinitf(fmt, ap);
|
2017-10-18 16:20:27 +02:00
|
|
|
va_end(ap);
|
|
|
|
return s;
|
2019-01-29 12:46:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd)
|
|
|
|
{
|
|
|
|
int sock_fd_init;
|
2022-09-25 13:29:59 +02:00
|
|
|
g_autofree char *sock_dir = NULL;
|
|
|
|
char *sock_path;
|
2019-01-29 12:46:04 +01:00
|
|
|
QTestState *qts;
|
|
|
|
|
2022-09-25 13:29:59 +02:00
|
|
|
sock_dir = g_dir_make_tmp("qtest-serial-XXXXXX", NULL);
|
|
|
|
g_assert_true(sock_dir != NULL);
|
2019-01-29 12:46:04 +01:00
|
|
|
sock_path = g_strdup_printf("%s/sock", sock_dir);
|
|
|
|
|
2022-10-28 06:57:28 +02:00
|
|
|
socket_init();
|
2019-01-29 12:46:04 +01:00
|
|
|
sock_fd_init = init_socket(sock_path);
|
|
|
|
|
|
|
|
qts = qtest_initf("-chardev socket,id=s0,path=%s -serial chardev:s0 %s",
|
|
|
|
sock_path, extra_args);
|
|
|
|
|
|
|
|
*sock_fd = socket_accept(sock_fd_init);
|
|
|
|
|
|
|
|
unlink(sock_path);
|
|
|
|
g_free(sock_path);
|
|
|
|
rmdir(sock_dir);
|
|
|
|
|
|
|
|
g_assert_true(*sock_fd >= 0);
|
|
|
|
|
|
|
|
return qts;
|
2017-10-18 16:20:27 +02:00
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
void qtest_quit(QTestState *s)
|
|
|
|
{
|
2021-02-23 15:46:45 +01:00
|
|
|
qtest_remove_abrt_handler(s);
|
2014-03-27 15:09:50 +01:00
|
|
|
|
2021-02-23 15:46:44 +01:00
|
|
|
qtest_kill_qemu(s);
|
2023-02-21 13:48:01 +01:00
|
|
|
close(s->fd);
|
|
|
|
close(s->qmp_fd);
|
2013-06-20 08:55:28 +02:00
|
|
|
g_string_free(s->rx, true);
|
2020-10-06 14:38:53 +02:00
|
|
|
|
|
|
|
for (GList *it = s->pending_events; it != NULL; it = it->next) {
|
|
|
|
qobject_unref((QDict *)it->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free(s->pending_events);
|
|
|
|
|
2013-06-20 08:55:28 +02:00
|
|
|
g_free(s);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
2014-06-13 09:35:18 +02:00
|
|
|
static void socket_send(int fd, const char *buf, size_t size)
|
2012-03-28 15:42:05 +02:00
|
|
|
{
|
2022-10-28 06:57:27 +02:00
|
|
|
ssize_t res = qemu_send_full(fd, buf, size);
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2022-04-20 15:26:15 +02:00
|
|
|
assert(res == size);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 05:11:02 +01:00
|
|
|
static void qtest_client_socket_send(QTestState *s, const char *buf)
|
2014-06-13 09:35:18 +02:00
|
|
|
{
|
2020-02-20 05:11:02 +01:00
|
|
|
socket_send(s->fd, buf, strlen(buf));
|
2014-06-13 09:35:18 +02:00
|
|
|
}
|
|
|
|
|
2022-02-20 17:39:25 +01:00
|
|
|
static void G_GNUC_PRINTF(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
|
2012-04-26 19:07:55 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2020-02-20 05:11:02 +01:00
|
|
|
gchar *str = g_strdup_vprintf(fmt, ap);
|
2012-04-26 19:07:55 +02:00
|
|
|
va_end(ap);
|
2020-02-20 05:11:02 +01:00
|
|
|
|
|
|
|
s->ops.send(s, str);
|
|
|
|
g_free(str);
|
2012-04-26 19:07:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-20 05:11:02 +01:00
|
|
|
static GString *qtest_client_socket_recv_line(QTestState *s)
|
2012-03-28 15:42:05 +02:00
|
|
|
{
|
|
|
|
GString *line;
|
|
|
|
size_t offset;
|
|
|
|
char *eol;
|
|
|
|
|
|
|
|
while ((eol = strchr(s->rx->str, '\n')) == NULL) {
|
|
|
|
ssize_t len;
|
|
|
|
char buffer[1024];
|
|
|
|
|
2022-10-28 06:57:27 +02:00
|
|
|
len = recv(s->fd, buffer, sizeof(buffer), 0);
|
2012-03-28 15:42:05 +02:00
|
|
|
if (len == -1 && errno == EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == -1 || len == 0) {
|
|
|
|
fprintf(stderr, "Broken pipe\n");
|
2018-08-15 16:19:45 +02:00
|
|
|
abort();
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append_len(s->rx, buffer, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = eol - s->rx->str;
|
|
|
|
line = g_string_new_len(s->rx->str, offset);
|
|
|
|
g_string_erase(s->rx, 0, offset + 1);
|
|
|
|
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:16:49 +01:00
|
|
|
static gchar **qtest_rsp_args(QTestState *s, int expected_args)
|
2012-03-28 15:42:05 +02:00
|
|
|
{
|
|
|
|
GString *line;
|
|
|
|
gchar **words;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
redo:
|
2020-02-20 05:11:02 +01:00
|
|
|
line = s->ops.recv_line(s);
|
2012-03-28 15:42:05 +02:00
|
|
|
words = g_strsplit(line->str, " ", 0);
|
|
|
|
g_string_free(line, TRUE);
|
|
|
|
|
|
|
|
if (strcmp(words[0], "IRQ") == 0) {
|
2017-09-11 19:19:50 +02:00
|
|
|
long irq;
|
|
|
|
int ret;
|
2012-03-28 15:42:05 +02:00
|
|
|
|
|
|
|
g_assert(words[1] != NULL);
|
|
|
|
g_assert(words[2] != NULL);
|
|
|
|
|
2017-09-11 19:19:50 +02:00
|
|
|
ret = qemu_strtol(words[2], NULL, 0, &irq);
|
|
|
|
g_assert(!ret);
|
2012-03-28 15:42:05 +02:00
|
|
|
g_assert_cmpint(irq, >=, 0);
|
|
|
|
g_assert_cmpint(irq, <, MAX_IRQ);
|
|
|
|
|
|
|
|
if (strcmp(words[1], "raise") == 0) {
|
|
|
|
s->irq_level[irq] = true;
|
|
|
|
} else {
|
|
|
|
s->irq_level[irq] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(words);
|
|
|
|
goto redo;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert(words[0] != NULL);
|
|
|
|
g_assert_cmpstr(words[0], ==, "OK");
|
|
|
|
|
2021-01-26 16:16:49 +01:00
|
|
|
for (i = 0; i < expected_args; i++) {
|
|
|
|
g_assert(words[i] != NULL);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return words;
|
|
|
|
}
|
|
|
|
|
2021-01-26 16:16:49 +01:00
|
|
|
static void qtest_rsp(QTestState *s)
|
|
|
|
{
|
|
|
|
gchar **words = qtest_rsp_args(s, 0);
|
|
|
|
|
|
|
|
g_strfreev(words);
|
|
|
|
}
|
|
|
|
|
2016-10-07 12:14:27 +02:00
|
|
|
static int qtest_query_target_endianness(QTestState *s)
|
|
|
|
{
|
|
|
|
gchar **args;
|
|
|
|
int big_endian;
|
|
|
|
|
|
|
|
qtest_sendf(s, "endianness\n");
|
2021-01-26 16:16:49 +01:00
|
|
|
args = qtest_rsp_args(s, 1);
|
2016-10-07 12:14:27 +02:00
|
|
|
g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
|
|
|
|
big_endian = strcmp(args[1], "big") == 0;
|
|
|
|
g_strfreev(args);
|
|
|
|
|
|
|
|
return big_endian;
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:38:53 +02:00
|
|
|
QDict *qtest_qmp_receive(QTestState *s)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
QDict *response = qtest_qmp_receive_dict(s);
|
|
|
|
|
|
|
|
if (!qdict_get_try_str(response, "event")) {
|
|
|
|
return response;
|
|
|
|
}
|
2023-06-01 18:13:39 +02:00
|
|
|
|
|
|
|
if (!s->eventCB ||
|
|
|
|
!s->eventCB(s, qdict_get_str(response, "event"),
|
|
|
|
response, s->eventData)) {
|
|
|
|
/* Stash the event for a later consumption */
|
|
|
|
s->pending_events = g_list_append(s->pending_events, response);
|
|
|
|
}
|
2020-10-06 14:38:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:38:52 +02:00
|
|
|
QDict *qtest_qmp_receive_dict(QTestState *s)
|
2015-10-02 14:58:16 +02:00
|
|
|
{
|
|
|
|
return qmp_fd_receive(s->qmp_fd);
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:46:43 +01:00
|
|
|
int qtest_socket_server(const char *socket_path)
|
|
|
|
{
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int sock;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
g_assert_cmpint(sock, !=, -1);
|
|
|
|
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
|
|
|
|
|
2022-10-23 11:04:22 +02:00
|
|
|
ret = RETRY_ON_EINTR(bind(sock, (struct sockaddr *)&addr, sizeof(addr)));
|
2021-02-23 15:46:43 +01:00
|
|
|
g_assert_cmpint(ret, !=, -1);
|
|
|
|
ret = listen(sock, 1);
|
|
|
|
g_assert_cmpint(ret, !=, -1);
|
|
|
|
|
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2022-09-25 13:30:12 +02:00
|
|
|
#ifndef _WIN32
|
2019-05-28 11:16:33 +02:00
|
|
|
void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
qmp_fd_vsend_fds(s->qmp_fd, fds, fds_num, fmt, ap);
|
|
|
|
}
|
2022-09-25 13:30:12 +02:00
|
|
|
#endif
|
2019-05-28 11:16:33 +02:00
|
|
|
|
2018-08-06 08:53:22 +02:00
|
|
|
void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
|
2015-10-02 14:58:16 +02:00
|
|
|
{
|
2022-09-25 13:30:12 +02:00
|
|
|
qmp_fd_vsend(s->qmp_fd, fmt, ap);
|
2015-10-02 14:58:16 +02:00
|
|
|
}
|
|
|
|
|
2022-09-25 13:30:12 +02:00
|
|
|
#ifndef _WIN32
|
2019-05-28 11:16:33 +02:00
|
|
|
QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
qtest_qmp_vsend_fds(s, fds, fds_num, fmt, ap);
|
|
|
|
|
|
|
|
/* Receive reply */
|
2020-10-06 14:38:53 +02:00
|
|
|
return qtest_qmp_receive(s);
|
2019-05-28 11:16:33 +02:00
|
|
|
}
|
2022-09-25 13:30:12 +02:00
|
|
|
#endif
|
2019-05-28 11:16:33 +02:00
|
|
|
|
2018-08-06 08:53:44 +02:00
|
|
|
QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
|
2015-04-28 21:27:51 +02:00
|
|
|
{
|
2018-08-06 08:53:22 +02:00
|
|
|
qtest_qmp_vsend(s, fmt, ap);
|
2014-02-21 20:55:30 +01:00
|
|
|
|
|
|
|
/* Receive reply */
|
2020-10-06 14:38:53 +02:00
|
|
|
return qtest_qmp_receive(s);
|
2014-02-21 20:55:30 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 13:30:12 +02:00
|
|
|
#ifndef _WIN32
|
2019-05-28 11:16:33 +02:00
|
|
|
QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
response = qtest_vqmp_fds(s, fds, fds_num, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
2022-09-25 13:30:12 +02:00
|
|
|
#endif
|
2019-05-28 11:16:33 +02:00
|
|
|
|
2013-10-30 14:54:33 +01:00
|
|
|
QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 08:53:44 +02:00
|
|
|
response = qtest_vqmp(s, fmt, ap);
|
2013-10-30 14:54:33 +01:00
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2018-08-06 08:53:22 +02:00
|
|
|
void qtest_qmp_send(QTestState *s, const char *fmt, ...)
|
2015-04-28 21:27:51 +02:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 08:53:22 +02:00
|
|
|
qtest_qmp_vsend(s, fmt, ap);
|
2015-04-28 21:27:51 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2018-08-23 18:39:34 +02:00
|
|
|
void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
qmp_fd_vsend_raw(s->qmp_fd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2023-06-01 18:13:39 +02:00
|
|
|
void qtest_qmp_set_event_callback(QTestState *s,
|
|
|
|
QTestQMPEventCallback cb, void *opaque)
|
|
|
|
{
|
|
|
|
s->eventCB = cb;
|
|
|
|
s->eventData = opaque;
|
|
|
|
}
|
|
|
|
|
2020-10-06 14:38:53 +02:00
|
|
|
QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
|
2015-04-28 21:27:51 +02:00
|
|
|
{
|
2020-10-19 18:37:01 +02:00
|
|
|
while (s->pending_events) {
|
2020-10-06 14:38:53 +02:00
|
|
|
|
2020-10-19 18:37:01 +02:00
|
|
|
GList *first = s->pending_events;
|
|
|
|
QDict *response = (QDict *)first->data;
|
2020-10-06 14:38:53 +02:00
|
|
|
|
2020-10-19 18:37:01 +02:00
|
|
|
s->pending_events = g_list_delete_link(s->pending_events, first);
|
2020-10-06 14:38:53 +02:00
|
|
|
|
|
|
|
if (!strcmp(qdict_get_str(response, "event"), event)) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
qobject_unref(response);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
|
|
|
|
{
|
|
|
|
QDict *response = qtest_qmp_event_ref(s, event);
|
|
|
|
|
|
|
|
if (response) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:27:51 +02:00
|
|
|
for (;;) {
|
2020-10-06 14:38:52 +02:00
|
|
|
response = qtest_qmp_receive_dict(s);
|
2015-04-28 21:27:51 +02:00
|
|
|
if ((qdict_haskey(response, "event")) &&
|
|
|
|
(strcmp(qdict_get_str(response, "event"), event) == 0)) {
|
2016-11-14 17:15:54 +01:00
|
|
|
return response;
|
2015-04-28 21:27:51 +02:00
|
|
|
}
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(response);
|
2015-04-28 21:27:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-14 17:15:54 +01:00
|
|
|
void qtest_qmp_eventwait(QTestState *s, const char *event)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
response = qtest_qmp_eventwait_ref(s, event);
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(response);
|
2016-11-14 17:15:54 +01:00
|
|
|
}
|
|
|
|
|
2018-08-06 08:53:44 +02:00
|
|
|
char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
|
2015-10-01 10:59:55 +02:00
|
|
|
{
|
|
|
|
char *cmd;
|
|
|
|
QDict *resp;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
cmd = g_strdup_vprintf(fmt, ap);
|
|
|
|
resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
|
|
|
|
" 'arguments': {'command-line': %s}}",
|
|
|
|
cmd);
|
|
|
|
ret = g_strdup(qdict_get_try_str(resp, "return"));
|
|
|
|
g_assert(ret);
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(resp);
|
2015-10-01 10:59:55 +02:00
|
|
|
g_free(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *qtest_hmp(QTestState *s, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2018-08-06 08:53:44 +02:00
|
|
|
ret = qtest_vhmp(s, fmt, ap);
|
2015-10-01 10:59:55 +02:00
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
2015-04-28 21:27:51 +02:00
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
const char *qtest_get_arch(void)
|
|
|
|
{
|
2023-10-18 21:27:30 +02:00
|
|
|
const char *qemu = qtest_qemu_binary(NULL);
|
2020-09-30 13:13:52 +02:00
|
|
|
const char *end = strrchr(qemu, '-');
|
2012-03-28 15:42:05 +02:00
|
|
|
|
2020-09-30 13:13:52 +02:00
|
|
|
if (!end) {
|
|
|
|
fprintf(stderr, "Can't determine architecture from binary name.\n");
|
2021-04-12 16:30:50 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strstr(qemu, "-system-")) {
|
|
|
|
fprintf(stderr, "QTEST_QEMU_BINARY must end with *-system-<arch> "
|
|
|
|
"where 'arch' is the target\narchitecture (x86_64, aarch64, "
|
|
|
|
"etc).\n");
|
|
|
|
exit(1);
|
2020-09-30 13:13:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return end + 1;
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
2021-09-02 13:35:38 +02:00
|
|
|
bool qtest_has_accel(const char *accel_name)
|
|
|
|
{
|
|
|
|
if (g_str_equal(accel_name, "tcg")) {
|
|
|
|
#if defined(CONFIG_TCG)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
} else if (g_str_equal(accel_name, "kvm")) {
|
|
|
|
int i;
|
|
|
|
const char *arch = qtest_get_arch();
|
|
|
|
const char *targets[] = { CONFIG_KVM_TARGETS };
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(targets); i++) {
|
|
|
|
if (!strncmp(targets[i], arch, strlen(arch))) {
|
|
|
|
if (!access("/dev/kvm", R_OK | W_OK)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* not implemented */
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
bool qtest_get_irq(QTestState *s, int num)
|
|
|
|
{
|
|
|
|
/* dummy operation in order to make sure irq is up to date */
|
|
|
|
qtest_inb(s, 0);
|
|
|
|
|
|
|
|
return s->irq_level[num];
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:51:40 +02:00
|
|
|
void qtest_module_load(QTestState *s, const char *prefix, const char *libname)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "module_load %s %s\n", prefix, libname);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2019-07-22 20:51:40 +02:00
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
static int64_t qtest_clock_rsp(QTestState *s)
|
|
|
|
{
|
|
|
|
gchar **words;
|
|
|
|
int64_t clock;
|
2021-01-26 16:16:49 +01:00
|
|
|
words = qtest_rsp_args(s, 2);
|
2012-03-28 15:42:05 +02:00
|
|
|
clock = g_ascii_strtoll(words[1], NULL, 0);
|
|
|
|
g_strfreev(words);
|
|
|
|
return clock;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qtest_clock_step_next(QTestState *s)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "clock_step\n");
|
|
|
|
return qtest_clock_rsp(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qtest_clock_step(QTestState *s, int64_t step)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "clock_step %"PRIi64"\n", step);
|
|
|
|
return qtest_clock_rsp(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t qtest_clock_set(QTestState *s, int64_t val)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "clock_set %"PRIi64"\n", val);
|
|
|
|
return qtest_clock_rsp(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
2023-08-22 18:31:00 +02:00
|
|
|
void qtest_irq_intercept_out_named(QTestState *s, const char *qom_path, const char *name)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "irq_intercept_out %s %s\n", qom_path, name);
|
|
|
|
qtest_rsp(s);
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
2019-01-07 16:23:47 +01:00
|
|
|
void qtest_set_irq_in(QTestState *s, const char *qom_path, const char *name,
|
|
|
|
int num, int level)
|
|
|
|
{
|
|
|
|
if (!name) {
|
|
|
|
name = "unnamed-gpio-in";
|
|
|
|
}
|
|
|
|
qtest_sendf(s, "set_irq_in %s %s %d %d\n", qom_path, name, num, level);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2019-01-07 16:23:47 +01:00
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
qtest_out(s, "outb", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
|
|
|
|
{
|
|
|
|
qtest_out(s, "outw", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
qtest_out(s, "outl", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
|
|
|
|
{
|
|
|
|
gchar **args;
|
2017-09-11 19:19:50 +02:00
|
|
|
int ret;
|
|
|
|
unsigned long value;
|
2012-03-28 15:42:05 +02:00
|
|
|
|
|
|
|
qtest_sendf(s, "%s 0x%x\n", cmd, addr);
|
2021-01-26 16:16:49 +01:00
|
|
|
args = qtest_rsp_args(s, 2);
|
2017-09-11 19:19:50 +02:00
|
|
|
ret = qemu_strtoul(args[1], NULL, 0, &value);
|
|
|
|
g_assert(!ret && value <= UINT32_MAX);
|
2012-03-28 15:42:05 +02:00
|
|
|
g_strfreev(args);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t qtest_inb(QTestState *s, uint16_t addr)
|
|
|
|
{
|
|
|
|
return qtest_in(s, "inb", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t qtest_inw(QTestState *s, uint16_t addr)
|
|
|
|
{
|
|
|
|
return qtest_in(s, "inw", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t qtest_inl(QTestState *s, uint16_t addr)
|
|
|
|
{
|
|
|
|
return qtest_in(s, "inl", addr);
|
|
|
|
}
|
|
|
|
|
2013-02-16 22:44:03 +01:00
|
|
|
static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
|
|
|
|
uint64_t value)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2013-02-16 22:44:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writeb", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writew", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writel", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
|
|
|
|
{
|
|
|
|
qtest_write(s, "writeq", addr, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
|
|
|
|
{
|
|
|
|
gchar **args;
|
2017-09-11 19:19:50 +02:00
|
|
|
int ret;
|
2013-02-16 22:44:03 +01:00
|
|
|
uint64_t value;
|
|
|
|
|
|
|
|
qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
|
2021-01-26 16:16:49 +01:00
|
|
|
args = qtest_rsp_args(s, 2);
|
2017-09-11 19:19:50 +02:00
|
|
|
ret = qemu_strtou64(args[1], NULL, 0, &value);
|
|
|
|
g_assert(!ret);
|
2013-02-16 22:44:03 +01:00
|
|
|
g_strfreev(args);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t qtest_readb(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readb", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t qtest_readw(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readw", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t qtest_readl(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readl", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t qtest_readq(QTestState *s, uint64_t addr)
|
|
|
|
{
|
|
|
|
return qtest_read(s, "readq", addr);
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
static int hex2nib(char ch)
|
|
|
|
{
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
|
|
|
return ch - '0';
|
|
|
|
} else if (ch >= 'a' && ch <= 'f') {
|
|
|
|
return 10 + (ch - 'a');
|
|
|
|
} else if (ch >= 'A' && ch <= 'F') {
|
|
|
|
return 10 + (ch - 'a');
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
|
|
|
|
{
|
|
|
|
uint8_t *ptr = data;
|
|
|
|
gchar **args;
|
|
|
|
size_t i;
|
|
|
|
|
2017-01-11 09:49:32 +01:00
|
|
|
if (!size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-23 21:40:08 +02:00
|
|
|
qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
|
2021-01-26 16:16:49 +01:00
|
|
|
args = qtest_rsp_args(s, 2);
|
2012-03-28 15:42:05 +02:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
|
|
|
|
ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(args);
|
|
|
|
}
|
|
|
|
|
2016-09-13 14:52:45 +02:00
|
|
|
uint64_t qtest_rtas_call(QTestState *s, const char *name,
|
|
|
|
uint32_t nargs, uint64_t args,
|
|
|
|
uint32_t nret, uint64_t ret)
|
|
|
|
{
|
|
|
|
qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
|
|
|
|
name, nargs, args, nret, ret);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2016-09-13 14:52:45 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:20:33 +01:00
|
|
|
void qtest_add_func(const char *str, void (*fn)(void))
|
2012-03-28 15:42:05 +02:00
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
|
|
|
g_test_add_func(path, fn);
|
2014-08-04 23:11:22 +02:00
|
|
|
g_free(path);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
|
|
|
|
2016-07-19 13:42:01 +02:00
|
|
|
void qtest_add_data_func_full(const char *str, void *data,
|
|
|
|
void (*fn)(const void *),
|
|
|
|
GDestroyNotify data_free_func)
|
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
|
|
|
g_test_add_data_func_full(path, data, fn, data_free_func);
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2015-12-02 21:20:33 +01:00
|
|
|
void qtest_add_data_func(const char *str, const void *data,
|
|
|
|
void (*fn)(const void *))
|
2015-03-24 19:33:34 +01:00
|
|
|
{
|
|
|
|
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
|
|
|
g_test_add_data_func(path, data, fn);
|
|
|
|
g_free(path);
|
|
|
|
}
|
|
|
|
|
2015-05-22 20:13:44 +02:00
|
|
|
void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
gchar *bdata;
|
|
|
|
|
|
|
|
bdata = g_base64_encode(data, size);
|
|
|
|
qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
|
2020-02-20 05:11:03 +01:00
|
|
|
s->ops.send(s, bdata);
|
|
|
|
s->ops.send(s, "\n");
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2015-05-22 20:13:44 +02:00
|
|
|
g_free(bdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
|
|
|
|
{
|
|
|
|
gchar **args;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
|
2021-01-26 16:16:49 +01:00
|
|
|
args = qtest_rsp_args(s, 2);
|
2015-05-22 20:13:44 +02:00
|
|
|
|
|
|
|
g_base64_decode_inplace(args[1], &len);
|
|
|
|
if (size != len) {
|
|
|
|
fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
|
|
|
|
size, len);
|
|
|
|
len = MIN(len, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(data, args[1], len);
|
|
|
|
g_strfreev(args);
|
|
|
|
}
|
|
|
|
|
2012-03-28 15:42:05 +02:00
|
|
|
void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
|
|
|
|
{
|
|
|
|
const uint8_t *ptr = data;
|
|
|
|
size_t i;
|
2017-01-11 09:49:32 +01:00
|
|
|
char *enc;
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
enc = g_malloc(2 * size + 1);
|
2012-03-28 15:42:05 +02:00
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
2015-05-22 20:13:44 +02:00
|
|
|
sprintf(&enc[i * 2], "%02x", ptr[i]);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
2015-05-22 20:13:44 +02:00
|
|
|
|
|
|
|
qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2015-05-22 20:13:44 +02:00
|
|
|
g_free(enc);
|
2012-03-28 15:42:05 +02:00
|
|
|
}
|
2014-03-13 12:24:15 +01:00
|
|
|
|
2014-08-04 23:11:20 +02:00
|
|
|
void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
|
|
|
|
{
|
2015-05-22 20:13:44 +02:00
|
|
|
qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
|
2021-01-26 16:16:49 +01:00
|
|
|
qtest_rsp(s);
|
2014-03-13 12:24:15 +01:00
|
|
|
}
|
|
|
|
|
2023-07-12 21:07:41 +02:00
|
|
|
QDict *qtest_vqmp_assert_failure_ref(QTestState *qts,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
QDict *ret;
|
|
|
|
|
|
|
|
response = qtest_vqmp(qts, fmt, args);
|
|
|
|
|
|
|
|
g_assert(response);
|
|
|
|
if (!qdict_haskey(response, "error")) {
|
|
|
|
g_autoptr(GString) s = qobject_to_json_pretty(QOBJECT(response), true);
|
|
|
|
g_test_message("%s", s->str);
|
|
|
|
}
|
|
|
|
g_assert(qdict_haskey(response, "error"));
|
|
|
|
g_assert(!qdict_haskey(response, "return"));
|
|
|
|
ret = qdict_get_qdict(response, "error");
|
|
|
|
qobject_ref(ret);
|
|
|
|
qobject_unref(response);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-06-01 18:13:38 +02:00
|
|
|
QDict *qtest_vqmp_assert_success_ref(QTestState *qts,
|
|
|
|
const char *fmt, va_list args)
|
2019-05-08 11:58:45 +02:00
|
|
|
{
|
|
|
|
QDict *response;
|
2023-06-01 18:13:38 +02:00
|
|
|
QDict *ret;
|
2019-05-08 11:58:45 +02:00
|
|
|
|
2023-06-01 18:13:38 +02:00
|
|
|
response = qtest_vqmp(qts, fmt, args);
|
|
|
|
|
|
|
|
g_assert(response);
|
|
|
|
if (!qdict_haskey(response, "return")) {
|
|
|
|
g_autoptr(GString) s = qobject_to_json_pretty(QOBJECT(response), true);
|
|
|
|
g_test_message("%s", s->str);
|
|
|
|
}
|
|
|
|
g_assert(qdict_haskey(response, "return"));
|
|
|
|
ret = qdict_get_qdict(response, "return");
|
|
|
|
qobject_ref(ret);
|
|
|
|
qobject_unref(response);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_vqmp_assert_success(QTestState *qts,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
|
|
|
|
response = qtest_vqmp_assert_success_ref(qts, fmt, args);
|
|
|
|
|
|
|
|
qobject_unref(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
QDict *qtest_vqmp_fds_assert_success_ref(QTestState *qts, int *fds, size_t nfds,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
QDict *ret;
|
|
|
|
|
|
|
|
response = qtest_vqmp_fds(qts, fds, nfds, fmt, args);
|
2019-05-08 11:58:45 +02:00
|
|
|
|
|
|
|
g_assert(response);
|
|
|
|
if (!qdict_haskey(response, "return")) {
|
2023-06-01 18:13:38 +02:00
|
|
|
g_autoptr(GString) s = qobject_to_json_pretty(QOBJECT(response), true);
|
2020-12-11 18:11:37 +01:00
|
|
|
g_test_message("%s", s->str);
|
2019-05-08 11:58:45 +02:00
|
|
|
}
|
|
|
|
g_assert(qdict_haskey(response, "return"));
|
2023-06-01 18:13:38 +02:00
|
|
|
ret = qdict_get_qdict(response, "return");
|
|
|
|
qobject_ref(ret);
|
2019-05-08 11:58:45 +02:00
|
|
|
qobject_unref(response);
|
2023-06-01 18:13:38 +02:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_vqmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds,
|
|
|
|
const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
response = qtest_vqmp_fds_assert_success_ref(qts, fds, nfds, fmt, args);
|
|
|
|
qobject_unref(response);
|
|
|
|
}
|
|
|
|
#endif /* !_WIN32 */
|
|
|
|
|
2023-07-12 21:07:41 +02:00
|
|
|
QDict *qtest_qmp_assert_failure_ref(QTestState *qts, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
response = qtest_vqmp_assert_failure_ref(qts, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2023-06-01 18:13:38 +02:00
|
|
|
QDict *qtest_qmp_assert_success_ref(QTestState *qts, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
response = qtest_vqmp_assert_success_ref(qts, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
qtest_vqmp_assert_success(qts, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
QDict *qtest_qmp_fds_assert_success_ref(QTestState *qts, int *fds, size_t nfds,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
response = qtest_vqmp_fds_assert_success_ref(qts, fds, nfds, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_qmp_fds_assert_success(QTestState *qts, int *fds, size_t nfds,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
qtest_vqmp_fds_assert_success(qts, fds, nfds, fmt, ap);
|
|
|
|
va_end(ap);
|
2019-05-08 11:58:45 +02:00
|
|
|
}
|
2023-06-01 18:13:38 +02:00
|
|
|
#endif /* !_WIN32 */
|
2019-05-08 11:58:45 +02:00
|
|
|
|
2016-10-07 12:14:27 +02:00
|
|
|
bool qtest_big_endian(QTestState *s)
|
2014-09-01 12:07:55 +02:00
|
|
|
{
|
2016-10-07 12:14:27 +02:00
|
|
|
return s->big_endian;
|
2014-09-01 12:07:55 +02:00
|
|
|
}
|
2017-03-30 09:50:06 +02:00
|
|
|
|
2018-08-16 13:35:55 +02:00
|
|
|
static bool qtest_check_machine_version(const char *mname, const char *basename,
|
|
|
|
int major, int minor)
|
|
|
|
{
|
|
|
|
char *newname;
|
|
|
|
bool is_equal;
|
|
|
|
|
|
|
|
newname = g_strdup_printf("%s-%i.%i", basename, major, minor);
|
|
|
|
is_equal = g_str_equal(mname, newname);
|
|
|
|
g_free(newname);
|
|
|
|
|
|
|
|
return is_equal;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool qtest_is_old_versioned_machine(const char *mname)
|
|
|
|
{
|
|
|
|
const char *dash = strrchr(mname, '-');
|
|
|
|
const char *dot = strrchr(mname, '.');
|
|
|
|
const char *chr;
|
|
|
|
char *bname;
|
|
|
|
const int major = QEMU_VERSION_MAJOR;
|
|
|
|
const int minor = QEMU_VERSION_MINOR;
|
|
|
|
bool res = false;
|
|
|
|
|
|
|
|
if (dash && dot && dot > dash) {
|
|
|
|
for (chr = dash + 1; *chr; chr++) {
|
|
|
|
if (!qemu_isdigit(*chr) && *chr != '.') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Now check if it is one of the latest versions. Check major + 1
|
|
|
|
* and minor + 1 versions as well, since they might already exist
|
|
|
|
* in the development branch.
|
|
|
|
*/
|
|
|
|
bname = g_strdup(mname);
|
|
|
|
bname[dash - mname] = 0;
|
|
|
|
res = !qtest_check_machine_version(mname, bname, major + 1, 0) &&
|
|
|
|
!qtest_check_machine_version(mname, bname, major, minor + 1) &&
|
|
|
|
!qtest_check_machine_version(mname, bname, major, minor);
|
|
|
|
g_free(bname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:43:46 +01:00
|
|
|
struct MachInfo {
|
|
|
|
char *name;
|
|
|
|
char *alias;
|
|
|
|
};
|
|
|
|
|
2023-10-18 21:27:32 +02:00
|
|
|
static void qtest_free_machine_list(struct MachInfo *machines)
|
|
|
|
{
|
|
|
|
if (machines) {
|
|
|
|
for (int i = 0; machines[i].name != NULL; i++) {
|
|
|
|
g_free(machines[i].name);
|
|
|
|
g_free(machines[i].alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(machines);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:43:46 +01:00
|
|
|
/*
|
|
|
|
* Returns an array with pointers to the available machine names.
|
|
|
|
* The terminating entry has the name set to NULL.
|
|
|
|
*/
|
2023-10-18 21:27:32 +02:00
|
|
|
static struct MachInfo *qtest_get_machines(const char *var)
|
2017-03-30 09:50:06 +02:00
|
|
|
{
|
2021-12-01 11:43:46 +01:00
|
|
|
static struct MachInfo *machines;
|
2023-10-18 21:27:32 +02:00
|
|
|
static char *qemu_var;
|
2017-03-30 09:50:06 +02:00
|
|
|
QDict *response, *minfo;
|
|
|
|
QList *list;
|
|
|
|
const QListEntry *p;
|
|
|
|
QObject *qobj;
|
|
|
|
QString *qstr;
|
2019-07-22 16:34:42 +02:00
|
|
|
QTestState *qts;
|
2021-12-01 11:43:46 +01:00
|
|
|
int idx;
|
|
|
|
|
2023-10-18 21:27:32 +02:00
|
|
|
if (g_strcmp0(qemu_var, var)) {
|
|
|
|
qemu_var = g_strdup(var);
|
|
|
|
|
|
|
|
/* new qemu, clear the cache */
|
|
|
|
qtest_free_machine_list(machines);
|
|
|
|
machines = NULL;
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:43:46 +01:00
|
|
|
if (machines) {
|
|
|
|
return machines;
|
|
|
|
}
|
2017-03-30 09:50:06 +02:00
|
|
|
|
2023-10-18 21:27:32 +02:00
|
|
|
qts = qtest_init_with_env(qemu_var, "-machine none");
|
2019-07-22 16:34:42 +02:00
|
|
|
response = qtest_qmp(qts, "{ 'execute': 'query-machines' }");
|
2017-03-30 09:50:06 +02:00
|
|
|
g_assert(response);
|
|
|
|
list = qdict_get_qlist(response, "return");
|
|
|
|
g_assert(list);
|
|
|
|
|
2021-12-01 11:43:46 +01:00
|
|
|
machines = g_new(struct MachInfo, qlist_size(list) + 1);
|
|
|
|
|
|
|
|
for (p = qlist_first(list), idx = 0; p; p = qlist_next(p), idx++) {
|
2018-02-24 16:40:29 +01:00
|
|
|
minfo = qobject_to(QDict, qlist_entry_obj(p));
|
2017-03-30 09:50:06 +02:00
|
|
|
g_assert(minfo);
|
2021-12-01 11:43:46 +01:00
|
|
|
|
2017-03-30 09:50:06 +02:00
|
|
|
qobj = qdict_get(minfo, "name");
|
|
|
|
g_assert(qobj);
|
2018-02-24 16:40:29 +01:00
|
|
|
qstr = qobject_to(QString, qobj);
|
2017-03-30 09:50:06 +02:00
|
|
|
g_assert(qstr);
|
2021-12-01 11:43:46 +01:00
|
|
|
machines[idx].name = g_strdup(qstring_get_str(qstr));
|
|
|
|
|
|
|
|
qobj = qdict_get(minfo, "alias");
|
|
|
|
if (qobj) { /* The alias is optional */
|
|
|
|
qstr = qobject_to(QString, qobj);
|
|
|
|
g_assert(qstr);
|
|
|
|
machines[idx].alias = g_strdup(qstring_get_str(qstr));
|
|
|
|
} else {
|
|
|
|
machines[idx].alias = NULL;
|
2018-08-16 13:35:55 +02:00
|
|
|
}
|
2017-03-30 09:50:06 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:34:42 +02:00
|
|
|
qtest_quit(qts);
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(response);
|
2021-12-01 11:43:46 +01:00
|
|
|
|
|
|
|
memset(&machines[idx], 0, sizeof(struct MachInfo)); /* Terminating entry */
|
|
|
|
return machines;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_cb_for_every_machine(void (*cb)(const char *machine),
|
|
|
|
bool skip_old_versioned)
|
|
|
|
{
|
|
|
|
struct MachInfo *machines;
|
|
|
|
int i;
|
|
|
|
|
2023-10-18 21:27:32 +02:00
|
|
|
machines = qtest_get_machines(NULL);
|
2021-12-01 11:43:46 +01:00
|
|
|
|
|
|
|
for (i = 0; machines[i].name != NULL; i++) {
|
|
|
|
/* Ignore machines that cannot be used for qtests */
|
|
|
|
if (!strncmp("xenfv", machines[i].name, 5) ||
|
2023-06-15 02:03:39 +02:00
|
|
|
g_str_equal("xenpv", machines[i].name) ||
|
|
|
|
g_str_equal("xenpvh", machines[i].name)) {
|
2021-12-01 11:43:46 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!skip_old_versioned ||
|
|
|
|
!qtest_is_old_versioned_machine(machines[i].name)) {
|
|
|
|
cb(machines[i].name);
|
|
|
|
}
|
|
|
|
}
|
2017-03-30 09:50:06 +02:00
|
|
|
}
|
2017-08-15 08:58:54 +02:00
|
|
|
|
2023-10-18 21:27:33 +02:00
|
|
|
bool qtest_has_machine_with_env(const char *var, const char *machine)
|
2021-12-01 11:43:47 +01:00
|
|
|
{
|
|
|
|
struct MachInfo *machines;
|
|
|
|
int i;
|
|
|
|
|
2023-10-18 21:27:33 +02:00
|
|
|
machines = qtest_get_machines(var);
|
2021-12-01 11:43:47 +01:00
|
|
|
|
|
|
|
for (i = 0; machines[i].name != NULL; i++) {
|
|
|
|
if (g_str_equal(machine, machines[i].name) ||
|
|
|
|
(machines[i].alias && g_str_equal(machine, machines[i].alias))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-10-18 21:27:33 +02:00
|
|
|
bool qtest_has_machine(const char *machine)
|
|
|
|
{
|
|
|
|
return qtest_has_machine_with_env(NULL, machine);
|
|
|
|
}
|
|
|
|
|
2021-12-20 09:10:51 +01:00
|
|
|
bool qtest_has_device(const char *device)
|
|
|
|
{
|
|
|
|
static QList *list;
|
|
|
|
const QListEntry *p;
|
|
|
|
QObject *qobj;
|
|
|
|
QString *qstr;
|
|
|
|
QDict *devinfo;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!list) {
|
|
|
|
QDict *resp;
|
|
|
|
QDict *args;
|
|
|
|
QTestState *qts = qtest_init("-machine none");
|
|
|
|
|
|
|
|
args = qdict_new();
|
|
|
|
qdict_put_bool(args, "abstract", false);
|
|
|
|
qdict_put_str(args, "implements", "device");
|
|
|
|
|
|
|
|
resp = qtest_qmp(qts, "{'execute': 'qom-list-types', 'arguments': %p }",
|
|
|
|
args);
|
|
|
|
g_assert(qdict_haskey(resp, "return"));
|
|
|
|
list = qdict_get_qlist(resp, "return");
|
|
|
|
qobject_ref(list);
|
|
|
|
qobject_unref(resp);
|
|
|
|
|
|
|
|
qtest_quit(qts);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (p = qlist_first(list), idx = 0; p; p = qlist_next(p), idx++) {
|
|
|
|
devinfo = qobject_to(QDict, qlist_entry_obj(p));
|
|
|
|
g_assert(devinfo);
|
|
|
|
|
|
|
|
qobj = qdict_get(devinfo, "name");
|
|
|
|
g_assert(qobj);
|
|
|
|
qstr = qobject_to(QString, qobj);
|
|
|
|
g_assert(qstr);
|
|
|
|
if (g_str_equal(qstring_get_str(qstr), device)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-15 08:58:54 +02:00
|
|
|
/*
|
2019-08-30 13:07:22 +02:00
|
|
|
* Generic hot-plugging test via the device_add QMP commands.
|
2017-08-15 08:58:54 +02:00
|
|
|
*/
|
2019-08-30 13:07:22 +02:00
|
|
|
void qtest_qmp_device_add_qdict(QTestState *qts, const char *drv,
|
|
|
|
const QDict *arguments)
|
|
|
|
{
|
|
|
|
QDict *resp;
|
|
|
|
QDict *args = arguments ? qdict_clone_shallow(arguments) : qdict_new();
|
|
|
|
|
|
|
|
g_assert(!qdict_haskey(args, "driver"));
|
|
|
|
qdict_put_str(args, "driver", drv);
|
|
|
|
resp = qtest_qmp(qts, "{'execute': 'device_add', 'arguments': %p}", args);
|
|
|
|
g_assert(resp);
|
|
|
|
g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */
|
2023-01-12 15:02:33 +01:00
|
|
|
if (qdict_haskey(resp, "error")) {
|
|
|
|
fprintf(stderr, "error: %s\n",
|
|
|
|
qdict_get_str(qdict_get_qdict(resp, "error"), "desc"));
|
|
|
|
}
|
2019-08-30 13:07:22 +02:00
|
|
|
g_assert(!qdict_haskey(resp, "error"));
|
|
|
|
qobject_unref(resp);
|
|
|
|
}
|
|
|
|
|
2019-07-22 17:10:55 +02:00
|
|
|
void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
|
2018-08-06 08:53:35 +02:00
|
|
|
const char *fmt, ...)
|
2017-08-15 08:58:54 +02:00
|
|
|
{
|
2019-08-30 13:07:22 +02:00
|
|
|
QDict *args;
|
2018-08-06 08:53:35 +02:00
|
|
|
va_list ap;
|
2017-08-15 08:58:54 +02:00
|
|
|
|
2018-08-06 08:53:35 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
args = qdict_from_vjsonf_nofail(fmt, ap);
|
|
|
|
va_end(ap);
|
2017-08-15 08:58:54 +02:00
|
|
|
|
2019-08-30 13:07:22 +02:00
|
|
|
g_assert(!qdict_haskey(args, "id"));
|
2018-08-06 08:53:35 +02:00
|
|
|
qdict_put_str(args, "id", id);
|
2017-08-15 08:58:54 +02:00
|
|
|
|
2019-08-30 13:07:22 +02:00
|
|
|
qtest_qmp_device_add_qdict(qts, driver, args);
|
2019-11-07 20:27:30 +01:00
|
|
|
qobject_unref(args);
|
2017-08-15 08:58:54 +02:00
|
|
|
}
|
|
|
|
|
2021-10-06 23:07:30 +02:00
|
|
|
void qtest_qmp_add_client(QTestState *qts, const char *protocol, int fd)
|
|
|
|
{
|
|
|
|
QDict *resp;
|
|
|
|
|
2023-03-06 13:27:49 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
WSAPROTOCOL_INFOW info;
|
|
|
|
g_autofree char *info64 = NULL;
|
|
|
|
SOCKET s;
|
|
|
|
|
|
|
|
assert(fd_is_socket(fd));
|
|
|
|
s = _get_osfhandle(fd);
|
|
|
|
if (WSADuplicateSocketW(s, GetProcessId((HANDLE)qts->qemu_pid), &info) == SOCKET_ERROR) {
|
|
|
|
g_autofree char *emsg = g_win32_error_message(WSAGetLastError());
|
|
|
|
g_error("WSADuplicateSocketW failed: %s", emsg);
|
|
|
|
}
|
|
|
|
info64 = g_base64_encode((guchar *)&info, sizeof(info));
|
|
|
|
resp = qtest_qmp(qts, "{'execute': 'get-win32-socket',"
|
|
|
|
"'arguments': {'fdname': 'fdname', 'info': %s}}", info64);
|
|
|
|
#else
|
2021-10-06 23:07:30 +02:00
|
|
|
resp = qtest_qmp_fds(qts, &fd, 1, "{'execute': 'getfd',"
|
|
|
|
"'arguments': {'fdname': 'fdname'}}");
|
2023-03-06 13:27:49 +01:00
|
|
|
#endif
|
2021-10-06 23:07:30 +02:00
|
|
|
g_assert(resp);
|
|
|
|
g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */
|
|
|
|
g_assert(!qdict_haskey(resp, "error"));
|
|
|
|
qobject_unref(resp);
|
|
|
|
|
|
|
|
resp = qtest_qmp(
|
|
|
|
qts, "{'execute': 'add_client',"
|
|
|
|
"'arguments': {'protocol': %s, 'fdname': 'fdname'}}", protocol);
|
|
|
|
g_assert(resp);
|
|
|
|
g_assert(!qdict_haskey(resp, "event")); /* We don't expect any events */
|
|
|
|
g_assert(!qdict_haskey(resp, "error"));
|
|
|
|
qobject_unref(resp);
|
|
|
|
}
|
2018-08-06 08:53:37 +02:00
|
|
|
|
2017-08-15 08:58:54 +02:00
|
|
|
/*
|
|
|
|
* Generic hot-unplugging test via the device_del QMP command.
|
|
|
|
* Device deletion will get one response and one event. For example:
|
|
|
|
*
|
|
|
|
* {'execute': 'device_del','arguments': { 'id': 'scsi-hd'}}
|
|
|
|
*
|
|
|
|
* will get this one:
|
|
|
|
*
|
|
|
|
* {"timestamp": {"seconds": 1505289667, "microseconds": 569862},
|
|
|
|
* "event": "DEVICE_DELETED", "data": {"device": "scsi-hd",
|
|
|
|
* "path": "/machine/peripheral/scsi-hd"}}
|
|
|
|
*
|
|
|
|
* and this one:
|
|
|
|
*
|
|
|
|
* {"return": {}}
|
|
|
|
*/
|
2022-09-30 00:35:39 +02:00
|
|
|
void qtest_qmp_device_del_send(QTestState *qts, const char *id)
|
2017-08-15 08:58:54 +02:00
|
|
|
{
|
2022-09-30 00:35:39 +02:00
|
|
|
QDict *rsp = qtest_qmp(qts, "{'execute': 'device_del', "
|
|
|
|
"'arguments': {'id': %s}}", id);
|
|
|
|
g_assert(rsp);
|
2020-10-06 14:59:32 +02:00
|
|
|
g_assert(qdict_haskey(rsp, "return"));
|
2022-09-30 00:35:39 +02:00
|
|
|
g_assert(!qdict_haskey(rsp, "error"));
|
2018-08-06 08:53:37 +02:00
|
|
|
qobject_unref(rsp);
|
2022-09-30 00:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_qmp_device_del(QTestState *qts, const char *id)
|
|
|
|
{
|
|
|
|
qtest_qmp_device_del_send(qts, id);
|
2020-10-06 14:59:32 +02:00
|
|
|
qtest_qmp_eventwait(qts, "DEVICE_DELETED");
|
2017-08-15 08:58:54 +02:00
|
|
|
}
|
2018-05-17 13:30:07 +02:00
|
|
|
|
2020-02-20 05:11:02 +01:00
|
|
|
static void qtest_client_set_tx_handler(QTestState *s,
|
|
|
|
QTestSendFn send)
|
|
|
|
{
|
|
|
|
s->ops.send = send;
|
|
|
|
}
|
|
|
|
static void qtest_client_set_rx_handler(QTestState *s, QTestRecvFn recv)
|
|
|
|
{
|
|
|
|
s->ops.recv_line = recv;
|
|
|
|
}
|
2020-02-20 05:11:03 +01:00
|
|
|
/* A type-safe wrapper for s->send() */
|
|
|
|
static void send_wrapper(QTestState *s, const char *buf)
|
|
|
|
{
|
|
|
|
s->ops.external_send(s, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GString *qtest_client_inproc_recv_line(QTestState *s)
|
|
|
|
{
|
|
|
|
GString *line;
|
|
|
|
size_t offset;
|
|
|
|
char *eol;
|
|
|
|
|
|
|
|
eol = strchr(s->rx->str, '\n');
|
|
|
|
offset = eol - s->rx->str;
|
|
|
|
line = g_string_new_len(s->rx->str, offset);
|
|
|
|
g_string_erase(s->rx, 0, offset + 1);
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTestState *qtest_inproc_init(QTestState **s, bool log, const char* arch,
|
|
|
|
void (*send)(void*, const char*))
|
|
|
|
{
|
|
|
|
QTestState *qts;
|
|
|
|
qts = g_new0(QTestState, 1);
|
2020-10-06 14:38:53 +02:00
|
|
|
qts->pending_events = NULL;
|
2020-02-20 05:11:03 +01:00
|
|
|
*s = qts; /* Expose qts early on, since the query endianness relies on it */
|
|
|
|
qts->wstatus = 0;
|
|
|
|
for (int i = 0; i < MAX_IRQ; i++) {
|
|
|
|
qts->irq_level[i] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
qtest_client_set_rx_handler(qts, qtest_client_inproc_recv_line);
|
|
|
|
|
2023-07-14 13:33:27 +02:00
|
|
|
/* send() may not have a matching prototype, so use a type-safe wrapper */
|
2020-02-20 05:11:03 +01:00
|
|
|
qts->ops.external_send = send;
|
|
|
|
qtest_client_set_tx_handler(qts, send_wrapper);
|
|
|
|
|
|
|
|
qts->big_endian = qtest_query_target_endianness(qts);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set a dummy path for QTEST_QEMU_BINARY. Doesn't need to exist, but this
|
|
|
|
* way, qtest_get_arch works for inproc qtest.
|
|
|
|
*/
|
|
|
|
gchar *bin_path = g_strconcat("/qemu-system-", arch, NULL);
|
2022-08-24 11:39:39 +02:00
|
|
|
g_setenv("QTEST_QEMU_BINARY", bin_path, 0);
|
2020-02-20 05:11:03 +01:00
|
|
|
g_free(bin_path);
|
|
|
|
|
|
|
|
return qts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qtest_client_inproc_recv(void *opaque, const char *str)
|
|
|
|
{
|
|
|
|
QTestState *qts = *(QTestState **)opaque;
|
|
|
|
|
|
|
|
if (!qts->rx) {
|
|
|
|
qts->rx = g_string_new(NULL);
|
|
|
|
}
|
|
|
|
g_string_append(qts->rx, str);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-25 10:31:33 +02:00
|
|
|
|
|
|
|
void qtest_qom_set_bool(QTestState *s, const char *path, const char *property,
|
|
|
|
bool value)
|
|
|
|
{
|
|
|
|
QDict *r;
|
|
|
|
|
|
|
|
r = qtest_qmp(s, "{ 'execute': 'qom-set', 'arguments': "
|
|
|
|
"{ 'path': %s, 'property': %s, 'value': %i } }",
|
|
|
|
path, property, value);
|
|
|
|
qobject_unref(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool qtest_qom_get_bool(QTestState *s, const char *path, const char *property)
|
|
|
|
{
|
|
|
|
QDict *r;
|
|
|
|
bool b;
|
|
|
|
|
|
|
|
r = qtest_qmp(s, "{ 'execute': 'qom-get', 'arguments': "
|
|
|
|
"{ 'path': %s, 'property': %s } }", path, property);
|
|
|
|
b = qdict_get_bool(r, "return");
|
|
|
|
qobject_unref(r);
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
2023-07-04 09:16:54 +02:00
|
|
|
|
|
|
|
bool have_qemu_img(void)
|
|
|
|
{
|
|
|
|
char *rpath;
|
|
|
|
const char *path = getenv("QTEST_QEMU_IMG");
|
|
|
|
if (!path) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
rpath = realpath(path, NULL);
|
|
|
|
if (!rpath) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
free(rpath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mkimg(const char *file, const char *fmt, unsigned size_mb)
|
|
|
|
{
|
|
|
|
gchar *cli;
|
|
|
|
bool ret;
|
|
|
|
int rc;
|
|
|
|
GError *err = NULL;
|
|
|
|
char *qemu_img_path;
|
|
|
|
gchar *out, *out2;
|
|
|
|
char *qemu_img_abs_path;
|
|
|
|
|
|
|
|
qemu_img_path = getenv("QTEST_QEMU_IMG");
|
|
|
|
if (!qemu_img_path) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qemu_img_abs_path = realpath(qemu_img_path, NULL);
|
|
|
|
if (!qemu_img_abs_path) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cli = g_strdup_printf("%s create -f %s %s %uM", qemu_img_abs_path,
|
|
|
|
fmt, file, size_mb);
|
|
|
|
ret = g_spawn_command_line_sync(cli, &out, &out2, &rc, &err);
|
|
|
|
if (err || !g_spawn_check_exit_status(rc, &err)) {
|
|
|
|
fprintf(stderr, "%s\n", err->message);
|
|
|
|
g_error_free(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(out);
|
|
|
|
g_free(out2);
|
|
|
|
g_free(cli);
|
|
|
|
free(qemu_img_abs_path);
|
|
|
|
|
|
|
|
return ret && !err;
|
|
|
|
}
|