4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
43 #include "audio/audio.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
60 #include "trace/control.h"
61 #ifdef CONFIG_TRACE_SIMPLE
62 #include "trace/simple.h"
64 #include "trace/control.h"
65 #include "ui/qemu-spice.h"
69 //#define DEBUG_COMPLETION
75 * 'B' block device name
76 * 's' string (accept optional quote)
77 * 'O' option string of the form NAME=VALUE,...
78 * parsed according to QemuOptsList given by its name
79 * Example: 'device:O' uses qemu_device_opts.
80 * Restriction: only lists with empty desc are supported
81 * TODO lift the restriction
83 * 'l' target long (32 or 64 bit)
84 * 'M' just like 'l', except in user mode the value is
85 * multiplied by 2^20 (think Mebibyte)
86 * 'o' octets (aka bytes)
87 * user mode accepts an optional T, t, G, g, M, m, K, k
88 * suffix, which multiplies the value by 2^40 for
89 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
90 * M and m, 2^10 for K and k
92 * user mode accepts an optional ms, us, ns suffix,
93 * which divides the value by 1e3, 1e6, 1e9, respectively
94 * '/' optional gdb-like print format (like "/10x")
96 * '?' optional type (for all types, except '/')
97 * '.' other form of optional type (for 'i' and 'l')
99 * user mode accepts "on" or "off"
100 * '-' optional parameter (eg. '-f')
104 typedef struct MonitorCompletionData MonitorCompletionData
;
105 struct MonitorCompletionData
{
107 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
110 typedef struct mon_cmd_t
{
112 const char *args_type
;
115 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
117 void (*info
)(Monitor
*mon
);
118 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
119 int (*info_async
)(Monitor
*mon
, MonitorCompletion
*cb
, void *opaque
);
120 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
121 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
122 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
123 MonitorCompletion
*cb
, void *opaque
);
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
)
375 if (!monitor_has_error(mon
)) {
376 /* success response */
378 qobject_incref(data
);
379 qdict_put_obj(qmp
, "return", data
);
381 /* return an empty QDict by default */
382 qdict_put(qmp
, "return", qdict_new());
386 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
387 qdict_put(qmp
, "error", mon
->error
->error
);
388 QINCREF(mon
->error
->error
);
394 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
398 monitor_json_emitter(mon
, QOBJECT(qmp
));
402 static void timestamp_put(QDict
*qdict
)
408 err
= qemu_gettimeofday(&tv
);
412 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
413 "'microseconds': %" PRId64
" }",
414 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
415 qdict_put_obj(qdict
, "timestamp", obj
);
419 * monitor_protocol_event(): Generate a Monitor event
421 * Event-specific data can be emitted through the (optional) 'data' parameter.
423 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
426 const char *event_name
;
429 assert(event
< QEVENT_MAX
);
432 case QEVENT_SHUTDOWN
:
433 event_name
= "SHUTDOWN";
436 event_name
= "RESET";
438 case QEVENT_POWERDOWN
:
439 event_name
= "POWERDOWN";
445 event_name
= "RESUME";
447 case QEVENT_VNC_CONNECTED
:
448 event_name
= "VNC_CONNECTED";
450 case QEVENT_VNC_INITIALIZED
:
451 event_name
= "VNC_INITIALIZED";
453 case QEVENT_VNC_DISCONNECTED
:
454 event_name
= "VNC_DISCONNECTED";
456 case QEVENT_BLOCK_IO_ERROR
:
457 event_name
= "BLOCK_IO_ERROR";
459 case QEVENT_RTC_CHANGE
:
460 event_name
= "RTC_CHANGE";
462 case QEVENT_WATCHDOG
:
463 event_name
= "WATCHDOG";
465 case QEVENT_SPICE_CONNECTED
:
466 event_name
= "SPICE_CONNECTED";
468 case QEVENT_SPICE_INITIALIZED
:
469 event_name
= "SPICE_INITIALIZED";
471 case QEVENT_SPICE_DISCONNECTED
:
472 event_name
= "SPICE_DISCONNECTED";
481 qdict_put(qmp
, "event", qstring_from_str(event_name
));
483 qobject_incref(data
);
484 qdict_put_obj(qmp
, "data", data
);
487 QLIST_FOREACH(mon
, &mon_list
, entry
) {
488 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
489 monitor_json_emitter(mon
, QOBJECT(qmp
));
495 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
498 /* Will setup QMP capabilities in the future */
499 if (monitor_ctrl_mode(mon
)) {
500 mon
->mc
->command_mode
= 1;
506 static int mon_set_cpu(int cpu_index
);
507 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
509 static int do_hmp_passthrough(Monitor
*mon
, const QDict
*params
,
513 Monitor
*old_mon
, hmp
;
514 CharDriverState mchar
;
516 memset(&hmp
, 0, sizeof(hmp
));
517 qemu_chr_init_mem(&mchar
);
523 if (qdict_haskey(params
, "cpu-index")) {
524 ret
= mon_set_cpu(qdict_get_int(params
, "cpu-index"));
527 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "cpu-index", "a CPU number");
532 handle_user_command(&hmp
, qdict_get_str(params
, "command-line"));
535 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
536 *ret_data
= QOBJECT(qemu_chr_mem_to_qs(hmp
.chr
));
540 qemu_chr_close_mem(hmp
.chr
);
544 static int compare_cmd(const char *name
, const char *list
)
546 const char *p
, *pstart
;
554 p
= pstart
+ strlen(pstart
);
555 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
564 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
565 const char *prefix
, const char *name
)
567 const mon_cmd_t
*cmd
;
569 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
570 if (!name
|| !strcmp(name
, cmd
->name
))
571 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
572 cmd
->params
, cmd
->help
);
576 static void help_cmd(Monitor
*mon
, const char *name
)
578 if (name
&& !strcmp(name
, "info")) {
579 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
581 help_cmd_dump(mon
, mon_cmds
, "", name
);
582 if (name
&& !strcmp(name
, "log")) {
583 const CPULogItem
*item
;
584 monitor_printf(mon
, "Log items (comma separated):\n");
585 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
586 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
587 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
593 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
595 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
598 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
600 const char *tp_name
= qdict_get_str(qdict
, "name");
601 bool new_state
= qdict_get_bool(qdict
, "option");
602 int ret
= trace_event_set_state(tp_name
, new_state
);
605 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
609 #ifdef CONFIG_SIMPLE_TRACE
610 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
612 const char *op
= qdict_get_try_str(qdict
, "op");
613 const char *arg
= qdict_get_try_str(qdict
, "arg");
616 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
617 } else if (!strcmp(op
, "on")) {
618 st_set_trace_file_enabled(true);
619 } else if (!strcmp(op
, "off")) {
620 st_set_trace_file_enabled(false);
621 } else if (!strcmp(op
, "flush")) {
622 st_flush_trace_buffer();
623 } else if (!strcmp(op
, "set")) {
625 st_set_trace_file(arg
);
628 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
629 help_cmd(mon
, "trace-file");
634 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
636 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
639 data
->user_print(data
->mon
, ret_data
);
641 monitor_resume(data
->mon
);
645 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
647 monitor_protocol_emitter(opaque
, ret_data
);
650 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
653 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
656 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
658 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
661 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
666 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
668 cb_data
->user_print
= cmd
->user_print
;
669 monitor_suspend(mon
);
670 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
671 user_monitor_complete
, cb_data
);
678 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
682 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
684 cb_data
->user_print
= cmd
->user_print
;
685 monitor_suspend(mon
);
686 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
693 static void do_info(Monitor
*mon
, const QDict
*qdict
)
695 const mon_cmd_t
*cmd
;
696 const char *item
= qdict_get_try_str(qdict
, "item");
702 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
703 if (compare_cmd(item
, cmd
->name
))
707 if (cmd
->name
== NULL
) {
711 if (handler_is_async(cmd
)) {
712 user_async_info_handler(mon
, cmd
);
713 } else if (handler_is_qobject(cmd
)) {
714 QObject
*info_data
= NULL
;
716 cmd
->mhandler
.info_new(mon
, &info_data
);
718 cmd
->user_print(mon
, info_data
);
719 qobject_decref(info_data
);
722 cmd
->mhandler
.info(mon
);
728 help_cmd(mon
, "info");
731 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
736 qdict
= qobject_to_qdict(data
);
737 qemu
= qdict_get_qdict(qdict
, "qemu");
739 monitor_printf(mon
, "%" PRId64
".%" PRId64
".%" PRId64
"%s\n",
740 qdict_get_int(qemu
, "major"),
741 qdict_get_int(qemu
, "minor"),
742 qdict_get_int(qemu
, "micro"),
743 qdict_get_str(qdict
, "package"));
746 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
748 const char *version
= QEMU_VERSION
;
749 int major
= 0, minor
= 0, micro
= 0;
752 major
= strtol(version
, &tmp
, 10);
754 minor
= strtol(tmp
, &tmp
, 10);
756 micro
= strtol(tmp
, &tmp
, 10);
758 *ret_data
= qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
759 'micro': %d }, 'package': %s }", major
, minor
, micro
, QEMU_PKGVERSION
);
762 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
766 qdict
= qobject_to_qdict(data
);
767 if (qdict_size(qdict
) == 0) {
771 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
774 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
776 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
777 qobject_from_jsonf("{}");
780 static QObject
*get_cmd_dict(const char *name
)
784 /* Remove '|' from some commands */
785 p
= strchr(name
, '|');
792 return qobject_from_jsonf("{ 'name': %s }", p
);
795 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
798 const mon_cmd_t
*cmd
;
800 cmd_list
= qlist_new();
802 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
803 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
806 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
808 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
809 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
812 *ret_data
= QOBJECT(cmd_list
);
815 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
817 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
820 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
824 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
825 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
826 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
827 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
828 qemu_uuid
[14], qemu_uuid
[15]);
829 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
832 /* get the current CPU defined by the user */
833 static int mon_set_cpu(int cpu_index
)
837 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
838 if (env
->cpu_index
== cpu_index
) {
839 cur_mon
->mon_cpu
= env
;
846 static CPUState
*mon_get_cpu(void)
848 if (!cur_mon
->mon_cpu
) {
851 cpu_synchronize_state(cur_mon
->mon_cpu
);
852 return cur_mon
->mon_cpu
;
855 static void do_info_registers(Monitor
*mon
)
860 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
863 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
868 static void print_cpu_iter(QObject
*obj
, void *opaque
)
872 Monitor
*mon
= opaque
;
874 assert(qobject_type(obj
) == QTYPE_QDICT
);
875 cpu
= qobject_to_qdict(obj
);
877 if (qdict_get_bool(cpu
, "current")) {
881 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
883 #if defined(TARGET_I386)
884 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
885 (target_ulong
) qdict_get_int(cpu
, "pc"));
886 #elif defined(TARGET_PPC)
887 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
888 (target_long
) qdict_get_int(cpu
, "nip"));
889 #elif defined(TARGET_SPARC)
890 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
891 (target_long
) qdict_get_int(cpu
, "pc"));
892 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
893 (target_long
) qdict_get_int(cpu
, "npc"));
894 #elif defined(TARGET_MIPS)
895 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
896 (target_long
) qdict_get_int(cpu
, "PC"));
899 if (qdict_get_bool(cpu
, "halted")) {
900 monitor_printf(mon
, " (halted)");
903 monitor_printf(mon
, " thread_id=%" PRId64
" ",
904 qdict_get_int(cpu
, "thread_id"));
906 monitor_printf(mon
, "\n");
909 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
913 assert(qobject_type(data
) == QTYPE_QLIST
);
914 cpu_list
= qobject_to_qlist(data
);
915 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
918 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
923 cpu_list
= qlist_new();
925 /* just to set the default cpu if not already done */
928 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
932 cpu_synchronize_state(env
);
934 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
935 env
->cpu_index
, env
== mon
->mon_cpu
,
938 cpu
= qobject_to_qdict(obj
);
940 #if defined(TARGET_I386)
941 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
942 #elif defined(TARGET_PPC)
943 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
944 #elif defined(TARGET_SPARC)
945 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
946 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
947 #elif defined(TARGET_MIPS)
948 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
950 qdict_put(cpu
, "thread_id", qint_from_int(env
->thread_id
));
952 qlist_append(cpu_list
, cpu
);
955 *ret_data
= QOBJECT(cpu_list
);
958 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
960 int index
= qdict_get_int(qdict
, "index");
961 if (mon_set_cpu(index
) < 0) {
962 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "index",
969 static void do_info_jit(Monitor
*mon
)
971 dump_exec_info((FILE *)mon
, monitor_fprintf
);
974 static void do_info_history(Monitor
*mon
)
983 str
= readline_get_history(mon
->rs
, i
);
986 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
991 #if defined(TARGET_PPC)
992 /* XXX: not implemented in other targets */
993 static void do_info_cpu_stats(Monitor
*mon
)
998 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
1002 #if defined(CONFIG_TRACE_SIMPLE)
1003 static void do_info_trace(Monitor
*mon
)
1005 st_print_trace((FILE *)mon
, &monitor_fprintf
);
1009 static void do_trace_print_events(Monitor
*mon
)
1011 trace_print_events((FILE *)mon
, &monitor_fprintf
);
1015 * do_quit(): Quit QEMU execution
1017 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1019 monitor_suspend(mon
);
1021 qemu_system_shutdown_request();
1027 static int change_vnc_password(const char *password
)
1029 if (!password
|| !password
[0]) {
1030 if (vnc_display_disable_login(NULL
)) {
1031 qerror_report(QERR_SET_PASSWD_FAILED
);
1037 if (vnc_display_password(NULL
, password
) < 0) {
1038 qerror_report(QERR_SET_PASSWD_FAILED
);
1045 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
1048 change_vnc_password(password
);
1049 monitor_read_command(mon
, 1);
1052 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1054 if (strcmp(target
, "passwd") == 0 ||
1055 strcmp(target
, "password") == 0) {
1058 strncpy(password
, arg
, sizeof(password
));
1059 password
[sizeof(password
) - 1] = '\0';
1060 return change_vnc_password(password
);
1062 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1065 if (vnc_display_open(NULL
, target
) < 0) {
1066 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
1074 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
1076 qerror_report(QERR_FEATURE_DISABLED
, "vnc");
1082 * do_change(): Change a removable medium, or VNC configuration
1084 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1086 const char *device
= qdict_get_str(qdict
, "device");
1087 const char *target
= qdict_get_str(qdict
, "target");
1088 const char *arg
= qdict_get_try_str(qdict
, "arg");
1091 if (strcmp(device
, "vnc") == 0) {
1092 ret
= do_change_vnc(mon
, target
, arg
);
1094 ret
= do_change_block(mon
, device
, target
, arg
);
1100 static int set_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1102 const char *protocol
= qdict_get_str(qdict
, "protocol");
1103 const char *password
= qdict_get_str(qdict
, "password");
1104 const char *connected
= qdict_get_try_str(qdict
, "connected");
1105 int disconnect_if_connected
= 0;
1106 int fail_if_connected
= 0;
1110 if (strcmp(connected
, "fail") == 0) {
1111 fail_if_connected
= 1;
1112 } else if (strcmp(connected
, "disconnect") == 0) {
1113 disconnect_if_connected
= 1;
1114 } else if (strcmp(connected
, "keep") == 0) {
1117 qerror_report(QERR_INVALID_PARAMETER
, "connected");
1122 if (strcmp(protocol
, "spice") == 0) {
1124 /* correct one? spice isn't a device ,,, */
1125 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1128 rc
= qemu_spice_set_passwd(password
, fail_if_connected
,
1129 disconnect_if_connected
);
1131 qerror_report(QERR_SET_PASSWD_FAILED
);
1137 if (strcmp(protocol
, "vnc") == 0) {
1138 if (fail_if_connected
|| disconnect_if_connected
) {
1139 /* vnc supports "connected=keep" only */
1140 qerror_report(QERR_INVALID_PARAMETER
, "connected");
1143 /* Note that setting an empty password will not disable login through
1144 * this interface. */
1145 return vnc_display_password(NULL
, password
);
1148 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1152 static int expire_password(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1154 const char *protocol
= qdict_get_str(qdict
, "protocol");
1155 const char *whenstr
= qdict_get_str(qdict
, "time");
1159 if (strcmp(whenstr
, "now") == 0) {
1161 } else if (strcmp(whenstr
, "never") == 0) {
1163 } else if (whenstr
[0] == '+') {
1164 when
= time(NULL
) + strtoull(whenstr
+1, NULL
, 10);
1166 when
= strtoull(whenstr
, NULL
, 10);
1169 if (strcmp(protocol
, "spice") == 0) {
1171 /* correct one? spice isn't a device ,,, */
1172 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1175 rc
= qemu_spice_set_pw_expire(when
);
1177 qerror_report(QERR_SET_PASSWD_FAILED
);
1183 if (strcmp(protocol
, "vnc") == 0) {
1184 return vnc_display_pw_expire(NULL
, when
);
1187 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1191 static int add_graphics_client(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1193 const char *protocol
= qdict_get_str(qdict
, "protocol");
1194 const char *fdname
= qdict_get_str(qdict
, "fdname");
1197 if (strcmp(protocol
, "spice") == 0) {
1199 /* correct one? spice isn't a device ,,, */
1200 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1203 qerror_report(QERR_ADD_CLIENT_FAILED
);
1206 } else if (strcmp(protocol
, "vnc") == 0) {
1207 int fd
= monitor_get_fd(mon
, fdname
);
1208 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
1209 vnc_display_add_client(NULL
, fd
, skipauth
);
1212 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
1213 int fd
= monitor_get_fd(mon
, fdname
);
1214 if (qemu_chr_add_client(s
, fd
) < 0) {
1215 qerror_report(QERR_ADD_CLIENT_FAILED
);
1221 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1225 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1227 const char *protocol
= qdict_get_str(qdict
, "protocol");
1228 const char *hostname
= qdict_get_str(qdict
, "hostname");
1229 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
1230 int port
= qdict_get_try_int(qdict
, "port", -1);
1231 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
1234 if (strcmp(protocol
, "spice") == 0) {
1236 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1240 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
);
1242 qerror_report(QERR_UNDEFINED_ERROR
);
1248 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1252 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1254 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1258 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1260 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1263 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1266 const char *items
= qdict_get_str(qdict
, "items");
1268 if (!strcmp(items
, "none")) {
1271 mask
= cpu_str_to_log_mask(items
);
1273 help_cmd(mon
, "log");
1280 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1282 const char *option
= qdict_get_try_str(qdict
, "option");
1283 if (!option
|| !strcmp(option
, "on")) {
1285 } else if (!strcmp(option
, "off")) {
1288 monitor_printf(mon
, "unexpected option %s\n", option
);
1293 * do_stop(): Stop VM execution
1295 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1297 vm_stop(RSTATE_PAUSED
);
1301 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1303 struct bdrv_iterate_context
{
1309 * do_cont(): Resume emulation.
1311 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1313 struct bdrv_iterate_context context
= { mon
, 0 };
1315 if (runstate_check(RSTATE_IN_MIGRATE
)) {
1316 qerror_report(QERR_MIGRATION_EXPECTED
);
1318 } else if (runstate_check(RSTATE_PANICKED
) ||
1319 runstate_check(RSTATE_SHUTDOWN
)) {
1320 qerror_report(QERR_RESET_REQUIRED
);
1324 bdrv_iterate(encrypted_bdrv_it
, &context
);
1325 /* only resume the vm if all keys are set and valid */
1334 static void bdrv_key_cb(void *opaque
, int err
)
1336 Monitor
*mon
= opaque
;
1338 /* another key was set successfully, retry to continue */
1340 do_cont(mon
, NULL
, NULL
);
1343 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1345 struct bdrv_iterate_context
*context
= opaque
;
1347 if (!context
->err
&& bdrv_key_required(bs
)) {
1348 context
->err
= -EBUSY
;
1349 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1354 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1356 const char *device
= qdict_get_try_str(qdict
, "device");
1358 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1359 if (gdbserver_start(device
) < 0) {
1360 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1362 } else if (strcmp(device
, "none") == 0) {
1363 monitor_printf(mon
, "Disabled gdbserver\n");
1365 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1370 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1372 const char *action
= qdict_get_str(qdict
, "action");
1373 if (select_watchdog_action(action
) == -1) {
1374 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1378 static void monitor_printc(Monitor
*mon
, int c
)
1380 monitor_printf(mon
, "'");
1383 monitor_printf(mon
, "\\'");
1386 monitor_printf(mon
, "\\\\");
1389 monitor_printf(mon
, "\\n");
1392 monitor_printf(mon
, "\\r");
1395 if (c
>= 32 && c
<= 126) {
1396 monitor_printf(mon
, "%c", c
);
1398 monitor_printf(mon
, "\\x%02x", c
);
1402 monitor_printf(mon
, "'");
1405 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1406 target_phys_addr_t addr
, int is_physical
)
1409 int l
, line_size
, i
, max_digits
, len
;
1413 if (format
== 'i') {
1416 env
= mon_get_cpu();
1420 } else if (wsize
== 4) {
1423 /* as default we use the current CS size */
1426 #ifdef TARGET_X86_64
1427 if ((env
->efer
& MSR_EFER_LMA
) &&
1428 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1432 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1437 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1441 len
= wsize
* count
;
1450 max_digits
= (wsize
* 8 + 2) / 3;
1454 max_digits
= (wsize
* 8) / 4;
1458 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1467 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1469 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1474 cpu_physical_memory_read(addr
, buf
, l
);
1476 env
= mon_get_cpu();
1477 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1478 monitor_printf(mon
, " Cannot access memory\n");
1487 v
= ldub_raw(buf
+ i
);
1490 v
= lduw_raw(buf
+ i
);
1493 v
= (uint32_t)ldl_raw(buf
+ i
);
1496 v
= ldq_raw(buf
+ i
);
1499 monitor_printf(mon
, " ");
1502 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1505 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1508 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1511 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1514 monitor_printc(mon
, v
);
1519 monitor_printf(mon
, "\n");
1525 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1527 int count
= qdict_get_int(qdict
, "count");
1528 int format
= qdict_get_int(qdict
, "format");
1529 int size
= qdict_get_int(qdict
, "size");
1530 target_long addr
= qdict_get_int(qdict
, "addr");
1532 memory_dump(mon
, count
, format
, size
, addr
, 0);
1535 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1537 int count
= qdict_get_int(qdict
, "count");
1538 int format
= qdict_get_int(qdict
, "format");
1539 int size
= qdict_get_int(qdict
, "size");
1540 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1542 memory_dump(mon
, count
, format
, size
, addr
, 1);
1545 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1547 int format
= qdict_get_int(qdict
, "format");
1548 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1550 #if TARGET_PHYS_ADDR_BITS == 32
1553 monitor_printf(mon
, "%#o", val
);
1556 monitor_printf(mon
, "%#x", val
);
1559 monitor_printf(mon
, "%u", val
);
1563 monitor_printf(mon
, "%d", val
);
1566 monitor_printc(mon
, val
);
1572 monitor_printf(mon
, "%#" PRIo64
, val
);
1575 monitor_printf(mon
, "%#" PRIx64
, val
);
1578 monitor_printf(mon
, "%" PRIu64
, val
);
1582 monitor_printf(mon
, "%" PRId64
, val
);
1585 monitor_printc(mon
, val
);
1589 monitor_printf(mon
, "\n");
1592 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1595 uint32_t size
= qdict_get_int(qdict
, "size");
1596 const char *filename
= qdict_get_str(qdict
, "filename");
1597 target_long addr
= qdict_get_int(qdict
, "val");
1603 env
= mon_get_cpu();
1605 f
= fopen(filename
, "wb");
1607 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1614 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1615 if (fwrite(buf
, 1, l
, f
) != l
) {
1616 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1630 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1636 uint32_t size
= qdict_get_int(qdict
, "size");
1637 const char *filename
= qdict_get_str(qdict
, "filename");
1638 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1641 f
= fopen(filename
, "wb");
1643 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1650 cpu_physical_memory_read(addr
, buf
, l
);
1651 if (fwrite(buf
, 1, l
, f
) != l
) {
1652 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1667 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1671 uint32_t start
= qdict_get_int(qdict
, "start");
1672 uint32_t size
= qdict_get_int(qdict
, "size");
1675 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1676 uint8_t val
= ldub_phys(addr
);
1677 /* BSD sum algorithm ('sum' Unix command) */
1678 sum
= (sum
>> 1) | (sum
<< 15);
1681 monitor_printf(mon
, "%05d\n", sum
);
1689 static const KeyDef key_defs
[] = {
1691 { 0x36, "shift_r" },
1696 { 0xe4, "altgr_r" },
1716 { 0x0e, "backspace" },
1729 { 0x1a, "bracket_left" },
1730 { 0x1b, "bracket_right" },
1742 { 0x27, "semicolon" },
1743 { 0x28, "apostrophe" },
1744 { 0x29, "grave_accent" },
1746 { 0x2b, "backslash" },
1758 { 0x37, "asterisk" },
1761 { 0x3a, "caps_lock" },
1772 { 0x45, "num_lock" },
1773 { 0x46, "scroll_lock" },
1775 { 0xb5, "kp_divide" },
1776 { 0x37, "kp_multiply" },
1777 { 0x4a, "kp_subtract" },
1779 { 0x9c, "kp_enter" },
1780 { 0x53, "kp_decimal" },
1813 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1828 { 0xfe, "compose" },
1833 static int get_keycode(const char *key
)
1839 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1840 if (!strcmp(key
, p
->name
))
1843 if (strstart(key
, "0x", NULL
)) {
1844 ret
= strtoul(key
, &endp
, 0);
1845 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1851 #define MAX_KEYCODES 16
1852 static uint8_t keycodes
[MAX_KEYCODES
];
1853 static int nb_pending_keycodes
;
1854 static QEMUTimer
*key_timer
;
1856 static void release_keys(void *opaque
)
1860 while (nb_pending_keycodes
> 0) {
1861 nb_pending_keycodes
--;
1862 keycode
= keycodes
[nb_pending_keycodes
];
1864 kbd_put_keycode(0xe0);
1865 kbd_put_keycode(keycode
| 0x80);
1869 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1871 char keyname_buf
[16];
1873 int keyname_len
, keycode
, i
;
1874 const char *string
= qdict_get_str(qdict
, "string");
1875 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1876 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1878 if (nb_pending_keycodes
> 0) {
1879 qemu_del_timer(key_timer
);
1886 separator
= strchr(string
, '-');
1887 keyname_len
= separator
? separator
- string
: strlen(string
);
1888 if (keyname_len
> 0) {
1889 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1890 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1891 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1894 if (i
== MAX_KEYCODES
) {
1895 monitor_printf(mon
, "too many keys\n");
1898 keyname_buf
[keyname_len
] = 0;
1899 keycode
= get_keycode(keyname_buf
);
1901 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1904 keycodes
[i
++] = keycode
;
1908 string
= separator
+ 1;
1910 nb_pending_keycodes
= i
;
1911 /* key down events */
1912 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1913 keycode
= keycodes
[i
];
1915 kbd_put_keycode(0xe0);
1916 kbd_put_keycode(keycode
& 0x7f);
1918 /* delayed key up events */
1919 qemu_mod_timer(key_timer
, qemu_get_clock_ns(vm_clock
) +
1920 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1923 static int mouse_button_state
;
1925 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1928 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1929 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1930 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1931 dx
= strtol(dx_str
, NULL
, 0);
1932 dy
= strtol(dy_str
, NULL
, 0);
1935 dz
= strtol(dz_str
, NULL
, 0);
1936 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1939 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1941 int button_state
= qdict_get_int(qdict
, "button_state");
1942 mouse_button_state
= button_state
;
1943 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1946 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1948 int size
= qdict_get_int(qdict
, "size");
1949 int addr
= qdict_get_int(qdict
, "addr");
1950 int has_index
= qdict_haskey(qdict
, "index");
1955 int index
= qdict_get_int(qdict
, "index");
1956 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1964 val
= cpu_inb(addr
);
1968 val
= cpu_inw(addr
);
1972 val
= cpu_inl(addr
);
1976 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1977 suffix
, addr
, size
* 2, val
);
1980 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1982 int size
= qdict_get_int(qdict
, "size");
1983 int addr
= qdict_get_int(qdict
, "addr");
1984 int val
= qdict_get_int(qdict
, "val");
1986 addr
&= IOPORTS_MASK
;
1991 cpu_outb(addr
, val
);
1994 cpu_outw(addr
, val
);
1997 cpu_outl(addr
, val
);
2002 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
2005 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
2007 res
= qemu_boot_set(bootdevice
);
2009 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
2010 } else if (res
> 0) {
2011 monitor_printf(mon
, "setting boot device list failed\n");
2013 monitor_printf(mon
, "no function defined to set boot device list for "
2014 "this architecture\n");
2019 * do_system_reset(): Issue a machine reset
2021 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
2024 qemu_system_reset_request();
2029 * do_system_powerdown(): Issue a machine powerdown
2031 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
2034 qemu_system_powerdown_request();
2038 #if defined(TARGET_I386)
2039 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
2040 target_phys_addr_t pte
,
2041 target_phys_addr_t mask
)
2043 #ifdef TARGET_X86_64
2044 if (addr
& (1ULL << 47)) {
2048 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
2049 " %c%c%c%c%c%c%c%c%c\n",
2052 pte
& PG_NX_MASK
? 'X' : '-',
2053 pte
& PG_GLOBAL_MASK
? 'G' : '-',
2054 pte
& PG_PSE_MASK
? 'P' : '-',
2055 pte
& PG_DIRTY_MASK
? 'D' : '-',
2056 pte
& PG_ACCESSED_MASK
? 'A' : '-',
2057 pte
& PG_PCD_MASK
? 'C' : '-',
2058 pte
& PG_PWT_MASK
? 'T' : '-',
2059 pte
& PG_USER_MASK
? 'U' : '-',
2060 pte
& PG_RW_MASK
? 'W' : '-');
2063 static void tlb_info_32(Monitor
*mon
, CPUState
*env
)
2065 unsigned int l1
, l2
;
2066 uint32_t pgd
, pde
, pte
;
2068 pgd
= env
->cr
[3] & ~0xfff;
2069 for(l1
= 0; l1
< 1024; l1
++) {
2070 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2071 pde
= le32_to_cpu(pde
);
2072 if (pde
& PG_PRESENT_MASK
) {
2073 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2075 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
2077 for(l2
= 0; l2
< 1024; l2
++) {
2078 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2079 pte
= le32_to_cpu(pte
);
2080 if (pte
& PG_PRESENT_MASK
) {
2081 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
2091 static void tlb_info_pae32(Monitor
*mon
, CPUState
*env
)
2093 unsigned int l1
, l2
, l3
;
2094 uint64_t pdpe
, pde
, pte
;
2095 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2097 pdp_addr
= env
->cr
[3] & ~0x1f;
2098 for (l1
= 0; l1
< 4; l1
++) {
2099 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2100 pdpe
= le64_to_cpu(pdpe
);
2101 if (pdpe
& PG_PRESENT_MASK
) {
2102 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2103 for (l2
= 0; l2
< 512; l2
++) {
2104 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2105 pde
= le64_to_cpu(pde
);
2106 if (pde
& PG_PRESENT_MASK
) {
2107 if (pde
& PG_PSE_MASK
) {
2108 /* 2M pages with PAE, CR4.PSE is ignored */
2109 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
2110 ~((target_phys_addr_t
)(1 << 20) - 1));
2112 pt_addr
= pde
& 0x3fffffffff000ULL
;
2113 for (l3
= 0; l3
< 512; l3
++) {
2114 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2115 pte
= le64_to_cpu(pte
);
2116 if (pte
& PG_PRESENT_MASK
) {
2117 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
2120 ~(target_phys_addr_t
)0xfff);
2130 #ifdef TARGET_X86_64
2131 static void tlb_info_64(Monitor
*mon
, CPUState
*env
)
2133 uint64_t l1
, l2
, l3
, l4
;
2134 uint64_t pml4e
, pdpe
, pde
, pte
;
2135 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
2137 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2138 for (l1
= 0; l1
< 512; l1
++) {
2139 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2140 pml4e
= le64_to_cpu(pml4e
);
2141 if (pml4e
& PG_PRESENT_MASK
) {
2142 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2143 for (l2
= 0; l2
< 512; l2
++) {
2144 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2145 pdpe
= le64_to_cpu(pdpe
);
2146 if (pdpe
& PG_PRESENT_MASK
) {
2147 if (pdpe
& PG_PSE_MASK
) {
2148 /* 1G pages, CR4.PSE is ignored */
2149 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
2150 0x3ffffc0000000ULL
);
2152 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2153 for (l3
= 0; l3
< 512; l3
++) {
2154 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2155 pde
= le64_to_cpu(pde
);
2156 if (pde
& PG_PRESENT_MASK
) {
2157 if (pde
& PG_PSE_MASK
) {
2158 /* 2M pages, CR4.PSE is ignored */
2159 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
2161 0x3ffffffe00000ULL
);
2163 pt_addr
= pde
& 0x3fffffffff000ULL
;
2164 for (l4
= 0; l4
< 512; l4
++) {
2165 cpu_physical_memory_read(pt_addr
2168 pte
= le64_to_cpu(pte
);
2169 if (pte
& PG_PRESENT_MASK
) {
2170 print_pte(mon
, (l1
<< 39) +
2172 (l3
<< 21) + (l4
<< 12),
2174 0x3fffffffff000ULL
);
2188 static void tlb_info(Monitor
*mon
)
2192 env
= mon_get_cpu();
2194 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2195 monitor_printf(mon
, "PG disabled\n");
2198 if (env
->cr
[4] & CR4_PAE_MASK
) {
2199 #ifdef TARGET_X86_64
2200 if (env
->hflags
& HF_LMA_MASK
) {
2201 tlb_info_64(mon
, env
);
2205 tlb_info_pae32(mon
, env
);
2208 tlb_info_32(mon
, env
);
2212 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
2214 target_phys_addr_t end
, int prot
)
2217 prot1
= *plast_prot
;
2218 if (prot
!= prot1
) {
2219 if (*pstart
!= -1) {
2220 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
2221 TARGET_FMT_plx
" %c%c%c\n",
2222 *pstart
, end
, end
- *pstart
,
2223 prot1
& PG_USER_MASK
? 'u' : '-',
2225 prot1
& PG_RW_MASK
? 'w' : '-');
2235 static void mem_info_32(Monitor
*mon
, CPUState
*env
)
2237 unsigned int l1
, l2
;
2238 int prot
, last_prot
;
2239 uint32_t pgd
, pde
, pte
;
2240 target_phys_addr_t start
, end
;
2242 pgd
= env
->cr
[3] & ~0xfff;
2245 for(l1
= 0; l1
< 1024; l1
++) {
2246 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
2247 pde
= le32_to_cpu(pde
);
2249 if (pde
& PG_PRESENT_MASK
) {
2250 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
2251 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2252 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2254 for(l2
= 0; l2
< 1024; l2
++) {
2255 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
2256 pte
= le32_to_cpu(pte
);
2257 end
= (l1
<< 22) + (l2
<< 12);
2258 if (pte
& PG_PRESENT_MASK
) {
2260 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
2264 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2269 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2272 /* Flush last range */
2273 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2276 static void mem_info_pae32(Monitor
*mon
, CPUState
*env
)
2278 unsigned int l1
, l2
, l3
;
2279 int prot
, last_prot
;
2280 uint64_t pdpe
, pde
, pte
;
2281 uint64_t pdp_addr
, pd_addr
, pt_addr
;
2282 target_phys_addr_t start
, end
;
2284 pdp_addr
= env
->cr
[3] & ~0x1f;
2287 for (l1
= 0; l1
< 4; l1
++) {
2288 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
2289 pdpe
= le64_to_cpu(pdpe
);
2291 if (pdpe
& PG_PRESENT_MASK
) {
2292 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2293 for (l2
= 0; l2
< 512; l2
++) {
2294 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
2295 pde
= le64_to_cpu(pde
);
2296 end
= (l1
<< 30) + (l2
<< 21);
2297 if (pde
& PG_PRESENT_MASK
) {
2298 if (pde
& PG_PSE_MASK
) {
2299 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2301 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2303 pt_addr
= pde
& 0x3fffffffff000ULL
;
2304 for (l3
= 0; l3
< 512; l3
++) {
2305 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
2306 pte
= le64_to_cpu(pte
);
2307 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
2308 if (pte
& PG_PRESENT_MASK
) {
2309 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
2314 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2319 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2324 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2327 /* Flush last range */
2328 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
2332 #ifdef TARGET_X86_64
2333 static void mem_info_64(Monitor
*mon
, CPUState
*env
)
2335 int prot
, last_prot
;
2336 uint64_t l1
, l2
, l3
, l4
;
2337 uint64_t pml4e
, pdpe
, pde
, pte
;
2338 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
2340 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
2343 for (l1
= 0; l1
< 512; l1
++) {
2344 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
2345 pml4e
= le64_to_cpu(pml4e
);
2347 if (pml4e
& PG_PRESENT_MASK
) {
2348 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
2349 for (l2
= 0; l2
< 512; l2
++) {
2350 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
2351 pdpe
= le64_to_cpu(pdpe
);
2352 end
= (l1
<< 39) + (l2
<< 30);
2353 if (pdpe
& PG_PRESENT_MASK
) {
2354 if (pdpe
& PG_PSE_MASK
) {
2355 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
2358 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2360 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
2361 for (l3
= 0; l3
< 512; l3
++) {
2362 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
2363 pde
= le64_to_cpu(pde
);
2364 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
2365 if (pde
& PG_PRESENT_MASK
) {
2366 if (pde
& PG_PSE_MASK
) {
2367 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
2369 prot
&= pml4e
& pdpe
;
2370 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2372 pt_addr
= pde
& 0x3fffffffff000ULL
;
2373 for (l4
= 0; l4
< 512; l4
++) {
2374 cpu_physical_memory_read(pt_addr
2377 pte
= le64_to_cpu(pte
);
2378 end
= (l1
<< 39) + (l2
<< 30) +
2379 (l3
<< 21) + (l4
<< 12);
2380 if (pte
& PG_PRESENT_MASK
) {
2381 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
2383 prot
&= pml4e
& pdpe
& pde
;
2387 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2392 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2398 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2403 mem_print(mon
, &start
, &last_prot
, end
, prot
);
2406 /* Flush last range */
2407 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 48, 0);
2411 static void mem_info(Monitor
*mon
)
2415 env
= mon_get_cpu();
2417 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2418 monitor_printf(mon
, "PG disabled\n");
2421 if (env
->cr
[4] & CR4_PAE_MASK
) {
2422 #ifdef TARGET_X86_64
2423 if (env
->hflags
& HF_LMA_MASK
) {
2424 mem_info_64(mon
, env
);
2428 mem_info_pae32(mon
, env
);
2431 mem_info_32(mon
, env
);
2436 #if defined(TARGET_SH4)
2438 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2440 monitor_printf(mon
, " tlb%i:\t"
2441 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2442 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2443 "dirty=%hhu writethrough=%hhu\n",
2445 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2446 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2450 static void tlb_info(Monitor
*mon
)
2452 CPUState
*env
= mon_get_cpu();
2455 monitor_printf (mon
, "ITLB:\n");
2456 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2457 print_tlb (mon
, i
, &env
->itlb
[i
]);
2458 monitor_printf (mon
, "UTLB:\n");
2459 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2460 print_tlb (mon
, i
, &env
->utlb
[i
]);
2465 #if defined(TARGET_SPARC)
2466 static void tlb_info(Monitor
*mon
)
2468 CPUState
*env1
= mon_get_cpu();
2470 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
2474 static void do_info_mtree(Monitor
*mon
)
2476 mtree_info((fprintf_function
)monitor_printf
, mon
);
2479 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
2483 qdict
= qobject_to_qdict(data
);
2485 monitor_printf(mon
, "kvm support: ");
2486 if (qdict_get_bool(qdict
, "present")) {
2487 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
2488 "enabled" : "disabled");
2490 monitor_printf(mon
, "not compiled\n");
2494 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
2497 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
2500 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
2504 static void do_info_numa(Monitor
*mon
)
2509 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2510 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2511 monitor_printf(mon
, "node %d cpus:", i
);
2512 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2513 if (env
->numa_node
== i
) {
2514 monitor_printf(mon
, " %d", env
->cpu_index
);
2517 monitor_printf(mon
, "\n");
2518 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2523 #ifdef CONFIG_PROFILER
2528 static void do_info_profile(Monitor
*mon
)
2534 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2535 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2536 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2537 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2542 static void do_info_profile(Monitor
*mon
)
2544 monitor_printf(mon
, "Internal profiler not compiled\n");
2548 /* Capture support */
2549 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2551 static void do_info_capture(Monitor
*mon
)
2556 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2557 monitor_printf(mon
, "[%d]: ", i
);
2558 s
->ops
.info (s
->opaque
);
2563 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2566 int n
= qdict_get_int(qdict
, "n");
2569 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2571 s
->ops
.destroy (s
->opaque
);
2572 QLIST_REMOVE (s
, entries
);
2579 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2581 const char *path
= qdict_get_str(qdict
, "path");
2582 int has_freq
= qdict_haskey(qdict
, "freq");
2583 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2584 int has_bits
= qdict_haskey(qdict
, "bits");
2585 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2586 int has_channels
= qdict_haskey(qdict
, "nchannels");
2587 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2590 s
= g_malloc0 (sizeof (*s
));
2592 freq
= has_freq
? freq
: 44100;
2593 bits
= has_bits
? bits
: 16;
2594 nchannels
= has_channels
? nchannels
: 2;
2596 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2597 monitor_printf(mon
, "Failed to add wave capture\n");
2601 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2605 #if defined(TARGET_I386)
2606 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2610 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2611 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2617 static int do_inject_nmi(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2619 qerror_report(QERR_UNSUPPORTED
);
2624 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2629 qdict
= qobject_to_qdict(data
);
2631 monitor_printf(mon
, "VM status: ");
2632 if (qdict_get_bool(qdict
, "running")) {
2633 monitor_printf(mon
, "running");
2634 if (qdict_get_bool(qdict
, "singlestep")) {
2635 monitor_printf(mon
, " (single step mode)");
2638 monitor_printf(mon
, "paused");
2641 status
= qdict_get_str(qdict
, "status");
2642 if (strcmp(status
, "paused") && strcmp(status
, "running")) {
2643 monitor_printf(mon
, " (%s)", status
);
2646 monitor_printf(mon
, "\n");
2649 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2651 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i, 'status': %s }", runstate_is_running(), singlestep
, runstate_as_string());
2654 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2656 qemu_acl
*acl
= qemu_acl_find(name
);
2659 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2664 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2666 const char *aclname
= qdict_get_str(qdict
, "aclname");
2667 qemu_acl
*acl
= find_acl(mon
, aclname
);
2668 qemu_acl_entry
*entry
;
2672 monitor_printf(mon
, "policy: %s\n",
2673 acl
->defaultDeny
? "deny" : "allow");
2674 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2676 monitor_printf(mon
, "%d: %s %s\n", i
,
2677 entry
->deny
? "deny" : "allow", entry
->match
);
2682 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2684 const char *aclname
= qdict_get_str(qdict
, "aclname");
2685 qemu_acl
*acl
= find_acl(mon
, aclname
);
2688 qemu_acl_reset(acl
);
2689 monitor_printf(mon
, "acl: removed all rules\n");
2693 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2695 const char *aclname
= qdict_get_str(qdict
, "aclname");
2696 const char *policy
= qdict_get_str(qdict
, "policy");
2697 qemu_acl
*acl
= find_acl(mon
, aclname
);
2700 if (strcmp(policy
, "allow") == 0) {
2701 acl
->defaultDeny
= 0;
2702 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2703 } else if (strcmp(policy
, "deny") == 0) {
2704 acl
->defaultDeny
= 1;
2705 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2707 monitor_printf(mon
, "acl: unknown policy '%s', "
2708 "expected 'deny' or 'allow'\n", policy
);
2713 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2715 const char *aclname
= qdict_get_str(qdict
, "aclname");
2716 const char *match
= qdict_get_str(qdict
, "match");
2717 const char *policy
= qdict_get_str(qdict
, "policy");
2718 int has_index
= qdict_haskey(qdict
, "index");
2719 int index
= qdict_get_try_int(qdict
, "index", -1);
2720 qemu_acl
*acl
= find_acl(mon
, aclname
);
2724 if (strcmp(policy
, "allow") == 0) {
2726 } else if (strcmp(policy
, "deny") == 0) {
2729 monitor_printf(mon
, "acl: unknown policy '%s', "
2730 "expected 'deny' or 'allow'\n", policy
);
2734 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2736 ret
= qemu_acl_append(acl
, deny
, match
);
2738 monitor_printf(mon
, "acl: unable to add acl entry\n");
2740 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2744 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2746 const char *aclname
= qdict_get_str(qdict
, "aclname");
2747 const char *match
= qdict_get_str(qdict
, "match");
2748 qemu_acl
*acl
= find_acl(mon
, aclname
);
2752 ret
= qemu_acl_remove(acl
, match
);
2754 monitor_printf(mon
, "acl: no matching acl entry\n");
2756 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2760 #if defined(TARGET_I386)
2761 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2764 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2765 int bank
= qdict_get_int(qdict
, "bank");
2766 uint64_t status
= qdict_get_int(qdict
, "status");
2767 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2768 uint64_t addr
= qdict_get_int(qdict
, "addr");
2769 uint64_t misc
= qdict_get_int(qdict
, "misc");
2770 int flags
= MCE_INJECT_UNCOND_AO
;
2772 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2773 flags
|= MCE_INJECT_BROADCAST
;
2775 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
2776 if (cenv
->cpu_index
== cpu_index
) {
2777 cpu_x86_inject_mce(mon
, cenv
, bank
, status
, mcg_status
, addr
, misc
,
2785 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2787 const char *fdname
= qdict_get_str(qdict
, "fdname");
2791 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2793 qerror_report(QERR_FD_NOT_SUPPLIED
);
2797 if (qemu_isdigit(fdname
[0])) {
2798 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2799 "a name not starting with a digit");
2803 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2804 if (strcmp(monfd
->name
, fdname
) != 0) {
2813 monfd
= g_malloc0(sizeof(mon_fd_t
));
2814 monfd
->name
= g_strdup(fdname
);
2817 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2821 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2823 const char *fdname
= qdict_get_str(qdict
, "fdname");
2826 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2827 if (strcmp(monfd
->name
, fdname
) != 0) {
2831 QLIST_REMOVE(monfd
, next
);
2833 g_free(monfd
->name
);
2838 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2842 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2844 int saved_vm_running
= runstate_is_running();
2845 const char *name
= qdict_get_str(qdict
, "name");
2847 vm_stop(RSTATE_RESTORE
);
2849 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2854 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2858 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2861 if (strcmp(monfd
->name
, fdname
) != 0) {
2867 /* caller takes ownership of fd */
2868 QLIST_REMOVE(monfd
, next
);
2869 g_free(monfd
->name
);
2878 static const mon_cmd_t mon_cmds
[] = {
2879 #include "hmp-commands.h"
2883 /* Please update hmp-commands.hx when adding or changing commands */
2884 static const mon_cmd_t info_cmds
[] = {
2889 .help
= "show the version of QEMU",
2890 .user_print
= do_info_version_print
,
2891 .mhandler
.info_new
= do_info_version
,
2897 .help
= "show the network state",
2898 .mhandler
.info
= do_info_network
,
2904 .help
= "show the character devices",
2905 .user_print
= qemu_chr_info_print
,
2906 .mhandler
.info_new
= qemu_chr_info
,
2912 .help
= "show the block devices",
2913 .user_print
= bdrv_info_print
,
2914 .mhandler
.info_new
= bdrv_info
,
2917 .name
= "blockstats",
2920 .help
= "show block device statistics",
2921 .user_print
= bdrv_stats_print
,
2922 .mhandler
.info_new
= bdrv_info_stats
,
2925 .name
= "registers",
2928 .help
= "show the cpu registers",
2929 .mhandler
.info
= do_info_registers
,
2935 .help
= "show infos for each CPU",
2936 .user_print
= monitor_print_cpus
,
2937 .mhandler
.info_new
= do_info_cpus
,
2943 .help
= "show the command line history",
2944 .mhandler
.info
= do_info_history
,
2950 .help
= "show the interrupts statistics (if available)",
2951 .mhandler
.info
= irq_info
,
2957 .help
= "show i8259 (PIC) state",
2958 .mhandler
.info
= pic_info
,
2964 .help
= "show PCI info",
2965 .user_print
= do_pci_info_print
,
2966 .mhandler
.info_new
= do_pci_info
,
2968 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC)
2973 .help
= "show virtual to physical memory mappings",
2974 .mhandler
.info
= tlb_info
,
2977 #if defined(TARGET_I386)
2982 .help
= "show the active virtual memory mappings",
2983 .mhandler
.info
= mem_info
,
2990 .help
= "show memory tree",
2991 .mhandler
.info
= do_info_mtree
,
2997 .help
= "show dynamic compiler info",
2998 .mhandler
.info
= do_info_jit
,
3004 .help
= "show KVM information",
3005 .user_print
= do_info_kvm_print
,
3006 .mhandler
.info_new
= do_info_kvm
,
3012 .help
= "show NUMA information",
3013 .mhandler
.info
= do_info_numa
,
3019 .help
= "show guest USB devices",
3020 .mhandler
.info
= usb_info
,
3026 .help
= "show host USB devices",
3027 .mhandler
.info
= usb_host_info
,
3033 .help
= "show profiling information",
3034 .mhandler
.info
= do_info_profile
,
3040 .help
= "show capture information",
3041 .mhandler
.info
= do_info_capture
,
3044 .name
= "snapshots",
3047 .help
= "show the currently saved VM snapshots",
3048 .mhandler
.info
= do_info_snapshots
,
3054 .help
= "show the current VM status (running|paused)",
3055 .user_print
= do_info_status_print
,
3056 .mhandler
.info_new
= do_info_status
,
3062 .help
= "show guest PCMCIA status",
3063 .mhandler
.info
= pcmcia_info
,
3069 .help
= "show which guest mouse is receiving events",
3070 .user_print
= do_info_mice_print
,
3071 .mhandler
.info_new
= do_info_mice
,
3077 .help
= "show the vnc server status",
3078 .user_print
= do_info_vnc_print
,
3079 .mhandler
.info_new
= do_info_vnc
,
3081 #if defined(CONFIG_SPICE)
3086 .help
= "show the spice server status",
3087 .user_print
= do_info_spice_print
,
3088 .mhandler
.info_new
= do_info_spice
,
3095 .help
= "show the current VM name",
3096 .user_print
= do_info_name_print
,
3097 .mhandler
.info_new
= do_info_name
,
3103 .help
= "show the current VM UUID",
3104 .user_print
= do_info_uuid_print
,
3105 .mhandler
.info_new
= do_info_uuid
,
3107 #if defined(TARGET_PPC)
3112 .help
= "show CPU statistics",
3113 .mhandler
.info
= do_info_cpu_stats
,
3116 #if defined(CONFIG_SLIRP)
3121 .help
= "show user network stack connection states",
3122 .mhandler
.info
= do_info_usernet
,
3129 .help
= "show migration status",
3130 .user_print
= do_info_migrate_print
,
3131 .mhandler
.info_new
= do_info_migrate
,
3137 .help
= "show balloon information",
3138 .user_print
= monitor_print_balloon
,
3139 .mhandler
.info_async
= do_info_balloon
,
3140 .flags
= MONITOR_CMD_ASYNC
,
3146 .help
= "show device tree",
3147 .mhandler
.info
= do_info_qtree
,
3153 .help
= "show qdev device model list",
3154 .mhandler
.info
= do_info_qdm
,
3160 .help
= "show roms",
3161 .mhandler
.info
= do_info_roms
,
3163 #if defined(CONFIG_TRACE_SIMPLE)
3168 .help
= "show current contents of trace buffer",
3169 .mhandler
.info
= do_info_trace
,
3173 .name
= "trace-events",
3176 .help
= "show available trace-events & their state",
3177 .mhandler
.info
= do_trace_print_events
,
3184 static const mon_cmd_t qmp_cmds
[] = {
3185 #include "qmp-commands.h"
3189 static const mon_cmd_t qmp_query_cmds
[] = {
3194 .help
= "show the version of QEMU",
3195 .user_print
= do_info_version_print
,
3196 .mhandler
.info_new
= do_info_version
,
3202 .help
= "list QMP available commands",
3203 .user_print
= monitor_user_noop
,
3204 .mhandler
.info_new
= do_info_commands
,
3210 .help
= "show the character devices",
3211 .user_print
= qemu_chr_info_print
,
3212 .mhandler
.info_new
= qemu_chr_info
,
3218 .help
= "show the block devices",
3219 .user_print
= bdrv_info_print
,
3220 .mhandler
.info_new
= bdrv_info
,
3223 .name
= "blockstats",
3226 .help
= "show block device statistics",
3227 .user_print
= bdrv_stats_print
,
3228 .mhandler
.info_new
= bdrv_info_stats
,
3234 .help
= "show infos for each CPU",
3235 .user_print
= monitor_print_cpus
,
3236 .mhandler
.info_new
= do_info_cpus
,
3242 .help
= "show PCI info",
3243 .user_print
= do_pci_info_print
,
3244 .mhandler
.info_new
= do_pci_info
,
3250 .help
= "show KVM information",
3251 .user_print
= do_info_kvm_print
,
3252 .mhandler
.info_new
= do_info_kvm
,
3258 .help
= "show the current VM status (running|paused)",
3259 .user_print
= do_info_status_print
,
3260 .mhandler
.info_new
= do_info_status
,
3266 .help
= "show which guest mouse is receiving events",
3267 .user_print
= do_info_mice_print
,
3268 .mhandler
.info_new
= do_info_mice
,
3274 .help
= "show the vnc server status",
3275 .user_print
= do_info_vnc_print
,
3276 .mhandler
.info_new
= do_info_vnc
,
3278 #if defined(CONFIG_SPICE)
3283 .help
= "show the spice server status",
3284 .user_print
= do_info_spice_print
,
3285 .mhandler
.info_new
= do_info_spice
,
3292 .help
= "show the current VM name",
3293 .user_print
= do_info_name_print
,
3294 .mhandler
.info_new
= do_info_name
,
3300 .help
= "show the current VM UUID",
3301 .user_print
= do_info_uuid_print
,
3302 .mhandler
.info_new
= do_info_uuid
,
3308 .help
= "show migration status",
3309 .user_print
= do_info_migrate_print
,
3310 .mhandler
.info_new
= do_info_migrate
,
3316 .help
= "show balloon information",
3317 .user_print
= monitor_print_balloon
,
3318 .mhandler
.info_async
= do_info_balloon
,
3319 .flags
= MONITOR_CMD_ASYNC
,
3324 /*******************************************************************/
3326 static const char *pch
;
3327 static jmp_buf expr_env
;
3332 typedef struct MonitorDef
{
3335 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
3339 #if defined(TARGET_I386)
3340 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
3342 CPUState
*env
= mon_get_cpu();
3343 return env
->eip
+ env
->segs
[R_CS
].base
;
3347 #if defined(TARGET_PPC)
3348 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3350 CPUState
*env
= mon_get_cpu();
3355 for (i
= 0; i
< 8; i
++)
3356 u
|= env
->crf
[i
] << (32 - (4 * i
));
3361 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3363 CPUState
*env
= mon_get_cpu();
3367 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3369 CPUState
*env
= mon_get_cpu();
3373 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3375 CPUState
*env
= mon_get_cpu();
3376 return cpu_ppc_load_decr(env
);
3379 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3381 CPUState
*env
= mon_get_cpu();
3382 return cpu_ppc_load_tbu(env
);
3385 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3387 CPUState
*env
= mon_get_cpu();
3388 return cpu_ppc_load_tbl(env
);
3392 #if defined(TARGET_SPARC)
3393 #ifndef TARGET_SPARC64
3394 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3396 CPUState
*env
= mon_get_cpu();
3398 return cpu_get_psr(env
);
3402 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3404 CPUState
*env
= mon_get_cpu();
3405 return env
->regwptr
[val
];
3409 static const MonitorDef monitor_defs
[] = {
3412 #define SEG(name, seg) \
3413 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
3414 { name ".base", offsetof(CPUState, segs[seg].base) },\
3415 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
3417 { "eax", offsetof(CPUState
, regs
[0]) },
3418 { "ecx", offsetof(CPUState
, regs
[1]) },
3419 { "edx", offsetof(CPUState
, regs
[2]) },
3420 { "ebx", offsetof(CPUState
, regs
[3]) },
3421 { "esp|sp", offsetof(CPUState
, regs
[4]) },
3422 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
3423 { "esi", offsetof(CPUState
, regs
[6]) },
3424 { "edi", offsetof(CPUState
, regs
[7]) },
3425 #ifdef TARGET_X86_64
3426 { "r8", offsetof(CPUState
, regs
[8]) },
3427 { "r9", offsetof(CPUState
, regs
[9]) },
3428 { "r10", offsetof(CPUState
, regs
[10]) },
3429 { "r11", offsetof(CPUState
, regs
[11]) },
3430 { "r12", offsetof(CPUState
, regs
[12]) },
3431 { "r13", offsetof(CPUState
, regs
[13]) },
3432 { "r14", offsetof(CPUState
, regs
[14]) },
3433 { "r15", offsetof(CPUState
, regs
[15]) },
3435 { "eflags", offsetof(CPUState
, eflags
) },
3436 { "eip", offsetof(CPUState
, eip
) },
3443 { "pc", 0, monitor_get_pc
, },
3444 #elif defined(TARGET_PPC)
3445 /* General purpose registers */
3446 { "r0", offsetof(CPUState
, gpr
[0]) },
3447 { "r1", offsetof(CPUState
, gpr
[1]) },
3448 { "r2", offsetof(CPUState
, gpr
[2]) },
3449 { "r3", offsetof(CPUState
, gpr
[3]) },
3450 { "r4", offsetof(CPUState
, gpr
[4]) },
3451 { "r5", offsetof(CPUState
, gpr
[5]) },
3452 { "r6", offsetof(CPUState
, gpr
[6]) },
3453 { "r7", offsetof(CPUState
, gpr
[7]) },
3454 { "r8", offsetof(CPUState
, gpr
[8]) },
3455 { "r9", offsetof(CPUState
, gpr
[9]) },
3456 { "r10", offsetof(CPUState
, gpr
[10]) },
3457 { "r11", offsetof(CPUState
, gpr
[11]) },
3458 { "r12", offsetof(CPUState
, gpr
[12]) },
3459 { "r13", offsetof(CPUState
, gpr
[13]) },
3460 { "r14", offsetof(CPUState
, gpr
[14]) },
3461 { "r15", offsetof(CPUState
, gpr
[15]) },
3462 { "r16", offsetof(CPUState
, gpr
[16]) },
3463 { "r17", offsetof(CPUState
, gpr
[17]) },
3464 { "r18", offsetof(CPUState
, gpr
[18]) },
3465 { "r19", offsetof(CPUState
, gpr
[19]) },
3466 { "r20", offsetof(CPUState
, gpr
[20]) },
3467 { "r21", offsetof(CPUState
, gpr
[21]) },
3468 { "r22", offsetof(CPUState
, gpr
[22]) },
3469 { "r23", offsetof(CPUState
, gpr
[23]) },
3470 { "r24", offsetof(CPUState
, gpr
[24]) },
3471 { "r25", offsetof(CPUState
, gpr
[25]) },
3472 { "r26", offsetof(CPUState
, gpr
[26]) },
3473 { "r27", offsetof(CPUState
, gpr
[27]) },
3474 { "r28", offsetof(CPUState
, gpr
[28]) },
3475 { "r29", offsetof(CPUState
, gpr
[29]) },
3476 { "r30", offsetof(CPUState
, gpr
[30]) },
3477 { "r31", offsetof(CPUState
, gpr
[31]) },
3478 /* Floating point registers */
3479 { "f0", offsetof(CPUState
, fpr
[0]) },
3480 { "f1", offsetof(CPUState
, fpr
[1]) },
3481 { "f2", offsetof(CPUState
, fpr
[2]) },
3482 { "f3", offsetof(CPUState
, fpr
[3]) },
3483 { "f4", offsetof(CPUState
, fpr
[4]) },
3484 { "f5", offsetof(CPUState
, fpr
[5]) },
3485 { "f6", offsetof(CPUState
, fpr
[6]) },
3486 { "f7", offsetof(CPUState
, fpr
[7]) },
3487 { "f8", offsetof(CPUState
, fpr
[8]) },
3488 { "f9", offsetof(CPUState
, fpr
[9]) },
3489 { "f10", offsetof(CPUState
, fpr
[10]) },
3490 { "f11", offsetof(CPUState
, fpr
[11]) },
3491 { "f12", offsetof(CPUState
, fpr
[12]) },
3492 { "f13", offsetof(CPUState
, fpr
[13]) },
3493 { "f14", offsetof(CPUState
, fpr
[14]) },
3494 { "f15", offsetof(CPUState
, fpr
[15]) },
3495 { "f16", offsetof(CPUState
, fpr
[16]) },
3496 { "f17", offsetof(CPUState
, fpr
[17]) },
3497 { "f18", offsetof(CPUState
, fpr
[18]) },
3498 { "f19", offsetof(CPUState
, fpr
[19]) },
3499 { "f20", offsetof(CPUState
, fpr
[20]) },
3500 { "f21", offsetof(CPUState
, fpr
[21]) },
3501 { "f22", offsetof(CPUState
, fpr
[22]) },
3502 { "f23", offsetof(CPUState
, fpr
[23]) },
3503 { "f24", offsetof(CPUState
, fpr
[24]) },
3504 { "f25", offsetof(CPUState
, fpr
[25]) },
3505 { "f26", offsetof(CPUState
, fpr
[26]) },
3506 { "f27", offsetof(CPUState
, fpr
[27]) },
3507 { "f28", offsetof(CPUState
, fpr
[28]) },
3508 { "f29", offsetof(CPUState
, fpr
[29]) },
3509 { "f30", offsetof(CPUState
, fpr
[30]) },
3510 { "f31", offsetof(CPUState
, fpr
[31]) },
3511 { "fpscr", offsetof(CPUState
, fpscr
) },
3512 /* Next instruction pointer */
3513 { "nip|pc", offsetof(CPUState
, nip
) },
3514 { "lr", offsetof(CPUState
, lr
) },
3515 { "ctr", offsetof(CPUState
, ctr
) },
3516 { "decr", 0, &monitor_get_decr
, },
3517 { "ccr", 0, &monitor_get_ccr
, },
3518 /* Machine state register */
3519 { "msr", 0, &monitor_get_msr
, },
3520 { "xer", 0, &monitor_get_xer
, },
3521 { "tbu", 0, &monitor_get_tbu
, },
3522 { "tbl", 0, &monitor_get_tbl
, },
3523 #if defined(TARGET_PPC64)
3524 /* Address space register */
3525 { "asr", offsetof(CPUState
, asr
) },
3527 /* Segment registers */
3528 { "sdr1", offsetof(CPUState
, spr
[SPR_SDR1
]) },
3529 { "sr0", offsetof(CPUState
, sr
[0]) },
3530 { "sr1", offsetof(CPUState
, sr
[1]) },
3531 { "sr2", offsetof(CPUState
, sr
[2]) },
3532 { "sr3", offsetof(CPUState
, sr
[3]) },
3533 { "sr4", offsetof(CPUState
, sr
[4]) },
3534 { "sr5", offsetof(CPUState
, sr
[5]) },
3535 { "sr6", offsetof(CPUState
, sr
[6]) },
3536 { "sr7", offsetof(CPUState
, sr
[7]) },
3537 { "sr8", offsetof(CPUState
, sr
[8]) },
3538 { "sr9", offsetof(CPUState
, sr
[9]) },
3539 { "sr10", offsetof(CPUState
, sr
[10]) },
3540 { "sr11", offsetof(CPUState
, sr
[11]) },
3541 { "sr12", offsetof(CPUState
, sr
[12]) },
3542 { "sr13", offsetof(CPUState
, sr
[13]) },
3543 { "sr14", offsetof(CPUState
, sr
[14]) },
3544 { "sr15", offsetof(CPUState
, sr
[15]) },
3545 /* Too lazy to put BATs... */
3546 { "pvr", offsetof(CPUState
, spr
[SPR_PVR
]) },
3548 { "srr0", offsetof(CPUState
, spr
[SPR_SRR0
]) },
3549 { "srr1", offsetof(CPUState
, spr
[SPR_SRR1
]) },
3550 { "sprg0", offsetof(CPUState
, spr
[SPR_SPRG0
]) },
3551 { "sprg1", offsetof(CPUState
, spr
[SPR_SPRG1
]) },
3552 { "sprg2", offsetof(CPUState
, spr
[SPR_SPRG2
]) },
3553 { "sprg3", offsetof(CPUState
, spr
[SPR_SPRG3
]) },
3554 { "sprg4", offsetof(CPUState
, spr
[SPR_SPRG4
]) },
3555 { "sprg5", offsetof(CPUState
, spr
[SPR_SPRG5
]) },
3556 { "sprg6", offsetof(CPUState
, spr
[SPR_SPRG6
]) },
3557 { "sprg7", offsetof(CPUState
, spr
[SPR_SPRG7
]) },
3558 { "pid", offsetof(CPUState
, spr
[SPR_BOOKE_PID
]) },
3559 { "csrr0", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR0
]) },
3560 { "csrr1", offsetof(CPUState
, spr
[SPR_BOOKE_CSRR1
]) },
3561 { "esr", offsetof(CPUState
, spr
[SPR_BOOKE_ESR
]) },
3562 { "dear", offsetof(CPUState
, spr
[SPR_BOOKE_DEAR
]) },
3563 { "mcsr", offsetof(CPUState
, spr
[SPR_BOOKE_MCSR
]) },
3564 { "tsr", offsetof(CPUState
, spr
[SPR_BOOKE_TSR
]) },
3565 { "tcr", offsetof(CPUState
, spr
[SPR_BOOKE_TCR
]) },
3566 { "vrsave", offsetof(CPUState
, spr
[SPR_VRSAVE
]) },
3567 { "pir", offsetof(CPUState
, spr
[SPR_BOOKE_PIR
]) },
3568 { "mcsrr0", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR0
]) },
3569 { "mcsrr1", offsetof(CPUState
, spr
[SPR_BOOKE_MCSRR1
]) },
3570 { "decar", offsetof(CPUState
, spr
[SPR_BOOKE_DECAR
]) },
3571 { "ivpr", offsetof(CPUState
, spr
[SPR_BOOKE_IVPR
]) },
3572 { "epcr", offsetof(CPUState
, spr
[SPR_BOOKE_EPCR
]) },
3573 { "sprg8", offsetof(CPUState
, spr
[SPR_BOOKE_SPRG8
]) },
3574 { "ivor0", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR0
]) },
3575 { "ivor1", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR1
]) },
3576 { "ivor2", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR2
]) },
3577 { "ivor3", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR3
]) },
3578 { "ivor4", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR4
]) },
3579 { "ivor5", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR5
]) },
3580 { "ivor6", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR6
]) },
3581 { "ivor7", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR7
]) },
3582 { "ivor8", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR8
]) },
3583 { "ivor9", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR9
]) },
3584 { "ivor10", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR10
]) },
3585 { "ivor11", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR11
]) },
3586 { "ivor12", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR12
]) },
3587 { "ivor13", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR13
]) },
3588 { "ivor14", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR14
]) },
3589 { "ivor15", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR15
]) },
3590 { "ivor32", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR32
]) },
3591 { "ivor33", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR33
]) },
3592 { "ivor34", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR34
]) },
3593 { "ivor35", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR35
]) },
3594 { "ivor36", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR36
]) },
3595 { "ivor37", offsetof(CPUState
, spr
[SPR_BOOKE_IVOR37
]) },
3596 { "mas0", offsetof(CPUState
, spr
[SPR_BOOKE_MAS0
]) },
3597 { "mas1", offsetof(CPUState
, spr
[SPR_BOOKE_MAS1
]) },
3598 { "mas2", offsetof(CPUState
, spr
[SPR_BOOKE_MAS2
]) },
3599 { "mas3", offsetof(CPUState
, spr
[SPR_BOOKE_MAS3
]) },
3600 { "mas4", offsetof(CPUState
, spr
[SPR_BOOKE_MAS4
]) },
3601 { "mas6", offsetof(CPUState
, spr
[SPR_BOOKE_MAS6
]) },
3602 { "mas7", offsetof(CPUState
, spr
[SPR_BOOKE_MAS7
]) },
3603 { "mmucfg", offsetof(CPUState
, spr
[SPR_MMUCFG
]) },
3604 { "tlb0cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3605 { "tlb1cfg", offsetof(CPUState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3606 { "epr", offsetof(CPUState
, spr
[SPR_BOOKE_EPR
]) },
3607 { "eplc", offsetof(CPUState
, spr
[SPR_BOOKE_EPLC
]) },
3608 { "epsc", offsetof(CPUState
, spr
[SPR_BOOKE_EPSC
]) },
3609 { "svr", offsetof(CPUState
, spr
[SPR_E500_SVR
]) },
3610 { "mcar", offsetof(CPUState
, spr
[SPR_Exxx_MCAR
]) },
3611 { "pid1", offsetof(CPUState
, spr
[SPR_BOOKE_PID1
]) },
3612 { "pid2", offsetof(CPUState
, spr
[SPR_BOOKE_PID2
]) },
3613 { "hid0", offsetof(CPUState
, spr
[SPR_HID0
]) },
3615 #elif defined(TARGET_SPARC)
3616 { "g0", offsetof(CPUState
, gregs
[0]) },
3617 { "g1", offsetof(CPUState
, gregs
[1]) },
3618 { "g2", offsetof(CPUState
, gregs
[2]) },
3619 { "g3", offsetof(CPUState
, gregs
[3]) },
3620 { "g4", offsetof(CPUState
, gregs
[4]) },
3621 { "g5", offsetof(CPUState
, gregs
[5]) },
3622 { "g6", offsetof(CPUState
, gregs
[6]) },
3623 { "g7", offsetof(CPUState
, gregs
[7]) },
3624 { "o0", 0, monitor_get_reg
},
3625 { "o1", 1, monitor_get_reg
},
3626 { "o2", 2, monitor_get_reg
},
3627 { "o3", 3, monitor_get_reg
},
3628 { "o4", 4, monitor_get_reg
},
3629 { "o5", 5, monitor_get_reg
},
3630 { "o6", 6, monitor_get_reg
},
3631 { "o7", 7, monitor_get_reg
},
3632 { "l0", 8, monitor_get_reg
},
3633 { "l1", 9, monitor_get_reg
},
3634 { "l2", 10, monitor_get_reg
},
3635 { "l3", 11, monitor_get_reg
},
3636 { "l4", 12, monitor_get_reg
},
3637 { "l5", 13, monitor_get_reg
},
3638 { "l6", 14, monitor_get_reg
},
3639 { "l7", 15, monitor_get_reg
},
3640 { "i0", 16, monitor_get_reg
},
3641 { "i1", 17, monitor_get_reg
},
3642 { "i2", 18, monitor_get_reg
},
3643 { "i3", 19, monitor_get_reg
},
3644 { "i4", 20, monitor_get_reg
},
3645 { "i5", 21, monitor_get_reg
},
3646 { "i6", 22, monitor_get_reg
},
3647 { "i7", 23, monitor_get_reg
},
3648 { "pc", offsetof(CPUState
, pc
) },
3649 { "npc", offsetof(CPUState
, npc
) },
3650 { "y", offsetof(CPUState
, y
) },
3651 #ifndef TARGET_SPARC64
3652 { "psr", 0, &monitor_get_psr
, },
3653 { "wim", offsetof(CPUState
, wim
) },
3655 { "tbr", offsetof(CPUState
, tbr
) },
3656 { "fsr", offsetof(CPUState
, fsr
) },
3657 { "f0", offsetof(CPUState
, fpr
[0]) },
3658 { "f1", offsetof(CPUState
, fpr
[1]) },
3659 { "f2", offsetof(CPUState
, fpr
[2]) },
3660 { "f3", offsetof(CPUState
, fpr
[3]) },
3661 { "f4", offsetof(CPUState
, fpr
[4]) },
3662 { "f5", offsetof(CPUState
, fpr
[5]) },
3663 { "f6", offsetof(CPUState
, fpr
[6]) },
3664 { "f7", offsetof(CPUState
, fpr
[7]) },
3665 { "f8", offsetof(CPUState
, fpr
[8]) },
3666 { "f9", offsetof(CPUState
, fpr
[9]) },
3667 { "f10", offsetof(CPUState
, fpr
[10]) },
3668 { "f11", offsetof(CPUState
, fpr
[11]) },
3669 { "f12", offsetof(CPUState
, fpr
[12]) },
3670 { "f13", offsetof(CPUState
, fpr
[13]) },
3671 { "f14", offsetof(CPUState
, fpr
[14]) },
3672 { "f15", offsetof(CPUState
, fpr
[15]) },
3673 { "f16", offsetof(CPUState
, fpr
[16]) },
3674 { "f17", offsetof(CPUState
, fpr
[17]) },
3675 { "f18", offsetof(CPUState
, fpr
[18]) },
3676 { "f19", offsetof(CPUState
, fpr
[19]) },
3677 { "f20", offsetof(CPUState
, fpr
[20]) },
3678 { "f21", offsetof(CPUState
, fpr
[21]) },
3679 { "f22", offsetof(CPUState
, fpr
[22]) },
3680 { "f23", offsetof(CPUState
, fpr
[23]) },
3681 { "f24", offsetof(CPUState
, fpr
[24]) },
3682 { "f25", offsetof(CPUState
, fpr
[25]) },
3683 { "f26", offsetof(CPUState
, fpr
[26]) },
3684 { "f27", offsetof(CPUState
, fpr
[27]) },
3685 { "f28", offsetof(CPUState
, fpr
[28]) },
3686 { "f29", offsetof(CPUState
, fpr
[29]) },
3687 { "f30", offsetof(CPUState
, fpr
[30]) },
3688 { "f31", offsetof(CPUState
, fpr
[31]) },
3689 #ifdef TARGET_SPARC64
3690 { "f32", offsetof(CPUState
, fpr
[32]) },
3691 { "f34", offsetof(CPUState
, fpr
[34]) },
3692 { "f36", offsetof(CPUState
, fpr
[36]) },
3693 { "f38", offsetof(CPUState
, fpr
[38]) },
3694 { "f40", offsetof(CPUState
, fpr
[40]) },
3695 { "f42", offsetof(CPUState
, fpr
[42]) },
3696 { "f44", offsetof(CPUState
, fpr
[44]) },
3697 { "f46", offsetof(CPUState
, fpr
[46]) },
3698 { "f48", offsetof(CPUState
, fpr
[48]) },
3699 { "f50", offsetof(CPUState
, fpr
[50]) },
3700 { "f52", offsetof(CPUState
, fpr
[52]) },
3701 { "f54", offsetof(CPUState
, fpr
[54]) },
3702 { "f56", offsetof(CPUState
, fpr
[56]) },
3703 { "f58", offsetof(CPUState
, fpr
[58]) },
3704 { "f60", offsetof(CPUState
, fpr
[60]) },
3705 { "f62", offsetof(CPUState
, fpr
[62]) },
3706 { "asi", offsetof(CPUState
, asi
) },
3707 { "pstate", offsetof(CPUState
, pstate
) },
3708 { "cansave", offsetof(CPUState
, cansave
) },
3709 { "canrestore", offsetof(CPUState
, canrestore
) },
3710 { "otherwin", offsetof(CPUState
, otherwin
) },
3711 { "wstate", offsetof(CPUState
, wstate
) },
3712 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3713 { "fprs", offsetof(CPUState
, fprs
) },
3719 static void expr_error(Monitor
*mon
, const char *msg
)
3721 monitor_printf(mon
, "%s\n", msg
);
3722 longjmp(expr_env
, 1);
3725 /* return 0 if OK, -1 if not found */
3726 static int get_monitor_def(target_long
*pval
, const char *name
)
3728 const MonitorDef
*md
;
3731 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3732 if (compare_cmd(name
, md
->name
)) {
3733 if (md
->get_value
) {
3734 *pval
= md
->get_value(md
, md
->offset
);
3736 CPUState
*env
= mon_get_cpu();
3737 ptr
= (uint8_t *)env
+ md
->offset
;
3740 *pval
= *(int32_t *)ptr
;
3743 *pval
= *(target_long
*)ptr
;
3756 static void next(void)
3760 while (qemu_isspace(*pch
))
3765 static int64_t expr_sum(Monitor
*mon
);
3767 static int64_t expr_unary(Monitor
*mon
)
3776 n
= expr_unary(mon
);
3780 n
= -expr_unary(mon
);
3784 n
= ~expr_unary(mon
);
3790 expr_error(mon
, "')' expected");
3797 expr_error(mon
, "character constant expected");
3801 expr_error(mon
, "missing terminating \' character");
3811 while ((*pch
>= 'a' && *pch
<= 'z') ||
3812 (*pch
>= 'A' && *pch
<= 'Z') ||
3813 (*pch
>= '0' && *pch
<= '9') ||
3814 *pch
== '_' || *pch
== '.') {
3815 if ((q
- buf
) < sizeof(buf
) - 1)
3819 while (qemu_isspace(*pch
))
3822 ret
= get_monitor_def(®
, buf
);
3824 expr_error(mon
, "unknown register");
3829 expr_error(mon
, "unexpected end of expression");
3833 #if TARGET_PHYS_ADDR_BITS > 32
3834 n
= strtoull(pch
, &p
, 0);
3836 n
= strtoul(pch
, &p
, 0);
3839 expr_error(mon
, "invalid char in expression");
3842 while (qemu_isspace(*pch
))
3850 static int64_t expr_prod(Monitor
*mon
)
3855 val
= expr_unary(mon
);
3858 if (op
!= '*' && op
!= '/' && op
!= '%')
3861 val2
= expr_unary(mon
);
3870 expr_error(mon
, "division by zero");
3881 static int64_t expr_logic(Monitor
*mon
)
3886 val
= expr_prod(mon
);
3889 if (op
!= '&' && op
!= '|' && op
!= '^')
3892 val2
= expr_prod(mon
);
3909 static int64_t expr_sum(Monitor
*mon
)
3914 val
= expr_logic(mon
);
3917 if (op
!= '+' && op
!= '-')
3920 val2
= expr_logic(mon
);
3929 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3932 if (setjmp(expr_env
)) {
3936 while (qemu_isspace(*pch
))
3938 *pval
= expr_sum(mon
);
3943 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3945 const char *p
= *pp
;
3949 d
= strtod(p
, &tailp
);
3951 monitor_printf(mon
, "Number expected\n");
3954 if (d
!= d
|| d
- d
!= 0) {
3955 /* NaN or infinity */
3956 monitor_printf(mon
, "Bad number\n");
3964 static int get_str(char *buf
, int buf_size
, const char **pp
)
3972 while (qemu_isspace(*p
))
3982 while (*p
!= '\0' && *p
!= '\"') {
3998 qemu_printf("unsupported escape code: '\\%c'\n", c
);
4001 if ((q
- buf
) < buf_size
- 1) {
4005 if ((q
- buf
) < buf_size
- 1) {
4012 qemu_printf("unterminated string\n");
4017 while (*p
!= '\0' && !qemu_isspace(*p
)) {
4018 if ((q
- buf
) < buf_size
- 1) {
4030 * Store the command-name in cmdname, and return a pointer to
4031 * the remaining of the command string.
4033 static const char *get_command_name(const char *cmdline
,
4034 char *cmdname
, size_t nlen
)
4037 const char *p
, *pstart
;
4040 while (qemu_isspace(*p
))
4045 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
4050 memcpy(cmdname
, pstart
, len
);
4051 cmdname
[len
] = '\0';
4056 * Read key of 'type' into 'key' and return the current
4059 static char *key_get_info(const char *type
, char **key
)
4067 p
= strchr(type
, ':');
4074 str
= g_malloc(len
+ 1);
4075 memcpy(str
, type
, len
);
4082 static int default_fmt_format
= 'x';
4083 static int default_fmt_size
= 4;
4087 static int is_valid_option(const char *c
, const char *typestr
)
4095 typestr
= strstr(typestr
, option
);
4096 return (typestr
!= NULL
);
4099 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
4100 const char *cmdname
)
4102 const mon_cmd_t
*cmd
;
4104 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
4105 if (compare_cmd(cmdname
, cmd
->name
)) {
4113 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
4115 return search_dispatch_table(mon_cmds
, cmdname
);
4118 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
4120 return search_dispatch_table(qmp_query_cmds
, info_item
);
4123 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
4125 return search_dispatch_table(qmp_cmds
, cmdname
);
4128 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
4129 const char *cmdline
,
4132 const char *p
, *typestr
;
4134 const mon_cmd_t
*cmd
;
4140 monitor_printf(mon
, "command='%s'\n", cmdline
);
4143 /* extract the command name */
4144 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
4148 cmd
= monitor_find_command(cmdname
);
4150 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
4154 /* parse the parameters */
4155 typestr
= cmd
->args_type
;
4157 typestr
= key_get_info(typestr
, &key
);
4169 while (qemu_isspace(*p
))
4171 if (*typestr
== '?') {
4174 /* no optional string: NULL argument */
4178 ret
= get_str(buf
, sizeof(buf
), &p
);
4182 monitor_printf(mon
, "%s: filename expected\n",
4186 monitor_printf(mon
, "%s: block device name expected\n",
4190 monitor_printf(mon
, "%s: string expected\n", cmdname
);
4195 qdict_put(qdict
, key
, qstring_from_str(buf
));
4200 QemuOptsList
*opts_list
;
4203 opts_list
= qemu_find_opts(key
);
4204 if (!opts_list
|| opts_list
->desc
->name
) {
4207 while (qemu_isspace(*p
)) {
4212 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
4215 opts
= qemu_opts_parse(opts_list
, buf
, 1);
4219 qemu_opts_to_qdict(opts
, qdict
);
4220 qemu_opts_del(opts
);
4225 int count
, format
, size
;
4227 while (qemu_isspace(*p
))
4233 if (qemu_isdigit(*p
)) {
4235 while (qemu_isdigit(*p
)) {
4236 count
= count
* 10 + (*p
- '0');
4274 if (*p
!= '\0' && !qemu_isspace(*p
)) {
4275 monitor_printf(mon
, "invalid char in format: '%c'\n",
4280 format
= default_fmt_format
;
4281 if (format
!= 'i') {
4282 /* for 'i', not specifying a size gives -1 as size */
4284 size
= default_fmt_size
;
4285 default_fmt_size
= size
;
4287 default_fmt_format
= format
;
4290 format
= default_fmt_format
;
4291 if (format
!= 'i') {
4292 size
= default_fmt_size
;
4297 qdict_put(qdict
, "count", qint_from_int(count
));
4298 qdict_put(qdict
, "format", qint_from_int(format
));
4299 qdict_put(qdict
, "size", qint_from_int(size
));
4308 while (qemu_isspace(*p
))
4310 if (*typestr
== '?' || *typestr
== '.') {
4311 if (*typestr
== '?') {
4319 while (qemu_isspace(*p
))
4328 if (get_expr(mon
, &val
, &p
))
4330 /* Check if 'i' is greater than 32-bit */
4331 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
4332 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
4333 monitor_printf(mon
, "integer is for 32-bit values\n");
4335 } else if (c
== 'M') {
4338 qdict_put(qdict
, key
, qint_from_int(val
));
4346 while (qemu_isspace(*p
)) {
4349 if (*typestr
== '?') {
4355 val
= strtosz(p
, &end
);
4357 monitor_printf(mon
, "invalid size\n");
4360 qdict_put(qdict
, key
, qint_from_int(val
));
4368 while (qemu_isspace(*p
))
4370 if (*typestr
== '?') {
4376 if (get_double(mon
, &val
, &p
) < 0) {
4379 if (p
[0] && p
[1] == 's') {
4382 val
/= 1e3
; p
+= 2; break;
4384 val
/= 1e6
; p
+= 2; break;
4386 val
/= 1e9
; p
+= 2; break;
4389 if (*p
&& !qemu_isspace(*p
)) {
4390 monitor_printf(mon
, "Unknown unit suffix\n");
4393 qdict_put(qdict
, key
, qfloat_from_double(val
));
4401 while (qemu_isspace(*p
)) {
4405 while (qemu_isgraph(*p
)) {
4408 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4410 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4413 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4416 qdict_put(qdict
, key
, qbool_from_int(val
));
4421 const char *tmp
= p
;
4428 while (qemu_isspace(*p
))
4433 if(!is_valid_option(p
, typestr
)) {
4435 monitor_printf(mon
, "%s: unsupported option -%c\n",
4447 qdict_put(qdict
, key
, qbool_from_int(1));
4454 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4460 /* check that all arguments were parsed */
4461 while (qemu_isspace(*p
))
4464 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4476 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4478 /* report only the first error */
4480 mon
->error
= qerror
;
4482 MON_DEBUG("Additional error report at %s:%d\n",
4483 qerror
->file
, qerror
->linenr
);
4488 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4490 if (ret
&& !monitor_has_error(mon
)) {
4492 * If it returns failure, it must have passed on error.
4494 * Action: Report an internal error to the client if in QMP.
4496 qerror_report(QERR_UNDEFINED_ERROR
);
4497 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
4501 #ifdef CONFIG_DEBUG_MONITOR
4502 if (!ret
&& monitor_has_error(mon
)) {
4504 * If it returns success, it must not have passed an error.
4506 * Action: Report the passed error to the client.
4508 MON_DEBUG("command '%s' returned success but passed an error\n",
4512 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
4514 * Handlers should not call Monitor print functions.
4516 * Action: Ignore them in QMP.
4518 * (XXX: we don't check any 'info' or 'query' command here
4519 * because the user print function _is_ called by do_info(), hence
4520 * we will trigger this check. This problem will go away when we
4521 * make 'query' commands real and kill do_info())
4523 MON_DEBUG("command '%s' called print functions %d time(s)\n",
4524 cmd
->name
, mon_print_count_get(mon
));
4529 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4532 const mon_cmd_t
*cmd
;
4534 qdict
= qdict_new();
4536 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4540 if (handler_is_async(cmd
)) {
4541 user_async_cmd_handler(mon
, cmd
, qdict
);
4542 } else if (handler_is_qobject(cmd
)) {
4543 QObject
*data
= NULL
;
4545 /* XXX: ignores the error code */
4546 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4547 assert(!monitor_has_error(mon
));
4549 cmd
->user_print(mon
, data
);
4550 qobject_decref(data
);
4553 cmd
->mhandler
.cmd(mon
, qdict
);
4560 static void cmd_completion(const char *name
, const char *list
)
4562 const char *p
, *pstart
;
4571 p
= pstart
+ strlen(pstart
);
4573 if (len
> sizeof(cmd
) - 2)
4574 len
= sizeof(cmd
) - 2;
4575 memcpy(cmd
, pstart
, len
);
4577 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4578 readline_add_completion(cur_mon
->rs
, cmd
);
4586 static void file_completion(const char *input
)
4591 char file
[1024], file_prefix
[1024];
4595 p
= strrchr(input
, '/');
4598 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4599 pstrcpy(path
, sizeof(path
), ".");
4601 input_path_len
= p
- input
+ 1;
4602 memcpy(path
, input
, input_path_len
);
4603 if (input_path_len
> sizeof(path
) - 1)
4604 input_path_len
= sizeof(path
) - 1;
4605 path
[input_path_len
] = '\0';
4606 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4608 #ifdef DEBUG_COMPLETION
4609 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4610 input
, path
, file_prefix
);
4612 ffs
= opendir(path
);
4621 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4625 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4626 memcpy(file
, input
, input_path_len
);
4627 if (input_path_len
< sizeof(file
))
4628 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4630 /* stat the file to find out if it's a directory.
4631 * In that case add a slash to speed up typing long paths
4634 if(S_ISDIR(sb
.st_mode
))
4635 pstrcat(file
, sizeof(file
), "/");
4636 readline_add_completion(cur_mon
->rs
, file
);
4642 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4644 const char *name
= bdrv_get_device_name(bs
);
4645 const char *input
= opaque
;
4647 if (input
[0] == '\0' ||
4648 !strncmp(name
, (char *)input
, strlen(input
))) {
4649 readline_add_completion(cur_mon
->rs
, name
);
4653 /* NOTE: this parser is an approximate form of the real command parser */
4654 static void parse_cmdline(const char *cmdline
,
4655 int *pnb_args
, char **args
)
4664 while (qemu_isspace(*p
))
4668 if (nb_args
>= MAX_ARGS
)
4670 ret
= get_str(buf
, sizeof(buf
), &p
);
4671 args
[nb_args
] = g_strdup(buf
);
4676 *pnb_args
= nb_args
;
4679 static const char *next_arg_type(const char *typestr
)
4681 const char *p
= strchr(typestr
, ':');
4682 return (p
!= NULL
? ++p
: typestr
);
4685 static void monitor_find_completion(const char *cmdline
)
4687 const char *cmdname
;
4688 char *args
[MAX_ARGS
];
4689 int nb_args
, i
, len
;
4690 const char *ptype
, *str
;
4691 const mon_cmd_t
*cmd
;
4694 parse_cmdline(cmdline
, &nb_args
, args
);
4695 #ifdef DEBUG_COMPLETION
4696 for(i
= 0; i
< nb_args
; i
++) {
4697 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4701 /* if the line ends with a space, it means we want to complete the
4703 len
= strlen(cmdline
);
4704 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4705 if (nb_args
>= MAX_ARGS
) {
4708 args
[nb_args
++] = g_strdup("");
4711 /* command completion */
4716 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4717 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4718 cmd_completion(cmdname
, cmd
->name
);
4721 /* find the command */
4722 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4723 if (compare_cmd(args
[0], cmd
->name
)) {
4731 ptype
= next_arg_type(cmd
->args_type
);
4732 for(i
= 0; i
< nb_args
- 2; i
++) {
4733 if (*ptype
!= '\0') {
4734 ptype
= next_arg_type(ptype
);
4735 while (*ptype
== '?')
4736 ptype
= next_arg_type(ptype
);
4739 str
= args
[nb_args
- 1];
4740 if (*ptype
== '-' && ptype
[1] != '\0') {
4741 ptype
= next_arg_type(ptype
);
4745 /* file completion */
4746 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4747 file_completion(str
);
4750 /* block device name completion */
4751 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4752 bdrv_iterate(block_completion_it
, (void *)str
);
4755 /* XXX: more generic ? */
4756 if (!strcmp(cmd
->name
, "info")) {
4757 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4758 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4759 cmd_completion(str
, cmd
->name
);
4761 } else if (!strcmp(cmd
->name
, "sendkey")) {
4762 char *sep
= strrchr(str
, '-');
4765 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4766 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4767 cmd_completion(str
, key
->name
);
4769 } else if (!strcmp(cmd
->name
, "help|?")) {
4770 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4771 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4772 cmd_completion(str
, cmd
->name
);
4782 for (i
= 0; i
< nb_args
; i
++) {
4787 static int monitor_can_read(void *opaque
)
4789 Monitor
*mon
= opaque
;
4791 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4794 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4796 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4797 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4801 * Argument validation rules:
4803 * 1. The argument must exist in cmd_args qdict
4804 * 2. The argument type must be the expected one
4806 * Special case: If the argument doesn't exist in cmd_args and
4807 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4808 * checking is skipped for it.
4810 static int check_client_args_type(const QDict
*client_args
,
4811 const QDict
*cmd_args
, int flags
)
4813 const QDictEntry
*ent
;
4815 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4818 const QObject
*client_arg
= qdict_entry_value(ent
);
4819 const char *client_arg_name
= qdict_entry_key(ent
);
4821 obj
= qdict_get(cmd_args
, client_arg_name
);
4823 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4824 /* handler accepts unknowns */
4827 /* client arg doesn't exist */
4828 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4832 arg_type
= qobject_to_qstring(obj
);
4833 assert(arg_type
!= NULL
);
4835 /* check if argument's type is correct */
4836 switch (qstring_get_str(arg_type
)[0]) {
4840 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4841 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4850 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4851 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4857 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4858 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4859 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4866 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4867 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4873 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4878 * These types are not supported by QMP and thus are not
4879 * handled here. Fall through.
4890 * - Check if the client has passed all mandatory args
4891 * - Set special flags for argument validation
4893 static int check_mandatory_args(const QDict
*cmd_args
,
4894 const QDict
*client_args
, int *flags
)
4896 const QDictEntry
*ent
;
4898 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4899 const char *cmd_arg_name
= qdict_entry_key(ent
);
4900 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4901 assert(type
!= NULL
);
4903 if (qstring_get_str(type
)[0] == 'O') {
4904 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4905 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4906 } else if (qstring_get_str(type
)[0] != '-' &&
4907 qstring_get_str(type
)[1] != '?' &&
4908 !qdict_haskey(client_args
, cmd_arg_name
)) {
4909 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4917 static QDict
*qdict_from_args_type(const char *args_type
)
4921 QString
*key
, *type
, *cur_qs
;
4923 assert(args_type
!= NULL
);
4925 qdict
= qdict_new();
4927 if (args_type
== NULL
|| args_type
[0] == '\0') {
4928 /* no args, empty qdict */
4932 key
= qstring_new();
4933 type
= qstring_new();
4938 switch (args_type
[i
]) {
4941 qdict_put(qdict
, qstring_get_str(key
), type
);
4943 if (args_type
[i
] == '\0') {
4946 type
= qstring_new(); /* qdict has ref */
4947 cur_qs
= key
= qstring_new();
4953 qstring_append_chr(cur_qs
, args_type
[i
]);
4963 * Client argument checking rules:
4965 * 1. Client must provide all mandatory arguments
4966 * 2. Each argument provided by the client must be expected
4967 * 3. Each argument provided by the client must have the type expected
4970 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4975 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4978 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4983 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4991 * Input object checking rules
4993 * 1. Input object must be a dict
4994 * 2. The "execute" key must exist
4995 * 3. The "execute" key must be a string
4996 * 4. If the "arguments" key exists, it must be a dict
4997 * 5. If the "id" key exists, it can be anything (ie. json-value)
4998 * 6. Any argument not listed above is considered invalid
5000 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
5002 const QDictEntry
*ent
;
5003 int has_exec_key
= 0;
5006 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
5007 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
5011 input_dict
= qobject_to_qdict(input_obj
);
5013 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
5014 const char *arg_name
= qdict_entry_key(ent
);
5015 const QObject
*arg_obj
= qdict_entry_value(ent
);
5017 if (!strcmp(arg_name
, "execute")) {
5018 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
5019 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
5024 } else if (!strcmp(arg_name
, "arguments")) {
5025 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
5026 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
5030 } else if (!strcmp(arg_name
, "id")) {
5031 /* FIXME: check duplicated IDs for async commands */
5033 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
5038 if (!has_exec_key
) {
5039 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
5046 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
5048 QObject
*ret_data
= NULL
;
5050 if (handler_is_async(cmd
)) {
5051 qmp_async_info_handler(mon
, cmd
);
5052 if (monitor_has_error(mon
)) {
5053 monitor_protocol_emitter(mon
, NULL
);
5056 cmd
->mhandler
.info_new(mon
, &ret_data
);
5057 monitor_protocol_emitter(mon
, ret_data
);
5058 qobject_decref(ret_data
);
5062 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
5063 const QDict
*params
)
5066 QObject
*data
= NULL
;
5068 mon_print_count_init(mon
);
5070 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
5071 handler_audit(mon
, cmd
, ret
);
5072 monitor_protocol_emitter(mon
, data
);
5073 qobject_decref(data
);
5076 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
5080 QDict
*input
, *args
;
5081 const mon_cmd_t
*cmd
;
5082 Monitor
*mon
= cur_mon
;
5083 const char *cmd_name
, *query_cmd
;
5086 args
= input
= NULL
;
5088 obj
= json_parser_parse(tokens
, NULL
);
5090 // FIXME: should be triggered in json_parser_parse()
5091 qerror_report(QERR_JSON_PARSING
);
5095 input
= qmp_check_input_obj(obj
);
5097 qobject_decref(obj
);
5101 mon
->mc
->id
= qdict_get(input
, "id");
5102 qobject_incref(mon
->mc
->id
);
5104 cmd_name
= qdict_get_str(input
, "execute");
5105 if (invalid_qmp_mode(mon
, cmd_name
)) {
5106 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
5110 if (strstart(cmd_name
, "query-", &query_cmd
)) {
5111 cmd
= qmp_find_query_cmd(query_cmd
);
5113 cmd
= qmp_find_cmd(cmd_name
);
5117 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
5121 obj
= qdict_get(input
, "arguments");
5125 args
= qobject_to_qdict(obj
);
5129 err
= qmp_check_client_args(cmd
, args
);
5135 qmp_call_query_cmd(mon
, cmd
);
5136 } else if (handler_is_async(cmd
)) {
5137 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
5139 /* emit the error response */
5143 qmp_call_cmd(mon
, cmd
, args
);
5149 monitor_protocol_emitter(mon
, NULL
);
5156 * monitor_control_read(): Read and handle QMP input
5158 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
5160 Monitor
*old_mon
= cur_mon
;
5164 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
5169 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
5171 Monitor
*old_mon
= cur_mon
;
5177 for (i
= 0; i
< size
; i
++)
5178 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
5180 if (size
== 0 || buf
[size
- 1] != 0)
5181 monitor_printf(cur_mon
, "corrupted command\n");
5183 handle_user_command(cur_mon
, (char *)buf
);
5189 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
5191 monitor_suspend(mon
);
5192 handle_user_command(mon
, cmdline
);
5193 monitor_resume(mon
);
5196 int monitor_suspend(Monitor
*mon
)
5204 void monitor_resume(Monitor
*mon
)
5208 if (--mon
->suspend_cnt
== 0)
5209 readline_show_prompt(mon
->rs
);
5212 static QObject
*get_qmp_greeting(void)
5216 do_info_version(NULL
, &ver
);
5217 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
5221 * monitor_control_event(): Print QMP gretting
5223 static void monitor_control_event(void *opaque
, int event
)
5226 Monitor
*mon
= opaque
;
5229 case CHR_EVENT_OPENED
:
5230 mon
->mc
->command_mode
= 0;
5231 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5232 data
= get_qmp_greeting();
5233 monitor_json_emitter(mon
, data
);
5234 qobject_decref(data
);
5236 case CHR_EVENT_CLOSED
:
5237 json_message_parser_destroy(&mon
->mc
->parser
);
5242 static void monitor_event(void *opaque
, int event
)
5244 Monitor
*mon
= opaque
;
5247 case CHR_EVENT_MUX_IN
:
5249 if (mon
->reset_seen
) {
5250 readline_restart(mon
->rs
);
5251 monitor_resume(mon
);
5254 mon
->suspend_cnt
= 0;
5258 case CHR_EVENT_MUX_OUT
:
5259 if (mon
->reset_seen
) {
5260 if (mon
->suspend_cnt
== 0) {
5261 monitor_printf(mon
, "\n");
5264 monitor_suspend(mon
);
5271 case CHR_EVENT_OPENED
:
5272 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
5273 "information\n", QEMU_VERSION
);
5274 if (!mon
->mux_out
) {
5275 readline_show_prompt(mon
->rs
);
5277 mon
->reset_seen
= 1;
5291 void monitor_init(CharDriverState
*chr
, int flags
)
5293 static int is_first_init
= 1;
5296 if (is_first_init
) {
5297 key_timer
= qemu_new_timer_ns(vm_clock
, release_keys
, NULL
);
5301 mon
= g_malloc0(sizeof(*mon
));
5305 if (flags
& MONITOR_USE_READLINE
) {
5306 mon
->rs
= readline_init(mon
, monitor_find_completion
);
5307 monitor_read_command(mon
, 0);
5310 if (monitor_ctrl_mode(mon
)) {
5311 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
5312 /* Control mode requires special handlers */
5313 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
5314 monitor_control_event
, mon
);
5315 qemu_chr_fe_set_echo(chr
, true);
5317 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5318 monitor_event
, mon
);
5321 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5322 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5326 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
5328 BlockDriverState
*bs
= opaque
;
5331 if (bdrv_set_key(bs
, password
) != 0) {
5332 monitor_printf(mon
, "invalid password\n");
5335 if (mon
->password_completion_cb
)
5336 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5338 monitor_read_command(mon
, 1);
5341 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5342 BlockDriverCompletionFunc
*completion_cb
,
5347 if (!bdrv_key_required(bs
)) {
5349 completion_cb(opaque
, 0);
5353 if (monitor_ctrl_mode(mon
)) {
5354 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
5358 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5359 bdrv_get_encrypted_filename(bs
));
5361 mon
->password_completion_cb
= completion_cb
;
5362 mon
->password_opaque
= opaque
;
5364 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5366 if (err
&& completion_cb
)
5367 completion_cb(opaque
, err
);