4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
17 #include "sysemu/qtest.h"
18 #include "sysemu/runstate.h"
19 #include "chardev/char-fe.h"
20 #include "exec/ioport.h"
21 #include "exec/memory.h"
22 #include "hw/qdev-core.h"
24 #include "qemu/accel.h"
25 #include "sysemu/cpu-timers.h"
26 #include "qemu/config-file.h"
27 #include "qemu/option.h"
28 #include "qemu/error-report.h"
29 #include "qemu/module.h"
30 #include "qemu/cutils.h"
31 #include "qom/object_interfaces.h"
32 #include CONFIG_DEVICES
34 #include "hw/ppc/spapr_rtas.h"
39 #define TYPE_QTEST "qtest"
41 OBJECT_DECLARE_SIMPLE_TYPE(QTest
, QTEST
)
46 bool has_machine_link
;
49 CharBackend qtest_chr
;
55 static DeviceState
*irq_intercept_dev
;
56 static FILE *qtest_log_fp
;
58 static GString
*inbuf
;
59 static int irq_levels
[MAX_IRQ
];
61 static bool qtest_opened
;
62 static void (*qtest_server_send
)(void*, const char*);
63 static void *qtest_server_send_opaque
;
65 #define FMT_timeval "%.06f"
70 * Line based protocol, request/response based. Server can send async messages
71 * so clients should always handle many async messages before the response
80 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
81 * let you adjust the value of the clock (monotonically). All the commands
82 * return the current value of the clock in nanoseconds.
84 * .. code-block:: none
89 * Advance the clock to the next deadline. Useful when waiting for
90 * asynchronous events.
92 * .. code-block:: none
97 * Advance the clock by NS nanoseconds.
99 * .. code-block:: none
104 * Advance the clock to NS nanoseconds (do nothing if it's already past).
106 * PIO and memory access:
107 * """"""""""""""""""""""
109 * .. code-block:: none
114 * .. code-block:: none
119 * .. code-block:: none
124 * .. code-block:: none
129 * .. code-block:: none
134 * .. code-block:: none
139 * .. code-block:: none
141 * > writeb ADDR VALUE
144 * .. code-block:: none
146 * > writew ADDR VALUE
149 * .. code-block:: none
151 * > writel ADDR VALUE
154 * .. code-block:: none
156 * > writeq ADDR VALUE
159 * .. code-block:: none
164 * .. code-block:: none
169 * .. code-block:: none
174 * .. code-block:: none
179 * .. code-block:: none
184 * .. code-block:: none
186 * > write ADDR SIZE DATA
189 * .. code-block:: none
191 * > b64read ADDR SIZE
194 * .. code-block:: none
196 * > b64write ADDR SIZE B64_DATA
199 * .. code-block:: none
201 * > memset ADDR SIZE VALUE
204 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
205 * For 'memset' a zero size is permitted and does nothing.
207 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
208 * than the expected size, the value will be zero filled at the end of the data
211 * B64_DATA is an arbitrarily long base64 encoded string.
212 * If the sizes do not match, the data will be truncated.
217 * .. code-block:: none
219 * > irq_intercept_in QOM-PATH
222 * .. code-block:: none
224 * > irq_intercept_out QOM-PATH
227 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
228 * QOM-PATH. When the pin is triggered, one of the following async messages
229 * will be printed to the qtest stream::
234 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
235 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
236 * NUM=0 even though it is remapped to GSI 2).
238 * Setting interrupt level:
239 * """"""""""""""""""""""""
241 * .. code-block:: none
243 * > set_irq_in QOM-PATH NAME NUM LEVEL
246 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
247 * LEVEL is an signed integer IRQ level.
249 * Forcibly set the given interrupt pin to the given level.
253 static int hex2nib(char ch
)
255 if (ch
>= '0' && ch
<= '9') {
257 } else if (ch
>= 'a' && ch
<= 'f') {
258 return 10 + (ch
- 'a');
259 } else if (ch
>= 'A' && ch
<= 'F') {
260 return 10 + (ch
- 'A');
266 static void qtest_send_prefix(CharBackend
*chr
)
268 if (!qtest_log_fp
|| !qtest_opened
) {
272 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ", g_timer_elapsed(timer
, NULL
));
275 static void G_GNUC_PRINTF(1, 2) qtest_log_send(const char *fmt
, ...)
279 if (!qtest_log_fp
|| !qtest_opened
) {
283 qtest_send_prefix(NULL
);
286 vfprintf(qtest_log_fp
, fmt
, ap
);
290 static void qtest_server_char_be_send(void *opaque
, const char *str
)
292 size_t len
= strlen(str
);
293 CharBackend
* chr
= (CharBackend
*)opaque
;
294 qemu_chr_fe_write_all(chr
, (uint8_t *)str
, len
);
295 if (qtest_log_fp
&& qtest_opened
) {
296 fprintf(qtest_log_fp
, "%s", str
);
300 static void qtest_send(CharBackend
*chr
, const char *str
)
302 qtest_server_send(qtest_server_send_opaque
, str
);
305 static void G_GNUC_PRINTF(2, 3) qtest_sendf(CharBackend
*chr
,
306 const char *fmt
, ...)
312 buffer
= g_strdup_vprintf(fmt
, ap
);
313 qtest_send(chr
, buffer
);
318 static void qtest_irq_handler(void *opaque
, int n
, int level
)
320 qemu_irq old_irq
= *(qemu_irq
*)opaque
;
321 qemu_set_irq(old_irq
, level
);
323 if (irq_levels
[n
] != level
) {
324 CharBackend
*chr
= &qtest
->qtest_chr
;
325 irq_levels
[n
] = level
;
326 qtest_send_prefix(chr
);
327 qtest_sendf(chr
, "IRQ %s %d\n",
328 level
? "raise" : "lower", n
);
332 static int64_t qtest_clock_counter
;
334 int64_t qtest_get_virtual_clock(void)
336 return qatomic_read_i64(&qtest_clock_counter
);
339 static void qtest_set_virtual_clock(int64_t count
)
341 qatomic_set_i64(&qtest_clock_counter
, count
);
344 static void qtest_clock_warp(int64_t dest
)
346 int64_t clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
347 AioContext
*aio_context
;
348 assert(qtest_enabled());
349 aio_context
= qemu_get_aio_context();
350 while (clock
< dest
) {
351 int64_t deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
352 QEMU_TIMER_ATTR_ALL
);
353 int64_t warp
= qemu_soonest_timeout(dest
- clock
, deadline
);
355 qtest_set_virtual_clock(qtest_get_virtual_clock() + warp
);
357 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
358 timerlist_run_timers(aio_context
->tlg
.tl
[QEMU_CLOCK_VIRTUAL
]);
359 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
361 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
364 static void qtest_process_command(CharBackend
*chr
, gchar
**words
)
366 const gchar
*command
;
375 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]", g_timer_elapsed(timer
, NULL
));
376 for (i
= 0; words
[i
]; i
++) {
377 fprintf(qtest_log_fp
, " %s", words
[i
]);
379 fprintf(qtest_log_fp
, "\n");
383 if (strcmp(words
[0], "irq_intercept_out") == 0
384 || strcmp(words
[0], "irq_intercept_in") == 0) {
389 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
391 qtest_send_prefix(chr
);
392 qtest_send(chr
, "FAIL Unknown device\n");
396 if (irq_intercept_dev
) {
397 qtest_send_prefix(chr
);
398 if (irq_intercept_dev
!= dev
) {
399 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
401 qtest_send(chr
, "OK\n");
406 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
407 /* We don't support intercept of named GPIOs yet */
411 if (words
[0][14] == 'o') {
413 for (i
= 0; i
< ngl
->num_out
; ++i
) {
414 qemu_irq
*disconnected
= g_new0(qemu_irq
, 1);
415 qemu_irq icpt
= qemu_allocate_irq(qtest_irq_handler
,
418 *disconnected
= qdev_intercept_gpio_out(dev
, icpt
,
422 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
426 irq_intercept_dev
= dev
;
427 qtest_send_prefix(chr
);
428 qtest_send(chr
, "OK\n");
429 } else if (strcmp(words
[0], "set_irq_in") == 0) {
437 g_assert(words
[1] && words
[2] && words
[3] && words
[4]);
439 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
441 qtest_send_prefix(chr
);
442 qtest_send(chr
, "FAIL Unknown device\n");
446 if (strcmp(words
[2], "unnamed-gpio-in") == 0) {
452 ret
= qemu_strtoi(words
[3], NULL
, 0, &num
);
454 ret
= qemu_strtoi(words
[4], NULL
, 0, &level
);
457 irq
= qdev_get_gpio_in_named(dev
, name
, num
);
459 qemu_set_irq(irq
, level
);
460 qtest_send_prefix(chr
);
461 qtest_send(chr
, "OK\n");
462 } else if (strcmp(words
[0], "outb") == 0 ||
463 strcmp(words
[0], "outw") == 0 ||
464 strcmp(words
[0], "outl") == 0) {
469 g_assert(words
[1] && words
[2]);
470 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
472 ret
= qemu_strtoul(words
[2], NULL
, 0, &value
);
474 g_assert(addr
<= 0xffff);
476 if (words
[0][3] == 'b') {
477 cpu_outb(addr
, value
);
478 } else if (words
[0][3] == 'w') {
479 cpu_outw(addr
, value
);
480 } else if (words
[0][3] == 'l') {
481 cpu_outl(addr
, value
);
483 qtest_send_prefix(chr
);
484 qtest_send(chr
, "OK\n");
485 } else if (strcmp(words
[0], "inb") == 0 ||
486 strcmp(words
[0], "inw") == 0 ||
487 strcmp(words
[0], "inl") == 0) {
489 uint32_t value
= -1U;
493 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
495 g_assert(addr
<= 0xffff);
497 if (words
[0][2] == 'b') {
498 value
= cpu_inb(addr
);
499 } else if (words
[0][2] == 'w') {
500 value
= cpu_inw(addr
);
501 } else if (words
[0][2] == 'l') {
502 value
= cpu_inl(addr
);
504 qtest_send_prefix(chr
);
505 qtest_sendf(chr
, "OK 0x%04x\n", value
);
506 } else if (strcmp(words
[0], "writeb") == 0 ||
507 strcmp(words
[0], "writew") == 0 ||
508 strcmp(words
[0], "writel") == 0 ||
509 strcmp(words
[0], "writeq") == 0) {
514 g_assert(words
[1] && words
[2]);
515 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
517 ret
= qemu_strtou64(words
[2], NULL
, 0, &value
);
520 if (words
[0][5] == 'b') {
521 uint8_t data
= value
;
522 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
524 } else if (words
[0][5] == 'w') {
525 uint16_t data
= value
;
527 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
529 } else if (words
[0][5] == 'l') {
530 uint32_t data
= value
;
532 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
534 } else if (words
[0][5] == 'q') {
535 uint64_t data
= value
;
537 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
540 qtest_send_prefix(chr
);
541 qtest_send(chr
, "OK\n");
542 } else if (strcmp(words
[0], "readb") == 0 ||
543 strcmp(words
[0], "readw") == 0 ||
544 strcmp(words
[0], "readl") == 0 ||
545 strcmp(words
[0], "readq") == 0) {
547 uint64_t value
= UINT64_C(-1);
551 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
554 if (words
[0][4] == 'b') {
556 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
559 } else if (words
[0][4] == 'w') {
561 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
563 value
= tswap16(data
);
564 } else if (words
[0][4] == 'l') {
566 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
568 value
= tswap32(data
);
569 } else if (words
[0][4] == 'q') {
570 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
574 qtest_send_prefix(chr
);
575 qtest_sendf(chr
, "OK 0x%016" PRIx64
"\n", value
);
576 } else if (strcmp(words
[0], "read") == 0) {
577 uint64_t addr
, len
, i
;
582 g_assert(words
[1] && words
[2]);
583 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
585 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
587 /* We'd send garbage to libqtest if len is 0 */
590 data
= g_malloc(len
);
591 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
594 enc
= g_malloc(2 * len
+ 1);
595 for (i
= 0; i
< len
; i
++) {
596 sprintf(&enc
[i
* 2], "%02x", data
[i
]);
599 qtest_send_prefix(chr
);
600 qtest_sendf(chr
, "OK 0x%s\n", enc
);
604 } else if (strcmp(words
[0], "b64read") == 0) {
610 g_assert(words
[1] && words
[2]);
611 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
613 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
616 data
= g_malloc(len
);
617 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
619 b64_data
= g_base64_encode(data
, len
);
620 qtest_send_prefix(chr
);
621 qtest_sendf(chr
, "OK %s\n", b64_data
);
625 } else if (strcmp(words
[0], "write") == 0) {
626 uint64_t addr
, len
, i
;
631 g_assert(words
[1] && words
[2] && words
[3]);
632 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
634 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
637 data_len
= strlen(words
[3]);
639 qtest_send(chr
, "ERR invalid argument size\n");
643 data
= g_malloc(len
);
644 for (i
= 0; i
< len
; i
++) {
645 if ((i
* 2 + 4) <= data_len
) {
646 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
647 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
652 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
656 qtest_send_prefix(chr
);
657 qtest_send(chr
, "OK\n");
658 } else if (strcmp(words
[0], "memset") == 0) {
661 unsigned long pattern
;
664 g_assert(words
[1] && words
[2] && words
[3]);
665 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
667 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
669 ret
= qemu_strtoul(words
[3], NULL
, 0, &pattern
);
673 data
= g_malloc(len
);
674 memset(data
, pattern
, len
);
675 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
680 qtest_send_prefix(chr
);
681 qtest_send(chr
, "OK\n");
682 } else if (strcmp(words
[0], "b64write") == 0) {
689 g_assert(words
[1] && words
[2] && words
[3]);
690 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
692 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
695 data_len
= strlen(words
[3]);
697 qtest_send(chr
, "ERR invalid argument size\n");
701 data
= g_base64_decode_inplace(words
[3], &out_len
);
702 if (out_len
!= len
) {
703 qtest_log_send("b64write: data length mismatch (told %"PRIu64
", "
706 out_len
= MIN(out_len
, len
);
709 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
712 qtest_send_prefix(chr
);
713 qtest_send(chr
, "OK\n");
714 } else if (strcmp(words
[0], "endianness") == 0) {
715 qtest_send_prefix(chr
);
716 #if TARGET_BIG_ENDIAN
717 qtest_sendf(chr
, "OK big\n");
719 qtest_sendf(chr
, "OK little\n");
721 #ifdef CONFIG_PSERIES
722 } else if (strcmp(words
[0], "rtas") == 0) {
723 uint64_t res
, args
, ret
;
724 unsigned long nargs
, nret
;
727 rc
= qemu_strtoul(words
[2], NULL
, 0, &nargs
);
729 rc
= qemu_strtou64(words
[3], NULL
, 0, &args
);
731 rc
= qemu_strtoul(words
[4], NULL
, 0, &nret
);
733 rc
= qemu_strtou64(words
[5], NULL
, 0, &ret
);
735 res
= qtest_rtas_call(words
[1], nargs
, args
, nret
, ret
);
737 qtest_send_prefix(chr
);
738 qtest_sendf(chr
, "OK %"PRIu64
"\n", res
);
740 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
744 int ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
747 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
748 QEMU_TIMER_ATTR_ALL
);
750 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ns
);
751 qtest_send_prefix(chr
);
752 qtest_sendf(chr
, "OK %"PRIi64
"\n",
753 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
754 } else if (strcmp(words
[0], "module_load") == 0) {
755 Error
*local_err
= NULL
;
757 g_assert(words
[1] && words
[2]);
759 qtest_send_prefix(chr
);
760 rv
= module_load(words
[1], words
[2], &local_err
);
762 qtest_sendf(chr
, "OK\n");
765 error_report_err(local_err
);
767 qtest_sendf(chr
, "FAIL\n");
769 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
774 ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
776 qtest_clock_warp(ns
);
777 qtest_send_prefix(chr
);
778 qtest_sendf(chr
, "OK %"PRIi64
"\n",
779 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
781 qtest_send_prefix(chr
);
782 qtest_sendf(chr
, "FAIL Unknown command '%s'\n", words
[0]);
786 static void qtest_process_inbuf(CharBackend
*chr
, GString
*inbuf
)
790 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
795 offset
= end
- inbuf
->str
;
797 cmd
= g_string_new_len(inbuf
->str
, offset
);
798 g_string_erase(inbuf
, 0, offset
+ 1);
800 words
= g_strsplit(cmd
->str
, " ", 0);
801 qtest_process_command(chr
, words
);
804 g_string_free(cmd
, TRUE
);
808 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
810 CharBackend
*chr
= opaque
;
812 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
813 qtest_process_inbuf(chr
, inbuf
);
816 static int qtest_can_read(void *opaque
)
821 static void qtest_event(void *opaque
, QEMUChrEvent event
)
826 case CHR_EVENT_OPENED
:
828 * We used to call qemu_system_reset() here, hoping we could
829 * use the same process for multiple tests that way. Never
830 * used. Injects an extra reset even when it's not used, and
831 * that can mess up tests, e.g. -boot once.
833 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
837 g_clear_pointer(&timer
, g_timer_destroy
);
838 timer
= g_timer_new();
841 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n", g_timer_elapsed(timer
, NULL
));
844 case CHR_EVENT_CLOSED
:
845 qtest_opened
= false;
847 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n", g_timer_elapsed(timer
, NULL
));
849 g_clear_pointer(&timer
, g_timer_destroy
);
856 void qtest_server_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
862 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
864 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
869 qtest
= object_new(TYPE_QTEST
);
870 object_property_set_str(qtest
, "chardev", "qtest", &error_abort
);
872 object_property_set_str(qtest
, "log", qtest_log
, &error_abort
);
874 object_property_add_child(qdev_get_machine(), "qtest", qtest
);
875 user_creatable_complete(USER_CREATABLE(qtest
), errp
);
877 object_unparent(qtest
);
879 object_unref(OBJECT(chr
));
883 static bool qtest_server_start(QTest
*q
, Error
**errp
)
885 Chardev
*chr
= q
->chr
;
886 const char *qtest_log
= q
->log
;
889 if (strcmp(qtest_log
, "none") != 0) {
890 qtest_log_fp
= fopen(qtest_log
, "w+");
893 qtest_log_fp
= stderr
;
896 if (!qemu_chr_fe_init(&q
->qtest_chr
, chr
, errp
)) {
899 qemu_chr_fe_set_handlers(&q
->qtest_chr
, qtest_can_read
, qtest_read
,
900 qtest_event
, NULL
, &q
->qtest_chr
, NULL
, true);
901 qemu_chr_fe_set_echo(&q
->qtest_chr
, true);
903 inbuf
= g_string_new("");
905 if (!qtest_server_send
) {
906 qtest_server_set_send_handler(qtest_server_char_be_send
, &q
->qtest_chr
);
912 void qtest_server_set_send_handler(void (*send
)(void*, const char*),
915 qtest_server_send
= send
;
916 qtest_server_send_opaque
= opaque
;
919 bool qtest_driver(void)
921 return qtest
&& qtest
->qtest_chr
.chr
!= NULL
;
924 void qtest_server_inproc_recv(void *dummy
, const char *buf
)
926 static GString
*gstr
;
928 gstr
= g_string_new(NULL
);
930 g_string_append(gstr
, buf
);
931 if (gstr
->str
[gstr
->len
- 1] == '\n') {
932 qtest_process_inbuf(NULL
, gstr
);
933 g_string_truncate(gstr
, 0);
937 static void qtest_complete(UserCreatable
*uc
, Error
**errp
)
939 QTest
*q
= QTEST(uc
);
941 error_setg(errp
, "Only one instance of qtest can be created");
945 error_setg(errp
, "No backend specified");
949 if (OBJECT(uc
)->parent
!= qdev_get_machine()) {
950 q
->has_machine_link
= true;
951 object_property_add_const_link(qdev_get_machine(), "qtest", OBJECT(uc
));
953 /* -qtest was used. */
956 qtest_server_start(q
, errp
);
959 static void qtest_unparent(Object
*obj
)
961 QTest
*q
= QTEST(obj
);
964 qemu_chr_fe_disconnect(&q
->qtest_chr
);
965 assert(!qtest_opened
);
966 qemu_chr_fe_deinit(&q
->qtest_chr
, false);
968 fclose(qtest_log_fp
);
974 if (q
->has_machine_link
) {
975 object_property_del(qdev_get_machine(), "qtest");
976 q
->has_machine_link
= false;
980 static void qtest_set_log(Object
*obj
, const char *value
, Error
**errp
)
982 QTest
*q
= QTEST(obj
);
985 error_setg(errp
, "Property 'log' can not be set now");
988 q
->log
= g_strdup(value
);
992 static char *qtest_get_log(Object
*obj
, Error
**errp
)
994 QTest
*q
= QTEST(obj
);
996 return g_strdup(q
->log
);
999 static void qtest_set_chardev(Object
*obj
, const char *value
, Error
**errp
)
1001 QTest
*q
= QTEST(obj
);
1005 error_setg(errp
, "Property 'chardev' can not be set now");
1009 chr
= qemu_chr_find(value
);
1011 error_setg(errp
, "Cannot find character device '%s'", value
);
1015 g_free(q
->chr_name
);
1016 q
->chr_name
= g_strdup(value
);
1019 object_unref(q
->chr
);
1025 static char *qtest_get_chardev(Object
*obj
, Error
**errp
)
1027 QTest
*q
= QTEST(obj
);
1029 return g_strdup(q
->chr_name
);
1032 static void qtest_class_init(ObjectClass
*oc
, void *data
)
1034 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
1036 oc
->unparent
= qtest_unparent
;
1037 ucc
->complete
= qtest_complete
;
1039 object_class_property_add_str(oc
, "chardev",
1040 qtest_get_chardev
, qtest_set_chardev
);
1041 object_class_property_add_str(oc
, "log",
1042 qtest_get_log
, qtest_set_log
);
1045 static const TypeInfo qtest_info
= {
1047 .parent
= TYPE_OBJECT
,
1048 .class_init
= qtest_class_init
,
1049 .instance_size
= sizeof(QTest
),
1050 .interfaces
= (InterfaceInfo
[]) {
1051 { TYPE_USER_CREATABLE
},
1056 static void register_types(void)
1058 type_register_static(&qtest_info
);
1061 type_init(register_types
);