Remove obsolete 'f' double parameter type
[qemu/stefanha.git] / monitor.c
blob8cee35d77a4c9cc36087cb0847cfe8cd8ed930d8
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 "hw/qdev.h"
27 #include "hw/usb.h"
28 #include "hw/pcmcia.h"
29 #include "hw/pc.h"
30 #include "hw/pci.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
33 #include "gdbstub.h"
34 #include "net.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "sysemu.h"
38 #include "monitor.h"
39 #include "readline.h"
40 #include "console.h"
41 #include "blockdev.h"
42 #include "audio/audio.h"
43 #include "disas.h"
44 #include "balloon.h"
45 #include "qemu-timer.h"
46 #include "migration.h"
47 #include "kvm.h"
48 #include "acl.h"
49 #include "qint.h"
50 #include "qfloat.h"
51 #include "qlist.h"
52 #include "qbool.h"
53 #include "qstring.h"
54 #include "qjson.h"
55 #include "json-streamer.h"
56 #include "json-parser.h"
57 #include "osdep.h"
58 #include "exec-all.h"
59 #ifdef CONFIG_SIMPLE_TRACE
60 #include "trace.h"
61 #endif
63 //#define DEBUG
64 //#define DEBUG_COMPLETION
67 * Supported types:
69 * 'F' filename
70 * 'B' block device name
71 * 's' string (accept optional quote)
72 * 'O' option string of the form NAME=VALUE,...
73 * parsed according to QemuOptsList given by its name
74 * Example: 'device:O' uses qemu_device_opts.
75 * Restriction: only lists with empty desc are supported
76 * TODO lift the restriction
77 * 'i' 32 bit integer
78 * 'l' target long (32 or 64 bit)
79 * 'M' just like 'l', except in user mode the value is
80 * multiplied by 2^20 (think Mebibyte)
81 * 'o' octets (aka bytes)
82 * user mode accepts an optional T, t, G, g, M, m, K, k
83 * suffix, which multiplies the value by 2^40 for
84 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
85 * M and m, 2^10 for K and k
86 * 'T' double
87 * user mode accepts an optional ms, us, ns suffix,
88 * which divides the value by 1e3, 1e6, 1e9, respectively
89 * '/' optional gdb-like print format (like "/10x")
91 * '?' optional type (for all types, except '/')
92 * '.' other form of optional type (for 'i' and 'l')
93 * 'b' boolean
94 * user mode accepts "on" or "off"
95 * '-' optional parameter (eg. '-f')
99 typedef struct MonitorCompletionData MonitorCompletionData;
100 struct MonitorCompletionData {
101 Monitor *mon;
102 void (*user_print)(Monitor *mon, const QObject *data);
105 typedef struct mon_cmd_t {
106 const char *name;
107 const char *args_type;
108 const char *params;
109 const char *help;
110 void (*user_print)(Monitor *mon, const QObject *data);
111 union {
112 void (*info)(Monitor *mon);
113 void (*info_new)(Monitor *mon, QObject **ret_data);
114 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
115 void (*cmd)(Monitor *mon, const QDict *qdict);
116 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
117 int (*cmd_async)(Monitor *mon, const QDict *params,
118 MonitorCompletion *cb, void *opaque);
119 } mhandler;
120 int flags;
121 } mon_cmd_t;
123 /* file descriptors passed via SCM_RIGHTS */
124 typedef struct mon_fd_t mon_fd_t;
125 struct mon_fd_t {
126 char *name;
127 int fd;
128 QLIST_ENTRY(mon_fd_t) next;
131 typedef struct MonitorControl {
132 QObject *id;
133 JSONMessageParser parser;
134 int command_mode;
135 } MonitorControl;
137 struct Monitor {
138 CharDriverState *chr;
139 int mux_out;
140 int reset_seen;
141 int flags;
142 int suspend_cnt;
143 uint8_t outbuf[1024];
144 int outbuf_index;
145 ReadLineState *rs;
146 MonitorControl *mc;
147 CPUState *mon_cpu;
148 BlockDriverCompletionFunc *password_completion_cb;
149 void *password_opaque;
150 #ifdef CONFIG_DEBUG_MONITOR
151 int print_calls_nr;
152 #endif
153 QError *error;
154 QLIST_HEAD(,mon_fd_t) fds;
155 QLIST_ENTRY(Monitor) entry;
158 #ifdef CONFIG_DEBUG_MONITOR
159 #define MON_DEBUG(fmt, ...) do { \
160 fprintf(stderr, "Monitor: "); \
161 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
163 static inline void mon_print_count_inc(Monitor *mon)
165 mon->print_calls_nr++;
168 static inline void mon_print_count_init(Monitor *mon)
170 mon->print_calls_nr = 0;
173 static inline int mon_print_count_get(const Monitor *mon)
175 return mon->print_calls_nr;
178 #else /* !CONFIG_DEBUG_MONITOR */
179 #define MON_DEBUG(fmt, ...) do { } while (0)
180 static inline void mon_print_count_inc(Monitor *mon) { }
181 static inline void mon_print_count_init(Monitor *mon) { }
182 static inline int mon_print_count_get(const Monitor *mon) { return 0; }
183 #endif /* CONFIG_DEBUG_MONITOR */
185 /* QMP checker flags */
186 #define QMP_ACCEPT_UNKNOWNS 1
188 static QLIST_HEAD(mon_list, Monitor) mon_list;
190 static const mon_cmd_t mon_cmds[];
191 static const mon_cmd_t info_cmds[];
193 static const mon_cmd_t qmp_cmds[];
194 static const mon_cmd_t qmp_query_cmds[];
196 Monitor *cur_mon;
197 Monitor *default_mon;
199 static void monitor_command_cb(Monitor *mon, const char *cmdline,
200 void *opaque);
202 static inline int qmp_cmd_mode(const Monitor *mon)
204 return (mon->mc ? mon->mc->command_mode : 0);
207 /* Return true if in control mode, false otherwise */
208 static inline int monitor_ctrl_mode(const Monitor *mon)
210 return (mon->flags & MONITOR_USE_CONTROL);
213 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
214 int monitor_cur_is_qmp(void)
216 return cur_mon && monitor_ctrl_mode(cur_mon);
219 static void monitor_read_command(Monitor *mon, int show_prompt)
221 if (!mon->rs)
222 return;
224 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
225 if (show_prompt)
226 readline_show_prompt(mon->rs);
229 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
230 void *opaque)
232 if (monitor_ctrl_mode(mon)) {
233 qerror_report(QERR_MISSING_PARAMETER, "password");
234 return -EINVAL;
235 } else if (mon->rs) {
236 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
237 /* prompt is printed on return from the command handler */
238 return 0;
239 } else {
240 monitor_printf(mon, "terminal does not support password prompting\n");
241 return -ENOTTY;
245 void monitor_flush(Monitor *mon)
247 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
248 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
249 mon->outbuf_index = 0;
253 /* flush at every end of line or if the buffer is full */
254 static void monitor_puts(Monitor *mon, const char *str)
256 char c;
258 for(;;) {
259 c = *str++;
260 if (c == '\0')
261 break;
262 if (c == '\n')
263 mon->outbuf[mon->outbuf_index++] = '\r';
264 mon->outbuf[mon->outbuf_index++] = c;
265 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
266 || c == '\n')
267 monitor_flush(mon);
271 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
273 char buf[4096];
275 if (!mon)
276 return;
278 mon_print_count_inc(mon);
280 if (monitor_ctrl_mode(mon)) {
281 return;
284 vsnprintf(buf, sizeof(buf), fmt, ap);
285 monitor_puts(mon, buf);
288 void monitor_printf(Monitor *mon, const char *fmt, ...)
290 va_list ap;
291 va_start(ap, fmt);
292 monitor_vprintf(mon, fmt, ap);
293 va_end(ap);
296 void monitor_print_filename(Monitor *mon, const char *filename)
298 int i;
300 for (i = 0; filename[i]; i++) {
301 switch (filename[i]) {
302 case ' ':
303 case '"':
304 case '\\':
305 monitor_printf(mon, "\\%c", filename[i]);
306 break;
307 case '\t':
308 monitor_printf(mon, "\\t");
309 break;
310 case '\r':
311 monitor_printf(mon, "\\r");
312 break;
313 case '\n':
314 monitor_printf(mon, "\\n");
315 break;
316 default:
317 monitor_printf(mon, "%c", filename[i]);
318 break;
323 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
324 const char *fmt, ...)
326 va_list ap;
327 va_start(ap, fmt);
328 monitor_vprintf((Monitor *)stream, fmt, ap);
329 va_end(ap);
330 return 0;
333 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
335 static inline int handler_is_qobject(const mon_cmd_t *cmd)
337 return cmd->user_print != NULL;
340 static inline bool handler_is_async(const mon_cmd_t *cmd)
342 return cmd->flags & MONITOR_CMD_ASYNC;
345 static inline int monitor_has_error(const Monitor *mon)
347 return mon->error != NULL;
350 static void monitor_json_emitter(Monitor *mon, const QObject *data)
352 QString *json;
354 if (mon->flags & MONITOR_USE_PRETTY)
355 json = qobject_to_json_pretty(data);
356 else
357 json = qobject_to_json(data);
358 assert(json != NULL);
360 qstring_append_chr(json, '\n');
361 monitor_puts(mon, qstring_get_str(json));
363 QDECREF(json);
366 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
368 QDict *qmp;
370 qmp = qdict_new();
372 if (!monitor_has_error(mon)) {
373 /* success response */
374 if (data) {
375 qobject_incref(data);
376 qdict_put_obj(qmp, "return", data);
377 } else {
378 /* return an empty QDict by default */
379 qdict_put(qmp, "return", qdict_new());
381 } else {
382 /* error response */
383 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
384 qdict_put(qmp, "error", mon->error->error);
385 QINCREF(mon->error->error);
386 QDECREF(mon->error);
387 mon->error = NULL;
390 if (mon->mc->id) {
391 qdict_put_obj(qmp, "id", mon->mc->id);
392 mon->mc->id = NULL;
395 monitor_json_emitter(mon, QOBJECT(qmp));
396 QDECREF(qmp);
399 static void timestamp_put(QDict *qdict)
401 int err;
402 QObject *obj;
403 qemu_timeval tv;
405 err = qemu_gettimeofday(&tv);
406 if (err < 0)
407 return;
409 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
410 "'microseconds': %" PRId64 " }",
411 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
412 qdict_put_obj(qdict, "timestamp", obj);
416 * monitor_protocol_event(): Generate a Monitor event
418 * Event-specific data can be emitted through the (optional) 'data' parameter.
420 void monitor_protocol_event(MonitorEvent event, QObject *data)
422 QDict *qmp;
423 const char *event_name;
424 Monitor *mon;
426 assert(event < QEVENT_MAX);
428 switch (event) {
429 case QEVENT_SHUTDOWN:
430 event_name = "SHUTDOWN";
431 break;
432 case QEVENT_RESET:
433 event_name = "RESET";
434 break;
435 case QEVENT_POWERDOWN:
436 event_name = "POWERDOWN";
437 break;
438 case QEVENT_STOP:
439 event_name = "STOP";
440 break;
441 case QEVENT_RESUME:
442 event_name = "RESUME";
443 break;
444 case QEVENT_VNC_CONNECTED:
445 event_name = "VNC_CONNECTED";
446 break;
447 case QEVENT_VNC_INITIALIZED:
448 event_name = "VNC_INITIALIZED";
449 break;
450 case QEVENT_VNC_DISCONNECTED:
451 event_name = "VNC_DISCONNECTED";
452 break;
453 case QEVENT_BLOCK_IO_ERROR:
454 event_name = "BLOCK_IO_ERROR";
455 break;
456 case QEVENT_RTC_CHANGE:
457 event_name = "RTC_CHANGE";
458 break;
459 case QEVENT_WATCHDOG:
460 event_name = "WATCHDOG";
461 break;
462 default:
463 abort();
464 break;
467 qmp = qdict_new();
468 timestamp_put(qmp);
469 qdict_put(qmp, "event", qstring_from_str(event_name));
470 if (data) {
471 qobject_incref(data);
472 qdict_put_obj(qmp, "data", data);
475 QLIST_FOREACH(mon, &mon_list, entry) {
476 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
477 monitor_json_emitter(mon, QOBJECT(qmp));
480 QDECREF(qmp);
483 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
484 QObject **ret_data)
486 /* Will setup QMP capabilities in the future */
487 if (monitor_ctrl_mode(mon)) {
488 mon->mc->command_mode = 1;
491 return 0;
494 static int compare_cmd(const char *name, const char *list)
496 const char *p, *pstart;
497 int len;
498 len = strlen(name);
499 p = list;
500 for(;;) {
501 pstart = p;
502 p = strchr(p, '|');
503 if (!p)
504 p = pstart + strlen(pstart);
505 if ((p - pstart) == len && !memcmp(pstart, name, len))
506 return 1;
507 if (*p == '\0')
508 break;
509 p++;
511 return 0;
514 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
515 const char *prefix, const char *name)
517 const mon_cmd_t *cmd;
519 for(cmd = cmds; cmd->name != NULL; cmd++) {
520 if (!name || !strcmp(name, cmd->name))
521 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
522 cmd->params, cmd->help);
526 static void help_cmd(Monitor *mon, const char *name)
528 if (name && !strcmp(name, "info")) {
529 help_cmd_dump(mon, info_cmds, "info ", NULL);
530 } else {
531 help_cmd_dump(mon, mon_cmds, "", name);
532 if (name && !strcmp(name, "log")) {
533 const CPULogItem *item;
534 monitor_printf(mon, "Log items (comma separated):\n");
535 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
536 for(item = cpu_log_items; item->mask != 0; item++) {
537 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
543 static void do_help_cmd(Monitor *mon, const QDict *qdict)
545 help_cmd(mon, qdict_get_try_str(qdict, "name"));
548 #ifdef CONFIG_SIMPLE_TRACE
549 static void do_change_trace_event_state(Monitor *mon, const QDict *qdict)
551 const char *tp_name = qdict_get_str(qdict, "name");
552 bool new_state = qdict_get_bool(qdict, "option");
553 int ret = st_change_trace_event_state(tp_name, new_state);
555 if (!ret) {
556 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
560 static void do_trace_file(Monitor *mon, const QDict *qdict)
562 const char *op = qdict_get_try_str(qdict, "op");
563 const char *arg = qdict_get_try_str(qdict, "arg");
565 if (!op) {
566 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
567 } else if (!strcmp(op, "on")) {
568 st_set_trace_file_enabled(true);
569 } else if (!strcmp(op, "off")) {
570 st_set_trace_file_enabled(false);
571 } else if (!strcmp(op, "flush")) {
572 st_flush_trace_buffer();
573 } else if (!strcmp(op, "set")) {
574 if (arg) {
575 st_set_trace_file(arg);
577 } else {
578 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
579 help_cmd(mon, "trace-file");
582 #endif
584 static void user_monitor_complete(void *opaque, QObject *ret_data)
586 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
588 if (ret_data) {
589 data->user_print(data->mon, ret_data);
591 monitor_resume(data->mon);
592 qemu_free(data);
595 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
597 monitor_protocol_emitter(opaque, ret_data);
600 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
601 const QDict *params)
603 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
606 static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
608 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
611 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
612 const QDict *params)
614 int ret;
616 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
617 cb_data->mon = mon;
618 cb_data->user_print = cmd->user_print;
619 monitor_suspend(mon);
620 ret = cmd->mhandler.cmd_async(mon, params,
621 user_monitor_complete, cb_data);
622 if (ret < 0) {
623 monitor_resume(mon);
624 qemu_free(cb_data);
628 static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
630 int ret;
632 MonitorCompletionData *cb_data = qemu_malloc(sizeof(*cb_data));
633 cb_data->mon = mon;
634 cb_data->user_print = cmd->user_print;
635 monitor_suspend(mon);
636 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
637 if (ret < 0) {
638 monitor_resume(mon);
639 qemu_free(cb_data);
643 static void do_info(Monitor *mon, const QDict *qdict)
645 const mon_cmd_t *cmd;
646 const char *item = qdict_get_try_str(qdict, "item");
648 if (!item) {
649 goto help;
652 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
653 if (compare_cmd(item, cmd->name))
654 break;
657 if (cmd->name == NULL) {
658 goto help;
661 if (handler_is_async(cmd)) {
662 user_async_info_handler(mon, cmd);
663 } else if (handler_is_qobject(cmd)) {
664 QObject *info_data = NULL;
666 cmd->mhandler.info_new(mon, &info_data);
667 if (info_data) {
668 cmd->user_print(mon, info_data);
669 qobject_decref(info_data);
671 } else {
672 cmd->mhandler.info(mon);
675 return;
677 help:
678 help_cmd(mon, "info");
681 static void do_info_version_print(Monitor *mon, const QObject *data)
683 QDict *qdict;
684 QDict *qemu;
686 qdict = qobject_to_qdict(data);
687 qemu = qdict_get_qdict(qdict, "qemu");
689 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
690 qdict_get_int(qemu, "major"),
691 qdict_get_int(qemu, "minor"),
692 qdict_get_int(qemu, "micro"),
693 qdict_get_str(qdict, "package"));
696 static void do_info_version(Monitor *mon, QObject **ret_data)
698 const char *version = QEMU_VERSION;
699 int major = 0, minor = 0, micro = 0;
700 char *tmp;
702 major = strtol(version, &tmp, 10);
703 tmp++;
704 minor = strtol(tmp, &tmp, 10);
705 tmp++;
706 micro = strtol(tmp, &tmp, 10);
708 *ret_data = qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
709 'micro': %d }, 'package': %s }", major, minor, micro, QEMU_PKGVERSION);
712 static void do_info_name_print(Monitor *mon, const QObject *data)
714 QDict *qdict;
716 qdict = qobject_to_qdict(data);
717 if (qdict_size(qdict) == 0) {
718 return;
721 monitor_printf(mon, "%s\n", qdict_get_str(qdict, "name"));
724 static void do_info_name(Monitor *mon, QObject **ret_data)
726 *ret_data = qemu_name ? qobject_from_jsonf("{'name': %s }", qemu_name) :
727 qobject_from_jsonf("{}");
730 static QObject *get_cmd_dict(const char *name)
732 const char *p;
734 /* Remove '|' from some commands */
735 p = strchr(name, '|');
736 if (p) {
737 p++;
738 } else {
739 p = name;
742 return qobject_from_jsonf("{ 'name': %s }", p);
745 static void do_info_commands(Monitor *mon, QObject **ret_data)
747 QList *cmd_list;
748 const mon_cmd_t *cmd;
750 cmd_list = qlist_new();
752 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
753 qlist_append_obj(cmd_list, get_cmd_dict(cmd->name));
756 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
757 char buf[128];
758 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
759 qlist_append_obj(cmd_list, get_cmd_dict(buf));
762 *ret_data = QOBJECT(cmd_list);
765 static void do_info_uuid_print(Monitor *mon, const QObject *data)
767 monitor_printf(mon, "%s\n", qdict_get_str(qobject_to_qdict(data), "UUID"));
770 static void do_info_uuid(Monitor *mon, QObject **ret_data)
772 char uuid[64];
774 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
775 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
776 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
777 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
778 qemu_uuid[14], qemu_uuid[15]);
779 *ret_data = qobject_from_jsonf("{ 'UUID': %s }", uuid);
782 /* get the current CPU defined by the user */
783 static int mon_set_cpu(int cpu_index)
785 CPUState *env;
787 for(env = first_cpu; env != NULL; env = env->next_cpu) {
788 if (env->cpu_index == cpu_index) {
789 cur_mon->mon_cpu = env;
790 return 0;
793 return -1;
796 static CPUState *mon_get_cpu(void)
798 if (!cur_mon->mon_cpu) {
799 mon_set_cpu(0);
801 cpu_synchronize_state(cur_mon->mon_cpu);
802 return cur_mon->mon_cpu;
805 static void do_info_registers(Monitor *mon)
807 CPUState *env;
808 env = mon_get_cpu();
809 #ifdef TARGET_I386
810 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
811 X86_DUMP_FPU);
812 #else
813 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
815 #endif
818 static void print_cpu_iter(QObject *obj, void *opaque)
820 QDict *cpu;
821 int active = ' ';
822 Monitor *mon = opaque;
824 assert(qobject_type(obj) == QTYPE_QDICT);
825 cpu = qobject_to_qdict(obj);
827 if (qdict_get_bool(cpu, "current")) {
828 active = '*';
831 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
833 #if defined(TARGET_I386)
834 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
835 (target_ulong) qdict_get_int(cpu, "pc"));
836 #elif defined(TARGET_PPC)
837 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
838 (target_long) qdict_get_int(cpu, "nip"));
839 #elif defined(TARGET_SPARC)
840 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
841 (target_long) qdict_get_int(cpu, "pc"));
842 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
843 (target_long) qdict_get_int(cpu, "npc"));
844 #elif defined(TARGET_MIPS)
845 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
846 (target_long) qdict_get_int(cpu, "PC"));
847 #endif
849 if (qdict_get_bool(cpu, "halted")) {
850 monitor_printf(mon, " (halted)");
853 monitor_printf(mon, "\n");
856 static void monitor_print_cpus(Monitor *mon, const QObject *data)
858 QList *cpu_list;
860 assert(qobject_type(data) == QTYPE_QLIST);
861 cpu_list = qobject_to_qlist(data);
862 qlist_iter(cpu_list, print_cpu_iter, mon);
865 static void do_info_cpus(Monitor *mon, QObject **ret_data)
867 CPUState *env;
868 QList *cpu_list;
870 cpu_list = qlist_new();
872 /* just to set the default cpu if not already done */
873 mon_get_cpu();
875 for(env = first_cpu; env != NULL; env = env->next_cpu) {
876 QDict *cpu;
877 QObject *obj;
879 cpu_synchronize_state(env);
881 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
882 env->cpu_index, env == mon->mon_cpu,
883 env->halted);
885 cpu = qobject_to_qdict(obj);
887 #if defined(TARGET_I386)
888 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
889 #elif defined(TARGET_PPC)
890 qdict_put(cpu, "nip", qint_from_int(env->nip));
891 #elif defined(TARGET_SPARC)
892 qdict_put(cpu, "pc", qint_from_int(env->pc));
893 qdict_put(cpu, "npc", qint_from_int(env->npc));
894 #elif defined(TARGET_MIPS)
895 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
896 #endif
898 qlist_append(cpu_list, cpu);
901 *ret_data = QOBJECT(cpu_list);
904 static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
906 int index = qdict_get_int(qdict, "index");
907 if (mon_set_cpu(index) < 0) {
908 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
909 "a CPU number");
910 return -1;
912 return 0;
915 static void do_info_jit(Monitor *mon)
917 dump_exec_info((FILE *)mon, monitor_fprintf);
920 static void do_info_history(Monitor *mon)
922 int i;
923 const char *str;
925 if (!mon->rs)
926 return;
927 i = 0;
928 for(;;) {
929 str = readline_get_history(mon->rs, i);
930 if (!str)
931 break;
932 monitor_printf(mon, "%d: '%s'\n", i, str);
933 i++;
937 #if defined(TARGET_PPC)
938 /* XXX: not implemented in other targets */
939 static void do_info_cpu_stats(Monitor *mon)
941 CPUState *env;
943 env = mon_get_cpu();
944 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
946 #endif
948 #if defined(CONFIG_SIMPLE_TRACE)
949 static void do_info_trace(Monitor *mon)
951 st_print_trace((FILE *)mon, &monitor_fprintf);
954 static void do_info_trace_events(Monitor *mon)
956 st_print_trace_events((FILE *)mon, &monitor_fprintf);
958 #endif
961 * do_quit(): Quit QEMU execution
963 static int do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
965 monitor_suspend(mon);
966 no_shutdown = 0;
967 qemu_system_shutdown_request();
969 return 0;
972 static int change_vnc_password(const char *password)
974 if (vnc_display_password(NULL, password) < 0) {
975 qerror_report(QERR_SET_PASSWD_FAILED);
976 return -1;
979 return 0;
982 static void change_vnc_password_cb(Monitor *mon, const char *password,
983 void *opaque)
985 change_vnc_password(password);
986 monitor_read_command(mon, 1);
989 static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
991 if (strcmp(target, "passwd") == 0 ||
992 strcmp(target, "password") == 0) {
993 if (arg) {
994 char password[9];
995 strncpy(password, arg, sizeof(password));
996 password[sizeof(password) - 1] = '\0';
997 return change_vnc_password(password);
998 } else {
999 return monitor_read_password(mon, change_vnc_password_cb, NULL);
1001 } else {
1002 if (vnc_display_open(NULL, target) < 0) {
1003 qerror_report(QERR_VNC_SERVER_FAILED, target);
1004 return -1;
1008 return 0;
1012 * do_change(): Change a removable medium, or VNC configuration
1014 static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
1016 const char *device = qdict_get_str(qdict, "device");
1017 const char *target = qdict_get_str(qdict, "target");
1018 const char *arg = qdict_get_try_str(qdict, "arg");
1019 int ret;
1021 if (strcmp(device, "vnc") == 0) {
1022 ret = do_change_vnc(mon, target, arg);
1023 } else {
1024 ret = do_change_block(mon, device, target, arg);
1027 return ret;
1030 static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
1032 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
1033 return 0;
1036 static void do_logfile(Monitor *mon, const QDict *qdict)
1038 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
1041 static void do_log(Monitor *mon, const QDict *qdict)
1043 int mask;
1044 const char *items = qdict_get_str(qdict, "items");
1046 if (!strcmp(items, "none")) {
1047 mask = 0;
1048 } else {
1049 mask = cpu_str_to_log_mask(items);
1050 if (!mask) {
1051 help_cmd(mon, "log");
1052 return;
1055 cpu_set_log(mask);
1058 static void do_singlestep(Monitor *mon, const QDict *qdict)
1060 const char *option = qdict_get_try_str(qdict, "option");
1061 if (!option || !strcmp(option, "on")) {
1062 singlestep = 1;
1063 } else if (!strcmp(option, "off")) {
1064 singlestep = 0;
1065 } else {
1066 monitor_printf(mon, "unexpected option %s\n", option);
1071 * do_stop(): Stop VM execution
1073 static int do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
1075 vm_stop(EXCP_INTERRUPT);
1076 return 0;
1079 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
1081 struct bdrv_iterate_context {
1082 Monitor *mon;
1083 int err;
1087 * do_cont(): Resume emulation.
1089 static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
1091 struct bdrv_iterate_context context = { mon, 0 };
1093 if (incoming_expected) {
1094 qerror_report(QERR_MIGRATION_EXPECTED);
1095 return -1;
1097 bdrv_iterate(encrypted_bdrv_it, &context);
1098 /* only resume the vm if all keys are set and valid */
1099 if (!context.err) {
1100 vm_start();
1101 return 0;
1102 } else {
1103 return -1;
1107 static void bdrv_key_cb(void *opaque, int err)
1109 Monitor *mon = opaque;
1111 /* another key was set successfully, retry to continue */
1112 if (!err)
1113 do_cont(mon, NULL, NULL);
1116 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1118 struct bdrv_iterate_context *context = opaque;
1120 if (!context->err && bdrv_key_required(bs)) {
1121 context->err = -EBUSY;
1122 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1123 context->mon);
1127 static void do_gdbserver(Monitor *mon, const QDict *qdict)
1129 const char *device = qdict_get_try_str(qdict, "device");
1130 if (!device)
1131 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1132 if (gdbserver_start(device) < 0) {
1133 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1134 device);
1135 } else if (strcmp(device, "none") == 0) {
1136 monitor_printf(mon, "Disabled gdbserver\n");
1137 } else {
1138 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1139 device);
1143 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1145 const char *action = qdict_get_str(qdict, "action");
1146 if (select_watchdog_action(action) == -1) {
1147 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1151 static void monitor_printc(Monitor *mon, int c)
1153 monitor_printf(mon, "'");
1154 switch(c) {
1155 case '\'':
1156 monitor_printf(mon, "\\'");
1157 break;
1158 case '\\':
1159 monitor_printf(mon, "\\\\");
1160 break;
1161 case '\n':
1162 monitor_printf(mon, "\\n");
1163 break;
1164 case '\r':
1165 monitor_printf(mon, "\\r");
1166 break;
1167 default:
1168 if (c >= 32 && c <= 126) {
1169 monitor_printf(mon, "%c", c);
1170 } else {
1171 monitor_printf(mon, "\\x%02x", c);
1173 break;
1175 monitor_printf(mon, "'");
1178 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1179 target_phys_addr_t addr, int is_physical)
1181 CPUState *env;
1182 int l, line_size, i, max_digits, len;
1183 uint8_t buf[16];
1184 uint64_t v;
1186 if (format == 'i') {
1187 int flags;
1188 flags = 0;
1189 env = mon_get_cpu();
1190 #ifdef TARGET_I386
1191 if (wsize == 2) {
1192 flags = 1;
1193 } else if (wsize == 4) {
1194 flags = 0;
1195 } else {
1196 /* as default we use the current CS size */
1197 flags = 0;
1198 if (env) {
1199 #ifdef TARGET_X86_64
1200 if ((env->efer & MSR_EFER_LMA) &&
1201 (env->segs[R_CS].flags & DESC_L_MASK))
1202 flags = 2;
1203 else
1204 #endif
1205 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1206 flags = 1;
1209 #endif
1210 monitor_disas(mon, env, addr, count, is_physical, flags);
1211 return;
1214 len = wsize * count;
1215 if (wsize == 1)
1216 line_size = 8;
1217 else
1218 line_size = 16;
1219 max_digits = 0;
1221 switch(format) {
1222 case 'o':
1223 max_digits = (wsize * 8 + 2) / 3;
1224 break;
1225 default:
1226 case 'x':
1227 max_digits = (wsize * 8) / 4;
1228 break;
1229 case 'u':
1230 case 'd':
1231 max_digits = (wsize * 8 * 10 + 32) / 33;
1232 break;
1233 case 'c':
1234 wsize = 1;
1235 break;
1238 while (len > 0) {
1239 if (is_physical)
1240 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1241 else
1242 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1243 l = len;
1244 if (l > line_size)
1245 l = line_size;
1246 if (is_physical) {
1247 cpu_physical_memory_rw(addr, buf, l, 0);
1248 } else {
1249 env = mon_get_cpu();
1250 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
1251 monitor_printf(mon, " Cannot access memory\n");
1252 break;
1255 i = 0;
1256 while (i < l) {
1257 switch(wsize) {
1258 default:
1259 case 1:
1260 v = ldub_raw(buf + i);
1261 break;
1262 case 2:
1263 v = lduw_raw(buf + i);
1264 break;
1265 case 4:
1266 v = (uint32_t)ldl_raw(buf + i);
1267 break;
1268 case 8:
1269 v = ldq_raw(buf + i);
1270 break;
1272 monitor_printf(mon, " ");
1273 switch(format) {
1274 case 'o':
1275 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1276 break;
1277 case 'x':
1278 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1279 break;
1280 case 'u':
1281 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1282 break;
1283 case 'd':
1284 monitor_printf(mon, "%*" PRId64, max_digits, v);
1285 break;
1286 case 'c':
1287 monitor_printc(mon, v);
1288 break;
1290 i += wsize;
1292 monitor_printf(mon, "\n");
1293 addr += l;
1294 len -= l;
1298 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1300 int count = qdict_get_int(qdict, "count");
1301 int format = qdict_get_int(qdict, "format");
1302 int size = qdict_get_int(qdict, "size");
1303 target_long addr = qdict_get_int(qdict, "addr");
1305 memory_dump(mon, count, format, size, addr, 0);
1308 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1310 int count = qdict_get_int(qdict, "count");
1311 int format = qdict_get_int(qdict, "format");
1312 int size = qdict_get_int(qdict, "size");
1313 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1315 memory_dump(mon, count, format, size, addr, 1);
1318 static void do_print(Monitor *mon, const QDict *qdict)
1320 int format = qdict_get_int(qdict, "format");
1321 target_phys_addr_t val = qdict_get_int(qdict, "val");
1323 #if TARGET_PHYS_ADDR_BITS == 32
1324 switch(format) {
1325 case 'o':
1326 monitor_printf(mon, "%#o", val);
1327 break;
1328 case 'x':
1329 monitor_printf(mon, "%#x", val);
1330 break;
1331 case 'u':
1332 monitor_printf(mon, "%u", val);
1333 break;
1334 default:
1335 case 'd':
1336 monitor_printf(mon, "%d", val);
1337 break;
1338 case 'c':
1339 monitor_printc(mon, val);
1340 break;
1342 #else
1343 switch(format) {
1344 case 'o':
1345 monitor_printf(mon, "%#" PRIo64, val);
1346 break;
1347 case 'x':
1348 monitor_printf(mon, "%#" PRIx64, val);
1349 break;
1350 case 'u':
1351 monitor_printf(mon, "%" PRIu64, val);
1352 break;
1353 default:
1354 case 'd':
1355 monitor_printf(mon, "%" PRId64, val);
1356 break;
1357 case 'c':
1358 monitor_printc(mon, val);
1359 break;
1361 #endif
1362 monitor_printf(mon, "\n");
1365 static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1367 FILE *f;
1368 uint32_t size = qdict_get_int(qdict, "size");
1369 const char *filename = qdict_get_str(qdict, "filename");
1370 target_long addr = qdict_get_int(qdict, "val");
1371 uint32_t l;
1372 CPUState *env;
1373 uint8_t buf[1024];
1374 int ret = -1;
1376 env = mon_get_cpu();
1378 f = fopen(filename, "wb");
1379 if (!f) {
1380 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1381 return -1;
1383 while (size != 0) {
1384 l = sizeof(buf);
1385 if (l > size)
1386 l = size;
1387 cpu_memory_rw_debug(env, addr, buf, l, 0);
1388 if (fwrite(buf, 1, l, f) != l) {
1389 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1390 goto exit;
1392 addr += l;
1393 size -= l;
1396 ret = 0;
1398 exit:
1399 fclose(f);
1400 return ret;
1403 static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
1404 QObject **ret_data)
1406 FILE *f;
1407 uint32_t l;
1408 uint8_t buf[1024];
1409 uint32_t size = qdict_get_int(qdict, "size");
1410 const char *filename = qdict_get_str(qdict, "filename");
1411 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1412 int ret = -1;
1414 f = fopen(filename, "wb");
1415 if (!f) {
1416 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1417 return -1;
1419 while (size != 0) {
1420 l = sizeof(buf);
1421 if (l > size)
1422 l = size;
1423 cpu_physical_memory_rw(addr, buf, l, 0);
1424 if (fwrite(buf, 1, l, f) != l) {
1425 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1426 goto exit;
1428 fflush(f);
1429 addr += l;
1430 size -= l;
1433 ret = 0;
1435 exit:
1436 fclose(f);
1437 return ret;
1440 static void do_sum(Monitor *mon, const QDict *qdict)
1442 uint32_t addr;
1443 uint8_t buf[1];
1444 uint16_t sum;
1445 uint32_t start = qdict_get_int(qdict, "start");
1446 uint32_t size = qdict_get_int(qdict, "size");
1448 sum = 0;
1449 for(addr = start; addr < (start + size); addr++) {
1450 cpu_physical_memory_rw(addr, buf, 1, 0);
1451 /* BSD sum algorithm ('sum' Unix command) */
1452 sum = (sum >> 1) | (sum << 15);
1453 sum += buf[0];
1455 monitor_printf(mon, "%05d\n", sum);
1458 typedef struct {
1459 int keycode;
1460 const char *name;
1461 } KeyDef;
1463 static const KeyDef key_defs[] = {
1464 { 0x2a, "shift" },
1465 { 0x36, "shift_r" },
1467 { 0x38, "alt" },
1468 { 0xb8, "alt_r" },
1469 { 0x64, "altgr" },
1470 { 0xe4, "altgr_r" },
1471 { 0x1d, "ctrl" },
1472 { 0x9d, "ctrl_r" },
1474 { 0xdd, "menu" },
1476 { 0x01, "esc" },
1478 { 0x02, "1" },
1479 { 0x03, "2" },
1480 { 0x04, "3" },
1481 { 0x05, "4" },
1482 { 0x06, "5" },
1483 { 0x07, "6" },
1484 { 0x08, "7" },
1485 { 0x09, "8" },
1486 { 0x0a, "9" },
1487 { 0x0b, "0" },
1488 { 0x0c, "minus" },
1489 { 0x0d, "equal" },
1490 { 0x0e, "backspace" },
1492 { 0x0f, "tab" },
1493 { 0x10, "q" },
1494 { 0x11, "w" },
1495 { 0x12, "e" },
1496 { 0x13, "r" },
1497 { 0x14, "t" },
1498 { 0x15, "y" },
1499 { 0x16, "u" },
1500 { 0x17, "i" },
1501 { 0x18, "o" },
1502 { 0x19, "p" },
1503 { 0x1a, "bracket_left" },
1504 { 0x1b, "bracket_right" },
1505 { 0x1c, "ret" },
1507 { 0x1e, "a" },
1508 { 0x1f, "s" },
1509 { 0x20, "d" },
1510 { 0x21, "f" },
1511 { 0x22, "g" },
1512 { 0x23, "h" },
1513 { 0x24, "j" },
1514 { 0x25, "k" },
1515 { 0x26, "l" },
1516 { 0x27, "semicolon" },
1517 { 0x28, "apostrophe" },
1518 { 0x29, "grave_accent" },
1520 { 0x2b, "backslash" },
1521 { 0x2c, "z" },
1522 { 0x2d, "x" },
1523 { 0x2e, "c" },
1524 { 0x2f, "v" },
1525 { 0x30, "b" },
1526 { 0x31, "n" },
1527 { 0x32, "m" },
1528 { 0x33, "comma" },
1529 { 0x34, "dot" },
1530 { 0x35, "slash" },
1532 { 0x37, "asterisk" },
1534 { 0x39, "spc" },
1535 { 0x3a, "caps_lock" },
1536 { 0x3b, "f1" },
1537 { 0x3c, "f2" },
1538 { 0x3d, "f3" },
1539 { 0x3e, "f4" },
1540 { 0x3f, "f5" },
1541 { 0x40, "f6" },
1542 { 0x41, "f7" },
1543 { 0x42, "f8" },
1544 { 0x43, "f9" },
1545 { 0x44, "f10" },
1546 { 0x45, "num_lock" },
1547 { 0x46, "scroll_lock" },
1549 { 0xb5, "kp_divide" },
1550 { 0x37, "kp_multiply" },
1551 { 0x4a, "kp_subtract" },
1552 { 0x4e, "kp_add" },
1553 { 0x9c, "kp_enter" },
1554 { 0x53, "kp_decimal" },
1555 { 0x54, "sysrq" },
1557 { 0x52, "kp_0" },
1558 { 0x4f, "kp_1" },
1559 { 0x50, "kp_2" },
1560 { 0x51, "kp_3" },
1561 { 0x4b, "kp_4" },
1562 { 0x4c, "kp_5" },
1563 { 0x4d, "kp_6" },
1564 { 0x47, "kp_7" },
1565 { 0x48, "kp_8" },
1566 { 0x49, "kp_9" },
1568 { 0x56, "<" },
1570 { 0x57, "f11" },
1571 { 0x58, "f12" },
1573 { 0xb7, "print" },
1575 { 0xc7, "home" },
1576 { 0xc9, "pgup" },
1577 { 0xd1, "pgdn" },
1578 { 0xcf, "end" },
1580 { 0xcb, "left" },
1581 { 0xc8, "up" },
1582 { 0xd0, "down" },
1583 { 0xcd, "right" },
1585 { 0xd2, "insert" },
1586 { 0xd3, "delete" },
1587 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1588 { 0xf0, "stop" },
1589 { 0xf1, "again" },
1590 { 0xf2, "props" },
1591 { 0xf3, "undo" },
1592 { 0xf4, "front" },
1593 { 0xf5, "copy" },
1594 { 0xf6, "open" },
1595 { 0xf7, "paste" },
1596 { 0xf8, "find" },
1597 { 0xf9, "cut" },
1598 { 0xfa, "lf" },
1599 { 0xfb, "help" },
1600 { 0xfc, "meta_l" },
1601 { 0xfd, "meta_r" },
1602 { 0xfe, "compose" },
1603 #endif
1604 { 0, NULL },
1607 static int get_keycode(const char *key)
1609 const KeyDef *p;
1610 char *endp;
1611 int ret;
1613 for(p = key_defs; p->name != NULL; p++) {
1614 if (!strcmp(key, p->name))
1615 return p->keycode;
1617 if (strstart(key, "0x", NULL)) {
1618 ret = strtoul(key, &endp, 0);
1619 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1620 return ret;
1622 return -1;
1625 #define MAX_KEYCODES 16
1626 static uint8_t keycodes[MAX_KEYCODES];
1627 static int nb_pending_keycodes;
1628 static QEMUTimer *key_timer;
1630 static void release_keys(void *opaque)
1632 int keycode;
1634 while (nb_pending_keycodes > 0) {
1635 nb_pending_keycodes--;
1636 keycode = keycodes[nb_pending_keycodes];
1637 if (keycode & 0x80)
1638 kbd_put_keycode(0xe0);
1639 kbd_put_keycode(keycode | 0x80);
1643 static void do_sendkey(Monitor *mon, const QDict *qdict)
1645 char keyname_buf[16];
1646 char *separator;
1647 int keyname_len, keycode, i;
1648 const char *string = qdict_get_str(qdict, "string");
1649 int has_hold_time = qdict_haskey(qdict, "hold_time");
1650 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1652 if (nb_pending_keycodes > 0) {
1653 qemu_del_timer(key_timer);
1654 release_keys(NULL);
1656 if (!has_hold_time)
1657 hold_time = 100;
1658 i = 0;
1659 while (1) {
1660 separator = strchr(string, '-');
1661 keyname_len = separator ? separator - string : strlen(string);
1662 if (keyname_len > 0) {
1663 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1664 if (keyname_len > sizeof(keyname_buf) - 1) {
1665 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1666 return;
1668 if (i == MAX_KEYCODES) {
1669 monitor_printf(mon, "too many keys\n");
1670 return;
1672 keyname_buf[keyname_len] = 0;
1673 keycode = get_keycode(keyname_buf);
1674 if (keycode < 0) {
1675 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1676 return;
1678 keycodes[i++] = keycode;
1680 if (!separator)
1681 break;
1682 string = separator + 1;
1684 nb_pending_keycodes = i;
1685 /* key down events */
1686 for (i = 0; i < nb_pending_keycodes; i++) {
1687 keycode = keycodes[i];
1688 if (keycode & 0x80)
1689 kbd_put_keycode(0xe0);
1690 kbd_put_keycode(keycode & 0x7f);
1692 /* delayed key up events */
1693 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1694 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1697 static int mouse_button_state;
1699 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1701 int dx, dy, dz;
1702 const char *dx_str = qdict_get_str(qdict, "dx_str");
1703 const char *dy_str = qdict_get_str(qdict, "dy_str");
1704 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1705 dx = strtol(dx_str, NULL, 0);
1706 dy = strtol(dy_str, NULL, 0);
1707 dz = 0;
1708 if (dz_str)
1709 dz = strtol(dz_str, NULL, 0);
1710 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1713 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1715 int button_state = qdict_get_int(qdict, "button_state");
1716 mouse_button_state = button_state;
1717 kbd_mouse_event(0, 0, 0, mouse_button_state);
1720 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1722 int size = qdict_get_int(qdict, "size");
1723 int addr = qdict_get_int(qdict, "addr");
1724 int has_index = qdict_haskey(qdict, "index");
1725 uint32_t val;
1726 int suffix;
1728 if (has_index) {
1729 int index = qdict_get_int(qdict, "index");
1730 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1731 addr++;
1733 addr &= 0xffff;
1735 switch(size) {
1736 default:
1737 case 1:
1738 val = cpu_inb(addr);
1739 suffix = 'b';
1740 break;
1741 case 2:
1742 val = cpu_inw(addr);
1743 suffix = 'w';
1744 break;
1745 case 4:
1746 val = cpu_inl(addr);
1747 suffix = 'l';
1748 break;
1750 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1751 suffix, addr, size * 2, val);
1754 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1756 int size = qdict_get_int(qdict, "size");
1757 int addr = qdict_get_int(qdict, "addr");
1758 int val = qdict_get_int(qdict, "val");
1760 addr &= IOPORTS_MASK;
1762 switch (size) {
1763 default:
1764 case 1:
1765 cpu_outb(addr, val);
1766 break;
1767 case 2:
1768 cpu_outw(addr, val);
1769 break;
1770 case 4:
1771 cpu_outl(addr, val);
1772 break;
1776 static void do_boot_set(Monitor *mon, const QDict *qdict)
1778 int res;
1779 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1781 res = qemu_boot_set(bootdevice);
1782 if (res == 0) {
1783 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1784 } else if (res > 0) {
1785 monitor_printf(mon, "setting boot device list failed\n");
1786 } else {
1787 monitor_printf(mon, "no function defined to set boot device list for "
1788 "this architecture\n");
1793 * do_system_reset(): Issue a machine reset
1795 static int do_system_reset(Monitor *mon, const QDict *qdict,
1796 QObject **ret_data)
1798 qemu_system_reset_request();
1799 return 0;
1803 * do_system_powerdown(): Issue a machine powerdown
1805 static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1806 QObject **ret_data)
1808 qemu_system_powerdown_request();
1809 return 0;
1812 #if defined(TARGET_I386)
1813 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1815 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1816 addr,
1817 pte & mask,
1818 pte & PG_GLOBAL_MASK ? 'G' : '-',
1819 pte & PG_PSE_MASK ? 'P' : '-',
1820 pte & PG_DIRTY_MASK ? 'D' : '-',
1821 pte & PG_ACCESSED_MASK ? 'A' : '-',
1822 pte & PG_PCD_MASK ? 'C' : '-',
1823 pte & PG_PWT_MASK ? 'T' : '-',
1824 pte & PG_USER_MASK ? 'U' : '-',
1825 pte & PG_RW_MASK ? 'W' : '-');
1828 static void tlb_info(Monitor *mon)
1830 CPUState *env;
1831 int l1, l2;
1832 uint32_t pgd, pde, pte;
1834 env = mon_get_cpu();
1836 if (!(env->cr[0] & CR0_PG_MASK)) {
1837 monitor_printf(mon, "PG disabled\n");
1838 return;
1840 pgd = env->cr[3] & ~0xfff;
1841 for(l1 = 0; l1 < 1024; l1++) {
1842 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1843 pde = le32_to_cpu(pde);
1844 if (pde & PG_PRESENT_MASK) {
1845 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1846 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1847 } else {
1848 for(l2 = 0; l2 < 1024; l2++) {
1849 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1850 (uint8_t *)&pte, 4);
1851 pte = le32_to_cpu(pte);
1852 if (pte & PG_PRESENT_MASK) {
1853 print_pte(mon, (l1 << 22) + (l2 << 12),
1854 pte & ~PG_PSE_MASK,
1855 ~0xfff);
1863 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1864 uint32_t end, int prot)
1866 int prot1;
1867 prot1 = *plast_prot;
1868 if (prot != prot1) {
1869 if (*pstart != -1) {
1870 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1871 *pstart, end, end - *pstart,
1872 prot1 & PG_USER_MASK ? 'u' : '-',
1873 'r',
1874 prot1 & PG_RW_MASK ? 'w' : '-');
1876 if (prot != 0)
1877 *pstart = end;
1878 else
1879 *pstart = -1;
1880 *plast_prot = prot;
1884 static void mem_info(Monitor *mon)
1886 CPUState *env;
1887 int l1, l2, prot, last_prot;
1888 uint32_t pgd, pde, pte, start, end;
1890 env = mon_get_cpu();
1892 if (!(env->cr[0] & CR0_PG_MASK)) {
1893 monitor_printf(mon, "PG disabled\n");
1894 return;
1896 pgd = env->cr[3] & ~0xfff;
1897 last_prot = 0;
1898 start = -1;
1899 for(l1 = 0; l1 < 1024; l1++) {
1900 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1901 pde = le32_to_cpu(pde);
1902 end = l1 << 22;
1903 if (pde & PG_PRESENT_MASK) {
1904 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1905 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1906 mem_print(mon, &start, &last_prot, end, prot);
1907 } else {
1908 for(l2 = 0; l2 < 1024; l2++) {
1909 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1910 (uint8_t *)&pte, 4);
1911 pte = le32_to_cpu(pte);
1912 end = (l1 << 22) + (l2 << 12);
1913 if (pte & PG_PRESENT_MASK) {
1914 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1915 } else {
1916 prot = 0;
1918 mem_print(mon, &start, &last_prot, end, prot);
1921 } else {
1922 prot = 0;
1923 mem_print(mon, &start, &last_prot, end, prot);
1927 #endif
1929 #if defined(TARGET_SH4)
1931 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1933 monitor_printf(mon, " tlb%i:\t"
1934 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1935 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1936 "dirty=%hhu writethrough=%hhu\n",
1937 idx,
1938 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1939 tlb->v, tlb->sh, tlb->c, tlb->pr,
1940 tlb->d, tlb->wt);
1943 static void tlb_info(Monitor *mon)
1945 CPUState *env = mon_get_cpu();
1946 int i;
1948 monitor_printf (mon, "ITLB:\n");
1949 for (i = 0 ; i < ITLB_SIZE ; i++)
1950 print_tlb (mon, i, &env->itlb[i]);
1951 monitor_printf (mon, "UTLB:\n");
1952 for (i = 0 ; i < UTLB_SIZE ; i++)
1953 print_tlb (mon, i, &env->utlb[i]);
1956 #endif
1958 static void do_info_kvm_print(Monitor *mon, const QObject *data)
1960 QDict *qdict;
1962 qdict = qobject_to_qdict(data);
1964 monitor_printf(mon, "kvm support: ");
1965 if (qdict_get_bool(qdict, "present")) {
1966 monitor_printf(mon, "%s\n", qdict_get_bool(qdict, "enabled") ?
1967 "enabled" : "disabled");
1968 } else {
1969 monitor_printf(mon, "not compiled\n");
1973 static void do_info_kvm(Monitor *mon, QObject **ret_data)
1975 #ifdef CONFIG_KVM
1976 *ret_data = qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1977 kvm_enabled());
1978 #else
1979 *ret_data = qobject_from_jsonf("{ 'enabled': false, 'present': false }");
1980 #endif
1983 static void do_info_numa(Monitor *mon)
1985 int i;
1986 CPUState *env;
1988 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1989 for (i = 0; i < nb_numa_nodes; i++) {
1990 monitor_printf(mon, "node %d cpus:", i);
1991 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1992 if (env->numa_node == i) {
1993 monitor_printf(mon, " %d", env->cpu_index);
1996 monitor_printf(mon, "\n");
1997 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1998 node_mem[i] >> 20);
2002 #ifdef CONFIG_PROFILER
2004 int64_t qemu_time;
2005 int64_t dev_time;
2007 static void do_info_profile(Monitor *mon)
2009 int64_t total;
2010 total = qemu_time;
2011 if (total == 0)
2012 total = 1;
2013 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
2014 dev_time, dev_time / (double)get_ticks_per_sec());
2015 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
2016 qemu_time, qemu_time / (double)get_ticks_per_sec());
2017 qemu_time = 0;
2018 dev_time = 0;
2020 #else
2021 static void do_info_profile(Monitor *mon)
2023 monitor_printf(mon, "Internal profiler not compiled\n");
2025 #endif
2027 /* Capture support */
2028 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
2030 static void do_info_capture(Monitor *mon)
2032 int i;
2033 CaptureState *s;
2035 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2036 monitor_printf(mon, "[%d]: ", i);
2037 s->ops.info (s->opaque);
2041 #ifdef HAS_AUDIO
2042 static void do_stop_capture(Monitor *mon, const QDict *qdict)
2044 int i;
2045 int n = qdict_get_int(qdict, "n");
2046 CaptureState *s;
2048 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2049 if (i == n) {
2050 s->ops.destroy (s->opaque);
2051 QLIST_REMOVE (s, entries);
2052 qemu_free (s);
2053 return;
2058 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2060 const char *path = qdict_get_str(qdict, "path");
2061 int has_freq = qdict_haskey(qdict, "freq");
2062 int freq = qdict_get_try_int(qdict, "freq", -1);
2063 int has_bits = qdict_haskey(qdict, "bits");
2064 int bits = qdict_get_try_int(qdict, "bits", -1);
2065 int has_channels = qdict_haskey(qdict, "nchannels");
2066 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2067 CaptureState *s;
2069 s = qemu_mallocz (sizeof (*s));
2071 freq = has_freq ? freq : 44100;
2072 bits = has_bits ? bits : 16;
2073 nchannels = has_channels ? nchannels : 2;
2075 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2076 monitor_printf(mon, "Faied to add wave capture\n");
2077 qemu_free (s);
2079 QLIST_INSERT_HEAD (&capture_head, s, entries);
2081 #endif
2083 #if defined(TARGET_I386)
2084 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
2086 CPUState *env;
2087 int cpu_index = qdict_get_int(qdict, "cpu_index");
2089 for (env = first_cpu; env != NULL; env = env->next_cpu)
2090 if (env->cpu_index == cpu_index) {
2091 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2092 break;
2095 #endif
2097 static void do_info_status_print(Monitor *mon, const QObject *data)
2099 QDict *qdict;
2101 qdict = qobject_to_qdict(data);
2103 monitor_printf(mon, "VM status: ");
2104 if (qdict_get_bool(qdict, "running")) {
2105 monitor_printf(mon, "running");
2106 if (qdict_get_bool(qdict, "singlestep")) {
2107 monitor_printf(mon, " (single step mode)");
2109 } else {
2110 monitor_printf(mon, "paused");
2113 monitor_printf(mon, "\n");
2116 static void do_info_status(Monitor *mon, QObject **ret_data)
2118 *ret_data = qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2119 vm_running, singlestep);
2122 static qemu_acl *find_acl(Monitor *mon, const char *name)
2124 qemu_acl *acl = qemu_acl_find(name);
2126 if (!acl) {
2127 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2129 return acl;
2132 static void do_acl_show(Monitor *mon, const QDict *qdict)
2134 const char *aclname = qdict_get_str(qdict, "aclname");
2135 qemu_acl *acl = find_acl(mon, aclname);
2136 qemu_acl_entry *entry;
2137 int i = 0;
2139 if (acl) {
2140 monitor_printf(mon, "policy: %s\n",
2141 acl->defaultDeny ? "deny" : "allow");
2142 QTAILQ_FOREACH(entry, &acl->entries, next) {
2143 i++;
2144 monitor_printf(mon, "%d: %s %s\n", i,
2145 entry->deny ? "deny" : "allow", entry->match);
2150 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2152 const char *aclname = qdict_get_str(qdict, "aclname");
2153 qemu_acl *acl = find_acl(mon, aclname);
2155 if (acl) {
2156 qemu_acl_reset(acl);
2157 monitor_printf(mon, "acl: removed all rules\n");
2161 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2163 const char *aclname = qdict_get_str(qdict, "aclname");
2164 const char *policy = qdict_get_str(qdict, "policy");
2165 qemu_acl *acl = find_acl(mon, aclname);
2167 if (acl) {
2168 if (strcmp(policy, "allow") == 0) {
2169 acl->defaultDeny = 0;
2170 monitor_printf(mon, "acl: policy set to 'allow'\n");
2171 } else if (strcmp(policy, "deny") == 0) {
2172 acl->defaultDeny = 1;
2173 monitor_printf(mon, "acl: policy set to 'deny'\n");
2174 } else {
2175 monitor_printf(mon, "acl: unknown policy '%s', "
2176 "expected 'deny' or 'allow'\n", policy);
2181 static void do_acl_add(Monitor *mon, const QDict *qdict)
2183 const char *aclname = qdict_get_str(qdict, "aclname");
2184 const char *match = qdict_get_str(qdict, "match");
2185 const char *policy = qdict_get_str(qdict, "policy");
2186 int has_index = qdict_haskey(qdict, "index");
2187 int index = qdict_get_try_int(qdict, "index", -1);
2188 qemu_acl *acl = find_acl(mon, aclname);
2189 int deny, ret;
2191 if (acl) {
2192 if (strcmp(policy, "allow") == 0) {
2193 deny = 0;
2194 } else if (strcmp(policy, "deny") == 0) {
2195 deny = 1;
2196 } else {
2197 monitor_printf(mon, "acl: unknown policy '%s', "
2198 "expected 'deny' or 'allow'\n", policy);
2199 return;
2201 if (has_index)
2202 ret = qemu_acl_insert(acl, deny, match, index);
2203 else
2204 ret = qemu_acl_append(acl, deny, match);
2205 if (ret < 0)
2206 monitor_printf(mon, "acl: unable to add acl entry\n");
2207 else
2208 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2212 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2214 const char *aclname = qdict_get_str(qdict, "aclname");
2215 const char *match = qdict_get_str(qdict, "match");
2216 qemu_acl *acl = find_acl(mon, aclname);
2217 int ret;
2219 if (acl) {
2220 ret = qemu_acl_remove(acl, match);
2221 if (ret < 0)
2222 monitor_printf(mon, "acl: no matching acl entry\n");
2223 else
2224 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2228 #if defined(TARGET_I386)
2229 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2231 CPUState *cenv;
2232 int cpu_index = qdict_get_int(qdict, "cpu_index");
2233 int bank = qdict_get_int(qdict, "bank");
2234 uint64_t status = qdict_get_int(qdict, "status");
2235 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2236 uint64_t addr = qdict_get_int(qdict, "addr");
2237 uint64_t misc = qdict_get_int(qdict, "misc");
2239 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
2240 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
2241 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
2242 break;
2245 #endif
2247 static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2249 const char *fdname = qdict_get_str(qdict, "fdname");
2250 mon_fd_t *monfd;
2251 int fd;
2253 fd = qemu_chr_get_msgfd(mon->chr);
2254 if (fd == -1) {
2255 qerror_report(QERR_FD_NOT_SUPPLIED);
2256 return -1;
2259 if (qemu_isdigit(fdname[0])) {
2260 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2261 "a name not starting with a digit");
2262 return -1;
2265 QLIST_FOREACH(monfd, &mon->fds, next) {
2266 if (strcmp(monfd->name, fdname) != 0) {
2267 continue;
2270 close(monfd->fd);
2271 monfd->fd = fd;
2272 return 0;
2275 monfd = qemu_mallocz(sizeof(mon_fd_t));
2276 monfd->name = qemu_strdup(fdname);
2277 monfd->fd = fd;
2279 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
2280 return 0;
2283 static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2285 const char *fdname = qdict_get_str(qdict, "fdname");
2286 mon_fd_t *monfd;
2288 QLIST_FOREACH(monfd, &mon->fds, next) {
2289 if (strcmp(monfd->name, fdname) != 0) {
2290 continue;
2293 QLIST_REMOVE(monfd, next);
2294 close(monfd->fd);
2295 qemu_free(monfd->name);
2296 qemu_free(monfd);
2297 return 0;
2300 qerror_report(QERR_FD_NOT_FOUND, fdname);
2301 return -1;
2304 static void do_loadvm(Monitor *mon, const QDict *qdict)
2306 int saved_vm_running = vm_running;
2307 const char *name = qdict_get_str(qdict, "name");
2309 vm_stop(0);
2311 if (load_vmstate(name) == 0 && saved_vm_running) {
2312 vm_start();
2316 int monitor_get_fd(Monitor *mon, const char *fdname)
2318 mon_fd_t *monfd;
2320 QLIST_FOREACH(monfd, &mon->fds, next) {
2321 int fd;
2323 if (strcmp(monfd->name, fdname) != 0) {
2324 continue;
2327 fd = monfd->fd;
2329 /* caller takes ownership of fd */
2330 QLIST_REMOVE(monfd, next);
2331 qemu_free(monfd->name);
2332 qemu_free(monfd);
2334 return fd;
2337 return -1;
2340 static const mon_cmd_t mon_cmds[] = {
2341 #include "hmp-commands.h"
2342 { NULL, NULL, },
2345 /* Please update hmp-commands.hx when adding or changing commands */
2346 static const mon_cmd_t info_cmds[] = {
2348 .name = "version",
2349 .args_type = "",
2350 .params = "",
2351 .help = "show the version of QEMU",
2352 .user_print = do_info_version_print,
2353 .mhandler.info_new = do_info_version,
2356 .name = "network",
2357 .args_type = "",
2358 .params = "",
2359 .help = "show the network state",
2360 .mhandler.info = do_info_network,
2363 .name = "chardev",
2364 .args_type = "",
2365 .params = "",
2366 .help = "show the character devices",
2367 .user_print = qemu_chr_info_print,
2368 .mhandler.info_new = qemu_chr_info,
2371 .name = "block",
2372 .args_type = "",
2373 .params = "",
2374 .help = "show the block devices",
2375 .user_print = bdrv_info_print,
2376 .mhandler.info_new = bdrv_info,
2379 .name = "blockstats",
2380 .args_type = "",
2381 .params = "",
2382 .help = "show block device statistics",
2383 .user_print = bdrv_stats_print,
2384 .mhandler.info_new = bdrv_info_stats,
2387 .name = "registers",
2388 .args_type = "",
2389 .params = "",
2390 .help = "show the cpu registers",
2391 .mhandler.info = do_info_registers,
2394 .name = "cpus",
2395 .args_type = "",
2396 .params = "",
2397 .help = "show infos for each CPU",
2398 .user_print = monitor_print_cpus,
2399 .mhandler.info_new = do_info_cpus,
2402 .name = "history",
2403 .args_type = "",
2404 .params = "",
2405 .help = "show the command line history",
2406 .mhandler.info = do_info_history,
2409 .name = "irq",
2410 .args_type = "",
2411 .params = "",
2412 .help = "show the interrupts statistics (if available)",
2413 .mhandler.info = irq_info,
2416 .name = "pic",
2417 .args_type = "",
2418 .params = "",
2419 .help = "show i8259 (PIC) state",
2420 .mhandler.info = pic_info,
2423 .name = "pci",
2424 .args_type = "",
2425 .params = "",
2426 .help = "show PCI info",
2427 .user_print = do_pci_info_print,
2428 .mhandler.info_new = do_pci_info,
2430 #if defined(TARGET_I386) || defined(TARGET_SH4)
2432 .name = "tlb",
2433 .args_type = "",
2434 .params = "",
2435 .help = "show virtual to physical memory mappings",
2436 .mhandler.info = tlb_info,
2438 #endif
2439 #if defined(TARGET_I386)
2441 .name = "mem",
2442 .args_type = "",
2443 .params = "",
2444 .help = "show the active virtual memory mappings",
2445 .mhandler.info = mem_info,
2447 #endif
2449 .name = "jit",
2450 .args_type = "",
2451 .params = "",
2452 .help = "show dynamic compiler info",
2453 .mhandler.info = do_info_jit,
2456 .name = "kvm",
2457 .args_type = "",
2458 .params = "",
2459 .help = "show KVM information",
2460 .user_print = do_info_kvm_print,
2461 .mhandler.info_new = do_info_kvm,
2464 .name = "numa",
2465 .args_type = "",
2466 .params = "",
2467 .help = "show NUMA information",
2468 .mhandler.info = do_info_numa,
2471 .name = "usb",
2472 .args_type = "",
2473 .params = "",
2474 .help = "show guest USB devices",
2475 .mhandler.info = usb_info,
2478 .name = "usbhost",
2479 .args_type = "",
2480 .params = "",
2481 .help = "show host USB devices",
2482 .mhandler.info = usb_host_info,
2485 .name = "profile",
2486 .args_type = "",
2487 .params = "",
2488 .help = "show profiling information",
2489 .mhandler.info = do_info_profile,
2492 .name = "capture",
2493 .args_type = "",
2494 .params = "",
2495 .help = "show capture information",
2496 .mhandler.info = do_info_capture,
2499 .name = "snapshots",
2500 .args_type = "",
2501 .params = "",
2502 .help = "show the currently saved VM snapshots",
2503 .mhandler.info = do_info_snapshots,
2506 .name = "status",
2507 .args_type = "",
2508 .params = "",
2509 .help = "show the current VM status (running|paused)",
2510 .user_print = do_info_status_print,
2511 .mhandler.info_new = do_info_status,
2514 .name = "pcmcia",
2515 .args_type = "",
2516 .params = "",
2517 .help = "show guest PCMCIA status",
2518 .mhandler.info = pcmcia_info,
2521 .name = "mice",
2522 .args_type = "",
2523 .params = "",
2524 .help = "show which guest mouse is receiving events",
2525 .user_print = do_info_mice_print,
2526 .mhandler.info_new = do_info_mice,
2529 .name = "vnc",
2530 .args_type = "",
2531 .params = "",
2532 .help = "show the vnc server status",
2533 .user_print = do_info_vnc_print,
2534 .mhandler.info_new = do_info_vnc,
2537 .name = "name",
2538 .args_type = "",
2539 .params = "",
2540 .help = "show the current VM name",
2541 .user_print = do_info_name_print,
2542 .mhandler.info_new = do_info_name,
2545 .name = "uuid",
2546 .args_type = "",
2547 .params = "",
2548 .help = "show the current VM UUID",
2549 .user_print = do_info_uuid_print,
2550 .mhandler.info_new = do_info_uuid,
2552 #if defined(TARGET_PPC)
2554 .name = "cpustats",
2555 .args_type = "",
2556 .params = "",
2557 .help = "show CPU statistics",
2558 .mhandler.info = do_info_cpu_stats,
2560 #endif
2561 #if defined(CONFIG_SLIRP)
2563 .name = "usernet",
2564 .args_type = "",
2565 .params = "",
2566 .help = "show user network stack connection states",
2567 .mhandler.info = do_info_usernet,
2569 #endif
2571 .name = "migrate",
2572 .args_type = "",
2573 .params = "",
2574 .help = "show migration status",
2575 .user_print = do_info_migrate_print,
2576 .mhandler.info_new = do_info_migrate,
2579 .name = "balloon",
2580 .args_type = "",
2581 .params = "",
2582 .help = "show balloon information",
2583 .user_print = monitor_print_balloon,
2584 .mhandler.info_async = do_info_balloon,
2585 .flags = MONITOR_CMD_ASYNC,
2588 .name = "qtree",
2589 .args_type = "",
2590 .params = "",
2591 .help = "show device tree",
2592 .mhandler.info = do_info_qtree,
2595 .name = "qdm",
2596 .args_type = "",
2597 .params = "",
2598 .help = "show qdev device model list",
2599 .mhandler.info = do_info_qdm,
2602 .name = "roms",
2603 .args_type = "",
2604 .params = "",
2605 .help = "show roms",
2606 .mhandler.info = do_info_roms,
2608 #if defined(CONFIG_SIMPLE_TRACE)
2610 .name = "trace",
2611 .args_type = "",
2612 .params = "",
2613 .help = "show current contents of trace buffer",
2614 .mhandler.info = do_info_trace,
2617 .name = "trace-events",
2618 .args_type = "",
2619 .params = "",
2620 .help = "show available trace-events & their state",
2621 .mhandler.info = do_info_trace_events,
2623 #endif
2625 .name = NULL,
2629 static const mon_cmd_t qmp_cmds[] = {
2630 #include "qmp-commands.h"
2631 { /* NULL */ },
2634 static const mon_cmd_t qmp_query_cmds[] = {
2636 .name = "version",
2637 .args_type = "",
2638 .params = "",
2639 .help = "show the version of QEMU",
2640 .user_print = do_info_version_print,
2641 .mhandler.info_new = do_info_version,
2644 .name = "commands",
2645 .args_type = "",
2646 .params = "",
2647 .help = "list QMP available commands",
2648 .user_print = monitor_user_noop,
2649 .mhandler.info_new = do_info_commands,
2652 .name = "chardev",
2653 .args_type = "",
2654 .params = "",
2655 .help = "show the character devices",
2656 .user_print = qemu_chr_info_print,
2657 .mhandler.info_new = qemu_chr_info,
2660 .name = "block",
2661 .args_type = "",
2662 .params = "",
2663 .help = "show the block devices",
2664 .user_print = bdrv_info_print,
2665 .mhandler.info_new = bdrv_info,
2668 .name = "blockstats",
2669 .args_type = "",
2670 .params = "",
2671 .help = "show block device statistics",
2672 .user_print = bdrv_stats_print,
2673 .mhandler.info_new = bdrv_info_stats,
2676 .name = "cpus",
2677 .args_type = "",
2678 .params = "",
2679 .help = "show infos for each CPU",
2680 .user_print = monitor_print_cpus,
2681 .mhandler.info_new = do_info_cpus,
2684 .name = "pci",
2685 .args_type = "",
2686 .params = "",
2687 .help = "show PCI info",
2688 .user_print = do_pci_info_print,
2689 .mhandler.info_new = do_pci_info,
2692 .name = "kvm",
2693 .args_type = "",
2694 .params = "",
2695 .help = "show KVM information",
2696 .user_print = do_info_kvm_print,
2697 .mhandler.info_new = do_info_kvm,
2700 .name = "status",
2701 .args_type = "",
2702 .params = "",
2703 .help = "show the current VM status (running|paused)",
2704 .user_print = do_info_status_print,
2705 .mhandler.info_new = do_info_status,
2708 .name = "mice",
2709 .args_type = "",
2710 .params = "",
2711 .help = "show which guest mouse is receiving events",
2712 .user_print = do_info_mice_print,
2713 .mhandler.info_new = do_info_mice,
2716 .name = "vnc",
2717 .args_type = "",
2718 .params = "",
2719 .help = "show the vnc server status",
2720 .user_print = do_info_vnc_print,
2721 .mhandler.info_new = do_info_vnc,
2724 .name = "name",
2725 .args_type = "",
2726 .params = "",
2727 .help = "show the current VM name",
2728 .user_print = do_info_name_print,
2729 .mhandler.info_new = do_info_name,
2732 .name = "uuid",
2733 .args_type = "",
2734 .params = "",
2735 .help = "show the current VM UUID",
2736 .user_print = do_info_uuid_print,
2737 .mhandler.info_new = do_info_uuid,
2740 .name = "migrate",
2741 .args_type = "",
2742 .params = "",
2743 .help = "show migration status",
2744 .user_print = do_info_migrate_print,
2745 .mhandler.info_new = do_info_migrate,
2748 .name = "balloon",
2749 .args_type = "",
2750 .params = "",
2751 .help = "show balloon information",
2752 .user_print = monitor_print_balloon,
2753 .mhandler.info_async = do_info_balloon,
2754 .flags = MONITOR_CMD_ASYNC,
2756 { /* NULL */ },
2759 /*******************************************************************/
2761 static const char *pch;
2762 static jmp_buf expr_env;
2764 #define MD_TLONG 0
2765 #define MD_I32 1
2767 typedef struct MonitorDef {
2768 const char *name;
2769 int offset;
2770 target_long (*get_value)(const struct MonitorDef *md, int val);
2771 int type;
2772 } MonitorDef;
2774 #if defined(TARGET_I386)
2775 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2777 CPUState *env = mon_get_cpu();
2778 return env->eip + env->segs[R_CS].base;
2780 #endif
2782 #if defined(TARGET_PPC)
2783 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2785 CPUState *env = mon_get_cpu();
2786 unsigned int u;
2787 int i;
2789 u = 0;
2790 for (i = 0; i < 8; i++)
2791 u |= env->crf[i] << (32 - (4 * i));
2793 return u;
2796 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2798 CPUState *env = mon_get_cpu();
2799 return env->msr;
2802 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2804 CPUState *env = mon_get_cpu();
2805 return env->xer;
2808 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2810 CPUState *env = mon_get_cpu();
2811 return cpu_ppc_load_decr(env);
2814 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2816 CPUState *env = mon_get_cpu();
2817 return cpu_ppc_load_tbu(env);
2820 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2822 CPUState *env = mon_get_cpu();
2823 return cpu_ppc_load_tbl(env);
2825 #endif
2827 #if defined(TARGET_SPARC)
2828 #ifndef TARGET_SPARC64
2829 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2831 CPUState *env = mon_get_cpu();
2833 return cpu_get_psr(env);
2835 #endif
2837 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2839 CPUState *env = mon_get_cpu();
2840 return env->regwptr[val];
2842 #endif
2844 static const MonitorDef monitor_defs[] = {
2845 #ifdef TARGET_I386
2847 #define SEG(name, seg) \
2848 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2849 { name ".base", offsetof(CPUState, segs[seg].base) },\
2850 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2852 { "eax", offsetof(CPUState, regs[0]) },
2853 { "ecx", offsetof(CPUState, regs[1]) },
2854 { "edx", offsetof(CPUState, regs[2]) },
2855 { "ebx", offsetof(CPUState, regs[3]) },
2856 { "esp|sp", offsetof(CPUState, regs[4]) },
2857 { "ebp|fp", offsetof(CPUState, regs[5]) },
2858 { "esi", offsetof(CPUState, regs[6]) },
2859 { "edi", offsetof(CPUState, regs[7]) },
2860 #ifdef TARGET_X86_64
2861 { "r8", offsetof(CPUState, regs[8]) },
2862 { "r9", offsetof(CPUState, regs[9]) },
2863 { "r10", offsetof(CPUState, regs[10]) },
2864 { "r11", offsetof(CPUState, regs[11]) },
2865 { "r12", offsetof(CPUState, regs[12]) },
2866 { "r13", offsetof(CPUState, regs[13]) },
2867 { "r14", offsetof(CPUState, regs[14]) },
2868 { "r15", offsetof(CPUState, regs[15]) },
2869 #endif
2870 { "eflags", offsetof(CPUState, eflags) },
2871 { "eip", offsetof(CPUState, eip) },
2872 SEG("cs", R_CS)
2873 SEG("ds", R_DS)
2874 SEG("es", R_ES)
2875 SEG("ss", R_SS)
2876 SEG("fs", R_FS)
2877 SEG("gs", R_GS)
2878 { "pc", 0, monitor_get_pc, },
2879 #elif defined(TARGET_PPC)
2880 /* General purpose registers */
2881 { "r0", offsetof(CPUState, gpr[0]) },
2882 { "r1", offsetof(CPUState, gpr[1]) },
2883 { "r2", offsetof(CPUState, gpr[2]) },
2884 { "r3", offsetof(CPUState, gpr[3]) },
2885 { "r4", offsetof(CPUState, gpr[4]) },
2886 { "r5", offsetof(CPUState, gpr[5]) },
2887 { "r6", offsetof(CPUState, gpr[6]) },
2888 { "r7", offsetof(CPUState, gpr[7]) },
2889 { "r8", offsetof(CPUState, gpr[8]) },
2890 { "r9", offsetof(CPUState, gpr[9]) },
2891 { "r10", offsetof(CPUState, gpr[10]) },
2892 { "r11", offsetof(CPUState, gpr[11]) },
2893 { "r12", offsetof(CPUState, gpr[12]) },
2894 { "r13", offsetof(CPUState, gpr[13]) },
2895 { "r14", offsetof(CPUState, gpr[14]) },
2896 { "r15", offsetof(CPUState, gpr[15]) },
2897 { "r16", offsetof(CPUState, gpr[16]) },
2898 { "r17", offsetof(CPUState, gpr[17]) },
2899 { "r18", offsetof(CPUState, gpr[18]) },
2900 { "r19", offsetof(CPUState, gpr[19]) },
2901 { "r20", offsetof(CPUState, gpr[20]) },
2902 { "r21", offsetof(CPUState, gpr[21]) },
2903 { "r22", offsetof(CPUState, gpr[22]) },
2904 { "r23", offsetof(CPUState, gpr[23]) },
2905 { "r24", offsetof(CPUState, gpr[24]) },
2906 { "r25", offsetof(CPUState, gpr[25]) },
2907 { "r26", offsetof(CPUState, gpr[26]) },
2908 { "r27", offsetof(CPUState, gpr[27]) },
2909 { "r28", offsetof(CPUState, gpr[28]) },
2910 { "r29", offsetof(CPUState, gpr[29]) },
2911 { "r30", offsetof(CPUState, gpr[30]) },
2912 { "r31", offsetof(CPUState, gpr[31]) },
2913 /* Floating point registers */
2914 { "f0", offsetof(CPUState, fpr[0]) },
2915 { "f1", offsetof(CPUState, fpr[1]) },
2916 { "f2", offsetof(CPUState, fpr[2]) },
2917 { "f3", offsetof(CPUState, fpr[3]) },
2918 { "f4", offsetof(CPUState, fpr[4]) },
2919 { "f5", offsetof(CPUState, fpr[5]) },
2920 { "f6", offsetof(CPUState, fpr[6]) },
2921 { "f7", offsetof(CPUState, fpr[7]) },
2922 { "f8", offsetof(CPUState, fpr[8]) },
2923 { "f9", offsetof(CPUState, fpr[9]) },
2924 { "f10", offsetof(CPUState, fpr[10]) },
2925 { "f11", offsetof(CPUState, fpr[11]) },
2926 { "f12", offsetof(CPUState, fpr[12]) },
2927 { "f13", offsetof(CPUState, fpr[13]) },
2928 { "f14", offsetof(CPUState, fpr[14]) },
2929 { "f15", offsetof(CPUState, fpr[15]) },
2930 { "f16", offsetof(CPUState, fpr[16]) },
2931 { "f17", offsetof(CPUState, fpr[17]) },
2932 { "f18", offsetof(CPUState, fpr[18]) },
2933 { "f19", offsetof(CPUState, fpr[19]) },
2934 { "f20", offsetof(CPUState, fpr[20]) },
2935 { "f21", offsetof(CPUState, fpr[21]) },
2936 { "f22", offsetof(CPUState, fpr[22]) },
2937 { "f23", offsetof(CPUState, fpr[23]) },
2938 { "f24", offsetof(CPUState, fpr[24]) },
2939 { "f25", offsetof(CPUState, fpr[25]) },
2940 { "f26", offsetof(CPUState, fpr[26]) },
2941 { "f27", offsetof(CPUState, fpr[27]) },
2942 { "f28", offsetof(CPUState, fpr[28]) },
2943 { "f29", offsetof(CPUState, fpr[29]) },
2944 { "f30", offsetof(CPUState, fpr[30]) },
2945 { "f31", offsetof(CPUState, fpr[31]) },
2946 { "fpscr", offsetof(CPUState, fpscr) },
2947 /* Next instruction pointer */
2948 { "nip|pc", offsetof(CPUState, nip) },
2949 { "lr", offsetof(CPUState, lr) },
2950 { "ctr", offsetof(CPUState, ctr) },
2951 { "decr", 0, &monitor_get_decr, },
2952 { "ccr", 0, &monitor_get_ccr, },
2953 /* Machine state register */
2954 { "msr", 0, &monitor_get_msr, },
2955 { "xer", 0, &monitor_get_xer, },
2956 { "tbu", 0, &monitor_get_tbu, },
2957 { "tbl", 0, &monitor_get_tbl, },
2958 #if defined(TARGET_PPC64)
2959 /* Address space register */
2960 { "asr", offsetof(CPUState, asr) },
2961 #endif
2962 /* Segment registers */
2963 { "sdr1", offsetof(CPUState, sdr1) },
2964 { "sr0", offsetof(CPUState, sr[0]) },
2965 { "sr1", offsetof(CPUState, sr[1]) },
2966 { "sr2", offsetof(CPUState, sr[2]) },
2967 { "sr3", offsetof(CPUState, sr[3]) },
2968 { "sr4", offsetof(CPUState, sr[4]) },
2969 { "sr5", offsetof(CPUState, sr[5]) },
2970 { "sr6", offsetof(CPUState, sr[6]) },
2971 { "sr7", offsetof(CPUState, sr[7]) },
2972 { "sr8", offsetof(CPUState, sr[8]) },
2973 { "sr9", offsetof(CPUState, sr[9]) },
2974 { "sr10", offsetof(CPUState, sr[10]) },
2975 { "sr11", offsetof(CPUState, sr[11]) },
2976 { "sr12", offsetof(CPUState, sr[12]) },
2977 { "sr13", offsetof(CPUState, sr[13]) },
2978 { "sr14", offsetof(CPUState, sr[14]) },
2979 { "sr15", offsetof(CPUState, sr[15]) },
2980 /* Too lazy to put BATs and SPRs ... */
2981 #elif defined(TARGET_SPARC)
2982 { "g0", offsetof(CPUState, gregs[0]) },
2983 { "g1", offsetof(CPUState, gregs[1]) },
2984 { "g2", offsetof(CPUState, gregs[2]) },
2985 { "g3", offsetof(CPUState, gregs[3]) },
2986 { "g4", offsetof(CPUState, gregs[4]) },
2987 { "g5", offsetof(CPUState, gregs[5]) },
2988 { "g6", offsetof(CPUState, gregs[6]) },
2989 { "g7", offsetof(CPUState, gregs[7]) },
2990 { "o0", 0, monitor_get_reg },
2991 { "o1", 1, monitor_get_reg },
2992 { "o2", 2, monitor_get_reg },
2993 { "o3", 3, monitor_get_reg },
2994 { "o4", 4, monitor_get_reg },
2995 { "o5", 5, monitor_get_reg },
2996 { "o6", 6, monitor_get_reg },
2997 { "o7", 7, monitor_get_reg },
2998 { "l0", 8, monitor_get_reg },
2999 { "l1", 9, monitor_get_reg },
3000 { "l2", 10, monitor_get_reg },
3001 { "l3", 11, monitor_get_reg },
3002 { "l4", 12, monitor_get_reg },
3003 { "l5", 13, monitor_get_reg },
3004 { "l6", 14, monitor_get_reg },
3005 { "l7", 15, monitor_get_reg },
3006 { "i0", 16, monitor_get_reg },
3007 { "i1", 17, monitor_get_reg },
3008 { "i2", 18, monitor_get_reg },
3009 { "i3", 19, monitor_get_reg },
3010 { "i4", 20, monitor_get_reg },
3011 { "i5", 21, monitor_get_reg },
3012 { "i6", 22, monitor_get_reg },
3013 { "i7", 23, monitor_get_reg },
3014 { "pc", offsetof(CPUState, pc) },
3015 { "npc", offsetof(CPUState, npc) },
3016 { "y", offsetof(CPUState, y) },
3017 #ifndef TARGET_SPARC64
3018 { "psr", 0, &monitor_get_psr, },
3019 { "wim", offsetof(CPUState, wim) },
3020 #endif
3021 { "tbr", offsetof(CPUState, tbr) },
3022 { "fsr", offsetof(CPUState, fsr) },
3023 { "f0", offsetof(CPUState, fpr[0]) },
3024 { "f1", offsetof(CPUState, fpr[1]) },
3025 { "f2", offsetof(CPUState, fpr[2]) },
3026 { "f3", offsetof(CPUState, fpr[3]) },
3027 { "f4", offsetof(CPUState, fpr[4]) },
3028 { "f5", offsetof(CPUState, fpr[5]) },
3029 { "f6", offsetof(CPUState, fpr[6]) },
3030 { "f7", offsetof(CPUState, fpr[7]) },
3031 { "f8", offsetof(CPUState, fpr[8]) },
3032 { "f9", offsetof(CPUState, fpr[9]) },
3033 { "f10", offsetof(CPUState, fpr[10]) },
3034 { "f11", offsetof(CPUState, fpr[11]) },
3035 { "f12", offsetof(CPUState, fpr[12]) },
3036 { "f13", offsetof(CPUState, fpr[13]) },
3037 { "f14", offsetof(CPUState, fpr[14]) },
3038 { "f15", offsetof(CPUState, fpr[15]) },
3039 { "f16", offsetof(CPUState, fpr[16]) },
3040 { "f17", offsetof(CPUState, fpr[17]) },
3041 { "f18", offsetof(CPUState, fpr[18]) },
3042 { "f19", offsetof(CPUState, fpr[19]) },
3043 { "f20", offsetof(CPUState, fpr[20]) },
3044 { "f21", offsetof(CPUState, fpr[21]) },
3045 { "f22", offsetof(CPUState, fpr[22]) },
3046 { "f23", offsetof(CPUState, fpr[23]) },
3047 { "f24", offsetof(CPUState, fpr[24]) },
3048 { "f25", offsetof(CPUState, fpr[25]) },
3049 { "f26", offsetof(CPUState, fpr[26]) },
3050 { "f27", offsetof(CPUState, fpr[27]) },
3051 { "f28", offsetof(CPUState, fpr[28]) },
3052 { "f29", offsetof(CPUState, fpr[29]) },
3053 { "f30", offsetof(CPUState, fpr[30]) },
3054 { "f31", offsetof(CPUState, fpr[31]) },
3055 #ifdef TARGET_SPARC64
3056 { "f32", offsetof(CPUState, fpr[32]) },
3057 { "f34", offsetof(CPUState, fpr[34]) },
3058 { "f36", offsetof(CPUState, fpr[36]) },
3059 { "f38", offsetof(CPUState, fpr[38]) },
3060 { "f40", offsetof(CPUState, fpr[40]) },
3061 { "f42", offsetof(CPUState, fpr[42]) },
3062 { "f44", offsetof(CPUState, fpr[44]) },
3063 { "f46", offsetof(CPUState, fpr[46]) },
3064 { "f48", offsetof(CPUState, fpr[48]) },
3065 { "f50", offsetof(CPUState, fpr[50]) },
3066 { "f52", offsetof(CPUState, fpr[52]) },
3067 { "f54", offsetof(CPUState, fpr[54]) },
3068 { "f56", offsetof(CPUState, fpr[56]) },
3069 { "f58", offsetof(CPUState, fpr[58]) },
3070 { "f60", offsetof(CPUState, fpr[60]) },
3071 { "f62", offsetof(CPUState, fpr[62]) },
3072 { "asi", offsetof(CPUState, asi) },
3073 { "pstate", offsetof(CPUState, pstate) },
3074 { "cansave", offsetof(CPUState, cansave) },
3075 { "canrestore", offsetof(CPUState, canrestore) },
3076 { "otherwin", offsetof(CPUState, otherwin) },
3077 { "wstate", offsetof(CPUState, wstate) },
3078 { "cleanwin", offsetof(CPUState, cleanwin) },
3079 { "fprs", offsetof(CPUState, fprs) },
3080 #endif
3081 #endif
3082 { NULL },
3085 static void expr_error(Monitor *mon, const char *msg)
3087 monitor_printf(mon, "%s\n", msg);
3088 longjmp(expr_env, 1);
3091 /* return 0 if OK, -1 if not found */
3092 static int get_monitor_def(target_long *pval, const char *name)
3094 const MonitorDef *md;
3095 void *ptr;
3097 for(md = monitor_defs; md->name != NULL; md++) {
3098 if (compare_cmd(name, md->name)) {
3099 if (md->get_value) {
3100 *pval = md->get_value(md, md->offset);
3101 } else {
3102 CPUState *env = mon_get_cpu();
3103 ptr = (uint8_t *)env + md->offset;
3104 switch(md->type) {
3105 case MD_I32:
3106 *pval = *(int32_t *)ptr;
3107 break;
3108 case MD_TLONG:
3109 *pval = *(target_long *)ptr;
3110 break;
3111 default:
3112 *pval = 0;
3113 break;
3116 return 0;
3119 return -1;
3122 static void next(void)
3124 if (*pch != '\0') {
3125 pch++;
3126 while (qemu_isspace(*pch))
3127 pch++;
3131 static int64_t expr_sum(Monitor *mon);
3133 static int64_t expr_unary(Monitor *mon)
3135 int64_t n;
3136 char *p;
3137 int ret;
3139 switch(*pch) {
3140 case '+':
3141 next();
3142 n = expr_unary(mon);
3143 break;
3144 case '-':
3145 next();
3146 n = -expr_unary(mon);
3147 break;
3148 case '~':
3149 next();
3150 n = ~expr_unary(mon);
3151 break;
3152 case '(':
3153 next();
3154 n = expr_sum(mon);
3155 if (*pch != ')') {
3156 expr_error(mon, "')' expected");
3158 next();
3159 break;
3160 case '\'':
3161 pch++;
3162 if (*pch == '\0')
3163 expr_error(mon, "character constant expected");
3164 n = *pch;
3165 pch++;
3166 if (*pch != '\'')
3167 expr_error(mon, "missing terminating \' character");
3168 next();
3169 break;
3170 case '$':
3172 char buf[128], *q;
3173 target_long reg=0;
3175 pch++;
3176 q = buf;
3177 while ((*pch >= 'a' && *pch <= 'z') ||
3178 (*pch >= 'A' && *pch <= 'Z') ||
3179 (*pch >= '0' && *pch <= '9') ||
3180 *pch == '_' || *pch == '.') {
3181 if ((q - buf) < sizeof(buf) - 1)
3182 *q++ = *pch;
3183 pch++;
3185 while (qemu_isspace(*pch))
3186 pch++;
3187 *q = 0;
3188 ret = get_monitor_def(&reg, buf);
3189 if (ret < 0)
3190 expr_error(mon, "unknown register");
3191 n = reg;
3193 break;
3194 case '\0':
3195 expr_error(mon, "unexpected end of expression");
3196 n = 0;
3197 break;
3198 default:
3199 #if TARGET_PHYS_ADDR_BITS > 32
3200 n = strtoull(pch, &p, 0);
3201 #else
3202 n = strtoul(pch, &p, 0);
3203 #endif
3204 if (pch == p) {
3205 expr_error(mon, "invalid char in expression");
3207 pch = p;
3208 while (qemu_isspace(*pch))
3209 pch++;
3210 break;
3212 return n;
3216 static int64_t expr_prod(Monitor *mon)
3218 int64_t val, val2;
3219 int op;
3221 val = expr_unary(mon);
3222 for(;;) {
3223 op = *pch;
3224 if (op != '*' && op != '/' && op != '%')
3225 break;
3226 next();
3227 val2 = expr_unary(mon);
3228 switch(op) {
3229 default:
3230 case '*':
3231 val *= val2;
3232 break;
3233 case '/':
3234 case '%':
3235 if (val2 == 0)
3236 expr_error(mon, "division by zero");
3237 if (op == '/')
3238 val /= val2;
3239 else
3240 val %= val2;
3241 break;
3244 return val;
3247 static int64_t expr_logic(Monitor *mon)
3249 int64_t val, val2;
3250 int op;
3252 val = expr_prod(mon);
3253 for(;;) {
3254 op = *pch;
3255 if (op != '&' && op != '|' && op != '^')
3256 break;
3257 next();
3258 val2 = expr_prod(mon);
3259 switch(op) {
3260 default:
3261 case '&':
3262 val &= val2;
3263 break;
3264 case '|':
3265 val |= val2;
3266 break;
3267 case '^':
3268 val ^= val2;
3269 break;
3272 return val;
3275 static int64_t expr_sum(Monitor *mon)
3277 int64_t val, val2;
3278 int op;
3280 val = expr_logic(mon);
3281 for(;;) {
3282 op = *pch;
3283 if (op != '+' && op != '-')
3284 break;
3285 next();
3286 val2 = expr_logic(mon);
3287 if (op == '+')
3288 val += val2;
3289 else
3290 val -= val2;
3292 return val;
3295 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3297 pch = *pp;
3298 if (setjmp(expr_env)) {
3299 *pp = pch;
3300 return -1;
3302 while (qemu_isspace(*pch))
3303 pch++;
3304 *pval = expr_sum(mon);
3305 *pp = pch;
3306 return 0;
3309 static int get_double(Monitor *mon, double *pval, const char **pp)
3311 const char *p = *pp;
3312 char *tailp;
3313 double d;
3315 d = strtod(p, &tailp);
3316 if (tailp == p) {
3317 monitor_printf(mon, "Number expected\n");
3318 return -1;
3320 if (d != d || d - d != 0) {
3321 /* NaN or infinity */
3322 monitor_printf(mon, "Bad number\n");
3323 return -1;
3325 *pval = d;
3326 *pp = tailp;
3327 return 0;
3330 static int get_str(char *buf, int buf_size, const char **pp)
3332 const char *p;
3333 char *q;
3334 int c;
3336 q = buf;
3337 p = *pp;
3338 while (qemu_isspace(*p))
3339 p++;
3340 if (*p == '\0') {
3341 fail:
3342 *q = '\0';
3343 *pp = p;
3344 return -1;
3346 if (*p == '\"') {
3347 p++;
3348 while (*p != '\0' && *p != '\"') {
3349 if (*p == '\\') {
3350 p++;
3351 c = *p++;
3352 switch(c) {
3353 case 'n':
3354 c = '\n';
3355 break;
3356 case 'r':
3357 c = '\r';
3358 break;
3359 case '\\':
3360 case '\'':
3361 case '\"':
3362 break;
3363 default:
3364 qemu_printf("unsupported escape code: '\\%c'\n", c);
3365 goto fail;
3367 if ((q - buf) < buf_size - 1) {
3368 *q++ = c;
3370 } else {
3371 if ((q - buf) < buf_size - 1) {
3372 *q++ = *p;
3374 p++;
3377 if (*p != '\"') {
3378 qemu_printf("unterminated string\n");
3379 goto fail;
3381 p++;
3382 } else {
3383 while (*p != '\0' && !qemu_isspace(*p)) {
3384 if ((q - buf) < buf_size - 1) {
3385 *q++ = *p;
3387 p++;
3390 *q = '\0';
3391 *pp = p;
3392 return 0;
3396 * Store the command-name in cmdname, and return a pointer to
3397 * the remaining of the command string.
3399 static const char *get_command_name(const char *cmdline,
3400 char *cmdname, size_t nlen)
3402 size_t len;
3403 const char *p, *pstart;
3405 p = cmdline;
3406 while (qemu_isspace(*p))
3407 p++;
3408 if (*p == '\0')
3409 return NULL;
3410 pstart = p;
3411 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3412 p++;
3413 len = p - pstart;
3414 if (len > nlen - 1)
3415 len = nlen - 1;
3416 memcpy(cmdname, pstart, len);
3417 cmdname[len] = '\0';
3418 return p;
3422 * Read key of 'type' into 'key' and return the current
3423 * 'type' pointer.
3425 static char *key_get_info(const char *type, char **key)
3427 size_t len;
3428 char *p, *str;
3430 if (*type == ',')
3431 type++;
3433 p = strchr(type, ':');
3434 if (!p) {
3435 *key = NULL;
3436 return NULL;
3438 len = p - type;
3440 str = qemu_malloc(len + 1);
3441 memcpy(str, type, len);
3442 str[len] = '\0';
3444 *key = str;
3445 return ++p;
3448 static int default_fmt_format = 'x';
3449 static int default_fmt_size = 4;
3451 #define MAX_ARGS 16
3453 static int is_valid_option(const char *c, const char *typestr)
3455 char option[3];
3457 option[0] = '-';
3458 option[1] = *c;
3459 option[2] = '\0';
3461 typestr = strstr(typestr, option);
3462 return (typestr != NULL);
3465 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3466 const char *cmdname)
3468 const mon_cmd_t *cmd;
3470 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3471 if (compare_cmd(cmdname, cmd->name)) {
3472 return cmd;
3476 return NULL;
3479 static const mon_cmd_t *monitor_find_command(const char *cmdname)
3481 return search_dispatch_table(mon_cmds, cmdname);
3484 static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3486 return search_dispatch_table(qmp_query_cmds, info_item);
3489 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3491 return search_dispatch_table(qmp_cmds, cmdname);
3494 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3495 const char *cmdline,
3496 QDict *qdict)
3498 const char *p, *typestr;
3499 int c;
3500 const mon_cmd_t *cmd;
3501 char cmdname[256];
3502 char buf[1024];
3503 char *key;
3505 #ifdef DEBUG
3506 monitor_printf(mon, "command='%s'\n", cmdline);
3507 #endif
3509 /* extract the command name */
3510 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3511 if (!p)
3512 return NULL;
3514 cmd = monitor_find_command(cmdname);
3515 if (!cmd) {
3516 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
3517 return NULL;
3520 /* parse the parameters */
3521 typestr = cmd->args_type;
3522 for(;;) {
3523 typestr = key_get_info(typestr, &key);
3524 if (!typestr)
3525 break;
3526 c = *typestr;
3527 typestr++;
3528 switch(c) {
3529 case 'F':
3530 case 'B':
3531 case 's':
3533 int ret;
3535 while (qemu_isspace(*p))
3536 p++;
3537 if (*typestr == '?') {
3538 typestr++;
3539 if (*p == '\0') {
3540 /* no optional string: NULL argument */
3541 break;
3544 ret = get_str(buf, sizeof(buf), &p);
3545 if (ret < 0) {
3546 switch(c) {
3547 case 'F':
3548 monitor_printf(mon, "%s: filename expected\n",
3549 cmdname);
3550 break;
3551 case 'B':
3552 monitor_printf(mon, "%s: block device name expected\n",
3553 cmdname);
3554 break;
3555 default:
3556 monitor_printf(mon, "%s: string expected\n", cmdname);
3557 break;
3559 goto fail;
3561 qdict_put(qdict, key, qstring_from_str(buf));
3563 break;
3564 case 'O':
3566 QemuOptsList *opts_list;
3567 QemuOpts *opts;
3569 opts_list = qemu_find_opts(key);
3570 if (!opts_list || opts_list->desc->name) {
3571 goto bad_type;
3573 while (qemu_isspace(*p)) {
3574 p++;
3576 if (!*p)
3577 break;
3578 if (get_str(buf, sizeof(buf), &p) < 0) {
3579 goto fail;
3581 opts = qemu_opts_parse(opts_list, buf, 1);
3582 if (!opts) {
3583 goto fail;
3585 qemu_opts_to_qdict(opts, qdict);
3586 qemu_opts_del(opts);
3588 break;
3589 case '/':
3591 int count, format, size;
3593 while (qemu_isspace(*p))
3594 p++;
3595 if (*p == '/') {
3596 /* format found */
3597 p++;
3598 count = 1;
3599 if (qemu_isdigit(*p)) {
3600 count = 0;
3601 while (qemu_isdigit(*p)) {
3602 count = count * 10 + (*p - '0');
3603 p++;
3606 size = -1;
3607 format = -1;
3608 for(;;) {
3609 switch(*p) {
3610 case 'o':
3611 case 'd':
3612 case 'u':
3613 case 'x':
3614 case 'i':
3615 case 'c':
3616 format = *p++;
3617 break;
3618 case 'b':
3619 size = 1;
3620 p++;
3621 break;
3622 case 'h':
3623 size = 2;
3624 p++;
3625 break;
3626 case 'w':
3627 size = 4;
3628 p++;
3629 break;
3630 case 'g':
3631 case 'L':
3632 size = 8;
3633 p++;
3634 break;
3635 default:
3636 goto next;
3639 next:
3640 if (*p != '\0' && !qemu_isspace(*p)) {
3641 monitor_printf(mon, "invalid char in format: '%c'\n",
3642 *p);
3643 goto fail;
3645 if (format < 0)
3646 format = default_fmt_format;
3647 if (format != 'i') {
3648 /* for 'i', not specifying a size gives -1 as size */
3649 if (size < 0)
3650 size = default_fmt_size;
3651 default_fmt_size = size;
3653 default_fmt_format = format;
3654 } else {
3655 count = 1;
3656 format = default_fmt_format;
3657 if (format != 'i') {
3658 size = default_fmt_size;
3659 } else {
3660 size = -1;
3663 qdict_put(qdict, "count", qint_from_int(count));
3664 qdict_put(qdict, "format", qint_from_int(format));
3665 qdict_put(qdict, "size", qint_from_int(size));
3667 break;
3668 case 'i':
3669 case 'l':
3670 case 'M':
3672 int64_t val;
3674 while (qemu_isspace(*p))
3675 p++;
3676 if (*typestr == '?' || *typestr == '.') {
3677 if (*typestr == '?') {
3678 if (*p == '\0') {
3679 typestr++;
3680 break;
3682 } else {
3683 if (*p == '.') {
3684 p++;
3685 while (qemu_isspace(*p))
3686 p++;
3687 } else {
3688 typestr++;
3689 break;
3692 typestr++;
3694 if (get_expr(mon, &val, &p))
3695 goto fail;
3696 /* Check if 'i' is greater than 32-bit */
3697 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3698 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3699 monitor_printf(mon, "integer is for 32-bit values\n");
3700 goto fail;
3701 } else if (c == 'M') {
3702 val <<= 20;
3704 qdict_put(qdict, key, qint_from_int(val));
3706 break;
3707 case 'o':
3709 ssize_t val;
3710 char *end;
3712 while (qemu_isspace(*p)) {
3713 p++;
3715 if (*typestr == '?') {
3716 typestr++;
3717 if (*p == '\0') {
3718 break;
3721 val = strtosz(p, &end);
3722 if (val < 0) {
3723 monitor_printf(mon, "invalid size\n");
3724 goto fail;
3726 qdict_put(qdict, key, qint_from_int(val));
3727 p = end;
3729 break;
3730 case 'T':
3732 double val;
3734 while (qemu_isspace(*p))
3735 p++;
3736 if (*typestr == '?') {
3737 typestr++;
3738 if (*p == '\0') {
3739 break;
3742 if (get_double(mon, &val, &p) < 0) {
3743 goto fail;
3745 if (p[0] && p[1] == 's') {
3746 switch (*p) {
3747 case 'm':
3748 val /= 1e3; p += 2; break;
3749 case 'u':
3750 val /= 1e6; p += 2; break;
3751 case 'n':
3752 val /= 1e9; p += 2; break;
3755 if (*p && !qemu_isspace(*p)) {
3756 monitor_printf(mon, "Unknown unit suffix\n");
3757 goto fail;
3759 qdict_put(qdict, key, qfloat_from_double(val));
3761 break;
3762 case 'b':
3764 const char *beg;
3765 int val;
3767 while (qemu_isspace(*p)) {
3768 p++;
3770 beg = p;
3771 while (qemu_isgraph(*p)) {
3772 p++;
3774 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3775 val = 1;
3776 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3777 val = 0;
3778 } else {
3779 monitor_printf(mon, "Expected 'on' or 'off'\n");
3780 goto fail;
3782 qdict_put(qdict, key, qbool_from_int(val));
3784 break;
3785 case '-':
3787 const char *tmp = p;
3788 int skip_key = 0;
3789 /* option */
3791 c = *typestr++;
3792 if (c == '\0')
3793 goto bad_type;
3794 while (qemu_isspace(*p))
3795 p++;
3796 if (*p == '-') {
3797 p++;
3798 if(c != *p) {
3799 if(!is_valid_option(p, typestr)) {
3801 monitor_printf(mon, "%s: unsupported option -%c\n",
3802 cmdname, *p);
3803 goto fail;
3804 } else {
3805 skip_key = 1;
3808 if(skip_key) {
3809 p = tmp;
3810 } else {
3811 /* has option */
3812 p++;
3813 qdict_put(qdict, key, qbool_from_int(1));
3817 break;
3818 default:
3819 bad_type:
3820 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3821 goto fail;
3823 qemu_free(key);
3824 key = NULL;
3826 /* check that all arguments were parsed */
3827 while (qemu_isspace(*p))
3828 p++;
3829 if (*p != '\0') {
3830 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3831 cmdname);
3832 goto fail;
3835 return cmd;
3837 fail:
3838 qemu_free(key);
3839 return NULL;
3842 void monitor_set_error(Monitor *mon, QError *qerror)
3844 /* report only the first error */
3845 if (!mon->error) {
3846 mon->error = qerror;
3847 } else {
3848 MON_DEBUG("Additional error report at %s:%d\n",
3849 qerror->file, qerror->linenr);
3850 QDECREF(qerror);
3854 static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3856 if (monitor_ctrl_mode(mon)) {
3857 if (ret && !monitor_has_error(mon)) {
3859 * If it returns failure, it must have passed on error.
3861 * Action: Report an internal error to the client if in QMP.
3863 qerror_report(QERR_UNDEFINED_ERROR);
3864 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3865 cmd->name);
3868 #ifdef CONFIG_DEBUG_MONITOR
3869 if (!ret && monitor_has_error(mon)) {
3871 * If it returns success, it must not have passed an error.
3873 * Action: Report the passed error to the client.
3875 MON_DEBUG("command '%s' returned success but passed an error\n",
3876 cmd->name);
3879 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3881 * Handlers should not call Monitor print functions.
3883 * Action: Ignore them in QMP.
3885 * (XXX: we don't check any 'info' or 'query' command here
3886 * because the user print function _is_ called by do_info(), hence
3887 * we will trigger this check. This problem will go away when we
3888 * make 'query' commands real and kill do_info())
3890 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3891 cmd->name, mon_print_count_get(mon));
3893 #endif
3894 } else {
3895 assert(!monitor_has_error(mon));
3896 QDECREF(mon->error);
3897 mon->error = NULL;
3901 static void handle_user_command(Monitor *mon, const char *cmdline)
3903 QDict *qdict;
3904 const mon_cmd_t *cmd;
3906 qdict = qdict_new();
3908 cmd = monitor_parse_command(mon, cmdline, qdict);
3909 if (!cmd)
3910 goto out;
3912 if (handler_is_async(cmd)) {
3913 user_async_cmd_handler(mon, cmd, qdict);
3914 } else if (handler_is_qobject(cmd)) {
3915 QObject *data = NULL;
3917 /* XXX: ignores the error code */
3918 cmd->mhandler.cmd_new(mon, qdict, &data);
3919 assert(!monitor_has_error(mon));
3920 if (data) {
3921 cmd->user_print(mon, data);
3922 qobject_decref(data);
3924 } else {
3925 cmd->mhandler.cmd(mon, qdict);
3928 out:
3929 QDECREF(qdict);
3932 static void cmd_completion(const char *name, const char *list)
3934 const char *p, *pstart;
3935 char cmd[128];
3936 int len;
3938 p = list;
3939 for(;;) {
3940 pstart = p;
3941 p = strchr(p, '|');
3942 if (!p)
3943 p = pstart + strlen(pstart);
3944 len = p - pstart;
3945 if (len > sizeof(cmd) - 2)
3946 len = sizeof(cmd) - 2;
3947 memcpy(cmd, pstart, len);
3948 cmd[len] = '\0';
3949 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3950 readline_add_completion(cur_mon->rs, cmd);
3952 if (*p == '\0')
3953 break;
3954 p++;
3958 static void file_completion(const char *input)
3960 DIR *ffs;
3961 struct dirent *d;
3962 char path[1024];
3963 char file[1024], file_prefix[1024];
3964 int input_path_len;
3965 const char *p;
3967 p = strrchr(input, '/');
3968 if (!p) {
3969 input_path_len = 0;
3970 pstrcpy(file_prefix, sizeof(file_prefix), input);
3971 pstrcpy(path, sizeof(path), ".");
3972 } else {
3973 input_path_len = p - input + 1;
3974 memcpy(path, input, input_path_len);
3975 if (input_path_len > sizeof(path) - 1)
3976 input_path_len = sizeof(path) - 1;
3977 path[input_path_len] = '\0';
3978 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3980 #ifdef DEBUG_COMPLETION
3981 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3982 input, path, file_prefix);
3983 #endif
3984 ffs = opendir(path);
3985 if (!ffs)
3986 return;
3987 for(;;) {
3988 struct stat sb;
3989 d = readdir(ffs);
3990 if (!d)
3991 break;
3993 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3994 continue;
3997 if (strstart(d->d_name, file_prefix, NULL)) {
3998 memcpy(file, input, input_path_len);
3999 if (input_path_len < sizeof(file))
4000 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4001 d->d_name);
4002 /* stat the file to find out if it's a directory.
4003 * In that case add a slash to speed up typing long paths
4005 stat(file, &sb);
4006 if(S_ISDIR(sb.st_mode))
4007 pstrcat(file, sizeof(file), "/");
4008 readline_add_completion(cur_mon->rs, file);
4011 closedir(ffs);
4014 static void block_completion_it(void *opaque, BlockDriverState *bs)
4016 const char *name = bdrv_get_device_name(bs);
4017 const char *input = opaque;
4019 if (input[0] == '\0' ||
4020 !strncmp(name, (char *)input, strlen(input))) {
4021 readline_add_completion(cur_mon->rs, name);
4025 /* NOTE: this parser is an approximate form of the real command parser */
4026 static void parse_cmdline(const char *cmdline,
4027 int *pnb_args, char **args)
4029 const char *p;
4030 int nb_args, ret;
4031 char buf[1024];
4033 p = cmdline;
4034 nb_args = 0;
4035 for(;;) {
4036 while (qemu_isspace(*p))
4037 p++;
4038 if (*p == '\0')
4039 break;
4040 if (nb_args >= MAX_ARGS)
4041 break;
4042 ret = get_str(buf, sizeof(buf), &p);
4043 args[nb_args] = qemu_strdup(buf);
4044 nb_args++;
4045 if (ret < 0)
4046 break;
4048 *pnb_args = nb_args;
4051 static const char *next_arg_type(const char *typestr)
4053 const char *p = strchr(typestr, ':');
4054 return (p != NULL ? ++p : typestr);
4057 static void monitor_find_completion(const char *cmdline)
4059 const char *cmdname;
4060 char *args[MAX_ARGS];
4061 int nb_args, i, len;
4062 const char *ptype, *str;
4063 const mon_cmd_t *cmd;
4064 const KeyDef *key;
4066 parse_cmdline(cmdline, &nb_args, args);
4067 #ifdef DEBUG_COMPLETION
4068 for(i = 0; i < nb_args; i++) {
4069 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
4071 #endif
4073 /* if the line ends with a space, it means we want to complete the
4074 next arg */
4075 len = strlen(cmdline);
4076 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4077 if (nb_args >= MAX_ARGS) {
4078 goto cleanup;
4080 args[nb_args++] = qemu_strdup("");
4082 if (nb_args <= 1) {
4083 /* command completion */
4084 if (nb_args == 0)
4085 cmdname = "";
4086 else
4087 cmdname = args[0];
4088 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
4089 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
4090 cmd_completion(cmdname, cmd->name);
4092 } else {
4093 /* find the command */
4094 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4095 if (compare_cmd(args[0], cmd->name)) {
4096 break;
4099 if (!cmd->name) {
4100 goto cleanup;
4103 ptype = next_arg_type(cmd->args_type);
4104 for(i = 0; i < nb_args - 2; i++) {
4105 if (*ptype != '\0') {
4106 ptype = next_arg_type(ptype);
4107 while (*ptype == '?')
4108 ptype = next_arg_type(ptype);
4111 str = args[nb_args - 1];
4112 if (*ptype == '-' && ptype[1] != '\0') {
4113 ptype = next_arg_type(ptype);
4115 switch(*ptype) {
4116 case 'F':
4117 /* file completion */
4118 readline_set_completion_index(cur_mon->rs, strlen(str));
4119 file_completion(str);
4120 break;
4121 case 'B':
4122 /* block device name completion */
4123 readline_set_completion_index(cur_mon->rs, strlen(str));
4124 bdrv_iterate(block_completion_it, (void *)str);
4125 break;
4126 case 's':
4127 /* XXX: more generic ? */
4128 if (!strcmp(cmd->name, "info")) {
4129 readline_set_completion_index(cur_mon->rs, strlen(str));
4130 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4131 cmd_completion(str, cmd->name);
4133 } else if (!strcmp(cmd->name, "sendkey")) {
4134 char *sep = strrchr(str, '-');
4135 if (sep)
4136 str = sep + 1;
4137 readline_set_completion_index(cur_mon->rs, strlen(str));
4138 for(key = key_defs; key->name != NULL; key++) {
4139 cmd_completion(str, key->name);
4141 } else if (!strcmp(cmd->name, "help|?")) {
4142 readline_set_completion_index(cur_mon->rs, strlen(str));
4143 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4144 cmd_completion(str, cmd->name);
4147 break;
4148 default:
4149 break;
4153 cleanup:
4154 for (i = 0; i < nb_args; i++) {
4155 qemu_free(args[i]);
4159 static int monitor_can_read(void *opaque)
4161 Monitor *mon = opaque;
4163 return (mon->suspend_cnt == 0) ? 1 : 0;
4166 static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4168 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4169 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4173 * Argument validation rules:
4175 * 1. The argument must exist in cmd_args qdict
4176 * 2. The argument type must be the expected one
4178 * Special case: If the argument doesn't exist in cmd_args and
4179 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4180 * checking is skipped for it.
4182 static int check_client_args_type(const QDict *client_args,
4183 const QDict *cmd_args, int flags)
4185 const QDictEntry *ent;
4187 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4188 QObject *obj;
4189 QString *arg_type;
4190 const QObject *client_arg = qdict_entry_value(ent);
4191 const char *client_arg_name = qdict_entry_key(ent);
4193 obj = qdict_get(cmd_args, client_arg_name);
4194 if (!obj) {
4195 if (flags & QMP_ACCEPT_UNKNOWNS) {
4196 /* handler accepts unknowns */
4197 continue;
4199 /* client arg doesn't exist */
4200 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4201 return -1;
4204 arg_type = qobject_to_qstring(obj);
4205 assert(arg_type != NULL);
4207 /* check if argument's type is correct */
4208 switch (qstring_get_str(arg_type)[0]) {
4209 case 'F':
4210 case 'B':
4211 case 's':
4212 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4213 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4214 "string");
4215 return -1;
4217 break;
4218 case 'i':
4219 case 'l':
4220 case 'M':
4221 case 'o':
4222 if (qobject_type(client_arg) != QTYPE_QINT) {
4223 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4224 "int");
4225 return -1;
4227 break;
4228 case 'T':
4229 if (qobject_type(client_arg) != QTYPE_QINT &&
4230 qobject_type(client_arg) != QTYPE_QFLOAT) {
4231 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4232 "number");
4233 return -1;
4235 break;
4236 case 'b':
4237 case '-':
4238 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4239 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4240 "bool");
4241 return -1;
4243 break;
4244 case 'O':
4245 assert(flags & QMP_ACCEPT_UNKNOWNS);
4246 break;
4247 case '/':
4248 case '.':
4250 * These types are not supported by QMP and thus are not
4251 * handled here. Fall through.
4253 default:
4254 abort();
4258 return 0;
4262 * - Check if the client has passed all mandatory args
4263 * - Set special flags for argument validation
4265 static int check_mandatory_args(const QDict *cmd_args,
4266 const QDict *client_args, int *flags)
4268 const QDictEntry *ent;
4270 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4271 const char *cmd_arg_name = qdict_entry_key(ent);
4272 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4273 assert(type != NULL);
4275 if (qstring_get_str(type)[0] == 'O') {
4276 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4277 *flags |= QMP_ACCEPT_UNKNOWNS;
4278 } else if (qstring_get_str(type)[0] != '-' &&
4279 qstring_get_str(type)[1] != '?' &&
4280 !qdict_haskey(client_args, cmd_arg_name)) {
4281 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4282 return -1;
4286 return 0;
4289 static QDict *qdict_from_args_type(const char *args_type)
4291 int i;
4292 QDict *qdict;
4293 QString *key, *type, *cur_qs;
4295 assert(args_type != NULL);
4297 qdict = qdict_new();
4299 if (args_type == NULL || args_type[0] == '\0') {
4300 /* no args, empty qdict */
4301 goto out;
4304 key = qstring_new();
4305 type = qstring_new();
4307 cur_qs = key;
4309 for (i = 0;; i++) {
4310 switch (args_type[i]) {
4311 case ',':
4312 case '\0':
4313 qdict_put(qdict, qstring_get_str(key), type);
4314 QDECREF(key);
4315 if (args_type[i] == '\0') {
4316 goto out;
4318 type = qstring_new(); /* qdict has ref */
4319 cur_qs = key = qstring_new();
4320 break;
4321 case ':':
4322 cur_qs = type;
4323 break;
4324 default:
4325 qstring_append_chr(cur_qs, args_type[i]);
4326 break;
4330 out:
4331 return qdict;
4335 * Client argument checking rules:
4337 * 1. Client must provide all mandatory arguments
4338 * 2. Each argument provided by the client must be expected
4339 * 3. Each argument provided by the client must have the type expected
4340 * by the command
4342 static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4344 int flags, err;
4345 QDict *cmd_args;
4347 cmd_args = qdict_from_args_type(cmd->args_type);
4349 flags = 0;
4350 err = check_mandatory_args(cmd_args, client_args, &flags);
4351 if (err) {
4352 goto out;
4355 err = check_client_args_type(client_args, cmd_args, flags);
4357 out:
4358 QDECREF(cmd_args);
4359 return err;
4363 * Input object checking rules
4365 * 1. Input object must be a dict
4366 * 2. The "execute" key must exist
4367 * 3. The "execute" key must be a string
4368 * 4. If the "arguments" key exists, it must be a dict
4369 * 5. If the "id" key exists, it can be anything (ie. json-value)
4370 * 6. Any argument not listed above is considered invalid
4372 static QDict *qmp_check_input_obj(QObject *input_obj)
4374 const QDictEntry *ent;
4375 int has_exec_key = 0;
4376 QDict *input_dict;
4378 if (qobject_type(input_obj) != QTYPE_QDICT) {
4379 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4380 return NULL;
4383 input_dict = qobject_to_qdict(input_obj);
4385 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4386 const char *arg_name = qdict_entry_key(ent);
4387 const QObject *arg_obj = qdict_entry_value(ent);
4389 if (!strcmp(arg_name, "execute")) {
4390 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4391 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4392 "string");
4393 return NULL;
4395 has_exec_key = 1;
4396 } else if (!strcmp(arg_name, "arguments")) {
4397 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4398 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4399 "object");
4400 return NULL;
4402 } else if (!strcmp(arg_name, "id")) {
4403 /* FIXME: check duplicated IDs for async commands */
4404 } else {
4405 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4406 return NULL;
4410 if (!has_exec_key) {
4411 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4412 return NULL;
4415 return input_dict;
4418 static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4420 QObject *ret_data = NULL;
4422 if (handler_is_async(cmd)) {
4423 qmp_async_info_handler(mon, cmd);
4424 if (monitor_has_error(mon)) {
4425 monitor_protocol_emitter(mon, NULL);
4427 } else {
4428 cmd->mhandler.info_new(mon, &ret_data);
4429 if (ret_data) {
4430 monitor_protocol_emitter(mon, ret_data);
4431 qobject_decref(ret_data);
4436 static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4437 const QDict *params)
4439 int ret;
4440 QObject *data = NULL;
4442 mon_print_count_init(mon);
4444 ret = cmd->mhandler.cmd_new(mon, params, &data);
4445 handler_audit(mon, cmd, ret);
4446 monitor_protocol_emitter(mon, data);
4447 qobject_decref(data);
4450 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4452 int err;
4453 QObject *obj;
4454 QDict *input, *args;
4455 const mon_cmd_t *cmd;
4456 Monitor *mon = cur_mon;
4457 const char *cmd_name, *query_cmd;
4459 query_cmd = NULL;
4460 args = input = NULL;
4462 obj = json_parser_parse(tokens, NULL);
4463 if (!obj) {
4464 // FIXME: should be triggered in json_parser_parse()
4465 qerror_report(QERR_JSON_PARSING);
4466 goto err_out;
4469 input = qmp_check_input_obj(obj);
4470 if (!input) {
4471 qobject_decref(obj);
4472 goto err_out;
4475 mon->mc->id = qdict_get(input, "id");
4476 qobject_incref(mon->mc->id);
4478 cmd_name = qdict_get_str(input, "execute");
4479 if (invalid_qmp_mode(mon, cmd_name)) {
4480 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4481 goto err_out;
4484 if (strstart(cmd_name, "query-", &query_cmd)) {
4485 cmd = qmp_find_query_cmd(query_cmd);
4486 } else {
4487 cmd = qmp_find_cmd(cmd_name);
4490 if (!cmd) {
4491 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4492 goto err_out;
4495 obj = qdict_get(input, "arguments");
4496 if (!obj) {
4497 args = qdict_new();
4498 } else {
4499 args = qobject_to_qdict(obj);
4500 QINCREF(args);
4503 err = qmp_check_client_args(cmd, args);
4504 if (err < 0) {
4505 goto err_out;
4508 if (query_cmd) {
4509 qmp_call_query_cmd(mon, cmd);
4510 } else if (handler_is_async(cmd)) {
4511 err = qmp_async_cmd_handler(mon, cmd, args);
4512 if (err) {
4513 /* emit the error response */
4514 goto err_out;
4516 } else {
4517 qmp_call_cmd(mon, cmd, args);
4520 goto out;
4522 err_out:
4523 monitor_protocol_emitter(mon, NULL);
4524 out:
4525 QDECREF(input);
4526 QDECREF(args);
4530 * monitor_control_read(): Read and handle QMP input
4532 static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4534 Monitor *old_mon = cur_mon;
4536 cur_mon = opaque;
4538 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
4540 cur_mon = old_mon;
4543 static void monitor_read(void *opaque, const uint8_t *buf, int size)
4545 Monitor *old_mon = cur_mon;
4546 int i;
4548 cur_mon = opaque;
4550 if (cur_mon->rs) {
4551 for (i = 0; i < size; i++)
4552 readline_handle_byte(cur_mon->rs, buf[i]);
4553 } else {
4554 if (size == 0 || buf[size - 1] != 0)
4555 monitor_printf(cur_mon, "corrupted command\n");
4556 else
4557 handle_user_command(cur_mon, (char *)buf);
4560 cur_mon = old_mon;
4563 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
4565 monitor_suspend(mon);
4566 handle_user_command(mon, cmdline);
4567 monitor_resume(mon);
4570 int monitor_suspend(Monitor *mon)
4572 if (!mon->rs)
4573 return -ENOTTY;
4574 mon->suspend_cnt++;
4575 return 0;
4578 void monitor_resume(Monitor *mon)
4580 if (!mon->rs)
4581 return;
4582 if (--mon->suspend_cnt == 0)
4583 readline_show_prompt(mon->rs);
4586 static QObject *get_qmp_greeting(void)
4588 QObject *ver;
4590 do_info_version(NULL, &ver);
4591 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4595 * monitor_control_event(): Print QMP gretting
4597 static void monitor_control_event(void *opaque, int event)
4599 QObject *data;
4600 Monitor *mon = opaque;
4602 switch (event) {
4603 case CHR_EVENT_OPENED:
4604 mon->mc->command_mode = 0;
4605 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4606 data = get_qmp_greeting();
4607 monitor_json_emitter(mon, data);
4608 qobject_decref(data);
4609 break;
4610 case CHR_EVENT_CLOSED:
4611 json_message_parser_destroy(&mon->mc->parser);
4612 break;
4616 static void monitor_event(void *opaque, int event)
4618 Monitor *mon = opaque;
4620 switch (event) {
4621 case CHR_EVENT_MUX_IN:
4622 mon->mux_out = 0;
4623 if (mon->reset_seen) {
4624 readline_restart(mon->rs);
4625 monitor_resume(mon);
4626 monitor_flush(mon);
4627 } else {
4628 mon->suspend_cnt = 0;
4630 break;
4632 case CHR_EVENT_MUX_OUT:
4633 if (mon->reset_seen) {
4634 if (mon->suspend_cnt == 0) {
4635 monitor_printf(mon, "\n");
4637 monitor_flush(mon);
4638 monitor_suspend(mon);
4639 } else {
4640 mon->suspend_cnt++;
4642 mon->mux_out = 1;
4643 break;
4645 case CHR_EVENT_OPENED:
4646 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4647 "information\n", QEMU_VERSION);
4648 if (!mon->mux_out) {
4649 readline_show_prompt(mon->rs);
4651 mon->reset_seen = 1;
4652 break;
4658 * Local variables:
4659 * c-indent-level: 4
4660 * c-basic-offset: 4
4661 * tab-width: 8
4662 * End:
4665 void monitor_init(CharDriverState *chr, int flags)
4667 static int is_first_init = 1;
4668 Monitor *mon;
4670 if (is_first_init) {
4671 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
4672 is_first_init = 0;
4675 mon = qemu_mallocz(sizeof(*mon));
4677 mon->chr = chr;
4678 mon->flags = flags;
4679 if (flags & MONITOR_USE_READLINE) {
4680 mon->rs = readline_init(mon, monitor_find_completion);
4681 monitor_read_command(mon, 0);
4684 if (monitor_ctrl_mode(mon)) {
4685 mon->mc = qemu_mallocz(sizeof(MonitorControl));
4686 /* Control mode requires special handlers */
4687 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4688 monitor_control_event, mon);
4689 } else {
4690 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4691 monitor_event, mon);
4694 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4695 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4696 default_mon = mon;
4699 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
4701 BlockDriverState *bs = opaque;
4702 int ret = 0;
4704 if (bdrv_set_key(bs, password) != 0) {
4705 monitor_printf(mon, "invalid password\n");
4706 ret = -EPERM;
4708 if (mon->password_completion_cb)
4709 mon->password_completion_cb(mon->password_opaque, ret);
4711 monitor_read_command(mon, 1);
4714 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4715 BlockDriverCompletionFunc *completion_cb,
4716 void *opaque)
4718 int err;
4720 if (!bdrv_key_required(bs)) {
4721 if (completion_cb)
4722 completion_cb(opaque, 0);
4723 return 0;
4726 if (monitor_ctrl_mode(mon)) {
4727 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
4728 return -1;
4731 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4732 bdrv_get_encrypted_filename(bs));
4734 mon->password_completion_cb = completion_cb;
4735 mon->password_opaque = opaque;
4737 err = monitor_read_password(mon, bdrv_password_cb, bs);
4739 if (err && completion_cb)
4740 completion_cb(opaque, err);
4742 return err;