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"
16 #include "sysemu/qtest.h"
17 #include "sysemu/runstate.h"
18 #include "chardev/char-fe.h"
19 #include "exec/ioport.h"
20 #include "exec/memory.h"
21 #include "exec/tswap.h"
22 #include "hw/qdev-core.h"
24 #include "hw/core/cpu.h"
25 #include "qemu/accel.h"
26 #include "sysemu/cpu-timers.h"
27 #include "qemu/config-file.h"
28 #include "qemu/option.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/cutils.h"
32 #include "qom/object_interfaces.h"
36 #define TYPE_QTEST "qtest"
38 OBJECT_DECLARE_SIMPLE_TYPE(QTest
, QTEST
)
43 bool has_machine_link
;
46 CharBackend qtest_chr
;
52 static DeviceState
*irq_intercept_dev
;
53 static FILE *qtest_log_fp
;
55 static GString
*inbuf
;
56 static int irq_levels
[MAX_IRQ
];
58 static bool qtest_opened
;
59 static void (*qtest_server_send
)(void*, const char*);
60 static void *qtest_server_send_opaque
;
62 #define FMT_timeval "%.06f"
67 * Line based protocol, request/response based. Server can send async messages
68 * so clients should always handle many async messages before the response
77 * The qtest client is completely in charge of the QEMU_CLOCK_VIRTUAL. qtest commands
78 * let you adjust the value of the clock (monotonically). All the commands
79 * return the current value of the clock in nanoseconds.
81 * .. code-block:: none
86 * Advance the clock to the next deadline. Useful when waiting for
87 * asynchronous events.
89 * .. code-block:: none
94 * Advance the clock by NS nanoseconds.
96 * .. code-block:: none
101 * Advance the clock to NS nanoseconds (do nothing if it's already past).
103 * PIO and memory access:
104 * """"""""""""""""""""""
106 * .. code-block:: none
111 * .. code-block:: none
116 * .. code-block:: none
121 * .. code-block:: none
126 * .. code-block:: none
131 * .. code-block:: none
136 * .. code-block:: none
138 * > writeb ADDR VALUE
141 * .. code-block:: none
143 * > writew ADDR VALUE
146 * .. code-block:: none
148 * > writel ADDR VALUE
151 * .. code-block:: none
153 * > writeq ADDR VALUE
156 * .. code-block:: none
161 * .. code-block:: none
166 * .. code-block:: none
171 * .. code-block:: none
176 * .. code-block:: none
181 * .. code-block:: none
183 * > write ADDR SIZE DATA
186 * .. code-block:: none
188 * > b64read ADDR SIZE
191 * .. code-block:: none
193 * > b64write ADDR SIZE B64_DATA
196 * .. code-block:: none
198 * > memset ADDR SIZE VALUE
201 * ADDR, SIZE, VALUE are all integers parsed with strtoul() with a base of 0.
202 * For 'memset' a zero size is permitted and does nothing.
204 * DATA is an arbitrarily long hex number prefixed with '0x'. If it's smaller
205 * than the expected size, the value will be zero filled at the end of the data
208 * B64_DATA is an arbitrarily long base64 encoded string.
209 * If the sizes do not match, the data will be truncated.
214 * .. code-block:: none
216 * > irq_intercept_in QOM-PATH
219 * .. code-block:: none
221 * > irq_intercept_out QOM-PATH
224 * Attach to the gpio-in (resp. gpio-out) pins exported by the device at
225 * QOM-PATH. When the pin is triggered, one of the following async messages
226 * will be printed to the qtest stream::
231 * where NUM is an IRQ number. For the PC, interrupts can be intercepted
232 * simply with "irq_intercept_in ioapic" (note that IRQ0 comes out with
233 * NUM=0 even though it is remapped to GSI 2).
235 * Setting interrupt level:
236 * """"""""""""""""""""""""
238 * .. code-block:: none
240 * > set_irq_in QOM-PATH NAME NUM LEVEL
243 * where NAME is the name of the irq/gpio list, NUM is an IRQ number and
244 * LEVEL is an signed integer IRQ level.
246 * Forcibly set the given interrupt pin to the given level.
250 static int hex2nib(char ch
)
252 if (ch
>= '0' && ch
<= '9') {
254 } else if (ch
>= 'a' && ch
<= 'f') {
255 return 10 + (ch
- 'a');
256 } else if (ch
>= 'A' && ch
<= 'F') {
257 return 10 + (ch
- 'A');
263 void qtest_send_prefix(CharBackend
*chr
)
265 if (!qtest_log_fp
|| !qtest_opened
) {
269 fprintf(qtest_log_fp
, "[S +" FMT_timeval
"] ", g_timer_elapsed(timer
, NULL
));
272 static void G_GNUC_PRINTF(1, 2) qtest_log_send(const char *fmt
, ...)
276 if (!qtest_log_fp
|| !qtest_opened
) {
280 qtest_send_prefix(NULL
);
283 vfprintf(qtest_log_fp
, fmt
, ap
);
287 static void qtest_server_char_be_send(void *opaque
, const char *str
)
289 size_t len
= strlen(str
);
290 CharBackend
* chr
= (CharBackend
*)opaque
;
291 qemu_chr_fe_write_all(chr
, (uint8_t *)str
, len
);
292 if (qtest_log_fp
&& qtest_opened
) {
293 fprintf(qtest_log_fp
, "%s", str
);
297 static void qtest_send(CharBackend
*chr
, const char *str
)
299 qtest_server_send(qtest_server_send_opaque
, str
);
302 void qtest_sendf(CharBackend
*chr
, const char *fmt
, ...)
308 buffer
= g_strdup_vprintf(fmt
, ap
);
309 qtest_send(chr
, buffer
);
314 static void qtest_irq_handler(void *opaque
, int n
, int level
)
316 qemu_irq old_irq
= *(qemu_irq
*)opaque
;
317 qemu_set_irq(old_irq
, level
);
319 if (irq_levels
[n
] != level
) {
320 CharBackend
*chr
= &qtest
->qtest_chr
;
321 irq_levels
[n
] = level
;
322 qtest_send_prefix(chr
);
323 qtest_sendf(chr
, "IRQ %s %d\n",
324 level
? "raise" : "lower", n
);
328 static int64_t qtest_clock_counter
;
330 int64_t qtest_get_virtual_clock(void)
332 return qatomic_read_i64(&qtest_clock_counter
);
335 static void qtest_set_virtual_clock(int64_t count
)
337 qatomic_set_i64(&qtest_clock_counter
, count
);
340 static void qtest_clock_warp(int64_t dest
)
342 int64_t clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
343 AioContext
*aio_context
;
344 assert(qtest_enabled());
345 aio_context
= qemu_get_aio_context();
346 while (clock
< dest
) {
347 int64_t deadline
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
348 QEMU_TIMER_ATTR_ALL
);
349 int64_t warp
= qemu_soonest_timeout(dest
- clock
, deadline
);
351 qtest_set_virtual_clock(qtest_get_virtual_clock() + warp
);
353 qemu_clock_run_timers(QEMU_CLOCK_VIRTUAL
);
354 timerlist_run_timers(aio_context
->tlg
.tl
[QEMU_CLOCK_VIRTUAL
]);
355 clock
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
357 qemu_clock_notify(QEMU_CLOCK_VIRTUAL
);
360 static bool (*process_command_cb
)(CharBackend
*chr
, gchar
**words
);
362 void qtest_set_command_cb(bool (*pc_cb
)(CharBackend
*chr
, gchar
**words
))
364 assert(!process_command_cb
); /* Switch to a list if we need more than one */
366 process_command_cb
= pc_cb
;
369 static void qtest_install_gpio_out_intercept(DeviceState
*dev
, const char *name
, int n
)
371 qemu_irq
*disconnected
= g_new0(qemu_irq
, 1);
372 qemu_irq icpt
= qemu_allocate_irq(qtest_irq_handler
,
375 *disconnected
= qdev_intercept_gpio_out(dev
, icpt
, name
, n
);
378 static void qtest_process_command(CharBackend
*chr
, gchar
**words
)
380 const gchar
*command
;
389 fprintf(qtest_log_fp
, "[R +" FMT_timeval
"]", g_timer_elapsed(timer
, NULL
));
390 for (i
= 0; words
[i
]; i
++) {
391 fprintf(qtest_log_fp
, " %s", words
[i
]);
393 fprintf(qtest_log_fp
, "\n");
397 if (strcmp(words
[0], "irq_intercept_out") == 0
398 || strcmp(words
[0], "irq_intercept_in") == 0) {
403 bool interception_succeeded
= false;
406 is_named
= words
[2] != NULL
;
407 is_outbound
= words
[0][14] == 'o';
408 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
410 qtest_send_prefix(chr
);
411 qtest_send(chr
, "FAIL Unknown device\n");
415 if (is_named
&& !is_outbound
) {
416 qtest_send_prefix(chr
);
417 qtest_send(chr
, "FAIL Interception of named in-GPIOs not yet supported\n");
421 if (irq_intercept_dev
) {
422 qtest_send_prefix(chr
);
423 if (irq_intercept_dev
!= dev
) {
424 qtest_send(chr
, "FAIL IRQ intercept already enabled\n");
426 qtest_send(chr
, "OK\n");
431 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
432 /* We don't support inbound interception of named GPIOs yet */
434 /* NULL is valid and matchable, for "unnamed GPIO" */
435 if (g_strcmp0(ngl
->name
, words
[2]) == 0) {
437 for (i
= 0; i
< ngl
->num_out
; ++i
) {
438 qtest_install_gpio_out_intercept(dev
, ngl
->name
, i
);
440 interception_succeeded
= true;
443 qemu_irq_intercept_in(ngl
->in
, qtest_irq_handler
,
445 interception_succeeded
= true;
449 qtest_send_prefix(chr
);
450 if (interception_succeeded
) {
451 irq_intercept_dev
= dev
;
452 qtest_send(chr
, "OK\n");
454 qtest_send(chr
, "FAIL No intercepts installed\n");
456 } else if (strcmp(words
[0], "set_irq_in") == 0) {
464 g_assert(words
[1] && words
[2] && words
[3] && words
[4]);
466 dev
= DEVICE(object_resolve_path(words
[1], NULL
));
468 qtest_send_prefix(chr
);
469 qtest_send(chr
, "FAIL Unknown device\n");
473 if (strcmp(words
[2], "unnamed-gpio-in") == 0) {
479 ret
= qemu_strtoi(words
[3], NULL
, 0, &num
);
481 ret
= qemu_strtoi(words
[4], NULL
, 0, &level
);
484 irq
= qdev_get_gpio_in_named(dev
, name
, num
);
486 qemu_set_irq(irq
, level
);
487 qtest_send_prefix(chr
);
488 qtest_send(chr
, "OK\n");
489 } else if (strcmp(words
[0], "outb") == 0 ||
490 strcmp(words
[0], "outw") == 0 ||
491 strcmp(words
[0], "outl") == 0) {
496 g_assert(words
[1] && words
[2]);
497 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
499 ret
= qemu_strtoul(words
[2], NULL
, 0, &value
);
501 g_assert(addr
<= 0xffff);
503 if (words
[0][3] == 'b') {
504 cpu_outb(addr
, value
);
505 } else if (words
[0][3] == 'w') {
506 cpu_outw(addr
, value
);
507 } else if (words
[0][3] == 'l') {
508 cpu_outl(addr
, value
);
510 qtest_send_prefix(chr
);
511 qtest_send(chr
, "OK\n");
512 } else if (strcmp(words
[0], "inb") == 0 ||
513 strcmp(words
[0], "inw") == 0 ||
514 strcmp(words
[0], "inl") == 0) {
516 uint32_t value
= -1U;
520 ret
= qemu_strtoul(words
[1], NULL
, 0, &addr
);
522 g_assert(addr
<= 0xffff);
524 if (words
[0][2] == 'b') {
525 value
= cpu_inb(addr
);
526 } else if (words
[0][2] == 'w') {
527 value
= cpu_inw(addr
);
528 } else if (words
[0][2] == 'l') {
529 value
= cpu_inl(addr
);
531 qtest_send_prefix(chr
);
532 qtest_sendf(chr
, "OK 0x%04x\n", value
);
533 } else if (strcmp(words
[0], "writeb") == 0 ||
534 strcmp(words
[0], "writew") == 0 ||
535 strcmp(words
[0], "writel") == 0 ||
536 strcmp(words
[0], "writeq") == 0) {
541 g_assert(words
[1] && words
[2]);
542 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
544 ret
= qemu_strtou64(words
[2], NULL
, 0, &value
);
547 if (words
[0][5] == 'b') {
548 uint8_t data
= value
;
549 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
551 } else if (words
[0][5] == 'w') {
552 uint16_t data
= value
;
554 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
556 } else if (words
[0][5] == 'l') {
557 uint32_t data
= value
;
559 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
561 } else if (words
[0][5] == 'q') {
562 uint64_t data
= value
;
564 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
567 qtest_send_prefix(chr
);
568 qtest_send(chr
, "OK\n");
569 } else if (strcmp(words
[0], "readb") == 0 ||
570 strcmp(words
[0], "readw") == 0 ||
571 strcmp(words
[0], "readl") == 0 ||
572 strcmp(words
[0], "readq") == 0) {
574 uint64_t value
= UINT64_C(-1);
578 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
581 if (words
[0][4] == 'b') {
583 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
586 } else if (words
[0][4] == 'w') {
588 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
590 value
= tswap16(data
);
591 } else if (words
[0][4] == 'l') {
593 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
595 value
= tswap32(data
);
596 } else if (words
[0][4] == 'q') {
597 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
601 qtest_send_prefix(chr
);
602 qtest_sendf(chr
, "OK 0x%016" PRIx64
"\n", value
);
603 } else if (strcmp(words
[0], "read") == 0) {
604 uint64_t addr
, len
, i
;
609 g_assert(words
[1] && words
[2]);
610 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
612 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
614 /* We'd send garbage to libqtest if len is 0 */
617 data
= g_malloc(len
);
618 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
621 enc
= g_malloc(2 * len
+ 1);
622 for (i
= 0; i
< len
; i
++) {
623 sprintf(&enc
[i
* 2], "%02x", data
[i
]);
626 qtest_send_prefix(chr
);
627 qtest_sendf(chr
, "OK 0x%s\n", enc
);
631 } else if (strcmp(words
[0], "b64read") == 0) {
637 g_assert(words
[1] && words
[2]);
638 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
640 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
643 data
= g_malloc(len
);
644 address_space_read(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
646 b64_data
= g_base64_encode(data
, len
);
647 qtest_send_prefix(chr
);
648 qtest_sendf(chr
, "OK %s\n", b64_data
);
652 } else if (strcmp(words
[0], "write") == 0) {
653 uint64_t addr
, len
, i
;
658 g_assert(words
[1] && words
[2] && words
[3]);
659 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
661 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
664 data_len
= strlen(words
[3]);
666 qtest_send(chr
, "ERR invalid argument size\n");
670 data
= g_malloc(len
);
671 for (i
= 0; i
< len
; i
++) {
672 if ((i
* 2 + 4) <= data_len
) {
673 data
[i
] = hex2nib(words
[3][i
* 2 + 2]) << 4;
674 data
[i
] |= hex2nib(words
[3][i
* 2 + 3]);
679 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
683 qtest_send_prefix(chr
);
684 qtest_send(chr
, "OK\n");
685 } else if (strcmp(words
[0], "memset") == 0) {
688 unsigned long pattern
;
691 g_assert(words
[1] && words
[2] && words
[3]);
692 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
694 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
696 ret
= qemu_strtoul(words
[3], NULL
, 0, &pattern
);
700 data
= g_malloc(len
);
701 memset(data
, pattern
, len
);
702 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
,
707 qtest_send_prefix(chr
);
708 qtest_send(chr
, "OK\n");
709 } else if (strcmp(words
[0], "b64write") == 0) {
716 g_assert(words
[1] && words
[2] && words
[3]);
717 ret
= qemu_strtou64(words
[1], NULL
, 0, &addr
);
719 ret
= qemu_strtou64(words
[2], NULL
, 0, &len
);
722 data_len
= strlen(words
[3]);
724 qtest_send(chr
, "ERR invalid argument size\n");
728 data
= g_base64_decode_inplace(words
[3], &out_len
);
729 if (out_len
!= len
) {
730 qtest_log_send("b64write: data length mismatch (told %"PRIu64
", "
733 out_len
= MIN(out_len
, len
);
736 address_space_write(first_cpu
->as
, addr
, MEMTXATTRS_UNSPECIFIED
, data
,
739 qtest_send_prefix(chr
);
740 qtest_send(chr
, "OK\n");
741 } else if (strcmp(words
[0], "endianness") == 0) {
742 qtest_send_prefix(chr
);
743 if (target_words_bigendian()) {
744 qtest_sendf(chr
, "OK big\n");
746 qtest_sendf(chr
, "OK little\n");
748 } else if (qtest_enabled() && strcmp(words
[0], "clock_step") == 0) {
752 int ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
755 ns
= qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL
,
756 QEMU_TIMER_ATTR_ALL
);
758 qtest_clock_warp(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + ns
);
759 qtest_send_prefix(chr
);
760 qtest_sendf(chr
, "OK %"PRIi64
"\n",
761 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
762 } else if (strcmp(words
[0], "module_load") == 0) {
763 Error
*local_err
= NULL
;
765 g_assert(words
[1] && words
[2]);
767 qtest_send_prefix(chr
);
768 rv
= module_load(words
[1], words
[2], &local_err
);
770 qtest_sendf(chr
, "OK\n");
773 error_report_err(local_err
);
775 qtest_sendf(chr
, "FAIL\n");
777 } else if (qtest_enabled() && strcmp(words
[0], "clock_set") == 0) {
782 ret
= qemu_strtoi64(words
[1], NULL
, 0, &ns
);
784 qtest_clock_warp(ns
);
785 qtest_send_prefix(chr
);
786 qtest_sendf(chr
, "OK %"PRIi64
"\n",
787 (int64_t)qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
));
788 } else if (process_command_cb
&& process_command_cb(chr
, words
)) {
789 /* Command got consumed by the callback handler */
791 qtest_send_prefix(chr
);
792 qtest_sendf(chr
, "FAIL Unknown command '%s'\n", words
[0]);
796 static void qtest_process_inbuf(CharBackend
*chr
, GString
*inbuf
)
800 while ((end
= strchr(inbuf
->str
, '\n')) != NULL
) {
805 offset
= end
- inbuf
->str
;
807 cmd
= g_string_new_len(inbuf
->str
, offset
);
808 g_string_erase(inbuf
, 0, offset
+ 1);
810 words
= g_strsplit(cmd
->str
, " ", 0);
811 qtest_process_command(chr
, words
);
814 g_string_free(cmd
, TRUE
);
818 static void qtest_read(void *opaque
, const uint8_t *buf
, int size
)
820 CharBackend
*chr
= opaque
;
822 g_string_append_len(inbuf
, (const gchar
*)buf
, size
);
823 qtest_process_inbuf(chr
, inbuf
);
826 static int qtest_can_read(void *opaque
)
831 static void qtest_event(void *opaque
, QEMUChrEvent event
)
836 case CHR_EVENT_OPENED
:
838 * We used to call qemu_system_reset() here, hoping we could
839 * use the same process for multiple tests that way. Never
840 * used. Injects an extra reset even when it's not used, and
841 * that can mess up tests, e.g. -boot once.
843 for (i
= 0; i
< ARRAY_SIZE(irq_levels
); i
++) {
847 g_clear_pointer(&timer
, g_timer_destroy
);
848 timer
= g_timer_new();
851 fprintf(qtest_log_fp
, "[I " FMT_timeval
"] OPENED\n", g_timer_elapsed(timer
, NULL
));
854 case CHR_EVENT_CLOSED
:
855 qtest_opened
= false;
857 fprintf(qtest_log_fp
, "[I +" FMT_timeval
"] CLOSED\n", g_timer_elapsed(timer
, NULL
));
859 g_clear_pointer(&timer
, g_timer_destroy
);
866 void qtest_server_init(const char *qtest_chrdev
, const char *qtest_log
, Error
**errp
)
872 chr
= qemu_chr_new("qtest", qtest_chrdev
, NULL
);
874 error_setg(errp
, "Failed to initialize device for qtest: \"%s\"",
879 qobj
= object_new(TYPE_QTEST
);
880 object_property_set_str(qobj
, "chardev", chr
->label
, &error_abort
);
882 object_property_set_str(qobj
, "log", qtest_log
, &error_abort
);
884 object_property_add_child(qdev_get_machine(), "qtest", qobj
);
885 user_creatable_complete(USER_CREATABLE(qobj
), errp
);
887 object_unparent(qobj
);
889 object_unref(OBJECT(chr
));
893 static bool qtest_server_start(QTest
*q
, Error
**errp
)
895 Chardev
*chr
= q
->chr
;
896 const char *qtest_log
= q
->log
;
899 if (strcmp(qtest_log
, "none") != 0) {
900 qtest_log_fp
= fopen(qtest_log
, "w+");
903 qtest_log_fp
= stderr
;
906 if (!qemu_chr_fe_init(&q
->qtest_chr
, chr
, errp
)) {
909 qemu_chr_fe_set_handlers(&q
->qtest_chr
, qtest_can_read
, qtest_read
,
910 qtest_event
, NULL
, &q
->qtest_chr
, NULL
, true);
911 qemu_chr_fe_set_echo(&q
->qtest_chr
, true);
913 inbuf
= g_string_new("");
915 if (!qtest_server_send
) {
916 qtest_server_set_send_handler(qtest_server_char_be_send
, &q
->qtest_chr
);
922 void qtest_server_set_send_handler(void (*send
)(void*, const char*),
925 qtest_server_send
= send
;
926 qtest_server_send_opaque
= opaque
;
929 bool qtest_driver(void)
931 return qtest
&& qtest
->qtest_chr
.chr
!= NULL
;
934 void qtest_server_inproc_recv(void *dummy
, const char *buf
)
936 static GString
*gstr
;
938 gstr
= g_string_new(NULL
);
940 g_string_append(gstr
, buf
);
941 if (gstr
->str
[gstr
->len
- 1] == '\n') {
942 qtest_process_inbuf(NULL
, gstr
);
943 g_string_truncate(gstr
, 0);
947 static void qtest_complete(UserCreatable
*uc
, Error
**errp
)
949 QTest
*q
= QTEST(uc
);
951 error_setg(errp
, "Only one instance of qtest can be created");
955 error_setg(errp
, "No backend specified");
959 if (OBJECT(uc
)->parent
!= qdev_get_machine()) {
960 q
->has_machine_link
= true;
961 object_property_add_const_link(qdev_get_machine(), "qtest", OBJECT(uc
));
963 /* -qtest was used. */
966 qtest_server_start(q
, errp
);
969 static void qtest_unparent(Object
*obj
)
971 QTest
*q
= QTEST(obj
);
974 qemu_chr_fe_disconnect(&q
->qtest_chr
);
975 assert(!qtest_opened
);
976 qemu_chr_fe_deinit(&q
->qtest_chr
, false);
978 fclose(qtest_log_fp
);
984 if (q
->has_machine_link
) {
985 object_property_del(qdev_get_machine(), "qtest");
986 q
->has_machine_link
= false;
990 static void qtest_set_log(Object
*obj
, const char *value
, Error
**errp
)
992 QTest
*q
= QTEST(obj
);
995 error_setg(errp
, "Property 'log' can not be set now");
998 q
->log
= g_strdup(value
);
1002 static char *qtest_get_log(Object
*obj
, Error
**errp
)
1004 QTest
*q
= QTEST(obj
);
1006 return g_strdup(q
->log
);
1009 static void qtest_set_chardev(Object
*obj
, const char *value
, Error
**errp
)
1011 QTest
*q
= QTEST(obj
);
1015 error_setg(errp
, "Property 'chardev' can not be set now");
1019 chr
= qemu_chr_find(value
);
1021 error_setg(errp
, "Cannot find character device '%s'", value
);
1025 g_free(q
->chr_name
);
1026 q
->chr_name
= g_strdup(value
);
1029 object_unref(q
->chr
);
1035 static char *qtest_get_chardev(Object
*obj
, Error
**errp
)
1037 QTest
*q
= QTEST(obj
);
1039 return g_strdup(q
->chr_name
);
1042 static void qtest_class_init(ObjectClass
*oc
, void *data
)
1044 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
1046 oc
->unparent
= qtest_unparent
;
1047 ucc
->complete
= qtest_complete
;
1049 object_class_property_add_str(oc
, "chardev",
1050 qtest_get_chardev
, qtest_set_chardev
);
1051 object_class_property_add_str(oc
, "log",
1052 qtest_get_log
, qtest_set_log
);
1055 static const TypeInfo qtest_info
= {
1057 .parent
= TYPE_OBJECT
,
1058 .class_init
= qtest_class_init
,
1059 .instance_size
= sizeof(QTest
),
1060 .interfaces
= (InterfaceInfo
[]) {
1061 { TYPE_USER_CREATABLE
},
1066 static void register_types(void)
1068 type_register_static(&qtest_info
);
1071 type_init(register_types
);