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 /* Protects mon_fdsets */
110 static QemuMutex mon_fdsets_lock
;
111 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
113 static HMPCommand hmp_info_cmds
[];
115 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
116 int64_t cpu_index
, Error
**errp
)
122 monitor_data_init(&hmp
.common
, false, true, false);
125 cur_mon
= &hmp
.common
;
128 int ret
= monitor_set_cpu(cpu_index
);
131 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
137 handle_hmp_command(&hmp
, command_line
);
140 qemu_mutex_lock(&hmp
.common
.mon_lock
);
141 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
142 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
144 output
= g_strdup("");
146 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
149 monitor_data_destroy(&hmp
.common
);
154 * Is @name in the '|' separated list of names @list?
156 int hmp_compare_cmd(const char *name
, const char *list
)
158 const char *p
, *pstart
;
164 p
= qemu_strchrnul(p
, '|');
165 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
176 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
178 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
181 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
183 const char *tp_name
= qdict_get_str(qdict
, "name");
184 bool new_state
= qdict_get_bool(qdict
, "option");
185 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
186 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
187 Error
*local_err
= NULL
;
190 monitor_printf(mon
, "argument vcpu must be positive");
194 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
196 error_report_err(local_err
);
200 #ifdef CONFIG_TRACE_SIMPLE
201 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
203 const char *op
= qdict_get_try_str(qdict
, "op");
204 const char *arg
= qdict_get_try_str(qdict
, "arg");
207 st_print_trace_file_status();
208 } else if (!strcmp(op
, "on")) {
209 st_set_trace_file_enabled(true);
210 } else if (!strcmp(op
, "off")) {
211 st_set_trace_file_enabled(false);
212 } else if (!strcmp(op
, "flush")) {
213 st_flush_trace_buffer();
214 } else if (!strcmp(op
, "set")) {
216 st_set_trace_file(arg
);
219 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
220 help_cmd(mon
, "trace-file");
225 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
227 help_cmd(mon
, "info");
230 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
232 CommandInfoList
*info
, **list
= opaque
;
238 info
= g_malloc0(sizeof(*info
));
239 info
->value
= g_malloc0(sizeof(*info
->value
));
240 info
->value
->name
= g_strdup(cmd
->name
);
245 CommandInfoList
*qmp_query_commands(Error
**errp
)
247 CommandInfoList
*list
= NULL
;
250 assert(monitor_is_qmp(cur_mon
));
251 mon
= container_of(cur_mon
, MonitorQMP
, common
);
253 qmp_for_each_command(mon
->commands
, query_commands_cb
, &list
);
258 EventInfoList
*qmp_query_events(Error
**errp
)
261 * TODO This deprecated command is the only user of
262 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
263 * they should go, too.
265 EventInfoList
*info
, *ev_list
= NULL
;
268 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
269 const char *event_name
= QAPIEvent_str(e
);
270 assert(event_name
!= NULL
);
271 info
= g_malloc0(sizeof(*info
));
272 info
->value
= g_malloc0(sizeof(*info
->value
));
273 info
->value
->name
= g_strdup(event_name
);
275 info
->next
= ev_list
;
283 * Minor hack: generated marshalling suppressed for this command
284 * ('gen': false in the schema) so we can parse the JSON string
285 * directly into QObject instead of first parsing it with
286 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
287 * to QObject with generated output marshallers, every time. Instead,
288 * we do it in test-qobject-input-visitor.c, just to make sure
289 * qapi-gen.py's output actually conforms to the schema.
291 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
294 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
297 static void monitor_init_qmp_commands(void)
301 * - qmp_commands contains all QMP commands
302 * - qmp_cap_negotiation_commands contains just
303 * "qmp_capabilities", to enforce capability negotiation
306 qmp_init_marshal(&qmp_commands
);
308 qmp_register_command(&qmp_commands
, "query-qmp-schema",
309 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
310 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
312 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
315 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
316 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
317 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
321 * Accept QMP capabilities in @list for @mon.
322 * On success, set mon->qmp.capab[], and return true.
323 * On error, set @errp, and return false.
325 static bool qmp_caps_accept(MonitorQMP
*mon
, QMPCapabilityList
*list
,
328 GString
*unavailable
= NULL
;
329 bool capab
[QMP_CAPABILITY__MAX
];
331 memset(capab
, 0, sizeof(capab
));
333 for (; list
; list
= list
->next
) {
334 if (!mon
->capab_offered
[list
->value
]) {
336 unavailable
= g_string_new(QMPCapability_str(list
->value
));
338 g_string_append_printf(unavailable
, ", %s",
339 QMPCapability_str(list
->value
));
342 capab
[list
->value
] = true;
346 error_setg(errp
, "Capability %s not available", unavailable
->str
);
347 g_string_free(unavailable
, true);
351 memcpy(mon
->capab
, capab
, sizeof(capab
));
355 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
360 assert(monitor_is_qmp(cur_mon
));
361 mon
= container_of(cur_mon
, MonitorQMP
, common
);
363 if (mon
->commands
== &qmp_commands
) {
364 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
365 "Capabilities negotiation is already complete, command "
370 if (!qmp_caps_accept(mon
, enable
, errp
)) {
374 mon
->commands
= &qmp_commands
;
377 /* Set the current CPU defined by the user. Callers must hold BQL. */
378 int monitor_set_cpu(int cpu_index
)
382 cpu
= qemu_get_cpu(cpu_index
);
386 g_free(cur_mon
->mon_cpu_path
);
387 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
391 /* Callers must hold BQL. */
392 static CPUState
*mon_get_cpu_sync(bool synchronize
)
394 CPUState
*cpu
= NULL
;
396 if (cur_mon
->mon_cpu_path
) {
397 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
400 g_free(cur_mon
->mon_cpu_path
);
401 cur_mon
->mon_cpu_path
= NULL
;
404 if (!cur_mon
->mon_cpu_path
) {
408 monitor_set_cpu(first_cpu
->cpu_index
);
413 cpu_synchronize_state(cpu
);
418 CPUState
*mon_get_cpu(void)
420 return mon_get_cpu_sync(true);
423 CPUArchState
*mon_get_cpu_env(void)
425 CPUState
*cs
= mon_get_cpu();
427 return cs
? cs
->env_ptr
: NULL
;
430 int monitor_get_cpu_index(void)
432 CPUState
*cs
= mon_get_cpu_sync(false);
434 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
437 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
439 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
444 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
445 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
451 monitor_printf(mon
, "No CPU available\n");
455 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
460 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
462 if (!tcg_enabled()) {
463 error_report("JIT information is only available with accel=tcg");
471 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
477 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
479 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
480 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
481 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
482 enum QSPSortBy sort_by
;
484 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
485 qsp_report(max
, sort_by
, coalesce
);
488 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
490 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
499 str
= readline_get_history(hmp_mon
->rs
, i
);
503 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
508 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
510 CPUState
*cs
= mon_get_cpu();
513 monitor_printf(mon
, "No CPU available\n");
516 cpu_dump_statistics(cs
, 0);
519 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
521 const char *name
= qdict_get_try_str(qdict
, "name");
522 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
523 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
524 TraceEventInfoList
*events
;
525 TraceEventInfoList
*elem
;
526 Error
*local_err
= NULL
;
532 monitor_printf(mon
, "argument vcpu must be positive");
536 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
538 error_report_err(local_err
);
542 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
543 monitor_printf(mon
, "%s : state %u\n",
545 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
547 qapi_free_TraceEventInfoList(events
);
550 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
551 bool has_port
, int64_t port
,
552 bool has_tls_port
, int64_t tls_port
,
553 bool has_cert_subject
, const char *cert_subject
,
556 if (strcmp(protocol
, "spice") == 0) {
557 if (!qemu_using_spice(errp
)) {
561 if (!has_port
&& !has_tls_port
) {
562 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
566 if (qemu_spice_migrate_info(hostname
,
567 has_port
? port
: -1,
568 has_tls_port
? tls_port
: -1,
570 error_setg(errp
, QERR_UNDEFINED_ERROR
);
576 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
579 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
583 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
585 error_report_err(err
);
589 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
592 const char *items
= qdict_get_str(qdict
, "items");
594 if (!strcmp(items
, "none")) {
597 mask
= qemu_str_to_log_mask(items
);
599 help_cmd(mon
, "log");
606 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
608 const char *option
= qdict_get_try_str(qdict
, "option");
609 if (!option
|| !strcmp(option
, "on")) {
611 } else if (!strcmp(option
, "off")) {
614 monitor_printf(mon
, "unexpected option %s\n", option
);
618 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
620 const char *device
= qdict_get_try_str(qdict
, "device");
622 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
623 if (gdbserver_start(device
) < 0) {
624 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
626 } else if (strcmp(device
, "none") == 0) {
627 monitor_printf(mon
, "Disabled gdbserver\n");
629 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
634 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
636 const char *action
= qdict_get_str(qdict
, "action");
637 if (select_watchdog_action(action
) == -1) {
638 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
642 static void monitor_printc(Monitor
*mon
, int c
)
644 monitor_printf(mon
, "'");
647 monitor_printf(mon
, "\\'");
650 monitor_printf(mon
, "\\\\");
653 monitor_printf(mon
, "\\n");
656 monitor_printf(mon
, "\\r");
659 if (c
>= 32 && c
<= 126) {
660 monitor_printf(mon
, "%c", c
);
662 monitor_printf(mon
, "\\x%02x", c
);
666 monitor_printf(mon
, "'");
669 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
670 hwaddr addr
, int is_physical
)
672 int l
, line_size
, i
, max_digits
, len
;
675 CPUState
*cs
= mon_get_cpu();
677 if (!cs
&& (format
== 'i' || !is_physical
)) {
678 monitor_printf(mon
, "Can not dump without CPU\n");
683 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
696 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
700 max_digits
= (wsize
* 8) / 4;
704 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
713 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
715 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
720 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
721 MemTxResult r
= address_space_read(as
, addr
,
722 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
724 monitor_printf(mon
, " Cannot access memory\n");
728 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
729 monitor_printf(mon
, " Cannot access memory\n");
744 v
= (uint32_t)ldl_p(buf
+ i
);
750 monitor_printf(mon
, " ");
753 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
756 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
759 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
762 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
765 monitor_printc(mon
, v
);
770 monitor_printf(mon
, "\n");
776 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
778 int count
= qdict_get_int(qdict
, "count");
779 int format
= qdict_get_int(qdict
, "format");
780 int size
= qdict_get_int(qdict
, "size");
781 target_long addr
= qdict_get_int(qdict
, "addr");
783 memory_dump(mon
, count
, format
, size
, addr
, 0);
786 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
788 int count
= qdict_get_int(qdict
, "count");
789 int format
= qdict_get_int(qdict
, "format");
790 int size
= qdict_get_int(qdict
, "size");
791 hwaddr addr
= qdict_get_int(qdict
, "addr");
793 memory_dump(mon
, count
, format
, size
, addr
, 1);
796 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
798 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
802 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
806 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
807 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
808 memory_region_unref(mrs
.mr
);
813 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
816 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
818 hwaddr addr
= qdict_get_int(qdict
, "addr");
819 Error
*local_err
= NULL
;
820 MemoryRegion
*mr
= NULL
;
823 ptr
= gpa2hva(&mr
, addr
, &local_err
);
825 error_report_err(local_err
);
829 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
831 addr
, mr
->name
, ptr
);
833 memory_region_unref(mr
);
836 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
838 target_ulong addr
= qdict_get_int(qdict
, "addr");
840 CPUState
*cs
= mon_get_cpu();
844 monitor_printf(mon
, "No cpu\n");
848 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
850 monitor_printf(mon
, "Unmapped\n");
852 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
853 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
858 static uint64_t vtop(void *ptr
, Error
**errp
)
862 uintptr_t addr
= (uintptr_t) ptr
;
863 uintptr_t pagesize
= qemu_real_host_page_size
;
864 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
867 fd
= open("/proc/self/pagemap", O_RDONLY
);
869 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
873 /* Force copy-on-write if necessary. */
874 atomic_add((uint8_t *)ptr
, 0);
876 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
877 error_setg_errno(errp
, errno
, "Cannot read pagemap");
880 if ((pinfo
& (1ull << 63)) == 0) {
881 error_setg(errp
, "Page not present");
884 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
891 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
893 hwaddr addr
= qdict_get_int(qdict
, "addr");
894 Error
*local_err
= NULL
;
895 MemoryRegion
*mr
= NULL
;
899 ptr
= gpa2hva(&mr
, addr
, &local_err
);
901 error_report_err(local_err
);
905 physaddr
= vtop(ptr
, &local_err
);
907 error_report_err(local_err
);
909 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
910 " (%s) is 0x%" PRIx64
"\n",
911 addr
, mr
->name
, (uint64_t) physaddr
);
914 memory_region_unref(mr
);
918 static void do_print(Monitor
*mon
, const QDict
*qdict
)
920 int format
= qdict_get_int(qdict
, "format");
921 hwaddr val
= qdict_get_int(qdict
, "val");
925 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
928 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
931 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
935 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
938 monitor_printc(mon
, val
);
941 monitor_printf(mon
, "\n");
944 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
948 uint32_t start
= qdict_get_int(qdict
, "start");
949 uint32_t size
= qdict_get_int(qdict
, "size");
952 for(addr
= start
; addr
< (start
+ size
); addr
++) {
953 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
954 MEMTXATTRS_UNSPECIFIED
, NULL
);
955 /* BSD sum algorithm ('sum' Unix command) */
956 sum
= (sum
>> 1) | (sum
<< 15);
959 monitor_printf(mon
, "%05d\n", sum
);
962 static int mouse_button_state
;
964 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
966 int dx
, dy
, dz
, button
;
967 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
968 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
969 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
971 dx
= strtol(dx_str
, NULL
, 0);
972 dy
= strtol(dy_str
, NULL
, 0);
973 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
974 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
977 dz
= strtol(dz_str
, NULL
, 0);
979 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
980 qemu_input_queue_btn(NULL
, button
, true);
981 qemu_input_event_sync();
982 qemu_input_queue_btn(NULL
, button
, false);
985 qemu_input_event_sync();
988 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
990 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
991 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
992 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
993 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
995 int button_state
= qdict_get_int(qdict
, "button_state");
997 if (mouse_button_state
== button_state
) {
1000 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1001 qemu_input_event_sync();
1002 mouse_button_state
= button_state
;
1005 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1007 int size
= qdict_get_int(qdict
, "size");
1008 int addr
= qdict_get_int(qdict
, "addr");
1009 int has_index
= qdict_haskey(qdict
, "index");
1014 int index
= qdict_get_int(qdict
, "index");
1015 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1023 val
= cpu_inb(addr
);
1027 val
= cpu_inw(addr
);
1031 val
= cpu_inl(addr
);
1035 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1036 suffix
, addr
, size
* 2, val
);
1039 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1041 int size
= qdict_get_int(qdict
, "size");
1042 int addr
= qdict_get_int(qdict
, "addr");
1043 int val
= qdict_get_int(qdict
, "val");
1045 addr
&= IOPORTS_MASK
;
1050 cpu_outb(addr
, val
);
1053 cpu_outw(addr
, val
);
1056 cpu_outl(addr
, val
);
1061 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1063 Error
*local_err
= NULL
;
1064 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1066 qemu_boot_set(bootdevice
, &local_err
);
1068 error_report_err(local_err
);
1070 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1074 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1076 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1077 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1078 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1080 mtree_info(flatview
, dispatch_tree
, owner
);
1083 #ifdef CONFIG_PROFILER
1087 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1089 static int64_t last_cpu_exec_time
;
1090 int64_t cpu_exec_time
;
1093 cpu_exec_time
= tcg_cpu_exec_time();
1094 delta
= cpu_exec_time
- last_cpu_exec_time
;
1096 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1097 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1098 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1099 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1100 last_cpu_exec_time
= cpu_exec_time
;
1104 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1106 monitor_printf(mon
, "Internal profiler not compiled\n");
1110 /* Capture support */
1111 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1113 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1118 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1119 monitor_printf(mon
, "[%d]: ", i
);
1120 s
->ops
.info (s
->opaque
);
1124 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1127 int n
= qdict_get_int(qdict
, "n");
1130 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1132 s
->ops
.destroy (s
->opaque
);
1133 QLIST_REMOVE (s
, entries
);
1140 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1142 const char *path
= qdict_get_str(qdict
, "path");
1143 int freq
= qdict_get_try_int(qdict
, "freq", 44100);
1144 int bits
= qdict_get_try_int(qdict
, "bits", 16);
1145 int nchannels
= qdict_get_try_int(qdict
, "nchannels", 2);
1146 const char *audiodev
= qdict_get_str(qdict
, "audiodev");
1148 AudioState
*as
= audio_state_by_name(audiodev
);
1151 monitor_printf(mon
, "Audiodev '%s' not found\n", audiodev
);
1155 s
= g_malloc0 (sizeof (*s
));
1157 if (wav_start_capture(as
, s
, path
, freq
, bits
, nchannels
)) {
1158 monitor_printf(mon
, "Failed to add wave capture\n");
1162 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1165 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1170 container
= object_get_objects_root();
1171 obj
= object_resolve_path_component(container
, name
);
1173 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1177 return QAUTHZ_LIST(obj
);
1180 static bool warn_acl
;
1181 static void hmp_warn_acl(void)
1186 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1187 "commands are deprecated with no replacement. Authorization "
1188 "for VNC should be performed using the pluggable QAuthZ "
1193 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1195 const char *aclname
= qdict_get_str(qdict
, "aclname");
1196 QAuthZList
*auth
= find_auth(mon
, aclname
);
1197 QAuthZListRuleList
*rules
;
1206 monitor_printf(mon
, "policy: %s\n",
1207 QAuthZListPolicy_str(auth
->policy
));
1209 rules
= auth
->rules
;
1211 QAuthZListRule
*rule
= rules
->value
;
1213 monitor_printf(mon
, "%zu: %s %s\n", i
,
1214 QAuthZListPolicy_str(rule
->policy
),
1216 rules
= rules
->next
;
1220 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1222 const char *aclname
= qdict_get_str(qdict
, "aclname");
1223 QAuthZList
*auth
= find_auth(mon
, aclname
);
1231 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1232 qapi_free_QAuthZListRuleList(auth
->rules
);
1234 monitor_printf(mon
, "acl: removed all rules\n");
1237 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1239 const char *aclname
= qdict_get_str(qdict
, "aclname");
1240 const char *policy
= qdict_get_str(qdict
, "policy");
1241 QAuthZList
*auth
= find_auth(mon
, aclname
);
1251 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1253 QAUTHZ_LIST_POLICY_DENY
,
1257 monitor_printf(mon
, "acl: unknown policy '%s', "
1258 "expected 'deny' or 'allow'\n", policy
);
1261 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1262 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1264 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1269 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1271 if (strchr(match
, '*')) {
1272 return QAUTHZ_LIST_FORMAT_GLOB
;
1274 return QAUTHZ_LIST_FORMAT_EXACT
;
1278 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1280 const char *aclname
= qdict_get_str(qdict
, "aclname");
1281 const char *match
= qdict_get_str(qdict
, "match");
1282 const char *policystr
= qdict_get_str(qdict
, "policy");
1283 int has_index
= qdict_haskey(qdict
, "index");
1284 int index
= qdict_get_try_int(qdict
, "index", -1);
1285 QAuthZList
*auth
= find_auth(mon
, aclname
);
1287 QAuthZListPolicy policy
;
1288 QAuthZListFormat format
;
1297 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1299 QAUTHZ_LIST_POLICY_DENY
,
1303 monitor_printf(mon
, "acl: unknown policy '%s', "
1304 "expected 'deny' or 'allow'\n", policystr
);
1308 format
= hmp_acl_get_format(match
);
1310 if (has_index
&& index
== 0) {
1311 monitor_printf(mon
, "acl: unable to add acl entry\n");
1316 i
= qauthz_list_insert_rule(auth
, match
, policy
,
1317 format
, index
- 1, &err
);
1319 i
= qauthz_list_append_rule(auth
, match
, policy
,
1323 monitor_printf(mon
, "acl: unable to add rule: %s",
1324 error_get_pretty(err
));
1327 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
1331 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1333 const char *aclname
= qdict_get_str(qdict
, "aclname");
1334 const char *match
= qdict_get_str(qdict
, "match");
1335 QAuthZList
*auth
= find_auth(mon
, aclname
);
1344 i
= qauthz_list_delete_rule(auth
, match
);
1346 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
1348 monitor_printf(mon
, "acl: no matching acl entry\n");
1352 void qmp_getfd(const char *fdname
, Error
**errp
)
1357 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1359 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1363 if (qemu_isdigit(fdname
[0])) {
1365 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1366 "a name not starting with a digit");
1370 qemu_mutex_lock(&cur_mon
->mon_lock
);
1371 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1372 if (strcmp(monfd
->name
, fdname
) != 0) {
1378 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1379 /* Make sure close() is outside critical section */
1384 monfd
= g_malloc0(sizeof(mon_fd_t
));
1385 monfd
->name
= g_strdup(fdname
);
1388 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1389 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1392 void qmp_closefd(const char *fdname
, Error
**errp
)
1397 qemu_mutex_lock(&cur_mon
->mon_lock
);
1398 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1399 if (strcmp(monfd
->name
, fdname
) != 0) {
1403 QLIST_REMOVE(monfd
, next
);
1405 g_free(monfd
->name
);
1407 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1408 /* Make sure close() is outside critical section */
1413 qemu_mutex_unlock(&cur_mon
->mon_lock
);
1414 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1417 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1421 qemu_mutex_lock(&mon
->mon_lock
);
1422 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1425 if (strcmp(monfd
->name
, fdname
) != 0) {
1431 /* caller takes ownership of fd */
1432 QLIST_REMOVE(monfd
, next
);
1433 g_free(monfd
->name
);
1435 qemu_mutex_unlock(&mon
->mon_lock
);
1440 qemu_mutex_unlock(&mon
->mon_lock
);
1441 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1445 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1447 MonFdsetFd
*mon_fdset_fd
;
1448 MonFdsetFd
*mon_fdset_fd_next
;
1450 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1451 if ((mon_fdset_fd
->removed
||
1452 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1453 runstate_is_running()) {
1454 close(mon_fdset_fd
->fd
);
1455 g_free(mon_fdset_fd
->opaque
);
1456 QLIST_REMOVE(mon_fdset_fd
, next
);
1457 g_free(mon_fdset_fd
);
1461 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1462 QLIST_REMOVE(mon_fdset
, next
);
1467 void monitor_fdsets_cleanup(void)
1469 MonFdset
*mon_fdset
;
1470 MonFdset
*mon_fdset_next
;
1472 qemu_mutex_lock(&mon_fdsets_lock
);
1473 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1474 monitor_fdset_cleanup(mon_fdset
);
1476 qemu_mutex_unlock(&mon_fdsets_lock
);
1479 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1480 const char *opaque
, Error
**errp
)
1483 Monitor
*mon
= cur_mon
;
1486 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
1488 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1492 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1493 has_opaque
, opaque
, errp
);
1505 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1507 MonFdset
*mon_fdset
;
1508 MonFdsetFd
*mon_fdset_fd
;
1511 qemu_mutex_lock(&mon_fdsets_lock
);
1512 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1513 if (mon_fdset
->id
!= fdset_id
) {
1516 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1518 if (mon_fdset_fd
->fd
!= fd
) {
1521 mon_fdset_fd
->removed
= true;
1524 mon_fdset_fd
->removed
= true;
1527 if (has_fd
&& !mon_fdset_fd
) {
1530 monitor_fdset_cleanup(mon_fdset
);
1531 qemu_mutex_unlock(&mon_fdsets_lock
);
1536 qemu_mutex_unlock(&mon_fdsets_lock
);
1538 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1541 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1543 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1546 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1548 MonFdset
*mon_fdset
;
1549 MonFdsetFd
*mon_fdset_fd
;
1550 FdsetInfoList
*fdset_list
= NULL
;
1552 qemu_mutex_lock(&mon_fdsets_lock
);
1553 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1554 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1555 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1557 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1558 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1560 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1561 FdsetFdInfoList
*fdsetfd_info
;
1563 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1564 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1565 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1566 if (mon_fdset_fd
->opaque
) {
1567 fdsetfd_info
->value
->has_opaque
= true;
1568 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1570 fdsetfd_info
->value
->has_opaque
= false;
1573 fdsetfd_info
->next
= fdsetfd_list
;
1574 fdsetfd_list
= fdsetfd_info
;
1577 fdset_info
->value
->fds
= fdsetfd_list
;
1579 fdset_info
->next
= fdset_list
;
1580 fdset_list
= fdset_info
;
1582 qemu_mutex_unlock(&mon_fdsets_lock
);
1587 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1588 bool has_opaque
, const char *opaque
,
1591 MonFdset
*mon_fdset
= NULL
;
1592 MonFdsetFd
*mon_fdset_fd
;
1595 qemu_mutex_lock(&mon_fdsets_lock
);
1597 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1598 /* Break if match found or match impossible due to ordering by ID */
1599 if (fdset_id
<= mon_fdset
->id
) {
1600 if (fdset_id
< mon_fdset
->id
) {
1608 if (mon_fdset
== NULL
) {
1609 int64_t fdset_id_prev
= -1;
1610 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1614 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1615 "a non-negative value");
1616 qemu_mutex_unlock(&mon_fdsets_lock
);
1619 /* Use specified fdset ID */
1620 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1621 mon_fdset_cur
= mon_fdset
;
1622 if (fdset_id
< mon_fdset_cur
->id
) {
1627 /* Use first available fdset ID */
1628 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1629 mon_fdset_cur
= mon_fdset
;
1630 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1631 fdset_id_prev
= mon_fdset_cur
->id
;
1638 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1640 mon_fdset
->id
= fdset_id
;
1642 mon_fdset
->id
= fdset_id_prev
+ 1;
1645 /* The fdset list is ordered by fdset ID */
1646 if (!mon_fdset_cur
) {
1647 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1648 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1649 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1651 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1655 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1656 mon_fdset_fd
->fd
= fd
;
1657 mon_fdset_fd
->removed
= false;
1659 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1661 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1663 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1664 fdinfo
->fdset_id
= mon_fdset
->id
;
1665 fdinfo
->fd
= mon_fdset_fd
->fd
;
1667 qemu_mutex_unlock(&mon_fdsets_lock
);
1671 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1676 MonFdset
*mon_fdset
;
1677 MonFdsetFd
*mon_fdset_fd
;
1681 qemu_mutex_lock(&mon_fdsets_lock
);
1682 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1683 if (mon_fdset
->id
!= fdset_id
) {
1686 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1687 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1688 if (mon_fd_flags
== -1) {
1693 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1694 ret
= mon_fdset_fd
->fd
;
1704 qemu_mutex_unlock(&mon_fdsets_lock
);
1709 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
1711 MonFdset
*mon_fdset
;
1712 MonFdsetFd
*mon_fdset_fd_dup
;
1714 qemu_mutex_lock(&mon_fdsets_lock
);
1715 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1716 if (mon_fdset
->id
!= fdset_id
) {
1719 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1720 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1724 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1725 mon_fdset_fd_dup
->fd
= dup_fd
;
1726 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1727 qemu_mutex_unlock(&mon_fdsets_lock
);
1732 qemu_mutex_unlock(&mon_fdsets_lock
);
1736 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1738 MonFdset
*mon_fdset
;
1739 MonFdsetFd
*mon_fdset_fd_dup
;
1741 qemu_mutex_lock(&mon_fdsets_lock
);
1742 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1743 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1744 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1746 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1747 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1748 monitor_fdset_cleanup(mon_fdset
);
1752 qemu_mutex_unlock(&mon_fdsets_lock
);
1753 return mon_fdset
->id
;
1760 qemu_mutex_unlock(&mon_fdsets_lock
);
1764 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1766 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1769 void monitor_fdset_dup_fd_remove(int dup_fd
)
1771 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1774 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1777 Error
*local_err
= NULL
;
1779 if (!qemu_isdigit(fdname
[0]) && mon
) {
1780 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
1782 fd
= qemu_parse_fd(fdname
);
1784 error_setg(&local_err
, "Invalid file descriptor number '%s'",
1789 error_propagate(errp
, local_err
);
1798 /* Please update hmp-commands.hx when adding or changing commands */
1799 static HMPCommand hmp_info_cmds
[] = {
1800 #include "hmp-commands-info.h"
1804 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1805 HMPCommand hmp_cmds
[] = {
1806 #include "hmp-commands.h"
1811 * Set @pval to the value in the register identified by @name.
1812 * return 0 if OK, -1 if not found
1814 int get_monitor_def(int64_t *pval
, const char *name
)
1816 const MonitorDef
*md
= target_monitor_defs();
1817 CPUState
*cs
= mon_get_cpu();
1822 if (cs
== NULL
|| md
== NULL
) {
1826 for(; md
->name
!= NULL
; md
++) {
1827 if (hmp_compare_cmd(name
, md
->name
)) {
1828 if (md
->get_value
) {
1829 *pval
= md
->get_value(md
, md
->offset
);
1831 CPUArchState
*env
= mon_get_cpu_env();
1832 ptr
= (uint8_t *)env
+ md
->offset
;
1835 *pval
= *(int32_t *)ptr
;
1838 *pval
= *(target_long
*)ptr
;
1849 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1851 *pval
= (target_long
) tmp
;
1857 static void add_completion_option(ReadLineState
*rs
, const char *str
,
1860 if (!str
|| !option
) {
1863 if (!strncmp(option
, str
, strlen(str
))) {
1864 readline_add_completion(rs
, option
);
1868 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1871 ChardevBackendInfoList
*list
, *start
;
1877 readline_set_completion_index(rs
, len
);
1879 start
= list
= qmp_query_chardev_backends(NULL
);
1881 const char *chr_name
= list
->value
->name
;
1883 if (!strncmp(chr_name
, str
, len
)) {
1884 readline_add_completion(rs
, chr_name
);
1888 qapi_free_ChardevBackendInfoList(start
);
1891 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1900 readline_set_completion_index(rs
, len
);
1901 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
1902 add_completion_option(rs
, str
, NetClientDriver_str(i
));
1906 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1916 readline_set_completion_index(rs
, len
);
1917 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1920 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1922 name
= object_class_get_name(OBJECT_CLASS(dc
));
1924 if (dc
->user_creatable
1925 && !strncmp(name
, str
, len
)) {
1926 readline_add_completion(rs
, name
);
1933 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1943 readline_set_completion_index(rs
, len
);
1944 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
1948 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
1949 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
1950 readline_add_completion(rs
, name
);
1957 static void peripheral_device_del_completion(ReadLineState
*rs
,
1958 const char *str
, size_t len
)
1960 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1961 GSList
*list
, *item
;
1963 list
= qdev_build_hotpluggable_device_list(peripheral
);
1968 for (item
= list
; item
; item
= g_slist_next(item
)) {
1969 DeviceState
*dev
= item
->data
;
1971 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
1972 readline_add_completion(rs
, dev
->id
);
1979 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1982 ChardevInfoList
*list
, *start
;
1988 readline_set_completion_index(rs
, len
);
1990 start
= list
= qmp_query_chardev(NULL
);
1992 ChardevInfo
*chr
= list
->value
;
1994 if (!strncmp(chr
->label
, str
, len
)) {
1995 readline_add_completion(rs
, chr
->label
);
1999 qapi_free_ChardevInfoList(start
);
2002 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
2005 ChardevInfoList
*list
, *start
;
2008 readline_set_completion_index(rs
, len
);
2010 start
= list
= qmp_query_chardev(NULL
);
2012 ChardevInfo
*chr_info
= list
->value
;
2014 if (!strncmp(chr_info
->label
, str
, len
)) {
2015 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
2016 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
2017 readline_add_completion(rs
, chr_info
->label
);
2022 qapi_free_ChardevInfoList(start
);
2025 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2030 ringbuf_completion(rs
, str
);
2033 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2042 readline_set_completion_index(rs
, len
);
2043 peripheral_device_del_completion(rs
, str
, len
);
2046 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2048 ObjectPropertyInfoList
*list
, *start
;
2055 readline_set_completion_index(rs
, len
);
2057 start
= list
= qmp_qom_list("/objects", NULL
);
2059 ObjectPropertyInfo
*info
= list
->value
;
2061 if (!strncmp(info
->type
, "child<", 5)
2062 && !strncmp(info
->name
, str
, len
)) {
2063 readline_add_completion(rs
, info
->name
);
2067 qapi_free_ObjectPropertyInfoList(start
);
2070 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2079 sep
= strrchr(str
, '-');
2084 readline_set_completion_index(rs
, len
);
2085 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
2086 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
2087 readline_add_completion(rs
, QKeyCode_str(i
));
2092 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2097 readline_set_completion_index(rs
, len
);
2099 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2101 count
= qemu_find_net_clients_except(NULL
, ncs
,
2102 NET_CLIENT_DRIVER_NONE
,
2104 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2105 const char *name
= ncs
[i
]->name
;
2106 if (!strncmp(str
, name
, len
)) {
2107 readline_add_completion(rs
, name
);
2110 } else if (nb_args
== 3) {
2111 add_completion_option(rs
, str
, "on");
2112 add_completion_option(rs
, str
, "off");
2116 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2119 NetClientState
*ncs
[MAX_QUEUE_NUM
];
2126 readline_set_completion_index(rs
, len
);
2127 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
2129 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
2131 const char *name
= ncs
[i
]->name
;
2132 if (strncmp(str
, name
, len
)) {
2135 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
2137 readline_add_completion(rs
, name
);
2142 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2147 readline_set_completion_index(rs
, len
);
2149 TraceEventIter iter
;
2151 char *pattern
= g_strdup_printf("%s*", str
);
2152 trace_event_iter_init(&iter
, pattern
);
2153 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2154 readline_add_completion(rs
, trace_event_get_name(ev
));
2160 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2165 readline_set_completion_index(rs
, len
);
2167 TraceEventIter iter
;
2169 char *pattern
= g_strdup_printf("%s*", str
);
2170 trace_event_iter_init(&iter
, pattern
);
2171 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
2172 readline_add_completion(rs
, trace_event_get_name(ev
));
2175 } else if (nb_args
== 3) {
2176 add_completion_option(rs
, str
, "on");
2177 add_completion_option(rs
, str
, "off");
2181 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2188 readline_set_completion_index(rs
, strlen(str
));
2189 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
2190 add_completion_option(rs
, str
, WatchdogAction_str(i
));
2194 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
2200 readline_set_completion_index(rs
, len
);
2203 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
2204 const char *name
= MigrationCapability_str(i
);
2205 if (!strncmp(str
, name
, len
)) {
2206 readline_add_completion(rs
, name
);
2209 } else if (nb_args
== 3) {
2210 add_completion_option(rs
, str
, "on");
2211 add_completion_option(rs
, str
, "off");
2215 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
2221 readline_set_completion_index(rs
, len
);
2224 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
2225 const char *name
= MigrationParameter_str(i
);
2226 if (!strncmp(str
, name
, len
)) {
2227 readline_add_completion(rs
, name
);
2233 static void vm_completion(ReadLineState
*rs
, const char *str
)
2236 BlockDriverState
*bs
;
2237 BdrvNextIterator it
;
2240 readline_set_completion_index(rs
, len
);
2242 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
2243 SnapshotInfoList
*snapshots
, *snapshot
;
2244 AioContext
*ctx
= bdrv_get_aio_context(bs
);
2247 aio_context_acquire(ctx
);
2248 if (bdrv_can_snapshot(bs
)) {
2249 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
2251 aio_context_release(ctx
);
2256 snapshot
= snapshots
;
2258 char *completion
= snapshot
->value
->name
;
2259 if (!strncmp(str
, completion
, len
)) {
2260 readline_add_completion(rs
, completion
);
2262 completion
= snapshot
->value
->id
;
2263 if (!strncmp(str
, completion
, len
)) {
2264 readline_add_completion(rs
, completion
);
2266 snapshot
= snapshot
->next
;
2268 qapi_free_SnapshotInfoList(snapshots
);
2273 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2276 vm_completion(rs
, str
);
2280 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2283 vm_completion(rs
, str
);
2288 compare_mon_cmd(const void *a
, const void *b
)
2290 return strcmp(((const HMPCommand
*)a
)->name
,
2291 ((const HMPCommand
*)b
)->name
);
2294 static void sortcmdlist(void)
2296 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
2299 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
2300 sizeof(*hmp_info_cmds
),
2304 void monitor_init_globals(void)
2306 monitor_init_globals_core();
2307 monitor_init_qmp_commands();
2309 qemu_mutex_init(&mon_fdsets_lock
);