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 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
355 qobject_to_json(data
);
356 assert(json
!= NULL
);
358 qstring_append_chr(json
, '\n');
359 monitor_puts(mon
, qstring_get_str(json
));
364 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
370 if (!monitor_has_error(mon
)) {
371 /* success response */
373 qobject_incref(data
);
374 qdict_put_obj(qmp
, "return", data
);
376 /* return an empty QDict by default */
377 qdict_put(qmp
, "return", qdict_new());
381 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
382 qdict_put(qmp
, "error", mon
->error
->error
);
383 QINCREF(mon
->error
->error
);
389 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
393 monitor_json_emitter(mon
, QOBJECT(qmp
));
397 static void timestamp_put(QDict
*qdict
)
403 err
= qemu_gettimeofday(&tv
);
407 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
408 "'microseconds': %" PRId64
" }",
409 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
410 qdict_put_obj(qdict
, "timestamp", obj
);
414 * monitor_protocol_event(): Generate a Monitor event
416 * Event-specific data can be emitted through the (optional) 'data' parameter.
418 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
421 const char *event_name
;
424 assert(event
< QEVENT_MAX
);
427 case QEVENT_SHUTDOWN
:
428 event_name
= "SHUTDOWN";
431 event_name
= "RESET";
433 case QEVENT_POWERDOWN
:
434 event_name
= "POWERDOWN";
440 event_name
= "RESUME";
442 case QEVENT_VNC_CONNECTED
:
443 event_name
= "VNC_CONNECTED";
445 case QEVENT_VNC_INITIALIZED
:
446 event_name
= "VNC_INITIALIZED";
448 case QEVENT_VNC_DISCONNECTED
:
449 event_name
= "VNC_DISCONNECTED";
451 case QEVENT_BLOCK_IO_ERROR
:
452 event_name
= "BLOCK_IO_ERROR";
454 case QEVENT_RTC_CHANGE
:
455 event_name
= "RTC_CHANGE";
457 case QEVENT_WATCHDOG
:
458 event_name
= "WATCHDOG";
467 qdict_put(qmp
, "event", qstring_from_str(event_name
));
469 qobject_incref(data
);
470 qdict_put_obj(qmp
, "data", data
);
473 QLIST_FOREACH(mon
, &mon_list
, entry
) {
474 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
475 monitor_json_emitter(mon
, QOBJECT(qmp
));
481 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
484 /* Will setup QMP capabilities in the future */
485 if (monitor_ctrl_mode(mon
)) {
486 mon
->mc
->command_mode
= 1;
492 static int mon_set_cpu(int cpu_index
);
493 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
495 static int do_hmp_passthrough(Monitor
*mon
, const QDict
*params
,
499 Monitor
*old_mon
, hmp
;
500 CharDriverState mchar
;
502 memset(&hmp
, 0, sizeof(hmp
));
503 qemu_chr_init_mem(&mchar
);
509 if (qdict_haskey(params
, "cpu-index")) {
510 ret
= mon_set_cpu(qdict_get_int(params
, "cpu-index"));
513 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "cpu-index", "a CPU number");
518 handle_user_command(&hmp
, qdict_get_str(params
, "command-line"));
521 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
522 *ret_data
= QOBJECT(qemu_chr_mem_to_qs(hmp
.chr
));
526 qemu_chr_close_mem(hmp
.chr
);
530 static int compare_cmd(const char *name
, const char *list
)
532 const char *p
, *pstart
;
540 p
= pstart
+ strlen(pstart
);
541 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
550 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
551 const char *prefix
, const char *name
)
553 const mon_cmd_t
*cmd
;
555 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
556 if (!name
|| !strcmp(name
, cmd
->name
))
557 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
558 cmd
->params
, cmd
->help
);
562 static void help_cmd(Monitor
*mon
, const char *name
)
564 if (name
&& !strcmp(name
, "info")) {
565 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
567 help_cmd_dump(mon
, mon_cmds
, "", name
);
568 if (name
&& !strcmp(name
, "log")) {
569 const CPULogItem
*item
;
570 monitor_printf(mon
, "Log items (comma separated):\n");
571 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
572 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
573 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
579 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
581 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
584 #ifdef CONFIG_SIMPLE_TRACE
585 static void do_change_trace_event_state(Monitor
*mon
, const QDict
*qdict
)
587 const char *tp_name
= qdict_get_str(qdict
, "name");
588 bool new_state
= qdict_get_bool(qdict
, "option");
589 int ret
= st_change_trace_event_state(tp_name
, new_state
);
592 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
596 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
598 const char *op
= qdict_get_try_str(qdict
, "op");
599 const char *arg
= qdict_get_try_str(qdict
, "arg");
602 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
603 } else if (!strcmp(op
, "on")) {
604 st_set_trace_file_enabled(true);
605 } else if (!strcmp(op
, "off")) {
606 st_set_trace_file_enabled(false);
607 } else if (!strcmp(op
, "flush")) {
608 st_flush_trace_buffer();
609 } else if (!strcmp(op
, "set")) {
611 st_set_trace_file(arg
);
614 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
615 help_cmd(mon
, "trace-file");
620 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
622 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
625 data
->user_print(data
->mon
, ret_data
);
627 monitor_resume(data
->mon
);
631 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
633 monitor_protocol_emitter(opaque
, ret_data
);
636 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
639 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
642 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
644 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
647 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
652 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
654 cb_data
->user_print
= cmd
->user_print
;
655 monitor_suspend(mon
);
656 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
657 user_monitor_complete
, cb_data
);
664 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
668 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
670 cb_data
->user_print
= cmd
->user_print
;
671 monitor_suspend(mon
);
672 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
679 static void do_info(Monitor
*mon
, const QDict
*qdict
)
681 const mon_cmd_t
*cmd
;
682 const char *item
= qdict_get_try_str(qdict
, "item");
688 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
689 if (compare_cmd(item
, cmd
->name
))
693 if (cmd
->name
== NULL
) {
697 if (handler_is_async(cmd
)) {
698 user_async_info_handler(mon
, cmd
);
699 } else if (handler_is_qobject(cmd
)) {
700 QObject
*info_data
= NULL
;
702 cmd
->mhandler
.info_new(mon
, &info_data
);
704 cmd
->user_print(mon
, info_data
);
705 qobject_decref(info_data
);
708 cmd
->mhandler
.info(mon
);
714 help_cmd(mon
, "info");
717 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
722 qdict
= qobject_to_qdict(data
);
723 qemu
= qdict_get_qdict(qdict
, "qemu");
725 monitor_printf(mon
, "%" PRId64
".%" PRId64
".%" PRId64
"%s\n",
726 qdict_get_int(qemu
, "major"),
727 qdict_get_int(qemu
, "minor"),
728 qdict_get_int(qemu
, "micro"),
729 qdict_get_str(qdict
, "package"));
732 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
734 const char *version
= QEMU_VERSION
;
735 int major
= 0, minor
= 0, micro
= 0;
738 major
= strtol(version
, &tmp
, 10);
740 minor
= strtol(tmp
, &tmp
, 10);
742 micro
= strtol(tmp
, &tmp
, 10);
744 *ret_data
= qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
745 'micro': %d }, 'package': %s }", major
, minor
, micro
, QEMU_PKGVERSION
);
748 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
752 qdict
= qobject_to_qdict(data
);
753 if (qdict_size(qdict
) == 0) {
757 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
760 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
762 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
763 qobject_from_jsonf("{}");
766 static QObject
*get_cmd_dict(const char *name
)
770 /* Remove '|' from some commands */
771 p
= strchr(name
, '|');
778 return qobject_from_jsonf("{ 'name': %s }", p
);
781 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
784 const mon_cmd_t
*cmd
;
786 cmd_list
= qlist_new();
788 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
789 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
792 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
794 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
795 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
798 *ret_data
= QOBJECT(cmd_list
);
801 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
803 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
806 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
810 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
811 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
812 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
813 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
814 qemu_uuid
[14], qemu_uuid
[15]);
815 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
818 /* get the current CPU defined by the user */
819 static int mon_set_cpu(int cpu_index
)
823 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
824 if (env
->cpu_index
== cpu_index
) {
825 cur_mon
->mon_cpu
= env
;
832 static CPUState
*mon_get_cpu(void)
834 if (!cur_mon
->mon_cpu
) {
837 cpu_synchronize_state(cur_mon
->mon_cpu
);
838 return cur_mon
->mon_cpu
;
841 static void do_info_registers(Monitor
*mon
)
846 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
849 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
854 static void print_cpu_iter(QObject
*obj
, void *opaque
)
858 Monitor
*mon
= opaque
;
860 assert(qobject_type(obj
) == QTYPE_QDICT
);
861 cpu
= qobject_to_qdict(obj
);
863 if (qdict_get_bool(cpu
, "current")) {
867 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
869 #if defined(TARGET_I386)
870 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
871 (target_ulong
) qdict_get_int(cpu
, "pc"));
872 #elif defined(TARGET_PPC)
873 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
874 (target_long
) qdict_get_int(cpu
, "nip"));
875 #elif defined(TARGET_SPARC)
876 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
877 (target_long
) qdict_get_int(cpu
, "pc"));
878 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
879 (target_long
) qdict_get_int(cpu
, "npc"));
880 #elif defined(TARGET_MIPS)
881 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
882 (target_long
) qdict_get_int(cpu
, "PC"));
885 if (qdict_get_bool(cpu
, "halted")) {
886 monitor_printf(mon
, " (halted)");
889 monitor_printf(mon
, "\n");
892 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
896 assert(qobject_type(data
) == QTYPE_QLIST
);
897 cpu_list
= qobject_to_qlist(data
);
898 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
901 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
906 cpu_list
= qlist_new();
908 /* just to set the default cpu if not already done */
911 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
915 cpu_synchronize_state(env
);
917 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
918 env
->cpu_index
, env
== mon
->mon_cpu
,
921 cpu
= qobject_to_qdict(obj
);
923 #if defined(TARGET_I386)
924 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
925 #elif defined(TARGET_PPC)
926 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
927 #elif defined(TARGET_SPARC)
928 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
929 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
930 #elif defined(TARGET_MIPS)
931 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
934 qlist_append(cpu_list
, cpu
);
937 *ret_data
= QOBJECT(cpu_list
);
940 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
942 int index
= qdict_get_int(qdict
, "index");
943 if (mon_set_cpu(index
) < 0) {
944 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "index",
951 static void do_info_jit(Monitor
*mon
)
953 dump_exec_info((FILE *)mon
, monitor_fprintf
);
956 static void do_info_history(Monitor
*mon
)
965 str
= readline_get_history(mon
->rs
, i
);
968 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
973 #if defined(TARGET_PPC)
974 /* XXX: not implemented in other targets */
975 static void do_info_cpu_stats(Monitor
*mon
)
980 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
984 #if defined(CONFIG_SIMPLE_TRACE)
985 static void do_info_trace(Monitor
*mon
)
987 st_print_trace((FILE *)mon
, &monitor_fprintf
);
990 static void do_info_trace_events(Monitor
*mon
)
992 st_print_trace_events((FILE *)mon
, &monitor_fprintf
);
997 * do_quit(): Quit QEMU execution
999 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1001 monitor_suspend(mon
);
1003 qemu_system_shutdown_request();
1008 static int change_vnc_password(const char *password
)
1010 if (vnc_display_password(NULL
, password
) < 0) {
1011 qerror_report(QERR_SET_PASSWD_FAILED
);
1018 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1021 change_vnc_password(password
);
1022 monitor_read_command(mon
, 1);
1025 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1027 if (strcmp(target
, "passwd") == 0 ||
1028 strcmp(target
, "password") == 0) {
1031 strncpy(password
, arg
, sizeof(password
));
1032 password
[sizeof(password
) - 1] = '\0';
1033 return change_vnc_password(password
);
1035 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1038 if (vnc_display_open(NULL
, target
) < 0) {
1039 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
1048 * do_change(): Change a removable medium, or VNC configuration
1050 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1052 const char *device
= qdict_get_str(qdict
, "device");
1053 const char *target
= qdict_get_str(qdict
, "target");
1054 const char *arg
= qdict_get_try_str(qdict
, "arg");
1057 if (strcmp(device
, "vnc") == 0) {
1058 ret
= do_change_vnc(mon
, target
, arg
);
1060 ret
= do_change_block(mon
, device
, target
, arg
);
1066 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1068 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1072 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1074 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1077 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1080 const char *items
= qdict_get_str(qdict
, "items");
1082 if (!strcmp(items
, "none")) {
1085 mask
= cpu_str_to_log_mask(items
);
1087 help_cmd(mon
, "log");
1094 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1096 const char *option
= qdict_get_try_str(qdict
, "option");
1097 if (!option
|| !strcmp(option
, "on")) {
1099 } else if (!strcmp(option
, "off")) {
1102 monitor_printf(mon
, "unexpected option %s\n", option
);
1107 * do_stop(): Stop VM execution
1109 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1111 vm_stop(EXCP_INTERRUPT
);
1115 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1117 struct bdrv_iterate_context
{
1123 * do_cont(): Resume emulation.
1125 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1127 struct bdrv_iterate_context context
= { mon
, 0 };
1129 if (incoming_expected
) {
1130 qerror_report(QERR_MIGRATION_EXPECTED
);
1133 bdrv_iterate(encrypted_bdrv_it
, &context
);
1134 /* only resume the vm if all keys are set and valid */
1143 static void bdrv_key_cb(void *opaque
, int err
)
1145 Monitor
*mon
= opaque
;
1147 /* another key was set successfully, retry to continue */
1149 do_cont(mon
, NULL
, NULL
);
1152 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1154 struct bdrv_iterate_context
*context
= opaque
;
1156 if (!context
->err
&& bdrv_key_required(bs
)) {
1157 context
->err
= -EBUSY
;
1158 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1163 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1165 const char *device
= qdict_get_try_str(qdict
, "device");
1167 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1168 if (gdbserver_start(device
) < 0) {
1169 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1171 } else if (strcmp(device
, "none") == 0) {
1172 monitor_printf(mon
, "Disabled gdbserver\n");
1174 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1179 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1181 const char *action
= qdict_get_str(qdict
, "action");
1182 if (select_watchdog_action(action
) == -1) {
1183 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1187 static void monitor_printc(Monitor
*mon
, int c
)
1189 monitor_printf(mon
, "'");
1192 monitor_printf(mon
, "\\'");
1195 monitor_printf(mon
, "\\\\");
1198 monitor_printf(mon
, "\\n");
1201 monitor_printf(mon
, "\\r");
1204 if (c
>= 32 && c
<= 126) {
1205 monitor_printf(mon
, "%c", c
);
1207 monitor_printf(mon
, "\\x%02x", c
);
1211 monitor_printf(mon
, "'");
1214 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1215 target_phys_addr_t addr
, int is_physical
)
1218 int l
, line_size
, i
, max_digits
, len
;
1222 if (format
== 'i') {
1225 env
= mon_get_cpu();
1229 } else if (wsize
== 4) {
1232 /* as default we use the current CS size */
1235 #ifdef TARGET_X86_64
1236 if ((env
->efer
& MSR_EFER_LMA
) &&
1237 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1241 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1246 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1250 len
= wsize
* count
;
1259 max_digits
= (wsize
* 8 + 2) / 3;
1263 max_digits
= (wsize
* 8) / 4;
1267 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1276 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1278 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1283 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1285 env
= mon_get_cpu();
1286 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1287 monitor_printf(mon
, " Cannot access memory\n");
1296 v
= ldub_raw(buf
+ i
);
1299 v
= lduw_raw(buf
+ i
);
1302 v
= (uint32_t)ldl_raw(buf
+ i
);
1305 v
= ldq_raw(buf
+ i
);
1308 monitor_printf(mon
, " ");
1311 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1314 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1317 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1320 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1323 monitor_printc(mon
, v
);
1328 monitor_printf(mon
, "\n");
1334 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1336 int count
= qdict_get_int(qdict
, "count");
1337 int format
= qdict_get_int(qdict
, "format");
1338 int size
= qdict_get_int(qdict
, "size");
1339 target_long addr
= qdict_get_int(qdict
, "addr");
1341 memory_dump(mon
, count
, format
, size
, addr
, 0);
1344 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1346 int count
= qdict_get_int(qdict
, "count");
1347 int format
= qdict_get_int(qdict
, "format");
1348 int size
= qdict_get_int(qdict
, "size");
1349 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1351 memory_dump(mon
, count
, format
, size
, addr
, 1);
1354 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1356 int format
= qdict_get_int(qdict
, "format");
1357 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1359 #if TARGET_PHYS_ADDR_BITS == 32
1362 monitor_printf(mon
, "%#o", val
);
1365 monitor_printf(mon
, "%#x", val
);
1368 monitor_printf(mon
, "%u", val
);
1372 monitor_printf(mon
, "%d", val
);
1375 monitor_printc(mon
, val
);
1381 monitor_printf(mon
, "%#" PRIo64
, val
);
1384 monitor_printf(mon
, "%#" PRIx64
, val
);
1387 monitor_printf(mon
, "%" PRIu64
, val
);
1391 monitor_printf(mon
, "%" PRId64
, val
);
1394 monitor_printc(mon
, val
);
1398 monitor_printf(mon
, "\n");
1401 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1404 uint32_t size
= qdict_get_int(qdict
, "size");
1405 const char *filename
= qdict_get_str(qdict
, "filename");
1406 target_long addr
= qdict_get_int(qdict
, "val");
1412 env
= mon_get_cpu();
1414 f
= fopen(filename
, "wb");
1416 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1423 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1424 if (fwrite(buf
, 1, l
, f
) != l
) {
1425 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1439 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1445 uint32_t size
= qdict_get_int(qdict
, "size");
1446 const char *filename
= qdict_get_str(qdict
, "filename");
1447 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1450 f
= fopen(filename
, "wb");
1452 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1459 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1460 if (fwrite(buf
, 1, l
, f
) != l
) {
1461 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1476 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1481 uint32_t start
= qdict_get_int(qdict
, "start");
1482 uint32_t size
= qdict_get_int(qdict
, "size");
1485 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1486 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1487 /* BSD sum algorithm ('sum' Unix command) */
1488 sum
= (sum
>> 1) | (sum
<< 15);
1491 monitor_printf(mon
, "%05d\n", sum
);
1499 static const KeyDef key_defs
[] = {
1501 { 0x36, "shift_r" },
1506 { 0xe4, "altgr_r" },
1526 { 0x0e, "backspace" },
1539 { 0x1a, "bracket_left" },
1540 { 0x1b, "bracket_right" },
1552 { 0x27, "semicolon" },
1553 { 0x28, "apostrophe" },
1554 { 0x29, "grave_accent" },
1556 { 0x2b, "backslash" },
1568 { 0x37, "asterisk" },
1571 { 0x3a, "caps_lock" },
1582 { 0x45, "num_lock" },
1583 { 0x46, "scroll_lock" },
1585 { 0xb5, "kp_divide" },
1586 { 0x37, "kp_multiply" },
1587 { 0x4a, "kp_subtract" },
1589 { 0x9c, "kp_enter" },
1590 { 0x53, "kp_decimal" },
1623 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1638 { 0xfe, "compose" },
1643 static int get_keycode(const char *key
)
1649 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1650 if (!strcmp(key
, p
->name
))
1653 if (strstart(key
, "0x", NULL
)) {
1654 ret
= strtoul(key
, &endp
, 0);
1655 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1661 #define MAX_KEYCODES 16
1662 static uint8_t keycodes
[MAX_KEYCODES
];
1663 static int nb_pending_keycodes
;
1664 static QEMUTimer
*key_timer
;
1666 static void release_keys(void *opaque
)
1670 while (nb_pending_keycodes
> 0) {
1671 nb_pending_keycodes
--;
1672 keycode
= keycodes
[nb_pending_keycodes
];
1674 kbd_put_keycode(0xe0);
1675 kbd_put_keycode(keycode
| 0x80);
1679 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1681 char keyname_buf
[16];
1683 int keyname_len
, keycode
, i
;
1684 const char *string
= qdict_get_str(qdict
, "string");
1685 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1686 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1688 if (nb_pending_keycodes
> 0) {
1689 qemu_del_timer(key_timer
);
1696 separator
= strchr(string
, '-');
1697 keyname_len
= separator
? separator
- string
: strlen(string
);
1698 if (keyname_len
> 0) {
1699 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1700 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1701 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1704 if (i
== MAX_KEYCODES
) {
1705 monitor_printf(mon
, "too many keys\n");
1708 keyname_buf
[keyname_len
] = 0;
1709 keycode
= get_keycode(keyname_buf
);
1711 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1714 keycodes
[i
++] = keycode
;
1718 string
= separator
+ 1;
1720 nb_pending_keycodes
= i
;
1721 /* key down events */
1722 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1723 keycode
= keycodes
[i
];
1725 kbd_put_keycode(0xe0);
1726 kbd_put_keycode(keycode
& 0x7f);
1728 /* delayed key up events */
1729 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1730 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1733 static int mouse_button_state
;
1735 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1738 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1739 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1740 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1741 dx
= strtol(dx_str
, NULL
, 0);
1742 dy
= strtol(dy_str
, NULL
, 0);
1745 dz
= strtol(dz_str
, NULL
, 0);
1746 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1749 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1751 int button_state
= qdict_get_int(qdict
, "button_state");
1752 mouse_button_state
= button_state
;
1753 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1756 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1758 int size
= qdict_get_int(qdict
, "size");
1759 int addr
= qdict_get_int(qdict
, "addr");
1760 int has_index
= qdict_haskey(qdict
, "index");
1765 int index
= qdict_get_int(qdict
, "index");
1766 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1774 val
= cpu_inb(addr
);
1778 val
= cpu_inw(addr
);
1782 val
= cpu_inl(addr
);
1786 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1787 suffix
, addr
, size
* 2, val
);
1790 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1792 int size
= qdict_get_int(qdict
, "size");
1793 int addr
= qdict_get_int(qdict
, "addr");
1794 int val
= qdict_get_int(qdict
, "val");
1796 addr
&= IOPORTS_MASK
;
1801 cpu_outb(addr
, val
);
1804 cpu_outw(addr
, val
);
1807 cpu_outl(addr
, val
);
1812 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1815 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1817 res
= qemu_boot_set(bootdevice
);
1819 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1820 } else if (res
> 0) {
1821 monitor_printf(mon
, "setting boot device list failed\n");
1823 monitor_printf(mon
, "no function defined to set boot device list for "
1824 "this architecture\n");
1829 * do_system_reset(): Issue a machine reset
1831 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1834 qemu_system_reset_request();
1839 * do_system_powerdown(): Issue a machine powerdown
1841 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1844 qemu_system_powerdown_request();
1848 #if defined(TARGET_I386)
1849 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1851 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1854 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1855 pte
& PG_PSE_MASK
? 'P' : '-',
1856 pte
& PG_DIRTY_MASK
? 'D' : '-',
1857 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1858 pte
& PG_PCD_MASK
? 'C' : '-',
1859 pte
& PG_PWT_MASK
? 'T' : '-',
1860 pte
& PG_USER_MASK
? 'U' : '-',
1861 pte
& PG_RW_MASK
? 'W' : '-');
1864 static void tlb_info(Monitor
*mon
)
1868 uint32_t pgd
, pde
, pte
;
1870 env
= mon_get_cpu();
1872 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1873 monitor_printf(mon
, "PG disabled\n");
1876 pgd
= env
->cr
[3] & ~0xfff;
1877 for(l1
= 0; l1
< 1024; l1
++) {
1878 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1879 pde
= le32_to_cpu(pde
);
1880 if (pde
& PG_PRESENT_MASK
) {
1881 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1882 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1884 for(l2
= 0; l2
< 1024; l2
++) {
1885 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1886 (uint8_t *)&pte
, 4);
1887 pte
= le32_to_cpu(pte
);
1888 if (pte
& PG_PRESENT_MASK
) {
1889 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1899 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1900 uint32_t end
, int prot
)
1903 prot1
= *plast_prot
;
1904 if (prot
!= prot1
) {
1905 if (*pstart
!= -1) {
1906 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1907 *pstart
, end
, end
- *pstart
,
1908 prot1
& PG_USER_MASK
? 'u' : '-',
1910 prot1
& PG_RW_MASK
? 'w' : '-');
1920 static void mem_info(Monitor
*mon
)
1923 int l1
, l2
, prot
, last_prot
;
1924 uint32_t pgd
, pde
, pte
, start
, end
;
1926 env
= mon_get_cpu();
1928 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1929 monitor_printf(mon
, "PG disabled\n");
1932 pgd
= env
->cr
[3] & ~0xfff;
1935 for(l1
= 0; l1
< 1024; l1
++) {
1936 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1937 pde
= le32_to_cpu(pde
);
1939 if (pde
& PG_PRESENT_MASK
) {
1940 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1941 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1942 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1944 for(l2
= 0; l2
< 1024; l2
++) {
1945 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1946 (uint8_t *)&pte
, 4);
1947 pte
= le32_to_cpu(pte
);
1948 end
= (l1
<< 22) + (l2
<< 12);
1949 if (pte
& PG_PRESENT_MASK
) {
1950 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1954 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1959 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1965 #if defined(TARGET_SH4)
1967 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1969 monitor_printf(mon
, " tlb%i:\t"
1970 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1971 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1972 "dirty=%hhu writethrough=%hhu\n",
1974 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1975 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1979 static void tlb_info(Monitor
*mon
)
1981 CPUState
*env
= mon_get_cpu();
1984 monitor_printf (mon
, "ITLB:\n");
1985 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1986 print_tlb (mon
, i
, &env
->itlb
[i
]);
1987 monitor_printf (mon
, "UTLB:\n");
1988 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1989 print_tlb (mon
, i
, &env
->utlb
[i
]);
1994 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
1998 qdict
= qobject_to_qdict(data
);
2000 monitor_printf(mon
, "kvm support: ");
2001 if (qdict_get_bool(qdict
, "present")) {
2002 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2003 "enabled" : "disabled");
2005 monitor_printf(mon
, "not compiled\n");
2009 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2012 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2015 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2019 static void do_info_numa(Monitor
*mon
)
2024 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2025 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2026 monitor_printf(mon
, "node %d cpus:", i
);
2027 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2028 if (env
->numa_node
== i
) {
2029 monitor_printf(mon
, " %d", env
->cpu_index
);
2032 monitor_printf(mon
, "\n");
2033 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2038 #ifdef CONFIG_PROFILER
2043 static void do_info_profile(Monitor
*mon
)
2049 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2050 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2051 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2052 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2057 static void do_info_profile(Monitor
*mon
)
2059 monitor_printf(mon
, "Internal profiler not compiled\n");
2063 /* Capture support */
2064 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2066 static void do_info_capture(Monitor
*mon
)
2071 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2072 monitor_printf(mon
, "[%d]: ", i
);
2073 s
->ops
.info (s
->opaque
);
2078 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2081 int n
= qdict_get_int(qdict
, "n");
2084 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2086 s
->ops
.destroy (s
->opaque
);
2087 QLIST_REMOVE (s
, entries
);
2094 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2096 const char *path
= qdict_get_str(qdict
, "path");
2097 int has_freq
= qdict_haskey(qdict
, "freq");
2098 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2099 int has_bits
= qdict_haskey(qdict
, "bits");
2100 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2101 int has_channels
= qdict_haskey(qdict
, "nchannels");
2102 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2105 s
= qemu_mallocz (sizeof (*s
));
2107 freq
= has_freq
? freq
: 44100;
2108 bits
= has_bits
? bits
: 16;
2109 nchannels
= has_channels
? nchannels
: 2;
2111 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2112 monitor_printf(mon
, "Faied to add wave capture\n");
2115 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2119 #if defined(TARGET_I386)
2120 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
2123 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2125 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2126 if (env
->cpu_index
== cpu_index
) {
2127 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2133 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2137 qdict
= qobject_to_qdict(data
);
2139 monitor_printf(mon
, "VM status: ");
2140 if (qdict_get_bool(qdict
, "running")) {
2141 monitor_printf(mon
, "running");
2142 if (qdict_get_bool(qdict
, "singlestep")) {
2143 monitor_printf(mon
, " (single step mode)");
2146 monitor_printf(mon
, "paused");
2149 monitor_printf(mon
, "\n");
2152 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2154 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2155 vm_running
, singlestep
);
2158 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2160 qemu_acl
*acl
= qemu_acl_find(name
);
2163 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2168 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2170 const char *aclname
= qdict_get_str(qdict
, "aclname");
2171 qemu_acl
*acl
= find_acl(mon
, aclname
);
2172 qemu_acl_entry
*entry
;
2176 monitor_printf(mon
, "policy: %s\n",
2177 acl
->defaultDeny
? "deny" : "allow");
2178 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2180 monitor_printf(mon
, "%d: %s %s\n", i
,
2181 entry
->deny
? "deny" : "allow", entry
->match
);
2186 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2188 const char *aclname
= qdict_get_str(qdict
, "aclname");
2189 qemu_acl
*acl
= find_acl(mon
, aclname
);
2192 qemu_acl_reset(acl
);
2193 monitor_printf(mon
, "acl: removed all rules\n");
2197 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2199 const char *aclname
= qdict_get_str(qdict
, "aclname");
2200 const char *policy
= qdict_get_str(qdict
, "policy");
2201 qemu_acl
*acl
= find_acl(mon
, aclname
);
2204 if (strcmp(policy
, "allow") == 0) {
2205 acl
->defaultDeny
= 0;
2206 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2207 } else if (strcmp(policy
, "deny") == 0) {
2208 acl
->defaultDeny
= 1;
2209 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2211 monitor_printf(mon
, "acl: unknown policy '%s', "
2212 "expected 'deny' or 'allow'\n", policy
);
2217 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2219 const char *aclname
= qdict_get_str(qdict
, "aclname");
2220 const char *match
= qdict_get_str(qdict
, "match");
2221 const char *policy
= qdict_get_str(qdict
, "policy");
2222 int has_index
= qdict_haskey(qdict
, "index");
2223 int index
= qdict_get_try_int(qdict
, "index", -1);
2224 qemu_acl
*acl
= find_acl(mon
, aclname
);
2228 if (strcmp(policy
, "allow") == 0) {
2230 } else if (strcmp(policy
, "deny") == 0) {
2233 monitor_printf(mon
, "acl: unknown policy '%s', "
2234 "expected 'deny' or 'allow'\n", policy
);
2238 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2240 ret
= qemu_acl_append(acl
, deny
, match
);
2242 monitor_printf(mon
, "acl: unable to add acl entry\n");
2244 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2248 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2250 const char *aclname
= qdict_get_str(qdict
, "aclname");
2251 const char *match
= qdict_get_str(qdict
, "match");
2252 qemu_acl
*acl
= find_acl(mon
, aclname
);
2256 ret
= qemu_acl_remove(acl
, match
);
2258 monitor_printf(mon
, "acl: no matching acl entry\n");
2260 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2264 #if defined(TARGET_I386)
2265 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2268 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2269 int bank
= qdict_get_int(qdict
, "bank");
2270 uint64_t status
= qdict_get_int(qdict
, "status");
2271 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2272 uint64_t addr
= qdict_get_int(qdict
, "addr");
2273 uint64_t misc
= qdict_get_int(qdict
, "misc");
2275 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
2276 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
2277 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
2283 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2285 const char *fdname
= qdict_get_str(qdict
, "fdname");
2289 fd
= qemu_chr_get_msgfd(mon
->chr
);
2291 qerror_report(QERR_FD_NOT_SUPPLIED
);
2295 if (qemu_isdigit(fdname
[0])) {
2296 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2297 "a name not starting with a digit");
2301 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2302 if (strcmp(monfd
->name
, fdname
) != 0) {
2311 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
2312 monfd
->name
= qemu_strdup(fdname
);
2315 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2319 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2321 const char *fdname
= qdict_get_str(qdict
, "fdname");
2324 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2325 if (strcmp(monfd
->name
, fdname
) != 0) {
2329 QLIST_REMOVE(monfd
, next
);
2331 qemu_free(monfd
->name
);
2336 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2340 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2342 int saved_vm_running
= vm_running
;
2343 const char *name
= qdict_get_str(qdict
, "name");
2347 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2352 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2356 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2359 if (strcmp(monfd
->name
, fdname
) != 0) {
2365 /* caller takes ownership of fd */
2366 QLIST_REMOVE(monfd
, next
);
2367 qemu_free(monfd
->name
);
2376 static const mon_cmd_t mon_cmds
[] = {
2377 #include "hmp-commands.h"
2381 /* Please update hmp-commands.hx when adding or changing commands */
2382 static const mon_cmd_t info_cmds
[] = {
2387 .help
= "show the version of QEMU",
2388 .user_print
= do_info_version_print
,
2389 .mhandler
.info_new
= do_info_version
,
2395 .help
= "show the network state",
2396 .mhandler
.info
= do_info_network
,
2402 .help
= "show the character devices",
2403 .user_print
= qemu_chr_info_print
,
2404 .mhandler
.info_new
= qemu_chr_info
,
2410 .help
= "show the block devices",
2411 .user_print
= bdrv_info_print
,
2412 .mhandler
.info_new
= bdrv_info
,
2415 .name
= "blockstats",
2418 .help
= "show block device statistics",
2419 .user_print
= bdrv_stats_print
,
2420 .mhandler
.info_new
= bdrv_info_stats
,
2423 .name
= "registers",
2426 .help
= "show the cpu registers",
2427 .mhandler
.info
= do_info_registers
,
2433 .help
= "show infos for each CPU",
2434 .user_print
= monitor_print_cpus
,
2435 .mhandler
.info_new
= do_info_cpus
,
2441 .help
= "show the command line history",
2442 .mhandler
.info
= do_info_history
,
2448 .help
= "show the interrupts statistics (if available)",
2449 .mhandler
.info
= irq_info
,
2455 .help
= "show i8259 (PIC) state",
2456 .mhandler
.info
= pic_info
,
2462 .help
= "show PCI info",
2463 .user_print
= do_pci_info_print
,
2464 .mhandler
.info_new
= do_pci_info
,
2466 #if defined(TARGET_I386) || defined(TARGET_SH4)
2471 .help
= "show virtual to physical memory mappings",
2472 .mhandler
.info
= tlb_info
,
2475 #if defined(TARGET_I386)
2480 .help
= "show the active virtual memory mappings",
2481 .mhandler
.info
= mem_info
,
2488 .help
= "show dynamic compiler info",
2489 .mhandler
.info
= do_info_jit
,
2495 .help
= "show KVM information",
2496 .user_print
= do_info_kvm_print
,
2497 .mhandler
.info_new
= do_info_kvm
,
2503 .help
= "show NUMA information",
2504 .mhandler
.info
= do_info_numa
,
2510 .help
= "show guest USB devices",
2511 .mhandler
.info
= usb_info
,
2517 .help
= "show host USB devices",
2518 .mhandler
.info
= usb_host_info
,
2524 .help
= "show profiling information",
2525 .mhandler
.info
= do_info_profile
,
2531 .help
= "show capture information",
2532 .mhandler
.info
= do_info_capture
,
2535 .name
= "snapshots",
2538 .help
= "show the currently saved VM snapshots",
2539 .mhandler
.info
= do_info_snapshots
,
2545 .help
= "show the current VM status (running|paused)",
2546 .user_print
= do_info_status_print
,
2547 .mhandler
.info_new
= do_info_status
,
2553 .help
= "show guest PCMCIA status",
2554 .mhandler
.info
= pcmcia_info
,
2560 .help
= "show which guest mouse is receiving events",
2561 .user_print
= do_info_mice_print
,
2562 .mhandler
.info_new
= do_info_mice
,
2568 .help
= "show the vnc server status",
2569 .user_print
= do_info_vnc_print
,
2570 .mhandler
.info_new
= do_info_vnc
,
2576 .help
= "show the current VM name",
2577 .user_print
= do_info_name_print
,
2578 .mhandler
.info_new
= do_info_name
,
2584 .help
= "show the current VM UUID",
2585 .user_print
= do_info_uuid_print
,
2586 .mhandler
.info_new
= do_info_uuid
,
2588 #if defined(TARGET_PPC)
2593 .help
= "show CPU statistics",
2594 .mhandler
.info
= do_info_cpu_stats
,
2597 #if defined(CONFIG_SLIRP)
2602 .help
= "show user network stack connection states",
2603 .mhandler
.info
= do_info_usernet
,
2610 .help
= "show migration status",
2611 .user_print
= do_info_migrate_print
,
2612 .mhandler
.info_new
= do_info_migrate
,
2618 .help
= "show balloon information",
2619 .user_print
= monitor_print_balloon
,
2620 .mhandler
.info_async
= do_info_balloon
,
2621 .flags
= MONITOR_CMD_ASYNC
,
2627 .help
= "show device tree",
2628 .mhandler
.info
= do_info_qtree
,
2634 .help
= "show qdev device model list",
2635 .mhandler
.info
= do_info_qdm
,
2641 .help
= "show roms",
2642 .mhandler
.info
= do_info_roms
,
2644 #if defined(CONFIG_SIMPLE_TRACE)
2649 .help
= "show current contents of trace buffer",
2650 .mhandler
.info
= do_info_trace
,
2653 .name
= "trace-events",
2656 .help
= "show available trace-events & their state",
2657 .mhandler
.info
= do_info_trace_events
,
2665 static const mon_cmd_t qmp_cmds
[] = {
2666 #include "qmp-commands.h"
2670 static const mon_cmd_t qmp_query_cmds
[] = {
2675 .help
= "show the version of QEMU",
2676 .user_print
= do_info_version_print
,
2677 .mhandler
.info_new
= do_info_version
,
2683 .help
= "list QMP available commands",
2684 .user_print
= monitor_user_noop
,
2685 .mhandler
.info_new
= do_info_commands
,
2691 .help
= "show the character devices",
2692 .user_print
= qemu_chr_info_print
,
2693 .mhandler
.info_new
= qemu_chr_info
,
2699 .help
= "show the block devices",
2700 .user_print
= bdrv_info_print
,
2701 .mhandler
.info_new
= bdrv_info
,
2704 .name
= "blockstats",
2707 .help
= "show block device statistics",
2708 .user_print
= bdrv_stats_print
,
2709 .mhandler
.info_new
= bdrv_info_stats
,
2715 .help
= "show infos for each CPU",
2716 .user_print
= monitor_print_cpus
,
2717 .mhandler
.info_new
= do_info_cpus
,
2723 .help
= "show PCI info",
2724 .user_print
= do_pci_info_print
,
2725 .mhandler
.info_new
= do_pci_info
,
2731 .help
= "show KVM information",
2732 .user_print
= do_info_kvm_print
,
2733 .mhandler
.info_new
= do_info_kvm
,
2739 .help
= "show the current VM status (running|paused)",
2740 .user_print
= do_info_status_print
,
2741 .mhandler
.info_new
= do_info_status
,
2747 .help
= "show which guest mouse is receiving events",
2748 .user_print
= do_info_mice_print
,
2749 .mhandler
.info_new
= do_info_mice
,
2755 .help
= "show the vnc server status",
2756 .user_print
= do_info_vnc_print
,
2757 .mhandler
.info_new
= do_info_vnc
,
2763 .help
= "show the current VM name",
2764 .user_print
= do_info_name_print
,
2765 .mhandler
.info_new
= do_info_name
,
2771 .help
= "show the current VM UUID",
2772 .user_print
= do_info_uuid_print
,
2773 .mhandler
.info_new
= do_info_uuid
,
2779 .help
= "show migration status",
2780 .user_print
= do_info_migrate_print
,
2781 .mhandler
.info_new
= do_info_migrate
,
2787 .help
= "show balloon information",
2788 .user_print
= monitor_print_balloon
,
2789 .mhandler
.info_async
= do_info_balloon
,
2790 .flags
= MONITOR_CMD_ASYNC
,
2795 /*******************************************************************/
2797 static const char *pch
;
2798 static jmp_buf expr_env
;
2803 typedef struct MonitorDef
{
2806 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2810 #if defined(TARGET_I386)
2811 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2813 CPUState
*env
= mon_get_cpu();
2814 return env
->eip
+ env
->segs
[R_CS
].base
;
2818 #if defined(TARGET_PPC)
2819 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2821 CPUState
*env
= mon_get_cpu();
2826 for (i
= 0; i
< 8; i
++)
2827 u
|= env
->crf
[i
] << (32 - (4 * i
));
2832 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2834 CPUState
*env
= mon_get_cpu();
2838 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2840 CPUState
*env
= mon_get_cpu();
2844 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2846 CPUState
*env
= mon_get_cpu();
2847 return cpu_ppc_load_decr(env
);
2850 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2852 CPUState
*env
= mon_get_cpu();
2853 return cpu_ppc_load_tbu(env
);
2856 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2858 CPUState
*env
= mon_get_cpu();
2859 return cpu_ppc_load_tbl(env
);
2863 #if defined(TARGET_SPARC)
2864 #ifndef TARGET_SPARC64
2865 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2867 CPUState
*env
= mon_get_cpu();
2869 return cpu_get_psr(env
);
2873 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2875 CPUState
*env
= mon_get_cpu();
2876 return env
->regwptr
[val
];
2880 static const MonitorDef monitor_defs
[] = {
2883 #define SEG(name, seg) \
2884 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2885 { name ".base", offsetof(CPUState, segs[seg].base) },\
2886 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2888 { "eax", offsetof(CPUState
, regs
[0]) },
2889 { "ecx", offsetof(CPUState
, regs
[1]) },
2890 { "edx", offsetof(CPUState
, regs
[2]) },
2891 { "ebx", offsetof(CPUState
, regs
[3]) },
2892 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2893 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2894 { "esi", offsetof(CPUState
, regs
[6]) },
2895 { "edi", offsetof(CPUState
, regs
[7]) },
2896 #ifdef TARGET_X86_64
2897 { "r8", offsetof(CPUState
, regs
[8]) },
2898 { "r9", offsetof(CPUState
, regs
[9]) },
2899 { "r10", offsetof(CPUState
, regs
[10]) },
2900 { "r11", offsetof(CPUState
, regs
[11]) },
2901 { "r12", offsetof(CPUState
, regs
[12]) },
2902 { "r13", offsetof(CPUState
, regs
[13]) },
2903 { "r14", offsetof(CPUState
, regs
[14]) },
2904 { "r15", offsetof(CPUState
, regs
[15]) },
2906 { "eflags", offsetof(CPUState
, eflags
) },
2907 { "eip", offsetof(CPUState
, eip
) },
2914 { "pc", 0, monitor_get_pc
, },
2915 #elif defined(TARGET_PPC)
2916 /* General purpose registers */
2917 { "r0", offsetof(CPUState
, gpr
[0]) },
2918 { "r1", offsetof(CPUState
, gpr
[1]) },
2919 { "r2", offsetof(CPUState
, gpr
[2]) },
2920 { "r3", offsetof(CPUState
, gpr
[3]) },
2921 { "r4", offsetof(CPUState
, gpr
[4]) },
2922 { "r5", offsetof(CPUState
, gpr
[5]) },
2923 { "r6", offsetof(CPUState
, gpr
[6]) },
2924 { "r7", offsetof(CPUState
, gpr
[7]) },
2925 { "r8", offsetof(CPUState
, gpr
[8]) },
2926 { "r9", offsetof(CPUState
, gpr
[9]) },
2927 { "r10", offsetof(CPUState
, gpr
[10]) },
2928 { "r11", offsetof(CPUState
, gpr
[11]) },
2929 { "r12", offsetof(CPUState
, gpr
[12]) },
2930 { "r13", offsetof(CPUState
, gpr
[13]) },
2931 { "r14", offsetof(CPUState
, gpr
[14]) },
2932 { "r15", offsetof(CPUState
, gpr
[15]) },
2933 { "r16", offsetof(CPUState
, gpr
[16]) },
2934 { "r17", offsetof(CPUState
, gpr
[17]) },
2935 { "r18", offsetof(CPUState
, gpr
[18]) },
2936 { "r19", offsetof(CPUState
, gpr
[19]) },
2937 { "r20", offsetof(CPUState
, gpr
[20]) },
2938 { "r21", offsetof(CPUState
, gpr
[21]) },
2939 { "r22", offsetof(CPUState
, gpr
[22]) },
2940 { "r23", offsetof(CPUState
, gpr
[23]) },
2941 { "r24", offsetof(CPUState
, gpr
[24]) },
2942 { "r25", offsetof(CPUState
, gpr
[25]) },
2943 { "r26", offsetof(CPUState
, gpr
[26]) },
2944 { "r27", offsetof(CPUState
, gpr
[27]) },
2945 { "r28", offsetof(CPUState
, gpr
[28]) },
2946 { "r29", offsetof(CPUState
, gpr
[29]) },
2947 { "r30", offsetof(CPUState
, gpr
[30]) },
2948 { "r31", offsetof(CPUState
, gpr
[31]) },
2949 /* Floating point registers */
2950 { "f0", offsetof(CPUState
, fpr
[0]) },
2951 { "f1", offsetof(CPUState
, fpr
[1]) },
2952 { "f2", offsetof(CPUState
, fpr
[2]) },
2953 { "f3", offsetof(CPUState
, fpr
[3]) },
2954 { "f4", offsetof(CPUState
, fpr
[4]) },
2955 { "f5", offsetof(CPUState
, fpr
[5]) },
2956 { "f6", offsetof(CPUState
, fpr
[6]) },
2957 { "f7", offsetof(CPUState
, fpr
[7]) },
2958 { "f8", offsetof(CPUState
, fpr
[8]) },
2959 { "f9", offsetof(CPUState
, fpr
[9]) },
2960 { "f10", offsetof(CPUState
, fpr
[10]) },
2961 { "f11", offsetof(CPUState
, fpr
[11]) },
2962 { "f12", offsetof(CPUState
, fpr
[12]) },
2963 { "f13", offsetof(CPUState
, fpr
[13]) },
2964 { "f14", offsetof(CPUState
, fpr
[14]) },
2965 { "f15", offsetof(CPUState
, fpr
[15]) },
2966 { "f16", offsetof(CPUState
, fpr
[16]) },
2967 { "f17", offsetof(CPUState
, fpr
[17]) },
2968 { "f18", offsetof(CPUState
, fpr
[18]) },
2969 { "f19", offsetof(CPUState
, fpr
[19]) },
2970 { "f20", offsetof(CPUState
, fpr
[20]) },
2971 { "f21", offsetof(CPUState
, fpr
[21]) },
2972 { "f22", offsetof(CPUState
, fpr
[22]) },
2973 { "f23", offsetof(CPUState
, fpr
[23]) },
2974 { "f24", offsetof(CPUState
, fpr
[24]) },
2975 { "f25", offsetof(CPUState
, fpr
[25]) },
2976 { "f26", offsetof(CPUState
, fpr
[26]) },
2977 { "f27", offsetof(CPUState
, fpr
[27]) },
2978 { "f28", offsetof(CPUState
, fpr
[28]) },
2979 { "f29", offsetof(CPUState
, fpr
[29]) },
2980 { "f30", offsetof(CPUState
, fpr
[30]) },
2981 { "f31", offsetof(CPUState
, fpr
[31]) },
2982 { "fpscr", offsetof(CPUState
, fpscr
) },
2983 /* Next instruction pointer */
2984 { "nip|pc", offsetof(CPUState
, nip
) },
2985 { "lr", offsetof(CPUState
, lr
) },
2986 { "ctr", offsetof(CPUState
, ctr
) },
2987 { "decr", 0, &monitor_get_decr
, },
2988 { "ccr", 0, &monitor_get_ccr
, },
2989 /* Machine state register */
2990 { "msr", 0, &monitor_get_msr
, },
2991 { "xer", 0, &monitor_get_xer
, },
2992 { "tbu", 0, &monitor_get_tbu
, },
2993 { "tbl", 0, &monitor_get_tbl
, },
2994 #if defined(TARGET_PPC64)
2995 /* Address space register */
2996 { "asr", offsetof(CPUState
, asr
) },
2998 /* Segment registers */
2999 { "sdr1", offsetof(CPUState
, sdr1
) },
3000 { "sr0", offsetof(CPUState
, sr
[0]) },
3001 { "sr1", offsetof(CPUState
, sr
[1]) },
3002 { "sr2", offsetof(CPUState
, sr
[2]) },
3003 { "sr3", offsetof(CPUState
, sr
[3]) },
3004 { "sr4", offsetof(CPUState
, sr
[4]) },
3005 { "sr5", offsetof(CPUState
, sr
[5]) },
3006 { "sr6", offsetof(CPUState
, sr
[6]) },
3007 { "sr7", offsetof(CPUState
, sr
[7]) },
3008 { "sr8", offsetof(CPUState
, sr
[8]) },
3009 { "sr9", offsetof(CPUState
, sr
[9]) },
3010 { "sr10", offsetof(CPUState
, sr
[10]) },
3011 { "sr11", offsetof(CPUState
, sr
[11]) },
3012 { "sr12", offsetof(CPUState
, sr
[12]) },
3013 { "sr13", offsetof(CPUState
, sr
[13]) },
3014 { "sr14", offsetof(CPUState
, sr
[14]) },
3015 { "sr15", offsetof(CPUState
, sr
[15]) },
3016 /* Too lazy to put BATs and SPRs ... */
3017 #elif defined(TARGET_SPARC)
3018 { "g0", offsetof(CPUState
, gregs
[0]) },
3019 { "g1", offsetof(CPUState
, gregs
[1]) },
3020 { "g2", offsetof(CPUState
, gregs
[2]) },
3021 { "g3", offsetof(CPUState
, gregs
[3]) },
3022 { "g4", offsetof(CPUState
, gregs
[4]) },
3023 { "g5", offsetof(CPUState
, gregs
[5]) },
3024 { "g6", offsetof(CPUState
, gregs
[6]) },
3025 { "g7", offsetof(CPUState
, gregs
[7]) },
3026 { "o0", 0, monitor_get_reg
},
3027 { "o1", 1, monitor_get_reg
},
3028 { "o2", 2, monitor_get_reg
},
3029 { "o3", 3, monitor_get_reg
},
3030 { "o4", 4, monitor_get_reg
},
3031 { "o5", 5, monitor_get_reg
},
3032 { "o6", 6, monitor_get_reg
},
3033 { "o7", 7, monitor_get_reg
},
3034 { "l0", 8, monitor_get_reg
},
3035 { "l1", 9, monitor_get_reg
},
3036 { "l2", 10, monitor_get_reg
},
3037 { "l3", 11, monitor_get_reg
},
3038 { "l4", 12, monitor_get_reg
},
3039 { "l5", 13, monitor_get_reg
},
3040 { "l6", 14, monitor_get_reg
},
3041 { "l7", 15, monitor_get_reg
},
3042 { "i0", 16, monitor_get_reg
},
3043 { "i1", 17, monitor_get_reg
},
3044 { "i2", 18, monitor_get_reg
},
3045 { "i3", 19, monitor_get_reg
},
3046 { "i4", 20, monitor_get_reg
},
3047 { "i5", 21, monitor_get_reg
},
3048 { "i6", 22, monitor_get_reg
},
3049 { "i7", 23, monitor_get_reg
},
3050 { "pc", offsetof(CPUState
, pc
) },
3051 { "npc", offsetof(CPUState
, npc
) },
3052 { "y", offsetof(CPUState
, y
) },
3053 #ifndef TARGET_SPARC64
3054 { "psr", 0, &monitor_get_psr
, },
3055 { "wim", offsetof(CPUState
, wim
) },
3057 { "tbr", offsetof(CPUState
, tbr
) },
3058 { "fsr", offsetof(CPUState
, fsr
) },
3059 { "f0", offsetof(CPUState
, fpr
[0]) },
3060 { "f1", offsetof(CPUState
, fpr
[1]) },
3061 { "f2", offsetof(CPUState
, fpr
[2]) },
3062 { "f3", offsetof(CPUState
, fpr
[3]) },
3063 { "f4", offsetof(CPUState
, fpr
[4]) },
3064 { "f5", offsetof(CPUState
, fpr
[5]) },
3065 { "f6", offsetof(CPUState
, fpr
[6]) },
3066 { "f7", offsetof(CPUState
, fpr
[7]) },
3067 { "f8", offsetof(CPUState
, fpr
[8]) },
3068 { "f9", offsetof(CPUState
, fpr
[9]) },
3069 { "f10", offsetof(CPUState
, fpr
[10]) },
3070 { "f11", offsetof(CPUState
, fpr
[11]) },
3071 { "f12", offsetof(CPUState
, fpr
[12]) },
3072 { "f13", offsetof(CPUState
, fpr
[13]) },
3073 { "f14", offsetof(CPUState
, fpr
[14]) },
3074 { "f15", offsetof(CPUState
, fpr
[15]) },
3075 { "f16", offsetof(CPUState
, fpr
[16]) },
3076 { "f17", offsetof(CPUState
, fpr
[17]) },
3077 { "f18", offsetof(CPUState
, fpr
[18]) },
3078 { "f19", offsetof(CPUState
, fpr
[19]) },
3079 { "f20", offsetof(CPUState
, fpr
[20]) },
3080 { "f21", offsetof(CPUState
, fpr
[21]) },
3081 { "f22", offsetof(CPUState
, fpr
[22]) },
3082 { "f23", offsetof(CPUState
, fpr
[23]) },
3083 { "f24", offsetof(CPUState
, fpr
[24]) },
3084 { "f25", offsetof(CPUState
, fpr
[25]) },
3085 { "f26", offsetof(CPUState
, fpr
[26]) },
3086 { "f27", offsetof(CPUState
, fpr
[27]) },
3087 { "f28", offsetof(CPUState
, fpr
[28]) },
3088 { "f29", offsetof(CPUState
, fpr
[29]) },
3089 { "f30", offsetof(CPUState
, fpr
[30]) },
3090 { "f31", offsetof(CPUState
, fpr
[31]) },
3091 #ifdef TARGET_SPARC64
3092 { "f32", offsetof(CPUState
, fpr
[32]) },
3093 { "f34", offsetof(CPUState
, fpr
[34]) },
3094 { "f36", offsetof(CPUState
, fpr
[36]) },
3095 { "f38", offsetof(CPUState
, fpr
[38]) },
3096 { "f40", offsetof(CPUState
, fpr
[40]) },
3097 { "f42", offsetof(CPUState
, fpr
[42]) },
3098 { "f44", offsetof(CPUState
, fpr
[44]) },
3099 { "f46", offsetof(CPUState
, fpr
[46]) },
3100 { "f48", offsetof(CPUState
, fpr
[48]) },
3101 { "f50", offsetof(CPUState
, fpr
[50]) },
3102 { "f52", offsetof(CPUState
, fpr
[52]) },
3103 { "f54", offsetof(CPUState
, fpr
[54]) },
3104 { "f56", offsetof(CPUState
, fpr
[56]) },
3105 { "f58", offsetof(CPUState
, fpr
[58]) },
3106 { "f60", offsetof(CPUState
, fpr
[60]) },
3107 { "f62", offsetof(CPUState
, fpr
[62]) },
3108 { "asi", offsetof(CPUState
, asi
) },
3109 { "pstate", offsetof(CPUState
, pstate
) },
3110 { "cansave", offsetof(CPUState
, cansave
) },
3111 { "canrestore", offsetof(CPUState
, canrestore
) },
3112 { "otherwin", offsetof(CPUState
, otherwin
) },
3113 { "wstate", offsetof(CPUState
, wstate
) },
3114 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3115 { "fprs", offsetof(CPUState
, fprs
) },
3121 static void expr_error(Monitor
*mon
, const char *msg
)
3123 monitor_printf(mon
, "%s\n", msg
);
3124 longjmp(expr_env
, 1);
3127 /* return 0 if OK, -1 if not found */
3128 static int get_monitor_def(target_long
*pval
, const char *name
)
3130 const MonitorDef
*md
;
3133 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3134 if (compare_cmd(name
, md
->name
)) {
3135 if (md
->get_value
) {
3136 *pval
= md
->get_value(md
, md
->offset
);
3138 CPUState
*env
= mon_get_cpu();
3139 ptr
= (uint8_t *)env
+ md
->offset
;
3142 *pval
= *(int32_t *)ptr
;
3145 *pval
= *(target_long
*)ptr
;
3158 static void next(void)
3162 while (qemu_isspace(*pch
))
3167 static int64_t expr_sum(Monitor
*mon
);
3169 static int64_t expr_unary(Monitor
*mon
)
3178 n
= expr_unary(mon
);
3182 n
= -expr_unary(mon
);
3186 n
= ~expr_unary(mon
);
3192 expr_error(mon
, "')' expected");
3199 expr_error(mon
, "character constant expected");
3203 expr_error(mon
, "missing terminating \' character");
3213 while ((*pch
>= 'a' && *pch
<= 'z') ||
3214 (*pch
>= 'A' && *pch
<= 'Z') ||
3215 (*pch
>= '0' && *pch
<= '9') ||
3216 *pch
== '_' || *pch
== '.') {
3217 if ((q
- buf
) < sizeof(buf
) - 1)
3221 while (qemu_isspace(*pch
))
3224 ret
= get_monitor_def(®
, buf
);
3226 expr_error(mon
, "unknown register");
3231 expr_error(mon
, "unexpected end of expression");
3235 #if TARGET_PHYS_ADDR_BITS > 32
3236 n
= strtoull(pch
, &p
, 0);
3238 n
= strtoul(pch
, &p
, 0);
3241 expr_error(mon
, "invalid char in expression");
3244 while (qemu_isspace(*pch
))
3252 static int64_t expr_prod(Monitor
*mon
)
3257 val
= expr_unary(mon
);
3260 if (op
!= '*' && op
!= '/' && op
!= '%')
3263 val2
= expr_unary(mon
);
3272 expr_error(mon
, "division by zero");
3283 static int64_t expr_logic(Monitor
*mon
)
3288 val
= expr_prod(mon
);
3291 if (op
!= '&' && op
!= '|' && op
!= '^')
3294 val2
= expr_prod(mon
);
3311 static int64_t expr_sum(Monitor
*mon
)
3316 val
= expr_logic(mon
);
3319 if (op
!= '+' && op
!= '-')
3322 val2
= expr_logic(mon
);
3331 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3334 if (setjmp(expr_env
)) {
3338 while (qemu_isspace(*pch
))
3340 *pval
= expr_sum(mon
);
3345 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3347 const char *p
= *pp
;
3351 d
= strtod(p
, &tailp
);
3353 monitor_printf(mon
, "Number expected\n");
3356 if (d
!= d
|| d
- d
!= 0) {
3357 /* NaN or infinity */
3358 monitor_printf(mon
, "Bad number\n");
3366 static int get_str(char *buf
, int buf_size
, const char **pp
)
3374 while (qemu_isspace(*p
))
3384 while (*p
!= '\0' && *p
!= '\"') {
3400 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3403 if ((q
- buf
) < buf_size
- 1) {
3407 if ((q
- buf
) < buf_size
- 1) {
3414 qemu_printf("unterminated string\n");
3419 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3420 if ((q
- buf
) < buf_size
- 1) {
3432 * Store the command-name in cmdname, and return a pointer to
3433 * the remaining of the command string.
3435 static const char *get_command_name(const char *cmdline
,
3436 char *cmdname
, size_t nlen
)
3439 const char *p
, *pstart
;
3442 while (qemu_isspace(*p
))
3447 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3452 memcpy(cmdname
, pstart
, len
);
3453 cmdname
[len
] = '\0';
3458 * Read key of 'type' into 'key' and return the current
3461 static char *key_get_info(const char *type
, char **key
)
3469 p
= strchr(type
, ':');
3476 str
= qemu_malloc(len
+ 1);
3477 memcpy(str
, type
, len
);
3484 static int default_fmt_format
= 'x';
3485 static int default_fmt_size
= 4;
3489 static int is_valid_option(const char *c
, const char *typestr
)
3497 typestr
= strstr(typestr
, option
);
3498 return (typestr
!= NULL
);
3501 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3502 const char *cmdname
)
3504 const mon_cmd_t
*cmd
;
3506 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3507 if (compare_cmd(cmdname
, cmd
->name
)) {
3515 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3517 return search_dispatch_table(mon_cmds
, cmdname
);
3520 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
3522 return search_dispatch_table(qmp_query_cmds
, info_item
);
3525 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3527 return search_dispatch_table(qmp_cmds
, cmdname
);
3530 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3531 const char *cmdline
,
3534 const char *p
, *typestr
;
3536 const mon_cmd_t
*cmd
;
3542 monitor_printf(mon
, "command='%s'\n", cmdline
);
3545 /* extract the command name */
3546 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3550 cmd
= monitor_find_command(cmdname
);
3552 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3556 /* parse the parameters */
3557 typestr
= cmd
->args_type
;
3559 typestr
= key_get_info(typestr
, &key
);
3571 while (qemu_isspace(*p
))
3573 if (*typestr
== '?') {
3576 /* no optional string: NULL argument */
3580 ret
= get_str(buf
, sizeof(buf
), &p
);
3584 monitor_printf(mon
, "%s: filename expected\n",
3588 monitor_printf(mon
, "%s: block device name expected\n",
3592 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3597 qdict_put(qdict
, key
, qstring_from_str(buf
));
3602 QemuOptsList
*opts_list
;
3605 opts_list
= qemu_find_opts(key
);
3606 if (!opts_list
|| opts_list
->desc
->name
) {
3609 while (qemu_isspace(*p
)) {
3614 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3617 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3621 qemu_opts_to_qdict(opts
, qdict
);
3622 qemu_opts_del(opts
);
3627 int count
, format
, size
;
3629 while (qemu_isspace(*p
))
3635 if (qemu_isdigit(*p
)) {
3637 while (qemu_isdigit(*p
)) {
3638 count
= count
* 10 + (*p
- '0');
3676 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3677 monitor_printf(mon
, "invalid char in format: '%c'\n",
3682 format
= default_fmt_format
;
3683 if (format
!= 'i') {
3684 /* for 'i', not specifying a size gives -1 as size */
3686 size
= default_fmt_size
;
3687 default_fmt_size
= size
;
3689 default_fmt_format
= format
;
3692 format
= default_fmt_format
;
3693 if (format
!= 'i') {
3694 size
= default_fmt_size
;
3699 qdict_put(qdict
, "count", qint_from_int(count
));
3700 qdict_put(qdict
, "format", qint_from_int(format
));
3701 qdict_put(qdict
, "size", qint_from_int(size
));
3710 while (qemu_isspace(*p
))
3712 if (*typestr
== '?' || *typestr
== '.') {
3713 if (*typestr
== '?') {
3721 while (qemu_isspace(*p
))
3730 if (get_expr(mon
, &val
, &p
))
3732 /* Check if 'i' is greater than 32-bit */
3733 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3734 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3735 monitor_printf(mon
, "integer is for 32-bit values\n");
3737 } else if (c
== 'M') {
3740 qdict_put(qdict
, key
, qint_from_int(val
));
3748 while (qemu_isspace(*p
)) {
3751 if (*typestr
== '?') {
3757 val
= strtosz(p
, &end
);
3759 monitor_printf(mon
, "invalid size\n");
3762 qdict_put(qdict
, key
, qint_from_int(val
));
3770 while (qemu_isspace(*p
))
3772 if (*typestr
== '?') {
3778 if (get_double(mon
, &val
, &p
) < 0) {
3781 if (p
[0] && p
[1] == 's') {
3784 val
/= 1e3
; p
+= 2; break;
3786 val
/= 1e6
; p
+= 2; break;
3788 val
/= 1e9
; p
+= 2; break;
3791 if (*p
&& !qemu_isspace(*p
)) {
3792 monitor_printf(mon
, "Unknown unit suffix\n");
3795 qdict_put(qdict
, key
, qfloat_from_double(val
));
3803 while (qemu_isspace(*p
)) {
3807 while (qemu_isgraph(*p
)) {
3810 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3812 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3815 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3818 qdict_put(qdict
, key
, qbool_from_int(val
));
3823 const char *tmp
= p
;
3830 while (qemu_isspace(*p
))
3835 if(!is_valid_option(p
, typestr
)) {
3837 monitor_printf(mon
, "%s: unsupported option -%c\n",
3849 qdict_put(qdict
, key
, qbool_from_int(1));
3856 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3862 /* check that all arguments were parsed */
3863 while (qemu_isspace(*p
))
3866 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3878 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
3880 /* report only the first error */
3882 mon
->error
= qerror
;
3884 MON_DEBUG("Additional error report at %s:%d\n",
3885 qerror
->file
, qerror
->linenr
);
3890 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
3892 if (ret
&& !monitor_has_error(mon
)) {
3894 * If it returns failure, it must have passed on error.
3896 * Action: Report an internal error to the client if in QMP.
3898 qerror_report(QERR_UNDEFINED_ERROR
);
3899 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3903 #ifdef CONFIG_DEBUG_MONITOR
3904 if (!ret
&& monitor_has_error(mon
)) {
3906 * If it returns success, it must not have passed an error.
3908 * Action: Report the passed error to the client.
3910 MON_DEBUG("command '%s' returned success but passed an error\n",
3914 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
3916 * Handlers should not call Monitor print functions.
3918 * Action: Ignore them in QMP.
3920 * (XXX: we don't check any 'info' or 'query' command here
3921 * because the user print function _is_ called by do_info(), hence
3922 * we will trigger this check. This problem will go away when we
3923 * make 'query' commands real and kill do_info())
3925 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3926 cmd
->name
, mon_print_count_get(mon
));
3931 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
3934 const mon_cmd_t
*cmd
;
3936 qdict
= qdict_new();
3938 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3942 if (handler_is_async(cmd
)) {
3943 user_async_cmd_handler(mon
, cmd
, qdict
);
3944 } else if (handler_is_qobject(cmd
)) {
3945 QObject
*data
= NULL
;
3947 /* XXX: ignores the error code */
3948 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3949 assert(!monitor_has_error(mon
));
3951 cmd
->user_print(mon
, data
);
3952 qobject_decref(data
);
3955 cmd
->mhandler
.cmd(mon
, qdict
);
3962 static void cmd_completion(const char *name
, const char *list
)
3964 const char *p
, *pstart
;
3973 p
= pstart
+ strlen(pstart
);
3975 if (len
> sizeof(cmd
) - 2)
3976 len
= sizeof(cmd
) - 2;
3977 memcpy(cmd
, pstart
, len
);
3979 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3980 readline_add_completion(cur_mon
->rs
, cmd
);
3988 static void file_completion(const char *input
)
3993 char file
[1024], file_prefix
[1024];
3997 p
= strrchr(input
, '/');
4000 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4001 pstrcpy(path
, sizeof(path
), ".");
4003 input_path_len
= p
- input
+ 1;
4004 memcpy(path
, input
, input_path_len
);
4005 if (input_path_len
> sizeof(path
) - 1)
4006 input_path_len
= sizeof(path
) - 1;
4007 path
[input_path_len
] = '\0';
4008 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4010 #ifdef DEBUG_COMPLETION
4011 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4012 input
, path
, file_prefix
);
4014 ffs
= opendir(path
);
4023 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4027 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4028 memcpy(file
, input
, input_path_len
);
4029 if (input_path_len
< sizeof(file
))
4030 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4032 /* stat the file to find out if it's a directory.
4033 * In that case add a slash to speed up typing long paths
4036 if(S_ISDIR(sb
.st_mode
))
4037 pstrcat(file
, sizeof(file
), "/");
4038 readline_add_completion(cur_mon
->rs
, file
);
4044 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4046 const char *name
= bdrv_get_device_name(bs
);
4047 const char *input
= opaque
;
4049 if (input
[0] == '\0' ||
4050 !strncmp(name
, (char *)input
, strlen(input
))) {
4051 readline_add_completion(cur_mon
->rs
, name
);
4055 /* NOTE: this parser is an approximate form of the real command parser */
4056 static void parse_cmdline(const char *cmdline
,
4057 int *pnb_args
, char **args
)
4066 while (qemu_isspace(*p
))
4070 if (nb_args
>= MAX_ARGS
)
4072 ret
= get_str(buf
, sizeof(buf
), &p
);
4073 args
[nb_args
] = qemu_strdup(buf
);
4078 *pnb_args
= nb_args
;
4081 static const char *next_arg_type(const char *typestr
)
4083 const char *p
= strchr(typestr
, ':');
4084 return (p
!= NULL
? ++p
: typestr
);
4087 static void monitor_find_completion(const char *cmdline
)
4089 const char *cmdname
;
4090 char *args
[MAX_ARGS
];
4091 int nb_args
, i
, len
;
4092 const char *ptype
, *str
;
4093 const mon_cmd_t
*cmd
;
4096 parse_cmdline(cmdline
, &nb_args
, args
);
4097 #ifdef DEBUG_COMPLETION
4098 for(i
= 0; i
< nb_args
; i
++) {
4099 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4103 /* if the line ends with a space, it means we want to complete the
4105 len
= strlen(cmdline
);
4106 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4107 if (nb_args
>= MAX_ARGS
) {
4110 args
[nb_args
++] = qemu_strdup("");
4113 /* command completion */
4118 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4119 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4120 cmd_completion(cmdname
, cmd
->name
);
4123 /* find the command */
4124 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4125 if (compare_cmd(args
[0], cmd
->name
)) {
4133 ptype
= next_arg_type(cmd
->args_type
);
4134 for(i
= 0; i
< nb_args
- 2; i
++) {
4135 if (*ptype
!= '\0') {
4136 ptype
= next_arg_type(ptype
);
4137 while (*ptype
== '?')
4138 ptype
= next_arg_type(ptype
);
4141 str
= args
[nb_args
- 1];
4142 if (*ptype
== '-' && ptype
[1] != '\0') {
4143 ptype
= next_arg_type(ptype
);
4147 /* file completion */
4148 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4149 file_completion(str
);
4152 /* block device name completion */
4153 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4154 bdrv_iterate(block_completion_it
, (void *)str
);
4157 /* XXX: more generic ? */
4158 if (!strcmp(cmd
->name
, "info")) {
4159 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4160 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4161 cmd_completion(str
, cmd
->name
);
4163 } else if (!strcmp(cmd
->name
, "sendkey")) {
4164 char *sep
= strrchr(str
, '-');
4167 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4168 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4169 cmd_completion(str
, key
->name
);
4171 } else if (!strcmp(cmd
->name
, "help|?")) {
4172 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4173 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4174 cmd_completion(str
, cmd
->name
);
4184 for (i
= 0; i
< nb_args
; i
++) {
4189 static int monitor_can_read(void *opaque
)
4191 Monitor
*mon
= opaque
;
4193 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4196 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4198 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4199 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4203 * Argument validation rules:
4205 * 1. The argument must exist in cmd_args qdict
4206 * 2. The argument type must be the expected one
4208 * Special case: If the argument doesn't exist in cmd_args and
4209 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4210 * checking is skipped for it.
4212 static int check_client_args_type(const QDict
*client_args
,
4213 const QDict
*cmd_args
, int flags
)
4215 const QDictEntry
*ent
;
4217 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4220 const QObject
*client_arg
= qdict_entry_value(ent
);
4221 const char *client_arg_name
= qdict_entry_key(ent
);
4223 obj
= qdict_get(cmd_args
, client_arg_name
);
4225 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4226 /* handler accepts unknowns */
4229 /* client arg doesn't exist */
4230 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4234 arg_type
= qobject_to_qstring(obj
);
4235 assert(arg_type
!= NULL
);
4237 /* check if argument's type is correct */
4238 switch (qstring_get_str(arg_type
)[0]) {
4242 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4243 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4252 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4253 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4259 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4260 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4261 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4268 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4269 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4275 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4280 * These types are not supported by QMP and thus are not
4281 * handled here. Fall through.
4292 * - Check if the client has passed all mandatory args
4293 * - Set special flags for argument validation
4295 static int check_mandatory_args(const QDict
*cmd_args
,
4296 const QDict
*client_args
, int *flags
)
4298 const QDictEntry
*ent
;
4300 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4301 const char *cmd_arg_name
= qdict_entry_key(ent
);
4302 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4303 assert(type
!= NULL
);
4305 if (qstring_get_str(type
)[0] == 'O') {
4306 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4307 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4308 } else if (qstring_get_str(type
)[0] != '-' &&
4309 qstring_get_str(type
)[1] != '?' &&
4310 !qdict_haskey(client_args
, cmd_arg_name
)) {
4311 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4319 static QDict
*qdict_from_args_type(const char *args_type
)
4323 QString
*key
, *type
, *cur_qs
;
4325 assert(args_type
!= NULL
);
4327 qdict
= qdict_new();
4329 if (args_type
== NULL
|| args_type
[0] == '\0') {
4330 /* no args, empty qdict */
4334 key
= qstring_new();
4335 type
= qstring_new();
4340 switch (args_type
[i
]) {
4343 qdict_put(qdict
, qstring_get_str(key
), type
);
4345 if (args_type
[i
] == '\0') {
4348 type
= qstring_new(); /* qdict has ref */
4349 cur_qs
= key
= qstring_new();
4355 qstring_append_chr(cur_qs
, args_type
[i
]);
4365 * Client argument checking rules:
4367 * 1. Client must provide all mandatory arguments
4368 * 2. Each argument provided by the client must be expected
4369 * 3. Each argument provided by the client must have the type expected
4372 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4377 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4380 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4385 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4393 * Input object checking rules
4395 * 1. Input object must be a dict
4396 * 2. The "execute" key must exist
4397 * 3. The "execute" key must be a string
4398 * 4. If the "arguments" key exists, it must be a dict
4399 * 5. If the "id" key exists, it can be anything (ie. json-value)
4400 * 6. Any argument not listed above is considered invalid
4402 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4404 const QDictEntry
*ent
;
4405 int has_exec_key
= 0;
4408 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4409 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4413 input_dict
= qobject_to_qdict(input_obj
);
4415 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4416 const char *arg_name
= qdict_entry_key(ent
);
4417 const QObject
*arg_obj
= qdict_entry_value(ent
);
4419 if (!strcmp(arg_name
, "execute")) {
4420 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4421 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4426 } else if (!strcmp(arg_name
, "arguments")) {
4427 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4428 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4432 } else if (!strcmp(arg_name
, "id")) {
4433 /* FIXME: check duplicated IDs for async commands */
4435 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4440 if (!has_exec_key
) {
4441 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4448 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
4450 QObject
*ret_data
= NULL
;
4452 if (handler_is_async(cmd
)) {
4453 qmp_async_info_handler(mon
, cmd
);
4454 if (monitor_has_error(mon
)) {
4455 monitor_protocol_emitter(mon
, NULL
);
4458 cmd
->mhandler
.info_new(mon
, &ret_data
);
4459 monitor_protocol_emitter(mon
, ret_data
);
4460 qobject_decref(ret_data
);
4464 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4465 const QDict
*params
)
4468 QObject
*data
= NULL
;
4470 mon_print_count_init(mon
);
4472 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4473 handler_audit(mon
, cmd
, ret
);
4474 monitor_protocol_emitter(mon
, data
);
4475 qobject_decref(data
);
4478 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4482 QDict
*input
, *args
;
4483 const mon_cmd_t
*cmd
;
4484 Monitor
*mon
= cur_mon
;
4485 const char *cmd_name
, *query_cmd
;
4488 args
= input
= NULL
;
4490 obj
= json_parser_parse(tokens
, NULL
);
4492 // FIXME: should be triggered in json_parser_parse()
4493 qerror_report(QERR_JSON_PARSING
);
4497 input
= qmp_check_input_obj(obj
);
4499 qobject_decref(obj
);
4503 mon
->mc
->id
= qdict_get(input
, "id");
4504 qobject_incref(mon
->mc
->id
);
4506 cmd_name
= qdict_get_str(input
, "execute");
4507 if (invalid_qmp_mode(mon
, cmd_name
)) {
4508 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4512 if (strstart(cmd_name
, "query-", &query_cmd
)) {
4513 cmd
= qmp_find_query_cmd(query_cmd
);
4515 cmd
= qmp_find_cmd(cmd_name
);
4519 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4523 obj
= qdict_get(input
, "arguments");
4527 args
= qobject_to_qdict(obj
);
4531 err
= qmp_check_client_args(cmd
, args
);
4537 qmp_call_query_cmd(mon
, cmd
);
4538 } else if (handler_is_async(cmd
)) {
4539 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4541 /* emit the error response */
4545 qmp_call_cmd(mon
, cmd
, args
);
4551 monitor_protocol_emitter(mon
, NULL
);
4558 * monitor_control_read(): Read and handle QMP input
4560 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4562 Monitor
*old_mon
= cur_mon
;
4566 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4571 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4573 Monitor
*old_mon
= cur_mon
;
4579 for (i
= 0; i
< size
; i
++)
4580 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4582 if (size
== 0 || buf
[size
- 1] != 0)
4583 monitor_printf(cur_mon
, "corrupted command\n");
4585 handle_user_command(cur_mon
, (char *)buf
);
4591 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4593 monitor_suspend(mon
);
4594 handle_user_command(mon
, cmdline
);
4595 monitor_resume(mon
);
4598 int monitor_suspend(Monitor
*mon
)
4606 void monitor_resume(Monitor
*mon
)
4610 if (--mon
->suspend_cnt
== 0)
4611 readline_show_prompt(mon
->rs
);
4614 static QObject
*get_qmp_greeting(void)
4618 do_info_version(NULL
, &ver
);
4619 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4623 * monitor_control_event(): Print QMP gretting
4625 static void monitor_control_event(void *opaque
, int event
)
4628 Monitor
*mon
= opaque
;
4631 case CHR_EVENT_OPENED
:
4632 mon
->mc
->command_mode
= 0;
4633 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4634 data
= get_qmp_greeting();
4635 monitor_json_emitter(mon
, data
);
4636 qobject_decref(data
);
4638 case CHR_EVENT_CLOSED
:
4639 json_message_parser_destroy(&mon
->mc
->parser
);
4644 static void monitor_event(void *opaque
, int event
)
4646 Monitor
*mon
= opaque
;
4649 case CHR_EVENT_MUX_IN
:
4651 if (mon
->reset_seen
) {
4652 readline_restart(mon
->rs
);
4653 monitor_resume(mon
);
4656 mon
->suspend_cnt
= 0;
4660 case CHR_EVENT_MUX_OUT
:
4661 if (mon
->reset_seen
) {
4662 if (mon
->suspend_cnt
== 0) {
4663 monitor_printf(mon
, "\n");
4666 monitor_suspend(mon
);
4673 case CHR_EVENT_OPENED
:
4674 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4675 "information\n", QEMU_VERSION
);
4676 if (!mon
->mux_out
) {
4677 readline_show_prompt(mon
->rs
);
4679 mon
->reset_seen
= 1;
4693 void monitor_init(CharDriverState
*chr
, int flags
)
4695 static int is_first_init
= 1;
4698 if (is_first_init
) {
4699 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
4703 mon
= qemu_mallocz(sizeof(*mon
));
4707 if (flags
& MONITOR_USE_READLINE
) {
4708 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4709 monitor_read_command(mon
, 0);
4712 if (monitor_ctrl_mode(mon
)) {
4713 mon
->mc
= qemu_mallocz(sizeof(MonitorControl
));
4714 /* Control mode requires special handlers */
4715 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4716 monitor_control_event
, mon
);
4718 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4719 monitor_event
, mon
);
4722 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4723 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
4727 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
4729 BlockDriverState
*bs
= opaque
;
4732 if (bdrv_set_key(bs
, password
) != 0) {
4733 monitor_printf(mon
, "invalid password\n");
4736 if (mon
->password_completion_cb
)
4737 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4739 monitor_read_command(mon
, 1);
4742 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4743 BlockDriverCompletionFunc
*completion_cb
,
4748 if (!bdrv_key_required(bs
)) {
4750 completion_cb(opaque
, 0);
4754 if (monitor_ctrl_mode(mon
)) {
4755 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
4759 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4760 bdrv_get_encrypted_filename(bs
));
4762 mon
->password_completion_cb
= completion_cb
;
4763 mon
->password_opaque
= opaque
;
4765 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4767 if (err
&& completion_cb
)
4768 completion_cb(opaque
, err
);