qmp/hmp, device_tree.c: introduce dumpdtb
[qemu/armbru.git] / monitor / misc.c
blob205487e2b9ed6f5841f5f0faa796a1b16a6d9271
1 /*
2 * QEMU monitor
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
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "hw/usb.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
39 #include "ui/input.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
44 #include "qemu/log.h"
45 #include "sysemu/hw_accel.h"
46 #include "sysemu/runstate.h"
47 #include "authz/list.h"
48 #include "qapi/util.h"
49 #include "sysemu/blockdev.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/tpm.h"
52 #include "sysemu/device_tree.h"
53 #include "qapi/qmp/qdict.h"
54 #include "qapi/qmp/qerror.h"
55 #include "qapi/qmp/qstring.h"
56 #include "qom/object_interfaces.h"
57 #include "trace/control.h"
58 #include "monitor/hmp-target.h"
59 #include "monitor/hmp.h"
60 #ifdef CONFIG_TRACE_SIMPLE
61 #include "trace/simple.h"
62 #endif
63 #include "exec/memory.h"
64 #include "exec/exec-all.h"
65 #include "qemu/option.h"
66 #include "qemu/thread.h"
67 #include "block/qapi.h"
68 #include "block/block-hmp-cmds.h"
69 #include "qapi/qapi-commands-char.h"
70 #include "qapi/qapi-commands-control.h"
71 #include "qapi/qapi-commands-migration.h"
72 #include "qapi/qapi-commands-misc.h"
73 #include "qapi/qapi-commands-qom.h"
74 #include "qapi/qapi-commands-run-state.h"
75 #include "qapi/qapi-commands-trace.h"
76 #include "qapi/qapi-commands-machine.h"
77 #include "qapi/qapi-init-commands.h"
78 #include "qapi/error.h"
79 #include "qapi/qmp-event.h"
80 #include "sysemu/cpus.h"
81 #include "qemu/cutils.h"
83 #if defined(TARGET_S390X)
84 #include "hw/s390x/storage-keys.h"
85 #include "hw/s390x/storage-attributes.h"
86 #endif
88 /* Make devices configuration available for use in hmp-commands*.hx templates */
89 #include CONFIG_DEVICES
91 /* file descriptors passed via SCM_RIGHTS */
92 typedef struct mon_fd_t mon_fd_t;
93 struct mon_fd_t {
94 char *name;
95 int fd;
96 QLIST_ENTRY(mon_fd_t) next;
99 /* file descriptor associated with a file descriptor set */
100 typedef struct MonFdsetFd MonFdsetFd;
101 struct MonFdsetFd {
102 int fd;
103 bool removed;
104 char *opaque;
105 QLIST_ENTRY(MonFdsetFd) next;
108 /* file descriptor set containing fds passed via SCM_RIGHTS */
109 typedef struct MonFdset MonFdset;
110 struct MonFdset {
111 int64_t id;
112 QLIST_HEAD(, MonFdsetFd) fds;
113 QLIST_HEAD(, MonFdsetFd) dup_fds;
114 QLIST_ENTRY(MonFdset) next;
117 /* Protects mon_fdsets */
118 static QemuMutex mon_fdsets_lock;
119 static QLIST_HEAD(, MonFdset) mon_fdsets;
121 static HMPCommand hmp_info_cmds[];
123 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
124 int64_t cpu_index, Error **errp)
126 char *output = NULL;
127 MonitorHMP hmp = {};
129 monitor_data_init(&hmp.common, false, true, false);
131 if (has_cpu_index) {
132 int ret = monitor_set_cpu(&hmp.common, cpu_index);
133 if (ret < 0) {
134 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
135 "a CPU number");
136 goto out;
140 handle_hmp_command(&hmp, command_line);
142 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
143 output = g_strdup(hmp.common.outbuf->str);
146 out:
147 monitor_data_destroy(&hmp.common);
148 return output;
152 * Is @name in the '|' separated list of names @list?
154 int hmp_compare_cmd(const char *name, const char *list)
156 const char *p, *pstart;
157 int len;
158 len = strlen(name);
159 p = list;
160 for (;;) {
161 pstart = p;
162 p = qemu_strchrnul(p, '|');
163 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
164 return 1;
166 if (*p == '\0') {
167 break;
169 p++;
171 return 0;
174 static void do_help_cmd(Monitor *mon, const QDict *qdict)
176 help_cmd(mon, qdict_get_try_str(qdict, "name"));
179 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
181 const char *tp_name = qdict_get_str(qdict, "name");
182 bool new_state = qdict_get_bool(qdict, "option");
183 bool has_vcpu = qdict_haskey(qdict, "vcpu");
184 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
185 Error *local_err = NULL;
187 if (vcpu < 0) {
188 monitor_printf(mon, "argument vcpu must be positive");
189 return;
192 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
193 if (local_err) {
194 error_report_err(local_err);
198 #ifdef CONFIG_TRACE_SIMPLE
199 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
201 const char *op = qdict_get_try_str(qdict, "op");
202 const char *arg = qdict_get_try_str(qdict, "arg");
204 if (!op) {
205 st_print_trace_file_status();
206 } else if (!strcmp(op, "on")) {
207 st_set_trace_file_enabled(true);
208 } else if (!strcmp(op, "off")) {
209 st_set_trace_file_enabled(false);
210 } else if (!strcmp(op, "flush")) {
211 st_flush_trace_buffer();
212 } else if (!strcmp(op, "set")) {
213 if (arg) {
214 st_set_trace_file(arg);
216 } else {
217 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
218 help_cmd(mon, "trace-file");
221 #endif
223 static void hmp_info_help(Monitor *mon, const QDict *qdict)
225 help_cmd(mon, "info");
228 static void monitor_init_qmp_commands(void)
231 * Two command lists:
232 * - qmp_commands contains all QMP commands
233 * - qmp_cap_negotiation_commands contains just
234 * "qmp_capabilities", to enforce capability negotiation
237 qmp_init_marshal(&qmp_commands);
239 qmp_register_command(&qmp_commands, "device_add",
240 qmp_device_add, 0, 0);
242 QTAILQ_INIT(&qmp_cap_negotiation_commands);
243 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
244 qmp_marshal_qmp_capabilities,
245 QCO_ALLOW_PRECONFIG, 0);
248 /* Set the current CPU defined by the user. Callers must hold BQL. */
249 int monitor_set_cpu(Monitor *mon, int cpu_index)
251 CPUState *cpu;
253 cpu = qemu_get_cpu(cpu_index);
254 if (cpu == NULL) {
255 return -1;
257 g_free(mon->mon_cpu_path);
258 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
259 return 0;
262 /* Callers must hold BQL. */
263 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
265 CPUState *cpu = NULL;
267 if (mon->mon_cpu_path) {
268 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
269 TYPE_CPU, NULL);
270 if (!cpu) {
271 g_free(mon->mon_cpu_path);
272 mon->mon_cpu_path = NULL;
275 if (!mon->mon_cpu_path) {
276 if (!first_cpu) {
277 return NULL;
279 monitor_set_cpu(mon, first_cpu->cpu_index);
280 cpu = first_cpu;
282 assert(cpu != NULL);
283 if (synchronize) {
284 cpu_synchronize_state(cpu);
286 return cpu;
289 CPUState *mon_get_cpu(Monitor *mon)
291 return mon_get_cpu_sync(mon, true);
294 CPUArchState *mon_get_cpu_env(Monitor *mon)
296 CPUState *cs = mon_get_cpu(mon);
298 return cs ? cs->env_ptr : NULL;
301 int monitor_get_cpu_index(Monitor *mon)
303 CPUState *cs = mon_get_cpu_sync(mon, false);
305 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
308 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
310 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
311 int vcpu = qdict_get_try_int(qdict, "vcpu", -1);
312 CPUState *cs;
314 if (all_cpus) {
315 CPU_FOREACH(cs) {
316 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
317 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
319 } else {
320 cs = vcpu >= 0 ? qemu_get_cpu(vcpu) : mon_get_cpu(mon);
322 if (!cs) {
323 if (vcpu >= 0) {
324 monitor_printf(mon, "CPU#%d not available\n", vcpu);
325 } else {
326 monitor_printf(mon, "No CPU available\n");
328 return;
331 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
332 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
336 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
338 int64_t max = qdict_get_try_int(qdict, "max", 10);
339 bool mean = qdict_get_try_bool(qdict, "mean", false);
340 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
341 enum QSPSortBy sort_by;
343 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
344 qsp_report(max, sort_by, coalesce);
347 static void hmp_info_history(Monitor *mon, const QDict *qdict)
349 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
350 int i;
351 const char *str;
353 if (!hmp_mon->rs) {
354 return;
356 i = 0;
357 for(;;) {
358 str = readline_get_history(hmp_mon->rs, i);
359 if (!str) {
360 break;
362 monitor_printf(mon, "%d: '%s'\n", i, str);
363 i++;
367 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
369 const char *name = qdict_get_try_str(qdict, "name");
370 bool has_vcpu = qdict_haskey(qdict, "vcpu");
371 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
372 TraceEventInfoList *events;
373 TraceEventInfoList *elem;
374 Error *local_err = NULL;
376 if (name == NULL) {
377 name = "*";
379 if (vcpu < 0) {
380 monitor_printf(mon, "argument vcpu must be positive");
381 return;
384 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
385 if (local_err) {
386 error_report_err(local_err);
387 return;
390 for (elem = events; elem != NULL; elem = elem->next) {
391 monitor_printf(mon, "%s : state %u\n",
392 elem->value->name,
393 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
395 qapi_free_TraceEventInfoList(events);
398 void qmp_client_migrate_info(const char *protocol, const char *hostname,
399 bool has_port, int64_t port,
400 bool has_tls_port, int64_t tls_port,
401 bool has_cert_subject, const char *cert_subject,
402 Error **errp)
404 if (strcmp(protocol, "spice") == 0) {
405 if (!qemu_using_spice(errp)) {
406 return;
409 if (!has_port && !has_tls_port) {
410 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
411 return;
414 if (qemu_spice.migrate_info(hostname,
415 has_port ? port : -1,
416 has_tls_port ? tls_port : -1,
417 cert_subject)) {
418 error_setg(errp, "Could not set up display for migration");
419 return;
421 return;
424 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
427 static void hmp_logfile(Monitor *mon, const QDict *qdict)
429 Error *err = NULL;
431 if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
432 error_report_err(err);
436 static void hmp_log(Monitor *mon, const QDict *qdict)
438 int mask;
439 const char *items = qdict_get_str(qdict, "items");
440 Error *err = NULL;
442 if (!strcmp(items, "none")) {
443 mask = 0;
444 } else {
445 mask = qemu_str_to_log_mask(items);
446 if (!mask) {
447 help_cmd(mon, "log");
448 return;
452 if (!qemu_set_log(mask, &err)) {
453 error_report_err(err);
457 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
459 const char *option = qdict_get_try_str(qdict, "option");
460 if (!option || !strcmp(option, "on")) {
461 singlestep = 1;
462 } else if (!strcmp(option, "off")) {
463 singlestep = 0;
464 } else {
465 monitor_printf(mon, "unexpected option %s\n", option);
469 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
471 const char *device = qdict_get_try_str(qdict, "device");
472 if (!device) {
473 device = "tcp::" DEFAULT_GDBSTUB_PORT;
476 if (gdbserver_start(device) < 0) {
477 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
478 device);
479 } else if (strcmp(device, "none") == 0) {
480 monitor_printf(mon, "Disabled gdbserver\n");
481 } else {
482 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
483 device);
487 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
489 Error *err = NULL;
490 WatchdogAction action;
491 char *qapi_value;
493 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
494 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
495 g_free(qapi_value);
496 if (err) {
497 hmp_handle_error(mon, err);
498 return;
500 qmp_watchdog_set_action(action, &error_abort);
503 static void monitor_printc(Monitor *mon, int c)
505 monitor_printf(mon, "'");
506 switch(c) {
507 case '\'':
508 monitor_printf(mon, "\\'");
509 break;
510 case '\\':
511 monitor_printf(mon, "\\\\");
512 break;
513 case '\n':
514 monitor_printf(mon, "\\n");
515 break;
516 case '\r':
517 monitor_printf(mon, "\\r");
518 break;
519 default:
520 if (c >= 32 && c <= 126) {
521 monitor_printf(mon, "%c", c);
522 } else {
523 monitor_printf(mon, "\\x%02x", c);
525 break;
527 monitor_printf(mon, "'");
530 static void memory_dump(Monitor *mon, int count, int format, int wsize,
531 hwaddr addr, int is_physical)
533 int l, line_size, i, max_digits, len;
534 uint8_t buf[16];
535 uint64_t v;
536 CPUState *cs = mon_get_cpu(mon);
538 if (!cs && (format == 'i' || !is_physical)) {
539 monitor_printf(mon, "Can not dump without CPU\n");
540 return;
543 if (format == 'i') {
544 monitor_disas(mon, cs, addr, count, is_physical);
545 return;
548 len = wsize * count;
549 if (wsize == 1) {
550 line_size = 8;
551 } else {
552 line_size = 16;
554 max_digits = 0;
556 switch(format) {
557 case 'o':
558 max_digits = DIV_ROUND_UP(wsize * 8, 3);
559 break;
560 default:
561 case 'x':
562 max_digits = (wsize * 8) / 4;
563 break;
564 case 'u':
565 case 'd':
566 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
567 break;
568 case 'c':
569 wsize = 1;
570 break;
573 while (len > 0) {
574 if (is_physical) {
575 monitor_printf(mon, TARGET_FMT_plx ":", addr);
576 } else {
577 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
579 l = len;
580 if (l > line_size)
581 l = line_size;
582 if (is_physical) {
583 AddressSpace *as = cs ? cs->as : &address_space_memory;
584 MemTxResult r = address_space_read(as, addr,
585 MEMTXATTRS_UNSPECIFIED, buf, l);
586 if (r != MEMTX_OK) {
587 monitor_printf(mon, " Cannot access memory\n");
588 break;
590 } else {
591 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
592 monitor_printf(mon, " Cannot access memory\n");
593 break;
596 i = 0;
597 while (i < l) {
598 switch(wsize) {
599 default:
600 case 1:
601 v = ldub_p(buf + i);
602 break;
603 case 2:
604 v = lduw_p(buf + i);
605 break;
606 case 4:
607 v = (uint32_t)ldl_p(buf + i);
608 break;
609 case 8:
610 v = ldq_p(buf + i);
611 break;
613 monitor_printf(mon, " ");
614 switch(format) {
615 case 'o':
616 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
617 break;
618 case 'x':
619 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
620 break;
621 case 'u':
622 monitor_printf(mon, "%*" PRIu64, max_digits, v);
623 break;
624 case 'd':
625 monitor_printf(mon, "%*" PRId64, max_digits, v);
626 break;
627 case 'c':
628 monitor_printc(mon, v);
629 break;
631 i += wsize;
633 monitor_printf(mon, "\n");
634 addr += l;
635 len -= l;
639 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
641 int count = qdict_get_int(qdict, "count");
642 int format = qdict_get_int(qdict, "format");
643 int size = qdict_get_int(qdict, "size");
644 target_long addr = qdict_get_int(qdict, "addr");
646 memory_dump(mon, count, format, size, addr, 0);
649 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
651 int count = qdict_get_int(qdict, "count");
652 int format = qdict_get_int(qdict, "format");
653 int size = qdict_get_int(qdict, "size");
654 hwaddr addr = qdict_get_int(qdict, "addr");
656 memory_dump(mon, count, format, size, addr, 1);
659 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
661 Int128 gpa_region_size;
662 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
663 addr, size);
665 if (!mrs.mr) {
666 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
667 return NULL;
670 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
671 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
672 memory_region_unref(mrs.mr);
673 return NULL;
676 gpa_region_size = int128_make64(size);
677 if (int128_lt(mrs.size, gpa_region_size)) {
678 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
679 " exceeded.", addr);
680 memory_region_unref(mrs.mr);
681 return NULL;
684 *p_mr = mrs.mr;
685 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
688 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
690 hwaddr addr = qdict_get_int(qdict, "addr");
691 Error *local_err = NULL;
692 MemoryRegion *mr = NULL;
693 void *ptr;
695 ptr = gpa2hva(&mr, addr, 1, &local_err);
696 if (local_err) {
697 error_report_err(local_err);
698 return;
701 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
702 " (%s) is %p\n",
703 addr, mr->name, ptr);
705 memory_region_unref(mr);
708 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
710 target_ulong addr = qdict_get_int(qdict, "addr");
711 MemTxAttrs attrs;
712 CPUState *cs = mon_get_cpu(mon);
713 hwaddr gpa;
715 if (!cs) {
716 monitor_printf(mon, "No cpu\n");
717 return;
720 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
721 if (gpa == -1) {
722 monitor_printf(mon, "Unmapped\n");
723 } else {
724 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
725 gpa + (addr & ~TARGET_PAGE_MASK));
729 #ifdef CONFIG_LINUX
730 static uint64_t vtop(void *ptr, Error **errp)
732 uint64_t pinfo;
733 uint64_t ret = -1;
734 uintptr_t addr = (uintptr_t) ptr;
735 uintptr_t pagesize = qemu_real_host_page_size();
736 off_t offset = addr / pagesize * sizeof(pinfo);
737 int fd;
739 fd = open("/proc/self/pagemap", O_RDONLY);
740 if (fd == -1) {
741 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
742 return -1;
745 /* Force copy-on-write if necessary. */
746 qatomic_add((uint8_t *)ptr, 0);
748 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
749 error_setg_errno(errp, errno, "Cannot read pagemap");
750 goto out;
752 if ((pinfo & (1ull << 63)) == 0) {
753 error_setg(errp, "Page not present");
754 goto out;
756 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
758 out:
759 close(fd);
760 return ret;
763 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
765 hwaddr addr = qdict_get_int(qdict, "addr");
766 Error *local_err = NULL;
767 MemoryRegion *mr = NULL;
768 void *ptr;
769 uint64_t physaddr;
771 ptr = gpa2hva(&mr, addr, 1, &local_err);
772 if (local_err) {
773 error_report_err(local_err);
774 return;
777 physaddr = vtop(ptr, &local_err);
778 if (local_err) {
779 error_report_err(local_err);
780 } else {
781 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
782 " (%s) is 0x%" PRIx64 "\n",
783 addr, mr->name, (uint64_t) physaddr);
786 memory_region_unref(mr);
788 #endif
790 static void do_print(Monitor *mon, const QDict *qdict)
792 int format = qdict_get_int(qdict, "format");
793 hwaddr val = qdict_get_int(qdict, "val");
795 switch(format) {
796 case 'o':
797 monitor_printf(mon, "%#" HWADDR_PRIo, val);
798 break;
799 case 'x':
800 monitor_printf(mon, "%#" HWADDR_PRIx, val);
801 break;
802 case 'u':
803 monitor_printf(mon, "%" HWADDR_PRIu, val);
804 break;
805 default:
806 case 'd':
807 monitor_printf(mon, "%" HWADDR_PRId, val);
808 break;
809 case 'c':
810 monitor_printc(mon, val);
811 break;
813 monitor_printf(mon, "\n");
816 static void hmp_sum(Monitor *mon, const QDict *qdict)
818 uint32_t addr;
819 uint16_t sum;
820 uint32_t start = qdict_get_int(qdict, "start");
821 uint32_t size = qdict_get_int(qdict, "size");
823 sum = 0;
824 for(addr = start; addr < (start + size); addr++) {
825 uint8_t val = address_space_ldub(&address_space_memory, addr,
826 MEMTXATTRS_UNSPECIFIED, NULL);
827 /* BSD sum algorithm ('sum' Unix command) */
828 sum = (sum >> 1) | (sum << 15);
829 sum += val;
831 monitor_printf(mon, "%05d\n", sum);
834 static int mouse_button_state;
836 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
838 int dx, dy, dz, button;
839 const char *dx_str = qdict_get_str(qdict, "dx_str");
840 const char *dy_str = qdict_get_str(qdict, "dy_str");
841 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
843 dx = strtol(dx_str, NULL, 0);
844 dy = strtol(dy_str, NULL, 0);
845 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
846 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
848 if (dz_str) {
849 dz = strtol(dz_str, NULL, 0);
850 if (dz != 0) {
851 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
852 qemu_input_queue_btn(NULL, button, true);
853 qemu_input_event_sync();
854 qemu_input_queue_btn(NULL, button, false);
857 qemu_input_event_sync();
860 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
862 static uint32_t bmap[INPUT_BUTTON__MAX] = {
863 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
864 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
865 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
867 int button_state = qdict_get_int(qdict, "button_state");
869 if (mouse_button_state == button_state) {
870 return;
872 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
873 qemu_input_event_sync();
874 mouse_button_state = button_state;
877 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
879 int size = qdict_get_int(qdict, "size");
880 int addr = qdict_get_int(qdict, "addr");
881 int has_index = qdict_haskey(qdict, "index");
882 uint32_t val;
883 int suffix;
885 if (has_index) {
886 int index = qdict_get_int(qdict, "index");
887 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
888 addr++;
890 addr &= 0xffff;
892 switch(size) {
893 default:
894 case 1:
895 val = cpu_inb(addr);
896 suffix = 'b';
897 break;
898 case 2:
899 val = cpu_inw(addr);
900 suffix = 'w';
901 break;
902 case 4:
903 val = cpu_inl(addr);
904 suffix = 'l';
905 break;
907 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
908 suffix, addr, size * 2, val);
911 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
913 int size = qdict_get_int(qdict, "size");
914 int addr = qdict_get_int(qdict, "addr");
915 int val = qdict_get_int(qdict, "val");
917 addr &= IOPORTS_MASK;
919 switch (size) {
920 default:
921 case 1:
922 cpu_outb(addr, val);
923 break;
924 case 2:
925 cpu_outw(addr, val);
926 break;
927 case 4:
928 cpu_outl(addr, val);
929 break;
933 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
935 Error *local_err = NULL;
936 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
938 qemu_boot_set(bootdevice, &local_err);
939 if (local_err) {
940 error_report_err(local_err);
941 } else {
942 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
946 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
948 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
949 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
950 bool owner = qdict_get_try_bool(qdict, "owner", false);
951 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
953 mtree_info(flatview, dispatch_tree, owner, disabled);
956 /* Capture support */
957 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
959 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
961 int i;
962 CaptureState *s;
964 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
965 monitor_printf(mon, "[%d]: ", i);
966 s->ops.info (s->opaque);
970 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
972 int i;
973 int n = qdict_get_int(qdict, "n");
974 CaptureState *s;
976 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
977 if (i == n) {
978 s->ops.destroy (s->opaque);
979 QLIST_REMOVE (s, entries);
980 g_free (s);
981 return;
986 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
988 const char *path = qdict_get_str(qdict, "path");
989 int freq = qdict_get_try_int(qdict, "freq", 44100);
990 int bits = qdict_get_try_int(qdict, "bits", 16);
991 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
992 const char *audiodev = qdict_get_str(qdict, "audiodev");
993 CaptureState *s;
994 AudioState *as = audio_state_by_name(audiodev);
996 if (!as) {
997 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
998 return;
1001 s = g_malloc0 (sizeof (*s));
1003 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1004 monitor_printf(mon, "Failed to add wave capture\n");
1005 g_free (s);
1006 return;
1008 QLIST_INSERT_HEAD (&capture_head, s, entries);
1011 void qmp_getfd(const char *fdname, Error **errp)
1013 Monitor *cur_mon = monitor_cur();
1014 mon_fd_t *monfd;
1015 int fd, tmp_fd;
1017 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1018 if (fd == -1) {
1019 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1020 return;
1023 if (qemu_isdigit(fdname[0])) {
1024 close(fd);
1025 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1026 "a name not starting with a digit");
1027 return;
1030 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1031 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1032 if (strcmp(monfd->name, fdname) != 0) {
1033 continue;
1036 tmp_fd = monfd->fd;
1037 monfd->fd = fd;
1038 /* Make sure close() is outside critical section */
1039 close(tmp_fd);
1040 return;
1043 monfd = g_new0(mon_fd_t, 1);
1044 monfd->name = g_strdup(fdname);
1045 monfd->fd = fd;
1047 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1050 void qmp_closefd(const char *fdname, Error **errp)
1052 Monitor *cur_mon = monitor_cur();
1053 mon_fd_t *monfd;
1054 int tmp_fd;
1056 qemu_mutex_lock(&cur_mon->mon_lock);
1057 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1058 if (strcmp(monfd->name, fdname) != 0) {
1059 continue;
1062 QLIST_REMOVE(monfd, next);
1063 tmp_fd = monfd->fd;
1064 g_free(monfd->name);
1065 g_free(monfd);
1066 qemu_mutex_unlock(&cur_mon->mon_lock);
1067 /* Make sure close() is outside critical section */
1068 close(tmp_fd);
1069 return;
1072 qemu_mutex_unlock(&cur_mon->mon_lock);
1073 error_setg(errp, "File descriptor named '%s' not found", fdname);
1076 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1078 mon_fd_t *monfd;
1080 QEMU_LOCK_GUARD(&mon->mon_lock);
1081 QLIST_FOREACH(monfd, &mon->fds, next) {
1082 int fd;
1084 if (strcmp(monfd->name, fdname) != 0) {
1085 continue;
1088 fd = monfd->fd;
1090 /* caller takes ownership of fd */
1091 QLIST_REMOVE(monfd, next);
1092 g_free(monfd->name);
1093 g_free(monfd);
1095 return fd;
1098 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1099 return -1;
1102 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1104 MonFdsetFd *mon_fdset_fd;
1105 MonFdsetFd *mon_fdset_fd_next;
1107 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1108 if ((mon_fdset_fd->removed ||
1109 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1110 runstate_is_running()) {
1111 close(mon_fdset_fd->fd);
1112 g_free(mon_fdset_fd->opaque);
1113 QLIST_REMOVE(mon_fdset_fd, next);
1114 g_free(mon_fdset_fd);
1118 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1119 QLIST_REMOVE(mon_fdset, next);
1120 g_free(mon_fdset);
1124 void monitor_fdsets_cleanup(void)
1126 MonFdset *mon_fdset;
1127 MonFdset *mon_fdset_next;
1129 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1130 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1131 monitor_fdset_cleanup(mon_fdset);
1135 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1136 const char *opaque, Error **errp)
1138 int fd;
1139 Monitor *mon = monitor_cur();
1140 AddfdInfo *fdinfo;
1142 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1143 if (fd == -1) {
1144 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1145 goto error;
1148 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1149 has_opaque, opaque, errp);
1150 if (fdinfo) {
1151 return fdinfo;
1154 error:
1155 if (fd != -1) {
1156 close(fd);
1158 return NULL;
1161 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1163 MonFdset *mon_fdset;
1164 MonFdsetFd *mon_fdset_fd;
1165 char fd_str[60];
1167 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1168 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1169 if (mon_fdset->id != fdset_id) {
1170 continue;
1172 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1173 if (has_fd) {
1174 if (mon_fdset_fd->fd != fd) {
1175 continue;
1177 mon_fdset_fd->removed = true;
1178 break;
1179 } else {
1180 mon_fdset_fd->removed = true;
1183 if (has_fd && !mon_fdset_fd) {
1184 goto error;
1186 monitor_fdset_cleanup(mon_fdset);
1187 return;
1190 error:
1191 if (has_fd) {
1192 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1193 fdset_id, fd);
1194 } else {
1195 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1197 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1200 FdsetInfoList *qmp_query_fdsets(Error **errp)
1202 MonFdset *mon_fdset;
1203 MonFdsetFd *mon_fdset_fd;
1204 FdsetInfoList *fdset_list = NULL;
1206 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1207 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1208 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1210 fdset_info->fdset_id = mon_fdset->id;
1212 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1213 FdsetFdInfo *fdsetfd_info;
1215 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1216 fdsetfd_info->fd = mon_fdset_fd->fd;
1217 if (mon_fdset_fd->opaque) {
1218 fdsetfd_info->has_opaque = true;
1219 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1220 } else {
1221 fdsetfd_info->has_opaque = false;
1224 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1227 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1230 return fdset_list;
1233 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1234 bool has_opaque, const char *opaque,
1235 Error **errp)
1237 MonFdset *mon_fdset = NULL;
1238 MonFdsetFd *mon_fdset_fd;
1239 AddfdInfo *fdinfo;
1241 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1242 if (has_fdset_id) {
1243 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1244 /* Break if match found or match impossible due to ordering by ID */
1245 if (fdset_id <= mon_fdset->id) {
1246 if (fdset_id < mon_fdset->id) {
1247 mon_fdset = NULL;
1249 break;
1254 if (mon_fdset == NULL) {
1255 int64_t fdset_id_prev = -1;
1256 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1258 if (has_fdset_id) {
1259 if (fdset_id < 0) {
1260 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1261 "a non-negative value");
1262 return NULL;
1264 /* Use specified fdset ID */
1265 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1266 mon_fdset_cur = mon_fdset;
1267 if (fdset_id < mon_fdset_cur->id) {
1268 break;
1271 } else {
1272 /* Use first available fdset ID */
1273 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1274 mon_fdset_cur = mon_fdset;
1275 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1276 fdset_id_prev = mon_fdset_cur->id;
1277 continue;
1279 break;
1283 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1284 if (has_fdset_id) {
1285 mon_fdset->id = fdset_id;
1286 } else {
1287 mon_fdset->id = fdset_id_prev + 1;
1290 /* The fdset list is ordered by fdset ID */
1291 if (!mon_fdset_cur) {
1292 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1293 } else if (mon_fdset->id < mon_fdset_cur->id) {
1294 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1295 } else {
1296 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1300 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1301 mon_fdset_fd->fd = fd;
1302 mon_fdset_fd->removed = false;
1303 if (has_opaque) {
1304 mon_fdset_fd->opaque = g_strdup(opaque);
1306 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1308 fdinfo = g_malloc0(sizeof(*fdinfo));
1309 fdinfo->fdset_id = mon_fdset->id;
1310 fdinfo->fd = mon_fdset_fd->fd;
1312 return fdinfo;
1315 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1317 #ifdef _WIN32
1318 return -ENOENT;
1319 #else
1320 MonFdset *mon_fdset;
1322 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1323 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1324 MonFdsetFd *mon_fdset_fd;
1325 MonFdsetFd *mon_fdset_fd_dup;
1326 int fd = -1;
1327 int dup_fd;
1328 int mon_fd_flags;
1330 if (mon_fdset->id != fdset_id) {
1331 continue;
1334 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1335 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1336 if (mon_fd_flags == -1) {
1337 return -1;
1340 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1341 fd = mon_fdset_fd->fd;
1342 break;
1346 if (fd == -1) {
1347 errno = EACCES;
1348 return -1;
1351 dup_fd = qemu_dup_flags(fd, flags);
1352 if (dup_fd == -1) {
1353 return -1;
1356 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1357 mon_fdset_fd_dup->fd = dup_fd;
1358 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1359 return dup_fd;
1362 errno = ENOENT;
1363 return -1;
1364 #endif
1367 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1369 MonFdset *mon_fdset;
1370 MonFdsetFd *mon_fdset_fd_dup;
1372 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1373 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1374 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1375 if (mon_fdset_fd_dup->fd == dup_fd) {
1376 if (remove) {
1377 QLIST_REMOVE(mon_fdset_fd_dup, next);
1378 g_free(mon_fdset_fd_dup);
1379 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1380 monitor_fdset_cleanup(mon_fdset);
1382 return -1;
1383 } else {
1384 return mon_fdset->id;
1390 return -1;
1393 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1395 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1398 void monitor_fdset_dup_fd_remove(int dup_fd)
1400 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1403 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1405 int fd;
1406 Error *local_err = NULL;
1408 if (!qemu_isdigit(fdname[0]) && mon) {
1409 fd = monitor_get_fd(mon, fdname, &local_err);
1410 } else {
1411 fd = qemu_parse_fd(fdname);
1412 if (fd == -1) {
1413 error_setg(&local_err, "Invalid file descriptor number '%s'",
1414 fdname);
1417 if (local_err) {
1418 error_propagate(errp, local_err);
1419 assert(fd == -1);
1420 } else {
1421 assert(fd != -1);
1424 return fd;
1427 /* Please update hmp-commands.hx when adding or changing commands */
1428 static HMPCommand hmp_info_cmds[] = {
1429 #include "hmp-commands-info.h"
1430 { NULL, NULL, },
1433 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1434 HMPCommand hmp_cmds[] = {
1435 #include "hmp-commands.h"
1436 { NULL, NULL, },
1440 * Set @pval to the value in the register identified by @name.
1441 * return 0 if OK, -1 if not found
1443 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1445 const MonitorDef *md = target_monitor_defs();
1446 CPUState *cs = mon_get_cpu(mon);
1447 void *ptr;
1448 uint64_t tmp = 0;
1449 int ret;
1451 if (cs == NULL || md == NULL) {
1452 return -1;
1455 for(; md->name != NULL; md++) {
1456 if (hmp_compare_cmd(name, md->name)) {
1457 if (md->get_value) {
1458 *pval = md->get_value(mon, md, md->offset);
1459 } else {
1460 CPUArchState *env = mon_get_cpu_env(mon);
1461 ptr = (uint8_t *)env + md->offset;
1462 switch(md->type) {
1463 case MD_I32:
1464 *pval = *(int32_t *)ptr;
1465 break;
1466 case MD_TLONG:
1467 *pval = *(target_long *)ptr;
1468 break;
1469 default:
1470 *pval = 0;
1471 break;
1474 return 0;
1478 ret = target_get_monitor_def(cs, name, &tmp);
1479 if (!ret) {
1480 *pval = (target_long) tmp;
1483 return ret;
1486 static void add_completion_option(ReadLineState *rs, const char *str,
1487 const char *option)
1489 if (!str || !option) {
1490 return;
1492 if (!strncmp(option, str, strlen(str))) {
1493 readline_add_completion(rs, option);
1497 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1499 size_t len;
1500 ChardevBackendInfoList *list, *start;
1502 if (nb_args != 2) {
1503 return;
1505 len = strlen(str);
1506 readline_set_completion_index(rs, len);
1508 start = list = qmp_query_chardev_backends(NULL);
1509 while (list) {
1510 const char *chr_name = list->value->name;
1512 if (!strncmp(chr_name, str, len)) {
1513 readline_add_completion(rs, chr_name);
1515 list = list->next;
1517 qapi_free_ChardevBackendInfoList(start);
1520 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1522 size_t len;
1523 int i;
1525 if (nb_args != 2) {
1526 return;
1528 len = strlen(str);
1529 readline_set_completion_index(rs, len);
1530 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1531 add_completion_option(rs, str, NetClientDriver_str(i));
1535 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1537 GSList *list, *elt;
1538 size_t len;
1540 if (nb_args != 2) {
1541 return;
1544 len = strlen(str);
1545 readline_set_completion_index(rs, len);
1546 list = elt = object_class_get_list(TYPE_DEVICE, false);
1547 while (elt) {
1548 const char *name;
1549 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1550 TYPE_DEVICE);
1551 name = object_class_get_name(OBJECT_CLASS(dc));
1553 if (dc->user_creatable
1554 && !strncmp(name, str, len)) {
1555 readline_add_completion(rs, name);
1557 elt = elt->next;
1559 g_slist_free(list);
1562 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1564 GSList *list, *elt;
1565 size_t len;
1567 if (nb_args != 2) {
1568 return;
1571 len = strlen(str);
1572 readline_set_completion_index(rs, len);
1573 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1574 while (elt) {
1575 const char *name;
1577 name = object_class_get_name(OBJECT_CLASS(elt->data));
1578 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1579 readline_add_completion(rs, name);
1581 elt = elt->next;
1583 g_slist_free(list);
1586 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1588 GSList **list = opaque;
1589 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1591 if (dev == NULL) {
1592 return 0;
1595 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1596 *list = g_slist_append(*list, dev);
1599 return 0;
1602 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1604 GSList *list = NULL;
1606 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1608 return list;
1611 static void peripheral_device_del_completion(ReadLineState *rs,
1612 const char *str, size_t len)
1614 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1615 GSList *list, *item;
1617 list = qdev_build_hotpluggable_device_list(peripheral);
1618 if (!list) {
1619 return;
1622 for (item = list; item; item = g_slist_next(item)) {
1623 DeviceState *dev = item->data;
1625 if (dev->id && !strncmp(str, dev->id, len)) {
1626 readline_add_completion(rs, dev->id);
1630 g_slist_free(list);
1633 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1635 size_t len;
1636 ChardevInfoList *list, *start;
1638 if (nb_args != 2) {
1639 return;
1641 len = strlen(str);
1642 readline_set_completion_index(rs, len);
1644 start = list = qmp_query_chardev(NULL);
1645 while (list) {
1646 ChardevInfo *chr = list->value;
1648 if (!strncmp(chr->label, str, len)) {
1649 readline_add_completion(rs, chr->label);
1651 list = list->next;
1653 qapi_free_ChardevInfoList(start);
1656 static void ringbuf_completion(ReadLineState *rs, const char *str)
1658 size_t len;
1659 ChardevInfoList *list, *start;
1661 len = strlen(str);
1662 readline_set_completion_index(rs, len);
1664 start = list = qmp_query_chardev(NULL);
1665 while (list) {
1666 ChardevInfo *chr_info = list->value;
1668 if (!strncmp(chr_info->label, str, len)) {
1669 Chardev *chr = qemu_chr_find(chr_info->label);
1670 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1671 readline_add_completion(rs, chr_info->label);
1674 list = list->next;
1676 qapi_free_ChardevInfoList(start);
1679 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1681 if (nb_args != 2) {
1682 return;
1684 ringbuf_completion(rs, str);
1687 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1689 size_t len;
1691 if (nb_args != 2) {
1692 return;
1695 len = strlen(str);
1696 readline_set_completion_index(rs, len);
1697 peripheral_device_del_completion(rs, str, len);
1700 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1702 ObjectPropertyInfoList *list, *start;
1703 size_t len;
1705 if (nb_args != 2) {
1706 return;
1708 len = strlen(str);
1709 readline_set_completion_index(rs, len);
1711 start = list = qmp_qom_list("/objects", NULL);
1712 while (list) {
1713 ObjectPropertyInfo *info = list->value;
1715 if (!strncmp(info->type, "child<", 5)
1716 && !strncmp(info->name, str, len)) {
1717 readline_add_completion(rs, info->name);
1719 list = list->next;
1721 qapi_free_ObjectPropertyInfoList(start);
1724 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1726 int i;
1727 char *sep;
1728 size_t len;
1730 if (nb_args != 2) {
1731 return;
1733 sep = strrchr(str, '-');
1734 if (sep) {
1735 str = sep + 1;
1737 len = strlen(str);
1738 readline_set_completion_index(rs, len);
1739 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1740 if (!strncmp(str, QKeyCode_str(i), len)) {
1741 readline_add_completion(rs, QKeyCode_str(i));
1746 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1748 size_t len;
1750 len = strlen(str);
1751 readline_set_completion_index(rs, len);
1752 if (nb_args == 2) {
1753 NetClientState *ncs[MAX_QUEUE_NUM];
1754 int count, i;
1755 count = qemu_find_net_clients_except(NULL, ncs,
1756 NET_CLIENT_DRIVER_NONE,
1757 MAX_QUEUE_NUM);
1758 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1759 const char *name = ncs[i]->name;
1760 if (!strncmp(str, name, len)) {
1761 readline_add_completion(rs, name);
1764 } else if (nb_args == 3) {
1765 add_completion_option(rs, str, "on");
1766 add_completion_option(rs, str, "off");
1770 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1772 int len, count, i;
1773 NetClientState *ncs[MAX_QUEUE_NUM];
1775 if (nb_args != 2) {
1776 return;
1779 len = strlen(str);
1780 readline_set_completion_index(rs, len);
1781 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1782 MAX_QUEUE_NUM);
1783 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1784 const char *name = ncs[i]->name;
1785 if (strncmp(str, name, len)) {
1786 continue;
1788 if (ncs[i]->is_netdev) {
1789 readline_add_completion(rs, name);
1794 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1796 size_t len;
1798 len = strlen(str);
1799 readline_set_completion_index(rs, len);
1800 if (nb_args == 2) {
1801 TraceEventIter iter;
1802 TraceEvent *ev;
1803 char *pattern = g_strdup_printf("%s*", str);
1804 trace_event_iter_init_pattern(&iter, pattern);
1805 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1806 readline_add_completion(rs, trace_event_get_name(ev));
1808 g_free(pattern);
1812 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1814 size_t len;
1816 len = strlen(str);
1817 readline_set_completion_index(rs, len);
1818 if (nb_args == 2) {
1819 TraceEventIter iter;
1820 TraceEvent *ev;
1821 char *pattern = g_strdup_printf("%s*", str);
1822 trace_event_iter_init_pattern(&iter, pattern);
1823 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1824 readline_add_completion(rs, trace_event_get_name(ev));
1826 g_free(pattern);
1827 } else if (nb_args == 3) {
1828 add_completion_option(rs, str, "on");
1829 add_completion_option(rs, str, "off");
1833 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1835 int i;
1837 if (nb_args != 2) {
1838 return;
1840 readline_set_completion_index(rs, strlen(str));
1841 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1842 add_completion_option(rs, str, WatchdogAction_str(i));
1846 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1847 const char *str)
1849 size_t len;
1851 len = strlen(str);
1852 readline_set_completion_index(rs, len);
1853 if (nb_args == 2) {
1854 int i;
1855 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1856 const char *name = MigrationCapability_str(i);
1857 if (!strncmp(str, name, len)) {
1858 readline_add_completion(rs, name);
1861 } else if (nb_args == 3) {
1862 add_completion_option(rs, str, "on");
1863 add_completion_option(rs, str, "off");
1867 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1868 const char *str)
1870 size_t len;
1872 len = strlen(str);
1873 readline_set_completion_index(rs, len);
1874 if (nb_args == 2) {
1875 int i;
1876 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1877 const char *name = MigrationParameter_str(i);
1878 if (!strncmp(str, name, len)) {
1879 readline_add_completion(rs, name);
1885 static void vm_completion(ReadLineState *rs, const char *str)
1887 size_t len;
1888 BlockDriverState *bs;
1889 BdrvNextIterator it;
1891 len = strlen(str);
1892 readline_set_completion_index(rs, len);
1894 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1895 SnapshotInfoList *snapshots, *snapshot;
1896 AioContext *ctx = bdrv_get_aio_context(bs);
1897 bool ok = false;
1899 aio_context_acquire(ctx);
1900 if (bdrv_can_snapshot(bs)) {
1901 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1903 aio_context_release(ctx);
1904 if (!ok) {
1905 continue;
1908 snapshot = snapshots;
1909 while (snapshot) {
1910 char *completion = snapshot->value->name;
1911 if (!strncmp(str, completion, len)) {
1912 readline_add_completion(rs, completion);
1914 completion = snapshot->value->id;
1915 if (!strncmp(str, completion, len)) {
1916 readline_add_completion(rs, completion);
1918 snapshot = snapshot->next;
1920 qapi_free_SnapshotInfoList(snapshots);
1925 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1927 if (nb_args == 2) {
1928 vm_completion(rs, str);
1932 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1934 if (nb_args == 2) {
1935 vm_completion(rs, str);
1939 static int
1940 compare_mon_cmd(const void *a, const void *b)
1942 return strcmp(((const HMPCommand *)a)->name,
1943 ((const HMPCommand *)b)->name);
1946 static void sortcmdlist(void)
1948 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1949 sizeof(*hmp_cmds),
1950 compare_mon_cmd);
1951 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1952 sizeof(*hmp_info_cmds),
1953 compare_mon_cmd);
1956 void monitor_register_hmp(const char *name, bool info,
1957 void (*cmd)(Monitor *mon, const QDict *qdict))
1959 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1961 while (table->name != NULL) {
1962 if (strcmp(table->name, name) == 0) {
1963 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1964 table->cmd = cmd;
1965 return;
1967 table++;
1969 g_assert_not_reached();
1972 void monitor_register_hmp_info_hrt(const char *name,
1973 HumanReadableText *(*handler)(Error **errp))
1975 HMPCommand *table = hmp_info_cmds;
1977 while (table->name != NULL) {
1978 if (strcmp(table->name, name) == 0) {
1979 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1980 table->cmd_info_hrt = handler;
1981 return;
1983 table++;
1985 g_assert_not_reached();
1988 void monitor_init_globals(void)
1990 monitor_init_globals_core();
1991 monitor_init_qmp_commands();
1992 sortcmdlist();
1993 qemu_mutex_init(&mon_fdsets_lock);