Do not include cpu.h if it's not really necessary
[qemu/ar7.git] / monitor / misc.c
blobf3a393ea590b5a05e1c370c65260b77cafa1f249
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "hw/usb.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
39 #include "ui/input.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
44 #include "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "authz/list.h"
47 #include "qapi/util.h"
48 #include "sysemu/blockdev.h"
49 #include "sysemu/sysemu.h"
50 #include "sysemu/tcg.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qdict.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qstring.h"
55 #include "qom/object_interfaces.h"
56 #include "trace/control.h"
57 #include "monitor/hmp-target.h"
58 #include "monitor/hmp.h"
59 #ifdef CONFIG_TRACE_SIMPLE
60 #include "trace/simple.h"
61 #endif
62 #include "exec/memory.h"
63 #include "exec/exec-all.h"
64 #include "qemu/option.h"
65 #include "qemu/thread.h"
66 #include "block/qapi.h"
67 #include "block/block-hmp-cmds.h"
68 #include "qapi/qapi-commands-char.h"
69 #include "qapi/qapi-commands-control.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-trace.h"
74 #include "qapi/qapi-init-commands.h"
75 #include "qapi/error.h"
76 #include "qapi/qmp-event.h"
77 #include "sysemu/cpus.h"
78 #include "qemu/cutils.h"
80 #if defined(TARGET_S390X)
81 #include "hw/s390x/storage-keys.h"
82 #include "hw/s390x/storage-attributes.h"
83 #endif
85 /* file descriptors passed via SCM_RIGHTS */
86 typedef struct mon_fd_t mon_fd_t;
87 struct mon_fd_t {
88 char *name;
89 int fd;
90 QLIST_ENTRY(mon_fd_t) next;
93 /* file descriptor associated with a file descriptor set */
94 typedef struct MonFdsetFd MonFdsetFd;
95 struct MonFdsetFd {
96 int fd;
97 bool removed;
98 char *opaque;
99 QLIST_ENTRY(MonFdsetFd) next;
102 /* file descriptor set containing fds passed via SCM_RIGHTS */
103 typedef struct MonFdset MonFdset;
104 struct MonFdset {
105 int64_t id;
106 QLIST_HEAD(, MonFdsetFd) fds;
107 QLIST_HEAD(, MonFdsetFd) dup_fds;
108 QLIST_ENTRY(MonFdset) next;
111 /* Protects mon_fdsets */
112 static QemuMutex mon_fdsets_lock;
113 static QLIST_HEAD(, MonFdset) mon_fdsets;
115 static HMPCommand hmp_info_cmds[];
117 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
118 int64_t cpu_index, Error **errp)
120 char *output = NULL;
121 MonitorHMP hmp = {};
123 monitor_data_init(&hmp.common, false, true, false);
125 if (has_cpu_index) {
126 int ret = monitor_set_cpu(&hmp.common, cpu_index);
127 if (ret < 0) {
128 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
129 "a CPU number");
130 goto out;
134 handle_hmp_command(&hmp, command_line);
136 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
137 output = g_strdup(hmp.common.outbuf->str);
140 out:
141 monitor_data_destroy(&hmp.common);
142 return output;
146 * Is @name in the '|' separated list of names @list?
148 int hmp_compare_cmd(const char *name, const char *list)
150 const char *p, *pstart;
151 int len;
152 len = strlen(name);
153 p = list;
154 for (;;) {
155 pstart = p;
156 p = qemu_strchrnul(p, '|');
157 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
158 return 1;
160 if (*p == '\0') {
161 break;
163 p++;
165 return 0;
168 static void do_help_cmd(Monitor *mon, const QDict *qdict)
170 help_cmd(mon, qdict_get_try_str(qdict, "name"));
173 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
175 const char *tp_name = qdict_get_str(qdict, "name");
176 bool new_state = qdict_get_bool(qdict, "option");
177 bool has_vcpu = qdict_haskey(qdict, "vcpu");
178 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
179 Error *local_err = NULL;
181 if (vcpu < 0) {
182 monitor_printf(mon, "argument vcpu must be positive");
183 return;
186 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
187 if (local_err) {
188 error_report_err(local_err);
192 #ifdef CONFIG_TRACE_SIMPLE
193 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
195 const char *op = qdict_get_try_str(qdict, "op");
196 const char *arg = qdict_get_try_str(qdict, "arg");
198 if (!op) {
199 st_print_trace_file_status();
200 } else if (!strcmp(op, "on")) {
201 st_set_trace_file_enabled(true);
202 } else if (!strcmp(op, "off")) {
203 st_set_trace_file_enabled(false);
204 } else if (!strcmp(op, "flush")) {
205 st_flush_trace_buffer();
206 } else if (!strcmp(op, "set")) {
207 if (arg) {
208 st_set_trace_file(arg);
210 } else {
211 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
212 help_cmd(mon, "trace-file");
215 #endif
217 static void hmp_info_help(Monitor *mon, const QDict *qdict)
219 help_cmd(mon, "info");
222 static void monitor_init_qmp_commands(void)
225 * Two command lists:
226 * - qmp_commands contains all QMP commands
227 * - qmp_cap_negotiation_commands contains just
228 * "qmp_capabilities", to enforce capability negotiation
231 qmp_init_marshal(&qmp_commands);
233 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
234 QCO_NO_OPTIONS);
236 QTAILQ_INIT(&qmp_cap_negotiation_commands);
237 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
238 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
241 /* Set the current CPU defined by the user. Callers must hold BQL. */
242 int monitor_set_cpu(Monitor *mon, int cpu_index)
244 CPUState *cpu;
246 cpu = qemu_get_cpu(cpu_index);
247 if (cpu == NULL) {
248 return -1;
250 g_free(mon->mon_cpu_path);
251 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
252 return 0;
255 /* Callers must hold BQL. */
256 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
258 CPUState *cpu = NULL;
260 if (mon->mon_cpu_path) {
261 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
262 TYPE_CPU, NULL);
263 if (!cpu) {
264 g_free(mon->mon_cpu_path);
265 mon->mon_cpu_path = NULL;
268 if (!mon->mon_cpu_path) {
269 if (!first_cpu) {
270 return NULL;
272 monitor_set_cpu(mon, first_cpu->cpu_index);
273 cpu = first_cpu;
275 assert(cpu != NULL);
276 if (synchronize) {
277 cpu_synchronize_state(cpu);
279 return cpu;
282 CPUState *mon_get_cpu(Monitor *mon)
284 return mon_get_cpu_sync(mon, true);
287 CPUArchState *mon_get_cpu_env(Monitor *mon)
289 CPUState *cs = mon_get_cpu(mon);
291 return cs ? cs->env_ptr : NULL;
294 int monitor_get_cpu_index(Monitor *mon)
296 CPUState *cs = mon_get_cpu_sync(mon, false);
298 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
301 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
303 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
304 CPUState *cs;
306 if (all_cpus) {
307 CPU_FOREACH(cs) {
308 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
309 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
311 } else {
312 cs = mon_get_cpu(mon);
314 if (!cs) {
315 monitor_printf(mon, "No CPU available\n");
316 return;
319 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
323 #ifdef CONFIG_TCG
324 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
326 if (!tcg_enabled()) {
327 error_report("JIT information is only available with accel=tcg");
328 return;
331 dump_exec_info();
332 dump_drift_info();
335 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
337 dump_opcount_info();
339 #endif
341 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
343 int64_t max = qdict_get_try_int(qdict, "max", 10);
344 bool mean = qdict_get_try_bool(qdict, "mean", false);
345 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
346 enum QSPSortBy sort_by;
348 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
349 qsp_report(max, sort_by, coalesce);
352 static void hmp_info_history(Monitor *mon, const QDict *qdict)
354 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
355 int i;
356 const char *str;
358 if (!hmp_mon->rs) {
359 return;
361 i = 0;
362 for(;;) {
363 str = readline_get_history(hmp_mon->rs, i);
364 if (!str) {
365 break;
367 monitor_printf(mon, "%d: '%s'\n", i, str);
368 i++;
372 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
374 CPUState *cs = mon_get_cpu(mon);
376 if (!cs) {
377 monitor_printf(mon, "No CPU available\n");
378 return;
380 cpu_dump_statistics(cs, 0);
383 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
385 const char *name = qdict_get_try_str(qdict, "name");
386 bool has_vcpu = qdict_haskey(qdict, "vcpu");
387 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
388 TraceEventInfoList *events;
389 TraceEventInfoList *elem;
390 Error *local_err = NULL;
392 if (name == NULL) {
393 name = "*";
395 if (vcpu < 0) {
396 monitor_printf(mon, "argument vcpu must be positive");
397 return;
400 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
401 if (local_err) {
402 error_report_err(local_err);
403 return;
406 for (elem = events; elem != NULL; elem = elem->next) {
407 monitor_printf(mon, "%s : state %u\n",
408 elem->value->name,
409 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
411 qapi_free_TraceEventInfoList(events);
414 void qmp_client_migrate_info(const char *protocol, const char *hostname,
415 bool has_port, int64_t port,
416 bool has_tls_port, int64_t tls_port,
417 bool has_cert_subject, const char *cert_subject,
418 Error **errp)
420 if (strcmp(protocol, "spice") == 0) {
421 if (!qemu_using_spice(errp)) {
422 return;
425 if (!has_port && !has_tls_port) {
426 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
427 return;
430 if (qemu_spice.migrate_info(hostname,
431 has_port ? port : -1,
432 has_tls_port ? tls_port : -1,
433 cert_subject)) {
434 error_setg(errp, "Could not set up display for migration");
435 return;
437 return;
440 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
443 static void hmp_logfile(Monitor *mon, const QDict *qdict)
445 Error *err = NULL;
447 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
448 if (err) {
449 error_report_err(err);
453 static void hmp_log(Monitor *mon, const QDict *qdict)
455 int mask;
456 const char *items = qdict_get_str(qdict, "items");
458 if (!strcmp(items, "none")) {
459 mask = 0;
460 } else {
461 mask = qemu_str_to_log_mask(items);
462 if (!mask) {
463 help_cmd(mon, "log");
464 return;
467 qemu_set_log(mask);
470 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
472 const char *option = qdict_get_try_str(qdict, "option");
473 if (!option || !strcmp(option, "on")) {
474 singlestep = 1;
475 } else if (!strcmp(option, "off")) {
476 singlestep = 0;
477 } else {
478 monitor_printf(mon, "unexpected option %s\n", option);
482 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
484 const char *device = qdict_get_try_str(qdict, "device");
485 if (!device) {
486 device = "tcp::" DEFAULT_GDBSTUB_PORT;
489 if (gdbserver_start(device) < 0) {
490 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
491 device);
492 } else if (strcmp(device, "none") == 0) {
493 monitor_printf(mon, "Disabled gdbserver\n");
494 } else {
495 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
496 device);
500 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
502 const char *action = qdict_get_str(qdict, "action");
503 if (select_watchdog_action(action) == -1) {
504 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
508 static void monitor_printc(Monitor *mon, int c)
510 monitor_printf(mon, "'");
511 switch(c) {
512 case '\'':
513 monitor_printf(mon, "\\'");
514 break;
515 case '\\':
516 monitor_printf(mon, "\\\\");
517 break;
518 case '\n':
519 monitor_printf(mon, "\\n");
520 break;
521 case '\r':
522 monitor_printf(mon, "\\r");
523 break;
524 default:
525 if (c >= 32 && c <= 126) {
526 monitor_printf(mon, "%c", c);
527 } else {
528 monitor_printf(mon, "\\x%02x", c);
530 break;
532 monitor_printf(mon, "'");
535 static void memory_dump(Monitor *mon, int count, int format, int wsize,
536 hwaddr addr, int is_physical)
538 int l, line_size, i, max_digits, len;
539 uint8_t buf[16];
540 uint64_t v;
541 CPUState *cs = mon_get_cpu(mon);
543 if (!cs && (format == 'i' || !is_physical)) {
544 monitor_printf(mon, "Can not dump without CPU\n");
545 return;
548 if (format == 'i') {
549 monitor_disas(mon, cs, addr, count, is_physical);
550 return;
553 len = wsize * count;
554 if (wsize == 1) {
555 line_size = 8;
556 } else {
557 line_size = 16;
559 max_digits = 0;
561 switch(format) {
562 case 'o':
563 max_digits = DIV_ROUND_UP(wsize * 8, 3);
564 break;
565 default:
566 case 'x':
567 max_digits = (wsize * 8) / 4;
568 break;
569 case 'u':
570 case 'd':
571 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
572 break;
573 case 'c':
574 wsize = 1;
575 break;
578 while (len > 0) {
579 if (is_physical) {
580 monitor_printf(mon, TARGET_FMT_plx ":", addr);
581 } else {
582 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
584 l = len;
585 if (l > line_size)
586 l = line_size;
587 if (is_physical) {
588 AddressSpace *as = cs ? cs->as : &address_space_memory;
589 MemTxResult r = address_space_read(as, addr,
590 MEMTXATTRS_UNSPECIFIED, buf, l);
591 if (r != MEMTX_OK) {
592 monitor_printf(mon, " Cannot access memory\n");
593 break;
595 } else {
596 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
597 monitor_printf(mon, " Cannot access memory\n");
598 break;
601 i = 0;
602 while (i < l) {
603 switch(wsize) {
604 default:
605 case 1:
606 v = ldub_p(buf + i);
607 break;
608 case 2:
609 v = lduw_p(buf + i);
610 break;
611 case 4:
612 v = (uint32_t)ldl_p(buf + i);
613 break;
614 case 8:
615 v = ldq_p(buf + i);
616 break;
618 monitor_printf(mon, " ");
619 switch(format) {
620 case 'o':
621 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
622 break;
623 case 'x':
624 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
625 break;
626 case 'u':
627 monitor_printf(mon, "%*" PRIu64, max_digits, v);
628 break;
629 case 'd':
630 monitor_printf(mon, "%*" PRId64, max_digits, v);
631 break;
632 case 'c':
633 monitor_printc(mon, v);
634 break;
636 i += wsize;
638 monitor_printf(mon, "\n");
639 addr += l;
640 len -= l;
644 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
646 int count = qdict_get_int(qdict, "count");
647 int format = qdict_get_int(qdict, "format");
648 int size = qdict_get_int(qdict, "size");
649 target_long addr = qdict_get_int(qdict, "addr");
651 memory_dump(mon, count, format, size, addr, 0);
654 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
656 int count = qdict_get_int(qdict, "count");
657 int format = qdict_get_int(qdict, "format");
658 int size = qdict_get_int(qdict, "size");
659 hwaddr addr = qdict_get_int(qdict, "addr");
661 memory_dump(mon, count, format, size, addr, 1);
664 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
666 Int128 gpa_region_size;
667 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
668 addr, size);
670 if (!mrs.mr) {
671 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
672 return NULL;
675 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
676 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
677 memory_region_unref(mrs.mr);
678 return NULL;
681 gpa_region_size = int128_make64(size);
682 if (int128_lt(mrs.size, gpa_region_size)) {
683 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
684 " exceeded.", addr);
685 memory_region_unref(mrs.mr);
686 return NULL;
689 *p_mr = mrs.mr;
690 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
693 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
695 hwaddr addr = qdict_get_int(qdict, "addr");
696 Error *local_err = NULL;
697 MemoryRegion *mr = NULL;
698 void *ptr;
700 ptr = gpa2hva(&mr, addr, 1, &local_err);
701 if (local_err) {
702 error_report_err(local_err);
703 return;
706 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
707 " (%s) is %p\n",
708 addr, mr->name, ptr);
710 memory_region_unref(mr);
713 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
715 target_ulong addr = qdict_get_int(qdict, "addr");
716 MemTxAttrs attrs;
717 CPUState *cs = mon_get_cpu(mon);
718 hwaddr gpa;
720 if (!cs) {
721 monitor_printf(mon, "No cpu\n");
722 return;
725 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
726 if (gpa == -1) {
727 monitor_printf(mon, "Unmapped\n");
728 } else {
729 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
730 gpa + (addr & ~TARGET_PAGE_MASK));
734 #ifdef CONFIG_LINUX
735 static uint64_t vtop(void *ptr, Error **errp)
737 uint64_t pinfo;
738 uint64_t ret = -1;
739 uintptr_t addr = (uintptr_t) ptr;
740 uintptr_t pagesize = qemu_real_host_page_size;
741 off_t offset = addr / pagesize * sizeof(pinfo);
742 int fd;
744 fd = open("/proc/self/pagemap", O_RDONLY);
745 if (fd == -1) {
746 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
747 return -1;
750 /* Force copy-on-write if necessary. */
751 qatomic_add((uint8_t *)ptr, 0);
753 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
754 error_setg_errno(errp, errno, "Cannot read pagemap");
755 goto out;
757 if ((pinfo & (1ull << 63)) == 0) {
758 error_setg(errp, "Page not present");
759 goto out;
761 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
763 out:
764 close(fd);
765 return ret;
768 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
770 hwaddr addr = qdict_get_int(qdict, "addr");
771 Error *local_err = NULL;
772 MemoryRegion *mr = NULL;
773 void *ptr;
774 uint64_t physaddr;
776 ptr = gpa2hva(&mr, addr, 1, &local_err);
777 if (local_err) {
778 error_report_err(local_err);
779 return;
782 physaddr = vtop(ptr, &local_err);
783 if (local_err) {
784 error_report_err(local_err);
785 } else {
786 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
787 " (%s) is 0x%" PRIx64 "\n",
788 addr, mr->name, (uint64_t) physaddr);
791 memory_region_unref(mr);
793 #endif
795 static void do_print(Monitor *mon, const QDict *qdict)
797 int format = qdict_get_int(qdict, "format");
798 hwaddr val = qdict_get_int(qdict, "val");
800 switch(format) {
801 case 'o':
802 monitor_printf(mon, "%#" HWADDR_PRIo, val);
803 break;
804 case 'x':
805 monitor_printf(mon, "%#" HWADDR_PRIx, val);
806 break;
807 case 'u':
808 monitor_printf(mon, "%" HWADDR_PRIu, val);
809 break;
810 default:
811 case 'd':
812 monitor_printf(mon, "%" HWADDR_PRId, val);
813 break;
814 case 'c':
815 monitor_printc(mon, val);
816 break;
818 monitor_printf(mon, "\n");
821 static void hmp_sum(Monitor *mon, const QDict *qdict)
823 uint32_t addr;
824 uint16_t sum;
825 uint32_t start = qdict_get_int(qdict, "start");
826 uint32_t size = qdict_get_int(qdict, "size");
828 sum = 0;
829 for(addr = start; addr < (start + size); addr++) {
830 uint8_t val = address_space_ldub(&address_space_memory, addr,
831 MEMTXATTRS_UNSPECIFIED, NULL);
832 /* BSD sum algorithm ('sum' Unix command) */
833 sum = (sum >> 1) | (sum << 15);
834 sum += val;
836 monitor_printf(mon, "%05d\n", sum);
839 static int mouse_button_state;
841 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
843 int dx, dy, dz, button;
844 const char *dx_str = qdict_get_str(qdict, "dx_str");
845 const char *dy_str = qdict_get_str(qdict, "dy_str");
846 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
848 dx = strtol(dx_str, NULL, 0);
849 dy = strtol(dy_str, NULL, 0);
850 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
851 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
853 if (dz_str) {
854 dz = strtol(dz_str, NULL, 0);
855 if (dz != 0) {
856 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
857 qemu_input_queue_btn(NULL, button, true);
858 qemu_input_event_sync();
859 qemu_input_queue_btn(NULL, button, false);
862 qemu_input_event_sync();
865 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
867 static uint32_t bmap[INPUT_BUTTON__MAX] = {
868 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
869 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
870 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
872 int button_state = qdict_get_int(qdict, "button_state");
874 if (mouse_button_state == button_state) {
875 return;
877 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
878 qemu_input_event_sync();
879 mouse_button_state = button_state;
882 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
884 int size = qdict_get_int(qdict, "size");
885 int addr = qdict_get_int(qdict, "addr");
886 int has_index = qdict_haskey(qdict, "index");
887 uint32_t val;
888 int suffix;
890 if (has_index) {
891 int index = qdict_get_int(qdict, "index");
892 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
893 addr++;
895 addr &= 0xffff;
897 switch(size) {
898 default:
899 case 1:
900 val = cpu_inb(addr);
901 suffix = 'b';
902 break;
903 case 2:
904 val = cpu_inw(addr);
905 suffix = 'w';
906 break;
907 case 4:
908 val = cpu_inl(addr);
909 suffix = 'l';
910 break;
912 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
913 suffix, addr, size * 2, val);
916 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
918 int size = qdict_get_int(qdict, "size");
919 int addr = qdict_get_int(qdict, "addr");
920 int val = qdict_get_int(qdict, "val");
922 addr &= IOPORTS_MASK;
924 switch (size) {
925 default:
926 case 1:
927 cpu_outb(addr, val);
928 break;
929 case 2:
930 cpu_outw(addr, val);
931 break;
932 case 4:
933 cpu_outl(addr, val);
934 break;
938 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
940 Error *local_err = NULL;
941 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
943 qemu_boot_set(bootdevice, &local_err);
944 if (local_err) {
945 error_report_err(local_err);
946 } else {
947 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
951 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
953 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
954 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
955 bool owner = qdict_get_try_bool(qdict, "owner", false);
956 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
958 mtree_info(flatview, dispatch_tree, owner, disabled);
961 #ifdef CONFIG_PROFILER
963 int64_t dev_time;
965 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
967 static int64_t last_cpu_exec_time;
968 int64_t cpu_exec_time;
969 int64_t delta;
971 cpu_exec_time = tcg_cpu_exec_time();
972 delta = cpu_exec_time - last_cpu_exec_time;
974 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
975 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
976 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
977 delta, delta / (double)NANOSECONDS_PER_SECOND);
978 last_cpu_exec_time = cpu_exec_time;
979 dev_time = 0;
981 #else
982 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
984 monitor_printf(mon, "Internal profiler not compiled\n");
986 #endif
988 /* Capture support */
989 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
991 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
993 int i;
994 CaptureState *s;
996 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
997 monitor_printf(mon, "[%d]: ", i);
998 s->ops.info (s->opaque);
1002 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1004 int i;
1005 int n = qdict_get_int(qdict, "n");
1006 CaptureState *s;
1008 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1009 if (i == n) {
1010 s->ops.destroy (s->opaque);
1011 QLIST_REMOVE (s, entries);
1012 g_free (s);
1013 return;
1018 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1020 const char *path = qdict_get_str(qdict, "path");
1021 int freq = qdict_get_try_int(qdict, "freq", 44100);
1022 int bits = qdict_get_try_int(qdict, "bits", 16);
1023 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1024 const char *audiodev = qdict_get_str(qdict, "audiodev");
1025 CaptureState *s;
1026 AudioState *as = audio_state_by_name(audiodev);
1028 if (!as) {
1029 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1030 return;
1033 s = g_malloc0 (sizeof (*s));
1035 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1036 monitor_printf(mon, "Failed to add wave capture\n");
1037 g_free (s);
1038 return;
1040 QLIST_INSERT_HEAD (&capture_head, s, entries);
1043 void qmp_getfd(const char *fdname, Error **errp)
1045 Monitor *cur_mon = monitor_cur();
1046 mon_fd_t *monfd;
1047 int fd, tmp_fd;
1049 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1050 if (fd == -1) {
1051 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1052 return;
1055 if (qemu_isdigit(fdname[0])) {
1056 close(fd);
1057 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1058 "a name not starting with a digit");
1059 return;
1062 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1063 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1064 if (strcmp(monfd->name, fdname) != 0) {
1065 continue;
1068 tmp_fd = monfd->fd;
1069 monfd->fd = fd;
1070 /* Make sure close() is outside critical section */
1071 close(tmp_fd);
1072 return;
1075 monfd = g_malloc0(sizeof(mon_fd_t));
1076 monfd->name = g_strdup(fdname);
1077 monfd->fd = fd;
1079 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1082 void qmp_closefd(const char *fdname, Error **errp)
1084 Monitor *cur_mon = monitor_cur();
1085 mon_fd_t *monfd;
1086 int tmp_fd;
1088 qemu_mutex_lock(&cur_mon->mon_lock);
1089 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1090 if (strcmp(monfd->name, fdname) != 0) {
1091 continue;
1094 QLIST_REMOVE(monfd, next);
1095 tmp_fd = monfd->fd;
1096 g_free(monfd->name);
1097 g_free(monfd);
1098 qemu_mutex_unlock(&cur_mon->mon_lock);
1099 /* Make sure close() is outside critical section */
1100 close(tmp_fd);
1101 return;
1104 qemu_mutex_unlock(&cur_mon->mon_lock);
1105 error_setg(errp, "File descriptor named '%s' not found", fdname);
1108 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1110 mon_fd_t *monfd;
1112 QEMU_LOCK_GUARD(&mon->mon_lock);
1113 QLIST_FOREACH(monfd, &mon->fds, next) {
1114 int fd;
1116 if (strcmp(monfd->name, fdname) != 0) {
1117 continue;
1120 fd = monfd->fd;
1122 /* caller takes ownership of fd */
1123 QLIST_REMOVE(monfd, next);
1124 g_free(monfd->name);
1125 g_free(monfd);
1127 return fd;
1130 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1131 return -1;
1134 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1136 MonFdsetFd *mon_fdset_fd;
1137 MonFdsetFd *mon_fdset_fd_next;
1139 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1140 if ((mon_fdset_fd->removed ||
1141 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1142 runstate_is_running()) {
1143 close(mon_fdset_fd->fd);
1144 g_free(mon_fdset_fd->opaque);
1145 QLIST_REMOVE(mon_fdset_fd, next);
1146 g_free(mon_fdset_fd);
1150 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1151 QLIST_REMOVE(mon_fdset, next);
1152 g_free(mon_fdset);
1156 void monitor_fdsets_cleanup(void)
1158 MonFdset *mon_fdset;
1159 MonFdset *mon_fdset_next;
1161 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1162 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1163 monitor_fdset_cleanup(mon_fdset);
1167 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1168 const char *opaque, Error **errp)
1170 int fd;
1171 Monitor *mon = monitor_cur();
1172 AddfdInfo *fdinfo;
1174 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1175 if (fd == -1) {
1176 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1177 goto error;
1180 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1181 has_opaque, opaque, errp);
1182 if (fdinfo) {
1183 return fdinfo;
1186 error:
1187 if (fd != -1) {
1188 close(fd);
1190 return NULL;
1193 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1195 MonFdset *mon_fdset;
1196 MonFdsetFd *mon_fdset_fd;
1197 char fd_str[60];
1199 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1200 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1201 if (mon_fdset->id != fdset_id) {
1202 continue;
1204 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1205 if (has_fd) {
1206 if (mon_fdset_fd->fd != fd) {
1207 continue;
1209 mon_fdset_fd->removed = true;
1210 break;
1211 } else {
1212 mon_fdset_fd->removed = true;
1215 if (has_fd && !mon_fdset_fd) {
1216 goto error;
1218 monitor_fdset_cleanup(mon_fdset);
1219 return;
1222 error:
1223 if (has_fd) {
1224 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1225 fdset_id, fd);
1226 } else {
1227 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1229 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1232 FdsetInfoList *qmp_query_fdsets(Error **errp)
1234 MonFdset *mon_fdset;
1235 MonFdsetFd *mon_fdset_fd;
1236 FdsetInfoList *fdset_list = NULL;
1238 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1239 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1240 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1242 fdset_info->fdset_id = mon_fdset->id;
1244 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1245 FdsetFdInfo *fdsetfd_info;
1247 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1248 fdsetfd_info->fd = mon_fdset_fd->fd;
1249 if (mon_fdset_fd->opaque) {
1250 fdsetfd_info->has_opaque = true;
1251 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1252 } else {
1253 fdsetfd_info->has_opaque = false;
1256 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1259 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1262 return fdset_list;
1265 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1266 bool has_opaque, const char *opaque,
1267 Error **errp)
1269 MonFdset *mon_fdset = NULL;
1270 MonFdsetFd *mon_fdset_fd;
1271 AddfdInfo *fdinfo;
1273 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1274 if (has_fdset_id) {
1275 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1276 /* Break if match found or match impossible due to ordering by ID */
1277 if (fdset_id <= mon_fdset->id) {
1278 if (fdset_id < mon_fdset->id) {
1279 mon_fdset = NULL;
1281 break;
1286 if (mon_fdset == NULL) {
1287 int64_t fdset_id_prev = -1;
1288 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1290 if (has_fdset_id) {
1291 if (fdset_id < 0) {
1292 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1293 "a non-negative value");
1294 return NULL;
1296 /* Use specified fdset ID */
1297 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1298 mon_fdset_cur = mon_fdset;
1299 if (fdset_id < mon_fdset_cur->id) {
1300 break;
1303 } else {
1304 /* Use first available fdset ID */
1305 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1306 mon_fdset_cur = mon_fdset;
1307 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1308 fdset_id_prev = mon_fdset_cur->id;
1309 continue;
1311 break;
1315 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1316 if (has_fdset_id) {
1317 mon_fdset->id = fdset_id;
1318 } else {
1319 mon_fdset->id = fdset_id_prev + 1;
1322 /* The fdset list is ordered by fdset ID */
1323 if (!mon_fdset_cur) {
1324 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1325 } else if (mon_fdset->id < mon_fdset_cur->id) {
1326 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1327 } else {
1328 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1332 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1333 mon_fdset_fd->fd = fd;
1334 mon_fdset_fd->removed = false;
1335 if (has_opaque) {
1336 mon_fdset_fd->opaque = g_strdup(opaque);
1338 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1340 fdinfo = g_malloc0(sizeof(*fdinfo));
1341 fdinfo->fdset_id = mon_fdset->id;
1342 fdinfo->fd = mon_fdset_fd->fd;
1344 return fdinfo;
1347 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1349 #ifdef _WIN32
1350 return -ENOENT;
1351 #else
1352 MonFdset *mon_fdset;
1354 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1355 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1356 MonFdsetFd *mon_fdset_fd;
1357 MonFdsetFd *mon_fdset_fd_dup;
1358 int fd = -1;
1359 int dup_fd;
1360 int mon_fd_flags;
1362 if (mon_fdset->id != fdset_id) {
1363 continue;
1366 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1367 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1368 if (mon_fd_flags == -1) {
1369 return -1;
1372 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1373 fd = mon_fdset_fd->fd;
1374 break;
1378 if (fd == -1) {
1379 errno = EACCES;
1380 return -1;
1383 dup_fd = qemu_dup_flags(fd, flags);
1384 if (dup_fd == -1) {
1385 return -1;
1388 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1389 mon_fdset_fd_dup->fd = dup_fd;
1390 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1391 return dup_fd;
1394 errno = ENOENT;
1395 return -1;
1396 #endif
1399 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1401 MonFdset *mon_fdset;
1402 MonFdsetFd *mon_fdset_fd_dup;
1404 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1405 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1406 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1407 if (mon_fdset_fd_dup->fd == dup_fd) {
1408 if (remove) {
1409 QLIST_REMOVE(mon_fdset_fd_dup, next);
1410 g_free(mon_fdset_fd_dup);
1411 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1412 monitor_fdset_cleanup(mon_fdset);
1414 return -1;
1415 } else {
1416 return mon_fdset->id;
1422 return -1;
1425 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1427 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1430 void monitor_fdset_dup_fd_remove(int dup_fd)
1432 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1435 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1437 int fd;
1438 Error *local_err = NULL;
1440 if (!qemu_isdigit(fdname[0]) && mon) {
1441 fd = monitor_get_fd(mon, fdname, &local_err);
1442 } else {
1443 fd = qemu_parse_fd(fdname);
1444 if (fd == -1) {
1445 error_setg(&local_err, "Invalid file descriptor number '%s'",
1446 fdname);
1449 if (local_err) {
1450 error_propagate(errp, local_err);
1451 assert(fd == -1);
1452 } else {
1453 assert(fd != -1);
1456 return fd;
1459 /* Please update hmp-commands.hx when adding or changing commands */
1460 static HMPCommand hmp_info_cmds[] = {
1461 #include "hmp-commands-info.h"
1462 { NULL, NULL, },
1465 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1466 HMPCommand hmp_cmds[] = {
1467 #include "hmp-commands.h"
1468 { NULL, NULL, },
1472 * Set @pval to the value in the register identified by @name.
1473 * return 0 if OK, -1 if not found
1475 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1477 const MonitorDef *md = target_monitor_defs();
1478 CPUState *cs = mon_get_cpu(mon);
1479 void *ptr;
1480 uint64_t tmp = 0;
1481 int ret;
1483 if (cs == NULL || md == NULL) {
1484 return -1;
1487 for(; md->name != NULL; md++) {
1488 if (hmp_compare_cmd(name, md->name)) {
1489 if (md->get_value) {
1490 *pval = md->get_value(mon, md, md->offset);
1491 } else {
1492 CPUArchState *env = mon_get_cpu_env(mon);
1493 ptr = (uint8_t *)env + md->offset;
1494 switch(md->type) {
1495 case MD_I32:
1496 *pval = *(int32_t *)ptr;
1497 break;
1498 case MD_TLONG:
1499 *pval = *(target_long *)ptr;
1500 break;
1501 default:
1502 *pval = 0;
1503 break;
1506 return 0;
1510 ret = target_get_monitor_def(cs, name, &tmp);
1511 if (!ret) {
1512 *pval = (target_long) tmp;
1515 return ret;
1518 static void add_completion_option(ReadLineState *rs, const char *str,
1519 const char *option)
1521 if (!str || !option) {
1522 return;
1524 if (!strncmp(option, str, strlen(str))) {
1525 readline_add_completion(rs, option);
1529 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1531 size_t len;
1532 ChardevBackendInfoList *list, *start;
1534 if (nb_args != 2) {
1535 return;
1537 len = strlen(str);
1538 readline_set_completion_index(rs, len);
1540 start = list = qmp_query_chardev_backends(NULL);
1541 while (list) {
1542 const char *chr_name = list->value->name;
1544 if (!strncmp(chr_name, str, len)) {
1545 readline_add_completion(rs, chr_name);
1547 list = list->next;
1549 qapi_free_ChardevBackendInfoList(start);
1552 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1554 size_t len;
1555 int i;
1557 if (nb_args != 2) {
1558 return;
1560 len = strlen(str);
1561 readline_set_completion_index(rs, len);
1562 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1563 add_completion_option(rs, str, NetClientDriver_str(i));
1567 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1569 GSList *list, *elt;
1570 size_t len;
1572 if (nb_args != 2) {
1573 return;
1576 len = strlen(str);
1577 readline_set_completion_index(rs, len);
1578 list = elt = object_class_get_list(TYPE_DEVICE, false);
1579 while (elt) {
1580 const char *name;
1581 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1582 TYPE_DEVICE);
1583 name = object_class_get_name(OBJECT_CLASS(dc));
1585 if (dc->user_creatable
1586 && !strncmp(name, str, len)) {
1587 readline_add_completion(rs, name);
1589 elt = elt->next;
1591 g_slist_free(list);
1594 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1596 GSList *list, *elt;
1597 size_t len;
1599 if (nb_args != 2) {
1600 return;
1603 len = strlen(str);
1604 readline_set_completion_index(rs, len);
1605 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1606 while (elt) {
1607 const char *name;
1609 name = object_class_get_name(OBJECT_CLASS(elt->data));
1610 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1611 readline_add_completion(rs, name);
1613 elt = elt->next;
1615 g_slist_free(list);
1618 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1620 GSList **list = opaque;
1621 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1623 if (dev == NULL) {
1624 return 0;
1627 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1628 *list = g_slist_append(*list, dev);
1631 return 0;
1634 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1636 GSList *list = NULL;
1638 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1640 return list;
1643 static void peripheral_device_del_completion(ReadLineState *rs,
1644 const char *str, size_t len)
1646 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1647 GSList *list, *item;
1649 list = qdev_build_hotpluggable_device_list(peripheral);
1650 if (!list) {
1651 return;
1654 for (item = list; item; item = g_slist_next(item)) {
1655 DeviceState *dev = item->data;
1657 if (dev->id && !strncmp(str, dev->id, len)) {
1658 readline_add_completion(rs, dev->id);
1662 g_slist_free(list);
1665 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1667 size_t len;
1668 ChardevInfoList *list, *start;
1670 if (nb_args != 2) {
1671 return;
1673 len = strlen(str);
1674 readline_set_completion_index(rs, len);
1676 start = list = qmp_query_chardev(NULL);
1677 while (list) {
1678 ChardevInfo *chr = list->value;
1680 if (!strncmp(chr->label, str, len)) {
1681 readline_add_completion(rs, chr->label);
1683 list = list->next;
1685 qapi_free_ChardevInfoList(start);
1688 static void ringbuf_completion(ReadLineState *rs, const char *str)
1690 size_t len;
1691 ChardevInfoList *list, *start;
1693 len = strlen(str);
1694 readline_set_completion_index(rs, len);
1696 start = list = qmp_query_chardev(NULL);
1697 while (list) {
1698 ChardevInfo *chr_info = list->value;
1700 if (!strncmp(chr_info->label, str, len)) {
1701 Chardev *chr = qemu_chr_find(chr_info->label);
1702 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1703 readline_add_completion(rs, chr_info->label);
1706 list = list->next;
1708 qapi_free_ChardevInfoList(start);
1711 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1713 if (nb_args != 2) {
1714 return;
1716 ringbuf_completion(rs, str);
1719 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1721 size_t len;
1723 if (nb_args != 2) {
1724 return;
1727 len = strlen(str);
1728 readline_set_completion_index(rs, len);
1729 peripheral_device_del_completion(rs, str, len);
1732 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1734 ObjectPropertyInfoList *list, *start;
1735 size_t len;
1737 if (nb_args != 2) {
1738 return;
1740 len = strlen(str);
1741 readline_set_completion_index(rs, len);
1743 start = list = qmp_qom_list("/objects", NULL);
1744 while (list) {
1745 ObjectPropertyInfo *info = list->value;
1747 if (!strncmp(info->type, "child<", 5)
1748 && !strncmp(info->name, str, len)) {
1749 readline_add_completion(rs, info->name);
1751 list = list->next;
1753 qapi_free_ObjectPropertyInfoList(start);
1756 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1758 int i;
1759 char *sep;
1760 size_t len;
1762 if (nb_args != 2) {
1763 return;
1765 sep = strrchr(str, '-');
1766 if (sep) {
1767 str = sep + 1;
1769 len = strlen(str);
1770 readline_set_completion_index(rs, len);
1771 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1772 if (!strncmp(str, QKeyCode_str(i), len)) {
1773 readline_add_completion(rs, QKeyCode_str(i));
1778 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1780 size_t len;
1782 len = strlen(str);
1783 readline_set_completion_index(rs, len);
1784 if (nb_args == 2) {
1785 NetClientState *ncs[MAX_QUEUE_NUM];
1786 int count, i;
1787 count = qemu_find_net_clients_except(NULL, ncs,
1788 NET_CLIENT_DRIVER_NONE,
1789 MAX_QUEUE_NUM);
1790 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1791 const char *name = ncs[i]->name;
1792 if (!strncmp(str, name, len)) {
1793 readline_add_completion(rs, name);
1796 } else if (nb_args == 3) {
1797 add_completion_option(rs, str, "on");
1798 add_completion_option(rs, str, "off");
1802 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1804 int len, count, i;
1805 NetClientState *ncs[MAX_QUEUE_NUM];
1807 if (nb_args != 2) {
1808 return;
1811 len = strlen(str);
1812 readline_set_completion_index(rs, len);
1813 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1814 MAX_QUEUE_NUM);
1815 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1816 const char *name = ncs[i]->name;
1817 if (strncmp(str, name, len)) {
1818 continue;
1820 if (ncs[i]->is_netdev) {
1821 readline_add_completion(rs, name);
1826 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1828 size_t len;
1830 len = strlen(str);
1831 readline_set_completion_index(rs, len);
1832 if (nb_args == 2) {
1833 TraceEventIter iter;
1834 TraceEvent *ev;
1835 char *pattern = g_strdup_printf("%s*", str);
1836 trace_event_iter_init(&iter, pattern);
1837 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1838 readline_add_completion(rs, trace_event_get_name(ev));
1840 g_free(pattern);
1844 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1846 size_t len;
1848 len = strlen(str);
1849 readline_set_completion_index(rs, len);
1850 if (nb_args == 2) {
1851 TraceEventIter iter;
1852 TraceEvent *ev;
1853 char *pattern = g_strdup_printf("%s*", str);
1854 trace_event_iter_init(&iter, pattern);
1855 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1856 readline_add_completion(rs, trace_event_get_name(ev));
1858 g_free(pattern);
1859 } else if (nb_args == 3) {
1860 add_completion_option(rs, str, "on");
1861 add_completion_option(rs, str, "off");
1865 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1867 int i;
1869 if (nb_args != 2) {
1870 return;
1872 readline_set_completion_index(rs, strlen(str));
1873 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1874 add_completion_option(rs, str, WatchdogAction_str(i));
1878 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1879 const char *str)
1881 size_t len;
1883 len = strlen(str);
1884 readline_set_completion_index(rs, len);
1885 if (nb_args == 2) {
1886 int i;
1887 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1888 const char *name = MigrationCapability_str(i);
1889 if (!strncmp(str, name, len)) {
1890 readline_add_completion(rs, name);
1893 } else if (nb_args == 3) {
1894 add_completion_option(rs, str, "on");
1895 add_completion_option(rs, str, "off");
1899 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1900 const char *str)
1902 size_t len;
1904 len = strlen(str);
1905 readline_set_completion_index(rs, len);
1906 if (nb_args == 2) {
1907 int i;
1908 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1909 const char *name = MigrationParameter_str(i);
1910 if (!strncmp(str, name, len)) {
1911 readline_add_completion(rs, name);
1917 static void vm_completion(ReadLineState *rs, const char *str)
1919 size_t len;
1920 BlockDriverState *bs;
1921 BdrvNextIterator it;
1923 len = strlen(str);
1924 readline_set_completion_index(rs, len);
1926 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1927 SnapshotInfoList *snapshots, *snapshot;
1928 AioContext *ctx = bdrv_get_aio_context(bs);
1929 bool ok = false;
1931 aio_context_acquire(ctx);
1932 if (bdrv_can_snapshot(bs)) {
1933 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1935 aio_context_release(ctx);
1936 if (!ok) {
1937 continue;
1940 snapshot = snapshots;
1941 while (snapshot) {
1942 char *completion = snapshot->value->name;
1943 if (!strncmp(str, completion, len)) {
1944 readline_add_completion(rs, completion);
1946 completion = snapshot->value->id;
1947 if (!strncmp(str, completion, len)) {
1948 readline_add_completion(rs, completion);
1950 snapshot = snapshot->next;
1952 qapi_free_SnapshotInfoList(snapshots);
1957 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1959 if (nb_args == 2) {
1960 vm_completion(rs, str);
1964 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1966 if (nb_args == 2) {
1967 vm_completion(rs, str);
1971 static int
1972 compare_mon_cmd(const void *a, const void *b)
1974 return strcmp(((const HMPCommand *)a)->name,
1975 ((const HMPCommand *)b)->name);
1978 static void sortcmdlist(void)
1980 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1981 sizeof(*hmp_cmds),
1982 compare_mon_cmd);
1983 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1984 sizeof(*hmp_info_cmds),
1985 compare_mon_cmd);
1988 void monitor_init_globals(void)
1990 monitor_init_globals_core();
1991 monitor_init_qmp_commands();
1992 sortcmdlist();
1993 qemu_mutex_init(&mon_fdsets_lock);