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