]> git.proxmox.com Git - mirror_qemu.git/blame - tests/libqtest.c
util/cutils: Rename qemu_strtosz() to qemu_strtosz_MiB()
[mirror_qemu.git] / tests / libqtest.c
CommitLineData
49ee3590
AL
1/*
2 * QTest
3 *
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
872536bf 6 * Copyright SUSE LINUX Products GmbH 2013
49ee3590
AL
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
872536bf 11 * Andreas Färber <afaerber@suse.de>
49ee3590
AL
12 *
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.
15 *
16 */
681c28a3 17#include "qemu/osdep.h"
49ee3590
AL
18#include "libqtest.h"
19
49ee3590
AL
20#include <sys/socket.h>
21#include <sys/wait.h>
22#include <sys/un.h>
49ee3590 23
0c460dac 24#include "qapi/qmp/json-parser.h"
563890c7
PB
25#include "qapi/qmp/json-streamer.h"
26#include "qapi/qmp/qjson.h"
49ee3590
AL
27
28#define MAX_IRQ 256
f5d45791 29#define SOCKET_TIMEOUT 50
49ee3590
AL
30
31QTestState *global_qtest;
32
33struct QTestState
34{
35 int fd;
a3ca163c 36 int qmp_fd;
49ee3590
AL
37 bool irq_level[MAX_IRQ];
38 GString *rx;
cef60c92 39 pid_t qemu_pid; /* our child QEMU process */
54ce6f22 40 bool big_endian;
49ee3590
AL
41};
42
063c23d9 43static GHookList abrt_hooks;
d7668251
SH
44static GList *qtest_instances;
45static struct sigaction sigact_old;
46
49ee3590
AL
47#define g_assert_no_errno(ret) do { \
48 g_assert_cmpint(ret, !=, -1); \
49} while (0)
50
54ce6f22
LV
51static int qtest_query_target_endianness(QTestState *s);
52
a3ca163c
KW
53static int init_socket(const char *socket_path)
54{
55 struct sockaddr_un addr;
56 int sock;
57 int ret;
58
59 sock = socket(PF_UNIX, SOCK_STREAM, 0);
60 g_assert_no_errno(sock);
61
62 addr.sun_family = AF_UNIX;
63 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
64 qemu_set_cloexec(sock);
65
66 do {
67 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
68 } while (ret == -1 && errno == EINTR);
69 g_assert_no_errno(ret);
a7ded163
AF
70 ret = listen(sock, 1);
71 g_assert_no_errno(ret);
a3ca163c
KW
72
73 return sock;
74}
75
76static int socket_accept(int sock)
77{
78 struct sockaddr_un addr;
79 socklen_t addrlen;
80 int ret;
f8762027
MA
81 struct timeval timeout = { .tv_sec = SOCKET_TIMEOUT,
82 .tv_usec = 0 };
83
84 setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout,
85 sizeof(timeout));
a3ca163c
KW
86
87 do {
535b4563 88 addrlen = sizeof(addr);
a3ca163c
KW
89 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
90 } while (ret == -1 && errno == EINTR);
a7d915f3
AF
91 if (ret == -1) {
92 fprintf(stderr, "%s failed: %s\n", __func__, strerror(errno));
93 }
a3ca163c
KW
94 close(sock);
95
96 return ret;
97}
98
b15d422a
SH
99static void kill_qemu(QTestState *s)
100{
101 if (s->qemu_pid != -1) {
102 kill(s->qemu_pid, SIGTERM);
103 waitpid(s->qemu_pid, NULL, 0);
104 }
105}
106
041088c7
MA
107static void kill_qemu_hook_func(void *s)
108{
109 kill_qemu(s);
110}
111
b15d422a
SH
112static void sigabrt_handler(int signo)
113{
063c23d9 114 g_hook_list_invoke(&abrt_hooks, FALSE);
d7668251
SH
115}
116
117static void setup_sigabrt_handler(void)
118{
119 struct sigaction sigact;
120
121 /* Catch SIGABRT to clean up on g_assert() failure */
122 sigact = (struct sigaction){
123 .sa_handler = sigabrt_handler,
124 .sa_flags = SA_RESETHAND,
125 };
126 sigemptyset(&sigact.sa_mask);
127 sigaction(SIGABRT, &sigact, &sigact_old);
128}
129
130static void cleanup_sigabrt_handler(void)
131{
132 sigaction(SIGABRT, &sigact_old, NULL);
b15d422a
SH
133}
134
041088c7 135void qtest_add_abrt_handler(GHookFunc fn, const void *data)
063c23d9
MAL
136{
137 GHook *hook;
138
139 /* Only install SIGABRT handler once */
140 if (!abrt_hooks.is_setup) {
141 g_hook_list_init(&abrt_hooks, sizeof(GHook));
142 setup_sigabrt_handler();
143 }
144
145 hook = g_hook_alloc(&abrt_hooks);
146 hook->func = fn;
147 hook->data = (void *)data;
148
149 g_hook_prepend(&abrt_hooks, hook);
150}
151
49ee3590
AL
152QTestState *qtest_init(const char *extra_args)
153{
154 QTestState *s;
8a8fd637 155 int sock, qmpsock, i;
56db2e58
SH
156 gchar *socket_path;
157 gchar *qmp_socket_path;
49ee3590
AL
158 gchar *command;
159 const char *qemu_binary;
49ee3590
AL
160
161 qemu_binary = getenv("QTEST_QEMU_BINARY");
162 g_assert(qemu_binary != NULL);
163
96b8ca47 164 s = g_malloc(sizeof(*s));
49ee3590 165
56db2e58
SH
166 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
167 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
bb494a50 168
56db2e58
SH
169 sock = init_socket(socket_path);
170 qmpsock = init_socket(qmp_socket_path);
49ee3590 171
041088c7 172 qtest_add_abrt_handler(kill_qemu_hook_func, s);
b15d422a 173
cef60c92
SH
174 s->qemu_pid = fork();
175 if (s->qemu_pid == 0) {
6b029216 176 setenv("QEMU_AUDIO_DRV", "none", true);
cef60c92 177 command = g_strdup_printf("exec %s "
49ee3590 178 "-qtest unix:%s,nowait "
7c282cb4 179 "-qtest-log %s "
a3ca163c 180 "-qmp unix:%s,nowait "
49ee3590 181 "-machine accel=qtest "
2ad645d2 182 "-display none "
56db2e58 183 "%s", qemu_binary, socket_path,
7c282cb4 184 getenv("QTEST_LOG") ? "/dev/fd/2" : "/dev/null",
cef60c92 185 qmp_socket_path,
49ee3590 186 extra_args ?: "");
8a8fd637
AL
187 execlp("/bin/sh", "sh", "-c", command, NULL);
188 exit(1);
49ee3590
AL
189 }
190
a3ca163c 191 s->fd = socket_accept(sock);
f8762027
MA
192 if (s->fd >= 0) {
193 s->qmp_fd = socket_accept(qmpsock);
194 }
56db2e58
SH
195 unlink(socket_path);
196 unlink(qmp_socket_path);
197 g_free(socket_path);
198 g_free(qmp_socket_path);
49ee3590 199
f8762027
MA
200 g_assert(s->fd >= 0 && s->qmp_fd >= 0);
201
49ee3590 202 s->rx = g_string_new("");
49ee3590
AL
203 for (i = 0; i < MAX_IRQ; i++) {
204 s->irq_level[i] = false;
205 }
206
a3ca163c 207 /* Read the QMP greeting and then do the handshake */
0d1aa05e
SH
208 qtest_qmp_discard_response(s, "");
209 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
49ee3590 210
e0fea6b1 211 if (getenv("QTEST_STOP")) {
1ad3c6ab 212 kill(s->qemu_pid, SIGSTOP);
e0fea6b1
PB
213 }
214
54ce6f22
LV
215 /* ask endianness of the target */
216
217 s->big_endian = qtest_query_target_endianness(s);
218
49ee3590
AL
219 return s;
220}
221
222void qtest_quit(QTestState *s)
223{
063c23d9
MAL
224 qtest_instances = g_list_remove(qtest_instances, s);
225 g_hook_destroy_link(&abrt_hooks, g_hook_find_data(&abrt_hooks, TRUE, s));
226
d7668251 227 /* Uninstall SIGABRT handler on last instance */
063c23d9 228 if (!qtest_instances) {
d7668251
SH
229 cleanup_sigabrt_handler();
230 }
231
b15d422a 232 kill_qemu(s);
fdd26fca
MA
233 close(s->fd);
234 close(s->qmp_fd);
235 g_string_free(s->rx, true);
fdd26fca 236 g_free(s);
49ee3590
AL
237}
238
563890c7 239static void socket_send(int fd, const char *buf, size_t size)
49ee3590 240{
563890c7 241 size_t offset;
49ee3590
AL
242
243 offset = 0;
244 while (offset < size) {
245 ssize_t len;
246
563890c7 247 len = write(fd, buf + offset, size - offset);
49ee3590
AL
248 if (len == -1 && errno == EINTR) {
249 continue;
250 }
251
252 g_assert_no_errno(len);
253 g_assert_cmpint(len, >, 0);
254
255 offset += len;
256 }
257}
258
563890c7
PB
259static void socket_sendf(int fd, const char *fmt, va_list ap)
260{
261 gchar *str = g_strdup_vprintf(fmt, ap);
262 size_t size = strlen(str);
263
264 socket_send(fd, str, size);
265 g_free(str);
266}
267
a3ca163c
KW
268static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
269{
270 va_list ap;
271
272 va_start(ap, fmt);
273 socket_sendf(s->fd, fmt, ap);
274 va_end(ap);
275}
276
49ee3590
AL
277static GString *qtest_recv_line(QTestState *s)
278{
279 GString *line;
280 size_t offset;
281 char *eol;
282
283 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
284 ssize_t len;
285 char buffer[1024];
286
287 len = read(s->fd, buffer, sizeof(buffer));
288 if (len == -1 && errno == EINTR) {
289 continue;
290 }
291
292 if (len == -1 || len == 0) {
293 fprintf(stderr, "Broken pipe\n");
294 exit(1);
295 }
296
297 g_string_append_len(s->rx, buffer, len);
298 }
299
300 offset = eol - s->rx->str;
301 line = g_string_new_len(s->rx->str, offset);
302 g_string_erase(s->rx, 0, offset + 1);
303
304 return line;
305}
306
307static gchar **qtest_rsp(QTestState *s, int expected_args)
308{
309 GString *line;
310 gchar **words;
311 int i;
312
313redo:
314 line = qtest_recv_line(s);
315 words = g_strsplit(line->str, " ", 0);
316 g_string_free(line, TRUE);
317
318 if (strcmp(words[0], "IRQ") == 0) {
319 int irq;
320
321 g_assert(words[1] != NULL);
322 g_assert(words[2] != NULL);
323
324 irq = strtoul(words[2], NULL, 0);
325 g_assert_cmpint(irq, >=, 0);
326 g_assert_cmpint(irq, <, MAX_IRQ);
327
328 if (strcmp(words[1], "raise") == 0) {
329 s->irq_level[irq] = true;
330 } else {
331 s->irq_level[irq] = false;
332 }
333
334 g_strfreev(words);
335 goto redo;
336 }
337
338 g_assert(words[0] != NULL);
339 g_assert_cmpstr(words[0], ==, "OK");
340
341 if (expected_args) {
342 for (i = 0; i < expected_args; i++) {
343 g_assert(words[i] != NULL);
344 }
345 } else {
346 g_strfreev(words);
347 }
348
349 return words;
350}
351
54ce6f22
LV
352static int qtest_query_target_endianness(QTestState *s)
353{
354 gchar **args;
355 int big_endian;
356
357 qtest_sendf(s, "endianness\n");
358 args = qtest_rsp(s, 1);
359 g_assert(strcmp(args[1], "big") == 0 || strcmp(args[1], "little") == 0);
360 big_endian = strcmp(args[1], "big") == 0;
361 g_strfreev(args);
362
363 return big_endian;
364}
365
0c460dac
SH
366typedef struct {
367 JSONMessageParser parser;
368 QDict *response;
369} QMPResponseParser;
370
95385fe9 371static void qmp_response(JSONMessageParser *parser, GQueue *tokens)
a3ca163c 372{
0c460dac
SH
373 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
374 QObject *obj;
375
376 obj = json_parser_parse(tokens, NULL);
377 if (!obj) {
378 fprintf(stderr, "QMP JSON response parsing failed\n");
379 exit(1);
380 }
381
382 g_assert(qobject_type(obj) == QTYPE_QDICT);
383 g_assert(!qmp->response);
384 qmp->response = (QDict *)obj;
385}
386
dc47995e 387QDict *qmp_fd_receive(int fd)
0c460dac
SH
388{
389 QMPResponseParser qmp;
7c282cb4 390 bool log = getenv("QTEST_LOG") != NULL;
a3ca163c 391
0c460dac
SH
392 qmp.response = NULL;
393 json_message_parser_init(&qmp.parser, qmp_response);
394 while (!qmp.response) {
a3ca163c
KW
395 ssize_t len;
396 char c;
397
dc47995e 398 len = read(fd, &c, 1);
a3ca163c
KW
399 if (len == -1 && errno == EINTR) {
400 continue;
401 }
402
039380a8
AL
403 if (len == -1 || len == 0) {
404 fprintf(stderr, "Broken pipe\n");
405 exit(1);
406 }
407
7c282cb4
PB
408 if (log) {
409 len = write(2, &c, 1);
410 }
0c460dac 411 json_message_parser_feed(&qmp.parser, &c, 1);
a3ca163c 412 }
0c460dac
SH
413 json_message_parser_destroy(&qmp.parser);
414
415 return qmp.response;
416}
417
dc47995e
MAL
418QDict *qtest_qmp_receive(QTestState *s)
419{
420 return qmp_fd_receive(s->qmp_fd);
421}
422
ba4ed393
JS
423/**
424 * Allow users to send a message without waiting for the reply,
425 * in the case that they choose to discard all replies up until
426 * a particular EVENT is received.
427 */
dc47995e 428void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
66e0c7b1 429{
563890c7
PB
430 va_list ap_copy;
431 QObject *qobj;
432
433 /* Going through qobject ensures we escape strings properly.
434 * This seemingly unnecessary copy is required in case va_list
435 * is an array type.
436 */
437 va_copy(ap_copy, ap);
438 qobj = qobject_from_jsonv(fmt, &ap_copy);
439 va_end(ap_copy);
440
441 /* No need to send anything for an empty QObject. */
442 if (qobj) {
ae74f187 443 int log = getenv("QTEST_LOG") != NULL;
563890c7
PB
444 QString *qstr = qobject_to_json(qobj);
445 const char *str = qstring_get_str(qstr);
446 size_t size = qstring_get_length(qstr);
447
ae74f187
MM
448 if (log) {
449 fprintf(stderr, "%s", str);
450 }
563890c7 451 /* Send QMP request */
dc47995e 452 socket_send(fd, str, size);
563890c7
PB
453
454 QDECREF(qstr);
455 qobject_decref(qobj);
456 }
ba4ed393
JS
457}
458
dc47995e
MAL
459void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
460{
461 qmp_fd_sendv(s->qmp_fd, fmt, ap);
462}
463
464QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
465{
466 qmp_fd_sendv(fd, fmt, ap);
467
468 return qmp_fd_receive(fd);
469}
470
ba4ed393
JS
471QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
472{
473 qtest_async_qmpv(s, fmt, ap);
66e0c7b1
AF
474
475 /* Receive reply */
476 return qtest_qmp_receive(s);
477}
478
dc47995e
MAL
479QDict *qmp_fd(int fd, const char *fmt, ...)
480{
481 va_list ap;
482 QDict *response;
483
484 va_start(ap, fmt);
485 response = qmp_fdv(fd, fmt, ap);
486 va_end(ap);
487 return response;
488}
489
490void qmp_fd_send(int fd, const char *fmt, ...)
491{
492 va_list ap;
493
494 va_start(ap, fmt);
495 qmp_fd_sendv(fd, fmt, ap);
496 va_end(ap);
497}
498
0c460dac
SH
499QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
500{
501 va_list ap;
502 QDict *response;
503
504 va_start(ap, fmt);
505 response = qtest_qmpv(s, fmt, ap);
506 va_end(ap);
507 return response;
508}
509
ba4ed393
JS
510void qtest_async_qmp(QTestState *s, const char *fmt, ...)
511{
512 va_list ap;
513
514 va_start(ap, fmt);
515 qtest_async_qmpv(s, fmt, ap);
516 va_end(ap);
517}
518
0c460dac
SH
519void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
520{
521 QDict *response = qtest_qmpv(s, fmt, ap);
522 QDECREF(response);
a3ca163c
KW
523}
524
0d1aa05e 525void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
b73cf9e9
AF
526{
527 va_list ap;
0c460dac 528 QDict *response;
b73cf9e9
AF
529
530 va_start(ap, fmt);
0c460dac 531 response = qtest_qmpv(s, fmt, ap);
b73cf9e9 532 va_end(ap);
0c460dac 533 QDECREF(response);
b73cf9e9
AF
534}
535
7ffe3124 536QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
8fe941f7
JS
537{
538 QDict *response;
539
540 for (;;) {
541 response = qtest_qmp_receive(s);
542 if ((qdict_haskey(response, "event")) &&
543 (strcmp(qdict_get_str(response, "event"), event) == 0)) {
7ffe3124 544 return response;
8fe941f7
JS
545 }
546 QDECREF(response);
547 }
548}
549
7ffe3124
JS
550void qtest_qmp_eventwait(QTestState *s, const char *event)
551{
552 QDict *response;
553
554 response = qtest_qmp_eventwait_ref(s, event);
555 QDECREF(response);
556}
557
5fb48d96
MA
558char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
559{
560 char *cmd;
561 QDict *resp;
562 char *ret;
563
564 cmd = g_strdup_vprintf(fmt, ap);
565 resp = qtest_qmp(s, "{'execute': 'human-monitor-command',"
566 " 'arguments': {'command-line': %s}}",
567 cmd);
568 ret = g_strdup(qdict_get_try_str(resp, "return"));
569 g_assert(ret);
570 QDECREF(resp);
571 g_free(cmd);
572 return ret;
573}
574
575char *qtest_hmp(QTestState *s, const char *fmt, ...)
576{
577 va_list ap;
578 char *ret;
579
580 va_start(ap, fmt);
581 ret = qtest_hmpv(s, fmt, ap);
582 va_end(ap);
583 return ret;
584}
8fe941f7 585
49ee3590
AL
586const char *qtest_get_arch(void)
587{
588 const char *qemu = getenv("QTEST_QEMU_BINARY");
c8368674 589 g_assert(qemu != NULL);
49ee3590
AL
590 const char *end = strrchr(qemu, '/');
591
592 return end + strlen("/qemu-system-");
593}
594
595bool qtest_get_irq(QTestState *s, int num)
596{
597 /* dummy operation in order to make sure irq is up to date */
598 qtest_inb(s, 0);
599
600 return s->irq_level[num];
601}
602
603static int64_t qtest_clock_rsp(QTestState *s)
604{
605 gchar **words;
606 int64_t clock;
607 words = qtest_rsp(s, 2);
608 clock = g_ascii_strtoll(words[1], NULL, 0);
609 g_strfreev(words);
610 return clock;
611}
612
613int64_t qtest_clock_step_next(QTestState *s)
614{
615 qtest_sendf(s, "clock_step\n");
616 return qtest_clock_rsp(s);
617}
618
619int64_t qtest_clock_step(QTestState *s, int64_t step)
620{
621 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
622 return qtest_clock_rsp(s);
623}
624
625int64_t qtest_clock_set(QTestState *s, int64_t val)
626{
627 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
628 return qtest_clock_rsp(s);
629}
630
631void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
632{
633 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
634 qtest_rsp(s, 0);
635}
636
637void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
638{
639 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
640 qtest_rsp(s, 0);
641}
642
643static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
644{
645 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
646 qtest_rsp(s, 0);
647}
648
649void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
650{
651 qtest_out(s, "outb", addr, value);
652}
653
654void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
655{
656 qtest_out(s, "outw", addr, value);
657}
658
659void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
660{
661 qtest_out(s, "outl", addr, value);
662}
663
664static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
665{
666 gchar **args;
667 uint32_t value;
668
669 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
670 args = qtest_rsp(s, 2);
671 value = strtoul(args[1], NULL, 0);
672 g_strfreev(args);
673
674 return value;
675}
676
677uint8_t qtest_inb(QTestState *s, uint16_t addr)
678{
679 return qtest_in(s, "inb", addr);
680}
681
682uint16_t qtest_inw(QTestState *s, uint16_t addr)
683{
684 return qtest_in(s, "inw", addr);
685}
686
687uint32_t qtest_inl(QTestState *s, uint16_t addr)
688{
689 return qtest_in(s, "inl", addr);
690}
691
872536bf
AF
692static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
693 uint64_t value)
694{
695 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
696 qtest_rsp(s, 0);
697}
698
699void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
700{
701 qtest_write(s, "writeb", addr, value);
702}
703
704void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
705{
706 qtest_write(s, "writew", addr, value);
707}
708
709void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
710{
711 qtest_write(s, "writel", addr, value);
712}
713
714void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
715{
716 qtest_write(s, "writeq", addr, value);
717}
718
719static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
720{
721 gchar **args;
722 uint64_t value;
723
724 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
725 args = qtest_rsp(s, 2);
726 value = strtoull(args[1], NULL, 0);
727 g_strfreev(args);
728
729 return value;
730}
731
732uint8_t qtest_readb(QTestState *s, uint64_t addr)
733{
734 return qtest_read(s, "readb", addr);
735}
736
737uint16_t qtest_readw(QTestState *s, uint64_t addr)
738{
739 return qtest_read(s, "readw", addr);
740}
741
742uint32_t qtest_readl(QTestState *s, uint64_t addr)
743{
744 return qtest_read(s, "readl", addr);
745}
746
747uint64_t qtest_readq(QTestState *s, uint64_t addr)
748{
749 return qtest_read(s, "readq", addr);
750}
751
49ee3590
AL
752static int hex2nib(char ch)
753{
754 if (ch >= '0' && ch <= '9') {
755 return ch - '0';
756 } else if (ch >= 'a' && ch <= 'f') {
757 return 10 + (ch - 'a');
758 } else if (ch >= 'A' && ch <= 'F') {
759 return 10 + (ch - 'a');
760 } else {
761 return -1;
762 }
763}
764
765void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
766{
767 uint8_t *ptr = data;
768 gchar **args;
769 size_t i;
770
204febd1
GK
771 if (!size) {
772 return;
773 }
774
ea7697fc 775 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
49ee3590
AL
776 args = qtest_rsp(s, 2);
777
778 for (i = 0; i < size; i++) {
779 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
780 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
781 }
782
783 g_strfreev(args);
784}
785
eeddd59f
LV
786uint64_t qtest_rtas_call(QTestState *s, const char *name,
787 uint32_t nargs, uint64_t args,
788 uint32_t nret, uint64_t ret)
789{
790 qtest_sendf(s, "rtas %s %u 0x%"PRIx64" %u 0x%"PRIx64"\n",
791 name, nargs, args, nret, ret);
792 qtest_rsp(s, 0);
793 return 0;
794}
795
041088c7 796void qtest_add_func(const char *str, void (*fn)(void))
49ee3590
AL
797{
798 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
799 g_test_add_func(path, fn);
a7afc6b8 800 g_free(path);
49ee3590
AL
801}
802
822e36ca
MAL
803void qtest_add_data_func_full(const char *str, void *data,
804 void (*fn)(const void *),
805 GDestroyNotify data_free_func)
806{
807 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
808#if GLIB_CHECK_VERSION(2, 34, 0)
809 g_test_add_data_func_full(path, data, fn, data_free_func);
810#elif GLIB_CHECK_VERSION(2, 26, 0)
811 /* back-compat casts, remove this once we can require new-enough glib */
812 g_test_add_vtable(path, 0, data, NULL,
813 (GTestFixtureFunc)fn, (GTestFixtureFunc) data_free_func);
814#else
815 /* back-compat casts, remove this once we can require new-enough glib */
816 g_test_add_vtable(path, 0, data, NULL,
817 (void (*)(void)) fn, (void (*)(void)) data_free_func);
818#endif
819 g_free(path);
820}
821
041088c7
MA
822void qtest_add_data_func(const char *str, const void *data,
823 void (*fn)(const void *))
7949c0e3
AF
824{
825 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
826 g_test_add_data_func(path, data, fn);
827 g_free(path);
828}
829
7a6a740d
JS
830void qtest_bufwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
831{
832 gchar *bdata;
833
834 bdata = g_base64_encode(data, size);
835 qtest_sendf(s, "b64write 0x%" PRIx64 " 0x%zx ", addr, size);
836 socket_send(s->fd, bdata, strlen(bdata));
837 socket_send(s->fd, "\n", 1);
838 qtest_rsp(s, 0);
839 g_free(bdata);
840}
841
842void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
843{
844 gchar **args;
845 size_t len;
846
847 qtest_sendf(s, "b64read 0x%" PRIx64 " 0x%zx\n", addr, size);
848 args = qtest_rsp(s, 2);
849
850 g_base64_decode_inplace(args[1], &len);
851 if (size != len) {
852 fprintf(stderr, "bufread: asked for %zu bytes but decoded %zu\n",
853 size, len);
854 len = MIN(len, size);
855 }
856
857 memcpy(data, args[1], len);
858 g_strfreev(args);
859}
860
49ee3590
AL
861void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
862{
863 const uint8_t *ptr = data;
864 size_t i;
204febd1
GK
865 char *enc;
866
867 if (!size) {
868 return;
869 }
870
871 enc = g_malloc(2 * size + 1);
49ee3590 872
49ee3590 873 for (i = 0; i < size; i++) {
5560b85a 874 sprintf(&enc[i * 2], "%02x", ptr[i]);
49ee3590 875 }
5560b85a
JS
876
877 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
49ee3590 878 qtest_rsp(s, 0);
5560b85a 879 g_free(enc);
49ee3590 880}
0100f425 881
86298845
JS
882void qtest_memset(QTestState *s, uint64_t addr, uint8_t pattern, size_t size)
883{
4d007963 884 qtest_sendf(s, "memset 0x%" PRIx64 " 0x%zx 0x%02x\n", addr, size, pattern);
86298845
JS
885 qtest_rsp(s, 0);
886}
887
0100f425
PM
888QDict *qmp(const char *fmt, ...)
889{
890 va_list ap;
891 QDict *response;
892
893 va_start(ap, fmt);
894 response = qtest_qmpv(global_qtest, fmt, ap);
895 va_end(ap);
896 return response;
897}
898
ba4ed393
JS
899void qmp_async(const char *fmt, ...)
900{
901 va_list ap;
902
903 va_start(ap, fmt);
904 qtest_async_qmpv(global_qtest, fmt, ap);
905 va_end(ap);
906}
907
0100f425
PM
908void qmp_discard_response(const char *fmt, ...)
909{
910 va_list ap;
911
912 va_start(ap, fmt);
913 qtest_qmpv_discard_response(global_qtest, fmt, ap);
914 va_end(ap);
915}
5fb48d96
MA
916char *hmp(const char *fmt, ...)
917{
918 va_list ap;
919 char *ret;
920
921 va_start(ap, fmt);
922 ret = qtest_hmpv(global_qtest, fmt, ap);
923 va_end(ap);
924 return ret;
925}
46e0cf76 926
54ce6f22 927bool qtest_big_endian(QTestState *s)
46e0cf76 928{
54ce6f22 929 return s->big_endian;
46e0cf76 930}