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