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 "chardev/char-mux.h"
37 #include "ui/qemu-spice.h"
38 #include "qemu/config-file.h"
39 #include "qemu/ctype.h"
40 #include "ui/console.h"
42 #include "audio/audio.h"
43 #include "disas/disas.h"
44 #include "sysemu/balloon.h"
45 #include "qemu/timer.h"
46 #include "sysemu/hw_accel.h"
47 #include "sysemu/runstate.h"
48 #include "authz/list.h"
49 #include "qapi/util.h"
50 #include "sysemu/blockdev.h"
51 #include "sysemu/sysemu.h"
52 #include "sysemu/tcg.h"
53 #include "sysemu/tpm.h"
54 #include "qapi/qmp/qdict.h"
55 #include "qapi/qmp/qerror.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qom/object_interfaces.h"
58 #include "trace/control.h"
59 #include "monitor/hmp-target.h"
60 #include "monitor/hmp.h"
61 #ifdef CONFIG_TRACE_SIMPLE
62 #include "trace/simple.h"
64 #include "exec/memory.h"
65 #include "exec/exec-all.h"
66 #include "qemu/option.h"
67 #include "qemu/thread.h"
68 #include "block/qapi.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
)
126 monitor_data_init(&hmp
.common
, false, true, false);
129 cur_mon
= &hmp
.common
;
132 int ret
= monitor_set_cpu(cpu_index
);
135 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
141 handle_hmp_command(&hmp
, command_line
);
144 qemu_mutex_lock(&hmp
.common
.mon_lock
);
145 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
146 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
148 output
= g_strdup("");
150 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
153 monitor_data_destroy(&hmp
.common
);
158 * Is @name in the '|' separated list of names @list?
160 int hmp_compare_cmd(const char *name
, const char *list
)
162 const char *p
, *pstart
;
168 p
= qemu_strchrnul(p
, '|');
169 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
180 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
182 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
185 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
187 const char *tp_name
= qdict_get_str(qdict
, "name");
188 bool new_state
= qdict_get_bool(qdict
, "option");
189 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
190 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
191 Error
*local_err
= NULL
;
194 monitor_printf(mon
, "argument vcpu must be positive");
198 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
200 error_report_err(local_err
);
204 #ifdef CONFIG_TRACE_SIMPLE
205 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
207 const char *op
= qdict_get_try_str(qdict
, "op");
208 const char *arg
= qdict_get_try_str(qdict
, "arg");
211 st_print_trace_file_status();
212 } else if (!strcmp(op
, "on")) {
213 st_set_trace_file_enabled(true);
214 } else if (!strcmp(op
, "off")) {
215 st_set_trace_file_enabled(false);
216 } else if (!strcmp(op
, "flush")) {
217 st_flush_trace_buffer();
218 } else if (!strcmp(op
, "set")) {
220 st_set_trace_file(arg
);
223 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
224 help_cmd(mon
, "trace-file");
229 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
231 help_cmd(mon
, "info");
234 static void monitor_init_qmp_commands(void)
238 * - qmp_commands contains all QMP commands
239 * - qmp_cap_negotiation_commands contains just
240 * "qmp_capabilities", to enforce capability negotiation
243 qmp_init_marshal(&qmp_commands
);
245 qmp_register_command(&qmp_commands
, "query-qmp-schema",
246 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
247 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
249 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
252 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
253 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
254 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
257 /* Set the current CPU defined by the user. Callers must hold BQL. */
258 int monitor_set_cpu(int cpu_index
)
262 cpu
= qemu_get_cpu(cpu_index
);
266 g_free(cur_mon
->mon_cpu_path
);
267 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
271 /* Callers must hold BQL. */
272 static CPUState
*mon_get_cpu_sync(bool synchronize
)
274 CPUState
*cpu
= NULL
;
276 if (cur_mon
->mon_cpu_path
) {
277 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
280 g_free(cur_mon
->mon_cpu_path
);
281 cur_mon
->mon_cpu_path
= NULL
;
284 if (!cur_mon
->mon_cpu_path
) {
288 monitor_set_cpu(first_cpu
->cpu_index
);
293 cpu_synchronize_state(cpu
);
298 CPUState
*mon_get_cpu(void)
300 return mon_get_cpu_sync(true);
303 CPUArchState
*mon_get_cpu_env(void)
305 CPUState
*cs
= mon_get_cpu();
307 return cs
? cs
->env_ptr
: NULL
;
310 int monitor_get_cpu_index(void)
312 CPUState
*cs
= mon_get_cpu_sync(false);
314 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
317 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
319 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
324 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
325 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
331 monitor_printf(mon
, "No CPU available\n");
335 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
340 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
342 if (!tcg_enabled()) {
343 error_report("JIT information is only available with accel=tcg");
351 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
357 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
359 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
360 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
361 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
362 enum QSPSortBy sort_by
;
364 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
365 qsp_report(max
, sort_by
, coalesce
);
368 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
370 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
379 str
= readline_get_history(hmp_mon
->rs
, i
);
383 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
388 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
390 CPUState
*cs
= mon_get_cpu();
393 monitor_printf(mon
, "No CPU available\n");
396 cpu_dump_statistics(cs
, 0);
399 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
401 const char *name
= qdict_get_try_str(qdict
, "name");
402 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
403 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
404 TraceEventInfoList
*events
;
405 TraceEventInfoList
*elem
;
406 Error
*local_err
= NULL
;
412 monitor_printf(mon
, "argument vcpu must be positive");
416 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
418 error_report_err(local_err
);
422 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
423 monitor_printf(mon
, "%s : state %u\n",
425 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
427 qapi_free_TraceEventInfoList(events
);
430 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
431 bool has_port
, int64_t port
,
432 bool has_tls_port
, int64_t tls_port
,
433 bool has_cert_subject
, const char *cert_subject
,
436 if (strcmp(protocol
, "spice") == 0) {
437 if (!qemu_using_spice(errp
)) {
441 if (!has_port
&& !has_tls_port
) {
442 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
446 if (qemu_spice_migrate_info(hostname
,
447 has_port
? port
: -1,
448 has_tls_port
? tls_port
: -1,
450 error_setg(errp
, QERR_UNDEFINED_ERROR
);
456 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
459 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
463 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
465 error_report_err(err
);
469 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
472 const char *items
= qdict_get_str(qdict
, "items");
474 if (!strcmp(items
, "none")) {
477 mask
= qemu_str_to_log_mask(items
);
479 help_cmd(mon
, "log");
486 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
488 const char *option
= qdict_get_try_str(qdict
, "option");
489 if (!option
|| !strcmp(option
, "on")) {
491 } else if (!strcmp(option
, "off")) {
494 monitor_printf(mon
, "unexpected option %s\n", option
);
498 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
500 const char *device
= qdict_get_try_str(qdict
, "device");
502 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
503 if (gdbserver_start(device
) < 0) {
504 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
506 } else if (strcmp(device
, "none") == 0) {
507 monitor_printf(mon
, "Disabled gdbserver\n");
509 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
514 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
516 const char *action
= qdict_get_str(qdict
, "action");
517 if (select_watchdog_action(action
) == -1) {
518 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
522 static void monitor_printc(Monitor
*mon
, int c
)
524 monitor_printf(mon
, "'");
527 monitor_printf(mon
, "\\'");
530 monitor_printf(mon
, "\\\\");
533 monitor_printf(mon
, "\\n");
536 monitor_printf(mon
, "\\r");
539 if (c
>= 32 && c
<= 126) {
540 monitor_printf(mon
, "%c", c
);
542 monitor_printf(mon
, "\\x%02x", c
);
546 monitor_printf(mon
, "'");
549 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
550 hwaddr addr
, int is_physical
)
552 int l
, line_size
, i
, max_digits
, len
;
555 CPUState
*cs
= mon_get_cpu();
557 if (!cs
&& (format
== 'i' || !is_physical
)) {
558 monitor_printf(mon
, "Can not dump without CPU\n");
563 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
576 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
580 max_digits
= (wsize
* 8) / 4;
584 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
593 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
595 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
600 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
601 MemTxResult r
= address_space_read(as
, addr
,
602 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
604 monitor_printf(mon
, " Cannot access memory\n");
608 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
609 monitor_printf(mon
, " Cannot access memory\n");
624 v
= (uint32_t)ldl_p(buf
+ i
);
630 monitor_printf(mon
, " ");
633 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
636 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
639 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
642 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
645 monitor_printc(mon
, v
);
650 monitor_printf(mon
, "\n");
656 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
658 int count
= qdict_get_int(qdict
, "count");
659 int format
= qdict_get_int(qdict
, "format");
660 int size
= qdict_get_int(qdict
, "size");
661 target_long addr
= qdict_get_int(qdict
, "addr");
663 memory_dump(mon
, count
, format
, size
, addr
, 0);
666 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
668 int count
= qdict_get_int(qdict
, "count");
669 int format
= qdict_get_int(qdict
, "format");
670 int size
= qdict_get_int(qdict
, "size");
671 hwaddr addr
= qdict_get_int(qdict
, "addr");
673 memory_dump(mon
, count
, format
, size
, addr
, 1);
676 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
678 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
682 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
686 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
687 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
688 memory_region_unref(mrs
.mr
);
693 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
696 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
698 hwaddr addr
= qdict_get_int(qdict
, "addr");
699 Error
*local_err
= NULL
;
700 MemoryRegion
*mr
= NULL
;
703 ptr
= gpa2hva(&mr
, addr
, &local_err
);
705 error_report_err(local_err
);
709 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
711 addr
, mr
->name
, ptr
);
713 memory_region_unref(mr
);
716 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
718 target_ulong addr
= qdict_get_int(qdict
, "addr");
720 CPUState
*cs
= mon_get_cpu();
724 monitor_printf(mon
, "No cpu\n");
728 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
730 monitor_printf(mon
, "Unmapped\n");
732 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
733 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
738 static uint64_t vtop(void *ptr
, Error
**errp
)
742 uintptr_t addr
= (uintptr_t) ptr
;
743 uintptr_t pagesize
= qemu_real_host_page_size
;
744 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
747 fd
= open("/proc/self/pagemap", O_RDONLY
);
749 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
753 /* Force copy-on-write if necessary. */
754 atomic_add((uint8_t *)ptr
, 0);
756 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
757 error_setg_errno(errp
, errno
, "Cannot read pagemap");
760 if ((pinfo
& (1ull << 63)) == 0) {
761 error_setg(errp
, "Page not present");
764 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
771 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
773 hwaddr addr
= qdict_get_int(qdict
, "addr");
774 Error
*local_err
= NULL
;
775 MemoryRegion
*mr
= NULL
;
779 ptr
= gpa2hva(&mr
, addr
, &local_err
);
781 error_report_err(local_err
);
785 physaddr
= vtop(ptr
, &local_err
);
787 error_report_err(local_err
);
789 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
790 " (%s) is 0x%" PRIx64
"\n",
791 addr
, mr
->name
, (uint64_t) physaddr
);
794 memory_region_unref(mr
);
798 static void do_print(Monitor
*mon
, const QDict
*qdict
)
800 int format
= qdict_get_int(qdict
, "format");
801 hwaddr val
= qdict_get_int(qdict
, "val");
805 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
808 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
811 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
815 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
818 monitor_printc(mon
, val
);
821 monitor_printf(mon
, "\n");
824 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
828 uint32_t start
= qdict_get_int(qdict
, "start");
829 uint32_t size
= qdict_get_int(qdict
, "size");
832 for(addr
= start
; addr
< (start
+ size
); addr
++) {
833 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
834 MEMTXATTRS_UNSPECIFIED
, NULL
);
835 /* BSD sum algorithm ('sum' Unix command) */
836 sum
= (sum
>> 1) | (sum
<< 15);
839 monitor_printf(mon
, "%05d\n", sum
);
842 static int mouse_button_state
;
844 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
846 int dx
, dy
, dz
, button
;
847 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
848 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
849 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
851 dx
= strtol(dx_str
, NULL
, 0);
852 dy
= strtol(dy_str
, NULL
, 0);
853 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
854 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
857 dz
= strtol(dz_str
, NULL
, 0);
859 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
860 qemu_input_queue_btn(NULL
, button
, true);
861 qemu_input_event_sync();
862 qemu_input_queue_btn(NULL
, button
, false);
865 qemu_input_event_sync();
868 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
870 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
871 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
872 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
873 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
875 int button_state
= qdict_get_int(qdict
, "button_state");
877 if (mouse_button_state
== button_state
) {
880 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
881 qemu_input_event_sync();
882 mouse_button_state
= button_state
;
885 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
887 int size
= qdict_get_int(qdict
, "size");
888 int addr
= qdict_get_int(qdict
, "addr");
889 int has_index
= qdict_haskey(qdict
, "index");
894 int index
= qdict_get_int(qdict
, "index");
895 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
915 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
916 suffix
, addr
, size
* 2, val
);
919 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
921 int size
= qdict_get_int(qdict
, "size");
922 int addr
= qdict_get_int(qdict
, "addr");
923 int val
= qdict_get_int(qdict
, "val");
925 addr
&= IOPORTS_MASK
;
941 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
943 Error
*local_err
= NULL
;
944 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
946 qemu_boot_set(bootdevice
, &local_err
);
948 error_report_err(local_err
);
950 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
954 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
956 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
957 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
958 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
960 mtree_info(flatview
, dispatch_tree
, owner
);
963 #ifdef CONFIG_PROFILER
967 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
969 static int64_t last_cpu_exec_time
;
970 int64_t cpu_exec_time
;
973 cpu_exec_time
= tcg_cpu_exec_time();
974 delta
= cpu_exec_time
- last_cpu_exec_time
;
976 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
977 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
978 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
979 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
980 last_cpu_exec_time
= cpu_exec_time
;
984 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
986 monitor_printf(mon
, "Internal profiler not compiled\n");
990 /* Capture support */
991 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
993 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
998 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
999 monitor_printf(mon
, "[%d]: ", i
);
1000 s
->ops
.info (s
->opaque
);
1004 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1007 int n
= qdict_get_int(qdict
, "n");
1010 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1012 s
->ops
.destroy (s
->opaque
);
1013 QLIST_REMOVE (s
, entries
);
1020 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1022 const char *path
= qdict_get_str(qdict
, "path");
1023 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1024 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1025 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1026 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1028 AudioState
*as
= audio_state_by_name(audiodev
);
1031 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1035 s
= g_malloc0 (sizeof (*s
));
1037 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1038 monitor_printf(mon
, "Failed to add wave capture\n");
1042 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1045 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1050 container
= object_get_objects_root();
1051 obj
= object_resolve_path_component(container
, name
);
1053 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1057 return QAUTHZ_LIST(obj
);
1060 static bool warn_acl
;
1061 static void hmp_warn_acl(void)
1066 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1067 "commands are deprecated with no replacement. Authorization "
1068 "for VNC should be performed using the pluggable QAuthZ "
1073 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1075 const char *aclname
= qdict_get_str(qdict
, "aclname");
1076 QAuthZList
*auth
= find_auth(mon
, aclname
);
1077 QAuthZListRuleList
*rules
;
1086 monitor_printf(mon
, "policy: %s\n",
1087 QAuthZListPolicy_str(auth
->policy
));
1089 rules
= auth
->rules
;
1091 QAuthZListRule
*rule
= rules
->value
;
1093 monitor_printf(mon
, "%zu: %s %s\n", i
,
1094 QAuthZListPolicy_str(rule
->policy
),
1096 rules
= rules
->next
;
1100 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1102 const char *aclname
= qdict_get_str(qdict
, "aclname");
1103 QAuthZList
*auth
= find_auth(mon
, aclname
);
1111 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1112 qapi_free_QAuthZListRuleList(auth
->rules
);
1114 monitor_printf(mon
, "acl: removed all rules\n");
1117 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1119 const char *aclname
= qdict_get_str(qdict
, "aclname");
1120 const char *policy
= qdict_get_str(qdict
, "policy");
1121 QAuthZList
*auth
= find_auth(mon
, aclname
);
1131 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1133 QAUTHZ_LIST_POLICY_DENY
,
1137 monitor_printf(mon
, "acl: unknown policy '%s', "
1138 "expected 'deny' or 'allow'\n", policy
);
1141 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1142 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1144 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1149 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1151 if (strchr(match
, '*')) {
1152 return QAUTHZ_LIST_FORMAT_GLOB
;
1154 return QAUTHZ_LIST_FORMAT_EXACT
;
1158 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1160 const char *aclname
= qdict_get_str(qdict
, "aclname");
1161 const char *match
= qdict_get_str(qdict
, "match");
1162 const char *policystr
= qdict_get_str(qdict
, "policy");
1163 int has_index
= qdict_haskey(qdict
, "index");
1164 int index
= qdict_get_try_int(qdict
, "index", -1);
1165 QAuthZList
*auth
= find_auth(mon
, aclname
);
1167 QAuthZListPolicy policy
;
1168 QAuthZListFormat format
;
1177 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1179 QAUTHZ_LIST_POLICY_DENY
,
1183 monitor_printf(mon
, "acl: unknown policy '%s', "
1184 "expected 'deny' or 'allow'\n", policystr
);
1188 format
= hmp_acl_get_format(match
);
1190 if (has_index
&& index
== 0) {
1191 monitor_printf(mon
, "acl: unable to add acl entry\n");
1196 i
= qauthz_list_insert_rule(auth
, match
, policy
,
1197 format
, index
- 1, &err
);
1199 i
= qauthz_list_append_rule(auth
, match
, policy
,
1203 monitor_printf(mon
, "acl: unable to add rule: %s",
1204 error_get_pretty(err
));
1207 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
1211 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1213 const char *aclname
= qdict_get_str(qdict
, "aclname");
1214 const char *match
= qdict_get_str(qdict
, "match");
1215 QAuthZList
*auth
= find_auth(mon
, aclname
);
1224 i
= qauthz_list_delete_rule(auth
, match
);
1226 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
1228 monitor_printf(mon
, "acl: no matching acl entry\n");
1232 void qmp_getfd(const char *fdname
, Error
**errp
)
1237 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1239 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1243 if (qemu_isdigit(fdname
[0])) {
1245 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1246 "a name not starting with a digit");
1250 qemu_mutex_lock(&cur_mon
->mon_lock
);
1251 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1252 if (strcmp(monfd
->name
, fdname
) != 0) {
1258 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1259 /* Make sure close() is outside critical section */
1264 monfd
= g_malloc0(sizeof(mon_fd_t
));
1265 monfd
->name
= g_strdup(fdname
);
1268 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1269 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1272 void qmp_closefd(const char *fdname
, Error
**errp
)
1277 qemu_mutex_lock(&cur_mon
->mon_lock
);
1278 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1279 if (strcmp(monfd
->name
, fdname
) != 0) {
1283 QLIST_REMOVE(monfd
, next
);
1285 g_free(monfd
->name
);
1287 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1288 /* Make sure close() is outside critical section */
1293 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1294 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1297 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1301 qemu_mutex_lock(&mon
->mon_lock
);
1302 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1305 if (strcmp(monfd
->name
, fdname
) != 0) {
1311 /* caller takes ownership of fd */
1312 QLIST_REMOVE(monfd
, next
);
1313 g_free(monfd
->name
);
1315 qemu_mutex_unlock(&mon
->mon_lock
);
1320 qemu_mutex_unlock(&mon
->mon_lock
);
1321 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1325 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1327 MonFdsetFd
*mon_fdset_fd
;
1328 MonFdsetFd
*mon_fdset_fd_next
;
1330 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1331 if ((mon_fdset_fd
->removed
||
1332 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1333 runstate_is_running()) {
1334 close(mon_fdset_fd
->fd
);
1335 g_free(mon_fdset_fd
->opaque
);
1336 QLIST_REMOVE(mon_fdset_fd
, next
);
1337 g_free(mon_fdset_fd
);
1341 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1342 QLIST_REMOVE(mon_fdset
, next
);
1347 void monitor_fdsets_cleanup(void)
1349 MonFdset
*mon_fdset
;
1350 MonFdset
*mon_fdset_next
;
1352 qemu_mutex_lock(&mon_fdsets_lock
);
1353 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1354 monitor_fdset_cleanup(mon_fdset
);
1356 qemu_mutex_unlock(&mon_fdsets_lock
);
1359 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1360 const char *opaque
, Error
**errp
)
1363 Monitor
*mon
= cur_mon
;
1366 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1368 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1372 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1373 has_opaque
, opaque
, errp
);
1385 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1387 MonFdset
*mon_fdset
;
1388 MonFdsetFd
*mon_fdset_fd
;
1391 qemu_mutex_lock(&mon_fdsets_lock
);
1392 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1393 if (mon_fdset
->id
!= fdset_id
) {
1396 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1398 if (mon_fdset_fd
->fd
!= fd
) {
1401 mon_fdset_fd
->removed
= true;
1404 mon_fdset_fd
->removed
= true;
1407 if (has_fd
&& !mon_fdset_fd
) {
1410 monitor_fdset_cleanup(mon_fdset
);
1411 qemu_mutex_unlock(&mon_fdsets_lock
);
1416 qemu_mutex_unlock(&mon_fdsets_lock
);
1418 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1421 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1423 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1426 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1428 MonFdset
*mon_fdset
;
1429 MonFdsetFd
*mon_fdset_fd
;
1430 FdsetInfoList
*fdset_list
= NULL
;
1432 qemu_mutex_lock(&mon_fdsets_lock
);
1433 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1434 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1435 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1437 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1438 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1440 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1441 FdsetFdInfoList
*fdsetfd_info
;
1443 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1444 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1445 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1446 if (mon_fdset_fd
->opaque
) {
1447 fdsetfd_info
->value
->has_opaque
= true;
1448 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1450 fdsetfd_info
->value
->has_opaque
= false;
1453 fdsetfd_info
->next
= fdsetfd_list
;
1454 fdsetfd_list
= fdsetfd_info
;
1457 fdset_info
->value
->fds
= fdsetfd_list
;
1459 fdset_info
->next
= fdset_list
;
1460 fdset_list
= fdset_info
;
1462 qemu_mutex_unlock(&mon_fdsets_lock
);
1467 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1468 bool has_opaque
, const char *opaque
,
1471 MonFdset
*mon_fdset
= NULL
;
1472 MonFdsetFd
*mon_fdset_fd
;
1475 qemu_mutex_lock(&mon_fdsets_lock
);
1477 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1478 /* Break if match found or match impossible due to ordering by ID */
1479 if (fdset_id
<= mon_fdset
->id
) {
1480 if (fdset_id
< mon_fdset
->id
) {
1488 if (mon_fdset
== NULL
) {
1489 int64_t fdset_id_prev
= -1;
1490 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1494 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1495 "a non-negative value");
1496 qemu_mutex_unlock(&mon_fdsets_lock
);
1499 /* Use specified fdset ID */
1500 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1501 mon_fdset_cur
= mon_fdset
;
1502 if (fdset_id
< mon_fdset_cur
->id
) {
1507 /* Use first available fdset ID */
1508 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1509 mon_fdset_cur
= mon_fdset
;
1510 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1511 fdset_id_prev
= mon_fdset_cur
->id
;
1518 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1520 mon_fdset
->id
= fdset_id
;
1522 mon_fdset
->id
= fdset_id_prev
+ 1;
1525 /* The fdset list is ordered by fdset ID */
1526 if (!mon_fdset_cur
) {
1527 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1528 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1529 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1531 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1535 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1536 mon_fdset_fd
->fd
= fd
;
1537 mon_fdset_fd
->removed
= false;
1539 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1541 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1543 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1544 fdinfo
->fdset_id
= mon_fdset
->id
;
1545 fdinfo
->fd
= mon_fdset_fd
->fd
;
1547 qemu_mutex_unlock(&mon_fdsets_lock
);
1551 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1556 MonFdset
*mon_fdset
;
1557 MonFdsetFd
*mon_fdset_fd
;
1561 qemu_mutex_lock(&mon_fdsets_lock
);
1562 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1563 if (mon_fdset
->id
!= fdset_id
) {
1566 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1567 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1568 if (mon_fd_flags
== -1) {
1573 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1574 ret
= mon_fdset_fd
->fd
;
1584 qemu_mutex_unlock(&mon_fdsets_lock
);
1589 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
1591 MonFdset
*mon_fdset
;
1592 MonFdsetFd
*mon_fdset_fd_dup
;
1594 qemu_mutex_lock(&mon_fdsets_lock
);
1595 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1596 if (mon_fdset
->id
!= fdset_id
) {
1599 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1600 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1604 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1605 mon_fdset_fd_dup
->fd
= dup_fd
;
1606 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1607 qemu_mutex_unlock(&mon_fdsets_lock
);
1612 qemu_mutex_unlock(&mon_fdsets_lock
);
1616 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1618 MonFdset
*mon_fdset
;
1619 MonFdsetFd
*mon_fdset_fd_dup
;
1621 qemu_mutex_lock(&mon_fdsets_lock
);
1622 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1623 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1624 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1626 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1627 g_free(mon_fdset_fd_dup
);
1628 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1629 monitor_fdset_cleanup(mon_fdset
);
1633 qemu_mutex_unlock(&mon_fdsets_lock
);
1634 return mon_fdset
->id
;
1641 qemu_mutex_unlock(&mon_fdsets_lock
);
1645 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1647 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1650 void monitor_fdset_dup_fd_remove(int dup_fd
)
1652 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1655 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1658 Error
*local_err
= NULL
;
1660 if (!qemu_isdigit(fdname
[0]) && mon
) {
1661 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1663 fd
= qemu_parse_fd(fdname
);
1665 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1670 error_propagate(errp
, local_err
);
1679 /* Please update hmp-commands.hx when adding or changing commands */
1680 static HMPCommand hmp_info_cmds
[] = {
1681 #include "hmp-commands-info.h"
1685 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1686 HMPCommand hmp_cmds
[] = {
1687 #include "hmp-commands.h"
1692 * Set @pval to the value in the register identified by @name.
1693 * return 0 if OK, -1 if not found
1695 int get_monitor_def(int64_t *pval
, const char *name
)
1697 const MonitorDef
*md
= target_monitor_defs();
1698 CPUState
*cs
= mon_get_cpu();
1703 if (cs
== NULL
|| md
== NULL
) {
1707 for(; md
->name
!= NULL
; md
++) {
1708 if (hmp_compare_cmd(name
, md
->name
)) {
1709 if (md
->get_value
) {
1710 *pval
= md
->get_value(md
, md
->offset
);
1712 CPUArchState
*env
= mon_get_cpu_env();
1713 ptr
= (uint8_t *)env
+ md
->offset
;
1716 *pval
= *(int32_t *)ptr
;
1719 *pval
= *(target_long
*)ptr
;
1730 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1732 *pval
= (target_long
) tmp
;
1738 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1741 if (!str
|| !option
) {
1744 if (!strncmp(option
, str
, strlen(str
))) {
1745 readline_add_completion(rs
, option
);
1749 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1752 ChardevBackendInfoList
*list
, *start
;
1758 readline_set_completion_index(rs
, len
);
1760 start
= list
= qmp_query_chardev_backends(NULL
);
1762 const char *chr_name
= list
->value
->name
;
1764 if (!strncmp(chr_name
, str
, len
)) {
1765 readline_add_completion(rs
, chr_name
);
1769 qapi_free_ChardevBackendInfoList(start
);
1772 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1781 readline_set_completion_index(rs
, len
);
1782 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1783 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1787 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1797 readline_set_completion_index(rs
, len
);
1798 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1801 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1803 name
= object_class_get_name(OBJECT_CLASS(dc
));
1805 if (dc
->user_creatable
1806 && !strncmp(name
, str
, len
)) {
1807 readline_add_completion(rs
, name
);
1814 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1824 readline_set_completion_index(rs
, len
);
1825 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1829 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1830 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1831 readline_add_completion(rs
, name
);
1838 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1840 GSList
**list
= opaque
;
1841 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(OBJECT(obj
),
1848 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1849 *list
= g_slist_append(*list
, dev
);
1855 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1857 GSList
*list
= NULL
;
1859 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1864 static void peripheral_device_del_completion(ReadLineState
*rs
,
1865 const char *str
, size_t len
)
1867 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1868 GSList
*list
, *item
;
1870 list
= qdev_build_hotpluggable_device_list(peripheral
);
1875 for (item
= list
; item
; item
= g_slist_next(item
)) {
1876 DeviceState
*dev
= item
->data
;
1878 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1879 readline_add_completion(rs
, dev
->id
);
1886 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1889 ChardevInfoList
*list
, *start
;
1895 readline_set_completion_index(rs
, len
);
1897 start
= list
= qmp_query_chardev(NULL
);
1899 ChardevInfo
*chr
= list
->value
;
1901 if (!strncmp(chr
->label
, str
, len
)) {
1902 readline_add_completion(rs
, chr
->label
);
1906 qapi_free_ChardevInfoList(start
);
1909 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1912 ChardevInfoList
*list
, *start
;
1915 readline_set_completion_index(rs
, len
);
1917 start
= list
= qmp_query_chardev(NULL
);
1919 ChardevInfo
*chr_info
= list
->value
;
1921 if (!strncmp(chr_info
->label
, str
, len
)) {
1922 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1923 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1924 readline_add_completion(rs
, chr_info
->label
);
1929 qapi_free_ChardevInfoList(start
);
1932 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1937 ringbuf_completion(rs
, str
);
1940 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1949 readline_set_completion_index(rs
, len
);
1950 peripheral_device_del_completion(rs
, str
, len
);
1953 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1955 ObjectPropertyInfoList
*list
, *start
;
1962 readline_set_completion_index(rs
, len
);
1964 start
= list
= qmp_qom_list("/objects", NULL
);
1966 ObjectPropertyInfo
*info
= list
->value
;
1968 if (!strncmp(info
->type
, "child<", 5)
1969 && !strncmp(info
->name
, str
, len
)) {
1970 readline_add_completion(rs
, info
->name
);
1974 qapi_free_ObjectPropertyInfoList(start
);
1977 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1986 sep
= strrchr(str
, '-');
1991 readline_set_completion_index(rs
, len
);
1992 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1993 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1994 readline_add_completion(rs
, QKeyCode_str(i
));
1999 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2004 readline_set_completion_index(rs
, len
);
2006 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2008 count
= qemu_find_net_clients_except(NULL
, ncs
,
2009 NET_CLIENT_DRIVER_NONE
,
2011 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2012 const char *name
= ncs
[i
]->name
;
2013 if (!strncmp(str
, name
, len
)) {
2014 readline_add_completion(rs
, name
);
2017 } else if (nb_args
== 3) {
2018 add_completion_option(rs
, str
, "on");
2019 add_completion_option(rs
, str
, "off");
2023 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2026 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2033 readline_set_completion_index(rs
, len
);
2034 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
2036 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2038 const char *name
= ncs
[i
]->name
;
2039 if (strncmp(str
, name
, len
)) {
2042 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
2044 readline_add_completion(rs
, name
);
2049 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2054 readline_set_completion_index(rs
, len
);
2056 TraceEventIter iter
;
2058 char *pattern
= g_strdup_printf("%s*", str
);
2059 trace_event_iter_init(&iter
, pattern
);
2060 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2061 readline_add_completion(rs
, trace_event_get_name(ev
));
2067 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2072 readline_set_completion_index(rs
, len
);
2074 TraceEventIter iter
;
2076 char *pattern
= g_strdup_printf("%s*", str
);
2077 trace_event_iter_init(&iter
, pattern
);
2078 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2079 readline_add_completion(rs
, trace_event_get_name(ev
));
2082 } else if (nb_args
== 3) {
2083 add_completion_option(rs
, str
, "on");
2084 add_completion_option(rs
, str
, "off");
2088 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2095 readline_set_completion_index(rs
, strlen(str
));
2096 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
2097 add_completion_option(rs
, str
, WatchdogAction_str(i
));
2101 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
2107 readline_set_completion_index(rs
, len
);
2110 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2111 const char *name
= MigrationCapability_str(i
);
2112 if (!strncmp(str
, name
, len
)) {
2113 readline_add_completion(rs
, name
);
2116 } else if (nb_args
== 3) {
2117 add_completion_option(rs
, str
, "on");
2118 add_completion_option(rs
, str
, "off");
2122 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
2128 readline_set_completion_index(rs
, len
);
2131 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
2132 const char *name
= MigrationParameter_str(i
);
2133 if (!strncmp(str
, name
, len
)) {
2134 readline_add_completion(rs
, name
);
2140 static void vm_completion(ReadLineState
*rs
, const char *str
)
2143 BlockDriverState
*bs
;
2144 BdrvNextIterator it
;
2147 readline_set_completion_index(rs
, len
);
2149 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2150 SnapshotInfoList
*snapshots
, *snapshot
;
2151 AioContext
*ctx
= bdrv_get_aio_context(bs
);
2154 aio_context_acquire(ctx
);
2155 if (bdrv_can_snapshot(bs
)) {
2156 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
2158 aio_context_release(ctx
);
2163 snapshot
= snapshots
;
2165 char *completion
= snapshot
->value
->name
;
2166 if (!strncmp(str
, completion
, len
)) {
2167 readline_add_completion(rs
, completion
);
2169 completion
= snapshot
->value
->id
;
2170 if (!strncmp(str
, completion
, len
)) {
2171 readline_add_completion(rs
, completion
);
2173 snapshot
= snapshot
->next
;
2175 qapi_free_SnapshotInfoList(snapshots
);
2180 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2183 vm_completion(rs
, str
);
2187 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2190 vm_completion(rs
, str
);
2195 compare_mon_cmd(const void *a
, const void *b
)
2197 return strcmp(((const HMPCommand
*)a
)->name
,
2198 ((const HMPCommand
*)b
)->name
);
2201 static void sortcmdlist(void)
2203 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
2206 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
2207 sizeof(*hmp_info_cmds
),
2211 void monitor_init_globals(void)
2213 monitor_init_globals_core();
2214 monitor_init_qmp_commands();
2216 qemu_mutex_init(&mon_fdsets_lock
);