migration: Move the QMP command from monitor/ to migration/
[qemu/armbru.git] / monitor / misc.c
blobff3002a8808799c8020c75073850d73c8c6de795
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "exec/gdbstub.h"
29 #include "net/slirp.h"
30 #include "qemu/ctype.h"
31 #include "disas/disas.h"
32 #include "qemu/log.h"
33 #include "sysemu/hw_accel.h"
34 #include "sysemu/runstate.h"
35 #include "sysemu/sysemu.h"
36 #include "sysemu/device_tree.h"
37 #include "qapi/qmp/qdict.h"
38 #include "qapi/qmp/qerror.h"
39 #include "monitor/hmp-target.h"
40 #include "monitor/hmp.h"
41 #include "exec/address-spaces.h"
42 #include "exec/ioport.h"
43 #include "block/block-hmp-cmds.h"
44 #include "qapi/qapi-commands-control.h"
45 #include "qapi/qapi-commands-misc.h"
46 #include "qapi/qapi-commands-run-state.h"
47 #include "qapi/qapi-commands-machine.h"
48 #include "qapi/qapi-init-commands.h"
49 #include "qapi/error.h"
50 #include "qemu/cutils.h"
52 #if defined(TARGET_S390X)
53 #include "hw/s390x/storage-keys.h"
54 #include "hw/s390x/storage-attributes.h"
55 #endif
57 /* Make devices configuration available for use in hmp-commands*.hx templates */
58 #include CONFIG_DEVICES
60 /* file descriptors passed via SCM_RIGHTS */
61 typedef struct mon_fd_t mon_fd_t;
62 struct mon_fd_t {
63 char *name;
64 int fd;
65 QLIST_ENTRY(mon_fd_t) next;
68 /* file descriptor associated with a file descriptor set */
69 typedef struct MonFdsetFd MonFdsetFd;
70 struct MonFdsetFd {
71 int fd;
72 bool removed;
73 char *opaque;
74 QLIST_ENTRY(MonFdsetFd) next;
77 /* file descriptor set containing fds passed via SCM_RIGHTS */
78 typedef struct MonFdset MonFdset;
79 struct MonFdset {
80 int64_t id;
81 QLIST_HEAD(, MonFdsetFd) fds;
82 QLIST_HEAD(, MonFdsetFd) dup_fds;
83 QLIST_ENTRY(MonFdset) next;
86 /* Protects mon_fdsets */
87 static QemuMutex mon_fdsets_lock;
88 static QLIST_HEAD(, MonFdset) mon_fdsets;
90 static HMPCommand hmp_info_cmds[];
92 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
93 int64_t cpu_index, Error **errp)
95 char *output = NULL;
96 MonitorHMP hmp = {};
98 monitor_data_init(&hmp.common, false, true, false);
100 if (has_cpu_index) {
101 int ret = monitor_set_cpu(&hmp.common, cpu_index);
102 if (ret < 0) {
103 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
104 "a CPU number");
105 goto out;
109 handle_hmp_command(&hmp, command_line);
111 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
112 output = g_strdup(hmp.common.outbuf->str);
115 out:
116 monitor_data_destroy(&hmp.common);
117 return output;
121 * Is @name in the '|' separated list of names @list?
123 int hmp_compare_cmd(const char *name, const char *list)
125 const char *p, *pstart;
126 int len;
127 len = strlen(name);
128 p = list;
129 for (;;) {
130 pstart = p;
131 p = qemu_strchrnul(p, '|');
132 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
133 return 1;
135 if (*p == '\0') {
136 break;
138 p++;
140 return 0;
143 static void do_help_cmd(Monitor *mon, const QDict *qdict)
145 hmp_help_cmd(mon, qdict_get_try_str(qdict, "name"));
148 static void hmp_info_help(Monitor *mon, const QDict *qdict)
150 hmp_help_cmd(mon, "info");
153 static void monitor_init_qmp_commands(void)
156 * Two command lists:
157 * - qmp_commands contains all QMP commands
158 * - qmp_cap_negotiation_commands contains just
159 * "qmp_capabilities", to enforce capability negotiation
162 qmp_init_marshal(&qmp_commands);
164 qmp_register_command(&qmp_commands, "device_add",
165 qmp_device_add, 0, 0);
167 QTAILQ_INIT(&qmp_cap_negotiation_commands);
168 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
169 qmp_marshal_qmp_capabilities,
170 QCO_ALLOW_PRECONFIG, 0);
173 /* Set the current CPU defined by the user. Callers must hold BQL. */
174 int monitor_set_cpu(Monitor *mon, int cpu_index)
176 CPUState *cpu;
178 cpu = qemu_get_cpu(cpu_index);
179 if (cpu == NULL) {
180 return -1;
182 g_free(mon->mon_cpu_path);
183 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
184 return 0;
187 /* Callers must hold BQL. */
188 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
190 CPUState *cpu = NULL;
192 if (mon->mon_cpu_path) {
193 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
194 TYPE_CPU, NULL);
195 if (!cpu) {
196 g_free(mon->mon_cpu_path);
197 mon->mon_cpu_path = NULL;
200 if (!mon->mon_cpu_path) {
201 if (!first_cpu) {
202 return NULL;
204 monitor_set_cpu(mon, first_cpu->cpu_index);
205 cpu = first_cpu;
207 assert(cpu != NULL);
208 if (synchronize) {
209 cpu_synchronize_state(cpu);
211 return cpu;
214 CPUState *mon_get_cpu(Monitor *mon)
216 return mon_get_cpu_sync(mon, true);
219 CPUArchState *mon_get_cpu_env(Monitor *mon)
221 CPUState *cs = mon_get_cpu(mon);
223 return cs ? cs->env_ptr : NULL;
226 int monitor_get_cpu_index(Monitor *mon)
228 CPUState *cs = mon_get_cpu_sync(mon, false);
230 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
233 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
235 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
236 int vcpu = qdict_get_try_int(qdict, "vcpu", -1);
237 CPUState *cs;
239 if (all_cpus) {
240 CPU_FOREACH(cs) {
241 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
242 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
244 } else {
245 cs = vcpu >= 0 ? qemu_get_cpu(vcpu) : mon_get_cpu(mon);
247 if (!cs) {
248 if (vcpu >= 0) {
249 monitor_printf(mon, "CPU#%d not available\n", vcpu);
250 } else {
251 monitor_printf(mon, "No CPU available\n");
253 return;
256 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
257 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
261 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
263 int64_t max = qdict_get_try_int(qdict, "max", 10);
264 bool mean = qdict_get_try_bool(qdict, "mean", false);
265 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
266 enum QSPSortBy sort_by;
268 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
269 qsp_report(max, sort_by, coalesce);
272 static void hmp_info_history(Monitor *mon, const QDict *qdict)
274 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
275 int i;
276 const char *str;
278 if (!hmp_mon->rs) {
279 return;
281 i = 0;
282 for(;;) {
283 str = readline_get_history(hmp_mon->rs, i);
284 if (!str) {
285 break;
287 monitor_printf(mon, "%d: '%s'\n", i, str);
288 i++;
292 static void hmp_logfile(Monitor *mon, const QDict *qdict)
294 Error *err = NULL;
296 if (!qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err)) {
297 error_report_err(err);
301 static void hmp_log(Monitor *mon, const QDict *qdict)
303 int mask;
304 const char *items = qdict_get_str(qdict, "items");
305 Error *err = NULL;
307 if (!strcmp(items, "none")) {
308 mask = 0;
309 } else {
310 mask = qemu_str_to_log_mask(items);
311 if (!mask) {
312 hmp_help_cmd(mon, "log");
313 return;
317 if (!qemu_set_log(mask, &err)) {
318 error_report_err(err);
322 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
324 const char *option = qdict_get_try_str(qdict, "option");
325 if (!option || !strcmp(option, "on")) {
326 singlestep = 1;
327 } else if (!strcmp(option, "off")) {
328 singlestep = 0;
329 } else {
330 monitor_printf(mon, "unexpected option %s\n", option);
334 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
336 const char *device = qdict_get_try_str(qdict, "device");
337 if (!device) {
338 device = "tcp::" DEFAULT_GDBSTUB_PORT;
341 if (gdbserver_start(device) < 0) {
342 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
343 device);
344 } else if (strcmp(device, "none") == 0) {
345 monitor_printf(mon, "Disabled gdbserver\n");
346 } else {
347 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
348 device);
352 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
354 Error *err = NULL;
355 WatchdogAction action;
356 char *qapi_value;
358 qapi_value = g_ascii_strdown(qdict_get_str(qdict, "action"), -1);
359 action = qapi_enum_parse(&WatchdogAction_lookup, qapi_value, -1, &err);
360 g_free(qapi_value);
361 if (err) {
362 hmp_handle_error(mon, err);
363 return;
365 qmp_watchdog_set_action(action, &error_abort);
368 static void monitor_printc(Monitor *mon, int c)
370 monitor_printf(mon, "'");
371 switch(c) {
372 case '\'':
373 monitor_printf(mon, "\\'");
374 break;
375 case '\\':
376 monitor_printf(mon, "\\\\");
377 break;
378 case '\n':
379 monitor_printf(mon, "\\n");
380 break;
381 case '\r':
382 monitor_printf(mon, "\\r");
383 break;
384 default:
385 if (c >= 32 && c <= 126) {
386 monitor_printf(mon, "%c", c);
387 } else {
388 monitor_printf(mon, "\\x%02x", c);
390 break;
392 monitor_printf(mon, "'");
395 static void memory_dump(Monitor *mon, int count, int format, int wsize,
396 hwaddr addr, int is_physical)
398 int l, line_size, i, max_digits, len;
399 uint8_t buf[16];
400 uint64_t v;
401 CPUState *cs = mon_get_cpu(mon);
403 if (!cs && (format == 'i' || !is_physical)) {
404 monitor_printf(mon, "Can not dump without CPU\n");
405 return;
408 if (format == 'i') {
409 monitor_disas(mon, cs, addr, count, is_physical);
410 return;
413 len = wsize * count;
414 if (wsize == 1) {
415 line_size = 8;
416 } else {
417 line_size = 16;
419 max_digits = 0;
421 switch(format) {
422 case 'o':
423 max_digits = DIV_ROUND_UP(wsize * 8, 3);
424 break;
425 default:
426 case 'x':
427 max_digits = (wsize * 8) / 4;
428 break;
429 case 'u':
430 case 'd':
431 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
432 break;
433 case 'c':
434 wsize = 1;
435 break;
438 while (len > 0) {
439 if (is_physical) {
440 monitor_printf(mon, HWADDR_FMT_plx ":", addr);
441 } else {
442 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
444 l = len;
445 if (l > line_size)
446 l = line_size;
447 if (is_physical) {
448 AddressSpace *as = cs ? cs->as : &address_space_memory;
449 MemTxResult r = address_space_read(as, addr,
450 MEMTXATTRS_UNSPECIFIED, buf, l);
451 if (r != MEMTX_OK) {
452 monitor_printf(mon, " Cannot access memory\n");
453 break;
455 } else {
456 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
457 monitor_printf(mon, " Cannot access memory\n");
458 break;
461 i = 0;
462 while (i < l) {
463 switch(wsize) {
464 default:
465 case 1:
466 v = ldub_p(buf + i);
467 break;
468 case 2:
469 v = lduw_p(buf + i);
470 break;
471 case 4:
472 v = (uint32_t)ldl_p(buf + i);
473 break;
474 case 8:
475 v = ldq_p(buf + i);
476 break;
478 monitor_printf(mon, " ");
479 switch(format) {
480 case 'o':
481 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
482 break;
483 case 'x':
484 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
485 break;
486 case 'u':
487 monitor_printf(mon, "%*" PRIu64, max_digits, v);
488 break;
489 case 'd':
490 monitor_printf(mon, "%*" PRId64, max_digits, v);
491 break;
492 case 'c':
493 monitor_printc(mon, v);
494 break;
496 i += wsize;
498 monitor_printf(mon, "\n");
499 addr += l;
500 len -= l;
504 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
506 int count = qdict_get_int(qdict, "count");
507 int format = qdict_get_int(qdict, "format");
508 int size = qdict_get_int(qdict, "size");
509 target_long addr = qdict_get_int(qdict, "addr");
511 memory_dump(mon, count, format, size, addr, 0);
514 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
516 int count = qdict_get_int(qdict, "count");
517 int format = qdict_get_int(qdict, "format");
518 int size = qdict_get_int(qdict, "size");
519 hwaddr addr = qdict_get_int(qdict, "addr");
521 memory_dump(mon, count, format, size, addr, 1);
524 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
526 Int128 gpa_region_size;
527 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
528 addr, size);
530 if (!mrs.mr) {
531 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
532 return NULL;
535 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
536 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
537 memory_region_unref(mrs.mr);
538 return NULL;
541 gpa_region_size = int128_make64(size);
542 if (int128_lt(mrs.size, gpa_region_size)) {
543 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
544 " exceeded.", addr);
545 memory_region_unref(mrs.mr);
546 return NULL;
549 *p_mr = mrs.mr;
550 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
553 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
555 hwaddr addr = qdict_get_int(qdict, "addr");
556 Error *local_err = NULL;
557 MemoryRegion *mr = NULL;
558 void *ptr;
560 ptr = gpa2hva(&mr, addr, 1, &local_err);
561 if (local_err) {
562 error_report_err(local_err);
563 return;
566 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
567 " (%s) is %p\n",
568 addr, mr->name, ptr);
570 memory_region_unref(mr);
573 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
575 target_ulong addr = qdict_get_int(qdict, "addr");
576 MemTxAttrs attrs;
577 CPUState *cs = mon_get_cpu(mon);
578 hwaddr gpa;
580 if (!cs) {
581 monitor_printf(mon, "No cpu\n");
582 return;
585 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
586 if (gpa == -1) {
587 monitor_printf(mon, "Unmapped\n");
588 } else {
589 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
590 gpa + (addr & ~TARGET_PAGE_MASK));
594 #ifdef CONFIG_LINUX
595 static uint64_t vtop(void *ptr, Error **errp)
597 uint64_t pinfo;
598 uint64_t ret = -1;
599 uintptr_t addr = (uintptr_t) ptr;
600 uintptr_t pagesize = qemu_real_host_page_size();
601 off_t offset = addr / pagesize * sizeof(pinfo);
602 int fd;
604 fd = open("/proc/self/pagemap", O_RDONLY);
605 if (fd == -1) {
606 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
607 return -1;
610 /* Force copy-on-write if necessary. */
611 qatomic_add((uint8_t *)ptr, 0);
613 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
614 error_setg_errno(errp, errno, "Cannot read pagemap");
615 goto out;
617 if ((pinfo & (1ull << 63)) == 0) {
618 error_setg(errp, "Page not present");
619 goto out;
621 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
623 out:
624 close(fd);
625 return ret;
628 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
630 hwaddr addr = qdict_get_int(qdict, "addr");
631 Error *local_err = NULL;
632 MemoryRegion *mr = NULL;
633 void *ptr;
634 uint64_t physaddr;
636 ptr = gpa2hva(&mr, addr, 1, &local_err);
637 if (local_err) {
638 error_report_err(local_err);
639 return;
642 physaddr = vtop(ptr, &local_err);
643 if (local_err) {
644 error_report_err(local_err);
645 } else {
646 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
647 " (%s) is 0x%" PRIx64 "\n",
648 addr, mr->name, (uint64_t) physaddr);
651 memory_region_unref(mr);
653 #endif
655 static void do_print(Monitor *mon, const QDict *qdict)
657 int format = qdict_get_int(qdict, "format");
658 hwaddr val = qdict_get_int(qdict, "val");
660 switch(format) {
661 case 'o':
662 monitor_printf(mon, "%#" HWADDR_PRIo, val);
663 break;
664 case 'x':
665 monitor_printf(mon, "%#" HWADDR_PRIx, val);
666 break;
667 case 'u':
668 monitor_printf(mon, "%" HWADDR_PRIu, val);
669 break;
670 default:
671 case 'd':
672 monitor_printf(mon, "%" HWADDR_PRId, val);
673 break;
674 case 'c':
675 monitor_printc(mon, val);
676 break;
678 monitor_printf(mon, "\n");
681 static void hmp_sum(Monitor *mon, const QDict *qdict)
683 uint32_t addr;
684 uint16_t sum;
685 uint32_t start = qdict_get_int(qdict, "start");
686 uint32_t size = qdict_get_int(qdict, "size");
688 sum = 0;
689 for(addr = start; addr < (start + size); addr++) {
690 uint8_t val = address_space_ldub(&address_space_memory, addr,
691 MEMTXATTRS_UNSPECIFIED, NULL);
692 /* BSD sum algorithm ('sum' Unix command) */
693 sum = (sum >> 1) | (sum << 15);
694 sum += val;
696 monitor_printf(mon, "%05d\n", sum);
699 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
701 int size = qdict_get_int(qdict, "size");
702 int addr = qdict_get_int(qdict, "addr");
703 int has_index = qdict_haskey(qdict, "index");
704 uint32_t val;
705 int suffix;
707 if (has_index) {
708 int index = qdict_get_int(qdict, "index");
709 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
710 addr++;
712 addr &= 0xffff;
714 switch(size) {
715 default:
716 case 1:
717 val = cpu_inb(addr);
718 suffix = 'b';
719 break;
720 case 2:
721 val = cpu_inw(addr);
722 suffix = 'w';
723 break;
724 case 4:
725 val = cpu_inl(addr);
726 suffix = 'l';
727 break;
729 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
730 suffix, addr, size * 2, val);
733 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
735 int size = qdict_get_int(qdict, "size");
736 int addr = qdict_get_int(qdict, "addr");
737 int val = qdict_get_int(qdict, "val");
739 addr &= IOPORTS_MASK;
741 switch (size) {
742 default:
743 case 1:
744 cpu_outb(addr, val);
745 break;
746 case 2:
747 cpu_outw(addr, val);
748 break;
749 case 4:
750 cpu_outl(addr, val);
751 break;
755 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
757 Error *local_err = NULL;
758 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
760 qemu_boot_set(bootdevice, &local_err);
761 if (local_err) {
762 error_report_err(local_err);
763 } else {
764 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
768 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
770 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
771 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
772 bool owner = qdict_get_try_bool(qdict, "owner", false);
773 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
775 mtree_info(flatview, dispatch_tree, owner, disabled);
778 void qmp_getfd(const char *fdname, Error **errp)
780 Monitor *cur_mon = monitor_cur();
781 mon_fd_t *monfd;
782 int fd, tmp_fd;
784 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
785 if (fd == -1) {
786 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
787 return;
790 if (qemu_isdigit(fdname[0])) {
791 close(fd);
792 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
793 "a name not starting with a digit");
794 return;
797 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
798 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
799 if (strcmp(monfd->name, fdname) != 0) {
800 continue;
803 tmp_fd = monfd->fd;
804 monfd->fd = fd;
805 /* Make sure close() is outside critical section */
806 close(tmp_fd);
807 return;
810 monfd = g_new0(mon_fd_t, 1);
811 monfd->name = g_strdup(fdname);
812 monfd->fd = fd;
814 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
817 void qmp_closefd(const char *fdname, Error **errp)
819 Monitor *cur_mon = monitor_cur();
820 mon_fd_t *monfd;
821 int tmp_fd;
823 qemu_mutex_lock(&cur_mon->mon_lock);
824 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
825 if (strcmp(monfd->name, fdname) != 0) {
826 continue;
829 QLIST_REMOVE(monfd, next);
830 tmp_fd = monfd->fd;
831 g_free(monfd->name);
832 g_free(monfd);
833 qemu_mutex_unlock(&cur_mon->mon_lock);
834 /* Make sure close() is outside critical section */
835 close(tmp_fd);
836 return;
839 qemu_mutex_unlock(&cur_mon->mon_lock);
840 error_setg(errp, "File descriptor named '%s' not found", fdname);
843 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
845 mon_fd_t *monfd;
847 QEMU_LOCK_GUARD(&mon->mon_lock);
848 QLIST_FOREACH(monfd, &mon->fds, next) {
849 int fd;
851 if (strcmp(monfd->name, fdname) != 0) {
852 continue;
855 fd = monfd->fd;
856 assert(fd >= 0);
858 /* caller takes ownership of fd */
859 QLIST_REMOVE(monfd, next);
860 g_free(monfd->name);
861 g_free(monfd);
863 return fd;
866 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
867 return -1;
870 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
872 MonFdsetFd *mon_fdset_fd;
873 MonFdsetFd *mon_fdset_fd_next;
875 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
876 if ((mon_fdset_fd->removed ||
877 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
878 runstate_is_running()) {
879 close(mon_fdset_fd->fd);
880 g_free(mon_fdset_fd->opaque);
881 QLIST_REMOVE(mon_fdset_fd, next);
882 g_free(mon_fdset_fd);
886 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
887 QLIST_REMOVE(mon_fdset, next);
888 g_free(mon_fdset);
892 void monitor_fdsets_cleanup(void)
894 MonFdset *mon_fdset;
895 MonFdset *mon_fdset_next;
897 QEMU_LOCK_GUARD(&mon_fdsets_lock);
898 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
899 monitor_fdset_cleanup(mon_fdset);
903 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id,
904 const char *opaque, Error **errp)
906 int fd;
907 Monitor *mon = monitor_cur();
908 AddfdInfo *fdinfo;
910 fd = qemu_chr_fe_get_msgfd(&mon->chr);
911 if (fd == -1) {
912 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
913 goto error;
916 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id, opaque, errp);
917 if (fdinfo) {
918 return fdinfo;
921 error:
922 if (fd != -1) {
923 close(fd);
925 return NULL;
928 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
930 MonFdset *mon_fdset;
931 MonFdsetFd *mon_fdset_fd;
932 char fd_str[60];
934 QEMU_LOCK_GUARD(&mon_fdsets_lock);
935 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
936 if (mon_fdset->id != fdset_id) {
937 continue;
939 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
940 if (has_fd) {
941 if (mon_fdset_fd->fd != fd) {
942 continue;
944 mon_fdset_fd->removed = true;
945 break;
946 } else {
947 mon_fdset_fd->removed = true;
950 if (has_fd && !mon_fdset_fd) {
951 goto error;
953 monitor_fdset_cleanup(mon_fdset);
954 return;
957 error:
958 if (has_fd) {
959 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
960 fdset_id, fd);
961 } else {
962 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
964 error_setg(errp, "File descriptor named '%s' not found", fd_str);
967 FdsetInfoList *qmp_query_fdsets(Error **errp)
969 MonFdset *mon_fdset;
970 MonFdsetFd *mon_fdset_fd;
971 FdsetInfoList *fdset_list = NULL;
973 QEMU_LOCK_GUARD(&mon_fdsets_lock);
974 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
975 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
977 fdset_info->fdset_id = mon_fdset->id;
979 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
980 FdsetFdInfo *fdsetfd_info;
982 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
983 fdsetfd_info->fd = mon_fdset_fd->fd;
984 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
986 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
989 QAPI_LIST_PREPEND(fdset_list, fdset_info);
992 return fdset_list;
995 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
996 const char *opaque, Error **errp)
998 MonFdset *mon_fdset = NULL;
999 MonFdsetFd *mon_fdset_fd;
1000 AddfdInfo *fdinfo;
1002 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1003 if (has_fdset_id) {
1004 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1005 /* Break if match found or match impossible due to ordering by ID */
1006 if (fdset_id <= mon_fdset->id) {
1007 if (fdset_id < mon_fdset->id) {
1008 mon_fdset = NULL;
1010 break;
1015 if (mon_fdset == NULL) {
1016 int64_t fdset_id_prev = -1;
1017 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1019 if (has_fdset_id) {
1020 if (fdset_id < 0) {
1021 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1022 "a non-negative value");
1023 return NULL;
1025 /* Use specified fdset ID */
1026 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1027 mon_fdset_cur = mon_fdset;
1028 if (fdset_id < mon_fdset_cur->id) {
1029 break;
1032 } else {
1033 /* Use first available fdset ID */
1034 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1035 mon_fdset_cur = mon_fdset;
1036 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1037 fdset_id_prev = mon_fdset_cur->id;
1038 continue;
1040 break;
1044 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1045 if (has_fdset_id) {
1046 mon_fdset->id = fdset_id;
1047 } else {
1048 mon_fdset->id = fdset_id_prev + 1;
1051 /* The fdset list is ordered by fdset ID */
1052 if (!mon_fdset_cur) {
1053 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1054 } else if (mon_fdset->id < mon_fdset_cur->id) {
1055 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1056 } else {
1057 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1061 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1062 mon_fdset_fd->fd = fd;
1063 mon_fdset_fd->removed = false;
1064 mon_fdset_fd->opaque = g_strdup(opaque);
1065 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1067 fdinfo = g_malloc0(sizeof(*fdinfo));
1068 fdinfo->fdset_id = mon_fdset->id;
1069 fdinfo->fd = mon_fdset_fd->fd;
1071 return fdinfo;
1074 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1076 #ifdef _WIN32
1077 return -ENOENT;
1078 #else
1079 MonFdset *mon_fdset;
1081 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1082 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1083 MonFdsetFd *mon_fdset_fd;
1084 MonFdsetFd *mon_fdset_fd_dup;
1085 int fd = -1;
1086 int dup_fd;
1087 int mon_fd_flags;
1089 if (mon_fdset->id != fdset_id) {
1090 continue;
1093 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1094 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1095 if (mon_fd_flags == -1) {
1096 return -1;
1099 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1100 fd = mon_fdset_fd->fd;
1101 break;
1105 if (fd == -1) {
1106 errno = EACCES;
1107 return -1;
1110 dup_fd = qemu_dup_flags(fd, flags);
1111 if (dup_fd == -1) {
1112 return -1;
1115 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1116 mon_fdset_fd_dup->fd = dup_fd;
1117 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1118 return dup_fd;
1121 errno = ENOENT;
1122 return -1;
1123 #endif
1126 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1128 MonFdset *mon_fdset;
1129 MonFdsetFd *mon_fdset_fd_dup;
1131 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1132 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1133 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1134 if (mon_fdset_fd_dup->fd == dup_fd) {
1135 if (remove) {
1136 QLIST_REMOVE(mon_fdset_fd_dup, next);
1137 g_free(mon_fdset_fd_dup);
1138 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1139 monitor_fdset_cleanup(mon_fdset);
1141 return -1;
1142 } else {
1143 return mon_fdset->id;
1149 return -1;
1152 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1154 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1157 void monitor_fdset_dup_fd_remove(int dup_fd)
1159 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1162 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1164 int fd;
1166 if (!qemu_isdigit(fdname[0]) && mon) {
1167 fd = monitor_get_fd(mon, fdname, errp);
1168 } else {
1169 fd = qemu_parse_fd(fdname);
1170 if (fd < 0) {
1171 error_setg(errp, "Invalid file descriptor number '%s'",
1172 fdname);
1176 return fd;
1179 /* Please update hmp-commands.hx when adding or changing commands */
1180 static HMPCommand hmp_info_cmds[] = {
1181 #include "hmp-commands-info.h"
1182 { NULL, NULL, },
1185 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1186 HMPCommand hmp_cmds[] = {
1187 #include "hmp-commands.h"
1188 { NULL, NULL, },
1192 * Set @pval to the value in the register identified by @name.
1193 * return 0 if OK, -1 if not found
1195 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1197 const MonitorDef *md = target_monitor_defs();
1198 CPUState *cs = mon_get_cpu(mon);
1199 void *ptr;
1200 uint64_t tmp = 0;
1201 int ret;
1203 if (cs == NULL || md == NULL) {
1204 return -1;
1207 for(; md->name != NULL; md++) {
1208 if (hmp_compare_cmd(name, md->name)) {
1209 if (md->get_value) {
1210 *pval = md->get_value(mon, md, md->offset);
1211 } else {
1212 CPUArchState *env = mon_get_cpu_env(mon);
1213 ptr = (uint8_t *)env + md->offset;
1214 switch(md->type) {
1215 case MD_I32:
1216 *pval = *(int32_t *)ptr;
1217 break;
1218 case MD_TLONG:
1219 *pval = *(target_long *)ptr;
1220 break;
1221 default:
1222 *pval = 0;
1223 break;
1226 return 0;
1230 ret = target_get_monitor_def(cs, name, &tmp);
1231 if (!ret) {
1232 *pval = (target_long) tmp;
1235 return ret;
1238 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1240 GSList *list, *elt;
1241 size_t len;
1243 if (nb_args != 2) {
1244 return;
1247 len = strlen(str);
1248 readline_set_completion_index(rs, len);
1249 list = elt = object_class_get_list(TYPE_DEVICE, false);
1250 while (elt) {
1251 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1252 TYPE_DEVICE);
1254 if (dc->user_creatable) {
1255 readline_add_completion_of(rs, str,
1256 object_class_get_name(OBJECT_CLASS(dc)));
1258 elt = elt->next;
1260 g_slist_free(list);
1263 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1265 GSList **list = opaque;
1266 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1268 if (dev == NULL) {
1269 return 0;
1272 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1273 *list = g_slist_append(*list, dev);
1276 return 0;
1279 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1281 GSList *list = NULL;
1283 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1285 return list;
1288 static void peripheral_device_del_completion(ReadLineState *rs,
1289 const char *str)
1291 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1292 GSList *list, *item;
1294 list = qdev_build_hotpluggable_device_list(peripheral);
1295 if (!list) {
1296 return;
1299 for (item = list; item; item = g_slist_next(item)) {
1300 DeviceState *dev = item->data;
1302 if (dev->id) {
1303 readline_add_completion_of(rs, str, dev->id);
1307 g_slist_free(list);
1310 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1312 if (nb_args != 2) {
1313 return;
1316 readline_set_completion_index(rs, strlen(str));
1317 peripheral_device_del_completion(rs, str);
1320 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1322 int i;
1324 if (nb_args != 2) {
1325 return;
1327 readline_set_completion_index(rs, strlen(str));
1328 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1329 readline_add_completion_of(rs, str, WatchdogAction_str(i));
1333 static int
1334 compare_mon_cmd(const void *a, const void *b)
1336 return strcmp(((const HMPCommand *)a)->name,
1337 ((const HMPCommand *)b)->name);
1340 static void sortcmdlist(void)
1342 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1343 sizeof(*hmp_cmds),
1344 compare_mon_cmd);
1345 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1346 sizeof(*hmp_info_cmds),
1347 compare_mon_cmd);
1350 void monitor_register_hmp(const char *name, bool info,
1351 void (*cmd)(Monitor *mon, const QDict *qdict))
1353 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1355 while (table->name != NULL) {
1356 if (strcmp(table->name, name) == 0) {
1357 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1358 table->cmd = cmd;
1359 return;
1361 table++;
1363 g_assert_not_reached();
1366 void monitor_register_hmp_info_hrt(const char *name,
1367 HumanReadableText *(*handler)(Error **errp))
1369 HMPCommand *table = hmp_info_cmds;
1371 while (table->name != NULL) {
1372 if (strcmp(table->name, name) == 0) {
1373 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1374 table->cmd_info_hrt = handler;
1375 return;
1377 table++;
1379 g_assert_not_reached();
1382 void monitor_init_globals(void)
1384 monitor_init_globals_core();
1385 monitor_init_qmp_commands();
1386 sortcmdlist();
1387 qemu_mutex_init(&mon_fdsets_lock);