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 /* 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 if (!qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
)) {
422 error_report_err(err
);
426 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
429 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");
442 if (!qemu_set_log(mask
, &err
)) {
443 error_report_err(err
);
447 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
449 const char *option
= qdict_get_try_str(qdict
, "option");
450 if (!option
|| !strcmp(option
, "on")) {
452 } else if (!strcmp(option
, "off")) {
455 monitor_printf(mon
, "unexpected option %s\n", option
);
459 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
461 const char *device
= qdict_get_try_str(qdict
, "device");
463 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
466 if (gdbserver_start(device
) < 0) {
467 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
469 } else if (strcmp(device
, "none") == 0) {
470 monitor_printf(mon
, "Disabled gdbserver\n");
472 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
477 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
480 WatchdogAction action
;
483 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
484 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
487 hmp_handle_error(mon
, err
);
490 qmp_watchdog_set_action(action
, &error_abort
);
493 static void monitor_printc(Monitor
*mon
, int c
)
495 monitor_printf(mon
, "'");
498 monitor_printf(mon
, "\\'");
501 monitor_printf(mon
, "\\\\");
504 monitor_printf(mon
, "\\n");
507 monitor_printf(mon
, "\\r");
510 if (c
>= 32 && c
<= 126) {
511 monitor_printf(mon
, "%c", c
);
513 monitor_printf(mon
, "\\x%02x", c
);
517 monitor_printf(mon
, "'");
520 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
521 hwaddr addr
, int is_physical
)
523 int l
, line_size
, i
, max_digits
, len
;
526 CPUState
*cs
= mon_get_cpu(mon
);
528 if (!cs
&& (format
== 'i' || !is_physical
)) {
529 monitor_printf(mon
, "Can not dump without CPU\n");
534 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
548 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
552 max_digits
= (wsize
* 8) / 4;
556 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
565 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
567 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
573 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
574 MemTxResult r
= address_space_read(as
, addr
,
575 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
577 monitor_printf(mon
, " Cannot access memory\n");
581 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
582 monitor_printf(mon
, " Cannot access memory\n");
597 v
= (uint32_t)ldl_p(buf
+ i
);
603 monitor_printf(mon
, " ");
606 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
609 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
612 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
615 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
618 monitor_printc(mon
, v
);
623 monitor_printf(mon
, "\n");
629 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
631 int count
= qdict_get_int(qdict
, "count");
632 int format
= qdict_get_int(qdict
, "format");
633 int size
= qdict_get_int(qdict
, "size");
634 target_long addr
= qdict_get_int(qdict
, "addr");
636 memory_dump(mon
, count
, format
, size
, addr
, 0);
639 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
641 int count
= qdict_get_int(qdict
, "count");
642 int format
= qdict_get_int(qdict
, "format");
643 int size
= qdict_get_int(qdict
, "size");
644 hwaddr addr
= qdict_get_int(qdict
, "addr");
646 memory_dump(mon
, count
, format
, size
, addr
, 1);
649 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
651 Int128 gpa_region_size
;
652 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
656 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
660 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
661 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
662 memory_region_unref(mrs
.mr
);
666 gpa_region_size
= int128_make64(size
);
667 if (int128_lt(mrs
.size
, gpa_region_size
)) {
668 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
670 memory_region_unref(mrs
.mr
);
675 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
678 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
680 hwaddr addr
= qdict_get_int(qdict
, "addr");
681 Error
*local_err
= NULL
;
682 MemoryRegion
*mr
= NULL
;
685 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
687 error_report_err(local_err
);
691 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
693 addr
, mr
->name
, ptr
);
695 memory_region_unref(mr
);
698 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
700 target_ulong addr
= qdict_get_int(qdict
, "addr");
702 CPUState
*cs
= mon_get_cpu(mon
);
706 monitor_printf(mon
, "No cpu\n");
710 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
712 monitor_printf(mon
, "Unmapped\n");
714 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
715 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
720 static uint64_t vtop(void *ptr
, Error
**errp
)
724 uintptr_t addr
= (uintptr_t) ptr
;
725 uintptr_t pagesize
= qemu_real_host_page_size();
726 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
729 fd
= open("/proc/self/pagemap", O_RDONLY
);
731 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
735 /* Force copy-on-write if necessary. */
736 qatomic_add((uint8_t *)ptr
, 0);
738 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
739 error_setg_errno(errp
, errno
, "Cannot read pagemap");
742 if ((pinfo
& (1ull << 63)) == 0) {
743 error_setg(errp
, "Page not present");
746 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
753 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
755 hwaddr addr
= qdict_get_int(qdict
, "addr");
756 Error
*local_err
= NULL
;
757 MemoryRegion
*mr
= NULL
;
761 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
763 error_report_err(local_err
);
767 physaddr
= vtop(ptr
, &local_err
);
769 error_report_err(local_err
);
771 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
772 " (%s) is 0x%" PRIx64
"\n",
773 addr
, mr
->name
, (uint64_t) physaddr
);
776 memory_region_unref(mr
);
780 static void do_print(Monitor
*mon
, const QDict
*qdict
)
782 int format
= qdict_get_int(qdict
, "format");
783 hwaddr val
= qdict_get_int(qdict
, "val");
787 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
790 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
793 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
797 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
800 monitor_printc(mon
, val
);
803 monitor_printf(mon
, "\n");
806 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
810 uint32_t start
= qdict_get_int(qdict
, "start");
811 uint32_t size
= qdict_get_int(qdict
, "size");
814 for(addr
= start
; addr
< (start
+ size
); addr
++) {
815 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
816 MEMTXATTRS_UNSPECIFIED
, NULL
);
817 /* BSD sum algorithm ('sum' Unix command) */
818 sum
= (sum
>> 1) | (sum
<< 15);
821 monitor_printf(mon
, "%05d\n", sum
);
824 static int mouse_button_state
;
826 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
828 int dx
, dy
, dz
, button
;
829 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
830 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
831 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
833 dx
= strtol(dx_str
, NULL
, 0);
834 dy
= strtol(dy_str
, NULL
, 0);
835 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
836 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
839 dz
= strtol(dz_str
, NULL
, 0);
841 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
842 qemu_input_queue_btn(NULL
, button
, true);
843 qemu_input_event_sync();
844 qemu_input_queue_btn(NULL
, button
, false);
847 qemu_input_event_sync();
850 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
852 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
853 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
854 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
855 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
857 int button_state
= qdict_get_int(qdict
, "button_state");
859 if (mouse_button_state
== button_state
) {
862 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
863 qemu_input_event_sync();
864 mouse_button_state
= button_state
;
867 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
869 int size
= qdict_get_int(qdict
, "size");
870 int addr
= qdict_get_int(qdict
, "addr");
871 int has_index
= qdict_haskey(qdict
, "index");
876 int index
= qdict_get_int(qdict
, "index");
877 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
897 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
898 suffix
, addr
, size
* 2, val
);
901 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
903 int size
= qdict_get_int(qdict
, "size");
904 int addr
= qdict_get_int(qdict
, "addr");
905 int val
= qdict_get_int(qdict
, "val");
907 addr
&= IOPORTS_MASK
;
923 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
925 Error
*local_err
= NULL
;
926 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
928 qemu_boot_set(bootdevice
, &local_err
);
930 error_report_err(local_err
);
932 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
936 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
938 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
939 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
940 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
941 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
943 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
946 /* Capture support */
947 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
949 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
954 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
955 monitor_printf(mon
, "[%d]: ", i
);
956 s
->ops
.info (s
->opaque
);
960 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
963 int n
= qdict_get_int(qdict
, "n");
966 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
968 s
->ops
.destroy (s
->opaque
);
969 QLIST_REMOVE (s
, entries
);
976 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
978 const char *path
= qdict_get_str(qdict
, "path");
979 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
980 int bits
= qdict_get_try_int(qdict
, "bits", 16);
981 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
982 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
984 AudioState
*as
= audio_state_by_name(audiodev
);
987 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
991 s
= g_malloc0 (sizeof (*s
));
993 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
994 monitor_printf(mon
, "Failed to add wave capture\n");
998 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1001 void qmp_getfd(const char *fdname
, Error
**errp
)
1003 Monitor
*cur_mon
= monitor_cur();
1007 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1009 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1013 if (qemu_isdigit(fdname
[0])) {
1015 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1016 "a name not starting with a digit");
1020 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1021 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1022 if (strcmp(monfd
->name
, fdname
) != 0) {
1028 /* Make sure close() is outside critical section */
1033 monfd
= g_new0(mon_fd_t
, 1);
1034 monfd
->name
= g_strdup(fdname
);
1037 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1040 void qmp_closefd(const char *fdname
, Error
**errp
)
1042 Monitor
*cur_mon
= monitor_cur();
1046 qemu_mutex_lock(&cur_mon
->mon_lock
);
1047 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1048 if (strcmp(monfd
->name
, fdname
) != 0) {
1052 QLIST_REMOVE(monfd
, next
);
1054 g_free(monfd
->name
);
1056 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1057 /* Make sure close() is outside critical section */
1062 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1063 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1066 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1070 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1071 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1074 if (strcmp(monfd
->name
, fdname
) != 0) {
1080 /* caller takes ownership of fd */
1081 QLIST_REMOVE(monfd
, next
);
1082 g_free(monfd
->name
);
1088 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1092 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1094 MonFdsetFd
*mon_fdset_fd
;
1095 MonFdsetFd
*mon_fdset_fd_next
;
1097 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1098 if ((mon_fdset_fd
->removed
||
1099 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1100 runstate_is_running()) {
1101 close(mon_fdset_fd
->fd
);
1102 g_free(mon_fdset_fd
->opaque
);
1103 QLIST_REMOVE(mon_fdset_fd
, next
);
1104 g_free(mon_fdset_fd
);
1108 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1109 QLIST_REMOVE(mon_fdset
, next
);
1114 void monitor_fdsets_cleanup(void)
1116 MonFdset
*mon_fdset
;
1117 MonFdset
*mon_fdset_next
;
1119 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1120 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1121 monitor_fdset_cleanup(mon_fdset
);
1125 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1126 const char *opaque
, Error
**errp
)
1129 Monitor
*mon
= monitor_cur();
1132 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1134 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1138 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1139 has_opaque
, opaque
, errp
);
1151 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1153 MonFdset
*mon_fdset
;
1154 MonFdsetFd
*mon_fdset_fd
;
1157 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1158 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1159 if (mon_fdset
->id
!= fdset_id
) {
1162 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1164 if (mon_fdset_fd
->fd
!= fd
) {
1167 mon_fdset_fd
->removed
= true;
1170 mon_fdset_fd
->removed
= true;
1173 if (has_fd
&& !mon_fdset_fd
) {
1176 monitor_fdset_cleanup(mon_fdset
);
1182 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1185 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1187 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1190 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1192 MonFdset
*mon_fdset
;
1193 MonFdsetFd
*mon_fdset_fd
;
1194 FdsetInfoList
*fdset_list
= NULL
;
1196 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1197 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1198 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1200 fdset_info
->fdset_id
= mon_fdset
->id
;
1202 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1203 FdsetFdInfo
*fdsetfd_info
;
1205 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1206 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1207 if (mon_fdset_fd
->opaque
) {
1208 fdsetfd_info
->has_opaque
= true;
1209 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1211 fdsetfd_info
->has_opaque
= false;
1214 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1217 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1223 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1224 bool has_opaque
, const char *opaque
,
1227 MonFdset
*mon_fdset
= NULL
;
1228 MonFdsetFd
*mon_fdset_fd
;
1231 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1233 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1234 /* Break if match found or match impossible due to ordering by ID */
1235 if (fdset_id
<= mon_fdset
->id
) {
1236 if (fdset_id
< mon_fdset
->id
) {
1244 if (mon_fdset
== NULL
) {
1245 int64_t fdset_id_prev
= -1;
1246 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1250 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1251 "a non-negative value");
1254 /* Use specified fdset ID */
1255 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1256 mon_fdset_cur
= mon_fdset
;
1257 if (fdset_id
< mon_fdset_cur
->id
) {
1262 /* Use first available fdset ID */
1263 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1264 mon_fdset_cur
= mon_fdset
;
1265 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1266 fdset_id_prev
= mon_fdset_cur
->id
;
1273 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1275 mon_fdset
->id
= fdset_id
;
1277 mon_fdset
->id
= fdset_id_prev
+ 1;
1280 /* The fdset list is ordered by fdset ID */
1281 if (!mon_fdset_cur
) {
1282 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1283 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1284 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1286 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1290 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1291 mon_fdset_fd
->fd
= fd
;
1292 mon_fdset_fd
->removed
= false;
1294 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1296 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1298 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1299 fdinfo
->fdset_id
= mon_fdset
->id
;
1300 fdinfo
->fd
= mon_fdset_fd
->fd
;
1305 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1310 MonFdset
*mon_fdset
;
1312 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1313 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1314 MonFdsetFd
*mon_fdset_fd
;
1315 MonFdsetFd
*mon_fdset_fd_dup
;
1320 if (mon_fdset
->id
!= fdset_id
) {
1324 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1325 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1326 if (mon_fd_flags
== -1) {
1330 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1331 fd
= mon_fdset_fd
->fd
;
1341 dup_fd
= qemu_dup_flags(fd
, flags
);
1346 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1347 mon_fdset_fd_dup
->fd
= dup_fd
;
1348 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1357 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1359 MonFdset
*mon_fdset
;
1360 MonFdsetFd
*mon_fdset_fd_dup
;
1362 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1363 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1364 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1365 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1367 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1368 g_free(mon_fdset_fd_dup
);
1369 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1370 monitor_fdset_cleanup(mon_fdset
);
1374 return mon_fdset
->id
;
1383 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1385 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1388 void monitor_fdset_dup_fd_remove(int dup_fd
)
1390 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1393 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1396 Error
*local_err
= NULL
;
1398 if (!qemu_isdigit(fdname
[0]) && mon
) {
1399 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1401 fd
= qemu_parse_fd(fdname
);
1403 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1408 error_propagate(errp
, local_err
);
1417 /* Please update hmp-commands.hx when adding or changing commands */
1418 static HMPCommand hmp_info_cmds
[] = {
1419 #include "hmp-commands-info.h"
1423 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1424 HMPCommand hmp_cmds
[] = {
1425 #include "hmp-commands.h"
1430 * Set @pval to the value in the register identified by @name.
1431 * return 0 if OK, -1 if not found
1433 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1435 const MonitorDef
*md
= target_monitor_defs();
1436 CPUState
*cs
= mon_get_cpu(mon
);
1441 if (cs
== NULL
|| md
== NULL
) {
1445 for(; md
->name
!= NULL
; md
++) {
1446 if (hmp_compare_cmd(name
, md
->name
)) {
1447 if (md
->get_value
) {
1448 *pval
= md
->get_value(mon
, md
, md
->offset
);
1450 CPUArchState
*env
= mon_get_cpu_env(mon
);
1451 ptr
= (uint8_t *)env
+ md
->offset
;
1454 *pval
= *(int32_t *)ptr
;
1457 *pval
= *(target_long
*)ptr
;
1468 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1470 *pval
= (target_long
) tmp
;
1476 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1479 if (!str
|| !option
) {
1482 if (!strncmp(option
, str
, strlen(str
))) {
1483 readline_add_completion(rs
, option
);
1487 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1490 ChardevBackendInfoList
*list
, *start
;
1496 readline_set_completion_index(rs
, len
);
1498 start
= list
= qmp_query_chardev_backends(NULL
);
1500 const char *chr_name
= list
->value
->name
;
1502 if (!strncmp(chr_name
, str
, len
)) {
1503 readline_add_completion(rs
, chr_name
);
1507 qapi_free_ChardevBackendInfoList(start
);
1510 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1519 readline_set_completion_index(rs
, len
);
1520 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1521 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1525 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1535 readline_set_completion_index(rs
, len
);
1536 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1539 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1541 name
= object_class_get_name(OBJECT_CLASS(dc
));
1543 if (dc
->user_creatable
1544 && !strncmp(name
, str
, len
)) {
1545 readline_add_completion(rs
, name
);
1552 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1562 readline_set_completion_index(rs
, len
);
1563 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1567 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1568 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1569 readline_add_completion(rs
, name
);
1576 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1578 GSList
**list
= opaque
;
1579 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1585 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1586 *list
= g_slist_append(*list
, dev
);
1592 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1594 GSList
*list
= NULL
;
1596 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1601 static void peripheral_device_del_completion(ReadLineState
*rs
,
1602 const char *str
, size_t len
)
1604 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1605 GSList
*list
, *item
;
1607 list
= qdev_build_hotpluggable_device_list(peripheral
);
1612 for (item
= list
; item
; item
= g_slist_next(item
)) {
1613 DeviceState
*dev
= item
->data
;
1615 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1616 readline_add_completion(rs
, dev
->id
);
1623 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1626 ChardevInfoList
*list
, *start
;
1632 readline_set_completion_index(rs
, len
);
1634 start
= list
= qmp_query_chardev(NULL
);
1636 ChardevInfo
*chr
= list
->value
;
1638 if (!strncmp(chr
->label
, str
, len
)) {
1639 readline_add_completion(rs
, chr
->label
);
1643 qapi_free_ChardevInfoList(start
);
1646 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1649 ChardevInfoList
*list
, *start
;
1652 readline_set_completion_index(rs
, len
);
1654 start
= list
= qmp_query_chardev(NULL
);
1656 ChardevInfo
*chr_info
= list
->value
;
1658 if (!strncmp(chr_info
->label
, str
, len
)) {
1659 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1660 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1661 readline_add_completion(rs
, chr_info
->label
);
1666 qapi_free_ChardevInfoList(start
);
1669 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1674 ringbuf_completion(rs
, str
);
1677 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1686 readline_set_completion_index(rs
, len
);
1687 peripheral_device_del_completion(rs
, str
, len
);
1690 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1692 ObjectPropertyInfoList
*list
, *start
;
1699 readline_set_completion_index(rs
, len
);
1701 start
= list
= qmp_qom_list("/objects", NULL
);
1703 ObjectPropertyInfo
*info
= list
->value
;
1705 if (!strncmp(info
->type
, "child<", 5)
1706 && !strncmp(info
->name
, str
, len
)) {
1707 readline_add_completion(rs
, info
->name
);
1711 qapi_free_ObjectPropertyInfoList(start
);
1714 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1723 sep
= strrchr(str
, '-');
1728 readline_set_completion_index(rs
, len
);
1729 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1730 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1731 readline_add_completion(rs
, QKeyCode_str(i
));
1736 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1741 readline_set_completion_index(rs
, len
);
1743 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1745 count
= qemu_find_net_clients_except(NULL
, ncs
,
1746 NET_CLIENT_DRIVER_NONE
,
1748 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1749 const char *name
= ncs
[i
]->name
;
1750 if (!strncmp(str
, name
, len
)) {
1751 readline_add_completion(rs
, name
);
1754 } else if (nb_args
== 3) {
1755 add_completion_option(rs
, str
, "on");
1756 add_completion_option(rs
, str
, "off");
1760 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1763 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1770 readline_set_completion_index(rs
, len
);
1771 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1773 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1774 const char *name
= ncs
[i
]->name
;
1775 if (strncmp(str
, name
, len
)) {
1778 if (ncs
[i
]->is_netdev
) {
1779 readline_add_completion(rs
, name
);
1784 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1789 readline_set_completion_index(rs
, len
);
1791 TraceEventIter iter
;
1793 char *pattern
= g_strdup_printf("%s*", str
);
1794 trace_event_iter_init_pattern(&iter
, pattern
);
1795 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1796 readline_add_completion(rs
, trace_event_get_name(ev
));
1802 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1807 readline_set_completion_index(rs
, len
);
1809 TraceEventIter iter
;
1811 char *pattern
= g_strdup_printf("%s*", str
);
1812 trace_event_iter_init_pattern(&iter
, pattern
);
1813 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1814 readline_add_completion(rs
, trace_event_get_name(ev
));
1817 } else if (nb_args
== 3) {
1818 add_completion_option(rs
, str
, "on");
1819 add_completion_option(rs
, str
, "off");
1823 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1830 readline_set_completion_index(rs
, strlen(str
));
1831 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1832 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1836 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1842 readline_set_completion_index(rs
, len
);
1845 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1846 const char *name
= MigrationCapability_str(i
);
1847 if (!strncmp(str
, name
, len
)) {
1848 readline_add_completion(rs
, name
);
1851 } else if (nb_args
== 3) {
1852 add_completion_option(rs
, str
, "on");
1853 add_completion_option(rs
, str
, "off");
1857 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1863 readline_set_completion_index(rs
, len
);
1866 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1867 const char *name
= MigrationParameter_str(i
);
1868 if (!strncmp(str
, name
, len
)) {
1869 readline_add_completion(rs
, name
);
1875 static void vm_completion(ReadLineState
*rs
, const char *str
)
1878 BlockDriverState
*bs
;
1879 BdrvNextIterator it
;
1882 readline_set_completion_index(rs
, len
);
1884 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1885 SnapshotInfoList
*snapshots
, *snapshot
;
1886 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1889 aio_context_acquire(ctx
);
1890 if (bdrv_can_snapshot(bs
)) {
1891 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1893 aio_context_release(ctx
);
1898 snapshot
= snapshots
;
1900 char *completion
= snapshot
->value
->name
;
1901 if (!strncmp(str
, completion
, len
)) {
1902 readline_add_completion(rs
, completion
);
1904 completion
= snapshot
->value
->id
;
1905 if (!strncmp(str
, completion
, len
)) {
1906 readline_add_completion(rs
, completion
);
1908 snapshot
= snapshot
->next
;
1910 qapi_free_SnapshotInfoList(snapshots
);
1915 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1918 vm_completion(rs
, str
);
1922 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1925 vm_completion(rs
, str
);
1930 compare_mon_cmd(const void *a
, const void *b
)
1932 return strcmp(((const HMPCommand
*)a
)->name
,
1933 ((const HMPCommand
*)b
)->name
);
1936 static void sortcmdlist(void)
1938 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1941 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1942 sizeof(*hmp_info_cmds
),
1946 void monitor_register_hmp(const char *name
, bool info
,
1947 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1949 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1951 while (table
->name
!= NULL
) {
1952 if (strcmp(table
->name
, name
) == 0) {
1953 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1959 g_assert_not_reached();
1962 void monitor_register_hmp_info_hrt(const char *name
,
1963 HumanReadableText
*(*handler
)(Error
**errp
))
1965 HMPCommand
*table
= hmp_info_cmds
;
1967 while (table
->name
!= NULL
) {
1968 if (strcmp(table
->name
, name
) == 0) {
1969 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1970 table
->cmd_info_hrt
= handler
;
1975 g_assert_not_reached();
1978 void monitor_init_globals(void)
1980 monitor_init_globals_core();
1981 monitor_init_qmp_commands();
1983 qemu_mutex_init(&mon_fdsets_lock
);