tests/tcg: extend SSE tests to AVX
[qemu.git] / monitor / misc.c
blob6436a8786b442223254c27da9cbb0913729bea79
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "hw/usb.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
39 #include "ui/input.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
44 #include "qemu/log.h"
45 #include "sysemu/hw_accel.h"
46 #include "sysemu/runstate.h"
47 #include "authz/list.h"
48 #include "qapi/util.h"
49 #include "sysemu/blockdev.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/tpm.h"
52 #include "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 /* Make devices configuration available for use in hmp-commands*.hx templates */
88 #include CONFIG_DEVICES
90 /* file descriptors passed via SCM_RIGHTS */
91 typedef struct mon_fd_t mon_fd_t;
92 struct mon_fd_t {
93 char *name;
94 int fd;
95 QLIST_ENTRY(mon_fd_t) next;
98 /* file descriptor associated with a file descriptor set */
99 typedef struct MonFdsetFd MonFdsetFd;
100 struct MonFdsetFd {
101 int fd;
102 bool removed;
103 char *opaque;
104 QLIST_ENTRY(MonFdsetFd) next;
107 /* file descriptor set containing fds passed via SCM_RIGHTS */
108 typedef struct MonFdset MonFdset;
109 struct MonFdset {
110 int64_t id;
111 QLIST_HEAD(, MonFdsetFd) fds;
112 QLIST_HEAD(, MonFdsetFd) dup_fds;
113 QLIST_ENTRY(MonFdset) next;
116 /* Protects mon_fdsets */
117 static QemuMutex mon_fdsets_lock;
118 static QLIST_HEAD(, MonFdset) mon_fdsets;
120 static HMPCommand hmp_info_cmds[];
122 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
123 int64_t cpu_index, Error **errp)
125 char *output = NULL;
126 MonitorHMP hmp = {};
128 monitor_data_init(&hmp.common, false, true, false);
130 if (has_cpu_index) {
131 int ret = monitor_set_cpu(&hmp.common, cpu_index);
132 if (ret < 0) {
133 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
134 "a CPU number");
135 goto out;
139 handle_hmp_command(&hmp, command_line);
141 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
142 output = g_strdup(hmp.common.outbuf->str);
145 out:
146 monitor_data_destroy(&hmp.common);
147 return output;
151 * Is @name in the '|' separated list of names @list?
153 int hmp_compare_cmd(const char *name, const char *list)
155 const char *p, *pstart;
156 int len;
157 len = strlen(name);
158 p = list;
159 for (;;) {
160 pstart = p;
161 p = qemu_strchrnul(p, '|');
162 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
163 return 1;
165 if (*p == '\0') {
166 break;
168 p++;
170 return 0;
173 static void do_help_cmd(Monitor *mon, const QDict *qdict)
175 help_cmd(mon, qdict_get_try_str(qdict, "name"));
178 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
180 const char *tp_name = qdict_get_str(qdict, "name");
181 bool new_state = qdict_get_bool(qdict, "option");
182 bool has_vcpu = qdict_haskey(qdict, "vcpu");
183 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
184 Error *local_err = NULL;
186 if (vcpu < 0) {
187 monitor_printf(mon, "argument vcpu must be positive");
188 return;
191 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
192 if (local_err) {
193 error_report_err(local_err);
197 #ifdef CONFIG_TRACE_SIMPLE
198 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
200 const char *op = qdict_get_try_str(qdict, "op");
201 const char *arg = qdict_get_try_str(qdict, "arg");
203 if (!op) {
204 st_print_trace_file_status();
205 } else if (!strcmp(op, "on")) {
206 st_set_trace_file_enabled(true);
207 } else if (!strcmp(op, "off")) {
208 st_set_trace_file_enabled(false);
209 } else if (!strcmp(op, "flush")) {
210 st_flush_trace_buffer();
211 } else if (!strcmp(op, "set")) {
212 if (arg) {
213 st_set_trace_file(arg);
215 } else {
216 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
217 help_cmd(mon, "trace-file");
220 #endif
222 static void hmp_info_help(Monitor *mon, const QDict *qdict)
224 help_cmd(mon, "info");
227 static void monitor_init_qmp_commands(void)
230 * Two command lists:
231 * - qmp_commands contains all QMP commands
232 * - qmp_cap_negotiation_commands contains just
233 * "qmp_capabilities", to enforce capability negotiation
236 qmp_init_marshal(&qmp_commands);
238 qmp_register_command(&qmp_commands, "device_add",
239 qmp_device_add, 0, 0);
241 QTAILQ_INIT(&qmp_cap_negotiation_commands);
242 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
243 qmp_marshal_qmp_capabilities,
244 QCO_ALLOW_PRECONFIG, 0);
247 /* Set the current CPU defined by the user. Callers must hold BQL. */
248 int monitor_set_cpu(Monitor *mon, int cpu_index)
250 CPUState *cpu;
252 cpu = qemu_get_cpu(cpu_index);
253 if (cpu == NULL) {
254 return -1;
256 g_free(mon->mon_cpu_path);
257 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
258 return 0;
261 /* Callers must hold BQL. */
262 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
264 CPUState *cpu = NULL;
266 if (mon->mon_cpu_path) {
267 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
268 TYPE_CPU, NULL);
269 if (!cpu) {
270 g_free(mon->mon_cpu_path);
271 mon->mon_cpu_path = NULL;
274 if (!mon->mon_cpu_path) {
275 if (!first_cpu) {
276 return NULL;
278 monitor_set_cpu(mon, first_cpu->cpu_index);
279 cpu = first_cpu;
281 assert(cpu != NULL);
282 if (synchronize) {
283 cpu_synchronize_state(cpu);
285 return cpu;
288 CPUState *mon_get_cpu(Monitor *mon)
290 return mon_get_cpu_sync(mon, true);
293 CPUArchState *mon_get_cpu_env(Monitor *mon)
295 CPUState *cs = mon_get_cpu(mon);
297 return cs ? cs->env_ptr : NULL;
300 int monitor_get_cpu_index(Monitor *mon)
302 CPUState *cs = mon_get_cpu_sync(mon, false);
304 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
307 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
309 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
310 int vcpu = qdict_get_try_int(qdict, "vcpu", -1);
311 CPUState *cs;
313 if (all_cpus) {
314 CPU_FOREACH(cs) {
315 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
316 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
318 } else {
319 cs = vcpu >= 0 ? qemu_get_cpu(vcpu) : mon_get_cpu(mon);
321 if (!cs) {
322 if (vcpu >= 0) {
323 monitor_printf(mon, "CPU#%d not available\n", vcpu);
324 } else {
325 monitor_printf(mon, "No CPU available\n");
327 return;
330 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
331 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
335 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
337 int64_t max = qdict_get_try_int(qdict, "max", 10);
338 bool mean = qdict_get_try_bool(qdict, "mean", false);
339 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
340 enum QSPSortBy sort_by;
342 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
343 qsp_report(max, sort_by, coalesce);
346 static void hmp_info_history(Monitor *mon, const QDict *qdict)
348 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
349 int i;
350 const char *str;
352 if (!hmp_mon->rs) {
353 return;
355 i = 0;
356 for(;;) {
357 str = readline_get_history(hmp_mon->rs, i);
358 if (!str) {
359 break;
361 monitor_printf(mon, "%d: '%s'\n", i, str);
362 i++;
366 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
368 const char *name = qdict_get_try_str(qdict, "name");
369 bool has_vcpu = qdict_haskey(qdict, "vcpu");
370 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
371 TraceEventInfoList *events;
372 TraceEventInfoList *elem;
373 Error *local_err = NULL;
375 if (name == NULL) {
376 name = "*";
378 if (vcpu < 0) {
379 monitor_printf(mon, "argument vcpu must be positive");
380 return;
383 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
384 if (local_err) {
385 error_report_err(local_err);
386 return;
389 for (elem = events; elem != NULL; elem = elem->next) {
390 monitor_printf(mon, "%s : state %u\n",
391 elem->value->name,
392 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
394 qapi_free_TraceEventInfoList(events);
397 void qmp_client_migrate_info(const char *protocol, const char *hostname,
398 bool has_port, int64_t port,
399 bool has_tls_port, int64_t tls_port,
400 bool has_cert_subject, const char *cert_subject,
401 Error **errp)
403 if (strcmp(protocol, "spice") == 0) {
404 if (!qemu_using_spice(errp)) {
405 return;
408 if (!has_port && !has_tls_port) {
409 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
410 return;
413 if (qemu_spice.migrate_info(hostname,
414 has_port ? port : -1,
415 has_tls_port ? tls_port : -1,
416 cert_subject)) {
417 error_setg(errp, "Could not set up display for migration");
418 return;
420 return;
423 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
426 static void hmp_logfile(Monitor *mon, const QDict *qdict)
428 Error *err = NULL;
430 if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
431 error_report_err(err);
435 static void hmp_log(Monitor *mon, const QDict *qdict)
437 int mask;
438 const char *items = qdict_get_str(qdict, "items");
439 Error *err = NULL;
441 if (!strcmp(items, "none")) {
442 mask = 0;
443 } else {
444 mask = qemu_str_to_log_mask(items);
445 if (!mask) {
446 help_cmd(mon, "log");
447 return;
451 if (!qemu_set_log(mask, &err)) {
452 error_report_err(err);
456 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
458 const char *option = qdict_get_try_str(qdict, "option");
459 if (!option || !strcmp(option, "on")) {
460 singlestep = 1;
461 } else if (!strcmp(option, "off")) {
462 singlestep = 0;
463 } else {
464 monitor_printf(mon, "unexpected option %s\n", option);
468 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
470 const char *device = qdict_get_try_str(qdict, "device");
471 if (!device) {
472 device = "tcp::" DEFAULT_GDBSTUB_PORT;
475 if (gdbserver_start(device) < 0) {
476 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
477 device);
478 } else if (strcmp(device, "none") == 0) {
479 monitor_printf(mon, "Disabled gdbserver\n");
480 } else {
481 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
482 device);
486 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
488 Error *err = NULL;
489 WatchdogAction action;
490 char *qapi_value;
492 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
493 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
494 g_free(qapi_value);
495 if (err) {
496 hmp_handle_error(mon, err);
497 return;
499 qmp_watchdog_set_action(action, &error_abort);
502 static void monitor_printc(Monitor *mon, int c)
504 monitor_printf(mon, "'");
505 switch(c) {
506 case '\'':
507 monitor_printf(mon, "\\'");
508 break;
509 case '\\':
510 monitor_printf(mon, "\\\\");
511 break;
512 case '\n':
513 monitor_printf(mon, "\\n");
514 break;
515 case '\r':
516 monitor_printf(mon, "\\r");
517 break;
518 default:
519 if (c >= 32 && c <= 126) {
520 monitor_printf(mon, "%c", c);
521 } else {
522 monitor_printf(mon, "\\x%02x", c);
524 break;
526 monitor_printf(mon, "'");
529 static void memory_dump(Monitor *mon, int count, int format, int wsize,
530 hwaddr addr, int is_physical)
532 int l, line_size, i, max_digits, len;
533 uint8_t buf[16];
534 uint64_t v;
535 CPUState *cs = mon_get_cpu(mon);
537 if (!cs && (format == 'i' || !is_physical)) {
538 monitor_printf(mon, "Can not dump without CPU\n");
539 return;
542 if (format == 'i') {
543 monitor_disas(mon, cs, addr, count, is_physical);
544 return;
547 len = wsize * count;
548 if (wsize == 1) {
549 line_size = 8;
550 } else {
551 line_size = 16;
553 max_digits = 0;
555 switch(format) {
556 case 'o':
557 max_digits = DIV_ROUND_UP(wsize * 8, 3);
558 break;
559 default:
560 case 'x':
561 max_digits = (wsize * 8) / 4;
562 break;
563 case 'u':
564 case 'd':
565 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
566 break;
567 case 'c':
568 wsize = 1;
569 break;
572 while (len > 0) {
573 if (is_physical) {
574 monitor_printf(mon, TARGET_FMT_plx ":", addr);
575 } else {
576 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
578 l = len;
579 if (l > line_size)
580 l = line_size;
581 if (is_physical) {
582 AddressSpace *as = cs ? cs->as : &address_space_memory;
583 MemTxResult r = address_space_read(as, addr,
584 MEMTXATTRS_UNSPECIFIED, buf, l);
585 if (r != MEMTX_OK) {
586 monitor_printf(mon, " Cannot access memory\n");
587 break;
589 } else {
590 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
591 monitor_printf(mon, " Cannot access memory\n");
592 break;
595 i = 0;
596 while (i < l) {
597 switch(wsize) {
598 default:
599 case 1:
600 v = ldub_p(buf + i);
601 break;
602 case 2:
603 v = lduw_p(buf + i);
604 break;
605 case 4:
606 v = (uint32_t)ldl_p(buf + i);
607 break;
608 case 8:
609 v = ldq_p(buf + i);
610 break;
612 monitor_printf(mon, " ");
613 switch(format) {
614 case 'o':
615 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
616 break;
617 case 'x':
618 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
619 break;
620 case 'u':
621 monitor_printf(mon, "%*" PRIu64, max_digits, v);
622 break;
623 case 'd':
624 monitor_printf(mon, "%*" PRId64, max_digits, v);
625 break;
626 case 'c':
627 monitor_printc(mon, v);
628 break;
630 i += wsize;
632 monitor_printf(mon, "\n");
633 addr += l;
634 len -= l;
638 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
640 int count = qdict_get_int(qdict, "count");
641 int format = qdict_get_int(qdict, "format");
642 int size = qdict_get_int(qdict, "size");
643 target_long addr = qdict_get_int(qdict, "addr");
645 memory_dump(mon, count, format, size, addr, 0);
648 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
650 int count = qdict_get_int(qdict, "count");
651 int format = qdict_get_int(qdict, "format");
652 int size = qdict_get_int(qdict, "size");
653 hwaddr addr = qdict_get_int(qdict, "addr");
655 memory_dump(mon, count, format, size, addr, 1);
658 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
660 Int128 gpa_region_size;
661 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
662 addr, size);
664 if (!mrs.mr) {
665 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
666 return NULL;
669 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
670 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
671 memory_region_unref(mrs.mr);
672 return NULL;
675 gpa_region_size = int128_make64(size);
676 if (int128_lt(mrs.size, gpa_region_size)) {
677 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
678 " exceeded.", addr);
679 memory_region_unref(mrs.mr);
680 return NULL;
683 *p_mr = mrs.mr;
684 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
687 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
689 hwaddr addr = qdict_get_int(qdict, "addr");
690 Error *local_err = NULL;
691 MemoryRegion *mr = NULL;
692 void *ptr;
694 ptr = gpa2hva(&mr, addr, 1, &local_err);
695 if (local_err) {
696 error_report_err(local_err);
697 return;
700 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
701 " (%s) is %p\n",
702 addr, mr->name, ptr);
704 memory_region_unref(mr);
707 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
709 target_ulong addr = qdict_get_int(qdict, "addr");
710 MemTxAttrs attrs;
711 CPUState *cs = mon_get_cpu(mon);
712 hwaddr gpa;
714 if (!cs) {
715 monitor_printf(mon, "No cpu\n");
716 return;
719 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
720 if (gpa == -1) {
721 monitor_printf(mon, "Unmapped\n");
722 } else {
723 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
724 gpa + (addr & ~TARGET_PAGE_MASK));
728 #ifdef CONFIG_LINUX
729 static uint64_t vtop(void *ptr, Error **errp)
731 uint64_t pinfo;
732 uint64_t ret = -1;
733 uintptr_t addr = (uintptr_t) ptr;
734 uintptr_t pagesize = qemu_real_host_page_size();
735 off_t offset = addr / pagesize * sizeof(pinfo);
736 int fd;
738 fd = open("/proc/self/pagemap", O_RDONLY);
739 if (fd == -1) {
740 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
741 return -1;
744 /* Force copy-on-write if necessary. */
745 qatomic_add((uint8_t *)ptr, 0);
747 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
748 error_setg_errno(errp, errno, "Cannot read pagemap");
749 goto out;
751 if ((pinfo & (1ull << 63)) == 0) {
752 error_setg(errp, "Page not present");
753 goto out;
755 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
757 out:
758 close(fd);
759 return ret;
762 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
764 hwaddr addr = qdict_get_int(qdict, "addr");
765 Error *local_err = NULL;
766 MemoryRegion *mr = NULL;
767 void *ptr;
768 uint64_t physaddr;
770 ptr = gpa2hva(&mr, addr, 1, &local_err);
771 if (local_err) {
772 error_report_err(local_err);
773 return;
776 physaddr = vtop(ptr, &local_err);
777 if (local_err) {
778 error_report_err(local_err);
779 } else {
780 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
781 " (%s) is 0x%" PRIx64 "\n",
782 addr, mr->name, (uint64_t) physaddr);
785 memory_region_unref(mr);
787 #endif
789 static void do_print(Monitor *mon, const QDict *qdict)
791 int format = qdict_get_int(qdict, "format");
792 hwaddr val = qdict_get_int(qdict, "val");
794 switch(format) {
795 case 'o':
796 monitor_printf(mon, "%#" HWADDR_PRIo, val);
797 break;
798 case 'x':
799 monitor_printf(mon, "%#" HWADDR_PRIx, val);
800 break;
801 case 'u':
802 monitor_printf(mon, "%" HWADDR_PRIu, val);
803 break;
804 default:
805 case 'd':
806 monitor_printf(mon, "%" HWADDR_PRId, val);
807 break;
808 case 'c':
809 monitor_printc(mon, val);
810 break;
812 monitor_printf(mon, "\n");
815 static void hmp_sum(Monitor *mon, const QDict *qdict)
817 uint32_t addr;
818 uint16_t sum;
819 uint32_t start = qdict_get_int(qdict, "start");
820 uint32_t size = qdict_get_int(qdict, "size");
822 sum = 0;
823 for(addr = start; addr < (start + size); addr++) {
824 uint8_t val = address_space_ldub(&address_space_memory, addr,
825 MEMTXATTRS_UNSPECIFIED, NULL);
826 /* BSD sum algorithm ('sum' Unix command) */
827 sum = (sum >> 1) | (sum << 15);
828 sum += val;
830 monitor_printf(mon, "%05d\n", sum);
833 static int mouse_button_state;
835 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
837 int dx, dy, dz, button;
838 const char *dx_str = qdict_get_str(qdict, "dx_str");
839 const char *dy_str = qdict_get_str(qdict, "dy_str");
840 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
842 dx = strtol(dx_str, NULL, 0);
843 dy = strtol(dy_str, NULL, 0);
844 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
845 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
847 if (dz_str) {
848 dz = strtol(dz_str, NULL, 0);
849 if (dz != 0) {
850 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
851 qemu_input_queue_btn(NULL, button, true);
852 qemu_input_event_sync();
853 qemu_input_queue_btn(NULL, button, false);
856 qemu_input_event_sync();
859 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
861 static uint32_t bmap[INPUT_BUTTON__MAX] = {
862 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
863 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
864 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
866 int button_state = qdict_get_int(qdict, "button_state");
868 if (mouse_button_state == button_state) {
869 return;
871 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
872 qemu_input_event_sync();
873 mouse_button_state = button_state;
876 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
878 int size = qdict_get_int(qdict, "size");
879 int addr = qdict_get_int(qdict, "addr");
880 int has_index = qdict_haskey(qdict, "index");
881 uint32_t val;
882 int suffix;
884 if (has_index) {
885 int index = qdict_get_int(qdict, "index");
886 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
887 addr++;
889 addr &= 0xffff;
891 switch(size) {
892 default:
893 case 1:
894 val = cpu_inb(addr);
895 suffix = 'b';
896 break;
897 case 2:
898 val = cpu_inw(addr);
899 suffix = 'w';
900 break;
901 case 4:
902 val = cpu_inl(addr);
903 suffix = 'l';
904 break;
906 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
907 suffix, addr, size * 2, val);
910 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
912 int size = qdict_get_int(qdict, "size");
913 int addr = qdict_get_int(qdict, "addr");
914 int val = qdict_get_int(qdict, "val");
916 addr &= IOPORTS_MASK;
918 switch (size) {
919 default:
920 case 1:
921 cpu_outb(addr, val);
922 break;
923 case 2:
924 cpu_outw(addr, val);
925 break;
926 case 4:
927 cpu_outl(addr, val);
928 break;
932 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
934 Error *local_err = NULL;
935 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
937 qemu_boot_set(bootdevice, &local_err);
938 if (local_err) {
939 error_report_err(local_err);
940 } else {
941 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
945 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
947 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
948 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
949 bool owner = qdict_get_try_bool(qdict, "owner", false);
950 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
952 mtree_info(flatview, dispatch_tree, owner, disabled);
955 /* Capture support */
956 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
958 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
960 int i;
961 CaptureState *s;
963 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
964 monitor_printf(mon, "[%d]: ", i);
965 s->ops.info (s->opaque);
969 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
971 int i;
972 int n = qdict_get_int(qdict, "n");
973 CaptureState *s;
975 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
976 if (i == n) {
977 s->ops.destroy (s->opaque);
978 QLIST_REMOVE (s, entries);
979 g_free (s);
980 return;
985 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
987 const char *path = qdict_get_str(qdict, "path");
988 int freq = qdict_get_try_int(qdict, "freq", 44100);
989 int bits = qdict_get_try_int(qdict, "bits", 16);
990 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
991 const char *audiodev = qdict_get_str(qdict, "audiodev");
992 CaptureState *s;
993 AudioState *as = audio_state_by_name(audiodev);
995 if (!as) {
996 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
997 return;
1000 s = g_malloc0 (sizeof (*s));
1002 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1003 monitor_printf(mon, "Failed to add wave capture\n");
1004 g_free (s);
1005 return;
1007 QLIST_INSERT_HEAD (&capture_head, s, entries);
1010 void qmp_getfd(const char *fdname, Error **errp)
1012 Monitor *cur_mon = monitor_cur();
1013 mon_fd_t *monfd;
1014 int fd, tmp_fd;
1016 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1017 if (fd == -1) {
1018 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1019 return;
1022 if (qemu_isdigit(fdname[0])) {
1023 close(fd);
1024 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1025 "a name not starting with a digit");
1026 return;
1029 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1030 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1031 if (strcmp(monfd->name, fdname) != 0) {
1032 continue;
1035 tmp_fd = monfd->fd;
1036 monfd->fd = fd;
1037 /* Make sure close() is outside critical section */
1038 close(tmp_fd);
1039 return;
1042 monfd = g_new0(mon_fd_t, 1);
1043 monfd->name = g_strdup(fdname);
1044 monfd->fd = fd;
1046 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1049 void qmp_closefd(const char *fdname, Error **errp)
1051 Monitor *cur_mon = monitor_cur();
1052 mon_fd_t *monfd;
1053 int tmp_fd;
1055 qemu_mutex_lock(&cur_mon->mon_lock);
1056 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1057 if (strcmp(monfd->name, fdname) != 0) {
1058 continue;
1061 QLIST_REMOVE(monfd, next);
1062 tmp_fd = monfd->fd;
1063 g_free(monfd->name);
1064 g_free(monfd);
1065 qemu_mutex_unlock(&cur_mon->mon_lock);
1066 /* Make sure close() is outside critical section */
1067 close(tmp_fd);
1068 return;
1071 qemu_mutex_unlock(&cur_mon->mon_lock);
1072 error_setg(errp, "File descriptor named '%s' not found", fdname);
1075 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1077 mon_fd_t *monfd;
1079 QEMU_LOCK_GUARD(&mon->mon_lock);
1080 QLIST_FOREACH(monfd, &mon->fds, next) {
1081 int fd;
1083 if (strcmp(monfd->name, fdname) != 0) {
1084 continue;
1087 fd = monfd->fd;
1089 /* caller takes ownership of fd */
1090 QLIST_REMOVE(monfd, next);
1091 g_free(monfd->name);
1092 g_free(monfd);
1094 return fd;
1097 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1098 return -1;
1101 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1103 MonFdsetFd *mon_fdset_fd;
1104 MonFdsetFd *mon_fdset_fd_next;
1106 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1107 if ((mon_fdset_fd->removed ||
1108 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1109 runstate_is_running()) {
1110 close(mon_fdset_fd->fd);
1111 g_free(mon_fdset_fd->opaque);
1112 QLIST_REMOVE(mon_fdset_fd, next);
1113 g_free(mon_fdset_fd);
1117 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1118 QLIST_REMOVE(mon_fdset, next);
1119 g_free(mon_fdset);
1123 void monitor_fdsets_cleanup(void)
1125 MonFdset *mon_fdset;
1126 MonFdset *mon_fdset_next;
1128 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1129 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1130 monitor_fdset_cleanup(mon_fdset);
1134 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1135 const char *opaque, Error **errp)
1137 int fd;
1138 Monitor *mon = monitor_cur();
1139 AddfdInfo *fdinfo;
1141 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1142 if (fd == -1) {
1143 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1144 goto error;
1147 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1148 has_opaque, opaque, errp);
1149 if (fdinfo) {
1150 return fdinfo;
1153 error:
1154 if (fd != -1) {
1155 close(fd);
1157 return NULL;
1160 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1162 MonFdset *mon_fdset;
1163 MonFdsetFd *mon_fdset_fd;
1164 char fd_str[60];
1166 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1167 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1168 if (mon_fdset->id != fdset_id) {
1169 continue;
1171 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1172 if (has_fd) {
1173 if (mon_fdset_fd->fd != fd) {
1174 continue;
1176 mon_fdset_fd->removed = true;
1177 break;
1178 } else {
1179 mon_fdset_fd->removed = true;
1182 if (has_fd && !mon_fdset_fd) {
1183 goto error;
1185 monitor_fdset_cleanup(mon_fdset);
1186 return;
1189 error:
1190 if (has_fd) {
1191 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1192 fdset_id, fd);
1193 } else {
1194 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1196 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1199 FdsetInfoList *qmp_query_fdsets(Error **errp)
1201 MonFdset *mon_fdset;
1202 MonFdsetFd *mon_fdset_fd;
1203 FdsetInfoList *fdset_list = NULL;
1205 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1206 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1207 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1209 fdset_info->fdset_id = mon_fdset->id;
1211 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1212 FdsetFdInfo *fdsetfd_info;
1214 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1215 fdsetfd_info->fd = mon_fdset_fd->fd;
1216 if (mon_fdset_fd->opaque) {
1217 fdsetfd_info->has_opaque = true;
1218 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1219 } else {
1220 fdsetfd_info->has_opaque = false;
1223 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1226 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1229 return fdset_list;
1232 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1233 bool has_opaque, const char *opaque,
1234 Error **errp)
1236 MonFdset *mon_fdset = NULL;
1237 MonFdsetFd *mon_fdset_fd;
1238 AddfdInfo *fdinfo;
1240 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1241 if (has_fdset_id) {
1242 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1243 /* Break if match found or match impossible due to ordering by ID */
1244 if (fdset_id <= mon_fdset->id) {
1245 if (fdset_id < mon_fdset->id) {
1246 mon_fdset = NULL;
1248 break;
1253 if (mon_fdset == NULL) {
1254 int64_t fdset_id_prev = -1;
1255 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1257 if (has_fdset_id) {
1258 if (fdset_id < 0) {
1259 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1260 "a non-negative value");
1261 return NULL;
1263 /* Use specified fdset ID */
1264 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1265 mon_fdset_cur = mon_fdset;
1266 if (fdset_id < mon_fdset_cur->id) {
1267 break;
1270 } else {
1271 /* Use first available fdset ID */
1272 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1273 mon_fdset_cur = mon_fdset;
1274 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1275 fdset_id_prev = mon_fdset_cur->id;
1276 continue;
1278 break;
1282 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1283 if (has_fdset_id) {
1284 mon_fdset->id = fdset_id;
1285 } else {
1286 mon_fdset->id = fdset_id_prev + 1;
1289 /* The fdset list is ordered by fdset ID */
1290 if (!mon_fdset_cur) {
1291 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1292 } else if (mon_fdset->id < mon_fdset_cur->id) {
1293 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1294 } else {
1295 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1299 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1300 mon_fdset_fd->fd = fd;
1301 mon_fdset_fd->removed = false;
1302 if (has_opaque) {
1303 mon_fdset_fd->opaque = g_strdup(opaque);
1305 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1307 fdinfo = g_malloc0(sizeof(*fdinfo));
1308 fdinfo->fdset_id = mon_fdset->id;
1309 fdinfo->fd = mon_fdset_fd->fd;
1311 return fdinfo;
1314 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1316 #ifdef _WIN32
1317 return -ENOENT;
1318 #else
1319 MonFdset *mon_fdset;
1321 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1322 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1323 MonFdsetFd *mon_fdset_fd;
1324 MonFdsetFd *mon_fdset_fd_dup;
1325 int fd = -1;
1326 int dup_fd;
1327 int mon_fd_flags;
1329 if (mon_fdset->id != fdset_id) {
1330 continue;
1333 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1334 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1335 if (mon_fd_flags == -1) {
1336 return -1;
1339 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1340 fd = mon_fdset_fd->fd;
1341 break;
1345 if (fd == -1) {
1346 errno = EACCES;
1347 return -1;
1350 dup_fd = qemu_dup_flags(fd, flags);
1351 if (dup_fd == -1) {
1352 return -1;
1355 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1356 mon_fdset_fd_dup->fd = dup_fd;
1357 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1358 return dup_fd;
1361 errno = ENOENT;
1362 return -1;
1363 #endif
1366 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1368 MonFdset *mon_fdset;
1369 MonFdsetFd *mon_fdset_fd_dup;
1371 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1372 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1373 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1374 if (mon_fdset_fd_dup->fd == dup_fd) {
1375 if (remove) {
1376 QLIST_REMOVE(mon_fdset_fd_dup, next);
1377 g_free(mon_fdset_fd_dup);
1378 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1379 monitor_fdset_cleanup(mon_fdset);
1381 return -1;
1382 } else {
1383 return mon_fdset->id;
1389 return -1;
1392 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1394 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1397 void monitor_fdset_dup_fd_remove(int dup_fd)
1399 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1402 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1404 int fd;
1405 Error *local_err = NULL;
1407 if (!qemu_isdigit(fdname[0]) && mon) {
1408 fd = monitor_get_fd(mon, fdname, &local_err);
1409 } else {
1410 fd = qemu_parse_fd(fdname);
1411 if (fd == -1) {
1412 error_setg(&local_err, "Invalid file descriptor number '%s'",
1413 fdname);
1416 if (local_err) {
1417 error_propagate(errp, local_err);
1418 assert(fd == -1);
1419 } else {
1420 assert(fd != -1);
1423 return fd;
1426 /* Please update hmp-commands.hx when adding or changing commands */
1427 static HMPCommand hmp_info_cmds[] = {
1428 #include "hmp-commands-info.h"
1429 { NULL, NULL, },
1432 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1433 HMPCommand hmp_cmds[] = {
1434 #include "hmp-commands.h"
1435 { NULL, NULL, },
1439 * Set @pval to the value in the register identified by @name.
1440 * return 0 if OK, -1 if not found
1442 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1444 const MonitorDef *md = target_monitor_defs();
1445 CPUState *cs = mon_get_cpu(mon);
1446 void *ptr;
1447 uint64_t tmp = 0;
1448 int ret;
1450 if (cs == NULL || md == NULL) {
1451 return -1;
1454 for(; md->name != NULL; md++) {
1455 if (hmp_compare_cmd(name, md->name)) {
1456 if (md->get_value) {
1457 *pval = md->get_value(mon, md, md->offset);
1458 } else {
1459 CPUArchState *env = mon_get_cpu_env(mon);
1460 ptr = (uint8_t *)env + md->offset;
1461 switch(md->type) {
1462 case MD_I32:
1463 *pval = *(int32_t *)ptr;
1464 break;
1465 case MD_TLONG:
1466 *pval = *(target_long *)ptr;
1467 break;
1468 default:
1469 *pval = 0;
1470 break;
1473 return 0;
1477 ret = target_get_monitor_def(cs, name, &tmp);
1478 if (!ret) {
1479 *pval = (target_long) tmp;
1482 return ret;
1485 static void add_completion_option(ReadLineState *rs, const char *str,
1486 const char *option)
1488 if (!str || !option) {
1489 return;
1491 if (!strncmp(option, str, strlen(str))) {
1492 readline_add_completion(rs, option);
1496 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1498 size_t len;
1499 ChardevBackendInfoList *list, *start;
1501 if (nb_args != 2) {
1502 return;
1504 len = strlen(str);
1505 readline_set_completion_index(rs, len);
1507 start = list = qmp_query_chardev_backends(NULL);
1508 while (list) {
1509 const char *chr_name = list->value->name;
1511 if (!strncmp(chr_name, str, len)) {
1512 readline_add_completion(rs, chr_name);
1514 list = list->next;
1516 qapi_free_ChardevBackendInfoList(start);
1519 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1521 size_t len;
1522 int i;
1524 if (nb_args != 2) {
1525 return;
1527 len = strlen(str);
1528 readline_set_completion_index(rs, len);
1529 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1530 add_completion_option(rs, str, NetClientDriver_str(i));
1534 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1536 GSList *list, *elt;
1537 size_t len;
1539 if (nb_args != 2) {
1540 return;
1543 len = strlen(str);
1544 readline_set_completion_index(rs, len);
1545 list = elt = object_class_get_list(TYPE_DEVICE, false);
1546 while (elt) {
1547 const char *name;
1548 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1549 TYPE_DEVICE);
1550 name = object_class_get_name(OBJECT_CLASS(dc));
1552 if (dc->user_creatable
1553 && !strncmp(name, str, len)) {
1554 readline_add_completion(rs, name);
1556 elt = elt->next;
1558 g_slist_free(list);
1561 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1563 GSList *list, *elt;
1564 size_t len;
1566 if (nb_args != 2) {
1567 return;
1570 len = strlen(str);
1571 readline_set_completion_index(rs, len);
1572 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1573 while (elt) {
1574 const char *name;
1576 name = object_class_get_name(OBJECT_CLASS(elt->data));
1577 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1578 readline_add_completion(rs, name);
1580 elt = elt->next;
1582 g_slist_free(list);
1585 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1587 GSList **list = opaque;
1588 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1590 if (dev == NULL) {
1591 return 0;
1594 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1595 *list = g_slist_append(*list, dev);
1598 return 0;
1601 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1603 GSList *list = NULL;
1605 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1607 return list;
1610 static void peripheral_device_del_completion(ReadLineState *rs,
1611 const char *str, size_t len)
1613 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1614 GSList *list, *item;
1616 list = qdev_build_hotpluggable_device_list(peripheral);
1617 if (!list) {
1618 return;
1621 for (item = list; item; item = g_slist_next(item)) {
1622 DeviceState *dev = item->data;
1624 if (dev->id && !strncmp(str, dev->id, len)) {
1625 readline_add_completion(rs, dev->id);
1629 g_slist_free(list);
1632 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1634 size_t len;
1635 ChardevInfoList *list, *start;
1637 if (nb_args != 2) {
1638 return;
1640 len = strlen(str);
1641 readline_set_completion_index(rs, len);
1643 start = list = qmp_query_chardev(NULL);
1644 while (list) {
1645 ChardevInfo *chr = list->value;
1647 if (!strncmp(chr->label, str, len)) {
1648 readline_add_completion(rs, chr->label);
1650 list = list->next;
1652 qapi_free_ChardevInfoList(start);
1655 static void ringbuf_completion(ReadLineState *rs, const char *str)
1657 size_t len;
1658 ChardevInfoList *list, *start;
1660 len = strlen(str);
1661 readline_set_completion_index(rs, len);
1663 start = list = qmp_query_chardev(NULL);
1664 while (list) {
1665 ChardevInfo *chr_info = list->value;
1667 if (!strncmp(chr_info->label, str, len)) {
1668 Chardev *chr = qemu_chr_find(chr_info->label);
1669 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1670 readline_add_completion(rs, chr_info->label);
1673 list = list->next;
1675 qapi_free_ChardevInfoList(start);
1678 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1680 if (nb_args != 2) {
1681 return;
1683 ringbuf_completion(rs, str);
1686 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1688 size_t len;
1690 if (nb_args != 2) {
1691 return;
1694 len = strlen(str);
1695 readline_set_completion_index(rs, len);
1696 peripheral_device_del_completion(rs, str, len);
1699 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1701 ObjectPropertyInfoList *list, *start;
1702 size_t len;
1704 if (nb_args != 2) {
1705 return;
1707 len = strlen(str);
1708 readline_set_completion_index(rs, len);
1710 start = list = qmp_qom_list("/objects", NULL);
1711 while (list) {
1712 ObjectPropertyInfo *info = list->value;
1714 if (!strncmp(info->type, "child<", 5)
1715 && !strncmp(info->name, str, len)) {
1716 readline_add_completion(rs, info->name);
1718 list = list->next;
1720 qapi_free_ObjectPropertyInfoList(start);
1723 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1725 int i;
1726 char *sep;
1727 size_t len;
1729 if (nb_args != 2) {
1730 return;
1732 sep = strrchr(str, '-');
1733 if (sep) {
1734 str = sep + 1;
1736 len = strlen(str);
1737 readline_set_completion_index(rs, len);
1738 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1739 if (!strncmp(str, QKeyCode_str(i), len)) {
1740 readline_add_completion(rs, QKeyCode_str(i));
1745 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1747 size_t len;
1749 len = strlen(str);
1750 readline_set_completion_index(rs, len);
1751 if (nb_args == 2) {
1752 NetClientState *ncs[MAX_QUEUE_NUM];
1753 int count, i;
1754 count = qemu_find_net_clients_except(NULL, ncs,
1755 NET_CLIENT_DRIVER_NONE,
1756 MAX_QUEUE_NUM);
1757 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1758 const char *name = ncs[i]->name;
1759 if (!strncmp(str, name, len)) {
1760 readline_add_completion(rs, name);
1763 } else if (nb_args == 3) {
1764 add_completion_option(rs, str, "on");
1765 add_completion_option(rs, str, "off");
1769 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1771 int len, count, i;
1772 NetClientState *ncs[MAX_QUEUE_NUM];
1774 if (nb_args != 2) {
1775 return;
1778 len = strlen(str);
1779 readline_set_completion_index(rs, len);
1780 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1781 MAX_QUEUE_NUM);
1782 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1783 const char *name = ncs[i]->name;
1784 if (strncmp(str, name, len)) {
1785 continue;
1787 if (ncs[i]->is_netdev) {
1788 readline_add_completion(rs, name);
1793 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1795 size_t len;
1797 len = strlen(str);
1798 readline_set_completion_index(rs, len);
1799 if (nb_args == 2) {
1800 TraceEventIter iter;
1801 TraceEvent *ev;
1802 char *pattern = g_strdup_printf("%s*", str);
1803 trace_event_iter_init_pattern(&iter, pattern);
1804 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1805 readline_add_completion(rs, trace_event_get_name(ev));
1807 g_free(pattern);
1811 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1813 size_t len;
1815 len = strlen(str);
1816 readline_set_completion_index(rs, len);
1817 if (nb_args == 2) {
1818 TraceEventIter iter;
1819 TraceEvent *ev;
1820 char *pattern = g_strdup_printf("%s*", str);
1821 trace_event_iter_init_pattern(&iter, pattern);
1822 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1823 readline_add_completion(rs, trace_event_get_name(ev));
1825 g_free(pattern);
1826 } else if (nb_args == 3) {
1827 add_completion_option(rs, str, "on");
1828 add_completion_option(rs, str, "off");
1832 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1834 int i;
1836 if (nb_args != 2) {
1837 return;
1839 readline_set_completion_index(rs, strlen(str));
1840 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1841 add_completion_option(rs, str, WatchdogAction_str(i));
1845 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1846 const char *str)
1848 size_t len;
1850 len = strlen(str);
1851 readline_set_completion_index(rs, len);
1852 if (nb_args == 2) {
1853 int i;
1854 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1855 const char *name = MigrationCapability_str(i);
1856 if (!strncmp(str, name, len)) {
1857 readline_add_completion(rs, name);
1860 } else if (nb_args == 3) {
1861 add_completion_option(rs, str, "on");
1862 add_completion_option(rs, str, "off");
1866 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1867 const char *str)
1869 size_t len;
1871 len = strlen(str);
1872 readline_set_completion_index(rs, len);
1873 if (nb_args == 2) {
1874 int i;
1875 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1876 const char *name = MigrationParameter_str(i);
1877 if (!strncmp(str, name, len)) {
1878 readline_add_completion(rs, name);
1884 static void vm_completion(ReadLineState *rs, const char *str)
1886 size_t len;
1887 BlockDriverState *bs;
1888 BdrvNextIterator it;
1890 len = strlen(str);
1891 readline_set_completion_index(rs, len);
1893 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1894 SnapshotInfoList *snapshots, *snapshot;
1895 AioContext *ctx = bdrv_get_aio_context(bs);
1896 bool ok = false;
1898 aio_context_acquire(ctx);
1899 if (bdrv_can_snapshot(bs)) {
1900 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1902 aio_context_release(ctx);
1903 if (!ok) {
1904 continue;
1907 snapshot = snapshots;
1908 while (snapshot) {
1909 char *completion = snapshot->value->name;
1910 if (!strncmp(str, completion, len)) {
1911 readline_add_completion(rs, completion);
1913 completion = snapshot->value->id;
1914 if (!strncmp(str, completion, len)) {
1915 readline_add_completion(rs, completion);
1917 snapshot = snapshot->next;
1919 qapi_free_SnapshotInfoList(snapshots);
1924 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1926 if (nb_args == 2) {
1927 vm_completion(rs, str);
1931 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1933 if (nb_args == 2) {
1934 vm_completion(rs, str);
1938 static int
1939 compare_mon_cmd(const void *a, const void *b)
1941 return strcmp(((const HMPCommand *)a)->name,
1942 ((const HMPCommand *)b)->name);
1945 static void sortcmdlist(void)
1947 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1948 sizeof(*hmp_cmds),
1949 compare_mon_cmd);
1950 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1951 sizeof(*hmp_info_cmds),
1952 compare_mon_cmd);
1955 void monitor_register_hmp(const char *name, bool info,
1956 void (*cmd)(Monitor *mon, const QDict *qdict))
1958 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1960 while (table->name != NULL) {
1961 if (strcmp(table->name, name) == 0) {
1962 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1963 table->cmd = cmd;
1964 return;
1966 table++;
1968 g_assert_not_reached();
1971 void monitor_register_hmp_info_hrt(const char *name,
1972 HumanReadableText *(*handler)(Error **errp))
1974 HMPCommand *table = hmp_info_cmds;
1976 while (table->name != NULL) {
1977 if (strcmp(table->name, name) == 0) {
1978 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1979 table->cmd_info_hrt = handler;
1980 return;
1982 table++;
1984 g_assert_not_reached();
1987 void monitor_init_globals(void)
1989 monitor_init_globals_core();
1990 monitor_init_qmp_commands();
1991 sortcmdlist();
1992 qemu_mutex_init(&mon_fdsets_lock);