vfio/pci: Config mirror quirk
[qemu/ar7.git] / monitor.c
blobd0edb3bb15416bb7fc68aeecbf36084f0ddc9e2f
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 int64_t rate; /* Minimum time (in ns) between two events */
186 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
187 QEMUTimer *timer; /* Timer for handling delayed events */
188 QObject *data; /* Event pending delayed dispatch */
189 } MonitorQAPIEventState;
191 struct Monitor {
192 CharDriverState *chr;
193 int reset_seen;
194 int flags;
195 int suspend_cnt;
196 bool skip_flush;
198 QemuMutex out_lock;
199 QString *outbuf;
200 guint out_watch;
202 /* Read under either BQL or out_lock, written with BQL+out_lock. */
203 int mux_out;
205 ReadLineState *rs;
206 MonitorQMP qmp;
207 CPUState *mon_cpu;
208 BlockCompletionFunc *password_completion_cb;
209 void *password_opaque;
210 mon_cmd_t *cmd_table;
211 QLIST_HEAD(,mon_fd_t) fds;
212 QLIST_ENTRY(Monitor) entry;
215 /* QMP checker flags */
216 #define QMP_ACCEPT_UNKNOWNS 1
218 /* Protects mon_list, monitor_event_state. */
219 static QemuMutex monitor_lock;
221 static QLIST_HEAD(mon_list, Monitor) mon_list;
222 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
223 static int mon_refcount;
225 static mon_cmd_t mon_cmds[];
226 static mon_cmd_t info_cmds[];
228 static const mon_cmd_t qmp_cmds[];
230 Monitor *cur_mon;
232 static void monitor_command_cb(void *opaque, const char *cmdline,
233 void *readline_opaque);
236 * Is @mon a QMP monitor?
238 static inline bool monitor_is_qmp(const Monitor *mon)
240 return (mon->flags & MONITOR_USE_CONTROL);
244 * Is the current monitor, if any, a QMP monitor?
246 bool monitor_cur_is_qmp(void)
248 return cur_mon && monitor_is_qmp(cur_mon);
251 void monitor_read_command(Monitor *mon, int show_prompt)
253 if (!mon->rs)
254 return;
256 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
257 if (show_prompt)
258 readline_show_prompt(mon->rs);
261 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
262 void *opaque)
264 if (mon->rs) {
265 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
266 /* prompt is printed on return from the command handler */
267 return 0;
268 } else {
269 monitor_printf(mon, "terminal does not support password prompting\n");
270 return -ENOTTY;
274 static void monitor_flush_locked(Monitor *mon);
276 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
277 void *opaque)
279 Monitor *mon = opaque;
281 qemu_mutex_lock(&mon->out_lock);
282 mon->out_watch = 0;
283 monitor_flush_locked(mon);
284 qemu_mutex_unlock(&mon->out_lock);
285 return FALSE;
288 /* Called with mon->out_lock held. */
289 static void monitor_flush_locked(Monitor *mon)
291 int rc;
292 size_t len;
293 const char *buf;
295 if (mon->skip_flush) {
296 return;
299 buf = qstring_get_str(mon->outbuf);
300 len = qstring_get_length(mon->outbuf);
302 if (len && !mon->mux_out) {
303 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
304 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
305 /* all flushed or error */
306 QDECREF(mon->outbuf);
307 mon->outbuf = qstring_new();
308 return;
310 if (rc > 0) {
311 /* partinal write */
312 QString *tmp = qstring_from_str(buf + rc);
313 QDECREF(mon->outbuf);
314 mon->outbuf = tmp;
316 if (mon->out_watch == 0) {
317 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
318 monitor_unblocked, mon);
323 void monitor_flush(Monitor *mon)
325 qemu_mutex_lock(&mon->out_lock);
326 monitor_flush_locked(mon);
327 qemu_mutex_unlock(&mon->out_lock);
330 /* flush at every end of line */
331 static void monitor_puts(Monitor *mon, const char *str)
333 char c;
335 qemu_mutex_lock(&mon->out_lock);
336 for(;;) {
337 c = *str++;
338 if (c == '\0')
339 break;
340 if (c == '\n') {
341 qstring_append_chr(mon->outbuf, '\r');
343 qstring_append_chr(mon->outbuf, c);
344 if (c == '\n') {
345 monitor_flush_locked(mon);
348 qemu_mutex_unlock(&mon->out_lock);
351 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
353 char *buf;
355 if (!mon)
356 return;
358 if (monitor_is_qmp(mon)) {
359 return;
362 buf = g_strdup_vprintf(fmt, ap);
363 monitor_puts(mon, buf);
364 g_free(buf);
367 void monitor_printf(Monitor *mon, const char *fmt, ...)
369 va_list ap;
370 va_start(ap, fmt);
371 monitor_vprintf(mon, fmt, ap);
372 va_end(ap);
375 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
376 const char *fmt, ...)
378 va_list ap;
379 va_start(ap, fmt);
380 monitor_vprintf((Monitor *)stream, fmt, ap);
381 va_end(ap);
382 return 0;
385 static void monitor_json_emitter(Monitor *mon, const QObject *data)
387 QString *json;
389 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
390 qobject_to_json(data);
391 assert(json != NULL);
393 qstring_append_chr(json, '\n');
394 monitor_puts(mon, qstring_get_str(json));
396 QDECREF(json);
399 static QDict *build_qmp_error_dict(Error *err)
401 QObject *obj;
403 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
404 ErrorClass_lookup[error_get_class(err)],
405 error_get_pretty(err));
407 return qobject_to_qdict(obj);
410 static void monitor_protocol_emitter(Monitor *mon, QObject *data,
411 Error *err)
413 QDict *qmp;
415 trace_monitor_protocol_emitter(mon);
417 if (!err) {
418 /* success response */
419 qmp = qdict_new();
420 if (data) {
421 qobject_incref(data);
422 qdict_put_obj(qmp, "return", data);
423 } else {
424 /* return an empty QDict by default */
425 qdict_put(qmp, "return", qdict_new());
427 } else {
428 /* error response */
429 qmp = build_qmp_error_dict(err);
432 if (mon->qmp.id) {
433 qdict_put_obj(qmp, "id", mon->qmp.id);
434 mon->qmp.id = NULL;
437 monitor_json_emitter(mon, QOBJECT(qmp));
438 QDECREF(qmp);
442 static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
445 * Emits the event to every monitor instance, @event is only used for trace
446 * Called with monitor_lock held.
448 static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
450 Monitor *mon;
452 trace_monitor_protocol_event_emit(event, data);
453 QLIST_FOREACH(mon, &mon_list, entry) {
454 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
455 monitor_json_emitter(mon, data);
461 * Queue a new event for emission to Monitor instances,
462 * applying any rate limiting if required.
464 static void
465 monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
467 MonitorQAPIEventState *evstate;
468 assert(event < QAPI_EVENT_MAX);
469 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
471 evstate = &(monitor_qapi_event_state[event]);
472 trace_monitor_protocol_event_queue(event,
473 data,
474 evstate->rate,
475 evstate->last,
476 now);
478 /* Rate limit of 0 indicates no throttling */
479 qemu_mutex_lock(&monitor_lock);
480 if (!evstate->rate) {
481 monitor_qapi_event_emit(event, QOBJECT(data));
482 evstate->last = now;
483 } else {
484 int64_t delta = now - evstate->last;
485 if (evstate->data ||
486 delta < evstate->rate) {
487 /* If there's an existing event pending, replace
488 * it with the new event, otherwise schedule a
489 * timer for delayed emission
491 if (evstate->data) {
492 qobject_decref(evstate->data);
493 } else {
494 int64_t then = evstate->last + evstate->rate;
495 timer_mod_ns(evstate->timer, then);
497 evstate->data = QOBJECT(data);
498 qobject_incref(evstate->data);
499 } else {
500 monitor_qapi_event_emit(event, QOBJECT(data));
501 evstate->last = now;
504 qemu_mutex_unlock(&monitor_lock);
508 * The callback invoked by QemuTimer when a delayed
509 * event is ready to be emitted
511 static void monitor_qapi_event_handler(void *opaque)
513 MonitorQAPIEventState *evstate = opaque;
514 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
516 trace_monitor_protocol_event_handler(evstate->event,
517 evstate->data,
518 evstate->last,
519 now);
520 qemu_mutex_lock(&monitor_lock);
521 if (evstate->data) {
522 monitor_qapi_event_emit(evstate->event, evstate->data);
523 qobject_decref(evstate->data);
524 evstate->data = NULL;
526 evstate->last = now;
527 qemu_mutex_unlock(&monitor_lock);
531 * @event: the event ID to be limited
532 * @rate: the rate limit in milliseconds
534 * Sets a rate limit on a particular event, so no
535 * more than 1 event will be emitted within @rate
536 * milliseconds
538 static void
539 monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
541 MonitorQAPIEventState *evstate;
542 assert(event < QAPI_EVENT_MAX);
544 evstate = &(monitor_qapi_event_state[event]);
546 trace_monitor_protocol_event_throttle(event, rate);
547 evstate->event = event;
548 assert(rate * SCALE_MS <= INT64_MAX);
549 evstate->rate = rate * SCALE_MS;
550 evstate->last = 0;
551 evstate->data = NULL;
552 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
553 SCALE_MS,
554 monitor_qapi_event_handler,
555 evstate);
558 static void monitor_qapi_event_init(void)
560 /* Limit guest-triggerable events to 1 per second */
561 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
562 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
563 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
565 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
566 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
568 qmp_event_set_func_emit(monitor_qapi_event_queue);
571 static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
573 cur_mon->qmp.in_command_mode = true;
576 static void handle_hmp_command(Monitor *mon, const char *cmdline);
578 static void monitor_data_init(Monitor *mon)
580 memset(mon, 0, sizeof(Monitor));
581 qemu_mutex_init(&mon->out_lock);
582 mon->outbuf = qstring_new();
583 /* Use *mon_cmds by default. */
584 mon->cmd_table = mon_cmds;
587 static void monitor_data_destroy(Monitor *mon)
589 QDECREF(mon->outbuf);
590 qemu_mutex_destroy(&mon->out_lock);
593 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
594 int64_t cpu_index, Error **errp)
596 char *output = NULL;
597 Monitor *old_mon, hmp;
599 monitor_data_init(&hmp);
600 hmp.skip_flush = true;
602 old_mon = cur_mon;
603 cur_mon = &hmp;
605 if (has_cpu_index) {
606 int ret = monitor_set_cpu(cpu_index);
607 if (ret < 0) {
608 cur_mon = old_mon;
609 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
610 "a CPU number");
611 goto out;
615 handle_hmp_command(&hmp, command_line);
616 cur_mon = old_mon;
618 qemu_mutex_lock(&hmp.out_lock);
619 if (qstring_get_length(hmp.outbuf) > 0) {
620 output = g_strdup(qstring_get_str(hmp.outbuf));
621 } else {
622 output = g_strdup("");
624 qemu_mutex_unlock(&hmp.out_lock);
626 out:
627 monitor_data_destroy(&hmp);
628 return output;
631 static int compare_cmd(const char *name, const char *list)
633 const char *p, *pstart;
634 int len;
635 len = strlen(name);
636 p = list;
637 for(;;) {
638 pstart = p;
639 p = strchr(p, '|');
640 if (!p)
641 p = pstart + strlen(pstart);
642 if ((p - pstart) == len && !memcmp(pstart, name, len))
643 return 1;
644 if (*p == '\0')
645 break;
646 p++;
648 return 0;
651 static int get_str(char *buf, int buf_size, const char **pp)
653 const char *p;
654 char *q;
655 int c;
657 q = buf;
658 p = *pp;
659 while (qemu_isspace(*p)) {
660 p++;
662 if (*p == '\0') {
663 fail:
664 *q = '\0';
665 *pp = p;
666 return -1;
668 if (*p == '\"') {
669 p++;
670 while (*p != '\0' && *p != '\"') {
671 if (*p == '\\') {
672 p++;
673 c = *p++;
674 switch (c) {
675 case 'n':
676 c = '\n';
677 break;
678 case 'r':
679 c = '\r';
680 break;
681 case '\\':
682 case '\'':
683 case '\"':
684 break;
685 default:
686 printf("unsupported escape code: '\\%c'\n", c);
687 goto fail;
689 if ((q - buf) < buf_size - 1) {
690 *q++ = c;
692 } else {
693 if ((q - buf) < buf_size - 1) {
694 *q++ = *p;
696 p++;
699 if (*p != '\"') {
700 printf("unterminated string\n");
701 goto fail;
703 p++;
704 } else {
705 while (*p != '\0' && !qemu_isspace(*p)) {
706 if ((q - buf) < buf_size - 1) {
707 *q++ = *p;
709 p++;
712 *q = '\0';
713 *pp = p;
714 return 0;
717 #define MAX_ARGS 16
719 static void free_cmdline_args(char **args, int nb_args)
721 int i;
723 assert(nb_args <= MAX_ARGS);
725 for (i = 0; i < nb_args; i++) {
726 g_free(args[i]);
732 * Parse the command line to get valid args.
733 * @cmdline: command line to be parsed.
734 * @pnb_args: location to store the number of args, must NOT be NULL.
735 * @args: location to store the args, which should be freed by caller, must
736 * NOT be NULL.
738 * Returns 0 on success, negative on failure.
740 * NOTE: this parser is an approximate form of the real command parser. Number
741 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
742 * return with failure.
744 static int parse_cmdline(const char *cmdline,
745 int *pnb_args, char **args)
747 const char *p;
748 int nb_args, ret;
749 char buf[1024];
751 p = cmdline;
752 nb_args = 0;
753 for (;;) {
754 while (qemu_isspace(*p)) {
755 p++;
757 if (*p == '\0') {
758 break;
760 if (nb_args >= MAX_ARGS) {
761 goto fail;
763 ret = get_str(buf, sizeof(buf), &p);
764 if (ret < 0) {
765 goto fail;
767 args[nb_args] = g_strdup(buf);
768 nb_args++;
770 *pnb_args = nb_args;
771 return 0;
773 fail:
774 free_cmdline_args(args, nb_args);
775 return -1;
778 static void help_cmd_dump_one(Monitor *mon,
779 const mon_cmd_t *cmd,
780 char **prefix_args,
781 int prefix_args_nb)
783 int i;
785 for (i = 0; i < prefix_args_nb; i++) {
786 monitor_printf(mon, "%s ", prefix_args[i]);
788 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
791 /* @args[@arg_index] is the valid command need to find in @cmds */
792 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
793 char **args, int nb_args, int arg_index)
795 const mon_cmd_t *cmd;
797 /* No valid arg need to compare with, dump all in *cmds */
798 if (arg_index >= nb_args) {
799 for (cmd = cmds; cmd->name != NULL; cmd++) {
800 help_cmd_dump_one(mon, cmd, args, arg_index);
802 return;
805 /* Find one entry to dump */
806 for (cmd = cmds; cmd->name != NULL; cmd++) {
807 if (compare_cmd(args[arg_index], cmd->name)) {
808 if (cmd->sub_table) {
809 /* continue with next arg */
810 help_cmd_dump(mon, cmd->sub_table,
811 args, nb_args, arg_index + 1);
812 } else {
813 help_cmd_dump_one(mon, cmd, args, arg_index);
815 break;
820 static void help_cmd(Monitor *mon, const char *name)
822 char *args[MAX_ARGS];
823 int nb_args = 0;
825 /* 1. parse user input */
826 if (name) {
827 /* special case for log, directly dump and return */
828 if (!strcmp(name, "log")) {
829 const QEMULogItem *item;
830 monitor_printf(mon, "Log items (comma separated):\n");
831 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
832 for (item = qemu_log_items; item->mask != 0; item++) {
833 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
835 return;
838 if (parse_cmdline(name, &nb_args, args) < 0) {
839 return;
843 /* 2. dump the contents according to parsed args */
844 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
846 free_cmdline_args(args, nb_args);
849 static void do_help_cmd(Monitor *mon, const QDict *qdict)
851 help_cmd(mon, qdict_get_try_str(qdict, "name"));
854 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
856 const char *tp_name = qdict_get_str(qdict, "name");
857 bool new_state = qdict_get_bool(qdict, "option");
858 Error *local_err = NULL;
860 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
861 if (local_err) {
862 error_report_err(local_err);
866 #ifdef CONFIG_TRACE_SIMPLE
867 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
869 const char *op = qdict_get_try_str(qdict, "op");
870 const char *arg = qdict_get_try_str(qdict, "arg");
872 if (!op) {
873 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
874 } else if (!strcmp(op, "on")) {
875 st_set_trace_file_enabled(true);
876 } else if (!strcmp(op, "off")) {
877 st_set_trace_file_enabled(false);
878 } else if (!strcmp(op, "flush")) {
879 st_flush_trace_buffer();
880 } else if (!strcmp(op, "set")) {
881 if (arg) {
882 st_set_trace_file(arg);
884 } else {
885 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
886 help_cmd(mon, "trace-file");
889 #endif
891 static void hmp_info_help(Monitor *mon, const QDict *qdict)
893 help_cmd(mon, "info");
896 CommandInfoList *qmp_query_commands(Error **errp)
898 CommandInfoList *info, *cmd_list = NULL;
899 const mon_cmd_t *cmd;
901 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
902 info = g_malloc0(sizeof(*info));
903 info->value = g_malloc0(sizeof(*info->value));
904 info->value->name = g_strdup(cmd->name);
906 info->next = cmd_list;
907 cmd_list = info;
910 return cmd_list;
913 EventInfoList *qmp_query_events(Error **errp)
915 EventInfoList *info, *ev_list = NULL;
916 QAPIEvent e;
918 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
919 const char *event_name = QAPIEvent_lookup[e];
920 assert(event_name != NULL);
921 info = g_malloc0(sizeof(*info));
922 info->value = g_malloc0(sizeof(*info->value));
923 info->value->name = g_strdup(event_name);
925 info->next = ev_list;
926 ev_list = info;
929 return ev_list;
933 * Minor hack: generated marshalling suppressed for this command
934 * ('gen': false in the schema) so we can parse the JSON string
935 * directly into QObject instead of first parsing it with
936 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
937 * to QObject with generated output marshallers, every time. Instead,
938 * we do it in test-qmp-input-visitor.c, just to make sure
939 * qapi-introspect.py's output actually conforms to the schema.
941 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
942 Error **errp)
944 *ret_data = qobject_from_json(qmp_schema_json);
947 /* set the current CPU defined by the user */
948 int monitor_set_cpu(int cpu_index)
950 CPUState *cpu;
952 cpu = qemu_get_cpu(cpu_index);
953 if (cpu == NULL) {
954 return -1;
956 cur_mon->mon_cpu = cpu;
957 return 0;
960 static CPUState *mon_get_cpu(void)
962 if (!cur_mon->mon_cpu) {
963 monitor_set_cpu(0);
965 cpu_synchronize_state(cur_mon->mon_cpu);
966 return cur_mon->mon_cpu;
969 CPUArchState *mon_get_cpu_env(void)
971 return mon_get_cpu()->env_ptr;
974 int monitor_get_cpu_index(void)
976 return mon_get_cpu()->cpu_index;
979 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
981 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
984 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
986 dump_exec_info((FILE *)mon, monitor_fprintf);
987 dump_drift_info((FILE *)mon, monitor_fprintf);
990 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
992 dump_opcount_info((FILE *)mon, monitor_fprintf);
995 static void hmp_info_history(Monitor *mon, const QDict *qdict)
997 int i;
998 const char *str;
1000 if (!mon->rs)
1001 return;
1002 i = 0;
1003 for(;;) {
1004 str = readline_get_history(mon->rs, i);
1005 if (!str)
1006 break;
1007 monitor_printf(mon, "%d: '%s'\n", i, str);
1008 i++;
1012 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
1014 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
1017 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1019 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1020 TraceEventInfoList *elem;
1022 for (elem = events; elem != NULL; elem = elem->next) {
1023 monitor_printf(mon, "%s : state %u\n",
1024 elem->value->name,
1025 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1027 qapi_free_TraceEventInfoList(events);
1030 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1031 bool has_port, int64_t port,
1032 bool has_tls_port, int64_t tls_port,
1033 bool has_cert_subject, const char *cert_subject,
1034 Error **errp)
1036 if (strcmp(protocol, "spice") == 0) {
1037 if (!qemu_using_spice(errp)) {
1038 return;
1041 if (!has_port && !has_tls_port) {
1042 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1043 return;
1046 if (qemu_spice_migrate_info(hostname,
1047 has_port ? port : -1,
1048 has_tls_port ? tls_port : -1,
1049 cert_subject)) {
1050 error_setg(errp, QERR_UNDEFINED_ERROR);
1051 return;
1053 return;
1056 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1059 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1061 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1064 static void hmp_log(Monitor *mon, const QDict *qdict)
1066 int mask;
1067 const char *items = qdict_get_str(qdict, "items");
1069 if (!strcmp(items, "none")) {
1070 mask = 0;
1071 } else {
1072 mask = qemu_str_to_log_mask(items);
1073 if (!mask) {
1074 help_cmd(mon, "log");
1075 return;
1078 qemu_set_log(mask);
1081 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1083 const char *option = qdict_get_try_str(qdict, "option");
1084 if (!option || !strcmp(option, "on")) {
1085 singlestep = 1;
1086 } else if (!strcmp(option, "off")) {
1087 singlestep = 0;
1088 } else {
1089 monitor_printf(mon, "unexpected option %s\n", option);
1093 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1095 const char *device = qdict_get_try_str(qdict, "device");
1096 if (!device)
1097 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1098 if (gdbserver_start(device) < 0) {
1099 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1100 device);
1101 } else if (strcmp(device, "none") == 0) {
1102 monitor_printf(mon, "Disabled gdbserver\n");
1103 } else {
1104 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1105 device);
1109 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1111 const char *action = qdict_get_str(qdict, "action");
1112 if (select_watchdog_action(action) == -1) {
1113 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1117 static void monitor_printc(Monitor *mon, int c)
1119 monitor_printf(mon, "'");
1120 switch(c) {
1121 case '\'':
1122 monitor_printf(mon, "\\'");
1123 break;
1124 case '\\':
1125 monitor_printf(mon, "\\\\");
1126 break;
1127 case '\n':
1128 monitor_printf(mon, "\\n");
1129 break;
1130 case '\r':
1131 monitor_printf(mon, "\\r");
1132 break;
1133 default:
1134 if (c >= 32 && c <= 126) {
1135 monitor_printf(mon, "%c", c);
1136 } else {
1137 monitor_printf(mon, "\\x%02x", c);
1139 break;
1141 monitor_printf(mon, "'");
1144 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1145 hwaddr addr, int is_physical)
1147 int l, line_size, i, max_digits, len;
1148 uint8_t buf[16];
1149 uint64_t v;
1151 if (format == 'i') {
1152 int flags = 0;
1153 #ifdef TARGET_I386
1154 CPUArchState *env = mon_get_cpu_env();
1155 if (wsize == 2) {
1156 flags = 1;
1157 } else if (wsize == 4) {
1158 flags = 0;
1159 } else {
1160 /* as default we use the current CS size */
1161 flags = 0;
1162 if (env) {
1163 #ifdef TARGET_X86_64
1164 if ((env->efer & MSR_EFER_LMA) &&
1165 (env->segs[R_CS].flags & DESC_L_MASK))
1166 flags = 2;
1167 else
1168 #endif
1169 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1170 flags = 1;
1173 #endif
1174 #ifdef TARGET_PPC
1175 CPUArchState *env = mon_get_cpu_env();
1176 flags = msr_le << 16;
1177 flags |= env->bfd_mach;
1178 #endif
1179 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
1180 return;
1183 len = wsize * count;
1184 if (wsize == 1)
1185 line_size = 8;
1186 else
1187 line_size = 16;
1188 max_digits = 0;
1190 switch(format) {
1191 case 'o':
1192 max_digits = (wsize * 8 + 2) / 3;
1193 break;
1194 default:
1195 case 'x':
1196 max_digits = (wsize * 8) / 4;
1197 break;
1198 case 'u':
1199 case 'd':
1200 max_digits = (wsize * 8 * 10 + 32) / 33;
1201 break;
1202 case 'c':
1203 wsize = 1;
1204 break;
1207 while (len > 0) {
1208 if (is_physical)
1209 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1210 else
1211 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1212 l = len;
1213 if (l > line_size)
1214 l = line_size;
1215 if (is_physical) {
1216 cpu_physical_memory_read(addr, buf, l);
1217 } else {
1218 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
1219 monitor_printf(mon, " Cannot access memory\n");
1220 break;
1223 i = 0;
1224 while (i < l) {
1225 switch(wsize) {
1226 default:
1227 case 1:
1228 v = ldub_p(buf + i);
1229 break;
1230 case 2:
1231 v = lduw_p(buf + i);
1232 break;
1233 case 4:
1234 v = (uint32_t)ldl_p(buf + i);
1235 break;
1236 case 8:
1237 v = ldq_p(buf + i);
1238 break;
1240 monitor_printf(mon, " ");
1241 switch(format) {
1242 case 'o':
1243 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1244 break;
1245 case 'x':
1246 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1247 break;
1248 case 'u':
1249 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1250 break;
1251 case 'd':
1252 monitor_printf(mon, "%*" PRId64, max_digits, v);
1253 break;
1254 case 'c':
1255 monitor_printc(mon, v);
1256 break;
1258 i += wsize;
1260 monitor_printf(mon, "\n");
1261 addr += l;
1262 len -= l;
1266 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1268 int count = qdict_get_int(qdict, "count");
1269 int format = qdict_get_int(qdict, "format");
1270 int size = qdict_get_int(qdict, "size");
1271 target_long addr = qdict_get_int(qdict, "addr");
1273 memory_dump(mon, count, format, size, addr, 0);
1276 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1278 int count = qdict_get_int(qdict, "count");
1279 int format = qdict_get_int(qdict, "format");
1280 int size = qdict_get_int(qdict, "size");
1281 hwaddr addr = qdict_get_int(qdict, "addr");
1283 memory_dump(mon, count, format, size, addr, 1);
1286 static void do_print(Monitor *mon, const QDict *qdict)
1288 int format = qdict_get_int(qdict, "format");
1289 hwaddr val = qdict_get_int(qdict, "val");
1291 switch(format) {
1292 case 'o':
1293 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1294 break;
1295 case 'x':
1296 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1297 break;
1298 case 'u':
1299 monitor_printf(mon, "%" HWADDR_PRIu, val);
1300 break;
1301 default:
1302 case 'd':
1303 monitor_printf(mon, "%" HWADDR_PRId, val);
1304 break;
1305 case 'c':
1306 monitor_printc(mon, val);
1307 break;
1309 monitor_printf(mon, "\n");
1312 static void hmp_sum(Monitor *mon, const QDict *qdict)
1314 uint32_t addr;
1315 uint16_t sum;
1316 uint32_t start = qdict_get_int(qdict, "start");
1317 uint32_t size = qdict_get_int(qdict, "size");
1319 sum = 0;
1320 for(addr = start; addr < (start + size); addr++) {
1321 uint8_t val = address_space_ldub(&address_space_memory, addr,
1322 MEMTXATTRS_UNSPECIFIED, NULL);
1323 /* BSD sum algorithm ('sum' Unix command) */
1324 sum = (sum >> 1) | (sum << 15);
1325 sum += val;
1327 monitor_printf(mon, "%05d\n", sum);
1330 static int mouse_button_state;
1332 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1334 int dx, dy, dz, button;
1335 const char *dx_str = qdict_get_str(qdict, "dx_str");
1336 const char *dy_str = qdict_get_str(qdict, "dy_str");
1337 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1339 dx = strtol(dx_str, NULL, 0);
1340 dy = strtol(dy_str, NULL, 0);
1341 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1342 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1344 if (dz_str) {
1345 dz = strtol(dz_str, NULL, 0);
1346 if (dz != 0) {
1347 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1348 qemu_input_queue_btn(NULL, button, true);
1349 qemu_input_event_sync();
1350 qemu_input_queue_btn(NULL, button, false);
1353 qemu_input_event_sync();
1356 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1358 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1359 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1360 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1361 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1363 int button_state = qdict_get_int(qdict, "button_state");
1365 if (mouse_button_state == button_state) {
1366 return;
1368 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1369 qemu_input_event_sync();
1370 mouse_button_state = button_state;
1373 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1375 int size = qdict_get_int(qdict, "size");
1376 int addr = qdict_get_int(qdict, "addr");
1377 int has_index = qdict_haskey(qdict, "index");
1378 uint32_t val;
1379 int suffix;
1381 if (has_index) {
1382 int index = qdict_get_int(qdict, "index");
1383 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1384 addr++;
1386 addr &= 0xffff;
1388 switch(size) {
1389 default:
1390 case 1:
1391 val = cpu_inb(addr);
1392 suffix = 'b';
1393 break;
1394 case 2:
1395 val = cpu_inw(addr);
1396 suffix = 'w';
1397 break;
1398 case 4:
1399 val = cpu_inl(addr);
1400 suffix = 'l';
1401 break;
1403 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1404 suffix, addr, size * 2, val);
1407 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1409 int size = qdict_get_int(qdict, "size");
1410 int addr = qdict_get_int(qdict, "addr");
1411 int val = qdict_get_int(qdict, "val");
1413 addr &= IOPORTS_MASK;
1415 switch (size) {
1416 default:
1417 case 1:
1418 cpu_outb(addr, val);
1419 break;
1420 case 2:
1421 cpu_outw(addr, val);
1422 break;
1423 case 4:
1424 cpu_outl(addr, val);
1425 break;
1429 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1431 Error *local_err = NULL;
1432 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1434 qemu_boot_set(bootdevice, &local_err);
1435 if (local_err) {
1436 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1437 error_free(local_err);
1438 } else {
1439 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1443 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1445 mtree_info((fprintf_function)monitor_printf, mon);
1448 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1450 int i;
1451 CPUState *cpu;
1452 uint64_t *node_mem;
1454 node_mem = g_new0(uint64_t, nb_numa_nodes);
1455 query_numa_node_mem(node_mem);
1456 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1457 for (i = 0; i < nb_numa_nodes; i++) {
1458 monitor_printf(mon, "node %d cpus:", i);
1459 CPU_FOREACH(cpu) {
1460 if (cpu->numa_node == i) {
1461 monitor_printf(mon, " %d", cpu->cpu_index);
1464 monitor_printf(mon, "\n");
1465 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1466 node_mem[i] >> 20);
1468 g_free(node_mem);
1471 #ifdef CONFIG_PROFILER
1473 int64_t tcg_time;
1474 int64_t dev_time;
1476 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1478 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1479 dev_time, dev_time / (double)get_ticks_per_sec());
1480 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1481 tcg_time, tcg_time / (double)get_ticks_per_sec());
1482 tcg_time = 0;
1483 dev_time = 0;
1485 #else
1486 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1488 monitor_printf(mon, "Internal profiler not compiled\n");
1490 #endif
1492 /* Capture support */
1493 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1495 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1497 int i;
1498 CaptureState *s;
1500 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1501 monitor_printf(mon, "[%d]: ", i);
1502 s->ops.info (s->opaque);
1506 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1508 int i;
1509 int n = qdict_get_int(qdict, "n");
1510 CaptureState *s;
1512 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1513 if (i == n) {
1514 s->ops.destroy (s->opaque);
1515 QLIST_REMOVE (s, entries);
1516 g_free (s);
1517 return;
1522 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1524 const char *path = qdict_get_str(qdict, "path");
1525 int has_freq = qdict_haskey(qdict, "freq");
1526 int freq = qdict_get_try_int(qdict, "freq", -1);
1527 int has_bits = qdict_haskey(qdict, "bits");
1528 int bits = qdict_get_try_int(qdict, "bits", -1);
1529 int has_channels = qdict_haskey(qdict, "nchannels");
1530 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1531 CaptureState *s;
1533 s = g_malloc0 (sizeof (*s));
1535 freq = has_freq ? freq : 44100;
1536 bits = has_bits ? bits : 16;
1537 nchannels = has_channels ? nchannels : 2;
1539 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1540 monitor_printf(mon, "Failed to add wave capture\n");
1541 g_free (s);
1542 return;
1544 QLIST_INSERT_HEAD (&capture_head, s, entries);
1547 static qemu_acl *find_acl(Monitor *mon, const char *name)
1549 qemu_acl *acl = qemu_acl_find(name);
1551 if (!acl) {
1552 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1554 return acl;
1557 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1559 const char *aclname = qdict_get_str(qdict, "aclname");
1560 qemu_acl *acl = find_acl(mon, aclname);
1561 qemu_acl_entry *entry;
1562 int i = 0;
1564 if (acl) {
1565 monitor_printf(mon, "policy: %s\n",
1566 acl->defaultDeny ? "deny" : "allow");
1567 QTAILQ_FOREACH(entry, &acl->entries, next) {
1568 i++;
1569 monitor_printf(mon, "%d: %s %s\n", i,
1570 entry->deny ? "deny" : "allow", entry->match);
1575 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1577 const char *aclname = qdict_get_str(qdict, "aclname");
1578 qemu_acl *acl = find_acl(mon, aclname);
1580 if (acl) {
1581 qemu_acl_reset(acl);
1582 monitor_printf(mon, "acl: removed all rules\n");
1586 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1588 const char *aclname = qdict_get_str(qdict, "aclname");
1589 const char *policy = qdict_get_str(qdict, "policy");
1590 qemu_acl *acl = find_acl(mon, aclname);
1592 if (acl) {
1593 if (strcmp(policy, "allow") == 0) {
1594 acl->defaultDeny = 0;
1595 monitor_printf(mon, "acl: policy set to 'allow'\n");
1596 } else if (strcmp(policy, "deny") == 0) {
1597 acl->defaultDeny = 1;
1598 monitor_printf(mon, "acl: policy set to 'deny'\n");
1599 } else {
1600 monitor_printf(mon, "acl: unknown policy '%s', "
1601 "expected 'deny' or 'allow'\n", policy);
1606 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1608 const char *aclname = qdict_get_str(qdict, "aclname");
1609 const char *match = qdict_get_str(qdict, "match");
1610 const char *policy = qdict_get_str(qdict, "policy");
1611 int has_index = qdict_haskey(qdict, "index");
1612 int index = qdict_get_try_int(qdict, "index", -1);
1613 qemu_acl *acl = find_acl(mon, aclname);
1614 int deny, ret;
1616 if (acl) {
1617 if (strcmp(policy, "allow") == 0) {
1618 deny = 0;
1619 } else if (strcmp(policy, "deny") == 0) {
1620 deny = 1;
1621 } else {
1622 monitor_printf(mon, "acl: unknown policy '%s', "
1623 "expected 'deny' or 'allow'\n", policy);
1624 return;
1626 if (has_index)
1627 ret = qemu_acl_insert(acl, deny, match, index);
1628 else
1629 ret = qemu_acl_append(acl, deny, match);
1630 if (ret < 0)
1631 monitor_printf(mon, "acl: unable to add acl entry\n");
1632 else
1633 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1637 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1639 const char *aclname = qdict_get_str(qdict, "aclname");
1640 const char *match = qdict_get_str(qdict, "match");
1641 qemu_acl *acl = find_acl(mon, aclname);
1642 int ret;
1644 if (acl) {
1645 ret = qemu_acl_remove(acl, match);
1646 if (ret < 0)
1647 monitor_printf(mon, "acl: no matching acl entry\n");
1648 else
1649 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1653 void qmp_getfd(const char *fdname, Error **errp)
1655 mon_fd_t *monfd;
1656 int fd;
1658 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
1659 if (fd == -1) {
1660 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1661 return;
1664 if (qemu_isdigit(fdname[0])) {
1665 close(fd);
1666 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1667 "a name not starting with a digit");
1668 return;
1671 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1672 if (strcmp(monfd->name, fdname) != 0) {
1673 continue;
1676 close(monfd->fd);
1677 monfd->fd = fd;
1678 return;
1681 monfd = g_malloc0(sizeof(mon_fd_t));
1682 monfd->name = g_strdup(fdname);
1683 monfd->fd = fd;
1685 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1688 void qmp_closefd(const char *fdname, Error **errp)
1690 mon_fd_t *monfd;
1692 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1693 if (strcmp(monfd->name, fdname) != 0) {
1694 continue;
1697 QLIST_REMOVE(monfd, next);
1698 close(monfd->fd);
1699 g_free(monfd->name);
1700 g_free(monfd);
1701 return;
1704 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1707 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
1709 int saved_vm_running = runstate_is_running();
1710 const char *name = qdict_get_str(qdict, "name");
1712 vm_stop(RUN_STATE_RESTORE_VM);
1714 if (load_vmstate(name) == 0 && saved_vm_running) {
1715 vm_start();
1719 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1721 mon_fd_t *monfd;
1723 QLIST_FOREACH(monfd, &mon->fds, next) {
1724 int fd;
1726 if (strcmp(monfd->name, fdname) != 0) {
1727 continue;
1730 fd = monfd->fd;
1732 /* caller takes ownership of fd */
1733 QLIST_REMOVE(monfd, next);
1734 g_free(monfd->name);
1735 g_free(monfd);
1737 return fd;
1740 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1741 return -1;
1744 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1746 MonFdsetFd *mon_fdset_fd;
1747 MonFdsetFd *mon_fdset_fd_next;
1749 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1750 if ((mon_fdset_fd->removed ||
1751 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1752 runstate_is_running()) {
1753 close(mon_fdset_fd->fd);
1754 g_free(mon_fdset_fd->opaque);
1755 QLIST_REMOVE(mon_fdset_fd, next);
1756 g_free(mon_fdset_fd);
1760 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1761 QLIST_REMOVE(mon_fdset, next);
1762 g_free(mon_fdset);
1766 static void monitor_fdsets_cleanup(void)
1768 MonFdset *mon_fdset;
1769 MonFdset *mon_fdset_next;
1771 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1772 monitor_fdset_cleanup(mon_fdset);
1776 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1777 const char *opaque, Error **errp)
1779 int fd;
1780 Monitor *mon = cur_mon;
1781 AddfdInfo *fdinfo;
1783 fd = qemu_chr_fe_get_msgfd(mon->chr);
1784 if (fd == -1) {
1785 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1786 goto error;
1789 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1790 has_opaque, opaque, errp);
1791 if (fdinfo) {
1792 return fdinfo;
1795 error:
1796 if (fd != -1) {
1797 close(fd);
1799 return NULL;
1802 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1804 MonFdset *mon_fdset;
1805 MonFdsetFd *mon_fdset_fd;
1806 char fd_str[60];
1808 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1809 if (mon_fdset->id != fdset_id) {
1810 continue;
1812 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1813 if (has_fd) {
1814 if (mon_fdset_fd->fd != fd) {
1815 continue;
1817 mon_fdset_fd->removed = true;
1818 break;
1819 } else {
1820 mon_fdset_fd->removed = true;
1823 if (has_fd && !mon_fdset_fd) {
1824 goto error;
1826 monitor_fdset_cleanup(mon_fdset);
1827 return;
1830 error:
1831 if (has_fd) {
1832 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1833 fdset_id, fd);
1834 } else {
1835 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1837 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1840 FdsetInfoList *qmp_query_fdsets(Error **errp)
1842 MonFdset *mon_fdset;
1843 MonFdsetFd *mon_fdset_fd;
1844 FdsetInfoList *fdset_list = NULL;
1846 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1847 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1848 FdsetFdInfoList *fdsetfd_list = NULL;
1850 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1851 fdset_info->value->fdset_id = mon_fdset->id;
1853 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1854 FdsetFdInfoList *fdsetfd_info;
1856 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1857 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1858 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1859 if (mon_fdset_fd->opaque) {
1860 fdsetfd_info->value->has_opaque = true;
1861 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1862 } else {
1863 fdsetfd_info->value->has_opaque = false;
1866 fdsetfd_info->next = fdsetfd_list;
1867 fdsetfd_list = fdsetfd_info;
1870 fdset_info->value->fds = fdsetfd_list;
1872 fdset_info->next = fdset_list;
1873 fdset_list = fdset_info;
1876 return fdset_list;
1879 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1880 bool has_opaque, const char *opaque,
1881 Error **errp)
1883 MonFdset *mon_fdset = NULL;
1884 MonFdsetFd *mon_fdset_fd;
1885 AddfdInfo *fdinfo;
1887 if (has_fdset_id) {
1888 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1889 /* Break if match found or match impossible due to ordering by ID */
1890 if (fdset_id <= mon_fdset->id) {
1891 if (fdset_id < mon_fdset->id) {
1892 mon_fdset = NULL;
1894 break;
1899 if (mon_fdset == NULL) {
1900 int64_t fdset_id_prev = -1;
1901 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
1903 if (has_fdset_id) {
1904 if (fdset_id < 0) {
1905 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
1906 "a non-negative value");
1907 return NULL;
1909 /* Use specified fdset ID */
1910 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1911 mon_fdset_cur = mon_fdset;
1912 if (fdset_id < mon_fdset_cur->id) {
1913 break;
1916 } else {
1917 /* Use first available fdset ID */
1918 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1919 mon_fdset_cur = mon_fdset;
1920 if (fdset_id_prev == mon_fdset_cur->id - 1) {
1921 fdset_id_prev = mon_fdset_cur->id;
1922 continue;
1924 break;
1928 mon_fdset = g_malloc0(sizeof(*mon_fdset));
1929 if (has_fdset_id) {
1930 mon_fdset->id = fdset_id;
1931 } else {
1932 mon_fdset->id = fdset_id_prev + 1;
1935 /* The fdset list is ordered by fdset ID */
1936 if (!mon_fdset_cur) {
1937 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
1938 } else if (mon_fdset->id < mon_fdset_cur->id) {
1939 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
1940 } else {
1941 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
1945 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
1946 mon_fdset_fd->fd = fd;
1947 mon_fdset_fd->removed = false;
1948 if (has_opaque) {
1949 mon_fdset_fd->opaque = g_strdup(opaque);
1951 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
1953 fdinfo = g_malloc0(sizeof(*fdinfo));
1954 fdinfo->fdset_id = mon_fdset->id;
1955 fdinfo->fd = mon_fdset_fd->fd;
1957 return fdinfo;
1960 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
1962 #ifndef _WIN32
1963 MonFdset *mon_fdset;
1964 MonFdsetFd *mon_fdset_fd;
1965 int mon_fd_flags;
1967 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1968 if (mon_fdset->id != fdset_id) {
1969 continue;
1971 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1972 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
1973 if (mon_fd_flags == -1) {
1974 return -1;
1977 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
1978 return mon_fdset_fd->fd;
1981 errno = EACCES;
1982 return -1;
1984 #endif
1986 errno = ENOENT;
1987 return -1;
1990 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
1992 MonFdset *mon_fdset;
1993 MonFdsetFd *mon_fdset_fd_dup;
1995 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1996 if (mon_fdset->id != fdset_id) {
1997 continue;
1999 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2000 if (mon_fdset_fd_dup->fd == dup_fd) {
2001 return -1;
2004 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2005 mon_fdset_fd_dup->fd = dup_fd;
2006 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2007 return 0;
2009 return -1;
2012 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2014 MonFdset *mon_fdset;
2015 MonFdsetFd *mon_fdset_fd_dup;
2017 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2018 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2019 if (mon_fdset_fd_dup->fd == dup_fd) {
2020 if (remove) {
2021 QLIST_REMOVE(mon_fdset_fd_dup, next);
2022 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2023 monitor_fdset_cleanup(mon_fdset);
2025 return -1;
2026 } else {
2027 return mon_fdset->id;
2032 return -1;
2035 int monitor_fdset_dup_fd_find(int dup_fd)
2037 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2040 void monitor_fdset_dup_fd_remove(int dup_fd)
2042 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2045 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2047 int fd;
2048 Error *local_err = NULL;
2050 if (!qemu_isdigit(fdname[0]) && mon) {
2051 fd = monitor_get_fd(mon, fdname, &local_err);
2052 } else {
2053 fd = qemu_parse_fd(fdname);
2054 if (fd == -1) {
2055 error_setg(&local_err, "Invalid file descriptor number '%s'",
2056 fdname);
2059 if (local_err) {
2060 error_propagate(errp, local_err);
2061 assert(fd == -1);
2062 } else {
2063 assert(fd != -1);
2066 return fd;
2069 /* Please update hmp-commands.hx when adding or changing commands */
2070 static mon_cmd_t info_cmds[] = {
2071 #include "hmp-commands-info.h"
2072 { NULL, NULL, },
2075 /* mon_cmds and info_cmds would be sorted at runtime */
2076 static mon_cmd_t mon_cmds[] = {
2077 #include "hmp-commands.h"
2078 { NULL, NULL, },
2081 static const mon_cmd_t qmp_cmds[] = {
2082 #include "qmp-commands-old.h"
2083 { /* NULL */ },
2086 /*******************************************************************/
2088 static const char *pch;
2089 static sigjmp_buf expr_env;
2092 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2093 expr_error(Monitor *mon, const char *fmt, ...)
2095 va_list ap;
2096 va_start(ap, fmt);
2097 monitor_vprintf(mon, fmt, ap);
2098 monitor_printf(mon, "\n");
2099 va_end(ap);
2100 siglongjmp(expr_env, 1);
2103 /* return 0 if OK, -1 if not found */
2104 static int get_monitor_def(target_long *pval, const char *name)
2106 const MonitorDef *md = target_monitor_defs();
2107 void *ptr;
2109 if (md == NULL) {
2110 return -1;
2113 for(; md->name != NULL; md++) {
2114 if (compare_cmd(name, md->name)) {
2115 if (md->get_value) {
2116 *pval = md->get_value(md, md->offset);
2117 } else {
2118 CPUArchState *env = mon_get_cpu_env();
2119 ptr = (uint8_t *)env + md->offset;
2120 switch(md->type) {
2121 case MD_I32:
2122 *pval = *(int32_t *)ptr;
2123 break;
2124 case MD_TLONG:
2125 *pval = *(target_long *)ptr;
2126 break;
2127 default:
2128 *pval = 0;
2129 break;
2132 return 0;
2135 return -1;
2138 static void next(void)
2140 if (*pch != '\0') {
2141 pch++;
2142 while (qemu_isspace(*pch))
2143 pch++;
2147 static int64_t expr_sum(Monitor *mon);
2149 static int64_t expr_unary(Monitor *mon)
2151 int64_t n;
2152 char *p;
2153 int ret;
2155 switch(*pch) {
2156 case '+':
2157 next();
2158 n = expr_unary(mon);
2159 break;
2160 case '-':
2161 next();
2162 n = -expr_unary(mon);
2163 break;
2164 case '~':
2165 next();
2166 n = ~expr_unary(mon);
2167 break;
2168 case '(':
2169 next();
2170 n = expr_sum(mon);
2171 if (*pch != ')') {
2172 expr_error(mon, "')' expected");
2174 next();
2175 break;
2176 case '\'':
2177 pch++;
2178 if (*pch == '\0')
2179 expr_error(mon, "character constant expected");
2180 n = *pch;
2181 pch++;
2182 if (*pch != '\'')
2183 expr_error(mon, "missing terminating \' character");
2184 next();
2185 break;
2186 case '$':
2188 char buf[128], *q;
2189 target_long reg=0;
2191 pch++;
2192 q = buf;
2193 while ((*pch >= 'a' && *pch <= 'z') ||
2194 (*pch >= 'A' && *pch <= 'Z') ||
2195 (*pch >= '0' && *pch <= '9') ||
2196 *pch == '_' || *pch == '.') {
2197 if ((q - buf) < sizeof(buf) - 1)
2198 *q++ = *pch;
2199 pch++;
2201 while (qemu_isspace(*pch))
2202 pch++;
2203 *q = 0;
2204 ret = get_monitor_def(&reg, buf);
2205 if (ret < 0)
2206 expr_error(mon, "unknown register");
2207 n = reg;
2209 break;
2210 case '\0':
2211 expr_error(mon, "unexpected end of expression");
2212 n = 0;
2213 break;
2214 default:
2215 errno = 0;
2216 n = strtoull(pch, &p, 0);
2217 if (errno == ERANGE) {
2218 expr_error(mon, "number too large");
2220 if (pch == p) {
2221 expr_error(mon, "invalid char '%c' in expression", *p);
2223 pch = p;
2224 while (qemu_isspace(*pch))
2225 pch++;
2226 break;
2228 return n;
2232 static int64_t expr_prod(Monitor *mon)
2234 int64_t val, val2;
2235 int op;
2237 val = expr_unary(mon);
2238 for(;;) {
2239 op = *pch;
2240 if (op != '*' && op != '/' && op != '%')
2241 break;
2242 next();
2243 val2 = expr_unary(mon);
2244 switch(op) {
2245 default:
2246 case '*':
2247 val *= val2;
2248 break;
2249 case '/':
2250 case '%':
2251 if (val2 == 0)
2252 expr_error(mon, "division by zero");
2253 if (op == '/')
2254 val /= val2;
2255 else
2256 val %= val2;
2257 break;
2260 return val;
2263 static int64_t expr_logic(Monitor *mon)
2265 int64_t val, val2;
2266 int op;
2268 val = expr_prod(mon);
2269 for(;;) {
2270 op = *pch;
2271 if (op != '&' && op != '|' && op != '^')
2272 break;
2273 next();
2274 val2 = expr_prod(mon);
2275 switch(op) {
2276 default:
2277 case '&':
2278 val &= val2;
2279 break;
2280 case '|':
2281 val |= val2;
2282 break;
2283 case '^':
2284 val ^= val2;
2285 break;
2288 return val;
2291 static int64_t expr_sum(Monitor *mon)
2293 int64_t val, val2;
2294 int op;
2296 val = expr_logic(mon);
2297 for(;;) {
2298 op = *pch;
2299 if (op != '+' && op != '-')
2300 break;
2301 next();
2302 val2 = expr_logic(mon);
2303 if (op == '+')
2304 val += val2;
2305 else
2306 val -= val2;
2308 return val;
2311 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2313 pch = *pp;
2314 if (sigsetjmp(expr_env, 0)) {
2315 *pp = pch;
2316 return -1;
2318 while (qemu_isspace(*pch))
2319 pch++;
2320 *pval = expr_sum(mon);
2321 *pp = pch;
2322 return 0;
2325 static int get_double(Monitor *mon, double *pval, const char **pp)
2327 const char *p = *pp;
2328 char *tailp;
2329 double d;
2331 d = strtod(p, &tailp);
2332 if (tailp == p) {
2333 monitor_printf(mon, "Number expected\n");
2334 return -1;
2336 if (d != d || d - d != 0) {
2337 /* NaN or infinity */
2338 monitor_printf(mon, "Bad number\n");
2339 return -1;
2341 *pval = d;
2342 *pp = tailp;
2343 return 0;
2347 * Store the command-name in cmdname, and return a pointer to
2348 * the remaining of the command string.
2350 static const char *get_command_name(const char *cmdline,
2351 char *cmdname, size_t nlen)
2353 size_t len;
2354 const char *p, *pstart;
2356 p = cmdline;
2357 while (qemu_isspace(*p))
2358 p++;
2359 if (*p == '\0')
2360 return NULL;
2361 pstart = p;
2362 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2363 p++;
2364 len = p - pstart;
2365 if (len > nlen - 1)
2366 len = nlen - 1;
2367 memcpy(cmdname, pstart, len);
2368 cmdname[len] = '\0';
2369 return p;
2373 * Read key of 'type' into 'key' and return the current
2374 * 'type' pointer.
2376 static char *key_get_info(const char *type, char **key)
2378 size_t len;
2379 char *p, *str;
2381 if (*type == ',')
2382 type++;
2384 p = strchr(type, ':');
2385 if (!p) {
2386 *key = NULL;
2387 return NULL;
2389 len = p - type;
2391 str = g_malloc(len + 1);
2392 memcpy(str, type, len);
2393 str[len] = '\0';
2395 *key = str;
2396 return ++p;
2399 static int default_fmt_format = 'x';
2400 static int default_fmt_size = 4;
2402 static int is_valid_option(const char *c, const char *typestr)
2404 char option[3];
2406 option[0] = '-';
2407 option[1] = *c;
2408 option[2] = '\0';
2410 typestr = strstr(typestr, option);
2411 return (typestr != NULL);
2414 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2415 const char *cmdname)
2417 const mon_cmd_t *cmd;
2419 for (cmd = disp_table; cmd->name != NULL; cmd++) {
2420 if (compare_cmd(cmdname, cmd->name)) {
2421 return cmd;
2425 return NULL;
2428 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
2430 return search_dispatch_table(qmp_cmds, cmdname);
2434 * Parse command name from @cmdp according to command table @table.
2435 * If blank, return NULL.
2436 * Else, if no valid command can be found, report to @mon, and return
2437 * NULL.
2438 * Else, change @cmdp to point right behind the name, and return its
2439 * command table entry.
2440 * Do not assume the return value points into @table! It doesn't when
2441 * the command is found in a sub-command table.
2443 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2444 const char **cmdp,
2445 mon_cmd_t *table)
2447 const char *p;
2448 const mon_cmd_t *cmd;
2449 char cmdname[256];
2451 /* extract the command name */
2452 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
2453 if (!p)
2454 return NULL;
2456 cmd = search_dispatch_table(table, cmdname);
2457 if (!cmd) {
2458 monitor_printf(mon, "unknown command: '%.*s'\n",
2459 (int)(p - *cmdp), *cmdp);
2460 return NULL;
2463 /* filter out following useless space */
2464 while (qemu_isspace(*p)) {
2465 p++;
2468 *cmdp = p;
2469 /* search sub command */
2470 if (cmd->sub_table != NULL && *p != '\0') {
2471 return monitor_parse_command(mon, cmdp, cmd->sub_table);
2474 return cmd;
2478 * Parse arguments for @cmd.
2479 * If it can't be parsed, report to @mon, and return NULL.
2480 * Else, insert command arguments into a QDict, and return it.
2481 * Note: On success, caller has to free the QDict structure.
2484 static QDict *monitor_parse_arguments(Monitor *mon,
2485 const char **endp,
2486 const mon_cmd_t *cmd)
2488 const char *typestr;
2489 char *key;
2490 int c;
2491 const char *p = *endp;
2492 char buf[1024];
2493 QDict *qdict = qdict_new();
2495 /* parse the parameters */
2496 typestr = cmd->args_type;
2497 for(;;) {
2498 typestr = key_get_info(typestr, &key);
2499 if (!typestr)
2500 break;
2501 c = *typestr;
2502 typestr++;
2503 switch(c) {
2504 case 'F':
2505 case 'B':
2506 case 's':
2508 int ret;
2510 while (qemu_isspace(*p))
2511 p++;
2512 if (*typestr == '?') {
2513 typestr++;
2514 if (*p == '\0') {
2515 /* no optional string: NULL argument */
2516 break;
2519 ret = get_str(buf, sizeof(buf), &p);
2520 if (ret < 0) {
2521 switch(c) {
2522 case 'F':
2523 monitor_printf(mon, "%s: filename expected\n",
2524 cmd->name);
2525 break;
2526 case 'B':
2527 monitor_printf(mon, "%s: block device name expected\n",
2528 cmd->name);
2529 break;
2530 default:
2531 monitor_printf(mon, "%s: string expected\n", cmd->name);
2532 break;
2534 goto fail;
2536 qdict_put(qdict, key, qstring_from_str(buf));
2538 break;
2539 case 'O':
2541 QemuOptsList *opts_list;
2542 QemuOpts *opts;
2544 opts_list = qemu_find_opts(key);
2545 if (!opts_list || opts_list->desc->name) {
2546 goto bad_type;
2548 while (qemu_isspace(*p)) {
2549 p++;
2551 if (!*p)
2552 break;
2553 if (get_str(buf, sizeof(buf), &p) < 0) {
2554 goto fail;
2556 opts = qemu_opts_parse_noisily(opts_list, buf, true);
2557 if (!opts) {
2558 goto fail;
2560 qemu_opts_to_qdict(opts, qdict);
2561 qemu_opts_del(opts);
2563 break;
2564 case '/':
2566 int count, format, size;
2568 while (qemu_isspace(*p))
2569 p++;
2570 if (*p == '/') {
2571 /* format found */
2572 p++;
2573 count = 1;
2574 if (qemu_isdigit(*p)) {
2575 count = 0;
2576 while (qemu_isdigit(*p)) {
2577 count = count * 10 + (*p - '0');
2578 p++;
2581 size = -1;
2582 format = -1;
2583 for(;;) {
2584 switch(*p) {
2585 case 'o':
2586 case 'd':
2587 case 'u':
2588 case 'x':
2589 case 'i':
2590 case 'c':
2591 format = *p++;
2592 break;
2593 case 'b':
2594 size = 1;
2595 p++;
2596 break;
2597 case 'h':
2598 size = 2;
2599 p++;
2600 break;
2601 case 'w':
2602 size = 4;
2603 p++;
2604 break;
2605 case 'g':
2606 case 'L':
2607 size = 8;
2608 p++;
2609 break;
2610 default:
2611 goto next;
2614 next:
2615 if (*p != '\0' && !qemu_isspace(*p)) {
2616 monitor_printf(mon, "invalid char in format: '%c'\n",
2617 *p);
2618 goto fail;
2620 if (format < 0)
2621 format = default_fmt_format;
2622 if (format != 'i') {
2623 /* for 'i', not specifying a size gives -1 as size */
2624 if (size < 0)
2625 size = default_fmt_size;
2626 default_fmt_size = size;
2628 default_fmt_format = format;
2629 } else {
2630 count = 1;
2631 format = default_fmt_format;
2632 if (format != 'i') {
2633 size = default_fmt_size;
2634 } else {
2635 size = -1;
2638 qdict_put(qdict, "count", qint_from_int(count));
2639 qdict_put(qdict, "format", qint_from_int(format));
2640 qdict_put(qdict, "size", qint_from_int(size));
2642 break;
2643 case 'i':
2644 case 'l':
2645 case 'M':
2647 int64_t val;
2649 while (qemu_isspace(*p))
2650 p++;
2651 if (*typestr == '?' || *typestr == '.') {
2652 if (*typestr == '?') {
2653 if (*p == '\0') {
2654 typestr++;
2655 break;
2657 } else {
2658 if (*p == '.') {
2659 p++;
2660 while (qemu_isspace(*p))
2661 p++;
2662 } else {
2663 typestr++;
2664 break;
2667 typestr++;
2669 if (get_expr(mon, &val, &p))
2670 goto fail;
2671 /* Check if 'i' is greater than 32-bit */
2672 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2673 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
2674 monitor_printf(mon, "integer is for 32-bit values\n");
2675 goto fail;
2676 } else if (c == 'M') {
2677 if (val < 0) {
2678 monitor_printf(mon, "enter a positive value\n");
2679 goto fail;
2681 val <<= 20;
2683 qdict_put(qdict, key, qint_from_int(val));
2685 break;
2686 case 'o':
2688 int64_t val;
2689 char *end;
2691 while (qemu_isspace(*p)) {
2692 p++;
2694 if (*typestr == '?') {
2695 typestr++;
2696 if (*p == '\0') {
2697 break;
2700 val = strtosz(p, &end);
2701 if (val < 0) {
2702 monitor_printf(mon, "invalid size\n");
2703 goto fail;
2705 qdict_put(qdict, key, qint_from_int(val));
2706 p = end;
2708 break;
2709 case 'T':
2711 double val;
2713 while (qemu_isspace(*p))
2714 p++;
2715 if (*typestr == '?') {
2716 typestr++;
2717 if (*p == '\0') {
2718 break;
2721 if (get_double(mon, &val, &p) < 0) {
2722 goto fail;
2724 if (p[0] && p[1] == 's') {
2725 switch (*p) {
2726 case 'm':
2727 val /= 1e3; p += 2; break;
2728 case 'u':
2729 val /= 1e6; p += 2; break;
2730 case 'n':
2731 val /= 1e9; p += 2; break;
2734 if (*p && !qemu_isspace(*p)) {
2735 monitor_printf(mon, "Unknown unit suffix\n");
2736 goto fail;
2738 qdict_put(qdict, key, qfloat_from_double(val));
2740 break;
2741 case 'b':
2743 const char *beg;
2744 bool val;
2746 while (qemu_isspace(*p)) {
2747 p++;
2749 beg = p;
2750 while (qemu_isgraph(*p)) {
2751 p++;
2753 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
2754 val = true;
2755 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
2756 val = false;
2757 } else {
2758 monitor_printf(mon, "Expected 'on' or 'off'\n");
2759 goto fail;
2761 qdict_put(qdict, key, qbool_from_bool(val));
2763 break;
2764 case '-':
2766 const char *tmp = p;
2767 int skip_key = 0;
2768 /* option */
2770 c = *typestr++;
2771 if (c == '\0')
2772 goto bad_type;
2773 while (qemu_isspace(*p))
2774 p++;
2775 if (*p == '-') {
2776 p++;
2777 if(c != *p) {
2778 if(!is_valid_option(p, typestr)) {
2780 monitor_printf(mon, "%s: unsupported option -%c\n",
2781 cmd->name, *p);
2782 goto fail;
2783 } else {
2784 skip_key = 1;
2787 if(skip_key) {
2788 p = tmp;
2789 } else {
2790 /* has option */
2791 p++;
2792 qdict_put(qdict, key, qbool_from_bool(true));
2796 break;
2797 case 'S':
2799 /* package all remaining string */
2800 int len;
2802 while (qemu_isspace(*p)) {
2803 p++;
2805 if (*typestr == '?') {
2806 typestr++;
2807 if (*p == '\0') {
2808 /* no remaining string: NULL argument */
2809 break;
2812 len = strlen(p);
2813 if (len <= 0) {
2814 monitor_printf(mon, "%s: string expected\n",
2815 cmd->name);
2816 goto fail;
2818 qdict_put(qdict, key, qstring_from_str(p));
2819 p += len;
2821 break;
2822 default:
2823 bad_type:
2824 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
2825 goto fail;
2827 g_free(key);
2828 key = NULL;
2830 /* check that all arguments were parsed */
2831 while (qemu_isspace(*p))
2832 p++;
2833 if (*p != '\0') {
2834 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2835 cmd->name);
2836 goto fail;
2839 return qdict;
2841 fail:
2842 QDECREF(qdict);
2843 g_free(key);
2844 return NULL;
2847 static void handle_hmp_command(Monitor *mon, const char *cmdline)
2849 QDict *qdict;
2850 const mon_cmd_t *cmd;
2852 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2853 if (!cmd) {
2854 return;
2857 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2858 if (!qdict) {
2859 monitor_printf(mon, "Try \"help %s\" for more information\n",
2860 cmd->name);
2861 return;
2864 cmd->mhandler.cmd(mon, qdict);
2865 QDECREF(qdict);
2868 static void cmd_completion(Monitor *mon, const char *name, const char *list)
2870 const char *p, *pstart;
2871 char cmd[128];
2872 int len;
2874 p = list;
2875 for(;;) {
2876 pstart = p;
2877 p = strchr(p, '|');
2878 if (!p)
2879 p = pstart + strlen(pstart);
2880 len = p - pstart;
2881 if (len > sizeof(cmd) - 2)
2882 len = sizeof(cmd) - 2;
2883 memcpy(cmd, pstart, len);
2884 cmd[len] = '\0';
2885 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2886 readline_add_completion(mon->rs, cmd);
2888 if (*p == '\0')
2889 break;
2890 p++;
2894 static void file_completion(Monitor *mon, const char *input)
2896 DIR *ffs;
2897 struct dirent *d;
2898 char path[1024];
2899 char file[1024], file_prefix[1024];
2900 int input_path_len;
2901 const char *p;
2903 p = strrchr(input, '/');
2904 if (!p) {
2905 input_path_len = 0;
2906 pstrcpy(file_prefix, sizeof(file_prefix), input);
2907 pstrcpy(path, sizeof(path), ".");
2908 } else {
2909 input_path_len = p - input + 1;
2910 memcpy(path, input, input_path_len);
2911 if (input_path_len > sizeof(path) - 1)
2912 input_path_len = sizeof(path) - 1;
2913 path[input_path_len] = '\0';
2914 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2917 ffs = opendir(path);
2918 if (!ffs)
2919 return;
2920 for(;;) {
2921 struct stat sb;
2922 d = readdir(ffs);
2923 if (!d)
2924 break;
2926 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
2927 continue;
2930 if (strstart(d->d_name, file_prefix, NULL)) {
2931 memcpy(file, input, input_path_len);
2932 if (input_path_len < sizeof(file))
2933 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2934 d->d_name);
2935 /* stat the file to find out if it's a directory.
2936 * In that case add a slash to speed up typing long paths
2938 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
2939 pstrcat(file, sizeof(file), "/");
2941 readline_add_completion(mon->rs, file);
2944 closedir(ffs);
2947 static const char *next_arg_type(const char *typestr)
2949 const char *p = strchr(typestr, ':');
2950 return (p != NULL ? ++p : typestr);
2953 static void add_completion_option(ReadLineState *rs, const char *str,
2954 const char *option)
2956 if (!str || !option) {
2957 return;
2959 if (!strncmp(option, str, strlen(str))) {
2960 readline_add_completion(rs, option);
2964 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2966 size_t len;
2967 ChardevBackendInfoList *list, *start;
2969 if (nb_args != 2) {
2970 return;
2972 len = strlen(str);
2973 readline_set_completion_index(rs, len);
2975 start = list = qmp_query_chardev_backends(NULL);
2976 while (list) {
2977 const char *chr_name = list->value->name;
2979 if (!strncmp(chr_name, str, len)) {
2980 readline_add_completion(rs, chr_name);
2982 list = list->next;
2984 qapi_free_ChardevBackendInfoList(start);
2987 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
2989 size_t len;
2990 int i;
2992 if (nb_args != 2) {
2993 return;
2995 len = strlen(str);
2996 readline_set_completion_index(rs, len);
2997 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
2998 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
3002 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
3004 GSList *list, *elt;
3005 size_t len;
3007 if (nb_args != 2) {
3008 return;
3011 len = strlen(str);
3012 readline_set_completion_index(rs, len);
3013 list = elt = object_class_get_list(TYPE_DEVICE, false);
3014 while (elt) {
3015 const char *name;
3016 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3017 TYPE_DEVICE);
3018 name = object_class_get_name(OBJECT_CLASS(dc));
3020 if (!dc->cannot_instantiate_with_device_add_yet
3021 && !strncmp(name, str, len)) {
3022 readline_add_completion(rs, name);
3024 elt = elt->next;
3026 g_slist_free(list);
3029 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
3031 GSList *list, *elt;
3032 size_t len;
3034 if (nb_args != 2) {
3035 return;
3038 len = strlen(str);
3039 readline_set_completion_index(rs, len);
3040 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3041 while (elt) {
3042 const char *name;
3044 name = object_class_get_name(OBJECT_CLASS(elt->data));
3045 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3046 readline_add_completion(rs, name);
3048 elt = elt->next;
3050 g_slist_free(list);
3053 static void peripheral_device_del_completion(ReadLineState *rs,
3054 const char *str, size_t len)
3056 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3057 GSList *list, *item;
3059 list = qdev_build_hotpluggable_device_list(peripheral);
3060 if (!list) {
3061 return;
3064 for (item = list; item; item = g_slist_next(item)) {
3065 DeviceState *dev = item->data;
3067 if (dev->id && !strncmp(str, dev->id, len)) {
3068 readline_add_completion(rs, dev->id);
3072 g_slist_free(list);
3075 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3077 size_t len;
3078 ChardevInfoList *list, *start;
3080 if (nb_args != 2) {
3081 return;
3083 len = strlen(str);
3084 readline_set_completion_index(rs, len);
3086 start = list = qmp_query_chardev(NULL);
3087 while (list) {
3088 ChardevInfo *chr = list->value;
3090 if (!strncmp(chr->label, str, len)) {
3091 readline_add_completion(rs, chr->label);
3093 list = list->next;
3095 qapi_free_ChardevInfoList(start);
3098 static void ringbuf_completion(ReadLineState *rs, const char *str)
3100 size_t len;
3101 ChardevInfoList *list, *start;
3103 len = strlen(str);
3104 readline_set_completion_index(rs, len);
3106 start = list = qmp_query_chardev(NULL);
3107 while (list) {
3108 ChardevInfo *chr_info = list->value;
3110 if (!strncmp(chr_info->label, str, len)) {
3111 CharDriverState *chr = qemu_chr_find(chr_info->label);
3112 if (chr && chr_is_ringbuf(chr)) {
3113 readline_add_completion(rs, chr_info->label);
3116 list = list->next;
3118 qapi_free_ChardevInfoList(start);
3121 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3123 if (nb_args != 2) {
3124 return;
3126 ringbuf_completion(rs, str);
3129 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3131 size_t len;
3133 if (nb_args != 2) {
3134 return;
3137 len = strlen(str);
3138 readline_set_completion_index(rs, len);
3139 peripheral_device_del_completion(rs, str, len);
3142 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
3144 ObjectPropertyInfoList *list, *start;
3145 size_t len;
3147 if (nb_args != 2) {
3148 return;
3150 len = strlen(str);
3151 readline_set_completion_index(rs, len);
3153 start = list = qmp_qom_list("/objects", NULL);
3154 while (list) {
3155 ObjectPropertyInfo *info = list->value;
3157 if (!strncmp(info->type, "child<", 5)
3158 && !strncmp(info->name, str, len)) {
3159 readline_add_completion(rs, info->name);
3161 list = list->next;
3163 qapi_free_ObjectPropertyInfoList(start);
3166 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3168 int i;
3169 char *sep;
3170 size_t len;
3172 if (nb_args != 2) {
3173 return;
3175 sep = strrchr(str, '-');
3176 if (sep) {
3177 str = sep + 1;
3179 len = strlen(str);
3180 readline_set_completion_index(rs, len);
3181 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
3182 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3183 readline_add_completion(rs, QKeyCode_lookup[i]);
3188 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3190 size_t len;
3192 len = strlen(str);
3193 readline_set_completion_index(rs, len);
3194 if (nb_args == 2) {
3195 NetClientState *ncs[MAX_QUEUE_NUM];
3196 int count, i;
3197 count = qemu_find_net_clients_except(NULL, ncs,
3198 NET_CLIENT_OPTIONS_KIND_NONE,
3199 MAX_QUEUE_NUM);
3200 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3201 const char *name = ncs[i]->name;
3202 if (!strncmp(str, name, len)) {
3203 readline_add_completion(rs, name);
3206 } else if (nb_args == 3) {
3207 add_completion_option(rs, str, "on");
3208 add_completion_option(rs, str, "off");
3212 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3214 int len, count, i;
3215 NetClientState *ncs[MAX_QUEUE_NUM];
3217 if (nb_args != 2) {
3218 return;
3221 len = strlen(str);
3222 readline_set_completion_index(rs, len);
3223 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
3224 MAX_QUEUE_NUM);
3225 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3226 QemuOpts *opts;
3227 const char *name = ncs[i]->name;
3228 if (strncmp(str, name, len)) {
3229 continue;
3231 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3232 if (opts) {
3233 readline_add_completion(rs, name);
3238 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3240 size_t len;
3242 len = strlen(str);
3243 readline_set_completion_index(rs, len);
3244 if (nb_args == 2) {
3245 TraceEventID id;
3246 for (id = 0; id < trace_event_count(); id++) {
3247 const char *event_name = trace_event_get_name(trace_event_id(id));
3248 if (!strncmp(str, event_name, len)) {
3249 readline_add_completion(rs, event_name);
3252 } else if (nb_args == 3) {
3253 add_completion_option(rs, str, "on");
3254 add_completion_option(rs, str, "off");
3258 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3260 int i;
3262 if (nb_args != 2) {
3263 return;
3265 readline_set_completion_index(rs, strlen(str));
3266 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3267 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3271 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3272 const char *str)
3274 size_t len;
3276 len = strlen(str);
3277 readline_set_completion_index(rs, len);
3278 if (nb_args == 2) {
3279 int i;
3280 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
3281 const char *name = MigrationCapability_lookup[i];
3282 if (!strncmp(str, name, len)) {
3283 readline_add_completion(rs, name);
3286 } else if (nb_args == 3) {
3287 add_completion_option(rs, str, "on");
3288 add_completion_option(rs, str, "off");
3292 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3293 const char *str)
3295 size_t len;
3297 len = strlen(str);
3298 readline_set_completion_index(rs, len);
3299 if (nb_args == 2) {
3300 int i;
3301 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
3302 const char *name = MigrationParameter_lookup[i];
3303 if (!strncmp(str, name, len)) {
3304 readline_add_completion(rs, name);
3310 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3312 int i;
3313 size_t len;
3314 if (nb_args != 2) {
3315 return;
3317 len = strlen(str);
3318 readline_set_completion_index(rs, len);
3319 for (i = 0; host_net_devices[i]; i++) {
3320 if (!strncmp(host_net_devices[i], str, len)) {
3321 readline_add_completion(rs, host_net_devices[i]);
3326 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3328 NetClientState *ncs[MAX_QUEUE_NUM];
3329 int count, i, len;
3331 len = strlen(str);
3332 readline_set_completion_index(rs, len);
3333 if (nb_args == 2) {
3334 count = qemu_find_net_clients_except(NULL, ncs,
3335 NET_CLIENT_OPTIONS_KIND_NONE,
3336 MAX_QUEUE_NUM);
3337 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3338 int id;
3339 char name[16];
3341 if (net_hub_id_for_client(ncs[i], &id)) {
3342 continue;
3344 snprintf(name, sizeof(name), "%d", id);
3345 if (!strncmp(str, name, len)) {
3346 readline_add_completion(rs, name);
3349 return;
3350 } else if (nb_args == 3) {
3351 count = qemu_find_net_clients_except(NULL, ncs,
3352 NET_CLIENT_OPTIONS_KIND_NIC,
3353 MAX_QUEUE_NUM);
3354 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3355 int id;
3356 const char *name;
3358 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
3359 net_hub_id_for_client(ncs[i], &id)) {
3360 continue;
3362 name = ncs[i]->name;
3363 if (!strncmp(str, name, len)) {
3364 readline_add_completion(rs, name);
3367 return;
3371 static void vm_completion(ReadLineState *rs, const char *str)
3373 size_t len;
3374 BlockDriverState *bs = NULL;
3376 len = strlen(str);
3377 readline_set_completion_index(rs, len);
3378 while ((bs = bdrv_next(bs))) {
3379 SnapshotInfoList *snapshots, *snapshot;
3381 if (!bdrv_can_snapshot(bs)) {
3382 continue;
3384 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
3385 continue;
3387 snapshot = snapshots;
3388 while (snapshot) {
3389 char *completion = snapshot->value->name;
3390 if (!strncmp(str, completion, len)) {
3391 readline_add_completion(rs, completion);
3393 completion = snapshot->value->id;
3394 if (!strncmp(str, completion, len)) {
3395 readline_add_completion(rs, completion);
3397 snapshot = snapshot->next;
3399 qapi_free_SnapshotInfoList(snapshots);
3404 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3406 if (nb_args == 2) {
3407 vm_completion(rs, str);
3411 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3413 if (nb_args == 2) {
3414 vm_completion(rs, str);
3418 static void monitor_find_completion_by_table(Monitor *mon,
3419 const mon_cmd_t *cmd_table,
3420 char **args,
3421 int nb_args)
3423 const char *cmdname;
3424 int i;
3425 const char *ptype, *str, *name;
3426 const mon_cmd_t *cmd;
3427 BlockDriverState *bs;
3429 if (nb_args <= 1) {
3430 /* command completion */
3431 if (nb_args == 0)
3432 cmdname = "";
3433 else
3434 cmdname = args[0];
3435 readline_set_completion_index(mon->rs, strlen(cmdname));
3436 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3437 cmd_completion(mon, cmdname, cmd->name);
3439 } else {
3440 /* find the command */
3441 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3442 if (compare_cmd(args[0], cmd->name)) {
3443 break;
3446 if (!cmd->name) {
3447 return;
3450 if (cmd->sub_table) {
3451 /* do the job again */
3452 monitor_find_completion_by_table(mon, cmd->sub_table,
3453 &args[1], nb_args - 1);
3454 return;
3456 if (cmd->command_completion) {
3457 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3458 return;
3461 ptype = next_arg_type(cmd->args_type);
3462 for(i = 0; i < nb_args - 2; i++) {
3463 if (*ptype != '\0') {
3464 ptype = next_arg_type(ptype);
3465 while (*ptype == '?')
3466 ptype = next_arg_type(ptype);
3469 str = args[nb_args - 1];
3470 while (*ptype == '-' && ptype[1] != '\0') {
3471 ptype = next_arg_type(ptype);
3473 switch(*ptype) {
3474 case 'F':
3475 /* file completion */
3476 readline_set_completion_index(mon->rs, strlen(str));
3477 file_completion(mon, str);
3478 break;
3479 case 'B':
3480 /* block device name completion */
3481 readline_set_completion_index(mon->rs, strlen(str));
3482 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
3483 name = bdrv_get_device_name(bs);
3484 if (str[0] == '\0' ||
3485 !strncmp(name, str, strlen(str))) {
3486 readline_add_completion(mon->rs, name);
3489 break;
3490 case 's':
3491 case 'S':
3492 if (!strcmp(cmd->name, "help|?")) {
3493 monitor_find_completion_by_table(mon, cmd_table,
3494 &args[1], nb_args - 1);
3496 break;
3497 default:
3498 break;
3503 static void monitor_find_completion(void *opaque,
3504 const char *cmdline)
3506 Monitor *mon = opaque;
3507 char *args[MAX_ARGS];
3508 int nb_args, len;
3510 /* 1. parse the cmdline */
3511 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3512 return;
3515 /* if the line ends with a space, it means we want to complete the
3516 next arg */
3517 len = strlen(cmdline);
3518 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3519 if (nb_args >= MAX_ARGS) {
3520 goto cleanup;
3522 args[nb_args++] = g_strdup("");
3525 /* 2. auto complete according to args */
3526 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
3528 cleanup:
3529 free_cmdline_args(args, nb_args);
3532 static int monitor_can_read(void *opaque)
3534 Monitor *mon = opaque;
3536 return (mon->suspend_cnt == 0) ? 1 : 0;
3539 static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
3540 Error **errp)
3542 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
3544 if (is_cap && mon->qmp.in_command_mode) {
3545 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3546 "Capabilities negotiation is already complete, command "
3547 "'%s' ignored", cmd->name);
3548 return true;
3550 if (!is_cap && !mon->qmp.in_command_mode) {
3551 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3552 "Expecting capabilities negotiation with "
3553 "'qmp_capabilities' before command '%s'", cmd->name);
3554 return true;
3556 return false;
3560 * Argument validation rules:
3562 * 1. The argument must exist in cmd_args qdict
3563 * 2. The argument type must be the expected one
3565 * Special case: If the argument doesn't exist in cmd_args and
3566 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3567 * checking is skipped for it.
3569 static void check_client_args_type(const QDict *client_args,
3570 const QDict *cmd_args, int flags,
3571 Error **errp)
3573 const QDictEntry *ent;
3575 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
3576 QObject *obj;
3577 QString *arg_type;
3578 const QObject *client_arg = qdict_entry_value(ent);
3579 const char *client_arg_name = qdict_entry_key(ent);
3581 obj = qdict_get(cmd_args, client_arg_name);
3582 if (!obj) {
3583 if (flags & QMP_ACCEPT_UNKNOWNS) {
3584 /* handler accepts unknowns */
3585 continue;
3587 /* client arg doesn't exist */
3588 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
3589 return;
3592 arg_type = qobject_to_qstring(obj);
3593 assert(arg_type != NULL);
3595 /* check if argument's type is correct */
3596 switch (qstring_get_str(arg_type)[0]) {
3597 case 'F':
3598 case 'B':
3599 case 's':
3600 if (qobject_type(client_arg) != QTYPE_QSTRING) {
3601 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3602 client_arg_name, "string");
3603 return;
3605 break;
3606 case 'i':
3607 case 'l':
3608 case 'M':
3609 case 'o':
3610 if (qobject_type(client_arg) != QTYPE_QINT) {
3611 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3612 client_arg_name, "int");
3613 return;
3615 break;
3616 case 'T':
3617 if (qobject_type(client_arg) != QTYPE_QINT &&
3618 qobject_type(client_arg) != QTYPE_QFLOAT) {
3619 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3620 client_arg_name, "number");
3621 return;
3623 break;
3624 case 'b':
3625 case '-':
3626 if (qobject_type(client_arg) != QTYPE_QBOOL) {
3627 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
3628 client_arg_name, "bool");
3629 return;
3631 break;
3632 case 'O':
3633 assert(flags & QMP_ACCEPT_UNKNOWNS);
3634 break;
3635 case 'q':
3636 /* Any QObject can be passed. */
3637 break;
3638 case '/':
3639 case '.':
3641 * These types are not supported by QMP and thus are not
3642 * handled here. Fall through.
3644 default:
3645 abort();
3651 * - Check if the client has passed all mandatory args
3652 * - Set special flags for argument validation
3654 static void check_mandatory_args(const QDict *cmd_args,
3655 const QDict *client_args, int *flags,
3656 Error **errp)
3658 const QDictEntry *ent;
3660 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
3661 const char *cmd_arg_name = qdict_entry_key(ent);
3662 QString *type = qobject_to_qstring(qdict_entry_value(ent));
3663 assert(type != NULL);
3665 if (qstring_get_str(type)[0] == 'O') {
3666 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
3667 *flags |= QMP_ACCEPT_UNKNOWNS;
3668 } else if (qstring_get_str(type)[0] != '-' &&
3669 qstring_get_str(type)[1] != '?' &&
3670 !qdict_haskey(client_args, cmd_arg_name)) {
3671 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
3672 return;
3677 static QDict *qdict_from_args_type(const char *args_type)
3679 int i;
3680 QDict *qdict;
3681 QString *key, *type, *cur_qs;
3683 assert(args_type != NULL);
3685 qdict = qdict_new();
3687 if (args_type == NULL || args_type[0] == '\0') {
3688 /* no args, empty qdict */
3689 goto out;
3692 key = qstring_new();
3693 type = qstring_new();
3695 cur_qs = key;
3697 for (i = 0;; i++) {
3698 switch (args_type[i]) {
3699 case ',':
3700 case '\0':
3701 qdict_put(qdict, qstring_get_str(key), type);
3702 QDECREF(key);
3703 if (args_type[i] == '\0') {
3704 goto out;
3706 type = qstring_new(); /* qdict has ref */
3707 cur_qs = key = qstring_new();
3708 break;
3709 case ':':
3710 cur_qs = type;
3711 break;
3712 default:
3713 qstring_append_chr(cur_qs, args_type[i]);
3714 break;
3718 out:
3719 return qdict;
3723 * Client argument checking rules:
3725 * 1. Client must provide all mandatory arguments
3726 * 2. Each argument provided by the client must be expected
3727 * 3. Each argument provided by the client must have the type expected
3728 * by the command
3730 static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
3731 Error **errp)
3733 Error *err = NULL;
3734 int flags;
3735 QDict *cmd_args;
3737 cmd_args = qdict_from_args_type(cmd->args_type);
3739 flags = 0;
3740 check_mandatory_args(cmd_args, client_args, &flags, &err);
3741 if (err) {
3742 goto out;
3745 check_client_args_type(client_args, cmd_args, flags, &err);
3747 out:
3748 error_propagate(errp, err);
3749 QDECREF(cmd_args);
3753 * Input object checking rules
3755 * 1. Input object must be a dict
3756 * 2. The "execute" key must exist
3757 * 3. The "execute" key must be a string
3758 * 4. If the "arguments" key exists, it must be a dict
3759 * 5. If the "id" key exists, it can be anything (ie. json-value)
3760 * 6. Any argument not listed above is considered invalid
3762 static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
3764 const QDictEntry *ent;
3765 int has_exec_key = 0;
3766 QDict *input_dict;
3768 if (qobject_type(input_obj) != QTYPE_QDICT) {
3769 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
3770 return NULL;
3773 input_dict = qobject_to_qdict(input_obj);
3775 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3776 const char *arg_name = qdict_entry_key(ent);
3777 const QObject *arg_obj = qdict_entry_value(ent);
3779 if (!strcmp(arg_name, "execute")) {
3780 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
3781 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3782 "execute", "string");
3783 return NULL;
3785 has_exec_key = 1;
3786 } else if (!strcmp(arg_name, "arguments")) {
3787 if (qobject_type(arg_obj) != QTYPE_QDICT) {
3788 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3789 "arguments", "object");
3790 return NULL;
3792 } else if (!strcmp(arg_name, "id")) {
3793 /* Any string is acceptable as "id", so nothing to check */
3794 } else {
3795 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
3796 return NULL;
3800 if (!has_exec_key) {
3801 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
3802 return NULL;
3805 return input_dict;
3808 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
3810 Error *local_err = NULL;
3811 QObject *obj, *data;
3812 QDict *input, *args;
3813 const mon_cmd_t *cmd;
3814 const char *cmd_name;
3815 Monitor *mon = cur_mon;
3817 args = input = NULL;
3818 data = NULL;
3820 obj = json_parser_parse(tokens, NULL);
3821 if (!obj) {
3822 // FIXME: should be triggered in json_parser_parse()
3823 error_setg(&local_err, QERR_JSON_PARSING);
3824 goto err_out;
3827 input = qmp_check_input_obj(obj, &local_err);
3828 if (!input) {
3829 qobject_decref(obj);
3830 goto err_out;
3833 mon->qmp.id = qdict_get(input, "id");
3834 qobject_incref(mon->qmp.id);
3836 cmd_name = qdict_get_str(input, "execute");
3837 trace_handle_qmp_command(mon, cmd_name);
3838 cmd = qmp_find_cmd(cmd_name);
3839 if (!cmd) {
3840 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
3841 "The command %s has not been found", cmd_name);
3842 goto err_out;
3844 if (invalid_qmp_mode(mon, cmd, &local_err)) {
3845 goto err_out;
3848 obj = qdict_get(input, "arguments");
3849 if (!obj) {
3850 args = qdict_new();
3851 } else {
3852 args = qobject_to_qdict(obj);
3853 QINCREF(args);
3856 qmp_check_client_args(cmd, args, &local_err);
3857 if (local_err) {
3858 goto err_out;
3861 cmd->mhandler.cmd_new(args, &data, &local_err);
3863 err_out:
3864 monitor_protocol_emitter(mon, data, local_err);
3865 qobject_decref(data);
3866 QDECREF(input);
3867 QDECREF(args);
3870 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
3872 Monitor *old_mon = cur_mon;
3874 cur_mon = opaque;
3876 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
3878 cur_mon = old_mon;
3881 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3883 Monitor *old_mon = cur_mon;
3884 int i;
3886 cur_mon = opaque;
3888 if (cur_mon->rs) {
3889 for (i = 0; i < size; i++)
3890 readline_handle_byte(cur_mon->rs, buf[i]);
3891 } else {
3892 if (size == 0 || buf[size - 1] != 0)
3893 monitor_printf(cur_mon, "corrupted command\n");
3894 else
3895 handle_hmp_command(cur_mon, (char *)buf);
3898 cur_mon = old_mon;
3901 static void monitor_command_cb(void *opaque, const char *cmdline,
3902 void *readline_opaque)
3904 Monitor *mon = opaque;
3906 monitor_suspend(mon);
3907 handle_hmp_command(mon, cmdline);
3908 monitor_resume(mon);
3911 int monitor_suspend(Monitor *mon)
3913 if (!mon->rs)
3914 return -ENOTTY;
3915 mon->suspend_cnt++;
3916 return 0;
3919 void monitor_resume(Monitor *mon)
3921 if (!mon->rs)
3922 return;
3923 if (--mon->suspend_cnt == 0)
3924 readline_show_prompt(mon->rs);
3927 static QObject *get_qmp_greeting(void)
3929 QObject *ver = NULL;
3931 qmp_marshal_query_version(NULL, &ver, NULL);
3932 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
3935 static void monitor_qmp_event(void *opaque, int event)
3937 QObject *data;
3938 Monitor *mon = opaque;
3940 switch (event) {
3941 case CHR_EVENT_OPENED:
3942 mon->qmp.in_command_mode = false;
3943 data = get_qmp_greeting();
3944 monitor_json_emitter(mon, data);
3945 qobject_decref(data);
3946 mon_refcount++;
3947 break;
3948 case CHR_EVENT_CLOSED:
3949 json_message_parser_destroy(&mon->qmp.parser);
3950 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
3951 mon_refcount--;
3952 monitor_fdsets_cleanup();
3953 break;
3957 static void monitor_event(void *opaque, int event)
3959 Monitor *mon = opaque;
3961 switch (event) {
3962 case CHR_EVENT_MUX_IN:
3963 qemu_mutex_lock(&mon->out_lock);
3964 mon->mux_out = 0;
3965 qemu_mutex_unlock(&mon->out_lock);
3966 if (mon->reset_seen) {
3967 readline_restart(mon->rs);
3968 monitor_resume(mon);
3969 monitor_flush(mon);
3970 } else {
3971 mon->suspend_cnt = 0;
3973 break;
3975 case CHR_EVENT_MUX_OUT:
3976 if (mon->reset_seen) {
3977 if (mon->suspend_cnt == 0) {
3978 monitor_printf(mon, "\n");
3980 monitor_flush(mon);
3981 monitor_suspend(mon);
3982 } else {
3983 mon->suspend_cnt++;
3985 qemu_mutex_lock(&mon->out_lock);
3986 mon->mux_out = 1;
3987 qemu_mutex_unlock(&mon->out_lock);
3988 break;
3990 case CHR_EVENT_OPENED:
3991 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3992 "information\n", QEMU_VERSION);
3993 if (!mon->mux_out) {
3994 readline_restart(mon->rs);
3995 readline_show_prompt(mon->rs);
3997 mon->reset_seen = 1;
3998 mon_refcount++;
3999 break;
4001 case CHR_EVENT_CLOSED:
4002 mon_refcount--;
4003 monitor_fdsets_cleanup();
4004 break;
4008 static int
4009 compare_mon_cmd(const void *a, const void *b)
4011 return strcmp(((const mon_cmd_t *)a)->name,
4012 ((const mon_cmd_t *)b)->name);
4015 static void sortcmdlist(void)
4017 int array_num;
4018 int elem_size = sizeof(mon_cmd_t);
4020 array_num = sizeof(mon_cmds)/elem_size-1;
4021 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4023 array_num = sizeof(info_cmds)/elem_size-1;
4024 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4029 * Local variables:
4030 * c-indent-level: 4
4031 * c-basic-offset: 4
4032 * tab-width: 8
4033 * End:
4036 /* These functions just adapt the readline interface in a typesafe way. We
4037 * could cast function pointers but that discards compiler checks.
4039 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
4040 const char *fmt, ...)
4042 va_list ap;
4043 va_start(ap, fmt);
4044 monitor_vprintf(opaque, fmt, ap);
4045 va_end(ap);
4048 static void monitor_readline_flush(void *opaque)
4050 monitor_flush(opaque);
4053 static void __attribute__((constructor)) monitor_lock_init(void)
4055 qemu_mutex_init(&monitor_lock);
4058 void monitor_init(CharDriverState *chr, int flags)
4060 static int is_first_init = 1;
4061 Monitor *mon;
4063 if (is_first_init) {
4064 monitor_qapi_event_init();
4065 sortcmdlist();
4066 is_first_init = 0;
4069 mon = g_malloc(sizeof(*mon));
4070 monitor_data_init(mon);
4072 mon->chr = chr;
4073 mon->flags = flags;
4074 if (flags & MONITOR_USE_READLINE) {
4075 mon->rs = readline_init(monitor_readline_printf,
4076 monitor_readline_flush,
4077 mon,
4078 monitor_find_completion);
4079 monitor_read_command(mon, 0);
4082 if (monitor_is_qmp(mon)) {
4083 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
4084 monitor_qmp_event, mon);
4085 qemu_chr_fe_set_echo(chr, true);
4086 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
4087 } else {
4088 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4089 monitor_event, mon);
4092 qemu_mutex_lock(&monitor_lock);
4093 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4094 qemu_mutex_unlock(&monitor_lock);
4097 static void bdrv_password_cb(void *opaque, const char *password,
4098 void *readline_opaque)
4100 Monitor *mon = opaque;
4101 BlockDriverState *bs = readline_opaque;
4102 int ret = 0;
4103 Error *local_err = NULL;
4105 bdrv_add_key(bs, password, &local_err);
4106 if (local_err) {
4107 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
4108 error_free(local_err);
4109 ret = -EPERM;
4111 if (mon->password_completion_cb)
4112 mon->password_completion_cb(mon->password_opaque, ret);
4114 monitor_read_command(mon, 1);
4117 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4118 BlockCompletionFunc *completion_cb,
4119 void *opaque)
4121 int err;
4123 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4124 bdrv_get_encrypted_filename(bs));
4126 mon->password_completion_cb = completion_cb;
4127 mon->password_opaque = opaque;
4129 err = monitor_read_password(mon, bdrv_password_cb, bs);
4131 if (err && completion_cb)
4132 completion_cb(opaque, err);
4134 return err;
4137 int monitor_read_block_device_key(Monitor *mon, const char *device,
4138 BlockCompletionFunc *completion_cb,
4139 void *opaque)
4141 Error *err = NULL;
4142 BlockBackend *blk;
4144 blk = blk_by_name(device);
4145 if (!blk) {
4146 monitor_printf(mon, "Device not found %s\n", device);
4147 return -1;
4150 bdrv_add_key(blk_bs(blk), NULL, &err);
4151 if (err) {
4152 error_free(err);
4153 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4156 if (completion_cb) {
4157 completion_cb(opaque, 0);
4159 return 0;
4162 QemuOptsList qemu_mon_opts = {
4163 .name = "mon",
4164 .implied_opt_name = "chardev",
4165 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4166 .desc = {
4168 .name = "mode",
4169 .type = QEMU_OPT_STRING,
4171 .name = "chardev",
4172 .type = QEMU_OPT_STRING,
4174 .name = "default",
4175 .type = QEMU_OPT_BOOL,
4177 .name = "pretty",
4178 .type = QEMU_OPT_BOOL,
4180 { /* end of list */ }
4184 #ifndef TARGET_I386
4185 void qmp_rtc_reset_reinjection(Error **errp)
4187 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
4189 #endif
4191 #ifndef TARGET_S390X
4192 void qmp_dump_skeys(const char *filename, Error **errp)
4194 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4196 #endif