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 void handle_user_command(Monitor
*mon
, const char *cmdline
);
519 static int do_hmp_passthrough(Monitor
*mon
, const QDict
*params
,
523 Monitor
*old_mon
, hmp
;
524 CharDriverState mchar
;
526 memset(&hmp
, 0, sizeof(hmp
));
527 qemu_chr_init_mem(&mchar
);
533 if (qdict_haskey(params
, "cpu-index")) {
534 ret
= monitor_set_cpu(qdict_get_int(params
, "cpu-index"));
537 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "cpu-index", "a CPU number");
542 handle_user_command(&hmp
, qdict_get_str(params
, "command-line"));
545 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
546 *ret_data
= QOBJECT(qemu_chr_mem_to_qs(hmp
.chr
));
550 qemu_chr_close_mem(hmp
.chr
);
554 static int compare_cmd(const char *name
, const char *list
)
556 const char *p
, *pstart
;
564 p
= pstart
+ strlen(pstart
);
565 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
574 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
575 const char *prefix
, const char *name
)
577 const mon_cmd_t
*cmd
;
579 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
580 if (!name
|| !strcmp(name
, cmd
->name
))
581 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
582 cmd
->params
, cmd
->help
);
586 static void help_cmd(Monitor
*mon
, const char *name
)
588 if (name
&& !strcmp(name
, "info")) {
589 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
591 help_cmd_dump(mon
, mon_cmds
, "", name
);
592 if (name
&& !strcmp(name
, "log")) {
593 const CPULogItem
*item
;
594 monitor_printf(mon
, "Log items (comma separated):\n");
595 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
596 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
597 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
603 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
605 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
608 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
610 const char *tp_name
= qdict_get_str(qdict
, "name");
611 bool new_state
= qdict_get_bool(qdict
, "option");
612 int ret
= trace_event_set_state(tp_name
, new_state
);
615 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
619 #ifdef CONFIG_TRACE_SIMPLE
620 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
622 const char *op
= qdict_get_try_str(qdict
, "op");
623 const char *arg
= qdict_get_try_str(qdict
, "arg");
626 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
627 } else if (!strcmp(op
, "on")) {
628 st_set_trace_file_enabled(true);
629 } else if (!strcmp(op
, "off")) {
630 st_set_trace_file_enabled(false);
631 } else if (!strcmp(op
, "flush")) {
632 st_flush_trace_buffer();
633 } else if (!strcmp(op
, "set")) {
635 st_set_trace_file(arg
);
638 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
639 help_cmd(mon
, "trace-file");
644 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
646 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
649 data
->user_print(data
->mon
, ret_data
);
651 monitor_resume(data
->mon
);
655 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
657 monitor_protocol_emitter(opaque
, ret_data
);
660 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
663 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
666 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
668 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
671 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
676 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
678 cb_data
->user_print
= cmd
->user_print
;
679 monitor_suspend(mon
);
680 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
681 user_monitor_complete
, cb_data
);
688 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
692 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
694 cb_data
->user_print
= cmd
->user_print
;
695 monitor_suspend(mon
);
696 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
703 static void do_info(Monitor
*mon
, const QDict
*qdict
)
705 const mon_cmd_t
*cmd
;
706 const char *item
= qdict_get_try_str(qdict
, "item");
712 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
713 if (compare_cmd(item
, cmd
->name
))
717 if (cmd
->name
== NULL
) {
721 if (handler_is_async(cmd
)) {
722 user_async_info_handler(mon
, cmd
);
723 } else if (handler_is_qobject(cmd
)) {
724 QObject
*info_data
= NULL
;
726 cmd
->mhandler
.info_new(mon
, &info_data
);
728 cmd
->user_print(mon
, info_data
);
729 qobject_decref(info_data
);
732 cmd
->mhandler
.info(mon
);
738 help_cmd(mon
, "info");
741 static CommandInfoList
*alloc_cmd_entry(const char *cmd_name
)
743 CommandInfoList
*info
;
745 info
= g_malloc0(sizeof(*info
));
746 info
->value
= g_malloc0(sizeof(*info
->value
));
747 info
->value
->name
= g_strdup(cmd_name
);
752 CommandInfoList
*qmp_query_commands(Error
**errp
)
754 CommandInfoList
*info
, *cmd_list
= NULL
;
755 const mon_cmd_t
*cmd
;
757 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
758 info
= alloc_cmd_entry(cmd
->name
);
759 info
->next
= cmd_list
;
763 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
765 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
766 info
= alloc_cmd_entry(buf
);
767 info
->next
= cmd_list
;
774 /* set the current CPU defined by the user */
775 int monitor_set_cpu(int cpu_index
)
779 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
780 if (env
->cpu_index
== cpu_index
) {
781 cur_mon
->mon_cpu
= env
;
788 static CPUState
*mon_get_cpu(void)
790 if (!cur_mon
->mon_cpu
) {
793 cpu_synchronize_state(cur_mon
->mon_cpu
);
794 return cur_mon
->mon_cpu
;
797 int monitor_get_cpu_index(void)
799 return mon_get_cpu()->cpu_index
;
802 static void do_info_registers(Monitor
*mon
)
807 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
810 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
815 static void do_info_jit(Monitor
*mon
)
817 dump_exec_info((FILE *)mon
, monitor_fprintf
);
820 static void do_info_history(Monitor
*mon
)
829 str
= readline_get_history(mon
->rs
, i
);
832 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
837 #if defined(TARGET_PPC)
838 /* XXX: not implemented in other targets */
839 static void do_info_cpu_stats(Monitor
*mon
)
844 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
848 #if defined(CONFIG_TRACE_SIMPLE)
849 static void do_info_trace(Monitor
*mon
)
851 st_print_trace((FILE *)mon
, &monitor_fprintf
);
855 static void do_trace_print_events(Monitor
*mon
)
857 trace_print_events((FILE *)mon
, &monitor_fprintf
);
861 static int change_vnc_password(const char *password
)
863 if (!password
|| !password
[0]) {
864 if (vnc_display_disable_login(NULL
)) {
865 qerror_report(QERR_SET_PASSWD_FAILED
);
871 if (vnc_display_password(NULL
, password
) < 0) {
872 qerror_report(QERR_SET_PASSWD_FAILED
);
879 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
882 change_vnc_password(password
);
883 monitor_read_command(mon
, 1);
886 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
888 if (strcmp(target
, "passwd") == 0 ||
889 strcmp(target
, "password") == 0) {
892 strncpy(password
, arg
, sizeof(password
));
893 password
[sizeof(password
) - 1] = '\0';
894 return change_vnc_password(password
);
896 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
899 if (vnc_display_open(NULL
, target
) < 0) {
900 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
908 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
910 qerror_report(QERR_FEATURE_DISABLED
, "vnc");
916 * do_change(): Change a removable medium, or VNC configuration
918 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
920 const char *device
= qdict_get_str(qdict
, "device");
921 const char *target
= qdict_get_str(qdict
, "target");
922 const char *arg
= qdict_get_try_str(qdict
, "arg");
925 if (strcmp(device
, "vnc") == 0) {
926 ret
= do_change_vnc(mon
, target
, arg
);
928 ret
= do_change_block(mon
, device
, target
, arg
);
934 static int set_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
936 const char *protocol
= qdict_get_str(qdict
, "protocol");
937 const char *password
= qdict_get_str(qdict
, "password");
938 const char *connected
= qdict_get_try_str(qdict
, "connected");
939 int disconnect_if_connected
= 0;
940 int fail_if_connected
= 0;
944 if (strcmp(connected
, "fail") == 0) {
945 fail_if_connected
= 1;
946 } else if (strcmp(connected
, "disconnect") == 0) {
947 disconnect_if_connected
= 1;
948 } else if (strcmp(connected
, "keep") == 0) {
951 qerror_report(QERR_INVALID_PARAMETER
, "connected");
956 if (strcmp(protocol
, "spice") == 0) {
958 /* correct one? spice isn't a device ,,, */
959 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
962 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
963 disconnect_if_connected
);
965 qerror_report(QERR_SET_PASSWD_FAILED
);
971 if (strcmp(protocol
, "vnc") == 0) {
972 if (fail_if_connected
|| disconnect_if_connected
) {
973 /* vnc supports "connected=keep" only */
974 qerror_report(QERR_INVALID_PARAMETER
, "connected");
977 /* Note that setting an empty password will not disable login through
979 return vnc_display_password(NULL
, password
);
982 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
986 static int expire_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
988 const char *protocol
= qdict_get_str(qdict
, "protocol");
989 const char *whenstr
= qdict_get_str(qdict
, "time");
993 if (strcmp(whenstr
, "now") == 0) {
995 } else if (strcmp(whenstr
, "never") == 0) {
997 } else if (whenstr
[0] == '+') {
998 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
1000 when
= strtoull(whenstr
, NULL
, 10);
1003 if (strcmp(protocol
, "spice") == 0) {
1005 /* correct one? spice isn't a device ,,, */
1006 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1009 rc
= qemu_spice_set_pw_expire(when
);
1011 qerror_report(QERR_SET_PASSWD_FAILED
);
1017 if (strcmp(protocol
, "vnc") == 0) {
1018 return vnc_display_pw_expire(NULL
, when
);
1021 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1025 static int add_graphics_client(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1027 const char *protocol
= qdict_get_str(qdict
, "protocol");
1028 const char *fdname
= qdict_get_str(qdict
, "fdname");
1031 if (strcmp(protocol
, "spice") == 0) {
1033 /* correct one? spice isn't a device ,,, */
1034 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1037 qerror_report(QERR_ADD_CLIENT_FAILED
);
1040 } else if (strcmp(protocol
, "vnc") == 0) {
1041 int fd
= monitor_get_fd(mon
, fdname
);
1042 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
1043 vnc_display_add_client(NULL
, fd
, skipauth
);
1046 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
1047 int fd
= monitor_get_fd(mon
, fdname
);
1048 if (qemu_chr_add_client(s
, fd
) < 0) {
1049 qerror_report(QERR_ADD_CLIENT_FAILED
);
1055 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1059 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1061 const char *protocol
= qdict_get_str(qdict
, "protocol");
1062 const char *hostname
= qdict_get_str(qdict
, "hostname");
1063 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
1064 int port
= qdict_get_try_int(qdict
, "port", -1);
1065 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
1068 if (strcmp(protocol
, "spice") == 0) {
1070 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1074 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
);
1076 qerror_report(QERR_UNDEFINED_ERROR
);
1082 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1086 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1088 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1092 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1094 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1097 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1100 const char *items
= qdict_get_str(qdict
, "items");
1102 if (!strcmp(items
, "none")) {
1105 mask
= cpu_str_to_log_mask(items
);
1107 help_cmd(mon
, "log");
1114 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1116 const char *option
= qdict_get_try_str(qdict
, "option");
1117 if (!option
|| !strcmp(option
, "on")) {
1119 } else if (!strcmp(option
, "off")) {
1122 monitor_printf(mon
, "unexpected option %s\n", option
);
1126 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1128 struct bdrv_iterate_context
{
1133 static void iostatus_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1135 bdrv_iostatus_reset(bs
);
1139 * do_cont(): Resume emulation.
1141 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1143 struct bdrv_iterate_context context
= { mon
, 0 };
1145 if (runstate_check(RUN_STATE_INMIGRATE
)) {
1146 qerror_report(QERR_MIGRATION_EXPECTED
);
1148 } else if (runstate_check(RUN_STATE_INTERNAL_ERROR
) ||
1149 runstate_check(RUN_STATE_SHUTDOWN
)) {
1150 qerror_report(QERR_RESET_REQUIRED
);
1154 bdrv_iterate(iostatus_bdrv_it
, NULL
);
1155 bdrv_iterate(encrypted_bdrv_it
, &context
);
1156 /* only resume the vm if all keys are set and valid */
1165 static void bdrv_key_cb(void *opaque
, int err
)
1167 Monitor
*mon
= opaque
;
1169 /* another key was set successfully, retry to continue */
1171 do_cont(mon
, NULL
, NULL
);
1174 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1176 struct bdrv_iterate_context
*context
= opaque
;
1178 if (!context
->err
&& bdrv_key_required(bs
)) {
1179 context
->err
= -EBUSY
;
1180 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1185 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1187 const char *device
= qdict_get_try_str(qdict
, "device");
1189 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1190 if (gdbserver_start(device
) < 0) {
1191 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1193 } else if (strcmp(device
, "none") == 0) {
1194 monitor_printf(mon
, "Disabled gdbserver\n");
1196 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1201 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1203 const char *action
= qdict_get_str(qdict
, "action");
1204 if (select_watchdog_action(action
) == -1) {
1205 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1209 static void monitor_printc(Monitor
*mon
, int c
)
1211 monitor_printf(mon
, "'");
1214 monitor_printf(mon
, "\\'");
1217 monitor_printf(mon
, "\\\\");
1220 monitor_printf(mon
, "\\n");
1223 monitor_printf(mon
, "\\r");
1226 if (c
>= 32 && c
<= 126) {
1227 monitor_printf(mon
, "%c", c
);
1229 monitor_printf(mon
, "\\x%02x", c
);
1233 monitor_printf(mon
, "'");
1236 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1237 target_phys_addr_t addr
, int is_physical
)
1240 int l
, line_size
, i
, max_digits
, len
;
1244 if (format
== 'i') {
1247 env
= mon_get_cpu();
1251 } else if (wsize
== 4) {
1254 /* as default we use the current CS size */
1257 #ifdef TARGET_X86_64
1258 if ((env
->efer
& MSR_EFER_LMA
) &&
1259 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1263 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1268 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1272 len
= wsize
* count
;
1281 max_digits
= (wsize
* 8 + 2) / 3;
1285 max_digits
= (wsize
* 8) / 4;
1289 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1298 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1300 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1305 cpu_physical_memory_read(addr
, buf
, l
);
1307 env
= mon_get_cpu();
1308 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1309 monitor_printf(mon
, " Cannot access memory\n");
1318 v
= ldub_raw(buf
+ i
);
1321 v
= lduw_raw(buf
+ i
);
1324 v
= (uint32_t)ldl_raw(buf
+ i
);
1327 v
= ldq_raw(buf
+ i
);
1330 monitor_printf(mon
, " ");
1333 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1336 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1339 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1342 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1345 monitor_printc(mon
, v
);
1350 monitor_printf(mon
, "\n");
1356 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1358 int count
= qdict_get_int(qdict
, "count");
1359 int format
= qdict_get_int(qdict
, "format");
1360 int size
= qdict_get_int(qdict
, "size");
1361 target_long addr
= qdict_get_int(qdict
, "addr");
1363 memory_dump(mon
, count
, format
, size
, addr
, 0);
1366 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1368 int count
= qdict_get_int(qdict
, "count");
1369 int format
= qdict_get_int(qdict
, "format");
1370 int size
= qdict_get_int(qdict
, "size");
1371 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1373 memory_dump(mon
, count
, format
, size
, addr
, 1);
1376 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1378 int format
= qdict_get_int(qdict
, "format");
1379 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1381 #if TARGET_PHYS_ADDR_BITS == 32
1384 monitor_printf(mon
, "%#o", val
);
1387 monitor_printf(mon
, "%#x", val
);
1390 monitor_printf(mon
, "%u", val
);
1394 monitor_printf(mon
, "%d", val
);
1397 monitor_printc(mon
, val
);
1403 monitor_printf(mon
, "%#" PRIo64
, val
);
1406 monitor_printf(mon
, "%#" PRIx64
, val
);
1409 monitor_printf(mon
, "%" PRIu64
, val
);
1413 monitor_printf(mon
, "%" PRId64
, val
);
1416 monitor_printc(mon
, val
);
1420 monitor_printf(mon
, "\n");
1423 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1426 uint32_t size
= qdict_get_int(qdict
, "size");
1427 const char *filename
= qdict_get_str(qdict
, "filename");
1428 target_long addr
= qdict_get_int(qdict
, "val");
1434 env
= mon_get_cpu();
1436 f
= fopen(filename
, "wb");
1438 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1445 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1446 if (fwrite(buf
, 1, l
, f
) != l
) {
1447 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1461 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1467 uint32_t size
= qdict_get_int(qdict
, "size");
1468 const char *filename
= qdict_get_str(qdict
, "filename");
1469 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1472 f
= fopen(filename
, "wb");
1474 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1481 cpu_physical_memory_read(addr
, buf
, l
);
1482 if (fwrite(buf
, 1, l
, f
) != l
) {
1483 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1498 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1502 uint32_t start
= qdict_get_int(qdict
, "start");
1503 uint32_t size
= qdict_get_int(qdict
, "size");
1506 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1507 uint8_t val
= ldub_phys(addr
);
1508 /* BSD sum algorithm ('sum' Unix command) */
1509 sum
= (sum
>> 1) | (sum
<< 15);
1512 monitor_printf(mon
, "%05d\n", sum
);
1520 static const KeyDef key_defs
[] = {
1522 { 0x36, "shift_r" },
1527 { 0xe4, "altgr_r" },
1547 { 0x0e, "backspace" },
1560 { 0x1a, "bracket_left" },
1561 { 0x1b, "bracket_right" },
1573 { 0x27, "semicolon" },
1574 { 0x28, "apostrophe" },
1575 { 0x29, "grave_accent" },
1577 { 0x2b, "backslash" },
1589 { 0x37, "asterisk" },
1592 { 0x3a, "caps_lock" },
1603 { 0x45, "num_lock" },
1604 { 0x46, "scroll_lock" },
1606 { 0xb5, "kp_divide" },
1607 { 0x37, "kp_multiply" },
1608 { 0x4a, "kp_subtract" },
1610 { 0x9c, "kp_enter" },
1611 { 0x53, "kp_decimal" },
1644 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1659 { 0xfe, "compose" },
1664 static int get_keycode(const char *key
)
1670 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1671 if (!strcmp(key
, p
->name
))
1674 if (strstart(key
, "0x", NULL
)) {
1675 ret
= strtoul(key
, &endp
, 0);
1676 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1682 #define MAX_KEYCODES 16
1683 static uint8_t keycodes
[MAX_KEYCODES
];
1684 static int nb_pending_keycodes
;
1685 static QEMUTimer
*key_timer
;
1687 static void release_keys(void *opaque
)
1691 while (nb_pending_keycodes
> 0) {
1692 nb_pending_keycodes
--;
1693 keycode
= keycodes
[nb_pending_keycodes
];
1695 kbd_put_keycode(0xe0);
1696 kbd_put_keycode(keycode
| 0x80);
1700 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1702 char keyname_buf
[16];
1704 int keyname_len
, keycode
, i
;
1705 const char *string
= qdict_get_str(qdict
, "string");
1706 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1707 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1709 if (nb_pending_keycodes
> 0) {
1710 qemu_del_timer(key_timer
);
1717 separator
= strchr(string
, '-');
1718 keyname_len
= separator
? separator
- string
: strlen(string
);
1719 if (keyname_len
> 0) {
1720 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1721 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1722 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1725 if (i
== MAX_KEYCODES
) {
1726 monitor_printf(mon
, "too many keys\n");
1729 keyname_buf
[keyname_len
] = 0;
1730 keycode
= get_keycode(keyname_buf
);
1732 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1735 keycodes
[i
++] = keycode
;
1739 string
= separator
+ 1;
1741 nb_pending_keycodes
= i
;
1742 /* key down events */
1743 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1744 keycode
= keycodes
[i
];
1746 kbd_put_keycode(0xe0);
1747 kbd_put_keycode(keycode
& 0x7f);
1749 /* delayed key up events */
1750 qemu_mod_timer(key_timer
, qemu_get_clock_ns(vm_clock
) +
1751 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1754 static int mouse_button_state
;
1756 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1759 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1760 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1761 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1762 dx
= strtol(dx_str
, NULL
, 0);
1763 dy
= strtol(dy_str
, NULL
, 0);
1766 dz
= strtol(dz_str
, NULL
, 0);
1767 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1770 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1772 int button_state
= qdict_get_int(qdict
, "button_state");
1773 mouse_button_state
= button_state
;
1774 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1777 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1779 int size
= qdict_get_int(qdict
, "size");
1780 int addr
= qdict_get_int(qdict
, "addr");
1781 int has_index
= qdict_haskey(qdict
, "index");
1786 int index
= qdict_get_int(qdict
, "index");
1787 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1795 val
= cpu_inb(addr
);
1799 val
= cpu_inw(addr
);
1803 val
= cpu_inl(addr
);
1807 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1808 suffix
, addr
, size
* 2, val
);
1811 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1813 int size
= qdict_get_int(qdict
, "size");
1814 int addr
= qdict_get_int(qdict
, "addr");
1815 int val
= qdict_get_int(qdict
, "val");
1817 addr
&= IOPORTS_MASK
;
1822 cpu_outb(addr
, val
);
1825 cpu_outw(addr
, val
);
1828 cpu_outl(addr
, val
);
1833 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1836 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1838 res
= qemu_boot_set(bootdevice
);
1840 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1841 } else if (res
> 0) {
1842 monitor_printf(mon
, "setting boot device list failed\n");
1844 monitor_printf(mon
, "no function defined to set boot device list for "
1845 "this architecture\n");
1850 * do_system_powerdown(): Issue a machine powerdown
1852 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1855 qemu_system_powerdown_request();
1859 #if defined(TARGET_I386)
1860 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
1861 target_phys_addr_t pte
,
1862 target_phys_addr_t mask
)
1864 #ifdef TARGET_X86_64
1865 if (addr
& (1ULL << 47)) {
1869 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1870 " %c%c%c%c%c%c%c%c%c\n",
1873 pte
& PG_NX_MASK
? 'X' : '-',
1874 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1875 pte
& PG_PSE_MASK
? 'P' : '-',
1876 pte
& PG_DIRTY_MASK
? 'D' : '-',
1877 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1878 pte
& PG_PCD_MASK
? 'C' : '-',
1879 pte
& PG_PWT_MASK
? 'T' : '-',
1880 pte
& PG_USER_MASK
? 'U' : '-',
1881 pte
& PG_RW_MASK
? 'W' : '-');
1884 static void tlb_info_32(Monitor
*mon
, CPUState
*env
)
1886 unsigned int l1
, l2
;
1887 uint32_t pgd
, pde
, pte
;
1889 pgd
= env
->cr
[3] & ~0xfff;
1890 for(l1
= 0; l1
< 1024; l1
++) {
1891 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1892 pde
= le32_to_cpu(pde
);
1893 if (pde
& PG_PRESENT_MASK
) {
1894 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1896 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1898 for(l2
= 0; l2
< 1024; l2
++) {
1899 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1900 pte
= le32_to_cpu(pte
);
1901 if (pte
& PG_PRESENT_MASK
) {
1902 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1912 static void tlb_info_pae32(Monitor
*mon
, CPUState
*env
)
1914 unsigned int l1
, l2
, l3
;
1915 uint64_t pdpe
, pde
, pte
;
1916 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1918 pdp_addr
= env
->cr
[3] & ~0x1f;
1919 for (l1
= 0; l1
< 4; l1
++) {
1920 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1921 pdpe
= le64_to_cpu(pdpe
);
1922 if (pdpe
& PG_PRESENT_MASK
) {
1923 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1924 for (l2
= 0; l2
< 512; l2
++) {
1925 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1926 pde
= le64_to_cpu(pde
);
1927 if (pde
& PG_PRESENT_MASK
) {
1928 if (pde
& PG_PSE_MASK
) {
1929 /* 2M pages with PAE, CR4.PSE is ignored */
1930 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1931 ~((target_phys_addr_t
)(1 << 20) - 1));
1933 pt_addr
= pde
& 0x3fffffffff000ULL
;
1934 for (l3
= 0; l3
< 512; l3
++) {
1935 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1936 pte
= le64_to_cpu(pte
);
1937 if (pte
& PG_PRESENT_MASK
) {
1938 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1941 ~(target_phys_addr_t
)0xfff);
1951 #ifdef TARGET_X86_64
1952 static void tlb_info_64(Monitor
*mon
, CPUState
*env
)
1954 uint64_t l1
, l2
, l3
, l4
;
1955 uint64_t pml4e
, pdpe
, pde
, pte
;
1956 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1958 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1959 for (l1
= 0; l1
< 512; l1
++) {
1960 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1961 pml4e
= le64_to_cpu(pml4e
);
1962 if (pml4e
& PG_PRESENT_MASK
) {
1963 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1964 for (l2
= 0; l2
< 512; l2
++) {
1965 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1966 pdpe
= le64_to_cpu(pdpe
);
1967 if (pdpe
& PG_PRESENT_MASK
) {
1968 if (pdpe
& PG_PSE_MASK
) {
1969 /* 1G pages, CR4.PSE is ignored */
1970 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1971 0x3ffffc0000000ULL
);
1973 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1974 for (l3
= 0; l3
< 512; l3
++) {
1975 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1976 pde
= le64_to_cpu(pde
);
1977 if (pde
& PG_PRESENT_MASK
) {
1978 if (pde
& PG_PSE_MASK
) {
1979 /* 2M pages, CR4.PSE is ignored */
1980 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1982 0x3ffffffe00000ULL
);
1984 pt_addr
= pde
& 0x3fffffffff000ULL
;
1985 for (l4
= 0; l4
< 512; l4
++) {
1986 cpu_physical_memory_read(pt_addr
1989 pte
= le64_to_cpu(pte
);
1990 if (pte
& PG_PRESENT_MASK
) {
1991 print_pte(mon
, (l1
<< 39) +
1993 (l3
<< 21) + (l4
<< 12),
1995 0x3fffffffff000ULL
);
2009 static void tlb_info(Monitor
*mon
)
2013 env
= mon_get_cpu();
2015 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2016 monitor_printf(mon
, "PG disabled\n");
2019 if (env
->cr
[4] & CR4_PAE_MASK
) {
2020 #ifdef TARGET_X86_64
2021 if (env
->hflags
& HF_LMA_MASK
) {
2022 tlb_info_64(mon
, env
);
2026 tlb_info_pae32(mon
, env
);
2029 tlb_info_32(mon
, env
);
2033 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
2035 target_phys_addr_t end
, int prot
)
2038 prot1
= *plast_prot
;
2039 if (prot
!= prot1
) {
2040 if (*pstart
!= -1) {
2041 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
2042 TARGET_FMT_plx
" %c%c%c\n",
2043 *pstart
, end
, end
- *pstart
,
2044 prot1
& PG_USER_MASK
? 'u' : '-',
2046 prot1
& PG_RW_MASK
? 'w' : '-');
2056 static void mem_info_32(Monitor
*mon
, CPUState
*env
)
2058 unsigned int l1
, l2
;
2059 int prot
, last_prot
;
2060 uint32_t pgd
, pde
, pte
;
2061 target_phys_addr_t start
, end
;
2063 pgd
= env
->cr
[3] & ~0xfff;
2066 for(l1
= 0; l1
< 1024; l1
++) {
2067 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2068 pde
= le32_to_cpu(pde
);
2070 if (pde
& PG_PRESENT_MASK
) {
2071 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2072 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2073 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2075 for(l2
= 0; l2
< 1024; l2
++) {
2076 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2077 pte
= le32_to_cpu(pte
);
2078 end
= (l1
<< 22) + (l2
<< 12);
2079 if (pte
& PG_PRESENT_MASK
) {
2081 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2085 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2090 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2093 /* Flush last range */
2094 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2097 static void mem_info_pae32(Monitor
*mon
, CPUState
*env
)
2099 unsigned int l1
, l2
, l3
;
2100 int prot
, last_prot
;
2101 uint64_t pdpe
, pde
, pte
;
2102 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2103 target_phys_addr_t start
, end
;
2105 pdp_addr
= env
->cr
[3] & ~0x1f;
2108 for (l1
= 0; l1
< 4; l1
++) {
2109 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2110 pdpe
= le64_to_cpu(pdpe
);
2112 if (pdpe
& PG_PRESENT_MASK
) {
2113 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2114 for (l2
= 0; l2
< 512; l2
++) {
2115 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2116 pde
= le64_to_cpu(pde
);
2117 end
= (l1
<< 30) + (l2
<< 21);
2118 if (pde
& PG_PRESENT_MASK
) {
2119 if (pde
& PG_PSE_MASK
) {
2120 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2122 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2124 pt_addr
= pde
& 0x3fffffffff000ULL
;
2125 for (l3
= 0; l3
< 512; l3
++) {
2126 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2127 pte
= le64_to_cpu(pte
);
2128 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
2129 if (pte
& PG_PRESENT_MASK
) {
2130 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
2135 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2140 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2145 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2148 /* Flush last range */
2149 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2153 #ifdef TARGET_X86_64
2154 static void mem_info_64(Monitor
*mon
, CPUState
*env
)
2156 int prot
, last_prot
;
2157 uint64_t l1
, l2
, l3
, l4
;
2158 uint64_t pml4e
, pdpe
, pde
, pte
;
2159 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
2161 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2164 for (l1
= 0; l1
< 512; l1
++) {
2165 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2166 pml4e
= le64_to_cpu(pml4e
);
2168 if (pml4e
& PG_PRESENT_MASK
) {
2169 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2170 for (l2
= 0; l2
< 512; l2
++) {
2171 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2172 pdpe
= le64_to_cpu(pdpe
);
2173 end
= (l1
<< 39) + (l2
<< 30);
2174 if (pdpe
& PG_PRESENT_MASK
) {
2175 if (pdpe
& PG_PSE_MASK
) {
2176 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
2179 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2181 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2182 for (l3
= 0; l3
< 512; l3
++) {
2183 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2184 pde
= le64_to_cpu(pde
);
2185 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
2186 if (pde
& PG_PRESENT_MASK
) {
2187 if (pde
& PG_PSE_MASK
) {
2188 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2190 prot
&= pml4e
& pdpe
;
2191 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2193 pt_addr
= pde
& 0x3fffffffff000ULL
;
2194 for (l4
= 0; l4
< 512; l4
++) {
2195 cpu_physical_memory_read(pt_addr
2198 pte
= le64_to_cpu(pte
);
2199 end
= (l1
<< 39) + (l2
<< 30) +
2200 (l3
<< 21) + (l4
<< 12);
2201 if (pte
& PG_PRESENT_MASK
) {
2202 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
2204 prot
&= pml4e
& pdpe
& pde
;
2208 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2213 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2219 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2224 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2227 /* Flush last range */
2228 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 48, 0);
2232 static void mem_info(Monitor
*mon
)
2236 env
= mon_get_cpu();
2238 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2239 monitor_printf(mon
, "PG disabled\n");
2242 if (env
->cr
[4] & CR4_PAE_MASK
) {
2243 #ifdef TARGET_X86_64
2244 if (env
->hflags
& HF_LMA_MASK
) {
2245 mem_info_64(mon
, env
);
2249 mem_info_pae32(mon
, env
);
2252 mem_info_32(mon
, env
);
2257 #if defined(TARGET_SH4)
2259 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2261 monitor_printf(mon
, " tlb%i:\t"
2262 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2263 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2264 "dirty=%hhu writethrough=%hhu\n",
2266 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2267 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2271 static void tlb_info(Monitor
*mon
)
2273 CPUState
*env
= mon_get_cpu();
2276 monitor_printf (mon
, "ITLB:\n");
2277 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2278 print_tlb (mon
, i
, &env
->itlb
[i
]);
2279 monitor_printf (mon
, "UTLB:\n");
2280 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2281 print_tlb (mon
, i
, &env
->utlb
[i
]);
2286 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
2287 static void tlb_info(Monitor
*mon
)
2289 CPUState
*env1
= mon_get_cpu();
2291 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
2295 static void do_info_mtree(Monitor
*mon
)
2297 mtree_info((fprintf_function
)monitor_printf
, mon
);
2300 static void do_info_numa(Monitor
*mon
)
2305 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2306 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2307 monitor_printf(mon
, "node %d cpus:", i
);
2308 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2309 if (env
->numa_node
== i
) {
2310 monitor_printf(mon
, " %d", env
->cpu_index
);
2313 monitor_printf(mon
, "\n");
2314 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2319 #ifdef CONFIG_PROFILER
2324 static void do_info_profile(Monitor
*mon
)
2330 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2331 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2332 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2333 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2338 static void do_info_profile(Monitor
*mon
)
2340 monitor_printf(mon
, "Internal profiler not compiled\n");
2344 /* Capture support */
2345 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2347 static void do_info_capture(Monitor
*mon
)
2352 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2353 monitor_printf(mon
, "[%d]: ", i
);
2354 s
->ops
.info (s
->opaque
);
2359 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2362 int n
= qdict_get_int(qdict
, "n");
2365 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2367 s
->ops
.destroy (s
->opaque
);
2368 QLIST_REMOVE (s
, entries
);
2375 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2377 const char *path
= qdict_get_str(qdict
, "path");
2378 int has_freq
= qdict_haskey(qdict
, "freq");
2379 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2380 int has_bits
= qdict_haskey(qdict
, "bits");
2381 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2382 int has_channels
= qdict_haskey(qdict
, "nchannels");
2383 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2386 s
= g_malloc0 (sizeof (*s
));
2388 freq
= has_freq
? freq
: 44100;
2389 bits
= has_bits
? bits
: 16;
2390 nchannels
= has_channels
? nchannels
: 2;
2392 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2393 monitor_printf(mon
, "Failed to add wave capture\n");
2397 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2401 #if defined(TARGET_I386)
2402 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2406 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2407 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2413 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2415 qerror_report(QERR_UNSUPPORTED
);
2420 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2422 qemu_acl
*acl
= qemu_acl_find(name
);
2425 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2430 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2432 const char *aclname
= qdict_get_str(qdict
, "aclname");
2433 qemu_acl
*acl
= find_acl(mon
, aclname
);
2434 qemu_acl_entry
*entry
;
2438 monitor_printf(mon
, "policy: %s\n",
2439 acl
->defaultDeny
? "deny" : "allow");
2440 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2442 monitor_printf(mon
, "%d: %s %s\n", i
,
2443 entry
->deny
? "deny" : "allow", entry
->match
);
2448 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2450 const char *aclname
= qdict_get_str(qdict
, "aclname");
2451 qemu_acl
*acl
= find_acl(mon
, aclname
);
2454 qemu_acl_reset(acl
);
2455 monitor_printf(mon
, "acl: removed all rules\n");
2459 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2461 const char *aclname
= qdict_get_str(qdict
, "aclname");
2462 const char *policy
= qdict_get_str(qdict
, "policy");
2463 qemu_acl
*acl
= find_acl(mon
, aclname
);
2466 if (strcmp(policy
, "allow") == 0) {
2467 acl
->defaultDeny
= 0;
2468 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2469 } else if (strcmp(policy
, "deny") == 0) {
2470 acl
->defaultDeny
= 1;
2471 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2473 monitor_printf(mon
, "acl: unknown policy '%s', "
2474 "expected 'deny' or 'allow'\n", policy
);
2479 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2481 const char *aclname
= qdict_get_str(qdict
, "aclname");
2482 const char *match
= qdict_get_str(qdict
, "match");
2483 const char *policy
= qdict_get_str(qdict
, "policy");
2484 int has_index
= qdict_haskey(qdict
, "index");
2485 int index
= qdict_get_try_int(qdict
, "index", -1);
2486 qemu_acl
*acl
= find_acl(mon
, aclname
);
2490 if (strcmp(policy
, "allow") == 0) {
2492 } else if (strcmp(policy
, "deny") == 0) {
2495 monitor_printf(mon
, "acl: unknown policy '%s', "
2496 "expected 'deny' or 'allow'\n", policy
);
2500 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2502 ret
= qemu_acl_append(acl
, deny
, match
);
2504 monitor_printf(mon
, "acl: unable to add acl entry\n");
2506 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2510 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2512 const char *aclname
= qdict_get_str(qdict
, "aclname");
2513 const char *match
= qdict_get_str(qdict
, "match");
2514 qemu_acl
*acl
= find_acl(mon
, aclname
);
2518 ret
= qemu_acl_remove(acl
, match
);
2520 monitor_printf(mon
, "acl: no matching acl entry\n");
2522 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2526 #if defined(TARGET_I386)
2527 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2530 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2531 int bank
= qdict_get_int(qdict
, "bank");
2532 uint64_t status
= qdict_get_int(qdict
, "status");
2533 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2534 uint64_t addr
= qdict_get_int(qdict
, "addr");
2535 uint64_t misc
= qdict_get_int(qdict
, "misc");
2536 int flags
= MCE_INJECT_UNCOND_AO
;
2538 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2539 flags
|= MCE_INJECT_BROADCAST
;
2541 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
2542 if (cenv
->cpu_index
== cpu_index
) {
2543 cpu_x86_inject_mce(mon
, cenv
, bank
, status
, mcg_status
, addr
, misc
,
2551 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2553 const char *fdname
= qdict_get_str(qdict
, "fdname");
2557 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2559 qerror_report(QERR_FD_NOT_SUPPLIED
);
2563 if (qemu_isdigit(fdname
[0])) {
2564 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2565 "a name not starting with a digit");
2569 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2570 if (strcmp(monfd
->name
, fdname
) != 0) {
2579 monfd
= g_malloc0(sizeof(mon_fd_t
));
2580 monfd
->name
= g_strdup(fdname
);
2583 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2587 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2589 const char *fdname
= qdict_get_str(qdict
, "fdname");
2592 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2593 if (strcmp(monfd
->name
, fdname
) != 0) {
2597 QLIST_REMOVE(monfd
, next
);
2599 g_free(monfd
->name
);
2604 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2608 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2610 int saved_vm_running
= runstate_is_running();
2611 const char *name
= qdict_get_str(qdict
, "name");
2613 vm_stop(RUN_STATE_RESTORE_VM
);
2615 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2620 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2624 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2627 if (strcmp(monfd
->name
, fdname
) != 0) {
2633 /* caller takes ownership of fd */
2634 QLIST_REMOVE(monfd
, next
);
2635 g_free(monfd
->name
);
2644 static const mon_cmd_t mon_cmds
[] = {
2645 #include "hmp-commands.h"
2649 /* Please update hmp-commands.hx when adding or changing commands */
2650 static const mon_cmd_t info_cmds
[] = {
2655 .help
= "show the version of QEMU",
2656 .mhandler
.info
= hmp_info_version
,
2662 .help
= "show the network state",
2663 .mhandler
.info
= do_info_network
,
2669 .help
= "show the character devices",
2670 .mhandler
.info
= hmp_info_chardev
,
2676 .help
= "show the block devices",
2677 .mhandler
.info
= hmp_info_block
,
2680 .name
= "blockstats",
2683 .help
= "show block device statistics",
2684 .mhandler
.info
= hmp_info_blockstats
,
2687 .name
= "registers",
2690 .help
= "show the cpu registers",
2691 .mhandler
.info
= do_info_registers
,
2697 .help
= "show infos for each CPU",
2698 .mhandler
.info
= hmp_info_cpus
,
2704 .help
= "show the command line history",
2705 .mhandler
.info
= do_info_history
,
2707 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2708 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2713 .help
= "show the interrupts statistics (if available)",
2715 .mhandler
.info
= sun4m_irq_info
,
2716 #elif defined(TARGET_LM32)
2717 .mhandler
.info
= lm32_irq_info
,
2719 .mhandler
.info
= irq_info
,
2726 .help
= "show i8259 (PIC) state",
2728 .mhandler
.info
= sun4m_pic_info
,
2729 #elif defined(TARGET_LM32)
2730 .mhandler
.info
= lm32_do_pic_info
,
2732 .mhandler
.info
= pic_info
,
2740 .help
= "show PCI info",
2741 .user_print
= do_pci_info_print
,
2742 .mhandler
.info_new
= do_pci_info
,
2744 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2750 .help
= "show virtual to physical memory mappings",
2751 .mhandler
.info
= tlb_info
,
2754 #if defined(TARGET_I386)
2759 .help
= "show the active virtual memory mappings",
2760 .mhandler
.info
= mem_info
,
2767 .help
= "show memory tree",
2768 .mhandler
.info
= do_info_mtree
,
2774 .help
= "show dynamic compiler info",
2775 .mhandler
.info
= do_info_jit
,
2781 .help
= "show KVM information",
2782 .mhandler
.info
= hmp_info_kvm
,
2788 .help
= "show NUMA information",
2789 .mhandler
.info
= do_info_numa
,
2795 .help
= "show guest USB devices",
2796 .mhandler
.info
= usb_info
,
2802 .help
= "show host USB devices",
2803 .mhandler
.info
= usb_host_info
,
2809 .help
= "show profiling information",
2810 .mhandler
.info
= do_info_profile
,
2816 .help
= "show capture information",
2817 .mhandler
.info
= do_info_capture
,
2820 .name
= "snapshots",
2823 .help
= "show the currently saved VM snapshots",
2824 .mhandler
.info
= do_info_snapshots
,
2830 .help
= "show the current VM status (running|paused)",
2831 .mhandler
.info
= hmp_info_status
,
2837 .help
= "show guest PCMCIA status",
2838 .mhandler
.info
= pcmcia_info
,
2844 .help
= "show which guest mouse is receiving events",
2845 .mhandler
.info
= hmp_info_mice
,
2851 .help
= "show the vnc server status",
2852 .user_print
= do_info_vnc_print
,
2853 .mhandler
.info_new
= do_info_vnc
,
2855 #if defined(CONFIG_SPICE)
2860 .help
= "show the spice server status",
2861 .user_print
= do_info_spice_print
,
2862 .mhandler
.info_new
= do_info_spice
,
2869 .help
= "show the current VM name",
2870 .mhandler
.info
= hmp_info_name
,
2876 .help
= "show the current VM UUID",
2877 .mhandler
.info
= hmp_info_uuid
,
2879 #if defined(TARGET_PPC)
2884 .help
= "show CPU statistics",
2885 .mhandler
.info
= do_info_cpu_stats
,
2888 #if defined(CONFIG_SLIRP)
2893 .help
= "show user network stack connection states",
2894 .mhandler
.info
= do_info_usernet
,
2901 .help
= "show migration status",
2902 .mhandler
.info
= hmp_info_migrate
,
2908 .help
= "show balloon information",
2909 .user_print
= monitor_print_balloon
,
2910 .mhandler
.info_async
= do_info_balloon
,
2911 .flags
= MONITOR_CMD_ASYNC
,
2917 .help
= "show device tree",
2918 .mhandler
.info
= do_info_qtree
,
2924 .help
= "show qdev device model list",
2925 .mhandler
.info
= do_info_qdm
,
2931 .help
= "show roms",
2932 .mhandler
.info
= do_info_roms
,
2934 #if defined(CONFIG_TRACE_SIMPLE)
2939 .help
= "show current contents of trace buffer",
2940 .mhandler
.info
= do_info_trace
,
2944 .name
= "trace-events",
2947 .help
= "show available trace-events & their state",
2948 .mhandler
.info
= do_trace_print_events
,
2955 static const mon_cmd_t qmp_cmds
[] = {
2956 #include "qmp-commands-old.h"
2960 static const mon_cmd_t qmp_query_cmds
[] = {
2965 .help
= "show PCI info",
2966 .user_print
= do_pci_info_print
,
2967 .mhandler
.info_new
= do_pci_info
,
2973 .help
= "show the vnc server status",
2974 .user_print
= do_info_vnc_print
,
2975 .mhandler
.info_new
= do_info_vnc
,
2977 #if defined(CONFIG_SPICE)
2982 .help
= "show the spice server status",
2983 .user_print
= do_info_spice_print
,
2984 .mhandler
.info_new
= do_info_spice
,
2991 .help
= "show balloon information",
2992 .user_print
= monitor_print_balloon
,
2993 .mhandler
.info_async
= do_info_balloon
,
2994 .flags
= MONITOR_CMD_ASYNC
,
2999 /*******************************************************************/
3001 static const char *pch
;
3002 static jmp_buf expr_env
;
3007 typedef struct MonitorDef
{
3010 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
3014 #if defined(TARGET_I386)
3015 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
3017 CPUState
*env
= mon_get_cpu();
3018 return env
->eip
+ env
->segs
[R_CS
].base
;
3022 #if defined(TARGET_PPC)
3023 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3025 CPUState
*env
= mon_get_cpu();
3030 for (i
= 0; i
< 8; i
++)
3031 u
|= env
->crf
[i
] << (32 - (4 * i
));
3036 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3038 CPUState
*env
= mon_get_cpu();
3042 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3044 CPUState
*env
= mon_get_cpu();
3048 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3050 CPUState
*env
= mon_get_cpu();
3051 return cpu_ppc_load_decr(env
);
3054 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3056 CPUState
*env
= mon_get_cpu();
3057 return cpu_ppc_load_tbu(env
);
3060 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3062 CPUState
*env
= mon_get_cpu();
3063 return cpu_ppc_load_tbl(env
);
3067 #if defined(TARGET_SPARC)
3068 #ifndef TARGET_SPARC64
3069 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3071 CPUState
*env
= mon_get_cpu();
3073 return cpu_get_psr(env
);
3077 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3079 CPUState
*env
= mon_get_cpu();
3080 return env
->regwptr
[val
];
3084 static const MonitorDef monitor_defs
[] = {
3087 #define SEG(name, seg) \
3088 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3089 { name ".base", offsetof(CPUState, segs[seg].base) },\
3090 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3092 { "eax", offsetof(CPUState
, regs
[0]) },
3093 { "ecx", offsetof(CPUState
, regs
[1]) },
3094 { "edx", offsetof(CPUState
, regs
[2]) },
3095 { "ebx", offsetof(CPUState
, regs
[3]) },
3096 { "esp|sp", offsetof(CPUState
, regs
[4]) },
3097 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
3098 { "esi", offsetof(CPUState
, regs
[6]) },
3099 { "edi", offsetof(CPUState
, regs
[7]) },
3100 #ifdef TARGET_X86_64
3101 { "r8", offsetof(CPUState
, regs
[8]) },
3102 { "r9", offsetof(CPUState
, regs
[9]) },
3103 { "r10", offsetof(CPUState
, regs
[10]) },
3104 { "r11", offsetof(CPUState
, regs
[11]) },
3105 { "r12", offsetof(CPUState
, regs
[12]) },
3106 { "r13", offsetof(CPUState
, regs
[13]) },
3107 { "r14", offsetof(CPUState
, regs
[14]) },
3108 { "r15", offsetof(CPUState
, regs
[15]) },
3110 { "eflags", offsetof(CPUState
, eflags
) },
3111 { "eip", offsetof(CPUState
, eip
) },
3118 { "pc", 0, monitor_get_pc
, },
3119 #elif defined(TARGET_PPC)
3120 /* General purpose registers */
3121 { "r0", offsetof(CPUState
, gpr
[0]) },
3122 { "r1", offsetof(CPUState
, gpr
[1]) },
3123 { "r2", offsetof(CPUState
, gpr
[2]) },
3124 { "r3", offsetof(CPUState
, gpr
[3]) },
3125 { "r4", offsetof(CPUState
, gpr
[4]) },
3126 { "r5", offsetof(CPUState
, gpr
[5]) },
3127 { "r6", offsetof(CPUState
, gpr
[6]) },
3128 { "r7", offsetof(CPUState
, gpr
[7]) },
3129 { "r8", offsetof(CPUState
, gpr
[8]) },
3130 { "r9", offsetof(CPUState
, gpr
[9]) },
3131 { "r10", offsetof(CPUState
, gpr
[10]) },
3132 { "r11", offsetof(CPUState
, gpr
[11]) },
3133 { "r12", offsetof(CPUState
, gpr
[12]) },
3134 { "r13", offsetof(CPUState
, gpr
[13]) },
3135 { "r14", offsetof(CPUState
, gpr
[14]) },
3136 { "r15", offsetof(CPUState
, gpr
[15]) },
3137 { "r16", offsetof(CPUState
, gpr
[16]) },
3138 { "r17", offsetof(CPUState
, gpr
[17]) },
3139 { "r18", offsetof(CPUState
, gpr
[18]) },
3140 { "r19", offsetof(CPUState
, gpr
[19]) },
3141 { "r20", offsetof(CPUState
, gpr
[20]) },
3142 { "r21", offsetof(CPUState
, gpr
[21]) },
3143 { "r22", offsetof(CPUState
, gpr
[22]) },
3144 { "r23", offsetof(CPUState
, gpr
[23]) },
3145 { "r24", offsetof(CPUState
, gpr
[24]) },
3146 { "r25", offsetof(CPUState
, gpr
[25]) },
3147 { "r26", offsetof(CPUState
, gpr
[26]) },
3148 { "r27", offsetof(CPUState
, gpr
[27]) },
3149 { "r28", offsetof(CPUState
, gpr
[28]) },
3150 { "r29", offsetof(CPUState
, gpr
[29]) },
3151 { "r30", offsetof(CPUState
, gpr
[30]) },
3152 { "r31", offsetof(CPUState
, gpr
[31]) },
3153 /* Floating point registers */
3154 { "f0", offsetof(CPUState
, fpr
[0]) },
3155 { "f1", offsetof(CPUState
, fpr
[1]) },
3156 { "f2", offsetof(CPUState
, fpr
[2]) },
3157 { "f3", offsetof(CPUState
, fpr
[3]) },
3158 { "f4", offsetof(CPUState
, fpr
[4]) },
3159 { "f5", offsetof(CPUState
, fpr
[5]) },
3160 { "f6", offsetof(CPUState
, fpr
[6]) },
3161 { "f7", offsetof(CPUState
, fpr
[7]) },
3162 { "f8", offsetof(CPUState
, fpr
[8]) },
3163 { "f9", offsetof(CPUState
, fpr
[9]) },
3164 { "f10", offsetof(CPUState
, fpr
[10]) },
3165 { "f11", offsetof(CPUState
, fpr
[11]) },
3166 { "f12", offsetof(CPUState
, fpr
[12]) },
3167 { "f13", offsetof(CPUState
, fpr
[13]) },
3168 { "f14", offsetof(CPUState
, fpr
[14]) },
3169 { "f15", offsetof(CPUState
, fpr
[15]) },
3170 { "f16", offsetof(CPUState
, fpr
[16]) },
3171 { "f17", offsetof(CPUState
, fpr
[17]) },
3172 { "f18", offsetof(CPUState
, fpr
[18]) },
3173 { "f19", offsetof(CPUState
, fpr
[19]) },
3174 { "f20", offsetof(CPUState
, fpr
[20]) },
3175 { "f21", offsetof(CPUState
, fpr
[21]) },
3176 { "f22", offsetof(CPUState
, fpr
[22]) },
3177 { "f23", offsetof(CPUState
, fpr
[23]) },
3178 { "f24", offsetof(CPUState
, fpr
[24]) },
3179 { "f25", offsetof(CPUState
, fpr
[25]) },
3180 { "f26", offsetof(CPUState
, fpr
[26]) },
3181 { "f27", offsetof(CPUState
, fpr
[27]) },
3182 { "f28", offsetof(CPUState
, fpr
[28]) },
3183 { "f29", offsetof(CPUState
, fpr
[29]) },
3184 { "f30", offsetof(CPUState
, fpr
[30]) },
3185 { "f31", offsetof(CPUState
, fpr
[31]) },
3186 { "fpscr", offsetof(CPUState
, fpscr
) },
3187 /* Next instruction pointer */
3188 { "nip|pc", offsetof(CPUState
, nip
) },
3189 { "lr", offsetof(CPUState
, lr
) },
3190 { "ctr", offsetof(CPUState
, ctr
) },
3191 { "decr", 0, &monitor_get_decr
, },
3192 { "ccr", 0, &monitor_get_ccr
, },
3193 /* Machine state register */
3194 { "msr", 0, &monitor_get_msr
, },
3195 { "xer", 0, &monitor_get_xer
, },
3196 { "tbu", 0, &monitor_get_tbu
, },
3197 { "tbl", 0, &monitor_get_tbl
, },
3198 #if defined(TARGET_PPC64)
3199 /* Address space register */
3200 { "asr", offsetof(CPUState
, asr
) },
3202 /* Segment registers */
3203 { "sdr1", offsetof(CPUState
, spr
[SPR_SDR1
]) },
3204 { "sr0", offsetof(CPUState
, sr
[0]) },
3205 { "sr1", offsetof(CPUState
, sr
[1]) },
3206 { "sr2", offsetof(CPUState
, sr
[2]) },
3207 { "sr3", offsetof(CPUState
, sr
[3]) },
3208 { "sr4", offsetof(CPUState
, sr
[4]) },
3209 { "sr5", offsetof(CPUState
, sr
[5]) },
3210 { "sr6", offsetof(CPUState
, sr
[6]) },
3211 { "sr7", offsetof(CPUState
, sr
[7]) },
3212 { "sr8", offsetof(CPUState
, sr
[8]) },
3213 { "sr9", offsetof(CPUState
, sr
[9]) },
3214 { "sr10", offsetof(CPUState
, sr
[10]) },
3215 { "sr11", offsetof(CPUState
, sr
[11]) },
3216 { "sr12", offsetof(CPUState
, sr
[12]) },
3217 { "sr13", offsetof(CPUState
, sr
[13]) },
3218 { "sr14", offsetof(CPUState
, sr
[14]) },
3219 { "sr15", offsetof(CPUState
, sr
[15]) },
3220 /* Too lazy to put BATs... */
3221 { "pvr", offsetof(CPUState
, spr
[SPR_PVR
]) },
3223 { "srr0", offsetof(CPUState
, spr
[SPR_SRR0
]) },
3224 { "srr1", offsetof(CPUState
, spr
[SPR_SRR1
]) },
3225 { "sprg0", offsetof(CPUState
, spr
[SPR_SPRG0
]) },
3226 { "sprg1", offsetof(CPUState
, spr
[SPR_SPRG1
]) },
3227 { "sprg2", offsetof(CPUState
, spr
[SPR_SPRG2
]) },
3228 { "sprg3", offsetof(CPUState
, spr
[SPR_SPRG3
]) },
3229 { "sprg4", offsetof(CPUState
, spr
[SPR_SPRG4
]) },
3230 { "sprg5", offsetof(CPUState
, spr
[SPR_SPRG5
]) },
3231 { "sprg6", offsetof(CPUState
, spr
[SPR_SPRG6
]) },
3232 { "sprg7", offsetof(CPUState
, spr
[SPR_SPRG7
]) },
3233 { "pid", offsetof(CPUState
, spr
[SPR_BOOKE_PID
]) },
3234 { "csrr0", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR0
]) },
3235 { "csrr1", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR1
]) },
3236 { "esr", offsetof(CPUState
, spr
[SPR_BOOKE_ESR
]) },
3237 { "dear", offsetof(CPUState
, spr
[SPR_BOOKE_DEAR
]) },
3238 { "mcsr", offsetof(CPUState
, spr
[SPR_BOOKE_MCSR
]) },
3239 { "tsr", offsetof(CPUState
, spr
[SPR_BOOKE_TSR
]) },
3240 { "tcr", offsetof(CPUState
, spr
[SPR_BOOKE_TCR
]) },
3241 { "vrsave", offsetof(CPUState
, spr
[SPR_VRSAVE
]) },
3242 { "pir", offsetof(CPUState
, spr
[SPR_BOOKE_PIR
]) },
3243 { "mcsrr0", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR0
]) },
3244 { "mcsrr1", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR1
]) },
3245 { "decar", offsetof(CPUState
, spr
[SPR_BOOKE_DECAR
]) },
3246 { "ivpr", offsetof(CPUState
, spr
[SPR_BOOKE_IVPR
]) },
3247 { "epcr", offsetof(CPUState
, spr
[SPR_BOOKE_EPCR
]) },
3248 { "sprg8", offsetof(CPUState
, spr
[SPR_BOOKE_SPRG8
]) },
3249 { "ivor0", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR0
]) },
3250 { "ivor1", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR1
]) },
3251 { "ivor2", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR2
]) },
3252 { "ivor3", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR3
]) },
3253 { "ivor4", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR4
]) },
3254 { "ivor5", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR5
]) },
3255 { "ivor6", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR6
]) },
3256 { "ivor7", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR7
]) },
3257 { "ivor8", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR8
]) },
3258 { "ivor9", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR9
]) },
3259 { "ivor10", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR10
]) },
3260 { "ivor11", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR11
]) },
3261 { "ivor12", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR12
]) },
3262 { "ivor13", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR13
]) },
3263 { "ivor14", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR14
]) },
3264 { "ivor15", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR15
]) },
3265 { "ivor32", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR32
]) },
3266 { "ivor33", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR33
]) },
3267 { "ivor34", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR34
]) },
3268 { "ivor35", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR35
]) },
3269 { "ivor36", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR36
]) },
3270 { "ivor37", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR37
]) },
3271 { "mas0", offsetof(CPUState
, spr
[SPR_BOOKE_MAS0
]) },
3272 { "mas1", offsetof(CPUState
, spr
[SPR_BOOKE_MAS1
]) },
3273 { "mas2", offsetof(CPUState
, spr
[SPR_BOOKE_MAS2
]) },
3274 { "mas3", offsetof(CPUState
, spr
[SPR_BOOKE_MAS3
]) },
3275 { "mas4", offsetof(CPUState
, spr
[SPR_BOOKE_MAS4
]) },
3276 { "mas6", offsetof(CPUState
, spr
[SPR_BOOKE_MAS6
]) },
3277 { "mas7", offsetof(CPUState
, spr
[SPR_BOOKE_MAS7
]) },
3278 { "mmucfg", offsetof(CPUState
, spr
[SPR_MMUCFG
]) },
3279 { "tlb0cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3280 { "tlb1cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3281 { "epr", offsetof(CPUState
, spr
[SPR_BOOKE_EPR
]) },
3282 { "eplc", offsetof(CPUState
, spr
[SPR_BOOKE_EPLC
]) },
3283 { "epsc", offsetof(CPUState
, spr
[SPR_BOOKE_EPSC
]) },
3284 { "svr", offsetof(CPUState
, spr
[SPR_E500_SVR
]) },
3285 { "mcar", offsetof(CPUState
, spr
[SPR_Exxx_MCAR
]) },
3286 { "pid1", offsetof(CPUState
, spr
[SPR_BOOKE_PID1
]) },
3287 { "pid2", offsetof(CPUState
, spr
[SPR_BOOKE_PID2
]) },
3288 { "hid0", offsetof(CPUState
, spr
[SPR_HID0
]) },
3290 #elif defined(TARGET_SPARC)
3291 { "g0", offsetof(CPUState
, gregs
[0]) },
3292 { "g1", offsetof(CPUState
, gregs
[1]) },
3293 { "g2", offsetof(CPUState
, gregs
[2]) },
3294 { "g3", offsetof(CPUState
, gregs
[3]) },
3295 { "g4", offsetof(CPUState
, gregs
[4]) },
3296 { "g5", offsetof(CPUState
, gregs
[5]) },
3297 { "g6", offsetof(CPUState
, gregs
[6]) },
3298 { "g7", offsetof(CPUState
, gregs
[7]) },
3299 { "o0", 0, monitor_get_reg
},
3300 { "o1", 1, monitor_get_reg
},
3301 { "o2", 2, monitor_get_reg
},
3302 { "o3", 3, monitor_get_reg
},
3303 { "o4", 4, monitor_get_reg
},
3304 { "o5", 5, monitor_get_reg
},
3305 { "o6", 6, monitor_get_reg
},
3306 { "o7", 7, monitor_get_reg
},
3307 { "l0", 8, monitor_get_reg
},
3308 { "l1", 9, monitor_get_reg
},
3309 { "l2", 10, monitor_get_reg
},
3310 { "l3", 11, monitor_get_reg
},
3311 { "l4", 12, monitor_get_reg
},
3312 { "l5", 13, monitor_get_reg
},
3313 { "l6", 14, monitor_get_reg
},
3314 { "l7", 15, monitor_get_reg
},
3315 { "i0", 16, monitor_get_reg
},
3316 { "i1", 17, monitor_get_reg
},
3317 { "i2", 18, monitor_get_reg
},
3318 { "i3", 19, monitor_get_reg
},
3319 { "i4", 20, monitor_get_reg
},
3320 { "i5", 21, monitor_get_reg
},
3321 { "i6", 22, monitor_get_reg
},
3322 { "i7", 23, monitor_get_reg
},
3323 { "pc", offsetof(CPUState
, pc
) },
3324 { "npc", offsetof(CPUState
, npc
) },
3325 { "y", offsetof(CPUState
, y
) },
3326 #ifndef TARGET_SPARC64
3327 { "psr", 0, &monitor_get_psr
, },
3328 { "wim", offsetof(CPUState
, wim
) },
3330 { "tbr", offsetof(CPUState
, tbr
) },
3331 { "fsr", offsetof(CPUState
, fsr
) },
3332 { "f0", offsetof(CPUState
, fpr
[0]) },
3333 { "f1", offsetof(CPUState
, fpr
[1]) },
3334 { "f2", offsetof(CPUState
, fpr
[2]) },
3335 { "f3", offsetof(CPUState
, fpr
[3]) },
3336 { "f4", offsetof(CPUState
, fpr
[4]) },
3337 { "f5", offsetof(CPUState
, fpr
[5]) },
3338 { "f6", offsetof(CPUState
, fpr
[6]) },
3339 { "f7", offsetof(CPUState
, fpr
[7]) },
3340 { "f8", offsetof(CPUState
, fpr
[8]) },
3341 { "f9", offsetof(CPUState
, fpr
[9]) },
3342 { "f10", offsetof(CPUState
, fpr
[10]) },
3343 { "f11", offsetof(CPUState
, fpr
[11]) },
3344 { "f12", offsetof(CPUState
, fpr
[12]) },
3345 { "f13", offsetof(CPUState
, fpr
[13]) },
3346 { "f14", offsetof(CPUState
, fpr
[14]) },
3347 { "f15", offsetof(CPUState
, fpr
[15]) },
3348 { "f16", offsetof(CPUState
, fpr
[16]) },
3349 { "f17", offsetof(CPUState
, fpr
[17]) },
3350 { "f18", offsetof(CPUState
, fpr
[18]) },
3351 { "f19", offsetof(CPUState
, fpr
[19]) },
3352 { "f20", offsetof(CPUState
, fpr
[20]) },
3353 { "f21", offsetof(CPUState
, fpr
[21]) },
3354 { "f22", offsetof(CPUState
, fpr
[22]) },
3355 { "f23", offsetof(CPUState
, fpr
[23]) },
3356 { "f24", offsetof(CPUState
, fpr
[24]) },
3357 { "f25", offsetof(CPUState
, fpr
[25]) },
3358 { "f26", offsetof(CPUState
, fpr
[26]) },
3359 { "f27", offsetof(CPUState
, fpr
[27]) },
3360 { "f28", offsetof(CPUState
, fpr
[28]) },
3361 { "f29", offsetof(CPUState
, fpr
[29]) },
3362 { "f30", offsetof(CPUState
, fpr
[30]) },
3363 { "f31", offsetof(CPUState
, fpr
[31]) },
3364 #ifdef TARGET_SPARC64
3365 { "f32", offsetof(CPUState
, fpr
[32]) },
3366 { "f34", offsetof(CPUState
, fpr
[34]) },
3367 { "f36", offsetof(CPUState
, fpr
[36]) },
3368 { "f38", offsetof(CPUState
, fpr
[38]) },
3369 { "f40", offsetof(CPUState
, fpr
[40]) },
3370 { "f42", offsetof(CPUState
, fpr
[42]) },
3371 { "f44", offsetof(CPUState
, fpr
[44]) },
3372 { "f46", offsetof(CPUState
, fpr
[46]) },
3373 { "f48", offsetof(CPUState
, fpr
[48]) },
3374 { "f50", offsetof(CPUState
, fpr
[50]) },
3375 { "f52", offsetof(CPUState
, fpr
[52]) },
3376 { "f54", offsetof(CPUState
, fpr
[54]) },
3377 { "f56", offsetof(CPUState
, fpr
[56]) },
3378 { "f58", offsetof(CPUState
, fpr
[58]) },
3379 { "f60", offsetof(CPUState
, fpr
[60]) },
3380 { "f62", offsetof(CPUState
, fpr
[62]) },
3381 { "asi", offsetof(CPUState
, asi
) },
3382 { "pstate", offsetof(CPUState
, pstate
) },
3383 { "cansave", offsetof(CPUState
, cansave
) },
3384 { "canrestore", offsetof(CPUState
, canrestore
) },
3385 { "otherwin", offsetof(CPUState
, otherwin
) },
3386 { "wstate", offsetof(CPUState
, wstate
) },
3387 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3388 { "fprs", offsetof(CPUState
, fprs
) },
3394 static void expr_error(Monitor
*mon
, const char *msg
)
3396 monitor_printf(mon
, "%s\n", msg
);
3397 longjmp(expr_env
, 1);
3400 /* return 0 if OK, -1 if not found */
3401 static int get_monitor_def(target_long
*pval
, const char *name
)
3403 const MonitorDef
*md
;
3406 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3407 if (compare_cmd(name
, md
->name
)) {
3408 if (md
->get_value
) {
3409 *pval
= md
->get_value(md
, md
->offset
);
3411 CPUState
*env
= mon_get_cpu();
3412 ptr
= (uint8_t *)env
+ md
->offset
;
3415 *pval
= *(int32_t *)ptr
;
3418 *pval
= *(target_long
*)ptr
;
3431 static void next(void)
3435 while (qemu_isspace(*pch
))
3440 static int64_t expr_sum(Monitor
*mon
);
3442 static int64_t expr_unary(Monitor
*mon
)
3451 n
= expr_unary(mon
);
3455 n
= -expr_unary(mon
);
3459 n
= ~expr_unary(mon
);
3465 expr_error(mon
, "')' expected");
3472 expr_error(mon
, "character constant expected");
3476 expr_error(mon
, "missing terminating \' character");
3486 while ((*pch
>= 'a' && *pch
<= 'z') ||
3487 (*pch
>= 'A' && *pch
<= 'Z') ||
3488 (*pch
>= '0' && *pch
<= '9') ||
3489 *pch
== '_' || *pch
== '.') {
3490 if ((q
- buf
) < sizeof(buf
) - 1)
3494 while (qemu_isspace(*pch
))
3497 ret
= get_monitor_def(®
, buf
);
3499 expr_error(mon
, "unknown register");
3504 expr_error(mon
, "unexpected end of expression");
3508 #if TARGET_PHYS_ADDR_BITS > 32
3509 n
= strtoull(pch
, &p
, 0);
3511 n
= strtoul(pch
, &p
, 0);
3514 expr_error(mon
, "invalid char in expression");
3517 while (qemu_isspace(*pch
))
3525 static int64_t expr_prod(Monitor
*mon
)
3530 val
= expr_unary(mon
);
3533 if (op
!= '*' && op
!= '/' && op
!= '%')
3536 val2
= expr_unary(mon
);
3545 expr_error(mon
, "division by zero");
3556 static int64_t expr_logic(Monitor
*mon
)
3561 val
= expr_prod(mon
);
3564 if (op
!= '&' && op
!= '|' && op
!= '^')
3567 val2
= expr_prod(mon
);
3584 static int64_t expr_sum(Monitor
*mon
)
3589 val
= expr_logic(mon
);
3592 if (op
!= '+' && op
!= '-')
3595 val2
= expr_logic(mon
);
3604 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3607 if (setjmp(expr_env
)) {
3611 while (qemu_isspace(*pch
))
3613 *pval
= expr_sum(mon
);
3618 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3620 const char *p
= *pp
;
3624 d
= strtod(p
, &tailp
);
3626 monitor_printf(mon
, "Number expected\n");
3629 if (d
!= d
|| d
- d
!= 0) {
3630 /* NaN or infinity */
3631 monitor_printf(mon
, "Bad number\n");
3639 static int get_str(char *buf
, int buf_size
, const char **pp
)
3647 while (qemu_isspace(*p
))
3657 while (*p
!= '\0' && *p
!= '\"') {
3673 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3676 if ((q
- buf
) < buf_size
- 1) {
3680 if ((q
- buf
) < buf_size
- 1) {
3687 qemu_printf("unterminated string\n");
3692 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3693 if ((q
- buf
) < buf_size
- 1) {
3705 * Store the command-name in cmdname, and return a pointer to
3706 * the remaining of the command string.
3708 static const char *get_command_name(const char *cmdline
,
3709 char *cmdname
, size_t nlen
)
3712 const char *p
, *pstart
;
3715 while (qemu_isspace(*p
))
3720 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3725 memcpy(cmdname
, pstart
, len
);
3726 cmdname
[len
] = '\0';
3731 * Read key of 'type' into 'key' and return the current
3734 static char *key_get_info(const char *type
, char **key
)
3742 p
= strchr(type
, ':');
3749 str
= g_malloc(len
+ 1);
3750 memcpy(str
, type
, len
);
3757 static int default_fmt_format
= 'x';
3758 static int default_fmt_size
= 4;
3762 static int is_valid_option(const char *c
, const char *typestr
)
3770 typestr
= strstr(typestr
, option
);
3771 return (typestr
!= NULL
);
3774 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3775 const char *cmdname
)
3777 const mon_cmd_t
*cmd
;
3779 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3780 if (compare_cmd(cmdname
, cmd
->name
)) {
3788 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3790 return search_dispatch_table(mon_cmds
, cmdname
);
3793 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
3795 return search_dispatch_table(qmp_query_cmds
, info_item
);
3798 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3800 return search_dispatch_table(qmp_cmds
, cmdname
);
3803 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3804 const char *cmdline
,
3807 const char *p
, *typestr
;
3809 const mon_cmd_t
*cmd
;
3815 monitor_printf(mon
, "command='%s'\n", cmdline
);
3818 /* extract the command name */
3819 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3823 cmd
= monitor_find_command(cmdname
);
3825 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3829 /* parse the parameters */
3830 typestr
= cmd
->args_type
;
3832 typestr
= key_get_info(typestr
, &key
);
3844 while (qemu_isspace(*p
))
3846 if (*typestr
== '?') {
3849 /* no optional string: NULL argument */
3853 ret
= get_str(buf
, sizeof(buf
), &p
);
3857 monitor_printf(mon
, "%s: filename expected\n",
3861 monitor_printf(mon
, "%s: block device name expected\n",
3865 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3870 qdict_put(qdict
, key
, qstring_from_str(buf
));
3875 QemuOptsList
*opts_list
;
3878 opts_list
= qemu_find_opts(key
);
3879 if (!opts_list
|| opts_list
->desc
->name
) {
3882 while (qemu_isspace(*p
)) {
3887 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3890 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3894 qemu_opts_to_qdict(opts
, qdict
);
3895 qemu_opts_del(opts
);
3900 int count
, format
, size
;
3902 while (qemu_isspace(*p
))
3908 if (qemu_isdigit(*p
)) {
3910 while (qemu_isdigit(*p
)) {
3911 count
= count
* 10 + (*p
- '0');
3949 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3950 monitor_printf(mon
, "invalid char in format: '%c'\n",
3955 format
= default_fmt_format
;
3956 if (format
!= 'i') {
3957 /* for 'i', not specifying a size gives -1 as size */
3959 size
= default_fmt_size
;
3960 default_fmt_size
= size
;
3962 default_fmt_format
= format
;
3965 format
= default_fmt_format
;
3966 if (format
!= 'i') {
3967 size
= default_fmt_size
;
3972 qdict_put(qdict
, "count", qint_from_int(count
));
3973 qdict_put(qdict
, "format", qint_from_int(format
));
3974 qdict_put(qdict
, "size", qint_from_int(size
));
3983 while (qemu_isspace(*p
))
3985 if (*typestr
== '?' || *typestr
== '.') {
3986 if (*typestr
== '?') {
3994 while (qemu_isspace(*p
))
4003 if (get_expr(mon
, &val
, &p
))
4005 /* Check if 'i' is greater than 32-bit */
4006 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
4007 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
4008 monitor_printf(mon
, "integer is for 32-bit values\n");
4010 } else if (c
== 'M') {
4013 qdict_put(qdict
, key
, qint_from_int(val
));
4021 while (qemu_isspace(*p
)) {
4024 if (*typestr
== '?') {
4030 val
= strtosz(p
, &end
);
4032 monitor_printf(mon
, "invalid size\n");
4035 qdict_put(qdict
, key
, qint_from_int(val
));
4043 while (qemu_isspace(*p
))
4045 if (*typestr
== '?') {
4051 if (get_double(mon
, &val
, &p
) < 0) {
4054 if (p
[0] && p
[1] == 's') {
4057 val
/= 1e3
; p
+= 2; break;
4059 val
/= 1e6
; p
+= 2; break;
4061 val
/= 1e9
; p
+= 2; break;
4064 if (*p
&& !qemu_isspace(*p
)) {
4065 monitor_printf(mon
, "Unknown unit suffix\n");
4068 qdict_put(qdict
, key
, qfloat_from_double(val
));
4076 while (qemu_isspace(*p
)) {
4080 while (qemu_isgraph(*p
)) {
4083 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4085 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4088 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4091 qdict_put(qdict
, key
, qbool_from_int(val
));
4096 const char *tmp
= p
;
4103 while (qemu_isspace(*p
))
4108 if(!is_valid_option(p
, typestr
)) {
4110 monitor_printf(mon
, "%s: unsupported option -%c\n",
4122 qdict_put(qdict
, key
, qbool_from_int(1));
4129 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4135 /* check that all arguments were parsed */
4136 while (qemu_isspace(*p
))
4139 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4151 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4153 /* report only the first error */
4155 mon
->error
= qerror
;
4157 MON_DEBUG("Additional error report at %s:%d\n",
4158 qerror
->file
, qerror
->linenr
);
4163 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4165 if (ret
&& !monitor_has_error(mon
)) {
4167 * If it returns failure, it must have passed on error.
4169 * Action: Report an internal error to the client if in QMP.
4171 qerror_report(QERR_UNDEFINED_ERROR
);
4172 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4176 #ifdef CONFIG_DEBUG_MONITOR
4177 if (!ret
&& monitor_has_error(mon
)) {
4179 * If it returns success, it must not have passed an error.
4181 * Action: Report the passed error to the client.
4183 MON_DEBUG("command '%s' returned success but passed an error\n",
4187 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
4189 * Handlers should not call Monitor print functions.
4191 * Action: Ignore them in QMP.
4193 * (XXX: we don't check any 'info' or 'query' command here
4194 * because the user print function _is_ called by do_info(), hence
4195 * we will trigger this check. This problem will go away when we
4196 * make 'query' commands real and kill do_info())
4198 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4199 cmd
->name
, mon_print_count_get(mon
));
4204 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4207 const mon_cmd_t
*cmd
;
4209 qdict
= qdict_new();
4211 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4215 if (handler_is_async(cmd
)) {
4216 user_async_cmd_handler(mon
, cmd
, qdict
);
4217 } else if (handler_is_qobject(cmd
)) {
4218 QObject
*data
= NULL
;
4220 /* XXX: ignores the error code */
4221 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4222 assert(!monitor_has_error(mon
));
4224 cmd
->user_print(mon
, data
);
4225 qobject_decref(data
);
4228 cmd
->mhandler
.cmd(mon
, qdict
);
4235 static void cmd_completion(const char *name
, const char *list
)
4237 const char *p
, *pstart
;
4246 p
= pstart
+ strlen(pstart
);
4248 if (len
> sizeof(cmd
) - 2)
4249 len
= sizeof(cmd
) - 2;
4250 memcpy(cmd
, pstart
, len
);
4252 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4253 readline_add_completion(cur_mon
->rs
, cmd
);
4261 static void file_completion(const char *input
)
4266 char file
[1024], file_prefix
[1024];
4270 p
= strrchr(input
, '/');
4273 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4274 pstrcpy(path
, sizeof(path
), ".");
4276 input_path_len
= p
- input
+ 1;
4277 memcpy(path
, input
, input_path_len
);
4278 if (input_path_len
> sizeof(path
) - 1)
4279 input_path_len
= sizeof(path
) - 1;
4280 path
[input_path_len
] = '\0';
4281 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4283 #ifdef DEBUG_COMPLETION
4284 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4285 input
, path
, file_prefix
);
4287 ffs
= opendir(path
);
4296 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4300 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4301 memcpy(file
, input
, input_path_len
);
4302 if (input_path_len
< sizeof(file
))
4303 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4305 /* stat the file to find out if it's a directory.
4306 * In that case add a slash to speed up typing long paths
4309 if(S_ISDIR(sb
.st_mode
))
4310 pstrcat(file
, sizeof(file
), "/");
4311 readline_add_completion(cur_mon
->rs
, file
);
4317 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4319 const char *name
= bdrv_get_device_name(bs
);
4320 const char *input
= opaque
;
4322 if (input
[0] == '\0' ||
4323 !strncmp(name
, (char *)input
, strlen(input
))) {
4324 readline_add_completion(cur_mon
->rs
, name
);
4328 /* NOTE: this parser is an approximate form of the real command parser */
4329 static void parse_cmdline(const char *cmdline
,
4330 int *pnb_args
, char **args
)
4339 while (qemu_isspace(*p
))
4343 if (nb_args
>= MAX_ARGS
)
4345 ret
= get_str(buf
, sizeof(buf
), &p
);
4346 args
[nb_args
] = g_strdup(buf
);
4351 *pnb_args
= nb_args
;
4354 static const char *next_arg_type(const char *typestr
)
4356 const char *p
= strchr(typestr
, ':');
4357 return (p
!= NULL
? ++p
: typestr
);
4360 static void monitor_find_completion(const char *cmdline
)
4362 const char *cmdname
;
4363 char *args
[MAX_ARGS
];
4364 int nb_args
, i
, len
;
4365 const char *ptype
, *str
;
4366 const mon_cmd_t
*cmd
;
4369 parse_cmdline(cmdline
, &nb_args
, args
);
4370 #ifdef DEBUG_COMPLETION
4371 for(i
= 0; i
< nb_args
; i
++) {
4372 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4376 /* if the line ends with a space, it means we want to complete the
4378 len
= strlen(cmdline
);
4379 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4380 if (nb_args
>= MAX_ARGS
) {
4383 args
[nb_args
++] = g_strdup("");
4386 /* command completion */
4391 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4392 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4393 cmd_completion(cmdname
, cmd
->name
);
4396 /* find the command */
4397 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4398 if (compare_cmd(args
[0], cmd
->name
)) {
4406 ptype
= next_arg_type(cmd
->args_type
);
4407 for(i
= 0; i
< nb_args
- 2; i
++) {
4408 if (*ptype
!= '\0') {
4409 ptype
= next_arg_type(ptype
);
4410 while (*ptype
== '?')
4411 ptype
= next_arg_type(ptype
);
4414 str
= args
[nb_args
- 1];
4415 if (*ptype
== '-' && ptype
[1] != '\0') {
4416 ptype
= next_arg_type(ptype
);
4420 /* file completion */
4421 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4422 file_completion(str
);
4425 /* block device name completion */
4426 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4427 bdrv_iterate(block_completion_it
, (void *)str
);
4430 /* XXX: more generic ? */
4431 if (!strcmp(cmd
->name
, "info")) {
4432 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4433 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4434 cmd_completion(str
, cmd
->name
);
4436 } else if (!strcmp(cmd
->name
, "sendkey")) {
4437 char *sep
= strrchr(str
, '-');
4440 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4441 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4442 cmd_completion(str
, key
->name
);
4444 } else if (!strcmp(cmd
->name
, "help|?")) {
4445 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4446 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4447 cmd_completion(str
, cmd
->name
);
4457 for (i
= 0; i
< nb_args
; i
++) {
4462 static int monitor_can_read(void *opaque
)
4464 Monitor
*mon
= opaque
;
4466 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4469 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4471 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4472 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4476 * Argument validation rules:
4478 * 1. The argument must exist in cmd_args qdict
4479 * 2. The argument type must be the expected one
4481 * Special case: If the argument doesn't exist in cmd_args and
4482 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4483 * checking is skipped for it.
4485 static int check_client_args_type(const QDict
*client_args
,
4486 const QDict
*cmd_args
, int flags
)
4488 const QDictEntry
*ent
;
4490 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4493 const QObject
*client_arg
= qdict_entry_value(ent
);
4494 const char *client_arg_name
= qdict_entry_key(ent
);
4496 obj
= qdict_get(cmd_args
, client_arg_name
);
4498 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4499 /* handler accepts unknowns */
4502 /* client arg doesn't exist */
4503 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4507 arg_type
= qobject_to_qstring(obj
);
4508 assert(arg_type
!= NULL
);
4510 /* check if argument's type is correct */
4511 switch (qstring_get_str(arg_type
)[0]) {
4515 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4516 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4525 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4526 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4532 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4533 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4534 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4541 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4542 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4548 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4553 * These types are not supported by QMP and thus are not
4554 * handled here. Fall through.
4565 * - Check if the client has passed all mandatory args
4566 * - Set special flags for argument validation
4568 static int check_mandatory_args(const QDict
*cmd_args
,
4569 const QDict
*client_args
, int *flags
)
4571 const QDictEntry
*ent
;
4573 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4574 const char *cmd_arg_name
= qdict_entry_key(ent
);
4575 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4576 assert(type
!= NULL
);
4578 if (qstring_get_str(type
)[0] == 'O') {
4579 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4580 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4581 } else if (qstring_get_str(type
)[0] != '-' &&
4582 qstring_get_str(type
)[1] != '?' &&
4583 !qdict_haskey(client_args
, cmd_arg_name
)) {
4584 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4592 static QDict
*qdict_from_args_type(const char *args_type
)
4596 QString
*key
, *type
, *cur_qs
;
4598 assert(args_type
!= NULL
);
4600 qdict
= qdict_new();
4602 if (args_type
== NULL
|| args_type
[0] == '\0') {
4603 /* no args, empty qdict */
4607 key
= qstring_new();
4608 type
= qstring_new();
4613 switch (args_type
[i
]) {
4616 qdict_put(qdict
, qstring_get_str(key
), type
);
4618 if (args_type
[i
] == '\0') {
4621 type
= qstring_new(); /* qdict has ref */
4622 cur_qs
= key
= qstring_new();
4628 qstring_append_chr(cur_qs
, args_type
[i
]);
4638 * Client argument checking rules:
4640 * 1. Client must provide all mandatory arguments
4641 * 2. Each argument provided by the client must be expected
4642 * 3. Each argument provided by the client must have the type expected
4645 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4650 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4653 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4658 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4666 * Input object checking rules
4668 * 1. Input object must be a dict
4669 * 2. The "execute" key must exist
4670 * 3. The "execute" key must be a string
4671 * 4. If the "arguments" key exists, it must be a dict
4672 * 5. If the "id" key exists, it can be anything (ie. json-value)
4673 * 6. Any argument not listed above is considered invalid
4675 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4677 const QDictEntry
*ent
;
4678 int has_exec_key
= 0;
4681 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4682 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4686 input_dict
= qobject_to_qdict(input_obj
);
4688 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4689 const char *arg_name
= qdict_entry_key(ent
);
4690 const QObject
*arg_obj
= qdict_entry_value(ent
);
4692 if (!strcmp(arg_name
, "execute")) {
4693 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4694 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4699 } else if (!strcmp(arg_name
, "arguments")) {
4700 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4701 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4705 } else if (!strcmp(arg_name
, "id")) {
4706 /* FIXME: check duplicated IDs for async commands */
4708 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4713 if (!has_exec_key
) {
4714 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4721 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
4723 QObject
*ret_data
= NULL
;
4725 if (handler_is_async(cmd
)) {
4726 qmp_async_info_handler(mon
, cmd
);
4727 if (monitor_has_error(mon
)) {
4728 monitor_protocol_emitter(mon
, NULL
);
4731 cmd
->mhandler
.info_new(mon
, &ret_data
);
4732 monitor_protocol_emitter(mon
, ret_data
);
4733 qobject_decref(ret_data
);
4737 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4738 const QDict
*params
)
4741 QObject
*data
= NULL
;
4743 mon_print_count_init(mon
);
4745 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4746 handler_audit(mon
, cmd
, ret
);
4747 monitor_protocol_emitter(mon
, data
);
4748 qobject_decref(data
);
4751 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4755 QDict
*input
, *args
;
4756 const mon_cmd_t
*cmd
;
4757 Monitor
*mon
= cur_mon
;
4758 const char *cmd_name
, *query_cmd
;
4761 args
= input
= NULL
;
4763 obj
= json_parser_parse(tokens
, NULL
);
4765 // FIXME: should be triggered in json_parser_parse()
4766 qerror_report(QERR_JSON_PARSING
);
4770 input
= qmp_check_input_obj(obj
);
4772 qobject_decref(obj
);
4776 mon
->mc
->id
= qdict_get(input
, "id");
4777 qobject_incref(mon
->mc
->id
);
4779 cmd_name
= qdict_get_str(input
, "execute");
4780 trace_handle_qmp_command(mon
, cmd_name
);
4781 if (invalid_qmp_mode(mon
, cmd_name
)) {
4782 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4786 cmd
= qmp_find_cmd(cmd_name
);
4787 if (!cmd
&& strstart(cmd_name
, "query-", &query_cmd
)) {
4788 cmd
= qmp_find_query_cmd(query_cmd
);
4791 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4795 obj
= qdict_get(input
, "arguments");
4799 args
= qobject_to_qdict(obj
);
4803 err
= qmp_check_client_args(cmd
, args
);
4809 qmp_call_query_cmd(mon
, cmd
);
4810 } else if (handler_is_async(cmd
)) {
4811 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4813 /* emit the error response */
4817 qmp_call_cmd(mon
, cmd
, args
);
4823 monitor_protocol_emitter(mon
, NULL
);
4830 * monitor_control_read(): Read and handle QMP input
4832 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4834 Monitor
*old_mon
= cur_mon
;
4838 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4843 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4845 Monitor
*old_mon
= cur_mon
;
4851 for (i
= 0; i
< size
; i
++)
4852 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4854 if (size
== 0 || buf
[size
- 1] != 0)
4855 monitor_printf(cur_mon
, "corrupted command\n");
4857 handle_user_command(cur_mon
, (char *)buf
);
4863 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4865 monitor_suspend(mon
);
4866 handle_user_command(mon
, cmdline
);
4867 monitor_resume(mon
);
4870 int monitor_suspend(Monitor
*mon
)
4878 void monitor_resume(Monitor
*mon
)
4882 if (--mon
->suspend_cnt
== 0)
4883 readline_show_prompt(mon
->rs
);
4886 static QObject
*get_qmp_greeting(void)
4888 QObject
*ver
= NULL
;
4890 qmp_marshal_input_query_version(NULL
, NULL
, &ver
);
4891 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4895 * monitor_control_event(): Print QMP gretting
4897 static void monitor_control_event(void *opaque
, int event
)
4900 Monitor
*mon
= opaque
;
4903 case CHR_EVENT_OPENED
:
4904 mon
->mc
->command_mode
= 0;
4905 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4906 data
= get_qmp_greeting();
4907 monitor_json_emitter(mon
, data
);
4908 qobject_decref(data
);
4910 case CHR_EVENT_CLOSED
:
4911 json_message_parser_destroy(&mon
->mc
->parser
);
4916 static void monitor_event(void *opaque
, int event
)
4918 Monitor
*mon
= opaque
;
4921 case CHR_EVENT_MUX_IN
:
4923 if (mon
->reset_seen
) {
4924 readline_restart(mon
->rs
);
4925 monitor_resume(mon
);
4928 mon
->suspend_cnt
= 0;
4932 case CHR_EVENT_MUX_OUT
:
4933 if (mon
->reset_seen
) {
4934 if (mon
->suspend_cnt
== 0) {
4935 monitor_printf(mon
, "\n");
4938 monitor_suspend(mon
);
4945 case CHR_EVENT_OPENED
:
4946 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4947 "information\n", QEMU_VERSION
);
4948 if (!mon
->mux_out
) {
4949 readline_show_prompt(mon
->rs
);
4951 mon
->reset_seen
= 1;
4965 void monitor_init(CharDriverState
*chr
, int flags
)
4967 static int is_first_init
= 1;
4970 if (is_first_init
) {
4971 key_timer
= qemu_new_timer_ns(vm_clock
, release_keys
, NULL
);
4975 mon
= g_malloc0(sizeof(*mon
));
4979 if (flags
& MONITOR_USE_READLINE
) {
4980 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4981 monitor_read_command(mon
, 0);
4984 if (monitor_ctrl_mode(mon
)) {
4985 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
4986 /* Control mode requires special handlers */
4987 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4988 monitor_control_event
, mon
);
4989 qemu_chr_fe_set_echo(chr
, true);
4991 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4992 monitor_event
, mon
);
4995 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4996 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5000 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
5002 BlockDriverState
*bs
= opaque
;
5005 if (bdrv_set_key(bs
, password
) != 0) {
5006 monitor_printf(mon
, "invalid password\n");
5009 if (mon
->password_completion_cb
)
5010 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5012 monitor_read_command(mon
, 1);
5015 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5016 BlockDriverCompletionFunc
*completion_cb
,
5021 if (!bdrv_key_required(bs
)) {
5023 completion_cb(opaque
, 0);
5027 if (monitor_ctrl_mode(mon
)) {
5028 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
5032 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5033 bdrv_get_encrypted_filename(bs
));
5035 mon
->password_completion_cb
= completion_cb
;
5036 mon
->password_opaque
= opaque
;
5038 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5040 if (err
&& completion_cb
)
5041 completion_cb(opaque
, err
);