]> git.proxmox.com Git - mirror_qemu.git/blob - monitor/misc.c
qapi: introduce x-query-roms QMP command
[mirror_qemu.git] / monitor / misc.c
1 /*
2 * QEMU monitor
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24
25 #include "qemu/osdep.h"
26 #include "monitor-internal.h"
27 #include "monitor/qdev.h"
28 #include "hw/usb.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "ui/qemu-spice.h"
36 #include "qemu/config-file.h"
37 #include "qemu/ctype.h"
38 #include "ui/console.h"
39 #include "ui/input.h"
40 #include "audio/audio.h"
41 #include "disas/disas.h"
42 #include "sysemu/balloon.h"
43 #include "qemu/timer.h"
44 #include "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "authz/list.h"
47 #include "qapi/util.h"
48 #include "sysemu/blockdev.h"
49 #include "sysemu/sysemu.h"
50 #include "sysemu/tcg.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qdict.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qstring.h"
55 #include "qom/object_interfaces.h"
56 #include "trace/control.h"
57 #include "monitor/hmp-target.h"
58 #include "monitor/hmp.h"
59 #ifdef CONFIG_TRACE_SIMPLE
60 #include "trace/simple.h"
61 #endif
62 #include "exec/memory.h"
63 #include "exec/exec-all.h"
64 #include "qemu/option.h"
65 #include "qemu/thread.h"
66 #include "block/qapi.h"
67 #include "block/block-hmp-cmds.h"
68 #include "qapi/qapi-commands-char.h"
69 #include "qapi/qapi-commands-control.h"
70 #include "qapi/qapi-commands-migration.h"
71 #include "qapi/qapi-commands-misc.h"
72 #include "qapi/qapi-commands-qom.h"
73 #include "qapi/qapi-commands-trace.h"
74 #include "qapi/qapi-commands-machine.h"
75 #include "qapi/qapi-init-commands.h"
76 #include "qapi/error.h"
77 #include "qapi/qmp-event.h"
78 #include "sysemu/cpus.h"
79 #include "qemu/cutils.h"
80
81 #if defined(TARGET_S390X)
82 #include "hw/s390x/storage-keys.h"
83 #include "hw/s390x/storage-attributes.h"
84 #endif
85
86 /* file descriptors passed via SCM_RIGHTS */
87 typedef struct mon_fd_t mon_fd_t;
88 struct mon_fd_t {
89 char *name;
90 int fd;
91 QLIST_ENTRY(mon_fd_t) next;
92 };
93
94 /* file descriptor associated with a file descriptor set */
95 typedef struct MonFdsetFd MonFdsetFd;
96 struct MonFdsetFd {
97 int fd;
98 bool removed;
99 char *opaque;
100 QLIST_ENTRY(MonFdsetFd) next;
101 };
102
103 /* file descriptor set containing fds passed via SCM_RIGHTS */
104 typedef struct MonFdset MonFdset;
105 struct MonFdset {
106 int64_t id;
107 QLIST_HEAD(, MonFdsetFd) fds;
108 QLIST_HEAD(, MonFdsetFd) dup_fds;
109 QLIST_ENTRY(MonFdset) next;
110 };
111
112 /* Protects mon_fdsets */
113 static QemuMutex mon_fdsets_lock;
114 static QLIST_HEAD(, MonFdset) mon_fdsets;
115
116 static HMPCommand hmp_info_cmds[];
117
118 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
119 int64_t cpu_index, Error **errp)
120 {
121 char *output = NULL;
122 MonitorHMP hmp = {};
123
124 monitor_data_init(&hmp.common, false, true, false);
125
126 if (has_cpu_index) {
127 int ret = monitor_set_cpu(&hmp.common, cpu_index);
128 if (ret < 0) {
129 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
130 "a CPU number");
131 goto out;
132 }
133 }
134
135 handle_hmp_command(&hmp, command_line);
136
137 WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
138 output = g_strdup(hmp.common.outbuf->str);
139 }
140
141 out:
142 monitor_data_destroy(&hmp.common);
143 return output;
144 }
145
146 /**
147 * Is @name in the '|' separated list of names @list?
148 */
149 int hmp_compare_cmd(const char *name, const char *list)
150 {
151 const char *p, *pstart;
152 int len;
153 len = strlen(name);
154 p = list;
155 for (;;) {
156 pstart = p;
157 p = qemu_strchrnul(p, '|');
158 if ((p - pstart) == len && !memcmp(pstart, name, len)) {
159 return 1;
160 }
161 if (*p == '\0') {
162 break;
163 }
164 p++;
165 }
166 return 0;
167 }
168
169 static void do_help_cmd(Monitor *mon, const QDict *qdict)
170 {
171 help_cmd(mon, qdict_get_try_str(qdict, "name"));
172 }
173
174 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
175 {
176 const char *tp_name = qdict_get_str(qdict, "name");
177 bool new_state = qdict_get_bool(qdict, "option");
178 bool has_vcpu = qdict_haskey(qdict, "vcpu");
179 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
180 Error *local_err = NULL;
181
182 if (vcpu < 0) {
183 monitor_printf(mon, "argument vcpu must be positive");
184 return;
185 }
186
187 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
188 if (local_err) {
189 error_report_err(local_err);
190 }
191 }
192
193 #ifdef CONFIG_TRACE_SIMPLE
194 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
195 {
196 const char *op = qdict_get_try_str(qdict, "op");
197 const char *arg = qdict_get_try_str(qdict, "arg");
198
199 if (!op) {
200 st_print_trace_file_status();
201 } else if (!strcmp(op, "on")) {
202 st_set_trace_file_enabled(true);
203 } else if (!strcmp(op, "off")) {
204 st_set_trace_file_enabled(false);
205 } else if (!strcmp(op, "flush")) {
206 st_flush_trace_buffer();
207 } else if (!strcmp(op, "set")) {
208 if (arg) {
209 st_set_trace_file(arg);
210 }
211 } else {
212 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
213 help_cmd(mon, "trace-file");
214 }
215 }
216 #endif
217
218 static void hmp_info_help(Monitor *mon, const QDict *qdict)
219 {
220 help_cmd(mon, "info");
221 }
222
223 static void monitor_init_qmp_commands(void)
224 {
225 /*
226 * Two command lists:
227 * - qmp_commands contains all QMP commands
228 * - qmp_cap_negotiation_commands contains just
229 * "qmp_capabilities", to enforce capability negotiation
230 */
231
232 qmp_init_marshal(&qmp_commands);
233
234 qmp_register_command(&qmp_commands, "device_add",
235 qmp_device_add, 0, 0);
236
237 QTAILQ_INIT(&qmp_cap_negotiation_commands);
238 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
239 qmp_marshal_qmp_capabilities,
240 QCO_ALLOW_PRECONFIG, 0);
241 }
242
243 /* Set the current CPU defined by the user. Callers must hold BQL. */
244 int monitor_set_cpu(Monitor *mon, int cpu_index)
245 {
246 CPUState *cpu;
247
248 cpu = qemu_get_cpu(cpu_index);
249 if (cpu == NULL) {
250 return -1;
251 }
252 g_free(mon->mon_cpu_path);
253 mon->mon_cpu_path = object_get_canonical_path(OBJECT(cpu));
254 return 0;
255 }
256
257 /* Callers must hold BQL. */
258 static CPUState *mon_get_cpu_sync(Monitor *mon, bool synchronize)
259 {
260 CPUState *cpu = NULL;
261
262 if (mon->mon_cpu_path) {
263 cpu = (CPUState *) object_resolve_path_type(mon->mon_cpu_path,
264 TYPE_CPU, NULL);
265 if (!cpu) {
266 g_free(mon->mon_cpu_path);
267 mon->mon_cpu_path = NULL;
268 }
269 }
270 if (!mon->mon_cpu_path) {
271 if (!first_cpu) {
272 return NULL;
273 }
274 monitor_set_cpu(mon, first_cpu->cpu_index);
275 cpu = first_cpu;
276 }
277 assert(cpu != NULL);
278 if (synchronize) {
279 cpu_synchronize_state(cpu);
280 }
281 return cpu;
282 }
283
284 CPUState *mon_get_cpu(Monitor *mon)
285 {
286 return mon_get_cpu_sync(mon, true);
287 }
288
289 CPUArchState *mon_get_cpu_env(Monitor *mon)
290 {
291 CPUState *cs = mon_get_cpu(mon);
292
293 return cs ? cs->env_ptr : NULL;
294 }
295
296 int monitor_get_cpu_index(Monitor *mon)
297 {
298 CPUState *cs = mon_get_cpu_sync(mon, false);
299
300 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
301 }
302
303 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
304 {
305 bool all_cpus = qdict_get_try_bool(qdict, "cpustate_all", false);
306 CPUState *cs;
307
308 if (all_cpus) {
309 CPU_FOREACH(cs) {
310 monitor_printf(mon, "\nCPU#%d\n", cs->cpu_index);
311 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
312 }
313 } else {
314 cs = mon_get_cpu(mon);
315
316 if (!cs) {
317 monitor_printf(mon, "No CPU available\n");
318 return;
319 }
320
321 cpu_dump_state(cs, NULL, CPU_DUMP_FPU);
322 }
323 }
324
325 static void hmp_info_sync_profile(Monitor *mon, const QDict *qdict)
326 {
327 int64_t max = qdict_get_try_int(qdict, "max", 10);
328 bool mean = qdict_get_try_bool(qdict, "mean", false);
329 bool coalesce = !qdict_get_try_bool(qdict, "no_coalesce", false);
330 enum QSPSortBy sort_by;
331
332 sort_by = mean ? QSP_SORT_BY_AVG_WAIT_TIME : QSP_SORT_BY_TOTAL_WAIT_TIME;
333 qsp_report(max, sort_by, coalesce);
334 }
335
336 static void hmp_info_history(Monitor *mon, const QDict *qdict)
337 {
338 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
339 int i;
340 const char *str;
341
342 if (!hmp_mon->rs) {
343 return;
344 }
345 i = 0;
346 for(;;) {
347 str = readline_get_history(hmp_mon->rs, i);
348 if (!str) {
349 break;
350 }
351 monitor_printf(mon, "%d: '%s'\n", i, str);
352 i++;
353 }
354 }
355
356 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
357 {
358 const char *name = qdict_get_try_str(qdict, "name");
359 bool has_vcpu = qdict_haskey(qdict, "vcpu");
360 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
361 TraceEventInfoList *events;
362 TraceEventInfoList *elem;
363 Error *local_err = NULL;
364
365 if (name == NULL) {
366 name = "*";
367 }
368 if (vcpu < 0) {
369 monitor_printf(mon, "argument vcpu must be positive");
370 return;
371 }
372
373 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
374 if (local_err) {
375 error_report_err(local_err);
376 return;
377 }
378
379 for (elem = events; elem != NULL; elem = elem->next) {
380 monitor_printf(mon, "%s : state %u\n",
381 elem->value->name,
382 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
383 }
384 qapi_free_TraceEventInfoList(events);
385 }
386
387 void qmp_client_migrate_info(const char *protocol, const char *hostname,
388 bool has_port, int64_t port,
389 bool has_tls_port, int64_t tls_port,
390 bool has_cert_subject, const char *cert_subject,
391 Error **errp)
392 {
393 if (strcmp(protocol, "spice") == 0) {
394 if (!qemu_using_spice(errp)) {
395 return;
396 }
397
398 if (!has_port && !has_tls_port) {
399 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
400 return;
401 }
402
403 if (qemu_spice.migrate_info(hostname,
404 has_port ? port : -1,
405 has_tls_port ? tls_port : -1,
406 cert_subject)) {
407 error_setg(errp, "Could not set up display for migration");
408 return;
409 }
410 return;
411 }
412
413 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "'spice'");
414 }
415
416 static void hmp_logfile(Monitor *mon, const QDict *qdict)
417 {
418 Error *err = NULL;
419
420 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
421 if (err) {
422 error_report_err(err);
423 }
424 }
425
426 static void hmp_log(Monitor *mon, const QDict *qdict)
427 {
428 int mask;
429 const char *items = qdict_get_str(qdict, "items");
430
431 if (!strcmp(items, "none")) {
432 mask = 0;
433 } else {
434 mask = qemu_str_to_log_mask(items);
435 if (!mask) {
436 help_cmd(mon, "log");
437 return;
438 }
439 }
440 qemu_set_log(mask);
441 }
442
443 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
444 {
445 const char *option = qdict_get_try_str(qdict, "option");
446 if (!option || !strcmp(option, "on")) {
447 singlestep = 1;
448 } else if (!strcmp(option, "off")) {
449 singlestep = 0;
450 } else {
451 monitor_printf(mon, "unexpected option %s\n", option);
452 }
453 }
454
455 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
456 {
457 const char *device = qdict_get_try_str(qdict, "device");
458 if (!device) {
459 device = "tcp::" DEFAULT_GDBSTUB_PORT;
460 }
461
462 if (gdbserver_start(device) < 0) {
463 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
464 device);
465 } else if (strcmp(device, "none") == 0) {
466 monitor_printf(mon, "Disabled gdbserver\n");
467 } else {
468 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
469 device);
470 }
471 }
472
473 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
474 {
475 const char *action = qdict_get_str(qdict, "action");
476 if (select_watchdog_action(action) == -1) {
477 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
478 }
479 }
480
481 static void monitor_printc(Monitor *mon, int c)
482 {
483 monitor_printf(mon, "'");
484 switch(c) {
485 case '\'':
486 monitor_printf(mon, "\\'");
487 break;
488 case '\\':
489 monitor_printf(mon, "\\\\");
490 break;
491 case '\n':
492 monitor_printf(mon, "\\n");
493 break;
494 case '\r':
495 monitor_printf(mon, "\\r");
496 break;
497 default:
498 if (c >= 32 && c <= 126) {
499 monitor_printf(mon, "%c", c);
500 } else {
501 monitor_printf(mon, "\\x%02x", c);
502 }
503 break;
504 }
505 monitor_printf(mon, "'");
506 }
507
508 static void memory_dump(Monitor *mon, int count, int format, int wsize,
509 hwaddr addr, int is_physical)
510 {
511 int l, line_size, i, max_digits, len;
512 uint8_t buf[16];
513 uint64_t v;
514 CPUState *cs = mon_get_cpu(mon);
515
516 if (!cs && (format == 'i' || !is_physical)) {
517 monitor_printf(mon, "Can not dump without CPU\n");
518 return;
519 }
520
521 if (format == 'i') {
522 monitor_disas(mon, cs, addr, count, is_physical);
523 return;
524 }
525
526 len = wsize * count;
527 if (wsize == 1) {
528 line_size = 8;
529 } else {
530 line_size = 16;
531 }
532 max_digits = 0;
533
534 switch(format) {
535 case 'o':
536 max_digits = DIV_ROUND_UP(wsize * 8, 3);
537 break;
538 default:
539 case 'x':
540 max_digits = (wsize * 8) / 4;
541 break;
542 case 'u':
543 case 'd':
544 max_digits = DIV_ROUND_UP(wsize * 8 * 10, 33);
545 break;
546 case 'c':
547 wsize = 1;
548 break;
549 }
550
551 while (len > 0) {
552 if (is_physical) {
553 monitor_printf(mon, TARGET_FMT_plx ":", addr);
554 } else {
555 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
556 }
557 l = len;
558 if (l > line_size)
559 l = line_size;
560 if (is_physical) {
561 AddressSpace *as = cs ? cs->as : &address_space_memory;
562 MemTxResult r = address_space_read(as, addr,
563 MEMTXATTRS_UNSPECIFIED, buf, l);
564 if (r != MEMTX_OK) {
565 monitor_printf(mon, " Cannot access memory\n");
566 break;
567 }
568 } else {
569 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
570 monitor_printf(mon, " Cannot access memory\n");
571 break;
572 }
573 }
574 i = 0;
575 while (i < l) {
576 switch(wsize) {
577 default:
578 case 1:
579 v = ldub_p(buf + i);
580 break;
581 case 2:
582 v = lduw_p(buf + i);
583 break;
584 case 4:
585 v = (uint32_t)ldl_p(buf + i);
586 break;
587 case 8:
588 v = ldq_p(buf + i);
589 break;
590 }
591 monitor_printf(mon, " ");
592 switch(format) {
593 case 'o':
594 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
595 break;
596 case 'x':
597 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
598 break;
599 case 'u':
600 monitor_printf(mon, "%*" PRIu64, max_digits, v);
601 break;
602 case 'd':
603 monitor_printf(mon, "%*" PRId64, max_digits, v);
604 break;
605 case 'c':
606 monitor_printc(mon, v);
607 break;
608 }
609 i += wsize;
610 }
611 monitor_printf(mon, "\n");
612 addr += l;
613 len -= l;
614 }
615 }
616
617 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
618 {
619 int count = qdict_get_int(qdict, "count");
620 int format = qdict_get_int(qdict, "format");
621 int size = qdict_get_int(qdict, "size");
622 target_long addr = qdict_get_int(qdict, "addr");
623
624 memory_dump(mon, count, format, size, addr, 0);
625 }
626
627 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
628 {
629 int count = qdict_get_int(qdict, "count");
630 int format = qdict_get_int(qdict, "format");
631 int size = qdict_get_int(qdict, "size");
632 hwaddr addr = qdict_get_int(qdict, "addr");
633
634 memory_dump(mon, count, format, size, addr, 1);
635 }
636
637 void *gpa2hva(MemoryRegion **p_mr, hwaddr addr, uint64_t size, Error **errp)
638 {
639 Int128 gpa_region_size;
640 MemoryRegionSection mrs = memory_region_find(get_system_memory(),
641 addr, size);
642
643 if (!mrs.mr) {
644 error_setg(errp, "No memory is mapped at address 0x%" HWADDR_PRIx, addr);
645 return NULL;
646 }
647
648 if (!memory_region_is_ram(mrs.mr) && !memory_region_is_romd(mrs.mr)) {
649 error_setg(errp, "Memory at address 0x%" HWADDR_PRIx "is not RAM", addr);
650 memory_region_unref(mrs.mr);
651 return NULL;
652 }
653
654 gpa_region_size = int128_make64(size);
655 if (int128_lt(mrs.size, gpa_region_size)) {
656 error_setg(errp, "Size of memory region at 0x%" HWADDR_PRIx
657 " exceeded.", addr);
658 memory_region_unref(mrs.mr);
659 return NULL;
660 }
661
662 *p_mr = mrs.mr;
663 return qemu_map_ram_ptr(mrs.mr->ram_block, mrs.offset_within_region);
664 }
665
666 static void hmp_gpa2hva(Monitor *mon, const QDict *qdict)
667 {
668 hwaddr addr = qdict_get_int(qdict, "addr");
669 Error *local_err = NULL;
670 MemoryRegion *mr = NULL;
671 void *ptr;
672
673 ptr = gpa2hva(&mr, addr, 1, &local_err);
674 if (local_err) {
675 error_report_err(local_err);
676 return;
677 }
678
679 monitor_printf(mon, "Host virtual address for 0x%" HWADDR_PRIx
680 " (%s) is %p\n",
681 addr, mr->name, ptr);
682
683 memory_region_unref(mr);
684 }
685
686 static void hmp_gva2gpa(Monitor *mon, const QDict *qdict)
687 {
688 target_ulong addr = qdict_get_int(qdict, "addr");
689 MemTxAttrs attrs;
690 CPUState *cs = mon_get_cpu(mon);
691 hwaddr gpa;
692
693 if (!cs) {
694 monitor_printf(mon, "No cpu\n");
695 return;
696 }
697
698 gpa = cpu_get_phys_page_attrs_debug(cs, addr & TARGET_PAGE_MASK, &attrs);
699 if (gpa == -1) {
700 monitor_printf(mon, "Unmapped\n");
701 } else {
702 monitor_printf(mon, "gpa: %#" HWADDR_PRIx "\n",
703 gpa + (addr & ~TARGET_PAGE_MASK));
704 }
705 }
706
707 #ifdef CONFIG_LINUX
708 static uint64_t vtop(void *ptr, Error **errp)
709 {
710 uint64_t pinfo;
711 uint64_t ret = -1;
712 uintptr_t addr = (uintptr_t) ptr;
713 uintptr_t pagesize = qemu_real_host_page_size;
714 off_t offset = addr / pagesize * sizeof(pinfo);
715 int fd;
716
717 fd = open("/proc/self/pagemap", O_RDONLY);
718 if (fd == -1) {
719 error_setg_errno(errp, errno, "Cannot open /proc/self/pagemap");
720 return -1;
721 }
722
723 /* Force copy-on-write if necessary. */
724 qatomic_add((uint8_t *)ptr, 0);
725
726 if (pread(fd, &pinfo, sizeof(pinfo), offset) != sizeof(pinfo)) {
727 error_setg_errno(errp, errno, "Cannot read pagemap");
728 goto out;
729 }
730 if ((pinfo & (1ull << 63)) == 0) {
731 error_setg(errp, "Page not present");
732 goto out;
733 }
734 ret = ((pinfo & 0x007fffffffffffffull) * pagesize) | (addr & (pagesize - 1));
735
736 out:
737 close(fd);
738 return ret;
739 }
740
741 static void hmp_gpa2hpa(Monitor *mon, const QDict *qdict)
742 {
743 hwaddr addr = qdict_get_int(qdict, "addr");
744 Error *local_err = NULL;
745 MemoryRegion *mr = NULL;
746 void *ptr;
747 uint64_t physaddr;
748
749 ptr = gpa2hva(&mr, addr, 1, &local_err);
750 if (local_err) {
751 error_report_err(local_err);
752 return;
753 }
754
755 physaddr = vtop(ptr, &local_err);
756 if (local_err) {
757 error_report_err(local_err);
758 } else {
759 monitor_printf(mon, "Host physical address for 0x%" HWADDR_PRIx
760 " (%s) is 0x%" PRIx64 "\n",
761 addr, mr->name, (uint64_t) physaddr);
762 }
763
764 memory_region_unref(mr);
765 }
766 #endif
767
768 static void do_print(Monitor *mon, const QDict *qdict)
769 {
770 int format = qdict_get_int(qdict, "format");
771 hwaddr val = qdict_get_int(qdict, "val");
772
773 switch(format) {
774 case 'o':
775 monitor_printf(mon, "%#" HWADDR_PRIo, val);
776 break;
777 case 'x':
778 monitor_printf(mon, "%#" HWADDR_PRIx, val);
779 break;
780 case 'u':
781 monitor_printf(mon, "%" HWADDR_PRIu, val);
782 break;
783 default:
784 case 'd':
785 monitor_printf(mon, "%" HWADDR_PRId, val);
786 break;
787 case 'c':
788 monitor_printc(mon, val);
789 break;
790 }
791 monitor_printf(mon, "\n");
792 }
793
794 static void hmp_sum(Monitor *mon, const QDict *qdict)
795 {
796 uint32_t addr;
797 uint16_t sum;
798 uint32_t start = qdict_get_int(qdict, "start");
799 uint32_t size = qdict_get_int(qdict, "size");
800
801 sum = 0;
802 for(addr = start; addr < (start + size); addr++) {
803 uint8_t val = address_space_ldub(&address_space_memory, addr,
804 MEMTXATTRS_UNSPECIFIED, NULL);
805 /* BSD sum algorithm ('sum' Unix command) */
806 sum = (sum >> 1) | (sum << 15);
807 sum += val;
808 }
809 monitor_printf(mon, "%05d\n", sum);
810 }
811
812 static int mouse_button_state;
813
814 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
815 {
816 int dx, dy, dz, button;
817 const char *dx_str = qdict_get_str(qdict, "dx_str");
818 const char *dy_str = qdict_get_str(qdict, "dy_str");
819 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
820
821 dx = strtol(dx_str, NULL, 0);
822 dy = strtol(dy_str, NULL, 0);
823 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
824 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
825
826 if (dz_str) {
827 dz = strtol(dz_str, NULL, 0);
828 if (dz != 0) {
829 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
830 qemu_input_queue_btn(NULL, button, true);
831 qemu_input_event_sync();
832 qemu_input_queue_btn(NULL, button, false);
833 }
834 }
835 qemu_input_event_sync();
836 }
837
838 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
839 {
840 static uint32_t bmap[INPUT_BUTTON__MAX] = {
841 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
842 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
843 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
844 };
845 int button_state = qdict_get_int(qdict, "button_state");
846
847 if (mouse_button_state == button_state) {
848 return;
849 }
850 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
851 qemu_input_event_sync();
852 mouse_button_state = button_state;
853 }
854
855 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
856 {
857 int size = qdict_get_int(qdict, "size");
858 int addr = qdict_get_int(qdict, "addr");
859 int has_index = qdict_haskey(qdict, "index");
860 uint32_t val;
861 int suffix;
862
863 if (has_index) {
864 int index = qdict_get_int(qdict, "index");
865 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
866 addr++;
867 }
868 addr &= 0xffff;
869
870 switch(size) {
871 default:
872 case 1:
873 val = cpu_inb(addr);
874 suffix = 'b';
875 break;
876 case 2:
877 val = cpu_inw(addr);
878 suffix = 'w';
879 break;
880 case 4:
881 val = cpu_inl(addr);
882 suffix = 'l';
883 break;
884 }
885 monitor_printf(mon, "port%c[0x%04x] = 0x%0*x\n",
886 suffix, addr, size * 2, val);
887 }
888
889 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
890 {
891 int size = qdict_get_int(qdict, "size");
892 int addr = qdict_get_int(qdict, "addr");
893 int val = qdict_get_int(qdict, "val");
894
895 addr &= IOPORTS_MASK;
896
897 switch (size) {
898 default:
899 case 1:
900 cpu_outb(addr, val);
901 break;
902 case 2:
903 cpu_outw(addr, val);
904 break;
905 case 4:
906 cpu_outl(addr, val);
907 break;
908 }
909 }
910
911 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
912 {
913 Error *local_err = NULL;
914 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
915
916 qemu_boot_set(bootdevice, &local_err);
917 if (local_err) {
918 error_report_err(local_err);
919 } else {
920 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
921 }
922 }
923
924 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
925 {
926 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
927 bool dispatch_tree = qdict_get_try_bool(qdict, "dispatch_tree", false);
928 bool owner = qdict_get_try_bool(qdict, "owner", false);
929 bool disabled = qdict_get_try_bool(qdict, "disabled", false);
930
931 mtree_info(flatview, dispatch_tree, owner, disabled);
932 }
933
934 #ifdef CONFIG_PROFILER
935
936 int64_t dev_time;
937
938 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
939 {
940 static int64_t last_cpu_exec_time;
941 int64_t cpu_exec_time;
942 int64_t delta;
943
944 cpu_exec_time = tcg_cpu_exec_time();
945 delta = cpu_exec_time - last_cpu_exec_time;
946
947 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
948 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
949 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
950 delta, delta / (double)NANOSECONDS_PER_SECOND);
951 last_cpu_exec_time = cpu_exec_time;
952 dev_time = 0;
953 }
954 #else
955 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
956 {
957 monitor_printf(mon, "Internal profiler not compiled\n");
958 }
959 #endif
960
961 /* Capture support */
962 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
963
964 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
965 {
966 int i;
967 CaptureState *s;
968
969 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
970 monitor_printf(mon, "[%d]: ", i);
971 s->ops.info (s->opaque);
972 }
973 }
974
975 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
976 {
977 int i;
978 int n = qdict_get_int(qdict, "n");
979 CaptureState *s;
980
981 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
982 if (i == n) {
983 s->ops.destroy (s->opaque);
984 QLIST_REMOVE (s, entries);
985 g_free (s);
986 return;
987 }
988 }
989 }
990
991 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
992 {
993 const char *path = qdict_get_str(qdict, "path");
994 int freq = qdict_get_try_int(qdict, "freq", 44100);
995 int bits = qdict_get_try_int(qdict, "bits", 16);
996 int nchannels = qdict_get_try_int(qdict, "nchannels", 2);
997 const char *audiodev = qdict_get_str(qdict, "audiodev");
998 CaptureState *s;
999 AudioState *as = audio_state_by_name(audiodev);
1000
1001 if (!as) {
1002 monitor_printf(mon, "Audiodev '%s' not found\n", audiodev);
1003 return;
1004 }
1005
1006 s = g_malloc0 (sizeof (*s));
1007
1008 if (wav_start_capture(as, s, path, freq, bits, nchannels)) {
1009 monitor_printf(mon, "Failed to add wave capture\n");
1010 g_free (s);
1011 return;
1012 }
1013 QLIST_INSERT_HEAD (&capture_head, s, entries);
1014 }
1015
1016 void qmp_getfd(const char *fdname, Error **errp)
1017 {
1018 Monitor *cur_mon = monitor_cur();
1019 mon_fd_t *monfd;
1020 int fd, tmp_fd;
1021
1022 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1023 if (fd == -1) {
1024 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1025 return;
1026 }
1027
1028 if (qemu_isdigit(fdname[0])) {
1029 close(fd);
1030 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1031 "a name not starting with a digit");
1032 return;
1033 }
1034
1035 QEMU_LOCK_GUARD(&cur_mon->mon_lock);
1036 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1037 if (strcmp(monfd->name, fdname) != 0) {
1038 continue;
1039 }
1040
1041 tmp_fd = monfd->fd;
1042 monfd->fd = fd;
1043 /* Make sure close() is outside critical section */
1044 close(tmp_fd);
1045 return;
1046 }
1047
1048 monfd = g_malloc0(sizeof(mon_fd_t));
1049 monfd->name = g_strdup(fdname);
1050 monfd->fd = fd;
1051
1052 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1053 }
1054
1055 void qmp_closefd(const char *fdname, Error **errp)
1056 {
1057 Monitor *cur_mon = monitor_cur();
1058 mon_fd_t *monfd;
1059 int tmp_fd;
1060
1061 qemu_mutex_lock(&cur_mon->mon_lock);
1062 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1063 if (strcmp(monfd->name, fdname) != 0) {
1064 continue;
1065 }
1066
1067 QLIST_REMOVE(monfd, next);
1068 tmp_fd = monfd->fd;
1069 g_free(monfd->name);
1070 g_free(monfd);
1071 qemu_mutex_unlock(&cur_mon->mon_lock);
1072 /* Make sure close() is outside critical section */
1073 close(tmp_fd);
1074 return;
1075 }
1076
1077 qemu_mutex_unlock(&cur_mon->mon_lock);
1078 error_setg(errp, "File descriptor named '%s' not found", fdname);
1079 }
1080
1081 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1082 {
1083 mon_fd_t *monfd;
1084
1085 QEMU_LOCK_GUARD(&mon->mon_lock);
1086 QLIST_FOREACH(monfd, &mon->fds, next) {
1087 int fd;
1088
1089 if (strcmp(monfd->name, fdname) != 0) {
1090 continue;
1091 }
1092
1093 fd = monfd->fd;
1094
1095 /* caller takes ownership of fd */
1096 QLIST_REMOVE(monfd, next);
1097 g_free(monfd->name);
1098 g_free(monfd);
1099
1100 return fd;
1101 }
1102
1103 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1104 return -1;
1105 }
1106
1107 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1108 {
1109 MonFdsetFd *mon_fdset_fd;
1110 MonFdsetFd *mon_fdset_fd_next;
1111
1112 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1113 if ((mon_fdset_fd->removed ||
1114 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1115 runstate_is_running()) {
1116 close(mon_fdset_fd->fd);
1117 g_free(mon_fdset_fd->opaque);
1118 QLIST_REMOVE(mon_fdset_fd, next);
1119 g_free(mon_fdset_fd);
1120 }
1121 }
1122
1123 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1124 QLIST_REMOVE(mon_fdset, next);
1125 g_free(mon_fdset);
1126 }
1127 }
1128
1129 void monitor_fdsets_cleanup(void)
1130 {
1131 MonFdset *mon_fdset;
1132 MonFdset *mon_fdset_next;
1133
1134 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1135 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1136 monitor_fdset_cleanup(mon_fdset);
1137 }
1138 }
1139
1140 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1141 const char *opaque, Error **errp)
1142 {
1143 int fd;
1144 Monitor *mon = monitor_cur();
1145 AddfdInfo *fdinfo;
1146
1147 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1148 if (fd == -1) {
1149 error_setg(errp, "No file descriptor supplied via SCM_RIGHTS");
1150 goto error;
1151 }
1152
1153 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1154 has_opaque, opaque, errp);
1155 if (fdinfo) {
1156 return fdinfo;
1157 }
1158
1159 error:
1160 if (fd != -1) {
1161 close(fd);
1162 }
1163 return NULL;
1164 }
1165
1166 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1167 {
1168 MonFdset *mon_fdset;
1169 MonFdsetFd *mon_fdset_fd;
1170 char fd_str[60];
1171
1172 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1173 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1174 if (mon_fdset->id != fdset_id) {
1175 continue;
1176 }
1177 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1178 if (has_fd) {
1179 if (mon_fdset_fd->fd != fd) {
1180 continue;
1181 }
1182 mon_fdset_fd->removed = true;
1183 break;
1184 } else {
1185 mon_fdset_fd->removed = true;
1186 }
1187 }
1188 if (has_fd && !mon_fdset_fd) {
1189 goto error;
1190 }
1191 monitor_fdset_cleanup(mon_fdset);
1192 return;
1193 }
1194
1195 error:
1196 if (has_fd) {
1197 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1198 fdset_id, fd);
1199 } else {
1200 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1201 }
1202 error_setg(errp, "File descriptor named '%s' not found", fd_str);
1203 }
1204
1205 FdsetInfoList *qmp_query_fdsets(Error **errp)
1206 {
1207 MonFdset *mon_fdset;
1208 MonFdsetFd *mon_fdset_fd;
1209 FdsetInfoList *fdset_list = NULL;
1210
1211 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1212 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1213 FdsetInfo *fdset_info = g_malloc0(sizeof(*fdset_info));
1214
1215 fdset_info->fdset_id = mon_fdset->id;
1216
1217 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1218 FdsetFdInfo *fdsetfd_info;
1219
1220 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1221 fdsetfd_info->fd = mon_fdset_fd->fd;
1222 if (mon_fdset_fd->opaque) {
1223 fdsetfd_info->has_opaque = true;
1224 fdsetfd_info->opaque = g_strdup(mon_fdset_fd->opaque);
1225 } else {
1226 fdsetfd_info->has_opaque = false;
1227 }
1228
1229 QAPI_LIST_PREPEND(fdset_info->fds, fdsetfd_info);
1230 }
1231
1232 QAPI_LIST_PREPEND(fdset_list, fdset_info);
1233 }
1234
1235 return fdset_list;
1236 }
1237
1238 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1239 bool has_opaque, const char *opaque,
1240 Error **errp)
1241 {
1242 MonFdset *mon_fdset = NULL;
1243 MonFdsetFd *mon_fdset_fd;
1244 AddfdInfo *fdinfo;
1245
1246 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1247 if (has_fdset_id) {
1248 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1249 /* Break if match found or match impossible due to ordering by ID */
1250 if (fdset_id <= mon_fdset->id) {
1251 if (fdset_id < mon_fdset->id) {
1252 mon_fdset = NULL;
1253 }
1254 break;
1255 }
1256 }
1257 }
1258
1259 if (mon_fdset == NULL) {
1260 int64_t fdset_id_prev = -1;
1261 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1262
1263 if (has_fdset_id) {
1264 if (fdset_id < 0) {
1265 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1266 "a non-negative value");
1267 return NULL;
1268 }
1269 /* Use specified fdset ID */
1270 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1271 mon_fdset_cur = mon_fdset;
1272 if (fdset_id < mon_fdset_cur->id) {
1273 break;
1274 }
1275 }
1276 } else {
1277 /* Use first available fdset ID */
1278 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1279 mon_fdset_cur = mon_fdset;
1280 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1281 fdset_id_prev = mon_fdset_cur->id;
1282 continue;
1283 }
1284 break;
1285 }
1286 }
1287
1288 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1289 if (has_fdset_id) {
1290 mon_fdset->id = fdset_id;
1291 } else {
1292 mon_fdset->id = fdset_id_prev + 1;
1293 }
1294
1295 /* The fdset list is ordered by fdset ID */
1296 if (!mon_fdset_cur) {
1297 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1298 } else if (mon_fdset->id < mon_fdset_cur->id) {
1299 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1300 } else {
1301 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1302 }
1303 }
1304
1305 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1306 mon_fdset_fd->fd = fd;
1307 mon_fdset_fd->removed = false;
1308 if (has_opaque) {
1309 mon_fdset_fd->opaque = g_strdup(opaque);
1310 }
1311 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1312
1313 fdinfo = g_malloc0(sizeof(*fdinfo));
1314 fdinfo->fdset_id = mon_fdset->id;
1315 fdinfo->fd = mon_fdset_fd->fd;
1316
1317 return fdinfo;
1318 }
1319
1320 int monitor_fdset_dup_fd_add(int64_t fdset_id, int flags)
1321 {
1322 #ifdef _WIN32
1323 return -ENOENT;
1324 #else
1325 MonFdset *mon_fdset;
1326
1327 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1328 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1329 MonFdsetFd *mon_fdset_fd;
1330 MonFdsetFd *mon_fdset_fd_dup;
1331 int fd = -1;
1332 int dup_fd;
1333 int mon_fd_flags;
1334
1335 if (mon_fdset->id != fdset_id) {
1336 continue;
1337 }
1338
1339 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1340 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1341 if (mon_fd_flags == -1) {
1342 return -1;
1343 }
1344
1345 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1346 fd = mon_fdset_fd->fd;
1347 break;
1348 }
1349 }
1350
1351 if (fd == -1) {
1352 errno = EACCES;
1353 return -1;
1354 }
1355
1356 dup_fd = qemu_dup_flags(fd, flags);
1357 if (dup_fd == -1) {
1358 return -1;
1359 }
1360
1361 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1362 mon_fdset_fd_dup->fd = dup_fd;
1363 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1364 return dup_fd;
1365 }
1366
1367 errno = ENOENT;
1368 return -1;
1369 #endif
1370 }
1371
1372 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
1373 {
1374 MonFdset *mon_fdset;
1375 MonFdsetFd *mon_fdset_fd_dup;
1376
1377 QEMU_LOCK_GUARD(&mon_fdsets_lock);
1378 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1379 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1380 if (mon_fdset_fd_dup->fd == dup_fd) {
1381 if (remove) {
1382 QLIST_REMOVE(mon_fdset_fd_dup, next);
1383 g_free(mon_fdset_fd_dup);
1384 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
1385 monitor_fdset_cleanup(mon_fdset);
1386 }
1387 return -1;
1388 } else {
1389 return mon_fdset->id;
1390 }
1391 }
1392 }
1393 }
1394
1395 return -1;
1396 }
1397
1398 int64_t monitor_fdset_dup_fd_find(int dup_fd)
1399 {
1400 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
1401 }
1402
1403 void monitor_fdset_dup_fd_remove(int dup_fd)
1404 {
1405 monitor_fdset_dup_fd_find_remove(dup_fd, true);
1406 }
1407
1408 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
1409 {
1410 int fd;
1411 Error *local_err = NULL;
1412
1413 if (!qemu_isdigit(fdname[0]) && mon) {
1414 fd = monitor_get_fd(mon, fdname, &local_err);
1415 } else {
1416 fd = qemu_parse_fd(fdname);
1417 if (fd == -1) {
1418 error_setg(&local_err, "Invalid file descriptor number '%s'",
1419 fdname);
1420 }
1421 }
1422 if (local_err) {
1423 error_propagate(errp, local_err);
1424 assert(fd == -1);
1425 } else {
1426 assert(fd != -1);
1427 }
1428
1429 return fd;
1430 }
1431
1432 /* Please update hmp-commands.hx when adding or changing commands */
1433 static HMPCommand hmp_info_cmds[] = {
1434 #include "hmp-commands-info.h"
1435 { NULL, NULL, },
1436 };
1437
1438 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
1439 HMPCommand hmp_cmds[] = {
1440 #include "hmp-commands.h"
1441 { NULL, NULL, },
1442 };
1443
1444 /*
1445 * Set @pval to the value in the register identified by @name.
1446 * return 0 if OK, -1 if not found
1447 */
1448 int get_monitor_def(Monitor *mon, int64_t *pval, const char *name)
1449 {
1450 const MonitorDef *md = target_monitor_defs();
1451 CPUState *cs = mon_get_cpu(mon);
1452 void *ptr;
1453 uint64_t tmp = 0;
1454 int ret;
1455
1456 if (cs == NULL || md == NULL) {
1457 return -1;
1458 }
1459
1460 for(; md->name != NULL; md++) {
1461 if (hmp_compare_cmd(name, md->name)) {
1462 if (md->get_value) {
1463 *pval = md->get_value(mon, md, md->offset);
1464 } else {
1465 CPUArchState *env = mon_get_cpu_env(mon);
1466 ptr = (uint8_t *)env + md->offset;
1467 switch(md->type) {
1468 case MD_I32:
1469 *pval = *(int32_t *)ptr;
1470 break;
1471 case MD_TLONG:
1472 *pval = *(target_long *)ptr;
1473 break;
1474 default:
1475 *pval = 0;
1476 break;
1477 }
1478 }
1479 return 0;
1480 }
1481 }
1482
1483 ret = target_get_monitor_def(cs, name, &tmp);
1484 if (!ret) {
1485 *pval = (target_long) tmp;
1486 }
1487
1488 return ret;
1489 }
1490
1491 static void add_completion_option(ReadLineState *rs, const char *str,
1492 const char *option)
1493 {
1494 if (!str || !option) {
1495 return;
1496 }
1497 if (!strncmp(option, str, strlen(str))) {
1498 readline_add_completion(rs, option);
1499 }
1500 }
1501
1502 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1503 {
1504 size_t len;
1505 ChardevBackendInfoList *list, *start;
1506
1507 if (nb_args != 2) {
1508 return;
1509 }
1510 len = strlen(str);
1511 readline_set_completion_index(rs, len);
1512
1513 start = list = qmp_query_chardev_backends(NULL);
1514 while (list) {
1515 const char *chr_name = list->value->name;
1516
1517 if (!strncmp(chr_name, str, len)) {
1518 readline_add_completion(rs, chr_name);
1519 }
1520 list = list->next;
1521 }
1522 qapi_free_ChardevBackendInfoList(start);
1523 }
1524
1525 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
1526 {
1527 size_t len;
1528 int i;
1529
1530 if (nb_args != 2) {
1531 return;
1532 }
1533 len = strlen(str);
1534 readline_set_completion_index(rs, len);
1535 for (i = 0; i < NET_CLIENT_DRIVER__MAX; i++) {
1536 add_completion_option(rs, str, NetClientDriver_str(i));
1537 }
1538 }
1539
1540 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
1541 {
1542 GSList *list, *elt;
1543 size_t len;
1544
1545 if (nb_args != 2) {
1546 return;
1547 }
1548
1549 len = strlen(str);
1550 readline_set_completion_index(rs, len);
1551 list = elt = object_class_get_list(TYPE_DEVICE, false);
1552 while (elt) {
1553 const char *name;
1554 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
1555 TYPE_DEVICE);
1556 name = object_class_get_name(OBJECT_CLASS(dc));
1557
1558 if (dc->user_creatable
1559 && !strncmp(name, str, len)) {
1560 readline_add_completion(rs, name);
1561 }
1562 elt = elt->next;
1563 }
1564 g_slist_free(list);
1565 }
1566
1567 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
1568 {
1569 GSList *list, *elt;
1570 size_t len;
1571
1572 if (nb_args != 2) {
1573 return;
1574 }
1575
1576 len = strlen(str);
1577 readline_set_completion_index(rs, len);
1578 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
1579 while (elt) {
1580 const char *name;
1581
1582 name = object_class_get_name(OBJECT_CLASS(elt->data));
1583 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
1584 readline_add_completion(rs, name);
1585 }
1586 elt = elt->next;
1587 }
1588 g_slist_free(list);
1589 }
1590
1591 static int qdev_add_hotpluggable_device(Object *obj, void *opaque)
1592 {
1593 GSList **list = opaque;
1594 DeviceState *dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
1595
1596 if (dev == NULL) {
1597 return 0;
1598 }
1599
1600 if (dev->realized && object_property_get_bool(obj, "hotpluggable", NULL)) {
1601 *list = g_slist_append(*list, dev);
1602 }
1603
1604 return 0;
1605 }
1606
1607 static GSList *qdev_build_hotpluggable_device_list(Object *peripheral)
1608 {
1609 GSList *list = NULL;
1610
1611 object_child_foreach(peripheral, qdev_add_hotpluggable_device, &list);
1612
1613 return list;
1614 }
1615
1616 static void peripheral_device_del_completion(ReadLineState *rs,
1617 const char *str, size_t len)
1618 {
1619 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
1620 GSList *list, *item;
1621
1622 list = qdev_build_hotpluggable_device_list(peripheral);
1623 if (!list) {
1624 return;
1625 }
1626
1627 for (item = list; item; item = g_slist_next(item)) {
1628 DeviceState *dev = item->data;
1629
1630 if (dev->id && !strncmp(str, dev->id, len)) {
1631 readline_add_completion(rs, dev->id);
1632 }
1633 }
1634
1635 g_slist_free(list);
1636 }
1637
1638 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
1639 {
1640 size_t len;
1641 ChardevInfoList *list, *start;
1642
1643 if (nb_args != 2) {
1644 return;
1645 }
1646 len = strlen(str);
1647 readline_set_completion_index(rs, len);
1648
1649 start = list = qmp_query_chardev(NULL);
1650 while (list) {
1651 ChardevInfo *chr = list->value;
1652
1653 if (!strncmp(chr->label, str, len)) {
1654 readline_add_completion(rs, chr->label);
1655 }
1656 list = list->next;
1657 }
1658 qapi_free_ChardevInfoList(start);
1659 }
1660
1661 static void ringbuf_completion(ReadLineState *rs, const char *str)
1662 {
1663 size_t len;
1664 ChardevInfoList *list, *start;
1665
1666 len = strlen(str);
1667 readline_set_completion_index(rs, len);
1668
1669 start = list = qmp_query_chardev(NULL);
1670 while (list) {
1671 ChardevInfo *chr_info = list->value;
1672
1673 if (!strncmp(chr_info->label, str, len)) {
1674 Chardev *chr = qemu_chr_find(chr_info->label);
1675 if (chr && CHARDEV_IS_RINGBUF(chr)) {
1676 readline_add_completion(rs, chr_info->label);
1677 }
1678 }
1679 list = list->next;
1680 }
1681 qapi_free_ChardevInfoList(start);
1682 }
1683
1684 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
1685 {
1686 if (nb_args != 2) {
1687 return;
1688 }
1689 ringbuf_completion(rs, str);
1690 }
1691
1692 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
1693 {
1694 size_t len;
1695
1696 if (nb_args != 2) {
1697 return;
1698 }
1699
1700 len = strlen(str);
1701 readline_set_completion_index(rs, len);
1702 peripheral_device_del_completion(rs, str, len);
1703 }
1704
1705 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
1706 {
1707 ObjectPropertyInfoList *list, *start;
1708 size_t len;
1709
1710 if (nb_args != 2) {
1711 return;
1712 }
1713 len = strlen(str);
1714 readline_set_completion_index(rs, len);
1715
1716 start = list = qmp_qom_list("/objects", NULL);
1717 while (list) {
1718 ObjectPropertyInfo *info = list->value;
1719
1720 if (!strncmp(info->type, "child<", 5)
1721 && !strncmp(info->name, str, len)) {
1722 readline_add_completion(rs, info->name);
1723 }
1724 list = list->next;
1725 }
1726 qapi_free_ObjectPropertyInfoList(start);
1727 }
1728
1729 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
1730 {
1731 int i;
1732 char *sep;
1733 size_t len;
1734
1735 if (nb_args != 2) {
1736 return;
1737 }
1738 sep = strrchr(str, '-');
1739 if (sep) {
1740 str = sep + 1;
1741 }
1742 len = strlen(str);
1743 readline_set_completion_index(rs, len);
1744 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
1745 if (!strncmp(str, QKeyCode_str(i), len)) {
1746 readline_add_completion(rs, QKeyCode_str(i));
1747 }
1748 }
1749 }
1750
1751 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
1752 {
1753 size_t len;
1754
1755 len = strlen(str);
1756 readline_set_completion_index(rs, len);
1757 if (nb_args == 2) {
1758 NetClientState *ncs[MAX_QUEUE_NUM];
1759 int count, i;
1760 count = qemu_find_net_clients_except(NULL, ncs,
1761 NET_CLIENT_DRIVER_NONE,
1762 MAX_QUEUE_NUM);
1763 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1764 const char *name = ncs[i]->name;
1765 if (!strncmp(str, name, len)) {
1766 readline_add_completion(rs, name);
1767 }
1768 }
1769 } else if (nb_args == 3) {
1770 add_completion_option(rs, str, "on");
1771 add_completion_option(rs, str, "off");
1772 }
1773 }
1774
1775 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
1776 {
1777 int len, count, i;
1778 NetClientState *ncs[MAX_QUEUE_NUM];
1779
1780 if (nb_args != 2) {
1781 return;
1782 }
1783
1784 len = strlen(str);
1785 readline_set_completion_index(rs, len);
1786 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
1787 MAX_QUEUE_NUM);
1788 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
1789 const char *name = ncs[i]->name;
1790 if (strncmp(str, name, len)) {
1791 continue;
1792 }
1793 if (ncs[i]->is_netdev) {
1794 readline_add_completion(rs, name);
1795 }
1796 }
1797 }
1798
1799 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
1800 {
1801 size_t len;
1802
1803 len = strlen(str);
1804 readline_set_completion_index(rs, len);
1805 if (nb_args == 2) {
1806 TraceEventIter iter;
1807 TraceEvent *ev;
1808 char *pattern = g_strdup_printf("%s*", str);
1809 trace_event_iter_init_pattern(&iter, pattern);
1810 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1811 readline_add_completion(rs, trace_event_get_name(ev));
1812 }
1813 g_free(pattern);
1814 }
1815 }
1816
1817 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
1818 {
1819 size_t len;
1820
1821 len = strlen(str);
1822 readline_set_completion_index(rs, len);
1823 if (nb_args == 2) {
1824 TraceEventIter iter;
1825 TraceEvent *ev;
1826 char *pattern = g_strdup_printf("%s*", str);
1827 trace_event_iter_init_pattern(&iter, pattern);
1828 while ((ev = trace_event_iter_next(&iter)) != NULL) {
1829 readline_add_completion(rs, trace_event_get_name(ev));
1830 }
1831 g_free(pattern);
1832 } else if (nb_args == 3) {
1833 add_completion_option(rs, str, "on");
1834 add_completion_option(rs, str, "off");
1835 }
1836 }
1837
1838 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
1839 {
1840 int i;
1841
1842 if (nb_args != 2) {
1843 return;
1844 }
1845 readline_set_completion_index(rs, strlen(str));
1846 for (i = 0; i < WATCHDOG_ACTION__MAX; i++) {
1847 add_completion_option(rs, str, WatchdogAction_str(i));
1848 }
1849 }
1850
1851 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
1852 const char *str)
1853 {
1854 size_t len;
1855
1856 len = strlen(str);
1857 readline_set_completion_index(rs, len);
1858 if (nb_args == 2) {
1859 int i;
1860 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1861 const char *name = MigrationCapability_str(i);
1862 if (!strncmp(str, name, len)) {
1863 readline_add_completion(rs, name);
1864 }
1865 }
1866 } else if (nb_args == 3) {
1867 add_completion_option(rs, str, "on");
1868 add_completion_option(rs, str, "off");
1869 }
1870 }
1871
1872 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
1873 const char *str)
1874 {
1875 size_t len;
1876
1877 len = strlen(str);
1878 readline_set_completion_index(rs, len);
1879 if (nb_args == 2) {
1880 int i;
1881 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1882 const char *name = MigrationParameter_str(i);
1883 if (!strncmp(str, name, len)) {
1884 readline_add_completion(rs, name);
1885 }
1886 }
1887 }
1888 }
1889
1890 static void vm_completion(ReadLineState *rs, const char *str)
1891 {
1892 size_t len;
1893 BlockDriverState *bs;
1894 BdrvNextIterator it;
1895
1896 len = strlen(str);
1897 readline_set_completion_index(rs, len);
1898
1899 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
1900 SnapshotInfoList *snapshots, *snapshot;
1901 AioContext *ctx = bdrv_get_aio_context(bs);
1902 bool ok = false;
1903
1904 aio_context_acquire(ctx);
1905 if (bdrv_can_snapshot(bs)) {
1906 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
1907 }
1908 aio_context_release(ctx);
1909 if (!ok) {
1910 continue;
1911 }
1912
1913 snapshot = snapshots;
1914 while (snapshot) {
1915 char *completion = snapshot->value->name;
1916 if (!strncmp(str, completion, len)) {
1917 readline_add_completion(rs, completion);
1918 }
1919 completion = snapshot->value->id;
1920 if (!strncmp(str, completion, len)) {
1921 readline_add_completion(rs, completion);
1922 }
1923 snapshot = snapshot->next;
1924 }
1925 qapi_free_SnapshotInfoList(snapshots);
1926 }
1927
1928 }
1929
1930 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
1931 {
1932 if (nb_args == 2) {
1933 vm_completion(rs, str);
1934 }
1935 }
1936
1937 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
1938 {
1939 if (nb_args == 2) {
1940 vm_completion(rs, str);
1941 }
1942 }
1943
1944 static int
1945 compare_mon_cmd(const void *a, const void *b)
1946 {
1947 return strcmp(((const HMPCommand *)a)->name,
1948 ((const HMPCommand *)b)->name);
1949 }
1950
1951 static void sortcmdlist(void)
1952 {
1953 qsort(hmp_cmds, ARRAY_SIZE(hmp_cmds) - 1,
1954 sizeof(*hmp_cmds),
1955 compare_mon_cmd);
1956 qsort(hmp_info_cmds, ARRAY_SIZE(hmp_info_cmds) - 1,
1957 sizeof(*hmp_info_cmds),
1958 compare_mon_cmd);
1959 }
1960
1961 void monitor_register_hmp(const char *name, bool info,
1962 void (*cmd)(Monitor *mon, const QDict *qdict))
1963 {
1964 HMPCommand *table = info ? hmp_info_cmds : hmp_cmds;
1965
1966 while (table->name != NULL) {
1967 if (strcmp(table->name, name) == 0) {
1968 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1969 table->cmd = cmd;
1970 return;
1971 }
1972 table++;
1973 }
1974 g_assert_not_reached();
1975 }
1976
1977 void monitor_register_hmp_info_hrt(const char *name,
1978 HumanReadableText *(*handler)(Error **errp))
1979 {
1980 HMPCommand *table = hmp_info_cmds;
1981
1982 while (table->name != NULL) {
1983 if (strcmp(table->name, name) == 0) {
1984 g_assert(table->cmd == NULL && table->cmd_info_hrt == NULL);
1985 table->cmd_info_hrt = handler;
1986 return;
1987 }
1988 table++;
1989 }
1990 g_assert_not_reached();
1991 }
1992
1993 void monitor_init_globals(void)
1994 {
1995 monitor_init_globals_core();
1996 monitor_init_qmp_commands();
1997 sortcmdlist();
1998 qemu_mutex_init(&mon_fdsets_lock);
1999 }