4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
42 #include "audio/audio.h"
45 #include "qemu-timer.h"
46 #include "migration.h"
55 #include "json-streamer.h"
56 #include "json-parser.h"
59 #ifdef CONFIG_SIMPLE_TRACE
64 //#define DEBUG_COMPLETION
70 * 'B' block device name
71 * 's' string (accept optional quote)
72 * 'O' option string of the form NAME=VALUE,...
73 * parsed according to QemuOptsList given by its name
74 * Example: 'device:O' uses qemu_device_opts.
75 * Restriction: only lists with empty desc are supported
76 * TODO lift the restriction
78 * 'l' target long (32 or 64 bit)
79 * 'M' just like 'l', except in user mode the value is
80 * multiplied by 2^20 (think Mebibyte)
81 * 'o' octets (aka bytes)
82 * user mode accepts an optional T, t, G, g, M, m, K, k
83 * suffix, which multiplies the value by 2^40 for
84 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
85 * M and m, 2^10 for K and k
87 * user mode accepts an optional ms, us, ns suffix,
88 * which divides the value by 1e3, 1e6, 1e9, respectively
89 * '/' optional gdb-like print format (like "/10x")
91 * '?' optional type (for all types, except '/')
92 * '.' other form of optional type (for 'i' and 'l')
94 * user mode accepts "on" or "off"
95 * '-' optional parameter (eg. '-f')
99 typedef struct MonitorCompletionData MonitorCompletionData
;
100 struct MonitorCompletionData
{
102 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
105 typedef struct mon_cmd_t
{
107 const char *args_type
;
110 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
112 void (*info
)(Monitor
*mon
);
113 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
114 int (*info_async
)(Monitor
*mon
, MonitorCompletion
*cb
, void *opaque
);
115 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
116 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
117 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
118 MonitorCompletion
*cb
, void *opaque
);
123 /* file descriptors passed via SCM_RIGHTS */
124 typedef struct mon_fd_t mon_fd_t
;
128 QLIST_ENTRY(mon_fd_t
) next
;
131 typedef struct MonitorControl
{
133 JSONMessageParser parser
;
138 CharDriverState
*chr
;
143 uint8_t outbuf
[1024];
148 BlockDriverCompletionFunc
*password_completion_cb
;
149 void *password_opaque
;
150 #ifdef CONFIG_DEBUG_MONITOR
154 QLIST_HEAD(,mon_fd_t
) fds
;
155 QLIST_ENTRY(Monitor
) entry
;
158 #ifdef CONFIG_DEBUG_MONITOR
159 #define MON_DEBUG(fmt, ...) do { \
160 fprintf(stderr, "Monitor: "); \
161 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
163 static inline void mon_print_count_inc(Monitor
*mon
)
165 mon
->print_calls_nr
++;
168 static inline void mon_print_count_init(Monitor
*mon
)
170 mon
->print_calls_nr
= 0;
173 static inline int mon_print_count_get(const Monitor
*mon
)
175 return mon
->print_calls_nr
;
178 #else /* !CONFIG_DEBUG_MONITOR */
179 #define MON_DEBUG(fmt, ...) do { } while (0)
180 static inline void mon_print_count_inc(Monitor
*mon
) { }
181 static inline void mon_print_count_init(Monitor
*mon
) { }
182 static inline int mon_print_count_get(const Monitor
*mon
) { return 0; }
183 #endif /* CONFIG_DEBUG_MONITOR */
185 /* QMP checker flags */
186 #define QMP_ACCEPT_UNKNOWNS 1
188 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
190 static const mon_cmd_t mon_cmds
[];
191 static const mon_cmd_t info_cmds
[];
193 static const mon_cmd_t qmp_cmds
[];
194 static const mon_cmd_t qmp_query_cmds
[];
197 Monitor
*default_mon
;
199 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
202 static inline int qmp_cmd_mode(const Monitor
*mon
)
204 return (mon
->mc
? mon
->mc
->command_mode
: 0);
207 /* Return true if in control mode, false otherwise */
208 static inline int monitor_ctrl_mode(const Monitor
*mon
)
210 return (mon
->flags
& MONITOR_USE_CONTROL
);
213 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
214 int monitor_cur_is_qmp(void)
216 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
219 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
224 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
226 readline_show_prompt(mon
->rs
);
229 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
232 if (monitor_ctrl_mode(mon
)) {
233 qerror_report(QERR_MISSING_PARAMETER
, "password");
235 } else if (mon
->rs
) {
236 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
237 /* prompt is printed on return from the command handler */
240 monitor_printf(mon
, "terminal does not support password prompting\n");
245 void monitor_flush(Monitor
*mon
)
247 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
248 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
249 mon
->outbuf_index
= 0;
253 /* flush at every end of line or if the buffer is full */
254 static void monitor_puts(Monitor
*mon
, const char *str
)
263 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
264 mon
->outbuf
[mon
->outbuf_index
++] = c
;
265 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
271 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
278 mon_print_count_inc(mon
);
280 if (monitor_ctrl_mode(mon
)) {
284 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
285 monitor_puts(mon
, buf
);
288 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
292 monitor_vprintf(mon
, fmt
, ap
);
296 void monitor_print_filename(Monitor
*mon
, const char *filename
)
300 for (i
= 0; filename
[i
]; i
++) {
301 switch (filename
[i
]) {
305 monitor_printf(mon
, "\\%c", filename
[i
]);
308 monitor_printf(mon
, "\\t");
311 monitor_printf(mon
, "\\r");
314 monitor_printf(mon
, "\\n");
317 monitor_printf(mon
, "%c", filename
[i
]);
323 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
324 const char *fmt
, ...)
328 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
333 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
335 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
337 return cmd
->user_print
!= NULL
;
340 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
342 return cmd
->flags
& MONITOR_CMD_ASYNC
;
345 static inline int monitor_has_error(const Monitor
*mon
)
347 return mon
->error
!= NULL
;
350 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
354 if (mon
->flags
& MONITOR_USE_PRETTY
)
355 json
= qobject_to_json_pretty(data
);
357 json
= qobject_to_json(data
);
358 assert(json
!= NULL
);
360 qstring_append_chr(json
, '\n');
361 monitor_puts(mon
, qstring_get_str(json
));
366 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
372 if (!monitor_has_error(mon
)) {
373 /* success response */
375 qobject_incref(data
);
376 qdict_put_obj(qmp
, "return", data
);
378 /* return an empty QDict by default */
379 qdict_put(qmp
, "return", qdict_new());
383 qdict_put(mon
->error
->error
, "desc", qerror_human(mon
->error
));
384 qdict_put(qmp
, "error", mon
->error
->error
);
385 QINCREF(mon
->error
->error
);
391 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
395 monitor_json_emitter(mon
, QOBJECT(qmp
));
399 static void timestamp_put(QDict
*qdict
)
405 err
= qemu_gettimeofday(&tv
);
409 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
410 "'microseconds': %" PRId64
" }",
411 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
412 qdict_put_obj(qdict
, "timestamp", obj
);
416 * monitor_protocol_event(): Generate a Monitor event
418 * Event-specific data can be emitted through the (optional) 'data' parameter.
420 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
423 const char *event_name
;
426 assert(event
< QEVENT_MAX
);
429 case QEVENT_SHUTDOWN
:
430 event_name
= "SHUTDOWN";
433 event_name
= "RESET";
435 case QEVENT_POWERDOWN
:
436 event_name
= "POWERDOWN";
442 event_name
= "RESUME";
444 case QEVENT_VNC_CONNECTED
:
445 event_name
= "VNC_CONNECTED";
447 case QEVENT_VNC_INITIALIZED
:
448 event_name
= "VNC_INITIALIZED";
450 case QEVENT_VNC_DISCONNECTED
:
451 event_name
= "VNC_DISCONNECTED";
453 case QEVENT_BLOCK_IO_ERROR
:
454 event_name
= "BLOCK_IO_ERROR";
456 case QEVENT_RTC_CHANGE
:
457 event_name
= "RTC_CHANGE";
459 case QEVENT_WATCHDOG
:
460 event_name
= "WATCHDOG";
469 qdict_put(qmp
, "event", qstring_from_str(event_name
));
471 qobject_incref(data
);
472 qdict_put_obj(qmp
, "data", data
);
475 QLIST_FOREACH(mon
, &mon_list
, entry
) {
476 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
477 monitor_json_emitter(mon
, QOBJECT(qmp
));
483 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
486 /* Will setup QMP capabilities in the future */
487 if (monitor_ctrl_mode(mon
)) {
488 mon
->mc
->command_mode
= 1;
494 static int compare_cmd(const char *name
, const char *list
)
496 const char *p
, *pstart
;
504 p
= pstart
+ strlen(pstart
);
505 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
514 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
515 const char *prefix
, const char *name
)
517 const mon_cmd_t
*cmd
;
519 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
520 if (!name
|| !strcmp(name
, cmd
->name
))
521 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
522 cmd
->params
, cmd
->help
);
526 static void help_cmd(Monitor
*mon
, const char *name
)
528 if (name
&& !strcmp(name
, "info")) {
529 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
531 help_cmd_dump(mon
, mon_cmds
, "", name
);
532 if (name
&& !strcmp(name
, "log")) {
533 const CPULogItem
*item
;
534 monitor_printf(mon
, "Log items (comma separated):\n");
535 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
536 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
537 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
543 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
545 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
548 #ifdef CONFIG_SIMPLE_TRACE
549 static void do_change_trace_event_state(Monitor
*mon
, const QDict
*qdict
)
551 const char *tp_name
= qdict_get_str(qdict
, "name");
552 bool new_state
= qdict_get_bool(qdict
, "option");
553 int ret
= st_change_trace_event_state(tp_name
, new_state
);
556 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
560 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
562 const char *op
= qdict_get_try_str(qdict
, "op");
563 const char *arg
= qdict_get_try_str(qdict
, "arg");
566 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
567 } else if (!strcmp(op
, "on")) {
568 st_set_trace_file_enabled(true);
569 } else if (!strcmp(op
, "off")) {
570 st_set_trace_file_enabled(false);
571 } else if (!strcmp(op
, "flush")) {
572 st_flush_trace_buffer();
573 } else if (!strcmp(op
, "set")) {
575 st_set_trace_file(arg
);
578 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
579 help_cmd(mon
, "trace-file");
584 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
586 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
589 data
->user_print(data
->mon
, ret_data
);
591 monitor_resume(data
->mon
);
595 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
597 monitor_protocol_emitter(opaque
, ret_data
);
600 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
603 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
606 static void qmp_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
608 cmd
->mhandler
.info_async(mon
, qmp_monitor_complete
, mon
);
611 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
616 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
618 cb_data
->user_print
= cmd
->user_print
;
619 monitor_suspend(mon
);
620 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
621 user_monitor_complete
, cb_data
);
628 static void user_async_info_handler(Monitor
*mon
, const mon_cmd_t
*cmd
)
632 MonitorCompletionData
*cb_data
= qemu_malloc(sizeof(*cb_data
));
634 cb_data
->user_print
= cmd
->user_print
;
635 monitor_suspend(mon
);
636 ret
= cmd
->mhandler
.info_async(mon
, user_monitor_complete
, cb_data
);
643 static void do_info(Monitor
*mon
, const QDict
*qdict
)
645 const mon_cmd_t
*cmd
;
646 const char *item
= qdict_get_try_str(qdict
, "item");
652 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
653 if (compare_cmd(item
, cmd
->name
))
657 if (cmd
->name
== NULL
) {
661 if (handler_is_async(cmd
)) {
662 user_async_info_handler(mon
, cmd
);
663 } else if (handler_is_qobject(cmd
)) {
664 QObject
*info_data
= NULL
;
666 cmd
->mhandler
.info_new(mon
, &info_data
);
668 cmd
->user_print(mon
, info_data
);
669 qobject_decref(info_data
);
672 cmd
->mhandler
.info(mon
);
678 help_cmd(mon
, "info");
681 static void do_info_version_print(Monitor
*mon
, const QObject
*data
)
686 qdict
= qobject_to_qdict(data
);
687 qemu
= qdict_get_qdict(qdict
, "qemu");
689 monitor_printf(mon
, "%" PRId64
".%" PRId64
".%" PRId64
"%s\n",
690 qdict_get_int(qemu
, "major"),
691 qdict_get_int(qemu
, "minor"),
692 qdict_get_int(qemu
, "micro"),
693 qdict_get_str(qdict
, "package"));
696 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
698 const char *version
= QEMU_VERSION
;
699 int major
= 0, minor
= 0, micro
= 0;
702 major
= strtol(version
, &tmp
, 10);
704 minor
= strtol(tmp
, &tmp
, 10);
706 micro
= strtol(tmp
, &tmp
, 10);
708 *ret_data
= qobject_from_jsonf("{ 'qemu': { 'major': %d, 'minor': %d, \
709 'micro': %d }, 'package': %s }", major
, minor
, micro
, QEMU_PKGVERSION
);
712 static void do_info_name_print(Monitor
*mon
, const QObject
*data
)
716 qdict
= qobject_to_qdict(data
);
717 if (qdict_size(qdict
) == 0) {
721 monitor_printf(mon
, "%s\n", qdict_get_str(qdict
, "name"));
724 static void do_info_name(Monitor
*mon
, QObject
**ret_data
)
726 *ret_data
= qemu_name
? qobject_from_jsonf("{'name': %s }", qemu_name
) :
727 qobject_from_jsonf("{}");
730 static QObject
*get_cmd_dict(const char *name
)
734 /* Remove '|' from some commands */
735 p
= strchr(name
, '|');
742 return qobject_from_jsonf("{ 'name': %s }", p
);
745 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
748 const mon_cmd_t
*cmd
;
750 cmd_list
= qlist_new();
752 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
753 qlist_append_obj(cmd_list
, get_cmd_dict(cmd
->name
));
756 for (cmd
= qmp_query_cmds
; cmd
->name
!= NULL
; cmd
++) {
758 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
759 qlist_append_obj(cmd_list
, get_cmd_dict(buf
));
762 *ret_data
= QOBJECT(cmd_list
);
765 static void do_info_uuid_print(Monitor
*mon
, const QObject
*data
)
767 monitor_printf(mon
, "%s\n", qdict_get_str(qobject_to_qdict(data
), "UUID"));
770 static void do_info_uuid(Monitor
*mon
, QObject
**ret_data
)
774 snprintf(uuid
, sizeof(uuid
), UUID_FMT
, qemu_uuid
[0], qemu_uuid
[1],
775 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
776 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
777 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
778 qemu_uuid
[14], qemu_uuid
[15]);
779 *ret_data
= qobject_from_jsonf("{ 'UUID': %s }", uuid
);
782 /* get the current CPU defined by the user */
783 static int mon_set_cpu(int cpu_index
)
787 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
788 if (env
->cpu_index
== cpu_index
) {
789 cur_mon
->mon_cpu
= env
;
796 static CPUState
*mon_get_cpu(void)
798 if (!cur_mon
->mon_cpu
) {
801 cpu_synchronize_state(cur_mon
->mon_cpu
);
802 return cur_mon
->mon_cpu
;
805 static void do_info_registers(Monitor
*mon
)
810 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
813 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
818 static void print_cpu_iter(QObject
*obj
, void *opaque
)
822 Monitor
*mon
= opaque
;
824 assert(qobject_type(obj
) == QTYPE_QDICT
);
825 cpu
= qobject_to_qdict(obj
);
827 if (qdict_get_bool(cpu
, "current")) {
831 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
833 #if defined(TARGET_I386)
834 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
835 (target_ulong
) qdict_get_int(cpu
, "pc"));
836 #elif defined(TARGET_PPC)
837 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
838 (target_long
) qdict_get_int(cpu
, "nip"));
839 #elif defined(TARGET_SPARC)
840 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
841 (target_long
) qdict_get_int(cpu
, "pc"));
842 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
843 (target_long
) qdict_get_int(cpu
, "npc"));
844 #elif defined(TARGET_MIPS)
845 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
846 (target_long
) qdict_get_int(cpu
, "PC"));
849 if (qdict_get_bool(cpu
, "halted")) {
850 monitor_printf(mon
, " (halted)");
853 monitor_printf(mon
, "\n");
856 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
860 assert(qobject_type(data
) == QTYPE_QLIST
);
861 cpu_list
= qobject_to_qlist(data
);
862 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
865 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
870 cpu_list
= qlist_new();
872 /* just to set the default cpu if not already done */
875 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
879 cpu_synchronize_state(env
);
881 obj
= qobject_from_jsonf("{ 'CPU': %d, 'current': %i, 'halted': %i }",
882 env
->cpu_index
, env
== mon
->mon_cpu
,
885 cpu
= qobject_to_qdict(obj
);
887 #if defined(TARGET_I386)
888 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
889 #elif defined(TARGET_PPC)
890 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
891 #elif defined(TARGET_SPARC)
892 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
893 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
894 #elif defined(TARGET_MIPS)
895 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
898 qlist_append(cpu_list
, cpu
);
901 *ret_data
= QOBJECT(cpu_list
);
904 static int do_cpu_set(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
906 int index
= qdict_get_int(qdict
, "index");
907 if (mon_set_cpu(index
) < 0) {
908 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "index",
915 static void do_info_jit(Monitor
*mon
)
917 dump_exec_info((FILE *)mon
, monitor_fprintf
);
920 static void do_info_history(Monitor
*mon
)
929 str
= readline_get_history(mon
->rs
, i
);
932 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
937 #if defined(TARGET_PPC)
938 /* XXX: not implemented in other targets */
939 static void do_info_cpu_stats(Monitor
*mon
)
944 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
948 #if defined(CONFIG_SIMPLE_TRACE)
949 static void do_info_trace(Monitor
*mon
)
951 st_print_trace((FILE *)mon
, &monitor_fprintf
);
954 static void do_info_trace_events(Monitor
*mon
)
956 st_print_trace_events((FILE *)mon
, &monitor_fprintf
);
961 * do_quit(): Quit QEMU execution
963 static int do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
965 monitor_suspend(mon
);
967 qemu_system_shutdown_request();
972 static int change_vnc_password(const char *password
)
974 if (vnc_display_password(NULL
, password
) < 0) {
975 qerror_report(QERR_SET_PASSWD_FAILED
);
982 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
985 change_vnc_password(password
);
986 monitor_read_command(mon
, 1);
989 static int do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
991 if (strcmp(target
, "passwd") == 0 ||
992 strcmp(target
, "password") == 0) {
995 strncpy(password
, arg
, sizeof(password
));
996 password
[sizeof(password
) - 1] = '\0';
997 return change_vnc_password(password
);
999 return monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
1002 if (vnc_display_open(NULL
, target
) < 0) {
1003 qerror_report(QERR_VNC_SERVER_FAILED
, target
);
1012 * do_change(): Change a removable medium, or VNC configuration
1014 static int do_change(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1016 const char *device
= qdict_get_str(qdict
, "device");
1017 const char *target
= qdict_get_str(qdict
, "target");
1018 const char *arg
= qdict_get_try_str(qdict
, "arg");
1021 if (strcmp(device
, "vnc") == 0) {
1022 ret
= do_change_vnc(mon
, target
, arg
);
1024 ret
= do_change_block(mon
, device
, target
, arg
);
1030 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1032 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1036 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1038 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1041 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1044 const char *items
= qdict_get_str(qdict
, "items");
1046 if (!strcmp(items
, "none")) {
1049 mask
= cpu_str_to_log_mask(items
);
1051 help_cmd(mon
, "log");
1058 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1060 const char *option
= qdict_get_try_str(qdict
, "option");
1061 if (!option
|| !strcmp(option
, "on")) {
1063 } else if (!strcmp(option
, "off")) {
1066 monitor_printf(mon
, "unexpected option %s\n", option
);
1071 * do_stop(): Stop VM execution
1073 static int do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1075 vm_stop(EXCP_INTERRUPT
);
1079 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
1081 struct bdrv_iterate_context
{
1087 * do_cont(): Resume emulation.
1089 static int do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1091 struct bdrv_iterate_context context
= { mon
, 0 };
1093 if (incoming_expected
) {
1094 qerror_report(QERR_MIGRATION_EXPECTED
);
1097 bdrv_iterate(encrypted_bdrv_it
, &context
);
1098 /* only resume the vm if all keys are set and valid */
1107 static void bdrv_key_cb(void *opaque
, int err
)
1109 Monitor
*mon
= opaque
;
1111 /* another key was set successfully, retry to continue */
1113 do_cont(mon
, NULL
, NULL
);
1116 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
1118 struct bdrv_iterate_context
*context
= opaque
;
1120 if (!context
->err
&& bdrv_key_required(bs
)) {
1121 context
->err
= -EBUSY
;
1122 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
1127 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1129 const char *device
= qdict_get_try_str(qdict
, "device");
1131 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1132 if (gdbserver_start(device
) < 0) {
1133 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1135 } else if (strcmp(device
, "none") == 0) {
1136 monitor_printf(mon
, "Disabled gdbserver\n");
1138 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1143 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1145 const char *action
= qdict_get_str(qdict
, "action");
1146 if (select_watchdog_action(action
) == -1) {
1147 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1151 static void monitor_printc(Monitor
*mon
, int c
)
1153 monitor_printf(mon
, "'");
1156 monitor_printf(mon
, "\\'");
1159 monitor_printf(mon
, "\\\\");
1162 monitor_printf(mon
, "\\n");
1165 monitor_printf(mon
, "\\r");
1168 if (c
>= 32 && c
<= 126) {
1169 monitor_printf(mon
, "%c", c
);
1171 monitor_printf(mon
, "\\x%02x", c
);
1175 monitor_printf(mon
, "'");
1178 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1179 target_phys_addr_t addr
, int is_physical
)
1182 int l
, line_size
, i
, max_digits
, len
;
1186 if (format
== 'i') {
1189 env
= mon_get_cpu();
1193 } else if (wsize
== 4) {
1196 /* as default we use the current CS size */
1199 #ifdef TARGET_X86_64
1200 if ((env
->efer
& MSR_EFER_LMA
) &&
1201 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1205 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1210 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1214 len
= wsize
* count
;
1223 max_digits
= (wsize
* 8 + 2) / 3;
1227 max_digits
= (wsize
* 8) / 4;
1231 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1240 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1242 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1247 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1249 env
= mon_get_cpu();
1250 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1251 monitor_printf(mon
, " Cannot access memory\n");
1260 v
= ldub_raw(buf
+ i
);
1263 v
= lduw_raw(buf
+ i
);
1266 v
= (uint32_t)ldl_raw(buf
+ i
);
1269 v
= ldq_raw(buf
+ i
);
1272 monitor_printf(mon
, " ");
1275 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1278 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1281 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1284 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1287 monitor_printc(mon
, v
);
1292 monitor_printf(mon
, "\n");
1298 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1300 int count
= qdict_get_int(qdict
, "count");
1301 int format
= qdict_get_int(qdict
, "format");
1302 int size
= qdict_get_int(qdict
, "size");
1303 target_long addr
= qdict_get_int(qdict
, "addr");
1305 memory_dump(mon
, count
, format
, size
, addr
, 0);
1308 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1310 int count
= qdict_get_int(qdict
, "count");
1311 int format
= qdict_get_int(qdict
, "format");
1312 int size
= qdict_get_int(qdict
, "size");
1313 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1315 memory_dump(mon
, count
, format
, size
, addr
, 1);
1318 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1320 int format
= qdict_get_int(qdict
, "format");
1321 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1323 #if TARGET_PHYS_ADDR_BITS == 32
1326 monitor_printf(mon
, "%#o", val
);
1329 monitor_printf(mon
, "%#x", val
);
1332 monitor_printf(mon
, "%u", val
);
1336 monitor_printf(mon
, "%d", val
);
1339 monitor_printc(mon
, val
);
1345 monitor_printf(mon
, "%#" PRIo64
, val
);
1348 monitor_printf(mon
, "%#" PRIx64
, val
);
1351 monitor_printf(mon
, "%" PRIu64
, val
);
1355 monitor_printf(mon
, "%" PRId64
, val
);
1358 monitor_printc(mon
, val
);
1362 monitor_printf(mon
, "\n");
1365 static int do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1368 uint32_t size
= qdict_get_int(qdict
, "size");
1369 const char *filename
= qdict_get_str(qdict
, "filename");
1370 target_long addr
= qdict_get_int(qdict
, "val");
1376 env
= mon_get_cpu();
1378 f
= fopen(filename
, "wb");
1380 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1387 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1388 if (fwrite(buf
, 1, l
, f
) != l
) {
1389 monitor_printf(mon
, "fwrite() error in do_memory_save\n");
1403 static int do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1409 uint32_t size
= qdict_get_int(qdict
, "size");
1410 const char *filename
= qdict_get_str(qdict
, "filename");
1411 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1414 f
= fopen(filename
, "wb");
1416 qerror_report(QERR_OPEN_FILE_FAILED
, filename
);
1423 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1424 if (fwrite(buf
, 1, l
, f
) != l
) {
1425 monitor_printf(mon
, "fwrite() error in do_physical_memory_save\n");
1440 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1445 uint32_t start
= qdict_get_int(qdict
, "start");
1446 uint32_t size
= qdict_get_int(qdict
, "size");
1449 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1450 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1451 /* BSD sum algorithm ('sum' Unix command) */
1452 sum
= (sum
>> 1) | (sum
<< 15);
1455 monitor_printf(mon
, "%05d\n", sum
);
1463 static const KeyDef key_defs
[] = {
1465 { 0x36, "shift_r" },
1470 { 0xe4, "altgr_r" },
1490 { 0x0e, "backspace" },
1503 { 0x1a, "bracket_left" },
1504 { 0x1b, "bracket_right" },
1516 { 0x27, "semicolon" },
1517 { 0x28, "apostrophe" },
1518 { 0x29, "grave_accent" },
1520 { 0x2b, "backslash" },
1532 { 0x37, "asterisk" },
1535 { 0x3a, "caps_lock" },
1546 { 0x45, "num_lock" },
1547 { 0x46, "scroll_lock" },
1549 { 0xb5, "kp_divide" },
1550 { 0x37, "kp_multiply" },
1551 { 0x4a, "kp_subtract" },
1553 { 0x9c, "kp_enter" },
1554 { 0x53, "kp_decimal" },
1587 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1602 { 0xfe, "compose" },
1607 static int get_keycode(const char *key
)
1613 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1614 if (!strcmp(key
, p
->name
))
1617 if (strstart(key
, "0x", NULL
)) {
1618 ret
= strtoul(key
, &endp
, 0);
1619 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1625 #define MAX_KEYCODES 16
1626 static uint8_t keycodes
[MAX_KEYCODES
];
1627 static int nb_pending_keycodes
;
1628 static QEMUTimer
*key_timer
;
1630 static void release_keys(void *opaque
)
1634 while (nb_pending_keycodes
> 0) {
1635 nb_pending_keycodes
--;
1636 keycode
= keycodes
[nb_pending_keycodes
];
1638 kbd_put_keycode(0xe0);
1639 kbd_put_keycode(keycode
| 0x80);
1643 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1645 char keyname_buf
[16];
1647 int keyname_len
, keycode
, i
;
1648 const char *string
= qdict_get_str(qdict
, "string");
1649 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1650 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1652 if (nb_pending_keycodes
> 0) {
1653 qemu_del_timer(key_timer
);
1660 separator
= strchr(string
, '-');
1661 keyname_len
= separator
? separator
- string
: strlen(string
);
1662 if (keyname_len
> 0) {
1663 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1664 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1665 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1668 if (i
== MAX_KEYCODES
) {
1669 monitor_printf(mon
, "too many keys\n");
1672 keyname_buf
[keyname_len
] = 0;
1673 keycode
= get_keycode(keyname_buf
);
1675 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1678 keycodes
[i
++] = keycode
;
1682 string
= separator
+ 1;
1684 nb_pending_keycodes
= i
;
1685 /* key down events */
1686 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1687 keycode
= keycodes
[i
];
1689 kbd_put_keycode(0xe0);
1690 kbd_put_keycode(keycode
& 0x7f);
1692 /* delayed key up events */
1693 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1694 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1697 static int mouse_button_state
;
1699 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1702 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1703 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1704 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1705 dx
= strtol(dx_str
, NULL
, 0);
1706 dy
= strtol(dy_str
, NULL
, 0);
1709 dz
= strtol(dz_str
, NULL
, 0);
1710 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1713 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1715 int button_state
= qdict_get_int(qdict
, "button_state");
1716 mouse_button_state
= button_state
;
1717 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1720 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1722 int size
= qdict_get_int(qdict
, "size");
1723 int addr
= qdict_get_int(qdict
, "addr");
1724 int has_index
= qdict_haskey(qdict
, "index");
1729 int index
= qdict_get_int(qdict
, "index");
1730 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1738 val
= cpu_inb(addr
);
1742 val
= cpu_inw(addr
);
1746 val
= cpu_inl(addr
);
1750 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1751 suffix
, addr
, size
* 2, val
);
1754 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1756 int size
= qdict_get_int(qdict
, "size");
1757 int addr
= qdict_get_int(qdict
, "addr");
1758 int val
= qdict_get_int(qdict
, "val");
1760 addr
&= IOPORTS_MASK
;
1765 cpu_outb(addr
, val
);
1768 cpu_outw(addr
, val
);
1771 cpu_outl(addr
, val
);
1776 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1779 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1781 res
= qemu_boot_set(bootdevice
);
1783 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1784 } else if (res
> 0) {
1785 monitor_printf(mon
, "setting boot device list failed\n");
1787 monitor_printf(mon
, "no function defined to set boot device list for "
1788 "this architecture\n");
1793 * do_system_reset(): Issue a machine reset
1795 static int do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1798 qemu_system_reset_request();
1803 * do_system_powerdown(): Issue a machine powerdown
1805 static int do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1808 qemu_system_powerdown_request();
1812 #if defined(TARGET_I386)
1813 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1815 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1818 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1819 pte
& PG_PSE_MASK
? 'P' : '-',
1820 pte
& PG_DIRTY_MASK
? 'D' : '-',
1821 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1822 pte
& PG_PCD_MASK
? 'C' : '-',
1823 pte
& PG_PWT_MASK
? 'T' : '-',
1824 pte
& PG_USER_MASK
? 'U' : '-',
1825 pte
& PG_RW_MASK
? 'W' : '-');
1828 static void tlb_info(Monitor
*mon
)
1832 uint32_t pgd
, pde
, pte
;
1834 env
= mon_get_cpu();
1836 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1837 monitor_printf(mon
, "PG disabled\n");
1840 pgd
= env
->cr
[3] & ~0xfff;
1841 for(l1
= 0; l1
< 1024; l1
++) {
1842 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1843 pde
= le32_to_cpu(pde
);
1844 if (pde
& PG_PRESENT_MASK
) {
1845 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1846 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1848 for(l2
= 0; l2
< 1024; l2
++) {
1849 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1850 (uint8_t *)&pte
, 4);
1851 pte
= le32_to_cpu(pte
);
1852 if (pte
& PG_PRESENT_MASK
) {
1853 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1863 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1864 uint32_t end
, int prot
)
1867 prot1
= *plast_prot
;
1868 if (prot
!= prot1
) {
1869 if (*pstart
!= -1) {
1870 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1871 *pstart
, end
, end
- *pstart
,
1872 prot1
& PG_USER_MASK
? 'u' : '-',
1874 prot1
& PG_RW_MASK
? 'w' : '-');
1884 static void mem_info(Monitor
*mon
)
1887 int l1
, l2
, prot
, last_prot
;
1888 uint32_t pgd
, pde
, pte
, start
, end
;
1890 env
= mon_get_cpu();
1892 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1893 monitor_printf(mon
, "PG disabled\n");
1896 pgd
= env
->cr
[3] & ~0xfff;
1899 for(l1
= 0; l1
< 1024; l1
++) {
1900 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1901 pde
= le32_to_cpu(pde
);
1903 if (pde
& PG_PRESENT_MASK
) {
1904 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1905 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1906 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1908 for(l2
= 0; l2
< 1024; l2
++) {
1909 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1910 (uint8_t *)&pte
, 4);
1911 pte
= le32_to_cpu(pte
);
1912 end
= (l1
<< 22) + (l2
<< 12);
1913 if (pte
& PG_PRESENT_MASK
) {
1914 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1918 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1923 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1929 #if defined(TARGET_SH4)
1931 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1933 monitor_printf(mon
, " tlb%i:\t"
1934 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1935 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1936 "dirty=%hhu writethrough=%hhu\n",
1938 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1939 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1943 static void tlb_info(Monitor
*mon
)
1945 CPUState
*env
= mon_get_cpu();
1948 monitor_printf (mon
, "ITLB:\n");
1949 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1950 print_tlb (mon
, i
, &env
->itlb
[i
]);
1951 monitor_printf (mon
, "UTLB:\n");
1952 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1953 print_tlb (mon
, i
, &env
->utlb
[i
]);
1958 static void do_info_kvm_print(Monitor
*mon
, const QObject
*data
)
1962 qdict
= qobject_to_qdict(data
);
1964 monitor_printf(mon
, "kvm support: ");
1965 if (qdict_get_bool(qdict
, "present")) {
1966 monitor_printf(mon
, "%s\n", qdict_get_bool(qdict
, "enabled") ?
1967 "enabled" : "disabled");
1969 monitor_printf(mon
, "not compiled\n");
1973 static void do_info_kvm(Monitor
*mon
, QObject
**ret_data
)
1976 *ret_data
= qobject_from_jsonf("{ 'enabled': %i, 'present': true }",
1979 *ret_data
= qobject_from_jsonf("{ 'enabled': false, 'present': false }");
1983 static void do_info_numa(Monitor
*mon
)
1988 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1989 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1990 monitor_printf(mon
, "node %d cpus:", i
);
1991 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1992 if (env
->numa_node
== i
) {
1993 monitor_printf(mon
, " %d", env
->cpu_index
);
1996 monitor_printf(mon
, "\n");
1997 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2002 #ifdef CONFIG_PROFILER
2007 static void do_info_profile(Monitor
*mon
)
2013 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2014 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2015 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2016 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2021 static void do_info_profile(Monitor
*mon
)
2023 monitor_printf(mon
, "Internal profiler not compiled\n");
2027 /* Capture support */
2028 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2030 static void do_info_capture(Monitor
*mon
)
2035 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2036 monitor_printf(mon
, "[%d]: ", i
);
2037 s
->ops
.info (s
->opaque
);
2042 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2045 int n
= qdict_get_int(qdict
, "n");
2048 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2050 s
->ops
.destroy (s
->opaque
);
2051 QLIST_REMOVE (s
, entries
);
2058 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2060 const char *path
= qdict_get_str(qdict
, "path");
2061 int has_freq
= qdict_haskey(qdict
, "freq");
2062 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2063 int has_bits
= qdict_haskey(qdict
, "bits");
2064 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2065 int has_channels
= qdict_haskey(qdict
, "nchannels");
2066 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2069 s
= qemu_mallocz (sizeof (*s
));
2071 freq
= has_freq
? freq
: 44100;
2072 bits
= has_bits
? bits
: 16;
2073 nchannels
= has_channels
? nchannels
: 2;
2075 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2076 monitor_printf(mon
, "Faied to add wave capture\n");
2079 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2083 #if defined(TARGET_I386)
2084 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
2087 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2089 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
2090 if (env
->cpu_index
== cpu_index
) {
2091 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
2097 static void do_info_status_print(Monitor
*mon
, const QObject
*data
)
2101 qdict
= qobject_to_qdict(data
);
2103 monitor_printf(mon
, "VM status: ");
2104 if (qdict_get_bool(qdict
, "running")) {
2105 monitor_printf(mon
, "running");
2106 if (qdict_get_bool(qdict
, "singlestep")) {
2107 monitor_printf(mon
, " (single step mode)");
2110 monitor_printf(mon
, "paused");
2113 monitor_printf(mon
, "\n");
2116 static void do_info_status(Monitor
*mon
, QObject
**ret_data
)
2118 *ret_data
= qobject_from_jsonf("{ 'running': %i, 'singlestep': %i }",
2119 vm_running
, singlestep
);
2122 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2124 qemu_acl
*acl
= qemu_acl_find(name
);
2127 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2132 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2134 const char *aclname
= qdict_get_str(qdict
, "aclname");
2135 qemu_acl
*acl
= find_acl(mon
, aclname
);
2136 qemu_acl_entry
*entry
;
2140 monitor_printf(mon
, "policy: %s\n",
2141 acl
->defaultDeny
? "deny" : "allow");
2142 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2144 monitor_printf(mon
, "%d: %s %s\n", i
,
2145 entry
->deny
? "deny" : "allow", entry
->match
);
2150 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2152 const char *aclname
= qdict_get_str(qdict
, "aclname");
2153 qemu_acl
*acl
= find_acl(mon
, aclname
);
2156 qemu_acl_reset(acl
);
2157 monitor_printf(mon
, "acl: removed all rules\n");
2161 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2163 const char *aclname
= qdict_get_str(qdict
, "aclname");
2164 const char *policy
= qdict_get_str(qdict
, "policy");
2165 qemu_acl
*acl
= find_acl(mon
, aclname
);
2168 if (strcmp(policy
, "allow") == 0) {
2169 acl
->defaultDeny
= 0;
2170 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2171 } else if (strcmp(policy
, "deny") == 0) {
2172 acl
->defaultDeny
= 1;
2173 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2175 monitor_printf(mon
, "acl: unknown policy '%s', "
2176 "expected 'deny' or 'allow'\n", policy
);
2181 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2183 const char *aclname
= qdict_get_str(qdict
, "aclname");
2184 const char *match
= qdict_get_str(qdict
, "match");
2185 const char *policy
= qdict_get_str(qdict
, "policy");
2186 int has_index
= qdict_haskey(qdict
, "index");
2187 int index
= qdict_get_try_int(qdict
, "index", -1);
2188 qemu_acl
*acl
= find_acl(mon
, aclname
);
2192 if (strcmp(policy
, "allow") == 0) {
2194 } else if (strcmp(policy
, "deny") == 0) {
2197 monitor_printf(mon
, "acl: unknown policy '%s', "
2198 "expected 'deny' or 'allow'\n", policy
);
2202 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2204 ret
= qemu_acl_append(acl
, deny
, match
);
2206 monitor_printf(mon
, "acl: unable to add acl entry\n");
2208 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2212 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2214 const char *aclname
= qdict_get_str(qdict
, "aclname");
2215 const char *match
= qdict_get_str(qdict
, "match");
2216 qemu_acl
*acl
= find_acl(mon
, aclname
);
2220 ret
= qemu_acl_remove(acl
, match
);
2222 monitor_printf(mon
, "acl: no matching acl entry\n");
2224 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2228 #if defined(TARGET_I386)
2229 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2232 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2233 int bank
= qdict_get_int(qdict
, "bank");
2234 uint64_t status
= qdict_get_int(qdict
, "status");
2235 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2236 uint64_t addr
= qdict_get_int(qdict
, "addr");
2237 uint64_t misc
= qdict_get_int(qdict
, "misc");
2239 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
2240 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
2241 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
2247 static int do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2249 const char *fdname
= qdict_get_str(qdict
, "fdname");
2253 fd
= qemu_chr_get_msgfd(mon
->chr
);
2255 qerror_report(QERR_FD_NOT_SUPPLIED
);
2259 if (qemu_isdigit(fdname
[0])) {
2260 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "fdname",
2261 "a name not starting with a digit");
2265 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2266 if (strcmp(monfd
->name
, fdname
) != 0) {
2275 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
2276 monfd
->name
= qemu_strdup(fdname
);
2279 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
2283 static int do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
2285 const char *fdname
= qdict_get_str(qdict
, "fdname");
2288 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2289 if (strcmp(monfd
->name
, fdname
) != 0) {
2293 QLIST_REMOVE(monfd
, next
);
2295 qemu_free(monfd
->name
);
2300 qerror_report(QERR_FD_NOT_FOUND
, fdname
);
2304 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2306 int saved_vm_running
= vm_running
;
2307 const char *name
= qdict_get_str(qdict
, "name");
2311 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2316 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2320 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2323 if (strcmp(monfd
->name
, fdname
) != 0) {
2329 /* caller takes ownership of fd */
2330 QLIST_REMOVE(monfd
, next
);
2331 qemu_free(monfd
->name
);
2340 static const mon_cmd_t mon_cmds
[] = {
2341 #include "hmp-commands.h"
2345 /* Please update hmp-commands.hx when adding or changing commands */
2346 static const mon_cmd_t info_cmds
[] = {
2351 .help
= "show the version of QEMU",
2352 .user_print
= do_info_version_print
,
2353 .mhandler
.info_new
= do_info_version
,
2359 .help
= "show the network state",
2360 .mhandler
.info
= do_info_network
,
2366 .help
= "show the character devices",
2367 .user_print
= qemu_chr_info_print
,
2368 .mhandler
.info_new
= qemu_chr_info
,
2374 .help
= "show the block devices",
2375 .user_print
= bdrv_info_print
,
2376 .mhandler
.info_new
= bdrv_info
,
2379 .name
= "blockstats",
2382 .help
= "show block device statistics",
2383 .user_print
= bdrv_stats_print
,
2384 .mhandler
.info_new
= bdrv_info_stats
,
2387 .name
= "registers",
2390 .help
= "show the cpu registers",
2391 .mhandler
.info
= do_info_registers
,
2397 .help
= "show infos for each CPU",
2398 .user_print
= monitor_print_cpus
,
2399 .mhandler
.info_new
= do_info_cpus
,
2405 .help
= "show the command line history",
2406 .mhandler
.info
= do_info_history
,
2412 .help
= "show the interrupts statistics (if available)",
2413 .mhandler
.info
= irq_info
,
2419 .help
= "show i8259 (PIC) state",
2420 .mhandler
.info
= pic_info
,
2426 .help
= "show PCI info",
2427 .user_print
= do_pci_info_print
,
2428 .mhandler
.info_new
= do_pci_info
,
2430 #if defined(TARGET_I386) || defined(TARGET_SH4)
2435 .help
= "show virtual to physical memory mappings",
2436 .mhandler
.info
= tlb_info
,
2439 #if defined(TARGET_I386)
2444 .help
= "show the active virtual memory mappings",
2445 .mhandler
.info
= mem_info
,
2452 .help
= "show dynamic compiler info",
2453 .mhandler
.info
= do_info_jit
,
2459 .help
= "show KVM information",
2460 .user_print
= do_info_kvm_print
,
2461 .mhandler
.info_new
= do_info_kvm
,
2467 .help
= "show NUMA information",
2468 .mhandler
.info
= do_info_numa
,
2474 .help
= "show guest USB devices",
2475 .mhandler
.info
= usb_info
,
2481 .help
= "show host USB devices",
2482 .mhandler
.info
= usb_host_info
,
2488 .help
= "show profiling information",
2489 .mhandler
.info
= do_info_profile
,
2495 .help
= "show capture information",
2496 .mhandler
.info
= do_info_capture
,
2499 .name
= "snapshots",
2502 .help
= "show the currently saved VM snapshots",
2503 .mhandler
.info
= do_info_snapshots
,
2509 .help
= "show the current VM status (running|paused)",
2510 .user_print
= do_info_status_print
,
2511 .mhandler
.info_new
= do_info_status
,
2517 .help
= "show guest PCMCIA status",
2518 .mhandler
.info
= pcmcia_info
,
2524 .help
= "show which guest mouse is receiving events",
2525 .user_print
= do_info_mice_print
,
2526 .mhandler
.info_new
= do_info_mice
,
2532 .help
= "show the vnc server status",
2533 .user_print
= do_info_vnc_print
,
2534 .mhandler
.info_new
= do_info_vnc
,
2540 .help
= "show the current VM name",
2541 .user_print
= do_info_name_print
,
2542 .mhandler
.info_new
= do_info_name
,
2548 .help
= "show the current VM UUID",
2549 .user_print
= do_info_uuid_print
,
2550 .mhandler
.info_new
= do_info_uuid
,
2552 #if defined(TARGET_PPC)
2557 .help
= "show CPU statistics",
2558 .mhandler
.info
= do_info_cpu_stats
,
2561 #if defined(CONFIG_SLIRP)
2566 .help
= "show user network stack connection states",
2567 .mhandler
.info
= do_info_usernet
,
2574 .help
= "show migration status",
2575 .user_print
= do_info_migrate_print
,
2576 .mhandler
.info_new
= do_info_migrate
,
2582 .help
= "show balloon information",
2583 .user_print
= monitor_print_balloon
,
2584 .mhandler
.info_async
= do_info_balloon
,
2585 .flags
= MONITOR_CMD_ASYNC
,
2591 .help
= "show device tree",
2592 .mhandler
.info
= do_info_qtree
,
2598 .help
= "show qdev device model list",
2599 .mhandler
.info
= do_info_qdm
,
2605 .help
= "show roms",
2606 .mhandler
.info
= do_info_roms
,
2608 #if defined(CONFIG_SIMPLE_TRACE)
2613 .help
= "show current contents of trace buffer",
2614 .mhandler
.info
= do_info_trace
,
2617 .name
= "trace-events",
2620 .help
= "show available trace-events & their state",
2621 .mhandler
.info
= do_info_trace_events
,
2629 static const mon_cmd_t qmp_cmds
[] = {
2630 #include "qmp-commands.h"
2634 static const mon_cmd_t qmp_query_cmds
[] = {
2639 .help
= "show the version of QEMU",
2640 .user_print
= do_info_version_print
,
2641 .mhandler
.info_new
= do_info_version
,
2647 .help
= "list QMP available commands",
2648 .user_print
= monitor_user_noop
,
2649 .mhandler
.info_new
= do_info_commands
,
2655 .help
= "show the character devices",
2656 .user_print
= qemu_chr_info_print
,
2657 .mhandler
.info_new
= qemu_chr_info
,
2663 .help
= "show the block devices",
2664 .user_print
= bdrv_info_print
,
2665 .mhandler
.info_new
= bdrv_info
,
2668 .name
= "blockstats",
2671 .help
= "show block device statistics",
2672 .user_print
= bdrv_stats_print
,
2673 .mhandler
.info_new
= bdrv_info_stats
,
2679 .help
= "show infos for each CPU",
2680 .user_print
= monitor_print_cpus
,
2681 .mhandler
.info_new
= do_info_cpus
,
2687 .help
= "show PCI info",
2688 .user_print
= do_pci_info_print
,
2689 .mhandler
.info_new
= do_pci_info
,
2695 .help
= "show KVM information",
2696 .user_print
= do_info_kvm_print
,
2697 .mhandler
.info_new
= do_info_kvm
,
2703 .help
= "show the current VM status (running|paused)",
2704 .user_print
= do_info_status_print
,
2705 .mhandler
.info_new
= do_info_status
,
2711 .help
= "show which guest mouse is receiving events",
2712 .user_print
= do_info_mice_print
,
2713 .mhandler
.info_new
= do_info_mice
,
2719 .help
= "show the vnc server status",
2720 .user_print
= do_info_vnc_print
,
2721 .mhandler
.info_new
= do_info_vnc
,
2727 .help
= "show the current VM name",
2728 .user_print
= do_info_name_print
,
2729 .mhandler
.info_new
= do_info_name
,
2735 .help
= "show the current VM UUID",
2736 .user_print
= do_info_uuid_print
,
2737 .mhandler
.info_new
= do_info_uuid
,
2743 .help
= "show migration status",
2744 .user_print
= do_info_migrate_print
,
2745 .mhandler
.info_new
= do_info_migrate
,
2751 .help
= "show balloon information",
2752 .user_print
= monitor_print_balloon
,
2753 .mhandler
.info_async
= do_info_balloon
,
2754 .flags
= MONITOR_CMD_ASYNC
,
2759 /*******************************************************************/
2761 static const char *pch
;
2762 static jmp_buf expr_env
;
2767 typedef struct MonitorDef
{
2770 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2774 #if defined(TARGET_I386)
2775 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2777 CPUState
*env
= mon_get_cpu();
2778 return env
->eip
+ env
->segs
[R_CS
].base
;
2782 #if defined(TARGET_PPC)
2783 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2785 CPUState
*env
= mon_get_cpu();
2790 for (i
= 0; i
< 8; i
++)
2791 u
|= env
->crf
[i
] << (32 - (4 * i
));
2796 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2798 CPUState
*env
= mon_get_cpu();
2802 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2804 CPUState
*env
= mon_get_cpu();
2808 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2810 CPUState
*env
= mon_get_cpu();
2811 return cpu_ppc_load_decr(env
);
2814 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2816 CPUState
*env
= mon_get_cpu();
2817 return cpu_ppc_load_tbu(env
);
2820 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2822 CPUState
*env
= mon_get_cpu();
2823 return cpu_ppc_load_tbl(env
);
2827 #if defined(TARGET_SPARC)
2828 #ifndef TARGET_SPARC64
2829 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2831 CPUState
*env
= mon_get_cpu();
2833 return cpu_get_psr(env
);
2837 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2839 CPUState
*env
= mon_get_cpu();
2840 return env
->regwptr
[val
];
2844 static const MonitorDef monitor_defs
[] = {
2847 #define SEG(name, seg) \
2848 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2849 { name ".base", offsetof(CPUState, segs[seg].base) },\
2850 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2852 { "eax", offsetof(CPUState
, regs
[0]) },
2853 { "ecx", offsetof(CPUState
, regs
[1]) },
2854 { "edx", offsetof(CPUState
, regs
[2]) },
2855 { "ebx", offsetof(CPUState
, regs
[3]) },
2856 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2857 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2858 { "esi", offsetof(CPUState
, regs
[6]) },
2859 { "edi", offsetof(CPUState
, regs
[7]) },
2860 #ifdef TARGET_X86_64
2861 { "r8", offsetof(CPUState
, regs
[8]) },
2862 { "r9", offsetof(CPUState
, regs
[9]) },
2863 { "r10", offsetof(CPUState
, regs
[10]) },
2864 { "r11", offsetof(CPUState
, regs
[11]) },
2865 { "r12", offsetof(CPUState
, regs
[12]) },
2866 { "r13", offsetof(CPUState
, regs
[13]) },
2867 { "r14", offsetof(CPUState
, regs
[14]) },
2868 { "r15", offsetof(CPUState
, regs
[15]) },
2870 { "eflags", offsetof(CPUState
, eflags
) },
2871 { "eip", offsetof(CPUState
, eip
) },
2878 { "pc", 0, monitor_get_pc
, },
2879 #elif defined(TARGET_PPC)
2880 /* General purpose registers */
2881 { "r0", offsetof(CPUState
, gpr
[0]) },
2882 { "r1", offsetof(CPUState
, gpr
[1]) },
2883 { "r2", offsetof(CPUState
, gpr
[2]) },
2884 { "r3", offsetof(CPUState
, gpr
[3]) },
2885 { "r4", offsetof(CPUState
, gpr
[4]) },
2886 { "r5", offsetof(CPUState
, gpr
[5]) },
2887 { "r6", offsetof(CPUState
, gpr
[6]) },
2888 { "r7", offsetof(CPUState
, gpr
[7]) },
2889 { "r8", offsetof(CPUState
, gpr
[8]) },
2890 { "r9", offsetof(CPUState
, gpr
[9]) },
2891 { "r10", offsetof(CPUState
, gpr
[10]) },
2892 { "r11", offsetof(CPUState
, gpr
[11]) },
2893 { "r12", offsetof(CPUState
, gpr
[12]) },
2894 { "r13", offsetof(CPUState
, gpr
[13]) },
2895 { "r14", offsetof(CPUState
, gpr
[14]) },
2896 { "r15", offsetof(CPUState
, gpr
[15]) },
2897 { "r16", offsetof(CPUState
, gpr
[16]) },
2898 { "r17", offsetof(CPUState
, gpr
[17]) },
2899 { "r18", offsetof(CPUState
, gpr
[18]) },
2900 { "r19", offsetof(CPUState
, gpr
[19]) },
2901 { "r20", offsetof(CPUState
, gpr
[20]) },
2902 { "r21", offsetof(CPUState
, gpr
[21]) },
2903 { "r22", offsetof(CPUState
, gpr
[22]) },
2904 { "r23", offsetof(CPUState
, gpr
[23]) },
2905 { "r24", offsetof(CPUState
, gpr
[24]) },
2906 { "r25", offsetof(CPUState
, gpr
[25]) },
2907 { "r26", offsetof(CPUState
, gpr
[26]) },
2908 { "r27", offsetof(CPUState
, gpr
[27]) },
2909 { "r28", offsetof(CPUState
, gpr
[28]) },
2910 { "r29", offsetof(CPUState
, gpr
[29]) },
2911 { "r30", offsetof(CPUState
, gpr
[30]) },
2912 { "r31", offsetof(CPUState
, gpr
[31]) },
2913 /* Floating point registers */
2914 { "f0", offsetof(CPUState
, fpr
[0]) },
2915 { "f1", offsetof(CPUState
, fpr
[1]) },
2916 { "f2", offsetof(CPUState
, fpr
[2]) },
2917 { "f3", offsetof(CPUState
, fpr
[3]) },
2918 { "f4", offsetof(CPUState
, fpr
[4]) },
2919 { "f5", offsetof(CPUState
, fpr
[5]) },
2920 { "f6", offsetof(CPUState
, fpr
[6]) },
2921 { "f7", offsetof(CPUState
, fpr
[7]) },
2922 { "f8", offsetof(CPUState
, fpr
[8]) },
2923 { "f9", offsetof(CPUState
, fpr
[9]) },
2924 { "f10", offsetof(CPUState
, fpr
[10]) },
2925 { "f11", offsetof(CPUState
, fpr
[11]) },
2926 { "f12", offsetof(CPUState
, fpr
[12]) },
2927 { "f13", offsetof(CPUState
, fpr
[13]) },
2928 { "f14", offsetof(CPUState
, fpr
[14]) },
2929 { "f15", offsetof(CPUState
, fpr
[15]) },
2930 { "f16", offsetof(CPUState
, fpr
[16]) },
2931 { "f17", offsetof(CPUState
, fpr
[17]) },
2932 { "f18", offsetof(CPUState
, fpr
[18]) },
2933 { "f19", offsetof(CPUState
, fpr
[19]) },
2934 { "f20", offsetof(CPUState
, fpr
[20]) },
2935 { "f21", offsetof(CPUState
, fpr
[21]) },
2936 { "f22", offsetof(CPUState
, fpr
[22]) },
2937 { "f23", offsetof(CPUState
, fpr
[23]) },
2938 { "f24", offsetof(CPUState
, fpr
[24]) },
2939 { "f25", offsetof(CPUState
, fpr
[25]) },
2940 { "f26", offsetof(CPUState
, fpr
[26]) },
2941 { "f27", offsetof(CPUState
, fpr
[27]) },
2942 { "f28", offsetof(CPUState
, fpr
[28]) },
2943 { "f29", offsetof(CPUState
, fpr
[29]) },
2944 { "f30", offsetof(CPUState
, fpr
[30]) },
2945 { "f31", offsetof(CPUState
, fpr
[31]) },
2946 { "fpscr", offsetof(CPUState
, fpscr
) },
2947 /* Next instruction pointer */
2948 { "nip|pc", offsetof(CPUState
, nip
) },
2949 { "lr", offsetof(CPUState
, lr
) },
2950 { "ctr", offsetof(CPUState
, ctr
) },
2951 { "decr", 0, &monitor_get_decr
, },
2952 { "ccr", 0, &monitor_get_ccr
, },
2953 /* Machine state register */
2954 { "msr", 0, &monitor_get_msr
, },
2955 { "xer", 0, &monitor_get_xer
, },
2956 { "tbu", 0, &monitor_get_tbu
, },
2957 { "tbl", 0, &monitor_get_tbl
, },
2958 #if defined(TARGET_PPC64)
2959 /* Address space register */
2960 { "asr", offsetof(CPUState
, asr
) },
2962 /* Segment registers */
2963 { "sdr1", offsetof(CPUState
, sdr1
) },
2964 { "sr0", offsetof(CPUState
, sr
[0]) },
2965 { "sr1", offsetof(CPUState
, sr
[1]) },
2966 { "sr2", offsetof(CPUState
, sr
[2]) },
2967 { "sr3", offsetof(CPUState
, sr
[3]) },
2968 { "sr4", offsetof(CPUState
, sr
[4]) },
2969 { "sr5", offsetof(CPUState
, sr
[5]) },
2970 { "sr6", offsetof(CPUState
, sr
[6]) },
2971 { "sr7", offsetof(CPUState
, sr
[7]) },
2972 { "sr8", offsetof(CPUState
, sr
[8]) },
2973 { "sr9", offsetof(CPUState
, sr
[9]) },
2974 { "sr10", offsetof(CPUState
, sr
[10]) },
2975 { "sr11", offsetof(CPUState
, sr
[11]) },
2976 { "sr12", offsetof(CPUState
, sr
[12]) },
2977 { "sr13", offsetof(CPUState
, sr
[13]) },
2978 { "sr14", offsetof(CPUState
, sr
[14]) },
2979 { "sr15", offsetof(CPUState
, sr
[15]) },
2980 /* Too lazy to put BATs and SPRs ... */
2981 #elif defined(TARGET_SPARC)
2982 { "g0", offsetof(CPUState
, gregs
[0]) },
2983 { "g1", offsetof(CPUState
, gregs
[1]) },
2984 { "g2", offsetof(CPUState
, gregs
[2]) },
2985 { "g3", offsetof(CPUState
, gregs
[3]) },
2986 { "g4", offsetof(CPUState
, gregs
[4]) },
2987 { "g5", offsetof(CPUState
, gregs
[5]) },
2988 { "g6", offsetof(CPUState
, gregs
[6]) },
2989 { "g7", offsetof(CPUState
, gregs
[7]) },
2990 { "o0", 0, monitor_get_reg
},
2991 { "o1", 1, monitor_get_reg
},
2992 { "o2", 2, monitor_get_reg
},
2993 { "o3", 3, monitor_get_reg
},
2994 { "o4", 4, monitor_get_reg
},
2995 { "o5", 5, monitor_get_reg
},
2996 { "o6", 6, monitor_get_reg
},
2997 { "o7", 7, monitor_get_reg
},
2998 { "l0", 8, monitor_get_reg
},
2999 { "l1", 9, monitor_get_reg
},
3000 { "l2", 10, monitor_get_reg
},
3001 { "l3", 11, monitor_get_reg
},
3002 { "l4", 12, monitor_get_reg
},
3003 { "l5", 13, monitor_get_reg
},
3004 { "l6", 14, monitor_get_reg
},
3005 { "l7", 15, monitor_get_reg
},
3006 { "i0", 16, monitor_get_reg
},
3007 { "i1", 17, monitor_get_reg
},
3008 { "i2", 18, monitor_get_reg
},
3009 { "i3", 19, monitor_get_reg
},
3010 { "i4", 20, monitor_get_reg
},
3011 { "i5", 21, monitor_get_reg
},
3012 { "i6", 22, monitor_get_reg
},
3013 { "i7", 23, monitor_get_reg
},
3014 { "pc", offsetof(CPUState
, pc
) },
3015 { "npc", offsetof(CPUState
, npc
) },
3016 { "y", offsetof(CPUState
, y
) },
3017 #ifndef TARGET_SPARC64
3018 { "psr", 0, &monitor_get_psr
, },
3019 { "wim", offsetof(CPUState
, wim
) },
3021 { "tbr", offsetof(CPUState
, tbr
) },
3022 { "fsr", offsetof(CPUState
, fsr
) },
3023 { "f0", offsetof(CPUState
, fpr
[0]) },
3024 { "f1", offsetof(CPUState
, fpr
[1]) },
3025 { "f2", offsetof(CPUState
, fpr
[2]) },
3026 { "f3", offsetof(CPUState
, fpr
[3]) },
3027 { "f4", offsetof(CPUState
, fpr
[4]) },
3028 { "f5", offsetof(CPUState
, fpr
[5]) },
3029 { "f6", offsetof(CPUState
, fpr
[6]) },
3030 { "f7", offsetof(CPUState
, fpr
[7]) },
3031 { "f8", offsetof(CPUState
, fpr
[8]) },
3032 { "f9", offsetof(CPUState
, fpr
[9]) },
3033 { "f10", offsetof(CPUState
, fpr
[10]) },
3034 { "f11", offsetof(CPUState
, fpr
[11]) },
3035 { "f12", offsetof(CPUState
, fpr
[12]) },
3036 { "f13", offsetof(CPUState
, fpr
[13]) },
3037 { "f14", offsetof(CPUState
, fpr
[14]) },
3038 { "f15", offsetof(CPUState
, fpr
[15]) },
3039 { "f16", offsetof(CPUState
, fpr
[16]) },
3040 { "f17", offsetof(CPUState
, fpr
[17]) },
3041 { "f18", offsetof(CPUState
, fpr
[18]) },
3042 { "f19", offsetof(CPUState
, fpr
[19]) },
3043 { "f20", offsetof(CPUState
, fpr
[20]) },
3044 { "f21", offsetof(CPUState
, fpr
[21]) },
3045 { "f22", offsetof(CPUState
, fpr
[22]) },
3046 { "f23", offsetof(CPUState
, fpr
[23]) },
3047 { "f24", offsetof(CPUState
, fpr
[24]) },
3048 { "f25", offsetof(CPUState
, fpr
[25]) },
3049 { "f26", offsetof(CPUState
, fpr
[26]) },
3050 { "f27", offsetof(CPUState
, fpr
[27]) },
3051 { "f28", offsetof(CPUState
, fpr
[28]) },
3052 { "f29", offsetof(CPUState
, fpr
[29]) },
3053 { "f30", offsetof(CPUState
, fpr
[30]) },
3054 { "f31", offsetof(CPUState
, fpr
[31]) },
3055 #ifdef TARGET_SPARC64
3056 { "f32", offsetof(CPUState
, fpr
[32]) },
3057 { "f34", offsetof(CPUState
, fpr
[34]) },
3058 { "f36", offsetof(CPUState
, fpr
[36]) },
3059 { "f38", offsetof(CPUState
, fpr
[38]) },
3060 { "f40", offsetof(CPUState
, fpr
[40]) },
3061 { "f42", offsetof(CPUState
, fpr
[42]) },
3062 { "f44", offsetof(CPUState
, fpr
[44]) },
3063 { "f46", offsetof(CPUState
, fpr
[46]) },
3064 { "f48", offsetof(CPUState
, fpr
[48]) },
3065 { "f50", offsetof(CPUState
, fpr
[50]) },
3066 { "f52", offsetof(CPUState
, fpr
[52]) },
3067 { "f54", offsetof(CPUState
, fpr
[54]) },
3068 { "f56", offsetof(CPUState
, fpr
[56]) },
3069 { "f58", offsetof(CPUState
, fpr
[58]) },
3070 { "f60", offsetof(CPUState
, fpr
[60]) },
3071 { "f62", offsetof(CPUState
, fpr
[62]) },
3072 { "asi", offsetof(CPUState
, asi
) },
3073 { "pstate", offsetof(CPUState
, pstate
) },
3074 { "cansave", offsetof(CPUState
, cansave
) },
3075 { "canrestore", offsetof(CPUState
, canrestore
) },
3076 { "otherwin", offsetof(CPUState
, otherwin
) },
3077 { "wstate", offsetof(CPUState
, wstate
) },
3078 { "cleanwin", offsetof(CPUState
, cleanwin
) },
3079 { "fprs", offsetof(CPUState
, fprs
) },
3085 static void expr_error(Monitor
*mon
, const char *msg
)
3087 monitor_printf(mon
, "%s\n", msg
);
3088 longjmp(expr_env
, 1);
3091 /* return 0 if OK, -1 if not found */
3092 static int get_monitor_def(target_long
*pval
, const char *name
)
3094 const MonitorDef
*md
;
3097 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3098 if (compare_cmd(name
, md
->name
)) {
3099 if (md
->get_value
) {
3100 *pval
= md
->get_value(md
, md
->offset
);
3102 CPUState
*env
= mon_get_cpu();
3103 ptr
= (uint8_t *)env
+ md
->offset
;
3106 *pval
= *(int32_t *)ptr
;
3109 *pval
= *(target_long
*)ptr
;
3122 static void next(void)
3126 while (qemu_isspace(*pch
))
3131 static int64_t expr_sum(Monitor
*mon
);
3133 static int64_t expr_unary(Monitor
*mon
)
3142 n
= expr_unary(mon
);
3146 n
= -expr_unary(mon
);
3150 n
= ~expr_unary(mon
);
3156 expr_error(mon
, "')' expected");
3163 expr_error(mon
, "character constant expected");
3167 expr_error(mon
, "missing terminating \' character");
3177 while ((*pch
>= 'a' && *pch
<= 'z') ||
3178 (*pch
>= 'A' && *pch
<= 'Z') ||
3179 (*pch
>= '0' && *pch
<= '9') ||
3180 *pch
== '_' || *pch
== '.') {
3181 if ((q
- buf
) < sizeof(buf
) - 1)
3185 while (qemu_isspace(*pch
))
3188 ret
= get_monitor_def(®
, buf
);
3190 expr_error(mon
, "unknown register");
3195 expr_error(mon
, "unexpected end of expression");
3199 #if TARGET_PHYS_ADDR_BITS > 32
3200 n
= strtoull(pch
, &p
, 0);
3202 n
= strtoul(pch
, &p
, 0);
3205 expr_error(mon
, "invalid char in expression");
3208 while (qemu_isspace(*pch
))
3216 static int64_t expr_prod(Monitor
*mon
)
3221 val
= expr_unary(mon
);
3224 if (op
!= '*' && op
!= '/' && op
!= '%')
3227 val2
= expr_unary(mon
);
3236 expr_error(mon
, "division by zero");
3247 static int64_t expr_logic(Monitor
*mon
)
3252 val
= expr_prod(mon
);
3255 if (op
!= '&' && op
!= '|' && op
!= '^')
3258 val2
= expr_prod(mon
);
3275 static int64_t expr_sum(Monitor
*mon
)
3280 val
= expr_logic(mon
);
3283 if (op
!= '+' && op
!= '-')
3286 val2
= expr_logic(mon
);
3295 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3298 if (setjmp(expr_env
)) {
3302 while (qemu_isspace(*pch
))
3304 *pval
= expr_sum(mon
);
3309 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3311 const char *p
= *pp
;
3315 d
= strtod(p
, &tailp
);
3317 monitor_printf(mon
, "Number expected\n");
3320 if (d
!= d
|| d
- d
!= 0) {
3321 /* NaN or infinity */
3322 monitor_printf(mon
, "Bad number\n");
3330 static int get_str(char *buf
, int buf_size
, const char **pp
)
3338 while (qemu_isspace(*p
))
3348 while (*p
!= '\0' && *p
!= '\"') {
3364 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3367 if ((q
- buf
) < buf_size
- 1) {
3371 if ((q
- buf
) < buf_size
- 1) {
3378 qemu_printf("unterminated string\n");
3383 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3384 if ((q
- buf
) < buf_size
- 1) {
3396 * Store the command-name in cmdname, and return a pointer to
3397 * the remaining of the command string.
3399 static const char *get_command_name(const char *cmdline
,
3400 char *cmdname
, size_t nlen
)
3403 const char *p
, *pstart
;
3406 while (qemu_isspace(*p
))
3411 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3416 memcpy(cmdname
, pstart
, len
);
3417 cmdname
[len
] = '\0';
3422 * Read key of 'type' into 'key' and return the current
3425 static char *key_get_info(const char *type
, char **key
)
3433 p
= strchr(type
, ':');
3440 str
= qemu_malloc(len
+ 1);
3441 memcpy(str
, type
, len
);
3448 static int default_fmt_format
= 'x';
3449 static int default_fmt_size
= 4;
3453 static int is_valid_option(const char *c
, const char *typestr
)
3461 typestr
= strstr(typestr
, option
);
3462 return (typestr
!= NULL
);
3465 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3466 const char *cmdname
)
3468 const mon_cmd_t
*cmd
;
3470 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3471 if (compare_cmd(cmdname
, cmd
->name
)) {
3479 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3481 return search_dispatch_table(mon_cmds
, cmdname
);
3484 static const mon_cmd_t
*qmp_find_query_cmd(const char *info_item
)
3486 return search_dispatch_table(qmp_query_cmds
, info_item
);
3489 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3491 return search_dispatch_table(qmp_cmds
, cmdname
);
3494 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3495 const char *cmdline
,
3498 const char *p
, *typestr
;
3500 const mon_cmd_t
*cmd
;
3506 monitor_printf(mon
, "command='%s'\n", cmdline
);
3509 /* extract the command name */
3510 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3514 cmd
= monitor_find_command(cmdname
);
3516 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3520 /* parse the parameters */
3521 typestr
= cmd
->args_type
;
3523 typestr
= key_get_info(typestr
, &key
);
3535 while (qemu_isspace(*p
))
3537 if (*typestr
== '?') {
3540 /* no optional string: NULL argument */
3544 ret
= get_str(buf
, sizeof(buf
), &p
);
3548 monitor_printf(mon
, "%s: filename expected\n",
3552 monitor_printf(mon
, "%s: block device name expected\n",
3556 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3561 qdict_put(qdict
, key
, qstring_from_str(buf
));
3566 QemuOptsList
*opts_list
;
3569 opts_list
= qemu_find_opts(key
);
3570 if (!opts_list
|| opts_list
->desc
->name
) {
3573 while (qemu_isspace(*p
)) {
3578 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3581 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3585 qemu_opts_to_qdict(opts
, qdict
);
3586 qemu_opts_del(opts
);
3591 int count
, format
, size
;
3593 while (qemu_isspace(*p
))
3599 if (qemu_isdigit(*p
)) {
3601 while (qemu_isdigit(*p
)) {
3602 count
= count
* 10 + (*p
- '0');
3640 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3641 monitor_printf(mon
, "invalid char in format: '%c'\n",
3646 format
= default_fmt_format
;
3647 if (format
!= 'i') {
3648 /* for 'i', not specifying a size gives -1 as size */
3650 size
= default_fmt_size
;
3651 default_fmt_size
= size
;
3653 default_fmt_format
= format
;
3656 format
= default_fmt_format
;
3657 if (format
!= 'i') {
3658 size
= default_fmt_size
;
3663 qdict_put(qdict
, "count", qint_from_int(count
));
3664 qdict_put(qdict
, "format", qint_from_int(format
));
3665 qdict_put(qdict
, "size", qint_from_int(size
));
3674 while (qemu_isspace(*p
))
3676 if (*typestr
== '?' || *typestr
== '.') {
3677 if (*typestr
== '?') {
3685 while (qemu_isspace(*p
))
3694 if (get_expr(mon
, &val
, &p
))
3696 /* Check if 'i' is greater than 32-bit */
3697 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3698 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3699 monitor_printf(mon
, "integer is for 32-bit values\n");
3701 } else if (c
== 'M') {
3704 qdict_put(qdict
, key
, qint_from_int(val
));
3712 while (qemu_isspace(*p
)) {
3715 if (*typestr
== '?') {
3721 val
= strtosz(p
, &end
);
3723 monitor_printf(mon
, "invalid size\n");
3726 qdict_put(qdict
, key
, qint_from_int(val
));
3734 while (qemu_isspace(*p
))
3736 if (*typestr
== '?') {
3742 if (get_double(mon
, &val
, &p
) < 0) {
3745 if (p
[0] && p
[1] == 's') {
3748 val
/= 1e3
; p
+= 2; break;
3750 val
/= 1e6
; p
+= 2; break;
3752 val
/= 1e9
; p
+= 2; break;
3755 if (*p
&& !qemu_isspace(*p
)) {
3756 monitor_printf(mon
, "Unknown unit suffix\n");
3759 qdict_put(qdict
, key
, qfloat_from_double(val
));
3767 while (qemu_isspace(*p
)) {
3771 while (qemu_isgraph(*p
)) {
3774 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3776 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3779 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3782 qdict_put(qdict
, key
, qbool_from_int(val
));
3787 const char *tmp
= p
;
3794 while (qemu_isspace(*p
))
3799 if(!is_valid_option(p
, typestr
)) {
3801 monitor_printf(mon
, "%s: unsupported option -%c\n",
3813 qdict_put(qdict
, key
, qbool_from_int(1));
3820 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3826 /* check that all arguments were parsed */
3827 while (qemu_isspace(*p
))
3830 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3842 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
3844 /* report only the first error */
3846 mon
->error
= qerror
;
3848 MON_DEBUG("Additional error report at %s:%d\n",
3849 qerror
->file
, qerror
->linenr
);
3854 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
3856 if (monitor_ctrl_mode(mon
)) {
3857 if (ret
&& !monitor_has_error(mon
)) {
3859 * If it returns failure, it must have passed on error.
3861 * Action: Report an internal error to the client if in QMP.
3863 qerror_report(QERR_UNDEFINED_ERROR
);
3864 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3868 #ifdef CONFIG_DEBUG_MONITOR
3869 if (!ret
&& monitor_has_error(mon
)) {
3871 * If it returns success, it must not have passed an error.
3873 * Action: Report the passed error to the client.
3875 MON_DEBUG("command '%s' returned success but passed an error\n",
3879 if (mon_print_count_get(mon
) > 0 && strcmp(cmd
->name
, "info") != 0) {
3881 * Handlers should not call Monitor print functions.
3883 * Action: Ignore them in QMP.
3885 * (XXX: we don't check any 'info' or 'query' command here
3886 * because the user print function _is_ called by do_info(), hence
3887 * we will trigger this check. This problem will go away when we
3888 * make 'query' commands real and kill do_info())
3890 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3891 cmd
->name
, mon_print_count_get(mon
));
3895 assert(!monitor_has_error(mon
));
3896 QDECREF(mon
->error
);
3901 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
3904 const mon_cmd_t
*cmd
;
3906 qdict
= qdict_new();
3908 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3912 if (handler_is_async(cmd
)) {
3913 user_async_cmd_handler(mon
, cmd
, qdict
);
3914 } else if (handler_is_qobject(cmd
)) {
3915 QObject
*data
= NULL
;
3917 /* XXX: ignores the error code */
3918 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3919 assert(!monitor_has_error(mon
));
3921 cmd
->user_print(mon
, data
);
3922 qobject_decref(data
);
3925 cmd
->mhandler
.cmd(mon
, qdict
);
3932 static void cmd_completion(const char *name
, const char *list
)
3934 const char *p
, *pstart
;
3943 p
= pstart
+ strlen(pstart
);
3945 if (len
> sizeof(cmd
) - 2)
3946 len
= sizeof(cmd
) - 2;
3947 memcpy(cmd
, pstart
, len
);
3949 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3950 readline_add_completion(cur_mon
->rs
, cmd
);
3958 static void file_completion(const char *input
)
3963 char file
[1024], file_prefix
[1024];
3967 p
= strrchr(input
, '/');
3970 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3971 pstrcpy(path
, sizeof(path
), ".");
3973 input_path_len
= p
- input
+ 1;
3974 memcpy(path
, input
, input_path_len
);
3975 if (input_path_len
> sizeof(path
) - 1)
3976 input_path_len
= sizeof(path
) - 1;
3977 path
[input_path_len
] = '\0';
3978 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3980 #ifdef DEBUG_COMPLETION
3981 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3982 input
, path
, file_prefix
);
3984 ffs
= opendir(path
);
3993 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3997 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3998 memcpy(file
, input
, input_path_len
);
3999 if (input_path_len
< sizeof(file
))
4000 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4002 /* stat the file to find out if it's a directory.
4003 * In that case add a slash to speed up typing long paths
4006 if(S_ISDIR(sb
.st_mode
))
4007 pstrcat(file
, sizeof(file
), "/");
4008 readline_add_completion(cur_mon
->rs
, file
);
4014 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4016 const char *name
= bdrv_get_device_name(bs
);
4017 const char *input
= opaque
;
4019 if (input
[0] == '\0' ||
4020 !strncmp(name
, (char *)input
, strlen(input
))) {
4021 readline_add_completion(cur_mon
->rs
, name
);
4025 /* NOTE: this parser is an approximate form of the real command parser */
4026 static void parse_cmdline(const char *cmdline
,
4027 int *pnb_args
, char **args
)
4036 while (qemu_isspace(*p
))
4040 if (nb_args
>= MAX_ARGS
)
4042 ret
= get_str(buf
, sizeof(buf
), &p
);
4043 args
[nb_args
] = qemu_strdup(buf
);
4048 *pnb_args
= nb_args
;
4051 static const char *next_arg_type(const char *typestr
)
4053 const char *p
= strchr(typestr
, ':');
4054 return (p
!= NULL
? ++p
: typestr
);
4057 static void monitor_find_completion(const char *cmdline
)
4059 const char *cmdname
;
4060 char *args
[MAX_ARGS
];
4061 int nb_args
, i
, len
;
4062 const char *ptype
, *str
;
4063 const mon_cmd_t
*cmd
;
4066 parse_cmdline(cmdline
, &nb_args
, args
);
4067 #ifdef DEBUG_COMPLETION
4068 for(i
= 0; i
< nb_args
; i
++) {
4069 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4073 /* if the line ends with a space, it means we want to complete the
4075 len
= strlen(cmdline
);
4076 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4077 if (nb_args
>= MAX_ARGS
) {
4080 args
[nb_args
++] = qemu_strdup("");
4083 /* command completion */
4088 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4089 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4090 cmd_completion(cmdname
, cmd
->name
);
4093 /* find the command */
4094 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4095 if (compare_cmd(args
[0], cmd
->name
)) {
4103 ptype
= next_arg_type(cmd
->args_type
);
4104 for(i
= 0; i
< nb_args
- 2; i
++) {
4105 if (*ptype
!= '\0') {
4106 ptype
= next_arg_type(ptype
);
4107 while (*ptype
== '?')
4108 ptype
= next_arg_type(ptype
);
4111 str
= args
[nb_args
- 1];
4112 if (*ptype
== '-' && ptype
[1] != '\0') {
4113 ptype
= next_arg_type(ptype
);
4117 /* file completion */
4118 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4119 file_completion(str
);
4122 /* block device name completion */
4123 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4124 bdrv_iterate(block_completion_it
, (void *)str
);
4127 /* XXX: more generic ? */
4128 if (!strcmp(cmd
->name
, "info")) {
4129 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4130 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4131 cmd_completion(str
, cmd
->name
);
4133 } else if (!strcmp(cmd
->name
, "sendkey")) {
4134 char *sep
= strrchr(str
, '-');
4137 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4138 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4139 cmd_completion(str
, key
->name
);
4141 } else if (!strcmp(cmd
->name
, "help|?")) {
4142 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4143 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4144 cmd_completion(str
, cmd
->name
);
4154 for (i
= 0; i
< nb_args
; i
++) {
4159 static int monitor_can_read(void *opaque
)
4161 Monitor
*mon
= opaque
;
4163 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4166 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4168 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4169 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4173 * Argument validation rules:
4175 * 1. The argument must exist in cmd_args qdict
4176 * 2. The argument type must be the expected one
4178 * Special case: If the argument doesn't exist in cmd_args and
4179 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4180 * checking is skipped for it.
4182 static int check_client_args_type(const QDict
*client_args
,
4183 const QDict
*cmd_args
, int flags
)
4185 const QDictEntry
*ent
;
4187 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4190 const QObject
*client_arg
= qdict_entry_value(ent
);
4191 const char *client_arg_name
= qdict_entry_key(ent
);
4193 obj
= qdict_get(cmd_args
, client_arg_name
);
4195 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4196 /* handler accepts unknowns */
4199 /* client arg doesn't exist */
4200 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4204 arg_type
= qobject_to_qstring(obj
);
4205 assert(arg_type
!= NULL
);
4207 /* check if argument's type is correct */
4208 switch (qstring_get_str(arg_type
)[0]) {
4212 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4213 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4222 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4223 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4229 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4230 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4231 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4238 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4239 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4245 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4250 * These types are not supported by QMP and thus are not
4251 * handled here. Fall through.
4262 * - Check if the client has passed all mandatory args
4263 * - Set special flags for argument validation
4265 static int check_mandatory_args(const QDict
*cmd_args
,
4266 const QDict
*client_args
, int *flags
)
4268 const QDictEntry
*ent
;
4270 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4271 const char *cmd_arg_name
= qdict_entry_key(ent
);
4272 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4273 assert(type
!= NULL
);
4275 if (qstring_get_str(type
)[0] == 'O') {
4276 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4277 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4278 } else if (qstring_get_str(type
)[0] != '-' &&
4279 qstring_get_str(type
)[1] != '?' &&
4280 !qdict_haskey(client_args
, cmd_arg_name
)) {
4281 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4289 static QDict
*qdict_from_args_type(const char *args_type
)
4293 QString
*key
, *type
, *cur_qs
;
4295 assert(args_type
!= NULL
);
4297 qdict
= qdict_new();
4299 if (args_type
== NULL
|| args_type
[0] == '\0') {
4300 /* no args, empty qdict */
4304 key
= qstring_new();
4305 type
= qstring_new();
4310 switch (args_type
[i
]) {
4313 qdict_put(qdict
, qstring_get_str(key
), type
);
4315 if (args_type
[i
] == '\0') {
4318 type
= qstring_new(); /* qdict has ref */
4319 cur_qs
= key
= qstring_new();
4325 qstring_append_chr(cur_qs
, args_type
[i
]);
4335 * Client argument checking rules:
4337 * 1. Client must provide all mandatory arguments
4338 * 2. Each argument provided by the client must be expected
4339 * 3. Each argument provided by the client must have the type expected
4342 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4347 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4350 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4355 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4363 * Input object checking rules
4365 * 1. Input object must be a dict
4366 * 2. The "execute" key must exist
4367 * 3. The "execute" key must be a string
4368 * 4. If the "arguments" key exists, it must be a dict
4369 * 5. If the "id" key exists, it can be anything (ie. json-value)
4370 * 6. Any argument not listed above is considered invalid
4372 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4374 const QDictEntry
*ent
;
4375 int has_exec_key
= 0;
4378 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4379 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4383 input_dict
= qobject_to_qdict(input_obj
);
4385 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4386 const char *arg_name
= qdict_entry_key(ent
);
4387 const QObject
*arg_obj
= qdict_entry_value(ent
);
4389 if (!strcmp(arg_name
, "execute")) {
4390 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4391 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4396 } else if (!strcmp(arg_name
, "arguments")) {
4397 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4398 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4402 } else if (!strcmp(arg_name
, "id")) {
4403 /* FIXME: check duplicated IDs for async commands */
4405 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4410 if (!has_exec_key
) {
4411 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4418 static void qmp_call_query_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
)
4420 QObject
*ret_data
= NULL
;
4422 if (handler_is_async(cmd
)) {
4423 qmp_async_info_handler(mon
, cmd
);
4424 if (monitor_has_error(mon
)) {
4425 monitor_protocol_emitter(mon
, NULL
);
4428 cmd
->mhandler
.info_new(mon
, &ret_data
);
4430 monitor_protocol_emitter(mon
, ret_data
);
4431 qobject_decref(ret_data
);
4436 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4437 const QDict
*params
)
4440 QObject
*data
= NULL
;
4442 mon_print_count_init(mon
);
4444 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4445 handler_audit(mon
, cmd
, ret
);
4446 monitor_protocol_emitter(mon
, data
);
4447 qobject_decref(data
);
4450 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4454 QDict
*input
, *args
;
4455 const mon_cmd_t
*cmd
;
4456 Monitor
*mon
= cur_mon
;
4457 const char *cmd_name
, *query_cmd
;
4460 args
= input
= NULL
;
4462 obj
= json_parser_parse(tokens
, NULL
);
4464 // FIXME: should be triggered in json_parser_parse()
4465 qerror_report(QERR_JSON_PARSING
);
4469 input
= qmp_check_input_obj(obj
);
4471 qobject_decref(obj
);
4475 mon
->mc
->id
= qdict_get(input
, "id");
4476 qobject_incref(mon
->mc
->id
);
4478 cmd_name
= qdict_get_str(input
, "execute");
4479 if (invalid_qmp_mode(mon
, cmd_name
)) {
4480 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4484 if (strstart(cmd_name
, "query-", &query_cmd
)) {
4485 cmd
= qmp_find_query_cmd(query_cmd
);
4487 cmd
= qmp_find_cmd(cmd_name
);
4491 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4495 obj
= qdict_get(input
, "arguments");
4499 args
= qobject_to_qdict(obj
);
4503 err
= qmp_check_client_args(cmd
, args
);
4509 qmp_call_query_cmd(mon
, cmd
);
4510 } else if (handler_is_async(cmd
)) {
4511 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4513 /* emit the error response */
4517 qmp_call_cmd(mon
, cmd
, args
);
4523 monitor_protocol_emitter(mon
, NULL
);
4530 * monitor_control_read(): Read and handle QMP input
4532 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4534 Monitor
*old_mon
= cur_mon
;
4538 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4543 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4545 Monitor
*old_mon
= cur_mon
;
4551 for (i
= 0; i
< size
; i
++)
4552 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4554 if (size
== 0 || buf
[size
- 1] != 0)
4555 monitor_printf(cur_mon
, "corrupted command\n");
4557 handle_user_command(cur_mon
, (char *)buf
);
4563 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4565 monitor_suspend(mon
);
4566 handle_user_command(mon
, cmdline
);
4567 monitor_resume(mon
);
4570 int monitor_suspend(Monitor
*mon
)
4578 void monitor_resume(Monitor
*mon
)
4582 if (--mon
->suspend_cnt
== 0)
4583 readline_show_prompt(mon
->rs
);
4586 static QObject
*get_qmp_greeting(void)
4590 do_info_version(NULL
, &ver
);
4591 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4595 * monitor_control_event(): Print QMP gretting
4597 static void monitor_control_event(void *opaque
, int event
)
4600 Monitor
*mon
= opaque
;
4603 case CHR_EVENT_OPENED
:
4604 mon
->mc
->command_mode
= 0;
4605 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4606 data
= get_qmp_greeting();
4607 monitor_json_emitter(mon
, data
);
4608 qobject_decref(data
);
4610 case CHR_EVENT_CLOSED
:
4611 json_message_parser_destroy(&mon
->mc
->parser
);
4616 static void monitor_event(void *opaque
, int event
)
4618 Monitor
*mon
= opaque
;
4621 case CHR_EVENT_MUX_IN
:
4623 if (mon
->reset_seen
) {
4624 readline_restart(mon
->rs
);
4625 monitor_resume(mon
);
4628 mon
->suspend_cnt
= 0;
4632 case CHR_EVENT_MUX_OUT
:
4633 if (mon
->reset_seen
) {
4634 if (mon
->suspend_cnt
== 0) {
4635 monitor_printf(mon
, "\n");
4638 monitor_suspend(mon
);
4645 case CHR_EVENT_OPENED
:
4646 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4647 "information\n", QEMU_VERSION
);
4648 if (!mon
->mux_out
) {
4649 readline_show_prompt(mon
->rs
);
4651 mon
->reset_seen
= 1;
4665 void monitor_init(CharDriverState
*chr
, int flags
)
4667 static int is_first_init
= 1;
4670 if (is_first_init
) {
4671 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
4675 mon
= qemu_mallocz(sizeof(*mon
));
4679 if (flags
& MONITOR_USE_READLINE
) {
4680 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4681 monitor_read_command(mon
, 0);
4684 if (monitor_ctrl_mode(mon
)) {
4685 mon
->mc
= qemu_mallocz(sizeof(MonitorControl
));
4686 /* Control mode requires special handlers */
4687 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4688 monitor_control_event
, mon
);
4690 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4691 monitor_event
, mon
);
4694 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4695 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
4699 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
4701 BlockDriverState
*bs
= opaque
;
4704 if (bdrv_set_key(bs
, password
) != 0) {
4705 monitor_printf(mon
, "invalid password\n");
4708 if (mon
->password_completion_cb
)
4709 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4711 monitor_read_command(mon
, 1);
4714 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4715 BlockDriverCompletionFunc
*completion_cb
,
4720 if (!bdrv_key_required(bs
)) {
4722 completion_cb(opaque
, 0);
4726 if (monitor_ctrl_mode(mon
)) {
4727 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
));
4731 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4732 bdrv_get_encrypted_filename(bs
));
4734 mon
->password_completion_cb
= completion_cb
;
4735 mon
->password_opaque
= opaque
;
4737 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4739 if (err
&& completion_cb
)
4740 completion_cb(opaque
, err
);