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
26 #include "monitor/qdev.h"
28 #include "hw/pcmcia.h"
29 #include "hw/i386/pc.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
35 #include "net/slirp.h"
36 #include "sysemu/char.h"
37 #include "ui/qemu-spice.h"
38 #include "sysemu/sysemu.h"
39 #include "monitor/monitor.h"
40 #include "qemu/readline.h"
41 #include "ui/console.h"
43 #include "sysemu/blockdev.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "migration/migration.h"
49 #include "sysemu/kvm.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qint.h"
53 #include "qapi/qmp/qfloat.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qmp/qbool.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qapi/qmp/qjson.h"
58 #include "qapi/qmp/json-streamer.h"
59 #include "qapi/qmp/json-parser.h"
60 #include <qom/object_interfaces.h>
61 #include "qemu/osdep.h"
64 #include "trace/control.h"
65 #ifdef CONFIG_TRACE_SIMPLE
66 #include "trace/simple.h"
68 #include "exec/memory.h"
69 #include "exec/cpu_ldst.h"
70 #include "qmp-commands.h"
72 #include "qemu/thread.h"
74 /* for pic/irq_info */
75 #if defined(TARGET_SPARC)
76 #include "hw/sparc/sun4m.h"
78 #include "hw/lm32/lm32_pic.h"
81 //#define DEBUG_COMPLETION
87 * 'B' block device name
88 * 's' string (accept optional quote)
89 * 'S' it just appends the rest of the string (accept optional quote)
90 * 'O' option string of the form NAME=VALUE,...
91 * parsed according to QemuOptsList given by its name
92 * Example: 'device:O' uses qemu_device_opts.
93 * Restriction: only lists with empty desc are supported
94 * TODO lift the restriction
96 * 'l' target long (32 or 64 bit)
97 * 'M' Non-negative target long (32 or 64 bit), in user mode the
98 * value is multiplied by 2^20 (think Mebibyte)
99 * 'o' octets (aka bytes)
100 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
101 * K, k suffix, which multiplies the value by 2^60 for suffixes E
102 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
103 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
105 * user mode accepts an optional ms, us, ns suffix,
106 * which divides the value by 1e3, 1e6, 1e9, respectively
107 * '/' optional gdb-like print format (like "/10x")
109 * '?' optional type (for all types, except '/')
110 * '.' other form of optional type (for 'i' and 'l')
112 * user mode accepts "on" or "off"
113 * '-' optional parameter (eg. '-f')
117 typedef struct MonitorCompletionData MonitorCompletionData
;
118 struct MonitorCompletionData
{
120 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
123 typedef struct mon_cmd_t
{
125 const char *args_type
;
128 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
130 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
131 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
132 int (*cmd_async
)(Monitor
*mon
, const QDict
*params
,
133 MonitorCompletion
*cb
, void *opaque
);
136 /* @sub_table is a list of 2nd level of commands. If it do not exist,
137 * mhandler should be used. If it exist, sub_table[?].mhandler should be
138 * used, and mhandler of 1st level plays the role of help function.
140 struct mon_cmd_t
*sub_table
;
141 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
144 /* file descriptors passed via SCM_RIGHTS */
145 typedef struct mon_fd_t mon_fd_t
;
149 QLIST_ENTRY(mon_fd_t
) next
;
152 /* file descriptor associated with a file descriptor set */
153 typedef struct MonFdsetFd MonFdsetFd
;
158 QLIST_ENTRY(MonFdsetFd
) next
;
161 /* file descriptor set containing fds passed via SCM_RIGHTS */
162 typedef struct MonFdset MonFdset
;
165 QLIST_HEAD(, MonFdsetFd
) fds
;
166 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
167 QLIST_ENTRY(MonFdset
) next
;
170 typedef struct MonitorControl
{
172 JSONMessageParser parser
;
177 * To prevent flooding clients, events can be throttled. The
178 * throttling is calculated globally, rather than per-Monitor
181 typedef struct MonitorEventState
{
182 MonitorEvent event
; /* Event being tracked */
183 int64_t rate
; /* Period over which to throttle. 0 to disable */
184 int64_t last
; /* Time at which event was last emitted */
185 QEMUTimer
*timer
; /* Timer for handling delayed events */
186 QObject
*data
; /* Event pending delayed dispatch */
190 CharDriverState
*chr
;
201 BlockDriverCompletionFunc
*password_completion_cb
;
202 void *password_opaque
;
203 mon_cmd_t
*cmd_table
;
205 QLIST_HEAD(,mon_fd_t
) fds
;
206 QLIST_ENTRY(Monitor
) entry
;
209 /* QMP checker flags */
210 #define QMP_ACCEPT_UNKNOWNS 1
212 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
213 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
214 static int mon_refcount
;
216 static mon_cmd_t mon_cmds
[];
217 static mon_cmd_t info_cmds
[];
219 static const mon_cmd_t qmp_cmds
[];
222 Monitor
*default_mon
;
224 static void monitor_command_cb(void *opaque
, const char *cmdline
,
225 void *readline_opaque
);
227 static inline int qmp_cmd_mode(const Monitor
*mon
)
229 return (mon
->mc
? mon
->mc
->command_mode
: 0);
232 /* Return true if in control mode, false otherwise */
233 static inline int monitor_ctrl_mode(const Monitor
*mon
)
235 return (mon
->flags
& MONITOR_USE_CONTROL
);
238 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
239 int monitor_cur_is_qmp(void)
241 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
244 void monitor_read_command(Monitor
*mon
, int show_prompt
)
249 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
251 readline_show_prompt(mon
->rs
);
254 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
257 if (monitor_ctrl_mode(mon
)) {
258 qerror_report(QERR_MISSING_PARAMETER
, "password");
260 } else if (mon
->rs
) {
261 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
262 /* prompt is printed on return from the command handler */
265 monitor_printf(mon
, "terminal does not support password prompting\n");
270 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
273 Monitor
*mon
= opaque
;
280 void monitor_flush(Monitor
*mon
)
286 if (mon
->skip_flush
) {
290 buf
= qstring_get_str(mon
->outbuf
);
291 len
= qstring_get_length(mon
->outbuf
);
293 if (len
&& !mon
->mux_out
) {
294 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
295 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
296 /* all flushed or error */
297 QDECREF(mon
->outbuf
);
298 mon
->outbuf
= qstring_new();
303 QString
*tmp
= qstring_from_str(buf
+ rc
);
304 QDECREF(mon
->outbuf
);
307 if (mon
->watch
== 0) {
308 mon
->watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
,
309 monitor_unblocked
, mon
);
314 /* flush at every end of line */
315 static void monitor_puts(Monitor
*mon
, const char *str
)
324 qstring_append_chr(mon
->outbuf
, '\r');
326 qstring_append_chr(mon
->outbuf
, c
);
333 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
340 if (monitor_ctrl_mode(mon
)) {
344 buf
= g_strdup_vprintf(fmt
, ap
);
345 monitor_puts(mon
, buf
);
349 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
353 monitor_vprintf(mon
, fmt
, ap
);
357 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
358 const char *fmt
, ...)
362 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
367 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
369 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
371 return cmd
->user_print
!= NULL
;
374 static inline bool handler_is_async(const mon_cmd_t
*cmd
)
376 return cmd
->flags
& MONITOR_CMD_ASYNC
;
379 static inline int monitor_has_error(const Monitor
*mon
)
381 return mon
->error
!= NULL
;
384 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
388 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
389 qobject_to_json(data
);
390 assert(json
!= NULL
);
392 qstring_append_chr(json
, '\n');
393 monitor_puts(mon
, qstring_get_str(json
));
398 static QDict
*build_qmp_error_dict(const QError
*err
)
402 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
403 ErrorClass_lookup
[err
->err_class
],
406 return qobject_to_qdict(obj
);
409 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
413 trace_monitor_protocol_emitter(mon
);
415 if (!monitor_has_error(mon
)) {
416 /* success response */
419 qobject_incref(data
);
420 qdict_put_obj(qmp
, "return", data
);
422 /* return an empty QDict by default */
423 qdict_put(qmp
, "return", qdict_new());
427 qmp
= build_qmp_error_dict(mon
->error
);
433 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
437 monitor_json_emitter(mon
, QOBJECT(qmp
));
441 static void timestamp_put(QDict
*qdict
)
447 err
= qemu_gettimeofday(&tv
);
451 obj
= qobject_from_jsonf("{ 'seconds': %" PRId64
", "
452 "'microseconds': %" PRId64
" }",
453 (int64_t) tv
.tv_sec
, (int64_t) tv
.tv_usec
);
454 qdict_put_obj(qdict
, "timestamp", obj
);
458 static const char *monitor_event_names
[] = {
459 [QEVENT_SHUTDOWN
] = "SHUTDOWN",
460 [QEVENT_RESET
] = "RESET",
461 [QEVENT_POWERDOWN
] = "POWERDOWN",
462 [QEVENT_STOP
] = "STOP",
463 [QEVENT_RESUME
] = "RESUME",
464 [QEVENT_VNC_CONNECTED
] = "VNC_CONNECTED",
465 [QEVENT_VNC_INITIALIZED
] = "VNC_INITIALIZED",
466 [QEVENT_VNC_DISCONNECTED
] = "VNC_DISCONNECTED",
467 [QEVENT_BLOCK_IO_ERROR
] = "BLOCK_IO_ERROR",
468 [QEVENT_RTC_CHANGE
] = "RTC_CHANGE",
469 [QEVENT_WATCHDOG
] = "WATCHDOG",
470 [QEVENT_SPICE_CONNECTED
] = "SPICE_CONNECTED",
471 [QEVENT_SPICE_INITIALIZED
] = "SPICE_INITIALIZED",
472 [QEVENT_SPICE_DISCONNECTED
] = "SPICE_DISCONNECTED",
473 [QEVENT_BLOCK_JOB_COMPLETED
] = "BLOCK_JOB_COMPLETED",
474 [QEVENT_BLOCK_JOB_CANCELLED
] = "BLOCK_JOB_CANCELLED",
475 [QEVENT_BLOCK_JOB_ERROR
] = "BLOCK_JOB_ERROR",
476 [QEVENT_BLOCK_JOB_READY
] = "BLOCK_JOB_READY",
477 [QEVENT_DEVICE_DELETED
] = "DEVICE_DELETED",
478 [QEVENT_DEVICE_TRAY_MOVED
] = "DEVICE_TRAY_MOVED",
479 [QEVENT_NIC_RX_FILTER_CHANGED
] = "NIC_RX_FILTER_CHANGED",
480 [QEVENT_SUSPEND
] = "SUSPEND",
481 [QEVENT_SUSPEND_DISK
] = "SUSPEND_DISK",
482 [QEVENT_WAKEUP
] = "WAKEUP",
483 [QEVENT_BALLOON_CHANGE
] = "BALLOON_CHANGE",
484 [QEVENT_SPICE_MIGRATE_COMPLETED
] = "SPICE_MIGRATE_COMPLETED",
485 [QEVENT_GUEST_PANICKED
] = "GUEST_PANICKED",
486 [QEVENT_BLOCK_IMAGE_CORRUPTED
] = "BLOCK_IMAGE_CORRUPTED",
487 [QEVENT_QUORUM_FAILURE
] = "QUORUM_FAILURE",
488 [QEVENT_QUORUM_REPORT_BAD
] = "QUORUM_REPORT_BAD",
490 QEMU_BUILD_BUG_ON(ARRAY_SIZE(monitor_event_names
) != QEVENT_MAX
)
492 static MonitorEventState monitor_event_state
[QEVENT_MAX
];
495 * Emits the event to every monitor instance
498 monitor_protocol_event_emit(MonitorEvent event
,
503 trace_monitor_protocol_event_emit(event
, data
);
504 QLIST_FOREACH(mon
, &mon_list
, entry
) {
505 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
506 monitor_json_emitter(mon
, data
);
513 * Queue a new event for emission to Monitor instances,
514 * applying any rate limiting if required.
517 monitor_protocol_event_queue(MonitorEvent event
,
520 MonitorEventState
*evstate
;
521 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
522 assert(event
< QEVENT_MAX
);
524 evstate
= &(monitor_event_state
[event
]);
525 trace_monitor_protocol_event_queue(event
,
531 /* Rate limit of 0 indicates no throttling */
532 if (!evstate
->rate
) {
533 monitor_protocol_event_emit(event
, data
);
536 int64_t delta
= now
- evstate
->last
;
538 delta
< evstate
->rate
) {
539 /* If there's an existing event pending, replace
540 * it with the new event, otherwise schedule a
541 * timer for delayed emission
544 qobject_decref(evstate
->data
);
546 int64_t then
= evstate
->last
+ evstate
->rate
;
547 timer_mod_ns(evstate
->timer
, then
);
549 evstate
->data
= data
;
550 qobject_incref(evstate
->data
);
552 monitor_protocol_event_emit(event
, data
);
560 * The callback invoked by QemuTimer when a delayed
561 * event is ready to be emitted
563 static void monitor_protocol_event_handler(void *opaque
)
565 MonitorEventState
*evstate
= opaque
;
566 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
569 trace_monitor_protocol_event_handler(evstate
->event
,
574 monitor_protocol_event_emit(evstate
->event
, evstate
->data
);
575 qobject_decref(evstate
->data
);
576 evstate
->data
= NULL
;
583 * @event: the event ID to be limited
584 * @rate: the rate limit in milliseconds
586 * Sets a rate limit on a particular event, so no
587 * more than 1 event will be emitted within @rate
591 monitor_protocol_event_throttle(MonitorEvent event
,
594 MonitorEventState
*evstate
;
595 assert(event
< QEVENT_MAX
);
597 evstate
= &(monitor_event_state
[event
]);
599 trace_monitor_protocol_event_throttle(event
, rate
);
600 evstate
->event
= event
;
601 evstate
->rate
= rate
* SCALE_MS
;
602 evstate
->timer
= timer_new(QEMU_CLOCK_REALTIME
,
604 monitor_protocol_event_handler
,
607 evstate
->data
= NULL
;
611 /* Global, one-time initializer to configure the rate limiting
612 * and initialize state */
613 static void monitor_protocol_event_init(void)
615 /* Limit RTC & BALLOON events to 1 per second */
616 monitor_protocol_event_throttle(QEVENT_RTC_CHANGE
, 1000);
617 monitor_protocol_event_throttle(QEVENT_BALLOON_CHANGE
, 1000);
618 monitor_protocol_event_throttle(QEVENT_WATCHDOG
, 1000);
619 /* limit the rate of quorum events to avoid hammering the management */
620 monitor_protocol_event_throttle(QEVENT_QUORUM_REPORT_BAD
, 1000);
621 monitor_protocol_event_throttle(QEVENT_QUORUM_FAILURE
, 1000);
625 * monitor_protocol_event(): Generate a Monitor event
627 * Event-specific data can be emitted through the (optional) 'data' parameter.
629 void monitor_protocol_event(MonitorEvent event
, QObject
*data
)
632 const char *event_name
;
634 assert(event
< QEVENT_MAX
);
636 event_name
= monitor_event_names
[event
];
637 assert(event_name
!= NULL
);
641 qdict_put(qmp
, "event", qstring_from_str(event_name
));
643 qobject_incref(data
);
644 qdict_put_obj(qmp
, "data", data
);
647 trace_monitor_protocol_event(event
, event_name
, qmp
);
648 monitor_protocol_event_queue(event
, QOBJECT(qmp
));
652 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
655 /* Will setup QMP capabilities in the future */
656 if (monitor_ctrl_mode(mon
)) {
657 mon
->mc
->command_mode
= 1;
663 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
665 static void monitor_data_init(Monitor
*mon
)
667 memset(mon
, 0, sizeof(Monitor
));
668 mon
->outbuf
= qstring_new();
669 /* Use *mon_cmds by default. */
670 mon
->cmd_table
= mon_cmds
;
673 static void monitor_data_destroy(Monitor
*mon
)
675 QDECREF(mon
->outbuf
);
678 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
679 int64_t cpu_index
, Error
**errp
)
682 Monitor
*old_mon
, hmp
;
684 monitor_data_init(&hmp
);
685 hmp
.skip_flush
= true;
691 int ret
= monitor_set_cpu(cpu_index
);
694 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
700 handle_user_command(&hmp
, command_line
);
703 if (qstring_get_length(hmp
.outbuf
) > 0) {
704 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
706 output
= g_strdup("");
710 monitor_data_destroy(&hmp
);
714 static int compare_cmd(const char *name
, const char *list
)
716 const char *p
, *pstart
;
724 p
= pstart
+ strlen(pstart
);
725 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
734 static int get_str(char *buf
, int buf_size
, const char **pp
)
742 while (qemu_isspace(*p
)) {
753 while (*p
!= '\0' && *p
!= '\"') {
769 qemu_printf("unsupported escape code: '\\%c'\n", c
);
772 if ((q
- buf
) < buf_size
- 1) {
776 if ((q
- buf
) < buf_size
- 1) {
783 qemu_printf("unterminated string\n");
788 while (*p
!= '\0' && !qemu_isspace(*p
)) {
789 if ((q
- buf
) < buf_size
- 1) {
802 static void free_cmdline_args(char **args
, int nb_args
)
806 assert(nb_args
<= MAX_ARGS
);
808 for (i
= 0; i
< nb_args
; i
++) {
815 * Parse the command line to get valid args.
816 * @cmdline: command line to be parsed.
817 * @pnb_args: location to store the number of args, must NOT be NULL.
818 * @args: location to store the args, which should be freed by caller, must
821 * Returns 0 on success, negative on failure.
823 * NOTE: this parser is an approximate form of the real command parser. Number
824 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
825 * return with failure.
827 static int parse_cmdline(const char *cmdline
,
828 int *pnb_args
, char **args
)
837 while (qemu_isspace(*p
)) {
843 if (nb_args
>= MAX_ARGS
) {
846 ret
= get_str(buf
, sizeof(buf
), &p
);
850 args
[nb_args
] = g_strdup(buf
);
857 free_cmdline_args(args
, nb_args
);
861 static void help_cmd_dump_one(Monitor
*mon
,
862 const mon_cmd_t
*cmd
,
868 for (i
= 0; i
< prefix_args_nb
; i
++) {
869 monitor_printf(mon
, "%s ", prefix_args
[i
]);
871 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
874 /* @args[@arg_index] is the valid command need to find in @cmds */
875 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
876 char **args
, int nb_args
, int arg_index
)
878 const mon_cmd_t
*cmd
;
880 /* No valid arg need to compare with, dump all in *cmds */
881 if (arg_index
>= nb_args
) {
882 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
883 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
888 /* Find one entry to dump */
889 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
890 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
891 if (cmd
->sub_table
) {
892 /* continue with next arg */
893 help_cmd_dump(mon
, cmd
->sub_table
,
894 args
, nb_args
, arg_index
+ 1);
896 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
903 static void help_cmd(Monitor
*mon
, const char *name
)
905 char *args
[MAX_ARGS
];
908 /* 1. parse user input */
910 /* special case for log, directly dump and return */
911 if (!strcmp(name
, "log")) {
912 const QEMULogItem
*item
;
913 monitor_printf(mon
, "Log items (comma separated):\n");
914 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
915 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
916 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
921 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
926 /* 2. dump the contents according to parsed args */
927 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
929 free_cmdline_args(args
, nb_args
);
932 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
934 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
937 static void do_trace_event_set_state(Monitor
*mon
, const QDict
*qdict
)
939 const char *tp_name
= qdict_get_str(qdict
, "name");
940 bool new_state
= qdict_get_bool(qdict
, "option");
943 TraceEvent
*ev
= NULL
;
944 while ((ev
= trace_event_pattern(tp_name
, ev
)) != NULL
) {
946 if (!trace_event_get_state_static(ev
)) {
947 monitor_printf(mon
, "event \"%s\" is not traceable\n", tp_name
);
949 trace_event_set_state_dynamic(ev
, new_state
);
952 if (!trace_event_is_pattern(tp_name
) && !found
) {
953 monitor_printf(mon
, "unknown event name \"%s\"\n", tp_name
);
957 #ifdef CONFIG_TRACE_SIMPLE
958 static void do_trace_file(Monitor
*mon
, const QDict
*qdict
)
960 const char *op
= qdict_get_try_str(qdict
, "op");
961 const char *arg
= qdict_get_try_str(qdict
, "arg");
964 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
965 } else if (!strcmp(op
, "on")) {
966 st_set_trace_file_enabled(true);
967 } else if (!strcmp(op
, "off")) {
968 st_set_trace_file_enabled(false);
969 } else if (!strcmp(op
, "flush")) {
970 st_flush_trace_buffer();
971 } else if (!strcmp(op
, "set")) {
973 st_set_trace_file(arg
);
976 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
977 help_cmd(mon
, "trace-file");
982 static void user_monitor_complete(void *opaque
, QObject
*ret_data
)
984 MonitorCompletionData
*data
= (MonitorCompletionData
*)opaque
;
987 data
->user_print(data
->mon
, ret_data
);
989 monitor_resume(data
->mon
);
993 static void qmp_monitor_complete(void *opaque
, QObject
*ret_data
)
995 monitor_protocol_emitter(opaque
, ret_data
);
998 static int qmp_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
1001 return cmd
->mhandler
.cmd_async(mon
, params
, qmp_monitor_complete
, mon
);
1004 static void user_async_cmd_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
1005 const QDict
*params
)
1009 MonitorCompletionData
*cb_data
= g_malloc(sizeof(*cb_data
));
1011 cb_data
->user_print
= cmd
->user_print
;
1012 monitor_suspend(mon
);
1013 ret
= cmd
->mhandler
.cmd_async(mon
, params
,
1014 user_monitor_complete
, cb_data
);
1016 monitor_resume(mon
);
1021 static void do_info_help(Monitor
*mon
, const QDict
*qdict
)
1023 help_cmd(mon
, "info");
1026 CommandInfoList
*qmp_query_commands(Error
**errp
)
1028 CommandInfoList
*info
, *cmd_list
= NULL
;
1029 const mon_cmd_t
*cmd
;
1031 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
1032 info
= g_malloc0(sizeof(*info
));
1033 info
->value
= g_malloc0(sizeof(*info
->value
));
1034 info
->value
->name
= g_strdup(cmd
->name
);
1036 info
->next
= cmd_list
;
1043 EventInfoList
*qmp_query_events(Error
**errp
)
1045 EventInfoList
*info
, *ev_list
= NULL
;
1048 for (e
= 0 ; e
< QEVENT_MAX
; e
++) {
1049 const char *event_name
= monitor_event_names
[e
];
1050 assert(event_name
!= NULL
);
1051 info
= g_malloc0(sizeof(*info
));
1052 info
->value
= g_malloc0(sizeof(*info
->value
));
1053 info
->value
->name
= g_strdup(event_name
);
1055 info
->next
= ev_list
;
1062 /* set the current CPU defined by the user */
1063 int monitor_set_cpu(int cpu_index
)
1067 cpu
= qemu_get_cpu(cpu_index
);
1071 cur_mon
->mon_cpu
= cpu
;
1075 static CPUArchState
*mon_get_cpu(void)
1077 if (!cur_mon
->mon_cpu
) {
1080 cpu_synchronize_state(cur_mon
->mon_cpu
);
1081 return cur_mon
->mon_cpu
->env_ptr
;
1084 int monitor_get_cpu_index(void)
1086 CPUState
*cpu
= ENV_GET_CPU(mon_get_cpu());
1087 return cpu
->cpu_index
;
1090 static void do_info_registers(Monitor
*mon
, const QDict
*qdict
)
1094 env
= mon_get_cpu();
1095 cpu
= ENV_GET_CPU(env
);
1096 cpu_dump_state(cpu
, (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1099 static void do_info_jit(Monitor
*mon
, const QDict
*qdict
)
1101 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1104 static void do_info_history(Monitor
*mon
, const QDict
*qdict
)
1113 str
= readline_get_history(mon
->rs
, i
);
1116 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1121 static void do_info_cpu_stats(Monitor
*mon
, const QDict
*qdict
)
1126 env
= mon_get_cpu();
1127 cpu
= ENV_GET_CPU(env
);
1128 cpu_dump_statistics(cpu
, (FILE *)mon
, &monitor_fprintf
, 0);
1131 static void do_trace_print_events(Monitor
*mon
, const QDict
*qdict
)
1133 trace_print_events((FILE *)mon
, &monitor_fprintf
);
1136 static int client_migrate_info(Monitor
*mon
, const QDict
*qdict
,
1137 MonitorCompletion cb
, void *opaque
)
1139 const char *protocol
= qdict_get_str(qdict
, "protocol");
1140 const char *hostname
= qdict_get_str(qdict
, "hostname");
1141 const char *subject
= qdict_get_try_str(qdict
, "cert-subject");
1142 int port
= qdict_get_try_int(qdict
, "port", -1);
1143 int tls_port
= qdict_get_try_int(qdict
, "tls-port", -1);
1146 if (strcmp(protocol
, "spice") == 0) {
1148 qerror_report(QERR_DEVICE_NOT_ACTIVE
, "spice");
1152 if (port
== -1 && tls_port
== -1) {
1153 qerror_report(QERR_MISSING_PARAMETER
, "port/tls-port");
1157 ret
= qemu_spice_migrate_info(hostname
, port
, tls_port
, subject
,
1160 qerror_report(QERR_UNDEFINED_ERROR
);
1166 qerror_report(QERR_INVALID_PARAMETER
, "protocol");
1170 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
1172 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1175 static void do_log(Monitor
*mon
, const QDict
*qdict
)
1178 const char *items
= qdict_get_str(qdict
, "items");
1180 if (!strcmp(items
, "none")) {
1183 mask
= qemu_str_to_log_mask(items
);
1185 help_cmd(mon
, "log");
1192 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
1194 const char *option
= qdict_get_try_str(qdict
, "option");
1195 if (!option
|| !strcmp(option
, "on")) {
1197 } else if (!strcmp(option
, "off")) {
1200 monitor_printf(mon
, "unexpected option %s\n", option
);
1204 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1206 const char *device
= qdict_get_try_str(qdict
, "device");
1208 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1209 if (gdbserver_start(device
) < 0) {
1210 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1212 } else if (strcmp(device
, "none") == 0) {
1213 monitor_printf(mon
, "Disabled gdbserver\n");
1215 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1220 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1222 const char *action
= qdict_get_str(qdict
, "action");
1223 if (select_watchdog_action(action
) == -1) {
1224 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1228 static void monitor_printc(Monitor
*mon
, int c
)
1230 monitor_printf(mon
, "'");
1233 monitor_printf(mon
, "\\'");
1236 monitor_printf(mon
, "\\\\");
1239 monitor_printf(mon
, "\\n");
1242 monitor_printf(mon
, "\\r");
1245 if (c
>= 32 && c
<= 126) {
1246 monitor_printf(mon
, "%c", c
);
1248 monitor_printf(mon
, "\\x%02x", c
);
1252 monitor_printf(mon
, "'");
1255 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1256 hwaddr addr
, int is_physical
)
1259 int l
, line_size
, i
, max_digits
, len
;
1263 if (format
== 'i') {
1266 env
= mon_get_cpu();
1270 } else if (wsize
== 4) {
1273 /* as default we use the current CS size */
1276 #ifdef TARGET_X86_64
1277 if ((env
->efer
& MSR_EFER_LMA
) &&
1278 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1282 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1287 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1291 len
= wsize
* count
;
1300 max_digits
= (wsize
* 8 + 2) / 3;
1304 max_digits
= (wsize
* 8) / 4;
1308 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1317 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1319 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1324 cpu_physical_memory_read(addr
, buf
, l
);
1326 env
= mon_get_cpu();
1327 if (cpu_memory_rw_debug(ENV_GET_CPU(env
), addr
, buf
, l
, 0) < 0) {
1328 monitor_printf(mon
, " Cannot access memory\n");
1337 v
= ldub_raw(buf
+ i
);
1340 v
= lduw_raw(buf
+ i
);
1343 v
= (uint32_t)ldl_raw(buf
+ i
);
1346 v
= ldq_raw(buf
+ i
);
1349 monitor_printf(mon
, " ");
1352 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1355 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1358 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1361 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1364 monitor_printc(mon
, v
);
1369 monitor_printf(mon
, "\n");
1375 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1377 int count
= qdict_get_int(qdict
, "count");
1378 int format
= qdict_get_int(qdict
, "format");
1379 int size
= qdict_get_int(qdict
, "size");
1380 target_long addr
= qdict_get_int(qdict
, "addr");
1382 memory_dump(mon
, count
, format
, size
, addr
, 0);
1385 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1387 int count
= qdict_get_int(qdict
, "count");
1388 int format
= qdict_get_int(qdict
, "format");
1389 int size
= qdict_get_int(qdict
, "size");
1390 hwaddr addr
= qdict_get_int(qdict
, "addr");
1392 memory_dump(mon
, count
, format
, size
, addr
, 1);
1395 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1397 int format
= qdict_get_int(qdict
, "format");
1398 hwaddr val
= qdict_get_int(qdict
, "val");
1402 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1405 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1408 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1412 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1415 monitor_printc(mon
, val
);
1418 monitor_printf(mon
, "\n");
1421 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1425 uint32_t start
= qdict_get_int(qdict
, "start");
1426 uint32_t size
= qdict_get_int(qdict
, "size");
1429 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1430 uint8_t val
= ldub_phys(&address_space_memory
, addr
);
1431 /* BSD sum algorithm ('sum' Unix command) */
1432 sum
= (sum
>> 1) | (sum
<< 15);
1435 monitor_printf(mon
, "%05d\n", sum
);
1438 static int mouse_button_state
;
1440 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1442 int dx
, dy
, dz
, button
;
1443 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1444 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1445 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1447 dx
= strtol(dx_str
, NULL
, 0);
1448 dy
= strtol(dy_str
, NULL
, 0);
1449 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1450 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1453 dz
= strtol(dz_str
, NULL
, 0);
1455 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1456 qemu_input_queue_btn(NULL
, button
, true);
1457 qemu_input_event_sync();
1458 qemu_input_queue_btn(NULL
, button
, false);
1461 qemu_input_event_sync();
1464 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1466 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
1467 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1468 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1469 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1471 int button_state
= qdict_get_int(qdict
, "button_state");
1473 if (mouse_button_state
== button_state
) {
1476 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1477 qemu_input_event_sync();
1478 mouse_button_state
= button_state
;
1481 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1483 int size
= qdict_get_int(qdict
, "size");
1484 int addr
= qdict_get_int(qdict
, "addr");
1485 int has_index
= qdict_haskey(qdict
, "index");
1490 int index
= qdict_get_int(qdict
, "index");
1491 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1499 val
= cpu_inb(addr
);
1503 val
= cpu_inw(addr
);
1507 val
= cpu_inl(addr
);
1511 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1512 suffix
, addr
, size
* 2, val
);
1515 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1517 int size
= qdict_get_int(qdict
, "size");
1518 int addr
= qdict_get_int(qdict
, "addr");
1519 int val
= qdict_get_int(qdict
, "val");
1521 addr
&= IOPORTS_MASK
;
1526 cpu_outb(addr
, val
);
1529 cpu_outw(addr
, val
);
1532 cpu_outl(addr
, val
);
1537 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1540 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1542 res
= qemu_boot_set(bootdevice
);
1544 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1545 } else if (res
> 0) {
1546 monitor_printf(mon
, "setting boot device list failed\n");
1548 monitor_printf(mon
, "no function defined to set boot device list for "
1549 "this architecture\n");
1553 #if defined(TARGET_I386)
1554 static void print_pte(Monitor
*mon
, hwaddr addr
,
1558 #ifdef TARGET_X86_64
1559 if (addr
& (1ULL << 47)) {
1563 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1564 " %c%c%c%c%c%c%c%c%c\n",
1567 pte
& PG_NX_MASK
? 'X' : '-',
1568 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1569 pte
& PG_PSE_MASK
? 'P' : '-',
1570 pte
& PG_DIRTY_MASK
? 'D' : '-',
1571 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1572 pte
& PG_PCD_MASK
? 'C' : '-',
1573 pte
& PG_PWT_MASK
? 'T' : '-',
1574 pte
& PG_USER_MASK
? 'U' : '-',
1575 pte
& PG_RW_MASK
? 'W' : '-');
1578 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
1580 unsigned int l1
, l2
;
1581 uint32_t pgd
, pde
, pte
;
1583 pgd
= env
->cr
[3] & ~0xfff;
1584 for(l1
= 0; l1
< 1024; l1
++) {
1585 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1586 pde
= le32_to_cpu(pde
);
1587 if (pde
& PG_PRESENT_MASK
) {
1588 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1590 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1592 for(l2
= 0; l2
< 1024; l2
++) {
1593 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1594 pte
= le32_to_cpu(pte
);
1595 if (pte
& PG_PRESENT_MASK
) {
1596 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1606 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1608 unsigned int l1
, l2
, l3
;
1609 uint64_t pdpe
, pde
, pte
;
1610 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1612 pdp_addr
= env
->cr
[3] & ~0x1f;
1613 for (l1
= 0; l1
< 4; l1
++) {
1614 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1615 pdpe
= le64_to_cpu(pdpe
);
1616 if (pdpe
& PG_PRESENT_MASK
) {
1617 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1618 for (l2
= 0; l2
< 512; l2
++) {
1619 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1620 pde
= le64_to_cpu(pde
);
1621 if (pde
& PG_PRESENT_MASK
) {
1622 if (pde
& PG_PSE_MASK
) {
1623 /* 2M pages with PAE, CR4.PSE is ignored */
1624 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1625 ~((hwaddr
)(1 << 20) - 1));
1627 pt_addr
= pde
& 0x3fffffffff000ULL
;
1628 for (l3
= 0; l3
< 512; l3
++) {
1629 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1630 pte
= le64_to_cpu(pte
);
1631 if (pte
& PG_PRESENT_MASK
) {
1632 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1645 #ifdef TARGET_X86_64
1646 static void tlb_info_64(Monitor
*mon
, CPUArchState
*env
)
1648 uint64_t l1
, l2
, l3
, l4
;
1649 uint64_t pml4e
, pdpe
, pde
, pte
;
1650 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1652 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1653 for (l1
= 0; l1
< 512; l1
++) {
1654 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1655 pml4e
= le64_to_cpu(pml4e
);
1656 if (pml4e
& PG_PRESENT_MASK
) {
1657 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1658 for (l2
= 0; l2
< 512; l2
++) {
1659 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1660 pdpe
= le64_to_cpu(pdpe
);
1661 if (pdpe
& PG_PRESENT_MASK
) {
1662 if (pdpe
& PG_PSE_MASK
) {
1663 /* 1G pages, CR4.PSE is ignored */
1664 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1665 0x3ffffc0000000ULL
);
1667 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1668 for (l3
= 0; l3
< 512; l3
++) {
1669 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1670 pde
= le64_to_cpu(pde
);
1671 if (pde
& PG_PRESENT_MASK
) {
1672 if (pde
& PG_PSE_MASK
) {
1673 /* 2M pages, CR4.PSE is ignored */
1674 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1676 0x3ffffffe00000ULL
);
1678 pt_addr
= pde
& 0x3fffffffff000ULL
;
1679 for (l4
= 0; l4
< 512; l4
++) {
1680 cpu_physical_memory_read(pt_addr
1683 pte
= le64_to_cpu(pte
);
1684 if (pte
& PG_PRESENT_MASK
) {
1685 print_pte(mon
, (l1
<< 39) +
1687 (l3
<< 21) + (l4
<< 12),
1689 0x3fffffffff000ULL
);
1703 static void tlb_info(Monitor
*mon
, const QDict
*qdict
)
1707 env
= mon_get_cpu();
1709 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1710 monitor_printf(mon
, "PG disabled\n");
1713 if (env
->cr
[4] & CR4_PAE_MASK
) {
1714 #ifdef TARGET_X86_64
1715 if (env
->hflags
& HF_LMA_MASK
) {
1716 tlb_info_64(mon
, env
);
1720 tlb_info_pae32(mon
, env
);
1723 tlb_info_32(mon
, env
);
1727 static void mem_print(Monitor
*mon
, hwaddr
*pstart
,
1729 hwaddr end
, int prot
)
1732 prot1
= *plast_prot
;
1733 if (prot
!= prot1
) {
1734 if (*pstart
!= -1) {
1735 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
1736 TARGET_FMT_plx
" %c%c%c\n",
1737 *pstart
, end
, end
- *pstart
,
1738 prot1
& PG_USER_MASK
? 'u' : '-',
1740 prot1
& PG_RW_MASK
? 'w' : '-');
1750 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
1752 unsigned int l1
, l2
;
1753 int prot
, last_prot
;
1754 uint32_t pgd
, pde
, pte
;
1757 pgd
= env
->cr
[3] & ~0xfff;
1760 for(l1
= 0; l1
< 1024; l1
++) {
1761 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1762 pde
= le32_to_cpu(pde
);
1764 if (pde
& PG_PRESENT_MASK
) {
1765 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1766 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1767 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1769 for(l2
= 0; l2
< 1024; l2
++) {
1770 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1771 pte
= le32_to_cpu(pte
);
1772 end
= (l1
<< 22) + (l2
<< 12);
1773 if (pte
& PG_PRESENT_MASK
) {
1775 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1779 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1784 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1787 /* Flush last range */
1788 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1791 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1793 unsigned int l1
, l2
, l3
;
1794 int prot
, last_prot
;
1795 uint64_t pdpe
, pde
, pte
;
1796 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1799 pdp_addr
= env
->cr
[3] & ~0x1f;
1802 for (l1
= 0; l1
< 4; l1
++) {
1803 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1804 pdpe
= le64_to_cpu(pdpe
);
1806 if (pdpe
& PG_PRESENT_MASK
) {
1807 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1808 for (l2
= 0; l2
< 512; l2
++) {
1809 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1810 pde
= le64_to_cpu(pde
);
1811 end
= (l1
<< 30) + (l2
<< 21);
1812 if (pde
& PG_PRESENT_MASK
) {
1813 if (pde
& PG_PSE_MASK
) {
1814 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1816 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1818 pt_addr
= pde
& 0x3fffffffff000ULL
;
1819 for (l3
= 0; l3
< 512; l3
++) {
1820 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1821 pte
= le64_to_cpu(pte
);
1822 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
1823 if (pte
& PG_PRESENT_MASK
) {
1824 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
1829 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1834 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1839 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1842 /* Flush last range */
1843 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1847 #ifdef TARGET_X86_64
1848 static void mem_info_64(Monitor
*mon
, CPUArchState
*env
)
1850 int prot
, last_prot
;
1851 uint64_t l1
, l2
, l3
, l4
;
1852 uint64_t pml4e
, pdpe
, pde
, pte
;
1853 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
1855 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1858 for (l1
= 0; l1
< 512; l1
++) {
1859 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1860 pml4e
= le64_to_cpu(pml4e
);
1862 if (pml4e
& PG_PRESENT_MASK
) {
1863 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1864 for (l2
= 0; l2
< 512; l2
++) {
1865 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1866 pdpe
= le64_to_cpu(pdpe
);
1867 end
= (l1
<< 39) + (l2
<< 30);
1868 if (pdpe
& PG_PRESENT_MASK
) {
1869 if (pdpe
& PG_PSE_MASK
) {
1870 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
1873 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1875 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1876 for (l3
= 0; l3
< 512; l3
++) {
1877 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1878 pde
= le64_to_cpu(pde
);
1879 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
1880 if (pde
& PG_PRESENT_MASK
) {
1881 if (pde
& PG_PSE_MASK
) {
1882 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1884 prot
&= pml4e
& pdpe
;
1885 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1887 pt_addr
= pde
& 0x3fffffffff000ULL
;
1888 for (l4
= 0; l4
< 512; l4
++) {
1889 cpu_physical_memory_read(pt_addr
1892 pte
= le64_to_cpu(pte
);
1893 end
= (l1
<< 39) + (l2
<< 30) +
1894 (l3
<< 21) + (l4
<< 12);
1895 if (pte
& PG_PRESENT_MASK
) {
1896 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
1898 prot
&= pml4e
& pdpe
& pde
;
1902 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1907 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1913 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1918 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1921 /* Flush last range */
1922 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 48, 0);
1926 static void mem_info(Monitor
*mon
, const QDict
*qdict
)
1930 env
= mon_get_cpu();
1932 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1933 monitor_printf(mon
, "PG disabled\n");
1936 if (env
->cr
[4] & CR4_PAE_MASK
) {
1937 #ifdef TARGET_X86_64
1938 if (env
->hflags
& HF_LMA_MASK
) {
1939 mem_info_64(mon
, env
);
1943 mem_info_pae32(mon
, env
);
1946 mem_info_32(mon
, env
);
1951 #if defined(TARGET_SH4)
1953 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1955 monitor_printf(mon
, " tlb%i:\t"
1956 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1957 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1958 "dirty=%hhu writethrough=%hhu\n",
1960 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1961 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1965 static void tlb_info(Monitor
*mon
, const QDict
*qdict
)
1967 CPUArchState
*env
= mon_get_cpu();
1970 monitor_printf (mon
, "ITLB:\n");
1971 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1972 print_tlb (mon
, i
, &env
->itlb
[i
]);
1973 monitor_printf (mon
, "UTLB:\n");
1974 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1975 print_tlb (mon
, i
, &env
->utlb
[i
]);
1980 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1981 static void tlb_info(Monitor
*mon
, const QDict
*qdict
)
1983 CPUArchState
*env1
= mon_get_cpu();
1985 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
1989 static void do_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1991 mtree_info((fprintf_function
)monitor_printf
, mon
);
1994 static void do_info_numa(Monitor
*mon
, const QDict
*qdict
)
1999 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
2000 for (i
= 0; i
< nb_numa_nodes
; i
++) {
2001 monitor_printf(mon
, "node %d cpus:", i
);
2003 if (cpu
->numa_node
== i
) {
2004 monitor_printf(mon
, " %d", cpu
->cpu_index
);
2007 monitor_printf(mon
, "\n");
2008 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2013 #ifdef CONFIG_PROFILER
2018 static void do_info_profile(Monitor
*mon
, const QDict
*qdict
)
2020 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2021 dev_time
, dev_time
/ (double)get_ticks_per_sec());
2022 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2023 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
2028 static void do_info_profile(Monitor
*mon
, const QDict
*qdict
)
2030 monitor_printf(mon
, "Internal profiler not compiled\n");
2034 /* Capture support */
2035 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2037 static void do_info_capture(Monitor
*mon
, const QDict
*qdict
)
2042 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2043 monitor_printf(mon
, "[%d]: ", i
);
2044 s
->ops
.info (s
->opaque
);
2048 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
2051 int n
= qdict_get_int(qdict
, "n");
2054 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2056 s
->ops
.destroy (s
->opaque
);
2057 QLIST_REMOVE (s
, entries
);
2064 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
2066 const char *path
= qdict_get_str(qdict
, "path");
2067 int has_freq
= qdict_haskey(qdict
, "freq");
2068 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2069 int has_bits
= qdict_haskey(qdict
, "bits");
2070 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2071 int has_channels
= qdict_haskey(qdict
, "nchannels");
2072 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2075 s
= g_malloc0 (sizeof (*s
));
2077 freq
= has_freq
? freq
: 44100;
2078 bits
= has_bits
? bits
: 16;
2079 nchannels
= has_channels
? nchannels
: 2;
2081 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2082 monitor_printf(mon
, "Failed to add wave capture\n");
2086 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2089 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2091 qemu_acl
*acl
= qemu_acl_find(name
);
2094 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2099 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
2101 const char *aclname
= qdict_get_str(qdict
, "aclname");
2102 qemu_acl
*acl
= find_acl(mon
, aclname
);
2103 qemu_acl_entry
*entry
;
2107 monitor_printf(mon
, "policy: %s\n",
2108 acl
->defaultDeny
? "deny" : "allow");
2109 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2111 monitor_printf(mon
, "%d: %s %s\n", i
,
2112 entry
->deny
? "deny" : "allow", entry
->match
);
2117 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2119 const char *aclname
= qdict_get_str(qdict
, "aclname");
2120 qemu_acl
*acl
= find_acl(mon
, aclname
);
2123 qemu_acl_reset(acl
);
2124 monitor_printf(mon
, "acl: removed all rules\n");
2128 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2130 const char *aclname
= qdict_get_str(qdict
, "aclname");
2131 const char *policy
= qdict_get_str(qdict
, "policy");
2132 qemu_acl
*acl
= find_acl(mon
, aclname
);
2135 if (strcmp(policy
, "allow") == 0) {
2136 acl
->defaultDeny
= 0;
2137 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2138 } else if (strcmp(policy
, "deny") == 0) {
2139 acl
->defaultDeny
= 1;
2140 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2142 monitor_printf(mon
, "acl: unknown policy '%s', "
2143 "expected 'deny' or 'allow'\n", policy
);
2148 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
2150 const char *aclname
= qdict_get_str(qdict
, "aclname");
2151 const char *match
= qdict_get_str(qdict
, "match");
2152 const char *policy
= qdict_get_str(qdict
, "policy");
2153 int has_index
= qdict_haskey(qdict
, "index");
2154 int index
= qdict_get_try_int(qdict
, "index", -1);
2155 qemu_acl
*acl
= find_acl(mon
, aclname
);
2159 if (strcmp(policy
, "allow") == 0) {
2161 } else if (strcmp(policy
, "deny") == 0) {
2164 monitor_printf(mon
, "acl: unknown policy '%s', "
2165 "expected 'deny' or 'allow'\n", policy
);
2169 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2171 ret
= qemu_acl_append(acl
, deny
, match
);
2173 monitor_printf(mon
, "acl: unable to add acl entry\n");
2175 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2179 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2181 const char *aclname
= qdict_get_str(qdict
, "aclname");
2182 const char *match
= qdict_get_str(qdict
, "match");
2183 qemu_acl
*acl
= find_acl(mon
, aclname
);
2187 ret
= qemu_acl_remove(acl
, match
);
2189 monitor_printf(mon
, "acl: no matching acl entry\n");
2191 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2195 #if defined(TARGET_I386)
2196 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
2200 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2201 int bank
= qdict_get_int(qdict
, "bank");
2202 uint64_t status
= qdict_get_int(qdict
, "status");
2203 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2204 uint64_t addr
= qdict_get_int(qdict
, "addr");
2205 uint64_t misc
= qdict_get_int(qdict
, "misc");
2206 int flags
= MCE_INJECT_UNCOND_AO
;
2208 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2209 flags
|= MCE_INJECT_BROADCAST
;
2211 cs
= qemu_get_cpu(cpu_index
);
2214 cpu_x86_inject_mce(mon
, cpu
, bank
, status
, mcg_status
, addr
, misc
,
2220 void qmp_getfd(const char *fdname
, Error
**errp
)
2225 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
2227 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2231 if (qemu_isdigit(fdname
[0])) {
2233 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2234 "a name not starting with a digit");
2238 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2239 if (strcmp(monfd
->name
, fdname
) != 0) {
2248 monfd
= g_malloc0(sizeof(mon_fd_t
));
2249 monfd
->name
= g_strdup(fdname
);
2252 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2255 void qmp_closefd(const char *fdname
, Error
**errp
)
2259 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2260 if (strcmp(monfd
->name
, fdname
) != 0) {
2264 QLIST_REMOVE(monfd
, next
);
2266 g_free(monfd
->name
);
2271 error_set(errp
, QERR_FD_NOT_FOUND
, fdname
);
2274 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2276 int saved_vm_running
= runstate_is_running();
2277 const char *name
= qdict_get_str(qdict
, "name");
2279 vm_stop(RUN_STATE_RESTORE_VM
);
2281 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2286 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2290 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2293 if (strcmp(monfd
->name
, fdname
) != 0) {
2299 /* caller takes ownership of fd */
2300 QLIST_REMOVE(monfd
, next
);
2301 g_free(monfd
->name
);
2307 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2311 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2313 MonFdsetFd
*mon_fdset_fd
;
2314 MonFdsetFd
*mon_fdset_fd_next
;
2316 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2317 if ((mon_fdset_fd
->removed
||
2318 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2319 runstate_is_running()) {
2320 close(mon_fdset_fd
->fd
);
2321 g_free(mon_fdset_fd
->opaque
);
2322 QLIST_REMOVE(mon_fdset_fd
, next
);
2323 g_free(mon_fdset_fd
);
2327 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2328 QLIST_REMOVE(mon_fdset
, next
);
2333 static void monitor_fdsets_cleanup(void)
2335 MonFdset
*mon_fdset
;
2336 MonFdset
*mon_fdset_next
;
2338 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2339 monitor_fdset_cleanup(mon_fdset
);
2343 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2344 const char *opaque
, Error
**errp
)
2347 Monitor
*mon
= cur_mon
;
2350 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2352 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2356 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2357 has_opaque
, opaque
, errp
);
2369 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2371 MonFdset
*mon_fdset
;
2372 MonFdsetFd
*mon_fdset_fd
;
2375 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2376 if (mon_fdset
->id
!= fdset_id
) {
2379 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2381 if (mon_fdset_fd
->fd
!= fd
) {
2384 mon_fdset_fd
->removed
= true;
2387 mon_fdset_fd
->removed
= true;
2390 if (has_fd
&& !mon_fdset_fd
) {
2393 monitor_fdset_cleanup(mon_fdset
);
2399 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2402 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2404 error_set(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2407 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2409 MonFdset
*mon_fdset
;
2410 MonFdsetFd
*mon_fdset_fd
;
2411 FdsetInfoList
*fdset_list
= NULL
;
2413 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2414 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2415 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2417 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2418 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2420 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2421 FdsetFdInfoList
*fdsetfd_info
;
2423 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2424 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2425 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2426 if (mon_fdset_fd
->opaque
) {
2427 fdsetfd_info
->value
->has_opaque
= true;
2428 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2430 fdsetfd_info
->value
->has_opaque
= false;
2433 fdsetfd_info
->next
= fdsetfd_list
;
2434 fdsetfd_list
= fdsetfd_info
;
2437 fdset_info
->value
->fds
= fdsetfd_list
;
2439 fdset_info
->next
= fdset_list
;
2440 fdset_list
= fdset_info
;
2446 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2447 bool has_opaque
, const char *opaque
,
2450 MonFdset
*mon_fdset
= NULL
;
2451 MonFdsetFd
*mon_fdset_fd
;
2455 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2456 /* Break if match found or match impossible due to ordering by ID */
2457 if (fdset_id
<= mon_fdset
->id
) {
2458 if (fdset_id
< mon_fdset
->id
) {
2466 if (mon_fdset
== NULL
) {
2467 int64_t fdset_id_prev
= -1;
2468 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2472 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2473 "a non-negative value");
2476 /* Use specified fdset ID */
2477 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2478 mon_fdset_cur
= mon_fdset
;
2479 if (fdset_id
< mon_fdset_cur
->id
) {
2484 /* Use first available fdset ID */
2485 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2486 mon_fdset_cur
= mon_fdset
;
2487 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2488 fdset_id_prev
= mon_fdset_cur
->id
;
2495 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2497 mon_fdset
->id
= fdset_id
;
2499 mon_fdset
->id
= fdset_id_prev
+ 1;
2502 /* The fdset list is ordered by fdset ID */
2503 if (!mon_fdset_cur
) {
2504 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2505 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2506 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2508 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2512 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2513 mon_fdset_fd
->fd
= fd
;
2514 mon_fdset_fd
->removed
= false;
2516 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2518 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2520 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2521 fdinfo
->fdset_id
= mon_fdset
->id
;
2522 fdinfo
->fd
= mon_fdset_fd
->fd
;
2527 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2530 MonFdset
*mon_fdset
;
2531 MonFdsetFd
*mon_fdset_fd
;
2534 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2535 if (mon_fdset
->id
!= fdset_id
) {
2538 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2539 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2540 if (mon_fd_flags
== -1) {
2544 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2545 return mon_fdset_fd
->fd
;
2557 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2559 MonFdset
*mon_fdset
;
2560 MonFdsetFd
*mon_fdset_fd_dup
;
2562 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2563 if (mon_fdset
->id
!= fdset_id
) {
2566 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2567 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2571 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2572 mon_fdset_fd_dup
->fd
= dup_fd
;
2573 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2579 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2581 MonFdset
*mon_fdset
;
2582 MonFdsetFd
*mon_fdset_fd_dup
;
2584 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2585 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2586 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2588 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2589 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2590 monitor_fdset_cleanup(mon_fdset
);
2593 return mon_fdset
->id
;
2600 int monitor_fdset_dup_fd_find(int dup_fd
)
2602 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2605 int monitor_fdset_dup_fd_remove(int dup_fd
)
2607 return monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2610 int monitor_handle_fd_param(Monitor
*mon
, const char *fdname
)
2613 Error
*local_err
= NULL
;
2615 fd
= monitor_handle_fd_param2(mon
, fdname
, &local_err
);
2617 qerror_report_err(local_err
);
2618 error_free(local_err
);
2623 int monitor_handle_fd_param2(Monitor
*mon
, const char *fdname
, Error
**errp
)
2626 Error
*local_err
= NULL
;
2628 if (!qemu_isdigit(fdname
[0]) && mon
) {
2629 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2631 fd
= qemu_parse_fd(fdname
);
2633 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2638 error_propagate(errp
, local_err
);
2647 /* Please update hmp-commands.hx when adding or changing commands */
2648 static mon_cmd_t info_cmds
[] = {
2653 .help
= "show the version of QEMU",
2654 .mhandler
.cmd
= hmp_info_version
,
2660 .help
= "show the network state",
2661 .mhandler
.cmd
= do_info_network
,
2667 .help
= "show the character devices",
2668 .mhandler
.cmd
= hmp_info_chardev
,
2672 .args_type
= "verbose:-v,device:B?",
2673 .params
= "[-v] [device]",
2674 .help
= "show info of one block device or all block devices "
2675 "(and details of images with -v option)",
2676 .mhandler
.cmd
= hmp_info_block
,
2679 .name
= "blockstats",
2682 .help
= "show block device statistics",
2683 .mhandler
.cmd
= hmp_info_blockstats
,
2686 .name
= "block-jobs",
2689 .help
= "show progress of ongoing block device operations",
2690 .mhandler
.cmd
= hmp_info_block_jobs
,
2693 .name
= "registers",
2696 .help
= "show the cpu registers",
2697 .mhandler
.cmd
= do_info_registers
,
2703 .help
= "show infos for each CPU",
2704 .mhandler
.cmd
= hmp_info_cpus
,
2710 .help
= "show the command line history",
2711 .mhandler
.cmd
= do_info_history
,
2713 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2714 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2719 .help
= "show the interrupts statistics (if available)",
2721 .mhandler
.cmd
= sun4m_irq_info
,
2722 #elif defined(TARGET_LM32)
2723 .mhandler
.cmd
= lm32_irq_info
,
2725 .mhandler
.cmd
= irq_info
,
2732 .help
= "show i8259 (PIC) state",
2734 .mhandler
.cmd
= sun4m_pic_info
,
2735 #elif defined(TARGET_LM32)
2736 .mhandler
.cmd
= lm32_do_pic_info
,
2738 .mhandler
.cmd
= pic_info
,
2746 .help
= "show PCI info",
2747 .mhandler
.cmd
= hmp_info_pci
,
2749 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2750 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2755 .help
= "show virtual to physical memory mappings",
2756 .mhandler
.cmd
= tlb_info
,
2759 #if defined(TARGET_I386)
2764 .help
= "show the active virtual memory mappings",
2765 .mhandler
.cmd
= mem_info
,
2772 .help
= "show memory tree",
2773 .mhandler
.cmd
= do_info_mtree
,
2779 .help
= "show dynamic compiler info",
2780 .mhandler
.cmd
= do_info_jit
,
2786 .help
= "show KVM information",
2787 .mhandler
.cmd
= hmp_info_kvm
,
2793 .help
= "show NUMA information",
2794 .mhandler
.cmd
= do_info_numa
,
2800 .help
= "show guest USB devices",
2801 .mhandler
.cmd
= usb_info
,
2807 .help
= "show host USB devices",
2808 .mhandler
.cmd
= usb_host_info
,
2814 .help
= "show profiling information",
2815 .mhandler
.cmd
= do_info_profile
,
2821 .help
= "show capture information",
2822 .mhandler
.cmd
= do_info_capture
,
2825 .name
= "snapshots",
2828 .help
= "show the currently saved VM snapshots",
2829 .mhandler
.cmd
= do_info_snapshots
,
2835 .help
= "show the current VM status (running|paused)",
2836 .mhandler
.cmd
= hmp_info_status
,
2842 .help
= "show guest PCMCIA status",
2843 .mhandler
.cmd
= pcmcia_info
,
2849 .help
= "show which guest mouse is receiving events",
2850 .mhandler
.cmd
= hmp_info_mice
,
2856 .help
= "show the vnc server status",
2857 .mhandler
.cmd
= hmp_info_vnc
,
2859 #if defined(CONFIG_SPICE)
2864 .help
= "show the spice server status",
2865 .mhandler
.cmd
= hmp_info_spice
,
2872 .help
= "show the current VM name",
2873 .mhandler
.cmd
= hmp_info_name
,
2879 .help
= "show the current VM UUID",
2880 .mhandler
.cmd
= hmp_info_uuid
,
2886 .help
= "show CPU statistics",
2887 .mhandler
.cmd
= do_info_cpu_stats
,
2889 #if defined(CONFIG_SLIRP)
2894 .help
= "show user network stack connection states",
2895 .mhandler
.cmd
= do_info_usernet
,
2902 .help
= "show migration status",
2903 .mhandler
.cmd
= hmp_info_migrate
,
2906 .name
= "migrate_capabilities",
2909 .help
= "show current migration capabilities",
2910 .mhandler
.cmd
= hmp_info_migrate_capabilities
,
2913 .name
= "migrate_cache_size",
2916 .help
= "show current migration xbzrle cache size",
2917 .mhandler
.cmd
= hmp_info_migrate_cache_size
,
2923 .help
= "show balloon information",
2924 .mhandler
.cmd
= hmp_info_balloon
,
2930 .help
= "show device tree",
2931 .mhandler
.cmd
= do_info_qtree
,
2937 .help
= "show qdev device model list",
2938 .mhandler
.cmd
= do_info_qdm
,
2944 .help
= "show roms",
2945 .mhandler
.cmd
= do_info_roms
,
2948 .name
= "trace-events",
2951 .help
= "show available trace-events & their state",
2952 .mhandler
.cmd
= do_trace_print_events
,
2958 .help
= "show the TPM device",
2959 .mhandler
.cmd
= hmp_info_tpm
,
2966 /* mon_cmds and info_cmds would be sorted at runtime */
2967 static mon_cmd_t mon_cmds
[] = {
2968 #include "hmp-commands.h"
2972 static const mon_cmd_t qmp_cmds
[] = {
2973 #include "qmp-commands-old.h"
2977 /*******************************************************************/
2979 static const char *pch
;
2980 static sigjmp_buf expr_env
;
2985 typedef struct MonitorDef
{
2988 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2992 #if defined(TARGET_I386)
2993 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2995 CPUArchState
*env
= mon_get_cpu();
2996 return env
->eip
+ env
->segs
[R_CS
].base
;
3000 #if defined(TARGET_PPC)
3001 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
3003 CPUArchState
*env
= mon_get_cpu();
3008 for (i
= 0; i
< 8; i
++)
3009 u
|= env
->crf
[i
] << (32 - (4 * i
));
3014 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
3016 CPUArchState
*env
= mon_get_cpu();
3020 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
3022 CPUArchState
*env
= mon_get_cpu();
3026 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
3028 CPUArchState
*env
= mon_get_cpu();
3029 return cpu_ppc_load_decr(env
);
3032 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
3034 CPUArchState
*env
= mon_get_cpu();
3035 return cpu_ppc_load_tbu(env
);
3038 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
3040 CPUArchState
*env
= mon_get_cpu();
3041 return cpu_ppc_load_tbl(env
);
3045 #if defined(TARGET_SPARC)
3046 #ifndef TARGET_SPARC64
3047 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
3049 CPUArchState
*env
= mon_get_cpu();
3051 return cpu_get_psr(env
);
3055 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
3057 CPUArchState
*env
= mon_get_cpu();
3058 return env
->regwptr
[val
];
3062 static const MonitorDef monitor_defs
[] = {
3065 #define SEG(name, seg) \
3066 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
3067 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
3068 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
3070 { "eax", offsetof(CPUX86State
, regs
[0]) },
3071 { "ecx", offsetof(CPUX86State
, regs
[1]) },
3072 { "edx", offsetof(CPUX86State
, regs
[2]) },
3073 { "ebx", offsetof(CPUX86State
, regs
[3]) },
3074 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
3075 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
3076 { "esi", offsetof(CPUX86State
, regs
[6]) },
3077 { "edi", offsetof(CPUX86State
, regs
[7]) },
3078 #ifdef TARGET_X86_64
3079 { "r8", offsetof(CPUX86State
, regs
[8]) },
3080 { "r9", offsetof(CPUX86State
, regs
[9]) },
3081 { "r10", offsetof(CPUX86State
, regs
[10]) },
3082 { "r11", offsetof(CPUX86State
, regs
[11]) },
3083 { "r12", offsetof(CPUX86State
, regs
[12]) },
3084 { "r13", offsetof(CPUX86State
, regs
[13]) },
3085 { "r14", offsetof(CPUX86State
, regs
[14]) },
3086 { "r15", offsetof(CPUX86State
, regs
[15]) },
3088 { "eflags", offsetof(CPUX86State
, eflags
) },
3089 { "eip", offsetof(CPUX86State
, eip
) },
3096 { "pc", 0, monitor_get_pc
, },
3097 #elif defined(TARGET_PPC)
3098 /* General purpose registers */
3099 { "r0", offsetof(CPUPPCState
, gpr
[0]) },
3100 { "r1", offsetof(CPUPPCState
, gpr
[1]) },
3101 { "r2", offsetof(CPUPPCState
, gpr
[2]) },
3102 { "r3", offsetof(CPUPPCState
, gpr
[3]) },
3103 { "r4", offsetof(CPUPPCState
, gpr
[4]) },
3104 { "r5", offsetof(CPUPPCState
, gpr
[5]) },
3105 { "r6", offsetof(CPUPPCState
, gpr
[6]) },
3106 { "r7", offsetof(CPUPPCState
, gpr
[7]) },
3107 { "r8", offsetof(CPUPPCState
, gpr
[8]) },
3108 { "r9", offsetof(CPUPPCState
, gpr
[9]) },
3109 { "r10", offsetof(CPUPPCState
, gpr
[10]) },
3110 { "r11", offsetof(CPUPPCState
, gpr
[11]) },
3111 { "r12", offsetof(CPUPPCState
, gpr
[12]) },
3112 { "r13", offsetof(CPUPPCState
, gpr
[13]) },
3113 { "r14", offsetof(CPUPPCState
, gpr
[14]) },
3114 { "r15", offsetof(CPUPPCState
, gpr
[15]) },
3115 { "r16", offsetof(CPUPPCState
, gpr
[16]) },
3116 { "r17", offsetof(CPUPPCState
, gpr
[17]) },
3117 { "r18", offsetof(CPUPPCState
, gpr
[18]) },
3118 { "r19", offsetof(CPUPPCState
, gpr
[19]) },
3119 { "r20", offsetof(CPUPPCState
, gpr
[20]) },
3120 { "r21", offsetof(CPUPPCState
, gpr
[21]) },
3121 { "r22", offsetof(CPUPPCState
, gpr
[22]) },
3122 { "r23", offsetof(CPUPPCState
, gpr
[23]) },
3123 { "r24", offsetof(CPUPPCState
, gpr
[24]) },
3124 { "r25", offsetof(CPUPPCState
, gpr
[25]) },
3125 { "r26", offsetof(CPUPPCState
, gpr
[26]) },
3126 { "r27", offsetof(CPUPPCState
, gpr
[27]) },
3127 { "r28", offsetof(CPUPPCState
, gpr
[28]) },
3128 { "r29", offsetof(CPUPPCState
, gpr
[29]) },
3129 { "r30", offsetof(CPUPPCState
, gpr
[30]) },
3130 { "r31", offsetof(CPUPPCState
, gpr
[31]) },
3131 /* Floating point registers */
3132 { "f0", offsetof(CPUPPCState
, fpr
[0]) },
3133 { "f1", offsetof(CPUPPCState
, fpr
[1]) },
3134 { "f2", offsetof(CPUPPCState
, fpr
[2]) },
3135 { "f3", offsetof(CPUPPCState
, fpr
[3]) },
3136 { "f4", offsetof(CPUPPCState
, fpr
[4]) },
3137 { "f5", offsetof(CPUPPCState
, fpr
[5]) },
3138 { "f6", offsetof(CPUPPCState
, fpr
[6]) },
3139 { "f7", offsetof(CPUPPCState
, fpr
[7]) },
3140 { "f8", offsetof(CPUPPCState
, fpr
[8]) },
3141 { "f9", offsetof(CPUPPCState
, fpr
[9]) },
3142 { "f10", offsetof(CPUPPCState
, fpr
[10]) },
3143 { "f11", offsetof(CPUPPCState
, fpr
[11]) },
3144 { "f12", offsetof(CPUPPCState
, fpr
[12]) },
3145 { "f13", offsetof(CPUPPCState
, fpr
[13]) },
3146 { "f14", offsetof(CPUPPCState
, fpr
[14]) },
3147 { "f15", offsetof(CPUPPCState
, fpr
[15]) },
3148 { "f16", offsetof(CPUPPCState
, fpr
[16]) },
3149 { "f17", offsetof(CPUPPCState
, fpr
[17]) },
3150 { "f18", offsetof(CPUPPCState
, fpr
[18]) },
3151 { "f19", offsetof(CPUPPCState
, fpr
[19]) },
3152 { "f20", offsetof(CPUPPCState
, fpr
[20]) },
3153 { "f21", offsetof(CPUPPCState
, fpr
[21]) },
3154 { "f22", offsetof(CPUPPCState
, fpr
[22]) },
3155 { "f23", offsetof(CPUPPCState
, fpr
[23]) },
3156 { "f24", offsetof(CPUPPCState
, fpr
[24]) },
3157 { "f25", offsetof(CPUPPCState
, fpr
[25]) },
3158 { "f26", offsetof(CPUPPCState
, fpr
[26]) },
3159 { "f27", offsetof(CPUPPCState
, fpr
[27]) },
3160 { "f28", offsetof(CPUPPCState
, fpr
[28]) },
3161 { "f29", offsetof(CPUPPCState
, fpr
[29]) },
3162 { "f30", offsetof(CPUPPCState
, fpr
[30]) },
3163 { "f31", offsetof(CPUPPCState
, fpr
[31]) },
3164 { "fpscr", offsetof(CPUPPCState
, fpscr
) },
3165 /* Next instruction pointer */
3166 { "nip|pc", offsetof(CPUPPCState
, nip
) },
3167 { "lr", offsetof(CPUPPCState
, lr
) },
3168 { "ctr", offsetof(CPUPPCState
, ctr
) },
3169 { "decr", 0, &monitor_get_decr
, },
3170 { "ccr", 0, &monitor_get_ccr
, },
3171 /* Machine state register */
3172 { "msr", 0, &monitor_get_msr
, },
3173 { "xer", 0, &monitor_get_xer
, },
3174 { "tbu", 0, &monitor_get_tbu
, },
3175 { "tbl", 0, &monitor_get_tbl
, },
3176 /* Segment registers */
3177 { "sdr1", offsetof(CPUPPCState
, spr
[SPR_SDR1
]) },
3178 { "sr0", offsetof(CPUPPCState
, sr
[0]) },
3179 { "sr1", offsetof(CPUPPCState
, sr
[1]) },
3180 { "sr2", offsetof(CPUPPCState
, sr
[2]) },
3181 { "sr3", offsetof(CPUPPCState
, sr
[3]) },
3182 { "sr4", offsetof(CPUPPCState
, sr
[4]) },
3183 { "sr5", offsetof(CPUPPCState
, sr
[5]) },
3184 { "sr6", offsetof(CPUPPCState
, sr
[6]) },
3185 { "sr7", offsetof(CPUPPCState
, sr
[7]) },
3186 { "sr8", offsetof(CPUPPCState
, sr
[8]) },
3187 { "sr9", offsetof(CPUPPCState
, sr
[9]) },
3188 { "sr10", offsetof(CPUPPCState
, sr
[10]) },
3189 { "sr11", offsetof(CPUPPCState
, sr
[11]) },
3190 { "sr12", offsetof(CPUPPCState
, sr
[12]) },
3191 { "sr13", offsetof(CPUPPCState
, sr
[13]) },
3192 { "sr14", offsetof(CPUPPCState
, sr
[14]) },
3193 { "sr15", offsetof(CPUPPCState
, sr
[15]) },
3194 /* Too lazy to put BATs... */
3195 { "pvr", offsetof(CPUPPCState
, spr
[SPR_PVR
]) },
3197 { "srr0", offsetof(CPUPPCState
, spr
[SPR_SRR0
]) },
3198 { "srr1", offsetof(CPUPPCState
, spr
[SPR_SRR1
]) },
3199 { "dar", offsetof(CPUPPCState
, spr
[SPR_DAR
]) },
3200 { "dsisr", offsetof(CPUPPCState
, spr
[SPR_DSISR
]) },
3201 { "cfar", offsetof(CPUPPCState
, spr
[SPR_CFAR
]) },
3202 { "sprg0", offsetof(CPUPPCState
, spr
[SPR_SPRG0
]) },
3203 { "sprg1", offsetof(CPUPPCState
, spr
[SPR_SPRG1
]) },
3204 { "sprg2", offsetof(CPUPPCState
, spr
[SPR_SPRG2
]) },
3205 { "sprg3", offsetof(CPUPPCState
, spr
[SPR_SPRG3
]) },
3206 { "sprg4", offsetof(CPUPPCState
, spr
[SPR_SPRG4
]) },
3207 { "sprg5", offsetof(CPUPPCState
, spr
[SPR_SPRG5
]) },
3208 { "sprg6", offsetof(CPUPPCState
, spr
[SPR_SPRG6
]) },
3209 { "sprg7", offsetof(CPUPPCState
, spr
[SPR_SPRG7
]) },
3210 { "pid", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID
]) },
3211 { "csrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR0
]) },
3212 { "csrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR1
]) },
3213 { "esr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_ESR
]) },
3214 { "dear", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DEAR
]) },
3215 { "mcsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSR
]) },
3216 { "tsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TSR
]) },
3217 { "tcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TCR
]) },
3218 { "vrsave", offsetof(CPUPPCState
, spr
[SPR_VRSAVE
]) },
3219 { "pir", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PIR
]) },
3220 { "mcsrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR0
]) },
3221 { "mcsrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR1
]) },
3222 { "decar", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DECAR
]) },
3223 { "ivpr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVPR
]) },
3224 { "epcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPCR
]) },
3225 { "sprg8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_SPRG8
]) },
3226 { "ivor0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR0
]) },
3227 { "ivor1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR1
]) },
3228 { "ivor2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR2
]) },
3229 { "ivor3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR3
]) },
3230 { "ivor4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR4
]) },
3231 { "ivor5", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR5
]) },
3232 { "ivor6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR6
]) },
3233 { "ivor7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR7
]) },
3234 { "ivor8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR8
]) },
3235 { "ivor9", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR9
]) },
3236 { "ivor10", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR10
]) },
3237 { "ivor11", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR11
]) },
3238 { "ivor12", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR12
]) },
3239 { "ivor13", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR13
]) },
3240 { "ivor14", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR14
]) },
3241 { "ivor15", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR15
]) },
3242 { "ivor32", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR32
]) },
3243 { "ivor33", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR33
]) },
3244 { "ivor34", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR34
]) },
3245 { "ivor35", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR35
]) },
3246 { "ivor36", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR36
]) },
3247 { "ivor37", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR37
]) },
3248 { "mas0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS0
]) },
3249 { "mas1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS1
]) },
3250 { "mas2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS2
]) },
3251 { "mas3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS3
]) },
3252 { "mas4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS4
]) },
3253 { "mas6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS6
]) },
3254 { "mas7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS7
]) },
3255 { "mmucfg", offsetof(CPUPPCState
, spr
[SPR_MMUCFG
]) },
3256 { "tlb0cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3257 { "tlb1cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3258 { "epr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPR
]) },
3259 { "eplc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPLC
]) },
3260 { "epsc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPSC
]) },
3261 { "svr", offsetof(CPUPPCState
, spr
[SPR_E500_SVR
]) },
3262 { "mcar", offsetof(CPUPPCState
, spr
[SPR_Exxx_MCAR
]) },
3263 { "pid1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID1
]) },
3264 { "pid2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID2
]) },
3265 { "hid0", offsetof(CPUPPCState
, spr
[SPR_HID0
]) },
3267 #elif defined(TARGET_SPARC)
3268 { "g0", offsetof(CPUSPARCState
, gregs
[0]) },
3269 { "g1", offsetof(CPUSPARCState
, gregs
[1]) },
3270 { "g2", offsetof(CPUSPARCState
, gregs
[2]) },
3271 { "g3", offsetof(CPUSPARCState
, gregs
[3]) },
3272 { "g4", offsetof(CPUSPARCState
, gregs
[4]) },
3273 { "g5", offsetof(CPUSPARCState
, gregs
[5]) },
3274 { "g6", offsetof(CPUSPARCState
, gregs
[6]) },
3275 { "g7", offsetof(CPUSPARCState
, gregs
[7]) },
3276 { "o0", 0, monitor_get_reg
},
3277 { "o1", 1, monitor_get_reg
},
3278 { "o2", 2, monitor_get_reg
},
3279 { "o3", 3, monitor_get_reg
},
3280 { "o4", 4, monitor_get_reg
},
3281 { "o5", 5, monitor_get_reg
},
3282 { "o6", 6, monitor_get_reg
},
3283 { "o7", 7, monitor_get_reg
},
3284 { "l0", 8, monitor_get_reg
},
3285 { "l1", 9, monitor_get_reg
},
3286 { "l2", 10, monitor_get_reg
},
3287 { "l3", 11, monitor_get_reg
},
3288 { "l4", 12, monitor_get_reg
},
3289 { "l5", 13, monitor_get_reg
},
3290 { "l6", 14, monitor_get_reg
},
3291 { "l7", 15, monitor_get_reg
},
3292 { "i0", 16, monitor_get_reg
},
3293 { "i1", 17, monitor_get_reg
},
3294 { "i2", 18, monitor_get_reg
},
3295 { "i3", 19, monitor_get_reg
},
3296 { "i4", 20, monitor_get_reg
},
3297 { "i5", 21, monitor_get_reg
},
3298 { "i6", 22, monitor_get_reg
},
3299 { "i7", 23, monitor_get_reg
},
3300 { "pc", offsetof(CPUSPARCState
, pc
) },
3301 { "npc", offsetof(CPUSPARCState
, npc
) },
3302 { "y", offsetof(CPUSPARCState
, y
) },
3303 #ifndef TARGET_SPARC64
3304 { "psr", 0, &monitor_get_psr
, },
3305 { "wim", offsetof(CPUSPARCState
, wim
) },
3307 { "tbr", offsetof(CPUSPARCState
, tbr
) },
3308 { "fsr", offsetof(CPUSPARCState
, fsr
) },
3309 { "f0", offsetof(CPUSPARCState
, fpr
[0].l
.upper
) },
3310 { "f1", offsetof(CPUSPARCState
, fpr
[0].l
.lower
) },
3311 { "f2", offsetof(CPUSPARCState
, fpr
[1].l
.upper
) },
3312 { "f3", offsetof(CPUSPARCState
, fpr
[1].l
.lower
) },
3313 { "f4", offsetof(CPUSPARCState
, fpr
[2].l
.upper
) },
3314 { "f5", offsetof(CPUSPARCState
, fpr
[2].l
.lower
) },
3315 { "f6", offsetof(CPUSPARCState
, fpr
[3].l
.upper
) },
3316 { "f7", offsetof(CPUSPARCState
, fpr
[3].l
.lower
) },
3317 { "f8", offsetof(CPUSPARCState
, fpr
[4].l
.upper
) },
3318 { "f9", offsetof(CPUSPARCState
, fpr
[4].l
.lower
) },
3319 { "f10", offsetof(CPUSPARCState
, fpr
[5].l
.upper
) },
3320 { "f11", offsetof(CPUSPARCState
, fpr
[5].l
.lower
) },
3321 { "f12", offsetof(CPUSPARCState
, fpr
[6].l
.upper
) },
3322 { "f13", offsetof(CPUSPARCState
, fpr
[6].l
.lower
) },
3323 { "f14", offsetof(CPUSPARCState
, fpr
[7].l
.upper
) },
3324 { "f15", offsetof(CPUSPARCState
, fpr
[7].l
.lower
) },
3325 { "f16", offsetof(CPUSPARCState
, fpr
[8].l
.upper
) },
3326 { "f17", offsetof(CPUSPARCState
, fpr
[8].l
.lower
) },
3327 { "f18", offsetof(CPUSPARCState
, fpr
[9].l
.upper
) },
3328 { "f19", offsetof(CPUSPARCState
, fpr
[9].l
.lower
) },
3329 { "f20", offsetof(CPUSPARCState
, fpr
[10].l
.upper
) },
3330 { "f21", offsetof(CPUSPARCState
, fpr
[10].l
.lower
) },
3331 { "f22", offsetof(CPUSPARCState
, fpr
[11].l
.upper
) },
3332 { "f23", offsetof(CPUSPARCState
, fpr
[11].l
.lower
) },
3333 { "f24", offsetof(CPUSPARCState
, fpr
[12].l
.upper
) },
3334 { "f25", offsetof(CPUSPARCState
, fpr
[12].l
.lower
) },
3335 { "f26", offsetof(CPUSPARCState
, fpr
[13].l
.upper
) },
3336 { "f27", offsetof(CPUSPARCState
, fpr
[13].l
.lower
) },
3337 { "f28", offsetof(CPUSPARCState
, fpr
[14].l
.upper
) },
3338 { "f29", offsetof(CPUSPARCState
, fpr
[14].l
.lower
) },
3339 { "f30", offsetof(CPUSPARCState
, fpr
[15].l
.upper
) },
3340 { "f31", offsetof(CPUSPARCState
, fpr
[15].l
.lower
) },
3341 #ifdef TARGET_SPARC64
3342 { "f32", offsetof(CPUSPARCState
, fpr
[16]) },
3343 { "f34", offsetof(CPUSPARCState
, fpr
[17]) },
3344 { "f36", offsetof(CPUSPARCState
, fpr
[18]) },
3345 { "f38", offsetof(CPUSPARCState
, fpr
[19]) },
3346 { "f40", offsetof(CPUSPARCState
, fpr
[20]) },
3347 { "f42", offsetof(CPUSPARCState
, fpr
[21]) },
3348 { "f44", offsetof(CPUSPARCState
, fpr
[22]) },
3349 { "f46", offsetof(CPUSPARCState
, fpr
[23]) },
3350 { "f48", offsetof(CPUSPARCState
, fpr
[24]) },
3351 { "f50", offsetof(CPUSPARCState
, fpr
[25]) },
3352 { "f52", offsetof(CPUSPARCState
, fpr
[26]) },
3353 { "f54", offsetof(CPUSPARCState
, fpr
[27]) },
3354 { "f56", offsetof(CPUSPARCState
, fpr
[28]) },
3355 { "f58", offsetof(CPUSPARCState
, fpr
[29]) },
3356 { "f60", offsetof(CPUSPARCState
, fpr
[30]) },
3357 { "f62", offsetof(CPUSPARCState
, fpr
[31]) },
3358 { "asi", offsetof(CPUSPARCState
, asi
) },
3359 { "pstate", offsetof(CPUSPARCState
, pstate
) },
3360 { "cansave", offsetof(CPUSPARCState
, cansave
) },
3361 { "canrestore", offsetof(CPUSPARCState
, canrestore
) },
3362 { "otherwin", offsetof(CPUSPARCState
, otherwin
) },
3363 { "wstate", offsetof(CPUSPARCState
, wstate
) },
3364 { "cleanwin", offsetof(CPUSPARCState
, cleanwin
) },
3365 { "fprs", offsetof(CPUSPARCState
, fprs
) },
3371 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3372 expr_error(Monitor
*mon
, const char *fmt
, ...)
3376 monitor_vprintf(mon
, fmt
, ap
);
3377 monitor_printf(mon
, "\n");
3379 siglongjmp(expr_env
, 1);
3382 /* return 0 if OK, -1 if not found */
3383 static int get_monitor_def(target_long
*pval
, const char *name
)
3385 const MonitorDef
*md
;
3388 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3389 if (compare_cmd(name
, md
->name
)) {
3390 if (md
->get_value
) {
3391 *pval
= md
->get_value(md
, md
->offset
);
3393 CPUArchState
*env
= mon_get_cpu();
3394 ptr
= (uint8_t *)env
+ md
->offset
;
3397 *pval
= *(int32_t *)ptr
;
3400 *pval
= *(target_long
*)ptr
;
3413 static void next(void)
3417 while (qemu_isspace(*pch
))
3422 static int64_t expr_sum(Monitor
*mon
);
3424 static int64_t expr_unary(Monitor
*mon
)
3433 n
= expr_unary(mon
);
3437 n
= -expr_unary(mon
);
3441 n
= ~expr_unary(mon
);
3447 expr_error(mon
, "')' expected");
3454 expr_error(mon
, "character constant expected");
3458 expr_error(mon
, "missing terminating \' character");
3468 while ((*pch
>= 'a' && *pch
<= 'z') ||
3469 (*pch
>= 'A' && *pch
<= 'Z') ||
3470 (*pch
>= '0' && *pch
<= '9') ||
3471 *pch
== '_' || *pch
== '.') {
3472 if ((q
- buf
) < sizeof(buf
) - 1)
3476 while (qemu_isspace(*pch
))
3479 ret
= get_monitor_def(®
, buf
);
3481 expr_error(mon
, "unknown register");
3486 expr_error(mon
, "unexpected end of expression");
3491 n
= strtoull(pch
, &p
, 0);
3492 if (errno
== ERANGE
) {
3493 expr_error(mon
, "number too large");
3496 expr_error(mon
, "invalid char '%c' in expression", *p
);
3499 while (qemu_isspace(*pch
))
3507 static int64_t expr_prod(Monitor
*mon
)
3512 val
= expr_unary(mon
);
3515 if (op
!= '*' && op
!= '/' && op
!= '%')
3518 val2
= expr_unary(mon
);
3527 expr_error(mon
, "division by zero");
3538 static int64_t expr_logic(Monitor
*mon
)
3543 val
= expr_prod(mon
);
3546 if (op
!= '&' && op
!= '|' && op
!= '^')
3549 val2
= expr_prod(mon
);
3566 static int64_t expr_sum(Monitor
*mon
)
3571 val
= expr_logic(mon
);
3574 if (op
!= '+' && op
!= '-')
3577 val2
= expr_logic(mon
);
3586 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3589 if (sigsetjmp(expr_env
, 0)) {
3593 while (qemu_isspace(*pch
))
3595 *pval
= expr_sum(mon
);
3600 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3602 const char *p
= *pp
;
3606 d
= strtod(p
, &tailp
);
3608 monitor_printf(mon
, "Number expected\n");
3611 if (d
!= d
|| d
- d
!= 0) {
3612 /* NaN or infinity */
3613 monitor_printf(mon
, "Bad number\n");
3622 * Store the command-name in cmdname, and return a pointer to
3623 * the remaining of the command string.
3625 static const char *get_command_name(const char *cmdline
,
3626 char *cmdname
, size_t nlen
)
3629 const char *p
, *pstart
;
3632 while (qemu_isspace(*p
))
3637 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3642 memcpy(cmdname
, pstart
, len
);
3643 cmdname
[len
] = '\0';
3648 * Read key of 'type' into 'key' and return the current
3651 static char *key_get_info(const char *type
, char **key
)
3659 p
= strchr(type
, ':');
3666 str
= g_malloc(len
+ 1);
3667 memcpy(str
, type
, len
);
3674 static int default_fmt_format
= 'x';
3675 static int default_fmt_size
= 4;
3677 static int is_valid_option(const char *c
, const char *typestr
)
3685 typestr
= strstr(typestr
, option
);
3686 return (typestr
!= NULL
);
3689 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3690 const char *cmdname
)
3692 const mon_cmd_t
*cmd
;
3694 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3695 if (compare_cmd(cmdname
, cmd
->name
)) {
3703 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3705 return search_dispatch_table(qmp_cmds
, cmdname
);
3709 * Parse @cmdline according to command table @table.
3710 * If @cmdline is blank, return NULL.
3711 * If it can't be parsed, report to @mon, and return NULL.
3712 * Else, insert command arguments into @qdict, and return the command.
3713 * If a sub-command table exists, and if @cmdline contains an additional string
3714 * for a sub-command, this function will try to search the sub-command table.
3715 * If no additional string for a sub-command is present, this function will
3716 * return the command found in @table.
3717 * Do not assume the returned command points into @table! It doesn't
3718 * when the command is a sub-command.
3720 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3721 const char *cmdline
,
3726 const char *p
, *typestr
;
3728 const mon_cmd_t
*cmd
;
3734 monitor_printf(mon
, "command='%s', start='%d'\n", cmdline
, start
);
3737 /* extract the command name */
3738 p
= get_command_name(cmdline
+ start
, cmdname
, sizeof(cmdname
));
3742 cmd
= search_dispatch_table(table
, cmdname
);
3744 monitor_printf(mon
, "unknown command: '%.*s'\n",
3745 (int)(p
- cmdline
), cmdline
);
3749 /* filter out following useless space */
3750 while (qemu_isspace(*p
)) {
3753 /* search sub command */
3754 if (cmd
->sub_table
!= NULL
) {
3755 /* check if user set additional command */
3759 return monitor_parse_command(mon
, cmdline
, p
- cmdline
,
3760 cmd
->sub_table
, qdict
);
3763 /* parse the parameters */
3764 typestr
= cmd
->args_type
;
3766 typestr
= key_get_info(typestr
, &key
);
3778 while (qemu_isspace(*p
))
3780 if (*typestr
== '?') {
3783 /* no optional string: NULL argument */
3787 ret
= get_str(buf
, sizeof(buf
), &p
);
3791 monitor_printf(mon
, "%s: filename expected\n",
3795 monitor_printf(mon
, "%s: block device name expected\n",
3799 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3804 qdict_put(qdict
, key
, qstring_from_str(buf
));
3809 QemuOptsList
*opts_list
;
3812 opts_list
= qemu_find_opts(key
);
3813 if (!opts_list
|| opts_list
->desc
->name
) {
3816 while (qemu_isspace(*p
)) {
3821 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3824 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3828 qemu_opts_to_qdict(opts
, qdict
);
3829 qemu_opts_del(opts
);
3834 int count
, format
, size
;
3836 while (qemu_isspace(*p
))
3842 if (qemu_isdigit(*p
)) {
3844 while (qemu_isdigit(*p
)) {
3845 count
= count
* 10 + (*p
- '0');
3883 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3884 monitor_printf(mon
, "invalid char in format: '%c'\n",
3889 format
= default_fmt_format
;
3890 if (format
!= 'i') {
3891 /* for 'i', not specifying a size gives -1 as size */
3893 size
= default_fmt_size
;
3894 default_fmt_size
= size
;
3896 default_fmt_format
= format
;
3899 format
= default_fmt_format
;
3900 if (format
!= 'i') {
3901 size
= default_fmt_size
;
3906 qdict_put(qdict
, "count", qint_from_int(count
));
3907 qdict_put(qdict
, "format", qint_from_int(format
));
3908 qdict_put(qdict
, "size", qint_from_int(size
));
3917 while (qemu_isspace(*p
))
3919 if (*typestr
== '?' || *typestr
== '.') {
3920 if (*typestr
== '?') {
3928 while (qemu_isspace(*p
))
3937 if (get_expr(mon
, &val
, &p
))
3939 /* Check if 'i' is greater than 32-bit */
3940 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3941 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3942 monitor_printf(mon
, "integer is for 32-bit values\n");
3944 } else if (c
== 'M') {
3946 monitor_printf(mon
, "enter a positive value\n");
3951 qdict_put(qdict
, key
, qint_from_int(val
));
3959 while (qemu_isspace(*p
)) {
3962 if (*typestr
== '?') {
3968 val
= strtosz(p
, &end
);
3970 monitor_printf(mon
, "invalid size\n");
3973 qdict_put(qdict
, key
, qint_from_int(val
));
3981 while (qemu_isspace(*p
))
3983 if (*typestr
== '?') {
3989 if (get_double(mon
, &val
, &p
) < 0) {
3992 if (p
[0] && p
[1] == 's') {
3995 val
/= 1e3
; p
+= 2; break;
3997 val
/= 1e6
; p
+= 2; break;
3999 val
/= 1e9
; p
+= 2; break;
4002 if (*p
&& !qemu_isspace(*p
)) {
4003 monitor_printf(mon
, "Unknown unit suffix\n");
4006 qdict_put(qdict
, key
, qfloat_from_double(val
));
4014 while (qemu_isspace(*p
)) {
4018 while (qemu_isgraph(*p
)) {
4021 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
4023 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
4026 monitor_printf(mon
, "Expected 'on' or 'off'\n");
4029 qdict_put(qdict
, key
, qbool_from_int(val
));
4034 const char *tmp
= p
;
4041 while (qemu_isspace(*p
))
4046 if(!is_valid_option(p
, typestr
)) {
4048 monitor_printf(mon
, "%s: unsupported option -%c\n",
4060 qdict_put(qdict
, key
, qbool_from_int(1));
4067 /* package all remaining string */
4070 while (qemu_isspace(*p
)) {
4073 if (*typestr
== '?') {
4076 /* no remaining string: NULL argument */
4082 monitor_printf(mon
, "%s: string expected\n",
4086 qdict_put(qdict
, key
, qstring_from_str(p
));
4092 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4098 /* check that all arguments were parsed */
4099 while (qemu_isspace(*p
))
4102 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4114 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4116 /* report only the first error */
4118 mon
->error
= qerror
;
4124 static void handler_audit(Monitor
*mon
, const mon_cmd_t
*cmd
, int ret
)
4126 if (ret
&& !monitor_has_error(mon
)) {
4128 * If it returns failure, it must have passed on error.
4130 * Action: Report an internal error to the client if in QMP.
4132 qerror_report(QERR_UNDEFINED_ERROR
);
4136 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4139 const mon_cmd_t
*cmd
;
4141 qdict
= qdict_new();
4143 cmd
= monitor_parse_command(mon
, cmdline
, 0, mon
->cmd_table
, qdict
);
4147 if (handler_is_async(cmd
)) {
4148 user_async_cmd_handler(mon
, cmd
, qdict
);
4149 } else if (handler_is_qobject(cmd
)) {
4150 QObject
*data
= NULL
;
4152 /* XXX: ignores the error code */
4153 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4154 assert(!monitor_has_error(mon
));
4156 cmd
->user_print(mon
, data
);
4157 qobject_decref(data
);
4160 cmd
->mhandler
.cmd(mon
, qdict
);
4167 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
4169 const char *p
, *pstart
;
4178 p
= pstart
+ strlen(pstart
);
4180 if (len
> sizeof(cmd
) - 2)
4181 len
= sizeof(cmd
) - 2;
4182 memcpy(cmd
, pstart
, len
);
4184 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4185 readline_add_completion(mon
->rs
, cmd
);
4193 static void file_completion(Monitor
*mon
, const char *input
)
4198 char file
[1024], file_prefix
[1024];
4202 p
= strrchr(input
, '/');
4205 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4206 pstrcpy(path
, sizeof(path
), ".");
4208 input_path_len
= p
- input
+ 1;
4209 memcpy(path
, input
, input_path_len
);
4210 if (input_path_len
> sizeof(path
) - 1)
4211 input_path_len
= sizeof(path
) - 1;
4212 path
[input_path_len
] = '\0';
4213 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4215 #ifdef DEBUG_COMPLETION
4216 monitor_printf(mon
, "input='%s' path='%s' prefix='%s'\n",
4217 input
, path
, file_prefix
);
4219 ffs
= opendir(path
);
4228 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4232 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4233 memcpy(file
, input
, input_path_len
);
4234 if (input_path_len
< sizeof(file
))
4235 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4237 /* stat the file to find out if it's a directory.
4238 * In that case add a slash to speed up typing long paths
4240 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
4241 pstrcat(file
, sizeof(file
), "/");
4243 readline_add_completion(mon
->rs
, file
);
4249 typedef struct MonitorBlockComplete
{
4252 } MonitorBlockComplete
;
4254 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
4256 const char *name
= bdrv_get_device_name(bs
);
4257 MonitorBlockComplete
*mbc
= opaque
;
4258 Monitor
*mon
= mbc
->mon
;
4259 const char *input
= mbc
->input
;
4261 if (input
[0] == '\0' ||
4262 !strncmp(name
, (char *)input
, strlen(input
))) {
4263 readline_add_completion(mon
->rs
, name
);
4267 static const char *next_arg_type(const char *typestr
)
4269 const char *p
= strchr(typestr
, ':');
4270 return (p
!= NULL
? ++p
: typestr
);
4273 static void add_completion_option(ReadLineState
*rs
, const char *str
,
4276 if (!str
|| !option
) {
4279 if (!strncmp(option
, str
, strlen(str
))) {
4280 readline_add_completion(rs
, option
);
4284 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4287 ChardevBackendInfoList
*list
, *start
;
4293 readline_set_completion_index(rs
, len
);
4295 start
= list
= qmp_query_chardev_backends(NULL
);
4297 const char *chr_name
= list
->value
->name
;
4299 if (!strncmp(chr_name
, str
, len
)) {
4300 readline_add_completion(rs
, chr_name
);
4304 qapi_free_ChardevBackendInfoList(start
);
4307 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4316 readline_set_completion_index(rs
, len
);
4317 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
4318 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
4322 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4332 readline_set_completion_index(rs
, len
);
4333 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
4336 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
4338 name
= object_class_get_name(OBJECT_CLASS(dc
));
4340 if (!dc
->cannot_instantiate_with_device_add_yet
4341 && !strncmp(name
, str
, len
)) {
4342 readline_add_completion(rs
, name
);
4349 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4359 readline_set_completion_index(rs
, len
);
4360 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
4364 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
4365 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
4366 readline_add_completion(rs
, name
);
4373 static void device_del_bus_completion(ReadLineState
*rs
, BusState
*bus
,
4374 const char *str
, size_t len
)
4378 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
4379 DeviceState
*dev
= kid
->child
;
4380 BusState
*dev_child
;
4382 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
4383 readline_add_completion(rs
, dev
->id
);
4386 QLIST_FOREACH(dev_child
, &dev
->child_bus
, sibling
) {
4387 device_del_bus_completion(rs
, dev_child
, str
, len
);
4392 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4395 ChardevInfoList
*list
, *start
;
4401 readline_set_completion_index(rs
, len
);
4403 start
= list
= qmp_query_chardev(NULL
);
4405 ChardevInfo
*chr
= list
->value
;
4407 if (!strncmp(chr
->label
, str
, len
)) {
4408 readline_add_completion(rs
, chr
->label
);
4412 qapi_free_ChardevInfoList(start
);
4415 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4424 readline_set_completion_index(rs
, len
);
4425 device_del_bus_completion(rs
, sysbus_get_default(), str
, len
);
4428 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4430 ObjectPropertyInfoList
*list
, *start
;
4437 readline_set_completion_index(rs
, len
);
4439 start
= list
= qmp_qom_list("/objects", NULL
);
4441 ObjectPropertyInfo
*info
= list
->value
;
4443 if (!strncmp(info
->type
, "child<", 5)
4444 && !strncmp(info
->name
, str
, len
)) {
4445 readline_add_completion(rs
, info
->name
);
4449 qapi_free_ObjectPropertyInfoList(start
);
4452 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4461 sep
= strrchr(str
, '-');
4466 readline_set_completion_index(rs
, len
);
4467 for (i
= 0; i
< Q_KEY_CODE_MAX
; i
++) {
4468 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
4469 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
4474 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4479 readline_set_completion_index(rs
, len
);
4481 NetClientState
*ncs
[255];
4483 count
= qemu_find_net_clients_except(NULL
, ncs
,
4484 NET_CLIENT_OPTIONS_KIND_NONE
, 255);
4485 for (i
= 0; i
< count
; i
++) {
4486 const char *name
= ncs
[i
]->name
;
4487 if (!strncmp(str
, name
, len
)) {
4488 readline_add_completion(rs
, name
);
4491 } else if (nb_args
== 3) {
4492 add_completion_option(rs
, str
, "on");
4493 add_completion_option(rs
, str
, "off");
4497 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4500 NetClientState
*ncs
[255];
4507 readline_set_completion_index(rs
, len
);
4508 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
4510 for (i
= 0; i
< count
; i
++) {
4512 const char *name
= ncs
[i
]->name
;
4513 if (strncmp(str
, name
, len
)) {
4516 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
4518 readline_add_completion(rs
, name
);
4523 static void monitor_find_completion_by_table(Monitor
*mon
,
4524 const mon_cmd_t
*cmd_table
,
4528 const char *cmdname
;
4530 const char *ptype
, *str
;
4531 const mon_cmd_t
*cmd
;
4532 MonitorBlockComplete mbs
;
4535 /* command completion */
4540 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4541 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4542 cmd_completion(mon
, cmdname
, cmd
->name
);
4545 /* find the command */
4546 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4547 if (compare_cmd(args
[0], cmd
->name
)) {
4555 if (cmd
->sub_table
) {
4556 /* do the job again */
4557 return monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4558 &args
[1], nb_args
- 1);
4560 if (cmd
->command_completion
) {
4561 return cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4564 ptype
= next_arg_type(cmd
->args_type
);
4565 for(i
= 0; i
< nb_args
- 2; i
++) {
4566 if (*ptype
!= '\0') {
4567 ptype
= next_arg_type(ptype
);
4568 while (*ptype
== '?')
4569 ptype
= next_arg_type(ptype
);
4572 str
= args
[nb_args
- 1];
4573 if (*ptype
== '-' && ptype
[1] != '\0') {
4574 ptype
= next_arg_type(ptype
);
4578 /* file completion */
4579 readline_set_completion_index(mon
->rs
, strlen(str
));
4580 file_completion(mon
, str
);
4583 /* block device name completion */
4586 readline_set_completion_index(mon
->rs
, strlen(str
));
4587 bdrv_iterate(block_completion_it
, &mbs
);
4591 if (!strcmp(cmd
->name
, "help|?")) {
4592 monitor_find_completion_by_table(mon
, cmd_table
,
4593 &args
[1], nb_args
- 1);
4602 static void monitor_find_completion(void *opaque
,
4603 const char *cmdline
)
4605 Monitor
*mon
= opaque
;
4606 char *args
[MAX_ARGS
];
4609 /* 1. parse the cmdline */
4610 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4613 #ifdef DEBUG_COMPLETION
4614 for (i
= 0; i
< nb_args
; i
++) {
4615 monitor_printf(mon
, "arg%d = '%s'\n", i
, args
[i
]);
4619 /* if the line ends with a space, it means we want to complete the
4621 len
= strlen(cmdline
);
4622 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4623 if (nb_args
>= MAX_ARGS
) {
4626 args
[nb_args
++] = g_strdup("");
4629 /* 2. auto complete according to args */
4630 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
4633 free_cmdline_args(args
, nb_args
);
4636 static int monitor_can_read(void *opaque
)
4638 Monitor
*mon
= opaque
;
4640 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4643 static int invalid_qmp_mode(const Monitor
*mon
, const char *cmd_name
)
4645 int is_cap
= compare_cmd(cmd_name
, "qmp_capabilities");
4646 return (qmp_cmd_mode(mon
) ? is_cap
: !is_cap
);
4650 * Argument validation rules:
4652 * 1. The argument must exist in cmd_args qdict
4653 * 2. The argument type must be the expected one
4655 * Special case: If the argument doesn't exist in cmd_args and
4656 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4657 * checking is skipped for it.
4659 static int check_client_args_type(const QDict
*client_args
,
4660 const QDict
*cmd_args
, int flags
)
4662 const QDictEntry
*ent
;
4664 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4667 const QObject
*client_arg
= qdict_entry_value(ent
);
4668 const char *client_arg_name
= qdict_entry_key(ent
);
4670 obj
= qdict_get(cmd_args
, client_arg_name
);
4672 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4673 /* handler accepts unknowns */
4676 /* client arg doesn't exist */
4677 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4681 arg_type
= qobject_to_qstring(obj
);
4682 assert(arg_type
!= NULL
);
4684 /* check if argument's type is correct */
4685 switch (qstring_get_str(arg_type
)[0]) {
4689 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4690 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4699 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4700 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4706 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4707 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4708 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4715 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4716 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4722 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4725 /* Any QObject can be passed. */
4730 * These types are not supported by QMP and thus are not
4731 * handled here. Fall through.
4742 * - Check if the client has passed all mandatory args
4743 * - Set special flags for argument validation
4745 static int check_mandatory_args(const QDict
*cmd_args
,
4746 const QDict
*client_args
, int *flags
)
4748 const QDictEntry
*ent
;
4750 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4751 const char *cmd_arg_name
= qdict_entry_key(ent
);
4752 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4753 assert(type
!= NULL
);
4755 if (qstring_get_str(type
)[0] == 'O') {
4756 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4757 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4758 } else if (qstring_get_str(type
)[0] != '-' &&
4759 qstring_get_str(type
)[1] != '?' &&
4760 !qdict_haskey(client_args
, cmd_arg_name
)) {
4761 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4769 static QDict
*qdict_from_args_type(const char *args_type
)
4773 QString
*key
, *type
, *cur_qs
;
4775 assert(args_type
!= NULL
);
4777 qdict
= qdict_new();
4779 if (args_type
== NULL
|| args_type
[0] == '\0') {
4780 /* no args, empty qdict */
4784 key
= qstring_new();
4785 type
= qstring_new();
4790 switch (args_type
[i
]) {
4793 qdict_put(qdict
, qstring_get_str(key
), type
);
4795 if (args_type
[i
] == '\0') {
4798 type
= qstring_new(); /* qdict has ref */
4799 cur_qs
= key
= qstring_new();
4805 qstring_append_chr(cur_qs
, args_type
[i
]);
4815 * Client argument checking rules:
4817 * 1. Client must provide all mandatory arguments
4818 * 2. Each argument provided by the client must be expected
4819 * 3. Each argument provided by the client must have the type expected
4822 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4827 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4830 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4835 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4843 * Input object checking rules
4845 * 1. Input object must be a dict
4846 * 2. The "execute" key must exist
4847 * 3. The "execute" key must be a string
4848 * 4. If the "arguments" key exists, it must be a dict
4849 * 5. If the "id" key exists, it can be anything (ie. json-value)
4850 * 6. Any argument not listed above is considered invalid
4852 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4854 const QDictEntry
*ent
;
4855 int has_exec_key
= 0;
4858 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4859 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4863 input_dict
= qobject_to_qdict(input_obj
);
4865 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4866 const char *arg_name
= qdict_entry_key(ent
);
4867 const QObject
*arg_obj
= qdict_entry_value(ent
);
4869 if (!strcmp(arg_name
, "execute")) {
4870 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4871 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4876 } else if (!strcmp(arg_name
, "arguments")) {
4877 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4878 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4882 } else if (!strcmp(arg_name
, "id")) {
4883 /* FIXME: check duplicated IDs for async commands */
4885 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4890 if (!has_exec_key
) {
4891 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4898 static void qmp_call_cmd(Monitor
*mon
, const mon_cmd_t
*cmd
,
4899 const QDict
*params
)
4902 QObject
*data
= NULL
;
4904 ret
= cmd
->mhandler
.cmd_new(mon
, params
, &data
);
4905 handler_audit(mon
, cmd
, ret
);
4906 monitor_protocol_emitter(mon
, data
);
4907 qobject_decref(data
);
4910 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4914 QDict
*input
, *args
;
4915 const mon_cmd_t
*cmd
;
4916 const char *cmd_name
;
4917 Monitor
*mon
= cur_mon
;
4919 args
= input
= NULL
;
4921 obj
= json_parser_parse(tokens
, NULL
);
4923 // FIXME: should be triggered in json_parser_parse()
4924 qerror_report(QERR_JSON_PARSING
);
4928 input
= qmp_check_input_obj(obj
);
4930 qobject_decref(obj
);
4934 mon
->mc
->id
= qdict_get(input
, "id");
4935 qobject_incref(mon
->mc
->id
);
4937 cmd_name
= qdict_get_str(input
, "execute");
4938 trace_handle_qmp_command(mon
, cmd_name
);
4939 if (invalid_qmp_mode(mon
, cmd_name
)) {
4940 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4944 cmd
= qmp_find_cmd(cmd_name
);
4946 qerror_report(QERR_COMMAND_NOT_FOUND
, cmd_name
);
4950 obj
= qdict_get(input
, "arguments");
4954 args
= qobject_to_qdict(obj
);
4958 err
= qmp_check_client_args(cmd
, args
);
4963 if (handler_is_async(cmd
)) {
4964 err
= qmp_async_cmd_handler(mon
, cmd
, args
);
4966 /* emit the error response */
4970 qmp_call_cmd(mon
, cmd
, args
);
4976 monitor_protocol_emitter(mon
, NULL
);
4983 * monitor_control_read(): Read and handle QMP input
4985 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
4987 Monitor
*old_mon
= cur_mon
;
4991 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
4996 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4998 Monitor
*old_mon
= cur_mon
;
5004 for (i
= 0; i
< size
; i
++)
5005 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
5007 if (size
== 0 || buf
[size
- 1] != 0)
5008 monitor_printf(cur_mon
, "corrupted command\n");
5010 handle_user_command(cur_mon
, (char *)buf
);
5016 static void monitor_command_cb(void *opaque
, const char *cmdline
,
5017 void *readline_opaque
)
5019 Monitor
*mon
= opaque
;
5021 monitor_suspend(mon
);
5022 handle_user_command(mon
, cmdline
);
5023 monitor_resume(mon
);
5026 int monitor_suspend(Monitor
*mon
)
5034 void monitor_resume(Monitor
*mon
)
5038 if (--mon
->suspend_cnt
== 0)
5039 readline_show_prompt(mon
->rs
);
5042 static QObject
*get_qmp_greeting(void)
5044 QObject
*ver
= NULL
;
5046 qmp_marshal_input_query_version(NULL
, NULL
, &ver
);
5047 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
5051 * monitor_control_event(): Print QMP gretting
5053 static void monitor_control_event(void *opaque
, int event
)
5056 Monitor
*mon
= opaque
;
5059 case CHR_EVENT_OPENED
:
5060 mon
->mc
->command_mode
= 0;
5061 data
= get_qmp_greeting();
5062 monitor_json_emitter(mon
, data
);
5063 qobject_decref(data
);
5066 case CHR_EVENT_CLOSED
:
5067 json_message_parser_destroy(&mon
->mc
->parser
);
5068 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5070 monitor_fdsets_cleanup();
5075 static void monitor_event(void *opaque
, int event
)
5077 Monitor
*mon
= opaque
;
5080 case CHR_EVENT_MUX_IN
:
5082 if (mon
->reset_seen
) {
5083 readline_restart(mon
->rs
);
5084 monitor_resume(mon
);
5087 mon
->suspend_cnt
= 0;
5091 case CHR_EVENT_MUX_OUT
:
5092 if (mon
->reset_seen
) {
5093 if (mon
->suspend_cnt
== 0) {
5094 monitor_printf(mon
, "\n");
5097 monitor_suspend(mon
);
5104 case CHR_EVENT_OPENED
:
5105 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
5106 "information\n", QEMU_VERSION
);
5107 if (!mon
->mux_out
) {
5108 readline_show_prompt(mon
->rs
);
5110 mon
->reset_seen
= 1;
5114 case CHR_EVENT_CLOSED
:
5116 monitor_fdsets_cleanup();
5122 compare_mon_cmd(const void *a
, const void *b
)
5124 return strcmp(((const mon_cmd_t
*)a
)->name
,
5125 ((const mon_cmd_t
*)b
)->name
);
5128 static void sortcmdlist(void)
5131 int elem_size
= sizeof(mon_cmd_t
);
5133 array_num
= sizeof(mon_cmds
)/elem_size
-1;
5134 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
5136 array_num
= sizeof(info_cmds
)/elem_size
-1;
5137 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
5149 /* These functions just adapt the readline interface in a typesafe way. We
5150 * could cast function pointers but that discards compiler checks.
5152 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
5153 const char *fmt
, ...)
5157 monitor_vprintf(opaque
, fmt
, ap
);
5161 static void monitor_readline_flush(void *opaque
)
5163 monitor_flush(opaque
);
5166 void monitor_init(CharDriverState
*chr
, int flags
)
5168 static int is_first_init
= 1;
5171 if (is_first_init
) {
5172 monitor_protocol_event_init();
5177 mon
= g_malloc(sizeof(*mon
));
5178 monitor_data_init(mon
);
5182 if (flags
& MONITOR_USE_READLINE
) {
5183 mon
->rs
= readline_init(monitor_readline_printf
,
5184 monitor_readline_flush
,
5186 monitor_find_completion
);
5187 monitor_read_command(mon
, 0);
5190 if (monitor_ctrl_mode(mon
)) {
5191 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
5192 /* Control mode requires special handlers */
5193 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
5194 monitor_control_event
, mon
);
5195 qemu_chr_fe_set_echo(chr
, true);
5197 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5199 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5200 monitor_event
, mon
);
5203 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5204 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5208 static void bdrv_password_cb(void *opaque
, const char *password
,
5209 void *readline_opaque
)
5211 Monitor
*mon
= opaque
;
5212 BlockDriverState
*bs
= readline_opaque
;
5215 if (bdrv_set_key(bs
, password
) != 0) {
5216 monitor_printf(mon
, "invalid password\n");
5219 if (mon
->password_completion_cb
)
5220 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5222 monitor_read_command(mon
, 1);
5225 ReadLineState
*monitor_get_rs(Monitor
*mon
)
5230 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5231 BlockDriverCompletionFunc
*completion_cb
,
5236 if (!bdrv_key_required(bs
)) {
5238 completion_cb(opaque
, 0);
5242 if (monitor_ctrl_mode(mon
)) {
5243 qerror_report(QERR_DEVICE_ENCRYPTED
, bdrv_get_device_name(bs
),
5244 bdrv_get_encrypted_filename(bs
));
5248 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5249 bdrv_get_encrypted_filename(bs
));
5251 mon
->password_completion_cb
= completion_cb
;
5252 mon
->password_opaque
= opaque
;
5254 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5256 if (err
&& completion_cb
)
5257 completion_cb(opaque
, err
);
5262 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
5263 BlockDriverCompletionFunc
*completion_cb
,
5266 BlockDriverState
*bs
;
5268 bs
= bdrv_find(device
);
5270 monitor_printf(mon
, "Device not found %s\n", device
);
5274 return monitor_read_bdrv_key_start(mon
, bs
, completion_cb
, opaque
);
5277 QemuOptsList qemu_mon_opts
= {
5279 .implied_opt_name
= "chardev",
5280 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
5284 .type
= QEMU_OPT_STRING
,
5287 .type
= QEMU_OPT_STRING
,
5290 .type
= QEMU_OPT_BOOL
,
5293 .type
= QEMU_OPT_BOOL
,
5295 { /* end of list */ }