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