win32: add generic RC rules to rules.mak
[qemu/ar7.git] / tests / libqtest.c
blob884f959992e359573282e35e2e77d8efd0aa400b
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"
34 #define MAX_IRQ 256
36 QTestState *global_qtest;
38 struct QTestState
40 int fd;
41 int qmp_fd;
42 bool irq_level[MAX_IRQ];
43 GString *rx;
44 gchar *pid_file; /* QEMU PID file */
45 int child_pid; /* Child process created to execute QEMU */
46 char *socket_path, *qmp_socket_path;
49 #define g_assert_no_errno(ret) do { \
50 g_assert_cmpint(ret, !=, -1); \
51 } while (0)
53 static int init_socket(const char *socket_path)
55 struct sockaddr_un addr;
56 int sock;
57 int ret;
59 sock = socket(PF_UNIX, SOCK_STREAM, 0);
60 g_assert_no_errno(sock);
62 addr.sun_family = AF_UNIX;
63 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
64 qemu_set_cloexec(sock);
66 do {
67 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
68 } while (ret == -1 && errno == EINTR);
69 g_assert_no_errno(ret);
70 listen(sock, 1);
72 return sock;
75 static int socket_accept(int sock)
77 struct sockaddr_un addr;
78 socklen_t addrlen;
79 int ret;
81 addrlen = sizeof(addr);
82 do {
83 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
84 } while (ret == -1 && errno == EINTR);
85 g_assert_no_errno(ret);
86 close(sock);
88 return ret;
91 static pid_t qtest_qemu_pid(QTestState *s)
93 FILE *f;
94 char buffer[1024];
95 pid_t pid = -1;
97 f = fopen(s->pid_file, "r");
98 if (f) {
99 if (fgets(buffer, sizeof(buffer), f)) {
100 pid = atoi(buffer);
103 fclose(f);
104 return pid;
107 QTestState *qtest_init(const char *extra_args)
109 QTestState *s;
110 int sock, qmpsock, i;
111 gchar *pid_file;
112 gchar *command;
113 const char *qemu_binary;
114 pid_t pid;
116 qemu_binary = getenv("QTEST_QEMU_BINARY");
117 g_assert(qemu_binary != NULL);
119 s = g_malloc(sizeof(*s));
121 s->socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
122 s->qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
123 pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
125 sock = init_socket(s->socket_path);
126 qmpsock = init_socket(s->qmp_socket_path);
128 pid = fork();
129 if (pid == 0) {
130 command = g_strdup_printf("%s "
131 "-qtest unix:%s,nowait "
132 "-qtest-log /dev/null "
133 "-qmp unix:%s,nowait "
134 "-pidfile %s "
135 "-machine accel=qtest "
136 "%s", qemu_binary, s->socket_path,
137 s->qmp_socket_path, pid_file,
138 extra_args ?: "");
139 execlp("/bin/sh", "sh", "-c", command, NULL);
140 exit(1);
143 s->fd = socket_accept(sock);
144 s->qmp_fd = socket_accept(qmpsock);
146 s->rx = g_string_new("");
147 s->pid_file = pid_file;
148 s->child_pid = pid;
149 for (i = 0; i < MAX_IRQ; i++) {
150 s->irq_level[i] = false;
153 /* Read the QMP greeting and then do the handshake */
154 qtest_qmp(s, "");
155 qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }");
157 if (getenv("QTEST_STOP")) {
158 kill(qtest_qemu_pid(s), SIGSTOP);
161 return s;
164 void qtest_quit(QTestState *s)
166 int status;
168 pid_t pid = qtest_qemu_pid(s);
169 if (pid != -1) {
170 kill(pid, SIGTERM);
171 waitpid(pid, &status, 0);
174 unlink(s->pid_file);
175 unlink(s->socket_path);
176 unlink(s->qmp_socket_path);
177 g_free(s->pid_file);
178 g_free(s->socket_path);
179 g_free(s->qmp_socket_path);
182 static void socket_sendf(int fd, const char *fmt, va_list ap)
184 gchar *str;
185 size_t size, offset;
187 str = g_strdup_vprintf(fmt, ap);
188 size = strlen(str);
190 offset = 0;
191 while (offset < size) {
192 ssize_t len;
194 len = write(fd, str + offset, size - offset);
195 if (len == -1 && errno == EINTR) {
196 continue;
199 g_assert_no_errno(len);
200 g_assert_cmpint(len, >, 0);
202 offset += len;
206 static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...)
208 va_list ap;
210 va_start(ap, fmt);
211 socket_sendf(s->fd, fmt, ap);
212 va_end(ap);
215 static GString *qtest_recv_line(QTestState *s)
217 GString *line;
218 size_t offset;
219 char *eol;
221 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
222 ssize_t len;
223 char buffer[1024];
225 len = read(s->fd, buffer, sizeof(buffer));
226 if (len == -1 && errno == EINTR) {
227 continue;
230 if (len == -1 || len == 0) {
231 fprintf(stderr, "Broken pipe\n");
232 exit(1);
235 g_string_append_len(s->rx, buffer, len);
238 offset = eol - s->rx->str;
239 line = g_string_new_len(s->rx->str, offset);
240 g_string_erase(s->rx, 0, offset + 1);
242 return line;
245 static gchar **qtest_rsp(QTestState *s, int expected_args)
247 GString *line;
248 gchar **words;
249 int i;
251 redo:
252 line = qtest_recv_line(s);
253 words = g_strsplit(line->str, " ", 0);
254 g_string_free(line, TRUE);
256 if (strcmp(words[0], "IRQ") == 0) {
257 int irq;
259 g_assert(words[1] != NULL);
260 g_assert(words[2] != NULL);
262 irq = strtoul(words[2], NULL, 0);
263 g_assert_cmpint(irq, >=, 0);
264 g_assert_cmpint(irq, <, MAX_IRQ);
266 if (strcmp(words[1], "raise") == 0) {
267 s->irq_level[irq] = true;
268 } else {
269 s->irq_level[irq] = false;
272 g_strfreev(words);
273 goto redo;
276 g_assert(words[0] != NULL);
277 g_assert_cmpstr(words[0], ==, "OK");
279 if (expected_args) {
280 for (i = 0; i < expected_args; i++) {
281 g_assert(words[i] != NULL);
283 } else {
284 g_strfreev(words);
287 return words;
290 void qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
292 bool has_reply = false;
293 int nesting = 0;
295 /* Send QMP request */
296 socket_sendf(s->qmp_fd, fmt, ap);
298 /* Receive reply */
299 while (!has_reply || nesting > 0) {
300 ssize_t len;
301 char c;
303 len = read(s->qmp_fd, &c, 1);
304 if (len == -1 && errno == EINTR) {
305 continue;
308 if (len == -1 || len == 0) {
309 fprintf(stderr, "Broken pipe\n");
310 exit(1);
313 switch (c) {
314 case '{':
315 nesting++;
316 has_reply = true;
317 break;
318 case '}':
319 nesting--;
320 break;
325 void qtest_qmp(QTestState *s, const char *fmt, ...)
327 va_list ap;
329 va_start(ap, fmt);
330 qtest_qmpv(s, fmt, ap);
331 va_end(ap);
334 const char *qtest_get_arch(void)
336 const char *qemu = getenv("QTEST_QEMU_BINARY");
337 const char *end = strrchr(qemu, '/');
339 return end + strlen("/qemu-system-");
342 bool qtest_get_irq(QTestState *s, int num)
344 /* dummy operation in order to make sure irq is up to date */
345 qtest_inb(s, 0);
347 return s->irq_level[num];
350 static int64_t qtest_clock_rsp(QTestState *s)
352 gchar **words;
353 int64_t clock;
354 words = qtest_rsp(s, 2);
355 clock = g_ascii_strtoll(words[1], NULL, 0);
356 g_strfreev(words);
357 return clock;
360 int64_t qtest_clock_step_next(QTestState *s)
362 qtest_sendf(s, "clock_step\n");
363 return qtest_clock_rsp(s);
366 int64_t qtest_clock_step(QTestState *s, int64_t step)
368 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
369 return qtest_clock_rsp(s);
372 int64_t qtest_clock_set(QTestState *s, int64_t val)
374 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
375 return qtest_clock_rsp(s);
378 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
380 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
381 qtest_rsp(s, 0);
384 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
386 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
387 qtest_rsp(s, 0);
390 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
392 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
393 qtest_rsp(s, 0);
396 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
398 qtest_out(s, "outb", addr, value);
401 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
403 qtest_out(s, "outw", addr, value);
406 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
408 qtest_out(s, "outl", addr, value);
411 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
413 gchar **args;
414 uint32_t value;
416 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
417 args = qtest_rsp(s, 2);
418 value = strtoul(args[1], NULL, 0);
419 g_strfreev(args);
421 return value;
424 uint8_t qtest_inb(QTestState *s, uint16_t addr)
426 return qtest_in(s, "inb", addr);
429 uint16_t qtest_inw(QTestState *s, uint16_t addr)
431 return qtest_in(s, "inw", addr);
434 uint32_t qtest_inl(QTestState *s, uint16_t addr)
436 return qtest_in(s, "inl", addr);
439 static void qtest_write(QTestState *s, const char *cmd, uint64_t addr,
440 uint64_t value)
442 qtest_sendf(s, "%s 0x%" PRIx64 " 0x%" PRIx64 "\n", cmd, addr, value);
443 qtest_rsp(s, 0);
446 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value)
448 qtest_write(s, "writeb", addr, value);
451 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value)
453 qtest_write(s, "writew", addr, value);
456 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value)
458 qtest_write(s, "writel", addr, value);
461 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value)
463 qtest_write(s, "writeq", addr, value);
466 static uint64_t qtest_read(QTestState *s, const char *cmd, uint64_t addr)
468 gchar **args;
469 uint64_t value;
471 qtest_sendf(s, "%s 0x%" PRIx64 "\n", cmd, addr);
472 args = qtest_rsp(s, 2);
473 value = strtoull(args[1], NULL, 0);
474 g_strfreev(args);
476 return value;
479 uint8_t qtest_readb(QTestState *s, uint64_t addr)
481 return qtest_read(s, "readb", addr);
484 uint16_t qtest_readw(QTestState *s, uint64_t addr)
486 return qtest_read(s, "readw", addr);
489 uint32_t qtest_readl(QTestState *s, uint64_t addr)
491 return qtest_read(s, "readl", addr);
494 uint64_t qtest_readq(QTestState *s, uint64_t addr)
496 return qtest_read(s, "readq", addr);
499 static int hex2nib(char ch)
501 if (ch >= '0' && ch <= '9') {
502 return ch - '0';
503 } else if (ch >= 'a' && ch <= 'f') {
504 return 10 + (ch - 'a');
505 } else if (ch >= 'A' && ch <= 'F') {
506 return 10 + (ch - 'a');
507 } else {
508 return -1;
512 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
514 uint8_t *ptr = data;
515 gchar **args;
516 size_t i;
518 qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size);
519 args = qtest_rsp(s, 2);
521 for (i = 0; i < size; i++) {
522 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
523 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
526 g_strfreev(args);
529 void qtest_add_func(const char *str, void (*fn))
531 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
532 g_test_add_func(path, fn);
535 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
537 const uint8_t *ptr = data;
538 size_t i;
540 qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size);
541 for (i = 0; i < size; i++) {
542 qtest_sendf(s, "%02x", ptr[i]);
544 qtest_sendf(s, "\n");
545 qtest_rsp(s, 0);