root / tests / libqtest.c @ 1de7afc9
History | View | Annotate | Download (10.2 kB)
1 |
/*
|
---|---|
2 |
* QTest
|
3 |
*
|
4 |
* Copyright IBM, Corp. 2012
|
5 |
* Copyright Red Hat, Inc. 2012
|
6 |
*
|
7 |
* Authors:
|
8 |
* Anthony Liguori <aliguori@us.ibm.com>
|
9 |
* Paolo Bonzini <pbonzini@redhat.com>
|
10 |
*
|
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.
|
13 |
*
|
14 |
*/
|
15 |
#include "libqtest.h" |
16 |
|
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> |
28 |
|
29 |
#include "qemu/compiler.h" |
30 |
#include "qemu/osdep.h" |
31 |
|
32 |
#define MAX_IRQ 256 |
33 |
|
34 |
QTestState *global_qtest; |
35 |
|
36 |
struct QTestState
|
37 |
{ |
38 |
int fd;
|
39 |
int qmp_fd;
|
40 |
bool irq_level[MAX_IRQ];
|
41 |
GString *rx; |
42 |
gchar *pid_file; |
43 |
char *socket_path, *qmp_socket_path;
|
44 |
}; |
45 |
|
46 |
#define g_assert_no_errno(ret) do { \ |
47 |
g_assert_cmpint(ret, !=, -1); \
|
48 |
} while (0) |
49 |
|
50 |
static int init_socket(const char *socket_path) |
51 |
{ |
52 |
struct sockaddr_un addr;
|
53 |
int sock;
|
54 |
int ret;
|
55 |
|
56 |
sock = socket(PF_UNIX, SOCK_STREAM, 0);
|
57 |
g_assert_no_errno(sock); |
58 |
|
59 |
addr.sun_family = AF_UNIX; |
60 |
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", socket_path); |
61 |
qemu_set_cloexec(sock); |
62 |
|
63 |
do {
|
64 |
ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr)); |
65 |
} while (ret == -1 && errno == EINTR); |
66 |
g_assert_no_errno(ret); |
67 |
listen(sock, 1);
|
68 |
|
69 |
return sock;
|
70 |
} |
71 |
|
72 |
static int socket_accept(int sock) |
73 |
{ |
74 |
struct sockaddr_un addr;
|
75 |
socklen_t addrlen; |
76 |
int ret;
|
77 |
|
78 |
addrlen = sizeof(addr);
|
79 |
do {
|
80 |
ret = accept(sock, (struct sockaddr *)&addr, &addrlen);
|
81 |
} while (ret == -1 && errno == EINTR); |
82 |
g_assert_no_errno(ret); |
83 |
close(sock); |
84 |
|
85 |
return ret;
|
86 |
} |
87 |
|
88 |
static pid_t qtest_qemu_pid(QTestState *s)
|
89 |
{ |
90 |
FILE *f; |
91 |
char buffer[1024]; |
92 |
pid_t pid = -1;
|
93 |
|
94 |
f = fopen(s->pid_file, "r");
|
95 |
if (f) {
|
96 |
if (fgets(buffer, sizeof(buffer), f)) { |
97 |
pid = atoi(buffer); |
98 |
} |
99 |
} |
100 |
fclose(f); |
101 |
return pid;
|
102 |
} |
103 |
|
104 |
QTestState *qtest_init(const char *extra_args) |
105 |
{ |
106 |
QTestState *s; |
107 |
int sock, qmpsock, ret, i;
|
108 |
gchar *pid_file; |
109 |
gchar *command; |
110 |
const char *qemu_binary; |
111 |
pid_t pid; |
112 |
|
113 |
qemu_binary = getenv("QTEST_QEMU_BINARY");
|
114 |
g_assert(qemu_binary != NULL);
|
115 |
|
116 |
s = g_malloc(sizeof(*s));
|
117 |
|
118 |
s->socket_path = g_strdup_printf("/tmp/qtest-%d.sock", getpid());
|
119 |
s->qmp_socket_path = g_strdup_printf("/tmp/qtest-%d.qmp", getpid());
|
120 |
pid_file = g_strdup_printf("/tmp/qtest-%d.pid", getpid());
|
121 |
|
122 |
sock = init_socket(s->socket_path); |
123 |
qmpsock = init_socket(s->qmp_socket_path); |
124 |
|
125 |
pid = fork(); |
126 |
if (pid == 0) { |
127 |
command = g_strdup_printf("%s "
|
128 |
"-qtest unix:%s,nowait "
|
129 |
"-qtest-log /dev/null "
|
130 |
"-qmp unix:%s,nowait "
|
131 |
"-pidfile %s "
|
132 |
"-machine accel=qtest "
|
133 |
"%s", qemu_binary, s->socket_path,
|
134 |
s->qmp_socket_path, pid_file, |
135 |
extra_args ?: "");
|
136 |
|
137 |
ret = system(command); |
138 |
exit(ret); |
139 |
g_free(command); |
140 |
} |
141 |
|
142 |
s->fd = socket_accept(sock); |
143 |
s->qmp_fd = socket_accept(qmpsock); |
144 |
|
145 |
s->rx = g_string_new("");
|
146 |
s->pid_file = pid_file; |
147 |
for (i = 0; i < MAX_IRQ; i++) { |
148 |
s->irq_level[i] = false;
|
149 |
} |
150 |
|
151 |
/* Read the QMP greeting and then do the handshake */
|
152 |
qtest_qmp(s, "");
|
153 |
qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }");
|
154 |
|
155 |
if (getenv("QTEST_STOP")) { |
156 |
kill(qtest_qemu_pid(s), SIGSTOP); |
157 |
} |
158 |
|
159 |
return s;
|
160 |
} |
161 |
|
162 |
void qtest_quit(QTestState *s)
|
163 |
{ |
164 |
int status;
|
165 |
|
166 |
pid_t pid = qtest_qemu_pid(s); |
167 |
if (pid != -1) { |
168 |
kill(pid, SIGTERM); |
169 |
waitpid(pid, &status, 0);
|
170 |
} |
171 |
|
172 |
unlink(s->pid_file); |
173 |
unlink(s->socket_path); |
174 |
unlink(s->qmp_socket_path); |
175 |
g_free(s->pid_file); |
176 |
g_free(s->socket_path); |
177 |
g_free(s->qmp_socket_path); |
178 |
} |
179 |
|
180 |
static void socket_sendf(int fd, const char *fmt, va_list ap) |
181 |
{ |
182 |
gchar *str; |
183 |
size_t size, offset; |
184 |
|
185 |
str = g_strdup_vprintf(fmt, ap); |
186 |
size = strlen(str); |
187 |
|
188 |
offset = 0;
|
189 |
while (offset < size) {
|
190 |
ssize_t len; |
191 |
|
192 |
len = write(fd, str + offset, size - offset); |
193 |
if (len == -1 && errno == EINTR) { |
194 |
continue;
|
195 |
} |
196 |
|
197 |
g_assert_no_errno(len); |
198 |
g_assert_cmpint(len, >, 0);
|
199 |
|
200 |
offset += len; |
201 |
} |
202 |
} |
203 |
|
204 |
static void GCC_FMT_ATTR(2, 3) qtest_sendf(QTestState *s, const char *fmt, ...) |
205 |
{ |
206 |
va_list ap; |
207 |
|
208 |
va_start(ap, fmt); |
209 |
socket_sendf(s->fd, fmt, ap); |
210 |
va_end(ap); |
211 |
} |
212 |
|
213 |
static GString *qtest_recv_line(QTestState *s)
|
214 |
{ |
215 |
GString *line; |
216 |
size_t offset; |
217 |
char *eol;
|
218 |
|
219 |
while ((eol = strchr(s->rx->str, '\n')) == NULL) { |
220 |
ssize_t len; |
221 |
char buffer[1024]; |
222 |
|
223 |
len = read(s->fd, buffer, sizeof(buffer));
|
224 |
if (len == -1 && errno == EINTR) { |
225 |
continue;
|
226 |
} |
227 |
|
228 |
if (len == -1 || len == 0) { |
229 |
fprintf(stderr, "Broken pipe\n");
|
230 |
exit(1);
|
231 |
} |
232 |
|
233 |
g_string_append_len(s->rx, buffer, len); |
234 |
} |
235 |
|
236 |
offset = eol - s->rx->str; |
237 |
line = g_string_new_len(s->rx->str, offset); |
238 |
g_string_erase(s->rx, 0, offset + 1); |
239 |
|
240 |
return line;
|
241 |
} |
242 |
|
243 |
static gchar **qtest_rsp(QTestState *s, int expected_args) |
244 |
{ |
245 |
GString *line; |
246 |
gchar **words; |
247 |
int i;
|
248 |
|
249 |
redo:
|
250 |
line = qtest_recv_line(s); |
251 |
words = g_strsplit(line->str, " ", 0); |
252 |
g_string_free(line, TRUE); |
253 |
|
254 |
if (strcmp(words[0], "IRQ") == 0) { |
255 |
int irq;
|
256 |
|
257 |
g_assert(words[1] != NULL); |
258 |
g_assert(words[2] != NULL); |
259 |
|
260 |
irq = strtoul(words[2], NULL, 0); |
261 |
g_assert_cmpint(irq, >=, 0);
|
262 |
g_assert_cmpint(irq, <, MAX_IRQ); |
263 |
|
264 |
if (strcmp(words[1], "raise") == 0) { |
265 |
s->irq_level[irq] = true;
|
266 |
} else {
|
267 |
s->irq_level[irq] = false;
|
268 |
} |
269 |
|
270 |
g_strfreev(words); |
271 |
goto redo;
|
272 |
} |
273 |
|
274 |
g_assert(words[0] != NULL); |
275 |
g_assert_cmpstr(words[0], ==, "OK"); |
276 |
|
277 |
if (expected_args) {
|
278 |
for (i = 0; i < expected_args; i++) { |
279 |
g_assert(words[i] != NULL);
|
280 |
} |
281 |
} else {
|
282 |
g_strfreev(words); |
283 |
} |
284 |
|
285 |
return words;
|
286 |
} |
287 |
|
288 |
void qtest_qmp(QTestState *s, const char *fmt, ...) |
289 |
{ |
290 |
va_list ap; |
291 |
bool has_reply = false; |
292 |
int nesting = 0; |
293 |
|
294 |
/* Send QMP request */
|
295 |
va_start(ap, fmt); |
296 |
socket_sendf(s->qmp_fd, fmt, ap); |
297 |
va_end(ap); |
298 |
|
299 |
/* Receive reply */
|
300 |
while (!has_reply || nesting > 0) { |
301 |
ssize_t len; |
302 |
char c;
|
303 |
|
304 |
len = read(s->qmp_fd, &c, 1);
|
305 |
if (len == -1 && errno == EINTR) { |
306 |
continue;
|
307 |
} |
308 |
|
309 |
if (len == -1 || len == 0) { |
310 |
fprintf(stderr, "Broken pipe\n");
|
311 |
exit(1);
|
312 |
} |
313 |
|
314 |
switch (c) {
|
315 |
case '{': |
316 |
nesting++; |
317 |
has_reply = true;
|
318 |
break;
|
319 |
case '}': |
320 |
nesting--; |
321 |
break;
|
322 |
} |
323 |
} |
324 |
} |
325 |
|
326 |
const char *qtest_get_arch(void) |
327 |
{ |
328 |
const char *qemu = getenv("QTEST_QEMU_BINARY"); |
329 |
const char *end = strrchr(qemu, '/'); |
330 |
|
331 |
return end + strlen("/qemu-system-"); |
332 |
} |
333 |
|
334 |
bool qtest_get_irq(QTestState *s, int num) |
335 |
{ |
336 |
/* dummy operation in order to make sure irq is up to date */
|
337 |
qtest_inb(s, 0);
|
338 |
|
339 |
return s->irq_level[num];
|
340 |
} |
341 |
|
342 |
static int64_t qtest_clock_rsp(QTestState *s)
|
343 |
{ |
344 |
gchar **words; |
345 |
int64_t clock; |
346 |
words = qtest_rsp(s, 2);
|
347 |
clock = g_ascii_strtoll(words[1], NULL, 0); |
348 |
g_strfreev(words); |
349 |
return clock;
|
350 |
} |
351 |
|
352 |
int64_t qtest_clock_step_next(QTestState *s) |
353 |
{ |
354 |
qtest_sendf(s, "clock_step\n");
|
355 |
return qtest_clock_rsp(s);
|
356 |
} |
357 |
|
358 |
int64_t qtest_clock_step(QTestState *s, int64_t step) |
359 |
{ |
360 |
qtest_sendf(s, "clock_step %"PRIi64"\n", step); |
361 |
return qtest_clock_rsp(s);
|
362 |
} |
363 |
|
364 |
int64_t qtest_clock_set(QTestState *s, int64_t val) |
365 |
{ |
366 |
qtest_sendf(s, "clock_set %"PRIi64"\n", val); |
367 |
return qtest_clock_rsp(s);
|
368 |
} |
369 |
|
370 |
void qtest_irq_intercept_out(QTestState *s, const char *qom_path) |
371 |
{ |
372 |
qtest_sendf(s, "irq_intercept_out %s\n", qom_path);
|
373 |
qtest_rsp(s, 0);
|
374 |
} |
375 |
|
376 |
void qtest_irq_intercept_in(QTestState *s, const char *qom_path) |
377 |
{ |
378 |
qtest_sendf(s, "irq_intercept_in %s\n", qom_path);
|
379 |
qtest_rsp(s, 0);
|
380 |
} |
381 |
|
382 |
static void qtest_out(QTestState *s, const char *cmd, uint16_t addr, uint32_t value) |
383 |
{ |
384 |
qtest_sendf(s, "%s 0x%x 0x%x\n", cmd, addr, value);
|
385 |
qtest_rsp(s, 0);
|
386 |
} |
387 |
|
388 |
void qtest_outb(QTestState *s, uint16_t addr, uint8_t value)
|
389 |
{ |
390 |
qtest_out(s, "outb", addr, value);
|
391 |
} |
392 |
|
393 |
void qtest_outw(QTestState *s, uint16_t addr, uint16_t value)
|
394 |
{ |
395 |
qtest_out(s, "outw", addr, value);
|
396 |
} |
397 |
|
398 |
void qtest_outl(QTestState *s, uint16_t addr, uint32_t value)
|
399 |
{ |
400 |
qtest_out(s, "outl", addr, value);
|
401 |
} |
402 |
|
403 |
static uint32_t qtest_in(QTestState *s, const char *cmd, uint16_t addr) |
404 |
{ |
405 |
gchar **args; |
406 |
uint32_t value; |
407 |
|
408 |
qtest_sendf(s, "%s 0x%x\n", cmd, addr);
|
409 |
args = qtest_rsp(s, 2);
|
410 |
value = strtoul(args[1], NULL, 0); |
411 |
g_strfreev(args); |
412 |
|
413 |
return value;
|
414 |
} |
415 |
|
416 |
uint8_t qtest_inb(QTestState *s, uint16_t addr) |
417 |
{ |
418 |
return qtest_in(s, "inb", addr); |
419 |
} |
420 |
|
421 |
uint16_t qtest_inw(QTestState *s, uint16_t addr) |
422 |
{ |
423 |
return qtest_in(s, "inw", addr); |
424 |
} |
425 |
|
426 |
uint32_t qtest_inl(QTestState *s, uint16_t addr) |
427 |
{ |
428 |
return qtest_in(s, "inl", addr); |
429 |
} |
430 |
|
431 |
static int hex2nib(char ch) |
432 |
{ |
433 |
if (ch >= '0' && ch <= '9') { |
434 |
return ch - '0'; |
435 |
} else if (ch >= 'a' && ch <= 'f') { |
436 |
return 10 + (ch - 'a'); |
437 |
} else if (ch >= 'A' && ch <= 'F') { |
438 |
return 10 + (ch - 'a'); |
439 |
} else {
|
440 |
return -1; |
441 |
} |
442 |
} |
443 |
|
444 |
void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size) |
445 |
{ |
446 |
uint8_t *ptr = data; |
447 |
gchar **args; |
448 |
size_t i; |
449 |
|
450 |
qtest_sendf(s, "read 0x%" PRIx64 " 0x%zx\n", addr, size); |
451 |
args = qtest_rsp(s, 2);
|
452 |
|
453 |
for (i = 0; i < size; i++) { |
454 |
ptr[i] = hex2nib(args[1][2 + (i * 2)]) << 4; |
455 |
ptr[i] |= hex2nib(args[1][2 + (i * 2) + 1]); |
456 |
} |
457 |
|
458 |
g_strfreev(args); |
459 |
} |
460 |
|
461 |
void qtest_add_func(const char *str, void (*fn)) |
462 |
{ |
463 |
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
|
464 |
g_test_add_func(path, fn); |
465 |
} |
466 |
|
467 |
void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size) |
468 |
{ |
469 |
const uint8_t *ptr = data;
|
470 |
size_t i; |
471 |
|
472 |
qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x", addr, size); |
473 |
for (i = 0; i < size; i++) { |
474 |
qtest_sendf(s, "%02x", ptr[i]);
|
475 |
} |
476 |
qtest_sendf(s, "\n");
|
477 |
qtest_rsp(s, 0);
|
478 |
} |