libqtest: Avoid inline varargs functions
[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 "libqtest.h"
18
19 #include <glib.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <sys/un.h>
24 #include <inttypes.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33 #include "qapi/qmp/json-streamer.h"
34 #include "qapi/qmp/json-parser.h"
35
36 #define MAX_IRQ 256
37
38 QTestState *global_qtest;
39
40 struct QTestState
41 {
42 int fd;
43 int qmp_fd;
44 bool irq_level[MAX_IRQ];
45 GString *rx;
46 pid_t qemu_pid; /* our child QEMU process */
47 struct sigaction sigact_old; /* restored on exit */
48 };
49
50 #define g_assert_no_errno(ret) do { \
51 g_assert_cmpint(ret, !=, -1); \
52 } while (0)
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 listen(sock, 1);
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
82 addrlen = sizeof(addr);
83 do {
84 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
85 } while (ret == -1 && errno == EINTR);
86 g_assert_no_errno(ret);
87 close(sock);
88
89 return ret;
90 }
91
92 static void kill_qemu(QTestState *s)
93 {
94 if (s->qemu_pid != -1) {
95 kill(s->qemu_pid, SIGTERM);
96 waitpid(s->qemu_pid, NULL, 0);
97 }
98 }
99
100 static void sigabrt_handler(int signo)
101 {
102 kill_qemu(global_qtest);
103 }
104
105 QTestState *qtest_init(const char *extra_args)
106 {
107 QTestState *s;
108 int sock, qmpsock, i;
109 gchar *socket_path;
110 gchar *qmp_socket_path;
111 gchar *command;
112 const char *qemu_binary;
113 struct sigaction sigact;
114
115 qemu_binary = getenv("QTEST_QEMU_BINARY");
116 g_assert(qemu_binary != NULL);
117
118 s = g_malloc(sizeof(*s));
119
120 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
121 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
122
123 sock = init_socket(socket_path);
124 qmpsock = init_socket(qmp_socket_path);
125
126 /* Catch SIGABRT to clean up on g_assert() failure */
127 sigact = (struct sigaction){
128 .sa_handler = sigabrt_handler,
129 .sa_flags = SA_RESETHAND,
130 };
131 sigemptyset(&sigact.sa_mask);
132 sigaction(SIGABRT, &sigact, &s->sigact_old);
133
134 s->qemu_pid = fork();
135 if (s->qemu_pid == 0) {
136 command = g_strdup_printf("exec %s "
137 "-qtest unix:%s,nowait "
138 "-qtest-log /dev/null "
139 "-qmp unix:%s,nowait "
140 "-machine accel=qtest "
141 "-display none "
142 "%s", qemu_binary, socket_path,
143 qmp_socket_path,
144 extra_args ?: "");
145 execlp("/bin/sh", "sh", "-c", command, NULL);
146 exit(1);
147 }
148
149 s->fd = socket_accept(sock);
150 s->qmp_fd = socket_accept(qmpsock);
151 unlink(socket_path);
152 unlink(qmp_socket_path);
153 g_free(socket_path);
154 g_free(qmp_socket_path);
155
156 s->rx = g_string_new("");
157 for (i = 0; i < MAX_IRQ; i++) {
158 s->irq_level[i] = false;
159 }
160
161 /* Read the QMP greeting and then do the handshake */
162 qtest_qmp_discard_response(s, "");
163 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
164
165 if (getenv("QTEST_STOP")) {
166 kill(s->qemu_pid, SIGSTOP);
167 }
168
169 return s;
170 }
171
172 void qtest_quit(QTestState *s)
173 {
174 sigaction(SIGABRT, &s->sigact_old, NULL);
175
176 kill_qemu(s);
177 close(s->fd);
178 close(s->qmp_fd);
179 g_string_free(s->rx, true);
180 g_free(s);
181 }
182
183 static void socket_sendf(int fd, const char *fmt, va_list ap)
184 {
185 gchar *str;
186 size_t size, offset;
187
188 str = g_strdup_vprintf(fmt, ap);
189 size = strlen(str);
190
191 offset = 0;
192 while (offset < size) {
193 ssize_t len;
194
195 len = write(fd, str + offset, size - offset);
196 if (len == -1 && errno == EINTR) {
197 continue;
198 }
199
200 g_assert_no_errno(len);
201 g_assert_cmpint(len, >, 0);
202
203 offset += len;
204 }
205 }
206
207 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
208 {
209 va_list ap;
210
211 va_start(ap, fmt);
212 socket_sendf(s->fd, fmt, ap);
213 va_end(ap);
214 }
215
216 static GString *qtest_recv_line(QTestState *s)
217 {
218 GString *line;
219 size_t offset;
220 char *eol;
221
222 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
223 ssize_t len;
224 char buffer[1024];
225
226 len = read(s->fd, buffer, sizeof(buffer));
227 if (len == -1 && errno == EINTR) {
228 continue;
229 }
230
231 if (len == -1 || len == 0) {
232 fprintf(stderr, "Broken pipe\n");
233 exit(1);
234 }
235
236 g_string_append_len(s->rx, buffer, len);
237 }
238
239 offset = eol - s->rx->str;
240 line = g_string_new_len(s->rx->str, offset);
241 g_string_erase(s->rx, 0, offset + 1);
242
243 return line;
244 }
245
246 static gchar **qtest_rsp(QTestState *s, int expected_args)
247 {
248 GString *line;
249 gchar **words;
250 int i;
251
252 redo:
253 line = qtest_recv_line(s);
254 words = g_strsplit(line->str, " ", 0);
255 g_string_free(line, TRUE);
256
257 if (strcmp(words[0], "IRQ") == 0) {
258 int irq;
259
260 g_assert(words[1] != NULL);
261 g_assert(words[2] != NULL);
262
263 irq = strtoul(words[2], NULL, 0);
264 g_assert_cmpint(irq, >=, 0);
265 g_assert_cmpint(irq, <, MAX_IRQ);
266
267 if (strcmp(words[1], "raise") == 0) {
268 s->irq_level[irq] = true;
269 } else {
270 s->irq_level[irq] = false;
271 }
272
273 g_strfreev(words);
274 goto redo;
275 }
276
277 g_assert(words[0] != NULL);
278 g_assert_cmpstr(words[0], ==, "OK");
279
280 if (expected_args) {
281 for (i = 0; i < expected_args; i++) {
282 g_assert(words[i] != NULL);
283 }
284 } else {
285 g_strfreev(words);
286 }
287
288 return words;
289 }
290
291 typedef struct {
292 JSONMessageParser parser;
293 QDict *response;
294 } QMPResponseParser;
295
296 static void qmp_response(JSONMessageParser *parser, QList *tokens)
297 {
298 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
299 QObject *obj;
300
301 obj = json_parser_parse(tokens, NULL);
302 if (!obj) {
303 fprintf(stderr, "QMP JSON response parsing failed\n");
304 exit(1);
305 }
306
307 g_assert(qobject_type(obj) == QTYPE_QDICT);
308 g_assert(!qmp->response);
309 qmp->response = (QDict *)obj;
310 }
311
312 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
313 {
314 QMPResponseParser qmp;
315
316 /* Send QMP request */
317 socket_sendf(s->qmp_fd, fmt, ap);
318
319 /* Receive reply */
320 qmp.response = NULL;
321 json_message_parser_init(&qmp.parser, qmp_response);
322 while (!qmp.response) {
323 ssize_t len;
324 char c;
325
326 len = read(s->qmp_fd, &c, 1);
327 if (len == -1 && errno == EINTR) {
328 continue;
329 }
330
331 if (len == -1 || len == 0) {
332 fprintf(stderr, "Broken pipe\n");
333 exit(1);
334 }
335
336 json_message_parser_feed(&qmp.parser, &c, 1);
337 }
338 json_message_parser_destroy(&qmp.parser);
339
340 return qmp.response;
341 }
342
343 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
344 {
345 va_list ap;
346 QDict *response;
347
348 va_start(ap, fmt);
349 response = qtest_qmpv(s, fmt, ap);
350 va_end(ap);
351 return response;
352 }
353
354 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
355 {
356 QDict *response = qtest_qmpv(s, fmt, ap);
357 QDECREF(response);
358 }
359
360 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
361 {
362 va_list ap;
363 QDict *response;
364
365 va_start(ap, fmt);
366 response = qtest_qmpv(s, fmt, ap);
367 va_end(ap);
368 QDECREF(response);
369 }
370
371 const char *qtest_get_arch(void)
372 {
373 const char *qemu = getenv("QTEST_QEMU_BINARY");
374 const char *end = strrchr(qemu, '/');
375
376 return end + strlen("/qemu-system-");
377 }
378
379 bool qtest_get_irq(QTestState *s, int num)
380 {
381 /* dummy operation in order to make sure irq is up to date */
382 qtest_inb(s, 0);
383
384 return s->irq_level[num];
385 }
386
387 static int64_t qtest_clock_rsp(QTestState *s)
388 {
389 gchar **words;
390 int64_t clock;
391 words = qtest_rsp(s, 2);
392 clock = g_ascii_strtoll(words[1], NULL, 0);
393 g_strfreev(words);
394 return clock;
395 }
396
397 int64_t qtest_clock_step_next(QTestState *s)
398 {
399 qtest_sendf(s, "clock_step\n");
400 return qtest_clock_rsp(s);
401 }
402
403 int64_t qtest_clock_step(QTestState *s, int64_t step)
404 {
405 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
406 return qtest_clock_rsp(s);
407 }
408
409 int64_t qtest_clock_set(QTestState *s, int64_t val)
410 {
411 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
412 return qtest_clock_rsp(s);
413 }
414
415 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
416 {
417 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
418 qtest_rsp(s, 0);
419 }
420
421 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
422 {
423 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
424 qtest_rsp(s, 0);
425 }
426
427 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
428 {
429 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
430 qtest_rsp(s, 0);
431 }
432
433 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
434 {
435 qtest_out(s, "outb", addr, value);
436 }
437
438 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
439 {
440 qtest_out(s, "outw", addr, value);
441 }
442
443 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
444 {
445 qtest_out(s, "outl", addr, value);
446 }
447
448 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
449 {
450 gchar **args;
451 uint32_t value;
452
453 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
454 args = qtest_rsp(s, 2);
455 value = strtoul(args[1], NULL, 0);
456 g_strfreev(args);
457
458 return value;
459 }
460
461 uint8_t qtest_inb(QTestState *s, uint16_t addr)
462 {
463 return qtest_in(s, "inb", addr);
464 }
465
466 uint16_t qtest_inw(QTestState *s, uint16_t addr)
467 {
468 return qtest_in(s, "inw", addr);
469 }
470
471 uint32_t qtest_inl(QTestState *s, uint16_t addr)
472 {
473 return qtest_in(s, "inl", addr);
474 }
475
476 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
477 uint64_t value)
478 {
479 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
480 qtest_rsp(s, 0);
481 }
482
483 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
484 {
485 qtest_write(s, "writeb", addr, value);
486 }
487
488 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
489 {
490 qtest_write(s, "writew", addr, value);
491 }
492
493 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
494 {
495 qtest_write(s, "writel", addr, value);
496 }
497
498 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
499 {
500 qtest_write(s, "writeq", addr, value);
501 }
502
503 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
504 {
505 gchar **args;
506 uint64_t value;
507
508 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
509 args = qtest_rsp(s, 2);
510 value = strtoull(args[1], NULL, 0);
511 g_strfreev(args);
512
513 return value;
514 }
515
516 uint8_t qtest_readb(QTestState *s, uint64_t addr)
517 {
518 return qtest_read(s, "readb", addr);
519 }
520
521 uint16_t qtest_readw(QTestState *s, uint64_t addr)
522 {
523 return qtest_read(s, "readw", addr);
524 }
525
526 uint32_t qtest_readl(QTestState *s, uint64_t addr)
527 {
528 return qtest_read(s, "readl", addr);
529 }
530
531 uint64_t qtest_readq(QTestState *s, uint64_t addr)
532 {
533 return qtest_read(s, "readq", addr);
534 }
535
536 static int hex2nib(char ch)
537 {
538 if (ch >= '0' && ch <= '9') {
539 return ch - '0';
540 } else if (ch >= 'a' && ch <= 'f') {
541 return 10 + (ch - 'a');
542 } else if (ch >= 'A' && ch <= 'F') {
543 return 10 + (ch - 'a');
544 } else {
545 return -1;
546 }
547 }
548
549 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
550 {
551 uint8_t *ptr = data;
552 gchar **args;
553 size_t i;
554
555 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
556 args = qtest_rsp(s, 2);
557
558 for (i = 0; i < size; i++) {
559 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
560 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
561 }
562
563 g_strfreev(args);
564 }
565
566 void qtest_add_func(const char *str, void (*fn))
567 {
568 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
569 g_test_add_func(path, fn);
570 }
571
572 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
573 {
574 const uint8_t *ptr = data;
575 size_t i;
576
577 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
578 for (i = 0; i < size; i++) {
579 qtest_sendf(s, "%02x", ptr[i]);
580 }
581 qtest_sendf(s, "\n");
582 qtest_rsp(s, 0);
583 }
584
585 QDict *qmp(const char *fmt, ...)
586 {
587 va_list ap;
588 QDict *response;
589
590 va_start(ap, fmt);
591 response = qtest_qmpv(global_qtest, fmt, ap);
592 va_end(ap);
593 return response;
594 }
595
596 void qmp_discard_response(const char *fmt, ...)
597 {
598 va_list ap;
599
600 va_start(ap, fmt);
601 qtest_qmpv_discard_response(global_qtest, fmt, ap);
602 va_end(ap);
603 }