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