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