qapi-commands: Drop useless initialization
[qemu.git] / monitor.c
blobb1f2a2979377160da0606134f53a5b1a141454be
1 /*
2 * QEMU monitor
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
24 #include <dirent.h>
25 #include "hw/hw.h"
26 #include "monitor/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/i386/pc.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
33 #include "net/net.h"
34 #include "net/slirp.h"
35 #include "sysemu/char.h"
36 #include "ui/qemu-spice.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/numa.h"
39 #include "monitor/monitor.h"
40 #include "qemu/readline.h"
41 #include "ui/console.h"
42 #include "ui/input.h"
43 #include "sysemu/blockdev.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "migration/migration.h"
49 #include "sysemu/kvm.h"
50 #include "qemu/acl.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qerror.h"
53 #include "qapi/qmp/qint.h"
54 #include "qapi/qmp/qfloat.h"
55 #include "qapi/qmp/qlist.h"
56 #include "qapi/qmp/qbool.h"
57 #include "qapi/qmp/qstring.h"
58 #include "qapi/qmp/qjson.h"
59 #include "qapi/qmp/json-streamer.h"
60 #include "qapi/qmp/json-parser.h"
61 #include <qom/object_interfaces.h>
62 #include "qemu/osdep.h"
63 #include "cpu.h"
64 #include "trace.h"
65 #include "trace/control.h"
66 #ifdef CONFIG_TRACE_SIMPLE
67 #include "trace/simple.h"
68 #endif
69 #include "exec/memory.h"
70 #include "exec/cpu_ldst.h"
71 #include "qmp-commands.h"
72 #include "hmp.h"
73 #include "qemu/thread.h"
74 #include "block/qapi.h"
75 #include "qapi/qmp-event.h"
76 #include "qapi-event.h"
77 #include "sysemu/block-backend.h"
79 /* for hmp_info_irq/pic */
80 #if defined(TARGET_SPARC)
81 #include "hw/sparc/sun4m.h"
82 #endif
83 #include "hw/lm32/lm32_pic.h"
85 #if defined(TARGET_S390X)
86 #include "hw/s390x/storage-keys.h"
87 #endif
90 * Supported types:
92 * 'F' filename
93 * 'B' block device name
94 * 's' string (accept optional quote)
95 * 'S' it just appends the rest of the string (accept optional quote)
96 * 'O' option string of the form NAME=VALUE,...
97 * parsed according to QemuOptsList given by its name
98 * Example: 'device:O' uses qemu_device_opts.
99 * Restriction: only lists with empty desc are supported
100 * TODO lift the restriction
101 * 'i' 32 bit integer
102 * 'l' target long (32 or 64 bit)
103 * 'M' Non-negative target long (32 or 64 bit), in user mode the
104 * value is multiplied by 2^20 (think Mebibyte)
105 * 'o' octets (aka bytes)
106 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
107 * K, k suffix, which multiplies the value by 2^60 for suffixes E
108 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
109 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
110 * 'T' double
111 * user mode accepts an optional ms, us, ns suffix,
112 * which divides the value by 1e3, 1e6, 1e9, respectively
113 * '/' optional gdb-like print format (like "/10x")
115 * '?' optional type (for all types, except '/')
116 * '.' other form of optional type (for 'i' and 'l')
117 * 'b' boolean
118 * user mode accepts "on" or "off"
119 * '-' optional parameter (eg. '-f')
123 typedef struct mon_cmd_t {
124 const char *name;
125 const char *args_type;
126 const char *params;
127 const char *help;
128 union {
129 void (*cmd)(Monitor *mon, const QDict *qdict);
130 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
131 } mhandler;
132 /* @sub_table is a list of 2nd level of commands. If it do not exist,
133 * mhandler should be used. If it exist, sub_table[?].mhandler should be
134 * used, and mhandler of 1st level plays the role of help function.
136 struct mon_cmd_t *sub_table;
137 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
138 } mon_cmd_t;
140 /* file descriptors passed via SCM_RIGHTS */
141 typedef struct mon_fd_t mon_fd_t;
142 struct mon_fd_t {
143 char *name;
144 int fd;
145 QLIST_ENTRY(mon_fd_t) next;
148 /* file descriptor associated with a file descriptor set */
149 typedef struct MonFdsetFd MonFdsetFd;
150 struct MonFdsetFd {
151 int fd;
152 bool removed;
153 char *opaque;
154 QLIST_ENTRY(MonFdsetFd) next;
157 /* file descriptor set containing fds passed via SCM_RIGHTS */
158 typedef struct MonFdset MonFdset;
159 struct MonFdset {
160 int64_t id;
161 QLIST_HEAD(, MonFdsetFd) fds;
162 QLIST_HEAD(, MonFdsetFd) dup_fds;
163 QLIST_ENTRY(MonFdset) next;
166 typedef struct {
167 QObject *id;
168 JSONMessageParser parser;
170 * When a client connects, we're in capabilities negotiation mode.
171 * When command qmp_capabilities succeeds, we go into command
172 * mode.
174 bool in_command_mode; /* are we in command mode? */
175 } MonitorQMP;
178 * To prevent flooding clients, events can be throttled. The
179 * throttling is calculated globally, rather than per-Monitor
180 * instance.
182 typedef struct MonitorQAPIEventState {
183 QAPIEvent event; /* Event being tracked */
184 int64_t rate; /* Minimum time (in ns) between two events */
185 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
186 QEMUTimer *timer; /* Timer for handling delayed events */
187 QObject *data; /* Event pending delayed dispatch */
188 } MonitorQAPIEventState;
190 struct Monitor {
191 CharDriverState *chr;
192 int reset_seen;
193 int flags;
194 int suspend_cnt;
195 bool skip_flush;
197 QemuMutex out_lock;
198 QString *outbuf;
199 guint out_watch;
201 /* Read under either BQL or out_lock, written with BQL+out_lock. */
202 int mux_out;
204 ReadLineState *rs;
205 MonitorQMP qmp;
206 CPUState *mon_cpu;
207 BlockCompletionFunc *password_completion_cb;
208 void *password_opaque;
209 mon_cmd_t *cmd_table;
210 QLIST_HEAD(,mon_fd_t) fds;
211 QLIST_ENTRY(Monitor) entry;
214 /* QMP checker flags */
215 #define QMP_ACCEPT_UNKNOWNS 1
217 /* Protects mon_list, monitor_event_state. */
218 static QemuMutex monitor_lock;
220 static QLIST_HEAD(mon_list, Monitor) mon_list;
221 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
222 static int mon_refcount;
224 static mon_cmd_t mon_cmds[];
225 static mon_cmd_t info_cmds[];
227 static const mon_cmd_t qmp_cmds[];
229 Monitor *cur_mon;
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 /* partinal 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 = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
317 monitor_unblocked, mon);
322 void monitor_flush(Monitor *mon)
324 qemu_mutex_lock(&mon->out_lock);
325 monitor_flush_locked(mon);
326 qemu_mutex_unlock(&mon->out_lock);
329 /* flush at every end of line */
330 static void monitor_puts(Monitor *mon, const char *str)
332 char c;
334 qemu_mutex_lock(&mon->out_lock);
335 for(;;) {
336 c = *str++;
337 if (c == '\0')
338 break;
339 if (c == '\n') {
340 qstring_append_chr(mon->outbuf, '\r');
342 qstring_append_chr(mon->outbuf, c);
343 if (c == '\n') {
344 monitor_flush_locked(mon);
347 qemu_mutex_unlock(&mon->out_lock);
350 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
352 char *buf;
354 if (!mon)
355 return;
357 if (monitor_is_qmp(mon)) {
358 return;
361 buf = g_strdup_vprintf(fmt, ap);
362 monitor_puts(mon, buf);
363 g_free(buf);
366 void monitor_printf(Monitor *mon, const char *fmt, ...)
368 va_list ap;
369 va_start(ap, fmt);
370 monitor_vprintf(mon, fmt, ap);
371 va_end(ap);
374 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
375 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 QDict *build_qmp_error_dict(Error *err)
400 QObject *obj;
402 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
403 ErrorClass_lookup[error_get_class(err)],
404 error_get_pretty(err));
406 return qobject_to_qdict(obj);
409 static void monitor_protocol_emitter(Monitor *mon, QObject *data,
410 Error *err)
412 QDict *qmp;
414 trace_monitor_protocol_emitter(mon);
416 if (!err) {
417 /* success response */
418 qmp = qdict_new();
419 if (data) {
420 qobject_incref(data);
421 qdict_put_obj(qmp, "return", data);
422 } else {
423 /* return an empty QDict by default */
424 qdict_put(qmp, "return", qdict_new());
426 } else {
427 /* error response */
428 qmp = build_qmp_error_dict(err);
431 if (mon->qmp.id) {
432 qdict_put_obj(qmp, "id", mon->qmp.id);
433 mon->qmp.id = NULL;
436 monitor_json_emitter(mon, QOBJECT(qmp));
437 QDECREF(qmp);
441 static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
444 * Emits the event to every monitor instance, @event is only used for trace
445 * Called with monitor_lock held.
447 static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
449 Monitor *mon;
451 trace_monitor_protocol_event_emit(event, data);
452 QLIST_FOREACH(mon, &mon_list, entry) {
453 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
454 monitor_json_emitter(mon, data);
460 * Queue a new event for emission to Monitor instances,
461 * applying any rate limiting if required.
463 static void
464 monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
466 MonitorQAPIEventState *evstate;
467 assert(event < QAPI_EVENT_MAX);
468 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
470 evstate = &(monitor_qapi_event_state[event]);
471 trace_monitor_protocol_event_queue(event,
472 data,
473 evstate->rate,
474 evstate->last,
475 now);
477 /* Rate limit of 0 indicates no throttling */
478 qemu_mutex_lock(&monitor_lock);
479 if (!evstate->rate) {
480 monitor_qapi_event_emit(event, QOBJECT(data));
481 evstate->last = now;
482 } else {
483 int64_t delta = now - evstate->last;
484 if (evstate->data ||
485 delta < evstate->rate) {
486 /* If there's an existing event pending, replace
487 * it with the new event, otherwise schedule a
488 * timer for delayed emission
490 if (evstate->data) {
491 qobject_decref(evstate->data);
492 } else {
493 int64_t then = evstate->last + evstate->rate;
494 timer_mod_ns(evstate->timer, then);
496 evstate->data = QOBJECT(data);
497 qobject_incref(evstate->data);
498 } else {
499 monitor_qapi_event_emit(event, QOBJECT(data));
500 evstate->last = now;
503 qemu_mutex_unlock(&monitor_lock);
507 * The callback invoked by QemuTimer when a delayed
508 * event is ready to be emitted
510 static void monitor_qapi_event_handler(void *opaque)
512 MonitorQAPIEventState *evstate = opaque;
513 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
515 trace_monitor_protocol_event_handler(evstate->event,
516 evstate->data,
517 evstate->last,
518 now);
519 qemu_mutex_lock(&monitor_lock);
520 if (evstate->data) {
521 monitor_qapi_event_emit(evstate->event, evstate->data);
522 qobject_decref(evstate->data);
523 evstate->data = NULL;
525 evstate->last = now;
526 qemu_mutex_unlock(&monitor_lock);
530 * @event: the event ID to be limited
531 * @rate: the rate limit in milliseconds
533 * Sets a rate limit on a particular event, so no
534 * more than 1 event will be emitted within @rate
535 * milliseconds
537 static void
538 monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
540 MonitorQAPIEventState *evstate;
541 assert(event < QAPI_EVENT_MAX);
543 evstate = &(monitor_qapi_event_state[event]);
545 trace_monitor_protocol_event_throttle(event, rate);
546 evstate->event = event;
547 assert(rate * SCALE_MS <= INT64_MAX);
548 evstate->rate = rate * SCALE_MS;
549 evstate->last = 0;
550 evstate->data = NULL;
551 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
552 SCALE_MS,
553 monitor_qapi_event_handler,
554 evstate);
557 static void monitor_qapi_event_init(void)
559 /* Limit guest-triggerable events to 1 per second */
560 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
561 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
562 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
565 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
567 qmp_event_set_func_emit(monitor_qapi_event_queue);
570 static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
572 cur_mon->qmp.in_command_mode = true;
575 static void handle_hmp_command(Monitor *mon, const char *cmdline);
577 static void monitor_data_init(Monitor *mon)
579 memset(mon, 0, sizeof(Monitor));
580 qemu_mutex_init(&mon->out_lock);
581 mon->outbuf = qstring_new();
582 /* Use *mon_cmds by default. */
583 mon->cmd_table = mon_cmds;
586 static void monitor_data_destroy(Monitor *mon)
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 Error *local_err = NULL;
859 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
860 if (local_err) {
861 error_report_err(local_err);
865 #ifdef CONFIG_TRACE_SIMPLE
866 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
868 const char *op = qdict_get_try_str(qdict, "op");
869 const char *arg = qdict_get_try_str(qdict, "arg");
871 if (!op) {
872 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
873 } else if (!strcmp(op, "on")) {
874 st_set_trace_file_enabled(true);
875 } else if (!strcmp(op, "off")) {
876 st_set_trace_file_enabled(false);
877 } else if (!strcmp(op, "flush")) {
878 st_flush_trace_buffer();
879 } else if (!strcmp(op, "set")) {
880 if (arg) {
881 st_set_trace_file(arg);
883 } else {
884 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
885 help_cmd(mon, "trace-file");
888 #endif
890 static void hmp_info_help(Monitor *mon, const QDict *qdict)
892 help_cmd(mon, "info");
895 CommandInfoList *qmp_query_commands(Error **errp)
897 CommandInfoList *info, *cmd_list = NULL;
898 const mon_cmd_t *cmd;
900 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
901 info = g_malloc0(sizeof(*info));
902 info->value = g_malloc0(sizeof(*info->value));
903 info->value->name = g_strdup(cmd->name);
905 info->next = cmd_list;
906 cmd_list = info;
909 return cmd_list;
912 EventInfoList *qmp_query_events(Error **errp)
914 EventInfoList *info, *ev_list = NULL;
915 QAPIEvent e;
917 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
918 const char *event_name = QAPIEvent_lookup[e];
919 assert(event_name != NULL);
920 info = g_malloc0(sizeof(*info));
921 info->value = g_malloc0(sizeof(*info->value));
922 info->value->name = g_strdup(event_name);
924 info->next = ev_list;
925 ev_list = info;
928 return ev_list;
931 /* set the current CPU defined by the user */
932 int monitor_set_cpu(int cpu_index)
934 CPUState *cpu;
936 cpu = qemu_get_cpu(cpu_index);
937 if (cpu == NULL) {
938 return -1;
940 cur_mon->mon_cpu = cpu;
941 return 0;
944 static CPUState *mon_get_cpu(void)
946 if (!cur_mon->mon_cpu) {
947 monitor_set_cpu(0);
949 cpu_synchronize_state(cur_mon->mon_cpu);
950 return cur_mon->mon_cpu;
953 static CPUArchState *mon_get_cpu_env(void)
955 return mon_get_cpu()->env_ptr;
958 int monitor_get_cpu_index(void)
960 return mon_get_cpu()->cpu_index;
963 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
965 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
968 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
970 dump_exec_info((FILE *)mon, monitor_fprintf);
971 dump_drift_info((FILE *)mon, monitor_fprintf);
974 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
976 dump_opcount_info((FILE *)mon, monitor_fprintf);
979 static void hmp_info_history(Monitor *mon, const QDict *qdict)
981 int i;
982 const char *str;
984 if (!mon->rs)
985 return;
986 i = 0;
987 for(;;) {
988 str = readline_get_history(mon->rs, i);
989 if (!str)
990 break;
991 monitor_printf(mon, "%d: '%s'\n", i, str);
992 i++;
996 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
998 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
1001 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1003 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1004 TraceEventInfoList *elem;
1006 for (elem = events; elem != NULL; elem = elem->next) {
1007 monitor_printf(mon, "%s : state %u\n",
1008 elem->value->name,
1009 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1011 qapi_free_TraceEventInfoList(events);
1014 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1015 bool has_port, int64_t port,
1016 bool has_tls_port, int64_t tls_port,
1017 bool has_cert_subject, const char *cert_subject,
1018 Error **errp)
1020 if (strcmp(protocol, "spice") == 0) {
1021 if (!qemu_using_spice(errp)) {
1022 return;
1025 if (!has_port && !has_tls_port) {
1026 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1027 return;
1030 if (qemu_spice_migrate_info(hostname,
1031 has_port ? port : -1,
1032 has_tls_port ? tls_port : -1,
1033 cert_subject)) {
1034 error_setg(errp, QERR_UNDEFINED_ERROR);
1035 return;
1037 return;
1040 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1043 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1045 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1048 static void hmp_log(Monitor *mon, const QDict *qdict)
1050 int mask;
1051 const char *items = qdict_get_str(qdict, "items");
1053 if (!strcmp(items, "none")) {
1054 mask = 0;
1055 } else {
1056 mask = qemu_str_to_log_mask(items);
1057 if (!mask) {
1058 help_cmd(mon, "log");
1059 return;
1062 qemu_set_log(mask);
1065 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1067 const char *option = qdict_get_try_str(qdict, "option");
1068 if (!option || !strcmp(option, "on")) {
1069 singlestep = 1;
1070 } else if (!strcmp(option, "off")) {
1071 singlestep = 0;
1072 } else {
1073 monitor_printf(mon, "unexpected option %s\n", option);
1077 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1079 const char *device = qdict_get_try_str(qdict, "device");
1080 if (!device)
1081 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1082 if (gdbserver_start(device) < 0) {
1083 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1084 device);
1085 } else if (strcmp(device, "none") == 0) {
1086 monitor_printf(mon, "Disabled gdbserver\n");
1087 } else {
1088 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1089 device);
1093 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1095 const char *action = qdict_get_str(qdict, "action");
1096 if (select_watchdog_action(action) == -1) {
1097 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1101 static void monitor_printc(Monitor *mon, int c)
1103 monitor_printf(mon, "'");
1104 switch(c) {
1105 case '\'':
1106 monitor_printf(mon, "\\'");
1107 break;
1108 case '\\':
1109 monitor_printf(mon, "\\\\");
1110 break;
1111 case '\n':
1112 monitor_printf(mon, "\\n");
1113 break;
1114 case '\r':
1115 monitor_printf(mon, "\\r");
1116 break;
1117 default:
1118 if (c >= 32 && c <= 126) {
1119 monitor_printf(mon, "%c", c);
1120 } else {
1121 monitor_printf(mon, "\\x%02x", c);
1123 break;
1125 monitor_printf(mon, "'");
1128 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1129 hwaddr addr, int is_physical)
1131 int l, line_size, i, max_digits, len;
1132 uint8_t buf[16];
1133 uint64_t v;
1135 if (format == 'i') {
1136 int flags = 0;
1137 #ifdef TARGET_I386
1138 CPUArchState *env = mon_get_cpu_env();
1139 if (wsize == 2) {
1140 flags = 1;
1141 } else if (wsize == 4) {
1142 flags = 0;
1143 } else {
1144 /* as default we use the current CS size */
1145 flags = 0;
1146 if (env) {
1147 #ifdef TARGET_X86_64
1148 if ((env->efer & MSR_EFER_LMA) &&
1149 (env->segs[R_CS].flags & DESC_L_MASK))
1150 flags = 2;
1151 else
1152 #endif
1153 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1154 flags = 1;
1157 #endif
1158 #ifdef TARGET_PPC
1159 CPUArchState *env = mon_get_cpu_env();
1160 flags = msr_le << 16;
1161 flags |= env->bfd_mach;
1162 #endif
1163 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
1164 return;
1167 len = wsize * count;
1168 if (wsize == 1)
1169 line_size = 8;
1170 else
1171 line_size = 16;
1172 max_digits = 0;
1174 switch(format) {
1175 case 'o':
1176 max_digits = (wsize * 8 + 2) / 3;
1177 break;
1178 default:
1179 case 'x':
1180 max_digits = (wsize * 8) / 4;
1181 break;
1182 case 'u':
1183 case 'd':
1184 max_digits = (wsize * 8 * 10 + 32) / 33;
1185 break;
1186 case 'c':
1187 wsize = 1;
1188 break;
1191 while (len > 0) {
1192 if (is_physical)
1193 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1194 else
1195 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1196 l = len;
1197 if (l > line_size)
1198 l = line_size;
1199 if (is_physical) {
1200 cpu_physical_memory_read(addr, buf, l);
1201 } else {
1202 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
1203 monitor_printf(mon, " Cannot access memory\n");
1204 break;
1207 i = 0;
1208 while (i < l) {
1209 switch(wsize) {
1210 default:
1211 case 1:
1212 v = ldub_p(buf + i);
1213 break;
1214 case 2:
1215 v = lduw_p(buf + i);
1216 break;
1217 case 4:
1218 v = (uint32_t)ldl_p(buf + i);
1219 break;
1220 case 8:
1221 v = ldq_p(buf + i);
1222 break;
1224 monitor_printf(mon, " ");
1225 switch(format) {
1226 case 'o':
1227 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1228 break;
1229 case 'x':
1230 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1231 break;
1232 case 'u':
1233 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1234 break;
1235 case 'd':
1236 monitor_printf(mon, "%*" PRId64, max_digits, v);
1237 break;
1238 case 'c':
1239 monitor_printc(mon, v);
1240 break;
1242 i += wsize;
1244 monitor_printf(mon, "\n");
1245 addr += l;
1246 len -= l;
1250 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1252 int count = qdict_get_int(qdict, "count");
1253 int format = qdict_get_int(qdict, "format");
1254 int size = qdict_get_int(qdict, "size");
1255 target_long addr = qdict_get_int(qdict, "addr");
1257 memory_dump(mon, count, format, size, addr, 0);
1260 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1262 int count = qdict_get_int(qdict, "count");
1263 int format = qdict_get_int(qdict, "format");
1264 int size = qdict_get_int(qdict, "size");
1265 hwaddr addr = qdict_get_int(qdict, "addr");
1267 memory_dump(mon, count, format, size, addr, 1);
1270 static void do_print(Monitor *mon, const QDict *qdict)
1272 int format = qdict_get_int(qdict, "format");
1273 hwaddr val = qdict_get_int(qdict, "val");
1275 switch(format) {
1276 case 'o':
1277 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1278 break;
1279 case 'x':
1280 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1281 break;
1282 case 'u':
1283 monitor_printf(mon, "%" HWADDR_PRIu, val);
1284 break;
1285 default:
1286 case 'd':
1287 monitor_printf(mon, "%" HWADDR_PRId, val);
1288 break;
1289 case 'c':
1290 monitor_printc(mon, val);
1291 break;
1293 monitor_printf(mon, "\n");
1296 static void hmp_sum(Monitor *mon, const QDict *qdict)
1298 uint32_t addr;
1299 uint16_t sum;
1300 uint32_t start = qdict_get_int(qdict, "start");
1301 uint32_t size = qdict_get_int(qdict, "size");
1303 sum = 0;
1304 for(addr = start; addr < (start + size); addr++) {
1305 uint8_t val = address_space_ldub(&address_space_memory, addr,
1306 MEMTXATTRS_UNSPECIFIED, NULL);
1307 /* BSD sum algorithm ('sum' Unix command) */
1308 sum = (sum >> 1) | (sum << 15);
1309 sum += val;
1311 monitor_printf(mon, "%05d\n", sum);
1314 static int mouse_button_state;
1316 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1318 int dx, dy, dz, button;
1319 const char *dx_str = qdict_get_str(qdict, "dx_str");
1320 const char *dy_str = qdict_get_str(qdict, "dy_str");
1321 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1323 dx = strtol(dx_str, NULL, 0);
1324 dy = strtol(dy_str, NULL, 0);
1325 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1326 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1328 if (dz_str) {
1329 dz = strtol(dz_str, NULL, 0);
1330 if (dz != 0) {
1331 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1332 qemu_input_queue_btn(NULL, button, true);
1333 qemu_input_event_sync();
1334 qemu_input_queue_btn(NULL, button, false);
1337 qemu_input_event_sync();
1340 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1342 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1343 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1344 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1345 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1347 int button_state = qdict_get_int(qdict, "button_state");
1349 if (mouse_button_state == button_state) {
1350 return;
1352 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1353 qemu_input_event_sync();
1354 mouse_button_state = button_state;
1357 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1359 int size = qdict_get_int(qdict, "size");
1360 int addr = qdict_get_int(qdict, "addr");
1361 int has_index = qdict_haskey(qdict, "index");
1362 uint32_t val;
1363 int suffix;
1365 if (has_index) {
1366 int index = qdict_get_int(qdict, "index");
1367 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1368 addr++;
1370 addr &= 0xffff;
1372 switch(size) {
1373 default:
1374 case 1:
1375 val = cpu_inb(addr);
1376 suffix = 'b';
1377 break;
1378 case 2:
1379 val = cpu_inw(addr);
1380 suffix = 'w';
1381 break;
1382 case 4:
1383 val = cpu_inl(addr);
1384 suffix = 'l';
1385 break;
1387 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1388 suffix, addr, size * 2, val);
1391 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1393 int size = qdict_get_int(qdict, "size");
1394 int addr = qdict_get_int(qdict, "addr");
1395 int val = qdict_get_int(qdict, "val");
1397 addr &= IOPORTS_MASK;
1399 switch (size) {
1400 default:
1401 case 1:
1402 cpu_outb(addr, val);
1403 break;
1404 case 2:
1405 cpu_outw(addr, val);
1406 break;
1407 case 4:
1408 cpu_outl(addr, val);
1409 break;
1413 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1415 Error *local_err = NULL;
1416 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1418 qemu_boot_set(bootdevice, &local_err);
1419 if (local_err) {
1420 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1421 error_free(local_err);
1422 } else {
1423 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1427 #if defined(TARGET_I386)
1428 static void print_pte(Monitor *mon, hwaddr addr,
1429 hwaddr pte,
1430 hwaddr mask)
1432 #ifdef TARGET_X86_64
1433 if (addr & (1ULL << 47)) {
1434 addr |= -1LL << 48;
1436 #endif
1437 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1438 " %c%c%c%c%c%c%c%c%c\n",
1439 addr,
1440 pte & mask,
1441 pte & PG_NX_MASK ? 'X' : '-',
1442 pte & PG_GLOBAL_MASK ? 'G' : '-',
1443 pte & PG_PSE_MASK ? 'P' : '-',
1444 pte & PG_DIRTY_MASK ? 'D' : '-',
1445 pte & PG_ACCESSED_MASK ? 'A' : '-',
1446 pte & PG_PCD_MASK ? 'C' : '-',
1447 pte & PG_PWT_MASK ? 'T' : '-',
1448 pte & PG_USER_MASK ? 'U' : '-',
1449 pte & PG_RW_MASK ? 'W' : '-');
1452 static void tlb_info_32(Monitor *mon, CPUArchState *env)
1454 unsigned int l1, l2;
1455 uint32_t pgd, pde, pte;
1457 pgd = env->cr[3] & ~0xfff;
1458 for(l1 = 0; l1 < 1024; l1++) {
1459 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1460 pde = le32_to_cpu(pde);
1461 if (pde & PG_PRESENT_MASK) {
1462 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1463 /* 4M pages */
1464 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1465 } else {
1466 for(l2 = 0; l2 < 1024; l2++) {
1467 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1468 pte = le32_to_cpu(pte);
1469 if (pte & PG_PRESENT_MASK) {
1470 print_pte(mon, (l1 << 22) + (l2 << 12),
1471 pte & ~PG_PSE_MASK,
1472 ~0xfff);
1480 static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
1482 unsigned int l1, l2, l3;
1483 uint64_t pdpe, pde, pte;
1484 uint64_t pdp_addr, pd_addr, pt_addr;
1486 pdp_addr = env->cr[3] & ~0x1f;
1487 for (l1 = 0; l1 < 4; l1++) {
1488 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1489 pdpe = le64_to_cpu(pdpe);
1490 if (pdpe & PG_PRESENT_MASK) {
1491 pd_addr = pdpe & 0x3fffffffff000ULL;
1492 for (l2 = 0; l2 < 512; l2++) {
1493 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1494 pde = le64_to_cpu(pde);
1495 if (pde & PG_PRESENT_MASK) {
1496 if (pde & PG_PSE_MASK) {
1497 /* 2M pages with PAE, CR4.PSE is ignored */
1498 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1499 ~((hwaddr)(1 << 20) - 1));
1500 } else {
1501 pt_addr = pde & 0x3fffffffff000ULL;
1502 for (l3 = 0; l3 < 512; l3++) {
1503 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1504 pte = le64_to_cpu(pte);
1505 if (pte & PG_PRESENT_MASK) {
1506 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1507 + (l3 << 12),
1508 pte & ~PG_PSE_MASK,
1509 ~(hwaddr)0xfff);
1519 #ifdef TARGET_X86_64
1520 static void tlb_info_64(Monitor *mon, CPUArchState *env)
1522 uint64_t l1, l2, l3, l4;
1523 uint64_t pml4e, pdpe, pde, pte;
1524 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1526 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1527 for (l1 = 0; l1 < 512; l1++) {
1528 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1529 pml4e = le64_to_cpu(pml4e);
1530 if (pml4e & PG_PRESENT_MASK) {
1531 pdp_addr = pml4e & 0x3fffffffff000ULL;
1532 for (l2 = 0; l2 < 512; l2++) {
1533 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1534 pdpe = le64_to_cpu(pdpe);
1535 if (pdpe & PG_PRESENT_MASK) {
1536 if (pdpe & PG_PSE_MASK) {
1537 /* 1G pages, CR4.PSE is ignored */
1538 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1539 0x3ffffc0000000ULL);
1540 } else {
1541 pd_addr = pdpe & 0x3fffffffff000ULL;
1542 for (l3 = 0; l3 < 512; l3++) {
1543 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1544 pde = le64_to_cpu(pde);
1545 if (pde & PG_PRESENT_MASK) {
1546 if (pde & PG_PSE_MASK) {
1547 /* 2M pages, CR4.PSE is ignored */
1548 print_pte(mon, (l1 << 39) + (l2 << 30) +
1549 (l3 << 21), pde,
1550 0x3ffffffe00000ULL);
1551 } else {
1552 pt_addr = pde & 0x3fffffffff000ULL;
1553 for (l4 = 0; l4 < 512; l4++) {
1554 cpu_physical_memory_read(pt_addr
1555 + l4 * 8,
1556 &pte, 8);
1557 pte = le64_to_cpu(pte);
1558 if (pte & PG_PRESENT_MASK) {
1559 print_pte(mon, (l1 << 39) +
1560 (l2 << 30) +
1561 (l3 << 21) + (l4 << 12),
1562 pte & ~PG_PSE_MASK,
1563 0x3fffffffff000ULL);
1575 #endif
1577 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1579 CPUArchState *env;
1581 env = mon_get_cpu_env();
1583 if (!(env->cr[0] & CR0_PG_MASK)) {
1584 monitor_printf(mon, "PG disabled\n");
1585 return;
1587 if (env->cr[4] & CR4_PAE_MASK) {
1588 #ifdef TARGET_X86_64
1589 if (env->hflags & HF_LMA_MASK) {
1590 tlb_info_64(mon, env);
1591 } else
1592 #endif
1594 tlb_info_pae32(mon, env);
1596 } else {
1597 tlb_info_32(mon, env);
1601 static void mem_print(Monitor *mon, hwaddr *pstart,
1602 int *plast_prot,
1603 hwaddr end, int prot)
1605 int prot1;
1606 prot1 = *plast_prot;
1607 if (prot != prot1) {
1608 if (*pstart != -1) {
1609 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1610 TARGET_FMT_plx " %c%c%c\n",
1611 *pstart, end, end - *pstart,
1612 prot1 & PG_USER_MASK ? 'u' : '-',
1613 'r',
1614 prot1 & PG_RW_MASK ? 'w' : '-');
1616 if (prot != 0)
1617 *pstart = end;
1618 else
1619 *pstart = -1;
1620 *plast_prot = prot;
1624 static void mem_info_32(Monitor *mon, CPUArchState *env)
1626 unsigned int l1, l2;
1627 int prot, last_prot;
1628 uint32_t pgd, pde, pte;
1629 hwaddr start, end;
1631 pgd = env->cr[3] & ~0xfff;
1632 last_prot = 0;
1633 start = -1;
1634 for(l1 = 0; l1 < 1024; l1++) {
1635 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1636 pde = le32_to_cpu(pde);
1637 end = l1 << 22;
1638 if (pde & PG_PRESENT_MASK) {
1639 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1640 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1641 mem_print(mon, &start, &last_prot, end, prot);
1642 } else {
1643 for(l2 = 0; l2 < 1024; l2++) {
1644 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1645 pte = le32_to_cpu(pte);
1646 end = (l1 << 22) + (l2 << 12);
1647 if (pte & PG_PRESENT_MASK) {
1648 prot = pte & pde &
1649 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1650 } else {
1651 prot = 0;
1653 mem_print(mon, &start, &last_prot, end, prot);
1656 } else {
1657 prot = 0;
1658 mem_print(mon, &start, &last_prot, end, prot);
1661 /* Flush last range */
1662 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1665 static void mem_info_pae32(Monitor *mon, CPUArchState *env)
1667 unsigned int l1, l2, l3;
1668 int prot, last_prot;
1669 uint64_t pdpe, pde, pte;
1670 uint64_t pdp_addr, pd_addr, pt_addr;
1671 hwaddr start, end;
1673 pdp_addr = env->cr[3] & ~0x1f;
1674 last_prot = 0;
1675 start = -1;
1676 for (l1 = 0; l1 < 4; l1++) {
1677 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1678 pdpe = le64_to_cpu(pdpe);
1679 end = l1 << 30;
1680 if (pdpe & PG_PRESENT_MASK) {
1681 pd_addr = pdpe & 0x3fffffffff000ULL;
1682 for (l2 = 0; l2 < 512; l2++) {
1683 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1684 pde = le64_to_cpu(pde);
1685 end = (l1 << 30) + (l2 << 21);
1686 if (pde & PG_PRESENT_MASK) {
1687 if (pde & PG_PSE_MASK) {
1688 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1689 PG_PRESENT_MASK);
1690 mem_print(mon, &start, &last_prot, end, prot);
1691 } else {
1692 pt_addr = pde & 0x3fffffffff000ULL;
1693 for (l3 = 0; l3 < 512; l3++) {
1694 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1695 pte = le64_to_cpu(pte);
1696 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1697 if (pte & PG_PRESENT_MASK) {
1698 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1699 PG_PRESENT_MASK);
1700 } else {
1701 prot = 0;
1703 mem_print(mon, &start, &last_prot, end, prot);
1706 } else {
1707 prot = 0;
1708 mem_print(mon, &start, &last_prot, end, prot);
1711 } else {
1712 prot = 0;
1713 mem_print(mon, &start, &last_prot, end, prot);
1716 /* Flush last range */
1717 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1721 #ifdef TARGET_X86_64
1722 static void mem_info_64(Monitor *mon, CPUArchState *env)
1724 int prot, last_prot;
1725 uint64_t l1, l2, l3, l4;
1726 uint64_t pml4e, pdpe, pde, pte;
1727 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1729 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1730 last_prot = 0;
1731 start = -1;
1732 for (l1 = 0; l1 < 512; l1++) {
1733 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1734 pml4e = le64_to_cpu(pml4e);
1735 end = l1 << 39;
1736 if (pml4e & PG_PRESENT_MASK) {
1737 pdp_addr = pml4e & 0x3fffffffff000ULL;
1738 for (l2 = 0; l2 < 512; l2++) {
1739 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1740 pdpe = le64_to_cpu(pdpe);
1741 end = (l1 << 39) + (l2 << 30);
1742 if (pdpe & PG_PRESENT_MASK) {
1743 if (pdpe & PG_PSE_MASK) {
1744 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1745 PG_PRESENT_MASK);
1746 prot &= pml4e;
1747 mem_print(mon, &start, &last_prot, end, prot);
1748 } else {
1749 pd_addr = pdpe & 0x3fffffffff000ULL;
1750 for (l3 = 0; l3 < 512; l3++) {
1751 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1752 pde = le64_to_cpu(pde);
1753 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1754 if (pde & PG_PRESENT_MASK) {
1755 if (pde & PG_PSE_MASK) {
1756 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1757 PG_PRESENT_MASK);
1758 prot &= pml4e & pdpe;
1759 mem_print(mon, &start, &last_prot, end, prot);
1760 } else {
1761 pt_addr = pde & 0x3fffffffff000ULL;
1762 for (l4 = 0; l4 < 512; l4++) {
1763 cpu_physical_memory_read(pt_addr
1764 + l4 * 8,
1765 &pte, 8);
1766 pte = le64_to_cpu(pte);
1767 end = (l1 << 39) + (l2 << 30) +
1768 (l3 << 21) + (l4 << 12);
1769 if (pte & PG_PRESENT_MASK) {
1770 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1771 PG_PRESENT_MASK);
1772 prot &= pml4e & pdpe & pde;
1773 } else {
1774 prot = 0;
1776 mem_print(mon, &start, &last_prot, end, prot);
1779 } else {
1780 prot = 0;
1781 mem_print(mon, &start, &last_prot, end, prot);
1785 } else {
1786 prot = 0;
1787 mem_print(mon, &start, &last_prot, end, prot);
1790 } else {
1791 prot = 0;
1792 mem_print(mon, &start, &last_prot, end, prot);
1795 /* Flush last range */
1796 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
1798 #endif
1800 static void hmp_info_mem(Monitor *mon, const QDict *qdict)
1802 CPUArchState *env;
1804 env = mon_get_cpu_env();
1806 if (!(env->cr[0] & CR0_PG_MASK)) {
1807 monitor_printf(mon, "PG disabled\n");
1808 return;
1810 if (env->cr[4] & CR4_PAE_MASK) {
1811 #ifdef TARGET_X86_64
1812 if (env->hflags & HF_LMA_MASK) {
1813 mem_info_64(mon, env);
1814 } else
1815 #endif
1817 mem_info_pae32(mon, env);
1819 } else {
1820 mem_info_32(mon, env);
1823 #endif
1825 #if defined(TARGET_SH4)
1827 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1829 monitor_printf(mon, " tlb%i:\t"
1830 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1831 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1832 "dirty=%hhu writethrough=%hhu\n",
1833 idx,
1834 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1835 tlb->v, tlb->sh, tlb->c, tlb->pr,
1836 tlb->d, tlb->wt);
1839 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1841 CPUArchState *env = mon_get_cpu_env();
1842 int i;
1844 monitor_printf (mon, "ITLB:\n");
1845 for (i = 0 ; i < ITLB_SIZE ; i++)
1846 print_tlb (mon, i, &env->itlb[i]);
1847 monitor_printf (mon, "UTLB:\n");
1848 for (i = 0 ; i < UTLB_SIZE ; i++)
1849 print_tlb (mon, i, &env->utlb[i]);
1852 #endif
1854 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1855 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1857 CPUArchState *env1 = mon_get_cpu_env();
1859 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1861 #endif
1863 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1865 mtree_info((fprintf_function)monitor_printf, mon);
1868 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1870 int i;
1871 CPUState *cpu;
1872 uint64_t *node_mem;
1874 node_mem = g_new0(uint64_t, nb_numa_nodes);
1875 query_numa_node_mem(node_mem);
1876 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1877 for (i = 0; i < nb_numa_nodes; i++) {
1878 monitor_printf(mon, "node %d cpus:", i);
1879 CPU_FOREACH(cpu) {
1880 if (cpu->numa_node == i) {
1881 monitor_printf(mon, " %d", cpu->cpu_index);
1884 monitor_printf(mon, "\n");
1885 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1886 node_mem[i] >> 20);
1888 g_free(node_mem);
1891 #ifdef CONFIG_PROFILER
1893 int64_t tcg_time;
1894 int64_t dev_time;
1896 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1898 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1899 dev_time, dev_time / (double)get_ticks_per_sec());
1900 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1901 tcg_time, tcg_time / (double)get_ticks_per_sec());
1902 tcg_time = 0;
1903 dev_time = 0;
1905 #else
1906 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1908 monitor_printf(mon, "Internal profiler not compiled\n");
1910 #endif
1912 /* Capture support */
1913 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1915 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1917 int i;
1918 CaptureState *s;
1920 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1921 monitor_printf(mon, "[%d]: ", i);
1922 s->ops.info (s->opaque);
1926 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1928 int i;
1929 int n = qdict_get_int(qdict, "n");
1930 CaptureState *s;
1932 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1933 if (i == n) {
1934 s->ops.destroy (s->opaque);
1935 QLIST_REMOVE (s, entries);
1936 g_free (s);
1937 return;
1942 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1944 const char *path = qdict_get_str(qdict, "path");
1945 int has_freq = qdict_haskey(qdict, "freq");
1946 int freq = qdict_get_try_int(qdict, "freq", -1);
1947 int has_bits = qdict_haskey(qdict, "bits");
1948 int bits = qdict_get_try_int(qdict, "bits", -1);
1949 int has_channels = qdict_haskey(qdict, "nchannels");
1950 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1951 CaptureState *s;
1953 s = g_malloc0 (sizeof (*s));
1955 freq = has_freq ? freq : 44100;
1956 bits = has_bits ? bits : 16;
1957 nchannels = has_channels ? nchannels : 2;
1959 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1960 monitor_printf(mon, "Failed to add wave capture\n");
1961 g_free (s);
1962 return;
1964 QLIST_INSERT_HEAD (&capture_head, s, entries);
1967 static qemu_acl *find_acl(Monitor *mon, const char *name)
1969 qemu_acl *acl = qemu_acl_find(name);
1971 if (!acl) {
1972 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1974 return acl;
1977 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1979 const char *aclname = qdict_get_str(qdict, "aclname");
1980 qemu_acl *acl = find_acl(mon, aclname);
1981 qemu_acl_entry *entry;
1982 int i = 0;
1984 if (acl) {
1985 monitor_printf(mon, "policy: %s\n",
1986 acl->defaultDeny ? "deny" : "allow");
1987 QTAILQ_FOREACH(entry, &acl->entries, next) {
1988 i++;
1989 monitor_printf(mon, "%d: %s %s\n", i,
1990 entry->deny ? "deny" : "allow", entry->match);
1995 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1997 const char *aclname = qdict_get_str(qdict, "aclname");
1998 qemu_acl *acl = find_acl(mon, aclname);
2000 if (acl) {
2001 qemu_acl_reset(acl);
2002 monitor_printf(mon, "acl: removed all rules\n");
2006 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
2008 const char *aclname = qdict_get_str(qdict, "aclname");
2009 const char *policy = qdict_get_str(qdict, "policy");
2010 qemu_acl *acl = find_acl(mon, aclname);
2012 if (acl) {
2013 if (strcmp(policy, "allow") == 0) {
2014 acl->defaultDeny = 0;
2015 monitor_printf(mon, "acl: policy set to 'allow'\n");
2016 } else if (strcmp(policy, "deny") == 0) {
2017 acl->defaultDeny = 1;
2018 monitor_printf(mon, "acl: policy set to 'deny'\n");
2019 } else {
2020 monitor_printf(mon, "acl: unknown policy '%s', "
2021 "expected 'deny' or 'allow'\n", policy);
2026 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
2028 const char *aclname = qdict_get_str(qdict, "aclname");
2029 const char *match = qdict_get_str(qdict, "match");
2030 const char *policy = qdict_get_str(qdict, "policy");
2031 int has_index = qdict_haskey(qdict, "index");
2032 int index = qdict_get_try_int(qdict, "index", -1);
2033 qemu_acl *acl = find_acl(mon, aclname);
2034 int deny, ret;
2036 if (acl) {
2037 if (strcmp(policy, "allow") == 0) {
2038 deny = 0;
2039 } else if (strcmp(policy, "deny") == 0) {
2040 deny = 1;
2041 } else {
2042 monitor_printf(mon, "acl: unknown policy '%s', "
2043 "expected 'deny' or 'allow'\n", policy);
2044 return;
2046 if (has_index)
2047 ret = qemu_acl_insert(acl, deny, match, index);
2048 else
2049 ret = qemu_acl_append(acl, deny, match);
2050 if (ret < 0)
2051 monitor_printf(mon, "acl: unable to add acl entry\n");
2052 else
2053 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2057 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
2059 const char *aclname = qdict_get_str(qdict, "aclname");
2060 const char *match = qdict_get_str(qdict, "match");
2061 qemu_acl *acl = find_acl(mon, aclname);
2062 int ret;
2064 if (acl) {
2065 ret = qemu_acl_remove(acl, match);
2066 if (ret < 0)
2067 monitor_printf(mon, "acl: no matching acl entry\n");
2068 else
2069 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2073 #if defined(TARGET_I386)
2074 static void hmp_mce(Monitor *mon, const QDict *qdict)
2076 X86CPU *cpu;
2077 CPUState *cs;
2078 int cpu_index = qdict_get_int(qdict, "cpu_index");
2079 int bank = qdict_get_int(qdict, "bank");
2080 uint64_t status = qdict_get_int(qdict, "status");
2081 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2082 uint64_t addr = qdict_get_int(qdict, "addr");
2083 uint64_t misc = qdict_get_int(qdict, "misc");
2084 int flags = MCE_INJECT_UNCOND_AO;
2086 if (qdict_get_try_bool(qdict, "broadcast", false)) {
2087 flags |= MCE_INJECT_BROADCAST;
2089 cs = qemu_get_cpu(cpu_index);
2090 if (cs != NULL) {
2091 cpu = X86_CPU(cs);
2092 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2093 flags);
2096 #endif
2098 void qmp_getfd(const char *fdname, Error **errp)
2100 mon_fd_t *monfd;
2101 int fd;
2103 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
2104 if (fd == -1) {
2105 error_setg(errp, QERR_FD_NOT_SUPPLIED);
2106 return;
2109 if (qemu_isdigit(fdname[0])) {
2110 close(fd);
2111 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2112 "a name not starting with a digit");
2113 return;
2116 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2117 if (strcmp(monfd->name, fdname) != 0) {
2118 continue;
2121 close(monfd->fd);
2122 monfd->fd = fd;
2123 return;
2126 monfd = g_malloc0(sizeof(mon_fd_t));
2127 monfd->name = g_strdup(fdname);
2128 monfd->fd = fd;
2130 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
2133 void qmp_closefd(const char *fdname, Error **errp)
2135 mon_fd_t *monfd;
2137 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2138 if (strcmp(monfd->name, fdname) != 0) {
2139 continue;
2142 QLIST_REMOVE(monfd, next);
2143 close(monfd->fd);
2144 g_free(monfd->name);
2145 g_free(monfd);
2146 return;
2149 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
2152 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
2154 int saved_vm_running = runstate_is_running();
2155 const char *name = qdict_get_str(qdict, "name");
2157 vm_stop(RUN_STATE_RESTORE_VM);
2159 if (load_vmstate(name) == 0 && saved_vm_running) {
2160 vm_start();
2164 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
2166 mon_fd_t *monfd;
2168 QLIST_FOREACH(monfd, &mon->fds, next) {
2169 int fd;
2171 if (strcmp(monfd->name, fdname) != 0) {
2172 continue;
2175 fd = monfd->fd;
2177 /* caller takes ownership of fd */
2178 QLIST_REMOVE(monfd, next);
2179 g_free(monfd->name);
2180 g_free(monfd);
2182 return fd;
2185 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
2186 return -1;
2189 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2191 MonFdsetFd *mon_fdset_fd;
2192 MonFdsetFd *mon_fdset_fd_next;
2194 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2195 if ((mon_fdset_fd->removed ||
2196 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2197 runstate_is_running()) {
2198 close(mon_fdset_fd->fd);
2199 g_free(mon_fdset_fd->opaque);
2200 QLIST_REMOVE(mon_fdset_fd, next);
2201 g_free(mon_fdset_fd);
2205 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
2206 QLIST_REMOVE(mon_fdset, next);
2207 g_free(mon_fdset);
2211 static void monitor_fdsets_cleanup(void)
2213 MonFdset *mon_fdset;
2214 MonFdset *mon_fdset_next;
2216 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2217 monitor_fdset_cleanup(mon_fdset);
2221 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2222 const char *opaque, Error **errp)
2224 int fd;
2225 Monitor *mon = cur_mon;
2226 AddfdInfo *fdinfo;
2228 fd = qemu_chr_fe_get_msgfd(mon->chr);
2229 if (fd == -1) {
2230 error_setg(errp, QERR_FD_NOT_SUPPLIED);
2231 goto error;
2234 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2235 has_opaque, opaque, errp);
2236 if (fdinfo) {
2237 return fdinfo;
2240 error:
2241 if (fd != -1) {
2242 close(fd);
2244 return NULL;
2247 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2249 MonFdset *mon_fdset;
2250 MonFdsetFd *mon_fdset_fd;
2251 char fd_str[60];
2253 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2254 if (mon_fdset->id != fdset_id) {
2255 continue;
2257 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2258 if (has_fd) {
2259 if (mon_fdset_fd->fd != fd) {
2260 continue;
2262 mon_fdset_fd->removed = true;
2263 break;
2264 } else {
2265 mon_fdset_fd->removed = true;
2268 if (has_fd && !mon_fdset_fd) {
2269 goto error;
2271 monitor_fdset_cleanup(mon_fdset);
2272 return;
2275 error:
2276 if (has_fd) {
2277 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2278 fdset_id, fd);
2279 } else {
2280 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2282 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
2285 FdsetInfoList *qmp_query_fdsets(Error **errp)
2287 MonFdset *mon_fdset;
2288 MonFdsetFd *mon_fdset_fd;
2289 FdsetInfoList *fdset_list = NULL;
2291 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2292 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2293 FdsetFdInfoList *fdsetfd_list = NULL;
2295 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2296 fdset_info->value->fdset_id = mon_fdset->id;
2298 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2299 FdsetFdInfoList *fdsetfd_info;
2301 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2302 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2303 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2304 if (mon_fdset_fd->opaque) {
2305 fdsetfd_info->value->has_opaque = true;
2306 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2307 } else {
2308 fdsetfd_info->value->has_opaque = false;
2311 fdsetfd_info->next = fdsetfd_list;
2312 fdsetfd_list = fdsetfd_info;
2315 fdset_info->value->fds = fdsetfd_list;
2317 fdset_info->next = fdset_list;
2318 fdset_list = fdset_info;
2321 return fdset_list;
2324 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2325 bool has_opaque, const char *opaque,
2326 Error **errp)
2328 MonFdset *mon_fdset = NULL;
2329 MonFdsetFd *mon_fdset_fd;
2330 AddfdInfo *fdinfo;
2332 if (has_fdset_id) {
2333 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2334 /* Break if match found or match impossible due to ordering by ID */
2335 if (fdset_id <= mon_fdset->id) {
2336 if (fdset_id < mon_fdset->id) {
2337 mon_fdset = NULL;
2339 break;
2344 if (mon_fdset == NULL) {
2345 int64_t fdset_id_prev = -1;
2346 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2348 if (has_fdset_id) {
2349 if (fdset_id < 0) {
2350 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2351 "a non-negative value");
2352 return NULL;
2354 /* Use specified fdset ID */
2355 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2356 mon_fdset_cur = mon_fdset;
2357 if (fdset_id < mon_fdset_cur->id) {
2358 break;
2361 } else {
2362 /* Use first available fdset ID */
2363 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2364 mon_fdset_cur = mon_fdset;
2365 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2366 fdset_id_prev = mon_fdset_cur->id;
2367 continue;
2369 break;
2373 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2374 if (has_fdset_id) {
2375 mon_fdset->id = fdset_id;
2376 } else {
2377 mon_fdset->id = fdset_id_prev + 1;
2380 /* The fdset list is ordered by fdset ID */
2381 if (!mon_fdset_cur) {
2382 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2383 } else if (mon_fdset->id < mon_fdset_cur->id) {
2384 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2385 } else {
2386 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2390 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2391 mon_fdset_fd->fd = fd;
2392 mon_fdset_fd->removed = false;
2393 if (has_opaque) {
2394 mon_fdset_fd->opaque = g_strdup(opaque);
2396 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2398 fdinfo = g_malloc0(sizeof(*fdinfo));
2399 fdinfo->fdset_id = mon_fdset->id;
2400 fdinfo->fd = mon_fdset_fd->fd;
2402 return fdinfo;
2405 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2407 #ifndef _WIN32
2408 MonFdset *mon_fdset;
2409 MonFdsetFd *mon_fdset_fd;
2410 int mon_fd_flags;
2412 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2413 if (mon_fdset->id != fdset_id) {
2414 continue;
2416 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2417 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2418 if (mon_fd_flags == -1) {
2419 return -1;
2422 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2423 return mon_fdset_fd->fd;
2426 errno = EACCES;
2427 return -1;
2429 #endif
2431 errno = ENOENT;
2432 return -1;
2435 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2437 MonFdset *mon_fdset;
2438 MonFdsetFd *mon_fdset_fd_dup;
2440 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2441 if (mon_fdset->id != fdset_id) {
2442 continue;
2444 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2445 if (mon_fdset_fd_dup->fd == dup_fd) {
2446 return -1;
2449 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2450 mon_fdset_fd_dup->fd = dup_fd;
2451 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2452 return 0;
2454 return -1;
2457 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2459 MonFdset *mon_fdset;
2460 MonFdsetFd *mon_fdset_fd_dup;
2462 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2463 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2464 if (mon_fdset_fd_dup->fd == dup_fd) {
2465 if (remove) {
2466 QLIST_REMOVE(mon_fdset_fd_dup, next);
2467 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2468 monitor_fdset_cleanup(mon_fdset);
2470 return -1;
2471 } else {
2472 return mon_fdset->id;
2477 return -1;
2480 int monitor_fdset_dup_fd_find(int dup_fd)
2482 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2485 void monitor_fdset_dup_fd_remove(int dup_fd)
2487 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2490 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2492 int fd;
2493 Error *local_err = NULL;
2495 if (!qemu_isdigit(fdname[0]) && mon) {
2496 fd = monitor_get_fd(mon, fdname, &local_err);
2497 } else {
2498 fd = qemu_parse_fd(fdname);
2499 if (fd == -1) {
2500 error_setg(&local_err, "Invalid file descriptor number '%s'",
2501 fdname);
2504 if (local_err) {
2505 error_propagate(errp, local_err);
2506 assert(fd == -1);
2507 } else {
2508 assert(fd != -1);
2511 return fd;
2514 /* Please update hmp-commands.hx when adding or changing commands */
2515 static mon_cmd_t info_cmds[] = {
2517 .name = "version",
2518 .args_type = "",
2519 .params = "",
2520 .help = "show the version of QEMU",
2521 .mhandler.cmd = hmp_info_version,
2524 .name = "network",
2525 .args_type = "",
2526 .params = "",
2527 .help = "show the network state",
2528 .mhandler.cmd = hmp_info_network,
2531 .name = "chardev",
2532 .args_type = "",
2533 .params = "",
2534 .help = "show the character devices",
2535 .mhandler.cmd = hmp_info_chardev,
2538 .name = "block",
2539 .args_type = "nodes:-n,verbose:-v,device:B?",
2540 .params = "[-n] [-v] [device]",
2541 .help = "show info of one block device or all block devices "
2542 "(-n: show named nodes; -v: show details)",
2543 .mhandler.cmd = hmp_info_block,
2546 .name = "blockstats",
2547 .args_type = "",
2548 .params = "",
2549 .help = "show block device statistics",
2550 .mhandler.cmd = hmp_info_blockstats,
2553 .name = "block-jobs",
2554 .args_type = "",
2555 .params = "",
2556 .help = "show progress of ongoing block device operations",
2557 .mhandler.cmd = hmp_info_block_jobs,
2560 .name = "registers",
2561 .args_type = "",
2562 .params = "",
2563 .help = "show the cpu registers",
2564 .mhandler.cmd = hmp_info_registers,
2567 .name = "cpus",
2568 .args_type = "",
2569 .params = "",
2570 .help = "show infos for each CPU",
2571 .mhandler.cmd = hmp_info_cpus,
2574 .name = "history",
2575 .args_type = "",
2576 .params = "",
2577 .help = "show the command line history",
2578 .mhandler.cmd = hmp_info_history,
2580 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2581 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2583 .name = "irq",
2584 .args_type = "",
2585 .params = "",
2586 .help = "show the interrupts statistics (if available)",
2587 #ifdef TARGET_SPARC
2588 .mhandler.cmd = sun4m_hmp_info_irq,
2589 #elif defined(TARGET_LM32)
2590 .mhandler.cmd = lm32_hmp_info_irq,
2591 #else
2592 .mhandler.cmd = hmp_info_irq,
2593 #endif
2596 .name = "pic",
2597 .args_type = "",
2598 .params = "",
2599 .help = "show i8259 (PIC) state",
2600 #ifdef TARGET_SPARC
2601 .mhandler.cmd = sun4m_hmp_info_pic,
2602 #elif defined(TARGET_LM32)
2603 .mhandler.cmd = lm32_hmp_info_pic,
2604 #else
2605 .mhandler.cmd = hmp_info_pic,
2606 #endif
2608 #endif
2610 .name = "pci",
2611 .args_type = "",
2612 .params = "",
2613 .help = "show PCI info",
2614 .mhandler.cmd = hmp_info_pci,
2616 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2617 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2619 .name = "tlb",
2620 .args_type = "",
2621 .params = "",
2622 .help = "show virtual to physical memory mappings",
2623 .mhandler.cmd = hmp_info_tlb,
2625 #endif
2626 #if defined(TARGET_I386)
2628 .name = "mem",
2629 .args_type = "",
2630 .params = "",
2631 .help = "show the active virtual memory mappings",
2632 .mhandler.cmd = hmp_info_mem,
2634 #endif
2636 .name = "mtree",
2637 .args_type = "",
2638 .params = "",
2639 .help = "show memory tree",
2640 .mhandler.cmd = hmp_info_mtree,
2643 .name = "jit",
2644 .args_type = "",
2645 .params = "",
2646 .help = "show dynamic compiler info",
2647 .mhandler.cmd = hmp_info_jit,
2650 .name = "opcount",
2651 .args_type = "",
2652 .params = "",
2653 .help = "show dynamic compiler opcode counters",
2654 .mhandler.cmd = hmp_info_opcount,
2657 .name = "kvm",
2658 .args_type = "",
2659 .params = "",
2660 .help = "show KVM information",
2661 .mhandler.cmd = hmp_info_kvm,
2664 .name = "numa",
2665 .args_type = "",
2666 .params = "",
2667 .help = "show NUMA information",
2668 .mhandler.cmd = hmp_info_numa,
2671 .name = "usb",
2672 .args_type = "",
2673 .params = "",
2674 .help = "show guest USB devices",
2675 .mhandler.cmd = hmp_info_usb,
2678 .name = "usbhost",
2679 .args_type = "",
2680 .params = "",
2681 .help = "show host USB devices",
2682 .mhandler.cmd = hmp_info_usbhost,
2685 .name = "profile",
2686 .args_type = "",
2687 .params = "",
2688 .help = "show profiling information",
2689 .mhandler.cmd = hmp_info_profile,
2692 .name = "capture",
2693 .args_type = "",
2694 .params = "",
2695 .help = "show capture information",
2696 .mhandler.cmd = hmp_info_capture,
2699 .name = "snapshots",
2700 .args_type = "",
2701 .params = "",
2702 .help = "show the currently saved VM snapshots",
2703 .mhandler.cmd = hmp_info_snapshots,
2706 .name = "status",
2707 .args_type = "",
2708 .params = "",
2709 .help = "show the current VM status (running|paused)",
2710 .mhandler.cmd = hmp_info_status,
2713 .name = "mice",
2714 .args_type = "",
2715 .params = "",
2716 .help = "show which guest mouse is receiving events",
2717 .mhandler.cmd = hmp_info_mice,
2720 .name = "vnc",
2721 .args_type = "",
2722 .params = "",
2723 .help = "show the vnc server status",
2724 .mhandler.cmd = hmp_info_vnc,
2726 #if defined(CONFIG_SPICE)
2728 .name = "spice",
2729 .args_type = "",
2730 .params = "",
2731 .help = "show the spice server status",
2732 .mhandler.cmd = hmp_info_spice,
2734 #endif
2736 .name = "name",
2737 .args_type = "",
2738 .params = "",
2739 .help = "show the current VM name",
2740 .mhandler.cmd = hmp_info_name,
2743 .name = "uuid",
2744 .args_type = "",
2745 .params = "",
2746 .help = "show the current VM UUID",
2747 .mhandler.cmd = hmp_info_uuid,
2750 .name = "cpustats",
2751 .args_type = "",
2752 .params = "",
2753 .help = "show CPU statistics",
2754 .mhandler.cmd = hmp_info_cpustats,
2756 #if defined(CONFIG_SLIRP)
2758 .name = "usernet",
2759 .args_type = "",
2760 .params = "",
2761 .help = "show user network stack connection states",
2762 .mhandler.cmd = hmp_info_usernet,
2764 #endif
2766 .name = "migrate",
2767 .args_type = "",
2768 .params = "",
2769 .help = "show migration status",
2770 .mhandler.cmd = hmp_info_migrate,
2773 .name = "migrate_capabilities",
2774 .args_type = "",
2775 .params = "",
2776 .help = "show current migration capabilities",
2777 .mhandler.cmd = hmp_info_migrate_capabilities,
2780 .name = "migrate_parameters",
2781 .args_type = "",
2782 .params = "",
2783 .help = "show current migration parameters",
2784 .mhandler.cmd = hmp_info_migrate_parameters,
2787 .name = "migrate_cache_size",
2788 .args_type = "",
2789 .params = "",
2790 .help = "show current migration xbzrle cache size",
2791 .mhandler.cmd = hmp_info_migrate_cache_size,
2794 .name = "balloon",
2795 .args_type = "",
2796 .params = "",
2797 .help = "show balloon information",
2798 .mhandler.cmd = hmp_info_balloon,
2801 .name = "qtree",
2802 .args_type = "",
2803 .params = "",
2804 .help = "show device tree",
2805 .mhandler.cmd = hmp_info_qtree,
2808 .name = "qdm",
2809 .args_type = "",
2810 .params = "",
2811 .help = "show qdev device model list",
2812 .mhandler.cmd = hmp_info_qdm,
2815 .name = "qom-tree",
2816 .args_type = "path:s?",
2817 .params = "[path]",
2818 .help = "show QOM composition tree",
2819 .mhandler.cmd = hmp_info_qom_tree,
2822 .name = "roms",
2823 .args_type = "",
2824 .params = "",
2825 .help = "show roms",
2826 .mhandler.cmd = hmp_info_roms,
2829 .name = "trace-events",
2830 .args_type = "",
2831 .params = "",
2832 .help = "show available trace-events & their state",
2833 .mhandler.cmd = hmp_info_trace_events,
2836 .name = "tpm",
2837 .args_type = "",
2838 .params = "",
2839 .help = "show the TPM device",
2840 .mhandler.cmd = hmp_info_tpm,
2843 .name = "memdev",
2844 .args_type = "",
2845 .params = "",
2846 .help = "show memory backends",
2847 .mhandler.cmd = hmp_info_memdev,
2850 .name = "memory-devices",
2851 .args_type = "",
2852 .params = "",
2853 .help = "show memory devices",
2854 .mhandler.cmd = hmp_info_memory_devices,
2857 .name = "rocker",
2858 .args_type = "name:s",
2859 .params = "name",
2860 .help = "Show rocker switch",
2861 .mhandler.cmd = hmp_rocker,
2864 .name = "rocker-ports",
2865 .args_type = "name:s",
2866 .params = "name",
2867 .help = "Show rocker ports",
2868 .mhandler.cmd = hmp_rocker_ports,
2871 .name = "rocker-of-dpa-flows",
2872 .args_type = "name:s,tbl_id:i?",
2873 .params = "name [tbl_id]",
2874 .help = "Show rocker OF-DPA flow tables",
2875 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2878 .name = "rocker-of-dpa-groups",
2879 .args_type = "name:s,type:i?",
2880 .params = "name [type]",
2881 .help = "Show rocker OF-DPA groups",
2882 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2884 #if defined(TARGET_S390X)
2886 .name = "skeys",
2887 .args_type = "addr:l",
2888 .params = "address",
2889 .help = "Display the value of a storage key",
2890 .mhandler.cmd = hmp_info_skeys,
2892 #endif
2894 .name = NULL,
2898 /* mon_cmds and info_cmds would be sorted at runtime */
2899 static mon_cmd_t mon_cmds[] = {
2900 #include "hmp-commands.h"
2901 { NULL, NULL, },
2904 static const mon_cmd_t qmp_cmds[] = {
2905 #include "qmp-commands-old.h"
2906 { /* NULL */ },
2909 /*******************************************************************/
2911 static const char *pch;
2912 static sigjmp_buf expr_env;
2914 #define MD_TLONG 0
2915 #define MD_I32 1
2917 typedef struct MonitorDef {
2918 const char *name;
2919 int offset;
2920 target_long (*get_value)(const struct MonitorDef *md, int val);
2921 int type;
2922 } MonitorDef;
2924 #if defined(TARGET_I386)
2925 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2927 CPUArchState *env = mon_get_cpu_env();
2928 return env->eip + env->segs[R_CS].base;
2930 #endif
2932 #if defined(TARGET_PPC)
2933 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2935 CPUArchState *env = mon_get_cpu_env();
2936 unsigned int u;
2937 int i;
2939 u = 0;
2940 for (i = 0; i < 8; i++)
2941 u |= env->crf[i] << (32 - (4 * (i + 1)));
2943 return u;
2946 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2948 CPUArchState *env = mon_get_cpu_env();
2949 return env->msr;
2952 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2954 CPUArchState *env = mon_get_cpu_env();
2955 return env->xer;
2958 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2960 CPUArchState *env = mon_get_cpu_env();
2961 return cpu_ppc_load_decr(env);
2964 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2966 CPUArchState *env = mon_get_cpu_env();
2967 return cpu_ppc_load_tbu(env);
2970 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2972 CPUArchState *env = mon_get_cpu_env();
2973 return cpu_ppc_load_tbl(env);
2975 #endif
2977 #if defined(TARGET_SPARC)
2978 #ifndef TARGET_SPARC64
2979 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2981 CPUArchState *env = mon_get_cpu_env();
2983 return cpu_get_psr(env);
2985 #endif
2987 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2989 CPUArchState *env = mon_get_cpu_env();
2990 return env->regwptr[val];
2992 #endif
2994 static const MonitorDef monitor_defs[] = {
2995 #ifdef TARGET_I386
2997 #define SEG(name, seg) \
2998 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2999 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3000 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
3002 { "eax", offsetof(CPUX86State, regs[0]) },
3003 { "ecx", offsetof(CPUX86State, regs[1]) },
3004 { "edx", offsetof(CPUX86State, regs[2]) },
3005 { "ebx", offsetof(CPUX86State, regs[3]) },
3006 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3007 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3008 { "esi", offsetof(CPUX86State, regs[6]) },
3009 { "edi", offsetof(CPUX86State, regs[7]) },
3010 #ifdef TARGET_X86_64
3011 { "r8", offsetof(CPUX86State, regs[8]) },
3012 { "r9", offsetof(CPUX86State, regs[9]) },
3013 { "r10", offsetof(CPUX86State, regs[10]) },
3014 { "r11", offsetof(CPUX86State, regs[11]) },
3015 { "r12", offsetof(CPUX86State, regs[12]) },
3016 { "r13", offsetof(CPUX86State, regs[13]) },
3017 { "r14", offsetof(CPUX86State, regs[14]) },
3018 { "r15", offsetof(CPUX86State, regs[15]) },
3019 #endif
3020 { "eflags", offsetof(CPUX86State, eflags) },
3021 { "eip", offsetof(CPUX86State, eip) },
3022 SEG("cs", R_CS)
3023 SEG("ds", R_DS)
3024 SEG("es", R_ES)
3025 SEG("ss", R_SS)
3026 SEG("fs", R_FS)
3027 SEG("gs", R_GS)
3028 { "pc", 0, monitor_get_pc, },
3029 #elif defined(TARGET_PPC)
3030 /* General purpose registers */
3031 { "r0", offsetof(CPUPPCState, gpr[0]) },
3032 { "r1", offsetof(CPUPPCState, gpr[1]) },
3033 { "r2", offsetof(CPUPPCState, gpr[2]) },
3034 { "r3", offsetof(CPUPPCState, gpr[3]) },
3035 { "r4", offsetof(CPUPPCState, gpr[4]) },
3036 { "r5", offsetof(CPUPPCState, gpr[5]) },
3037 { "r6", offsetof(CPUPPCState, gpr[6]) },
3038 { "r7", offsetof(CPUPPCState, gpr[7]) },
3039 { "r8", offsetof(CPUPPCState, gpr[8]) },
3040 { "r9", offsetof(CPUPPCState, gpr[9]) },
3041 { "r10", offsetof(CPUPPCState, gpr[10]) },
3042 { "r11", offsetof(CPUPPCState, gpr[11]) },
3043 { "r12", offsetof(CPUPPCState, gpr[12]) },
3044 { "r13", offsetof(CPUPPCState, gpr[13]) },
3045 { "r14", offsetof(CPUPPCState, gpr[14]) },
3046 { "r15", offsetof(CPUPPCState, gpr[15]) },
3047 { "r16", offsetof(CPUPPCState, gpr[16]) },
3048 { "r17", offsetof(CPUPPCState, gpr[17]) },
3049 { "r18", offsetof(CPUPPCState, gpr[18]) },
3050 { "r19", offsetof(CPUPPCState, gpr[19]) },
3051 { "r20", offsetof(CPUPPCState, gpr[20]) },
3052 { "r21", offsetof(CPUPPCState, gpr[21]) },
3053 { "r22", offsetof(CPUPPCState, gpr[22]) },
3054 { "r23", offsetof(CPUPPCState, gpr[23]) },
3055 { "r24", offsetof(CPUPPCState, gpr[24]) },
3056 { "r25", offsetof(CPUPPCState, gpr[25]) },
3057 { "r26", offsetof(CPUPPCState, gpr[26]) },
3058 { "r27", offsetof(CPUPPCState, gpr[27]) },
3059 { "r28", offsetof(CPUPPCState, gpr[28]) },
3060 { "r29", offsetof(CPUPPCState, gpr[29]) },
3061 { "r30", offsetof(CPUPPCState, gpr[30]) },
3062 { "r31", offsetof(CPUPPCState, gpr[31]) },
3063 /* Floating point registers */
3064 { "f0", offsetof(CPUPPCState, fpr[0]) },
3065 { "f1", offsetof(CPUPPCState, fpr[1]) },
3066 { "f2", offsetof(CPUPPCState, fpr[2]) },
3067 { "f3", offsetof(CPUPPCState, fpr[3]) },
3068 { "f4", offsetof(CPUPPCState, fpr[4]) },
3069 { "f5", offsetof(CPUPPCState, fpr[5]) },
3070 { "f6", offsetof(CPUPPCState, fpr[6]) },
3071 { "f7", offsetof(CPUPPCState, fpr[7]) },
3072 { "f8", offsetof(CPUPPCState, fpr[8]) },
3073 { "f9", offsetof(CPUPPCState, fpr[9]) },
3074 { "f10", offsetof(CPUPPCState, fpr[10]) },
3075 { "f11", offsetof(CPUPPCState, fpr[11]) },
3076 { "f12", offsetof(CPUPPCState, fpr[12]) },
3077 { "f13", offsetof(CPUPPCState, fpr[13]) },
3078 { "f14", offsetof(CPUPPCState, fpr[14]) },
3079 { "f15", offsetof(CPUPPCState, fpr[15]) },
3080 { "f16", offsetof(CPUPPCState, fpr[16]) },
3081 { "f17", offsetof(CPUPPCState, fpr[17]) },
3082 { "f18", offsetof(CPUPPCState, fpr[18]) },
3083 { "f19", offsetof(CPUPPCState, fpr[19]) },
3084 { "f20", offsetof(CPUPPCState, fpr[20]) },
3085 { "f21", offsetof(CPUPPCState, fpr[21]) },
3086 { "f22", offsetof(CPUPPCState, fpr[22]) },
3087 { "f23", offsetof(CPUPPCState, fpr[23]) },
3088 { "f24", offsetof(CPUPPCState, fpr[24]) },
3089 { "f25", offsetof(CPUPPCState, fpr[25]) },
3090 { "f26", offsetof(CPUPPCState, fpr[26]) },
3091 { "f27", offsetof(CPUPPCState, fpr[27]) },
3092 { "f28", offsetof(CPUPPCState, fpr[28]) },
3093 { "f29", offsetof(CPUPPCState, fpr[29]) },
3094 { "f30", offsetof(CPUPPCState, fpr[30]) },
3095 { "f31", offsetof(CPUPPCState, fpr[31]) },
3096 { "fpscr", offsetof(CPUPPCState, fpscr) },
3097 /* Next instruction pointer */
3098 { "nip|pc", offsetof(CPUPPCState, nip) },
3099 { "lr", offsetof(CPUPPCState, lr) },
3100 { "ctr", offsetof(CPUPPCState, ctr) },
3101 { "decr", 0, &monitor_get_decr, },
3102 { "ccr", 0, &monitor_get_ccr, },
3103 /* Machine state register */
3104 { "msr", 0, &monitor_get_msr, },
3105 { "xer", 0, &monitor_get_xer, },
3106 { "tbu", 0, &monitor_get_tbu, },
3107 { "tbl", 0, &monitor_get_tbl, },
3108 /* Segment registers */
3109 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3110 { "sr0", offsetof(CPUPPCState, sr[0]) },
3111 { "sr1", offsetof(CPUPPCState, sr[1]) },
3112 { "sr2", offsetof(CPUPPCState, sr[2]) },
3113 { "sr3", offsetof(CPUPPCState, sr[3]) },
3114 { "sr4", offsetof(CPUPPCState, sr[4]) },
3115 { "sr5", offsetof(CPUPPCState, sr[5]) },
3116 { "sr6", offsetof(CPUPPCState, sr[6]) },
3117 { "sr7", offsetof(CPUPPCState, sr[7]) },
3118 { "sr8", offsetof(CPUPPCState, sr[8]) },
3119 { "sr9", offsetof(CPUPPCState, sr[9]) },
3120 { "sr10", offsetof(CPUPPCState, sr[10]) },
3121 { "sr11", offsetof(CPUPPCState, sr[11]) },
3122 { "sr12", offsetof(CPUPPCState, sr[12]) },
3123 { "sr13", offsetof(CPUPPCState, sr[13]) },
3124 { "sr14", offsetof(CPUPPCState, sr[14]) },
3125 { "sr15", offsetof(CPUPPCState, sr[15]) },
3126 /* Too lazy to put BATs... */
3127 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
3129 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3130 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3131 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3132 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3133 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
3134 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3135 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3136 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3137 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3138 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3139 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3140 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3141 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3142 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3143 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3144 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3145 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3146 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3147 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3148 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3149 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3150 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3151 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3152 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3153 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3154 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3155 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3156 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3157 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3158 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3159 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3160 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3161 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3162 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3163 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3164 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3165 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3166 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3167 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3168 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3169 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3170 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3171 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3172 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3173 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3174 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3175 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3176 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3177 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3178 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3179 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3180 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3181 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3182 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3183 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3184 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3185 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3186 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3187 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3188 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3189 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3190 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3191 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3192 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3193 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3194 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3195 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3196 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3197 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
3199 #elif defined(TARGET_SPARC)
3200 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3201 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3202 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3203 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3204 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3205 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3206 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3207 { "g7", offsetof(CPUSPARCState, gregs[7]) },
3208 { "o0", 0, monitor_get_reg },
3209 { "o1", 1, monitor_get_reg },
3210 { "o2", 2, monitor_get_reg },
3211 { "o3", 3, monitor_get_reg },
3212 { "o4", 4, monitor_get_reg },
3213 { "o5", 5, monitor_get_reg },
3214 { "o6", 6, monitor_get_reg },
3215 { "o7", 7, monitor_get_reg },
3216 { "l0", 8, monitor_get_reg },
3217 { "l1", 9, monitor_get_reg },
3218 { "l2", 10, monitor_get_reg },
3219 { "l3", 11, monitor_get_reg },
3220 { "l4", 12, monitor_get_reg },
3221 { "l5", 13, monitor_get_reg },
3222 { "l6", 14, monitor_get_reg },
3223 { "l7", 15, monitor_get_reg },
3224 { "i0", 16, monitor_get_reg },
3225 { "i1", 17, monitor_get_reg },
3226 { "i2", 18, monitor_get_reg },
3227 { "i3", 19, monitor_get_reg },
3228 { "i4", 20, monitor_get_reg },
3229 { "i5", 21, monitor_get_reg },
3230 { "i6", 22, monitor_get_reg },
3231 { "i7", 23, monitor_get_reg },
3232 { "pc", offsetof(CPUSPARCState, pc) },
3233 { "npc", offsetof(CPUSPARCState, npc) },
3234 { "y", offsetof(CPUSPARCState, y) },
3235 #ifndef TARGET_SPARC64
3236 { "psr", 0, &monitor_get_psr, },
3237 { "wim", offsetof(CPUSPARCState, wim) },
3238 #endif
3239 { "tbr", offsetof(CPUSPARCState, tbr) },
3240 { "fsr", offsetof(CPUSPARCState, fsr) },
3241 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3242 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3243 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3244 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3245 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3246 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3247 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3248 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3249 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3250 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3251 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3252 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3253 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3254 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3255 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3256 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3257 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3258 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3259 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3260 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3261 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3262 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3263 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3264 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3265 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3266 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3267 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3268 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3269 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3270 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3271 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3272 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
3273 #ifdef TARGET_SPARC64
3274 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3275 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3276 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3277 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3278 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3279 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3280 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3281 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3282 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3283 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3284 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3285 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3286 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3287 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3288 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3289 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3290 { "asi", offsetof(CPUSPARCState, asi) },
3291 { "pstate", offsetof(CPUSPARCState, pstate) },
3292 { "cansave", offsetof(CPUSPARCState, cansave) },
3293 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3294 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3295 { "wstate", offsetof(CPUSPARCState, wstate) },
3296 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3297 { "fprs", offsetof(CPUSPARCState, fprs) },
3298 #endif
3299 #endif
3300 { NULL },
3303 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3304 expr_error(Monitor *mon, const char *fmt, ...)
3306 va_list ap;
3307 va_start(ap, fmt);
3308 monitor_vprintf(mon, fmt, ap);
3309 monitor_printf(mon, "\n");
3310 va_end(ap);
3311 siglongjmp(expr_env, 1);
3314 /* return 0 if OK, -1 if not found */
3315 static int get_monitor_def(target_long *pval, const char *name)
3317 const MonitorDef *md;
3318 void *ptr;
3320 for(md = monitor_defs; md->name != NULL; md++) {
3321 if (compare_cmd(name, md->name)) {
3322 if (md->get_value) {
3323 *pval = md->get_value(md, md->offset);
3324 } else {
3325 CPUArchState *env = mon_get_cpu_env();
3326 ptr = (uint8_t *)env + md->offset;
3327 switch(md->type) {
3328 case MD_I32:
3329 *pval = *(int32_t *)ptr;
3330 break;
3331 case MD_TLONG:
3332 *pval = *(target_long *)ptr;
3333 break;
3334 default:
3335 *pval = 0;
3336 break;
3339 return 0;
3342 return -1;
3345 static void next(void)
3347 if (*pch != '\0') {
3348 pch++;
3349 while (qemu_isspace(*pch))
3350 pch++;
3354 static int64_t expr_sum(Monitor *mon);
3356 static int64_t expr_unary(Monitor *mon)
3358 int64_t n;
3359 char *p;
3360 int ret;
3362 switch(*pch) {
3363 case '+':
3364 next();
3365 n = expr_unary(mon);
3366 break;
3367 case '-':
3368 next();
3369 n = -expr_unary(mon);
3370 break;
3371 case '~':
3372 next();
3373 n = ~expr_unary(mon);
3374 break;
3375 case '(':
3376 next();
3377 n = expr_sum(mon);
3378 if (*pch != ')') {
3379 expr_error(mon, "')' expected");
3381 next();
3382 break;
3383 case '\'':
3384 pch++;
3385 if (*pch == '\0')
3386 expr_error(mon, "character constant expected");
3387 n = *pch;
3388 pch++;
3389 if (*pch != '\'')
3390 expr_error(mon, "missing terminating \' character");
3391 next();
3392 break;
3393 case '$':
3395 char buf[128], *q;
3396 target_long reg=0;
3398 pch++;
3399 q = buf;
3400 while ((*pch >= 'a' && *pch <= 'z') ||
3401 (*pch >= 'A' && *pch <= 'Z') ||
3402 (*pch >= '0' && *pch <= '9') ||
3403 *pch == '_' || *pch == '.') {
3404 if ((q - buf) < sizeof(buf) - 1)
3405 *q++ = *pch;
3406 pch++;
3408 while (qemu_isspace(*pch))
3409 pch++;
3410 *q = 0;
3411 ret = get_monitor_def(&reg, buf);
3412 if (ret < 0)
3413 expr_error(mon, "unknown register");
3414 n = reg;
3416 break;
3417 case '\0':
3418 expr_error(mon, "unexpected end of expression");
3419 n = 0;
3420 break;
3421 default:
3422 errno = 0;
3423 n = strtoull(pch, &p, 0);
3424 if (errno == ERANGE) {
3425 expr_error(mon, "number too large");
3427 if (pch == p) {
3428 expr_error(mon, "invalid char '%c' in expression", *p);
3430 pch = p;
3431 while (qemu_isspace(*pch))
3432 pch++;
3433 break;
3435 return n;
3439 static int64_t expr_prod(Monitor *mon)
3441 int64_t val, val2;
3442 int op;
3444 val = expr_unary(mon);
3445 for(;;) {
3446 op = *pch;
3447 if (op != '*' && op != '/' && op != '%')
3448 break;
3449 next();
3450 val2 = expr_unary(mon);
3451 switch(op) {
3452 default:
3453 case '*':
3454 val *= val2;
3455 break;
3456 case '/':
3457 case '%':
3458 if (val2 == 0)
3459 expr_error(mon, "division by zero");
3460 if (op == '/')
3461 val /= val2;
3462 else
3463 val %= val2;
3464 break;
3467 return val;
3470 static int64_t expr_logic(Monitor *mon)
3472 int64_t val, val2;
3473 int op;
3475 val = expr_prod(mon);
3476 for(;;) {
3477 op = *pch;
3478 if (op != '&' && op != '|' && op != '^')
3479 break;
3480 next();
3481 val2 = expr_prod(mon);
3482 switch(op) {
3483 default:
3484 case '&':
3485 val &= val2;
3486 break;
3487 case '|':
3488 val |= val2;
3489 break;
3490 case '^':
3491 val ^= val2;
3492 break;
3495 return val;
3498 static int64_t expr_sum(Monitor *mon)
3500 int64_t val, val2;
3501 int op;
3503 val = expr_logic(mon);
3504 for(;;) {
3505 op = *pch;
3506 if (op != '+' && op != '-')
3507 break;
3508 next();
3509 val2 = expr_logic(mon);
3510 if (op == '+')
3511 val += val2;
3512 else
3513 val -= val2;
3515 return val;
3518 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3520 pch = *pp;
3521 if (sigsetjmp(expr_env, 0)) {
3522 *pp = pch;
3523 return -1;
3525 while (qemu_isspace(*pch))
3526 pch++;
3527 *pval = expr_sum(mon);
3528 *pp = pch;
3529 return 0;
3532 static int get_double(Monitor *mon, double *pval, const char **pp)
3534 const char *p = *pp;
3535 char *tailp;
3536 double d;
3538 d = strtod(p, &tailp);
3539 if (tailp == p) {
3540 monitor_printf(mon, "Number expected\n");
3541 return -1;
3543 if (d != d || d - d != 0) {
3544 /* NaN or infinity */
3545 monitor_printf(mon, "Bad number\n");
3546 return -1;
3548 *pval = d;
3549 *pp = tailp;
3550 return 0;
3554 * Store the command-name in cmdname, and return a pointer to
3555 * the remaining of the command string.
3557 static const char *get_command_name(const char *cmdline,
3558 char *cmdname, size_t nlen)
3560 size_t len;
3561 const char *p, *pstart;
3563 p = cmdline;
3564 while (qemu_isspace(*p))
3565 p++;
3566 if (*p == '\0')
3567 return NULL;
3568 pstart = p;
3569 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3570 p++;
3571 len = p - pstart;
3572 if (len > nlen - 1)
3573 len = nlen - 1;
3574 memcpy(cmdname, pstart, len);
3575 cmdname[len] = '\0';
3576 return p;
3580 * Read key of 'type' into 'key' and return the current
3581 * 'type' pointer.
3583 static char *key_get_info(const char *type, char **key)
3585 size_t len;
3586 char *p, *str;
3588 if (*type == ',')
3589 type++;
3591 p = strchr(type, ':');
3592 if (!p) {
3593 *key = NULL;
3594 return NULL;
3596 len = p - type;
3598 str = g_malloc(len + 1);
3599 memcpy(str, type, len);
3600 str[len] = '\0';
3602 *key = str;
3603 return ++p;
3606 static int default_fmt_format = 'x';
3607 static int default_fmt_size = 4;
3609 static int is_valid_option(const char *c, const char *typestr)
3611 char option[3];
3613 option[0] = '-';
3614 option[1] = *c;
3615 option[2] = '\0';
3617 typestr = strstr(typestr, option);
3618 return (typestr != NULL);
3621 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3622 const char *cmdname)
3624 const mon_cmd_t *cmd;
3626 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3627 if (compare_cmd(cmdname, cmd->name)) {
3628 return cmd;
3632 return NULL;
3635 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3637 return search_dispatch_table(qmp_cmds, cmdname);
3641 * Parse command name from @cmdp according to command table @table.
3642 * If blank, return NULL.
3643 * Else, if no valid command can be found, report to @mon, and return
3644 * NULL.
3645 * Else, change @cmdp to point right behind the name, and return its
3646 * command table entry.
3647 * Do not assume the return value points into @table! It doesn't when
3648 * the command is found in a sub-command table.
3650 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3651 const char **cmdp,
3652 mon_cmd_t *table)
3654 const char *p;
3655 const mon_cmd_t *cmd;
3656 char cmdname[256];
3658 /* extract the command name */
3659 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
3660 if (!p)
3661 return NULL;
3663 cmd = search_dispatch_table(table, cmdname);
3664 if (!cmd) {
3665 monitor_printf(mon, "unknown command: '%.*s'\n",
3666 (int)(p - *cmdp), *cmdp);
3667 return NULL;
3670 /* filter out following useless space */
3671 while (qemu_isspace(*p)) {
3672 p++;
3675 *cmdp = p;
3676 /* search sub command */
3677 if (cmd->sub_table != NULL && *p != '\0') {
3678 return monitor_parse_command(mon, cmdp, cmd->sub_table);
3681 return cmd;
3685 * Parse arguments for @cmd.
3686 * If it can't be parsed, report to @mon, and return NULL.
3687 * Else, insert command arguments into a QDict, and return it.
3688 * Note: On success, caller has to free the QDict structure.
3691 static QDict *monitor_parse_arguments(Monitor *mon,
3692 const char **endp,
3693 const mon_cmd_t *cmd)
3695 const char *typestr;
3696 char *key;
3697 int c;
3698 const char *p = *endp;
3699 char buf[1024];
3700 QDict *qdict = qdict_new();
3702 /* parse the parameters */
3703 typestr = cmd->args_type;
3704 for(;;) {
3705 typestr = key_get_info(typestr, &key);
3706 if (!typestr)
3707 break;
3708 c = *typestr;
3709 typestr++;
3710 switch(c) {
3711 case 'F':
3712 case 'B':
3713 case 's':
3715 int ret;
3717 while (qemu_isspace(*p))
3718 p++;
3719 if (*typestr == '?') {
3720 typestr++;
3721 if (*p == '\0') {
3722 /* no optional string: NULL argument */
3723 break;
3726 ret = get_str(buf, sizeof(buf), &p);
3727 if (ret < 0) {
3728 switch(c) {
3729 case 'F':
3730 monitor_printf(mon, "%s: filename expected\n",
3731 cmd->name);
3732 break;
3733 case 'B':
3734 monitor_printf(mon, "%s: block device name expected\n",
3735 cmd->name);
3736 break;
3737 default:
3738 monitor_printf(mon, "%s: string expected\n", cmd->name);
3739 break;
3741 goto fail;
3743 qdict_put(qdict, key, qstring_from_str(buf));
3745 break;
3746 case 'O':
3748 QemuOptsList *opts_list;
3749 QemuOpts *opts;
3751 opts_list = qemu_find_opts(key);
3752 if (!opts_list || opts_list->desc->name) {
3753 goto bad_type;
3755 while (qemu_isspace(*p)) {
3756 p++;
3758 if (!*p)
3759 break;
3760 if (get_str(buf, sizeof(buf), &p) < 0) {
3761 goto fail;
3763 opts = qemu_opts_parse_noisily(opts_list, buf, true);
3764 if (!opts) {
3765 goto fail;
3767 qemu_opts_to_qdict(opts, qdict);
3768 qemu_opts_del(opts);
3770 break;
3771 case '/':
3773 int count, format, size;
3775 while (qemu_isspace(*p))
3776 p++;
3777 if (*p == '/') {
3778 /* format found */
3779 p++;
3780 count = 1;
3781 if (qemu_isdigit(*p)) {
3782 count = 0;
3783 while (qemu_isdigit(*p)) {
3784 count = count * 10 + (*p - '0');
3785 p++;
3788 size = -1;
3789 format = -1;
3790 for(;;) {
3791 switch(*p) {
3792 case 'o':
3793 case 'd':
3794 case 'u':
3795 case 'x':
3796 case 'i':
3797 case 'c':
3798 format = *p++;
3799 break;
3800 case 'b':
3801 size = 1;
3802 p++;
3803 break;
3804 case 'h':
3805 size = 2;
3806 p++;
3807 break;
3808 case 'w':
3809 size = 4;
3810 p++;
3811 break;
3812 case 'g':
3813 case 'L':
3814 size = 8;
3815 p++;
3816 break;
3817 default:
3818 goto next;
3821 next:
3822 if (*p != '\0' && !qemu_isspace(*p)) {
3823 monitor_printf(mon, "invalid char in format: '%c'\n",
3824 *p);
3825 goto fail;
3827 if (format < 0)
3828 format = default_fmt_format;
3829 if (format != 'i') {
3830 /* for 'i', not specifying a size gives -1 as size */
3831 if (size < 0)
3832 size = default_fmt_size;
3833 default_fmt_size = size;
3835 default_fmt_format = format;
3836 } else {
3837 count = 1;
3838 format = default_fmt_format;
3839 if (format != 'i') {
3840 size = default_fmt_size;
3841 } else {
3842 size = -1;
3845 qdict_put(qdict, "count", qint_from_int(count));
3846 qdict_put(qdict, "format", qint_from_int(format));
3847 qdict_put(qdict, "size", qint_from_int(size));
3849 break;
3850 case 'i':
3851 case 'l':
3852 case 'M':
3854 int64_t val;
3856 while (qemu_isspace(*p))
3857 p++;
3858 if (*typestr == '?' || *typestr == '.') {
3859 if (*typestr == '?') {
3860 if (*p == '\0') {
3861 typestr++;
3862 break;
3864 } else {
3865 if (*p == '.') {
3866 p++;
3867 while (qemu_isspace(*p))
3868 p++;
3869 } else {
3870 typestr++;
3871 break;
3874 typestr++;
3876 if (get_expr(mon, &val, &p))
3877 goto fail;
3878 /* Check if 'i' is greater than 32-bit */
3879 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3880 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
3881 monitor_printf(mon, "integer is for 32-bit values\n");
3882 goto fail;
3883 } else if (c == 'M') {
3884 if (val < 0) {
3885 monitor_printf(mon, "enter a positive value\n");
3886 goto fail;
3888 val <<= 20;
3890 qdict_put(qdict, key, qint_from_int(val));
3892 break;
3893 case 'o':
3895 int64_t val;
3896 char *end;
3898 while (qemu_isspace(*p)) {
3899 p++;
3901 if (*typestr == '?') {
3902 typestr++;
3903 if (*p == '\0') {
3904 break;
3907 val = strtosz(p, &end);
3908 if (val < 0) {
3909 monitor_printf(mon, "invalid size\n");
3910 goto fail;
3912 qdict_put(qdict, key, qint_from_int(val));
3913 p = end;
3915 break;
3916 case 'T':
3918 double val;
3920 while (qemu_isspace(*p))
3921 p++;
3922 if (*typestr == '?') {
3923 typestr++;
3924 if (*p == '\0') {
3925 break;
3928 if (get_double(mon, &val, &p) < 0) {
3929 goto fail;
3931 if (p[0] && p[1] == 's') {
3932 switch (*p) {
3933 case 'm':
3934 val /= 1e3; p += 2; break;
3935 case 'u':
3936 val /= 1e6; p += 2; break;
3937 case 'n':
3938 val /= 1e9; p += 2; break;
3941 if (*p && !qemu_isspace(*p)) {
3942 monitor_printf(mon, "Unknown unit suffix\n");
3943 goto fail;
3945 qdict_put(qdict, key, qfloat_from_double(val));
3947 break;
3948 case 'b':
3950 const char *beg;
3951 bool val;
3953 while (qemu_isspace(*p)) {
3954 p++;
3956 beg = p;
3957 while (qemu_isgraph(*p)) {
3958 p++;
3960 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3961 val = true;
3962 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3963 val = false;
3964 } else {
3965 monitor_printf(mon, "Expected 'on' or 'off'\n");
3966 goto fail;
3968 qdict_put(qdict, key, qbool_from_bool(val));
3970 break;
3971 case '-':
3973 const char *tmp = p;
3974 int skip_key = 0;
3975 /* option */
3977 c = *typestr++;
3978 if (c == '\0')
3979 goto bad_type;
3980 while (qemu_isspace(*p))
3981 p++;
3982 if (*p == '-') {
3983 p++;
3984 if(c != *p) {
3985 if(!is_valid_option(p, typestr)) {
3987 monitor_printf(mon, "%s: unsupported option -%c\n",
3988 cmd->name, *p);
3989 goto fail;
3990 } else {
3991 skip_key = 1;
3994 if(skip_key) {
3995 p = tmp;
3996 } else {
3997 /* has option */
3998 p++;
3999 qdict_put(qdict, key, qbool_from_bool(true));
4003 break;
4004 case 'S':
4006 /* package all remaining string */
4007 int len;
4009 while (qemu_isspace(*p)) {
4010 p++;
4012 if (*typestr == '?') {
4013 typestr++;
4014 if (*p == '\0') {
4015 /* no remaining string: NULL argument */
4016 break;
4019 len = strlen(p);
4020 if (len <= 0) {
4021 monitor_printf(mon, "%s: string expected\n",
4022 cmd->name);
4023 goto fail;
4025 qdict_put(qdict, key, qstring_from_str(p));
4026 p += len;
4028 break;
4029 default:
4030 bad_type:
4031 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
4032 goto fail;
4034 g_free(key);
4035 key = NULL;
4037 /* check that all arguments were parsed */
4038 while (qemu_isspace(*p))
4039 p++;
4040 if (*p != '\0') {
4041 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4042 cmd->name);
4043 goto fail;
4046 return qdict;
4048 fail:
4049 QDECREF(qdict);
4050 g_free(key);
4051 return NULL;
4054 static void handle_hmp_command(Monitor *mon, const char *cmdline)
4056 QDict *qdict;
4057 const mon_cmd_t *cmd;
4059 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4060 if (!cmd) {
4061 return;
4064 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4065 if (!qdict) {
4066 monitor_printf(mon, "Try \"help %s\" for more information\n",
4067 cmd->name);
4068 return;
4071 cmd->mhandler.cmd(mon, qdict);
4072 QDECREF(qdict);
4075 static void cmd_completion(Monitor *mon, const char *name, const char *list)
4077 const char *p, *pstart;
4078 char cmd[128];
4079 int len;
4081 p = list;
4082 for(;;) {
4083 pstart = p;
4084 p = strchr(p, '|');
4085 if (!p)
4086 p = pstart + strlen(pstart);
4087 len = p - pstart;
4088 if (len > sizeof(cmd) - 2)
4089 len = sizeof(cmd) - 2;
4090 memcpy(cmd, pstart, len);
4091 cmd[len] = '\0';
4092 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
4093 readline_add_completion(mon->rs, cmd);
4095 if (*p == '\0')
4096 break;
4097 p++;
4101 static void file_completion(Monitor *mon, const char *input)
4103 DIR *ffs;
4104 struct dirent *d;
4105 char path[1024];
4106 char file[1024], file_prefix[1024];
4107 int input_path_len;
4108 const char *p;
4110 p = strrchr(input, '/');
4111 if (!p) {
4112 input_path_len = 0;
4113 pstrcpy(file_prefix, sizeof(file_prefix), input);
4114 pstrcpy(path, sizeof(path), ".");
4115 } else {
4116 input_path_len = p - input + 1;
4117 memcpy(path, input, input_path_len);
4118 if (input_path_len > sizeof(path) - 1)
4119 input_path_len = sizeof(path) - 1;
4120 path[input_path_len] = '\0';
4121 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4124 ffs = opendir(path);
4125 if (!ffs)
4126 return;
4127 for(;;) {
4128 struct stat sb;
4129 d = readdir(ffs);
4130 if (!d)
4131 break;
4133 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4134 continue;
4137 if (strstart(d->d_name, file_prefix, NULL)) {
4138 memcpy(file, input, input_path_len);
4139 if (input_path_len < sizeof(file))
4140 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4141 d->d_name);
4142 /* stat the file to find out if it's a directory.
4143 * In that case add a slash to speed up typing long paths
4145 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
4146 pstrcat(file, sizeof(file), "/");
4148 readline_add_completion(mon->rs, file);
4151 closedir(ffs);
4154 static const char *next_arg_type(const char *typestr)
4156 const char *p = strchr(typestr, ':');
4157 return (p != NULL ? ++p : typestr);
4160 static void add_completion_option(ReadLineState *rs, const char *str,
4161 const char *option)
4163 if (!str || !option) {
4164 return;
4166 if (!strncmp(option, str, strlen(str))) {
4167 readline_add_completion(rs, option);
4171 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4173 size_t len;
4174 ChardevBackendInfoList *list, *start;
4176 if (nb_args != 2) {
4177 return;
4179 len = strlen(str);
4180 readline_set_completion_index(rs, len);
4182 start = list = qmp_query_chardev_backends(NULL);
4183 while (list) {
4184 const char *chr_name = list->value->name;
4186 if (!strncmp(chr_name, str, len)) {
4187 readline_add_completion(rs, chr_name);
4189 list = list->next;
4191 qapi_free_ChardevBackendInfoList(start);
4194 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4196 size_t len;
4197 int i;
4199 if (nb_args != 2) {
4200 return;
4202 len = strlen(str);
4203 readline_set_completion_index(rs, len);
4204 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4205 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4209 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
4211 GSList *list, *elt;
4212 size_t len;
4214 if (nb_args != 2) {
4215 return;
4218 len = strlen(str);
4219 readline_set_completion_index(rs, len);
4220 list = elt = object_class_get_list(TYPE_DEVICE, false);
4221 while (elt) {
4222 const char *name;
4223 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4224 TYPE_DEVICE);
4225 name = object_class_get_name(OBJECT_CLASS(dc));
4227 if (!dc->cannot_instantiate_with_device_add_yet
4228 && !strncmp(name, str, len)) {
4229 readline_add_completion(rs, name);
4231 elt = elt->next;
4233 g_slist_free(list);
4236 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
4238 GSList *list, *elt;
4239 size_t len;
4241 if (nb_args != 2) {
4242 return;
4245 len = strlen(str);
4246 readline_set_completion_index(rs, len);
4247 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4248 while (elt) {
4249 const char *name;
4251 name = object_class_get_name(OBJECT_CLASS(elt->data));
4252 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4253 readline_add_completion(rs, name);
4255 elt = elt->next;
4257 g_slist_free(list);
4260 static void peripheral_device_del_completion(ReadLineState *rs,
4261 const char *str, size_t len)
4263 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4264 GSList *list, *item;
4266 list = qdev_build_hotpluggable_device_list(peripheral);
4267 if (!list) {
4268 return;
4271 for (item = list; item; item = g_slist_next(item)) {
4272 DeviceState *dev = item->data;
4274 if (dev->id && !strncmp(str, dev->id, len)) {
4275 readline_add_completion(rs, dev->id);
4279 g_slist_free(list);
4282 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4284 size_t len;
4285 ChardevInfoList *list, *start;
4287 if (nb_args != 2) {
4288 return;
4290 len = strlen(str);
4291 readline_set_completion_index(rs, len);
4293 start = list = qmp_query_chardev(NULL);
4294 while (list) {
4295 ChardevInfo *chr = list->value;
4297 if (!strncmp(chr->label, str, len)) {
4298 readline_add_completion(rs, chr->label);
4300 list = list->next;
4302 qapi_free_ChardevInfoList(start);
4305 static void ringbuf_completion(ReadLineState *rs, const char *str)
4307 size_t len;
4308 ChardevInfoList *list, *start;
4310 len = strlen(str);
4311 readline_set_completion_index(rs, len);
4313 start = list = qmp_query_chardev(NULL);
4314 while (list) {
4315 ChardevInfo *chr_info = list->value;
4317 if (!strncmp(chr_info->label, str, len)) {
4318 CharDriverState *chr = qemu_chr_find(chr_info->label);
4319 if (chr && chr_is_ringbuf(chr)) {
4320 readline_add_completion(rs, chr_info->label);
4323 list = list->next;
4325 qapi_free_ChardevInfoList(start);
4328 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4330 if (nb_args != 2) {
4331 return;
4333 ringbuf_completion(rs, str);
4336 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4338 size_t len;
4340 if (nb_args != 2) {
4341 return;
4344 len = strlen(str);
4345 readline_set_completion_index(rs, len);
4346 peripheral_device_del_completion(rs, str, len);
4349 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
4351 ObjectPropertyInfoList *list, *start;
4352 size_t len;
4354 if (nb_args != 2) {
4355 return;
4357 len = strlen(str);
4358 readline_set_completion_index(rs, len);
4360 start = list = qmp_qom_list("/objects", NULL);
4361 while (list) {
4362 ObjectPropertyInfo *info = list->value;
4364 if (!strncmp(info->type, "child<", 5)
4365 && !strncmp(info->name, str, len)) {
4366 readline_add_completion(rs, info->name);
4368 list = list->next;
4370 qapi_free_ObjectPropertyInfoList(start);
4373 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4375 int i;
4376 char *sep;
4377 size_t len;
4379 if (nb_args != 2) {
4380 return;
4382 sep = strrchr(str, '-');
4383 if (sep) {
4384 str = sep + 1;
4386 len = strlen(str);
4387 readline_set_completion_index(rs, len);
4388 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4389 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4390 readline_add_completion(rs, QKeyCode_lookup[i]);
4395 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4397 size_t len;
4399 len = strlen(str);
4400 readline_set_completion_index(rs, len);
4401 if (nb_args == 2) {
4402 NetClientState *ncs[MAX_QUEUE_NUM];
4403 int count, i;
4404 count = qemu_find_net_clients_except(NULL, ncs,
4405 NET_CLIENT_OPTIONS_KIND_NONE,
4406 MAX_QUEUE_NUM);
4407 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4408 const char *name = ncs[i]->name;
4409 if (!strncmp(str, name, len)) {
4410 readline_add_completion(rs, name);
4413 } else if (nb_args == 3) {
4414 add_completion_option(rs, str, "on");
4415 add_completion_option(rs, str, "off");
4419 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4421 int len, count, i;
4422 NetClientState *ncs[MAX_QUEUE_NUM];
4424 if (nb_args != 2) {
4425 return;
4428 len = strlen(str);
4429 readline_set_completion_index(rs, len);
4430 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
4431 MAX_QUEUE_NUM);
4432 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4433 QemuOpts *opts;
4434 const char *name = ncs[i]->name;
4435 if (strncmp(str, name, len)) {
4436 continue;
4438 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4439 if (opts) {
4440 readline_add_completion(rs, name);
4445 void trace_event_completion(ReadLineState *rs, int nb_args, const char *str)
4447 size_t len;
4449 len = strlen(str);
4450 readline_set_completion_index(rs, len);
4451 if (nb_args == 2) {
4452 TraceEventID id;
4453 for (id = 0; id < trace_event_count(); id++) {
4454 const char *event_name = trace_event_get_name(trace_event_id(id));
4455 if (!strncmp(str, event_name, len)) {
4456 readline_add_completion(rs, event_name);
4459 } else if (nb_args == 3) {
4460 add_completion_option(rs, str, "on");
4461 add_completion_option(rs, str, "off");
4465 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4467 int i;
4469 if (nb_args != 2) {
4470 return;
4472 readline_set_completion_index(rs, strlen(str));
4473 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4474 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4478 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4479 const char *str)
4481 size_t len;
4483 len = strlen(str);
4484 readline_set_completion_index(rs, len);
4485 if (nb_args == 2) {
4486 int i;
4487 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4488 const char *name = MigrationCapability_lookup[i];
4489 if (!strncmp(str, name, len)) {
4490 readline_add_completion(rs, name);
4493 } else if (nb_args == 3) {
4494 add_completion_option(rs, str, "on");
4495 add_completion_option(rs, str, "off");
4499 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4500 const char *str)
4502 size_t len;
4504 len = strlen(str);
4505 readline_set_completion_index(rs, len);
4506 if (nb_args == 2) {
4507 int i;
4508 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4509 const char *name = MigrationParameter_lookup[i];
4510 if (!strncmp(str, name, len)) {
4511 readline_add_completion(rs, name);
4517 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4519 int i;
4520 size_t len;
4521 if (nb_args != 2) {
4522 return;
4524 len = strlen(str);
4525 readline_set_completion_index(rs, len);
4526 for (i = 0; host_net_devices[i]; i++) {
4527 if (!strncmp(host_net_devices[i], str, len)) {
4528 readline_add_completion(rs, host_net_devices[i]);
4533 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4535 NetClientState *ncs[MAX_QUEUE_NUM];
4536 int count, i, len;
4538 len = strlen(str);
4539 readline_set_completion_index(rs, len);
4540 if (nb_args == 2) {
4541 count = qemu_find_net_clients_except(NULL, ncs,
4542 NET_CLIENT_OPTIONS_KIND_NONE,
4543 MAX_QUEUE_NUM);
4544 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4545 int id;
4546 char name[16];
4548 if (net_hub_id_for_client(ncs[i], &id)) {
4549 continue;
4551 snprintf(name, sizeof(name), "%d", id);
4552 if (!strncmp(str, name, len)) {
4553 readline_add_completion(rs, name);
4556 return;
4557 } else if (nb_args == 3) {
4558 count = qemu_find_net_clients_except(NULL, ncs,
4559 NET_CLIENT_OPTIONS_KIND_NIC,
4560 MAX_QUEUE_NUM);
4561 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4562 int id;
4563 const char *name;
4565 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4566 net_hub_id_for_client(ncs[i], &id)) {
4567 continue;
4569 name = ncs[i]->name;
4570 if (!strncmp(str, name, len)) {
4571 readline_add_completion(rs, name);
4574 return;
4578 static void vm_completion(ReadLineState *rs, const char *str)
4580 size_t len;
4581 BlockDriverState *bs = NULL;
4583 len = strlen(str);
4584 readline_set_completion_index(rs, len);
4585 while ((bs = bdrv_next(bs))) {
4586 SnapshotInfoList *snapshots, *snapshot;
4588 if (!bdrv_can_snapshot(bs)) {
4589 continue;
4591 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4592 continue;
4594 snapshot = snapshots;
4595 while (snapshot) {
4596 char *completion = snapshot->value->name;
4597 if (!strncmp(str, completion, len)) {
4598 readline_add_completion(rs, completion);
4600 completion = snapshot->value->id;
4601 if (!strncmp(str, completion, len)) {
4602 readline_add_completion(rs, completion);
4604 snapshot = snapshot->next;
4606 qapi_free_SnapshotInfoList(snapshots);
4611 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4613 if (nb_args == 2) {
4614 vm_completion(rs, str);
4618 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4620 if (nb_args == 2) {
4621 vm_completion(rs, str);
4625 static void monitor_find_completion_by_table(Monitor *mon,
4626 const mon_cmd_t *cmd_table,
4627 char **args,
4628 int nb_args)
4630 const char *cmdname;
4631 int i;
4632 const char *ptype, *str, *name;
4633 const mon_cmd_t *cmd;
4634 BlockDriverState *bs;
4636 if (nb_args <= 1) {
4637 /* command completion */
4638 if (nb_args == 0)
4639 cmdname = "";
4640 else
4641 cmdname = args[0];
4642 readline_set_completion_index(mon->rs, strlen(cmdname));
4643 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
4644 cmd_completion(mon, cmdname, cmd->name);
4646 } else {
4647 /* find the command */
4648 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
4649 if (compare_cmd(args[0], cmd->name)) {
4650 break;
4653 if (!cmd->name) {
4654 return;
4657 if (cmd->sub_table) {
4658 /* do the job again */
4659 monitor_find_completion_by_table(mon, cmd->sub_table,
4660 &args[1], nb_args - 1);
4661 return;
4663 if (cmd->command_completion) {
4664 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4665 return;
4668 ptype = next_arg_type(cmd->args_type);
4669 for(i = 0; i < nb_args - 2; i++) {
4670 if (*ptype != '\0') {
4671 ptype = next_arg_type(ptype);
4672 while (*ptype == '?')
4673 ptype = next_arg_type(ptype);
4676 str = args[nb_args - 1];
4677 while (*ptype == '-' && ptype[1] != '\0') {
4678 ptype = next_arg_type(ptype);
4680 switch(*ptype) {
4681 case 'F':
4682 /* file completion */
4683 readline_set_completion_index(mon->rs, strlen(str));
4684 file_completion(mon, str);
4685 break;
4686 case 'B':
4687 /* block device name completion */
4688 readline_set_completion_index(mon->rs, strlen(str));
4689 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4690 name = bdrv_get_device_name(bs);
4691 if (str[0] == '\0' ||
4692 !strncmp(name, str, strlen(str))) {
4693 readline_add_completion(mon->rs, name);
4696 break;
4697 case 's':
4698 case 'S':
4699 if (!strcmp(cmd->name, "help|?")) {
4700 monitor_find_completion_by_table(mon, cmd_table,
4701 &args[1], nb_args - 1);
4703 break;
4704 default:
4705 break;
4710 static void monitor_find_completion(void *opaque,
4711 const char *cmdline)
4713 Monitor *mon = opaque;
4714 char *args[MAX_ARGS];
4715 int nb_args, len;
4717 /* 1. parse the cmdline */
4718 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4719 return;
4722 /* if the line ends with a space, it means we want to complete the
4723 next arg */
4724 len = strlen(cmdline);
4725 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4726 if (nb_args >= MAX_ARGS) {
4727 goto cleanup;
4729 args[nb_args++] = g_strdup("");
4732 /* 2. auto complete according to args */
4733 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
4735 cleanup:
4736 free_cmdline_args(args, nb_args);
4739 static int monitor_can_read(void *opaque)
4741 Monitor *mon = opaque;
4743 return (mon->suspend_cnt == 0) ? 1 : 0;
4746 static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4747 Error **errp)
4749 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
4751 if (is_cap && mon->qmp.in_command_mode) {
4752 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4753 "Capabilities negotiation is already complete, command "
4754 "'%s' ignored", cmd->name);
4755 return true;
4757 if (!is_cap && !mon->qmp.in_command_mode) {
4758 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4759 "Expecting capabilities negotiation with "
4760 "'qmp_capabilities' before command '%s'", cmd->name);
4761 return true;
4763 return false;
4767 * Argument validation rules:
4769 * 1. The argument must exist in cmd_args qdict
4770 * 2. The argument type must be the expected one
4772 * Special case: If the argument doesn't exist in cmd_args and
4773 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4774 * checking is skipped for it.
4776 static void check_client_args_type(const QDict *client_args,
4777 const QDict *cmd_args, int flags,
4778 Error **errp)
4780 const QDictEntry *ent;
4782 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4783 QObject *obj;
4784 QString *arg_type;
4785 const QObject *client_arg = qdict_entry_value(ent);
4786 const char *client_arg_name = qdict_entry_key(ent);
4788 obj = qdict_get(cmd_args, client_arg_name);
4789 if (!obj) {
4790 if (flags & QMP_ACCEPT_UNKNOWNS) {
4791 /* handler accepts unknowns */
4792 continue;
4794 /* client arg doesn't exist */
4795 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
4796 return;
4799 arg_type = qobject_to_qstring(obj);
4800 assert(arg_type != NULL);
4802 /* check if argument's type is correct */
4803 switch (qstring_get_str(arg_type)[0]) {
4804 case 'F':
4805 case 'B':
4806 case 's':
4807 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4808 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4809 client_arg_name, "string");
4810 return;
4812 break;
4813 case 'i':
4814 case 'l':
4815 case 'M':
4816 case 'o':
4817 if (qobject_type(client_arg) != QTYPE_QINT) {
4818 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4819 client_arg_name, "int");
4820 return;
4822 break;
4823 case 'T':
4824 if (qobject_type(client_arg) != QTYPE_QINT &&
4825 qobject_type(client_arg) != QTYPE_QFLOAT) {
4826 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4827 client_arg_name, "number");
4828 return;
4830 break;
4831 case 'b':
4832 case '-':
4833 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4834 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4835 client_arg_name, "bool");
4836 return;
4838 break;
4839 case 'O':
4840 assert(flags & QMP_ACCEPT_UNKNOWNS);
4841 break;
4842 case 'q':
4843 /* Any QObject can be passed. */
4844 break;
4845 case '/':
4846 case '.':
4848 * These types are not supported by QMP and thus are not
4849 * handled here. Fall through.
4851 default:
4852 abort();
4858 * - Check if the client has passed all mandatory args
4859 * - Set special flags for argument validation
4861 static void check_mandatory_args(const QDict *cmd_args,
4862 const QDict *client_args, int *flags,
4863 Error **errp)
4865 const QDictEntry *ent;
4867 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4868 const char *cmd_arg_name = qdict_entry_key(ent);
4869 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4870 assert(type != NULL);
4872 if (qstring_get_str(type)[0] == 'O') {
4873 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4874 *flags |= QMP_ACCEPT_UNKNOWNS;
4875 } else if (qstring_get_str(type)[0] != '-' &&
4876 qstring_get_str(type)[1] != '?' &&
4877 !qdict_haskey(client_args, cmd_arg_name)) {
4878 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4879 return;
4884 static QDict *qdict_from_args_type(const char *args_type)
4886 int i;
4887 QDict *qdict;
4888 QString *key, *type, *cur_qs;
4890 assert(args_type != NULL);
4892 qdict = qdict_new();
4894 if (args_type == NULL || args_type[0] == '\0') {
4895 /* no args, empty qdict */
4896 goto out;
4899 key = qstring_new();
4900 type = qstring_new();
4902 cur_qs = key;
4904 for (i = 0;; i++) {
4905 switch (args_type[i]) {
4906 case ',':
4907 case '\0':
4908 qdict_put(qdict, qstring_get_str(key), type);
4909 QDECREF(key);
4910 if (args_type[i] == '\0') {
4911 goto out;
4913 type = qstring_new(); /* qdict has ref */
4914 cur_qs = key = qstring_new();
4915 break;
4916 case ':':
4917 cur_qs = type;
4918 break;
4919 default:
4920 qstring_append_chr(cur_qs, args_type[i]);
4921 break;
4925 out:
4926 return qdict;
4930 * Client argument checking rules:
4932 * 1. Client must provide all mandatory arguments
4933 * 2. Each argument provided by the client must be expected
4934 * 3. Each argument provided by the client must have the type expected
4935 * by the command
4937 static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4938 Error **errp)
4940 Error *err = NULL;
4941 int flags;
4942 QDict *cmd_args;
4944 cmd_args = qdict_from_args_type(cmd->args_type);
4946 flags = 0;
4947 check_mandatory_args(cmd_args, client_args, &flags, &err);
4948 if (err) {
4949 goto out;
4952 check_client_args_type(client_args, cmd_args, flags, &err);
4954 out:
4955 error_propagate(errp, err);
4956 QDECREF(cmd_args);
4960 * Input object checking rules
4962 * 1. Input object must be a dict
4963 * 2. The "execute" key must exist
4964 * 3. The "execute" key must be a string
4965 * 4. If the "arguments" key exists, it must be a dict
4966 * 5. If the "id" key exists, it can be anything (ie. json-value)
4967 * 6. Any argument not listed above is considered invalid
4969 static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
4971 const QDictEntry *ent;
4972 int has_exec_key = 0;
4973 QDict *input_dict;
4975 if (qobject_type(input_obj) != QTYPE_QDICT) {
4976 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
4977 return NULL;
4980 input_dict = qobject_to_qdict(input_obj);
4982 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4983 const char *arg_name = qdict_entry_key(ent);
4984 const QObject *arg_obj = qdict_entry_value(ent);
4986 if (!strcmp(arg_name, "execute")) {
4987 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4988 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4989 "execute", "string");
4990 return NULL;
4992 has_exec_key = 1;
4993 } else if (!strcmp(arg_name, "arguments")) {
4994 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4995 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4996 "arguments", "object");
4997 return NULL;
4999 } else if (!strcmp(arg_name, "id")) {
5000 /* Any string is acceptable as "id", so nothing to check */
5001 } else {
5002 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
5003 return NULL;
5007 if (!has_exec_key) {
5008 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
5009 return NULL;
5012 return input_dict;
5015 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5017 Error *local_err = NULL;
5018 QObject *obj, *data;
5019 QDict *input, *args;
5020 const mon_cmd_t *cmd;
5021 const char *cmd_name;
5022 Monitor *mon = cur_mon;
5024 args = input = NULL;
5025 data = NULL;
5027 obj = json_parser_parse(tokens, NULL);
5028 if (!obj) {
5029 // FIXME: should be triggered in json_parser_parse()
5030 error_setg(&local_err, QERR_JSON_PARSING);
5031 goto err_out;
5034 input = qmp_check_input_obj(obj, &local_err);
5035 if (!input) {
5036 qobject_decref(obj);
5037 goto err_out;
5040 mon->qmp.id = qdict_get(input, "id");
5041 qobject_incref(mon->qmp.id);
5043 cmd_name = qdict_get_str(input, "execute");
5044 trace_handle_qmp_command(mon, cmd_name);
5045 cmd = qmp_find_cmd(cmd_name);
5046 if (!cmd) {
5047 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5048 "The command %s has not been found", cmd_name);
5049 goto err_out;
5051 if (invalid_qmp_mode(mon, cmd, &local_err)) {
5052 goto err_out;
5055 obj = qdict_get(input, "arguments");
5056 if (!obj) {
5057 args = qdict_new();
5058 } else {
5059 args = qobject_to_qdict(obj);
5060 QINCREF(args);
5063 qmp_check_client_args(cmd, args, &local_err);
5064 if (local_err) {
5065 goto err_out;
5068 cmd->mhandler.cmd_new(args, &data, &local_err);
5070 err_out:
5071 monitor_protocol_emitter(mon, data, local_err);
5072 qobject_decref(data);
5073 QDECREF(input);
5074 QDECREF(args);
5077 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
5079 Monitor *old_mon = cur_mon;
5081 cur_mon = opaque;
5083 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
5085 cur_mon = old_mon;
5088 static void monitor_read(void *opaque, const uint8_t *buf, int size)
5090 Monitor *old_mon = cur_mon;
5091 int i;
5093 cur_mon = opaque;
5095 if (cur_mon->rs) {
5096 for (i = 0; i < size; i++)
5097 readline_handle_byte(cur_mon->rs, buf[i]);
5098 } else {
5099 if (size == 0 || buf[size - 1] != 0)
5100 monitor_printf(cur_mon, "corrupted command\n");
5101 else
5102 handle_hmp_command(cur_mon, (char *)buf);
5105 cur_mon = old_mon;
5108 static void monitor_command_cb(void *opaque, const char *cmdline,
5109 void *readline_opaque)
5111 Monitor *mon = opaque;
5113 monitor_suspend(mon);
5114 handle_hmp_command(mon, cmdline);
5115 monitor_resume(mon);
5118 int monitor_suspend(Monitor *mon)
5120 if (!mon->rs)
5121 return -ENOTTY;
5122 mon->suspend_cnt++;
5123 return 0;
5126 void monitor_resume(Monitor *mon)
5128 if (!mon->rs)
5129 return;
5130 if (--mon->suspend_cnt == 0)
5131 readline_show_prompt(mon->rs);
5134 static QObject *get_qmp_greeting(void)
5136 QObject *ver = NULL;
5138 qmp_marshal_input_query_version(NULL, &ver, NULL);
5139 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5142 static void monitor_qmp_event(void *opaque, int event)
5144 QObject *data;
5145 Monitor *mon = opaque;
5147 switch (event) {
5148 case CHR_EVENT_OPENED:
5149 mon->qmp.in_command_mode = false;
5150 data = get_qmp_greeting();
5151 monitor_json_emitter(mon, data);
5152 qobject_decref(data);
5153 mon_refcount++;
5154 break;
5155 case CHR_EVENT_CLOSED:
5156 json_message_parser_destroy(&mon->qmp.parser);
5157 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
5158 mon_refcount--;
5159 monitor_fdsets_cleanup();
5160 break;
5164 static void monitor_event(void *opaque, int event)
5166 Monitor *mon = opaque;
5168 switch (event) {
5169 case CHR_EVENT_MUX_IN:
5170 qemu_mutex_lock(&mon->out_lock);
5171 mon->mux_out = 0;
5172 qemu_mutex_unlock(&mon->out_lock);
5173 if (mon->reset_seen) {
5174 readline_restart(mon->rs);
5175 monitor_resume(mon);
5176 monitor_flush(mon);
5177 } else {
5178 mon->suspend_cnt = 0;
5180 break;
5182 case CHR_EVENT_MUX_OUT:
5183 if (mon->reset_seen) {
5184 if (mon->suspend_cnt == 0) {
5185 monitor_printf(mon, "\n");
5187 monitor_flush(mon);
5188 monitor_suspend(mon);
5189 } else {
5190 mon->suspend_cnt++;
5192 qemu_mutex_lock(&mon->out_lock);
5193 mon->mux_out = 1;
5194 qemu_mutex_unlock(&mon->out_lock);
5195 break;
5197 case CHR_EVENT_OPENED:
5198 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5199 "information\n", QEMU_VERSION);
5200 if (!mon->mux_out) {
5201 readline_restart(mon->rs);
5202 readline_show_prompt(mon->rs);
5204 mon->reset_seen = 1;
5205 mon_refcount++;
5206 break;
5208 case CHR_EVENT_CLOSED:
5209 mon_refcount--;
5210 monitor_fdsets_cleanup();
5211 break;
5215 static int
5216 compare_mon_cmd(const void *a, const void *b)
5218 return strcmp(((const mon_cmd_t *)a)->name,
5219 ((const mon_cmd_t *)b)->name);
5222 static void sortcmdlist(void)
5224 int array_num;
5225 int elem_size = sizeof(mon_cmd_t);
5227 array_num = sizeof(mon_cmds)/elem_size-1;
5228 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5230 array_num = sizeof(info_cmds)/elem_size-1;
5231 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5236 * Local variables:
5237 * c-indent-level: 4
5238 * c-basic-offset: 4
5239 * tab-width: 8
5240 * End:
5243 /* These functions just adapt the readline interface in a typesafe way. We
5244 * could cast function pointers but that discards compiler checks.
5246 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5247 const char *fmt, ...)
5249 va_list ap;
5250 va_start(ap, fmt);
5251 monitor_vprintf(opaque, fmt, ap);
5252 va_end(ap);
5255 static void monitor_readline_flush(void *opaque)
5257 monitor_flush(opaque);
5260 static void __attribute__((constructor)) monitor_lock_init(void)
5262 qemu_mutex_init(&monitor_lock);
5265 void monitor_init(CharDriverState *chr, int flags)
5267 static int is_first_init = 1;
5268 Monitor *mon;
5270 if (is_first_init) {
5271 monitor_qapi_event_init();
5272 sortcmdlist();
5273 is_first_init = 0;
5276 mon = g_malloc(sizeof(*mon));
5277 monitor_data_init(mon);
5279 mon->chr = chr;
5280 mon->flags = flags;
5281 if (flags & MONITOR_USE_READLINE) {
5282 mon->rs = readline_init(monitor_readline_printf,
5283 monitor_readline_flush,
5284 mon,
5285 monitor_find_completion);
5286 monitor_read_command(mon, 0);
5289 if (monitor_is_qmp(mon)) {
5290 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5291 monitor_qmp_event, mon);
5292 qemu_chr_fe_set_echo(chr, true);
5293 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
5294 } else {
5295 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5296 monitor_event, mon);
5299 qemu_mutex_lock(&monitor_lock);
5300 QLIST_INSERT_HEAD(&mon_list, mon, entry);
5301 qemu_mutex_unlock(&monitor_lock);
5304 static void bdrv_password_cb(void *opaque, const char *password,
5305 void *readline_opaque)
5307 Monitor *mon = opaque;
5308 BlockDriverState *bs = readline_opaque;
5309 int ret = 0;
5310 Error *local_err = NULL;
5312 bdrv_add_key(bs, password, &local_err);
5313 if (local_err) {
5314 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5315 error_free(local_err);
5316 ret = -EPERM;
5318 if (mon->password_completion_cb)
5319 mon->password_completion_cb(mon->password_opaque, ret);
5321 monitor_read_command(mon, 1);
5324 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5325 BlockCompletionFunc *completion_cb,
5326 void *opaque)
5328 int err;
5330 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5331 bdrv_get_encrypted_filename(bs));
5333 mon->password_completion_cb = completion_cb;
5334 mon->password_opaque = opaque;
5336 err = monitor_read_password(mon, bdrv_password_cb, bs);
5338 if (err && completion_cb)
5339 completion_cb(opaque, err);
5341 return err;
5344 int monitor_read_block_device_key(Monitor *mon, const char *device,
5345 BlockCompletionFunc *completion_cb,
5346 void *opaque)
5348 Error *err = NULL;
5349 BlockBackend *blk;
5351 blk = blk_by_name(device);
5352 if (!blk) {
5353 monitor_printf(mon, "Device not found %s\n", device);
5354 return -1;
5357 bdrv_add_key(blk_bs(blk), NULL, &err);
5358 if (err) {
5359 error_free(err);
5360 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5363 if (completion_cb) {
5364 completion_cb(opaque, 0);
5366 return 0;
5369 QemuOptsList qemu_mon_opts = {
5370 .name = "mon",
5371 .implied_opt_name = "chardev",
5372 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5373 .desc = {
5375 .name = "mode",
5376 .type = QEMU_OPT_STRING,
5378 .name = "chardev",
5379 .type = QEMU_OPT_STRING,
5381 .name = "default",
5382 .type = QEMU_OPT_BOOL,
5384 .name = "pretty",
5385 .type = QEMU_OPT_BOOL,
5387 { /* end of list */ }
5391 #ifndef TARGET_I386
5392 void qmp_rtc_reset_reinjection(Error **errp)
5394 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5396 #endif
5398 #ifndef TARGET_S390X
5399 void qmp_dump_skeys(const char *filename, Error **errp)
5401 error_setg(errp, QERR_FEATURE_DISABLED, "dump-skeys");
5403 #endif