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 "block/block-hmp-cmds.h"
70 #include "qapi/qapi-commands-char.h"
71 #include "qapi/qapi-commands-control.h"
72 #include "qapi/qapi-commands-migration.h"
73 #include "qapi/qapi-commands-misc.h"
74 #include "qapi/qapi-commands-qom.h"
75 #include "qapi/qapi-commands-trace.h"
76 #include "qapi/qapi-init-commands.h"
77 #include "qapi/error.h"
78 #include "qapi/qmp-event.h"
79 #include "sysemu/cpus.h"
80 #include "qemu/cutils.h"
83 #if defined(TARGET_S390X)
84 #include "hw/s390x/storage-keys.h"
85 #include "hw/s390x/storage-attributes.h"
88 /* file descriptors passed via SCM_RIGHTS */
89 typedef struct mon_fd_t mon_fd_t
;
93 QLIST_ENTRY(mon_fd_t
) next
;
96 /* file descriptor associated with a file descriptor set */
97 typedef struct MonFdsetFd MonFdsetFd
;
102 QLIST_ENTRY(MonFdsetFd
) next
;
105 /* file descriptor set containing fds passed via SCM_RIGHTS */
106 typedef struct MonFdset MonFdset
;
109 QLIST_HEAD(, MonFdsetFd
) fds
;
110 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
111 QLIST_ENTRY(MonFdset
) next
;
114 /* Protects mon_fdsets */
115 static QemuMutex mon_fdsets_lock
;
116 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
118 static HMPCommand hmp_info_cmds
[];
120 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
121 int64_t cpu_index
, Error
**errp
)
127 monitor_data_init(&hmp
.common
, false, true, false);
130 cur_mon
= &hmp
.common
;
133 int ret
= monitor_set_cpu(cpu_index
);
136 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
142 handle_hmp_command(&hmp
, command_line
);
145 qemu_mutex_lock(&hmp
.common
.mon_lock
);
146 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
147 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
149 output
= g_strdup("");
151 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
154 monitor_data_destroy(&hmp
.common
);
159 * Is @name in the '|' separated list of names @list?
161 int hmp_compare_cmd(const char *name
, const char *list
)
163 const char *p
, *pstart
;
169 p
= qemu_strchrnul(p
, '|');
170 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
181 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
183 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
186 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
188 const char *tp_name
= qdict_get_str(qdict
, "name");
189 bool new_state
= qdict_get_bool(qdict
, "option");
190 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
191 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
192 Error
*local_err
= NULL
;
195 monitor_printf(mon
, "argument vcpu must be positive");
199 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
201 error_report_err(local_err
);
205 #ifdef CONFIG_TRACE_SIMPLE
206 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
208 const char *op
= qdict_get_try_str(qdict
, "op");
209 const char *arg
= qdict_get_try_str(qdict
, "arg");
212 st_print_trace_file_status();
213 } else if (!strcmp(op
, "on")) {
214 st_set_trace_file_enabled(true);
215 } else if (!strcmp(op
, "off")) {
216 st_set_trace_file_enabled(false);
217 } else if (!strcmp(op
, "flush")) {
218 st_flush_trace_buffer();
219 } else if (!strcmp(op
, "set")) {
221 st_set_trace_file(arg
);
224 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
225 help_cmd(mon
, "trace-file");
230 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
232 help_cmd(mon
, "info");
235 static void monitor_init_qmp_commands(void)
239 * - qmp_commands contains all QMP commands
240 * - qmp_cap_negotiation_commands contains just
241 * "qmp_capabilities", to enforce capability negotiation
244 qmp_init_marshal(&qmp_commands
);
246 qmp_register_command(&qmp_commands
, "query-qmp-schema",
247 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
248 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
250 qmp_register_command(&qmp_commands
, "object-add", qmp_object_add
,
253 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
254 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
255 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
258 /* Set the current CPU defined by the user. Callers must hold BQL. */
259 int monitor_set_cpu(int cpu_index
)
263 cpu
= qemu_get_cpu(cpu_index
);
267 g_free(cur_mon
->mon_cpu_path
);
268 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
272 /* Callers must hold BQL. */
273 static CPUState
*mon_get_cpu_sync(bool synchronize
)
275 CPUState
*cpu
= NULL
;
277 if (cur_mon
->mon_cpu_path
) {
278 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
281 g_free(cur_mon
->mon_cpu_path
);
282 cur_mon
->mon_cpu_path
= NULL
;
285 if (!cur_mon
->mon_cpu_path
) {
289 monitor_set_cpu(first_cpu
->cpu_index
);
294 cpu_synchronize_state(cpu
);
299 CPUState
*mon_get_cpu(void)
301 return mon_get_cpu_sync(true);
304 CPUArchState
*mon_get_cpu_env(void)
306 CPUState
*cs
= mon_get_cpu();
308 return cs
? cs
->env_ptr
: NULL
;
311 int monitor_get_cpu_index(void)
313 CPUState
*cs
= mon_get_cpu_sync(false);
315 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
318 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
320 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
325 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
326 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
332 monitor_printf(mon
, "No CPU available\n");
336 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
341 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
343 if (!tcg_enabled()) {
344 error_report("JIT information is only available with accel=tcg");
352 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
358 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
360 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
361 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
362 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
363 enum QSPSortBy sort_by
;
365 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
366 qsp_report(max
, sort_by
, coalesce
);
369 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
371 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
380 str
= readline_get_history(hmp_mon
->rs
, i
);
384 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
389 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
391 CPUState
*cs
= mon_get_cpu();
394 monitor_printf(mon
, "No CPU available\n");
397 cpu_dump_statistics(cs
, 0);
400 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
402 const char *name
= qdict_get_try_str(qdict
, "name");
403 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
404 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
405 TraceEventInfoList
*events
;
406 TraceEventInfoList
*elem
;
407 Error
*local_err
= NULL
;
413 monitor_printf(mon
, "argument vcpu must be positive");
417 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
419 error_report_err(local_err
);
423 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
424 monitor_printf(mon
, "%s : state %u\n",
426 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
428 qapi_free_TraceEventInfoList(events
);
431 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
432 bool has_port
, int64_t port
,
433 bool has_tls_port
, int64_t tls_port
,
434 bool has_cert_subject
, const char *cert_subject
,
437 if (strcmp(protocol
, "spice") == 0) {
438 if (!qemu_using_spice(errp
)) {
442 if (!has_port
&& !has_tls_port
) {
443 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
447 if (qemu_spice_migrate_info(hostname
,
448 has_port
? port
: -1,
449 has_tls_port
? tls_port
: -1,
451 error_setg(errp
, QERR_UNDEFINED_ERROR
);
457 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
460 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
464 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
466 error_report_err(err
);
470 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
473 const char *items
= qdict_get_str(qdict
, "items");
475 if (!strcmp(items
, "none")) {
478 mask
= qemu_str_to_log_mask(items
);
480 help_cmd(mon
, "log");
487 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
489 const char *option
= qdict_get_try_str(qdict
, "option");
490 if (!option
|| !strcmp(option
, "on")) {
492 } else if (!strcmp(option
, "off")) {
495 monitor_printf(mon
, "unexpected option %s\n", option
);
499 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
501 const char *device
= qdict_get_try_str(qdict
, "device");
503 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
504 if (gdbserver_start(device
) < 0) {
505 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
507 } else if (strcmp(device
, "none") == 0) {
508 monitor_printf(mon
, "Disabled gdbserver\n");
510 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
515 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
517 const char *action
= qdict_get_str(qdict
, "action");
518 if (select_watchdog_action(action
) == -1) {
519 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
523 static void monitor_printc(Monitor
*mon
, int c
)
525 monitor_printf(mon
, "'");
528 monitor_printf(mon
, "\\'");
531 monitor_printf(mon
, "\\\\");
534 monitor_printf(mon
, "\\n");
537 monitor_printf(mon
, "\\r");
540 if (c
>= 32 && c
<= 126) {
541 monitor_printf(mon
, "%c", c
);
543 monitor_printf(mon
, "\\x%02x", c
);
547 monitor_printf(mon
, "'");
550 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
551 hwaddr addr
, int is_physical
)
553 int l
, line_size
, i
, max_digits
, len
;
556 CPUState
*cs
= mon_get_cpu();
558 if (!cs
&& (format
== 'i' || !is_physical
)) {
559 monitor_printf(mon
, "Can not dump without CPU\n");
564 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
577 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
581 max_digits
= (wsize
* 8) / 4;
585 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
594 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
596 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
601 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
602 MemTxResult r
= address_space_read(as
, addr
,
603 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
605 monitor_printf(mon
, " Cannot access memory\n");
609 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
610 monitor_printf(mon
, " Cannot access memory\n");
625 v
= (uint32_t)ldl_p(buf
+ i
);
631 monitor_printf(mon
, " ");
634 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
637 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
640 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
643 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
646 monitor_printc(mon
, v
);
651 monitor_printf(mon
, "\n");
657 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
659 int count
= qdict_get_int(qdict
, "count");
660 int format
= qdict_get_int(qdict
, "format");
661 int size
= qdict_get_int(qdict
, "size");
662 target_long addr
= qdict_get_int(qdict
, "addr");
664 memory_dump(mon
, count
, format
, size
, addr
, 0);
667 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
669 int count
= qdict_get_int(qdict
, "count");
670 int format
= qdict_get_int(qdict
, "format");
671 int size
= qdict_get_int(qdict
, "size");
672 hwaddr addr
= qdict_get_int(qdict
, "addr");
674 memory_dump(mon
, count
, format
, size
, addr
, 1);
677 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
679 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
683 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
687 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
688 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
689 memory_region_unref(mrs
.mr
);
694 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
697 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
699 hwaddr addr
= qdict_get_int(qdict
, "addr");
700 Error
*local_err
= NULL
;
701 MemoryRegion
*mr
= NULL
;
704 ptr
= gpa2hva(&mr
, addr
, &local_err
);
706 error_report_err(local_err
);
710 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
712 addr
, mr
->name
, ptr
);
714 memory_region_unref(mr
);
717 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
719 target_ulong addr
= qdict_get_int(qdict
, "addr");
721 CPUState
*cs
= mon_get_cpu();
725 monitor_printf(mon
, "No cpu\n");
729 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
731 monitor_printf(mon
, "Unmapped\n");
733 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
734 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
739 static uint64_t vtop(void *ptr
, Error
**errp
)
743 uintptr_t addr
= (uintptr_t) ptr
;
744 uintptr_t pagesize
= qemu_real_host_page_size
;
745 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
748 fd
= open("/proc/self/pagemap", O_RDONLY
);
750 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
754 /* Force copy-on-write if necessary. */
755 atomic_add((uint8_t *)ptr
, 0);
757 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
758 error_setg_errno(errp
, errno
, "Cannot read pagemap");
761 if ((pinfo
& (1ull << 63)) == 0) {
762 error_setg(errp
, "Page not present");
765 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
772 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
774 hwaddr addr
= qdict_get_int(qdict
, "addr");
775 Error
*local_err
= NULL
;
776 MemoryRegion
*mr
= NULL
;
780 ptr
= gpa2hva(&mr
, addr
, &local_err
);
782 error_report_err(local_err
);
786 physaddr
= vtop(ptr
, &local_err
);
788 error_report_err(local_err
);
790 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
791 " (%s) is 0x%" PRIx64
"\n",
792 addr
, mr
->name
, (uint64_t) physaddr
);
795 memory_region_unref(mr
);
799 static void do_print(Monitor
*mon
, const QDict
*qdict
)
801 int format
= qdict_get_int(qdict
, "format");
802 hwaddr val
= qdict_get_int(qdict
, "val");
806 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
809 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
812 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
816 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
819 monitor_printc(mon
, val
);
822 monitor_printf(mon
, "\n");
825 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
829 uint32_t start
= qdict_get_int(qdict
, "start");
830 uint32_t size
= qdict_get_int(qdict
, "size");
833 for(addr
= start
; addr
< (start
+ size
); addr
++) {
834 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
835 MEMTXATTRS_UNSPECIFIED
, NULL
);
836 /* BSD sum algorithm ('sum' Unix command) */
837 sum
= (sum
>> 1) | (sum
<< 15);
840 monitor_printf(mon
, "%05d\n", sum
);
843 static int mouse_button_state
;
845 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
847 int dx
, dy
, dz
, button
;
848 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
849 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
850 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
852 dx
= strtol(dx_str
, NULL
, 0);
853 dy
= strtol(dy_str
, NULL
, 0);
854 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
855 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
858 dz
= strtol(dz_str
, NULL
, 0);
860 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
861 qemu_input_queue_btn(NULL
, button
, true);
862 qemu_input_event_sync();
863 qemu_input_queue_btn(NULL
, button
, false);
866 qemu_input_event_sync();
869 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
871 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
872 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
873 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
874 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
876 int button_state
= qdict_get_int(qdict
, "button_state");
878 if (mouse_button_state
== button_state
) {
881 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
882 qemu_input_event_sync();
883 mouse_button_state
= button_state
;
886 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
888 int size
= qdict_get_int(qdict
, "size");
889 int addr
= qdict_get_int(qdict
, "addr");
890 int has_index
= qdict_haskey(qdict
, "index");
895 int index
= qdict_get_int(qdict
, "index");
896 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
916 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
917 suffix
, addr
, size
* 2, val
);
920 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
922 int size
= qdict_get_int(qdict
, "size");
923 int addr
= qdict_get_int(qdict
, "addr");
924 int val
= qdict_get_int(qdict
, "val");
926 addr
&= IOPORTS_MASK
;
942 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
944 Error
*local_err
= NULL
;
945 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
947 qemu_boot_set(bootdevice
, &local_err
);
949 error_report_err(local_err
);
951 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
955 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
957 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
958 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
959 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
961 mtree_info(flatview
, dispatch_tree
, owner
);
964 #ifdef CONFIG_PROFILER
968 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
970 static int64_t last_cpu_exec_time
;
971 int64_t cpu_exec_time
;
974 cpu_exec_time
= tcg_cpu_exec_time();
975 delta
= cpu_exec_time
- last_cpu_exec_time
;
977 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
978 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
979 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
980 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
981 last_cpu_exec_time
= cpu_exec_time
;
985 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
987 monitor_printf(mon
, "Internal profiler not compiled\n");
991 /* Capture support */
992 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
994 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
999 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1000 monitor_printf(mon
, "[%d]: ", i
);
1001 s
->ops
.info (s
->opaque
);
1005 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1008 int n
= qdict_get_int(qdict
, "n");
1011 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1013 s
->ops
.destroy (s
->opaque
);
1014 QLIST_REMOVE (s
, entries
);
1021 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1023 const char *path
= qdict_get_str(qdict
, "path");
1024 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1025 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1026 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1027 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1029 AudioState
*as
= audio_state_by_name(audiodev
);
1032 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1036 s
= g_malloc0 (sizeof (*s
));
1038 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1039 monitor_printf(mon
, "Failed to add wave capture\n");
1043 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1046 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1051 container
= object_get_objects_root();
1052 obj
= object_resolve_path_component(container
, name
);
1054 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1058 return QAUTHZ_LIST(obj
);
1061 static bool warn_acl
;
1062 static void hmp_warn_acl(void)
1067 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1068 "commands are deprecated with no replacement. Authorization "
1069 "for VNC should be performed using the pluggable QAuthZ "
1074 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1076 const char *aclname
= qdict_get_str(qdict
, "aclname");
1077 QAuthZList
*auth
= find_auth(mon
, aclname
);
1078 QAuthZListRuleList
*rules
;
1087 monitor_printf(mon
, "policy: %s\n",
1088 QAuthZListPolicy_str(auth
->policy
));
1090 rules
= auth
->rules
;
1092 QAuthZListRule
*rule
= rules
->value
;
1094 monitor_printf(mon
, "%zu: %s %s\n", i
,
1095 QAuthZListPolicy_str(rule
->policy
),
1097 rules
= rules
->next
;
1101 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1103 const char *aclname
= qdict_get_str(qdict
, "aclname");
1104 QAuthZList
*auth
= find_auth(mon
, aclname
);
1112 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1113 qapi_free_QAuthZListRuleList(auth
->rules
);
1115 monitor_printf(mon
, "acl: removed all rules\n");
1118 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1120 const char *aclname
= qdict_get_str(qdict
, "aclname");
1121 const char *policy
= qdict_get_str(qdict
, "policy");
1122 QAuthZList
*auth
= find_auth(mon
, aclname
);
1132 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1134 QAUTHZ_LIST_POLICY_DENY
,
1138 monitor_printf(mon
, "acl: unknown policy '%s', "
1139 "expected 'deny' or 'allow'\n", policy
);
1142 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1143 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1145 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1150 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1152 if (strchr(match
, '*')) {
1153 return QAUTHZ_LIST_FORMAT_GLOB
;
1155 return QAUTHZ_LIST_FORMAT_EXACT
;
1159 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1161 const char *aclname
= qdict_get_str(qdict
, "aclname");
1162 const char *match
= qdict_get_str(qdict
, "match");
1163 const char *policystr
= qdict_get_str(qdict
, "policy");
1164 int has_index
= qdict_haskey(qdict
, "index");
1165 int index
= qdict_get_try_int(qdict
, "index", -1);
1166 QAuthZList
*auth
= find_auth(mon
, aclname
);
1168 QAuthZListPolicy policy
;
1169 QAuthZListFormat format
;
1178 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1180 QAUTHZ_LIST_POLICY_DENY
,
1184 monitor_printf(mon
, "acl: unknown policy '%s', "
1185 "expected 'deny' or 'allow'\n", policystr
);
1189 format
= hmp_acl_get_format(match
);
1191 if (has_index
&& index
== 0) {
1192 monitor_printf(mon
, "acl: unable to add acl entry\n");
1197 i
= qauthz_list_insert_rule(auth
, match
, policy
,
1198 format
, index
- 1, &err
);
1200 i
= qauthz_list_append_rule(auth
, match
, policy
,
1204 monitor_printf(mon
, "acl: unable to add rule: %s",
1205 error_get_pretty(err
));
1208 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
1212 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1214 const char *aclname
= qdict_get_str(qdict
, "aclname");
1215 const char *match
= qdict_get_str(qdict
, "match");
1216 QAuthZList
*auth
= find_auth(mon
, aclname
);
1225 i
= qauthz_list_delete_rule(auth
, match
);
1227 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
1229 monitor_printf(mon
, "acl: no matching acl entry\n");
1233 void qmp_getfd(const char *fdname
, Error
**errp
)
1238 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1240 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1244 if (qemu_isdigit(fdname
[0])) {
1246 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1247 "a name not starting with a digit");
1251 qemu_mutex_lock(&cur_mon
->mon_lock
);
1252 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1253 if (strcmp(monfd
->name
, fdname
) != 0) {
1259 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1260 /* Make sure close() is outside critical section */
1265 monfd
= g_malloc0(sizeof(mon_fd_t
));
1266 monfd
->name
= g_strdup(fdname
);
1269 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1270 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1273 void qmp_closefd(const char *fdname
, Error
**errp
)
1278 qemu_mutex_lock(&cur_mon
->mon_lock
);
1279 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1280 if (strcmp(monfd
->name
, fdname
) != 0) {
1284 QLIST_REMOVE(monfd
, next
);
1286 g_free(monfd
->name
);
1288 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1289 /* Make sure close() is outside critical section */
1294 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1295 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1298 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1302 qemu_mutex_lock(&mon
->mon_lock
);
1303 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1306 if (strcmp(monfd
->name
, fdname
) != 0) {
1312 /* caller takes ownership of fd */
1313 QLIST_REMOVE(monfd
, next
);
1314 g_free(monfd
->name
);
1316 qemu_mutex_unlock(&mon
->mon_lock
);
1321 qemu_mutex_unlock(&mon
->mon_lock
);
1322 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1326 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1328 MonFdsetFd
*mon_fdset_fd
;
1329 MonFdsetFd
*mon_fdset_fd_next
;
1331 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1332 if ((mon_fdset_fd
->removed
||
1333 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1334 runstate_is_running()) {
1335 close(mon_fdset_fd
->fd
);
1336 g_free(mon_fdset_fd
->opaque
);
1337 QLIST_REMOVE(mon_fdset_fd
, next
);
1338 g_free(mon_fdset_fd
);
1342 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1343 QLIST_REMOVE(mon_fdset
, next
);
1348 void monitor_fdsets_cleanup(void)
1350 MonFdset
*mon_fdset
;
1351 MonFdset
*mon_fdset_next
;
1353 qemu_mutex_lock(&mon_fdsets_lock
);
1354 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1355 monitor_fdset_cleanup(mon_fdset
);
1357 qemu_mutex_unlock(&mon_fdsets_lock
);
1360 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1361 const char *opaque
, Error
**errp
)
1364 Monitor
*mon
= cur_mon
;
1367 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1369 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1373 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1374 has_opaque
, opaque
, errp
);
1386 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1388 MonFdset
*mon_fdset
;
1389 MonFdsetFd
*mon_fdset_fd
;
1392 qemu_mutex_lock(&mon_fdsets_lock
);
1393 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1394 if (mon_fdset
->id
!= fdset_id
) {
1397 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1399 if (mon_fdset_fd
->fd
!= fd
) {
1402 mon_fdset_fd
->removed
= true;
1405 mon_fdset_fd
->removed
= true;
1408 if (has_fd
&& !mon_fdset_fd
) {
1411 monitor_fdset_cleanup(mon_fdset
);
1412 qemu_mutex_unlock(&mon_fdsets_lock
);
1417 qemu_mutex_unlock(&mon_fdsets_lock
);
1419 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1422 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1424 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1427 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1429 MonFdset
*mon_fdset
;
1430 MonFdsetFd
*mon_fdset_fd
;
1431 FdsetInfoList
*fdset_list
= NULL
;
1433 qemu_mutex_lock(&mon_fdsets_lock
);
1434 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1435 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1436 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1438 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1439 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1441 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1442 FdsetFdInfoList
*fdsetfd_info
;
1444 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1445 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1446 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1447 if (mon_fdset_fd
->opaque
) {
1448 fdsetfd_info
->value
->has_opaque
= true;
1449 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1451 fdsetfd_info
->value
->has_opaque
= false;
1454 fdsetfd_info
->next
= fdsetfd_list
;
1455 fdsetfd_list
= fdsetfd_info
;
1458 fdset_info
->value
->fds
= fdsetfd_list
;
1460 fdset_info
->next
= fdset_list
;
1461 fdset_list
= fdset_info
;
1463 qemu_mutex_unlock(&mon_fdsets_lock
);
1468 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1469 bool has_opaque
, const char *opaque
,
1472 MonFdset
*mon_fdset
= NULL
;
1473 MonFdsetFd
*mon_fdset_fd
;
1476 qemu_mutex_lock(&mon_fdsets_lock
);
1478 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1479 /* Break if match found or match impossible due to ordering by ID */
1480 if (fdset_id
<= mon_fdset
->id
) {
1481 if (fdset_id
< mon_fdset
->id
) {
1489 if (mon_fdset
== NULL
) {
1490 int64_t fdset_id_prev
= -1;
1491 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1495 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1496 "a non-negative value");
1497 qemu_mutex_unlock(&mon_fdsets_lock
);
1500 /* Use specified fdset ID */
1501 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1502 mon_fdset_cur
= mon_fdset
;
1503 if (fdset_id
< mon_fdset_cur
->id
) {
1508 /* Use first available fdset ID */
1509 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1510 mon_fdset_cur
= mon_fdset
;
1511 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1512 fdset_id_prev
= mon_fdset_cur
->id
;
1519 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1521 mon_fdset
->id
= fdset_id
;
1523 mon_fdset
->id
= fdset_id_prev
+ 1;
1526 /* The fdset list is ordered by fdset ID */
1527 if (!mon_fdset_cur
) {
1528 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1529 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1530 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1532 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1536 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1537 mon_fdset_fd
->fd
= fd
;
1538 mon_fdset_fd
->removed
= false;
1540 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1542 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1544 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1545 fdinfo
->fdset_id
= mon_fdset
->id
;
1546 fdinfo
->fd
= mon_fdset_fd
->fd
;
1548 qemu_mutex_unlock(&mon_fdsets_lock
);
1552 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1557 MonFdset
*mon_fdset
;
1558 MonFdsetFd
*mon_fdset_fd
;
1562 qemu_mutex_lock(&mon_fdsets_lock
);
1563 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1564 if (mon_fdset
->id
!= fdset_id
) {
1567 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1568 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1569 if (mon_fd_flags
== -1) {
1574 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1575 ret
= mon_fdset_fd
->fd
;
1585 qemu_mutex_unlock(&mon_fdsets_lock
);
1590 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
1592 MonFdset
*mon_fdset
;
1593 MonFdsetFd
*mon_fdset_fd_dup
;
1595 qemu_mutex_lock(&mon_fdsets_lock
);
1596 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1597 if (mon_fdset
->id
!= fdset_id
) {
1600 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1601 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1605 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1606 mon_fdset_fd_dup
->fd
= dup_fd
;
1607 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1608 qemu_mutex_unlock(&mon_fdsets_lock
);
1613 qemu_mutex_unlock(&mon_fdsets_lock
);
1617 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1619 MonFdset
*mon_fdset
;
1620 MonFdsetFd
*mon_fdset_fd_dup
;
1622 qemu_mutex_lock(&mon_fdsets_lock
);
1623 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1624 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1625 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1627 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1628 g_free(mon_fdset_fd_dup
);
1629 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1630 monitor_fdset_cleanup(mon_fdset
);
1634 qemu_mutex_unlock(&mon_fdsets_lock
);
1635 return mon_fdset
->id
;
1642 qemu_mutex_unlock(&mon_fdsets_lock
);
1646 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1648 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1651 void monitor_fdset_dup_fd_remove(int dup_fd
)
1653 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1656 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1659 Error
*local_err
= NULL
;
1661 if (!qemu_isdigit(fdname
[0]) && mon
) {
1662 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1664 fd
= qemu_parse_fd(fdname
);
1666 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1671 error_propagate(errp
, local_err
);
1680 /* Please update hmp-commands.hx when adding or changing commands */
1681 static HMPCommand hmp_info_cmds
[] = {
1682 #include "hmp-commands-info.h"
1686 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1687 HMPCommand hmp_cmds
[] = {
1688 #include "hmp-commands.h"
1693 * Set @pval to the value in the register identified by @name.
1694 * return 0 if OK, -1 if not found
1696 int get_monitor_def(int64_t *pval
, const char *name
)
1698 const MonitorDef
*md
= target_monitor_defs();
1699 CPUState
*cs
= mon_get_cpu();
1704 if (cs
== NULL
|| md
== NULL
) {
1708 for(; md
->name
!= NULL
; md
++) {
1709 if (hmp_compare_cmd(name
, md
->name
)) {
1710 if (md
->get_value
) {
1711 *pval
= md
->get_value(md
, md
->offset
);
1713 CPUArchState
*env
= mon_get_cpu_env();
1714 ptr
= (uint8_t *)env
+ md
->offset
;
1717 *pval
= *(int32_t *)ptr
;
1720 *pval
= *(target_long
*)ptr
;
1731 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1733 *pval
= (target_long
) tmp
;
1739 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1742 if (!str
|| !option
) {
1745 if (!strncmp(option
, str
, strlen(str
))) {
1746 readline_add_completion(rs
, option
);
1750 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1753 ChardevBackendInfoList
*list
, *start
;
1759 readline_set_completion_index(rs
, len
);
1761 start
= list
= qmp_query_chardev_backends(NULL
);
1763 const char *chr_name
= list
->value
->name
;
1765 if (!strncmp(chr_name
, str
, len
)) {
1766 readline_add_completion(rs
, chr_name
);
1770 qapi_free_ChardevBackendInfoList(start
);
1773 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1782 readline_set_completion_index(rs
, len
);
1783 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1784 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1788 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1798 readline_set_completion_index(rs
, len
);
1799 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1802 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1804 name
= object_class_get_name(OBJECT_CLASS(dc
));
1806 if (dc
->user_creatable
1807 && !strncmp(name
, str
, len
)) {
1808 readline_add_completion(rs
, name
);
1815 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1825 readline_set_completion_index(rs
, len
);
1826 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1830 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1831 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1832 readline_add_completion(rs
, name
);
1839 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1841 GSList
**list
= opaque
;
1842 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(OBJECT(obj
),
1849 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1850 *list
= g_slist_append(*list
, dev
);
1856 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1858 GSList
*list
= NULL
;
1860 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1865 static void peripheral_device_del_completion(ReadLineState
*rs
,
1866 const char *str
, size_t len
)
1868 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1869 GSList
*list
, *item
;
1871 list
= qdev_build_hotpluggable_device_list(peripheral
);
1876 for (item
= list
; item
; item
= g_slist_next(item
)) {
1877 DeviceState
*dev
= item
->data
;
1879 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1880 readline_add_completion(rs
, dev
->id
);
1887 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1890 ChardevInfoList
*list
, *start
;
1896 readline_set_completion_index(rs
, len
);
1898 start
= list
= qmp_query_chardev(NULL
);
1900 ChardevInfo
*chr
= list
->value
;
1902 if (!strncmp(chr
->label
, str
, len
)) {
1903 readline_add_completion(rs
, chr
->label
);
1907 qapi_free_ChardevInfoList(start
);
1910 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1913 ChardevInfoList
*list
, *start
;
1916 readline_set_completion_index(rs
, len
);
1918 start
= list
= qmp_query_chardev(NULL
);
1920 ChardevInfo
*chr_info
= list
->value
;
1922 if (!strncmp(chr_info
->label
, str
, len
)) {
1923 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1924 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1925 readline_add_completion(rs
, chr_info
->label
);
1930 qapi_free_ChardevInfoList(start
);
1933 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1938 ringbuf_completion(rs
, str
);
1941 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1950 readline_set_completion_index(rs
, len
);
1951 peripheral_device_del_completion(rs
, str
, len
);
1954 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1956 ObjectPropertyInfoList
*list
, *start
;
1963 readline_set_completion_index(rs
, len
);
1965 start
= list
= qmp_qom_list("/objects", NULL
);
1967 ObjectPropertyInfo
*info
= list
->value
;
1969 if (!strncmp(info
->type
, "child<", 5)
1970 && !strncmp(info
->name
, str
, len
)) {
1971 readline_add_completion(rs
, info
->name
);
1975 qapi_free_ObjectPropertyInfoList(start
);
1978 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1987 sep
= strrchr(str
, '-');
1992 readline_set_completion_index(rs
, len
);
1993 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1994 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1995 readline_add_completion(rs
, QKeyCode_str(i
));
2000 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2005 readline_set_completion_index(rs
, len
);
2007 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2009 count
= qemu_find_net_clients_except(NULL
, ncs
,
2010 NET_CLIENT_DRIVER_NONE
,
2012 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2013 const char *name
= ncs
[i
]->name
;
2014 if (!strncmp(str
, name
, len
)) {
2015 readline_add_completion(rs
, name
);
2018 } else if (nb_args
== 3) {
2019 add_completion_option(rs
, str
, "on");
2020 add_completion_option(rs
, str
, "off");
2024 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2027 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2034 readline_set_completion_index(rs
, len
);
2035 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
2037 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2038 const char *name
= ncs
[i
]->name
;
2039 if (strncmp(str
, name
, len
)) {
2042 if (ncs
[i
]->is_netdev
) {
2043 readline_add_completion(rs
, name
);
2048 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2053 readline_set_completion_index(rs
, len
);
2055 TraceEventIter iter
;
2057 char *pattern
= g_strdup_printf("%s*", str
);
2058 trace_event_iter_init(&iter
, pattern
);
2059 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2060 readline_add_completion(rs
, trace_event_get_name(ev
));
2066 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2071 readline_set_completion_index(rs
, len
);
2073 TraceEventIter iter
;
2075 char *pattern
= g_strdup_printf("%s*", str
);
2076 trace_event_iter_init(&iter
, pattern
);
2077 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2078 readline_add_completion(rs
, trace_event_get_name(ev
));
2081 } else if (nb_args
== 3) {
2082 add_completion_option(rs
, str
, "on");
2083 add_completion_option(rs
, str
, "off");
2087 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2094 readline_set_completion_index(rs
, strlen(str
));
2095 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
2096 add_completion_option(rs
, str
, WatchdogAction_str(i
));
2100 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
2106 readline_set_completion_index(rs
, len
);
2109 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2110 const char *name
= MigrationCapability_str(i
);
2111 if (!strncmp(str
, name
, len
)) {
2112 readline_add_completion(rs
, name
);
2115 } else if (nb_args
== 3) {
2116 add_completion_option(rs
, str
, "on");
2117 add_completion_option(rs
, str
, "off");
2121 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
2127 readline_set_completion_index(rs
, len
);
2130 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
2131 const char *name
= MigrationParameter_str(i
);
2132 if (!strncmp(str
, name
, len
)) {
2133 readline_add_completion(rs
, name
);
2139 static void vm_completion(ReadLineState
*rs
, const char *str
)
2142 BlockDriverState
*bs
;
2143 BdrvNextIterator it
;
2146 readline_set_completion_index(rs
, len
);
2148 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2149 SnapshotInfoList
*snapshots
, *snapshot
;
2150 AioContext
*ctx
= bdrv_get_aio_context(bs
);
2153 aio_context_acquire(ctx
);
2154 if (bdrv_can_snapshot(bs
)) {
2155 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
2157 aio_context_release(ctx
);
2162 snapshot
= snapshots
;
2164 char *completion
= snapshot
->value
->name
;
2165 if (!strncmp(str
, completion
, len
)) {
2166 readline_add_completion(rs
, completion
);
2168 completion
= snapshot
->value
->id
;
2169 if (!strncmp(str
, completion
, len
)) {
2170 readline_add_completion(rs
, completion
);
2172 snapshot
= snapshot
->next
;
2174 qapi_free_SnapshotInfoList(snapshots
);
2179 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2182 vm_completion(rs
, str
);
2186 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2189 vm_completion(rs
, str
);
2194 compare_mon_cmd(const void *a
, const void *b
)
2196 return strcmp(((const HMPCommand
*)a
)->name
,
2197 ((const HMPCommand
*)b
)->name
);
2200 static void sortcmdlist(void)
2202 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
2205 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
2206 sizeof(*hmp_info_cmds
),
2210 void monitor_init_globals(void)
2212 monitor_init_globals_core();
2213 monitor_init_qmp_commands();
2215 qemu_mutex_init(&mon_fdsets_lock
);