Merge remote-tracking branch 'remotes/kraxel/tags/vga-20191220-pull-request' into...
[qemu/ar7.git] / monitor / misc.c
bloba04d7edde09acc00e1f55eebc63a88cc7e2d2233
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "cpu.h"
28 #include "monitor/qdev.h"
29 #include "hw/usb.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
34 #include "net/net.h"
35 #include "net/slirp.h"
36 #include "chardev/char-mux.h"
37 #include "ui/qemu-spice.h"
38 #include "qemu/config-file.h"
39 #include "qemu/ctype.h"
40 #include "ui/console.h"
41 #include "ui/input.h"
42 #include "audio/audio.h"
43 #include "disas/disas.h"
44 #include "sysemu/balloon.h"
45 #include "qemu/timer.h"
46 #include "sysemu/hw_accel.h"
47 #include "sysemu/runstate.h"
48 #include "authz/list.h"
49 #include "qapi/util.h"
50 #include "sysemu/blockdev.h"
51 #include "sysemu/sysemu.h"
52 #include "sysemu/tcg.h"
53 #include "sysemu/tpm.h"
54 #include "qapi/qmp/qdict.h"
55 #include "qapi/qmp/qerror.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qom/object_interfaces.h"
58 #include "trace/control.h"
59 #include "monitor/hmp-target.h"
60 #include "monitor/hmp.h"
61 #ifdef CONFIG_TRACE_SIMPLE
62 #include "trace/simple.h"
63 #endif
64 #include "exec/memory.h"
65 #include "exec/exec-all.h"
66 #include "qemu/option.h"
67 #include "qemu/thread.h"
68 #include "block/qapi.h"
69 #include "qapi/qapi-commands.h"
70 #include "qapi/qapi-emit-events.h"
71 #include "qapi/error.h"
72 #include "qapi/qmp-event.h"
73 #include "qapi/qapi-introspect.h"
74 #include "sysemu/cpus.h"
75 #include "qemu/cutils.h"
76 #include "tcg/tcg.h"
78 #if defined(TARGET_S390X)
79 #include "hw/s390x/storage-keys.h"
80 #include "hw/s390x/storage-attributes.h"
81 #endif
83 /* file descriptors passed via SCM_RIGHTS */
84 typedef struct mon_fd_t mon_fd_t;
85 struct mon_fd_t {
86 char *name;
87 int fd;
88 QLIST_ENTRY(mon_fd_t) next;
91 /* file descriptor associated with a file descriptor set */
92 typedef struct MonFdsetFd MonFdsetFd;
93 struct MonFdsetFd {
94 int fd;
95 bool removed;
96 char *opaque;
97 QLIST_ENTRY(MonFdsetFd) next;
100 /* file descriptor set containing fds passed via SCM_RIGHTS */
101 typedef struct MonFdset MonFdset;
102 struct MonFdset {
103 int64_t id;
104 QLIST_HEAD(, MonFdsetFd) fds;
105 QLIST_HEAD(, MonFdsetFd) dup_fds;
106 QLIST_ENTRY(MonFdset) next;
109 /* Protects mon_fdsets */
110 static QemuMutex mon_fdsets_lock;
111 static QLIST_HEAD(, MonFdset) mon_fdsets;
113 static HMPCommand hmp_info_cmds[];
115 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
116 int64_t cpu_index, Error **errp)
118 char *output = NULL;
119 Monitor *old_mon;
120 MonitorHMP hmp = {};
122 monitor_data_init(&hmp.common, false, true, false);
124 old_mon = cur_mon;
125 cur_mon = &hmp.common;
127 if (has_cpu_index) {
128 int ret = monitor_set_cpu(cpu_index);
129 if (ret < 0) {
130 cur_mon = old_mon;
131 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
132 "a CPU number");
133 goto out;
137 handle_hmp_command(&hmp, command_line);
138 cur_mon = old_mon;
140 qemu_mutex_lock(&hmp.common.mon_lock);
141 if (qstring_get_length(hmp.common.outbuf) > 0) {
142 output = g_strdup(qstring_get_str(hmp.common.outbuf));
143 } else {
144 output = g_strdup("");
146 qemu_mutex_unlock(&hmp.common.mon_lock);
148 out:
149 monitor_data_destroy(&hmp.common);
150 return output;
154 * Is @name in the '|' separated list of names @list?
156 int hmp_compare_cmd(const char *name, const char *list)
158 const char *p, *pstart;
159 int len;
160 len = strlen(name);
161 p = list;
162 for (;;) {
163 pstart = p;
164 p = qemu_strchrnul(p, '|');
165 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
166 return 1;
168 if (*p == '\0') {
169 break;
171 p++;
173 return 0;
176 static void do_help_cmd(Monitor *mon, const QDict *qdict)
178 help_cmd(mon, qdict_get_try_str(qdict, "name"));
181 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
183 const char *tp_name = qdict_get_str(qdict, "name");
184 bool new_state = qdict_get_bool(qdict, "option");
185 bool has_vcpu = qdict_haskey(qdict, "vcpu");
186 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
187 Error *local_err = NULL;
189 if (vcpu < 0) {
190 monitor_printf(mon, "argument vcpu must be positive");
191 return;
194 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
195 if (local_err) {
196 error_report_err(local_err);
200 #ifdef CONFIG_TRACE_SIMPLE
201 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
203 const char *op = qdict_get_try_str(qdict, "op");
204 const char *arg = qdict_get_try_str(qdict, "arg");
206 if (!op) {
207 st_print_trace_file_status();
208 } else if (!strcmp(op, "on")) {
209 st_set_trace_file_enabled(true);
210 } else if (!strcmp(op, "off")) {
211 st_set_trace_file_enabled(false);
212 } else if (!strcmp(op, "flush")) {
213 st_flush_trace_buffer();
214 } else if (!strcmp(op, "set")) {
215 if (arg) {
216 st_set_trace_file(arg);
218 } else {
219 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
220 help_cmd(mon, "trace-file");
223 #endif
225 static void hmp_info_help(Monitor *mon, const QDict *qdict)
227 help_cmd(mon, "info");
230 static void query_commands_cb(QmpCommand *cmd, void *opaque)
232 CommandInfoList *info, **list = opaque;
234 if (!cmd->enabled) {
235 return;
238 info = g_malloc0(sizeof(*info));
239 info->value = g_malloc0(sizeof(*info->value));
240 info->value->name = g_strdup(cmd->name);
241 info->next = *list;
242 *list = info;
245 CommandInfoList *qmp_query_commands(Error **errp)
247 CommandInfoList *list = NULL;
248 MonitorQMP *mon;
250 assert(monitor_is_qmp(cur_mon));
251 mon = container_of(cur_mon, MonitorQMP, common);
253 qmp_for_each_command(mon->commands, query_commands_cb, &list);
255 return list;
258 EventInfoList *qmp_query_events(Error **errp)
261 * TODO This deprecated command is the only user of
262 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
263 * they should go, too.
265 EventInfoList *info, *ev_list = NULL;
266 QAPIEvent e;
268 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
269 const char *event_name = QAPIEvent_str(e);
270 assert(event_name != NULL);
271 info = g_malloc0(sizeof(*info));
272 info->value = g_malloc0(sizeof(*info->value));
273 info->value->name = g_strdup(event_name);
275 info->next = ev_list;
276 ev_list = info;
279 return ev_list;
283 * Minor hack: generated marshalling suppressed for this command
284 * ('gen': false in the schema) so we can parse the JSON string
285 * directly into QObject instead of first parsing it with
286 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
287 * to QObject with generated output marshallers, every time. Instead,
288 * we do it in test-qobject-input-visitor.c, just to make sure
289 * qapi-gen.py's output actually conforms to the schema.
291 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
292 Error **errp)
294 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
297 static void monitor_init_qmp_commands(void)
300 * Two command lists:
301 * - qmp_commands contains all QMP commands
302 * - qmp_cap_negotiation_commands contains just
303 * "qmp_capabilities", to enforce capability negotiation
306 qmp_init_marshal(&qmp_commands);
308 qmp_register_command(&qmp_commands, "query-qmp-schema",
309 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
310 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
311 QCO_NO_OPTIONS);
312 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
313 QCO_NO_OPTIONS);
315 QTAILQ_INIT(&qmp_cap_negotiation_commands);
316 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
317 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
321 * Accept QMP capabilities in @list for @mon.
322 * On success, set mon->qmp.capab[], and return true.
323 * On error, set @errp, and return false.
325 static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
326 Error **errp)
328 GString *unavailable = NULL;
329 bool capab[QMP_CAPABILITY__MAX];
331 memset(capab, 0, sizeof(capab));
333 for (; list; list = list->next) {
334 if (!mon->capab_offered[list->value]) {
335 if (!unavailable) {
336 unavailable = g_string_new(QMPCapability_str(list->value));
337 } else {
338 g_string_append_printf(unavailable, ", %s",
339 QMPCapability_str(list->value));
342 capab[list->value] = true;
345 if (unavailable) {
346 error_setg(errp, "Capability %s not available", unavailable->str);
347 g_string_free(unavailable, true);
348 return false;
351 memcpy(mon->capab, capab, sizeof(capab));
352 return true;
355 void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
356 Error **errp)
358 MonitorQMP *mon;
360 assert(monitor_is_qmp(cur_mon));
361 mon = container_of(cur_mon, MonitorQMP, common);
363 if (mon->commands == &qmp_commands) {
364 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
365 "Capabilities negotiation is already complete, command "
366 "ignored");
367 return;
370 if (!qmp_caps_accept(mon, enable, errp)) {
371 return;
374 mon->commands = &qmp_commands;
377 /* Set the current CPU defined by the user. Callers must hold BQL. */
378 int monitor_set_cpu(int cpu_index)
380 CPUState *cpu;
382 cpu = qemu_get_cpu(cpu_index);
383 if (cpu == NULL) {
384 return -1;
386 g_free(cur_mon->mon_cpu_path);
387 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
388 return 0;
391 /* Callers must hold BQL. */
392 static CPUState *mon_get_cpu_sync(bool synchronize)
394 CPUState *cpu = NULL;
396 if (cur_mon->mon_cpu_path) {
397 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
398 TYPE_CPU, NULL);
399 if (!cpu) {
400 g_free(cur_mon->mon_cpu_path);
401 cur_mon->mon_cpu_path = NULL;
404 if (!cur_mon->mon_cpu_path) {
405 if (!first_cpu) {
406 return NULL;
408 monitor_set_cpu(first_cpu->cpu_index);
409 cpu = first_cpu;
411 assert(cpu != NULL);
412 if (synchronize) {
413 cpu_synchronize_state(cpu);
415 return cpu;
418 CPUState *mon_get_cpu(void)
420 return mon_get_cpu_sync(true);
423 CPUArchState *mon_get_cpu_env(void)
425 CPUState *cs = mon_get_cpu();
427 return cs ? cs->env_ptr : NULL;
430 int monitor_get_cpu_index(void)
432 CPUState *cs = mon_get_cpu_sync(false);
434 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
437 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
439 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
440 CPUState *cs;
442 if (all_cpus) {
443 CPU_FOREACH(cs) {
444 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
445 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
447 } else {
448 cs = mon_get_cpu();
450 if (!cs) {
451 monitor_printf(mon, "No CPU available\n");
452 return;
455 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
459 #ifdef CONFIG_TCG
460 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
462 if (!tcg_enabled()) {
463 error_report("JIT information is only available with accel=tcg");
464 return;
467 dump_exec_info();
468 dump_drift_info();
471 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
473 dump_opcount_info();
475 #endif
477 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
479 int64_t max = qdict_get_try_int(qdict, "max", 10);
480 bool mean = qdict_get_try_bool(qdict, "mean", false);
481 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
482 enum QSPSortBy sort_by;
484 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
485 qsp_report(max, sort_by, coalesce);
488 static void hmp_info_history(Monitor *mon, const QDict *qdict)
490 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
491 int i;
492 const char *str;
494 if (!hmp_mon->rs) {
495 return;
497 i = 0;
498 for(;;) {
499 str = readline_get_history(hmp_mon->rs, i);
500 if (!str) {
501 break;
503 monitor_printf(mon, "%d: '%s'\n", i, str);
504 i++;
508 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
510 CPUState *cs = mon_get_cpu();
512 if (!cs) {
513 monitor_printf(mon, "No CPU available\n");
514 return;
516 cpu_dump_statistics(cs, 0);
519 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
521 const char *name = qdict_get_try_str(qdict, "name");
522 bool has_vcpu = qdict_haskey(qdict, "vcpu");
523 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
524 TraceEventInfoList *events;
525 TraceEventInfoList *elem;
526 Error *local_err = NULL;
528 if (name == NULL) {
529 name = "*";
531 if (vcpu < 0) {
532 monitor_printf(mon, "argument vcpu must be positive");
533 return;
536 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
537 if (local_err) {
538 error_report_err(local_err);
539 return;
542 for (elem = events; elem != NULL; elem = elem->next) {
543 monitor_printf(mon, "%s : state %u\n",
544 elem->value->name,
545 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
547 qapi_free_TraceEventInfoList(events);
550 void qmp_client_migrate_info(const char *protocol, const char *hostname,
551 bool has_port, int64_t port,
552 bool has_tls_port, int64_t tls_port,
553 bool has_cert_subject, const char *cert_subject,
554 Error **errp)
556 if (strcmp(protocol, "spice") == 0) {
557 if (!qemu_using_spice(errp)) {
558 return;
561 if (!has_port && !has_tls_port) {
562 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
563 return;
566 if (qemu_spice_migrate_info(hostname,
567 has_port ? port : -1,
568 has_tls_port ? tls_port : -1,
569 cert_subject)) {
570 error_setg(errp, QERR_UNDEFINED_ERROR);
571 return;
573 return;
576 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
579 static void hmp_logfile(Monitor *mon, const QDict *qdict)
581 Error *err = NULL;
583 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
584 if (err) {
585 error_report_err(err);
589 static void hmp_log(Monitor *mon, const QDict *qdict)
591 int mask;
592 const char *items = qdict_get_str(qdict, "items");
594 if (!strcmp(items, "none")) {
595 mask = 0;
596 } else {
597 mask = qemu_str_to_log_mask(items);
598 if (!mask) {
599 help_cmd(mon, "log");
600 return;
603 qemu_set_log(mask);
606 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
608 const char *option = qdict_get_try_str(qdict, "option");
609 if (!option || !strcmp(option, "on")) {
610 singlestep = 1;
611 } else if (!strcmp(option, "off")) {
612 singlestep = 0;
613 } else {
614 monitor_printf(mon, "unexpected option %s\n", option);
618 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
620 const char *device = qdict_get_try_str(qdict, "device");
621 if (!device)
622 device = "tcp::" DEFAULT_GDBSTUB_PORT;
623 if (gdbserver_start(device) < 0) {
624 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
625 device);
626 } else if (strcmp(device, "none") == 0) {
627 monitor_printf(mon, "Disabled gdbserver\n");
628 } else {
629 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
630 device);
634 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
636 const char *action = qdict_get_str(qdict, "action");
637 if (select_watchdog_action(action) == -1) {
638 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
642 static void monitor_printc(Monitor *mon, int c)
644 monitor_printf(mon, "'");
645 switch(c) {
646 case '\'':
647 monitor_printf(mon, "\\'");
648 break;
649 case '\\':
650 monitor_printf(mon, "\\\\");
651 break;
652 case '\n':
653 monitor_printf(mon, "\\n");
654 break;
655 case '\r':
656 monitor_printf(mon, "\\r");
657 break;
658 default:
659 if (c >= 32 && c <= 126) {
660 monitor_printf(mon, "%c", c);
661 } else {
662 monitor_printf(mon, "\\x%02x", c);
664 break;
666 monitor_printf(mon, "'");
669 static void memory_dump(Monitor *mon, int count, int format, int wsize,
670 hwaddr addr, int is_physical)
672 int l, line_size, i, max_digits, len;
673 uint8_t buf[16];
674 uint64_t v;
675 CPUState *cs = mon_get_cpu();
677 if (!cs && (format == 'i' || !is_physical)) {
678 monitor_printf(mon, "Can not dump without CPU\n");
679 return;
682 if (format == 'i') {
683 monitor_disas(mon, cs, addr, count, is_physical);
684 return;
687 len = wsize * count;
688 if (wsize == 1)
689 line_size = 8;
690 else
691 line_size = 16;
692 max_digits = 0;
694 switch(format) {
695 case 'o':
696 max_digits = DIV_ROUND_UP(wsize * 8, 3);
697 break;
698 default:
699 case 'x':
700 max_digits = (wsize * 8) / 4;
701 break;
702 case 'u':
703 case 'd':
704 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
705 break;
706 case 'c':
707 wsize = 1;
708 break;
711 while (len > 0) {
712 if (is_physical)
713 monitor_printf(mon, TARGET_FMT_plx ":", addr);
714 else
715 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
716 l = len;
717 if (l > line_size)
718 l = line_size;
719 if (is_physical) {
720 AddressSpace *as = cs ? cs->as : &address_space_memory;
721 MemTxResult r = address_space_read(as, addr,
722 MEMTXATTRS_UNSPECIFIED, buf, l);
723 if (r != MEMTX_OK) {
724 monitor_printf(mon, " Cannot access memory\n");
725 break;
727 } else {
728 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
729 monitor_printf(mon, " Cannot access memory\n");
730 break;
733 i = 0;
734 while (i < l) {
735 switch(wsize) {
736 default:
737 case 1:
738 v = ldub_p(buf + i);
739 break;
740 case 2:
741 v = lduw_p(buf + i);
742 break;
743 case 4:
744 v = (uint32_t)ldl_p(buf + i);
745 break;
746 case 8:
747 v = ldq_p(buf + i);
748 break;
750 monitor_printf(mon, " ");
751 switch(format) {
752 case 'o':
753 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
754 break;
755 case 'x':
756 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
757 break;
758 case 'u':
759 monitor_printf(mon, "%*" PRIu64, max_digits, v);
760 break;
761 case 'd':
762 monitor_printf(mon, "%*" PRId64, max_digits, v);
763 break;
764 case 'c':
765 monitor_printc(mon, v);
766 break;
768 i += wsize;
770 monitor_printf(mon, "\n");
771 addr += l;
772 len -= l;
776 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
778 int count = qdict_get_int(qdict, "count");
779 int format = qdict_get_int(qdict, "format");
780 int size = qdict_get_int(qdict, "size");
781 target_long addr = qdict_get_int(qdict, "addr");
783 memory_dump(mon, count, format, size, addr, 0);
786 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
788 int count = qdict_get_int(qdict, "count");
789 int format = qdict_get_int(qdict, "format");
790 int size = qdict_get_int(qdict, "size");
791 hwaddr addr = qdict_get_int(qdict, "addr");
793 memory_dump(mon, count, format, size, addr, 1);
796 static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
798 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
799 addr, 1);
801 if (!mrs.mr) {
802 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
803 return NULL;
806 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
807 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
808 memory_region_unref(mrs.mr);
809 return NULL;
812 *p_mr = mrs.mr;
813 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
816 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
818 hwaddr addr = qdict_get_int(qdict, "addr");
819 Error *local_err = NULL;
820 MemoryRegion *mr = NULL;
821 void *ptr;
823 ptr = gpa2hva(&mr, addr, &local_err);
824 if (local_err) {
825 error_report_err(local_err);
826 return;
829 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
830 " (%s) is %p\n",
831 addr, mr->name, ptr);
833 memory_region_unref(mr);
836 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
838 target_ulong addr = qdict_get_int(qdict, "addr");
839 MemTxAttrs attrs;
840 CPUState *cs = mon_get_cpu();
841 hwaddr gpa;
843 if (!cs) {
844 monitor_printf(mon, "No cpu\n");
845 return;
848 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
849 if (gpa == -1) {
850 monitor_printf(mon, "Unmapped\n");
851 } else {
852 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
853 gpa + (addr & ~TARGET_PAGE_MASK));
857 #ifdef CONFIG_LINUX
858 static uint64_t vtop(void *ptr, Error **errp)
860 uint64_t pinfo;
861 uint64_t ret = -1;
862 uintptr_t addr = (uintptr_t) ptr;
863 uintptr_t pagesize = qemu_real_host_page_size;
864 off_t offset = addr / pagesize * sizeof(pinfo);
865 int fd;
867 fd = open("/proc/self/pagemap", O_RDONLY);
868 if (fd == -1) {
869 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
870 return -1;
873 /* Force copy-on-write if necessary. */
874 atomic_add((uint8_t *)ptr, 0);
876 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
877 error_setg_errno(errp, errno, "Cannot read pagemap");
878 goto out;
880 if ((pinfo & (1ull << 63)) == 0) {
881 error_setg(errp, "Page not present");
882 goto out;
884 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
886 out:
887 close(fd);
888 return ret;
891 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
893 hwaddr addr = qdict_get_int(qdict, "addr");
894 Error *local_err = NULL;
895 MemoryRegion *mr = NULL;
896 void *ptr;
897 uint64_t physaddr;
899 ptr = gpa2hva(&mr, addr, &local_err);
900 if (local_err) {
901 error_report_err(local_err);
902 return;
905 physaddr = vtop(ptr, &local_err);
906 if (local_err) {
907 error_report_err(local_err);
908 } else {
909 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
910 " (%s) is 0x%" PRIx64 "\n",
911 addr, mr->name, (uint64_t) physaddr);
914 memory_region_unref(mr);
916 #endif
918 static void do_print(Monitor *mon, const QDict *qdict)
920 int format = qdict_get_int(qdict, "format");
921 hwaddr val = qdict_get_int(qdict, "val");
923 switch(format) {
924 case 'o':
925 monitor_printf(mon, "%#" HWADDR_PRIo, val);
926 break;
927 case 'x':
928 monitor_printf(mon, "%#" HWADDR_PRIx, val);
929 break;
930 case 'u':
931 monitor_printf(mon, "%" HWADDR_PRIu, val);
932 break;
933 default:
934 case 'd':
935 monitor_printf(mon, "%" HWADDR_PRId, val);
936 break;
937 case 'c':
938 monitor_printc(mon, val);
939 break;
941 monitor_printf(mon, "\n");
944 static void hmp_sum(Monitor *mon, const QDict *qdict)
946 uint32_t addr;
947 uint16_t sum;
948 uint32_t start = qdict_get_int(qdict, "start");
949 uint32_t size = qdict_get_int(qdict, "size");
951 sum = 0;
952 for(addr = start; addr < (start + size); addr++) {
953 uint8_t val = address_space_ldub(&address_space_memory, addr,
954 MEMTXATTRS_UNSPECIFIED, NULL);
955 /* BSD sum algorithm ('sum' Unix command) */
956 sum = (sum >> 1) | (sum << 15);
957 sum += val;
959 monitor_printf(mon, "%05d\n", sum);
962 static int mouse_button_state;
964 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
966 int dx, dy, dz, button;
967 const char *dx_str = qdict_get_str(qdict, "dx_str");
968 const char *dy_str = qdict_get_str(qdict, "dy_str");
969 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
971 dx = strtol(dx_str, NULL, 0);
972 dy = strtol(dy_str, NULL, 0);
973 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
974 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
976 if (dz_str) {
977 dz = strtol(dz_str, NULL, 0);
978 if (dz != 0) {
979 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
980 qemu_input_queue_btn(NULL, button, true);
981 qemu_input_event_sync();
982 qemu_input_queue_btn(NULL, button, false);
985 qemu_input_event_sync();
988 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
990 static uint32_t bmap[INPUT_BUTTON__MAX] = {
991 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
992 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
993 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
995 int button_state = qdict_get_int(qdict, "button_state");
997 if (mouse_button_state == button_state) {
998 return;
1000 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1001 qemu_input_event_sync();
1002 mouse_button_state = button_state;
1005 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1007 int size = qdict_get_int(qdict, "size");
1008 int addr = qdict_get_int(qdict, "addr");
1009 int has_index = qdict_haskey(qdict, "index");
1010 uint32_t val;
1011 int suffix;
1013 if (has_index) {
1014 int index = qdict_get_int(qdict, "index");
1015 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1016 addr++;
1018 addr &= 0xffff;
1020 switch(size) {
1021 default:
1022 case 1:
1023 val = cpu_inb(addr);
1024 suffix = 'b';
1025 break;
1026 case 2:
1027 val = cpu_inw(addr);
1028 suffix = 'w';
1029 break;
1030 case 4:
1031 val = cpu_inl(addr);
1032 suffix = 'l';
1033 break;
1035 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1036 suffix, addr, size * 2, val);
1039 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1041 int size = qdict_get_int(qdict, "size");
1042 int addr = qdict_get_int(qdict, "addr");
1043 int val = qdict_get_int(qdict, "val");
1045 addr &= IOPORTS_MASK;
1047 switch (size) {
1048 default:
1049 case 1:
1050 cpu_outb(addr, val);
1051 break;
1052 case 2:
1053 cpu_outw(addr, val);
1054 break;
1055 case 4:
1056 cpu_outl(addr, val);
1057 break;
1061 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1063 Error *local_err = NULL;
1064 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1066 qemu_boot_set(bootdevice, &local_err);
1067 if (local_err) {
1068 error_report_err(local_err);
1069 } else {
1070 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1074 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1076 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1077 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
1078 bool owner = qdict_get_try_bool(qdict, "owner", false);
1080 mtree_info(flatview, dispatch_tree, owner);
1083 #ifdef CONFIG_PROFILER
1085 int64_t dev_time;
1087 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1089 static int64_t last_cpu_exec_time;
1090 int64_t cpu_exec_time;
1091 int64_t delta;
1093 cpu_exec_time = tcg_cpu_exec_time();
1094 delta = cpu_exec_time - last_cpu_exec_time;
1096 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1097 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1098 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1099 delta, delta / (double)NANOSECONDS_PER_SECOND);
1100 last_cpu_exec_time = cpu_exec_time;
1101 dev_time = 0;
1103 #else
1104 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1106 monitor_printf(mon, "Internal profiler not compiled\n");
1108 #endif
1110 /* Capture support */
1111 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1113 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1115 int i;
1116 CaptureState *s;
1118 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1119 monitor_printf(mon, "[%d]: ", i);
1120 s->ops.info (s->opaque);
1124 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1126 int i;
1127 int n = qdict_get_int(qdict, "n");
1128 CaptureState *s;
1130 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1131 if (i == n) {
1132 s->ops.destroy (s->opaque);
1133 QLIST_REMOVE (s, entries);
1134 g_free (s);
1135 return;
1140 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1142 const char *path = qdict_get_str(qdict, "path");
1143 int freq = qdict_get_try_int(qdict, "freq", 44100);
1144 int bits = qdict_get_try_int(qdict, "bits", 16);
1145 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1146 const char *audiodev = qdict_get_str(qdict, "audiodev");
1147 CaptureState *s;
1148 AudioState *as = audio_state_by_name(audiodev);
1150 if (!as) {
1151 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1152 return;
1155 s = g_malloc0 (sizeof (*s));
1157 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1158 monitor_printf(mon, "Failed to add wave capture\n");
1159 g_free (s);
1160 return;
1162 QLIST_INSERT_HEAD (&capture_head, s, entries);
1165 static QAuthZList *find_auth(Monitor *mon, const char *name)
1167 Object *obj;
1168 Object *container;
1170 container = object_get_objects_root();
1171 obj = object_resolve_path_component(container, name);
1172 if (!obj) {
1173 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1174 return NULL;
1177 return QAUTHZ_LIST(obj);
1180 static bool warn_acl;
1181 static void hmp_warn_acl(void)
1183 if (warn_acl) {
1184 return;
1186 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1187 "commands are deprecated with no replacement. Authorization "
1188 "for VNC should be performed using the pluggable QAuthZ "
1189 "objects");
1190 warn_acl = true;
1193 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1195 const char *aclname = qdict_get_str(qdict, "aclname");
1196 QAuthZList *auth = find_auth(mon, aclname);
1197 QAuthZListRuleList *rules;
1198 size_t i = 0;
1200 hmp_warn_acl();
1202 if (!auth) {
1203 return;
1206 monitor_printf(mon, "policy: %s\n",
1207 QAuthZListPolicy_str(auth->policy));
1209 rules = auth->rules;
1210 while (rules) {
1211 QAuthZListRule *rule = rules->value;
1212 i++;
1213 monitor_printf(mon, "%zu: %s %s\n", i,
1214 QAuthZListPolicy_str(rule->policy),
1215 rule->match);
1216 rules = rules->next;
1220 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1222 const char *aclname = qdict_get_str(qdict, "aclname");
1223 QAuthZList *auth = find_auth(mon, aclname);
1225 hmp_warn_acl();
1227 if (!auth) {
1228 return;
1231 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1232 qapi_free_QAuthZListRuleList(auth->rules);
1233 auth->rules = NULL;
1234 monitor_printf(mon, "acl: removed all rules\n");
1237 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1239 const char *aclname = qdict_get_str(qdict, "aclname");
1240 const char *policy = qdict_get_str(qdict, "policy");
1241 QAuthZList *auth = find_auth(mon, aclname);
1242 int val;
1243 Error *err = NULL;
1245 hmp_warn_acl();
1247 if (!auth) {
1248 return;
1251 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1252 policy,
1253 QAUTHZ_LIST_POLICY_DENY,
1254 &err);
1255 if (err) {
1256 error_free(err);
1257 monitor_printf(mon, "acl: unknown policy '%s', "
1258 "expected 'deny' or 'allow'\n", policy);
1259 } else {
1260 auth->policy = val;
1261 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
1262 monitor_printf(mon, "acl: policy set to 'allow'\n");
1263 } else {
1264 monitor_printf(mon, "acl: policy set to 'deny'\n");
1269 static QAuthZListFormat hmp_acl_get_format(const char *match)
1271 if (strchr(match, '*')) {
1272 return QAUTHZ_LIST_FORMAT_GLOB;
1273 } else {
1274 return QAUTHZ_LIST_FORMAT_EXACT;
1278 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1280 const char *aclname = qdict_get_str(qdict, "aclname");
1281 const char *match = qdict_get_str(qdict, "match");
1282 const char *policystr = qdict_get_str(qdict, "policy");
1283 int has_index = qdict_haskey(qdict, "index");
1284 int index = qdict_get_try_int(qdict, "index", -1);
1285 QAuthZList *auth = find_auth(mon, aclname);
1286 Error *err = NULL;
1287 QAuthZListPolicy policy;
1288 QAuthZListFormat format;
1289 size_t i = 0;
1291 hmp_warn_acl();
1293 if (!auth) {
1294 return;
1297 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1298 policystr,
1299 QAUTHZ_LIST_POLICY_DENY,
1300 &err);
1301 if (err) {
1302 error_free(err);
1303 monitor_printf(mon, "acl: unknown policy '%s', "
1304 "expected 'deny' or 'allow'\n", policystr);
1305 return;
1308 format = hmp_acl_get_format(match);
1310 if (has_index && index == 0) {
1311 monitor_printf(mon, "acl: unable to add acl entry\n");
1312 return;
1315 if (has_index) {
1316 i = qauthz_list_insert_rule(auth, match, policy,
1317 format, index - 1, &err);
1318 } else {
1319 i = qauthz_list_append_rule(auth, match, policy,
1320 format, &err);
1322 if (err) {
1323 monitor_printf(mon, "acl: unable to add rule: %s",
1324 error_get_pretty(err));
1325 error_free(err);
1326 } else {
1327 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
1331 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1333 const char *aclname = qdict_get_str(qdict, "aclname");
1334 const char *match = qdict_get_str(qdict, "match");
1335 QAuthZList *auth = find_auth(mon, aclname);
1336 ssize_t i = 0;
1338 hmp_warn_acl();
1340 if (!auth) {
1341 return;
1344 i = qauthz_list_delete_rule(auth, match);
1345 if (i >= 0) {
1346 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1347 } else {
1348 monitor_printf(mon, "acl: no matching acl entry\n");
1352 void qmp_getfd(const char *fdname, Error **errp)
1354 mon_fd_t *monfd;
1355 int fd, tmp_fd;
1357 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1358 if (fd == -1) {
1359 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1360 return;
1363 if (qemu_isdigit(fdname[0])) {
1364 close(fd);
1365 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1366 "a name not starting with a digit");
1367 return;
1370 qemu_mutex_lock(&cur_mon->mon_lock);
1371 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1372 if (strcmp(monfd->name, fdname) != 0) {
1373 continue;
1376 tmp_fd = monfd->fd;
1377 monfd->fd = fd;
1378 qemu_mutex_unlock(&cur_mon->mon_lock);
1379 /* Make sure close() is outside critical section */
1380 close(tmp_fd);
1381 return;
1384 monfd = g_malloc0(sizeof(mon_fd_t));
1385 monfd->name = g_strdup(fdname);
1386 monfd->fd = fd;
1388 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1389 qemu_mutex_unlock(&cur_mon->mon_lock);
1392 void qmp_closefd(const char *fdname, Error **errp)
1394 mon_fd_t *monfd;
1395 int tmp_fd;
1397 qemu_mutex_lock(&cur_mon->mon_lock);
1398 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1399 if (strcmp(monfd->name, fdname) != 0) {
1400 continue;
1403 QLIST_REMOVE(monfd, next);
1404 tmp_fd = monfd->fd;
1405 g_free(monfd->name);
1406 g_free(monfd);
1407 qemu_mutex_unlock(&cur_mon->mon_lock);
1408 /* Make sure close() is outside critical section */
1409 close(tmp_fd);
1410 return;
1413 qemu_mutex_unlock(&cur_mon->mon_lock);
1414 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1417 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1419 mon_fd_t *monfd;
1421 qemu_mutex_lock(&mon->mon_lock);
1422 QLIST_FOREACH(monfd, &mon->fds, next) {
1423 int fd;
1425 if (strcmp(monfd->name, fdname) != 0) {
1426 continue;
1429 fd = monfd->fd;
1431 /* caller takes ownership of fd */
1432 QLIST_REMOVE(monfd, next);
1433 g_free(monfd->name);
1434 g_free(monfd);
1435 qemu_mutex_unlock(&mon->mon_lock);
1437 return fd;
1440 qemu_mutex_unlock(&mon->mon_lock);
1441 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1442 return -1;
1445 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1447 MonFdsetFd *mon_fdset_fd;
1448 MonFdsetFd *mon_fdset_fd_next;
1450 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1451 if ((mon_fdset_fd->removed ||
1452 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1453 runstate_is_running()) {
1454 close(mon_fdset_fd->fd);
1455 g_free(mon_fdset_fd->opaque);
1456 QLIST_REMOVE(mon_fdset_fd, next);
1457 g_free(mon_fdset_fd);
1461 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1462 QLIST_REMOVE(mon_fdset, next);
1463 g_free(mon_fdset);
1467 void monitor_fdsets_cleanup(void)
1469 MonFdset *mon_fdset;
1470 MonFdset *mon_fdset_next;
1472 qemu_mutex_lock(&mon_fdsets_lock);
1473 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1474 monitor_fdset_cleanup(mon_fdset);
1476 qemu_mutex_unlock(&mon_fdsets_lock);
1479 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1480 const char *opaque, Error **errp)
1482 int fd;
1483 Monitor *mon = cur_mon;
1484 AddfdInfo *fdinfo;
1486 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1487 if (fd == -1) {
1488 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1489 goto error;
1492 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1493 has_opaque, opaque, errp);
1494 if (fdinfo) {
1495 return fdinfo;
1498 error:
1499 if (fd != -1) {
1500 close(fd);
1502 return NULL;
1505 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1507 MonFdset *mon_fdset;
1508 MonFdsetFd *mon_fdset_fd;
1509 char fd_str[60];
1511 qemu_mutex_lock(&mon_fdsets_lock);
1512 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1513 if (mon_fdset->id != fdset_id) {
1514 continue;
1516 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1517 if (has_fd) {
1518 if (mon_fdset_fd->fd != fd) {
1519 continue;
1521 mon_fdset_fd->removed = true;
1522 break;
1523 } else {
1524 mon_fdset_fd->removed = true;
1527 if (has_fd && !mon_fdset_fd) {
1528 goto error;
1530 monitor_fdset_cleanup(mon_fdset);
1531 qemu_mutex_unlock(&mon_fdsets_lock);
1532 return;
1535 error:
1536 qemu_mutex_unlock(&mon_fdsets_lock);
1537 if (has_fd) {
1538 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1539 fdset_id, fd);
1540 } else {
1541 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1543 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1546 FdsetInfoList *qmp_query_fdsets(Error **errp)
1548 MonFdset *mon_fdset;
1549 MonFdsetFd *mon_fdset_fd;
1550 FdsetInfoList *fdset_list = NULL;
1552 qemu_mutex_lock(&mon_fdsets_lock);
1553 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1554 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1555 FdsetFdInfoList *fdsetfd_list = NULL;
1557 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1558 fdset_info->value->fdset_id = mon_fdset->id;
1560 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1561 FdsetFdInfoList *fdsetfd_info;
1563 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1564 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1565 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1566 if (mon_fdset_fd->opaque) {
1567 fdsetfd_info->value->has_opaque = true;
1568 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1569 } else {
1570 fdsetfd_info->value->has_opaque = false;
1573 fdsetfd_info->next = fdsetfd_list;
1574 fdsetfd_list = fdsetfd_info;
1577 fdset_info->value->fds = fdsetfd_list;
1579 fdset_info->next = fdset_list;
1580 fdset_list = fdset_info;
1582 qemu_mutex_unlock(&mon_fdsets_lock);
1584 return fdset_list;
1587 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1588 bool has_opaque, const char *opaque,
1589 Error **errp)
1591 MonFdset *mon_fdset = NULL;
1592 MonFdsetFd *mon_fdset_fd;
1593 AddfdInfo *fdinfo;
1595 qemu_mutex_lock(&mon_fdsets_lock);
1596 if (has_fdset_id) {
1597 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1598 /* Break if match found or match impossible due to ordering by ID */
1599 if (fdset_id <= mon_fdset->id) {
1600 if (fdset_id < mon_fdset->id) {
1601 mon_fdset = NULL;
1603 break;
1608 if (mon_fdset == NULL) {
1609 int64_t fdset_id_prev = -1;
1610 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1612 if (has_fdset_id) {
1613 if (fdset_id < 0) {
1614 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1615 "a non-negative value");
1616 qemu_mutex_unlock(&mon_fdsets_lock);
1617 return NULL;
1619 /* Use specified fdset ID */
1620 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1621 mon_fdset_cur = mon_fdset;
1622 if (fdset_id < mon_fdset_cur->id) {
1623 break;
1626 } else {
1627 /* Use first available fdset ID */
1628 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1629 mon_fdset_cur = mon_fdset;
1630 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1631 fdset_id_prev = mon_fdset_cur->id;
1632 continue;
1634 break;
1638 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1639 if (has_fdset_id) {
1640 mon_fdset->id = fdset_id;
1641 } else {
1642 mon_fdset->id = fdset_id_prev + 1;
1645 /* The fdset list is ordered by fdset ID */
1646 if (!mon_fdset_cur) {
1647 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1648 } else if (mon_fdset->id < mon_fdset_cur->id) {
1649 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1650 } else {
1651 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1655 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1656 mon_fdset_fd->fd = fd;
1657 mon_fdset_fd->removed = false;
1658 if (has_opaque) {
1659 mon_fdset_fd->opaque = g_strdup(opaque);
1661 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1663 fdinfo = g_malloc0(sizeof(*fdinfo));
1664 fdinfo->fdset_id = mon_fdset->id;
1665 fdinfo->fd = mon_fdset_fd->fd;
1667 qemu_mutex_unlock(&mon_fdsets_lock);
1668 return fdinfo;
1671 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1673 #ifdef _WIN32
1674 return -ENOENT;
1675 #else
1676 MonFdset *mon_fdset;
1677 MonFdsetFd *mon_fdset_fd;
1678 int mon_fd_flags;
1679 int ret;
1681 qemu_mutex_lock(&mon_fdsets_lock);
1682 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1683 if (mon_fdset->id != fdset_id) {
1684 continue;
1686 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1687 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1688 if (mon_fd_flags == -1) {
1689 ret = -errno;
1690 goto out;
1693 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1694 ret = mon_fdset_fd->fd;
1695 goto out;
1698 ret = -EACCES;
1699 goto out;
1701 ret = -ENOENT;
1703 out:
1704 qemu_mutex_unlock(&mon_fdsets_lock);
1705 return ret;
1706 #endif
1709 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1711 MonFdset *mon_fdset;
1712 MonFdsetFd *mon_fdset_fd_dup;
1714 qemu_mutex_lock(&mon_fdsets_lock);
1715 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1716 if (mon_fdset->id != fdset_id) {
1717 continue;
1719 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1720 if (mon_fdset_fd_dup->fd == dup_fd) {
1721 goto err;
1724 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1725 mon_fdset_fd_dup->fd = dup_fd;
1726 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1727 qemu_mutex_unlock(&mon_fdsets_lock);
1728 return 0;
1731 err:
1732 qemu_mutex_unlock(&mon_fdsets_lock);
1733 return -1;
1736 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1738 MonFdset *mon_fdset;
1739 MonFdsetFd *mon_fdset_fd_dup;
1741 qemu_mutex_lock(&mon_fdsets_lock);
1742 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1743 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1744 if (mon_fdset_fd_dup->fd == dup_fd) {
1745 if (remove) {
1746 QLIST_REMOVE(mon_fdset_fd_dup, next);
1747 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1748 monitor_fdset_cleanup(mon_fdset);
1750 goto err;
1751 } else {
1752 qemu_mutex_unlock(&mon_fdsets_lock);
1753 return mon_fdset->id;
1759 err:
1760 qemu_mutex_unlock(&mon_fdsets_lock);
1761 return -1;
1764 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1766 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1769 void monitor_fdset_dup_fd_remove(int dup_fd)
1771 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1774 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1776 int fd;
1777 Error *local_err = NULL;
1779 if (!qemu_isdigit(fdname[0]) && mon) {
1780 fd = monitor_get_fd(mon, fdname, &local_err);
1781 } else {
1782 fd = qemu_parse_fd(fdname);
1783 if (fd == -1) {
1784 error_setg(&local_err, "Invalid file descriptor number '%s'",
1785 fdname);
1788 if (local_err) {
1789 error_propagate(errp, local_err);
1790 assert(fd == -1);
1791 } else {
1792 assert(fd != -1);
1795 return fd;
1798 /* Please update hmp-commands.hx when adding or changing commands */
1799 static HMPCommand hmp_info_cmds[] = {
1800 #include "hmp-commands-info.h"
1801 { NULL, NULL, },
1804 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1805 HMPCommand hmp_cmds[] = {
1806 #include "hmp-commands.h"
1807 { NULL, NULL, },
1811 * Set @pval to the value in the register identified by @name.
1812 * return 0 if OK, -1 if not found
1814 int get_monitor_def(int64_t *pval, const char *name)
1816 const MonitorDef *md = target_monitor_defs();
1817 CPUState *cs = mon_get_cpu();
1818 void *ptr;
1819 uint64_t tmp = 0;
1820 int ret;
1822 if (cs == NULL || md == NULL) {
1823 return -1;
1826 for(; md->name != NULL; md++) {
1827 if (hmp_compare_cmd(name, md->name)) {
1828 if (md->get_value) {
1829 *pval = md->get_value(md, md->offset);
1830 } else {
1831 CPUArchState *env = mon_get_cpu_env();
1832 ptr = (uint8_t *)env + md->offset;
1833 switch(md->type) {
1834 case MD_I32:
1835 *pval = *(int32_t *)ptr;
1836 break;
1837 case MD_TLONG:
1838 *pval = *(target_long *)ptr;
1839 break;
1840 default:
1841 *pval = 0;
1842 break;
1845 return 0;
1849 ret = target_get_monitor_def(cs, name, &tmp);
1850 if (!ret) {
1851 *pval = (target_long) tmp;
1854 return ret;
1857 static void add_completion_option(ReadLineState *rs, const char *str,
1858 const char *option)
1860 if (!str || !option) {
1861 return;
1863 if (!strncmp(option, str, strlen(str))) {
1864 readline_add_completion(rs, option);
1868 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1870 size_t len;
1871 ChardevBackendInfoList *list, *start;
1873 if (nb_args != 2) {
1874 return;
1876 len = strlen(str);
1877 readline_set_completion_index(rs, len);
1879 start = list = qmp_query_chardev_backends(NULL);
1880 while (list) {
1881 const char *chr_name = list->value->name;
1883 if (!strncmp(chr_name, str, len)) {
1884 readline_add_completion(rs, chr_name);
1886 list = list->next;
1888 qapi_free_ChardevBackendInfoList(start);
1891 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1893 size_t len;
1894 int i;
1896 if (nb_args != 2) {
1897 return;
1899 len = strlen(str);
1900 readline_set_completion_index(rs, len);
1901 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1902 add_completion_option(rs, str, NetClientDriver_str(i));
1906 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1908 GSList *list, *elt;
1909 size_t len;
1911 if (nb_args != 2) {
1912 return;
1915 len = strlen(str);
1916 readline_set_completion_index(rs, len);
1917 list = elt = object_class_get_list(TYPE_DEVICE, false);
1918 while (elt) {
1919 const char *name;
1920 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1921 TYPE_DEVICE);
1922 name = object_class_get_name(OBJECT_CLASS(dc));
1924 if (dc->user_creatable
1925 && !strncmp(name, str, len)) {
1926 readline_add_completion(rs, name);
1928 elt = elt->next;
1930 g_slist_free(list);
1933 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1935 GSList *list, *elt;
1936 size_t len;
1938 if (nb_args != 2) {
1939 return;
1942 len = strlen(str);
1943 readline_set_completion_index(rs, len);
1944 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1945 while (elt) {
1946 const char *name;
1948 name = object_class_get_name(OBJECT_CLASS(elt->data));
1949 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1950 readline_add_completion(rs, name);
1952 elt = elt->next;
1954 g_slist_free(list);
1957 static void peripheral_device_del_completion(ReadLineState *rs,
1958 const char *str, size_t len)
1960 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1961 GSList *list, *item;
1963 list = qdev_build_hotpluggable_device_list(peripheral);
1964 if (!list) {
1965 return;
1968 for (item = list; item; item = g_slist_next(item)) {
1969 DeviceState *dev = item->data;
1971 if (dev->id && !strncmp(str, dev->id, len)) {
1972 readline_add_completion(rs, dev->id);
1976 g_slist_free(list);
1979 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1981 size_t len;
1982 ChardevInfoList *list, *start;
1984 if (nb_args != 2) {
1985 return;
1987 len = strlen(str);
1988 readline_set_completion_index(rs, len);
1990 start = list = qmp_query_chardev(NULL);
1991 while (list) {
1992 ChardevInfo *chr = list->value;
1994 if (!strncmp(chr->label, str, len)) {
1995 readline_add_completion(rs, chr->label);
1997 list = list->next;
1999 qapi_free_ChardevInfoList(start);
2002 static void ringbuf_completion(ReadLineState *rs, const char *str)
2004 size_t len;
2005 ChardevInfoList *list, *start;
2007 len = strlen(str);
2008 readline_set_completion_index(rs, len);
2010 start = list = qmp_query_chardev(NULL);
2011 while (list) {
2012 ChardevInfo *chr_info = list->value;
2014 if (!strncmp(chr_info->label, str, len)) {
2015 Chardev *chr = qemu_chr_find(chr_info->label);
2016 if (chr && CHARDEV_IS_RINGBUF(chr)) {
2017 readline_add_completion(rs, chr_info->label);
2020 list = list->next;
2022 qapi_free_ChardevInfoList(start);
2025 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
2027 if (nb_args != 2) {
2028 return;
2030 ringbuf_completion(rs, str);
2033 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
2035 size_t len;
2037 if (nb_args != 2) {
2038 return;
2041 len = strlen(str);
2042 readline_set_completion_index(rs, len);
2043 peripheral_device_del_completion(rs, str, len);
2046 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
2048 ObjectPropertyInfoList *list, *start;
2049 size_t len;
2051 if (nb_args != 2) {
2052 return;
2054 len = strlen(str);
2055 readline_set_completion_index(rs, len);
2057 start = list = qmp_qom_list("/objects", NULL);
2058 while (list) {
2059 ObjectPropertyInfo *info = list->value;
2061 if (!strncmp(info->type, "child<", 5)
2062 && !strncmp(info->name, str, len)) {
2063 readline_add_completion(rs, info->name);
2065 list = list->next;
2067 qapi_free_ObjectPropertyInfoList(start);
2070 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
2072 int i;
2073 char *sep;
2074 size_t len;
2076 if (nb_args != 2) {
2077 return;
2079 sep = strrchr(str, '-');
2080 if (sep) {
2081 str = sep + 1;
2083 len = strlen(str);
2084 readline_set_completion_index(rs, len);
2085 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
2086 if (!strncmp(str, QKeyCode_str(i), len)) {
2087 readline_add_completion(rs, QKeyCode_str(i));
2092 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
2094 size_t len;
2096 len = strlen(str);
2097 readline_set_completion_index(rs, len);
2098 if (nb_args == 2) {
2099 NetClientState *ncs[MAX_QUEUE_NUM];
2100 int count, i;
2101 count = qemu_find_net_clients_except(NULL, ncs,
2102 NET_CLIENT_DRIVER_NONE,
2103 MAX_QUEUE_NUM);
2104 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2105 const char *name = ncs[i]->name;
2106 if (!strncmp(str, name, len)) {
2107 readline_add_completion(rs, name);
2110 } else if (nb_args == 3) {
2111 add_completion_option(rs, str, "on");
2112 add_completion_option(rs, str, "off");
2116 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
2118 int len, count, i;
2119 NetClientState *ncs[MAX_QUEUE_NUM];
2121 if (nb_args != 2) {
2122 return;
2125 len = strlen(str);
2126 readline_set_completion_index(rs, len);
2127 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
2128 MAX_QUEUE_NUM);
2129 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2130 QemuOpts *opts;
2131 const char *name = ncs[i]->name;
2132 if (strncmp(str, name, len)) {
2133 continue;
2135 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
2136 if (opts) {
2137 readline_add_completion(rs, name);
2142 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2144 size_t len;
2146 len = strlen(str);
2147 readline_set_completion_index(rs, len);
2148 if (nb_args == 2) {
2149 TraceEventIter iter;
2150 TraceEvent *ev;
2151 char *pattern = g_strdup_printf("%s*", str);
2152 trace_event_iter_init(&iter, pattern);
2153 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2154 readline_add_completion(rs, trace_event_get_name(ev));
2156 g_free(pattern);
2160 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2162 size_t len;
2164 len = strlen(str);
2165 readline_set_completion_index(rs, len);
2166 if (nb_args == 2) {
2167 TraceEventIter iter;
2168 TraceEvent *ev;
2169 char *pattern = g_strdup_printf("%s*", str);
2170 trace_event_iter_init(&iter, pattern);
2171 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2172 readline_add_completion(rs, trace_event_get_name(ev));
2174 g_free(pattern);
2175 } else if (nb_args == 3) {
2176 add_completion_option(rs, str, "on");
2177 add_completion_option(rs, str, "off");
2181 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2183 int i;
2185 if (nb_args != 2) {
2186 return;
2188 readline_set_completion_index(rs, strlen(str));
2189 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2190 add_completion_option(rs, str, WatchdogAction_str(i));
2194 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2195 const char *str)
2197 size_t len;
2199 len = strlen(str);
2200 readline_set_completion_index(rs, len);
2201 if (nb_args == 2) {
2202 int i;
2203 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2204 const char *name = MigrationCapability_str(i);
2205 if (!strncmp(str, name, len)) {
2206 readline_add_completion(rs, name);
2209 } else if (nb_args == 3) {
2210 add_completion_option(rs, str, "on");
2211 add_completion_option(rs, str, "off");
2215 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2216 const char *str)
2218 size_t len;
2220 len = strlen(str);
2221 readline_set_completion_index(rs, len);
2222 if (nb_args == 2) {
2223 int i;
2224 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
2225 const char *name = MigrationParameter_str(i);
2226 if (!strncmp(str, name, len)) {
2227 readline_add_completion(rs, name);
2233 static void vm_completion(ReadLineState *rs, const char *str)
2235 size_t len;
2236 BlockDriverState *bs;
2237 BdrvNextIterator it;
2239 len = strlen(str);
2240 readline_set_completion_index(rs, len);
2242 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2243 SnapshotInfoList *snapshots, *snapshot;
2244 AioContext *ctx = bdrv_get_aio_context(bs);
2245 bool ok = false;
2247 aio_context_acquire(ctx);
2248 if (bdrv_can_snapshot(bs)) {
2249 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2251 aio_context_release(ctx);
2252 if (!ok) {
2253 continue;
2256 snapshot = snapshots;
2257 while (snapshot) {
2258 char *completion = snapshot->value->name;
2259 if (!strncmp(str, completion, len)) {
2260 readline_add_completion(rs, completion);
2262 completion = snapshot->value->id;
2263 if (!strncmp(str, completion, len)) {
2264 readline_add_completion(rs, completion);
2266 snapshot = snapshot->next;
2268 qapi_free_SnapshotInfoList(snapshots);
2273 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2275 if (nb_args == 2) {
2276 vm_completion(rs, str);
2280 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2282 if (nb_args == 2) {
2283 vm_completion(rs, str);
2287 static int
2288 compare_mon_cmd(const void *a, const void *b)
2290 return strcmp(((const HMPCommand *)a)->name,
2291 ((const HMPCommand *)b)->name);
2294 static void sortcmdlist(void)
2296 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2297 sizeof(*hmp_cmds),
2298 compare_mon_cmd);
2299 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2300 sizeof(*hmp_info_cmds),
2301 compare_mon_cmd);
2304 void monitor_init_globals(void)
2306 monitor_init_globals_core();
2307 monitor_init_qmp_commands();
2308 sortcmdlist();
2309 qemu_mutex_init(&mon_fdsets_lock);