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-char.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-trace.h"
74 #include "qapi/qapi-emit-events.h"
75 #include "qapi/qapi-init-commands.h"
76 #include "qapi/error.h"
77 #include "qapi/qmp-event.h"
78 #include "qapi/qapi-introspect.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 query_commands_cb(QmpCommand *cmd, void *opaque)
237 CommandInfoList *info, **list = opaque;
243 info = g_malloc0(sizeof(*info));
244 info->value = g_malloc0(sizeof(*info->value));
245 info->value->name = g_strdup(cmd->name);
250 CommandInfoList *qmp_query_commands(Error **errp)
252 CommandInfoList *list = NULL;
255 assert(monitor_is_qmp(cur_mon));
256 mon = container_of(cur_mon, MonitorQMP, common);
258 qmp_for_each_command(mon->commands, query_commands_cb, &list);
263 EventInfoList *qmp_query_events(Error **errp)
266 * TODO This deprecated command is the only user of
267 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
268 * they should go, too.
270 EventInfoList *info, *ev_list = NULL;
273 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
274 const char *event_name = QAPIEvent_str(e);
275 assert(event_name != NULL);
276 info = g_malloc0(sizeof(*info));
277 info->value = g_malloc0(sizeof(*info->value));
278 info->value->name = g_strdup(event_name);
280 info->next = ev_list;
288 * Minor hack: generated marshalling suppressed for this command
289 * ('gen': false in the schema) so we can parse the JSON string
290 * directly into QObject instead of first parsing it with
291 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
292 * to QObject with generated output marshallers, every time. Instead,
293 * we do it in test-qobject-input-visitor.c, just to make sure
294 * qapi-gen.py's output actually conforms to the schema.
296 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
299 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
302 static void monitor_init_qmp_commands(void)
306 * - qmp_commands contains all QMP commands
307 * - qmp_cap_negotiation_commands contains just
308 * "qmp_capabilities", to enforce capability negotiation
311 qmp_init_marshal(&qmp_commands);
313 qmp_register_command(&qmp_commands, "query-qmp-schema",
314 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
315 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
317 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
320 QTAILQ_INIT(&qmp_cap_negotiation_commands);
321 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
322 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
326 * Accept QMP capabilities in @list for @mon.
327 * On success, set mon->qmp.capab[], and return true.
328 * On error, set @errp, and return false.
330 static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
333 GString *unavailable = NULL;
334 bool capab[QMP_CAPABILITY__MAX];
336 memset(capab, 0, sizeof(capab));
338 for (; list; list = list->next) {
339 if (!mon->capab_offered[list->value]) {
341 unavailable = g_string_new(QMPCapability_str(list->value));
343 g_string_append_printf(unavailable, ", %s",
344 QMPCapability_str(list->value));
347 capab[list->value] = true;
351 error_setg(errp, "Capability %s not available", unavailable->str);
352 g_string_free(unavailable, true);
356 memcpy(mon->capab, capab, sizeof(capab));
360 void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
365 assert(monitor_is_qmp(cur_mon));
366 mon = container_of(cur_mon, MonitorQMP, common);
368 if (mon->commands == &qmp_commands) {
369 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
370 "Capabilities negotiation is already complete, command "
375 if (!qmp_caps_accept(mon, enable, errp)) {
379 mon->commands = &qmp_commands;
382 /* Set the current CPU defined by the user. Callers must hold BQL. */
383 int monitor_set_cpu(int cpu_index)
387 cpu = qemu_get_cpu(cpu_index);
391 g_free(cur_mon->mon_cpu_path);
392 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
396 /* Callers must hold BQL. */
397 static CPUState *mon_get_cpu_sync(bool synchronize)
399 CPUState *cpu = NULL;
401 if (cur_mon->mon_cpu_path) {
402 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
405 g_free(cur_mon->mon_cpu_path);
406 cur_mon->mon_cpu_path = NULL;
409 if (!cur_mon->mon_cpu_path) {
413 monitor_set_cpu(first_cpu->cpu_index);
418 cpu_synchronize_state(cpu);
423 CPUState *mon_get_cpu(void)
425 return mon_get_cpu_sync(true);
428 CPUArchState *mon_get_cpu_env(void)
430 CPUState *cs = mon_get_cpu();
432 return cs ? cs->env_ptr : NULL;
435 int monitor_get_cpu_index(void)
437 CPUState *cs = mon_get_cpu_sync(false);
439 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
442 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
444 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
449 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
450 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
456 monitor_printf(mon, "No CPU available\n");
460 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
465 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
467 if (!tcg_enabled()) {
468 error_report("JIT information is only available with accel=tcg");
476 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
482 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
484 int64_t max = qdict_get_try_int(qdict, "max", 10);
485 bool mean = qdict_get_try_bool(qdict, "mean", false);
486 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
487 enum QSPSortBy sort_by;
489 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
490 qsp_report(max, sort_by, coalesce);
493 static void hmp_info_history(Monitor *mon, const QDict *qdict)
495 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
504 str = readline_get_history(hmp_mon->rs, i);
508 monitor_printf(mon, "%d: '%s'\n", i, str);
513 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
515 CPUState *cs = mon_get_cpu();
518 monitor_printf(mon, "No CPU available\n");
521 cpu_dump_statistics(cs, 0);
524 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
526 const char *name = qdict_get_try_str(qdict, "name");
527 bool has_vcpu = qdict_haskey(qdict, "vcpu");
528 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
529 TraceEventInfoList *events;
530 TraceEventInfoList *elem;
531 Error *local_err = NULL;
537 monitor_printf(mon, "argument vcpu must be positive");
541 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
543 error_report_err(local_err);
547 for (elem = events; elem != NULL; elem = elem->next) {
548 monitor_printf(mon, "%s : state %u\n",
550 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
552 qapi_free_TraceEventInfoList(events);
555 void qmp_client_migrate_info(const char *protocol, const char *hostname,
556 bool has_port, int64_t port,
557 bool has_tls_port, int64_t tls_port,
558 bool has_cert_subject, const char *cert_subject,
561 if (strcmp(protocol, "spice") == 0) {
562 if (!qemu_using_spice(errp)) {
566 if (!has_port && !has_tls_port) {
567 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
571 if (qemu_spice_migrate_info(hostname,
572 has_port ? port : -1,
573 has_tls_port ? tls_port : -1,
575 error_setg(errp, QERR_UNDEFINED_ERROR);
581 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
584 static void hmp_logfile(Monitor *mon, const QDict *qdict)
588 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
590 error_report_err(err);
594 static void hmp_log(Monitor *mon, const QDict *qdict)
597 const char *items = qdict_get_str(qdict, "items");
599 if (!strcmp(items, "none")) {
602 mask = qemu_str_to_log_mask(items);
604 help_cmd(mon, "log");
611 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
613 const char *option = qdict_get_try_str(qdict, "option");
614 if (!option || !strcmp(option, "on")) {
616 } else if (!strcmp(option, "off")) {
619 monitor_printf(mon, "unexpected option %s\n", option);
623 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
625 const char *device = qdict_get_try_str(qdict, "device");
627 device = "tcp::" DEFAULT_GDBSTUB_PORT;
628 if (gdbserver_start(device) < 0) {
629 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
631 } else if (strcmp(device, "none") == 0) {
632 monitor_printf(mon, "Disabled gdbserver\n");
634 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
639 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
641 const char *action = qdict_get_str(qdict, "action");
642 if (select_watchdog_action(action) == -1) {
643 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
647 static void monitor_printc(Monitor *mon, int c)
649 monitor_printf(mon, "'");
652 monitor_printf(mon, "\\'");
655 monitor_printf(mon, "\\\\");
658 monitor_printf(mon, "\\n");
661 monitor_printf(mon, "\\r");
664 if (c >= 32 && c <= 126) {
665 monitor_printf(mon, "%c", c);
667 monitor_printf(mon, "\\x%02x", c);
671 monitor_printf(mon, "'");
674 static void memory_dump(Monitor *mon, int count, int format, int wsize,
675 hwaddr addr, int is_physical)
677 int l, line_size, i, max_digits, len;
680 CPUState *cs = mon_get_cpu();
682 if (!cs && (format == 'i' || !is_physical)) {
683 monitor_printf(mon, "Can not dump without CPU\n");
688 monitor_disas(mon, cs, addr, count, is_physical);
701 max_digits = DIV_ROUND_UP(wsize * 8, 3);
705 max_digits = (wsize * 8) / 4;
709 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
718 monitor_printf(mon, TARGET_FMT_plx ":", addr);
720 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
725 AddressSpace *as = cs ? cs->as : &address_space_memory;
726 MemTxResult r = address_space_read(as, addr,
727 MEMTXATTRS_UNSPECIFIED, buf, l);
729 monitor_printf(mon, " Cannot access memory\n");
733 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
734 monitor_printf(mon, " Cannot access memory\n");
749 v = (uint32_t)ldl_p(buf + i);
755 monitor_printf(mon, " ");
758 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
761 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
764 monitor_printf(mon, "%*" PRIu64, max_digits, v);
767 monitor_printf(mon, "%*" PRId64, max_digits, v);
770 monitor_printc(mon, v);
775 monitor_printf(mon, "\n");
781 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
783 int count = qdict_get_int(qdict, "count");
784 int format = qdict_get_int(qdict, "format");
785 int size = qdict_get_int(qdict, "size");
786 target_long addr = qdict_get_int(qdict, "addr");
788 memory_dump(mon, count, format, size, addr, 0);
791 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
793 int count = qdict_get_int(qdict, "count");
794 int format = qdict_get_int(qdict, "format");
795 int size = qdict_get_int(qdict, "size");
796 hwaddr addr = qdict_get_int(qdict, "addr");
798 memory_dump(mon, count, format, size, addr, 1);
801 static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
803 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
807 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
811 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
812 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
813 memory_region_unref(mrs.mr);
818 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
821 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
823 hwaddr addr = qdict_get_int(qdict, "addr");
824 Error *local_err = NULL;
825 MemoryRegion *mr = NULL;
828 ptr = gpa2hva(&mr, addr, &local_err);
830 error_report_err(local_err);
834 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
836 addr, mr->name, ptr);
838 memory_region_unref(mr);
841 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
843 target_ulong addr = qdict_get_int(qdict, "addr");
845 CPUState *cs = mon_get_cpu();
849 monitor_printf(mon, "No cpu\n");
853 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
855 monitor_printf(mon, "Unmapped\n");
857 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
858 gpa + (addr & ~TARGET_PAGE_MASK));
863 static uint64_t vtop(void *ptr, Error **errp)
867 uintptr_t addr = (uintptr_t) ptr;
868 uintptr_t pagesize = qemu_real_host_page_size;
869 off_t offset = addr / pagesize * sizeof(pinfo);
872 fd = open("/proc/self/pagemap", O_RDONLY);
874 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
878 /* Force copy-on-write if necessary. */
879 atomic_add((uint8_t *)ptr, 0);
881 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
882 error_setg_errno(errp, errno, "Cannot read pagemap");
885 if ((pinfo & (1ull << 63)) == 0) {
886 error_setg(errp, "Page not present");
889 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
896 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
898 hwaddr addr = qdict_get_int(qdict, "addr");
899 Error *local_err = NULL;
900 MemoryRegion *mr = NULL;
904 ptr = gpa2hva(&mr, addr, &local_err);
906 error_report_err(local_err);
910 physaddr = vtop(ptr, &local_err);
912 error_report_err(local_err);
914 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
915 " (%s) is 0x%" PRIx64 "\n",
916 addr, mr->name, (uint64_t) physaddr);
919 memory_region_unref(mr);
923 static void do_print(Monitor *mon, const QDict *qdict)
925 int format = qdict_get_int(qdict, "format");
926 hwaddr val = qdict_get_int(qdict, "val");
930 monitor_printf(mon, "%#" HWADDR_PRIo, val);
933 monitor_printf(mon, "%#" HWADDR_PRIx, val);
936 monitor_printf(mon, "%" HWADDR_PRIu, val);
940 monitor_printf(mon, "%" HWADDR_PRId, val);
943 monitor_printc(mon, val);
946 monitor_printf(mon, "\n");
949 static void hmp_sum(Monitor *mon, const QDict *qdict)
953 uint32_t start = qdict_get_int(qdict, "start");
954 uint32_t size = qdict_get_int(qdict, "size");
957 for(addr = start; addr < (start + size); addr++) {
958 uint8_t val = address_space_ldub(&address_space_memory, addr,
959 MEMTXATTRS_UNSPECIFIED, NULL);
960 /* BSD sum algorithm ('sum' Unix command) */
961 sum = (sum >> 1) | (sum << 15);
964 monitor_printf(mon, "%05d\n", sum);
967 static int mouse_button_state;
969 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
971 int dx, dy, dz, button;
972 const char *dx_str = qdict_get_str(qdict, "dx_str");
973 const char *dy_str = qdict_get_str(qdict, "dy_str");
974 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
976 dx = strtol(dx_str, NULL, 0);
977 dy = strtol(dy_str, NULL, 0);
978 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
979 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
982 dz = strtol(dz_str, NULL, 0);
984 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
985 qemu_input_queue_btn(NULL, button, true);
986 qemu_input_event_sync();
987 qemu_input_queue_btn(NULL, button, false);
990 qemu_input_event_sync();
993 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
995 static uint32_t bmap[INPUT_BUTTON__MAX] = {
996 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
997 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
998 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1000 int button_state = qdict_get_int(qdict, "button_state");
1002 if (mouse_button_state == button_state) {
1005 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1006 qemu_input_event_sync();
1007 mouse_button_state = button_state;
1010 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1012 int size = qdict_get_int(qdict, "size");
1013 int addr = qdict_get_int(qdict, "addr");
1014 int has_index = qdict_haskey(qdict, "index");
1019 int index = qdict_get_int(qdict, "index");
1020 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1028 val = cpu_inb(addr);
1032 val = cpu_inw(addr);
1036 val = cpu_inl(addr);
1040 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1041 suffix, addr, size * 2, val);
1044 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1046 int size = qdict_get_int(qdict, "size");
1047 int addr = qdict_get_int(qdict, "addr");
1048 int val = qdict_get_int(qdict, "val");
1050 addr &= IOPORTS_MASK;
1055 cpu_outb(addr, val);
1058 cpu_outw(addr, val);
1061 cpu_outl(addr, val);
1066 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1068 Error *local_err = NULL;
1069 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1071 qemu_boot_set(bootdevice, &local_err);
1073 error_report_err(local_err);
1075 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1079 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1081 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1082 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
1083 bool owner = qdict_get_try_bool(qdict, "owner", false);
1085 mtree_info(flatview, dispatch_tree, owner);
1088 #ifdef CONFIG_PROFILER
1092 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1094 static int64_t last_cpu_exec_time;
1095 int64_t cpu_exec_time;
1098 cpu_exec_time = tcg_cpu_exec_time();
1099 delta = cpu_exec_time - last_cpu_exec_time;
1101 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1102 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1103 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1104 delta, delta / (double)NANOSECONDS_PER_SECOND);
1105 last_cpu_exec_time = cpu_exec_time;
1109 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1111 monitor_printf(mon, "Internal profiler not compiled\n");
1115 /* Capture support */
1116 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1118 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1123 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1124 monitor_printf(mon, "[%d]: ", i);
1125 s->ops.info (s->opaque);
1129 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1132 int n = qdict_get_int(qdict, "n");
1135 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1137 s->ops.destroy (s->opaque);
1138 QLIST_REMOVE (s, entries);
1145 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1147 const char *path = qdict_get_str(qdict, "path");
1148 int freq = qdict_get_try_int(qdict, "freq", 44100);
1149 int bits = qdict_get_try_int(qdict, "bits", 16);
1150 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1151 const char *audiodev = qdict_get_str(qdict, "audiodev");
1153 AudioState *as = audio_state_by_name(audiodev);
1156 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1160 s = g_malloc0 (sizeof (*s));
1162 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1163 monitor_printf(mon, "Failed to add wave capture\n");
1167 QLIST_INSERT_HEAD (&capture_head, s, entries);
1170 static QAuthZList *find_auth(Monitor *mon, const char *name)
1175 container = object_get_objects_root();
1176 obj = object_resolve_path_component(container, name);
1178 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1182 return QAUTHZ_LIST(obj);
1185 static bool warn_acl;
1186 static void hmp_warn_acl(void)
1191 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1192 "commands are deprecated with no replacement. Authorization "
1193 "for VNC should be performed using the pluggable QAuthZ "
1198 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1200 const char *aclname = qdict_get_str(qdict, "aclname");
1201 QAuthZList *auth = find_auth(mon, aclname);
1202 QAuthZListRuleList *rules;
1211 monitor_printf(mon, "policy: %s\n",
1212 QAuthZListPolicy_str(auth->policy));
1214 rules = auth->rules;
1216 QAuthZListRule *rule = rules->value;
1218 monitor_printf(mon, "%zu: %s %s\n", i,
1219 QAuthZListPolicy_str(rule->policy),
1221 rules = rules->next;
1225 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1227 const char *aclname = qdict_get_str(qdict, "aclname");
1228 QAuthZList *auth = find_auth(mon, aclname);
1236 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1237 qapi_free_QAuthZListRuleList(auth->rules);
1239 monitor_printf(mon, "acl: removed all rules\n");
1242 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1244 const char *aclname = qdict_get_str(qdict, "aclname");
1245 const char *policy = qdict_get_str(qdict, "policy");
1246 QAuthZList *auth = find_auth(mon, aclname);
1256 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1258 QAUTHZ_LIST_POLICY_DENY,
1262 monitor_printf(mon, "acl: unknown policy '%s', "
1263 "expected 'deny' or 'allow'\n", policy);
1266 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
1267 monitor_printf(mon, "acl: policy set to 'allow'\n");
1269 monitor_printf(mon, "acl: policy set to 'deny'\n");
1274 static QAuthZListFormat hmp_acl_get_format(const char *match)
1276 if (strchr(match, '*')) {
1277 return QAUTHZ_LIST_FORMAT_GLOB;
1279 return QAUTHZ_LIST_FORMAT_EXACT;
1283 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1285 const char *aclname = qdict_get_str(qdict, "aclname");
1286 const char *match = qdict_get_str(qdict, "match");
1287 const char *policystr = qdict_get_str(qdict, "policy");
1288 int has_index = qdict_haskey(qdict, "index");
1289 int index = qdict_get_try_int(qdict, "index", -1);
1290 QAuthZList *auth = find_auth(mon, aclname);
1292 QAuthZListPolicy policy;
1293 QAuthZListFormat format;
1302 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1304 QAUTHZ_LIST_POLICY_DENY,
1308 monitor_printf(mon, "acl: unknown policy '%s', "
1309 "expected 'deny' or 'allow'\n", policystr);
1313 format = hmp_acl_get_format(match);
1315 if (has_index && index == 0) {
1316 monitor_printf(mon, "acl: unable to add acl entry\n");
1321 i = qauthz_list_insert_rule(auth, match, policy,
1322 format, index - 1, &err);
1324 i = qauthz_list_append_rule(auth, match, policy,
1328 monitor_printf(mon, "acl: unable to add rule: %s",
1329 error_get_pretty(err));
1332 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
1336 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1338 const char *aclname = qdict_get_str(qdict, "aclname");
1339 const char *match = qdict_get_str(qdict, "match");
1340 QAuthZList *auth = find_auth(mon, aclname);
1349 i = qauthz_list_delete_rule(auth, match);
1351 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1353 monitor_printf(mon, "acl: no matching acl entry\n");
1357 void qmp_getfd(const char *fdname, Error **errp)
1362 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1364 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1368 if (qemu_isdigit(fdname[0])) {
1370 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1371 "a name not starting with a digit");
1375 qemu_mutex_lock(&cur_mon->mon_lock);
1376 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1377 if (strcmp(monfd->name, fdname) != 0) {
1383 qemu_mutex_unlock(&cur_mon->mon_lock);
1384 /* Make sure close() is outside critical section */
1389 monfd = g_malloc0(sizeof(mon_fd_t));
1390 monfd->name = g_strdup(fdname);
1393 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1394 qemu_mutex_unlock(&cur_mon->mon_lock);
1397 void qmp_closefd(const char *fdname, Error **errp)
1402 qemu_mutex_lock(&cur_mon->mon_lock);
1403 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1404 if (strcmp(monfd->name, fdname) != 0) {
1408 QLIST_REMOVE(monfd, next);
1410 g_free(monfd->name);
1412 qemu_mutex_unlock(&cur_mon->mon_lock);
1413 /* Make sure close() is outside critical section */
1418 qemu_mutex_unlock(&cur_mon->mon_lock);
1419 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1422 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1426 qemu_mutex_lock(&mon->mon_lock);
1427 QLIST_FOREACH(monfd, &mon->fds, next) {
1430 if (strcmp(monfd->name, fdname) != 0) {
1436 /* caller takes ownership of fd */
1437 QLIST_REMOVE(monfd, next);
1438 g_free(monfd->name);
1440 qemu_mutex_unlock(&mon->mon_lock);
1445 qemu_mutex_unlock(&mon->mon_lock);
1446 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1450 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1452 MonFdsetFd *mon_fdset_fd;
1453 MonFdsetFd *mon_fdset_fd_next;
1455 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1456 if ((mon_fdset_fd->removed ||
1457 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1458 runstate_is_running()) {
1459 close(mon_fdset_fd->fd);
1460 g_free(mon_fdset_fd->opaque);
1461 QLIST_REMOVE(mon_fdset_fd, next);
1462 g_free(mon_fdset_fd);
1466 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1467 QLIST_REMOVE(mon_fdset, next);
1472 void monitor_fdsets_cleanup(void)
1474 MonFdset *mon_fdset;
1475 MonFdset *mon_fdset_next;
1477 qemu_mutex_lock(&mon_fdsets_lock);
1478 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1479 monitor_fdset_cleanup(mon_fdset);
1481 qemu_mutex_unlock(&mon_fdsets_lock);
1484 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1485 const char *opaque, Error **errp)
1488 Monitor *mon = cur_mon;
1491 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1493 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1497 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1498 has_opaque, opaque, errp);
1510 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1512 MonFdset *mon_fdset;
1513 MonFdsetFd *mon_fdset_fd;
1516 qemu_mutex_lock(&mon_fdsets_lock);
1517 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1518 if (mon_fdset->id != fdset_id) {
1521 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1523 if (mon_fdset_fd->fd != fd) {
1526 mon_fdset_fd->removed = true;
1529 mon_fdset_fd->removed = true;
1532 if (has_fd && !mon_fdset_fd) {
1535 monitor_fdset_cleanup(mon_fdset);
1536 qemu_mutex_unlock(&mon_fdsets_lock);
1541 qemu_mutex_unlock(&mon_fdsets_lock);
1543 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1546 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1548 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1551 FdsetInfoList *qmp_query_fdsets(Error **errp)
1553 MonFdset *mon_fdset;
1554 MonFdsetFd *mon_fdset_fd;
1555 FdsetInfoList *fdset_list = NULL;
1557 qemu_mutex_lock(&mon_fdsets_lock);
1558 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1559 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1560 FdsetFdInfoList *fdsetfd_list = NULL;
1562 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1563 fdset_info->value->fdset_id = mon_fdset->id;
1565 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1566 FdsetFdInfoList *fdsetfd_info;
1568 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1569 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1570 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1571 if (mon_fdset_fd->opaque) {
1572 fdsetfd_info->value->has_opaque = true;
1573 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1575 fdsetfd_info->value->has_opaque = false;
1578 fdsetfd_info->next = fdsetfd_list;
1579 fdsetfd_list = fdsetfd_info;
1582 fdset_info->value->fds = fdsetfd_list;
1584 fdset_info->next = fdset_list;
1585 fdset_list = fdset_info;
1587 qemu_mutex_unlock(&mon_fdsets_lock);
1592 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1593 bool has_opaque, const char *opaque,
1596 MonFdset *mon_fdset = NULL;
1597 MonFdsetFd *mon_fdset_fd;
1600 qemu_mutex_lock(&mon_fdsets_lock);
1602 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1603 /* Break if match found or match impossible due to ordering by ID */
1604 if (fdset_id <= mon_fdset->id) {
1605 if (fdset_id < mon_fdset->id) {
1613 if (mon_fdset == NULL) {
1614 int64_t fdset_id_prev = -1;
1615 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1619 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1620 "a non-negative value");
1621 qemu_mutex_unlock(&mon_fdsets_lock);
1624 /* Use specified fdset ID */
1625 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1626 mon_fdset_cur = mon_fdset;
1627 if (fdset_id < mon_fdset_cur->id) {
1632 /* Use first available fdset ID */
1633 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1634 mon_fdset_cur = mon_fdset;
1635 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1636 fdset_id_prev = mon_fdset_cur->id;
1643 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1645 mon_fdset->id = fdset_id;
1647 mon_fdset->id = fdset_id_prev + 1;
1650 /* The fdset list is ordered by fdset ID */
1651 if (!mon_fdset_cur) {
1652 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1653 } else if (mon_fdset->id < mon_fdset_cur->id) {
1654 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1656 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1660 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1661 mon_fdset_fd->fd = fd;
1662 mon_fdset_fd->removed = false;
1664 mon_fdset_fd->opaque = g_strdup(opaque);
1666 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1668 fdinfo = g_malloc0(sizeof(*fdinfo));
1669 fdinfo->fdset_id = mon_fdset->id;
1670 fdinfo->fd = mon_fdset_fd->fd;
1672 qemu_mutex_unlock(&mon_fdsets_lock);
1676 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1681 MonFdset *mon_fdset;
1682 MonFdsetFd *mon_fdset_fd;
1686 qemu_mutex_lock(&mon_fdsets_lock);
1687 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1688 if (mon_fdset->id != fdset_id) {
1691 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1692 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1693 if (mon_fd_flags == -1) {
1698 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1699 ret = mon_fdset_fd->fd;
1709 qemu_mutex_unlock(&mon_fdsets_lock);
1714 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1716 MonFdset *mon_fdset;
1717 MonFdsetFd *mon_fdset_fd_dup;
1719 qemu_mutex_lock(&mon_fdsets_lock);
1720 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1721 if (mon_fdset->id != fdset_id) {
1724 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1725 if (mon_fdset_fd_dup->fd == dup_fd) {
1729 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1730 mon_fdset_fd_dup->fd = dup_fd;
1731 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1732 qemu_mutex_unlock(&mon_fdsets_lock);
1737 qemu_mutex_unlock(&mon_fdsets_lock);
1741 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1743 MonFdset *mon_fdset;
1744 MonFdsetFd *mon_fdset_fd_dup;
1746 qemu_mutex_lock(&mon_fdsets_lock);
1747 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1748 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1749 if (mon_fdset_fd_dup->fd == dup_fd) {
1751 QLIST_REMOVE(mon_fdset_fd_dup, next);
1752 g_free(mon_fdset_fd_dup);
1753 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1754 monitor_fdset_cleanup(mon_fdset);
1758 qemu_mutex_unlock(&mon_fdsets_lock);
1759 return mon_fdset->id;
1766 qemu_mutex_unlock(&mon_fdsets_lock);
1770 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1772 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1775 void monitor_fdset_dup_fd_remove(int dup_fd)
1777 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1780 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1783 Error *local_err = NULL;
1785 if (!qemu_isdigit(fdname[0]) && mon) {
1786 fd = monitor_get_fd(mon, fdname, &local_err);
1788 fd = qemu_parse_fd(fdname);
1790 error_setg(&local_err, "Invalid file descriptor number '%s'",
1795 error_propagate(errp, local_err);
1804 /* Please update hmp-commands.hx when adding or changing commands */
1805 static HMPCommand hmp_info_cmds[] = {
1806 #include "hmp-commands-info.h"
1810 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1811 HMPCommand hmp_cmds[] = {
1812 #include "hmp-commands.h"
1817 * Set @pval to the value in the register identified by @name.
1818 * return 0 if OK, -1 if not found
1820 int get_monitor_def(int64_t *pval, const char *name)
1822 const MonitorDef *md = target_monitor_defs();
1823 CPUState *cs = mon_get_cpu();
1828 if (cs == NULL || md == NULL) {
1832 for(; md->name != NULL; md++) {
1833 if (hmp_compare_cmd(name, md->name)) {
1834 if (md->get_value) {
1835 *pval = md->get_value(md, md->offset);
1837 CPUArchState *env = mon_get_cpu_env();
1838 ptr = (uint8_t *)env + md->offset;
1841 *pval = *(int32_t *)ptr;
1844 *pval = *(target_long *)ptr;
1855 ret = target_get_monitor_def(cs, name, &tmp);
1857 *pval = (target_long) tmp;
1863 static void add_completion_option(ReadLineState *rs, const char *str,
1866 if (!str || !option) {
1869 if (!strncmp(option, str, strlen(str))) {
1870 readline_add_completion(rs, option);
1874 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1877 ChardevBackendInfoList *list, *start;
1883 readline_set_completion_index(rs, len);
1885 start = list = qmp_query_chardev_backends(NULL);
1887 const char *chr_name = list->value->name;
1889 if (!strncmp(chr_name, str, len)) {
1890 readline_add_completion(rs, chr_name);
1894 qapi_free_ChardevBackendInfoList(start);
1897 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1906 readline_set_completion_index(rs, len);
1907 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1908 add_completion_option(rs, str, NetClientDriver_str(i));
1912 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1922 readline_set_completion_index(rs, len);
1923 list = elt = object_class_get_list(TYPE_DEVICE, false);
1926 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1928 name = object_class_get_name(OBJECT_CLASS(dc));
1930 if (dc->user_creatable
1931 && !strncmp(name, str, len)) {
1932 readline_add_completion(rs, name);
1939 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1949 readline_set_completion_index(rs, len);
1950 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1954 name = object_class_get_name(OBJECT_CLASS(elt->data));
1955 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1956 readline_add_completion(rs, name);
1963 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1965 GSList **list = opaque;
1966 DeviceState *dev = (DeviceState *)object_dynamic_cast(OBJECT(obj),
1973 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1974 *list = g_slist_append(*list, dev);
1980 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1982 GSList *list = NULL;
1984 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1989 static void peripheral_device_del_completion(ReadLineState *rs,
1990 const char *str, size_t len)
1992 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1993 GSList *list, *item;
1995 list = qdev_build_hotpluggable_device_list(peripheral);
2000 for (item = list; item; item = g_slist_next(item)) {
2001 DeviceState *dev = item->data;
2003 if (dev->id && !strncmp(str, dev->id, len)) {
2004 readline_add_completion(rs, dev->id);
2011 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
2014 ChardevInfoList *list, *start;
2020 readline_set_completion_index(rs, len);
2022 start = list = qmp_query_chardev(NULL);
2024 ChardevInfo *chr = list->value;
2026 if (!strncmp(chr->label, str, len)) {
2027 readline_add_completion(rs, chr->label);
2031 qapi_free_ChardevInfoList(start);
2034 static void ringbuf_completion(ReadLineState *rs, const char *str)
2037 ChardevInfoList *list, *start;
2040 readline_set_completion_index(rs, len);
2042 start = list = qmp_query_chardev(NULL);
2044 ChardevInfo *chr_info = list->value;
2046 if (!strncmp(chr_info->label, str, len)) {
2047 Chardev *chr = qemu_chr_find(chr_info->label);
2048 if (chr && CHARDEV_IS_RINGBUF(chr)) {
2049 readline_add_completion(rs, chr_info->label);
2054 qapi_free_ChardevInfoList(start);
2057 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
2062 ringbuf_completion(rs, str);
2065 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
2074 readline_set_completion_index(rs, len);
2075 peripheral_device_del_completion(rs, str, len);
2078 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
2080 ObjectPropertyInfoList *list, *start;
2087 readline_set_completion_index(rs, len);
2089 start = list = qmp_qom_list("/objects", NULL);
2091 ObjectPropertyInfo *info = list->value;
2093 if (!strncmp(info->type, "child<", 5)
2094 && !strncmp(info->name, str, len)) {
2095 readline_add_completion(rs, info->name);
2099 qapi_free_ObjectPropertyInfoList(start);
2102 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
2111 sep = strrchr(str, '-');
2116 readline_set_completion_index(rs, len);
2117 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
2118 if (!strncmp(str, QKeyCode_str(i), len)) {
2119 readline_add_completion(rs, QKeyCode_str(i));
2124 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
2129 readline_set_completion_index(rs, len);
2131 NetClientState *ncs[MAX_QUEUE_NUM];
2133 count = qemu_find_net_clients_except(NULL, ncs,
2134 NET_CLIENT_DRIVER_NONE,
2136 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2137 const char *name = ncs[i]->name;
2138 if (!strncmp(str, name, len)) {
2139 readline_add_completion(rs, name);
2142 } else if (nb_args == 3) {
2143 add_completion_option(rs, str, "on");
2144 add_completion_option(rs, str, "off");
2148 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
2151 NetClientState *ncs[MAX_QUEUE_NUM];
2158 readline_set_completion_index(rs, len);
2159 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
2161 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2163 const char *name = ncs[i]->name;
2164 if (strncmp(str, name, len)) {
2167 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
2169 readline_add_completion(rs, name);
2174 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2179 readline_set_completion_index(rs, len);
2181 TraceEventIter iter;
2183 char *pattern = g_strdup_printf("%s*", str);
2184 trace_event_iter_init(&iter, pattern);
2185 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2186 readline_add_completion(rs, trace_event_get_name(ev));
2192 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2197 readline_set_completion_index(rs, len);
2199 TraceEventIter iter;
2201 char *pattern = g_strdup_printf("%s*", str);
2202 trace_event_iter_init(&iter, pattern);
2203 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2204 readline_add_completion(rs, trace_event_get_name(ev));
2207 } else if (nb_args == 3) {
2208 add_completion_option(rs, str, "on");
2209 add_completion_option(rs, str, "off");
2213 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2220 readline_set_completion_index(rs, strlen(str));
2221 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2222 add_completion_option(rs, str, WatchdogAction_str(i));
2226 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2232 readline_set_completion_index(rs, len);
2235 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2236 const char *name = MigrationCapability_str(i);
2237 if (!strncmp(str, name, len)) {
2238 readline_add_completion(rs, name);
2241 } else if (nb_args == 3) {
2242 add_completion_option(rs, str, "on");
2243 add_completion_option(rs, str, "off");
2247 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2253 readline_set_completion_index(rs, len);
2256 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
2257 const char *name = MigrationParameter_str(i);
2258 if (!strncmp(str, name, len)) {
2259 readline_add_completion(rs, name);
2265 static void vm_completion(ReadLineState *rs, const char *str)
2268 BlockDriverState *bs;
2269 BdrvNextIterator it;
2272 readline_set_completion_index(rs, len);
2274 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2275 SnapshotInfoList *snapshots, *snapshot;
2276 AioContext *ctx = bdrv_get_aio_context(bs);
2279 aio_context_acquire(ctx);
2280 if (bdrv_can_snapshot(bs)) {
2281 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2283 aio_context_release(ctx);
2288 snapshot = snapshots;
2290 char *completion = snapshot->value->name;
2291 if (!strncmp(str, completion, len)) {
2292 readline_add_completion(rs, completion);
2294 completion = snapshot->value->id;
2295 if (!strncmp(str, completion, len)) {
2296 readline_add_completion(rs, completion);
2298 snapshot = snapshot->next;
2300 qapi_free_SnapshotInfoList(snapshots);
2305 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2308 vm_completion(rs, str);
2312 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2315 vm_completion(rs, str);
2320 compare_mon_cmd(const void *a, const void *b)
2322 return strcmp(((const HMPCommand *)a)->name,
2323 ((const HMPCommand *)b)->name);
2326 static void sortcmdlist(void)
2328 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2331 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2332 sizeof(*hmp_info_cmds),
2336 void monitor_init_globals(void)
2338 monitor_init_globals_core();
2339 monitor_init_qmp_commands();
2341 qemu_mutex_init(&mon_fdsets_lock);