Merge remote-tracking branch 'luiz/queue/qmp' into staging
[qemu/raspi.git] / tests / libqtest.c
blob83424c3c6b69975c702bb9a5a5241035b41f57c0
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
6 * Copyright SUSE LINUX Products GmbH 2013
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
11 * Andreas Färber <afaerber@suse.de>
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.
17 #include "libqtest.h"
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>
31 #include "qemu/compiler.h"
32 #include "qemu/osdep.h"
33 #include "qapi/qmp/json-streamer.h"
34 #include "qapi/qmp/json-parser.h"
36 #define MAX_IRQ 256
38 QTestState *global_qtest;
40 struct QTestState
42 int fd;
43 int qmp_fd;
44 bool irq_level[MAX_IRQ];
45 GString *rx;
46 gchar *pid_file; /* QEMU PID file */
47 int child_pid; /* Child process created to execute QEMU */
48 char *socket_path, *qmp_socket_path;
51 #define g_assert_no_errno(ret) do { \
52 g_assert_cmpint(ret, !=, -1); \
53 } while (0)
55 static int init_socket(const char *socket_path)
57 struct sockaddr_un addr;
58 int sock;
59 int ret;
61 sock = socket(PF_UNIX, SOCK_STREAM, 0);
62 g_assert_no_errno(sock);
64 addr.sun_family = AF_UNIX;
65 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
66 qemu_set_cloexec(sock);
68 do {
69 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
70 } while (ret == -1 && errno == EINTR);
71 g_assert_no_errno(ret);
72 listen(sock, 1);
74 return sock;
77 static int socket_accept(int sock)
79 struct sockaddr_un addr;
80 socklen_t addrlen;
81 int ret;
83 addrlen = sizeof(addr);
84 do {
85 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
86 } while (ret == -1 && errno == EINTR);
87 g_assert_no_errno(ret);
88 close(sock);
90 return ret;
93 static pid_t qtest_qemu_pid(QTestState *s)
95 FILE *f;
96 char buffer[1024];
97 pid_t pid = -1;
99 f = fopen(s->pid_file, "r");
100 if (f) {
101 if (fgets(buffer, sizeof(buffer), f)) {
102 pid = atoi(buffer);
104 fclose(f);
106 return pid;
109 QTestState *qtest_init(const char *extra_args)
111 QTestState *s;
112 int sock, qmpsock, i;
113 gchar *pid_file;
114 gchar *command;
115 const char *qemu_binary;
116 pid_t pid;
118 qemu_binary = getenv("QTEST_QEMU_BINARY");
119 g_assert(qemu_binary != NULL);
121 s = g_malloc(sizeof(*s));
123 s->socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
124 s->qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
125 pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
127 sock = init_socket(s->socket_path);
128 qmpsock = init_socket(s->qmp_socket_path);
130 pid = fork();
131 if (pid == 0) {
132 command = g_strdup_printf("%s "
133 "-qtest unix:%s,nowait "
134 "-qtest-log /dev/null "
135 "-qmp unix:%s,nowait "
136 "-pidfile %s "
137 "-machine accel=qtest "
138 "%s", qemu_binary, s->socket_path,
139 s->qmp_socket_path, pid_file,
140 extra_args ?: "");
141 execlp("/bin/sh", "sh", "-c", command, NULL);
142 exit(1);
145 s->fd = socket_accept(sock);
146 s->qmp_fd = socket_accept(qmpsock);
148 s->rx = g_string_new("");
149 s->pid_file = pid_file;
150 s->child_pid = pid;
151 for (i = 0; i < MAX_IRQ; i++) {
152 s->irq_level[i] = false;
155 /* Read the QMP greeting and then do the handshake */
156 qtest_qmp_discard_response(s, "");
157 qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
159 if (getenv("QTEST_STOP")) {
160 kill(qtest_qemu_pid(s), SIGSTOP);
163 return s;
166 void qtest_quit(QTestState *s)
168 int status;
170 pid_t pid = qtest_qemu_pid(s);
171 if (pid != -1) {
172 kill(pid, SIGTERM);
173 waitpid(pid, &status, 0);
176 close(s->fd);
177 close(s->qmp_fd);
178 g_string_free(s->rx, true);
179 unlink(s->pid_file);
180 unlink(s->socket_path);
181 unlink(s->qmp_socket_path);
182 g_free(s->pid_file);
183 g_free(s->socket_path);
184 g_free(s->qmp_socket_path);
185 g_free(s);
188 static void socket_sendf(int fd, const char *fmt, va_list ap)
190 gchar *str;
191 size_t size, offset;
193 str = g_strdup_vprintf(fmt, ap);
194 size = strlen(str);
196 offset = 0;
197 while (offset < size) {
198 ssize_t len;
200 len = write(fd, str + offset, size - offset);
201 if (len == -1 && errno == EINTR) {
202 continue;
205 g_assert_no_errno(len);
206 g_assert_cmpint(len, >, 0);
208 offset += len;
212 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
214 va_list ap;
216 va_start(ap, fmt);
217 socket_sendf(s->fd, fmt, ap);
218 va_end(ap);
221 static GString *qtest_recv_line(QTestState *s)
223 GString *line;
224 size_t offset;
225 char *eol;
227 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
228 ssize_t len;
229 char buffer[1024];
231 len = read(s->fd, buffer, sizeof(buffer));
232 if (len == -1 && errno == EINTR) {
233 continue;
236 if (len == -1 || len == 0) {
237 fprintf(stderr, "Broken pipe\n");
238 exit(1);
241 g_string_append_len(s->rx, buffer, len);
244 offset = eol - s->rx->str;
245 line = g_string_new_len(s->rx->str, offset);
246 g_string_erase(s->rx, 0, offset + 1);
248 return line;
251 static gchar **qtest_rsp(QTestState *s, int expected_args)
253 GString *line;
254 gchar **words;
255 int i;
257 redo:
258 line = qtest_recv_line(s);
259 words = g_strsplit(line->str, " ", 0);
260 g_string_free(line, TRUE);
262 if (strcmp(words[0], "IRQ") == 0) {
263 int irq;
265 g_assert(words[1] != NULL);
266 g_assert(words[2] != NULL);
268 irq = strtoul(words[2], NULL, 0);
269 g_assert_cmpint(irq, >=, 0);
270 g_assert_cmpint(irq, <, MAX_IRQ);
272 if (strcmp(words[1], "raise") == 0) {
273 s->irq_level[irq] = true;
274 } else {
275 s->irq_level[irq] = false;
278 g_strfreev(words);
279 goto redo;
282 g_assert(words[0] != NULL);
283 g_assert_cmpstr(words[0], ==, "OK");
285 if (expected_args) {
286 for (i = 0; i < expected_args; i++) {
287 g_assert(words[i] != NULL);
289 } else {
290 g_strfreev(words);
293 return words;
296 typedef struct {
297 JSONMessageParser parser;
298 QDict *response;
299 } QMPResponseParser;
301 static void qmp_response(JSONMessageParser *parser, QList *tokens)
303 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
304 QObject *obj;
306 obj = json_parser_parse(tokens, NULL);
307 if (!obj) {
308 fprintf(stderr, "QMP JSON response parsing failed\n");
309 exit(1);
312 g_assert(qobject_type(obj) == QTYPE_QDICT);
313 g_assert(!qmp->response);
314 qmp->response = (QDict *)obj;
317 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
319 QMPResponseParser qmp;
321 /* Send QMP request */
322 socket_sendf(s->qmp_fd, fmt, ap);
324 /* Receive reply */
325 qmp.response = NULL;
326 json_message_parser_init(&qmp.parser, qmp_response);
327 while (!qmp.response) {
328 ssize_t len;
329 char c;
331 len = read(s->qmp_fd, &c, 1);
332 if (len == -1 && errno == EINTR) {
333 continue;
336 if (len == -1 || len == 0) {
337 fprintf(stderr, "Broken pipe\n");
338 exit(1);
341 json_message_parser_feed(&qmp.parser, &c, 1);
343 json_message_parser_destroy(&qmp.parser);
345 return qmp.response;
348 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
350 va_list ap;
351 QDict *response;
353 va_start(ap, fmt);
354 response = qtest_qmpv(s, fmt, ap);
355 va_end(ap);
356 return response;
359 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
361 QDict *response = qtest_qmpv(s, fmt, ap);
362 QDECREF(response);
365 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
367 va_list ap;
368 QDict *response;
370 va_start(ap, fmt);
371 response = qtest_qmpv(s, fmt, ap);
372 va_end(ap);
373 QDECREF(response);
376 const char *qtest_get_arch(void)
378 const char *qemu = getenv("QTEST_QEMU_BINARY");
379 const char *end = strrchr(qemu, '/');
381 return end + strlen("/qemu-system-");
384 bool qtest_get_irq(QTestState *s, int num)
386 /* dummy operation in order to make sure irq is up to date */
387 qtest_inb(s, 0);
389 return s->irq_level[num];
392 static int64_t qtest_clock_rsp(QTestState *s)
394 gchar **words;
395 int64_t clock;
396 words = qtest_rsp(s, 2);
397 clock = g_ascii_strtoll(words[1], NULL, 0);
398 g_strfreev(words);
399 return clock;
402 int64_t qtest_clock_step_next(QTestState *s)
404 qtest_sendf(s, "clock_step\n");
405 return qtest_clock_rsp(s);
408 int64_t qtest_clock_step(QTestState *s, int64_t step)
410 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
411 return qtest_clock_rsp(s);
414 int64_t qtest_clock_set(QTestState *s, int64_t val)
416 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
417 return qtest_clock_rsp(s);
420 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
422 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
423 qtest_rsp(s, 0);
426 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
428 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
429 qtest_rsp(s, 0);
432 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
434 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
435 qtest_rsp(s, 0);
438 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
440 qtest_out(s, "outb", addr, value);
443 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
445 qtest_out(s, "outw", addr, value);
448 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
450 qtest_out(s, "outl", addr, value);
453 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
455 gchar **args;
456 uint32_t value;
458 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
459 args = qtest_rsp(s, 2);
460 value = strtoul(args[1], NULL, 0);
461 g_strfreev(args);
463 return value;
466 uint8_t qtest_inb(QTestState *s, uint16_t addr)
468 return qtest_in(s, "inb", addr);
471 uint16_t qtest_inw(QTestState *s, uint16_t addr)
473 return qtest_in(s, "inw", addr);
476 uint32_t qtest_inl(QTestState *s, uint16_t addr)
478 return qtest_in(s, "inl", addr);
481 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
482 uint64_t value)
484 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
485 qtest_rsp(s, 0);
488 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
490 qtest_write(s, "writeb", addr, value);
493 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
495 qtest_write(s, "writew", addr, value);
498 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
500 qtest_write(s, "writel", addr, value);
503 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
505 qtest_write(s, "writeq", addr, value);
508 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
510 gchar **args;
511 uint64_t value;
513 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
514 args = qtest_rsp(s, 2);
515 value = strtoull(args[1], NULL, 0);
516 g_strfreev(args);
518 return value;
521 uint8_t qtest_readb(QTestState *s, uint64_t addr)
523 return qtest_read(s, "readb", addr);
526 uint16_t qtest_readw(QTestState *s, uint64_t addr)
528 return qtest_read(s, "readw", addr);
531 uint32_t qtest_readl(QTestState *s, uint64_t addr)
533 return qtest_read(s, "readl", addr);
536 uint64_t qtest_readq(QTestState *s, uint64_t addr)
538 return qtest_read(s, "readq", addr);
541 static int hex2nib(char ch)
543 if (ch >= '0' && ch <= '9') {
544 return ch - '0';
545 } else if (ch >= 'a' && ch <= 'f') {
546 return 10 + (ch - 'a');
547 } else if (ch >= 'A' && ch <= 'F') {
548 return 10 + (ch - 'a');
549 } else {
550 return -1;
554 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
556 uint8_t *ptr = data;
557 gchar **args;
558 size_t i;
560 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
561 args = qtest_rsp(s, 2);
563 for (i = 0; i < size; i++) {
564 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
565 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
568 g_strfreev(args);
571 void qtest_add_func(const char *str, void (*fn))
573 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
574 g_test_add_func(path, fn);
577 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
579 const uint8_t *ptr = data;
580 size_t i;
582 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
583 for (i = 0; i < size; i++) {
584 qtest_sendf(s, "%02x", ptr[i]);
586 qtest_sendf(s, "\n");
587 qtest_rsp(s, 0);