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