virtiofsd: passthrough_ll: add renameat2 support
[qemu/ar7.git] / monitor / misc.c
blobde1ca4d11474b6930bb1069bf1cd2cdf01312838
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-char.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-trace.h"
74 #include "qapi/qapi-emit-events.h"
75 #include "qapi/qapi-init-commands.h"
76 #include "qapi/error.h"
77 #include "qapi/qmp-event.h"
78 #include "qapi/qapi-introspect.h"
79 #include "sysemu/cpus.h"
80 #include "qemu/cutils.h"
81 #include "tcg/tcg.h"
83 #if defined(TARGET_S390X)
84 #include "hw/s390x/storage-keys.h"
85 #include "hw/s390x/storage-attributes.h"
86 #endif
88 /* file descriptors passed via SCM_RIGHTS */
89 typedef struct mon_fd_t mon_fd_t;
90 struct mon_fd_t {
91 char *name;
92 int fd;
93 QLIST_ENTRY(mon_fd_t) next;
96 /* file descriptor associated with a file descriptor set */
97 typedef struct MonFdsetFd MonFdsetFd;
98 struct MonFdsetFd {
99 int fd;
100 bool removed;
101 char *opaque;
102 QLIST_ENTRY(MonFdsetFd) next;
105 /* file descriptor set containing fds passed via SCM_RIGHTS */
106 typedef struct MonFdset MonFdset;
107 struct MonFdset {
108 int64_t id;
109 QLIST_HEAD(, MonFdsetFd) fds;
110 QLIST_HEAD(, MonFdsetFd) dup_fds;
111 QLIST_ENTRY(MonFdset) next;
114 /* Protects mon_fdsets */
115 static QemuMutex mon_fdsets_lock;
116 static QLIST_HEAD(, MonFdset) mon_fdsets;
118 static HMPCommand hmp_info_cmds[];
120 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
121 int64_t cpu_index, Error **errp)
123 char *output = NULL;
124 Monitor *old_mon;
125 MonitorHMP hmp = {};
127 monitor_data_init(&hmp.common, false, true, false);
129 old_mon = cur_mon;
130 cur_mon = &hmp.common;
132 if (has_cpu_index) {
133 int ret = monitor_set_cpu(cpu_index);
134 if (ret < 0) {
135 cur_mon = old_mon;
136 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
137 "a CPU number");
138 goto out;
142 handle_hmp_command(&hmp, command_line);
143 cur_mon = old_mon;
145 qemu_mutex_lock(&hmp.common.mon_lock);
146 if (qstring_get_length(hmp.common.outbuf) > 0) {
147 output = g_strdup(qstring_get_str(hmp.common.outbuf));
148 } else {
149 output = g_strdup("");
151 qemu_mutex_unlock(&hmp.common.mon_lock);
153 out:
154 monitor_data_destroy(&hmp.common);
155 return output;
159 * Is @name in the '|' separated list of names @list?
161 int hmp_compare_cmd(const char *name, const char *list)
163 const char *p, *pstart;
164 int len;
165 len = strlen(name);
166 p = list;
167 for (;;) {
168 pstart = p;
169 p = qemu_strchrnul(p, '|');
170 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
171 return 1;
173 if (*p == '\0') {
174 break;
176 p++;
178 return 0;
181 static void do_help_cmd(Monitor *mon, const QDict *qdict)
183 help_cmd(mon, qdict_get_try_str(qdict, "name"));
186 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
188 const char *tp_name = qdict_get_str(qdict, "name");
189 bool new_state = qdict_get_bool(qdict, "option");
190 bool has_vcpu = qdict_haskey(qdict, "vcpu");
191 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
192 Error *local_err = NULL;
194 if (vcpu < 0) {
195 monitor_printf(mon, "argument vcpu must be positive");
196 return;
199 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
200 if (local_err) {
201 error_report_err(local_err);
205 #ifdef CONFIG_TRACE_SIMPLE
206 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
208 const char *op = qdict_get_try_str(qdict, "op");
209 const char *arg = qdict_get_try_str(qdict, "arg");
211 if (!op) {
212 st_print_trace_file_status();
213 } else if (!strcmp(op, "on")) {
214 st_set_trace_file_enabled(true);
215 } else if (!strcmp(op, "off")) {
216 st_set_trace_file_enabled(false);
217 } else if (!strcmp(op, "flush")) {
218 st_flush_trace_buffer();
219 } else if (!strcmp(op, "set")) {
220 if (arg) {
221 st_set_trace_file(arg);
223 } else {
224 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
225 help_cmd(mon, "trace-file");
228 #endif
230 static void hmp_info_help(Monitor *mon, const QDict *qdict)
232 help_cmd(mon, "info");
235 static void query_commands_cb(QmpCommand *cmd, void *opaque)
237 CommandInfoList *info, **list = opaque;
239 if (!cmd->enabled) {
240 return;
243 info = g_malloc0(sizeof(*info));
244 info->value = g_malloc0(sizeof(*info->value));
245 info->value->name = g_strdup(cmd->name);
246 info->next = *list;
247 *list = info;
250 CommandInfoList *qmp_query_commands(Error **errp)
252 CommandInfoList *list = NULL;
253 MonitorQMP *mon;
255 assert(monitor_is_qmp(cur_mon));
256 mon = container_of(cur_mon, MonitorQMP, common);
258 qmp_for_each_command(mon->commands, query_commands_cb, &list);
260 return list;
263 EventInfoList *qmp_query_events(Error **errp)
266 * TODO This deprecated command is the only user of
267 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
268 * they should go, too.
270 EventInfoList *info, *ev_list = NULL;
271 QAPIEvent e;
273 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
274 const char *event_name = QAPIEvent_str(e);
275 assert(event_name != NULL);
276 info = g_malloc0(sizeof(*info));
277 info->value = g_malloc0(sizeof(*info->value));
278 info->value->name = g_strdup(event_name);
280 info->next = ev_list;
281 ev_list = info;
284 return ev_list;
288 * Minor hack: generated marshalling suppressed for this command
289 * ('gen': false in the schema) so we can parse the JSON string
290 * directly into QObject instead of first parsing it with
291 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
292 * to QObject with generated output marshallers, every time. Instead,
293 * we do it in test-qobject-input-visitor.c, just to make sure
294 * qapi-gen.py's output actually conforms to the schema.
296 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
297 Error **errp)
299 *ret_data = qobject_from_qlit(&qmp_schema_qlit);
302 static void monitor_init_qmp_commands(void)
305 * Two command lists:
306 * - qmp_commands contains all QMP commands
307 * - qmp_cap_negotiation_commands contains just
308 * "qmp_capabilities", to enforce capability negotiation
311 qmp_init_marshal(&qmp_commands);
313 qmp_register_command(&qmp_commands, "query-qmp-schema",
314 qmp_query_qmp_schema, QCO_ALLOW_PRECONFIG);
315 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
316 QCO_NO_OPTIONS);
317 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
318 QCO_NO_OPTIONS);
320 QTAILQ_INIT(&qmp_cap_negotiation_commands);
321 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
322 qmp_marshal_qmp_capabilities, QCO_ALLOW_PRECONFIG);
326 * Accept QMP capabilities in @list for @mon.
327 * On success, set mon->qmp.capab[], and return true.
328 * On error, set @errp, and return false.
330 static bool qmp_caps_accept(MonitorQMP *mon, QMPCapabilityList *list,
331 Error **errp)
333 GString *unavailable = NULL;
334 bool capab[QMP_CAPABILITY__MAX];
336 memset(capab, 0, sizeof(capab));
338 for (; list; list = list->next) {
339 if (!mon->capab_offered[list->value]) {
340 if (!unavailable) {
341 unavailable = g_string_new(QMPCapability_str(list->value));
342 } else {
343 g_string_append_printf(unavailable, ", %s",
344 QMPCapability_str(list->value));
347 capab[list->value] = true;
350 if (unavailable) {
351 error_setg(errp, "Capability %s not available", unavailable->str);
352 g_string_free(unavailable, true);
353 return false;
356 memcpy(mon->capab, capab, sizeof(capab));
357 return true;
360 void qmp_qmp_capabilities(bool has_enable, QMPCapabilityList *enable,
361 Error **errp)
363 MonitorQMP *mon;
365 assert(monitor_is_qmp(cur_mon));
366 mon = container_of(cur_mon, MonitorQMP, common);
368 if (mon->commands == &qmp_commands) {
369 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
370 "Capabilities negotiation is already complete, command "
371 "ignored");
372 return;
375 if (!qmp_caps_accept(mon, enable, errp)) {
376 return;
379 mon->commands = &qmp_commands;
382 /* Set the current CPU defined by the user. Callers must hold BQL. */
383 int monitor_set_cpu(int cpu_index)
385 CPUState *cpu;
387 cpu = qemu_get_cpu(cpu_index);
388 if (cpu == NULL) {
389 return -1;
391 g_free(cur_mon->mon_cpu_path);
392 cur_mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
393 return 0;
396 /* Callers must hold BQL. */
397 static CPUState *mon_get_cpu_sync(bool synchronize)
399 CPUState *cpu = NULL;
401 if (cur_mon->mon_cpu_path) {
402 cpu = (CPUState *) object_resolve_path_type(cur_mon->mon_cpu_path,
403 TYPE_CPU, NULL);
404 if (!cpu) {
405 g_free(cur_mon->mon_cpu_path);
406 cur_mon->mon_cpu_path = NULL;
409 if (!cur_mon->mon_cpu_path) {
410 if (!first_cpu) {
411 return NULL;
413 monitor_set_cpu(first_cpu->cpu_index);
414 cpu = first_cpu;
416 assert(cpu != NULL);
417 if (synchronize) {
418 cpu_synchronize_state(cpu);
420 return cpu;
423 CPUState *mon_get_cpu(void)
425 return mon_get_cpu_sync(true);
428 CPUArchState *mon_get_cpu_env(void)
430 CPUState *cs = mon_get_cpu();
432 return cs ? cs->env_ptr : NULL;
435 int monitor_get_cpu_index(void)
437 CPUState *cs = mon_get_cpu_sync(false);
439 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
442 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
444 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
445 CPUState *cs;
447 if (all_cpus) {
448 CPU_FOREACH(cs) {
449 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
450 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
452 } else {
453 cs = mon_get_cpu();
455 if (!cs) {
456 monitor_printf(mon, "No CPU available\n");
457 return;
460 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
464 #ifdef CONFIG_TCG
465 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
467 if (!tcg_enabled()) {
468 error_report("JIT information is only available with accel=tcg");
469 return;
472 dump_exec_info();
473 dump_drift_info();
476 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
478 dump_opcount_info();
480 #endif
482 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
484 int64_t max = qdict_get_try_int(qdict, "max", 10);
485 bool mean = qdict_get_try_bool(qdict, "mean", false);
486 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
487 enum QSPSortBy sort_by;
489 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
490 qsp_report(max, sort_by, coalesce);
493 static void hmp_info_history(Monitor *mon, const QDict *qdict)
495 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
496 int i;
497 const char *str;
499 if (!hmp_mon->rs) {
500 return;
502 i = 0;
503 for(;;) {
504 str = readline_get_history(hmp_mon->rs, i);
505 if (!str) {
506 break;
508 monitor_printf(mon, "%d: '%s'\n", i, str);
509 i++;
513 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
515 CPUState *cs = mon_get_cpu();
517 if (!cs) {
518 monitor_printf(mon, "No CPU available\n");
519 return;
521 cpu_dump_statistics(cs, 0);
524 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
526 const char *name = qdict_get_try_str(qdict, "name");
527 bool has_vcpu = qdict_haskey(qdict, "vcpu");
528 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
529 TraceEventInfoList *events;
530 TraceEventInfoList *elem;
531 Error *local_err = NULL;
533 if (name == NULL) {
534 name = "*";
536 if (vcpu < 0) {
537 monitor_printf(mon, "argument vcpu must be positive");
538 return;
541 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
542 if (local_err) {
543 error_report_err(local_err);
544 return;
547 for (elem = events; elem != NULL; elem = elem->next) {
548 monitor_printf(mon, "%s : state %u\n",
549 elem->value->name,
550 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
552 qapi_free_TraceEventInfoList(events);
555 void qmp_client_migrate_info(const char *protocol, const char *hostname,
556 bool has_port, int64_t port,
557 bool has_tls_port, int64_t tls_port,
558 bool has_cert_subject, const char *cert_subject,
559 Error **errp)
561 if (strcmp(protocol, "spice") == 0) {
562 if (!qemu_using_spice(errp)) {
563 return;
566 if (!has_port && !has_tls_port) {
567 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
568 return;
571 if (qemu_spice_migrate_info(hostname,
572 has_port ? port : -1,
573 has_tls_port ? tls_port : -1,
574 cert_subject)) {
575 error_setg(errp, QERR_UNDEFINED_ERROR);
576 return;
578 return;
581 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
584 static void hmp_logfile(Monitor *mon, const QDict *qdict)
586 Error *err = NULL;
588 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
589 if (err) {
590 error_report_err(err);
594 static void hmp_log(Monitor *mon, const QDict *qdict)
596 int mask;
597 const char *items = qdict_get_str(qdict, "items");
599 if (!strcmp(items, "none")) {
600 mask = 0;
601 } else {
602 mask = qemu_str_to_log_mask(items);
603 if (!mask) {
604 help_cmd(mon, "log");
605 return;
608 qemu_set_log(mask);
611 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
613 const char *option = qdict_get_try_str(qdict, "option");
614 if (!option || !strcmp(option, "on")) {
615 singlestep = 1;
616 } else if (!strcmp(option, "off")) {
617 singlestep = 0;
618 } else {
619 monitor_printf(mon, "unexpected option %s\n", option);
623 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
625 const char *device = qdict_get_try_str(qdict, "device");
626 if (!device)
627 device = "tcp::" DEFAULT_GDBSTUB_PORT;
628 if (gdbserver_start(device) < 0) {
629 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
630 device);
631 } else if (strcmp(device, "none") == 0) {
632 monitor_printf(mon, "Disabled gdbserver\n");
633 } else {
634 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
635 device);
639 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
641 const char *action = qdict_get_str(qdict, "action");
642 if (select_watchdog_action(action) == -1) {
643 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
647 static void monitor_printc(Monitor *mon, int c)
649 monitor_printf(mon, "'");
650 switch(c) {
651 case '\'':
652 monitor_printf(mon, "\\'");
653 break;
654 case '\\':
655 monitor_printf(mon, "\\\\");
656 break;
657 case '\n':
658 monitor_printf(mon, "\\n");
659 break;
660 case '\r':
661 monitor_printf(mon, "\\r");
662 break;
663 default:
664 if (c >= 32 && c <= 126) {
665 monitor_printf(mon, "%c", c);
666 } else {
667 monitor_printf(mon, "\\x%02x", c);
669 break;
671 monitor_printf(mon, "'");
674 static void memory_dump(Monitor *mon, int count, int format, int wsize,
675 hwaddr addr, int is_physical)
677 int l, line_size, i, max_digits, len;
678 uint8_t buf[16];
679 uint64_t v;
680 CPUState *cs = mon_get_cpu();
682 if (!cs && (format == 'i' || !is_physical)) {
683 monitor_printf(mon, "Can not dump without CPU\n");
684 return;
687 if (format == 'i') {
688 monitor_disas(mon, cs, addr, count, is_physical);
689 return;
692 len = wsize * count;
693 if (wsize == 1)
694 line_size = 8;
695 else
696 line_size = 16;
697 max_digits = 0;
699 switch(format) {
700 case 'o':
701 max_digits = DIV_ROUND_UP(wsize * 8, 3);
702 break;
703 default:
704 case 'x':
705 max_digits = (wsize * 8) / 4;
706 break;
707 case 'u':
708 case 'd':
709 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
710 break;
711 case 'c':
712 wsize = 1;
713 break;
716 while (len > 0) {
717 if (is_physical)
718 monitor_printf(mon, TARGET_FMT_plx ":", addr);
719 else
720 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
721 l = len;
722 if (l > line_size)
723 l = line_size;
724 if (is_physical) {
725 AddressSpace *as = cs ? cs->as : &address_space_memory;
726 MemTxResult r = address_space_read(as, addr,
727 MEMTXATTRS_UNSPECIFIED, buf, l);
728 if (r != MEMTX_OK) {
729 monitor_printf(mon, " Cannot access memory\n");
730 break;
732 } else {
733 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
734 monitor_printf(mon, " Cannot access memory\n");
735 break;
738 i = 0;
739 while (i < l) {
740 switch(wsize) {
741 default:
742 case 1:
743 v = ldub_p(buf + i);
744 break;
745 case 2:
746 v = lduw_p(buf + i);
747 break;
748 case 4:
749 v = (uint32_t)ldl_p(buf + i);
750 break;
751 case 8:
752 v = ldq_p(buf + i);
753 break;
755 monitor_printf(mon, " ");
756 switch(format) {
757 case 'o':
758 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
759 break;
760 case 'x':
761 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
762 break;
763 case 'u':
764 monitor_printf(mon, "%*" PRIu64, max_digits, v);
765 break;
766 case 'd':
767 monitor_printf(mon, "%*" PRId64, max_digits, v);
768 break;
769 case 'c':
770 monitor_printc(mon, v);
771 break;
773 i += wsize;
775 monitor_printf(mon, "\n");
776 addr += l;
777 len -= l;
781 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
783 int count = qdict_get_int(qdict, "count");
784 int format = qdict_get_int(qdict, "format");
785 int size = qdict_get_int(qdict, "size");
786 target_long addr = qdict_get_int(qdict, "addr");
788 memory_dump(mon, count, format, size, addr, 0);
791 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
793 int count = qdict_get_int(qdict, "count");
794 int format = qdict_get_int(qdict, "format");
795 int size = qdict_get_int(qdict, "size");
796 hwaddr addr = qdict_get_int(qdict, "addr");
798 memory_dump(mon, count, format, size, addr, 1);
801 static void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, Error **errp)
803 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
804 addr, 1);
806 if (!mrs.mr) {
807 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
808 return NULL;
811 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
812 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
813 memory_region_unref(mrs.mr);
814 return NULL;
817 *p_mr = mrs.mr;
818 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
821 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
823 hwaddr addr = qdict_get_int(qdict, "addr");
824 Error *local_err = NULL;
825 MemoryRegion *mr = NULL;
826 void *ptr;
828 ptr = gpa2hva(&mr, addr, &local_err);
829 if (local_err) {
830 error_report_err(local_err);
831 return;
834 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
835 " (%s) is %p\n",
836 addr, mr->name, ptr);
838 memory_region_unref(mr);
841 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
843 target_ulong addr = qdict_get_int(qdict, "addr");
844 MemTxAttrs attrs;
845 CPUState *cs = mon_get_cpu();
846 hwaddr gpa;
848 if (!cs) {
849 monitor_printf(mon, "No cpu\n");
850 return;
853 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
854 if (gpa == -1) {
855 monitor_printf(mon, "Unmapped\n");
856 } else {
857 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
858 gpa + (addr & ~TARGET_PAGE_MASK));
862 #ifdef CONFIG_LINUX
863 static uint64_t vtop(void *ptr, Error **errp)
865 uint64_t pinfo;
866 uint64_t ret = -1;
867 uintptr_t addr = (uintptr_t) ptr;
868 uintptr_t pagesize = qemu_real_host_page_size;
869 off_t offset = addr / pagesize * sizeof(pinfo);
870 int fd;
872 fd = open("/proc/self/pagemap", O_RDONLY);
873 if (fd == -1) {
874 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
875 return -1;
878 /* Force copy-on-write if necessary. */
879 atomic_add((uint8_t *)ptr, 0);
881 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
882 error_setg_errno(errp, errno, "Cannot read pagemap");
883 goto out;
885 if ((pinfo & (1ull << 63)) == 0) {
886 error_setg(errp, "Page not present");
887 goto out;
889 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
891 out:
892 close(fd);
893 return ret;
896 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
898 hwaddr addr = qdict_get_int(qdict, "addr");
899 Error *local_err = NULL;
900 MemoryRegion *mr = NULL;
901 void *ptr;
902 uint64_t physaddr;
904 ptr = gpa2hva(&mr, addr, &local_err);
905 if (local_err) {
906 error_report_err(local_err);
907 return;
910 physaddr = vtop(ptr, &local_err);
911 if (local_err) {
912 error_report_err(local_err);
913 } else {
914 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
915 " (%s) is 0x%" PRIx64 "\n",
916 addr, mr->name, (uint64_t) physaddr);
919 memory_region_unref(mr);
921 #endif
923 static void do_print(Monitor *mon, const QDict *qdict)
925 int format = qdict_get_int(qdict, "format");
926 hwaddr val = qdict_get_int(qdict, "val");
928 switch(format) {
929 case 'o':
930 monitor_printf(mon, "%#" HWADDR_PRIo, val);
931 break;
932 case 'x':
933 monitor_printf(mon, "%#" HWADDR_PRIx, val);
934 break;
935 case 'u':
936 monitor_printf(mon, "%" HWADDR_PRIu, val);
937 break;
938 default:
939 case 'd':
940 monitor_printf(mon, "%" HWADDR_PRId, val);
941 break;
942 case 'c':
943 monitor_printc(mon, val);
944 break;
946 monitor_printf(mon, "\n");
949 static void hmp_sum(Monitor *mon, const QDict *qdict)
951 uint32_t addr;
952 uint16_t sum;
953 uint32_t start = qdict_get_int(qdict, "start");
954 uint32_t size = qdict_get_int(qdict, "size");
956 sum = 0;
957 for(addr = start; addr < (start + size); addr++) {
958 uint8_t val = address_space_ldub(&address_space_memory, addr,
959 MEMTXATTRS_UNSPECIFIED, NULL);
960 /* BSD sum algorithm ('sum' Unix command) */
961 sum = (sum >> 1) | (sum << 15);
962 sum += val;
964 monitor_printf(mon, "%05d\n", sum);
967 static int mouse_button_state;
969 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
971 int dx, dy, dz, button;
972 const char *dx_str = qdict_get_str(qdict, "dx_str");
973 const char *dy_str = qdict_get_str(qdict, "dy_str");
974 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
976 dx = strtol(dx_str, NULL, 0);
977 dy = strtol(dy_str, NULL, 0);
978 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
979 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
981 if (dz_str) {
982 dz = strtol(dz_str, NULL, 0);
983 if (dz != 0) {
984 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
985 qemu_input_queue_btn(NULL, button, true);
986 qemu_input_event_sync();
987 qemu_input_queue_btn(NULL, button, false);
990 qemu_input_event_sync();
993 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
995 static uint32_t bmap[INPUT_BUTTON__MAX] = {
996 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
997 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
998 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1000 int button_state = qdict_get_int(qdict, "button_state");
1002 if (mouse_button_state == button_state) {
1003 return;
1005 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1006 qemu_input_event_sync();
1007 mouse_button_state = button_state;
1010 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1012 int size = qdict_get_int(qdict, "size");
1013 int addr = qdict_get_int(qdict, "addr");
1014 int has_index = qdict_haskey(qdict, "index");
1015 uint32_t val;
1016 int suffix;
1018 if (has_index) {
1019 int index = qdict_get_int(qdict, "index");
1020 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1021 addr++;
1023 addr &= 0xffff;
1025 switch(size) {
1026 default:
1027 case 1:
1028 val = cpu_inb(addr);
1029 suffix = 'b';
1030 break;
1031 case 2:
1032 val = cpu_inw(addr);
1033 suffix = 'w';
1034 break;
1035 case 4:
1036 val = cpu_inl(addr);
1037 suffix = 'l';
1038 break;
1040 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1041 suffix, addr, size * 2, val);
1044 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1046 int size = qdict_get_int(qdict, "size");
1047 int addr = qdict_get_int(qdict, "addr");
1048 int val = qdict_get_int(qdict, "val");
1050 addr &= IOPORTS_MASK;
1052 switch (size) {
1053 default:
1054 case 1:
1055 cpu_outb(addr, val);
1056 break;
1057 case 2:
1058 cpu_outw(addr, val);
1059 break;
1060 case 4:
1061 cpu_outl(addr, val);
1062 break;
1066 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1068 Error *local_err = NULL;
1069 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1071 qemu_boot_set(bootdevice, &local_err);
1072 if (local_err) {
1073 error_report_err(local_err);
1074 } else {
1075 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1079 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1081 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1082 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
1083 bool owner = qdict_get_try_bool(qdict, "owner", false);
1085 mtree_info(flatview, dispatch_tree, owner);
1088 #ifdef CONFIG_PROFILER
1090 int64_t dev_time;
1092 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1094 static int64_t last_cpu_exec_time;
1095 int64_t cpu_exec_time;
1096 int64_t delta;
1098 cpu_exec_time = tcg_cpu_exec_time();
1099 delta = cpu_exec_time - last_cpu_exec_time;
1101 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1102 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1103 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1104 delta, delta / (double)NANOSECONDS_PER_SECOND);
1105 last_cpu_exec_time = cpu_exec_time;
1106 dev_time = 0;
1108 #else
1109 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1111 monitor_printf(mon, "Internal profiler not compiled\n");
1113 #endif
1115 /* Capture support */
1116 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1118 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1120 int i;
1121 CaptureState *s;
1123 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1124 monitor_printf(mon, "[%d]: ", i);
1125 s->ops.info (s->opaque);
1129 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1131 int i;
1132 int n = qdict_get_int(qdict, "n");
1133 CaptureState *s;
1135 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1136 if (i == n) {
1137 s->ops.destroy (s->opaque);
1138 QLIST_REMOVE (s, entries);
1139 g_free (s);
1140 return;
1145 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1147 const char *path = qdict_get_str(qdict, "path");
1148 int freq = qdict_get_try_int(qdict, "freq", 44100);
1149 int bits = qdict_get_try_int(qdict, "bits", 16);
1150 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
1151 const char *audiodev = qdict_get_str(qdict, "audiodev");
1152 CaptureState *s;
1153 AudioState *as = audio_state_by_name(audiodev);
1155 if (!as) {
1156 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1157 return;
1160 s = g_malloc0 (sizeof (*s));
1162 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1163 monitor_printf(mon, "Failed to add wave capture\n");
1164 g_free (s);
1165 return;
1167 QLIST_INSERT_HEAD (&capture_head, s, entries);
1170 static QAuthZList *find_auth(Monitor *mon, const char *name)
1172 Object *obj;
1173 Object *container;
1175 container = object_get_objects_root();
1176 obj = object_resolve_path_component(container, name);
1177 if (!obj) {
1178 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1179 return NULL;
1182 return QAUTHZ_LIST(obj);
1185 static bool warn_acl;
1186 static void hmp_warn_acl(void)
1188 if (warn_acl) {
1189 return;
1191 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1192 "commands are deprecated with no replacement. Authorization "
1193 "for VNC should be performed using the pluggable QAuthZ "
1194 "objects");
1195 warn_acl = true;
1198 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1200 const char *aclname = qdict_get_str(qdict, "aclname");
1201 QAuthZList *auth = find_auth(mon, aclname);
1202 QAuthZListRuleList *rules;
1203 size_t i = 0;
1205 hmp_warn_acl();
1207 if (!auth) {
1208 return;
1211 monitor_printf(mon, "policy: %s\n",
1212 QAuthZListPolicy_str(auth->policy));
1214 rules = auth->rules;
1215 while (rules) {
1216 QAuthZListRule *rule = rules->value;
1217 i++;
1218 monitor_printf(mon, "%zu: %s %s\n", i,
1219 QAuthZListPolicy_str(rule->policy),
1220 rule->match);
1221 rules = rules->next;
1225 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1227 const char *aclname = qdict_get_str(qdict, "aclname");
1228 QAuthZList *auth = find_auth(mon, aclname);
1230 hmp_warn_acl();
1232 if (!auth) {
1233 return;
1236 auth->policy = QAUTHZ_LIST_POLICY_DENY;
1237 qapi_free_QAuthZListRuleList(auth->rules);
1238 auth->rules = NULL;
1239 monitor_printf(mon, "acl: removed all rules\n");
1242 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1244 const char *aclname = qdict_get_str(qdict, "aclname");
1245 const char *policy = qdict_get_str(qdict, "policy");
1246 QAuthZList *auth = find_auth(mon, aclname);
1247 int val;
1248 Error *err = NULL;
1250 hmp_warn_acl();
1252 if (!auth) {
1253 return;
1256 val = qapi_enum_parse(&QAuthZListPolicy_lookup,
1257 policy,
1258 QAUTHZ_LIST_POLICY_DENY,
1259 &err);
1260 if (err) {
1261 error_free(err);
1262 monitor_printf(mon, "acl: unknown policy '%s', "
1263 "expected 'deny' or 'allow'\n", policy);
1264 } else {
1265 auth->policy = val;
1266 if (auth->policy == QAUTHZ_LIST_POLICY_ALLOW) {
1267 monitor_printf(mon, "acl: policy set to 'allow'\n");
1268 } else {
1269 monitor_printf(mon, "acl: policy set to 'deny'\n");
1274 static QAuthZListFormat hmp_acl_get_format(const char *match)
1276 if (strchr(match, '*')) {
1277 return QAUTHZ_LIST_FORMAT_GLOB;
1278 } else {
1279 return QAUTHZ_LIST_FORMAT_EXACT;
1283 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1285 const char *aclname = qdict_get_str(qdict, "aclname");
1286 const char *match = qdict_get_str(qdict, "match");
1287 const char *policystr = qdict_get_str(qdict, "policy");
1288 int has_index = qdict_haskey(qdict, "index");
1289 int index = qdict_get_try_int(qdict, "index", -1);
1290 QAuthZList *auth = find_auth(mon, aclname);
1291 Error *err = NULL;
1292 QAuthZListPolicy policy;
1293 QAuthZListFormat format;
1294 size_t i = 0;
1296 hmp_warn_acl();
1298 if (!auth) {
1299 return;
1302 policy = qapi_enum_parse(&QAuthZListPolicy_lookup,
1303 policystr,
1304 QAUTHZ_LIST_POLICY_DENY,
1305 &err);
1306 if (err) {
1307 error_free(err);
1308 monitor_printf(mon, "acl: unknown policy '%s', "
1309 "expected 'deny' or 'allow'\n", policystr);
1310 return;
1313 format = hmp_acl_get_format(match);
1315 if (has_index && index == 0) {
1316 monitor_printf(mon, "acl: unable to add acl entry\n");
1317 return;
1320 if (has_index) {
1321 i = qauthz_list_insert_rule(auth, match, policy,
1322 format, index - 1, &err);
1323 } else {
1324 i = qauthz_list_append_rule(auth, match, policy,
1325 format, &err);
1327 if (err) {
1328 monitor_printf(mon, "acl: unable to add rule: %s",
1329 error_get_pretty(err));
1330 error_free(err);
1331 } else {
1332 monitor_printf(mon, "acl: added rule at position %zu\n", i + 1);
1336 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1338 const char *aclname = qdict_get_str(qdict, "aclname");
1339 const char *match = qdict_get_str(qdict, "match");
1340 QAuthZList *auth = find_auth(mon, aclname);
1341 ssize_t i = 0;
1343 hmp_warn_acl();
1345 if (!auth) {
1346 return;
1349 i = qauthz_list_delete_rule(auth, match);
1350 if (i >= 0) {
1351 monitor_printf(mon, "acl: removed rule at position %zu\n", i + 1);
1352 } else {
1353 monitor_printf(mon, "acl: no matching acl entry\n");
1357 void qmp_getfd(const char *fdname, Error **errp)
1359 mon_fd_t *monfd;
1360 int fd, tmp_fd;
1362 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1363 if (fd == -1) {
1364 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1365 return;
1368 if (qemu_isdigit(fdname[0])) {
1369 close(fd);
1370 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1371 "a name not starting with a digit");
1372 return;
1375 qemu_mutex_lock(&cur_mon->mon_lock);
1376 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1377 if (strcmp(monfd->name, fdname) != 0) {
1378 continue;
1381 tmp_fd = monfd->fd;
1382 monfd->fd = fd;
1383 qemu_mutex_unlock(&cur_mon->mon_lock);
1384 /* Make sure close() is outside critical section */
1385 close(tmp_fd);
1386 return;
1389 monfd = g_malloc0(sizeof(mon_fd_t));
1390 monfd->name = g_strdup(fdname);
1391 monfd->fd = fd;
1393 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1394 qemu_mutex_unlock(&cur_mon->mon_lock);
1397 void qmp_closefd(const char *fdname, Error **errp)
1399 mon_fd_t *monfd;
1400 int tmp_fd;
1402 qemu_mutex_lock(&cur_mon->mon_lock);
1403 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1404 if (strcmp(monfd->name, fdname) != 0) {
1405 continue;
1408 QLIST_REMOVE(monfd, next);
1409 tmp_fd = monfd->fd;
1410 g_free(monfd->name);
1411 g_free(monfd);
1412 qemu_mutex_unlock(&cur_mon->mon_lock);
1413 /* Make sure close() is outside critical section */
1414 close(tmp_fd);
1415 return;
1418 qemu_mutex_unlock(&cur_mon->mon_lock);
1419 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1422 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1424 mon_fd_t *monfd;
1426 qemu_mutex_lock(&mon->mon_lock);
1427 QLIST_FOREACH(monfd, &mon->fds, next) {
1428 int fd;
1430 if (strcmp(monfd->name, fdname) != 0) {
1431 continue;
1434 fd = monfd->fd;
1436 /* caller takes ownership of fd */
1437 QLIST_REMOVE(monfd, next);
1438 g_free(monfd->name);
1439 g_free(monfd);
1440 qemu_mutex_unlock(&mon->mon_lock);
1442 return fd;
1445 qemu_mutex_unlock(&mon->mon_lock);
1446 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1447 return -1;
1450 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1452 MonFdsetFd *mon_fdset_fd;
1453 MonFdsetFd *mon_fdset_fd_next;
1455 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1456 if ((mon_fdset_fd->removed ||
1457 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1458 runstate_is_running()) {
1459 close(mon_fdset_fd->fd);
1460 g_free(mon_fdset_fd->opaque);
1461 QLIST_REMOVE(mon_fdset_fd, next);
1462 g_free(mon_fdset_fd);
1466 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1467 QLIST_REMOVE(mon_fdset, next);
1468 g_free(mon_fdset);
1472 void monitor_fdsets_cleanup(void)
1474 MonFdset *mon_fdset;
1475 MonFdset *mon_fdset_next;
1477 qemu_mutex_lock(&mon_fdsets_lock);
1478 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1479 monitor_fdset_cleanup(mon_fdset);
1481 qemu_mutex_unlock(&mon_fdsets_lock);
1484 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1485 const char *opaque, Error **errp)
1487 int fd;
1488 Monitor *mon = cur_mon;
1489 AddfdInfo *fdinfo;
1491 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1492 if (fd == -1) {
1493 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1494 goto error;
1497 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1498 has_opaque, opaque, errp);
1499 if (fdinfo) {
1500 return fdinfo;
1503 error:
1504 if (fd != -1) {
1505 close(fd);
1507 return NULL;
1510 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1512 MonFdset *mon_fdset;
1513 MonFdsetFd *mon_fdset_fd;
1514 char fd_str[60];
1516 qemu_mutex_lock(&mon_fdsets_lock);
1517 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1518 if (mon_fdset->id != fdset_id) {
1519 continue;
1521 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1522 if (has_fd) {
1523 if (mon_fdset_fd->fd != fd) {
1524 continue;
1526 mon_fdset_fd->removed = true;
1527 break;
1528 } else {
1529 mon_fdset_fd->removed = true;
1532 if (has_fd && !mon_fdset_fd) {
1533 goto error;
1535 monitor_fdset_cleanup(mon_fdset);
1536 qemu_mutex_unlock(&mon_fdsets_lock);
1537 return;
1540 error:
1541 qemu_mutex_unlock(&mon_fdsets_lock);
1542 if (has_fd) {
1543 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1544 fdset_id, fd);
1545 } else {
1546 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1548 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1551 FdsetInfoList *qmp_query_fdsets(Error **errp)
1553 MonFdset *mon_fdset;
1554 MonFdsetFd *mon_fdset_fd;
1555 FdsetInfoList *fdset_list = NULL;
1557 qemu_mutex_lock(&mon_fdsets_lock);
1558 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1559 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1560 FdsetFdInfoList *fdsetfd_list = NULL;
1562 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1563 fdset_info->value->fdset_id = mon_fdset->id;
1565 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1566 FdsetFdInfoList *fdsetfd_info;
1568 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1569 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1570 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1571 if (mon_fdset_fd->opaque) {
1572 fdsetfd_info->value->has_opaque = true;
1573 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1574 } else {
1575 fdsetfd_info->value->has_opaque = false;
1578 fdsetfd_info->next = fdsetfd_list;
1579 fdsetfd_list = fdsetfd_info;
1582 fdset_info->value->fds = fdsetfd_list;
1584 fdset_info->next = fdset_list;
1585 fdset_list = fdset_info;
1587 qemu_mutex_unlock(&mon_fdsets_lock);
1589 return fdset_list;
1592 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1593 bool has_opaque, const char *opaque,
1594 Error **errp)
1596 MonFdset *mon_fdset = NULL;
1597 MonFdsetFd *mon_fdset_fd;
1598 AddfdInfo *fdinfo;
1600 qemu_mutex_lock(&mon_fdsets_lock);
1601 if (has_fdset_id) {
1602 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1603 /* Break if match found or match impossible due to ordering by ID */
1604 if (fdset_id <= mon_fdset->id) {
1605 if (fdset_id < mon_fdset->id) {
1606 mon_fdset = NULL;
1608 break;
1613 if (mon_fdset == NULL) {
1614 int64_t fdset_id_prev = -1;
1615 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1617 if (has_fdset_id) {
1618 if (fdset_id < 0) {
1619 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1620 "a non-negative value");
1621 qemu_mutex_unlock(&mon_fdsets_lock);
1622 return NULL;
1624 /* Use specified fdset ID */
1625 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1626 mon_fdset_cur = mon_fdset;
1627 if (fdset_id < mon_fdset_cur->id) {
1628 break;
1631 } else {
1632 /* Use first available fdset ID */
1633 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1634 mon_fdset_cur = mon_fdset;
1635 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1636 fdset_id_prev = mon_fdset_cur->id;
1637 continue;
1639 break;
1643 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1644 if (has_fdset_id) {
1645 mon_fdset->id = fdset_id;
1646 } else {
1647 mon_fdset->id = fdset_id_prev + 1;
1650 /* The fdset list is ordered by fdset ID */
1651 if (!mon_fdset_cur) {
1652 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1653 } else if (mon_fdset->id < mon_fdset_cur->id) {
1654 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1655 } else {
1656 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1660 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1661 mon_fdset_fd->fd = fd;
1662 mon_fdset_fd->removed = false;
1663 if (has_opaque) {
1664 mon_fdset_fd->opaque = g_strdup(opaque);
1666 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1668 fdinfo = g_malloc0(sizeof(*fdinfo));
1669 fdinfo->fdset_id = mon_fdset->id;
1670 fdinfo->fd = mon_fdset_fd->fd;
1672 qemu_mutex_unlock(&mon_fdsets_lock);
1673 return fdinfo;
1676 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1678 #ifdef _WIN32
1679 return -ENOENT;
1680 #else
1681 MonFdset *mon_fdset;
1682 MonFdsetFd *mon_fdset_fd;
1683 int mon_fd_flags;
1684 int ret;
1686 qemu_mutex_lock(&mon_fdsets_lock);
1687 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1688 if (mon_fdset->id != fdset_id) {
1689 continue;
1691 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1692 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1693 if (mon_fd_flags == -1) {
1694 ret = -errno;
1695 goto out;
1698 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1699 ret = mon_fdset_fd->fd;
1700 goto out;
1703 ret = -EACCES;
1704 goto out;
1706 ret = -ENOENT;
1708 out:
1709 qemu_mutex_unlock(&mon_fdsets_lock);
1710 return ret;
1711 #endif
1714 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1716 MonFdset *mon_fdset;
1717 MonFdsetFd *mon_fdset_fd_dup;
1719 qemu_mutex_lock(&mon_fdsets_lock);
1720 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1721 if (mon_fdset->id != fdset_id) {
1722 continue;
1724 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1725 if (mon_fdset_fd_dup->fd == dup_fd) {
1726 goto err;
1729 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1730 mon_fdset_fd_dup->fd = dup_fd;
1731 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1732 qemu_mutex_unlock(&mon_fdsets_lock);
1733 return 0;
1736 err:
1737 qemu_mutex_unlock(&mon_fdsets_lock);
1738 return -1;
1741 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1743 MonFdset *mon_fdset;
1744 MonFdsetFd *mon_fdset_fd_dup;
1746 qemu_mutex_lock(&mon_fdsets_lock);
1747 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1748 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1749 if (mon_fdset_fd_dup->fd == dup_fd) {
1750 if (remove) {
1751 QLIST_REMOVE(mon_fdset_fd_dup, next);
1752 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1753 monitor_fdset_cleanup(mon_fdset);
1755 goto err;
1756 } else {
1757 qemu_mutex_unlock(&mon_fdsets_lock);
1758 return mon_fdset->id;
1764 err:
1765 qemu_mutex_unlock(&mon_fdsets_lock);
1766 return -1;
1769 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1771 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1774 void monitor_fdset_dup_fd_remove(int dup_fd)
1776 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1779 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1781 int fd;
1782 Error *local_err = NULL;
1784 if (!qemu_isdigit(fdname[0]) && mon) {
1785 fd = monitor_get_fd(mon, fdname, &local_err);
1786 } else {
1787 fd = qemu_parse_fd(fdname);
1788 if (fd == -1) {
1789 error_setg(&local_err, "Invalid file descriptor number '%s'",
1790 fdname);
1793 if (local_err) {
1794 error_propagate(errp, local_err);
1795 assert(fd == -1);
1796 } else {
1797 assert(fd != -1);
1800 return fd;
1803 /* Please update hmp-commands.hx when adding or changing commands */
1804 static HMPCommand hmp_info_cmds[] = {
1805 #include "hmp-commands-info.h"
1806 { NULL, NULL, },
1809 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1810 HMPCommand hmp_cmds[] = {
1811 #include "hmp-commands.h"
1812 { NULL, NULL, },
1816 * Set @pval to the value in the register identified by @name.
1817 * return 0 if OK, -1 if not found
1819 int get_monitor_def(int64_t *pval, const char *name)
1821 const MonitorDef *md = target_monitor_defs();
1822 CPUState *cs = mon_get_cpu();
1823 void *ptr;
1824 uint64_t tmp = 0;
1825 int ret;
1827 if (cs == NULL || md == NULL) {
1828 return -1;
1831 for(; md->name != NULL; md++) {
1832 if (hmp_compare_cmd(name, md->name)) {
1833 if (md->get_value) {
1834 *pval = md->get_value(md, md->offset);
1835 } else {
1836 CPUArchState *env = mon_get_cpu_env();
1837 ptr = (uint8_t *)env + md->offset;
1838 switch(md->type) {
1839 case MD_I32:
1840 *pval = *(int32_t *)ptr;
1841 break;
1842 case MD_TLONG:
1843 *pval = *(target_long *)ptr;
1844 break;
1845 default:
1846 *pval = 0;
1847 break;
1850 return 0;
1854 ret = target_get_monitor_def(cs, name, &tmp);
1855 if (!ret) {
1856 *pval = (target_long) tmp;
1859 return ret;
1862 static void add_completion_option(ReadLineState *rs, const char *str,
1863 const char *option)
1865 if (!str || !option) {
1866 return;
1868 if (!strncmp(option, str, strlen(str))) {
1869 readline_add_completion(rs, option);
1873 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1875 size_t len;
1876 ChardevBackendInfoList *list, *start;
1878 if (nb_args != 2) {
1879 return;
1881 len = strlen(str);
1882 readline_set_completion_index(rs, len);
1884 start = list = qmp_query_chardev_backends(NULL);
1885 while (list) {
1886 const char *chr_name = list->value->name;
1888 if (!strncmp(chr_name, str, len)) {
1889 readline_add_completion(rs, chr_name);
1891 list = list->next;
1893 qapi_free_ChardevBackendInfoList(start);
1896 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1898 size_t len;
1899 int i;
1901 if (nb_args != 2) {
1902 return;
1904 len = strlen(str);
1905 readline_set_completion_index(rs, len);
1906 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1907 add_completion_option(rs, str, NetClientDriver_str(i));
1911 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1913 GSList *list, *elt;
1914 size_t len;
1916 if (nb_args != 2) {
1917 return;
1920 len = strlen(str);
1921 readline_set_completion_index(rs, len);
1922 list = elt = object_class_get_list(TYPE_DEVICE, false);
1923 while (elt) {
1924 const char *name;
1925 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1926 TYPE_DEVICE);
1927 name = object_class_get_name(OBJECT_CLASS(dc));
1929 if (dc->user_creatable
1930 && !strncmp(name, str, len)) {
1931 readline_add_completion(rs, name);
1933 elt = elt->next;
1935 g_slist_free(list);
1938 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1940 GSList *list, *elt;
1941 size_t len;
1943 if (nb_args != 2) {
1944 return;
1947 len = strlen(str);
1948 readline_set_completion_index(rs, len);
1949 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1950 while (elt) {
1951 const char *name;
1953 name = object_class_get_name(OBJECT_CLASS(elt->data));
1954 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1955 readline_add_completion(rs, name);
1957 elt = elt->next;
1959 g_slist_free(list);
1962 static void peripheral_device_del_completion(ReadLineState *rs,
1963 const char *str, size_t len)
1965 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1966 GSList *list, *item;
1968 list = qdev_build_hotpluggable_device_list(peripheral);
1969 if (!list) {
1970 return;
1973 for (item = list; item; item = g_slist_next(item)) {
1974 DeviceState *dev = item->data;
1976 if (dev->id && !strncmp(str, dev->id, len)) {
1977 readline_add_completion(rs, dev->id);
1981 g_slist_free(list);
1984 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1986 size_t len;
1987 ChardevInfoList *list, *start;
1989 if (nb_args != 2) {
1990 return;
1992 len = strlen(str);
1993 readline_set_completion_index(rs, len);
1995 start = list = qmp_query_chardev(NULL);
1996 while (list) {
1997 ChardevInfo *chr = list->value;
1999 if (!strncmp(chr->label, str, len)) {
2000 readline_add_completion(rs, chr->label);
2002 list = list->next;
2004 qapi_free_ChardevInfoList(start);
2007 static void ringbuf_completion(ReadLineState *rs, const char *str)
2009 size_t len;
2010 ChardevInfoList *list, *start;
2012 len = strlen(str);
2013 readline_set_completion_index(rs, len);
2015 start = list = qmp_query_chardev(NULL);
2016 while (list) {
2017 ChardevInfo *chr_info = list->value;
2019 if (!strncmp(chr_info->label, str, len)) {
2020 Chardev *chr = qemu_chr_find(chr_info->label);
2021 if (chr && CHARDEV_IS_RINGBUF(chr)) {
2022 readline_add_completion(rs, chr_info->label);
2025 list = list->next;
2027 qapi_free_ChardevInfoList(start);
2030 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
2032 if (nb_args != 2) {
2033 return;
2035 ringbuf_completion(rs, str);
2038 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
2040 size_t len;
2042 if (nb_args != 2) {
2043 return;
2046 len = strlen(str);
2047 readline_set_completion_index(rs, len);
2048 peripheral_device_del_completion(rs, str, len);
2051 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
2053 ObjectPropertyInfoList *list, *start;
2054 size_t len;
2056 if (nb_args != 2) {
2057 return;
2059 len = strlen(str);
2060 readline_set_completion_index(rs, len);
2062 start = list = qmp_qom_list("/objects", NULL);
2063 while (list) {
2064 ObjectPropertyInfo *info = list->value;
2066 if (!strncmp(info->type, "child<", 5)
2067 && !strncmp(info->name, str, len)) {
2068 readline_add_completion(rs, info->name);
2070 list = list->next;
2072 qapi_free_ObjectPropertyInfoList(start);
2075 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
2077 int i;
2078 char *sep;
2079 size_t len;
2081 if (nb_args != 2) {
2082 return;
2084 sep = strrchr(str, '-');
2085 if (sep) {
2086 str = sep + 1;
2088 len = strlen(str);
2089 readline_set_completion_index(rs, len);
2090 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
2091 if (!strncmp(str, QKeyCode_str(i), len)) {
2092 readline_add_completion(rs, QKeyCode_str(i));
2097 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
2099 size_t len;
2101 len = strlen(str);
2102 readline_set_completion_index(rs, len);
2103 if (nb_args == 2) {
2104 NetClientState *ncs[MAX_QUEUE_NUM];
2105 int count, i;
2106 count = qemu_find_net_clients_except(NULL, ncs,
2107 NET_CLIENT_DRIVER_NONE,
2108 MAX_QUEUE_NUM);
2109 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2110 const char *name = ncs[i]->name;
2111 if (!strncmp(str, name, len)) {
2112 readline_add_completion(rs, name);
2115 } else if (nb_args == 3) {
2116 add_completion_option(rs, str, "on");
2117 add_completion_option(rs, str, "off");
2121 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
2123 int len, count, i;
2124 NetClientState *ncs[MAX_QUEUE_NUM];
2126 if (nb_args != 2) {
2127 return;
2130 len = strlen(str);
2131 readline_set_completion_index(rs, len);
2132 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
2133 MAX_QUEUE_NUM);
2134 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
2135 QemuOpts *opts;
2136 const char *name = ncs[i]->name;
2137 if (strncmp(str, name, len)) {
2138 continue;
2140 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
2141 if (opts) {
2142 readline_add_completion(rs, name);
2147 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
2149 size_t len;
2151 len = strlen(str);
2152 readline_set_completion_index(rs, len);
2153 if (nb_args == 2) {
2154 TraceEventIter iter;
2155 TraceEvent *ev;
2156 char *pattern = g_strdup_printf("%s*", str);
2157 trace_event_iter_init(&iter, pattern);
2158 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2159 readline_add_completion(rs, trace_event_get_name(ev));
2161 g_free(pattern);
2165 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
2167 size_t len;
2169 len = strlen(str);
2170 readline_set_completion_index(rs, len);
2171 if (nb_args == 2) {
2172 TraceEventIter iter;
2173 TraceEvent *ev;
2174 char *pattern = g_strdup_printf("%s*", str);
2175 trace_event_iter_init(&iter, pattern);
2176 while ((ev = trace_event_iter_next(&iter)) != NULL) {
2177 readline_add_completion(rs, trace_event_get_name(ev));
2179 g_free(pattern);
2180 } else if (nb_args == 3) {
2181 add_completion_option(rs, str, "on");
2182 add_completion_option(rs, str, "off");
2186 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
2188 int i;
2190 if (nb_args != 2) {
2191 return;
2193 readline_set_completion_index(rs, strlen(str));
2194 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
2195 add_completion_option(rs, str, WatchdogAction_str(i));
2199 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
2200 const char *str)
2202 size_t len;
2204 len = strlen(str);
2205 readline_set_completion_index(rs, len);
2206 if (nb_args == 2) {
2207 int i;
2208 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
2209 const char *name = MigrationCapability_str(i);
2210 if (!strncmp(str, name, len)) {
2211 readline_add_completion(rs, name);
2214 } else if (nb_args == 3) {
2215 add_completion_option(rs, str, "on");
2216 add_completion_option(rs, str, "off");
2220 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
2221 const char *str)
2223 size_t len;
2225 len = strlen(str);
2226 readline_set_completion_index(rs, len);
2227 if (nb_args == 2) {
2228 int i;
2229 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
2230 const char *name = MigrationParameter_str(i);
2231 if (!strncmp(str, name, len)) {
2232 readline_add_completion(rs, name);
2238 static void vm_completion(ReadLineState *rs, const char *str)
2240 size_t len;
2241 BlockDriverState *bs;
2242 BdrvNextIterator it;
2244 len = strlen(str);
2245 readline_set_completion_index(rs, len);
2247 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
2248 SnapshotInfoList *snapshots, *snapshot;
2249 AioContext *ctx = bdrv_get_aio_context(bs);
2250 bool ok = false;
2252 aio_context_acquire(ctx);
2253 if (bdrv_can_snapshot(bs)) {
2254 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
2256 aio_context_release(ctx);
2257 if (!ok) {
2258 continue;
2261 snapshot = snapshots;
2262 while (snapshot) {
2263 char *completion = snapshot->value->name;
2264 if (!strncmp(str, completion, len)) {
2265 readline_add_completion(rs, completion);
2267 completion = snapshot->value->id;
2268 if (!strncmp(str, completion, len)) {
2269 readline_add_completion(rs, completion);
2271 snapshot = snapshot->next;
2273 qapi_free_SnapshotInfoList(snapshots);
2278 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
2280 if (nb_args == 2) {
2281 vm_completion(rs, str);
2285 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
2287 if (nb_args == 2) {
2288 vm_completion(rs, str);
2292 static int
2293 compare_mon_cmd(const void *a, const void *b)
2295 return strcmp(((const HMPCommand *)a)->name,
2296 ((const HMPCommand *)b)->name);
2299 static void sortcmdlist(void)
2301 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
2302 sizeof(*hmp_cmds),
2303 compare_mon_cmd);
2304 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
2305 sizeof(*hmp_info_cmds),
2306 compare_mon_cmd);
2309 void monitor_init_globals(void)
2311 monitor_init_globals_core();
2312 monitor_init_qmp_commands();
2313 sortcmdlist();
2314 qemu_mutex_init(&mon_fdsets_lock);