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