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