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