4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
43 #include "audio/audio.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
61 #include "trace/control.h"
62 #ifdef CONFIG_TRACE_SIMPLE
63 #include "trace/simple.h"
65 #include "ui/qemu-spice.h"
67 #include "qmp-commands.h"
69 #include "qemu-thread.h"
71 /* for pic/irq_info */
72 #if defined(TARGET_SPARC)
75 #include "hw/lm32_pic.h"
78 //#define DEBUG_COMPLETION
84 * 'B' block device name
85 * 's' string (accept optional quote)
86 * 'O' option string of the form NAME=VALUE,...
87 * parsed according to QemuOptsList given by its name
88 * Example: 'device:O' uses qemu_device_opts.
89 * Restriction: only lists with empty desc are supported
90 * TODO lift the restriction
92 * 'l' target long (32 or 64 bit)
93 * 'M' Non-negative target long (32 or 64 bit), in user mode the
94 * value is multiplied by 2^20 (think Mebibyte)
95 * 'o' octets (aka bytes)
96 * user mode accepts an optional T, t, G, g, M, m, K, k
97 * suffix, which multiplies the value by 2^40 for
98 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
99 * M and m, 2^10 for K and k
101 * user mode accepts an optional ms, us, ns suffix,
102 * which divides the value by 1e3, 1e6, 1e9, respectively
103 * '/' optional gdb-like print format (like "/10x")
105 * '?' optional type (for all types, except '/')
106 * '.' other form of optional type (for 'i' and 'l')
108 * user mode accepts "on" or "off"
109 * '-' optional parameter (eg. '-f')
113 typedef struct MonitorCompletionData MonitorCompletionData
;
114 struct MonitorCompletionData
{
116 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
119 typedef struct mon_cmd_t
{
121 const char *args_type
;
124 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
126 void (*info
)(Monitor
*mon
);
127 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
128 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
129 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
130 MonitorCompletion
*cb
, void *opaque
);
135 /* file descriptors passed via SCM_RIGHTS */
136 typedef struct mon_fd_t mon_fd_t
;
140 QLIST_ENTRY(mon_fd_t
) next
;
143 /* file descriptor associated with a file descriptor set */
144 typedef struct MonFdsetFd MonFdsetFd
;
149 QLIST_ENTRY(MonFdsetFd
) next
;
152 /* file descriptor set containing fds passed via SCM_RIGHTS */
153 typedef struct MonFdset MonFdset
;
156 QLIST_HEAD(, MonFdsetFd
) fds
;
157 QLIST_ENTRY(MonFdset
) next
;
160 typedef struct MonitorControl
{
162 JSONMessageParser parser
;
167 * To prevent flooding clients, events can be throttled. The
168 * throttling is calculated globally, rather than per-Monitor
171 typedef struct MonitorEventState
{
172 MonitorEvent event
; /* Event being tracked */
173 int64_t rate
; /* Period over which to throttle. 0 to disable */
174 int64_t last
; /* Time at which event was last emitted */
175 QEMUTimer
*timer
; /* Timer for handling delayed events */
176 QObject
*data
; /* Event pending delayed dispatch */
180 CharDriverState
*chr
;
185 uint8_t outbuf
[1024];
189 CPUArchState
*mon_cpu
;
190 BlockDriverCompletionFunc
*password_completion_cb
;
191 void *password_opaque
;
193 QLIST_HEAD(,mon_fd_t
) fds
;
194 QLIST_ENTRY(Monitor
) entry
;
197 /* QMP checker flags */
198 #define QMP_ACCEPT_UNKNOWNS 1
200 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
201 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
203 static mon_cmd_t mon_cmds
[];
204 static mon_cmd_t info_cmds
[];
206 static const mon_cmd_t qmp_cmds
[];
209 Monitor
*default_mon
;
211 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
214 static inline int qmp_cmd_mode(const Monitor
*mon
)
216 return (mon
->mc
? mon
->mc
->command_mode
: 0);
219 /* Return true if in control mode, false otherwise */
220 static inline int monitor_ctrl_mode(const Monitor
*mon
)
222 return (mon
->flags
& MONITOR_USE_CONTROL
);
225 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
226 int monitor_cur_is_qmp(void)
228 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
231 void monitor_read_command(Monitor
*mon
, int show_prompt
)
236 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
238 readline_show_prompt(mon
->rs
);
241 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
244 if (monitor_ctrl_mode(mon
)) {
245 qerror_report(QERR_MISSING_PARAMETER
, "password");
247 } else if (mon
->rs
) {
248 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
249 /* prompt is printed on return from the command handler */
252 monitor_printf(mon
, "terminal does not support password prompting\n");
257 void monitor_flush(Monitor
*mon
)
259 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
260 qemu_chr_fe_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
261 mon
->outbuf_index
= 0;
265 /* flush at every end of line or if the buffer is full */
266 static void monitor_puts(Monitor
*mon
, const char *str
)
275 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
276 mon
->outbuf
[mon
->outbuf_index
++] = c
;
277 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
283 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
290 if (monitor_ctrl_mode(mon
)) {
294 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
295 monitor_puts(mon
, buf
);
298 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
302 monitor_vprintf(mon
, fmt
, ap
);
306 void monitor_print_filename(Monitor
*mon
, const char *filename
)
310 for (i
= 0; filename
[i
]; i
++) {
311 switch (filename
[i
]) {
315 monitor_printf(mon
, "\\%c", filename
[i
]);
318 monitor_printf(mon
, "\\t");
321 monitor_printf(mon
, "\\r");
324 monitor_printf(mon
, "\\n");
327 monitor_printf(mon
, "%c", filename
[i
]);
333 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
334 const char *fmt
, ...)
338 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
343 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
345 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
347 return cmd
->user_print
!= NULL
;
350 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
352 return cmd
->flags
& MONITOR_CMD_ASYNC
;
355 static inline int monitor_has_error(const Monitor
*mon
)
357 return mon
->error
!= NULL
;
360 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
364 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
365 qobject_to_json(data
);
366 assert(json
!= NULL
);
368 qstring_append_chr(json
, '\n');
369 monitor_puts(mon
, qstring_get_str(json
));
374 static QDict
*build_qmp_error_dict(const QError
*err
)
378 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
379 ErrorClass_lookup
[err
->err_class
],
382 return qobject_to_qdict(obj
);
385 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
389 trace_monitor_protocol_emitter(mon
);
391 if (!monitor_has_error(mon
)) {
392 /* success response */
395 qobject_incref(data
);
396 qdict_put_obj(qmp
, "return", data
);
398 /* return an empty QDict by default */
399 qdict_put(qmp
, "return", qdict_new());
403 qmp
= build_qmp_error_dict(mon
->error
);
409 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
413 monitor_json_emitter(mon
, QOBJECT(qmp
));
417 static void timestamp_put(QDict
*qdict
)
423 err
= qemu_gettimeofday(&tv
);
427 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
428 "'microseconds': %" PRId64
" }",
429 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
430 qdict_put_obj(qdict
, "timestamp", obj
);
434 static const char *monitor_event_names
[] = {
435 [QEVENT_SHUTDOWN
] = "SHUTDOWN",
436 [QEVENT_RESET
] = "RESET",
437 [QEVENT_POWERDOWN
] = "POWERDOWN",
438 [QEVENT_STOP
] = "STOP",
439 [QEVENT_RESUME
] = "RESUME",
440 [QEVENT_VNC_CONNECTED
] = "VNC_CONNECTED",
441 [QEVENT_VNC_INITIALIZED
] = "VNC_INITIALIZED",
442 [QEVENT_VNC_DISCONNECTED
] = "VNC_DISCONNECTED",
443 [QEVENT_BLOCK_IO_ERROR
] = "BLOCK_IO_ERROR",
444 [QEVENT_RTC_CHANGE
] = "RTC_CHANGE",
445 [QEVENT_WATCHDOG
] = "WATCHDOG",
446 [QEVENT_SPICE_CONNECTED
] = "SPICE_CONNECTED",
447 [QEVENT_SPICE_INITIALIZED
] = "SPICE_INITIALIZED",
448 [QEVENT_SPICE_DISCONNECTED
] = "SPICE_DISCONNECTED",
449 [QEVENT_BLOCK_JOB_COMPLETED
] = "BLOCK_JOB_COMPLETED",
450 [QEVENT_BLOCK_JOB_CANCELLED
] = "BLOCK_JOB_CANCELLED",
451 [QEVENT_DEVICE_TRAY_MOVED
] = "DEVICE_TRAY_MOVED",
452 [QEVENT_SUSPEND
] = "SUSPEND",
453 [QEVENT_SUSPEND_DISK
] = "SUSPEND_DISK",
454 [QEVENT_WAKEUP
] = "WAKEUP",
455 [QEVENT_BALLOON_CHANGE
] = "BALLOON_CHANGE",
457 QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names
) != QEVENT_MAX
)
459 MonitorEventState monitor_event_state
[QEVENT_MAX
];
460 QemuMutex monitor_event_state_lock
;
463 * Emits the event to every monitor instance
466 monitor_protocol_event_emit(MonitorEvent event
,
471 trace_monitor_protocol_event_emit(event
, data
);
472 QLIST_FOREACH(mon
, &mon_list
, entry
) {
473 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
474 monitor_json_emitter(mon
, data
);
481 * Queue a new event for emission to Monitor instances,
482 * applying any rate limiting if required.
485 monitor_protocol_event_queue(MonitorEvent event
,
488 MonitorEventState
*evstate
;
489 int64_t now
= qemu_get_clock_ns(rt_clock
);
490 assert(event
< QEVENT_MAX
);
492 qemu_mutex_lock(&monitor_event_state_lock
);
493 evstate
= &(monitor_event_state
[event
]);
494 trace_monitor_protocol_event_queue(event
,
500 /* Rate limit of 0 indicates no throttling */
501 if (!evstate
->rate
) {
502 monitor_protocol_event_emit(event
, data
);
505 int64_t delta
= now
- evstate
->last
;
507 delta
< evstate
->rate
) {
508 /* If there's an existing event pending, replace
509 * it with the new event, otherwise schedule a
510 * timer for delayed emission
513 qobject_decref(evstate
->data
);
515 int64_t then
= evstate
->last
+ evstate
->rate
;
516 qemu_mod_timer_ns(evstate
->timer
, then
);
518 evstate
->data
= data
;
519 qobject_incref(evstate
->data
);
521 monitor_protocol_event_emit(event
, data
);
525 qemu_mutex_unlock(&monitor_event_state_lock
);
530 * The callback invoked by QemuTimer when a delayed
531 * event is ready to be emitted
533 static void monitor_protocol_event_handler(void *opaque
)
535 MonitorEventState
*evstate
= opaque
;
536 int64_t now
= qemu_get_clock_ns(rt_clock
);
538 qemu_mutex_lock(&monitor_event_state_lock
);
540 trace_monitor_protocol_event_handler(evstate
->event
,
545 monitor_protocol_event_emit(evstate
->event
, evstate
->data
);
546 qobject_decref(evstate
->data
);
547 evstate
->data
= NULL
;
550 qemu_mutex_unlock(&monitor_event_state_lock
);
555 * @event: the event ID to be limited
556 * @rate: the rate limit in milliseconds
558 * Sets a rate limit on a particular event, so no
559 * more than 1 event will be emitted within @rate
563 monitor_protocol_event_throttle(MonitorEvent event
,
566 MonitorEventState
*evstate
;
567 assert(event
< QEVENT_MAX
);
569 evstate
= &(monitor_event_state
[event
]);
571 trace_monitor_protocol_event_throttle(event
, rate
);
572 evstate
->event
= event
;
573 evstate
->rate
= rate
* SCALE_MS
;
574 evstate
->timer
= qemu_new_timer(rt_clock
,
576 monitor_protocol_event_handler
,
579 evstate
->data
= NULL
;
583 /* Global, one-time initializer to configure the rate limiting
584 * and initialize state */
585 static void monitor_protocol_event_init(void)
587 qemu_mutex_init(&monitor_event_state_lock
);
588 /* Limit RTC & BALLOON events to 1 per second */
589 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE
, 1000);
590 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE
, 1000);
591 monitor_protocol_event_throttle(QEVENT_WATCHDOG
, 1000);
595 * monitor_protocol_event(): Generate a Monitor event
597 * Event-specific data can be emitted through the (optional) 'data' parameter.
599 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
602 const char *event_name
;
604 assert(event
< QEVENT_MAX
);
606 event_name
= monitor_event_names
[event
];
607 assert(event_name
!= NULL
);
611 qdict_put(qmp
, "event", qstring_from_str(event_name
));
613 qobject_incref(data
);
614 qdict_put_obj(qmp
, "data", data
);
617 trace_monitor_protocol_event(event
, event_name
, qmp
);
618 monitor_protocol_event_queue(event
, QOBJECT(qmp
));
622 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
625 /* Will setup QMP capabilities in the future */
626 if (monitor_ctrl_mode(mon
)) {
627 mon
->mc
->command_mode
= 1;
633 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
635 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
636 int64_t cpu_index
, Error
**errp
)
639 Monitor
*old_mon
, hmp
;
640 CharDriverState mchar
;
642 memset(&hmp
, 0, sizeof(hmp
));
643 qemu_chr_init_mem(&mchar
);
650 int ret
= monitor_set_cpu(cpu_index
);
653 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
659 handle_user_command(&hmp
, command_line
);
662 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
663 QString
*str
= qemu_chr_mem_to_qs(hmp
.chr
);
664 output
= g_strdup(qstring_get_str(str
));
667 output
= g_strdup("");
671 qemu_chr_close_mem(hmp
.chr
);
675 static int compare_cmd(const char *name
, const char *list
)
677 const char *p
, *pstart
;
685 p
= pstart
+ strlen(pstart
);
686 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
695 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
696 const char *prefix
, const char *name
)
698 const mon_cmd_t
*cmd
;
700 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
701 if (!name
|| !strcmp(name
, cmd
->name
))
702 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
703 cmd
->params
, cmd
->help
);
707 static void help_cmd(Monitor
*mon
, const char *name
)
709 if (name
&& !strcmp(name
, "info")) {
710 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
712 help_cmd_dump(mon
, mon_cmds
, "", name
);
713 if (name
&& !strcmp(name
, "log")) {
714 const CPULogItem
*item
;
715 monitor_printf(mon
, "Log items (comma separated):\n");
716 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
717 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
718 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
724 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
726 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
729 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
731 const char *tp_name
= qdict_get_str(qdict
, "name");
732 bool new_state
= qdict_get_bool(qdict
, "option");
733 int ret
= trace_event_set_state(tp_name
, new_state
);
736 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
740 #ifdef CONFIG_TRACE_SIMPLE
741 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
743 const char *op
= qdict_get_try_str(qdict
, "op");
744 const char *arg
= qdict_get_try_str(qdict
, "arg");
747 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
748 } else if (!strcmp(op
, "on")) {
749 st_set_trace_file_enabled(true);
750 } else if (!strcmp(op
, "off")) {
751 st_set_trace_file_enabled(false);
752 } else if (!strcmp(op
, "flush")) {
753 st_flush_trace_buffer();
754 } else if (!strcmp(op
, "set")) {
756 st_set_trace_file(arg
);
759 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
760 help_cmd(mon
, "trace-file");
765 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
767 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
770 data
->user_print(data
->mon
, ret_data
);
772 monitor_resume(data
->mon
);
776 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
778 monitor_protocol_emitter(opaque
, ret_data
);
781 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
784 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
787 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
792 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
794 cb_data
->user_print
= cmd
->user_print
;
795 monitor_suspend(mon
);
796 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
797 user_monitor_complete
, cb_data
);
804 static void do_info(Monitor
*mon
, const QDict
*qdict
)
806 const mon_cmd_t
*cmd
;
807 const char *item
= qdict_get_try_str(qdict
, "item");
813 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
814 if (compare_cmd(item
, cmd
->name
))
818 if (cmd
->name
== NULL
) {
822 cmd
->mhandler
.info(mon
);
826 help_cmd(mon
, "info");
829 CommandInfoList
*qmp_query_commands(Error
**errp
)
831 CommandInfoList
*info
, *cmd_list
= NULL
;
832 const mon_cmd_t
*cmd
;
834 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
835 info
= g_malloc0(sizeof(*info
));
836 info
->value
= g_malloc0(sizeof(*info
->value
));
837 info
->value
->name
= g_strdup(cmd
->name
);
839 info
->next
= cmd_list
;
846 EventInfoList
*qmp_query_events(Error
**errp
)
848 EventInfoList
*info
, *ev_list
= NULL
;
851 for (e
= 0 ; e
< QEVENT_MAX
; e
++) {
852 const char *event_name
= monitor_event_names
[e
];
853 assert(event_name
!= NULL
);
854 info
= g_malloc0(sizeof(*info
));
855 info
->value
= g_malloc0(sizeof(*info
->value
));
856 info
->value
->name
= g_strdup(event_name
);
858 info
->next
= ev_list
;
865 /* set the current CPU defined by the user */
866 int monitor_set_cpu(int cpu_index
)
870 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
871 if (env
->cpu_index
== cpu_index
) {
872 cur_mon
->mon_cpu
= env
;
879 static CPUArchState
*mon_get_cpu(void)
881 if (!cur_mon
->mon_cpu
) {
884 cpu_synchronize_state(cur_mon
->mon_cpu
);
885 return cur_mon
->mon_cpu
;
888 int monitor_get_cpu_index(void)
890 return mon_get_cpu()->cpu_index
;
893 static void do_info_registers(Monitor
*mon
)
898 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
901 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
906 static void do_info_jit(Monitor
*mon
)
908 dump_exec_info((FILE *)mon
, monitor_fprintf
);
911 static void do_info_history(Monitor
*mon
)
920 str
= readline_get_history(mon
->rs
, i
);
923 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
928 #if defined(TARGET_PPC)
929 /* XXX: not implemented in other targets */
930 static void do_info_cpu_stats(Monitor
*mon
)
935 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
939 static void do_trace_print_events(Monitor
*mon
)
941 trace_print_events((FILE *)mon
, &monitor_fprintf
);
944 static int add_graphics_client(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
946 const char *protocol
= qdict_get_str(qdict
, "protocol");
947 const char *fdname
= qdict_get_str(qdict
, "fdname");
950 if (strcmp(protocol
, "spice") == 0) {
951 int fd
= monitor_get_fd(mon
, fdname
);
952 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
953 int tls
= qdict_get_try_bool(qdict
, "tls", 0);
955 /* correct one? spice isn't a device ,,, */
956 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
959 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
964 } else if (strcmp(protocol
, "vnc") == 0) {
965 int fd
= monitor_get_fd(mon
, fdname
);
966 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
967 vnc_display_add_client(NULL
, fd
, skipauth
);
970 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
971 int fd
= monitor_get_fd(mon
, fdname
);
972 if (qemu_chr_add_client(s
, fd
) < 0) {
973 qerror_report(QERR_ADD_CLIENT_FAILED
);
979 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
983 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
,
984 MonitorCompletion cb
, void *opaque
)
986 const char *protocol
= qdict_get_str(qdict
, "protocol");
987 const char *hostname
= qdict_get_str(qdict
, "hostname");
988 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
989 int port
= qdict_get_try_int(qdict
, "port", -1);
990 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
993 if (strcmp(protocol
, "spice") == 0) {
995 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
999 if (port
== -1 && tls_port
== -1) {
1000 qerror_report(QERR_MISSING_PARAMETER
, "port/tls-port");
1004 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
,
1007 qerror_report(QERR_UNDEFINED_ERROR
);
1013 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1017 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1019 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1023 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1025 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1028 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1031 const char *items
= qdict_get_str(qdict
, "items");
1033 if (!strcmp(items
, "none")) {
1036 mask
= cpu_str_to_log_mask(items
);
1038 help_cmd(mon
, "log");
1045 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1047 const char *option
= qdict_get_try_str(qdict
, "option");
1048 if (!option
|| !strcmp(option
, "on")) {
1050 } else if (!strcmp(option
, "off")) {
1053 monitor_printf(mon
, "unexpected option %s\n", option
);
1057 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1059 const char *device
= qdict_get_try_str(qdict
, "device");
1061 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1062 if (gdbserver_start(device
) < 0) {
1063 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1065 } else if (strcmp(device
, "none") == 0) {
1066 monitor_printf(mon
, "Disabled gdbserver\n");
1068 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1073 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1075 const char *action
= qdict_get_str(qdict
, "action");
1076 if (select_watchdog_action(action
) == -1) {
1077 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1081 static void monitor_printc(Monitor
*mon
, int c
)
1083 monitor_printf(mon
, "'");
1086 monitor_printf(mon
, "\\'");
1089 monitor_printf(mon
, "\\\\");
1092 monitor_printf(mon
, "\\n");
1095 monitor_printf(mon
, "\\r");
1098 if (c
>= 32 && c
<= 126) {
1099 monitor_printf(mon
, "%c", c
);
1101 monitor_printf(mon
, "\\x%02x", c
);
1105 monitor_printf(mon
, "'");
1108 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1109 target_phys_addr_t addr
, int is_physical
)
1112 int l
, line_size
, i
, max_digits
, len
;
1116 if (format
== 'i') {
1119 env
= mon_get_cpu();
1123 } else if (wsize
== 4) {
1126 /* as default we use the current CS size */
1129 #ifdef TARGET_X86_64
1130 if ((env
->efer
& MSR_EFER_LMA
) &&
1131 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1135 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1140 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1144 len
= wsize
* count
;
1153 max_digits
= (wsize
* 8 + 2) / 3;
1157 max_digits
= (wsize
* 8) / 4;
1161 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1170 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1172 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1177 cpu_physical_memory_read(addr
, buf
, l
);
1179 env
= mon_get_cpu();
1180 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1181 monitor_printf(mon
, " Cannot access memory\n");
1190 v
= ldub_raw(buf
+ i
);
1193 v
= lduw_raw(buf
+ i
);
1196 v
= (uint32_t)ldl_raw(buf
+ i
);
1199 v
= ldq_raw(buf
+ i
);
1202 monitor_printf(mon
, " ");
1205 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1208 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1211 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1214 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1217 monitor_printc(mon
, v
);
1222 monitor_printf(mon
, "\n");
1228 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1230 int count
= qdict_get_int(qdict
, "count");
1231 int format
= qdict_get_int(qdict
, "format");
1232 int size
= qdict_get_int(qdict
, "size");
1233 target_long addr
= qdict_get_int(qdict
, "addr");
1235 memory_dump(mon
, count
, format
, size
, addr
, 0);
1238 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1240 int count
= qdict_get_int(qdict
, "count");
1241 int format
= qdict_get_int(qdict
, "format");
1242 int size
= qdict_get_int(qdict
, "size");
1243 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1245 memory_dump(mon
, count
, format
, size
, addr
, 1);
1248 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1250 int format
= qdict_get_int(qdict
, "format");
1251 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1255 monitor_printf(mon
, "%#" TARGET_PRIoPHYS
, val
);
1258 monitor_printf(mon
, "%#" TARGET_PRIxPHYS
, val
);
1261 monitor_printf(mon
, "%" TARGET_PRIuPHYS
, val
);
1265 monitor_printf(mon
, "%" TARGET_PRIdPHYS
, val
);
1268 monitor_printc(mon
, val
);
1271 monitor_printf(mon
, "\n");
1274 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1278 uint32_t start
= qdict_get_int(qdict
, "start");
1279 uint32_t size
= qdict_get_int(qdict
, "size");
1282 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1283 uint8_t val
= ldub_phys(addr
);
1284 /* BSD sum algorithm ('sum' Unix command) */
1285 sum
= (sum
>> 1) | (sum
<< 15);
1288 monitor_printf(mon
, "%05d\n", sum
);
1296 static const KeyDef key_defs
[] = {
1298 { 0x36, "shift_r" },
1303 { 0xe4, "altgr_r" },
1323 { 0x0e, "backspace" },
1336 { 0x1a, "bracket_left" },
1337 { 0x1b, "bracket_right" },
1349 { 0x27, "semicolon" },
1350 { 0x28, "apostrophe" },
1351 { 0x29, "grave_accent" },
1353 { 0x2b, "backslash" },
1365 { 0x37, "asterisk" },
1368 { 0x3a, "caps_lock" },
1379 { 0x45, "num_lock" },
1380 { 0x46, "scroll_lock" },
1382 { 0xb5, "kp_divide" },
1383 { 0x37, "kp_multiply" },
1384 { 0x4a, "kp_subtract" },
1386 { 0x9c, "kp_enter" },
1387 { 0x53, "kp_decimal" },
1420 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1435 { 0xfe, "compose" },
1440 static int get_keycode(const char *key
)
1446 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1447 if (!strcmp(key
, p
->name
))
1450 if (strstart(key
, "0x", NULL
)) {
1451 ret
= strtoul(key
, &endp
, 0);
1452 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1458 #define MAX_KEYCODES 16
1459 static uint8_t keycodes
[MAX_KEYCODES
];
1460 static int nb_pending_keycodes
;
1461 static QEMUTimer
*key_timer
;
1463 static void release_keys(void *opaque
)
1467 while (nb_pending_keycodes
> 0) {
1468 nb_pending_keycodes
--;
1469 keycode
= keycodes
[nb_pending_keycodes
];
1471 kbd_put_keycode(0xe0);
1472 kbd_put_keycode(keycode
| 0x80);
1476 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1478 char keyname_buf
[16];
1480 int keyname_len
, keycode
, i
;
1481 const char *string
= qdict_get_str(qdict
, "string");
1482 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1483 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1485 if (nb_pending_keycodes
> 0) {
1486 qemu_del_timer(key_timer
);
1493 separator
= strchr(string
, '-');
1494 keyname_len
= separator
? separator
- string
: strlen(string
);
1495 if (keyname_len
> 0) {
1496 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1497 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1498 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1501 if (i
== MAX_KEYCODES
) {
1502 monitor_printf(mon
, "too many keys\n");
1505 keyname_buf
[keyname_len
] = 0;
1506 keycode
= get_keycode(keyname_buf
);
1508 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1511 keycodes
[i
++] = keycode
;
1515 string
= separator
+ 1;
1517 nb_pending_keycodes
= i
;
1518 /* key down events */
1519 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1520 keycode
= keycodes
[i
];
1522 kbd_put_keycode(0xe0);
1523 kbd_put_keycode(keycode
& 0x7f);
1525 /* delayed key up events */
1526 qemu_mod_timer(key_timer
, qemu_get_clock_ns(vm_clock
) +
1527 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1530 static int mouse_button_state
;
1532 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1535 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1536 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1537 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1538 dx
= strtol(dx_str
, NULL
, 0);
1539 dy
= strtol(dy_str
, NULL
, 0);
1542 dz
= strtol(dz_str
, NULL
, 0);
1543 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1546 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1548 int button_state
= qdict_get_int(qdict
, "button_state");
1549 mouse_button_state
= button_state
;
1550 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1553 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1555 int size
= qdict_get_int(qdict
, "size");
1556 int addr
= qdict_get_int(qdict
, "addr");
1557 int has_index
= qdict_haskey(qdict
, "index");
1562 int index
= qdict_get_int(qdict
, "index");
1563 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1571 val
= cpu_inb(addr
);
1575 val
= cpu_inw(addr
);
1579 val
= cpu_inl(addr
);
1583 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1584 suffix
, addr
, size
* 2, val
);
1587 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1589 int size
= qdict_get_int(qdict
, "size");
1590 int addr
= qdict_get_int(qdict
, "addr");
1591 int val
= qdict_get_int(qdict
, "val");
1593 addr
&= IOPORTS_MASK
;
1598 cpu_outb(addr
, val
);
1601 cpu_outw(addr
, val
);
1604 cpu_outl(addr
, val
);
1609 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1612 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1614 res
= qemu_boot_set(bootdevice
);
1616 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1617 } else if (res
> 0) {
1618 monitor_printf(mon
, "setting boot device list failed\n");
1620 monitor_printf(mon
, "no function defined to set boot device list for "
1621 "this architecture\n");
1625 #if defined(TARGET_I386)
1626 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
1627 target_phys_addr_t pte
,
1628 target_phys_addr_t mask
)
1630 #ifdef TARGET_X86_64
1631 if (addr
& (1ULL << 47)) {
1635 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1636 " %c%c%c%c%c%c%c%c%c\n",
1639 pte
& PG_NX_MASK
? 'X' : '-',
1640 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1641 pte
& PG_PSE_MASK
? 'P' : '-',
1642 pte
& PG_DIRTY_MASK
? 'D' : '-',
1643 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1644 pte
& PG_PCD_MASK
? 'C' : '-',
1645 pte
& PG_PWT_MASK
? 'T' : '-',
1646 pte
& PG_USER_MASK
? 'U' : '-',
1647 pte
& PG_RW_MASK
? 'W' : '-');
1650 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
1652 unsigned int l1
, l2
;
1653 uint32_t pgd
, pde
, pte
;
1655 pgd
= env
->cr
[3] & ~0xfff;
1656 for(l1
= 0; l1
< 1024; l1
++) {
1657 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1658 pde
= le32_to_cpu(pde
);
1659 if (pde
& PG_PRESENT_MASK
) {
1660 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1662 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1664 for(l2
= 0; l2
< 1024; l2
++) {
1665 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1666 pte
= le32_to_cpu(pte
);
1667 if (pte
& PG_PRESENT_MASK
) {
1668 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1678 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1680 unsigned int l1
, l2
, l3
;
1681 uint64_t pdpe
, pde
, pte
;
1682 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1684 pdp_addr
= env
->cr
[3] & ~0x1f;
1685 for (l1
= 0; l1
< 4; l1
++) {
1686 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1687 pdpe
= le64_to_cpu(pdpe
);
1688 if (pdpe
& PG_PRESENT_MASK
) {
1689 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1690 for (l2
= 0; l2
< 512; l2
++) {
1691 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1692 pde
= le64_to_cpu(pde
);
1693 if (pde
& PG_PRESENT_MASK
) {
1694 if (pde
& PG_PSE_MASK
) {
1695 /* 2M pages with PAE, CR4.PSE is ignored */
1696 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1697 ~((target_phys_addr_t
)(1 << 20) - 1));
1699 pt_addr
= pde
& 0x3fffffffff000ULL
;
1700 for (l3
= 0; l3
< 512; l3
++) {
1701 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1702 pte
= le64_to_cpu(pte
);
1703 if (pte
& PG_PRESENT_MASK
) {
1704 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1707 ~(target_phys_addr_t
)0xfff);
1717 #ifdef TARGET_X86_64
1718 static void tlb_info_64(Monitor
*mon
, CPUArchState
*env
)
1720 uint64_t l1
, l2
, l3
, l4
;
1721 uint64_t pml4e
, pdpe
, pde
, pte
;
1722 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1724 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1725 for (l1
= 0; l1
< 512; l1
++) {
1726 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1727 pml4e
= le64_to_cpu(pml4e
);
1728 if (pml4e
& PG_PRESENT_MASK
) {
1729 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1730 for (l2
= 0; l2
< 512; l2
++) {
1731 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1732 pdpe
= le64_to_cpu(pdpe
);
1733 if (pdpe
& PG_PRESENT_MASK
) {
1734 if (pdpe
& PG_PSE_MASK
) {
1735 /* 1G pages, CR4.PSE is ignored */
1736 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1737 0x3ffffc0000000ULL
);
1739 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1740 for (l3
= 0; l3
< 512; l3
++) {
1741 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1742 pde
= le64_to_cpu(pde
);
1743 if (pde
& PG_PRESENT_MASK
) {
1744 if (pde
& PG_PSE_MASK
) {
1745 /* 2M pages, CR4.PSE is ignored */
1746 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1748 0x3ffffffe00000ULL
);
1750 pt_addr
= pde
& 0x3fffffffff000ULL
;
1751 for (l4
= 0; l4
< 512; l4
++) {
1752 cpu_physical_memory_read(pt_addr
1755 pte
= le64_to_cpu(pte
);
1756 if (pte
& PG_PRESENT_MASK
) {
1757 print_pte(mon
, (l1
<< 39) +
1759 (l3
<< 21) + (l4
<< 12),
1761 0x3fffffffff000ULL
);
1775 static void tlb_info(Monitor
*mon
)
1779 env
= mon_get_cpu();
1781 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1782 monitor_printf(mon
, "PG disabled\n");
1785 if (env
->cr
[4] & CR4_PAE_MASK
) {
1786 #ifdef TARGET_X86_64
1787 if (env
->hflags
& HF_LMA_MASK
) {
1788 tlb_info_64(mon
, env
);
1792 tlb_info_pae32(mon
, env
);
1795 tlb_info_32(mon
, env
);
1799 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
1801 target_phys_addr_t end
, int prot
)
1804 prot1
= *plast_prot
;
1805 if (prot
!= prot1
) {
1806 if (*pstart
!= -1) {
1807 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
1808 TARGET_FMT_plx
" %c%c%c\n",
1809 *pstart
, end
, end
- *pstart
,
1810 prot1
& PG_USER_MASK
? 'u' : '-',
1812 prot1
& PG_RW_MASK
? 'w' : '-');
1822 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
1824 unsigned int l1
, l2
;
1825 int prot
, last_prot
;
1826 uint32_t pgd
, pde
, pte
;
1827 target_phys_addr_t start
, end
;
1829 pgd
= env
->cr
[3] & ~0xfff;
1832 for(l1
= 0; l1
< 1024; l1
++) {
1833 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1834 pde
= le32_to_cpu(pde
);
1836 if (pde
& PG_PRESENT_MASK
) {
1837 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1838 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1839 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1841 for(l2
= 0; l2
< 1024; l2
++) {
1842 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1843 pte
= le32_to_cpu(pte
);
1844 end
= (l1
<< 22) + (l2
<< 12);
1845 if (pte
& PG_PRESENT_MASK
) {
1847 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1851 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1856 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1859 /* Flush last range */
1860 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
1863 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1865 unsigned int l1
, l2
, l3
;
1866 int prot
, last_prot
;
1867 uint64_t pdpe
, pde
, pte
;
1868 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1869 target_phys_addr_t start
, end
;
1871 pdp_addr
= env
->cr
[3] & ~0x1f;
1874 for (l1
= 0; l1
< 4; l1
++) {
1875 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1876 pdpe
= le64_to_cpu(pdpe
);
1878 if (pdpe
& PG_PRESENT_MASK
) {
1879 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1880 for (l2
= 0; l2
< 512; l2
++) {
1881 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1882 pde
= le64_to_cpu(pde
);
1883 end
= (l1
<< 30) + (l2
<< 21);
1884 if (pde
& PG_PRESENT_MASK
) {
1885 if (pde
& PG_PSE_MASK
) {
1886 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1888 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1890 pt_addr
= pde
& 0x3fffffffff000ULL
;
1891 for (l3
= 0; l3
< 512; l3
++) {
1892 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1893 pte
= le64_to_cpu(pte
);
1894 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
1895 if (pte
& PG_PRESENT_MASK
) {
1896 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
1901 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1906 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1911 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1914 /* Flush last range */
1915 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
1919 #ifdef TARGET_X86_64
1920 static void mem_info_64(Monitor
*mon
, CPUArchState
*env
)
1922 int prot
, last_prot
;
1923 uint64_t l1
, l2
, l3
, l4
;
1924 uint64_t pml4e
, pdpe
, pde
, pte
;
1925 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
1927 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1930 for (l1
= 0; l1
< 512; l1
++) {
1931 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1932 pml4e
= le64_to_cpu(pml4e
);
1934 if (pml4e
& PG_PRESENT_MASK
) {
1935 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1936 for (l2
= 0; l2
< 512; l2
++) {
1937 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1938 pdpe
= le64_to_cpu(pdpe
);
1939 end
= (l1
<< 39) + (l2
<< 30);
1940 if (pdpe
& PG_PRESENT_MASK
) {
1941 if (pdpe
& PG_PSE_MASK
) {
1942 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
1945 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1947 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1948 for (l3
= 0; l3
< 512; l3
++) {
1949 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1950 pde
= le64_to_cpu(pde
);
1951 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
1952 if (pde
& PG_PRESENT_MASK
) {
1953 if (pde
& PG_PSE_MASK
) {
1954 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1956 prot
&= pml4e
& pdpe
;
1957 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1959 pt_addr
= pde
& 0x3fffffffff000ULL
;
1960 for (l4
= 0; l4
< 512; l4
++) {
1961 cpu_physical_memory_read(pt_addr
1964 pte
= le64_to_cpu(pte
);
1965 end
= (l1
<< 39) + (l2
<< 30) +
1966 (l3
<< 21) + (l4
<< 12);
1967 if (pte
& PG_PRESENT_MASK
) {
1968 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
1970 prot
&= pml4e
& pdpe
& pde
;
1974 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1979 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1985 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1990 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1993 /* Flush last range */
1994 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 48, 0);
1998 static void mem_info(Monitor
*mon
)
2002 env
= mon_get_cpu();
2004 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2005 monitor_printf(mon
, "PG disabled\n");
2008 if (env
->cr
[4] & CR4_PAE_MASK
) {
2009 #ifdef TARGET_X86_64
2010 if (env
->hflags
& HF_LMA_MASK
) {
2011 mem_info_64(mon
, env
);
2015 mem_info_pae32(mon
, env
);
2018 mem_info_32(mon
, env
);
2023 #if defined(TARGET_SH4)
2025 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2027 monitor_printf(mon
, " tlb%i:\t"
2028 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2029 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2030 "dirty=%hhu writethrough=%hhu\n",
2032 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2033 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2037 static void tlb_info(Monitor
*mon
)
2039 CPUArchState
*env
= mon_get_cpu();
2042 monitor_printf (mon
, "ITLB:\n");
2043 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2044 print_tlb (mon
, i
, &env
->itlb
[i
]);
2045 monitor_printf (mon
, "UTLB:\n");
2046 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2047 print_tlb (mon
, i
, &env
->utlb
[i
]);
2052 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
2053 static void tlb_info(Monitor
*mon
)
2055 CPUArchState
*env1
= mon_get_cpu();
2057 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
2061 static void do_info_mtree(Monitor
*mon
)
2063 mtree_info((fprintf_function
)monitor_printf
, mon
);
2066 static void do_info_numa(Monitor
*mon
)
2071 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2072 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2073 monitor_printf(mon
, "node %d cpus:", i
);
2074 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2075 if (env
->numa_node
== i
) {
2076 monitor_printf(mon
, " %d", env
->cpu_index
);
2079 monitor_printf(mon
, "\n");
2080 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2085 #ifdef CONFIG_PROFILER
2090 static void do_info_profile(Monitor
*mon
)
2096 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2097 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2098 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2099 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2104 static void do_info_profile(Monitor
*mon
)
2106 monitor_printf(mon
, "Internal profiler not compiled\n");
2110 /* Capture support */
2111 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2113 static void do_info_capture(Monitor
*mon
)
2118 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2119 monitor_printf(mon
, "[%d]: ", i
);
2120 s
->ops
.info (s
->opaque
);
2125 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2128 int n
= qdict_get_int(qdict
, "n");
2131 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2133 s
->ops
.destroy (s
->opaque
);
2134 QLIST_REMOVE (s
, entries
);
2141 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2143 const char *path
= qdict_get_str(qdict
, "path");
2144 int has_freq
= qdict_haskey(qdict
, "freq");
2145 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2146 int has_bits
= qdict_haskey(qdict
, "bits");
2147 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2148 int has_channels
= qdict_haskey(qdict
, "nchannels");
2149 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2152 s
= g_malloc0 (sizeof (*s
));
2154 freq
= has_freq
? freq
: 44100;
2155 bits
= has_bits
? bits
: 16;
2156 nchannels
= has_channels
? nchannels
: 2;
2158 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2159 monitor_printf(mon
, "Failed to add wave capture\n");
2163 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2167 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2169 qemu_acl
*acl
= qemu_acl_find(name
);
2172 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2177 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2179 const char *aclname
= qdict_get_str(qdict
, "aclname");
2180 qemu_acl
*acl
= find_acl(mon
, aclname
);
2181 qemu_acl_entry
*entry
;
2185 monitor_printf(mon
, "policy: %s\n",
2186 acl
->defaultDeny
? "deny" : "allow");
2187 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2189 monitor_printf(mon
, "%d: %s %s\n", i
,
2190 entry
->deny
? "deny" : "allow", entry
->match
);
2195 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2197 const char *aclname
= qdict_get_str(qdict
, "aclname");
2198 qemu_acl
*acl
= find_acl(mon
, aclname
);
2201 qemu_acl_reset(acl
);
2202 monitor_printf(mon
, "acl: removed all rules\n");
2206 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2208 const char *aclname
= qdict_get_str(qdict
, "aclname");
2209 const char *policy
= qdict_get_str(qdict
, "policy");
2210 qemu_acl
*acl
= find_acl(mon
, aclname
);
2213 if (strcmp(policy
, "allow") == 0) {
2214 acl
->defaultDeny
= 0;
2215 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2216 } else if (strcmp(policy
, "deny") == 0) {
2217 acl
->defaultDeny
= 1;
2218 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2220 monitor_printf(mon
, "acl: unknown policy '%s', "
2221 "expected 'deny' or 'allow'\n", policy
);
2226 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2228 const char *aclname
= qdict_get_str(qdict
, "aclname");
2229 const char *match
= qdict_get_str(qdict
, "match");
2230 const char *policy
= qdict_get_str(qdict
, "policy");
2231 int has_index
= qdict_haskey(qdict
, "index");
2232 int index
= qdict_get_try_int(qdict
, "index", -1);
2233 qemu_acl
*acl
= find_acl(mon
, aclname
);
2237 if (strcmp(policy
, "allow") == 0) {
2239 } else if (strcmp(policy
, "deny") == 0) {
2242 monitor_printf(mon
, "acl: unknown policy '%s', "
2243 "expected 'deny' or 'allow'\n", policy
);
2247 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2249 ret
= qemu_acl_append(acl
, deny
, match
);
2251 monitor_printf(mon
, "acl: unable to add acl entry\n");
2253 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2257 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2259 const char *aclname
= qdict_get_str(qdict
, "aclname");
2260 const char *match
= qdict_get_str(qdict
, "match");
2261 qemu_acl
*acl
= find_acl(mon
, aclname
);
2265 ret
= qemu_acl_remove(acl
, match
);
2267 monitor_printf(mon
, "acl: no matching acl entry\n");
2269 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2273 #if defined(TARGET_I386)
2274 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2277 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2278 int bank
= qdict_get_int(qdict
, "bank");
2279 uint64_t status
= qdict_get_int(qdict
, "status");
2280 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2281 uint64_t addr
= qdict_get_int(qdict
, "addr");
2282 uint64_t misc
= qdict_get_int(qdict
, "misc");
2283 int flags
= MCE_INJECT_UNCOND_AO
;
2285 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2286 flags
|= MCE_INJECT_BROADCAST
;
2288 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
2289 if (cenv
->cpu_index
== cpu_index
) {
2290 cpu_x86_inject_mce(mon
, cenv
, bank
, status
, mcg_status
, addr
, misc
,
2298 void qmp_getfd(const char *fdname
, Error
**errp
)
2303 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
2305 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2309 if (qemu_isdigit(fdname
[0])) {
2310 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2311 "a name not starting with a digit");
2315 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2316 if (strcmp(monfd
->name
, fdname
) != 0) {
2325 monfd
= g_malloc0(sizeof(mon_fd_t
));
2326 monfd
->name
= g_strdup(fdname
);
2329 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2332 void qmp_closefd(const char *fdname
, Error
**errp
)
2336 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2337 if (strcmp(monfd
->name
, fdname
) != 0) {
2341 QLIST_REMOVE(monfd
, next
);
2343 g_free(monfd
->name
);
2348 error_set(errp
, QERR_FD_NOT_FOUND
, fdname
);
2351 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2353 int saved_vm_running
= runstate_is_running();
2354 const char *name
= qdict_get_str(qdict
, "name");
2356 vm_stop(RUN_STATE_RESTORE_VM
);
2358 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2363 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2367 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2370 if (strcmp(monfd
->name
, fdname
) != 0) {
2376 /* caller takes ownership of fd */
2377 QLIST_REMOVE(monfd
, next
);
2378 g_free(monfd
->name
);
2387 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2389 MonFdsetFd
*mon_fdset_fd
;
2390 MonFdsetFd
*mon_fdset_fd_next
;
2392 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2393 if (mon_fdset_fd
->removed
) {
2394 close(mon_fdset_fd
->fd
);
2395 g_free(mon_fdset_fd
->opaque
);
2396 QLIST_REMOVE(mon_fdset_fd
, next
);
2397 g_free(mon_fdset_fd
);
2401 if (QLIST_EMPTY(&mon_fdset
->fds
)) {
2402 QLIST_REMOVE(mon_fdset
, next
);
2407 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2408 const char *opaque
, Error
**errp
)
2411 Monitor
*mon
= cur_mon
;
2412 MonFdset
*mon_fdset
;
2413 MonFdsetFd
*mon_fdset_fd
;
2416 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2418 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2423 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2424 if (mon_fdset
->id
== fdset_id
) {
2428 if (mon_fdset
== NULL
) {
2429 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2430 "an existing fdset-id");
2434 int64_t fdset_id_prev
= -1;
2435 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2437 /* Use first available fdset ID */
2438 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2439 mon_fdset_cur
= mon_fdset
;
2440 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2441 fdset_id_prev
= mon_fdset_cur
->id
;
2447 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2448 mon_fdset
->id
= fdset_id_prev
+ 1;
2450 /* The fdset list is ordered by fdset ID */
2451 if (mon_fdset
->id
== 0) {
2452 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2453 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2454 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2456 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2460 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2461 mon_fdset_fd
->fd
= fd
;
2462 mon_fdset_fd
->removed
= false;
2464 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2466 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2468 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2469 fdinfo
->fdset_id
= mon_fdset
->id
;
2470 fdinfo
->fd
= mon_fdset_fd
->fd
;
2481 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2483 MonFdset
*mon_fdset
;
2484 MonFdsetFd
*mon_fdset_fd
;
2487 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2488 if (mon_fdset
->id
!= fdset_id
) {
2491 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2493 if (mon_fdset_fd
->fd
!= fd
) {
2496 mon_fdset_fd
->removed
= true;
2499 mon_fdset_fd
->removed
= true;
2502 if (has_fd
&& !mon_fdset_fd
) {
2505 monitor_fdset_cleanup(mon_fdset
);
2511 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2514 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2516 error_set(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2519 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2521 MonFdset
*mon_fdset
;
2522 MonFdsetFd
*mon_fdset_fd
;
2523 FdsetInfoList
*fdset_list
= NULL
;
2525 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2526 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2527 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2529 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2530 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2532 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2533 FdsetFdInfoList
*fdsetfd_info
;
2535 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2536 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2537 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2538 if (mon_fdset_fd
->opaque
) {
2539 fdsetfd_info
->value
->has_opaque
= true;
2540 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2542 fdsetfd_info
->value
->has_opaque
= false;
2545 fdsetfd_info
->next
= fdsetfd_list
;
2546 fdsetfd_list
= fdsetfd_info
;
2549 fdset_info
->value
->fds
= fdsetfd_list
;
2551 fdset_info
->next
= fdset_list
;
2552 fdset_list
= fdset_info
;
2558 /* mon_cmds and info_cmds would be sorted at runtime */
2559 static mon_cmd_t mon_cmds
[] = {
2560 #include "hmp-commands.h"
2564 /* Please update hmp-commands.hx when adding or changing commands */
2565 static mon_cmd_t info_cmds
[] = {
2570 .help
= "show the version of QEMU",
2571 .mhandler
.info
= hmp_info_version
,
2577 .help
= "show the network state",
2578 .mhandler
.info
= do_info_network
,
2584 .help
= "show the character devices",
2585 .mhandler
.info
= hmp_info_chardev
,
2591 .help
= "show the block devices",
2592 .mhandler
.info
= hmp_info_block
,
2595 .name
= "blockstats",
2598 .help
= "show block device statistics",
2599 .mhandler
.info
= hmp_info_blockstats
,
2602 .name
= "block-jobs",
2605 .help
= "show progress of ongoing block device operations",
2606 .mhandler
.info
= hmp_info_block_jobs
,
2609 .name
= "registers",
2612 .help
= "show the cpu registers",
2613 .mhandler
.info
= do_info_registers
,
2619 .help
= "show infos for each CPU",
2620 .mhandler
.info
= hmp_info_cpus
,
2626 .help
= "show the command line history",
2627 .mhandler
.info
= do_info_history
,
2629 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2630 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2635 .help
= "show the interrupts statistics (if available)",
2637 .mhandler
.info
= sun4m_irq_info
,
2638 #elif defined(TARGET_LM32)
2639 .mhandler
.info
= lm32_irq_info
,
2641 .mhandler
.info
= irq_info
,
2648 .help
= "show i8259 (PIC) state",
2650 .mhandler
.info
= sun4m_pic_info
,
2651 #elif defined(TARGET_LM32)
2652 .mhandler
.info
= lm32_do_pic_info
,
2654 .mhandler
.info
= pic_info
,
2662 .help
= "show PCI info",
2663 .mhandler
.info
= hmp_info_pci
,
2665 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2666 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2671 .help
= "show virtual to physical memory mappings",
2672 .mhandler
.info
= tlb_info
,
2675 #if defined(TARGET_I386)
2680 .help
= "show the active virtual memory mappings",
2681 .mhandler
.info
= mem_info
,
2688 .help
= "show memory tree",
2689 .mhandler
.info
= do_info_mtree
,
2695 .help
= "show dynamic compiler info",
2696 .mhandler
.info
= do_info_jit
,
2702 .help
= "show KVM information",
2703 .mhandler
.info
= hmp_info_kvm
,
2709 .help
= "show NUMA information",
2710 .mhandler
.info
= do_info_numa
,
2716 .help
= "show guest USB devices",
2717 .mhandler
.info
= usb_info
,
2723 .help
= "show host USB devices",
2724 .mhandler
.info
= usb_host_info
,
2730 .help
= "show profiling information",
2731 .mhandler
.info
= do_info_profile
,
2737 .help
= "show capture information",
2738 .mhandler
.info
= do_info_capture
,
2741 .name
= "snapshots",
2744 .help
= "show the currently saved VM snapshots",
2745 .mhandler
.info
= do_info_snapshots
,
2751 .help
= "show the current VM status (running|paused)",
2752 .mhandler
.info
= hmp_info_status
,
2758 .help
= "show guest PCMCIA status",
2759 .mhandler
.info
= pcmcia_info
,
2765 .help
= "show which guest mouse is receiving events",
2766 .mhandler
.info
= hmp_info_mice
,
2772 .help
= "show the vnc server status",
2773 .mhandler
.info
= hmp_info_vnc
,
2775 #if defined(CONFIG_SPICE)
2780 .help
= "show the spice server status",
2781 .mhandler
.info
= hmp_info_spice
,
2788 .help
= "show the current VM name",
2789 .mhandler
.info
= hmp_info_name
,
2795 .help
= "show the current VM UUID",
2796 .mhandler
.info
= hmp_info_uuid
,
2798 #if defined(TARGET_PPC)
2803 .help
= "show CPU statistics",
2804 .mhandler
.info
= do_info_cpu_stats
,
2807 #if defined(CONFIG_SLIRP)
2812 .help
= "show user network stack connection states",
2813 .mhandler
.info
= do_info_usernet
,
2820 .help
= "show migration status",
2821 .mhandler
.info
= hmp_info_migrate
,
2824 .name
= "migrate_capabilities",
2827 .help
= "show current migration capabilities",
2828 .mhandler
.info
= hmp_info_migrate_capabilities
,
2831 .name
= "migrate_cache_size",
2834 .help
= "show current migration xbzrle cache size",
2835 .mhandler
.info
= hmp_info_migrate_cache_size
,
2841 .help
= "show balloon information",
2842 .mhandler
.info
= hmp_info_balloon
,
2848 .help
= "show device tree",
2849 .mhandler
.info
= do_info_qtree
,
2855 .help
= "show qdev device model list",
2856 .mhandler
.info
= do_info_qdm
,
2862 .help
= "show roms",
2863 .mhandler
.info
= do_info_roms
,
2866 .name
= "trace-events",
2869 .help
= "show available trace-events & their state",
2870 .mhandler
.info
= do_trace_print_events
,
2877 static const mon_cmd_t qmp_cmds
[] = {
2878 #include "qmp-commands-old.h"
2882 /*******************************************************************/
2884 static const char *pch
;
2885 static jmp_buf expr_env
;
2890 typedef struct MonitorDef
{
2893 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2897 #if defined(TARGET_I386)
2898 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2900 CPUArchState
*env
= mon_get_cpu();
2901 return env
->eip
+ env
->segs
[R_CS
].base
;
2905 #if defined(TARGET_PPC)
2906 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2908 CPUArchState
*env
= mon_get_cpu();
2913 for (i
= 0; i
< 8; i
++)
2914 u
|= env
->crf
[i
] << (32 - (4 * i
));
2919 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2921 CPUArchState
*env
= mon_get_cpu();
2925 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2927 CPUArchState
*env
= mon_get_cpu();
2931 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2933 CPUArchState
*env
= mon_get_cpu();
2934 return cpu_ppc_load_decr(env
);
2937 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2939 CPUArchState
*env
= mon_get_cpu();
2940 return cpu_ppc_load_tbu(env
);
2943 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2945 CPUArchState
*env
= mon_get_cpu();
2946 return cpu_ppc_load_tbl(env
);
2950 #if defined(TARGET_SPARC)
2951 #ifndef TARGET_SPARC64
2952 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2954 CPUArchState
*env
= mon_get_cpu();
2956 return cpu_get_psr(env
);
2960 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2962 CPUArchState
*env
= mon_get_cpu();
2963 return env
->regwptr
[val
];
2967 static const MonitorDef monitor_defs
[] = {
2970 #define SEG(name, seg) \
2971 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2972 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2973 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2975 { "eax", offsetof(CPUX86State
, regs
[0]) },
2976 { "ecx", offsetof(CPUX86State
, regs
[1]) },
2977 { "edx", offsetof(CPUX86State
, regs
[2]) },
2978 { "ebx", offsetof(CPUX86State
, regs
[3]) },
2979 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
2980 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
2981 { "esi", offsetof(CPUX86State
, regs
[6]) },
2982 { "edi", offsetof(CPUX86State
, regs
[7]) },
2983 #ifdef TARGET_X86_64
2984 { "r8", offsetof(CPUX86State
, regs
[8]) },
2985 { "r9", offsetof(CPUX86State
, regs
[9]) },
2986 { "r10", offsetof(CPUX86State
, regs
[10]) },
2987 { "r11", offsetof(CPUX86State
, regs
[11]) },
2988 { "r12", offsetof(CPUX86State
, regs
[12]) },
2989 { "r13", offsetof(CPUX86State
, regs
[13]) },
2990 { "r14", offsetof(CPUX86State
, regs
[14]) },
2991 { "r15", offsetof(CPUX86State
, regs
[15]) },
2993 { "eflags", offsetof(CPUX86State
, eflags
) },
2994 { "eip", offsetof(CPUX86State
, eip
) },
3001 { "pc", 0, monitor_get_pc
, },
3002 #elif defined(TARGET_PPC)
3003 /* General purpose registers */
3004 { "r0", offsetof(CPUPPCState
, gpr
[0]) },
3005 { "r1", offsetof(CPUPPCState
, gpr
[1]) },
3006 { "r2", offsetof(CPUPPCState
, gpr
[2]) },
3007 { "r3", offsetof(CPUPPCState
, gpr
[3]) },
3008 { "r4", offsetof(CPUPPCState
, gpr
[4]) },
3009 { "r5", offsetof(CPUPPCState
, gpr
[5]) },
3010 { "r6", offsetof(CPUPPCState
, gpr
[6]) },
3011 { "r7", offsetof(CPUPPCState
, gpr
[7]) },
3012 { "r8", offsetof(CPUPPCState
, gpr
[8]) },
3013 { "r9", offsetof(CPUPPCState
, gpr
[9]) },
3014 { "r10", offsetof(CPUPPCState
, gpr
[10]) },
3015 { "r11", offsetof(CPUPPCState
, gpr
[11]) },
3016 { "r12", offsetof(CPUPPCState
, gpr
[12]) },
3017 { "r13", offsetof(CPUPPCState
, gpr
[13]) },
3018 { "r14", offsetof(CPUPPCState
, gpr
[14]) },
3019 { "r15", offsetof(CPUPPCState
, gpr
[15]) },
3020 { "r16", offsetof(CPUPPCState
, gpr
[16]) },
3021 { "r17", offsetof(CPUPPCState
, gpr
[17]) },
3022 { "r18", offsetof(CPUPPCState
, gpr
[18]) },
3023 { "r19", offsetof(CPUPPCState
, gpr
[19]) },
3024 { "r20", offsetof(CPUPPCState
, gpr
[20]) },
3025 { "r21", offsetof(CPUPPCState
, gpr
[21]) },
3026 { "r22", offsetof(CPUPPCState
, gpr
[22]) },
3027 { "r23", offsetof(CPUPPCState
, gpr
[23]) },
3028 { "r24", offsetof(CPUPPCState
, gpr
[24]) },
3029 { "r25", offsetof(CPUPPCState
, gpr
[25]) },
3030 { "r26", offsetof(CPUPPCState
, gpr
[26]) },
3031 { "r27", offsetof(CPUPPCState
, gpr
[27]) },
3032 { "r28", offsetof(CPUPPCState
, gpr
[28]) },
3033 { "r29", offsetof(CPUPPCState
, gpr
[29]) },
3034 { "r30", offsetof(CPUPPCState
, gpr
[30]) },
3035 { "r31", offsetof(CPUPPCState
, gpr
[31]) },
3036 /* Floating point registers */
3037 { "f0", offsetof(CPUPPCState
, fpr
[0]) },
3038 { "f1", offsetof(CPUPPCState
, fpr
[1]) },
3039 { "f2", offsetof(CPUPPCState
, fpr
[2]) },
3040 { "f3", offsetof(CPUPPCState
, fpr
[3]) },
3041 { "f4", offsetof(CPUPPCState
, fpr
[4]) },
3042 { "f5", offsetof(CPUPPCState
, fpr
[5]) },
3043 { "f6", offsetof(CPUPPCState
, fpr
[6]) },
3044 { "f7", offsetof(CPUPPCState
, fpr
[7]) },
3045 { "f8", offsetof(CPUPPCState
, fpr
[8]) },
3046 { "f9", offsetof(CPUPPCState
, fpr
[9]) },
3047 { "f10", offsetof(CPUPPCState
, fpr
[10]) },
3048 { "f11", offsetof(CPUPPCState
, fpr
[11]) },
3049 { "f12", offsetof(CPUPPCState
, fpr
[12]) },
3050 { "f13", offsetof(CPUPPCState
, fpr
[13]) },
3051 { "f14", offsetof(CPUPPCState
, fpr
[14]) },
3052 { "f15", offsetof(CPUPPCState
, fpr
[15]) },
3053 { "f16", offsetof(CPUPPCState
, fpr
[16]) },
3054 { "f17", offsetof(CPUPPCState
, fpr
[17]) },
3055 { "f18", offsetof(CPUPPCState
, fpr
[18]) },
3056 { "f19", offsetof(CPUPPCState
, fpr
[19]) },
3057 { "f20", offsetof(CPUPPCState
, fpr
[20]) },
3058 { "f21", offsetof(CPUPPCState
, fpr
[21]) },
3059 { "f22", offsetof(CPUPPCState
, fpr
[22]) },
3060 { "f23", offsetof(CPUPPCState
, fpr
[23]) },
3061 { "f24", offsetof(CPUPPCState
, fpr
[24]) },
3062 { "f25", offsetof(CPUPPCState
, fpr
[25]) },
3063 { "f26", offsetof(CPUPPCState
, fpr
[26]) },
3064 { "f27", offsetof(CPUPPCState
, fpr
[27]) },
3065 { "f28", offsetof(CPUPPCState
, fpr
[28]) },
3066 { "f29", offsetof(CPUPPCState
, fpr
[29]) },
3067 { "f30", offsetof(CPUPPCState
, fpr
[30]) },
3068 { "f31", offsetof(CPUPPCState
, fpr
[31]) },
3069 { "fpscr", offsetof(CPUPPCState
, fpscr
) },
3070 /* Next instruction pointer */
3071 { "nip|pc", offsetof(CPUPPCState
, nip
) },
3072 { "lr", offsetof(CPUPPCState
, lr
) },
3073 { "ctr", offsetof(CPUPPCState
, ctr
) },
3074 { "decr", 0, &monitor_get_decr
, },
3075 { "ccr", 0, &monitor_get_ccr
, },
3076 /* Machine state register */
3077 { "msr", 0, &monitor_get_msr
, },
3078 { "xer", 0, &monitor_get_xer
, },
3079 { "tbu", 0, &monitor_get_tbu
, },
3080 { "tbl", 0, &monitor_get_tbl
, },
3081 #if defined(TARGET_PPC64)
3082 /* Address space register */
3083 { "asr", offsetof(CPUPPCState
, asr
) },
3085 /* Segment registers */
3086 { "sdr1", offsetof(CPUPPCState
, spr
[SPR_SDR1
]) },
3087 { "sr0", offsetof(CPUPPCState
, sr
[0]) },
3088 { "sr1", offsetof(CPUPPCState
, sr
[1]) },
3089 { "sr2", offsetof(CPUPPCState
, sr
[2]) },
3090 { "sr3", offsetof(CPUPPCState
, sr
[3]) },
3091 { "sr4", offsetof(CPUPPCState
, sr
[4]) },
3092 { "sr5", offsetof(CPUPPCState
, sr
[5]) },
3093 { "sr6", offsetof(CPUPPCState
, sr
[6]) },
3094 { "sr7", offsetof(CPUPPCState
, sr
[7]) },
3095 { "sr8", offsetof(CPUPPCState
, sr
[8]) },
3096 { "sr9", offsetof(CPUPPCState
, sr
[9]) },
3097 { "sr10", offsetof(CPUPPCState
, sr
[10]) },
3098 { "sr11", offsetof(CPUPPCState
, sr
[11]) },
3099 { "sr12", offsetof(CPUPPCState
, sr
[12]) },
3100 { "sr13", offsetof(CPUPPCState
, sr
[13]) },
3101 { "sr14", offsetof(CPUPPCState
, sr
[14]) },
3102 { "sr15", offsetof(CPUPPCState
, sr
[15]) },
3103 /* Too lazy to put BATs... */
3104 { "pvr", offsetof(CPUPPCState
, spr
[SPR_PVR
]) },
3106 { "srr0", offsetof(CPUPPCState
, spr
[SPR_SRR0
]) },
3107 { "srr1", offsetof(CPUPPCState
, spr
[SPR_SRR1
]) },
3108 { "sprg0", offsetof(CPUPPCState
, spr
[SPR_SPRG0
]) },
3109 { "sprg1", offsetof(CPUPPCState
, spr
[SPR_SPRG1
]) },
3110 { "sprg2", offsetof(CPUPPCState
, spr
[SPR_SPRG2
]) },
3111 { "sprg3", offsetof(CPUPPCState
, spr
[SPR_SPRG3
]) },
3112 { "sprg4", offsetof(CPUPPCState
, spr
[SPR_SPRG4
]) },
3113 { "sprg5", offsetof(CPUPPCState
, spr
[SPR_SPRG5
]) },
3114 { "sprg6", offsetof(CPUPPCState
, spr
[SPR_SPRG6
]) },
3115 { "sprg7", offsetof(CPUPPCState
, spr
[SPR_SPRG7
]) },
3116 { "pid", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID
]) },
3117 { "csrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR0
]) },
3118 { "csrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR1
]) },
3119 { "esr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_ESR
]) },
3120 { "dear", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DEAR
]) },
3121 { "mcsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSR
]) },
3122 { "tsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TSR
]) },
3123 { "tcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TCR
]) },
3124 { "vrsave", offsetof(CPUPPCState
, spr
[SPR_VRSAVE
]) },
3125 { "pir", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PIR
]) },
3126 { "mcsrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR0
]) },
3127 { "mcsrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR1
]) },
3128 { "decar", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DECAR
]) },
3129 { "ivpr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVPR
]) },
3130 { "epcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPCR
]) },
3131 { "sprg8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_SPRG8
]) },
3132 { "ivor0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR0
]) },
3133 { "ivor1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR1
]) },
3134 { "ivor2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR2
]) },
3135 { "ivor3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR3
]) },
3136 { "ivor4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR4
]) },
3137 { "ivor5", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR5
]) },
3138 { "ivor6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR6
]) },
3139 { "ivor7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR7
]) },
3140 { "ivor8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR8
]) },
3141 { "ivor9", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR9
]) },
3142 { "ivor10", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR10
]) },
3143 { "ivor11", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR11
]) },
3144 { "ivor12", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR12
]) },
3145 { "ivor13", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR13
]) },
3146 { "ivor14", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR14
]) },
3147 { "ivor15", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR15
]) },
3148 { "ivor32", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR32
]) },
3149 { "ivor33", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR33
]) },
3150 { "ivor34", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR34
]) },
3151 { "ivor35", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR35
]) },
3152 { "ivor36", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR36
]) },
3153 { "ivor37", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR37
]) },
3154 { "mas0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS0
]) },
3155 { "mas1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS1
]) },
3156 { "mas2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS2
]) },
3157 { "mas3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS3
]) },
3158 { "mas4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS4
]) },
3159 { "mas6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS6
]) },
3160 { "mas7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS7
]) },
3161 { "mmucfg", offsetof(CPUPPCState
, spr
[SPR_MMUCFG
]) },
3162 { "tlb0cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3163 { "tlb1cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3164 { "epr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPR
]) },
3165 { "eplc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPLC
]) },
3166 { "epsc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPSC
]) },
3167 { "svr", offsetof(CPUPPCState
, spr
[SPR_E500_SVR
]) },
3168 { "mcar", offsetof(CPUPPCState
, spr
[SPR_Exxx_MCAR
]) },
3169 { "pid1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID1
]) },
3170 { "pid2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID2
]) },
3171 { "hid0", offsetof(CPUPPCState
, spr
[SPR_HID0
]) },
3173 #elif defined(TARGET_SPARC)
3174 { "g0", offsetof(CPUSPARCState
, gregs
[0]) },
3175 { "g1", offsetof(CPUSPARCState
, gregs
[1]) },
3176 { "g2", offsetof(CPUSPARCState
, gregs
[2]) },
3177 { "g3", offsetof(CPUSPARCState
, gregs
[3]) },
3178 { "g4", offsetof(CPUSPARCState
, gregs
[4]) },
3179 { "g5", offsetof(CPUSPARCState
, gregs
[5]) },
3180 { "g6", offsetof(CPUSPARCState
, gregs
[6]) },
3181 { "g7", offsetof(CPUSPARCState
, gregs
[7]) },
3182 { "o0", 0, monitor_get_reg
},
3183 { "o1", 1, monitor_get_reg
},
3184 { "o2", 2, monitor_get_reg
},
3185 { "o3", 3, monitor_get_reg
},
3186 { "o4", 4, monitor_get_reg
},
3187 { "o5", 5, monitor_get_reg
},
3188 { "o6", 6, monitor_get_reg
},
3189 { "o7", 7, monitor_get_reg
},
3190 { "l0", 8, monitor_get_reg
},
3191 { "l1", 9, monitor_get_reg
},
3192 { "l2", 10, monitor_get_reg
},
3193 { "l3", 11, monitor_get_reg
},
3194 { "l4", 12, monitor_get_reg
},
3195 { "l5", 13, monitor_get_reg
},
3196 { "l6", 14, monitor_get_reg
},
3197 { "l7", 15, monitor_get_reg
},
3198 { "i0", 16, monitor_get_reg
},
3199 { "i1", 17, monitor_get_reg
},
3200 { "i2", 18, monitor_get_reg
},
3201 { "i3", 19, monitor_get_reg
},
3202 { "i4", 20, monitor_get_reg
},
3203 { "i5", 21, monitor_get_reg
},
3204 { "i6", 22, monitor_get_reg
},
3205 { "i7", 23, monitor_get_reg
},
3206 { "pc", offsetof(CPUSPARCState
, pc
) },
3207 { "npc", offsetof(CPUSPARCState
, npc
) },
3208 { "y", offsetof(CPUSPARCState
, y
) },
3209 #ifndef TARGET_SPARC64
3210 { "psr", 0, &monitor_get_psr
, },
3211 { "wim", offsetof(CPUSPARCState
, wim
) },
3213 { "tbr", offsetof(CPUSPARCState
, tbr
) },
3214 { "fsr", offsetof(CPUSPARCState
, fsr
) },
3215 { "f0", offsetof(CPUSPARCState
, fpr
[0].l
.upper
) },
3216 { "f1", offsetof(CPUSPARCState
, fpr
[0].l
.lower
) },
3217 { "f2", offsetof(CPUSPARCState
, fpr
[1].l
.upper
) },
3218 { "f3", offsetof(CPUSPARCState
, fpr
[1].l
.lower
) },
3219 { "f4", offsetof(CPUSPARCState
, fpr
[2].l
.upper
) },
3220 { "f5", offsetof(CPUSPARCState
, fpr
[2].l
.lower
) },
3221 { "f6", offsetof(CPUSPARCState
, fpr
[3].l
.upper
) },
3222 { "f7", offsetof(CPUSPARCState
, fpr
[3].l
.lower
) },
3223 { "f8", offsetof(CPUSPARCState
, fpr
[4].l
.upper
) },
3224 { "f9", offsetof(CPUSPARCState
, fpr
[4].l
.lower
) },
3225 { "f10", offsetof(CPUSPARCState
, fpr
[5].l
.upper
) },
3226 { "f11", offsetof(CPUSPARCState
, fpr
[5].l
.lower
) },
3227 { "f12", offsetof(CPUSPARCState
, fpr
[6].l
.upper
) },
3228 { "f13", offsetof(CPUSPARCState
, fpr
[6].l
.lower
) },
3229 { "f14", offsetof(CPUSPARCState
, fpr
[7].l
.upper
) },
3230 { "f15", offsetof(CPUSPARCState
, fpr
[7].l
.lower
) },
3231 { "f16", offsetof(CPUSPARCState
, fpr
[8].l
.upper
) },
3232 { "f17", offsetof(CPUSPARCState
, fpr
[8].l
.lower
) },
3233 { "f18", offsetof(CPUSPARCState
, fpr
[9].l
.upper
) },
3234 { "f19", offsetof(CPUSPARCState
, fpr
[9].l
.lower
) },
3235 { "f20", offsetof(CPUSPARCState
, fpr
[10].l
.upper
) },
3236 { "f21", offsetof(CPUSPARCState
, fpr
[10].l
.lower
) },
3237 { "f22", offsetof(CPUSPARCState
, fpr
[11].l
.upper
) },
3238 { "f23", offsetof(CPUSPARCState
, fpr
[11].l
.lower
) },
3239 { "f24", offsetof(CPUSPARCState
, fpr
[12].l
.upper
) },
3240 { "f25", offsetof(CPUSPARCState
, fpr
[12].l
.lower
) },
3241 { "f26", offsetof(CPUSPARCState
, fpr
[13].l
.upper
) },
3242 { "f27", offsetof(CPUSPARCState
, fpr
[13].l
.lower
) },
3243 { "f28", offsetof(CPUSPARCState
, fpr
[14].l
.upper
) },
3244 { "f29", offsetof(CPUSPARCState
, fpr
[14].l
.lower
) },
3245 { "f30", offsetof(CPUSPARCState
, fpr
[15].l
.upper
) },
3246 { "f31", offsetof(CPUSPARCState
, fpr
[15].l
.lower
) },
3247 #ifdef TARGET_SPARC64
3248 { "f32", offsetof(CPUSPARCState
, fpr
[16]) },
3249 { "f34", offsetof(CPUSPARCState
, fpr
[17]) },
3250 { "f36", offsetof(CPUSPARCState
, fpr
[18]) },
3251 { "f38", offsetof(CPUSPARCState
, fpr
[19]) },
3252 { "f40", offsetof(CPUSPARCState
, fpr
[20]) },
3253 { "f42", offsetof(CPUSPARCState
, fpr
[21]) },
3254 { "f44", offsetof(CPUSPARCState
, fpr
[22]) },
3255 { "f46", offsetof(CPUSPARCState
, fpr
[23]) },
3256 { "f48", offsetof(CPUSPARCState
, fpr
[24]) },
3257 { "f50", offsetof(CPUSPARCState
, fpr
[25]) },
3258 { "f52", offsetof(CPUSPARCState
, fpr
[26]) },
3259 { "f54", offsetof(CPUSPARCState
, fpr
[27]) },
3260 { "f56", offsetof(CPUSPARCState
, fpr
[28]) },
3261 { "f58", offsetof(CPUSPARCState
, fpr
[29]) },
3262 { "f60", offsetof(CPUSPARCState
, fpr
[30]) },
3263 { "f62", offsetof(CPUSPARCState
, fpr
[31]) },
3264 { "asi", offsetof(CPUSPARCState
, asi
) },
3265 { "pstate", offsetof(CPUSPARCState
, pstate
) },
3266 { "cansave", offsetof(CPUSPARCState
, cansave
) },
3267 { "canrestore", offsetof(CPUSPARCState
, canrestore
) },
3268 { "otherwin", offsetof(CPUSPARCState
, otherwin
) },
3269 { "wstate", offsetof(CPUSPARCState
, wstate
) },
3270 { "cleanwin", offsetof(CPUSPARCState
, cleanwin
) },
3271 { "fprs", offsetof(CPUSPARCState
, fprs
) },
3277 static void expr_error(Monitor
*mon
, const char *msg
)
3279 monitor_printf(mon
, "%s\n", msg
);
3280 longjmp(expr_env
, 1);
3283 /* return 0 if OK, -1 if not found */
3284 static int get_monitor_def(target_long
*pval
, const char *name
)
3286 const MonitorDef
*md
;
3289 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3290 if (compare_cmd(name
, md
->name
)) {
3291 if (md
->get_value
) {
3292 *pval
= md
->get_value(md
, md
->offset
);
3294 CPUArchState
*env
= mon_get_cpu();
3295 ptr
= (uint8_t *)env
+ md
->offset
;
3298 *pval
= *(int32_t *)ptr
;
3301 *pval
= *(target_long
*)ptr
;
3314 static void next(void)
3318 while (qemu_isspace(*pch
))
3323 static int64_t expr_sum(Monitor
*mon
);
3325 static int64_t expr_unary(Monitor
*mon
)
3334 n
= expr_unary(mon
);
3338 n
= -expr_unary(mon
);
3342 n
= ~expr_unary(mon
);
3348 expr_error(mon
, "')' expected");
3355 expr_error(mon
, "character constant expected");
3359 expr_error(mon
, "missing terminating \' character");
3369 while ((*pch
>= 'a' && *pch
<= 'z') ||
3370 (*pch
>= 'A' && *pch
<= 'Z') ||
3371 (*pch
>= '0' && *pch
<= '9') ||
3372 *pch
== '_' || *pch
== '.') {
3373 if ((q
- buf
) < sizeof(buf
) - 1)
3377 while (qemu_isspace(*pch
))
3380 ret
= get_monitor_def(®
, buf
);
3382 expr_error(mon
, "unknown register");
3387 expr_error(mon
, "unexpected end of expression");
3392 #if TARGET_PHYS_ADDR_BITS > 32
3393 n
= strtoull(pch
, &p
, 0);
3395 n
= strtoul(pch
, &p
, 0);
3397 if (errno
== ERANGE
) {
3398 expr_error(mon
, "number too large");
3401 expr_error(mon
, "invalid char in expression");
3404 while (qemu_isspace(*pch
))
3412 static int64_t expr_prod(Monitor
*mon
)
3417 val
= expr_unary(mon
);
3420 if (op
!= '*' && op
!= '/' && op
!= '%')
3423 val2
= expr_unary(mon
);
3432 expr_error(mon
, "division by zero");
3443 static int64_t expr_logic(Monitor
*mon
)
3448 val
= expr_prod(mon
);
3451 if (op
!= '&' && op
!= '|' && op
!= '^')
3454 val2
= expr_prod(mon
);
3471 static int64_t expr_sum(Monitor
*mon
)
3476 val
= expr_logic(mon
);
3479 if (op
!= '+' && op
!= '-')
3482 val2
= expr_logic(mon
);
3491 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3494 if (setjmp(expr_env
)) {
3498 while (qemu_isspace(*pch
))
3500 *pval
= expr_sum(mon
);
3505 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3507 const char *p
= *pp
;
3511 d
= strtod(p
, &tailp
);
3513 monitor_printf(mon
, "Number expected\n");
3516 if (d
!= d
|| d
- d
!= 0) {
3517 /* NaN or infinity */
3518 monitor_printf(mon
, "Bad number\n");
3526 static int get_str(char *buf
, int buf_size
, const char **pp
)
3534 while (qemu_isspace(*p
))
3544 while (*p
!= '\0' && *p
!= '\"') {
3560 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3563 if ((q
- buf
) < buf_size
- 1) {
3567 if ((q
- buf
) < buf_size
- 1) {
3574 qemu_printf("unterminated string\n");
3579 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3580 if ((q
- buf
) < buf_size
- 1) {
3592 * Store the command-name in cmdname, and return a pointer to
3593 * the remaining of the command string.
3595 static const char *get_command_name(const char *cmdline
,
3596 char *cmdname
, size_t nlen
)
3599 const char *p
, *pstart
;
3602 while (qemu_isspace(*p
))
3607 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3612 memcpy(cmdname
, pstart
, len
);
3613 cmdname
[len
] = '\0';
3618 * Read key of 'type' into 'key' and return the current
3621 static char *key_get_info(const char *type
, char **key
)
3629 p
= strchr(type
, ':');
3636 str
= g_malloc(len
+ 1);
3637 memcpy(str
, type
, len
);
3644 static int default_fmt_format
= 'x';
3645 static int default_fmt_size
= 4;
3649 static int is_valid_option(const char *c
, const char *typestr
)
3657 typestr
= strstr(typestr
, option
);
3658 return (typestr
!= NULL
);
3661 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3662 const char *cmdname
)
3664 const mon_cmd_t
*cmd
;
3666 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3667 if (compare_cmd(cmdname
, cmd
->name
)) {
3675 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3677 return search_dispatch_table(mon_cmds
, cmdname
);
3680 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3682 return search_dispatch_table(qmp_cmds
, cmdname
);
3685 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3686 const char *cmdline
,
3689 const char *p
, *typestr
;
3691 const mon_cmd_t
*cmd
;
3697 monitor_printf(mon
, "command='%s'\n", cmdline
);
3700 /* extract the command name */
3701 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3705 cmd
= monitor_find_command(cmdname
);
3707 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3711 /* parse the parameters */
3712 typestr
= cmd
->args_type
;
3714 typestr
= key_get_info(typestr
, &key
);
3726 while (qemu_isspace(*p
))
3728 if (*typestr
== '?') {
3731 /* no optional string: NULL argument */
3735 ret
= get_str(buf
, sizeof(buf
), &p
);
3739 monitor_printf(mon
, "%s: filename expected\n",
3743 monitor_printf(mon
, "%s: block device name expected\n",
3747 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3752 qdict_put(qdict
, key
, qstring_from_str(buf
));
3757 QemuOptsList
*opts_list
;
3760 opts_list
= qemu_find_opts(key
);
3761 if (!opts_list
|| opts_list
->desc
->name
) {
3764 while (qemu_isspace(*p
)) {
3769 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3772 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3776 qemu_opts_to_qdict(opts
, qdict
);
3777 qemu_opts_del(opts
);
3782 int count
, format
, size
;
3784 while (qemu_isspace(*p
))
3790 if (qemu_isdigit(*p
)) {
3792 while (qemu_isdigit(*p
)) {
3793 count
= count
* 10 + (*p
- '0');
3831 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3832 monitor_printf(mon
, "invalid char in format: '%c'\n",
3837 format
= default_fmt_format
;
3838 if (format
!= 'i') {
3839 /* for 'i', not specifying a size gives -1 as size */
3841 size
= default_fmt_size
;
3842 default_fmt_size
= size
;
3844 default_fmt_format
= format
;
3847 format
= default_fmt_format
;
3848 if (format
!= 'i') {
3849 size
= default_fmt_size
;
3854 qdict_put(qdict
, "count", qint_from_int(count
));
3855 qdict_put(qdict
, "format", qint_from_int(format
));
3856 qdict_put(qdict
, "size", qint_from_int(size
));
3865 while (qemu_isspace(*p
))
3867 if (*typestr
== '?' || *typestr
== '.') {
3868 if (*typestr
== '?') {
3876 while (qemu_isspace(*p
))
3885 if (get_expr(mon
, &val
, &p
))
3887 /* Check if 'i' is greater than 32-bit */
3888 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3889 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3890 monitor_printf(mon
, "integer is for 32-bit values\n");
3892 } else if (c
== 'M') {
3894 monitor_printf(mon
, "enter a positive value\n");
3899 qdict_put(qdict
, key
, qint_from_int(val
));
3907 while (qemu_isspace(*p
)) {
3910 if (*typestr
== '?') {
3916 val
= strtosz(p
, &end
);
3918 monitor_printf(mon
, "invalid size\n");
3921 qdict_put(qdict
, key
, qint_from_int(val
));
3929 while (qemu_isspace(*p
))
3931 if (*typestr
== '?') {
3937 if (get_double(mon
, &val
, &p
) < 0) {
3940 if (p
[0] && p
[1] == 's') {
3943 val
/= 1e3
; p
+= 2; break;
3945 val
/= 1e6
; p
+= 2; break;
3947 val
/= 1e9
; p
+= 2; break;
3950 if (*p
&& !qemu_isspace(*p
)) {
3951 monitor_printf(mon
, "Unknown unit suffix\n");
3954 qdict_put(qdict
, key
, qfloat_from_double(val
));
3962 while (qemu_isspace(*p
)) {
3966 while (qemu_isgraph(*p
)) {
3969 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3971 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3974 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3977 qdict_put(qdict
, key
, qbool_from_int(val
));
3982 const char *tmp
= p
;
3989 while (qemu_isspace(*p
))
3994 if(!is_valid_option(p
, typestr
)) {
3996 monitor_printf(mon
, "%s: unsupported option -%c\n",
4008 qdict_put(qdict
, key
, qbool_from_int(1));
4015 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4021 /* check that all arguments were parsed */
4022 while (qemu_isspace(*p
))
4025 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4037 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4039 /* report only the first error */
4041 mon
->error
= qerror
;
4047 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4049 if (ret
&& !monitor_has_error(mon
)) {
4051 * If it returns failure, it must have passed on error.
4053 * Action: Report an internal error to the client if in QMP.
4055 qerror_report(QERR_UNDEFINED_ERROR
);
4059 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4062 const mon_cmd_t
*cmd
;
4064 qdict
= qdict_new();
4066 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4070 if (handler_is_async(cmd
)) {
4071 user_async_cmd_handler(mon
, cmd
, qdict
);
4072 } else if (handler_is_qobject(cmd
)) {
4073 QObject
*data
= NULL
;
4075 /* XXX: ignores the error code */
4076 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4077 assert(!monitor_has_error(mon
));
4079 cmd
->user_print(mon
, data
);
4080 qobject_decref(data
);
4083 cmd
->mhandler
.cmd(mon
, qdict
);
4090 static void cmd_completion(const char *name
, const char *list
)
4092 const char *p
, *pstart
;
4101 p
= pstart
+ strlen(pstart
);
4103 if (len
> sizeof(cmd
) - 2)
4104 len
= sizeof(cmd
) - 2;
4105 memcpy(cmd
, pstart
, len
);
4107 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4108 readline_add_completion(cur_mon
->rs
, cmd
);
4116 static void file_completion(const char *input
)
4121 char file
[1024], file_prefix
[1024];
4125 p
= strrchr(input
, '/');
4128 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4129 pstrcpy(path
, sizeof(path
), ".");
4131 input_path_len
= p
- input
+ 1;
4132 memcpy(path
, input
, input_path_len
);
4133 if (input_path_len
> sizeof(path
) - 1)
4134 input_path_len
= sizeof(path
) - 1;
4135 path
[input_path_len
] = '\0';
4136 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4138 #ifdef DEBUG_COMPLETION
4139 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4140 input
, path
, file_prefix
);
4142 ffs
= opendir(path
);
4151 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4155 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4156 memcpy(file
, input
, input_path_len
);
4157 if (input_path_len
< sizeof(file
))
4158 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4160 /* stat the file to find out if it's a directory.
4161 * In that case add a slash to speed up typing long paths
4163 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
4164 pstrcat(file
, sizeof(file
), "/");
4166 readline_add_completion(cur_mon
->rs
, file
);
4172 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4174 const char *name
= bdrv_get_device_name(bs
);
4175 const char *input
= opaque
;
4177 if (input
[0] == '\0' ||
4178 !strncmp(name
, (char *)input
, strlen(input
))) {
4179 readline_add_completion(cur_mon
->rs
, name
);
4183 /* NOTE: this parser is an approximate form of the real command parser */
4184 static void parse_cmdline(const char *cmdline
,
4185 int *pnb_args
, char **args
)
4194 while (qemu_isspace(*p
))
4198 if (nb_args
>= MAX_ARGS
)
4200 ret
= get_str(buf
, sizeof(buf
), &p
);
4201 args
[nb_args
] = g_strdup(buf
);
4206 *pnb_args
= nb_args
;
4209 static const char *next_arg_type(const char *typestr
)
4211 const char *p
= strchr(typestr
, ':');
4212 return (p
!= NULL
? ++p
: typestr
);
4215 static void monitor_find_completion(const char *cmdline
)
4217 const char *cmdname
;
4218 char *args
[MAX_ARGS
];
4219 int nb_args
, i
, len
;
4220 const char *ptype
, *str
;
4221 const mon_cmd_t
*cmd
;
4224 parse_cmdline(cmdline
, &nb_args
, args
);
4225 #ifdef DEBUG_COMPLETION
4226 for(i
= 0; i
< nb_args
; i
++) {
4227 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4231 /* if the line ends with a space, it means we want to complete the
4233 len
= strlen(cmdline
);
4234 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4235 if (nb_args
>= MAX_ARGS
) {
4238 args
[nb_args
++] = g_strdup("");
4241 /* command completion */
4246 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4247 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4248 cmd_completion(cmdname
, cmd
->name
);
4251 /* find the command */
4252 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4253 if (compare_cmd(args
[0], cmd
->name
)) {
4261 ptype
= next_arg_type(cmd
->args_type
);
4262 for(i
= 0; i
< nb_args
- 2; i
++) {
4263 if (*ptype
!= '\0') {
4264 ptype
= next_arg_type(ptype
);
4265 while (*ptype
== '?')
4266 ptype
= next_arg_type(ptype
);
4269 str
= args
[nb_args
- 1];
4270 if (*ptype
== '-' && ptype
[1] != '\0') {
4271 ptype
= next_arg_type(ptype
);
4275 /* file completion */
4276 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4277 file_completion(str
);
4280 /* block device name completion */
4281 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4282 bdrv_iterate(block_completion_it
, (void *)str
);
4285 /* XXX: more generic ? */
4286 if (!strcmp(cmd
->name
, "info")) {
4287 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4288 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4289 cmd_completion(str
, cmd
->name
);
4291 } else if (!strcmp(cmd
->name
, "sendkey")) {
4292 char *sep
= strrchr(str
, '-');
4295 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4296 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4297 cmd_completion(str
, key
->name
);
4299 } else if (!strcmp(cmd
->name
, "help|?")) {
4300 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4301 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4302 cmd_completion(str
, cmd
->name
);
4312 for (i
= 0; i
< nb_args
; i
++) {
4317 static int monitor_can_read(void *opaque
)
4319 Monitor
*mon
= opaque
;
4321 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4324 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4326 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4327 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4331 * Argument validation rules:
4333 * 1. The argument must exist in cmd_args qdict
4334 * 2. The argument type must be the expected one
4336 * Special case: If the argument doesn't exist in cmd_args and
4337 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4338 * checking is skipped for it.
4340 static int check_client_args_type(const QDict
*client_args
,
4341 const QDict
*cmd_args
, int flags
)
4343 const QDictEntry
*ent
;
4345 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4348 const QObject
*client_arg
= qdict_entry_value(ent
);
4349 const char *client_arg_name
= qdict_entry_key(ent
);
4351 obj
= qdict_get(cmd_args
, client_arg_name
);
4353 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4354 /* handler accepts unknowns */
4357 /* client arg doesn't exist */
4358 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4362 arg_type
= qobject_to_qstring(obj
);
4363 assert(arg_type
!= NULL
);
4365 /* check if argument's type is correct */
4366 switch (qstring_get_str(arg_type
)[0]) {
4370 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4371 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4380 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4381 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4387 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4388 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4389 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4396 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4397 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4403 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4406 /* Any QObject can be passed. */
4411 * These types are not supported by QMP and thus are not
4412 * handled here. Fall through.
4423 * - Check if the client has passed all mandatory args
4424 * - Set special flags for argument validation
4426 static int check_mandatory_args(const QDict
*cmd_args
,
4427 const QDict
*client_args
, int *flags
)
4429 const QDictEntry
*ent
;
4431 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4432 const char *cmd_arg_name
= qdict_entry_key(ent
);
4433 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4434 assert(type
!= NULL
);
4436 if (qstring_get_str(type
)[0] == 'O') {
4437 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4438 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4439 } else if (qstring_get_str(type
)[0] != '-' &&
4440 qstring_get_str(type
)[1] != '?' &&
4441 !qdict_haskey(client_args
, cmd_arg_name
)) {
4442 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4450 static QDict
*qdict_from_args_type(const char *args_type
)
4454 QString
*key
, *type
, *cur_qs
;
4456 assert(args_type
!= NULL
);
4458 qdict
= qdict_new();
4460 if (args_type
== NULL
|| args_type
[0] == '\0') {
4461 /* no args, empty qdict */
4465 key
= qstring_new();
4466 type
= qstring_new();
4471 switch (args_type
[i
]) {
4474 qdict_put(qdict
, qstring_get_str(key
), type
);
4476 if (args_type
[i
] == '\0') {
4479 type
= qstring_new(); /* qdict has ref */
4480 cur_qs
= key
= qstring_new();
4486 qstring_append_chr(cur_qs
, args_type
[i
]);
4496 * Client argument checking rules:
4498 * 1. Client must provide all mandatory arguments
4499 * 2. Each argument provided by the client must be expected
4500 * 3. Each argument provided by the client must have the type expected
4503 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4508 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4511 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4516 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4524 * Input object checking rules
4526 * 1. Input object must be a dict
4527 * 2. The "execute" key must exist
4528 * 3. The "execute" key must be a string
4529 * 4. If the "arguments" key exists, it must be a dict
4530 * 5. If the "id" key exists, it can be anything (ie. json-value)
4531 * 6. Any argument not listed above is considered invalid
4533 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4535 const QDictEntry
*ent
;
4536 int has_exec_key
= 0;
4539 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4540 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4544 input_dict
= qobject_to_qdict(input_obj
);
4546 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4547 const char *arg_name
= qdict_entry_key(ent
);
4548 const QObject
*arg_obj
= qdict_entry_value(ent
);
4550 if (!strcmp(arg_name
, "execute")) {
4551 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4552 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4557 } else if (!strcmp(arg_name
, "arguments")) {
4558 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4559 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4563 } else if (!strcmp(arg_name
, "id")) {
4564 /* FIXME: check duplicated IDs for async commands */
4566 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4571 if (!has_exec_key
) {
4572 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4579 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4580 const QDict
*params
)
4583 QObject
*data
= NULL
;
4585 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4586 handler_audit(mon
, cmd
, ret
);
4587 monitor_protocol_emitter(mon
, data
);
4588 qobject_decref(data
);
4591 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4595 QDict
*input
, *args
;
4596 const mon_cmd_t
*cmd
;
4597 const char *cmd_name
;
4598 Monitor
*mon
= cur_mon
;
4600 args
= input
= NULL
;
4602 obj
= json_parser_parse(tokens
, NULL
);
4604 // FIXME: should be triggered in json_parser_parse()
4605 qerror_report(QERR_JSON_PARSING
);
4609 input
= qmp_check_input_obj(obj
);
4611 qobject_decref(obj
);
4615 mon
->mc
->id
= qdict_get(input
, "id");
4616 qobject_incref(mon
->mc
->id
);
4618 cmd_name
= qdict_get_str(input
, "execute");
4619 trace_handle_qmp_command(mon
, cmd_name
);
4620 if (invalid_qmp_mode(mon
, cmd_name
)) {
4621 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4625 cmd
= qmp_find_cmd(cmd_name
);
4627 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4631 obj
= qdict_get(input
, "arguments");
4635 args
= qobject_to_qdict(obj
);
4639 err
= qmp_check_client_args(cmd
, args
);
4644 if (handler_is_async(cmd
)) {
4645 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4647 /* emit the error response */
4651 qmp_call_cmd(mon
, cmd
, args
);
4657 monitor_protocol_emitter(mon
, NULL
);
4664 * monitor_control_read(): Read and handle QMP input
4666 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4668 Monitor
*old_mon
= cur_mon
;
4672 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4677 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4679 Monitor
*old_mon
= cur_mon
;
4685 for (i
= 0; i
< size
; i
++)
4686 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4688 if (size
== 0 || buf
[size
- 1] != 0)
4689 monitor_printf(cur_mon
, "corrupted command\n");
4691 handle_user_command(cur_mon
, (char *)buf
);
4697 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4699 monitor_suspend(mon
);
4700 handle_user_command(mon
, cmdline
);
4701 monitor_resume(mon
);
4704 int monitor_suspend(Monitor
*mon
)
4712 void monitor_resume(Monitor
*mon
)
4716 if (--mon
->suspend_cnt
== 0)
4717 readline_show_prompt(mon
->rs
);
4720 static QObject
*get_qmp_greeting(void)
4722 QObject
*ver
= NULL
;
4724 qmp_marshal_input_query_version(NULL
, NULL
, &ver
);
4725 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4729 * monitor_control_event(): Print QMP gretting
4731 static void monitor_control_event(void *opaque
, int event
)
4734 Monitor
*mon
= opaque
;
4737 case CHR_EVENT_OPENED
:
4738 mon
->mc
->command_mode
= 0;
4739 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4740 data
= get_qmp_greeting();
4741 monitor_json_emitter(mon
, data
);
4742 qobject_decref(data
);
4744 case CHR_EVENT_CLOSED
:
4745 json_message_parser_destroy(&mon
->mc
->parser
);
4750 static void monitor_event(void *opaque
, int event
)
4752 Monitor
*mon
= opaque
;
4755 case CHR_EVENT_MUX_IN
:
4757 if (mon
->reset_seen
) {
4758 readline_restart(mon
->rs
);
4759 monitor_resume(mon
);
4762 mon
->suspend_cnt
= 0;
4766 case CHR_EVENT_MUX_OUT
:
4767 if (mon
->reset_seen
) {
4768 if (mon
->suspend_cnt
== 0) {
4769 monitor_printf(mon
, "\n");
4772 monitor_suspend(mon
);
4779 case CHR_EVENT_OPENED
:
4780 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4781 "information\n", QEMU_VERSION
);
4782 if (!mon
->mux_out
) {
4783 readline_show_prompt(mon
->rs
);
4785 mon
->reset_seen
= 1;
4791 compare_mon_cmd(const void *a
, const void *b
)
4793 return strcmp(((const mon_cmd_t
*)a
)->name
,
4794 ((const mon_cmd_t
*)b
)->name
);
4797 static void sortcmdlist(void)
4800 int elem_size
= sizeof(mon_cmd_t
);
4802 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4803 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4805 array_num
= sizeof(info_cmds
)/elem_size
-1;
4806 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4818 void monitor_init(CharDriverState
*chr
, int flags
)
4820 static int is_first_init
= 1;
4823 if (is_first_init
) {
4824 key_timer
= qemu_new_timer_ns(vm_clock
, release_keys
, NULL
);
4825 monitor_protocol_event_init();
4829 mon
= g_malloc0(sizeof(*mon
));
4833 if (flags
& MONITOR_USE_READLINE
) {
4834 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4835 monitor_read_command(mon
, 0);
4838 if (monitor_ctrl_mode(mon
)) {
4839 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
4840 /* Control mode requires special handlers */
4841 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4842 monitor_control_event
, mon
);
4843 qemu_chr_fe_set_echo(chr
, true);
4845 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4846 monitor_event
, mon
);
4849 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4850 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
4856 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
4858 BlockDriverState
*bs
= opaque
;
4861 if (bdrv_set_key(bs
, password
) != 0) {
4862 monitor_printf(mon
, "invalid password\n");
4865 if (mon
->password_completion_cb
)
4866 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4868 monitor_read_command(mon
, 1);
4871 ReadLineState
*monitor_get_rs(Monitor
*mon
)
4876 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4877 BlockDriverCompletionFunc
*completion_cb
,
4882 if (!bdrv_key_required(bs
)) {
4884 completion_cb(opaque
, 0);
4888 if (monitor_ctrl_mode(mon
)) {
4889 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
4890 bdrv_get_encrypted_filename(bs
));
4894 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4895 bdrv_get_encrypted_filename(bs
));
4897 mon
->password_completion_cb
= completion_cb
;
4898 mon
->password_opaque
= opaque
;
4900 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4902 if (err
&& completion_cb
)
4903 completion_cb(opaque
, err
);
4908 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4909 BlockDriverCompletionFunc
*completion_cb
,
4912 BlockDriverState
*bs
;
4914 bs
= bdrv_find(device
);
4916 monitor_printf(mon
, "Device not found %s\n", device
);
4920 return monitor_read_bdrv_key_start(mon
, bs
, completion_cb
, opaque
);