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"
44 #include "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "authz/list.h"
47 #include "qapi/util.h"
48 #include "sysemu/blockdev.h"
49 #include "sysemu/sysemu.h"
50 #include "sysemu/tcg.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 /* file descriptors passed via SCM_RIGHTS */
88 typedef struct mon_fd_t mon_fd_t
;
92 QLIST_ENTRY(mon_fd_t
) next
;
95 /* file descriptor associated with a file descriptor set */
96 typedef struct MonFdsetFd MonFdsetFd
;
101 QLIST_ENTRY(MonFdsetFd
) next
;
104 /* file descriptor set containing fds passed via SCM_RIGHTS */
105 typedef struct MonFdset MonFdset
;
108 QLIST_HEAD(, MonFdsetFd
) fds
;
109 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
110 QLIST_ENTRY(MonFdset
) next
;
113 /* Protects mon_fdsets */
114 static QemuMutex mon_fdsets_lock
;
115 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
117 static HMPCommand hmp_info_cmds
[];
119 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
120 int64_t cpu_index
, Error
**errp
)
125 monitor_data_init(&hmp
.common
, false, true, false);
128 int ret
= monitor_set_cpu(&hmp
.common
, cpu_index
);
130 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
136 handle_hmp_command(&hmp
, command_line
);
138 WITH_QEMU_LOCK_GUARD(&hmp
.common
.mon_lock
) {
139 output
= g_strdup(hmp
.common
.outbuf
->str
);
143 monitor_data_destroy(&hmp
.common
);
148 * Is @name in the '|' separated list of names @list?
150 int hmp_compare_cmd(const char *name
, const char *list
)
152 const char *p
, *pstart
;
158 p
= qemu_strchrnul(p
, '|');
159 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
170 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
172 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
175 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
177 const char *tp_name
= qdict_get_str(qdict
, "name");
178 bool new_state
= qdict_get_bool(qdict
, "option");
179 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
180 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
181 Error
*local_err
= NULL
;
184 monitor_printf(mon
, "argument vcpu must be positive");
188 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
190 error_report_err(local_err
);
194 #ifdef CONFIG_TRACE_SIMPLE
195 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
197 const char *op
= qdict_get_try_str(qdict
, "op");
198 const char *arg
= qdict_get_try_str(qdict
, "arg");
201 st_print_trace_file_status();
202 } else if (!strcmp(op
, "on")) {
203 st_set_trace_file_enabled(true);
204 } else if (!strcmp(op
, "off")) {
205 st_set_trace_file_enabled(false);
206 } else if (!strcmp(op
, "flush")) {
207 st_flush_trace_buffer();
208 } else if (!strcmp(op
, "set")) {
210 st_set_trace_file(arg
);
213 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
214 help_cmd(mon
, "trace-file");
219 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
221 help_cmd(mon
, "info");
224 static void monitor_init_qmp_commands(void)
228 * - qmp_commands contains all QMP commands
229 * - qmp_cap_negotiation_commands contains just
230 * "qmp_capabilities", to enforce capability negotiation
233 qmp_init_marshal(&qmp_commands
);
235 qmp_register_command(&qmp_commands
, "device_add",
236 qmp_device_add
, 0, 0);
238 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
239 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
240 qmp_marshal_qmp_capabilities
,
241 QCO_ALLOW_PRECONFIG
, 0);
244 /* Set the current CPU defined by the user. Callers must hold BQL. */
245 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
249 cpu
= qemu_get_cpu(cpu_index
);
253 g_free(mon
->mon_cpu_path
);
254 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
258 /* Callers must hold BQL. */
259 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
261 CPUState
*cpu
= NULL
;
263 if (mon
->mon_cpu_path
) {
264 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
267 g_free(mon
->mon_cpu_path
);
268 mon
->mon_cpu_path
= NULL
;
271 if (!mon
->mon_cpu_path
) {
275 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
280 cpu_synchronize_state(cpu
);
285 CPUState
*mon_get_cpu(Monitor
*mon
)
287 return mon_get_cpu_sync(mon
, true);
290 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
292 CPUState
*cs
= mon_get_cpu(mon
);
294 return cs
? cs
->env_ptr
: NULL
;
297 int monitor_get_cpu_index(Monitor
*mon
)
299 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
301 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
304 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
306 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
311 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
312 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
315 cs
= mon_get_cpu(mon
);
318 monitor_printf(mon
, "No CPU available\n");
322 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
326 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
328 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
329 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
330 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
331 enum QSPSortBy sort_by
;
333 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
334 qsp_report(max
, sort_by
, coalesce
);
337 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
339 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
348 str
= readline_get_history(hmp_mon
->rs
, i
);
352 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
357 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
359 const char *name
= qdict_get_try_str(qdict
, "name");
360 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
361 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
362 TraceEventInfoList
*events
;
363 TraceEventInfoList
*elem
;
364 Error
*local_err
= NULL
;
370 monitor_printf(mon
, "argument vcpu must be positive");
374 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
376 error_report_err(local_err
);
380 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
381 monitor_printf(mon
, "%s : state %u\n",
383 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
385 qapi_free_TraceEventInfoList(events
);
388 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
389 bool has_port
, int64_t port
,
390 bool has_tls_port
, int64_t tls_port
,
391 bool has_cert_subject
, const char *cert_subject
,
394 if (strcmp(protocol
, "spice") == 0) {
395 if (!qemu_using_spice(errp
)) {
399 if (!has_port
&& !has_tls_port
) {
400 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
404 if (qemu_spice
.migrate_info(hostname
,
405 has_port
? port
: -1,
406 has_tls_port
? tls_port
: -1,
408 error_setg(errp
, "Could not set up display for migration");
414 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "'spice'");
417 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
421 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
423 error_report_err(err
);
427 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
430 const char *items
= qdict_get_str(qdict
, "items");
432 if (!strcmp(items
, "none")) {
435 mask
= qemu_str_to_log_mask(items
);
437 help_cmd(mon
, "log");
444 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
446 const char *option
= qdict_get_try_str(qdict
, "option");
447 if (!option
|| !strcmp(option
, "on")) {
449 } else if (!strcmp(option
, "off")) {
452 monitor_printf(mon
, "unexpected option %s\n", option
);
456 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
458 const char *device
= qdict_get_try_str(qdict
, "device");
460 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
463 if (gdbserver_start(device
) < 0) {
464 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
466 } else if (strcmp(device
, "none") == 0) {
467 monitor_printf(mon
, "Disabled gdbserver\n");
469 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
474 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
477 WatchdogAction action
;
480 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
481 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
484 hmp_handle_error(mon
, err
);
487 qmp_watchdog_set_action(action
, &error_abort
);
490 static void monitor_printc(Monitor
*mon
, int c
)
492 monitor_printf(mon
, "'");
495 monitor_printf(mon
, "\\'");
498 monitor_printf(mon
, "\\\\");
501 monitor_printf(mon
, "\\n");
504 monitor_printf(mon
, "\\r");
507 if (c
>= 32 && c
<= 126) {
508 monitor_printf(mon
, "%c", c
);
510 monitor_printf(mon
, "\\x%02x", c
);
514 monitor_printf(mon
, "'");
517 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
518 hwaddr addr
, int is_physical
)
520 int l
, line_size
, i
, max_digits
, len
;
523 CPUState
*cs
= mon_get_cpu(mon
);
525 if (!cs
&& (format
== 'i' || !is_physical
)) {
526 monitor_printf(mon
, "Can not dump without CPU\n");
531 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
545 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
549 max_digits
= (wsize
* 8) / 4;
553 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
562 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
564 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
570 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
571 MemTxResult r
= address_space_read(as
, addr
,
572 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
574 monitor_printf(mon
, " Cannot access memory\n");
578 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
579 monitor_printf(mon
, " Cannot access memory\n");
594 v
= (uint32_t)ldl_p(buf
+ i
);
600 monitor_printf(mon
, " ");
603 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
606 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
609 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
612 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
615 monitor_printc(mon
, v
);
620 monitor_printf(mon
, "\n");
626 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
628 int count
= qdict_get_int(qdict
, "count");
629 int format
= qdict_get_int(qdict
, "format");
630 int size
= qdict_get_int(qdict
, "size");
631 target_long addr
= qdict_get_int(qdict
, "addr");
633 memory_dump(mon
, count
, format
, size
, addr
, 0);
636 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
638 int count
= qdict_get_int(qdict
, "count");
639 int format
= qdict_get_int(qdict
, "format");
640 int size
= qdict_get_int(qdict
, "size");
641 hwaddr addr
= qdict_get_int(qdict
, "addr");
643 memory_dump(mon
, count
, format
, size
, addr
, 1);
646 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
648 Int128 gpa_region_size
;
649 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
653 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
657 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
658 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
659 memory_region_unref(mrs
.mr
);
663 gpa_region_size
= int128_make64(size
);
664 if (int128_lt(mrs
.size
, gpa_region_size
)) {
665 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
667 memory_region_unref(mrs
.mr
);
672 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
675 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
677 hwaddr addr
= qdict_get_int(qdict
, "addr");
678 Error
*local_err
= NULL
;
679 MemoryRegion
*mr
= NULL
;
682 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
684 error_report_err(local_err
);
688 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
690 addr
, mr
->name
, ptr
);
692 memory_region_unref(mr
);
695 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
697 target_ulong addr
= qdict_get_int(qdict
, "addr");
699 CPUState
*cs
= mon_get_cpu(mon
);
703 monitor_printf(mon
, "No cpu\n");
707 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
709 monitor_printf(mon
, "Unmapped\n");
711 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
712 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
717 static uint64_t vtop(void *ptr
, Error
**errp
)
721 uintptr_t addr
= (uintptr_t) ptr
;
722 uintptr_t pagesize
= qemu_real_host_page_size
;
723 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
726 fd
= open("/proc/self/pagemap", O_RDONLY
);
728 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
732 /* Force copy-on-write if necessary. */
733 qatomic_add((uint8_t *)ptr
, 0);
735 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
736 error_setg_errno(errp
, errno
, "Cannot read pagemap");
739 if ((pinfo
& (1ull << 63)) == 0) {
740 error_setg(errp
, "Page not present");
743 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
750 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
752 hwaddr addr
= qdict_get_int(qdict
, "addr");
753 Error
*local_err
= NULL
;
754 MemoryRegion
*mr
= NULL
;
758 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
760 error_report_err(local_err
);
764 physaddr
= vtop(ptr
, &local_err
);
766 error_report_err(local_err
);
768 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
769 " (%s) is 0x%" PRIx64
"\n",
770 addr
, mr
->name
, (uint64_t) physaddr
);
773 memory_region_unref(mr
);
777 static void do_print(Monitor
*mon
, const QDict
*qdict
)
779 int format
= qdict_get_int(qdict
, "format");
780 hwaddr val
= qdict_get_int(qdict
, "val");
784 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
787 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
790 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
794 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
797 monitor_printc(mon
, val
);
800 monitor_printf(mon
, "\n");
803 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
807 uint32_t start
= qdict_get_int(qdict
, "start");
808 uint32_t size
= qdict_get_int(qdict
, "size");
811 for(addr
= start
; addr
< (start
+ size
); addr
++) {
812 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
813 MEMTXATTRS_UNSPECIFIED
, NULL
);
814 /* BSD sum algorithm ('sum' Unix command) */
815 sum
= (sum
>> 1) | (sum
<< 15);
818 monitor_printf(mon
, "%05d\n", sum
);
821 static int mouse_button_state
;
823 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
825 int dx
, dy
, dz
, button
;
826 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
827 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
828 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
830 dx
= strtol(dx_str
, NULL
, 0);
831 dy
= strtol(dy_str
, NULL
, 0);
832 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
833 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
836 dz
= strtol(dz_str
, NULL
, 0);
838 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
839 qemu_input_queue_btn(NULL
, button
, true);
840 qemu_input_event_sync();
841 qemu_input_queue_btn(NULL
, button
, false);
844 qemu_input_event_sync();
847 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
849 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
850 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
851 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
852 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
854 int button_state
= qdict_get_int(qdict
, "button_state");
856 if (mouse_button_state
== button_state
) {
859 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
860 qemu_input_event_sync();
861 mouse_button_state
= button_state
;
864 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
866 int size
= qdict_get_int(qdict
, "size");
867 int addr
= qdict_get_int(qdict
, "addr");
868 int has_index
= qdict_haskey(qdict
, "index");
873 int index
= qdict_get_int(qdict
, "index");
874 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
894 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
895 suffix
, addr
, size
* 2, val
);
898 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
900 int size
= qdict_get_int(qdict
, "size");
901 int addr
= qdict_get_int(qdict
, "addr");
902 int val
= qdict_get_int(qdict
, "val");
904 addr
&= IOPORTS_MASK
;
920 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
922 Error
*local_err
= NULL
;
923 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
925 qemu_boot_set(bootdevice
, &local_err
);
927 error_report_err(local_err
);
929 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
933 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
935 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
936 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
937 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
938 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
940 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
943 /* Capture support */
944 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
946 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
951 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
952 monitor_printf(mon
, "[%d]: ", i
);
953 s
->ops
.info (s
->opaque
);
957 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
960 int n
= qdict_get_int(qdict
, "n");
963 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
965 s
->ops
.destroy (s
->opaque
);
966 QLIST_REMOVE (s
, entries
);
973 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
975 const char *path
= qdict_get_str(qdict
, "path");
976 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
977 int bits
= qdict_get_try_int(qdict
, "bits", 16);
978 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
979 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
981 AudioState
*as
= audio_state_by_name(audiodev
);
984 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
988 s
= g_malloc0 (sizeof (*s
));
990 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
991 monitor_printf(mon
, "Failed to add wave capture\n");
995 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
998 void qmp_getfd(const char *fdname
, Error
**errp
)
1000 Monitor
*cur_mon
= monitor_cur();
1004 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1006 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1010 if (qemu_isdigit(fdname
[0])) {
1012 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1013 "a name not starting with a digit");
1017 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1018 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1019 if (strcmp(monfd
->name
, fdname
) != 0) {
1025 /* Make sure close() is outside critical section */
1030 monfd
= g_malloc0(sizeof(mon_fd_t
));
1031 monfd
->name
= g_strdup(fdname
);
1034 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1037 void qmp_closefd(const char *fdname
, Error
**errp
)
1039 Monitor
*cur_mon
= monitor_cur();
1043 qemu_mutex_lock(&cur_mon
->mon_lock
);
1044 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1045 if (strcmp(monfd
->name
, fdname
) != 0) {
1049 QLIST_REMOVE(monfd
, next
);
1051 g_free(monfd
->name
);
1053 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1054 /* Make sure close() is outside critical section */
1059 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1060 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1063 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1067 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1068 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1071 if (strcmp(monfd
->name
, fdname
) != 0) {
1077 /* caller takes ownership of fd */
1078 QLIST_REMOVE(monfd
, next
);
1079 g_free(monfd
->name
);
1085 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1089 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1091 MonFdsetFd
*mon_fdset_fd
;
1092 MonFdsetFd
*mon_fdset_fd_next
;
1094 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1095 if ((mon_fdset_fd
->removed
||
1096 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1097 runstate_is_running()) {
1098 close(mon_fdset_fd
->fd
);
1099 g_free(mon_fdset_fd
->opaque
);
1100 QLIST_REMOVE(mon_fdset_fd
, next
);
1101 g_free(mon_fdset_fd
);
1105 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1106 QLIST_REMOVE(mon_fdset
, next
);
1111 void monitor_fdsets_cleanup(void)
1113 MonFdset
*mon_fdset
;
1114 MonFdset
*mon_fdset_next
;
1116 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1117 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1118 monitor_fdset_cleanup(mon_fdset
);
1122 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1123 const char *opaque
, Error
**errp
)
1126 Monitor
*mon
= monitor_cur();
1129 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1131 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1135 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1136 has_opaque
, opaque
, errp
);
1148 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1150 MonFdset
*mon_fdset
;
1151 MonFdsetFd
*mon_fdset_fd
;
1154 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1155 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1156 if (mon_fdset
->id
!= fdset_id
) {
1159 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1161 if (mon_fdset_fd
->fd
!= fd
) {
1164 mon_fdset_fd
->removed
= true;
1167 mon_fdset_fd
->removed
= true;
1170 if (has_fd
&& !mon_fdset_fd
) {
1173 monitor_fdset_cleanup(mon_fdset
);
1179 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1182 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1184 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1187 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1189 MonFdset
*mon_fdset
;
1190 MonFdsetFd
*mon_fdset_fd
;
1191 FdsetInfoList
*fdset_list
= NULL
;
1193 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1194 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1195 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1197 fdset_info
->fdset_id
= mon_fdset
->id
;
1199 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1200 FdsetFdInfo
*fdsetfd_info
;
1202 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1203 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1204 if (mon_fdset_fd
->opaque
) {
1205 fdsetfd_info
->has_opaque
= true;
1206 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1208 fdsetfd_info
->has_opaque
= false;
1211 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1214 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1220 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1221 bool has_opaque
, const char *opaque
,
1224 MonFdset
*mon_fdset
= NULL
;
1225 MonFdsetFd
*mon_fdset_fd
;
1228 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1230 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1231 /* Break if match found or match impossible due to ordering by ID */
1232 if (fdset_id
<= mon_fdset
->id
) {
1233 if (fdset_id
< mon_fdset
->id
) {
1241 if (mon_fdset
== NULL
) {
1242 int64_t fdset_id_prev
= -1;
1243 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1247 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1248 "a non-negative value");
1251 /* Use specified fdset ID */
1252 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1253 mon_fdset_cur
= mon_fdset
;
1254 if (fdset_id
< mon_fdset_cur
->id
) {
1259 /* Use first available fdset ID */
1260 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1261 mon_fdset_cur
= mon_fdset
;
1262 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1263 fdset_id_prev
= mon_fdset_cur
->id
;
1270 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1272 mon_fdset
->id
= fdset_id
;
1274 mon_fdset
->id
= fdset_id_prev
+ 1;
1277 /* The fdset list is ordered by fdset ID */
1278 if (!mon_fdset_cur
) {
1279 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1280 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1281 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1283 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1287 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1288 mon_fdset_fd
->fd
= fd
;
1289 mon_fdset_fd
->removed
= false;
1291 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1293 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1295 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1296 fdinfo
->fdset_id
= mon_fdset
->id
;
1297 fdinfo
->fd
= mon_fdset_fd
->fd
;
1302 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1307 MonFdset
*mon_fdset
;
1309 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1310 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1311 MonFdsetFd
*mon_fdset_fd
;
1312 MonFdsetFd
*mon_fdset_fd_dup
;
1317 if (mon_fdset
->id
!= fdset_id
) {
1321 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1322 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1323 if (mon_fd_flags
== -1) {
1327 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1328 fd
= mon_fdset_fd
->fd
;
1338 dup_fd
= qemu_dup_flags(fd
, flags
);
1343 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1344 mon_fdset_fd_dup
->fd
= dup_fd
;
1345 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1354 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1356 MonFdset
*mon_fdset
;
1357 MonFdsetFd
*mon_fdset_fd_dup
;
1359 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1360 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1361 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1362 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1364 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1365 g_free(mon_fdset_fd_dup
);
1366 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1367 monitor_fdset_cleanup(mon_fdset
);
1371 return mon_fdset
->id
;
1380 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1382 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1385 void monitor_fdset_dup_fd_remove(int dup_fd
)
1387 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1390 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1393 Error
*local_err
= NULL
;
1395 if (!qemu_isdigit(fdname
[0]) && mon
) {
1396 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1398 fd
= qemu_parse_fd(fdname
);
1400 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1405 error_propagate(errp
, local_err
);
1414 /* Please update hmp-commands.hx when adding or changing commands */
1415 static HMPCommand hmp_info_cmds
[] = {
1416 #include "hmp-commands-info.h"
1420 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1421 HMPCommand hmp_cmds
[] = {
1422 #include "hmp-commands.h"
1427 * Set @pval to the value in the register identified by @name.
1428 * return 0 if OK, -1 if not found
1430 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1432 const MonitorDef
*md
= target_monitor_defs();
1433 CPUState
*cs
= mon_get_cpu(mon
);
1438 if (cs
== NULL
|| md
== NULL
) {
1442 for(; md
->name
!= NULL
; md
++) {
1443 if (hmp_compare_cmd(name
, md
->name
)) {
1444 if (md
->get_value
) {
1445 *pval
= md
->get_value(mon
, md
, md
->offset
);
1447 CPUArchState
*env
= mon_get_cpu_env(mon
);
1448 ptr
= (uint8_t *)env
+ md
->offset
;
1451 *pval
= *(int32_t *)ptr
;
1454 *pval
= *(target_long
*)ptr
;
1465 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1467 *pval
= (target_long
) tmp
;
1473 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1476 if (!str
|| !option
) {
1479 if (!strncmp(option
, str
, strlen(str
))) {
1480 readline_add_completion(rs
, option
);
1484 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1487 ChardevBackendInfoList
*list
, *start
;
1493 readline_set_completion_index(rs
, len
);
1495 start
= list
= qmp_query_chardev_backends(NULL
);
1497 const char *chr_name
= list
->value
->name
;
1499 if (!strncmp(chr_name
, str
, len
)) {
1500 readline_add_completion(rs
, chr_name
);
1504 qapi_free_ChardevBackendInfoList(start
);
1507 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1516 readline_set_completion_index(rs
, len
);
1517 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1518 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1522 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1532 readline_set_completion_index(rs
, len
);
1533 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1536 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1538 name
= object_class_get_name(OBJECT_CLASS(dc
));
1540 if (dc
->user_creatable
1541 && !strncmp(name
, str
, len
)) {
1542 readline_add_completion(rs
, name
);
1549 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1559 readline_set_completion_index(rs
, len
);
1560 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1564 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1565 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1566 readline_add_completion(rs
, name
);
1573 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1575 GSList
**list
= opaque
;
1576 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1582 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1583 *list
= g_slist_append(*list
, dev
);
1589 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1591 GSList
*list
= NULL
;
1593 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1598 static void peripheral_device_del_completion(ReadLineState
*rs
,
1599 const char *str
, size_t len
)
1601 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1602 GSList
*list
, *item
;
1604 list
= qdev_build_hotpluggable_device_list(peripheral
);
1609 for (item
= list
; item
; item
= g_slist_next(item
)) {
1610 DeviceState
*dev
= item
->data
;
1612 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1613 readline_add_completion(rs
, dev
->id
);
1620 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1623 ChardevInfoList
*list
, *start
;
1629 readline_set_completion_index(rs
, len
);
1631 start
= list
= qmp_query_chardev(NULL
);
1633 ChardevInfo
*chr
= list
->value
;
1635 if (!strncmp(chr
->label
, str
, len
)) {
1636 readline_add_completion(rs
, chr
->label
);
1640 qapi_free_ChardevInfoList(start
);
1643 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1646 ChardevInfoList
*list
, *start
;
1649 readline_set_completion_index(rs
, len
);
1651 start
= list
= qmp_query_chardev(NULL
);
1653 ChardevInfo
*chr_info
= list
->value
;
1655 if (!strncmp(chr_info
->label
, str
, len
)) {
1656 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1657 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1658 readline_add_completion(rs
, chr_info
->label
);
1663 qapi_free_ChardevInfoList(start
);
1666 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1671 ringbuf_completion(rs
, str
);
1674 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1683 readline_set_completion_index(rs
, len
);
1684 peripheral_device_del_completion(rs
, str
, len
);
1687 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1689 ObjectPropertyInfoList
*list
, *start
;
1696 readline_set_completion_index(rs
, len
);
1698 start
= list
= qmp_qom_list("/objects", NULL
);
1700 ObjectPropertyInfo
*info
= list
->value
;
1702 if (!strncmp(info
->type
, "child<", 5)
1703 && !strncmp(info
->name
, str
, len
)) {
1704 readline_add_completion(rs
, info
->name
);
1708 qapi_free_ObjectPropertyInfoList(start
);
1711 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1720 sep
= strrchr(str
, '-');
1725 readline_set_completion_index(rs
, len
);
1726 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1727 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1728 readline_add_completion(rs
, QKeyCode_str(i
));
1733 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1738 readline_set_completion_index(rs
, len
);
1740 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1742 count
= qemu_find_net_clients_except(NULL
, ncs
,
1743 NET_CLIENT_DRIVER_NONE
,
1745 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1746 const char *name
= ncs
[i
]->name
;
1747 if (!strncmp(str
, name
, len
)) {
1748 readline_add_completion(rs
, name
);
1751 } else if (nb_args
== 3) {
1752 add_completion_option(rs
, str
, "on");
1753 add_completion_option(rs
, str
, "off");
1757 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1760 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1767 readline_set_completion_index(rs
, len
);
1768 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1770 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1771 const char *name
= ncs
[i
]->name
;
1772 if (strncmp(str
, name
, len
)) {
1775 if (ncs
[i
]->is_netdev
) {
1776 readline_add_completion(rs
, name
);
1781 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1786 readline_set_completion_index(rs
, len
);
1788 TraceEventIter iter
;
1790 char *pattern
= g_strdup_printf("%s*", str
);
1791 trace_event_iter_init_pattern(&iter
, pattern
);
1792 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1793 readline_add_completion(rs
, trace_event_get_name(ev
));
1799 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1804 readline_set_completion_index(rs
, len
);
1806 TraceEventIter iter
;
1808 char *pattern
= g_strdup_printf("%s*", str
);
1809 trace_event_iter_init_pattern(&iter
, pattern
);
1810 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1811 readline_add_completion(rs
, trace_event_get_name(ev
));
1814 } else if (nb_args
== 3) {
1815 add_completion_option(rs
, str
, "on");
1816 add_completion_option(rs
, str
, "off");
1820 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1827 readline_set_completion_index(rs
, strlen(str
));
1828 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1829 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1833 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1839 readline_set_completion_index(rs
, len
);
1842 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1843 const char *name
= MigrationCapability_str(i
);
1844 if (!strncmp(str
, name
, len
)) {
1845 readline_add_completion(rs
, name
);
1848 } else if (nb_args
== 3) {
1849 add_completion_option(rs
, str
, "on");
1850 add_completion_option(rs
, str
, "off");
1854 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1860 readline_set_completion_index(rs
, len
);
1863 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1864 const char *name
= MigrationParameter_str(i
);
1865 if (!strncmp(str
, name
, len
)) {
1866 readline_add_completion(rs
, name
);
1872 static void vm_completion(ReadLineState
*rs
, const char *str
)
1875 BlockDriverState
*bs
;
1876 BdrvNextIterator it
;
1879 readline_set_completion_index(rs
, len
);
1881 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1882 SnapshotInfoList
*snapshots
, *snapshot
;
1883 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1886 aio_context_acquire(ctx
);
1887 if (bdrv_can_snapshot(bs
)) {
1888 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1890 aio_context_release(ctx
);
1895 snapshot
= snapshots
;
1897 char *completion
= snapshot
->value
->name
;
1898 if (!strncmp(str
, completion
, len
)) {
1899 readline_add_completion(rs
, completion
);
1901 completion
= snapshot
->value
->id
;
1902 if (!strncmp(str
, completion
, len
)) {
1903 readline_add_completion(rs
, completion
);
1905 snapshot
= snapshot
->next
;
1907 qapi_free_SnapshotInfoList(snapshots
);
1912 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1915 vm_completion(rs
, str
);
1919 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1922 vm_completion(rs
, str
);
1927 compare_mon_cmd(const void *a
, const void *b
)
1929 return strcmp(((const HMPCommand
*)a
)->name
,
1930 ((const HMPCommand
*)b
)->name
);
1933 static void sortcmdlist(void)
1935 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1938 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1939 sizeof(*hmp_info_cmds
),
1943 void monitor_register_hmp(const char *name
, bool info
,
1944 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1946 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1948 while (table
->name
!= NULL
) {
1949 if (strcmp(table
->name
, name
) == 0) {
1950 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1956 g_assert_not_reached();
1959 void monitor_register_hmp_info_hrt(const char *name
,
1960 HumanReadableText
*(*handler
)(Error
**errp
))
1962 HMPCommand
*table
= hmp_info_cmds
;
1964 while (table
->name
!= NULL
) {
1965 if (strcmp(table
->name
, name
) == 0) {
1966 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1967 table
->cmd_info_hrt
= handler
;
1972 g_assert_not_reached();
1975 void monitor_init_globals(void)
1977 monitor_init_globals_core();
1978 monitor_init_qmp_commands();
1980 qemu_mutex_init(&mon_fdsets_lock
);