monitor: Split MonitorQAPIEventConf off MonitorQAPIEventState
[qemu/ar7.git] / monitor.c
blob07a0f763829275841b2f59e6f69dd941b77b5418
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "monitor/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/i386/pc.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "sysemu/char.h"
36 #include "ui/qemu-spice.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/numa.h"
39 #include "monitor/monitor.h"
40 #include "qemu/readline.h"
41 #include "ui/console.h"
42 #include "ui/input.h"
43 #include "sysemu/blockdev.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "migration/migration.h"
49 #include "sysemu/kvm.h"
50 #include "qemu/acl.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qerror.h"
53 #include "qapi/qmp/qint.h"
54 #include "qapi/qmp/qfloat.h"
55 #include "qapi/qmp/qlist.h"
56 #include "qapi/qmp/qbool.h"
57 #include "qapi/qmp/qstring.h"
58 #include "qapi/qmp/qjson.h"
59 #include "qapi/qmp/json-streamer.h"
60 #include "qapi/qmp/json-parser.h"
61 #include <qom/object_interfaces.h>
62 #include "qemu/osdep.h"
63 #include "cpu.h"
64 #include "trace.h"
65 #include "trace/control.h"
66 #include "monitor/hmp-target.h"
67 #ifdef CONFIG_TRACE_SIMPLE
68 #include "trace/simple.h"
69 #endif
70 #include "exec/memory.h"
71 #include "qmp-commands.h"
72 #include "hmp.h"
73 #include "qemu/thread.h"
74 #include "block/qapi.h"
75 #include "qapi/qmp-event.h"
76 #include "qapi-event.h"
77 #include "qmp-introspect.h"
78 #include "sysemu/block-backend.h"
80 /* for hmp_info_irq/pic */
81 #if defined(TARGET_SPARC)
82 #include "hw/sparc/sun4m.h"
83 #endif
84 #include "hw/lm32/lm32_pic.h"
86 #if defined(TARGET_S390X)
87 #include "hw/s390x/storage-keys.h"
88 #endif
91 * Supported types:
93 * 'F' filename
94 * 'B' block device name
95 * 's' string (accept optional quote)
96 * 'S' it just appends the rest of the string (accept optional quote)
97 * 'O' option string of the form NAME=VALUE,...
98 * parsed according to QemuOptsList given by its name
99 * Example: 'device:O' uses qemu_device_opts.
100 * Restriction: only lists with empty desc are supported
101 * TODO lift the restriction
102 * 'i' 32 bit integer
103 * 'l' target long (32 or 64 bit)
104 * 'M' Non-negative target long (32 or 64 bit), in user mode the
105 * value is multiplied by 2^20 (think Mebibyte)
106 * 'o' octets (aka bytes)
107 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
108 * K, k suffix, which multiplies the value by 2^60 for suffixes E
109 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
110 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
111 * 'T' double
112 * user mode accepts an optional ms, us, ns suffix,
113 * which divides the value by 1e3, 1e6, 1e9, respectively
114 * '/' optional gdb-like print format (like "/10x")
116 * '?' optional type (for all types, except '/')
117 * '.' other form of optional type (for 'i' and 'l')
118 * 'b' boolean
119 * user mode accepts "on" or "off"
120 * '-' optional parameter (eg. '-f')
124 typedef struct mon_cmd_t {
125 const char *name;
126 const char *args_type;
127 const char *params;
128 const char *help;
129 union {
130 void (*cmd)(Monitor *mon, const QDict *qdict);
131 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
132 } mhandler;
133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
137 struct mon_cmd_t *sub_table;
138 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
139 } mon_cmd_t;
141 /* file descriptors passed via SCM_RIGHTS */
142 typedef struct mon_fd_t mon_fd_t;
143 struct mon_fd_t {
144 char *name;
145 int fd;
146 QLIST_ENTRY(mon_fd_t) next;
149 /* file descriptor associated with a file descriptor set */
150 typedef struct MonFdsetFd MonFdsetFd;
151 struct MonFdsetFd {
152 int fd;
153 bool removed;
154 char *opaque;
155 QLIST_ENTRY(MonFdsetFd) next;
158 /* file descriptor set containing fds passed via SCM_RIGHTS */
159 typedef struct MonFdset MonFdset;
160 struct MonFdset {
161 int64_t id;
162 QLIST_HEAD(, MonFdsetFd) fds;
163 QLIST_HEAD(, MonFdsetFd) dup_fds;
164 QLIST_ENTRY(MonFdset) next;
167 typedef struct {
168 QObject *id;
169 JSONMessageParser parser;
171 * When a client connects, we're in capabilities negotiation mode.
172 * When command qmp_capabilities succeeds, we go into command
173 * mode.
175 bool in_command_mode; /* are we in command mode? */
176 } MonitorQMP;
179 * To prevent flooding clients, events can be throttled. The
180 * throttling is calculated globally, rather than per-Monitor
181 * instance.
183 typedef struct MonitorQAPIEventState {
184 QAPIEvent event; /* Event being tracked */
185 QEMUTimer *timer; /* Timer for handling delayed events */
186 QDict *qdict; /* Delayed event (if any) */
187 } MonitorQAPIEventState;
189 typedef struct {
190 int64_t rate; /* Minimum time (in ns) between two events */
191 } MonitorQAPIEventConf;
193 struct Monitor {
194 CharDriverState *chr;
195 int reset_seen;
196 int flags;
197 int suspend_cnt;
198 bool skip_flush;
200 QemuMutex out_lock;
201 QString *outbuf;
202 guint out_watch;
204 /* Read under either BQL or out_lock, written with BQL+out_lock. */
205 int mux_out;
207 ReadLineState *rs;
208 MonitorQMP qmp;
209 CPUState *mon_cpu;
210 BlockCompletionFunc *password_completion_cb;
211 void *password_opaque;
212 mon_cmd_t *cmd_table;
213 QLIST_HEAD(,mon_fd_t) fds;
214 QLIST_ENTRY(Monitor) entry;
217 /* QMP checker flags */
218 #define QMP_ACCEPT_UNKNOWNS 1
220 /* Protects mon_list, monitor_event_state. */
221 static QemuMutex monitor_lock;
223 static QLIST_HEAD(mon_list, Monitor) mon_list;
224 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
225 static int mon_refcount;
227 static mon_cmd_t mon_cmds[];
228 static mon_cmd_t info_cmds[];
230 static const mon_cmd_t qmp_cmds[];
232 Monitor *cur_mon;
234 static void monitor_command_cb(void *opaque, const char *cmdline,
235 void *readline_opaque);
238 * Is @mon a QMP monitor?
240 static inline bool monitor_is_qmp(const Monitor *mon)
242 return (mon->flags & MONITOR_USE_CONTROL);
246 * Is the current monitor, if any, a QMP monitor?
248 bool monitor_cur_is_qmp(void)
250 return cur_mon && monitor_is_qmp(cur_mon);
253 void monitor_read_command(Monitor *mon, int show_prompt)
255 if (!mon->rs)
256 return;
258 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
259 if (show_prompt)
260 readline_show_prompt(mon->rs);
263 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
264 void *opaque)
266 if (mon->rs) {
267 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
268 /* prompt is printed on return from the command handler */
269 return 0;
270 } else {
271 monitor_printf(mon, "terminal does not support password prompting\n");
272 return -ENOTTY;
276 static void monitor_flush_locked(Monitor *mon);
278 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
279 void *opaque)
281 Monitor *mon = opaque;
283 qemu_mutex_lock(&mon->out_lock);
284 mon->out_watch = 0;
285 monitor_flush_locked(mon);
286 qemu_mutex_unlock(&mon->out_lock);
287 return FALSE;
290 /* Called with mon->out_lock held. */
291 static void monitor_flush_locked(Monitor *mon)
293 int rc;
294 size_t len;
295 const char *buf;
297 if (mon->skip_flush) {
298 return;
301 buf = qstring_get_str(mon->outbuf);
302 len = qstring_get_length(mon->outbuf);
304 if (len && !mon->mux_out) {
305 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
306 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
307 /* all flushed or error */
308 QDECREF(mon->outbuf);
309 mon->outbuf = qstring_new();
310 return;
312 if (rc > 0) {
313 /* partinal write */
314 QString *tmp = qstring_from_str(buf + rc);
315 QDECREF(mon->outbuf);
316 mon->outbuf = tmp;
318 if (mon->out_watch == 0) {
319 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
320 monitor_unblocked, mon);
325 void monitor_flush(Monitor *mon)
327 qemu_mutex_lock(&mon->out_lock);
328 monitor_flush_locked(mon);
329 qemu_mutex_unlock(&mon->out_lock);
332 /* flush at every end of line */
333 static void monitor_puts(Monitor *mon, const char *str)
335 char c;
337 qemu_mutex_lock(&mon->out_lock);
338 for(;;) {
339 c = *str++;
340 if (c == '\0')
341 break;
342 if (c == '\n') {
343 qstring_append_chr(mon->outbuf, '\r');
345 qstring_append_chr(mon->outbuf, c);
346 if (c == '\n') {
347 monitor_flush_locked(mon);
350 qemu_mutex_unlock(&mon->out_lock);
353 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
355 char *buf;
357 if (!mon)
358 return;
360 if (monitor_is_qmp(mon)) {
361 return;
364 buf = g_strdup_vprintf(fmt, ap);
365 monitor_puts(mon, buf);
366 g_free(buf);
369 void monitor_printf(Monitor *mon, const char *fmt, ...)
371 va_list ap;
372 va_start(ap, fmt);
373 monitor_vprintf(mon, fmt, ap);
374 va_end(ap);
377 int monitor_fprintf(FILE *stream, const char *fmt, ...)
379 va_list ap;
380 va_start(ap, fmt);
381 monitor_vprintf((Monitor *)stream, fmt, ap);
382 va_end(ap);
383 return 0;
386 static void monitor_json_emitter(Monitor *mon, const QObject *data)
388 QString *json;
390 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
391 qobject_to_json(data);
392 assert(json != NULL);
394 qstring_append_chr(json, '\n');
395 monitor_puts(mon, qstring_get_str(json));
397 QDECREF(json);
400 static QDict *build_qmp_error_dict(Error *err)
402 QObject *obj;
404 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
405 ErrorClass_lookup[error_get_class(err)],
406 error_get_pretty(err));
408 return qobject_to_qdict(obj);
411 static void monitor_protocol_emitter(Monitor *mon, QObject *data,
412 Error *err)
414 QDict *qmp;
416 trace_monitor_protocol_emitter(mon);
418 if (!err) {
419 /* success response */
420 qmp = qdict_new();
421 if (data) {
422 qobject_incref(data);
423 qdict_put_obj(qmp, "return", data);
424 } else {
425 /* return an empty QDict by default */
426 qdict_put(qmp, "return", qdict_new());
428 } else {
429 /* error response */
430 qmp = build_qmp_error_dict(err);
433 if (mon->qmp.id) {
434 qdict_put_obj(qmp, "id", mon->qmp.id);
435 mon->qmp.id = NULL;
438 monitor_json_emitter(mon, QOBJECT(qmp));
439 QDECREF(qmp);
443 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT_MAX] = {
444 /* Limit guest-triggerable events to 1 per second */
445 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
446 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
447 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
448 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
449 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
450 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
453 static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
456 * Emits the event to every monitor instance, @event is only used for trace
457 * Called with monitor_lock held.
459 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
461 Monitor *mon;
463 trace_monitor_protocol_event_emit(event, qdict);
464 QLIST_FOREACH(mon, &mon_list, entry) {
465 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
466 monitor_json_emitter(mon, QOBJECT(qdict));
472 * Queue a new event for emission to Monitor instances,
473 * applying any rate limiting if required.
475 static void
476 monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
478 MonitorQAPIEventConf *evconf;
479 MonitorQAPIEventState *evstate;
481 assert(event < QAPI_EVENT_MAX);
482 evconf = &monitor_qapi_event_conf[event];
483 evstate = &monitor_qapi_event_state[event];
484 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
486 qemu_mutex_lock(&monitor_lock);
488 if (!evconf->rate) {
489 /* Unthrottled event */
490 monitor_qapi_event_emit(event, qdict);
491 } else {
492 if (timer_pending(evstate->timer)) {
494 * Timer is pending for (at least) evconf->rate ns after
495 * last send. Store event for sending when timer fires,
496 * replacing a prior stored event if any.
498 QDECREF(evstate->qdict);
499 evstate->qdict = qdict;
500 QINCREF(evstate->qdict);
501 } else {
503 * Last send was (at least) evconf->rate ns ago.
504 * Send immediately, and arm the timer to call
505 * monitor_qapi_event_handler() in evconf->rate ns. Any
506 * events arriving before then will be delayed until then.
508 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
510 monitor_qapi_event_emit(event, qdict);
511 timer_mod_ns(evstate->timer, now + evconf->rate);
515 qemu_mutex_unlock(&monitor_lock);
519 * This function runs evconf->rate ns after sending a throttled
520 * event.
521 * If another event has since been stored, send it.
523 static void monitor_qapi_event_handler(void *opaque)
525 MonitorQAPIEventState *evstate = opaque;
526 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
528 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
529 qemu_mutex_lock(&monitor_lock);
531 if (evstate->qdict) {
532 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
534 monitor_qapi_event_emit(evstate->event, evstate->qdict);
535 QDECREF(evstate->qdict);
536 evstate->qdict = NULL;
537 timer_mod_ns(evstate->timer, now + evconf->rate);
540 qemu_mutex_unlock(&monitor_lock);
543 static void monitor_qapi_event_init(void)
545 int i;
546 MonitorQAPIEventState *evstate;
548 for (i = 0; i < QAPI_EVENT_MAX; i++) {
549 if (monitor_qapi_event_conf[i].rate) {
550 evstate = &monitor_qapi_event_state[i];
551 evstate->event = i;
552 evstate->qdict = NULL;
553 evstate->timer = timer_new_ns(QEMU_CLOCK_REALTIME,
554 monitor_qapi_event_handler,
555 evstate);
559 qmp_event_set_func_emit(monitor_qapi_event_queue);
562 static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
564 cur_mon->qmp.in_command_mode = true;
567 static void handle_hmp_command(Monitor *mon, const char *cmdline);
569 static void monitor_data_init(Monitor *mon)
571 memset(mon, 0, sizeof(Monitor));
572 qemu_mutex_init(&mon->out_lock);
573 mon->outbuf = qstring_new();
574 /* Use *mon_cmds by default. */
575 mon->cmd_table = mon_cmds;
578 static void monitor_data_destroy(Monitor *mon)
580 QDECREF(mon->outbuf);
581 qemu_mutex_destroy(&mon->out_lock);
584 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
585 int64_t cpu_index, Error **errp)
587 char *output = NULL;
588 Monitor *old_mon, hmp;
590 monitor_data_init(&hmp);
591 hmp.skip_flush = true;
593 old_mon = cur_mon;
594 cur_mon = &hmp;
596 if (has_cpu_index) {
597 int ret = monitor_set_cpu(cpu_index);
598 if (ret < 0) {
599 cur_mon = old_mon;
600 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
601 "a CPU number");
602 goto out;
606 handle_hmp_command(&hmp, command_line);
607 cur_mon = old_mon;
609 qemu_mutex_lock(&hmp.out_lock);
610 if (qstring_get_length(hmp.outbuf) > 0) {
611 output = g_strdup(qstring_get_str(hmp.outbuf));
612 } else {
613 output = g_strdup("");
615 qemu_mutex_unlock(&hmp.out_lock);
617 out:
618 monitor_data_destroy(&hmp);
619 return output;
622 static int compare_cmd(const char *name, const char *list)
624 const char *p, *pstart;
625 int len;
626 len = strlen(name);
627 p = list;
628 for(;;) {
629 pstart = p;
630 p = strchr(p, '|');
631 if (!p)
632 p = pstart + strlen(pstart);
633 if ((p - pstart) == len && !memcmp(pstart, name, len))
634 return 1;
635 if (*p == '\0')
636 break;
637 p++;
639 return 0;
642 static int get_str(char *buf, int buf_size, const char **pp)
644 const char *p;
645 char *q;
646 int c;
648 q = buf;
649 p = *pp;
650 while (qemu_isspace(*p)) {
651 p++;
653 if (*p == '\0') {
654 fail:
655 *q = '\0';
656 *pp = p;
657 return -1;
659 if (*p == '\"') {
660 p++;
661 while (*p != '\0' && *p != '\"') {
662 if (*p == '\\') {
663 p++;
664 c = *p++;
665 switch (c) {
666 case 'n':
667 c = '\n';
668 break;
669 case 'r':
670 c = '\r';
671 break;
672 case '\\':
673 case '\'':
674 case '\"':
675 break;
676 default:
677 printf("unsupported escape code: '\\%c'\n", c);
678 goto fail;
680 if ((q - buf) < buf_size - 1) {
681 *q++ = c;
683 } else {
684 if ((q - buf) < buf_size - 1) {
685 *q++ = *p;
687 p++;
690 if (*p != '\"') {
691 printf("unterminated string\n");
692 goto fail;
694 p++;
695 } else {
696 while (*p != '\0' && !qemu_isspace(*p)) {
697 if ((q - buf) < buf_size - 1) {
698 *q++ = *p;
700 p++;
703 *q = '\0';
704 *pp = p;
705 return 0;
708 #define MAX_ARGS 16
710 static void free_cmdline_args(char **args, int nb_args)
712 int i;
714 assert(nb_args <= MAX_ARGS);
716 for (i = 0; i < nb_args; i++) {
717 g_free(args[i]);
723 * Parse the command line to get valid args.
724 * @cmdline: command line to be parsed.
725 * @pnb_args: location to store the number of args, must NOT be NULL.
726 * @args: location to store the args, which should be freed by caller, must
727 * NOT be NULL.
729 * Returns 0 on success, negative on failure.
731 * NOTE: this parser is an approximate form of the real command parser. Number
732 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
733 * return with failure.
735 static int parse_cmdline(const char *cmdline,
736 int *pnb_args, char **args)
738 const char *p;
739 int nb_args, ret;
740 char buf[1024];
742 p = cmdline;
743 nb_args = 0;
744 for (;;) {
745 while (qemu_isspace(*p)) {
746 p++;
748 if (*p == '\0') {
749 break;
751 if (nb_args >= MAX_ARGS) {
752 goto fail;
754 ret = get_str(buf, sizeof(buf), &p);
755 if (ret < 0) {
756 goto fail;
758 args[nb_args] = g_strdup(buf);
759 nb_args++;
761 *pnb_args = nb_args;
762 return 0;
764 fail:
765 free_cmdline_args(args, nb_args);
766 return -1;
769 static void help_cmd_dump_one(Monitor *mon,
770 const mon_cmd_t *cmd,
771 char **prefix_args,
772 int prefix_args_nb)
774 int i;
776 for (i = 0; i < prefix_args_nb; i++) {
777 monitor_printf(mon, "%s ", prefix_args[i]);
779 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
782 /* @args[@arg_index] is the valid command need to find in @cmds */
783 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
784 char **args, int nb_args, int arg_index)
786 const mon_cmd_t *cmd;
788 /* No valid arg need to compare with, dump all in *cmds */
789 if (arg_index >= nb_args) {
790 for (cmd = cmds; cmd->name != NULL; cmd++) {
791 help_cmd_dump_one(mon, cmd, args, arg_index);
793 return;
796 /* Find one entry to dump */
797 for (cmd = cmds; cmd->name != NULL; cmd++) {
798 if (compare_cmd(args[arg_index], cmd->name)) {
799 if (cmd->sub_table) {
800 /* continue with next arg */
801 help_cmd_dump(mon, cmd->sub_table,
802 args, nb_args, arg_index + 1);
803 } else {
804 help_cmd_dump_one(mon, cmd, args, arg_index);
806 break;
811 static void help_cmd(Monitor *mon, const char *name)
813 char *args[MAX_ARGS];
814 int nb_args = 0;
816 /* 1. parse user input */
817 if (name) {
818 /* special case for log, directly dump and return */
819 if (!strcmp(name, "log")) {
820 const QEMULogItem *item;
821 monitor_printf(mon, "Log items (comma separated):\n");
822 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
823 for (item = qemu_log_items; item->mask != 0; item++) {
824 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
826 return;
829 if (parse_cmdline(name, &nb_args, args) < 0) {
830 return;
834 /* 2. dump the contents according to parsed args */
835 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
837 free_cmdline_args(args, nb_args);
840 static void do_help_cmd(Monitor *mon, const QDict *qdict)
842 help_cmd(mon, qdict_get_try_str(qdict, "name"));
845 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
847 const char *tp_name = qdict_get_str(qdict, "name");
848 bool new_state = qdict_get_bool(qdict, "option");
849 Error *local_err = NULL;
851 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
852 if (local_err) {
853 error_report_err(local_err);
857 #ifdef CONFIG_TRACE_SIMPLE
858 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
860 const char *op = qdict_get_try_str(qdict, "op");
861 const char *arg = qdict_get_try_str(qdict, "arg");
863 if (!op) {
864 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
865 } else if (!strcmp(op, "on")) {
866 st_set_trace_file_enabled(true);
867 } else if (!strcmp(op, "off")) {
868 st_set_trace_file_enabled(false);
869 } else if (!strcmp(op, "flush")) {
870 st_flush_trace_buffer();
871 } else if (!strcmp(op, "set")) {
872 if (arg) {
873 st_set_trace_file(arg);
875 } else {
876 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
877 help_cmd(mon, "trace-file");
880 #endif
882 static void hmp_info_help(Monitor *mon, const QDict *qdict)
884 help_cmd(mon, "info");
887 CommandInfoList *qmp_query_commands(Error **errp)
889 CommandInfoList *info, *cmd_list = NULL;
890 const mon_cmd_t *cmd;
892 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
893 info = g_malloc0(sizeof(*info));
894 info->value = g_malloc0(sizeof(*info->value));
895 info->value->name = g_strdup(cmd->name);
897 info->next = cmd_list;
898 cmd_list = info;
901 return cmd_list;
904 EventInfoList *qmp_query_events(Error **errp)
906 EventInfoList *info, *ev_list = NULL;
907 QAPIEvent e;
909 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
910 const char *event_name = QAPIEvent_lookup[e];
911 assert(event_name != NULL);
912 info = g_malloc0(sizeof(*info));
913 info->value = g_malloc0(sizeof(*info->value));
914 info->value->name = g_strdup(event_name);
916 info->next = ev_list;
917 ev_list = info;
920 return ev_list;
924 * Minor hack: generated marshalling suppressed for this command
925 * ('gen': false in the schema) so we can parse the JSON string
926 * directly into QObject instead of first parsing it with
927 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
928 * to QObject with generated output marshallers, every time. Instead,
929 * we do it in test-qmp-input-visitor.c, just to make sure
930 * qapi-introspect.py's output actually conforms to the schema.
932 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
933 Error **errp)
935 *ret_data = qobject_from_json(qmp_schema_json);
938 /* set the current CPU defined by the user */
939 int monitor_set_cpu(int cpu_index)
941 CPUState *cpu;
943 cpu = qemu_get_cpu(cpu_index);
944 if (cpu == NULL) {
945 return -1;
947 cur_mon->mon_cpu = cpu;
948 return 0;
951 CPUState *mon_get_cpu(void)
953 if (!cur_mon->mon_cpu) {
954 monitor_set_cpu(0);
956 cpu_synchronize_state(cur_mon->mon_cpu);
957 return cur_mon->mon_cpu;
960 CPUArchState *mon_get_cpu_env(void)
962 return mon_get_cpu()->env_ptr;
965 int monitor_get_cpu_index(void)
967 return mon_get_cpu()->cpu_index;
970 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
972 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
975 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
977 dump_exec_info((FILE *)mon, monitor_fprintf);
978 dump_drift_info((FILE *)mon, monitor_fprintf);
981 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
983 dump_opcount_info((FILE *)mon, monitor_fprintf);
986 static void hmp_info_history(Monitor *mon, const QDict *qdict)
988 int i;
989 const char *str;
991 if (!mon->rs)
992 return;
993 i = 0;
994 for(;;) {
995 str = readline_get_history(mon->rs, i);
996 if (!str)
997 break;
998 monitor_printf(mon, "%d: '%s'\n", i, str);
999 i++;
1003 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
1005 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
1008 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1010 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1011 TraceEventInfoList *elem;
1013 for (elem = events; elem != NULL; elem = elem->next) {
1014 monitor_printf(mon, "%s : state %u\n",
1015 elem->value->name,
1016 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1018 qapi_free_TraceEventInfoList(events);
1021 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1022 bool has_port, int64_t port,
1023 bool has_tls_port, int64_t tls_port,
1024 bool has_cert_subject, const char *cert_subject,
1025 Error **errp)
1027 if (strcmp(protocol, "spice") == 0) {
1028 if (!qemu_using_spice(errp)) {
1029 return;
1032 if (!has_port && !has_tls_port) {
1033 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1034 return;
1037 if (qemu_spice_migrate_info(hostname,
1038 has_port ? port : -1,
1039 has_tls_port ? tls_port : -1,
1040 cert_subject)) {
1041 error_setg(errp, QERR_UNDEFINED_ERROR);
1042 return;
1044 return;
1047 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1050 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1052 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1055 static void hmp_log(Monitor *mon, const QDict *qdict)
1057 int mask;
1058 const char *items = qdict_get_str(qdict, "items");
1060 if (!strcmp(items, "none")) {
1061 mask = 0;
1062 } else {
1063 mask = qemu_str_to_log_mask(items);
1064 if (!mask) {
1065 help_cmd(mon, "log");
1066 return;
1069 qemu_set_log(mask);
1072 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1074 const char *option = qdict_get_try_str(qdict, "option");
1075 if (!option || !strcmp(option, "on")) {
1076 singlestep = 1;
1077 } else if (!strcmp(option, "off")) {
1078 singlestep = 0;
1079 } else {
1080 monitor_printf(mon, "unexpected option %s\n", option);
1084 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1086 const char *device = qdict_get_try_str(qdict, "device");
1087 if (!device)
1088 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1089 if (gdbserver_start(device) < 0) {
1090 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1091 device);
1092 } else if (strcmp(device, "none") == 0) {
1093 monitor_printf(mon, "Disabled gdbserver\n");
1094 } else {
1095 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1096 device);
1100 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1102 const char *action = qdict_get_str(qdict, "action");
1103 if (select_watchdog_action(action) == -1) {
1104 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1108 static void monitor_printc(Monitor *mon, int c)
1110 monitor_printf(mon, "'");
1111 switch(c) {
1112 case '\'':
1113 monitor_printf(mon, "\\'");
1114 break;
1115 case '\\':
1116 monitor_printf(mon, "\\\\");
1117 break;
1118 case '\n':
1119 monitor_printf(mon, "\\n");
1120 break;
1121 case '\r':
1122 monitor_printf(mon, "\\r");
1123 break;
1124 default:
1125 if (c >= 32 && c <= 126) {
1126 monitor_printf(mon, "%c", c);
1127 } else {
1128 monitor_printf(mon, "\\x%02x", c);
1130 break;
1132 monitor_printf(mon, "'");
1135 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1136 hwaddr addr, int is_physical)
1138 int l, line_size, i, max_digits, len;
1139 uint8_t buf[16];
1140 uint64_t v;
1142 if (format == 'i') {
1143 int flags = 0;
1144 #ifdef TARGET_I386
1145 CPUArchState *env = mon_get_cpu_env();
1146 if (wsize == 2) {
1147 flags = 1;
1148 } else if (wsize == 4) {
1149 flags = 0;
1150 } else {
1151 /* as default we use the current CS size */
1152 flags = 0;
1153 if (env) {
1154 #ifdef TARGET_X86_64
1155 if ((env->efer & MSR_EFER_LMA) &&
1156 (env->segs[R_CS].flags & DESC_L_MASK))
1157 flags = 2;
1158 else
1159 #endif
1160 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1161 flags = 1;
1164 #endif
1165 #ifdef TARGET_PPC
1166 CPUArchState *env = mon_get_cpu_env();
1167 flags = msr_le << 16;
1168 flags |= env->bfd_mach;
1169 #endif
1170 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
1171 return;
1174 len = wsize * count;
1175 if (wsize == 1)
1176 line_size = 8;
1177 else
1178 line_size = 16;
1179 max_digits = 0;
1181 switch(format) {
1182 case 'o':
1183 max_digits = (wsize * 8 + 2) / 3;
1184 break;
1185 default:
1186 case 'x':
1187 max_digits = (wsize * 8) / 4;
1188 break;
1189 case 'u':
1190 case 'd':
1191 max_digits = (wsize * 8 * 10 + 32) / 33;
1192 break;
1193 case 'c':
1194 wsize = 1;
1195 break;
1198 while (len > 0) {
1199 if (is_physical)
1200 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1201 else
1202 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1203 l = len;
1204 if (l > line_size)
1205 l = line_size;
1206 if (is_physical) {
1207 cpu_physical_memory_read(addr, buf, l);
1208 } else {
1209 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
1210 monitor_printf(mon, " Cannot access memory\n");
1211 break;
1214 i = 0;
1215 while (i < l) {
1216 switch(wsize) {
1217 default:
1218 case 1:
1219 v = ldub_p(buf + i);
1220 break;
1221 case 2:
1222 v = lduw_p(buf + i);
1223 break;
1224 case 4:
1225 v = (uint32_t)ldl_p(buf + i);
1226 break;
1227 case 8:
1228 v = ldq_p(buf + i);
1229 break;
1231 monitor_printf(mon, " ");
1232 switch(format) {
1233 case 'o':
1234 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1235 break;
1236 case 'x':
1237 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1238 break;
1239 case 'u':
1240 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1241 break;
1242 case 'd':
1243 monitor_printf(mon, "%*" PRId64, max_digits, v);
1244 break;
1245 case 'c':
1246 monitor_printc(mon, v);
1247 break;
1249 i += wsize;
1251 monitor_printf(mon, "\n");
1252 addr += l;
1253 len -= l;
1257 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1259 int count = qdict_get_int(qdict, "count");
1260 int format = qdict_get_int(qdict, "format");
1261 int size = qdict_get_int(qdict, "size");
1262 target_long addr = qdict_get_int(qdict, "addr");
1264 memory_dump(mon, count, format, size, addr, 0);
1267 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1269 int count = qdict_get_int(qdict, "count");
1270 int format = qdict_get_int(qdict, "format");
1271 int size = qdict_get_int(qdict, "size");
1272 hwaddr addr = qdict_get_int(qdict, "addr");
1274 memory_dump(mon, count, format, size, addr, 1);
1277 static void do_print(Monitor *mon, const QDict *qdict)
1279 int format = qdict_get_int(qdict, "format");
1280 hwaddr val = qdict_get_int(qdict, "val");
1282 switch(format) {
1283 case 'o':
1284 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1285 break;
1286 case 'x':
1287 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1288 break;
1289 case 'u':
1290 monitor_printf(mon, "%" HWADDR_PRIu, val);
1291 break;
1292 default:
1293 case 'd':
1294 monitor_printf(mon, "%" HWADDR_PRId, val);
1295 break;
1296 case 'c':
1297 monitor_printc(mon, val);
1298 break;
1300 monitor_printf(mon, "\n");
1303 static void hmp_sum(Monitor *mon, const QDict *qdict)
1305 uint32_t addr;
1306 uint16_t sum;
1307 uint32_t start = qdict_get_int(qdict, "start");
1308 uint32_t size = qdict_get_int(qdict, "size");
1310 sum = 0;
1311 for(addr = start; addr < (start + size); addr++) {
1312 uint8_t val = address_space_ldub(&address_space_memory, addr,
1313 MEMTXATTRS_UNSPECIFIED, NULL);
1314 /* BSD sum algorithm ('sum' Unix command) */
1315 sum = (sum >> 1) | (sum << 15);
1316 sum += val;
1318 monitor_printf(mon, "%05d\n", sum);
1321 static int mouse_button_state;
1323 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1325 int dx, dy, dz, button;
1326 const char *dx_str = qdict_get_str(qdict, "dx_str");
1327 const char *dy_str = qdict_get_str(qdict, "dy_str");
1328 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1330 dx = strtol(dx_str, NULL, 0);
1331 dy = strtol(dy_str, NULL, 0);
1332 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1333 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1335 if (dz_str) {
1336 dz = strtol(dz_str, NULL, 0);
1337 if (dz != 0) {
1338 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1339 qemu_input_queue_btn(NULL, button, true);
1340 qemu_input_event_sync();
1341 qemu_input_queue_btn(NULL, button, false);
1344 qemu_input_event_sync();
1347 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1349 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1350 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1351 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1352 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1354 int button_state = qdict_get_int(qdict, "button_state");
1356 if (mouse_button_state == button_state) {
1357 return;
1359 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1360 qemu_input_event_sync();
1361 mouse_button_state = button_state;
1364 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1366 int size = qdict_get_int(qdict, "size");
1367 int addr = qdict_get_int(qdict, "addr");
1368 int has_index = qdict_haskey(qdict, "index");
1369 uint32_t val;
1370 int suffix;
1372 if (has_index) {
1373 int index = qdict_get_int(qdict, "index");
1374 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1375 addr++;
1377 addr &= 0xffff;
1379 switch(size) {
1380 default:
1381 case 1:
1382 val = cpu_inb(addr);
1383 suffix = 'b';
1384 break;
1385 case 2:
1386 val = cpu_inw(addr);
1387 suffix = 'w';
1388 break;
1389 case 4:
1390 val = cpu_inl(addr);
1391 suffix = 'l';
1392 break;
1394 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1395 suffix, addr, size * 2, val);
1398 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1400 int size = qdict_get_int(qdict, "size");
1401 int addr = qdict_get_int(qdict, "addr");
1402 int val = qdict_get_int(qdict, "val");
1404 addr &= IOPORTS_MASK;
1406 switch (size) {
1407 default:
1408 case 1:
1409 cpu_outb(addr, val);
1410 break;
1411 case 2:
1412 cpu_outw(addr, val);
1413 break;
1414 case 4:
1415 cpu_outl(addr, val);
1416 break;
1420 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1422 Error *local_err = NULL;
1423 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1425 qemu_boot_set(bootdevice, &local_err);
1426 if (local_err) {
1427 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1428 error_free(local_err);
1429 } else {
1430 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1434 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1436 mtree_info((fprintf_function)monitor_printf, mon);
1439 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1441 int i;
1442 CPUState *cpu;
1443 uint64_t *node_mem;
1445 node_mem = g_new0(uint64_t, nb_numa_nodes);
1446 query_numa_node_mem(node_mem);
1447 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1448 for (i = 0; i < nb_numa_nodes; i++) {
1449 monitor_printf(mon, "node %d cpus:", i);
1450 CPU_FOREACH(cpu) {
1451 if (cpu->numa_node == i) {
1452 monitor_printf(mon, " %d", cpu->cpu_index);
1455 monitor_printf(mon, "\n");
1456 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1457 node_mem[i] >> 20);
1459 g_free(node_mem);
1462 #ifdef CONFIG_PROFILER
1464 int64_t tcg_time;
1465 int64_t dev_time;
1467 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1469 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1470 dev_time, dev_time / (double)get_ticks_per_sec());
1471 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1472 tcg_time, tcg_time / (double)get_ticks_per_sec());
1473 tcg_time = 0;
1474 dev_time = 0;
1476 #else
1477 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1479 monitor_printf(mon, "Internal profiler not compiled\n");
1481 #endif
1483 /* Capture support */
1484 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1486 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1488 int i;
1489 CaptureState *s;
1491 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1492 monitor_printf(mon, "[%d]: ", i);
1493 s->ops.info (s->opaque);
1497 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1499 int i;
1500 int n = qdict_get_int(qdict, "n");
1501 CaptureState *s;
1503 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1504 if (i == n) {
1505 s->ops.destroy (s->opaque);
1506 QLIST_REMOVE (s, entries);
1507 g_free (s);
1508 return;
1513 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1515 const char *path = qdict_get_str(qdict, "path");
1516 int has_freq = qdict_haskey(qdict, "freq");
1517 int freq = qdict_get_try_int(qdict, "freq", -1);
1518 int has_bits = qdict_haskey(qdict, "bits");
1519 int bits = qdict_get_try_int(qdict, "bits", -1);
1520 int has_channels = qdict_haskey(qdict, "nchannels");
1521 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1522 CaptureState *s;
1524 s = g_malloc0 (sizeof (*s));
1526 freq = has_freq ? freq : 44100;
1527 bits = has_bits ? bits : 16;
1528 nchannels = has_channels ? nchannels : 2;
1530 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1531 monitor_printf(mon, "Failed to add wave capture\n");
1532 g_free (s);
1533 return;
1535 QLIST_INSERT_HEAD (&capture_head, s, entries);
1538 static qemu_acl *find_acl(Monitor *mon, const char *name)
1540 qemu_acl *acl = qemu_acl_find(name);
1542 if (!acl) {
1543 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1545 return acl;
1548 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1550 const char *aclname = qdict_get_str(qdict, "aclname");
1551 qemu_acl *acl = find_acl(mon, aclname);
1552 qemu_acl_entry *entry;
1553 int i = 0;
1555 if (acl) {
1556 monitor_printf(mon, "policy: %s\n",
1557 acl->defaultDeny ? "deny" : "allow");
1558 QTAILQ_FOREACH(entry, &acl->entries, next) {
1559 i++;
1560 monitor_printf(mon, "%d: %s %s\n", i,
1561 entry->deny ? "deny" : "allow", entry->match);
1566 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1568 const char *aclname = qdict_get_str(qdict, "aclname");
1569 qemu_acl *acl = find_acl(mon, aclname);
1571 if (acl) {
1572 qemu_acl_reset(acl);
1573 monitor_printf(mon, "acl: removed all rules\n");
1577 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1579 const char *aclname = qdict_get_str(qdict, "aclname");
1580 const char *policy = qdict_get_str(qdict, "policy");
1581 qemu_acl *acl = find_acl(mon, aclname);
1583 if (acl) {
1584 if (strcmp(policy, "allow") == 0) {
1585 acl->defaultDeny = 0;
1586 monitor_printf(mon, "acl: policy set to 'allow'\n");
1587 } else if (strcmp(policy, "deny") == 0) {
1588 acl->defaultDeny = 1;
1589 monitor_printf(mon, "acl: policy set to 'deny'\n");
1590 } else {
1591 monitor_printf(mon, "acl: unknown policy '%s', "
1592 "expected 'deny' or 'allow'\n", policy);
1597 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1599 const char *aclname = qdict_get_str(qdict, "aclname");
1600 const char *match = qdict_get_str(qdict, "match");
1601 const char *policy = qdict_get_str(qdict, "policy");
1602 int has_index = qdict_haskey(qdict, "index");
1603 int index = qdict_get_try_int(qdict, "index", -1);
1604 qemu_acl *acl = find_acl(mon, aclname);
1605 int deny, ret;
1607 if (acl) {
1608 if (strcmp(policy, "allow") == 0) {
1609 deny = 0;
1610 } else if (strcmp(policy, "deny") == 0) {
1611 deny = 1;
1612 } else {
1613 monitor_printf(mon, "acl: unknown policy '%s', "
1614 "expected 'deny' or 'allow'\n", policy);
1615 return;
1617 if (has_index)
1618 ret = qemu_acl_insert(acl, deny, match, index);
1619 else
1620 ret = qemu_acl_append(acl, deny, match);
1621 if (ret < 0)
1622 monitor_printf(mon, "acl: unable to add acl entry\n");
1623 else
1624 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1628 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1630 const char *aclname = qdict_get_str(qdict, "aclname");
1631 const char *match = qdict_get_str(qdict, "match");
1632 qemu_acl *acl = find_acl(mon, aclname);
1633 int ret;
1635 if (acl) {
1636 ret = qemu_acl_remove(acl, match);
1637 if (ret < 0)
1638 monitor_printf(mon, "acl: no matching acl entry\n");
1639 else
1640 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1644 void qmp_getfd(const char *fdname, Error **errp)
1646 mon_fd_t *monfd;
1647 int fd;
1649 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
1650 if (fd == -1) {
1651 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1652 return;
1655 if (qemu_isdigit(fdname[0])) {
1656 close(fd);
1657 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1658 "a name not starting with a digit");
1659 return;
1662 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1663 if (strcmp(monfd->name, fdname) != 0) {
1664 continue;
1667 close(monfd->fd);
1668 monfd->fd = fd;
1669 return;
1672 monfd = g_malloc0(sizeof(mon_fd_t));
1673 monfd->name = g_strdup(fdname);
1674 monfd->fd = fd;
1676 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1679 void qmp_closefd(const char *fdname, Error **errp)
1681 mon_fd_t *monfd;
1683 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1684 if (strcmp(monfd->name, fdname) != 0) {
1685 continue;
1688 QLIST_REMOVE(monfd, next);
1689 close(monfd->fd);
1690 g_free(monfd->name);
1691 g_free(monfd);
1692 return;
1695 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1698 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
1700 int saved_vm_running = runstate_is_running();
1701 const char *name = qdict_get_str(qdict, "name");
1703 vm_stop(RUN_STATE_RESTORE_VM);
1705 if (load_vmstate(name) == 0 && saved_vm_running) {
1706 vm_start();
1710 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1712 mon_fd_t *monfd;
1714 QLIST_FOREACH(monfd, &mon->fds, next) {
1715 int fd;
1717 if (strcmp(monfd->name, fdname) != 0) {
1718 continue;
1721 fd = monfd->fd;
1723 /* caller takes ownership of fd */
1724 QLIST_REMOVE(monfd, next);
1725 g_free(monfd->name);
1726 g_free(monfd);
1728 return fd;
1731 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1732 return -1;
1735 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1737 MonFdsetFd *mon_fdset_fd;
1738 MonFdsetFd *mon_fdset_fd_next;
1740 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1741 if ((mon_fdset_fd->removed ||
1742 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1743 runstate_is_running()) {
1744 close(mon_fdset_fd->fd);
1745 g_free(mon_fdset_fd->opaque);
1746 QLIST_REMOVE(mon_fdset_fd, next);
1747 g_free(mon_fdset_fd);
1751 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1752 QLIST_REMOVE(mon_fdset, next);
1753 g_free(mon_fdset);
1757 static void monitor_fdsets_cleanup(void)
1759 MonFdset *mon_fdset;
1760 MonFdset *mon_fdset_next;
1762 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1763 monitor_fdset_cleanup(mon_fdset);
1767 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1768 const char *opaque, Error **errp)
1770 int fd;
1771 Monitor *mon = cur_mon;
1772 AddfdInfo *fdinfo;
1774 fd = qemu_chr_fe_get_msgfd(mon->chr);
1775 if (fd == -1) {
1776 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1777 goto error;
1780 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1781 has_opaque, opaque, errp);
1782 if (fdinfo) {
1783 return fdinfo;
1786 error:
1787 if (fd != -1) {
1788 close(fd);
1790 return NULL;
1793 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1795 MonFdset *mon_fdset;
1796 MonFdsetFd *mon_fdset_fd;
1797 char fd_str[60];
1799 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1800 if (mon_fdset->id != fdset_id) {
1801 continue;
1803 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1804 if (has_fd) {
1805 if (mon_fdset_fd->fd != fd) {
1806 continue;
1808 mon_fdset_fd->removed = true;
1809 break;
1810 } else {
1811 mon_fdset_fd->removed = true;
1814 if (has_fd && !mon_fdset_fd) {
1815 goto error;
1817 monitor_fdset_cleanup(mon_fdset);
1818 return;
1821 error:
1822 if (has_fd) {
1823 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1824 fdset_id, fd);
1825 } else {
1826 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1828 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1831 FdsetInfoList *qmp_query_fdsets(Error **errp)
1833 MonFdset *mon_fdset;
1834 MonFdsetFd *mon_fdset_fd;
1835 FdsetInfoList *fdset_list = NULL;
1837 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1838 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1839 FdsetFdInfoList *fdsetfd_list = NULL;
1841 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1842 fdset_info->value->fdset_id = mon_fdset->id;
1844 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1845 FdsetFdInfoList *fdsetfd_info;
1847 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1848 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1849 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1850 if (mon_fdset_fd->opaque) {
1851 fdsetfd_info->value->has_opaque = true;
1852 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1853 } else {
1854 fdsetfd_info->value->has_opaque = false;
1857 fdsetfd_info->next = fdsetfd_list;
1858 fdsetfd_list = fdsetfd_info;
1861 fdset_info->value->fds = fdsetfd_list;
1863 fdset_info->next = fdset_list;
1864 fdset_list = fdset_info;
1867 return fdset_list;
1870 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1871 bool has_opaque, const char *opaque,
1872 Error **errp)
1874 MonFdset *mon_fdset = NULL;
1875 MonFdsetFd *mon_fdset_fd;
1876 AddfdInfo *fdinfo;
1878 if (has_fdset_id) {
1879 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1880 /* Break if match found or match impossible due to ordering by ID */
1881 if (fdset_id <= mon_fdset->id) {
1882 if (fdset_id < mon_fdset->id) {
1883 mon_fdset = NULL;
1885 break;
1890 if (mon_fdset == NULL) {
1891 int64_t fdset_id_prev = -1;
1892 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1894 if (has_fdset_id) {
1895 if (fdset_id < 0) {
1896 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1897 "a non-negative value");
1898 return NULL;
1900 /* Use specified fdset ID */
1901 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1902 mon_fdset_cur = mon_fdset;
1903 if (fdset_id < mon_fdset_cur->id) {
1904 break;
1907 } else {
1908 /* Use first available fdset ID */
1909 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1910 mon_fdset_cur = mon_fdset;
1911 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1912 fdset_id_prev = mon_fdset_cur->id;
1913 continue;
1915 break;
1919 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1920 if (has_fdset_id) {
1921 mon_fdset->id = fdset_id;
1922 } else {
1923 mon_fdset->id = fdset_id_prev + 1;
1926 /* The fdset list is ordered by fdset ID */
1927 if (!mon_fdset_cur) {
1928 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1929 } else if (mon_fdset->id < mon_fdset_cur->id) {
1930 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1931 } else {
1932 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1936 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1937 mon_fdset_fd->fd = fd;
1938 mon_fdset_fd->removed = false;
1939 if (has_opaque) {
1940 mon_fdset_fd->opaque = g_strdup(opaque);
1942 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1944 fdinfo = g_malloc0(sizeof(*fdinfo));
1945 fdinfo->fdset_id = mon_fdset->id;
1946 fdinfo->fd = mon_fdset_fd->fd;
1948 return fdinfo;
1951 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1953 #ifndef _WIN32
1954 MonFdset *mon_fdset;
1955 MonFdsetFd *mon_fdset_fd;
1956 int mon_fd_flags;
1958 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1959 if (mon_fdset->id != fdset_id) {
1960 continue;
1962 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1963 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1964 if (mon_fd_flags == -1) {
1965 return -1;
1968 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1969 return mon_fdset_fd->fd;
1972 errno = EACCES;
1973 return -1;
1975 #endif
1977 errno = ENOENT;
1978 return -1;
1981 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1983 MonFdset *mon_fdset;
1984 MonFdsetFd *mon_fdset_fd_dup;
1986 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1987 if (mon_fdset->id != fdset_id) {
1988 continue;
1990 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
1991 if (mon_fdset_fd_dup->fd == dup_fd) {
1992 return -1;
1995 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
1996 mon_fdset_fd_dup->fd = dup_fd;
1997 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
1998 return 0;
2000 return -1;
2003 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2005 MonFdset *mon_fdset;
2006 MonFdsetFd *mon_fdset_fd_dup;
2008 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2009 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2010 if (mon_fdset_fd_dup->fd == dup_fd) {
2011 if (remove) {
2012 QLIST_REMOVE(mon_fdset_fd_dup, next);
2013 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2014 monitor_fdset_cleanup(mon_fdset);
2016 return -1;
2017 } else {
2018 return mon_fdset->id;
2023 return -1;
2026 int monitor_fdset_dup_fd_find(int dup_fd)
2028 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2031 void monitor_fdset_dup_fd_remove(int dup_fd)
2033 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2036 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2038 int fd;
2039 Error *local_err = NULL;
2041 if (!qemu_isdigit(fdname[0]) && mon) {
2042 fd = monitor_get_fd(mon, fdname, &local_err);
2043 } else {
2044 fd = qemu_parse_fd(fdname);
2045 if (fd == -1) {
2046 error_setg(&local_err, "Invalid file descriptor number '%s'",
2047 fdname);
2050 if (local_err) {
2051 error_propagate(errp, local_err);
2052 assert(fd == -1);
2053 } else {
2054 assert(fd != -1);
2057 return fd;
2060 /* Please update hmp-commands.hx when adding or changing commands */
2061 static mon_cmd_t info_cmds[] = {
2062 #include "hmp-commands-info.h"
2063 { NULL, NULL, },
2066 /* mon_cmds and info_cmds would be sorted at runtime */
2067 static mon_cmd_t mon_cmds[] = {
2068 #include "hmp-commands.h"
2069 { NULL, NULL, },
2072 static const mon_cmd_t qmp_cmds[] = {
2073 #include "qmp-commands-old.h"
2074 { /* NULL */ },
2077 /*******************************************************************/
2079 static const char *pch;
2080 static sigjmp_buf expr_env;
2083 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2084 expr_error(Monitor *mon, const char *fmt, ...)
2086 va_list ap;
2087 va_start(ap, fmt);
2088 monitor_vprintf(mon, fmt, ap);
2089 monitor_printf(mon, "\n");
2090 va_end(ap);
2091 siglongjmp(expr_env, 1);
2094 /* return 0 if OK, -1 if not found */
2095 static int get_monitor_def(target_long *pval, const char *name)
2097 const MonitorDef *md = target_monitor_defs();
2098 void *ptr;
2100 if (md == NULL) {
2101 return -1;
2104 for(; md->name != NULL; md++) {
2105 if (compare_cmd(name, md->name)) {
2106 if (md->get_value) {
2107 *pval = md->get_value(md, md->offset);
2108 } else {
2109 CPUArchState *env = mon_get_cpu_env();
2110 ptr = (uint8_t *)env + md->offset;
2111 switch(md->type) {
2112 case MD_I32:
2113 *pval = *(int32_t *)ptr;
2114 break;
2115 case MD_TLONG:
2116 *pval = *(target_long *)ptr;
2117 break;
2118 default:
2119 *pval = 0;
2120 break;
2123 return 0;
2126 return -1;
2129 static void next(void)
2131 if (*pch != '\0') {
2132 pch++;
2133 while (qemu_isspace(*pch))
2134 pch++;
2138 static int64_t expr_sum(Monitor *mon);
2140 static int64_t expr_unary(Monitor *mon)
2142 int64_t n;
2143 char *p;
2144 int ret;
2146 switch(*pch) {
2147 case '+':
2148 next();
2149 n = expr_unary(mon);
2150 break;
2151 case '-':
2152 next();
2153 n = -expr_unary(mon);
2154 break;
2155 case '~':
2156 next();
2157 n = ~expr_unary(mon);
2158 break;
2159 case '(':
2160 next();
2161 n = expr_sum(mon);
2162 if (*pch != ')') {
2163 expr_error(mon, "')' expected");
2165 next();
2166 break;
2167 case '\'':
2168 pch++;
2169 if (*pch == '\0')
2170 expr_error(mon, "character constant expected");
2171 n = *pch;
2172 pch++;
2173 if (*pch != '\'')
2174 expr_error(mon, "missing terminating \' character");
2175 next();
2176 break;
2177 case '$':
2179 char buf[128], *q;
2180 target_long reg=0;
2182 pch++;
2183 q = buf;
2184 while ((*pch >= 'a' && *pch <= 'z') ||
2185 (*pch >= 'A' && *pch <= 'Z') ||
2186 (*pch >= '0' && *pch <= '9') ||
2187 *pch == '_' || *pch == '.') {
2188 if ((q - buf) < sizeof(buf) - 1)
2189 *q++ = *pch;
2190 pch++;
2192 while (qemu_isspace(*pch))
2193 pch++;
2194 *q = 0;
2195 ret = get_monitor_def(&reg, buf);
2196 if (ret < 0)
2197 expr_error(mon, "unknown register");
2198 n = reg;
2200 break;
2201 case '\0':
2202 expr_error(mon, "unexpected end of expression");
2203 n = 0;
2204 break;
2205 default:
2206 errno = 0;
2207 n = strtoull(pch, &p, 0);
2208 if (errno == ERANGE) {
2209 expr_error(mon, "number too large");
2211 if (pch == p) {
2212 expr_error(mon, "invalid char '%c' in expression", *p);
2214 pch = p;
2215 while (qemu_isspace(*pch))
2216 pch++;
2217 break;
2219 return n;
2223 static int64_t expr_prod(Monitor *mon)
2225 int64_t val, val2;
2226 int op;
2228 val = expr_unary(mon);
2229 for(;;) {
2230 op = *pch;
2231 if (op != '*' && op != '/' && op != '%')
2232 break;
2233 next();
2234 val2 = expr_unary(mon);
2235 switch(op) {
2236 default:
2237 case '*':
2238 val *= val2;
2239 break;
2240 case '/':
2241 case '%':
2242 if (val2 == 0)
2243 expr_error(mon, "division by zero");
2244 if (op == '/')
2245 val /= val2;
2246 else
2247 val %= val2;
2248 break;
2251 return val;
2254 static int64_t expr_logic(Monitor *mon)
2256 int64_t val, val2;
2257 int op;
2259 val = expr_prod(mon);
2260 for(;;) {
2261 op = *pch;
2262 if (op != '&' && op != '|' && op != '^')
2263 break;
2264 next();
2265 val2 = expr_prod(mon);
2266 switch(op) {
2267 default:
2268 case '&':
2269 val &= val2;
2270 break;
2271 case '|':
2272 val |= val2;
2273 break;
2274 case '^':
2275 val ^= val2;
2276 break;
2279 return val;
2282 static int64_t expr_sum(Monitor *mon)
2284 int64_t val, val2;
2285 int op;
2287 val = expr_logic(mon);
2288 for(;;) {
2289 op = *pch;
2290 if (op != '+' && op != '-')
2291 break;
2292 next();
2293 val2 = expr_logic(mon);
2294 if (op == '+')
2295 val += val2;
2296 else
2297 val -= val2;
2299 return val;
2302 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2304 pch = *pp;
2305 if (sigsetjmp(expr_env, 0)) {
2306 *pp = pch;
2307 return -1;
2309 while (qemu_isspace(*pch))
2310 pch++;
2311 *pval = expr_sum(mon);
2312 *pp = pch;
2313 return 0;
2316 static int get_double(Monitor *mon, double *pval, const char **pp)
2318 const char *p = *pp;
2319 char *tailp;
2320 double d;
2322 d = strtod(p, &tailp);
2323 if (tailp == p) {
2324 monitor_printf(mon, "Number expected\n");
2325 return -1;
2327 if (d != d || d - d != 0) {
2328 /* NaN or infinity */
2329 monitor_printf(mon, "Bad number\n");
2330 return -1;
2332 *pval = d;
2333 *pp = tailp;
2334 return 0;
2338 * Store the command-name in cmdname, and return a pointer to
2339 * the remaining of the command string.
2341 static const char *get_command_name(const char *cmdline,
2342 char *cmdname, size_t nlen)
2344 size_t len;
2345 const char *p, *pstart;
2347 p = cmdline;
2348 while (qemu_isspace(*p))
2349 p++;
2350 if (*p == '\0')
2351 return NULL;
2352 pstart = p;
2353 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2354 p++;
2355 len = p - pstart;
2356 if (len > nlen - 1)
2357 len = nlen - 1;
2358 memcpy(cmdname, pstart, len);
2359 cmdname[len] = '\0';
2360 return p;
2364 * Read key of 'type' into 'key' and return the current
2365 * 'type' pointer.
2367 static char *key_get_info(const char *type, char **key)
2369 size_t len;
2370 char *p, *str;
2372 if (*type == ',')
2373 type++;
2375 p = strchr(type, ':');
2376 if (!p) {
2377 *key = NULL;
2378 return NULL;
2380 len = p - type;
2382 str = g_malloc(len + 1);
2383 memcpy(str, type, len);
2384 str[len] = '\0';
2386 *key = str;
2387 return ++p;
2390 static int default_fmt_format = 'x';
2391 static int default_fmt_size = 4;
2393 static int is_valid_option(const char *c, const char *typestr)
2395 char option[3];
2397 option[0] = '-';
2398 option[1] = *c;
2399 option[2] = '\0';
2401 typestr = strstr(typestr, option);
2402 return (typestr != NULL);
2405 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2406 const char *cmdname)
2408 const mon_cmd_t *cmd;
2410 for (cmd = disp_table; cmd->name != NULL; cmd++) {
2411 if (compare_cmd(cmdname, cmd->name)) {
2412 return cmd;
2416 return NULL;
2419 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2421 return search_dispatch_table(qmp_cmds, cmdname);
2425 * Parse command name from @cmdp according to command table @table.
2426 * If blank, return NULL.
2427 * Else, if no valid command can be found, report to @mon, and return
2428 * NULL.
2429 * Else, change @cmdp to point right behind the name, and return its
2430 * command table entry.
2431 * Do not assume the return value points into @table! It doesn't when
2432 * the command is found in a sub-command table.
2434 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2435 const char **cmdp,
2436 mon_cmd_t *table)
2438 const char *p;
2439 const mon_cmd_t *cmd;
2440 char cmdname[256];
2442 /* extract the command name */
2443 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
2444 if (!p)
2445 return NULL;
2447 cmd = search_dispatch_table(table, cmdname);
2448 if (!cmd) {
2449 monitor_printf(mon, "unknown command: '%.*s'\n",
2450 (int)(p - *cmdp), *cmdp);
2451 return NULL;
2454 /* filter out following useless space */
2455 while (qemu_isspace(*p)) {
2456 p++;
2459 *cmdp = p;
2460 /* search sub command */
2461 if (cmd->sub_table != NULL && *p != '\0') {
2462 return monitor_parse_command(mon, cmdp, cmd->sub_table);
2465 return cmd;
2469 * Parse arguments for @cmd.
2470 * If it can't be parsed, report to @mon, and return NULL.
2471 * Else, insert command arguments into a QDict, and return it.
2472 * Note: On success, caller has to free the QDict structure.
2475 static QDict *monitor_parse_arguments(Monitor *mon,
2476 const char **endp,
2477 const mon_cmd_t *cmd)
2479 const char *typestr;
2480 char *key;
2481 int c;
2482 const char *p = *endp;
2483 char buf[1024];
2484 QDict *qdict = qdict_new();
2486 /* parse the parameters */
2487 typestr = cmd->args_type;
2488 for(;;) {
2489 typestr = key_get_info(typestr, &key);
2490 if (!typestr)
2491 break;
2492 c = *typestr;
2493 typestr++;
2494 switch(c) {
2495 case 'F':
2496 case 'B':
2497 case 's':
2499 int ret;
2501 while (qemu_isspace(*p))
2502 p++;
2503 if (*typestr == '?') {
2504 typestr++;
2505 if (*p == '\0') {
2506 /* no optional string: NULL argument */
2507 break;
2510 ret = get_str(buf, sizeof(buf), &p);
2511 if (ret < 0) {
2512 switch(c) {
2513 case 'F':
2514 monitor_printf(mon, "%s: filename expected\n",
2515 cmd->name);
2516 break;
2517 case 'B':
2518 monitor_printf(mon, "%s: block device name expected\n",
2519 cmd->name);
2520 break;
2521 default:
2522 monitor_printf(mon, "%s: string expected\n", cmd->name);
2523 break;
2525 goto fail;
2527 qdict_put(qdict, key, qstring_from_str(buf));
2529 break;
2530 case 'O':
2532 QemuOptsList *opts_list;
2533 QemuOpts *opts;
2535 opts_list = qemu_find_opts(key);
2536 if (!opts_list || opts_list->desc->name) {
2537 goto bad_type;
2539 while (qemu_isspace(*p)) {
2540 p++;
2542 if (!*p)
2543 break;
2544 if (get_str(buf, sizeof(buf), &p) < 0) {
2545 goto fail;
2547 opts = qemu_opts_parse_noisily(opts_list, buf, true);
2548 if (!opts) {
2549 goto fail;
2551 qemu_opts_to_qdict(opts, qdict);
2552 qemu_opts_del(opts);
2554 break;
2555 case '/':
2557 int count, format, size;
2559 while (qemu_isspace(*p))
2560 p++;
2561 if (*p == '/') {
2562 /* format found */
2563 p++;
2564 count = 1;
2565 if (qemu_isdigit(*p)) {
2566 count = 0;
2567 while (qemu_isdigit(*p)) {
2568 count = count * 10 + (*p - '0');
2569 p++;
2572 size = -1;
2573 format = -1;
2574 for(;;) {
2575 switch(*p) {
2576 case 'o':
2577 case 'd':
2578 case 'u':
2579 case 'x':
2580 case 'i':
2581 case 'c':
2582 format = *p++;
2583 break;
2584 case 'b':
2585 size = 1;
2586 p++;
2587 break;
2588 case 'h':
2589 size = 2;
2590 p++;
2591 break;
2592 case 'w':
2593 size = 4;
2594 p++;
2595 break;
2596 case 'g':
2597 case 'L':
2598 size = 8;
2599 p++;
2600 break;
2601 default:
2602 goto next;
2605 next:
2606 if (*p != '\0' && !qemu_isspace(*p)) {
2607 monitor_printf(mon, "invalid char in format: '%c'\n",
2608 *p);
2609 goto fail;
2611 if (format < 0)
2612 format = default_fmt_format;
2613 if (format != 'i') {
2614 /* for 'i', not specifying a size gives -1 as size */
2615 if (size < 0)
2616 size = default_fmt_size;
2617 default_fmt_size = size;
2619 default_fmt_format = format;
2620 } else {
2621 count = 1;
2622 format = default_fmt_format;
2623 if (format != 'i') {
2624 size = default_fmt_size;
2625 } else {
2626 size = -1;
2629 qdict_put(qdict, "count", qint_from_int(count));
2630 qdict_put(qdict, "format", qint_from_int(format));
2631 qdict_put(qdict, "size", qint_from_int(size));
2633 break;
2634 case 'i':
2635 case 'l':
2636 case 'M':
2638 int64_t val;
2640 while (qemu_isspace(*p))
2641 p++;
2642 if (*typestr == '?' || *typestr == '.') {
2643 if (*typestr == '?') {
2644 if (*p == '\0') {
2645 typestr++;
2646 break;
2648 } else {
2649 if (*p == '.') {
2650 p++;
2651 while (qemu_isspace(*p))
2652 p++;
2653 } else {
2654 typestr++;
2655 break;
2658 typestr++;
2660 if (get_expr(mon, &val, &p))
2661 goto fail;
2662 /* Check if 'i' is greater than 32-bit */
2663 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2664 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
2665 monitor_printf(mon, "integer is for 32-bit values\n");
2666 goto fail;
2667 } else if (c == 'M') {
2668 if (val < 0) {
2669 monitor_printf(mon, "enter a positive value\n");
2670 goto fail;
2672 val <<= 20;
2674 qdict_put(qdict, key, qint_from_int(val));
2676 break;
2677 case 'o':
2679 int64_t val;
2680 char *end;
2682 while (qemu_isspace(*p)) {
2683 p++;
2685 if (*typestr == '?') {
2686 typestr++;
2687 if (*p == '\0') {
2688 break;
2691 val = qemu_strtosz(p, &end);
2692 if (val < 0) {
2693 monitor_printf(mon, "invalid size\n");
2694 goto fail;
2696 qdict_put(qdict, key, qint_from_int(val));
2697 p = end;
2699 break;
2700 case 'T':
2702 double val;
2704 while (qemu_isspace(*p))
2705 p++;
2706 if (*typestr == '?') {
2707 typestr++;
2708 if (*p == '\0') {
2709 break;
2712 if (get_double(mon, &val, &p) < 0) {
2713 goto fail;
2715 if (p[0] && p[1] == 's') {
2716 switch (*p) {
2717 case 'm':
2718 val /= 1e3; p += 2; break;
2719 case 'u':
2720 val /= 1e6; p += 2; break;
2721 case 'n':
2722 val /= 1e9; p += 2; break;
2725 if (*p && !qemu_isspace(*p)) {
2726 monitor_printf(mon, "Unknown unit suffix\n");
2727 goto fail;
2729 qdict_put(qdict, key, qfloat_from_double(val));
2731 break;
2732 case 'b':
2734 const char *beg;
2735 bool val;
2737 while (qemu_isspace(*p)) {
2738 p++;
2740 beg = p;
2741 while (qemu_isgraph(*p)) {
2742 p++;
2744 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
2745 val = true;
2746 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
2747 val = false;
2748 } else {
2749 monitor_printf(mon, "Expected 'on' or 'off'\n");
2750 goto fail;
2752 qdict_put(qdict, key, qbool_from_bool(val));
2754 break;
2755 case '-':
2757 const char *tmp = p;
2758 int skip_key = 0;
2759 /* option */
2761 c = *typestr++;
2762 if (c == '\0')
2763 goto bad_type;
2764 while (qemu_isspace(*p))
2765 p++;
2766 if (*p == '-') {
2767 p++;
2768 if(c != *p) {
2769 if(!is_valid_option(p, typestr)) {
2771 monitor_printf(mon, "%s: unsupported option -%c\n",
2772 cmd->name, *p);
2773 goto fail;
2774 } else {
2775 skip_key = 1;
2778 if(skip_key) {
2779 p = tmp;
2780 } else {
2781 /* has option */
2782 p++;
2783 qdict_put(qdict, key, qbool_from_bool(true));
2787 break;
2788 case 'S':
2790 /* package all remaining string */
2791 int len;
2793 while (qemu_isspace(*p)) {
2794 p++;
2796 if (*typestr == '?') {
2797 typestr++;
2798 if (*p == '\0') {
2799 /* no remaining string: NULL argument */
2800 break;
2803 len = strlen(p);
2804 if (len <= 0) {
2805 monitor_printf(mon, "%s: string expected\n",
2806 cmd->name);
2807 goto fail;
2809 qdict_put(qdict, key, qstring_from_str(p));
2810 p += len;
2812 break;
2813 default:
2814 bad_type:
2815 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
2816 goto fail;
2818 g_free(key);
2819 key = NULL;
2821 /* check that all arguments were parsed */
2822 while (qemu_isspace(*p))
2823 p++;
2824 if (*p != '\0') {
2825 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2826 cmd->name);
2827 goto fail;
2830 return qdict;
2832 fail:
2833 QDECREF(qdict);
2834 g_free(key);
2835 return NULL;
2838 static void handle_hmp_command(Monitor *mon, const char *cmdline)
2840 QDict *qdict;
2841 const mon_cmd_t *cmd;
2843 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2844 if (!cmd) {
2845 return;
2848 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2849 if (!qdict) {
2850 monitor_printf(mon, "Try \"help %s\" for more information\n",
2851 cmd->name);
2852 return;
2855 cmd->mhandler.cmd(mon, qdict);
2856 QDECREF(qdict);
2859 static void cmd_completion(Monitor *mon, const char *name, const char *list)
2861 const char *p, *pstart;
2862 char cmd[128];
2863 int len;
2865 p = list;
2866 for(;;) {
2867 pstart = p;
2868 p = strchr(p, '|');
2869 if (!p)
2870 p = pstart + strlen(pstart);
2871 len = p - pstart;
2872 if (len > sizeof(cmd) - 2)
2873 len = sizeof(cmd) - 2;
2874 memcpy(cmd, pstart, len);
2875 cmd[len] = '\0';
2876 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2877 readline_add_completion(mon->rs, cmd);
2879 if (*p == '\0')
2880 break;
2881 p++;
2885 static void file_completion(Monitor *mon, const char *input)
2887 DIR *ffs;
2888 struct dirent *d;
2889 char path[1024];
2890 char file[1024], file_prefix[1024];
2891 int input_path_len;
2892 const char *p;
2894 p = strrchr(input, '/');
2895 if (!p) {
2896 input_path_len = 0;
2897 pstrcpy(file_prefix, sizeof(file_prefix), input);
2898 pstrcpy(path, sizeof(path), ".");
2899 } else {
2900 input_path_len = p - input + 1;
2901 memcpy(path, input, input_path_len);
2902 if (input_path_len > sizeof(path) - 1)
2903 input_path_len = sizeof(path) - 1;
2904 path[input_path_len] = '\0';
2905 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2908 ffs = opendir(path);
2909 if (!ffs)
2910 return;
2911 for(;;) {
2912 struct stat sb;
2913 d = readdir(ffs);
2914 if (!d)
2915 break;
2917 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2918 continue;
2921 if (strstart(d->d_name, file_prefix, NULL)) {
2922 memcpy(file, input, input_path_len);
2923 if (input_path_len < sizeof(file))
2924 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2925 d->d_name);
2926 /* stat the file to find out if it's a directory.
2927 * In that case add a slash to speed up typing long paths
2929 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
2930 pstrcat(file, sizeof(file), "/");
2932 readline_add_completion(mon->rs, file);
2935 closedir(ffs);
2938 static const char *next_arg_type(const char *typestr)
2940 const char *p = strchr(typestr, ':');
2941 return (p != NULL ? ++p : typestr);
2944 static void add_completion_option(ReadLineState *rs, const char *str,
2945 const char *option)
2947 if (!str || !option) {
2948 return;
2950 if (!strncmp(option, str, strlen(str))) {
2951 readline_add_completion(rs, option);
2955 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2957 size_t len;
2958 ChardevBackendInfoList *list, *start;
2960 if (nb_args != 2) {
2961 return;
2963 len = strlen(str);
2964 readline_set_completion_index(rs, len);
2966 start = list = qmp_query_chardev_backends(NULL);
2967 while (list) {
2968 const char *chr_name = list->value->name;
2970 if (!strncmp(chr_name, str, len)) {
2971 readline_add_completion(rs, chr_name);
2973 list = list->next;
2975 qapi_free_ChardevBackendInfoList(start);
2978 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2980 size_t len;
2981 int i;
2983 if (nb_args != 2) {
2984 return;
2986 len = strlen(str);
2987 readline_set_completion_index(rs, len);
2988 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2989 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
2993 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
2995 GSList *list, *elt;
2996 size_t len;
2998 if (nb_args != 2) {
2999 return;
3002 len = strlen(str);
3003 readline_set_completion_index(rs, len);
3004 list = elt = object_class_get_list(TYPE_DEVICE, false);
3005 while (elt) {
3006 const char *name;
3007 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3008 TYPE_DEVICE);
3009 name = object_class_get_name(OBJECT_CLASS(dc));
3011 if (!dc->cannot_instantiate_with_device_add_yet
3012 && !strncmp(name, str, len)) {
3013 readline_add_completion(rs, name);
3015 elt = elt->next;
3017 g_slist_free(list);
3020 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
3022 GSList *list, *elt;
3023 size_t len;
3025 if (nb_args != 2) {
3026 return;
3029 len = strlen(str);
3030 readline_set_completion_index(rs, len);
3031 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3032 while (elt) {
3033 const char *name;
3035 name = object_class_get_name(OBJECT_CLASS(elt->data));
3036 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3037 readline_add_completion(rs, name);
3039 elt = elt->next;
3041 g_slist_free(list);
3044 static void peripheral_device_del_completion(ReadLineState *rs,
3045 const char *str, size_t len)
3047 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3048 GSList *list, *item;
3050 list = qdev_build_hotpluggable_device_list(peripheral);
3051 if (!list) {
3052 return;
3055 for (item = list; item; item = g_slist_next(item)) {
3056 DeviceState *dev = item->data;
3058 if (dev->id && !strncmp(str, dev->id, len)) {
3059 readline_add_completion(rs, dev->id);
3063 g_slist_free(list);
3066 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3068 size_t len;
3069 ChardevInfoList *list, *start;
3071 if (nb_args != 2) {
3072 return;
3074 len = strlen(str);
3075 readline_set_completion_index(rs, len);
3077 start = list = qmp_query_chardev(NULL);
3078 while (list) {
3079 ChardevInfo *chr = list->value;
3081 if (!strncmp(chr->label, str, len)) {
3082 readline_add_completion(rs, chr->label);
3084 list = list->next;
3086 qapi_free_ChardevInfoList(start);
3089 static void ringbuf_completion(ReadLineState *rs, const char *str)
3091 size_t len;
3092 ChardevInfoList *list, *start;
3094 len = strlen(str);
3095 readline_set_completion_index(rs, len);
3097 start = list = qmp_query_chardev(NULL);
3098 while (list) {
3099 ChardevInfo *chr_info = list->value;
3101 if (!strncmp(chr_info->label, str, len)) {
3102 CharDriverState *chr = qemu_chr_find(chr_info->label);
3103 if (chr && chr_is_ringbuf(chr)) {
3104 readline_add_completion(rs, chr_info->label);
3107 list = list->next;
3109 qapi_free_ChardevInfoList(start);
3112 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3114 if (nb_args != 2) {
3115 return;
3117 ringbuf_completion(rs, str);
3120 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3122 size_t len;
3124 if (nb_args != 2) {
3125 return;
3128 len = strlen(str);
3129 readline_set_completion_index(rs, len);
3130 peripheral_device_del_completion(rs, str, len);
3133 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
3135 ObjectPropertyInfoList *list, *start;
3136 size_t len;
3138 if (nb_args != 2) {
3139 return;
3141 len = strlen(str);
3142 readline_set_completion_index(rs, len);
3144 start = list = qmp_qom_list("/objects", NULL);
3145 while (list) {
3146 ObjectPropertyInfo *info = list->value;
3148 if (!strncmp(info->type, "child<", 5)
3149 && !strncmp(info->name, str, len)) {
3150 readline_add_completion(rs, info->name);
3152 list = list->next;
3154 qapi_free_ObjectPropertyInfoList(start);
3157 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3159 int i;
3160 char *sep;
3161 size_t len;
3163 if (nb_args != 2) {
3164 return;
3166 sep = strrchr(str, '-');
3167 if (sep) {
3168 str = sep + 1;
3170 len = strlen(str);
3171 readline_set_completion_index(rs, len);
3172 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3173 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3174 readline_add_completion(rs, QKeyCode_lookup[i]);
3179 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3181 size_t len;
3183 len = strlen(str);
3184 readline_set_completion_index(rs, len);
3185 if (nb_args == 2) {
3186 NetClientState *ncs[MAX_QUEUE_NUM];
3187 int count, i;
3188 count = qemu_find_net_clients_except(NULL, ncs,
3189 NET_CLIENT_OPTIONS_KIND_NONE,
3190 MAX_QUEUE_NUM);
3191 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3192 const char *name = ncs[i]->name;
3193 if (!strncmp(str, name, len)) {
3194 readline_add_completion(rs, name);
3197 } else if (nb_args == 3) {
3198 add_completion_option(rs, str, "on");
3199 add_completion_option(rs, str, "off");
3203 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3205 int len, count, i;
3206 NetClientState *ncs[MAX_QUEUE_NUM];
3208 if (nb_args != 2) {
3209 return;
3212 len = strlen(str);
3213 readline_set_completion_index(rs, len);
3214 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
3215 MAX_QUEUE_NUM);
3216 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3217 QemuOpts *opts;
3218 const char *name = ncs[i]->name;
3219 if (strncmp(str, name, len)) {
3220 continue;
3222 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3223 if (opts) {
3224 readline_add_completion(rs, name);
3229 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3231 size_t len;
3233 len = strlen(str);
3234 readline_set_completion_index(rs, len);
3235 if (nb_args == 2) {
3236 TraceEventID id;
3237 for (id = 0; id < trace_event_count(); id++) {
3238 const char *event_name = trace_event_get_name(trace_event_id(id));
3239 if (!strncmp(str, event_name, len)) {
3240 readline_add_completion(rs, event_name);
3243 } else if (nb_args == 3) {
3244 add_completion_option(rs, str, "on");
3245 add_completion_option(rs, str, "off");
3249 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3251 int i;
3253 if (nb_args != 2) {
3254 return;
3256 readline_set_completion_index(rs, strlen(str));
3257 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3258 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3262 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3263 const char *str)
3265 size_t len;
3267 len = strlen(str);
3268 readline_set_completion_index(rs, len);
3269 if (nb_args == 2) {
3270 int i;
3271 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3272 const char *name = MigrationCapability_lookup[i];
3273 if (!strncmp(str, name, len)) {
3274 readline_add_completion(rs, name);
3277 } else if (nb_args == 3) {
3278 add_completion_option(rs, str, "on");
3279 add_completion_option(rs, str, "off");
3283 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3284 const char *str)
3286 size_t len;
3288 len = strlen(str);
3289 readline_set_completion_index(rs, len);
3290 if (nb_args == 2) {
3291 int i;
3292 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3293 const char *name = MigrationParameter_lookup[i];
3294 if (!strncmp(str, name, len)) {
3295 readline_add_completion(rs, name);
3301 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3303 int i;
3304 size_t len;
3305 if (nb_args != 2) {
3306 return;
3308 len = strlen(str);
3309 readline_set_completion_index(rs, len);
3310 for (i = 0; host_net_devices[i]; i++) {
3311 if (!strncmp(host_net_devices[i], str, len)) {
3312 readline_add_completion(rs, host_net_devices[i]);
3317 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3319 NetClientState *ncs[MAX_QUEUE_NUM];
3320 int count, i, len;
3322 len = strlen(str);
3323 readline_set_completion_index(rs, len);
3324 if (nb_args == 2) {
3325 count = qemu_find_net_clients_except(NULL, ncs,
3326 NET_CLIENT_OPTIONS_KIND_NONE,
3327 MAX_QUEUE_NUM);
3328 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3329 int id;
3330 char name[16];
3332 if (net_hub_id_for_client(ncs[i], &id)) {
3333 continue;
3335 snprintf(name, sizeof(name), "%d", id);
3336 if (!strncmp(str, name, len)) {
3337 readline_add_completion(rs, name);
3340 return;
3341 } else if (nb_args == 3) {
3342 count = qemu_find_net_clients_except(NULL, ncs,
3343 NET_CLIENT_OPTIONS_KIND_NIC,
3344 MAX_QUEUE_NUM);
3345 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3346 int id;
3347 const char *name;
3349 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3350 net_hub_id_for_client(ncs[i], &id)) {
3351 continue;
3353 name = ncs[i]->name;
3354 if (!strncmp(str, name, len)) {
3355 readline_add_completion(rs, name);
3358 return;
3362 static void vm_completion(ReadLineState *rs, const char *str)
3364 size_t len;
3365 BlockDriverState *bs = NULL;
3367 len = strlen(str);
3368 readline_set_completion_index(rs, len);
3369 while ((bs = bdrv_next(bs))) {
3370 SnapshotInfoList *snapshots, *snapshot;
3372 if (!bdrv_can_snapshot(bs)) {
3373 continue;
3375 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3376 continue;
3378 snapshot = snapshots;
3379 while (snapshot) {
3380 char *completion = snapshot->value->name;
3381 if (!strncmp(str, completion, len)) {
3382 readline_add_completion(rs, completion);
3384 completion = snapshot->value->id;
3385 if (!strncmp(str, completion, len)) {
3386 readline_add_completion(rs, completion);
3388 snapshot = snapshot->next;
3390 qapi_free_SnapshotInfoList(snapshots);
3395 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3397 if (nb_args == 2) {
3398 vm_completion(rs, str);
3402 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3404 if (nb_args == 2) {
3405 vm_completion(rs, str);
3409 static void monitor_find_completion_by_table(Monitor *mon,
3410 const mon_cmd_t *cmd_table,
3411 char **args,
3412 int nb_args)
3414 const char *cmdname;
3415 int i;
3416 const char *ptype, *str, *name;
3417 const mon_cmd_t *cmd;
3418 BlockDriverState *bs;
3420 if (nb_args <= 1) {
3421 /* command completion */
3422 if (nb_args == 0)
3423 cmdname = "";
3424 else
3425 cmdname = args[0];
3426 readline_set_completion_index(mon->rs, strlen(cmdname));
3427 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3428 cmd_completion(mon, cmdname, cmd->name);
3430 } else {
3431 /* find the command */
3432 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3433 if (compare_cmd(args[0], cmd->name)) {
3434 break;
3437 if (!cmd->name) {
3438 return;
3441 if (cmd->sub_table) {
3442 /* do the job again */
3443 monitor_find_completion_by_table(mon, cmd->sub_table,
3444 &args[1], nb_args - 1);
3445 return;
3447 if (cmd->command_completion) {
3448 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3449 return;
3452 ptype = next_arg_type(cmd->args_type);
3453 for(i = 0; i < nb_args - 2; i++) {
3454 if (*ptype != '\0') {
3455 ptype = next_arg_type(ptype);
3456 while (*ptype == '?')
3457 ptype = next_arg_type(ptype);
3460 str = args[nb_args - 1];
3461 while (*ptype == '-' && ptype[1] != '\0') {
3462 ptype = next_arg_type(ptype);
3464 switch(*ptype) {
3465 case 'F':
3466 /* file completion */
3467 readline_set_completion_index(mon->rs, strlen(str));
3468 file_completion(mon, str);
3469 break;
3470 case 'B':
3471 /* block device name completion */
3472 readline_set_completion_index(mon->rs, strlen(str));
3473 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3474 name = bdrv_get_device_name(bs);
3475 if (str[0] == '\0' ||
3476 !strncmp(name, str, strlen(str))) {
3477 readline_add_completion(mon->rs, name);
3480 break;
3481 case 's':
3482 case 'S':
3483 if (!strcmp(cmd->name, "help|?")) {
3484 monitor_find_completion_by_table(mon, cmd_table,
3485 &args[1], nb_args - 1);
3487 break;
3488 default:
3489 break;
3494 static void monitor_find_completion(void *opaque,
3495 const char *cmdline)
3497 Monitor *mon = opaque;
3498 char *args[MAX_ARGS];
3499 int nb_args, len;
3501 /* 1. parse the cmdline */
3502 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3503 return;
3506 /* if the line ends with a space, it means we want to complete the
3507 next arg */
3508 len = strlen(cmdline);
3509 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3510 if (nb_args >= MAX_ARGS) {
3511 goto cleanup;
3513 args[nb_args++] = g_strdup("");
3516 /* 2. auto complete according to args */
3517 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
3519 cleanup:
3520 free_cmdline_args(args, nb_args);
3523 static int monitor_can_read(void *opaque)
3525 Monitor *mon = opaque;
3527 return (mon->suspend_cnt == 0) ? 1 : 0;
3530 static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3531 Error **errp)
3533 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
3535 if (is_cap && mon->qmp.in_command_mode) {
3536 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3537 "Capabilities negotiation is already complete, command "
3538 "'%s' ignored", cmd->name);
3539 return true;
3541 if (!is_cap && !mon->qmp.in_command_mode) {
3542 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3543 "Expecting capabilities negotiation with "
3544 "'qmp_capabilities' before command '%s'", cmd->name);
3545 return true;
3547 return false;
3551 * Argument validation rules:
3553 * 1. The argument must exist in cmd_args qdict
3554 * 2. The argument type must be the expected one
3556 * Special case: If the argument doesn't exist in cmd_args and
3557 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3558 * checking is skipped for it.
3560 static void check_client_args_type(const QDict *client_args,
3561 const QDict *cmd_args, int flags,
3562 Error **errp)
3564 const QDictEntry *ent;
3566 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3567 QObject *obj;
3568 QString *arg_type;
3569 const QObject *client_arg = qdict_entry_value(ent);
3570 const char *client_arg_name = qdict_entry_key(ent);
3572 obj = qdict_get(cmd_args, client_arg_name);
3573 if (!obj) {
3574 if (flags & QMP_ACCEPT_UNKNOWNS) {
3575 /* handler accepts unknowns */
3576 continue;
3578 /* client arg doesn't exist */
3579 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
3580 return;
3583 arg_type = qobject_to_qstring(obj);
3584 assert(arg_type != NULL);
3586 /* check if argument's type is correct */
3587 switch (qstring_get_str(arg_type)[0]) {
3588 case 'F':
3589 case 'B':
3590 case 's':
3591 if (qobject_type(client_arg) != QTYPE_QSTRING) {
3592 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3593 client_arg_name, "string");
3594 return;
3596 break;
3597 case 'i':
3598 case 'l':
3599 case 'M':
3600 case 'o':
3601 if (qobject_type(client_arg) != QTYPE_QINT) {
3602 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3603 client_arg_name, "int");
3604 return;
3606 break;
3607 case 'T':
3608 if (qobject_type(client_arg) != QTYPE_QINT &&
3609 qobject_type(client_arg) != QTYPE_QFLOAT) {
3610 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3611 client_arg_name, "number");
3612 return;
3614 break;
3615 case 'b':
3616 case '-':
3617 if (qobject_type(client_arg) != QTYPE_QBOOL) {
3618 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3619 client_arg_name, "bool");
3620 return;
3622 break;
3623 case 'O':
3624 assert(flags & QMP_ACCEPT_UNKNOWNS);
3625 break;
3626 case 'q':
3627 /* Any QObject can be passed. */
3628 break;
3629 case '/':
3630 case '.':
3632 * These types are not supported by QMP and thus are not
3633 * handled here. Fall through.
3635 default:
3636 abort();
3642 * - Check if the client has passed all mandatory args
3643 * - Set special flags for argument validation
3645 static void check_mandatory_args(const QDict *cmd_args,
3646 const QDict *client_args, int *flags,
3647 Error **errp)
3649 const QDictEntry *ent;
3651 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3652 const char *cmd_arg_name = qdict_entry_key(ent);
3653 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3654 assert(type != NULL);
3656 if (qstring_get_str(type)[0] == 'O') {
3657 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3658 *flags |= QMP_ACCEPT_UNKNOWNS;
3659 } else if (qstring_get_str(type)[0] != '-' &&
3660 qstring_get_str(type)[1] != '?' &&
3661 !qdict_haskey(client_args, cmd_arg_name)) {
3662 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
3663 return;
3668 static QDict *qdict_from_args_type(const char *args_type)
3670 int i;
3671 QDict *qdict;
3672 QString *key, *type, *cur_qs;
3674 assert(args_type != NULL);
3676 qdict = qdict_new();
3678 if (args_type == NULL || args_type[0] == '\0') {
3679 /* no args, empty qdict */
3680 goto out;
3683 key = qstring_new();
3684 type = qstring_new();
3686 cur_qs = key;
3688 for (i = 0;; i++) {
3689 switch (args_type[i]) {
3690 case ',':
3691 case '\0':
3692 qdict_put(qdict, qstring_get_str(key), type);
3693 QDECREF(key);
3694 if (args_type[i] == '\0') {
3695 goto out;
3697 type = qstring_new(); /* qdict has ref */
3698 cur_qs = key = qstring_new();
3699 break;
3700 case ':':
3701 cur_qs = type;
3702 break;
3703 default:
3704 qstring_append_chr(cur_qs, args_type[i]);
3705 break;
3709 out:
3710 return qdict;
3714 * Client argument checking rules:
3716 * 1. Client must provide all mandatory arguments
3717 * 2. Each argument provided by the client must be expected
3718 * 3. Each argument provided by the client must have the type expected
3719 * by the command
3721 static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3722 Error **errp)
3724 Error *err = NULL;
3725 int flags;
3726 QDict *cmd_args;
3728 cmd_args = qdict_from_args_type(cmd->args_type);
3730 flags = 0;
3731 check_mandatory_args(cmd_args, client_args, &flags, &err);
3732 if (err) {
3733 goto out;
3736 check_client_args_type(client_args, cmd_args, flags, &err);
3738 out:
3739 error_propagate(errp, err);
3740 QDECREF(cmd_args);
3744 * Input object checking rules
3746 * 1. Input object must be a dict
3747 * 2. The "execute" key must exist
3748 * 3. The "execute" key must be a string
3749 * 4. If the "arguments" key exists, it must be a dict
3750 * 5. If the "id" key exists, it can be anything (ie. json-value)
3751 * 6. Any argument not listed above is considered invalid
3753 static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
3755 const QDictEntry *ent;
3756 int has_exec_key = 0;
3757 QDict *input_dict;
3759 if (qobject_type(input_obj) != QTYPE_QDICT) {
3760 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
3761 return NULL;
3764 input_dict = qobject_to_qdict(input_obj);
3766 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3767 const char *arg_name = qdict_entry_key(ent);
3768 const QObject *arg_obj = qdict_entry_value(ent);
3770 if (!strcmp(arg_name, "execute")) {
3771 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
3772 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3773 "execute", "string");
3774 return NULL;
3776 has_exec_key = 1;
3777 } else if (!strcmp(arg_name, "arguments")) {
3778 if (qobject_type(arg_obj) != QTYPE_QDICT) {
3779 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3780 "arguments", "object");
3781 return NULL;
3783 } else if (!strcmp(arg_name, "id")) {
3784 /* Any string is acceptable as "id", so nothing to check */
3785 } else {
3786 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
3787 return NULL;
3791 if (!has_exec_key) {
3792 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
3793 return NULL;
3796 return input_dict;
3799 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3801 Error *local_err = NULL;
3802 QObject *obj, *data;
3803 QDict *input, *args;
3804 const mon_cmd_t *cmd;
3805 const char *cmd_name;
3806 Monitor *mon = cur_mon;
3808 args = input = NULL;
3809 data = NULL;
3811 obj = json_parser_parse(tokens, NULL);
3812 if (!obj) {
3813 // FIXME: should be triggered in json_parser_parse()
3814 error_setg(&local_err, QERR_JSON_PARSING);
3815 goto err_out;
3818 input = qmp_check_input_obj(obj, &local_err);
3819 if (!input) {
3820 qobject_decref(obj);
3821 goto err_out;
3824 mon->qmp.id = qdict_get(input, "id");
3825 qobject_incref(mon->qmp.id);
3827 cmd_name = qdict_get_str(input, "execute");
3828 trace_handle_qmp_command(mon, cmd_name);
3829 cmd = qmp_find_cmd(cmd_name);
3830 if (!cmd) {
3831 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3832 "The command %s has not been found", cmd_name);
3833 goto err_out;
3835 if (invalid_qmp_mode(mon, cmd, &local_err)) {
3836 goto err_out;
3839 obj = qdict_get(input, "arguments");
3840 if (!obj) {
3841 args = qdict_new();
3842 } else {
3843 args = qobject_to_qdict(obj);
3844 QINCREF(args);
3847 qmp_check_client_args(cmd, args, &local_err);
3848 if (local_err) {
3849 goto err_out;
3852 cmd->mhandler.cmd_new(args, &data, &local_err);
3854 err_out:
3855 monitor_protocol_emitter(mon, data, local_err);
3856 qobject_decref(data);
3857 QDECREF(input);
3858 QDECREF(args);
3861 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
3863 Monitor *old_mon = cur_mon;
3865 cur_mon = opaque;
3867 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
3869 cur_mon = old_mon;
3872 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3874 Monitor *old_mon = cur_mon;
3875 int i;
3877 cur_mon = opaque;
3879 if (cur_mon->rs) {
3880 for (i = 0; i < size; i++)
3881 readline_handle_byte(cur_mon->rs, buf[i]);
3882 } else {
3883 if (size == 0 || buf[size - 1] != 0)
3884 monitor_printf(cur_mon, "corrupted command\n");
3885 else
3886 handle_hmp_command(cur_mon, (char *)buf);
3889 cur_mon = old_mon;
3892 static void monitor_command_cb(void *opaque, const char *cmdline,
3893 void *readline_opaque)
3895 Monitor *mon = opaque;
3897 monitor_suspend(mon);
3898 handle_hmp_command(mon, cmdline);
3899 monitor_resume(mon);
3902 int monitor_suspend(Monitor *mon)
3904 if (!mon->rs)
3905 return -ENOTTY;
3906 mon->suspend_cnt++;
3907 return 0;
3910 void monitor_resume(Monitor *mon)
3912 if (!mon->rs)
3913 return;
3914 if (--mon->suspend_cnt == 0)
3915 readline_show_prompt(mon->rs);
3918 static QObject *get_qmp_greeting(void)
3920 QObject *ver = NULL;
3922 qmp_marshal_query_version(NULL, &ver, NULL);
3923 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3926 static void monitor_qmp_event(void *opaque, int event)
3928 QObject *data;
3929 Monitor *mon = opaque;
3931 switch (event) {
3932 case CHR_EVENT_OPENED:
3933 mon->qmp.in_command_mode = false;
3934 data = get_qmp_greeting();
3935 monitor_json_emitter(mon, data);
3936 qobject_decref(data);
3937 mon_refcount++;
3938 break;
3939 case CHR_EVENT_CLOSED:
3940 json_message_parser_destroy(&mon->qmp.parser);
3941 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
3942 mon_refcount--;
3943 monitor_fdsets_cleanup();
3944 break;
3948 static void monitor_event(void *opaque, int event)
3950 Monitor *mon = opaque;
3952 switch (event) {
3953 case CHR_EVENT_MUX_IN:
3954 qemu_mutex_lock(&mon->out_lock);
3955 mon->mux_out = 0;
3956 qemu_mutex_unlock(&mon->out_lock);
3957 if (mon->reset_seen) {
3958 readline_restart(mon->rs);
3959 monitor_resume(mon);
3960 monitor_flush(mon);
3961 } else {
3962 mon->suspend_cnt = 0;
3964 break;
3966 case CHR_EVENT_MUX_OUT:
3967 if (mon->reset_seen) {
3968 if (mon->suspend_cnt == 0) {
3969 monitor_printf(mon, "\n");
3971 monitor_flush(mon);
3972 monitor_suspend(mon);
3973 } else {
3974 mon->suspend_cnt++;
3976 qemu_mutex_lock(&mon->out_lock);
3977 mon->mux_out = 1;
3978 qemu_mutex_unlock(&mon->out_lock);
3979 break;
3981 case CHR_EVENT_OPENED:
3982 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3983 "information\n", QEMU_VERSION);
3984 if (!mon->mux_out) {
3985 readline_restart(mon->rs);
3986 readline_show_prompt(mon->rs);
3988 mon->reset_seen = 1;
3989 mon_refcount++;
3990 break;
3992 case CHR_EVENT_CLOSED:
3993 mon_refcount--;
3994 monitor_fdsets_cleanup();
3995 break;
3999 static int
4000 compare_mon_cmd(const void *a, const void *b)
4002 return strcmp(((const mon_cmd_t *)a)->name,
4003 ((const mon_cmd_t *)b)->name);
4006 static void sortcmdlist(void)
4008 int array_num;
4009 int elem_size = sizeof(mon_cmd_t);
4011 array_num = sizeof(mon_cmds)/elem_size-1;
4012 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4014 array_num = sizeof(info_cmds)/elem_size-1;
4015 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4020 * Local variables:
4021 * c-indent-level: 4
4022 * c-basic-offset: 4
4023 * tab-width: 8
4024 * End:
4027 /* These functions just adapt the readline interface in a typesafe way. We
4028 * could cast function pointers but that discards compiler checks.
4030 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4031 const char *fmt, ...)
4033 va_list ap;
4034 va_start(ap, fmt);
4035 monitor_vprintf(opaque, fmt, ap);
4036 va_end(ap);
4039 static void monitor_readline_flush(void *opaque)
4041 monitor_flush(opaque);
4044 static void __attribute__((constructor)) monitor_lock_init(void)
4046 qemu_mutex_init(&monitor_lock);
4049 void monitor_init(CharDriverState *chr, int flags)
4051 static int is_first_init = 1;
4052 Monitor *mon;
4054 if (is_first_init) {
4055 monitor_qapi_event_init();
4056 sortcmdlist();
4057 is_first_init = 0;
4060 mon = g_malloc(sizeof(*mon));
4061 monitor_data_init(mon);
4063 mon->chr = chr;
4064 mon->flags = flags;
4065 if (flags & MONITOR_USE_READLINE) {
4066 mon->rs = readline_init(monitor_readline_printf,
4067 monitor_readline_flush,
4068 mon,
4069 monitor_find_completion);
4070 monitor_read_command(mon, 0);
4073 if (monitor_is_qmp(mon)) {
4074 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4075 monitor_qmp_event, mon);
4076 qemu_chr_fe_set_echo(chr, true);
4077 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
4078 } else {
4079 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4080 monitor_event, mon);
4083 qemu_mutex_lock(&monitor_lock);
4084 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4085 qemu_mutex_unlock(&monitor_lock);
4088 static void bdrv_password_cb(void *opaque, const char *password,
4089 void *readline_opaque)
4091 Monitor *mon = opaque;
4092 BlockDriverState *bs = readline_opaque;
4093 int ret = 0;
4094 Error *local_err = NULL;
4096 bdrv_add_key(bs, password, &local_err);
4097 if (local_err) {
4098 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4099 error_free(local_err);
4100 ret = -EPERM;
4102 if (mon->password_completion_cb)
4103 mon->password_completion_cb(mon->password_opaque, ret);
4105 monitor_read_command(mon, 1);
4108 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4109 BlockCompletionFunc *completion_cb,
4110 void *opaque)
4112 int err;
4114 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4115 bdrv_get_encrypted_filename(bs));
4117 mon->password_completion_cb = completion_cb;
4118 mon->password_opaque = opaque;
4120 err = monitor_read_password(mon, bdrv_password_cb, bs);
4122 if (err && completion_cb)
4123 completion_cb(opaque, err);
4125 return err;
4128 int monitor_read_block_device_key(Monitor *mon, const char *device,
4129 BlockCompletionFunc *completion_cb,
4130 void *opaque)
4132 Error *err = NULL;
4133 BlockBackend *blk;
4135 blk = blk_by_name(device);
4136 if (!blk) {
4137 monitor_printf(mon, "Device not found %s\n", device);
4138 return -1;
4140 if (!blk_bs(blk)) {
4141 monitor_printf(mon, "Device '%s' has no medium\n", device);
4142 return -1;
4145 bdrv_add_key(blk_bs(blk), NULL, &err);
4146 if (err) {
4147 error_free(err);
4148 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4151 if (completion_cb) {
4152 completion_cb(opaque, 0);
4154 return 0;
4157 QemuOptsList qemu_mon_opts = {
4158 .name = "mon",
4159 .implied_opt_name = "chardev",
4160 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4161 .desc = {
4163 .name = "mode",
4164 .type = QEMU_OPT_STRING,
4166 .name = "chardev",
4167 .type = QEMU_OPT_STRING,
4169 .name = "default",
4170 .type = QEMU_OPT_BOOL,
4172 .name = "pretty",
4173 .type = QEMU_OPT_BOOL,
4175 { /* end of list */ }
4179 #ifndef TARGET_I386
4180 void qmp_rtc_reset_reinjection(Error **errp)
4182 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
4184 #endif
4186 #ifndef TARGET_S390X
4187 void qmp_dump_skeys(const char *filename, Error **errp)
4189 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4191 #endif