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_HEAD(, MonFdsetFd
) dup_fds
;
158 QLIST_ENTRY(MonFdset
) next
;
161 typedef struct MonitorControl
{
163 JSONMessageParser parser
;
168 * To prevent flooding clients, events can be throttled. The
169 * throttling is calculated globally, rather than per-Monitor
172 typedef struct MonitorEventState
{
173 MonitorEvent event
; /* Event being tracked */
174 int64_t rate
; /* Period over which to throttle. 0 to disable */
175 int64_t last
; /* Time at which event was last emitted */
176 QEMUTimer
*timer
; /* Timer for handling delayed events */
177 QObject
*data
; /* Event pending delayed dispatch */
181 CharDriverState
*chr
;
186 uint8_t outbuf
[1024];
190 CPUArchState
*mon_cpu
;
191 BlockDriverCompletionFunc
*password_completion_cb
;
192 void *password_opaque
;
194 QLIST_HEAD(,mon_fd_t
) fds
;
195 QLIST_ENTRY(Monitor
) entry
;
198 /* QMP checker flags */
199 #define QMP_ACCEPT_UNKNOWNS 1
201 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
202 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
203 static int mon_refcount
;
205 static mon_cmd_t mon_cmds
[];
206 static mon_cmd_t info_cmds
[];
208 static const mon_cmd_t qmp_cmds
[];
211 Monitor
*default_mon
;
213 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
216 static inline int qmp_cmd_mode(const Monitor
*mon
)
218 return (mon
->mc
? mon
->mc
->command_mode
: 0);
221 /* Return true if in control mode, false otherwise */
222 static inline int monitor_ctrl_mode(const Monitor
*mon
)
224 return (mon
->flags
& MONITOR_USE_CONTROL
);
227 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
228 int monitor_cur_is_qmp(void)
230 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
233 void monitor_read_command(Monitor
*mon
, int show_prompt
)
238 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
240 readline_show_prompt(mon
->rs
);
243 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
246 if (monitor_ctrl_mode(mon
)) {
247 qerror_report(QERR_MISSING_PARAMETER
, "password");
249 } else if (mon
->rs
) {
250 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
251 /* prompt is printed on return from the command handler */
254 monitor_printf(mon
, "terminal does not support password prompting\n");
259 void monitor_flush(Monitor
*mon
)
261 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
262 qemu_chr_fe_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
263 mon
->outbuf_index
= 0;
267 /* flush at every end of line or if the buffer is full */
268 static void monitor_puts(Monitor
*mon
, const char *str
)
277 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
278 mon
->outbuf
[mon
->outbuf_index
++] = c
;
279 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
285 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
292 if (monitor_ctrl_mode(mon
)) {
296 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
297 monitor_puts(mon
, buf
);
300 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
304 monitor_vprintf(mon
, fmt
, ap
);
308 void monitor_print_filename(Monitor
*mon
, const char *filename
)
312 for (i
= 0; filename
[i
]; i
++) {
313 switch (filename
[i
]) {
317 monitor_printf(mon
, "\\%c", filename
[i
]);
320 monitor_printf(mon
, "\\t");
323 monitor_printf(mon
, "\\r");
326 monitor_printf(mon
, "\\n");
329 monitor_printf(mon
, "%c", filename
[i
]);
335 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
336 const char *fmt
, ...)
340 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
345 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
347 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
349 return cmd
->user_print
!= NULL
;
352 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
354 return cmd
->flags
& MONITOR_CMD_ASYNC
;
357 static inline int monitor_has_error(const Monitor
*mon
)
359 return mon
->error
!= NULL
;
362 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
366 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
367 qobject_to_json(data
);
368 assert(json
!= NULL
);
370 qstring_append_chr(json
, '\n');
371 monitor_puts(mon
, qstring_get_str(json
));
376 static QDict
*build_qmp_error_dict(const QError
*err
)
380 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
381 ErrorClass_lookup
[err
->err_class
],
384 return qobject_to_qdict(obj
);
387 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
391 trace_monitor_protocol_emitter(mon
);
393 if (!monitor_has_error(mon
)) {
394 /* success response */
397 qobject_incref(data
);
398 qdict_put_obj(qmp
, "return", data
);
400 /* return an empty QDict by default */
401 qdict_put(qmp
, "return", qdict_new());
405 qmp
= build_qmp_error_dict(mon
->error
);
411 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
415 monitor_json_emitter(mon
, QOBJECT(qmp
));
419 static void timestamp_put(QDict
*qdict
)
425 err
= qemu_gettimeofday(&tv
);
429 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
430 "'microseconds': %" PRId64
" }",
431 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
432 qdict_put_obj(qdict
, "timestamp", obj
);
436 static const char *monitor_event_names
[] = {
437 [QEVENT_SHUTDOWN
] = "SHUTDOWN",
438 [QEVENT_RESET
] = "RESET",
439 [QEVENT_POWERDOWN
] = "POWERDOWN",
440 [QEVENT_STOP
] = "STOP",
441 [QEVENT_RESUME
] = "RESUME",
442 [QEVENT_VNC_CONNECTED
] = "VNC_CONNECTED",
443 [QEVENT_VNC_INITIALIZED
] = "VNC_INITIALIZED",
444 [QEVENT_VNC_DISCONNECTED
] = "VNC_DISCONNECTED",
445 [QEVENT_BLOCK_IO_ERROR
] = "BLOCK_IO_ERROR",
446 [QEVENT_RTC_CHANGE
] = "RTC_CHANGE",
447 [QEVENT_WATCHDOG
] = "WATCHDOG",
448 [QEVENT_SPICE_CONNECTED
] = "SPICE_CONNECTED",
449 [QEVENT_SPICE_INITIALIZED
] = "SPICE_INITIALIZED",
450 [QEVENT_SPICE_DISCONNECTED
] = "SPICE_DISCONNECTED",
451 [QEVENT_BLOCK_JOB_COMPLETED
] = "BLOCK_JOB_COMPLETED",
452 [QEVENT_BLOCK_JOB_CANCELLED
] = "BLOCK_JOB_CANCELLED",
453 [QEVENT_DEVICE_TRAY_MOVED
] = "DEVICE_TRAY_MOVED",
454 [QEVENT_SUSPEND
] = "SUSPEND",
455 [QEVENT_SUSPEND_DISK
] = "SUSPEND_DISK",
456 [QEVENT_WAKEUP
] = "WAKEUP",
457 [QEVENT_BALLOON_CHANGE
] = "BALLOON_CHANGE",
458 [QEVENT_SPICE_MIGRATE_COMPLETED
] = "SPICE_MIGRATE_COMPLETED",
460 QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names
) != QEVENT_MAX
)
462 MonitorEventState monitor_event_state
[QEVENT_MAX
];
463 QemuMutex monitor_event_state_lock
;
466 * Emits the event to every monitor instance
469 monitor_protocol_event_emit(MonitorEvent event
,
474 trace_monitor_protocol_event_emit(event
, data
);
475 QLIST_FOREACH(mon
, &mon_list
, entry
) {
476 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
477 monitor_json_emitter(mon
, data
);
484 * Queue a new event for emission to Monitor instances,
485 * applying any rate limiting if required.
488 monitor_protocol_event_queue(MonitorEvent event
,
491 MonitorEventState
*evstate
;
492 int64_t now
= qemu_get_clock_ns(rt_clock
);
493 assert(event
< QEVENT_MAX
);
495 qemu_mutex_lock(&monitor_event_state_lock
);
496 evstate
= &(monitor_event_state
[event
]);
497 trace_monitor_protocol_event_queue(event
,
503 /* Rate limit of 0 indicates no throttling */
504 if (!evstate
->rate
) {
505 monitor_protocol_event_emit(event
, data
);
508 int64_t delta
= now
- evstate
->last
;
510 delta
< evstate
->rate
) {
511 /* If there's an existing event pending, replace
512 * it with the new event, otherwise schedule a
513 * timer for delayed emission
516 qobject_decref(evstate
->data
);
518 int64_t then
= evstate
->last
+ evstate
->rate
;
519 qemu_mod_timer_ns(evstate
->timer
, then
);
521 evstate
->data
= data
;
522 qobject_incref(evstate
->data
);
524 monitor_protocol_event_emit(event
, data
);
528 qemu_mutex_unlock(&monitor_event_state_lock
);
533 * The callback invoked by QemuTimer when a delayed
534 * event is ready to be emitted
536 static void monitor_protocol_event_handler(void *opaque
)
538 MonitorEventState
*evstate
= opaque
;
539 int64_t now
= qemu_get_clock_ns(rt_clock
);
541 qemu_mutex_lock(&monitor_event_state_lock
);
543 trace_monitor_protocol_event_handler(evstate
->event
,
548 monitor_protocol_event_emit(evstate
->event
, evstate
->data
);
549 qobject_decref(evstate
->data
);
550 evstate
->data
= NULL
;
553 qemu_mutex_unlock(&monitor_event_state_lock
);
558 * @event: the event ID to be limited
559 * @rate: the rate limit in milliseconds
561 * Sets a rate limit on a particular event, so no
562 * more than 1 event will be emitted within @rate
566 monitor_protocol_event_throttle(MonitorEvent event
,
569 MonitorEventState
*evstate
;
570 assert(event
< QEVENT_MAX
);
572 evstate
= &(monitor_event_state
[event
]);
574 trace_monitor_protocol_event_throttle(event
, rate
);
575 evstate
->event
= event
;
576 evstate
->rate
= rate
* SCALE_MS
;
577 evstate
->timer
= qemu_new_timer(rt_clock
,
579 monitor_protocol_event_handler
,
582 evstate
->data
= NULL
;
586 /* Global, one-time initializer to configure the rate limiting
587 * and initialize state */
588 static void monitor_protocol_event_init(void)
590 qemu_mutex_init(&monitor_event_state_lock
);
591 /* Limit RTC & BALLOON events to 1 per second */
592 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE
, 1000);
593 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE
, 1000);
594 monitor_protocol_event_throttle(QEVENT_WATCHDOG
, 1000);
598 * monitor_protocol_event(): Generate a Monitor event
600 * Event-specific data can be emitted through the (optional) 'data' parameter.
602 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
605 const char *event_name
;
607 assert(event
< QEVENT_MAX
);
609 event_name
= monitor_event_names
[event
];
610 assert(event_name
!= NULL
);
614 qdict_put(qmp
, "event", qstring_from_str(event_name
));
616 qobject_incref(data
);
617 qdict_put_obj(qmp
, "data", data
);
620 trace_monitor_protocol_event(event
, event_name
, qmp
);
621 monitor_protocol_event_queue(event
, QOBJECT(qmp
));
625 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
628 /* Will setup QMP capabilities in the future */
629 if (monitor_ctrl_mode(mon
)) {
630 mon
->mc
->command_mode
= 1;
636 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
638 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
639 int64_t cpu_index
, Error
**errp
)
642 Monitor
*old_mon
, hmp
;
643 CharDriverState mchar
;
645 memset(&hmp
, 0, sizeof(hmp
));
646 qemu_chr_init_mem(&mchar
);
653 int ret
= monitor_set_cpu(cpu_index
);
656 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
662 handle_user_command(&hmp
, command_line
);
665 if (qemu_chr_mem_osize(hmp
.chr
) > 0) {
666 QString
*str
= qemu_chr_mem_to_qs(hmp
.chr
);
667 output
= g_strdup(qstring_get_str(str
));
670 output
= g_strdup("");
674 qemu_chr_close_mem(hmp
.chr
);
678 static int compare_cmd(const char *name
, const char *list
)
680 const char *p
, *pstart
;
688 p
= pstart
+ strlen(pstart
);
689 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
698 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
699 const char *prefix
, const char *name
)
701 const mon_cmd_t
*cmd
;
703 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
704 if (!name
|| !strcmp(name
, cmd
->name
))
705 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
706 cmd
->params
, cmd
->help
);
710 static void help_cmd(Monitor
*mon
, const char *name
)
712 if (name
&& !strcmp(name
, "info")) {
713 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
715 help_cmd_dump(mon
, mon_cmds
, "", name
);
716 if (name
&& !strcmp(name
, "log")) {
717 const CPULogItem
*item
;
718 monitor_printf(mon
, "Log items (comma separated):\n");
719 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
720 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
721 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
727 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
729 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
732 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
734 const char *tp_name
= qdict_get_str(qdict
, "name");
735 bool new_state
= qdict_get_bool(qdict
, "option");
736 int ret
= trace_event_set_state(tp_name
, new_state
);
739 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
743 #ifdef CONFIG_TRACE_SIMPLE
744 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
746 const char *op
= qdict_get_try_str(qdict
, "op");
747 const char *arg
= qdict_get_try_str(qdict
, "arg");
750 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
751 } else if (!strcmp(op
, "on")) {
752 st_set_trace_file_enabled(true);
753 } else if (!strcmp(op
, "off")) {
754 st_set_trace_file_enabled(false);
755 } else if (!strcmp(op
, "flush")) {
756 st_flush_trace_buffer();
757 } else if (!strcmp(op
, "set")) {
759 st_set_trace_file(arg
);
762 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
763 help_cmd(mon
, "trace-file");
768 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
770 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
773 data
->user_print(data
->mon
, ret_data
);
775 monitor_resume(data
->mon
);
779 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
781 monitor_protocol_emitter(opaque
, ret_data
);
784 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
787 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
790 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
795 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
797 cb_data
->user_print
= cmd
->user_print
;
798 monitor_suspend(mon
);
799 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
800 user_monitor_complete
, cb_data
);
807 static void do_info(Monitor
*mon
, const QDict
*qdict
)
809 const mon_cmd_t
*cmd
;
810 const char *item
= qdict_get_try_str(qdict
, "item");
816 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
817 if (compare_cmd(item
, cmd
->name
))
821 if (cmd
->name
== NULL
) {
825 cmd
->mhandler
.info(mon
);
829 help_cmd(mon
, "info");
832 CommandInfoList
*qmp_query_commands(Error
**errp
)
834 CommandInfoList
*info
, *cmd_list
= NULL
;
835 const mon_cmd_t
*cmd
;
837 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
838 info
= g_malloc0(sizeof(*info
));
839 info
->value
= g_malloc0(sizeof(*info
->value
));
840 info
->value
->name
= g_strdup(cmd
->name
);
842 info
->next
= cmd_list
;
849 EventInfoList
*qmp_query_events(Error
**errp
)
851 EventInfoList
*info
, *ev_list
= NULL
;
854 for (e
= 0 ; e
< QEVENT_MAX
; e
++) {
855 const char *event_name
= monitor_event_names
[e
];
856 assert(event_name
!= NULL
);
857 info
= g_malloc0(sizeof(*info
));
858 info
->value
= g_malloc0(sizeof(*info
->value
));
859 info
->value
->name
= g_strdup(event_name
);
861 info
->next
= ev_list
;
868 /* set the current CPU defined by the user */
869 int monitor_set_cpu(int cpu_index
)
873 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
874 if (env
->cpu_index
== cpu_index
) {
875 cur_mon
->mon_cpu
= env
;
882 static CPUArchState
*mon_get_cpu(void)
884 if (!cur_mon
->mon_cpu
) {
887 cpu_synchronize_state(cur_mon
->mon_cpu
);
888 return cur_mon
->mon_cpu
;
891 int monitor_get_cpu_index(void)
893 return mon_get_cpu()->cpu_index
;
896 static void do_info_registers(Monitor
*mon
)
901 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
904 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
909 static void do_info_jit(Monitor
*mon
)
911 dump_exec_info((FILE *)mon
, monitor_fprintf
);
914 static void do_info_history(Monitor
*mon
)
923 str
= readline_get_history(mon
->rs
, i
);
926 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
931 #if defined(TARGET_PPC)
932 /* XXX: not implemented in other targets */
933 static void do_info_cpu_stats(Monitor
*mon
)
938 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
942 static void do_trace_print_events(Monitor
*mon
)
944 trace_print_events((FILE *)mon
, &monitor_fprintf
);
947 static int add_graphics_client(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
949 const char *protocol
= qdict_get_str(qdict
, "protocol");
950 const char *fdname
= qdict_get_str(qdict
, "fdname");
953 if (strcmp(protocol
, "spice") == 0) {
954 int fd
= monitor_get_fd(mon
, fdname
);
955 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
956 int tls
= qdict_get_try_bool(qdict
, "tls", 0);
958 /* correct one? spice isn't a device ,,, */
959 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
962 if (qemu_spice_display_add_client(fd
, skipauth
, tls
) < 0) {
967 } else if (strcmp(protocol
, "vnc") == 0) {
968 int fd
= monitor_get_fd(mon
, fdname
);
969 int skipauth
= qdict_get_try_bool(qdict
, "skipauth", 0);
970 vnc_display_add_client(NULL
, fd
, skipauth
);
973 } else if ((s
= qemu_chr_find(protocol
)) != NULL
) {
974 int fd
= monitor_get_fd(mon
, fdname
);
975 if (qemu_chr_add_client(s
, fd
) < 0) {
976 qerror_report(QERR_ADD_CLIENT_FAILED
);
982 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
986 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
,
987 MonitorCompletion cb
, void *opaque
)
989 const char *protocol
= qdict_get_str(qdict
, "protocol");
990 const char *hostname
= qdict_get_str(qdict
, "hostname");
991 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
992 int port
= qdict_get_try_int(qdict
, "port", -1);
993 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
996 if (strcmp(protocol
, "spice") == 0) {
998 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1002 if (port
== -1 && tls_port
== -1) {
1003 qerror_report(QERR_MISSING_PARAMETER
, "port/tls-port");
1007 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
,
1010 qerror_report(QERR_UNDEFINED_ERROR
);
1016 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1020 static int do_screen_dump(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1022 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
1026 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1028 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
1031 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1034 const char *items
= qdict_get_str(qdict
, "items");
1036 if (!strcmp(items
, "none")) {
1039 mask
= cpu_str_to_log_mask(items
);
1041 help_cmd(mon
, "log");
1048 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1050 const char *option
= qdict_get_try_str(qdict
, "option");
1051 if (!option
|| !strcmp(option
, "on")) {
1053 } else if (!strcmp(option
, "off")) {
1056 monitor_printf(mon
, "unexpected option %s\n", option
);
1060 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1062 const char *device
= qdict_get_try_str(qdict
, "device");
1064 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1065 if (gdbserver_start(device
) < 0) {
1066 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1068 } else if (strcmp(device
, "none") == 0) {
1069 monitor_printf(mon
, "Disabled gdbserver\n");
1071 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1076 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1078 const char *action
= qdict_get_str(qdict
, "action");
1079 if (select_watchdog_action(action
) == -1) {
1080 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1084 static void monitor_printc(Monitor
*mon
, int c
)
1086 monitor_printf(mon
, "'");
1089 monitor_printf(mon
, "\\'");
1092 monitor_printf(mon
, "\\\\");
1095 monitor_printf(mon
, "\\n");
1098 monitor_printf(mon
, "\\r");
1101 if (c
>= 32 && c
<= 126) {
1102 monitor_printf(mon
, "%c", c
);
1104 monitor_printf(mon
, "\\x%02x", c
);
1108 monitor_printf(mon
, "'");
1111 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1112 target_phys_addr_t addr
, int is_physical
)
1115 int l
, line_size
, i
, max_digits
, len
;
1119 if (format
== 'i') {
1122 env
= mon_get_cpu();
1126 } else if (wsize
== 4) {
1129 /* as default we use the current CS size */
1132 #ifdef TARGET_X86_64
1133 if ((env
->efer
& MSR_EFER_LMA
) &&
1134 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1138 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1143 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1147 len
= wsize
* count
;
1156 max_digits
= (wsize
* 8 + 2) / 3;
1160 max_digits
= (wsize
* 8) / 4;
1164 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1173 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1175 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1180 cpu_physical_memory_read(addr
, buf
, l
);
1182 env
= mon_get_cpu();
1183 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
1184 monitor_printf(mon
, " Cannot access memory\n");
1193 v
= ldub_raw(buf
+ i
);
1196 v
= lduw_raw(buf
+ i
);
1199 v
= (uint32_t)ldl_raw(buf
+ i
);
1202 v
= ldq_raw(buf
+ i
);
1205 monitor_printf(mon
, " ");
1208 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1211 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1214 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1217 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1220 monitor_printc(mon
, v
);
1225 monitor_printf(mon
, "\n");
1231 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1233 int count
= qdict_get_int(qdict
, "count");
1234 int format
= qdict_get_int(qdict
, "format");
1235 int size
= qdict_get_int(qdict
, "size");
1236 target_long addr
= qdict_get_int(qdict
, "addr");
1238 memory_dump(mon
, count
, format
, size
, addr
, 0);
1241 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1243 int count
= qdict_get_int(qdict
, "count");
1244 int format
= qdict_get_int(qdict
, "format");
1245 int size
= qdict_get_int(qdict
, "size");
1246 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1248 memory_dump(mon
, count
, format
, size
, addr
, 1);
1251 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1253 int format
= qdict_get_int(qdict
, "format");
1254 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1258 monitor_printf(mon
, "%#" TARGET_PRIoPHYS
, val
);
1261 monitor_printf(mon
, "%#" TARGET_PRIxPHYS
, val
);
1264 monitor_printf(mon
, "%" TARGET_PRIuPHYS
, val
);
1268 monitor_printf(mon
, "%" TARGET_PRIdPHYS
, val
);
1271 monitor_printc(mon
, val
);
1274 monitor_printf(mon
, "\n");
1277 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1281 uint32_t start
= qdict_get_int(qdict
, "start");
1282 uint32_t size
= qdict_get_int(qdict
, "size");
1285 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1286 uint8_t val
= ldub_phys(addr
);
1287 /* BSD sum algorithm ('sum' Unix command) */
1288 sum
= (sum
>> 1) | (sum
<< 15);
1291 monitor_printf(mon
, "%05d\n", sum
);
1299 static const KeyDef key_defs
[] = {
1301 { 0x36, "shift_r" },
1306 { 0xe4, "altgr_r" },
1326 { 0x0e, "backspace" },
1339 { 0x1a, "bracket_left" },
1340 { 0x1b, "bracket_right" },
1352 { 0x27, "semicolon" },
1353 { 0x28, "apostrophe" },
1354 { 0x29, "grave_accent" },
1356 { 0x2b, "backslash" },
1368 { 0x37, "asterisk" },
1371 { 0x3a, "caps_lock" },
1382 { 0x45, "num_lock" },
1383 { 0x46, "scroll_lock" },
1385 { 0xb5, "kp_divide" },
1386 { 0x37, "kp_multiply" },
1387 { 0x4a, "kp_subtract" },
1389 { 0x9c, "kp_enter" },
1390 { 0x53, "kp_decimal" },
1423 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1438 { 0xfe, "compose" },
1443 static int get_keycode(const char *key
)
1449 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1450 if (!strcmp(key
, p
->name
))
1453 if (strstart(key
, "0x", NULL
)) {
1454 ret
= strtoul(key
, &endp
, 0);
1455 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1461 #define MAX_KEYCODES 16
1462 static uint8_t keycodes
[MAX_KEYCODES
];
1463 static int nb_pending_keycodes
;
1464 static QEMUTimer
*key_timer
;
1466 static void release_keys(void *opaque
)
1470 while (nb_pending_keycodes
> 0) {
1471 nb_pending_keycodes
--;
1472 keycode
= keycodes
[nb_pending_keycodes
];
1474 kbd_put_keycode(0xe0);
1475 kbd_put_keycode(keycode
| 0x80);
1479 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1481 char keyname_buf
[16];
1483 int keyname_len
, keycode
, i
;
1484 const char *string
= qdict_get_str(qdict
, "string");
1485 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1486 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1488 if (nb_pending_keycodes
> 0) {
1489 qemu_del_timer(key_timer
);
1496 separator
= strchr(string
, '-');
1497 keyname_len
= separator
? separator
- string
: strlen(string
);
1498 if (keyname_len
> 0) {
1499 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1500 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1501 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1504 if (i
== MAX_KEYCODES
) {
1505 monitor_printf(mon
, "too many keys\n");
1508 keyname_buf
[keyname_len
] = 0;
1509 keycode
= get_keycode(keyname_buf
);
1511 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1514 keycodes
[i
++] = keycode
;
1518 string
= separator
+ 1;
1520 nb_pending_keycodes
= i
;
1521 /* key down events */
1522 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1523 keycode
= keycodes
[i
];
1525 kbd_put_keycode(0xe0);
1526 kbd_put_keycode(keycode
& 0x7f);
1528 /* delayed key up events */
1529 qemu_mod_timer(key_timer
, qemu_get_clock_ns(vm_clock
) +
1530 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1533 static int mouse_button_state
;
1535 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1538 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1539 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1540 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1541 dx
= strtol(dx_str
, NULL
, 0);
1542 dy
= strtol(dy_str
, NULL
, 0);
1545 dz
= strtol(dz_str
, NULL
, 0);
1546 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1549 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1551 int button_state
= qdict_get_int(qdict
, "button_state");
1552 mouse_button_state
= button_state
;
1553 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1556 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1558 int size
= qdict_get_int(qdict
, "size");
1559 int addr
= qdict_get_int(qdict
, "addr");
1560 int has_index
= qdict_haskey(qdict
, "index");
1565 int index
= qdict_get_int(qdict
, "index");
1566 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1574 val
= cpu_inb(addr
);
1578 val
= cpu_inw(addr
);
1582 val
= cpu_inl(addr
);
1586 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1587 suffix
, addr
, size
* 2, val
);
1590 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1592 int size
= qdict_get_int(qdict
, "size");
1593 int addr
= qdict_get_int(qdict
, "addr");
1594 int val
= qdict_get_int(qdict
, "val");
1596 addr
&= IOPORTS_MASK
;
1601 cpu_outb(addr
, val
);
1604 cpu_outw(addr
, val
);
1607 cpu_outl(addr
, val
);
1612 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1615 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1617 res
= qemu_boot_set(bootdevice
);
1619 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1620 } else if (res
> 0) {
1621 monitor_printf(mon
, "setting boot device list failed\n");
1623 monitor_printf(mon
, "no function defined to set boot device list for "
1624 "this architecture\n");
1628 #if defined(TARGET_I386)
1629 static void print_pte(Monitor
*mon
, target_phys_addr_t addr
,
1630 target_phys_addr_t pte
,
1631 target_phys_addr_t mask
)
1633 #ifdef TARGET_X86_64
1634 if (addr
& (1ULL << 47)) {
1638 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1639 " %c%c%c%c%c%c%c%c%c\n",
1642 pte
& PG_NX_MASK
? 'X' : '-',
1643 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1644 pte
& PG_PSE_MASK
? 'P' : '-',
1645 pte
& PG_DIRTY_MASK
? 'D' : '-',
1646 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1647 pte
& PG_PCD_MASK
? 'C' : '-',
1648 pte
& PG_PWT_MASK
? 'T' : '-',
1649 pte
& PG_USER_MASK
? 'U' : '-',
1650 pte
& PG_RW_MASK
? 'W' : '-');
1653 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
1655 unsigned int l1
, l2
;
1656 uint32_t pgd
, pde
, pte
;
1658 pgd
= env
->cr
[3] & ~0xfff;
1659 for(l1
= 0; l1
< 1024; l1
++) {
1660 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1661 pde
= le32_to_cpu(pde
);
1662 if (pde
& PG_PRESENT_MASK
) {
1663 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1665 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1667 for(l2
= 0; l2
< 1024; l2
++) {
1668 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1669 pte
= le32_to_cpu(pte
);
1670 if (pte
& PG_PRESENT_MASK
) {
1671 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1681 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1683 unsigned int l1
, l2
, l3
;
1684 uint64_t pdpe
, pde
, pte
;
1685 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1687 pdp_addr
= env
->cr
[3] & ~0x1f;
1688 for (l1
= 0; l1
< 4; l1
++) {
1689 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1690 pdpe
= le64_to_cpu(pdpe
);
1691 if (pdpe
& PG_PRESENT_MASK
) {
1692 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1693 for (l2
= 0; l2
< 512; l2
++) {
1694 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1695 pde
= le64_to_cpu(pde
);
1696 if (pde
& PG_PRESENT_MASK
) {
1697 if (pde
& PG_PSE_MASK
) {
1698 /* 2M pages with PAE, CR4.PSE is ignored */
1699 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1700 ~((target_phys_addr_t
)(1 << 20) - 1));
1702 pt_addr
= pde
& 0x3fffffffff000ULL
;
1703 for (l3
= 0; l3
< 512; l3
++) {
1704 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1705 pte
= le64_to_cpu(pte
);
1706 if (pte
& PG_PRESENT_MASK
) {
1707 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1710 ~(target_phys_addr_t
)0xfff);
1720 #ifdef TARGET_X86_64
1721 static void tlb_info_64(Monitor
*mon
, CPUArchState
*env
)
1723 uint64_t l1
, l2
, l3
, l4
;
1724 uint64_t pml4e
, pdpe
, pde
, pte
;
1725 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1727 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1728 for (l1
= 0; l1
< 512; l1
++) {
1729 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1730 pml4e
= le64_to_cpu(pml4e
);
1731 if (pml4e
& PG_PRESENT_MASK
) {
1732 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1733 for (l2
= 0; l2
< 512; l2
++) {
1734 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1735 pdpe
= le64_to_cpu(pdpe
);
1736 if (pdpe
& PG_PRESENT_MASK
) {
1737 if (pdpe
& PG_PSE_MASK
) {
1738 /* 1G pages, CR4.PSE is ignored */
1739 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1740 0x3ffffc0000000ULL
);
1742 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1743 for (l3
= 0; l3
< 512; l3
++) {
1744 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1745 pde
= le64_to_cpu(pde
);
1746 if (pde
& PG_PRESENT_MASK
) {
1747 if (pde
& PG_PSE_MASK
) {
1748 /* 2M pages, CR4.PSE is ignored */
1749 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1751 0x3ffffffe00000ULL
);
1753 pt_addr
= pde
& 0x3fffffffff000ULL
;
1754 for (l4
= 0; l4
< 512; l4
++) {
1755 cpu_physical_memory_read(pt_addr
1758 pte
= le64_to_cpu(pte
);
1759 if (pte
& PG_PRESENT_MASK
) {
1760 print_pte(mon
, (l1
<< 39) +
1762 (l3
<< 21) + (l4
<< 12),
1764 0x3fffffffff000ULL
);
1778 static void tlb_info(Monitor
*mon
)
1782 env
= mon_get_cpu();
1784 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1785 monitor_printf(mon
, "PG disabled\n");
1788 if (env
->cr
[4] & CR4_PAE_MASK
) {
1789 #ifdef TARGET_X86_64
1790 if (env
->hflags
& HF_LMA_MASK
) {
1791 tlb_info_64(mon
, env
);
1795 tlb_info_pae32(mon
, env
);
1798 tlb_info_32(mon
, env
);
1802 static void mem_print(Monitor
*mon
, target_phys_addr_t
*pstart
,
1804 target_phys_addr_t end
, int prot
)
1807 prot1
= *plast_prot
;
1808 if (prot
!= prot1
) {
1809 if (*pstart
!= -1) {
1810 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
1811 TARGET_FMT_plx
" %c%c%c\n",
1812 *pstart
, end
, end
- *pstart
,
1813 prot1
& PG_USER_MASK
? 'u' : '-',
1815 prot1
& PG_RW_MASK
? 'w' : '-');
1825 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
1827 unsigned int l1
, l2
;
1828 int prot
, last_prot
;
1829 uint32_t pgd
, pde
, pte
;
1830 target_phys_addr_t start
, end
;
1832 pgd
= env
->cr
[3] & ~0xfff;
1835 for(l1
= 0; l1
< 1024; l1
++) {
1836 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1837 pde
= le32_to_cpu(pde
);
1839 if (pde
& PG_PRESENT_MASK
) {
1840 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1841 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1842 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1844 for(l2
= 0; l2
< 1024; l2
++) {
1845 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1846 pte
= le32_to_cpu(pte
);
1847 end
= (l1
<< 22) + (l2
<< 12);
1848 if (pte
& PG_PRESENT_MASK
) {
1850 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1854 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1859 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1862 /* Flush last range */
1863 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
1866 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1868 unsigned int l1
, l2
, l3
;
1869 int prot
, last_prot
;
1870 uint64_t pdpe
, pde
, pte
;
1871 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1872 target_phys_addr_t start
, end
;
1874 pdp_addr
= env
->cr
[3] & ~0x1f;
1877 for (l1
= 0; l1
< 4; l1
++) {
1878 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1879 pdpe
= le64_to_cpu(pdpe
);
1881 if (pdpe
& PG_PRESENT_MASK
) {
1882 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1883 for (l2
= 0; l2
< 512; l2
++) {
1884 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1885 pde
= le64_to_cpu(pde
);
1886 end
= (l1
<< 30) + (l2
<< 21);
1887 if (pde
& PG_PRESENT_MASK
) {
1888 if (pde
& PG_PSE_MASK
) {
1889 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1891 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1893 pt_addr
= pde
& 0x3fffffffff000ULL
;
1894 for (l3
= 0; l3
< 512; l3
++) {
1895 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1896 pte
= le64_to_cpu(pte
);
1897 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
1898 if (pte
& PG_PRESENT_MASK
) {
1899 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
1904 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1909 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1914 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1917 /* Flush last range */
1918 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 32, 0);
1922 #ifdef TARGET_X86_64
1923 static void mem_info_64(Monitor
*mon
, CPUArchState
*env
)
1925 int prot
, last_prot
;
1926 uint64_t l1
, l2
, l3
, l4
;
1927 uint64_t pml4e
, pdpe
, pde
, pte
;
1928 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
1930 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1933 for (l1
= 0; l1
< 512; l1
++) {
1934 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1935 pml4e
= le64_to_cpu(pml4e
);
1937 if (pml4e
& PG_PRESENT_MASK
) {
1938 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1939 for (l2
= 0; l2
< 512; l2
++) {
1940 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1941 pdpe
= le64_to_cpu(pdpe
);
1942 end
= (l1
<< 39) + (l2
<< 30);
1943 if (pdpe
& PG_PRESENT_MASK
) {
1944 if (pdpe
& PG_PSE_MASK
) {
1945 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
1948 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1950 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1951 for (l3
= 0; l3
< 512; l3
++) {
1952 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1953 pde
= le64_to_cpu(pde
);
1954 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
1955 if (pde
& PG_PRESENT_MASK
) {
1956 if (pde
& PG_PSE_MASK
) {
1957 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1959 prot
&= pml4e
& pdpe
;
1960 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1962 pt_addr
= pde
& 0x3fffffffff000ULL
;
1963 for (l4
= 0; l4
< 512; l4
++) {
1964 cpu_physical_memory_read(pt_addr
1967 pte
= le64_to_cpu(pte
);
1968 end
= (l1
<< 39) + (l2
<< 30) +
1969 (l3
<< 21) + (l4
<< 12);
1970 if (pte
& PG_PRESENT_MASK
) {
1971 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
1973 prot
&= pml4e
& pdpe
& pde
;
1977 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1982 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1988 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1993 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1996 /* Flush last range */
1997 mem_print(mon
, &start
, &last_prot
, (target_phys_addr_t
)1 << 48, 0);
2001 static void mem_info(Monitor
*mon
)
2005 env
= mon_get_cpu();
2007 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
2008 monitor_printf(mon
, "PG disabled\n");
2011 if (env
->cr
[4] & CR4_PAE_MASK
) {
2012 #ifdef TARGET_X86_64
2013 if (env
->hflags
& HF_LMA_MASK
) {
2014 mem_info_64(mon
, env
);
2018 mem_info_pae32(mon
, env
);
2021 mem_info_32(mon
, env
);
2026 #if defined(TARGET_SH4)
2028 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
2030 monitor_printf(mon
, " tlb%i:\t"
2031 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
2032 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
2033 "dirty=%hhu writethrough=%hhu\n",
2035 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
2036 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
2040 static void tlb_info(Monitor
*mon
)
2042 CPUArchState
*env
= mon_get_cpu();
2045 monitor_printf (mon
, "ITLB:\n");
2046 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
2047 print_tlb (mon
, i
, &env
->itlb
[i
]);
2048 monitor_printf (mon
, "UTLB:\n");
2049 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
2050 print_tlb (mon
, i
, &env
->utlb
[i
]);
2055 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
2056 static void tlb_info(Monitor
*mon
)
2058 CPUArchState
*env1
= mon_get_cpu();
2060 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
2064 static void do_info_mtree(Monitor
*mon
)
2066 mtree_info((fprintf_function
)monitor_printf
, mon
);
2069 static void do_info_numa(Monitor
*mon
)
2074 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2075 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2076 monitor_printf(mon
, "node %d cpus:", i
);
2077 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
2078 if (env
->numa_node
== i
) {
2079 monitor_printf(mon
, " %d", env
->cpu_index
);
2082 monitor_printf(mon
, "\n");
2083 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2088 #ifdef CONFIG_PROFILER
2093 static void do_info_profile(Monitor
*mon
)
2099 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2100 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2101 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2102 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2107 static void do_info_profile(Monitor
*mon
)
2109 monitor_printf(mon
, "Internal profiler not compiled\n");
2113 /* Capture support */
2114 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2116 static void do_info_capture(Monitor
*mon
)
2121 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2122 monitor_printf(mon
, "[%d]: ", i
);
2123 s
->ops
.info (s
->opaque
);
2128 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2131 int n
= qdict_get_int(qdict
, "n");
2134 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2136 s
->ops
.destroy (s
->opaque
);
2137 QLIST_REMOVE (s
, entries
);
2144 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2146 const char *path
= qdict_get_str(qdict
, "path");
2147 int has_freq
= qdict_haskey(qdict
, "freq");
2148 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2149 int has_bits
= qdict_haskey(qdict
, "bits");
2150 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2151 int has_channels
= qdict_haskey(qdict
, "nchannels");
2152 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2155 s
= g_malloc0 (sizeof (*s
));
2157 freq
= has_freq
? freq
: 44100;
2158 bits
= has_bits
? bits
: 16;
2159 nchannels
= has_channels
? nchannels
: 2;
2161 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2162 monitor_printf(mon
, "Failed to add wave capture\n");
2166 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2170 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2172 qemu_acl
*acl
= qemu_acl_find(name
);
2175 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2180 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2182 const char *aclname
= qdict_get_str(qdict
, "aclname");
2183 qemu_acl
*acl
= find_acl(mon
, aclname
);
2184 qemu_acl_entry
*entry
;
2188 monitor_printf(mon
, "policy: %s\n",
2189 acl
->defaultDeny
? "deny" : "allow");
2190 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2192 monitor_printf(mon
, "%d: %s %s\n", i
,
2193 entry
->deny
? "deny" : "allow", entry
->match
);
2198 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2200 const char *aclname
= qdict_get_str(qdict
, "aclname");
2201 qemu_acl
*acl
= find_acl(mon
, aclname
);
2204 qemu_acl_reset(acl
);
2205 monitor_printf(mon
, "acl: removed all rules\n");
2209 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2211 const char *aclname
= qdict_get_str(qdict
, "aclname");
2212 const char *policy
= qdict_get_str(qdict
, "policy");
2213 qemu_acl
*acl
= find_acl(mon
, aclname
);
2216 if (strcmp(policy
, "allow") == 0) {
2217 acl
->defaultDeny
= 0;
2218 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2219 } else if (strcmp(policy
, "deny") == 0) {
2220 acl
->defaultDeny
= 1;
2221 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2223 monitor_printf(mon
, "acl: unknown policy '%s', "
2224 "expected 'deny' or 'allow'\n", policy
);
2229 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2231 const char *aclname
= qdict_get_str(qdict
, "aclname");
2232 const char *match
= qdict_get_str(qdict
, "match");
2233 const char *policy
= qdict_get_str(qdict
, "policy");
2234 int has_index
= qdict_haskey(qdict
, "index");
2235 int index
= qdict_get_try_int(qdict
, "index", -1);
2236 qemu_acl
*acl
= find_acl(mon
, aclname
);
2240 if (strcmp(policy
, "allow") == 0) {
2242 } else if (strcmp(policy
, "deny") == 0) {
2245 monitor_printf(mon
, "acl: unknown policy '%s', "
2246 "expected 'deny' or 'allow'\n", policy
);
2250 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2252 ret
= qemu_acl_append(acl
, deny
, match
);
2254 monitor_printf(mon
, "acl: unable to add acl entry\n");
2256 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2260 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2262 const char *aclname
= qdict_get_str(qdict
, "aclname");
2263 const char *match
= qdict_get_str(qdict
, "match");
2264 qemu_acl
*acl
= find_acl(mon
, aclname
);
2268 ret
= qemu_acl_remove(acl
, match
);
2270 monitor_printf(mon
, "acl: no matching acl entry\n");
2272 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2276 #if defined(TARGET_I386)
2277 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2280 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2281 int bank
= qdict_get_int(qdict
, "bank");
2282 uint64_t status
= qdict_get_int(qdict
, "status");
2283 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2284 uint64_t addr
= qdict_get_int(qdict
, "addr");
2285 uint64_t misc
= qdict_get_int(qdict
, "misc");
2286 int flags
= MCE_INJECT_UNCOND_AO
;
2288 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2289 flags
|= MCE_INJECT_BROADCAST
;
2291 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
) {
2292 if (cenv
->cpu_index
== cpu_index
) {
2293 cpu_x86_inject_mce(mon
, cenv
, bank
, status
, mcg_status
, addr
, misc
,
2301 void qmp_getfd(const char *fdname
, Error
**errp
)
2306 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
2308 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2312 if (qemu_isdigit(fdname
[0])) {
2313 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2314 "a name not starting with a digit");
2318 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2319 if (strcmp(monfd
->name
, fdname
) != 0) {
2328 monfd
= g_malloc0(sizeof(mon_fd_t
));
2329 monfd
->name
= g_strdup(fdname
);
2332 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2335 void qmp_closefd(const char *fdname
, Error
**errp
)
2339 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2340 if (strcmp(monfd
->name
, fdname
) != 0) {
2344 QLIST_REMOVE(monfd
, next
);
2346 g_free(monfd
->name
);
2351 error_set(errp
, QERR_FD_NOT_FOUND
, fdname
);
2354 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2356 int saved_vm_running
= runstate_is_running();
2357 const char *name
= qdict_get_str(qdict
, "name");
2359 vm_stop(RUN_STATE_RESTORE_VM
);
2361 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2366 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2370 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2373 if (strcmp(monfd
->name
, fdname
) != 0) {
2379 /* caller takes ownership of fd */
2380 QLIST_REMOVE(monfd
, next
);
2381 g_free(monfd
->name
);
2390 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2392 MonFdsetFd
*mon_fdset_fd
;
2393 MonFdsetFd
*mon_fdset_fd_next
;
2395 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2396 if (mon_fdset_fd
->removed
||
2397 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) {
2398 close(mon_fdset_fd
->fd
);
2399 g_free(mon_fdset_fd
->opaque
);
2400 QLIST_REMOVE(mon_fdset_fd
, next
);
2401 g_free(mon_fdset_fd
);
2405 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2406 QLIST_REMOVE(mon_fdset
, next
);
2411 static void monitor_fdsets_cleanup(void)
2413 MonFdset
*mon_fdset
;
2414 MonFdset
*mon_fdset_next
;
2416 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2417 monitor_fdset_cleanup(mon_fdset
);
2421 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2422 const char *opaque
, Error
**errp
)
2425 Monitor
*mon
= cur_mon
;
2426 MonFdset
*mon_fdset
;
2427 MonFdsetFd
*mon_fdset_fd
;
2430 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2432 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2437 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2438 if (mon_fdset
->id
== fdset_id
) {
2442 if (mon_fdset
== NULL
) {
2443 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2444 "an existing fdset-id");
2448 int64_t fdset_id_prev
= -1;
2449 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2451 /* Use first available fdset ID */
2452 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2453 mon_fdset_cur
= mon_fdset
;
2454 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2455 fdset_id_prev
= mon_fdset_cur
->id
;
2461 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2462 mon_fdset
->id
= fdset_id_prev
+ 1;
2464 /* The fdset list is ordered by fdset ID */
2465 if (mon_fdset
->id
== 0) {
2466 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2467 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2468 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2470 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2474 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2475 mon_fdset_fd
->fd
= fd
;
2476 mon_fdset_fd
->removed
= false;
2478 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2480 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2482 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2483 fdinfo
->fdset_id
= mon_fdset
->id
;
2484 fdinfo
->fd
= mon_fdset_fd
->fd
;
2495 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2497 MonFdset
*mon_fdset
;
2498 MonFdsetFd
*mon_fdset_fd
;
2501 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2502 if (mon_fdset
->id
!= fdset_id
) {
2505 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2507 if (mon_fdset_fd
->fd
!= fd
) {
2510 mon_fdset_fd
->removed
= true;
2513 mon_fdset_fd
->removed
= true;
2516 if (has_fd
&& !mon_fdset_fd
) {
2519 monitor_fdset_cleanup(mon_fdset
);
2525 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2528 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2530 error_set(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2533 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2535 MonFdset
*mon_fdset
;
2536 MonFdsetFd
*mon_fdset_fd
;
2537 FdsetInfoList
*fdset_list
= NULL
;
2539 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2540 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2541 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2543 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2544 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2546 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2547 FdsetFdInfoList
*fdsetfd_info
;
2549 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2550 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2551 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2552 if (mon_fdset_fd
->opaque
) {
2553 fdsetfd_info
->value
->has_opaque
= true;
2554 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2556 fdsetfd_info
->value
->has_opaque
= false;
2559 fdsetfd_info
->next
= fdsetfd_list
;
2560 fdsetfd_list
= fdsetfd_info
;
2563 fdset_info
->value
->fds
= fdsetfd_list
;
2565 fdset_info
->next
= fdset_list
;
2566 fdset_list
= fdset_info
;
2572 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2575 MonFdset
*mon_fdset
;
2576 MonFdsetFd
*mon_fdset_fd
;
2579 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2580 if (mon_fdset
->id
!= fdset_id
) {
2583 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2584 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2585 if (mon_fd_flags
== -1) {
2589 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2590 return mon_fdset_fd
->fd
;
2602 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2604 MonFdset
*mon_fdset
;
2605 MonFdsetFd
*mon_fdset_fd_dup
;
2607 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2608 if (mon_fdset
->id
!= fdset_id
) {
2611 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2612 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2616 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2617 mon_fdset_fd_dup
->fd
= dup_fd
;
2618 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2624 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2626 MonFdset
*mon_fdset
;
2627 MonFdsetFd
*mon_fdset_fd_dup
;
2629 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2630 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2631 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2633 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2634 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2635 monitor_fdset_cleanup(mon_fdset
);
2638 return mon_fdset
->id
;
2645 int monitor_fdset_dup_fd_find(int dup_fd
)
2647 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2650 int monitor_fdset_dup_fd_remove(int dup_fd
)
2652 return monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2655 /* mon_cmds and info_cmds would be sorted at runtime */
2656 static mon_cmd_t mon_cmds
[] = {
2657 #include "hmp-commands.h"
2661 /* Please update hmp-commands.hx when adding or changing commands */
2662 static mon_cmd_t info_cmds
[] = {
2667 .help
= "show the version of QEMU",
2668 .mhandler
.info
= hmp_info_version
,
2674 .help
= "show the network state",
2675 .mhandler
.info
= do_info_network
,
2681 .help
= "show the character devices",
2682 .mhandler
.info
= hmp_info_chardev
,
2688 .help
= "show the block devices",
2689 .mhandler
.info
= hmp_info_block
,
2692 .name
= "blockstats",
2695 .help
= "show block device statistics",
2696 .mhandler
.info
= hmp_info_blockstats
,
2699 .name
= "block-jobs",
2702 .help
= "show progress of ongoing block device operations",
2703 .mhandler
.info
= hmp_info_block_jobs
,
2706 .name
= "registers",
2709 .help
= "show the cpu registers",
2710 .mhandler
.info
= do_info_registers
,
2716 .help
= "show infos for each CPU",
2717 .mhandler
.info
= hmp_info_cpus
,
2723 .help
= "show the command line history",
2724 .mhandler
.info
= do_info_history
,
2726 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2727 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2732 .help
= "show the interrupts statistics (if available)",
2734 .mhandler
.info
= sun4m_irq_info
,
2735 #elif defined(TARGET_LM32)
2736 .mhandler
.info
= lm32_irq_info
,
2738 .mhandler
.info
= irq_info
,
2745 .help
= "show i8259 (PIC) state",
2747 .mhandler
.info
= sun4m_pic_info
,
2748 #elif defined(TARGET_LM32)
2749 .mhandler
.info
= lm32_do_pic_info
,
2751 .mhandler
.info
= pic_info
,
2759 .help
= "show PCI info",
2760 .mhandler
.info
= hmp_info_pci
,
2762 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2763 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2768 .help
= "show virtual to physical memory mappings",
2769 .mhandler
.info
= tlb_info
,
2772 #if defined(TARGET_I386)
2777 .help
= "show the active virtual memory mappings",
2778 .mhandler
.info
= mem_info
,
2785 .help
= "show memory tree",
2786 .mhandler
.info
= do_info_mtree
,
2792 .help
= "show dynamic compiler info",
2793 .mhandler
.info
= do_info_jit
,
2799 .help
= "show KVM information",
2800 .mhandler
.info
= hmp_info_kvm
,
2806 .help
= "show NUMA information",
2807 .mhandler
.info
= do_info_numa
,
2813 .help
= "show guest USB devices",
2814 .mhandler
.info
= usb_info
,
2820 .help
= "show host USB devices",
2821 .mhandler
.info
= usb_host_info
,
2827 .help
= "show profiling information",
2828 .mhandler
.info
= do_info_profile
,
2834 .help
= "show capture information",
2835 .mhandler
.info
= do_info_capture
,
2838 .name
= "snapshots",
2841 .help
= "show the currently saved VM snapshots",
2842 .mhandler
.info
= do_info_snapshots
,
2848 .help
= "show the current VM status (running|paused)",
2849 .mhandler
.info
= hmp_info_status
,
2855 .help
= "show guest PCMCIA status",
2856 .mhandler
.info
= pcmcia_info
,
2862 .help
= "show which guest mouse is receiving events",
2863 .mhandler
.info
= hmp_info_mice
,
2869 .help
= "show the vnc server status",
2870 .mhandler
.info
= hmp_info_vnc
,
2872 #if defined(CONFIG_SPICE)
2877 .help
= "show the spice server status",
2878 .mhandler
.info
= hmp_info_spice
,
2885 .help
= "show the current VM name",
2886 .mhandler
.info
= hmp_info_name
,
2892 .help
= "show the current VM UUID",
2893 .mhandler
.info
= hmp_info_uuid
,
2895 #if defined(TARGET_PPC)
2900 .help
= "show CPU statistics",
2901 .mhandler
.info
= do_info_cpu_stats
,
2904 #if defined(CONFIG_SLIRP)
2909 .help
= "show user network stack connection states",
2910 .mhandler
.info
= do_info_usernet
,
2917 .help
= "show migration status",
2918 .mhandler
.info
= hmp_info_migrate
,
2921 .name
= "migrate_capabilities",
2924 .help
= "show current migration capabilities",
2925 .mhandler
.info
= hmp_info_migrate_capabilities
,
2928 .name
= "migrate_cache_size",
2931 .help
= "show current migration xbzrle cache size",
2932 .mhandler
.info
= hmp_info_migrate_cache_size
,
2938 .help
= "show balloon information",
2939 .mhandler
.info
= hmp_info_balloon
,
2945 .help
= "show device tree",
2946 .mhandler
.info
= do_info_qtree
,
2952 .help
= "show qdev device model list",
2953 .mhandler
.info
= do_info_qdm
,
2959 .help
= "show roms",
2960 .mhandler
.info
= do_info_roms
,
2963 .name
= "trace-events",
2966 .help
= "show available trace-events & their state",
2967 .mhandler
.info
= do_trace_print_events
,
2974 static const mon_cmd_t qmp_cmds
[] = {
2975 #include "qmp-commands-old.h"
2979 /*******************************************************************/
2981 static const char *pch
;
2982 static jmp_buf expr_env
;
2987 typedef struct MonitorDef
{
2990 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2994 #if defined(TARGET_I386)
2995 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2997 CPUArchState
*env
= mon_get_cpu();
2998 return env
->eip
+ env
->segs
[R_CS
].base
;
3002 #if defined(TARGET_PPC)
3003 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3005 CPUArchState
*env
= mon_get_cpu();
3010 for (i
= 0; i
< 8; i
++)
3011 u
|= env
->crf
[i
] << (32 - (4 * i
));
3016 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3018 CPUArchState
*env
= mon_get_cpu();
3022 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3024 CPUArchState
*env
= mon_get_cpu();
3028 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3030 CPUArchState
*env
= mon_get_cpu();
3031 return cpu_ppc_load_decr(env
);
3034 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3036 CPUArchState
*env
= mon_get_cpu();
3037 return cpu_ppc_load_tbu(env
);
3040 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3042 CPUArchState
*env
= mon_get_cpu();
3043 return cpu_ppc_load_tbl(env
);
3047 #if defined(TARGET_SPARC)
3048 #ifndef TARGET_SPARC64
3049 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3051 CPUArchState
*env
= mon_get_cpu();
3053 return cpu_get_psr(env
);
3057 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3059 CPUArchState
*env
= mon_get_cpu();
3060 return env
->regwptr
[val
];
3064 static const MonitorDef monitor_defs
[] = {
3067 #define SEG(name, seg) \
3068 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3069 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3070 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
3072 { "eax", offsetof(CPUX86State
, regs
[0]) },
3073 { "ecx", offsetof(CPUX86State
, regs
[1]) },
3074 { "edx", offsetof(CPUX86State
, regs
[2]) },
3075 { "ebx", offsetof(CPUX86State
, regs
[3]) },
3076 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
3077 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
3078 { "esi", offsetof(CPUX86State
, regs
[6]) },
3079 { "edi", offsetof(CPUX86State
, regs
[7]) },
3080 #ifdef TARGET_X86_64
3081 { "r8", offsetof(CPUX86State
, regs
[8]) },
3082 { "r9", offsetof(CPUX86State
, regs
[9]) },
3083 { "r10", offsetof(CPUX86State
, regs
[10]) },
3084 { "r11", offsetof(CPUX86State
, regs
[11]) },
3085 { "r12", offsetof(CPUX86State
, regs
[12]) },
3086 { "r13", offsetof(CPUX86State
, regs
[13]) },
3087 { "r14", offsetof(CPUX86State
, regs
[14]) },
3088 { "r15", offsetof(CPUX86State
, regs
[15]) },
3090 { "eflags", offsetof(CPUX86State
, eflags
) },
3091 { "eip", offsetof(CPUX86State
, eip
) },
3098 { "pc", 0, monitor_get_pc
, },
3099 #elif defined(TARGET_PPC)
3100 /* General purpose registers */
3101 { "r0", offsetof(CPUPPCState
, gpr
[0]) },
3102 { "r1", offsetof(CPUPPCState
, gpr
[1]) },
3103 { "r2", offsetof(CPUPPCState
, gpr
[2]) },
3104 { "r3", offsetof(CPUPPCState
, gpr
[3]) },
3105 { "r4", offsetof(CPUPPCState
, gpr
[4]) },
3106 { "r5", offsetof(CPUPPCState
, gpr
[5]) },
3107 { "r6", offsetof(CPUPPCState
, gpr
[6]) },
3108 { "r7", offsetof(CPUPPCState
, gpr
[7]) },
3109 { "r8", offsetof(CPUPPCState
, gpr
[8]) },
3110 { "r9", offsetof(CPUPPCState
, gpr
[9]) },
3111 { "r10", offsetof(CPUPPCState
, gpr
[10]) },
3112 { "r11", offsetof(CPUPPCState
, gpr
[11]) },
3113 { "r12", offsetof(CPUPPCState
, gpr
[12]) },
3114 { "r13", offsetof(CPUPPCState
, gpr
[13]) },
3115 { "r14", offsetof(CPUPPCState
, gpr
[14]) },
3116 { "r15", offsetof(CPUPPCState
, gpr
[15]) },
3117 { "r16", offsetof(CPUPPCState
, gpr
[16]) },
3118 { "r17", offsetof(CPUPPCState
, gpr
[17]) },
3119 { "r18", offsetof(CPUPPCState
, gpr
[18]) },
3120 { "r19", offsetof(CPUPPCState
, gpr
[19]) },
3121 { "r20", offsetof(CPUPPCState
, gpr
[20]) },
3122 { "r21", offsetof(CPUPPCState
, gpr
[21]) },
3123 { "r22", offsetof(CPUPPCState
, gpr
[22]) },
3124 { "r23", offsetof(CPUPPCState
, gpr
[23]) },
3125 { "r24", offsetof(CPUPPCState
, gpr
[24]) },
3126 { "r25", offsetof(CPUPPCState
, gpr
[25]) },
3127 { "r26", offsetof(CPUPPCState
, gpr
[26]) },
3128 { "r27", offsetof(CPUPPCState
, gpr
[27]) },
3129 { "r28", offsetof(CPUPPCState
, gpr
[28]) },
3130 { "r29", offsetof(CPUPPCState
, gpr
[29]) },
3131 { "r30", offsetof(CPUPPCState
, gpr
[30]) },
3132 { "r31", offsetof(CPUPPCState
, gpr
[31]) },
3133 /* Floating point registers */
3134 { "f0", offsetof(CPUPPCState
, fpr
[0]) },
3135 { "f1", offsetof(CPUPPCState
, fpr
[1]) },
3136 { "f2", offsetof(CPUPPCState
, fpr
[2]) },
3137 { "f3", offsetof(CPUPPCState
, fpr
[3]) },
3138 { "f4", offsetof(CPUPPCState
, fpr
[4]) },
3139 { "f5", offsetof(CPUPPCState
, fpr
[5]) },
3140 { "f6", offsetof(CPUPPCState
, fpr
[6]) },
3141 { "f7", offsetof(CPUPPCState
, fpr
[7]) },
3142 { "f8", offsetof(CPUPPCState
, fpr
[8]) },
3143 { "f9", offsetof(CPUPPCState
, fpr
[9]) },
3144 { "f10", offsetof(CPUPPCState
, fpr
[10]) },
3145 { "f11", offsetof(CPUPPCState
, fpr
[11]) },
3146 { "f12", offsetof(CPUPPCState
, fpr
[12]) },
3147 { "f13", offsetof(CPUPPCState
, fpr
[13]) },
3148 { "f14", offsetof(CPUPPCState
, fpr
[14]) },
3149 { "f15", offsetof(CPUPPCState
, fpr
[15]) },
3150 { "f16", offsetof(CPUPPCState
, fpr
[16]) },
3151 { "f17", offsetof(CPUPPCState
, fpr
[17]) },
3152 { "f18", offsetof(CPUPPCState
, fpr
[18]) },
3153 { "f19", offsetof(CPUPPCState
, fpr
[19]) },
3154 { "f20", offsetof(CPUPPCState
, fpr
[20]) },
3155 { "f21", offsetof(CPUPPCState
, fpr
[21]) },
3156 { "f22", offsetof(CPUPPCState
, fpr
[22]) },
3157 { "f23", offsetof(CPUPPCState
, fpr
[23]) },
3158 { "f24", offsetof(CPUPPCState
, fpr
[24]) },
3159 { "f25", offsetof(CPUPPCState
, fpr
[25]) },
3160 { "f26", offsetof(CPUPPCState
, fpr
[26]) },
3161 { "f27", offsetof(CPUPPCState
, fpr
[27]) },
3162 { "f28", offsetof(CPUPPCState
, fpr
[28]) },
3163 { "f29", offsetof(CPUPPCState
, fpr
[29]) },
3164 { "f30", offsetof(CPUPPCState
, fpr
[30]) },
3165 { "f31", offsetof(CPUPPCState
, fpr
[31]) },
3166 { "fpscr", offsetof(CPUPPCState
, fpscr
) },
3167 /* Next instruction pointer */
3168 { "nip|pc", offsetof(CPUPPCState
, nip
) },
3169 { "lr", offsetof(CPUPPCState
, lr
) },
3170 { "ctr", offsetof(CPUPPCState
, ctr
) },
3171 { "decr", 0, &monitor_get_decr
, },
3172 { "ccr", 0, &monitor_get_ccr
, },
3173 /* Machine state register */
3174 { "msr", 0, &monitor_get_msr
, },
3175 { "xer", 0, &monitor_get_xer
, },
3176 { "tbu", 0, &monitor_get_tbu
, },
3177 { "tbl", 0, &monitor_get_tbl
, },
3178 #if defined(TARGET_PPC64)
3179 /* Address space register */
3180 { "asr", offsetof(CPUPPCState
, asr
) },
3182 /* Segment registers */
3183 { "sdr1", offsetof(CPUPPCState
, spr
[SPR_SDR1
]) },
3184 { "sr0", offsetof(CPUPPCState
, sr
[0]) },
3185 { "sr1", offsetof(CPUPPCState
, sr
[1]) },
3186 { "sr2", offsetof(CPUPPCState
, sr
[2]) },
3187 { "sr3", offsetof(CPUPPCState
, sr
[3]) },
3188 { "sr4", offsetof(CPUPPCState
, sr
[4]) },
3189 { "sr5", offsetof(CPUPPCState
, sr
[5]) },
3190 { "sr6", offsetof(CPUPPCState
, sr
[6]) },
3191 { "sr7", offsetof(CPUPPCState
, sr
[7]) },
3192 { "sr8", offsetof(CPUPPCState
, sr
[8]) },
3193 { "sr9", offsetof(CPUPPCState
, sr
[9]) },
3194 { "sr10", offsetof(CPUPPCState
, sr
[10]) },
3195 { "sr11", offsetof(CPUPPCState
, sr
[11]) },
3196 { "sr12", offsetof(CPUPPCState
, sr
[12]) },
3197 { "sr13", offsetof(CPUPPCState
, sr
[13]) },
3198 { "sr14", offsetof(CPUPPCState
, sr
[14]) },
3199 { "sr15", offsetof(CPUPPCState
, sr
[15]) },
3200 /* Too lazy to put BATs... */
3201 { "pvr", offsetof(CPUPPCState
, spr
[SPR_PVR
]) },
3203 { "srr0", offsetof(CPUPPCState
, spr
[SPR_SRR0
]) },
3204 { "srr1", offsetof(CPUPPCState
, spr
[SPR_SRR1
]) },
3205 { "sprg0", offsetof(CPUPPCState
, spr
[SPR_SPRG0
]) },
3206 { "sprg1", offsetof(CPUPPCState
, spr
[SPR_SPRG1
]) },
3207 { "sprg2", offsetof(CPUPPCState
, spr
[SPR_SPRG2
]) },
3208 { "sprg3", offsetof(CPUPPCState
, spr
[SPR_SPRG3
]) },
3209 { "sprg4", offsetof(CPUPPCState
, spr
[SPR_SPRG4
]) },
3210 { "sprg5", offsetof(CPUPPCState
, spr
[SPR_SPRG5
]) },
3211 { "sprg6", offsetof(CPUPPCState
, spr
[SPR_SPRG6
]) },
3212 { "sprg7", offsetof(CPUPPCState
, spr
[SPR_SPRG7
]) },
3213 { "pid", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID
]) },
3214 { "csrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR0
]) },
3215 { "csrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR1
]) },
3216 { "esr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_ESR
]) },
3217 { "dear", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DEAR
]) },
3218 { "mcsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSR
]) },
3219 { "tsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TSR
]) },
3220 { "tcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TCR
]) },
3221 { "vrsave", offsetof(CPUPPCState
, spr
[SPR_VRSAVE
]) },
3222 { "pir", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PIR
]) },
3223 { "mcsrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR0
]) },
3224 { "mcsrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR1
]) },
3225 { "decar", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DECAR
]) },
3226 { "ivpr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVPR
]) },
3227 { "epcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPCR
]) },
3228 { "sprg8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_SPRG8
]) },
3229 { "ivor0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR0
]) },
3230 { "ivor1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR1
]) },
3231 { "ivor2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR2
]) },
3232 { "ivor3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR3
]) },
3233 { "ivor4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR4
]) },
3234 { "ivor5", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR5
]) },
3235 { "ivor6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR6
]) },
3236 { "ivor7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR7
]) },
3237 { "ivor8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR8
]) },
3238 { "ivor9", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR9
]) },
3239 { "ivor10", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR10
]) },
3240 { "ivor11", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR11
]) },
3241 { "ivor12", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR12
]) },
3242 { "ivor13", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR13
]) },
3243 { "ivor14", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR14
]) },
3244 { "ivor15", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR15
]) },
3245 { "ivor32", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR32
]) },
3246 { "ivor33", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR33
]) },
3247 { "ivor34", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR34
]) },
3248 { "ivor35", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR35
]) },
3249 { "ivor36", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR36
]) },
3250 { "ivor37", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR37
]) },
3251 { "mas0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS0
]) },
3252 { "mas1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS1
]) },
3253 { "mas2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS2
]) },
3254 { "mas3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS3
]) },
3255 { "mas4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS4
]) },
3256 { "mas6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS6
]) },
3257 { "mas7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS7
]) },
3258 { "mmucfg", offsetof(CPUPPCState
, spr
[SPR_MMUCFG
]) },
3259 { "tlb0cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3260 { "tlb1cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3261 { "epr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPR
]) },
3262 { "eplc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPLC
]) },
3263 { "epsc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPSC
]) },
3264 { "svr", offsetof(CPUPPCState
, spr
[SPR_E500_SVR
]) },
3265 { "mcar", offsetof(CPUPPCState
, spr
[SPR_Exxx_MCAR
]) },
3266 { "pid1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID1
]) },
3267 { "pid2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID2
]) },
3268 { "hid0", offsetof(CPUPPCState
, spr
[SPR_HID0
]) },
3270 #elif defined(TARGET_SPARC)
3271 { "g0", offsetof(CPUSPARCState
, gregs
[0]) },
3272 { "g1", offsetof(CPUSPARCState
, gregs
[1]) },
3273 { "g2", offsetof(CPUSPARCState
, gregs
[2]) },
3274 { "g3", offsetof(CPUSPARCState
, gregs
[3]) },
3275 { "g4", offsetof(CPUSPARCState
, gregs
[4]) },
3276 { "g5", offsetof(CPUSPARCState
, gregs
[5]) },
3277 { "g6", offsetof(CPUSPARCState
, gregs
[6]) },
3278 { "g7", offsetof(CPUSPARCState
, gregs
[7]) },
3279 { "o0", 0, monitor_get_reg
},
3280 { "o1", 1, monitor_get_reg
},
3281 { "o2", 2, monitor_get_reg
},
3282 { "o3", 3, monitor_get_reg
},
3283 { "o4", 4, monitor_get_reg
},
3284 { "o5", 5, monitor_get_reg
},
3285 { "o6", 6, monitor_get_reg
},
3286 { "o7", 7, monitor_get_reg
},
3287 { "l0", 8, monitor_get_reg
},
3288 { "l1", 9, monitor_get_reg
},
3289 { "l2", 10, monitor_get_reg
},
3290 { "l3", 11, monitor_get_reg
},
3291 { "l4", 12, monitor_get_reg
},
3292 { "l5", 13, monitor_get_reg
},
3293 { "l6", 14, monitor_get_reg
},
3294 { "l7", 15, monitor_get_reg
},
3295 { "i0", 16, monitor_get_reg
},
3296 { "i1", 17, monitor_get_reg
},
3297 { "i2", 18, monitor_get_reg
},
3298 { "i3", 19, monitor_get_reg
},
3299 { "i4", 20, monitor_get_reg
},
3300 { "i5", 21, monitor_get_reg
},
3301 { "i6", 22, monitor_get_reg
},
3302 { "i7", 23, monitor_get_reg
},
3303 { "pc", offsetof(CPUSPARCState
, pc
) },
3304 { "npc", offsetof(CPUSPARCState
, npc
) },
3305 { "y", offsetof(CPUSPARCState
, y
) },
3306 #ifndef TARGET_SPARC64
3307 { "psr", 0, &monitor_get_psr
, },
3308 { "wim", offsetof(CPUSPARCState
, wim
) },
3310 { "tbr", offsetof(CPUSPARCState
, tbr
) },
3311 { "fsr", offsetof(CPUSPARCState
, fsr
) },
3312 { "f0", offsetof(CPUSPARCState
, fpr
[0].l
.upper
) },
3313 { "f1", offsetof(CPUSPARCState
, fpr
[0].l
.lower
) },
3314 { "f2", offsetof(CPUSPARCState
, fpr
[1].l
.upper
) },
3315 { "f3", offsetof(CPUSPARCState
, fpr
[1].l
.lower
) },
3316 { "f4", offsetof(CPUSPARCState
, fpr
[2].l
.upper
) },
3317 { "f5", offsetof(CPUSPARCState
, fpr
[2].l
.lower
) },
3318 { "f6", offsetof(CPUSPARCState
, fpr
[3].l
.upper
) },
3319 { "f7", offsetof(CPUSPARCState
, fpr
[3].l
.lower
) },
3320 { "f8", offsetof(CPUSPARCState
, fpr
[4].l
.upper
) },
3321 { "f9", offsetof(CPUSPARCState
, fpr
[4].l
.lower
) },
3322 { "f10", offsetof(CPUSPARCState
, fpr
[5].l
.upper
) },
3323 { "f11", offsetof(CPUSPARCState
, fpr
[5].l
.lower
) },
3324 { "f12", offsetof(CPUSPARCState
, fpr
[6].l
.upper
) },
3325 { "f13", offsetof(CPUSPARCState
, fpr
[6].l
.lower
) },
3326 { "f14", offsetof(CPUSPARCState
, fpr
[7].l
.upper
) },
3327 { "f15", offsetof(CPUSPARCState
, fpr
[7].l
.lower
) },
3328 { "f16", offsetof(CPUSPARCState
, fpr
[8].l
.upper
) },
3329 { "f17", offsetof(CPUSPARCState
, fpr
[8].l
.lower
) },
3330 { "f18", offsetof(CPUSPARCState
, fpr
[9].l
.upper
) },
3331 { "f19", offsetof(CPUSPARCState
, fpr
[9].l
.lower
) },
3332 { "f20", offsetof(CPUSPARCState
, fpr
[10].l
.upper
) },
3333 { "f21", offsetof(CPUSPARCState
, fpr
[10].l
.lower
) },
3334 { "f22", offsetof(CPUSPARCState
, fpr
[11].l
.upper
) },
3335 { "f23", offsetof(CPUSPARCState
, fpr
[11].l
.lower
) },
3336 { "f24", offsetof(CPUSPARCState
, fpr
[12].l
.upper
) },
3337 { "f25", offsetof(CPUSPARCState
, fpr
[12].l
.lower
) },
3338 { "f26", offsetof(CPUSPARCState
, fpr
[13].l
.upper
) },
3339 { "f27", offsetof(CPUSPARCState
, fpr
[13].l
.lower
) },
3340 { "f28", offsetof(CPUSPARCState
, fpr
[14].l
.upper
) },
3341 { "f29", offsetof(CPUSPARCState
, fpr
[14].l
.lower
) },
3342 { "f30", offsetof(CPUSPARCState
, fpr
[15].l
.upper
) },
3343 { "f31", offsetof(CPUSPARCState
, fpr
[15].l
.lower
) },
3344 #ifdef TARGET_SPARC64
3345 { "f32", offsetof(CPUSPARCState
, fpr
[16]) },
3346 { "f34", offsetof(CPUSPARCState
, fpr
[17]) },
3347 { "f36", offsetof(CPUSPARCState
, fpr
[18]) },
3348 { "f38", offsetof(CPUSPARCState
, fpr
[19]) },
3349 { "f40", offsetof(CPUSPARCState
, fpr
[20]) },
3350 { "f42", offsetof(CPUSPARCState
, fpr
[21]) },
3351 { "f44", offsetof(CPUSPARCState
, fpr
[22]) },
3352 { "f46", offsetof(CPUSPARCState
, fpr
[23]) },
3353 { "f48", offsetof(CPUSPARCState
, fpr
[24]) },
3354 { "f50", offsetof(CPUSPARCState
, fpr
[25]) },
3355 { "f52", offsetof(CPUSPARCState
, fpr
[26]) },
3356 { "f54", offsetof(CPUSPARCState
, fpr
[27]) },
3357 { "f56", offsetof(CPUSPARCState
, fpr
[28]) },
3358 { "f58", offsetof(CPUSPARCState
, fpr
[29]) },
3359 { "f60", offsetof(CPUSPARCState
, fpr
[30]) },
3360 { "f62", offsetof(CPUSPARCState
, fpr
[31]) },
3361 { "asi", offsetof(CPUSPARCState
, asi
) },
3362 { "pstate", offsetof(CPUSPARCState
, pstate
) },
3363 { "cansave", offsetof(CPUSPARCState
, cansave
) },
3364 { "canrestore", offsetof(CPUSPARCState
, canrestore
) },
3365 { "otherwin", offsetof(CPUSPARCState
, otherwin
) },
3366 { "wstate", offsetof(CPUSPARCState
, wstate
) },
3367 { "cleanwin", offsetof(CPUSPARCState
, cleanwin
) },
3368 { "fprs", offsetof(CPUSPARCState
, fprs
) },
3374 static void expr_error(Monitor
*mon
, const char *msg
)
3376 monitor_printf(mon
, "%s\n", msg
);
3377 longjmp(expr_env
, 1);
3380 /* return 0 if OK, -1 if not found */
3381 static int get_monitor_def(target_long
*pval
, const char *name
)
3383 const MonitorDef
*md
;
3386 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3387 if (compare_cmd(name
, md
->name
)) {
3388 if (md
->get_value
) {
3389 *pval
= md
->get_value(md
, md
->offset
);
3391 CPUArchState
*env
= mon_get_cpu();
3392 ptr
= (uint8_t *)env
+ md
->offset
;
3395 *pval
= *(int32_t *)ptr
;
3398 *pval
= *(target_long
*)ptr
;
3411 static void next(void)
3415 while (qemu_isspace(*pch
))
3420 static int64_t expr_sum(Monitor
*mon
);
3422 static int64_t expr_unary(Monitor
*mon
)
3431 n
= expr_unary(mon
);
3435 n
= -expr_unary(mon
);
3439 n
= ~expr_unary(mon
);
3445 expr_error(mon
, "')' expected");
3452 expr_error(mon
, "character constant expected");
3456 expr_error(mon
, "missing terminating \' character");
3466 while ((*pch
>= 'a' && *pch
<= 'z') ||
3467 (*pch
>= 'A' && *pch
<= 'Z') ||
3468 (*pch
>= '0' && *pch
<= '9') ||
3469 *pch
== '_' || *pch
== '.') {
3470 if ((q
- buf
) < sizeof(buf
) - 1)
3474 while (qemu_isspace(*pch
))
3477 ret
= get_monitor_def(®
, buf
);
3479 expr_error(mon
, "unknown register");
3484 expr_error(mon
, "unexpected end of expression");
3489 #if TARGET_PHYS_ADDR_BITS > 32
3490 n
= strtoull(pch
, &p
, 0);
3492 n
= strtoul(pch
, &p
, 0);
3494 if (errno
== ERANGE
) {
3495 expr_error(mon
, "number too large");
3498 expr_error(mon
, "invalid char in expression");
3501 while (qemu_isspace(*pch
))
3509 static int64_t expr_prod(Monitor
*mon
)
3514 val
= expr_unary(mon
);
3517 if (op
!= '*' && op
!= '/' && op
!= '%')
3520 val2
= expr_unary(mon
);
3529 expr_error(mon
, "division by zero");
3540 static int64_t expr_logic(Monitor
*mon
)
3545 val
= expr_prod(mon
);
3548 if (op
!= '&' && op
!= '|' && op
!= '^')
3551 val2
= expr_prod(mon
);
3568 static int64_t expr_sum(Monitor
*mon
)
3573 val
= expr_logic(mon
);
3576 if (op
!= '+' && op
!= '-')
3579 val2
= expr_logic(mon
);
3588 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3591 if (setjmp(expr_env
)) {
3595 while (qemu_isspace(*pch
))
3597 *pval
= expr_sum(mon
);
3602 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3604 const char *p
= *pp
;
3608 d
= strtod(p
, &tailp
);
3610 monitor_printf(mon
, "Number expected\n");
3613 if (d
!= d
|| d
- d
!= 0) {
3614 /* NaN or infinity */
3615 monitor_printf(mon
, "Bad number\n");
3623 static int get_str(char *buf
, int buf_size
, const char **pp
)
3631 while (qemu_isspace(*p
))
3641 while (*p
!= '\0' && *p
!= '\"') {
3657 qemu_printf("unsupported escape code: '\\%c'\n", c
);
3660 if ((q
- buf
) < buf_size
- 1) {
3664 if ((q
- buf
) < buf_size
- 1) {
3671 qemu_printf("unterminated string\n");
3676 while (*p
!= '\0' && !qemu_isspace(*p
)) {
3677 if ((q
- buf
) < buf_size
- 1) {
3689 * Store the command-name in cmdname, and return a pointer to
3690 * the remaining of the command string.
3692 static const char *get_command_name(const char *cmdline
,
3693 char *cmdname
, size_t nlen
)
3696 const char *p
, *pstart
;
3699 while (qemu_isspace(*p
))
3704 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3709 memcpy(cmdname
, pstart
, len
);
3710 cmdname
[len
] = '\0';
3715 * Read key of 'type' into 'key' and return the current
3718 static char *key_get_info(const char *type
, char **key
)
3726 p
= strchr(type
, ':');
3733 str
= g_malloc(len
+ 1);
3734 memcpy(str
, type
, len
);
3741 static int default_fmt_format
= 'x';
3742 static int default_fmt_size
= 4;
3746 static int is_valid_option(const char *c
, const char *typestr
)
3754 typestr
= strstr(typestr
, option
);
3755 return (typestr
!= NULL
);
3758 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3759 const char *cmdname
)
3761 const mon_cmd_t
*cmd
;
3763 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3764 if (compare_cmd(cmdname
, cmd
->name
)) {
3772 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3774 return search_dispatch_table(mon_cmds
, cmdname
);
3777 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3779 return search_dispatch_table(qmp_cmds
, cmdname
);
3782 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3783 const char *cmdline
,
3786 const char *p
, *typestr
;
3788 const mon_cmd_t
*cmd
;
3794 monitor_printf(mon
, "command='%s'\n", cmdline
);
3797 /* extract the command name */
3798 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3802 cmd
= monitor_find_command(cmdname
);
3804 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3808 /* parse the parameters */
3809 typestr
= cmd
->args_type
;
3811 typestr
= key_get_info(typestr
, &key
);
3823 while (qemu_isspace(*p
))
3825 if (*typestr
== '?') {
3828 /* no optional string: NULL argument */
3832 ret
= get_str(buf
, sizeof(buf
), &p
);
3836 monitor_printf(mon
, "%s: filename expected\n",
3840 monitor_printf(mon
, "%s: block device name expected\n",
3844 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3849 qdict_put(qdict
, key
, qstring_from_str(buf
));
3854 QemuOptsList
*opts_list
;
3857 opts_list
= qemu_find_opts(key
);
3858 if (!opts_list
|| opts_list
->desc
->name
) {
3861 while (qemu_isspace(*p
)) {
3866 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3869 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3873 qemu_opts_to_qdict(opts
, qdict
);
3874 qemu_opts_del(opts
);
3879 int count
, format
, size
;
3881 while (qemu_isspace(*p
))
3887 if (qemu_isdigit(*p
)) {
3889 while (qemu_isdigit(*p
)) {
3890 count
= count
* 10 + (*p
- '0');
3928 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3929 monitor_printf(mon
, "invalid char in format: '%c'\n",
3934 format
= default_fmt_format
;
3935 if (format
!= 'i') {
3936 /* for 'i', not specifying a size gives -1 as size */
3938 size
= default_fmt_size
;
3939 default_fmt_size
= size
;
3941 default_fmt_format
= format
;
3944 format
= default_fmt_format
;
3945 if (format
!= 'i') {
3946 size
= default_fmt_size
;
3951 qdict_put(qdict
, "count", qint_from_int(count
));
3952 qdict_put(qdict
, "format", qint_from_int(format
));
3953 qdict_put(qdict
, "size", qint_from_int(size
));
3962 while (qemu_isspace(*p
))
3964 if (*typestr
== '?' || *typestr
== '.') {
3965 if (*typestr
== '?') {
3973 while (qemu_isspace(*p
))
3982 if (get_expr(mon
, &val
, &p
))
3984 /* Check if 'i' is greater than 32-bit */
3985 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3986 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3987 monitor_printf(mon
, "integer is for 32-bit values\n");
3989 } else if (c
== 'M') {
3991 monitor_printf(mon
, "enter a positive value\n");
3996 qdict_put(qdict
, key
, qint_from_int(val
));
4004 while (qemu_isspace(*p
)) {
4007 if (*typestr
== '?') {
4013 val
= strtosz(p
, &end
);
4015 monitor_printf(mon
, "invalid size\n");
4018 qdict_put(qdict
, key
, qint_from_int(val
));
4026 while (qemu_isspace(*p
))
4028 if (*typestr
== '?') {
4034 if (get_double(mon
, &val
, &p
) < 0) {
4037 if (p
[0] && p
[1] == 's') {
4040 val
/= 1e3
; p
+= 2; break;
4042 val
/= 1e6
; p
+= 2; break;
4044 val
/= 1e9
; p
+= 2; break;
4047 if (*p
&& !qemu_isspace(*p
)) {
4048 monitor_printf(mon
, "Unknown unit suffix\n");
4051 qdict_put(qdict
, key
, qfloat_from_double(val
));
4059 while (qemu_isspace(*p
)) {
4063 while (qemu_isgraph(*p
)) {
4066 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4068 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4071 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4074 qdict_put(qdict
, key
, qbool_from_int(val
));
4079 const char *tmp
= p
;
4086 while (qemu_isspace(*p
))
4091 if(!is_valid_option(p
, typestr
)) {
4093 monitor_printf(mon
, "%s: unsupported option -%c\n",
4105 qdict_put(qdict
, key
, qbool_from_int(1));
4112 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4118 /* check that all arguments were parsed */
4119 while (qemu_isspace(*p
))
4122 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4134 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4136 /* report only the first error */
4138 mon
->error
= qerror
;
4144 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4146 if (ret
&& !monitor_has_error(mon
)) {
4148 * If it returns failure, it must have passed on error.
4150 * Action: Report an internal error to the client if in QMP.
4152 qerror_report(QERR_UNDEFINED_ERROR
);
4156 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4159 const mon_cmd_t
*cmd
;
4161 qdict
= qdict_new();
4163 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
4167 if (handler_is_async(cmd
)) {
4168 user_async_cmd_handler(mon
, cmd
, qdict
);
4169 } else if (handler_is_qobject(cmd
)) {
4170 QObject
*data
= NULL
;
4172 /* XXX: ignores the error code */
4173 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4174 assert(!monitor_has_error(mon
));
4176 cmd
->user_print(mon
, data
);
4177 qobject_decref(data
);
4180 cmd
->mhandler
.cmd(mon
, qdict
);
4187 static void cmd_completion(const char *name
, const char *list
)
4189 const char *p
, *pstart
;
4198 p
= pstart
+ strlen(pstart
);
4200 if (len
> sizeof(cmd
) - 2)
4201 len
= sizeof(cmd
) - 2;
4202 memcpy(cmd
, pstart
, len
);
4204 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4205 readline_add_completion(cur_mon
->rs
, cmd
);
4213 static void file_completion(const char *input
)
4218 char file
[1024], file_prefix
[1024];
4222 p
= strrchr(input
, '/');
4225 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4226 pstrcpy(path
, sizeof(path
), ".");
4228 input_path_len
= p
- input
+ 1;
4229 memcpy(path
, input
, input_path_len
);
4230 if (input_path_len
> sizeof(path
) - 1)
4231 input_path_len
= sizeof(path
) - 1;
4232 path
[input_path_len
] = '\0';
4233 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4235 #ifdef DEBUG_COMPLETION
4236 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
4237 input
, path
, file_prefix
);
4239 ffs
= opendir(path
);
4248 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4252 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4253 memcpy(file
, input
, input_path_len
);
4254 if (input_path_len
< sizeof(file
))
4255 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4257 /* stat the file to find out if it's a directory.
4258 * In that case add a slash to speed up typing long paths
4260 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
4261 pstrcat(file
, sizeof(file
), "/");
4263 readline_add_completion(cur_mon
->rs
, file
);
4269 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4271 const char *name
= bdrv_get_device_name(bs
);
4272 const char *input
= opaque
;
4274 if (input
[0] == '\0' ||
4275 !strncmp(name
, (char *)input
, strlen(input
))) {
4276 readline_add_completion(cur_mon
->rs
, name
);
4280 /* NOTE: this parser is an approximate form of the real command parser */
4281 static void parse_cmdline(const char *cmdline
,
4282 int *pnb_args
, char **args
)
4291 while (qemu_isspace(*p
))
4295 if (nb_args
>= MAX_ARGS
)
4297 ret
= get_str(buf
, sizeof(buf
), &p
);
4298 args
[nb_args
] = g_strdup(buf
);
4303 *pnb_args
= nb_args
;
4306 static const char *next_arg_type(const char *typestr
)
4308 const char *p
= strchr(typestr
, ':');
4309 return (p
!= NULL
? ++p
: typestr
);
4312 static void monitor_find_completion(const char *cmdline
)
4314 const char *cmdname
;
4315 char *args
[MAX_ARGS
];
4316 int nb_args
, i
, len
;
4317 const char *ptype
, *str
;
4318 const mon_cmd_t
*cmd
;
4321 parse_cmdline(cmdline
, &nb_args
, args
);
4322 #ifdef DEBUG_COMPLETION
4323 for(i
= 0; i
< nb_args
; i
++) {
4324 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
4328 /* if the line ends with a space, it means we want to complete the
4330 len
= strlen(cmdline
);
4331 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4332 if (nb_args
>= MAX_ARGS
) {
4335 args
[nb_args
++] = g_strdup("");
4338 /* command completion */
4343 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
4344 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4345 cmd_completion(cmdname
, cmd
->name
);
4348 /* find the command */
4349 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4350 if (compare_cmd(args
[0], cmd
->name
)) {
4358 ptype
= next_arg_type(cmd
->args_type
);
4359 for(i
= 0; i
< nb_args
- 2; i
++) {
4360 if (*ptype
!= '\0') {
4361 ptype
= next_arg_type(ptype
);
4362 while (*ptype
== '?')
4363 ptype
= next_arg_type(ptype
);
4366 str
= args
[nb_args
- 1];
4367 if (*ptype
== '-' && ptype
[1] != '\0') {
4368 ptype
= next_arg_type(ptype
);
4372 /* file completion */
4373 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4374 file_completion(str
);
4377 /* block device name completion */
4378 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4379 bdrv_iterate(block_completion_it
, (void *)str
);
4382 /* XXX: more generic ? */
4383 if (!strcmp(cmd
->name
, "info")) {
4384 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4385 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
4386 cmd_completion(str
, cmd
->name
);
4388 } else if (!strcmp(cmd
->name
, "sendkey")) {
4389 char *sep
= strrchr(str
, '-');
4392 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4393 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
4394 cmd_completion(str
, key
->name
);
4396 } else if (!strcmp(cmd
->name
, "help|?")) {
4397 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
4398 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
4399 cmd_completion(str
, cmd
->name
);
4409 for (i
= 0; i
< nb_args
; i
++) {
4414 static int monitor_can_read(void *opaque
)
4416 Monitor
*mon
= opaque
;
4418 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4421 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4423 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4424 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4428 * Argument validation rules:
4430 * 1. The argument must exist in cmd_args qdict
4431 * 2. The argument type must be the expected one
4433 * Special case: If the argument doesn't exist in cmd_args and
4434 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4435 * checking is skipped for it.
4437 static int check_client_args_type(const QDict
*client_args
,
4438 const QDict
*cmd_args
, int flags
)
4440 const QDictEntry
*ent
;
4442 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4445 const QObject
*client_arg
= qdict_entry_value(ent
);
4446 const char *client_arg_name
= qdict_entry_key(ent
);
4448 obj
= qdict_get(cmd_args
, client_arg_name
);
4450 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4451 /* handler accepts unknowns */
4454 /* client arg doesn't exist */
4455 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4459 arg_type
= qobject_to_qstring(obj
);
4460 assert(arg_type
!= NULL
);
4462 /* check if argument's type is correct */
4463 switch (qstring_get_str(arg_type
)[0]) {
4467 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4468 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4477 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4478 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4484 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4485 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4486 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4493 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4494 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4500 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4503 /* Any QObject can be passed. */
4508 * These types are not supported by QMP and thus are not
4509 * handled here. Fall through.
4520 * - Check if the client has passed all mandatory args
4521 * - Set special flags for argument validation
4523 static int check_mandatory_args(const QDict
*cmd_args
,
4524 const QDict
*client_args
, int *flags
)
4526 const QDictEntry
*ent
;
4528 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4529 const char *cmd_arg_name
= qdict_entry_key(ent
);
4530 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4531 assert(type
!= NULL
);
4533 if (qstring_get_str(type
)[0] == 'O') {
4534 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4535 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4536 } else if (qstring_get_str(type
)[0] != '-' &&
4537 qstring_get_str(type
)[1] != '?' &&
4538 !qdict_haskey(client_args
, cmd_arg_name
)) {
4539 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4547 static QDict
*qdict_from_args_type(const char *args_type
)
4551 QString
*key
, *type
, *cur_qs
;
4553 assert(args_type
!= NULL
);
4555 qdict
= qdict_new();
4557 if (args_type
== NULL
|| args_type
[0] == '\0') {
4558 /* no args, empty qdict */
4562 key
= qstring_new();
4563 type
= qstring_new();
4568 switch (args_type
[i
]) {
4571 qdict_put(qdict
, qstring_get_str(key
), type
);
4573 if (args_type
[i
] == '\0') {
4576 type
= qstring_new(); /* qdict has ref */
4577 cur_qs
= key
= qstring_new();
4583 qstring_append_chr(cur_qs
, args_type
[i
]);
4593 * Client argument checking rules:
4595 * 1. Client must provide all mandatory arguments
4596 * 2. Each argument provided by the client must be expected
4597 * 3. Each argument provided by the client must have the type expected
4600 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4605 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4608 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4613 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4621 * Input object checking rules
4623 * 1. Input object must be a dict
4624 * 2. The "execute" key must exist
4625 * 3. The "execute" key must be a string
4626 * 4. If the "arguments" key exists, it must be a dict
4627 * 5. If the "id" key exists, it can be anything (ie. json-value)
4628 * 6. Any argument not listed above is considered invalid
4630 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4632 const QDictEntry
*ent
;
4633 int has_exec_key
= 0;
4636 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4637 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4641 input_dict
= qobject_to_qdict(input_obj
);
4643 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4644 const char *arg_name
= qdict_entry_key(ent
);
4645 const QObject
*arg_obj
= qdict_entry_value(ent
);
4647 if (!strcmp(arg_name
, "execute")) {
4648 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4649 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4654 } else if (!strcmp(arg_name
, "arguments")) {
4655 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4656 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4660 } else if (!strcmp(arg_name
, "id")) {
4661 /* FIXME: check duplicated IDs for async commands */
4663 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4668 if (!has_exec_key
) {
4669 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4676 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4677 const QDict
*params
)
4680 QObject
*data
= NULL
;
4682 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4683 handler_audit(mon
, cmd
, ret
);
4684 monitor_protocol_emitter(mon
, data
);
4685 qobject_decref(data
);
4688 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4692 QDict
*input
, *args
;
4693 const mon_cmd_t
*cmd
;
4694 const char *cmd_name
;
4695 Monitor
*mon
= cur_mon
;
4697 args
= input
= NULL
;
4699 obj
= json_parser_parse(tokens
, NULL
);
4701 // FIXME: should be triggered in json_parser_parse()
4702 qerror_report(QERR_JSON_PARSING
);
4706 input
= qmp_check_input_obj(obj
);
4708 qobject_decref(obj
);
4712 mon
->mc
->id
= qdict_get(input
, "id");
4713 qobject_incref(mon
->mc
->id
);
4715 cmd_name
= qdict_get_str(input
, "execute");
4716 trace_handle_qmp_command(mon
, cmd_name
);
4717 if (invalid_qmp_mode(mon
, cmd_name
)) {
4718 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4722 cmd
= qmp_find_cmd(cmd_name
);
4724 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4728 obj
= qdict_get(input
, "arguments");
4732 args
= qobject_to_qdict(obj
);
4736 err
= qmp_check_client_args(cmd
, args
);
4741 if (handler_is_async(cmd
)) {
4742 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4744 /* emit the error response */
4748 qmp_call_cmd(mon
, cmd
, args
);
4754 monitor_protocol_emitter(mon
, NULL
);
4761 * monitor_control_read(): Read and handle QMP input
4763 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4765 Monitor
*old_mon
= cur_mon
;
4769 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4774 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4776 Monitor
*old_mon
= cur_mon
;
4782 for (i
= 0; i
< size
; i
++)
4783 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4785 if (size
== 0 || buf
[size
- 1] != 0)
4786 monitor_printf(cur_mon
, "corrupted command\n");
4788 handle_user_command(cur_mon
, (char *)buf
);
4794 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
4796 monitor_suspend(mon
);
4797 handle_user_command(mon
, cmdline
);
4798 monitor_resume(mon
);
4801 int monitor_suspend(Monitor
*mon
)
4809 void monitor_resume(Monitor
*mon
)
4813 if (--mon
->suspend_cnt
== 0)
4814 readline_show_prompt(mon
->rs
);
4817 static QObject
*get_qmp_greeting(void)
4819 QObject
*ver
= NULL
;
4821 qmp_marshal_input_query_version(NULL
, NULL
, &ver
);
4822 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4826 * monitor_control_event(): Print QMP gretting
4828 static void monitor_control_event(void *opaque
, int event
)
4831 Monitor
*mon
= opaque
;
4834 case CHR_EVENT_OPENED
:
4835 mon
->mc
->command_mode
= 0;
4836 data
= get_qmp_greeting();
4837 monitor_json_emitter(mon
, data
);
4838 qobject_decref(data
);
4841 case CHR_EVENT_CLOSED
:
4842 json_message_parser_destroy(&mon
->mc
->parser
);
4843 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4845 monitor_fdsets_cleanup();
4850 static void monitor_event(void *opaque
, int event
)
4852 Monitor
*mon
= opaque
;
4855 case CHR_EVENT_MUX_IN
:
4857 if (mon
->reset_seen
) {
4858 readline_restart(mon
->rs
);
4859 monitor_resume(mon
);
4862 mon
->suspend_cnt
= 0;
4866 case CHR_EVENT_MUX_OUT
:
4867 if (mon
->reset_seen
) {
4868 if (mon
->suspend_cnt
== 0) {
4869 monitor_printf(mon
, "\n");
4872 monitor_suspend(mon
);
4879 case CHR_EVENT_OPENED
:
4880 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4881 "information\n", QEMU_VERSION
);
4882 if (!mon
->mux_out
) {
4883 readline_show_prompt(mon
->rs
);
4885 mon
->reset_seen
= 1;
4889 case CHR_EVENT_CLOSED
:
4891 monitor_fdsets_cleanup();
4897 compare_mon_cmd(const void *a
, const void *b
)
4899 return strcmp(((const mon_cmd_t
*)a
)->name
,
4900 ((const mon_cmd_t
*)b
)->name
);
4903 static void sortcmdlist(void)
4906 int elem_size
= sizeof(mon_cmd_t
);
4908 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4909 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4911 array_num
= sizeof(info_cmds
)/elem_size
-1;
4912 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4924 void monitor_init(CharDriverState
*chr
, int flags
)
4926 static int is_first_init
= 1;
4929 if (is_first_init
) {
4930 key_timer
= qemu_new_timer_ns(vm_clock
, release_keys
, NULL
);
4931 monitor_protocol_event_init();
4935 mon
= g_malloc0(sizeof(*mon
));
4939 if (flags
& MONITOR_USE_READLINE
) {
4940 mon
->rs
= readline_init(mon
, monitor_find_completion
);
4941 monitor_read_command(mon
, 0);
4944 if (monitor_ctrl_mode(mon
)) {
4945 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
4946 /* Control mode requires special handlers */
4947 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
4948 monitor_control_event
, mon
);
4949 qemu_chr_fe_set_echo(chr
, true);
4951 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
4953 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4954 monitor_event
, mon
);
4957 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4958 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
4964 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
4966 BlockDriverState
*bs
= opaque
;
4969 if (bdrv_set_key(bs
, password
) != 0) {
4970 monitor_printf(mon
, "invalid password\n");
4973 if (mon
->password_completion_cb
)
4974 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4976 monitor_read_command(mon
, 1);
4979 ReadLineState
*monitor_get_rs(Monitor
*mon
)
4984 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4985 BlockDriverCompletionFunc
*completion_cb
,
4990 if (!bdrv_key_required(bs
)) {
4992 completion_cb(opaque
, 0);
4996 if (monitor_ctrl_mode(mon
)) {
4997 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
4998 bdrv_get_encrypted_filename(bs
));
5002 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5003 bdrv_get_encrypted_filename(bs
));
5005 mon
->password_completion_cb
= completion_cb
;
5006 mon
->password_opaque
= opaque
;
5008 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5010 if (err
&& completion_cb
)
5011 completion_cb(opaque
, err
);
5016 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
5017 BlockDriverCompletionFunc
*completion_cb
,
5020 BlockDriverState
*bs
;
5022 bs
= bdrv_find(device
);
5024 monitor_printf(mon
, "Device not found %s\n", device
);
5028 return monitor_read_bdrv_key_start(mon
, bs
, completion_cb
, opaque
);