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