4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
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/tpm.h"
52 #include "qapi/qmp/qdict.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qstring.h"
55 #include "qom/object_interfaces.h"
56 #include "trace/control.h"
57 #include "monitor/hmp-target.h"
58 #include "monitor/hmp.h"
59 #ifdef CONFIG_TRACE_SIMPLE
60 #include "trace/simple.h"
62 #include "exec/memory.h"
63 #include "exec/exec-all.h"
64 #include "qemu/option.h"
65 #include "qemu/thread.h"
66 #include "block/qapi.h"
67 #include "block/block-hmp-cmds.h"
68 #include "qapi/qapi-commands-char.h"
69 #include "qapi/qapi-commands-control.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-run-state.h"
74 #include "qapi/qapi-commands-trace.h"
75 #include "qapi/qapi-commands-machine.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"
82 #if defined(TARGET_S390X)
83 #include "hw/s390x/storage-keys.h"
84 #include "hw/s390x/storage-attributes.h"
87 /* Make devices configuration available for use in hmp-commands*.hx templates */
88 #include CONFIG_DEVICES
90 /* file descriptors passed via SCM_RIGHTS */
91 typedef struct mon_fd_t mon_fd_t
;
95 QLIST_ENTRY(mon_fd_t
) next
;
98 /* file descriptor associated with a file descriptor set */
99 typedef struct MonFdsetFd MonFdsetFd
;
104 QLIST_ENTRY(MonFdsetFd
) next
;
107 /* file descriptor set containing fds passed via SCM_RIGHTS */
108 typedef struct MonFdset MonFdset
;
111 QLIST_HEAD(, MonFdsetFd
) fds
;
112 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
113 QLIST_ENTRY(MonFdset
) next
;
116 /* Protects mon_fdsets */
117 static QemuMutex mon_fdsets_lock
;
118 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
120 static HMPCommand hmp_info_cmds
[];
122 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
123 int64_t cpu_index
, Error
**errp
)
128 monitor_data_init(&hmp
.common
, false, true, false);
131 int ret
= monitor_set_cpu(&hmp
.common
, cpu_index
);
133 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
139 handle_hmp_command(&hmp
, command_line
);
141 WITH_QEMU_LOCK_GUARD(&hmp
.common
.mon_lock
) {
142 output
= g_strdup(hmp
.common
.outbuf
->str
);
146 monitor_data_destroy(&hmp
.common
);
151 * Is @name in the '|' separated list of names @list?
153 int hmp_compare_cmd(const char *name
, const char *list
)
155 const char *p
, *pstart
;
161 p
= qemu_strchrnul(p
, '|');
162 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
173 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
175 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
178 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
180 const char *tp_name
= qdict_get_str(qdict
, "name");
181 bool new_state
= qdict_get_bool(qdict
, "option");
182 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
183 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
184 Error
*local_err
= NULL
;
187 monitor_printf(mon
, "argument vcpu must be positive");
191 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
193 error_report_err(local_err
);
197 #ifdef CONFIG_TRACE_SIMPLE
198 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
200 const char *op
= qdict_get_try_str(qdict
, "op");
201 const char *arg
= qdict_get_try_str(qdict
, "arg");
204 st_print_trace_file_status();
205 } else if (!strcmp(op
, "on")) {
206 st_set_trace_file_enabled(true);
207 } else if (!strcmp(op
, "off")) {
208 st_set_trace_file_enabled(false);
209 } else if (!strcmp(op
, "flush")) {
210 st_flush_trace_buffer();
211 } else if (!strcmp(op
, "set")) {
213 st_set_trace_file(arg
);
216 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
217 help_cmd(mon
, "trace-file");
222 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
224 help_cmd(mon
, "info");
227 static void monitor_init_qmp_commands(void)
231 * - qmp_commands contains all QMP commands
232 * - qmp_cap_negotiation_commands contains just
233 * "qmp_capabilities", to enforce capability negotiation
236 qmp_init_marshal(&qmp_commands
);
238 qmp_register_command(&qmp_commands
, "device_add",
239 qmp_device_add
, 0, 0);
241 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
242 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
243 qmp_marshal_qmp_capabilities
,
244 QCO_ALLOW_PRECONFIG
, 0);
247 /* Set the current CPU defined by the user. Callers must hold BQL. */
248 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
252 cpu
= qemu_get_cpu(cpu_index
);
256 g_free(mon
->mon_cpu_path
);
257 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
261 /* Callers must hold BQL. */
262 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
264 CPUState
*cpu
= NULL
;
266 if (mon
->mon_cpu_path
) {
267 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
270 g_free(mon
->mon_cpu_path
);
271 mon
->mon_cpu_path
= NULL
;
274 if (!mon
->mon_cpu_path
) {
278 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
283 cpu_synchronize_state(cpu
);
288 CPUState
*mon_get_cpu(Monitor
*mon
)
290 return mon_get_cpu_sync(mon
, true);
293 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
295 CPUState
*cs
= mon_get_cpu(mon
);
297 return cs
? cs
->env_ptr
: NULL
;
300 int monitor_get_cpu_index(Monitor
*mon
)
302 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
304 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
307 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
309 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
310 int vcpu
= qdict_get_try_int(qdict
, "vcpu", -1);
315 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
316 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
319 cs
= vcpu
>= 0 ? qemu_get_cpu(vcpu
) : mon_get_cpu(mon
);
323 monitor_printf(mon
, "CPU#%d not available\n", vcpu
);
325 monitor_printf(mon
, "No CPU available\n");
330 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
331 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
335 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
337 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
338 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
339 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
340 enum QSPSortBy sort_by
;
342 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
343 qsp_report(max
, sort_by
, coalesce
);
346 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
348 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
357 str
= readline_get_history(hmp_mon
->rs
, i
);
361 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
366 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
368 const char *name
= qdict_get_try_str(qdict
, "name");
369 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
370 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
371 TraceEventInfoList
*events
;
372 TraceEventInfoList
*elem
;
373 Error
*local_err
= NULL
;
379 monitor_printf(mon
, "argument vcpu must be positive");
383 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
385 error_report_err(local_err
);
389 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
390 monitor_printf(mon
, "%s : state %u\n",
392 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
394 qapi_free_TraceEventInfoList(events
);
397 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
398 bool has_port
, int64_t port
,
399 bool has_tls_port
, int64_t tls_port
,
400 bool has_cert_subject
, const char *cert_subject
,
403 if (strcmp(protocol
, "spice") == 0) {
404 if (!qemu_using_spice(errp
)) {
408 if (!has_port
&& !has_tls_port
) {
409 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
413 if (qemu_spice
.migrate_info(hostname
,
414 has_port
? port
: -1,
415 has_tls_port
? tls_port
: -1,
417 error_setg(errp
, "Could not set up display for migration");
423 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "'spice'");
426 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
430 if (!qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
)) {
431 error_report_err(err
);
435 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
438 const char *items
= qdict_get_str(qdict
, "items");
441 if (!strcmp(items
, "none")) {
444 mask
= qemu_str_to_log_mask(items
);
446 help_cmd(mon
, "log");
451 if (!qemu_set_log(mask
, &err
)) {
452 error_report_err(err
);
456 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
458 const char *option
= qdict_get_try_str(qdict
, "option");
459 if (!option
|| !strcmp(option
, "on")) {
461 } else if (!strcmp(option
, "off")) {
464 monitor_printf(mon
, "unexpected option %s\n", option
);
468 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
470 const char *device
= qdict_get_try_str(qdict
, "device");
472 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
475 if (gdbserver_start(device
) < 0) {
476 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
478 } else if (strcmp(device
, "none") == 0) {
479 monitor_printf(mon
, "Disabled gdbserver\n");
481 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
486 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
489 WatchdogAction action
;
492 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
493 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
496 hmp_handle_error(mon
, err
);
499 qmp_watchdog_set_action(action
, &error_abort
);
502 static void monitor_printc(Monitor
*mon
, int c
)
504 monitor_printf(mon
, "'");
507 monitor_printf(mon
, "\\'");
510 monitor_printf(mon
, "\\\\");
513 monitor_printf(mon
, "\\n");
516 monitor_printf(mon
, "\\r");
519 if (c
>= 32 && c
<= 126) {
520 monitor_printf(mon
, "%c", c
);
522 monitor_printf(mon
, "\\x%02x", c
);
526 monitor_printf(mon
, "'");
529 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
530 hwaddr addr
, int is_physical
)
532 int l
, line_size
, i
, max_digits
, len
;
535 CPUState
*cs
= mon_get_cpu(mon
);
537 if (!cs
&& (format
== 'i' || !is_physical
)) {
538 monitor_printf(mon
, "Can not dump without CPU\n");
543 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
557 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
561 max_digits
= (wsize
* 8) / 4;
565 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
574 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
576 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
582 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
583 MemTxResult r
= address_space_read(as
, addr
,
584 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
586 monitor_printf(mon
, " Cannot access memory\n");
590 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
591 monitor_printf(mon
, " Cannot access memory\n");
606 v
= (uint32_t)ldl_p(buf
+ i
);
612 monitor_printf(mon
, " ");
615 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
618 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
621 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
624 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
627 monitor_printc(mon
, v
);
632 monitor_printf(mon
, "\n");
638 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
640 int count
= qdict_get_int(qdict
, "count");
641 int format
= qdict_get_int(qdict
, "format");
642 int size
= qdict_get_int(qdict
, "size");
643 target_long addr
= qdict_get_int(qdict
, "addr");
645 memory_dump(mon
, count
, format
, size
, addr
, 0);
648 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
650 int count
= qdict_get_int(qdict
, "count");
651 int format
= qdict_get_int(qdict
, "format");
652 int size
= qdict_get_int(qdict
, "size");
653 hwaddr addr
= qdict_get_int(qdict
, "addr");
655 memory_dump(mon
, count
, format
, size
, addr
, 1);
658 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
660 Int128 gpa_region_size
;
661 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
665 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
669 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
670 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
671 memory_region_unref(mrs
.mr
);
675 gpa_region_size
= int128_make64(size
);
676 if (int128_lt(mrs
.size
, gpa_region_size
)) {
677 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
679 memory_region_unref(mrs
.mr
);
684 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
687 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
689 hwaddr addr
= qdict_get_int(qdict
, "addr");
690 Error
*local_err
= NULL
;
691 MemoryRegion
*mr
= NULL
;
694 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
696 error_report_err(local_err
);
700 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
702 addr
, mr
->name
, ptr
);
704 memory_region_unref(mr
);
707 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
709 target_ulong addr
= qdict_get_int(qdict
, "addr");
711 CPUState
*cs
= mon_get_cpu(mon
);
715 monitor_printf(mon
, "No cpu\n");
719 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
721 monitor_printf(mon
, "Unmapped\n");
723 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
724 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
729 static uint64_t vtop(void *ptr
, Error
**errp
)
733 uintptr_t addr
= (uintptr_t) ptr
;
734 uintptr_t pagesize
= qemu_real_host_page_size();
735 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
738 fd
= open("/proc/self/pagemap", O_RDONLY
);
740 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
744 /* Force copy-on-write if necessary. */
745 qatomic_add((uint8_t *)ptr
, 0);
747 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
748 error_setg_errno(errp
, errno
, "Cannot read pagemap");
751 if ((pinfo
& (1ull << 63)) == 0) {
752 error_setg(errp
, "Page not present");
755 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
762 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
764 hwaddr addr
= qdict_get_int(qdict
, "addr");
765 Error
*local_err
= NULL
;
766 MemoryRegion
*mr
= NULL
;
770 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
772 error_report_err(local_err
);
776 physaddr
= vtop(ptr
, &local_err
);
778 error_report_err(local_err
);
780 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
781 " (%s) is 0x%" PRIx64
"\n",
782 addr
, mr
->name
, (uint64_t) physaddr
);
785 memory_region_unref(mr
);
789 static void do_print(Monitor
*mon
, const QDict
*qdict
)
791 int format
= qdict_get_int(qdict
, "format");
792 hwaddr val
= qdict_get_int(qdict
, "val");
796 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
799 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
802 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
806 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
809 monitor_printc(mon
, val
);
812 monitor_printf(mon
, "\n");
815 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
819 uint32_t start
= qdict_get_int(qdict
, "start");
820 uint32_t size
= qdict_get_int(qdict
, "size");
823 for(addr
= start
; addr
< (start
+ size
); addr
++) {
824 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
825 MEMTXATTRS_UNSPECIFIED
, NULL
);
826 /* BSD sum algorithm ('sum' Unix command) */
827 sum
= (sum
>> 1) | (sum
<< 15);
830 monitor_printf(mon
, "%05d\n", sum
);
833 static int mouse_button_state
;
835 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
837 int dx
, dy
, dz
, button
;
838 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
839 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
840 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
842 dx
= strtol(dx_str
, NULL
, 0);
843 dy
= strtol(dy_str
, NULL
, 0);
844 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
845 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
848 dz
= strtol(dz_str
, NULL
, 0);
850 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
851 qemu_input_queue_btn(NULL
, button
, true);
852 qemu_input_event_sync();
853 qemu_input_queue_btn(NULL
, button
, false);
856 qemu_input_event_sync();
859 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
861 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
862 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
863 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
864 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
866 int button_state
= qdict_get_int(qdict
, "button_state");
868 if (mouse_button_state
== button_state
) {
871 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
872 qemu_input_event_sync();
873 mouse_button_state
= button_state
;
876 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
878 int size
= qdict_get_int(qdict
, "size");
879 int addr
= qdict_get_int(qdict
, "addr");
880 int has_index
= qdict_haskey(qdict
, "index");
885 int index
= qdict_get_int(qdict
, "index");
886 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
906 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
907 suffix
, addr
, size
* 2, val
);
910 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
912 int size
= qdict_get_int(qdict
, "size");
913 int addr
= qdict_get_int(qdict
, "addr");
914 int val
= qdict_get_int(qdict
, "val");
916 addr
&= IOPORTS_MASK
;
932 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
934 Error
*local_err
= NULL
;
935 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
937 qemu_boot_set(bootdevice
, &local_err
);
939 error_report_err(local_err
);
941 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
945 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
947 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
948 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
949 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
950 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
952 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
955 /* Capture support */
956 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
958 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
963 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
964 monitor_printf(mon
, "[%d]: ", i
);
965 s
->ops
.info (s
->opaque
);
969 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
972 int n
= qdict_get_int(qdict
, "n");
975 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
977 s
->ops
.destroy (s
->opaque
);
978 QLIST_REMOVE (s
, entries
);
985 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
987 const char *path
= qdict_get_str(qdict
, "path");
988 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
989 int bits
= qdict_get_try_int(qdict
, "bits", 16);
990 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
991 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
993 AudioState
*as
= audio_state_by_name(audiodev
);
996 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1000 s
= g_malloc0 (sizeof (*s
));
1002 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1003 monitor_printf(mon
, "Failed to add wave capture\n");
1007 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1010 void qmp_getfd(const char *fdname
, Error
**errp
)
1012 Monitor
*cur_mon
= monitor_cur();
1016 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1018 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1022 if (qemu_isdigit(fdname
[0])) {
1024 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1025 "a name not starting with a digit");
1029 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
1030 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1031 if (strcmp(monfd
->name
, fdname
) != 0) {
1037 /* Make sure close() is outside critical section */
1042 monfd
= g_new0(mon_fd_t
, 1);
1043 monfd
->name
= g_strdup(fdname
);
1046 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1049 void qmp_closefd(const char *fdname
, Error
**errp
)
1051 Monitor
*cur_mon
= monitor_cur();
1055 qemu_mutex_lock(&cur_mon
->mon_lock
);
1056 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1057 if (strcmp(monfd
->name
, fdname
) != 0) {
1061 QLIST_REMOVE(monfd
, next
);
1063 g_free(monfd
->name
);
1065 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1066 /* Make sure close() is outside critical section */
1071 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1072 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
1075 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1079 QEMU_LOCK_GUARD(&mon
->mon_lock
);
1080 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1083 if (strcmp(monfd
->name
, fdname
) != 0) {
1089 /* caller takes ownership of fd */
1090 QLIST_REMOVE(monfd
, next
);
1091 g_free(monfd
->name
);
1097 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1101 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1103 MonFdsetFd
*mon_fdset_fd
;
1104 MonFdsetFd
*mon_fdset_fd_next
;
1106 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1107 if ((mon_fdset_fd
->removed
||
1108 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1109 runstate_is_running()) {
1110 close(mon_fdset_fd
->fd
);
1111 g_free(mon_fdset_fd
->opaque
);
1112 QLIST_REMOVE(mon_fdset_fd
, next
);
1113 g_free(mon_fdset_fd
);
1117 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1118 QLIST_REMOVE(mon_fdset
, next
);
1123 void monitor_fdsets_cleanup(void)
1125 MonFdset
*mon_fdset
;
1126 MonFdset
*mon_fdset_next
;
1128 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1129 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1130 monitor_fdset_cleanup(mon_fdset
);
1134 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1135 const char *opaque
, Error
**errp
)
1138 Monitor
*mon
= monitor_cur();
1141 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1143 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
1147 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1148 has_opaque
, opaque
, errp
);
1160 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1162 MonFdset
*mon_fdset
;
1163 MonFdsetFd
*mon_fdset_fd
;
1166 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1167 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1168 if (mon_fdset
->id
!= fdset_id
) {
1171 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1173 if (mon_fdset_fd
->fd
!= fd
) {
1176 mon_fdset_fd
->removed
= true;
1179 mon_fdset_fd
->removed
= true;
1182 if (has_fd
&& !mon_fdset_fd
) {
1185 monitor_fdset_cleanup(mon_fdset
);
1191 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1194 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1196 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
1199 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1201 MonFdset
*mon_fdset
;
1202 MonFdsetFd
*mon_fdset_fd
;
1203 FdsetInfoList
*fdset_list
= NULL
;
1205 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1206 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1207 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1209 fdset_info
->fdset_id
= mon_fdset
->id
;
1211 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1212 FdsetFdInfo
*fdsetfd_info
;
1214 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1215 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
1216 if (mon_fdset_fd
->opaque
) {
1217 fdsetfd_info
->has_opaque
= true;
1218 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1220 fdsetfd_info
->has_opaque
= false;
1223 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
1226 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
1232 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1233 bool has_opaque
, const char *opaque
,
1236 MonFdset
*mon_fdset
= NULL
;
1237 MonFdsetFd
*mon_fdset_fd
;
1240 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1242 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1243 /* Break if match found or match impossible due to ordering by ID */
1244 if (fdset_id
<= mon_fdset
->id
) {
1245 if (fdset_id
< mon_fdset
->id
) {
1253 if (mon_fdset
== NULL
) {
1254 int64_t fdset_id_prev
= -1;
1255 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1259 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1260 "a non-negative value");
1263 /* Use specified fdset ID */
1264 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1265 mon_fdset_cur
= mon_fdset
;
1266 if (fdset_id
< mon_fdset_cur
->id
) {
1271 /* Use first available fdset ID */
1272 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1273 mon_fdset_cur
= mon_fdset
;
1274 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1275 fdset_id_prev
= mon_fdset_cur
->id
;
1282 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1284 mon_fdset
->id
= fdset_id
;
1286 mon_fdset
->id
= fdset_id_prev
+ 1;
1289 /* The fdset list is ordered by fdset ID */
1290 if (!mon_fdset_cur
) {
1291 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1292 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1293 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1295 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1299 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1300 mon_fdset_fd
->fd
= fd
;
1301 mon_fdset_fd
->removed
= false;
1303 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1305 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1307 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1308 fdinfo
->fdset_id
= mon_fdset
->id
;
1309 fdinfo
->fd
= mon_fdset_fd
->fd
;
1314 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1319 MonFdset
*mon_fdset
;
1321 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1322 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1323 MonFdsetFd
*mon_fdset_fd
;
1324 MonFdsetFd
*mon_fdset_fd_dup
;
1329 if (mon_fdset
->id
!= fdset_id
) {
1333 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1334 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1335 if (mon_fd_flags
== -1) {
1339 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1340 fd
= mon_fdset_fd
->fd
;
1350 dup_fd
= qemu_dup_flags(fd
, flags
);
1355 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1356 mon_fdset_fd_dup
->fd
= dup_fd
;
1357 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1366 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1368 MonFdset
*mon_fdset
;
1369 MonFdsetFd
*mon_fdset_fd_dup
;
1371 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1372 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1373 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1374 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1376 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1377 g_free(mon_fdset_fd_dup
);
1378 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1379 monitor_fdset_cleanup(mon_fdset
);
1383 return mon_fdset
->id
;
1392 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1394 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1397 void monitor_fdset_dup_fd_remove(int dup_fd
)
1399 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1402 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1405 Error
*local_err
= NULL
;
1407 if (!qemu_isdigit(fdname
[0]) && mon
) {
1408 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1410 fd
= qemu_parse_fd(fdname
);
1412 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1417 error_propagate(errp
, local_err
);
1426 /* Please update hmp-commands.hx when adding or changing commands */
1427 static HMPCommand hmp_info_cmds
[] = {
1428 #include "hmp-commands-info.h"
1432 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1433 HMPCommand hmp_cmds
[] = {
1434 #include "hmp-commands.h"
1439 * Set @pval to the value in the register identified by @name.
1440 * return 0 if OK, -1 if not found
1442 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1444 const MonitorDef
*md
= target_monitor_defs();
1445 CPUState
*cs
= mon_get_cpu(mon
);
1450 if (cs
== NULL
|| md
== NULL
) {
1454 for(; md
->name
!= NULL
; md
++) {
1455 if (hmp_compare_cmd(name
, md
->name
)) {
1456 if (md
->get_value
) {
1457 *pval
= md
->get_value(mon
, md
, md
->offset
);
1459 CPUArchState
*env
= mon_get_cpu_env(mon
);
1460 ptr
= (uint8_t *)env
+ md
->offset
;
1463 *pval
= *(int32_t *)ptr
;
1466 *pval
= *(target_long
*)ptr
;
1477 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1479 *pval
= (target_long
) tmp
;
1485 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1488 if (!str
|| !option
) {
1491 if (!strncmp(option
, str
, strlen(str
))) {
1492 readline_add_completion(rs
, option
);
1496 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1499 ChardevBackendInfoList
*list
, *start
;
1505 readline_set_completion_index(rs
, len
);
1507 start
= list
= qmp_query_chardev_backends(NULL
);
1509 const char *chr_name
= list
->value
->name
;
1511 if (!strncmp(chr_name
, str
, len
)) {
1512 readline_add_completion(rs
, chr_name
);
1516 qapi_free_ChardevBackendInfoList(start
);
1519 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1528 readline_set_completion_index(rs
, len
);
1529 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1530 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1534 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1544 readline_set_completion_index(rs
, len
);
1545 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1548 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1550 name
= object_class_get_name(OBJECT_CLASS(dc
));
1552 if (dc
->user_creatable
1553 && !strncmp(name
, str
, len
)) {
1554 readline_add_completion(rs
, name
);
1561 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1571 readline_set_completion_index(rs
, len
);
1572 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1576 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1577 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1578 readline_add_completion(rs
, name
);
1585 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1587 GSList
**list
= opaque
;
1588 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1594 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1595 *list
= g_slist_append(*list
, dev
);
1601 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1603 GSList
*list
= NULL
;
1605 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1610 static void peripheral_device_del_completion(ReadLineState
*rs
,
1611 const char *str
, size_t len
)
1613 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1614 GSList
*list
, *item
;
1616 list
= qdev_build_hotpluggable_device_list(peripheral
);
1621 for (item
= list
; item
; item
= g_slist_next(item
)) {
1622 DeviceState
*dev
= item
->data
;
1624 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1625 readline_add_completion(rs
, dev
->id
);
1632 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1635 ChardevInfoList
*list
, *start
;
1641 readline_set_completion_index(rs
, len
);
1643 start
= list
= qmp_query_chardev(NULL
);
1645 ChardevInfo
*chr
= list
->value
;
1647 if (!strncmp(chr
->label
, str
, len
)) {
1648 readline_add_completion(rs
, chr
->label
);
1652 qapi_free_ChardevInfoList(start
);
1655 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
1658 ChardevInfoList
*list
, *start
;
1661 readline_set_completion_index(rs
, len
);
1663 start
= list
= qmp_query_chardev(NULL
);
1665 ChardevInfo
*chr_info
= list
->value
;
1667 if (!strncmp(chr_info
->label
, str
, len
)) {
1668 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
1669 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
1670 readline_add_completion(rs
, chr_info
->label
);
1675 qapi_free_ChardevInfoList(start
);
1678 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1683 ringbuf_completion(rs
, str
);
1686 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1695 readline_set_completion_index(rs
, len
);
1696 peripheral_device_del_completion(rs
, str
, len
);
1699 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1701 ObjectPropertyInfoList
*list
, *start
;
1708 readline_set_completion_index(rs
, len
);
1710 start
= list
= qmp_qom_list("/objects", NULL
);
1712 ObjectPropertyInfo
*info
= list
->value
;
1714 if (!strncmp(info
->type
, "child<", 5)
1715 && !strncmp(info
->name
, str
, len
)) {
1716 readline_add_completion(rs
, info
->name
);
1720 qapi_free_ObjectPropertyInfoList(start
);
1723 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1732 sep
= strrchr(str
, '-');
1737 readline_set_completion_index(rs
, len
);
1738 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
1739 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
1740 readline_add_completion(rs
, QKeyCode_str(i
));
1745 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1750 readline_set_completion_index(rs
, len
);
1752 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1754 count
= qemu_find_net_clients_except(NULL
, ncs
,
1755 NET_CLIENT_DRIVER_NONE
,
1757 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1758 const char *name
= ncs
[i
]->name
;
1759 if (!strncmp(str
, name
, len
)) {
1760 readline_add_completion(rs
, name
);
1763 } else if (nb_args
== 3) {
1764 add_completion_option(rs
, str
, "on");
1765 add_completion_option(rs
, str
, "off");
1769 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1772 NetClientState
*ncs
[MAX_QUEUE_NUM
];
1779 readline_set_completion_index(rs
, len
);
1780 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
1782 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
1783 const char *name
= ncs
[i
]->name
;
1784 if (strncmp(str
, name
, len
)) {
1787 if (ncs
[i
]->is_netdev
) {
1788 readline_add_completion(rs
, name
);
1793 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1798 readline_set_completion_index(rs
, len
);
1800 TraceEventIter iter
;
1802 char *pattern
= g_strdup_printf("%s*", str
);
1803 trace_event_iter_init_pattern(&iter
, pattern
);
1804 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1805 readline_add_completion(rs
, trace_event_get_name(ev
));
1811 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1816 readline_set_completion_index(rs
, len
);
1818 TraceEventIter iter
;
1820 char *pattern
= g_strdup_printf("%s*", str
);
1821 trace_event_iter_init_pattern(&iter
, pattern
);
1822 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
1823 readline_add_completion(rs
, trace_event_get_name(ev
));
1826 } else if (nb_args
== 3) {
1827 add_completion_option(rs
, str
, "on");
1828 add_completion_option(rs
, str
, "off");
1832 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1839 readline_set_completion_index(rs
, strlen(str
));
1840 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1841 add_completion_option(rs
, str
, WatchdogAction_str(i
));
1845 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
1851 readline_set_completion_index(rs
, len
);
1854 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
1855 const char *name
= MigrationCapability_str(i
);
1856 if (!strncmp(str
, name
, len
)) {
1857 readline_add_completion(rs
, name
);
1860 } else if (nb_args
== 3) {
1861 add_completion_option(rs
, str
, "on");
1862 add_completion_option(rs
, str
, "off");
1866 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
1872 readline_set_completion_index(rs
, len
);
1875 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
1876 const char *name
= MigrationParameter_str(i
);
1877 if (!strncmp(str
, name
, len
)) {
1878 readline_add_completion(rs
, name
);
1884 static void vm_completion(ReadLineState
*rs
, const char *str
)
1887 BlockDriverState
*bs
;
1888 BdrvNextIterator it
;
1891 readline_set_completion_index(rs
, len
);
1893 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
1894 SnapshotInfoList
*snapshots
, *snapshot
;
1895 AioContext
*ctx
= bdrv_get_aio_context(bs
);
1898 aio_context_acquire(ctx
);
1899 if (bdrv_can_snapshot(bs
)) {
1900 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
1902 aio_context_release(ctx
);
1907 snapshot
= snapshots
;
1909 char *completion
= snapshot
->value
->name
;
1910 if (!strncmp(str
, completion
, len
)) {
1911 readline_add_completion(rs
, completion
);
1913 completion
= snapshot
->value
->id
;
1914 if (!strncmp(str
, completion
, len
)) {
1915 readline_add_completion(rs
, completion
);
1917 snapshot
= snapshot
->next
;
1919 qapi_free_SnapshotInfoList(snapshots
);
1924 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1927 vm_completion(rs
, str
);
1931 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1934 vm_completion(rs
, str
);
1939 compare_mon_cmd(const void *a
, const void *b
)
1941 return strcmp(((const HMPCommand
*)a
)->name
,
1942 ((const HMPCommand
*)b
)->name
);
1945 static void sortcmdlist(void)
1947 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1950 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1951 sizeof(*hmp_info_cmds
),
1955 void monitor_register_hmp(const char *name
, bool info
,
1956 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1958 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1960 while (table
->name
!= NULL
) {
1961 if (strcmp(table
->name
, name
) == 0) {
1962 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1968 g_assert_not_reached();
1971 void monitor_register_hmp_info_hrt(const char *name
,
1972 HumanReadableText
*(*handler
)(Error
**errp
))
1974 HMPCommand
*table
= hmp_info_cmds
;
1976 while (table
->name
!= NULL
) {
1977 if (strcmp(table
->name
, name
) == 0) {
1978 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1979 table
->cmd_info_hrt
= handler
;
1984 g_assert_not_reached();
1987 void monitor_init_globals(void)
1989 monitor_init_globals_core();
1990 monitor_init_qmp_commands();
1992 qemu_mutex_init(&mon_fdsets_lock
);