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
)
397 CPUState
*cpu
= NULL
;
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
);
416 cpu_synchronize_state(cpu
);
421 CPUState
*mon_get_cpu(void)
423 return mon_get_cpu_sync(true);
426 CPUArchState
*mon_get_cpu_env(void)
428 CPUState
*cs
= mon_get_cpu();
430 return cs
? cs
->env_ptr
: NULL
;
433 int monitor_get_cpu_index(void)
435 CPUState
*cs
= mon_get_cpu_sync(false);
437 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
440 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
442 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
447 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
448 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
454 monitor_printf(mon
, "No CPU available\n");
458 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
463 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
465 if (!tcg_enabled()) {
466 error_report("JIT information is only available with accel=tcg");
474 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
480 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
482 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
483 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
484 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
485 enum QSPSortBy sort_by
;
487 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
488 qsp_report(max
, sort_by
, coalesce
);
491 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
493 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
502 str
= readline_get_history(hmp_mon
->rs
, i
);
506 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
511 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
513 CPUState
*cs
= mon_get_cpu();
516 monitor_printf(mon
, "No CPU available\n");
519 cpu_dump_statistics(cs
, 0);
522 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
524 const char *name
= qdict_get_try_str(qdict
, "name");
525 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
526 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
527 TraceEventInfoList
*events
;
528 TraceEventInfoList
*elem
;
529 Error
*local_err
= NULL
;
535 monitor_printf(mon
, "argument vcpu must be positive");
539 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
541 error_report_err(local_err
);
545 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
546 monitor_printf(mon
, "%s : state %u\n",
548 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
550 qapi_free_TraceEventInfoList(events
);
553 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
554 bool has_port
, int64_t port
,
555 bool has_tls_port
, int64_t tls_port
,
556 bool has_cert_subject
, const char *cert_subject
,
559 if (strcmp(protocol
, "spice") == 0) {
560 if (!qemu_using_spice(errp
)) {
564 if (!has_port
&& !has_tls_port
) {
565 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
569 if (qemu_spice_migrate_info(hostname
,
570 has_port
? port
: -1,
571 has_tls_port
? tls_port
: -1,
573 error_setg(errp
, QERR_UNDEFINED_ERROR
);
579 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
582 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
586 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
588 error_report_err(err
);
592 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
595 const char *items
= qdict_get_str(qdict
, "items");
597 if (!strcmp(items
, "none")) {
600 mask
= qemu_str_to_log_mask(items
);
602 help_cmd(mon
, "log");
609 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
611 const char *option
= qdict_get_try_str(qdict
, "option");
612 if (!option
|| !strcmp(option
, "on")) {
614 } else if (!strcmp(option
, "off")) {
617 monitor_printf(mon
, "unexpected option %s\n", option
);
621 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
623 const char *device
= qdict_get_try_str(qdict
, "device");
625 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
626 if (gdbserver_start(device
) < 0) {
627 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
629 } else if (strcmp(device
, "none") == 0) {
630 monitor_printf(mon
, "Disabled gdbserver\n");
632 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
637 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
639 const char *action
= qdict_get_str(qdict
, "action");
640 if (select_watchdog_action(action
) == -1) {
641 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
645 static void monitor_printc(Monitor
*mon
, int c
)
647 monitor_printf(mon
, "'");
650 monitor_printf(mon
, "\\'");
653 monitor_printf(mon
, "\\\\");
656 monitor_printf(mon
, "\\n");
659 monitor_printf(mon
, "\\r");
662 if (c
>= 32 && c
<= 126) {
663 monitor_printf(mon
, "%c", c
);
665 monitor_printf(mon
, "\\x%02x", c
);
669 monitor_printf(mon
, "'");
672 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
673 hwaddr addr
, int is_physical
)
675 int l
, line_size
, i
, max_digits
, len
;
678 CPUState
*cs
= mon_get_cpu();
680 if (!cs
&& (format
== 'i' || !is_physical
)) {
681 monitor_printf(mon
, "Can not dump without CPU\n");
686 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
699 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
703 max_digits
= (wsize
* 8) / 4;
707 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
716 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
718 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
723 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
724 MemTxResult r
= address_space_read(as
, addr
,
725 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
727 monitor_printf(mon
, " Cannot access memory\n");
731 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
732 monitor_printf(mon
, " Cannot access memory\n");
747 v
= (uint32_t)ldl_p(buf
+ i
);
753 monitor_printf(mon
, " ");
756 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
759 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
762 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
765 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
768 monitor_printc(mon
, v
);
773 monitor_printf(mon
, "\n");
779 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
781 int count
= qdict_get_int(qdict
, "count");
782 int format
= qdict_get_int(qdict
, "format");
783 int size
= qdict_get_int(qdict
, "size");
784 target_long addr
= qdict_get_int(qdict
, "addr");
786 memory_dump(mon
, count
, format
, size
, addr
, 0);
789 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
791 int count
= qdict_get_int(qdict
, "count");
792 int format
= qdict_get_int(qdict
, "format");
793 int size
= qdict_get_int(qdict
, "size");
794 hwaddr addr
= qdict_get_int(qdict
, "addr");
796 memory_dump(mon
, count
, format
, size
, addr
, 1);
799 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
801 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
805 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
809 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
810 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
811 memory_region_unref(mrs
.mr
);
816 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
819 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
821 hwaddr addr
= qdict_get_int(qdict
, "addr");
822 Error
*local_err
= NULL
;
823 MemoryRegion
*mr
= NULL
;
826 ptr
= gpa2hva(&mr
, addr
, &local_err
);
828 error_report_err(local_err
);
832 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
834 addr
, mr
->name
, ptr
);
836 memory_region_unref(mr
);
839 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
841 target_ulong addr
= qdict_get_int(qdict
, "addr");
843 CPUState
*cs
= mon_get_cpu();
847 monitor_printf(mon
, "No cpu\n");
851 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
853 monitor_printf(mon
, "Unmapped\n");
855 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
856 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
861 static uint64_t vtop(void *ptr
, Error
**errp
)
865 uintptr_t addr
= (uintptr_t) ptr
;
866 uintptr_t pagesize
= qemu_real_host_page_size
;
867 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
870 fd
= open("/proc/self/pagemap", O_RDONLY
);
872 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
876 /* Force copy-on-write if necessary. */
877 atomic_add((uint8_t *)ptr
, 0);
879 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
880 error_setg_errno(errp
, errno
, "Cannot read pagemap");
883 if ((pinfo
& (1ull << 63)) == 0) {
884 error_setg(errp
, "Page not present");
887 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
894 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
896 hwaddr addr
= qdict_get_int(qdict
, "addr");
897 Error
*local_err
= NULL
;
898 MemoryRegion
*mr
= NULL
;
902 ptr
= gpa2hva(&mr
, addr
, &local_err
);
904 error_report_err(local_err
);
908 physaddr
= vtop(ptr
, &local_err
);
910 error_report_err(local_err
);
912 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
913 " (%s) is 0x%" PRIx64
"\n",
914 addr
, mr
->name
, (uint64_t) physaddr
);
917 memory_region_unref(mr
);
921 static void do_print(Monitor
*mon
, const QDict
*qdict
)
923 int format
= qdict_get_int(qdict
, "format");
924 hwaddr val
= qdict_get_int(qdict
, "val");
928 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
931 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
934 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
938 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
941 monitor_printc(mon
, val
);
944 monitor_printf(mon
, "\n");
947 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
951 uint32_t start
= qdict_get_int(qdict
, "start");
952 uint32_t size
= qdict_get_int(qdict
, "size");
955 for(addr
= start
; addr
< (start
+ size
); addr
++) {
956 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
957 MEMTXATTRS_UNSPECIFIED
, NULL
);
958 /* BSD sum algorithm ('sum' Unix command) */
959 sum
= (sum
>> 1) | (sum
<< 15);
962 monitor_printf(mon
, "%05d\n", sum
);
965 static int mouse_button_state
;
967 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
969 int dx
, dy
, dz
, button
;
970 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
971 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
972 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
974 dx
= strtol(dx_str
, NULL
, 0);
975 dy
= strtol(dy_str
, NULL
, 0);
976 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
977 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
980 dz
= strtol(dz_str
, NULL
, 0);
982 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
983 qemu_input_queue_btn(NULL
, button
, true);
984 qemu_input_event_sync();
985 qemu_input_queue_btn(NULL
, button
, false);
988 qemu_input_event_sync();
991 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
993 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
994 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
995 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
996 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
998 int button_state
= qdict_get_int(qdict
, "button_state");
1000 if (mouse_button_state
== button_state
) {
1003 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1004 qemu_input_event_sync();
1005 mouse_button_state
= button_state
;
1008 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1010 int size
= qdict_get_int(qdict
, "size");
1011 int addr
= qdict_get_int(qdict
, "addr");
1012 int has_index
= qdict_haskey(qdict
, "index");
1017 int index
= qdict_get_int(qdict
, "index");
1018 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1026 val
= cpu_inb(addr
);
1030 val
= cpu_inw(addr
);
1034 val
= cpu_inl(addr
);
1038 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1039 suffix
, addr
, size
* 2, val
);
1042 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1044 int size
= qdict_get_int(qdict
, "size");
1045 int addr
= qdict_get_int(qdict
, "addr");
1046 int val
= qdict_get_int(qdict
, "val");
1048 addr
&= IOPORTS_MASK
;
1053 cpu_outb(addr
, val
);
1056 cpu_outw(addr
, val
);
1059 cpu_outl(addr
, val
);
1064 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1066 Error
*local_err
= NULL
;
1067 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1069 qemu_boot_set(bootdevice
, &local_err
);
1071 error_report_err(local_err
);
1073 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1077 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1079 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1080 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1081 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1083 mtree_info(flatview
, dispatch_tree
, owner
);
1086 #ifdef CONFIG_PROFILER
1090 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1092 static int64_t last_cpu_exec_time
;
1093 int64_t cpu_exec_time
;
1096 cpu_exec_time
= tcg_cpu_exec_time();
1097 delta
= cpu_exec_time
- last_cpu_exec_time
;
1099 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1100 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1101 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1102 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1103 last_cpu_exec_time
= cpu_exec_time
;
1107 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1109 monitor_printf(mon
, "Internal profiler not compiled\n");
1113 /* Capture support */
1114 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1116 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1121 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1122 monitor_printf(mon
, "[%d]: ", i
);
1123 s
->ops
.info (s
->opaque
);
1127 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1130 int n
= qdict_get_int(qdict
, "n");
1133 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1135 s
->ops
.destroy (s
->opaque
);
1136 QLIST_REMOVE (s
, entries
);
1143 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1145 const char *path
= qdict_get_str(qdict
, "path");
1146 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1147 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1148 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1149 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1151 AudioState
*as
= audio_state_by_name(audiodev
);
1154 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1158 s
= g_malloc0 (sizeof (*s
));
1160 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1161 monitor_printf(mon
, "Failed to add wave capture\n");
1165 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1168 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1173 container
= object_get_objects_root();
1174 obj
= object_resolve_path_component(container
, name
);
1176 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1180 return QAUTHZ_LIST(obj
);
1183 static bool warn_acl
;
1184 static void hmp_warn_acl(void)
1189 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1190 "commands are deprecated with no replacement. Authorization "
1191 "for VNC should be performed using the pluggable QAuthZ "
1196 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1198 const char *aclname
= qdict_get_str(qdict
, "aclname");
1199 QAuthZList
*auth
= find_auth(mon
, aclname
);
1200 QAuthZListRuleList
*rules
;
1209 monitor_printf(mon
, "policy: %s\n",
1210 QAuthZListPolicy_str(auth
->policy
));
1212 rules
= auth
->rules
;
1214 QAuthZListRule
*rule
= rules
->value
;
1216 monitor_printf(mon
, "%zu: %s %s\n", i
,
1217 QAuthZListPolicy_str(rule
->policy
),
1219 rules
= rules
->next
;
1223 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1225 const char *aclname
= qdict_get_str(qdict
, "aclname");
1226 QAuthZList
*auth
= find_auth(mon
, aclname
);
1234 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1235 qapi_free_QAuthZListRuleList(auth
->rules
);
1237 monitor_printf(mon
, "acl: removed all rules\n");
1240 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1242 const char *aclname
= qdict_get_str(qdict
, "aclname");
1243 const char *policy
= qdict_get_str(qdict
, "policy");
1244 QAuthZList
*auth
= find_auth(mon
, aclname
);
1254 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1256 QAUTHZ_LIST_POLICY_DENY
,
1260 monitor_printf(mon
, "acl: unknown policy '%s', "
1261 "expected 'deny' or 'allow'\n", policy
);
1264 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1265 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1267 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1272 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1274 if (strchr(match
, '*')) {
1275 return QAUTHZ_LIST_FORMAT_GLOB
;
1277 return QAUTHZ_LIST_FORMAT_EXACT
;
1281 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1283 const char *aclname
= qdict_get_str(qdict
, "aclname");
1284 const char *match
= qdict_get_str(qdict
, "match");
1285 const char *policystr
= qdict_get_str(qdict
, "policy");
1286 int has_index
= qdict_haskey(qdict
, "index");
1287 int index
= qdict_get_try_int(qdict
, "index", -1);
1288 QAuthZList
*auth
= find_auth(mon
, aclname
);
1290 QAuthZListPolicy policy
;
1291 QAuthZListFormat format
;
1300 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1302 QAUTHZ_LIST_POLICY_DENY
,
1306 monitor_printf(mon
, "acl: unknown policy '%s', "
1307 "expected 'deny' or 'allow'\n", policystr
);
1311 format
= hmp_acl_get_format(match
);
1313 if (has_index
&& index
== 0) {
1314 monitor_printf(mon
, "acl: unable to add acl entry\n");
1319 i
= qauthz_list_insert_rule(auth
, match
, policy
,
1320 format
, index
- 1, &err
);
1322 i
= qauthz_list_append_rule(auth
, match
, policy
,
1326 monitor_printf(mon
, "acl: unable to add rule: %s",
1327 error_get_pretty(err
));
1330 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
1334 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1336 const char *aclname
= qdict_get_str(qdict
, "aclname");
1337 const char *match
= qdict_get_str(qdict
, "match");
1338 QAuthZList
*auth
= find_auth(mon
, aclname
);
1347 i
= qauthz_list_delete_rule(auth
, match
);
1349 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
1351 monitor_printf(mon
, "acl: no matching acl entry\n");
1355 void qmp_getfd(const char *fdname
, Error
**errp
)
1360 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1362 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1366 if (qemu_isdigit(fdname
[0])) {
1368 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1369 "a name not starting with a digit");
1373 qemu_mutex_lock(&cur_mon
->mon_lock
);
1374 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1375 if (strcmp(monfd
->name
, fdname
) != 0) {
1381 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1382 /* Make sure close() is outside critical section */
1387 monfd
= g_malloc0(sizeof(mon_fd_t
));
1388 monfd
->name
= g_strdup(fdname
);
1391 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1392 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1395 void qmp_closefd(const char *fdname
, Error
**errp
)
1400 qemu_mutex_lock(&cur_mon
->mon_lock
);
1401 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1402 if (strcmp(monfd
->name
, fdname
) != 0) {
1406 QLIST_REMOVE(monfd
, next
);
1408 g_free(monfd
->name
);
1410 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1411 /* Make sure close() is outside critical section */
1416 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1417 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1420 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1424 qemu_mutex_lock(&mon
->mon_lock
);
1425 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1428 if (strcmp(monfd
->name
, fdname
) != 0) {
1434 /* caller takes ownership of fd */
1435 QLIST_REMOVE(monfd
, next
);
1436 g_free(monfd
->name
);
1438 qemu_mutex_unlock(&mon
->mon_lock
);
1443 qemu_mutex_unlock(&mon
->mon_lock
);
1444 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1448 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1450 MonFdsetFd
*mon_fdset_fd
;
1451 MonFdsetFd
*mon_fdset_fd_next
;
1453 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1454 if ((mon_fdset_fd
->removed
||
1455 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1456 runstate_is_running()) {
1457 close(mon_fdset_fd
->fd
);
1458 g_free(mon_fdset_fd
->opaque
);
1459 QLIST_REMOVE(mon_fdset_fd
, next
);
1460 g_free(mon_fdset_fd
);
1464 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1465 QLIST_REMOVE(mon_fdset
, next
);
1470 void monitor_fdsets_cleanup(void)
1472 MonFdset
*mon_fdset
;
1473 MonFdset
*mon_fdset_next
;
1475 qemu_mutex_lock(&mon_fdsets_lock
);
1476 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1477 monitor_fdset_cleanup(mon_fdset
);
1479 qemu_mutex_unlock(&mon_fdsets_lock
);
1482 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1483 const char *opaque
, Error
**errp
)
1486 Monitor
*mon
= cur_mon
;
1489 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1491 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1495 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1496 has_opaque
, opaque
, errp
);
1508 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1510 MonFdset
*mon_fdset
;
1511 MonFdsetFd
*mon_fdset_fd
;
1514 qemu_mutex_lock(&mon_fdsets_lock
);
1515 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1516 if (mon_fdset
->id
!= fdset_id
) {
1519 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1521 if (mon_fdset_fd
->fd
!= fd
) {
1524 mon_fdset_fd
->removed
= true;
1527 mon_fdset_fd
->removed
= true;
1530 if (has_fd
&& !mon_fdset_fd
) {
1533 monitor_fdset_cleanup(mon_fdset
);
1534 qemu_mutex_unlock(&mon_fdsets_lock
);
1539 qemu_mutex_unlock(&mon_fdsets_lock
);
1541 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1544 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1546 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1549 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1551 MonFdset
*mon_fdset
;
1552 MonFdsetFd
*mon_fdset_fd
;
1553 FdsetInfoList
*fdset_list
= NULL
;
1555 qemu_mutex_lock(&mon_fdsets_lock
);
1556 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1557 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1558 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1560 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1561 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1563 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1564 FdsetFdInfoList
*fdsetfd_info
;
1566 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1567 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1568 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1569 if (mon_fdset_fd
->opaque
) {
1570 fdsetfd_info
->value
->has_opaque
= true;
1571 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1573 fdsetfd_info
->value
->has_opaque
= false;
1576 fdsetfd_info
->next
= fdsetfd_list
;
1577 fdsetfd_list
= fdsetfd_info
;
1580 fdset_info
->value
->fds
= fdsetfd_list
;
1582 fdset_info
->next
= fdset_list
;
1583 fdset_list
= fdset_info
;
1585 qemu_mutex_unlock(&mon_fdsets_lock
);
1590 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1591 bool has_opaque
, const char *opaque
,
1594 MonFdset
*mon_fdset
= NULL
;
1595 MonFdsetFd
*mon_fdset_fd
;
1598 qemu_mutex_lock(&mon_fdsets_lock
);
1600 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1601 /* Break if match found or match impossible due to ordering by ID */
1602 if (fdset_id
<= mon_fdset
->id
) {
1603 if (fdset_id
< mon_fdset
->id
) {
1611 if (mon_fdset
== NULL
) {
1612 int64_t fdset_id_prev
= -1;
1613 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1617 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1618 "a non-negative value");
1619 qemu_mutex_unlock(&mon_fdsets_lock
);
1622 /* Use specified fdset ID */
1623 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1624 mon_fdset_cur
= mon_fdset
;
1625 if (fdset_id
< mon_fdset_cur
->id
) {
1630 /* Use first available fdset ID */
1631 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1632 mon_fdset_cur
= mon_fdset
;
1633 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1634 fdset_id_prev
= mon_fdset_cur
->id
;
1641 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1643 mon_fdset
->id
= fdset_id
;
1645 mon_fdset
->id
= fdset_id_prev
+ 1;
1648 /* The fdset list is ordered by fdset ID */
1649 if (!mon_fdset_cur
) {
1650 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1651 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1652 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1654 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1658 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1659 mon_fdset_fd
->fd
= fd
;
1660 mon_fdset_fd
->removed
= false;
1662 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1664 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1666 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1667 fdinfo
->fdset_id
= mon_fdset
->id
;
1668 fdinfo
->fd
= mon_fdset_fd
->fd
;
1670 qemu_mutex_unlock(&mon_fdsets_lock
);
1674 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1679 MonFdset
*mon_fdset
;
1680 MonFdsetFd
*mon_fdset_fd
;
1684 qemu_mutex_lock(&mon_fdsets_lock
);
1685 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1686 if (mon_fdset
->id
!= fdset_id
) {
1689 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1690 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1691 if (mon_fd_flags
== -1) {
1696 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1697 ret
= mon_fdset_fd
->fd
;
1707 qemu_mutex_unlock(&mon_fdsets_lock
);
1712 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
1714 MonFdset
*mon_fdset
;
1715 MonFdsetFd
*mon_fdset_fd_dup
;
1717 qemu_mutex_lock(&mon_fdsets_lock
);
1718 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1719 if (mon_fdset
->id
!= fdset_id
) {
1722 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1723 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1727 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1728 mon_fdset_fd_dup
->fd
= dup_fd
;
1729 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1730 qemu_mutex_unlock(&mon_fdsets_lock
);
1735 qemu_mutex_unlock(&mon_fdsets_lock
);
1739 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1741 MonFdset
*mon_fdset
;
1742 MonFdsetFd
*mon_fdset_fd_dup
;
1744 qemu_mutex_lock(&mon_fdsets_lock
);
1745 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1746 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1747 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1749 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1750 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1751 monitor_fdset_cleanup(mon_fdset
);
1755 qemu_mutex_unlock(&mon_fdsets_lock
);
1756 return mon_fdset
->id
;
1763 qemu_mutex_unlock(&mon_fdsets_lock
);
1767 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1769 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1772 void monitor_fdset_dup_fd_remove(int dup_fd
)
1774 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1777 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1780 Error
*local_err
= NULL
;
1782 if (!qemu_isdigit(fdname
[0]) && mon
) {
1783 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1785 fd
= qemu_parse_fd(fdname
);
1787 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1792 error_propagate(errp
, local_err
);
1801 /* Please update hmp-commands.hx when adding or changing commands */
1802 static HMPCommand hmp_info_cmds
[] = {
1803 #include "hmp-commands-info.h"
1807 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1808 HMPCommand hmp_cmds
[] = {
1809 #include "hmp-commands.h"
1814 * Set @pval to the value in the register identified by @name.
1815 * return 0 if OK, -1 if not found
1817 int get_monitor_def(int64_t *pval
, const char *name
)
1819 const MonitorDef
*md
= target_monitor_defs();
1820 CPUState
*cs
= mon_get_cpu();
1825 if (cs
== NULL
|| md
== NULL
) {
1829 for(; md
->name
!= NULL
; md
++) {
1830 if (hmp_compare_cmd(name
, md
->name
)) {
1831 if (md
->get_value
) {
1832 *pval
= md
->get_value(md
, md
->offset
);
1834 CPUArchState
*env
= mon_get_cpu_env();
1835 ptr
= (uint8_t *)env
+ md
->offset
;
1838 *pval
= *(int32_t *)ptr
;
1841 *pval
= *(target_long
*)ptr
;
1852 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1854 *pval
= (target_long
) tmp
;
1860 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1863 if (!str
|| !option
) {
1866 if (!strncmp(option
, str
, strlen(str
))) {
1867 readline_add_completion(rs
, option
);
1871 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1874 ChardevBackendInfoList
*list
, *start
;
1880 readline_set_completion_index(rs
, len
);
1882 start
= list
= qmp_query_chardev_backends(NULL
);
1884 const char *chr_name
= list
->value
->name
;
1886 if (!strncmp(chr_name
, str
, len
)) {
1887 readline_add_completion(rs
, chr_name
);
1891 qapi_free_ChardevBackendInfoList(start
);
1894 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1903 readline_set_completion_index(rs
, len
);
1904 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1905 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1909 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1919 readline_set_completion_index(rs
, len
);
1920 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1923 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1925 name
= object_class_get_name(OBJECT_CLASS(dc
));
1927 if (dc
->user_creatable
1928 && !strncmp(name
, str
, len
)) {
1929 readline_add_completion(rs
, name
);
1936 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1946 readline_set_completion_index(rs
, len
);
1947 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1951 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1952 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1953 readline_add_completion(rs
, name
);
1960 static void peripheral_device_del_completion(ReadLineState
*rs
,
1961 const char *str
, size_t len
)
1963 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1964 GSList
*list
, *item
;
1966 list
= qdev_build_hotpluggable_device_list(peripheral
);
1971 for (item
= list
; item
; item
= g_slist_next(item
)) {
1972 DeviceState
*dev
= item
->data
;
1974 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1975 readline_add_completion(rs
, dev
->id
);
1982 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1985 ChardevInfoList
*list
, *start
;
1991 readline_set_completion_index(rs
, len
);
1993 start
= list
= qmp_query_chardev(NULL
);
1995 ChardevInfo
*chr
= list
->value
;
1997 if (!strncmp(chr
->label
, str
, len
)) {
1998 readline_add_completion(rs
, chr
->label
);
2002 qapi_free_ChardevInfoList(start
);
2005 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
2008 ChardevInfoList
*list
, *start
;
2011 readline_set_completion_index(rs
, len
);
2013 start
= list
= qmp_query_chardev(NULL
);
2015 ChardevInfo
*chr_info
= list
->value
;
2017 if (!strncmp(chr_info
->label
, str
, len
)) {
2018 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
2019 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
2020 readline_add_completion(rs
, chr_info
->label
);
2025 qapi_free_ChardevInfoList(start
);
2028 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2033 ringbuf_completion(rs
, str
);
2036 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2045 readline_set_completion_index(rs
, len
);
2046 peripheral_device_del_completion(rs
, str
, len
);
2049 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2051 ObjectPropertyInfoList
*list
, *start
;
2058 readline_set_completion_index(rs
, len
);
2060 start
= list
= qmp_qom_list("/objects", NULL
);
2062 ObjectPropertyInfo
*info
= list
->value
;
2064 if (!strncmp(info
->type
, "child<", 5)
2065 && !strncmp(info
->name
, str
, len
)) {
2066 readline_add_completion(rs
, info
->name
);
2070 qapi_free_ObjectPropertyInfoList(start
);
2073 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2082 sep
= strrchr(str
, '-');
2087 readline_set_completion_index(rs
, len
);
2088 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
2089 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
2090 readline_add_completion(rs
, QKeyCode_str(i
));
2095 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2100 readline_set_completion_index(rs
, len
);
2102 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2104 count
= qemu_find_net_clients_except(NULL
, ncs
,
2105 NET_CLIENT_DRIVER_NONE
,
2107 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2108 const char *name
= ncs
[i
]->name
;
2109 if (!strncmp(str
, name
, len
)) {
2110 readline_add_completion(rs
, name
);
2113 } else if (nb_args
== 3) {
2114 add_completion_option(rs
, str
, "on");
2115 add_completion_option(rs
, str
, "off");
2119 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2122 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2129 readline_set_completion_index(rs
, len
);
2130 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
2132 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2134 const char *name
= ncs
[i
]->name
;
2135 if (strncmp(str
, name
, len
)) {
2138 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
2140 readline_add_completion(rs
, name
);
2145 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2150 readline_set_completion_index(rs
, len
);
2152 TraceEventIter iter
;
2154 char *pattern
= g_strdup_printf("%s*", str
);
2155 trace_event_iter_init(&iter
, pattern
);
2156 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2157 readline_add_completion(rs
, trace_event_get_name(ev
));
2163 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2168 readline_set_completion_index(rs
, len
);
2170 TraceEventIter iter
;
2172 char *pattern
= g_strdup_printf("%s*", str
);
2173 trace_event_iter_init(&iter
, pattern
);
2174 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2175 readline_add_completion(rs
, trace_event_get_name(ev
));
2178 } else if (nb_args
== 3) {
2179 add_completion_option(rs
, str
, "on");
2180 add_completion_option(rs
, str
, "off");
2184 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2191 readline_set_completion_index(rs
, strlen(str
));
2192 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
2193 add_completion_option(rs
, str
, WatchdogAction_str(i
));
2197 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
2203 readline_set_completion_index(rs
, len
);
2206 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2207 const char *name
= MigrationCapability_str(i
);
2208 if (!strncmp(str
, name
, len
)) {
2209 readline_add_completion(rs
, name
);
2212 } else if (nb_args
== 3) {
2213 add_completion_option(rs
, str
, "on");
2214 add_completion_option(rs
, str
, "off");
2218 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
2224 readline_set_completion_index(rs
, len
);
2227 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
2228 const char *name
= MigrationParameter_str(i
);
2229 if (!strncmp(str
, name
, len
)) {
2230 readline_add_completion(rs
, name
);
2236 static void vm_completion(ReadLineState
*rs
, const char *str
)
2239 BlockDriverState
*bs
;
2240 BdrvNextIterator it
;
2243 readline_set_completion_index(rs
, len
);
2245 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2246 SnapshotInfoList
*snapshots
, *snapshot
;
2247 AioContext
*ctx
= bdrv_get_aio_context(bs
);
2250 aio_context_acquire(ctx
);
2251 if (bdrv_can_snapshot(bs
)) {
2252 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
2254 aio_context_release(ctx
);
2259 snapshot
= snapshots
;
2261 char *completion
= snapshot
->value
->name
;
2262 if (!strncmp(str
, completion
, len
)) {
2263 readline_add_completion(rs
, completion
);
2265 completion
= snapshot
->value
->id
;
2266 if (!strncmp(str
, completion
, len
)) {
2267 readline_add_completion(rs
, completion
);
2269 snapshot
= snapshot
->next
;
2271 qapi_free_SnapshotInfoList(snapshots
);
2276 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2279 vm_completion(rs
, str
);
2283 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2286 vm_completion(rs
, str
);
2291 compare_mon_cmd(const void *a
, const void *b
)
2293 return strcmp(((const HMPCommand
*)a
)->name
,
2294 ((const HMPCommand
*)b
)->name
);
2297 static void sortcmdlist(void)
2299 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
2302 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
2303 sizeof(*hmp_info_cmds
),
2307 void monitor_init_globals(void)
2309 monitor_init_globals_core();
2310 monitor_init_qmp_commands();
2312 qemu_mutex_init(&mon_fdsets_lock
);