acpi-build: append description for non-hotplug
[qemu/cris-port.git] / tests / libqtest.c
blobf587d36176ed388c69ab9ca48743f6b0f95c95c1
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 pid_t qemu_pid; /* our child QEMU process */
47 struct sigaction sigact_old; /* restored on exit */
50 #define g_assert_no_errno(ret) do { \
51 g_assert_cmpint(ret, !=, -1); \
52 } while (0)
54 static int init_socket(const char *socket_path)
56 struct sockaddr_un addr;
57 int sock;
58 int ret;
60 sock = socket(PF_UNIX, SOCK_STREAM, 0);
61 g_assert_no_errno(sock);
63 addr.sun_family = AF_UNIX;
64 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
65 qemu_set_cloexec(sock);
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);
73 return sock;
76 static int socket_accept(int sock)
78 struct sockaddr_un addr;
79 socklen_t addrlen;
80 int ret;
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);
89 return ret;
92 static void kill_qemu(QTestState *s)
94 if (s->qemu_pid != -1) {
95 kill(s->qemu_pid, SIGTERM);
96 waitpid(s->qemu_pid, NULL, 0);
100 static void sigabrt_handler(int signo)
102 kill_qemu(global_qtest);
105 QTestState *qtest_init(const char *extra_args)
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;
115 qemu_binary = getenv("QTEST_QEMU_BINARY");
116 g_assert(qemu_binary != NULL);
118 s = g_malloc(sizeof(*s));
120 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
121 qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
123 sock = init_socket(socket_path);
124 qmpsock = init_socket(qmp_socket_path);
126 /* Catch SIGABRT to clean up on g_assert() failure */
127 sigact = (struct sigaction){
128 .sa_handler = sigabrt_handler,
129 .sa_flags = SA_RESETHAND,
131 sigemptyset(&sigact.sa_mask);
132 sigaction(SIGABRT, &sigact, &s->sigact_old);
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);
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);
156 s->rx = g_string_new("");
157 for (i = 0; i < MAX_IRQ; i++) {
158 s->irq_level[i] = false;
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' }");
165 if (getenv("QTEST_STOP")) {
166 kill(s->qemu_pid, SIGSTOP);
169 return s;
172 void qtest_quit(QTestState *s)
174 sigaction(SIGABRT, &s->sigact_old, NULL);
176 kill_qemu(s);
177 close(s->fd);
178 close(s->qmp_fd);
179 g_string_free(s->rx, true);
180 g_free(s);
183 static void socket_sendf(int fd, const char *fmt, va_list ap)
185 gchar *str;
186 size_t size, offset;
188 str = g_strdup_vprintf(fmt, ap);
189 size = strlen(str);
191 offset = 0;
192 while (offset < size) {
193 ssize_t len;
195 len = write(fd, str + offset, size - offset);
196 if (len == -1 && errno == EINTR) {
197 continue;
200 g_assert_no_errno(len);
201 g_assert_cmpint(len, >, 0);
203 offset += len;
207 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
209 va_list ap;
211 va_start(ap, fmt);
212 socket_sendf(s->fd, fmt, ap);
213 va_end(ap);
216 static GString *qtest_recv_line(QTestState *s)
218 GString *line;
219 size_t offset;
220 char *eol;
222 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
223 ssize_t len;
224 char buffer[1024];
226 len = read(s->fd, buffer, sizeof(buffer));
227 if (len == -1 && errno == EINTR) {
228 continue;
231 if (len == -1 || len == 0) {
232 fprintf(stderr, "Broken pipe\n");
233 exit(1);
236 g_string_append_len(s->rx, buffer, len);
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);
243 return line;
246 static gchar **qtest_rsp(QTestState *s, int expected_args)
248 GString *line;
249 gchar **words;
250 int i;
252 redo:
253 line = qtest_recv_line(s);
254 words = g_strsplit(line->str, " ", 0);
255 g_string_free(line, TRUE);
257 if (strcmp(words[0], "IRQ") == 0) {
258 int irq;
260 g_assert(words[1] != NULL);
261 g_assert(words[2] != NULL);
263 irq = strtoul(words[2], NULL, 0);
264 g_assert_cmpint(irq, >=, 0);
265 g_assert_cmpint(irq, <, MAX_IRQ);
267 if (strcmp(words[1], "raise") == 0) {
268 s->irq_level[irq] = true;
269 } else {
270 s->irq_level[irq] = false;
273 g_strfreev(words);
274 goto redo;
277 g_assert(words[0] != NULL);
278 g_assert_cmpstr(words[0], ==, "OK");
280 if (expected_args) {
281 for (i = 0; i < expected_args; i++) {
282 g_assert(words[i] != NULL);
284 } else {
285 g_strfreev(words);
288 return words;
291 typedef struct {
292 JSONMessageParser parser;
293 QDict *response;
294 } QMPResponseParser;
296 static void qmp_response(JSONMessageParser *parser, QList *tokens)
298 QMPResponseParser *qmp = container_of(parser, QMPResponseParser, parser);
299 QObject *obj;
301 obj = json_parser_parse(tokens, NULL);
302 if (!obj) {
303 fprintf(stderr, "QMP JSON response parsing failed\n");
304 exit(1);
307 g_assert(qobject_type(obj) == QTYPE_QDICT);
308 g_assert(!qmp->response);
309 qmp->response = (QDict *)obj;
312 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
314 QMPResponseParser qmp;
316 /* Send QMP request */
317 socket_sendf(s->qmp_fd, fmt, ap);
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;
326 len = read(s->qmp_fd, &c, 1);
327 if (len == -1 && errno == EINTR) {
328 continue;
331 if (len == -1 || len == 0) {
332 fprintf(stderr, "Broken pipe\n");
333 exit(1);
336 json_message_parser_feed(&qmp.parser, &c, 1);
338 json_message_parser_destroy(&qmp.parser);
340 return qmp.response;
343 QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
345 va_list ap;
346 QDict *response;
348 va_start(ap, fmt);
349 response = qtest_qmpv(s, fmt, ap);
350 va_end(ap);
351 return response;
354 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
356 QDict *response = qtest_qmpv(s, fmt, ap);
357 QDECREF(response);
360 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
362 va_list ap;
363 QDict *response;
365 va_start(ap, fmt);
366 response = qtest_qmpv(s, fmt, ap);
367 va_end(ap);
368 QDECREF(response);
371 const char *qtest_get_arch(void)
373 const char *qemu = getenv("QTEST_QEMU_BINARY");
374 const char *end = strrchr(qemu, '/');
376 return end + strlen("/qemu-system-");
379 bool qtest_get_irq(QTestState *s, int num)
381 /* dummy operation in order to make sure irq is up to date */
382 qtest_inb(s, 0);
384 return s->irq_level[num];
387 static int64_t qtest_clock_rsp(QTestState *s)
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;
397 int64_t qtest_clock_step_next(QTestState *s)
399 qtest_sendf(s, "clock_step\n");
400 return qtest_clock_rsp(s);
403 int64_t qtest_clock_step(QTestState *s, int64_t step)
405 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
406 return qtest_clock_rsp(s);
409 int64_t qtest_clock_set(QTestState *s, int64_t val)
411 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
412 return qtest_clock_rsp(s);
415 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
417 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
418 qtest_rsp(s, 0);
421 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
423 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
424 qtest_rsp(s, 0);
427 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
429 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
430 qtest_rsp(s, 0);
433 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
435 qtest_out(s, "outb", addr, value);
438 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
440 qtest_out(s, "outw", addr, value);
443 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
445 qtest_out(s, "outl", addr, value);
448 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
450 gchar **args;
451 uint32_t value;
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);
458 return value;
461 uint8_t qtest_inb(QTestState *s, uint16_t addr)
463 return qtest_in(s, "inb", addr);
466 uint16_t qtest_inw(QTestState *s, uint16_t addr)
468 return qtest_in(s, "inw", addr);
471 uint32_t qtest_inl(QTestState *s, uint16_t addr)
473 return qtest_in(s, "inl", addr);
476 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
477 uint64_t value)
479 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
480 qtest_rsp(s, 0);
483 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
485 qtest_write(s, "writeb", addr, value);
488 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
490 qtest_write(s, "writew", addr, value);
493 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
495 qtest_write(s, "writel", addr, value);
498 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
500 qtest_write(s, "writeq", addr, value);
503 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
505 gchar **args;
506 uint64_t value;
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);
513 return value;
516 uint8_t qtest_readb(QTestState *s, uint64_t addr)
518 return qtest_read(s, "readb", addr);
521 uint16_t qtest_readw(QTestState *s, uint64_t addr)
523 return qtest_read(s, "readw", addr);
526 uint32_t qtest_readl(QTestState *s, uint64_t addr)
528 return qtest_read(s, "readl", addr);
531 uint64_t qtest_readq(QTestState *s, uint64_t addr)
533 return qtest_read(s, "readq", addr);
536 static int hex2nib(char ch)
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;
549 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
551 uint8_t *ptr = data;
552 gchar **args;
553 size_t i;
555 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
556 args = qtest_rsp(s, 2);
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]);
563 g_strfreev(args);
566 void qtest_add_func(const char *str, void (*fn))
568 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
569 g_test_add_func(path, fn);
572 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
574 const uint8_t *ptr = data;
575 size_t i;
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]);
581 qtest_sendf(s, "\n");
582 qtest_rsp(s, 0);