qerror: Finally unused, clean up
[qemu/ar7.git] / monitor.c
blob497fed289e1d68c6aad66c0e8d83beff42fabd22
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/qint.h"
53 #include "qapi/qmp/qfloat.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qmp/qbool.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qapi/qmp/qjson.h"
58 #include "qapi/qmp/json-streamer.h"
59 #include "qapi/qmp/json-parser.h"
60 #include <qom/object_interfaces.h>
61 #include "qemu/osdep.h"
62 #include "cpu.h"
63 #include "trace.h"
64 #include "trace/control.h"
65 #ifdef CONFIG_TRACE_SIMPLE
66 #include "trace/simple.h"
67 #endif
68 #include "exec/memory.h"
69 #include "exec/cpu_ldst.h"
70 #include "qmp-commands.h"
71 #include "hmp.h"
72 #include "qemu/thread.h"
73 #include "block/qapi.h"
74 #include "qapi/qmp-event.h"
75 #include "qapi-event.h"
76 #include "sysemu/block-backend.h"
78 /* for hmp_info_irq/pic */
79 #if defined(TARGET_SPARC)
80 #include "hw/sparc/sun4m.h"
81 #endif
82 #include "hw/lm32/lm32_pic.h"
85 * Supported types:
87 * 'F' filename
88 * 'B' block device name
89 * 's' string (accept optional quote)
90 * 'S' it just appends the rest of the string (accept optional quote)
91 * 'O' option string of the form NAME=VALUE,...
92 * parsed according to QemuOptsList given by its name
93 * Example: 'device:O' uses qemu_device_opts.
94 * Restriction: only lists with empty desc are supported
95 * TODO lift the restriction
96 * 'i' 32 bit integer
97 * 'l' target long (32 or 64 bit)
98 * 'M' Non-negative target long (32 or 64 bit), in user mode the
99 * value is multiplied by 2^20 (think Mebibyte)
100 * 'o' octets (aka bytes)
101 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
102 * K, k suffix, which multiplies the value by 2^60 for suffixes E
103 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
104 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
105 * 'T' double
106 * user mode accepts an optional ms, us, ns suffix,
107 * which divides the value by 1e3, 1e6, 1e9, respectively
108 * '/' optional gdb-like print format (like "/10x")
110 * '?' optional type (for all types, except '/')
111 * '.' other form of optional type (for 'i' and 'l')
112 * 'b' boolean
113 * user mode accepts "on" or "off"
114 * '-' optional parameter (eg. '-f')
118 typedef struct mon_cmd_t {
119 const char *name;
120 const char *args_type;
121 const char *params;
122 const char *help;
123 union {
124 void (*cmd)(Monitor *mon, const QDict *qdict);
125 void (*cmd_new)(QDict *params, QObject **ret_data, Error **errp);
126 } mhandler;
127 /* @sub_table is a list of 2nd level of commands. If it do not exist,
128 * mhandler should be used. If it exist, sub_table[?].mhandler should be
129 * used, and mhandler of 1st level plays the role of help function.
131 struct mon_cmd_t *sub_table;
132 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
133 } mon_cmd_t;
135 /* file descriptors passed via SCM_RIGHTS */
136 typedef struct mon_fd_t mon_fd_t;
137 struct mon_fd_t {
138 char *name;
139 int fd;
140 QLIST_ENTRY(mon_fd_t) next;
143 /* file descriptor associated with a file descriptor set */
144 typedef struct MonFdsetFd MonFdsetFd;
145 struct MonFdsetFd {
146 int fd;
147 bool removed;
148 char *opaque;
149 QLIST_ENTRY(MonFdsetFd) next;
152 /* file descriptor set containing fds passed via SCM_RIGHTS */
153 typedef struct MonFdset MonFdset;
154 struct MonFdset {
155 int64_t id;
156 QLIST_HEAD(, MonFdsetFd) fds;
157 QLIST_HEAD(, MonFdsetFd) dup_fds;
158 QLIST_ENTRY(MonFdset) next;
161 typedef struct {
162 QObject *id;
163 JSONMessageParser parser;
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
167 * mode.
169 bool in_command_mode; /* are we in command mode? */
170 } MonitorQMP;
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
175 * instance.
177 typedef struct MonitorQAPIEventState {
178 QAPIEvent event; /* Event being tracked */
179 int64_t rate; /* Minimum time (in ns) between two events */
180 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
181 QEMUTimer *timer; /* Timer for handling delayed events */
182 QObject *data; /* Event pending delayed dispatch */
183 } MonitorQAPIEventState;
185 struct Monitor {
186 CharDriverState *chr;
187 int reset_seen;
188 int flags;
189 int suspend_cnt;
190 bool skip_flush;
192 QemuMutex out_lock;
193 QString *outbuf;
194 guint out_watch;
196 /* Read under either BQL or out_lock, written with BQL+out_lock. */
197 int mux_out;
199 ReadLineState *rs;
200 MonitorQMP qmp;
201 CPUState *mon_cpu;
202 BlockCompletionFunc *password_completion_cb;
203 void *password_opaque;
204 mon_cmd_t *cmd_table;
205 QLIST_HEAD(,mon_fd_t) fds;
206 QLIST_ENTRY(Monitor) entry;
209 /* QMP checker flags */
210 #define QMP_ACCEPT_UNKNOWNS 1
212 /* Protects mon_list, monitor_event_state. */
213 static QemuMutex monitor_lock;
215 static QLIST_HEAD(mon_list, Monitor) mon_list;
216 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
217 static int mon_refcount;
219 static mon_cmd_t mon_cmds[];
220 static mon_cmd_t info_cmds[];
222 static const mon_cmd_t qmp_cmds[];
224 Monitor *cur_mon;
226 static void monitor_command_cb(void *opaque, const char *cmdline,
227 void *readline_opaque);
230 * Is @mon a QMP monitor?
232 static inline bool monitor_is_qmp(const Monitor *mon)
234 return (mon->flags & MONITOR_USE_CONTROL);
238 * Is the current monitor, if any, a QMP monitor?
240 bool monitor_cur_is_qmp(void)
242 return cur_mon && monitor_is_qmp(cur_mon);
245 void monitor_read_command(Monitor *mon, int show_prompt)
247 if (!mon->rs)
248 return;
250 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
251 if (show_prompt)
252 readline_show_prompt(mon->rs);
255 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
256 void *opaque)
258 if (mon->rs) {
259 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
260 /* prompt is printed on return from the command handler */
261 return 0;
262 } else {
263 monitor_printf(mon, "terminal does not support password prompting\n");
264 return -ENOTTY;
268 static void monitor_flush_locked(Monitor *mon);
270 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
271 void *opaque)
273 Monitor *mon = opaque;
275 qemu_mutex_lock(&mon->out_lock);
276 mon->out_watch = 0;
277 monitor_flush_locked(mon);
278 qemu_mutex_unlock(&mon->out_lock);
279 return FALSE;
282 /* Called with mon->out_lock held. */
283 static void monitor_flush_locked(Monitor *mon)
285 int rc;
286 size_t len;
287 const char *buf;
289 if (mon->skip_flush) {
290 return;
293 buf = qstring_get_str(mon->outbuf);
294 len = qstring_get_length(mon->outbuf);
296 if (len && !mon->mux_out) {
297 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
298 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
299 /* all flushed or error */
300 QDECREF(mon->outbuf);
301 mon->outbuf = qstring_new();
302 return;
304 if (rc > 0) {
305 /* partinal write */
306 QString *tmp = qstring_from_str(buf + rc);
307 QDECREF(mon->outbuf);
308 mon->outbuf = tmp;
310 if (mon->out_watch == 0) {
311 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
312 monitor_unblocked, mon);
317 void monitor_flush(Monitor *mon)
319 qemu_mutex_lock(&mon->out_lock);
320 monitor_flush_locked(mon);
321 qemu_mutex_unlock(&mon->out_lock);
324 /* flush at every end of line */
325 static void monitor_puts(Monitor *mon, const char *str)
327 char c;
329 qemu_mutex_lock(&mon->out_lock);
330 for(;;) {
331 c = *str++;
332 if (c == '\0')
333 break;
334 if (c == '\n') {
335 qstring_append_chr(mon->outbuf, '\r');
337 qstring_append_chr(mon->outbuf, c);
338 if (c == '\n') {
339 monitor_flush_locked(mon);
342 qemu_mutex_unlock(&mon->out_lock);
345 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
347 char *buf;
349 if (!mon)
350 return;
352 if (monitor_is_qmp(mon)) {
353 return;
356 buf = g_strdup_vprintf(fmt, ap);
357 monitor_puts(mon, buf);
358 g_free(buf);
361 void monitor_printf(Monitor *mon, const char *fmt, ...)
363 va_list ap;
364 va_start(ap, fmt);
365 monitor_vprintf(mon, fmt, ap);
366 va_end(ap);
369 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
370 const char *fmt, ...)
372 va_list ap;
373 va_start(ap, fmt);
374 monitor_vprintf((Monitor *)stream, fmt, ap);
375 va_end(ap);
376 return 0;
379 static void monitor_json_emitter(Monitor *mon, const QObject *data)
381 QString *json;
383 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
384 qobject_to_json(data);
385 assert(json != NULL);
387 qstring_append_chr(json, '\n');
388 monitor_puts(mon, qstring_get_str(json));
390 QDECREF(json);
393 static QDict *build_qmp_error_dict(Error *err)
395 QObject *obj;
397 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
398 ErrorClass_lookup[error_get_class(err)],
399 error_get_pretty(err));
401 return qobject_to_qdict(obj);
404 static void monitor_protocol_emitter(Monitor *mon, QObject *data,
405 Error *err)
407 QDict *qmp;
409 trace_monitor_protocol_emitter(mon);
411 if (!err) {
412 /* success response */
413 qmp = qdict_new();
414 if (data) {
415 qobject_incref(data);
416 qdict_put_obj(qmp, "return", data);
417 } else {
418 /* return an empty QDict by default */
419 qdict_put(qmp, "return", qdict_new());
421 } else {
422 /* error response */
423 qmp = build_qmp_error_dict(err);
426 if (mon->qmp.id) {
427 qdict_put_obj(qmp, "id", mon->qmp.id);
428 mon->qmp.id = NULL;
431 monitor_json_emitter(mon, QOBJECT(qmp));
432 QDECREF(qmp);
436 static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
439 * Emits the event to every monitor instance, @event is only used for trace
440 * Called with monitor_lock held.
442 static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
444 Monitor *mon;
446 trace_monitor_protocol_event_emit(event, data);
447 QLIST_FOREACH(mon, &mon_list, entry) {
448 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
449 monitor_json_emitter(mon, data);
455 * Queue a new event for emission to Monitor instances,
456 * applying any rate limiting if required.
458 static void
459 monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
461 MonitorQAPIEventState *evstate;
462 assert(event < QAPI_EVENT_MAX);
463 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
465 evstate = &(monitor_qapi_event_state[event]);
466 trace_monitor_protocol_event_queue(event,
467 data,
468 evstate->rate,
469 evstate->last,
470 now);
472 /* Rate limit of 0 indicates no throttling */
473 qemu_mutex_lock(&monitor_lock);
474 if (!evstate->rate) {
475 monitor_qapi_event_emit(event, QOBJECT(data));
476 evstate->last = now;
477 } else {
478 int64_t delta = now - evstate->last;
479 if (evstate->data ||
480 delta < evstate->rate) {
481 /* If there's an existing event pending, replace
482 * it with the new event, otherwise schedule a
483 * timer for delayed emission
485 if (evstate->data) {
486 qobject_decref(evstate->data);
487 } else {
488 int64_t then = evstate->last + evstate->rate;
489 timer_mod_ns(evstate->timer, then);
491 evstate->data = QOBJECT(data);
492 qobject_incref(evstate->data);
493 } else {
494 monitor_qapi_event_emit(event, QOBJECT(data));
495 evstate->last = now;
498 qemu_mutex_unlock(&monitor_lock);
502 * The callback invoked by QemuTimer when a delayed
503 * event is ready to be emitted
505 static void monitor_qapi_event_handler(void *opaque)
507 MonitorQAPIEventState *evstate = opaque;
508 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
510 trace_monitor_protocol_event_handler(evstate->event,
511 evstate->data,
512 evstate->last,
513 now);
514 qemu_mutex_lock(&monitor_lock);
515 if (evstate->data) {
516 monitor_qapi_event_emit(evstate->event, evstate->data);
517 qobject_decref(evstate->data);
518 evstate->data = NULL;
520 evstate->last = now;
521 qemu_mutex_unlock(&monitor_lock);
525 * @event: the event ID to be limited
526 * @rate: the rate limit in milliseconds
528 * Sets a rate limit on a particular event, so no
529 * more than 1 event will be emitted within @rate
530 * milliseconds
532 static void
533 monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
535 MonitorQAPIEventState *evstate;
536 assert(event < QAPI_EVENT_MAX);
538 evstate = &(monitor_qapi_event_state[event]);
540 trace_monitor_protocol_event_throttle(event, rate);
541 evstate->event = event;
542 assert(rate * SCALE_MS <= INT64_MAX);
543 evstate->rate = rate * SCALE_MS;
544 evstate->last = 0;
545 evstate->data = NULL;
546 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
547 SCALE_MS,
548 monitor_qapi_event_handler,
549 evstate);
552 static void monitor_qapi_event_init(void)
554 /* Limit guest-triggerable events to 1 per second */
555 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
556 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
557 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
558 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
560 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
562 qmp_event_set_func_emit(monitor_qapi_event_queue);
565 static void qmp_capabilities(QDict *params, QObject **ret_data, Error **errp)
567 cur_mon->qmp.in_command_mode = true;
570 static void handle_hmp_command(Monitor *mon, const char *cmdline);
572 static void monitor_data_init(Monitor *mon)
574 memset(mon, 0, sizeof(Monitor));
575 qemu_mutex_init(&mon->out_lock);
576 mon->outbuf = qstring_new();
577 /* Use *mon_cmds by default. */
578 mon->cmd_table = mon_cmds;
581 static void monitor_data_destroy(Monitor *mon)
583 QDECREF(mon->outbuf);
584 qemu_mutex_destroy(&mon->out_lock);
587 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
588 int64_t cpu_index, Error **errp)
590 char *output = NULL;
591 Monitor *old_mon, hmp;
593 monitor_data_init(&hmp);
594 hmp.skip_flush = true;
596 old_mon = cur_mon;
597 cur_mon = &hmp;
599 if (has_cpu_index) {
600 int ret = monitor_set_cpu(cpu_index);
601 if (ret < 0) {
602 cur_mon = old_mon;
603 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
604 "a CPU number");
605 goto out;
609 handle_hmp_command(&hmp, command_line);
610 cur_mon = old_mon;
612 qemu_mutex_lock(&hmp.out_lock);
613 if (qstring_get_length(hmp.outbuf) > 0) {
614 output = g_strdup(qstring_get_str(hmp.outbuf));
615 } else {
616 output = g_strdup("");
618 qemu_mutex_unlock(&hmp.out_lock);
620 out:
621 monitor_data_destroy(&hmp);
622 return output;
625 static int compare_cmd(const char *name, const char *list)
627 const char *p, *pstart;
628 int len;
629 len = strlen(name);
630 p = list;
631 for(;;) {
632 pstart = p;
633 p = strchr(p, '|');
634 if (!p)
635 p = pstart + strlen(pstart);
636 if ((p - pstart) == len && !memcmp(pstart, name, len))
637 return 1;
638 if (*p == '\0')
639 break;
640 p++;
642 return 0;
645 static int get_str(char *buf, int buf_size, const char **pp)
647 const char *p;
648 char *q;
649 int c;
651 q = buf;
652 p = *pp;
653 while (qemu_isspace(*p)) {
654 p++;
656 if (*p == '\0') {
657 fail:
658 *q = '\0';
659 *pp = p;
660 return -1;
662 if (*p == '\"') {
663 p++;
664 while (*p != '\0' && *p != '\"') {
665 if (*p == '\\') {
666 p++;
667 c = *p++;
668 switch (c) {
669 case 'n':
670 c = '\n';
671 break;
672 case 'r':
673 c = '\r';
674 break;
675 case '\\':
676 case '\'':
677 case '\"':
678 break;
679 default:
680 qemu_printf("unsupported escape code: '\\%c'\n", c);
681 goto fail;
683 if ((q - buf) < buf_size - 1) {
684 *q++ = c;
686 } else {
687 if ((q - buf) < buf_size - 1) {
688 *q++ = *p;
690 p++;
693 if (*p != '\"') {
694 qemu_printf("unterminated string\n");
695 goto fail;
697 p++;
698 } else {
699 while (*p != '\0' && !qemu_isspace(*p)) {
700 if ((q - buf) < buf_size - 1) {
701 *q++ = *p;
703 p++;
706 *q = '\0';
707 *pp = p;
708 return 0;
711 #define MAX_ARGS 16
713 static void free_cmdline_args(char **args, int nb_args)
715 int i;
717 assert(nb_args <= MAX_ARGS);
719 for (i = 0; i < nb_args; i++) {
720 g_free(args[i]);
726 * Parse the command line to get valid args.
727 * @cmdline: command line to be parsed.
728 * @pnb_args: location to store the number of args, must NOT be NULL.
729 * @args: location to store the args, which should be freed by caller, must
730 * NOT be NULL.
732 * Returns 0 on success, negative on failure.
734 * NOTE: this parser is an approximate form of the real command parser. Number
735 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
736 * return with failure.
738 static int parse_cmdline(const char *cmdline,
739 int *pnb_args, char **args)
741 const char *p;
742 int nb_args, ret;
743 char buf[1024];
745 p = cmdline;
746 nb_args = 0;
747 for (;;) {
748 while (qemu_isspace(*p)) {
749 p++;
751 if (*p == '\0') {
752 break;
754 if (nb_args >= MAX_ARGS) {
755 goto fail;
757 ret = get_str(buf, sizeof(buf), &p);
758 if (ret < 0) {
759 goto fail;
761 args[nb_args] = g_strdup(buf);
762 nb_args++;
764 *pnb_args = nb_args;
765 return 0;
767 fail:
768 free_cmdline_args(args, nb_args);
769 return -1;
772 static void help_cmd_dump_one(Monitor *mon,
773 const mon_cmd_t *cmd,
774 char **prefix_args,
775 int prefix_args_nb)
777 int i;
779 for (i = 0; i < prefix_args_nb; i++) {
780 monitor_printf(mon, "%s ", prefix_args[i]);
782 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
785 /* @args[@arg_index] is the valid command need to find in @cmds */
786 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
787 char **args, int nb_args, int arg_index)
789 const mon_cmd_t *cmd;
791 /* No valid arg need to compare with, dump all in *cmds */
792 if (arg_index >= nb_args) {
793 for (cmd = cmds; cmd->name != NULL; cmd++) {
794 help_cmd_dump_one(mon, cmd, args, arg_index);
796 return;
799 /* Find one entry to dump */
800 for (cmd = cmds; cmd->name != NULL; cmd++) {
801 if (compare_cmd(args[arg_index], cmd->name)) {
802 if (cmd->sub_table) {
803 /* continue with next arg */
804 help_cmd_dump(mon, cmd->sub_table,
805 args, nb_args, arg_index + 1);
806 } else {
807 help_cmd_dump_one(mon, cmd, args, arg_index);
809 break;
814 static void help_cmd(Monitor *mon, const char *name)
816 char *args[MAX_ARGS];
817 int nb_args = 0;
819 /* 1. parse user input */
820 if (name) {
821 /* special case for log, directly dump and return */
822 if (!strcmp(name, "log")) {
823 const QEMULogItem *item;
824 monitor_printf(mon, "Log items (comma separated):\n");
825 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
826 for (item = qemu_log_items; item->mask != 0; item++) {
827 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
829 return;
832 if (parse_cmdline(name, &nb_args, args) < 0) {
833 return;
837 /* 2. dump the contents according to parsed args */
838 help_cmd_dump(mon, mon->cmd_table, args, nb_args, 0);
840 free_cmdline_args(args, nb_args);
843 static void do_help_cmd(Monitor *mon, const QDict *qdict)
845 help_cmd(mon, qdict_get_try_str(qdict, "name"));
848 static void hmp_trace_event(Monitor *mon, const QDict *qdict)
850 const char *tp_name = qdict_get_str(qdict, "name");
851 bool new_state = qdict_get_bool(qdict, "option");
852 Error *local_err = NULL;
854 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
855 if (local_err) {
856 error_report_err(local_err);
860 #ifdef CONFIG_TRACE_SIMPLE
861 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
863 const char *op = qdict_get_try_str(qdict, "op");
864 const char *arg = qdict_get_try_str(qdict, "arg");
866 if (!op) {
867 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
868 } else if (!strcmp(op, "on")) {
869 st_set_trace_file_enabled(true);
870 } else if (!strcmp(op, "off")) {
871 st_set_trace_file_enabled(false);
872 } else if (!strcmp(op, "flush")) {
873 st_flush_trace_buffer();
874 } else if (!strcmp(op, "set")) {
875 if (arg) {
876 st_set_trace_file(arg);
878 } else {
879 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
880 help_cmd(mon, "trace-file");
883 #endif
885 static void hmp_info_help(Monitor *mon, const QDict *qdict)
887 help_cmd(mon, "info");
890 CommandInfoList *qmp_query_commands(Error **errp)
892 CommandInfoList *info, *cmd_list = NULL;
893 const mon_cmd_t *cmd;
895 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
896 info = g_malloc0(sizeof(*info));
897 info->value = g_malloc0(sizeof(*info->value));
898 info->value->name = g_strdup(cmd->name);
900 info->next = cmd_list;
901 cmd_list = info;
904 return cmd_list;
907 EventInfoList *qmp_query_events(Error **errp)
909 EventInfoList *info, *ev_list = NULL;
910 QAPIEvent e;
912 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
913 const char *event_name = QAPIEvent_lookup[e];
914 assert(event_name != NULL);
915 info = g_malloc0(sizeof(*info));
916 info->value = g_malloc0(sizeof(*info->value));
917 info->value->name = g_strdup(event_name);
919 info->next = ev_list;
920 ev_list = info;
923 return ev_list;
926 /* set the current CPU defined by the user */
927 int monitor_set_cpu(int cpu_index)
929 CPUState *cpu;
931 cpu = qemu_get_cpu(cpu_index);
932 if (cpu == NULL) {
933 return -1;
935 cur_mon->mon_cpu = cpu;
936 return 0;
939 static CPUState *mon_get_cpu(void)
941 if (!cur_mon->mon_cpu) {
942 monitor_set_cpu(0);
944 cpu_synchronize_state(cur_mon->mon_cpu);
945 return cur_mon->mon_cpu;
948 static CPUArchState *mon_get_cpu_env(void)
950 return mon_get_cpu()->env_ptr;
953 int monitor_get_cpu_index(void)
955 return mon_get_cpu()->cpu_index;
958 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
960 cpu_dump_state(mon_get_cpu(), (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
963 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
965 dump_exec_info((FILE *)mon, monitor_fprintf);
966 dump_drift_info((FILE *)mon, monitor_fprintf);
969 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
971 dump_opcount_info((FILE *)mon, monitor_fprintf);
974 static void hmp_info_history(Monitor *mon, const QDict *qdict)
976 int i;
977 const char *str;
979 if (!mon->rs)
980 return;
981 i = 0;
982 for(;;) {
983 str = readline_get_history(mon->rs, i);
984 if (!str)
985 break;
986 monitor_printf(mon, "%d: '%s'\n", i, str);
987 i++;
991 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
993 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon, &monitor_fprintf, 0);
996 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
998 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
999 TraceEventInfoList *elem;
1001 for (elem = events; elem != NULL; elem = elem->next) {
1002 monitor_printf(mon, "%s : state %u\n",
1003 elem->value->name,
1004 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1006 qapi_free_TraceEventInfoList(events);
1009 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1010 bool has_port, int64_t port,
1011 bool has_tls_port, int64_t tls_port,
1012 bool has_cert_subject, const char *cert_subject,
1013 Error **errp)
1015 if (strcmp(protocol, "spice") == 0) {
1016 if (!qemu_using_spice(errp)) {
1017 return;
1020 if (!has_port && !has_tls_port) {
1021 error_setg(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1022 return;
1025 if (qemu_spice_migrate_info(hostname,
1026 has_port ? port : -1,
1027 has_tls_port ? tls_port : -1,
1028 cert_subject)) {
1029 error_setg(errp, QERR_UNDEFINED_ERROR);
1030 return;
1032 return;
1035 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1038 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1040 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1043 static void hmp_log(Monitor *mon, const QDict *qdict)
1045 int mask;
1046 const char *items = qdict_get_str(qdict, "items");
1048 if (!strcmp(items, "none")) {
1049 mask = 0;
1050 } else {
1051 mask = qemu_str_to_log_mask(items);
1052 if (!mask) {
1053 help_cmd(mon, "log");
1054 return;
1057 qemu_set_log(mask);
1060 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1062 const char *option = qdict_get_try_str(qdict, "option");
1063 if (!option || !strcmp(option, "on")) {
1064 singlestep = 1;
1065 } else if (!strcmp(option, "off")) {
1066 singlestep = 0;
1067 } else {
1068 monitor_printf(mon, "unexpected option %s\n", option);
1072 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1074 const char *device = qdict_get_try_str(qdict, "device");
1075 if (!device)
1076 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1077 if (gdbserver_start(device) < 0) {
1078 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1079 device);
1080 } else if (strcmp(device, "none") == 0) {
1081 monitor_printf(mon, "Disabled gdbserver\n");
1082 } else {
1083 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1084 device);
1088 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1090 const char *action = qdict_get_str(qdict, "action");
1091 if (select_watchdog_action(action) == -1) {
1092 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1096 static void monitor_printc(Monitor *mon, int c)
1098 monitor_printf(mon, "'");
1099 switch(c) {
1100 case '\'':
1101 monitor_printf(mon, "\\'");
1102 break;
1103 case '\\':
1104 monitor_printf(mon, "\\\\");
1105 break;
1106 case '\n':
1107 monitor_printf(mon, "\\n");
1108 break;
1109 case '\r':
1110 monitor_printf(mon, "\\r");
1111 break;
1112 default:
1113 if (c >= 32 && c <= 126) {
1114 monitor_printf(mon, "%c", c);
1115 } else {
1116 monitor_printf(mon, "\\x%02x", c);
1118 break;
1120 monitor_printf(mon, "'");
1123 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1124 hwaddr addr, int is_physical)
1126 int l, line_size, i, max_digits, len;
1127 uint8_t buf[16];
1128 uint64_t v;
1130 if (format == 'i') {
1131 int flags = 0;
1132 #ifdef TARGET_I386
1133 CPUArchState *env = mon_get_cpu_env();
1134 if (wsize == 2) {
1135 flags = 1;
1136 } else if (wsize == 4) {
1137 flags = 0;
1138 } else {
1139 /* as default we use the current CS size */
1140 flags = 0;
1141 if (env) {
1142 #ifdef TARGET_X86_64
1143 if ((env->efer & MSR_EFER_LMA) &&
1144 (env->segs[R_CS].flags & DESC_L_MASK))
1145 flags = 2;
1146 else
1147 #endif
1148 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1149 flags = 1;
1152 #endif
1153 #ifdef TARGET_PPC
1154 CPUArchState *env = mon_get_cpu_env();
1155 flags = msr_le << 16;
1156 flags |= env->bfd_mach;
1157 #endif
1158 monitor_disas(mon, mon_get_cpu(), addr, count, is_physical, flags);
1159 return;
1162 len = wsize * count;
1163 if (wsize == 1)
1164 line_size = 8;
1165 else
1166 line_size = 16;
1167 max_digits = 0;
1169 switch(format) {
1170 case 'o':
1171 max_digits = (wsize * 8 + 2) / 3;
1172 break;
1173 default:
1174 case 'x':
1175 max_digits = (wsize * 8) / 4;
1176 break;
1177 case 'u':
1178 case 'd':
1179 max_digits = (wsize * 8 * 10 + 32) / 33;
1180 break;
1181 case 'c':
1182 wsize = 1;
1183 break;
1186 while (len > 0) {
1187 if (is_physical)
1188 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1189 else
1190 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1191 l = len;
1192 if (l > line_size)
1193 l = line_size;
1194 if (is_physical) {
1195 cpu_physical_memory_read(addr, buf, l);
1196 } else {
1197 if (cpu_memory_rw_debug(mon_get_cpu(), addr, buf, l, 0) < 0) {
1198 monitor_printf(mon, " Cannot access memory\n");
1199 break;
1202 i = 0;
1203 while (i < l) {
1204 switch(wsize) {
1205 default:
1206 case 1:
1207 v = ldub_p(buf + i);
1208 break;
1209 case 2:
1210 v = lduw_p(buf + i);
1211 break;
1212 case 4:
1213 v = (uint32_t)ldl_p(buf + i);
1214 break;
1215 case 8:
1216 v = ldq_p(buf + i);
1217 break;
1219 monitor_printf(mon, " ");
1220 switch(format) {
1221 case 'o':
1222 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1223 break;
1224 case 'x':
1225 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1226 break;
1227 case 'u':
1228 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1229 break;
1230 case 'd':
1231 monitor_printf(mon, "%*" PRId64, max_digits, v);
1232 break;
1233 case 'c':
1234 monitor_printc(mon, v);
1235 break;
1237 i += wsize;
1239 monitor_printf(mon, "\n");
1240 addr += l;
1241 len -= l;
1245 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1247 int count = qdict_get_int(qdict, "count");
1248 int format = qdict_get_int(qdict, "format");
1249 int size = qdict_get_int(qdict, "size");
1250 target_long addr = qdict_get_int(qdict, "addr");
1252 memory_dump(mon, count, format, size, addr, 0);
1255 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1257 int count = qdict_get_int(qdict, "count");
1258 int format = qdict_get_int(qdict, "format");
1259 int size = qdict_get_int(qdict, "size");
1260 hwaddr addr = qdict_get_int(qdict, "addr");
1262 memory_dump(mon, count, format, size, addr, 1);
1265 static void do_print(Monitor *mon, const QDict *qdict)
1267 int format = qdict_get_int(qdict, "format");
1268 hwaddr val = qdict_get_int(qdict, "val");
1270 switch(format) {
1271 case 'o':
1272 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1273 break;
1274 case 'x':
1275 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1276 break;
1277 case 'u':
1278 monitor_printf(mon, "%" HWADDR_PRIu, val);
1279 break;
1280 default:
1281 case 'd':
1282 monitor_printf(mon, "%" HWADDR_PRId, val);
1283 break;
1284 case 'c':
1285 monitor_printc(mon, val);
1286 break;
1288 monitor_printf(mon, "\n");
1291 static void hmp_sum(Monitor *mon, const QDict *qdict)
1293 uint32_t addr;
1294 uint16_t sum;
1295 uint32_t start = qdict_get_int(qdict, "start");
1296 uint32_t size = qdict_get_int(qdict, "size");
1298 sum = 0;
1299 for(addr = start; addr < (start + size); addr++) {
1300 uint8_t val = address_space_ldub(&address_space_memory, addr,
1301 MEMTXATTRS_UNSPECIFIED, NULL);
1302 /* BSD sum algorithm ('sum' Unix command) */
1303 sum = (sum >> 1) | (sum << 15);
1304 sum += val;
1306 monitor_printf(mon, "%05d\n", sum);
1309 static int mouse_button_state;
1311 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1313 int dx, dy, dz, button;
1314 const char *dx_str = qdict_get_str(qdict, "dx_str");
1315 const char *dy_str = qdict_get_str(qdict, "dy_str");
1316 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1318 dx = strtol(dx_str, NULL, 0);
1319 dy = strtol(dy_str, NULL, 0);
1320 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1321 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1323 if (dz_str) {
1324 dz = strtol(dz_str, NULL, 0);
1325 if (dz != 0) {
1326 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1327 qemu_input_queue_btn(NULL, button, true);
1328 qemu_input_event_sync();
1329 qemu_input_queue_btn(NULL, button, false);
1332 qemu_input_event_sync();
1335 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1337 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1338 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1339 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1340 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1342 int button_state = qdict_get_int(qdict, "button_state");
1344 if (mouse_button_state == button_state) {
1345 return;
1347 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1348 qemu_input_event_sync();
1349 mouse_button_state = button_state;
1352 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1354 int size = qdict_get_int(qdict, "size");
1355 int addr = qdict_get_int(qdict, "addr");
1356 int has_index = qdict_haskey(qdict, "index");
1357 uint32_t val;
1358 int suffix;
1360 if (has_index) {
1361 int index = qdict_get_int(qdict, "index");
1362 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1363 addr++;
1365 addr &= 0xffff;
1367 switch(size) {
1368 default:
1369 case 1:
1370 val = cpu_inb(addr);
1371 suffix = 'b';
1372 break;
1373 case 2:
1374 val = cpu_inw(addr);
1375 suffix = 'w';
1376 break;
1377 case 4:
1378 val = cpu_inl(addr);
1379 suffix = 'l';
1380 break;
1382 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1383 suffix, addr, size * 2, val);
1386 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1388 int size = qdict_get_int(qdict, "size");
1389 int addr = qdict_get_int(qdict, "addr");
1390 int val = qdict_get_int(qdict, "val");
1392 addr &= IOPORTS_MASK;
1394 switch (size) {
1395 default:
1396 case 1:
1397 cpu_outb(addr, val);
1398 break;
1399 case 2:
1400 cpu_outw(addr, val);
1401 break;
1402 case 4:
1403 cpu_outl(addr, val);
1404 break;
1408 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1410 Error *local_err = NULL;
1411 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1413 qemu_boot_set(bootdevice, &local_err);
1414 if (local_err) {
1415 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1416 error_free(local_err);
1417 } else {
1418 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1422 #if defined(TARGET_I386)
1423 static void print_pte(Monitor *mon, hwaddr addr,
1424 hwaddr pte,
1425 hwaddr mask)
1427 #ifdef TARGET_X86_64
1428 if (addr & (1ULL << 47)) {
1429 addr |= -1LL << 48;
1431 #endif
1432 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1433 " %c%c%c%c%c%c%c%c%c\n",
1434 addr,
1435 pte & mask,
1436 pte & PG_NX_MASK ? 'X' : '-',
1437 pte & PG_GLOBAL_MASK ? 'G' : '-',
1438 pte & PG_PSE_MASK ? 'P' : '-',
1439 pte & PG_DIRTY_MASK ? 'D' : '-',
1440 pte & PG_ACCESSED_MASK ? 'A' : '-',
1441 pte & PG_PCD_MASK ? 'C' : '-',
1442 pte & PG_PWT_MASK ? 'T' : '-',
1443 pte & PG_USER_MASK ? 'U' : '-',
1444 pte & PG_RW_MASK ? 'W' : '-');
1447 static void tlb_info_32(Monitor *mon, CPUArchState *env)
1449 unsigned int l1, l2;
1450 uint32_t pgd, pde, pte;
1452 pgd = env->cr[3] & ~0xfff;
1453 for(l1 = 0; l1 < 1024; l1++) {
1454 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1455 pde = le32_to_cpu(pde);
1456 if (pde & PG_PRESENT_MASK) {
1457 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1458 /* 4M pages */
1459 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1460 } else {
1461 for(l2 = 0; l2 < 1024; l2++) {
1462 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1463 pte = le32_to_cpu(pte);
1464 if (pte & PG_PRESENT_MASK) {
1465 print_pte(mon, (l1 << 22) + (l2 << 12),
1466 pte & ~PG_PSE_MASK,
1467 ~0xfff);
1475 static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
1477 unsigned int l1, l2, l3;
1478 uint64_t pdpe, pde, pte;
1479 uint64_t pdp_addr, pd_addr, pt_addr;
1481 pdp_addr = env->cr[3] & ~0x1f;
1482 for (l1 = 0; l1 < 4; l1++) {
1483 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1484 pdpe = le64_to_cpu(pdpe);
1485 if (pdpe & PG_PRESENT_MASK) {
1486 pd_addr = pdpe & 0x3fffffffff000ULL;
1487 for (l2 = 0; l2 < 512; l2++) {
1488 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1489 pde = le64_to_cpu(pde);
1490 if (pde & PG_PRESENT_MASK) {
1491 if (pde & PG_PSE_MASK) {
1492 /* 2M pages with PAE, CR4.PSE is ignored */
1493 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1494 ~((hwaddr)(1 << 20) - 1));
1495 } else {
1496 pt_addr = pde & 0x3fffffffff000ULL;
1497 for (l3 = 0; l3 < 512; l3++) {
1498 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1499 pte = le64_to_cpu(pte);
1500 if (pte & PG_PRESENT_MASK) {
1501 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1502 + (l3 << 12),
1503 pte & ~PG_PSE_MASK,
1504 ~(hwaddr)0xfff);
1514 #ifdef TARGET_X86_64
1515 static void tlb_info_64(Monitor *mon, CPUArchState *env)
1517 uint64_t l1, l2, l3, l4;
1518 uint64_t pml4e, pdpe, pde, pte;
1519 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1521 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1522 for (l1 = 0; l1 < 512; l1++) {
1523 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1524 pml4e = le64_to_cpu(pml4e);
1525 if (pml4e & PG_PRESENT_MASK) {
1526 pdp_addr = pml4e & 0x3fffffffff000ULL;
1527 for (l2 = 0; l2 < 512; l2++) {
1528 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1529 pdpe = le64_to_cpu(pdpe);
1530 if (pdpe & PG_PRESENT_MASK) {
1531 if (pdpe & PG_PSE_MASK) {
1532 /* 1G pages, CR4.PSE is ignored */
1533 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1534 0x3ffffc0000000ULL);
1535 } else {
1536 pd_addr = pdpe & 0x3fffffffff000ULL;
1537 for (l3 = 0; l3 < 512; l3++) {
1538 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1539 pde = le64_to_cpu(pde);
1540 if (pde & PG_PRESENT_MASK) {
1541 if (pde & PG_PSE_MASK) {
1542 /* 2M pages, CR4.PSE is ignored */
1543 print_pte(mon, (l1 << 39) + (l2 << 30) +
1544 (l3 << 21), pde,
1545 0x3ffffffe00000ULL);
1546 } else {
1547 pt_addr = pde & 0x3fffffffff000ULL;
1548 for (l4 = 0; l4 < 512; l4++) {
1549 cpu_physical_memory_read(pt_addr
1550 + l4 * 8,
1551 &pte, 8);
1552 pte = le64_to_cpu(pte);
1553 if (pte & PG_PRESENT_MASK) {
1554 print_pte(mon, (l1 << 39) +
1555 (l2 << 30) +
1556 (l3 << 21) + (l4 << 12),
1557 pte & ~PG_PSE_MASK,
1558 0x3fffffffff000ULL);
1570 #endif
1572 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1574 CPUArchState *env;
1576 env = mon_get_cpu_env();
1578 if (!(env->cr[0] & CR0_PG_MASK)) {
1579 monitor_printf(mon, "PG disabled\n");
1580 return;
1582 if (env->cr[4] & CR4_PAE_MASK) {
1583 #ifdef TARGET_X86_64
1584 if (env->hflags & HF_LMA_MASK) {
1585 tlb_info_64(mon, env);
1586 } else
1587 #endif
1589 tlb_info_pae32(mon, env);
1591 } else {
1592 tlb_info_32(mon, env);
1596 static void mem_print(Monitor *mon, hwaddr *pstart,
1597 int *plast_prot,
1598 hwaddr end, int prot)
1600 int prot1;
1601 prot1 = *plast_prot;
1602 if (prot != prot1) {
1603 if (*pstart != -1) {
1604 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1605 TARGET_FMT_plx " %c%c%c\n",
1606 *pstart, end, end - *pstart,
1607 prot1 & PG_USER_MASK ? 'u' : '-',
1608 'r',
1609 prot1 & PG_RW_MASK ? 'w' : '-');
1611 if (prot != 0)
1612 *pstart = end;
1613 else
1614 *pstart = -1;
1615 *plast_prot = prot;
1619 static void mem_info_32(Monitor *mon, CPUArchState *env)
1621 unsigned int l1, l2;
1622 int prot, last_prot;
1623 uint32_t pgd, pde, pte;
1624 hwaddr start, end;
1626 pgd = env->cr[3] & ~0xfff;
1627 last_prot = 0;
1628 start = -1;
1629 for(l1 = 0; l1 < 1024; l1++) {
1630 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1631 pde = le32_to_cpu(pde);
1632 end = l1 << 22;
1633 if (pde & PG_PRESENT_MASK) {
1634 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1635 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1636 mem_print(mon, &start, &last_prot, end, prot);
1637 } else {
1638 for(l2 = 0; l2 < 1024; l2++) {
1639 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1640 pte = le32_to_cpu(pte);
1641 end = (l1 << 22) + (l2 << 12);
1642 if (pte & PG_PRESENT_MASK) {
1643 prot = pte & pde &
1644 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1645 } else {
1646 prot = 0;
1648 mem_print(mon, &start, &last_prot, end, prot);
1651 } else {
1652 prot = 0;
1653 mem_print(mon, &start, &last_prot, end, prot);
1656 /* Flush last range */
1657 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1660 static void mem_info_pae32(Monitor *mon, CPUArchState *env)
1662 unsigned int l1, l2, l3;
1663 int prot, last_prot;
1664 uint64_t pdpe, pde, pte;
1665 uint64_t pdp_addr, pd_addr, pt_addr;
1666 hwaddr start, end;
1668 pdp_addr = env->cr[3] & ~0x1f;
1669 last_prot = 0;
1670 start = -1;
1671 for (l1 = 0; l1 < 4; l1++) {
1672 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1673 pdpe = le64_to_cpu(pdpe);
1674 end = l1 << 30;
1675 if (pdpe & PG_PRESENT_MASK) {
1676 pd_addr = pdpe & 0x3fffffffff000ULL;
1677 for (l2 = 0; l2 < 512; l2++) {
1678 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1679 pde = le64_to_cpu(pde);
1680 end = (l1 << 30) + (l2 << 21);
1681 if (pde & PG_PRESENT_MASK) {
1682 if (pde & PG_PSE_MASK) {
1683 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1684 PG_PRESENT_MASK);
1685 mem_print(mon, &start, &last_prot, end, prot);
1686 } else {
1687 pt_addr = pde & 0x3fffffffff000ULL;
1688 for (l3 = 0; l3 < 512; l3++) {
1689 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1690 pte = le64_to_cpu(pte);
1691 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1692 if (pte & PG_PRESENT_MASK) {
1693 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1694 PG_PRESENT_MASK);
1695 } else {
1696 prot = 0;
1698 mem_print(mon, &start, &last_prot, end, prot);
1701 } else {
1702 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 /* Flush last range */
1712 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1716 #ifdef TARGET_X86_64
1717 static void mem_info_64(Monitor *mon, CPUArchState *env)
1719 int prot, last_prot;
1720 uint64_t l1, l2, l3, l4;
1721 uint64_t pml4e, pdpe, pde, pte;
1722 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1724 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1725 last_prot = 0;
1726 start = -1;
1727 for (l1 = 0; l1 < 512; l1++) {
1728 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1729 pml4e = le64_to_cpu(pml4e);
1730 end = l1 << 39;
1731 if (pml4e & PG_PRESENT_MASK) {
1732 pdp_addr = pml4e & 0x3fffffffff000ULL;
1733 for (l2 = 0; l2 < 512; l2++) {
1734 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1735 pdpe = le64_to_cpu(pdpe);
1736 end = (l1 << 39) + (l2 << 30);
1737 if (pdpe & PG_PRESENT_MASK) {
1738 if (pdpe & PG_PSE_MASK) {
1739 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1740 PG_PRESENT_MASK);
1741 prot &= pml4e;
1742 mem_print(mon, &start, &last_prot, end, prot);
1743 } else {
1744 pd_addr = pdpe & 0x3fffffffff000ULL;
1745 for (l3 = 0; l3 < 512; l3++) {
1746 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1747 pde = le64_to_cpu(pde);
1748 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1749 if (pde & PG_PRESENT_MASK) {
1750 if (pde & PG_PSE_MASK) {
1751 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1752 PG_PRESENT_MASK);
1753 prot &= pml4e & pdpe;
1754 mem_print(mon, &start, &last_prot, end, prot);
1755 } else {
1756 pt_addr = pde & 0x3fffffffff000ULL;
1757 for (l4 = 0; l4 < 512; l4++) {
1758 cpu_physical_memory_read(pt_addr
1759 + l4 * 8,
1760 &pte, 8);
1761 pte = le64_to_cpu(pte);
1762 end = (l1 << 39) + (l2 << 30) +
1763 (l3 << 21) + (l4 << 12);
1764 if (pte & PG_PRESENT_MASK) {
1765 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1766 PG_PRESENT_MASK);
1767 prot &= pml4e & pdpe & pde;
1768 } else {
1769 prot = 0;
1771 mem_print(mon, &start, &last_prot, end, prot);
1774 } else {
1775 prot = 0;
1776 mem_print(mon, &start, &last_prot, end, prot);
1780 } else {
1781 prot = 0;
1782 mem_print(mon, &start, &last_prot, end, prot);
1785 } else {
1786 prot = 0;
1787 mem_print(mon, &start, &last_prot, end, prot);
1790 /* Flush last range */
1791 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
1793 #endif
1795 static void hmp_info_mem(Monitor *mon, const QDict *qdict)
1797 CPUArchState *env;
1799 env = mon_get_cpu_env();
1801 if (!(env->cr[0] & CR0_PG_MASK)) {
1802 monitor_printf(mon, "PG disabled\n");
1803 return;
1805 if (env->cr[4] & CR4_PAE_MASK) {
1806 #ifdef TARGET_X86_64
1807 if (env->hflags & HF_LMA_MASK) {
1808 mem_info_64(mon, env);
1809 } else
1810 #endif
1812 mem_info_pae32(mon, env);
1814 } else {
1815 mem_info_32(mon, env);
1818 #endif
1820 #if defined(TARGET_SH4)
1822 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1824 monitor_printf(mon, " tlb%i:\t"
1825 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1826 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1827 "dirty=%hhu writethrough=%hhu\n",
1828 idx,
1829 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1830 tlb->v, tlb->sh, tlb->c, tlb->pr,
1831 tlb->d, tlb->wt);
1834 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1836 CPUArchState *env = mon_get_cpu_env();
1837 int i;
1839 monitor_printf (mon, "ITLB:\n");
1840 for (i = 0 ; i < ITLB_SIZE ; i++)
1841 print_tlb (mon, i, &env->itlb[i]);
1842 monitor_printf (mon, "UTLB:\n");
1843 for (i = 0 ; i < UTLB_SIZE ; i++)
1844 print_tlb (mon, i, &env->utlb[i]);
1847 #endif
1849 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1850 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1852 CPUArchState *env1 = mon_get_cpu_env();
1854 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1856 #endif
1858 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1860 mtree_info((fprintf_function)monitor_printf, mon);
1863 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1865 int i;
1866 CPUState *cpu;
1867 uint64_t *node_mem;
1869 node_mem = g_new0(uint64_t, nb_numa_nodes);
1870 query_numa_node_mem(node_mem);
1871 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1872 for (i = 0; i < nb_numa_nodes; i++) {
1873 monitor_printf(mon, "node %d cpus:", i);
1874 CPU_FOREACH(cpu) {
1875 if (cpu->numa_node == i) {
1876 monitor_printf(mon, " %d", cpu->cpu_index);
1879 monitor_printf(mon, "\n");
1880 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1881 node_mem[i] >> 20);
1883 g_free(node_mem);
1886 #ifdef CONFIG_PROFILER
1888 int64_t tcg_time;
1889 int64_t dev_time;
1891 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1893 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1894 dev_time, dev_time / (double)get_ticks_per_sec());
1895 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1896 tcg_time, tcg_time / (double)get_ticks_per_sec());
1897 tcg_time = 0;
1898 dev_time = 0;
1900 #else
1901 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1903 monitor_printf(mon, "Internal profiler not compiled\n");
1905 #endif
1907 /* Capture support */
1908 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1910 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1912 int i;
1913 CaptureState *s;
1915 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1916 monitor_printf(mon, "[%d]: ", i);
1917 s->ops.info (s->opaque);
1921 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1923 int i;
1924 int n = qdict_get_int(qdict, "n");
1925 CaptureState *s;
1927 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1928 if (i == n) {
1929 s->ops.destroy (s->opaque);
1930 QLIST_REMOVE (s, entries);
1931 g_free (s);
1932 return;
1937 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1939 const char *path = qdict_get_str(qdict, "path");
1940 int has_freq = qdict_haskey(qdict, "freq");
1941 int freq = qdict_get_try_int(qdict, "freq", -1);
1942 int has_bits = qdict_haskey(qdict, "bits");
1943 int bits = qdict_get_try_int(qdict, "bits", -1);
1944 int has_channels = qdict_haskey(qdict, "nchannels");
1945 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1946 CaptureState *s;
1948 s = g_malloc0 (sizeof (*s));
1950 freq = has_freq ? freq : 44100;
1951 bits = has_bits ? bits : 16;
1952 nchannels = has_channels ? nchannels : 2;
1954 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1955 monitor_printf(mon, "Failed to add wave capture\n");
1956 g_free (s);
1957 return;
1959 QLIST_INSERT_HEAD (&capture_head, s, entries);
1962 static qemu_acl *find_acl(Monitor *mon, const char *name)
1964 qemu_acl *acl = qemu_acl_find(name);
1966 if (!acl) {
1967 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1969 return acl;
1972 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1974 const char *aclname = qdict_get_str(qdict, "aclname");
1975 qemu_acl *acl = find_acl(mon, aclname);
1976 qemu_acl_entry *entry;
1977 int i = 0;
1979 if (acl) {
1980 monitor_printf(mon, "policy: %s\n",
1981 acl->defaultDeny ? "deny" : "allow");
1982 QTAILQ_FOREACH(entry, &acl->entries, next) {
1983 i++;
1984 monitor_printf(mon, "%d: %s %s\n", i,
1985 entry->deny ? "deny" : "allow", entry->match);
1990 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
1992 const char *aclname = qdict_get_str(qdict, "aclname");
1993 qemu_acl *acl = find_acl(mon, aclname);
1995 if (acl) {
1996 qemu_acl_reset(acl);
1997 monitor_printf(mon, "acl: removed all rules\n");
2001 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
2003 const char *aclname = qdict_get_str(qdict, "aclname");
2004 const char *policy = qdict_get_str(qdict, "policy");
2005 qemu_acl *acl = find_acl(mon, aclname);
2007 if (acl) {
2008 if (strcmp(policy, "allow") == 0) {
2009 acl->defaultDeny = 0;
2010 monitor_printf(mon, "acl: policy set to 'allow'\n");
2011 } else if (strcmp(policy, "deny") == 0) {
2012 acl->defaultDeny = 1;
2013 monitor_printf(mon, "acl: policy set to 'deny'\n");
2014 } else {
2015 monitor_printf(mon, "acl: unknown policy '%s', "
2016 "expected 'deny' or 'allow'\n", policy);
2021 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
2023 const char *aclname = qdict_get_str(qdict, "aclname");
2024 const char *match = qdict_get_str(qdict, "match");
2025 const char *policy = qdict_get_str(qdict, "policy");
2026 int has_index = qdict_haskey(qdict, "index");
2027 int index = qdict_get_try_int(qdict, "index", -1);
2028 qemu_acl *acl = find_acl(mon, aclname);
2029 int deny, ret;
2031 if (acl) {
2032 if (strcmp(policy, "allow") == 0) {
2033 deny = 0;
2034 } else if (strcmp(policy, "deny") == 0) {
2035 deny = 1;
2036 } else {
2037 monitor_printf(mon, "acl: unknown policy '%s', "
2038 "expected 'deny' or 'allow'\n", policy);
2039 return;
2041 if (has_index)
2042 ret = qemu_acl_insert(acl, deny, match, index);
2043 else
2044 ret = qemu_acl_append(acl, deny, match);
2045 if (ret < 0)
2046 monitor_printf(mon, "acl: unable to add acl entry\n");
2047 else
2048 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2052 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
2054 const char *aclname = qdict_get_str(qdict, "aclname");
2055 const char *match = qdict_get_str(qdict, "match");
2056 qemu_acl *acl = find_acl(mon, aclname);
2057 int ret;
2059 if (acl) {
2060 ret = qemu_acl_remove(acl, match);
2061 if (ret < 0)
2062 monitor_printf(mon, "acl: no matching acl entry\n");
2063 else
2064 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2068 #if defined(TARGET_I386)
2069 static void hmp_mce(Monitor *mon, const QDict *qdict)
2071 X86CPU *cpu;
2072 CPUState *cs;
2073 int cpu_index = qdict_get_int(qdict, "cpu_index");
2074 int bank = qdict_get_int(qdict, "bank");
2075 uint64_t status = qdict_get_int(qdict, "status");
2076 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2077 uint64_t addr = qdict_get_int(qdict, "addr");
2078 uint64_t misc = qdict_get_int(qdict, "misc");
2079 int flags = MCE_INJECT_UNCOND_AO;
2081 if (qdict_get_try_bool(qdict, "broadcast", false)) {
2082 flags |= MCE_INJECT_BROADCAST;
2084 cs = qemu_get_cpu(cpu_index);
2085 if (cs != NULL) {
2086 cpu = X86_CPU(cs);
2087 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2088 flags);
2091 #endif
2093 void qmp_getfd(const char *fdname, Error **errp)
2095 mon_fd_t *monfd;
2096 int fd;
2098 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
2099 if (fd == -1) {
2100 error_setg(errp, QERR_FD_NOT_SUPPLIED);
2101 return;
2104 if (qemu_isdigit(fdname[0])) {
2105 close(fd);
2106 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2107 "a name not starting with a digit");
2108 return;
2111 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2112 if (strcmp(monfd->name, fdname) != 0) {
2113 continue;
2116 close(monfd->fd);
2117 monfd->fd = fd;
2118 return;
2121 monfd = g_malloc0(sizeof(mon_fd_t));
2122 monfd->name = g_strdup(fdname);
2123 monfd->fd = fd;
2125 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
2128 void qmp_closefd(const char *fdname, Error **errp)
2130 mon_fd_t *monfd;
2132 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2133 if (strcmp(monfd->name, fdname) != 0) {
2134 continue;
2137 QLIST_REMOVE(monfd, next);
2138 close(monfd->fd);
2139 g_free(monfd->name);
2140 g_free(monfd);
2141 return;
2144 error_setg(errp, QERR_FD_NOT_FOUND, fdname);
2147 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
2149 int saved_vm_running = runstate_is_running();
2150 const char *name = qdict_get_str(qdict, "name");
2152 vm_stop(RUN_STATE_RESTORE_VM);
2154 if (load_vmstate(name) == 0 && saved_vm_running) {
2155 vm_start();
2159 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
2161 mon_fd_t *monfd;
2163 QLIST_FOREACH(monfd, &mon->fds, next) {
2164 int fd;
2166 if (strcmp(monfd->name, fdname) != 0) {
2167 continue;
2170 fd = monfd->fd;
2172 /* caller takes ownership of fd */
2173 QLIST_REMOVE(monfd, next);
2174 g_free(monfd->name);
2175 g_free(monfd);
2177 return fd;
2180 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
2181 return -1;
2184 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2186 MonFdsetFd *mon_fdset_fd;
2187 MonFdsetFd *mon_fdset_fd_next;
2189 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2190 if ((mon_fdset_fd->removed ||
2191 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2192 runstate_is_running()) {
2193 close(mon_fdset_fd->fd);
2194 g_free(mon_fdset_fd->opaque);
2195 QLIST_REMOVE(mon_fdset_fd, next);
2196 g_free(mon_fdset_fd);
2200 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
2201 QLIST_REMOVE(mon_fdset, next);
2202 g_free(mon_fdset);
2206 static void monitor_fdsets_cleanup(void)
2208 MonFdset *mon_fdset;
2209 MonFdset *mon_fdset_next;
2211 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2212 monitor_fdset_cleanup(mon_fdset);
2216 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2217 const char *opaque, Error **errp)
2219 int fd;
2220 Monitor *mon = cur_mon;
2221 AddfdInfo *fdinfo;
2223 fd = qemu_chr_fe_get_msgfd(mon->chr);
2224 if (fd == -1) {
2225 error_setg(errp, QERR_FD_NOT_SUPPLIED);
2226 goto error;
2229 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2230 has_opaque, opaque, errp);
2231 if (fdinfo) {
2232 return fdinfo;
2235 error:
2236 if (fd != -1) {
2237 close(fd);
2239 return NULL;
2242 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2244 MonFdset *mon_fdset;
2245 MonFdsetFd *mon_fdset_fd;
2246 char fd_str[60];
2248 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2249 if (mon_fdset->id != fdset_id) {
2250 continue;
2252 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2253 if (has_fd) {
2254 if (mon_fdset_fd->fd != fd) {
2255 continue;
2257 mon_fdset_fd->removed = true;
2258 break;
2259 } else {
2260 mon_fdset_fd->removed = true;
2263 if (has_fd && !mon_fdset_fd) {
2264 goto error;
2266 monitor_fdset_cleanup(mon_fdset);
2267 return;
2270 error:
2271 if (has_fd) {
2272 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2273 fdset_id, fd);
2274 } else {
2275 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2277 error_setg(errp, QERR_FD_NOT_FOUND, fd_str);
2280 FdsetInfoList *qmp_query_fdsets(Error **errp)
2282 MonFdset *mon_fdset;
2283 MonFdsetFd *mon_fdset_fd;
2284 FdsetInfoList *fdset_list = NULL;
2286 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2287 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2288 FdsetFdInfoList *fdsetfd_list = NULL;
2290 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2291 fdset_info->value->fdset_id = mon_fdset->id;
2293 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2294 FdsetFdInfoList *fdsetfd_info;
2296 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2297 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2298 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2299 if (mon_fdset_fd->opaque) {
2300 fdsetfd_info->value->has_opaque = true;
2301 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2302 } else {
2303 fdsetfd_info->value->has_opaque = false;
2306 fdsetfd_info->next = fdsetfd_list;
2307 fdsetfd_list = fdsetfd_info;
2310 fdset_info->value->fds = fdsetfd_list;
2312 fdset_info->next = fdset_list;
2313 fdset_list = fdset_info;
2316 return fdset_list;
2319 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2320 bool has_opaque, const char *opaque,
2321 Error **errp)
2323 MonFdset *mon_fdset = NULL;
2324 MonFdsetFd *mon_fdset_fd;
2325 AddfdInfo *fdinfo;
2327 if (has_fdset_id) {
2328 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2329 /* Break if match found or match impossible due to ordering by ID */
2330 if (fdset_id <= mon_fdset->id) {
2331 if (fdset_id < mon_fdset->id) {
2332 mon_fdset = NULL;
2334 break;
2339 if (mon_fdset == NULL) {
2340 int64_t fdset_id_prev = -1;
2341 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2343 if (has_fdset_id) {
2344 if (fdset_id < 0) {
2345 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2346 "a non-negative value");
2347 return NULL;
2349 /* Use specified fdset ID */
2350 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2351 mon_fdset_cur = mon_fdset;
2352 if (fdset_id < mon_fdset_cur->id) {
2353 break;
2356 } else {
2357 /* Use first available fdset ID */
2358 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2359 mon_fdset_cur = mon_fdset;
2360 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2361 fdset_id_prev = mon_fdset_cur->id;
2362 continue;
2364 break;
2368 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2369 if (has_fdset_id) {
2370 mon_fdset->id = fdset_id;
2371 } else {
2372 mon_fdset->id = fdset_id_prev + 1;
2375 /* The fdset list is ordered by fdset ID */
2376 if (!mon_fdset_cur) {
2377 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2378 } else if (mon_fdset->id < mon_fdset_cur->id) {
2379 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2380 } else {
2381 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2385 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2386 mon_fdset_fd->fd = fd;
2387 mon_fdset_fd->removed = false;
2388 if (has_opaque) {
2389 mon_fdset_fd->opaque = g_strdup(opaque);
2391 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2393 fdinfo = g_malloc0(sizeof(*fdinfo));
2394 fdinfo->fdset_id = mon_fdset->id;
2395 fdinfo->fd = mon_fdset_fd->fd;
2397 return fdinfo;
2400 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2402 #ifndef _WIN32
2403 MonFdset *mon_fdset;
2404 MonFdsetFd *mon_fdset_fd;
2405 int mon_fd_flags;
2407 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2408 if (mon_fdset->id != fdset_id) {
2409 continue;
2411 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2412 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2413 if (mon_fd_flags == -1) {
2414 return -1;
2417 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2418 return mon_fdset_fd->fd;
2421 errno = EACCES;
2422 return -1;
2424 #endif
2426 errno = ENOENT;
2427 return -1;
2430 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2432 MonFdset *mon_fdset;
2433 MonFdsetFd *mon_fdset_fd_dup;
2435 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2436 if (mon_fdset->id != fdset_id) {
2437 continue;
2439 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2440 if (mon_fdset_fd_dup->fd == dup_fd) {
2441 return -1;
2444 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2445 mon_fdset_fd_dup->fd = dup_fd;
2446 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2447 return 0;
2449 return -1;
2452 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2454 MonFdset *mon_fdset;
2455 MonFdsetFd *mon_fdset_fd_dup;
2457 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2458 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2459 if (mon_fdset_fd_dup->fd == dup_fd) {
2460 if (remove) {
2461 QLIST_REMOVE(mon_fdset_fd_dup, next);
2462 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2463 monitor_fdset_cleanup(mon_fdset);
2465 return -1;
2466 } else {
2467 return mon_fdset->id;
2472 return -1;
2475 int monitor_fdset_dup_fd_find(int dup_fd)
2477 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2480 void monitor_fdset_dup_fd_remove(int dup_fd)
2482 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2485 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2487 int fd;
2488 Error *local_err = NULL;
2490 if (!qemu_isdigit(fdname[0]) && mon) {
2491 fd = monitor_get_fd(mon, fdname, &local_err);
2492 } else {
2493 fd = qemu_parse_fd(fdname);
2494 if (fd == -1) {
2495 error_setg(&local_err, "Invalid file descriptor number '%s'",
2496 fdname);
2499 if (local_err) {
2500 error_propagate(errp, local_err);
2501 assert(fd == -1);
2502 } else {
2503 assert(fd != -1);
2506 return fd;
2509 /* Please update hmp-commands.hx when adding or changing commands */
2510 static mon_cmd_t info_cmds[] = {
2512 .name = "version",
2513 .args_type = "",
2514 .params = "",
2515 .help = "show the version of QEMU",
2516 .mhandler.cmd = hmp_info_version,
2519 .name = "network",
2520 .args_type = "",
2521 .params = "",
2522 .help = "show the network state",
2523 .mhandler.cmd = hmp_info_network,
2526 .name = "chardev",
2527 .args_type = "",
2528 .params = "",
2529 .help = "show the character devices",
2530 .mhandler.cmd = hmp_info_chardev,
2533 .name = "block",
2534 .args_type = "nodes:-n,verbose:-v,device:B?",
2535 .params = "[-n] [-v] [device]",
2536 .help = "show info of one block device or all block devices "
2537 "(-n: show named nodes; -v: show details)",
2538 .mhandler.cmd = hmp_info_block,
2541 .name = "blockstats",
2542 .args_type = "",
2543 .params = "",
2544 .help = "show block device statistics",
2545 .mhandler.cmd = hmp_info_blockstats,
2548 .name = "block-jobs",
2549 .args_type = "",
2550 .params = "",
2551 .help = "show progress of ongoing block device operations",
2552 .mhandler.cmd = hmp_info_block_jobs,
2555 .name = "registers",
2556 .args_type = "",
2557 .params = "",
2558 .help = "show the cpu registers",
2559 .mhandler.cmd = hmp_info_registers,
2562 .name = "cpus",
2563 .args_type = "",
2564 .params = "",
2565 .help = "show infos for each CPU",
2566 .mhandler.cmd = hmp_info_cpus,
2569 .name = "history",
2570 .args_type = "",
2571 .params = "",
2572 .help = "show the command line history",
2573 .mhandler.cmd = hmp_info_history,
2575 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2576 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2578 .name = "irq",
2579 .args_type = "",
2580 .params = "",
2581 .help = "show the interrupts statistics (if available)",
2582 #ifdef TARGET_SPARC
2583 .mhandler.cmd = sun4m_hmp_info_irq,
2584 #elif defined(TARGET_LM32)
2585 .mhandler.cmd = lm32_hmp_info_irq,
2586 #else
2587 .mhandler.cmd = hmp_info_irq,
2588 #endif
2591 .name = "pic",
2592 .args_type = "",
2593 .params = "",
2594 .help = "show i8259 (PIC) state",
2595 #ifdef TARGET_SPARC
2596 .mhandler.cmd = sun4m_hmp_info_pic,
2597 #elif defined(TARGET_LM32)
2598 .mhandler.cmd = lm32_hmp_info_pic,
2599 #else
2600 .mhandler.cmd = hmp_info_pic,
2601 #endif
2603 #endif
2605 .name = "pci",
2606 .args_type = "",
2607 .params = "",
2608 .help = "show PCI info",
2609 .mhandler.cmd = hmp_info_pci,
2611 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2612 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2614 .name = "tlb",
2615 .args_type = "",
2616 .params = "",
2617 .help = "show virtual to physical memory mappings",
2618 .mhandler.cmd = hmp_info_tlb,
2620 #endif
2621 #if defined(TARGET_I386)
2623 .name = "mem",
2624 .args_type = "",
2625 .params = "",
2626 .help = "show the active virtual memory mappings",
2627 .mhandler.cmd = hmp_info_mem,
2629 #endif
2631 .name = "mtree",
2632 .args_type = "",
2633 .params = "",
2634 .help = "show memory tree",
2635 .mhandler.cmd = hmp_info_mtree,
2638 .name = "jit",
2639 .args_type = "",
2640 .params = "",
2641 .help = "show dynamic compiler info",
2642 .mhandler.cmd = hmp_info_jit,
2645 .name = "opcount",
2646 .args_type = "",
2647 .params = "",
2648 .help = "show dynamic compiler opcode counters",
2649 .mhandler.cmd = hmp_info_opcount,
2652 .name = "kvm",
2653 .args_type = "",
2654 .params = "",
2655 .help = "show KVM information",
2656 .mhandler.cmd = hmp_info_kvm,
2659 .name = "numa",
2660 .args_type = "",
2661 .params = "",
2662 .help = "show NUMA information",
2663 .mhandler.cmd = hmp_info_numa,
2666 .name = "usb",
2667 .args_type = "",
2668 .params = "",
2669 .help = "show guest USB devices",
2670 .mhandler.cmd = hmp_info_usb,
2673 .name = "usbhost",
2674 .args_type = "",
2675 .params = "",
2676 .help = "show host USB devices",
2677 .mhandler.cmd = hmp_info_usbhost,
2680 .name = "profile",
2681 .args_type = "",
2682 .params = "",
2683 .help = "show profiling information",
2684 .mhandler.cmd = hmp_info_profile,
2687 .name = "capture",
2688 .args_type = "",
2689 .params = "",
2690 .help = "show capture information",
2691 .mhandler.cmd = hmp_info_capture,
2694 .name = "snapshots",
2695 .args_type = "",
2696 .params = "",
2697 .help = "show the currently saved VM snapshots",
2698 .mhandler.cmd = hmp_info_snapshots,
2701 .name = "status",
2702 .args_type = "",
2703 .params = "",
2704 .help = "show the current VM status (running|paused)",
2705 .mhandler.cmd = hmp_info_status,
2708 .name = "mice",
2709 .args_type = "",
2710 .params = "",
2711 .help = "show which guest mouse is receiving events",
2712 .mhandler.cmd = hmp_info_mice,
2715 .name = "vnc",
2716 .args_type = "",
2717 .params = "",
2718 .help = "show the vnc server status",
2719 .mhandler.cmd = hmp_info_vnc,
2721 #if defined(CONFIG_SPICE)
2723 .name = "spice",
2724 .args_type = "",
2725 .params = "",
2726 .help = "show the spice server status",
2727 .mhandler.cmd = hmp_info_spice,
2729 #endif
2731 .name = "name",
2732 .args_type = "",
2733 .params = "",
2734 .help = "show the current VM name",
2735 .mhandler.cmd = hmp_info_name,
2738 .name = "uuid",
2739 .args_type = "",
2740 .params = "",
2741 .help = "show the current VM UUID",
2742 .mhandler.cmd = hmp_info_uuid,
2745 .name = "cpustats",
2746 .args_type = "",
2747 .params = "",
2748 .help = "show CPU statistics",
2749 .mhandler.cmd = hmp_info_cpustats,
2751 #if defined(CONFIG_SLIRP)
2753 .name = "usernet",
2754 .args_type = "",
2755 .params = "",
2756 .help = "show user network stack connection states",
2757 .mhandler.cmd = hmp_info_usernet,
2759 #endif
2761 .name = "migrate",
2762 .args_type = "",
2763 .params = "",
2764 .help = "show migration status",
2765 .mhandler.cmd = hmp_info_migrate,
2768 .name = "migrate_capabilities",
2769 .args_type = "",
2770 .params = "",
2771 .help = "show current migration capabilities",
2772 .mhandler.cmd = hmp_info_migrate_capabilities,
2775 .name = "migrate_parameters",
2776 .args_type = "",
2777 .params = "",
2778 .help = "show current migration parameters",
2779 .mhandler.cmd = hmp_info_migrate_parameters,
2782 .name = "migrate_cache_size",
2783 .args_type = "",
2784 .params = "",
2785 .help = "show current migration xbzrle cache size",
2786 .mhandler.cmd = hmp_info_migrate_cache_size,
2789 .name = "balloon",
2790 .args_type = "",
2791 .params = "",
2792 .help = "show balloon information",
2793 .mhandler.cmd = hmp_info_balloon,
2796 .name = "qtree",
2797 .args_type = "",
2798 .params = "",
2799 .help = "show device tree",
2800 .mhandler.cmd = hmp_info_qtree,
2803 .name = "qdm",
2804 .args_type = "",
2805 .params = "",
2806 .help = "show qdev device model list",
2807 .mhandler.cmd = hmp_info_qdm,
2810 .name = "qom-tree",
2811 .args_type = "path:s?",
2812 .params = "[path]",
2813 .help = "show QOM composition tree",
2814 .mhandler.cmd = hmp_info_qom_tree,
2817 .name = "roms",
2818 .args_type = "",
2819 .params = "",
2820 .help = "show roms",
2821 .mhandler.cmd = hmp_info_roms,
2824 .name = "trace-events",
2825 .args_type = "",
2826 .params = "",
2827 .help = "show available trace-events & their state",
2828 .mhandler.cmd = hmp_info_trace_events,
2831 .name = "tpm",
2832 .args_type = "",
2833 .params = "",
2834 .help = "show the TPM device",
2835 .mhandler.cmd = hmp_info_tpm,
2838 .name = "memdev",
2839 .args_type = "",
2840 .params = "",
2841 .help = "show memory backends",
2842 .mhandler.cmd = hmp_info_memdev,
2845 .name = "memory-devices",
2846 .args_type = "",
2847 .params = "",
2848 .help = "show memory devices",
2849 .mhandler.cmd = hmp_info_memory_devices,
2852 .name = "rocker",
2853 .args_type = "name:s",
2854 .params = "name",
2855 .help = "Show rocker switch",
2856 .mhandler.cmd = hmp_rocker,
2859 .name = "rocker-ports",
2860 .args_type = "name:s",
2861 .params = "name",
2862 .help = "Show rocker ports",
2863 .mhandler.cmd = hmp_rocker_ports,
2866 .name = "rocker-of-dpa-flows",
2867 .args_type = "name:s,tbl_id:i?",
2868 .params = "name [tbl_id]",
2869 .help = "Show rocker OF-DPA flow tables",
2870 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2873 .name = "rocker-of-dpa-groups",
2874 .args_type = "name:s,type:i?",
2875 .params = "name [type]",
2876 .help = "Show rocker OF-DPA groups",
2877 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2880 .name = NULL,
2884 /* mon_cmds and info_cmds would be sorted at runtime */
2885 static mon_cmd_t mon_cmds[] = {
2886 #include "hmp-commands.h"
2887 { NULL, NULL, },
2890 static const mon_cmd_t qmp_cmds[] = {
2891 #include "qmp-commands-old.h"
2892 { /* NULL */ },
2895 /*******************************************************************/
2897 static const char *pch;
2898 static sigjmp_buf expr_env;
2900 #define MD_TLONG 0
2901 #define MD_I32 1
2903 typedef struct MonitorDef {
2904 const char *name;
2905 int offset;
2906 target_long (*get_value)(const struct MonitorDef *md, int val);
2907 int type;
2908 } MonitorDef;
2910 #if defined(TARGET_I386)
2911 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2913 CPUArchState *env = mon_get_cpu_env();
2914 return env->eip + env->segs[R_CS].base;
2916 #endif
2918 #if defined(TARGET_PPC)
2919 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2921 CPUArchState *env = mon_get_cpu_env();
2922 unsigned int u;
2923 int i;
2925 u = 0;
2926 for (i = 0; i < 8; i++)
2927 u |= env->crf[i] << (32 - (4 * (i + 1)));
2929 return u;
2932 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2934 CPUArchState *env = mon_get_cpu_env();
2935 return env->msr;
2938 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2940 CPUArchState *env = mon_get_cpu_env();
2941 return env->xer;
2944 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2946 CPUArchState *env = mon_get_cpu_env();
2947 return cpu_ppc_load_decr(env);
2950 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2952 CPUArchState *env = mon_get_cpu_env();
2953 return cpu_ppc_load_tbu(env);
2956 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2958 CPUArchState *env = mon_get_cpu_env();
2959 return cpu_ppc_load_tbl(env);
2961 #endif
2963 #if defined(TARGET_SPARC)
2964 #ifndef TARGET_SPARC64
2965 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2967 CPUArchState *env = mon_get_cpu_env();
2969 return cpu_get_psr(env);
2971 #endif
2973 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2975 CPUArchState *env = mon_get_cpu_env();
2976 return env->regwptr[val];
2978 #endif
2980 static const MonitorDef monitor_defs[] = {
2981 #ifdef TARGET_I386
2983 #define SEG(name, seg) \
2984 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2985 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2986 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2988 { "eax", offsetof(CPUX86State, regs[0]) },
2989 { "ecx", offsetof(CPUX86State, regs[1]) },
2990 { "edx", offsetof(CPUX86State, regs[2]) },
2991 { "ebx", offsetof(CPUX86State, regs[3]) },
2992 { "esp|sp", offsetof(CPUX86State, regs[4]) },
2993 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
2994 { "esi", offsetof(CPUX86State, regs[6]) },
2995 { "edi", offsetof(CPUX86State, regs[7]) },
2996 #ifdef TARGET_X86_64
2997 { "r8", offsetof(CPUX86State, regs[8]) },
2998 { "r9", offsetof(CPUX86State, regs[9]) },
2999 { "r10", offsetof(CPUX86State, regs[10]) },
3000 { "r11", offsetof(CPUX86State, regs[11]) },
3001 { "r12", offsetof(CPUX86State, regs[12]) },
3002 { "r13", offsetof(CPUX86State, regs[13]) },
3003 { "r14", offsetof(CPUX86State, regs[14]) },
3004 { "r15", offsetof(CPUX86State, regs[15]) },
3005 #endif
3006 { "eflags", offsetof(CPUX86State, eflags) },
3007 { "eip", offsetof(CPUX86State, eip) },
3008 SEG("cs", R_CS)
3009 SEG("ds", R_DS)
3010 SEG("es", R_ES)
3011 SEG("ss", R_SS)
3012 SEG("fs", R_FS)
3013 SEG("gs", R_GS)
3014 { "pc", 0, monitor_get_pc, },
3015 #elif defined(TARGET_PPC)
3016 /* General purpose registers */
3017 { "r0", offsetof(CPUPPCState, gpr[0]) },
3018 { "r1", offsetof(CPUPPCState, gpr[1]) },
3019 { "r2", offsetof(CPUPPCState, gpr[2]) },
3020 { "r3", offsetof(CPUPPCState, gpr[3]) },
3021 { "r4", offsetof(CPUPPCState, gpr[4]) },
3022 { "r5", offsetof(CPUPPCState, gpr[5]) },
3023 { "r6", offsetof(CPUPPCState, gpr[6]) },
3024 { "r7", offsetof(CPUPPCState, gpr[7]) },
3025 { "r8", offsetof(CPUPPCState, gpr[8]) },
3026 { "r9", offsetof(CPUPPCState, gpr[9]) },
3027 { "r10", offsetof(CPUPPCState, gpr[10]) },
3028 { "r11", offsetof(CPUPPCState, gpr[11]) },
3029 { "r12", offsetof(CPUPPCState, gpr[12]) },
3030 { "r13", offsetof(CPUPPCState, gpr[13]) },
3031 { "r14", offsetof(CPUPPCState, gpr[14]) },
3032 { "r15", offsetof(CPUPPCState, gpr[15]) },
3033 { "r16", offsetof(CPUPPCState, gpr[16]) },
3034 { "r17", offsetof(CPUPPCState, gpr[17]) },
3035 { "r18", offsetof(CPUPPCState, gpr[18]) },
3036 { "r19", offsetof(CPUPPCState, gpr[19]) },
3037 { "r20", offsetof(CPUPPCState, gpr[20]) },
3038 { "r21", offsetof(CPUPPCState, gpr[21]) },
3039 { "r22", offsetof(CPUPPCState, gpr[22]) },
3040 { "r23", offsetof(CPUPPCState, gpr[23]) },
3041 { "r24", offsetof(CPUPPCState, gpr[24]) },
3042 { "r25", offsetof(CPUPPCState, gpr[25]) },
3043 { "r26", offsetof(CPUPPCState, gpr[26]) },
3044 { "r27", offsetof(CPUPPCState, gpr[27]) },
3045 { "r28", offsetof(CPUPPCState, gpr[28]) },
3046 { "r29", offsetof(CPUPPCState, gpr[29]) },
3047 { "r30", offsetof(CPUPPCState, gpr[30]) },
3048 { "r31", offsetof(CPUPPCState, gpr[31]) },
3049 /* Floating point registers */
3050 { "f0", offsetof(CPUPPCState, fpr[0]) },
3051 { "f1", offsetof(CPUPPCState, fpr[1]) },
3052 { "f2", offsetof(CPUPPCState, fpr[2]) },
3053 { "f3", offsetof(CPUPPCState, fpr[3]) },
3054 { "f4", offsetof(CPUPPCState, fpr[4]) },
3055 { "f5", offsetof(CPUPPCState, fpr[5]) },
3056 { "f6", offsetof(CPUPPCState, fpr[6]) },
3057 { "f7", offsetof(CPUPPCState, fpr[7]) },
3058 { "f8", offsetof(CPUPPCState, fpr[8]) },
3059 { "f9", offsetof(CPUPPCState, fpr[9]) },
3060 { "f10", offsetof(CPUPPCState, fpr[10]) },
3061 { "f11", offsetof(CPUPPCState, fpr[11]) },
3062 { "f12", offsetof(CPUPPCState, fpr[12]) },
3063 { "f13", offsetof(CPUPPCState, fpr[13]) },
3064 { "f14", offsetof(CPUPPCState, fpr[14]) },
3065 { "f15", offsetof(CPUPPCState, fpr[15]) },
3066 { "f16", offsetof(CPUPPCState, fpr[16]) },
3067 { "f17", offsetof(CPUPPCState, fpr[17]) },
3068 { "f18", offsetof(CPUPPCState, fpr[18]) },
3069 { "f19", offsetof(CPUPPCState, fpr[19]) },
3070 { "f20", offsetof(CPUPPCState, fpr[20]) },
3071 { "f21", offsetof(CPUPPCState, fpr[21]) },
3072 { "f22", offsetof(CPUPPCState, fpr[22]) },
3073 { "f23", offsetof(CPUPPCState, fpr[23]) },
3074 { "f24", offsetof(CPUPPCState, fpr[24]) },
3075 { "f25", offsetof(CPUPPCState, fpr[25]) },
3076 { "f26", offsetof(CPUPPCState, fpr[26]) },
3077 { "f27", offsetof(CPUPPCState, fpr[27]) },
3078 { "f28", offsetof(CPUPPCState, fpr[28]) },
3079 { "f29", offsetof(CPUPPCState, fpr[29]) },
3080 { "f30", offsetof(CPUPPCState, fpr[30]) },
3081 { "f31", offsetof(CPUPPCState, fpr[31]) },
3082 { "fpscr", offsetof(CPUPPCState, fpscr) },
3083 /* Next instruction pointer */
3084 { "nip|pc", offsetof(CPUPPCState, nip) },
3085 { "lr", offsetof(CPUPPCState, lr) },
3086 { "ctr", offsetof(CPUPPCState, ctr) },
3087 { "decr", 0, &monitor_get_decr, },
3088 { "ccr", 0, &monitor_get_ccr, },
3089 /* Machine state register */
3090 { "msr", 0, &monitor_get_msr, },
3091 { "xer", 0, &monitor_get_xer, },
3092 { "tbu", 0, &monitor_get_tbu, },
3093 { "tbl", 0, &monitor_get_tbl, },
3094 /* Segment registers */
3095 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3096 { "sr0", offsetof(CPUPPCState, sr[0]) },
3097 { "sr1", offsetof(CPUPPCState, sr[1]) },
3098 { "sr2", offsetof(CPUPPCState, sr[2]) },
3099 { "sr3", offsetof(CPUPPCState, sr[3]) },
3100 { "sr4", offsetof(CPUPPCState, sr[4]) },
3101 { "sr5", offsetof(CPUPPCState, sr[5]) },
3102 { "sr6", offsetof(CPUPPCState, sr[6]) },
3103 { "sr7", offsetof(CPUPPCState, sr[7]) },
3104 { "sr8", offsetof(CPUPPCState, sr[8]) },
3105 { "sr9", offsetof(CPUPPCState, sr[9]) },
3106 { "sr10", offsetof(CPUPPCState, sr[10]) },
3107 { "sr11", offsetof(CPUPPCState, sr[11]) },
3108 { "sr12", offsetof(CPUPPCState, sr[12]) },
3109 { "sr13", offsetof(CPUPPCState, sr[13]) },
3110 { "sr14", offsetof(CPUPPCState, sr[14]) },
3111 { "sr15", offsetof(CPUPPCState, sr[15]) },
3112 /* Too lazy to put BATs... */
3113 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
3115 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3116 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3117 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3118 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3119 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
3120 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3121 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3122 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3123 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3124 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3125 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3126 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3127 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3128 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3129 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3130 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3131 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3132 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3133 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3134 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3135 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3136 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3137 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3138 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3139 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3140 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3141 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3142 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3143 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3144 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3145 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3146 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3147 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3148 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3149 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3150 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3151 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3152 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3153 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3154 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3155 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3156 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3157 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3158 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3159 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3160 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3161 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3162 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3163 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3164 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3165 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3166 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3167 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3168 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3169 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3170 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3171 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3172 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3173 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3174 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3175 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3176 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3177 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3178 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3179 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3180 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3181 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3182 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3183 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
3185 #elif defined(TARGET_SPARC)
3186 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3187 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3188 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3189 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3190 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3191 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3192 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3193 { "g7", offsetof(CPUSPARCState, gregs[7]) },
3194 { "o0", 0, monitor_get_reg },
3195 { "o1", 1, monitor_get_reg },
3196 { "o2", 2, monitor_get_reg },
3197 { "o3", 3, monitor_get_reg },
3198 { "o4", 4, monitor_get_reg },
3199 { "o5", 5, monitor_get_reg },
3200 { "o6", 6, monitor_get_reg },
3201 { "o7", 7, monitor_get_reg },
3202 { "l0", 8, monitor_get_reg },
3203 { "l1", 9, monitor_get_reg },
3204 { "l2", 10, monitor_get_reg },
3205 { "l3", 11, monitor_get_reg },
3206 { "l4", 12, monitor_get_reg },
3207 { "l5", 13, monitor_get_reg },
3208 { "l6", 14, monitor_get_reg },
3209 { "l7", 15, monitor_get_reg },
3210 { "i0", 16, monitor_get_reg },
3211 { "i1", 17, monitor_get_reg },
3212 { "i2", 18, monitor_get_reg },
3213 { "i3", 19, monitor_get_reg },
3214 { "i4", 20, monitor_get_reg },
3215 { "i5", 21, monitor_get_reg },
3216 { "i6", 22, monitor_get_reg },
3217 { "i7", 23, monitor_get_reg },
3218 { "pc", offsetof(CPUSPARCState, pc) },
3219 { "npc", offsetof(CPUSPARCState, npc) },
3220 { "y", offsetof(CPUSPARCState, y) },
3221 #ifndef TARGET_SPARC64
3222 { "psr", 0, &monitor_get_psr, },
3223 { "wim", offsetof(CPUSPARCState, wim) },
3224 #endif
3225 { "tbr", offsetof(CPUSPARCState, tbr) },
3226 { "fsr", offsetof(CPUSPARCState, fsr) },
3227 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3228 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3229 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3230 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3231 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3232 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3233 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3234 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3235 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3236 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3237 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3238 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3239 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3240 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3241 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3242 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3243 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3244 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3245 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3246 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3247 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3248 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3249 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3250 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3251 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3252 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3253 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3254 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3255 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3256 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3257 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3258 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
3259 #ifdef TARGET_SPARC64
3260 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3261 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3262 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3263 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3264 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3265 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3266 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3267 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3268 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3269 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3270 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3271 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3272 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3273 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3274 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3275 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3276 { "asi", offsetof(CPUSPARCState, asi) },
3277 { "pstate", offsetof(CPUSPARCState, pstate) },
3278 { "cansave", offsetof(CPUSPARCState, cansave) },
3279 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3280 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3281 { "wstate", offsetof(CPUSPARCState, wstate) },
3282 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3283 { "fprs", offsetof(CPUSPARCState, fprs) },
3284 #endif
3285 #endif
3286 { NULL },
3289 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3290 expr_error(Monitor *mon, const char *fmt, ...)
3292 va_list ap;
3293 va_start(ap, fmt);
3294 monitor_vprintf(mon, fmt, ap);
3295 monitor_printf(mon, "\n");
3296 va_end(ap);
3297 siglongjmp(expr_env, 1);
3300 /* return 0 if OK, -1 if not found */
3301 static int get_monitor_def(target_long *pval, const char *name)
3303 const MonitorDef *md;
3304 void *ptr;
3306 for(md = monitor_defs; md->name != NULL; md++) {
3307 if (compare_cmd(name, md->name)) {
3308 if (md->get_value) {
3309 *pval = md->get_value(md, md->offset);
3310 } else {
3311 CPUArchState *env = mon_get_cpu_env();
3312 ptr = (uint8_t *)env + md->offset;
3313 switch(md->type) {
3314 case MD_I32:
3315 *pval = *(int32_t *)ptr;
3316 break;
3317 case MD_TLONG:
3318 *pval = *(target_long *)ptr;
3319 break;
3320 default:
3321 *pval = 0;
3322 break;
3325 return 0;
3328 return -1;
3331 static void next(void)
3333 if (*pch != '\0') {
3334 pch++;
3335 while (qemu_isspace(*pch))
3336 pch++;
3340 static int64_t expr_sum(Monitor *mon);
3342 static int64_t expr_unary(Monitor *mon)
3344 int64_t n;
3345 char *p;
3346 int ret;
3348 switch(*pch) {
3349 case '+':
3350 next();
3351 n = expr_unary(mon);
3352 break;
3353 case '-':
3354 next();
3355 n = -expr_unary(mon);
3356 break;
3357 case '~':
3358 next();
3359 n = ~expr_unary(mon);
3360 break;
3361 case '(':
3362 next();
3363 n = expr_sum(mon);
3364 if (*pch != ')') {
3365 expr_error(mon, "')' expected");
3367 next();
3368 break;
3369 case '\'':
3370 pch++;
3371 if (*pch == '\0')
3372 expr_error(mon, "character constant expected");
3373 n = *pch;
3374 pch++;
3375 if (*pch != '\'')
3376 expr_error(mon, "missing terminating \' character");
3377 next();
3378 break;
3379 case '$':
3381 char buf[128], *q;
3382 target_long reg=0;
3384 pch++;
3385 q = buf;
3386 while ((*pch >= 'a' && *pch <= 'z') ||
3387 (*pch >= 'A' && *pch <= 'Z') ||
3388 (*pch >= '0' && *pch <= '9') ||
3389 *pch == '_' || *pch == '.') {
3390 if ((q - buf) < sizeof(buf) - 1)
3391 *q++ = *pch;
3392 pch++;
3394 while (qemu_isspace(*pch))
3395 pch++;
3396 *q = 0;
3397 ret = get_monitor_def(&reg, buf);
3398 if (ret < 0)
3399 expr_error(mon, "unknown register");
3400 n = reg;
3402 break;
3403 case '\0':
3404 expr_error(mon, "unexpected end of expression");
3405 n = 0;
3406 break;
3407 default:
3408 errno = 0;
3409 n = strtoull(pch, &p, 0);
3410 if (errno == ERANGE) {
3411 expr_error(mon, "number too large");
3413 if (pch == p) {
3414 expr_error(mon, "invalid char '%c' in expression", *p);
3416 pch = p;
3417 while (qemu_isspace(*pch))
3418 pch++;
3419 break;
3421 return n;
3425 static int64_t expr_prod(Monitor *mon)
3427 int64_t val, val2;
3428 int op;
3430 val = expr_unary(mon);
3431 for(;;) {
3432 op = *pch;
3433 if (op != '*' && op != '/' && op != '%')
3434 break;
3435 next();
3436 val2 = expr_unary(mon);
3437 switch(op) {
3438 default:
3439 case '*':
3440 val *= val2;
3441 break;
3442 case '/':
3443 case '%':
3444 if (val2 == 0)
3445 expr_error(mon, "division by zero");
3446 if (op == '/')
3447 val /= val2;
3448 else
3449 val %= val2;
3450 break;
3453 return val;
3456 static int64_t expr_logic(Monitor *mon)
3458 int64_t val, val2;
3459 int op;
3461 val = expr_prod(mon);
3462 for(;;) {
3463 op = *pch;
3464 if (op != '&' && op != '|' && op != '^')
3465 break;
3466 next();
3467 val2 = expr_prod(mon);
3468 switch(op) {
3469 default:
3470 case '&':
3471 val &= val2;
3472 break;
3473 case '|':
3474 val |= val2;
3475 break;
3476 case '^':
3477 val ^= val2;
3478 break;
3481 return val;
3484 static int64_t expr_sum(Monitor *mon)
3486 int64_t val, val2;
3487 int op;
3489 val = expr_logic(mon);
3490 for(;;) {
3491 op = *pch;
3492 if (op != '+' && op != '-')
3493 break;
3494 next();
3495 val2 = expr_logic(mon);
3496 if (op == '+')
3497 val += val2;
3498 else
3499 val -= val2;
3501 return val;
3504 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3506 pch = *pp;
3507 if (sigsetjmp(expr_env, 0)) {
3508 *pp = pch;
3509 return -1;
3511 while (qemu_isspace(*pch))
3512 pch++;
3513 *pval = expr_sum(mon);
3514 *pp = pch;
3515 return 0;
3518 static int get_double(Monitor *mon, double *pval, const char **pp)
3520 const char *p = *pp;
3521 char *tailp;
3522 double d;
3524 d = strtod(p, &tailp);
3525 if (tailp == p) {
3526 monitor_printf(mon, "Number expected\n");
3527 return -1;
3529 if (d != d || d - d != 0) {
3530 /* NaN or infinity */
3531 monitor_printf(mon, "Bad number\n");
3532 return -1;
3534 *pval = d;
3535 *pp = tailp;
3536 return 0;
3540 * Store the command-name in cmdname, and return a pointer to
3541 * the remaining of the command string.
3543 static const char *get_command_name(const char *cmdline,
3544 char *cmdname, size_t nlen)
3546 size_t len;
3547 const char *p, *pstart;
3549 p = cmdline;
3550 while (qemu_isspace(*p))
3551 p++;
3552 if (*p == '\0')
3553 return NULL;
3554 pstart = p;
3555 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3556 p++;
3557 len = p - pstart;
3558 if (len > nlen - 1)
3559 len = nlen - 1;
3560 memcpy(cmdname, pstart, len);
3561 cmdname[len] = '\0';
3562 return p;
3566 * Read key of 'type' into 'key' and return the current
3567 * 'type' pointer.
3569 static char *key_get_info(const char *type, char **key)
3571 size_t len;
3572 char *p, *str;
3574 if (*type == ',')
3575 type++;
3577 p = strchr(type, ':');
3578 if (!p) {
3579 *key = NULL;
3580 return NULL;
3582 len = p - type;
3584 str = g_malloc(len + 1);
3585 memcpy(str, type, len);
3586 str[len] = '\0';
3588 *key = str;
3589 return ++p;
3592 static int default_fmt_format = 'x';
3593 static int default_fmt_size = 4;
3595 static int is_valid_option(const char *c, const char *typestr)
3597 char option[3];
3599 option[0] = '-';
3600 option[1] = *c;
3601 option[2] = '\0';
3603 typestr = strstr(typestr, option);
3604 return (typestr != NULL);
3607 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3608 const char *cmdname)
3610 const mon_cmd_t *cmd;
3612 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3613 if (compare_cmd(cmdname, cmd->name)) {
3614 return cmd;
3618 return NULL;
3621 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3623 return search_dispatch_table(qmp_cmds, cmdname);
3627 * Parse command name from @cmdp according to command table @table.
3628 * If blank, return NULL.
3629 * Else, if no valid command can be found, report to @mon, and return
3630 * NULL.
3631 * Else, change @cmdp to point right behind the name, and return its
3632 * command table entry.
3633 * Do not assume the return value points into @table! It doesn't when
3634 * the command is found in a sub-command table.
3636 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3637 const char **cmdp,
3638 mon_cmd_t *table)
3640 const char *p;
3641 const mon_cmd_t *cmd;
3642 char cmdname[256];
3644 /* extract the command name */
3645 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
3646 if (!p)
3647 return NULL;
3649 cmd = search_dispatch_table(table, cmdname);
3650 if (!cmd) {
3651 monitor_printf(mon, "unknown command: '%.*s'\n",
3652 (int)(p - *cmdp), *cmdp);
3653 return NULL;
3656 /* filter out following useless space */
3657 while (qemu_isspace(*p)) {
3658 p++;
3661 *cmdp = p;
3662 /* search sub command */
3663 if (cmd->sub_table != NULL && *p != '\0') {
3664 return monitor_parse_command(mon, cmdp, cmd->sub_table);
3667 return cmd;
3671 * Parse arguments for @cmd.
3672 * If it can't be parsed, report to @mon, and return NULL.
3673 * Else, insert command arguments into a QDict, and return it.
3674 * Note: On success, caller has to free the QDict structure.
3677 static QDict *monitor_parse_arguments(Monitor *mon,
3678 const char **endp,
3679 const mon_cmd_t *cmd)
3681 const char *typestr;
3682 char *key;
3683 int c;
3684 const char *p = *endp;
3685 char buf[1024];
3686 QDict *qdict = qdict_new();
3688 /* parse the parameters */
3689 typestr = cmd->args_type;
3690 for(;;) {
3691 typestr = key_get_info(typestr, &key);
3692 if (!typestr)
3693 break;
3694 c = *typestr;
3695 typestr++;
3696 switch(c) {
3697 case 'F':
3698 case 'B':
3699 case 's':
3701 int ret;
3703 while (qemu_isspace(*p))
3704 p++;
3705 if (*typestr == '?') {
3706 typestr++;
3707 if (*p == '\0') {
3708 /* no optional string: NULL argument */
3709 break;
3712 ret = get_str(buf, sizeof(buf), &p);
3713 if (ret < 0) {
3714 switch(c) {
3715 case 'F':
3716 monitor_printf(mon, "%s: filename expected\n",
3717 cmd->name);
3718 break;
3719 case 'B':
3720 monitor_printf(mon, "%s: block device name expected\n",
3721 cmd->name);
3722 break;
3723 default:
3724 monitor_printf(mon, "%s: string expected\n", cmd->name);
3725 break;
3727 goto fail;
3729 qdict_put(qdict, key, qstring_from_str(buf));
3731 break;
3732 case 'O':
3734 QemuOptsList *opts_list;
3735 QemuOpts *opts;
3737 opts_list = qemu_find_opts(key);
3738 if (!opts_list || opts_list->desc->name) {
3739 goto bad_type;
3741 while (qemu_isspace(*p)) {
3742 p++;
3744 if (!*p)
3745 break;
3746 if (get_str(buf, sizeof(buf), &p) < 0) {
3747 goto fail;
3749 opts = qemu_opts_parse_noisily(opts_list, buf, true);
3750 if (!opts) {
3751 goto fail;
3753 qemu_opts_to_qdict(opts, qdict);
3754 qemu_opts_del(opts);
3756 break;
3757 case '/':
3759 int count, format, size;
3761 while (qemu_isspace(*p))
3762 p++;
3763 if (*p == '/') {
3764 /* format found */
3765 p++;
3766 count = 1;
3767 if (qemu_isdigit(*p)) {
3768 count = 0;
3769 while (qemu_isdigit(*p)) {
3770 count = count * 10 + (*p - '0');
3771 p++;
3774 size = -1;
3775 format = -1;
3776 for(;;) {
3777 switch(*p) {
3778 case 'o':
3779 case 'd':
3780 case 'u':
3781 case 'x':
3782 case 'i':
3783 case 'c':
3784 format = *p++;
3785 break;
3786 case 'b':
3787 size = 1;
3788 p++;
3789 break;
3790 case 'h':
3791 size = 2;
3792 p++;
3793 break;
3794 case 'w':
3795 size = 4;
3796 p++;
3797 break;
3798 case 'g':
3799 case 'L':
3800 size = 8;
3801 p++;
3802 break;
3803 default:
3804 goto next;
3807 next:
3808 if (*p != '\0' && !qemu_isspace(*p)) {
3809 monitor_printf(mon, "invalid char in format: '%c'\n",
3810 *p);
3811 goto fail;
3813 if (format < 0)
3814 format = default_fmt_format;
3815 if (format != 'i') {
3816 /* for 'i', not specifying a size gives -1 as size */
3817 if (size < 0)
3818 size = default_fmt_size;
3819 default_fmt_size = size;
3821 default_fmt_format = format;
3822 } else {
3823 count = 1;
3824 format = default_fmt_format;
3825 if (format != 'i') {
3826 size = default_fmt_size;
3827 } else {
3828 size = -1;
3831 qdict_put(qdict, "count", qint_from_int(count));
3832 qdict_put(qdict, "format", qint_from_int(format));
3833 qdict_put(qdict, "size", qint_from_int(size));
3835 break;
3836 case 'i':
3837 case 'l':
3838 case 'M':
3840 int64_t val;
3842 while (qemu_isspace(*p))
3843 p++;
3844 if (*typestr == '?' || *typestr == '.') {
3845 if (*typestr == '?') {
3846 if (*p == '\0') {
3847 typestr++;
3848 break;
3850 } else {
3851 if (*p == '.') {
3852 p++;
3853 while (qemu_isspace(*p))
3854 p++;
3855 } else {
3856 typestr++;
3857 break;
3860 typestr++;
3862 if (get_expr(mon, &val, &p))
3863 goto fail;
3864 /* Check if 'i' is greater than 32-bit */
3865 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3866 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
3867 monitor_printf(mon, "integer is for 32-bit values\n");
3868 goto fail;
3869 } else if (c == 'M') {
3870 if (val < 0) {
3871 monitor_printf(mon, "enter a positive value\n");
3872 goto fail;
3874 val <<= 20;
3876 qdict_put(qdict, key, qint_from_int(val));
3878 break;
3879 case 'o':
3881 int64_t val;
3882 char *end;
3884 while (qemu_isspace(*p)) {
3885 p++;
3887 if (*typestr == '?') {
3888 typestr++;
3889 if (*p == '\0') {
3890 break;
3893 val = strtosz(p, &end);
3894 if (val < 0) {
3895 monitor_printf(mon, "invalid size\n");
3896 goto fail;
3898 qdict_put(qdict, key, qint_from_int(val));
3899 p = end;
3901 break;
3902 case 'T':
3904 double val;
3906 while (qemu_isspace(*p))
3907 p++;
3908 if (*typestr == '?') {
3909 typestr++;
3910 if (*p == '\0') {
3911 break;
3914 if (get_double(mon, &val, &p) < 0) {
3915 goto fail;
3917 if (p[0] && p[1] == 's') {
3918 switch (*p) {
3919 case 'm':
3920 val /= 1e3; p += 2; break;
3921 case 'u':
3922 val /= 1e6; p += 2; break;
3923 case 'n':
3924 val /= 1e9; p += 2; break;
3927 if (*p && !qemu_isspace(*p)) {
3928 monitor_printf(mon, "Unknown unit suffix\n");
3929 goto fail;
3931 qdict_put(qdict, key, qfloat_from_double(val));
3933 break;
3934 case 'b':
3936 const char *beg;
3937 bool val;
3939 while (qemu_isspace(*p)) {
3940 p++;
3942 beg = p;
3943 while (qemu_isgraph(*p)) {
3944 p++;
3946 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3947 val = true;
3948 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3949 val = false;
3950 } else {
3951 monitor_printf(mon, "Expected 'on' or 'off'\n");
3952 goto fail;
3954 qdict_put(qdict, key, qbool_from_bool(val));
3956 break;
3957 case '-':
3959 const char *tmp = p;
3960 int skip_key = 0;
3961 /* option */
3963 c = *typestr++;
3964 if (c == '\0')
3965 goto bad_type;
3966 while (qemu_isspace(*p))
3967 p++;
3968 if (*p == '-') {
3969 p++;
3970 if(c != *p) {
3971 if(!is_valid_option(p, typestr)) {
3973 monitor_printf(mon, "%s: unsupported option -%c\n",
3974 cmd->name, *p);
3975 goto fail;
3976 } else {
3977 skip_key = 1;
3980 if(skip_key) {
3981 p = tmp;
3982 } else {
3983 /* has option */
3984 p++;
3985 qdict_put(qdict, key, qbool_from_bool(true));
3989 break;
3990 case 'S':
3992 /* package all remaining string */
3993 int len;
3995 while (qemu_isspace(*p)) {
3996 p++;
3998 if (*typestr == '?') {
3999 typestr++;
4000 if (*p == '\0') {
4001 /* no remaining string: NULL argument */
4002 break;
4005 len = strlen(p);
4006 if (len <= 0) {
4007 monitor_printf(mon, "%s: string expected\n",
4008 cmd->name);
4009 goto fail;
4011 qdict_put(qdict, key, qstring_from_str(p));
4012 p += len;
4014 break;
4015 default:
4016 bad_type:
4017 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
4018 goto fail;
4020 g_free(key);
4021 key = NULL;
4023 /* check that all arguments were parsed */
4024 while (qemu_isspace(*p))
4025 p++;
4026 if (*p != '\0') {
4027 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4028 cmd->name);
4029 goto fail;
4032 return qdict;
4034 fail:
4035 QDECREF(qdict);
4036 g_free(key);
4037 return NULL;
4040 static void handle_hmp_command(Monitor *mon, const char *cmdline)
4042 QDict *qdict;
4043 const mon_cmd_t *cmd;
4045 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4046 if (!cmd) {
4047 return;
4050 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4051 if (!qdict) {
4052 monitor_printf(mon, "Try \"help %s\" for more information\n",
4053 cmd->name);
4054 return;
4057 cmd->mhandler.cmd(mon, qdict);
4058 QDECREF(qdict);
4061 static void cmd_completion(Monitor *mon, const char *name, const char *list)
4063 const char *p, *pstart;
4064 char cmd[128];
4065 int len;
4067 p = list;
4068 for(;;) {
4069 pstart = p;
4070 p = strchr(p, '|');
4071 if (!p)
4072 p = pstart + strlen(pstart);
4073 len = p - pstart;
4074 if (len > sizeof(cmd) - 2)
4075 len = sizeof(cmd) - 2;
4076 memcpy(cmd, pstart, len);
4077 cmd[len] = '\0';
4078 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
4079 readline_add_completion(mon->rs, cmd);
4081 if (*p == '\0')
4082 break;
4083 p++;
4087 static void file_completion(Monitor *mon, const char *input)
4089 DIR *ffs;
4090 struct dirent *d;
4091 char path[1024];
4092 char file[1024], file_prefix[1024];
4093 int input_path_len;
4094 const char *p;
4096 p = strrchr(input, '/');
4097 if (!p) {
4098 input_path_len = 0;
4099 pstrcpy(file_prefix, sizeof(file_prefix), input);
4100 pstrcpy(path, sizeof(path), ".");
4101 } else {
4102 input_path_len = p - input + 1;
4103 memcpy(path, input, input_path_len);
4104 if (input_path_len > sizeof(path) - 1)
4105 input_path_len = sizeof(path) - 1;
4106 path[input_path_len] = '\0';
4107 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4110 ffs = opendir(path);
4111 if (!ffs)
4112 return;
4113 for(;;) {
4114 struct stat sb;
4115 d = readdir(ffs);
4116 if (!d)
4117 break;
4119 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4120 continue;
4123 if (strstart(d->d_name, file_prefix, NULL)) {
4124 memcpy(file, input, input_path_len);
4125 if (input_path_len < sizeof(file))
4126 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4127 d->d_name);
4128 /* stat the file to find out if it's a directory.
4129 * In that case add a slash to speed up typing long paths
4131 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
4132 pstrcat(file, sizeof(file), "/");
4134 readline_add_completion(mon->rs, file);
4137 closedir(ffs);
4140 static const char *next_arg_type(const char *typestr)
4142 const char *p = strchr(typestr, ':');
4143 return (p != NULL ? ++p : typestr);
4146 static void add_completion_option(ReadLineState *rs, const char *str,
4147 const char *option)
4149 if (!str || !option) {
4150 return;
4152 if (!strncmp(option, str, strlen(str))) {
4153 readline_add_completion(rs, option);
4157 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4159 size_t len;
4160 ChardevBackendInfoList *list, *start;
4162 if (nb_args != 2) {
4163 return;
4165 len = strlen(str);
4166 readline_set_completion_index(rs, len);
4168 start = list = qmp_query_chardev_backends(NULL);
4169 while (list) {
4170 const char *chr_name = list->value->name;
4172 if (!strncmp(chr_name, str, len)) {
4173 readline_add_completion(rs, chr_name);
4175 list = list->next;
4177 qapi_free_ChardevBackendInfoList(start);
4180 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4182 size_t len;
4183 int i;
4185 if (nb_args != 2) {
4186 return;
4188 len = strlen(str);
4189 readline_set_completion_index(rs, len);
4190 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4191 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4195 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
4197 GSList *list, *elt;
4198 size_t len;
4200 if (nb_args != 2) {
4201 return;
4204 len = strlen(str);
4205 readline_set_completion_index(rs, len);
4206 list = elt = object_class_get_list(TYPE_DEVICE, false);
4207 while (elt) {
4208 const char *name;
4209 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4210 TYPE_DEVICE);
4211 name = object_class_get_name(OBJECT_CLASS(dc));
4213 if (!dc->cannot_instantiate_with_device_add_yet
4214 && !strncmp(name, str, len)) {
4215 readline_add_completion(rs, name);
4217 elt = elt->next;
4219 g_slist_free(list);
4222 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
4224 GSList *list, *elt;
4225 size_t len;
4227 if (nb_args != 2) {
4228 return;
4231 len = strlen(str);
4232 readline_set_completion_index(rs, len);
4233 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4234 while (elt) {
4235 const char *name;
4237 name = object_class_get_name(OBJECT_CLASS(elt->data));
4238 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4239 readline_add_completion(rs, name);
4241 elt = elt->next;
4243 g_slist_free(list);
4246 static void peripheral_device_del_completion(ReadLineState *rs,
4247 const char *str, size_t len)
4249 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4250 GSList *list, *item;
4252 list = qdev_build_hotpluggable_device_list(peripheral);
4253 if (!list) {
4254 return;
4257 for (item = list; item; item = g_slist_next(item)) {
4258 DeviceState *dev = item->data;
4260 if (dev->id && !strncmp(str, dev->id, len)) {
4261 readline_add_completion(rs, dev->id);
4265 g_slist_free(list);
4268 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4270 size_t len;
4271 ChardevInfoList *list, *start;
4273 if (nb_args != 2) {
4274 return;
4276 len = strlen(str);
4277 readline_set_completion_index(rs, len);
4279 start = list = qmp_query_chardev(NULL);
4280 while (list) {
4281 ChardevInfo *chr = list->value;
4283 if (!strncmp(chr->label, str, len)) {
4284 readline_add_completion(rs, chr->label);
4286 list = list->next;
4288 qapi_free_ChardevInfoList(start);
4291 static void ringbuf_completion(ReadLineState *rs, const char *str)
4293 size_t len;
4294 ChardevInfoList *list, *start;
4296 len = strlen(str);
4297 readline_set_completion_index(rs, len);
4299 start = list = qmp_query_chardev(NULL);
4300 while (list) {
4301 ChardevInfo *chr_info = list->value;
4303 if (!strncmp(chr_info->label, str, len)) {
4304 CharDriverState *chr = qemu_chr_find(chr_info->label);
4305 if (chr && chr_is_ringbuf(chr)) {
4306 readline_add_completion(rs, chr_info->label);
4309 list = list->next;
4311 qapi_free_ChardevInfoList(start);
4314 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4316 if (nb_args != 2) {
4317 return;
4319 ringbuf_completion(rs, str);
4322 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4324 size_t len;
4326 if (nb_args != 2) {
4327 return;
4330 len = strlen(str);
4331 readline_set_completion_index(rs, len);
4332 peripheral_device_del_completion(rs, str, len);
4335 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
4337 ObjectPropertyInfoList *list, *start;
4338 size_t len;
4340 if (nb_args != 2) {
4341 return;
4343 len = strlen(str);
4344 readline_set_completion_index(rs, len);
4346 start = list = qmp_qom_list("/objects", NULL);
4347 while (list) {
4348 ObjectPropertyInfo *info = list->value;
4350 if (!strncmp(info->type, "child<", 5)
4351 && !strncmp(info->name, str, len)) {
4352 readline_add_completion(rs, info->name);
4354 list = list->next;
4356 qapi_free_ObjectPropertyInfoList(start);
4359 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4361 int i;
4362 char *sep;
4363 size_t len;
4365 if (nb_args != 2) {
4366 return;
4368 sep = strrchr(str, '-');
4369 if (sep) {
4370 str = sep + 1;
4372 len = strlen(str);
4373 readline_set_completion_index(rs, len);
4374 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4375 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4376 readline_add_completion(rs, QKeyCode_lookup[i]);
4381 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4383 size_t len;
4385 len = strlen(str);
4386 readline_set_completion_index(rs, len);
4387 if (nb_args == 2) {
4388 NetClientState *ncs[MAX_QUEUE_NUM];
4389 int count, i;
4390 count = qemu_find_net_clients_except(NULL, ncs,
4391 NET_CLIENT_OPTIONS_KIND_NONE,
4392 MAX_QUEUE_NUM);
4393 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4394 const char *name = ncs[i]->name;
4395 if (!strncmp(str, name, len)) {
4396 readline_add_completion(rs, name);
4399 } else if (nb_args == 3) {
4400 add_completion_option(rs, str, "on");
4401 add_completion_option(rs, str, "off");
4405 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4407 int len, count, i;
4408 NetClientState *ncs[MAX_QUEUE_NUM];
4410 if (nb_args != 2) {
4411 return;
4414 len = strlen(str);
4415 readline_set_completion_index(rs, len);
4416 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
4417 MAX_QUEUE_NUM);
4418 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4419 QemuOpts *opts;
4420 const char *name = ncs[i]->name;
4421 if (strncmp(str, name, len)) {
4422 continue;
4424 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4425 if (opts) {
4426 readline_add_completion(rs, name);
4431 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4433 int i;
4435 if (nb_args != 2) {
4436 return;
4438 readline_set_completion_index(rs, strlen(str));
4439 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4440 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4444 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4445 const char *str)
4447 size_t len;
4449 len = strlen(str);
4450 readline_set_completion_index(rs, len);
4451 if (nb_args == 2) {
4452 int i;
4453 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4454 const char *name = MigrationCapability_lookup[i];
4455 if (!strncmp(str, name, len)) {
4456 readline_add_completion(rs, name);
4459 } else if (nb_args == 3) {
4460 add_completion_option(rs, str, "on");
4461 add_completion_option(rs, str, "off");
4465 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4466 const char *str)
4468 size_t len;
4470 len = strlen(str);
4471 readline_set_completion_index(rs, len);
4472 if (nb_args == 2) {
4473 int i;
4474 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4475 const char *name = MigrationParameter_lookup[i];
4476 if (!strncmp(str, name, len)) {
4477 readline_add_completion(rs, name);
4483 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4485 int i;
4486 size_t len;
4487 if (nb_args != 2) {
4488 return;
4490 len = strlen(str);
4491 readline_set_completion_index(rs, len);
4492 for (i = 0; host_net_devices[i]; i++) {
4493 if (!strncmp(host_net_devices[i], str, len)) {
4494 readline_add_completion(rs, host_net_devices[i]);
4499 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4501 NetClientState *ncs[MAX_QUEUE_NUM];
4502 int count, i, len;
4504 len = strlen(str);
4505 readline_set_completion_index(rs, len);
4506 if (nb_args == 2) {
4507 count = qemu_find_net_clients_except(NULL, ncs,
4508 NET_CLIENT_OPTIONS_KIND_NONE,
4509 MAX_QUEUE_NUM);
4510 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4511 int id;
4512 char name[16];
4514 if (net_hub_id_for_client(ncs[i], &id)) {
4515 continue;
4517 snprintf(name, sizeof(name), "%d", id);
4518 if (!strncmp(str, name, len)) {
4519 readline_add_completion(rs, name);
4522 return;
4523 } else if (nb_args == 3) {
4524 count = qemu_find_net_clients_except(NULL, ncs,
4525 NET_CLIENT_OPTIONS_KIND_NIC,
4526 MAX_QUEUE_NUM);
4527 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4528 int id;
4529 const char *name;
4531 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4532 net_hub_id_for_client(ncs[i], &id)) {
4533 continue;
4535 name = ncs[i]->name;
4536 if (!strncmp(str, name, len)) {
4537 readline_add_completion(rs, name);
4540 return;
4544 static void vm_completion(ReadLineState *rs, const char *str)
4546 size_t len;
4547 BlockDriverState *bs = NULL;
4549 len = strlen(str);
4550 readline_set_completion_index(rs, len);
4551 while ((bs = bdrv_next(bs))) {
4552 SnapshotInfoList *snapshots, *snapshot;
4554 if (!bdrv_can_snapshot(bs)) {
4555 continue;
4557 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4558 continue;
4560 snapshot = snapshots;
4561 while (snapshot) {
4562 char *completion = snapshot->value->name;
4563 if (!strncmp(str, completion, len)) {
4564 readline_add_completion(rs, completion);
4566 completion = snapshot->value->id;
4567 if (!strncmp(str, completion, len)) {
4568 readline_add_completion(rs, completion);
4570 snapshot = snapshot->next;
4572 qapi_free_SnapshotInfoList(snapshots);
4577 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4579 if (nb_args == 2) {
4580 vm_completion(rs, str);
4584 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4586 if (nb_args == 2) {
4587 vm_completion(rs, str);
4591 static void monitor_find_completion_by_table(Monitor *mon,
4592 const mon_cmd_t *cmd_table,
4593 char **args,
4594 int nb_args)
4596 const char *cmdname;
4597 int i;
4598 const char *ptype, *str, *name;
4599 const mon_cmd_t *cmd;
4600 BlockDriverState *bs;
4602 if (nb_args <= 1) {
4603 /* command completion */
4604 if (nb_args == 0)
4605 cmdname = "";
4606 else
4607 cmdname = args[0];
4608 readline_set_completion_index(mon->rs, strlen(cmdname));
4609 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
4610 cmd_completion(mon, cmdname, cmd->name);
4612 } else {
4613 /* find the command */
4614 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
4615 if (compare_cmd(args[0], cmd->name)) {
4616 break;
4619 if (!cmd->name) {
4620 return;
4623 if (cmd->sub_table) {
4624 /* do the job again */
4625 monitor_find_completion_by_table(mon, cmd->sub_table,
4626 &args[1], nb_args - 1);
4627 return;
4629 if (cmd->command_completion) {
4630 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4631 return;
4634 ptype = next_arg_type(cmd->args_type);
4635 for(i = 0; i < nb_args - 2; i++) {
4636 if (*ptype != '\0') {
4637 ptype = next_arg_type(ptype);
4638 while (*ptype == '?')
4639 ptype = next_arg_type(ptype);
4642 str = args[nb_args - 1];
4643 while (*ptype == '-' && ptype[1] != '\0') {
4644 ptype = next_arg_type(ptype);
4646 switch(*ptype) {
4647 case 'F':
4648 /* file completion */
4649 readline_set_completion_index(mon->rs, strlen(str));
4650 file_completion(mon, str);
4651 break;
4652 case 'B':
4653 /* block device name completion */
4654 readline_set_completion_index(mon->rs, strlen(str));
4655 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4656 name = bdrv_get_device_name(bs);
4657 if (str[0] == '\0' ||
4658 !strncmp(name, str, strlen(str))) {
4659 readline_add_completion(mon->rs, name);
4662 break;
4663 case 's':
4664 case 'S':
4665 if (!strcmp(cmd->name, "help|?")) {
4666 monitor_find_completion_by_table(mon, cmd_table,
4667 &args[1], nb_args - 1);
4669 break;
4670 default:
4671 break;
4676 static void monitor_find_completion(void *opaque,
4677 const char *cmdline)
4679 Monitor *mon = opaque;
4680 char *args[MAX_ARGS];
4681 int nb_args, len;
4683 /* 1. parse the cmdline */
4684 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4685 return;
4688 /* if the line ends with a space, it means we want to complete the
4689 next arg */
4690 len = strlen(cmdline);
4691 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4692 if (nb_args >= MAX_ARGS) {
4693 goto cleanup;
4695 args[nb_args++] = g_strdup("");
4698 /* 2. auto complete according to args */
4699 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
4701 cleanup:
4702 free_cmdline_args(args, nb_args);
4705 static int monitor_can_read(void *opaque)
4707 Monitor *mon = opaque;
4709 return (mon->suspend_cnt == 0) ? 1 : 0;
4712 static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4713 Error **errp)
4715 bool is_cap = cmd->mhandler.cmd_new == qmp_capabilities;
4717 if (is_cap && mon->qmp.in_command_mode) {
4718 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4719 "Capabilities negotiation is already complete, command "
4720 "'%s' ignored", cmd->name);
4721 return true;
4723 if (!is_cap && !mon->qmp.in_command_mode) {
4724 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4725 "Expecting capabilities negotiation with "
4726 "'qmp_capabilities' before command '%s'", cmd->name);
4727 return true;
4729 return false;
4733 * Argument validation rules:
4735 * 1. The argument must exist in cmd_args qdict
4736 * 2. The argument type must be the expected one
4738 * Special case: If the argument doesn't exist in cmd_args and
4739 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4740 * checking is skipped for it.
4742 static void check_client_args_type(const QDict *client_args,
4743 const QDict *cmd_args, int flags,
4744 Error **errp)
4746 const QDictEntry *ent;
4748 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4749 QObject *obj;
4750 QString *arg_type;
4751 const QObject *client_arg = qdict_entry_value(ent);
4752 const char *client_arg_name = qdict_entry_key(ent);
4754 obj = qdict_get(cmd_args, client_arg_name);
4755 if (!obj) {
4756 if (flags & QMP_ACCEPT_UNKNOWNS) {
4757 /* handler accepts unknowns */
4758 continue;
4760 /* client arg doesn't exist */
4761 error_setg(errp, QERR_INVALID_PARAMETER, client_arg_name);
4762 return;
4765 arg_type = qobject_to_qstring(obj);
4766 assert(arg_type != NULL);
4768 /* check if argument's type is correct */
4769 switch (qstring_get_str(arg_type)[0]) {
4770 case 'F':
4771 case 'B':
4772 case 's':
4773 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4774 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4775 client_arg_name, "string");
4776 return;
4778 break;
4779 case 'i':
4780 case 'l':
4781 case 'M':
4782 case 'o':
4783 if (qobject_type(client_arg) != QTYPE_QINT) {
4784 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4785 client_arg_name, "int");
4786 return;
4788 break;
4789 case 'T':
4790 if (qobject_type(client_arg) != QTYPE_QINT &&
4791 qobject_type(client_arg) != QTYPE_QFLOAT) {
4792 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4793 client_arg_name, "number");
4794 return;
4796 break;
4797 case 'b':
4798 case '-':
4799 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4800 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
4801 client_arg_name, "bool");
4802 return;
4804 break;
4805 case 'O':
4806 assert(flags & QMP_ACCEPT_UNKNOWNS);
4807 break;
4808 case 'q':
4809 /* Any QObject can be passed. */
4810 break;
4811 case '/':
4812 case '.':
4814 * These types are not supported by QMP and thus are not
4815 * handled here. Fall through.
4817 default:
4818 abort();
4824 * - Check if the client has passed all mandatory args
4825 * - Set special flags for argument validation
4827 static void check_mandatory_args(const QDict *cmd_args,
4828 const QDict *client_args, int *flags,
4829 Error **errp)
4831 const QDictEntry *ent;
4833 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4834 const char *cmd_arg_name = qdict_entry_key(ent);
4835 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4836 assert(type != NULL);
4838 if (qstring_get_str(type)[0] == 'O') {
4839 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4840 *flags |= QMP_ACCEPT_UNKNOWNS;
4841 } else if (qstring_get_str(type)[0] != '-' &&
4842 qstring_get_str(type)[1] != '?' &&
4843 !qdict_haskey(client_args, cmd_arg_name)) {
4844 error_setg(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4845 return;
4850 static QDict *qdict_from_args_type(const char *args_type)
4852 int i;
4853 QDict *qdict;
4854 QString *key, *type, *cur_qs;
4856 assert(args_type != NULL);
4858 qdict = qdict_new();
4860 if (args_type == NULL || args_type[0] == '\0') {
4861 /* no args, empty qdict */
4862 goto out;
4865 key = qstring_new();
4866 type = qstring_new();
4868 cur_qs = key;
4870 for (i = 0;; i++) {
4871 switch (args_type[i]) {
4872 case ',':
4873 case '\0':
4874 qdict_put(qdict, qstring_get_str(key), type);
4875 QDECREF(key);
4876 if (args_type[i] == '\0') {
4877 goto out;
4879 type = qstring_new(); /* qdict has ref */
4880 cur_qs = key = qstring_new();
4881 break;
4882 case ':':
4883 cur_qs = type;
4884 break;
4885 default:
4886 qstring_append_chr(cur_qs, args_type[i]);
4887 break;
4891 out:
4892 return qdict;
4896 * Client argument checking rules:
4898 * 1. Client must provide all mandatory arguments
4899 * 2. Each argument provided by the client must be expected
4900 * 3. Each argument provided by the client must have the type expected
4901 * by the command
4903 static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4904 Error **errp)
4906 Error *err = NULL;
4907 int flags;
4908 QDict *cmd_args;
4910 cmd_args = qdict_from_args_type(cmd->args_type);
4912 flags = 0;
4913 check_mandatory_args(cmd_args, client_args, &flags, &err);
4914 if (err) {
4915 goto out;
4918 check_client_args_type(client_args, cmd_args, flags, &err);
4920 out:
4921 error_propagate(errp, err);
4922 QDECREF(cmd_args);
4926 * Input object checking rules
4928 * 1. Input object must be a dict
4929 * 2. The "execute" key must exist
4930 * 3. The "execute" key must be a string
4931 * 4. If the "arguments" key exists, it must be a dict
4932 * 5. If the "id" key exists, it can be anything (ie. json-value)
4933 * 6. Any argument not listed above is considered invalid
4935 static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
4937 const QDictEntry *ent;
4938 int has_exec_key = 0;
4939 QDict *input_dict;
4941 if (qobject_type(input_obj) != QTYPE_QDICT) {
4942 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
4943 return NULL;
4946 input_dict = qobject_to_qdict(input_obj);
4948 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4949 const char *arg_name = qdict_entry_key(ent);
4950 const QObject *arg_obj = qdict_entry_value(ent);
4952 if (!strcmp(arg_name, "execute")) {
4953 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4954 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4955 "execute", "string");
4956 return NULL;
4958 has_exec_key = 1;
4959 } else if (!strcmp(arg_name, "arguments")) {
4960 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4961 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4962 "arguments", "object");
4963 return NULL;
4965 } else if (!strcmp(arg_name, "id")) {
4966 /* Any string is acceptable as "id", so nothing to check */
4967 } else {
4968 error_setg(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
4969 return NULL;
4973 if (!has_exec_key) {
4974 error_setg(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
4975 return NULL;
4978 return input_dict;
4981 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4983 Error *local_err = NULL;
4984 QObject *obj, *data;
4985 QDict *input, *args;
4986 const mon_cmd_t *cmd;
4987 const char *cmd_name;
4988 Monitor *mon = cur_mon;
4990 args = input = NULL;
4991 data = NULL;
4993 obj = json_parser_parse(tokens, NULL);
4994 if (!obj) {
4995 // FIXME: should be triggered in json_parser_parse()
4996 error_setg(&local_err, QERR_JSON_PARSING);
4997 goto err_out;
5000 input = qmp_check_input_obj(obj, &local_err);
5001 if (!input) {
5002 qobject_decref(obj);
5003 goto err_out;
5006 mon->qmp.id = qdict_get(input, "id");
5007 qobject_incref(mon->qmp.id);
5009 cmd_name = qdict_get_str(input, "execute");
5010 trace_handle_qmp_command(mon, cmd_name);
5011 cmd = qmp_find_cmd(cmd_name);
5012 if (!cmd) {
5013 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5014 "The command %s has not been found", cmd_name);
5015 goto err_out;
5017 if (invalid_qmp_mode(mon, cmd, &local_err)) {
5018 goto err_out;
5021 obj = qdict_get(input, "arguments");
5022 if (!obj) {
5023 args = qdict_new();
5024 } else {
5025 args = qobject_to_qdict(obj);
5026 QINCREF(args);
5029 qmp_check_client_args(cmd, args, &local_err);
5030 if (local_err) {
5031 goto err_out;
5034 cmd->mhandler.cmd_new(args, &data, &local_err);
5036 err_out:
5037 monitor_protocol_emitter(mon, data, local_err);
5038 qobject_decref(data);
5039 QDECREF(input);
5040 QDECREF(args);
5043 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
5045 Monitor *old_mon = cur_mon;
5047 cur_mon = opaque;
5049 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
5051 cur_mon = old_mon;
5054 static void monitor_read(void *opaque, const uint8_t *buf, int size)
5056 Monitor *old_mon = cur_mon;
5057 int i;
5059 cur_mon = opaque;
5061 if (cur_mon->rs) {
5062 for (i = 0; i < size; i++)
5063 readline_handle_byte(cur_mon->rs, buf[i]);
5064 } else {
5065 if (size == 0 || buf[size - 1] != 0)
5066 monitor_printf(cur_mon, "corrupted command\n");
5067 else
5068 handle_hmp_command(cur_mon, (char *)buf);
5071 cur_mon = old_mon;
5074 static void monitor_command_cb(void *opaque, const char *cmdline,
5075 void *readline_opaque)
5077 Monitor *mon = opaque;
5079 monitor_suspend(mon);
5080 handle_hmp_command(mon, cmdline);
5081 monitor_resume(mon);
5084 int monitor_suspend(Monitor *mon)
5086 if (!mon->rs)
5087 return -ENOTTY;
5088 mon->suspend_cnt++;
5089 return 0;
5092 void monitor_resume(Monitor *mon)
5094 if (!mon->rs)
5095 return;
5096 if (--mon->suspend_cnt == 0)
5097 readline_show_prompt(mon->rs);
5100 static QObject *get_qmp_greeting(void)
5102 QObject *ver = NULL;
5104 qmp_marshal_input_query_version(NULL, &ver, NULL);
5105 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5108 static void monitor_qmp_event(void *opaque, int event)
5110 QObject *data;
5111 Monitor *mon = opaque;
5113 switch (event) {
5114 case CHR_EVENT_OPENED:
5115 mon->qmp.in_command_mode = false;
5116 data = get_qmp_greeting();
5117 monitor_json_emitter(mon, data);
5118 qobject_decref(data);
5119 mon_refcount++;
5120 break;
5121 case CHR_EVENT_CLOSED:
5122 json_message_parser_destroy(&mon->qmp.parser);
5123 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
5124 mon_refcount--;
5125 monitor_fdsets_cleanup();
5126 break;
5130 static void monitor_event(void *opaque, int event)
5132 Monitor *mon = opaque;
5134 switch (event) {
5135 case CHR_EVENT_MUX_IN:
5136 qemu_mutex_lock(&mon->out_lock);
5137 mon->mux_out = 0;
5138 qemu_mutex_unlock(&mon->out_lock);
5139 if (mon->reset_seen) {
5140 readline_restart(mon->rs);
5141 monitor_resume(mon);
5142 monitor_flush(mon);
5143 } else {
5144 mon->suspend_cnt = 0;
5146 break;
5148 case CHR_EVENT_MUX_OUT:
5149 if (mon->reset_seen) {
5150 if (mon->suspend_cnt == 0) {
5151 monitor_printf(mon, "\n");
5153 monitor_flush(mon);
5154 monitor_suspend(mon);
5155 } else {
5156 mon->suspend_cnt++;
5158 qemu_mutex_lock(&mon->out_lock);
5159 mon->mux_out = 1;
5160 qemu_mutex_unlock(&mon->out_lock);
5161 break;
5163 case CHR_EVENT_OPENED:
5164 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5165 "information\n", QEMU_VERSION);
5166 if (!mon->mux_out) {
5167 readline_restart(mon->rs);
5168 readline_show_prompt(mon->rs);
5170 mon->reset_seen = 1;
5171 mon_refcount++;
5172 break;
5174 case CHR_EVENT_CLOSED:
5175 mon_refcount--;
5176 monitor_fdsets_cleanup();
5177 break;
5181 static int
5182 compare_mon_cmd(const void *a, const void *b)
5184 return strcmp(((const mon_cmd_t *)a)->name,
5185 ((const mon_cmd_t *)b)->name);
5188 static void sortcmdlist(void)
5190 int array_num;
5191 int elem_size = sizeof(mon_cmd_t);
5193 array_num = sizeof(mon_cmds)/elem_size-1;
5194 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5196 array_num = sizeof(info_cmds)/elem_size-1;
5197 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5202 * Local variables:
5203 * c-indent-level: 4
5204 * c-basic-offset: 4
5205 * tab-width: 8
5206 * End:
5209 /* These functions just adapt the readline interface in a typesafe way. We
5210 * could cast function pointers but that discards compiler checks.
5212 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5213 const char *fmt, ...)
5215 va_list ap;
5216 va_start(ap, fmt);
5217 monitor_vprintf(opaque, fmt, ap);
5218 va_end(ap);
5221 static void monitor_readline_flush(void *opaque)
5223 monitor_flush(opaque);
5226 static void __attribute__((constructor)) monitor_lock_init(void)
5228 qemu_mutex_init(&monitor_lock);
5231 void monitor_init(CharDriverState *chr, int flags)
5233 static int is_first_init = 1;
5234 Monitor *mon;
5236 if (is_first_init) {
5237 monitor_qapi_event_init();
5238 sortcmdlist();
5239 is_first_init = 0;
5242 mon = g_malloc(sizeof(*mon));
5243 monitor_data_init(mon);
5245 mon->chr = chr;
5246 mon->flags = flags;
5247 if (flags & MONITOR_USE_READLINE) {
5248 mon->rs = readline_init(monitor_readline_printf,
5249 monitor_readline_flush,
5250 mon,
5251 monitor_find_completion);
5252 monitor_read_command(mon, 0);
5255 if (monitor_is_qmp(mon)) {
5256 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5257 monitor_qmp_event, mon);
5258 qemu_chr_fe_set_echo(chr, true);
5259 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
5260 } else {
5261 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5262 monitor_event, mon);
5265 qemu_mutex_lock(&monitor_lock);
5266 QLIST_INSERT_HEAD(&mon_list, mon, entry);
5267 qemu_mutex_unlock(&monitor_lock);
5270 static void bdrv_password_cb(void *opaque, const char *password,
5271 void *readline_opaque)
5273 Monitor *mon = opaque;
5274 BlockDriverState *bs = readline_opaque;
5275 int ret = 0;
5276 Error *local_err = NULL;
5278 bdrv_add_key(bs, password, &local_err);
5279 if (local_err) {
5280 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5281 error_free(local_err);
5282 ret = -EPERM;
5284 if (mon->password_completion_cb)
5285 mon->password_completion_cb(mon->password_opaque, ret);
5287 monitor_read_command(mon, 1);
5290 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5291 BlockCompletionFunc *completion_cb,
5292 void *opaque)
5294 int err;
5296 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5297 bdrv_get_encrypted_filename(bs));
5299 mon->password_completion_cb = completion_cb;
5300 mon->password_opaque = opaque;
5302 err = monitor_read_password(mon, bdrv_password_cb, bs);
5304 if (err && completion_cb)
5305 completion_cb(opaque, err);
5307 return err;
5310 int monitor_read_block_device_key(Monitor *mon, const char *device,
5311 BlockCompletionFunc *completion_cb,
5312 void *opaque)
5314 Error *err = NULL;
5315 BlockBackend *blk;
5317 blk = blk_by_name(device);
5318 if (!blk) {
5319 monitor_printf(mon, "Device not found %s\n", device);
5320 return -1;
5323 bdrv_add_key(blk_bs(blk), NULL, &err);
5324 if (err) {
5325 error_free(err);
5326 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5329 if (completion_cb) {
5330 completion_cb(opaque, 0);
5332 return 0;
5335 QemuOptsList qemu_mon_opts = {
5336 .name = "mon",
5337 .implied_opt_name = "chardev",
5338 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5339 .desc = {
5341 .name = "mode",
5342 .type = QEMU_OPT_STRING,
5344 .name = "chardev",
5345 .type = QEMU_OPT_STRING,
5347 .name = "default",
5348 .type = QEMU_OPT_BOOL,
5350 .name = "pretty",
5351 .type = QEMU_OPT_BOOL,
5353 { /* end of list */ }
5357 #ifndef TARGET_I386
5358 void qmp_rtc_reset_reinjection(Error **errp)
5360 error_setg(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");
5362 #endif