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-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
)
476 WatchdogAction action
;
479 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
480 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
483 hmp_handle_error(mon
, err
);
486 qmp_watchdog_set_action(action
, &error_abort
);
489 static void monitor_printc(Monitor
*mon
, int c
)
491 monitor_printf(mon
, "'");
494 monitor_printf(mon
, "\\'");
497 monitor_printf(mon
, "\\\\");
500 monitor_printf(mon
, "\\n");
503 monitor_printf(mon
, "\\r");
506 if (c
>= 32 && c
<= 126) {
507 monitor_printf(mon
, "%c", c
);
509 monitor_printf(mon
, "\\x%02x", c
);
513 monitor_printf(mon
, "'");
516 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
517 hwaddr addr
, int is_physical
)
519 int l
, line_size
, i
, max_digits
, len
;
522 CPUState
*cs
= mon_get_cpu(mon
);
524 if (!cs
&& (format
== 'i' || !is_physical
)) {
525 monitor_printf(mon
, "Can not dump without CPU\n");
530 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
544 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
548 max_digits
= (wsize
* 8) / 4;
552 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
561 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
563 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
569 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
570 MemTxResult r
= address_space_read(as
, addr
,
571 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
573 monitor_printf(mon
, " Cannot access memory\n");
577 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
578 monitor_printf(mon
, " Cannot access memory\n");
593 v
= (uint32_t)ldl_p(buf
+ i
);
599 monitor_printf(mon
, " ");
602 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
605 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
608 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
611 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
614 monitor_printc(mon
, v
);
619 monitor_printf(mon
, "\n");
625 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
627 int count
= qdict_get_int(qdict
, "count");
628 int format
= qdict_get_int(qdict
, "format");
629 int size
= qdict_get_int(qdict
, "size");
630 target_long addr
= qdict_get_int(qdict
, "addr");
632 memory_dump(mon
, count
, format
, size
, addr
, 0);
635 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
637 int count
= qdict_get_int(qdict
, "count");
638 int format
= qdict_get_int(qdict
, "format");
639 int size
= qdict_get_int(qdict
, "size");
640 hwaddr addr
= qdict_get_int(qdict
, "addr");
642 memory_dump(mon
, count
, format
, size
, addr
, 1);
645 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
647 Int128 gpa_region_size
;
648 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
652 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
656 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
657 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
658 memory_region_unref(mrs
.mr
);
662 gpa_region_size
= int128_make64(size
);
663 if (int128_lt(mrs
.size
, gpa_region_size
)) {
664 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
666 memory_region_unref(mrs
.mr
);
671 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
674 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
676 hwaddr addr
= qdict_get_int(qdict
, "addr");
677 Error
*local_err
= NULL
;
678 MemoryRegion
*mr
= NULL
;
681 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
683 error_report_err(local_err
);
687 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
689 addr
, mr
->name
, ptr
);
691 memory_region_unref(mr
);
694 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
696 target_ulong addr
= qdict_get_int(qdict
, "addr");
698 CPUState
*cs
= mon_get_cpu(mon
);
702 monitor_printf(mon
, "No cpu\n");
706 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
708 monitor_printf(mon
, "Unmapped\n");
710 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
711 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
716 static uint64_t vtop(void *ptr
, Error
**errp
)
720 uintptr_t addr
= (uintptr_t) ptr
;
721 uintptr_t pagesize
= qemu_real_host_page_size
;
722 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
725 fd
= open("/proc/self/pagemap", O_RDONLY
);
727 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
731 /* Force copy-on-write if necessary. */
732 qatomic_add((uint8_t *)ptr
, 0);
734 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
735 error_setg_errno(errp
, errno
, "Cannot read pagemap");
738 if ((pinfo
& (1ull << 63)) == 0) {
739 error_setg(errp
, "Page not present");
742 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
749 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
751 hwaddr addr
= qdict_get_int(qdict
, "addr");
752 Error
*local_err
= NULL
;
753 MemoryRegion
*mr
= NULL
;
757 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
759 error_report_err(local_err
);
763 physaddr
= vtop(ptr
, &local_err
);
765 error_report_err(local_err
);
767 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
768 " (%s) is 0x%" PRIx64
"\n",
769 addr
, mr
->name
, (uint64_t) physaddr
);
772 memory_region_unref(mr
);
776 static void do_print(Monitor
*mon
, const QDict
*qdict
)
778 int format
= qdict_get_int(qdict
, "format");
779 hwaddr val
= qdict_get_int(qdict
, "val");
783 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
786 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
789 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
793 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
796 monitor_printc(mon
, val
);
799 monitor_printf(mon
, "\n");
802 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
806 uint32_t start
= qdict_get_int(qdict
, "start");
807 uint32_t size
= qdict_get_int(qdict
, "size");
810 for(addr
= start
; addr
< (start
+ size
); addr
++) {
811 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
812 MEMTXATTRS_UNSPECIFIED
, NULL
);
813 /* BSD sum algorithm ('sum' Unix command) */
814 sum
= (sum
>> 1) | (sum
<< 15);
817 monitor_printf(mon
, "%05d\n", sum
);
820 static int mouse_button_state
;
822 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
824 int dx
, dy
, dz
, button
;
825 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
826 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
827 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
829 dx
= strtol(dx_str
, NULL
, 0);
830 dy
= strtol(dy_str
, NULL
, 0);
831 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
832 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
835 dz
= strtol(dz_str
, NULL
, 0);
837 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
838 qemu_input_queue_btn(NULL
, button
, true);
839 qemu_input_event_sync();
840 qemu_input_queue_btn(NULL
, button
, false);
843 qemu_input_event_sync();
846 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
848 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
849 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
850 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
851 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
853 int button_state
= qdict_get_int(qdict
, "button_state");
855 if (mouse_button_state
== button_state
) {
858 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
859 qemu_input_event_sync();
860 mouse_button_state
= button_state
;
863 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
865 int size
= qdict_get_int(qdict
, "size");
866 int addr
= qdict_get_int(qdict
, "addr");
867 int has_index
= qdict_haskey(qdict
, "index");
872 int index
= qdict_get_int(qdict
, "index");
873 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
893 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
894 suffix
, addr
, size
* 2, val
);
897 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
899 int size
= qdict_get_int(qdict
, "size");
900 int addr
= qdict_get_int(qdict
, "addr");
901 int val
= qdict_get_int(qdict
, "val");
903 addr
&= IOPORTS_MASK
;
919 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
921 Error
*local_err
= NULL
;
922 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
924 qemu_boot_set(bootdevice
, &local_err
);
926 error_report_err(local_err
);
928 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
932 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
934 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
935 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
936 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
937 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
939 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
942 #ifdef CONFIG_PROFILER
946 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
948 static int64_t last_cpu_exec_time
;
949 int64_t cpu_exec_time
;
952 cpu_exec_time
= tcg_cpu_exec_time();
953 delta
= cpu_exec_time
- last_cpu_exec_time
;
955 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
956 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
957 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
958 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
959 last_cpu_exec_time
= cpu_exec_time
;
963 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
965 monitor_printf(mon
, "Internal profiler not compiled\n");
969 /* Capture support */
970 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
972 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
977 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
978 monitor_printf(mon
, "[%d]: ", i
);
979 s
->ops
.info (s
->opaque
);
983 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
986 int n
= qdict_get_int(qdict
, "n");
989 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
991 s
->ops
.destroy (s
->opaque
);
992 QLIST_REMOVE (s
, entries
);
999 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1001 const char *path
= qdict_get_str(qdict
, "path");
1002 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1003 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1004 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1005 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1007 AudioState
*as
= audio_state_by_name(audiodev
);
1010 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1014 s
= g_malloc0 (sizeof (*s
));
1016 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1017 monitor_printf(mon
, "Failed to add wave capture\n");
1021 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1024 void qmp_getfd(const char *fdname
, Error
**errp
)
1026 Monitor
*cur_mon
= monitor_cur();
1030 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1032 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1036 if (qemu_isdigit(fdname
[0])) {
1038 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1039 "a name not starting with a digit");
1043 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1044 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1045 if (strcmp(monfd
->name
, fdname
) != 0) {
1051 /* Make sure close() is outside critical section */
1056 monfd
= g_malloc0(sizeof(mon_fd_t
));
1057 monfd
->name
= g_strdup(fdname
);
1060 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1063 void qmp_closefd(const char *fdname
, Error
**errp
)
1065 Monitor
*cur_mon
= monitor_cur();
1069 qemu_mutex_lock(&cur_mon
->mon_lock
);
1070 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1071 if (strcmp(monfd
->name
, fdname
) != 0) {
1075 QLIST_REMOVE(monfd
, next
);
1077 g_free(monfd
->name
);
1079 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1080 /* Make sure close() is outside critical section */
1085 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1086 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1089 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1093 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1094 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1097 if (strcmp(monfd
->name
, fdname
) != 0) {
1103 /* caller takes ownership of fd */
1104 QLIST_REMOVE(monfd
, next
);
1105 g_free(monfd
->name
);
1111 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1115 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1117 MonFdsetFd
*mon_fdset_fd
;
1118 MonFdsetFd
*mon_fdset_fd_next
;
1120 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1121 if ((mon_fdset_fd
->removed
||
1122 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1123 runstate_is_running()) {
1124 close(mon_fdset_fd
->fd
);
1125 g_free(mon_fdset_fd
->opaque
);
1126 QLIST_REMOVE(mon_fdset_fd
, next
);
1127 g_free(mon_fdset_fd
);
1131 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1132 QLIST_REMOVE(mon_fdset
, next
);
1137 void monitor_fdsets_cleanup(void)
1139 MonFdset
*mon_fdset
;
1140 MonFdset
*mon_fdset_next
;
1142 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1143 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1144 monitor_fdset_cleanup(mon_fdset
);
1148 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1149 const char *opaque
, Error
**errp
)
1152 Monitor
*mon
= monitor_cur();
1155 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1157 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1161 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1162 has_opaque
, opaque
, errp
);
1174 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1176 MonFdset
*mon_fdset
;
1177 MonFdsetFd
*mon_fdset_fd
;
1180 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1181 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1182 if (mon_fdset
->id
!= fdset_id
) {
1185 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1187 if (mon_fdset_fd
->fd
!= fd
) {
1190 mon_fdset_fd
->removed
= true;
1193 mon_fdset_fd
->removed
= true;
1196 if (has_fd
&& !mon_fdset_fd
) {
1199 monitor_fdset_cleanup(mon_fdset
);
1205 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1208 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1210 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1213 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1215 MonFdset
*mon_fdset
;
1216 MonFdsetFd
*mon_fdset_fd
;
1217 FdsetInfoList
*fdset_list
= NULL
;
1219 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1220 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1221 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1223 fdset_info
->fdset_id
= mon_fdset
->id
;
1225 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1226 FdsetFdInfo
*fdsetfd_info
;
1228 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1229 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1230 if (mon_fdset_fd
->opaque
) {
1231 fdsetfd_info
->has_opaque
= true;
1232 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1234 fdsetfd_info
->has_opaque
= false;
1237 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1240 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1246 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1247 bool has_opaque
, const char *opaque
,
1250 MonFdset
*mon_fdset
= NULL
;
1251 MonFdsetFd
*mon_fdset_fd
;
1254 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1256 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1257 /* Break if match found or match impossible due to ordering by ID */
1258 if (fdset_id
<= mon_fdset
->id
) {
1259 if (fdset_id
< mon_fdset
->id
) {
1267 if (mon_fdset
== NULL
) {
1268 int64_t fdset_id_prev
= -1;
1269 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1273 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1274 "a non-negative value");
1277 /* Use specified fdset ID */
1278 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1279 mon_fdset_cur
= mon_fdset
;
1280 if (fdset_id
< mon_fdset_cur
->id
) {
1285 /* Use first available fdset ID */
1286 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1287 mon_fdset_cur
= mon_fdset
;
1288 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1289 fdset_id_prev
= mon_fdset_cur
->id
;
1296 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1298 mon_fdset
->id
= fdset_id
;
1300 mon_fdset
->id
= fdset_id_prev
+ 1;
1303 /* The fdset list is ordered by fdset ID */
1304 if (!mon_fdset_cur
) {
1305 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1306 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1307 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1309 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1313 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1314 mon_fdset_fd
->fd
= fd
;
1315 mon_fdset_fd
->removed
= false;
1317 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1319 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1321 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1322 fdinfo
->fdset_id
= mon_fdset
->id
;
1323 fdinfo
->fd
= mon_fdset_fd
->fd
;
1328 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1333 MonFdset
*mon_fdset
;
1335 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1336 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1337 MonFdsetFd
*mon_fdset_fd
;
1338 MonFdsetFd
*mon_fdset_fd_dup
;
1343 if (mon_fdset
->id
!= fdset_id
) {
1347 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1348 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1349 if (mon_fd_flags
== -1) {
1353 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1354 fd
= mon_fdset_fd
->fd
;
1364 dup_fd
= qemu_dup_flags(fd
, flags
);
1369 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1370 mon_fdset_fd_dup
->fd
= dup_fd
;
1371 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1380 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1382 MonFdset
*mon_fdset
;
1383 MonFdsetFd
*mon_fdset_fd_dup
;
1385 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1386 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1387 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1388 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1390 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1391 g_free(mon_fdset_fd_dup
);
1392 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1393 monitor_fdset_cleanup(mon_fdset
);
1397 return mon_fdset
->id
;
1406 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1408 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1411 void monitor_fdset_dup_fd_remove(int dup_fd
)
1413 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1416 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1419 Error
*local_err
= NULL
;
1421 if (!qemu_isdigit(fdname
[0]) && mon
) {
1422 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1424 fd
= qemu_parse_fd(fdname
);
1426 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1431 error_propagate(errp
, local_err
);
1440 /* Please update hmp-commands.hx when adding or changing commands */
1441 static HMPCommand hmp_info_cmds
[] = {
1442 #include "hmp-commands-info.h"
1446 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1447 HMPCommand hmp_cmds
[] = {
1448 #include "hmp-commands.h"
1453 * Set @pval to the value in the register identified by @name.
1454 * return 0 if OK, -1 if not found
1456 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1458 const MonitorDef
*md
= target_monitor_defs();
1459 CPUState
*cs
= mon_get_cpu(mon
);
1464 if (cs
== NULL
|| md
== NULL
) {
1468 for(; md
->name
!= NULL
; md
++) {
1469 if (hmp_compare_cmd(name
, md
->name
)) {
1470 if (md
->get_value
) {
1471 *pval
= md
->get_value(mon
, md
, md
->offset
);
1473 CPUArchState
*env
= mon_get_cpu_env(mon
);
1474 ptr
= (uint8_t *)env
+ md
->offset
;
1477 *pval
= *(int32_t *)ptr
;
1480 *pval
= *(target_long
*)ptr
;
1491 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1493 *pval
= (target_long
) tmp
;
1499 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1502 if (!str
|| !option
) {
1505 if (!strncmp(option
, str
, strlen(str
))) {
1506 readline_add_completion(rs
, option
);
1510 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1513 ChardevBackendInfoList
*list
, *start
;
1519 readline_set_completion_index(rs
, len
);
1521 start
= list
= qmp_query_chardev_backends(NULL
);
1523 const char *chr_name
= list
->value
->name
;
1525 if (!strncmp(chr_name
, str
, len
)) {
1526 readline_add_completion(rs
, chr_name
);
1530 qapi_free_ChardevBackendInfoList(start
);
1533 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1542 readline_set_completion_index(rs
, len
);
1543 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1544 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1548 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1558 readline_set_completion_index(rs
, len
);
1559 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1562 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1564 name
= object_class_get_name(OBJECT_CLASS(dc
));
1566 if (dc
->user_creatable
1567 && !strncmp(name
, str
, len
)) {
1568 readline_add_completion(rs
, name
);
1575 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1585 readline_set_completion_index(rs
, len
);
1586 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1590 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1591 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1592 readline_add_completion(rs
, name
);
1599 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1601 GSList
**list
= opaque
;
1602 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1608 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1609 *list
= g_slist_append(*list
, dev
);
1615 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1617 GSList
*list
= NULL
;
1619 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1624 static void peripheral_device_del_completion(ReadLineState
*rs
,
1625 const char *str
, size_t len
)
1627 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1628 GSList
*list
, *item
;
1630 list
= qdev_build_hotpluggable_device_list(peripheral
);
1635 for (item
= list
; item
; item
= g_slist_next(item
)) {
1636 DeviceState
*dev
= item
->data
;
1638 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1639 readline_add_completion(rs
, dev
->id
);
1646 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1649 ChardevInfoList
*list
, *start
;
1655 readline_set_completion_index(rs
, len
);
1657 start
= list
= qmp_query_chardev(NULL
);
1659 ChardevInfo
*chr
= list
->value
;
1661 if (!strncmp(chr
->label
, str
, len
)) {
1662 readline_add_completion(rs
, chr
->label
);
1666 qapi_free_ChardevInfoList(start
);
1669 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1672 ChardevInfoList
*list
, *start
;
1675 readline_set_completion_index(rs
, len
);
1677 start
= list
= qmp_query_chardev(NULL
);
1679 ChardevInfo
*chr_info
= list
->value
;
1681 if (!strncmp(chr_info
->label
, str
, len
)) {
1682 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1683 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1684 readline_add_completion(rs
, chr_info
->label
);
1689 qapi_free_ChardevInfoList(start
);
1692 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1697 ringbuf_completion(rs
, str
);
1700 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1709 readline_set_completion_index(rs
, len
);
1710 peripheral_device_del_completion(rs
, str
, len
);
1713 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1715 ObjectPropertyInfoList
*list
, *start
;
1722 readline_set_completion_index(rs
, len
);
1724 start
= list
= qmp_qom_list("/objects", NULL
);
1726 ObjectPropertyInfo
*info
= list
->value
;
1728 if (!strncmp(info
->type
, "child<", 5)
1729 && !strncmp(info
->name
, str
, len
)) {
1730 readline_add_completion(rs
, info
->name
);
1734 qapi_free_ObjectPropertyInfoList(start
);
1737 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1746 sep
= strrchr(str
, '-');
1751 readline_set_completion_index(rs
, len
);
1752 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1753 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1754 readline_add_completion(rs
, QKeyCode_str(i
));
1759 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1764 readline_set_completion_index(rs
, len
);
1766 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1768 count
= qemu_find_net_clients_except(NULL
, ncs
,
1769 NET_CLIENT_DRIVER_NONE
,
1771 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1772 const char *name
= ncs
[i
]->name
;
1773 if (!strncmp(str
, name
, len
)) {
1774 readline_add_completion(rs
, name
);
1777 } else if (nb_args
== 3) {
1778 add_completion_option(rs
, str
, "on");
1779 add_completion_option(rs
, str
, "off");
1783 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1786 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1793 readline_set_completion_index(rs
, len
);
1794 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1796 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1797 const char *name
= ncs
[i
]->name
;
1798 if (strncmp(str
, name
, len
)) {
1801 if (ncs
[i
]->is_netdev
) {
1802 readline_add_completion(rs
, name
);
1807 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1812 readline_set_completion_index(rs
, len
);
1814 TraceEventIter iter
;
1816 char *pattern
= g_strdup_printf("%s*", str
);
1817 trace_event_iter_init_pattern(&iter
, pattern
);
1818 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1819 readline_add_completion(rs
, trace_event_get_name(ev
));
1825 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1830 readline_set_completion_index(rs
, len
);
1832 TraceEventIter iter
;
1834 char *pattern
= g_strdup_printf("%s*", str
);
1835 trace_event_iter_init_pattern(&iter
, pattern
);
1836 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1837 readline_add_completion(rs
, trace_event_get_name(ev
));
1840 } else if (nb_args
== 3) {
1841 add_completion_option(rs
, str
, "on");
1842 add_completion_option(rs
, str
, "off");
1846 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1853 readline_set_completion_index(rs
, strlen(str
));
1854 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1855 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1859 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1865 readline_set_completion_index(rs
, len
);
1868 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1869 const char *name
= MigrationCapability_str(i
);
1870 if (!strncmp(str
, name
, len
)) {
1871 readline_add_completion(rs
, name
);
1874 } else if (nb_args
== 3) {
1875 add_completion_option(rs
, str
, "on");
1876 add_completion_option(rs
, str
, "off");
1880 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1886 readline_set_completion_index(rs
, len
);
1889 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1890 const char *name
= MigrationParameter_str(i
);
1891 if (!strncmp(str
, name
, len
)) {
1892 readline_add_completion(rs
, name
);
1898 static void vm_completion(ReadLineState
*rs
, const char *str
)
1901 BlockDriverState
*bs
;
1902 BdrvNextIterator it
;
1905 readline_set_completion_index(rs
, len
);
1907 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1908 SnapshotInfoList
*snapshots
, *snapshot
;
1909 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1912 aio_context_acquire(ctx
);
1913 if (bdrv_can_snapshot(bs
)) {
1914 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1916 aio_context_release(ctx
);
1921 snapshot
= snapshots
;
1923 char *completion
= snapshot
->value
->name
;
1924 if (!strncmp(str
, completion
, len
)) {
1925 readline_add_completion(rs
, completion
);
1927 completion
= snapshot
->value
->id
;
1928 if (!strncmp(str
, completion
, len
)) {
1929 readline_add_completion(rs
, completion
);
1931 snapshot
= snapshot
->next
;
1933 qapi_free_SnapshotInfoList(snapshots
);
1938 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1941 vm_completion(rs
, str
);
1945 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1948 vm_completion(rs
, str
);
1953 compare_mon_cmd(const void *a
, const void *b
)
1955 return strcmp(((const HMPCommand
*)a
)->name
,
1956 ((const HMPCommand
*)b
)->name
);
1959 static void sortcmdlist(void)
1961 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1964 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1965 sizeof(*hmp_info_cmds
),
1969 void monitor_register_hmp(const char *name
, bool info
,
1970 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1972 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1974 while (table
->name
!= NULL
) {
1975 if (strcmp(table
->name
, name
) == 0) {
1976 g_assert(table
->cmd
== NULL
);
1982 g_assert_not_reached();
1985 void monitor_init_globals(void)
1987 monitor_init_globals_core();
1988 monitor_init_qmp_commands();
1990 qemu_mutex_init(&mon_fdsets_lock
);