]> git.proxmox.com Git - mirror_qemu.git/blame - tests/libqtest.h
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[mirror_qemu.git] / tests / libqtest.h
CommitLineData
49ee3590
AL
1/*
2 * QTest
3 *
4 * Copyright IBM, Corp. 2012
5 * Copyright Red Hat, Inc. 2012
872536bf 6 * Copyright SUSE LINUX Products GmbH 2013
49ee3590
AL
7 *
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
10 * Paolo Bonzini <pbonzini@redhat.com>
872536bf 11 * Andreas Färber <afaerber@suse.de>
49ee3590
AL
12 *
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.
15 *
16 */
17#ifndef LIBQTEST_H
18#define LIBQTEST_H
19
5cb8f0db
PB
20#include "qapi/qmp/qobject.h"
21#include "qapi/qmp/qdict.h"
22
49ee3590
AL
23typedef struct QTestState QTestState;
24
25extern QTestState *global_qtest;
26
78b27bad 27/**
88b988c8 28 * qtest_initf:
78b27bad
EB
29 * @fmt...: Format for creating other arguments to pass to QEMU, formatted
30 * like sprintf().
31 *
00825d96 32 * Convenience wrapper around qtest_init().
78b27bad
EB
33 *
34 * Returns: #QTestState instance.
35 */
88b988c8 36QTestState *qtest_initf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
78b27bad
EB
37
38/**
88b988c8 39 * qtest_vinitf:
78b27bad
EB
40 * @fmt: Format for creating other arguments to pass to QEMU, formatted
41 * like vsprintf().
42 * @ap: Format arguments.
43 *
00825d96 44 * Convenience wrapper around qtest_init().
78b27bad
EB
45 *
46 * Returns: #QTestState instance.
47 */
88b988c8 48QTestState *qtest_vinitf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
78b27bad 49
49ee3590
AL
50/**
51 * qtest_init:
88b988c8
MA
52 * @extra_args: other arguments to pass to QEMU. CAUTION: these
53 * arguments are subject to word splitting and shell evaluation.
6acf801d
AF
54 *
55 * Returns: #QTestState instance.
49ee3590
AL
56 */
57QTestState *qtest_init(const char *extra_args);
58
f66e7ac8
MA
59/**
60 * qtest_init_without_qmp_handshake:
ddee57e0
EB
61 * @extra_args: other arguments to pass to QEMU. CAUTION: these
62 * arguments are subject to word splitting and shell evaluation.
f66e7ac8
MA
63 *
64 * Returns: #QTestState instance.
65 */
192f26a7 66QTestState *qtest_init_without_qmp_handshake(const char *extra_args);
f66e7ac8 67
6c90a82c
JS
68/**
69 * qtest_init_with_serial:
70 * @extra_args: other arguments to pass to QEMU. CAUTION: these
71 * arguments are subject to word splitting and shell evaluation.
72 * @sock_fd: pointer to store the socket file descriptor for
73 * connection with serial.
74 *
75 * Returns: #QTestState instance.
76 */
77QTestState *qtest_init_with_serial(const char *extra_args, int *sock_fd);
78
49ee3590
AL
79/**
80 * qtest_quit:
6acf801d 81 * @s: #QTestState instance to operate on.
49ee3590
AL
82 *
83 * Shut down the QEMU process associated to @s.
84 */
85void qtest_quit(QTestState *s);
86
24d5588c
YK
87/**
88 * qtest_qmp_fds:
89 * @s: #QTestState instance to operate on.
90 * @fds: array of file descriptors
91 * @fds_num: number of elements in @fds
92 * @fmt...: QMP message to send to qemu, formatted like
93 * qobject_from_jsonf_nofail(). See parse_escape() for what's
94 * supported after '%'.
95 *
96 * Sends a QMP message to QEMU with fds and returns the response.
97 */
98QDict *qtest_qmp_fds(QTestState *s, int *fds, size_t fds_num,
99 const char *fmt, ...)
100 GCC_FMT_ATTR(4, 5);
101
0c460dac
SH
102/**
103 * qtest_qmp:
104 * @s: #QTestState instance to operate on.
bb340eb2 105 * @fmt...: QMP message to send to qemu, formatted like
6ce80fd8
MA
106 * qobject_from_jsonf_nofail(). See parse_escape() for what's
107 * supported after '%'.
0c460dac
SH
108 *
109 * Sends a QMP message to QEMU and returns the response.
110 */
e3dc93be
MA
111QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
112 GCC_FMT_ATTR(2, 3);
0c460dac 113
ba4ed393 114/**
4277f1eb 115 * qtest_qmp_send:
ba4ed393 116 * @s: #QTestState instance to operate on.
bb340eb2 117 * @fmt...: QMP message to send to qemu, formatted like
6ce80fd8
MA
118 * qobject_from_jsonf_nofail(). See parse_escape() for what's
119 * supported after '%'.
ba4ed393
JS
120 *
121 * Sends a QMP message to QEMU and leaves the response in the stream.
122 */
e3dc93be
MA
123void qtest_qmp_send(QTestState *s, const char *fmt, ...)
124 GCC_FMT_ATTR(2, 3);
ba4ed393 125
aed877c5
MA
126/**
127 * qtest_qmp_send_raw:
128 * @s: #QTestState instance to operate on.
129 * @fmt...: text to send, formatted like sprintf()
130 *
131 * Sends text to the QMP monitor verbatim. Need not be valid JSON;
132 * this is useful for negative tests.
133 */
134void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
135 GCC_FMT_ATTR(2, 3);
136
0c460dac 137/**
24d5588c
YK
138 * qtest_vqmp_fds:
139 * @s: #QTestState instance to operate on.
140 * @fds: array of file descriptors
141 * @fds_num: number of elements in @fds
142 * @fmt: QMP message to send to QEMU, formatted like
143 * qobject_from_jsonf_nofail(). See parse_escape() for what's
144 * supported after '%'.
145 * @ap: QMP message arguments
146 *
147 * Sends a QMP message to QEMU with fds and returns the response.
148 */
149QDict *qtest_vqmp_fds(QTestState *s, int *fds, size_t fds_num,
150 const char *fmt, va_list ap)
151 GCC_FMT_ATTR(4, 0);
152
153/**
154 * qtest_vqmp:
0c460dac 155 * @s: #QTestState instance to operate on.
bb340eb2 156 * @fmt: QMP message to send to QEMU, formatted like
6ce80fd8
MA
157 * qobject_from_jsonf_nofail(). See parse_escape() for what's
158 * supported after '%'.
0c460dac
SH
159 * @ap: QMP message arguments
160 *
161 * Sends a QMP message to QEMU and returns the response.
162 */
248eef02 163QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
e3dc93be 164 GCC_FMT_ATTR(2, 0);
0c460dac 165
24d5588c
YK
166/**
167 * qtest_qmp_vsend_fds:
168 * @s: #QTestState instance to operate on.
169 * @fds: array of file descriptors
170 * @fds_num: number of elements in @fds
171 * @fmt: QMP message to send to QEMU, formatted like
172 * qobject_from_jsonf_nofail(). See parse_escape() for what's
173 * supported after '%'.
174 * @ap: QMP message arguments
175 *
176 * Sends a QMP message to QEMU and leaves the response in the stream.
177 */
178void qtest_qmp_vsend_fds(QTestState *s, int *fds, size_t fds_num,
179 const char *fmt, va_list ap)
180 GCC_FMT_ATTR(4, 0);
181
ba4ed393 182/**
4277f1eb 183 * qtest_qmp_vsend:
ba4ed393 184 * @s: #QTestState instance to operate on.
bb340eb2 185 * @fmt: QMP message to send to QEMU, formatted like
6ce80fd8
MA
186 * qobject_from_jsonf_nofail(). See parse_escape() for what's
187 * supported after '%'.
ba4ed393
JS
188 * @ap: QMP message arguments
189 *
190 * Sends a QMP message to QEMU and leaves the response in the stream.
191 */
e3dc93be
MA
192void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
193 GCC_FMT_ATTR(2, 0);
ba4ed393 194
66e0c7b1
AF
195/**
196 * qtest_receive:
197 * @s: #QTestState instance to operate on.
198 *
199 * Reads a QMP message from QEMU and returns the response.
200 */
201QDict *qtest_qmp_receive(QTestState *s);
202
8fe941f7
JS
203/**
204 * qtest_qmp_eventwait:
205 * @s: #QTestState instance to operate on.
206 * @s: #event event to wait for.
207 *
e8ec0117 208 * Continuously polls for QMP responses until it receives the desired event.
8fe941f7
JS
209 */
210void qtest_qmp_eventwait(QTestState *s, const char *event);
211
7ffe3124
JS
212/**
213 * qtest_qmp_eventwait_ref:
214 * @s: #QTestState instance to operate on.
215 * @s: #event event to wait for.
216 *
e8ec0117 217 * Continuously polls for QMP responses until it receives the desired event.
7ffe3124
JS
218 * Returns a copy of the event for further investigation.
219 */
220QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
221
3cd46d42
MA
222/**
223 * qtest_qmp_receive_success:
224 * @s: #QTestState instance to operate on
225 * @event_cb: Event callback
226 * @opaque: Argument for @event_cb
227 *
228 * Poll QMP messages until a command success response is received.
229 * If @event_cb, call it for each event received, passing @opaque,
230 * the event's name and data.
231 * Return the success response's "return" member.
232 */
233QDict *qtest_qmp_receive_success(QTestState *s,
234 void (*event_cb)(void *opaque,
235 const char *name,
236 QDict *data),
237 void *opaque);
238
5fb48d96 239/**
6bb87be8 240 * qtest_hmp:
5fb48d96 241 * @s: #QTestState instance to operate on.
7b899f4d 242 * @fmt...: HMP command to send to QEMU, formats arguments like sprintf().
5fb48d96
MA
243 *
244 * Send HMP command to QEMU via QMP's human-monitor-command.
6bb87be8 245 * QMP events are discarded.
5fb48d96
MA
246 *
247 * Returns: the command's output. The caller should g_free() it.
248 */
7b899f4d 249char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
5fb48d96
MA
250
251/**
252 * qtest_hmpv:
253 * @s: #QTestState instance to operate on.
bb340eb2 254 * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
5fb48d96
MA
255 * @ap: HMP command arguments
256 *
257 * Send HMP command to QEMU via QMP's human-monitor-command.
6bb87be8 258 * QMP events are discarded.
5fb48d96
MA
259 *
260 * Returns: the command's output. The caller should g_free() it.
261 */
248eef02 262char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
bb340eb2 263 GCC_FMT_ATTR(2, 0);
5fb48d96 264
49ee3590
AL
265/**
266 * qtest_get_irq:
6acf801d 267 * @s: #QTestState instance to operate on.
49ee3590
AL
268 * @num: Interrupt to observe.
269 *
6acf801d 270 * Returns: The level of the @num interrupt.
49ee3590
AL
271 */
272bool qtest_get_irq(QTestState *s, int num);
273
274/**
275 * qtest_irq_intercept_in:
6acf801d 276 * @s: #QTestState instance to operate on.
49ee3590
AL
277 * @string: QOM path of a device.
278 *
279 * Associate qtest irqs with the GPIO-in pins of the device
280 * whose path is specified by @string.
281 */
282void qtest_irq_intercept_in(QTestState *s, const char *string);
283
284/**
285 * qtest_irq_intercept_out:
6acf801d 286 * @s: #QTestState instance to operate on.
49ee3590
AL
287 * @string: QOM path of a device.
288 *
289 * Associate qtest irqs with the GPIO-out pins of the device
290 * whose path is specified by @string.
291 */
292void qtest_irq_intercept_out(QTestState *s, const char *string);
293
9813dc6a
SG
294/**
295 * qtest_set_irq_in:
296 * @s: QTestState instance to operate on.
297 * @string: QOM path of a device
298 * @name: IRQ name
299 * @irq: IRQ number
300 * @level: IRQ level
301 *
302 * Force given device/irq GPIO-in pin to the given level.
303 */
304void qtest_set_irq_in(QTestState *s, const char *string, const char *name,
305 int irq, int level);
306
49ee3590
AL
307/**
308 * qtest_outb:
6acf801d 309 * @s: #QTestState instance to operate on.
49ee3590
AL
310 * @addr: I/O port to write to.
311 * @value: Value being written.
312 *
313 * Write an 8-bit value to an I/O port.
314 */
315void qtest_outb(QTestState *s, uint16_t addr, uint8_t value);
316
317/**
318 * qtest_outw:
6acf801d 319 * @s: #QTestState instance to operate on.
49ee3590
AL
320 * @addr: I/O port to write to.
321 * @value: Value being written.
322 *
323 * Write a 16-bit value to an I/O port.
324 */
325void qtest_outw(QTestState *s, uint16_t addr, uint16_t value);
326
327/**
328 * qtest_outl:
6acf801d 329 * @s: #QTestState instance to operate on.
49ee3590
AL
330 * @addr: I/O port to write to.
331 * @value: Value being written.
332 *
333 * Write a 32-bit value to an I/O port.
334 */
335void qtest_outl(QTestState *s, uint16_t addr, uint32_t value);
336
337/**
338 * qtest_inb:
6acf801d 339 * @s: #QTestState instance to operate on.
49ee3590 340 * @addr: I/O port to read from.
49ee3590
AL
341 *
342 * Returns an 8-bit value from an I/O port.
343 */
344uint8_t qtest_inb(QTestState *s, uint16_t addr);
345
346/**
347 * qtest_inw:
6acf801d 348 * @s: #QTestState instance to operate on.
49ee3590 349 * @addr: I/O port to read from.
49ee3590
AL
350 *
351 * Returns a 16-bit value from an I/O port.
352 */
353uint16_t qtest_inw(QTestState *s, uint16_t addr);
354
355/**
356 * qtest_inl:
6acf801d 357 * @s: #QTestState instance to operate on.
49ee3590 358 * @addr: I/O port to read from.
49ee3590
AL
359 *
360 * Returns a 32-bit value from an I/O port.
361 */
362uint32_t qtest_inl(QTestState *s, uint16_t addr);
363
872536bf
AF
364/**
365 * qtest_writeb:
366 * @s: #QTestState instance to operate on.
367 * @addr: Guest address to write to.
368 * @value: Value being written.
369 *
370 * Writes an 8-bit value to memory.
371 */
372void qtest_writeb(QTestState *s, uint64_t addr, uint8_t value);
373
374/**
375 * qtest_writew:
376 * @s: #QTestState instance to operate on.
377 * @addr: Guest address to write to.
378 * @value: Value being written.
379 *
380 * Writes a 16-bit value to memory.
381 */
382void qtest_writew(QTestState *s, uint64_t addr, uint16_t value);
383
384/**
385 * qtest_writel:
386 * @s: #QTestState instance to operate on.
387 * @addr: Guest address to write to.
388 * @value: Value being written.
389 *
390 * Writes a 32-bit value to memory.
391 */
392void qtest_writel(QTestState *s, uint64_t addr, uint32_t value);
393
394/**
395 * qtest_writeq:
396 * @s: #QTestState instance to operate on.
397 * @addr: Guest address to write to.
398 * @value: Value being written.
399 *
400 * Writes a 64-bit value to memory.
401 */
402void qtest_writeq(QTestState *s, uint64_t addr, uint64_t value);
403
404/**
405 * qtest_readb:
406 * @s: #QTestState instance to operate on.
407 * @addr: Guest address to read from.
408 *
409 * Reads an 8-bit value from memory.
410 *
411 * Returns: Value read.
412 */
413uint8_t qtest_readb(QTestState *s, uint64_t addr);
414
415/**
416 * qtest_readw:
417 * @s: #QTestState instance to operate on.
418 * @addr: Guest address to read from.
419 *
420 * Reads a 16-bit value from memory.
421 *
422 * Returns: Value read.
423 */
424uint16_t qtest_readw(QTestState *s, uint64_t addr);
425
426/**
427 * qtest_readl:
428 * @s: #QTestState instance to operate on.
429 * @addr: Guest address to read from.
430 *
431 * Reads a 32-bit value from memory.
432 *
433 * Returns: Value read.
434 */
435uint32_t qtest_readl(QTestState *s, uint64_t addr);
436
437/**
438 * qtest_readq:
439 * @s: #QTestState instance to operate on.
440 * @addr: Guest address to read from.
441 *
442 * Reads a 64-bit value from memory.
443 *
444 * Returns: Value read.
445 */
446uint64_t qtest_readq(QTestState *s, uint64_t addr);
447
49ee3590
AL
448/**
449 * qtest_memread:
6acf801d 450 * @s: #QTestState instance to operate on.
49ee3590
AL
451 * @addr: Guest address to read from.
452 * @data: Pointer to where memory contents will be stored.
453 * @size: Number of bytes to read.
454 *
455 * Read guest memory into a buffer.
456 */
457void qtest_memread(QTestState *s, uint64_t addr, void *data, size_t size);
458
eeddd59f
LV
459/**
460 * qtest_rtas_call:
461 * @s: #QTestState instance to operate on.
462 * @name: name of the command to call.
463 * @nargs: Number of args.
464 * @args: Guest address to read args from.
465 * @nret: Number of return value.
466 * @ret: Guest address to write return values to.
467 *
468 * Call an RTAS function
469 */
470uint64_t qtest_rtas_call(QTestState *s, const char *name,
471 uint32_t nargs, uint64_t args,
472 uint32_t nret, uint64_t ret);
473
7a6a740d
JS
474/**
475 * qtest_bufread:
476 * @s: #QTestState instance to operate on.
477 * @addr: Guest address to read from.
478 * @data: Pointer to where memory contents will be stored.
479 * @size: Number of bytes to read.
480 *
481 * Read guest memory into a buffer and receive using a base64 encoding.
482 */
483void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size);
484
49ee3590
AL
485/**
486 * qtest_memwrite:
6acf801d 487 * @s: #QTestState instance to operate on.
49ee3590
AL
488 * @addr: Guest address to write to.
489 * @data: Pointer to the bytes that will be written to guest memory.
490 * @size: Number of bytes to write.
491 *
492 * Write a buffer to guest memory.
493 */
494void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size);
495
7a6a740d
JS
496/**
497 * qtest_bufwrite:
498 * @s: #QTestState instance to operate on.
499 * @addr: Guest address to write to.
500 * @data: Pointer to the bytes that will be written to guest memory.
501 * @size: Number of bytes to write.
502 *
503 * Write a buffer to guest memory and transmit using a base64 encoding.
504 */
505void qtest_bufwrite(QTestState *s, uint64_t addr,
506 const void *data, size_t size);
507
86298845
JS
508/**
509 * qtest_memset:
510 * @s: #QTestState instance to operate on.
511 * @addr: Guest address to write to.
512 * @patt: Byte pattern to fill the guest memory region with.
513 * @size: Number of bytes to write.
514 *
515 * Write a pattern to guest memory.
516 */
517void qtest_memset(QTestState *s, uint64_t addr, uint8_t patt, size_t size);
518
49ee3590
AL
519/**
520 * qtest_clock_step_next:
6acf801d
AF
521 * @s: #QTestState instance to operate on.
522 *
bc72ad67 523 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
49ee3590 524 *
bc72ad67 525 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590
AL
526 */
527int64_t qtest_clock_step_next(QTestState *s);
528
529/**
530 * qtest_clock_step:
531 * @s: QTestState instance to operate on.
532 * @step: Number of nanoseconds to advance the clock by.
533 *
bc72ad67 534 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
6acf801d 535 *
bc72ad67 536 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590
AL
537 */
538int64_t qtest_clock_step(QTestState *s, int64_t step);
539
540/**
541 * qtest_clock_set:
542 * @s: QTestState instance to operate on.
543 * @val: Nanoseconds value to advance the clock to.
544 *
bc72ad67 545 * Advance the QEMU_CLOCK_VIRTUAL to @val nanoseconds since the VM was launched.
6acf801d 546 *
bc72ad67 547 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590
AL
548 */
549int64_t qtest_clock_set(QTestState *s, int64_t val);
550
54ce6f22
LV
551/**
552 * qtest_big_endian:
553 * @s: QTestState instance to operate on.
554 *
555 * Returns: True if the architecture under test has a big endian configuration.
556 */
557bool qtest_big_endian(QTestState *s);
558
49ee3590
AL
559/**
560 * qtest_get_arch:
561 *
6acf801d 562 * Returns: The architecture for the QEMU executable under test.
49ee3590
AL
563 */
564const char *qtest_get_arch(void);
565
566/**
567 * qtest_add_func:
568 * @str: Test case path.
569 * @fn: Test case function
570 *
571 * Add a GTester testcase with the given name and function.
572 * The path is prefixed with the architecture under test, as
6acf801d 573 * returned by qtest_get_arch().
49ee3590 574 */
041088c7 575void qtest_add_func(const char *str, void (*fn)(void));
49ee3590 576
7949c0e3
AF
577/**
578 * qtest_add_data_func:
579 * @str: Test case path.
580 * @data: Test case data
581 * @fn: Test case function
582 *
583 * Add a GTester testcase with the given name, data and function.
584 * The path is prefixed with the architecture under test, as
585 * returned by qtest_get_arch().
586 */
041088c7
MA
587void qtest_add_data_func(const char *str, const void *data,
588 void (*fn)(const void *));
7949c0e3 589
822e36ca
MAL
590/**
591 * qtest_add_data_func_full:
592 * @str: Test case path.
593 * @data: Test case data
594 * @fn: Test case function
595 * @data_free_func: GDestroyNotify for data
596 *
597 * Add a GTester testcase with the given name, data and function.
598 * The path is prefixed with the architecture under test, as
599 * returned by qtest_get_arch().
600 *
601 * @data is passed to @data_free_func() on test completion.
602 */
603void qtest_add_data_func_full(const char *str, void *data,
604 void (*fn)(const void *),
605 GDestroyNotify data_free_func);
606
45b0f830
AF
607/**
608 * qtest_add:
609 * @testpath: Test case path
610 * @Fixture: Fixture type
611 * @tdata: Test case data
612 * @fsetup: Test case setup function
613 * @ftest: Test case function
614 * @fteardown: Test case teardown function
615 *
616 * Add a GTester testcase with the given name, data and functions.
617 * The path is prefixed with the architecture under test, as
618 * returned by qtest_get_arch().
619 */
620#define qtest_add(testpath, Fixture, tdata, fsetup, ftest, fteardown) \
621 do { \
622 char *path = g_strdup_printf("/%s/%s", qtest_get_arch(), testpath); \
623 g_test_add(path, Fixture, tdata, fsetup, ftest, fteardown); \
624 g_free(path); \
625 } while (0)
626
041088c7 627void qtest_add_abrt_handler(GHookFunc fn, const void *data);
063c23d9 628
49ee3590
AL
629/**
630 * qtest_start:
631 * @args: other arguments to pass to QEMU
632 *
6acf801d
AF
633 * Start QEMU and assign the resulting #QTestState to a global variable.
634 * The global variable is used by "shortcut" functions documented below.
635 *
636 * Returns: #QTestState instance.
49ee3590 637 */
6acf801d
AF
638static inline QTestState *qtest_start(const char *args)
639{
96b8ca47
SH
640 global_qtest = qtest_init(args);
641 return global_qtest;
6acf801d 642}
49ee3590 643
1d9358e6
MA
644/**
645 * qtest_end:
646 *
647 * Shut down the QEMU process started by qtest_start().
648 */
649static inline void qtest_end(void)
650{
fc281c80
EGE
651 if (!global_qtest) {
652 return;
653 }
1d9358e6 654 qtest_quit(global_qtest);
96b8ca47 655 global_qtest = NULL;
1d9358e6
MA
656}
657
0c460dac
SH
658/**
659 * qmp:
bb340eb2 660 * @fmt...: QMP message to send to qemu, formatted like
6ce80fd8
MA
661 * qobject_from_jsonf_nofail(). See parse_escape() for what's
662 * supported after '%'.
0c460dac
SH
663 *
664 * Sends a QMP message to QEMU and returns the response.
665 */
e3dc93be 666QDict *qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
0c460dac 667
8fe941f7 668/**
6fc9f3d3
TH
669 * qtest_qmp_assert_success:
670 * @qts: QTestState instance to operate on
edbe36ad
KW
671 * @fmt...: QMP message to send to qemu, formatted like
672 * qobject_from_jsonf_nofail(). See parse_escape() for what's
673 * supported after '%'.
674 *
675 * Sends a QMP message to QEMU and asserts that a 'return' key is present in
676 * the response.
677 */
6fc9f3d3
TH
678void qtest_qmp_assert_success(QTestState *qts, const char *fmt, ...)
679 GCC_FMT_ATTR(2, 3);
edbe36ad
KW
680
681/*
8fe941f7
JS
682 * qmp_eventwait:
683 * @s: #event event to wait for.
684 *
e8ec0117 685 * Continuously polls for QMP responses until it receives the desired event.
8fe941f7
JS
686 */
687static inline void qmp_eventwait(const char *event)
688{
689 return qtest_qmp_eventwait(global_qtest, event);
690}
691
49ee3590
AL
692/**
693 * get_irq:
694 * @num: Interrupt to observe.
695 *
6acf801d 696 * Returns: The level of the @num interrupt.
49ee3590 697 */
6acf801d
AF
698static inline bool get_irq(int num)
699{
700 return qtest_get_irq(global_qtest, num);
701}
49ee3590 702
49ee3590
AL
703/**
704 * outb:
705 * @addr: I/O port to write to.
706 * @value: Value being written.
707 *
708 * Write an 8-bit value to an I/O port.
709 */
6acf801d
AF
710static inline void outb(uint16_t addr, uint8_t value)
711{
712 qtest_outb(global_qtest, addr, value);
713}
49ee3590
AL
714
715/**
716 * outw:
717 * @addr: I/O port to write to.
718 * @value: Value being written.
719 *
720 * Write a 16-bit value to an I/O port.
721 */
6acf801d
AF
722static inline void outw(uint16_t addr, uint16_t value)
723{
724 qtest_outw(global_qtest, addr, value);
725}
49ee3590
AL
726
727/**
728 * outl:
729 * @addr: I/O port to write to.
730 * @value: Value being written.
731 *
732 * Write a 32-bit value to an I/O port.
733 */
6acf801d
AF
734static inline void outl(uint16_t addr, uint32_t value)
735{
736 qtest_outl(global_qtest, addr, value);
737}
49ee3590
AL
738
739/**
740 * inb:
741 * @addr: I/O port to read from.
49ee3590 742 *
6acf801d
AF
743 * Reads an 8-bit value from an I/O port.
744 *
745 * Returns: Value read.
49ee3590 746 */
6acf801d
AF
747static inline uint8_t inb(uint16_t addr)
748{
749 return qtest_inb(global_qtest, addr);
750}
49ee3590
AL
751
752/**
753 * inw:
754 * @addr: I/O port to read from.
49ee3590 755 *
6acf801d
AF
756 * Reads a 16-bit value from an I/O port.
757 *
758 * Returns: Value read.
49ee3590 759 */
6acf801d
AF
760static inline uint16_t inw(uint16_t addr)
761{
762 return qtest_inw(global_qtest, addr);
763}
49ee3590
AL
764
765/**
766 * inl:
767 * @addr: I/O port to read from.
49ee3590 768 *
6acf801d
AF
769 * Reads a 32-bit value from an I/O port.
770 *
771 * Returns: Value read.
49ee3590 772 */
6acf801d
AF
773static inline uint32_t inl(uint16_t addr)
774{
775 return qtest_inl(global_qtest, addr);
776}
49ee3590 777
872536bf
AF
778/**
779 * writeb:
780 * @addr: Guest address to write to.
781 * @value: Value being written.
782 *
783 * Writes an 8-bit value to guest memory.
784 */
785static inline void writeb(uint64_t addr, uint8_t value)
786{
787 qtest_writeb(global_qtest, addr, value);
788}
789
790/**
791 * writew:
792 * @addr: Guest address to write to.
793 * @value: Value being written.
794 *
795 * Writes a 16-bit value to guest memory.
796 */
797static inline void writew(uint64_t addr, uint16_t value)
798{
799 qtest_writew(global_qtest, addr, value);
800}
801
802/**
803 * writel:
804 * @addr: Guest address to write to.
805 * @value: Value being written.
806 *
807 * Writes a 32-bit value to guest memory.
808 */
809static inline void writel(uint64_t addr, uint32_t value)
810{
811 qtest_writel(global_qtest, addr, value);
812}
813
814/**
815 * writeq:
816 * @addr: Guest address to write to.
817 * @value: Value being written.
818 *
819 * Writes a 64-bit value to guest memory.
820 */
821static inline void writeq(uint64_t addr, uint64_t value)
822{
823 qtest_writeq(global_qtest, addr, value);
824}
825
826/**
827 * readb:
828 * @addr: Guest address to read from.
829 *
830 * Reads an 8-bit value from guest memory.
831 *
832 * Returns: Value read.
833 */
834static inline uint8_t readb(uint64_t addr)
835{
836 return qtest_readb(global_qtest, addr);
837}
838
839/**
840 * readw:
841 * @addr: Guest address to read from.
842 *
843 * Reads a 16-bit value from guest memory.
844 *
845 * Returns: Value read.
846 */
847static inline uint16_t readw(uint64_t addr)
848{
849 return qtest_readw(global_qtest, addr);
850}
851
852/**
853 * readl:
854 * @addr: Guest address to read from.
855 *
856 * Reads a 32-bit value from guest memory.
857 *
858 * Returns: Value read.
859 */
860static inline uint32_t readl(uint64_t addr)
861{
862 return qtest_readl(global_qtest, addr);
863}
864
865/**
866 * readq:
867 * @addr: Guest address to read from.
868 *
869 * Reads a 64-bit value from guest memory.
870 *
871 * Returns: Value read.
872 */
873static inline uint64_t readq(uint64_t addr)
874{
875 return qtest_readq(global_qtest, addr);
876}
877
49ee3590
AL
878/**
879 * memread:
880 * @addr: Guest address to read from.
881 * @data: Pointer to where memory contents will be stored.
882 * @size: Number of bytes to read.
883 *
884 * Read guest memory into a buffer.
885 */
6acf801d
AF
886static inline void memread(uint64_t addr, void *data, size_t size)
887{
888 qtest_memread(global_qtest, addr, data, size);
889}
49ee3590
AL
890
891/**
892 * memwrite:
893 * @addr: Guest address to write to.
894 * @data: Pointer to the bytes that will be written to guest memory.
895 * @size: Number of bytes to write.
896 *
897 * Write a buffer to guest memory.
898 */
6acf801d
AF
899static inline void memwrite(uint64_t addr, const void *data, size_t size)
900{
901 qtest_memwrite(global_qtest, addr, data, size);
902}
49ee3590
AL
903
904/**
905 * clock_step_next:
906 *
bc72ad67 907 * Advance the QEMU_CLOCK_VIRTUAL to the next deadline.
6acf801d 908 *
bc72ad67 909 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590 910 */
6acf801d
AF
911static inline int64_t clock_step_next(void)
912{
913 return qtest_clock_step_next(global_qtest);
914}
49ee3590
AL
915
916/**
917 * clock_step:
918 * @step: Number of nanoseconds to advance the clock by.
919 *
bc72ad67 920 * Advance the QEMU_CLOCK_VIRTUAL by @step nanoseconds.
6acf801d 921 *
bc72ad67 922 * Returns: The current value of the QEMU_CLOCK_VIRTUAL in nanoseconds.
49ee3590 923 */
6acf801d
AF
924static inline int64_t clock_step(int64_t step)
925{
926 return qtest_clock_step(global_qtest, step);
927}
49ee3590 928
dc47995e 929QDict *qmp_fd_receive(int fd);
24d5588c
YK
930void qmp_fd_vsend_fds(int fd, int *fds, size_t fds_num,
931 const char *fmt, va_list ap) GCC_FMT_ATTR(4, 0);
e3dc93be
MA
932void qmp_fd_vsend(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
933void qmp_fd_send(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
e2f64a68
MA
934void qmp_fd_send_raw(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
935void qmp_fd_vsend_raw(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
e3dc93be
MA
936QDict *qmp_fdv(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
937QDict *qmp_fd(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
dc47995e 938
02ef6e87
TH
939/**
940 * qtest_cb_for_every_machine:
941 * @cb: Pointer to the callback function
1f4a0d81 942 * @skip_old_versioned: true if versioned old machine types should be skipped
02ef6e87
TH
943 *
944 * Call a callback function for every name of all available machines.
945 */
1f4a0d81
TH
946void qtest_cb_for_every_machine(void (*cb)(const char *machine),
947 bool skip_old_versioned);
02ef6e87 948
acd80015
TH
949/**
950 * qtest_qmp_device_add:
e5758de4 951 * @qts: QTestState instance to operate on
acd80015
TH
952 * @driver: Name of the device that should be added
953 * @id: Identification string
82cab70b
MA
954 * @fmt...: QMP message to send to qemu, formatted like
955 * qobject_from_jsonf_nofail(). See parse_escape() for what's
956 * supported after '%'.
acd80015
TH
957 *
958 * Generic hot-plugging test via the device_add QMP command.
959 */
e5758de4
TH
960void qtest_qmp_device_add(QTestState *qts, const char *driver, const char *id,
961 const char *fmt, ...) GCC_FMT_ATTR(4, 5);
acd80015
TH
962
963/**
964 * qtest_qmp_device_del:
e5758de4 965 * @qts: QTestState instance to operate on
acd80015
TH
966 * @id: Identification string
967 *
968 * Generic hot-unplugging test via the device_del QMP command.
969 */
e5758de4 970void qtest_qmp_device_del(QTestState *qts, const char *id);
acd80015 971
c35665e1
IM
972/**
973 * qmp_rsp_is_err:
974 * @rsp: QMP response to check for error
975 *
976 * Test @rsp for error and discard @rsp.
977 * Returns 'true' if there is error in @rsp and 'false' otherwise.
978 */
979bool qmp_rsp_is_err(QDict *rsp);
980
ebb4d82d
MAL
981/**
982 * qmp_assert_error_class:
983 * @rsp: QMP response to check for error
984 * @class: an error class
985 *
986 * Assert the response has the given error class and discard @rsp.
987 */
988void qmp_assert_error_class(QDict *rsp, const char *class);
989
21f80286
RH
990/**
991 * qtest_probe_child:
992 * @s: QTestState instance to operate on.
993 *
994 * Returns: true if the child is still alive.
995 */
996bool qtest_probe_child(QTestState *s);
997
49ee3590 998#endif