libqtest: New hmp() & friends
[qemu.git] / tests / libqtest.h
blob55bccbf0e68c7ec2a112f4c0f9232785e7b366cb
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_async_qmp:
68 * @s: #QTestState instance to operate on.
69 * @fmt...: QMP message to send to qemu
71 * Sends a QMP message to QEMU and leaves the response in the stream.
73 void qtest_async_qmp(QTestState *s, const char *fmt, ...);
75 /**
76 * qtest_qmpv_discard_response:
77 * @s: #QTestState instance to operate on.
78 * @fmt: QMP message to send to QEMU
79 * @ap: QMP message arguments
81 * Sends a QMP message to QEMU and consumes the response.
83 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
85 /**
86 * qtest_qmpv:
87 * @s: #QTestState instance to operate on.
88 * @fmt: QMP message to send to QEMU
89 * @ap: QMP message arguments
91 * Sends a QMP message to QEMU and returns the response.
93 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
95 /**
96 * qtest_async_qmpv:
97 * @s: #QTestState instance to operate on.
98 * @fmt: QMP message to send to QEMU
99 * @ap: QMP message arguments
101 * Sends a QMP message to QEMU and leaves the response in the stream.
103 void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap);
106 * qtest_receive:
107 * @s: #QTestState instance to operate on.
109 * Reads a QMP message from QEMU and returns the response.
111 QDict *qtest_qmp_receive(QTestState *s);
114 * qtest_qmp_eventwait:
115 * @s: #QTestState instance to operate on.
116 * @s: #event event to wait for.
118 * Continuosly polls for QMP responses until it receives the desired event.
120 void qtest_qmp_eventwait(QTestState *s, const char *event);
123 * qtest_hmpv:
124 * @s: #QTestState instance to operate on.
125 * @fmt...: HMP command to send to QEMU
127 * Send HMP command to QEMU via QMP's human-monitor-command.
129 * Returns: the command's output. The caller should g_free() it.
131 char *qtest_hmp(QTestState *s, const char *fmt, ...);
134 * qtest_hmpv:
135 * @s: #QTestState instance to operate on.
136 * @fmt: HMP command to send to QEMU
137 * @ap: HMP command arguments
139 * Send HMP command to QEMU via QMP's human-monitor-command.
141 * Returns: the command's output. The caller should g_free() it.
143 char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);
146 * qtest_get_irq:
147 * @s: #QTestState instance to operate on.
148 * @num: Interrupt to observe.
150 * Returns: The level of the @num interrupt.
152 bool qtest_get_irq(QTestState *s, int num);
155 * qtest_irq_intercept_in:
156 * @s: #QTestState instance to operate on.
157 * @string: QOM path of a device.
159 * Associate qtest irqs with the GPIO-in pins of the device
160 * whose path is specified by @string.
162 void qtest_irq_intercept_in(QTestState *s, const char *string);
165 * qtest_irq_intercept_out:
166 * @s: #QTestState instance to operate on.
167 * @string: QOM path of a device.
169 * Associate qtest irqs with the GPIO-out pins of the device
170 * whose path is specified by @string.
172 void qtest_irq_intercept_out(QTestState *s, const char *string);
175 * qtest_outb:
176 * @s: #QTestState instance to operate on.
177 * @addr: I/O port to write to.
178 * @value: Value being written.
180 * Write an 8-bit value to an I/O port.
182 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
185 * qtest_outw:
186 * @s: #QTestState instance to operate on.
187 * @addr: I/O port to write to.
188 * @value: Value being written.
190 * Write a 16-bit value to an I/O port.
192 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
195 * qtest_outl:
196 * @s: #QTestState instance to operate on.
197 * @addr: I/O port to write to.
198 * @value: Value being written.
200 * Write a 32-bit value to an I/O port.
202 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
205 * qtest_inb:
206 * @s: #QTestState instance to operate on.
207 * @addr: I/O port to read from.
209 * Returns an 8-bit value from an I/O port.
211 uint8_t qtest_inb(QTestState *s, uint16_t addr);
214 * qtest_inw:
215 * @s: #QTestState instance to operate on.
216 * @addr: I/O port to read from.
218 * Returns a 16-bit value from an I/O port.
220 uint16_t qtest_inw(QTestState *s, uint16_t addr);
223 * qtest_inl:
224 * @s: #QTestState instance to operate on.
225 * @addr: I/O port to read from.
227 * Returns a 32-bit value from an I/O port.
229 uint32_t qtest_inl(QTestState *s, uint16_t addr);
232 * qtest_writeb:
233 * @s: #QTestState instance to operate on.
234 * @addr: Guest address to write to.
235 * @value: Value being written.
237 * Writes an 8-bit value to memory.
239 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
242 * qtest_writew:
243 * @s: #QTestState instance to operate on.
244 * @addr: Guest address to write to.
245 * @value: Value being written.
247 * Writes a 16-bit value to memory.
249 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
252 * qtest_writel:
253 * @s: #QTestState instance to operate on.
254 * @addr: Guest address to write to.
255 * @value: Value being written.
257 * Writes a 32-bit value to memory.
259 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
262 * qtest_writeq:
263 * @s: #QTestState instance to operate on.
264 * @addr: Guest address to write to.
265 * @value: Value being written.
267 * Writes a 64-bit value to memory.
269 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
272 * qtest_readb:
273 * @s: #QTestState instance to operate on.
274 * @addr: Guest address to read from.
276 * Reads an 8-bit value from memory.
278 * Returns: Value read.
280 uint8_t qtest_readb(QTestState *s, uint64_t addr);
283 * qtest_readw:
284 * @s: #QTestState instance to operate on.
285 * @addr: Guest address to read from.
287 * Reads a 16-bit value from memory.
289 * Returns: Value read.
291 uint16_t qtest_readw(QTestState *s, uint64_t addr);
294 * qtest_readl:
295 * @s: #QTestState instance to operate on.
296 * @addr: Guest address to read from.
298 * Reads a 32-bit value from memory.
300 * Returns: Value read.
302 uint32_t qtest_readl(QTestState *s, uint64_t addr);
305 * qtest_readq:
306 * @s: #QTestState instance to operate on.
307 * @addr: Guest address to read from.
309 * Reads a 64-bit value from memory.
311 * Returns: Value read.
313 uint64_t qtest_readq(QTestState *s, uint64_t addr);
316 * qtest_memread:
317 * @s: #QTestState instance to operate on.
318 * @addr: Guest address to read from.
319 * @data: Pointer to where memory contents will be stored.
320 * @size: Number of bytes to read.
322 * Read guest memory into a buffer.
324 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
327 * qtest_bufread:
328 * @s: #QTestState instance to operate on.
329 * @addr: Guest address to read from.
330 * @data: Pointer to where memory contents will be stored.
331 * @size: Number of bytes to read.
333 * Read guest memory into a buffer and receive using a base64 encoding.
335 void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
338 * qtest_memwrite:
339 * @s: #QTestState instance to operate on.
340 * @addr: Guest address to write to.
341 * @data: Pointer to the bytes that will be written to guest memory.
342 * @size: Number of bytes to write.
344 * Write a buffer to guest memory.
346 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
349 * qtest_bufwrite:
350 * @s: #QTestState instance to operate on.
351 * @addr: Guest address to write to.
352 * @data: Pointer to the bytes that will be written to guest memory.
353 * @size: Number of bytes to write.
355 * Write a buffer to guest memory and transmit using a base64 encoding.
357 void qtest_bufwrite(QTestState *s, uint64_t addr,
358 const void *data, size_t size);
361 * qtest_memset:
362 * @s: #QTestState instance to operate on.
363 * @addr: Guest address to write to.
364 * @patt: Byte pattern to fill the guest memory region with.
365 * @size: Number of bytes to write.
367 * Write a pattern to guest memory.
369 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
372 * qtest_clock_step_next:
373 * @s: #QTestState instance to operate on.
375 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
377 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
379 int64_t qtest_clock_step_next(QTestState *s);
382 * qtest_clock_step:
383 * @s: QTestState instance to operate on.
384 * @step: Number of nanoseconds to advance the clock by.
386 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
388 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
390 int64_t qtest_clock_step(QTestState *s, int64_t step);
393 * qtest_clock_set:
394 * @s: QTestState instance to operate on.
395 * @val: Nanoseconds value to advance the clock to.
397 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
399 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
401 int64_t qtest_clock_set(QTestState *s, int64_t val);
404 * qtest_get_arch:
406 * Returns: The architecture for the QEMU executable under test.
408 const char *qtest_get_arch(void);
411 * qtest_add_func:
412 * @str: Test case path.
413 * @fn: Test case function
415 * Add a GTester testcase with the given name and function.
416 * The path is prefixed with the architecture under test, as
417 * returned by qtest_get_arch().
419 void qtest_add_func(const char *str, void (*fn));
422 * qtest_add_data_func:
423 * @str: Test case path.
424 * @data: Test case data
425 * @fn: Test case function
427 * Add a GTester testcase with the given name, data and function.
428 * The path is prefixed with the architecture under test, as
429 * returned by qtest_get_arch().
431 void qtest_add_data_func(const char *str, const void *data, void (*fn));
434 * qtest_add:
435 * @testpath: Test case path
436 * @Fixture: Fixture type
437 * @tdata: Test case data
438 * @fsetup: Test case setup function
439 * @ftest: Test case function
440 * @fteardown: Test case teardown function
442 * Add a GTester testcase with the given name, data and functions.
443 * The path is prefixed with the architecture under test, as
444 * returned by qtest_get_arch().
446 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
447 do { \
448 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
449 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
450 g_free(path); \
451 } while (0)
454 * qtest_start:
455 * @args: other arguments to pass to QEMU
457 * Start QEMU and assign the resulting #QTestState to a global variable.
458 * The global variable is used by "shortcut" functions documented below.
460 * Returns: #QTestState instance.
462 static inline QTestState *qtest_start(const char *args)
464 global_qtest = qtest_init(args);
465 return global_qtest;
469 * qtest_end:
471 * Shut down the QEMU process started by qtest_start().
473 static inline void qtest_end(void)
475 qtest_quit(global_qtest);
476 global_qtest = NULL;
480 * qmp:
481 * @fmt...: QMP message to send to qemu
483 * Sends a QMP message to QEMU and returns the response.
485 QDict *qmp(const char *fmt, ...);
488 * qmp_async:
489 * @fmt...: QMP message to send to qemu
491 * Sends a QMP message to QEMU and leaves the response in the stream.
493 void qmp_async(const char *fmt, ...);
496 * qmp_discard_response:
497 * @fmt...: QMP message to send to qemu
499 * Sends a QMP message to QEMU and consumes the response.
501 void qmp_discard_response(const char *fmt, ...);
504 * qmp_receive:
506 * Reads a QMP message from QEMU and returns the response.
508 static inline QDict *qmp_receive(void)
510 return qtest_qmp_receive(global_qtest);
514 * qmp_eventwait:
515 * @s: #event event to wait for.
517 * Continuosly polls for QMP responses until it receives the desired event.
519 static inline void qmp_eventwait(const char *event)
521 return qtest_qmp_eventwait(global_qtest, event);
525 * hmp:
526 * @fmt...: HMP command to send to QEMU
528 * Send HMP command to QEMU via QMP's human-monitor-command.
530 * Returns: the command's output. The caller should g_free() it.
532 char *hmp(const char *fmt, ...);
535 * get_irq:
536 * @num: Interrupt to observe.
538 * Returns: The level of the @num interrupt.
540 static inline bool get_irq(int num)
542 return qtest_get_irq(global_qtest, num);
546 * irq_intercept_in:
547 * @string: QOM path of a device.
549 * Associate qtest irqs with the GPIO-in pins of the device
550 * whose path is specified by @string.
552 static inline void irq_intercept_in(const char *string)
554 qtest_irq_intercept_in(global_qtest, string);
558 * qtest_irq_intercept_out:
559 * @string: QOM path of a device.
561 * Associate qtest irqs with the GPIO-out pins of the device
562 * whose path is specified by @string.
564 static inline void irq_intercept_out(const char *string)
566 qtest_irq_intercept_out(global_qtest, string);
570 * outb:
571 * @addr: I/O port to write to.
572 * @value: Value being written.
574 * Write an 8-bit value to an I/O port.
576 static inline void outb(uint16_t addr, uint8_t value)
578 qtest_outb(global_qtest, addr, value);
582 * outw:
583 * @addr: I/O port to write to.
584 * @value: Value being written.
586 * Write a 16-bit value to an I/O port.
588 static inline void outw(uint16_t addr, uint16_t value)
590 qtest_outw(global_qtest, addr, value);
594 * outl:
595 * @addr: I/O port to write to.
596 * @value: Value being written.
598 * Write a 32-bit value to an I/O port.
600 static inline void outl(uint16_t addr, uint32_t value)
602 qtest_outl(global_qtest, addr, value);
606 * inb:
607 * @addr: I/O port to read from.
609 * Reads an 8-bit value from an I/O port.
611 * Returns: Value read.
613 static inline uint8_t inb(uint16_t addr)
615 return qtest_inb(global_qtest, addr);
619 * inw:
620 * @addr: I/O port to read from.
622 * Reads a 16-bit value from an I/O port.
624 * Returns: Value read.
626 static inline uint16_t inw(uint16_t addr)
628 return qtest_inw(global_qtest, addr);
632 * inl:
633 * @addr: I/O port to read from.
635 * Reads a 32-bit value from an I/O port.
637 * Returns: Value read.
639 static inline uint32_t inl(uint16_t addr)
641 return qtest_inl(global_qtest, addr);
645 * writeb:
646 * @addr: Guest address to write to.
647 * @value: Value being written.
649 * Writes an 8-bit value to guest memory.
651 static inline void writeb(uint64_t addr, uint8_t value)
653 qtest_writeb(global_qtest, addr, value);
657 * writew:
658 * @addr: Guest address to write to.
659 * @value: Value being written.
661 * Writes a 16-bit value to guest memory.
663 static inline void writew(uint64_t addr, uint16_t value)
665 qtest_writew(global_qtest, addr, value);
669 * writel:
670 * @addr: Guest address to write to.
671 * @value: Value being written.
673 * Writes a 32-bit value to guest memory.
675 static inline void writel(uint64_t addr, uint32_t value)
677 qtest_writel(global_qtest, addr, value);
681 * writeq:
682 * @addr: Guest address to write to.
683 * @value: Value being written.
685 * Writes a 64-bit value to guest memory.
687 static inline void writeq(uint64_t addr, uint64_t value)
689 qtest_writeq(global_qtest, addr, value);
693 * readb:
694 * @addr: Guest address to read from.
696 * Reads an 8-bit value from guest memory.
698 * Returns: Value read.
700 static inline uint8_t readb(uint64_t addr)
702 return qtest_readb(global_qtest, addr);
706 * readw:
707 * @addr: Guest address to read from.
709 * Reads a 16-bit value from guest memory.
711 * Returns: Value read.
713 static inline uint16_t readw(uint64_t addr)
715 return qtest_readw(global_qtest, addr);
719 * readl:
720 * @addr: Guest address to read from.
722 * Reads a 32-bit value from guest memory.
724 * Returns: Value read.
726 static inline uint32_t readl(uint64_t addr)
728 return qtest_readl(global_qtest, addr);
732 * readq:
733 * @addr: Guest address to read from.
735 * Reads a 64-bit value from guest memory.
737 * Returns: Value read.
739 static inline uint64_t readq(uint64_t addr)
741 return qtest_readq(global_qtest, addr);
745 * memread:
746 * @addr: Guest address to read from.
747 * @data: Pointer to where memory contents will be stored.
748 * @size: Number of bytes to read.
750 * Read guest memory into a buffer.
752 static inline void memread(uint64_t addr, void *data, size_t size)
754 qtest_memread(global_qtest, addr, data, size);
758 * bufread:
759 * @addr: Guest address to read from.
760 * @data: Pointer to where memory contents will be stored.
761 * @size: Number of bytes to read.
763 * Read guest memory into a buffer, receive using a base64 encoding.
765 static inline void bufread(uint64_t addr, void *data, size_t size)
767 qtest_bufread(global_qtest, addr, data, size);
771 * memwrite:
772 * @addr: Guest address to write to.
773 * @data: Pointer to the bytes that will be written to guest memory.
774 * @size: Number of bytes to write.
776 * Write a buffer to guest memory.
778 static inline void memwrite(uint64_t addr, const void *data, size_t size)
780 qtest_memwrite(global_qtest, addr, data, size);
784 * bufwrite:
785 * @addr: Guest address to write to.
786 * @data: Pointer to the bytes that will be written to guest memory.
787 * @size: Number of bytes to write.
789 * Write a buffer to guest memory, transmit using a base64 encoding.
791 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
793 qtest_bufwrite(global_qtest, addr, data, size);
797 * qmemset:
798 * @addr: Guest address to write to.
799 * @patt: Byte pattern to fill the guest memory region with.
800 * @size: Number of bytes to write.
802 * Write a pattern to guest memory.
804 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
806 qtest_memset(global_qtest, addr, patt, size);
810 * clock_step_next:
812 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
814 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
816 static inline int64_t clock_step_next(void)
818 return qtest_clock_step_next(global_qtest);
822 * clock_step:
823 * @step: Number of nanoseconds to advance the clock by.
825 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
827 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
829 static inline int64_t clock_step(int64_t step)
831 return qtest_clock_step(global_qtest, step);
835 * clock_set:
836 * @val: Nanoseconds value to advance the clock to.
838 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
840 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
842 static inline int64_t clock_set(int64_t val)
844 return qtest_clock_set(global_qtest, val);
848 * qtest_big_endian:
850 * Returns: True if the architecture under test has a big endian configuration.
852 bool qtest_big_endian(void);
854 #endif