Kick io-thread on qemu_chr_accept_input
[qemu/ar7.git] / tests / libqtest.c
blobd47969eae51a71092aaa957d19d978b5f48d0aff
1 /*
2 * QTest
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Paolo Bonzini <pbonzini@redhat.com>
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
15 #include "libqtest.h"
17 #include <glib.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/wait.h>
21 #include <sys/un.h>
22 #include <inttypes.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
29 #include "osdep.h"
31 #define MAX_IRQ 256
33 QTestState *global_qtest;
35 struct QTestState
37 int fd;
38 bool irq_level[MAX_IRQ];
39 GString *rx;
40 gchar *pid_file;
43 #define g_assert_no_errno(ret) do { \
44 g_assert_cmpint(ret, !=, -1); \
45 } while (0)
47 QTestState *qtest_init(const char *extra_args)
49 QTestState *s;
50 struct sockaddr_un addr;
51 int sock, ret, i;
52 gchar *socket_path;
53 gchar *pid_file;
54 gchar *command;
55 const char *qemu_binary;
56 pid_t pid;
57 socklen_t addrlen;
59 qemu_binary = getenv("QTEST_QEMU_BINARY");
60 g_assert(qemu_binary != NULL);
62 socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
63 pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
65 s = g_malloc(sizeof(*s));
67 sock = socket(PF_UNIX, SOCK_STREAM, 0);
68 g_assert_no_errno(sock);
70 addr.sun_family = AF_UNIX;
71 snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path);
72 qemu_set_cloexec(sock);
74 do {
75 ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
76 } while (ret == -1 && errno == EINTR);
77 g_assert_no_errno(ret);
78 listen(sock, 1);
80 pid = fork();
81 if (pid == 0) {
82 command = g_strdup_printf("%s "
83 "-qtest unix:%s,nowait "
84 "-qtest-log /dev/null "
85 "-pidfile %s "
86 "-machine accel=qtest "
87 "%s", qemu_binary, socket_path,
88 pid_file,
89 extra_args ?: "");
91 ret = system(command);
92 exit(ret);
93 g_free(command);
96 do {
97 ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
98 } while (ret == -1 && errno == EINTR);
99 g_assert_no_errno(ret);
100 close(sock);
102 s->fd = ret;
103 s->rx = g_string_new("");
104 s->pid_file = pid_file;
105 for (i = 0; i < MAX_IRQ; i++) {
106 s->irq_level[i] = false;
109 g_free(socket_path);
111 return s;
114 void qtest_quit(QTestState *s)
116 FILE *f;
117 char buffer[1024];
119 f = fopen(s->pid_file, "r");
120 if (f) {
121 if (fgets(buffer, sizeof(buffer), f)) {
122 pid_t pid = atoi(buffer);
123 int status = 0;
125 kill(pid, SIGTERM);
126 waitpid(pid, &status, 0);
129 fclose(f);
133 static void qtest_sendf(QTestState *s, const char *fmt, ...)
135 va_list ap;
136 gchar *str;
137 size_t size, offset;
139 va_start(ap, fmt);
140 str = g_strdup_vprintf(fmt, ap);
141 va_end(ap);
142 size = strlen(str);
144 offset = 0;
145 while (offset < size) {
146 ssize_t len;
148 len = write(s->fd, str + offset, size - offset);
149 if (len == -1 && errno == EINTR) {
150 continue;
153 g_assert_no_errno(len);
154 g_assert_cmpint(len, >, 0);
156 offset += len;
160 static GString *qtest_recv_line(QTestState *s)
162 GString *line;
163 size_t offset;
164 char *eol;
166 while ((eol = strchr(s->rx->str, '\n')) == NULL) {
167 ssize_t len;
168 char buffer[1024];
170 len = read(s->fd, buffer, sizeof(buffer));
171 if (len == -1 && errno == EINTR) {
172 continue;
175 if (len == -1 || len == 0) {
176 fprintf(stderr, "Broken pipe\n");
177 exit(1);
180 g_string_append_len(s->rx, buffer, len);
183 offset = eol - s->rx->str;
184 line = g_string_new_len(s->rx->str, offset);
185 g_string_erase(s->rx, 0, offset + 1);
187 return line;
190 static gchar **qtest_rsp(QTestState *s, int expected_args)
192 GString *line;
193 gchar **words;
194 int i;
196 redo:
197 line = qtest_recv_line(s);
198 words = g_strsplit(line->str, " ", 0);
199 g_string_free(line, TRUE);
201 if (strcmp(words[0], "IRQ") == 0) {
202 int irq;
204 g_assert(words[1] != NULL);
205 g_assert(words[2] != NULL);
207 irq = strtoul(words[2], NULL, 0);
208 g_assert_cmpint(irq, >=, 0);
209 g_assert_cmpint(irq, <, MAX_IRQ);
211 if (strcmp(words[1], "raise") == 0) {
212 s->irq_level[irq] = true;
213 } else {
214 s->irq_level[irq] = false;
217 g_strfreev(words);
218 goto redo;
221 g_assert(words[0] != NULL);
222 g_assert_cmpstr(words[0], ==, "OK");
224 if (expected_args) {
225 for (i = 0; i < expected_args; i++) {
226 g_assert(words[i] != NULL);
228 } else {
229 g_strfreev(words);
232 return words;
235 const char *qtest_get_arch(void)
237 const char *qemu = getenv("QTEST_QEMU_BINARY");
238 const char *end = strrchr(qemu, '/');
240 return end + strlen("/qemu-system-");
243 bool qtest_get_irq(QTestState *s, int num)
245 /* dummy operation in order to make sure irq is up to date */
246 qtest_inb(s, 0);
248 return s->irq_level[num];
251 static int64_t qtest_clock_rsp(QTestState *s)
253 gchar **words;
254 int64_t clock;
255 words = qtest_rsp(s, 2);
256 clock = g_ascii_strtoll(words[1], NULL, 0);
257 g_strfreev(words);
258 return clock;
261 int64_t qtest_clock_step_next(QTestState *s)
263 qtest_sendf(s, "clock_step\n");
264 return qtest_clock_rsp(s);
267 int64_t qtest_clock_step(QTestState *s, int64_t step)
269 qtest_sendf(s, "clock_step %"PRIi64"\n", step);
270 return qtest_clock_rsp(s);
273 int64_t qtest_clock_set(QTestState *s, int64_t val)
275 qtest_sendf(s, "clock_set %"PRIi64"\n", val);
276 return qtest_clock_rsp(s);
279 void qtest_irq_intercept_out(QTestState *s, const char *qom_path)
281 qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
282 qtest_rsp(s, 0);
285 void qtest_irq_intercept_in(QTestState *s, const char *qom_path)
287 qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
288 qtest_rsp(s, 0);
291 static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value)
293 qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
294 qtest_rsp(s, 0);
297 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
299 qtest_out(s, "outb", addr, value);
302 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
304 qtest_out(s, "outw", addr, value);
307 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
309 qtest_out(s, "outl", addr, value);
312 static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr)
314 gchar **args;
315 uint32_t value;
317 qtest_sendf(s, "%s 0x%x\n", cmd, addr);
318 args = qtest_rsp(s, 2);
319 value = strtoul(args[1], NULL, 0);
320 g_strfreev(args);
322 return value;
325 uint8_t qtest_inb(QTestState *s, uint16_t addr)
327 return qtest_in(s, "inb", addr);
330 uint16_t qtest_inw(QTestState *s, uint16_t addr)
332 return qtest_in(s, "inw", addr);
335 uint32_t qtest_inl(QTestState *s, uint16_t addr)
337 return qtest_in(s, "inl", addr);
340 static int hex2nib(char ch)
342 if (ch >= '0' && ch <= '9') {
343 return ch - '0';
344 } else if (ch >= 'a' && ch <= 'f') {
345 return 10 + (ch - 'a');
346 } else if (ch >= 'A' && ch <= 'F') {
347 return 10 + (ch - 'a');
348 } else {
349 return -1;
353 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size)
355 uint8_t *ptr = data;
356 gchar **args;
357 size_t i;
359 qtest_sendf(s, "read 0x%x 0x%x\n", addr, size);
360 args = qtest_rsp(s, 2);
362 for (i = 0; i < size; i++) {
363 ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4;
364 ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]);
367 g_strfreev(args);
370 void qtest_add_func(const char *str, void (*fn))
372 gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
373 g_test_add_func(path, fn);
376 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
378 const uint8_t *ptr = data;
379 size_t i;
381 qtest_sendf(s, "write 0x%x 0x%x 0x", addr, size);
382 for (i = 0; i < size; i++) {
383 qtest_sendf(s, "%02x", ptr[i]);
385 qtest_sendf(s, "\n");
386 qtest_rsp(s, 0);