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
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
43 #include "audio/audio.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
61 #include "trace/control.h"
62 #ifdef CONFIG_TRACE_SIMPLE
63 #include "trace/simple.h"
65 #include "ui/qemu-spice.h"
67 #include "qmp-commands.h"
70 /* for pic/irq_info */
71 #if defined(TARGET_SPARC)
74 #include "hw/lm32_pic.h"
77 //#define DEBUG_COMPLETION
83 * 'B' block device name
84 * 's' string (accept optional quote)
85 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
91 * 'l' target long (32 or 64 bit)
92 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
94 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
102 * '/' optional gdb-like print format (like "/10x")
104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
107 * user mode accepts "on" or "off"
108 * '-' optional parameter (eg. '-f')
112 typedef struct MonitorCompletionData MonitorCompletionData;
113 struct MonitorCompletionData {
115 void (*user_print)(Monitor *mon, const QObject *data);
118 typedef struct mon_cmd_t {
120 const char *args_type;
123 void (*user_print)(Monitor *mon, const QObject *data);
125 void (*info)(Monitor *mon);
126 void (*info_new)(Monitor *mon, QObject **ret_data);
127 int (*info_async)(Monitor *mon, MonitorCompletion *cb, void *opaque);
128 void (*cmd)(Monitor *mon, const QDict *qdict);
129 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
130 int (*cmd_async)(Monitor *mon, const QDict *params,
131 MonitorCompletion *cb, void *opaque);
137 /* file descriptors passed via SCM_RIGHTS */
138 typedef struct mon_fd_t mon_fd_t;
142 QLIST_ENTRY(mon_fd_t) next;
145 typedef struct MonitorControl {
147 JSONMessageParser parser;
152 CharDriverState *chr;
157 uint8_t outbuf[1024];
162 BlockDriverCompletionFunc *password_completion_cb;
163 void *password_opaque;
164 #ifdef CONFIG_DEBUG_MONITOR
168 QLIST_HEAD(,mon_fd_t) fds;
169 QLIST_ENTRY(Monitor) entry;
172 #ifdef CONFIG_DEBUG_MONITOR
173 #define MON_DEBUG(fmt, ...) do { \
174 fprintf(stderr, "Monitor: "); \
175 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
177 static inline void mon_print_count_inc(Monitor *mon)
179 mon->print_calls_nr++;
182 static inline void mon_print_count_init(Monitor *mon)
184 mon->print_calls_nr = 0;
187 static inline int mon_print_count_get(const Monitor *mon)
189 return mon->print_calls_nr;
192 #else /* !CONFIG_DEBUG_MONITOR */
193 #define MON_DEBUG(fmt, ...) do { } while (0)
194 static inline void mon_print_count_inc(Monitor *mon) { }
195 static inline void mon_print_count_init(Monitor *mon) { }
196 static inline int mon_print_count_get(const Monitor *mon) { return 0; }
197 #endif /* CONFIG_DEBUG_MONITOR */
199 /* QMP checker flags */
200 #define QMP_ACCEPT_UNKNOWNS 1
202 static QLIST_HEAD(mon_list, Monitor) mon_list;
204 static const mon_cmd_t mon_cmds[];
205 static const mon_cmd_t info_cmds[];
207 static const mon_cmd_t qmp_cmds[];
208 static const mon_cmd_t qmp_query_cmds[];
211 Monitor *default_mon;
213 static void monitor_command_cb(Monitor *mon, const char *cmdline,
216 static inline int qmp_cmd_mode(const Monitor *mon)
218 return (mon->mc ? mon->mc->command_mode : 0);
221 /* Return true if in control mode, false otherwise */
222 static inline int monitor_ctrl_mode(const Monitor *mon)
224 return (mon->flags & MONITOR_USE_CONTROL);
227 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228 int monitor_cur_is_qmp(void)
230 return cur_mon && monitor_ctrl_mode(cur_mon);
233 static void monitor_read_command(Monitor *mon, int show_prompt)
238 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
240 readline_show_prompt(mon->rs);
243 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
246 if (monitor_ctrl_mode(mon)) {
247 qerror_report(QERR_MISSING_PARAMETER, "password");
249 } else if (mon->rs) {
250 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
251 /* prompt is printed on return from the command handler */
254 monitor_printf(mon, "terminal does not support password prompting\n");
259 void monitor_flush(Monitor *mon)
261 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
262 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
263 mon->outbuf_index = 0;
267 /* flush at every end of line or if the buffer is full */
268 static void monitor_puts(Monitor *mon, const char *str)
277 mon->outbuf[mon->outbuf_index++] = '\r';
278 mon->outbuf[mon->outbuf_index++] = c;
279 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
285 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
292 mon_print_count_inc(mon);
294 if (monitor_ctrl_mode(mon)) {
298 vsnprintf(buf, sizeof(buf), fmt, ap);
299 monitor_puts(mon, buf);
302 void monitor_printf(Monitor *mon, const char *fmt, ...)
306 monitor_vprintf(mon, fmt, ap);
310 void monitor_print_filename(Monitor *mon, const char *filename)
314 for (i = 0; filename[i]; i++) {
315 switch (filename[i]) {
319 monitor_printf(mon, "\\%c", filename[i]);
322 monitor_printf(mon, "\\t");
325 monitor_printf(mon, "\\r");
328 monitor_printf(mon, "\\n");
331 monitor_printf(mon, "%c", filename[i]);
337 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
338 const char *fmt, ...)
342 monitor_vprintf((Monitor *)stream, fmt, ap);
347 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
349 static inline int handler_is_qobject(const mon_cmd_t *cmd)
351 return cmd->user_print != NULL;
354 static inline bool handler_is_async(const mon_cmd_t *cmd)
356 return cmd->flags & MONITOR_CMD_ASYNC;
359 static inline int monitor_has_error(const Monitor *mon)
361 return mon->error != NULL;
364 static void monitor_json_emitter(Monitor *mon, const QObject *data)
368 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
369 qobject_to_json(data);
370 assert(json != NULL);
372 qstring_append_chr(json, '\n');
373 monitor_puts(mon, qstring_get_str(json));
378 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
382 trace_monitor_protocol_emitter(mon);
386 if (!monitor_has_error(mon)) {
387 /* success response */
389 qobject_incref(data);
390 qdict_put_obj(qmp, "return", data);
392 /* return an empty QDict by default */
393 qdict_put(qmp, "return", qdict_new());
397 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
398 qdict_put(qmp, "error", mon->error->error);
399 QINCREF(mon->error->error);
405 qdict_put_obj(qmp, "id", mon->mc->id);
409 monitor_json_emitter(mon, QOBJECT(qmp));
413 static void timestamp_put(QDict *qdict)
419 err = qemu_gettimeofday(&tv);
423 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
424 "'microseconds': %" PRId64 " }",
425 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
426 qdict_put_obj(qdict, "timestamp", obj);
430 * monitor_protocol_event(): Generate a Monitor event
432 * Event-specific data can be emitted through the (optional) 'data' parameter.
434 void monitor_protocol_event(MonitorEvent event, QObject *data)
437 const char *event_name;
440 assert(event < QEVENT_MAX);
443 case QEVENT_SHUTDOWN:
444 event_name = "SHUTDOWN";
447 event_name = "RESET";
449 case QEVENT_POWERDOWN:
450 event_name = "POWERDOWN";
456 event_name = "RESUME";
458 case QEVENT_VNC_CONNECTED:
459 event_name = "VNC_CONNECTED";
461 case QEVENT_VNC_INITIALIZED:
462 event_name = "VNC_INITIALIZED";
464 case QEVENT_VNC_DISCONNECTED:
465 event_name = "VNC_DISCONNECTED";
467 case QEVENT_BLOCK_IO_ERROR:
468 event_name = "BLOCK_IO_ERROR";
470 case QEVENT_RTC_CHANGE:
471 event_name = "RTC_CHANGE";
473 case QEVENT_WATCHDOG:
474 event_name = "WATCHDOG";
476 case QEVENT_SPICE_CONNECTED:
477 event_name = "SPICE_CONNECTED";
479 case QEVENT_SPICE_INITIALIZED:
480 event_name = "SPICE_INITIALIZED";
482 case QEVENT_SPICE_DISCONNECTED:
483 event_name = "SPICE_DISCONNECTED";
492 qdict_put(qmp, "event", qstring_from_str(event_name));
494 qobject_incref(data);
495 qdict_put_obj(qmp, "data", data);
498 QLIST_FOREACH(mon, &mon_list, entry) {
499 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
500 monitor_json_emitter(mon, QOBJECT(qmp));
506 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
509 /* Will setup QMP capabilities in the future */
510 if (monitor_ctrl_mode(mon)) {
511 mon->mc->command_mode = 1;
517 static int mon_set_cpu(int cpu_index);
518 static void handle_user_command(Monitor *mon, const char *cmdline);
520 static int do_hmp_passthrough(Monitor *mon, const QDict *params,
524 Monitor *old_mon, hmp;
525 CharDriverState mchar;
527 memset(&hmp, 0, sizeof(hmp));
528 qemu_chr_init_mem(&mchar);
534 if (qdict_haskey(params, "cpu-index")) {
535 ret = mon_set_cpu(qdict_get_int(params, "cpu-index"));
538 qerror_report(QERR_INVALID_PARAMETER_VALUE, "cpu-index", "a CPU number");
543 handle_user_command(&hmp, qdict_get_str(params, "command-line"));
546 if (qemu_chr_mem_osize(hmp.chr) > 0) {
547 *ret_data = QOBJECT(qemu_chr_mem_to_qs(hmp.chr));
551 qemu_chr_close_mem(hmp.chr);
555 static int compare_cmd(const char *name, const char *list)
557 const char *p, *pstart;
565 p = pstart + strlen(pstart);
566 if ((p - pstart) == len && !memcmp(pstart, name, len))
575 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
576 const char *prefix, const char *name)
578 const mon_cmd_t *cmd;
580 for(cmd = cmds; cmd->name != NULL; cmd++) {
581 if (!name || !strcmp(name, cmd->name))
582 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
583 cmd->params, cmd->help);
587 static void help_cmd(Monitor *mon, const char *name)
589 if (name && !strcmp(name, "info")) {
590 help_cmd_dump(mon, info_cmds, "info ", NULL);
592 help_cmd_dump(mon, mon_cmds, "", name);
593 if (name && !strcmp(name, "log")) {
594 const CPULogItem *item;
595 monitor_printf(mon, "Log items (comma separated):\n");
596 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
597 for(item = cpu_log_items; item->mask != 0; item++) {
598 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
604 static void do_help_cmd(Monitor *mon, const QDict *qdict)
606 help_cmd(mon, qdict_get_try_str(qdict, "name"));
609 static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
611 const char *tp_name = qdict_get_str(qdict, "name");
612 bool new_state = qdict_get_bool(qdict, "option");
613 int ret = trace_event_set_state(tp_name, new_state);
616 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
620 #ifdef CONFIG_TRACE_SIMPLE
621 static void do_trace_file(Monitor *mon, const QDict *qdict)
623 const char *op = qdict_get_try_str(qdict, "op");
624 const char *arg = qdict_get_try_str(qdict, "arg");
627 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
628 } else if (!strcmp(op, "on")) {
629 st_set_trace_file_enabled(true);
630 } else if (!strcmp(op, "off")) {
631 st_set_trace_file_enabled(false);
632 } else if (!strcmp(op, "flush")) {
633 st_flush_trace_buffer();
634 } else if (!strcmp(op, "set")) {
636 st_set_trace_file(arg);
639 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
640 help_cmd(mon, "trace-file");
645 static void user_monitor_complete(void *opaque, QObject *ret_data)
647 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
650 data->user_print(data->mon, ret_data);
652 monitor_resume(data->mon);
656 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
658 monitor_protocol_emitter(opaque, ret_data);
661 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
664 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
667 static void qmp_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
669 cmd->mhandler.info_async(mon, qmp_monitor_complete, mon);
672 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
677 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
679 cb_data->user_print = cmd->user_print;
680 monitor_suspend(mon);
681 ret = cmd->mhandler.cmd_async(mon, params,
682 user_monitor_complete, cb_data);
689 static void user_async_info_handler(Monitor *mon, const mon_cmd_t *cmd)
693 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
695 cb_data->user_print = cmd->user_print;
696 monitor_suspend(mon);
697 ret = cmd->mhandler.info_async(mon, user_monitor_complete, cb_data);
704 static void do_info(Monitor *mon, const QDict *qdict)
706 const mon_cmd_t *cmd;
707 const char *item = qdict_get_try_str(qdict, "item");
713 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
714 if (compare_cmd(item, cmd->name))
718 if (cmd->name == NULL) {
722 if (handler_is_async(cmd)) {
723 user_async_info_handler(mon, cmd);
724 } else if (handler_is_qobject(cmd)) {
725 QObject *info_data = NULL;
727 cmd->mhandler.info_new(mon, &info_data);
729 cmd->user_print(mon, info_data);
730 qobject_decref(info_data);
733 cmd->mhandler.info(mon);
739 help_cmd(mon, "info");
742 static CommandInfoList *alloc_cmd_entry(const char *cmd_name)
744 CommandInfoList *info;
746 info = g_malloc0(sizeof(*info));
747 info->value = g_malloc0(sizeof(*info->value));
748 info->value->name = g_strdup(cmd_name);
753 CommandInfoList *qmp_query_commands(Error **errp)
755 CommandInfoList *info, *cmd_list = NULL;
756 const mon_cmd_t *cmd;
758 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
759 info = alloc_cmd_entry(cmd->name);
760 info->next = cmd_list;
764 for (cmd = qmp_query_cmds; cmd->name != NULL; cmd++) {
766 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
767 info = alloc_cmd_entry(buf);
768 info->next = cmd_list;
775 /* get the current CPU defined by the user */
776 static int mon_set_cpu(int cpu_index)
780 for(env = first_cpu; env != NULL; env = env->next_cpu) {
781 if (env->cpu_index == cpu_index) {
782 cur_mon->mon_cpu = env;
789 static CPUState *mon_get_cpu(void)
791 if (!cur_mon->mon_cpu) {
794 cpu_synchronize_state(cur_mon->mon_cpu);
795 return cur_mon->mon_cpu;
798 static void do_info_registers(Monitor *mon)
803 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
806 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
811 static void print_cpu_iter(QObject *obj, void *opaque)
815 Monitor *mon = opaque;
817 assert(qobject_type(obj) == QTYPE_QDICT);
818 cpu = qobject_to_qdict(obj);
820 if (qdict_get_bool(cpu, "current")) {
824 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
826 #if defined(TARGET_I386)
827 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
828 (target_ulong) qdict_get_int(cpu, "pc"));
829 #elif defined(TARGET_PPC)
830 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
831 (target_long) qdict_get_int(cpu, "nip"));
832 #elif defined(TARGET_SPARC)
833 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
834 (target_long) qdict_get_int(cpu, "pc"));
835 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
836 (target_long) qdict_get_int(cpu, "npc"));
837 #elif defined(TARGET_MIPS)
838 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
839 (target_long) qdict_get_int(cpu, "PC"));
842 if (qdict_get_bool(cpu, "halted")) {
843 monitor_printf(mon, " (halted)");
846 monitor_printf(mon, " thread_id=%" PRId64 " ",
847 qdict_get_int(cpu, "thread_id"));
849 monitor_printf(mon, "\n");
852 static void monitor_print_cpus(Monitor *mon, const QObject *data)
856 assert(qobject_type(data) == QTYPE_QLIST);
857 cpu_list = qobject_to_qlist(data);
858 qlist_iter(cpu_list, print_cpu_iter, mon);
861 static void do_info_cpus(Monitor *mon, QObject **ret_data)
866 cpu_list = qlist_new();
868 /* just to set the default cpu if not already done */
871 for(env = first_cpu; env != NULL; env = env->next_cpu) {
875 cpu_synchronize_state(env);
877 obj = qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
878 env->cpu_index, env == mon->mon_cpu,
881 cpu = qobject_to_qdict(obj);
883 #if defined(TARGET_I386)
884 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
885 #elif defined(TARGET_PPC)
886 qdict_put(cpu, "nip", qint_from_int(env->nip));
887 #elif defined(TARGET_SPARC)
888 qdict_put(cpu, "pc", qint_from_int(env->pc));
889 qdict_put(cpu, "npc", qint_from_int(env->npc));
890 #elif defined(TARGET_MIPS)
891 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
893 qdict_put(cpu, "thread_id", qint_from_int(env->thread_id));
895 qlist_append(cpu_list, cpu);
898 *ret_data = QOBJECT(cpu_list);
901 static int do_cpu_set(Monitor *mon, const QDict *qdict, QObject **ret_data)
903 int index = qdict_get_int(qdict, "index");
904 if (mon_set_cpu(index) < 0) {
905 qerror_report(QERR_INVALID_PARAMETER_VALUE, "index",
912 static void do_info_jit(Monitor *mon)
914 dump_exec_info((FILE *)mon, monitor_fprintf);
917 static void do_info_history(Monitor *mon)
926 str = readline_get_history(mon->rs, i);
929 monitor_printf(mon, "%d: '%s'\n", i, str);
934 #if defined(TARGET_PPC)
935 /* XXX: not implemented in other targets */
936 static void do_info_cpu_stats(Monitor *mon)
941 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
945 #if defined(CONFIG_TRACE_SIMPLE)
946 static void do_info_trace(Monitor *mon)
948 st_print_trace((FILE *)mon, &monitor_fprintf);
952 static void do_trace_print_events(Monitor *mon)
954 trace_print_events((FILE *)mon, &monitor_fprintf);
958 static int change_vnc_password(const char *password)
960 if (!password || !password[0]) {
961 if (vnc_display_disable_login(NULL)) {
962 qerror_report(QERR_SET_PASSWD_FAILED);
968 if (vnc_display_password(NULL, password) < 0) {
969 qerror_report(QERR_SET_PASSWD_FAILED);
976 static void change_vnc_password_cb(Monitor *mon, const char *password,
979 change_vnc_password(password);
980 monitor_read_command(mon, 1);
983 static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
985 if (strcmp(target, "passwd") == 0 ||
986 strcmp(target, "password") == 0) {
989 strncpy(password, arg, sizeof(password));
990 password[sizeof(password) - 1] = '\0';
991 return change_vnc_password(password);
993 return monitor_read_password(mon, change_vnc_password_cb, NULL);
996 if (vnc_display_open(NULL, target) < 0) {
997 qerror_report(QERR_VNC_SERVER_FAILED, target);
1005 static int do_change_vnc(Monitor *mon, const char *target, const char *arg)
1007 qerror_report(QERR_FEATURE_DISABLED, "vnc");
1013 * do_change(): Change a removable medium, or VNC configuration
1015 static int do_change(Monitor *mon, const QDict *qdict, QObject **ret_data)
1017 const char *device = qdict_get_str(qdict, "device");
1018 const char *target = qdict_get_str(qdict, "target");
1019 const char *arg = qdict_get_try_str(qdict, "arg");
1022 if (strcmp(device, "vnc") == 0) {
1023 ret = do_change_vnc(mon, target, arg);
1025 ret = do_change_block(mon, device, target, arg);
1031 static int set_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1033 const char *protocol = qdict_get_str(qdict, "protocol");
1034 const char *password = qdict_get_str(qdict, "password");
1035 const char *connected = qdict_get_try_str(qdict, "connected");
1036 int disconnect_if_connected = 0;
1037 int fail_if_connected = 0;
1041 if (strcmp(connected, "fail") == 0) {
1042 fail_if_connected = 1;
1043 } else if (strcmp(connected, "disconnect") == 0) {
1044 disconnect_if_connected = 1;
1045 } else if (strcmp(connected, "keep") == 0) {
1048 qerror_report(QERR_INVALID_PARAMETER, "connected");
1053 if (strcmp(protocol, "spice") == 0) {
1055 /* correct one? spice isn't a device ,,, */
1056 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1059 rc = qemu_spice_set_passwd(password, fail_if_connected,
1060 disconnect_if_connected);
1062 qerror_report(QERR_SET_PASSWD_FAILED);
1068 if (strcmp(protocol, "vnc") == 0) {
1069 if (fail_if_connected || disconnect_if_connected) {
1070 /* vnc supports "connected=keep" only */
1071 qerror_report(QERR_INVALID_PARAMETER, "connected");
1074 /* Note that setting an empty password will not disable login through
1075 * this interface. */
1076 return vnc_display_password(NULL, password);
1079 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1083 static int expire_password(Monitor *mon, const QDict *qdict, QObject **ret_data)
1085 const char *protocol = qdict_get_str(qdict, "protocol");
1086 const char *whenstr = qdict_get_str(qdict, "time");
1090 if (strcmp(whenstr, "now") == 0) {
1092 } else if (strcmp(whenstr, "never") == 0) {
1094 } else if (whenstr[0] == '+') {
1095 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
1097 when = strtoull(whenstr, NULL, 10);
1100 if (strcmp(protocol, "spice") == 0) {
1102 /* correct one? spice isn't a device ,,, */
1103 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1106 rc = qemu_spice_set_pw_expire(when);
1108 qerror_report(QERR_SET_PASSWD_FAILED);
1114 if (strcmp(protocol, "vnc") == 0) {
1115 return vnc_display_pw_expire(NULL, when);
1118 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1122 static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
1124 const char *protocol = qdict_get_str(qdict, "protocol");
1125 const char *fdname = qdict_get_str(qdict, "fdname");
1128 if (strcmp(protocol, "spice") == 0) {
1130 /* correct one? spice isn't a device ,,, */
1131 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1134 qerror_report(QERR_ADD_CLIENT_FAILED);
1137 } else if (strcmp(protocol, "vnc") == 0) {
1138 int fd = monitor_get_fd(mon, fdname);
1139 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
1140 vnc_display_add_client(NULL, fd, skipauth);
1143 } else if ((s = qemu_chr_find(protocol)) != NULL) {
1144 int fd = monitor_get_fd(mon, fdname);
1145 if (qemu_chr_add_client(s, fd) < 0) {
1146 qerror_report(QERR_ADD_CLIENT_FAILED);
1152 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1156 static int client_migrate_info(Monitor *mon, const QDict *qdict,
1157 MonitorCompletion cb, void *opaque)
1159 const char *protocol = qdict_get_str(qdict, "protocol");
1160 const char *hostname = qdict_get_str(qdict, "hostname");
1161 const char *subject = qdict_get_try_str(qdict, "cert-subject");
1162 int port = qdict_get_try_int(qdict, "port", -1);
1163 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
1166 if (strcmp(protocol, "spice") == 0) {
1168 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
1172 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
1175 qerror_report(QERR_UNDEFINED_ERROR);
1181 qerror_report(QERR_INVALID_PARAMETER, "protocol");
1185 static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
1187 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
1191 static void do_logfile(Monitor *mon, const QDict *qdict)
1193 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
1196 static void do_log(Monitor *mon, const QDict *qdict)
1199 const char *items = qdict_get_str(qdict, "items");
1201 if (!strcmp(items, "none")) {
1204 mask = cpu_str_to_log_mask(items);
1206 help_cmd(mon, "log");
1213 static void do_singlestep(Monitor *mon, const QDict *qdict)
1215 const char *option = qdict_get_try_str(qdict, "option");
1216 if (!option || !strcmp(option, "on")) {
1218 } else if (!strcmp(option, "off")) {
1221 monitor_printf(mon, "unexpected option %s\n", option);
1225 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
1227 struct bdrv_iterate_context {
1232 static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
1234 bdrv_iostatus_reset(bs);
1238 * do_cont(): Resume emulation.
1240 static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
1242 struct bdrv_iterate_context context = { mon, 0 };
1244 if (runstate_check(RUN_STATE_INMIGRATE)) {
1245 qerror_report(QERR_MIGRATION_EXPECTED);
1247 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
1248 runstate_check(RUN_STATE_SHUTDOWN)) {
1249 qerror_report(QERR_RESET_REQUIRED);
1253 bdrv_iterate(iostatus_bdrv_it, NULL);
1254 bdrv_iterate(encrypted_bdrv_it, &context);
1255 /* only resume the vm if all keys are set and valid */
1264 static void bdrv_key_cb(void *opaque, int err)
1266 Monitor *mon = opaque;
1268 /* another key was set successfully, retry to continue */
1270 do_cont(mon, NULL, NULL);
1273 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
1275 struct bdrv_iterate_context *context = opaque;
1277 if (!context->err && bdrv_key_required(bs)) {
1278 context->err = -EBUSY;
1279 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
1284 static void do_gdbserver(Monitor *mon, const QDict *qdict)
1286 const char *device = qdict_get_try_str(qdict, "device");
1288 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1289 if (gdbserver_start(device) < 0) {
1290 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1292 } else if (strcmp(device, "none") == 0) {
1293 monitor_printf(mon, "Disabled gdbserver\n");
1295 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1300 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
1302 const char *action = qdict_get_str(qdict, "action");
1303 if (select_watchdog_action(action) == -1) {
1304 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1308 static void monitor_printc(Monitor *mon, int c)
1310 monitor_printf(mon, "'");
1313 monitor_printf(mon, "\\'");
1316 monitor_printf(mon, "\\\\");
1319 monitor_printf(mon, "\\n");
1322 monitor_printf(mon, "\\r");
1325 if (c >= 32 && c <= 126) {
1326 monitor_printf(mon, "%c", c);
1328 monitor_printf(mon, "\\x%02x", c);
1332 monitor_printf(mon, "'");
1335 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1336 target_phys_addr_t addr, int is_physical)
1339 int l, line_size, i, max_digits, len;
1343 if (format == 'i') {
1346 env = mon_get_cpu();
1350 } else if (wsize == 4) {
1353 /* as default we use the current CS size */
1356 #ifdef TARGET_X86_64
1357 if ((env->efer & MSR_EFER_LMA) &&
1358 (env->segs[R_CS].flags & DESC_L_MASK))
1362 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1367 monitor_disas(mon, env, addr, count, is_physical, flags);
1371 len = wsize * count;
1380 max_digits = (wsize * 8 + 2) / 3;
1384 max_digits = (wsize * 8) / 4;
1388 max_digits = (wsize * 8 * 10 + 32) / 33;
1397 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1399 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1404 cpu_physical_memory_read(addr, buf, l);
1406 env = mon_get_cpu();
1407 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
1408 monitor_printf(mon, " Cannot access memory\n");
1417 v = ldub_raw(buf + i);
1420 v = lduw_raw(buf + i);
1423 v = (uint32_t)ldl_raw(buf + i);
1426 v = ldq_raw(buf + i);
1429 monitor_printf(mon, " ");
1432 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1435 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1438 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1441 monitor_printf(mon, "%*" PRId64, max_digits, v);
1444 monitor_printc(mon, v);
1449 monitor_printf(mon, "\n");
1455 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1457 int count = qdict_get_int(qdict, "count");
1458 int format = qdict_get_int(qdict, "format");
1459 int size = qdict_get_int(qdict, "size");
1460 target_long addr = qdict_get_int(qdict, "addr");
1462 memory_dump(mon, count, format, size, addr, 0);
1465 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1467 int count = qdict_get_int(qdict, "count");
1468 int format = qdict_get_int(qdict, "format");
1469 int size = qdict_get_int(qdict, "size");
1470 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1472 memory_dump(mon, count, format, size, addr, 1);
1475 static void do_print(Monitor *mon, const QDict *qdict)
1477 int format = qdict_get_int(qdict, "format");
1478 target_phys_addr_t val = qdict_get_int(qdict, "val");
1480 #if TARGET_PHYS_ADDR_BITS == 32
1483 monitor_printf(mon, "%#o", val);
1486 monitor_printf(mon, "%#x", val);
1489 monitor_printf(mon, "%u", val);
1493 monitor_printf(mon, "%d", val);
1496 monitor_printc(mon, val);
1502 monitor_printf(mon, "%#" PRIo64, val);
1505 monitor_printf(mon, "%#" PRIx64, val);
1508 monitor_printf(mon, "%" PRIu64, val);
1512 monitor_printf(mon, "%" PRId64, val);
1515 monitor_printc(mon, val);
1519 monitor_printf(mon, "\n");
1522 static int do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1525 uint32_t size = qdict_get_int(qdict, "size");
1526 const char *filename = qdict_get_str(qdict, "filename");
1527 target_long addr = qdict_get_int(qdict, "val");
1533 env = mon_get_cpu();
1535 f = fopen(filename, "wb");
1537 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1544 cpu_memory_rw_debug(env, addr, buf, l, 0);
1545 if (fwrite(buf, 1, l, f) != l) {
1546 monitor_printf(mon, "fwrite() error in do_memory_save\n");
1560 static int do_physical_memory_save(Monitor *mon, const QDict *qdict,
1566 uint32_t size = qdict_get_int(qdict, "size");
1567 const char *filename = qdict_get_str(qdict, "filename");
1568 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1571 f = fopen(filename, "wb");
1573 qerror_report(QERR_OPEN_FILE_FAILED, filename);
1580 cpu_physical_memory_read(addr, buf, l);
1581 if (fwrite(buf, 1, l, f) != l) {
1582 monitor_printf(mon, "fwrite() error in do_physical_memory_save\n");
1597 static void do_sum(Monitor *mon, const QDict *qdict)
1601 uint32_t start = qdict_get_int(qdict, "start");
1602 uint32_t size = qdict_get_int(qdict, "size");
1605 for(addr = start; addr < (start + size); addr++) {
1606 uint8_t val = ldub_phys(addr);
1607 /* BSD sum algorithm ('sum' Unix command) */
1608 sum = (sum >> 1) | (sum << 15);
1611 monitor_printf(mon, "%05d\n", sum);
1619 static const KeyDef key_defs[] = {
1621 { 0x36, "shift_r" },
1626 { 0xe4, "altgr_r" },
1646 { 0x0e, "backspace" },
1659 { 0x1a, "bracket_left" },
1660 { 0x1b, "bracket_right" },
1672 { 0x27, "semicolon" },
1673 { 0x28, "apostrophe" },
1674 { 0x29, "grave_accent" },
1676 { 0x2b, "backslash" },
1688 { 0x37, "asterisk" },
1691 { 0x3a, "caps_lock" },
1702 { 0x45, "num_lock" },
1703 { 0x46, "scroll_lock" },
1705 { 0xb5, "kp_divide" },
1706 { 0x37, "kp_multiply" },
1707 { 0x4a, "kp_subtract" },
1709 { 0x9c, "kp_enter" },
1710 { 0x53, "kp_decimal" },
1743 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1758 { 0xfe, "compose" },
1763 static int get_keycode(const char *key)
1769 for(p = key_defs; p->name != NULL; p++) {
1770 if (!strcmp(key, p->name))
1773 if (strstart(key, "0x", NULL)) {
1774 ret = strtoul(key, &endp, 0);
1775 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1781 #define MAX_KEYCODES 16
1782 static uint8_t keycodes[MAX_KEYCODES];
1783 static int nb_pending_keycodes;
1784 static QEMUTimer *key_timer;
1786 static void release_keys(void *opaque)
1790 while (nb_pending_keycodes > 0) {
1791 nb_pending_keycodes--;
1792 keycode = keycodes[nb_pending_keycodes];
1794 kbd_put_keycode(0xe0);
1795 kbd_put_keycode(keycode | 0x80);
1799 static void do_sendkey(Monitor *mon, const QDict *qdict)
1801 char keyname_buf[16];
1803 int keyname_len, keycode, i;
1804 const char *string = qdict_get_str(qdict, "string");
1805 int has_hold_time = qdict_haskey(qdict, "hold_time");
1806 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1808 if (nb_pending_keycodes > 0) {
1809 qemu_del_timer(key_timer);
1816 separator = strchr(string, '-');
1817 keyname_len = separator ? separator - string : strlen(string);
1818 if (keyname_len > 0) {
1819 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1820 if (keyname_len > sizeof(keyname_buf) - 1) {
1821 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1824 if (i == MAX_KEYCODES) {
1825 monitor_printf(mon, "too many keys\n");
1828 keyname_buf[keyname_len] = 0;
1829 keycode = get_keycode(keyname_buf);
1831 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1834 keycodes[i++] = keycode;
1838 string = separator + 1;
1840 nb_pending_keycodes = i;
1841 /* key down events */
1842 for (i = 0; i < nb_pending_keycodes; i++) {
1843 keycode = keycodes[i];
1845 kbd_put_keycode(0xe0);
1846 kbd_put_keycode(keycode & 0x7f);
1848 /* delayed key up events */
1849 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
1850 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1853 static int mouse_button_state;
1855 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1858 const char *dx_str = qdict_get_str(qdict, "dx_str");
1859 const char *dy_str = qdict_get_str(qdict, "dy_str");
1860 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1861 dx = strtol(dx_str, NULL, 0);
1862 dy = strtol(dy_str, NULL, 0);
1865 dz = strtol(dz_str, NULL, 0);
1866 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1869 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1871 int button_state = qdict_get_int(qdict, "button_state");
1872 mouse_button_state = button_state;
1873 kbd_mouse_event(0, 0, 0, mouse_button_state);
1876 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1878 int size = qdict_get_int(qdict, "size");
1879 int addr = qdict_get_int(qdict, "addr");
1880 int has_index = qdict_haskey(qdict, "index");
1885 int index = qdict_get_int(qdict, "index");
1886 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1894 val = cpu_inb(addr);
1898 val = cpu_inw(addr);
1902 val = cpu_inl(addr);
1906 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1907 suffix, addr, size * 2, val);
1910 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1912 int size = qdict_get_int(qdict, "size");
1913 int addr = qdict_get_int(qdict, "addr");
1914 int val = qdict_get_int(qdict, "val");
1916 addr &= IOPORTS_MASK;
1921 cpu_outb(addr, val);
1924 cpu_outw(addr, val);
1927 cpu_outl(addr, val);
1932 static void do_boot_set(Monitor *mon, const QDict *qdict)
1935 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1937 res = qemu_boot_set(bootdevice);
1939 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1940 } else if (res > 0) {
1941 monitor_printf(mon, "setting boot device list failed\n");
1943 monitor_printf(mon, "no function defined to set boot device list for "
1944 "this architecture\n");
1949 * do_system_powerdown(): Issue a machine powerdown
1951 static int do_system_powerdown(Monitor *mon, const QDict *qdict,
1954 qemu_system_powerdown_request();
1958 #if defined(TARGET_I386)
1959 static void print_pte(Monitor *mon, target_phys_addr_t addr,
1960 target_phys_addr_t pte,
1961 target_phys_addr_t mask)
1963 #ifdef TARGET_X86_64
1964 if (addr & (1ULL << 47)) {
1968 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1969 " %c%c%c%c%c%c%c%c%c\n",
1972 pte & PG_NX_MASK ? 'X' : '-',
1973 pte & PG_GLOBAL_MASK ? 'G' : '-',
1974 pte & PG_PSE_MASK ? 'P' : '-',
1975 pte & PG_DIRTY_MASK ? 'D' : '-',
1976 pte & PG_ACCESSED_MASK ? 'A' : '-',
1977 pte & PG_PCD_MASK ? 'C' : '-',
1978 pte & PG_PWT_MASK ? 'T' : '-',
1979 pte & PG_USER_MASK ? 'U' : '-',
1980 pte & PG_RW_MASK ? 'W' : '-');
1983 static void tlb_info_32(Monitor *mon, CPUState *env)
1985 unsigned int l1, l2;
1986 uint32_t pgd, pde, pte;
1988 pgd = env->cr[3] & ~0xfff;
1989 for(l1 = 0; l1 < 1024; l1++) {
1990 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1991 pde = le32_to_cpu(pde);
1992 if (pde & PG_PRESENT_MASK) {
1993 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1995 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1997 for(l2 = 0; l2 < 1024; l2++) {
1998 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1999 pte = le32_to_cpu(pte);
2000 if (pte & PG_PRESENT_MASK) {
2001 print_pte(mon, (l1 << 22) + (l2 << 12),
2011 static void tlb_info_pae32(Monitor *mon, CPUState *env)
2013 unsigned int l1, l2, l3;
2014 uint64_t pdpe, pde, pte;
2015 uint64_t pdp_addr, pd_addr, pt_addr;
2017 pdp_addr = env->cr[3] & ~0x1f;
2018 for (l1 = 0; l1 < 4; l1++) {
2019 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
2020 pdpe = le64_to_cpu(pdpe);
2021 if (pdpe & PG_PRESENT_MASK) {
2022 pd_addr = pdpe & 0x3fffffffff000ULL;
2023 for (l2 = 0; l2 < 512; l2++) {
2024 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
2025 pde = le64_to_cpu(pde);
2026 if (pde & PG_PRESENT_MASK) {
2027 if (pde & PG_PSE_MASK) {
2028 /* 2M pages with PAE, CR4.PSE is ignored */
2029 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
2030 ~((target_phys_addr_t)(1 << 20) - 1));
2032 pt_addr = pde & 0x3fffffffff000ULL;
2033 for (l3 = 0; l3 < 512; l3++) {
2034 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
2035 pte = le64_to_cpu(pte);
2036 if (pte & PG_PRESENT_MASK) {
2037 print_pte(mon, (l1 << 30 ) + (l2 << 21)
2040 ~(target_phys_addr_t)0xfff);
2050 #ifdef TARGET_X86_64
2051 static void tlb_info_64(Monitor *mon, CPUState *env)
2053 uint64_t l1, l2, l3, l4;
2054 uint64_t pml4e, pdpe, pde, pte;
2055 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
2057 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2058 for (l1 = 0; l1 < 512; l1++) {
2059 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
2060 pml4e = le64_to_cpu(pml4e);
2061 if (pml4e & PG_PRESENT_MASK) {
2062 pdp_addr = pml4e & 0x3fffffffff000ULL;
2063 for (l2 = 0; l2 < 512; l2++) {
2064 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
2065 pdpe = le64_to_cpu(pdpe);
2066 if (pdpe & PG_PRESENT_MASK) {
2067 if (pdpe & PG_PSE_MASK) {
2068 /* 1G pages, CR4.PSE is ignored */
2069 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
2070 0x3ffffc0000000ULL);
2072 pd_addr = pdpe & 0x3fffffffff000ULL;
2073 for (l3 = 0; l3 < 512; l3++) {
2074 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
2075 pde = le64_to_cpu(pde);
2076 if (pde & PG_PRESENT_MASK) {
2077 if (pde & PG_PSE_MASK) {
2078 /* 2M pages, CR4.PSE is ignored */
2079 print_pte(mon, (l1 << 39) + (l2 << 30) +
2081 0x3ffffffe00000ULL);
2083 pt_addr = pde & 0x3fffffffff000ULL;
2084 for (l4 = 0; l4 < 512; l4++) {
2085 cpu_physical_memory_read(pt_addr
2088 pte = le64_to_cpu(pte);
2089 if (pte & PG_PRESENT_MASK) {
2090 print_pte(mon, (l1 << 39) +
2092 (l3 << 21) + (l4 << 12),
2094 0x3fffffffff000ULL);
2108 static void tlb_info(Monitor *mon)
2112 env = mon_get_cpu();
2114 if (!(env->cr[0] & CR0_PG_MASK)) {
2115 monitor_printf(mon, "PG disabled\n");
2118 if (env->cr[4] & CR4_PAE_MASK) {
2119 #ifdef TARGET_X86_64
2120 if (env->hflags & HF_LMA_MASK) {
2121 tlb_info_64(mon, env);
2125 tlb_info_pae32(mon, env);
2128 tlb_info_32(mon, env);
2132 static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
2134 target_phys_addr_t end, int prot)
2137 prot1 = *plast_prot;
2138 if (prot != prot1) {
2139 if (*pstart != -1) {
2140 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
2141 TARGET_FMT_plx " %c%c%c\n",
2142 *pstart, end, end - *pstart,
2143 prot1 & PG_USER_MASK ? 'u' : '-',
2145 prot1 & PG_RW_MASK ? 'w' : '-');
2155 static void mem_info_32(Monitor *mon, CPUState *env)
2157 unsigned int l1, l2;
2158 int prot, last_prot;
2159 uint32_t pgd, pde, pte;
2160 target_phys_addr_t start, end;
2162 pgd = env->cr[3] & ~0xfff;
2165 for(l1 = 0; l1 < 1024; l1++) {
2166 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
2167 pde = le32_to_cpu(pde);
2169 if (pde & PG_PRESENT_MASK) {
2170 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
2171 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2172 mem_print(mon, &start, &last_prot, end, prot);
2174 for(l2 = 0; l2 < 1024; l2++) {
2175 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
2176 pte = le32_to_cpu(pte);
2177 end = (l1 << 22) + (l2 << 12);
2178 if (pte & PG_PRESENT_MASK) {
2180 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
2184 mem_print(mon, &start, &last_prot, end, prot);
2189 mem_print(mon, &start, &last_prot, end, prot);
2192 /* Flush last range */
2193 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
2196 static void mem_info_pae32(Monitor *mon, CPUState *env)
2198 unsigned int l1, l2, l3;
2199 int prot, last_prot;
2200 uint64_t pdpe, pde, pte;
2201 uint64_t pdp_addr, pd_addr, pt_addr;
2202 target_phys_addr_t start, end;
2204 pdp_addr = env->cr[3] & ~0x1f;
2207 for (l1 = 0; l1 < 4; l1++) {
2208 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
2209 pdpe = le64_to_cpu(pdpe);
2211 if (pdpe & PG_PRESENT_MASK) {
2212 pd_addr = pdpe & 0x3fffffffff000ULL;
2213 for (l2 = 0; l2 < 512; l2++) {
2214 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
2215 pde = le64_to_cpu(pde);
2216 end = (l1 << 30) + (l2 << 21);
2217 if (pde & PG_PRESENT_MASK) {
2218 if (pde & PG_PSE_MASK) {
2219 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2221 mem_print(mon, &start, &last_prot, end, prot);
2223 pt_addr = pde & 0x3fffffffff000ULL;
2224 for (l3 = 0; l3 < 512; l3++) {
2225 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
2226 pte = le64_to_cpu(pte);
2227 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
2228 if (pte & PG_PRESENT_MASK) {
2229 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
2234 mem_print(mon, &start, &last_prot, end, prot);
2239 mem_print(mon, &start, &last_prot, end, prot);
2244 mem_print(mon, &start, &last_prot, end, prot);
2247 /* Flush last range */
2248 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
2252 #ifdef TARGET_X86_64
2253 static void mem_info_64(Monitor *mon, CPUState *env)
2255 int prot, last_prot;
2256 uint64_t l1, l2, l3, l4;
2257 uint64_t pml4e, pdpe, pde, pte;
2258 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
2260 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
2263 for (l1 = 0; l1 < 512; l1++) {
2264 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
2265 pml4e = le64_to_cpu(pml4e);
2267 if (pml4e & PG_PRESENT_MASK) {
2268 pdp_addr = pml4e & 0x3fffffffff000ULL;
2269 for (l2 = 0; l2 < 512; l2++) {
2270 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
2271 pdpe = le64_to_cpu(pdpe);
2272 end = (l1 << 39) + (l2 << 30);
2273 if (pdpe & PG_PRESENT_MASK) {
2274 if (pdpe & PG_PSE_MASK) {
2275 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
2278 mem_print(mon, &start, &last_prot, end, prot);
2280 pd_addr = pdpe & 0x3fffffffff000ULL;
2281 for (l3 = 0; l3 < 512; l3++) {
2282 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
2283 pde = le64_to_cpu(pde);
2284 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
2285 if (pde & PG_PRESENT_MASK) {
2286 if (pde & PG_PSE_MASK) {
2287 prot = pde & (PG_USER_MASK | PG_RW_MASK |
2289 prot &= pml4e & pdpe;
2290 mem_print(mon, &start, &last_prot, end, prot);
2292 pt_addr = pde & 0x3fffffffff000ULL;
2293 for (l4 = 0; l4 < 512; l4++) {
2294 cpu_physical_memory_read(pt_addr
2297 pte = le64_to_cpu(pte);
2298 end = (l1 << 39) + (l2 << 30) +
2299 (l3 << 21) + (l4 << 12);
2300 if (pte & PG_PRESENT_MASK) {
2301 prot = pte & (PG_USER_MASK | PG_RW_MASK |
2303 prot &= pml4e & pdpe & pde;
2307 mem_print(mon, &start, &last_prot, end, prot);
2312 mem_print(mon, &start, &last_prot, end, prot);
2318 mem_print(mon, &start, &last_prot, end, prot);
2323 mem_print(mon, &start, &last_prot, end, prot);
2326 /* Flush last range */
2327 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
2331 static void mem_info(Monitor *mon)
2335 env = mon_get_cpu();
2337 if (!(env->cr[0] & CR0_PG_MASK)) {
2338 monitor_printf(mon, "PG disabled\n");
2341 if (env->cr[4] & CR4_PAE_MASK) {
2342 #ifdef TARGET_X86_64
2343 if (env->hflags & HF_LMA_MASK) {
2344 mem_info_64(mon, env);
2348 mem_info_pae32(mon, env);
2351 mem_info_32(mon, env);
2356 #if defined(TARGET_SH4)
2358 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
2360 monitor_printf(mon, " tlb%i:\t"
2361 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2362 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2363 "dirty=%hhu writethrough=%hhu\n",
2365 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
2366 tlb->v, tlb->sh, tlb->c, tlb->pr,
2370 static void tlb_info(Monitor *mon)
2372 CPUState *env = mon_get_cpu();
2375 monitor_printf (mon, "ITLB:\n");
2376 for (i = 0 ; i < ITLB_SIZE ; i++)
2377 print_tlb (mon, i, &env->itlb[i]);
2378 monitor_printf (mon, "UTLB:\n");
2379 for (i = 0 ; i < UTLB_SIZE ; i++)
2380 print_tlb (mon, i, &env->utlb[i]);
2385 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
2386 static void tlb_info(Monitor *mon)
2388 CPUState *env1 = mon_get_cpu();
2390 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
2394 static void do_info_mtree(Monitor *mon)
2396 mtree_info((fprintf_function)monitor_printf, mon);
2399 static void do_info_numa(Monitor *mon)
2404 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
2405 for (i = 0; i < nb_numa_nodes; i++) {
2406 monitor_printf(mon, "node %d cpus:", i);
2407 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2408 if (env->numa_node == i) {
2409 monitor_printf(mon, " %d", env->cpu_index);
2412 monitor_printf(mon, "\n");
2413 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
2418 #ifdef CONFIG_PROFILER
2423 static void do_info_profile(Monitor *mon)
2429 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
2430 dev_time, dev_time / (double)get_ticks_per_sec());
2431 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
2432 qemu_time, qemu_time / (double)get_ticks_per_sec());
2437 static void do_info_profile(Monitor *mon)
2439 monitor_printf(mon, "Internal profiler not compiled\n");
2443 /* Capture support */
2444 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
2446 static void do_info_capture(Monitor *mon)
2451 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2452 monitor_printf(mon, "[%d]: ", i);
2453 s->ops.info (s->opaque);
2458 static void do_stop_capture(Monitor *mon, const QDict *qdict)
2461 int n = qdict_get_int(qdict, "n");
2464 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2466 s->ops.destroy (s->opaque);
2467 QLIST_REMOVE (s, entries);
2474 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2476 const char *path = qdict_get_str(qdict, "path");
2477 int has_freq = qdict_haskey(qdict, "freq");
2478 int freq = qdict_get_try_int(qdict, "freq", -1);
2479 int has_bits = qdict_haskey(qdict, "bits");
2480 int bits = qdict_get_try_int(qdict, "bits", -1);
2481 int has_channels = qdict_haskey(qdict, "nchannels");
2482 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2485 s = g_malloc0 (sizeof (*s));
2487 freq = has_freq ? freq : 44100;
2488 bits = has_bits ? bits : 16;
2489 nchannels = has_channels ? nchannels : 2;
2491 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2492 monitor_printf(mon, "Failed to add wave capture\n");
2496 QLIST_INSERT_HEAD (&capture_head, s, entries);
2500 #if defined(TARGET_I386)
2501 static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
2505 for (env = first_cpu; env != NULL; env = env->next_cpu) {
2506 cpu_interrupt(env, CPU_INTERRUPT_NMI);
2512 static int do_inject_nmi(Monitor *mon, const QDict *qdict, QObject **ret_data)
2514 qerror_report(QERR_UNSUPPORTED);
2519 static qemu_acl *find_acl(Monitor *mon, const char *name)
2521 qemu_acl *acl = qemu_acl_find(name);
2524 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2529 static void do_acl_show(Monitor *mon, const QDict *qdict)
2531 const char *aclname = qdict_get_str(qdict, "aclname");
2532 qemu_acl *acl = find_acl(mon, aclname);
2533 qemu_acl_entry *entry;
2537 monitor_printf(mon, "policy: %s\n",
2538 acl->defaultDeny ? "deny" : "allow");
2539 QTAILQ_FOREACH(entry, &acl->entries, next) {
2541 monitor_printf(mon, "%d: %s %s\n", i,
2542 entry->deny ? "deny" : "allow", entry->match);
2547 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2549 const char *aclname = qdict_get_str(qdict, "aclname");
2550 qemu_acl *acl = find_acl(mon, aclname);
2553 qemu_acl_reset(acl);
2554 monitor_printf(mon, "acl: removed all rules\n");
2558 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2560 const char *aclname = qdict_get_str(qdict, "aclname");
2561 const char *policy = qdict_get_str(qdict, "policy");
2562 qemu_acl *acl = find_acl(mon, aclname);
2565 if (strcmp(policy, "allow") == 0) {
2566 acl->defaultDeny = 0;
2567 monitor_printf(mon, "acl: policy set to 'allow'\n");
2568 } else if (strcmp(policy, "deny") == 0) {
2569 acl->defaultDeny = 1;
2570 monitor_printf(mon, "acl: policy set to 'deny'\n");
2572 monitor_printf(mon, "acl: unknown policy '%s', "
2573 "expected 'deny' or 'allow'\n", policy);
2578 static void do_acl_add(Monitor *mon, const QDict *qdict)
2580 const char *aclname = qdict_get_str(qdict, "aclname");
2581 const char *match = qdict_get_str(qdict, "match");
2582 const char *policy = qdict_get_str(qdict, "policy");
2583 int has_index = qdict_haskey(qdict, "index");
2584 int index = qdict_get_try_int(qdict, "index", -1);
2585 qemu_acl *acl = find_acl(mon, aclname);
2589 if (strcmp(policy, "allow") == 0) {
2591 } else if (strcmp(policy, "deny") == 0) {
2594 monitor_printf(mon, "acl: unknown policy '%s', "
2595 "expected 'deny' or 'allow'\n", policy);
2599 ret = qemu_acl_insert(acl, deny, match, index);
2601 ret = qemu_acl_append(acl, deny, match);
2603 monitor_printf(mon, "acl: unable to add acl entry\n");
2605 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2609 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2611 const char *aclname = qdict_get_str(qdict, "aclname");
2612 const char *match = qdict_get_str(qdict, "match");
2613 qemu_acl *acl = find_acl(mon, aclname);
2617 ret = qemu_acl_remove(acl, match);
2619 monitor_printf(mon, "acl: no matching acl entry\n");
2621 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2625 #if defined(TARGET_I386)
2626 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2629 int cpu_index = qdict_get_int(qdict, "cpu_index");
2630 int bank = qdict_get_int(qdict, "bank");
2631 uint64_t status = qdict_get_int(qdict, "status");
2632 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2633 uint64_t addr = qdict_get_int(qdict, "addr");
2634 uint64_t misc = qdict_get_int(qdict, "misc");
2635 int flags = MCE_INJECT_UNCOND_AO;
2637 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2638 flags |= MCE_INJECT_BROADCAST;
2640 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
2641 if (cenv->cpu_index == cpu_index) {
2642 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
2650 static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2652 const char *fdname = qdict_get_str(qdict, "fdname");
2656 fd = qemu_chr_fe_get_msgfd(mon->chr);
2658 qerror_report(QERR_FD_NOT_SUPPLIED);
2662 if (qemu_isdigit(fdname[0])) {
2663 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2664 "a name not starting with a digit");
2668 QLIST_FOREACH(monfd, &mon->fds, next) {
2669 if (strcmp(monfd->name, fdname) != 0) {
2678 monfd = g_malloc0(sizeof(mon_fd_t));
2679 monfd->name = g_strdup(fdname);
2682 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
2686 static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2688 const char *fdname = qdict_get_str(qdict, "fdname");
2691 QLIST_FOREACH(monfd, &mon->fds, next) {
2692 if (strcmp(monfd->name, fdname) != 0) {
2696 QLIST_REMOVE(monfd, next);
2698 g_free(monfd->name);
2703 qerror_report(QERR_FD_NOT_FOUND, fdname);
2707 static void do_loadvm(Monitor *mon, const QDict *qdict)
2709 int saved_vm_running = runstate_is_running();
2710 const char *name = qdict_get_str(qdict, "name");
2712 vm_stop(RUN_STATE_RESTORE_VM);
2714 if (load_vmstate(name) == 0 && saved_vm_running) {
2719 int monitor_get_fd(Monitor *mon, const char *fdname)
2723 QLIST_FOREACH(monfd, &mon->fds, next) {
2726 if (strcmp(monfd->name, fdname) != 0) {
2732 /* caller takes ownership of fd */
2733 QLIST_REMOVE(monfd, next);
2734 g_free(monfd->name);
2743 static const mon_cmd_t mon_cmds[] = {
2744 #include "hmp-commands.h"
2748 /* Please update hmp-commands.hx when adding or changing commands */
2749 static const mon_cmd_t info_cmds[] = {
2754 .help = "show the version of QEMU",
2755 .mhandler.info = hmp_info_version,
2761 .help = "show the network state",
2762 .mhandler.info = do_info_network,
2768 .help = "show the character devices",
2769 .mhandler.info = hmp_info_chardev,
2775 .help = "show the block devices",
2776 .user_print = bdrv_info_print,
2777 .mhandler.info_new = bdrv_info,
2780 .name = "blockstats",
2783 .help = "show block device statistics",
2784 .user_print = bdrv_stats_print,
2785 .mhandler.info_new = bdrv_info_stats,
2788 .name = "registers",
2791 .help = "show the cpu registers",
2792 .mhandler.info = do_info_registers,
2798 .help = "show infos for each CPU",
2799 .user_print = monitor_print_cpus,
2800 .mhandler.info_new = do_info_cpus,
2806 .help = "show the command line history",
2807 .mhandler.info = do_info_history,
2809 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2810 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2815 .help = "show the interrupts statistics (if available)",
2817 .mhandler.info = sun4m_irq_info,
2818 #elif defined(TARGET_LM32)
2819 .mhandler.info = lm32_irq_info,
2821 .mhandler.info = irq_info,
2828 .help = "show i8259 (PIC) state",
2830 .mhandler.info = sun4m_pic_info,
2831 #elif defined(TARGET_LM32)
2832 .mhandler.info = lm32_do_pic_info,
2834 .mhandler.info = pic_info,
2842 .help = "show PCI info",
2843 .user_print = do_pci_info_print,
2844 .mhandler.info_new = do_pci_info,
2846 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2852 .help = "show virtual to physical memory mappings",
2853 .mhandler.info = tlb_info,
2856 #if defined(TARGET_I386)
2861 .help = "show the active virtual memory mappings",
2862 .mhandler.info = mem_info,
2869 .help = "show memory tree",
2870 .mhandler.info = do_info_mtree,
2876 .help = "show dynamic compiler info",
2877 .mhandler.info = do_info_jit,
2883 .help = "show KVM information",
2884 .mhandler.info = hmp_info_kvm,
2890 .help = "show NUMA information",
2891 .mhandler.info = do_info_numa,
2897 .help = "show guest USB devices",
2898 .mhandler.info = usb_info,
2904 .help = "show host USB devices",
2905 .mhandler.info = usb_host_info,
2911 .help = "show profiling information",
2912 .mhandler.info = do_info_profile,
2918 .help = "show capture information",
2919 .mhandler.info = do_info_capture,
2922 .name = "snapshots",
2925 .help = "show the currently saved VM snapshots",
2926 .mhandler.info = do_info_snapshots,
2932 .help = "show the current VM status (running|paused)",
2933 .mhandler.info = hmp_info_status,
2939 .help = "show guest PCMCIA status",
2940 .mhandler.info = pcmcia_info,
2946 .help = "show which guest mouse is receiving events",
2947 .user_print = do_info_mice_print,
2948 .mhandler.info_new = do_info_mice,
2954 .help = "show the vnc server status",
2955 .user_print = do_info_vnc_print,
2956 .mhandler.info_new = do_info_vnc,
2958 #if defined(CONFIG_SPICE)
2963 .help = "show the spice server status",
2964 .user_print = do_info_spice_print,
2965 .mhandler.info_new = do_info_spice,
2972 .help = "show the current VM name",
2973 .mhandler.info = hmp_info_name,
2979 .help = "show the current VM UUID",
2980 .mhandler.info = hmp_info_uuid,
2982 #if defined(TARGET_PPC)
2987 .help = "show CPU statistics",
2988 .mhandler.info = do_info_cpu_stats,
2991 #if defined(CONFIG_SLIRP)
2996 .help = "show user network stack connection states",
2997 .mhandler.info = do_info_usernet,
3004 .help = "show migration status",
3005 .user_print = do_info_migrate_print,
3006 .mhandler.info_new = do_info_migrate,
3012 .help = "show balloon information",
3013 .user_print = monitor_print_balloon,
3014 .mhandler.info_async = do_info_balloon,
3015 .flags = MONITOR_CMD_ASYNC,
3021 .help = "show device tree",
3022 .mhandler.info = do_info_qtree,
3028 .help = "show qdev device model list",
3029 .mhandler.info = do_info_qdm,
3035 .help = "show roms",
3036 .mhandler.info = do_info_roms,
3038 #if defined(CONFIG_TRACE_SIMPLE)
3043 .help = "show current contents of trace buffer",
3044 .mhandler.info = do_info_trace,
3048 .name = "trace-events",
3051 .help = "show available trace-events & their state",
3052 .mhandler.info = do_trace_print_events,
3059 static const mon_cmd_t qmp_cmds[] = {
3060 #include "qmp-commands-old.h"
3064 static const mon_cmd_t qmp_query_cmds[] = {
3069 .help = "show the block devices",
3070 .user_print = bdrv_info_print,
3071 .mhandler.info_new = bdrv_info,
3074 .name = "blockstats",
3077 .help = "show block device statistics",
3078 .user_print = bdrv_stats_print,
3079 .mhandler.info_new = bdrv_info_stats,
3085 .help = "show infos for each CPU",
3086 .user_print = monitor_print_cpus,
3087 .mhandler.info_new = do_info_cpus,
3093 .help = "show PCI info",
3094 .user_print = do_pci_info_print,
3095 .mhandler.info_new = do_pci_info,
3101 .help = "show which guest mouse is receiving events",
3102 .user_print = do_info_mice_print,
3103 .mhandler.info_new = do_info_mice,
3109 .help = "show the vnc server status",
3110 .user_print = do_info_vnc_print,
3111 .mhandler.info_new = do_info_vnc,
3113 #if defined(CONFIG_SPICE)
3118 .help = "show the spice server status",
3119 .user_print = do_info_spice_print,
3120 .mhandler.info_new = do_info_spice,
3127 .help = "show migration status",
3128 .user_print = do_info_migrate_print,
3129 .mhandler.info_new = do_info_migrate,
3135 .help = "show balloon information",
3136 .user_print = monitor_print_balloon,
3137 .mhandler.info_async = do_info_balloon,
3138 .flags = MONITOR_CMD_ASYNC,
3143 /*******************************************************************/
3145 static const char *pch;
3146 static jmp_buf expr_env;
3151 typedef struct MonitorDef {
3154 target_long (*get_value)(const struct MonitorDef *md, int val);
3158 #if defined(TARGET_I386)
3159 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
3161 CPUState *env = mon_get_cpu();
3162 return env->eip + env->segs[R_CS].base;
3166 #if defined(TARGET_PPC)
3167 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
3169 CPUState *env = mon_get_cpu();
3174 for (i = 0; i < 8; i++)
3175 u |= env->crf[i] << (32 - (4 * i));
3180 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
3182 CPUState *env = mon_get_cpu();
3186 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
3188 CPUState *env = mon_get_cpu();
3192 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
3194 CPUState *env = mon_get_cpu();
3195 return cpu_ppc_load_decr(env);
3198 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
3200 CPUState *env = mon_get_cpu();
3201 return cpu_ppc_load_tbu(env);
3204 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
3206 CPUState *env = mon_get_cpu();
3207 return cpu_ppc_load_tbl(env);
3211 #if defined(TARGET_SPARC)
3212 #ifndef TARGET_SPARC64
3213 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
3215 CPUState *env = mon_get_cpu();
3217 return cpu_get_psr(env);
3221 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
3223 CPUState *env = mon_get_cpu();
3224 return env->regwptr[val];
3228 static const MonitorDef monitor_defs[] = {
3231 #define SEG(name, seg) \
3232 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3233 { name ".base", offsetof(CPUState, segs[seg].base) },\
3234 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3236 { "eax", offsetof(CPUState, regs[0]) },
3237 { "ecx", offsetof(CPUState, regs[1]) },
3238 { "edx", offsetof(CPUState, regs[2]) },
3239 { "ebx", offsetof(CPUState, regs[3]) },
3240 { "esp|sp", offsetof(CPUState, regs[4]) },
3241 { "ebp|fp", offsetof(CPUState, regs[5]) },
3242 { "esi", offsetof(CPUState, regs[6]) },
3243 { "edi", offsetof(CPUState, regs[7]) },
3244 #ifdef TARGET_X86_64
3245 { "r8", offsetof(CPUState, regs[8]) },
3246 { "r9", offsetof(CPUState, regs[9]) },
3247 { "r10", offsetof(CPUState, regs[10]) },
3248 { "r11", offsetof(CPUState, regs[11]) },
3249 { "r12", offsetof(CPUState, regs[12]) },
3250 { "r13", offsetof(CPUState, regs[13]) },
3251 { "r14", offsetof(CPUState, regs[14]) },
3252 { "r15", offsetof(CPUState, regs[15]) },
3254 { "eflags", offsetof(CPUState, eflags) },
3255 { "eip", offsetof(CPUState, eip) },
3262 { "pc", 0, monitor_get_pc, },
3263 #elif defined(TARGET_PPC)
3264 /* General purpose registers */
3265 { "r0", offsetof(CPUState, gpr[0]) },
3266 { "r1", offsetof(CPUState, gpr[1]) },
3267 { "r2", offsetof(CPUState, gpr[2]) },
3268 { "r3", offsetof(CPUState, gpr[3]) },
3269 { "r4", offsetof(CPUState, gpr[4]) },
3270 { "r5", offsetof(CPUState, gpr[5]) },
3271 { "r6", offsetof(CPUState, gpr[6]) },
3272 { "r7", offsetof(CPUState, gpr[7]) },
3273 { "r8", offsetof(CPUState, gpr[8]) },
3274 { "r9", offsetof(CPUState, gpr[9]) },
3275 { "r10", offsetof(CPUState, gpr[10]) },
3276 { "r11", offsetof(CPUState, gpr[11]) },
3277 { "r12", offsetof(CPUState, gpr[12]) },
3278 { "r13", offsetof(CPUState, gpr[13]) },
3279 { "r14", offsetof(CPUState, gpr[14]) },
3280 { "r15", offsetof(CPUState, gpr[15]) },
3281 { "r16", offsetof(CPUState, gpr[16]) },
3282 { "r17", offsetof(CPUState, gpr[17]) },
3283 { "r18", offsetof(CPUState, gpr[18]) },
3284 { "r19", offsetof(CPUState, gpr[19]) },
3285 { "r20", offsetof(CPUState, gpr[20]) },
3286 { "r21", offsetof(CPUState, gpr[21]) },
3287 { "r22", offsetof(CPUState, gpr[22]) },
3288 { "r23", offsetof(CPUState, gpr[23]) },
3289 { "r24", offsetof(CPUState, gpr[24]) },
3290 { "r25", offsetof(CPUState, gpr[25]) },
3291 { "r26", offsetof(CPUState, gpr[26]) },
3292 { "r27", offsetof(CPUState, gpr[27]) },
3293 { "r28", offsetof(CPUState, gpr[28]) },
3294 { "r29", offsetof(CPUState, gpr[29]) },
3295 { "r30", offsetof(CPUState, gpr[30]) },
3296 { "r31", offsetof(CPUState, gpr[31]) },
3297 /* Floating point registers */
3298 { "f0", offsetof(CPUState, fpr[0]) },
3299 { "f1", offsetof(CPUState, fpr[1]) },
3300 { "f2", offsetof(CPUState, fpr[2]) },
3301 { "f3", offsetof(CPUState, fpr[3]) },
3302 { "f4", offsetof(CPUState, fpr[4]) },
3303 { "f5", offsetof(CPUState, fpr[5]) },
3304 { "f6", offsetof(CPUState, fpr[6]) },
3305 { "f7", offsetof(CPUState, fpr[7]) },
3306 { "f8", offsetof(CPUState, fpr[8]) },
3307 { "f9", offsetof(CPUState, fpr[9]) },
3308 { "f10", offsetof(CPUState, fpr[10]) },
3309 { "f11", offsetof(CPUState, fpr[11]) },
3310 { "f12", offsetof(CPUState, fpr[12]) },
3311 { "f13", offsetof(CPUState, fpr[13]) },
3312 { "f14", offsetof(CPUState, fpr[14]) },
3313 { "f15", offsetof(CPUState, fpr[15]) },
3314 { "f16", offsetof(CPUState, fpr[16]) },
3315 { "f17", offsetof(CPUState, fpr[17]) },
3316 { "f18", offsetof(CPUState, fpr[18]) },
3317 { "f19", offsetof(CPUState, fpr[19]) },
3318 { "f20", offsetof(CPUState, fpr[20]) },
3319 { "f21", offsetof(CPUState, fpr[21]) },
3320 { "f22", offsetof(CPUState, fpr[22]) },
3321 { "f23", offsetof(CPUState, fpr[23]) },
3322 { "f24", offsetof(CPUState, fpr[24]) },
3323 { "f25", offsetof(CPUState, fpr[25]) },
3324 { "f26", offsetof(CPUState, fpr[26]) },
3325 { "f27", offsetof(CPUState, fpr[27]) },
3326 { "f28", offsetof(CPUState, fpr[28]) },
3327 { "f29", offsetof(CPUState, fpr[29]) },
3328 { "f30", offsetof(CPUState, fpr[30]) },
3329 { "f31", offsetof(CPUState, fpr[31]) },
3330 { "fpscr", offsetof(CPUState, fpscr) },
3331 /* Next instruction pointer */
3332 { "nip|pc", offsetof(CPUState, nip) },
3333 { "lr", offsetof(CPUState, lr) },
3334 { "ctr", offsetof(CPUState, ctr) },
3335 { "decr", 0, &monitor_get_decr, },
3336 { "ccr", 0, &monitor_get_ccr, },
3337 /* Machine state register */
3338 { "msr", 0, &monitor_get_msr, },
3339 { "xer", 0, &monitor_get_xer, },
3340 { "tbu", 0, &monitor_get_tbu, },
3341 { "tbl", 0, &monitor_get_tbl, },
3342 #if defined(TARGET_PPC64)
3343 /* Address space register */
3344 { "asr", offsetof(CPUState, asr) },
3346 /* Segment registers */
3347 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
3348 { "sr0", offsetof(CPUState, sr[0]) },
3349 { "sr1", offsetof(CPUState, sr[1]) },
3350 { "sr2", offsetof(CPUState, sr[2]) },
3351 { "sr3", offsetof(CPUState, sr[3]) },
3352 { "sr4", offsetof(CPUState, sr[4]) },
3353 { "sr5", offsetof(CPUState, sr[5]) },
3354 { "sr6", offsetof(CPUState, sr[6]) },
3355 { "sr7", offsetof(CPUState, sr[7]) },
3356 { "sr8", offsetof(CPUState, sr[8]) },
3357 { "sr9", offsetof(CPUState, sr[9]) },
3358 { "sr10", offsetof(CPUState, sr[10]) },
3359 { "sr11", offsetof(CPUState, sr[11]) },
3360 { "sr12", offsetof(CPUState, sr[12]) },
3361 { "sr13", offsetof(CPUState, sr[13]) },
3362 { "sr14", offsetof(CPUState, sr[14]) },
3363 { "sr15", offsetof(CPUState, sr[15]) },
3364 /* Too lazy to put BATs... */
3365 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
3367 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
3368 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
3369 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
3370 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
3371 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
3372 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
3373 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
3374 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
3375 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
3376 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
3377 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
3378 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
3379 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
3380 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
3381 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
3382 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
3383 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
3384 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
3385 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
3386 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
3387 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
3388 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
3389 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
3390 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
3391 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
3392 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
3393 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
3394 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
3395 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
3396 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
3397 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
3398 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
3399 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
3400 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
3401 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
3402 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
3403 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
3404 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
3405 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
3406 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
3407 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
3408 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
3409 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
3410 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
3411 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
3412 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
3413 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
3414 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
3415 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
3416 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
3417 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
3418 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
3419 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
3420 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
3421 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
3422 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
3423 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
3424 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
3425 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
3426 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
3427 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
3428 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
3429 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
3430 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
3431 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
3432 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
3434 #elif defined(TARGET_SPARC)
3435 { "g0", offsetof(CPUState, gregs[0]) },
3436 { "g1", offsetof(CPUState, gregs[1]) },
3437 { "g2", offsetof(CPUState, gregs[2]) },
3438 { "g3", offsetof(CPUState, gregs[3]) },
3439 { "g4", offsetof(CPUState, gregs[4]) },
3440 { "g5", offsetof(CPUState, gregs[5]) },
3441 { "g6", offsetof(CPUState, gregs[6]) },
3442 { "g7", offsetof(CPUState, gregs[7]) },
3443 { "o0", 0, monitor_get_reg },
3444 { "o1", 1, monitor_get_reg },
3445 { "o2", 2, monitor_get_reg },
3446 { "o3", 3, monitor_get_reg },
3447 { "o4", 4, monitor_get_reg },
3448 { "o5", 5, monitor_get_reg },
3449 { "o6", 6, monitor_get_reg },
3450 { "o7", 7, monitor_get_reg },
3451 { "l0", 8, monitor_get_reg },
3452 { "l1", 9, monitor_get_reg },
3453 { "l2", 10, monitor_get_reg },
3454 { "l3", 11, monitor_get_reg },
3455 { "l4", 12, monitor_get_reg },
3456 { "l5", 13, monitor_get_reg },
3457 { "l6", 14, monitor_get_reg },
3458 { "l7", 15, monitor_get_reg },
3459 { "i0", 16, monitor_get_reg },
3460 { "i1", 17, monitor_get_reg },
3461 { "i2", 18, monitor_get_reg },
3462 { "i3", 19, monitor_get_reg },
3463 { "i4", 20, monitor_get_reg },
3464 { "i5", 21, monitor_get_reg },
3465 { "i6", 22, monitor_get_reg },
3466 { "i7", 23, monitor_get_reg },
3467 { "pc", offsetof(CPUState, pc) },
3468 { "npc", offsetof(CPUState, npc) },
3469 { "y", offsetof(CPUState, y) },
3470 #ifndef TARGET_SPARC64
3471 { "psr", 0, &monitor_get_psr, },
3472 { "wim", offsetof(CPUState, wim) },
3474 { "tbr", offsetof(CPUState, tbr) },
3475 { "fsr", offsetof(CPUState, fsr) },
3476 { "f0", offsetof(CPUState, fpr[0].l.upper) },
3477 { "f1", offsetof(CPUState, fpr[0].l.lower) },
3478 { "f2", offsetof(CPUState, fpr[1].l.upper) },
3479 { "f3", offsetof(CPUState, fpr[1].l.lower) },
3480 { "f4", offsetof(CPUState, fpr[2].l.upper) },
3481 { "f5", offsetof(CPUState, fpr[2].l.lower) },
3482 { "f6", offsetof(CPUState, fpr[3].l.upper) },
3483 { "f7", offsetof(CPUState, fpr[3].l.lower) },
3484 { "f8", offsetof(CPUState, fpr[4].l.upper) },
3485 { "f9", offsetof(CPUState, fpr[4].l.lower) },
3486 { "f10", offsetof(CPUState, fpr[5].l.upper) },
3487 { "f11", offsetof(CPUState, fpr[5].l.lower) },
3488 { "f12", offsetof(CPUState, fpr[6].l.upper) },
3489 { "f13", offsetof(CPUState, fpr[6].l.lower) },
3490 { "f14", offsetof(CPUState, fpr[7].l.upper) },
3491 { "f15", offsetof(CPUState, fpr[7].l.lower) },
3492 { "f16", offsetof(CPUState, fpr[8].l.upper) },
3493 { "f17", offsetof(CPUState, fpr[8].l.lower) },
3494 { "f18", offsetof(CPUState, fpr[9].l.upper) },
3495 { "f19", offsetof(CPUState, fpr[9].l.lower) },
3496 { "f20", offsetof(CPUState, fpr[10].l.upper) },
3497 { "f21", offsetof(CPUState, fpr[10].l.lower) },
3498 { "f22", offsetof(CPUState, fpr[11].l.upper) },
3499 { "f23", offsetof(CPUState, fpr[11].l.lower) },
3500 { "f24", offsetof(CPUState, fpr[12].l.upper) },
3501 { "f25", offsetof(CPUState, fpr[12].l.lower) },
3502 { "f26", offsetof(CPUState, fpr[13].l.upper) },
3503 { "f27", offsetof(CPUState, fpr[13].l.lower) },
3504 { "f28", offsetof(CPUState, fpr[14].l.upper) },
3505 { "f29", offsetof(CPUState, fpr[14].l.lower) },
3506 { "f30", offsetof(CPUState, fpr[15].l.upper) },
3507 { "f31", offsetof(CPUState, fpr[15].l.lower) },
3508 #ifdef TARGET_SPARC64
3509 { "f32", offsetof(CPUState, fpr[16]) },
3510 { "f34", offsetof(CPUState, fpr[17]) },
3511 { "f36", offsetof(CPUState, fpr[18]) },
3512 { "f38", offsetof(CPUState, fpr[19]) },
3513 { "f40", offsetof(CPUState, fpr[20]) },
3514 { "f42", offsetof(CPUState, fpr[21]) },
3515 { "f44", offsetof(CPUState, fpr[22]) },
3516 { "f46", offsetof(CPUState, fpr[23]) },
3517 { "f48", offsetof(CPUState, fpr[24]) },
3518 { "f50", offsetof(CPUState, fpr[25]) },
3519 { "f52", offsetof(CPUState, fpr[26]) },
3520 { "f54", offsetof(CPUState, fpr[27]) },
3521 { "f56", offsetof(CPUState, fpr[28]) },
3522 { "f58", offsetof(CPUState, fpr[29]) },
3523 { "f60", offsetof(CPUState, fpr[30]) },
3524 { "f62", offsetof(CPUState, fpr[31]) },
3525 { "asi", offsetof(CPUState, asi) },
3526 { "pstate", offsetof(CPUState, pstate) },
3527 { "cansave", offsetof(CPUState, cansave) },
3528 { "canrestore", offsetof(CPUState, canrestore) },
3529 { "otherwin", offsetof(CPUState, otherwin) },
3530 { "wstate", offsetof(CPUState, wstate) },
3531 { "cleanwin", offsetof(CPUState, cleanwin) },
3532 { "fprs", offsetof(CPUState, fprs) },
3538 static void expr_error(Monitor *mon, const char *msg)
3540 monitor_printf(mon, "%s\n", msg);
3541 longjmp(expr_env, 1);
3544 /* return 0 if OK, -1 if not found */
3545 static int get_monitor_def(target_long *pval, const char *name)
3547 const MonitorDef *md;
3550 for(md = monitor_defs; md->name != NULL; md++) {
3551 if (compare_cmd(name, md->name)) {
3552 if (md->get_value) {
3553 *pval = md->get_value(md, md->offset);
3555 CPUState *env = mon_get_cpu();
3556 ptr = (uint8_t *)env + md->offset;
3559 *pval = *(int32_t *)ptr;
3562 *pval = *(target_long *)ptr;
3575 static void next(void)
3579 while (qemu_isspace(*pch))
3584 static int64_t expr_sum(Monitor *mon);
3586 static int64_t expr_unary(Monitor *mon)
3595 n = expr_unary(mon);
3599 n = -expr_unary(mon);
3603 n = ~expr_unary(mon);
3609 expr_error(mon, "')' expected");
3616 expr_error(mon, "character constant expected");
3620 expr_error(mon, "missing terminating \' character");
3630 while ((*pch >= 'a' && *pch <= 'z') ||
3631 (*pch >= 'A' && *pch <= 'Z') ||
3632 (*pch >= '0' && *pch <= '9') ||
3633 *pch == '_' || *pch == '.') {
3634 if ((q - buf) < sizeof(buf) - 1)
3638 while (qemu_isspace(*pch))
3641 ret = get_monitor_def(®, buf);
3643 expr_error(mon, "unknown register");
3648 expr_error(mon, "unexpected end of expression");
3652 #if TARGET_PHYS_ADDR_BITS > 32
3653 n = strtoull(pch, &p, 0);
3655 n = strtoul(pch, &p, 0);
3658 expr_error(mon, "invalid char in expression");
3661 while (qemu_isspace(*pch))
3669 static int64_t expr_prod(Monitor *mon)
3674 val = expr_unary(mon);
3677 if (op != '*' && op != '/' && op != '%')
3680 val2 = expr_unary(mon);
3689 expr_error(mon, "division by zero");
3700 static int64_t expr_logic(Monitor *mon)
3705 val = expr_prod(mon);
3708 if (op != '&' && op != '|' && op != '^')
3711 val2 = expr_prod(mon);
3728 static int64_t expr_sum(Monitor *mon)
3733 val = expr_logic(mon);
3736 if (op != '+' && op != '-')
3739 val2 = expr_logic(mon);
3748 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3751 if (setjmp(expr_env)) {
3755 while (qemu_isspace(*pch))
3757 *pval = expr_sum(mon);
3762 static int get_double(Monitor *mon, double *pval, const char **pp)
3764 const char *p = *pp;
3768 d = strtod(p, &tailp);
3770 monitor_printf(mon, "Number expected\n");
3773 if (d != d || d - d != 0) {
3774 /* NaN or infinity */
3775 monitor_printf(mon, "Bad number\n");
3783 static int get_str(char *buf, int buf_size, const char **pp)
3791 while (qemu_isspace(*p))
3801 while (*p != '\0' && *p != '\"') {
3817 qemu_printf("unsupported escape code: '\\%c'\n", c);
3820 if ((q - buf) < buf_size - 1) {
3824 if ((q - buf) < buf_size - 1) {
3831 qemu_printf("unterminated string\n");
3836 while (*p != '\0' && !qemu_isspace(*p)) {
3837 if ((q - buf) < buf_size - 1) {
3849 * Store the command-name in cmdname, and return a pointer to
3850 * the remaining of the command string.
3852 static const char *get_command_name(const char *cmdline,
3853 char *cmdname, size_t nlen)
3856 const char *p, *pstart;
3859 while (qemu_isspace(*p))
3864 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3869 memcpy(cmdname, pstart, len);
3870 cmdname[len] = '\0';
3875 * Read key of 'type' into 'key' and return the current
3878 static char *key_get_info(const char *type, char **key)
3886 p = strchr(type, ':');
3893 str = g_malloc(len + 1);
3894 memcpy(str, type, len);
3901 static int default_fmt_format = 'x';
3902 static int default_fmt_size = 4;
3906 static int is_valid_option(const char *c, const char *typestr)
3914 typestr = strstr(typestr, option);
3915 return (typestr != NULL);
3918 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3919 const char *cmdname)
3921 const mon_cmd_t *cmd;
3923 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3924 if (compare_cmd(cmdname, cmd->name)) {
3932 static const mon_cmd_t *monitor_find_command(const char *cmdname)
3934 return search_dispatch_table(mon_cmds, cmdname);
3937 static const mon_cmd_t *qmp_find_query_cmd(const char *info_item)
3939 return search_dispatch_table(qmp_query_cmds, info_item);
3942 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3944 return search_dispatch_table(qmp_cmds, cmdname);
3947 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3948 const char *cmdline,
3951 const char *p, *typestr;
3953 const mon_cmd_t *cmd;
3959 monitor_printf(mon, "command='%s'\n", cmdline);
3962 /* extract the command name */
3963 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3967 cmd = monitor_find_command(cmdname);
3969 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
3973 /* parse the parameters */
3974 typestr = cmd->args_type;
3976 typestr = key_get_info(typestr, &key);
3988 while (qemu_isspace(*p))
3990 if (*typestr == '?') {
3993 /* no optional string: NULL argument */
3997 ret = get_str(buf, sizeof(buf), &p);
4001 monitor_printf(mon, "%s: filename expected\n",
4005 monitor_printf(mon, "%s: block device name expected\n",
4009 monitor_printf(mon, "%s: string expected\n", cmdname);
4014 qdict_put(qdict, key, qstring_from_str(buf));
4019 QemuOptsList *opts_list;
4022 opts_list = qemu_find_opts(key);
4023 if (!opts_list || opts_list->desc->name) {
4026 while (qemu_isspace(*p)) {
4031 if (get_str(buf, sizeof(buf), &p) < 0) {
4034 opts = qemu_opts_parse(opts_list, buf, 1);
4038 qemu_opts_to_qdict(opts, qdict);
4039 qemu_opts_del(opts);
4044 int count, format, size;
4046 while (qemu_isspace(*p))
4052 if (qemu_isdigit(*p)) {
4054 while (qemu_isdigit(*p)) {
4055 count = count * 10 + (*p - '0');
4093 if (*p != '\0' && !qemu_isspace(*p)) {
4094 monitor_printf(mon, "invalid char in format: '%c'\n",
4099 format = default_fmt_format;
4100 if (format != 'i') {
4101 /* for 'i', not specifying a size gives -1 as size */
4103 size = default_fmt_size;
4104 default_fmt_size = size;
4106 default_fmt_format = format;
4109 format = default_fmt_format;
4110 if (format != 'i') {
4111 size = default_fmt_size;
4116 qdict_put(qdict, "count", qint_from_int(count));
4117 qdict_put(qdict, "format", qint_from_int(format));
4118 qdict_put(qdict, "size", qint_from_int(size));
4127 while (qemu_isspace(*p))
4129 if (*typestr == '?' || *typestr == '.') {
4130 if (*typestr == '?') {
4138 while (qemu_isspace(*p))
4147 if (get_expr(mon, &val, &p))
4149 /* Check if 'i' is greater than 32-bit */
4150 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
4151 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
4152 monitor_printf(mon, "integer is for 32-bit values\n");
4154 } else if (c == 'M') {
4157 qdict_put(qdict, key, qint_from_int(val));
4165 while (qemu_isspace(*p)) {
4168 if (*typestr == '?') {
4174 val = strtosz(p, &end);
4176 monitor_printf(mon, "invalid size\n");
4179 qdict_put(qdict, key, qint_from_int(val));
4187 while (qemu_isspace(*p))
4189 if (*typestr == '?') {
4195 if (get_double(mon, &val, &p) < 0) {
4198 if (p[0] && p[1] == 's') {
4201 val /= 1e3; p += 2; break;
4203 val /= 1e6; p += 2; break;
4205 val /= 1e9; p += 2; break;
4208 if (*p && !qemu_isspace(*p)) {
4209 monitor_printf(mon, "Unknown unit suffix\n");
4212 qdict_put(qdict, key, qfloat_from_double(val));
4220 while (qemu_isspace(*p)) {
4224 while (qemu_isgraph(*p)) {
4227 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
4229 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
4232 monitor_printf(mon, "Expected 'on' or 'off'\n");
4235 qdict_put(qdict, key, qbool_from_int(val));
4240 const char *tmp = p;
4247 while (qemu_isspace(*p))
4252 if(!is_valid_option(p, typestr)) {
4254 monitor_printf(mon, "%s: unsupported option -%c\n",
4266 qdict_put(qdict, key, qbool_from_int(1));
4273 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
4279 /* check that all arguments were parsed */
4280 while (qemu_isspace(*p))
4283 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4295 void monitor_set_error(Monitor *mon, QError *qerror)
4297 /* report only the first error */
4299 mon->error = qerror;
4301 MON_DEBUG("Additional error report at %s:%d\n",
4302 qerror->file, qerror->linenr);
4307 static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
4309 if (ret && !monitor_has_error(mon)) {
4311 * If it returns failure, it must have passed on error.
4313 * Action: Report an internal error to the client if in QMP.
4315 qerror_report(QERR_UNDEFINED_ERROR);
4316 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4320 #ifdef CONFIG_DEBUG_MONITOR
4321 if (!ret && monitor_has_error(mon)) {
4323 * If it returns success, it must not have passed an error.
4325 * Action: Report the passed error to the client.
4327 MON_DEBUG("command '%s' returned success but passed an error\n",
4331 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
4333 * Handlers should not call Monitor print functions.
4335 * Action: Ignore them in QMP.
4337 * (XXX: we don't check any 'info' or 'query' command here
4338 * because the user print function _is_ called by do_info(), hence
4339 * we will trigger this check. This problem will go away when we
4340 * make 'query' commands real and kill do_info())
4342 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4343 cmd->name, mon_print_count_get(mon));
4348 static void handle_user_command(Monitor *mon, const char *cmdline)
4351 const mon_cmd_t *cmd;
4353 qdict = qdict_new();
4355 cmd = monitor_parse_command(mon, cmdline, qdict);
4359 if (handler_is_async(cmd)) {
4360 user_async_cmd_handler(mon, cmd, qdict);
4361 } else if (handler_is_qobject(cmd)) {
4362 QObject *data = NULL;
4364 /* XXX: ignores the error code */
4365 cmd->mhandler.cmd_new(mon, qdict, &data);
4366 assert(!monitor_has_error(mon));
4368 cmd->user_print(mon, data);
4369 qobject_decref(data);
4372 cmd->mhandler.cmd(mon, qdict);
4379 static void cmd_completion(const char *name, const char *list)
4381 const char *p, *pstart;
4390 p = pstart + strlen(pstart);
4392 if (len > sizeof(cmd) - 2)
4393 len = sizeof(cmd) - 2;
4394 memcpy(cmd, pstart, len);
4396 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
4397 readline_add_completion(cur_mon->rs, cmd);
4405 static void file_completion(const char *input)
4410 char file[1024], file_prefix[1024];
4414 p = strrchr(input, '/');
4417 pstrcpy(file_prefix, sizeof(file_prefix), input);
4418 pstrcpy(path, sizeof(path), ".");
4420 input_path_len = p - input + 1;
4421 memcpy(path, input, input_path_len);
4422 if (input_path_len > sizeof(path) - 1)
4423 input_path_len = sizeof(path) - 1;
4424 path[input_path_len] = '\0';
4425 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4427 #ifdef DEBUG_COMPLETION
4428 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
4429 input, path, file_prefix);
4431 ffs = opendir(path);
4440 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4444 if (strstart(d->d_name, file_prefix, NULL)) {
4445 memcpy(file, input, input_path_len);
4446 if (input_path_len < sizeof(file))
4447 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4449 /* stat the file to find out if it's a directory.
4450 * In that case add a slash to speed up typing long paths
4453 if(S_ISDIR(sb.st_mode))
4454 pstrcat(file, sizeof(file), "/");
4455 readline_add_completion(cur_mon->rs, file);
4461 static void block_completion_it(void *opaque, BlockDriverState *bs)
4463 const char *name = bdrv_get_device_name(bs);
4464 const char *input = opaque;
4466 if (input[0] == '\0' ||
4467 !strncmp(name, (char *)input, strlen(input))) {
4468 readline_add_completion(cur_mon->rs, name);
4472 /* NOTE: this parser is an approximate form of the real command parser */
4473 static void parse_cmdline(const char *cmdline,
4474 int *pnb_args, char **args)
4483 while (qemu_isspace(*p))
4487 if (nb_args >= MAX_ARGS)
4489 ret = get_str(buf, sizeof(buf), &p);
4490 args[nb_args] = g_strdup(buf);
4495 *pnb_args = nb_args;
4498 static const char *next_arg_type(const char *typestr)
4500 const char *p = strchr(typestr, ':');
4501 return (p != NULL ? ++p : typestr);
4504 static void monitor_find_completion(const char *cmdline)
4506 const char *cmdname;
4507 char *args[MAX_ARGS];
4508 int nb_args, i, len;
4509 const char *ptype, *str;
4510 const mon_cmd_t *cmd;
4513 parse_cmdline(cmdline, &nb_args, args);
4514 #ifdef DEBUG_COMPLETION
4515 for(i = 0; i < nb_args; i++) {
4516 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
4520 /* if the line ends with a space, it means we want to complete the
4522 len = strlen(cmdline);
4523 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4524 if (nb_args >= MAX_ARGS) {
4527 args[nb_args++] = g_strdup("");
4530 /* command completion */
4535 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
4536 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
4537 cmd_completion(cmdname, cmd->name);
4540 /* find the command */
4541 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4542 if (compare_cmd(args[0], cmd->name)) {
4550 ptype = next_arg_type(cmd->args_type);
4551 for(i = 0; i < nb_args - 2; i++) {
4552 if (*ptype != '\0') {
4553 ptype = next_arg_type(ptype);
4554 while (*ptype == '?')
4555 ptype = next_arg_type(ptype);
4558 str = args[nb_args - 1];
4559 if (*ptype == '-' && ptype[1] != '\0') {
4560 ptype = next_arg_type(ptype);
4564 /* file completion */
4565 readline_set_completion_index(cur_mon->rs, strlen(str));
4566 file_completion(str);
4569 /* block device name completion */
4570 readline_set_completion_index(cur_mon->rs, strlen(str));
4571 bdrv_iterate(block_completion_it, (void *)str);
4574 /* XXX: more generic ? */
4575 if (!strcmp(cmd->name, "info")) {
4576 readline_set_completion_index(cur_mon->rs, strlen(str));
4577 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4578 cmd_completion(str, cmd->name);
4580 } else if (!strcmp(cmd->name, "sendkey")) {
4581 char *sep = strrchr(str, '-');
4584 readline_set_completion_index(cur_mon->rs, strlen(str));
4585 for(key = key_defs; key->name != NULL; key++) {
4586 cmd_completion(str, key->name);
4588 } else if (!strcmp(cmd->name, "help|?")) {
4589 readline_set_completion_index(cur_mon->rs, strlen(str));
4590 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4591 cmd_completion(str, cmd->name);
4601 for (i = 0; i < nb_args; i++) {
4606 static int monitor_can_read(void *opaque)
4608 Monitor *mon = opaque;
4610 return (mon->suspend_cnt == 0) ? 1 : 0;
4613 static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4615 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4616 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4620 * Argument validation rules:
4622 * 1. The argument must exist in cmd_args qdict
4623 * 2. The argument type must be the expected one
4625 * Special case: If the argument doesn't exist in cmd_args and
4626 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4627 * checking is skipped for it.
4629 static int check_client_args_type(const QDict *client_args,
4630 const QDict *cmd_args, int flags)
4632 const QDictEntry *ent;
4634 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4637 const QObject *client_arg = qdict_entry_value(ent);
4638 const char *client_arg_name = qdict_entry_key(ent);
4640 obj = qdict_get(cmd_args, client_arg_name);
4642 if (flags & QMP_ACCEPT_UNKNOWNS) {
4643 /* handler accepts unknowns */
4646 /* client arg doesn't exist */
4647 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4651 arg_type = qobject_to_qstring(obj);
4652 assert(arg_type != NULL);
4654 /* check if argument's type is correct */
4655 switch (qstring_get_str(arg_type)[0]) {
4659 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4660 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4669 if (qobject_type(client_arg) != QTYPE_QINT) {
4670 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4676 if (qobject_type(client_arg) != QTYPE_QINT &&
4677 qobject_type(client_arg) != QTYPE_QFLOAT) {
4678 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4685 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4686 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4692 assert(flags & QMP_ACCEPT_UNKNOWNS);
4697 * These types are not supported by QMP and thus are not
4698 * handled here. Fall through.
4709 * - Check if the client has passed all mandatory args
4710 * - Set special flags for argument validation
4712 static int check_mandatory_args(const QDict *cmd_args,
4713 const QDict *client_args, int *flags)
4715 const QDictEntry *ent;
4717 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4718 const char *cmd_arg_name = qdict_entry_key(ent);
4719 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4720 assert(type != NULL);
4722 if (qstring_get_str(type)[0] == 'O') {
4723 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4724 *flags |= QMP_ACCEPT_UNKNOWNS;
4725 } else if (qstring_get_str(type)[0] != '-' &&
4726 qstring_get_str(type)[1] != '?' &&
4727 !qdict_haskey(client_args, cmd_arg_name)) {
4728 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4736 static QDict *qdict_from_args_type(const char *args_type)
4740 QString *key, *type, *cur_qs;
4742 assert(args_type != NULL);
4744 qdict = qdict_new();
4746 if (args_type == NULL || args_type[0] == '\0') {
4747 /* no args, empty qdict */
4751 key = qstring_new();
4752 type = qstring_new();
4757 switch (args_type[i]) {
4760 qdict_put(qdict, qstring_get_str(key), type);
4762 if (args_type[i] == '\0') {
4765 type = qstring_new(); /* qdict has ref */
4766 cur_qs = key = qstring_new();
4772 qstring_append_chr(cur_qs, args_type[i]);
4782 * Client argument checking rules:
4784 * 1. Client must provide all mandatory arguments
4785 * 2. Each argument provided by the client must be expected
4786 * 3. Each argument provided by the client must have the type expected
4789 static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4794 cmd_args = qdict_from_args_type(cmd->args_type);
4797 err = check_mandatory_args(cmd_args, client_args, &flags);
4802 err = check_client_args_type(client_args, cmd_args, flags);
4810 * Input object checking rules
4812 * 1. Input object must be a dict
4813 * 2. The "execute" key must exist
4814 * 3. The "execute" key must be a string
4815 * 4. If the "arguments" key exists, it must be a dict
4816 * 5. If the "id" key exists, it can be anything (ie. json-value)
4817 * 6. Any argument not listed above is considered invalid
4819 static QDict *qmp_check_input_obj(QObject *input_obj)
4821 const QDictEntry *ent;
4822 int has_exec_key = 0;
4825 if (qobject_type(input_obj) != QTYPE_QDICT) {
4826 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4830 input_dict = qobject_to_qdict(input_obj);
4832 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4833 const char *arg_name = qdict_entry_key(ent);
4834 const QObject *arg_obj = qdict_entry_value(ent);
4836 if (!strcmp(arg_name, "execute")) {
4837 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4838 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4843 } else if (!strcmp(arg_name, "arguments")) {
4844 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4845 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4849 } else if (!strcmp(arg_name, "id")) {
4850 /* FIXME: check duplicated IDs for async commands */
4852 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4857 if (!has_exec_key) {
4858 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4865 static void qmp_call_query_cmd(Monitor *mon, const mon_cmd_t *cmd)
4867 QObject *ret_data = NULL;
4869 if (handler_is_async(cmd)) {
4870 qmp_async_info_handler(mon, cmd);
4871 if (monitor_has_error(mon)) {
4872 monitor_protocol_emitter(mon, NULL);
4875 cmd->mhandler.info_new(mon, &ret_data);
4876 monitor_protocol_emitter(mon, ret_data);
4877 qobject_decref(ret_data);
4881 static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4882 const QDict *params)
4885 QObject *data = NULL;
4887 mon_print_count_init(mon);
4889 ret = cmd->mhandler.cmd_new(mon, params, &data);
4890 handler_audit(mon, cmd, ret);
4891 monitor_protocol_emitter(mon, data);
4892 qobject_decref(data);
4895 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4899 QDict *input, *args;
4900 const mon_cmd_t *cmd;
4901 Monitor *mon = cur_mon;
4902 const char *cmd_name, *query_cmd;
4905 args = input = NULL;
4907 obj = json_parser_parse(tokens, NULL);
4909 // FIXME: should be triggered in json_parser_parse()
4910 qerror_report(QERR_JSON_PARSING);
4914 input = qmp_check_input_obj(obj);
4916 qobject_decref(obj);
4920 mon->mc->id = qdict_get(input, "id");
4921 qobject_incref(mon->mc->id);
4923 cmd_name = qdict_get_str(input, "execute");
4924 trace_handle_qmp_command(mon, cmd_name);
4925 if (invalid_qmp_mode(mon, cmd_name)) {
4926 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4930 cmd = qmp_find_cmd(cmd_name);
4931 if (!cmd && strstart(cmd_name, "query-", &query_cmd)) {
4932 cmd = qmp_find_query_cmd(query_cmd);
4935 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4939 obj = qdict_get(input, "arguments");
4943 args = qobject_to_qdict(obj);
4947 err = qmp_check_client_args(cmd, args);
4953 qmp_call_query_cmd(mon, cmd);
4954 } else if (handler_is_async(cmd)) {
4955 err = qmp_async_cmd_handler(mon, cmd, args);
4957 /* emit the error response */
4961 qmp_call_cmd(mon, cmd, args);
4967 monitor_protocol_emitter(mon, NULL);
4974 * monitor_control_read(): Read and handle QMP input
4976 static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4978 Monitor *old_mon = cur_mon;
4982 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
4987 static void monitor_read(void *opaque, const uint8_t *buf, int size)
4989 Monitor *old_mon = cur_mon;
4995 for (i = 0; i < size; i++)
4996 readline_handle_byte(cur_mon->rs, buf[i]);
4998 if (size == 0 || buf[size - 1] != 0)
4999 monitor_printf(cur_mon, "corrupted command\n");
5001 handle_user_command(cur_mon, (char *)buf);
5007 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
5009 monitor_suspend(mon);
5010 handle_user_command(mon, cmdline);
5011 monitor_resume(mon);
5014 int monitor_suspend(Monitor *mon)
5022 void monitor_resume(Monitor *mon)
5026 if (--mon->suspend_cnt == 0)
5027 readline_show_prompt(mon->rs);
5030 static QObject *get_qmp_greeting(void)
5032 QObject *ver = NULL;
5034 qmp_marshal_input_query_version(NULL, NULL, &ver);
5035 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5039 * monitor_control_event(): Print QMP gretting
5041 static void monitor_control_event(void *opaque, int event)
5044 Monitor *mon = opaque;
5047 case CHR_EVENT_OPENED:
5048 mon->mc->command_mode = 0;
5049 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
5050 data = get_qmp_greeting();
5051 monitor_json_emitter(mon, data);
5052 qobject_decref(data);
5054 case CHR_EVENT_CLOSED:
5055 json_message_parser_destroy(&mon->mc->parser);
5060 static void monitor_event(void *opaque, int event)
5062 Monitor *mon = opaque;
5065 case CHR_EVENT_MUX_IN:
5067 if (mon->reset_seen) {
5068 readline_restart(mon->rs);
5069 monitor_resume(mon);
5072 mon->suspend_cnt = 0;
5076 case CHR_EVENT_MUX_OUT:
5077 if (mon->reset_seen) {
5078 if (mon->suspend_cnt == 0) {
5079 monitor_printf(mon, "\n");
5082 monitor_suspend(mon);
5089 case CHR_EVENT_OPENED:
5090 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5091 "information\n", QEMU_VERSION);
5092 if (!mon->mux_out) {
5093 readline_show_prompt(mon->rs);
5095 mon->reset_seen = 1;
5109 void monitor_init(CharDriverState *chr, int flags)
5111 static int is_first_init = 1;
5114 if (is_first_init) {
5115 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
5119 mon = g_malloc0(sizeof(*mon));
5123 if (flags & MONITOR_USE_READLINE) {
5124 mon->rs = readline_init(mon, monitor_find_completion);
5125 monitor_read_command(mon, 0);
5128 if (monitor_ctrl_mode(mon)) {
5129 mon->mc = g_malloc0(sizeof(MonitorControl));
5130 /* Control mode requires special handlers */
5131 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
5132 monitor_control_event, mon);
5133 qemu_chr_fe_set_echo(chr, true);
5135 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5136 monitor_event, mon);
5139 QLIST_INSERT_HEAD(&mon_list, mon, entry);
5140 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
5144 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
5146 BlockDriverState *bs = opaque;
5149 if (bdrv_set_key(bs, password) != 0) {
5150 monitor_printf(mon, "invalid password\n");
5153 if (mon->password_completion_cb)
5154 mon->password_completion_cb(mon->password_opaque, ret);
5156 monitor_read_command(mon, 1);
5159 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5160 BlockDriverCompletionFunc *completion_cb,
5165 if (!bdrv_key_required(bs)) {
5167 completion_cb(opaque, 0);
5171 if (monitor_ctrl_mode(mon)) {
5172 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs));
5176 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5177 bdrv_get_encrypted_filename(bs));
5179 mon->password_completion_cb = completion_cb;
5180 mon->password_opaque = opaque;
5182 err = monitor_read_password(mon, bdrv_password_cb, bs);
5184 if (err && completion_cb)
5185 completion_cb(opaque, err);