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"
28 #include "monitor/qdev.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
35 #include "net/slirp.h"
36 #include "ui/qemu-spice.h"
37 #include "qemu/config-file.h"
38 #include "qemu/ctype.h"
39 #include "ui/console.h"
41 #include "audio/audio.h"
42 #include "disas/disas.h"
43 #include "sysemu/balloon.h"
44 #include "qemu/timer.h"
45 #include "sysemu/hw_accel.h"
46 #include "sysemu/runstate.h"
47 #include "authz/list.h"
48 #include "qapi/util.h"
49 #include "sysemu/blockdev.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/tcg.h"
52 #include "sysemu/tpm.h"
53 #include "qapi/qmp/qdict.h"
54 #include "qapi/qmp/qerror.h"
55 #include "qapi/qmp/qstring.h"
56 #include "qom/object_interfaces.h"
57 #include "trace/control.h"
58 #include "monitor/hmp-target.h"
59 #include "monitor/hmp.h"
60 #ifdef CONFIG_TRACE_SIMPLE
61 #include "trace/simple.h"
63 #include "exec/memory.h"
64 #include "exec/exec-all.h"
65 #include "qemu/option.h"
66 #include "qemu/thread.h"
67 #include "block/qapi.h"
68 #include "block/block-hmp-cmds.h"
69 #include "qapi/qapi-commands-char.h"
70 #include "qapi/qapi-commands-control.h"
71 #include "qapi/qapi-commands-migration.h"
72 #include "qapi/qapi-commands-misc.h"
73 #include "qapi/qapi-commands-qom.h"
74 #include "qapi/qapi-commands-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"
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 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
140 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
142 output
= g_strdup("");
147 monitor_data_destroy(&hmp
.common
);
152 * Is @name in the '|' separated list of names @list?
154 int hmp_compare_cmd(const char *name
, const char *list
)
156 const char *p
, *pstart
;
162 p
= qemu_strchrnul(p
, '|');
163 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
174 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
176 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
179 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
181 const char *tp_name
= qdict_get_str(qdict
, "name");
182 bool new_state
= qdict_get_bool(qdict
, "option");
183 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
184 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
185 Error
*local_err
= NULL
;
188 monitor_printf(mon
, "argument vcpu must be positive");
192 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
194 error_report_err(local_err
);
198 #ifdef CONFIG_TRACE_SIMPLE
199 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
201 const char *op
= qdict_get_try_str(qdict
, "op");
202 const char *arg
= qdict_get_try_str(qdict
, "arg");
205 st_print_trace_file_status();
206 } else if (!strcmp(op
, "on")) {
207 st_set_trace_file_enabled(true);
208 } else if (!strcmp(op
, "off")) {
209 st_set_trace_file_enabled(false);
210 } else if (!strcmp(op
, "flush")) {
211 st_flush_trace_buffer();
212 } else if (!strcmp(op
, "set")) {
214 st_set_trace_file(arg
);
217 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
218 help_cmd(mon
, "trace-file");
223 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
225 help_cmd(mon
, "info");
228 static void monitor_init_qmp_commands(void)
232 * - qmp_commands contains all QMP commands
233 * - qmp_cap_negotiation_commands contains just
234 * "qmp_capabilities", to enforce capability negotiation
237 qmp_init_marshal(&qmp_commands
);
239 qmp_register_command(&qmp_commands
, "query-qmp-schema",
240 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
241 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
243 qmp_register_command(&qmp_commands
, "object-add", qmp_object_add
,
246 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
247 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
248 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
251 /* Set the current CPU defined by the user. Callers must hold BQL. */
252 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
256 cpu
= qemu_get_cpu(cpu_index
);
260 g_free(mon
->mon_cpu_path
);
261 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
265 /* Callers must hold BQL. */
266 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
268 CPUState
*cpu
= NULL
;
270 if (mon
->mon_cpu_path
) {
271 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
274 g_free(mon
->mon_cpu_path
);
275 mon
->mon_cpu_path
= NULL
;
278 if (!mon
->mon_cpu_path
) {
282 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
287 cpu_synchronize_state(cpu
);
292 CPUState
*mon_get_cpu(void)
294 return mon_get_cpu_sync(monitor_cur(), true);
297 CPUArchState
*mon_get_cpu_env(void)
299 CPUState
*cs
= mon_get_cpu();
301 return cs
? cs
->env_ptr
: NULL
;
304 int monitor_get_cpu_index(Monitor
*mon
)
306 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
308 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
311 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
313 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
318 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
319 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
325 monitor_printf(mon
, "No CPU available\n");
329 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
334 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
336 if (!tcg_enabled()) {
337 error_report("JIT information is only available with accel=tcg");
345 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
351 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
353 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
354 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
355 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
356 enum QSPSortBy sort_by
;
358 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
359 qsp_report(max
, sort_by
, coalesce
);
362 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
364 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
373 str
= readline_get_history(hmp_mon
->rs
, i
);
377 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
382 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
384 CPUState
*cs
= mon_get_cpu();
387 monitor_printf(mon
, "No CPU available\n");
390 cpu_dump_statistics(cs
, 0);
393 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
395 const char *name
= qdict_get_try_str(qdict
, "name");
396 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
397 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
398 TraceEventInfoList
*events
;
399 TraceEventInfoList
*elem
;
400 Error
*local_err
= NULL
;
406 monitor_printf(mon
, "argument vcpu must be positive");
410 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
412 error_report_err(local_err
);
416 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
417 monitor_printf(mon
, "%s : state %u\n",
419 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
421 qapi_free_TraceEventInfoList(events
);
424 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
425 bool has_port
, int64_t port
,
426 bool has_tls_port
, int64_t tls_port
,
427 bool has_cert_subject
, const char *cert_subject
,
430 if (strcmp(protocol
, "spice") == 0) {
431 if (!qemu_using_spice(errp
)) {
435 if (!has_port
&& !has_tls_port
) {
436 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
440 if (qemu_spice
.migrate_info(hostname
,
441 has_port
? port
: -1,
442 has_tls_port
? tls_port
: -1,
444 error_setg(errp
, QERR_UNDEFINED_ERROR
);
450 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
453 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
457 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
459 error_report_err(err
);
463 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
466 const char *items
= qdict_get_str(qdict
, "items");
468 if (!strcmp(items
, "none")) {
471 mask
= qemu_str_to_log_mask(items
);
473 help_cmd(mon
, "log");
480 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
482 const char *option
= qdict_get_try_str(qdict
, "option");
483 if (!option
|| !strcmp(option
, "on")) {
485 } else if (!strcmp(option
, "off")) {
488 monitor_printf(mon
, "unexpected option %s\n", option
);
492 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
494 const char *device
= qdict_get_try_str(qdict
, "device");
496 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
497 if (gdbserver_start(device
) < 0) {
498 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
500 } else if (strcmp(device
, "none") == 0) {
501 monitor_printf(mon
, "Disabled gdbserver\n");
503 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
508 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
510 const char *action
= qdict_get_str(qdict
, "action");
511 if (select_watchdog_action(action
) == -1) {
512 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
516 static void monitor_printc(Monitor
*mon
, int c
)
518 monitor_printf(mon
, "'");
521 monitor_printf(mon
, "\\'");
524 monitor_printf(mon
, "\\\\");
527 monitor_printf(mon
, "\\n");
530 monitor_printf(mon
, "\\r");
533 if (c
>= 32 && c
<= 126) {
534 monitor_printf(mon
, "%c", c
);
536 monitor_printf(mon
, "\\x%02x", c
);
540 monitor_printf(mon
, "'");
543 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
544 hwaddr addr
, int is_physical
)
546 int l
, line_size
, i
, max_digits
, len
;
549 CPUState
*cs
= mon_get_cpu();
551 if (!cs
&& (format
== 'i' || !is_physical
)) {
552 monitor_printf(mon
, "Can not dump without CPU\n");
557 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
570 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
574 max_digits
= (wsize
* 8) / 4;
578 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
587 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
589 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
594 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
595 MemTxResult r
= address_space_read(as
, addr
,
596 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
598 monitor_printf(mon
, " Cannot access memory\n");
602 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
603 monitor_printf(mon
, " Cannot access memory\n");
618 v
= (uint32_t)ldl_p(buf
+ i
);
624 monitor_printf(mon
, " ");
627 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
630 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
633 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
636 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
639 monitor_printc(mon
, v
);
644 monitor_printf(mon
, "\n");
650 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
652 int count
= qdict_get_int(qdict
, "count");
653 int format
= qdict_get_int(qdict
, "format");
654 int size
= qdict_get_int(qdict
, "size");
655 target_long addr
= qdict_get_int(qdict
, "addr");
657 memory_dump(mon
, count
, format
, size
, addr
, 0);
660 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
662 int count
= qdict_get_int(qdict
, "count");
663 int format
= qdict_get_int(qdict
, "format");
664 int size
= qdict_get_int(qdict
, "size");
665 hwaddr addr
= qdict_get_int(qdict
, "addr");
667 memory_dump(mon
, count
, format
, size
, addr
, 1);
670 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
672 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
676 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
680 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
681 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
682 memory_region_unref(mrs
.mr
);
687 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
690 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
692 hwaddr addr
= qdict_get_int(qdict
, "addr");
693 Error
*local_err
= NULL
;
694 MemoryRegion
*mr
= NULL
;
697 ptr
= gpa2hva(&mr
, addr
, &local_err
);
699 error_report_err(local_err
);
703 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
705 addr
, mr
->name
, ptr
);
707 memory_region_unref(mr
);
710 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
712 target_ulong addr
= qdict_get_int(qdict
, "addr");
714 CPUState
*cs
= mon_get_cpu();
718 monitor_printf(mon
, "No cpu\n");
722 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
724 monitor_printf(mon
, "Unmapped\n");
726 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
727 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
732 static uint64_t vtop(void *ptr
, Error
**errp
)
736 uintptr_t addr
= (uintptr_t) ptr
;
737 uintptr_t pagesize
= qemu_real_host_page_size
;
738 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
741 fd
= open("/proc/self/pagemap", O_RDONLY
);
743 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
747 /* Force copy-on-write if necessary. */
748 qatomic_add((uint8_t *)ptr
, 0);
750 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
751 error_setg_errno(errp
, errno
, "Cannot read pagemap");
754 if ((pinfo
& (1ull << 63)) == 0) {
755 error_setg(errp
, "Page not present");
758 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
765 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
767 hwaddr addr
= qdict_get_int(qdict
, "addr");
768 Error
*local_err
= NULL
;
769 MemoryRegion
*mr
= NULL
;
773 ptr
= gpa2hva(&mr
, addr
, &local_err
);
775 error_report_err(local_err
);
779 physaddr
= vtop(ptr
, &local_err
);
781 error_report_err(local_err
);
783 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
784 " (%s) is 0x%" PRIx64
"\n",
785 addr
, mr
->name
, (uint64_t) physaddr
);
788 memory_region_unref(mr
);
792 static void do_print(Monitor
*mon
, const QDict
*qdict
)
794 int format
= qdict_get_int(qdict
, "format");
795 hwaddr val
= qdict_get_int(qdict
, "val");
799 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
802 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
805 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
809 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
812 monitor_printc(mon
, val
);
815 monitor_printf(mon
, "\n");
818 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
822 uint32_t start
= qdict_get_int(qdict
, "start");
823 uint32_t size
= qdict_get_int(qdict
, "size");
826 for(addr
= start
; addr
< (start
+ size
); addr
++) {
827 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
828 MEMTXATTRS_UNSPECIFIED
, NULL
);
829 /* BSD sum algorithm ('sum' Unix command) */
830 sum
= (sum
>> 1) | (sum
<< 15);
833 monitor_printf(mon
, "%05d\n", sum
);
836 static int mouse_button_state
;
838 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
840 int dx
, dy
, dz
, button
;
841 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
842 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
843 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
845 dx
= strtol(dx_str
, NULL
, 0);
846 dy
= strtol(dy_str
, NULL
, 0);
847 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
848 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
851 dz
= strtol(dz_str
, NULL
, 0);
853 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
854 qemu_input_queue_btn(NULL
, button
, true);
855 qemu_input_event_sync();
856 qemu_input_queue_btn(NULL
, button
, false);
859 qemu_input_event_sync();
862 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
864 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
865 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
866 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
867 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
869 int button_state
= qdict_get_int(qdict
, "button_state");
871 if (mouse_button_state
== button_state
) {
874 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
875 qemu_input_event_sync();
876 mouse_button_state
= button_state
;
879 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
881 int size
= qdict_get_int(qdict
, "size");
882 int addr
= qdict_get_int(qdict
, "addr");
883 int has_index
= qdict_haskey(qdict
, "index");
888 int index
= qdict_get_int(qdict
, "index");
889 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
909 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
910 suffix
, addr
, size
* 2, val
);
913 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
915 int size
= qdict_get_int(qdict
, "size");
916 int addr
= qdict_get_int(qdict
, "addr");
917 int val
= qdict_get_int(qdict
, "val");
919 addr
&= IOPORTS_MASK
;
935 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
937 Error
*local_err
= NULL
;
938 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
940 qemu_boot_set(bootdevice
, &local_err
);
942 error_report_err(local_err
);
944 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
948 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
950 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
951 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
952 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
953 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
955 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
958 #ifdef CONFIG_PROFILER
962 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
964 static int64_t last_cpu_exec_time
;
965 int64_t cpu_exec_time
;
968 cpu_exec_time
= tcg_cpu_exec_time();
969 delta
= cpu_exec_time
- last_cpu_exec_time
;
971 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
972 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
973 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
974 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
975 last_cpu_exec_time
= cpu_exec_time
;
979 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
981 monitor_printf(mon
, "Internal profiler not compiled\n");
985 /* Capture support */
986 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
988 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
993 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
994 monitor_printf(mon
, "[%d]: ", i
);
995 s
->ops
.info (s
->opaque
);
999 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1002 int n
= qdict_get_int(qdict
, "n");
1005 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1007 s
->ops
.destroy (s
->opaque
);
1008 QLIST_REMOVE (s
, entries
);
1015 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1017 const char *path
= qdict_get_str(qdict
, "path");
1018 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1019 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1020 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1021 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1023 AudioState
*as
= audio_state_by_name(audiodev
);
1026 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1030 s
= g_malloc0 (sizeof (*s
));
1032 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1033 monitor_printf(mon
, "Failed to add wave capture\n");
1037 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1040 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1045 container
= object_get_objects_root();
1046 obj
= object_resolve_path_component(container
, name
);
1048 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1052 return QAUTHZ_LIST(obj
);
1055 static bool warn_acl
;
1056 static void hmp_warn_acl(void)
1061 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1062 "commands are deprecated with no replacement. Authorization "
1063 "for VNC should be performed using the pluggable QAuthZ "
1068 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1070 const char *aclname
= qdict_get_str(qdict
, "aclname");
1071 QAuthZList
*auth
= find_auth(mon
, aclname
);
1072 QAuthZListRuleList
*rules
;
1081 monitor_printf(mon
, "policy: %s\n",
1082 QAuthZListPolicy_str(auth
->policy
));
1084 rules
= auth
->rules
;
1086 QAuthZListRule
*rule
= rules
->value
;
1088 monitor_printf(mon
, "%zu: %s %s\n", i
,
1089 QAuthZListPolicy_str(rule
->policy
),
1091 rules
= rules
->next
;
1095 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1097 const char *aclname
= qdict_get_str(qdict
, "aclname");
1098 QAuthZList
*auth
= find_auth(mon
, aclname
);
1106 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1107 qapi_free_QAuthZListRuleList(auth
->rules
);
1109 monitor_printf(mon
, "acl: removed all rules\n");
1112 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1114 const char *aclname
= qdict_get_str(qdict
, "aclname");
1115 const char *policy
= qdict_get_str(qdict
, "policy");
1116 QAuthZList
*auth
= find_auth(mon
, aclname
);
1126 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1128 QAUTHZ_LIST_POLICY_DENY
,
1132 monitor_printf(mon
, "acl: unknown policy '%s', "
1133 "expected 'deny' or 'allow'\n", policy
);
1136 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1137 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1139 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1144 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1146 if (strchr(match
, '*')) {
1147 return QAUTHZ_LIST_FORMAT_GLOB
;
1149 return QAUTHZ_LIST_FORMAT_EXACT
;
1153 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1155 const char *aclname
= qdict_get_str(qdict
, "aclname");
1156 const char *match
= qdict_get_str(qdict
, "match");
1157 const char *policystr
= qdict_get_str(qdict
, "policy");
1158 int has_index
= qdict_haskey(qdict
, "index");
1159 int index
= qdict_get_try_int(qdict
, "index", -1);
1160 QAuthZList
*auth
= find_auth(mon
, aclname
);
1162 QAuthZListPolicy policy
;
1163 QAuthZListFormat format
;
1172 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1174 QAUTHZ_LIST_POLICY_DENY
,
1178 monitor_printf(mon
, "acl: unknown policy '%s', "
1179 "expected 'deny' or 'allow'\n", policystr
);
1183 format
= hmp_acl_get_format(match
);
1185 if (has_index
&& index
== 0) {
1186 monitor_printf(mon
, "acl: unable to add acl entry\n");
1191 i
= qauthz_list_insert_rule(auth
, match
, policy
,
1192 format
, index
- 1, &err
);
1194 i
= qauthz_list_append_rule(auth
, match
, policy
,
1198 monitor_printf(mon
, "acl: unable to add rule: %s",
1199 error_get_pretty(err
));
1202 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
1206 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1208 const char *aclname
= qdict_get_str(qdict
, "aclname");
1209 const char *match
= qdict_get_str(qdict
, "match");
1210 QAuthZList
*auth
= find_auth(mon
, aclname
);
1219 i
= qauthz_list_delete_rule(auth
, match
);
1221 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
1223 monitor_printf(mon
, "acl: no matching acl entry\n");
1227 void qmp_getfd(const char *fdname
, Error
**errp
)
1229 Monitor
*cur_mon
= monitor_cur();
1233 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1235 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1239 if (qemu_isdigit(fdname
[0])) {
1241 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1242 "a name not starting with a digit");
1246 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1247 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1248 if (strcmp(monfd
->name
, fdname
) != 0) {
1254 /* Make sure close() is outside critical section */
1259 monfd
= g_malloc0(sizeof(mon_fd_t
));
1260 monfd
->name
= g_strdup(fdname
);
1263 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1266 void qmp_closefd(const char *fdname
, Error
**errp
)
1268 Monitor
*cur_mon
= monitor_cur();
1272 qemu_mutex_lock(&cur_mon
->mon_lock
);
1273 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1274 if (strcmp(monfd
->name
, fdname
) != 0) {
1278 QLIST_REMOVE(monfd
, next
);
1280 g_free(monfd
->name
);
1282 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1283 /* Make sure close() is outside critical section */
1288 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1289 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1292 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1296 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1297 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1300 if (strcmp(monfd
->name
, fdname
) != 0) {
1306 /* caller takes ownership of fd */
1307 QLIST_REMOVE(monfd
, next
);
1308 g_free(monfd
->name
);
1314 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1318 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1320 MonFdsetFd
*mon_fdset_fd
;
1321 MonFdsetFd
*mon_fdset_fd_next
;
1323 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1324 if ((mon_fdset_fd
->removed
||
1325 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1326 runstate_is_running()) {
1327 close(mon_fdset_fd
->fd
);
1328 g_free(mon_fdset_fd
->opaque
);
1329 QLIST_REMOVE(mon_fdset_fd
, next
);
1330 g_free(mon_fdset_fd
);
1334 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1335 QLIST_REMOVE(mon_fdset
, next
);
1340 void monitor_fdsets_cleanup(void)
1342 MonFdset
*mon_fdset
;
1343 MonFdset
*mon_fdset_next
;
1345 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1346 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1347 monitor_fdset_cleanup(mon_fdset
);
1351 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1352 const char *opaque
, Error
**errp
)
1355 Monitor
*mon
= monitor_cur();
1358 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1360 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1364 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1365 has_opaque
, opaque
, errp
);
1377 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1379 MonFdset
*mon_fdset
;
1380 MonFdsetFd
*mon_fdset_fd
;
1383 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1384 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1385 if (mon_fdset
->id
!= fdset_id
) {
1388 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1390 if (mon_fdset_fd
->fd
!= fd
) {
1393 mon_fdset_fd
->removed
= true;
1396 mon_fdset_fd
->removed
= true;
1399 if (has_fd
&& !mon_fdset_fd
) {
1402 monitor_fdset_cleanup(mon_fdset
);
1408 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1411 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1413 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1416 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1418 MonFdset
*mon_fdset
;
1419 MonFdsetFd
*mon_fdset_fd
;
1420 FdsetInfoList
*fdset_list
= NULL
;
1422 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1423 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1424 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1425 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1427 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1428 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1430 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1431 FdsetFdInfoList
*fdsetfd_info
;
1433 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1434 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1435 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1436 if (mon_fdset_fd
->opaque
) {
1437 fdsetfd_info
->value
->has_opaque
= true;
1438 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1440 fdsetfd_info
->value
->has_opaque
= false;
1443 fdsetfd_info
->next
= fdsetfd_list
;
1444 fdsetfd_list
= fdsetfd_info
;
1447 fdset_info
->value
->fds
= fdsetfd_list
;
1449 fdset_info
->next
= fdset_list
;
1450 fdset_list
= fdset_info
;
1456 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1457 bool has_opaque
, const char *opaque
,
1460 MonFdset
*mon_fdset
= NULL
;
1461 MonFdsetFd
*mon_fdset_fd
;
1464 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1466 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1467 /* Break if match found or match impossible due to ordering by ID */
1468 if (fdset_id
<= mon_fdset
->id
) {
1469 if (fdset_id
< mon_fdset
->id
) {
1477 if (mon_fdset
== NULL
) {
1478 int64_t fdset_id_prev
= -1;
1479 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1483 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1484 "a non-negative value");
1487 /* Use specified fdset ID */
1488 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1489 mon_fdset_cur
= mon_fdset
;
1490 if (fdset_id
< mon_fdset_cur
->id
) {
1495 /* Use first available fdset ID */
1496 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1497 mon_fdset_cur
= mon_fdset
;
1498 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1499 fdset_id_prev
= mon_fdset_cur
->id
;
1506 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1508 mon_fdset
->id
= fdset_id
;
1510 mon_fdset
->id
= fdset_id_prev
+ 1;
1513 /* The fdset list is ordered by fdset ID */
1514 if (!mon_fdset_cur
) {
1515 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1516 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1517 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1519 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1523 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1524 mon_fdset_fd
->fd
= fd
;
1525 mon_fdset_fd
->removed
= false;
1527 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1529 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1531 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1532 fdinfo
->fdset_id
= mon_fdset
->id
;
1533 fdinfo
->fd
= mon_fdset_fd
->fd
;
1538 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1543 MonFdset
*mon_fdset
;
1545 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1546 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1547 MonFdsetFd
*mon_fdset_fd
;
1548 MonFdsetFd
*mon_fdset_fd_dup
;
1553 if (mon_fdset
->id
!= fdset_id
) {
1557 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1558 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1559 if (mon_fd_flags
== -1) {
1563 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1564 fd
= mon_fdset_fd
->fd
;
1574 dup_fd
= qemu_dup_flags(fd
, flags
);
1579 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1580 mon_fdset_fd_dup
->fd
= dup_fd
;
1581 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1590 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1592 MonFdset
*mon_fdset
;
1593 MonFdsetFd
*mon_fdset_fd_dup
;
1595 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1596 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1597 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1598 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1600 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1601 g_free(mon_fdset_fd_dup
);
1602 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1603 monitor_fdset_cleanup(mon_fdset
);
1607 return mon_fdset
->id
;
1616 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1618 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1621 void monitor_fdset_dup_fd_remove(int dup_fd
)
1623 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1626 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1629 Error
*local_err
= NULL
;
1631 if (!qemu_isdigit(fdname
[0]) && mon
) {
1632 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1634 fd
= qemu_parse_fd(fdname
);
1636 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1641 error_propagate(errp
, local_err
);
1650 /* Please update hmp-commands.hx when adding or changing commands */
1651 static HMPCommand hmp_info_cmds
[] = {
1652 #include "hmp-commands-info.h"
1656 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1657 HMPCommand hmp_cmds
[] = {
1658 #include "hmp-commands.h"
1663 * Set @pval to the value in the register identified by @name.
1664 * return 0 if OK, -1 if not found
1666 int get_monitor_def(int64_t *pval
, const char *name
)
1668 const MonitorDef
*md
= target_monitor_defs();
1669 CPUState
*cs
= mon_get_cpu();
1674 if (cs
== NULL
|| md
== NULL
) {
1678 for(; md
->name
!= NULL
; md
++) {
1679 if (hmp_compare_cmd(name
, md
->name
)) {
1680 if (md
->get_value
) {
1681 *pval
= md
->get_value(md
, md
->offset
);
1683 CPUArchState
*env
= mon_get_cpu_env();
1684 ptr
= (uint8_t *)env
+ md
->offset
;
1687 *pval
= *(int32_t *)ptr
;
1690 *pval
= *(target_long
*)ptr
;
1701 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1703 *pval
= (target_long
) tmp
;
1709 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1712 if (!str
|| !option
) {
1715 if (!strncmp(option
, str
, strlen(str
))) {
1716 readline_add_completion(rs
, option
);
1720 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1723 ChardevBackendInfoList
*list
, *start
;
1729 readline_set_completion_index(rs
, len
);
1731 start
= list
= qmp_query_chardev_backends(NULL
);
1733 const char *chr_name
= list
->value
->name
;
1735 if (!strncmp(chr_name
, str
, len
)) {
1736 readline_add_completion(rs
, chr_name
);
1740 qapi_free_ChardevBackendInfoList(start
);
1743 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1752 readline_set_completion_index(rs
, len
);
1753 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1754 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1758 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1768 readline_set_completion_index(rs
, len
);
1769 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1772 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1774 name
= object_class_get_name(OBJECT_CLASS(dc
));
1776 if (dc
->user_creatable
1777 && !strncmp(name
, str
, len
)) {
1778 readline_add_completion(rs
, name
);
1785 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1795 readline_set_completion_index(rs
, len
);
1796 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1800 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1801 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1802 readline_add_completion(rs
, name
);
1809 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1811 GSList
**list
= opaque
;
1812 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1818 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1819 *list
= g_slist_append(*list
, dev
);
1825 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1827 GSList
*list
= NULL
;
1829 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1834 static void peripheral_device_del_completion(ReadLineState
*rs
,
1835 const char *str
, size_t len
)
1837 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1838 GSList
*list
, *item
;
1840 list
= qdev_build_hotpluggable_device_list(peripheral
);
1845 for (item
= list
; item
; item
= g_slist_next(item
)) {
1846 DeviceState
*dev
= item
->data
;
1848 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1849 readline_add_completion(rs
, dev
->id
);
1856 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1859 ChardevInfoList
*list
, *start
;
1865 readline_set_completion_index(rs
, len
);
1867 start
= list
= qmp_query_chardev(NULL
);
1869 ChardevInfo
*chr
= list
->value
;
1871 if (!strncmp(chr
->label
, str
, len
)) {
1872 readline_add_completion(rs
, chr
->label
);
1876 qapi_free_ChardevInfoList(start
);
1879 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1882 ChardevInfoList
*list
, *start
;
1885 readline_set_completion_index(rs
, len
);
1887 start
= list
= qmp_query_chardev(NULL
);
1889 ChardevInfo
*chr_info
= list
->value
;
1891 if (!strncmp(chr_info
->label
, str
, len
)) {
1892 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1893 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1894 readline_add_completion(rs
, chr_info
->label
);
1899 qapi_free_ChardevInfoList(start
);
1902 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1907 ringbuf_completion(rs
, str
);
1910 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1919 readline_set_completion_index(rs
, len
);
1920 peripheral_device_del_completion(rs
, str
, len
);
1923 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1925 ObjectPropertyInfoList
*list
, *start
;
1932 readline_set_completion_index(rs
, len
);
1934 start
= list
= qmp_qom_list("/objects", NULL
);
1936 ObjectPropertyInfo
*info
= list
->value
;
1938 if (!strncmp(info
->type
, "child<", 5)
1939 && !strncmp(info
->name
, str
, len
)) {
1940 readline_add_completion(rs
, info
->name
);
1944 qapi_free_ObjectPropertyInfoList(start
);
1947 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1956 sep
= strrchr(str
, '-');
1961 readline_set_completion_index(rs
, len
);
1962 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1963 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1964 readline_add_completion(rs
, QKeyCode_str(i
));
1969 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1974 readline_set_completion_index(rs
, len
);
1976 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1978 count
= qemu_find_net_clients_except(NULL
, ncs
,
1979 NET_CLIENT_DRIVER_NONE
,
1981 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1982 const char *name
= ncs
[i
]->name
;
1983 if (!strncmp(str
, name
, len
)) {
1984 readline_add_completion(rs
, name
);
1987 } else if (nb_args
== 3) {
1988 add_completion_option(rs
, str
, "on");
1989 add_completion_option(rs
, str
, "off");
1993 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1996 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2003 readline_set_completion_index(rs
, len
);
2004 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
2006 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2007 const char *name
= ncs
[i
]->name
;
2008 if (strncmp(str
, name
, len
)) {
2011 if (ncs
[i
]->is_netdev
) {
2012 readline_add_completion(rs
, name
);
2017 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2022 readline_set_completion_index(rs
, len
);
2024 TraceEventIter iter
;
2026 char *pattern
= g_strdup_printf("%s*", str
);
2027 trace_event_iter_init(&iter
, pattern
);
2028 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2029 readline_add_completion(rs
, trace_event_get_name(ev
));
2035 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2040 readline_set_completion_index(rs
, len
);
2042 TraceEventIter iter
;
2044 char *pattern
= g_strdup_printf("%s*", str
);
2045 trace_event_iter_init(&iter
, pattern
);
2046 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2047 readline_add_completion(rs
, trace_event_get_name(ev
));
2050 } else if (nb_args
== 3) {
2051 add_completion_option(rs
, str
, "on");
2052 add_completion_option(rs
, str
, "off");
2056 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2063 readline_set_completion_index(rs
, strlen(str
));
2064 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
2065 add_completion_option(rs
, str
, WatchdogAction_str(i
));
2069 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
2075 readline_set_completion_index(rs
, len
);
2078 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2079 const char *name
= MigrationCapability_str(i
);
2080 if (!strncmp(str
, name
, len
)) {
2081 readline_add_completion(rs
, name
);
2084 } else if (nb_args
== 3) {
2085 add_completion_option(rs
, str
, "on");
2086 add_completion_option(rs
, str
, "off");
2090 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
2096 readline_set_completion_index(rs
, len
);
2099 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
2100 const char *name
= MigrationParameter_str(i
);
2101 if (!strncmp(str
, name
, len
)) {
2102 readline_add_completion(rs
, name
);
2108 static void vm_completion(ReadLineState
*rs
, const char *str
)
2111 BlockDriverState
*bs
;
2112 BdrvNextIterator it
;
2115 readline_set_completion_index(rs
, len
);
2117 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2118 SnapshotInfoList
*snapshots
, *snapshot
;
2119 AioContext
*ctx
= bdrv_get_aio_context(bs
);
2122 aio_context_acquire(ctx
);
2123 if (bdrv_can_snapshot(bs
)) {
2124 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
2126 aio_context_release(ctx
);
2131 snapshot
= snapshots
;
2133 char *completion
= snapshot
->value
->name
;
2134 if (!strncmp(str
, completion
, len
)) {
2135 readline_add_completion(rs
, completion
);
2137 completion
= snapshot
->value
->id
;
2138 if (!strncmp(str
, completion
, len
)) {
2139 readline_add_completion(rs
, completion
);
2141 snapshot
= snapshot
->next
;
2143 qapi_free_SnapshotInfoList(snapshots
);
2148 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2151 vm_completion(rs
, str
);
2155 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2158 vm_completion(rs
, str
);
2163 compare_mon_cmd(const void *a
, const void *b
)
2165 return strcmp(((const HMPCommand
*)a
)->name
,
2166 ((const HMPCommand
*)b
)->name
);
2169 static void sortcmdlist(void)
2171 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
2174 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
2175 sizeof(*hmp_info_cmds
),
2179 void monitor_init_globals(void)
2181 monitor_init_globals_core();
2182 monitor_init_qmp_commands();
2184 qemu_mutex_init(&mon_fdsets_lock
);