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-trace.h"
74 #include "qapi/qapi-commands-machine.h"
75 #include "qapi/qapi-init-commands.h"
76 #include "qapi/error.h"
77 #include "qapi/qmp-event.h"
78 #include "sysemu/cpus.h"
79 #include "qemu/cutils.h"
81 #if defined(TARGET_S390X)
82 #include "hw/s390x/storage-keys.h"
83 #include "hw/s390x/storage-attributes.h"
86 /* file descriptors passed via SCM_RIGHTS */
87 typedef struct mon_fd_t mon_fd_t
;
91 QLIST_ENTRY(mon_fd_t
) next
;
94 /* file descriptor associated with a file descriptor set */
95 typedef struct MonFdsetFd MonFdsetFd
;
100 QLIST_ENTRY(MonFdsetFd
) next
;
103 /* file descriptor set containing fds passed via SCM_RIGHTS */
104 typedef struct MonFdset MonFdset
;
107 QLIST_HEAD(, MonFdsetFd
) fds
;
108 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
109 QLIST_ENTRY(MonFdset
) next
;
112 /* Protects mon_fdsets */
113 static QemuMutex mon_fdsets_lock
;
114 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
116 static HMPCommand hmp_info_cmds
[];
118 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
119 int64_t cpu_index
, Error
**errp
)
124 monitor_data_init(&hmp
.common
, false, true, false);
127 int ret
= monitor_set_cpu(&hmp
.common
, cpu_index
);
129 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
135 handle_hmp_command(&hmp
, command_line
);
137 WITH_QEMU_LOCK_GUARD(&hmp
.common
.mon_lock
) {
138 output
= g_strdup(hmp
.common
.outbuf
->str
);
142 monitor_data_destroy(&hmp
.common
);
147 * Is @name in the '|' separated list of names @list?
149 int hmp_compare_cmd(const char *name
, const char *list
)
151 const char *p
, *pstart
;
157 p
= qemu_strchrnul(p
, '|');
158 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
169 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
171 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
174 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
176 const char *tp_name
= qdict_get_str(qdict
, "name");
177 bool new_state
= qdict_get_bool(qdict
, "option");
178 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
179 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
180 Error
*local_err
= NULL
;
183 monitor_printf(mon
, "argument vcpu must be positive");
187 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
189 error_report_err(local_err
);
193 #ifdef CONFIG_TRACE_SIMPLE
194 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
196 const char *op
= qdict_get_try_str(qdict
, "op");
197 const char *arg
= qdict_get_try_str(qdict
, "arg");
200 st_print_trace_file_status();
201 } else if (!strcmp(op
, "on")) {
202 st_set_trace_file_enabled(true);
203 } else if (!strcmp(op
, "off")) {
204 st_set_trace_file_enabled(false);
205 } else if (!strcmp(op
, "flush")) {
206 st_flush_trace_buffer();
207 } else if (!strcmp(op
, "set")) {
209 st_set_trace_file(arg
);
212 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
213 help_cmd(mon
, "trace-file");
218 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
220 help_cmd(mon
, "info");
223 static void monitor_init_qmp_commands(void)
227 * - qmp_commands contains all QMP commands
228 * - qmp_cap_negotiation_commands contains just
229 * "qmp_capabilities", to enforce capability negotiation
232 qmp_init_marshal(&qmp_commands
);
234 qmp_register_command(&qmp_commands
, "device_add",
235 qmp_device_add
, 0, 0);
237 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
238 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
239 qmp_marshal_qmp_capabilities
,
240 QCO_ALLOW_PRECONFIG
, 0);
243 /* Set the current CPU defined by the user. Callers must hold BQL. */
244 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
248 cpu
= qemu_get_cpu(cpu_index
);
252 g_free(mon
->mon_cpu_path
);
253 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
257 /* Callers must hold BQL. */
258 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
260 CPUState
*cpu
= NULL
;
262 if (mon
->mon_cpu_path
) {
263 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
266 g_free(mon
->mon_cpu_path
);
267 mon
->mon_cpu_path
= NULL
;
270 if (!mon
->mon_cpu_path
) {
274 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
279 cpu_synchronize_state(cpu
);
284 CPUState
*mon_get_cpu(Monitor
*mon
)
286 return mon_get_cpu_sync(mon
, true);
289 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
291 CPUState
*cs
= mon_get_cpu(mon
);
293 return cs
? cs
->env_ptr
: NULL
;
296 int monitor_get_cpu_index(Monitor
*mon
)
298 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
300 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
303 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
305 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
310 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
311 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
314 cs
= mon_get_cpu(mon
);
317 monitor_printf(mon
, "No CPU available\n");
321 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
325 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
327 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
328 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
329 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
330 enum QSPSortBy sort_by
;
332 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
333 qsp_report(max
, sort_by
, coalesce
);
336 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
338 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
347 str
= readline_get_history(hmp_mon
->rs
, i
);
351 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
356 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
358 const char *name
= qdict_get_try_str(qdict
, "name");
359 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
360 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
361 TraceEventInfoList
*events
;
362 TraceEventInfoList
*elem
;
363 Error
*local_err
= NULL
;
369 monitor_printf(mon
, "argument vcpu must be positive");
373 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
375 error_report_err(local_err
);
379 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
380 monitor_printf(mon
, "%s : state %u\n",
382 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
384 qapi_free_TraceEventInfoList(events
);
387 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
388 bool has_port
, int64_t port
,
389 bool has_tls_port
, int64_t tls_port
,
390 bool has_cert_subject
, const char *cert_subject
,
393 if (strcmp(protocol
, "spice") == 0) {
394 if (!qemu_using_spice(errp
)) {
398 if (!has_port
&& !has_tls_port
) {
399 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
403 if (qemu_spice
.migrate_info(hostname
,
404 has_port
? port
: -1,
405 has_tls_port
? tls_port
: -1,
407 error_setg(errp
, "Could not set up display for migration");
413 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "'spice'");
416 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
420 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");
431 if (!strcmp(items
, "none")) {
434 mask
= qemu_str_to_log_mask(items
);
436 help_cmd(mon
, "log");
443 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
445 const char *option
= qdict_get_try_str(qdict
, "option");
446 if (!option
|| !strcmp(option
, "on")) {
448 } else if (!strcmp(option
, "off")) {
451 monitor_printf(mon
, "unexpected option %s\n", option
);
455 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
457 const char *device
= qdict_get_try_str(qdict
, "device");
459 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
462 if (gdbserver_start(device
) < 0) {
463 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
465 } else if (strcmp(device
, "none") == 0) {
466 monitor_printf(mon
, "Disabled gdbserver\n");
468 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
473 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
475 const char *action
= qdict_get_str(qdict
, "action");
476 if (select_watchdog_action(action
) == -1) {
477 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
481 static void monitor_printc(Monitor
*mon
, int c
)
483 monitor_printf(mon
, "'");
486 monitor_printf(mon
, "\\'");
489 monitor_printf(mon
, "\\\\");
492 monitor_printf(mon
, "\\n");
495 monitor_printf(mon
, "\\r");
498 if (c
>= 32 && c
<= 126) {
499 monitor_printf(mon
, "%c", c
);
501 monitor_printf(mon
, "\\x%02x", c
);
505 monitor_printf(mon
, "'");
508 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
509 hwaddr addr
, int is_physical
)
511 int l
, line_size
, i
, max_digits
, len
;
514 CPUState
*cs
= mon_get_cpu(mon
);
516 if (!cs
&& (format
== 'i' || !is_physical
)) {
517 monitor_printf(mon
, "Can not dump without CPU\n");
522 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
536 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
540 max_digits
= (wsize
* 8) / 4;
544 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
553 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
555 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
561 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
562 MemTxResult r
= address_space_read(as
, addr
,
563 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
565 monitor_printf(mon
, " Cannot access memory\n");
569 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
570 monitor_printf(mon
, " Cannot access memory\n");
585 v
= (uint32_t)ldl_p(buf
+ i
);
591 monitor_printf(mon
, " ");
594 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
597 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
600 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
603 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
606 monitor_printc(mon
, v
);
611 monitor_printf(mon
, "\n");
617 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
619 int count
= qdict_get_int(qdict
, "count");
620 int format
= qdict_get_int(qdict
, "format");
621 int size
= qdict_get_int(qdict
, "size");
622 target_long addr
= qdict_get_int(qdict
, "addr");
624 memory_dump(mon
, count
, format
, size
, addr
, 0);
627 static void hmp_physical_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 hwaddr addr
= qdict_get_int(qdict
, "addr");
634 memory_dump(mon
, count
, format
, size
, addr
, 1);
637 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
639 Int128 gpa_region_size
;
640 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
644 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
648 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
649 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
650 memory_region_unref(mrs
.mr
);
654 gpa_region_size
= int128_make64(size
);
655 if (int128_lt(mrs
.size
, gpa_region_size
)) {
656 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
658 memory_region_unref(mrs
.mr
);
663 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
666 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
668 hwaddr addr
= qdict_get_int(qdict
, "addr");
669 Error
*local_err
= NULL
;
670 MemoryRegion
*mr
= NULL
;
673 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
675 error_report_err(local_err
);
679 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
681 addr
, mr
->name
, ptr
);
683 memory_region_unref(mr
);
686 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
688 target_ulong addr
= qdict_get_int(qdict
, "addr");
690 CPUState
*cs
= mon_get_cpu(mon
);
694 monitor_printf(mon
, "No cpu\n");
698 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
700 monitor_printf(mon
, "Unmapped\n");
702 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
703 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
708 static uint64_t vtop(void *ptr
, Error
**errp
)
712 uintptr_t addr
= (uintptr_t) ptr
;
713 uintptr_t pagesize
= qemu_real_host_page_size
;
714 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
717 fd
= open("/proc/self/pagemap", O_RDONLY
);
719 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
723 /* Force copy-on-write if necessary. */
724 qatomic_add((uint8_t *)ptr
, 0);
726 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
727 error_setg_errno(errp
, errno
, "Cannot read pagemap");
730 if ((pinfo
& (1ull << 63)) == 0) {
731 error_setg(errp
, "Page not present");
734 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
741 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
743 hwaddr addr
= qdict_get_int(qdict
, "addr");
744 Error
*local_err
= NULL
;
745 MemoryRegion
*mr
= NULL
;
749 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
751 error_report_err(local_err
);
755 physaddr
= vtop(ptr
, &local_err
);
757 error_report_err(local_err
);
759 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
760 " (%s) is 0x%" PRIx64
"\n",
761 addr
, mr
->name
, (uint64_t) physaddr
);
764 memory_region_unref(mr
);
768 static void do_print(Monitor
*mon
, const QDict
*qdict
)
770 int format
= qdict_get_int(qdict
, "format");
771 hwaddr val
= qdict_get_int(qdict
, "val");
775 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
778 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
781 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
785 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
788 monitor_printc(mon
, val
);
791 monitor_printf(mon
, "\n");
794 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
798 uint32_t start
= qdict_get_int(qdict
, "start");
799 uint32_t size
= qdict_get_int(qdict
, "size");
802 for(addr
= start
; addr
< (start
+ size
); addr
++) {
803 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
804 MEMTXATTRS_UNSPECIFIED
, NULL
);
805 /* BSD sum algorithm ('sum' Unix command) */
806 sum
= (sum
>> 1) | (sum
<< 15);
809 monitor_printf(mon
, "%05d\n", sum
);
812 static int mouse_button_state
;
814 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
816 int dx
, dy
, dz
, button
;
817 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
818 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
819 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
821 dx
= strtol(dx_str
, NULL
, 0);
822 dy
= strtol(dy_str
, NULL
, 0);
823 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
824 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
827 dz
= strtol(dz_str
, NULL
, 0);
829 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
830 qemu_input_queue_btn(NULL
, button
, true);
831 qemu_input_event_sync();
832 qemu_input_queue_btn(NULL
, button
, false);
835 qemu_input_event_sync();
838 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
840 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
841 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
842 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
843 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
845 int button_state
= qdict_get_int(qdict
, "button_state");
847 if (mouse_button_state
== button_state
) {
850 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
851 qemu_input_event_sync();
852 mouse_button_state
= button_state
;
855 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
857 int size
= qdict_get_int(qdict
, "size");
858 int addr
= qdict_get_int(qdict
, "addr");
859 int has_index
= qdict_haskey(qdict
, "index");
864 int index
= qdict_get_int(qdict
, "index");
865 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
885 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
886 suffix
, addr
, size
* 2, val
);
889 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
891 int size
= qdict_get_int(qdict
, "size");
892 int addr
= qdict_get_int(qdict
, "addr");
893 int val
= qdict_get_int(qdict
, "val");
895 addr
&= IOPORTS_MASK
;
911 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
913 Error
*local_err
= NULL
;
914 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
916 qemu_boot_set(bootdevice
, &local_err
);
918 error_report_err(local_err
);
920 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
924 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
926 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
927 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
928 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
929 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
931 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
934 #ifdef CONFIG_PROFILER
938 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
940 static int64_t last_cpu_exec_time
;
941 int64_t cpu_exec_time
;
944 cpu_exec_time
= tcg_cpu_exec_time();
945 delta
= cpu_exec_time
- last_cpu_exec_time
;
947 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
948 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
949 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
950 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
951 last_cpu_exec_time
= cpu_exec_time
;
955 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
957 monitor_printf(mon
, "Internal profiler not compiled\n");
961 /* Capture support */
962 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
964 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
969 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
970 monitor_printf(mon
, "[%d]: ", i
);
971 s
->ops
.info (s
->opaque
);
975 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
978 int n
= qdict_get_int(qdict
, "n");
981 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
983 s
->ops
.destroy (s
->opaque
);
984 QLIST_REMOVE (s
, entries
);
991 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
993 const char *path
= qdict_get_str(qdict
, "path");
994 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
995 int bits
= qdict_get_try_int(qdict
, "bits", 16);
996 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
997 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
999 AudioState
*as
= audio_state_by_name(audiodev
);
1002 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1006 s
= g_malloc0 (sizeof (*s
));
1008 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1009 monitor_printf(mon
, "Failed to add wave capture\n");
1013 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1016 void qmp_getfd(const char *fdname
, Error
**errp
)
1018 Monitor
*cur_mon
= monitor_cur();
1022 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1024 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1028 if (qemu_isdigit(fdname
[0])) {
1030 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1031 "a name not starting with a digit");
1035 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1036 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1037 if (strcmp(monfd
->name
, fdname
) != 0) {
1043 /* Make sure close() is outside critical section */
1048 monfd
= g_malloc0(sizeof(mon_fd_t
));
1049 monfd
->name
= g_strdup(fdname
);
1052 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1055 void qmp_closefd(const char *fdname
, Error
**errp
)
1057 Monitor
*cur_mon
= monitor_cur();
1061 qemu_mutex_lock(&cur_mon
->mon_lock
);
1062 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1063 if (strcmp(monfd
->name
, fdname
) != 0) {
1067 QLIST_REMOVE(monfd
, next
);
1069 g_free(monfd
->name
);
1071 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1072 /* Make sure close() is outside critical section */
1077 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1078 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1081 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1085 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1086 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1089 if (strcmp(monfd
->name
, fdname
) != 0) {
1095 /* caller takes ownership of fd */
1096 QLIST_REMOVE(monfd
, next
);
1097 g_free(monfd
->name
);
1103 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1107 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1109 MonFdsetFd
*mon_fdset_fd
;
1110 MonFdsetFd
*mon_fdset_fd_next
;
1112 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1113 if ((mon_fdset_fd
->removed
||
1114 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1115 runstate_is_running()) {
1116 close(mon_fdset_fd
->fd
);
1117 g_free(mon_fdset_fd
->opaque
);
1118 QLIST_REMOVE(mon_fdset_fd
, next
);
1119 g_free(mon_fdset_fd
);
1123 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1124 QLIST_REMOVE(mon_fdset
, next
);
1129 void monitor_fdsets_cleanup(void)
1131 MonFdset
*mon_fdset
;
1132 MonFdset
*mon_fdset_next
;
1134 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1135 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1136 monitor_fdset_cleanup(mon_fdset
);
1140 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1141 const char *opaque
, Error
**errp
)
1144 Monitor
*mon
= monitor_cur();
1147 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1149 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1153 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1154 has_opaque
, opaque
, errp
);
1166 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1168 MonFdset
*mon_fdset
;
1169 MonFdsetFd
*mon_fdset_fd
;
1172 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1173 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1174 if (mon_fdset
->id
!= fdset_id
) {
1177 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1179 if (mon_fdset_fd
->fd
!= fd
) {
1182 mon_fdset_fd
->removed
= true;
1185 mon_fdset_fd
->removed
= true;
1188 if (has_fd
&& !mon_fdset_fd
) {
1191 monitor_fdset_cleanup(mon_fdset
);
1197 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1200 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1202 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1205 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1207 MonFdset
*mon_fdset
;
1208 MonFdsetFd
*mon_fdset_fd
;
1209 FdsetInfoList
*fdset_list
= NULL
;
1211 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1212 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1213 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1215 fdset_info
->fdset_id
= mon_fdset
->id
;
1217 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1218 FdsetFdInfo
*fdsetfd_info
;
1220 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1221 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1222 if (mon_fdset_fd
->opaque
) {
1223 fdsetfd_info
->has_opaque
= true;
1224 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1226 fdsetfd_info
->has_opaque
= false;
1229 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1232 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1238 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1239 bool has_opaque
, const char *opaque
,
1242 MonFdset
*mon_fdset
= NULL
;
1243 MonFdsetFd
*mon_fdset_fd
;
1246 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1248 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1249 /* Break if match found or match impossible due to ordering by ID */
1250 if (fdset_id
<= mon_fdset
->id
) {
1251 if (fdset_id
< mon_fdset
->id
) {
1259 if (mon_fdset
== NULL
) {
1260 int64_t fdset_id_prev
= -1;
1261 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1265 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1266 "a non-negative value");
1269 /* Use specified fdset ID */
1270 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1271 mon_fdset_cur
= mon_fdset
;
1272 if (fdset_id
< mon_fdset_cur
->id
) {
1277 /* Use first available fdset ID */
1278 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1279 mon_fdset_cur
= mon_fdset
;
1280 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1281 fdset_id_prev
= mon_fdset_cur
->id
;
1288 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1290 mon_fdset
->id
= fdset_id
;
1292 mon_fdset
->id
= fdset_id_prev
+ 1;
1295 /* The fdset list is ordered by fdset ID */
1296 if (!mon_fdset_cur
) {
1297 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1298 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1299 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1301 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1305 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1306 mon_fdset_fd
->fd
= fd
;
1307 mon_fdset_fd
->removed
= false;
1309 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1311 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1313 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1314 fdinfo
->fdset_id
= mon_fdset
->id
;
1315 fdinfo
->fd
= mon_fdset_fd
->fd
;
1320 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1325 MonFdset
*mon_fdset
;
1327 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1328 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1329 MonFdsetFd
*mon_fdset_fd
;
1330 MonFdsetFd
*mon_fdset_fd_dup
;
1335 if (mon_fdset
->id
!= fdset_id
) {
1339 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1340 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1341 if (mon_fd_flags
== -1) {
1345 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1346 fd
= mon_fdset_fd
->fd
;
1356 dup_fd
= qemu_dup_flags(fd
, flags
);
1361 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1362 mon_fdset_fd_dup
->fd
= dup_fd
;
1363 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1372 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1374 MonFdset
*mon_fdset
;
1375 MonFdsetFd
*mon_fdset_fd_dup
;
1377 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1378 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1379 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1380 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1382 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1383 g_free(mon_fdset_fd_dup
);
1384 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1385 monitor_fdset_cleanup(mon_fdset
);
1389 return mon_fdset
->id
;
1398 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1400 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1403 void monitor_fdset_dup_fd_remove(int dup_fd
)
1405 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1408 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1411 Error
*local_err
= NULL
;
1413 if (!qemu_isdigit(fdname
[0]) && mon
) {
1414 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1416 fd
= qemu_parse_fd(fdname
);
1418 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1423 error_propagate(errp
, local_err
);
1432 /* Please update hmp-commands.hx when adding or changing commands */
1433 static HMPCommand hmp_info_cmds
[] = {
1434 #include "hmp-commands-info.h"
1438 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1439 HMPCommand hmp_cmds
[] = {
1440 #include "hmp-commands.h"
1445 * Set @pval to the value in the register identified by @name.
1446 * return 0 if OK, -1 if not found
1448 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1450 const MonitorDef
*md
= target_monitor_defs();
1451 CPUState
*cs
= mon_get_cpu(mon
);
1456 if (cs
== NULL
|| md
== NULL
) {
1460 for(; md
->name
!= NULL
; md
++) {
1461 if (hmp_compare_cmd(name
, md
->name
)) {
1462 if (md
->get_value
) {
1463 *pval
= md
->get_value(mon
, md
, md
->offset
);
1465 CPUArchState
*env
= mon_get_cpu_env(mon
);
1466 ptr
= (uint8_t *)env
+ md
->offset
;
1469 *pval
= *(int32_t *)ptr
;
1472 *pval
= *(target_long
*)ptr
;
1483 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1485 *pval
= (target_long
) tmp
;
1491 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1494 if (!str
|| !option
) {
1497 if (!strncmp(option
, str
, strlen(str
))) {
1498 readline_add_completion(rs
, option
);
1502 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1505 ChardevBackendInfoList
*list
, *start
;
1511 readline_set_completion_index(rs
, len
);
1513 start
= list
= qmp_query_chardev_backends(NULL
);
1515 const char *chr_name
= list
->value
->name
;
1517 if (!strncmp(chr_name
, str
, len
)) {
1518 readline_add_completion(rs
, chr_name
);
1522 qapi_free_ChardevBackendInfoList(start
);
1525 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1534 readline_set_completion_index(rs
, len
);
1535 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1536 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1540 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1550 readline_set_completion_index(rs
, len
);
1551 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1554 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1556 name
= object_class_get_name(OBJECT_CLASS(dc
));
1558 if (dc
->user_creatable
1559 && !strncmp(name
, str
, len
)) {
1560 readline_add_completion(rs
, name
);
1567 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1577 readline_set_completion_index(rs
, len
);
1578 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1582 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1583 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1584 readline_add_completion(rs
, name
);
1591 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1593 GSList
**list
= opaque
;
1594 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1600 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1601 *list
= g_slist_append(*list
, dev
);
1607 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1609 GSList
*list
= NULL
;
1611 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1616 static void peripheral_device_del_completion(ReadLineState
*rs
,
1617 const char *str
, size_t len
)
1619 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1620 GSList
*list
, *item
;
1622 list
= qdev_build_hotpluggable_device_list(peripheral
);
1627 for (item
= list
; item
; item
= g_slist_next(item
)) {
1628 DeviceState
*dev
= item
->data
;
1630 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1631 readline_add_completion(rs
, dev
->id
);
1638 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1641 ChardevInfoList
*list
, *start
;
1647 readline_set_completion_index(rs
, len
);
1649 start
= list
= qmp_query_chardev(NULL
);
1651 ChardevInfo
*chr
= list
->value
;
1653 if (!strncmp(chr
->label
, str
, len
)) {
1654 readline_add_completion(rs
, chr
->label
);
1658 qapi_free_ChardevInfoList(start
);
1661 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1664 ChardevInfoList
*list
, *start
;
1667 readline_set_completion_index(rs
, len
);
1669 start
= list
= qmp_query_chardev(NULL
);
1671 ChardevInfo
*chr_info
= list
->value
;
1673 if (!strncmp(chr_info
->label
, str
, len
)) {
1674 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1675 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1676 readline_add_completion(rs
, chr_info
->label
);
1681 qapi_free_ChardevInfoList(start
);
1684 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1689 ringbuf_completion(rs
, str
);
1692 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1701 readline_set_completion_index(rs
, len
);
1702 peripheral_device_del_completion(rs
, str
, len
);
1705 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1707 ObjectPropertyInfoList
*list
, *start
;
1714 readline_set_completion_index(rs
, len
);
1716 start
= list
= qmp_qom_list("/objects", NULL
);
1718 ObjectPropertyInfo
*info
= list
->value
;
1720 if (!strncmp(info
->type
, "child<", 5)
1721 && !strncmp(info
->name
, str
, len
)) {
1722 readline_add_completion(rs
, info
->name
);
1726 qapi_free_ObjectPropertyInfoList(start
);
1729 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1738 sep
= strrchr(str
, '-');
1743 readline_set_completion_index(rs
, len
);
1744 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1745 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1746 readline_add_completion(rs
, QKeyCode_str(i
));
1751 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1756 readline_set_completion_index(rs
, len
);
1758 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1760 count
= qemu_find_net_clients_except(NULL
, ncs
,
1761 NET_CLIENT_DRIVER_NONE
,
1763 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1764 const char *name
= ncs
[i
]->name
;
1765 if (!strncmp(str
, name
, len
)) {
1766 readline_add_completion(rs
, name
);
1769 } else if (nb_args
== 3) {
1770 add_completion_option(rs
, str
, "on");
1771 add_completion_option(rs
, str
, "off");
1775 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1778 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1785 readline_set_completion_index(rs
, len
);
1786 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1788 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1789 const char *name
= ncs
[i
]->name
;
1790 if (strncmp(str
, name
, len
)) {
1793 if (ncs
[i
]->is_netdev
) {
1794 readline_add_completion(rs
, name
);
1799 void info_trace_events_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
));
1817 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1822 readline_set_completion_index(rs
, len
);
1824 TraceEventIter iter
;
1826 char *pattern
= g_strdup_printf("%s*", str
);
1827 trace_event_iter_init_pattern(&iter
, pattern
);
1828 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1829 readline_add_completion(rs
, trace_event_get_name(ev
));
1832 } else if (nb_args
== 3) {
1833 add_completion_option(rs
, str
, "on");
1834 add_completion_option(rs
, str
, "off");
1838 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1845 readline_set_completion_index(rs
, strlen(str
));
1846 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1847 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1851 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1857 readline_set_completion_index(rs
, len
);
1860 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1861 const char *name
= MigrationCapability_str(i
);
1862 if (!strncmp(str
, name
, len
)) {
1863 readline_add_completion(rs
, name
);
1866 } else if (nb_args
== 3) {
1867 add_completion_option(rs
, str
, "on");
1868 add_completion_option(rs
, str
, "off");
1872 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1878 readline_set_completion_index(rs
, len
);
1881 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1882 const char *name
= MigrationParameter_str(i
);
1883 if (!strncmp(str
, name
, len
)) {
1884 readline_add_completion(rs
, name
);
1890 static void vm_completion(ReadLineState
*rs
, const char *str
)
1893 BlockDriverState
*bs
;
1894 BdrvNextIterator it
;
1897 readline_set_completion_index(rs
, len
);
1899 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1900 SnapshotInfoList
*snapshots
, *snapshot
;
1901 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1904 aio_context_acquire(ctx
);
1905 if (bdrv_can_snapshot(bs
)) {
1906 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1908 aio_context_release(ctx
);
1913 snapshot
= snapshots
;
1915 char *completion
= snapshot
->value
->name
;
1916 if (!strncmp(str
, completion
, len
)) {
1917 readline_add_completion(rs
, completion
);
1919 completion
= snapshot
->value
->id
;
1920 if (!strncmp(str
, completion
, len
)) {
1921 readline_add_completion(rs
, completion
);
1923 snapshot
= snapshot
->next
;
1925 qapi_free_SnapshotInfoList(snapshots
);
1930 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1933 vm_completion(rs
, str
);
1937 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1940 vm_completion(rs
, str
);
1945 compare_mon_cmd(const void *a
, const void *b
)
1947 return strcmp(((const HMPCommand
*)a
)->name
,
1948 ((const HMPCommand
*)b
)->name
);
1951 static void sortcmdlist(void)
1953 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1956 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1957 sizeof(*hmp_info_cmds
),
1961 void monitor_register_hmp(const char *name
, bool info
,
1962 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1964 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1966 while (table
->name
!= NULL
) {
1967 if (strcmp(table
->name
, name
) == 0) {
1968 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1974 g_assert_not_reached();
1977 void monitor_register_hmp_info_hrt(const char *name
,
1978 HumanReadableText
*(*handler
)(Error
**errp
))
1980 HMPCommand
*table
= hmp_info_cmds
;
1982 while (table
->name
!= NULL
) {
1983 if (strcmp(table
->name
, name
) == 0) {
1984 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1985 table
->cmd_info_hrt
= handler
;
1990 g_assert_not_reached();
1993 void monitor_init_globals(void)
1995 monitor_init_globals_core();
1996 monitor_init_qmp_commands();
1998 qemu_mutex_init(&mon_fdsets_lock
);