4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
45 #include "sysemu/hw_accel.h"
46 #include "sysemu/runstate.h"
47 #include "authz/list.h"
48 #include "qapi/util.h"
49 #include "sysemu/blockdev.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qdict.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qstring.h"
55 #include "qom/object_interfaces.h"
56 #include "trace/control.h"
57 #include "monitor/hmp-target.h"
58 #include "monitor/hmp.h"
59 #ifdef CONFIG_TRACE_SIMPLE
60 #include "trace/simple.h"
62 #include "exec/memory.h"
63 #include "exec/exec-all.h"
64 #include "qemu/option.h"
65 #include "qemu/thread.h"
66 #include "block/qapi.h"
67 #include "block/block-hmp-cmds.h"
68 #include "qapi/qapi-commands-char.h"
69 #include "qapi/qapi-commands-control.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-run-state.h"
74 #include "qapi/qapi-commands-trace.h"
75 #include "qapi/qapi-commands-machine.h"
76 #include "qapi/qapi-init-commands.h"
77 #include "qapi/error.h"
78 #include "qapi/qmp-event.h"
79 #include "sysemu/cpus.h"
80 #include "qemu/cutils.h"
82 #if defined(TARGET_S390X)
83 #include "hw/s390x/storage-keys.h"
84 #include "hw/s390x/storage-attributes.h"
87 /* Make devices configuration available for use in hmp-commands*.hx templates */
88 #include CONFIG_DEVICES
90 /* file descriptors passed via SCM_RIGHTS */
91 typedef struct mon_fd_t mon_fd_t
;
95 QLIST_ENTRY(mon_fd_t
) next
;
98 /* file descriptor associated with a file descriptor set */
99 typedef struct MonFdsetFd MonFdsetFd
;
104 QLIST_ENTRY(MonFdsetFd
) next
;
107 /* file descriptor set containing fds passed via SCM_RIGHTS */
108 typedef struct MonFdset MonFdset
;
111 QLIST_HEAD(, MonFdsetFd
) fds
;
112 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
113 QLIST_ENTRY(MonFdset
) next
;
116 /* Protects mon_fdsets */
117 static QemuMutex mon_fdsets_lock
;
118 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
120 static HMPCommand hmp_info_cmds
[];
122 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
123 int64_t cpu_index
, Error
**errp
)
128 monitor_data_init(&hmp
.common
, false, true, false);
131 int ret
= monitor_set_cpu(&hmp
.common
, cpu_index
);
133 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
139 handle_hmp_command(&hmp
, command_line
);
141 WITH_QEMU_LOCK_GUARD(&hmp
.common
.mon_lock
) {
142 output
= g_strdup(hmp
.common
.outbuf
->str
);
146 monitor_data_destroy(&hmp
.common
);
151 * Is @name in the '|' separated list of names @list?
153 int hmp_compare_cmd(const char *name
, const char *list
)
155 const char *p
, *pstart
;
161 p
= qemu_strchrnul(p
, '|');
162 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
173 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
175 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
178 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
180 const char *tp_name
= qdict_get_str(qdict
, "name");
181 bool new_state
= qdict_get_bool(qdict
, "option");
182 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
183 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
184 Error
*local_err
= NULL
;
187 monitor_printf(mon
, "argument vcpu must be positive");
191 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
193 error_report_err(local_err
);
197 #ifdef CONFIG_TRACE_SIMPLE
198 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
200 const char *op
= qdict_get_try_str(qdict
, "op");
201 const char *arg
= qdict_get_try_str(qdict
, "arg");
204 st_print_trace_file_status();
205 } else if (!strcmp(op
, "on")) {
206 st_set_trace_file_enabled(true);
207 } else if (!strcmp(op
, "off")) {
208 st_set_trace_file_enabled(false);
209 } else if (!strcmp(op
, "flush")) {
210 st_flush_trace_buffer();
211 } else if (!strcmp(op
, "set")) {
213 st_set_trace_file(arg
);
216 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
217 help_cmd(mon
, "trace-file");
222 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
224 help_cmd(mon
, "info");
227 static void monitor_init_qmp_commands(void)
231 * - qmp_commands contains all QMP commands
232 * - qmp_cap_negotiation_commands contains just
233 * "qmp_capabilities", to enforce capability negotiation
236 qmp_init_marshal(&qmp_commands
);
238 qmp_register_command(&qmp_commands
, "device_add",
239 qmp_device_add
, 0, 0);
241 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
242 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
243 qmp_marshal_qmp_capabilities
,
244 QCO_ALLOW_PRECONFIG
, 0);
247 /* Set the current CPU defined by the user. Callers must hold BQL. */
248 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
252 cpu
= qemu_get_cpu(cpu_index
);
256 g_free(mon
->mon_cpu_path
);
257 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
261 /* Callers must hold BQL. */
262 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
264 CPUState
*cpu
= NULL
;
266 if (mon
->mon_cpu_path
) {
267 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
270 g_free(mon
->mon_cpu_path
);
271 mon
->mon_cpu_path
= NULL
;
274 if (!mon
->mon_cpu_path
) {
278 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
283 cpu_synchronize_state(cpu
);
288 CPUState
*mon_get_cpu(Monitor
*mon
)
290 return mon_get_cpu_sync(mon
, true);
293 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
295 CPUState
*cs
= mon_get_cpu(mon
);
297 return cs
? cs
->env_ptr
: NULL
;
300 int monitor_get_cpu_index(Monitor
*mon
)
302 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
304 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
307 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
309 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
314 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
315 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
318 cs
= mon_get_cpu(mon
);
321 monitor_printf(mon
, "No CPU available\n");
325 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
329 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
331 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
332 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
333 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
334 enum QSPSortBy sort_by
;
336 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
337 qsp_report(max
, sort_by
, coalesce
);
340 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
342 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
351 str
= readline_get_history(hmp_mon
->rs
, i
);
355 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
360 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
362 const char *name
= qdict_get_try_str(qdict
, "name");
363 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
364 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
365 TraceEventInfoList
*events
;
366 TraceEventInfoList
*elem
;
367 Error
*local_err
= NULL
;
373 monitor_printf(mon
, "argument vcpu must be positive");
377 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
379 error_report_err(local_err
);
383 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
384 monitor_printf(mon
, "%s : state %u\n",
386 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
388 qapi_free_TraceEventInfoList(events
);
391 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
392 bool has_port
, int64_t port
,
393 bool has_tls_port
, int64_t tls_port
,
394 bool has_cert_subject
, const char *cert_subject
,
397 if (strcmp(protocol
, "spice") == 0) {
398 if (!qemu_using_spice(errp
)) {
402 if (!has_port
&& !has_tls_port
) {
403 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
407 if (qemu_spice
.migrate_info(hostname
,
408 has_port
? port
: -1,
409 has_tls_port
? tls_port
: -1,
411 error_setg(errp
, "Could not set up display for migration");
417 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "'spice'");
420 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
424 if (!qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
)) {
425 error_report_err(err
);
429 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
432 const char *items
= qdict_get_str(qdict
, "items");
435 if (!strcmp(items
, "none")) {
438 mask
= qemu_str_to_log_mask(items
);
440 help_cmd(mon
, "log");
445 if (!qemu_set_log(mask
, &err
)) {
446 error_report_err(err
);
450 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
452 const char *option
= qdict_get_try_str(qdict
, "option");
453 if (!option
|| !strcmp(option
, "on")) {
455 } else if (!strcmp(option
, "off")) {
458 monitor_printf(mon
, "unexpected option %s\n", option
);
462 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
464 const char *device
= qdict_get_try_str(qdict
, "device");
466 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
469 if (gdbserver_start(device
) < 0) {
470 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
472 } else if (strcmp(device
, "none") == 0) {
473 monitor_printf(mon
, "Disabled gdbserver\n");
475 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
480 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
483 WatchdogAction action
;
486 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
487 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
490 hmp_handle_error(mon
, err
);
493 qmp_watchdog_set_action(action
, &error_abort
);
496 static void monitor_printc(Monitor
*mon
, int c
)
498 monitor_printf(mon
, "'");
501 monitor_printf(mon
, "\\'");
504 monitor_printf(mon
, "\\\\");
507 monitor_printf(mon
, "\\n");
510 monitor_printf(mon
, "\\r");
513 if (c
>= 32 && c
<= 126) {
514 monitor_printf(mon
, "%c", c
);
516 monitor_printf(mon
, "\\x%02x", c
);
520 monitor_printf(mon
, "'");
523 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
524 hwaddr addr
, int is_physical
)
526 int l
, line_size
, i
, max_digits
, len
;
529 CPUState
*cs
= mon_get_cpu(mon
);
531 if (!cs
&& (format
== 'i' || !is_physical
)) {
532 monitor_printf(mon
, "Can not dump without CPU\n");
537 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
551 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
555 max_digits
= (wsize
* 8) / 4;
559 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
568 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
570 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
576 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
577 MemTxResult r
= address_space_read(as
, addr
,
578 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
580 monitor_printf(mon
, " Cannot access memory\n");
584 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
585 monitor_printf(mon
, " Cannot access memory\n");
600 v
= (uint32_t)ldl_p(buf
+ i
);
606 monitor_printf(mon
, " ");
609 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
612 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
615 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
618 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
621 monitor_printc(mon
, v
);
626 monitor_printf(mon
, "\n");
632 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
634 int count
= qdict_get_int(qdict
, "count");
635 int format
= qdict_get_int(qdict
, "format");
636 int size
= qdict_get_int(qdict
, "size");
637 target_long addr
= qdict_get_int(qdict
, "addr");
639 memory_dump(mon
, count
, format
, size
, addr
, 0);
642 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
644 int count
= qdict_get_int(qdict
, "count");
645 int format
= qdict_get_int(qdict
, "format");
646 int size
= qdict_get_int(qdict
, "size");
647 hwaddr addr
= qdict_get_int(qdict
, "addr");
649 memory_dump(mon
, count
, format
, size
, addr
, 1);
652 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
654 Int128 gpa_region_size
;
655 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
659 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
663 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
664 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
665 memory_region_unref(mrs
.mr
);
669 gpa_region_size
= int128_make64(size
);
670 if (int128_lt(mrs
.size
, gpa_region_size
)) {
671 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
673 memory_region_unref(mrs
.mr
);
678 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
681 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
683 hwaddr addr
= qdict_get_int(qdict
, "addr");
684 Error
*local_err
= NULL
;
685 MemoryRegion
*mr
= NULL
;
688 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
690 error_report_err(local_err
);
694 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
696 addr
, mr
->name
, ptr
);
698 memory_region_unref(mr
);
701 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
703 target_ulong addr
= qdict_get_int(qdict
, "addr");
705 CPUState
*cs
= mon_get_cpu(mon
);
709 monitor_printf(mon
, "No cpu\n");
713 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
715 monitor_printf(mon
, "Unmapped\n");
717 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
718 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
723 static uint64_t vtop(void *ptr
, Error
**errp
)
727 uintptr_t addr
= (uintptr_t) ptr
;
728 uintptr_t pagesize
= qemu_real_host_page_size();
729 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
732 fd
= open("/proc/self/pagemap", O_RDONLY
);
734 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
738 /* Force copy-on-write if necessary. */
739 qatomic_add((uint8_t *)ptr
, 0);
741 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
742 error_setg_errno(errp
, errno
, "Cannot read pagemap");
745 if ((pinfo
& (1ull << 63)) == 0) {
746 error_setg(errp
, "Page not present");
749 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
756 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
758 hwaddr addr
= qdict_get_int(qdict
, "addr");
759 Error
*local_err
= NULL
;
760 MemoryRegion
*mr
= NULL
;
764 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
766 error_report_err(local_err
);
770 physaddr
= vtop(ptr
, &local_err
);
772 error_report_err(local_err
);
774 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
775 " (%s) is 0x%" PRIx64
"\n",
776 addr
, mr
->name
, (uint64_t) physaddr
);
779 memory_region_unref(mr
);
783 static void do_print(Monitor
*mon
, const QDict
*qdict
)
785 int format
= qdict_get_int(qdict
, "format");
786 hwaddr val
= qdict_get_int(qdict
, "val");
790 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
793 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
796 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
800 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
803 monitor_printc(mon
, val
);
806 monitor_printf(mon
, "\n");
809 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
813 uint32_t start
= qdict_get_int(qdict
, "start");
814 uint32_t size
= qdict_get_int(qdict
, "size");
817 for(addr
= start
; addr
< (start
+ size
); addr
++) {
818 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
819 MEMTXATTRS_UNSPECIFIED
, NULL
);
820 /* BSD sum algorithm ('sum' Unix command) */
821 sum
= (sum
>> 1) | (sum
<< 15);
824 monitor_printf(mon
, "%05d\n", sum
);
827 static int mouse_button_state
;
829 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
831 int dx
, dy
, dz
, button
;
832 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
833 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
834 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
836 dx
= strtol(dx_str
, NULL
, 0);
837 dy
= strtol(dy_str
, NULL
, 0);
838 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
839 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
842 dz
= strtol(dz_str
, NULL
, 0);
844 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
845 qemu_input_queue_btn(NULL
, button
, true);
846 qemu_input_event_sync();
847 qemu_input_queue_btn(NULL
, button
, false);
850 qemu_input_event_sync();
853 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
855 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
856 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
857 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
858 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
860 int button_state
= qdict_get_int(qdict
, "button_state");
862 if (mouse_button_state
== button_state
) {
865 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
866 qemu_input_event_sync();
867 mouse_button_state
= button_state
;
870 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
872 int size
= qdict_get_int(qdict
, "size");
873 int addr
= qdict_get_int(qdict
, "addr");
874 int has_index
= qdict_haskey(qdict
, "index");
879 int index
= qdict_get_int(qdict
, "index");
880 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
900 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
901 suffix
, addr
, size
* 2, val
);
904 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
906 int size
= qdict_get_int(qdict
, "size");
907 int addr
= qdict_get_int(qdict
, "addr");
908 int val
= qdict_get_int(qdict
, "val");
910 addr
&= IOPORTS_MASK
;
926 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
928 Error
*local_err
= NULL
;
929 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
931 qemu_boot_set(bootdevice
, &local_err
);
933 error_report_err(local_err
);
935 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
939 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
941 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
942 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
943 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
944 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
946 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
949 /* Capture support */
950 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
952 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
957 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
958 monitor_printf(mon
, "[%d]: ", i
);
959 s
->ops
.info (s
->opaque
);
963 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
966 int n
= qdict_get_int(qdict
, "n");
969 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
971 s
->ops
.destroy (s
->opaque
);
972 QLIST_REMOVE (s
, entries
);
979 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
981 const char *path
= qdict_get_str(qdict
, "path");
982 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
983 int bits
= qdict_get_try_int(qdict
, "bits", 16);
984 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
985 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
987 AudioState
*as
= audio_state_by_name(audiodev
);
990 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
994 s
= g_malloc0 (sizeof (*s
));
996 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
997 monitor_printf(mon
, "Failed to add wave capture\n");
1001 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1004 void qmp_getfd(const char *fdname
, Error
**errp
)
1006 Monitor
*cur_mon
= monitor_cur();
1010 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1012 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1016 if (qemu_isdigit(fdname
[0])) {
1018 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1019 "a name not starting with a digit");
1023 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1024 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1025 if (strcmp(monfd
->name
, fdname
) != 0) {
1031 /* Make sure close() is outside critical section */
1036 monfd
= g_new0(mon_fd_t
, 1);
1037 monfd
->name
= g_strdup(fdname
);
1040 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1043 void qmp_closefd(const char *fdname
, Error
**errp
)
1045 Monitor
*cur_mon
= monitor_cur();
1049 qemu_mutex_lock(&cur_mon
->mon_lock
);
1050 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1051 if (strcmp(monfd
->name
, fdname
) != 0) {
1055 QLIST_REMOVE(monfd
, next
);
1057 g_free(monfd
->name
);
1059 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1060 /* Make sure close() is outside critical section */
1065 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1066 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1069 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1073 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1074 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1077 if (strcmp(monfd
->name
, fdname
) != 0) {
1083 /* caller takes ownership of fd */
1084 QLIST_REMOVE(monfd
, next
);
1085 g_free(monfd
->name
);
1091 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1095 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1097 MonFdsetFd
*mon_fdset_fd
;
1098 MonFdsetFd
*mon_fdset_fd_next
;
1100 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1101 if ((mon_fdset_fd
->removed
||
1102 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1103 runstate_is_running()) {
1104 close(mon_fdset_fd
->fd
);
1105 g_free(mon_fdset_fd
->opaque
);
1106 QLIST_REMOVE(mon_fdset_fd
, next
);
1107 g_free(mon_fdset_fd
);
1111 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1112 QLIST_REMOVE(mon_fdset
, next
);
1117 void monitor_fdsets_cleanup(void)
1119 MonFdset
*mon_fdset
;
1120 MonFdset
*mon_fdset_next
;
1122 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1123 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1124 monitor_fdset_cleanup(mon_fdset
);
1128 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1129 const char *opaque
, Error
**errp
)
1132 Monitor
*mon
= monitor_cur();
1135 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1137 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1141 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1142 has_opaque
, opaque
, errp
);
1154 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1156 MonFdset
*mon_fdset
;
1157 MonFdsetFd
*mon_fdset_fd
;
1160 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1161 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1162 if (mon_fdset
->id
!= fdset_id
) {
1165 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1167 if (mon_fdset_fd
->fd
!= fd
) {
1170 mon_fdset_fd
->removed
= true;
1173 mon_fdset_fd
->removed
= true;
1176 if (has_fd
&& !mon_fdset_fd
) {
1179 monitor_fdset_cleanup(mon_fdset
);
1185 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1188 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1190 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1193 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1195 MonFdset
*mon_fdset
;
1196 MonFdsetFd
*mon_fdset_fd
;
1197 FdsetInfoList
*fdset_list
= NULL
;
1199 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1200 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1201 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1203 fdset_info
->fdset_id
= mon_fdset
->id
;
1205 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1206 FdsetFdInfo
*fdsetfd_info
;
1208 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1209 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1210 if (mon_fdset_fd
->opaque
) {
1211 fdsetfd_info
->has_opaque
= true;
1212 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1214 fdsetfd_info
->has_opaque
= false;
1217 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1220 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1226 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1227 bool has_opaque
, const char *opaque
,
1230 MonFdset
*mon_fdset
= NULL
;
1231 MonFdsetFd
*mon_fdset_fd
;
1234 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1236 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1237 /* Break if match found or match impossible due to ordering by ID */
1238 if (fdset_id
<= mon_fdset
->id
) {
1239 if (fdset_id
< mon_fdset
->id
) {
1247 if (mon_fdset
== NULL
) {
1248 int64_t fdset_id_prev
= -1;
1249 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1253 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1254 "a non-negative value");
1257 /* Use specified fdset ID */
1258 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1259 mon_fdset_cur
= mon_fdset
;
1260 if (fdset_id
< mon_fdset_cur
->id
) {
1265 /* Use first available fdset ID */
1266 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1267 mon_fdset_cur
= mon_fdset
;
1268 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1269 fdset_id_prev
= mon_fdset_cur
->id
;
1276 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1278 mon_fdset
->id
= fdset_id
;
1280 mon_fdset
->id
= fdset_id_prev
+ 1;
1283 /* The fdset list is ordered by fdset ID */
1284 if (!mon_fdset_cur
) {
1285 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1286 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1287 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1289 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1293 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1294 mon_fdset_fd
->fd
= fd
;
1295 mon_fdset_fd
->removed
= false;
1297 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1299 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1301 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1302 fdinfo
->fdset_id
= mon_fdset
->id
;
1303 fdinfo
->fd
= mon_fdset_fd
->fd
;
1308 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1313 MonFdset
*mon_fdset
;
1315 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1316 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1317 MonFdsetFd
*mon_fdset_fd
;
1318 MonFdsetFd
*mon_fdset_fd_dup
;
1323 if (mon_fdset
->id
!= fdset_id
) {
1327 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1328 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1329 if (mon_fd_flags
== -1) {
1333 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1334 fd
= mon_fdset_fd
->fd
;
1344 dup_fd
= qemu_dup_flags(fd
, flags
);
1349 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1350 mon_fdset_fd_dup
->fd
= dup_fd
;
1351 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1360 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1362 MonFdset
*mon_fdset
;
1363 MonFdsetFd
*mon_fdset_fd_dup
;
1365 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1366 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1367 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1368 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1370 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1371 g_free(mon_fdset_fd_dup
);
1372 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1373 monitor_fdset_cleanup(mon_fdset
);
1377 return mon_fdset
->id
;
1386 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1388 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1391 void monitor_fdset_dup_fd_remove(int dup_fd
)
1393 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1396 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1399 Error
*local_err
= NULL
;
1401 if (!qemu_isdigit(fdname
[0]) && mon
) {
1402 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1404 fd
= qemu_parse_fd(fdname
);
1406 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1411 error_propagate(errp
, local_err
);
1420 /* Please update hmp-commands.hx when adding or changing commands */
1421 static HMPCommand hmp_info_cmds
[] = {
1422 #include "hmp-commands-info.h"
1426 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1427 HMPCommand hmp_cmds
[] = {
1428 #include "hmp-commands.h"
1433 * Set @pval to the value in the register identified by @name.
1434 * return 0 if OK, -1 if not found
1436 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1438 const MonitorDef
*md
= target_monitor_defs();
1439 CPUState
*cs
= mon_get_cpu(mon
);
1444 if (cs
== NULL
|| md
== NULL
) {
1448 for(; md
->name
!= NULL
; md
++) {
1449 if (hmp_compare_cmd(name
, md
->name
)) {
1450 if (md
->get_value
) {
1451 *pval
= md
->get_value(mon
, md
, md
->offset
);
1453 CPUArchState
*env
= mon_get_cpu_env(mon
);
1454 ptr
= (uint8_t *)env
+ md
->offset
;
1457 *pval
= *(int32_t *)ptr
;
1460 *pval
= *(target_long
*)ptr
;
1471 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1473 *pval
= (target_long
) tmp
;
1479 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1482 if (!str
|| !option
) {
1485 if (!strncmp(option
, str
, strlen(str
))) {
1486 readline_add_completion(rs
, option
);
1490 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1493 ChardevBackendInfoList
*list
, *start
;
1499 readline_set_completion_index(rs
, len
);
1501 start
= list
= qmp_query_chardev_backends(NULL
);
1503 const char *chr_name
= list
->value
->name
;
1505 if (!strncmp(chr_name
, str
, len
)) {
1506 readline_add_completion(rs
, chr_name
);
1510 qapi_free_ChardevBackendInfoList(start
);
1513 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1522 readline_set_completion_index(rs
, len
);
1523 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1524 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1528 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1538 readline_set_completion_index(rs
, len
);
1539 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1542 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1544 name
= object_class_get_name(OBJECT_CLASS(dc
));
1546 if (dc
->user_creatable
1547 && !strncmp(name
, str
, len
)) {
1548 readline_add_completion(rs
, name
);
1555 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1565 readline_set_completion_index(rs
, len
);
1566 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1570 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1571 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1572 readline_add_completion(rs
, name
);
1579 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1581 GSList
**list
= opaque
;
1582 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1588 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1589 *list
= g_slist_append(*list
, dev
);
1595 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1597 GSList
*list
= NULL
;
1599 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1604 static void peripheral_device_del_completion(ReadLineState
*rs
,
1605 const char *str
, size_t len
)
1607 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1608 GSList
*list
, *item
;
1610 list
= qdev_build_hotpluggable_device_list(peripheral
);
1615 for (item
= list
; item
; item
= g_slist_next(item
)) {
1616 DeviceState
*dev
= item
->data
;
1618 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1619 readline_add_completion(rs
, dev
->id
);
1626 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1629 ChardevInfoList
*list
, *start
;
1635 readline_set_completion_index(rs
, len
);
1637 start
= list
= qmp_query_chardev(NULL
);
1639 ChardevInfo
*chr
= list
->value
;
1641 if (!strncmp(chr
->label
, str
, len
)) {
1642 readline_add_completion(rs
, chr
->label
);
1646 qapi_free_ChardevInfoList(start
);
1649 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1652 ChardevInfoList
*list
, *start
;
1655 readline_set_completion_index(rs
, len
);
1657 start
= list
= qmp_query_chardev(NULL
);
1659 ChardevInfo
*chr_info
= list
->value
;
1661 if (!strncmp(chr_info
->label
, str
, len
)) {
1662 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1663 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1664 readline_add_completion(rs
, chr_info
->label
);
1669 qapi_free_ChardevInfoList(start
);
1672 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1677 ringbuf_completion(rs
, str
);
1680 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1689 readline_set_completion_index(rs
, len
);
1690 peripheral_device_del_completion(rs
, str
, len
);
1693 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1695 ObjectPropertyInfoList
*list
, *start
;
1702 readline_set_completion_index(rs
, len
);
1704 start
= list
= qmp_qom_list("/objects", NULL
);
1706 ObjectPropertyInfo
*info
= list
->value
;
1708 if (!strncmp(info
->type
, "child<", 5)
1709 && !strncmp(info
->name
, str
, len
)) {
1710 readline_add_completion(rs
, info
->name
);
1714 qapi_free_ObjectPropertyInfoList(start
);
1717 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1726 sep
= strrchr(str
, '-');
1731 readline_set_completion_index(rs
, len
);
1732 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1733 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1734 readline_add_completion(rs
, QKeyCode_str(i
));
1739 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1744 readline_set_completion_index(rs
, len
);
1746 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1748 count
= qemu_find_net_clients_except(NULL
, ncs
,
1749 NET_CLIENT_DRIVER_NONE
,
1751 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1752 const char *name
= ncs
[i
]->name
;
1753 if (!strncmp(str
, name
, len
)) {
1754 readline_add_completion(rs
, name
);
1757 } else if (nb_args
== 3) {
1758 add_completion_option(rs
, str
, "on");
1759 add_completion_option(rs
, str
, "off");
1763 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1766 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1773 readline_set_completion_index(rs
, len
);
1774 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1776 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1777 const char *name
= ncs
[i
]->name
;
1778 if (strncmp(str
, name
, len
)) {
1781 if (ncs
[i
]->is_netdev
) {
1782 readline_add_completion(rs
, name
);
1787 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1792 readline_set_completion_index(rs
, len
);
1794 TraceEventIter iter
;
1796 char *pattern
= g_strdup_printf("%s*", str
);
1797 trace_event_iter_init_pattern(&iter
, pattern
);
1798 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1799 readline_add_completion(rs
, trace_event_get_name(ev
));
1805 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1810 readline_set_completion_index(rs
, len
);
1812 TraceEventIter iter
;
1814 char *pattern
= g_strdup_printf("%s*", str
);
1815 trace_event_iter_init_pattern(&iter
, pattern
);
1816 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1817 readline_add_completion(rs
, trace_event_get_name(ev
));
1820 } else if (nb_args
== 3) {
1821 add_completion_option(rs
, str
, "on");
1822 add_completion_option(rs
, str
, "off");
1826 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1833 readline_set_completion_index(rs
, strlen(str
));
1834 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1835 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1839 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1845 readline_set_completion_index(rs
, len
);
1848 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1849 const char *name
= MigrationCapability_str(i
);
1850 if (!strncmp(str
, name
, len
)) {
1851 readline_add_completion(rs
, name
);
1854 } else if (nb_args
== 3) {
1855 add_completion_option(rs
, str
, "on");
1856 add_completion_option(rs
, str
, "off");
1860 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1866 readline_set_completion_index(rs
, len
);
1869 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1870 const char *name
= MigrationParameter_str(i
);
1871 if (!strncmp(str
, name
, len
)) {
1872 readline_add_completion(rs
, name
);
1878 static void vm_completion(ReadLineState
*rs
, const char *str
)
1881 BlockDriverState
*bs
;
1882 BdrvNextIterator it
;
1885 readline_set_completion_index(rs
, len
);
1887 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1888 SnapshotInfoList
*snapshots
, *snapshot
;
1889 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1892 aio_context_acquire(ctx
);
1893 if (bdrv_can_snapshot(bs
)) {
1894 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1896 aio_context_release(ctx
);
1901 snapshot
= snapshots
;
1903 char *completion
= snapshot
->value
->name
;
1904 if (!strncmp(str
, completion
, len
)) {
1905 readline_add_completion(rs
, completion
);
1907 completion
= snapshot
->value
->id
;
1908 if (!strncmp(str
, completion
, len
)) {
1909 readline_add_completion(rs
, completion
);
1911 snapshot
= snapshot
->next
;
1913 qapi_free_SnapshotInfoList(snapshots
);
1918 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1921 vm_completion(rs
, str
);
1925 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1928 vm_completion(rs
, str
);
1933 compare_mon_cmd(const void *a
, const void *b
)
1935 return strcmp(((const HMPCommand
*)a
)->name
,
1936 ((const HMPCommand
*)b
)->name
);
1939 static void sortcmdlist(void)
1941 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1944 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1945 sizeof(*hmp_info_cmds
),
1949 void monitor_register_hmp(const char *name
, bool info
,
1950 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1952 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1954 while (table
->name
!= NULL
) {
1955 if (strcmp(table
->name
, name
) == 0) {
1956 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1962 g_assert_not_reached();
1965 void monitor_register_hmp_info_hrt(const char *name
,
1966 HumanReadableText
*(*handler
)(Error
**errp
))
1968 HMPCommand
*table
= hmp_info_cmds
;
1970 while (table
->name
!= NULL
) {
1971 if (strcmp(table
->name
, name
) == 0) {
1972 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1973 table
->cmd_info_hrt
= handler
;
1978 g_assert_not_reached();
1981 void monitor_init_globals(void)
1983 monitor_init_globals_core();
1984 monitor_init_qmp_commands();
1986 qemu_mutex_init(&mon_fdsets_lock
);