iotests: qemu-img info output for corrupt image
[qemu/ar7.git] / tests / libqtest.h
blob3e12cab2f244ce9d435a5693d824db5b2bbb52e4
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"
27 typedef struct QTestState QTestState;
29 extern QTestState *global_qtest;
31 /**
32 * qtest_init:
33 * @extra_args: other arguments to pass to QEMU.
35 * Returns: #QTestState instance.
37 QTestState *qtest_init(const char *extra_args);
39 /**
40 * qtest_quit:
41 * @s: #QTestState instance to operate on.
43 * Shut down the QEMU process associated to @s.
45 void qtest_quit(QTestState *s);
47 /**
48 * qtest_qmp_discard_response:
49 * @s: #QTestState instance to operate on.
50 * @fmt...: QMP message to send to qemu
52 * Sends a QMP message to QEMU and consumes the response.
54 void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
56 /**
57 * qtest_qmp:
58 * @s: #QTestState instance to operate on.
59 * @fmt...: QMP message to send to qemu
61 * Sends a QMP message to QEMU and returns the response.
63 QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
65 /**
66 * qtest_qmpv_discard_response:
67 * @s: #QTestState instance to operate on.
68 * @fmt: QMP message to send to QEMU
69 * @ap: QMP message arguments
71 * Sends a QMP message to QEMU and consumes the response.
73 void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
75 /**
76 * qtest_qmpv:
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 returns the response.
83 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
85 /**
86 * qtest_receive:
87 * @s: #QTestState instance to operate on.
89 * Reads a QMP message from QEMU and returns the response.
91 QDict *qtest_qmp_receive(QTestState *s);
93 /**
94 * qtest_get_irq:
95 * @s: #QTestState instance to operate on.
96 * @num: Interrupt to observe.
98 * Returns: The level of the @num interrupt.
100 bool qtest_get_irq(QTestState *s, int num);
103 * qtest_irq_intercept_in:
104 * @s: #QTestState instance to operate on.
105 * @string: QOM path of a device.
107 * Associate qtest irqs with the GPIO-in pins of the device
108 * whose path is specified by @string.
110 void qtest_irq_intercept_in(QTestState *s, const char *string);
113 * qtest_irq_intercept_out:
114 * @s: #QTestState instance to operate on.
115 * @string: QOM path of a device.
117 * Associate qtest irqs with the GPIO-out pins of the device
118 * whose path is specified by @string.
120 void qtest_irq_intercept_out(QTestState *s, const char *string);
123 * qtest_outb:
124 * @s: #QTestState instance to operate on.
125 * @addr: I/O port to write to.
126 * @value: Value being written.
128 * Write an 8-bit value to an I/O port.
130 void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
133 * qtest_outw:
134 * @s: #QTestState instance to operate on.
135 * @addr: I/O port to write to.
136 * @value: Value being written.
138 * Write a 16-bit value to an I/O port.
140 void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
143 * qtest_outl:
144 * @s: #QTestState instance to operate on.
145 * @addr: I/O port to write to.
146 * @value: Value being written.
148 * Write a 32-bit value to an I/O port.
150 void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
153 * qtest_inb:
154 * @s: #QTestState instance to operate on.
155 * @addr: I/O port to read from.
157 * Returns an 8-bit value from an I/O port.
159 uint8_t qtest_inb(QTestState *s, uint16_t addr);
162 * qtest_inw:
163 * @s: #QTestState instance to operate on.
164 * @addr: I/O port to read from.
166 * Returns a 16-bit value from an I/O port.
168 uint16_t qtest_inw(QTestState *s, uint16_t addr);
171 * qtest_inl:
172 * @s: #QTestState instance to operate on.
173 * @addr: I/O port to read from.
175 * Returns a 32-bit value from an I/O port.
177 uint32_t qtest_inl(QTestState *s, uint16_t addr);
180 * qtest_writeb:
181 * @s: #QTestState instance to operate on.
182 * @addr: Guest address to write to.
183 * @value: Value being written.
185 * Writes an 8-bit value to memory.
187 void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
190 * qtest_writew:
191 * @s: #QTestState instance to operate on.
192 * @addr: Guest address to write to.
193 * @value: Value being written.
195 * Writes a 16-bit value to memory.
197 void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
200 * qtest_writel:
201 * @s: #QTestState instance to operate on.
202 * @addr: Guest address to write to.
203 * @value: Value being written.
205 * Writes a 32-bit value to memory.
207 void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
210 * qtest_writeq:
211 * @s: #QTestState instance to operate on.
212 * @addr: Guest address to write to.
213 * @value: Value being written.
215 * Writes a 64-bit value to memory.
217 void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
220 * qtest_readb:
221 * @s: #QTestState instance to operate on.
222 * @addr: Guest address to read from.
224 * Reads an 8-bit value from memory.
226 * Returns: Value read.
228 uint8_t qtest_readb(QTestState *s, uint64_t addr);
231 * qtest_readw:
232 * @s: #QTestState instance to operate on.
233 * @addr: Guest address to read from.
235 * Reads a 16-bit value from memory.
237 * Returns: Value read.
239 uint16_t qtest_readw(QTestState *s, uint64_t addr);
242 * qtest_readl:
243 * @s: #QTestState instance to operate on.
244 * @addr: Guest address to read from.
246 * Reads a 32-bit value from memory.
248 * Returns: Value read.
250 uint32_t qtest_readl(QTestState *s, uint64_t addr);
253 * qtest_readq:
254 * @s: #QTestState instance to operate on.
255 * @addr: Guest address to read from.
257 * Reads a 64-bit value from memory.
259 * Returns: Value read.
261 uint64_t qtest_readq(QTestState *s, uint64_t addr);
264 * qtest_memread:
265 * @s: #QTestState instance to operate on.
266 * @addr: Guest address to read from.
267 * @data: Pointer to where memory contents will be stored.
268 * @size: Number of bytes to read.
270 * Read guest memory into a buffer.
272 void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
275 * qtest_memwrite:
276 * @s: #QTestState instance to operate on.
277 * @addr: Guest address to write to.
278 * @data: Pointer to the bytes that will be written to guest memory.
279 * @size: Number of bytes to write.
281 * Write a buffer to guest memory.
283 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
286 * qtest_memset:
287 * @s: #QTestState instance to operate on.
288 * @addr: Guest address to write to.
289 * @patt: Byte pattern to fill the guest memory region with.
290 * @size: Number of bytes to write.
292 * Write a pattern to guest memory.
294 void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
297 * qtest_clock_step_next:
298 * @s: #QTestState instance to operate on.
300 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
302 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
304 int64_t qtest_clock_step_next(QTestState *s);
307 * qtest_clock_step:
308 * @s: QTestState instance to operate on.
309 * @step: Number of nanoseconds to advance the clock by.
311 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
313 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
315 int64_t qtest_clock_step(QTestState *s, int64_t step);
318 * qtest_clock_set:
319 * @s: QTestState instance to operate on.
320 * @val: Nanoseconds value to advance the clock to.
322 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
324 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
326 int64_t qtest_clock_set(QTestState *s, int64_t val);
329 * qtest_get_arch:
331 * Returns: The architecture for the QEMU executable under test.
333 const char *qtest_get_arch(void);
336 * qtest_add_func:
337 * @str: Test case path.
338 * @fn: Test case function
340 * Add a GTester testcase with the given name and function.
341 * The path is prefixed with the architecture under test, as
342 * returned by qtest_get_arch().
344 void qtest_add_func(const char *str, void (*fn));
347 * qtest_start:
348 * @args: other arguments to pass to QEMU
350 * Start QEMU and assign the resulting #QTestState to a global variable.
351 * The global variable is used by "shortcut" functions documented below.
353 * Returns: #QTestState instance.
355 static inline QTestState *qtest_start(const char *args)
357 global_qtest = qtest_init(args);
358 return global_qtest;
362 * qtest_end:
364 * Shut down the QEMU process started by qtest_start().
366 static inline void qtest_end(void)
368 qtest_quit(global_qtest);
369 global_qtest = NULL;
373 * qmp:
374 * @fmt...: QMP message to send to qemu
376 * Sends a QMP message to QEMU and returns the response.
378 QDict *qmp(const char *fmt, ...);
381 * qmp_discard_response:
382 * @fmt...: QMP message to send to qemu
384 * Sends a QMP message to QEMU and consumes the response.
386 void qmp_discard_response(const char *fmt, ...);
389 * qmp_receive:
391 * Reads a QMP message from QEMU and returns the response.
393 static inline QDict *qmp_receive(void)
395 return qtest_qmp_receive(global_qtest);
399 * get_irq:
400 * @num: Interrupt to observe.
402 * Returns: The level of the @num interrupt.
404 static inline bool get_irq(int num)
406 return qtest_get_irq(global_qtest, num);
410 * irq_intercept_in:
411 * @string: QOM path of a device.
413 * Associate qtest irqs with the GPIO-in pins of the device
414 * whose path is specified by @string.
416 static inline void irq_intercept_in(const char *string)
418 qtest_irq_intercept_in(global_qtest, string);
422 * qtest_irq_intercept_out:
423 * @string: QOM path of a device.
425 * Associate qtest irqs with the GPIO-out pins of the device
426 * whose path is specified by @string.
428 static inline void irq_intercept_out(const char *string)
430 qtest_irq_intercept_out(global_qtest, string);
434 * outb:
435 * @addr: I/O port to write to.
436 * @value: Value being written.
438 * Write an 8-bit value to an I/O port.
440 static inline void outb(uint16_t addr, uint8_t value)
442 qtest_outb(global_qtest, addr, value);
446 * outw:
447 * @addr: I/O port to write to.
448 * @value: Value being written.
450 * Write a 16-bit value to an I/O port.
452 static inline void outw(uint16_t addr, uint16_t value)
454 qtest_outw(global_qtest, addr, value);
458 * outl:
459 * @addr: I/O port to write to.
460 * @value: Value being written.
462 * Write a 32-bit value to an I/O port.
464 static inline void outl(uint16_t addr, uint32_t value)
466 qtest_outl(global_qtest, addr, value);
470 * inb:
471 * @addr: I/O port to read from.
473 * Reads an 8-bit value from an I/O port.
475 * Returns: Value read.
477 static inline uint8_t inb(uint16_t addr)
479 return qtest_inb(global_qtest, addr);
483 * inw:
484 * @addr: I/O port to read from.
486 * Reads a 16-bit value from an I/O port.
488 * Returns: Value read.
490 static inline uint16_t inw(uint16_t addr)
492 return qtest_inw(global_qtest, addr);
496 * inl:
497 * @addr: I/O port to read from.
499 * Reads a 32-bit value from an I/O port.
501 * Returns: Value read.
503 static inline uint32_t inl(uint16_t addr)
505 return qtest_inl(global_qtest, addr);
509 * writeb:
510 * @addr: Guest address to write to.
511 * @value: Value being written.
513 * Writes an 8-bit value to guest memory.
515 static inline void writeb(uint64_t addr, uint8_t value)
517 qtest_writeb(global_qtest, addr, value);
521 * writew:
522 * @addr: Guest address to write to.
523 * @value: Value being written.
525 * Writes a 16-bit value to guest memory.
527 static inline void writew(uint64_t addr, uint16_t value)
529 qtest_writew(global_qtest, addr, value);
533 * writel:
534 * @addr: Guest address to write to.
535 * @value: Value being written.
537 * Writes a 32-bit value to guest memory.
539 static inline void writel(uint64_t addr, uint32_t value)
541 qtest_writel(global_qtest, addr, value);
545 * writeq:
546 * @addr: Guest address to write to.
547 * @value: Value being written.
549 * Writes a 64-bit value to guest memory.
551 static inline void writeq(uint64_t addr, uint64_t value)
553 qtest_writeq(global_qtest, addr, value);
557 * readb:
558 * @addr: Guest address to read from.
560 * Reads an 8-bit value from guest memory.
562 * Returns: Value read.
564 static inline uint8_t readb(uint64_t addr)
566 return qtest_readb(global_qtest, addr);
570 * readw:
571 * @addr: Guest address to read from.
573 * Reads a 16-bit value from guest memory.
575 * Returns: Value read.
577 static inline uint16_t readw(uint64_t addr)
579 return qtest_readw(global_qtest, addr);
583 * readl:
584 * @addr: Guest address to read from.
586 * Reads a 32-bit value from guest memory.
588 * Returns: Value read.
590 static inline uint32_t readl(uint64_t addr)
592 return qtest_readl(global_qtest, addr);
596 * readq:
597 * @addr: Guest address to read from.
599 * Reads a 64-bit value from guest memory.
601 * Returns: Value read.
603 static inline uint64_t readq(uint64_t addr)
605 return qtest_readq(global_qtest, addr);
609 * memread:
610 * @addr: Guest address to read from.
611 * @data: Pointer to where memory contents will be stored.
612 * @size: Number of bytes to read.
614 * Read guest memory into a buffer.
616 static inline void memread(uint64_t addr, void *data, size_t size)
618 qtest_memread(global_qtest, addr, data, size);
622 * memwrite:
623 * @addr: Guest address to write to.
624 * @data: Pointer to the bytes that will be written to guest memory.
625 * @size: Number of bytes to write.
627 * Write a buffer to guest memory.
629 static inline void memwrite(uint64_t addr, const void *data, size_t size)
631 qtest_memwrite(global_qtest, addr, data, size);
635 * qmemset:
636 * @addr: Guest address to write to.
637 * @patt: Byte pattern to fill the guest memory region with.
638 * @size: Number of bytes to write.
640 * Write a pattern to guest memory.
642 static inline void qmemset(uint64_t addr, uint8_t patt, size_t size)
644 qtest_memset(global_qtest, addr, patt, size);
648 * clock_step_next:
650 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
652 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
654 static inline int64_t clock_step_next(void)
656 return qtest_clock_step_next(global_qtest);
660 * clock_step:
661 * @step: Number of nanoseconds to advance the clock by.
663 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
665 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
667 static inline int64_t clock_step(int64_t step)
669 return qtest_clock_step(global_qtest, step);
673 * clock_set:
674 * @val: Nanoseconds value to advance the clock to.
676 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
678 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
680 static inline int64_t clock_set(int64_t val)
682 return qtest_clock_set(global_qtest, val);
686 * qtest_big_endian:
688 * Returns: True if the architecture under test has a big endian configuration.
690 bool qtest_big_endian(void);
692 #endif