libqos: remove some leaks
[qemu/kevin.git] / tests / libqtest.h
blobebdd5bbe537ccd78802a716ed90a0d291a26d484
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)(void));
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,
432 void (*fn)(const void *));
435 * qtest_add:
436 * @testpath: Test case path
437 * @Fixture: Fixture type
438 * @tdata: Test case data
439 * @fsetup: Test case setup function
440 * @ftest: Test case function
441 * @fteardown: Test case teardown function
443 * Add a GTester testcase with the given name, data and functions.
444 * The path is prefixed with the architecture under test, as
445 * returned by qtest_get_arch().
447 #define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
448 do { \
449 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
450 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
451 g_free(path); \
452 } while (0)
454 void qtest_add_abrt_handler(GHookFunc fn, const void *data);
457 * qtest_start:
458 * @args: other arguments to pass to QEMU
460 * Start QEMU and assign the resulting #QTestState to a global variable.
461 * The global variable is used by "shortcut" functions documented below.
463 * Returns: #QTestState instance.
465 static inline QTestState *qtest_start(const char *args)
467 global_qtest = qtest_init(args);
468 return global_qtest;
472 * qtest_end:
474 * Shut down the QEMU process started by qtest_start().
476 static inline void qtest_end(void)
478 qtest_quit(global_qtest);
479 global_qtest = NULL;
483 * qmp:
484 * @fmt...: QMP message to send to qemu
486 * Sends a QMP message to QEMU and returns the response.
488 QDict *qmp(const char *fmt, ...);
491 * qmp_async:
492 * @fmt...: QMP message to send to qemu
494 * Sends a QMP message to QEMU and leaves the response in the stream.
496 void qmp_async(const char *fmt, ...);
499 * qmp_discard_response:
500 * @fmt...: QMP message to send to qemu
502 * Sends a QMP message to QEMU and consumes the response.
504 void qmp_discard_response(const char *fmt, ...);
507 * qmp_receive:
509 * Reads a QMP message from QEMU and returns the response.
511 static inline QDict *qmp_receive(void)
513 return qtest_qmp_receive(global_qtest);
517 * qmp_eventwait:
518 * @s: #event event to wait for.
520 * Continuosly polls for QMP responses until it receives the desired event.
522 static inline void qmp_eventwait(const char *event)
524 return qtest_qmp_eventwait(global_qtest, event);
528 * hmp:
529 * @fmt...: HMP command to send to QEMU
531 * Send HMP command to QEMU via QMP's human-monitor-command.
533 * Returns: the command's output. The caller should g_free() it.
535 char *hmp(const char *fmt, ...);
538 * get_irq:
539 * @num: Interrupt to observe.
541 * Returns: The level of the @num interrupt.
543 static inline bool get_irq(int num)
545 return qtest_get_irq(global_qtest, num);
549 * irq_intercept_in:
550 * @string: QOM path of a device.
552 * Associate qtest irqs with the GPIO-in pins of the device
553 * whose path is specified by @string.
555 static inline void irq_intercept_in(const char *string)
557 qtest_irq_intercept_in(global_qtest, string);
561 * qtest_irq_intercept_out:
562 * @string: QOM path of a device.
564 * Associate qtest irqs with the GPIO-out pins of the device
565 * whose path is specified by @string.
567 static inline void irq_intercept_out(const char *string)
569 qtest_irq_intercept_out(global_qtest, string);
573 * outb:
574 * @addr: I/O port to write to.
575 * @value: Value being written.
577 * Write an 8-bit value to an I/O port.
579 static inline void outb(uint16_t addr, uint8_t value)
581 qtest_outb(global_qtest, addr, value);
585 * outw:
586 * @addr: I/O port to write to.
587 * @value: Value being written.
589 * Write a 16-bit value to an I/O port.
591 static inline void outw(uint16_t addr, uint16_t value)
593 qtest_outw(global_qtest, addr, value);
597 * outl:
598 * @addr: I/O port to write to.
599 * @value: Value being written.
601 * Write a 32-bit value to an I/O port.
603 static inline void outl(uint16_t addr, uint32_t value)
605 qtest_outl(global_qtest, addr, value);
609 * inb:
610 * @addr: I/O port to read from.
612 * Reads an 8-bit value from an I/O port.
614 * Returns: Value read.
616 static inline uint8_t inb(uint16_t addr)
618 return qtest_inb(global_qtest, addr);
622 * inw:
623 * @addr: I/O port to read from.
625 * Reads a 16-bit value from an I/O port.
627 * Returns: Value read.
629 static inline uint16_t inw(uint16_t addr)
631 return qtest_inw(global_qtest, addr);
635 * inl:
636 * @addr: I/O port to read from.
638 * Reads a 32-bit value from an I/O port.
640 * Returns: Value read.
642 static inline uint32_t inl(uint16_t addr)
644 return qtest_inl(global_qtest, addr);
648 * writeb:
649 * @addr: Guest address to write to.
650 * @value: Value being written.
652 * Writes an 8-bit value to guest memory.
654 static inline void writeb(uint64_t addr, uint8_t value)
656 qtest_writeb(global_qtest, addr, value);
660 * writew:
661 * @addr: Guest address to write to.
662 * @value: Value being written.
664 * Writes a 16-bit value to guest memory.
666 static inline void writew(uint64_t addr, uint16_t value)
668 qtest_writew(global_qtest, addr, value);
672 * writel:
673 * @addr: Guest address to write to.
674 * @value: Value being written.
676 * Writes a 32-bit value to guest memory.
678 static inline void writel(uint64_t addr, uint32_t value)
680 qtest_writel(global_qtest, addr, value);
684 * writeq:
685 * @addr: Guest address to write to.
686 * @value: Value being written.
688 * Writes a 64-bit value to guest memory.
690 static inline void writeq(uint64_t addr, uint64_t value)
692 qtest_writeq(global_qtest, addr, value);
696 * readb:
697 * @addr: Guest address to read from.
699 * Reads an 8-bit value from guest memory.
701 * Returns: Value read.
703 static inline uint8_t readb(uint64_t addr)
705 return qtest_readb(global_qtest, addr);
709 * readw:
710 * @addr: Guest address to read from.
712 * Reads a 16-bit value from guest memory.
714 * Returns: Value read.
716 static inline uint16_t readw(uint64_t addr)
718 return qtest_readw(global_qtest, addr);
722 * readl:
723 * @addr: Guest address to read from.
725 * Reads a 32-bit value from guest memory.
727 * Returns: Value read.
729 static inline uint32_t readl(uint64_t addr)
731 return qtest_readl(global_qtest, addr);
735 * readq:
736 * @addr: Guest address to read from.
738 * Reads a 64-bit value from guest memory.
740 * Returns: Value read.
742 static inline uint64_t readq(uint64_t addr)
744 return qtest_readq(global_qtest, addr);
748 * memread:
749 * @addr: Guest address to read from.
750 * @data: Pointer to where memory contents will be stored.
751 * @size: Number of bytes to read.
753 * Read guest memory into a buffer.
755 static inline void memread(uint64_t addr, void *data, size_t size)
757 qtest_memread(global_qtest, addr, data, size);
761 * bufread:
762 * @addr: Guest address to read from.
763 * @data: Pointer to where memory contents will be stored.
764 * @size: Number of bytes to read.
766 * Read guest memory into a buffer, receive using a base64 encoding.
768 static inline void bufread(uint64_t addr, void *data, size_t size)
770 qtest_bufread(global_qtest, addr, data, size);
774 * memwrite:
775 * @addr: Guest address to write to.
776 * @data: Pointer to the bytes that will be written to guest memory.
777 * @size: Number of bytes to write.
779 * Write a buffer to guest memory.
781 static inline void memwrite(uint64_t addr, const void *data, size_t size)
783 qtest_memwrite(global_qtest, addr, data, size);
787 * bufwrite:
788 * @addr: Guest address to write to.
789 * @data: Pointer to the bytes that will be written to guest memory.
790 * @size: Number of bytes to write.
792 * Write a buffer to guest memory, transmit using a base64 encoding.
794 static inline void bufwrite(uint64_t addr, const void *data, size_t size)
796 qtest_bufwrite(global_qtest, addr, data, size);
800 * qmemset:
801 * @addr: Guest address to write to.
802 * @patt: Byte pattern to fill the guest memory region with.
803 * @size: Number of bytes to write.
805 * Write a pattern to guest memory.
807 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
809 qtest_memset(global_qtest, addr, patt, size);
813 * clock_step_next:
815 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
817 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
819 static inline int64_t clock_step_next(void)
821 return qtest_clock_step_next(global_qtest);
825 * clock_step:
826 * @step: Number of nanoseconds to advance the clock by.
828 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
830 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
832 static inline int64_t clock_step(int64_t step)
834 return qtest_clock_step(global_qtest, step);
838 * clock_set:
839 * @val: Nanoseconds value to advance the clock to.
841 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
843 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
845 static inline int64_t clock_set(int64_t val)
847 return qtest_clock_set(global_qtest, val);
851 * qtest_big_endian:
853 * Returns: True if the architecture under test has a big endian configuration.
855 bool qtest_big_endian(void);
858 QDict *qmp_fd_receive(int fd);
859 void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
860 void qmp_fd_send(int fd, const char *fmt, ...);
861 QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
862 QDict *qmp_fd(int fd, const char *fmt, ...);
864 #endif