qemu-iotests: Test that "stop" doesn't drain block jobs
[qemu.git] / tests / libqtest.h
blob03469b87817e76e1df87cce3ea5f4ddb5ab4607e
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 #ifndef LIBQTEST_H
18 #define LIBQTEST_H
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <stdarg.h>
24 #include <sys/types.h>
25 #include "qapi/qmp/qdict.h"
26 #include "glib-compat.h"
28 typedef struct QTestState QTestState;
30 extern QTestState *global_qtest;
32 /**
33 * qtest_init:
34 * @extra_args: other arguments to pass to QEMU.
36 * Returns: #QTestState instance.
38 QTestState *qtest_init(const char *extra_args);
40 /**
41 * qtest_quit:
42 * @s: #QTestState instance to operate on.
44 * Shut down the QEMU process associated to @s.
46 void qtest_quit(QTestState *s);
48 /**
49 * qtest_qmp_discard_response:
50 * @s: #QTestState instance to operate on.
51 * @fmt...: QMP message to send to qemu
53 * Sends a QMP message to QEMU and consumes the response.
55 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
57 /**
58 * qtest_qmp:
59 * @s: #QTestState instance to operate on.
60 * @fmt...: QMP message to send to qemu
62 * Sends a QMP message to QEMU and returns the response.
64 QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
66 /**
67 * qtest_qmpv_discard_response:
68 * @s: #QTestState instance to operate on.
69 * @fmt: QMP message to send to QEMU
70 * @ap: QMP message arguments
72 * Sends a QMP message to QEMU and consumes the response.
74 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
76 /**
77 * qtest_qmpv:
78 * @s: #QTestState instance to operate on.
79 * @fmt: QMP message to send to QEMU
80 * @ap: QMP message arguments
82 * Sends a QMP message to QEMU and returns the response.
84 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
86 /**
87 * qtest_receive:
88 * @s: #QTestState instance to operate on.
90 * Reads a QMP message from QEMU and returns the response.
92 QDict *qtest_qmp_receive(QTestState *s);
94 /**
95 * qtest_get_irq:
96 * @s: #QTestState instance to operate on.
97 * @num: Interrupt to observe.
99 * Returns: The level of the @num interrupt.
101 bool qtest_get_irq(QTestState *s, int num);
104 * qtest_irq_intercept_in:
105 * @s: #QTestState instance to operate on.
106 * @string: QOM path of a device.
108 * Associate qtest irqs with the GPIO-in pins of the device
109 * whose path is specified by @string.
111 void qtest_irq_intercept_in(QTestState *s, const char *string);
114 * qtest_irq_intercept_out:
115 * @s: #QTestState instance to operate on.
116 * @string: QOM path of a device.
118 * Associate qtest irqs with the GPIO-out pins of the device
119 * whose path is specified by @string.
121 void qtest_irq_intercept_out(QTestState *s, const char *string);
124 * qtest_outb:
125 * @s: #QTestState instance to operate on.
126 * @addr: I/O port to write to.
127 * @value: Value being written.
129 * Write an 8-bit value to an I/O port.
131 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
134 * qtest_outw:
135 * @s: #QTestState instance to operate on.
136 * @addr: I/O port to write to.
137 * @value: Value being written.
139 * Write a 16-bit value to an I/O port.
141 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
144 * qtest_outl:
145 * @s: #QTestState instance to operate on.
146 * @addr: I/O port to write to.
147 * @value: Value being written.
149 * Write a 32-bit value to an I/O port.
151 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
154 * qtest_inb:
155 * @s: #QTestState instance to operate on.
156 * @addr: I/O port to read from.
158 * Returns an 8-bit value from an I/O port.
160 uint8_t qtest_inb(QTestState *s, uint16_t addr);
163 * qtest_inw:
164 * @s: #QTestState instance to operate on.
165 * @addr: I/O port to read from.
167 * Returns a 16-bit value from an I/O port.
169 uint16_t qtest_inw(QTestState *s, uint16_t addr);
172 * qtest_inl:
173 * @s: #QTestState instance to operate on.
174 * @addr: I/O port to read from.
176 * Returns a 32-bit value from an I/O port.
178 uint32_t qtest_inl(QTestState *s, uint16_t addr);
181 * qtest_writeb:
182 * @s: #QTestState instance to operate on.
183 * @addr: Guest address to write to.
184 * @value: Value being written.
186 * Writes an 8-bit value to memory.
188 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
191 * qtest_writew:
192 * @s: #QTestState instance to operate on.
193 * @addr: Guest address to write to.
194 * @value: Value being written.
196 * Writes a 16-bit value to memory.
198 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
201 * qtest_writel:
202 * @s: #QTestState instance to operate on.
203 * @addr: Guest address to write to.
204 * @value: Value being written.
206 * Writes a 32-bit value to memory.
208 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
211 * qtest_writeq:
212 * @s: #QTestState instance to operate on.
213 * @addr: Guest address to write to.
214 * @value: Value being written.
216 * Writes a 64-bit value to memory.
218 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
221 * qtest_readb:
222 * @s: #QTestState instance to operate on.
223 * @addr: Guest address to read from.
225 * Reads an 8-bit value from memory.
227 * Returns: Value read.
229 uint8_t qtest_readb(QTestState *s, uint64_t addr);
232 * qtest_readw:
233 * @s: #QTestState instance to operate on.
234 * @addr: Guest address to read from.
236 * Reads a 16-bit value from memory.
238 * Returns: Value read.
240 uint16_t qtest_readw(QTestState *s, uint64_t addr);
243 * qtest_readl:
244 * @s: #QTestState instance to operate on.
245 * @addr: Guest address to read from.
247 * Reads a 32-bit value from memory.
249 * Returns: Value read.
251 uint32_t qtest_readl(QTestState *s, uint64_t addr);
254 * qtest_readq:
255 * @s: #QTestState instance to operate on.
256 * @addr: Guest address to read from.
258 * Reads a 64-bit value from memory.
260 * Returns: Value read.
262 uint64_t qtest_readq(QTestState *s, uint64_t addr);
265 * qtest_memread:
266 * @s: #QTestState instance to operate on.
267 * @addr: Guest address to read from.
268 * @data: Pointer to where memory contents will be stored.
269 * @size: Number of bytes to read.
271 * Read guest memory into a buffer.
273 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
276 * qtest_memwrite:
277 * @s: #QTestState instance to operate on.
278 * @addr: Guest address to write to.
279 * @data: Pointer to the bytes that will be written to guest memory.
280 * @size: Number of bytes to write.
282 * Write a buffer to guest memory.
284 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
287 * qtest_memset:
288 * @s: #QTestState instance to operate on.
289 * @addr: Guest address to write to.
290 * @patt: Byte pattern to fill the guest memory region with.
291 * @size: Number of bytes to write.
293 * Write a pattern to guest memory.
295 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
298 * qtest_clock_step_next:
299 * @s: #QTestState instance to operate on.
301 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
303 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
305 int64_t qtest_clock_step_next(QTestState *s);
308 * qtest_clock_step:
309 * @s: QTestState instance to operate on.
310 * @step: Number of nanoseconds to advance the clock by.
312 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
314 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
316 int64_t qtest_clock_step(QTestState *s, int64_t step);
319 * qtest_clock_set:
320 * @s: QTestState instance to operate on.
321 * @val: Nanoseconds value to advance the clock to.
323 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
325 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
327 int64_t qtest_clock_set(QTestState *s, int64_t val);
330 * qtest_get_arch:
332 * Returns: The architecture for the QEMU executable under test.
334 const char *qtest_get_arch(void);
337 * qtest_add_func:
338 * @str: Test case path.
339 * @fn: Test case function
341 * Add a GTester testcase with the given name and function.
342 * The path is prefixed with the architecture under test, as
343 * returned by qtest_get_arch().
345 void qtest_add_func(const char *str, void (*fn));
348 * qtest_add_data_func:
349 * @str: Test case path.
350 * @data: Test case data
351 * @fn: Test case function
353 * Add a GTester testcase with the given name, data and function.
354 * The path is prefixed with the architecture under test, as
355 * returned by qtest_get_arch().
357 void qtest_add_data_func(const char *str, const void *data, void (*fn));
360 * qtest_add:
361 * @testpath: Test case path
362 * @Fixture: Fixture type
363 * @tdata: Test case data
364 * @fsetup: Test case setup function
365 * @ftest: Test case function
366 * @fteardown: Test case teardown function
368 * Add a GTester testcase with the given name, data and functions.
369 * The path is prefixed with the architecture under test, as
370 * returned by qtest_get_arch().
372 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
373 do { \
374 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
375 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
376 g_free(path); \
377 } while (0)
380 * qtest_start:
381 * @args: other arguments to pass to QEMU
383 * Start QEMU and assign the resulting #QTestState to a global variable.
384 * The global variable is used by "shortcut" functions documented below.
386 * Returns: #QTestState instance.
388 static inline QTestState *qtest_start(const char *args)
390 global_qtest = qtest_init(args);
391 return global_qtest;
395 * qtest_end:
397 * Shut down the QEMU process started by qtest_start().
399 static inline void qtest_end(void)
401 qtest_quit(global_qtest);
402 global_qtest = NULL;
406 * qmp:
407 * @fmt...: QMP message to send to qemu
409 * Sends a QMP message to QEMU and returns the response.
411 QDict *qmp(const char *fmt, ...);
414 * qmp_discard_response:
415 * @fmt...: QMP message to send to qemu
417 * Sends a QMP message to QEMU and consumes the response.
419 void qmp_discard_response(const char *fmt, ...);
422 * qmp_receive:
424 * Reads a QMP message from QEMU and returns the response.
426 static inline QDict *qmp_receive(void)
428 return qtest_qmp_receive(global_qtest);
432 * get_irq:
433 * @num: Interrupt to observe.
435 * Returns: The level of the @num interrupt.
437 static inline bool get_irq(int num)
439 return qtest_get_irq(global_qtest, num);
443 * irq_intercept_in:
444 * @string: QOM path of a device.
446 * Associate qtest irqs with the GPIO-in pins of the device
447 * whose path is specified by @string.
449 static inline void irq_intercept_in(const char *string)
451 qtest_irq_intercept_in(global_qtest, string);
455 * qtest_irq_intercept_out:
456 * @string: QOM path of a device.
458 * Associate qtest irqs with the GPIO-out pins of the device
459 * whose path is specified by @string.
461 static inline void irq_intercept_out(const char *string)
463 qtest_irq_intercept_out(global_qtest, string);
467 * outb:
468 * @addr: I/O port to write to.
469 * @value: Value being written.
471 * Write an 8-bit value to an I/O port.
473 static inline void outb(uint16_t addr, uint8_t value)
475 qtest_outb(global_qtest, addr, value);
479 * outw:
480 * @addr: I/O port to write to.
481 * @value: Value being written.
483 * Write a 16-bit value to an I/O port.
485 static inline void outw(uint16_t addr, uint16_t value)
487 qtest_outw(global_qtest, addr, value);
491 * outl:
492 * @addr: I/O port to write to.
493 * @value: Value being written.
495 * Write a 32-bit value to an I/O port.
497 static inline void outl(uint16_t addr, uint32_t value)
499 qtest_outl(global_qtest, addr, value);
503 * inb:
504 * @addr: I/O port to read from.
506 * Reads an 8-bit value from an I/O port.
508 * Returns: Value read.
510 static inline uint8_t inb(uint16_t addr)
512 return qtest_inb(global_qtest, addr);
516 * inw:
517 * @addr: I/O port to read from.
519 * Reads a 16-bit value from an I/O port.
521 * Returns: Value read.
523 static inline uint16_t inw(uint16_t addr)
525 return qtest_inw(global_qtest, addr);
529 * inl:
530 * @addr: I/O port to read from.
532 * Reads a 32-bit value from an I/O port.
534 * Returns: Value read.
536 static inline uint32_t inl(uint16_t addr)
538 return qtest_inl(global_qtest, addr);
542 * writeb:
543 * @addr: Guest address to write to.
544 * @value: Value being written.
546 * Writes an 8-bit value to guest memory.
548 static inline void writeb(uint64_t addr, uint8_t value)
550 qtest_writeb(global_qtest, addr, value);
554 * writew:
555 * @addr: Guest address to write to.
556 * @value: Value being written.
558 * Writes a 16-bit value to guest memory.
560 static inline void writew(uint64_t addr, uint16_t value)
562 qtest_writew(global_qtest, addr, value);
566 * writel:
567 * @addr: Guest address to write to.
568 * @value: Value being written.
570 * Writes a 32-bit value to guest memory.
572 static inline void writel(uint64_t addr, uint32_t value)
574 qtest_writel(global_qtest, addr, value);
578 * writeq:
579 * @addr: Guest address to write to.
580 * @value: Value being written.
582 * Writes a 64-bit value to guest memory.
584 static inline void writeq(uint64_t addr, uint64_t value)
586 qtest_writeq(global_qtest, addr, value);
590 * readb:
591 * @addr: Guest address to read from.
593 * Reads an 8-bit value from guest memory.
595 * Returns: Value read.
597 static inline uint8_t readb(uint64_t addr)
599 return qtest_readb(global_qtest, addr);
603 * readw:
604 * @addr: Guest address to read from.
606 * Reads a 16-bit value from guest memory.
608 * Returns: Value read.
610 static inline uint16_t readw(uint64_t addr)
612 return qtest_readw(global_qtest, addr);
616 * readl:
617 * @addr: Guest address to read from.
619 * Reads a 32-bit value from guest memory.
621 * Returns: Value read.
623 static inline uint32_t readl(uint64_t addr)
625 return qtest_readl(global_qtest, addr);
629 * readq:
630 * @addr: Guest address to read from.
632 * Reads a 64-bit value from guest memory.
634 * Returns: Value read.
636 static inline uint64_t readq(uint64_t addr)
638 return qtest_readq(global_qtest, addr);
642 * memread:
643 * @addr: Guest address to read from.
644 * @data: Pointer to where memory contents will be stored.
645 * @size: Number of bytes to read.
647 * Read guest memory into a buffer.
649 static inline void memread(uint64_t addr, void *data, size_t size)
651 qtest_memread(global_qtest, addr, data, size);
655 * memwrite:
656 * @addr: Guest address to write to.
657 * @data: Pointer to the bytes that will be written to guest memory.
658 * @size: Number of bytes to write.
660 * Write a buffer to guest memory.
662 static inline void memwrite(uint64_t addr, const void *data, size_t size)
664 qtest_memwrite(global_qtest, addr, data, size);
668 * qmemset:
669 * @addr: Guest address to write to.
670 * @patt: Byte pattern to fill the guest memory region with.
671 * @size: Number of bytes to write.
673 * Write a pattern to guest memory.
675 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
677 qtest_memset(global_qtest, addr, patt, size);
681 * clock_step_next:
683 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
685 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
687 static inline int64_t clock_step_next(void)
689 return qtest_clock_step_next(global_qtest);
693 * clock_step:
694 * @step: Number of nanoseconds to advance the clock by.
696 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
698 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
700 static inline int64_t clock_step(int64_t step)
702 return qtest_clock_step(global_qtest, step);
706 * clock_set:
707 * @val: Nanoseconds value to advance the clock to.
709 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
711 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
713 static inline int64_t clock_set(int64_t val)
715 return qtest_clock_set(global_qtest, val);
719 * qtest_big_endian:
721 * Returns: True if the architecture under test has a big endian configuration.
723 bool qtest_big_endian(void);
725 #endif