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/tcg.h"
52 #include "sysemu/tpm.h"
53 #include "qapi/qmp/qdict.h"
54 #include "qapi/qmp/qerror.h"
55 #include "qapi/qmp/qstring.h"
56 #include "qom/object_interfaces.h"
57 #include "trace/control.h"
58 #include "monitor/hmp-target.h"
59 #include "monitor/hmp.h"
60 #ifdef CONFIG_TRACE_SIMPLE
61 #include "trace/simple.h"
63 #include "exec/memory.h"
64 #include "exec/exec-all.h"
65 #include "qemu/option.h"
66 #include "qemu/thread.h"
67 #include "block/qapi.h"
68 #include "block/block-hmp-cmds.h"
69 #include "qapi/qapi-commands-char.h"
70 #include "qapi/qapi-commands-control.h"
71 #include "qapi/qapi-commands-migration.h"
72 #include "qapi/qapi-commands-misc.h"
73 #include "qapi/qapi-commands-qom.h"
74 #include "qapi/qapi-commands-run-state.h"
75 #include "qapi/qapi-commands-trace.h"
76 #include "qapi/qapi-commands-machine.h"
77 #include "qapi/qapi-init-commands.h"
78 #include "qapi/error.h"
79 #include "qapi/qmp-event.h"
80 #include "sysemu/cpus.h"
81 #include "qemu/cutils.h"
83 #if defined(TARGET_S390X)
84 #include "hw/s390x/storage-keys.h"
85 #include "hw/s390x/storage-attributes.h"
88 /* file descriptors passed via SCM_RIGHTS */
89 typedef struct mon_fd_t mon_fd_t
;
93 QLIST_ENTRY(mon_fd_t
) next
;
96 /* file descriptor associated with a file descriptor set */
97 typedef struct MonFdsetFd MonFdsetFd
;
102 QLIST_ENTRY(MonFdsetFd
) next
;
105 /* file descriptor set containing fds passed via SCM_RIGHTS */
106 typedef struct MonFdset MonFdset
;
109 QLIST_HEAD(, MonFdsetFd
) fds
;
110 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
111 QLIST_ENTRY(MonFdset
) next
;
114 /* Protects mon_fdsets */
115 static QemuMutex mon_fdsets_lock
;
116 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
118 static HMPCommand hmp_info_cmds
[];
120 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
121 int64_t cpu_index
, Error
**errp
)
126 monitor_data_init(&hmp
.common
, false, true, false);
129 int ret
= monitor_set_cpu(&hmp
.common
, cpu_index
);
131 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
137 handle_hmp_command(&hmp
, command_line
);
139 WITH_QEMU_LOCK_GUARD(&hmp
.common
.mon_lock
) {
140 output
= g_strdup(hmp
.common
.outbuf
->str
);
144 monitor_data_destroy(&hmp
.common
);
149 * Is @name in the '|' separated list of names @list?
151 int hmp_compare_cmd(const char *name
, const char *list
)
153 const char *p
, *pstart
;
159 p
= qemu_strchrnul(p
, '|');
160 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
171 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
173 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
176 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
178 const char *tp_name
= qdict_get_str(qdict
, "name");
179 bool new_state
= qdict_get_bool(qdict
, "option");
180 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
181 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
182 Error
*local_err
= NULL
;
185 monitor_printf(mon
, "argument vcpu must be positive");
189 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
191 error_report_err(local_err
);
195 #ifdef CONFIG_TRACE_SIMPLE
196 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
198 const char *op
= qdict_get_try_str(qdict
, "op");
199 const char *arg
= qdict_get_try_str(qdict
, "arg");
202 st_print_trace_file_status();
203 } else if (!strcmp(op
, "on")) {
204 st_set_trace_file_enabled(true);
205 } else if (!strcmp(op
, "off")) {
206 st_set_trace_file_enabled(false);
207 } else if (!strcmp(op
, "flush")) {
208 st_flush_trace_buffer();
209 } else if (!strcmp(op
, "set")) {
211 st_set_trace_file(arg
);
214 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
215 help_cmd(mon
, "trace-file");
220 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
222 help_cmd(mon
, "info");
225 static void monitor_init_qmp_commands(void)
229 * - qmp_commands contains all QMP commands
230 * - qmp_cap_negotiation_commands contains just
231 * "qmp_capabilities", to enforce capability negotiation
234 qmp_init_marshal(&qmp_commands
);
236 qmp_register_command(&qmp_commands
, "device_add",
237 qmp_device_add
, 0, 0);
239 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
240 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
241 qmp_marshal_qmp_capabilities
,
242 QCO_ALLOW_PRECONFIG
, 0);
245 /* Set the current CPU defined by the user. Callers must hold BQL. */
246 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
250 cpu
= qemu_get_cpu(cpu_index
);
254 g_free(mon
->mon_cpu_path
);
255 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
259 /* Callers must hold BQL. */
260 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
262 CPUState
*cpu
= NULL
;
264 if (mon
->mon_cpu_path
) {
265 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
268 g_free(mon
->mon_cpu_path
);
269 mon
->mon_cpu_path
= NULL
;
272 if (!mon
->mon_cpu_path
) {
276 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
281 cpu_synchronize_state(cpu
);
286 CPUState
*mon_get_cpu(Monitor
*mon
)
288 return mon_get_cpu_sync(mon
, true);
291 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
293 CPUState
*cs
= mon_get_cpu(mon
);
295 return cs
? cs
->env_ptr
: NULL
;
298 int monitor_get_cpu_index(Monitor
*mon
)
300 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
302 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
305 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
307 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
312 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
313 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
316 cs
= mon_get_cpu(mon
);
319 monitor_printf(mon
, "No CPU available\n");
323 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
327 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
329 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
330 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
331 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
332 enum QSPSortBy sort_by
;
334 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
335 qsp_report(max
, sort_by
, coalesce
);
338 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
340 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
349 str
= readline_get_history(hmp_mon
->rs
, i
);
353 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
358 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
360 const char *name
= qdict_get_try_str(qdict
, "name");
361 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
362 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
363 TraceEventInfoList
*events
;
364 TraceEventInfoList
*elem
;
365 Error
*local_err
= NULL
;
371 monitor_printf(mon
, "argument vcpu must be positive");
375 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
377 error_report_err(local_err
);
381 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
382 monitor_printf(mon
, "%s : state %u\n",
384 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
386 qapi_free_TraceEventInfoList(events
);
389 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
390 bool has_port
, int64_t port
,
391 bool has_tls_port
, int64_t tls_port
,
392 bool has_cert_subject
, const char *cert_subject
,
395 if (strcmp(protocol
, "spice") == 0) {
396 if (!qemu_using_spice(errp
)) {
400 if (!has_port
&& !has_tls_port
) {
401 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
405 if (qemu_spice
.migrate_info(hostname
,
406 has_port
? port
: -1,
407 has_tls_port
? tls_port
: -1,
409 error_setg(errp
, "Could not set up display for migration");
415 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "'spice'");
418 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
422 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
424 error_report_err(err
);
428 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
431 const char *items
= qdict_get_str(qdict
, "items");
433 if (!strcmp(items
, "none")) {
436 mask
= qemu_str_to_log_mask(items
);
438 help_cmd(mon
, "log");
445 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
447 const char *option
= qdict_get_try_str(qdict
, "option");
448 if (!option
|| !strcmp(option
, "on")) {
450 } else if (!strcmp(option
, "off")) {
453 monitor_printf(mon
, "unexpected option %s\n", option
);
457 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
459 const char *device
= qdict_get_try_str(qdict
, "device");
461 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
464 if (gdbserver_start(device
) < 0) {
465 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
467 } else if (strcmp(device
, "none") == 0) {
468 monitor_printf(mon
, "Disabled gdbserver\n");
470 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
475 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
478 WatchdogAction action
;
481 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
482 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
485 hmp_handle_error(mon
, err
);
488 qmp_watchdog_set_action(action
, &error_abort
);
491 static void monitor_printc(Monitor
*mon
, int c
)
493 monitor_printf(mon
, "'");
496 monitor_printf(mon
, "\\'");
499 monitor_printf(mon
, "\\\\");
502 monitor_printf(mon
, "\\n");
505 monitor_printf(mon
, "\\r");
508 if (c
>= 32 && c
<= 126) {
509 monitor_printf(mon
, "%c", c
);
511 monitor_printf(mon
, "\\x%02x", c
);
515 monitor_printf(mon
, "'");
518 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
519 hwaddr addr
, int is_physical
)
521 int l
, line_size
, i
, max_digits
, len
;
524 CPUState
*cs
= mon_get_cpu(mon
);
526 if (!cs
&& (format
== 'i' || !is_physical
)) {
527 monitor_printf(mon
, "Can not dump without CPU\n");
532 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
546 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
550 max_digits
= (wsize
* 8) / 4;
554 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
563 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
565 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
571 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
572 MemTxResult r
= address_space_read(as
, addr
,
573 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
575 monitor_printf(mon
, " Cannot access memory\n");
579 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
580 monitor_printf(mon
, " Cannot access memory\n");
595 v
= (uint32_t)ldl_p(buf
+ i
);
601 monitor_printf(mon
, " ");
604 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
607 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
610 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
613 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
616 monitor_printc(mon
, v
);
621 monitor_printf(mon
, "\n");
627 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
629 int count
= qdict_get_int(qdict
, "count");
630 int format
= qdict_get_int(qdict
, "format");
631 int size
= qdict_get_int(qdict
, "size");
632 target_long addr
= qdict_get_int(qdict
, "addr");
634 memory_dump(mon
, count
, format
, size
, addr
, 0);
637 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
639 int count
= qdict_get_int(qdict
, "count");
640 int format
= qdict_get_int(qdict
, "format");
641 int size
= qdict_get_int(qdict
, "size");
642 hwaddr addr
= qdict_get_int(qdict
, "addr");
644 memory_dump(mon
, count
, format
, size
, addr
, 1);
647 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
649 Int128 gpa_region_size
;
650 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
654 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
658 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
659 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
660 memory_region_unref(mrs
.mr
);
664 gpa_region_size
= int128_make64(size
);
665 if (int128_lt(mrs
.size
, gpa_region_size
)) {
666 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
668 memory_region_unref(mrs
.mr
);
673 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
676 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
678 hwaddr addr
= qdict_get_int(qdict
, "addr");
679 Error
*local_err
= NULL
;
680 MemoryRegion
*mr
= NULL
;
683 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
685 error_report_err(local_err
);
689 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
691 addr
, mr
->name
, ptr
);
693 memory_region_unref(mr
);
696 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
698 target_ulong addr
= qdict_get_int(qdict
, "addr");
700 CPUState
*cs
= mon_get_cpu(mon
);
704 monitor_printf(mon
, "No cpu\n");
708 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
710 monitor_printf(mon
, "Unmapped\n");
712 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
713 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
718 static uint64_t vtop(void *ptr
, Error
**errp
)
722 uintptr_t addr
= (uintptr_t) ptr
;
723 uintptr_t pagesize
= qemu_real_host_page_size
;
724 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
727 fd
= open("/proc/self/pagemap", O_RDONLY
);
729 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
733 /* Force copy-on-write if necessary. */
734 qatomic_add((uint8_t *)ptr
, 0);
736 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
737 error_setg_errno(errp
, errno
, "Cannot read pagemap");
740 if ((pinfo
& (1ull << 63)) == 0) {
741 error_setg(errp
, "Page not present");
744 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
751 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
753 hwaddr addr
= qdict_get_int(qdict
, "addr");
754 Error
*local_err
= NULL
;
755 MemoryRegion
*mr
= NULL
;
759 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
761 error_report_err(local_err
);
765 physaddr
= vtop(ptr
, &local_err
);
767 error_report_err(local_err
);
769 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
770 " (%s) is 0x%" PRIx64
"\n",
771 addr
, mr
->name
, (uint64_t) physaddr
);
774 memory_region_unref(mr
);
778 static void do_print(Monitor
*mon
, const QDict
*qdict
)
780 int format
= qdict_get_int(qdict
, "format");
781 hwaddr val
= qdict_get_int(qdict
, "val");
785 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
788 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
791 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
795 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
798 monitor_printc(mon
, val
);
801 monitor_printf(mon
, "\n");
804 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
808 uint32_t start
= qdict_get_int(qdict
, "start");
809 uint32_t size
= qdict_get_int(qdict
, "size");
812 for(addr
= start
; addr
< (start
+ size
); addr
++) {
813 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
814 MEMTXATTRS_UNSPECIFIED
, NULL
);
815 /* BSD sum algorithm ('sum' Unix command) */
816 sum
= (sum
>> 1) | (sum
<< 15);
819 monitor_printf(mon
, "%05d\n", sum
);
822 static int mouse_button_state
;
824 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
826 int dx
, dy
, dz
, button
;
827 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
828 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
829 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
831 dx
= strtol(dx_str
, NULL
, 0);
832 dy
= strtol(dy_str
, NULL
, 0);
833 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
834 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
837 dz
= strtol(dz_str
, NULL
, 0);
839 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
840 qemu_input_queue_btn(NULL
, button
, true);
841 qemu_input_event_sync();
842 qemu_input_queue_btn(NULL
, button
, false);
845 qemu_input_event_sync();
848 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
850 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
851 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
852 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
853 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
855 int button_state
= qdict_get_int(qdict
, "button_state");
857 if (mouse_button_state
== button_state
) {
860 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
861 qemu_input_event_sync();
862 mouse_button_state
= button_state
;
865 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
867 int size
= qdict_get_int(qdict
, "size");
868 int addr
= qdict_get_int(qdict
, "addr");
869 int has_index
= qdict_haskey(qdict
, "index");
874 int index
= qdict_get_int(qdict
, "index");
875 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
895 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
896 suffix
, addr
, size
* 2, val
);
899 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
901 int size
= qdict_get_int(qdict
, "size");
902 int addr
= qdict_get_int(qdict
, "addr");
903 int val
= qdict_get_int(qdict
, "val");
905 addr
&= IOPORTS_MASK
;
921 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
923 Error
*local_err
= NULL
;
924 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
926 qemu_boot_set(bootdevice
, &local_err
);
928 error_report_err(local_err
);
930 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
934 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
936 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
937 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
938 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
939 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
941 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
944 /* Capture support */
945 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
947 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
952 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
953 monitor_printf(mon
, "[%d]: ", i
);
954 s
->ops
.info (s
->opaque
);
958 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
961 int n
= qdict_get_int(qdict
, "n");
964 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
966 s
->ops
.destroy (s
->opaque
);
967 QLIST_REMOVE (s
, entries
);
974 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
976 const char *path
= qdict_get_str(qdict
, "path");
977 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
978 int bits
= qdict_get_try_int(qdict
, "bits", 16);
979 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
980 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
982 AudioState
*as
= audio_state_by_name(audiodev
);
985 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
989 s
= g_malloc0 (sizeof (*s
));
991 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
992 monitor_printf(mon
, "Failed to add wave capture\n");
996 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
999 void qmp_getfd(const char *fdname
, Error
**errp
)
1001 Monitor
*cur_mon
= monitor_cur();
1005 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1007 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1011 if (qemu_isdigit(fdname
[0])) {
1013 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1014 "a name not starting with a digit");
1018 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1019 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1020 if (strcmp(monfd
->name
, fdname
) != 0) {
1026 /* Make sure close() is outside critical section */
1031 monfd
= g_new0(mon_fd_t
, 1);
1032 monfd
->name
= g_strdup(fdname
);
1035 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1038 void qmp_closefd(const char *fdname
, Error
**errp
)
1040 Monitor
*cur_mon
= monitor_cur();
1044 qemu_mutex_lock(&cur_mon
->mon_lock
);
1045 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1046 if (strcmp(monfd
->name
, fdname
) != 0) {
1050 QLIST_REMOVE(monfd
, next
);
1052 g_free(monfd
->name
);
1054 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1055 /* Make sure close() is outside critical section */
1060 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1061 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1064 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1068 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1069 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1072 if (strcmp(monfd
->name
, fdname
) != 0) {
1078 /* caller takes ownership of fd */
1079 QLIST_REMOVE(monfd
, next
);
1080 g_free(monfd
->name
);
1086 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1090 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1092 MonFdsetFd
*mon_fdset_fd
;
1093 MonFdsetFd
*mon_fdset_fd_next
;
1095 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1096 if ((mon_fdset_fd
->removed
||
1097 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1098 runstate_is_running()) {
1099 close(mon_fdset_fd
->fd
);
1100 g_free(mon_fdset_fd
->opaque
);
1101 QLIST_REMOVE(mon_fdset_fd
, next
);
1102 g_free(mon_fdset_fd
);
1106 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1107 QLIST_REMOVE(mon_fdset
, next
);
1112 void monitor_fdsets_cleanup(void)
1114 MonFdset
*mon_fdset
;
1115 MonFdset
*mon_fdset_next
;
1117 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1118 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1119 monitor_fdset_cleanup(mon_fdset
);
1123 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1124 const char *opaque
, Error
**errp
)
1127 Monitor
*mon
= monitor_cur();
1130 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1132 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1136 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1137 has_opaque
, opaque
, errp
);
1149 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1151 MonFdset
*mon_fdset
;
1152 MonFdsetFd
*mon_fdset_fd
;
1155 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1156 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1157 if (mon_fdset
->id
!= fdset_id
) {
1160 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1162 if (mon_fdset_fd
->fd
!= fd
) {
1165 mon_fdset_fd
->removed
= true;
1168 mon_fdset_fd
->removed
= true;
1171 if (has_fd
&& !mon_fdset_fd
) {
1174 monitor_fdset_cleanup(mon_fdset
);
1180 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1183 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1185 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1188 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1190 MonFdset
*mon_fdset
;
1191 MonFdsetFd
*mon_fdset_fd
;
1192 FdsetInfoList
*fdset_list
= NULL
;
1194 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1195 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1196 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1198 fdset_info
->fdset_id
= mon_fdset
->id
;
1200 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1201 FdsetFdInfo
*fdsetfd_info
;
1203 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1204 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1205 if (mon_fdset_fd
->opaque
) {
1206 fdsetfd_info
->has_opaque
= true;
1207 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1209 fdsetfd_info
->has_opaque
= false;
1212 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1215 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1221 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1222 bool has_opaque
, const char *opaque
,
1225 MonFdset
*mon_fdset
= NULL
;
1226 MonFdsetFd
*mon_fdset_fd
;
1229 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1231 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1232 /* Break if match found or match impossible due to ordering by ID */
1233 if (fdset_id
<= mon_fdset
->id
) {
1234 if (fdset_id
< mon_fdset
->id
) {
1242 if (mon_fdset
== NULL
) {
1243 int64_t fdset_id_prev
= -1;
1244 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1248 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1249 "a non-negative value");
1252 /* Use specified fdset ID */
1253 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1254 mon_fdset_cur
= mon_fdset
;
1255 if (fdset_id
< mon_fdset_cur
->id
) {
1260 /* Use first available fdset ID */
1261 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1262 mon_fdset_cur
= mon_fdset
;
1263 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1264 fdset_id_prev
= mon_fdset_cur
->id
;
1271 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1273 mon_fdset
->id
= fdset_id
;
1275 mon_fdset
->id
= fdset_id_prev
+ 1;
1278 /* The fdset list is ordered by fdset ID */
1279 if (!mon_fdset_cur
) {
1280 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1281 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1282 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1284 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1288 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1289 mon_fdset_fd
->fd
= fd
;
1290 mon_fdset_fd
->removed
= false;
1292 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1294 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1296 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1297 fdinfo
->fdset_id
= mon_fdset
->id
;
1298 fdinfo
->fd
= mon_fdset_fd
->fd
;
1303 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1308 MonFdset
*mon_fdset
;
1310 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1311 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1312 MonFdsetFd
*mon_fdset_fd
;
1313 MonFdsetFd
*mon_fdset_fd_dup
;
1318 if (mon_fdset
->id
!= fdset_id
) {
1322 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1323 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1324 if (mon_fd_flags
== -1) {
1328 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1329 fd
= mon_fdset_fd
->fd
;
1339 dup_fd
= qemu_dup_flags(fd
, flags
);
1344 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1345 mon_fdset_fd_dup
->fd
= dup_fd
;
1346 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1355 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1357 MonFdset
*mon_fdset
;
1358 MonFdsetFd
*mon_fdset_fd_dup
;
1360 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1361 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1362 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1363 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1365 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1366 g_free(mon_fdset_fd_dup
);
1367 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1368 monitor_fdset_cleanup(mon_fdset
);
1372 return mon_fdset
->id
;
1381 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1383 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1386 void monitor_fdset_dup_fd_remove(int dup_fd
)
1388 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1391 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1394 Error
*local_err
= NULL
;
1396 if (!qemu_isdigit(fdname
[0]) && mon
) {
1397 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1399 fd
= qemu_parse_fd(fdname
);
1401 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1406 error_propagate(errp
, local_err
);
1415 /* Please update hmp-commands.hx when adding or changing commands */
1416 static HMPCommand hmp_info_cmds
[] = {
1417 #include "hmp-commands-info.h"
1421 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1422 HMPCommand hmp_cmds
[] = {
1423 #include "hmp-commands.h"
1428 * Set @pval to the value in the register identified by @name.
1429 * return 0 if OK, -1 if not found
1431 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1433 const MonitorDef
*md
= target_monitor_defs();
1434 CPUState
*cs
= mon_get_cpu(mon
);
1439 if (cs
== NULL
|| md
== NULL
) {
1443 for(; md
->name
!= NULL
; md
++) {
1444 if (hmp_compare_cmd(name
, md
->name
)) {
1445 if (md
->get_value
) {
1446 *pval
= md
->get_value(mon
, md
, md
->offset
);
1448 CPUArchState
*env
= mon_get_cpu_env(mon
);
1449 ptr
= (uint8_t *)env
+ md
->offset
;
1452 *pval
= *(int32_t *)ptr
;
1455 *pval
= *(target_long
*)ptr
;
1466 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1468 *pval
= (target_long
) tmp
;
1474 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1477 if (!str
|| !option
) {
1480 if (!strncmp(option
, str
, strlen(str
))) {
1481 readline_add_completion(rs
, option
);
1485 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1488 ChardevBackendInfoList
*list
, *start
;
1494 readline_set_completion_index(rs
, len
);
1496 start
= list
= qmp_query_chardev_backends(NULL
);
1498 const char *chr_name
= list
->value
->name
;
1500 if (!strncmp(chr_name
, str
, len
)) {
1501 readline_add_completion(rs
, chr_name
);
1505 qapi_free_ChardevBackendInfoList(start
);
1508 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1517 readline_set_completion_index(rs
, len
);
1518 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1519 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1523 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1533 readline_set_completion_index(rs
, len
);
1534 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1537 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1539 name
= object_class_get_name(OBJECT_CLASS(dc
));
1541 if (dc
->user_creatable
1542 && !strncmp(name
, str
, len
)) {
1543 readline_add_completion(rs
, name
);
1550 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1560 readline_set_completion_index(rs
, len
);
1561 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1565 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1566 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1567 readline_add_completion(rs
, name
);
1574 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1576 GSList
**list
= opaque
;
1577 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1583 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1584 *list
= g_slist_append(*list
, dev
);
1590 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1592 GSList
*list
= NULL
;
1594 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1599 static void peripheral_device_del_completion(ReadLineState
*rs
,
1600 const char *str
, size_t len
)
1602 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1603 GSList
*list
, *item
;
1605 list
= qdev_build_hotpluggable_device_list(peripheral
);
1610 for (item
= list
; item
; item
= g_slist_next(item
)) {
1611 DeviceState
*dev
= item
->data
;
1613 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1614 readline_add_completion(rs
, dev
->id
);
1621 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1624 ChardevInfoList
*list
, *start
;
1630 readline_set_completion_index(rs
, len
);
1632 start
= list
= qmp_query_chardev(NULL
);
1634 ChardevInfo
*chr
= list
->value
;
1636 if (!strncmp(chr
->label
, str
, len
)) {
1637 readline_add_completion(rs
, chr
->label
);
1641 qapi_free_ChardevInfoList(start
);
1644 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1647 ChardevInfoList
*list
, *start
;
1650 readline_set_completion_index(rs
, len
);
1652 start
= list
= qmp_query_chardev(NULL
);
1654 ChardevInfo
*chr_info
= list
->value
;
1656 if (!strncmp(chr_info
->label
, str
, len
)) {
1657 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1658 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1659 readline_add_completion(rs
, chr_info
->label
);
1664 qapi_free_ChardevInfoList(start
);
1667 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1672 ringbuf_completion(rs
, str
);
1675 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1684 readline_set_completion_index(rs
, len
);
1685 peripheral_device_del_completion(rs
, str
, len
);
1688 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1690 ObjectPropertyInfoList
*list
, *start
;
1697 readline_set_completion_index(rs
, len
);
1699 start
= list
= qmp_qom_list("/objects", NULL
);
1701 ObjectPropertyInfo
*info
= list
->value
;
1703 if (!strncmp(info
->type
, "child<", 5)
1704 && !strncmp(info
->name
, str
, len
)) {
1705 readline_add_completion(rs
, info
->name
);
1709 qapi_free_ObjectPropertyInfoList(start
);
1712 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1721 sep
= strrchr(str
, '-');
1726 readline_set_completion_index(rs
, len
);
1727 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1728 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1729 readline_add_completion(rs
, QKeyCode_str(i
));
1734 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1739 readline_set_completion_index(rs
, len
);
1741 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1743 count
= qemu_find_net_clients_except(NULL
, ncs
,
1744 NET_CLIENT_DRIVER_NONE
,
1746 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1747 const char *name
= ncs
[i
]->name
;
1748 if (!strncmp(str
, name
, len
)) {
1749 readline_add_completion(rs
, name
);
1752 } else if (nb_args
== 3) {
1753 add_completion_option(rs
, str
, "on");
1754 add_completion_option(rs
, str
, "off");
1758 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1761 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1768 readline_set_completion_index(rs
, len
);
1769 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1771 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1772 const char *name
= ncs
[i
]->name
;
1773 if (strncmp(str
, name
, len
)) {
1776 if (ncs
[i
]->is_netdev
) {
1777 readline_add_completion(rs
, name
);
1782 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1787 readline_set_completion_index(rs
, len
);
1789 TraceEventIter iter
;
1791 char *pattern
= g_strdup_printf("%s*", str
);
1792 trace_event_iter_init_pattern(&iter
, pattern
);
1793 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1794 readline_add_completion(rs
, trace_event_get_name(ev
));
1800 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1805 readline_set_completion_index(rs
, len
);
1807 TraceEventIter iter
;
1809 char *pattern
= g_strdup_printf("%s*", str
);
1810 trace_event_iter_init_pattern(&iter
, pattern
);
1811 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1812 readline_add_completion(rs
, trace_event_get_name(ev
));
1815 } else if (nb_args
== 3) {
1816 add_completion_option(rs
, str
, "on");
1817 add_completion_option(rs
, str
, "off");
1821 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1828 readline_set_completion_index(rs
, strlen(str
));
1829 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1830 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1834 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1840 readline_set_completion_index(rs
, len
);
1843 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1844 const char *name
= MigrationCapability_str(i
);
1845 if (!strncmp(str
, name
, len
)) {
1846 readline_add_completion(rs
, name
);
1849 } else if (nb_args
== 3) {
1850 add_completion_option(rs
, str
, "on");
1851 add_completion_option(rs
, str
, "off");
1855 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1861 readline_set_completion_index(rs
, len
);
1864 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1865 const char *name
= MigrationParameter_str(i
);
1866 if (!strncmp(str
, name
, len
)) {
1867 readline_add_completion(rs
, name
);
1873 static void vm_completion(ReadLineState
*rs
, const char *str
)
1876 BlockDriverState
*bs
;
1877 BdrvNextIterator it
;
1880 readline_set_completion_index(rs
, len
);
1882 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1883 SnapshotInfoList
*snapshots
, *snapshot
;
1884 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1887 aio_context_acquire(ctx
);
1888 if (bdrv_can_snapshot(bs
)) {
1889 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1891 aio_context_release(ctx
);
1896 snapshot
= snapshots
;
1898 char *completion
= snapshot
->value
->name
;
1899 if (!strncmp(str
, completion
, len
)) {
1900 readline_add_completion(rs
, completion
);
1902 completion
= snapshot
->value
->id
;
1903 if (!strncmp(str
, completion
, len
)) {
1904 readline_add_completion(rs
, completion
);
1906 snapshot
= snapshot
->next
;
1908 qapi_free_SnapshotInfoList(snapshots
);
1913 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1916 vm_completion(rs
, str
);
1920 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1923 vm_completion(rs
, str
);
1928 compare_mon_cmd(const void *a
, const void *b
)
1930 return strcmp(((const HMPCommand
*)a
)->name
,
1931 ((const HMPCommand
*)b
)->name
);
1934 static void sortcmdlist(void)
1936 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1939 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1940 sizeof(*hmp_info_cmds
),
1944 void monitor_register_hmp(const char *name
, bool info
,
1945 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1947 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1949 while (table
->name
!= NULL
) {
1950 if (strcmp(table
->name
, name
) == 0) {
1951 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1957 g_assert_not_reached();
1960 void monitor_register_hmp_info_hrt(const char *name
,
1961 HumanReadableText
*(*handler
)(Error
**errp
))
1963 HMPCommand
*table
= hmp_info_cmds
;
1965 while (table
->name
!= NULL
) {
1966 if (strcmp(table
->name
, name
) == 0) {
1967 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1968 table
->cmd_info_hrt
= handler
;
1973 g_assert_not_reached();
1976 void monitor_init_globals(void)
1978 monitor_init_globals_core();
1979 monitor_init_qmp_commands();
1981 qemu_mutex_init(&mon_fdsets_lock
);