4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "exec/gdbstub.h"
29 #include "net/slirp.h"
30 #include "qemu/ctype.h"
31 #include "disas/disas.h"
33 #include "sysemu/hw_accel.h"
34 #include "sysemu/runstate.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/device_tree.h"
37 #include "qapi/qmp/qdict.h"
38 #include "qapi/qmp/qerror.h"
39 #include "monitor/hmp-target.h"
40 #include "monitor/hmp.h"
41 #include "exec/address-spaces.h"
42 #include "exec/ioport.h"
43 #include "block/block-hmp-cmds.h"
44 #include "qapi/qapi-commands-control.h"
45 #include "qapi/qapi-commands-misc.h"
46 #include "qapi/qapi-commands-run-state.h"
47 #include "qapi/qapi-commands-machine.h"
48 #include "qapi/qapi-init-commands.h"
49 #include "qapi/error.h"
50 #include "qemu/cutils.h"
52 #if defined(TARGET_S390X)
53 #include "hw/s390x/storage-keys.h"
54 #include "hw/s390x/storage-attributes.h"
57 /* Make devices configuration available for use in hmp-commands*.hx templates */
58 #include CONFIG_DEVICES
60 /* file descriptors passed via SCM_RIGHTS */
61 typedef struct mon_fd_t mon_fd_t
;
65 QLIST_ENTRY(mon_fd_t
) next
;
68 /* file descriptor associated with a file descriptor set */
69 typedef struct MonFdsetFd MonFdsetFd
;
74 QLIST_ENTRY(MonFdsetFd
) next
;
77 /* file descriptor set containing fds passed via SCM_RIGHTS */
78 typedef struct MonFdset MonFdset
;
81 QLIST_HEAD(, MonFdsetFd
) fds
;
82 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
83 QLIST_ENTRY(MonFdset
) next
;
86 /* Protects mon_fdsets */
87 static QemuMutex mon_fdsets_lock
;
88 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
90 static HMPCommand hmp_info_cmds
[];
92 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
93 int64_t cpu_index
, Error
**errp
)
98 monitor_data_init(&hmp
.common
, false, true, false);
101 int ret
= monitor_set_cpu(&hmp
.common
, cpu_index
);
103 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
109 handle_hmp_command(&hmp
, command_line
);
111 WITH_QEMU_LOCK_GUARD(&hmp
.common
.mon_lock
) {
112 output
= g_strdup(hmp
.common
.outbuf
->str
);
116 monitor_data_destroy(&hmp
.common
);
121 * Is @name in the '|' separated list of names @list?
123 int hmp_compare_cmd(const char *name
, const char *list
)
125 const char *p
, *pstart
;
131 p
= qemu_strchrnul(p
, '|');
132 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
)) {
143 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
145 hmp_help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
148 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
150 hmp_help_cmd(mon
, "info");
153 static void monitor_init_qmp_commands(void)
157 * - qmp_commands contains all QMP commands
158 * - qmp_cap_negotiation_commands contains just
159 * "qmp_capabilities", to enforce capability negotiation
162 qmp_init_marshal(&qmp_commands
);
164 qmp_register_command(&qmp_commands
, "device_add",
165 qmp_device_add
, 0, 0);
167 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
168 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
169 qmp_marshal_qmp_capabilities
,
170 QCO_ALLOW_PRECONFIG
, 0);
173 /* Set the current CPU defined by the user. Callers must hold BQL. */
174 int monitor_set_cpu(Monitor
*mon
, int cpu_index
)
178 cpu
= qemu_get_cpu(cpu_index
);
182 g_free(mon
->mon_cpu_path
);
183 mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
187 /* Callers must hold BQL. */
188 static CPUState
*mon_get_cpu_sync(Monitor
*mon
, bool synchronize
)
190 CPUState
*cpu
= NULL
;
192 if (mon
->mon_cpu_path
) {
193 cpu
= (CPUState
*) object_resolve_path_type(mon
->mon_cpu_path
,
196 g_free(mon
->mon_cpu_path
);
197 mon
->mon_cpu_path
= NULL
;
200 if (!mon
->mon_cpu_path
) {
204 monitor_set_cpu(mon
, first_cpu
->cpu_index
);
209 cpu_synchronize_state(cpu
);
214 CPUState
*mon_get_cpu(Monitor
*mon
)
216 return mon_get_cpu_sync(mon
, true);
219 CPUArchState
*mon_get_cpu_env(Monitor
*mon
)
221 CPUState
*cs
= mon_get_cpu(mon
);
223 return cs
? cs
->env_ptr
: NULL
;
226 int monitor_get_cpu_index(Monitor
*mon
)
228 CPUState
*cs
= mon_get_cpu_sync(mon
, false);
230 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
233 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
235 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
236 int vcpu
= qdict_get_try_int(qdict
, "vcpu", -1);
241 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
242 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
245 cs
= vcpu
>= 0 ? qemu_get_cpu(vcpu
) : mon_get_cpu(mon
);
249 monitor_printf(mon
, "CPU#%d not available\n", vcpu
);
251 monitor_printf(mon
, "No CPU available\n");
256 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
257 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
261 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
263 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
264 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
265 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
266 enum QSPSortBy sort_by
;
268 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
269 qsp_report(max
, sort_by
, coalesce
);
272 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
274 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
283 str
= readline_get_history(hmp_mon
->rs
, i
);
287 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
292 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
296 if (!qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
)) {
297 error_report_err(err
);
301 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
304 const char *items
= qdict_get_str(qdict
, "items");
307 if (!strcmp(items
, "none")) {
310 mask
= qemu_str_to_log_mask(items
);
312 hmp_help_cmd(mon
, "log");
317 if (!qemu_set_log(mask
, &err
)) {
318 error_report_err(err
);
322 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
324 const char *option
= qdict_get_try_str(qdict
, "option");
325 if (!option
|| !strcmp(option
, "on")) {
327 } else if (!strcmp(option
, "off")) {
330 monitor_printf(mon
, "unexpected option %s\n", option
);
334 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
336 const char *device
= qdict_get_try_str(qdict
, "device");
338 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
341 if (gdbserver_start(device
) < 0) {
342 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
344 } else if (strcmp(device
, "none") == 0) {
345 monitor_printf(mon
, "Disabled gdbserver\n");
347 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
352 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
355 WatchdogAction action
;
358 qapi_value
= g_ascii_strdown(qdict_get_str(qdict
, "action"), -1);
359 action
= qapi_enum_parse(&WatchdogAction_lookup
, qapi_value
, -1, &err
);
362 hmp_handle_error(mon
, err
);
365 qmp_watchdog_set_action(action
, &error_abort
);
368 static void monitor_printc(Monitor
*mon
, int c
)
370 monitor_printf(mon
, "'");
373 monitor_printf(mon
, "\\'");
376 monitor_printf(mon
, "\\\\");
379 monitor_printf(mon
, "\\n");
382 monitor_printf(mon
, "\\r");
385 if (c
>= 32 && c
<= 126) {
386 monitor_printf(mon
, "%c", c
);
388 monitor_printf(mon
, "\\x%02x", c
);
392 monitor_printf(mon
, "'");
395 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
396 hwaddr addr
, int is_physical
)
398 int l
, line_size
, i
, max_digits
, len
;
401 CPUState
*cs
= mon_get_cpu(mon
);
403 if (!cs
&& (format
== 'i' || !is_physical
)) {
404 monitor_printf(mon
, "Can not dump without CPU\n");
409 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
423 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
427 max_digits
= (wsize
* 8) / 4;
431 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
440 monitor_printf(mon
, HWADDR_FMT_plx
":", addr
);
442 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
448 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
449 MemTxResult r
= address_space_read(as
, addr
,
450 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
452 monitor_printf(mon
, " Cannot access memory\n");
456 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
457 monitor_printf(mon
, " Cannot access memory\n");
472 v
= (uint32_t)ldl_p(buf
+ i
);
478 monitor_printf(mon
, " ");
481 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
484 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
487 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
490 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
493 monitor_printc(mon
, v
);
498 monitor_printf(mon
, "\n");
504 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
506 int count
= qdict_get_int(qdict
, "count");
507 int format
= qdict_get_int(qdict
, "format");
508 int size
= qdict_get_int(qdict
, "size");
509 target_long addr
= qdict_get_int(qdict
, "addr");
511 memory_dump(mon
, count
, format
, size
, addr
, 0);
514 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
516 int count
= qdict_get_int(qdict
, "count");
517 int format
= qdict_get_int(qdict
, "format");
518 int size
= qdict_get_int(qdict
, "size");
519 hwaddr addr
= qdict_get_int(qdict
, "addr");
521 memory_dump(mon
, count
, format
, size
, addr
, 1);
524 void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, uint64_t size
, Error
**errp
)
526 Int128 gpa_region_size
;
527 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
531 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
535 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
536 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
537 memory_region_unref(mrs
.mr
);
541 gpa_region_size
= int128_make64(size
);
542 if (int128_lt(mrs
.size
, gpa_region_size
)) {
543 error_setg(errp
, "Size of memory region at 0x%" HWADDR_PRIx
545 memory_region_unref(mrs
.mr
);
550 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
553 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
555 hwaddr addr
= qdict_get_int(qdict
, "addr");
556 Error
*local_err
= NULL
;
557 MemoryRegion
*mr
= NULL
;
560 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
562 error_report_err(local_err
);
566 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
568 addr
, mr
->name
, ptr
);
570 memory_region_unref(mr
);
573 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
575 target_ulong addr
= qdict_get_int(qdict
, "addr");
577 CPUState
*cs
= mon_get_cpu(mon
);
581 monitor_printf(mon
, "No cpu\n");
585 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
587 monitor_printf(mon
, "Unmapped\n");
589 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
590 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
595 static uint64_t vtop(void *ptr
, Error
**errp
)
599 uintptr_t addr
= (uintptr_t) ptr
;
600 uintptr_t pagesize
= qemu_real_host_page_size();
601 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
604 fd
= open("/proc/self/pagemap", O_RDONLY
);
606 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
610 /* Force copy-on-write if necessary. */
611 qatomic_add((uint8_t *)ptr
, 0);
613 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
614 error_setg_errno(errp
, errno
, "Cannot read pagemap");
617 if ((pinfo
& (1ull << 63)) == 0) {
618 error_setg(errp
, "Page not present");
621 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
628 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
630 hwaddr addr
= qdict_get_int(qdict
, "addr");
631 Error
*local_err
= NULL
;
632 MemoryRegion
*mr
= NULL
;
636 ptr
= gpa2hva(&mr
, addr
, 1, &local_err
);
638 error_report_err(local_err
);
642 physaddr
= vtop(ptr
, &local_err
);
644 error_report_err(local_err
);
646 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
647 " (%s) is 0x%" PRIx64
"\n",
648 addr
, mr
->name
, (uint64_t) physaddr
);
651 memory_region_unref(mr
);
655 static void do_print(Monitor
*mon
, const QDict
*qdict
)
657 int format
= qdict_get_int(qdict
, "format");
658 hwaddr val
= qdict_get_int(qdict
, "val");
662 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
665 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
668 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
672 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
675 monitor_printc(mon
, val
);
678 monitor_printf(mon
, "\n");
681 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
685 uint32_t start
= qdict_get_int(qdict
, "start");
686 uint32_t size
= qdict_get_int(qdict
, "size");
689 for(addr
= start
; addr
< (start
+ size
); addr
++) {
690 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
691 MEMTXATTRS_UNSPECIFIED
, NULL
);
692 /* BSD sum algorithm ('sum' Unix command) */
693 sum
= (sum
>> 1) | (sum
<< 15);
696 monitor_printf(mon
, "%05d\n", sum
);
699 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
701 int size
= qdict_get_int(qdict
, "size");
702 int addr
= qdict_get_int(qdict
, "addr");
703 int has_index
= qdict_haskey(qdict
, "index");
708 int index
= qdict_get_int(qdict
, "index");
709 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
729 monitor_printf(mon
, "port%c[0x%04x] = 0x%0*x\n",
730 suffix
, addr
, size
* 2, val
);
733 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
735 int size
= qdict_get_int(qdict
, "size");
736 int addr
= qdict_get_int(qdict
, "addr");
737 int val
= qdict_get_int(qdict
, "val");
739 addr
&= IOPORTS_MASK
;
755 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
757 Error
*local_err
= NULL
;
758 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
760 qemu_boot_set(bootdevice
, &local_err
);
762 error_report_err(local_err
);
764 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
768 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
770 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
771 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
772 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
773 bool disabled
= qdict_get_try_bool(qdict
, "disabled", false);
775 mtree_info(flatview
, dispatch_tree
, owner
, disabled
);
778 void qmp_getfd(const char *fdname
, Error
**errp
)
780 Monitor
*cur_mon
= monitor_cur();
784 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
786 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
790 if (qemu_isdigit(fdname
[0])) {
792 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
793 "a name not starting with a digit");
797 QEMU_LOCK_GUARD(&cur_mon
->mon_lock
);
798 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
799 if (strcmp(monfd
->name
, fdname
) != 0) {
805 /* Make sure close() is outside critical section */
810 monfd
= g_new0(mon_fd_t
, 1);
811 monfd
->name
= g_strdup(fdname
);
814 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
817 void qmp_closefd(const char *fdname
, Error
**errp
)
819 Monitor
*cur_mon
= monitor_cur();
823 qemu_mutex_lock(&cur_mon
->mon_lock
);
824 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
825 if (strcmp(monfd
->name
, fdname
) != 0) {
829 QLIST_REMOVE(monfd
, next
);
833 qemu_mutex_unlock(&cur_mon
->mon_lock
);
834 /* Make sure close() is outside critical section */
839 qemu_mutex_unlock(&cur_mon
->mon_lock
);
840 error_setg(errp
, "File descriptor named '%s' not found", fdname
);
843 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
847 QEMU_LOCK_GUARD(&mon
->mon_lock
);
848 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
851 if (strcmp(monfd
->name
, fdname
) != 0) {
858 /* caller takes ownership of fd */
859 QLIST_REMOVE(monfd
, next
);
866 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
870 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
872 MonFdsetFd
*mon_fdset_fd
;
873 MonFdsetFd
*mon_fdset_fd_next
;
875 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
876 if ((mon_fdset_fd
->removed
||
877 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
878 runstate_is_running()) {
879 close(mon_fdset_fd
->fd
);
880 g_free(mon_fdset_fd
->opaque
);
881 QLIST_REMOVE(mon_fdset_fd
, next
);
882 g_free(mon_fdset_fd
);
886 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
887 QLIST_REMOVE(mon_fdset
, next
);
892 void monitor_fdsets_cleanup(void)
895 MonFdset
*mon_fdset_next
;
897 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
898 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
899 monitor_fdset_cleanup(mon_fdset
);
903 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
,
904 const char *opaque
, Error
**errp
)
907 Monitor
*mon
= monitor_cur();
910 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
912 error_setg(errp
, "No file descriptor supplied via SCM_RIGHTS");
916 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
, opaque
, errp
);
928 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
931 MonFdsetFd
*mon_fdset_fd
;
934 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
935 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
936 if (mon_fdset
->id
!= fdset_id
) {
939 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
941 if (mon_fdset_fd
->fd
!= fd
) {
944 mon_fdset_fd
->removed
= true;
947 mon_fdset_fd
->removed
= true;
950 if (has_fd
&& !mon_fdset_fd
) {
953 monitor_fdset_cleanup(mon_fdset
);
959 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
962 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
964 error_setg(errp
, "File descriptor named '%s' not found", fd_str
);
967 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
970 MonFdsetFd
*mon_fdset_fd
;
971 FdsetInfoList
*fdset_list
= NULL
;
973 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
974 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
975 FdsetInfo
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
977 fdset_info
->fdset_id
= mon_fdset
->id
;
979 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
980 FdsetFdInfo
*fdsetfd_info
;
982 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
983 fdsetfd_info
->fd
= mon_fdset_fd
->fd
;
984 fdsetfd_info
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
986 QAPI_LIST_PREPEND(fdset_info
->fds
, fdsetfd_info
);
989 QAPI_LIST_PREPEND(fdset_list
, fdset_info
);
995 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
996 const char *opaque
, Error
**errp
)
998 MonFdset
*mon_fdset
= NULL
;
999 MonFdsetFd
*mon_fdset_fd
;
1002 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1004 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1005 /* Break if match found or match impossible due to ordering by ID */
1006 if (fdset_id
<= mon_fdset
->id
) {
1007 if (fdset_id
< mon_fdset
->id
) {
1015 if (mon_fdset
== NULL
) {
1016 int64_t fdset_id_prev
= -1;
1017 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1021 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1022 "a non-negative value");
1025 /* Use specified fdset ID */
1026 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1027 mon_fdset_cur
= mon_fdset
;
1028 if (fdset_id
< mon_fdset_cur
->id
) {
1033 /* Use first available fdset ID */
1034 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1035 mon_fdset_cur
= mon_fdset
;
1036 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1037 fdset_id_prev
= mon_fdset_cur
->id
;
1044 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1046 mon_fdset
->id
= fdset_id
;
1048 mon_fdset
->id
= fdset_id_prev
+ 1;
1051 /* The fdset list is ordered by fdset ID */
1052 if (!mon_fdset_cur
) {
1053 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1054 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1055 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1057 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1061 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1062 mon_fdset_fd
->fd
= fd
;
1063 mon_fdset_fd
->removed
= false;
1064 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1065 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1067 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1068 fdinfo
->fdset_id
= mon_fdset
->id
;
1069 fdinfo
->fd
= mon_fdset_fd
->fd
;
1074 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int flags
)
1079 MonFdset
*mon_fdset
;
1081 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1082 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1083 MonFdsetFd
*mon_fdset_fd
;
1084 MonFdsetFd
*mon_fdset_fd_dup
;
1089 if (mon_fdset
->id
!= fdset_id
) {
1093 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1094 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1095 if (mon_fd_flags
== -1) {
1099 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1100 fd
= mon_fdset_fd
->fd
;
1110 dup_fd
= qemu_dup_flags(fd
, flags
);
1115 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
1116 mon_fdset_fd_dup
->fd
= dup_fd
;
1117 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
1126 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
1128 MonFdset
*mon_fdset
;
1129 MonFdsetFd
*mon_fdset_fd_dup
;
1131 QEMU_LOCK_GUARD(&mon_fdsets_lock
);
1132 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1133 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1134 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
1136 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
1137 g_free(mon_fdset_fd_dup
);
1138 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1139 monitor_fdset_cleanup(mon_fdset
);
1143 return mon_fdset
->id
;
1152 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
1154 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
1157 void monitor_fdset_dup_fd_remove(int dup_fd
)
1159 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
1162 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
1166 if (!qemu_isdigit(fdname
[0]) && mon
) {
1167 fd
= monitor_get_fd(mon
, fdname
, errp
);
1169 fd
= qemu_parse_fd(fdname
);
1171 error_setg(errp
, "Invalid file descriptor number '%s'",
1179 /* Please update hmp-commands.hx when adding or changing commands */
1180 static HMPCommand hmp_info_cmds
[] = {
1181 #include "hmp-commands-info.h"
1185 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1186 HMPCommand hmp_cmds
[] = {
1187 #include "hmp-commands.h"
1192 * Set @pval to the value in the register identified by @name.
1193 * return 0 if OK, -1 if not found
1195 int get_monitor_def(Monitor
*mon
, int64_t *pval
, const char *name
)
1197 const MonitorDef
*md
= target_monitor_defs();
1198 CPUState
*cs
= mon_get_cpu(mon
);
1203 if (cs
== NULL
|| md
== NULL
) {
1207 for(; md
->name
!= NULL
; md
++) {
1208 if (hmp_compare_cmd(name
, md
->name
)) {
1209 if (md
->get_value
) {
1210 *pval
= md
->get_value(mon
, md
, md
->offset
);
1212 CPUArchState
*env
= mon_get_cpu_env(mon
);
1213 ptr
= (uint8_t *)env
+ md
->offset
;
1216 *pval
= *(int32_t *)ptr
;
1219 *pval
= *(target_long
*)ptr
;
1230 ret
= target_get_monitor_def(cs
, name
, &tmp
);
1232 *pval
= (target_long
) tmp
;
1238 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1248 readline_set_completion_index(rs
, len
);
1249 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
1251 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
1254 if (dc
->user_creatable
) {
1255 readline_add_completion_of(rs
, str
,
1256 object_class_get_name(OBJECT_CLASS(dc
)));
1263 static int qdev_add_hotpluggable_device(Object
*obj
, void *opaque
)
1265 GSList
**list
= opaque
;
1266 DeviceState
*dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
1272 if (dev
->realized
&& object_property_get_bool(obj
, "hotpluggable", NULL
)) {
1273 *list
= g_slist_append(*list
, dev
);
1279 static GSList
*qdev_build_hotpluggable_device_list(Object
*peripheral
)
1281 GSList
*list
= NULL
;
1283 object_child_foreach(peripheral
, qdev_add_hotpluggable_device
, &list
);
1288 static void peripheral_device_del_completion(ReadLineState
*rs
,
1291 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
1292 GSList
*list
, *item
;
1294 list
= qdev_build_hotpluggable_device_list(peripheral
);
1299 for (item
= list
; item
; item
= g_slist_next(item
)) {
1300 DeviceState
*dev
= item
->data
;
1303 readline_add_completion_of(rs
, str
, dev
->id
);
1310 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1316 readline_set_completion_index(rs
, strlen(str
));
1317 peripheral_device_del_completion(rs
, str
);
1320 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
1327 readline_set_completion_index(rs
, strlen(str
));
1328 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
1329 readline_add_completion_of(rs
, str
, WatchdogAction_str(i
));
1334 compare_mon_cmd(const void *a
, const void *b
)
1336 return strcmp(((const HMPCommand
*)a
)->name
,
1337 ((const HMPCommand
*)b
)->name
);
1340 static void sortcmdlist(void)
1342 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
1345 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
1346 sizeof(*hmp_info_cmds
),
1350 void monitor_register_hmp(const char *name
, bool info
,
1351 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
))
1353 HMPCommand
*table
= info
? hmp_info_cmds
: hmp_cmds
;
1355 while (table
->name
!= NULL
) {
1356 if (strcmp(table
->name
, name
) == 0) {
1357 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1363 g_assert_not_reached();
1366 void monitor_register_hmp_info_hrt(const char *name
,
1367 HumanReadableText
*(*handler
)(Error
**errp
))
1369 HMPCommand
*table
= hmp_info_cmds
;
1371 while (table
->name
!= NULL
) {
1372 if (strcmp(table
->name
, name
) == 0) {
1373 g_assert(table
->cmd
== NULL
&& table
->cmd_info_hrt
== NULL
);
1374 table
->cmd_info_hrt
= handler
;
1379 g_assert_not_reached();
1382 void monitor_init_globals(void)
1384 monitor_init_globals_core();
1385 monitor_init_qmp_commands();
1387 qemu_mutex_init(&mon_fdsets_lock
);