qapi: Support multiple command registries per program
[qemu.git] / monitor.c
blobc7f7602466acb2a5ca0b351a84cbe07a9a80978d
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 "qemu/osdep.h"
25 #include <dirent.h>
26 #include "qemu-common.h"
27 #include "cpu.h"
28 #include "hw/hw.h"
29 #include "monitor/qdev.h"
30 #include "hw/usb.h"
31 #include "hw/i386/pc.h"
32 #include "hw/pci/pci.h"
33 #include "sysemu/watchdog.h"
34 #include "hw/loader.h"
35 #include "exec/gdbstub.h"
36 #include "net/net.h"
37 #include "net/slirp.h"
38 #include "sysemu/char.h"
39 #include "ui/qemu-spice.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/numa.h"
42 #include "monitor/monitor.h"
43 #include "qemu/config-file.h"
44 #include "qemu/readline.h"
45 #include "ui/console.h"
46 #include "ui/input.h"
47 #include "sysemu/blockdev.h"
48 #include "sysemu/block-backend.h"
49 #include "audio/audio.h"
50 #include "disas/disas.h"
51 #include "sysemu/balloon.h"
52 #include "qemu/timer.h"
53 #include "migration/migration.h"
54 #include "sysemu/hw_accel.h"
55 #include "qemu/acl.h"
56 #include "sysemu/tpm.h"
57 #include "qapi/qmp/qerror.h"
58 #include "qapi/qmp/types.h"
59 #include "qapi/qmp/qjson.h"
60 #include "qapi/qmp/json-streamer.h"
61 #include "qapi/qmp/json-parser.h"
62 #include "qom/object_interfaces.h"
63 #include "trace-root.h"
64 #include "trace/control.h"
65 #include "monitor/hmp-target.h"
66 #ifdef CONFIG_TRACE_SIMPLE
67 #include "trace/simple.h"
68 #endif
69 #include "exec/memory.h"
70 #include "exec/exec-all.h"
71 #include "qemu/log.h"
72 #include "qmp-commands.h"
73 #include "hmp.h"
74 #include "qemu/thread.h"
75 #include "block/qapi.h"
76 #include "qapi/qmp-event.h"
77 #include "qapi-event.h"
78 #include "qmp-introspect.h"
79 #include "sysemu/qtest.h"
80 #include "qemu/cutils.h"
81 #include "qapi/qmp/dispatch.h"
83 #if defined(TARGET_S390X)
84 #include "hw/s390x/storage-keys.h"
85 #endif
88 * Supported types:
90 * 'F' filename
91 * 'B' block device name
92 * 's' string (accept optional quote)
93 * 'S' it just appends the rest of the string (accept optional quote)
94 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
99 * 'i' 32 bit integer
100 * 'l' target long (32 or 64 bit)
101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
103 * 'o' octets (aka bytes)
104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
108 * 'T' double
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
111 * '/' optional gdb-like print format (like "/10x")
113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
115 * 'b' boolean
116 * user mode accepts "on" or "off"
117 * '-' optional parameter (eg. '-f')
121 typedef struct mon_cmd_t {
122 const char *name;
123 const char *args_type;
124 const char *params;
125 const char *help;
126 void (*cmd)(Monitor *mon, const QDict *qdict);
127 /* @sub_table is a list of 2nd level of commands. If it does not exist,
128 * cmd should be used. If it exists, sub_table[?].cmd should be
129 * used, and cmd of 1st level plays the role of help function.
131 struct mon_cmd_t *sub_table;
132 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
133 } mon_cmd_t;
135 /* file descriptors passed via SCM_RIGHTS */
136 typedef struct mon_fd_t mon_fd_t;
137 struct mon_fd_t {
138 char *name;
139 int fd;
140 QLIST_ENTRY(mon_fd_t) next;
143 /* file descriptor associated with a file descriptor set */
144 typedef struct MonFdsetFd MonFdsetFd;
145 struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
152 /* file descriptor set containing fds passed via SCM_RIGHTS */
153 typedef struct MonFdset MonFdset;
154 struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
157 QLIST_HEAD(, MonFdsetFd) dup_fds;
158 QLIST_ENTRY(MonFdset) next;
161 typedef struct {
162 JSONMessageParser parser;
164 * When a client connects, we're in capabilities negotiation mode.
165 * When command qmp_capabilities succeeds, we go into command
166 * mode.
168 bool in_command_mode; /* are we in command mode? */
169 } MonitorQMP;
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
174 * instance.
176 typedef struct MonitorQAPIEventState {
177 QAPIEvent event; /* Throttling state for this event type and... */
178 QDict *data; /* ... data, see qapi_event_throttle_equal() */
179 QEMUTimer *timer; /* Timer for handling delayed events */
180 QDict *qdict; /* Delayed event (if any) */
181 } MonitorQAPIEventState;
183 typedef struct {
184 int64_t rate; /* Minimum time (in ns) between two events */
185 } MonitorQAPIEventConf;
187 struct Monitor {
188 CharBackend chr;
189 int reset_seen;
190 int flags;
191 int suspend_cnt;
192 bool skip_flush;
194 QemuMutex out_lock;
195 QString *outbuf;
196 guint out_watch;
198 /* Read under either BQL or out_lock, written with BQL+out_lock. */
199 int mux_out;
201 ReadLineState *rs;
202 MonitorQMP qmp;
203 CPUState *mon_cpu;
204 BlockCompletionFunc *password_completion_cb;
205 void *password_opaque;
206 mon_cmd_t *cmd_table;
207 QLIST_HEAD(,mon_fd_t) fds;
208 QLIST_ENTRY(Monitor) entry;
211 /* QMP checker flags */
212 #define QMP_ACCEPT_UNKNOWNS 1
214 /* Protects mon_list, monitor_event_state. */
215 static QemuMutex monitor_lock;
217 static QLIST_HEAD(mon_list, Monitor) mon_list;
218 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
219 static int mon_refcount;
221 static mon_cmd_t mon_cmds[];
222 static mon_cmd_t info_cmds[];
224 QmpCommandList qmp_commands;
226 Monitor *cur_mon;
228 static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
230 static void monitor_command_cb(void *opaque, const char *cmdline,
231 void *readline_opaque);
234 * Is @mon a QMP monitor?
236 static inline bool monitor_is_qmp(const Monitor *mon)
238 return (mon->flags & MONITOR_USE_CONTROL);
242 * Is the current monitor, if any, a QMP monitor?
244 bool monitor_cur_is_qmp(void)
246 return cur_mon && monitor_is_qmp(cur_mon);
249 void monitor_read_command(Monitor *mon, int show_prompt)
251 if (!mon->rs)
252 return;
254 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
255 if (show_prompt)
256 readline_show_prompt(mon->rs);
259 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
260 void *opaque)
262 if (mon->rs) {
263 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
264 /* prompt is printed on return from the command handler */
265 return 0;
266 } else {
267 monitor_printf(mon, "terminal does not support password prompting\n");
268 return -ENOTTY;
272 static void monitor_flush_locked(Monitor *mon);
274 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
275 void *opaque)
277 Monitor *mon = opaque;
279 qemu_mutex_lock(&mon->out_lock);
280 mon->out_watch = 0;
281 monitor_flush_locked(mon);
282 qemu_mutex_unlock(&mon->out_lock);
283 return FALSE;
286 /* Called with mon->out_lock held. */
287 static void monitor_flush_locked(Monitor *mon)
289 int rc;
290 size_t len;
291 const char *buf;
293 if (mon->skip_flush) {
294 return;
297 buf = qstring_get_str(mon->outbuf);
298 len = qstring_get_length(mon->outbuf);
300 if (len && !mon->mux_out) {
301 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
302 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
303 /* all flushed or error */
304 QDECREF(mon->outbuf);
305 mon->outbuf = qstring_new();
306 return;
308 if (rc > 0) {
309 /* partial write */
310 QString *tmp = qstring_from_str(buf + rc);
311 QDECREF(mon->outbuf);
312 mon->outbuf = tmp;
314 if (mon->out_watch == 0) {
315 mon->out_watch =
316 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
317 monitor_unblocked, mon);
322 void monitor_flush(Monitor *mon)
324 qemu_mutex_lock(&mon->out_lock);
325 monitor_flush_locked(mon);
326 qemu_mutex_unlock(&mon->out_lock);
329 /* flush at every end of line */
330 static void monitor_puts(Monitor *mon, const char *str)
332 char c;
334 qemu_mutex_lock(&mon->out_lock);
335 for(;;) {
336 c = *str++;
337 if (c == '\0')
338 break;
339 if (c == '\n') {
340 qstring_append_chr(mon->outbuf, '\r');
342 qstring_append_chr(mon->outbuf, c);
343 if (c == '\n') {
344 monitor_flush_locked(mon);
347 qemu_mutex_unlock(&mon->out_lock);
350 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
352 char *buf;
354 if (!mon)
355 return;
357 if (monitor_is_qmp(mon)) {
358 return;
361 buf = g_strdup_vprintf(fmt, ap);
362 monitor_puts(mon, buf);
363 g_free(buf);
366 void monitor_printf(Monitor *mon, const char *fmt, ...)
368 va_list ap;
369 va_start(ap, fmt);
370 monitor_vprintf(mon, fmt, ap);
371 va_end(ap);
374 int monitor_fprintf(FILE *stream, const char *fmt, ...)
376 va_list ap;
377 va_start(ap, fmt);
378 monitor_vprintf((Monitor *)stream, fmt, ap);
379 va_end(ap);
380 return 0;
383 static void monitor_json_emitter(Monitor *mon, const QObject *data)
385 QString *json;
387 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
388 qobject_to_json(data);
389 assert(json != NULL);
391 qstring_append_chr(json, '\n');
392 monitor_puts(mon, qstring_get_str(json));
394 QDECREF(json);
397 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
398 /* Limit guest-triggerable events to 1 per second */
399 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
400 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
404 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
407 GHashTable *monitor_qapi_event_state;
410 * Emits the event to every monitor instance, @event is only used for trace
411 * Called with monitor_lock held.
413 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
415 Monitor *mon;
417 trace_monitor_protocol_event_emit(event, qdict);
418 QLIST_FOREACH(mon, &mon_list, entry) {
419 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
420 monitor_json_emitter(mon, QOBJECT(qdict));
425 static void monitor_qapi_event_handler(void *opaque);
428 * Queue a new event for emission to Monitor instances,
429 * applying any rate limiting if required.
431 static void
432 monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
434 MonitorQAPIEventConf *evconf;
435 MonitorQAPIEventState *evstate;
437 assert(event < QAPI_EVENT__MAX);
438 evconf = &monitor_qapi_event_conf[event];
439 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
441 qemu_mutex_lock(&monitor_lock);
443 if (!evconf->rate) {
444 /* Unthrottled event */
445 monitor_qapi_event_emit(event, qdict);
446 } else {
447 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
448 MonitorQAPIEventState key = { .event = event, .data = data };
450 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
451 assert(!evstate || timer_pending(evstate->timer));
453 if (evstate) {
455 * Timer is pending for (at least) evconf->rate ns after
456 * last send. Store event for sending when timer fires,
457 * replacing a prior stored event if any.
459 QDECREF(evstate->qdict);
460 evstate->qdict = qdict;
461 QINCREF(evstate->qdict);
462 } else {
464 * Last send was (at least) evconf->rate ns ago.
465 * Send immediately, and arm the timer to call
466 * monitor_qapi_event_handler() in evconf->rate ns. Any
467 * events arriving before then will be delayed until then.
469 int64_t now = qemu_clock_get_ns(event_clock_type);
471 monitor_qapi_event_emit(event, qdict);
473 evstate = g_new(MonitorQAPIEventState, 1);
474 evstate->event = event;
475 evstate->data = data;
476 QINCREF(evstate->data);
477 evstate->qdict = NULL;
478 evstate->timer = timer_new_ns(event_clock_type,
479 monitor_qapi_event_handler,
480 evstate);
481 g_hash_table_add(monitor_qapi_event_state, evstate);
482 timer_mod_ns(evstate->timer, now + evconf->rate);
486 qemu_mutex_unlock(&monitor_lock);
490 * This function runs evconf->rate ns after sending a throttled
491 * event.
492 * If another event has since been stored, send it.
494 static void monitor_qapi_event_handler(void *opaque)
496 MonitorQAPIEventState *evstate = opaque;
497 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
499 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
500 qemu_mutex_lock(&monitor_lock);
502 if (evstate->qdict) {
503 int64_t now = qemu_clock_get_ns(event_clock_type);
505 monitor_qapi_event_emit(evstate->event, evstate->qdict);
506 QDECREF(evstate->qdict);
507 evstate->qdict = NULL;
508 timer_mod_ns(evstate->timer, now + evconf->rate);
509 } else {
510 g_hash_table_remove(monitor_qapi_event_state, evstate);
511 QDECREF(evstate->data);
512 timer_free(evstate->timer);
513 g_free(evstate);
516 qemu_mutex_unlock(&monitor_lock);
519 static unsigned int qapi_event_throttle_hash(const void *key)
521 const MonitorQAPIEventState *evstate = key;
522 unsigned int hash = evstate->event * 255;
524 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
525 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
528 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
529 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
532 return hash;
535 static gboolean qapi_event_throttle_equal(const void *a, const void *b)
537 const MonitorQAPIEventState *eva = a;
538 const MonitorQAPIEventState *evb = b;
540 if (eva->event != evb->event) {
541 return FALSE;
544 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
545 return !strcmp(qdict_get_str(eva->data, "id"),
546 qdict_get_str(evb->data, "id"));
549 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
550 return !strcmp(qdict_get_str(eva->data, "node-name"),
551 qdict_get_str(evb->data, "node-name"));
554 return TRUE;
557 static void monitor_qapi_event_init(void)
559 if (qtest_enabled()) {
560 event_clock_type = QEMU_CLOCK_VIRTUAL;
563 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
564 qapi_event_throttle_equal);
565 qmp_event_set_func_emit(monitor_qapi_event_queue);
568 void qmp_qmp_capabilities(Error **errp)
570 cur_mon->qmp.in_command_mode = true;
573 static void handle_hmp_command(Monitor *mon, const char *cmdline);
575 static void monitor_data_init(Monitor *mon)
577 memset(mon, 0, sizeof(Monitor));
578 qemu_mutex_init(&mon->out_lock);
579 mon->outbuf = qstring_new();
580 /* Use *mon_cmds by default. */
581 mon->cmd_table = mon_cmds;
584 static void monitor_data_destroy(Monitor *mon)
586 qemu_chr_fe_deinit(&mon->chr);
587 if (monitor_is_qmp(mon)) {
588 json_message_parser_destroy(&mon->qmp.parser);
590 g_free(mon->rs);
591 QDECREF(mon->outbuf);
592 qemu_mutex_destroy(&mon->out_lock);
595 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
596 int64_t cpu_index, Error **errp)
598 char *output = NULL;
599 Monitor *old_mon, hmp;
601 monitor_data_init(&hmp);
602 hmp.skip_flush = true;
604 old_mon = cur_mon;
605 cur_mon = &hmp;
607 if (has_cpu_index) {
608 int ret = monitor_set_cpu(cpu_index);
609 if (ret < 0) {
610 cur_mon = old_mon;
611 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
612 "a CPU number");
613 goto out;
617 handle_hmp_command(&hmp, command_line);
618 cur_mon = old_mon;
620 qemu_mutex_lock(&hmp.out_lock);
621 if (qstring_get_length(hmp.outbuf) > 0) {
622 output = g_strdup(qstring_get_str(hmp.outbuf));
623 } else {
624 output = g_strdup("");
626 qemu_mutex_unlock(&hmp.out_lock);
628 out:
629 monitor_data_destroy(&hmp);
630 return output;
633 static int compare_cmd(const char *name, const char *list)
635 const char *p, *pstart;
636 int len;
637 len = strlen(name);
638 p = list;
639 for(;;) {
640 pstart = p;
641 p = strchr(p, '|');
642 if (!p)
643 p = pstart + strlen(pstart);
644 if ((p - pstart) == len && !memcmp(pstart, name, len))
645 return 1;
646 if (*p == '\0')
647 break;
648 p++;
650 return 0;
653 static int get_str(char *buf, int buf_size, const char **pp)
655 const char *p;
656 char *q;
657 int c;
659 q = buf;
660 p = *pp;
661 while (qemu_isspace(*p)) {
662 p++;
664 if (*p == '\0') {
665 fail:
666 *q = '\0';
667 *pp = p;
668 return -1;
670 if (*p == '\"') {
671 p++;
672 while (*p != '\0' && *p != '\"') {
673 if (*p == '\\') {
674 p++;
675 c = *p++;
676 switch (c) {
677 case 'n':
678 c = '\n';
679 break;
680 case 'r':
681 c = '\r';
682 break;
683 case '\\':
684 case '\'':
685 case '\"':
686 break;
687 default:
688 printf("unsupported escape code: '\\%c'\n", c);
689 goto fail;
691 if ((q - buf) < buf_size - 1) {
692 *q++ = c;
694 } else {
695 if ((q - buf) < buf_size - 1) {
696 *q++ = *p;
698 p++;
701 if (*p != '\"') {
702 printf("unterminated string\n");
703 goto fail;
705 p++;
706 } else {
707 while (*p != '\0' && !qemu_isspace(*p)) {
708 if ((q - buf) < buf_size - 1) {
709 *q++ = *p;
711 p++;
714 *q = '\0';
715 *pp = p;
716 return 0;
719 #define MAX_ARGS 16
721 static void free_cmdline_args(char **args, int nb_args)
723 int i;
725 assert(nb_args <= MAX_ARGS);
727 for (i = 0; i < nb_args; i++) {
728 g_free(args[i]);
734 * Parse the command line to get valid args.
735 * @cmdline: command line to be parsed.
736 * @pnb_args: location to store the number of args, must NOT be NULL.
737 * @args: location to store the args, which should be freed by caller, must
738 * NOT be NULL.
740 * Returns 0 on success, negative on failure.
742 * NOTE: this parser is an approximate form of the real command parser. Number
743 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
744 * return with failure.
746 static int parse_cmdline(const char *cmdline,
747 int *pnb_args, char **args)
749 const char *p;
750 int nb_args, ret;
751 char buf[1024];
753 p = cmdline;
754 nb_args = 0;
755 for (;;) {
756 while (qemu_isspace(*p)) {
757 p++;
759 if (*p == '\0') {
760 break;
762 if (nb_args >= MAX_ARGS) {
763 goto fail;
765 ret = get_str(buf, sizeof(buf), &p);
766 if (ret < 0) {
767 goto fail;
769 args[nb_args] = g_strdup(buf);
770 nb_args++;
772 *pnb_args = nb_args;
773 return 0;
775 fail:
776 free_cmdline_args(args, nb_args);
777 return -1;
780 static void help_cmd_dump_one(Monitor *mon,
781 const mon_cmd_t *cmd,
782 char **prefix_args,
783 int prefix_args_nb)
785 int i;
787 for (i = 0; i < prefix_args_nb; i++) {
788 monitor_printf(mon, "%s ", prefix_args[i]);
790 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
793 /* @args[@arg_index] is the valid command need to find in @cmds */
794 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
795 char **args, int nb_args, int arg_index)
797 const mon_cmd_t *cmd;
799 /* No valid arg need to compare with, dump all in *cmds */
800 if (arg_index >= nb_args) {
801 for (cmd = cmds; cmd->name != NULL; cmd++) {
802 help_cmd_dump_one(mon, cmd, args, arg_index);
804 return;
807 /* Find one entry to dump */
808 for (cmd = cmds; cmd->name != NULL; cmd++) {
809 if (compare_cmd(args[arg_index], cmd->name)) {
810 if (cmd->sub_table) {
811 /* continue with next arg */
812 help_cmd_dump(mon, cmd->sub_table,
813 args, nb_args, arg_index + 1);
814 } else {
815 help_cmd_dump_one(mon, cmd, args, arg_index);
817 break;
822 static void help_cmd(Monitor *mon, const char *name)
824 char *args[MAX_ARGS];
825 int nb_args = 0;
827 /* 1. parse user input */
828 if (name) {
829 /* special case for log, directly dump and return */
830 if (!strcmp(name, "log")) {
831 const QEMULogItem *item;
832 monitor_printf(mon, "Log items (comma separated):\n");
833 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
834 for (item = qemu_log_items; item->mask != 0; item++) {
835 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
837 return;
840 if (parse_cmdline(name, &nb_args, args) < 0) {
841 return;
845 /* 2. dump the contents according to parsed args */
846 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
848 free_cmdline_args(args, nb_args);
851 static void do_help_cmd(Monitor *mon, const QDict *qdict)
853 help_cmd(mon, qdict_get_try_str(qdict, "name"));
856 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
858 const char *tp_name = qdict_get_str(qdict, "name");
859 bool new_state = qdict_get_bool(qdict, "option");
860 bool has_vcpu = qdict_haskey(qdict, "vcpu");
861 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
862 Error *local_err = NULL;
864 if (vcpu < 0) {
865 monitor_printf(mon, "argument vcpu must be positive");
866 return;
869 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
870 if (local_err) {
871 error_report_err(local_err);
875 #ifdef CONFIG_TRACE_SIMPLE
876 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
878 const char *op = qdict_get_try_str(qdict, "op");
879 const char *arg = qdict_get_try_str(qdict, "arg");
881 if (!op) {
882 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
883 } else if (!strcmp(op, "on")) {
884 st_set_trace_file_enabled(true);
885 } else if (!strcmp(op, "off")) {
886 st_set_trace_file_enabled(false);
887 } else if (!strcmp(op, "flush")) {
888 st_flush_trace_buffer();
889 } else if (!strcmp(op, "set")) {
890 if (arg) {
891 st_set_trace_file(arg);
893 } else {
894 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
895 help_cmd(mon, "trace-file");
898 #endif
900 static void hmp_info_help(Monitor *mon, const QDict *qdict)
902 help_cmd(mon, "info");
905 static void query_commands_cb(QmpCommand *cmd, void *opaque)
907 CommandInfoList *info, **list = opaque;
909 if (!cmd->enabled) {
910 return;
913 info = g_malloc0(sizeof(*info));
914 info->value = g_malloc0(sizeof(*info->value));
915 info->value->name = g_strdup(cmd->name);
916 info->next = *list;
917 *list = info;
920 CommandInfoList *qmp_query_commands(Error **errp)
922 CommandInfoList *list = NULL;
924 qmp_for_each_command(&qmp_commands, query_commands_cb, &list);
926 return list;
929 EventInfoList *qmp_query_events(Error **errp)
931 EventInfoList *info, *ev_list = NULL;
932 QAPIEvent e;
934 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
935 const char *event_name = QAPIEvent_lookup[e];
936 assert(event_name != NULL);
937 info = g_malloc0(sizeof(*info));
938 info->value = g_malloc0(sizeof(*info->value));
939 info->value->name = g_strdup(event_name);
941 info->next = ev_list;
942 ev_list = info;
945 return ev_list;
949 * Minor hack: generated marshalling suppressed for this command
950 * ('gen': false in the schema) so we can parse the JSON string
951 * directly into QObject instead of first parsing it with
952 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
953 * to QObject with generated output marshallers, every time. Instead,
954 * we do it in test-qobject-input-visitor.c, just to make sure
955 * qapi-introspect.py's output actually conforms to the schema.
957 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
958 Error **errp)
960 *ret_data = qobject_from_json(qmp_schema_json);
964 * We used to define commands in qmp-commands.hx in addition to the
965 * QAPI schema. This permitted defining some of them only in certain
966 * configurations. query-commands has always reflected that (good,
967 * because it lets QMP clients figure out what's actually available),
968 * while query-qmp-schema never did (not so good). This function is a
969 * hack to keep the configuration-specific commands defined exactly as
970 * before, even though qmp-commands.hx is gone.
972 * FIXME Educate the QAPI schema on configuration-specific commands,
973 * and drop this hack.
975 static void qmp_unregister_commands_hack(void)
977 #ifndef CONFIG_SPICE
978 qmp_unregister_command(&qmp_commands, "query-spice");
979 #endif
980 #ifndef TARGET_I386
981 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
982 #endif
983 #ifndef TARGET_S390X
984 qmp_unregister_command(&qmp_commands, "dump-skeys");
985 #endif
986 #ifndef TARGET_ARM
987 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
988 #endif
989 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
990 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
991 #endif
992 #if !defined(TARGET_S390X)
993 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
994 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
995 #endif
996 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
997 && !defined(TARGET_S390X)
998 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
999 #endif
1002 void monitor_init_qmp_commands(void)
1004 qmp_init_marshal(&qmp_commands);
1006 qmp_register_command(&qmp_commands, "query-qmp-schema",
1007 qmp_query_qmp_schema,
1008 QCO_NO_OPTIONS);
1009 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
1010 QCO_NO_OPTIONS);
1011 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
1012 QCO_NO_OPTIONS);
1014 qmp_unregister_commands_hack();
1017 /* set the current CPU defined by the user */
1018 int monitor_set_cpu(int cpu_index)
1020 CPUState *cpu;
1022 cpu = qemu_get_cpu(cpu_index);
1023 if (cpu == NULL) {
1024 return -1;
1026 cur_mon->mon_cpu = cpu;
1027 return 0;
1030 CPUState *mon_get_cpu(void)
1032 if (!cur_mon->mon_cpu) {
1033 if (!first_cpu) {
1034 return NULL;
1036 monitor_set_cpu(first_cpu->cpu_index);
1038 cpu_synchronize_state(cur_mon->mon_cpu);
1039 return cur_mon->mon_cpu;
1042 CPUArchState *mon_get_cpu_env(void)
1044 CPUState *cs = mon_get_cpu();
1046 return cs ? cs->env_ptr : NULL;
1049 int monitor_get_cpu_index(void)
1051 CPUState *cs = mon_get_cpu();
1053 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
1056 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
1058 CPUState *cs = mon_get_cpu();
1060 if (!cs) {
1061 monitor_printf(mon, "No CPU available\n");
1062 return;
1064 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1067 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
1069 dump_exec_info((FILE *)mon, monitor_fprintf);
1070 dump_drift_info((FILE *)mon, monitor_fprintf);
1073 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
1075 dump_opcount_info((FILE *)mon, monitor_fprintf);
1078 static void hmp_info_history(Monitor *mon, const QDict *qdict)
1080 int i;
1081 const char *str;
1083 if (!mon->rs)
1084 return;
1085 i = 0;
1086 for(;;) {
1087 str = readline_get_history(mon->rs, i);
1088 if (!str)
1089 break;
1090 monitor_printf(mon, "%d: '%s'\n", i, str);
1091 i++;
1095 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
1097 CPUState *cs = mon_get_cpu();
1099 if (!cs) {
1100 monitor_printf(mon, "No CPU available\n");
1101 return;
1103 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
1106 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1108 const char *name = qdict_get_try_str(qdict, "name");
1109 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1110 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
1111 TraceEventInfoList *events;
1112 TraceEventInfoList *elem;
1113 Error *local_err = NULL;
1115 if (name == NULL) {
1116 name = "*";
1118 if (vcpu < 0) {
1119 monitor_printf(mon, "argument vcpu must be positive");
1120 return;
1123 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
1124 if (local_err) {
1125 error_report_err(local_err);
1126 return;
1129 for (elem = events; elem != NULL; elem = elem->next) {
1130 monitor_printf(mon, "%s : state %u\n",
1131 elem->value->name,
1132 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1134 qapi_free_TraceEventInfoList(events);
1137 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1138 bool has_port, int64_t port,
1139 bool has_tls_port, int64_t tls_port,
1140 bool has_cert_subject, const char *cert_subject,
1141 Error **errp)
1143 if (strcmp(protocol, "spice") == 0) {
1144 if (!qemu_using_spice(errp)) {
1145 return;
1148 if (!has_port && !has_tls_port) {
1149 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1150 return;
1153 if (qemu_spice_migrate_info(hostname,
1154 has_port ? port : -1,
1155 has_tls_port ? tls_port : -1,
1156 cert_subject)) {
1157 error_setg(errp, QERR_UNDEFINED_ERROR);
1158 return;
1160 return;
1163 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1166 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1168 Error *err = NULL;
1170 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1171 if (err) {
1172 error_report_err(err);
1176 static void hmp_log(Monitor *mon, const QDict *qdict)
1178 int mask;
1179 const char *items = qdict_get_str(qdict, "items");
1181 if (!strcmp(items, "none")) {
1182 mask = 0;
1183 } else {
1184 mask = qemu_str_to_log_mask(items);
1185 if (!mask) {
1186 help_cmd(mon, "log");
1187 return;
1190 qemu_set_log(mask);
1193 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1195 const char *option = qdict_get_try_str(qdict, "option");
1196 if (!option || !strcmp(option, "on")) {
1197 singlestep = 1;
1198 } else if (!strcmp(option, "off")) {
1199 singlestep = 0;
1200 } else {
1201 monitor_printf(mon, "unexpected option %s\n", option);
1205 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1207 const char *device = qdict_get_try_str(qdict, "device");
1208 if (!device)
1209 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1210 if (gdbserver_start(device) < 0) {
1211 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1212 device);
1213 } else if (strcmp(device, "none") == 0) {
1214 monitor_printf(mon, "Disabled gdbserver\n");
1215 } else {
1216 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1217 device);
1221 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1223 const char *action = qdict_get_str(qdict, "action");
1224 if (select_watchdog_action(action) == -1) {
1225 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1229 static void monitor_printc(Monitor *mon, int c)
1231 monitor_printf(mon, "'");
1232 switch(c) {
1233 case '\'':
1234 monitor_printf(mon, "\\'");
1235 break;
1236 case '\\':
1237 monitor_printf(mon, "\\\\");
1238 break;
1239 case '\n':
1240 monitor_printf(mon, "\\n");
1241 break;
1242 case '\r':
1243 monitor_printf(mon, "\\r");
1244 break;
1245 default:
1246 if (c >= 32 && c <= 126) {
1247 monitor_printf(mon, "%c", c);
1248 } else {
1249 monitor_printf(mon, "\\x%02x", c);
1251 break;
1253 monitor_printf(mon, "'");
1256 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1257 hwaddr addr, int is_physical)
1259 int l, line_size, i, max_digits, len;
1260 uint8_t buf[16];
1261 uint64_t v;
1262 CPUState *cs = mon_get_cpu();
1264 if (!cs && (format == 'i' || !is_physical)) {
1265 monitor_printf(mon, "Can not dump without CPU\n");
1266 return;
1269 if (format == 'i') {
1270 int flags = 0;
1271 #ifdef TARGET_I386
1272 CPUArchState *env = mon_get_cpu_env();
1273 if (wsize == 2) {
1274 flags = 1;
1275 } else if (wsize == 4) {
1276 flags = 0;
1277 } else {
1278 /* as default we use the current CS size */
1279 flags = 0;
1280 if (env) {
1281 #ifdef TARGET_X86_64
1282 if ((env->efer & MSR_EFER_LMA) &&
1283 (env->segs[R_CS].flags & DESC_L_MASK))
1284 flags = 2;
1285 else
1286 #endif
1287 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1288 flags = 1;
1291 #endif
1292 #ifdef TARGET_PPC
1293 CPUArchState *env = mon_get_cpu_env();
1294 flags = msr_le << 16;
1295 flags |= env->bfd_mach;
1296 #endif
1297 monitor_disas(mon, cs, addr, count, is_physical, flags);
1298 return;
1301 len = wsize * count;
1302 if (wsize == 1)
1303 line_size = 8;
1304 else
1305 line_size = 16;
1306 max_digits = 0;
1308 switch(format) {
1309 case 'o':
1310 max_digits = (wsize * 8 + 2) / 3;
1311 break;
1312 default:
1313 case 'x':
1314 max_digits = (wsize * 8) / 4;
1315 break;
1316 case 'u':
1317 case 'd':
1318 max_digits = (wsize * 8 * 10 + 32) / 33;
1319 break;
1320 case 'c':
1321 wsize = 1;
1322 break;
1325 while (len > 0) {
1326 if (is_physical)
1327 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1328 else
1329 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1330 l = len;
1331 if (l > line_size)
1332 l = line_size;
1333 if (is_physical) {
1334 cpu_physical_memory_read(addr, buf, l);
1335 } else {
1336 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
1337 monitor_printf(mon, " Cannot access memory\n");
1338 break;
1341 i = 0;
1342 while (i < l) {
1343 switch(wsize) {
1344 default:
1345 case 1:
1346 v = ldub_p(buf + i);
1347 break;
1348 case 2:
1349 v = lduw_p(buf + i);
1350 break;
1351 case 4:
1352 v = (uint32_t)ldl_p(buf + i);
1353 break;
1354 case 8:
1355 v = ldq_p(buf + i);
1356 break;
1358 monitor_printf(mon, " ");
1359 switch(format) {
1360 case 'o':
1361 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1362 break;
1363 case 'x':
1364 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1365 break;
1366 case 'u':
1367 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1368 break;
1369 case 'd':
1370 monitor_printf(mon, "%*" PRId64, max_digits, v);
1371 break;
1372 case 'c':
1373 monitor_printc(mon, v);
1374 break;
1376 i += wsize;
1378 monitor_printf(mon, "\n");
1379 addr += l;
1380 len -= l;
1384 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1386 int count = qdict_get_int(qdict, "count");
1387 int format = qdict_get_int(qdict, "format");
1388 int size = qdict_get_int(qdict, "size");
1389 target_long addr = qdict_get_int(qdict, "addr");
1391 memory_dump(mon, count, format, size, addr, 0);
1394 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1396 int count = qdict_get_int(qdict, "count");
1397 int format = qdict_get_int(qdict, "format");
1398 int size = qdict_get_int(qdict, "size");
1399 hwaddr addr = qdict_get_int(qdict, "addr");
1401 memory_dump(mon, count, format, size, addr, 1);
1404 static void do_print(Monitor *mon, const QDict *qdict)
1406 int format = qdict_get_int(qdict, "format");
1407 hwaddr val = qdict_get_int(qdict, "val");
1409 switch(format) {
1410 case 'o':
1411 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1412 break;
1413 case 'x':
1414 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1415 break;
1416 case 'u':
1417 monitor_printf(mon, "%" HWADDR_PRIu, val);
1418 break;
1419 default:
1420 case 'd':
1421 monitor_printf(mon, "%" HWADDR_PRId, val);
1422 break;
1423 case 'c':
1424 monitor_printc(mon, val);
1425 break;
1427 monitor_printf(mon, "\n");
1430 static void hmp_sum(Monitor *mon, const QDict *qdict)
1432 uint32_t addr;
1433 uint16_t sum;
1434 uint32_t start = qdict_get_int(qdict, "start");
1435 uint32_t size = qdict_get_int(qdict, "size");
1437 sum = 0;
1438 for(addr = start; addr < (start + size); addr++) {
1439 uint8_t val = address_space_ldub(&address_space_memory, addr,
1440 MEMTXATTRS_UNSPECIFIED, NULL);
1441 /* BSD sum algorithm ('sum' Unix command) */
1442 sum = (sum >> 1) | (sum << 15);
1443 sum += val;
1445 monitor_printf(mon, "%05d\n", sum);
1448 static int mouse_button_state;
1450 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1452 int dx, dy, dz, button;
1453 const char *dx_str = qdict_get_str(qdict, "dx_str");
1454 const char *dy_str = qdict_get_str(qdict, "dy_str");
1455 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1457 dx = strtol(dx_str, NULL, 0);
1458 dy = strtol(dy_str, NULL, 0);
1459 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1460 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1462 if (dz_str) {
1463 dz = strtol(dz_str, NULL, 0);
1464 if (dz != 0) {
1465 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1466 qemu_input_queue_btn(NULL, button, true);
1467 qemu_input_event_sync();
1468 qemu_input_queue_btn(NULL, button, false);
1471 qemu_input_event_sync();
1474 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1476 static uint32_t bmap[INPUT_BUTTON__MAX] = {
1477 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1478 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1479 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1481 int button_state = qdict_get_int(qdict, "button_state");
1483 if (mouse_button_state == button_state) {
1484 return;
1486 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1487 qemu_input_event_sync();
1488 mouse_button_state = button_state;
1491 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1493 int size = qdict_get_int(qdict, "size");
1494 int addr = qdict_get_int(qdict, "addr");
1495 int has_index = qdict_haskey(qdict, "index");
1496 uint32_t val;
1497 int suffix;
1499 if (has_index) {
1500 int index = qdict_get_int(qdict, "index");
1501 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1502 addr++;
1504 addr &= 0xffff;
1506 switch(size) {
1507 default:
1508 case 1:
1509 val = cpu_inb(addr);
1510 suffix = 'b';
1511 break;
1512 case 2:
1513 val = cpu_inw(addr);
1514 suffix = 'w';
1515 break;
1516 case 4:
1517 val = cpu_inl(addr);
1518 suffix = 'l';
1519 break;
1521 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1522 suffix, addr, size * 2, val);
1525 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1527 int size = qdict_get_int(qdict, "size");
1528 int addr = qdict_get_int(qdict, "addr");
1529 int val = qdict_get_int(qdict, "val");
1531 addr &= IOPORTS_MASK;
1533 switch (size) {
1534 default:
1535 case 1:
1536 cpu_outb(addr, val);
1537 break;
1538 case 2:
1539 cpu_outw(addr, val);
1540 break;
1541 case 4:
1542 cpu_outl(addr, val);
1543 break;
1547 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1549 Error *local_err = NULL;
1550 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1552 qemu_boot_set(bootdevice, &local_err);
1553 if (local_err) {
1554 error_report_err(local_err);
1555 } else {
1556 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1560 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1562 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1564 mtree_info((fprintf_function)monitor_printf, mon, flatview);
1567 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1569 int i;
1570 CPUState *cpu;
1571 uint64_t *node_mem;
1573 node_mem = g_new0(uint64_t, nb_numa_nodes);
1574 query_numa_node_mem(node_mem);
1575 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1576 for (i = 0; i < nb_numa_nodes; i++) {
1577 monitor_printf(mon, "node %d cpus:", i);
1578 CPU_FOREACH(cpu) {
1579 if (cpu->numa_node == i) {
1580 monitor_printf(mon, " %d", cpu->cpu_index);
1583 monitor_printf(mon, "\n");
1584 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1585 node_mem[i] >> 20);
1587 g_free(node_mem);
1590 #ifdef CONFIG_PROFILER
1592 int64_t tcg_time;
1593 int64_t dev_time;
1595 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1597 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1598 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1599 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1600 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
1601 tcg_time = 0;
1602 dev_time = 0;
1604 #else
1605 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1607 monitor_printf(mon, "Internal profiler not compiled\n");
1609 #endif
1611 /* Capture support */
1612 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1614 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1616 int i;
1617 CaptureState *s;
1619 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1620 monitor_printf(mon, "[%d]: ", i);
1621 s->ops.info (s->opaque);
1625 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1627 int i;
1628 int n = qdict_get_int(qdict, "n");
1629 CaptureState *s;
1631 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1632 if (i == n) {
1633 s->ops.destroy (s->opaque);
1634 QLIST_REMOVE (s, entries);
1635 g_free (s);
1636 return;
1641 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1643 const char *path = qdict_get_str(qdict, "path");
1644 int has_freq = qdict_haskey(qdict, "freq");
1645 int freq = qdict_get_try_int(qdict, "freq", -1);
1646 int has_bits = qdict_haskey(qdict, "bits");
1647 int bits = qdict_get_try_int(qdict, "bits", -1);
1648 int has_channels = qdict_haskey(qdict, "nchannels");
1649 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1650 CaptureState *s;
1652 s = g_malloc0 (sizeof (*s));
1654 freq = has_freq ? freq : 44100;
1655 bits = has_bits ? bits : 16;
1656 nchannels = has_channels ? nchannels : 2;
1658 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1659 monitor_printf(mon, "Failed to add wave capture\n");
1660 g_free (s);
1661 return;
1663 QLIST_INSERT_HEAD (&capture_head, s, entries);
1666 static qemu_acl *find_acl(Monitor *mon, const char *name)
1668 qemu_acl *acl = qemu_acl_find(name);
1670 if (!acl) {
1671 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1673 return acl;
1676 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1678 const char *aclname = qdict_get_str(qdict, "aclname");
1679 qemu_acl *acl = find_acl(mon, aclname);
1680 qemu_acl_entry *entry;
1681 int i = 0;
1683 if (acl) {
1684 monitor_printf(mon, "policy: %s\n",
1685 acl->defaultDeny ? "deny" : "allow");
1686 QTAILQ_FOREACH(entry, &acl->entries, next) {
1687 i++;
1688 monitor_printf(mon, "%d: %s %s\n", i,
1689 entry->deny ? "deny" : "allow", entry->match);
1694 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1696 const char *aclname = qdict_get_str(qdict, "aclname");
1697 qemu_acl *acl = find_acl(mon, aclname);
1699 if (acl) {
1700 qemu_acl_reset(acl);
1701 monitor_printf(mon, "acl: removed all rules\n");
1705 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1707 const char *aclname = qdict_get_str(qdict, "aclname");
1708 const char *policy = qdict_get_str(qdict, "policy");
1709 qemu_acl *acl = find_acl(mon, aclname);
1711 if (acl) {
1712 if (strcmp(policy, "allow") == 0) {
1713 acl->defaultDeny = 0;
1714 monitor_printf(mon, "acl: policy set to 'allow'\n");
1715 } else if (strcmp(policy, "deny") == 0) {
1716 acl->defaultDeny = 1;
1717 monitor_printf(mon, "acl: policy set to 'deny'\n");
1718 } else {
1719 monitor_printf(mon, "acl: unknown policy '%s', "
1720 "expected 'deny' or 'allow'\n", policy);
1725 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1727 const char *aclname = qdict_get_str(qdict, "aclname");
1728 const char *match = qdict_get_str(qdict, "match");
1729 const char *policy = qdict_get_str(qdict, "policy");
1730 int has_index = qdict_haskey(qdict, "index");
1731 int index = qdict_get_try_int(qdict, "index", -1);
1732 qemu_acl *acl = find_acl(mon, aclname);
1733 int deny, ret;
1735 if (acl) {
1736 if (strcmp(policy, "allow") == 0) {
1737 deny = 0;
1738 } else if (strcmp(policy, "deny") == 0) {
1739 deny = 1;
1740 } else {
1741 monitor_printf(mon, "acl: unknown policy '%s', "
1742 "expected 'deny' or 'allow'\n", policy);
1743 return;
1745 if (has_index)
1746 ret = qemu_acl_insert(acl, deny, match, index);
1747 else
1748 ret = qemu_acl_append(acl, deny, match);
1749 if (ret < 0)
1750 monitor_printf(mon, "acl: unable to add acl entry\n");
1751 else
1752 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1756 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1758 const char *aclname = qdict_get_str(qdict, "aclname");
1759 const char *match = qdict_get_str(qdict, "match");
1760 qemu_acl *acl = find_acl(mon, aclname);
1761 int ret;
1763 if (acl) {
1764 ret = qemu_acl_remove(acl, match);
1765 if (ret < 0)
1766 monitor_printf(mon, "acl: no matching acl entry\n");
1767 else
1768 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1772 void qmp_getfd(const char *fdname, Error **errp)
1774 mon_fd_t *monfd;
1775 int fd;
1777 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1778 if (fd == -1) {
1779 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1780 return;
1783 if (qemu_isdigit(fdname[0])) {
1784 close(fd);
1785 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1786 "a name not starting with a digit");
1787 return;
1790 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1791 if (strcmp(monfd->name, fdname) != 0) {
1792 continue;
1795 close(monfd->fd);
1796 monfd->fd = fd;
1797 return;
1800 monfd = g_malloc0(sizeof(mon_fd_t));
1801 monfd->name = g_strdup(fdname);
1802 monfd->fd = fd;
1804 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1807 void qmp_closefd(const char *fdname, Error **errp)
1809 mon_fd_t *monfd;
1811 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1812 if (strcmp(monfd->name, fdname) != 0) {
1813 continue;
1816 QLIST_REMOVE(monfd, next);
1817 close(monfd->fd);
1818 g_free(monfd->name);
1819 g_free(monfd);
1820 return;
1823 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1826 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
1828 int saved_vm_running = runstate_is_running();
1829 const char *name = qdict_get_str(qdict, "name");
1831 vm_stop(RUN_STATE_RESTORE_VM);
1833 if (load_vmstate(name) == 0 && saved_vm_running) {
1834 vm_start();
1838 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1840 mon_fd_t *monfd;
1842 QLIST_FOREACH(monfd, &mon->fds, next) {
1843 int fd;
1845 if (strcmp(monfd->name, fdname) != 0) {
1846 continue;
1849 fd = monfd->fd;
1851 /* caller takes ownership of fd */
1852 QLIST_REMOVE(monfd, next);
1853 g_free(monfd->name);
1854 g_free(monfd);
1856 return fd;
1859 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1860 return -1;
1863 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1865 MonFdsetFd *mon_fdset_fd;
1866 MonFdsetFd *mon_fdset_fd_next;
1868 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1869 if ((mon_fdset_fd->removed ||
1870 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1871 runstate_is_running()) {
1872 close(mon_fdset_fd->fd);
1873 g_free(mon_fdset_fd->opaque);
1874 QLIST_REMOVE(mon_fdset_fd, next);
1875 g_free(mon_fdset_fd);
1879 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1880 QLIST_REMOVE(mon_fdset, next);
1881 g_free(mon_fdset);
1885 static void monitor_fdsets_cleanup(void)
1887 MonFdset *mon_fdset;
1888 MonFdset *mon_fdset_next;
1890 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1891 monitor_fdset_cleanup(mon_fdset);
1895 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1896 const char *opaque, Error **errp)
1898 int fd;
1899 Monitor *mon = cur_mon;
1900 AddfdInfo *fdinfo;
1902 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1903 if (fd == -1) {
1904 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1905 goto error;
1908 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1909 has_opaque, opaque, errp);
1910 if (fdinfo) {
1911 return fdinfo;
1914 error:
1915 if (fd != -1) {
1916 close(fd);
1918 return NULL;
1921 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1923 MonFdset *mon_fdset;
1924 MonFdsetFd *mon_fdset_fd;
1925 char fd_str[60];
1927 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1928 if (mon_fdset->id != fdset_id) {
1929 continue;
1931 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1932 if (has_fd) {
1933 if (mon_fdset_fd->fd != fd) {
1934 continue;
1936 mon_fdset_fd->removed = true;
1937 break;
1938 } else {
1939 mon_fdset_fd->removed = true;
1942 if (has_fd && !mon_fdset_fd) {
1943 goto error;
1945 monitor_fdset_cleanup(mon_fdset);
1946 return;
1949 error:
1950 if (has_fd) {
1951 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1952 fdset_id, fd);
1953 } else {
1954 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1956 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1959 FdsetInfoList *qmp_query_fdsets(Error **errp)
1961 MonFdset *mon_fdset;
1962 MonFdsetFd *mon_fdset_fd;
1963 FdsetInfoList *fdset_list = NULL;
1965 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1966 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1967 FdsetFdInfoList *fdsetfd_list = NULL;
1969 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1970 fdset_info->value->fdset_id = mon_fdset->id;
1972 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1973 FdsetFdInfoList *fdsetfd_info;
1975 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1976 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1977 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1978 if (mon_fdset_fd->opaque) {
1979 fdsetfd_info->value->has_opaque = true;
1980 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
1981 } else {
1982 fdsetfd_info->value->has_opaque = false;
1985 fdsetfd_info->next = fdsetfd_list;
1986 fdsetfd_list = fdsetfd_info;
1989 fdset_info->value->fds = fdsetfd_list;
1991 fdset_info->next = fdset_list;
1992 fdset_list = fdset_info;
1995 return fdset_list;
1998 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
1999 bool has_opaque, const char *opaque,
2000 Error **errp)
2002 MonFdset *mon_fdset = NULL;
2003 MonFdsetFd *mon_fdset_fd;
2004 AddfdInfo *fdinfo;
2006 if (has_fdset_id) {
2007 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2008 /* Break if match found or match impossible due to ordering by ID */
2009 if (fdset_id <= mon_fdset->id) {
2010 if (fdset_id < mon_fdset->id) {
2011 mon_fdset = NULL;
2013 break;
2018 if (mon_fdset == NULL) {
2019 int64_t fdset_id_prev = -1;
2020 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2022 if (has_fdset_id) {
2023 if (fdset_id < 0) {
2024 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2025 "a non-negative value");
2026 return NULL;
2028 /* Use specified fdset ID */
2029 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2030 mon_fdset_cur = mon_fdset;
2031 if (fdset_id < mon_fdset_cur->id) {
2032 break;
2035 } else {
2036 /* Use first available fdset ID */
2037 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2038 mon_fdset_cur = mon_fdset;
2039 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2040 fdset_id_prev = mon_fdset_cur->id;
2041 continue;
2043 break;
2047 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2048 if (has_fdset_id) {
2049 mon_fdset->id = fdset_id;
2050 } else {
2051 mon_fdset->id = fdset_id_prev + 1;
2054 /* The fdset list is ordered by fdset ID */
2055 if (!mon_fdset_cur) {
2056 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2057 } else if (mon_fdset->id < mon_fdset_cur->id) {
2058 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2059 } else {
2060 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2064 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2065 mon_fdset_fd->fd = fd;
2066 mon_fdset_fd->removed = false;
2067 if (has_opaque) {
2068 mon_fdset_fd->opaque = g_strdup(opaque);
2070 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2072 fdinfo = g_malloc0(sizeof(*fdinfo));
2073 fdinfo->fdset_id = mon_fdset->id;
2074 fdinfo->fd = mon_fdset_fd->fd;
2076 return fdinfo;
2079 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2081 #ifndef _WIN32
2082 MonFdset *mon_fdset;
2083 MonFdsetFd *mon_fdset_fd;
2084 int mon_fd_flags;
2086 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2087 if (mon_fdset->id != fdset_id) {
2088 continue;
2090 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2091 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2092 if (mon_fd_flags == -1) {
2093 return -1;
2096 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2097 return mon_fdset_fd->fd;
2100 errno = EACCES;
2101 return -1;
2103 #endif
2105 errno = ENOENT;
2106 return -1;
2109 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2111 MonFdset *mon_fdset;
2112 MonFdsetFd *mon_fdset_fd_dup;
2114 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2115 if (mon_fdset->id != fdset_id) {
2116 continue;
2118 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2119 if (mon_fdset_fd_dup->fd == dup_fd) {
2120 return -1;
2123 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2124 mon_fdset_fd_dup->fd = dup_fd;
2125 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2126 return 0;
2128 return -1;
2131 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2133 MonFdset *mon_fdset;
2134 MonFdsetFd *mon_fdset_fd_dup;
2136 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2137 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2138 if (mon_fdset_fd_dup->fd == dup_fd) {
2139 if (remove) {
2140 QLIST_REMOVE(mon_fdset_fd_dup, next);
2141 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2142 monitor_fdset_cleanup(mon_fdset);
2144 return -1;
2145 } else {
2146 return mon_fdset->id;
2151 return -1;
2154 int monitor_fdset_dup_fd_find(int dup_fd)
2156 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2159 void monitor_fdset_dup_fd_remove(int dup_fd)
2161 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2164 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2166 int fd;
2167 Error *local_err = NULL;
2169 if (!qemu_isdigit(fdname[0]) && mon) {
2170 fd = monitor_get_fd(mon, fdname, &local_err);
2171 } else {
2172 fd = qemu_parse_fd(fdname);
2173 if (fd == -1) {
2174 error_setg(&local_err, "Invalid file descriptor number '%s'",
2175 fdname);
2178 if (local_err) {
2179 error_propagate(errp, local_err);
2180 assert(fd == -1);
2181 } else {
2182 assert(fd != -1);
2185 return fd;
2188 /* Please update hmp-commands.hx when adding or changing commands */
2189 static mon_cmd_t info_cmds[] = {
2190 #include "hmp-commands-info.h"
2191 { NULL, NULL, },
2194 /* mon_cmds and info_cmds would be sorted at runtime */
2195 static mon_cmd_t mon_cmds[] = {
2196 #include "hmp-commands.h"
2197 { NULL, NULL, },
2200 /*******************************************************************/
2202 static const char *pch;
2203 static sigjmp_buf expr_env;
2206 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2207 expr_error(Monitor *mon, const char *fmt, ...)
2209 va_list ap;
2210 va_start(ap, fmt);
2211 monitor_vprintf(mon, fmt, ap);
2212 monitor_printf(mon, "\n");
2213 va_end(ap);
2214 siglongjmp(expr_env, 1);
2217 /* return 0 if OK, -1 if not found */
2218 static int get_monitor_def(target_long *pval, const char *name)
2220 const MonitorDef *md = target_monitor_defs();
2221 CPUState *cs = mon_get_cpu();
2222 void *ptr;
2223 uint64_t tmp = 0;
2224 int ret;
2226 if (cs == NULL || md == NULL) {
2227 return -1;
2230 for(; md->name != NULL; md++) {
2231 if (compare_cmd(name, md->name)) {
2232 if (md->get_value) {
2233 *pval = md->get_value(md, md->offset);
2234 } else {
2235 CPUArchState *env = mon_get_cpu_env();
2236 ptr = (uint8_t *)env + md->offset;
2237 switch(md->type) {
2238 case MD_I32:
2239 *pval = *(int32_t *)ptr;
2240 break;
2241 case MD_TLONG:
2242 *pval = *(target_long *)ptr;
2243 break;
2244 default:
2245 *pval = 0;
2246 break;
2249 return 0;
2253 ret = target_get_monitor_def(cs, name, &tmp);
2254 if (!ret) {
2255 *pval = (target_long) tmp;
2258 return ret;
2261 static void next(void)
2263 if (*pch != '\0') {
2264 pch++;
2265 while (qemu_isspace(*pch))
2266 pch++;
2270 static int64_t expr_sum(Monitor *mon);
2272 static int64_t expr_unary(Monitor *mon)
2274 int64_t n;
2275 char *p;
2276 int ret;
2278 switch(*pch) {
2279 case '+':
2280 next();
2281 n = expr_unary(mon);
2282 break;
2283 case '-':
2284 next();
2285 n = -expr_unary(mon);
2286 break;
2287 case '~':
2288 next();
2289 n = ~expr_unary(mon);
2290 break;
2291 case '(':
2292 next();
2293 n = expr_sum(mon);
2294 if (*pch != ')') {
2295 expr_error(mon, "')' expected");
2297 next();
2298 break;
2299 case '\'':
2300 pch++;
2301 if (*pch == '\0')
2302 expr_error(mon, "character constant expected");
2303 n = *pch;
2304 pch++;
2305 if (*pch != '\'')
2306 expr_error(mon, "missing terminating \' character");
2307 next();
2308 break;
2309 case '$':
2311 char buf[128], *q;
2312 target_long reg=0;
2314 pch++;
2315 q = buf;
2316 while ((*pch >= 'a' && *pch <= 'z') ||
2317 (*pch >= 'A' && *pch <= 'Z') ||
2318 (*pch >= '0' && *pch <= '9') ||
2319 *pch == '_' || *pch == '.') {
2320 if ((q - buf) < sizeof(buf) - 1)
2321 *q++ = *pch;
2322 pch++;
2324 while (qemu_isspace(*pch))
2325 pch++;
2326 *q = 0;
2327 ret = get_monitor_def(&reg, buf);
2328 if (ret < 0)
2329 expr_error(mon, "unknown register");
2330 n = reg;
2332 break;
2333 case '\0':
2334 expr_error(mon, "unexpected end of expression");
2335 n = 0;
2336 break;
2337 default:
2338 errno = 0;
2339 n = strtoull(pch, &p, 0);
2340 if (errno == ERANGE) {
2341 expr_error(mon, "number too large");
2343 if (pch == p) {
2344 expr_error(mon, "invalid char '%c' in expression", *p);
2346 pch = p;
2347 while (qemu_isspace(*pch))
2348 pch++;
2349 break;
2351 return n;
2355 static int64_t expr_prod(Monitor *mon)
2357 int64_t val, val2;
2358 int op;
2360 val = expr_unary(mon);
2361 for(;;) {
2362 op = *pch;
2363 if (op != '*' && op != '/' && op != '%')
2364 break;
2365 next();
2366 val2 = expr_unary(mon);
2367 switch(op) {
2368 default:
2369 case '*':
2370 val *= val2;
2371 break;
2372 case '/':
2373 case '%':
2374 if (val2 == 0)
2375 expr_error(mon, "division by zero");
2376 if (op == '/')
2377 val /= val2;
2378 else
2379 val %= val2;
2380 break;
2383 return val;
2386 static int64_t expr_logic(Monitor *mon)
2388 int64_t val, val2;
2389 int op;
2391 val = expr_prod(mon);
2392 for(;;) {
2393 op = *pch;
2394 if (op != '&' && op != '|' && op != '^')
2395 break;
2396 next();
2397 val2 = expr_prod(mon);
2398 switch(op) {
2399 default:
2400 case '&':
2401 val &= val2;
2402 break;
2403 case '|':
2404 val |= val2;
2405 break;
2406 case '^':
2407 val ^= val2;
2408 break;
2411 return val;
2414 static int64_t expr_sum(Monitor *mon)
2416 int64_t val, val2;
2417 int op;
2419 val = expr_logic(mon);
2420 for(;;) {
2421 op = *pch;
2422 if (op != '+' && op != '-')
2423 break;
2424 next();
2425 val2 = expr_logic(mon);
2426 if (op == '+')
2427 val += val2;
2428 else
2429 val -= val2;
2431 return val;
2434 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2436 pch = *pp;
2437 if (sigsetjmp(expr_env, 0)) {
2438 *pp = pch;
2439 return -1;
2441 while (qemu_isspace(*pch))
2442 pch++;
2443 *pval = expr_sum(mon);
2444 *pp = pch;
2445 return 0;
2448 static int get_double(Monitor *mon, double *pval, const char **pp)
2450 const char *p = *pp;
2451 char *tailp;
2452 double d;
2454 d = strtod(p, &tailp);
2455 if (tailp == p) {
2456 monitor_printf(mon, "Number expected\n");
2457 return -1;
2459 if (d != d || d - d != 0) {
2460 /* NaN or infinity */
2461 monitor_printf(mon, "Bad number\n");
2462 return -1;
2464 *pval = d;
2465 *pp = tailp;
2466 return 0;
2470 * Store the command-name in cmdname, and return a pointer to
2471 * the remaining of the command string.
2473 static const char *get_command_name(const char *cmdline,
2474 char *cmdname, size_t nlen)
2476 size_t len;
2477 const char *p, *pstart;
2479 p = cmdline;
2480 while (qemu_isspace(*p))
2481 p++;
2482 if (*p == '\0')
2483 return NULL;
2484 pstart = p;
2485 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2486 p++;
2487 len = p - pstart;
2488 if (len > nlen - 1)
2489 len = nlen - 1;
2490 memcpy(cmdname, pstart, len);
2491 cmdname[len] = '\0';
2492 return p;
2496 * Read key of 'type' into 'key' and return the current
2497 * 'type' pointer.
2499 static char *key_get_info(const char *type, char **key)
2501 size_t len;
2502 char *p, *str;
2504 if (*type == ',')
2505 type++;
2507 p = strchr(type, ':');
2508 if (!p) {
2509 *key = NULL;
2510 return NULL;
2512 len = p - type;
2514 str = g_malloc(len + 1);
2515 memcpy(str, type, len);
2516 str[len] = '\0';
2518 *key = str;
2519 return ++p;
2522 static int default_fmt_format = 'x';
2523 static int default_fmt_size = 4;
2525 static int is_valid_option(const char *c, const char *typestr)
2527 char option[3];
2529 option[0] = '-';
2530 option[1] = *c;
2531 option[2] = '\0';
2533 typestr = strstr(typestr, option);
2534 return (typestr != NULL);
2537 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2538 const char *cmdname)
2540 const mon_cmd_t *cmd;
2542 for (cmd = disp_table; cmd->name != NULL; cmd++) {
2543 if (compare_cmd(cmdname, cmd->name)) {
2544 return cmd;
2548 return NULL;
2552 * Parse command name from @cmdp according to command table @table.
2553 * If blank, return NULL.
2554 * Else, if no valid command can be found, report to @mon, and return
2555 * NULL.
2556 * Else, change @cmdp to point right behind the name, and return its
2557 * command table entry.
2558 * Do not assume the return value points into @table! It doesn't when
2559 * the command is found in a sub-command table.
2561 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2562 const char **cmdp,
2563 mon_cmd_t *table)
2565 const char *p;
2566 const mon_cmd_t *cmd;
2567 char cmdname[256];
2569 /* extract the command name */
2570 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
2571 if (!p)
2572 return NULL;
2574 cmd = search_dispatch_table(table, cmdname);
2575 if (!cmd) {
2576 monitor_printf(mon, "unknown command: '%.*s'\n",
2577 (int)(p - *cmdp), *cmdp);
2578 return NULL;
2581 /* filter out following useless space */
2582 while (qemu_isspace(*p)) {
2583 p++;
2586 *cmdp = p;
2587 /* search sub command */
2588 if (cmd->sub_table != NULL && *p != '\0') {
2589 return monitor_parse_command(mon, cmdp, cmd->sub_table);
2592 return cmd;
2596 * Parse arguments for @cmd.
2597 * If it can't be parsed, report to @mon, and return NULL.
2598 * Else, insert command arguments into a QDict, and return it.
2599 * Note: On success, caller has to free the QDict structure.
2602 static QDict *monitor_parse_arguments(Monitor *mon,
2603 const char **endp,
2604 const mon_cmd_t *cmd)
2606 const char *typestr;
2607 char *key;
2608 int c;
2609 const char *p = *endp;
2610 char buf[1024];
2611 QDict *qdict = qdict_new();
2613 /* parse the parameters */
2614 typestr = cmd->args_type;
2615 for(;;) {
2616 typestr = key_get_info(typestr, &key);
2617 if (!typestr)
2618 break;
2619 c = *typestr;
2620 typestr++;
2621 switch(c) {
2622 case 'F':
2623 case 'B':
2624 case 's':
2626 int ret;
2628 while (qemu_isspace(*p))
2629 p++;
2630 if (*typestr == '?') {
2631 typestr++;
2632 if (*p == '\0') {
2633 /* no optional string: NULL argument */
2634 break;
2637 ret = get_str(buf, sizeof(buf), &p);
2638 if (ret < 0) {
2639 switch(c) {
2640 case 'F':
2641 monitor_printf(mon, "%s: filename expected\n",
2642 cmd->name);
2643 break;
2644 case 'B':
2645 monitor_printf(mon, "%s: block device name expected\n",
2646 cmd->name);
2647 break;
2648 default:
2649 monitor_printf(mon, "%s: string expected\n", cmd->name);
2650 break;
2652 goto fail;
2654 qdict_put(qdict, key, qstring_from_str(buf));
2656 break;
2657 case 'O':
2659 QemuOptsList *opts_list;
2660 QemuOpts *opts;
2662 opts_list = qemu_find_opts(key);
2663 if (!opts_list || opts_list->desc->name) {
2664 goto bad_type;
2666 while (qemu_isspace(*p)) {
2667 p++;
2669 if (!*p)
2670 break;
2671 if (get_str(buf, sizeof(buf), &p) < 0) {
2672 goto fail;
2674 opts = qemu_opts_parse_noisily(opts_list, buf, true);
2675 if (!opts) {
2676 goto fail;
2678 qemu_opts_to_qdict(opts, qdict);
2679 qemu_opts_del(opts);
2681 break;
2682 case '/':
2684 int count, format, size;
2686 while (qemu_isspace(*p))
2687 p++;
2688 if (*p == '/') {
2689 /* format found */
2690 p++;
2691 count = 1;
2692 if (qemu_isdigit(*p)) {
2693 count = 0;
2694 while (qemu_isdigit(*p)) {
2695 count = count * 10 + (*p - '0');
2696 p++;
2699 size = -1;
2700 format = -1;
2701 for(;;) {
2702 switch(*p) {
2703 case 'o':
2704 case 'd':
2705 case 'u':
2706 case 'x':
2707 case 'i':
2708 case 'c':
2709 format = *p++;
2710 break;
2711 case 'b':
2712 size = 1;
2713 p++;
2714 break;
2715 case 'h':
2716 size = 2;
2717 p++;
2718 break;
2719 case 'w':
2720 size = 4;
2721 p++;
2722 break;
2723 case 'g':
2724 case 'L':
2725 size = 8;
2726 p++;
2727 break;
2728 default:
2729 goto next;
2732 next:
2733 if (*p != '\0' && !qemu_isspace(*p)) {
2734 monitor_printf(mon, "invalid char in format: '%c'\n",
2735 *p);
2736 goto fail;
2738 if (format < 0)
2739 format = default_fmt_format;
2740 if (format != 'i') {
2741 /* for 'i', not specifying a size gives -1 as size */
2742 if (size < 0)
2743 size = default_fmt_size;
2744 default_fmt_size = size;
2746 default_fmt_format = format;
2747 } else {
2748 count = 1;
2749 format = default_fmt_format;
2750 if (format != 'i') {
2751 size = default_fmt_size;
2752 } else {
2753 size = -1;
2756 qdict_put(qdict, "count", qint_from_int(count));
2757 qdict_put(qdict, "format", qint_from_int(format));
2758 qdict_put(qdict, "size", qint_from_int(size));
2760 break;
2761 case 'i':
2762 case 'l':
2763 case 'M':
2765 int64_t val;
2767 while (qemu_isspace(*p))
2768 p++;
2769 if (*typestr == '?' || *typestr == '.') {
2770 if (*typestr == '?') {
2771 if (*p == '\0') {
2772 typestr++;
2773 break;
2775 } else {
2776 if (*p == '.') {
2777 p++;
2778 while (qemu_isspace(*p))
2779 p++;
2780 } else {
2781 typestr++;
2782 break;
2785 typestr++;
2787 if (get_expr(mon, &val, &p))
2788 goto fail;
2789 /* Check if 'i' is greater than 32-bit */
2790 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2791 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
2792 monitor_printf(mon, "integer is for 32-bit values\n");
2793 goto fail;
2794 } else if (c == 'M') {
2795 if (val < 0) {
2796 monitor_printf(mon, "enter a positive value\n");
2797 goto fail;
2799 val <<= 20;
2801 qdict_put(qdict, key, qint_from_int(val));
2803 break;
2804 case 'o':
2806 int ret;
2807 uint64_t val;
2808 char *end;
2810 while (qemu_isspace(*p)) {
2811 p++;
2813 if (*typestr == '?') {
2814 typestr++;
2815 if (*p == '\0') {
2816 break;
2819 ret = qemu_strtosz_MiB(p, &end, &val);
2820 if (ret < 0 || val > INT64_MAX) {
2821 monitor_printf(mon, "invalid size\n");
2822 goto fail;
2824 qdict_put(qdict, key, qint_from_int(val));
2825 p = end;
2827 break;
2828 case 'T':
2830 double val;
2832 while (qemu_isspace(*p))
2833 p++;
2834 if (*typestr == '?') {
2835 typestr++;
2836 if (*p == '\0') {
2837 break;
2840 if (get_double(mon, &val, &p) < 0) {
2841 goto fail;
2843 if (p[0] && p[1] == 's') {
2844 switch (*p) {
2845 case 'm':
2846 val /= 1e3; p += 2; break;
2847 case 'u':
2848 val /= 1e6; p += 2; break;
2849 case 'n':
2850 val /= 1e9; p += 2; break;
2853 if (*p && !qemu_isspace(*p)) {
2854 monitor_printf(mon, "Unknown unit suffix\n");
2855 goto fail;
2857 qdict_put(qdict, key, qfloat_from_double(val));
2859 break;
2860 case 'b':
2862 const char *beg;
2863 bool val;
2865 while (qemu_isspace(*p)) {
2866 p++;
2868 beg = p;
2869 while (qemu_isgraph(*p)) {
2870 p++;
2872 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
2873 val = true;
2874 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
2875 val = false;
2876 } else {
2877 monitor_printf(mon, "Expected 'on' or 'off'\n");
2878 goto fail;
2880 qdict_put(qdict, key, qbool_from_bool(val));
2882 break;
2883 case '-':
2885 const char *tmp = p;
2886 int skip_key = 0;
2887 /* option */
2889 c = *typestr++;
2890 if (c == '\0')
2891 goto bad_type;
2892 while (qemu_isspace(*p))
2893 p++;
2894 if (*p == '-') {
2895 p++;
2896 if(c != *p) {
2897 if(!is_valid_option(p, typestr)) {
2899 monitor_printf(mon, "%s: unsupported option -%c\n",
2900 cmd->name, *p);
2901 goto fail;
2902 } else {
2903 skip_key = 1;
2906 if(skip_key) {
2907 p = tmp;
2908 } else {
2909 /* has option */
2910 p++;
2911 qdict_put(qdict, key, qbool_from_bool(true));
2915 break;
2916 case 'S':
2918 /* package all remaining string */
2919 int len;
2921 while (qemu_isspace(*p)) {
2922 p++;
2924 if (*typestr == '?') {
2925 typestr++;
2926 if (*p == '\0') {
2927 /* no remaining string: NULL argument */
2928 break;
2931 len = strlen(p);
2932 if (len <= 0) {
2933 monitor_printf(mon, "%s: string expected\n",
2934 cmd->name);
2935 goto fail;
2937 qdict_put(qdict, key, qstring_from_str(p));
2938 p += len;
2940 break;
2941 default:
2942 bad_type:
2943 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
2944 goto fail;
2946 g_free(key);
2947 key = NULL;
2949 /* check that all arguments were parsed */
2950 while (qemu_isspace(*p))
2951 p++;
2952 if (*p != '\0') {
2953 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2954 cmd->name);
2955 goto fail;
2958 return qdict;
2960 fail:
2961 QDECREF(qdict);
2962 g_free(key);
2963 return NULL;
2966 static void handle_hmp_command(Monitor *mon, const char *cmdline)
2968 QDict *qdict;
2969 const mon_cmd_t *cmd;
2971 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2972 if (!cmd) {
2973 return;
2976 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2977 if (!qdict) {
2978 monitor_printf(mon, "Try \"help %s\" for more information\n",
2979 cmd->name);
2980 return;
2983 cmd->cmd(mon, qdict);
2984 QDECREF(qdict);
2987 static void cmd_completion(Monitor *mon, const char *name, const char *list)
2989 const char *p, *pstart;
2990 char cmd[128];
2991 int len;
2993 p = list;
2994 for(;;) {
2995 pstart = p;
2996 p = strchr(p, '|');
2997 if (!p)
2998 p = pstart + strlen(pstart);
2999 len = p - pstart;
3000 if (len > sizeof(cmd) - 2)
3001 len = sizeof(cmd) - 2;
3002 memcpy(cmd, pstart, len);
3003 cmd[len] = '\0';
3004 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3005 readline_add_completion(mon->rs, cmd);
3007 if (*p == '\0')
3008 break;
3009 p++;
3013 static void file_completion(Monitor *mon, const char *input)
3015 DIR *ffs;
3016 struct dirent *d;
3017 char path[1024];
3018 char file[1024], file_prefix[1024];
3019 int input_path_len;
3020 const char *p;
3022 p = strrchr(input, '/');
3023 if (!p) {
3024 input_path_len = 0;
3025 pstrcpy(file_prefix, sizeof(file_prefix), input);
3026 pstrcpy(path, sizeof(path), ".");
3027 } else {
3028 input_path_len = p - input + 1;
3029 memcpy(path, input, input_path_len);
3030 if (input_path_len > sizeof(path) - 1)
3031 input_path_len = sizeof(path) - 1;
3032 path[input_path_len] = '\0';
3033 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3036 ffs = opendir(path);
3037 if (!ffs)
3038 return;
3039 for(;;) {
3040 struct stat sb;
3041 d = readdir(ffs);
3042 if (!d)
3043 break;
3045 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3046 continue;
3049 if (strstart(d->d_name, file_prefix, NULL)) {
3050 memcpy(file, input, input_path_len);
3051 if (input_path_len < sizeof(file))
3052 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3053 d->d_name);
3054 /* stat the file to find out if it's a directory.
3055 * In that case add a slash to speed up typing long paths
3057 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
3058 pstrcat(file, sizeof(file), "/");
3060 readline_add_completion(mon->rs, file);
3063 closedir(ffs);
3066 static const char *next_arg_type(const char *typestr)
3068 const char *p = strchr(typestr, ':');
3069 return (p != NULL ? ++p : typestr);
3072 static void add_completion_option(ReadLineState *rs, const char *str,
3073 const char *option)
3075 if (!str || !option) {
3076 return;
3078 if (!strncmp(option, str, strlen(str))) {
3079 readline_add_completion(rs, option);
3083 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3085 size_t len;
3086 ChardevBackendInfoList *list, *start;
3088 if (nb_args != 2) {
3089 return;
3091 len = strlen(str);
3092 readline_set_completion_index(rs, len);
3094 start = list = qmp_query_chardev_backends(NULL);
3095 while (list) {
3096 const char *chr_name = list->value->name;
3098 if (!strncmp(chr_name, str, len)) {
3099 readline_add_completion(rs, chr_name);
3101 list = list->next;
3103 qapi_free_ChardevBackendInfoList(start);
3106 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3108 size_t len;
3109 int i;
3111 if (nb_args != 2) {
3112 return;
3114 len = strlen(str);
3115 readline_set_completion_index(rs, len);
3116 for (i = 0; NetClientDriver_lookup[i]; i++) {
3117 add_completion_option(rs, str, NetClientDriver_lookup[i]);
3121 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
3123 GSList *list, *elt;
3124 size_t len;
3126 if (nb_args != 2) {
3127 return;
3130 len = strlen(str);
3131 readline_set_completion_index(rs, len);
3132 list = elt = object_class_get_list(TYPE_DEVICE, false);
3133 while (elt) {
3134 const char *name;
3135 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3136 TYPE_DEVICE);
3137 name = object_class_get_name(OBJECT_CLASS(dc));
3139 if (!dc->cannot_instantiate_with_device_add_yet
3140 && !strncmp(name, str, len)) {
3141 readline_add_completion(rs, name);
3143 elt = elt->next;
3145 g_slist_free(list);
3148 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
3150 GSList *list, *elt;
3151 size_t len;
3153 if (nb_args != 2) {
3154 return;
3157 len = strlen(str);
3158 readline_set_completion_index(rs, len);
3159 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3160 while (elt) {
3161 const char *name;
3163 name = object_class_get_name(OBJECT_CLASS(elt->data));
3164 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3165 readline_add_completion(rs, name);
3167 elt = elt->next;
3169 g_slist_free(list);
3172 static void peripheral_device_del_completion(ReadLineState *rs,
3173 const char *str, size_t len)
3175 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3176 GSList *list, *item;
3178 list = qdev_build_hotpluggable_device_list(peripheral);
3179 if (!list) {
3180 return;
3183 for (item = list; item; item = g_slist_next(item)) {
3184 DeviceState *dev = item->data;
3186 if (dev->id && !strncmp(str, dev->id, len)) {
3187 readline_add_completion(rs, dev->id);
3191 g_slist_free(list);
3194 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3196 size_t len;
3197 ChardevInfoList *list, *start;
3199 if (nb_args != 2) {
3200 return;
3202 len = strlen(str);
3203 readline_set_completion_index(rs, len);
3205 start = list = qmp_query_chardev(NULL);
3206 while (list) {
3207 ChardevInfo *chr = list->value;
3209 if (!strncmp(chr->label, str, len)) {
3210 readline_add_completion(rs, chr->label);
3212 list = list->next;
3214 qapi_free_ChardevInfoList(start);
3217 static void ringbuf_completion(ReadLineState *rs, const char *str)
3219 size_t len;
3220 ChardevInfoList *list, *start;
3222 len = strlen(str);
3223 readline_set_completion_index(rs, len);
3225 start = list = qmp_query_chardev(NULL);
3226 while (list) {
3227 ChardevInfo *chr_info = list->value;
3229 if (!strncmp(chr_info->label, str, len)) {
3230 Chardev *chr = qemu_chr_find(chr_info->label);
3231 if (chr && CHARDEV_IS_RINGBUF(chr)) {
3232 readline_add_completion(rs, chr_info->label);
3235 list = list->next;
3237 qapi_free_ChardevInfoList(start);
3240 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3242 if (nb_args != 2) {
3243 return;
3245 ringbuf_completion(rs, str);
3248 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3250 size_t len;
3252 if (nb_args != 2) {
3253 return;
3256 len = strlen(str);
3257 readline_set_completion_index(rs, len);
3258 peripheral_device_del_completion(rs, str, len);
3261 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
3263 ObjectPropertyInfoList *list, *start;
3264 size_t len;
3266 if (nb_args != 2) {
3267 return;
3269 len = strlen(str);
3270 readline_set_completion_index(rs, len);
3272 start = list = qmp_qom_list("/objects", NULL);
3273 while (list) {
3274 ObjectPropertyInfo *info = list->value;
3276 if (!strncmp(info->type, "child<", 5)
3277 && !strncmp(info->name, str, len)) {
3278 readline_add_completion(rs, info->name);
3280 list = list->next;
3282 qapi_free_ObjectPropertyInfoList(start);
3285 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3287 int i;
3288 char *sep;
3289 size_t len;
3291 if (nb_args != 2) {
3292 return;
3294 sep = strrchr(str, '-');
3295 if (sep) {
3296 str = sep + 1;
3298 len = strlen(str);
3299 readline_set_completion_index(rs, len);
3300 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
3301 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3302 readline_add_completion(rs, QKeyCode_lookup[i]);
3307 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3309 size_t len;
3311 len = strlen(str);
3312 readline_set_completion_index(rs, len);
3313 if (nb_args == 2) {
3314 NetClientState *ncs[MAX_QUEUE_NUM];
3315 int count, i;
3316 count = qemu_find_net_clients_except(NULL, ncs,
3317 NET_CLIENT_DRIVER_NONE,
3318 MAX_QUEUE_NUM);
3319 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3320 const char *name = ncs[i]->name;
3321 if (!strncmp(str, name, len)) {
3322 readline_add_completion(rs, name);
3325 } else if (nb_args == 3) {
3326 add_completion_option(rs, str, "on");
3327 add_completion_option(rs, str, "off");
3331 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3333 int len, count, i;
3334 NetClientState *ncs[MAX_QUEUE_NUM];
3336 if (nb_args != 2) {
3337 return;
3340 len = strlen(str);
3341 readline_set_completion_index(rs, len);
3342 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
3343 MAX_QUEUE_NUM);
3344 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3345 QemuOpts *opts;
3346 const char *name = ncs[i]->name;
3347 if (strncmp(str, name, len)) {
3348 continue;
3350 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3351 if (opts) {
3352 readline_add_completion(rs, name);
3357 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3359 size_t len;
3361 len = strlen(str);
3362 readline_set_completion_index(rs, len);
3363 if (nb_args == 2) {
3364 TraceEventIter iter;
3365 TraceEvent *ev;
3366 char *pattern = g_strdup_printf("%s*", str);
3367 trace_event_iter_init(&iter, pattern);
3368 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3369 readline_add_completion(rs, trace_event_get_name(ev));
3371 g_free(pattern);
3375 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3377 size_t len;
3379 len = strlen(str);
3380 readline_set_completion_index(rs, len);
3381 if (nb_args == 2) {
3382 TraceEventIter iter;
3383 TraceEvent *ev;
3384 char *pattern = g_strdup_printf("%s*", str);
3385 trace_event_iter_init(&iter, pattern);
3386 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3387 readline_add_completion(rs, trace_event_get_name(ev));
3389 g_free(pattern);
3390 } else if (nb_args == 3) {
3391 add_completion_option(rs, str, "on");
3392 add_completion_option(rs, str, "off");
3396 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3398 int i;
3400 if (nb_args != 2) {
3401 return;
3403 readline_set_completion_index(rs, strlen(str));
3404 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3405 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3409 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3410 const char *str)
3412 size_t len;
3414 len = strlen(str);
3415 readline_set_completion_index(rs, len);
3416 if (nb_args == 2) {
3417 int i;
3418 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3419 const char *name = MigrationCapability_lookup[i];
3420 if (!strncmp(str, name, len)) {
3421 readline_add_completion(rs, name);
3424 } else if (nb_args == 3) {
3425 add_completion_option(rs, str, "on");
3426 add_completion_option(rs, str, "off");
3430 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3431 const char *str)
3433 size_t len;
3435 len = strlen(str);
3436 readline_set_completion_index(rs, len);
3437 if (nb_args == 2) {
3438 int i;
3439 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
3440 const char *name = MigrationParameter_lookup[i];
3441 if (!strncmp(str, name, len)) {
3442 readline_add_completion(rs, name);
3448 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3450 int i;
3451 size_t len;
3452 if (nb_args != 2) {
3453 return;
3455 len = strlen(str);
3456 readline_set_completion_index(rs, len);
3457 for (i = 0; host_net_devices[i]; i++) {
3458 if (!strncmp(host_net_devices[i], str, len)) {
3459 readline_add_completion(rs, host_net_devices[i]);
3464 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3466 NetClientState *ncs[MAX_QUEUE_NUM];
3467 int count, i, len;
3469 len = strlen(str);
3470 readline_set_completion_index(rs, len);
3471 if (nb_args == 2) {
3472 count = qemu_find_net_clients_except(NULL, ncs,
3473 NET_CLIENT_DRIVER_NONE,
3474 MAX_QUEUE_NUM);
3475 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3476 int id;
3477 char name[16];
3479 if (net_hub_id_for_client(ncs[i], &id)) {
3480 continue;
3482 snprintf(name, sizeof(name), "%d", id);
3483 if (!strncmp(str, name, len)) {
3484 readline_add_completion(rs, name);
3487 return;
3488 } else if (nb_args == 3) {
3489 count = qemu_find_net_clients_except(NULL, ncs,
3490 NET_CLIENT_DRIVER_NIC,
3491 MAX_QUEUE_NUM);
3492 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3493 int id;
3494 const char *name;
3496 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
3497 net_hub_id_for_client(ncs[i], &id)) {
3498 continue;
3500 name = ncs[i]->name;
3501 if (!strncmp(str, name, len)) {
3502 readline_add_completion(rs, name);
3505 return;
3509 static void vm_completion(ReadLineState *rs, const char *str)
3511 size_t len;
3512 BlockDriverState *bs;
3513 BdrvNextIterator it;
3515 len = strlen(str);
3516 readline_set_completion_index(rs, len);
3518 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3519 SnapshotInfoList *snapshots, *snapshot;
3520 AioContext *ctx = bdrv_get_aio_context(bs);
3521 bool ok = false;
3523 aio_context_acquire(ctx);
3524 if (bdrv_can_snapshot(bs)) {
3525 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3527 aio_context_release(ctx);
3528 if (!ok) {
3529 continue;
3532 snapshot = snapshots;
3533 while (snapshot) {
3534 char *completion = snapshot->value->name;
3535 if (!strncmp(str, completion, len)) {
3536 readline_add_completion(rs, completion);
3538 completion = snapshot->value->id;
3539 if (!strncmp(str, completion, len)) {
3540 readline_add_completion(rs, completion);
3542 snapshot = snapshot->next;
3544 qapi_free_SnapshotInfoList(snapshots);
3549 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3551 if (nb_args == 2) {
3552 vm_completion(rs, str);
3556 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3558 if (nb_args == 2) {
3559 vm_completion(rs, str);
3563 static void monitor_find_completion_by_table(Monitor *mon,
3564 const mon_cmd_t *cmd_table,
3565 char **args,
3566 int nb_args)
3568 const char *cmdname;
3569 int i;
3570 const char *ptype, *str, *name;
3571 const mon_cmd_t *cmd;
3572 BlockBackend *blk = NULL;
3574 if (nb_args <= 1) {
3575 /* command completion */
3576 if (nb_args == 0)
3577 cmdname = "";
3578 else
3579 cmdname = args[0];
3580 readline_set_completion_index(mon->rs, strlen(cmdname));
3581 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3582 cmd_completion(mon, cmdname, cmd->name);
3584 } else {
3585 /* find the command */
3586 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3587 if (compare_cmd(args[0], cmd->name)) {
3588 break;
3591 if (!cmd->name) {
3592 return;
3595 if (cmd->sub_table) {
3596 /* do the job again */
3597 monitor_find_completion_by_table(mon, cmd->sub_table,
3598 &args[1], nb_args - 1);
3599 return;
3601 if (cmd->command_completion) {
3602 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3603 return;
3606 ptype = next_arg_type(cmd->args_type);
3607 for(i = 0; i < nb_args - 2; i++) {
3608 if (*ptype != '\0') {
3609 ptype = next_arg_type(ptype);
3610 while (*ptype == '?')
3611 ptype = next_arg_type(ptype);
3614 str = args[nb_args - 1];
3615 while (*ptype == '-' && ptype[1] != '\0') {
3616 ptype = next_arg_type(ptype);
3618 switch(*ptype) {
3619 case 'F':
3620 /* file completion */
3621 readline_set_completion_index(mon->rs, strlen(str));
3622 file_completion(mon, str);
3623 break;
3624 case 'B':
3625 /* block device name completion */
3626 readline_set_completion_index(mon->rs, strlen(str));
3627 while ((blk = blk_next(blk)) != NULL) {
3628 name = blk_name(blk);
3629 if (str[0] == '\0' ||
3630 !strncmp(name, str, strlen(str))) {
3631 readline_add_completion(mon->rs, name);
3634 break;
3635 case 's':
3636 case 'S':
3637 if (!strcmp(cmd->name, "help|?")) {
3638 monitor_find_completion_by_table(mon, cmd_table,
3639 &args[1], nb_args - 1);
3641 break;
3642 default:
3643 break;
3648 static void monitor_find_completion(void *opaque,
3649 const char *cmdline)
3651 Monitor *mon = opaque;
3652 char *args[MAX_ARGS];
3653 int nb_args, len;
3655 /* 1. parse the cmdline */
3656 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3657 return;
3660 /* if the line ends with a space, it means we want to complete the
3661 next arg */
3662 len = strlen(cmdline);
3663 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3664 if (nb_args >= MAX_ARGS) {
3665 goto cleanup;
3667 args[nb_args++] = g_strdup("");
3670 /* 2. auto complete according to args */
3671 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
3673 cleanup:
3674 free_cmdline_args(args, nb_args);
3677 static int monitor_can_read(void *opaque)
3679 Monitor *mon = opaque;
3681 return (mon->suspend_cnt == 0) ? 1 : 0;
3684 static bool invalid_qmp_mode(const Monitor *mon, const char *cmd,
3685 Error **errp)
3687 bool is_cap = g_str_equal(cmd, "qmp_capabilities");
3689 if (is_cap && mon->qmp.in_command_mode) {
3690 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3691 "Capabilities negotiation is already complete, command "
3692 "'%s' ignored", cmd);
3693 return true;
3695 if (!is_cap && !mon->qmp.in_command_mode) {
3696 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
3697 "Expecting capabilities negotiation with "
3698 "'qmp_capabilities' before command '%s'", cmd);
3699 return true;
3701 return false;
3705 * Input object checking rules
3707 * 1. Input object must be a dict
3708 * 2. The "execute" key must exist
3709 * 3. The "execute" key must be a string
3710 * 4. If the "arguments" key exists, it must be a dict
3711 * 5. If the "id" key exists, it can be anything (ie. json-value)
3712 * 6. Any argument not listed above is considered invalid
3714 static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
3716 const QDictEntry *ent;
3717 int has_exec_key = 0;
3718 QDict *input_dict;
3720 input_dict = qobject_to_qdict(input_obj);
3721 if (!input_dict) {
3722 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
3723 return NULL;
3727 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
3728 const char *arg_name = qdict_entry_key(ent);
3729 const QObject *arg_obj = qdict_entry_value(ent);
3731 if (!strcmp(arg_name, "execute")) {
3732 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
3733 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3734 "execute", "string");
3735 return NULL;
3737 has_exec_key = 1;
3738 } else if (!strcmp(arg_name, "arguments")) {
3739 if (qobject_type(arg_obj) != QTYPE_QDICT) {
3740 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
3741 "arguments", "object");
3742 return NULL;
3744 } else if (!strcmp(arg_name, "id")) {
3745 /* Any string is acceptable as "id", so nothing to check */
3746 } else {
3747 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
3748 return NULL;
3752 if (!has_exec_key) {
3753 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
3754 return NULL;
3757 return input_dict;
3760 static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
3762 QObject *req, *rsp = NULL, *id = NULL;
3763 QDict *qdict = NULL;
3764 const char *cmd_name;
3765 Monitor *mon = cur_mon;
3766 Error *err = NULL;
3768 req = json_parser_parse_err(tokens, NULL, &err);
3769 if (!req && !err) {
3770 /* json_parser_parse_err() sucks: can fail without setting @err */
3771 error_setg(&err, QERR_JSON_PARSING);
3773 if (err) {
3774 goto err_out;
3777 qdict = qmp_check_input_obj(req, &err);
3778 if (!qdict) {
3779 goto err_out;
3782 id = qdict_get(qdict, "id");
3783 qobject_incref(id);
3784 qdict_del(qdict, "id");
3786 cmd_name = qdict_get_str(qdict, "execute");
3787 trace_handle_qmp_command(mon, cmd_name);
3789 if (invalid_qmp_mode(mon, cmd_name, &err)) {
3790 goto err_out;
3793 rsp = qmp_dispatch(&qmp_commands, req);
3795 err_out:
3796 if (err) {
3797 qdict = qdict_new();
3798 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3799 error_free(err);
3800 rsp = QOBJECT(qdict);
3803 if (rsp) {
3804 if (id) {
3805 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3806 id = NULL;
3809 monitor_json_emitter(mon, rsp);
3812 qobject_decref(id);
3813 qobject_decref(rsp);
3814 qobject_decref(req);
3817 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
3819 Monitor *old_mon = cur_mon;
3821 cur_mon = opaque;
3823 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
3825 cur_mon = old_mon;
3828 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3830 Monitor *old_mon = cur_mon;
3831 int i;
3833 cur_mon = opaque;
3835 if (cur_mon->rs) {
3836 for (i = 0; i < size; i++)
3837 readline_handle_byte(cur_mon->rs, buf[i]);
3838 } else {
3839 if (size == 0 || buf[size - 1] != 0)
3840 monitor_printf(cur_mon, "corrupted command\n");
3841 else
3842 handle_hmp_command(cur_mon, (char *)buf);
3845 cur_mon = old_mon;
3848 static void monitor_command_cb(void *opaque, const char *cmdline,
3849 void *readline_opaque)
3851 Monitor *mon = opaque;
3853 monitor_suspend(mon);
3854 handle_hmp_command(mon, cmdline);
3855 monitor_resume(mon);
3858 int monitor_suspend(Monitor *mon)
3860 if (!mon->rs)
3861 return -ENOTTY;
3862 mon->suspend_cnt++;
3863 return 0;
3866 void monitor_resume(Monitor *mon)
3868 if (!mon->rs)
3869 return;
3870 if (--mon->suspend_cnt == 0)
3871 readline_show_prompt(mon->rs);
3874 static QObject *get_qmp_greeting(void)
3876 QObject *ver = NULL;
3878 qmp_marshal_query_version(NULL, &ver, NULL);
3880 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3881 ver);
3884 static void monitor_qmp_event(void *opaque, int event)
3886 QObject *data;
3887 Monitor *mon = opaque;
3889 switch (event) {
3890 case CHR_EVENT_OPENED:
3891 mon->qmp.in_command_mode = false;
3892 data = get_qmp_greeting();
3893 monitor_json_emitter(mon, data);
3894 qobject_decref(data);
3895 mon_refcount++;
3896 break;
3897 case CHR_EVENT_CLOSED:
3898 json_message_parser_destroy(&mon->qmp.parser);
3899 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
3900 mon_refcount--;
3901 monitor_fdsets_cleanup();
3902 break;
3906 static void monitor_event(void *opaque, int event)
3908 Monitor *mon = opaque;
3910 switch (event) {
3911 case CHR_EVENT_MUX_IN:
3912 qemu_mutex_lock(&mon->out_lock);
3913 mon->mux_out = 0;
3914 qemu_mutex_unlock(&mon->out_lock);
3915 if (mon->reset_seen) {
3916 readline_restart(mon->rs);
3917 monitor_resume(mon);
3918 monitor_flush(mon);
3919 } else {
3920 mon->suspend_cnt = 0;
3922 break;
3924 case CHR_EVENT_MUX_OUT:
3925 if (mon->reset_seen) {
3926 if (mon->suspend_cnt == 0) {
3927 monitor_printf(mon, "\n");
3929 monitor_flush(mon);
3930 monitor_suspend(mon);
3931 } else {
3932 mon->suspend_cnt++;
3934 qemu_mutex_lock(&mon->out_lock);
3935 mon->mux_out = 1;
3936 qemu_mutex_unlock(&mon->out_lock);
3937 break;
3939 case CHR_EVENT_OPENED:
3940 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3941 "information\n", QEMU_VERSION);
3942 if (!mon->mux_out) {
3943 readline_restart(mon->rs);
3944 readline_show_prompt(mon->rs);
3946 mon->reset_seen = 1;
3947 mon_refcount++;
3948 break;
3950 case CHR_EVENT_CLOSED:
3951 mon_refcount--;
3952 monitor_fdsets_cleanup();
3953 break;
3957 static int
3958 compare_mon_cmd(const void *a, const void *b)
3960 return strcmp(((const mon_cmd_t *)a)->name,
3961 ((const mon_cmd_t *)b)->name);
3964 static void sortcmdlist(void)
3966 int array_num;
3967 int elem_size = sizeof(mon_cmd_t);
3969 array_num = sizeof(mon_cmds)/elem_size-1;
3970 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
3972 array_num = sizeof(info_cmds)/elem_size-1;
3973 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
3976 /* These functions just adapt the readline interface in a typesafe way. We
3977 * could cast function pointers but that discards compiler checks.
3979 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
3980 const char *fmt, ...)
3982 va_list ap;
3983 va_start(ap, fmt);
3984 monitor_vprintf(opaque, fmt, ap);
3985 va_end(ap);
3988 static void monitor_readline_flush(void *opaque)
3990 monitor_flush(opaque);
3994 * Print to current monitor if we have one, else to stderr.
3995 * TODO should return int, so callers can calculate width, but that
3996 * requires surgery to monitor_vprintf(). Left for another day.
3998 void error_vprintf(const char *fmt, va_list ap)
4000 if (cur_mon && !monitor_cur_is_qmp()) {
4001 monitor_vprintf(cur_mon, fmt, ap);
4002 } else {
4003 vfprintf(stderr, fmt, ap);
4007 void error_vprintf_unless_qmp(const char *fmt, va_list ap)
4009 if (cur_mon && !monitor_cur_is_qmp()) {
4010 monitor_vprintf(cur_mon, fmt, ap);
4011 } else if (!cur_mon) {
4012 vfprintf(stderr, fmt, ap);
4016 static void __attribute__((constructor)) monitor_lock_init(void)
4018 qemu_mutex_init(&monitor_lock);
4021 void monitor_init(Chardev *chr, int flags)
4023 static int is_first_init = 1;
4024 Monitor *mon;
4026 if (is_first_init) {
4027 monitor_qapi_event_init();
4028 sortcmdlist();
4029 is_first_init = 0;
4032 mon = g_malloc(sizeof(*mon));
4033 monitor_data_init(mon);
4035 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
4036 mon->flags = flags;
4037 if (flags & MONITOR_USE_READLINE) {
4038 mon->rs = readline_init(monitor_readline_printf,
4039 monitor_readline_flush,
4040 mon,
4041 monitor_find_completion);
4042 monitor_read_command(mon, 0);
4045 if (monitor_is_qmp(mon)) {
4046 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
4047 monitor_qmp_event, mon, NULL, true);
4048 qemu_chr_fe_set_echo(&mon->chr, true);
4049 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
4050 } else {
4051 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
4052 monitor_event, mon, NULL, true);
4055 qemu_mutex_lock(&monitor_lock);
4056 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4057 qemu_mutex_unlock(&monitor_lock);
4060 void monitor_cleanup(void)
4062 Monitor *mon, *next;
4064 qemu_mutex_lock(&monitor_lock);
4065 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4066 QLIST_REMOVE(mon, entry);
4067 monitor_data_destroy(mon);
4068 g_free(mon);
4070 qemu_mutex_unlock(&monitor_lock);
4073 static void bdrv_password_cb(void *opaque, const char *password,
4074 void *readline_opaque)
4076 Monitor *mon = opaque;
4077 BlockDriverState *bs = readline_opaque;
4078 int ret = 0;
4079 Error *local_err = NULL;
4081 bdrv_add_key(bs, password, &local_err);
4082 if (local_err) {
4083 error_report_err(local_err);
4084 ret = -EPERM;
4086 if (mon->password_completion_cb)
4087 mon->password_completion_cb(mon->password_opaque, ret);
4089 monitor_read_command(mon, 1);
4092 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4093 BlockCompletionFunc *completion_cb,
4094 void *opaque)
4096 int err;
4098 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4099 bdrv_get_encrypted_filename(bs));
4101 mon->password_completion_cb = completion_cb;
4102 mon->password_opaque = opaque;
4104 err = monitor_read_password(mon, bdrv_password_cb, bs);
4106 if (err && completion_cb)
4107 completion_cb(opaque, err);
4109 return err;
4112 int monitor_read_block_device_key(Monitor *mon, const char *device,
4113 BlockCompletionFunc *completion_cb,
4114 void *opaque)
4116 Error *err = NULL;
4117 BlockBackend *blk;
4119 blk = blk_by_name(device);
4120 if (!blk) {
4121 monitor_printf(mon, "Device not found %s\n", device);
4122 return -1;
4124 if (!blk_bs(blk)) {
4125 monitor_printf(mon, "Device '%s' has no medium\n", device);
4126 return -1;
4129 bdrv_add_key(blk_bs(blk), NULL, &err);
4130 if (err) {
4131 error_free(err);
4132 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4135 if (completion_cb) {
4136 completion_cb(opaque, 0);
4138 return 0;
4141 QemuOptsList qemu_mon_opts = {
4142 .name = "mon",
4143 .implied_opt_name = "chardev",
4144 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4145 .desc = {
4147 .name = "mode",
4148 .type = QEMU_OPT_STRING,
4150 .name = "chardev",
4151 .type = QEMU_OPT_STRING,
4153 .name = "default", /* deprecated */
4154 .type = QEMU_OPT_BOOL,
4156 .name = "pretty",
4157 .type = QEMU_OPT_BOOL,
4159 { /* end of list */ }
4163 #ifndef TARGET_I386
4164 void qmp_rtc_reset_reinjection(Error **errp)
4166 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
4168 #endif
4170 #ifndef TARGET_S390X
4171 void qmp_dump_skeys(const char *filename, Error **errp)
4173 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4175 #endif
4177 #ifndef TARGET_ARM
4178 GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4180 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4181 return NULL;
4183 #endif
4185 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4187 MachineState *ms = MACHINE(qdev_get_machine());
4188 MachineClass *mc = MACHINE_GET_CLASS(ms);
4190 if (!mc->has_hotpluggable_cpus) {
4191 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4192 return NULL;
4195 return machine_query_hotpluggable_cpus(ms);