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 "block/block-hmp-cmds.h"
70 #include "qapi/qapi-commands-char.h"
71 #include "qapi/qapi-commands-control.h"
72 #include "qapi/qapi-commands-migration.h"
73 #include "qapi/qapi-commands-misc.h"
74 #include "qapi/qapi-commands-qom.h"
75 #include "qapi/qapi-commands-trace.h"
76 #include "qapi/qapi-init-commands.h"
77 #include "qapi/error.h"
78 #include "qapi/qmp-event.h"
79 #include "sysemu/cpus.h"
80 #include "qemu/cutils.h"
83 #if defined(TARGET_S390X)
84 #include "hw/s390x/storage-keys.h"
85 #include "hw/s390x/storage-attributes.h"
88 /* file descriptors passed via SCM_RIGHTS */
89 typedef struct mon_fd_t mon_fd_t;
93 QLIST_ENTRY(mon_fd_t) next;
96 /* file descriptor associated with a file descriptor set */
97 typedef struct MonFdsetFd MonFdsetFd;
102 QLIST_ENTRY(MonFdsetFd) next;
105 /* file descriptor set containing fds passed via SCM_RIGHTS */
106 typedef struct MonFdset MonFdset;
109 QLIST_HEAD(, MonFdsetFd) fds;
110 QLIST_HEAD(, MonFdsetFd) dup_fds;
111 QLIST_ENTRY(MonFdset) next;
114 /* Protects mon_fdsets */
115 static QemuMutex mon_fdsets_lock;
116 static QLIST_HEAD(, MonFdset) mon_fdsets;
118 static HMPCommand hmp_info_cmds[];
120 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
121 int64_t cpu_index, Error **errp)
127 monitor_data_init(&hmp.common, false, true, false);
130 cur_mon = &hmp.common;
133 int ret = monitor_set_cpu(cpu_index);
136 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
142 handle_hmp_command(&hmp, command_line);
145 qemu_mutex_lock(&hmp.common.mon_lock);
146 if (qstring_get_length(hmp.common.outbuf) > 0) {
147 output = g_strdup(qstring_get_str(hmp.common.outbuf));
149 output = g_strdup("");
151 qemu_mutex_unlock(&hmp.common.mon_lock);
154 monitor_data_destroy(&hmp.common);
159 * Is @name in the '|' separated list of names @list?
161 int hmp_compare_cmd(const char *name, const char *list)
163 const char *p, *pstart;
169 p = qemu_strchrnul(p, '|');
170 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
181 static void do_help_cmd(Monitor *mon, const QDict *qdict)
183 help_cmd(mon, qdict_get_try_str(qdict, "name"));
186 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
188 const char *tp_name = qdict_get_str(qdict, "name");
189 bool new_state = qdict_get_bool(qdict, "option");
190 bool has_vcpu = qdict_haskey(qdict, "vcpu");
191 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
192 Error *local_err = NULL;
195 monitor_printf(mon, "argument vcpu must be positive");
199 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
201 error_report_err(local_err);
205 #ifdef CONFIG_TRACE_SIMPLE
206 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
208 const char *op = qdict_get_try_str(qdict, "op");
209 const char *arg = qdict_get_try_str(qdict, "arg");
212 st_print_trace_file_status();
213 } else if (!strcmp(op, "on")) {
214 st_set_trace_file_enabled(true);
215 } else if (!strcmp(op, "off")) {
216 st_set_trace_file_enabled(false);
217 } else if (!strcmp(op, "flush")) {
218 st_flush_trace_buffer();
219 } else if (!strcmp(op, "set")) {
221 st_set_trace_file(arg);
224 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
225 help_cmd(mon, "trace-file");
230 static void hmp_info_help(Monitor *mon, const QDict *qdict)
232 help_cmd(mon, "info");
235 static void monitor_init_qmp_commands(void)
239 * - qmp_commands contains all QMP commands
240 * - qmp_cap_negotiation_commands contains just
241 * "qmp_capabilities", to enforce capability negotiation
244 qmp_init_marshal(&qmp_commands);
246 qmp_register_command(&qmp_commands, "query-qmp-schema",
247 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
248 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
250 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
252 qmp_register_command(&qmp_commands, "object-add", qmp_object_add,
255 QTAILQ_INIT(&qmp_cap_negotiation_commands);
256 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
257 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
260 /* Set the current CPU defined by the user. Callers must hold BQL. */
261 int monitor_set_cpu(int cpu_index)
265 cpu = qemu_get_cpu(cpu_index);
269 g_free(cur_mon->mon_cpu_path);
270 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
274 /* Callers must hold BQL. */
275 static CPUState *mon_get_cpu_sync(bool synchronize)
277 CPUState *cpu = NULL;
279 if (cur_mon->mon_cpu_path) {
280 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
283 g_free(cur_mon->mon_cpu_path);
284 cur_mon->mon_cpu_path = NULL;
287 if (!cur_mon->mon_cpu_path) {
291 monitor_set_cpu(first_cpu->cpu_index);
296 cpu_synchronize_state(cpu);
301 CPUState *mon_get_cpu(void)
303 return mon_get_cpu_sync(true);
306 CPUArchState *mon_get_cpu_env(void)
308 CPUState *cs = mon_get_cpu();
310 return cs ? cs->env_ptr : NULL;
313 int monitor_get_cpu_index(void)
315 CPUState *cs = mon_get_cpu_sync(false);
317 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
320 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
322 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
327 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
328 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
334 monitor_printf(mon, "No CPU available\n");
338 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
343 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
345 if (!tcg_enabled()) {
346 error_report("JIT information is only available with accel=tcg");
354 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
360 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
362 int64_t max = qdict_get_try_int(qdict, "max", 10);
363 bool mean = qdict_get_try_bool(qdict, "mean", false);
364 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
365 enum QSPSortBy sort_by;
367 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
368 qsp_report(max, sort_by, coalesce);
371 static void hmp_info_history(Monitor *mon, const QDict *qdict)
373 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
382 str = readline_get_history(hmp_mon->rs, i);
386 monitor_printf(mon, "%d: '%s'\n", i, str);
391 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
393 CPUState *cs = mon_get_cpu();
396 monitor_printf(mon, "No CPU available\n");
399 cpu_dump_statistics(cs, 0);
402 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
404 const char *name = qdict_get_try_str(qdict, "name");
405 bool has_vcpu = qdict_haskey(qdict, "vcpu");
406 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
407 TraceEventInfoList *events;
408 TraceEventInfoList *elem;
409 Error *local_err = NULL;
415 monitor_printf(mon, "argument vcpu must be positive");
419 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
421 error_report_err(local_err);
425 for (elem = events; elem != NULL; elem = elem->next) {
426 monitor_printf(mon, "%s : state %u\n",
428 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
430 qapi_free_TraceEventInfoList(events);
433 void qmp_client_migrate_info(const char *protocol, const char *hostname,
434 bool has_port, int64_t port,
435 bool has_tls_port, int64_t tls_port,
436 bool has_cert_subject, const char *cert_subject,
439 if (strcmp(protocol, "spice") == 0) {
440 if (!qemu_using_spice(errp)) {
444 if (!has_port && !has_tls_port) {
445 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
449 if (qemu_spice_migrate_info(hostname,
450 has_port ? port : -1,
451 has_tls_port ? tls_port : -1,
453 error_setg(errp, QERR_UNDEFINED_ERROR);
459 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
462 static void hmp_logfile(Monitor *mon, const QDict *qdict)
466 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
468 error_report_err(err);
472 static void hmp_log(Monitor *mon, const QDict *qdict)
475 const char *items = qdict_get_str(qdict, "items");
477 if (!strcmp(items, "none")) {
480 mask = qemu_str_to_log_mask(items);
482 help_cmd(mon, "log");
489 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
491 const char *option = qdict_get_try_str(qdict, "option");
492 if (!option || !strcmp(option, "on")) {
494 } else if (!strcmp(option, "off")) {
497 monitor_printf(mon, "unexpected option %s\n", option);
501 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
503 const char *device = qdict_get_try_str(qdict, "device");
505 device = "tcp::" DEFAULT_GDBSTUB_PORT;
506 if (gdbserver_start(device) < 0) {
507 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
509 } else if (strcmp(device, "none") == 0) {
510 monitor_printf(mon, "Disabled gdbserver\n");
512 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
517 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
519 const char *action = qdict_get_str(qdict, "action");
520 if (select_watchdog_action(action) == -1) {
521 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
525 static void monitor_printc(Monitor *mon, int c)
527 monitor_printf(mon, "'");
530 monitor_printf(mon, "\\'");
533 monitor_printf(mon, "\\\\");
536 monitor_printf(mon, "\\n");
539 monitor_printf(mon, "\\r");
542 if (c >= 32 && c <= 126) {
543 monitor_printf(mon, "%c", c);
545 monitor_printf(mon, "\\x%02x", c);
549 monitor_printf(mon, "'");
552 static void memory_dump(Monitor *mon, int count, int format, int wsize,
553 hwaddr addr, int is_physical)
555 int l, line_size, i, max_digits, len;
558 CPUState *cs = mon_get_cpu();
560 if (!cs && (format == 'i' || !is_physical)) {
561 monitor_printf(mon, "Can not dump without CPU\n");
566 monitor_disas(mon, cs, addr, count, is_physical);
579 max_digits = DIV_ROUND_UP(wsize * 8, 3);
583 max_digits = (wsize * 8) / 4;
587 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
596 monitor_printf(mon, TARGET_FMT_plx ":", addr);
598 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
603 AddressSpace *as = cs ? cs->as : &address_space_memory;
604 MemTxResult r = address_space_read(as, addr,
605 MEMTXATTRS_UNSPECIFIED, buf, l);
607 monitor_printf(mon, " Cannot access memory\n");
611 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
612 monitor_printf(mon, " Cannot access memory\n");
627 v = (uint32_t)ldl_p(buf + i);
633 monitor_printf(mon, " ");
636 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
639 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
642 monitor_printf(mon, "%*" PRIu64, max_digits, v);
645 monitor_printf(mon, "%*" PRId64, max_digits, v);
648 monitor_printc(mon, v);
653 monitor_printf(mon, "\n");
659 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
661 int count = qdict_get_int(qdict, "count");
662 int format = qdict_get_int(qdict, "format");
663 int size = qdict_get_int(qdict, "size");
664 target_long addr = qdict_get_int(qdict, "addr");
666 memory_dump(mon, count, format, size, addr, 0);
669 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
671 int count = qdict_get_int(qdict, "count");
672 int format = qdict_get_int(qdict, "format");
673 int size = qdict_get_int(qdict, "size");
674 hwaddr addr = qdict_get_int(qdict, "addr");
676 memory_dump(mon, count, format, size, addr, 1);
679 static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
681 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
685 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
689 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
690 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
691 memory_region_unref(mrs.mr);
696 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
699 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
701 hwaddr addr = qdict_get_int(qdict, "addr");
702 Error *local_err = NULL;
703 MemoryRegion *mr = NULL;
706 ptr = gpa2hva(&mr, addr, &local_err);
708 error_report_err(local_err);
712 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
714 addr, mr->name, ptr);
716 memory_region_unref(mr);
719 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
721 target_ulong addr = qdict_get_int(qdict, "addr");
723 CPUState *cs = mon_get_cpu();
727 monitor_printf(mon, "No cpu\n");
731 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
733 monitor_printf(mon, "Unmapped\n");
735 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
736 gpa + (addr & ~TARGET_PAGE_MASK));
741 static uint64_t vtop(void *ptr, Error **errp)
745 uintptr_t addr = (uintptr_t) ptr;
746 uintptr_t pagesize = qemu_real_host_page_size;
747 off_t offset = addr / pagesize * sizeof(pinfo);
750 fd = open("/proc/self/pagemap", O_RDONLY);
752 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
756 /* Force copy-on-write if necessary. */
757 atomic_add((uint8_t *)ptr, 0);
759 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
760 error_setg_errno(errp, errno, "Cannot read pagemap");
763 if ((pinfo & (1ull << 63)) == 0) {
764 error_setg(errp, "Page not present");
767 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
774 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
776 hwaddr addr = qdict_get_int(qdict, "addr");
777 Error *local_err = NULL;
778 MemoryRegion *mr = NULL;
782 ptr = gpa2hva(&mr, addr, &local_err);
784 error_report_err(local_err);
788 physaddr = vtop(ptr, &local_err);
790 error_report_err(local_err);
792 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
793 " (%s) is 0x%" PRIx64 "\n",
794 addr, mr->name, (uint64_t) physaddr);
797 memory_region_unref(mr);
801 static void do_print(Monitor *mon, const QDict *qdict)
803 int format = qdict_get_int(qdict, "format");
804 hwaddr val = qdict_get_int(qdict, "val");
808 monitor_printf(mon, "%#" HWADDR_PRIo, val);
811 monitor_printf(mon, "%#" HWADDR_PRIx, val);
814 monitor_printf(mon, "%" HWADDR_PRIu, val);
818 monitor_printf(mon, "%" HWADDR_PRId, val);
821 monitor_printc(mon, val);
824 monitor_printf(mon, "\n");
827 static void hmp_sum(Monitor *mon, const QDict *qdict)
831 uint32_t start = qdict_get_int(qdict, "start");
832 uint32_t size = qdict_get_int(qdict, "size");
835 for(addr = start; addr < (start + size); addr++) {
836 uint8_t val = address_space_ldub(&address_space_memory, addr,
837 MEMTXATTRS_UNSPECIFIED, NULL);
838 /* BSD sum algorithm ('sum' Unix command) */
839 sum = (sum >> 1) | (sum << 15);
842 monitor_printf(mon, "%05d\n", sum);
845 static int mouse_button_state;
847 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
849 int dx, dy, dz, button;
850 const char *dx_str = qdict_get_str(qdict, "dx_str");
851 const char *dy_str = qdict_get_str(qdict, "dy_str");
852 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
854 dx = strtol(dx_str, NULL, 0);
855 dy = strtol(dy_str, NULL, 0);
856 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
857 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
860 dz = strtol(dz_str, NULL, 0);
862 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
863 qemu_input_queue_btn(NULL, button, true);
864 qemu_input_event_sync();
865 qemu_input_queue_btn(NULL, button, false);
868 qemu_input_event_sync();
871 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
873 static uint32_t bmap[INPUT_BUTTON__MAX] = {
874 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
875 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
876 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
878 int button_state = qdict_get_int(qdict, "button_state");
880 if (mouse_button_state == button_state) {
883 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
884 qemu_input_event_sync();
885 mouse_button_state = button_state;
888 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
890 int size = qdict_get_int(qdict, "size");
891 int addr = qdict_get_int(qdict, "addr");
892 int has_index = qdict_haskey(qdict, "index");
897 int index = qdict_get_int(qdict, "index");
898 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
918 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
919 suffix, addr, size * 2, val);
922 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
924 int size = qdict_get_int(qdict, "size");
925 int addr = qdict_get_int(qdict, "addr");
926 int val = qdict_get_int(qdict, "val");
928 addr &= IOPORTS_MASK;
944 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
946 Error *local_err = NULL;
947 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
949 qemu_boot_set(bootdevice, &local_err);
951 error_report_err(local_err);
953 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
957 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
959 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
960 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
961 bool owner = qdict_get_try_bool(qdict, "owner", false);
963 mtree_info(flatview, dispatch_tree, owner);
966 #ifdef CONFIG_PROFILER
970 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
972 static int64_t last_cpu_exec_time;
973 int64_t cpu_exec_time;
976 cpu_exec_time = tcg_cpu_exec_time();
977 delta = cpu_exec_time - last_cpu_exec_time;
979 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
980 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
981 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
982 delta, delta / (double)NANOSECONDS_PER_SECOND);
983 last_cpu_exec_time = cpu_exec_time;
987 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
989 monitor_printf(mon, "Internal profiler not compiled\n");
993 /* Capture support */
994 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
996 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1001 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1002 monitor_printf(mon, "[%d]: ", i);
1003 s->ops.info (s->opaque);
1007 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1010 int n = qdict_get_int(qdict, "n");
1013 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1015 s->ops.destroy (s->opaque);
1016 QLIST_REMOVE (s, entries);
1023 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1025 const char *path = qdict_get_str(qdict, "path");
1026 int freq = qdict_get_try_int(qdict, "freq", 44100);
1027 int bits = qdict_get_try_int(qdict, "bits", 16);
1028 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1029 const char *audiodev = qdict_get_str(qdict, "audiodev");
1031 AudioState *as = audio_state_by_name(audiodev);
1034 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1038 s = g_malloc0 (sizeof (*s));
1040 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1041 monitor_printf(mon, "Failed to add wave capture\n");
1045 QLIST_INSERT_HEAD (&capture_head, s, entries);
1048 static QAuthZList *find_auth(Monitor *mon, const char *name)
1053 container = object_get_objects_root();
1054 obj = object_resolve_path_component(container, name);
1056 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1060 return QAUTHZ_LIST(obj);
1063 static bool warn_acl;
1064 static void hmp_warn_acl(void)
1069 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1070 "commands are deprecated with no replacement. Authorization "
1071 "for VNC should be performed using the pluggable QAuthZ "
1076 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1078 const char *aclname = qdict_get_str(qdict, "aclname");
1079 QAuthZList *auth = find_auth(mon, aclname);
1080 QAuthZListRuleList *rules;
1089 monitor_printf(mon, "policy: %s\n",
1090 QAuthZListPolicy_str(auth->policy));
1092 rules = auth->rules;
1094 QAuthZListRule *rule = rules->value;
1096 monitor_printf(mon, "%zu: %s %s\n", i,
1097 QAuthZListPolicy_str(rule->policy),
1099 rules = rules->next;
1103 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1105 const char *aclname = qdict_get_str(qdict, "aclname");
1106 QAuthZList *auth = find_auth(mon, aclname);
1114 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1115 qapi_free_QAuthZListRuleList(auth->rules);
1117 monitor_printf(mon, "acl: removed all rules\n");
1120 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1122 const char *aclname = qdict_get_str(qdict, "aclname");
1123 const char *policy = qdict_get_str(qdict, "policy");
1124 QAuthZList *auth = find_auth(mon, aclname);
1134 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1136 QAUTHZ_LIST_POLICY_DENY,
1140 monitor_printf(mon, "acl: unknown policy '%s', "
1141 "expected 'deny' or 'allow'\n", policy);
1144 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
1145 monitor_printf(mon, "acl: policy set to 'allow'\n");
1147 monitor_printf(mon, "acl: policy set to 'deny'\n");
1152 static QAuthZListFormat hmp_acl_get_format(const char *match)
1154 if (strchr(match, '*')) {
1155 return QAUTHZ_LIST_FORMAT_GLOB;
1157 return QAUTHZ_LIST_FORMAT_EXACT;
1161 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1163 const char *aclname = qdict_get_str(qdict, "aclname");
1164 const char *match = qdict_get_str(qdict, "match");
1165 const char *policystr = qdict_get_str(qdict, "policy");
1166 int has_index = qdict_haskey(qdict, "index");
1167 int index = qdict_get_try_int(qdict, "index", -1);
1168 QAuthZList *auth = find_auth(mon, aclname);
1170 QAuthZListPolicy policy;
1171 QAuthZListFormat format;
1180 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1182 QAUTHZ_LIST_POLICY_DENY,
1186 monitor_printf(mon, "acl: unknown policy '%s', "
1187 "expected 'deny' or 'allow'\n", policystr);
1191 format = hmp_acl_get_format(match);
1193 if (has_index && index == 0) {
1194 monitor_printf(mon, "acl: unable to add acl entry\n");
1199 i = qauthz_list_insert_rule(auth, match, policy,
1200 format, index - 1, &err);
1202 i = qauthz_list_append_rule(auth, match, policy,
1206 monitor_printf(mon, "acl: unable to add rule: %s",
1207 error_get_pretty(err));
1210 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
1214 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1216 const char *aclname = qdict_get_str(qdict, "aclname");
1217 const char *match = qdict_get_str(qdict, "match");
1218 QAuthZList *auth = find_auth(mon, aclname);
1227 i = qauthz_list_delete_rule(auth, match);
1229 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1231 monitor_printf(mon, "acl: no matching acl entry\n");
1235 void qmp_getfd(const char *fdname, Error **errp)
1240 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1242 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1246 if (qemu_isdigit(fdname[0])) {
1248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1249 "a name not starting with a digit");
1253 qemu_mutex_lock(&cur_mon->mon_lock);
1254 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1255 if (strcmp(monfd->name, fdname) != 0) {
1261 qemu_mutex_unlock(&cur_mon->mon_lock);
1262 /* Make sure close() is outside critical section */
1267 monfd = g_malloc0(sizeof(mon_fd_t));
1268 monfd->name = g_strdup(fdname);
1271 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1272 qemu_mutex_unlock(&cur_mon->mon_lock);
1275 void qmp_closefd(const char *fdname, Error **errp)
1280 qemu_mutex_lock(&cur_mon->mon_lock);
1281 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1282 if (strcmp(monfd->name, fdname) != 0) {
1286 QLIST_REMOVE(monfd, next);
1288 g_free(monfd->name);
1290 qemu_mutex_unlock(&cur_mon->mon_lock);
1291 /* Make sure close() is outside critical section */
1296 qemu_mutex_unlock(&cur_mon->mon_lock);
1297 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1300 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1304 qemu_mutex_lock(&mon->mon_lock);
1305 QLIST_FOREACH(monfd, &mon->fds, next) {
1308 if (strcmp(monfd->name, fdname) != 0) {
1314 /* caller takes ownership of fd */
1315 QLIST_REMOVE(monfd, next);
1316 g_free(monfd->name);
1318 qemu_mutex_unlock(&mon->mon_lock);
1323 qemu_mutex_unlock(&mon->mon_lock);
1324 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1328 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1330 MonFdsetFd *mon_fdset_fd;
1331 MonFdsetFd *mon_fdset_fd_next;
1333 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1334 if ((mon_fdset_fd->removed ||
1335 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1336 runstate_is_running()) {
1337 close(mon_fdset_fd->fd);
1338 g_free(mon_fdset_fd->opaque);
1339 QLIST_REMOVE(mon_fdset_fd, next);
1340 g_free(mon_fdset_fd);
1344 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1345 QLIST_REMOVE(mon_fdset, next);
1350 void monitor_fdsets_cleanup(void)
1352 MonFdset *mon_fdset;
1353 MonFdset *mon_fdset_next;
1355 qemu_mutex_lock(&mon_fdsets_lock);
1356 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1357 monitor_fdset_cleanup(mon_fdset);
1359 qemu_mutex_unlock(&mon_fdsets_lock);
1362 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1363 const char *opaque, Error **errp)
1366 Monitor *mon = cur_mon;
1369 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1371 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1375 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1376 has_opaque, opaque, errp);
1388 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1390 MonFdset *mon_fdset;
1391 MonFdsetFd *mon_fdset_fd;
1394 qemu_mutex_lock(&mon_fdsets_lock);
1395 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1396 if (mon_fdset->id != fdset_id) {
1399 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1401 if (mon_fdset_fd->fd != fd) {
1404 mon_fdset_fd->removed = true;
1407 mon_fdset_fd->removed = true;
1410 if (has_fd && !mon_fdset_fd) {
1413 monitor_fdset_cleanup(mon_fdset);
1414 qemu_mutex_unlock(&mon_fdsets_lock);
1419 qemu_mutex_unlock(&mon_fdsets_lock);
1421 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1424 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1426 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1429 FdsetInfoList *qmp_query_fdsets(Error **errp)
1431 MonFdset *mon_fdset;
1432 MonFdsetFd *mon_fdset_fd;
1433 FdsetInfoList *fdset_list = NULL;
1435 qemu_mutex_lock(&mon_fdsets_lock);
1436 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1437 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1438 FdsetFdInfoList *fdsetfd_list = NULL;
1440 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1441 fdset_info->value->fdset_id = mon_fdset->id;
1443 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1444 FdsetFdInfoList *fdsetfd_info;
1446 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1447 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1448 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1449 if (mon_fdset_fd->opaque) {
1450 fdsetfd_info->value->has_opaque = true;
1451 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1453 fdsetfd_info->value->has_opaque = false;
1456 fdsetfd_info->next = fdsetfd_list;
1457 fdsetfd_list = fdsetfd_info;
1460 fdset_info->value->fds = fdsetfd_list;
1462 fdset_info->next = fdset_list;
1463 fdset_list = fdset_info;
1465 qemu_mutex_unlock(&mon_fdsets_lock);
1470 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1471 bool has_opaque, const char *opaque,
1474 MonFdset *mon_fdset = NULL;
1475 MonFdsetFd *mon_fdset_fd;
1478 qemu_mutex_lock(&mon_fdsets_lock);
1480 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1481 /* Break if match found or match impossible due to ordering by ID */
1482 if (fdset_id <= mon_fdset->id) {
1483 if (fdset_id < mon_fdset->id) {
1491 if (mon_fdset == NULL) {
1492 int64_t fdset_id_prev = -1;
1493 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1497 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1498 "a non-negative value");
1499 qemu_mutex_unlock(&mon_fdsets_lock);
1502 /* Use specified fdset ID */
1503 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1504 mon_fdset_cur = mon_fdset;
1505 if (fdset_id < mon_fdset_cur->id) {
1510 /* Use first available fdset ID */
1511 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1512 mon_fdset_cur = mon_fdset;
1513 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1514 fdset_id_prev = mon_fdset_cur->id;
1521 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1523 mon_fdset->id = fdset_id;
1525 mon_fdset->id = fdset_id_prev + 1;
1528 /* The fdset list is ordered by fdset ID */
1529 if (!mon_fdset_cur) {
1530 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1531 } else if (mon_fdset->id < mon_fdset_cur->id) {
1532 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1534 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1538 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1539 mon_fdset_fd->fd = fd;
1540 mon_fdset_fd->removed = false;
1542 mon_fdset_fd->opaque = g_strdup(opaque);
1544 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1546 fdinfo = g_malloc0(sizeof(*fdinfo));
1547 fdinfo->fdset_id = mon_fdset->id;
1548 fdinfo->fd = mon_fdset_fd->fd;
1550 qemu_mutex_unlock(&mon_fdsets_lock);
1554 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1559 MonFdset *mon_fdset;
1560 MonFdsetFd *mon_fdset_fd;
1564 qemu_mutex_lock(&mon_fdsets_lock);
1565 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1566 if (mon_fdset->id != fdset_id) {
1569 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1570 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1571 if (mon_fd_flags == -1) {
1576 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1577 ret = mon_fdset_fd->fd;
1587 qemu_mutex_unlock(&mon_fdsets_lock);
1592 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1594 MonFdset *mon_fdset;
1595 MonFdsetFd *mon_fdset_fd_dup;
1597 qemu_mutex_lock(&mon_fdsets_lock);
1598 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1599 if (mon_fdset->id != fdset_id) {
1602 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1603 if (mon_fdset_fd_dup->fd == dup_fd) {
1607 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1608 mon_fdset_fd_dup->fd = dup_fd;
1609 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1610 qemu_mutex_unlock(&mon_fdsets_lock);
1615 qemu_mutex_unlock(&mon_fdsets_lock);
1619 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1621 MonFdset *mon_fdset;
1622 MonFdsetFd *mon_fdset_fd_dup;
1624 qemu_mutex_lock(&mon_fdsets_lock);
1625 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1626 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1627 if (mon_fdset_fd_dup->fd == dup_fd) {
1629 QLIST_REMOVE(mon_fdset_fd_dup, next);
1630 g_free(mon_fdset_fd_dup);
1631 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1632 monitor_fdset_cleanup(mon_fdset);
1636 qemu_mutex_unlock(&mon_fdsets_lock);
1637 return mon_fdset->id;
1644 qemu_mutex_unlock(&mon_fdsets_lock);
1648 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1650 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1653 void monitor_fdset_dup_fd_remove(int dup_fd)
1655 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1658 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1661 Error *local_err = NULL;
1663 if (!qemu_isdigit(fdname[0]) && mon) {
1664 fd = monitor_get_fd(mon, fdname, &local_err);
1666 fd = qemu_parse_fd(fdname);
1668 error_setg(&local_err, "Invalid file descriptor number '%s'",
1673 error_propagate(errp, local_err);
1682 /* Please update hmp-commands.hx when adding or changing commands */
1683 static HMPCommand hmp_info_cmds[] = {
1684 #include "hmp-commands-info.h"
1688 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1689 HMPCommand hmp_cmds[] = {
1690 #include "hmp-commands.h"
1695 * Set @pval to the value in the register identified by @name.
1696 * return 0 if OK, -1 if not found
1698 int get_monitor_def(int64_t *pval, const char *name)
1700 const MonitorDef *md = target_monitor_defs();
1701 CPUState *cs = mon_get_cpu();
1706 if (cs == NULL || md == NULL) {
1710 for(; md->name != NULL; md++) {
1711 if (hmp_compare_cmd(name, md->name)) {
1712 if (md->get_value) {
1713 *pval = md->get_value(md, md->offset);
1715 CPUArchState *env = mon_get_cpu_env();
1716 ptr = (uint8_t *)env + md->offset;
1719 *pval = *(int32_t *)ptr;
1722 *pval = *(target_long *)ptr;
1733 ret = target_get_monitor_def(cs, name, &tmp);
1735 *pval = (target_long) tmp;
1741 static void add_completion_option(ReadLineState *rs, const char *str,
1744 if (!str || !option) {
1747 if (!strncmp(option, str, strlen(str))) {
1748 readline_add_completion(rs, option);
1752 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1755 ChardevBackendInfoList *list, *start;
1761 readline_set_completion_index(rs, len);
1763 start = list = qmp_query_chardev_backends(NULL);
1765 const char *chr_name = list->value->name;
1767 if (!strncmp(chr_name, str, len)) {
1768 readline_add_completion(rs, chr_name);
1772 qapi_free_ChardevBackendInfoList(start);
1775 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1784 readline_set_completion_index(rs, len);
1785 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1786 add_completion_option(rs, str, NetClientDriver_str(i));
1790 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1800 readline_set_completion_index(rs, len);
1801 list = elt = object_class_get_list(TYPE_DEVICE, false);
1804 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1806 name = object_class_get_name(OBJECT_CLASS(dc));
1808 if (dc->user_creatable
1809 && !strncmp(name, str, len)) {
1810 readline_add_completion(rs, name);
1817 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1827 readline_set_completion_index(rs, len);
1828 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1832 name = object_class_get_name(OBJECT_CLASS(elt->data));
1833 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1834 readline_add_completion(rs, name);
1841 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1843 GSList **list = opaque;
1844 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
1851 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1852 *list = g_slist_append(*list, dev);
1858 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1860 GSList *list = NULL;
1862 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1867 static void peripheral_device_del_completion(ReadLineState *rs,
1868 const char *str, size_t len)
1870 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1871 GSList *list, *item;
1873 list = qdev_build_hotpluggable_device_list(peripheral);
1878 for (item = list; item; item = g_slist_next(item)) {
1879 DeviceState *dev = item->data;
1881 if (dev->id && !strncmp(str, dev->id, len)) {
1882 readline_add_completion(rs, dev->id);
1889 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1892 ChardevInfoList *list, *start;
1898 readline_set_completion_index(rs, len);
1900 start = list = qmp_query_chardev(NULL);
1902 ChardevInfo *chr = list->value;
1904 if (!strncmp(chr->label, str, len)) {
1905 readline_add_completion(rs, chr->label);
1909 qapi_free_ChardevInfoList(start);
1912 static void ringbuf_completion(ReadLineState *rs, const char *str)
1915 ChardevInfoList *list, *start;
1918 readline_set_completion_index(rs, len);
1920 start = list = qmp_query_chardev(NULL);
1922 ChardevInfo *chr_info = list->value;
1924 if (!strncmp(chr_info->label, str, len)) {
1925 Chardev *chr = qemu_chr_find(chr_info->label);
1926 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1927 readline_add_completion(rs, chr_info->label);
1932 qapi_free_ChardevInfoList(start);
1935 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1940 ringbuf_completion(rs, str);
1943 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1952 readline_set_completion_index(rs, len);
1953 peripheral_device_del_completion(rs, str, len);
1956 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1958 ObjectPropertyInfoList *list, *start;
1965 readline_set_completion_index(rs, len);
1967 start = list = qmp_qom_list("/objects", NULL);
1969 ObjectPropertyInfo *info = list->value;
1971 if (!strncmp(info->type, "child<", 5)
1972 && !strncmp(info->name, str, len)) {
1973 readline_add_completion(rs, info->name);
1977 qapi_free_ObjectPropertyInfoList(start);
1980 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1989 sep = strrchr(str, '-');
1994 readline_set_completion_index(rs, len);
1995 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1996 if (!strncmp(str, QKeyCode_str(i), len)) {
1997 readline_add_completion(rs, QKeyCode_str(i));
2002 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
2007 readline_set_completion_index(rs, len);
2009 NetClientState *ncs[MAX_QUEUE_NUM];
2011 count = qemu_find_net_clients_except(NULL, ncs,
2012 NET_CLIENT_DRIVER_NONE,
2014 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2015 const char *name = ncs[i]->name;
2016 if (!strncmp(str, name, len)) {
2017 readline_add_completion(rs, name);
2020 } else if (nb_args == 3) {
2021 add_completion_option(rs, str, "on");
2022 add_completion_option(rs, str, "off");
2026 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
2029 NetClientState *ncs[MAX_QUEUE_NUM];
2036 readline_set_completion_index(rs, len);
2037 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
2039 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2041 const char *name = ncs[i]->name;
2042 if (strncmp(str, name, len)) {
2045 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
2047 readline_add_completion(rs, name);
2052 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2057 readline_set_completion_index(rs, len);
2059 TraceEventIter iter;
2061 char *pattern = g_strdup_printf("%s*", str);
2062 trace_event_iter_init(&iter, pattern);
2063 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2064 readline_add_completion(rs, trace_event_get_name(ev));
2070 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2075 readline_set_completion_index(rs, len);
2077 TraceEventIter iter;
2079 char *pattern = g_strdup_printf("%s*", str);
2080 trace_event_iter_init(&iter, pattern);
2081 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2082 readline_add_completion(rs, trace_event_get_name(ev));
2085 } else if (nb_args == 3) {
2086 add_completion_option(rs, str, "on");
2087 add_completion_option(rs, str, "off");
2091 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2098 readline_set_completion_index(rs, strlen(str));
2099 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2100 add_completion_option(rs, str, WatchdogAction_str(i));
2104 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2110 readline_set_completion_index(rs, len);
2113 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2114 const char *name = MigrationCapability_str(i);
2115 if (!strncmp(str, name, len)) {
2116 readline_add_completion(rs, name);
2119 } else if (nb_args == 3) {
2120 add_completion_option(rs, str, "on");
2121 add_completion_option(rs, str, "off");
2125 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2131 readline_set_completion_index(rs, len);
2134 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
2135 const char *name = MigrationParameter_str(i);
2136 if (!strncmp(str, name, len)) {
2137 readline_add_completion(rs, name);
2143 static void vm_completion(ReadLineState *rs, const char *str)
2146 BlockDriverState *bs;
2147 BdrvNextIterator it;
2150 readline_set_completion_index(rs, len);
2152 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2153 SnapshotInfoList *snapshots, *snapshot;
2154 AioContext *ctx = bdrv_get_aio_context(bs);
2157 aio_context_acquire(ctx);
2158 if (bdrv_can_snapshot(bs)) {
2159 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2161 aio_context_release(ctx);
2166 snapshot = snapshots;
2168 char *completion = snapshot->value->name;
2169 if (!strncmp(str, completion, len)) {
2170 readline_add_completion(rs, completion);
2172 completion = snapshot->value->id;
2173 if (!strncmp(str, completion, len)) {
2174 readline_add_completion(rs, completion);
2176 snapshot = snapshot->next;
2178 qapi_free_SnapshotInfoList(snapshots);
2183 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2186 vm_completion(rs, str);
2190 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2193 vm_completion(rs, str);
2198 compare_mon_cmd(const void *a, const void *b)
2200 return strcmp(((const HMPCommand *)a)->name,
2201 ((const HMPCommand *)b)->name);
2204 static void sortcmdlist(void)
2206 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2209 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2210 sizeof(*hmp_info_cmds),
2214 void monitor_init_globals(void)
2216 monitor_init_globals_core();
2217 monitor_init_qmp_commands();
2219 qemu_mutex_init(&mon_fdsets_lock);