Merge tag 'fixes-20220408-pull-request' of git://git.kraxel.org/qemu into staging
[qemu/ar7.git] / monitor / misc.c
bloba756dbd6db0c650ff20086248e0ab7a570458b66
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/tcg.h"
52 #include "sysemu/tpm.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 /* file descriptors passed via SCM_RIGHTS */
89 typedef struct mon_fd_t mon_fd_t;
90 struct mon_fd_t {
91 char *name;
92 int fd;
93 QLIST_ENTRY(mon_fd_t) next;
96 /* file descriptor associated with a file descriptor set */
97 typedef struct MonFdsetFd MonFdsetFd;
98 struct MonFdsetFd {
99 int fd;
100 bool removed;
101 char *opaque;
102 QLIST_ENTRY(MonFdsetFd) next;
105 /* file descriptor set containing fds passed via SCM_RIGHTS */
106 typedef struct MonFdset MonFdset;
107 struct MonFdset {
108 int64_t id;
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)
123 char *output = NULL;
124 MonitorHMP hmp = {};
126 monitor_data_init(&hmp.common, false, true, false);
128 if (has_cpu_index) {
129 int ret = monitor_set_cpu(&hmp.common, cpu_index);
130 if (ret < 0) {
131 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
132 "a CPU number");
133 goto out;
137 handle_hmp_command(&hmp, command_line);
139 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
140 output = g_strdup(hmp.common.outbuf->str);
143 out:
144 monitor_data_destroy(&hmp.common);
145 return output;
149 * Is @name in the '|' separated list of names @list?
151 int hmp_compare_cmd(const char *name, const char *list)
153 const char *p, *pstart;
154 int len;
155 len = strlen(name);
156 p = list;
157 for (;;) {
158 pstart = p;
159 p = qemu_strchrnul(p, '|');
160 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
161 return 1;
163 if (*p == '\0') {
164 break;
166 p++;
168 return 0;
171 static void do_help_cmd(Monitor *mon, const QDict *qdict)
173 help_cmd(mon, qdict_get_try_str(qdict, "name"));
176 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
178 const char *tp_name = qdict_get_str(qdict, "name");
179 bool new_state = qdict_get_bool(qdict, "option");
180 bool has_vcpu = qdict_haskey(qdict, "vcpu");
181 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
182 Error *local_err = NULL;
184 if (vcpu < 0) {
185 monitor_printf(mon, "argument vcpu must be positive");
186 return;
189 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
190 if (local_err) {
191 error_report_err(local_err);
195 #ifdef CONFIG_TRACE_SIMPLE
196 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
198 const char *op = qdict_get_try_str(qdict, "op");
199 const char *arg = qdict_get_try_str(qdict, "arg");
201 if (!op) {
202 st_print_trace_file_status();
203 } else if (!strcmp(op, "on")) {
204 st_set_trace_file_enabled(true);
205 } else if (!strcmp(op, "off")) {
206 st_set_trace_file_enabled(false);
207 } else if (!strcmp(op, "flush")) {
208 st_flush_trace_buffer();
209 } else if (!strcmp(op, "set")) {
210 if (arg) {
211 st_set_trace_file(arg);
213 } else {
214 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
215 help_cmd(mon, "trace-file");
218 #endif
220 static void hmp_info_help(Monitor *mon, const QDict *qdict)
222 help_cmd(mon, "info");
225 static void monitor_init_qmp_commands(void)
228 * Two command lists:
229 * - qmp_commands contains all QMP commands
230 * - qmp_cap_negotiation_commands contains just
231 * "qmp_capabilities", to enforce capability negotiation
234 qmp_init_marshal(&qmp_commands);
236 qmp_register_command(&qmp_commands, "device_add",
237 qmp_device_add, 0, 0);
239 QTAILQ_INIT(&qmp_cap_negotiation_commands);
240 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
241 qmp_marshal_qmp_capabilities,
242 QCO_ALLOW_PRECONFIG, 0);
245 /* Set the current CPU defined by the user. Callers must hold BQL. */
246 int monitor_set_cpu(Monitor *mon, int cpu_index)
248 CPUState *cpu;
250 cpu = qemu_get_cpu(cpu_index);
251 if (cpu == NULL) {
252 return -1;
254 g_free(mon->mon_cpu_path);
255 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
256 return 0;
259 /* Callers must hold BQL. */
260 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
262 CPUState *cpu = NULL;
264 if (mon->mon_cpu_path) {
265 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
266 TYPE_CPU, NULL);
267 if (!cpu) {
268 g_free(mon->mon_cpu_path);
269 mon->mon_cpu_path = NULL;
272 if (!mon->mon_cpu_path) {
273 if (!first_cpu) {
274 return NULL;
276 monitor_set_cpu(mon, first_cpu->cpu_index);
277 cpu = first_cpu;
279 assert(cpu != NULL);
280 if (synchronize) {
281 cpu_synchronize_state(cpu);
283 return cpu;
286 CPUState *mon_get_cpu(Monitor *mon)
288 return mon_get_cpu_sync(mon, true);
291 CPUArchState *mon_get_cpu_env(Monitor *mon)
293 CPUState *cs = mon_get_cpu(mon);
295 return cs ? cs->env_ptr : NULL;
298 int monitor_get_cpu_index(Monitor *mon)
300 CPUState *cs = mon_get_cpu_sync(mon, false);
302 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
305 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
307 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
308 CPUState *cs;
310 if (all_cpus) {
311 CPU_FOREACH(cs) {
312 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
313 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
315 } else {
316 cs = mon_get_cpu(mon);
318 if (!cs) {
319 monitor_printf(mon, "No CPU available\n");
320 return;
323 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
327 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
329 int64_t max = qdict_get_try_int(qdict, "max", 10);
330 bool mean = qdict_get_try_bool(qdict, "mean", false);
331 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
332 enum QSPSortBy sort_by;
334 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
335 qsp_report(max, sort_by, coalesce);
338 static void hmp_info_history(Monitor *mon, const QDict *qdict)
340 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
341 int i;
342 const char *str;
344 if (!hmp_mon->rs) {
345 return;
347 i = 0;
348 for(;;) {
349 str = readline_get_history(hmp_mon->rs, i);
350 if (!str) {
351 break;
353 monitor_printf(mon, "%d: '%s'\n", i, str);
354 i++;
358 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
360 const char *name = qdict_get_try_str(qdict, "name");
361 bool has_vcpu = qdict_haskey(qdict, "vcpu");
362 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
363 TraceEventInfoList *events;
364 TraceEventInfoList *elem;
365 Error *local_err = NULL;
367 if (name == NULL) {
368 name = "*";
370 if (vcpu < 0) {
371 monitor_printf(mon, "argument vcpu must be positive");
372 return;
375 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
376 if (local_err) {
377 error_report_err(local_err);
378 return;
381 for (elem = events; elem != NULL; elem = elem->next) {
382 monitor_printf(mon, "%s : state %u\n",
383 elem->value->name,
384 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
386 qapi_free_TraceEventInfoList(events);
389 void qmp_client_migrate_info(const char *protocol, const char *hostname,
390 bool has_port, int64_t port,
391 bool has_tls_port, int64_t tls_port,
392 bool has_cert_subject, const char *cert_subject,
393 Error **errp)
395 if (strcmp(protocol, "spice") == 0) {
396 if (!qemu_using_spice(errp)) {
397 return;
400 if (!has_port && !has_tls_port) {
401 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
402 return;
405 if (qemu_spice.migrate_info(hostname,
406 has_port ? port : -1,
407 has_tls_port ? tls_port : -1,
408 cert_subject)) {
409 error_setg(errp, "Could not set up display for migration");
410 return;
412 return;
415 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
418 static void hmp_logfile(Monitor *mon, const QDict *qdict)
420 Error *err = NULL;
422 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
423 if (err) {
424 error_report_err(err);
428 static void hmp_log(Monitor *mon, const QDict *qdict)
430 int mask;
431 const char *items = qdict_get_str(qdict, "items");
433 if (!strcmp(items, "none")) {
434 mask = 0;
435 } else {
436 mask = qemu_str_to_log_mask(items);
437 if (!mask) {
438 help_cmd(mon, "log");
439 return;
442 qemu_set_log(mask);
445 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
447 const char *option = qdict_get_try_str(qdict, "option");
448 if (!option || !strcmp(option, "on")) {
449 singlestep = 1;
450 } else if (!strcmp(option, "off")) {
451 singlestep = 0;
452 } else {
453 monitor_printf(mon, "unexpected option %s\n", option);
457 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
459 const char *device = qdict_get_try_str(qdict, "device");
460 if (!device) {
461 device = "tcp::" DEFAULT_GDBSTUB_PORT;
464 if (gdbserver_start(device) < 0) {
465 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
466 device);
467 } else if (strcmp(device, "none") == 0) {
468 monitor_printf(mon, "Disabled gdbserver\n");
469 } else {
470 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
471 device);
475 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
477 Error *err = NULL;
478 WatchdogAction action;
479 char *qapi_value;
481 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
482 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
483 g_free(qapi_value);
484 if (err) {
485 hmp_handle_error(mon, err);
486 return;
488 qmp_watchdog_set_action(action, &error_abort);
491 static void monitor_printc(Monitor *mon, int c)
493 monitor_printf(mon, "'");
494 switch(c) {
495 case '\'':
496 monitor_printf(mon, "\\'");
497 break;
498 case '\\':
499 monitor_printf(mon, "\\\\");
500 break;
501 case '\n':
502 monitor_printf(mon, "\\n");
503 break;
504 case '\r':
505 monitor_printf(mon, "\\r");
506 break;
507 default:
508 if (c >= 32 && c <= 126) {
509 monitor_printf(mon, "%c", c);
510 } else {
511 monitor_printf(mon, "\\x%02x", c);
513 break;
515 monitor_printf(mon, "'");
518 static void memory_dump(Monitor *mon, int count, int format, int wsize,
519 hwaddr addr, int is_physical)
521 int l, line_size, i, max_digits, len;
522 uint8_t buf[16];
523 uint64_t v;
524 CPUState *cs = mon_get_cpu(mon);
526 if (!cs && (format == 'i' || !is_physical)) {
527 monitor_printf(mon, "Can not dump without CPU\n");
528 return;
531 if (format == 'i') {
532 monitor_disas(mon, cs, addr, count, is_physical);
533 return;
536 len = wsize * count;
537 if (wsize == 1) {
538 line_size = 8;
539 } else {
540 line_size = 16;
542 max_digits = 0;
544 switch(format) {
545 case 'o':
546 max_digits = DIV_ROUND_UP(wsize * 8, 3);
547 break;
548 default:
549 case 'x':
550 max_digits = (wsize * 8) / 4;
551 break;
552 case 'u':
553 case 'd':
554 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
555 break;
556 case 'c':
557 wsize = 1;
558 break;
561 while (len > 0) {
562 if (is_physical) {
563 monitor_printf(mon, TARGET_FMT_plx ":", addr);
564 } else {
565 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
567 l = len;
568 if (l > line_size)
569 l = line_size;
570 if (is_physical) {
571 AddressSpace *as = cs ? cs->as : &address_space_memory;
572 MemTxResult r = address_space_read(as, addr,
573 MEMTXATTRS_UNSPECIFIED, buf, l);
574 if (r != MEMTX_OK) {
575 monitor_printf(mon, " Cannot access memory\n");
576 break;
578 } else {
579 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
580 monitor_printf(mon, " Cannot access memory\n");
581 break;
584 i = 0;
585 while (i < l) {
586 switch(wsize) {
587 default:
588 case 1:
589 v = ldub_p(buf + i);
590 break;
591 case 2:
592 v = lduw_p(buf + i);
593 break;
594 case 4:
595 v = (uint32_t)ldl_p(buf + i);
596 break;
597 case 8:
598 v = ldq_p(buf + i);
599 break;
601 monitor_printf(mon, " ");
602 switch(format) {
603 case 'o':
604 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
605 break;
606 case 'x':
607 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
608 break;
609 case 'u':
610 monitor_printf(mon, "%*" PRIu64, max_digits, v);
611 break;
612 case 'd':
613 monitor_printf(mon, "%*" PRId64, max_digits, v);
614 break;
615 case 'c':
616 monitor_printc(mon, v);
617 break;
619 i += wsize;
621 monitor_printf(mon, "\n");
622 addr += l;
623 len -= l;
627 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
629 int count = qdict_get_int(qdict, "count");
630 int format = qdict_get_int(qdict, "format");
631 int size = qdict_get_int(qdict, "size");
632 target_long addr = qdict_get_int(qdict, "addr");
634 memory_dump(mon, count, format, size, addr, 0);
637 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
639 int count = qdict_get_int(qdict, "count");
640 int format = qdict_get_int(qdict, "format");
641 int size = qdict_get_int(qdict, "size");
642 hwaddr addr = qdict_get_int(qdict, "addr");
644 memory_dump(mon, count, format, size, addr, 1);
647 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
649 Int128 gpa_region_size;
650 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
651 addr, size);
653 if (!mrs.mr) {
654 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
655 return NULL;
658 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
659 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
660 memory_region_unref(mrs.mr);
661 return NULL;
664 gpa_region_size = int128_make64(size);
665 if (int128_lt(mrs.size, gpa_region_size)) {
666 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
667 " exceeded.", addr);
668 memory_region_unref(mrs.mr);
669 return NULL;
672 *p_mr = mrs.mr;
673 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
676 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
678 hwaddr addr = qdict_get_int(qdict, "addr");
679 Error *local_err = NULL;
680 MemoryRegion *mr = NULL;
681 void *ptr;
683 ptr = gpa2hva(&mr, addr, 1, &local_err);
684 if (local_err) {
685 error_report_err(local_err);
686 return;
689 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
690 " (%s) is %p\n",
691 addr, mr->name, ptr);
693 memory_region_unref(mr);
696 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
698 target_ulong addr = qdict_get_int(qdict, "addr");
699 MemTxAttrs attrs;
700 CPUState *cs = mon_get_cpu(mon);
701 hwaddr gpa;
703 if (!cs) {
704 monitor_printf(mon, "No cpu\n");
705 return;
708 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
709 if (gpa == -1) {
710 monitor_printf(mon, "Unmapped\n");
711 } else {
712 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
713 gpa + (addr & ~TARGET_PAGE_MASK));
717 #ifdef CONFIG_LINUX
718 static uint64_t vtop(void *ptr, Error **errp)
720 uint64_t pinfo;
721 uint64_t ret = -1;
722 uintptr_t addr = (uintptr_t) ptr;
723 uintptr_t pagesize = qemu_real_host_page_size;
724 off_t offset = addr / pagesize * sizeof(pinfo);
725 int fd;
727 fd = open("/proc/self/pagemap", O_RDONLY);
728 if (fd == -1) {
729 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
730 return -1;
733 /* Force copy-on-write if necessary. */
734 qatomic_add((uint8_t *)ptr, 0);
736 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
737 error_setg_errno(errp, errno, "Cannot read pagemap");
738 goto out;
740 if ((pinfo & (1ull << 63)) == 0) {
741 error_setg(errp, "Page not present");
742 goto out;
744 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
746 out:
747 close(fd);
748 return ret;
751 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
753 hwaddr addr = qdict_get_int(qdict, "addr");
754 Error *local_err = NULL;
755 MemoryRegion *mr = NULL;
756 void *ptr;
757 uint64_t physaddr;
759 ptr = gpa2hva(&mr, addr, 1, &local_err);
760 if (local_err) {
761 error_report_err(local_err);
762 return;
765 physaddr = vtop(ptr, &local_err);
766 if (local_err) {
767 error_report_err(local_err);
768 } else {
769 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
770 " (%s) is 0x%" PRIx64 "\n",
771 addr, mr->name, (uint64_t) physaddr);
774 memory_region_unref(mr);
776 #endif
778 static void do_print(Monitor *mon, const QDict *qdict)
780 int format = qdict_get_int(qdict, "format");
781 hwaddr val = qdict_get_int(qdict, "val");
783 switch(format) {
784 case 'o':
785 monitor_printf(mon, "%#" HWADDR_PRIo, val);
786 break;
787 case 'x':
788 monitor_printf(mon, "%#" HWADDR_PRIx, val);
789 break;
790 case 'u':
791 monitor_printf(mon, "%" HWADDR_PRIu, val);
792 break;
793 default:
794 case 'd':
795 monitor_printf(mon, "%" HWADDR_PRId, val);
796 break;
797 case 'c':
798 monitor_printc(mon, val);
799 break;
801 monitor_printf(mon, "\n");
804 static void hmp_sum(Monitor *mon, const QDict *qdict)
806 uint32_t addr;
807 uint16_t sum;
808 uint32_t start = qdict_get_int(qdict, "start");
809 uint32_t size = qdict_get_int(qdict, "size");
811 sum = 0;
812 for(addr = start; addr < (start + size); addr++) {
813 uint8_t val = address_space_ldub(&address_space_memory, addr,
814 MEMTXATTRS_UNSPECIFIED, NULL);
815 /* BSD sum algorithm ('sum' Unix command) */
816 sum = (sum >> 1) | (sum << 15);
817 sum += val;
819 monitor_printf(mon, "%05d\n", sum);
822 static int mouse_button_state;
824 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
826 int dx, dy, dz, button;
827 const char *dx_str = qdict_get_str(qdict, "dx_str");
828 const char *dy_str = qdict_get_str(qdict, "dy_str");
829 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
831 dx = strtol(dx_str, NULL, 0);
832 dy = strtol(dy_str, NULL, 0);
833 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
834 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
836 if (dz_str) {
837 dz = strtol(dz_str, NULL, 0);
838 if (dz != 0) {
839 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
840 qemu_input_queue_btn(NULL, button, true);
841 qemu_input_event_sync();
842 qemu_input_queue_btn(NULL, button, false);
845 qemu_input_event_sync();
848 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
850 static uint32_t bmap[INPUT_BUTTON__MAX] = {
851 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
852 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
853 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
855 int button_state = qdict_get_int(qdict, "button_state");
857 if (mouse_button_state == button_state) {
858 return;
860 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
861 qemu_input_event_sync();
862 mouse_button_state = button_state;
865 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
867 int size = qdict_get_int(qdict, "size");
868 int addr = qdict_get_int(qdict, "addr");
869 int has_index = qdict_haskey(qdict, "index");
870 uint32_t val;
871 int suffix;
873 if (has_index) {
874 int index = qdict_get_int(qdict, "index");
875 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
876 addr++;
878 addr &= 0xffff;
880 switch(size) {
881 default:
882 case 1:
883 val = cpu_inb(addr);
884 suffix = 'b';
885 break;
886 case 2:
887 val = cpu_inw(addr);
888 suffix = 'w';
889 break;
890 case 4:
891 val = cpu_inl(addr);
892 suffix = 'l';
893 break;
895 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
896 suffix, addr, size * 2, val);
899 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
901 int size = qdict_get_int(qdict, "size");
902 int addr = qdict_get_int(qdict, "addr");
903 int val = qdict_get_int(qdict, "val");
905 addr &= IOPORTS_MASK;
907 switch (size) {
908 default:
909 case 1:
910 cpu_outb(addr, val);
911 break;
912 case 2:
913 cpu_outw(addr, val);
914 break;
915 case 4:
916 cpu_outl(addr, val);
917 break;
921 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
923 Error *local_err = NULL;
924 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
926 qemu_boot_set(bootdevice, &local_err);
927 if (local_err) {
928 error_report_err(local_err);
929 } else {
930 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
934 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
936 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
937 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
938 bool owner = qdict_get_try_bool(qdict, "owner", false);
939 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
941 mtree_info(flatview, dispatch_tree, owner, disabled);
944 /* Capture support */
945 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
947 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
949 int i;
950 CaptureState *s;
952 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
953 monitor_printf(mon, "[%d]: ", i);
954 s->ops.info (s->opaque);
958 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
960 int i;
961 int n = qdict_get_int(qdict, "n");
962 CaptureState *s;
964 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
965 if (i == n) {
966 s->ops.destroy (s->opaque);
967 QLIST_REMOVE (s, entries);
968 g_free (s);
969 return;
974 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
976 const char *path = qdict_get_str(qdict, "path");
977 int freq = qdict_get_try_int(qdict, "freq", 44100);
978 int bits = qdict_get_try_int(qdict, "bits", 16);
979 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
980 const char *audiodev = qdict_get_str(qdict, "audiodev");
981 CaptureState *s;
982 AudioState *as = audio_state_by_name(audiodev);
984 if (!as) {
985 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
986 return;
989 s = g_malloc0 (sizeof (*s));
991 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
992 monitor_printf(mon, "Failed to add wave capture\n");
993 g_free (s);
994 return;
996 QLIST_INSERT_HEAD (&capture_head, s, entries);
999 void qmp_getfd(const char *fdname, Error **errp)
1001 Monitor *cur_mon = monitor_cur();
1002 mon_fd_t *monfd;
1003 int fd, tmp_fd;
1005 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1006 if (fd == -1) {
1007 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1008 return;
1011 if (qemu_isdigit(fdname[0])) {
1012 close(fd);
1013 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1014 "a name not starting with a digit");
1015 return;
1018 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1019 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1020 if (strcmp(monfd->name, fdname) != 0) {
1021 continue;
1024 tmp_fd = monfd->fd;
1025 monfd->fd = fd;
1026 /* Make sure close() is outside critical section */
1027 close(tmp_fd);
1028 return;
1031 monfd = g_new0(mon_fd_t, 1);
1032 monfd->name = g_strdup(fdname);
1033 monfd->fd = fd;
1035 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1038 void qmp_closefd(const char *fdname, Error **errp)
1040 Monitor *cur_mon = monitor_cur();
1041 mon_fd_t *monfd;
1042 int tmp_fd;
1044 qemu_mutex_lock(&cur_mon->mon_lock);
1045 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1046 if (strcmp(monfd->name, fdname) != 0) {
1047 continue;
1050 QLIST_REMOVE(monfd, next);
1051 tmp_fd = monfd->fd;
1052 g_free(monfd->name);
1053 g_free(monfd);
1054 qemu_mutex_unlock(&cur_mon->mon_lock);
1055 /* Make sure close() is outside critical section */
1056 close(tmp_fd);
1057 return;
1060 qemu_mutex_unlock(&cur_mon->mon_lock);
1061 error_setg(errp, "File descriptor named '%s' not found", fdname);
1064 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1066 mon_fd_t *monfd;
1068 QEMU_LOCK_GUARD(&mon->mon_lock);
1069 QLIST_FOREACH(monfd, &mon->fds, next) {
1070 int fd;
1072 if (strcmp(monfd->name, fdname) != 0) {
1073 continue;
1076 fd = monfd->fd;
1078 /* caller takes ownership of fd */
1079 QLIST_REMOVE(monfd, next);
1080 g_free(monfd->name);
1081 g_free(monfd);
1083 return fd;
1086 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1087 return -1;
1090 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1092 MonFdsetFd *mon_fdset_fd;
1093 MonFdsetFd *mon_fdset_fd_next;
1095 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1096 if ((mon_fdset_fd->removed ||
1097 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1098 runstate_is_running()) {
1099 close(mon_fdset_fd->fd);
1100 g_free(mon_fdset_fd->opaque);
1101 QLIST_REMOVE(mon_fdset_fd, next);
1102 g_free(mon_fdset_fd);
1106 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1107 QLIST_REMOVE(mon_fdset, next);
1108 g_free(mon_fdset);
1112 void monitor_fdsets_cleanup(void)
1114 MonFdset *mon_fdset;
1115 MonFdset *mon_fdset_next;
1117 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1118 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1119 monitor_fdset_cleanup(mon_fdset);
1123 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1124 const char *opaque, Error **errp)
1126 int fd;
1127 Monitor *mon = monitor_cur();
1128 AddfdInfo *fdinfo;
1130 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1131 if (fd == -1) {
1132 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1133 goto error;
1136 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1137 has_opaque, opaque, errp);
1138 if (fdinfo) {
1139 return fdinfo;
1142 error:
1143 if (fd != -1) {
1144 close(fd);
1146 return NULL;
1149 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1151 MonFdset *mon_fdset;
1152 MonFdsetFd *mon_fdset_fd;
1153 char fd_str[60];
1155 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1156 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1157 if (mon_fdset->id != fdset_id) {
1158 continue;
1160 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1161 if (has_fd) {
1162 if (mon_fdset_fd->fd != fd) {
1163 continue;
1165 mon_fdset_fd->removed = true;
1166 break;
1167 } else {
1168 mon_fdset_fd->removed = true;
1171 if (has_fd && !mon_fdset_fd) {
1172 goto error;
1174 monitor_fdset_cleanup(mon_fdset);
1175 return;
1178 error:
1179 if (has_fd) {
1180 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1181 fdset_id, fd);
1182 } else {
1183 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1185 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1188 FdsetInfoList *qmp_query_fdsets(Error **errp)
1190 MonFdset *mon_fdset;
1191 MonFdsetFd *mon_fdset_fd;
1192 FdsetInfoList *fdset_list = NULL;
1194 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1195 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1196 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1198 fdset_info->fdset_id = mon_fdset->id;
1200 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1201 FdsetFdInfo *fdsetfd_info;
1203 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1204 fdsetfd_info->fd = mon_fdset_fd->fd;
1205 if (mon_fdset_fd->opaque) {
1206 fdsetfd_info->has_opaque = true;
1207 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1208 } else {
1209 fdsetfd_info->has_opaque = false;
1212 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1215 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1218 return fdset_list;
1221 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1222 bool has_opaque, const char *opaque,
1223 Error **errp)
1225 MonFdset *mon_fdset = NULL;
1226 MonFdsetFd *mon_fdset_fd;
1227 AddfdInfo *fdinfo;
1229 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1230 if (has_fdset_id) {
1231 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1232 /* Break if match found or match impossible due to ordering by ID */
1233 if (fdset_id <= mon_fdset->id) {
1234 if (fdset_id < mon_fdset->id) {
1235 mon_fdset = NULL;
1237 break;
1242 if (mon_fdset == NULL) {
1243 int64_t fdset_id_prev = -1;
1244 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1246 if (has_fdset_id) {
1247 if (fdset_id < 0) {
1248 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1249 "a non-negative value");
1250 return NULL;
1252 /* Use specified fdset ID */
1253 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1254 mon_fdset_cur = mon_fdset;
1255 if (fdset_id < mon_fdset_cur->id) {
1256 break;
1259 } else {
1260 /* Use first available fdset ID */
1261 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1262 mon_fdset_cur = mon_fdset;
1263 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1264 fdset_id_prev = mon_fdset_cur->id;
1265 continue;
1267 break;
1271 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1272 if (has_fdset_id) {
1273 mon_fdset->id = fdset_id;
1274 } else {
1275 mon_fdset->id = fdset_id_prev + 1;
1278 /* The fdset list is ordered by fdset ID */
1279 if (!mon_fdset_cur) {
1280 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1281 } else if (mon_fdset->id < mon_fdset_cur->id) {
1282 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1283 } else {
1284 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1288 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1289 mon_fdset_fd->fd = fd;
1290 mon_fdset_fd->removed = false;
1291 if (has_opaque) {
1292 mon_fdset_fd->opaque = g_strdup(opaque);
1294 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1296 fdinfo = g_malloc0(sizeof(*fdinfo));
1297 fdinfo->fdset_id = mon_fdset->id;
1298 fdinfo->fd = mon_fdset_fd->fd;
1300 return fdinfo;
1303 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1305 #ifdef _WIN32
1306 return -ENOENT;
1307 #else
1308 MonFdset *mon_fdset;
1310 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1311 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1312 MonFdsetFd *mon_fdset_fd;
1313 MonFdsetFd *mon_fdset_fd_dup;
1314 int fd = -1;
1315 int dup_fd;
1316 int mon_fd_flags;
1318 if (mon_fdset->id != fdset_id) {
1319 continue;
1322 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1323 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1324 if (mon_fd_flags == -1) {
1325 return -1;
1328 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1329 fd = mon_fdset_fd->fd;
1330 break;
1334 if (fd == -1) {
1335 errno = EACCES;
1336 return -1;
1339 dup_fd = qemu_dup_flags(fd, flags);
1340 if (dup_fd == -1) {
1341 return -1;
1344 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1345 mon_fdset_fd_dup->fd = dup_fd;
1346 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1347 return dup_fd;
1350 errno = ENOENT;
1351 return -1;
1352 #endif
1355 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1357 MonFdset *mon_fdset;
1358 MonFdsetFd *mon_fdset_fd_dup;
1360 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1361 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1362 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1363 if (mon_fdset_fd_dup->fd == dup_fd) {
1364 if (remove) {
1365 QLIST_REMOVE(mon_fdset_fd_dup, next);
1366 g_free(mon_fdset_fd_dup);
1367 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1368 monitor_fdset_cleanup(mon_fdset);
1370 return -1;
1371 } else {
1372 return mon_fdset->id;
1378 return -1;
1381 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1383 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1386 void monitor_fdset_dup_fd_remove(int dup_fd)
1388 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1391 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1393 int fd;
1394 Error *local_err = NULL;
1396 if (!qemu_isdigit(fdname[0]) && mon) {
1397 fd = monitor_get_fd(mon, fdname, &local_err);
1398 } else {
1399 fd = qemu_parse_fd(fdname);
1400 if (fd == -1) {
1401 error_setg(&local_err, "Invalid file descriptor number '%s'",
1402 fdname);
1405 if (local_err) {
1406 error_propagate(errp, local_err);
1407 assert(fd == -1);
1408 } else {
1409 assert(fd != -1);
1412 return fd;
1415 /* Please update hmp-commands.hx when adding or changing commands */
1416 static HMPCommand hmp_info_cmds[] = {
1417 #include "hmp-commands-info.h"
1418 { NULL, NULL, },
1421 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1422 HMPCommand hmp_cmds[] = {
1423 #include "hmp-commands.h"
1424 { NULL, NULL, },
1428 * Set @pval to the value in the register identified by @name.
1429 * return 0 if OK, -1 if not found
1431 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1433 const MonitorDef *md = target_monitor_defs();
1434 CPUState *cs = mon_get_cpu(mon);
1435 void *ptr;
1436 uint64_t tmp = 0;
1437 int ret;
1439 if (cs == NULL || md == NULL) {
1440 return -1;
1443 for(; md->name != NULL; md++) {
1444 if (hmp_compare_cmd(name, md->name)) {
1445 if (md->get_value) {
1446 *pval = md->get_value(mon, md, md->offset);
1447 } else {
1448 CPUArchState *env = mon_get_cpu_env(mon);
1449 ptr = (uint8_t *)env + md->offset;
1450 switch(md->type) {
1451 case MD_I32:
1452 *pval = *(int32_t *)ptr;
1453 break;
1454 case MD_TLONG:
1455 *pval = *(target_long *)ptr;
1456 break;
1457 default:
1458 *pval = 0;
1459 break;
1462 return 0;
1466 ret = target_get_monitor_def(cs, name, &tmp);
1467 if (!ret) {
1468 *pval = (target_long) tmp;
1471 return ret;
1474 static void add_completion_option(ReadLineState *rs, const char *str,
1475 const char *option)
1477 if (!str || !option) {
1478 return;
1480 if (!strncmp(option, str, strlen(str))) {
1481 readline_add_completion(rs, option);
1485 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1487 size_t len;
1488 ChardevBackendInfoList *list, *start;
1490 if (nb_args != 2) {
1491 return;
1493 len = strlen(str);
1494 readline_set_completion_index(rs, len);
1496 start = list = qmp_query_chardev_backends(NULL);
1497 while (list) {
1498 const char *chr_name = list->value->name;
1500 if (!strncmp(chr_name, str, len)) {
1501 readline_add_completion(rs, chr_name);
1503 list = list->next;
1505 qapi_free_ChardevBackendInfoList(start);
1508 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1510 size_t len;
1511 int i;
1513 if (nb_args != 2) {
1514 return;
1516 len = strlen(str);
1517 readline_set_completion_index(rs, len);
1518 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1519 add_completion_option(rs, str, NetClientDriver_str(i));
1523 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1525 GSList *list, *elt;
1526 size_t len;
1528 if (nb_args != 2) {
1529 return;
1532 len = strlen(str);
1533 readline_set_completion_index(rs, len);
1534 list = elt = object_class_get_list(TYPE_DEVICE, false);
1535 while (elt) {
1536 const char *name;
1537 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1538 TYPE_DEVICE);
1539 name = object_class_get_name(OBJECT_CLASS(dc));
1541 if (dc->user_creatable
1542 && !strncmp(name, str, len)) {
1543 readline_add_completion(rs, name);
1545 elt = elt->next;
1547 g_slist_free(list);
1550 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1552 GSList *list, *elt;
1553 size_t len;
1555 if (nb_args != 2) {
1556 return;
1559 len = strlen(str);
1560 readline_set_completion_index(rs, len);
1561 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1562 while (elt) {
1563 const char *name;
1565 name = object_class_get_name(OBJECT_CLASS(elt->data));
1566 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1567 readline_add_completion(rs, name);
1569 elt = elt->next;
1571 g_slist_free(list);
1574 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1576 GSList **list = opaque;
1577 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1579 if (dev == NULL) {
1580 return 0;
1583 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1584 *list = g_slist_append(*list, dev);
1587 return 0;
1590 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1592 GSList *list = NULL;
1594 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1596 return list;
1599 static void peripheral_device_del_completion(ReadLineState *rs,
1600 const char *str, size_t len)
1602 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1603 GSList *list, *item;
1605 list = qdev_build_hotpluggable_device_list(peripheral);
1606 if (!list) {
1607 return;
1610 for (item = list; item; item = g_slist_next(item)) {
1611 DeviceState *dev = item->data;
1613 if (dev->id && !strncmp(str, dev->id, len)) {
1614 readline_add_completion(rs, dev->id);
1618 g_slist_free(list);
1621 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1623 size_t len;
1624 ChardevInfoList *list, *start;
1626 if (nb_args != 2) {
1627 return;
1629 len = strlen(str);
1630 readline_set_completion_index(rs, len);
1632 start = list = qmp_query_chardev(NULL);
1633 while (list) {
1634 ChardevInfo *chr = list->value;
1636 if (!strncmp(chr->label, str, len)) {
1637 readline_add_completion(rs, chr->label);
1639 list = list->next;
1641 qapi_free_ChardevInfoList(start);
1644 static void ringbuf_completion(ReadLineState *rs, const char *str)
1646 size_t len;
1647 ChardevInfoList *list, *start;
1649 len = strlen(str);
1650 readline_set_completion_index(rs, len);
1652 start = list = qmp_query_chardev(NULL);
1653 while (list) {
1654 ChardevInfo *chr_info = list->value;
1656 if (!strncmp(chr_info->label, str, len)) {
1657 Chardev *chr = qemu_chr_find(chr_info->label);
1658 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1659 readline_add_completion(rs, chr_info->label);
1662 list = list->next;
1664 qapi_free_ChardevInfoList(start);
1667 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1669 if (nb_args != 2) {
1670 return;
1672 ringbuf_completion(rs, str);
1675 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1677 size_t len;
1679 if (nb_args != 2) {
1680 return;
1683 len = strlen(str);
1684 readline_set_completion_index(rs, len);
1685 peripheral_device_del_completion(rs, str, len);
1688 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1690 ObjectPropertyInfoList *list, *start;
1691 size_t len;
1693 if (nb_args != 2) {
1694 return;
1696 len = strlen(str);
1697 readline_set_completion_index(rs, len);
1699 start = list = qmp_qom_list("/objects", NULL);
1700 while (list) {
1701 ObjectPropertyInfo *info = list->value;
1703 if (!strncmp(info->type, "child<", 5)
1704 && !strncmp(info->name, str, len)) {
1705 readline_add_completion(rs, info->name);
1707 list = list->next;
1709 qapi_free_ObjectPropertyInfoList(start);
1712 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1714 int i;
1715 char *sep;
1716 size_t len;
1718 if (nb_args != 2) {
1719 return;
1721 sep = strrchr(str, '-');
1722 if (sep) {
1723 str = sep + 1;
1725 len = strlen(str);
1726 readline_set_completion_index(rs, len);
1727 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1728 if (!strncmp(str, QKeyCode_str(i), len)) {
1729 readline_add_completion(rs, QKeyCode_str(i));
1734 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1736 size_t len;
1738 len = strlen(str);
1739 readline_set_completion_index(rs, len);
1740 if (nb_args == 2) {
1741 NetClientState *ncs[MAX_QUEUE_NUM];
1742 int count, i;
1743 count = qemu_find_net_clients_except(NULL, ncs,
1744 NET_CLIENT_DRIVER_NONE,
1745 MAX_QUEUE_NUM);
1746 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1747 const char *name = ncs[i]->name;
1748 if (!strncmp(str, name, len)) {
1749 readline_add_completion(rs, name);
1752 } else if (nb_args == 3) {
1753 add_completion_option(rs, str, "on");
1754 add_completion_option(rs, str, "off");
1758 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1760 int len, count, i;
1761 NetClientState *ncs[MAX_QUEUE_NUM];
1763 if (nb_args != 2) {
1764 return;
1767 len = strlen(str);
1768 readline_set_completion_index(rs, len);
1769 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1770 MAX_QUEUE_NUM);
1771 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1772 const char *name = ncs[i]->name;
1773 if (strncmp(str, name, len)) {
1774 continue;
1776 if (ncs[i]->is_netdev) {
1777 readline_add_completion(rs, name);
1782 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1784 size_t len;
1786 len = strlen(str);
1787 readline_set_completion_index(rs, len);
1788 if (nb_args == 2) {
1789 TraceEventIter iter;
1790 TraceEvent *ev;
1791 char *pattern = g_strdup_printf("%s*", str);
1792 trace_event_iter_init_pattern(&iter, pattern);
1793 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1794 readline_add_completion(rs, trace_event_get_name(ev));
1796 g_free(pattern);
1800 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1802 size_t len;
1804 len = strlen(str);
1805 readline_set_completion_index(rs, len);
1806 if (nb_args == 2) {
1807 TraceEventIter iter;
1808 TraceEvent *ev;
1809 char *pattern = g_strdup_printf("%s*", str);
1810 trace_event_iter_init_pattern(&iter, pattern);
1811 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1812 readline_add_completion(rs, trace_event_get_name(ev));
1814 g_free(pattern);
1815 } else if (nb_args == 3) {
1816 add_completion_option(rs, str, "on");
1817 add_completion_option(rs, str, "off");
1821 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1823 int i;
1825 if (nb_args != 2) {
1826 return;
1828 readline_set_completion_index(rs, strlen(str));
1829 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1830 add_completion_option(rs, str, WatchdogAction_str(i));
1834 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1835 const char *str)
1837 size_t len;
1839 len = strlen(str);
1840 readline_set_completion_index(rs, len);
1841 if (nb_args == 2) {
1842 int i;
1843 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1844 const char *name = MigrationCapability_str(i);
1845 if (!strncmp(str, name, len)) {
1846 readline_add_completion(rs, name);
1849 } else if (nb_args == 3) {
1850 add_completion_option(rs, str, "on");
1851 add_completion_option(rs, str, "off");
1855 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1856 const char *str)
1858 size_t len;
1860 len = strlen(str);
1861 readline_set_completion_index(rs, len);
1862 if (nb_args == 2) {
1863 int i;
1864 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1865 const char *name = MigrationParameter_str(i);
1866 if (!strncmp(str, name, len)) {
1867 readline_add_completion(rs, name);
1873 static void vm_completion(ReadLineState *rs, const char *str)
1875 size_t len;
1876 BlockDriverState *bs;
1877 BdrvNextIterator it;
1879 len = strlen(str);
1880 readline_set_completion_index(rs, len);
1882 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1883 SnapshotInfoList *snapshots, *snapshot;
1884 AioContext *ctx = bdrv_get_aio_context(bs);
1885 bool ok = false;
1887 aio_context_acquire(ctx);
1888 if (bdrv_can_snapshot(bs)) {
1889 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1891 aio_context_release(ctx);
1892 if (!ok) {
1893 continue;
1896 snapshot = snapshots;
1897 while (snapshot) {
1898 char *completion = snapshot->value->name;
1899 if (!strncmp(str, completion, len)) {
1900 readline_add_completion(rs, completion);
1902 completion = snapshot->value->id;
1903 if (!strncmp(str, completion, len)) {
1904 readline_add_completion(rs, completion);
1906 snapshot = snapshot->next;
1908 qapi_free_SnapshotInfoList(snapshots);
1913 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1915 if (nb_args == 2) {
1916 vm_completion(rs, str);
1920 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1922 if (nb_args == 2) {
1923 vm_completion(rs, str);
1927 static int
1928 compare_mon_cmd(const void *a, const void *b)
1930 return strcmp(((const HMPCommand *)a)->name,
1931 ((const HMPCommand *)b)->name);
1934 static void sortcmdlist(void)
1936 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1937 sizeof(*hmp_cmds),
1938 compare_mon_cmd);
1939 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1940 sizeof(*hmp_info_cmds),
1941 compare_mon_cmd);
1944 void monitor_register_hmp(const char *name, bool info,
1945 void (*cmd)(Monitor *mon, const QDict *qdict))
1947 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1949 while (table->name != NULL) {
1950 if (strcmp(table->name, name) == 0) {
1951 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1952 table->cmd = cmd;
1953 return;
1955 table++;
1957 g_assert_not_reached();
1960 void monitor_register_hmp_info_hrt(const char *name,
1961 HumanReadableText *(*handler)(Error **errp))
1963 HMPCommand *table = hmp_info_cmds;
1965 while (table->name != NULL) {
1966 if (strcmp(table->name, name) == 0) {
1967 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1968 table->cmd_info_hrt = handler;
1969 return;
1971 table++;
1973 g_assert_not_reached();
1976 void monitor_init_globals(void)
1978 monitor_init_globals_core();
1979 monitor_init_qmp_commands();
1980 sortcmdlist();
1981 qemu_mutex_init(&mon_fdsets_lock);