ppc/pnv: Remove PHB4 version property
[qemu.git] / monitor / misc.c
bloba3a6e478444e80200d325e28075cf26c7a5b793b
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 "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "authz/list.h"
47 #include "qapi/util.h"
48 #include "sysemu/blockdev.h"
49 #include "sysemu/sysemu.h"
50 #include "sysemu/tcg.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qdict.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qstring.h"
55 #include "qom/object_interfaces.h"
56 #include "trace/control.h"
57 #include "monitor/hmp-target.h"
58 #include "monitor/hmp.h"
59 #ifdef CONFIG_TRACE_SIMPLE
60 #include "trace/simple.h"
61 #endif
62 #include "exec/memory.h"
63 #include "exec/exec-all.h"
64 #include "qemu/option.h"
65 #include "qemu/thread.h"
66 #include "block/qapi.h"
67 #include "block/block-hmp-cmds.h"
68 #include "qapi/qapi-commands-char.h"
69 #include "qapi/qapi-commands-control.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-run-state.h"
74 #include "qapi/qapi-commands-trace.h"
75 #include "qapi/qapi-commands-machine.h"
76 #include "qapi/qapi-init-commands.h"
77 #include "qapi/error.h"
78 #include "qapi/qmp-event.h"
79 #include "sysemu/cpus.h"
80 #include "qemu/cutils.h"
82 #if defined(TARGET_S390X)
83 #include "hw/s390x/storage-keys.h"
84 #include "hw/s390x/storage-attributes.h"
85 #endif
87 /* file descriptors passed via SCM_RIGHTS */
88 typedef struct mon_fd_t mon_fd_t;
89 struct mon_fd_t {
90 char *name;
91 int fd;
92 QLIST_ENTRY(mon_fd_t) next;
95 /* file descriptor associated with a file descriptor set */
96 typedef struct MonFdsetFd MonFdsetFd;
97 struct MonFdsetFd {
98 int fd;
99 bool removed;
100 char *opaque;
101 QLIST_ENTRY(MonFdsetFd) next;
104 /* file descriptor set containing fds passed via SCM_RIGHTS */
105 typedef struct MonFdset MonFdset;
106 struct MonFdset {
107 int64_t id;
108 QLIST_HEAD(, MonFdsetFd) fds;
109 QLIST_HEAD(, MonFdsetFd) dup_fds;
110 QLIST_ENTRY(MonFdset) next;
113 /* Protects mon_fdsets */
114 static QemuMutex mon_fdsets_lock;
115 static QLIST_HEAD(, MonFdset) mon_fdsets;
117 static HMPCommand hmp_info_cmds[];
119 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
120 int64_t cpu_index, Error **errp)
122 char *output = NULL;
123 MonitorHMP hmp = {};
125 monitor_data_init(&hmp.common, false, true, false);
127 if (has_cpu_index) {
128 int ret = monitor_set_cpu(&hmp.common, cpu_index);
129 if (ret < 0) {
130 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
131 "a CPU number");
132 goto out;
136 handle_hmp_command(&hmp, command_line);
138 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
139 output = g_strdup(hmp.common.outbuf->str);
142 out:
143 monitor_data_destroy(&hmp.common);
144 return output;
148 * Is @name in the '|' separated list of names @list?
150 int hmp_compare_cmd(const char *name, const char *list)
152 const char *p, *pstart;
153 int len;
154 len = strlen(name);
155 p = list;
156 for (;;) {
157 pstart = p;
158 p = qemu_strchrnul(p, '|');
159 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
160 return 1;
162 if (*p == '\0') {
163 break;
165 p++;
167 return 0;
170 static void do_help_cmd(Monitor *mon, const QDict *qdict)
172 help_cmd(mon, qdict_get_try_str(qdict, "name"));
175 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
177 const char *tp_name = qdict_get_str(qdict, "name");
178 bool new_state = qdict_get_bool(qdict, "option");
179 bool has_vcpu = qdict_haskey(qdict, "vcpu");
180 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
181 Error *local_err = NULL;
183 if (vcpu < 0) {
184 monitor_printf(mon, "argument vcpu must be positive");
185 return;
188 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
189 if (local_err) {
190 error_report_err(local_err);
194 #ifdef CONFIG_TRACE_SIMPLE
195 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
197 const char *op = qdict_get_try_str(qdict, "op");
198 const char *arg = qdict_get_try_str(qdict, "arg");
200 if (!op) {
201 st_print_trace_file_status();
202 } else if (!strcmp(op, "on")) {
203 st_set_trace_file_enabled(true);
204 } else if (!strcmp(op, "off")) {
205 st_set_trace_file_enabled(false);
206 } else if (!strcmp(op, "flush")) {
207 st_flush_trace_buffer();
208 } else if (!strcmp(op, "set")) {
209 if (arg) {
210 st_set_trace_file(arg);
212 } else {
213 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
214 help_cmd(mon, "trace-file");
217 #endif
219 static void hmp_info_help(Monitor *mon, const QDict *qdict)
221 help_cmd(mon, "info");
224 static void monitor_init_qmp_commands(void)
227 * Two command lists:
228 * - qmp_commands contains all QMP commands
229 * - qmp_cap_negotiation_commands contains just
230 * "qmp_capabilities", to enforce capability negotiation
233 qmp_init_marshal(&qmp_commands);
235 qmp_register_command(&qmp_commands, "device_add",
236 qmp_device_add, 0, 0);
238 QTAILQ_INIT(&qmp_cap_negotiation_commands);
239 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
240 qmp_marshal_qmp_capabilities,
241 QCO_ALLOW_PRECONFIG, 0);
244 /* Set the current CPU defined by the user. Callers must hold BQL. */
245 int monitor_set_cpu(Monitor *mon, int cpu_index)
247 CPUState *cpu;
249 cpu = qemu_get_cpu(cpu_index);
250 if (cpu == NULL) {
251 return -1;
253 g_free(mon->mon_cpu_path);
254 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
255 return 0;
258 /* Callers must hold BQL. */
259 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
261 CPUState *cpu = NULL;
263 if (mon->mon_cpu_path) {
264 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
265 TYPE_CPU, NULL);
266 if (!cpu) {
267 g_free(mon->mon_cpu_path);
268 mon->mon_cpu_path = NULL;
271 if (!mon->mon_cpu_path) {
272 if (!first_cpu) {
273 return NULL;
275 monitor_set_cpu(mon, first_cpu->cpu_index);
276 cpu = first_cpu;
278 assert(cpu != NULL);
279 if (synchronize) {
280 cpu_synchronize_state(cpu);
282 return cpu;
285 CPUState *mon_get_cpu(Monitor *mon)
287 return mon_get_cpu_sync(mon, true);
290 CPUArchState *mon_get_cpu_env(Monitor *mon)
292 CPUState *cs = mon_get_cpu(mon);
294 return cs ? cs->env_ptr : NULL;
297 int monitor_get_cpu_index(Monitor *mon)
299 CPUState *cs = mon_get_cpu_sync(mon, false);
301 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
304 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
306 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
307 CPUState *cs;
309 if (all_cpus) {
310 CPU_FOREACH(cs) {
311 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
312 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
314 } else {
315 cs = mon_get_cpu(mon);
317 if (!cs) {
318 monitor_printf(mon, "No CPU available\n");
319 return;
322 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
326 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
328 int64_t max = qdict_get_try_int(qdict, "max", 10);
329 bool mean = qdict_get_try_bool(qdict, "mean", false);
330 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
331 enum QSPSortBy sort_by;
333 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
334 qsp_report(max, sort_by, coalesce);
337 static void hmp_info_history(Monitor *mon, const QDict *qdict)
339 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
340 int i;
341 const char *str;
343 if (!hmp_mon->rs) {
344 return;
346 i = 0;
347 for(;;) {
348 str = readline_get_history(hmp_mon->rs, i);
349 if (!str) {
350 break;
352 monitor_printf(mon, "%d: '%s'\n", i, str);
353 i++;
357 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
359 const char *name = qdict_get_try_str(qdict, "name");
360 bool has_vcpu = qdict_haskey(qdict, "vcpu");
361 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
362 TraceEventInfoList *events;
363 TraceEventInfoList *elem;
364 Error *local_err = NULL;
366 if (name == NULL) {
367 name = "*";
369 if (vcpu < 0) {
370 monitor_printf(mon, "argument vcpu must be positive");
371 return;
374 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
375 if (local_err) {
376 error_report_err(local_err);
377 return;
380 for (elem = events; elem != NULL; elem = elem->next) {
381 monitor_printf(mon, "%s : state %u\n",
382 elem->value->name,
383 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
385 qapi_free_TraceEventInfoList(events);
388 void qmp_client_migrate_info(const char *protocol, const char *hostname,
389 bool has_port, int64_t port,
390 bool has_tls_port, int64_t tls_port,
391 bool has_cert_subject, const char *cert_subject,
392 Error **errp)
394 if (strcmp(protocol, "spice") == 0) {
395 if (!qemu_using_spice(errp)) {
396 return;
399 if (!has_port && !has_tls_port) {
400 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
401 return;
404 if (qemu_spice.migrate_info(hostname,
405 has_port ? port : -1,
406 has_tls_port ? tls_port : -1,
407 cert_subject)) {
408 error_setg(errp, "Could not set up display for migration");
409 return;
411 return;
414 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
417 static void hmp_logfile(Monitor *mon, const QDict *qdict)
419 Error *err = NULL;
421 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
422 if (err) {
423 error_report_err(err);
427 static void hmp_log(Monitor *mon, const QDict *qdict)
429 int mask;
430 const char *items = qdict_get_str(qdict, "items");
432 if (!strcmp(items, "none")) {
433 mask = 0;
434 } else {
435 mask = qemu_str_to_log_mask(items);
436 if (!mask) {
437 help_cmd(mon, "log");
438 return;
441 qemu_set_log(mask);
444 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
446 const char *option = qdict_get_try_str(qdict, "option");
447 if (!option || !strcmp(option, "on")) {
448 singlestep = 1;
449 } else if (!strcmp(option, "off")) {
450 singlestep = 0;
451 } else {
452 monitor_printf(mon, "unexpected option %s\n", option);
456 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
458 const char *device = qdict_get_try_str(qdict, "device");
459 if (!device) {
460 device = "tcp::" DEFAULT_GDBSTUB_PORT;
463 if (gdbserver_start(device) < 0) {
464 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
465 device);
466 } else if (strcmp(device, "none") == 0) {
467 monitor_printf(mon, "Disabled gdbserver\n");
468 } else {
469 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
470 device);
474 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
476 Error *err = NULL;
477 WatchdogAction action;
478 char *qapi_value;
480 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
481 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
482 g_free(qapi_value);
483 if (err) {
484 hmp_handle_error(mon, err);
485 return;
487 qmp_watchdog_set_action(action, &error_abort);
490 static void monitor_printc(Monitor *mon, int c)
492 monitor_printf(mon, "'");
493 switch(c) {
494 case '\'':
495 monitor_printf(mon, "\\'");
496 break;
497 case '\\':
498 monitor_printf(mon, "\\\\");
499 break;
500 case '\n':
501 monitor_printf(mon, "\\n");
502 break;
503 case '\r':
504 monitor_printf(mon, "\\r");
505 break;
506 default:
507 if (c >= 32 && c <= 126) {
508 monitor_printf(mon, "%c", c);
509 } else {
510 monitor_printf(mon, "\\x%02x", c);
512 break;
514 monitor_printf(mon, "'");
517 static void memory_dump(Monitor *mon, int count, int format, int wsize,
518 hwaddr addr, int is_physical)
520 int l, line_size, i, max_digits, len;
521 uint8_t buf[16];
522 uint64_t v;
523 CPUState *cs = mon_get_cpu(mon);
525 if (!cs && (format == 'i' || !is_physical)) {
526 monitor_printf(mon, "Can not dump without CPU\n");
527 return;
530 if (format == 'i') {
531 monitor_disas(mon, cs, addr, count, is_physical);
532 return;
535 len = wsize * count;
536 if (wsize == 1) {
537 line_size = 8;
538 } else {
539 line_size = 16;
541 max_digits = 0;
543 switch(format) {
544 case 'o':
545 max_digits = DIV_ROUND_UP(wsize * 8, 3);
546 break;
547 default:
548 case 'x':
549 max_digits = (wsize * 8) / 4;
550 break;
551 case 'u':
552 case 'd':
553 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
554 break;
555 case 'c':
556 wsize = 1;
557 break;
560 while (len > 0) {
561 if (is_physical) {
562 monitor_printf(mon, TARGET_FMT_plx ":", addr);
563 } else {
564 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
566 l = len;
567 if (l > line_size)
568 l = line_size;
569 if (is_physical) {
570 AddressSpace *as = cs ? cs->as : &address_space_memory;
571 MemTxResult r = address_space_read(as, addr,
572 MEMTXATTRS_UNSPECIFIED, buf, l);
573 if (r != MEMTX_OK) {
574 monitor_printf(mon, " Cannot access memory\n");
575 break;
577 } else {
578 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
579 monitor_printf(mon, " Cannot access memory\n");
580 break;
583 i = 0;
584 while (i < l) {
585 switch(wsize) {
586 default:
587 case 1:
588 v = ldub_p(buf + i);
589 break;
590 case 2:
591 v = lduw_p(buf + i);
592 break;
593 case 4:
594 v = (uint32_t)ldl_p(buf + i);
595 break;
596 case 8:
597 v = ldq_p(buf + i);
598 break;
600 monitor_printf(mon, " ");
601 switch(format) {
602 case 'o':
603 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
604 break;
605 case 'x':
606 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
607 break;
608 case 'u':
609 monitor_printf(mon, "%*" PRIu64, max_digits, v);
610 break;
611 case 'd':
612 monitor_printf(mon, "%*" PRId64, max_digits, v);
613 break;
614 case 'c':
615 monitor_printc(mon, v);
616 break;
618 i += wsize;
620 monitor_printf(mon, "\n");
621 addr += l;
622 len -= l;
626 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
628 int count = qdict_get_int(qdict, "count");
629 int format = qdict_get_int(qdict, "format");
630 int size = qdict_get_int(qdict, "size");
631 target_long addr = qdict_get_int(qdict, "addr");
633 memory_dump(mon, count, format, size, addr, 0);
636 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
638 int count = qdict_get_int(qdict, "count");
639 int format = qdict_get_int(qdict, "format");
640 int size = qdict_get_int(qdict, "size");
641 hwaddr addr = qdict_get_int(qdict, "addr");
643 memory_dump(mon, count, format, size, addr, 1);
646 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
648 Int128 gpa_region_size;
649 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
650 addr, size);
652 if (!mrs.mr) {
653 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
654 return NULL;
657 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
658 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
659 memory_region_unref(mrs.mr);
660 return NULL;
663 gpa_region_size = int128_make64(size);
664 if (int128_lt(mrs.size, gpa_region_size)) {
665 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
666 " exceeded.", addr);
667 memory_region_unref(mrs.mr);
668 return NULL;
671 *p_mr = mrs.mr;
672 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
675 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
677 hwaddr addr = qdict_get_int(qdict, "addr");
678 Error *local_err = NULL;
679 MemoryRegion *mr = NULL;
680 void *ptr;
682 ptr = gpa2hva(&mr, addr, 1, &local_err);
683 if (local_err) {
684 error_report_err(local_err);
685 return;
688 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
689 " (%s) is %p\n",
690 addr, mr->name, ptr);
692 memory_region_unref(mr);
695 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
697 target_ulong addr = qdict_get_int(qdict, "addr");
698 MemTxAttrs attrs;
699 CPUState *cs = mon_get_cpu(mon);
700 hwaddr gpa;
702 if (!cs) {
703 monitor_printf(mon, "No cpu\n");
704 return;
707 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
708 if (gpa == -1) {
709 monitor_printf(mon, "Unmapped\n");
710 } else {
711 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
712 gpa + (addr & ~TARGET_PAGE_MASK));
716 #ifdef CONFIG_LINUX
717 static uint64_t vtop(void *ptr, Error **errp)
719 uint64_t pinfo;
720 uint64_t ret = -1;
721 uintptr_t addr = (uintptr_t) ptr;
722 uintptr_t pagesize = qemu_real_host_page_size;
723 off_t offset = addr / pagesize * sizeof(pinfo);
724 int fd;
726 fd = open("/proc/self/pagemap", O_RDONLY);
727 if (fd == -1) {
728 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
729 return -1;
732 /* Force copy-on-write if necessary. */
733 qatomic_add((uint8_t *)ptr, 0);
735 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
736 error_setg_errno(errp, errno, "Cannot read pagemap");
737 goto out;
739 if ((pinfo & (1ull << 63)) == 0) {
740 error_setg(errp, "Page not present");
741 goto out;
743 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
745 out:
746 close(fd);
747 return ret;
750 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
752 hwaddr addr = qdict_get_int(qdict, "addr");
753 Error *local_err = NULL;
754 MemoryRegion *mr = NULL;
755 void *ptr;
756 uint64_t physaddr;
758 ptr = gpa2hva(&mr, addr, 1, &local_err);
759 if (local_err) {
760 error_report_err(local_err);
761 return;
764 physaddr = vtop(ptr, &local_err);
765 if (local_err) {
766 error_report_err(local_err);
767 } else {
768 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
769 " (%s) is 0x%" PRIx64 "\n",
770 addr, mr->name, (uint64_t) physaddr);
773 memory_region_unref(mr);
775 #endif
777 static void do_print(Monitor *mon, const QDict *qdict)
779 int format = qdict_get_int(qdict, "format");
780 hwaddr val = qdict_get_int(qdict, "val");
782 switch(format) {
783 case 'o':
784 monitor_printf(mon, "%#" HWADDR_PRIo, val);
785 break;
786 case 'x':
787 monitor_printf(mon, "%#" HWADDR_PRIx, val);
788 break;
789 case 'u':
790 monitor_printf(mon, "%" HWADDR_PRIu, val);
791 break;
792 default:
793 case 'd':
794 monitor_printf(mon, "%" HWADDR_PRId, val);
795 break;
796 case 'c':
797 monitor_printc(mon, val);
798 break;
800 monitor_printf(mon, "\n");
803 static void hmp_sum(Monitor *mon, const QDict *qdict)
805 uint32_t addr;
806 uint16_t sum;
807 uint32_t start = qdict_get_int(qdict, "start");
808 uint32_t size = qdict_get_int(qdict, "size");
810 sum = 0;
811 for(addr = start; addr < (start + size); addr++) {
812 uint8_t val = address_space_ldub(&address_space_memory, addr,
813 MEMTXATTRS_UNSPECIFIED, NULL);
814 /* BSD sum algorithm ('sum' Unix command) */
815 sum = (sum >> 1) | (sum << 15);
816 sum += val;
818 monitor_printf(mon, "%05d\n", sum);
821 static int mouse_button_state;
823 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
825 int dx, dy, dz, button;
826 const char *dx_str = qdict_get_str(qdict, "dx_str");
827 const char *dy_str = qdict_get_str(qdict, "dy_str");
828 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
830 dx = strtol(dx_str, NULL, 0);
831 dy = strtol(dy_str, NULL, 0);
832 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
833 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
835 if (dz_str) {
836 dz = strtol(dz_str, NULL, 0);
837 if (dz != 0) {
838 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
839 qemu_input_queue_btn(NULL, button, true);
840 qemu_input_event_sync();
841 qemu_input_queue_btn(NULL, button, false);
844 qemu_input_event_sync();
847 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
849 static uint32_t bmap[INPUT_BUTTON__MAX] = {
850 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
851 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
852 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
854 int button_state = qdict_get_int(qdict, "button_state");
856 if (mouse_button_state == button_state) {
857 return;
859 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
860 qemu_input_event_sync();
861 mouse_button_state = button_state;
864 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
866 int size = qdict_get_int(qdict, "size");
867 int addr = qdict_get_int(qdict, "addr");
868 int has_index = qdict_haskey(qdict, "index");
869 uint32_t val;
870 int suffix;
872 if (has_index) {
873 int index = qdict_get_int(qdict, "index");
874 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
875 addr++;
877 addr &= 0xffff;
879 switch(size) {
880 default:
881 case 1:
882 val = cpu_inb(addr);
883 suffix = 'b';
884 break;
885 case 2:
886 val = cpu_inw(addr);
887 suffix = 'w';
888 break;
889 case 4:
890 val = cpu_inl(addr);
891 suffix = 'l';
892 break;
894 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
895 suffix, addr, size * 2, val);
898 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
900 int size = qdict_get_int(qdict, "size");
901 int addr = qdict_get_int(qdict, "addr");
902 int val = qdict_get_int(qdict, "val");
904 addr &= IOPORTS_MASK;
906 switch (size) {
907 default:
908 case 1:
909 cpu_outb(addr, val);
910 break;
911 case 2:
912 cpu_outw(addr, val);
913 break;
914 case 4:
915 cpu_outl(addr, val);
916 break;
920 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
922 Error *local_err = NULL;
923 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
925 qemu_boot_set(bootdevice, &local_err);
926 if (local_err) {
927 error_report_err(local_err);
928 } else {
929 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
933 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
935 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
936 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
937 bool owner = qdict_get_try_bool(qdict, "owner", false);
938 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
940 mtree_info(flatview, dispatch_tree, owner, disabled);
943 /* Capture support */
944 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
946 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
948 int i;
949 CaptureState *s;
951 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
952 monitor_printf(mon, "[%d]: ", i);
953 s->ops.info (s->opaque);
957 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
959 int i;
960 int n = qdict_get_int(qdict, "n");
961 CaptureState *s;
963 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
964 if (i == n) {
965 s->ops.destroy (s->opaque);
966 QLIST_REMOVE (s, entries);
967 g_free (s);
968 return;
973 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
975 const char *path = qdict_get_str(qdict, "path");
976 int freq = qdict_get_try_int(qdict, "freq", 44100);
977 int bits = qdict_get_try_int(qdict, "bits", 16);
978 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
979 const char *audiodev = qdict_get_str(qdict, "audiodev");
980 CaptureState *s;
981 AudioState *as = audio_state_by_name(audiodev);
983 if (!as) {
984 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
985 return;
988 s = g_malloc0 (sizeof (*s));
990 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
991 monitor_printf(mon, "Failed to add wave capture\n");
992 g_free (s);
993 return;
995 QLIST_INSERT_HEAD (&capture_head, s, entries);
998 void qmp_getfd(const char *fdname, Error **errp)
1000 Monitor *cur_mon = monitor_cur();
1001 mon_fd_t *monfd;
1002 int fd, tmp_fd;
1004 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1005 if (fd == -1) {
1006 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1007 return;
1010 if (qemu_isdigit(fdname[0])) {
1011 close(fd);
1012 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1013 "a name not starting with a digit");
1014 return;
1017 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1018 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1019 if (strcmp(monfd->name, fdname) != 0) {
1020 continue;
1023 tmp_fd = monfd->fd;
1024 monfd->fd = fd;
1025 /* Make sure close() is outside critical section */
1026 close(tmp_fd);
1027 return;
1030 monfd = g_malloc0(sizeof(mon_fd_t));
1031 monfd->name = g_strdup(fdname);
1032 monfd->fd = fd;
1034 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1037 void qmp_closefd(const char *fdname, Error **errp)
1039 Monitor *cur_mon = monitor_cur();
1040 mon_fd_t *monfd;
1041 int tmp_fd;
1043 qemu_mutex_lock(&cur_mon->mon_lock);
1044 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1045 if (strcmp(monfd->name, fdname) != 0) {
1046 continue;
1049 QLIST_REMOVE(monfd, next);
1050 tmp_fd = monfd->fd;
1051 g_free(monfd->name);
1052 g_free(monfd);
1053 qemu_mutex_unlock(&cur_mon->mon_lock);
1054 /* Make sure close() is outside critical section */
1055 close(tmp_fd);
1056 return;
1059 qemu_mutex_unlock(&cur_mon->mon_lock);
1060 error_setg(errp, "File descriptor named '%s' not found", fdname);
1063 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1065 mon_fd_t *monfd;
1067 QEMU_LOCK_GUARD(&mon->mon_lock);
1068 QLIST_FOREACH(monfd, &mon->fds, next) {
1069 int fd;
1071 if (strcmp(monfd->name, fdname) != 0) {
1072 continue;
1075 fd = monfd->fd;
1077 /* caller takes ownership of fd */
1078 QLIST_REMOVE(monfd, next);
1079 g_free(monfd->name);
1080 g_free(monfd);
1082 return fd;
1085 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1086 return -1;
1089 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1091 MonFdsetFd *mon_fdset_fd;
1092 MonFdsetFd *mon_fdset_fd_next;
1094 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1095 if ((mon_fdset_fd->removed ||
1096 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1097 runstate_is_running()) {
1098 close(mon_fdset_fd->fd);
1099 g_free(mon_fdset_fd->opaque);
1100 QLIST_REMOVE(mon_fdset_fd, next);
1101 g_free(mon_fdset_fd);
1105 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1106 QLIST_REMOVE(mon_fdset, next);
1107 g_free(mon_fdset);
1111 void monitor_fdsets_cleanup(void)
1113 MonFdset *mon_fdset;
1114 MonFdset *mon_fdset_next;
1116 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1117 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1118 monitor_fdset_cleanup(mon_fdset);
1122 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1123 const char *opaque, Error **errp)
1125 int fd;
1126 Monitor *mon = monitor_cur();
1127 AddfdInfo *fdinfo;
1129 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1130 if (fd == -1) {
1131 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1132 goto error;
1135 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1136 has_opaque, opaque, errp);
1137 if (fdinfo) {
1138 return fdinfo;
1141 error:
1142 if (fd != -1) {
1143 close(fd);
1145 return NULL;
1148 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1150 MonFdset *mon_fdset;
1151 MonFdsetFd *mon_fdset_fd;
1152 char fd_str[60];
1154 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1155 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1156 if (mon_fdset->id != fdset_id) {
1157 continue;
1159 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1160 if (has_fd) {
1161 if (mon_fdset_fd->fd != fd) {
1162 continue;
1164 mon_fdset_fd->removed = true;
1165 break;
1166 } else {
1167 mon_fdset_fd->removed = true;
1170 if (has_fd && !mon_fdset_fd) {
1171 goto error;
1173 monitor_fdset_cleanup(mon_fdset);
1174 return;
1177 error:
1178 if (has_fd) {
1179 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1180 fdset_id, fd);
1181 } else {
1182 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1184 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1187 FdsetInfoList *qmp_query_fdsets(Error **errp)
1189 MonFdset *mon_fdset;
1190 MonFdsetFd *mon_fdset_fd;
1191 FdsetInfoList *fdset_list = NULL;
1193 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1194 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1195 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1197 fdset_info->fdset_id = mon_fdset->id;
1199 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1200 FdsetFdInfo *fdsetfd_info;
1202 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1203 fdsetfd_info->fd = mon_fdset_fd->fd;
1204 if (mon_fdset_fd->opaque) {
1205 fdsetfd_info->has_opaque = true;
1206 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1207 } else {
1208 fdsetfd_info->has_opaque = false;
1211 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1214 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1217 return fdset_list;
1220 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1221 bool has_opaque, const char *opaque,
1222 Error **errp)
1224 MonFdset *mon_fdset = NULL;
1225 MonFdsetFd *mon_fdset_fd;
1226 AddfdInfo *fdinfo;
1228 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1229 if (has_fdset_id) {
1230 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1231 /* Break if match found or match impossible due to ordering by ID */
1232 if (fdset_id <= mon_fdset->id) {
1233 if (fdset_id < mon_fdset->id) {
1234 mon_fdset = NULL;
1236 break;
1241 if (mon_fdset == NULL) {
1242 int64_t fdset_id_prev = -1;
1243 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1245 if (has_fdset_id) {
1246 if (fdset_id < 0) {
1247 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1248 "a non-negative value");
1249 return NULL;
1251 /* Use specified fdset ID */
1252 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1253 mon_fdset_cur = mon_fdset;
1254 if (fdset_id < mon_fdset_cur->id) {
1255 break;
1258 } else {
1259 /* Use first available fdset ID */
1260 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1261 mon_fdset_cur = mon_fdset;
1262 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1263 fdset_id_prev = mon_fdset_cur->id;
1264 continue;
1266 break;
1270 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1271 if (has_fdset_id) {
1272 mon_fdset->id = fdset_id;
1273 } else {
1274 mon_fdset->id = fdset_id_prev + 1;
1277 /* The fdset list is ordered by fdset ID */
1278 if (!mon_fdset_cur) {
1279 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1280 } else if (mon_fdset->id < mon_fdset_cur->id) {
1281 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1282 } else {
1283 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1287 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1288 mon_fdset_fd->fd = fd;
1289 mon_fdset_fd->removed = false;
1290 if (has_opaque) {
1291 mon_fdset_fd->opaque = g_strdup(opaque);
1293 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1295 fdinfo = g_malloc0(sizeof(*fdinfo));
1296 fdinfo->fdset_id = mon_fdset->id;
1297 fdinfo->fd = mon_fdset_fd->fd;
1299 return fdinfo;
1302 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1304 #ifdef _WIN32
1305 return -ENOENT;
1306 #else
1307 MonFdset *mon_fdset;
1309 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1310 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1311 MonFdsetFd *mon_fdset_fd;
1312 MonFdsetFd *mon_fdset_fd_dup;
1313 int fd = -1;
1314 int dup_fd;
1315 int mon_fd_flags;
1317 if (mon_fdset->id != fdset_id) {
1318 continue;
1321 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1322 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1323 if (mon_fd_flags == -1) {
1324 return -1;
1327 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1328 fd = mon_fdset_fd->fd;
1329 break;
1333 if (fd == -1) {
1334 errno = EACCES;
1335 return -1;
1338 dup_fd = qemu_dup_flags(fd, flags);
1339 if (dup_fd == -1) {
1340 return -1;
1343 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1344 mon_fdset_fd_dup->fd = dup_fd;
1345 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1346 return dup_fd;
1349 errno = ENOENT;
1350 return -1;
1351 #endif
1354 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1356 MonFdset *mon_fdset;
1357 MonFdsetFd *mon_fdset_fd_dup;
1359 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1360 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1361 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1362 if (mon_fdset_fd_dup->fd == dup_fd) {
1363 if (remove) {
1364 QLIST_REMOVE(mon_fdset_fd_dup, next);
1365 g_free(mon_fdset_fd_dup);
1366 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1367 monitor_fdset_cleanup(mon_fdset);
1369 return -1;
1370 } else {
1371 return mon_fdset->id;
1377 return -1;
1380 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1382 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1385 void monitor_fdset_dup_fd_remove(int dup_fd)
1387 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1390 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1392 int fd;
1393 Error *local_err = NULL;
1395 if (!qemu_isdigit(fdname[0]) && mon) {
1396 fd = monitor_get_fd(mon, fdname, &local_err);
1397 } else {
1398 fd = qemu_parse_fd(fdname);
1399 if (fd == -1) {
1400 error_setg(&local_err, "Invalid file descriptor number '%s'",
1401 fdname);
1404 if (local_err) {
1405 error_propagate(errp, local_err);
1406 assert(fd == -1);
1407 } else {
1408 assert(fd != -1);
1411 return fd;
1414 /* Please update hmp-commands.hx when adding or changing commands */
1415 static HMPCommand hmp_info_cmds[] = {
1416 #include "hmp-commands-info.h"
1417 { NULL, NULL, },
1420 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1421 HMPCommand hmp_cmds[] = {
1422 #include "hmp-commands.h"
1423 { NULL, NULL, },
1427 * Set @pval to the value in the register identified by @name.
1428 * return 0 if OK, -1 if not found
1430 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1432 const MonitorDef *md = target_monitor_defs();
1433 CPUState *cs = mon_get_cpu(mon);
1434 void *ptr;
1435 uint64_t tmp = 0;
1436 int ret;
1438 if (cs == NULL || md == NULL) {
1439 return -1;
1442 for(; md->name != NULL; md++) {
1443 if (hmp_compare_cmd(name, md->name)) {
1444 if (md->get_value) {
1445 *pval = md->get_value(mon, md, md->offset);
1446 } else {
1447 CPUArchState *env = mon_get_cpu_env(mon);
1448 ptr = (uint8_t *)env + md->offset;
1449 switch(md->type) {
1450 case MD_I32:
1451 *pval = *(int32_t *)ptr;
1452 break;
1453 case MD_TLONG:
1454 *pval = *(target_long *)ptr;
1455 break;
1456 default:
1457 *pval = 0;
1458 break;
1461 return 0;
1465 ret = target_get_monitor_def(cs, name, &tmp);
1466 if (!ret) {
1467 *pval = (target_long) tmp;
1470 return ret;
1473 static void add_completion_option(ReadLineState *rs, const char *str,
1474 const char *option)
1476 if (!str || !option) {
1477 return;
1479 if (!strncmp(option, str, strlen(str))) {
1480 readline_add_completion(rs, option);
1484 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1486 size_t len;
1487 ChardevBackendInfoList *list, *start;
1489 if (nb_args != 2) {
1490 return;
1492 len = strlen(str);
1493 readline_set_completion_index(rs, len);
1495 start = list = qmp_query_chardev_backends(NULL);
1496 while (list) {
1497 const char *chr_name = list->value->name;
1499 if (!strncmp(chr_name, str, len)) {
1500 readline_add_completion(rs, chr_name);
1502 list = list->next;
1504 qapi_free_ChardevBackendInfoList(start);
1507 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1509 size_t len;
1510 int i;
1512 if (nb_args != 2) {
1513 return;
1515 len = strlen(str);
1516 readline_set_completion_index(rs, len);
1517 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1518 add_completion_option(rs, str, NetClientDriver_str(i));
1522 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1524 GSList *list, *elt;
1525 size_t len;
1527 if (nb_args != 2) {
1528 return;
1531 len = strlen(str);
1532 readline_set_completion_index(rs, len);
1533 list = elt = object_class_get_list(TYPE_DEVICE, false);
1534 while (elt) {
1535 const char *name;
1536 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1537 TYPE_DEVICE);
1538 name = object_class_get_name(OBJECT_CLASS(dc));
1540 if (dc->user_creatable
1541 && !strncmp(name, str, len)) {
1542 readline_add_completion(rs, name);
1544 elt = elt->next;
1546 g_slist_free(list);
1549 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1551 GSList *list, *elt;
1552 size_t len;
1554 if (nb_args != 2) {
1555 return;
1558 len = strlen(str);
1559 readline_set_completion_index(rs, len);
1560 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1561 while (elt) {
1562 const char *name;
1564 name = object_class_get_name(OBJECT_CLASS(elt->data));
1565 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1566 readline_add_completion(rs, name);
1568 elt = elt->next;
1570 g_slist_free(list);
1573 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1575 GSList **list = opaque;
1576 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1578 if (dev == NULL) {
1579 return 0;
1582 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1583 *list = g_slist_append(*list, dev);
1586 return 0;
1589 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1591 GSList *list = NULL;
1593 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1595 return list;
1598 static void peripheral_device_del_completion(ReadLineState *rs,
1599 const char *str, size_t len)
1601 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1602 GSList *list, *item;
1604 list = qdev_build_hotpluggable_device_list(peripheral);
1605 if (!list) {
1606 return;
1609 for (item = list; item; item = g_slist_next(item)) {
1610 DeviceState *dev = item->data;
1612 if (dev->id && !strncmp(str, dev->id, len)) {
1613 readline_add_completion(rs, dev->id);
1617 g_slist_free(list);
1620 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1622 size_t len;
1623 ChardevInfoList *list, *start;
1625 if (nb_args != 2) {
1626 return;
1628 len = strlen(str);
1629 readline_set_completion_index(rs, len);
1631 start = list = qmp_query_chardev(NULL);
1632 while (list) {
1633 ChardevInfo *chr = list->value;
1635 if (!strncmp(chr->label, str, len)) {
1636 readline_add_completion(rs, chr->label);
1638 list = list->next;
1640 qapi_free_ChardevInfoList(start);
1643 static void ringbuf_completion(ReadLineState *rs, const char *str)
1645 size_t len;
1646 ChardevInfoList *list, *start;
1648 len = strlen(str);
1649 readline_set_completion_index(rs, len);
1651 start = list = qmp_query_chardev(NULL);
1652 while (list) {
1653 ChardevInfo *chr_info = list->value;
1655 if (!strncmp(chr_info->label, str, len)) {
1656 Chardev *chr = qemu_chr_find(chr_info->label);
1657 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1658 readline_add_completion(rs, chr_info->label);
1661 list = list->next;
1663 qapi_free_ChardevInfoList(start);
1666 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1668 if (nb_args != 2) {
1669 return;
1671 ringbuf_completion(rs, str);
1674 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1676 size_t len;
1678 if (nb_args != 2) {
1679 return;
1682 len = strlen(str);
1683 readline_set_completion_index(rs, len);
1684 peripheral_device_del_completion(rs, str, len);
1687 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1689 ObjectPropertyInfoList *list, *start;
1690 size_t len;
1692 if (nb_args != 2) {
1693 return;
1695 len = strlen(str);
1696 readline_set_completion_index(rs, len);
1698 start = list = qmp_qom_list("/objects", NULL);
1699 while (list) {
1700 ObjectPropertyInfo *info = list->value;
1702 if (!strncmp(info->type, "child<", 5)
1703 && !strncmp(info->name, str, len)) {
1704 readline_add_completion(rs, info->name);
1706 list = list->next;
1708 qapi_free_ObjectPropertyInfoList(start);
1711 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1713 int i;
1714 char *sep;
1715 size_t len;
1717 if (nb_args != 2) {
1718 return;
1720 sep = strrchr(str, '-');
1721 if (sep) {
1722 str = sep + 1;
1724 len = strlen(str);
1725 readline_set_completion_index(rs, len);
1726 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1727 if (!strncmp(str, QKeyCode_str(i), len)) {
1728 readline_add_completion(rs, QKeyCode_str(i));
1733 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1735 size_t len;
1737 len = strlen(str);
1738 readline_set_completion_index(rs, len);
1739 if (nb_args == 2) {
1740 NetClientState *ncs[MAX_QUEUE_NUM];
1741 int count, i;
1742 count = qemu_find_net_clients_except(NULL, ncs,
1743 NET_CLIENT_DRIVER_NONE,
1744 MAX_QUEUE_NUM);
1745 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1746 const char *name = ncs[i]->name;
1747 if (!strncmp(str, name, len)) {
1748 readline_add_completion(rs, name);
1751 } else if (nb_args == 3) {
1752 add_completion_option(rs, str, "on");
1753 add_completion_option(rs, str, "off");
1757 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1759 int len, count, i;
1760 NetClientState *ncs[MAX_QUEUE_NUM];
1762 if (nb_args != 2) {
1763 return;
1766 len = strlen(str);
1767 readline_set_completion_index(rs, len);
1768 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1769 MAX_QUEUE_NUM);
1770 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1771 const char *name = ncs[i]->name;
1772 if (strncmp(str, name, len)) {
1773 continue;
1775 if (ncs[i]->is_netdev) {
1776 readline_add_completion(rs, name);
1781 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1783 size_t len;
1785 len = strlen(str);
1786 readline_set_completion_index(rs, len);
1787 if (nb_args == 2) {
1788 TraceEventIter iter;
1789 TraceEvent *ev;
1790 char *pattern = g_strdup_printf("%s*", str);
1791 trace_event_iter_init_pattern(&iter, pattern);
1792 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1793 readline_add_completion(rs, trace_event_get_name(ev));
1795 g_free(pattern);
1799 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1801 size_t len;
1803 len = strlen(str);
1804 readline_set_completion_index(rs, len);
1805 if (nb_args == 2) {
1806 TraceEventIter iter;
1807 TraceEvent *ev;
1808 char *pattern = g_strdup_printf("%s*", str);
1809 trace_event_iter_init_pattern(&iter, pattern);
1810 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1811 readline_add_completion(rs, trace_event_get_name(ev));
1813 g_free(pattern);
1814 } else if (nb_args == 3) {
1815 add_completion_option(rs, str, "on");
1816 add_completion_option(rs, str, "off");
1820 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1822 int i;
1824 if (nb_args != 2) {
1825 return;
1827 readline_set_completion_index(rs, strlen(str));
1828 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1829 add_completion_option(rs, str, WatchdogAction_str(i));
1833 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1834 const char *str)
1836 size_t len;
1838 len = strlen(str);
1839 readline_set_completion_index(rs, len);
1840 if (nb_args == 2) {
1841 int i;
1842 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1843 const char *name = MigrationCapability_str(i);
1844 if (!strncmp(str, name, len)) {
1845 readline_add_completion(rs, name);
1848 } else if (nb_args == 3) {
1849 add_completion_option(rs, str, "on");
1850 add_completion_option(rs, str, "off");
1854 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1855 const char *str)
1857 size_t len;
1859 len = strlen(str);
1860 readline_set_completion_index(rs, len);
1861 if (nb_args == 2) {
1862 int i;
1863 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1864 const char *name = MigrationParameter_str(i);
1865 if (!strncmp(str, name, len)) {
1866 readline_add_completion(rs, name);
1872 static void vm_completion(ReadLineState *rs, const char *str)
1874 size_t len;
1875 BlockDriverState *bs;
1876 BdrvNextIterator it;
1878 len = strlen(str);
1879 readline_set_completion_index(rs, len);
1881 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1882 SnapshotInfoList *snapshots, *snapshot;
1883 AioContext *ctx = bdrv_get_aio_context(bs);
1884 bool ok = false;
1886 aio_context_acquire(ctx);
1887 if (bdrv_can_snapshot(bs)) {
1888 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1890 aio_context_release(ctx);
1891 if (!ok) {
1892 continue;
1895 snapshot = snapshots;
1896 while (snapshot) {
1897 char *completion = snapshot->value->name;
1898 if (!strncmp(str, completion, len)) {
1899 readline_add_completion(rs, completion);
1901 completion = snapshot->value->id;
1902 if (!strncmp(str, completion, len)) {
1903 readline_add_completion(rs, completion);
1905 snapshot = snapshot->next;
1907 qapi_free_SnapshotInfoList(snapshots);
1912 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1914 if (nb_args == 2) {
1915 vm_completion(rs, str);
1919 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1921 if (nb_args == 2) {
1922 vm_completion(rs, str);
1926 static int
1927 compare_mon_cmd(const void *a, const void *b)
1929 return strcmp(((const HMPCommand *)a)->name,
1930 ((const HMPCommand *)b)->name);
1933 static void sortcmdlist(void)
1935 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1936 sizeof(*hmp_cmds),
1937 compare_mon_cmd);
1938 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1939 sizeof(*hmp_info_cmds),
1940 compare_mon_cmd);
1943 void monitor_register_hmp(const char *name, bool info,
1944 void (*cmd)(Monitor *mon, const QDict *qdict))
1946 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1948 while (table->name != NULL) {
1949 if (strcmp(table->name, name) == 0) {
1950 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1951 table->cmd = cmd;
1952 return;
1954 table++;
1956 g_assert_not_reached();
1959 void monitor_register_hmp_info_hrt(const char *name,
1960 HumanReadableText *(*handler)(Error **errp))
1962 HMPCommand *table = hmp_info_cmds;
1964 while (table->name != NULL) {
1965 if (strcmp(table->name, name) == 0) {
1966 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1967 table->cmd_info_hrt = handler;
1968 return;
1970 table++;
1972 g_assert_not_reached();
1975 void monitor_init_globals(void)
1977 monitor_init_globals_core();
1978 monitor_init_qmp_commands();
1979 sortcmdlist();
1980 qemu_mutex_init(&mon_fdsets_lock);