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