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