4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
43 #include "audio/audio.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
60 #include "trace/control.h"
61 #ifdef CONFIG_TRACE_SIMPLE
62 #include "trace/simple.h"
64 #include "trace/control.h"
65 #include "ui/qemu-spice.h"
69 //#define DEBUG_COMPLETION
75 * 'B' block device name
76 * 's' string (accept optional quote)
77 * 'O' option string of the form NAME=VALUE,...
78 * parsed according to QemuOptsList given by its name
79 * Example: 'device:O' uses qemu_device_opts.
80 * Restriction: only lists with empty desc are supported
81 * TODO lift the restriction
83 * 'l' target long (32 or 64 bit)
84 * 'M' just like 'l', except in user mode the value is
85 * multiplied by 2^20 (think Mebibyte)
86 * 'o' octets (aka bytes)
87 * user mode accepts an optional T, t, G, g, M, m, K, k
88 * suffix, which multiplies the value by 2^40 for
89 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
90 * M and m, 2^10 for K and k
92 * user mode accepts an optional ms, us, ns suffix,
93 * which divides the value by 1e3, 1e6, 1e9, respectively
94 * '/' optional gdb-like print format (like "/10x")
96 * '?' optional type (for all types, except '/')
97 * '.' other form of optional type (for 'i' and 'l')
99 * user mode accepts "on" or "off"
100 * '-' optional parameter (eg. '-f')
104 typedef struct MonitorCompletionData MonitorCompletionData
;
105 struct MonitorCompletionData
{
107 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
110 typedef struct mon_cmd_t
{
112 const char *args_type
;
115 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
117 void (*info
)(Monitor
*mon
);
118 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
119 int (*info_async
)(Monitor
*mon
, MonitorCompletion
*cb
, void *opaque
);
120 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
121 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
122 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
123 MonitorCompletion
*cb
, void *opaque
);
129 /* file descriptors passed via SCM_RIGHTS */
130 typedef struct mon_fd_t mon_fd_t
;
134 QLIST_ENTRY(mon_fd_t
) next
;
137 typedef struct MonitorControl
{
139 JSONMessageParser parser
;
144 CharDriverState
*chr
;
149 uint8_t outbuf
[1024];
154 BlockDriverCompletionFunc
*password_completion_cb
;
155 void *password_opaque
;
156 #ifdef CONFIG_DEBUG_MONITOR
160 QLIST_HEAD(,mon_fd_t
) fds
;
161 QLIST_ENTRY(Monitor
) entry
;
164 #ifdef CONFIG_DEBUG_MONITOR
165 #define MON_DEBUG(fmt, ...) do { \
166 fprintf(stderr, "Monitor: "); \
167 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
169 static inline void mon_print_count_inc(Monitor
*mon
)
171 mon
->print_calls_nr
++;
174 static inline void mon_print_count_init(Monitor
*mon
)
176 mon
->print_calls_nr
= 0;
179 static inline int mon_print_count_get(const Monitor
*mon
)
181 return mon
->print_calls_nr
;
184 #else /* !CONFIG_DEBUG_MONITOR */
185 #define MON_DEBUG(fmt, ...) do { } while (0)
186 static inline void mon_print_count_inc(Monitor
*mon
) { }
187 static inline void mon_print_count_init(Monitor
*mon
) { }
188 static inline int mon_print_count_get(const Monitor
*mon
) { return 0; }
189 #endif /* CONFIG_DEBUG_MONITOR */
191 /* QMP checker flags */
192 #define QMP_ACCEPT_UNKNOWNS 1
194 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
196 static const mon_cmd_t mon_cmds
[];
197 static const mon_cmd_t info_cmds
[];
199 static const mon_cmd_t qmp_cmds
[];
200 static const mon_cmd_t qmp_query_cmds
[];
203 Monitor
*default_mon
;
205 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
208 static inline int qmp_cmd_mode(const Monitor
*mon
)
210 return (mon
->mc
? mon
->mc
->command_mode
: 0);
213 /* Return true if in control mode, false otherwise */
214 static inline int monitor_ctrl_mode(const Monitor
*mon
)
216 return (mon
->flags
& MONITOR_USE_CONTROL
);
219 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
220 int monitor_cur_is_qmp(void)
222 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
225 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
230 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
232 readline_show_prompt(mon
->rs
);
235 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
238 if (monitor_ctrl_mode(mon
)) {
239 qerror_report(QERR_MISSING_PARAMETER
, "password");
241 } else if (mon
->rs
) {
242 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
243 /* prompt is printed on return from the command handler */
246 monitor_printf(mon
, "terminal does not support password prompting\n");
251 void monitor_flush(Monitor
*mon
)
253 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
254 qemu_chr_fe_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
255 mon
->outbuf_index
= 0;
259 /* flush at every end of line or if the buffer is full */
260 static void monitor_puts(Monitor
*mon
, const char *str
)
269 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
270 mon
->outbuf
[mon
->outbuf_index
++] = c
;
271 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
277 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
284 mon_print_count_inc(mon
);
286 if (monitor_ctrl_mode(mon
)) {
290 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
291 monitor_puts(mon
, buf
);
294 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
298 monitor_vprintf(mon
, fmt
, ap
);
302 void monitor_print_filename(Monitor
*mon
, const char *filename
)
306 for (i
= 0; filename
[i
]; i
++) {
307 switch (filename
[i
]) {
311 monitor_printf(mon
, "\\%c", filename
[i
]);
314 monitor_printf(mon
, "\\t");
317 monitor_printf(mon
, "\\r");
320 monitor_printf(mon
, "\\n");
323 monitor_printf(mon
, "%c", filename
[i
]);
329 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
330 const char *fmt
, ...)
334 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
339 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
341 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
343 return cmd
->user_print
!= NULL
;
346 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
348 return cmd
->flags
& MONITOR_CMD_ASYNC
;
351 static inline int monitor_has_error(const Monitor
*mon
)
353 return mon
->error
!= NULL
;
356 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
360 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
361 qobject_to_json(data
);
362 assert(json
!= NULL
);
364 qstring_append_chr(json
, '\n');
365 monitor_puts(mon
, qstring_get_str(json
));
370 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
376 if (!monitor_has_error(mon
)) {
377 /* success response */
379 qobject_incref(data
);
380 qdict_put_obj(qmp
, "return", data
);
382 /* return an empty QDict by default */
383 qdict_put(qmp
, "return", qdict_new());
387 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
388 qdict_put(qmp
, "error", mon
->error
->error
);
389 QINCREF(mon
->error
->error
);
395 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
399 monitor_json_emitter(mon
, QOBJECT(qmp
));
403 static void timestamp_put(QDict
*qdict
)
409 err
= qemu_gettimeofday(&tv
);
413 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
414 "'microseconds': %" PRId64
" }",
415 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
416 qdict_put_obj(qdict
, "timestamp", obj
);
420 * monitor_protocol_event(): Generate a Monitor event
422 * Event-specific data can be emitted through the (optional) 'data' parameter.
424 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
427 const char *event_name
;
430 assert(event
< QEVENT_MAX
);
433 case QEVENT_SHUTDOWN
:
434 event_name
= "SHUTDOWN";
437 event_name
= "RESET";
439 case QEVENT_POWERDOWN
:
440 event_name
= "POWERDOWN";
446 event_name
= "RESUME";
448 case QEVENT_VNC_CONNECTED
:
449 event_name
= "VNC_CONNECTED";
451 case QEVENT_VNC_INITIALIZED
:
452 event_name
= "VNC_INITIALIZED";
454 case QEVENT_VNC_DISCONNECTED
:
455 event_name
= "VNC_DISCONNECTED";
457 case QEVENT_BLOCK_IO_ERROR
:
458 event_name
= "BLOCK_IO_ERROR";
460 case QEVENT_RTC_CHANGE
:
461 event_name
= "RTC_CHANGE";
463 case QEVENT_WATCHDOG
:
464 event_name
= "WATCHDOG";
466 case QEVENT_SPICE_CONNECTED
:
467 event_name
= "SPICE_CONNECTED";
469 case QEVENT_SPICE_INITIALIZED
:
470 event_name
= "SPICE_INITIALIZED";
472 case QEVENT_SPICE_DISCONNECTED
:
473 event_name
= "SPICE_DISCONNECTED";
482 qdict_put(qmp
, "event", qstring_from_str(event_name
));
484 qobject_incref(data
);
485 qdict_put_obj(qmp
, "data", data
);
488 QLIST_FOREACH(mon
, &mon_list
, entry
) {
489 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
490 monitor_json_emitter(mon
, QOBJECT(qmp
));
496 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
499 /* Will setup QMP capabilities in the future */
500 if (monitor_ctrl_mode(mon
)) {
501 mon
->mc
->command_mode
= 1;
507 static int mon_set_cpu(int cpu_index
);
508 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
510 static int do_hmp_passthrough(Monitor
*mon
, const QDict
*params
,
514 Monitor
*old_mon
, hmp
;
515 CharDriverState mchar
;
517 memset(&hmp
, 0, sizeof(hmp
));
518 qemu_chr_init_mem(&mchar
);
524 if (qdict_haskey(params
, "cpu-index")) {
525 ret
= mon_set_cpu(qdict_get_int(params
, "cpu-index"));
528 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "cpu-index", "a CPU number");
533 handle_user_command(&hmp
, qdict_get_str(params
, "command-line"));
536 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
537 *ret_data
= QOBJECT(qemu_chr_mem_to_qs(hmp
.chr
));
541 qemu_chr_close_mem(hmp
.chr
);
545 static int compare_cmd(const char *name
, const char *list
)
547 const char *p
, *pstart
;
555 p
= pstart
+ strlen(pstart
);
556 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
565 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
566 const char *prefix
, const char *name
)
568 const mon_cmd_t
*cmd
;
570 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
571 if (!name
|| !strcmp(name
, cmd
->name
))
572 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
573 cmd
->params
, cmd
->help
);
577 static void help_cmd(Monitor
*mon
, const char *name
)
579 if (name
&& !strcmp(name
, "info")) {
580 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
582 help_cmd_dump(mon
, mon_cmds
, "", name
);
583 if (name
&& !strcmp(name
, "log")) {
584 const CPULogItem
*item
;
585 monitor_printf(mon
, "Log items (comma separated):\n");
586 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
587 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
588 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
594 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
596 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
599 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
601 const char *tp_name
= qdict_get_str(qdict
, "name");
602 bool new_state
= qdict_get_bool(qdict
, "option");
603 int ret
= trace_event_set_state(tp_name
, new_state
);
606 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
610 #ifdef CONFIG_SIMPLE_TRACE
611 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
613 const char *op
= qdict_get_try_str(qdict
, "op");
614 const char *arg
= qdict_get_try_str(qdict
, "arg");
617 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
618 } else if (!strcmp(op
, "on")) {
619 st_set_trace_file_enabled(true);
620 } else if (!strcmp(op
, "off")) {
621 st_set_trace_file_enabled(false);
622 } else if (!strcmp(op
, "flush")) {
623 st_flush_trace_buffer();
624 } else if (!strcmp(op
, "set")) {
626 st_set_trace_file(arg
);
629 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
630 help_cmd(mon
, "trace-file");
635 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
637 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
640 data
->user_print(data
->mon
, ret_data
);
642 monitor_resume(data
->mon
);
646 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
648 monitor_protocol_emitter(opaque
, ret_data
);
651 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
654 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
657 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
659 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
662 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
667 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
669 cb_data
->user_print
= cmd
->user_print
;
670 monitor_suspend(mon
);
671 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
672 user_monitor_complete
, cb_data
);
679 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
683 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
685 cb_data
->user_print
= cmd
->user_print
;
686 monitor_suspend(mon
);
687 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
694 static void do_info(Monitor
*mon
, const QDict
*qdict
)
696 const mon_cmd_t
*cmd
;
697 const char *item
= qdict_get_try_str(qdict
, "item");
703 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
704 if (compare_cmd(item
, cmd
->name
))
708 if (cmd
->name
== NULL
) {
712 if (handler_is_async(cmd
)) {
713 user_async_info_handler(mon
, cmd
);
714 } else if (handler_is_qobject(cmd
)) {
715 QObject
*info_data
= NULL
;
717 cmd
->mhandler
.info_new(mon
, &info_data
);
719 cmd
->user_print(mon
, info_data
);
720 qobject_decref(info_data
);
723 cmd
->mhandler
.info(mon
);
729 help_cmd(mon
, "info");
732 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
737 qdict
= qobject_to_qdict(data
);
738 qemu
= qdict_get_qdict(qdict
, "qemu");
740 monitor_printf(mon
, "%" PRId64
".%" PRId64
".%" PRId64
"%s\n",
741 qdict_get_int(qemu
, "major"),
742 qdict_get_int(qemu
, "minor"),
743 qdict_get_int(qemu
, "micro"),
744 qdict_get_str(qdict
, "package"));
747 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
749 const char *version
= QEMU_VERSION
;
750 int major
= 0, minor
= 0, micro
= 0;
753 major
= strtol(version
, &tmp
, 10);
755 minor
= strtol(tmp
, &tmp
, 10);
757 micro
= strtol(tmp
, &tmp
, 10);
759 *ret_data
= qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
760 'micro': %d }, 'package': %s }", major
, minor
, micro
, QEMU_PKGVERSION
);
763 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
767 qdict
= qobject_to_qdict(data
);
768 if (qdict_size(qdict
) == 0) {
772 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
775 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
777 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
778 qobject_from_jsonf("{}");
781 static QObject
*get_cmd_dict(const char *name
)
785 /* Remove '|' from some commands */
786 p
= strchr(name
, '|');
793 return qobject_from_jsonf("{ 'name': %s }", p
);
796 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
799 const mon_cmd_t
*cmd
;
801 cmd_list
= qlist_new();
803 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
804 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
807 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
809 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
810 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
813 *ret_data
= QOBJECT(cmd_list
);
816 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
818 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
821 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
825 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
826 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
827 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
828 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
829 qemu_uuid
[14], qemu_uuid
[15]);
830 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
833 /* get the current CPU defined by the user */
834 static int mon_set_cpu(int cpu_index
)
838 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
839 if (env
->cpu_index
== cpu_index
) {
840 cur_mon
->mon_cpu
= env
;
847 static CPUState
*mon_get_cpu(void)
849 if (!cur_mon
->mon_cpu
) {
852 cpu_synchronize_state(cur_mon
->mon_cpu
);
853 return cur_mon
->mon_cpu
;
856 static void do_info_registers(Monitor
*mon
)
861 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
864 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
869 static void print_cpu_iter(QObject
*obj
, void *opaque
)
873 Monitor
*mon
= opaque
;
875 assert(qobject_type(obj
) == QTYPE_QDICT
);
876 cpu
= qobject_to_qdict(obj
);
878 if (qdict_get_bool(cpu
, "current")) {
882 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
884 #if defined(TARGET_I386)
885 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
886 (target_ulong
) qdict_get_int(cpu
, "pc"));
887 #elif defined(TARGET_PPC)
888 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
889 (target_long
) qdict_get_int(cpu
, "nip"));
890 #elif defined(TARGET_SPARC)
891 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
892 (target_long
) qdict_get_int(cpu
, "pc"));
893 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
894 (target_long
) qdict_get_int(cpu
, "npc"));
895 #elif defined(TARGET_MIPS)
896 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
897 (target_long
) qdict_get_int(cpu
, "PC"));
900 if (qdict_get_bool(cpu
, "halted")) {
901 monitor_printf(mon
, " (halted)");
904 monitor_printf(mon
, " thread_id=%" PRId64
" ",
905 qdict_get_int(cpu
, "thread_id"));
907 monitor_printf(mon
, "\n");
910 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
914 assert(qobject_type(data
) == QTYPE_QLIST
);
915 cpu_list
= qobject_to_qlist(data
);
916 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
919 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
924 cpu_list
= qlist_new();
926 /* just to set the default cpu if not already done */
929 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
933 cpu_synchronize_state(env
);
935 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
936 env
->cpu_index
, env
== mon
->mon_cpu
,
939 cpu
= qobject_to_qdict(obj
);
941 #if defined(TARGET_I386)
942 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
943 #elif defined(TARGET_PPC)
944 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
945 #elif defined(TARGET_SPARC)
946 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
947 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
948 #elif defined(TARGET_MIPS)
949 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
951 qdict_put(cpu
, "thread_id", qint_from_int(env
->thread_id
));
953 qlist_append(cpu_list
, cpu
);
956 *ret_data
= QOBJECT(cpu_list
);
959 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
961 int index
= qdict_get_int(qdict
, "index");
962 if (mon_set_cpu(index
) < 0) {
963 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "index",
970 static void do_info_jit(Monitor
*mon
)
972 dump_exec_info((FILE *)mon
, monitor_fprintf
);
975 static void do_info_history(Monitor
*mon
)
984 str
= readline_get_history(mon
->rs
, i
);
987 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
992 #if defined(TARGET_PPC)
993 /* XXX: not implemented in other targets */
994 static void do_info_cpu_stats(Monitor
*mon
)
999 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
1003 #if defined(CONFIG_TRACE_SIMPLE)
1004 static void do_info_trace(Monitor
*mon
)
1006 st_print_trace((FILE *)mon
, &monitor_fprintf
);
1010 static void do_trace_print_events(Monitor
*mon
)
1012 trace_print_events((FILE *)mon
, &monitor_fprintf
);
1016 * do_quit(): Quit QEMU execution
1018 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1020 monitor_suspend(mon
);
1022 qemu_system_shutdown_request();
1028 static int change_vnc_password(const char *password
)
1030 if (!password
|| !password
[0]) {
1031 if (vnc_display_disable_login(NULL
)) {
1032 qerror_report(QERR_SET_PASSWD_FAILED
);
1038 if (vnc_display_password(NULL
, password
) < 0) {
1039 qerror_report(QERR_SET_PASSWD_FAILED
);
1046 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1049 change_vnc_password(password
);
1050 monitor_read_command(mon
, 1);
1053 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1055 if (strcmp(target
, "passwd") == 0 ||
1056 strcmp(target
, "password") == 0) {
1059 strncpy(password
, arg
, sizeof(password
));
1060 password
[sizeof(password
) - 1] = '\0';
1061 return change_vnc_password(password
);
1063 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1066 if (vnc_display_open(NULL
, target
) < 0) {
1067 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
1075 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1077 qerror_report(QERR_FEATURE_DISABLED
, "vnc");
1083 * do_change(): Change a removable medium, or VNC configuration
1085 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1087 const char *device
= qdict_get_str(qdict
, "device");
1088 const char *target
= qdict_get_str(qdict
, "target");
1089 const char *arg
= qdict_get_try_str(qdict
, "arg");
1092 if (strcmp(device
, "vnc") == 0) {
1093 ret
= do_change_vnc(mon
, target
, arg
);
1095 ret
= do_change_block(mon
, device
, target
, arg
);
1101 static int set_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1103 const char *protocol
= qdict_get_str(qdict
, "protocol");
1104 const char *password
= qdict_get_str(qdict
, "password");
1105 const char *connected
= qdict_get_try_str(qdict
, "connected");
1106 int disconnect_if_connected
= 0;
1107 int fail_if_connected
= 0;
1111 if (strcmp(connected
, "fail") == 0) {
1112 fail_if_connected
= 1;
1113 } else if (strcmp(connected
, "disconnect") == 0) {
1114 disconnect_if_connected
= 1;
1115 } else if (strcmp(connected
, "keep") == 0) {
1118 qerror_report(QERR_INVALID_PARAMETER
, "connected");
1123 if (strcmp(protocol
, "spice") == 0) {
1125 /* correct one? spice isn't a device ,,, */
1126 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1129 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
1130 disconnect_if_connected
);
1132 qerror_report(QERR_SET_PASSWD_FAILED
);
1138 if (strcmp(protocol
, "vnc") == 0) {
1139 if (fail_if_connected
|| disconnect_if_connected
) {
1140 /* vnc supports "connected=keep" only */
1141 qerror_report(QERR_INVALID_PARAMETER
, "connected");
1144 /* Note that setting an empty password will not disable login through
1145 * this interface. */
1146 return vnc_display_password(NULL
, password
);
1149 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1153 static int expire_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1155 const char *protocol
= qdict_get_str(qdict
, "protocol");
1156 const char *whenstr
= qdict_get_str(qdict
, "time");
1160 if (strcmp(whenstr
, "now") == 0) {
1162 } else if (strcmp(whenstr
, "never") == 0) {
1164 } else if (whenstr
[0] == '+') {
1165 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
1167 when
= strtoull(whenstr
, NULL
, 10);
1170 if (strcmp(protocol
, "spice") == 0) {
1172 /* correct one? spice isn't a device ,,, */
1173 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1176 rc
= qemu_spice_set_pw_expire(when
);
1178 qerror_report(QERR_SET_PASSWD_FAILED
);
1184 if (strcmp(protocol
, "vnc") == 0) {
1185 return vnc_display_pw_expire(NULL
, when
);
1188 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1192 static int add_graphics_client(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1194 const char *protocol
= qdict_get_str(qdict
, "protocol");
1195 const char *fdname
= qdict_get_str(qdict
, "fdname");
1198 if (strcmp(protocol
, "spice") == 0) {
1200 /* correct one? spice isn't a device ,,, */
1201 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1204 qerror_report(QERR_ADD_CLIENT_FAILED
);
1207 } else if (strcmp(protocol
, "vnc") == 0) {
1208 int fd
= monitor_get_fd(mon
, fdname
);
1209 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
1210 vnc_display_add_client(NULL
, fd
, skipauth
);
1213 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
1214 int fd
= monitor_get_fd(mon
, fdname
);
1215 if (qemu_chr_add_client(s
, fd
) < 0) {
1216 qerror_report(QERR_ADD_CLIENT_FAILED
);
1222 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1226 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1228 const char *protocol
= qdict_get_str(qdict
, "protocol");
1229 const char *hostname
= qdict_get_str(qdict
, "hostname");
1230 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
1231 int port
= qdict_get_try_int(qdict
, "port", -1);
1232 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
1235 if (strcmp(protocol
, "spice") == 0) {
1237 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1241 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
);
1243 qerror_report(QERR_UNDEFINED_ERROR
);
1249 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1253 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1255 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1259 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1261 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1264 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1267 const char *items
= qdict_get_str(qdict
, "items");
1269 if (!strcmp(items
, "none")) {
1272 mask
= cpu_str_to_log_mask(items
);
1274 help_cmd(mon
, "log");
1281 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1283 const char *option
= qdict_get_try_str(qdict
, "option");
1284 if (!option
|| !strcmp(option
, "on")) {
1286 } else if (!strcmp(option
, "off")) {
1289 monitor_printf(mon
, "unexpected option %s\n", option
);
1294 * do_stop(): Stop VM execution
1296 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1298 vm_stop(RSTATE_PAUSED
);
1302 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1304 struct bdrv_iterate_context
{
1310 * do_cont(): Resume emulation.
1312 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1314 struct bdrv_iterate_context context
= { mon
, 0 };
1316 if (runstate_check(RSTATE_IN_MIGRATE
)) {
1317 qerror_report(QERR_MIGRATION_EXPECTED
);
1319 } else if (runstate_check(RSTATE_PANICKED
) ||
1320 runstate_check(RSTATE_SHUTDOWN
)) {
1321 qerror_report(QERR_RESET_REQUIRED
);
1325 bdrv_iterate(encrypted_bdrv_it
, &context
);
1326 /* only resume the vm if all keys are set and valid */
1335 static void bdrv_key_cb(void *opaque
, int err
)
1337 Monitor
*mon
= opaque
;
1339 /* another key was set successfully, retry to continue */
1341 do_cont(mon
, NULL
, NULL
);
1344 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1346 struct bdrv_iterate_context
*context
= opaque
;
1348 if (!context
->err
&& bdrv_key_required(bs
)) {
1349 context
->err
= -EBUSY
;
1350 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1355 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1357 const char *device
= qdict_get_try_str(qdict
, "device");
1359 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1360 if (gdbserver_start(device
) < 0) {
1361 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1363 } else if (strcmp(device
, "none") == 0) {
1364 monitor_printf(mon
, "Disabled gdbserver\n");
1366 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1371 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1373 const char *action
= qdict_get_str(qdict
, "action");
1374 if (select_watchdog_action(action
) == -1) {
1375 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1379 static void monitor_printc(Monitor
*mon
, int c
)
1381 monitor_printf(mon
, "'");
1384 monitor_printf(mon
, "\\'");
1387 monitor_printf(mon
, "\\\\");
1390 monitor_printf(mon
, "\\n");
1393 monitor_printf(mon
, "\\r");
1396 if (c
>= 32 && c
<= 126) {
1397 monitor_printf(mon
, "%c", c
);
1399 monitor_printf(mon
, "\\x%02x", c
);
1403 monitor_printf(mon
, "'");
1406 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1407 target_phys_addr_t addr
, int is_physical
)
1410 int l
, line_size
, i
, max_digits
, len
;
1414 if (format
== 'i') {
1417 env
= mon_get_cpu();
1421 } else if (wsize
== 4) {
1424 /* as default we use the current CS size */
1427 #ifdef TARGET_X86_64
1428 if ((env
->efer
& MSR_EFER_LMA
) &&
1429 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1433 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1438 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1442 len
= wsize
* count
;
1451 max_digits
= (wsize
* 8 + 2) / 3;
1455 max_digits
= (wsize
* 8) / 4;
1459 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1468 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1470 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1475 cpu_physical_memory_read(addr
, buf
, l
);
1477 env
= mon_get_cpu();
1478 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1479 monitor_printf(mon
, " Cannot access memory\n");
1488 v
= ldub_raw(buf
+ i
);
1491 v
= lduw_raw(buf
+ i
);
1494 v
= (uint32_t)ldl_raw(buf
+ i
);
1497 v
= ldq_raw(buf
+ i
);
1500 monitor_printf(mon
, " ");
1503 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1506 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1509 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1512 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1515 monitor_printc(mon
, v
);
1520 monitor_printf(mon
, "\n");
1526 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1528 int count
= qdict_get_int(qdict
, "count");
1529 int format
= qdict_get_int(qdict
, "format");
1530 int size
= qdict_get_int(qdict
, "size");
1531 target_long addr
= qdict_get_int(qdict
, "addr");
1533 memory_dump(mon
, count
, format
, size
, addr
, 0);
1536 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1538 int count
= qdict_get_int(qdict
, "count");
1539 int format
= qdict_get_int(qdict
, "format");
1540 int size
= qdict_get_int(qdict
, "size");
1541 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1543 memory_dump(mon
, count
, format
, size
, addr
, 1);
1546 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1548 int format
= qdict_get_int(qdict
, "format");
1549 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1551 #if TARGET_PHYS_ADDR_BITS == 32
1554 monitor_printf(mon
, "%#o", val
);
1557 monitor_printf(mon
, "%#x", val
);
1560 monitor_printf(mon
, "%u", val
);
1564 monitor_printf(mon
, "%d", val
);
1567 monitor_printc(mon
, val
);
1573 monitor_printf(mon
, "%#" PRIo64
, val
);
1576 monitor_printf(mon
, "%#" PRIx64
, val
);
1579 monitor_printf(mon
, "%" PRIu64
, val
);
1583 monitor_printf(mon
, "%" PRId64
, val
);
1586 monitor_printc(mon
, val
);
1590 monitor_printf(mon
, "\n");
1593 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1596 uint32_t size
= qdict_get_int(qdict
, "size");
1597 const char *filename
= qdict_get_str(qdict
, "filename");
1598 target_long addr
= qdict_get_int(qdict
, "val");
1604 env
= mon_get_cpu();
1606 f
= fopen(filename
, "wb");
1608 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1615 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1616 if (fwrite(buf
, 1, l
, f
) != l
) {
1617 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1631 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1637 uint32_t size
= qdict_get_int(qdict
, "size");
1638 const char *filename
= qdict_get_str(qdict
, "filename");
1639 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1642 f
= fopen(filename
, "wb");
1644 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1651 cpu_physical_memory_read(addr
, buf
, l
);
1652 if (fwrite(buf
, 1, l
, f
) != l
) {
1653 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1668 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1672 uint32_t start
= qdict_get_int(qdict
, "start");
1673 uint32_t size
= qdict_get_int(qdict
, "size");
1676 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1677 uint8_t val
= ldub_phys(addr
);
1678 /* BSD sum algorithm ('sum' Unix command) */
1679 sum
= (sum
>> 1) | (sum
<< 15);
1682 monitor_printf(mon
, "%05d\n", sum
);
1690 static const KeyDef key_defs
[] = {
1692 { 0x36, "shift_r" },
1697 { 0xe4, "altgr_r" },
1717 { 0x0e, "backspace" },
1730 { 0x1a, "bracket_left" },
1731 { 0x1b, "bracket_right" },
1743 { 0x27, "semicolon" },
1744 { 0x28, "apostrophe" },
1745 { 0x29, "grave_accent" },
1747 { 0x2b, "backslash" },
1759 { 0x37, "asterisk" },
1762 { 0x3a, "caps_lock" },
1773 { 0x45, "num_lock" },
1774 { 0x46, "scroll_lock" },
1776 { 0xb5, "kp_divide" },
1777 { 0x37, "kp_multiply" },
1778 { 0x4a, "kp_subtract" },
1780 { 0x9c, "kp_enter" },
1781 { 0x53, "kp_decimal" },
1814 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1829 { 0xfe, "compose" },
1834 static int get_keycode(const char *key
)
1840 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1841 if (!strcmp(key
, p
->name
))
1844 if (strstart(key
, "0x", NULL
)) {
1845 ret
= strtoul(key
, &endp
, 0);
1846 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1852 #define MAX_KEYCODES 16
1853 static uint8_t keycodes
[MAX_KEYCODES
];
1854 static int nb_pending_keycodes
;
1855 static QEMUTimer
*key_timer
;
1857 static void release_keys(void *opaque
)
1861 while (nb_pending_keycodes
> 0) {
1862 nb_pending_keycodes
--;
1863 keycode
= keycodes
[nb_pending_keycodes
];
1865 kbd_put_keycode(0xe0);
1866 kbd_put_keycode(keycode
| 0x80);
1870 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1872 char keyname_buf
[16];
1874 int keyname_len
, keycode
, i
;
1875 const char *string
= qdict_get_str(qdict
, "string");
1876 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1877 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1879 if (nb_pending_keycodes
> 0) {
1880 qemu_del_timer(key_timer
);
1887 separator
= strchr(string
, '-');
1888 keyname_len
= separator
? separator
- string
: strlen(string
);
1889 if (keyname_len
> 0) {
1890 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1891 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1892 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1895 if (i
== MAX_KEYCODES
) {
1896 monitor_printf(mon
, "too many keys\n");
1899 keyname_buf
[keyname_len
] = 0;
1900 keycode
= get_keycode(keyname_buf
);
1902 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1905 keycodes
[i
++] = keycode
;
1909 string
= separator
+ 1;
1911 nb_pending_keycodes
= i
;
1912 /* key down events */
1913 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1914 keycode
= keycodes
[i
];
1916 kbd_put_keycode(0xe0);
1917 kbd_put_keycode(keycode
& 0x7f);
1919 /* delayed key up events */
1920 qemu_mod_timer(key_timer
, qemu_get_clock_ns(vm_clock
) +
1921 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1924 static int mouse_button_state
;
1926 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1929 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1930 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1931 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1932 dx
= strtol(dx_str
, NULL
, 0);
1933 dy
= strtol(dy_str
, NULL
, 0);
1936 dz
= strtol(dz_str
, NULL
, 0);
1937 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1940 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1942 int button_state
= qdict_get_int(qdict
, "button_state");
1943 mouse_button_state
= button_state
;
1944 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1947 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1949 int size
= qdict_get_int(qdict
, "size");
1950 int addr
= qdict_get_int(qdict
, "addr");
1951 int has_index
= qdict_haskey(qdict
, "index");
1956 int index
= qdict_get_int(qdict
, "index");
1957 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1965 val
= cpu_inb(addr
);
1969 val
= cpu_inw(addr
);
1973 val
= cpu_inl(addr
);
1977 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1978 suffix
, addr
, size
* 2, val
);
1981 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1983 int size
= qdict_get_int(qdict
, "size");
1984 int addr
= qdict_get_int(qdict
, "addr");
1985 int val
= qdict_get_int(qdict
, "val");
1987 addr
&= IOPORTS_MASK
;
1992 cpu_outb(addr
, val
);
1995 cpu_outw(addr
, val
);
1998 cpu_outl(addr
, val
);
2003 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
2006 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
2008 res
= qemu_boot_set(bootdevice
);
2010 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
2011 } else if (res
> 0) {
2012 monitor_printf(mon
, "setting boot device list failed\n");
2014 monitor_printf(mon
, "no function defined to set boot device list for "
2015 "this architecture\n");
2020 * do_system_reset(): Issue a machine reset
2022 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
2025 qemu_system_reset_request();
2030 * do_system_powerdown(): Issue a machine powerdown
2032 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
2035 qemu_system_powerdown_request();
2039 #if defined(TARGET_I386)
2040 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
2041 target_phys_addr_t pte
,
2042 target_phys_addr_t mask
)
2044 #ifdef TARGET_X86_64
2045 if (addr
& (1ULL << 47)) {
2049 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
2050 " %c%c%c%c%c%c%c%c%c\n",
2053 pte
& PG_NX_MASK
? 'X' : '-',
2054 pte
& PG_GLOBAL_MASK
? 'G' : '-',
2055 pte
& PG_PSE_MASK
? 'P' : '-',
2056 pte
& PG_DIRTY_MASK
? 'D' : '-',
2057 pte
& PG_ACCESSED_MASK
? 'A' : '-',
2058 pte
& PG_PCD_MASK
? 'C' : '-',
2059 pte
& PG_PWT_MASK
? 'T' : '-',
2060 pte
& PG_USER_MASK
? 'U' : '-',
2061 pte
& PG_RW_MASK
? 'W' : '-');
2064 static void tlb_info_32(Monitor
*mon
, CPUState
*env
)
2066 unsigned int l1
, l2
;
2067 uint32_t pgd
, pde
, pte
;
2069 pgd
= env
->cr
[3] & ~0xfff;
2070 for(l1
= 0; l1
< 1024; l1
++) {
2071 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2072 pde
= le32_to_cpu(pde
);
2073 if (pde
& PG_PRESENT_MASK
) {
2074 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2076 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
2078 for(l2
= 0; l2
< 1024; l2
++) {
2079 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2080 pte
= le32_to_cpu(pte
);
2081 if (pte
& PG_PRESENT_MASK
) {
2082 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
2092 static void tlb_info_pae32(Monitor
*mon
, CPUState
*env
)
2094 unsigned int l1
, l2
, l3
;
2095 uint64_t pdpe
, pde
, pte
;
2096 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2098 pdp_addr
= env
->cr
[3] & ~0x1f;
2099 for (l1
= 0; l1
< 4; l1
++) {
2100 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2101 pdpe
= le64_to_cpu(pdpe
);
2102 if (pdpe
& PG_PRESENT_MASK
) {
2103 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2104 for (l2
= 0; l2
< 512; l2
++) {
2105 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2106 pde
= le64_to_cpu(pde
);
2107 if (pde
& PG_PRESENT_MASK
) {
2108 if (pde
& PG_PSE_MASK
) {
2109 /* 2M pages with PAE, CR4.PSE is ignored */
2110 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
2111 ~((target_phys_addr_t
)(1 << 20) - 1));
2113 pt_addr
= pde
& 0x3fffffffff000ULL
;
2114 for (l3
= 0; l3
< 512; l3
++) {
2115 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2116 pte
= le64_to_cpu(pte
);
2117 if (pte
& PG_PRESENT_MASK
) {
2118 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
2121 ~(target_phys_addr_t
)0xfff);
2131 #ifdef TARGET_X86_64
2132 static void tlb_info_64(Monitor
*mon
, CPUState
*env
)
2134 uint64_t l1
, l2
, l3
, l4
;
2135 uint64_t pml4e
, pdpe
, pde
, pte
;
2136 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
2138 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2139 for (l1
= 0; l1
< 512; l1
++) {
2140 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2141 pml4e
= le64_to_cpu(pml4e
);
2142 if (pml4e
& PG_PRESENT_MASK
) {
2143 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2144 for (l2
= 0; l2
< 512; l2
++) {
2145 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2146 pdpe
= le64_to_cpu(pdpe
);
2147 if (pdpe
& PG_PRESENT_MASK
) {
2148 if (pdpe
& PG_PSE_MASK
) {
2149 /* 1G pages, CR4.PSE is ignored */
2150 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
2151 0x3ffffc0000000ULL
);
2153 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2154 for (l3
= 0; l3
< 512; l3
++) {
2155 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2156 pde
= le64_to_cpu(pde
);
2157 if (pde
& PG_PRESENT_MASK
) {
2158 if (pde
& PG_PSE_MASK
) {
2159 /* 2M pages, CR4.PSE is ignored */
2160 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
2162 0x3ffffffe00000ULL
);
2164 pt_addr
= pde
& 0x3fffffffff000ULL
;
2165 for (l4
= 0; l4
< 512; l4
++) {
2166 cpu_physical_memory_read(pt_addr
2169 pte
= le64_to_cpu(pte
);
2170 if (pte
& PG_PRESENT_MASK
) {
2171 print_pte(mon
, (l1
<< 39) +
2173 (l3
<< 21) + (l4
<< 12),
2175 0x3fffffffff000ULL
);
2189 static void tlb_info(Monitor
*mon
)
2193 env
= mon_get_cpu();
2195 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2196 monitor_printf(mon
, "PG disabled\n");
2199 if (env
->cr
[4] & CR4_PAE_MASK
) {
2200 #ifdef TARGET_X86_64
2201 if (env
->hflags
& HF_LMA_MASK
) {
2202 tlb_info_64(mon
, env
);
2206 tlb_info_pae32(mon
, env
);
2209 tlb_info_32(mon
, env
);
2213 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
2215 target_phys_addr_t end
, int prot
)
2218 prot1
= *plast_prot
;
2219 if (prot
!= prot1
) {
2220 if (*pstart
!= -1) {
2221 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
2222 TARGET_FMT_plx
" %c%c%c\n",
2223 *pstart
, end
, end
- *pstart
,
2224 prot1
& PG_USER_MASK
? 'u' : '-',
2226 prot1
& PG_RW_MASK
? 'w' : '-');
2236 static void mem_info_32(Monitor
*mon
, CPUState
*env
)
2238 unsigned int l1
, l2
;
2239 int prot
, last_prot
;
2240 uint32_t pgd
, pde
, pte
;
2241 target_phys_addr_t start
, end
;
2243 pgd
= env
->cr
[3] & ~0xfff;
2246 for(l1
= 0; l1
< 1024; l1
++) {
2247 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2248 pde
= le32_to_cpu(pde
);
2250 if (pde
& PG_PRESENT_MASK
) {
2251 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2252 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2253 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2255 for(l2
= 0; l2
< 1024; l2
++) {
2256 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2257 pte
= le32_to_cpu(pte
);
2258 end
= (l1
<< 22) + (l2
<< 12);
2259 if (pte
& PG_PRESENT_MASK
) {
2261 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2265 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2270 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2273 /* Flush last range */
2274 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2277 static void mem_info_pae32(Monitor
*mon
, CPUState
*env
)
2279 unsigned int l1
, l2
, l3
;
2280 int prot
, last_prot
;
2281 uint64_t pdpe
, pde
, pte
;
2282 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2283 target_phys_addr_t start
, end
;
2285 pdp_addr
= env
->cr
[3] & ~0x1f;
2288 for (l1
= 0; l1
< 4; l1
++) {
2289 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2290 pdpe
= le64_to_cpu(pdpe
);
2292 if (pdpe
& PG_PRESENT_MASK
) {
2293 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2294 for (l2
= 0; l2
< 512; l2
++) {
2295 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2296 pde
= le64_to_cpu(pde
);
2297 end
= (l1
<< 30) + (l2
<< 21);
2298 if (pde
& PG_PRESENT_MASK
) {
2299 if (pde
& PG_PSE_MASK
) {
2300 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2302 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2304 pt_addr
= pde
& 0x3fffffffff000ULL
;
2305 for (l3
= 0; l3
< 512; l3
++) {
2306 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2307 pte
= le64_to_cpu(pte
);
2308 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
2309 if (pte
& PG_PRESENT_MASK
) {
2310 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
2315 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2320 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2325 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2328 /* Flush last range */
2329 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2333 #ifdef TARGET_X86_64
2334 static void mem_info_64(Monitor
*mon
, CPUState
*env
)
2336 int prot
, last_prot
;
2337 uint64_t l1
, l2
, l3
, l4
;
2338 uint64_t pml4e
, pdpe
, pde
, pte
;
2339 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
2341 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2344 for (l1
= 0; l1
< 512; l1
++) {
2345 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2346 pml4e
= le64_to_cpu(pml4e
);
2348 if (pml4e
& PG_PRESENT_MASK
) {
2349 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2350 for (l2
= 0; l2
< 512; l2
++) {
2351 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2352 pdpe
= le64_to_cpu(pdpe
);
2353 end
= (l1
<< 39) + (l2
<< 30);
2354 if (pdpe
& PG_PRESENT_MASK
) {
2355 if (pdpe
& PG_PSE_MASK
) {
2356 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
2359 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2361 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2362 for (l3
= 0; l3
< 512; l3
++) {
2363 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2364 pde
= le64_to_cpu(pde
);
2365 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
2366 if (pde
& PG_PRESENT_MASK
) {
2367 if (pde
& PG_PSE_MASK
) {
2368 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2370 prot
&= pml4e
& pdpe
;
2371 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2373 pt_addr
= pde
& 0x3fffffffff000ULL
;
2374 for (l4
= 0; l4
< 512; l4
++) {
2375 cpu_physical_memory_read(pt_addr
2378 pte
= le64_to_cpu(pte
);
2379 end
= (l1
<< 39) + (l2
<< 30) +
2380 (l3
<< 21) + (l4
<< 12);
2381 if (pte
& PG_PRESENT_MASK
) {
2382 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
2384 prot
&= pml4e
& pdpe
& pde
;
2388 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2393 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2399 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2404 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2407 /* Flush last range */
2408 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 48, 0);
2412 static void mem_info(Monitor
*mon
)
2416 env
= mon_get_cpu();
2418 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2419 monitor_printf(mon
, "PG disabled\n");
2422 if (env
->cr
[4] & CR4_PAE_MASK
) {
2423 #ifdef TARGET_X86_64
2424 if (env
->hflags
& HF_LMA_MASK
) {
2425 mem_info_64(mon
, env
);
2429 mem_info_pae32(mon
, env
);
2432 mem_info_32(mon
, env
);
2437 #if defined(TARGET_SH4)
2439 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2441 monitor_printf(mon
, " tlb%i:\t"
2442 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2443 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2444 "dirty=%hhu writethrough=%hhu\n",
2446 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2447 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2451 static void tlb_info(Monitor
*mon
)
2453 CPUState
*env
= mon_get_cpu();
2456 monitor_printf (mon
, "ITLB:\n");
2457 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2458 print_tlb (mon
, i
, &env
->itlb
[i
]);
2459 monitor_printf (mon
, "UTLB:\n");
2460 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2461 print_tlb (mon
, i
, &env
->utlb
[i
]);
2466 #if defined(TARGET_SPARC)
2467 static void tlb_info(Monitor
*mon
)
2469 CPUState
*env1
= mon_get_cpu();
2471 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
2475 static void do_info_mtree(Monitor
*mon
)
2477 mtree_info((fprintf_function
)monitor_printf
, mon
);
2480 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
2484 qdict
= qobject_to_qdict(data
);
2486 monitor_printf(mon
, "kvm support: ");
2487 if (qdict_get_bool(qdict
, "present")) {
2488 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2489 "enabled" : "disabled");
2491 monitor_printf(mon
, "not compiled\n");
2495 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2498 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2501 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2505 static void do_info_numa(Monitor
*mon
)
2510 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2511 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2512 monitor_printf(mon
, "node %d cpus:", i
);
2513 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2514 if (env
->numa_node
== i
) {
2515 monitor_printf(mon
, " %d", env
->cpu_index
);
2518 monitor_printf(mon
, "\n");
2519 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2524 #ifdef CONFIG_PROFILER
2529 static void do_info_profile(Monitor
*mon
)
2535 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2536 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2537 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2538 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2543 static void do_info_profile(Monitor
*mon
)
2545 monitor_printf(mon
, "Internal profiler not compiled\n");
2549 /* Capture support */
2550 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2552 static void do_info_capture(Monitor
*mon
)
2557 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2558 monitor_printf(mon
, "[%d]: ", i
);
2559 s
->ops
.info (s
->opaque
);
2564 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2567 int n
= qdict_get_int(qdict
, "n");
2570 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2572 s
->ops
.destroy (s
->opaque
);
2573 QLIST_REMOVE (s
, entries
);
2580 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2582 const char *path
= qdict_get_str(qdict
, "path");
2583 int has_freq
= qdict_haskey(qdict
, "freq");
2584 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2585 int has_bits
= qdict_haskey(qdict
, "bits");
2586 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2587 int has_channels
= qdict_haskey(qdict
, "nchannels");
2588 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2591 s
= g_malloc0 (sizeof (*s
));
2593 freq
= has_freq
? freq
: 44100;
2594 bits
= has_bits
? bits
: 16;
2595 nchannels
= has_channels
? nchannels
: 2;
2597 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2598 monitor_printf(mon
, "Failed to add wave capture\n");
2602 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2606 #if defined(TARGET_I386)
2607 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2611 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2612 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2618 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2620 qerror_report(QERR_UNSUPPORTED
);
2625 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2630 qdict
= qobject_to_qdict(data
);
2632 monitor_printf(mon
, "VM status: ");
2633 if (qdict_get_bool(qdict
, "running")) {
2634 monitor_printf(mon
, "running");
2635 if (qdict_get_bool(qdict
, "singlestep")) {
2636 monitor_printf(mon
, " (single step mode)");
2639 monitor_printf(mon
, "paused");
2642 status
= qdict_get_str(qdict
, "status");
2643 if (strcmp(status
, "paused") && strcmp(status
, "running")) {
2644 monitor_printf(mon
, " (%s)", status
);
2647 monitor_printf(mon
, "\n");
2650 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2652 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep
, runstate_as_string());
2655 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2657 qemu_acl
*acl
= qemu_acl_find(name
);
2660 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2665 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2667 const char *aclname
= qdict_get_str(qdict
, "aclname");
2668 qemu_acl
*acl
= find_acl(mon
, aclname
);
2669 qemu_acl_entry
*entry
;
2673 monitor_printf(mon
, "policy: %s\n",
2674 acl
->defaultDeny
? "deny" : "allow");
2675 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2677 monitor_printf(mon
, "%d: %s %s\n", i
,
2678 entry
->deny
? "deny" : "allow", entry
->match
);
2683 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2685 const char *aclname
= qdict_get_str(qdict
, "aclname");
2686 qemu_acl
*acl
= find_acl(mon
, aclname
);
2689 qemu_acl_reset(acl
);
2690 monitor_printf(mon
, "acl: removed all rules\n");
2694 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2696 const char *aclname
= qdict_get_str(qdict
, "aclname");
2697 const char *policy
= qdict_get_str(qdict
, "policy");
2698 qemu_acl
*acl
= find_acl(mon
, aclname
);
2701 if (strcmp(policy
, "allow") == 0) {
2702 acl
->defaultDeny
= 0;
2703 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2704 } else if (strcmp(policy
, "deny") == 0) {
2705 acl
->defaultDeny
= 1;
2706 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2708 monitor_printf(mon
, "acl: unknown policy '%s', "
2709 "expected 'deny' or 'allow'\n", policy
);
2714 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2716 const char *aclname
= qdict_get_str(qdict
, "aclname");
2717 const char *match
= qdict_get_str(qdict
, "match");
2718 const char *policy
= qdict_get_str(qdict
, "policy");
2719 int has_index
= qdict_haskey(qdict
, "index");
2720 int index
= qdict_get_try_int(qdict
, "index", -1);
2721 qemu_acl
*acl
= find_acl(mon
, aclname
);
2725 if (strcmp(policy
, "allow") == 0) {
2727 } else if (strcmp(policy
, "deny") == 0) {
2730 monitor_printf(mon
, "acl: unknown policy '%s', "
2731 "expected 'deny' or 'allow'\n", policy
);
2735 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2737 ret
= qemu_acl_append(acl
, deny
, match
);
2739 monitor_printf(mon
, "acl: unable to add acl entry\n");
2741 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2745 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2747 const char *aclname
= qdict_get_str(qdict
, "aclname");
2748 const char *match
= qdict_get_str(qdict
, "match");
2749 qemu_acl
*acl
= find_acl(mon
, aclname
);
2753 ret
= qemu_acl_remove(acl
, match
);
2755 monitor_printf(mon
, "acl: no matching acl entry\n");
2757 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2761 #if defined(TARGET_I386)
2762 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2765 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2766 int bank
= qdict_get_int(qdict
, "bank");
2767 uint64_t status
= qdict_get_int(qdict
, "status");
2768 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2769 uint64_t addr
= qdict_get_int(qdict
, "addr");
2770 uint64_t misc
= qdict_get_int(qdict
, "misc");
2771 int flags
= MCE_INJECT_UNCOND_AO
;
2773 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2774 flags
|= MCE_INJECT_BROADCAST
;
2776 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
2777 if (cenv
->cpu_index
== cpu_index
) {
2778 cpu_x86_inject_mce(mon
, cenv
, bank
, status
, mcg_status
, addr
, misc
,
2786 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2788 const char *fdname
= qdict_get_str(qdict
, "fdname");
2792 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2794 qerror_report(QERR_FD_NOT_SUPPLIED
);
2798 if (qemu_isdigit(fdname
[0])) {
2799 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2800 "a name not starting with a digit");
2804 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2805 if (strcmp(monfd
->name
, fdname
) != 0) {
2814 monfd
= g_malloc0(sizeof(mon_fd_t
));
2815 monfd
->name
= g_strdup(fdname
);
2818 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2822 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2824 const char *fdname
= qdict_get_str(qdict
, "fdname");
2827 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2828 if (strcmp(monfd
->name
, fdname
) != 0) {
2832 QLIST_REMOVE(monfd
, next
);
2834 g_free(monfd
->name
);
2839 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2843 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2845 int saved_vm_running
= runstate_is_running();
2846 const char *name
= qdict_get_str(qdict
, "name");
2848 vm_stop(RSTATE_RESTORE
);
2850 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2855 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2859 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2862 if (strcmp(monfd
->name
, fdname
) != 0) {
2868 /* caller takes ownership of fd */
2869 QLIST_REMOVE(monfd
, next
);
2870 g_free(monfd
->name
);
2879 static const mon_cmd_t mon_cmds
[] = {
2880 #include "hmp-commands.h"
2884 /* Please update hmp-commands.hx when adding or changing commands */
2885 static const mon_cmd_t info_cmds
[] = {
2890 .help
= "show the version of QEMU",
2891 .user_print
= do_info_version_print
,
2892 .mhandler
.info_new
= do_info_version
,
2898 .help
= "show the network state",
2899 .mhandler
.info
= do_info_network
,
2905 .help
= "show the character devices",
2906 .user_print
= qemu_chr_info_print
,
2907 .mhandler
.info_new
= qemu_chr_info
,
2913 .help
= "show the block devices",
2914 .user_print
= bdrv_info_print
,
2915 .mhandler
.info_new
= bdrv_info
,
2918 .name
= "blockstats",
2921 .help
= "show block device statistics",
2922 .user_print
= bdrv_stats_print
,
2923 .mhandler
.info_new
= bdrv_info_stats
,
2926 .name
= "registers",
2929 .help
= "show the cpu registers",
2930 .mhandler
.info
= do_info_registers
,
2936 .help
= "show infos for each CPU",
2937 .user_print
= monitor_print_cpus
,
2938 .mhandler
.info_new
= do_info_cpus
,
2944 .help
= "show the command line history",
2945 .mhandler
.info
= do_info_history
,
2951 .help
= "show the interrupts statistics (if available)",
2952 .mhandler
.info
= irq_info
,
2958 .help
= "show i8259 (PIC) state",
2959 .mhandler
.info
= pic_info
,
2965 .help
= "show PCI info",
2966 .user_print
= do_pci_info_print
,
2967 .mhandler
.info_new
= do_pci_info
,
2969 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
2974 .help
= "show virtual to physical memory mappings",
2975 .mhandler
.info
= tlb_info
,
2978 #if defined(TARGET_I386)
2983 .help
= "show the active virtual memory mappings",
2984 .mhandler
.info
= mem_info
,
2991 .help
= "show memory tree",
2992 .mhandler
.info
= do_info_mtree
,
2998 .help
= "show dynamic compiler info",
2999 .mhandler
.info
= do_info_jit
,
3005 .help
= "show KVM information",
3006 .user_print
= do_info_kvm_print
,
3007 .mhandler
.info_new
= do_info_kvm
,
3013 .help
= "show NUMA information",
3014 .mhandler
.info
= do_info_numa
,
3020 .help
= "show guest USB devices",
3021 .mhandler
.info
= usb_info
,
3027 .help
= "show host USB devices",
3028 .mhandler
.info
= usb_host_info
,
3034 .help
= "show profiling information",
3035 .mhandler
.info
= do_info_profile
,
3041 .help
= "show capture information",
3042 .mhandler
.info
= do_info_capture
,
3045 .name
= "snapshots",
3048 .help
= "show the currently saved VM snapshots",
3049 .mhandler
.info
= do_info_snapshots
,
3055 .help
= "show the current VM status (running|paused)",
3056 .user_print
= do_info_status_print
,
3057 .mhandler
.info_new
= do_info_status
,
3063 .help
= "show guest PCMCIA status",
3064 .mhandler
.info
= pcmcia_info
,
3070 .help
= "show which guest mouse is receiving events",
3071 .user_print
= do_info_mice_print
,
3072 .mhandler
.info_new
= do_info_mice
,
3078 .help
= "show the vnc server status",
3079 .user_print
= do_info_vnc_print
,
3080 .mhandler
.info_new
= do_info_vnc
,
3082 #if defined(CONFIG_SPICE)
3087 .help
= "show the spice server status",
3088 .user_print
= do_info_spice_print
,
3089 .mhandler
.info_new
= do_info_spice
,
3096 .help
= "show the current VM name",
3097 .user_print
= do_info_name_print
,
3098 .mhandler
.info_new
= do_info_name
,
3104 .help
= "show the current VM UUID",
3105 .user_print
= do_info_uuid_print
,
3106 .mhandler
.info_new
= do_info_uuid
,
3108 #if defined(TARGET_PPC)
3113 .help
= "show CPU statistics",
3114 .mhandler
.info
= do_info_cpu_stats
,
3117 #if defined(CONFIG_SLIRP)
3122 .help
= "show user network stack connection states",
3123 .mhandler
.info
= do_info_usernet
,
3130 .help
= "show migration status",
3131 .user_print
= do_info_migrate_print
,
3132 .mhandler
.info_new
= do_info_migrate
,
3138 .help
= "show balloon information",
3139 .user_print
= monitor_print_balloon
,
3140 .mhandler
.info_async
= do_info_balloon
,
3141 .flags
= MONITOR_CMD_ASYNC
,
3147 .help
= "show device tree",
3148 .mhandler
.info
= do_info_qtree
,
3154 .help
= "show qdev device model list",
3155 .mhandler
.info
= do_info_qdm
,
3161 .help
= "show roms",
3162 .mhandler
.info
= do_info_roms
,
3164 #if defined(CONFIG_TRACE_SIMPLE)
3169 .help
= "show current contents of trace buffer",
3170 .mhandler
.info
= do_info_trace
,
3174 .name
= "trace-events",
3177 .help
= "show available trace-events & their state",
3178 .mhandler
.info
= do_trace_print_events
,
3185 static const mon_cmd_t qmp_cmds
[] = {
3186 #include "qmp-commands-old.h"
3190 static const mon_cmd_t qmp_query_cmds
[] = {
3195 .help
= "show the version of QEMU",
3196 .user_print
= do_info_version_print
,
3197 .mhandler
.info_new
= do_info_version
,
3203 .help
= "list QMP available commands",
3204 .user_print
= monitor_user_noop
,
3205 .mhandler
.info_new
= do_info_commands
,
3211 .help
= "show the character devices",
3212 .user_print
= qemu_chr_info_print
,
3213 .mhandler
.info_new
= qemu_chr_info
,
3219 .help
= "show the block devices",
3220 .user_print
= bdrv_info_print
,
3221 .mhandler
.info_new
= bdrv_info
,
3224 .name
= "blockstats",
3227 .help
= "show block device statistics",
3228 .user_print
= bdrv_stats_print
,
3229 .mhandler
.info_new
= bdrv_info_stats
,
3235 .help
= "show infos for each CPU",
3236 .user_print
= monitor_print_cpus
,
3237 .mhandler
.info_new
= do_info_cpus
,
3243 .help
= "show PCI info",
3244 .user_print
= do_pci_info_print
,
3245 .mhandler
.info_new
= do_pci_info
,
3251 .help
= "show KVM information",
3252 .user_print
= do_info_kvm_print
,
3253 .mhandler
.info_new
= do_info_kvm
,
3259 .help
= "show the current VM status (running|paused)",
3260 .user_print
= do_info_status_print
,
3261 .mhandler
.info_new
= do_info_status
,
3267 .help
= "show which guest mouse is receiving events",
3268 .user_print
= do_info_mice_print
,
3269 .mhandler
.info_new
= do_info_mice
,
3275 .help
= "show the vnc server status",
3276 .user_print
= do_info_vnc_print
,
3277 .mhandler
.info_new
= do_info_vnc
,
3279 #if defined(CONFIG_SPICE)
3284 .help
= "show the spice server status",
3285 .user_print
= do_info_spice_print
,
3286 .mhandler
.info_new
= do_info_spice
,
3293 .help
= "show the current VM name",
3294 .user_print
= do_info_name_print
,
3295 .mhandler
.info_new
= do_info_name
,
3301 .help
= "show the current VM UUID",
3302 .user_print
= do_info_uuid_print
,
3303 .mhandler
.info_new
= do_info_uuid
,
3309 .help
= "show migration status",
3310 .user_print
= do_info_migrate_print
,
3311 .mhandler
.info_new
= do_info_migrate
,
3317 .help
= "show balloon information",
3318 .user_print
= monitor_print_balloon
,
3319 .mhandler
.info_async
= do_info_balloon
,
3320 .flags
= MONITOR_CMD_ASYNC
,
3325 /*******************************************************************/
3327 static const char *pch
;
3328 static jmp_buf expr_env
;
3333 typedef struct MonitorDef
{
3336 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
3340 #if defined(TARGET_I386)
3341 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
3343 CPUState
*env
= mon_get_cpu();
3344 return env
->eip
+ env
->segs
[R_CS
].base
;
3348 #if defined(TARGET_PPC)
3349 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3351 CPUState
*env
= mon_get_cpu();
3356 for (i
= 0; i
< 8; i
++)
3357 u
|= env
->crf
[i
] << (32 - (4 * i
));
3362 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3364 CPUState
*env
= mon_get_cpu();
3368 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3370 CPUState
*env
= mon_get_cpu();
3374 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3376 CPUState
*env
= mon_get_cpu();
3377 return cpu_ppc_load_decr(env
);
3380 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3382 CPUState
*env
= mon_get_cpu();
3383 return cpu_ppc_load_tbu(env
);
3386 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3388 CPUState
*env
= mon_get_cpu();
3389 return cpu_ppc_load_tbl(env
);
3393 #if defined(TARGET_SPARC)
3394 #ifndef TARGET_SPARC64
3395 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3397 CPUState
*env
= mon_get_cpu();
3399 return cpu_get_psr(env
);
3403 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3405 CPUState
*env
= mon_get_cpu();
3406 return env
->regwptr
[val
];
3410 static const MonitorDef monitor_defs
[] = {
3413 #define SEG(name, seg) \
3414 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3415 { name ".base", offsetof(CPUState, segs[seg].base) },\
3416 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3418 { "eax", offsetof(CPUState
, regs
[0]) },
3419 { "ecx", offsetof(CPUState
, regs
[1]) },
3420 { "edx", offsetof(CPUState
, regs
[2]) },
3421 { "ebx", offsetof(CPUState
, regs
[3]) },
3422 { "esp|sp", offsetof(CPUState
, regs
[4]) },
3423 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
3424 { "esi", offsetof(CPUState
, regs
[6]) },
3425 { "edi", offsetof(CPUState
, regs
[7]) },
3426 #ifdef TARGET_X86_64
3427 { "r8", offsetof(CPUState
, regs
[8]) },
3428 { "r9", offsetof(CPUState
, regs
[9]) },
3429 { "r10", offsetof(CPUState
, regs
[10]) },
3430 { "r11", offsetof(CPUState
, regs
[11]) },
3431 { "r12", offsetof(CPUState
, regs
[12]) },
3432 { "r13", offsetof(CPUState
, regs
[13]) },
3433 { "r14", offsetof(CPUState
, regs
[14]) },
3434 { "r15", offsetof(CPUState
, regs
[15]) },
3436 { "eflags", offsetof(CPUState
, eflags
) },
3437 { "eip", offsetof(CPUState
, eip
) },
3444 { "pc", 0, monitor_get_pc
, },
3445 #elif defined(TARGET_PPC)
3446 /* General purpose registers */
3447 { "r0", offsetof(CPUState
, gpr
[0]) },
3448 { "r1", offsetof(CPUState
, gpr
[1]) },
3449 { "r2", offsetof(CPUState
, gpr
[2]) },
3450 { "r3", offsetof(CPUState
, gpr
[3]) },
3451 { "r4", offsetof(CPUState
, gpr
[4]) },
3452 { "r5", offsetof(CPUState
, gpr
[5]) },
3453 { "r6", offsetof(CPUState
, gpr
[6]) },
3454 { "r7", offsetof(CPUState
, gpr
[7]) },
3455 { "r8", offsetof(CPUState
, gpr
[8]) },
3456 { "r9", offsetof(CPUState
, gpr
[9]) },
3457 { "r10", offsetof(CPUState
, gpr
[10]) },
3458 { "r11", offsetof(CPUState
, gpr
[11]) },
3459 { "r12", offsetof(CPUState
, gpr
[12]) },
3460 { "r13", offsetof(CPUState
, gpr
[13]) },
3461 { "r14", offsetof(CPUState
, gpr
[14]) },
3462 { "r15", offsetof(CPUState
, gpr
[15]) },
3463 { "r16", offsetof(CPUState
, gpr
[16]) },
3464 { "r17", offsetof(CPUState
, gpr
[17]) },
3465 { "r18", offsetof(CPUState
, gpr
[18]) },
3466 { "r19", offsetof(CPUState
, gpr
[19]) },
3467 { "r20", offsetof(CPUState
, gpr
[20]) },
3468 { "r21", offsetof(CPUState
, gpr
[21]) },
3469 { "r22", offsetof(CPUState
, gpr
[22]) },
3470 { "r23", offsetof(CPUState
, gpr
[23]) },
3471 { "r24", offsetof(CPUState
, gpr
[24]) },
3472 { "r25", offsetof(CPUState
, gpr
[25]) },
3473 { "r26", offsetof(CPUState
, gpr
[26]) },
3474 { "r27", offsetof(CPUState
, gpr
[27]) },
3475 { "r28", offsetof(CPUState
, gpr
[28]) },
3476 { "r29", offsetof(CPUState
, gpr
[29]) },
3477 { "r30", offsetof(CPUState
, gpr
[30]) },
3478 { "r31", offsetof(CPUState
, gpr
[31]) },
3479 /* Floating point registers */
3480 { "f0", offsetof(CPUState
, fpr
[0]) },
3481 { "f1", offsetof(CPUState
, fpr
[1]) },
3482 { "f2", offsetof(CPUState
, fpr
[2]) },
3483 { "f3", offsetof(CPUState
, fpr
[3]) },
3484 { "f4", offsetof(CPUState
, fpr
[4]) },
3485 { "f5", offsetof(CPUState
, fpr
[5]) },
3486 { "f6", offsetof(CPUState
, fpr
[6]) },
3487 { "f7", offsetof(CPUState
, fpr
[7]) },
3488 { "f8", offsetof(CPUState
, fpr
[8]) },
3489 { "f9", offsetof(CPUState
, fpr
[9]) },
3490 { "f10", offsetof(CPUState
, fpr
[10]) },
3491 { "f11", offsetof(CPUState
, fpr
[11]) },
3492 { "f12", offsetof(CPUState
, fpr
[12]) },
3493 { "f13", offsetof(CPUState
, fpr
[13]) },
3494 { "f14", offsetof(CPUState
, fpr
[14]) },
3495 { "f15", offsetof(CPUState
, fpr
[15]) },
3496 { "f16", offsetof(CPUState
, fpr
[16]) },
3497 { "f17", offsetof(CPUState
, fpr
[17]) },
3498 { "f18", offsetof(CPUState
, fpr
[18]) },
3499 { "f19", offsetof(CPUState
, fpr
[19]) },
3500 { "f20", offsetof(CPUState
, fpr
[20]) },
3501 { "f21", offsetof(CPUState
, fpr
[21]) },
3502 { "f22", offsetof(CPUState
, fpr
[22]) },
3503 { "f23", offsetof(CPUState
, fpr
[23]) },
3504 { "f24", offsetof(CPUState
, fpr
[24]) },
3505 { "f25", offsetof(CPUState
, fpr
[25]) },
3506 { "f26", offsetof(CPUState
, fpr
[26]) },
3507 { "f27", offsetof(CPUState
, fpr
[27]) },
3508 { "f28", offsetof(CPUState
, fpr
[28]) },
3509 { "f29", offsetof(CPUState
, fpr
[29]) },
3510 { "f30", offsetof(CPUState
, fpr
[30]) },
3511 { "f31", offsetof(CPUState
, fpr
[31]) },
3512 { "fpscr", offsetof(CPUState
, fpscr
) },
3513 /* Next instruction pointer */
3514 { "nip|pc", offsetof(CPUState
, nip
) },
3515 { "lr", offsetof(CPUState
, lr
) },
3516 { "ctr", offsetof(CPUState
, ctr
) },
3517 { "decr", 0, &monitor_get_decr
, },
3518 { "ccr", 0, &monitor_get_ccr
, },
3519 /* Machine state register */
3520 { "msr", 0, &monitor_get_msr
, },
3521 { "xer", 0, &monitor_get_xer
, },
3522 { "tbu", 0, &monitor_get_tbu
, },
3523 { "tbl", 0, &monitor_get_tbl
, },
3524 #if defined(TARGET_PPC64)
3525 /* Address space register */
3526 { "asr", offsetof(CPUState
, asr
) },
3528 /* Segment registers */
3529 { "sdr1", offsetof(CPUState
, spr
[SPR_SDR1
]) },
3530 { "sr0", offsetof(CPUState
, sr
[0]) },
3531 { "sr1", offsetof(CPUState
, sr
[1]) },
3532 { "sr2", offsetof(CPUState
, sr
[2]) },
3533 { "sr3", offsetof(CPUState
, sr
[3]) },
3534 { "sr4", offsetof(CPUState
, sr
[4]) },
3535 { "sr5", offsetof(CPUState
, sr
[5]) },
3536 { "sr6", offsetof(CPUState
, sr
[6]) },
3537 { "sr7", offsetof(CPUState
, sr
[7]) },
3538 { "sr8", offsetof(CPUState
, sr
[8]) },
3539 { "sr9", offsetof(CPUState
, sr
[9]) },
3540 { "sr10", offsetof(CPUState
, sr
[10]) },
3541 { "sr11", offsetof(CPUState
, sr
[11]) },
3542 { "sr12", offsetof(CPUState
, sr
[12]) },
3543 { "sr13", offsetof(CPUState
, sr
[13]) },
3544 { "sr14", offsetof(CPUState
, sr
[14]) },
3545 { "sr15", offsetof(CPUState
, sr
[15]) },
3546 /* Too lazy to put BATs... */
3547 { "pvr", offsetof(CPUState
, spr
[SPR_PVR
]) },
3549 { "srr0", offsetof(CPUState
, spr
[SPR_SRR0
]) },
3550 { "srr1", offsetof(CPUState
, spr
[SPR_SRR1
]) },
3551 { "sprg0", offsetof(CPUState
, spr
[SPR_SPRG0
]) },
3552 { "sprg1", offsetof(CPUState
, spr
[SPR_SPRG1
]) },
3553 { "sprg2", offsetof(CPUState
, spr
[SPR_SPRG2
]) },
3554 { "sprg3", offsetof(CPUState
, spr
[SPR_SPRG3
]) },
3555 { "sprg4", offsetof(CPUState
, spr
[SPR_SPRG4
]) },
3556 { "sprg5", offsetof(CPUState
, spr
[SPR_SPRG5
]) },
3557 { "sprg6", offsetof(CPUState
, spr
[SPR_SPRG6
]) },
3558 { "sprg7", offsetof(CPUState
, spr
[SPR_SPRG7
]) },
3559 { "pid", offsetof(CPUState
, spr
[SPR_BOOKE_PID
]) },
3560 { "csrr0", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR0
]) },
3561 { "csrr1", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR1
]) },
3562 { "esr", offsetof(CPUState
, spr
[SPR_BOOKE_ESR
]) },
3563 { "dear", offsetof(CPUState
, spr
[SPR_BOOKE_DEAR
]) },
3564 { "mcsr", offsetof(CPUState
, spr
[SPR_BOOKE_MCSR
]) },
3565 { "tsr", offsetof(CPUState
, spr
[SPR_BOOKE_TSR
]) },
3566 { "tcr", offsetof(CPUState
, spr
[SPR_BOOKE_TCR
]) },
3567 { "vrsave", offsetof(CPUState
, spr
[SPR_VRSAVE
]) },
3568 { "pir", offsetof(CPUState
, spr
[SPR_BOOKE_PIR
]) },
3569 { "mcsrr0", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR0
]) },
3570 { "mcsrr1", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR1
]) },
3571 { "decar", offsetof(CPUState
, spr
[SPR_BOOKE_DECAR
]) },
3572 { "ivpr", offsetof(CPUState
, spr
[SPR_BOOKE_IVPR
]) },
3573 { "epcr", offsetof(CPUState
, spr
[SPR_BOOKE_EPCR
]) },
3574 { "sprg8", offsetof(CPUState
, spr
[SPR_BOOKE_SPRG8
]) },
3575 { "ivor0", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR0
]) },
3576 { "ivor1", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR1
]) },
3577 { "ivor2", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR2
]) },
3578 { "ivor3", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR3
]) },
3579 { "ivor4", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR4
]) },
3580 { "ivor5", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR5
]) },
3581 { "ivor6", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR6
]) },
3582 { "ivor7", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR7
]) },
3583 { "ivor8", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR8
]) },
3584 { "ivor9", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR9
]) },
3585 { "ivor10", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR10
]) },
3586 { "ivor11", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR11
]) },
3587 { "ivor12", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR12
]) },
3588 { "ivor13", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR13
]) },
3589 { "ivor14", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR14
]) },
3590 { "ivor15", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR15
]) },
3591 { "ivor32", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR32
]) },
3592 { "ivor33", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR33
]) },
3593 { "ivor34", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR34
]) },
3594 { "ivor35", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR35
]) },
3595 { "ivor36", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR36
]) },
3596 { "ivor37", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR37
]) },
3597 { "mas0", offsetof(CPUState
, spr
[SPR_BOOKE_MAS0
]) },
3598 { "mas1", offsetof(CPUState
, spr
[SPR_BOOKE_MAS1
]) },
3599 { "mas2", offsetof(CPUState
, spr
[SPR_BOOKE_MAS2
]) },
3600 { "mas3", offsetof(CPUState
, spr
[SPR_BOOKE_MAS3
]) },
3601 { "mas4", offsetof(CPUState
, spr
[SPR_BOOKE_MAS4
]) },
3602 { "mas6", offsetof(CPUState
, spr
[SPR_BOOKE_MAS6
]) },
3603 { "mas7", offsetof(CPUState
, spr
[SPR_BOOKE_MAS7
]) },
3604 { "mmucfg", offsetof(CPUState
, spr
[SPR_MMUCFG
]) },
3605 { "tlb0cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3606 { "tlb1cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3607 { "epr", offsetof(CPUState
, spr
[SPR_BOOKE_EPR
]) },
3608 { "eplc", offsetof(CPUState
, spr
[SPR_BOOKE_EPLC
]) },
3609 { "epsc", offsetof(CPUState
, spr
[SPR_BOOKE_EPSC
]) },
3610 { "svr", offsetof(CPUState
, spr
[SPR_E500_SVR
]) },
3611 { "mcar", offsetof(CPUState
, spr
[SPR_Exxx_MCAR
]) },
3612 { "pid1", offsetof(CPUState
, spr
[SPR_BOOKE_PID1
]) },
3613 { "pid2", offsetof(CPUState
, spr
[SPR_BOOKE_PID2
]) },
3614 { "hid0", offsetof(CPUState
, spr
[SPR_HID0
]) },
3616 #elif defined(TARGET_SPARC)
3617 { "g0", offsetof(CPUState
, gregs
[0]) },
3618 { "g1", offsetof(CPUState
, gregs
[1]) },
3619 { "g2", offsetof(CPUState
, gregs
[2]) },
3620 { "g3", offsetof(CPUState
, gregs
[3]) },
3621 { "g4", offsetof(CPUState
, gregs
[4]) },
3622 { "g5", offsetof(CPUState
, gregs
[5]) },
3623 { "g6", offsetof(CPUState
, gregs
[6]) },
3624 { "g7", offsetof(CPUState
, gregs
[7]) },
3625 { "o0", 0, monitor_get_reg
},
3626 { "o1", 1, monitor_get_reg
},
3627 { "o2", 2, monitor_get_reg
},
3628 { "o3", 3, monitor_get_reg
},
3629 { "o4", 4, monitor_get_reg
},
3630 { "o5", 5, monitor_get_reg
},
3631 { "o6", 6, monitor_get_reg
},
3632 { "o7", 7, monitor_get_reg
},
3633 { "l0", 8, monitor_get_reg
},
3634 { "l1", 9, monitor_get_reg
},
3635 { "l2", 10, monitor_get_reg
},
3636 { "l3", 11, monitor_get_reg
},
3637 { "l4", 12, monitor_get_reg
},
3638 { "l5", 13, monitor_get_reg
},
3639 { "l6", 14, monitor_get_reg
},
3640 { "l7", 15, monitor_get_reg
},
3641 { "i0", 16, monitor_get_reg
},
3642 { "i1", 17, monitor_get_reg
},
3643 { "i2", 18, monitor_get_reg
},
3644 { "i3", 19, monitor_get_reg
},
3645 { "i4", 20, monitor_get_reg
},
3646 { "i5", 21, monitor_get_reg
},
3647 { "i6", 22, monitor_get_reg
},
3648 { "i7", 23, monitor_get_reg
},
3649 { "pc", offsetof(CPUState
, pc
) },
3650 { "npc", offsetof(CPUState
, npc
) },
3651 { "y", offsetof(CPUState
, y
) },
3652 #ifndef TARGET_SPARC64
3653 { "psr", 0, &monitor_get_psr
, },
3654 { "wim", offsetof(CPUState
, wim
) },
3656 { "tbr", offsetof(CPUState
, tbr
) },
3657 { "fsr", offsetof(CPUState
, fsr
) },
3658 { "f0", offsetof(CPUState
, fpr
[0]) },
3659 { "f1", offsetof(CPUState
, fpr
[1]) },
3660 { "f2", offsetof(CPUState
, fpr
[2]) },
3661 { "f3", offsetof(CPUState
, fpr
[3]) },
3662 { "f4", offsetof(CPUState
, fpr
[4]) },
3663 { "f5", offsetof(CPUState
, fpr
[5]) },
3664 { "f6", offsetof(CPUState
, fpr
[6]) },
3665 { "f7", offsetof(CPUState
, fpr
[7]) },
3666 { "f8", offsetof(CPUState
, fpr
[8]) },
3667 { "f9", offsetof(CPUState
, fpr
[9]) },
3668 { "f10", offsetof(CPUState
, fpr
[10]) },
3669 { "f11", offsetof(CPUState
, fpr
[11]) },
3670 { "f12", offsetof(CPUState
, fpr
[12]) },
3671 { "f13", offsetof(CPUState
, fpr
[13]) },
3672 { "f14", offsetof(CPUState
, fpr
[14]) },
3673 { "f15", offsetof(CPUState
, fpr
[15]) },
3674 { "f16", offsetof(CPUState
, fpr
[16]) },
3675 { "f17", offsetof(CPUState
, fpr
[17]) },
3676 { "f18", offsetof(CPUState
, fpr
[18]) },
3677 { "f19", offsetof(CPUState
, fpr
[19]) },
3678 { "f20", offsetof(CPUState
, fpr
[20]) },
3679 { "f21", offsetof(CPUState
, fpr
[21]) },
3680 { "f22", offsetof(CPUState
, fpr
[22]) },
3681 { "f23", offsetof(CPUState
, fpr
[23]) },
3682 { "f24", offsetof(CPUState
, fpr
[24]) },
3683 { "f25", offsetof(CPUState
, fpr
[25]) },
3684 { "f26", offsetof(CPUState
, fpr
[26]) },
3685 { "f27", offsetof(CPUState
, fpr
[27]) },
3686 { "f28", offsetof(CPUState
, fpr
[28]) },
3687 { "f29", offsetof(CPUState
, fpr
[29]) },
3688 { "f30", offsetof(CPUState
, fpr
[30]) },
3689 { "f31", offsetof(CPUState
, fpr
[31]) },
3690 #ifdef TARGET_SPARC64
3691 { "f32", offsetof(CPUState
, fpr
[32]) },
3692 { "f34", offsetof(CPUState
, fpr
[34]) },
3693 { "f36", offsetof(CPUState
, fpr
[36]) },
3694 { "f38", offsetof(CPUState
, fpr
[38]) },
3695 { "f40", offsetof(CPUState
, fpr
[40]) },
3696 { "f42", offsetof(CPUState
, fpr
[42]) },
3697 { "f44", offsetof(CPUState
, fpr
[44]) },
3698 { "f46", offsetof(CPUState
, fpr
[46]) },
3699 { "f48", offsetof(CPUState
, fpr
[48]) },
3700 { "f50", offsetof(CPUState
, fpr
[50]) },
3701 { "f52", offsetof(CPUState
, fpr
[52]) },
3702 { "f54", offsetof(CPUState
, fpr
[54]) },
3703 { "f56", offsetof(CPUState
, fpr
[56]) },
3704 { "f58", offsetof(CPUState
, fpr
[58]) },
3705 { "f60", offsetof(CPUState
, fpr
[60]) },
3706 { "f62", offsetof(CPUState
, fpr
[62]) },
3707 { "asi", offsetof(CPUState
, asi
) },
3708 { "pstate", offsetof(CPUState
, pstate
) },
3709 { "cansave", offsetof(CPUState
, cansave
) },
3710 { "canrestore", offsetof(CPUState
, canrestore
) },
3711 { "otherwin", offsetof(CPUState
, otherwin
) },
3712 { "wstate", offsetof(CPUState
, wstate
) },
3713 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3714 { "fprs", offsetof(CPUState
, fprs
) },
3720 static void expr_error(Monitor
*mon
, const char *msg
)
3722 monitor_printf(mon
, "%s\n", msg
);
3723 longjmp(expr_env
, 1);
3726 /* return 0 if OK, -1 if not found */
3727 static int get_monitor_def(target_long
*pval
, const char *name
)
3729 const MonitorDef
*md
;
3732 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3733 if (compare_cmd(name
, md
->name
)) {
3734 if (md
->get_value
) {
3735 *pval
= md
->get_value(md
, md
->offset
);
3737 CPUState
*env
= mon_get_cpu();
3738 ptr
= (uint8_t *)env
+ md
->offset
;
3741 *pval
= *(int32_t *)ptr
;
3744 *pval
= *(target_long
*)ptr
;
3757 static void next(void)
3761 while (qemu_isspace(*pch
))
3766 static int64_t expr_sum(Monitor
*mon
);
3768 static int64_t expr_unary(Monitor
*mon
)
3777 n
= expr_unary(mon
);
3781 n
= -expr_unary(mon
);
3785 n
= ~expr_unary(mon
);
3791 expr_error(mon
, "')' expected");
3798 expr_error(mon
, "character constant expected");
3802 expr_error(mon
, "missing terminating \' character");
3812 while ((*pch
>= 'a' && *pch
<= 'z') ||
3813 (*pch
>= 'A' && *pch
<= 'Z') ||
3814 (*pch
>= '0' && *pch
<= '9') ||
3815 *pch
== '_' || *pch
== '.') {
3816 if ((q
- buf
) < sizeof(buf
) - 1)
3820 while (qemu_isspace(*pch
))
3823 ret
= get_monitor_def(®
, buf
);
3825 expr_error(mon
, "unknown register");
3830 expr_error(mon
, "unexpected end of expression");
3834 #if TARGET_PHYS_ADDR_BITS > 32
3835 n
= strtoull(pch
, &p
, 0);
3837 n
= strtoul(pch
, &p
, 0);
3840 expr_error(mon
, "invalid char in expression");
3843 while (qemu_isspace(*pch
))
3851 static int64_t expr_prod(Monitor
*mon
)
3856 val
= expr_unary(mon
);
3859 if (op
!= '*' && op
!= '/' && op
!= '%')
3862 val2
= expr_unary(mon
);
3871 expr_error(mon
, "division by zero");
3882 static int64_t expr_logic(Monitor
*mon
)
3887 val
= expr_prod(mon
);
3890 if (op
!= '&' && op
!= '|' && op
!= '^')
3893 val2
= expr_prod(mon
);
3910 static int64_t expr_sum(Monitor
*mon
)
3915 val
= expr_logic(mon
);
3918 if (op
!= '+' && op
!= '-')
3921 val2
= expr_logic(mon
);
3930 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3933 if (setjmp(expr_env
)) {
3937 while (qemu_isspace(*pch
))
3939 *pval
= expr_sum(mon
);
3944 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3946 const char *p
= *pp
;
3950 d
= strtod(p
, &tailp
);
3952 monitor_printf(mon
, "Number expected\n");
3955 if (d
!= d
|| d
- d
!= 0) {
3956 /* NaN or infinity */
3957 monitor_printf(mon
, "Bad number\n");
3965 static int get_str(char *buf
, int buf_size
, const char **pp
)
3973 while (qemu_isspace(*p
))
3983 while (*p
!= '\0' && *p
!= '\"') {
3999 qemu_printf("unsupported escape code: '\\%c'\n", c
);
4002 if ((q
- buf
) < buf_size
- 1) {
4006 if ((q
- buf
) < buf_size
- 1) {
4013 qemu_printf("unterminated string\n");
4018 while (*p
!= '\0' && !qemu_isspace(*p
)) {
4019 if ((q
- buf
) < buf_size
- 1) {
4031 * Store the command-name in cmdname, and return a pointer to
4032 * the remaining of the command string.
4034 static const char *get_command_name(const char *cmdline
,
4035 char *cmdname
, size_t nlen
)
4038 const char *p
, *pstart
;
4041 while (qemu_isspace(*p
))
4046 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
4051 memcpy(cmdname
, pstart
, len
);
4052 cmdname
[len
] = '\0';
4057 * Read key of 'type' into 'key' and return the current
4060 static char *key_get_info(const char *type
, char **key
)
4068 p
= strchr(type
, ':');
4075 str
= g_malloc(len
+ 1);
4076 memcpy(str
, type
, len
);
4083 static int default_fmt_format
= 'x';
4084 static int default_fmt_size
= 4;
4088 static int is_valid_option(const char *c
, const char *typestr
)
4096 typestr
= strstr(typestr
, option
);
4097 return (typestr
!= NULL
);
4100 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
4101 const char *cmdname
)
4103 const mon_cmd_t
*cmd
;
4105 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
4106 if (compare_cmd(cmdname
, cmd
->name
)) {
4114 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
4116 return search_dispatch_table(mon_cmds
, cmdname
);
4119 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
4121 return search_dispatch_table(qmp_query_cmds
, info_item
);
4124 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
4126 return search_dispatch_table(qmp_cmds
, cmdname
);
4129 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
4130 const char *cmdline
,
4133 const char *p
, *typestr
;
4135 const mon_cmd_t
*cmd
;
4141 monitor_printf(mon
, "command='%s'\n", cmdline
);
4144 /* extract the command name */
4145 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
4149 cmd
= monitor_find_command(cmdname
);
4151 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
4155 /* parse the parameters */
4156 typestr
= cmd
->args_type
;
4158 typestr
= key_get_info(typestr
, &key
);
4170 while (qemu_isspace(*p
))
4172 if (*typestr
== '?') {
4175 /* no optional string: NULL argument */
4179 ret
= get_str(buf
, sizeof(buf
), &p
);
4183 monitor_printf(mon
, "%s: filename expected\n",
4187 monitor_printf(mon
, "%s: block device name expected\n",
4191 monitor_printf(mon
, "%s: string expected\n", cmdname
);
4196 qdict_put(qdict
, key
, qstring_from_str(buf
));
4201 QemuOptsList
*opts_list
;
4204 opts_list
= qemu_find_opts(key
);
4205 if (!opts_list
|| opts_list
->desc
->name
) {
4208 while (qemu_isspace(*p
)) {
4213 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
4216 opts
= qemu_opts_parse(opts_list
, buf
, 1);
4220 qemu_opts_to_qdict(opts
, qdict
);
4221 qemu_opts_del(opts
);
4226 int count
, format
, size
;
4228 while (qemu_isspace(*p
))
4234 if (qemu_isdigit(*p
)) {
4236 while (qemu_isdigit(*p
)) {
4237 count
= count
* 10 + (*p
- '0');
4275 if (*p
!= '\0' && !qemu_isspace(*p
)) {
4276 monitor_printf(mon
, "invalid char in format: '%c'\n",
4281 format
= default_fmt_format
;
4282 if (format
!= 'i') {
4283 /* for 'i', not specifying a size gives -1 as size */
4285 size
= default_fmt_size
;
4286 default_fmt_size
= size
;
4288 default_fmt_format
= format
;
4291 format
= default_fmt_format
;
4292 if (format
!= 'i') {
4293 size
= default_fmt_size
;
4298 qdict_put(qdict
, "count", qint_from_int(count
));
4299 qdict_put(qdict
, "format", qint_from_int(format
));
4300 qdict_put(qdict
, "size", qint_from_int(size
));
4309 while (qemu_isspace(*p
))
4311 if (*typestr
== '?' || *typestr
== '.') {
4312 if (*typestr
== '?') {
4320 while (qemu_isspace(*p
))
4329 if (get_expr(mon
, &val
, &p
))
4331 /* Check if 'i' is greater than 32-bit */
4332 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
4333 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
4334 monitor_printf(mon
, "integer is for 32-bit values\n");
4336 } else if (c
== 'M') {
4339 qdict_put(qdict
, key
, qint_from_int(val
));
4347 while (qemu_isspace(*p
)) {
4350 if (*typestr
== '?') {
4356 val
= strtosz(p
, &end
);
4358 monitor_printf(mon
, "invalid size\n");
4361 qdict_put(qdict
, key
, qint_from_int(val
));
4369 while (qemu_isspace(*p
))
4371 if (*typestr
== '?') {
4377 if (get_double(mon
, &val
, &p
) < 0) {
4380 if (p
[0] && p
[1] == 's') {
4383 val
/= 1e3
; p
+= 2; break;
4385 val
/= 1e6
; p
+= 2; break;
4387 val
/= 1e9
; p
+= 2; break;
4390 if (*p
&& !qemu_isspace(*p
)) {
4391 monitor_printf(mon
, "Unknown unit suffix\n");
4394 qdict_put(qdict
, key
, qfloat_from_double(val
));
4402 while (qemu_isspace(*p
)) {
4406 while (qemu_isgraph(*p
)) {
4409 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4411 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4414 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4417 qdict_put(qdict
, key
, qbool_from_int(val
));
4422 const char *tmp
= p
;
4429 while (qemu_isspace(*p
))
4434 if(!is_valid_option(p
, typestr
)) {
4436 monitor_printf(mon
, "%s: unsupported option -%c\n",
4448 qdict_put(qdict
, key
, qbool_from_int(1));
4455 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4461 /* check that all arguments were parsed */
4462 while (qemu_isspace(*p
))
4465 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4477 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4479 /* report only the first error */
4481 mon
->error
= qerror
;
4483 MON_DEBUG("Additional error report at %s:%d\n",
4484 qerror
->file
, qerror
->linenr
);
4489 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4491 if (ret
&& !monitor_has_error(mon
)) {
4493 * If it returns failure, it must have passed on error.
4495 * Action: Report an internal error to the client if in QMP.
4497 qerror_report(QERR_UNDEFINED_ERROR
);
4498 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4502 #ifdef CONFIG_DEBUG_MONITOR
4503 if (!ret
&& monitor_has_error(mon
)) {
4505 * If it returns success, it must not have passed an error.
4507 * Action: Report the passed error to the client.
4509 MON_DEBUG("command '%s' returned success but passed an error\n",
4513 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
4515 * Handlers should not call Monitor print functions.
4517 * Action: Ignore them in QMP.
4519 * (XXX: we don't check any 'info' or 'query' command here
4520 * because the user print function _is_ called by do_info(), hence
4521 * we will trigger this check. This problem will go away when we
4522 * make 'query' commands real and kill do_info())
4524 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4525 cmd
->name
, mon_print_count_get(mon
));
4530 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4533 const mon_cmd_t
*cmd
;
4535 qdict
= qdict_new();
4537 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4541 if (handler_is_async(cmd
)) {
4542 user_async_cmd_handler(mon
, cmd
, qdict
);
4543 } else if (handler_is_qobject(cmd
)) {
4544 QObject
*data
= NULL
;
4546 /* XXX: ignores the error code */
4547 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4548 assert(!monitor_has_error(mon
));
4550 cmd
->user_print(mon
, data
);
4551 qobject_decref(data
);
4554 cmd
->mhandler
.cmd(mon
, qdict
);
4561 static void cmd_completion(const char *name
, const char *list
)
4563 const char *p
, *pstart
;
4572 p
= pstart
+ strlen(pstart
);
4574 if (len
> sizeof(cmd
) - 2)
4575 len
= sizeof(cmd
) - 2;
4576 memcpy(cmd
, pstart
, len
);
4578 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4579 readline_add_completion(cur_mon
->rs
, cmd
);
4587 static void file_completion(const char *input
)
4592 char file
[1024], file_prefix
[1024];
4596 p
= strrchr(input
, '/');
4599 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4600 pstrcpy(path
, sizeof(path
), ".");
4602 input_path_len
= p
- input
+ 1;
4603 memcpy(path
, input
, input_path_len
);
4604 if (input_path_len
> sizeof(path
) - 1)
4605 input_path_len
= sizeof(path
) - 1;
4606 path
[input_path_len
] = '\0';
4607 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4609 #ifdef DEBUG_COMPLETION
4610 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4611 input
, path
, file_prefix
);
4613 ffs
= opendir(path
);
4622 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4626 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4627 memcpy(file
, input
, input_path_len
);
4628 if (input_path_len
< sizeof(file
))
4629 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4631 /* stat the file to find out if it's a directory.
4632 * In that case add a slash to speed up typing long paths
4635 if(S_ISDIR(sb
.st_mode
))
4636 pstrcat(file
, sizeof(file
), "/");
4637 readline_add_completion(cur_mon
->rs
, file
);
4643 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4645 const char *name
= bdrv_get_device_name(bs
);
4646 const char *input
= opaque
;
4648 if (input
[0] == '\0' ||
4649 !strncmp(name
, (char *)input
, strlen(input
))) {
4650 readline_add_completion(cur_mon
->rs
, name
);
4654 /* NOTE: this parser is an approximate form of the real command parser */
4655 static void parse_cmdline(const char *cmdline
,
4656 int *pnb_args
, char **args
)
4665 while (qemu_isspace(*p
))
4669 if (nb_args
>= MAX_ARGS
)
4671 ret
= get_str(buf
, sizeof(buf
), &p
);
4672 args
[nb_args
] = g_strdup(buf
);
4677 *pnb_args
= nb_args
;
4680 static const char *next_arg_type(const char *typestr
)
4682 const char *p
= strchr(typestr
, ':');
4683 return (p
!= NULL
? ++p
: typestr
);
4686 static void monitor_find_completion(const char *cmdline
)
4688 const char *cmdname
;
4689 char *args
[MAX_ARGS
];
4690 int nb_args
, i
, len
;
4691 const char *ptype
, *str
;
4692 const mon_cmd_t
*cmd
;
4695 parse_cmdline(cmdline
, &nb_args
, args
);
4696 #ifdef DEBUG_COMPLETION
4697 for(i
= 0; i
< nb_args
; i
++) {
4698 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4702 /* if the line ends with a space, it means we want to complete the
4704 len
= strlen(cmdline
);
4705 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4706 if (nb_args
>= MAX_ARGS
) {
4709 args
[nb_args
++] = g_strdup("");
4712 /* command completion */
4717 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4718 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4719 cmd_completion(cmdname
, cmd
->name
);
4722 /* find the command */
4723 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4724 if (compare_cmd(args
[0], cmd
->name
)) {
4732 ptype
= next_arg_type(cmd
->args_type
);
4733 for(i
= 0; i
< nb_args
- 2; i
++) {
4734 if (*ptype
!= '\0') {
4735 ptype
= next_arg_type(ptype
);
4736 while (*ptype
== '?')
4737 ptype
= next_arg_type(ptype
);
4740 str
= args
[nb_args
- 1];
4741 if (*ptype
== '-' && ptype
[1] != '\0') {
4742 ptype
= next_arg_type(ptype
);
4746 /* file completion */
4747 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4748 file_completion(str
);
4751 /* block device name completion */
4752 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4753 bdrv_iterate(block_completion_it
, (void *)str
);
4756 /* XXX: more generic ? */
4757 if (!strcmp(cmd
->name
, "info")) {
4758 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4759 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4760 cmd_completion(str
, cmd
->name
);
4762 } else if (!strcmp(cmd
->name
, "sendkey")) {
4763 char *sep
= strrchr(str
, '-');
4766 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4767 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4768 cmd_completion(str
, key
->name
);
4770 } else if (!strcmp(cmd
->name
, "help|?")) {
4771 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4772 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4773 cmd_completion(str
, cmd
->name
);
4783 for (i
= 0; i
< nb_args
; i
++) {
4788 static int monitor_can_read(void *opaque
)
4790 Monitor
*mon
= opaque
;
4792 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4795 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4797 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4798 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4802 * Argument validation rules:
4804 * 1. The argument must exist in cmd_args qdict
4805 * 2. The argument type must be the expected one
4807 * Special case: If the argument doesn't exist in cmd_args and
4808 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4809 * checking is skipped for it.
4811 static int check_client_args_type(const QDict
*client_args
,
4812 const QDict
*cmd_args
, int flags
)
4814 const QDictEntry
*ent
;
4816 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4819 const QObject
*client_arg
= qdict_entry_value(ent
);
4820 const char *client_arg_name
= qdict_entry_key(ent
);
4822 obj
= qdict_get(cmd_args
, client_arg_name
);
4824 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4825 /* handler accepts unknowns */
4828 /* client arg doesn't exist */
4829 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4833 arg_type
= qobject_to_qstring(obj
);
4834 assert(arg_type
!= NULL
);
4836 /* check if argument's type is correct */
4837 switch (qstring_get_str(arg_type
)[0]) {
4841 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4842 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4851 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4852 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4858 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4859 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4860 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4867 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4868 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4874 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4879 * These types are not supported by QMP and thus are not
4880 * handled here. Fall through.
4891 * - Check if the client has passed all mandatory args
4892 * - Set special flags for argument validation
4894 static int check_mandatory_args(const QDict
*cmd_args
,
4895 const QDict
*client_args
, int *flags
)
4897 const QDictEntry
*ent
;
4899 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4900 const char *cmd_arg_name
= qdict_entry_key(ent
);
4901 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4902 assert(type
!= NULL
);
4904 if (qstring_get_str(type
)[0] == 'O') {
4905 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4906 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4907 } else if (qstring_get_str(type
)[0] != '-' &&
4908 qstring_get_str(type
)[1] != '?' &&
4909 !qdict_haskey(client_args
, cmd_arg_name
)) {
4910 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4918 static QDict
*qdict_from_args_type(const char *args_type
)
4922 QString
*key
, *type
, *cur_qs
;
4924 assert(args_type
!= NULL
);
4926 qdict
= qdict_new();
4928 if (args_type
== NULL
|| args_type
[0] == '\0') {
4929 /* no args, empty qdict */
4933 key
= qstring_new();
4934 type
= qstring_new();
4939 switch (args_type
[i
]) {
4942 qdict_put(qdict
, qstring_get_str(key
), type
);
4944 if (args_type
[i
] == '\0') {
4947 type
= qstring_new(); /* qdict has ref */
4948 cur_qs
= key
= qstring_new();
4954 qstring_append_chr(cur_qs
, args_type
[i
]);
4964 * Client argument checking rules:
4966 * 1. Client must provide all mandatory arguments
4967 * 2. Each argument provided by the client must be expected
4968 * 3. Each argument provided by the client must have the type expected
4971 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4976 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4979 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4984 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4992 * Input object checking rules
4994 * 1. Input object must be a dict
4995 * 2. The "execute" key must exist
4996 * 3. The "execute" key must be a string
4997 * 4. If the "arguments" key exists, it must be a dict
4998 * 5. If the "id" key exists, it can be anything (ie. json-value)
4999 * 6. Any argument not listed above is considered invalid
5001 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
5003 const QDictEntry
*ent
;
5004 int has_exec_key
= 0;
5007 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
5008 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
5012 input_dict
= qobject_to_qdict(input_obj
);
5014 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
5015 const char *arg_name
= qdict_entry_key(ent
);
5016 const QObject
*arg_obj
= qdict_entry_value(ent
);
5018 if (!strcmp(arg_name
, "execute")) {
5019 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
5020 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
5025 } else if (!strcmp(arg_name
, "arguments")) {
5026 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
5027 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
5031 } else if (!strcmp(arg_name
, "id")) {
5032 /* FIXME: check duplicated IDs for async commands */
5034 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
5039 if (!has_exec_key
) {
5040 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
5047 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
5049 QObject
*ret_data
= NULL
;
5051 if (handler_is_async(cmd
)) {
5052 qmp_async_info_handler(mon
, cmd
);
5053 if (monitor_has_error(mon
)) {
5054 monitor_protocol_emitter(mon
, NULL
);
5057 cmd
->mhandler
.info_new(mon
, &ret_data
);
5058 monitor_protocol_emitter(mon
, ret_data
);
5059 qobject_decref(ret_data
);
5063 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
5064 const QDict
*params
)
5067 QObject
*data
= NULL
;
5069 mon_print_count_init(mon
);
5071 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
5072 handler_audit(mon
, cmd
, ret
);
5073 monitor_protocol_emitter(mon
, data
);
5074 qobject_decref(data
);
5077 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
5081 QDict
*input
, *args
;
5082 const mon_cmd_t
*cmd
;
5083 Monitor
*mon
= cur_mon
;
5084 const char *cmd_name
, *query_cmd
;
5087 args
= input
= NULL
;
5089 obj
= json_parser_parse(tokens
, NULL
);
5091 // FIXME: should be triggered in json_parser_parse()
5092 qerror_report(QERR_JSON_PARSING
);
5096 input
= qmp_check_input_obj(obj
);
5098 qobject_decref(obj
);
5102 mon
->mc
->id
= qdict_get(input
, "id");
5103 qobject_incref(mon
->mc
->id
);
5105 cmd_name
= qdict_get_str(input
, "execute");
5106 if (invalid_qmp_mode(mon
, cmd_name
)) {
5107 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
5111 cmd
= qmp_find_cmd(cmd_name
);
5112 if (!cmd
&& strstart(cmd_name
, "query-", &query_cmd
)) {
5113 cmd
= qmp_find_query_cmd(query_cmd
);
5116 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
5120 obj
= qdict_get(input
, "arguments");
5124 args
= qobject_to_qdict(obj
);
5128 err
= qmp_check_client_args(cmd
, args
);
5134 qmp_call_query_cmd(mon
, cmd
);
5135 } else if (handler_is_async(cmd
)) {
5136 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
5138 /* emit the error response */
5142 qmp_call_cmd(mon
, cmd
, args
);
5148 monitor_protocol_emitter(mon
, NULL
);
5155 * monitor_control_read(): Read and handle QMP input
5157 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
5159 Monitor
*old_mon
= cur_mon
;
5163 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
5168 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
5170 Monitor
*old_mon
= cur_mon
;
5176 for (i
= 0; i
< size
; i
++)
5177 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
5179 if (size
== 0 || buf
[size
- 1] != 0)
5180 monitor_printf(cur_mon
, "corrupted command\n");
5182 handle_user_command(cur_mon
, (char *)buf
);
5188 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
5190 monitor_suspend(mon
);
5191 handle_user_command(mon
, cmdline
);
5192 monitor_resume(mon
);
5195 int monitor_suspend(Monitor
*mon
)
5203 void monitor_resume(Monitor
*mon
)
5207 if (--mon
->suspend_cnt
== 0)
5208 readline_show_prompt(mon
->rs
);
5211 static QObject
*get_qmp_greeting(void)
5215 do_info_version(NULL
, &ver
);
5216 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
5220 * monitor_control_event(): Print QMP gretting
5222 static void monitor_control_event(void *opaque
, int event
)
5225 Monitor
*mon
= opaque
;
5228 case CHR_EVENT_OPENED
:
5229 mon
->mc
->command_mode
= 0;
5230 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5231 data
= get_qmp_greeting();
5232 monitor_json_emitter(mon
, data
);
5233 qobject_decref(data
);
5235 case CHR_EVENT_CLOSED
:
5236 json_message_parser_destroy(&mon
->mc
->parser
);
5241 static void monitor_event(void *opaque
, int event
)
5243 Monitor
*mon
= opaque
;
5246 case CHR_EVENT_MUX_IN
:
5248 if (mon
->reset_seen
) {
5249 readline_restart(mon
->rs
);
5250 monitor_resume(mon
);
5253 mon
->suspend_cnt
= 0;
5257 case CHR_EVENT_MUX_OUT
:
5258 if (mon
->reset_seen
) {
5259 if (mon
->suspend_cnt
== 0) {
5260 monitor_printf(mon
, "\n");
5263 monitor_suspend(mon
);
5270 case CHR_EVENT_OPENED
:
5271 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
5272 "information\n", QEMU_VERSION
);
5273 if (!mon
->mux_out
) {
5274 readline_show_prompt(mon
->rs
);
5276 mon
->reset_seen
= 1;
5290 void monitor_init(CharDriverState
*chr
, int flags
)
5292 static int is_first_init
= 1;
5295 if (is_first_init
) {
5296 key_timer
= qemu_new_timer_ns(vm_clock
, release_keys
, NULL
);
5300 mon
= g_malloc0(sizeof(*mon
));
5304 if (flags
& MONITOR_USE_READLINE
) {
5305 mon
->rs
= readline_init(mon
, monitor_find_completion
);
5306 monitor_read_command(mon
, 0);
5309 if (monitor_ctrl_mode(mon
)) {
5310 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
5311 /* Control mode requires special handlers */
5312 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
5313 monitor_control_event
, mon
);
5314 qemu_chr_fe_set_echo(chr
, true);
5316 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5317 monitor_event
, mon
);
5320 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5321 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5325 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
5327 BlockDriverState
*bs
= opaque
;
5330 if (bdrv_set_key(bs
, password
) != 0) {
5331 monitor_printf(mon
, "invalid password\n");
5334 if (mon
->password_completion_cb
)
5335 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5337 monitor_read_command(mon
, 1);
5340 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5341 BlockDriverCompletionFunc
*completion_cb
,
5346 if (!bdrv_key_required(bs
)) {
5348 completion_cb(opaque
, 0);
5352 if (monitor_ctrl_mode(mon
)) {
5353 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
5357 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5358 bdrv_get_encrypted_filename(bs
));
5360 mon
->password_completion_cb
= completion_cb
;
5361 mon
->password_opaque
= opaque
;
5363 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5365 if (err
&& completion_cb
)
5366 completion_cb(opaque
, err
);