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"
42 #include "audio/audio.h"
45 #include "qemu-timer.h"
46 #include "migration.h"
55 #include "json-streamer.h"
56 #include "json-parser.h"
59 #ifdef CONFIG_SIMPLE_TRACE
64 //#define DEBUG_COMPLETION
70 * 'B' block device name
71 * 's' string (accept optional quote)
72 * 'O' option string of the form NAME=VALUE,...
73 * parsed according to QemuOptsList given by its name
74 * Example: 'device:O' uses qemu_device_opts.
75 * Restriction: only lists with empty desc are supported
76 * TODO lift the restriction
78 * 'l' target long (32 or 64 bit)
79 * 'M' just like 'l', except in user mode the value is
80 * multiplied by 2^20 (think Mebibyte)
81 * 'o' octets (aka bytes)
82 * user mode accepts an optional T, t, G, g, M, m, K, k
83 * suffix, which multiplies the value by 2^40 for
84 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
85 * M and m, 2^10 for K and k
87 * user mode accepts an optional ms, us, ns suffix,
88 * which divides the value by 1e3, 1e6, 1e9, respectively
89 * '/' optional gdb-like print format (like "/10x")
91 * '?' optional type (for all types, except '/')
92 * '.' other form of optional type (for 'i' and 'l')
94 * user mode accepts "on" or "off"
95 * '-' optional parameter (eg. '-f')
99 typedef struct MonitorCompletionData MonitorCompletionData
;
100 struct MonitorCompletionData
{
102 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
105 typedef struct mon_cmd_t
{
107 const char *args_type
;
110 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
112 void (*info
)(Monitor
*mon
);
113 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
114 int (*info_async
)(Monitor
*mon
, MonitorCompletion
*cb
, void *opaque
);
115 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
116 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
117 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
118 MonitorCompletion
*cb
, void *opaque
);
123 /* file descriptors passed via SCM_RIGHTS */
124 typedef struct mon_fd_t mon_fd_t
;
128 QLIST_ENTRY(mon_fd_t
) next
;
131 typedef struct MonitorControl
{
133 JSONMessageParser parser
;
138 CharDriverState
*chr
;
143 uint8_t outbuf
[1024];
148 BlockDriverCompletionFunc
*password_completion_cb
;
149 void *password_opaque
;
150 #ifdef CONFIG_DEBUG_MONITOR
154 QLIST_HEAD(,mon_fd_t
) fds
;
155 QLIST_ENTRY(Monitor
) entry
;
158 #ifdef CONFIG_DEBUG_MONITOR
159 #define MON_DEBUG(fmt, ...) do { \
160 fprintf(stderr, "Monitor: "); \
161 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
163 static inline void mon_print_count_inc(Monitor
*mon
)
165 mon
->print_calls_nr
++;
168 static inline void mon_print_count_init(Monitor
*mon
)
170 mon
->print_calls_nr
= 0;
173 static inline int mon_print_count_get(const Monitor
*mon
)
175 return mon
->print_calls_nr
;
178 #else /* !CONFIG_DEBUG_MONITOR */
179 #define MON_DEBUG(fmt, ...) do { } while (0)
180 static inline void mon_print_count_inc(Monitor
*mon
) { }
181 static inline void mon_print_count_init(Monitor
*mon
) { }
182 static inline int mon_print_count_get(const Monitor
*mon
) { return 0; }
183 #endif /* CONFIG_DEBUG_MONITOR */
185 /* QMP checker flags */
186 #define QMP_ACCEPT_UNKNOWNS 1
188 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
190 static const mon_cmd_t mon_cmds
[];
191 static const mon_cmd_t info_cmds
[];
193 static const mon_cmd_t qmp_cmds
[];
194 static const mon_cmd_t qmp_query_cmds
[];
197 Monitor
*default_mon
;
199 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
202 static inline int qmp_cmd_mode(const Monitor
*mon
)
204 return (mon
->mc
? mon
->mc
->command_mode
: 0);
207 /* Return true if in control mode, false otherwise */
208 static inline int monitor_ctrl_mode(const Monitor
*mon
)
210 return (mon
->flags
& MONITOR_USE_CONTROL
);
213 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
214 int monitor_cur_is_qmp(void)
216 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
219 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
224 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
226 readline_show_prompt(mon
->rs
);
229 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
232 if (monitor_ctrl_mode(mon
)) {
233 qerror_report(QERR_MISSING_PARAMETER
, "password");
235 } else if (mon
->rs
) {
236 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
237 /* prompt is printed on return from the command handler */
240 monitor_printf(mon
, "terminal does not support password prompting\n");
245 void monitor_flush(Monitor
*mon
)
247 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
248 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
249 mon
->outbuf_index
= 0;
253 /* flush at every end of line or if the buffer is full */
254 static void monitor_puts(Monitor
*mon
, const char *str
)
263 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
264 mon
->outbuf
[mon
->outbuf_index
++] = c
;
265 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
271 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
278 mon_print_count_inc(mon
);
280 if (monitor_ctrl_mode(mon
)) {
284 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
285 monitor_puts(mon
, buf
);
288 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
292 monitor_vprintf(mon
, fmt
, ap
);
296 void monitor_print_filename(Monitor
*mon
, const char *filename
)
300 for (i
= 0; filename
[i
]; i
++) {
301 switch (filename
[i
]) {
305 monitor_printf(mon
, "\\%c", filename
[i
]);
308 monitor_printf(mon
, "\\t");
311 monitor_printf(mon
, "\\r");
314 monitor_printf(mon
, "\\n");
317 monitor_printf(mon
, "%c", filename
[i
]);
323 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
324 const char *fmt
, ...)
328 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
333 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
335 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
337 return cmd
->user_print
!= NULL
;
340 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
342 return cmd
->flags
& MONITOR_CMD_ASYNC
;
345 static inline int monitor_has_error(const Monitor
*mon
)
347 return mon
->error
!= NULL
;
350 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
354 if (mon
->flags
& MONITOR_USE_PRETTY
)
355 json
= qobject_to_json_pretty(data
);
357 json
= qobject_to_json(data
);
358 assert(json
!= NULL
);
360 qstring_append_chr(json
, '\n');
361 monitor_puts(mon
, qstring_get_str(json
));
366 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
372 if (!monitor_has_error(mon
)) {
373 /* success response */
375 qobject_incref(data
);
376 qdict_put_obj(qmp
, "return", data
);
378 /* return an empty QDict by default */
379 qdict_put(qmp
, "return", qdict_new());
383 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
384 qdict_put(qmp
, "error", mon
->error
->error
);
385 QINCREF(mon
->error
->error
);
391 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
395 monitor_json_emitter(mon
, QOBJECT(qmp
));
399 static void timestamp_put(QDict
*qdict
)
405 err
= qemu_gettimeofday(&tv
);
409 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
410 "'microseconds': %" PRId64
" }",
411 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
412 qdict_put_obj(qdict
, "timestamp", obj
);
416 * monitor_protocol_event(): Generate a Monitor event
418 * Event-specific data can be emitted through the (optional) 'data' parameter.
420 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
423 const char *event_name
;
426 assert(event
< QEVENT_MAX
);
429 case QEVENT_SHUTDOWN
:
430 event_name
= "SHUTDOWN";
433 event_name
= "RESET";
435 case QEVENT_POWERDOWN
:
436 event_name
= "POWERDOWN";
442 event_name
= "RESUME";
444 case QEVENT_VNC_CONNECTED
:
445 event_name
= "VNC_CONNECTED";
447 case QEVENT_VNC_INITIALIZED
:
448 event_name
= "VNC_INITIALIZED";
450 case QEVENT_VNC_DISCONNECTED
:
451 event_name
= "VNC_DISCONNECTED";
453 case QEVENT_BLOCK_IO_ERROR
:
454 event_name
= "BLOCK_IO_ERROR";
456 case QEVENT_RTC_CHANGE
:
457 event_name
= "RTC_CHANGE";
459 case QEVENT_WATCHDOG
:
460 event_name
= "WATCHDOG";
469 qdict_put(qmp
, "event", qstring_from_str(event_name
));
471 qobject_incref(data
);
472 qdict_put_obj(qmp
, "data", data
);
475 QLIST_FOREACH(mon
, &mon_list
, entry
) {
476 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
477 monitor_json_emitter(mon
, QOBJECT(qmp
));
483 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
486 /* Will setup QMP capabilities in the future */
487 if (monitor_ctrl_mode(mon
)) {
488 mon
->mc
->command_mode
= 1;
494 static int mon_set_cpu(int cpu_index
);
495 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
497 static int do_hmp_passthrough(Monitor
*mon
, const QDict
*params
,
501 Monitor
*old_mon
, hmp
;
502 CharDriverState mchar
;
504 memset(&hmp
, 0, sizeof(hmp
));
505 qemu_chr_init_mem(&mchar
);
511 if (qdict_haskey(params
, "cpu-index")) {
512 ret
= mon_set_cpu(qdict_get_int(params
, "cpu-index"));
515 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "cpu-index", "a CPU number");
520 handle_user_command(&hmp
, qdict_get_str(params
, "command-line"));
523 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
524 *ret_data
= QOBJECT(qemu_chr_mem_to_qs(hmp
.chr
));
528 qemu_chr_close_mem(hmp
.chr
);
532 static int compare_cmd(const char *name
, const char *list
)
534 const char *p
, *pstart
;
542 p
= pstart
+ strlen(pstart
);
543 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
552 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
553 const char *prefix
, const char *name
)
555 const mon_cmd_t
*cmd
;
557 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
558 if (!name
|| !strcmp(name
, cmd
->name
))
559 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
560 cmd
->params
, cmd
->help
);
564 static void help_cmd(Monitor
*mon
, const char *name
)
566 if (name
&& !strcmp(name
, "info")) {
567 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
569 help_cmd_dump(mon
, mon_cmds
, "", name
);
570 if (name
&& !strcmp(name
, "log")) {
571 const CPULogItem
*item
;
572 monitor_printf(mon
, "Log items (comma separated):\n");
573 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
574 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
575 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
581 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
583 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
586 #ifdef CONFIG_SIMPLE_TRACE
587 static void do_change_trace_event_state(Monitor
*mon
, const QDict
*qdict
)
589 const char *tp_name
= qdict_get_str(qdict
, "name");
590 bool new_state
= qdict_get_bool(qdict
, "option");
591 int ret
= st_change_trace_event_state(tp_name
, new_state
);
594 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
598 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
600 const char *op
= qdict_get_try_str(qdict
, "op");
601 const char *arg
= qdict_get_try_str(qdict
, "arg");
604 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
605 } else if (!strcmp(op
, "on")) {
606 st_set_trace_file_enabled(true);
607 } else if (!strcmp(op
, "off")) {
608 st_set_trace_file_enabled(false);
609 } else if (!strcmp(op
, "flush")) {
610 st_flush_trace_buffer();
611 } else if (!strcmp(op
, "set")) {
613 st_set_trace_file(arg
);
616 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
617 help_cmd(mon
, "trace-file");
622 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
624 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
627 data
->user_print(data
->mon
, ret_data
);
629 monitor_resume(data
->mon
);
633 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
635 monitor_protocol_emitter(opaque
, ret_data
);
638 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
641 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
644 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
646 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
649 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
654 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
656 cb_data
->user_print
= cmd
->user_print
;
657 monitor_suspend(mon
);
658 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
659 user_monitor_complete
, cb_data
);
666 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
670 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
672 cb_data
->user_print
= cmd
->user_print
;
673 monitor_suspend(mon
);
674 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
681 static void do_info(Monitor
*mon
, const QDict
*qdict
)
683 const mon_cmd_t
*cmd
;
684 const char *item
= qdict_get_try_str(qdict
, "item");
690 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
691 if (compare_cmd(item
, cmd
->name
))
695 if (cmd
->name
== NULL
) {
699 if (handler_is_async(cmd
)) {
700 user_async_info_handler(mon
, cmd
);
701 } else if (handler_is_qobject(cmd
)) {
702 QObject
*info_data
= NULL
;
704 cmd
->mhandler
.info_new(mon
, &info_data
);
706 cmd
->user_print(mon
, info_data
);
707 qobject_decref(info_data
);
710 cmd
->mhandler
.info(mon
);
716 help_cmd(mon
, "info");
719 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
724 qdict
= qobject_to_qdict(data
);
725 qemu
= qdict_get_qdict(qdict
, "qemu");
727 monitor_printf(mon
, "%" PRId64
".%" PRId64
".%" PRId64
"%s\n",
728 qdict_get_int(qemu
, "major"),
729 qdict_get_int(qemu
, "minor"),
730 qdict_get_int(qemu
, "micro"),
731 qdict_get_str(qdict
, "package"));
734 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
736 const char *version
= QEMU_VERSION
;
737 int major
= 0, minor
= 0, micro
= 0;
740 major
= strtol(version
, &tmp
, 10);
742 minor
= strtol(tmp
, &tmp
, 10);
744 micro
= strtol(tmp
, &tmp
, 10);
746 *ret_data
= qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
747 'micro': %d }, 'package': %s }", major
, minor
, micro
, QEMU_PKGVERSION
);
750 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
754 qdict
= qobject_to_qdict(data
);
755 if (qdict_size(qdict
) == 0) {
759 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
762 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
764 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
765 qobject_from_jsonf("{}");
768 static QObject
*get_cmd_dict(const char *name
)
772 /* Remove '|' from some commands */
773 p
= strchr(name
, '|');
780 return qobject_from_jsonf("{ 'name': %s }", p
);
783 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
786 const mon_cmd_t
*cmd
;
788 cmd_list
= qlist_new();
790 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
791 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
794 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
796 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
797 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
800 *ret_data
= QOBJECT(cmd_list
);
803 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
805 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
808 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
812 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
813 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
814 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
815 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
816 qemu_uuid
[14], qemu_uuid
[15]);
817 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
820 /* get the current CPU defined by the user */
821 static int mon_set_cpu(int cpu_index
)
825 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
826 if (env
->cpu_index
== cpu_index
) {
827 cur_mon
->mon_cpu
= env
;
834 static CPUState
*mon_get_cpu(void)
836 if (!cur_mon
->mon_cpu
) {
839 cpu_synchronize_state(cur_mon
->mon_cpu
);
840 return cur_mon
->mon_cpu
;
843 static void do_info_registers(Monitor
*mon
)
848 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
851 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
856 static void print_cpu_iter(QObject
*obj
, void *opaque
)
860 Monitor
*mon
= opaque
;
862 assert(qobject_type(obj
) == QTYPE_QDICT
);
863 cpu
= qobject_to_qdict(obj
);
865 if (qdict_get_bool(cpu
, "current")) {
869 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
871 #if defined(TARGET_I386)
872 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
873 (target_ulong
) qdict_get_int(cpu
, "pc"));
874 #elif defined(TARGET_PPC)
875 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
876 (target_long
) qdict_get_int(cpu
, "nip"));
877 #elif defined(TARGET_SPARC)
878 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
879 (target_long
) qdict_get_int(cpu
, "pc"));
880 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
881 (target_long
) qdict_get_int(cpu
, "npc"));
882 #elif defined(TARGET_MIPS)
883 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
884 (target_long
) qdict_get_int(cpu
, "PC"));
887 if (qdict_get_bool(cpu
, "halted")) {
888 monitor_printf(mon
, " (halted)");
891 monitor_printf(mon
, "\n");
894 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
898 assert(qobject_type(data
) == QTYPE_QLIST
);
899 cpu_list
= qobject_to_qlist(data
);
900 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
903 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
908 cpu_list
= qlist_new();
910 /* just to set the default cpu if not already done */
913 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
917 cpu_synchronize_state(env
);
919 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
920 env
->cpu_index
, env
== mon
->mon_cpu
,
923 cpu
= qobject_to_qdict(obj
);
925 #if defined(TARGET_I386)
926 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
927 #elif defined(TARGET_PPC)
928 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
929 #elif defined(TARGET_SPARC)
930 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
931 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
932 #elif defined(TARGET_MIPS)
933 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
936 qlist_append(cpu_list
, cpu
);
939 *ret_data
= QOBJECT(cpu_list
);
942 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
944 int index
= qdict_get_int(qdict
, "index");
945 if (mon_set_cpu(index
) < 0) {
946 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "index",
953 static void do_info_jit(Monitor
*mon
)
955 dump_exec_info((FILE *)mon
, monitor_fprintf
);
958 static void do_info_history(Monitor
*mon
)
967 str
= readline_get_history(mon
->rs
, i
);
970 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
975 #if defined(TARGET_PPC)
976 /* XXX: not implemented in other targets */
977 static void do_info_cpu_stats(Monitor
*mon
)
982 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
986 #if defined(CONFIG_SIMPLE_TRACE)
987 static void do_info_trace(Monitor
*mon
)
989 st_print_trace((FILE *)mon
, &monitor_fprintf
);
992 static void do_info_trace_events(Monitor
*mon
)
994 st_print_trace_events((FILE *)mon
, &monitor_fprintf
);
999 * do_quit(): Quit QEMU execution
1001 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1003 monitor_suspend(mon
);
1005 qemu_system_shutdown_request();
1010 static int change_vnc_password(const char *password
)
1012 if (vnc_display_password(NULL
, password
) < 0) {
1013 qerror_report(QERR_SET_PASSWD_FAILED
);
1020 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1023 change_vnc_password(password
);
1024 monitor_read_command(mon
, 1);
1027 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1029 if (strcmp(target
, "passwd") == 0 ||
1030 strcmp(target
, "password") == 0) {
1033 strncpy(password
, arg
, sizeof(password
));
1034 password
[sizeof(password
) - 1] = '\0';
1035 return change_vnc_password(password
);
1037 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1040 if (vnc_display_open(NULL
, target
) < 0) {
1041 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
1050 * do_change(): Change a removable medium, or VNC configuration
1052 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1054 const char *device
= qdict_get_str(qdict
, "device");
1055 const char *target
= qdict_get_str(qdict
, "target");
1056 const char *arg
= qdict_get_try_str(qdict
, "arg");
1059 if (strcmp(device
, "vnc") == 0) {
1060 ret
= do_change_vnc(mon
, target
, arg
);
1062 ret
= do_change_block(mon
, device
, target
, arg
);
1068 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1070 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1074 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1076 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1079 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1082 const char *items
= qdict_get_str(qdict
, "items");
1084 if (!strcmp(items
, "none")) {
1087 mask
= cpu_str_to_log_mask(items
);
1089 help_cmd(mon
, "log");
1096 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1098 const char *option
= qdict_get_try_str(qdict
, "option");
1099 if (!option
|| !strcmp(option
, "on")) {
1101 } else if (!strcmp(option
, "off")) {
1104 monitor_printf(mon
, "unexpected option %s\n", option
);
1109 * do_stop(): Stop VM execution
1111 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1113 vm_stop(EXCP_INTERRUPT
);
1117 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1119 struct bdrv_iterate_context
{
1125 * do_cont(): Resume emulation.
1127 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1129 struct bdrv_iterate_context context
= { mon
, 0 };
1131 if (incoming_expected
) {
1132 qerror_report(QERR_MIGRATION_EXPECTED
);
1135 bdrv_iterate(encrypted_bdrv_it
, &context
);
1136 /* only resume the vm if all keys are set and valid */
1145 static void bdrv_key_cb(void *opaque
, int err
)
1147 Monitor
*mon
= opaque
;
1149 /* another key was set successfully, retry to continue */
1151 do_cont(mon
, NULL
, NULL
);
1154 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1156 struct bdrv_iterate_context
*context
= opaque
;
1158 if (!context
->err
&& bdrv_key_required(bs
)) {
1159 context
->err
= -EBUSY
;
1160 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1165 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1167 const char *device
= qdict_get_try_str(qdict
, "device");
1169 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1170 if (gdbserver_start(device
) < 0) {
1171 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1173 } else if (strcmp(device
, "none") == 0) {
1174 monitor_printf(mon
, "Disabled gdbserver\n");
1176 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1181 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1183 const char *action
= qdict_get_str(qdict
, "action");
1184 if (select_watchdog_action(action
) == -1) {
1185 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1189 static void monitor_printc(Monitor
*mon
, int c
)
1191 monitor_printf(mon
, "'");
1194 monitor_printf(mon
, "\\'");
1197 monitor_printf(mon
, "\\\\");
1200 monitor_printf(mon
, "\\n");
1203 monitor_printf(mon
, "\\r");
1206 if (c
>= 32 && c
<= 126) {
1207 monitor_printf(mon
, "%c", c
);
1209 monitor_printf(mon
, "\\x%02x", c
);
1213 monitor_printf(mon
, "'");
1216 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1217 target_phys_addr_t addr
, int is_physical
)
1220 int l
, line_size
, i
, max_digits
, len
;
1224 if (format
== 'i') {
1227 env
= mon_get_cpu();
1231 } else if (wsize
== 4) {
1234 /* as default we use the current CS size */
1237 #ifdef TARGET_X86_64
1238 if ((env
->efer
& MSR_EFER_LMA
) &&
1239 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1243 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1248 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1252 len
= wsize
* count
;
1261 max_digits
= (wsize
* 8 + 2) / 3;
1265 max_digits
= (wsize
* 8) / 4;
1269 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1278 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1280 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1285 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1287 env
= mon_get_cpu();
1288 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1289 monitor_printf(mon
, " Cannot access memory\n");
1298 v
= ldub_raw(buf
+ i
);
1301 v
= lduw_raw(buf
+ i
);
1304 v
= (uint32_t)ldl_raw(buf
+ i
);
1307 v
= ldq_raw(buf
+ i
);
1310 monitor_printf(mon
, " ");
1313 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1316 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1319 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1322 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1325 monitor_printc(mon
, v
);
1330 monitor_printf(mon
, "\n");
1336 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1338 int count
= qdict_get_int(qdict
, "count");
1339 int format
= qdict_get_int(qdict
, "format");
1340 int size
= qdict_get_int(qdict
, "size");
1341 target_long addr
= qdict_get_int(qdict
, "addr");
1343 memory_dump(mon
, count
, format
, size
, addr
, 0);
1346 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1348 int count
= qdict_get_int(qdict
, "count");
1349 int format
= qdict_get_int(qdict
, "format");
1350 int size
= qdict_get_int(qdict
, "size");
1351 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1353 memory_dump(mon
, count
, format
, size
, addr
, 1);
1356 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1358 int format
= qdict_get_int(qdict
, "format");
1359 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1361 #if TARGET_PHYS_ADDR_BITS == 32
1364 monitor_printf(mon
, "%#o", val
);
1367 monitor_printf(mon
, "%#x", val
);
1370 monitor_printf(mon
, "%u", val
);
1374 monitor_printf(mon
, "%d", val
);
1377 monitor_printc(mon
, val
);
1383 monitor_printf(mon
, "%#" PRIo64
, val
);
1386 monitor_printf(mon
, "%#" PRIx64
, val
);
1389 monitor_printf(mon
, "%" PRIu64
, val
);
1393 monitor_printf(mon
, "%" PRId64
, val
);
1396 monitor_printc(mon
, val
);
1400 monitor_printf(mon
, "\n");
1403 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1406 uint32_t size
= qdict_get_int(qdict
, "size");
1407 const char *filename
= qdict_get_str(qdict
, "filename");
1408 target_long addr
= qdict_get_int(qdict
, "val");
1414 env
= mon_get_cpu();
1416 f
= fopen(filename
, "wb");
1418 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1425 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1426 if (fwrite(buf
, 1, l
, f
) != l
) {
1427 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1441 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1447 uint32_t size
= qdict_get_int(qdict
, "size");
1448 const char *filename
= qdict_get_str(qdict
, "filename");
1449 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1452 f
= fopen(filename
, "wb");
1454 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1461 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1462 if (fwrite(buf
, 1, l
, f
) != l
) {
1463 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1478 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1483 uint32_t start
= qdict_get_int(qdict
, "start");
1484 uint32_t size
= qdict_get_int(qdict
, "size");
1487 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1488 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1489 /* BSD sum algorithm ('sum' Unix command) */
1490 sum
= (sum
>> 1) | (sum
<< 15);
1493 monitor_printf(mon
, "%05d\n", sum
);
1501 static const KeyDef key_defs
[] = {
1503 { 0x36, "shift_r" },
1508 { 0xe4, "altgr_r" },
1528 { 0x0e, "backspace" },
1541 { 0x1a, "bracket_left" },
1542 { 0x1b, "bracket_right" },
1554 { 0x27, "semicolon" },
1555 { 0x28, "apostrophe" },
1556 { 0x29, "grave_accent" },
1558 { 0x2b, "backslash" },
1570 { 0x37, "asterisk" },
1573 { 0x3a, "caps_lock" },
1584 { 0x45, "num_lock" },
1585 { 0x46, "scroll_lock" },
1587 { 0xb5, "kp_divide" },
1588 { 0x37, "kp_multiply" },
1589 { 0x4a, "kp_subtract" },
1591 { 0x9c, "kp_enter" },
1592 { 0x53, "kp_decimal" },
1625 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1640 { 0xfe, "compose" },
1645 static int get_keycode(const char *key
)
1651 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1652 if (!strcmp(key
, p
->name
))
1655 if (strstart(key
, "0x", NULL
)) {
1656 ret
= strtoul(key
, &endp
, 0);
1657 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1663 #define MAX_KEYCODES 16
1664 static uint8_t keycodes
[MAX_KEYCODES
];
1665 static int nb_pending_keycodes
;
1666 static QEMUTimer
*key_timer
;
1668 static void release_keys(void *opaque
)
1672 while (nb_pending_keycodes
> 0) {
1673 nb_pending_keycodes
--;
1674 keycode
= keycodes
[nb_pending_keycodes
];
1676 kbd_put_keycode(0xe0);
1677 kbd_put_keycode(keycode
| 0x80);
1681 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1683 char keyname_buf
[16];
1685 int keyname_len
, keycode
, i
;
1686 const char *string
= qdict_get_str(qdict
, "string");
1687 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1688 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1690 if (nb_pending_keycodes
> 0) {
1691 qemu_del_timer(key_timer
);
1698 separator
= strchr(string
, '-');
1699 keyname_len
= separator
? separator
- string
: strlen(string
);
1700 if (keyname_len
> 0) {
1701 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1702 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1703 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1706 if (i
== MAX_KEYCODES
) {
1707 monitor_printf(mon
, "too many keys\n");
1710 keyname_buf
[keyname_len
] = 0;
1711 keycode
= get_keycode(keyname_buf
);
1713 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1716 keycodes
[i
++] = keycode
;
1720 string
= separator
+ 1;
1722 nb_pending_keycodes
= i
;
1723 /* key down events */
1724 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1725 keycode
= keycodes
[i
];
1727 kbd_put_keycode(0xe0);
1728 kbd_put_keycode(keycode
& 0x7f);
1730 /* delayed key up events */
1731 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1732 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1735 static int mouse_button_state
;
1737 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1740 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1741 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1742 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1743 dx
= strtol(dx_str
, NULL
, 0);
1744 dy
= strtol(dy_str
, NULL
, 0);
1747 dz
= strtol(dz_str
, NULL
, 0);
1748 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1751 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1753 int button_state
= qdict_get_int(qdict
, "button_state");
1754 mouse_button_state
= button_state
;
1755 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1758 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1760 int size
= qdict_get_int(qdict
, "size");
1761 int addr
= qdict_get_int(qdict
, "addr");
1762 int has_index
= qdict_haskey(qdict
, "index");
1767 int index
= qdict_get_int(qdict
, "index");
1768 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1776 val
= cpu_inb(addr
);
1780 val
= cpu_inw(addr
);
1784 val
= cpu_inl(addr
);
1788 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1789 suffix
, addr
, size
* 2, val
);
1792 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1794 int size
= qdict_get_int(qdict
, "size");
1795 int addr
= qdict_get_int(qdict
, "addr");
1796 int val
= qdict_get_int(qdict
, "val");
1798 addr
&= IOPORTS_MASK
;
1803 cpu_outb(addr
, val
);
1806 cpu_outw(addr
, val
);
1809 cpu_outl(addr
, val
);
1814 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1817 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1819 res
= qemu_boot_set(bootdevice
);
1821 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1822 } else if (res
> 0) {
1823 monitor_printf(mon
, "setting boot device list failed\n");
1825 monitor_printf(mon
, "no function defined to set boot device list for "
1826 "this architecture\n");
1831 * do_system_reset(): Issue a machine reset
1833 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1836 qemu_system_reset_request();
1841 * do_system_powerdown(): Issue a machine powerdown
1843 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1846 qemu_system_powerdown_request();
1850 #if defined(TARGET_I386)
1851 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
1852 target_phys_addr_t pte
,
1853 target_phys_addr_t mask
)
1855 #ifdef TARGET_X86_64
1856 if (addr
& (1ULL << 47)) {
1860 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1861 " %c%c%c%c%c%c%c%c%c\n",
1864 pte
& PG_NX_MASK
? 'X' : '-',
1865 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1866 pte
& PG_PSE_MASK
? 'P' : '-',
1867 pte
& PG_DIRTY_MASK
? 'D' : '-',
1868 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1869 pte
& PG_PCD_MASK
? 'C' : '-',
1870 pte
& PG_PWT_MASK
? 'T' : '-',
1871 pte
& PG_USER_MASK
? 'U' : '-',
1872 pte
& PG_RW_MASK
? 'W' : '-');
1875 static void tlb_info_32(Monitor
*mon
, CPUState
*env
)
1878 uint32_t pgd
, pde
, pte
;
1880 pgd
= env
->cr
[3] & ~0xfff;
1881 for(l1
= 0; l1
< 1024; l1
++) {
1882 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1883 pde
= le32_to_cpu(pde
);
1884 if (pde
& PG_PRESENT_MASK
) {
1885 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1887 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1889 for(l2
= 0; l2
< 1024; l2
++) {
1890 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1891 (uint8_t *)&pte
, 4);
1892 pte
= le32_to_cpu(pte
);
1893 if (pte
& PG_PRESENT_MASK
) {
1894 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1904 static void tlb_info_pae32(Monitor
*mon
, CPUState
*env
)
1907 uint64_t pdpe
, pde
, pte
;
1908 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1910 pdp_addr
= env
->cr
[3] & ~0x1f;
1911 for (l1
= 0; l1
< 4; l1
++) {
1912 cpu_physical_memory_read(pdp_addr
+ l1
* 8, (uint8_t *)&pdpe
, 8);
1913 pdpe
= le64_to_cpu(pdpe
);
1914 if (pdpe
& PG_PRESENT_MASK
) {
1915 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1916 for (l2
= 0; l2
< 512; l2
++) {
1917 cpu_physical_memory_read(pd_addr
+ l2
* 8,
1918 (uint8_t *)&pde
, 8);
1919 pde
= le64_to_cpu(pde
);
1920 if (pde
& PG_PRESENT_MASK
) {
1921 if (pde
& PG_PSE_MASK
) {
1922 /* 2M pages with PAE, CR4.PSE is ignored */
1923 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1924 ~((target_phys_addr_t
)(1 << 20) - 1));
1926 pt_addr
= pde
& 0x3fffffffff000ULL
;
1927 for (l3
= 0; l3
< 512; l3
++) {
1928 cpu_physical_memory_read(pt_addr
+ l3
* 8,
1929 (uint8_t *)&pte
, 8);
1930 pte
= le64_to_cpu(pte
);
1931 if (pte
& PG_PRESENT_MASK
) {
1932 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1935 ~(target_phys_addr_t
)0xfff);
1945 #ifdef TARGET_X86_64
1946 static void tlb_info_64(Monitor
*mon
, CPUState
*env
)
1948 uint64_t l1
, l2
, l3
, l4
;
1949 uint64_t pml4e
, pdpe
, pde
, pte
;
1950 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1952 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1953 for (l1
= 0; l1
< 512; l1
++) {
1954 cpu_physical_memory_read(pml4_addr
+ l1
* 8, (uint8_t *)&pml4e
, 8);
1955 pml4e
= le64_to_cpu(pml4e
);
1956 if (pml4e
& PG_PRESENT_MASK
) {
1957 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1958 for (l2
= 0; l2
< 512; l2
++) {
1959 cpu_physical_memory_read(pdp_addr
+ l2
* 8, (uint8_t *)&pdpe
,
1961 pdpe
= le64_to_cpu(pdpe
);
1962 if (pdpe
& PG_PRESENT_MASK
) {
1963 if (pdpe
& PG_PSE_MASK
) {
1964 /* 1G pages, CR4.PSE is ignored */
1965 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1966 0x3ffffc0000000ULL
);
1968 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1969 for (l3
= 0; l3
< 512; l3
++) {
1970 cpu_physical_memory_read(pd_addr
+ l3
* 8,
1971 (uint8_t *)&pde
, 8);
1972 pde
= le64_to_cpu(pde
);
1973 if (pde
& PG_PRESENT_MASK
) {
1974 if (pde
& PG_PSE_MASK
) {
1975 /* 2M pages, CR4.PSE is ignored */
1976 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1978 0x3ffffffe00000ULL
);
1980 pt_addr
= pde
& 0x3fffffffff000ULL
;
1981 for (l4
= 0; l4
< 512; l4
++) {
1982 cpu_physical_memory_read(pt_addr
1986 pte
= le64_to_cpu(pte
);
1987 if (pte
& PG_PRESENT_MASK
) {
1988 print_pte(mon
, (l1
<< 39) +
1990 (l3
<< 21) + (l4
<< 12),
1992 0x3fffffffff000ULL
);
2006 static void tlb_info(Monitor
*mon
)
2010 env
= mon_get_cpu();
2012 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2013 monitor_printf(mon
, "PG disabled\n");
2016 if (env
->cr
[4] & CR4_PAE_MASK
) {
2017 #ifdef TARGET_X86_64
2018 if (env
->hflags
& HF_LMA_MASK
) {
2019 tlb_info_64(mon
, env
);
2023 tlb_info_pae32(mon
, env
);
2026 tlb_info_32(mon
, env
);
2030 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
2032 target_phys_addr_t end
, int prot
)
2035 prot1
= *plast_prot
;
2036 if (prot
!= prot1
) {
2037 if (*pstart
!= -1) {
2038 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
2039 TARGET_FMT_plx
" %c%c%c\n",
2040 *pstart
, end
, end
- *pstart
,
2041 prot1
& PG_USER_MASK
? 'u' : '-',
2043 prot1
& PG_RW_MASK
? 'w' : '-');
2053 static void mem_info_32(Monitor
*mon
, CPUState
*env
)
2055 int l1
, l2
, prot
, last_prot
;
2056 uint32_t pgd
, pde
, pte
;
2057 target_phys_addr_t start
, end
;
2059 pgd
= env
->cr
[3] & ~0xfff;
2062 for(l1
= 0; l1
< 1024; l1
++) {
2063 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
2064 pde
= le32_to_cpu(pde
);
2066 if (pde
& PG_PRESENT_MASK
) {
2067 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2068 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2069 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2071 for(l2
= 0; l2
< 1024; l2
++) {
2072 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
2073 (uint8_t *)&pte
, 4);
2074 pte
= le32_to_cpu(pte
);
2075 end
= (l1
<< 22) + (l2
<< 12);
2076 if (pte
& PG_PRESENT_MASK
) {
2077 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2081 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2086 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2091 static void mem_info_pae32(Monitor
*mon
, CPUState
*env
)
2093 int l1
, l2
, l3
, prot
, last_prot
;
2094 uint64_t pdpe
, pde
, pte
;
2095 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2096 target_phys_addr_t start
, end
;
2098 pdp_addr
= env
->cr
[3] & ~0x1f;
2101 for (l1
= 0; l1
< 4; l1
++) {
2102 cpu_physical_memory_read(pdp_addr
+ l1
* 8, (uint8_t *)&pdpe
, 8);
2103 pdpe
= le64_to_cpu(pdpe
);
2105 if (pdpe
& PG_PRESENT_MASK
) {
2106 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2107 for (l2
= 0; l2
< 512; l2
++) {
2108 cpu_physical_memory_read(pd_addr
+ l2
* 8,
2109 (uint8_t *)&pde
, 8);
2110 pde
= le64_to_cpu(pde
);
2111 end
= (l1
<< 30) + (l2
<< 21);
2112 if (pde
& PG_PRESENT_MASK
) {
2113 if (pde
& PG_PSE_MASK
) {
2114 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2116 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2118 pt_addr
= pde
& 0x3fffffffff000ULL
;
2119 for (l3
= 0; l3
< 512; l3
++) {
2120 cpu_physical_memory_read(pt_addr
+ l3
* 8,
2121 (uint8_t *)&pte
, 8);
2122 pte
= le64_to_cpu(pte
);
2123 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
2124 if (pte
& PG_PRESENT_MASK
) {
2125 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
2130 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2135 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2140 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2146 #ifdef TARGET_X86_64
2147 static void mem_info_64(Monitor
*mon
, CPUState
*env
)
2149 int prot
, last_prot
;
2150 uint64_t l1
, l2
, l3
, l4
;
2151 uint64_t pml4e
, pdpe
, pde
, pte
;
2152 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
2154 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2157 for (l1
= 0; l1
< 512; l1
++) {
2158 cpu_physical_memory_read(pml4_addr
+ l1
* 8, (uint8_t *)&pml4e
, 8);
2159 pml4e
= le64_to_cpu(pml4e
);
2161 if (pml4e
& PG_PRESENT_MASK
) {
2162 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2163 for (l2
= 0; l2
< 512; l2
++) {
2164 cpu_physical_memory_read(pdp_addr
+ l2
* 8, (uint8_t *)&pdpe
,
2166 pdpe
= le64_to_cpu(pdpe
);
2167 end
= (l1
<< 39) + (l2
<< 30);
2168 if (pdpe
& PG_PRESENT_MASK
) {
2169 if (pdpe
& PG_PSE_MASK
) {
2170 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2172 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2174 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2175 for (l3
= 0; l3
< 512; l3
++) {
2176 cpu_physical_memory_read(pd_addr
+ l3
* 8,
2177 (uint8_t *)&pde
, 8);
2178 pde
= le64_to_cpu(pde
);
2179 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
2180 if (pde
& PG_PRESENT_MASK
) {
2181 if (pde
& PG_PSE_MASK
) {
2182 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2184 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2186 pt_addr
= pde
& 0x3fffffffff000ULL
;
2187 for (l4
= 0; l4
< 512; l4
++) {
2188 cpu_physical_memory_read(pt_addr
2192 pte
= le64_to_cpu(pte
);
2193 end
= (l1
<< 39) + (l2
<< 30) +
2194 (l3
<< 21) + (l4
<< 12);
2195 if (pte
& PG_PRESENT_MASK
) {
2196 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
2201 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2206 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2212 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2217 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2223 static void mem_info(Monitor
*mon
)
2227 env
= mon_get_cpu();
2229 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2230 monitor_printf(mon
, "PG disabled\n");
2233 if (env
->cr
[4] & CR4_PAE_MASK
) {
2234 #ifdef TARGET_X86_64
2235 if (env
->hflags
& HF_LMA_MASK
) {
2236 mem_info_64(mon
, env
);
2240 mem_info_pae32(mon
, env
);
2243 mem_info_32(mon
, env
);
2248 #if defined(TARGET_SH4)
2250 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2252 monitor_printf(mon
, " tlb%i:\t"
2253 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2254 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2255 "dirty=%hhu writethrough=%hhu\n",
2257 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2258 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2262 static void tlb_info(Monitor
*mon
)
2264 CPUState
*env
= mon_get_cpu();
2267 monitor_printf (mon
, "ITLB:\n");
2268 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2269 print_tlb (mon
, i
, &env
->itlb
[i
]);
2270 monitor_printf (mon
, "UTLB:\n");
2271 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2272 print_tlb (mon
, i
, &env
->utlb
[i
]);
2277 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
2281 qdict
= qobject_to_qdict(data
);
2283 monitor_printf(mon
, "kvm support: ");
2284 if (qdict_get_bool(qdict
, "present")) {
2285 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2286 "enabled" : "disabled");
2288 monitor_printf(mon
, "not compiled\n");
2292 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2295 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2298 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2302 static void do_info_numa(Monitor
*mon
)
2307 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2308 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2309 monitor_printf(mon
, "node %d cpus:", i
);
2310 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2311 if (env
->numa_node
== i
) {
2312 monitor_printf(mon
, " %d", env
->cpu_index
);
2315 monitor_printf(mon
, "\n");
2316 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2321 #ifdef CONFIG_PROFILER
2326 static void do_info_profile(Monitor
*mon
)
2332 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2333 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2334 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2335 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2340 static void do_info_profile(Monitor
*mon
)
2342 monitor_printf(mon
, "Internal profiler not compiled\n");
2346 /* Capture support */
2347 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2349 static void do_info_capture(Monitor
*mon
)
2354 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2355 monitor_printf(mon
, "[%d]: ", i
);
2356 s
->ops
.info (s
->opaque
);
2361 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2364 int n
= qdict_get_int(qdict
, "n");
2367 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2369 s
->ops
.destroy (s
->opaque
);
2370 QLIST_REMOVE (s
, entries
);
2377 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2379 const char *path
= qdict_get_str(qdict
, "path");
2380 int has_freq
= qdict_haskey(qdict
, "freq");
2381 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2382 int has_bits
= qdict_haskey(qdict
, "bits");
2383 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2384 int has_channels
= qdict_haskey(qdict
, "nchannels");
2385 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2388 s
= qemu_mallocz (sizeof (*s
));
2390 freq
= has_freq
? freq
: 44100;
2391 bits
= has_bits
? bits
: 16;
2392 nchannels
= has_channels
? nchannels
: 2;
2394 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2395 monitor_printf(mon
, "Faied to add wave capture\n");
2398 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2402 #if defined(TARGET_I386)
2403 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
2406 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2408 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2409 if (env
->cpu_index
== cpu_index
) {
2410 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2416 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2420 qdict
= qobject_to_qdict(data
);
2422 monitor_printf(mon
, "VM status: ");
2423 if (qdict_get_bool(qdict
, "running")) {
2424 monitor_printf(mon
, "running");
2425 if (qdict_get_bool(qdict
, "singlestep")) {
2426 monitor_printf(mon
, " (single step mode)");
2429 monitor_printf(mon
, "paused");
2432 monitor_printf(mon
, "\n");
2435 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2437 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2438 vm_running
, singlestep
);
2441 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2443 qemu_acl
*acl
= qemu_acl_find(name
);
2446 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2451 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2453 const char *aclname
= qdict_get_str(qdict
, "aclname");
2454 qemu_acl
*acl
= find_acl(mon
, aclname
);
2455 qemu_acl_entry
*entry
;
2459 monitor_printf(mon
, "policy: %s\n",
2460 acl
->defaultDeny
? "deny" : "allow");
2461 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2463 monitor_printf(mon
, "%d: %s %s\n", i
,
2464 entry
->deny
? "deny" : "allow", entry
->match
);
2469 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2471 const char *aclname
= qdict_get_str(qdict
, "aclname");
2472 qemu_acl
*acl
= find_acl(mon
, aclname
);
2475 qemu_acl_reset(acl
);
2476 monitor_printf(mon
, "acl: removed all rules\n");
2480 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2482 const char *aclname
= qdict_get_str(qdict
, "aclname");
2483 const char *policy
= qdict_get_str(qdict
, "policy");
2484 qemu_acl
*acl
= find_acl(mon
, aclname
);
2487 if (strcmp(policy
, "allow") == 0) {
2488 acl
->defaultDeny
= 0;
2489 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2490 } else if (strcmp(policy
, "deny") == 0) {
2491 acl
->defaultDeny
= 1;
2492 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2494 monitor_printf(mon
, "acl: unknown policy '%s', "
2495 "expected 'deny' or 'allow'\n", policy
);
2500 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2502 const char *aclname
= qdict_get_str(qdict
, "aclname");
2503 const char *match
= qdict_get_str(qdict
, "match");
2504 const char *policy
= qdict_get_str(qdict
, "policy");
2505 int has_index
= qdict_haskey(qdict
, "index");
2506 int index
= qdict_get_try_int(qdict
, "index", -1);
2507 qemu_acl
*acl
= find_acl(mon
, aclname
);
2511 if (strcmp(policy
, "allow") == 0) {
2513 } else if (strcmp(policy
, "deny") == 0) {
2516 monitor_printf(mon
, "acl: unknown policy '%s', "
2517 "expected 'deny' or 'allow'\n", policy
);
2521 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2523 ret
= qemu_acl_append(acl
, deny
, match
);
2525 monitor_printf(mon
, "acl: unable to add acl entry\n");
2527 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2531 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2533 const char *aclname
= qdict_get_str(qdict
, "aclname");
2534 const char *match
= qdict_get_str(qdict
, "match");
2535 qemu_acl
*acl
= find_acl(mon
, aclname
);
2539 ret
= qemu_acl_remove(acl
, match
);
2541 monitor_printf(mon
, "acl: no matching acl entry\n");
2543 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2547 #if defined(TARGET_I386)
2548 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2551 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2552 int bank
= qdict_get_int(qdict
, "bank");
2553 uint64_t status
= qdict_get_int(qdict
, "status");
2554 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2555 uint64_t addr
= qdict_get_int(qdict
, "addr");
2556 uint64_t misc
= qdict_get_int(qdict
, "misc");
2558 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
2559 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
2560 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
2566 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2568 const char *fdname
= qdict_get_str(qdict
, "fdname");
2572 fd
= qemu_chr_get_msgfd(mon
->chr
);
2574 qerror_report(QERR_FD_NOT_SUPPLIED
);
2578 if (qemu_isdigit(fdname
[0])) {
2579 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2580 "a name not starting with a digit");
2584 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2585 if (strcmp(monfd
->name
, fdname
) != 0) {
2594 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
2595 monfd
->name
= qemu_strdup(fdname
);
2598 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2602 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2604 const char *fdname
= qdict_get_str(qdict
, "fdname");
2607 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2608 if (strcmp(monfd
->name
, fdname
) != 0) {
2612 QLIST_REMOVE(monfd
, next
);
2614 qemu_free(monfd
->name
);
2619 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2623 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2625 int saved_vm_running
= vm_running
;
2626 const char *name
= qdict_get_str(qdict
, "name");
2630 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2635 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2639 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2642 if (strcmp(monfd
->name
, fdname
) != 0) {
2648 /* caller takes ownership of fd */
2649 QLIST_REMOVE(monfd
, next
);
2650 qemu_free(monfd
->name
);
2659 static const mon_cmd_t mon_cmds
[] = {
2660 #include "hmp-commands.h"
2664 /* Please update hmp-commands.hx when adding or changing commands */
2665 static const mon_cmd_t info_cmds
[] = {
2670 .help
= "show the version of QEMU",
2671 .user_print
= do_info_version_print
,
2672 .mhandler
.info_new
= do_info_version
,
2678 .help
= "show the network state",
2679 .mhandler
.info
= do_info_network
,
2685 .help
= "show the character devices",
2686 .user_print
= qemu_chr_info_print
,
2687 .mhandler
.info_new
= qemu_chr_info
,
2693 .help
= "show the block devices",
2694 .user_print
= bdrv_info_print
,
2695 .mhandler
.info_new
= bdrv_info
,
2698 .name
= "blockstats",
2701 .help
= "show block device statistics",
2702 .user_print
= bdrv_stats_print
,
2703 .mhandler
.info_new
= bdrv_info_stats
,
2706 .name
= "registers",
2709 .help
= "show the cpu registers",
2710 .mhandler
.info
= do_info_registers
,
2716 .help
= "show infos for each CPU",
2717 .user_print
= monitor_print_cpus
,
2718 .mhandler
.info_new
= do_info_cpus
,
2724 .help
= "show the command line history",
2725 .mhandler
.info
= do_info_history
,
2731 .help
= "show the interrupts statistics (if available)",
2732 .mhandler
.info
= irq_info
,
2738 .help
= "show i8259 (PIC) state",
2739 .mhandler
.info
= pic_info
,
2745 .help
= "show PCI info",
2746 .user_print
= do_pci_info_print
,
2747 .mhandler
.info_new
= do_pci_info
,
2749 #if defined(TARGET_I386) || defined(TARGET_SH4)
2754 .help
= "show virtual to physical memory mappings",
2755 .mhandler
.info
= tlb_info
,
2758 #if defined(TARGET_I386)
2763 .help
= "show the active virtual memory mappings",
2764 .mhandler
.info
= mem_info
,
2771 .help
= "show dynamic compiler info",
2772 .mhandler
.info
= do_info_jit
,
2778 .help
= "show KVM information",
2779 .user_print
= do_info_kvm_print
,
2780 .mhandler
.info_new
= do_info_kvm
,
2786 .help
= "show NUMA information",
2787 .mhandler
.info
= do_info_numa
,
2793 .help
= "show guest USB devices",
2794 .mhandler
.info
= usb_info
,
2800 .help
= "show host USB devices",
2801 .mhandler
.info
= usb_host_info
,
2807 .help
= "show profiling information",
2808 .mhandler
.info
= do_info_profile
,
2814 .help
= "show capture information",
2815 .mhandler
.info
= do_info_capture
,
2818 .name
= "snapshots",
2821 .help
= "show the currently saved VM snapshots",
2822 .mhandler
.info
= do_info_snapshots
,
2828 .help
= "show the current VM status (running|paused)",
2829 .user_print
= do_info_status_print
,
2830 .mhandler
.info_new
= do_info_status
,
2836 .help
= "show guest PCMCIA status",
2837 .mhandler
.info
= pcmcia_info
,
2843 .help
= "show which guest mouse is receiving events",
2844 .user_print
= do_info_mice_print
,
2845 .mhandler
.info_new
= do_info_mice
,
2851 .help
= "show the vnc server status",
2852 .user_print
= do_info_vnc_print
,
2853 .mhandler
.info_new
= do_info_vnc
,
2859 .help
= "show the current VM name",
2860 .user_print
= do_info_name_print
,
2861 .mhandler
.info_new
= do_info_name
,
2867 .help
= "show the current VM UUID",
2868 .user_print
= do_info_uuid_print
,
2869 .mhandler
.info_new
= do_info_uuid
,
2871 #if defined(TARGET_PPC)
2876 .help
= "show CPU statistics",
2877 .mhandler
.info
= do_info_cpu_stats
,
2880 #if defined(CONFIG_SLIRP)
2885 .help
= "show user network stack connection states",
2886 .mhandler
.info
= do_info_usernet
,
2893 .help
= "show migration status",
2894 .user_print
= do_info_migrate_print
,
2895 .mhandler
.info_new
= do_info_migrate
,
2901 .help
= "show balloon information",
2902 .user_print
= monitor_print_balloon
,
2903 .mhandler
.info_async
= do_info_balloon
,
2904 .flags
= MONITOR_CMD_ASYNC
,
2910 .help
= "show device tree",
2911 .mhandler
.info
= do_info_qtree
,
2917 .help
= "show qdev device model list",
2918 .mhandler
.info
= do_info_qdm
,
2924 .help
= "show roms",
2925 .mhandler
.info
= do_info_roms
,
2927 #if defined(CONFIG_SIMPLE_TRACE)
2932 .help
= "show current contents of trace buffer",
2933 .mhandler
.info
= do_info_trace
,
2936 .name
= "trace-events",
2939 .help
= "show available trace-events & their state",
2940 .mhandler
.info
= do_info_trace_events
,
2948 static const mon_cmd_t qmp_cmds
[] = {
2949 #include "qmp-commands.h"
2953 static const mon_cmd_t qmp_query_cmds
[] = {
2958 .help
= "show the version of QEMU",
2959 .user_print
= do_info_version_print
,
2960 .mhandler
.info_new
= do_info_version
,
2966 .help
= "list QMP available commands",
2967 .user_print
= monitor_user_noop
,
2968 .mhandler
.info_new
= do_info_commands
,
2974 .help
= "show the character devices",
2975 .user_print
= qemu_chr_info_print
,
2976 .mhandler
.info_new
= qemu_chr_info
,
2982 .help
= "show the block devices",
2983 .user_print
= bdrv_info_print
,
2984 .mhandler
.info_new
= bdrv_info
,
2987 .name
= "blockstats",
2990 .help
= "show block device statistics",
2991 .user_print
= bdrv_stats_print
,
2992 .mhandler
.info_new
= bdrv_info_stats
,
2998 .help
= "show infos for each CPU",
2999 .user_print
= monitor_print_cpus
,
3000 .mhandler
.info_new
= do_info_cpus
,
3006 .help
= "show PCI info",
3007 .user_print
= do_pci_info_print
,
3008 .mhandler
.info_new
= do_pci_info
,
3014 .help
= "show KVM information",
3015 .user_print
= do_info_kvm_print
,
3016 .mhandler
.info_new
= do_info_kvm
,
3022 .help
= "show the current VM status (running|paused)",
3023 .user_print
= do_info_status_print
,
3024 .mhandler
.info_new
= do_info_status
,
3030 .help
= "show which guest mouse is receiving events",
3031 .user_print
= do_info_mice_print
,
3032 .mhandler
.info_new
= do_info_mice
,
3038 .help
= "show the vnc server status",
3039 .user_print
= do_info_vnc_print
,
3040 .mhandler
.info_new
= do_info_vnc
,
3046 .help
= "show the current VM name",
3047 .user_print
= do_info_name_print
,
3048 .mhandler
.info_new
= do_info_name
,
3054 .help
= "show the current VM UUID",
3055 .user_print
= do_info_uuid_print
,
3056 .mhandler
.info_new
= do_info_uuid
,
3062 .help
= "show migration status",
3063 .user_print
= do_info_migrate_print
,
3064 .mhandler
.info_new
= do_info_migrate
,
3070 .help
= "show balloon information",
3071 .user_print
= monitor_print_balloon
,
3072 .mhandler
.info_async
= do_info_balloon
,
3073 .flags
= MONITOR_CMD_ASYNC
,
3078 /*******************************************************************/
3080 static const char *pch
;
3081 static jmp_buf expr_env
;
3086 typedef struct MonitorDef
{
3089 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
3093 #if defined(TARGET_I386)
3094 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
3096 CPUState
*env
= mon_get_cpu();
3097 return env
->eip
+ env
->segs
[R_CS
].base
;
3101 #if defined(TARGET_PPC)
3102 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3104 CPUState
*env
= mon_get_cpu();
3109 for (i
= 0; i
< 8; i
++)
3110 u
|= env
->crf
[i
] << (32 - (4 * i
));
3115 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3117 CPUState
*env
= mon_get_cpu();
3121 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3123 CPUState
*env
= mon_get_cpu();
3127 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3129 CPUState
*env
= mon_get_cpu();
3130 return cpu_ppc_load_decr(env
);
3133 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3135 CPUState
*env
= mon_get_cpu();
3136 return cpu_ppc_load_tbu(env
);
3139 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3141 CPUState
*env
= mon_get_cpu();
3142 return cpu_ppc_load_tbl(env
);
3146 #if defined(TARGET_SPARC)
3147 #ifndef TARGET_SPARC64
3148 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3150 CPUState
*env
= mon_get_cpu();
3152 return cpu_get_psr(env
);
3156 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3158 CPUState
*env
= mon_get_cpu();
3159 return env
->regwptr
[val
];
3163 static const MonitorDef monitor_defs
[] = {
3166 #define SEG(name, seg) \
3167 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3168 { name ".base", offsetof(CPUState, segs[seg].base) },\
3169 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3171 { "eax", offsetof(CPUState
, regs
[0]) },
3172 { "ecx", offsetof(CPUState
, regs
[1]) },
3173 { "edx", offsetof(CPUState
, regs
[2]) },
3174 { "ebx", offsetof(CPUState
, regs
[3]) },
3175 { "esp|sp", offsetof(CPUState
, regs
[4]) },
3176 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
3177 { "esi", offsetof(CPUState
, regs
[6]) },
3178 { "edi", offsetof(CPUState
, regs
[7]) },
3179 #ifdef TARGET_X86_64
3180 { "r8", offsetof(CPUState
, regs
[8]) },
3181 { "r9", offsetof(CPUState
, regs
[9]) },
3182 { "r10", offsetof(CPUState
, regs
[10]) },
3183 { "r11", offsetof(CPUState
, regs
[11]) },
3184 { "r12", offsetof(CPUState
, regs
[12]) },
3185 { "r13", offsetof(CPUState
, regs
[13]) },
3186 { "r14", offsetof(CPUState
, regs
[14]) },
3187 { "r15", offsetof(CPUState
, regs
[15]) },
3189 { "eflags", offsetof(CPUState
, eflags
) },
3190 { "eip", offsetof(CPUState
, eip
) },
3197 { "pc", 0, monitor_get_pc
, },
3198 #elif defined(TARGET_PPC)
3199 /* General purpose registers */
3200 { "r0", offsetof(CPUState
, gpr
[0]) },
3201 { "r1", offsetof(CPUState
, gpr
[1]) },
3202 { "r2", offsetof(CPUState
, gpr
[2]) },
3203 { "r3", offsetof(CPUState
, gpr
[3]) },
3204 { "r4", offsetof(CPUState
, gpr
[4]) },
3205 { "r5", offsetof(CPUState
, gpr
[5]) },
3206 { "r6", offsetof(CPUState
, gpr
[6]) },
3207 { "r7", offsetof(CPUState
, gpr
[7]) },
3208 { "r8", offsetof(CPUState
, gpr
[8]) },
3209 { "r9", offsetof(CPUState
, gpr
[9]) },
3210 { "r10", offsetof(CPUState
, gpr
[10]) },
3211 { "r11", offsetof(CPUState
, gpr
[11]) },
3212 { "r12", offsetof(CPUState
, gpr
[12]) },
3213 { "r13", offsetof(CPUState
, gpr
[13]) },
3214 { "r14", offsetof(CPUState
, gpr
[14]) },
3215 { "r15", offsetof(CPUState
, gpr
[15]) },
3216 { "r16", offsetof(CPUState
, gpr
[16]) },
3217 { "r17", offsetof(CPUState
, gpr
[17]) },
3218 { "r18", offsetof(CPUState
, gpr
[18]) },
3219 { "r19", offsetof(CPUState
, gpr
[19]) },
3220 { "r20", offsetof(CPUState
, gpr
[20]) },
3221 { "r21", offsetof(CPUState
, gpr
[21]) },
3222 { "r22", offsetof(CPUState
, gpr
[22]) },
3223 { "r23", offsetof(CPUState
, gpr
[23]) },
3224 { "r24", offsetof(CPUState
, gpr
[24]) },
3225 { "r25", offsetof(CPUState
, gpr
[25]) },
3226 { "r26", offsetof(CPUState
, gpr
[26]) },
3227 { "r27", offsetof(CPUState
, gpr
[27]) },
3228 { "r28", offsetof(CPUState
, gpr
[28]) },
3229 { "r29", offsetof(CPUState
, gpr
[29]) },
3230 { "r30", offsetof(CPUState
, gpr
[30]) },
3231 { "r31", offsetof(CPUState
, gpr
[31]) },
3232 /* Floating point registers */
3233 { "f0", offsetof(CPUState
, fpr
[0]) },
3234 { "f1", offsetof(CPUState
, fpr
[1]) },
3235 { "f2", offsetof(CPUState
, fpr
[2]) },
3236 { "f3", offsetof(CPUState
, fpr
[3]) },
3237 { "f4", offsetof(CPUState
, fpr
[4]) },
3238 { "f5", offsetof(CPUState
, fpr
[5]) },
3239 { "f6", offsetof(CPUState
, fpr
[6]) },
3240 { "f7", offsetof(CPUState
, fpr
[7]) },
3241 { "f8", offsetof(CPUState
, fpr
[8]) },
3242 { "f9", offsetof(CPUState
, fpr
[9]) },
3243 { "f10", offsetof(CPUState
, fpr
[10]) },
3244 { "f11", offsetof(CPUState
, fpr
[11]) },
3245 { "f12", offsetof(CPUState
, fpr
[12]) },
3246 { "f13", offsetof(CPUState
, fpr
[13]) },
3247 { "f14", offsetof(CPUState
, fpr
[14]) },
3248 { "f15", offsetof(CPUState
, fpr
[15]) },
3249 { "f16", offsetof(CPUState
, fpr
[16]) },
3250 { "f17", offsetof(CPUState
, fpr
[17]) },
3251 { "f18", offsetof(CPUState
, fpr
[18]) },
3252 { "f19", offsetof(CPUState
, fpr
[19]) },
3253 { "f20", offsetof(CPUState
, fpr
[20]) },
3254 { "f21", offsetof(CPUState
, fpr
[21]) },
3255 { "f22", offsetof(CPUState
, fpr
[22]) },
3256 { "f23", offsetof(CPUState
, fpr
[23]) },
3257 { "f24", offsetof(CPUState
, fpr
[24]) },
3258 { "f25", offsetof(CPUState
, fpr
[25]) },
3259 { "f26", offsetof(CPUState
, fpr
[26]) },
3260 { "f27", offsetof(CPUState
, fpr
[27]) },
3261 { "f28", offsetof(CPUState
, fpr
[28]) },
3262 { "f29", offsetof(CPUState
, fpr
[29]) },
3263 { "f30", offsetof(CPUState
, fpr
[30]) },
3264 { "f31", offsetof(CPUState
, fpr
[31]) },
3265 { "fpscr", offsetof(CPUState
, fpscr
) },
3266 /* Next instruction pointer */
3267 { "nip|pc", offsetof(CPUState
, nip
) },
3268 { "lr", offsetof(CPUState
, lr
) },
3269 { "ctr", offsetof(CPUState
, ctr
) },
3270 { "decr", 0, &monitor_get_decr
, },
3271 { "ccr", 0, &monitor_get_ccr
, },
3272 /* Machine state register */
3273 { "msr", 0, &monitor_get_msr
, },
3274 { "xer", 0, &monitor_get_xer
, },
3275 { "tbu", 0, &monitor_get_tbu
, },
3276 { "tbl", 0, &monitor_get_tbl
, },
3277 #if defined(TARGET_PPC64)
3278 /* Address space register */
3279 { "asr", offsetof(CPUState
, asr
) },
3281 /* Segment registers */
3282 { "sdr1", offsetof(CPUState
, sdr1
) },
3283 { "sr0", offsetof(CPUState
, sr
[0]) },
3284 { "sr1", offsetof(CPUState
, sr
[1]) },
3285 { "sr2", offsetof(CPUState
, sr
[2]) },
3286 { "sr3", offsetof(CPUState
, sr
[3]) },
3287 { "sr4", offsetof(CPUState
, sr
[4]) },
3288 { "sr5", offsetof(CPUState
, sr
[5]) },
3289 { "sr6", offsetof(CPUState
, sr
[6]) },
3290 { "sr7", offsetof(CPUState
, sr
[7]) },
3291 { "sr8", offsetof(CPUState
, sr
[8]) },
3292 { "sr9", offsetof(CPUState
, sr
[9]) },
3293 { "sr10", offsetof(CPUState
, sr
[10]) },
3294 { "sr11", offsetof(CPUState
, sr
[11]) },
3295 { "sr12", offsetof(CPUState
, sr
[12]) },
3296 { "sr13", offsetof(CPUState
, sr
[13]) },
3297 { "sr14", offsetof(CPUState
, sr
[14]) },
3298 { "sr15", offsetof(CPUState
, sr
[15]) },
3299 /* Too lazy to put BATs and SPRs ... */
3300 #elif defined(TARGET_SPARC)
3301 { "g0", offsetof(CPUState
, gregs
[0]) },
3302 { "g1", offsetof(CPUState
, gregs
[1]) },
3303 { "g2", offsetof(CPUState
, gregs
[2]) },
3304 { "g3", offsetof(CPUState
, gregs
[3]) },
3305 { "g4", offsetof(CPUState
, gregs
[4]) },
3306 { "g5", offsetof(CPUState
, gregs
[5]) },
3307 { "g6", offsetof(CPUState
, gregs
[6]) },
3308 { "g7", offsetof(CPUState
, gregs
[7]) },
3309 { "o0", 0, monitor_get_reg
},
3310 { "o1", 1, monitor_get_reg
},
3311 { "o2", 2, monitor_get_reg
},
3312 { "o3", 3, monitor_get_reg
},
3313 { "o4", 4, monitor_get_reg
},
3314 { "o5", 5, monitor_get_reg
},
3315 { "o6", 6, monitor_get_reg
},
3316 { "o7", 7, monitor_get_reg
},
3317 { "l0", 8, monitor_get_reg
},
3318 { "l1", 9, monitor_get_reg
},
3319 { "l2", 10, monitor_get_reg
},
3320 { "l3", 11, monitor_get_reg
},
3321 { "l4", 12, monitor_get_reg
},
3322 { "l5", 13, monitor_get_reg
},
3323 { "l6", 14, monitor_get_reg
},
3324 { "l7", 15, monitor_get_reg
},
3325 { "i0", 16, monitor_get_reg
},
3326 { "i1", 17, monitor_get_reg
},
3327 { "i2", 18, monitor_get_reg
},
3328 { "i3", 19, monitor_get_reg
},
3329 { "i4", 20, monitor_get_reg
},
3330 { "i5", 21, monitor_get_reg
},
3331 { "i6", 22, monitor_get_reg
},
3332 { "i7", 23, monitor_get_reg
},
3333 { "pc", offsetof(CPUState
, pc
) },
3334 { "npc", offsetof(CPUState
, npc
) },
3335 { "y", offsetof(CPUState
, y
) },
3336 #ifndef TARGET_SPARC64
3337 { "psr", 0, &monitor_get_psr
, },
3338 { "wim", offsetof(CPUState
, wim
) },
3340 { "tbr", offsetof(CPUState
, tbr
) },
3341 { "fsr", offsetof(CPUState
, fsr
) },
3342 { "f0", offsetof(CPUState
, fpr
[0]) },
3343 { "f1", offsetof(CPUState
, fpr
[1]) },
3344 { "f2", offsetof(CPUState
, fpr
[2]) },
3345 { "f3", offsetof(CPUState
, fpr
[3]) },
3346 { "f4", offsetof(CPUState
, fpr
[4]) },
3347 { "f5", offsetof(CPUState
, fpr
[5]) },
3348 { "f6", offsetof(CPUState
, fpr
[6]) },
3349 { "f7", offsetof(CPUState
, fpr
[7]) },
3350 { "f8", offsetof(CPUState
, fpr
[8]) },
3351 { "f9", offsetof(CPUState
, fpr
[9]) },
3352 { "f10", offsetof(CPUState
, fpr
[10]) },
3353 { "f11", offsetof(CPUState
, fpr
[11]) },
3354 { "f12", offsetof(CPUState
, fpr
[12]) },
3355 { "f13", offsetof(CPUState
, fpr
[13]) },
3356 { "f14", offsetof(CPUState
, fpr
[14]) },
3357 { "f15", offsetof(CPUState
, fpr
[15]) },
3358 { "f16", offsetof(CPUState
, fpr
[16]) },
3359 { "f17", offsetof(CPUState
, fpr
[17]) },
3360 { "f18", offsetof(CPUState
, fpr
[18]) },
3361 { "f19", offsetof(CPUState
, fpr
[19]) },
3362 { "f20", offsetof(CPUState
, fpr
[20]) },
3363 { "f21", offsetof(CPUState
, fpr
[21]) },
3364 { "f22", offsetof(CPUState
, fpr
[22]) },
3365 { "f23", offsetof(CPUState
, fpr
[23]) },
3366 { "f24", offsetof(CPUState
, fpr
[24]) },
3367 { "f25", offsetof(CPUState
, fpr
[25]) },
3368 { "f26", offsetof(CPUState
, fpr
[26]) },
3369 { "f27", offsetof(CPUState
, fpr
[27]) },
3370 { "f28", offsetof(CPUState
, fpr
[28]) },
3371 { "f29", offsetof(CPUState
, fpr
[29]) },
3372 { "f30", offsetof(CPUState
, fpr
[30]) },
3373 { "f31", offsetof(CPUState
, fpr
[31]) },
3374 #ifdef TARGET_SPARC64
3375 { "f32", offsetof(CPUState
, fpr
[32]) },
3376 { "f34", offsetof(CPUState
, fpr
[34]) },
3377 { "f36", offsetof(CPUState
, fpr
[36]) },
3378 { "f38", offsetof(CPUState
, fpr
[38]) },
3379 { "f40", offsetof(CPUState
, fpr
[40]) },
3380 { "f42", offsetof(CPUState
, fpr
[42]) },
3381 { "f44", offsetof(CPUState
, fpr
[44]) },
3382 { "f46", offsetof(CPUState
, fpr
[46]) },
3383 { "f48", offsetof(CPUState
, fpr
[48]) },
3384 { "f50", offsetof(CPUState
, fpr
[50]) },
3385 { "f52", offsetof(CPUState
, fpr
[52]) },
3386 { "f54", offsetof(CPUState
, fpr
[54]) },
3387 { "f56", offsetof(CPUState
, fpr
[56]) },
3388 { "f58", offsetof(CPUState
, fpr
[58]) },
3389 { "f60", offsetof(CPUState
, fpr
[60]) },
3390 { "f62", offsetof(CPUState
, fpr
[62]) },
3391 { "asi", offsetof(CPUState
, asi
) },
3392 { "pstate", offsetof(CPUState
, pstate
) },
3393 { "cansave", offsetof(CPUState
, cansave
) },
3394 { "canrestore", offsetof(CPUState
, canrestore
) },
3395 { "otherwin", offsetof(CPUState
, otherwin
) },
3396 { "wstate", offsetof(CPUState
, wstate
) },
3397 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3398 { "fprs", offsetof(CPUState
, fprs
) },
3404 static void expr_error(Monitor
*mon
, const char *msg
)
3406 monitor_printf(mon
, "%s\n", msg
);
3407 longjmp(expr_env
, 1);
3410 /* return 0 if OK, -1 if not found */
3411 static int get_monitor_def(target_long
*pval
, const char *name
)
3413 const MonitorDef
*md
;
3416 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3417 if (compare_cmd(name
, md
->name
)) {
3418 if (md
->get_value
) {
3419 *pval
= md
->get_value(md
, md
->offset
);
3421 CPUState
*env
= mon_get_cpu();
3422 ptr
= (uint8_t *)env
+ md
->offset
;
3425 *pval
= *(int32_t *)ptr
;
3428 *pval
= *(target_long
*)ptr
;
3441 static void next(void)
3445 while (qemu_isspace(*pch
))
3450 static int64_t expr_sum(Monitor
*mon
);
3452 static int64_t expr_unary(Monitor
*mon
)
3461 n
= expr_unary(mon
);
3465 n
= -expr_unary(mon
);
3469 n
= ~expr_unary(mon
);
3475 expr_error(mon
, "')' expected");
3482 expr_error(mon
, "character constant expected");
3486 expr_error(mon
, "missing terminating \' character");
3496 while ((*pch
>= 'a' && *pch
<= 'z') ||
3497 (*pch
>= 'A' && *pch
<= 'Z') ||
3498 (*pch
>= '0' && *pch
<= '9') ||
3499 *pch
== '_' || *pch
== '.') {
3500 if ((q
- buf
) < sizeof(buf
) - 1)
3504 while (qemu_isspace(*pch
))
3507 ret
= get_monitor_def(®
, buf
);
3509 expr_error(mon
, "unknown register");
3514 expr_error(mon
, "unexpected end of expression");
3518 #if TARGET_PHYS_ADDR_BITS > 32
3519 n
= strtoull(pch
, &p
, 0);
3521 n
= strtoul(pch
, &p
, 0);
3524 expr_error(mon
, "invalid char in expression");
3527 while (qemu_isspace(*pch
))
3535 static int64_t expr_prod(Monitor
*mon
)
3540 val
= expr_unary(mon
);
3543 if (op
!= '*' && op
!= '/' && op
!= '%')
3546 val2
= expr_unary(mon
);
3555 expr_error(mon
, "division by zero");
3566 static int64_t expr_logic(Monitor
*mon
)
3571 val
= expr_prod(mon
);
3574 if (op
!= '&' && op
!= '|' && op
!= '^')
3577 val2
= expr_prod(mon
);
3594 static int64_t expr_sum(Monitor
*mon
)
3599 val
= expr_logic(mon
);
3602 if (op
!= '+' && op
!= '-')
3605 val2
= expr_logic(mon
);
3614 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3617 if (setjmp(expr_env
)) {
3621 while (qemu_isspace(*pch
))
3623 *pval
= expr_sum(mon
);
3628 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3630 const char *p
= *pp
;
3634 d
= strtod(p
, &tailp
);
3636 monitor_printf(mon
, "Number expected\n");
3639 if (d
!= d
|| d
- d
!= 0) {
3640 /* NaN or infinity */
3641 monitor_printf(mon
, "Bad number\n");
3649 static int get_str(char *buf
, int buf_size
, const char **pp
)
3657 while (qemu_isspace(*p
))
3667 while (*p
!= '\0' && *p
!= '\"') {
3683 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3686 if ((q
- buf
) < buf_size
- 1) {
3690 if ((q
- buf
) < buf_size
- 1) {
3697 qemu_printf("unterminated string\n");
3702 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3703 if ((q
- buf
) < buf_size
- 1) {
3715 * Store the command-name in cmdname, and return a pointer to
3716 * the remaining of the command string.
3718 static const char *get_command_name(const char *cmdline
,
3719 char *cmdname
, size_t nlen
)
3722 const char *p
, *pstart
;
3725 while (qemu_isspace(*p
))
3730 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3735 memcpy(cmdname
, pstart
, len
);
3736 cmdname
[len
] = '\0';
3741 * Read key of 'type' into 'key' and return the current
3744 static char *key_get_info(const char *type
, char **key
)
3752 p
= strchr(type
, ':');
3759 str
= qemu_malloc(len
+ 1);
3760 memcpy(str
, type
, len
);
3767 static int default_fmt_format
= 'x';
3768 static int default_fmt_size
= 4;
3772 static int is_valid_option(const char *c
, const char *typestr
)
3780 typestr
= strstr(typestr
, option
);
3781 return (typestr
!= NULL
);
3784 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3785 const char *cmdname
)
3787 const mon_cmd_t
*cmd
;
3789 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3790 if (compare_cmd(cmdname
, cmd
->name
)) {
3798 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3800 return search_dispatch_table(mon_cmds
, cmdname
);
3803 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
3805 return search_dispatch_table(qmp_query_cmds
, info_item
);
3808 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3810 return search_dispatch_table(qmp_cmds
, cmdname
);
3813 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3814 const char *cmdline
,
3817 const char *p
, *typestr
;
3819 const mon_cmd_t
*cmd
;
3825 monitor_printf(mon
, "command='%s'\n", cmdline
);
3828 /* extract the command name */
3829 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3833 cmd
= monitor_find_command(cmdname
);
3835 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3839 /* parse the parameters */
3840 typestr
= cmd
->args_type
;
3842 typestr
= key_get_info(typestr
, &key
);
3854 while (qemu_isspace(*p
))
3856 if (*typestr
== '?') {
3859 /* no optional string: NULL argument */
3863 ret
= get_str(buf
, sizeof(buf
), &p
);
3867 monitor_printf(mon
, "%s: filename expected\n",
3871 monitor_printf(mon
, "%s: block device name expected\n",
3875 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3880 qdict_put(qdict
, key
, qstring_from_str(buf
));
3885 QemuOptsList
*opts_list
;
3888 opts_list
= qemu_find_opts(key
);
3889 if (!opts_list
|| opts_list
->desc
->name
) {
3892 while (qemu_isspace(*p
)) {
3897 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3900 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3904 qemu_opts_to_qdict(opts
, qdict
);
3905 qemu_opts_del(opts
);
3910 int count
, format
, size
;
3912 while (qemu_isspace(*p
))
3918 if (qemu_isdigit(*p
)) {
3920 while (qemu_isdigit(*p
)) {
3921 count
= count
* 10 + (*p
- '0');
3959 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3960 monitor_printf(mon
, "invalid char in format: '%c'\n",
3965 format
= default_fmt_format
;
3966 if (format
!= 'i') {
3967 /* for 'i', not specifying a size gives -1 as size */
3969 size
= default_fmt_size
;
3970 default_fmt_size
= size
;
3972 default_fmt_format
= format
;
3975 format
= default_fmt_format
;
3976 if (format
!= 'i') {
3977 size
= default_fmt_size
;
3982 qdict_put(qdict
, "count", qint_from_int(count
));
3983 qdict_put(qdict
, "format", qint_from_int(format
));
3984 qdict_put(qdict
, "size", qint_from_int(size
));
3993 while (qemu_isspace(*p
))
3995 if (*typestr
== '?' || *typestr
== '.') {
3996 if (*typestr
== '?') {
4004 while (qemu_isspace(*p
))
4013 if (get_expr(mon
, &val
, &p
))
4015 /* Check if 'i' is greater than 32-bit */
4016 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
4017 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
4018 monitor_printf(mon
, "integer is for 32-bit values\n");
4020 } else if (c
== 'M') {
4023 qdict_put(qdict
, key
, qint_from_int(val
));
4031 while (qemu_isspace(*p
)) {
4034 if (*typestr
== '?') {
4040 val
= strtosz(p
, &end
);
4042 monitor_printf(mon
, "invalid size\n");
4045 qdict_put(qdict
, key
, qint_from_int(val
));
4053 while (qemu_isspace(*p
))
4055 if (*typestr
== '?') {
4061 if (get_double(mon
, &val
, &p
) < 0) {
4064 if (p
[0] && p
[1] == 's') {
4067 val
/= 1e3
; p
+= 2; break;
4069 val
/= 1e6
; p
+= 2; break;
4071 val
/= 1e9
; p
+= 2; break;
4074 if (*p
&& !qemu_isspace(*p
)) {
4075 monitor_printf(mon
, "Unknown unit suffix\n");
4078 qdict_put(qdict
, key
, qfloat_from_double(val
));
4086 while (qemu_isspace(*p
)) {
4090 while (qemu_isgraph(*p
)) {
4093 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4095 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4098 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4101 qdict_put(qdict
, key
, qbool_from_int(val
));
4106 const char *tmp
= p
;
4113 while (qemu_isspace(*p
))
4118 if(!is_valid_option(p
, typestr
)) {
4120 monitor_printf(mon
, "%s: unsupported option -%c\n",
4132 qdict_put(qdict
, key
, qbool_from_int(1));
4139 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4145 /* check that all arguments were parsed */
4146 while (qemu_isspace(*p
))
4149 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4161 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4163 /* report only the first error */
4165 mon
->error
= qerror
;
4167 MON_DEBUG("Additional error report at %s:%d\n",
4168 qerror
->file
, qerror
->linenr
);
4173 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4175 if (monitor_ctrl_mode(mon
)) {
4176 if (ret
&& !monitor_has_error(mon
)) {
4178 * If it returns failure, it must have passed on error.
4180 * Action: Report an internal error to the client if in QMP.
4182 qerror_report(QERR_UNDEFINED_ERROR
);
4183 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4187 #ifdef CONFIG_DEBUG_MONITOR
4188 if (!ret
&& monitor_has_error(mon
)) {
4190 * If it returns success, it must not have passed an error.
4192 * Action: Report the passed error to the client.
4194 MON_DEBUG("command '%s' returned success but passed an error\n",
4198 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
4200 * Handlers should not call Monitor print functions.
4202 * Action: Ignore them in QMP.
4204 * (XXX: we don't check any 'info' or 'query' command here
4205 * because the user print function _is_ called by do_info(), hence
4206 * we will trigger this check. This problem will go away when we
4207 * make 'query' commands real and kill do_info())
4209 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4210 cmd
->name
, mon_print_count_get(mon
));
4214 assert(!monitor_has_error(mon
));
4215 QDECREF(mon
->error
);
4220 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4223 const mon_cmd_t
*cmd
;
4225 qdict
= qdict_new();
4227 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4231 if (handler_is_async(cmd
)) {
4232 user_async_cmd_handler(mon
, cmd
, qdict
);
4233 } else if (handler_is_qobject(cmd
)) {
4234 QObject
*data
= NULL
;
4236 /* XXX: ignores the error code */
4237 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4238 assert(!monitor_has_error(mon
));
4240 cmd
->user_print(mon
, data
);
4241 qobject_decref(data
);
4244 cmd
->mhandler
.cmd(mon
, qdict
);
4251 static void cmd_completion(const char *name
, const char *list
)
4253 const char *p
, *pstart
;
4262 p
= pstart
+ strlen(pstart
);
4264 if (len
> sizeof(cmd
) - 2)
4265 len
= sizeof(cmd
) - 2;
4266 memcpy(cmd
, pstart
, len
);
4268 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4269 readline_add_completion(cur_mon
->rs
, cmd
);
4277 static void file_completion(const char *input
)
4282 char file
[1024], file_prefix
[1024];
4286 p
= strrchr(input
, '/');
4289 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4290 pstrcpy(path
, sizeof(path
), ".");
4292 input_path_len
= p
- input
+ 1;
4293 memcpy(path
, input
, input_path_len
);
4294 if (input_path_len
> sizeof(path
) - 1)
4295 input_path_len
= sizeof(path
) - 1;
4296 path
[input_path_len
] = '\0';
4297 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4299 #ifdef DEBUG_COMPLETION
4300 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4301 input
, path
, file_prefix
);
4303 ffs
= opendir(path
);
4312 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4316 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4317 memcpy(file
, input
, input_path_len
);
4318 if (input_path_len
< sizeof(file
))
4319 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4321 /* stat the file to find out if it's a directory.
4322 * In that case add a slash to speed up typing long paths
4325 if(S_ISDIR(sb
.st_mode
))
4326 pstrcat(file
, sizeof(file
), "/");
4327 readline_add_completion(cur_mon
->rs
, file
);
4333 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4335 const char *name
= bdrv_get_device_name(bs
);
4336 const char *input
= opaque
;
4338 if (input
[0] == '\0' ||
4339 !strncmp(name
, (char *)input
, strlen(input
))) {
4340 readline_add_completion(cur_mon
->rs
, name
);
4344 /* NOTE: this parser is an approximate form of the real command parser */
4345 static void parse_cmdline(const char *cmdline
,
4346 int *pnb_args
, char **args
)
4355 while (qemu_isspace(*p
))
4359 if (nb_args
>= MAX_ARGS
)
4361 ret
= get_str(buf
, sizeof(buf
), &p
);
4362 args
[nb_args
] = qemu_strdup(buf
);
4367 *pnb_args
= nb_args
;
4370 static const char *next_arg_type(const char *typestr
)
4372 const char *p
= strchr(typestr
, ':');
4373 return (p
!= NULL
? ++p
: typestr
);
4376 static void monitor_find_completion(const char *cmdline
)
4378 const char *cmdname
;
4379 char *args
[MAX_ARGS
];
4380 int nb_args
, i
, len
;
4381 const char *ptype
, *str
;
4382 const mon_cmd_t
*cmd
;
4385 parse_cmdline(cmdline
, &nb_args
, args
);
4386 #ifdef DEBUG_COMPLETION
4387 for(i
= 0; i
< nb_args
; i
++) {
4388 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4392 /* if the line ends with a space, it means we want to complete the
4394 len
= strlen(cmdline
);
4395 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4396 if (nb_args
>= MAX_ARGS
) {
4399 args
[nb_args
++] = qemu_strdup("");
4402 /* command completion */
4407 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4408 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4409 cmd_completion(cmdname
, cmd
->name
);
4412 /* find the command */
4413 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4414 if (compare_cmd(args
[0], cmd
->name
)) {
4422 ptype
= next_arg_type(cmd
->args_type
);
4423 for(i
= 0; i
< nb_args
- 2; i
++) {
4424 if (*ptype
!= '\0') {
4425 ptype
= next_arg_type(ptype
);
4426 while (*ptype
== '?')
4427 ptype
= next_arg_type(ptype
);
4430 str
= args
[nb_args
- 1];
4431 if (*ptype
== '-' && ptype
[1] != '\0') {
4432 ptype
= next_arg_type(ptype
);
4436 /* file completion */
4437 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4438 file_completion(str
);
4441 /* block device name completion */
4442 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4443 bdrv_iterate(block_completion_it
, (void *)str
);
4446 /* XXX: more generic ? */
4447 if (!strcmp(cmd
->name
, "info")) {
4448 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4449 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4450 cmd_completion(str
, cmd
->name
);
4452 } else if (!strcmp(cmd
->name
, "sendkey")) {
4453 char *sep
= strrchr(str
, '-');
4456 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4457 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4458 cmd_completion(str
, key
->name
);
4460 } else if (!strcmp(cmd
->name
, "help|?")) {
4461 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4462 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4463 cmd_completion(str
, cmd
->name
);
4473 for (i
= 0; i
< nb_args
; i
++) {
4478 static int monitor_can_read(void *opaque
)
4480 Monitor
*mon
= opaque
;
4482 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4485 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4487 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4488 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4492 * Argument validation rules:
4494 * 1. The argument must exist in cmd_args qdict
4495 * 2. The argument type must be the expected one
4497 * Special case: If the argument doesn't exist in cmd_args and
4498 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4499 * checking is skipped for it.
4501 static int check_client_args_type(const QDict
*client_args
,
4502 const QDict
*cmd_args
, int flags
)
4504 const QDictEntry
*ent
;
4506 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4509 const QObject
*client_arg
= qdict_entry_value(ent
);
4510 const char *client_arg_name
= qdict_entry_key(ent
);
4512 obj
= qdict_get(cmd_args
, client_arg_name
);
4514 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4515 /* handler accepts unknowns */
4518 /* client arg doesn't exist */
4519 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4523 arg_type
= qobject_to_qstring(obj
);
4524 assert(arg_type
!= NULL
);
4526 /* check if argument's type is correct */
4527 switch (qstring_get_str(arg_type
)[0]) {
4531 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4532 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4541 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4542 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4548 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4549 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4550 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4557 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4558 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4564 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4569 * These types are not supported by QMP and thus are not
4570 * handled here. Fall through.
4581 * - Check if the client has passed all mandatory args
4582 * - Set special flags for argument validation
4584 static int check_mandatory_args(const QDict
*cmd_args
,
4585 const QDict
*client_args
, int *flags
)
4587 const QDictEntry
*ent
;
4589 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4590 const char *cmd_arg_name
= qdict_entry_key(ent
);
4591 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4592 assert(type
!= NULL
);
4594 if (qstring_get_str(type
)[0] == 'O') {
4595 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4596 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4597 } else if (qstring_get_str(type
)[0] != '-' &&
4598 qstring_get_str(type
)[1] != '?' &&
4599 !qdict_haskey(client_args
, cmd_arg_name
)) {
4600 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4608 static QDict
*qdict_from_args_type(const char *args_type
)
4612 QString
*key
, *type
, *cur_qs
;
4614 assert(args_type
!= NULL
);
4616 qdict
= qdict_new();
4618 if (args_type
== NULL
|| args_type
[0] == '\0') {
4619 /* no args, empty qdict */
4623 key
= qstring_new();
4624 type
= qstring_new();
4629 switch (args_type
[i
]) {
4632 qdict_put(qdict
, qstring_get_str(key
), type
);
4634 if (args_type
[i
] == '\0') {
4637 type
= qstring_new(); /* qdict has ref */
4638 cur_qs
= key
= qstring_new();
4644 qstring_append_chr(cur_qs
, args_type
[i
]);
4654 * Client argument checking rules:
4656 * 1. Client must provide all mandatory arguments
4657 * 2. Each argument provided by the client must be expected
4658 * 3. Each argument provided by the client must have the type expected
4661 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4666 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4669 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4674 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4682 * Input object checking rules
4684 * 1. Input object must be a dict
4685 * 2. The "execute" key must exist
4686 * 3. The "execute" key must be a string
4687 * 4. If the "arguments" key exists, it must be a dict
4688 * 5. If the "id" key exists, it can be anything (ie. json-value)
4689 * 6. Any argument not listed above is considered invalid
4691 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4693 const QDictEntry
*ent
;
4694 int has_exec_key
= 0;
4697 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4698 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4702 input_dict
= qobject_to_qdict(input_obj
);
4704 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4705 const char *arg_name
= qdict_entry_key(ent
);
4706 const QObject
*arg_obj
= qdict_entry_value(ent
);
4708 if (!strcmp(arg_name
, "execute")) {
4709 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4710 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4715 } else if (!strcmp(arg_name
, "arguments")) {
4716 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4717 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4721 } else if (!strcmp(arg_name
, "id")) {
4722 /* FIXME: check duplicated IDs for async commands */
4724 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4729 if (!has_exec_key
) {
4730 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4737 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
4739 QObject
*ret_data
= NULL
;
4741 if (handler_is_async(cmd
)) {
4742 qmp_async_info_handler(mon
, cmd
);
4743 if (monitor_has_error(mon
)) {
4744 monitor_protocol_emitter(mon
, NULL
);
4747 cmd
->mhandler
.info_new(mon
, &ret_data
);
4749 monitor_protocol_emitter(mon
, ret_data
);
4750 qobject_decref(ret_data
);
4755 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4756 const QDict
*params
)
4759 QObject
*data
= NULL
;
4761 mon_print_count_init(mon
);
4763 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4764 handler_audit(mon
, cmd
, ret
);
4765 monitor_protocol_emitter(mon
, data
);
4766 qobject_decref(data
);
4769 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4773 QDict
*input
, *args
;
4774 const mon_cmd_t
*cmd
;
4775 Monitor
*mon
= cur_mon
;
4776 const char *cmd_name
, *query_cmd
;
4779 args
= input
= NULL
;
4781 obj
= json_parser_parse(tokens
, NULL
);
4783 // FIXME: should be triggered in json_parser_parse()
4784 qerror_report(QERR_JSON_PARSING
);
4788 input
= qmp_check_input_obj(obj
);
4790 qobject_decref(obj
);
4794 mon
->mc
->id
= qdict_get(input
, "id");
4795 qobject_incref(mon
->mc
->id
);
4797 cmd_name
= qdict_get_str(input
, "execute");
4798 if (invalid_qmp_mode(mon
, cmd_name
)) {
4799 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4803 if (strstart(cmd_name
, "query-", &query_cmd
)) {
4804 cmd
= qmp_find_query_cmd(query_cmd
);
4806 cmd
= qmp_find_cmd(cmd_name
);
4810 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4814 obj
= qdict_get(input
, "arguments");
4818 args
= qobject_to_qdict(obj
);
4822 err
= qmp_check_client_args(cmd
, args
);
4828 qmp_call_query_cmd(mon
, cmd
);
4829 } else if (handler_is_async(cmd
)) {
4830 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4832 /* emit the error response */
4836 qmp_call_cmd(mon
, cmd
, args
);
4842 monitor_protocol_emitter(mon
, NULL
);
4849 * monitor_control_read(): Read and handle QMP input
4851 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4853 Monitor
*old_mon
= cur_mon
;
4857 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4862 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4864 Monitor
*old_mon
= cur_mon
;
4870 for (i
= 0; i
< size
; i
++)
4871 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4873 if (size
== 0 || buf
[size
- 1] != 0)
4874 monitor_printf(cur_mon
, "corrupted command\n");
4876 handle_user_command(cur_mon
, (char *)buf
);
4882 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4884 monitor_suspend(mon
);
4885 handle_user_command(mon
, cmdline
);
4886 monitor_resume(mon
);
4889 int monitor_suspend(Monitor
*mon
)
4897 void monitor_resume(Monitor
*mon
)
4901 if (--mon
->suspend_cnt
== 0)
4902 readline_show_prompt(mon
->rs
);
4905 static QObject
*get_qmp_greeting(void)
4909 do_info_version(NULL
, &ver
);
4910 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4914 * monitor_control_event(): Print QMP gretting
4916 static void monitor_control_event(void *opaque
, int event
)
4919 Monitor
*mon
= opaque
;
4922 case CHR_EVENT_OPENED
:
4923 mon
->mc
->command_mode
= 0;
4924 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4925 data
= get_qmp_greeting();
4926 monitor_json_emitter(mon
, data
);
4927 qobject_decref(data
);
4929 case CHR_EVENT_CLOSED
:
4930 json_message_parser_destroy(&mon
->mc
->parser
);
4935 static void monitor_event(void *opaque
, int event
)
4937 Monitor
*mon
= opaque
;
4940 case CHR_EVENT_MUX_IN
:
4942 if (mon
->reset_seen
) {
4943 readline_restart(mon
->rs
);
4944 monitor_resume(mon
);
4947 mon
->suspend_cnt
= 0;
4951 case CHR_EVENT_MUX_OUT
:
4952 if (mon
->reset_seen
) {
4953 if (mon
->suspend_cnt
== 0) {
4954 monitor_printf(mon
, "\n");
4957 monitor_suspend(mon
);
4964 case CHR_EVENT_OPENED
:
4965 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4966 "information\n", QEMU_VERSION
);
4967 if (!mon
->mux_out
) {
4968 readline_show_prompt(mon
->rs
);
4970 mon
->reset_seen
= 1;
4984 void monitor_init(CharDriverState
*chr
, int flags
)
4986 static int is_first_init
= 1;
4989 if (is_first_init
) {
4990 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
4994 mon
= qemu_mallocz(sizeof(*mon
));
4998 if (flags
& MONITOR_USE_READLINE
) {
4999 mon
->rs
= readline_init(mon
, monitor_find_completion
);
5000 monitor_read_command(mon
, 0);
5003 if (monitor_ctrl_mode(mon
)) {
5004 mon
->mc
= qemu_mallocz(sizeof(MonitorControl
));
5005 /* Control mode requires special handlers */
5006 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
5007 monitor_control_event
, mon
);
5009 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5010 monitor_event
, mon
);
5013 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5014 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5018 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
5020 BlockDriverState
*bs
= opaque
;
5023 if (bdrv_set_key(bs
, password
) != 0) {
5024 monitor_printf(mon
, "invalid password\n");
5027 if (mon
->password_completion_cb
)
5028 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5030 monitor_read_command(mon
, 1);
5033 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5034 BlockDriverCompletionFunc
*completion_cb
,
5039 if (!bdrv_key_required(bs
)) {
5041 completion_cb(opaque
, 0);
5045 if (monitor_ctrl_mode(mon
)) {
5046 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
5050 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5051 bdrv_get_encrypted_filename(bs
));
5053 mon
->password_completion_cb
= completion_cb
;
5054 mon
->password_opaque
= opaque
;
5056 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5058 if (err
&& completion_cb
)
5059 completion_cb(opaque
, err
);