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.h"
70 #include "qapi/qapi-emit-events.h"
71 #include "qapi/error.h"
72 #include "qapi/qmp-event.h"
73 #include "qapi/qapi-introspect.h"
74 #include "sysemu/cpus.h"
75 #include "qemu/cutils.h"
78 #if defined(TARGET_S390X)
79 #include "hw/s390x/storage-keys.h"
80 #include "hw/s390x/storage-attributes.h"
83 /* file descriptors passed via SCM_RIGHTS */
84 typedef struct mon_fd_t mon_fd_t
;
88 QLIST_ENTRY(mon_fd_t
) next
;
91 /* file descriptor associated with a file descriptor set */
92 typedef struct MonFdsetFd MonFdsetFd
;
97 QLIST_ENTRY(MonFdsetFd
) next
;
100 /* file descriptor set containing fds passed via SCM_RIGHTS */
101 typedef struct MonFdset MonFdset
;
104 QLIST_HEAD(, MonFdsetFd
) fds
;
105 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
106 QLIST_ENTRY(MonFdset
) next
;
109 /* QMP checker flags */
110 #define QMP_ACCEPT_UNKNOWNS 1
112 /* Protects mon_fdsets */
113 static QemuMutex mon_fdsets_lock
;
114 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
116 static HMPCommand hmp_info_cmds
[];
118 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
119 int64_t cpu_index
, Error
**errp
)
125 monitor_data_init(&hmp
.common
, false, true, false);
128 cur_mon
= &hmp
.common
;
131 int ret
= monitor_set_cpu(cpu_index
);
134 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
140 handle_hmp_command(&hmp
, command_line
);
143 qemu_mutex_lock(&hmp
.common
.mon_lock
);
144 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
145 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
147 output
= g_strdup("");
149 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
152 monitor_data_destroy(&hmp
.common
);
157 * Is @name in the '|' separated list of names @list?
159 int hmp_compare_cmd(const char *name
, const char *list
)
161 const char *p
, *pstart
;
167 p
= qemu_strchrnul(p
, '|');
168 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
179 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
181 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
184 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
186 const char *tp_name
= qdict_get_str(qdict
, "name");
187 bool new_state
= qdict_get_bool(qdict
, "option");
188 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
189 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
190 Error
*local_err
= NULL
;
193 monitor_printf(mon
, "argument vcpu must be positive");
197 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
199 error_report_err(local_err
);
203 #ifdef CONFIG_TRACE_SIMPLE
204 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
206 const char *op
= qdict_get_try_str(qdict
, "op");
207 const char *arg
= qdict_get_try_str(qdict
, "arg");
210 st_print_trace_file_status();
211 } else if (!strcmp(op
, "on")) {
212 st_set_trace_file_enabled(true);
213 } else if (!strcmp(op
, "off")) {
214 st_set_trace_file_enabled(false);
215 } else if (!strcmp(op
, "flush")) {
216 st_flush_trace_buffer();
217 } else if (!strcmp(op
, "set")) {
219 st_set_trace_file(arg
);
222 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
223 help_cmd(mon
, "trace-file");
228 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
230 help_cmd(mon
, "info");
233 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
235 CommandInfoList
*info
, **list
= opaque
;
241 info
= g_malloc0(sizeof(*info
));
242 info
->value
= g_malloc0(sizeof(*info
->value
));
243 info
->value
->name
= g_strdup(cmd
->name
);
248 CommandInfoList
*qmp_query_commands(Error
**errp
)
250 CommandInfoList
*list
= NULL
;
253 assert(monitor_is_qmp(cur_mon
));
254 mon
= container_of(cur_mon
, MonitorQMP
, common
);
256 qmp_for_each_command(mon
->commands
, query_commands_cb
, &list
);
261 EventInfoList
*qmp_query_events(Error
**errp
)
264 * TODO This deprecated command is the only user of
265 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
266 * they should go, too.
268 EventInfoList
*info
, *ev_list
= NULL
;
271 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
272 const char *event_name
= QAPIEvent_str(e
);
273 assert(event_name
!= NULL
);
274 info
= g_malloc0(sizeof(*info
));
275 info
->value
= g_malloc0(sizeof(*info
->value
));
276 info
->value
->name
= g_strdup(event_name
);
278 info
->next
= ev_list
;
286 * Minor hack: generated marshalling suppressed for this command
287 * ('gen': false in the schema) so we can parse the JSON string
288 * directly into QObject instead of first parsing it with
289 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
290 * to QObject with generated output marshallers, every time. Instead,
291 * we do it in test-qobject-input-visitor.c, just to make sure
292 * qapi-gen.py's output actually conforms to the schema.
294 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
297 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
300 static void monitor_init_qmp_commands(void)
304 * - qmp_commands contains all QMP commands
305 * - qmp_cap_negotiation_commands contains just
306 * "qmp_capabilities", to enforce capability negotiation
309 qmp_init_marshal(&qmp_commands
);
311 qmp_register_command(&qmp_commands
, "query-qmp-schema",
312 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
313 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
315 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
318 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
319 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
320 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
324 * Accept QMP capabilities in @list for @mon.
325 * On success, set mon->qmp.capab[], and return true.
326 * On error, set @errp, and return false.
328 static bool qmp_caps_accept(MonitorQMP
*mon
, QMPCapabilityList
*list
,
331 GString
*unavailable
= NULL
;
332 bool capab
[QMP_CAPABILITY__MAX
];
334 memset(capab
, 0, sizeof(capab
));
336 for (; list
; list
= list
->next
) {
337 if (!mon
->capab_offered
[list
->value
]) {
339 unavailable
= g_string_new(QMPCapability_str(list
->value
));
341 g_string_append_printf(unavailable
, ", %s",
342 QMPCapability_str(list
->value
));
345 capab
[list
->value
] = true;
349 error_setg(errp
, "Capability %s not available", unavailable
->str
);
350 g_string_free(unavailable
, true);
354 memcpy(mon
->capab
, capab
, sizeof(capab
));
358 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
363 assert(monitor_is_qmp(cur_mon
));
364 mon
= container_of(cur_mon
, MonitorQMP
, common
);
366 if (mon
->commands
== &qmp_commands
) {
367 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
368 "Capabilities negotiation is already complete, command "
373 if (!qmp_caps_accept(mon
, enable
, errp
)) {
377 mon
->commands
= &qmp_commands
;
380 /* Set the current CPU defined by the user. Callers must hold BQL. */
381 int monitor_set_cpu(int cpu_index
)
385 cpu
= qemu_get_cpu(cpu_index
);
389 g_free(cur_mon
->mon_cpu_path
);
390 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
394 /* Callers must hold BQL. */
395 static CPUState
*mon_get_cpu_sync(bool synchronize
)
399 if (cur_mon
->mon_cpu_path
) {
400 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
403 g_free(cur_mon
->mon_cpu_path
);
404 cur_mon
->mon_cpu_path
= NULL
;
407 if (!cur_mon
->mon_cpu_path
) {
411 monitor_set_cpu(first_cpu
->cpu_index
);
415 cpu_synchronize_state(cpu
);
420 CPUState
*mon_get_cpu(void)
422 return mon_get_cpu_sync(true);
425 CPUArchState
*mon_get_cpu_env(void)
427 CPUState
*cs
= mon_get_cpu();
429 return cs
? cs
->env_ptr
: NULL
;
432 int monitor_get_cpu_index(void)
434 CPUState
*cs
= mon_get_cpu_sync(false);
436 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
439 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
441 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
446 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
447 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
453 monitor_printf(mon
, "No CPU available\n");
457 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
462 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
464 if (!tcg_enabled()) {
465 error_report("JIT information is only available with accel=tcg");
473 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
479 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
481 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
482 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
483 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
484 enum QSPSortBy sort_by
;
486 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
487 qsp_report(max
, sort_by
, coalesce
);
490 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
492 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
501 str
= readline_get_history(hmp_mon
->rs
, i
);
505 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
510 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
512 CPUState
*cs
= mon_get_cpu();
515 monitor_printf(mon
, "No CPU available\n");
518 cpu_dump_statistics(cs
, 0);
521 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
523 const char *name
= qdict_get_try_str(qdict
, "name");
524 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
525 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
526 TraceEventInfoList
*events
;
527 TraceEventInfoList
*elem
;
528 Error
*local_err
= NULL
;
534 monitor_printf(mon
, "argument vcpu must be positive");
538 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
540 error_report_err(local_err
);
544 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
545 monitor_printf(mon
, "%s : state %u\n",
547 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
549 qapi_free_TraceEventInfoList(events
);
552 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
553 bool has_port
, int64_t port
,
554 bool has_tls_port
, int64_t tls_port
,
555 bool has_cert_subject
, const char *cert_subject
,
558 if (strcmp(protocol
, "spice") == 0) {
559 if (!qemu_using_spice(errp
)) {
563 if (!has_port
&& !has_tls_port
) {
564 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
568 if (qemu_spice_migrate_info(hostname
,
569 has_port
? port
: -1,
570 has_tls_port
? tls_port
: -1,
572 error_setg(errp
, QERR_UNDEFINED_ERROR
);
578 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
581 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
585 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
587 error_report_err(err
);
591 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
594 const char *items
= qdict_get_str(qdict
, "items");
596 if (!strcmp(items
, "none")) {
599 mask
= qemu_str_to_log_mask(items
);
601 help_cmd(mon
, "log");
608 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
610 const char *option
= qdict_get_try_str(qdict
, "option");
611 if (!option
|| !strcmp(option
, "on")) {
613 } else if (!strcmp(option
, "off")) {
616 monitor_printf(mon
, "unexpected option %s\n", option
);
620 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
622 const char *device
= qdict_get_try_str(qdict
, "device");
624 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
625 if (gdbserver_start(device
) < 0) {
626 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
628 } else if (strcmp(device
, "none") == 0) {
629 monitor_printf(mon
, "Disabled gdbserver\n");
631 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
636 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
638 const char *action
= qdict_get_str(qdict
, "action");
639 if (select_watchdog_action(action
) == -1) {
640 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
644 static void monitor_printc(Monitor
*mon
, int c
)
646 monitor_printf(mon
, "'");
649 monitor_printf(mon
, "\\'");
652 monitor_printf(mon
, "\\\\");
655 monitor_printf(mon
, "\\n");
658 monitor_printf(mon
, "\\r");
661 if (c
>= 32 && c
<= 126) {
662 monitor_printf(mon
, "%c", c
);
664 monitor_printf(mon
, "\\x%02x", c
);
668 monitor_printf(mon
, "'");
671 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
672 hwaddr addr
, int is_physical
)
674 int l
, line_size
, i
, max_digits
, len
;
677 CPUState
*cs
= mon_get_cpu();
679 if (!cs
&& (format
== 'i' || !is_physical
)) {
680 monitor_printf(mon
, "Can not dump without CPU\n");
685 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
698 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
702 max_digits
= (wsize
* 8) / 4;
706 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
715 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
717 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
722 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
723 MemTxResult r
= address_space_read(as
, addr
,
724 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
726 monitor_printf(mon
, " Cannot access memory\n");
730 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
731 monitor_printf(mon
, " Cannot access memory\n");
746 v
= (uint32_t)ldl_p(buf
+ i
);
752 monitor_printf(mon
, " ");
755 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
758 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
761 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
764 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
767 monitor_printc(mon
, v
);
772 monitor_printf(mon
, "\n");
778 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
780 int count
= qdict_get_int(qdict
, "count");
781 int format
= qdict_get_int(qdict
, "format");
782 int size
= qdict_get_int(qdict
, "size");
783 target_long addr
= qdict_get_int(qdict
, "addr");
785 memory_dump(mon
, count
, format
, size
, addr
, 0);
788 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
790 int count
= qdict_get_int(qdict
, "count");
791 int format
= qdict_get_int(qdict
, "format");
792 int size
= qdict_get_int(qdict
, "size");
793 hwaddr addr
= qdict_get_int(qdict
, "addr");
795 memory_dump(mon
, count
, format
, size
, addr
, 1);
798 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
800 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
804 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
808 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
809 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
810 memory_region_unref(mrs
.mr
);
815 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
818 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
820 hwaddr addr
= qdict_get_int(qdict
, "addr");
821 Error
*local_err
= NULL
;
822 MemoryRegion
*mr
= NULL
;
825 ptr
= gpa2hva(&mr
, addr
, &local_err
);
827 error_report_err(local_err
);
831 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
833 addr
, mr
->name
, ptr
);
835 memory_region_unref(mr
);
838 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
840 target_ulong addr
= qdict_get_int(qdict
, "addr");
842 CPUState
*cs
= mon_get_cpu();
846 monitor_printf(mon
, "No cpu\n");
850 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
852 monitor_printf(mon
, "Unmapped\n");
854 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
855 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
860 static uint64_t vtop(void *ptr
, Error
**errp
)
864 uintptr_t addr
= (uintptr_t) ptr
;
865 uintptr_t pagesize
= getpagesize();
866 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
869 fd
= open("/proc/self/pagemap", O_RDONLY
);
871 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
875 /* Force copy-on-write if necessary. */
876 atomic_add((uint8_t *)ptr
, 0);
878 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
879 error_setg_errno(errp
, errno
, "Cannot read pagemap");
882 if ((pinfo
& (1ull << 63)) == 0) {
883 error_setg(errp
, "Page not present");
886 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
893 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
895 hwaddr addr
= qdict_get_int(qdict
, "addr");
896 Error
*local_err
= NULL
;
897 MemoryRegion
*mr
= NULL
;
901 ptr
= gpa2hva(&mr
, addr
, &local_err
);
903 error_report_err(local_err
);
907 physaddr
= vtop(ptr
, &local_err
);
909 error_report_err(local_err
);
911 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
912 " (%s) is 0x%" PRIx64
"\n",
913 addr
, mr
->name
, (uint64_t) physaddr
);
916 memory_region_unref(mr
);
920 static void do_print(Monitor
*mon
, const QDict
*qdict
)
922 int format
= qdict_get_int(qdict
, "format");
923 hwaddr val
= qdict_get_int(qdict
, "val");
927 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
930 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
933 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
937 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
940 monitor_printc(mon
, val
);
943 monitor_printf(mon
, "\n");
946 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
950 uint32_t start
= qdict_get_int(qdict
, "start");
951 uint32_t size
= qdict_get_int(qdict
, "size");
954 for(addr
= start
; addr
< (start
+ size
); addr
++) {
955 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
956 MEMTXATTRS_UNSPECIFIED
, NULL
);
957 /* BSD sum algorithm ('sum' Unix command) */
958 sum
= (sum
>> 1) | (sum
<< 15);
961 monitor_printf(mon
, "%05d\n", sum
);
964 static int mouse_button_state
;
966 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
968 int dx
, dy
, dz
, button
;
969 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
970 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
971 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
973 dx
= strtol(dx_str
, NULL
, 0);
974 dy
= strtol(dy_str
, NULL
, 0);
975 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
976 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
979 dz
= strtol(dz_str
, NULL
, 0);
981 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
982 qemu_input_queue_btn(NULL
, button
, true);
983 qemu_input_event_sync();
984 qemu_input_queue_btn(NULL
, button
, false);
987 qemu_input_event_sync();
990 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
992 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
993 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
994 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
995 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
997 int button_state
= qdict_get_int(qdict
, "button_state");
999 if (mouse_button_state
== button_state
) {
1002 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1003 qemu_input_event_sync();
1004 mouse_button_state
= button_state
;
1007 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1009 int size
= qdict_get_int(qdict
, "size");
1010 int addr
= qdict_get_int(qdict
, "addr");
1011 int has_index
= qdict_haskey(qdict
, "index");
1016 int index
= qdict_get_int(qdict
, "index");
1017 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1025 val
= cpu_inb(addr
);
1029 val
= cpu_inw(addr
);
1033 val
= cpu_inl(addr
);
1037 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1038 suffix
, addr
, size
* 2, val
);
1041 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1043 int size
= qdict_get_int(qdict
, "size");
1044 int addr
= qdict_get_int(qdict
, "addr");
1045 int val
= qdict_get_int(qdict
, "val");
1047 addr
&= IOPORTS_MASK
;
1052 cpu_outb(addr
, val
);
1055 cpu_outw(addr
, val
);
1058 cpu_outl(addr
, val
);
1063 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1065 Error
*local_err
= NULL
;
1066 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1068 qemu_boot_set(bootdevice
, &local_err
);
1070 error_report_err(local_err
);
1072 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1076 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1078 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1079 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1080 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1082 mtree_info(flatview
, dispatch_tree
, owner
);
1085 #ifdef CONFIG_PROFILER
1089 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1091 static int64_t last_cpu_exec_time
;
1092 int64_t cpu_exec_time
;
1095 cpu_exec_time
= tcg_cpu_exec_time();
1096 delta
= cpu_exec_time
- last_cpu_exec_time
;
1098 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1099 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1100 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1101 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1102 last_cpu_exec_time
= cpu_exec_time
;
1106 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1108 monitor_printf(mon
, "Internal profiler not compiled\n");
1112 /* Capture support */
1113 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1115 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1120 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1121 monitor_printf(mon
, "[%d]: ", i
);
1122 s
->ops
.info (s
->opaque
);
1126 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1129 int n
= qdict_get_int(qdict
, "n");
1132 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1134 s
->ops
.destroy (s
->opaque
);
1135 QLIST_REMOVE (s
, entries
);
1142 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1144 const char *path
= qdict_get_str(qdict
, "path");
1145 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1146 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1147 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1148 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1150 AudioState
*as
= audio_state_by_name(audiodev
);
1153 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1157 s
= g_malloc0 (sizeof (*s
));
1159 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1160 monitor_printf(mon
, "Failed to add wave capture\n");
1164 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1167 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1172 container
= object_get_objects_root();
1173 obj
= object_resolve_path_component(container
, name
);
1175 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1179 return QAUTHZ_LIST(obj
);
1182 static bool warn_acl
;
1183 static void hmp_warn_acl(void)
1188 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1189 "commands are deprecated with no replacement. Authorization "
1190 "for VNC should be performed using the pluggable QAuthZ "
1195 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1197 const char *aclname
= qdict_get_str(qdict
, "aclname");
1198 QAuthZList
*auth
= find_auth(mon
, aclname
);
1199 QAuthZListRuleList
*rules
;
1208 monitor_printf(mon
, "policy: %s\n",
1209 QAuthZListPolicy_str(auth
->policy
));
1211 rules
= auth
->rules
;
1213 QAuthZListRule
*rule
= rules
->value
;
1215 monitor_printf(mon
, "%zu: %s %s\n", i
,
1216 QAuthZListPolicy_str(rule
->policy
),
1218 rules
= rules
->next
;
1222 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1224 const char *aclname
= qdict_get_str(qdict
, "aclname");
1225 QAuthZList
*auth
= find_auth(mon
, aclname
);
1233 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1234 qapi_free_QAuthZListRuleList(auth
->rules
);
1236 monitor_printf(mon
, "acl: removed all rules\n");
1239 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1241 const char *aclname
= qdict_get_str(qdict
, "aclname");
1242 const char *policy
= qdict_get_str(qdict
, "policy");
1243 QAuthZList
*auth
= find_auth(mon
, aclname
);
1253 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1255 QAUTHZ_LIST_POLICY_DENY
,
1259 monitor_printf(mon
, "acl: unknown policy '%s', "
1260 "expected 'deny' or 'allow'\n", policy
);
1263 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1264 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1266 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1271 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1273 if (strchr(match
, '*')) {
1274 return QAUTHZ_LIST_FORMAT_GLOB
;
1276 return QAUTHZ_LIST_FORMAT_EXACT
;
1280 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1282 const char *aclname
= qdict_get_str(qdict
, "aclname");
1283 const char *match
= qdict_get_str(qdict
, "match");
1284 const char *policystr
= qdict_get_str(qdict
, "policy");
1285 int has_index
= qdict_haskey(qdict
, "index");
1286 int index
= qdict_get_try_int(qdict
, "index", -1);
1287 QAuthZList
*auth
= find_auth(mon
, aclname
);
1289 QAuthZListPolicy policy
;
1290 QAuthZListFormat format
;
1299 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1301 QAUTHZ_LIST_POLICY_DENY
,
1305 monitor_printf(mon
, "acl: unknown policy '%s', "
1306 "expected 'deny' or 'allow'\n", policystr
);
1310 format
= hmp_acl_get_format(match
);
1312 if (has_index
&& index
== 0) {
1313 monitor_printf(mon
, "acl: unable to add acl entry\n");
1318 i
= qauthz_list_insert_rule(auth
, match
, policy
,
1319 format
, index
- 1, &err
);
1321 i
= qauthz_list_append_rule(auth
, match
, policy
,
1325 monitor_printf(mon
, "acl: unable to add rule: %s",
1326 error_get_pretty(err
));
1329 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
1333 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1335 const char *aclname
= qdict_get_str(qdict
, "aclname");
1336 const char *match
= qdict_get_str(qdict
, "match");
1337 QAuthZList
*auth
= find_auth(mon
, aclname
);
1346 i
= qauthz_list_delete_rule(auth
, match
);
1348 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
1350 monitor_printf(mon
, "acl: no matching acl entry\n");
1354 void qmp_getfd(const char *fdname
, Error
**errp
)
1359 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1361 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1365 if (qemu_isdigit(fdname
[0])) {
1367 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1368 "a name not starting with a digit");
1372 qemu_mutex_lock(&cur_mon
->mon_lock
);
1373 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1374 if (strcmp(monfd
->name
, fdname
) != 0) {
1380 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1381 /* Make sure close() is outside critical section */
1386 monfd
= g_malloc0(sizeof(mon_fd_t
));
1387 monfd
->name
= g_strdup(fdname
);
1390 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1391 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1394 void qmp_closefd(const char *fdname
, Error
**errp
)
1399 qemu_mutex_lock(&cur_mon
->mon_lock
);
1400 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1401 if (strcmp(monfd
->name
, fdname
) != 0) {
1405 QLIST_REMOVE(monfd
, next
);
1407 g_free(monfd
->name
);
1409 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1410 /* Make sure close() is outside critical section */
1415 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1416 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1419 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1423 qemu_mutex_lock(&mon
->mon_lock
);
1424 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1427 if (strcmp(monfd
->name
, fdname
) != 0) {
1433 /* caller takes ownership of fd */
1434 QLIST_REMOVE(monfd
, next
);
1435 g_free(monfd
->name
);
1437 qemu_mutex_unlock(&mon
->mon_lock
);
1442 qemu_mutex_unlock(&mon
->mon_lock
);
1443 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1447 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1449 MonFdsetFd
*mon_fdset_fd
;
1450 MonFdsetFd
*mon_fdset_fd_next
;
1452 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1453 if ((mon_fdset_fd
->removed
||
1454 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1455 runstate_is_running()) {
1456 close(mon_fdset_fd
->fd
);
1457 g_free(mon_fdset_fd
->opaque
);
1458 QLIST_REMOVE(mon_fdset_fd
, next
);
1459 g_free(mon_fdset_fd
);
1463 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1464 QLIST_REMOVE(mon_fdset
, next
);
1469 void monitor_fdsets_cleanup(void)
1471 MonFdset
*mon_fdset
;
1472 MonFdset
*mon_fdset_next
;
1474 qemu_mutex_lock(&mon_fdsets_lock
);
1475 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1476 monitor_fdset_cleanup(mon_fdset
);
1478 qemu_mutex_unlock(&mon_fdsets_lock
);
1481 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1482 const char *opaque
, Error
**errp
)
1485 Monitor
*mon
= cur_mon
;
1488 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1490 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1494 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1495 has_opaque
, opaque
, errp
);
1507 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1509 MonFdset
*mon_fdset
;
1510 MonFdsetFd
*mon_fdset_fd
;
1513 qemu_mutex_lock(&mon_fdsets_lock
);
1514 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1515 if (mon_fdset
->id
!= fdset_id
) {
1518 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1520 if (mon_fdset_fd
->fd
!= fd
) {
1523 mon_fdset_fd
->removed
= true;
1526 mon_fdset_fd
->removed
= true;
1529 if (has_fd
&& !mon_fdset_fd
) {
1532 monitor_fdset_cleanup(mon_fdset
);
1533 qemu_mutex_unlock(&mon_fdsets_lock
);
1538 qemu_mutex_unlock(&mon_fdsets_lock
);
1540 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1543 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1545 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1548 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1550 MonFdset
*mon_fdset
;
1551 MonFdsetFd
*mon_fdset_fd
;
1552 FdsetInfoList
*fdset_list
= NULL
;
1554 qemu_mutex_lock(&mon_fdsets_lock
);
1555 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1556 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1557 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1559 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1560 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1562 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1563 FdsetFdInfoList
*fdsetfd_info
;
1565 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1566 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1567 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1568 if (mon_fdset_fd
->opaque
) {
1569 fdsetfd_info
->value
->has_opaque
= true;
1570 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1572 fdsetfd_info
->value
->has_opaque
= false;
1575 fdsetfd_info
->next
= fdsetfd_list
;
1576 fdsetfd_list
= fdsetfd_info
;
1579 fdset_info
->value
->fds
= fdsetfd_list
;
1581 fdset_info
->next
= fdset_list
;
1582 fdset_list
= fdset_info
;
1584 qemu_mutex_unlock(&mon_fdsets_lock
);
1589 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1590 bool has_opaque
, const char *opaque
,
1593 MonFdset
*mon_fdset
= NULL
;
1594 MonFdsetFd
*mon_fdset_fd
;
1597 qemu_mutex_lock(&mon_fdsets_lock
);
1599 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1600 /* Break if match found or match impossible due to ordering by ID */
1601 if (fdset_id
<= mon_fdset
->id
) {
1602 if (fdset_id
< mon_fdset
->id
) {
1610 if (mon_fdset
== NULL
) {
1611 int64_t fdset_id_prev
= -1;
1612 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1616 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1617 "a non-negative value");
1618 qemu_mutex_unlock(&mon_fdsets_lock
);
1621 /* Use specified fdset ID */
1622 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1623 mon_fdset_cur
= mon_fdset
;
1624 if (fdset_id
< mon_fdset_cur
->id
) {
1629 /* Use first available fdset ID */
1630 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1631 mon_fdset_cur
= mon_fdset
;
1632 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1633 fdset_id_prev
= mon_fdset_cur
->id
;
1640 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1642 mon_fdset
->id
= fdset_id
;
1644 mon_fdset
->id
= fdset_id_prev
+ 1;
1647 /* The fdset list is ordered by fdset ID */
1648 if (!mon_fdset_cur
) {
1649 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1650 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1651 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1653 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1657 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1658 mon_fdset_fd
->fd
= fd
;
1659 mon_fdset_fd
->removed
= false;
1661 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1663 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1665 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1666 fdinfo
->fdset_id
= mon_fdset
->id
;
1667 fdinfo
->fd
= mon_fdset_fd
->fd
;
1669 qemu_mutex_unlock(&mon_fdsets_lock
);
1673 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1678 MonFdset
*mon_fdset
;
1679 MonFdsetFd
*mon_fdset_fd
;
1683 qemu_mutex_lock(&mon_fdsets_lock
);
1684 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1685 if (mon_fdset
->id
!= fdset_id
) {
1688 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1689 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1690 if (mon_fd_flags
== -1) {
1695 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1696 ret
= mon_fdset_fd
->fd
;
1706 qemu_mutex_unlock(&mon_fdsets_lock
);
1711 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
1713 MonFdset
*mon_fdset
;
1714 MonFdsetFd
*mon_fdset_fd_dup
;
1716 qemu_mutex_lock(&mon_fdsets_lock
);
1717 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1718 if (mon_fdset
->id
!= fdset_id
) {
1721 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1722 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1726 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1727 mon_fdset_fd_dup
->fd
= dup_fd
;
1728 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1729 qemu_mutex_unlock(&mon_fdsets_lock
);
1734 qemu_mutex_unlock(&mon_fdsets_lock
);
1738 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1740 MonFdset
*mon_fdset
;
1741 MonFdsetFd
*mon_fdset_fd_dup
;
1743 qemu_mutex_lock(&mon_fdsets_lock
);
1744 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1745 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1746 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1748 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1749 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1750 monitor_fdset_cleanup(mon_fdset
);
1754 qemu_mutex_unlock(&mon_fdsets_lock
);
1755 return mon_fdset
->id
;
1762 qemu_mutex_unlock(&mon_fdsets_lock
);
1766 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1768 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1771 void monitor_fdset_dup_fd_remove(int dup_fd
)
1773 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1776 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1779 Error
*local_err
= NULL
;
1781 if (!qemu_isdigit(fdname
[0]) && mon
) {
1782 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1784 fd
= qemu_parse_fd(fdname
);
1786 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1791 error_propagate(errp
, local_err
);
1800 /* Please update hmp-commands.hx when adding or changing commands */
1801 static HMPCommand hmp_info_cmds
[] = {
1802 #include "hmp-commands-info.h"
1806 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1807 HMPCommand hmp_cmds
[] = {
1808 #include "hmp-commands.h"
1813 * Set @pval to the value in the register identified by @name.
1814 * return 0 if OK, -1 if not found
1816 int get_monitor_def(int64_t *pval
, const char *name
)
1818 const MonitorDef
*md
= target_monitor_defs();
1819 CPUState
*cs
= mon_get_cpu();
1824 if (cs
== NULL
|| md
== NULL
) {
1828 for(; md
->name
!= NULL
; md
++) {
1829 if (hmp_compare_cmd(name
, md
->name
)) {
1830 if (md
->get_value
) {
1831 *pval
= md
->get_value(md
, md
->offset
);
1833 CPUArchState
*env
= mon_get_cpu_env();
1834 ptr
= (uint8_t *)env
+ md
->offset
;
1837 *pval
= *(int32_t *)ptr
;
1840 *pval
= *(target_long
*)ptr
;
1851 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1853 *pval
= (target_long
) tmp
;
1859 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1862 if (!str
|| !option
) {
1865 if (!strncmp(option
, str
, strlen(str
))) {
1866 readline_add_completion(rs
, option
);
1870 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1873 ChardevBackendInfoList
*list
, *start
;
1879 readline_set_completion_index(rs
, len
);
1881 start
= list
= qmp_query_chardev_backends(NULL
);
1883 const char *chr_name
= list
->value
->name
;
1885 if (!strncmp(chr_name
, str
, len
)) {
1886 readline_add_completion(rs
, chr_name
);
1890 qapi_free_ChardevBackendInfoList(start
);
1893 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1902 readline_set_completion_index(rs
, len
);
1903 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1904 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1908 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1918 readline_set_completion_index(rs
, len
);
1919 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1922 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1924 name
= object_class_get_name(OBJECT_CLASS(dc
));
1926 if (dc
->user_creatable
1927 && !strncmp(name
, str
, len
)) {
1928 readline_add_completion(rs
, name
);
1935 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1945 readline_set_completion_index(rs
, len
);
1946 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1950 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1951 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1952 readline_add_completion(rs
, name
);
1959 static void peripheral_device_del_completion(ReadLineState
*rs
,
1960 const char *str
, size_t len
)
1962 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1963 GSList
*list
, *item
;
1965 list
= qdev_build_hotpluggable_device_list(peripheral
);
1970 for (item
= list
; item
; item
= g_slist_next(item
)) {
1971 DeviceState
*dev
= item
->data
;
1973 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1974 readline_add_completion(rs
, dev
->id
);
1981 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1984 ChardevInfoList
*list
, *start
;
1990 readline_set_completion_index(rs
, len
);
1992 start
= list
= qmp_query_chardev(NULL
);
1994 ChardevInfo
*chr
= list
->value
;
1996 if (!strncmp(chr
->label
, str
, len
)) {
1997 readline_add_completion(rs
, chr
->label
);
2001 qapi_free_ChardevInfoList(start
);
2004 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
2007 ChardevInfoList
*list
, *start
;
2010 readline_set_completion_index(rs
, len
);
2012 start
= list
= qmp_query_chardev(NULL
);
2014 ChardevInfo
*chr_info
= list
->value
;
2016 if (!strncmp(chr_info
->label
, str
, len
)) {
2017 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
2018 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
2019 readline_add_completion(rs
, chr_info
->label
);
2024 qapi_free_ChardevInfoList(start
);
2027 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2032 ringbuf_completion(rs
, str
);
2035 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2044 readline_set_completion_index(rs
, len
);
2045 peripheral_device_del_completion(rs
, str
, len
);
2048 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2050 ObjectPropertyInfoList
*list
, *start
;
2057 readline_set_completion_index(rs
, len
);
2059 start
= list
= qmp_qom_list("/objects", NULL
);
2061 ObjectPropertyInfo
*info
= list
->value
;
2063 if (!strncmp(info
->type
, "child<", 5)
2064 && !strncmp(info
->name
, str
, len
)) {
2065 readline_add_completion(rs
, info
->name
);
2069 qapi_free_ObjectPropertyInfoList(start
);
2072 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2081 sep
= strrchr(str
, '-');
2086 readline_set_completion_index(rs
, len
);
2087 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
2088 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
2089 readline_add_completion(rs
, QKeyCode_str(i
));
2094 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2099 readline_set_completion_index(rs
, len
);
2101 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2103 count
= qemu_find_net_clients_except(NULL
, ncs
,
2104 NET_CLIENT_DRIVER_NONE
,
2106 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2107 const char *name
= ncs
[i
]->name
;
2108 if (!strncmp(str
, name
, len
)) {
2109 readline_add_completion(rs
, name
);
2112 } else if (nb_args
== 3) {
2113 add_completion_option(rs
, str
, "on");
2114 add_completion_option(rs
, str
, "off");
2118 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2121 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2128 readline_set_completion_index(rs
, len
);
2129 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
2131 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2133 const char *name
= ncs
[i
]->name
;
2134 if (strncmp(str
, name
, len
)) {
2137 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
2139 readline_add_completion(rs
, name
);
2144 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2149 readline_set_completion_index(rs
, len
);
2151 TraceEventIter iter
;
2153 char *pattern
= g_strdup_printf("%s*", str
);
2154 trace_event_iter_init(&iter
, pattern
);
2155 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2156 readline_add_completion(rs
, trace_event_get_name(ev
));
2162 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2167 readline_set_completion_index(rs
, len
);
2169 TraceEventIter iter
;
2171 char *pattern
= g_strdup_printf("%s*", str
);
2172 trace_event_iter_init(&iter
, pattern
);
2173 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2174 readline_add_completion(rs
, trace_event_get_name(ev
));
2177 } else if (nb_args
== 3) {
2178 add_completion_option(rs
, str
, "on");
2179 add_completion_option(rs
, str
, "off");
2183 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2190 readline_set_completion_index(rs
, strlen(str
));
2191 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
2192 add_completion_option(rs
, str
, WatchdogAction_str(i
));
2196 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
2202 readline_set_completion_index(rs
, len
);
2205 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2206 const char *name
= MigrationCapability_str(i
);
2207 if (!strncmp(str
, name
, len
)) {
2208 readline_add_completion(rs
, name
);
2211 } else if (nb_args
== 3) {
2212 add_completion_option(rs
, str
, "on");
2213 add_completion_option(rs
, str
, "off");
2217 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
2223 readline_set_completion_index(rs
, len
);
2226 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
2227 const char *name
= MigrationParameter_str(i
);
2228 if (!strncmp(str
, name
, len
)) {
2229 readline_add_completion(rs
, name
);
2235 static void vm_completion(ReadLineState
*rs
, const char *str
)
2238 BlockDriverState
*bs
;
2239 BdrvNextIterator it
;
2242 readline_set_completion_index(rs
, len
);
2244 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2245 SnapshotInfoList
*snapshots
, *snapshot
;
2246 AioContext
*ctx
= bdrv_get_aio_context(bs
);
2249 aio_context_acquire(ctx
);
2250 if (bdrv_can_snapshot(bs
)) {
2251 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
2253 aio_context_release(ctx
);
2258 snapshot
= snapshots
;
2260 char *completion
= snapshot
->value
->name
;
2261 if (!strncmp(str
, completion
, len
)) {
2262 readline_add_completion(rs
, completion
);
2264 completion
= snapshot
->value
->id
;
2265 if (!strncmp(str
, completion
, len
)) {
2266 readline_add_completion(rs
, completion
);
2268 snapshot
= snapshot
->next
;
2270 qapi_free_SnapshotInfoList(snapshots
);
2275 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2278 vm_completion(rs
, str
);
2282 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2285 vm_completion(rs
, str
);
2290 compare_mon_cmd(const void *a
, const void *b
)
2292 return strcmp(((const HMPCommand
*)a
)->name
,
2293 ((const HMPCommand
*)b
)->name
);
2296 static void sortcmdlist(void)
2298 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
2301 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
2302 sizeof(*hmp_info_cmds
),
2306 void monitor_init_globals(void)
2308 monitor_init_globals_core();
2309 monitor_init_qmp_commands();
2311 qemu_mutex_init(&mon_fdsets_lock
);