virtio: Fix error handling in virtio_bus_device_plugged
[qemu/kevin.git] / monitor.c
blobbe282ecb80fbcc44555bce60c0a2f072f7b3a581
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 "sysemu/cpus.h"
81 #include "qemu/cutils.h"
82 #include "qapi/qmp/dispatch.h"
84 #if defined(TARGET_S390X)
85 #include "hw/s390x/storage-keys.h"
86 #endif
89 * Supported types:
91 * 'F' filename
92 * 'B' block device name
93 * 's' string (accept optional quote)
94 * 'S' it just appends the rest of the string (accept optional quote)
95 * 'O' option string of the form NAME=VALUE,...
96 * parsed according to QemuOptsList given by its name
97 * Example: 'device:O' uses qemu_device_opts.
98 * Restriction: only lists with empty desc are supported
99 * TODO lift the restriction
100 * 'i' 32 bit integer
101 * 'l' target long (32 or 64 bit)
102 * 'M' Non-negative target long (32 or 64 bit), in user mode the
103 * value is multiplied by 2^20 (think Mebibyte)
104 * 'o' octets (aka bytes)
105 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
106 * K, k suffix, which multiplies the value by 2^60 for suffixes E
107 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
108 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
109 * 'T' double
110 * user mode accepts an optional ms, us, ns suffix,
111 * which divides the value by 1e3, 1e6, 1e9, respectively
112 * '/' optional gdb-like print format (like "/10x")
114 * '?' optional type (for all types, except '/')
115 * '.' other form of optional type (for 'i' and 'l')
116 * 'b' boolean
117 * user mode accepts "on" or "off"
118 * '-' optional parameter (eg. '-f')
122 typedef struct mon_cmd_t {
123 const char *name;
124 const char *args_type;
125 const char *params;
126 const char *help;
127 void (*cmd)(Monitor *mon, const QDict *qdict);
128 /* @sub_table is a list of 2nd level of commands. If it does not exist,
129 * cmd should be used. If it exists, sub_table[?].cmd should be
130 * used, and cmd of 1st level plays the role of help function.
132 struct mon_cmd_t *sub_table;
133 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
134 } mon_cmd_t;
136 /* file descriptors passed via SCM_RIGHTS */
137 typedef struct mon_fd_t mon_fd_t;
138 struct mon_fd_t {
139 char *name;
140 int fd;
141 QLIST_ENTRY(mon_fd_t) next;
144 /* file descriptor associated with a file descriptor set */
145 typedef struct MonFdsetFd MonFdsetFd;
146 struct MonFdsetFd {
147 int fd;
148 bool removed;
149 char *opaque;
150 QLIST_ENTRY(MonFdsetFd) next;
153 /* file descriptor set containing fds passed via SCM_RIGHTS */
154 typedef struct MonFdset MonFdset;
155 struct MonFdset {
156 int64_t id;
157 QLIST_HEAD(, MonFdsetFd) fds;
158 QLIST_HEAD(, MonFdsetFd) dup_fds;
159 QLIST_ENTRY(MonFdset) next;
162 typedef struct {
163 JSONMessageParser parser;
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
167 * mode.
169 QmpCommandList *commands;
170 } MonitorQMP;
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
177 typedef struct MonitorQAPIEventState {
178 QAPIEvent event; /* Throttling state for this event type and... */
179 QDict *data; /* ... data, see qapi_event_throttle_equal() */
180 QEMUTimer *timer; /* Timer for handling delayed events */
181 QDict *qdict; /* Delayed event (if any) */
182 } MonitorQAPIEventState;
184 typedef struct {
185 int64_t rate; /* Minimum time (in ns) between two events */
186 } MonitorQAPIEventConf;
188 struct Monitor {
189 CharBackend chr;
190 int reset_seen;
191 int flags;
192 int suspend_cnt;
193 bool skip_flush;
195 QemuMutex out_lock;
196 QString *outbuf;
197 guint out_watch;
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
200 int mux_out;
202 ReadLineState *rs;
203 MonitorQMP qmp;
204 CPUState *mon_cpu;
205 BlockCompletionFunc *password_completion_cb;
206 void *password_opaque;
207 mon_cmd_t *cmd_table;
208 QLIST_HEAD(,mon_fd_t) fds;
209 QLIST_ENTRY(Monitor) entry;
212 /* QMP checker flags */
213 #define QMP_ACCEPT_UNKNOWNS 1
215 /* Protects mon_list, monitor_event_state. */
216 static QemuMutex monitor_lock;
218 static QLIST_HEAD(mon_list, Monitor) mon_list;
219 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
220 static int mon_refcount;
222 static mon_cmd_t mon_cmds[];
223 static mon_cmd_t info_cmds[];
225 QmpCommandList qmp_commands, qmp_cap_negotiation_commands;
227 Monitor *cur_mon;
229 static QEMUClockType event_clock_type = QEMU_CLOCK_REALTIME;
231 static void monitor_command_cb(void *opaque, const char *cmdline,
232 void *readline_opaque);
235 * Is @mon a QMP monitor?
237 static inline bool monitor_is_qmp(const Monitor *mon)
239 return (mon->flags & MONITOR_USE_CONTROL);
243 * Is the current monitor, if any, a QMP monitor?
245 bool monitor_cur_is_qmp(void)
247 return cur_mon && monitor_is_qmp(cur_mon);
250 void monitor_read_command(Monitor *mon, int show_prompt)
252 if (!mon->rs)
253 return;
255 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
256 if (show_prompt)
257 readline_show_prompt(mon->rs);
260 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
261 void *opaque)
263 if (mon->rs) {
264 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
265 /* prompt is printed on return from the command handler */
266 return 0;
267 } else {
268 monitor_printf(mon, "terminal does not support password prompting\n");
269 return -ENOTTY;
273 static void monitor_flush_locked(Monitor *mon);
275 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
276 void *opaque)
278 Monitor *mon = opaque;
280 qemu_mutex_lock(&mon->out_lock);
281 mon->out_watch = 0;
282 monitor_flush_locked(mon);
283 qemu_mutex_unlock(&mon->out_lock);
284 return FALSE;
287 /* Called with mon->out_lock held. */
288 static void monitor_flush_locked(Monitor *mon)
290 int rc;
291 size_t len;
292 const char *buf;
294 if (mon->skip_flush) {
295 return;
298 buf = qstring_get_str(mon->outbuf);
299 len = qstring_get_length(mon->outbuf);
301 if (len && !mon->mux_out) {
302 rc = qemu_chr_fe_write(&mon->chr, (const uint8_t *) buf, len);
303 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
304 /* all flushed or error */
305 QDECREF(mon->outbuf);
306 mon->outbuf = qstring_new();
307 return;
309 if (rc > 0) {
310 /* partial write */
311 QString *tmp = qstring_from_str(buf + rc);
312 QDECREF(mon->outbuf);
313 mon->outbuf = tmp;
315 if (mon->out_watch == 0) {
316 mon->out_watch =
317 qemu_chr_fe_add_watch(&mon->chr, G_IO_OUT | G_IO_HUP,
318 monitor_unblocked, mon);
323 void monitor_flush(Monitor *mon)
325 qemu_mutex_lock(&mon->out_lock);
326 monitor_flush_locked(mon);
327 qemu_mutex_unlock(&mon->out_lock);
330 /* flush at every end of line */
331 static void monitor_puts(Monitor *mon, const char *str)
333 char c;
335 qemu_mutex_lock(&mon->out_lock);
336 for(;;) {
337 c = *str++;
338 if (c == '\0')
339 break;
340 if (c == '\n') {
341 qstring_append_chr(mon->outbuf, '\r');
343 qstring_append_chr(mon->outbuf, c);
344 if (c == '\n') {
345 monitor_flush_locked(mon);
348 qemu_mutex_unlock(&mon->out_lock);
351 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
353 char *buf;
355 if (!mon)
356 return;
358 if (monitor_is_qmp(mon)) {
359 return;
362 buf = g_strdup_vprintf(fmt, ap);
363 monitor_puts(mon, buf);
364 g_free(buf);
367 void monitor_printf(Monitor *mon, const char *fmt, ...)
369 va_list ap;
370 va_start(ap, fmt);
371 monitor_vprintf(mon, fmt, ap);
372 va_end(ap);
375 int monitor_fprintf(FILE *stream, const char *fmt, ...)
377 va_list ap;
378 va_start(ap, fmt);
379 monitor_vprintf((Monitor *)stream, fmt, ap);
380 va_end(ap);
381 return 0;
384 static void monitor_json_emitter(Monitor *mon, const QObject *data)
386 QString *json;
388 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
389 qobject_to_json(data);
390 assert(json != NULL);
392 qstring_append_chr(json, '\n');
393 monitor_puts(mon, qstring_get_str(json));
395 QDECREF(json);
398 static MonitorQAPIEventConf monitor_qapi_event_conf[QAPI_EVENT__MAX] = {
399 /* Limit guest-triggerable events to 1 per second */
400 [QAPI_EVENT_RTC_CHANGE] = { 1000 * SCALE_MS },
401 [QAPI_EVENT_WATCHDOG] = { 1000 * SCALE_MS },
402 [QAPI_EVENT_BALLOON_CHANGE] = { 1000 * SCALE_MS },
403 [QAPI_EVENT_QUORUM_REPORT_BAD] = { 1000 * SCALE_MS },
404 [QAPI_EVENT_QUORUM_FAILURE] = { 1000 * SCALE_MS },
405 [QAPI_EVENT_VSERPORT_CHANGE] = { 1000 * SCALE_MS },
408 GHashTable *monitor_qapi_event_state;
411 * Emits the event to every monitor instance, @event is only used for trace
412 * Called with monitor_lock held.
414 static void monitor_qapi_event_emit(QAPIEvent event, QDict *qdict)
416 Monitor *mon;
418 trace_monitor_protocol_event_emit(event, qdict);
419 QLIST_FOREACH(mon, &mon_list, entry) {
420 if (monitor_is_qmp(mon)
421 && mon->qmp.commands != &qmp_cap_negotiation_commands) {
422 monitor_json_emitter(mon, QOBJECT(qdict));
427 static void monitor_qapi_event_handler(void *opaque);
430 * Queue a new event for emission to Monitor instances,
431 * applying any rate limiting if required.
433 static void
434 monitor_qapi_event_queue(QAPIEvent event, QDict *qdict, Error **errp)
436 MonitorQAPIEventConf *evconf;
437 MonitorQAPIEventState *evstate;
439 assert(event < QAPI_EVENT__MAX);
440 evconf = &monitor_qapi_event_conf[event];
441 trace_monitor_protocol_event_queue(event, qdict, evconf->rate);
443 qemu_mutex_lock(&monitor_lock);
445 if (!evconf->rate) {
446 /* Unthrottled event */
447 monitor_qapi_event_emit(event, qdict);
448 } else {
449 QDict *data = qobject_to_qdict(qdict_get(qdict, "data"));
450 MonitorQAPIEventState key = { .event = event, .data = data };
452 evstate = g_hash_table_lookup(monitor_qapi_event_state, &key);
453 assert(!evstate || timer_pending(evstate->timer));
455 if (evstate) {
457 * Timer is pending for (at least) evconf->rate ns after
458 * last send. Store event for sending when timer fires,
459 * replacing a prior stored event if any.
461 QDECREF(evstate->qdict);
462 evstate->qdict = qdict;
463 QINCREF(evstate->qdict);
464 } else {
466 * Last send was (at least) evconf->rate ns ago.
467 * Send immediately, and arm the timer to call
468 * monitor_qapi_event_handler() in evconf->rate ns. Any
469 * events arriving before then will be delayed until then.
471 int64_t now = qemu_clock_get_ns(event_clock_type);
473 monitor_qapi_event_emit(event, qdict);
475 evstate = g_new(MonitorQAPIEventState, 1);
476 evstate->event = event;
477 evstate->data = data;
478 QINCREF(evstate->data);
479 evstate->qdict = NULL;
480 evstate->timer = timer_new_ns(event_clock_type,
481 monitor_qapi_event_handler,
482 evstate);
483 g_hash_table_add(monitor_qapi_event_state, evstate);
484 timer_mod_ns(evstate->timer, now + evconf->rate);
488 qemu_mutex_unlock(&monitor_lock);
492 * This function runs evconf->rate ns after sending a throttled
493 * event.
494 * If another event has since been stored, send it.
496 static void monitor_qapi_event_handler(void *opaque)
498 MonitorQAPIEventState *evstate = opaque;
499 MonitorQAPIEventConf *evconf = &monitor_qapi_event_conf[evstate->event];
501 trace_monitor_protocol_event_handler(evstate->event, evstate->qdict);
502 qemu_mutex_lock(&monitor_lock);
504 if (evstate->qdict) {
505 int64_t now = qemu_clock_get_ns(event_clock_type);
507 monitor_qapi_event_emit(evstate->event, evstate->qdict);
508 QDECREF(evstate->qdict);
509 evstate->qdict = NULL;
510 timer_mod_ns(evstate->timer, now + evconf->rate);
511 } else {
512 g_hash_table_remove(monitor_qapi_event_state, evstate);
513 QDECREF(evstate->data);
514 timer_free(evstate->timer);
515 g_free(evstate);
518 qemu_mutex_unlock(&monitor_lock);
521 static unsigned int qapi_event_throttle_hash(const void *key)
523 const MonitorQAPIEventState *evstate = key;
524 unsigned int hash = evstate->event * 255;
526 if (evstate->event == QAPI_EVENT_VSERPORT_CHANGE) {
527 hash += g_str_hash(qdict_get_str(evstate->data, "id"));
530 if (evstate->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
531 hash += g_str_hash(qdict_get_str(evstate->data, "node-name"));
534 return hash;
537 static gboolean qapi_event_throttle_equal(const void *a, const void *b)
539 const MonitorQAPIEventState *eva = a;
540 const MonitorQAPIEventState *evb = b;
542 if (eva->event != evb->event) {
543 return FALSE;
546 if (eva->event == QAPI_EVENT_VSERPORT_CHANGE) {
547 return !strcmp(qdict_get_str(eva->data, "id"),
548 qdict_get_str(evb->data, "id"));
551 if (eva->event == QAPI_EVENT_QUORUM_REPORT_BAD) {
552 return !strcmp(qdict_get_str(eva->data, "node-name"),
553 qdict_get_str(evb->data, "node-name"));
556 return TRUE;
559 static void monitor_qapi_event_init(void)
561 if (qtest_enabled()) {
562 event_clock_type = QEMU_CLOCK_VIRTUAL;
565 monitor_qapi_event_state = g_hash_table_new(qapi_event_throttle_hash,
566 qapi_event_throttle_equal);
567 qmp_event_set_func_emit(monitor_qapi_event_queue);
570 static void handle_hmp_command(Monitor *mon, const char *cmdline);
572 static void monitor_data_init(Monitor *mon)
574 memset(mon, 0, sizeof(Monitor));
575 qemu_mutex_init(&mon->out_lock);
576 mon->outbuf = qstring_new();
577 /* Use *mon_cmds by default. */
578 mon->cmd_table = mon_cmds;
581 static void monitor_data_destroy(Monitor *mon)
583 qemu_chr_fe_deinit(&mon->chr);
584 if (monitor_is_qmp(mon)) {
585 json_message_parser_destroy(&mon->qmp.parser);
587 g_free(mon->rs);
588 QDECREF(mon->outbuf);
589 qemu_mutex_destroy(&mon->out_lock);
592 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
593 int64_t cpu_index, Error **errp)
595 char *output = NULL;
596 Monitor *old_mon, hmp;
598 monitor_data_init(&hmp);
599 hmp.skip_flush = true;
601 old_mon = cur_mon;
602 cur_mon = &hmp;
604 if (has_cpu_index) {
605 int ret = monitor_set_cpu(cpu_index);
606 if (ret < 0) {
607 cur_mon = old_mon;
608 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
609 "a CPU number");
610 goto out;
614 handle_hmp_command(&hmp, command_line);
615 cur_mon = old_mon;
617 qemu_mutex_lock(&hmp.out_lock);
618 if (qstring_get_length(hmp.outbuf) > 0) {
619 output = g_strdup(qstring_get_str(hmp.outbuf));
620 } else {
621 output = g_strdup("");
623 qemu_mutex_unlock(&hmp.out_lock);
625 out:
626 monitor_data_destroy(&hmp);
627 return output;
630 static int compare_cmd(const char *name, const char *list)
632 const char *p, *pstart;
633 int len;
634 len = strlen(name);
635 p = list;
636 for(;;) {
637 pstart = p;
638 p = strchr(p, '|');
639 if (!p)
640 p = pstart + strlen(pstart);
641 if ((p - pstart) == len && !memcmp(pstart, name, len))
642 return 1;
643 if (*p == '\0')
644 break;
645 p++;
647 return 0;
650 static int get_str(char *buf, int buf_size, const char **pp)
652 const char *p;
653 char *q;
654 int c;
656 q = buf;
657 p = *pp;
658 while (qemu_isspace(*p)) {
659 p++;
661 if (*p == '\0') {
662 fail:
663 *q = '\0';
664 *pp = p;
665 return -1;
667 if (*p == '\"') {
668 p++;
669 while (*p != '\0' && *p != '\"') {
670 if (*p == '\\') {
671 p++;
672 c = *p++;
673 switch (c) {
674 case 'n':
675 c = '\n';
676 break;
677 case 'r':
678 c = '\r';
679 break;
680 case '\\':
681 case '\'':
682 case '\"':
683 break;
684 default:
685 printf("unsupported escape code: '\\%c'\n", c);
686 goto fail;
688 if ((q - buf) < buf_size - 1) {
689 *q++ = c;
691 } else {
692 if ((q - buf) < buf_size - 1) {
693 *q++ = *p;
695 p++;
698 if (*p != '\"') {
699 printf("unterminated string\n");
700 goto fail;
702 p++;
703 } else {
704 while (*p != '\0' && !qemu_isspace(*p)) {
705 if ((q - buf) < buf_size - 1) {
706 *q++ = *p;
708 p++;
711 *q = '\0';
712 *pp = p;
713 return 0;
716 #define MAX_ARGS 16
718 static void free_cmdline_args(char **args, int nb_args)
720 int i;
722 assert(nb_args <= MAX_ARGS);
724 for (i = 0; i < nb_args; i++) {
725 g_free(args[i]);
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
735 * NOT be NULL.
737 * Returns 0 on success, negative on failure.
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
743 static int parse_cmdline(const char *cmdline,
744 int *pnb_args, char **args)
746 const char *p;
747 int nb_args, ret;
748 char buf[1024];
750 p = cmdline;
751 nb_args = 0;
752 for (;;) {
753 while (qemu_isspace(*p)) {
754 p++;
756 if (*p == '\0') {
757 break;
759 if (nb_args >= MAX_ARGS) {
760 goto fail;
762 ret = get_str(buf, sizeof(buf), &p);
763 if (ret < 0) {
764 goto fail;
766 args[nb_args] = g_strdup(buf);
767 nb_args++;
769 *pnb_args = nb_args;
770 return 0;
772 fail:
773 free_cmdline_args(args, nb_args);
774 return -1;
777 static void help_cmd_dump_one(Monitor *mon,
778 const mon_cmd_t *cmd,
779 char **prefix_args,
780 int prefix_args_nb)
782 int i;
784 for (i = 0; i < prefix_args_nb; i++) {
785 monitor_printf(mon, "%s ", prefix_args[i]);
787 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
790 /* @args[@arg_index] is the valid command need to find in @cmds */
791 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
792 char **args, int nb_args, int arg_index)
794 const mon_cmd_t *cmd;
796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index >= nb_args) {
798 for (cmd = cmds; cmd->name != NULL; cmd++) {
799 help_cmd_dump_one(mon, cmd, args, arg_index);
801 return;
804 /* Find one entry to dump */
805 for (cmd = cmds; cmd->name != NULL; cmd++) {
806 if (compare_cmd(args[arg_index], cmd->name)) {
807 if (cmd->sub_table) {
808 /* continue with next arg */
809 help_cmd_dump(mon, cmd->sub_table,
810 args, nb_args, arg_index + 1);
811 } else {
812 help_cmd_dump_one(mon, cmd, args, arg_index);
814 break;
819 static void help_cmd(Monitor *mon, const char *name)
821 char *args[MAX_ARGS];
822 int nb_args = 0;
824 /* 1. parse user input */
825 if (name) {
826 /* special case for log, directly dump and return */
827 if (!strcmp(name, "log")) {
828 const QEMULogItem *item;
829 monitor_printf(mon, "Log items (comma separated):\n");
830 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
831 for (item = qemu_log_items; item->mask != 0; item++) {
832 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
834 return;
837 if (parse_cmdline(name, &nb_args, args) < 0) {
838 return;
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
845 free_cmdline_args(args, nb_args);
848 static void do_help_cmd(Monitor *mon, const QDict *qdict)
850 help_cmd(mon, qdict_get_try_str(qdict, "name"));
853 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
855 const char *tp_name = qdict_get_str(qdict, "name");
856 bool new_state = qdict_get_bool(qdict, "option");
857 bool has_vcpu = qdict_haskey(qdict, "vcpu");
858 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
859 Error *local_err = NULL;
861 if (vcpu < 0) {
862 monitor_printf(mon, "argument vcpu must be positive");
863 return;
866 qmp_trace_event_set_state(tp_name, new_state, true, true, has_vcpu, vcpu, &local_err);
867 if (local_err) {
868 error_report_err(local_err);
872 #ifdef CONFIG_TRACE_SIMPLE
873 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
875 const char *op = qdict_get_try_str(qdict, "op");
876 const char *arg = qdict_get_try_str(qdict, "arg");
878 if (!op) {
879 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
880 } else if (!strcmp(op, "on")) {
881 st_set_trace_file_enabled(true);
882 } else if (!strcmp(op, "off")) {
883 st_set_trace_file_enabled(false);
884 } else if (!strcmp(op, "flush")) {
885 st_flush_trace_buffer();
886 } else if (!strcmp(op, "set")) {
887 if (arg) {
888 st_set_trace_file(arg);
890 } else {
891 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
892 help_cmd(mon, "trace-file");
895 #endif
897 static void hmp_info_help(Monitor *mon, const QDict *qdict)
899 help_cmd(mon, "info");
902 static void query_commands_cb(QmpCommand *cmd, void *opaque)
904 CommandInfoList *info, **list = opaque;
906 if (!cmd->enabled) {
907 return;
910 info = g_malloc0(sizeof(*info));
911 info->value = g_malloc0(sizeof(*info->value));
912 info->value->name = g_strdup(cmd->name);
913 info->next = *list;
914 *list = info;
917 CommandInfoList *qmp_query_commands(Error **errp)
919 CommandInfoList *list = NULL;
921 qmp_for_each_command(cur_mon->qmp.commands, query_commands_cb, &list);
923 return list;
926 EventInfoList *qmp_query_events(Error **errp)
928 EventInfoList *info, *ev_list = NULL;
929 QAPIEvent e;
931 for (e = 0 ; e < QAPI_EVENT__MAX ; e++) {
932 const char *event_name = QAPIEvent_lookup[e];
933 assert(event_name != NULL);
934 info = g_malloc0(sizeof(*info));
935 info->value = g_malloc0(sizeof(*info->value));
936 info->value->name = g_strdup(event_name);
938 info->next = ev_list;
939 ev_list = info;
942 return ev_list;
946 * Minor hack: generated marshalling suppressed for this command
947 * ('gen': false in the schema) so we can parse the JSON string
948 * directly into QObject instead of first parsing it with
949 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
950 * to QObject with generated output marshallers, every time. Instead,
951 * we do it in test-qobject-input-visitor.c, just to make sure
952 * qapi-introspect.py's output actually conforms to the schema.
954 static void qmp_query_qmp_schema(QDict *qdict, QObject **ret_data,
955 Error **errp)
957 *ret_data = qobject_from_json(qmp_schema_json, &error_abort);
961 * We used to define commands in qmp-commands.hx in addition to the
962 * QAPI schema. This permitted defining some of them only in certain
963 * configurations. query-commands has always reflected that (good,
964 * because it lets QMP clients figure out what's actually available),
965 * while query-qmp-schema never did (not so good). This function is a
966 * hack to keep the configuration-specific commands defined exactly as
967 * before, even though qmp-commands.hx is gone.
969 * FIXME Educate the QAPI schema on configuration-specific commands,
970 * and drop this hack.
972 static void qmp_unregister_commands_hack(void)
974 #ifndef CONFIG_SPICE
975 qmp_unregister_command(&qmp_commands, "query-spice");
976 #endif
977 #ifndef TARGET_I386
978 qmp_unregister_command(&qmp_commands, "rtc-reset-reinjection");
979 #endif
980 #ifndef TARGET_S390X
981 qmp_unregister_command(&qmp_commands, "dump-skeys");
982 #endif
983 #ifndef TARGET_ARM
984 qmp_unregister_command(&qmp_commands, "query-gic-capabilities");
985 #endif
986 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
987 qmp_unregister_command(&qmp_commands, "query-cpu-model-expansion");
988 #endif
989 #if !defined(TARGET_S390X)
990 qmp_unregister_command(&qmp_commands, "query-cpu-model-baseline");
991 qmp_unregister_command(&qmp_commands, "query-cpu-model-comparison");
992 #endif
993 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
994 && !defined(TARGET_S390X)
995 qmp_unregister_command(&qmp_commands, "query-cpu-definitions");
996 #endif
999 void monitor_init_qmp_commands(void)
1002 * Two command lists:
1003 * - qmp_commands contains all QMP commands
1004 * - qmp_cap_negotiation_commands contains just
1005 * "qmp_capabilities", to enforce capability negotiation
1008 qmp_init_marshal(&qmp_commands);
1010 qmp_register_command(&qmp_commands, "query-qmp-schema",
1011 qmp_query_qmp_schema,
1012 QCO_NO_OPTIONS);
1013 qmp_register_command(&qmp_commands, "device_add", qmp_device_add,
1014 QCO_NO_OPTIONS);
1015 qmp_register_command(&qmp_commands, "netdev_add", qmp_netdev_add,
1016 QCO_NO_OPTIONS);
1018 qmp_unregister_commands_hack();
1020 QTAILQ_INIT(&qmp_cap_negotiation_commands);
1021 qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
1022 qmp_marshal_qmp_capabilities, QCO_NO_OPTIONS);
1025 void qmp_qmp_capabilities(Error **errp)
1027 if (cur_mon->qmp.commands == &qmp_commands) {
1028 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
1029 "Capabilities negotiation is already complete, command "
1030 "ignored");
1031 return;
1034 cur_mon->qmp.commands = &qmp_commands;
1037 /* set the current CPU defined by the user */
1038 int monitor_set_cpu(int cpu_index)
1040 CPUState *cpu;
1042 cpu = qemu_get_cpu(cpu_index);
1043 if (cpu == NULL) {
1044 return -1;
1046 cur_mon->mon_cpu = cpu;
1047 return 0;
1050 CPUState *mon_get_cpu(void)
1052 if (!cur_mon->mon_cpu) {
1053 if (!first_cpu) {
1054 return NULL;
1056 monitor_set_cpu(first_cpu->cpu_index);
1058 cpu_synchronize_state(cur_mon->mon_cpu);
1059 return cur_mon->mon_cpu;
1062 CPUArchState *mon_get_cpu_env(void)
1064 CPUState *cs = mon_get_cpu();
1066 return cs ? cs->env_ptr : NULL;
1069 int monitor_get_cpu_index(void)
1071 CPUState *cs = mon_get_cpu();
1073 return cs ? cs->cpu_index : UNASSIGNED_CPU_INDEX;
1076 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
1078 CPUState *cs = mon_get_cpu();
1080 if (!cs) {
1081 monitor_printf(mon, "No CPU available\n");
1082 return;
1084 cpu_dump_state(cs, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
1087 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
1089 dump_exec_info((FILE *)mon, monitor_fprintf);
1090 dump_drift_info((FILE *)mon, monitor_fprintf);
1093 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
1095 dump_opcount_info((FILE *)mon, monitor_fprintf);
1098 static void hmp_info_history(Monitor *mon, const QDict *qdict)
1100 int i;
1101 const char *str;
1103 if (!mon->rs)
1104 return;
1105 i = 0;
1106 for(;;) {
1107 str = readline_get_history(mon->rs, i);
1108 if (!str)
1109 break;
1110 monitor_printf(mon, "%d: '%s'\n", i, str);
1111 i++;
1115 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
1117 CPUState *cs = mon_get_cpu();
1119 if (!cs) {
1120 monitor_printf(mon, "No CPU available\n");
1121 return;
1123 cpu_dump_statistics(cs, (FILE *)mon, &monitor_fprintf, 0);
1126 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1128 const char *name = qdict_get_try_str(qdict, "name");
1129 bool has_vcpu = qdict_haskey(qdict, "vcpu");
1130 int vcpu = qdict_get_try_int(qdict, "vcpu", 0);
1131 TraceEventInfoList *events;
1132 TraceEventInfoList *elem;
1133 Error *local_err = NULL;
1135 if (name == NULL) {
1136 name = "*";
1138 if (vcpu < 0) {
1139 monitor_printf(mon, "argument vcpu must be positive");
1140 return;
1143 events = qmp_trace_event_get_state(name, has_vcpu, vcpu, &local_err);
1144 if (local_err) {
1145 error_report_err(local_err);
1146 return;
1149 for (elem = events; elem != NULL; elem = elem->next) {
1150 monitor_printf(mon, "%s : state %u\n",
1151 elem->value->name,
1152 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1154 qapi_free_TraceEventInfoList(events);
1157 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1158 bool has_port, int64_t port,
1159 bool has_tls_port, int64_t tls_port,
1160 bool has_cert_subject, const char *cert_subject,
1161 Error **errp)
1163 if (strcmp(protocol, "spice") == 0) {
1164 if (!qemu_using_spice(errp)) {
1165 return;
1168 if (!has_port && !has_tls_port) {
1169 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1170 return;
1173 if (qemu_spice_migrate_info(hostname,
1174 has_port ? port : -1,
1175 has_tls_port ? tls_port : -1,
1176 cert_subject)) {
1177 error_setg(errp, QERR_UNDEFINED_ERROR);
1178 return;
1180 return;
1183 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1186 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1188 Error *err = NULL;
1190 qemu_set_log_filename(qdict_get_str(qdict, "filename"), &err);
1191 if (err) {
1192 error_report_err(err);
1196 static void hmp_log(Monitor *mon, const QDict *qdict)
1198 int mask;
1199 const char *items = qdict_get_str(qdict, "items");
1201 if (!strcmp(items, "none")) {
1202 mask = 0;
1203 } else {
1204 mask = qemu_str_to_log_mask(items);
1205 if (!mask) {
1206 help_cmd(mon, "log");
1207 return;
1210 qemu_set_log(mask);
1213 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1215 const char *option = qdict_get_try_str(qdict, "option");
1216 if (!option || !strcmp(option, "on")) {
1217 singlestep = 1;
1218 } else if (!strcmp(option, "off")) {
1219 singlestep = 0;
1220 } else {
1221 monitor_printf(mon, "unexpected option %s\n", option);
1225 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1227 const char *device = qdict_get_try_str(qdict, "device");
1228 if (!device)
1229 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1230 if (gdbserver_start(device) < 0) {
1231 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1232 device);
1233 } else if (strcmp(device, "none") == 0) {
1234 monitor_printf(mon, "Disabled gdbserver\n");
1235 } else {
1236 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1237 device);
1241 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1243 const char *action = qdict_get_str(qdict, "action");
1244 if (select_watchdog_action(action) == -1) {
1245 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1249 static void monitor_printc(Monitor *mon, int c)
1251 monitor_printf(mon, "'");
1252 switch(c) {
1253 case '\'':
1254 monitor_printf(mon, "\\'");
1255 break;
1256 case '\\':
1257 monitor_printf(mon, "\\\\");
1258 break;
1259 case '\n':
1260 monitor_printf(mon, "\\n");
1261 break;
1262 case '\r':
1263 monitor_printf(mon, "\\r");
1264 break;
1265 default:
1266 if (c >= 32 && c <= 126) {
1267 monitor_printf(mon, "%c", c);
1268 } else {
1269 monitor_printf(mon, "\\x%02x", c);
1271 break;
1273 monitor_printf(mon, "'");
1276 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1277 hwaddr addr, int is_physical)
1279 int l, line_size, i, max_digits, len;
1280 uint8_t buf[16];
1281 uint64_t v;
1282 CPUState *cs = mon_get_cpu();
1284 if (!cs && (format == 'i' || !is_physical)) {
1285 monitor_printf(mon, "Can not dump without CPU\n");
1286 return;
1289 if (format == 'i') {
1290 int flags = 0;
1291 #ifdef TARGET_I386
1292 CPUArchState *env = mon_get_cpu_env();
1293 if (wsize == 2) {
1294 flags = 1;
1295 } else if (wsize == 4) {
1296 flags = 0;
1297 } else {
1298 /* as default we use the current CS size */
1299 flags = 0;
1300 if (env) {
1301 #ifdef TARGET_X86_64
1302 if ((env->efer & MSR_EFER_LMA) &&
1303 (env->segs[R_CS].flags & DESC_L_MASK))
1304 flags = 2;
1305 else
1306 #endif
1307 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1308 flags = 1;
1311 #endif
1312 #ifdef TARGET_PPC
1313 CPUArchState *env = mon_get_cpu_env();
1314 flags = msr_le << 16;
1315 flags |= env->bfd_mach;
1316 #endif
1317 monitor_disas(mon, cs, addr, count, is_physical, flags);
1318 return;
1321 len = wsize * count;
1322 if (wsize == 1)
1323 line_size = 8;
1324 else
1325 line_size = 16;
1326 max_digits = 0;
1328 switch(format) {
1329 case 'o':
1330 max_digits = (wsize * 8 + 2) / 3;
1331 break;
1332 default:
1333 case 'x':
1334 max_digits = (wsize * 8) / 4;
1335 break;
1336 case 'u':
1337 case 'd':
1338 max_digits = (wsize * 8 * 10 + 32) / 33;
1339 break;
1340 case 'c':
1341 wsize = 1;
1342 break;
1345 while (len > 0) {
1346 if (is_physical)
1347 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1348 else
1349 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1350 l = len;
1351 if (l > line_size)
1352 l = line_size;
1353 if (is_physical) {
1354 cpu_physical_memory_read(addr, buf, l);
1355 } else {
1356 if (cpu_memory_rw_debug(cs, addr, buf, l, 0) < 0) {
1357 monitor_printf(mon, " Cannot access memory\n");
1358 break;
1361 i = 0;
1362 while (i < l) {
1363 switch(wsize) {
1364 default:
1365 case 1:
1366 v = ldub_p(buf + i);
1367 break;
1368 case 2:
1369 v = lduw_p(buf + i);
1370 break;
1371 case 4:
1372 v = (uint32_t)ldl_p(buf + i);
1373 break;
1374 case 8:
1375 v = ldq_p(buf + i);
1376 break;
1378 monitor_printf(mon, " ");
1379 switch(format) {
1380 case 'o':
1381 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1382 break;
1383 case 'x':
1384 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1385 break;
1386 case 'u':
1387 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1388 break;
1389 case 'd':
1390 monitor_printf(mon, "%*" PRId64, max_digits, v);
1391 break;
1392 case 'c':
1393 monitor_printc(mon, v);
1394 break;
1396 i += wsize;
1398 monitor_printf(mon, "\n");
1399 addr += l;
1400 len -= l;
1404 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1406 int count = qdict_get_int(qdict, "count");
1407 int format = qdict_get_int(qdict, "format");
1408 int size = qdict_get_int(qdict, "size");
1409 target_long addr = qdict_get_int(qdict, "addr");
1411 memory_dump(mon, count, format, size, addr, 0);
1414 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1416 int count = qdict_get_int(qdict, "count");
1417 int format = qdict_get_int(qdict, "format");
1418 int size = qdict_get_int(qdict, "size");
1419 hwaddr addr = qdict_get_int(qdict, "addr");
1421 memory_dump(mon, count, format, size, addr, 1);
1424 static void do_print(Monitor *mon, const QDict *qdict)
1426 int format = qdict_get_int(qdict, "format");
1427 hwaddr val = qdict_get_int(qdict, "val");
1429 switch(format) {
1430 case 'o':
1431 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1432 break;
1433 case 'x':
1434 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1435 break;
1436 case 'u':
1437 monitor_printf(mon, "%" HWADDR_PRIu, val);
1438 break;
1439 default:
1440 case 'd':
1441 monitor_printf(mon, "%" HWADDR_PRId, val);
1442 break;
1443 case 'c':
1444 monitor_printc(mon, val);
1445 break;
1447 monitor_printf(mon, "\n");
1450 static void hmp_sum(Monitor *mon, const QDict *qdict)
1452 uint32_t addr;
1453 uint16_t sum;
1454 uint32_t start = qdict_get_int(qdict, "start");
1455 uint32_t size = qdict_get_int(qdict, "size");
1457 sum = 0;
1458 for(addr = start; addr < (start + size); addr++) {
1459 uint8_t val = address_space_ldub(&address_space_memory, addr,
1460 MEMTXATTRS_UNSPECIFIED, NULL);
1461 /* BSD sum algorithm ('sum' Unix command) */
1462 sum = (sum >> 1) | (sum << 15);
1463 sum += val;
1465 monitor_printf(mon, "%05d\n", sum);
1468 static int mouse_button_state;
1470 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1472 int dx, dy, dz, button;
1473 const char *dx_str = qdict_get_str(qdict, "dx_str");
1474 const char *dy_str = qdict_get_str(qdict, "dy_str");
1475 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1477 dx = strtol(dx_str, NULL, 0);
1478 dy = strtol(dy_str, NULL, 0);
1479 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1480 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1482 if (dz_str) {
1483 dz = strtol(dz_str, NULL, 0);
1484 if (dz != 0) {
1485 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1486 qemu_input_queue_btn(NULL, button, true);
1487 qemu_input_event_sync();
1488 qemu_input_queue_btn(NULL, button, false);
1491 qemu_input_event_sync();
1494 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1496 static uint32_t bmap[INPUT_BUTTON__MAX] = {
1497 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1498 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1499 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1501 int button_state = qdict_get_int(qdict, "button_state");
1503 if (mouse_button_state == button_state) {
1504 return;
1506 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1507 qemu_input_event_sync();
1508 mouse_button_state = button_state;
1511 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1513 int size = qdict_get_int(qdict, "size");
1514 int addr = qdict_get_int(qdict, "addr");
1515 int has_index = qdict_haskey(qdict, "index");
1516 uint32_t val;
1517 int suffix;
1519 if (has_index) {
1520 int index = qdict_get_int(qdict, "index");
1521 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1522 addr++;
1524 addr &= 0xffff;
1526 switch(size) {
1527 default:
1528 case 1:
1529 val = cpu_inb(addr);
1530 suffix = 'b';
1531 break;
1532 case 2:
1533 val = cpu_inw(addr);
1534 suffix = 'w';
1535 break;
1536 case 4:
1537 val = cpu_inl(addr);
1538 suffix = 'l';
1539 break;
1541 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1542 suffix, addr, size * 2, val);
1545 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1547 int size = qdict_get_int(qdict, "size");
1548 int addr = qdict_get_int(qdict, "addr");
1549 int val = qdict_get_int(qdict, "val");
1551 addr &= IOPORTS_MASK;
1553 switch (size) {
1554 default:
1555 case 1:
1556 cpu_outb(addr, val);
1557 break;
1558 case 2:
1559 cpu_outw(addr, val);
1560 break;
1561 case 4:
1562 cpu_outl(addr, val);
1563 break;
1567 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1569 Error *local_err = NULL;
1570 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1572 qemu_boot_set(bootdevice, &local_err);
1573 if (local_err) {
1574 error_report_err(local_err);
1575 } else {
1576 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1580 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1582 bool flatview = qdict_get_try_bool(qdict, "flatview", false);
1584 mtree_info((fprintf_function)monitor_printf, mon, flatview);
1587 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1589 int i;
1590 CPUState *cpu;
1591 uint64_t *node_mem;
1593 node_mem = g_new0(uint64_t, nb_numa_nodes);
1594 query_numa_node_mem(node_mem);
1595 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1596 for (i = 0; i < nb_numa_nodes; i++) {
1597 monitor_printf(mon, "node %d cpus:", i);
1598 CPU_FOREACH(cpu) {
1599 if (cpu->numa_node == i) {
1600 monitor_printf(mon, " %d", cpu->cpu_index);
1603 monitor_printf(mon, "\n");
1604 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1605 node_mem[i] >> 20);
1607 g_free(node_mem);
1610 #ifdef CONFIG_PROFILER
1612 int64_t tcg_time;
1613 int64_t dev_time;
1615 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1617 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1618 dev_time, dev_time / (double)NANOSECONDS_PER_SECOND);
1619 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1620 tcg_time, tcg_time / (double)NANOSECONDS_PER_SECOND);
1621 tcg_time = 0;
1622 dev_time = 0;
1624 #else
1625 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1627 monitor_printf(mon, "Internal profiler not compiled\n");
1629 #endif
1631 /* Capture support */
1632 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1634 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1636 int i;
1637 CaptureState *s;
1639 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1640 monitor_printf(mon, "[%d]: ", i);
1641 s->ops.info (s->opaque);
1645 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1647 int i;
1648 int n = qdict_get_int(qdict, "n");
1649 CaptureState *s;
1651 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1652 if (i == n) {
1653 s->ops.destroy (s->opaque);
1654 QLIST_REMOVE (s, entries);
1655 g_free (s);
1656 return;
1661 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1663 const char *path = qdict_get_str(qdict, "path");
1664 int has_freq = qdict_haskey(qdict, "freq");
1665 int freq = qdict_get_try_int(qdict, "freq", -1);
1666 int has_bits = qdict_haskey(qdict, "bits");
1667 int bits = qdict_get_try_int(qdict, "bits", -1);
1668 int has_channels = qdict_haskey(qdict, "nchannels");
1669 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1670 CaptureState *s;
1672 s = g_malloc0 (sizeof (*s));
1674 freq = has_freq ? freq : 44100;
1675 bits = has_bits ? bits : 16;
1676 nchannels = has_channels ? nchannels : 2;
1678 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1679 monitor_printf(mon, "Failed to add wave capture\n");
1680 g_free (s);
1681 return;
1683 QLIST_INSERT_HEAD (&capture_head, s, entries);
1686 static qemu_acl *find_acl(Monitor *mon, const char *name)
1688 qemu_acl *acl = qemu_acl_find(name);
1690 if (!acl) {
1691 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1693 return acl;
1696 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1698 const char *aclname = qdict_get_str(qdict, "aclname");
1699 qemu_acl *acl = find_acl(mon, aclname);
1700 qemu_acl_entry *entry;
1701 int i = 0;
1703 if (acl) {
1704 monitor_printf(mon, "policy: %s\n",
1705 acl->defaultDeny ? "deny" : "allow");
1706 QTAILQ_FOREACH(entry, &acl->entries, next) {
1707 i++;
1708 monitor_printf(mon, "%d: %s %s\n", i,
1709 entry->deny ? "deny" : "allow", entry->match);
1714 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1716 const char *aclname = qdict_get_str(qdict, "aclname");
1717 qemu_acl *acl = find_acl(mon, aclname);
1719 if (acl) {
1720 qemu_acl_reset(acl);
1721 monitor_printf(mon, "acl: removed all rules\n");
1725 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
1727 const char *aclname = qdict_get_str(qdict, "aclname");
1728 const char *policy = qdict_get_str(qdict, "policy");
1729 qemu_acl *acl = find_acl(mon, aclname);
1731 if (acl) {
1732 if (strcmp(policy, "allow") == 0) {
1733 acl->defaultDeny = 0;
1734 monitor_printf(mon, "acl: policy set to 'allow'\n");
1735 } else if (strcmp(policy, "deny") == 0) {
1736 acl->defaultDeny = 1;
1737 monitor_printf(mon, "acl: policy set to 'deny'\n");
1738 } else {
1739 monitor_printf(mon, "acl: unknown policy '%s', "
1740 "expected 'deny' or 'allow'\n", policy);
1745 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
1747 const char *aclname = qdict_get_str(qdict, "aclname");
1748 const char *match = qdict_get_str(qdict, "match");
1749 const char *policy = qdict_get_str(qdict, "policy");
1750 int has_index = qdict_haskey(qdict, "index");
1751 int index = qdict_get_try_int(qdict, "index", -1);
1752 qemu_acl *acl = find_acl(mon, aclname);
1753 int deny, ret;
1755 if (acl) {
1756 if (strcmp(policy, "allow") == 0) {
1757 deny = 0;
1758 } else if (strcmp(policy, "deny") == 0) {
1759 deny = 1;
1760 } else {
1761 monitor_printf(mon, "acl: unknown policy '%s', "
1762 "expected 'deny' or 'allow'\n", policy);
1763 return;
1765 if (has_index)
1766 ret = qemu_acl_insert(acl, deny, match, index);
1767 else
1768 ret = qemu_acl_append(acl, deny, match);
1769 if (ret < 0)
1770 monitor_printf(mon, "acl: unable to add acl entry\n");
1771 else
1772 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1776 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
1778 const char *aclname = qdict_get_str(qdict, "aclname");
1779 const char *match = qdict_get_str(qdict, "match");
1780 qemu_acl *acl = find_acl(mon, aclname);
1781 int ret;
1783 if (acl) {
1784 ret = qemu_acl_remove(acl, match);
1785 if (ret < 0)
1786 monitor_printf(mon, "acl: no matching acl entry\n");
1787 else
1788 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1792 void qmp_getfd(const char *fdname, Error **errp)
1794 mon_fd_t *monfd;
1795 int fd;
1797 fd = qemu_chr_fe_get_msgfd(&cur_mon->chr);
1798 if (fd == -1) {
1799 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1800 return;
1803 if (qemu_isdigit(fdname[0])) {
1804 close(fd);
1805 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
1806 "a name not starting with a digit");
1807 return;
1810 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1811 if (strcmp(monfd->name, fdname) != 0) {
1812 continue;
1815 close(monfd->fd);
1816 monfd->fd = fd;
1817 return;
1820 monfd = g_malloc0(sizeof(mon_fd_t));
1821 monfd->name = g_strdup(fdname);
1822 monfd->fd = fd;
1824 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
1827 void qmp_closefd(const char *fdname, Error **errp)
1829 mon_fd_t *monfd;
1831 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
1832 if (strcmp(monfd->name, fdname) != 0) {
1833 continue;
1836 QLIST_REMOVE(monfd, next);
1837 close(monfd->fd);
1838 g_free(monfd->name);
1839 g_free(monfd);
1840 return;
1843 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
1846 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
1848 int saved_vm_running = runstate_is_running();
1849 const char *name = qdict_get_str(qdict, "name");
1851 vm_stop(RUN_STATE_RESTORE_VM);
1853 if (load_vmstate(name) == 0 && saved_vm_running) {
1854 vm_start();
1858 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
1860 mon_fd_t *monfd;
1862 QLIST_FOREACH(monfd, &mon->fds, next) {
1863 int fd;
1865 if (strcmp(monfd->name, fdname) != 0) {
1866 continue;
1869 fd = monfd->fd;
1871 /* caller takes ownership of fd */
1872 QLIST_REMOVE(monfd, next);
1873 g_free(monfd->name);
1874 g_free(monfd);
1876 return fd;
1879 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
1880 return -1;
1883 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
1885 MonFdsetFd *mon_fdset_fd;
1886 MonFdsetFd *mon_fdset_fd_next;
1888 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
1889 if ((mon_fdset_fd->removed ||
1890 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
1891 runstate_is_running()) {
1892 close(mon_fdset_fd->fd);
1893 g_free(mon_fdset_fd->opaque);
1894 QLIST_REMOVE(mon_fdset_fd, next);
1895 g_free(mon_fdset_fd);
1899 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
1900 QLIST_REMOVE(mon_fdset, next);
1901 g_free(mon_fdset);
1905 static void monitor_fdsets_cleanup(void)
1907 MonFdset *mon_fdset;
1908 MonFdset *mon_fdset_next;
1910 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
1911 monitor_fdset_cleanup(mon_fdset);
1915 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
1916 const char *opaque, Error **errp)
1918 int fd;
1919 Monitor *mon = cur_mon;
1920 AddfdInfo *fdinfo;
1922 fd = qemu_chr_fe_get_msgfd(&mon->chr);
1923 if (fd == -1) {
1924 error_setg(errp, QERR_FD_NOT_SUPPLIED);
1925 goto error;
1928 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
1929 has_opaque, opaque, errp);
1930 if (fdinfo) {
1931 return fdinfo;
1934 error:
1935 if (fd != -1) {
1936 close(fd);
1938 return NULL;
1941 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
1943 MonFdset *mon_fdset;
1944 MonFdsetFd *mon_fdset_fd;
1945 char fd_str[60];
1947 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1948 if (mon_fdset->id != fdset_id) {
1949 continue;
1951 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1952 if (has_fd) {
1953 if (mon_fdset_fd->fd != fd) {
1954 continue;
1956 mon_fdset_fd->removed = true;
1957 break;
1958 } else {
1959 mon_fdset_fd->removed = true;
1962 if (has_fd && !mon_fdset_fd) {
1963 goto error;
1965 monitor_fdset_cleanup(mon_fdset);
1966 return;
1969 error:
1970 if (has_fd) {
1971 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
1972 fdset_id, fd);
1973 } else {
1974 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
1976 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
1979 FdsetInfoList *qmp_query_fdsets(Error **errp)
1981 MonFdset *mon_fdset;
1982 MonFdsetFd *mon_fdset_fd;
1983 FdsetInfoList *fdset_list = NULL;
1985 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
1986 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
1987 FdsetFdInfoList *fdsetfd_list = NULL;
1989 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
1990 fdset_info->value->fdset_id = mon_fdset->id;
1992 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
1993 FdsetFdInfoList *fdsetfd_info;
1995 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
1996 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
1997 fdsetfd_info->value->fd = mon_fdset_fd->fd;
1998 if (mon_fdset_fd->opaque) {
1999 fdsetfd_info->value->has_opaque = true;
2000 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2001 } else {
2002 fdsetfd_info->value->has_opaque = false;
2005 fdsetfd_info->next = fdsetfd_list;
2006 fdsetfd_list = fdsetfd_info;
2009 fdset_info->value->fds = fdsetfd_list;
2011 fdset_info->next = fdset_list;
2012 fdset_list = fdset_info;
2015 return fdset_list;
2018 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2019 bool has_opaque, const char *opaque,
2020 Error **errp)
2022 MonFdset *mon_fdset = NULL;
2023 MonFdsetFd *mon_fdset_fd;
2024 AddfdInfo *fdinfo;
2026 if (has_fdset_id) {
2027 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2028 /* Break if match found or match impossible due to ordering by ID */
2029 if (fdset_id <= mon_fdset->id) {
2030 if (fdset_id < mon_fdset->id) {
2031 mon_fdset = NULL;
2033 break;
2038 if (mon_fdset == NULL) {
2039 int64_t fdset_id_prev = -1;
2040 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2042 if (has_fdset_id) {
2043 if (fdset_id < 0) {
2044 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2045 "a non-negative value");
2046 return NULL;
2048 /* Use specified fdset ID */
2049 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2050 mon_fdset_cur = mon_fdset;
2051 if (fdset_id < mon_fdset_cur->id) {
2052 break;
2055 } else {
2056 /* Use first available fdset ID */
2057 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2058 mon_fdset_cur = mon_fdset;
2059 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2060 fdset_id_prev = mon_fdset_cur->id;
2061 continue;
2063 break;
2067 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2068 if (has_fdset_id) {
2069 mon_fdset->id = fdset_id;
2070 } else {
2071 mon_fdset->id = fdset_id_prev + 1;
2074 /* The fdset list is ordered by fdset ID */
2075 if (!mon_fdset_cur) {
2076 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2077 } else if (mon_fdset->id < mon_fdset_cur->id) {
2078 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2079 } else {
2080 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2084 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2085 mon_fdset_fd->fd = fd;
2086 mon_fdset_fd->removed = false;
2087 if (has_opaque) {
2088 mon_fdset_fd->opaque = g_strdup(opaque);
2090 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2092 fdinfo = g_malloc0(sizeof(*fdinfo));
2093 fdinfo->fdset_id = mon_fdset->id;
2094 fdinfo->fd = mon_fdset_fd->fd;
2096 return fdinfo;
2099 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2101 #ifndef _WIN32
2102 MonFdset *mon_fdset;
2103 MonFdsetFd *mon_fdset_fd;
2104 int mon_fd_flags;
2106 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2107 if (mon_fdset->id != fdset_id) {
2108 continue;
2110 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2111 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2112 if (mon_fd_flags == -1) {
2113 return -1;
2116 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2117 return mon_fdset_fd->fd;
2120 errno = EACCES;
2121 return -1;
2123 #endif
2125 errno = ENOENT;
2126 return -1;
2129 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2131 MonFdset *mon_fdset;
2132 MonFdsetFd *mon_fdset_fd_dup;
2134 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2135 if (mon_fdset->id != fdset_id) {
2136 continue;
2138 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2139 if (mon_fdset_fd_dup->fd == dup_fd) {
2140 return -1;
2143 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2144 mon_fdset_fd_dup->fd = dup_fd;
2145 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2146 return 0;
2148 return -1;
2151 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2153 MonFdset *mon_fdset;
2154 MonFdsetFd *mon_fdset_fd_dup;
2156 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2157 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2158 if (mon_fdset_fd_dup->fd == dup_fd) {
2159 if (remove) {
2160 QLIST_REMOVE(mon_fdset_fd_dup, next);
2161 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2162 monitor_fdset_cleanup(mon_fdset);
2164 return -1;
2165 } else {
2166 return mon_fdset->id;
2171 return -1;
2174 int monitor_fdset_dup_fd_find(int dup_fd)
2176 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2179 void monitor_fdset_dup_fd_remove(int dup_fd)
2181 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2184 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2186 int fd;
2187 Error *local_err = NULL;
2189 if (!qemu_isdigit(fdname[0]) && mon) {
2190 fd = monitor_get_fd(mon, fdname, &local_err);
2191 } else {
2192 fd = qemu_parse_fd(fdname);
2193 if (fd == -1) {
2194 error_setg(&local_err, "Invalid file descriptor number '%s'",
2195 fdname);
2198 if (local_err) {
2199 error_propagate(errp, local_err);
2200 assert(fd == -1);
2201 } else {
2202 assert(fd != -1);
2205 return fd;
2208 /* Please update hmp-commands.hx when adding or changing commands */
2209 static mon_cmd_t info_cmds[] = {
2210 #include "hmp-commands-info.h"
2211 { NULL, NULL, },
2214 /* mon_cmds and info_cmds would be sorted at runtime */
2215 static mon_cmd_t mon_cmds[] = {
2216 #include "hmp-commands.h"
2217 { NULL, NULL, },
2220 /*******************************************************************/
2222 static const char *pch;
2223 static sigjmp_buf expr_env;
2226 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2227 expr_error(Monitor *mon, const char *fmt, ...)
2229 va_list ap;
2230 va_start(ap, fmt);
2231 monitor_vprintf(mon, fmt, ap);
2232 monitor_printf(mon, "\n");
2233 va_end(ap);
2234 siglongjmp(expr_env, 1);
2237 /* return 0 if OK, -1 if not found */
2238 static int get_monitor_def(target_long *pval, const char *name)
2240 const MonitorDef *md = target_monitor_defs();
2241 CPUState *cs = mon_get_cpu();
2242 void *ptr;
2243 uint64_t tmp = 0;
2244 int ret;
2246 if (cs == NULL || md == NULL) {
2247 return -1;
2250 for(; md->name != NULL; md++) {
2251 if (compare_cmd(name, md->name)) {
2252 if (md->get_value) {
2253 *pval = md->get_value(md, md->offset);
2254 } else {
2255 CPUArchState *env = mon_get_cpu_env();
2256 ptr = (uint8_t *)env + md->offset;
2257 switch(md->type) {
2258 case MD_I32:
2259 *pval = *(int32_t *)ptr;
2260 break;
2261 case MD_TLONG:
2262 *pval = *(target_long *)ptr;
2263 break;
2264 default:
2265 *pval = 0;
2266 break;
2269 return 0;
2273 ret = target_get_monitor_def(cs, name, &tmp);
2274 if (!ret) {
2275 *pval = (target_long) tmp;
2278 return ret;
2281 static void next(void)
2283 if (*pch != '\0') {
2284 pch++;
2285 while (qemu_isspace(*pch))
2286 pch++;
2290 static int64_t expr_sum(Monitor *mon);
2292 static int64_t expr_unary(Monitor *mon)
2294 int64_t n;
2295 char *p;
2296 int ret;
2298 switch(*pch) {
2299 case '+':
2300 next();
2301 n = expr_unary(mon);
2302 break;
2303 case '-':
2304 next();
2305 n = -expr_unary(mon);
2306 break;
2307 case '~':
2308 next();
2309 n = ~expr_unary(mon);
2310 break;
2311 case '(':
2312 next();
2313 n = expr_sum(mon);
2314 if (*pch != ')') {
2315 expr_error(mon, "')' expected");
2317 next();
2318 break;
2319 case '\'':
2320 pch++;
2321 if (*pch == '\0')
2322 expr_error(mon, "character constant expected");
2323 n = *pch;
2324 pch++;
2325 if (*pch != '\'')
2326 expr_error(mon, "missing terminating \' character");
2327 next();
2328 break;
2329 case '$':
2331 char buf[128], *q;
2332 target_long reg=0;
2334 pch++;
2335 q = buf;
2336 while ((*pch >= 'a' && *pch <= 'z') ||
2337 (*pch >= 'A' && *pch <= 'Z') ||
2338 (*pch >= '0' && *pch <= '9') ||
2339 *pch == '_' || *pch == '.') {
2340 if ((q - buf) < sizeof(buf) - 1)
2341 *q++ = *pch;
2342 pch++;
2344 while (qemu_isspace(*pch))
2345 pch++;
2346 *q = 0;
2347 ret = get_monitor_def(&reg, buf);
2348 if (ret < 0)
2349 expr_error(mon, "unknown register");
2350 n = reg;
2352 break;
2353 case '\0':
2354 expr_error(mon, "unexpected end of expression");
2355 n = 0;
2356 break;
2357 default:
2358 errno = 0;
2359 n = strtoull(pch, &p, 0);
2360 if (errno == ERANGE) {
2361 expr_error(mon, "number too large");
2363 if (pch == p) {
2364 expr_error(mon, "invalid char '%c' in expression", *p);
2366 pch = p;
2367 while (qemu_isspace(*pch))
2368 pch++;
2369 break;
2371 return n;
2375 static int64_t expr_prod(Monitor *mon)
2377 int64_t val, val2;
2378 int op;
2380 val = expr_unary(mon);
2381 for(;;) {
2382 op = *pch;
2383 if (op != '*' && op != '/' && op != '%')
2384 break;
2385 next();
2386 val2 = expr_unary(mon);
2387 switch(op) {
2388 default:
2389 case '*':
2390 val *= val2;
2391 break;
2392 case '/':
2393 case '%':
2394 if (val2 == 0)
2395 expr_error(mon, "division by zero");
2396 if (op == '/')
2397 val /= val2;
2398 else
2399 val %= val2;
2400 break;
2403 return val;
2406 static int64_t expr_logic(Monitor *mon)
2408 int64_t val, val2;
2409 int op;
2411 val = expr_prod(mon);
2412 for(;;) {
2413 op = *pch;
2414 if (op != '&' && op != '|' && op != '^')
2415 break;
2416 next();
2417 val2 = expr_prod(mon);
2418 switch(op) {
2419 default:
2420 case '&':
2421 val &= val2;
2422 break;
2423 case '|':
2424 val |= val2;
2425 break;
2426 case '^':
2427 val ^= val2;
2428 break;
2431 return val;
2434 static int64_t expr_sum(Monitor *mon)
2436 int64_t val, val2;
2437 int op;
2439 val = expr_logic(mon);
2440 for(;;) {
2441 op = *pch;
2442 if (op != '+' && op != '-')
2443 break;
2444 next();
2445 val2 = expr_logic(mon);
2446 if (op == '+')
2447 val += val2;
2448 else
2449 val -= val2;
2451 return val;
2454 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2456 pch = *pp;
2457 if (sigsetjmp(expr_env, 0)) {
2458 *pp = pch;
2459 return -1;
2461 while (qemu_isspace(*pch))
2462 pch++;
2463 *pval = expr_sum(mon);
2464 *pp = pch;
2465 return 0;
2468 static int get_double(Monitor *mon, double *pval, const char **pp)
2470 const char *p = *pp;
2471 char *tailp;
2472 double d;
2474 d = strtod(p, &tailp);
2475 if (tailp == p) {
2476 monitor_printf(mon, "Number expected\n");
2477 return -1;
2479 if (d != d || d - d != 0) {
2480 /* NaN or infinity */
2481 monitor_printf(mon, "Bad number\n");
2482 return -1;
2484 *pval = d;
2485 *pp = tailp;
2486 return 0;
2490 * Store the command-name in cmdname, and return a pointer to
2491 * the remaining of the command string.
2493 static const char *get_command_name(const char *cmdline,
2494 char *cmdname, size_t nlen)
2496 size_t len;
2497 const char *p, *pstart;
2499 p = cmdline;
2500 while (qemu_isspace(*p))
2501 p++;
2502 if (*p == '\0')
2503 return NULL;
2504 pstart = p;
2505 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2506 p++;
2507 len = p - pstart;
2508 if (len > nlen - 1)
2509 len = nlen - 1;
2510 memcpy(cmdname, pstart, len);
2511 cmdname[len] = '\0';
2512 return p;
2516 * Read key of 'type' into 'key' and return the current
2517 * 'type' pointer.
2519 static char *key_get_info(const char *type, char **key)
2521 size_t len;
2522 char *p, *str;
2524 if (*type == ',')
2525 type++;
2527 p = strchr(type, ':');
2528 if (!p) {
2529 *key = NULL;
2530 return NULL;
2532 len = p - type;
2534 str = g_malloc(len + 1);
2535 memcpy(str, type, len);
2536 str[len] = '\0';
2538 *key = str;
2539 return ++p;
2542 static int default_fmt_format = 'x';
2543 static int default_fmt_size = 4;
2545 static int is_valid_option(const char *c, const char *typestr)
2547 char option[3];
2549 option[0] = '-';
2550 option[1] = *c;
2551 option[2] = '\0';
2553 typestr = strstr(typestr, option);
2554 return (typestr != NULL);
2557 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
2558 const char *cmdname)
2560 const mon_cmd_t *cmd;
2562 for (cmd = disp_table; cmd->name != NULL; cmd++) {
2563 if (compare_cmd(cmdname, cmd->name)) {
2564 return cmd;
2568 return NULL;
2572 * Parse command name from @cmdp according to command table @table.
2573 * If blank, return NULL.
2574 * Else, if no valid command can be found, report to @mon, and return
2575 * NULL.
2576 * Else, change @cmdp to point right behind the name, and return its
2577 * command table entry.
2578 * Do not assume the return value points into @table! It doesn't when
2579 * the command is found in a sub-command table.
2581 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2582 const char **cmdp,
2583 mon_cmd_t *table)
2585 const char *p;
2586 const mon_cmd_t *cmd;
2587 char cmdname[256];
2589 /* extract the command name */
2590 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
2591 if (!p)
2592 return NULL;
2594 cmd = search_dispatch_table(table, cmdname);
2595 if (!cmd) {
2596 monitor_printf(mon, "unknown command: '%.*s'\n",
2597 (int)(p - *cmdp), *cmdp);
2598 return NULL;
2601 /* filter out following useless space */
2602 while (qemu_isspace(*p)) {
2603 p++;
2606 *cmdp = p;
2607 /* search sub command */
2608 if (cmd->sub_table != NULL && *p != '\0') {
2609 return monitor_parse_command(mon, cmdp, cmd->sub_table);
2612 return cmd;
2616 * Parse arguments for @cmd.
2617 * If it can't be parsed, report to @mon, and return NULL.
2618 * Else, insert command arguments into a QDict, and return it.
2619 * Note: On success, caller has to free the QDict structure.
2622 static QDict *monitor_parse_arguments(Monitor *mon,
2623 const char **endp,
2624 const mon_cmd_t *cmd)
2626 const char *typestr;
2627 char *key;
2628 int c;
2629 const char *p = *endp;
2630 char buf[1024];
2631 QDict *qdict = qdict_new();
2633 /* parse the parameters */
2634 typestr = cmd->args_type;
2635 for(;;) {
2636 typestr = key_get_info(typestr, &key);
2637 if (!typestr)
2638 break;
2639 c = *typestr;
2640 typestr++;
2641 switch(c) {
2642 case 'F':
2643 case 'B':
2644 case 's':
2646 int ret;
2648 while (qemu_isspace(*p))
2649 p++;
2650 if (*typestr == '?') {
2651 typestr++;
2652 if (*p == '\0') {
2653 /* no optional string: NULL argument */
2654 break;
2657 ret = get_str(buf, sizeof(buf), &p);
2658 if (ret < 0) {
2659 switch(c) {
2660 case 'F':
2661 monitor_printf(mon, "%s: filename expected\n",
2662 cmd->name);
2663 break;
2664 case 'B':
2665 monitor_printf(mon, "%s: block device name expected\n",
2666 cmd->name);
2667 break;
2668 default:
2669 monitor_printf(mon, "%s: string expected\n", cmd->name);
2670 break;
2672 goto fail;
2674 qdict_put(qdict, key, qstring_from_str(buf));
2676 break;
2677 case 'O':
2679 QemuOptsList *opts_list;
2680 QemuOpts *opts;
2682 opts_list = qemu_find_opts(key);
2683 if (!opts_list || opts_list->desc->name) {
2684 goto bad_type;
2686 while (qemu_isspace(*p)) {
2687 p++;
2689 if (!*p)
2690 break;
2691 if (get_str(buf, sizeof(buf), &p) < 0) {
2692 goto fail;
2694 opts = qemu_opts_parse_noisily(opts_list, buf, true);
2695 if (!opts) {
2696 goto fail;
2698 qemu_opts_to_qdict(opts, qdict);
2699 qemu_opts_del(opts);
2701 break;
2702 case '/':
2704 int count, format, size;
2706 while (qemu_isspace(*p))
2707 p++;
2708 if (*p == '/') {
2709 /* format found */
2710 p++;
2711 count = 1;
2712 if (qemu_isdigit(*p)) {
2713 count = 0;
2714 while (qemu_isdigit(*p)) {
2715 count = count * 10 + (*p - '0');
2716 p++;
2719 size = -1;
2720 format = -1;
2721 for(;;) {
2722 switch(*p) {
2723 case 'o':
2724 case 'd':
2725 case 'u':
2726 case 'x':
2727 case 'i':
2728 case 'c':
2729 format = *p++;
2730 break;
2731 case 'b':
2732 size = 1;
2733 p++;
2734 break;
2735 case 'h':
2736 size = 2;
2737 p++;
2738 break;
2739 case 'w':
2740 size = 4;
2741 p++;
2742 break;
2743 case 'g':
2744 case 'L':
2745 size = 8;
2746 p++;
2747 break;
2748 default:
2749 goto next;
2752 next:
2753 if (*p != '\0' && !qemu_isspace(*p)) {
2754 monitor_printf(mon, "invalid char in format: '%c'\n",
2755 *p);
2756 goto fail;
2758 if (format < 0)
2759 format = default_fmt_format;
2760 if (format != 'i') {
2761 /* for 'i', not specifying a size gives -1 as size */
2762 if (size < 0)
2763 size = default_fmt_size;
2764 default_fmt_size = size;
2766 default_fmt_format = format;
2767 } else {
2768 count = 1;
2769 format = default_fmt_format;
2770 if (format != 'i') {
2771 size = default_fmt_size;
2772 } else {
2773 size = -1;
2776 qdict_put(qdict, "count", qint_from_int(count));
2777 qdict_put(qdict, "format", qint_from_int(format));
2778 qdict_put(qdict, "size", qint_from_int(size));
2780 break;
2781 case 'i':
2782 case 'l':
2783 case 'M':
2785 int64_t val;
2787 while (qemu_isspace(*p))
2788 p++;
2789 if (*typestr == '?' || *typestr == '.') {
2790 if (*typestr == '?') {
2791 if (*p == '\0') {
2792 typestr++;
2793 break;
2795 } else {
2796 if (*p == '.') {
2797 p++;
2798 while (qemu_isspace(*p))
2799 p++;
2800 } else {
2801 typestr++;
2802 break;
2805 typestr++;
2807 if (get_expr(mon, &val, &p))
2808 goto fail;
2809 /* Check if 'i' is greater than 32-bit */
2810 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2811 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
2812 monitor_printf(mon, "integer is for 32-bit values\n");
2813 goto fail;
2814 } else if (c == 'M') {
2815 if (val < 0) {
2816 monitor_printf(mon, "enter a positive value\n");
2817 goto fail;
2819 val <<= 20;
2821 qdict_put(qdict, key, qint_from_int(val));
2823 break;
2824 case 'o':
2826 int ret;
2827 uint64_t val;
2828 char *end;
2830 while (qemu_isspace(*p)) {
2831 p++;
2833 if (*typestr == '?') {
2834 typestr++;
2835 if (*p == '\0') {
2836 break;
2839 ret = qemu_strtosz_MiB(p, &end, &val);
2840 if (ret < 0 || val > INT64_MAX) {
2841 monitor_printf(mon, "invalid size\n");
2842 goto fail;
2844 qdict_put(qdict, key, qint_from_int(val));
2845 p = end;
2847 break;
2848 case 'T':
2850 double val;
2852 while (qemu_isspace(*p))
2853 p++;
2854 if (*typestr == '?') {
2855 typestr++;
2856 if (*p == '\0') {
2857 break;
2860 if (get_double(mon, &val, &p) < 0) {
2861 goto fail;
2863 if (p[0] && p[1] == 's') {
2864 switch (*p) {
2865 case 'm':
2866 val /= 1e3; p += 2; break;
2867 case 'u':
2868 val /= 1e6; p += 2; break;
2869 case 'n':
2870 val /= 1e9; p += 2; break;
2873 if (*p && !qemu_isspace(*p)) {
2874 monitor_printf(mon, "Unknown unit suffix\n");
2875 goto fail;
2877 qdict_put(qdict, key, qfloat_from_double(val));
2879 break;
2880 case 'b':
2882 const char *beg;
2883 bool val;
2885 while (qemu_isspace(*p)) {
2886 p++;
2888 beg = p;
2889 while (qemu_isgraph(*p)) {
2890 p++;
2892 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
2893 val = true;
2894 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
2895 val = false;
2896 } else {
2897 monitor_printf(mon, "Expected 'on' or 'off'\n");
2898 goto fail;
2900 qdict_put(qdict, key, qbool_from_bool(val));
2902 break;
2903 case '-':
2905 const char *tmp = p;
2906 int skip_key = 0;
2907 /* option */
2909 c = *typestr++;
2910 if (c == '\0')
2911 goto bad_type;
2912 while (qemu_isspace(*p))
2913 p++;
2914 if (*p == '-') {
2915 p++;
2916 if(c != *p) {
2917 if(!is_valid_option(p, typestr)) {
2919 monitor_printf(mon, "%s: unsupported option -%c\n",
2920 cmd->name, *p);
2921 goto fail;
2922 } else {
2923 skip_key = 1;
2926 if(skip_key) {
2927 p = tmp;
2928 } else {
2929 /* has option */
2930 p++;
2931 qdict_put(qdict, key, qbool_from_bool(true));
2935 break;
2936 case 'S':
2938 /* package all remaining string */
2939 int len;
2941 while (qemu_isspace(*p)) {
2942 p++;
2944 if (*typestr == '?') {
2945 typestr++;
2946 if (*p == '\0') {
2947 /* no remaining string: NULL argument */
2948 break;
2951 len = strlen(p);
2952 if (len <= 0) {
2953 monitor_printf(mon, "%s: string expected\n",
2954 cmd->name);
2955 goto fail;
2957 qdict_put(qdict, key, qstring_from_str(p));
2958 p += len;
2960 break;
2961 default:
2962 bad_type:
2963 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
2964 goto fail;
2966 g_free(key);
2967 key = NULL;
2969 /* check that all arguments were parsed */
2970 while (qemu_isspace(*p))
2971 p++;
2972 if (*p != '\0') {
2973 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2974 cmd->name);
2975 goto fail;
2978 return qdict;
2980 fail:
2981 QDECREF(qdict);
2982 g_free(key);
2983 return NULL;
2986 static void handle_hmp_command(Monitor *mon, const char *cmdline)
2988 QDict *qdict;
2989 const mon_cmd_t *cmd;
2991 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
2992 if (!cmd) {
2993 return;
2996 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
2997 if (!qdict) {
2998 monitor_printf(mon, "Try \"help %s\" for more information\n",
2999 cmd->name);
3000 return;
3003 cmd->cmd(mon, qdict);
3004 QDECREF(qdict);
3007 static void cmd_completion(Monitor *mon, const char *name, const char *list)
3009 const char *p, *pstart;
3010 char cmd[128];
3011 int len;
3013 p = list;
3014 for(;;) {
3015 pstart = p;
3016 p = strchr(p, '|');
3017 if (!p)
3018 p = pstart + strlen(pstart);
3019 len = p - pstart;
3020 if (len > sizeof(cmd) - 2)
3021 len = sizeof(cmd) - 2;
3022 memcpy(cmd, pstart, len);
3023 cmd[len] = '\0';
3024 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3025 readline_add_completion(mon->rs, cmd);
3027 if (*p == '\0')
3028 break;
3029 p++;
3033 static void file_completion(Monitor *mon, const char *input)
3035 DIR *ffs;
3036 struct dirent *d;
3037 char path[1024];
3038 char file[1024], file_prefix[1024];
3039 int input_path_len;
3040 const char *p;
3042 p = strrchr(input, '/');
3043 if (!p) {
3044 input_path_len = 0;
3045 pstrcpy(file_prefix, sizeof(file_prefix), input);
3046 pstrcpy(path, sizeof(path), ".");
3047 } else {
3048 input_path_len = p - input + 1;
3049 memcpy(path, input, input_path_len);
3050 if (input_path_len > sizeof(path) - 1)
3051 input_path_len = sizeof(path) - 1;
3052 path[input_path_len] = '\0';
3053 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3056 ffs = opendir(path);
3057 if (!ffs)
3058 return;
3059 for(;;) {
3060 struct stat sb;
3061 d = readdir(ffs);
3062 if (!d)
3063 break;
3065 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3066 continue;
3069 if (strstart(d->d_name, file_prefix, NULL)) {
3070 memcpy(file, input, input_path_len);
3071 if (input_path_len < sizeof(file))
3072 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3073 d->d_name);
3074 /* stat the file to find out if it's a directory.
3075 * In that case add a slash to speed up typing long paths
3077 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
3078 pstrcat(file, sizeof(file), "/");
3080 readline_add_completion(mon->rs, file);
3083 closedir(ffs);
3086 static const char *next_arg_type(const char *typestr)
3088 const char *p = strchr(typestr, ':');
3089 return (p != NULL ? ++p : typestr);
3092 static void add_completion_option(ReadLineState *rs, const char *str,
3093 const char *option)
3095 if (!str || !option) {
3096 return;
3098 if (!strncmp(option, str, strlen(str))) {
3099 readline_add_completion(rs, option);
3103 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3105 size_t len;
3106 ChardevBackendInfoList *list, *start;
3108 if (nb_args != 2) {
3109 return;
3111 len = strlen(str);
3112 readline_set_completion_index(rs, len);
3114 start = list = qmp_query_chardev_backends(NULL);
3115 while (list) {
3116 const char *chr_name = list->value->name;
3118 if (!strncmp(chr_name, str, len)) {
3119 readline_add_completion(rs, chr_name);
3121 list = list->next;
3123 qapi_free_ChardevBackendInfoList(start);
3126 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
3128 size_t len;
3129 int i;
3131 if (nb_args != 2) {
3132 return;
3134 len = strlen(str);
3135 readline_set_completion_index(rs, len);
3136 for (i = 0; NetClientDriver_lookup[i]; i++) {
3137 add_completion_option(rs, str, NetClientDriver_lookup[i]);
3141 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
3143 GSList *list, *elt;
3144 size_t len;
3146 if (nb_args != 2) {
3147 return;
3150 len = strlen(str);
3151 readline_set_completion_index(rs, len);
3152 list = elt = object_class_get_list(TYPE_DEVICE, false);
3153 while (elt) {
3154 const char *name;
3155 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
3156 TYPE_DEVICE);
3157 name = object_class_get_name(OBJECT_CLASS(dc));
3159 if (!dc->cannot_instantiate_with_device_add_yet
3160 && !strncmp(name, str, len)) {
3161 readline_add_completion(rs, name);
3163 elt = elt->next;
3165 g_slist_free(list);
3168 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
3170 GSList *list, *elt;
3171 size_t len;
3173 if (nb_args != 2) {
3174 return;
3177 len = strlen(str);
3178 readline_set_completion_index(rs, len);
3179 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
3180 while (elt) {
3181 const char *name;
3183 name = object_class_get_name(OBJECT_CLASS(elt->data));
3184 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
3185 readline_add_completion(rs, name);
3187 elt = elt->next;
3189 g_slist_free(list);
3192 static void peripheral_device_del_completion(ReadLineState *rs,
3193 const char *str, size_t len)
3195 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
3196 GSList *list, *item;
3198 list = qdev_build_hotpluggable_device_list(peripheral);
3199 if (!list) {
3200 return;
3203 for (item = list; item; item = g_slist_next(item)) {
3204 DeviceState *dev = item->data;
3206 if (dev->id && !strncmp(str, dev->id, len)) {
3207 readline_add_completion(rs, dev->id);
3211 g_slist_free(list);
3214 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3216 size_t len;
3217 ChardevInfoList *list, *start;
3219 if (nb_args != 2) {
3220 return;
3222 len = strlen(str);
3223 readline_set_completion_index(rs, len);
3225 start = list = qmp_query_chardev(NULL);
3226 while (list) {
3227 ChardevInfo *chr = list->value;
3229 if (!strncmp(chr->label, str, len)) {
3230 readline_add_completion(rs, chr->label);
3232 list = list->next;
3234 qapi_free_ChardevInfoList(start);
3237 static void ringbuf_completion(ReadLineState *rs, const char *str)
3239 size_t len;
3240 ChardevInfoList *list, *start;
3242 len = strlen(str);
3243 readline_set_completion_index(rs, len);
3245 start = list = qmp_query_chardev(NULL);
3246 while (list) {
3247 ChardevInfo *chr_info = list->value;
3249 if (!strncmp(chr_info->label, str, len)) {
3250 Chardev *chr = qemu_chr_find(chr_info->label);
3251 if (chr && CHARDEV_IS_RINGBUF(chr)) {
3252 readline_add_completion(rs, chr_info->label);
3255 list = list->next;
3257 qapi_free_ChardevInfoList(start);
3260 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
3262 if (nb_args != 2) {
3263 return;
3265 ringbuf_completion(rs, str);
3268 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
3270 size_t len;
3272 if (nb_args != 2) {
3273 return;
3276 len = strlen(str);
3277 readline_set_completion_index(rs, len);
3278 peripheral_device_del_completion(rs, str, len);
3281 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
3283 ObjectPropertyInfoList *list, *start;
3284 size_t len;
3286 if (nb_args != 2) {
3287 return;
3289 len = strlen(str);
3290 readline_set_completion_index(rs, len);
3292 start = list = qmp_qom_list("/objects", NULL);
3293 while (list) {
3294 ObjectPropertyInfo *info = list->value;
3296 if (!strncmp(info->type, "child<", 5)
3297 && !strncmp(info->name, str, len)) {
3298 readline_add_completion(rs, info->name);
3300 list = list->next;
3302 qapi_free_ObjectPropertyInfoList(start);
3305 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
3307 int i;
3308 char *sep;
3309 size_t len;
3311 if (nb_args != 2) {
3312 return;
3314 sep = strrchr(str, '-');
3315 if (sep) {
3316 str = sep + 1;
3318 len = strlen(str);
3319 readline_set_completion_index(rs, len);
3320 for (i = 0; i < Q_KEY_CODE__MAX; i++) {
3321 if (!strncmp(str, QKeyCode_lookup[i], len)) {
3322 readline_add_completion(rs, QKeyCode_lookup[i]);
3327 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
3329 size_t len;
3331 len = strlen(str);
3332 readline_set_completion_index(rs, len);
3333 if (nb_args == 2) {
3334 NetClientState *ncs[MAX_QUEUE_NUM];
3335 int count, i;
3336 count = qemu_find_net_clients_except(NULL, ncs,
3337 NET_CLIENT_DRIVER_NONE,
3338 MAX_QUEUE_NUM);
3339 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3340 const char *name = ncs[i]->name;
3341 if (!strncmp(str, name, len)) {
3342 readline_add_completion(rs, name);
3345 } else if (nb_args == 3) {
3346 add_completion_option(rs, str, "on");
3347 add_completion_option(rs, str, "off");
3351 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
3353 int len, count, i;
3354 NetClientState *ncs[MAX_QUEUE_NUM];
3356 if (nb_args != 2) {
3357 return;
3360 len = strlen(str);
3361 readline_set_completion_index(rs, len);
3362 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_DRIVER_NIC,
3363 MAX_QUEUE_NUM);
3364 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3365 QemuOpts *opts;
3366 const char *name = ncs[i]->name;
3367 if (strncmp(str, name, len)) {
3368 continue;
3370 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
3371 if (opts) {
3372 readline_add_completion(rs, name);
3377 void info_trace_events_completion(ReadLineState *rs, int nb_args, const char *str)
3379 size_t len;
3381 len = strlen(str);
3382 readline_set_completion_index(rs, len);
3383 if (nb_args == 2) {
3384 TraceEventIter iter;
3385 TraceEvent *ev;
3386 char *pattern = g_strdup_printf("%s*", str);
3387 trace_event_iter_init(&iter, pattern);
3388 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3389 readline_add_completion(rs, trace_event_get_name(ev));
3391 g_free(pattern);
3395 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
3397 size_t len;
3399 len = strlen(str);
3400 readline_set_completion_index(rs, len);
3401 if (nb_args == 2) {
3402 TraceEventIter iter;
3403 TraceEvent *ev;
3404 char *pattern = g_strdup_printf("%s*", str);
3405 trace_event_iter_init(&iter, pattern);
3406 while ((ev = trace_event_iter_next(&iter)) != NULL) {
3407 readline_add_completion(rs, trace_event_get_name(ev));
3409 g_free(pattern);
3410 } else if (nb_args == 3) {
3411 add_completion_option(rs, str, "on");
3412 add_completion_option(rs, str, "off");
3416 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
3418 int i;
3420 if (nb_args != 2) {
3421 return;
3423 readline_set_completion_index(rs, strlen(str));
3424 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
3425 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
3429 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
3430 const char *str)
3432 size_t len;
3434 len = strlen(str);
3435 readline_set_completion_index(rs, len);
3436 if (nb_args == 2) {
3437 int i;
3438 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
3439 const char *name = MigrationCapability_lookup[i];
3440 if (!strncmp(str, name, len)) {
3441 readline_add_completion(rs, name);
3444 } else if (nb_args == 3) {
3445 add_completion_option(rs, str, "on");
3446 add_completion_option(rs, str, "off");
3450 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
3451 const char *str)
3453 size_t len;
3455 len = strlen(str);
3456 readline_set_completion_index(rs, len);
3457 if (nb_args == 2) {
3458 int i;
3459 for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
3460 const char *name = MigrationParameter_lookup[i];
3461 if (!strncmp(str, name, len)) {
3462 readline_add_completion(rs, name);
3468 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
3470 int i;
3471 size_t len;
3472 if (nb_args != 2) {
3473 return;
3475 len = strlen(str);
3476 readline_set_completion_index(rs, len);
3477 for (i = 0; host_net_devices[i]; i++) {
3478 if (!strncmp(host_net_devices[i], str, len)) {
3479 readline_add_completion(rs, host_net_devices[i]);
3484 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
3486 NetClientState *ncs[MAX_QUEUE_NUM];
3487 int count, i, len;
3489 len = strlen(str);
3490 readline_set_completion_index(rs, len);
3491 if (nb_args == 2) {
3492 count = qemu_find_net_clients_except(NULL, ncs,
3493 NET_CLIENT_DRIVER_NONE,
3494 MAX_QUEUE_NUM);
3495 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3496 int id;
3497 char name[16];
3499 if (net_hub_id_for_client(ncs[i], &id)) {
3500 continue;
3502 snprintf(name, sizeof(name), "%d", id);
3503 if (!strncmp(str, name, len)) {
3504 readline_add_completion(rs, name);
3507 return;
3508 } else if (nb_args == 3) {
3509 count = qemu_find_net_clients_except(NULL, ncs,
3510 NET_CLIENT_DRIVER_NIC,
3511 MAX_QUEUE_NUM);
3512 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
3513 int id;
3514 const char *name;
3516 if (ncs[i]->info->type == NET_CLIENT_DRIVER_HUBPORT ||
3517 net_hub_id_for_client(ncs[i], &id)) {
3518 continue;
3520 name = ncs[i]->name;
3521 if (!strncmp(str, name, len)) {
3522 readline_add_completion(rs, name);
3525 return;
3529 static void vm_completion(ReadLineState *rs, const char *str)
3531 size_t len;
3532 BlockDriverState *bs;
3533 BdrvNextIterator it;
3535 len = strlen(str);
3536 readline_set_completion_index(rs, len);
3538 for (bs = bdrv_first(&it); bs; bs = bdrv_next(&it)) {
3539 SnapshotInfoList *snapshots, *snapshot;
3540 AioContext *ctx = bdrv_get_aio_context(bs);
3541 bool ok = false;
3543 aio_context_acquire(ctx);
3544 if (bdrv_can_snapshot(bs)) {
3545 ok = bdrv_query_snapshot_info_list(bs, &snapshots, NULL) == 0;
3547 aio_context_release(ctx);
3548 if (!ok) {
3549 continue;
3552 snapshot = snapshots;
3553 while (snapshot) {
3554 char *completion = snapshot->value->name;
3555 if (!strncmp(str, completion, len)) {
3556 readline_add_completion(rs, completion);
3558 completion = snapshot->value->id;
3559 if (!strncmp(str, completion, len)) {
3560 readline_add_completion(rs, completion);
3562 snapshot = snapshot->next;
3564 qapi_free_SnapshotInfoList(snapshots);
3569 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
3571 if (nb_args == 2) {
3572 vm_completion(rs, str);
3576 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
3578 if (nb_args == 2) {
3579 vm_completion(rs, str);
3583 static void monitor_find_completion_by_table(Monitor *mon,
3584 const mon_cmd_t *cmd_table,
3585 char **args,
3586 int nb_args)
3588 const char *cmdname;
3589 int i;
3590 const char *ptype, *str, *name;
3591 const mon_cmd_t *cmd;
3592 BlockBackend *blk = NULL;
3594 if (nb_args <= 1) {
3595 /* command completion */
3596 if (nb_args == 0)
3597 cmdname = "";
3598 else
3599 cmdname = args[0];
3600 readline_set_completion_index(mon->rs, strlen(cmdname));
3601 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3602 cmd_completion(mon, cmdname, cmd->name);
3604 } else {
3605 /* find the command */
3606 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
3607 if (compare_cmd(args[0], cmd->name)) {
3608 break;
3611 if (!cmd->name) {
3612 return;
3615 if (cmd->sub_table) {
3616 /* do the job again */
3617 monitor_find_completion_by_table(mon, cmd->sub_table,
3618 &args[1], nb_args - 1);
3619 return;
3621 if (cmd->command_completion) {
3622 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
3623 return;
3626 ptype = next_arg_type(cmd->args_type);
3627 for(i = 0; i < nb_args - 2; i++) {
3628 if (*ptype != '\0') {
3629 ptype = next_arg_type(ptype);
3630 while (*ptype == '?')
3631 ptype = next_arg_type(ptype);
3634 str = args[nb_args - 1];
3635 while (*ptype == '-' && ptype[1] != '\0') {
3636 ptype = next_arg_type(ptype);
3638 switch(*ptype) {
3639 case 'F':
3640 /* file completion */
3641 readline_set_completion_index(mon->rs, strlen(str));
3642 file_completion(mon, str);
3643 break;
3644 case 'B':
3645 /* block device name completion */
3646 readline_set_completion_index(mon->rs, strlen(str));
3647 while ((blk = blk_next(blk)) != NULL) {
3648 name = blk_name(blk);
3649 if (str[0] == '\0' ||
3650 !strncmp(name, str, strlen(str))) {
3651 readline_add_completion(mon->rs, name);
3654 break;
3655 case 's':
3656 case 'S':
3657 if (!strcmp(cmd->name, "help|?")) {
3658 monitor_find_completion_by_table(mon, cmd_table,
3659 &args[1], nb_args - 1);
3661 break;
3662 default:
3663 break;
3668 static void monitor_find_completion(void *opaque,
3669 const char *cmdline)
3671 Monitor *mon = opaque;
3672 char *args[MAX_ARGS];
3673 int nb_args, len;
3675 /* 1. parse the cmdline */
3676 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
3677 return;
3680 /* if the line ends with a space, it means we want to complete the
3681 next arg */
3682 len = strlen(cmdline);
3683 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3684 if (nb_args >= MAX_ARGS) {
3685 goto cleanup;
3687 args[nb_args++] = g_strdup("");
3690 /* 2. auto complete according to args */
3691 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
3693 cleanup:
3694 free_cmdline_args(args, nb_args);
3697 static int monitor_can_read(void *opaque)
3699 Monitor *mon = opaque;
3701 return (mon->suspend_cnt == 0) ? 1 : 0;
3704 static void handle_qmp_command(JSONMessageParser *parser, GQueue *tokens)
3706 QObject *req, *rsp = NULL, *id = NULL;
3707 QDict *qdict = NULL;
3708 Monitor *mon = cur_mon;
3709 Error *err = NULL;
3711 req = json_parser_parse_err(tokens, NULL, &err);
3712 if (!req && !err) {
3713 /* json_parser_parse_err() sucks: can fail without setting @err */
3714 error_setg(&err, QERR_JSON_PARSING);
3716 if (err) {
3717 goto err_out;
3720 qdict = qobject_to_qdict(req);
3721 if (qdict) {
3722 id = qdict_get(qdict, "id");
3723 qobject_incref(id);
3724 qdict_del(qdict, "id");
3725 } /* else will fail qmp_dispatch() */
3727 rsp = qmp_dispatch(cur_mon->qmp.commands, req);
3729 if (mon->qmp.commands == &qmp_cap_negotiation_commands) {
3730 qdict = qdict_get_qdict(qobject_to_qdict(rsp), "error");
3731 if (qdict
3732 && !g_strcmp0(qdict_get_try_str(qdict, "class"),
3733 QapiErrorClass_lookup[ERROR_CLASS_COMMAND_NOT_FOUND])) {
3734 /* Provide a more useful error message */
3735 qdict_del(qdict, "desc");
3736 qdict_put(qdict, "desc",
3737 qstring_from_str("Expecting capabilities negotiation"
3738 " with 'qmp_capabilities'"));
3742 err_out:
3743 if (err) {
3744 qdict = qdict_new();
3745 qdict_put_obj(qdict, "error", qmp_build_error_object(err));
3746 error_free(err);
3747 rsp = QOBJECT(qdict);
3750 if (rsp) {
3751 if (id) {
3752 qdict_put_obj(qobject_to_qdict(rsp), "id", id);
3753 id = NULL;
3756 monitor_json_emitter(mon, rsp);
3759 qobject_decref(id);
3760 qobject_decref(rsp);
3761 qobject_decref(req);
3764 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
3766 Monitor *old_mon = cur_mon;
3768 cur_mon = opaque;
3770 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
3772 cur_mon = old_mon;
3775 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3777 Monitor *old_mon = cur_mon;
3778 int i;
3780 cur_mon = opaque;
3782 if (cur_mon->rs) {
3783 for (i = 0; i < size; i++)
3784 readline_handle_byte(cur_mon->rs, buf[i]);
3785 } else {
3786 if (size == 0 || buf[size - 1] != 0)
3787 monitor_printf(cur_mon, "corrupted command\n");
3788 else
3789 handle_hmp_command(cur_mon, (char *)buf);
3792 cur_mon = old_mon;
3795 static void monitor_command_cb(void *opaque, const char *cmdline,
3796 void *readline_opaque)
3798 Monitor *mon = opaque;
3800 monitor_suspend(mon);
3801 handle_hmp_command(mon, cmdline);
3802 monitor_resume(mon);
3805 int monitor_suspend(Monitor *mon)
3807 if (!mon->rs)
3808 return -ENOTTY;
3809 mon->suspend_cnt++;
3810 return 0;
3813 void monitor_resume(Monitor *mon)
3815 if (!mon->rs)
3816 return;
3817 if (--mon->suspend_cnt == 0)
3818 readline_show_prompt(mon->rs);
3821 static QObject *get_qmp_greeting(void)
3823 QObject *ver = NULL;
3825 qmp_marshal_query_version(NULL, &ver, NULL);
3827 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3828 ver);
3831 static void monitor_qmp_event(void *opaque, int event)
3833 QObject *data;
3834 Monitor *mon = opaque;
3836 switch (event) {
3837 case CHR_EVENT_OPENED:
3838 mon->qmp.commands = &qmp_cap_negotiation_commands;
3839 data = get_qmp_greeting();
3840 monitor_json_emitter(mon, data);
3841 qobject_decref(data);
3842 mon_refcount++;
3843 break;
3844 case CHR_EVENT_CLOSED:
3845 json_message_parser_destroy(&mon->qmp.parser);
3846 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
3847 mon_refcount--;
3848 monitor_fdsets_cleanup();
3849 break;
3853 static void monitor_event(void *opaque, int event)
3855 Monitor *mon = opaque;
3857 switch (event) {
3858 case CHR_EVENT_MUX_IN:
3859 qemu_mutex_lock(&mon->out_lock);
3860 mon->mux_out = 0;
3861 qemu_mutex_unlock(&mon->out_lock);
3862 if (mon->reset_seen) {
3863 readline_restart(mon->rs);
3864 monitor_resume(mon);
3865 monitor_flush(mon);
3866 } else {
3867 mon->suspend_cnt = 0;
3869 break;
3871 case CHR_EVENT_MUX_OUT:
3872 if (mon->reset_seen) {
3873 if (mon->suspend_cnt == 0) {
3874 monitor_printf(mon, "\n");
3876 monitor_flush(mon);
3877 monitor_suspend(mon);
3878 } else {
3879 mon->suspend_cnt++;
3881 qemu_mutex_lock(&mon->out_lock);
3882 mon->mux_out = 1;
3883 qemu_mutex_unlock(&mon->out_lock);
3884 break;
3886 case CHR_EVENT_OPENED:
3887 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3888 "information\n", QEMU_VERSION);
3889 if (!mon->mux_out) {
3890 readline_restart(mon->rs);
3891 readline_show_prompt(mon->rs);
3893 mon->reset_seen = 1;
3894 mon_refcount++;
3895 break;
3897 case CHR_EVENT_CLOSED:
3898 mon_refcount--;
3899 monitor_fdsets_cleanup();
3900 break;
3904 static int
3905 compare_mon_cmd(const void *a, const void *b)
3907 return strcmp(((const mon_cmd_t *)a)->name,
3908 ((const mon_cmd_t *)b)->name);
3911 static void sortcmdlist(void)
3913 int array_num;
3914 int elem_size = sizeof(mon_cmd_t);
3916 array_num = sizeof(mon_cmds)/elem_size-1;
3917 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
3919 array_num = sizeof(info_cmds)/elem_size-1;
3920 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
3923 /* These functions just adapt the readline interface in a typesafe way. We
3924 * could cast function pointers but that discards compiler checks.
3926 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
3927 const char *fmt, ...)
3929 va_list ap;
3930 va_start(ap, fmt);
3931 monitor_vprintf(opaque, fmt, ap);
3932 va_end(ap);
3935 static void monitor_readline_flush(void *opaque)
3937 monitor_flush(opaque);
3941 * Print to current monitor if we have one, else to stderr.
3942 * TODO should return int, so callers can calculate width, but that
3943 * requires surgery to monitor_vprintf(). Left for another day.
3945 void error_vprintf(const char *fmt, va_list ap)
3947 if (cur_mon && !monitor_cur_is_qmp()) {
3948 monitor_vprintf(cur_mon, fmt, ap);
3949 } else {
3950 vfprintf(stderr, fmt, ap);
3954 void error_vprintf_unless_qmp(const char *fmt, va_list ap)
3956 if (cur_mon && !monitor_cur_is_qmp()) {
3957 monitor_vprintf(cur_mon, fmt, ap);
3958 } else if (!cur_mon) {
3959 vfprintf(stderr, fmt, ap);
3963 static void __attribute__((constructor)) monitor_lock_init(void)
3965 qemu_mutex_init(&monitor_lock);
3968 void monitor_init(Chardev *chr, int flags)
3970 static int is_first_init = 1;
3971 Monitor *mon;
3973 if (is_first_init) {
3974 monitor_qapi_event_init();
3975 sortcmdlist();
3976 is_first_init = 0;
3979 mon = g_malloc(sizeof(*mon));
3980 monitor_data_init(mon);
3982 qemu_chr_fe_init(&mon->chr, chr, &error_abort);
3983 mon->flags = flags;
3984 if (flags & MONITOR_USE_READLINE) {
3985 mon->rs = readline_init(monitor_readline_printf,
3986 monitor_readline_flush,
3987 mon,
3988 monitor_find_completion);
3989 monitor_read_command(mon, 0);
3992 if (monitor_is_qmp(mon)) {
3993 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_qmp_read,
3994 monitor_qmp_event, mon, NULL, true);
3995 qemu_chr_fe_set_echo(&mon->chr, true);
3996 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
3997 } else {
3998 qemu_chr_fe_set_handlers(&mon->chr, monitor_can_read, monitor_read,
3999 monitor_event, mon, NULL, true);
4002 qemu_mutex_lock(&monitor_lock);
4003 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4004 qemu_mutex_unlock(&monitor_lock);
4007 void monitor_cleanup(void)
4009 Monitor *mon, *next;
4011 qemu_mutex_lock(&monitor_lock);
4012 QLIST_FOREACH_SAFE(mon, &mon_list, entry, next) {
4013 QLIST_REMOVE(mon, entry);
4014 monitor_data_destroy(mon);
4015 g_free(mon);
4017 qemu_mutex_unlock(&monitor_lock);
4020 static void bdrv_password_cb(void *opaque, const char *password,
4021 void *readline_opaque)
4023 Monitor *mon = opaque;
4024 BlockDriverState *bs = readline_opaque;
4025 int ret = 0;
4026 Error *local_err = NULL;
4028 bdrv_add_key(bs, password, &local_err);
4029 if (local_err) {
4030 error_report_err(local_err);
4031 ret = -EPERM;
4033 if (mon->password_completion_cb)
4034 mon->password_completion_cb(mon->password_opaque, ret);
4036 monitor_read_command(mon, 1);
4039 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4040 BlockCompletionFunc *completion_cb,
4041 void *opaque)
4043 int err;
4045 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4046 bdrv_get_encrypted_filename(bs));
4048 mon->password_completion_cb = completion_cb;
4049 mon->password_opaque = opaque;
4051 err = monitor_read_password(mon, bdrv_password_cb, bs);
4053 if (err && completion_cb)
4054 completion_cb(opaque, err);
4056 return err;
4059 int monitor_read_block_device_key(Monitor *mon, const char *device,
4060 BlockCompletionFunc *completion_cb,
4061 void *opaque)
4063 Error *err = NULL;
4064 BlockBackend *blk;
4066 blk = blk_by_name(device);
4067 if (!blk) {
4068 monitor_printf(mon, "Device not found %s\n", device);
4069 return -1;
4071 if (!blk_bs(blk)) {
4072 monitor_printf(mon, "Device '%s' has no medium\n", device);
4073 return -1;
4076 bdrv_add_key(blk_bs(blk), NULL, &err);
4077 if (err) {
4078 error_free(err);
4079 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
4082 if (completion_cb) {
4083 completion_cb(opaque, 0);
4085 return 0;
4088 QemuOptsList qemu_mon_opts = {
4089 .name = "mon",
4090 .implied_opt_name = "chardev",
4091 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
4092 .desc = {
4094 .name = "mode",
4095 .type = QEMU_OPT_STRING,
4097 .name = "chardev",
4098 .type = QEMU_OPT_STRING,
4100 .name = "default", /* deprecated */
4101 .type = QEMU_OPT_BOOL,
4103 .name = "pretty",
4104 .type = QEMU_OPT_BOOL,
4106 { /* end of list */ }
4110 #ifndef TARGET_I386
4111 void qmp_rtc_reset_reinjection(Error **errp)
4113 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
4115 #endif
4117 #ifndef TARGET_S390X
4118 void qmp_dump_skeys(const char *filename, Error **errp)
4120 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
4122 #endif
4124 #ifndef TARGET_ARM
4125 GICCapabilityList *qmp_query_gic_capabilities(Error **errp)
4127 error_setg(errp, QERR_FEATURE_DISABLED, "query-gic-capabilities");
4128 return NULL;
4130 #endif
4132 HotpluggableCPUList *qmp_query_hotpluggable_cpus(Error **errp)
4134 MachineState *ms = MACHINE(qdev_get_machine());
4135 MachineClass *mc = MACHINE_GET_CLASS(ms);
4137 if (!mc->has_hotpluggable_cpus) {
4138 error_setg(errp, QERR_FEATURE_DISABLED, "query-hotpluggable-cpus");
4139 return NULL;
4142 return machine_query_hotpluggable_cpus(ms);