4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
43 #include "audio/audio.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
61 #include "trace/control.h"
62 #ifdef CONFIG_TRACE_SIMPLE
63 #include "trace/simple.h"
65 #include "ui/qemu-spice.h"
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
);
128 /* file descriptors passed via SCM_RIGHTS */
129 typedef struct mon_fd_t mon_fd_t
;
133 QLIST_ENTRY(mon_fd_t
) next
;
136 typedef struct MonitorControl
{
138 JSONMessageParser parser
;
143 CharDriverState
*chr
;
148 uint8_t outbuf
[1024];
153 BlockDriverCompletionFunc
*password_completion_cb
;
154 void *password_opaque
;
155 #ifdef CONFIG_DEBUG_MONITOR
159 QLIST_HEAD(,mon_fd_t
) fds
;
160 QLIST_ENTRY(Monitor
) entry
;
163 #ifdef CONFIG_DEBUG_MONITOR
164 #define MON_DEBUG(fmt, ...) do { \
165 fprintf(stderr, "Monitor: "); \
166 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
168 static inline void mon_print_count_inc(Monitor
*mon
)
170 mon
->print_calls_nr
++;
173 static inline void mon_print_count_init(Monitor
*mon
)
175 mon
->print_calls_nr
= 0;
178 static inline int mon_print_count_get(const Monitor
*mon
)
180 return mon
->print_calls_nr
;
183 #else /* !CONFIG_DEBUG_MONITOR */
184 #define MON_DEBUG(fmt, ...) do { } while (0)
185 static inline void mon_print_count_inc(Monitor
*mon
) { }
186 static inline void mon_print_count_init(Monitor
*mon
) { }
187 static inline int mon_print_count_get(const Monitor
*mon
) { return 0; }
188 #endif /* CONFIG_DEBUG_MONITOR */
190 /* QMP checker flags */
191 #define QMP_ACCEPT_UNKNOWNS 1
193 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
195 static const mon_cmd_t mon_cmds
[];
196 static const mon_cmd_t info_cmds
[];
198 static const mon_cmd_t qmp_cmds
[];
199 static const mon_cmd_t qmp_query_cmds
[];
202 Monitor
*default_mon
;
204 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
207 static inline int qmp_cmd_mode(const Monitor
*mon
)
209 return (mon
->mc
? mon
->mc
->command_mode
: 0);
212 /* Return true if in control mode, false otherwise */
213 static inline int monitor_ctrl_mode(const Monitor
*mon
)
215 return (mon
->flags
& MONITOR_USE_CONTROL
);
218 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
219 int monitor_cur_is_qmp(void)
221 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
224 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
229 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
231 readline_show_prompt(mon
->rs
);
234 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
237 if (monitor_ctrl_mode(mon
)) {
238 qerror_report(QERR_MISSING_PARAMETER
, "password");
240 } else if (mon
->rs
) {
241 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
242 /* prompt is printed on return from the command handler */
245 monitor_printf(mon
, "terminal does not support password prompting\n");
250 void monitor_flush(Monitor
*mon
)
252 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
253 qemu_chr_fe_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
254 mon
->outbuf_index
= 0;
258 /* flush at every end of line or if the buffer is full */
259 static void monitor_puts(Monitor
*mon
, const char *str
)
268 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
269 mon
->outbuf
[mon
->outbuf_index
++] = c
;
270 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
276 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
283 mon_print_count_inc(mon
);
285 if (monitor_ctrl_mode(mon
)) {
289 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
290 monitor_puts(mon
, buf
);
293 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
297 monitor_vprintf(mon
, fmt
, ap
);
301 void monitor_print_filename(Monitor
*mon
, const char *filename
)
305 for (i
= 0; filename
[i
]; i
++) {
306 switch (filename
[i
]) {
310 monitor_printf(mon
, "\\%c", filename
[i
]);
313 monitor_printf(mon
, "\\t");
316 monitor_printf(mon
, "\\r");
319 monitor_printf(mon
, "\\n");
322 monitor_printf(mon
, "%c", filename
[i
]);
328 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
329 const char *fmt
, ...)
333 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
338 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
340 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
342 return cmd
->user_print
!= NULL
;
345 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
347 return cmd
->flags
& MONITOR_CMD_ASYNC
;
350 static inline int monitor_has_error(const Monitor
*mon
)
352 return mon
->error
!= NULL
;
355 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
359 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
360 qobject_to_json(data
);
361 assert(json
!= NULL
);
363 qstring_append_chr(json
, '\n');
364 monitor_puts(mon
, qstring_get_str(json
));
369 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
373 trace_monitor_protocol_emitter(mon
);
377 if (!monitor_has_error(mon
)) {
378 /* success response */
380 qobject_incref(data
);
381 qdict_put_obj(qmp
, "return", data
);
383 /* return an empty QDict by default */
384 qdict_put(qmp
, "return", qdict_new());
388 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
389 qdict_put(qmp
, "error", mon
->error
->error
);
390 QINCREF(mon
->error
->error
);
396 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
400 monitor_json_emitter(mon
, QOBJECT(qmp
));
404 static void timestamp_put(QDict
*qdict
)
410 err
= qemu_gettimeofday(&tv
);
414 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
415 "'microseconds': %" PRId64
" }",
416 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
417 qdict_put_obj(qdict
, "timestamp", obj
);
421 * monitor_protocol_event(): Generate a Monitor event
423 * Event-specific data can be emitted through the (optional) 'data' parameter.
425 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
428 const char *event_name
;
431 assert(event
< QEVENT_MAX
);
434 case QEVENT_SHUTDOWN
:
435 event_name
= "SHUTDOWN";
438 event_name
= "RESET";
440 case QEVENT_POWERDOWN
:
441 event_name
= "POWERDOWN";
447 event_name
= "RESUME";
449 case QEVENT_VNC_CONNECTED
:
450 event_name
= "VNC_CONNECTED";
452 case QEVENT_VNC_INITIALIZED
:
453 event_name
= "VNC_INITIALIZED";
455 case QEVENT_VNC_DISCONNECTED
:
456 event_name
= "VNC_DISCONNECTED";
458 case QEVENT_BLOCK_IO_ERROR
:
459 event_name
= "BLOCK_IO_ERROR";
461 case QEVENT_RTC_CHANGE
:
462 event_name
= "RTC_CHANGE";
464 case QEVENT_WATCHDOG
:
465 event_name
= "WATCHDOG";
467 case QEVENT_SPICE_CONNECTED
:
468 event_name
= "SPICE_CONNECTED";
470 case QEVENT_SPICE_INITIALIZED
:
471 event_name
= "SPICE_INITIALIZED";
473 case QEVENT_SPICE_DISCONNECTED
:
474 event_name
= "SPICE_DISCONNECTED";
483 qdict_put(qmp
, "event", qstring_from_str(event_name
));
485 qobject_incref(data
);
486 qdict_put_obj(qmp
, "data", data
);
489 QLIST_FOREACH(mon
, &mon_list
, entry
) {
490 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
491 monitor_json_emitter(mon
, QOBJECT(qmp
));
497 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
500 /* Will setup QMP capabilities in the future */
501 if (monitor_ctrl_mode(mon
)) {
502 mon
->mc
->command_mode
= 1;
508 static int mon_set_cpu(int cpu_index
);
509 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
511 static int do_hmp_passthrough(Monitor
*mon
, const QDict
*params
,
515 Monitor
*old_mon
, hmp
;
516 CharDriverState mchar
;
518 memset(&hmp
, 0, sizeof(hmp
));
519 qemu_chr_init_mem(&mchar
);
525 if (qdict_haskey(params
, "cpu-index")) {
526 ret
= mon_set_cpu(qdict_get_int(params
, "cpu-index"));
529 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "cpu-index", "a CPU number");
534 handle_user_command(&hmp
, qdict_get_str(params
, "command-line"));
537 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
538 *ret_data
= QOBJECT(qemu_chr_mem_to_qs(hmp
.chr
));
542 qemu_chr_close_mem(hmp
.chr
);
546 static int compare_cmd(const char *name
, const char *list
)
548 const char *p
, *pstart
;
556 p
= pstart
+ strlen(pstart
);
557 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
566 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
567 const char *prefix
, const char *name
)
569 const mon_cmd_t
*cmd
;
571 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
572 if (!name
|| !strcmp(name
, cmd
->name
))
573 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
574 cmd
->params
, cmd
->help
);
578 static void help_cmd(Monitor
*mon
, const char *name
)
580 if (name
&& !strcmp(name
, "info")) {
581 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
583 help_cmd_dump(mon
, mon_cmds
, "", name
);
584 if (name
&& !strcmp(name
, "log")) {
585 const CPULogItem
*item
;
586 monitor_printf(mon
, "Log items (comma separated):\n");
587 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
588 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
589 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
595 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
597 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
600 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
602 const char *tp_name
= qdict_get_str(qdict
, "name");
603 bool new_state
= qdict_get_bool(qdict
, "option");
604 int ret
= trace_event_set_state(tp_name
, new_state
);
607 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
611 #ifdef CONFIG_TRACE_SIMPLE
612 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
614 const char *op
= qdict_get_try_str(qdict
, "op");
615 const char *arg
= qdict_get_try_str(qdict
, "arg");
618 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
619 } else if (!strcmp(op
, "on")) {
620 st_set_trace_file_enabled(true);
621 } else if (!strcmp(op
, "off")) {
622 st_set_trace_file_enabled(false);
623 } else if (!strcmp(op
, "flush")) {
624 st_flush_trace_buffer();
625 } else if (!strcmp(op
, "set")) {
627 st_set_trace_file(arg
);
630 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
631 help_cmd(mon
, "trace-file");
636 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
638 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
641 data
->user_print(data
->mon
, ret_data
);
643 monitor_resume(data
->mon
);
647 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
649 monitor_protocol_emitter(opaque
, ret_data
);
652 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
655 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
658 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
660 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
663 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
668 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
670 cb_data
->user_print
= cmd
->user_print
;
671 monitor_suspend(mon
);
672 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
673 user_monitor_complete
, cb_data
);
680 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
684 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
686 cb_data
->user_print
= cmd
->user_print
;
687 monitor_suspend(mon
);
688 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
695 static void do_info(Monitor
*mon
, const QDict
*qdict
)
697 const mon_cmd_t
*cmd
;
698 const char *item
= qdict_get_try_str(qdict
, "item");
704 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
705 if (compare_cmd(item
, cmd
->name
))
709 if (cmd
->name
== NULL
) {
713 if (handler_is_async(cmd
)) {
714 user_async_info_handler(mon
, cmd
);
715 } else if (handler_is_qobject(cmd
)) {
716 QObject
*info_data
= NULL
;
718 cmd
->mhandler
.info_new(mon
, &info_data
);
720 cmd
->user_print(mon
, info_data
);
721 qobject_decref(info_data
);
724 cmd
->mhandler
.info(mon
);
730 help_cmd(mon
, "info");
733 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
738 qdict
= qobject_to_qdict(data
);
739 qemu
= qdict_get_qdict(qdict
, "qemu");
741 monitor_printf(mon
, "%" PRId64
".%" PRId64
".%" PRId64
"%s\n",
742 qdict_get_int(qemu
, "major"),
743 qdict_get_int(qemu
, "minor"),
744 qdict_get_int(qemu
, "micro"),
745 qdict_get_str(qdict
, "package"));
748 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
750 const char *version
= QEMU_VERSION
;
751 int major
= 0, minor
= 0, micro
= 0;
754 major
= strtol(version
, &tmp
, 10);
756 minor
= strtol(tmp
, &tmp
, 10);
758 micro
= strtol(tmp
, &tmp
, 10);
760 *ret_data
= qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
761 'micro': %d }, 'package': %s }", major
, minor
, micro
, QEMU_PKGVERSION
);
764 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
768 qdict
= qobject_to_qdict(data
);
769 if (qdict_size(qdict
) == 0) {
773 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
776 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
778 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
779 qobject_from_jsonf("{}");
782 static QObject
*get_cmd_dict(const char *name
)
786 /* Remove '|' from some commands */
787 p
= strchr(name
, '|');
794 return qobject_from_jsonf("{ 'name': %s }", p
);
797 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
800 const mon_cmd_t
*cmd
;
802 cmd_list
= qlist_new();
804 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
805 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
808 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
810 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
811 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
814 *ret_data
= QOBJECT(cmd_list
);
817 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
819 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
822 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
826 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
827 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
828 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
829 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
830 qemu_uuid
[14], qemu_uuid
[15]);
831 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
834 /* get the current CPU defined by the user */
835 static int mon_set_cpu(int cpu_index
)
839 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
840 if (env
->cpu_index
== cpu_index
) {
841 cur_mon
->mon_cpu
= env
;
848 static CPUState
*mon_get_cpu(void)
850 if (!cur_mon
->mon_cpu
) {
853 cpu_synchronize_state(cur_mon
->mon_cpu
);
854 return cur_mon
->mon_cpu
;
857 static void do_info_registers(Monitor
*mon
)
862 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
865 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
870 static void print_cpu_iter(QObject
*obj
, void *opaque
)
874 Monitor
*mon
= opaque
;
876 assert(qobject_type(obj
) == QTYPE_QDICT
);
877 cpu
= qobject_to_qdict(obj
);
879 if (qdict_get_bool(cpu
, "current")) {
883 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
885 #if defined(TARGET_I386)
886 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
887 (target_ulong
) qdict_get_int(cpu
, "pc"));
888 #elif defined(TARGET_PPC)
889 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
890 (target_long
) qdict_get_int(cpu
, "nip"));
891 #elif defined(TARGET_SPARC)
892 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
893 (target_long
) qdict_get_int(cpu
, "pc"));
894 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
895 (target_long
) qdict_get_int(cpu
, "npc"));
896 #elif defined(TARGET_MIPS)
897 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
898 (target_long
) qdict_get_int(cpu
, "PC"));
901 if (qdict_get_bool(cpu
, "halted")) {
902 monitor_printf(mon
, " (halted)");
905 monitor_printf(mon
, " thread_id=%" PRId64
" ",
906 qdict_get_int(cpu
, "thread_id"));
908 monitor_printf(mon
, "\n");
911 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
915 assert(qobject_type(data
) == QTYPE_QLIST
);
916 cpu_list
= qobject_to_qlist(data
);
917 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
920 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
925 cpu_list
= qlist_new();
927 /* just to set the default cpu if not already done */
930 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
934 cpu_synchronize_state(env
);
936 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
937 env
->cpu_index
, env
== mon
->mon_cpu
,
940 cpu
= qobject_to_qdict(obj
);
942 #if defined(TARGET_I386)
943 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
944 #elif defined(TARGET_PPC)
945 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
946 #elif defined(TARGET_SPARC)
947 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
948 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
949 #elif defined(TARGET_MIPS)
950 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
952 qdict_put(cpu
, "thread_id", qint_from_int(env
->thread_id
));
954 qlist_append(cpu_list
, cpu
);
957 *ret_data
= QOBJECT(cpu_list
);
960 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
962 int index
= qdict_get_int(qdict
, "index");
963 if (mon_set_cpu(index
) < 0) {
964 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "index",
971 static void do_info_jit(Monitor
*mon
)
973 dump_exec_info((FILE *)mon
, monitor_fprintf
);
976 static void do_info_history(Monitor
*mon
)
985 str
= readline_get_history(mon
->rs
, i
);
988 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
993 #if defined(TARGET_PPC)
994 /* XXX: not implemented in other targets */
995 static void do_info_cpu_stats(Monitor
*mon
)
1000 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
1004 #if defined(CONFIG_TRACE_SIMPLE)
1005 static void do_info_trace(Monitor
*mon
)
1007 st_print_trace((FILE *)mon
, &monitor_fprintf
);
1011 static void do_trace_print_events(Monitor
*mon
)
1013 trace_print_events((FILE *)mon
, &monitor_fprintf
);
1017 * do_quit(): Quit QEMU execution
1019 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1021 monitor_suspend(mon
);
1023 qemu_system_shutdown_request();
1029 static int change_vnc_password(const char *password
)
1031 if (!password
|| !password
[0]) {
1032 if (vnc_display_disable_login(NULL
)) {
1033 qerror_report(QERR_SET_PASSWD_FAILED
);
1039 if (vnc_display_password(NULL
, password
) < 0) {
1040 qerror_report(QERR_SET_PASSWD_FAILED
);
1047 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1050 change_vnc_password(password
);
1051 monitor_read_command(mon
, 1);
1054 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1056 if (strcmp(target
, "passwd") == 0 ||
1057 strcmp(target
, "password") == 0) {
1060 strncpy(password
, arg
, sizeof(password
));
1061 password
[sizeof(password
) - 1] = '\0';
1062 return change_vnc_password(password
);
1064 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1067 if (vnc_display_open(NULL
, target
) < 0) {
1068 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
1076 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1078 qerror_report(QERR_FEATURE_DISABLED
, "vnc");
1084 * do_change(): Change a removable medium, or VNC configuration
1086 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1088 const char *device
= qdict_get_str(qdict
, "device");
1089 const char *target
= qdict_get_str(qdict
, "target");
1090 const char *arg
= qdict_get_try_str(qdict
, "arg");
1093 if (strcmp(device
, "vnc") == 0) {
1094 ret
= do_change_vnc(mon
, target
, arg
);
1096 ret
= do_change_block(mon
, device
, target
, arg
);
1102 static int set_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1104 const char *protocol
= qdict_get_str(qdict
, "protocol");
1105 const char *password
= qdict_get_str(qdict
, "password");
1106 const char *connected
= qdict_get_try_str(qdict
, "connected");
1107 int disconnect_if_connected
= 0;
1108 int fail_if_connected
= 0;
1112 if (strcmp(connected
, "fail") == 0) {
1113 fail_if_connected
= 1;
1114 } else if (strcmp(connected
, "disconnect") == 0) {
1115 disconnect_if_connected
= 1;
1116 } else if (strcmp(connected
, "keep") == 0) {
1119 qerror_report(QERR_INVALID_PARAMETER
, "connected");
1124 if (strcmp(protocol
, "spice") == 0) {
1126 /* correct one? spice isn't a device ,,, */
1127 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1130 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
1131 disconnect_if_connected
);
1133 qerror_report(QERR_SET_PASSWD_FAILED
);
1139 if (strcmp(protocol
, "vnc") == 0) {
1140 if (fail_if_connected
|| disconnect_if_connected
) {
1141 /* vnc supports "connected=keep" only */
1142 qerror_report(QERR_INVALID_PARAMETER
, "connected");
1145 /* Note that setting an empty password will not disable login through
1146 * this interface. */
1147 return vnc_display_password(NULL
, password
);
1150 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1154 static int expire_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1156 const char *protocol
= qdict_get_str(qdict
, "protocol");
1157 const char *whenstr
= qdict_get_str(qdict
, "time");
1161 if (strcmp(whenstr
, "now") == 0) {
1163 } else if (strcmp(whenstr
, "never") == 0) {
1165 } else if (whenstr
[0] == '+') {
1166 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
1168 when
= strtoull(whenstr
, NULL
, 10);
1171 if (strcmp(protocol
, "spice") == 0) {
1173 /* correct one? spice isn't a device ,,, */
1174 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1177 rc
= qemu_spice_set_pw_expire(when
);
1179 qerror_report(QERR_SET_PASSWD_FAILED
);
1185 if (strcmp(protocol
, "vnc") == 0) {
1186 return vnc_display_pw_expire(NULL
, when
);
1189 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1193 static int add_graphics_client(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1195 const char *protocol
= qdict_get_str(qdict
, "protocol");
1196 const char *fdname
= qdict_get_str(qdict
, "fdname");
1199 if (strcmp(protocol
, "spice") == 0) {
1201 /* correct one? spice isn't a device ,,, */
1202 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1205 qerror_report(QERR_ADD_CLIENT_FAILED
);
1208 } else if (strcmp(protocol
, "vnc") == 0) {
1209 int fd
= monitor_get_fd(mon
, fdname
);
1210 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
1211 vnc_display_add_client(NULL
, fd
, skipauth
);
1214 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
1215 int fd
= monitor_get_fd(mon
, fdname
);
1216 if (qemu_chr_add_client(s
, fd
) < 0) {
1217 qerror_report(QERR_ADD_CLIENT_FAILED
);
1223 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1227 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1229 const char *protocol
= qdict_get_str(qdict
, "protocol");
1230 const char *hostname
= qdict_get_str(qdict
, "hostname");
1231 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
1232 int port
= qdict_get_try_int(qdict
, "port", -1);
1233 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
1236 if (strcmp(protocol
, "spice") == 0) {
1238 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1242 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
);
1244 qerror_report(QERR_UNDEFINED_ERROR
);
1250 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1254 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1256 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1260 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1262 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1265 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1268 const char *items
= qdict_get_str(qdict
, "items");
1270 if (!strcmp(items
, "none")) {
1273 mask
= cpu_str_to_log_mask(items
);
1275 help_cmd(mon
, "log");
1282 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1284 const char *option
= qdict_get_try_str(qdict
, "option");
1285 if (!option
|| !strcmp(option
, "on")) {
1287 } else if (!strcmp(option
, "off")) {
1290 monitor_printf(mon
, "unexpected option %s\n", option
);
1295 * do_stop(): Stop VM execution
1297 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1299 vm_stop(RSTATE_PAUSED
);
1303 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1305 struct bdrv_iterate_context
{
1311 * do_cont(): Resume emulation.
1313 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1315 struct bdrv_iterate_context context
= { mon
, 0 };
1317 if (runstate_check(RSTATE_IN_MIGRATE
)) {
1318 qerror_report(QERR_MIGRATION_EXPECTED
);
1320 } else if (runstate_check(RSTATE_PANICKED
) ||
1321 runstate_check(RSTATE_SHUTDOWN
)) {
1322 qerror_report(QERR_RESET_REQUIRED
);
1326 bdrv_iterate(encrypted_bdrv_it
, &context
);
1327 /* only resume the vm if all keys are set and valid */
1336 static void bdrv_key_cb(void *opaque
, int err
)
1338 Monitor
*mon
= opaque
;
1340 /* another key was set successfully, retry to continue */
1342 do_cont(mon
, NULL
, NULL
);
1345 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1347 struct bdrv_iterate_context
*context
= opaque
;
1349 if (!context
->err
&& bdrv_key_required(bs
)) {
1350 context
->err
= -EBUSY
;
1351 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1356 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1358 const char *device
= qdict_get_try_str(qdict
, "device");
1360 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1361 if (gdbserver_start(device
) < 0) {
1362 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1364 } else if (strcmp(device
, "none") == 0) {
1365 monitor_printf(mon
, "Disabled gdbserver\n");
1367 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1372 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1374 const char *action
= qdict_get_str(qdict
, "action");
1375 if (select_watchdog_action(action
) == -1) {
1376 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1380 static void monitor_printc(Monitor
*mon
, int c
)
1382 monitor_printf(mon
, "'");
1385 monitor_printf(mon
, "\\'");
1388 monitor_printf(mon
, "\\\\");
1391 monitor_printf(mon
, "\\n");
1394 monitor_printf(mon
, "\\r");
1397 if (c
>= 32 && c
<= 126) {
1398 monitor_printf(mon
, "%c", c
);
1400 monitor_printf(mon
, "\\x%02x", c
);
1404 monitor_printf(mon
, "'");
1407 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1408 target_phys_addr_t addr
, int is_physical
)
1411 int l
, line_size
, i
, max_digits
, len
;
1415 if (format
== 'i') {
1418 env
= mon_get_cpu();
1422 } else if (wsize
== 4) {
1425 /* as default we use the current CS size */
1428 #ifdef TARGET_X86_64
1429 if ((env
->efer
& MSR_EFER_LMA
) &&
1430 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1434 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1439 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1443 len
= wsize
* count
;
1452 max_digits
= (wsize
* 8 + 2) / 3;
1456 max_digits
= (wsize
* 8) / 4;
1460 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1469 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1471 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1476 cpu_physical_memory_read(addr
, buf
, l
);
1478 env
= mon_get_cpu();
1479 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1480 monitor_printf(mon
, " Cannot access memory\n");
1489 v
= ldub_raw(buf
+ i
);
1492 v
= lduw_raw(buf
+ i
);
1495 v
= (uint32_t)ldl_raw(buf
+ i
);
1498 v
= ldq_raw(buf
+ i
);
1501 monitor_printf(mon
, " ");
1504 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1507 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1510 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1513 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1516 monitor_printc(mon
, v
);
1521 monitor_printf(mon
, "\n");
1527 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1529 int count
= qdict_get_int(qdict
, "count");
1530 int format
= qdict_get_int(qdict
, "format");
1531 int size
= qdict_get_int(qdict
, "size");
1532 target_long addr
= qdict_get_int(qdict
, "addr");
1534 memory_dump(mon
, count
, format
, size
, addr
, 0);
1537 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1539 int count
= qdict_get_int(qdict
, "count");
1540 int format
= qdict_get_int(qdict
, "format");
1541 int size
= qdict_get_int(qdict
, "size");
1542 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1544 memory_dump(mon
, count
, format
, size
, addr
, 1);
1547 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1549 int format
= qdict_get_int(qdict
, "format");
1550 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1552 #if TARGET_PHYS_ADDR_BITS == 32
1555 monitor_printf(mon
, "%#o", val
);
1558 monitor_printf(mon
, "%#x", val
);
1561 monitor_printf(mon
, "%u", val
);
1565 monitor_printf(mon
, "%d", val
);
1568 monitor_printc(mon
, val
);
1574 monitor_printf(mon
, "%#" PRIo64
, val
);
1577 monitor_printf(mon
, "%#" PRIx64
, val
);
1580 monitor_printf(mon
, "%" PRIu64
, val
);
1584 monitor_printf(mon
, "%" PRId64
, val
);
1587 monitor_printc(mon
, val
);
1591 monitor_printf(mon
, "\n");
1594 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1597 uint32_t size
= qdict_get_int(qdict
, "size");
1598 const char *filename
= qdict_get_str(qdict
, "filename");
1599 target_long addr
= qdict_get_int(qdict
, "val");
1605 env
= mon_get_cpu();
1607 f
= fopen(filename
, "wb");
1609 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1616 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1617 if (fwrite(buf
, 1, l
, f
) != l
) {
1618 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1632 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1638 uint32_t size
= qdict_get_int(qdict
, "size");
1639 const char *filename
= qdict_get_str(qdict
, "filename");
1640 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1643 f
= fopen(filename
, "wb");
1645 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1652 cpu_physical_memory_read(addr
, buf
, l
);
1653 if (fwrite(buf
, 1, l
, f
) != l
) {
1654 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1669 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1673 uint32_t start
= qdict_get_int(qdict
, "start");
1674 uint32_t size
= qdict_get_int(qdict
, "size");
1677 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1678 uint8_t val
= ldub_phys(addr
);
1679 /* BSD sum algorithm ('sum' Unix command) */
1680 sum
= (sum
>> 1) | (sum
<< 15);
1683 monitor_printf(mon
, "%05d\n", sum
);
1691 static const KeyDef key_defs
[] = {
1693 { 0x36, "shift_r" },
1698 { 0xe4, "altgr_r" },
1718 { 0x0e, "backspace" },
1731 { 0x1a, "bracket_left" },
1732 { 0x1b, "bracket_right" },
1744 { 0x27, "semicolon" },
1745 { 0x28, "apostrophe" },
1746 { 0x29, "grave_accent" },
1748 { 0x2b, "backslash" },
1760 { 0x37, "asterisk" },
1763 { 0x3a, "caps_lock" },
1774 { 0x45, "num_lock" },
1775 { 0x46, "scroll_lock" },
1777 { 0xb5, "kp_divide" },
1778 { 0x37, "kp_multiply" },
1779 { 0x4a, "kp_subtract" },
1781 { 0x9c, "kp_enter" },
1782 { 0x53, "kp_decimal" },
1815 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1830 { 0xfe, "compose" },
1835 static int get_keycode(const char *key
)
1841 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1842 if (!strcmp(key
, p
->name
))
1845 if (strstart(key
, "0x", NULL
)) {
1846 ret
= strtoul(key
, &endp
, 0);
1847 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1853 #define MAX_KEYCODES 16
1854 static uint8_t keycodes
[MAX_KEYCODES
];
1855 static int nb_pending_keycodes
;
1856 static QEMUTimer
*key_timer
;
1858 static void release_keys(void *opaque
)
1862 while (nb_pending_keycodes
> 0) {
1863 nb_pending_keycodes
--;
1864 keycode
= keycodes
[nb_pending_keycodes
];
1866 kbd_put_keycode(0xe0);
1867 kbd_put_keycode(keycode
| 0x80);
1871 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1873 char keyname_buf
[16];
1875 int keyname_len
, keycode
, i
;
1876 const char *string
= qdict_get_str(qdict
, "string");
1877 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1878 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1880 if (nb_pending_keycodes
> 0) {
1881 qemu_del_timer(key_timer
);
1888 separator
= strchr(string
, '-');
1889 keyname_len
= separator
? separator
- string
: strlen(string
);
1890 if (keyname_len
> 0) {
1891 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1892 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1893 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1896 if (i
== MAX_KEYCODES
) {
1897 monitor_printf(mon
, "too many keys\n");
1900 keyname_buf
[keyname_len
] = 0;
1901 keycode
= get_keycode(keyname_buf
);
1903 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1906 keycodes
[i
++] = keycode
;
1910 string
= separator
+ 1;
1912 nb_pending_keycodes
= i
;
1913 /* key down events */
1914 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1915 keycode
= keycodes
[i
];
1917 kbd_put_keycode(0xe0);
1918 kbd_put_keycode(keycode
& 0x7f);
1920 /* delayed key up events */
1921 qemu_mod_timer(key_timer
, qemu_get_clock_ns(vm_clock
) +
1922 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1925 static int mouse_button_state
;
1927 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1930 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1931 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1932 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1933 dx
= strtol(dx_str
, NULL
, 0);
1934 dy
= strtol(dy_str
, NULL
, 0);
1937 dz
= strtol(dz_str
, NULL
, 0);
1938 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1941 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1943 int button_state
= qdict_get_int(qdict
, "button_state");
1944 mouse_button_state
= button_state
;
1945 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1948 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1950 int size
= qdict_get_int(qdict
, "size");
1951 int addr
= qdict_get_int(qdict
, "addr");
1952 int has_index
= qdict_haskey(qdict
, "index");
1957 int index
= qdict_get_int(qdict
, "index");
1958 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1966 val
= cpu_inb(addr
);
1970 val
= cpu_inw(addr
);
1974 val
= cpu_inl(addr
);
1978 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1979 suffix
, addr
, size
* 2, val
);
1982 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1984 int size
= qdict_get_int(qdict
, "size");
1985 int addr
= qdict_get_int(qdict
, "addr");
1986 int val
= qdict_get_int(qdict
, "val");
1988 addr
&= IOPORTS_MASK
;
1993 cpu_outb(addr
, val
);
1996 cpu_outw(addr
, val
);
1999 cpu_outl(addr
, val
);
2004 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
2007 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
2009 res
= qemu_boot_set(bootdevice
);
2011 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
2012 } else if (res
> 0) {
2013 monitor_printf(mon
, "setting boot device list failed\n");
2015 monitor_printf(mon
, "no function defined to set boot device list for "
2016 "this architecture\n");
2021 * do_system_reset(): Issue a machine reset
2023 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
2026 qemu_system_reset_request();
2031 * do_system_powerdown(): Issue a machine powerdown
2033 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
2036 qemu_system_powerdown_request();
2040 #if defined(TARGET_I386)
2041 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
2042 target_phys_addr_t pte
,
2043 target_phys_addr_t mask
)
2045 #ifdef TARGET_X86_64
2046 if (addr
& (1ULL << 47)) {
2050 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
2051 " %c%c%c%c%c%c%c%c%c\n",
2054 pte
& PG_NX_MASK
? 'X' : '-',
2055 pte
& PG_GLOBAL_MASK
? 'G' : '-',
2056 pte
& PG_PSE_MASK
? 'P' : '-',
2057 pte
& PG_DIRTY_MASK
? 'D' : '-',
2058 pte
& PG_ACCESSED_MASK
? 'A' : '-',
2059 pte
& PG_PCD_MASK
? 'C' : '-',
2060 pte
& PG_PWT_MASK
? 'T' : '-',
2061 pte
& PG_USER_MASK
? 'U' : '-',
2062 pte
& PG_RW_MASK
? 'W' : '-');
2065 static void tlb_info_32(Monitor
*mon
, CPUState
*env
)
2067 unsigned int l1
, l2
;
2068 uint32_t pgd
, pde
, pte
;
2070 pgd
= env
->cr
[3] & ~0xfff;
2071 for(l1
= 0; l1
< 1024; l1
++) {
2072 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2073 pde
= le32_to_cpu(pde
);
2074 if (pde
& PG_PRESENT_MASK
) {
2075 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2077 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
2079 for(l2
= 0; l2
< 1024; l2
++) {
2080 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2081 pte
= le32_to_cpu(pte
);
2082 if (pte
& PG_PRESENT_MASK
) {
2083 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
2093 static void tlb_info_pae32(Monitor
*mon
, CPUState
*env
)
2095 unsigned int l1
, l2
, l3
;
2096 uint64_t pdpe
, pde
, pte
;
2097 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2099 pdp_addr
= env
->cr
[3] & ~0x1f;
2100 for (l1
= 0; l1
< 4; l1
++) {
2101 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2102 pdpe
= le64_to_cpu(pdpe
);
2103 if (pdpe
& PG_PRESENT_MASK
) {
2104 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2105 for (l2
= 0; l2
< 512; l2
++) {
2106 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2107 pde
= le64_to_cpu(pde
);
2108 if (pde
& PG_PRESENT_MASK
) {
2109 if (pde
& PG_PSE_MASK
) {
2110 /* 2M pages with PAE, CR4.PSE is ignored */
2111 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
2112 ~((target_phys_addr_t
)(1 << 20) - 1));
2114 pt_addr
= pde
& 0x3fffffffff000ULL
;
2115 for (l3
= 0; l3
< 512; l3
++) {
2116 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2117 pte
= le64_to_cpu(pte
);
2118 if (pte
& PG_PRESENT_MASK
) {
2119 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
2122 ~(target_phys_addr_t
)0xfff);
2132 #ifdef TARGET_X86_64
2133 static void tlb_info_64(Monitor
*mon
, CPUState
*env
)
2135 uint64_t l1
, l2
, l3
, l4
;
2136 uint64_t pml4e
, pdpe
, pde
, pte
;
2137 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
2139 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2140 for (l1
= 0; l1
< 512; l1
++) {
2141 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2142 pml4e
= le64_to_cpu(pml4e
);
2143 if (pml4e
& PG_PRESENT_MASK
) {
2144 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2145 for (l2
= 0; l2
< 512; l2
++) {
2146 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2147 pdpe
= le64_to_cpu(pdpe
);
2148 if (pdpe
& PG_PRESENT_MASK
) {
2149 if (pdpe
& PG_PSE_MASK
) {
2150 /* 1G pages, CR4.PSE is ignored */
2151 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
2152 0x3ffffc0000000ULL
);
2154 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2155 for (l3
= 0; l3
< 512; l3
++) {
2156 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2157 pde
= le64_to_cpu(pde
);
2158 if (pde
& PG_PRESENT_MASK
) {
2159 if (pde
& PG_PSE_MASK
) {
2160 /* 2M pages, CR4.PSE is ignored */
2161 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
2163 0x3ffffffe00000ULL
);
2165 pt_addr
= pde
& 0x3fffffffff000ULL
;
2166 for (l4
= 0; l4
< 512; l4
++) {
2167 cpu_physical_memory_read(pt_addr
2170 pte
= le64_to_cpu(pte
);
2171 if (pte
& PG_PRESENT_MASK
) {
2172 print_pte(mon
, (l1
<< 39) +
2174 (l3
<< 21) + (l4
<< 12),
2176 0x3fffffffff000ULL
);
2190 static void tlb_info(Monitor
*mon
)
2194 env
= mon_get_cpu();
2196 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2197 monitor_printf(mon
, "PG disabled\n");
2200 if (env
->cr
[4] & CR4_PAE_MASK
) {
2201 #ifdef TARGET_X86_64
2202 if (env
->hflags
& HF_LMA_MASK
) {
2203 tlb_info_64(mon
, env
);
2207 tlb_info_pae32(mon
, env
);
2210 tlb_info_32(mon
, env
);
2214 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
2216 target_phys_addr_t end
, int prot
)
2219 prot1
= *plast_prot
;
2220 if (prot
!= prot1
) {
2221 if (*pstart
!= -1) {
2222 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
2223 TARGET_FMT_plx
" %c%c%c\n",
2224 *pstart
, end
, end
- *pstart
,
2225 prot1
& PG_USER_MASK
? 'u' : '-',
2227 prot1
& PG_RW_MASK
? 'w' : '-');
2237 static void mem_info_32(Monitor
*mon
, CPUState
*env
)
2239 unsigned int l1
, l2
;
2240 int prot
, last_prot
;
2241 uint32_t pgd
, pde
, pte
;
2242 target_phys_addr_t start
, end
;
2244 pgd
= env
->cr
[3] & ~0xfff;
2247 for(l1
= 0; l1
< 1024; l1
++) {
2248 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2249 pde
= le32_to_cpu(pde
);
2251 if (pde
& PG_PRESENT_MASK
) {
2252 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2253 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2254 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2256 for(l2
= 0; l2
< 1024; l2
++) {
2257 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2258 pte
= le32_to_cpu(pte
);
2259 end
= (l1
<< 22) + (l2
<< 12);
2260 if (pte
& PG_PRESENT_MASK
) {
2262 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2266 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2271 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2274 /* Flush last range */
2275 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2278 static void mem_info_pae32(Monitor
*mon
, CPUState
*env
)
2280 unsigned int l1
, l2
, l3
;
2281 int prot
, last_prot
;
2282 uint64_t pdpe
, pde
, pte
;
2283 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2284 target_phys_addr_t start
, end
;
2286 pdp_addr
= env
->cr
[3] & ~0x1f;
2289 for (l1
= 0; l1
< 4; l1
++) {
2290 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2291 pdpe
= le64_to_cpu(pdpe
);
2293 if (pdpe
& PG_PRESENT_MASK
) {
2294 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2295 for (l2
= 0; l2
< 512; l2
++) {
2296 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2297 pde
= le64_to_cpu(pde
);
2298 end
= (l1
<< 30) + (l2
<< 21);
2299 if (pde
& PG_PRESENT_MASK
) {
2300 if (pde
& PG_PSE_MASK
) {
2301 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2303 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2305 pt_addr
= pde
& 0x3fffffffff000ULL
;
2306 for (l3
= 0; l3
< 512; l3
++) {
2307 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2308 pte
= le64_to_cpu(pte
);
2309 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
2310 if (pte
& PG_PRESENT_MASK
) {
2311 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
2316 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2321 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2326 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2329 /* Flush last range */
2330 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2334 #ifdef TARGET_X86_64
2335 static void mem_info_64(Monitor
*mon
, CPUState
*env
)
2337 int prot
, last_prot
;
2338 uint64_t l1
, l2
, l3
, l4
;
2339 uint64_t pml4e
, pdpe
, pde
, pte
;
2340 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
2342 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2345 for (l1
= 0; l1
< 512; l1
++) {
2346 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2347 pml4e
= le64_to_cpu(pml4e
);
2349 if (pml4e
& PG_PRESENT_MASK
) {
2350 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2351 for (l2
= 0; l2
< 512; l2
++) {
2352 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2353 pdpe
= le64_to_cpu(pdpe
);
2354 end
= (l1
<< 39) + (l2
<< 30);
2355 if (pdpe
& PG_PRESENT_MASK
) {
2356 if (pdpe
& PG_PSE_MASK
) {
2357 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
2360 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2362 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2363 for (l3
= 0; l3
< 512; l3
++) {
2364 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2365 pde
= le64_to_cpu(pde
);
2366 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
2367 if (pde
& PG_PRESENT_MASK
) {
2368 if (pde
& PG_PSE_MASK
) {
2369 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2371 prot
&= pml4e
& pdpe
;
2372 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2374 pt_addr
= pde
& 0x3fffffffff000ULL
;
2375 for (l4
= 0; l4
< 512; l4
++) {
2376 cpu_physical_memory_read(pt_addr
2379 pte
= le64_to_cpu(pte
);
2380 end
= (l1
<< 39) + (l2
<< 30) +
2381 (l3
<< 21) + (l4
<< 12);
2382 if (pte
& PG_PRESENT_MASK
) {
2383 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
2385 prot
&= pml4e
& pdpe
& pde
;
2389 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2394 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2400 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2405 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2408 /* Flush last range */
2409 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 48, 0);
2413 static void mem_info(Monitor
*mon
)
2417 env
= mon_get_cpu();
2419 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2420 monitor_printf(mon
, "PG disabled\n");
2423 if (env
->cr
[4] & CR4_PAE_MASK
) {
2424 #ifdef TARGET_X86_64
2425 if (env
->hflags
& HF_LMA_MASK
) {
2426 mem_info_64(mon
, env
);
2430 mem_info_pae32(mon
, env
);
2433 mem_info_32(mon
, env
);
2438 #if defined(TARGET_SH4)
2440 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2442 monitor_printf(mon
, " tlb%i:\t"
2443 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2444 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2445 "dirty=%hhu writethrough=%hhu\n",
2447 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2448 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2452 static void tlb_info(Monitor
*mon
)
2454 CPUState
*env
= mon_get_cpu();
2457 monitor_printf (mon
, "ITLB:\n");
2458 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2459 print_tlb (mon
, i
, &env
->itlb
[i
]);
2460 monitor_printf (mon
, "UTLB:\n");
2461 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2462 print_tlb (mon
, i
, &env
->utlb
[i
]);
2467 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
2468 static void tlb_info(Monitor
*mon
)
2470 CPUState
*env1
= mon_get_cpu();
2472 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
2476 static void do_info_mtree(Monitor
*mon
)
2478 mtree_info((fprintf_function
)monitor_printf
, mon
);
2481 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
2485 qdict
= qobject_to_qdict(data
);
2487 monitor_printf(mon
, "kvm support: ");
2488 if (qdict_get_bool(qdict
, "present")) {
2489 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2490 "enabled" : "disabled");
2492 monitor_printf(mon
, "not compiled\n");
2496 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2499 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2502 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2506 static void do_info_numa(Monitor
*mon
)
2511 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2512 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2513 monitor_printf(mon
, "node %d cpus:", i
);
2514 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2515 if (env
->numa_node
== i
) {
2516 monitor_printf(mon
, " %d", env
->cpu_index
);
2519 monitor_printf(mon
, "\n");
2520 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2525 #ifdef CONFIG_PROFILER
2530 static void do_info_profile(Monitor
*mon
)
2536 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2537 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2538 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2539 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2544 static void do_info_profile(Monitor
*mon
)
2546 monitor_printf(mon
, "Internal profiler not compiled\n");
2550 /* Capture support */
2551 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2553 static void do_info_capture(Monitor
*mon
)
2558 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2559 monitor_printf(mon
, "[%d]: ", i
);
2560 s
->ops
.info (s
->opaque
);
2565 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2568 int n
= qdict_get_int(qdict
, "n");
2571 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2573 s
->ops
.destroy (s
->opaque
);
2574 QLIST_REMOVE (s
, entries
);
2581 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2583 const char *path
= qdict_get_str(qdict
, "path");
2584 int has_freq
= qdict_haskey(qdict
, "freq");
2585 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2586 int has_bits
= qdict_haskey(qdict
, "bits");
2587 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2588 int has_channels
= qdict_haskey(qdict
, "nchannels");
2589 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2592 s
= g_malloc0 (sizeof (*s
));
2594 freq
= has_freq
? freq
: 44100;
2595 bits
= has_bits
? bits
: 16;
2596 nchannels
= has_channels
? nchannels
: 2;
2598 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2599 monitor_printf(mon
, "Failed to add wave capture\n");
2603 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2607 #if defined(TARGET_I386)
2608 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2612 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2613 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2619 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2621 qerror_report(QERR_UNSUPPORTED
);
2626 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2631 qdict
= qobject_to_qdict(data
);
2633 monitor_printf(mon
, "VM status: ");
2634 if (qdict_get_bool(qdict
, "running")) {
2635 monitor_printf(mon
, "running");
2636 if (qdict_get_bool(qdict
, "singlestep")) {
2637 monitor_printf(mon
, " (single step mode)");
2640 monitor_printf(mon
, "paused");
2643 status
= qdict_get_str(qdict
, "status");
2644 if (strcmp(status
, "paused") && strcmp(status
, "running")) {
2645 monitor_printf(mon
, " (%s)", status
);
2648 monitor_printf(mon
, "\n");
2651 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2653 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep
, runstate_as_string());
2656 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2658 qemu_acl
*acl
= qemu_acl_find(name
);
2661 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2666 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2668 const char *aclname
= qdict_get_str(qdict
, "aclname");
2669 qemu_acl
*acl
= find_acl(mon
, aclname
);
2670 qemu_acl_entry
*entry
;
2674 monitor_printf(mon
, "policy: %s\n",
2675 acl
->defaultDeny
? "deny" : "allow");
2676 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2678 monitor_printf(mon
, "%d: %s %s\n", i
,
2679 entry
->deny
? "deny" : "allow", entry
->match
);
2684 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2686 const char *aclname
= qdict_get_str(qdict
, "aclname");
2687 qemu_acl
*acl
= find_acl(mon
, aclname
);
2690 qemu_acl_reset(acl
);
2691 monitor_printf(mon
, "acl: removed all rules\n");
2695 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2697 const char *aclname
= qdict_get_str(qdict
, "aclname");
2698 const char *policy
= qdict_get_str(qdict
, "policy");
2699 qemu_acl
*acl
= find_acl(mon
, aclname
);
2702 if (strcmp(policy
, "allow") == 0) {
2703 acl
->defaultDeny
= 0;
2704 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2705 } else if (strcmp(policy
, "deny") == 0) {
2706 acl
->defaultDeny
= 1;
2707 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2709 monitor_printf(mon
, "acl: unknown policy '%s', "
2710 "expected 'deny' or 'allow'\n", policy
);
2715 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2717 const char *aclname
= qdict_get_str(qdict
, "aclname");
2718 const char *match
= qdict_get_str(qdict
, "match");
2719 const char *policy
= qdict_get_str(qdict
, "policy");
2720 int has_index
= qdict_haskey(qdict
, "index");
2721 int index
= qdict_get_try_int(qdict
, "index", -1);
2722 qemu_acl
*acl
= find_acl(mon
, aclname
);
2726 if (strcmp(policy
, "allow") == 0) {
2728 } else if (strcmp(policy
, "deny") == 0) {
2731 monitor_printf(mon
, "acl: unknown policy '%s', "
2732 "expected 'deny' or 'allow'\n", policy
);
2736 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2738 ret
= qemu_acl_append(acl
, deny
, match
);
2740 monitor_printf(mon
, "acl: unable to add acl entry\n");
2742 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2746 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2748 const char *aclname
= qdict_get_str(qdict
, "aclname");
2749 const char *match
= qdict_get_str(qdict
, "match");
2750 qemu_acl
*acl
= find_acl(mon
, aclname
);
2754 ret
= qemu_acl_remove(acl
, match
);
2756 monitor_printf(mon
, "acl: no matching acl entry\n");
2758 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2762 #if defined(TARGET_I386)
2763 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2766 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2767 int bank
= qdict_get_int(qdict
, "bank");
2768 uint64_t status
= qdict_get_int(qdict
, "status");
2769 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2770 uint64_t addr
= qdict_get_int(qdict
, "addr");
2771 uint64_t misc
= qdict_get_int(qdict
, "misc");
2772 int flags
= MCE_INJECT_UNCOND_AO
;
2774 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2775 flags
|= MCE_INJECT_BROADCAST
;
2777 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
2778 if (cenv
->cpu_index
== cpu_index
) {
2779 cpu_x86_inject_mce(mon
, cenv
, bank
, status
, mcg_status
, addr
, misc
,
2787 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2789 const char *fdname
= qdict_get_str(qdict
, "fdname");
2793 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2795 qerror_report(QERR_FD_NOT_SUPPLIED
);
2799 if (qemu_isdigit(fdname
[0])) {
2800 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2801 "a name not starting with a digit");
2805 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2806 if (strcmp(monfd
->name
, fdname
) != 0) {
2815 monfd
= g_malloc0(sizeof(mon_fd_t
));
2816 monfd
->name
= g_strdup(fdname
);
2819 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2823 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2825 const char *fdname
= qdict_get_str(qdict
, "fdname");
2828 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2829 if (strcmp(monfd
->name
, fdname
) != 0) {
2833 QLIST_REMOVE(monfd
, next
);
2835 g_free(monfd
->name
);
2840 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2844 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2846 int saved_vm_running
= runstate_is_running();
2847 const char *name
= qdict_get_str(qdict
, "name");
2849 vm_stop(RSTATE_RESTORE
);
2851 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2856 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2860 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2863 if (strcmp(monfd
->name
, fdname
) != 0) {
2869 /* caller takes ownership of fd */
2870 QLIST_REMOVE(monfd
, next
);
2871 g_free(monfd
->name
);
2880 static const mon_cmd_t mon_cmds
[] = {
2881 #include "hmp-commands.h"
2885 /* Please update hmp-commands.hx when adding or changing commands */
2886 static const mon_cmd_t info_cmds
[] = {
2891 .help
= "show the version of QEMU",
2892 .user_print
= do_info_version_print
,
2893 .mhandler
.info_new
= do_info_version
,
2899 .help
= "show the network state",
2900 .mhandler
.info
= do_info_network
,
2906 .help
= "show the character devices",
2907 .user_print
= qemu_chr_info_print
,
2908 .mhandler
.info_new
= qemu_chr_info
,
2914 .help
= "show the block devices",
2915 .user_print
= bdrv_info_print
,
2916 .mhandler
.info_new
= bdrv_info
,
2919 .name
= "blockstats",
2922 .help
= "show block device statistics",
2923 .user_print
= bdrv_stats_print
,
2924 .mhandler
.info_new
= bdrv_info_stats
,
2927 .name
= "registers",
2930 .help
= "show the cpu registers",
2931 .mhandler
.info
= do_info_registers
,
2937 .help
= "show infos for each CPU",
2938 .user_print
= monitor_print_cpus
,
2939 .mhandler
.info_new
= do_info_cpus
,
2945 .help
= "show the command line history",
2946 .mhandler
.info
= do_info_history
,
2952 .help
= "show the interrupts statistics (if available)",
2953 .mhandler
.info
= irq_info
,
2959 .help
= "show i8259 (PIC) state",
2960 .mhandler
.info
= pic_info
,
2966 .help
= "show PCI info",
2967 .user_print
= do_pci_info_print
,
2968 .mhandler
.info_new
= do_pci_info
,
2970 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2976 .help
= "show virtual to physical memory mappings",
2977 .mhandler
.info
= tlb_info
,
2980 #if defined(TARGET_I386)
2985 .help
= "show the active virtual memory mappings",
2986 .mhandler
.info
= mem_info
,
2993 .help
= "show memory tree",
2994 .mhandler
.info
= do_info_mtree
,
3000 .help
= "show dynamic compiler info",
3001 .mhandler
.info
= do_info_jit
,
3007 .help
= "show KVM information",
3008 .user_print
= do_info_kvm_print
,
3009 .mhandler
.info_new
= do_info_kvm
,
3015 .help
= "show NUMA information",
3016 .mhandler
.info
= do_info_numa
,
3022 .help
= "show guest USB devices",
3023 .mhandler
.info
= usb_info
,
3029 .help
= "show host USB devices",
3030 .mhandler
.info
= usb_host_info
,
3036 .help
= "show profiling information",
3037 .mhandler
.info
= do_info_profile
,
3043 .help
= "show capture information",
3044 .mhandler
.info
= do_info_capture
,
3047 .name
= "snapshots",
3050 .help
= "show the currently saved VM snapshots",
3051 .mhandler
.info
= do_info_snapshots
,
3057 .help
= "show the current VM status (running|paused)",
3058 .user_print
= do_info_status_print
,
3059 .mhandler
.info_new
= do_info_status
,
3065 .help
= "show guest PCMCIA status",
3066 .mhandler
.info
= pcmcia_info
,
3072 .help
= "show which guest mouse is receiving events",
3073 .user_print
= do_info_mice_print
,
3074 .mhandler
.info_new
= do_info_mice
,
3080 .help
= "show the vnc server status",
3081 .user_print
= do_info_vnc_print
,
3082 .mhandler
.info_new
= do_info_vnc
,
3084 #if defined(CONFIG_SPICE)
3089 .help
= "show the spice server status",
3090 .user_print
= do_info_spice_print
,
3091 .mhandler
.info_new
= do_info_spice
,
3098 .help
= "show the current VM name",
3099 .user_print
= do_info_name_print
,
3100 .mhandler
.info_new
= do_info_name
,
3106 .help
= "show the current VM UUID",
3107 .user_print
= do_info_uuid_print
,
3108 .mhandler
.info_new
= do_info_uuid
,
3110 #if defined(TARGET_PPC)
3115 .help
= "show CPU statistics",
3116 .mhandler
.info
= do_info_cpu_stats
,
3119 #if defined(CONFIG_SLIRP)
3124 .help
= "show user network stack connection states",
3125 .mhandler
.info
= do_info_usernet
,
3132 .help
= "show migration status",
3133 .user_print
= do_info_migrate_print
,
3134 .mhandler
.info_new
= do_info_migrate
,
3140 .help
= "show balloon information",
3141 .user_print
= monitor_print_balloon
,
3142 .mhandler
.info_async
= do_info_balloon
,
3143 .flags
= MONITOR_CMD_ASYNC
,
3149 .help
= "show device tree",
3150 .mhandler
.info
= do_info_qtree
,
3156 .help
= "show qdev device model list",
3157 .mhandler
.info
= do_info_qdm
,
3163 .help
= "show roms",
3164 .mhandler
.info
= do_info_roms
,
3166 #if defined(CONFIG_TRACE_SIMPLE)
3171 .help
= "show current contents of trace buffer",
3172 .mhandler
.info
= do_info_trace
,
3176 .name
= "trace-events",
3179 .help
= "show available trace-events & their state",
3180 .mhandler
.info
= do_trace_print_events
,
3187 static const mon_cmd_t qmp_cmds
[] = {
3188 #include "qmp-commands.h"
3192 static const mon_cmd_t qmp_query_cmds
[] = {
3197 .help
= "show the version of QEMU",
3198 .user_print
= do_info_version_print
,
3199 .mhandler
.info_new
= do_info_version
,
3205 .help
= "list QMP available commands",
3206 .user_print
= monitor_user_noop
,
3207 .mhandler
.info_new
= do_info_commands
,
3213 .help
= "show the character devices",
3214 .user_print
= qemu_chr_info_print
,
3215 .mhandler
.info_new
= qemu_chr_info
,
3221 .help
= "show the block devices",
3222 .user_print
= bdrv_info_print
,
3223 .mhandler
.info_new
= bdrv_info
,
3226 .name
= "blockstats",
3229 .help
= "show block device statistics",
3230 .user_print
= bdrv_stats_print
,
3231 .mhandler
.info_new
= bdrv_info_stats
,
3237 .help
= "show infos for each CPU",
3238 .user_print
= monitor_print_cpus
,
3239 .mhandler
.info_new
= do_info_cpus
,
3245 .help
= "show PCI info",
3246 .user_print
= do_pci_info_print
,
3247 .mhandler
.info_new
= do_pci_info
,
3253 .help
= "show KVM information",
3254 .user_print
= do_info_kvm_print
,
3255 .mhandler
.info_new
= do_info_kvm
,
3261 .help
= "show the current VM status (running|paused)",
3262 .user_print
= do_info_status_print
,
3263 .mhandler
.info_new
= do_info_status
,
3269 .help
= "show which guest mouse is receiving events",
3270 .user_print
= do_info_mice_print
,
3271 .mhandler
.info_new
= do_info_mice
,
3277 .help
= "show the vnc server status",
3278 .user_print
= do_info_vnc_print
,
3279 .mhandler
.info_new
= do_info_vnc
,
3281 #if defined(CONFIG_SPICE)
3286 .help
= "show the spice server status",
3287 .user_print
= do_info_spice_print
,
3288 .mhandler
.info_new
= do_info_spice
,
3295 .help
= "show the current VM name",
3296 .user_print
= do_info_name_print
,
3297 .mhandler
.info_new
= do_info_name
,
3303 .help
= "show the current VM UUID",
3304 .user_print
= do_info_uuid_print
,
3305 .mhandler
.info_new
= do_info_uuid
,
3311 .help
= "show migration status",
3312 .user_print
= do_info_migrate_print
,
3313 .mhandler
.info_new
= do_info_migrate
,
3319 .help
= "show balloon information",
3320 .user_print
= monitor_print_balloon
,
3321 .mhandler
.info_async
= do_info_balloon
,
3322 .flags
= MONITOR_CMD_ASYNC
,
3327 /*******************************************************************/
3329 static const char *pch
;
3330 static jmp_buf expr_env
;
3335 typedef struct MonitorDef
{
3338 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
3342 #if defined(TARGET_I386)
3343 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
3345 CPUState
*env
= mon_get_cpu();
3346 return env
->eip
+ env
->segs
[R_CS
].base
;
3350 #if defined(TARGET_PPC)
3351 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3353 CPUState
*env
= mon_get_cpu();
3358 for (i
= 0; i
< 8; i
++)
3359 u
|= env
->crf
[i
] << (32 - (4 * i
));
3364 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3366 CPUState
*env
= mon_get_cpu();
3370 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3372 CPUState
*env
= mon_get_cpu();
3376 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3378 CPUState
*env
= mon_get_cpu();
3379 return cpu_ppc_load_decr(env
);
3382 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3384 CPUState
*env
= mon_get_cpu();
3385 return cpu_ppc_load_tbu(env
);
3388 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3390 CPUState
*env
= mon_get_cpu();
3391 return cpu_ppc_load_tbl(env
);
3395 #if defined(TARGET_SPARC)
3396 #ifndef TARGET_SPARC64
3397 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3399 CPUState
*env
= mon_get_cpu();
3401 return cpu_get_psr(env
);
3405 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3407 CPUState
*env
= mon_get_cpu();
3408 return env
->regwptr
[val
];
3412 static const MonitorDef monitor_defs
[] = {
3415 #define SEG(name, seg) \
3416 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3417 { name ".base", offsetof(CPUState, segs[seg].base) },\
3418 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3420 { "eax", offsetof(CPUState
, regs
[0]) },
3421 { "ecx", offsetof(CPUState
, regs
[1]) },
3422 { "edx", offsetof(CPUState
, regs
[2]) },
3423 { "ebx", offsetof(CPUState
, regs
[3]) },
3424 { "esp|sp", offsetof(CPUState
, regs
[4]) },
3425 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
3426 { "esi", offsetof(CPUState
, regs
[6]) },
3427 { "edi", offsetof(CPUState
, regs
[7]) },
3428 #ifdef TARGET_X86_64
3429 { "r8", offsetof(CPUState
, regs
[8]) },
3430 { "r9", offsetof(CPUState
, regs
[9]) },
3431 { "r10", offsetof(CPUState
, regs
[10]) },
3432 { "r11", offsetof(CPUState
, regs
[11]) },
3433 { "r12", offsetof(CPUState
, regs
[12]) },
3434 { "r13", offsetof(CPUState
, regs
[13]) },
3435 { "r14", offsetof(CPUState
, regs
[14]) },
3436 { "r15", offsetof(CPUState
, regs
[15]) },
3438 { "eflags", offsetof(CPUState
, eflags
) },
3439 { "eip", offsetof(CPUState
, eip
) },
3446 { "pc", 0, monitor_get_pc
, },
3447 #elif defined(TARGET_PPC)
3448 /* General purpose registers */
3449 { "r0", offsetof(CPUState
, gpr
[0]) },
3450 { "r1", offsetof(CPUState
, gpr
[1]) },
3451 { "r2", offsetof(CPUState
, gpr
[2]) },
3452 { "r3", offsetof(CPUState
, gpr
[3]) },
3453 { "r4", offsetof(CPUState
, gpr
[4]) },
3454 { "r5", offsetof(CPUState
, gpr
[5]) },
3455 { "r6", offsetof(CPUState
, gpr
[6]) },
3456 { "r7", offsetof(CPUState
, gpr
[7]) },
3457 { "r8", offsetof(CPUState
, gpr
[8]) },
3458 { "r9", offsetof(CPUState
, gpr
[9]) },
3459 { "r10", offsetof(CPUState
, gpr
[10]) },
3460 { "r11", offsetof(CPUState
, gpr
[11]) },
3461 { "r12", offsetof(CPUState
, gpr
[12]) },
3462 { "r13", offsetof(CPUState
, gpr
[13]) },
3463 { "r14", offsetof(CPUState
, gpr
[14]) },
3464 { "r15", offsetof(CPUState
, gpr
[15]) },
3465 { "r16", offsetof(CPUState
, gpr
[16]) },
3466 { "r17", offsetof(CPUState
, gpr
[17]) },
3467 { "r18", offsetof(CPUState
, gpr
[18]) },
3468 { "r19", offsetof(CPUState
, gpr
[19]) },
3469 { "r20", offsetof(CPUState
, gpr
[20]) },
3470 { "r21", offsetof(CPUState
, gpr
[21]) },
3471 { "r22", offsetof(CPUState
, gpr
[22]) },
3472 { "r23", offsetof(CPUState
, gpr
[23]) },
3473 { "r24", offsetof(CPUState
, gpr
[24]) },
3474 { "r25", offsetof(CPUState
, gpr
[25]) },
3475 { "r26", offsetof(CPUState
, gpr
[26]) },
3476 { "r27", offsetof(CPUState
, gpr
[27]) },
3477 { "r28", offsetof(CPUState
, gpr
[28]) },
3478 { "r29", offsetof(CPUState
, gpr
[29]) },
3479 { "r30", offsetof(CPUState
, gpr
[30]) },
3480 { "r31", offsetof(CPUState
, gpr
[31]) },
3481 /* Floating point registers */
3482 { "f0", offsetof(CPUState
, fpr
[0]) },
3483 { "f1", offsetof(CPUState
, fpr
[1]) },
3484 { "f2", offsetof(CPUState
, fpr
[2]) },
3485 { "f3", offsetof(CPUState
, fpr
[3]) },
3486 { "f4", offsetof(CPUState
, fpr
[4]) },
3487 { "f5", offsetof(CPUState
, fpr
[5]) },
3488 { "f6", offsetof(CPUState
, fpr
[6]) },
3489 { "f7", offsetof(CPUState
, fpr
[7]) },
3490 { "f8", offsetof(CPUState
, fpr
[8]) },
3491 { "f9", offsetof(CPUState
, fpr
[9]) },
3492 { "f10", offsetof(CPUState
, fpr
[10]) },
3493 { "f11", offsetof(CPUState
, fpr
[11]) },
3494 { "f12", offsetof(CPUState
, fpr
[12]) },
3495 { "f13", offsetof(CPUState
, fpr
[13]) },
3496 { "f14", offsetof(CPUState
, fpr
[14]) },
3497 { "f15", offsetof(CPUState
, fpr
[15]) },
3498 { "f16", offsetof(CPUState
, fpr
[16]) },
3499 { "f17", offsetof(CPUState
, fpr
[17]) },
3500 { "f18", offsetof(CPUState
, fpr
[18]) },
3501 { "f19", offsetof(CPUState
, fpr
[19]) },
3502 { "f20", offsetof(CPUState
, fpr
[20]) },
3503 { "f21", offsetof(CPUState
, fpr
[21]) },
3504 { "f22", offsetof(CPUState
, fpr
[22]) },
3505 { "f23", offsetof(CPUState
, fpr
[23]) },
3506 { "f24", offsetof(CPUState
, fpr
[24]) },
3507 { "f25", offsetof(CPUState
, fpr
[25]) },
3508 { "f26", offsetof(CPUState
, fpr
[26]) },
3509 { "f27", offsetof(CPUState
, fpr
[27]) },
3510 { "f28", offsetof(CPUState
, fpr
[28]) },
3511 { "f29", offsetof(CPUState
, fpr
[29]) },
3512 { "f30", offsetof(CPUState
, fpr
[30]) },
3513 { "f31", offsetof(CPUState
, fpr
[31]) },
3514 { "fpscr", offsetof(CPUState
, fpscr
) },
3515 /* Next instruction pointer */
3516 { "nip|pc", offsetof(CPUState
, nip
) },
3517 { "lr", offsetof(CPUState
, lr
) },
3518 { "ctr", offsetof(CPUState
, ctr
) },
3519 { "decr", 0, &monitor_get_decr
, },
3520 { "ccr", 0, &monitor_get_ccr
, },
3521 /* Machine state register */
3522 { "msr", 0, &monitor_get_msr
, },
3523 { "xer", 0, &monitor_get_xer
, },
3524 { "tbu", 0, &monitor_get_tbu
, },
3525 { "tbl", 0, &monitor_get_tbl
, },
3526 #if defined(TARGET_PPC64)
3527 /* Address space register */
3528 { "asr", offsetof(CPUState
, asr
) },
3530 /* Segment registers */
3531 { "sdr1", offsetof(CPUState
, spr
[SPR_SDR1
]) },
3532 { "sr0", offsetof(CPUState
, sr
[0]) },
3533 { "sr1", offsetof(CPUState
, sr
[1]) },
3534 { "sr2", offsetof(CPUState
, sr
[2]) },
3535 { "sr3", offsetof(CPUState
, sr
[3]) },
3536 { "sr4", offsetof(CPUState
, sr
[4]) },
3537 { "sr5", offsetof(CPUState
, sr
[5]) },
3538 { "sr6", offsetof(CPUState
, sr
[6]) },
3539 { "sr7", offsetof(CPUState
, sr
[7]) },
3540 { "sr8", offsetof(CPUState
, sr
[8]) },
3541 { "sr9", offsetof(CPUState
, sr
[9]) },
3542 { "sr10", offsetof(CPUState
, sr
[10]) },
3543 { "sr11", offsetof(CPUState
, sr
[11]) },
3544 { "sr12", offsetof(CPUState
, sr
[12]) },
3545 { "sr13", offsetof(CPUState
, sr
[13]) },
3546 { "sr14", offsetof(CPUState
, sr
[14]) },
3547 { "sr15", offsetof(CPUState
, sr
[15]) },
3548 /* Too lazy to put BATs... */
3549 { "pvr", offsetof(CPUState
, spr
[SPR_PVR
]) },
3551 { "srr0", offsetof(CPUState
, spr
[SPR_SRR0
]) },
3552 { "srr1", offsetof(CPUState
, spr
[SPR_SRR1
]) },
3553 { "sprg0", offsetof(CPUState
, spr
[SPR_SPRG0
]) },
3554 { "sprg1", offsetof(CPUState
, spr
[SPR_SPRG1
]) },
3555 { "sprg2", offsetof(CPUState
, spr
[SPR_SPRG2
]) },
3556 { "sprg3", offsetof(CPUState
, spr
[SPR_SPRG3
]) },
3557 { "sprg4", offsetof(CPUState
, spr
[SPR_SPRG4
]) },
3558 { "sprg5", offsetof(CPUState
, spr
[SPR_SPRG5
]) },
3559 { "sprg6", offsetof(CPUState
, spr
[SPR_SPRG6
]) },
3560 { "sprg7", offsetof(CPUState
, spr
[SPR_SPRG7
]) },
3561 { "pid", offsetof(CPUState
, spr
[SPR_BOOKE_PID
]) },
3562 { "csrr0", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR0
]) },
3563 { "csrr1", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR1
]) },
3564 { "esr", offsetof(CPUState
, spr
[SPR_BOOKE_ESR
]) },
3565 { "dear", offsetof(CPUState
, spr
[SPR_BOOKE_DEAR
]) },
3566 { "mcsr", offsetof(CPUState
, spr
[SPR_BOOKE_MCSR
]) },
3567 { "tsr", offsetof(CPUState
, spr
[SPR_BOOKE_TSR
]) },
3568 { "tcr", offsetof(CPUState
, spr
[SPR_BOOKE_TCR
]) },
3569 { "vrsave", offsetof(CPUState
, spr
[SPR_VRSAVE
]) },
3570 { "pir", offsetof(CPUState
, spr
[SPR_BOOKE_PIR
]) },
3571 { "mcsrr0", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR0
]) },
3572 { "mcsrr1", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR1
]) },
3573 { "decar", offsetof(CPUState
, spr
[SPR_BOOKE_DECAR
]) },
3574 { "ivpr", offsetof(CPUState
, spr
[SPR_BOOKE_IVPR
]) },
3575 { "epcr", offsetof(CPUState
, spr
[SPR_BOOKE_EPCR
]) },
3576 { "sprg8", offsetof(CPUState
, spr
[SPR_BOOKE_SPRG8
]) },
3577 { "ivor0", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR0
]) },
3578 { "ivor1", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR1
]) },
3579 { "ivor2", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR2
]) },
3580 { "ivor3", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR3
]) },
3581 { "ivor4", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR4
]) },
3582 { "ivor5", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR5
]) },
3583 { "ivor6", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR6
]) },
3584 { "ivor7", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR7
]) },
3585 { "ivor8", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR8
]) },
3586 { "ivor9", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR9
]) },
3587 { "ivor10", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR10
]) },
3588 { "ivor11", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR11
]) },
3589 { "ivor12", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR12
]) },
3590 { "ivor13", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR13
]) },
3591 { "ivor14", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR14
]) },
3592 { "ivor15", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR15
]) },
3593 { "ivor32", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR32
]) },
3594 { "ivor33", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR33
]) },
3595 { "ivor34", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR34
]) },
3596 { "ivor35", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR35
]) },
3597 { "ivor36", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR36
]) },
3598 { "ivor37", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR37
]) },
3599 { "mas0", offsetof(CPUState
, spr
[SPR_BOOKE_MAS0
]) },
3600 { "mas1", offsetof(CPUState
, spr
[SPR_BOOKE_MAS1
]) },
3601 { "mas2", offsetof(CPUState
, spr
[SPR_BOOKE_MAS2
]) },
3602 { "mas3", offsetof(CPUState
, spr
[SPR_BOOKE_MAS3
]) },
3603 { "mas4", offsetof(CPUState
, spr
[SPR_BOOKE_MAS4
]) },
3604 { "mas6", offsetof(CPUState
, spr
[SPR_BOOKE_MAS6
]) },
3605 { "mas7", offsetof(CPUState
, spr
[SPR_BOOKE_MAS7
]) },
3606 { "mmucfg", offsetof(CPUState
, spr
[SPR_MMUCFG
]) },
3607 { "tlb0cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3608 { "tlb1cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3609 { "epr", offsetof(CPUState
, spr
[SPR_BOOKE_EPR
]) },
3610 { "eplc", offsetof(CPUState
, spr
[SPR_BOOKE_EPLC
]) },
3611 { "epsc", offsetof(CPUState
, spr
[SPR_BOOKE_EPSC
]) },
3612 { "svr", offsetof(CPUState
, spr
[SPR_E500_SVR
]) },
3613 { "mcar", offsetof(CPUState
, spr
[SPR_Exxx_MCAR
]) },
3614 { "pid1", offsetof(CPUState
, spr
[SPR_BOOKE_PID1
]) },
3615 { "pid2", offsetof(CPUState
, spr
[SPR_BOOKE_PID2
]) },
3616 { "hid0", offsetof(CPUState
, spr
[SPR_HID0
]) },
3618 #elif defined(TARGET_SPARC)
3619 { "g0", offsetof(CPUState
, gregs
[0]) },
3620 { "g1", offsetof(CPUState
, gregs
[1]) },
3621 { "g2", offsetof(CPUState
, gregs
[2]) },
3622 { "g3", offsetof(CPUState
, gregs
[3]) },
3623 { "g4", offsetof(CPUState
, gregs
[4]) },
3624 { "g5", offsetof(CPUState
, gregs
[5]) },
3625 { "g6", offsetof(CPUState
, gregs
[6]) },
3626 { "g7", offsetof(CPUState
, gregs
[7]) },
3627 { "o0", 0, monitor_get_reg
},
3628 { "o1", 1, monitor_get_reg
},
3629 { "o2", 2, monitor_get_reg
},
3630 { "o3", 3, monitor_get_reg
},
3631 { "o4", 4, monitor_get_reg
},
3632 { "o5", 5, monitor_get_reg
},
3633 { "o6", 6, monitor_get_reg
},
3634 { "o7", 7, monitor_get_reg
},
3635 { "l0", 8, monitor_get_reg
},
3636 { "l1", 9, monitor_get_reg
},
3637 { "l2", 10, monitor_get_reg
},
3638 { "l3", 11, monitor_get_reg
},
3639 { "l4", 12, monitor_get_reg
},
3640 { "l5", 13, monitor_get_reg
},
3641 { "l6", 14, monitor_get_reg
},
3642 { "l7", 15, monitor_get_reg
},
3643 { "i0", 16, monitor_get_reg
},
3644 { "i1", 17, monitor_get_reg
},
3645 { "i2", 18, monitor_get_reg
},
3646 { "i3", 19, monitor_get_reg
},
3647 { "i4", 20, monitor_get_reg
},
3648 { "i5", 21, monitor_get_reg
},
3649 { "i6", 22, monitor_get_reg
},
3650 { "i7", 23, monitor_get_reg
},
3651 { "pc", offsetof(CPUState
, pc
) },
3652 { "npc", offsetof(CPUState
, npc
) },
3653 { "y", offsetof(CPUState
, y
) },
3654 #ifndef TARGET_SPARC64
3655 { "psr", 0, &monitor_get_psr
, },
3656 { "wim", offsetof(CPUState
, wim
) },
3658 { "tbr", offsetof(CPUState
, tbr
) },
3659 { "fsr", offsetof(CPUState
, fsr
) },
3660 { "f0", offsetof(CPUState
, fpr
[0]) },
3661 { "f1", offsetof(CPUState
, fpr
[1]) },
3662 { "f2", offsetof(CPUState
, fpr
[2]) },
3663 { "f3", offsetof(CPUState
, fpr
[3]) },
3664 { "f4", offsetof(CPUState
, fpr
[4]) },
3665 { "f5", offsetof(CPUState
, fpr
[5]) },
3666 { "f6", offsetof(CPUState
, fpr
[6]) },
3667 { "f7", offsetof(CPUState
, fpr
[7]) },
3668 { "f8", offsetof(CPUState
, fpr
[8]) },
3669 { "f9", offsetof(CPUState
, fpr
[9]) },
3670 { "f10", offsetof(CPUState
, fpr
[10]) },
3671 { "f11", offsetof(CPUState
, fpr
[11]) },
3672 { "f12", offsetof(CPUState
, fpr
[12]) },
3673 { "f13", offsetof(CPUState
, fpr
[13]) },
3674 { "f14", offsetof(CPUState
, fpr
[14]) },
3675 { "f15", offsetof(CPUState
, fpr
[15]) },
3676 { "f16", offsetof(CPUState
, fpr
[16]) },
3677 { "f17", offsetof(CPUState
, fpr
[17]) },
3678 { "f18", offsetof(CPUState
, fpr
[18]) },
3679 { "f19", offsetof(CPUState
, fpr
[19]) },
3680 { "f20", offsetof(CPUState
, fpr
[20]) },
3681 { "f21", offsetof(CPUState
, fpr
[21]) },
3682 { "f22", offsetof(CPUState
, fpr
[22]) },
3683 { "f23", offsetof(CPUState
, fpr
[23]) },
3684 { "f24", offsetof(CPUState
, fpr
[24]) },
3685 { "f25", offsetof(CPUState
, fpr
[25]) },
3686 { "f26", offsetof(CPUState
, fpr
[26]) },
3687 { "f27", offsetof(CPUState
, fpr
[27]) },
3688 { "f28", offsetof(CPUState
, fpr
[28]) },
3689 { "f29", offsetof(CPUState
, fpr
[29]) },
3690 { "f30", offsetof(CPUState
, fpr
[30]) },
3691 { "f31", offsetof(CPUState
, fpr
[31]) },
3692 #ifdef TARGET_SPARC64
3693 { "f32", offsetof(CPUState
, fpr
[32]) },
3694 { "f34", offsetof(CPUState
, fpr
[34]) },
3695 { "f36", offsetof(CPUState
, fpr
[36]) },
3696 { "f38", offsetof(CPUState
, fpr
[38]) },
3697 { "f40", offsetof(CPUState
, fpr
[40]) },
3698 { "f42", offsetof(CPUState
, fpr
[42]) },
3699 { "f44", offsetof(CPUState
, fpr
[44]) },
3700 { "f46", offsetof(CPUState
, fpr
[46]) },
3701 { "f48", offsetof(CPUState
, fpr
[48]) },
3702 { "f50", offsetof(CPUState
, fpr
[50]) },
3703 { "f52", offsetof(CPUState
, fpr
[52]) },
3704 { "f54", offsetof(CPUState
, fpr
[54]) },
3705 { "f56", offsetof(CPUState
, fpr
[56]) },
3706 { "f58", offsetof(CPUState
, fpr
[58]) },
3707 { "f60", offsetof(CPUState
, fpr
[60]) },
3708 { "f62", offsetof(CPUState
, fpr
[62]) },
3709 { "asi", offsetof(CPUState
, asi
) },
3710 { "pstate", offsetof(CPUState
, pstate
) },
3711 { "cansave", offsetof(CPUState
, cansave
) },
3712 { "canrestore", offsetof(CPUState
, canrestore
) },
3713 { "otherwin", offsetof(CPUState
, otherwin
) },
3714 { "wstate", offsetof(CPUState
, wstate
) },
3715 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3716 { "fprs", offsetof(CPUState
, fprs
) },
3722 static void expr_error(Monitor
*mon
, const char *msg
)
3724 monitor_printf(mon
, "%s\n", msg
);
3725 longjmp(expr_env
, 1);
3728 /* return 0 if OK, -1 if not found */
3729 static int get_monitor_def(target_long
*pval
, const char *name
)
3731 const MonitorDef
*md
;
3734 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3735 if (compare_cmd(name
, md
->name
)) {
3736 if (md
->get_value
) {
3737 *pval
= md
->get_value(md
, md
->offset
);
3739 CPUState
*env
= mon_get_cpu();
3740 ptr
= (uint8_t *)env
+ md
->offset
;
3743 *pval
= *(int32_t *)ptr
;
3746 *pval
= *(target_long
*)ptr
;
3759 static void next(void)
3763 while (qemu_isspace(*pch
))
3768 static int64_t expr_sum(Monitor
*mon
);
3770 static int64_t expr_unary(Monitor
*mon
)
3779 n
= expr_unary(mon
);
3783 n
= -expr_unary(mon
);
3787 n
= ~expr_unary(mon
);
3793 expr_error(mon
, "')' expected");
3800 expr_error(mon
, "character constant expected");
3804 expr_error(mon
, "missing terminating \' character");
3814 while ((*pch
>= 'a' && *pch
<= 'z') ||
3815 (*pch
>= 'A' && *pch
<= 'Z') ||
3816 (*pch
>= '0' && *pch
<= '9') ||
3817 *pch
== '_' || *pch
== '.') {
3818 if ((q
- buf
) < sizeof(buf
) - 1)
3822 while (qemu_isspace(*pch
))
3825 ret
= get_monitor_def(®
, buf
);
3827 expr_error(mon
, "unknown register");
3832 expr_error(mon
, "unexpected end of expression");
3836 #if TARGET_PHYS_ADDR_BITS > 32
3837 n
= strtoull(pch
, &p
, 0);
3839 n
= strtoul(pch
, &p
, 0);
3842 expr_error(mon
, "invalid char in expression");
3845 while (qemu_isspace(*pch
))
3853 static int64_t expr_prod(Monitor
*mon
)
3858 val
= expr_unary(mon
);
3861 if (op
!= '*' && op
!= '/' && op
!= '%')
3864 val2
= expr_unary(mon
);
3873 expr_error(mon
, "division by zero");
3884 static int64_t expr_logic(Monitor
*mon
)
3889 val
= expr_prod(mon
);
3892 if (op
!= '&' && op
!= '|' && op
!= '^')
3895 val2
= expr_prod(mon
);
3912 static int64_t expr_sum(Monitor
*mon
)
3917 val
= expr_logic(mon
);
3920 if (op
!= '+' && op
!= '-')
3923 val2
= expr_logic(mon
);
3932 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3935 if (setjmp(expr_env
)) {
3939 while (qemu_isspace(*pch
))
3941 *pval
= expr_sum(mon
);
3946 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3948 const char *p
= *pp
;
3952 d
= strtod(p
, &tailp
);
3954 monitor_printf(mon
, "Number expected\n");
3957 if (d
!= d
|| d
- d
!= 0) {
3958 /* NaN or infinity */
3959 monitor_printf(mon
, "Bad number\n");
3967 static int get_str(char *buf
, int buf_size
, const char **pp
)
3975 while (qemu_isspace(*p
))
3985 while (*p
!= '\0' && *p
!= '\"') {
4001 qemu_printf("unsupported escape code: '\\%c'\n", c
);
4004 if ((q
- buf
) < buf_size
- 1) {
4008 if ((q
- buf
) < buf_size
- 1) {
4015 qemu_printf("unterminated string\n");
4020 while (*p
!= '\0' && !qemu_isspace(*p
)) {
4021 if ((q
- buf
) < buf_size
- 1) {
4033 * Store the command-name in cmdname, and return a pointer to
4034 * the remaining of the command string.
4036 static const char *get_command_name(const char *cmdline
,
4037 char *cmdname
, size_t nlen
)
4040 const char *p
, *pstart
;
4043 while (qemu_isspace(*p
))
4048 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
4053 memcpy(cmdname
, pstart
, len
);
4054 cmdname
[len
] = '\0';
4059 * Read key of 'type' into 'key' and return the current
4062 static char *key_get_info(const char *type
, char **key
)
4070 p
= strchr(type
, ':');
4077 str
= g_malloc(len
+ 1);
4078 memcpy(str
, type
, len
);
4085 static int default_fmt_format
= 'x';
4086 static int default_fmt_size
= 4;
4090 static int is_valid_option(const char *c
, const char *typestr
)
4098 typestr
= strstr(typestr
, option
);
4099 return (typestr
!= NULL
);
4102 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
4103 const char *cmdname
)
4105 const mon_cmd_t
*cmd
;
4107 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
4108 if (compare_cmd(cmdname
, cmd
->name
)) {
4116 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
4118 return search_dispatch_table(mon_cmds
, cmdname
);
4121 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
4123 return search_dispatch_table(qmp_query_cmds
, info_item
);
4126 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
4128 return search_dispatch_table(qmp_cmds
, cmdname
);
4131 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
4132 const char *cmdline
,
4135 const char *p
, *typestr
;
4137 const mon_cmd_t
*cmd
;
4143 monitor_printf(mon
, "command='%s'\n", cmdline
);
4146 /* extract the command name */
4147 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
4151 cmd
= monitor_find_command(cmdname
);
4153 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
4157 /* parse the parameters */
4158 typestr
= cmd
->args_type
;
4160 typestr
= key_get_info(typestr
, &key
);
4172 while (qemu_isspace(*p
))
4174 if (*typestr
== '?') {
4177 /* no optional string: NULL argument */
4181 ret
= get_str(buf
, sizeof(buf
), &p
);
4185 monitor_printf(mon
, "%s: filename expected\n",
4189 monitor_printf(mon
, "%s: block device name expected\n",
4193 monitor_printf(mon
, "%s: string expected\n", cmdname
);
4198 qdict_put(qdict
, key
, qstring_from_str(buf
));
4203 QemuOptsList
*opts_list
;
4206 opts_list
= qemu_find_opts(key
);
4207 if (!opts_list
|| opts_list
->desc
->name
) {
4210 while (qemu_isspace(*p
)) {
4215 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
4218 opts
= qemu_opts_parse(opts_list
, buf
, 1);
4222 qemu_opts_to_qdict(opts
, qdict
);
4223 qemu_opts_del(opts
);
4228 int count
, format
, size
;
4230 while (qemu_isspace(*p
))
4236 if (qemu_isdigit(*p
)) {
4238 while (qemu_isdigit(*p
)) {
4239 count
= count
* 10 + (*p
- '0');
4277 if (*p
!= '\0' && !qemu_isspace(*p
)) {
4278 monitor_printf(mon
, "invalid char in format: '%c'\n",
4283 format
= default_fmt_format
;
4284 if (format
!= 'i') {
4285 /* for 'i', not specifying a size gives -1 as size */
4287 size
= default_fmt_size
;
4288 default_fmt_size
= size
;
4290 default_fmt_format
= format
;
4293 format
= default_fmt_format
;
4294 if (format
!= 'i') {
4295 size
= default_fmt_size
;
4300 qdict_put(qdict
, "count", qint_from_int(count
));
4301 qdict_put(qdict
, "format", qint_from_int(format
));
4302 qdict_put(qdict
, "size", qint_from_int(size
));
4311 while (qemu_isspace(*p
))
4313 if (*typestr
== '?' || *typestr
== '.') {
4314 if (*typestr
== '?') {
4322 while (qemu_isspace(*p
))
4331 if (get_expr(mon
, &val
, &p
))
4333 /* Check if 'i' is greater than 32-bit */
4334 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
4335 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
4336 monitor_printf(mon
, "integer is for 32-bit values\n");
4338 } else if (c
== 'M') {
4341 qdict_put(qdict
, key
, qint_from_int(val
));
4349 while (qemu_isspace(*p
)) {
4352 if (*typestr
== '?') {
4358 val
= strtosz(p
, &end
);
4360 monitor_printf(mon
, "invalid size\n");
4363 qdict_put(qdict
, key
, qint_from_int(val
));
4371 while (qemu_isspace(*p
))
4373 if (*typestr
== '?') {
4379 if (get_double(mon
, &val
, &p
) < 0) {
4382 if (p
[0] && p
[1] == 's') {
4385 val
/= 1e3
; p
+= 2; break;
4387 val
/= 1e6
; p
+= 2; break;
4389 val
/= 1e9
; p
+= 2; break;
4392 if (*p
&& !qemu_isspace(*p
)) {
4393 monitor_printf(mon
, "Unknown unit suffix\n");
4396 qdict_put(qdict
, key
, qfloat_from_double(val
));
4404 while (qemu_isspace(*p
)) {
4408 while (qemu_isgraph(*p
)) {
4411 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4413 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4416 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4419 qdict_put(qdict
, key
, qbool_from_int(val
));
4424 const char *tmp
= p
;
4431 while (qemu_isspace(*p
))
4436 if(!is_valid_option(p
, typestr
)) {
4438 monitor_printf(mon
, "%s: unsupported option -%c\n",
4450 qdict_put(qdict
, key
, qbool_from_int(1));
4457 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4463 /* check that all arguments were parsed */
4464 while (qemu_isspace(*p
))
4467 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4479 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4481 /* report only the first error */
4483 mon
->error
= qerror
;
4485 MON_DEBUG("Additional error report at %s:%d\n",
4486 qerror
->file
, qerror
->linenr
);
4491 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4493 if (ret
&& !monitor_has_error(mon
)) {
4495 * If it returns failure, it must have passed on error.
4497 * Action: Report an internal error to the client if in QMP.
4499 qerror_report(QERR_UNDEFINED_ERROR
);
4500 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4504 #ifdef CONFIG_DEBUG_MONITOR
4505 if (!ret
&& monitor_has_error(mon
)) {
4507 * If it returns success, it must not have passed an error.
4509 * Action: Report the passed error to the client.
4511 MON_DEBUG("command '%s' returned success but passed an error\n",
4515 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
4517 * Handlers should not call Monitor print functions.
4519 * Action: Ignore them in QMP.
4521 * (XXX: we don't check any 'info' or 'query' command here
4522 * because the user print function _is_ called by do_info(), hence
4523 * we will trigger this check. This problem will go away when we
4524 * make 'query' commands real and kill do_info())
4526 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4527 cmd
->name
, mon_print_count_get(mon
));
4532 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4535 const mon_cmd_t
*cmd
;
4537 qdict
= qdict_new();
4539 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4543 if (handler_is_async(cmd
)) {
4544 user_async_cmd_handler(mon
, cmd
, qdict
);
4545 } else if (handler_is_qobject(cmd
)) {
4546 QObject
*data
= NULL
;
4548 /* XXX: ignores the error code */
4549 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4550 assert(!monitor_has_error(mon
));
4552 cmd
->user_print(mon
, data
);
4553 qobject_decref(data
);
4556 cmd
->mhandler
.cmd(mon
, qdict
);
4563 static void cmd_completion(const char *name
, const char *list
)
4565 const char *p
, *pstart
;
4574 p
= pstart
+ strlen(pstart
);
4576 if (len
> sizeof(cmd
) - 2)
4577 len
= sizeof(cmd
) - 2;
4578 memcpy(cmd
, pstart
, len
);
4580 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4581 readline_add_completion(cur_mon
->rs
, cmd
);
4589 static void file_completion(const char *input
)
4594 char file
[1024], file_prefix
[1024];
4598 p
= strrchr(input
, '/');
4601 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4602 pstrcpy(path
, sizeof(path
), ".");
4604 input_path_len
= p
- input
+ 1;
4605 memcpy(path
, input
, input_path_len
);
4606 if (input_path_len
> sizeof(path
) - 1)
4607 input_path_len
= sizeof(path
) - 1;
4608 path
[input_path_len
] = '\0';
4609 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4611 #ifdef DEBUG_COMPLETION
4612 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4613 input
, path
, file_prefix
);
4615 ffs
= opendir(path
);
4624 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4628 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4629 memcpy(file
, input
, input_path_len
);
4630 if (input_path_len
< sizeof(file
))
4631 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4633 /* stat the file to find out if it's a directory.
4634 * In that case add a slash to speed up typing long paths
4637 if(S_ISDIR(sb
.st_mode
))
4638 pstrcat(file
, sizeof(file
), "/");
4639 readline_add_completion(cur_mon
->rs
, file
);
4645 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4647 const char *name
= bdrv_get_device_name(bs
);
4648 const char *input
= opaque
;
4650 if (input
[0] == '\0' ||
4651 !strncmp(name
, (char *)input
, strlen(input
))) {
4652 readline_add_completion(cur_mon
->rs
, name
);
4656 /* NOTE: this parser is an approximate form of the real command parser */
4657 static void parse_cmdline(const char *cmdline
,
4658 int *pnb_args
, char **args
)
4667 while (qemu_isspace(*p
))
4671 if (nb_args
>= MAX_ARGS
)
4673 ret
= get_str(buf
, sizeof(buf
), &p
);
4674 args
[nb_args
] = g_strdup(buf
);
4679 *pnb_args
= nb_args
;
4682 static const char *next_arg_type(const char *typestr
)
4684 const char *p
= strchr(typestr
, ':');
4685 return (p
!= NULL
? ++p
: typestr
);
4688 static void monitor_find_completion(const char *cmdline
)
4690 const char *cmdname
;
4691 char *args
[MAX_ARGS
];
4692 int nb_args
, i
, len
;
4693 const char *ptype
, *str
;
4694 const mon_cmd_t
*cmd
;
4697 parse_cmdline(cmdline
, &nb_args
, args
);
4698 #ifdef DEBUG_COMPLETION
4699 for(i
= 0; i
< nb_args
; i
++) {
4700 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4704 /* if the line ends with a space, it means we want to complete the
4706 len
= strlen(cmdline
);
4707 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4708 if (nb_args
>= MAX_ARGS
) {
4711 args
[nb_args
++] = g_strdup("");
4714 /* command completion */
4719 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4720 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4721 cmd_completion(cmdname
, cmd
->name
);
4724 /* find the command */
4725 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4726 if (compare_cmd(args
[0], cmd
->name
)) {
4734 ptype
= next_arg_type(cmd
->args_type
);
4735 for(i
= 0; i
< nb_args
- 2; i
++) {
4736 if (*ptype
!= '\0') {
4737 ptype
= next_arg_type(ptype
);
4738 while (*ptype
== '?')
4739 ptype
= next_arg_type(ptype
);
4742 str
= args
[nb_args
- 1];
4743 if (*ptype
== '-' && ptype
[1] != '\0') {
4744 ptype
= next_arg_type(ptype
);
4748 /* file completion */
4749 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4750 file_completion(str
);
4753 /* block device name completion */
4754 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4755 bdrv_iterate(block_completion_it
, (void *)str
);
4758 /* XXX: more generic ? */
4759 if (!strcmp(cmd
->name
, "info")) {
4760 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4761 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4762 cmd_completion(str
, cmd
->name
);
4764 } else if (!strcmp(cmd
->name
, "sendkey")) {
4765 char *sep
= strrchr(str
, '-');
4768 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4769 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4770 cmd_completion(str
, key
->name
);
4772 } else if (!strcmp(cmd
->name
, "help|?")) {
4773 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4774 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4775 cmd_completion(str
, cmd
->name
);
4785 for (i
= 0; i
< nb_args
; i
++) {
4790 static int monitor_can_read(void *opaque
)
4792 Monitor
*mon
= opaque
;
4794 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4797 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4799 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4800 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4804 * Argument validation rules:
4806 * 1. The argument must exist in cmd_args qdict
4807 * 2. The argument type must be the expected one
4809 * Special case: If the argument doesn't exist in cmd_args and
4810 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4811 * checking is skipped for it.
4813 static int check_client_args_type(const QDict
*client_args
,
4814 const QDict
*cmd_args
, int flags
)
4816 const QDictEntry
*ent
;
4818 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4821 const QObject
*client_arg
= qdict_entry_value(ent
);
4822 const char *client_arg_name
= qdict_entry_key(ent
);
4824 obj
= qdict_get(cmd_args
, client_arg_name
);
4826 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4827 /* handler accepts unknowns */
4830 /* client arg doesn't exist */
4831 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4835 arg_type
= qobject_to_qstring(obj
);
4836 assert(arg_type
!= NULL
);
4838 /* check if argument's type is correct */
4839 switch (qstring_get_str(arg_type
)[0]) {
4843 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4844 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4853 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4854 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4860 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4861 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4862 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4869 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4870 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4876 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4881 * These types are not supported by QMP and thus are not
4882 * handled here. Fall through.
4893 * - Check if the client has passed all mandatory args
4894 * - Set special flags for argument validation
4896 static int check_mandatory_args(const QDict
*cmd_args
,
4897 const QDict
*client_args
, int *flags
)
4899 const QDictEntry
*ent
;
4901 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4902 const char *cmd_arg_name
= qdict_entry_key(ent
);
4903 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4904 assert(type
!= NULL
);
4906 if (qstring_get_str(type
)[0] == 'O') {
4907 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4908 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4909 } else if (qstring_get_str(type
)[0] != '-' &&
4910 qstring_get_str(type
)[1] != '?' &&
4911 !qdict_haskey(client_args
, cmd_arg_name
)) {
4912 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4920 static QDict
*qdict_from_args_type(const char *args_type
)
4924 QString
*key
, *type
, *cur_qs
;
4926 assert(args_type
!= NULL
);
4928 qdict
= qdict_new();
4930 if (args_type
== NULL
|| args_type
[0] == '\0') {
4931 /* no args, empty qdict */
4935 key
= qstring_new();
4936 type
= qstring_new();
4941 switch (args_type
[i
]) {
4944 qdict_put(qdict
, qstring_get_str(key
), type
);
4946 if (args_type
[i
] == '\0') {
4949 type
= qstring_new(); /* qdict has ref */
4950 cur_qs
= key
= qstring_new();
4956 qstring_append_chr(cur_qs
, args_type
[i
]);
4966 * Client argument checking rules:
4968 * 1. Client must provide all mandatory arguments
4969 * 2. Each argument provided by the client must be expected
4970 * 3. Each argument provided by the client must have the type expected
4973 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4978 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4981 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4986 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4994 * Input object checking rules
4996 * 1. Input object must be a dict
4997 * 2. The "execute" key must exist
4998 * 3. The "execute" key must be a string
4999 * 4. If the "arguments" key exists, it must be a dict
5000 * 5. If the "id" key exists, it can be anything (ie. json-value)
5001 * 6. Any argument not listed above is considered invalid
5003 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
5005 const QDictEntry
*ent
;
5006 int has_exec_key
= 0;
5009 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
5010 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
5014 input_dict
= qobject_to_qdict(input_obj
);
5016 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
5017 const char *arg_name
= qdict_entry_key(ent
);
5018 const QObject
*arg_obj
= qdict_entry_value(ent
);
5020 if (!strcmp(arg_name
, "execute")) {
5021 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
5022 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
5027 } else if (!strcmp(arg_name
, "arguments")) {
5028 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
5029 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
5033 } else if (!strcmp(arg_name
, "id")) {
5034 /* FIXME: check duplicated IDs for async commands */
5036 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
5041 if (!has_exec_key
) {
5042 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
5049 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
5051 QObject
*ret_data
= NULL
;
5053 if (handler_is_async(cmd
)) {
5054 qmp_async_info_handler(mon
, cmd
);
5055 if (monitor_has_error(mon
)) {
5056 monitor_protocol_emitter(mon
, NULL
);
5059 cmd
->mhandler
.info_new(mon
, &ret_data
);
5060 monitor_protocol_emitter(mon
, ret_data
);
5061 qobject_decref(ret_data
);
5065 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
5066 const QDict
*params
)
5069 QObject
*data
= NULL
;
5071 mon_print_count_init(mon
);
5073 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
5074 handler_audit(mon
, cmd
, ret
);
5075 monitor_protocol_emitter(mon
, data
);
5076 qobject_decref(data
);
5079 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
5083 QDict
*input
, *args
;
5084 const mon_cmd_t
*cmd
;
5085 Monitor
*mon
= cur_mon
;
5086 const char *cmd_name
, *query_cmd
;
5089 args
= input
= NULL
;
5091 obj
= json_parser_parse(tokens
, NULL
);
5093 // FIXME: should be triggered in json_parser_parse()
5094 qerror_report(QERR_JSON_PARSING
);
5098 input
= qmp_check_input_obj(obj
);
5100 qobject_decref(obj
);
5104 mon
->mc
->id
= qdict_get(input
, "id");
5105 qobject_incref(mon
->mc
->id
);
5107 cmd_name
= qdict_get_str(input
, "execute");
5108 trace_handle_qmp_command(mon
, cmd_name
);
5109 if (invalid_qmp_mode(mon
, cmd_name
)) {
5110 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
5114 if (strstart(cmd_name
, "query-", &query_cmd
)) {
5115 cmd
= qmp_find_query_cmd(query_cmd
);
5117 cmd
= qmp_find_cmd(cmd_name
);
5121 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
5125 obj
= qdict_get(input
, "arguments");
5129 args
= qobject_to_qdict(obj
);
5133 err
= qmp_check_client_args(cmd
, args
);
5139 qmp_call_query_cmd(mon
, cmd
);
5140 } else if (handler_is_async(cmd
)) {
5141 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
5143 /* emit the error response */
5147 qmp_call_cmd(mon
, cmd
, args
);
5153 monitor_protocol_emitter(mon
, NULL
);
5160 * monitor_control_read(): Read and handle QMP input
5162 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
5164 Monitor
*old_mon
= cur_mon
;
5168 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
5173 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
5175 Monitor
*old_mon
= cur_mon
;
5181 for (i
= 0; i
< size
; i
++)
5182 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
5184 if (size
== 0 || buf
[size
- 1] != 0)
5185 monitor_printf(cur_mon
, "corrupted command\n");
5187 handle_user_command(cur_mon
, (char *)buf
);
5193 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
5195 monitor_suspend(mon
);
5196 handle_user_command(mon
, cmdline
);
5197 monitor_resume(mon
);
5200 int monitor_suspend(Monitor
*mon
)
5208 void monitor_resume(Monitor
*mon
)
5212 if (--mon
->suspend_cnt
== 0)
5213 readline_show_prompt(mon
->rs
);
5216 static QObject
*get_qmp_greeting(void)
5220 do_info_version(NULL
, &ver
);
5221 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
5225 * monitor_control_event(): Print QMP gretting
5227 static void monitor_control_event(void *opaque
, int event
)
5230 Monitor
*mon
= opaque
;
5233 case CHR_EVENT_OPENED
:
5234 mon
->mc
->command_mode
= 0;
5235 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5236 data
= get_qmp_greeting();
5237 monitor_json_emitter(mon
, data
);
5238 qobject_decref(data
);
5240 case CHR_EVENT_CLOSED
:
5241 json_message_parser_destroy(&mon
->mc
->parser
);
5246 static void monitor_event(void *opaque
, int event
)
5248 Monitor
*mon
= opaque
;
5251 case CHR_EVENT_MUX_IN
:
5253 if (mon
->reset_seen
) {
5254 readline_restart(mon
->rs
);
5255 monitor_resume(mon
);
5258 mon
->suspend_cnt
= 0;
5262 case CHR_EVENT_MUX_OUT
:
5263 if (mon
->reset_seen
) {
5264 if (mon
->suspend_cnt
== 0) {
5265 monitor_printf(mon
, "\n");
5268 monitor_suspend(mon
);
5275 case CHR_EVENT_OPENED
:
5276 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
5277 "information\n", QEMU_VERSION
);
5278 if (!mon
->mux_out
) {
5279 readline_show_prompt(mon
->rs
);
5281 mon
->reset_seen
= 1;
5295 void monitor_init(CharDriverState
*chr
, int flags
)
5297 static int is_first_init
= 1;
5300 if (is_first_init
) {
5301 key_timer
= qemu_new_timer_ns(vm_clock
, release_keys
, NULL
);
5305 mon
= g_malloc0(sizeof(*mon
));
5309 if (flags
& MONITOR_USE_READLINE
) {
5310 mon
->rs
= readline_init(mon
, monitor_find_completion
);
5311 monitor_read_command(mon
, 0);
5314 if (monitor_ctrl_mode(mon
)) {
5315 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
5316 /* Control mode requires special handlers */
5317 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
5318 monitor_control_event
, mon
);
5319 qemu_chr_fe_set_echo(chr
, true);
5321 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5322 monitor_event
, mon
);
5325 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5326 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5330 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
5332 BlockDriverState
*bs
= opaque
;
5335 if (bdrv_set_key(bs
, password
) != 0) {
5336 monitor_printf(mon
, "invalid password\n");
5339 if (mon
->password_completion_cb
)
5340 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5342 monitor_read_command(mon
, 1);
5345 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5346 BlockDriverCompletionFunc
*completion_cb
,
5351 if (!bdrv_key_required(bs
)) {
5353 completion_cb(opaque
, 0);
5357 if (monitor_ctrl_mode(mon
)) {
5358 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
5362 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5363 bdrv_get_encrypted_filename(bs
));
5365 mon
->password_completion_cb
= completion_cb
;
5366 mon
->password_opaque
= opaque
;
5368 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5370 if (err
&& completion_cb
)
5371 completion_cb(opaque
, err
);