mirror: Do not enter a paused job on completion
[qemu.git] / monitor / misc.c
blob55f374405367e70ec49640e3c1afb03bee7a8b1d
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 "cpu.h"
28 #include "monitor/qdev.h"
29 #include "hw/usb.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
34 #include "net/net.h"
35 #include "net/slirp.h"
36 #include "ui/qemu-spice.h"
37 #include "qemu/config-file.h"
38 #include "qemu/ctype.h"
39 #include "ui/console.h"
40 #include "ui/input.h"
41 #include "audio/audio.h"
42 #include "disas/disas.h"
43 #include "sysemu/balloon.h"
44 #include "qemu/timer.h"
45 #include "sysemu/hw_accel.h"
46 #include "sysemu/runstate.h"
47 #include "authz/list.h"
48 #include "qapi/util.h"
49 #include "sysemu/blockdev.h"
50 #include "sysemu/sysemu.h"
51 #include "sysemu/tcg.h"
52 #include "sysemu/tpm.h"
53 #include "qapi/qmp/qdict.h"
54 #include "qapi/qmp/qerror.h"
55 #include "qapi/qmp/qstring.h"
56 #include "qom/object_interfaces.h"
57 #include "trace/control.h"
58 #include "monitor/hmp-target.h"
59 #include "monitor/hmp.h"
60 #ifdef CONFIG_TRACE_SIMPLE
61 #include "trace/simple.h"
62 #endif
63 #include "exec/memory.h"
64 #include "exec/exec-all.h"
65 #include "qemu/option.h"
66 #include "qemu/thread.h"
67 #include "block/qapi.h"
68 #include "block/block-hmp-cmds.h"
69 #include "qapi/qapi-commands-char.h"
70 #include "qapi/qapi-commands-control.h"
71 #include "qapi/qapi-commands-migration.h"
72 #include "qapi/qapi-commands-misc.h"
73 #include "qapi/qapi-commands-qom.h"
74 #include "qapi/qapi-commands-trace.h"
75 #include "qapi/qapi-init-commands.h"
76 #include "qapi/error.h"
77 #include "qapi/qmp-event.h"
78 #include "sysemu/cpus.h"
79 #include "qemu/cutils.h"
81 #if defined(TARGET_S390X)
82 #include "hw/s390x/storage-keys.h"
83 #include "hw/s390x/storage-attributes.h"
84 #endif
86 /* file descriptors passed via SCM_RIGHTS */
87 typedef struct mon_fd_t mon_fd_t;
88 struct mon_fd_t {
89 char *name;
90 int fd;
91 QLIST_ENTRY(mon_fd_t) next;
94 /* file descriptor associated with a file descriptor set */
95 typedef struct MonFdsetFd MonFdsetFd;
96 struct MonFdsetFd {
97 int fd;
98 bool removed;
99 char *opaque;
100 QLIST_ENTRY(MonFdsetFd) next;
103 /* file descriptor set containing fds passed via SCM_RIGHTS */
104 typedef struct MonFdset MonFdset;
105 struct MonFdset {
106 int64_t id;
107 QLIST_HEAD(, MonFdsetFd) fds;
108 QLIST_HEAD(, MonFdsetFd) dup_fds;
109 QLIST_ENTRY(MonFdset) next;
112 /* Protects mon_fdsets */
113 static QemuMutex mon_fdsets_lock;
114 static QLIST_HEAD(, MonFdset) mon_fdsets;
116 static HMPCommand hmp_info_cmds[];
118 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
119 int64_t cpu_index, Error **errp)
121 char *output = NULL;
122 MonitorHMP hmp = {};
124 monitor_data_init(&hmp.common, false, true, false);
126 if (has_cpu_index) {
127 int ret = monitor_set_cpu(&hmp.common, cpu_index);
128 if (ret < 0) {
129 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
130 "a CPU number");
131 goto out;
135 handle_hmp_command(&hmp, command_line);
137 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
138 output = g_strdup(hmp.common.outbuf->str);
141 out:
142 monitor_data_destroy(&hmp.common);
143 return output;
147 * Is @name in the '|' separated list of names @list?
149 int hmp_compare_cmd(const char *name, const char *list)
151 const char *p, *pstart;
152 int len;
153 len = strlen(name);
154 p = list;
155 for (;;) {
156 pstart = p;
157 p = qemu_strchrnul(p, '|');
158 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
159 return 1;
161 if (*p == '\0') {
162 break;
164 p++;
166 return 0;
169 static void do_help_cmd(Monitor *mon, const QDict *qdict)
171 help_cmd(mon, qdict_get_try_str(qdict, "name"));
174 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
176 const char *tp_name = qdict_get_str(qdict, "name");
177 bool new_state = qdict_get_bool(qdict, "option");
178 bool has_vcpu = qdict_haskey(qdict, "vcpu");
179 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
180 Error *local_err = NULL;
182 if (vcpu < 0) {
183 monitor_printf(mon, "argument vcpu must be positive");
184 return;
187 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
188 if (local_err) {
189 error_report_err(local_err);
193 #ifdef CONFIG_TRACE_SIMPLE
194 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
196 const char *op = qdict_get_try_str(qdict, "op");
197 const char *arg = qdict_get_try_str(qdict, "arg");
199 if (!op) {
200 st_print_trace_file_status();
201 } else if (!strcmp(op, "on")) {
202 st_set_trace_file_enabled(true);
203 } else if (!strcmp(op, "off")) {
204 st_set_trace_file_enabled(false);
205 } else if (!strcmp(op, "flush")) {
206 st_flush_trace_buffer();
207 } else if (!strcmp(op, "set")) {
208 if (arg) {
209 st_set_trace_file(arg);
211 } else {
212 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
213 help_cmd(mon, "trace-file");
216 #endif
218 static void hmp_info_help(Monitor *mon, const QDict *qdict)
220 help_cmd(mon, "info");
223 static void monitor_init_qmp_commands(void)
226 * Two command lists:
227 * - qmp_commands contains all QMP commands
228 * - qmp_cap_negotiation_commands contains just
229 * "qmp_capabilities", to enforce capability negotiation
232 qmp_init_marshal(&qmp_commands);
234 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
235 QCO_NO_OPTIONS);
237 QTAILQ_INIT(&qmp_cap_negotiation_commands);
238 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
239 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
242 /* Set the current CPU defined by the user. Callers must hold BQL. */
243 int monitor_set_cpu(Monitor *mon, int cpu_index)
245 CPUState *cpu;
247 cpu = qemu_get_cpu(cpu_index);
248 if (cpu == NULL) {
249 return -1;
251 g_free(mon->mon_cpu_path);
252 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
253 return 0;
256 /* Callers must hold BQL. */
257 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
259 CPUState *cpu = NULL;
261 if (mon->mon_cpu_path) {
262 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
263 TYPE_CPU, NULL);
264 if (!cpu) {
265 g_free(mon->mon_cpu_path);
266 mon->mon_cpu_path = NULL;
269 if (!mon->mon_cpu_path) {
270 if (!first_cpu) {
271 return NULL;
273 monitor_set_cpu(mon, first_cpu->cpu_index);
274 cpu = first_cpu;
276 assert(cpu != NULL);
277 if (synchronize) {
278 cpu_synchronize_state(cpu);
280 return cpu;
283 CPUState *mon_get_cpu(Monitor *mon)
285 return mon_get_cpu_sync(mon, true);
288 CPUArchState *mon_get_cpu_env(Monitor *mon)
290 CPUState *cs = mon_get_cpu(mon);
292 return cs ? cs->env_ptr : NULL;
295 int monitor_get_cpu_index(Monitor *mon)
297 CPUState *cs = mon_get_cpu_sync(mon, false);
299 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
302 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
304 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
305 CPUState *cs;
307 if (all_cpus) {
308 CPU_FOREACH(cs) {
309 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
310 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
312 } else {
313 cs = mon_get_cpu(mon);
315 if (!cs) {
316 monitor_printf(mon, "No CPU available\n");
317 return;
320 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
324 #ifdef CONFIG_TCG
325 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
327 if (!tcg_enabled()) {
328 error_report("JIT information is only available with accel=tcg");
329 return;
332 dump_exec_info();
333 dump_drift_info();
336 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
338 dump_opcount_info();
340 #endif
342 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
344 int64_t max = qdict_get_try_int(qdict, "max", 10);
345 bool mean = qdict_get_try_bool(qdict, "mean", false);
346 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
347 enum QSPSortBy sort_by;
349 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
350 qsp_report(max, sort_by, coalesce);
353 static void hmp_info_history(Monitor *mon, const QDict *qdict)
355 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
356 int i;
357 const char *str;
359 if (!hmp_mon->rs) {
360 return;
362 i = 0;
363 for(;;) {
364 str = readline_get_history(hmp_mon->rs, i);
365 if (!str) {
366 break;
368 monitor_printf(mon, "%d: '%s'\n", i, str);
369 i++;
373 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
375 CPUState *cs = mon_get_cpu(mon);
377 if (!cs) {
378 monitor_printf(mon, "No CPU available\n");
379 return;
381 cpu_dump_statistics(cs, 0);
384 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
386 const char *name = qdict_get_try_str(qdict, "name");
387 bool has_vcpu = qdict_haskey(qdict, "vcpu");
388 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
389 TraceEventInfoList *events;
390 TraceEventInfoList *elem;
391 Error *local_err = NULL;
393 if (name == NULL) {
394 name = "*";
396 if (vcpu < 0) {
397 monitor_printf(mon, "argument vcpu must be positive");
398 return;
401 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
402 if (local_err) {
403 error_report_err(local_err);
404 return;
407 for (elem = events; elem != NULL; elem = elem->next) {
408 monitor_printf(mon, "%s : state %u\n",
409 elem->value->name,
410 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
412 qapi_free_TraceEventInfoList(events);
415 void qmp_client_migrate_info(const char *protocol, const char *hostname,
416 bool has_port, int64_t port,
417 bool has_tls_port, int64_t tls_port,
418 bool has_cert_subject, const char *cert_subject,
419 Error **errp)
421 if (strcmp(protocol, "spice") == 0) {
422 if (!qemu_using_spice(errp)) {
423 return;
426 if (!has_port && !has_tls_port) {
427 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
428 return;
431 if (qemu_spice.migrate_info(hostname,
432 has_port ? port : -1,
433 has_tls_port ? tls_port : -1,
434 cert_subject)) {
435 error_setg(errp, "Could not set up display for migration");
436 return;
438 return;
441 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
444 static void hmp_logfile(Monitor *mon, const QDict *qdict)
446 Error *err = NULL;
448 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
449 if (err) {
450 error_report_err(err);
454 static void hmp_log(Monitor *mon, const QDict *qdict)
456 int mask;
457 const char *items = qdict_get_str(qdict, "items");
459 if (!strcmp(items, "none")) {
460 mask = 0;
461 } else {
462 mask = qemu_str_to_log_mask(items);
463 if (!mask) {
464 help_cmd(mon, "log");
465 return;
468 qemu_set_log(mask);
471 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
473 const char *option = qdict_get_try_str(qdict, "option");
474 if (!option || !strcmp(option, "on")) {
475 singlestep = 1;
476 } else if (!strcmp(option, "off")) {
477 singlestep = 0;
478 } else {
479 monitor_printf(mon, "unexpected option %s\n", option);
483 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
485 const char *device = qdict_get_try_str(qdict, "device");
486 if (!device) {
487 device = "tcp::" DEFAULT_GDBSTUB_PORT;
490 if (gdbserver_start(device) < 0) {
491 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
492 device);
493 } else if (strcmp(device, "none") == 0) {
494 monitor_printf(mon, "Disabled gdbserver\n");
495 } else {
496 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
497 device);
501 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
503 const char *action = qdict_get_str(qdict, "action");
504 if (select_watchdog_action(action) == -1) {
505 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
509 static void monitor_printc(Monitor *mon, int c)
511 monitor_printf(mon, "'");
512 switch(c) {
513 case '\'':
514 monitor_printf(mon, "\\'");
515 break;
516 case '\\':
517 monitor_printf(mon, "\\\\");
518 break;
519 case '\n':
520 monitor_printf(mon, "\\n");
521 break;
522 case '\r':
523 monitor_printf(mon, "\\r");
524 break;
525 default:
526 if (c >= 32 && c <= 126) {
527 monitor_printf(mon, "%c", c);
528 } else {
529 monitor_printf(mon, "\\x%02x", c);
531 break;
533 monitor_printf(mon, "'");
536 static void memory_dump(Monitor *mon, int count, int format, int wsize,
537 hwaddr addr, int is_physical)
539 int l, line_size, i, max_digits, len;
540 uint8_t buf[16];
541 uint64_t v;
542 CPUState *cs = mon_get_cpu(mon);
544 if (!cs && (format == 'i' || !is_physical)) {
545 monitor_printf(mon, "Can not dump without CPU\n");
546 return;
549 if (format == 'i') {
550 monitor_disas(mon, cs, addr, count, is_physical);
551 return;
554 len = wsize * count;
555 if (wsize == 1) {
556 line_size = 8;
557 } else {
558 line_size = 16;
560 max_digits = 0;
562 switch(format) {
563 case 'o':
564 max_digits = DIV_ROUND_UP(wsize * 8, 3);
565 break;
566 default:
567 case 'x':
568 max_digits = (wsize * 8) / 4;
569 break;
570 case 'u':
571 case 'd':
572 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
573 break;
574 case 'c':
575 wsize = 1;
576 break;
579 while (len > 0) {
580 if (is_physical) {
581 monitor_printf(mon, TARGET_FMT_plx ":", addr);
582 } else {
583 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
585 l = len;
586 if (l > line_size)
587 l = line_size;
588 if (is_physical) {
589 AddressSpace *as = cs ? cs->as : &address_space_memory;
590 MemTxResult r = address_space_read(as, addr,
591 MEMTXATTRS_UNSPECIFIED, buf, l);
592 if (r != MEMTX_OK) {
593 monitor_printf(mon, " Cannot access memory\n");
594 break;
596 } else {
597 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
598 monitor_printf(mon, " Cannot access memory\n");
599 break;
602 i = 0;
603 while (i < l) {
604 switch(wsize) {
605 default:
606 case 1:
607 v = ldub_p(buf + i);
608 break;
609 case 2:
610 v = lduw_p(buf + i);
611 break;
612 case 4:
613 v = (uint32_t)ldl_p(buf + i);
614 break;
615 case 8:
616 v = ldq_p(buf + i);
617 break;
619 monitor_printf(mon, " ");
620 switch(format) {
621 case 'o':
622 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
623 break;
624 case 'x':
625 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
626 break;
627 case 'u':
628 monitor_printf(mon, "%*" PRIu64, max_digits, v);
629 break;
630 case 'd':
631 monitor_printf(mon, "%*" PRId64, max_digits, v);
632 break;
633 case 'c':
634 monitor_printc(mon, v);
635 break;
637 i += wsize;
639 monitor_printf(mon, "\n");
640 addr += l;
641 len -= l;
645 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
647 int count = qdict_get_int(qdict, "count");
648 int format = qdict_get_int(qdict, "format");
649 int size = qdict_get_int(qdict, "size");
650 target_long addr = qdict_get_int(qdict, "addr");
652 memory_dump(mon, count, format, size, addr, 0);
655 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
657 int count = qdict_get_int(qdict, "count");
658 int format = qdict_get_int(qdict, "format");
659 int size = qdict_get_int(qdict, "size");
660 hwaddr addr = qdict_get_int(qdict, "addr");
662 memory_dump(mon, count, format, size, addr, 1);
665 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
667 Int128 gpa_region_size;
668 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
669 addr, size);
671 if (!mrs.mr) {
672 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
673 return NULL;
676 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
677 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
678 memory_region_unref(mrs.mr);
679 return NULL;
682 gpa_region_size = int128_make64(size);
683 if (int128_lt(mrs.size, gpa_region_size)) {
684 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
685 " exceeded.", addr);
686 memory_region_unref(mrs.mr);
687 return NULL;
690 *p_mr = mrs.mr;
691 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
694 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
696 hwaddr addr = qdict_get_int(qdict, "addr");
697 Error *local_err = NULL;
698 MemoryRegion *mr = NULL;
699 void *ptr;
701 ptr = gpa2hva(&mr, addr, 1, &local_err);
702 if (local_err) {
703 error_report_err(local_err);
704 return;
707 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
708 " (%s) is %p\n",
709 addr, mr->name, ptr);
711 memory_region_unref(mr);
714 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
716 target_ulong addr = qdict_get_int(qdict, "addr");
717 MemTxAttrs attrs;
718 CPUState *cs = mon_get_cpu(mon);
719 hwaddr gpa;
721 if (!cs) {
722 monitor_printf(mon, "No cpu\n");
723 return;
726 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
727 if (gpa == -1) {
728 monitor_printf(mon, "Unmapped\n");
729 } else {
730 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
731 gpa + (addr & ~TARGET_PAGE_MASK));
735 #ifdef CONFIG_LINUX
736 static uint64_t vtop(void *ptr, Error **errp)
738 uint64_t pinfo;
739 uint64_t ret = -1;
740 uintptr_t addr = (uintptr_t) ptr;
741 uintptr_t pagesize = qemu_real_host_page_size;
742 off_t offset = addr / pagesize * sizeof(pinfo);
743 int fd;
745 fd = open("/proc/self/pagemap", O_RDONLY);
746 if (fd == -1) {
747 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
748 return -1;
751 /* Force copy-on-write if necessary. */
752 qatomic_add((uint8_t *)ptr, 0);
754 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
755 error_setg_errno(errp, errno, "Cannot read pagemap");
756 goto out;
758 if ((pinfo & (1ull << 63)) == 0) {
759 error_setg(errp, "Page not present");
760 goto out;
762 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
764 out:
765 close(fd);
766 return ret;
769 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
771 hwaddr addr = qdict_get_int(qdict, "addr");
772 Error *local_err = NULL;
773 MemoryRegion *mr = NULL;
774 void *ptr;
775 uint64_t physaddr;
777 ptr = gpa2hva(&mr, addr, 1, &local_err);
778 if (local_err) {
779 error_report_err(local_err);
780 return;
783 physaddr = vtop(ptr, &local_err);
784 if (local_err) {
785 error_report_err(local_err);
786 } else {
787 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
788 " (%s) is 0x%" PRIx64 "\n",
789 addr, mr->name, (uint64_t) physaddr);
792 memory_region_unref(mr);
794 #endif
796 static void do_print(Monitor *mon, const QDict *qdict)
798 int format = qdict_get_int(qdict, "format");
799 hwaddr val = qdict_get_int(qdict, "val");
801 switch(format) {
802 case 'o':
803 monitor_printf(mon, "%#" HWADDR_PRIo, val);
804 break;
805 case 'x':
806 monitor_printf(mon, "%#" HWADDR_PRIx, val);
807 break;
808 case 'u':
809 monitor_printf(mon, "%" HWADDR_PRIu, val);
810 break;
811 default:
812 case 'd':
813 monitor_printf(mon, "%" HWADDR_PRId, val);
814 break;
815 case 'c':
816 monitor_printc(mon, val);
817 break;
819 monitor_printf(mon, "\n");
822 static void hmp_sum(Monitor *mon, const QDict *qdict)
824 uint32_t addr;
825 uint16_t sum;
826 uint32_t start = qdict_get_int(qdict, "start");
827 uint32_t size = qdict_get_int(qdict, "size");
829 sum = 0;
830 for(addr = start; addr < (start + size); addr++) {
831 uint8_t val = address_space_ldub(&address_space_memory, addr,
832 MEMTXATTRS_UNSPECIFIED, NULL);
833 /* BSD sum algorithm ('sum' Unix command) */
834 sum = (sum >> 1) | (sum << 15);
835 sum += val;
837 monitor_printf(mon, "%05d\n", sum);
840 static int mouse_button_state;
842 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
844 int dx, dy, dz, button;
845 const char *dx_str = qdict_get_str(qdict, "dx_str");
846 const char *dy_str = qdict_get_str(qdict, "dy_str");
847 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
849 dx = strtol(dx_str, NULL, 0);
850 dy = strtol(dy_str, NULL, 0);
851 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
852 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
854 if (dz_str) {
855 dz = strtol(dz_str, NULL, 0);
856 if (dz != 0) {
857 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
858 qemu_input_queue_btn(NULL, button, true);
859 qemu_input_event_sync();
860 qemu_input_queue_btn(NULL, button, false);
863 qemu_input_event_sync();
866 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
868 static uint32_t bmap[INPUT_BUTTON__MAX] = {
869 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
870 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
871 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
873 int button_state = qdict_get_int(qdict, "button_state");
875 if (mouse_button_state == button_state) {
876 return;
878 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
879 qemu_input_event_sync();
880 mouse_button_state = button_state;
883 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
885 int size = qdict_get_int(qdict, "size");
886 int addr = qdict_get_int(qdict, "addr");
887 int has_index = qdict_haskey(qdict, "index");
888 uint32_t val;
889 int suffix;
891 if (has_index) {
892 int index = qdict_get_int(qdict, "index");
893 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
894 addr++;
896 addr &= 0xffff;
898 switch(size) {
899 default:
900 case 1:
901 val = cpu_inb(addr);
902 suffix = 'b';
903 break;
904 case 2:
905 val = cpu_inw(addr);
906 suffix = 'w';
907 break;
908 case 4:
909 val = cpu_inl(addr);
910 suffix = 'l';
911 break;
913 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
914 suffix, addr, size * 2, val);
917 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
919 int size = qdict_get_int(qdict, "size");
920 int addr = qdict_get_int(qdict, "addr");
921 int val = qdict_get_int(qdict, "val");
923 addr &= IOPORTS_MASK;
925 switch (size) {
926 default:
927 case 1:
928 cpu_outb(addr, val);
929 break;
930 case 2:
931 cpu_outw(addr, val);
932 break;
933 case 4:
934 cpu_outl(addr, val);
935 break;
939 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
941 Error *local_err = NULL;
942 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
944 qemu_boot_set(bootdevice, &local_err);
945 if (local_err) {
946 error_report_err(local_err);
947 } else {
948 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
952 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
954 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
955 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
956 bool owner = qdict_get_try_bool(qdict, "owner", false);
957 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
959 mtree_info(flatview, dispatch_tree, owner, disabled);
962 #ifdef CONFIG_PROFILER
964 int64_t dev_time;
966 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
968 static int64_t last_cpu_exec_time;
969 int64_t cpu_exec_time;
970 int64_t delta;
972 cpu_exec_time = tcg_cpu_exec_time();
973 delta = cpu_exec_time - last_cpu_exec_time;
975 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
976 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
977 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
978 delta, delta / (double)NANOSECONDS_PER_SECOND);
979 last_cpu_exec_time = cpu_exec_time;
980 dev_time = 0;
982 #else
983 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
985 monitor_printf(mon, "Internal profiler not compiled\n");
987 #endif
989 /* Capture support */
990 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
992 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
994 int i;
995 CaptureState *s;
997 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
998 monitor_printf(mon, "[%d]: ", i);
999 s->ops.info (s->opaque);
1003 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1005 int i;
1006 int n = qdict_get_int(qdict, "n");
1007 CaptureState *s;
1009 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1010 if (i == n) {
1011 s->ops.destroy (s->opaque);
1012 QLIST_REMOVE (s, entries);
1013 g_free (s);
1014 return;
1019 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1021 const char *path = qdict_get_str(qdict, "path");
1022 int freq = qdict_get_try_int(qdict, "freq", 44100);
1023 int bits = qdict_get_try_int(qdict, "bits", 16);
1024 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1025 const char *audiodev = qdict_get_str(qdict, "audiodev");
1026 CaptureState *s;
1027 AudioState *as = audio_state_by_name(audiodev);
1029 if (!as) {
1030 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1031 return;
1034 s = g_malloc0 (sizeof (*s));
1036 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1037 monitor_printf(mon, "Failed to add wave capture\n");
1038 g_free (s);
1039 return;
1041 QLIST_INSERT_HEAD (&capture_head, s, entries);
1044 void qmp_getfd(const char *fdname, Error **errp)
1046 Monitor *cur_mon = monitor_cur();
1047 mon_fd_t *monfd;
1048 int fd, tmp_fd;
1050 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1051 if (fd == -1) {
1052 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1053 return;
1056 if (qemu_isdigit(fdname[0])) {
1057 close(fd);
1058 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1059 "a name not starting with a digit");
1060 return;
1063 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1064 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1065 if (strcmp(monfd->name, fdname) != 0) {
1066 continue;
1069 tmp_fd = monfd->fd;
1070 monfd->fd = fd;
1071 /* Make sure close() is outside critical section */
1072 close(tmp_fd);
1073 return;
1076 monfd = g_malloc0(sizeof(mon_fd_t));
1077 monfd->name = g_strdup(fdname);
1078 monfd->fd = fd;
1080 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1083 void qmp_closefd(const char *fdname, Error **errp)
1085 Monitor *cur_mon = monitor_cur();
1086 mon_fd_t *monfd;
1087 int tmp_fd;
1089 qemu_mutex_lock(&cur_mon->mon_lock);
1090 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1091 if (strcmp(monfd->name, fdname) != 0) {
1092 continue;
1095 QLIST_REMOVE(monfd, next);
1096 tmp_fd = monfd->fd;
1097 g_free(monfd->name);
1098 g_free(monfd);
1099 qemu_mutex_unlock(&cur_mon->mon_lock);
1100 /* Make sure close() is outside critical section */
1101 close(tmp_fd);
1102 return;
1105 qemu_mutex_unlock(&cur_mon->mon_lock);
1106 error_setg(errp, "File descriptor named '%s' not found", fdname);
1109 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1111 mon_fd_t *monfd;
1113 QEMU_LOCK_GUARD(&mon->mon_lock);
1114 QLIST_FOREACH(monfd, &mon->fds, next) {
1115 int fd;
1117 if (strcmp(monfd->name, fdname) != 0) {
1118 continue;
1121 fd = monfd->fd;
1123 /* caller takes ownership of fd */
1124 QLIST_REMOVE(monfd, next);
1125 g_free(monfd->name);
1126 g_free(monfd);
1128 return fd;
1131 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1132 return -1;
1135 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1137 MonFdsetFd *mon_fdset_fd;
1138 MonFdsetFd *mon_fdset_fd_next;
1140 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1141 if ((mon_fdset_fd->removed ||
1142 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1143 runstate_is_running()) {
1144 close(mon_fdset_fd->fd);
1145 g_free(mon_fdset_fd->opaque);
1146 QLIST_REMOVE(mon_fdset_fd, next);
1147 g_free(mon_fdset_fd);
1151 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1152 QLIST_REMOVE(mon_fdset, next);
1153 g_free(mon_fdset);
1157 void monitor_fdsets_cleanup(void)
1159 MonFdset *mon_fdset;
1160 MonFdset *mon_fdset_next;
1162 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1163 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1164 monitor_fdset_cleanup(mon_fdset);
1168 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1169 const char *opaque, Error **errp)
1171 int fd;
1172 Monitor *mon = monitor_cur();
1173 AddfdInfo *fdinfo;
1175 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1176 if (fd == -1) {
1177 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1178 goto error;
1181 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1182 has_opaque, opaque, errp);
1183 if (fdinfo) {
1184 return fdinfo;
1187 error:
1188 if (fd != -1) {
1189 close(fd);
1191 return NULL;
1194 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1196 MonFdset *mon_fdset;
1197 MonFdsetFd *mon_fdset_fd;
1198 char fd_str[60];
1200 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1201 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1202 if (mon_fdset->id != fdset_id) {
1203 continue;
1205 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1206 if (has_fd) {
1207 if (mon_fdset_fd->fd != fd) {
1208 continue;
1210 mon_fdset_fd->removed = true;
1211 break;
1212 } else {
1213 mon_fdset_fd->removed = true;
1216 if (has_fd && !mon_fdset_fd) {
1217 goto error;
1219 monitor_fdset_cleanup(mon_fdset);
1220 return;
1223 error:
1224 if (has_fd) {
1225 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1226 fdset_id, fd);
1227 } else {
1228 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1230 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1233 FdsetInfoList *qmp_query_fdsets(Error **errp)
1235 MonFdset *mon_fdset;
1236 MonFdsetFd *mon_fdset_fd;
1237 FdsetInfoList *fdset_list = NULL;
1239 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1240 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1241 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1243 fdset_info->fdset_id = mon_fdset->id;
1245 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1246 FdsetFdInfo *fdsetfd_info;
1248 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1249 fdsetfd_info->fd = mon_fdset_fd->fd;
1250 if (mon_fdset_fd->opaque) {
1251 fdsetfd_info->has_opaque = true;
1252 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1253 } else {
1254 fdsetfd_info->has_opaque = false;
1257 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1260 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1263 return fdset_list;
1266 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1267 bool has_opaque, const char *opaque,
1268 Error **errp)
1270 MonFdset *mon_fdset = NULL;
1271 MonFdsetFd *mon_fdset_fd;
1272 AddfdInfo *fdinfo;
1274 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1275 if (has_fdset_id) {
1276 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1277 /* Break if match found or match impossible due to ordering by ID */
1278 if (fdset_id <= mon_fdset->id) {
1279 if (fdset_id < mon_fdset->id) {
1280 mon_fdset = NULL;
1282 break;
1287 if (mon_fdset == NULL) {
1288 int64_t fdset_id_prev = -1;
1289 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1291 if (has_fdset_id) {
1292 if (fdset_id < 0) {
1293 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1294 "a non-negative value");
1295 return NULL;
1297 /* Use specified fdset ID */
1298 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1299 mon_fdset_cur = mon_fdset;
1300 if (fdset_id < mon_fdset_cur->id) {
1301 break;
1304 } else {
1305 /* Use first available fdset ID */
1306 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1307 mon_fdset_cur = mon_fdset;
1308 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1309 fdset_id_prev = mon_fdset_cur->id;
1310 continue;
1312 break;
1316 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1317 if (has_fdset_id) {
1318 mon_fdset->id = fdset_id;
1319 } else {
1320 mon_fdset->id = fdset_id_prev + 1;
1323 /* The fdset list is ordered by fdset ID */
1324 if (!mon_fdset_cur) {
1325 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1326 } else if (mon_fdset->id < mon_fdset_cur->id) {
1327 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1328 } else {
1329 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1333 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1334 mon_fdset_fd->fd = fd;
1335 mon_fdset_fd->removed = false;
1336 if (has_opaque) {
1337 mon_fdset_fd->opaque = g_strdup(opaque);
1339 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1341 fdinfo = g_malloc0(sizeof(*fdinfo));
1342 fdinfo->fdset_id = mon_fdset->id;
1343 fdinfo->fd = mon_fdset_fd->fd;
1345 return fdinfo;
1348 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1350 #ifdef _WIN32
1351 return -ENOENT;
1352 #else
1353 MonFdset *mon_fdset;
1355 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1356 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1357 MonFdsetFd *mon_fdset_fd;
1358 MonFdsetFd *mon_fdset_fd_dup;
1359 int fd = -1;
1360 int dup_fd;
1361 int mon_fd_flags;
1363 if (mon_fdset->id != fdset_id) {
1364 continue;
1367 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1368 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1369 if (mon_fd_flags == -1) {
1370 return -1;
1373 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1374 fd = mon_fdset_fd->fd;
1375 break;
1379 if (fd == -1) {
1380 errno = EACCES;
1381 return -1;
1384 dup_fd = qemu_dup_flags(fd, flags);
1385 if (dup_fd == -1) {
1386 return -1;
1389 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1390 mon_fdset_fd_dup->fd = dup_fd;
1391 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1392 return dup_fd;
1395 errno = ENOENT;
1396 return -1;
1397 #endif
1400 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1402 MonFdset *mon_fdset;
1403 MonFdsetFd *mon_fdset_fd_dup;
1405 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1406 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1407 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1408 if (mon_fdset_fd_dup->fd == dup_fd) {
1409 if (remove) {
1410 QLIST_REMOVE(mon_fdset_fd_dup, next);
1411 g_free(mon_fdset_fd_dup);
1412 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1413 monitor_fdset_cleanup(mon_fdset);
1415 return -1;
1416 } else {
1417 return mon_fdset->id;
1423 return -1;
1426 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1428 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1431 void monitor_fdset_dup_fd_remove(int dup_fd)
1433 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1436 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1438 int fd;
1439 Error *local_err = NULL;
1441 if (!qemu_isdigit(fdname[0]) && mon) {
1442 fd = monitor_get_fd(mon, fdname, &local_err);
1443 } else {
1444 fd = qemu_parse_fd(fdname);
1445 if (fd == -1) {
1446 error_setg(&local_err, "Invalid file descriptor number '%s'",
1447 fdname);
1450 if (local_err) {
1451 error_propagate(errp, local_err);
1452 assert(fd == -1);
1453 } else {
1454 assert(fd != -1);
1457 return fd;
1460 /* Please update hmp-commands.hx when adding or changing commands */
1461 static HMPCommand hmp_info_cmds[] = {
1462 #include "hmp-commands-info.h"
1463 { NULL, NULL, },
1466 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1467 HMPCommand hmp_cmds[] = {
1468 #include "hmp-commands.h"
1469 { NULL, NULL, },
1473 * Set @pval to the value in the register identified by @name.
1474 * return 0 if OK, -1 if not found
1476 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1478 const MonitorDef *md = target_monitor_defs();
1479 CPUState *cs = mon_get_cpu(mon);
1480 void *ptr;
1481 uint64_t tmp = 0;
1482 int ret;
1484 if (cs == NULL || md == NULL) {
1485 return -1;
1488 for(; md->name != NULL; md++) {
1489 if (hmp_compare_cmd(name, md->name)) {
1490 if (md->get_value) {
1491 *pval = md->get_value(mon, md, md->offset);
1492 } else {
1493 CPUArchState *env = mon_get_cpu_env(mon);
1494 ptr = (uint8_t *)env + md->offset;
1495 switch(md->type) {
1496 case MD_I32:
1497 *pval = *(int32_t *)ptr;
1498 break;
1499 case MD_TLONG:
1500 *pval = *(target_long *)ptr;
1501 break;
1502 default:
1503 *pval = 0;
1504 break;
1507 return 0;
1511 ret = target_get_monitor_def(cs, name, &tmp);
1512 if (!ret) {
1513 *pval = (target_long) tmp;
1516 return ret;
1519 static void add_completion_option(ReadLineState *rs, const char *str,
1520 const char *option)
1522 if (!str || !option) {
1523 return;
1525 if (!strncmp(option, str, strlen(str))) {
1526 readline_add_completion(rs, option);
1530 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1532 size_t len;
1533 ChardevBackendInfoList *list, *start;
1535 if (nb_args != 2) {
1536 return;
1538 len = strlen(str);
1539 readline_set_completion_index(rs, len);
1541 start = list = qmp_query_chardev_backends(NULL);
1542 while (list) {
1543 const char *chr_name = list->value->name;
1545 if (!strncmp(chr_name, str, len)) {
1546 readline_add_completion(rs, chr_name);
1548 list = list->next;
1550 qapi_free_ChardevBackendInfoList(start);
1553 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1555 size_t len;
1556 int i;
1558 if (nb_args != 2) {
1559 return;
1561 len = strlen(str);
1562 readline_set_completion_index(rs, len);
1563 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1564 add_completion_option(rs, str, NetClientDriver_str(i));
1568 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1570 GSList *list, *elt;
1571 size_t len;
1573 if (nb_args != 2) {
1574 return;
1577 len = strlen(str);
1578 readline_set_completion_index(rs, len);
1579 list = elt = object_class_get_list(TYPE_DEVICE, false);
1580 while (elt) {
1581 const char *name;
1582 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1583 TYPE_DEVICE);
1584 name = object_class_get_name(OBJECT_CLASS(dc));
1586 if (dc->user_creatable
1587 && !strncmp(name, str, len)) {
1588 readline_add_completion(rs, name);
1590 elt = elt->next;
1592 g_slist_free(list);
1595 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1597 GSList *list, *elt;
1598 size_t len;
1600 if (nb_args != 2) {
1601 return;
1604 len = strlen(str);
1605 readline_set_completion_index(rs, len);
1606 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1607 while (elt) {
1608 const char *name;
1610 name = object_class_get_name(OBJECT_CLASS(elt->data));
1611 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1612 readline_add_completion(rs, name);
1614 elt = elt->next;
1616 g_slist_free(list);
1619 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1621 GSList **list = opaque;
1622 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1624 if (dev == NULL) {
1625 return 0;
1628 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1629 *list = g_slist_append(*list, dev);
1632 return 0;
1635 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1637 GSList *list = NULL;
1639 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1641 return list;
1644 static void peripheral_device_del_completion(ReadLineState *rs,
1645 const char *str, size_t len)
1647 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1648 GSList *list, *item;
1650 list = qdev_build_hotpluggable_device_list(peripheral);
1651 if (!list) {
1652 return;
1655 for (item = list; item; item = g_slist_next(item)) {
1656 DeviceState *dev = item->data;
1658 if (dev->id && !strncmp(str, dev->id, len)) {
1659 readline_add_completion(rs, dev->id);
1663 g_slist_free(list);
1666 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1668 size_t len;
1669 ChardevInfoList *list, *start;
1671 if (nb_args != 2) {
1672 return;
1674 len = strlen(str);
1675 readline_set_completion_index(rs, len);
1677 start = list = qmp_query_chardev(NULL);
1678 while (list) {
1679 ChardevInfo *chr = list->value;
1681 if (!strncmp(chr->label, str, len)) {
1682 readline_add_completion(rs, chr->label);
1684 list = list->next;
1686 qapi_free_ChardevInfoList(start);
1689 static void ringbuf_completion(ReadLineState *rs, const char *str)
1691 size_t len;
1692 ChardevInfoList *list, *start;
1694 len = strlen(str);
1695 readline_set_completion_index(rs, len);
1697 start = list = qmp_query_chardev(NULL);
1698 while (list) {
1699 ChardevInfo *chr_info = list->value;
1701 if (!strncmp(chr_info->label, str, len)) {
1702 Chardev *chr = qemu_chr_find(chr_info->label);
1703 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1704 readline_add_completion(rs, chr_info->label);
1707 list = list->next;
1709 qapi_free_ChardevInfoList(start);
1712 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1714 if (nb_args != 2) {
1715 return;
1717 ringbuf_completion(rs, str);
1720 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1722 size_t len;
1724 if (nb_args != 2) {
1725 return;
1728 len = strlen(str);
1729 readline_set_completion_index(rs, len);
1730 peripheral_device_del_completion(rs, str, len);
1733 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1735 ObjectPropertyInfoList *list, *start;
1736 size_t len;
1738 if (nb_args != 2) {
1739 return;
1741 len = strlen(str);
1742 readline_set_completion_index(rs, len);
1744 start = list = qmp_qom_list("/objects", NULL);
1745 while (list) {
1746 ObjectPropertyInfo *info = list->value;
1748 if (!strncmp(info->type, "child<", 5)
1749 && !strncmp(info->name, str, len)) {
1750 readline_add_completion(rs, info->name);
1752 list = list->next;
1754 qapi_free_ObjectPropertyInfoList(start);
1757 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1759 int i;
1760 char *sep;
1761 size_t len;
1763 if (nb_args != 2) {
1764 return;
1766 sep = strrchr(str, '-');
1767 if (sep) {
1768 str = sep + 1;
1770 len = strlen(str);
1771 readline_set_completion_index(rs, len);
1772 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1773 if (!strncmp(str, QKeyCode_str(i), len)) {
1774 readline_add_completion(rs, QKeyCode_str(i));
1779 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1781 size_t len;
1783 len = strlen(str);
1784 readline_set_completion_index(rs, len);
1785 if (nb_args == 2) {
1786 NetClientState *ncs[MAX_QUEUE_NUM];
1787 int count, i;
1788 count = qemu_find_net_clients_except(NULL, ncs,
1789 NET_CLIENT_DRIVER_NONE,
1790 MAX_QUEUE_NUM);
1791 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1792 const char *name = ncs[i]->name;
1793 if (!strncmp(str, name, len)) {
1794 readline_add_completion(rs, name);
1797 } else if (nb_args == 3) {
1798 add_completion_option(rs, str, "on");
1799 add_completion_option(rs, str, "off");
1803 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1805 int len, count, i;
1806 NetClientState *ncs[MAX_QUEUE_NUM];
1808 if (nb_args != 2) {
1809 return;
1812 len = strlen(str);
1813 readline_set_completion_index(rs, len);
1814 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1815 MAX_QUEUE_NUM);
1816 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1817 const char *name = ncs[i]->name;
1818 if (strncmp(str, name, len)) {
1819 continue;
1821 if (ncs[i]->is_netdev) {
1822 readline_add_completion(rs, name);
1827 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1829 size_t len;
1831 len = strlen(str);
1832 readline_set_completion_index(rs, len);
1833 if (nb_args == 2) {
1834 TraceEventIter iter;
1835 TraceEvent *ev;
1836 char *pattern = g_strdup_printf("%s*", str);
1837 trace_event_iter_init(&iter, pattern);
1838 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1839 readline_add_completion(rs, trace_event_get_name(ev));
1841 g_free(pattern);
1845 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1847 size_t len;
1849 len = strlen(str);
1850 readline_set_completion_index(rs, len);
1851 if (nb_args == 2) {
1852 TraceEventIter iter;
1853 TraceEvent *ev;
1854 char *pattern = g_strdup_printf("%s*", str);
1855 trace_event_iter_init(&iter, pattern);
1856 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1857 readline_add_completion(rs, trace_event_get_name(ev));
1859 g_free(pattern);
1860 } else if (nb_args == 3) {
1861 add_completion_option(rs, str, "on");
1862 add_completion_option(rs, str, "off");
1866 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1868 int i;
1870 if (nb_args != 2) {
1871 return;
1873 readline_set_completion_index(rs, strlen(str));
1874 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1875 add_completion_option(rs, str, WatchdogAction_str(i));
1879 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1880 const char *str)
1882 size_t len;
1884 len = strlen(str);
1885 readline_set_completion_index(rs, len);
1886 if (nb_args == 2) {
1887 int i;
1888 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1889 const char *name = MigrationCapability_str(i);
1890 if (!strncmp(str, name, len)) {
1891 readline_add_completion(rs, name);
1894 } else if (nb_args == 3) {
1895 add_completion_option(rs, str, "on");
1896 add_completion_option(rs, str, "off");
1900 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1901 const char *str)
1903 size_t len;
1905 len = strlen(str);
1906 readline_set_completion_index(rs, len);
1907 if (nb_args == 2) {
1908 int i;
1909 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1910 const char *name = MigrationParameter_str(i);
1911 if (!strncmp(str, name, len)) {
1912 readline_add_completion(rs, name);
1918 static void vm_completion(ReadLineState *rs, const char *str)
1920 size_t len;
1921 BlockDriverState *bs;
1922 BdrvNextIterator it;
1924 len = strlen(str);
1925 readline_set_completion_index(rs, len);
1927 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1928 SnapshotInfoList *snapshots, *snapshot;
1929 AioContext *ctx = bdrv_get_aio_context(bs);
1930 bool ok = false;
1932 aio_context_acquire(ctx);
1933 if (bdrv_can_snapshot(bs)) {
1934 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1936 aio_context_release(ctx);
1937 if (!ok) {
1938 continue;
1941 snapshot = snapshots;
1942 while (snapshot) {
1943 char *completion = snapshot->value->name;
1944 if (!strncmp(str, completion, len)) {
1945 readline_add_completion(rs, completion);
1947 completion = snapshot->value->id;
1948 if (!strncmp(str, completion, len)) {
1949 readline_add_completion(rs, completion);
1951 snapshot = snapshot->next;
1953 qapi_free_SnapshotInfoList(snapshots);
1958 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1960 if (nb_args == 2) {
1961 vm_completion(rs, str);
1965 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1967 if (nb_args == 2) {
1968 vm_completion(rs, str);
1972 static int
1973 compare_mon_cmd(const void *a, const void *b)
1975 return strcmp(((const HMPCommand *)a)->name,
1976 ((const HMPCommand *)b)->name);
1979 static void sortcmdlist(void)
1981 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1982 sizeof(*hmp_cmds),
1983 compare_mon_cmd);
1984 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1985 sizeof(*hmp_info_cmds),
1986 compare_mon_cmd);
1989 void monitor_init_globals(void)
1991 monitor_init_globals_core();
1992 monitor_init_qmp_commands();
1993 sortcmdlist();
1994 qemu_mutex_init(&mon_fdsets_lock);