target/arm: Makefile cleanup (softmmu)
[qemu/ar7.git] / monitor / misc.c
blobbf9faceb865b2644982e10c829ae6b403491f3e8
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 "hw/hw.h"
29 #include "monitor/qdev.h"
30 #include "hw/usb.h"
31 #include "hw/pci/pci.h"
32 #include "sysemu/watchdog.h"
33 #include "hw/loader.h"
34 #include "exec/gdbstub.h"
35 #include "net/net.h"
36 #include "net/slirp.h"
37 #include "chardev/char-mux.h"
38 #include "ui/qemu-spice.h"
39 #include "sysemu/numa.h"
40 #include "qemu/config-file.h"
41 #include "qemu/ctype.h"
42 #include "ui/console.h"
43 #include "ui/input.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "sysemu/hw_accel.h"
49 #include "authz/list.h"
50 #include "qapi/util.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 #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 "hmp.h"
66 #include "qemu/thread.h"
67 #include "block/qapi.h"
68 #include "qapi/qapi-commands.h"
69 #include "qapi/qapi-emit-events.h"
70 #include "qapi/error.h"
71 #include "qapi/qmp-event.h"
72 #include "qapi/qapi-introspect.h"
73 #include "sysemu/cpus.h"
74 #include "qemu/cutils.h"
75 #include "tcg/tcg.h"
77 #if defined(TARGET_S390X)
78 #include "hw/s390x/storage-keys.h"
79 #include "hw/s390x/storage-attributes.h"
80 #endif
82 /* file descriptors passed via SCM_RIGHTS */
83 typedef struct mon_fd_t mon_fd_t;
84 struct mon_fd_t {
85 char *name;
86 int fd;
87 QLIST_ENTRY(mon_fd_t) next;
90 /* file descriptor associated with a file descriptor set */
91 typedef struct MonFdsetFd MonFdsetFd;
92 struct MonFdsetFd {
93 int fd;
94 bool removed;
95 char *opaque;
96 QLIST_ENTRY(MonFdsetFd) next;
99 /* file descriptor set containing fds passed via SCM_RIGHTS */
100 typedef struct MonFdset MonFdset;
101 struct MonFdset {
102 int64_t id;
103 QLIST_HEAD(, MonFdsetFd) fds;
104 QLIST_HEAD(, MonFdsetFd) dup_fds;
105 QLIST_ENTRY(MonFdset) next;
108 /* QMP checker flags */
109 #define QMP_ACCEPT_UNKNOWNS 1
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 Monitor *old_mon;
122 MonitorHMP hmp = {};
124 monitor_data_init(&hmp.common, false, true, false);
126 old_mon = cur_mon;
127 cur_mon = &hmp.common;
129 if (has_cpu_index) {
130 int ret = monitor_set_cpu(cpu_index);
131 if (ret < 0) {
132 cur_mon = old_mon;
133 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
134 "a CPU number");
135 goto out;
139 handle_hmp_command(&hmp, command_line);
140 cur_mon = old_mon;
142 qemu_mutex_lock(&hmp.common.mon_lock);
143 if (qstring_get_length(hmp.common.outbuf) > 0) {
144 output = g_strdup(qstring_get_str(hmp.common.outbuf));
145 } else {
146 output = g_strdup("");
148 qemu_mutex_unlock(&hmp.common.mon_lock);
150 out:
151 monitor_data_destroy(&hmp.common);
152 return output;
156 * Is @name in the '|' separated list of names @list?
158 int hmp_compare_cmd(const char *name, const char *list)
160 const char *p, *pstart;
161 int len;
162 len = strlen(name);
163 p = list;
164 for (;;) {
165 pstart = p;
166 p = qemu_strchrnul(p, '|');
167 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
168 return 1;
170 if (*p == '\0') {
171 break;
173 p++;
175 return 0;
178 static void do_help_cmd(Monitor *mon, const QDict *qdict)
180 help_cmd(mon, qdict_get_try_str(qdict, "name"));
183 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
185 const char *tp_name = qdict_get_str(qdict, "name");
186 bool new_state = qdict_get_bool(qdict, "option");
187 bool has_vcpu = qdict_haskey(qdict, "vcpu");
188 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
189 Error *local_err = NULL;
191 if (vcpu < 0) {
192 monitor_printf(mon, "argument vcpu must be positive");
193 return;
196 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
197 if (local_err) {
198 error_report_err(local_err);
202 #ifdef CONFIG_TRACE_SIMPLE
203 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
205 const char *op = qdict_get_try_str(qdict, "op");
206 const char *arg = qdict_get_try_str(qdict, "arg");
208 if (!op) {
209 st_print_trace_file_status();
210 } else if (!strcmp(op, "on")) {
211 st_set_trace_file_enabled(true);
212 } else if (!strcmp(op, "off")) {
213 st_set_trace_file_enabled(false);
214 } else if (!strcmp(op, "flush")) {
215 st_flush_trace_buffer();
216 } else if (!strcmp(op, "set")) {
217 if (arg) {
218 st_set_trace_file(arg);
220 } else {
221 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
222 help_cmd(mon, "trace-file");
225 #endif
227 static void hmp_info_help(Monitor *mon, const QDict *qdict)
229 help_cmd(mon, "info");
232 static void query_commands_cb(QmpCommand *cmd, void *opaque)
234 CommandInfoList *info, **list = opaque;
236 if (!cmd->enabled) {
237 return;
240 info = g_malloc0(sizeof(*info));
241 info->value = g_malloc0(sizeof(*info->value));
242 info->value->name = g_strdup(cmd->name);
243 info->next = *list;
244 *list = info;
247 CommandInfoList *qmp_query_commands(Error **errp)
249 CommandInfoList *list = NULL;
250 MonitorQMP *mon;
252 assert(monitor_is_qmp(cur_mon));
253 mon = container_of(cur_mon, MonitorQMP, common);
255 qmp_for_each_command(mon->commands, query_commands_cb, &list);
257 return list;
260 EventInfoList *qmp_query_events(Error **errp)
263 * TODO This deprecated command is the only user of
264 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
265 * they should go, too.
267 EventInfoList *info, *ev_list = NULL;
268 QAPIEvent e;
270 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
271 const char *event_name = QAPIEvent_str(e);
272 assert(event_name != NULL);
273 info = g_malloc0(sizeof(*info));
274 info->value = g_malloc0(sizeof(*info->value));
275 info->value->name = g_strdup(event_name);
277 info->next = ev_list;
278 ev_list = info;
281 return ev_list;
285 * Minor hack: generated marshalling suppressed for this command
286 * ('gen': false in the schema) so we can parse the JSON string
287 * directly into QObject instead of first parsing it with
288 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
289 * to QObject with generated output marshallers, every time. Instead,
290 * we do it in test-qobject-input-visitor.c, just to make sure
291 * qapi-gen.py's output actually conforms to the schema.
293 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
294 Error **errp)
296 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
299 static void monitor_init_qmp_commands(void)
302 * Two command lists:
303 * - qmp_commands contains all QMP commands
304 * - qmp_cap_negotiation_commands contains just
305 * "qmp_capabilities", to enforce capability negotiation
308 qmp_init_marshal(&qmp_commands);
310 qmp_register_command(&qmp_commands, "query-qmp-schema",
311 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
312 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
313 QCO_NO_OPTIONS);
314 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
315 QCO_NO_OPTIONS);
317 QTAILQ_INIT(&qmp_cap_negotiation_commands);
318 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
319 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
323 * Accept QMP capabilities in @list for @mon.
324 * On success, set mon->qmp.capab[], and return true.
325 * On error, set @errp, and return false.
327 static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
328 Error **errp)
330 GString *unavailable = NULL;
331 bool capab[QMP_CAPABILITY__MAX];
333 memset(capab, 0, sizeof(capab));
335 for (; list; list = list->next) {
336 if (!mon->capab_offered[list->value]) {
337 if (!unavailable) {
338 unavailable = g_string_new(QMPCapability_str(list->value));
339 } else {
340 g_string_append_printf(unavailable, ", %s",
341 QMPCapability_str(list->value));
344 capab[list->value] = true;
347 if (unavailable) {
348 error_setg(errp, "Capability %s not available", unavailable->str);
349 g_string_free(unavailable, true);
350 return false;
353 memcpy(mon->capab, capab, sizeof(capab));
354 return true;
357 void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
358 Error **errp)
360 MonitorQMP *mon;
362 assert(monitor_is_qmp(cur_mon));
363 mon = container_of(cur_mon, MonitorQMP, common);
365 if (mon->commands == &qmp_commands) {
366 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
367 "Capabilities negotiation is already complete, command "
368 "ignored");
369 return;
372 if (!qmp_caps_accept(mon, enable, errp)) {
373 return;
376 mon->commands = &qmp_commands;
379 /* Set the current CPU defined by the user. Callers must hold BQL. */
380 int monitor_set_cpu(int cpu_index)
382 CPUState *cpu;
384 cpu = qemu_get_cpu(cpu_index);
385 if (cpu == NULL) {
386 return -1;
388 g_free(cur_mon->mon_cpu_path);
389 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
390 return 0;
393 /* Callers must hold BQL. */
394 static CPUState *mon_get_cpu_sync(bool synchronize)
396 CPUState *cpu;
398 if (cur_mon->mon_cpu_path) {
399 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
400 TYPE_CPU, NULL);
401 if (!cpu) {
402 g_free(cur_mon->mon_cpu_path);
403 cur_mon->mon_cpu_path = NULL;
406 if (!cur_mon->mon_cpu_path) {
407 if (!first_cpu) {
408 return NULL;
410 monitor_set_cpu(first_cpu->cpu_index);
411 cpu = first_cpu;
413 if (synchronize) {
414 cpu_synchronize_state(cpu);
416 return cpu;
419 CPUState *mon_get_cpu(void)
421 return mon_get_cpu_sync(true);
424 CPUArchState *mon_get_cpu_env(void)
426 CPUState *cs = mon_get_cpu();
428 return cs ? cs->env_ptr : NULL;
431 int monitor_get_cpu_index(void)
433 CPUState *cs = mon_get_cpu_sync(false);
435 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
438 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
440 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
441 CPUState *cs;
443 if (all_cpus) {
444 CPU_FOREACH(cs) {
445 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
446 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
448 } else {
449 cs = mon_get_cpu();
451 if (!cs) {
452 monitor_printf(mon, "No CPU available\n");
453 return;
456 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
460 #ifdef CONFIG_TCG
461 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
463 if (!tcg_enabled()) {
464 error_report("JIT information is only available with accel=tcg");
465 return;
468 dump_exec_info();
469 dump_drift_info();
472 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
474 dump_opcount_info();
476 #endif
478 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
480 int64_t max = qdict_get_try_int(qdict, "max", 10);
481 bool mean = qdict_get_try_bool(qdict, "mean", false);
482 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
483 enum QSPSortBy sort_by;
485 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
486 qsp_report(max, sort_by, coalesce);
489 static void hmp_info_history(Monitor *mon, const QDict *qdict)
491 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
492 int i;
493 const char *str;
495 if (!hmp_mon->rs) {
496 return;
498 i = 0;
499 for(;;) {
500 str = readline_get_history(hmp_mon->rs, i);
501 if (!str) {
502 break;
504 monitor_printf(mon, "%d: '%s'\n", i, str);
505 i++;
509 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
511 CPUState *cs = mon_get_cpu();
513 if (!cs) {
514 monitor_printf(mon, "No CPU available\n");
515 return;
517 cpu_dump_statistics(cs, 0);
520 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
522 const char *name = qdict_get_try_str(qdict, "name");
523 bool has_vcpu = qdict_haskey(qdict, "vcpu");
524 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
525 TraceEventInfoList *events;
526 TraceEventInfoList *elem;
527 Error *local_err = NULL;
529 if (name == NULL) {
530 name = "*";
532 if (vcpu < 0) {
533 monitor_printf(mon, "argument vcpu must be positive");
534 return;
537 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
538 if (local_err) {
539 error_report_err(local_err);
540 return;
543 for (elem = events; elem != NULL; elem = elem->next) {
544 monitor_printf(mon, "%s : state %u\n",
545 elem->value->name,
546 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
548 qapi_free_TraceEventInfoList(events);
551 void qmp_client_migrate_info(const char *protocol, const char *hostname,
552 bool has_port, int64_t port,
553 bool has_tls_port, int64_t tls_port,
554 bool has_cert_subject, const char *cert_subject,
555 Error **errp)
557 if (strcmp(protocol, "spice") == 0) {
558 if (!qemu_using_spice(errp)) {
559 return;
562 if (!has_port && !has_tls_port) {
563 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
564 return;
567 if (qemu_spice_migrate_info(hostname,
568 has_port ? port : -1,
569 has_tls_port ? tls_port : -1,
570 cert_subject)) {
571 error_setg(errp, QERR_UNDEFINED_ERROR);
572 return;
574 return;
577 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
580 static void hmp_logfile(Monitor *mon, const QDict *qdict)
582 Error *err = NULL;
584 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
585 if (err) {
586 error_report_err(err);
590 static void hmp_log(Monitor *mon, const QDict *qdict)
592 int mask;
593 const char *items = qdict_get_str(qdict, "items");
595 if (!strcmp(items, "none")) {
596 mask = 0;
597 } else {
598 mask = qemu_str_to_log_mask(items);
599 if (!mask) {
600 help_cmd(mon, "log");
601 return;
604 qemu_set_log(mask);
607 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
609 const char *option = qdict_get_try_str(qdict, "option");
610 if (!option || !strcmp(option, "on")) {
611 singlestep = 1;
612 } else if (!strcmp(option, "off")) {
613 singlestep = 0;
614 } else {
615 monitor_printf(mon, "unexpected option %s\n", option);
619 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
621 const char *device = qdict_get_try_str(qdict, "device");
622 if (!device)
623 device = "tcp::" DEFAULT_GDBSTUB_PORT;
624 if (gdbserver_start(device) < 0) {
625 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
626 device);
627 } else if (strcmp(device, "none") == 0) {
628 monitor_printf(mon, "Disabled gdbserver\n");
629 } else {
630 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
631 device);
635 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
637 const char *action = qdict_get_str(qdict, "action");
638 if (select_watchdog_action(action) == -1) {
639 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
643 static void monitor_printc(Monitor *mon, int c)
645 monitor_printf(mon, "'");
646 switch(c) {
647 case '\'':
648 monitor_printf(mon, "\\'");
649 break;
650 case '\\':
651 monitor_printf(mon, "\\\\");
652 break;
653 case '\n':
654 monitor_printf(mon, "\\n");
655 break;
656 case '\r':
657 monitor_printf(mon, "\\r");
658 break;
659 default:
660 if (c >= 32 && c <= 126) {
661 monitor_printf(mon, "%c", c);
662 } else {
663 monitor_printf(mon, "\\x%02x", c);
665 break;
667 monitor_printf(mon, "'");
670 static void memory_dump(Monitor *mon, int count, int format, int wsize,
671 hwaddr addr, int is_physical)
673 int l, line_size, i, max_digits, len;
674 uint8_t buf[16];
675 uint64_t v;
676 CPUState *cs = mon_get_cpu();
678 if (!cs && (format == 'i' || !is_physical)) {
679 monitor_printf(mon, "Can not dump without CPU\n");
680 return;
683 if (format == 'i') {
684 monitor_disas(mon, cs, addr, count, is_physical);
685 return;
688 len = wsize * count;
689 if (wsize == 1)
690 line_size = 8;
691 else
692 line_size = 16;
693 max_digits = 0;
695 switch(format) {
696 case 'o':
697 max_digits = DIV_ROUND_UP(wsize * 8, 3);
698 break;
699 default:
700 case 'x':
701 max_digits = (wsize * 8) / 4;
702 break;
703 case 'u':
704 case 'd':
705 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
706 break;
707 case 'c':
708 wsize = 1;
709 break;
712 while (len > 0) {
713 if (is_physical)
714 monitor_printf(mon, TARGET_FMT_plx ":", addr);
715 else
716 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
717 l = len;
718 if (l > line_size)
719 l = line_size;
720 if (is_physical) {
721 AddressSpace *as = cs ? cs->as : &address_space_memory;
722 MemTxResult r = address_space_read(as, addr,
723 MEMTXATTRS_UNSPECIFIED, buf, l);
724 if (r != MEMTX_OK) {
725 monitor_printf(mon, " Cannot access memory\n");
726 break;
728 } else {
729 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
730 monitor_printf(mon, " Cannot access memory\n");
731 break;
734 i = 0;
735 while (i < l) {
736 switch(wsize) {
737 default:
738 case 1:
739 v = ldub_p(buf + i);
740 break;
741 case 2:
742 v = lduw_p(buf + i);
743 break;
744 case 4:
745 v = (uint32_t)ldl_p(buf + i);
746 break;
747 case 8:
748 v = ldq_p(buf + i);
749 break;
751 monitor_printf(mon, " ");
752 switch(format) {
753 case 'o':
754 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
755 break;
756 case 'x':
757 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
758 break;
759 case 'u':
760 monitor_printf(mon, "%*" PRIu64, max_digits, v);
761 break;
762 case 'd':
763 monitor_printf(mon, "%*" PRId64, max_digits, v);
764 break;
765 case 'c':
766 monitor_printc(mon, v);
767 break;
769 i += wsize;
771 monitor_printf(mon, "\n");
772 addr += l;
773 len -= l;
777 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
779 int count = qdict_get_int(qdict, "count");
780 int format = qdict_get_int(qdict, "format");
781 int size = qdict_get_int(qdict, "size");
782 target_long addr = qdict_get_int(qdict, "addr");
784 memory_dump(mon, count, format, size, addr, 0);
787 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
789 int count = qdict_get_int(qdict, "count");
790 int format = qdict_get_int(qdict, "format");
791 int size = qdict_get_int(qdict, "size");
792 hwaddr addr = qdict_get_int(qdict, "addr");
794 memory_dump(mon, count, format, size, addr, 1);
797 static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
799 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
800 addr, 1);
802 if (!mrs.mr) {
803 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
804 return NULL;
807 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
808 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
809 memory_region_unref(mrs.mr);
810 return NULL;
813 *p_mr = mrs.mr;
814 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
817 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
819 hwaddr addr = qdict_get_int(qdict, "addr");
820 Error *local_err = NULL;
821 MemoryRegion *mr = NULL;
822 void *ptr;
824 ptr = gpa2hva(&mr, addr, &local_err);
825 if (local_err) {
826 error_report_err(local_err);
827 return;
830 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
831 " (%s) is %p\n",
832 addr, mr->name, ptr);
834 memory_region_unref(mr);
837 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
839 target_ulong addr = qdict_get_int(qdict, "addr");
840 MemTxAttrs attrs;
841 CPUState *cs = mon_get_cpu();
842 hwaddr gpa;
844 if (!cs) {
845 monitor_printf(mon, "No cpu\n");
846 return;
849 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
850 if (gpa == -1) {
851 monitor_printf(mon, "Unmapped\n");
852 } else {
853 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
854 gpa + (addr & ~TARGET_PAGE_MASK));
858 #ifdef CONFIG_LINUX
859 static uint64_t vtop(void *ptr, Error **errp)
861 uint64_t pinfo;
862 uint64_t ret = -1;
863 uintptr_t addr = (uintptr_t) ptr;
864 uintptr_t pagesize = getpagesize();
865 off_t offset = addr / pagesize * sizeof(pinfo);
866 int fd;
868 fd = open("/proc/self/pagemap", O_RDONLY);
869 if (fd == -1) {
870 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
871 return -1;
874 /* Force copy-on-write if necessary. */
875 atomic_add((uint8_t *)ptr, 0);
877 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
878 error_setg_errno(errp, errno, "Cannot read pagemap");
879 goto out;
881 if ((pinfo & (1ull << 63)) == 0) {
882 error_setg(errp, "Page not present");
883 goto out;
885 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
887 out:
888 close(fd);
889 return ret;
892 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
894 hwaddr addr = qdict_get_int(qdict, "addr");
895 Error *local_err = NULL;
896 MemoryRegion *mr = NULL;
897 void *ptr;
898 uint64_t physaddr;
900 ptr = gpa2hva(&mr, addr, &local_err);
901 if (local_err) {
902 error_report_err(local_err);
903 return;
906 physaddr = vtop(ptr, &local_err);
907 if (local_err) {
908 error_report_err(local_err);
909 } else {
910 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
911 " (%s) is 0x%" PRIx64 "\n",
912 addr, mr->name, (uint64_t) physaddr);
915 memory_region_unref(mr);
917 #endif
919 static void do_print(Monitor *mon, const QDict *qdict)
921 int format = qdict_get_int(qdict, "format");
922 hwaddr val = qdict_get_int(qdict, "val");
924 switch(format) {
925 case 'o':
926 monitor_printf(mon, "%#" HWADDR_PRIo, val);
927 break;
928 case 'x':
929 monitor_printf(mon, "%#" HWADDR_PRIx, val);
930 break;
931 case 'u':
932 monitor_printf(mon, "%" HWADDR_PRIu, val);
933 break;
934 default:
935 case 'd':
936 monitor_printf(mon, "%" HWADDR_PRId, val);
937 break;
938 case 'c':
939 monitor_printc(mon, val);
940 break;
942 monitor_printf(mon, "\n");
945 static void hmp_sum(Monitor *mon, const QDict *qdict)
947 uint32_t addr;
948 uint16_t sum;
949 uint32_t start = qdict_get_int(qdict, "start");
950 uint32_t size = qdict_get_int(qdict, "size");
952 sum = 0;
953 for(addr = start; addr < (start + size); addr++) {
954 uint8_t val = address_space_ldub(&address_space_memory, addr,
955 MEMTXATTRS_UNSPECIFIED, NULL);
956 /* BSD sum algorithm ('sum' Unix command) */
957 sum = (sum >> 1) | (sum << 15);
958 sum += val;
960 monitor_printf(mon, "%05d\n", sum);
963 static int mouse_button_state;
965 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
967 int dx, dy, dz, button;
968 const char *dx_str = qdict_get_str(qdict, "dx_str");
969 const char *dy_str = qdict_get_str(qdict, "dy_str");
970 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
972 dx = strtol(dx_str, NULL, 0);
973 dy = strtol(dy_str, NULL, 0);
974 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
975 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
977 if (dz_str) {
978 dz = strtol(dz_str, NULL, 0);
979 if (dz != 0) {
980 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
981 qemu_input_queue_btn(NULL, button, true);
982 qemu_input_event_sync();
983 qemu_input_queue_btn(NULL, button, false);
986 qemu_input_event_sync();
989 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
991 static uint32_t bmap[INPUT_BUTTON__MAX] = {
992 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
993 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
994 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
996 int button_state = qdict_get_int(qdict, "button_state");
998 if (mouse_button_state == button_state) {
999 return;
1001 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1002 qemu_input_event_sync();
1003 mouse_button_state = button_state;
1006 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1008 int size = qdict_get_int(qdict, "size");
1009 int addr = qdict_get_int(qdict, "addr");
1010 int has_index = qdict_haskey(qdict, "index");
1011 uint32_t val;
1012 int suffix;
1014 if (has_index) {
1015 int index = qdict_get_int(qdict, "index");
1016 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1017 addr++;
1019 addr &= 0xffff;
1021 switch(size) {
1022 default:
1023 case 1:
1024 val = cpu_inb(addr);
1025 suffix = 'b';
1026 break;
1027 case 2:
1028 val = cpu_inw(addr);
1029 suffix = 'w';
1030 break;
1031 case 4:
1032 val = cpu_inl(addr);
1033 suffix = 'l';
1034 break;
1036 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1037 suffix, addr, size * 2, val);
1040 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1042 int size = qdict_get_int(qdict, "size");
1043 int addr = qdict_get_int(qdict, "addr");
1044 int val = qdict_get_int(qdict, "val");
1046 addr &= IOPORTS_MASK;
1048 switch (size) {
1049 default:
1050 case 1:
1051 cpu_outb(addr, val);
1052 break;
1053 case 2:
1054 cpu_outw(addr, val);
1055 break;
1056 case 4:
1057 cpu_outl(addr, val);
1058 break;
1062 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1064 Error *local_err = NULL;
1065 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1067 qemu_boot_set(bootdevice, &local_err);
1068 if (local_err) {
1069 error_report_err(local_err);
1070 } else {
1071 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1075 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1077 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1078 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
1079 bool owner = qdict_get_try_bool(qdict, "owner", false);
1081 mtree_info(flatview, dispatch_tree, owner);
1084 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1086 int i;
1087 NumaNodeMem *node_mem;
1088 CpuInfoList *cpu_list, *cpu;
1090 cpu_list = qmp_query_cpus(&error_abort);
1091 node_mem = g_new0(NumaNodeMem, nb_numa_nodes);
1093 query_numa_node_mem(node_mem);
1094 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1095 for (i = 0; i < nb_numa_nodes; i++) {
1096 monitor_printf(mon, "node %d cpus:", i);
1097 for (cpu = cpu_list; cpu; cpu = cpu->next) {
1098 if (cpu->value->has_props && cpu->value->props->has_node_id &&
1099 cpu->value->props->node_id == i) {
1100 monitor_printf(mon, " %" PRIi64, cpu->value->CPU);
1103 monitor_printf(mon, "\n");
1104 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1105 node_mem[i].node_mem >> 20);
1106 monitor_printf(mon, "node %d plugged: %" PRId64 " MB\n", i,
1107 node_mem[i].node_plugged_mem >> 20);
1109 qapi_free_CpuInfoList(cpu_list);
1110 g_free(node_mem);
1113 #ifdef CONFIG_PROFILER
1115 int64_t dev_time;
1117 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1119 static int64_t last_cpu_exec_time;
1120 int64_t cpu_exec_time;
1121 int64_t delta;
1123 cpu_exec_time = tcg_cpu_exec_time();
1124 delta = cpu_exec_time - last_cpu_exec_time;
1126 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1127 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1128 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1129 delta, delta / (double)NANOSECONDS_PER_SECOND);
1130 last_cpu_exec_time = cpu_exec_time;
1131 dev_time = 0;
1133 #else
1134 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1136 monitor_printf(mon, "Internal profiler not compiled\n");
1138 #endif
1140 /* Capture support */
1141 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1143 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1145 int i;
1146 CaptureState *s;
1148 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1149 monitor_printf(mon, "[%d]: ", i);
1150 s->ops.info (s->opaque);
1154 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1156 int i;
1157 int n = qdict_get_int(qdict, "n");
1158 CaptureState *s;
1160 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1161 if (i == n) {
1162 s->ops.destroy (s->opaque);
1163 QLIST_REMOVE (s, entries);
1164 g_free (s);
1165 return;
1170 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1172 const char *path = qdict_get_str(qdict, "path");
1173 int has_freq = qdict_haskey(qdict, "freq");
1174 int freq = qdict_get_try_int(qdict, "freq", -1);
1175 int has_bits = qdict_haskey(qdict, "bits");
1176 int bits = qdict_get_try_int(qdict, "bits", -1);
1177 int has_channels = qdict_haskey(qdict, "nchannels");
1178 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1179 CaptureState *s;
1181 s = g_malloc0 (sizeof (*s));
1183 freq = has_freq ? freq : 44100;
1184 bits = has_bits ? bits : 16;
1185 nchannels = has_channels ? nchannels : 2;
1187 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1188 monitor_printf(mon, "Failed to add wave capture\n");
1189 g_free (s);
1190 return;
1192 QLIST_INSERT_HEAD (&capture_head, s, entries);
1195 static QAuthZList *find_auth(Monitor *mon, const char *name)
1197 Object *obj;
1198 Object *container;
1200 container = object_get_objects_root();
1201 obj = object_resolve_path_component(container, name);
1202 if (!obj) {
1203 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1204 return NULL;
1207 return QAUTHZ_LIST(obj);
1210 static bool warn_acl;
1211 static void hmp_warn_acl(void)
1213 if (warn_acl) {
1214 return;
1216 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1217 "commands are deprecated with no replacement. Authorization "
1218 "for VNC should be performed using the pluggable QAuthZ "
1219 "objects");
1220 warn_acl = true;
1223 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1225 const char *aclname = qdict_get_str(qdict, "aclname");
1226 QAuthZList *auth = find_auth(mon, aclname);
1227 QAuthZListRuleList *rules;
1228 size_t i = 0;
1230 hmp_warn_acl();
1232 if (!auth) {
1233 return;
1236 monitor_printf(mon, "policy: %s\n",
1237 QAuthZListPolicy_str(auth->policy));
1239 rules = auth->rules;
1240 while (rules) {
1241 QAuthZListRule *rule = rules->value;
1242 i++;
1243 monitor_printf(mon, "%zu: %s %s\n", i,
1244 QAuthZListPolicy_str(rule->policy),
1245 rule->match);
1246 rules = rules->next;
1250 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1252 const char *aclname = qdict_get_str(qdict, "aclname");
1253 QAuthZList *auth = find_auth(mon, aclname);
1255 hmp_warn_acl();
1257 if (!auth) {
1258 return;
1261 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1262 qapi_free_QAuthZListRuleList(auth->rules);
1263 auth->rules = NULL;
1264 monitor_printf(mon, "acl: removed all rules\n");
1267 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1269 const char *aclname = qdict_get_str(qdict, "aclname");
1270 const char *policy = qdict_get_str(qdict, "policy");
1271 QAuthZList *auth = find_auth(mon, aclname);
1272 int val;
1273 Error *err = NULL;
1275 hmp_warn_acl();
1277 if (!auth) {
1278 return;
1281 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1282 policy,
1283 QAUTHZ_LIST_POLICY_DENY,
1284 &err);
1285 if (err) {
1286 error_free(err);
1287 monitor_printf(mon, "acl: unknown policy '%s', "
1288 "expected 'deny' or 'allow'\n", policy);
1289 } else {
1290 auth->policy = val;
1291 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
1292 monitor_printf(mon, "acl: policy set to 'allow'\n");
1293 } else {
1294 monitor_printf(mon, "acl: policy set to 'deny'\n");
1299 static QAuthZListFormat hmp_acl_get_format(const char *match)
1301 if (strchr(match, '*')) {
1302 return QAUTHZ_LIST_FORMAT_GLOB;
1303 } else {
1304 return QAUTHZ_LIST_FORMAT_EXACT;
1308 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1310 const char *aclname = qdict_get_str(qdict, "aclname");
1311 const char *match = qdict_get_str(qdict, "match");
1312 const char *policystr = qdict_get_str(qdict, "policy");
1313 int has_index = qdict_haskey(qdict, "index");
1314 int index = qdict_get_try_int(qdict, "index", -1);
1315 QAuthZList *auth = find_auth(mon, aclname);
1316 Error *err = NULL;
1317 QAuthZListPolicy policy;
1318 QAuthZListFormat format;
1319 size_t i = 0;
1321 hmp_warn_acl();
1323 if (!auth) {
1324 return;
1327 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1328 policystr,
1329 QAUTHZ_LIST_POLICY_DENY,
1330 &err);
1331 if (err) {
1332 error_free(err);
1333 monitor_printf(mon, "acl: unknown policy '%s', "
1334 "expected 'deny' or 'allow'\n", policystr);
1335 return;
1338 format = hmp_acl_get_format(match);
1340 if (has_index && index == 0) {
1341 monitor_printf(mon, "acl: unable to add acl entry\n");
1342 return;
1345 if (has_index) {
1346 i = qauthz_list_insert_rule(auth, match, policy,
1347 format, index - 1, &err);
1348 } else {
1349 i = qauthz_list_append_rule(auth, match, policy,
1350 format, &err);
1352 if (err) {
1353 monitor_printf(mon, "acl: unable to add rule: %s",
1354 error_get_pretty(err));
1355 error_free(err);
1356 } else {
1357 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
1361 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1363 const char *aclname = qdict_get_str(qdict, "aclname");
1364 const char *match = qdict_get_str(qdict, "match");
1365 QAuthZList *auth = find_auth(mon, aclname);
1366 ssize_t i = 0;
1368 hmp_warn_acl();
1370 if (!auth) {
1371 return;
1374 i = qauthz_list_delete_rule(auth, match);
1375 if (i >= 0) {
1376 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1377 } else {
1378 monitor_printf(mon, "acl: no matching acl entry\n");
1382 void qmp_getfd(const char *fdname, Error **errp)
1384 mon_fd_t *monfd;
1385 int fd, tmp_fd;
1387 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1388 if (fd == -1) {
1389 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1390 return;
1393 if (qemu_isdigit(fdname[0])) {
1394 close(fd);
1395 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1396 "a name not starting with a digit");
1397 return;
1400 qemu_mutex_lock(&cur_mon->mon_lock);
1401 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1402 if (strcmp(monfd->name, fdname) != 0) {
1403 continue;
1406 tmp_fd = monfd->fd;
1407 monfd->fd = fd;
1408 qemu_mutex_unlock(&cur_mon->mon_lock);
1409 /* Make sure close() is outside critical section */
1410 close(tmp_fd);
1411 return;
1414 monfd = g_malloc0(sizeof(mon_fd_t));
1415 monfd->name = g_strdup(fdname);
1416 monfd->fd = fd;
1418 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1419 qemu_mutex_unlock(&cur_mon->mon_lock);
1422 void qmp_closefd(const char *fdname, Error **errp)
1424 mon_fd_t *monfd;
1425 int tmp_fd;
1427 qemu_mutex_lock(&cur_mon->mon_lock);
1428 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1429 if (strcmp(monfd->name, fdname) != 0) {
1430 continue;
1433 QLIST_REMOVE(monfd, next);
1434 tmp_fd = monfd->fd;
1435 g_free(monfd->name);
1436 g_free(monfd);
1437 qemu_mutex_unlock(&cur_mon->mon_lock);
1438 /* Make sure close() is outside critical section */
1439 close(tmp_fd);
1440 return;
1443 qemu_mutex_unlock(&cur_mon->mon_lock);
1444 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1447 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1449 mon_fd_t *monfd;
1451 qemu_mutex_lock(&mon->mon_lock);
1452 QLIST_FOREACH(monfd, &mon->fds, next) {
1453 int fd;
1455 if (strcmp(monfd->name, fdname) != 0) {
1456 continue;
1459 fd = monfd->fd;
1461 /* caller takes ownership of fd */
1462 QLIST_REMOVE(monfd, next);
1463 g_free(monfd->name);
1464 g_free(monfd);
1465 qemu_mutex_unlock(&mon->mon_lock);
1467 return fd;
1470 qemu_mutex_unlock(&mon->mon_lock);
1471 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1472 return -1;
1475 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1477 MonFdsetFd *mon_fdset_fd;
1478 MonFdsetFd *mon_fdset_fd_next;
1480 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1481 if ((mon_fdset_fd->removed ||
1482 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1483 runstate_is_running()) {
1484 close(mon_fdset_fd->fd);
1485 g_free(mon_fdset_fd->opaque);
1486 QLIST_REMOVE(mon_fdset_fd, next);
1487 g_free(mon_fdset_fd);
1491 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1492 QLIST_REMOVE(mon_fdset, next);
1493 g_free(mon_fdset);
1497 void monitor_fdsets_cleanup(void)
1499 MonFdset *mon_fdset;
1500 MonFdset *mon_fdset_next;
1502 qemu_mutex_lock(&mon_fdsets_lock);
1503 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1504 monitor_fdset_cleanup(mon_fdset);
1506 qemu_mutex_unlock(&mon_fdsets_lock);
1509 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1510 const char *opaque, Error **errp)
1512 int fd;
1513 Monitor *mon = cur_mon;
1514 AddfdInfo *fdinfo;
1516 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1517 if (fd == -1) {
1518 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1519 goto error;
1522 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1523 has_opaque, opaque, errp);
1524 if (fdinfo) {
1525 return fdinfo;
1528 error:
1529 if (fd != -1) {
1530 close(fd);
1532 return NULL;
1535 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1537 MonFdset *mon_fdset;
1538 MonFdsetFd *mon_fdset_fd;
1539 char fd_str[60];
1541 qemu_mutex_lock(&mon_fdsets_lock);
1542 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1543 if (mon_fdset->id != fdset_id) {
1544 continue;
1546 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1547 if (has_fd) {
1548 if (mon_fdset_fd->fd != fd) {
1549 continue;
1551 mon_fdset_fd->removed = true;
1552 break;
1553 } else {
1554 mon_fdset_fd->removed = true;
1557 if (has_fd && !mon_fdset_fd) {
1558 goto error;
1560 monitor_fdset_cleanup(mon_fdset);
1561 qemu_mutex_unlock(&mon_fdsets_lock);
1562 return;
1565 error:
1566 qemu_mutex_unlock(&mon_fdsets_lock);
1567 if (has_fd) {
1568 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1569 fdset_id, fd);
1570 } else {
1571 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1573 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1576 FdsetInfoList *qmp_query_fdsets(Error **errp)
1578 MonFdset *mon_fdset;
1579 MonFdsetFd *mon_fdset_fd;
1580 FdsetInfoList *fdset_list = NULL;
1582 qemu_mutex_lock(&mon_fdsets_lock);
1583 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1584 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1585 FdsetFdInfoList *fdsetfd_list = NULL;
1587 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1588 fdset_info->value->fdset_id = mon_fdset->id;
1590 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1591 FdsetFdInfoList *fdsetfd_info;
1593 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1594 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1595 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1596 if (mon_fdset_fd->opaque) {
1597 fdsetfd_info->value->has_opaque = true;
1598 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1599 } else {
1600 fdsetfd_info->value->has_opaque = false;
1603 fdsetfd_info->next = fdsetfd_list;
1604 fdsetfd_list = fdsetfd_info;
1607 fdset_info->value->fds = fdsetfd_list;
1609 fdset_info->next = fdset_list;
1610 fdset_list = fdset_info;
1612 qemu_mutex_unlock(&mon_fdsets_lock);
1614 return fdset_list;
1617 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1618 bool has_opaque, const char *opaque,
1619 Error **errp)
1621 MonFdset *mon_fdset = NULL;
1622 MonFdsetFd *mon_fdset_fd;
1623 AddfdInfo *fdinfo;
1625 qemu_mutex_lock(&mon_fdsets_lock);
1626 if (has_fdset_id) {
1627 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1628 /* Break if match found or match impossible due to ordering by ID */
1629 if (fdset_id <= mon_fdset->id) {
1630 if (fdset_id < mon_fdset->id) {
1631 mon_fdset = NULL;
1633 break;
1638 if (mon_fdset == NULL) {
1639 int64_t fdset_id_prev = -1;
1640 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1642 if (has_fdset_id) {
1643 if (fdset_id < 0) {
1644 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1645 "a non-negative value");
1646 qemu_mutex_unlock(&mon_fdsets_lock);
1647 return NULL;
1649 /* Use specified fdset ID */
1650 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1651 mon_fdset_cur = mon_fdset;
1652 if (fdset_id < mon_fdset_cur->id) {
1653 break;
1656 } else {
1657 /* Use first available fdset ID */
1658 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1659 mon_fdset_cur = mon_fdset;
1660 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1661 fdset_id_prev = mon_fdset_cur->id;
1662 continue;
1664 break;
1668 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1669 if (has_fdset_id) {
1670 mon_fdset->id = fdset_id;
1671 } else {
1672 mon_fdset->id = fdset_id_prev + 1;
1675 /* The fdset list is ordered by fdset ID */
1676 if (!mon_fdset_cur) {
1677 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1678 } else if (mon_fdset->id < mon_fdset_cur->id) {
1679 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1680 } else {
1681 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1685 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1686 mon_fdset_fd->fd = fd;
1687 mon_fdset_fd->removed = false;
1688 if (has_opaque) {
1689 mon_fdset_fd->opaque = g_strdup(opaque);
1691 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1693 fdinfo = g_malloc0(sizeof(*fdinfo));
1694 fdinfo->fdset_id = mon_fdset->id;
1695 fdinfo->fd = mon_fdset_fd->fd;
1697 qemu_mutex_unlock(&mon_fdsets_lock);
1698 return fdinfo;
1701 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1703 #ifdef _WIN32
1704 return -ENOENT;
1705 #else
1706 MonFdset *mon_fdset;
1707 MonFdsetFd *mon_fdset_fd;
1708 int mon_fd_flags;
1709 int ret;
1711 qemu_mutex_lock(&mon_fdsets_lock);
1712 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1713 if (mon_fdset->id != fdset_id) {
1714 continue;
1716 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1717 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1718 if (mon_fd_flags == -1) {
1719 ret = -errno;
1720 goto out;
1723 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1724 ret = mon_fdset_fd->fd;
1725 goto out;
1728 ret = -EACCES;
1729 goto out;
1731 ret = -ENOENT;
1733 out:
1734 qemu_mutex_unlock(&mon_fdsets_lock);
1735 return ret;
1736 #endif
1739 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1741 MonFdset *mon_fdset;
1742 MonFdsetFd *mon_fdset_fd_dup;
1744 qemu_mutex_lock(&mon_fdsets_lock);
1745 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1746 if (mon_fdset->id != fdset_id) {
1747 continue;
1749 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1750 if (mon_fdset_fd_dup->fd == dup_fd) {
1751 goto err;
1754 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1755 mon_fdset_fd_dup->fd = dup_fd;
1756 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1757 qemu_mutex_unlock(&mon_fdsets_lock);
1758 return 0;
1761 err:
1762 qemu_mutex_unlock(&mon_fdsets_lock);
1763 return -1;
1766 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1768 MonFdset *mon_fdset;
1769 MonFdsetFd *mon_fdset_fd_dup;
1771 qemu_mutex_lock(&mon_fdsets_lock);
1772 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1773 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1774 if (mon_fdset_fd_dup->fd == dup_fd) {
1775 if (remove) {
1776 QLIST_REMOVE(mon_fdset_fd_dup, next);
1777 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1778 monitor_fdset_cleanup(mon_fdset);
1780 goto err;
1781 } else {
1782 qemu_mutex_unlock(&mon_fdsets_lock);
1783 return mon_fdset->id;
1789 err:
1790 qemu_mutex_unlock(&mon_fdsets_lock);
1791 return -1;
1794 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1796 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1799 void monitor_fdset_dup_fd_remove(int dup_fd)
1801 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1804 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1806 int fd;
1807 Error *local_err = NULL;
1809 if (!qemu_isdigit(fdname[0]) && mon) {
1810 fd = monitor_get_fd(mon, fdname, &local_err);
1811 } else {
1812 fd = qemu_parse_fd(fdname);
1813 if (fd == -1) {
1814 error_setg(&local_err, "Invalid file descriptor number '%s'",
1815 fdname);
1818 if (local_err) {
1819 error_propagate(errp, local_err);
1820 assert(fd == -1);
1821 } else {
1822 assert(fd != -1);
1825 return fd;
1828 /* Please update hmp-commands.hx when adding or changing commands */
1829 static HMPCommand hmp_info_cmds[] = {
1830 #include "hmp-commands-info.h"
1831 { NULL, NULL, },
1834 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1835 HMPCommand hmp_cmds[] = {
1836 #include "hmp-commands.h"
1837 { NULL, NULL, },
1841 * Set @pval to the value in the register identified by @name.
1842 * return 0 if OK, -1 if not found
1844 int get_monitor_def(int64_t *pval, const char *name)
1846 const MonitorDef *md = target_monitor_defs();
1847 CPUState *cs = mon_get_cpu();
1848 void *ptr;
1849 uint64_t tmp = 0;
1850 int ret;
1852 if (cs == NULL || md == NULL) {
1853 return -1;
1856 for(; md->name != NULL; md++) {
1857 if (hmp_compare_cmd(name, md->name)) {
1858 if (md->get_value) {
1859 *pval = md->get_value(md, md->offset);
1860 } else {
1861 CPUArchState *env = mon_get_cpu_env();
1862 ptr = (uint8_t *)env + md->offset;
1863 switch(md->type) {
1864 case MD_I32:
1865 *pval = *(int32_t *)ptr;
1866 break;
1867 case MD_TLONG:
1868 *pval = *(target_long *)ptr;
1869 break;
1870 default:
1871 *pval = 0;
1872 break;
1875 return 0;
1879 ret = target_get_monitor_def(cs, name, &tmp);
1880 if (!ret) {
1881 *pval = (target_long) tmp;
1884 return ret;
1887 static void add_completion_option(ReadLineState *rs, const char *str,
1888 const char *option)
1890 if (!str || !option) {
1891 return;
1893 if (!strncmp(option, str, strlen(str))) {
1894 readline_add_completion(rs, option);
1898 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1900 size_t len;
1901 ChardevBackendInfoList *list, *start;
1903 if (nb_args != 2) {
1904 return;
1906 len = strlen(str);
1907 readline_set_completion_index(rs, len);
1909 start = list = qmp_query_chardev_backends(NULL);
1910 while (list) {
1911 const char *chr_name = list->value->name;
1913 if (!strncmp(chr_name, str, len)) {
1914 readline_add_completion(rs, chr_name);
1916 list = list->next;
1918 qapi_free_ChardevBackendInfoList(start);
1921 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1923 size_t len;
1924 int i;
1926 if (nb_args != 2) {
1927 return;
1929 len = strlen(str);
1930 readline_set_completion_index(rs, len);
1931 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1932 add_completion_option(rs, str, NetClientDriver_str(i));
1936 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1938 GSList *list, *elt;
1939 size_t len;
1941 if (nb_args != 2) {
1942 return;
1945 len = strlen(str);
1946 readline_set_completion_index(rs, len);
1947 list = elt = object_class_get_list(TYPE_DEVICE, false);
1948 while (elt) {
1949 const char *name;
1950 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1951 TYPE_DEVICE);
1952 name = object_class_get_name(OBJECT_CLASS(dc));
1954 if (dc->user_creatable
1955 && !strncmp(name, str, len)) {
1956 readline_add_completion(rs, name);
1958 elt = elt->next;
1960 g_slist_free(list);
1963 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1965 GSList *list, *elt;
1966 size_t len;
1968 if (nb_args != 2) {
1969 return;
1972 len = strlen(str);
1973 readline_set_completion_index(rs, len);
1974 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1975 while (elt) {
1976 const char *name;
1978 name = object_class_get_name(OBJECT_CLASS(elt->data));
1979 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1980 readline_add_completion(rs, name);
1982 elt = elt->next;
1984 g_slist_free(list);
1987 static void peripheral_device_del_completion(ReadLineState *rs,
1988 const char *str, size_t len)
1990 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1991 GSList *list, *item;
1993 list = qdev_build_hotpluggable_device_list(peripheral);
1994 if (!list) {
1995 return;
1998 for (item = list; item; item = g_slist_next(item)) {
1999 DeviceState *dev = item->data;
2001 if (dev->id && !strncmp(str, dev->id, len)) {
2002 readline_add_completion(rs, dev->id);
2006 g_slist_free(list);
2009 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
2011 size_t len;
2012 ChardevInfoList *list, *start;
2014 if (nb_args != 2) {
2015 return;
2017 len = strlen(str);
2018 readline_set_completion_index(rs, len);
2020 start = list = qmp_query_chardev(NULL);
2021 while (list) {
2022 ChardevInfo *chr = list->value;
2024 if (!strncmp(chr->label, str, len)) {
2025 readline_add_completion(rs, chr->label);
2027 list = list->next;
2029 qapi_free_ChardevInfoList(start);
2032 static void ringbuf_completion(ReadLineState *rs, const char *str)
2034 size_t len;
2035 ChardevInfoList *list, *start;
2037 len = strlen(str);
2038 readline_set_completion_index(rs, len);
2040 start = list = qmp_query_chardev(NULL);
2041 while (list) {
2042 ChardevInfo *chr_info = list->value;
2044 if (!strncmp(chr_info->label, str, len)) {
2045 Chardev *chr = qemu_chr_find(chr_info->label);
2046 if (chr && CHARDEV_IS_RINGBUF(chr)) {
2047 readline_add_completion(rs, chr_info->label);
2050 list = list->next;
2052 qapi_free_ChardevInfoList(start);
2055 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
2057 if (nb_args != 2) {
2058 return;
2060 ringbuf_completion(rs, str);
2063 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
2065 size_t len;
2067 if (nb_args != 2) {
2068 return;
2071 len = strlen(str);
2072 readline_set_completion_index(rs, len);
2073 peripheral_device_del_completion(rs, str, len);
2076 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
2078 ObjectPropertyInfoList *list, *start;
2079 size_t len;
2081 if (nb_args != 2) {
2082 return;
2084 len = strlen(str);
2085 readline_set_completion_index(rs, len);
2087 start = list = qmp_qom_list("/objects", NULL);
2088 while (list) {
2089 ObjectPropertyInfo *info = list->value;
2091 if (!strncmp(info->type, "child<", 5)
2092 && !strncmp(info->name, str, len)) {
2093 readline_add_completion(rs, info->name);
2095 list = list->next;
2097 qapi_free_ObjectPropertyInfoList(start);
2100 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
2102 int i;
2103 char *sep;
2104 size_t len;
2106 if (nb_args != 2) {
2107 return;
2109 sep = strrchr(str, '-');
2110 if (sep) {
2111 str = sep + 1;
2113 len = strlen(str);
2114 readline_set_completion_index(rs, len);
2115 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
2116 if (!strncmp(str, QKeyCode_str(i), len)) {
2117 readline_add_completion(rs, QKeyCode_str(i));
2122 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
2124 size_t len;
2126 len = strlen(str);
2127 readline_set_completion_index(rs, len);
2128 if (nb_args == 2) {
2129 NetClientState *ncs[MAX_QUEUE_NUM];
2130 int count, i;
2131 count = qemu_find_net_clients_except(NULL, ncs,
2132 NET_CLIENT_DRIVER_NONE,
2133 MAX_QUEUE_NUM);
2134 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2135 const char *name = ncs[i]->name;
2136 if (!strncmp(str, name, len)) {
2137 readline_add_completion(rs, name);
2140 } else if (nb_args == 3) {
2141 add_completion_option(rs, str, "on");
2142 add_completion_option(rs, str, "off");
2146 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
2148 int len, count, i;
2149 NetClientState *ncs[MAX_QUEUE_NUM];
2151 if (nb_args != 2) {
2152 return;
2155 len = strlen(str);
2156 readline_set_completion_index(rs, len);
2157 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
2158 MAX_QUEUE_NUM);
2159 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2160 QemuOpts *opts;
2161 const char *name = ncs[i]->name;
2162 if (strncmp(str, name, len)) {
2163 continue;
2165 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
2166 if (opts) {
2167 readline_add_completion(rs, name);
2172 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2174 size_t len;
2176 len = strlen(str);
2177 readline_set_completion_index(rs, len);
2178 if (nb_args == 2) {
2179 TraceEventIter iter;
2180 TraceEvent *ev;
2181 char *pattern = g_strdup_printf("%s*", str);
2182 trace_event_iter_init(&iter, pattern);
2183 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2184 readline_add_completion(rs, trace_event_get_name(ev));
2186 g_free(pattern);
2190 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2192 size_t len;
2194 len = strlen(str);
2195 readline_set_completion_index(rs, len);
2196 if (nb_args == 2) {
2197 TraceEventIter iter;
2198 TraceEvent *ev;
2199 char *pattern = g_strdup_printf("%s*", str);
2200 trace_event_iter_init(&iter, pattern);
2201 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2202 readline_add_completion(rs, trace_event_get_name(ev));
2204 g_free(pattern);
2205 } else if (nb_args == 3) {
2206 add_completion_option(rs, str, "on");
2207 add_completion_option(rs, str, "off");
2211 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2213 int i;
2215 if (nb_args != 2) {
2216 return;
2218 readline_set_completion_index(rs, strlen(str));
2219 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2220 add_completion_option(rs, str, WatchdogAction_str(i));
2224 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2225 const char *str)
2227 size_t len;
2229 len = strlen(str);
2230 readline_set_completion_index(rs, len);
2231 if (nb_args == 2) {
2232 int i;
2233 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2234 const char *name = MigrationCapability_str(i);
2235 if (!strncmp(str, name, len)) {
2236 readline_add_completion(rs, name);
2239 } else if (nb_args == 3) {
2240 add_completion_option(rs, str, "on");
2241 add_completion_option(rs, str, "off");
2245 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2246 const char *str)
2248 size_t len;
2250 len = strlen(str);
2251 readline_set_completion_index(rs, len);
2252 if (nb_args == 2) {
2253 int i;
2254 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
2255 const char *name = MigrationParameter_str(i);
2256 if (!strncmp(str, name, len)) {
2257 readline_add_completion(rs, name);
2263 static void vm_completion(ReadLineState *rs, const char *str)
2265 size_t len;
2266 BlockDriverState *bs;
2267 BdrvNextIterator it;
2269 len = strlen(str);
2270 readline_set_completion_index(rs, len);
2272 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2273 SnapshotInfoList *snapshots, *snapshot;
2274 AioContext *ctx = bdrv_get_aio_context(bs);
2275 bool ok = false;
2277 aio_context_acquire(ctx);
2278 if (bdrv_can_snapshot(bs)) {
2279 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2281 aio_context_release(ctx);
2282 if (!ok) {
2283 continue;
2286 snapshot = snapshots;
2287 while (snapshot) {
2288 char *completion = snapshot->value->name;
2289 if (!strncmp(str, completion, len)) {
2290 readline_add_completion(rs, completion);
2292 completion = snapshot->value->id;
2293 if (!strncmp(str, completion, len)) {
2294 readline_add_completion(rs, completion);
2296 snapshot = snapshot->next;
2298 qapi_free_SnapshotInfoList(snapshots);
2303 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2305 if (nb_args == 2) {
2306 vm_completion(rs, str);
2310 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2312 if (nb_args == 2) {
2313 vm_completion(rs, str);
2317 static int
2318 compare_mon_cmd(const void *a, const void *b)
2320 return strcmp(((const HMPCommand *)a)->name,
2321 ((const HMPCommand *)b)->name);
2324 static void sortcmdlist(void)
2326 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2327 sizeof(*hmp_cmds),
2328 compare_mon_cmd);
2329 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2330 sizeof(*hmp_info_cmds),
2331 compare_mon_cmd);
2334 void monitor_init_globals(void)
2336 monitor_init_globals_core();
2337 monitor_init_qmp_commands();
2338 sortcmdlist();
2339 qemu_mutex_init(&mon_fdsets_lock);
2342 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
2344 MachineState *ms = MACHINE(qdev_get_machine());
2345 MachineClass *mc = MACHINE_GET_CLASS(ms);
2347 if (!mc->has_hotpluggable_cpus) {
2348 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
2349 return NULL;
2352 return machine_query_hotpluggable_cpus(ms);