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