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/i386/pc.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
34 #include "net/slirp.h"
35 #include "sysemu/char.h"
36 #include "ui/qemu-spice.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/numa.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"
73 #include "block/qapi.h"
74 #include "qapi/qmp-event.h"
75 #include "qapi-event.h"
76 #include "sysemu/block-backend.h"
78 /* for hmp_info_irq/pic */
79 #if defined(TARGET_SPARC)
80 #include "hw/sparc/sun4m.h"
82 #include "hw/lm32/lm32_pic.h"
85 //#define DEBUG_COMPLETION
91 * 'B' block device name
92 * 's' string (accept optional quote)
93 * 'S' it just appends the rest of the string (accept optional quote)
94 * 'O' option string of the form NAME=VALUE,...
95 * parsed according to QemuOptsList given by its name
96 * Example: 'device:O' uses qemu_device_opts.
97 * Restriction: only lists with empty desc are supported
98 * TODO lift the restriction
100 * 'l' target long (32 or 64 bit)
101 * 'M' Non-negative target long (32 or 64 bit), in user mode the
102 * value is multiplied by 2^20 (think Mebibyte)
103 * 'o' octets (aka bytes)
104 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
105 * K, k suffix, which multiplies the value by 2^60 for suffixes E
106 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
107 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
109 * user mode accepts an optional ms, us, ns suffix,
110 * which divides the value by 1e3, 1e6, 1e9, respectively
111 * '/' optional gdb-like print format (like "/10x")
113 * '?' optional type (for all types, except '/')
114 * '.' other form of optional type (for 'i' and 'l')
116 * user mode accepts "on" or "off"
117 * '-' optional parameter (eg. '-f')
121 typedef struct mon_cmd_t
{
123 const char *args_type
;
126 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
128 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
129 int (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
131 /* @sub_table is a list of 2nd level of commands. If it do not exist,
132 * mhandler should be used. If it exist, sub_table[?].mhandler should be
133 * used, and mhandler of 1st level plays the role of help function.
135 struct mon_cmd_t
*sub_table
;
136 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
139 /* file descriptors passed via SCM_RIGHTS */
140 typedef struct mon_fd_t mon_fd_t
;
144 QLIST_ENTRY(mon_fd_t
) next
;
147 /* file descriptor associated with a file descriptor set */
148 typedef struct MonFdsetFd MonFdsetFd
;
153 QLIST_ENTRY(MonFdsetFd
) next
;
156 /* file descriptor set containing fds passed via SCM_RIGHTS */
157 typedef struct MonFdset MonFdset
;
160 QLIST_HEAD(, MonFdsetFd
) fds
;
161 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
162 QLIST_ENTRY(MonFdset
) next
;
165 typedef struct MonitorControl
{
167 JSONMessageParser parser
;
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
176 typedef struct MonitorQAPIEventState
{
177 QAPIEvent event
; /* Event being tracked */
178 int64_t rate
; /* Minimum time (in ns) between two events */
179 int64_t last
; /* QEMU_CLOCK_REALTIME value at last emission */
180 QEMUTimer
*timer
; /* Timer for handling delayed events */
181 QObject
*data
; /* Event pending delayed dispatch */
182 } MonitorQAPIEventState
;
185 CharDriverState
*chr
;
195 /* Read under either BQL or out_lock, written with BQL+out_lock. */
201 BlockCompletionFunc
*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 /* Protects mon_list, monitor_event_state. */
213 static QemuMutex monitor_lock
;
215 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
216 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
217 static int mon_refcount
;
219 static mon_cmd_t mon_cmds
[];
220 static mon_cmd_t info_cmds
[];
222 static const mon_cmd_t qmp_cmds
[];
225 Monitor
*default_mon
;
227 static void monitor_command_cb(void *opaque
, const char *cmdline
,
228 void *readline_opaque
);
230 static inline int qmp_cmd_mode(const Monitor
*mon
)
232 return (mon
->mc
? mon
->mc
->command_mode
: 0);
235 /* Return true if in control mode, false otherwise */
236 static inline int monitor_ctrl_mode(const Monitor
*mon
)
238 return (mon
->flags
& MONITOR_USE_CONTROL
);
241 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
242 int monitor_cur_is_qmp(void)
244 return cur_mon
&& monitor_ctrl_mode(cur_mon
);
247 void monitor_read_command(Monitor
*mon
, int show_prompt
)
252 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
254 readline_show_prompt(mon
->rs
);
257 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
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 void monitor_flush_locked(Monitor
*mon
);
272 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
275 Monitor
*mon
= opaque
;
277 qemu_mutex_lock(&mon
->out_lock
);
279 monitor_flush_locked(mon
);
280 qemu_mutex_unlock(&mon
->out_lock
);
284 /* Called with mon->out_lock held. */
285 static void monitor_flush_locked(Monitor
*mon
)
291 if (mon
->skip_flush
) {
295 buf
= qstring_get_str(mon
->outbuf
);
296 len
= qstring_get_length(mon
->outbuf
);
298 if (len
&& !mon
->mux_out
) {
299 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
300 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
301 /* all flushed or error */
302 QDECREF(mon
->outbuf
);
303 mon
->outbuf
= qstring_new();
308 QString
*tmp
= qstring_from_str(buf
+ rc
);
309 QDECREF(mon
->outbuf
);
312 if (mon
->out_watch
== 0) {
313 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
314 monitor_unblocked
, mon
);
319 void monitor_flush(Monitor
*mon
)
321 qemu_mutex_lock(&mon
->out_lock
);
322 monitor_flush_locked(mon
);
323 qemu_mutex_unlock(&mon
->out_lock
);
326 /* flush at every end of line */
327 static void monitor_puts(Monitor
*mon
, const char *str
)
331 qemu_mutex_lock(&mon
->out_lock
);
337 qstring_append_chr(mon
->outbuf
, '\r');
339 qstring_append_chr(mon
->outbuf
, c
);
341 monitor_flush_locked(mon
);
344 qemu_mutex_unlock(&mon
->out_lock
);
347 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
354 if (monitor_ctrl_mode(mon
)) {
358 buf
= g_strdup_vprintf(fmt
, ap
);
359 monitor_puts(mon
, buf
);
363 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
367 monitor_vprintf(mon
, fmt
, ap
);
371 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
372 const char *fmt
, ...)
376 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
381 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
383 static inline int handler_is_qobject(const mon_cmd_t
*cmd
)
385 return cmd
->user_print
!= NULL
;
388 static inline int monitor_has_error(const Monitor
*mon
)
390 return mon
->error
!= NULL
;
393 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
397 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
398 qobject_to_json(data
);
399 assert(json
!= NULL
);
401 qstring_append_chr(json
, '\n');
402 monitor_puts(mon
, qstring_get_str(json
));
407 static QDict
*build_qmp_error_dict(const QError
*err
)
411 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %p } }",
412 ErrorClass_lookup
[err
->err_class
],
415 return qobject_to_qdict(obj
);
418 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
422 trace_monitor_protocol_emitter(mon
);
424 if (!monitor_has_error(mon
)) {
425 /* success response */
428 qobject_incref(data
);
429 qdict_put_obj(qmp
, "return", data
);
431 /* return an empty QDict by default */
432 qdict_put(qmp
, "return", qdict_new());
436 qmp
= build_qmp_error_dict(mon
->error
);
442 qdict_put_obj(qmp
, "id", mon
->mc
->id
);
446 monitor_json_emitter(mon
, QOBJECT(qmp
));
451 static MonitorQAPIEventState monitor_qapi_event_state
[QAPI_EVENT_MAX
];
454 * Emits the event to every monitor instance, @event is only used for trace
455 * Called with monitor_lock held.
457 static void monitor_qapi_event_emit(QAPIEvent event
, QObject
*data
)
461 trace_monitor_protocol_event_emit(event
, data
);
462 QLIST_FOREACH(mon
, &mon_list
, entry
) {
463 if (monitor_ctrl_mode(mon
) && qmp_cmd_mode(mon
)) {
464 monitor_json_emitter(mon
, data
);
470 * Queue a new event for emission to Monitor instances,
471 * applying any rate limiting if required.
474 monitor_qapi_event_queue(QAPIEvent event
, QDict
*data
, Error
**errp
)
476 MonitorQAPIEventState
*evstate
;
477 assert(event
< QAPI_EVENT_MAX
);
478 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
480 evstate
= &(monitor_qapi_event_state
[event
]);
481 trace_monitor_protocol_event_queue(event
,
487 /* Rate limit of 0 indicates no throttling */
488 qemu_mutex_lock(&monitor_lock
);
489 if (!evstate
->rate
) {
490 monitor_qapi_event_emit(event
, QOBJECT(data
));
493 int64_t delta
= now
- evstate
->last
;
495 delta
< evstate
->rate
) {
496 /* If there's an existing event pending, replace
497 * it with the new event, otherwise schedule a
498 * timer for delayed emission
501 qobject_decref(evstate
->data
);
503 int64_t then
= evstate
->last
+ evstate
->rate
;
504 timer_mod_ns(evstate
->timer
, then
);
506 evstate
->data
= QOBJECT(data
);
507 qobject_incref(evstate
->data
);
509 monitor_qapi_event_emit(event
, QOBJECT(data
));
513 qemu_mutex_unlock(&monitor_lock
);
517 * The callback invoked by QemuTimer when a delayed
518 * event is ready to be emitted
520 static void monitor_qapi_event_handler(void *opaque
)
522 MonitorQAPIEventState
*evstate
= opaque
;
523 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
525 trace_monitor_protocol_event_handler(evstate
->event
,
529 qemu_mutex_lock(&monitor_lock
);
531 monitor_qapi_event_emit(evstate
->event
, evstate
->data
);
532 qobject_decref(evstate
->data
);
533 evstate
->data
= NULL
;
536 qemu_mutex_unlock(&monitor_lock
);
540 * @event: the event ID to be limited
541 * @rate: the rate limit in milliseconds
543 * Sets a rate limit on a particular event, so no
544 * more than 1 event will be emitted within @rate
548 monitor_qapi_event_throttle(QAPIEvent event
, int64_t rate
)
550 MonitorQAPIEventState
*evstate
;
551 assert(event
< QAPI_EVENT_MAX
);
553 evstate
= &(monitor_qapi_event_state
[event
]);
555 trace_monitor_protocol_event_throttle(event
, rate
);
556 evstate
->event
= event
;
557 assert(rate
* SCALE_MS
<= INT64_MAX
);
558 evstate
->rate
= rate
* SCALE_MS
;
560 evstate
->data
= NULL
;
561 evstate
->timer
= timer_new(QEMU_CLOCK_REALTIME
,
563 monitor_qapi_event_handler
,
567 static void monitor_qapi_event_init(void)
569 /* Limit guest-triggerable events to 1 per second */
570 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE
, 1000);
571 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG
, 1000);
572 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE
, 1000);
573 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD
, 1000);
574 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE
, 1000);
575 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE
, 1000);
577 qmp_event_set_func_emit(monitor_qapi_event_queue
);
580 static int do_qmp_capabilities(Monitor
*mon
, const QDict
*params
,
583 /* Will setup QMP capabilities in the future */
584 if (monitor_ctrl_mode(mon
)) {
585 mon
->mc
->command_mode
= 1;
591 static void handle_user_command(Monitor
*mon
, const char *cmdline
);
593 static void monitor_data_init(Monitor
*mon
)
595 memset(mon
, 0, sizeof(Monitor
));
596 qemu_mutex_init(&mon
->out_lock
);
597 mon
->outbuf
= qstring_new();
598 /* Use *mon_cmds by default. */
599 mon
->cmd_table
= mon_cmds
;
602 static void monitor_data_destroy(Monitor
*mon
)
604 QDECREF(mon
->outbuf
);
605 qemu_mutex_destroy(&mon
->out_lock
);
608 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
609 int64_t cpu_index
, Error
**errp
)
612 Monitor
*old_mon
, hmp
;
614 monitor_data_init(&hmp
);
615 hmp
.skip_flush
= true;
621 int ret
= monitor_set_cpu(cpu_index
);
624 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
630 handle_user_command(&hmp
, command_line
);
633 qemu_mutex_lock(&hmp
.out_lock
);
634 if (qstring_get_length(hmp
.outbuf
) > 0) {
635 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
637 output
= g_strdup("");
639 qemu_mutex_unlock(&hmp
.out_lock
);
642 monitor_data_destroy(&hmp
);
646 static int compare_cmd(const char *name
, const char *list
)
648 const char *p
, *pstart
;
656 p
= pstart
+ strlen(pstart
);
657 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
666 static int get_str(char *buf
, int buf_size
, const char **pp
)
674 while (qemu_isspace(*p
)) {
685 while (*p
!= '\0' && *p
!= '\"') {
701 qemu_printf("unsupported escape code: '\\%c'\n", c
);
704 if ((q
- buf
) < buf_size
- 1) {
708 if ((q
- buf
) < buf_size
- 1) {
715 qemu_printf("unterminated string\n");
720 while (*p
!= '\0' && !qemu_isspace(*p
)) {
721 if ((q
- buf
) < buf_size
- 1) {
734 static void free_cmdline_args(char **args
, int nb_args
)
738 assert(nb_args
<= MAX_ARGS
);
740 for (i
= 0; i
< nb_args
; i
++) {
747 * Parse the command line to get valid args.
748 * @cmdline: command line to be parsed.
749 * @pnb_args: location to store the number of args, must NOT be NULL.
750 * @args: location to store the args, which should be freed by caller, must
753 * Returns 0 on success, negative on failure.
755 * NOTE: this parser is an approximate form of the real command parser. Number
756 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
757 * return with failure.
759 static int parse_cmdline(const char *cmdline
,
760 int *pnb_args
, char **args
)
769 while (qemu_isspace(*p
)) {
775 if (nb_args
>= MAX_ARGS
) {
778 ret
= get_str(buf
, sizeof(buf
), &p
);
782 args
[nb_args
] = g_strdup(buf
);
789 free_cmdline_args(args
, nb_args
);
793 static void help_cmd_dump_one(Monitor
*mon
,
794 const mon_cmd_t
*cmd
,
800 for (i
= 0; i
< prefix_args_nb
; i
++) {
801 monitor_printf(mon
, "%s ", prefix_args
[i
]);
803 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
806 /* @args[@arg_index] is the valid command need to find in @cmds */
807 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
808 char **args
, int nb_args
, int arg_index
)
810 const mon_cmd_t
*cmd
;
812 /* No valid arg need to compare with, dump all in *cmds */
813 if (arg_index
>= nb_args
) {
814 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
815 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
820 /* Find one entry to dump */
821 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
822 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
823 if (cmd
->sub_table
) {
824 /* continue with next arg */
825 help_cmd_dump(mon
, cmd
->sub_table
,
826 args
, nb_args
, arg_index
+ 1);
828 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
835 static void help_cmd(Monitor
*mon
, const char *name
)
837 char *args
[MAX_ARGS
];
840 /* 1. parse user input */
842 /* special case for log, directly dump and return */
843 if (!strcmp(name
, "log")) {
844 const QEMULogItem
*item
;
845 monitor_printf(mon
, "Log items (comma separated):\n");
846 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
847 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
848 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
853 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
858 /* 2. dump the contents according to parsed args */
859 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
861 free_cmdline_args(args
, nb_args
);
864 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
866 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
869 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
871 const char *tp_name
= qdict_get_str(qdict
, "name");
872 bool new_state
= qdict_get_bool(qdict
, "option");
873 Error
*local_err
= NULL
;
875 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
877 error_report_err(local_err
);
881 #ifdef CONFIG_TRACE_SIMPLE
882 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
884 const char *op
= qdict_get_try_str(qdict
, "op");
885 const char *arg
= qdict_get_try_str(qdict
, "arg");
888 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
889 } else if (!strcmp(op
, "on")) {
890 st_set_trace_file_enabled(true);
891 } else if (!strcmp(op
, "off")) {
892 st_set_trace_file_enabled(false);
893 } else if (!strcmp(op
, "flush")) {
894 st_flush_trace_buffer();
895 } else if (!strcmp(op
, "set")) {
897 st_set_trace_file(arg
);
900 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
901 help_cmd(mon
, "trace-file");
906 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
908 help_cmd(mon
, "info");
911 CommandInfoList
*qmp_query_commands(Error
**errp
)
913 CommandInfoList
*info
, *cmd_list
= NULL
;
914 const mon_cmd_t
*cmd
;
916 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
917 info
= g_malloc0(sizeof(*info
));
918 info
->value
= g_malloc0(sizeof(*info
->value
));
919 info
->value
->name
= g_strdup(cmd
->name
);
921 info
->next
= cmd_list
;
928 EventInfoList
*qmp_query_events(Error
**errp
)
930 EventInfoList
*info
, *ev_list
= NULL
;
933 for (e
= 0 ; e
< QAPI_EVENT_MAX
; e
++) {
934 const char *event_name
= QAPIEvent_lookup
[e
];
935 assert(event_name
!= NULL
);
936 info
= g_malloc0(sizeof(*info
));
937 info
->value
= g_malloc0(sizeof(*info
->value
));
938 info
->value
->name
= g_strdup(event_name
);
940 info
->next
= ev_list
;
947 /* set the current CPU defined by the user */
948 int monitor_set_cpu(int cpu_index
)
952 cpu
= qemu_get_cpu(cpu_index
);
956 cur_mon
->mon_cpu
= cpu
;
960 static CPUArchState
*mon_get_cpu(void)
962 if (!cur_mon
->mon_cpu
) {
965 cpu_synchronize_state(cur_mon
->mon_cpu
);
966 return cur_mon
->mon_cpu
->env_ptr
;
969 int monitor_get_cpu_index(void)
971 CPUState
*cpu
= ENV_GET_CPU(mon_get_cpu());
972 return cpu
->cpu_index
;
975 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
980 cpu
= ENV_GET_CPU(env
);
981 cpu_dump_state(cpu
, (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
984 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
986 dump_exec_info((FILE *)mon
, monitor_fprintf
);
987 dump_drift_info((FILE *)mon
, monitor_fprintf
);
990 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
992 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
995 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1004 str
= readline_get_history(mon
->rs
, i
);
1007 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1012 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1017 env
= mon_get_cpu();
1018 cpu
= ENV_GET_CPU(env
);
1019 cpu_dump_statistics(cpu
, (FILE *)mon
, &monitor_fprintf
, 0);
1022 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1024 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1025 TraceEventInfoList
*elem
;
1027 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1028 monitor_printf(mon
, "%s : state %u\n",
1030 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1032 qapi_free_TraceEventInfoList(events
);
1035 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1036 bool has_port
, int64_t port
,
1037 bool has_tls_port
, int64_t tls_port
,
1038 bool has_cert_subject
, const char *cert_subject
,
1041 if (strcmp(protocol
, "spice") == 0) {
1042 if (!qemu_using_spice(errp
)) {
1046 if (!has_port
&& !has_tls_port
) {
1047 error_set(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1051 if (qemu_spice_migrate_info(hostname
,
1052 has_port
? port
: -1,
1053 has_tls_port
? tls_port
: -1,
1055 error_set(errp
, QERR_UNDEFINED_ERROR
);
1061 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1064 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1066 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1069 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1072 const char *items
= qdict_get_str(qdict
, "items");
1074 if (!strcmp(items
, "none")) {
1077 mask
= qemu_str_to_log_mask(items
);
1079 help_cmd(mon
, "log");
1086 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1088 const char *option
= qdict_get_try_str(qdict
, "option");
1089 if (!option
|| !strcmp(option
, "on")) {
1091 } else if (!strcmp(option
, "off")) {
1094 monitor_printf(mon
, "unexpected option %s\n", option
);
1098 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1100 const char *device
= qdict_get_try_str(qdict
, "device");
1102 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1103 if (gdbserver_start(device
) < 0) {
1104 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1106 } else if (strcmp(device
, "none") == 0) {
1107 monitor_printf(mon
, "Disabled gdbserver\n");
1109 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1114 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1116 const char *action
= qdict_get_str(qdict
, "action");
1117 if (select_watchdog_action(action
) == -1) {
1118 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1122 static void monitor_printc(Monitor
*mon
, int c
)
1124 monitor_printf(mon
, "'");
1127 monitor_printf(mon
, "\\'");
1130 monitor_printf(mon
, "\\\\");
1133 monitor_printf(mon
, "\\n");
1136 monitor_printf(mon
, "\\r");
1139 if (c
>= 32 && c
<= 126) {
1140 monitor_printf(mon
, "%c", c
);
1142 monitor_printf(mon
, "\\x%02x", c
);
1146 monitor_printf(mon
, "'");
1149 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1150 hwaddr addr
, int is_physical
)
1153 int l
, line_size
, i
, max_digits
, len
;
1157 if (format
== 'i') {
1160 env
= mon_get_cpu();
1164 } else if (wsize
== 4) {
1167 /* as default we use the current CS size */
1170 #ifdef TARGET_X86_64
1171 if ((env
->efer
& MSR_EFER_LMA
) &&
1172 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1176 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1182 flags
= msr_le
<< 16;
1183 flags
|= env
->bfd_mach
;
1185 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
1189 len
= wsize
* count
;
1198 max_digits
= (wsize
* 8 + 2) / 3;
1202 max_digits
= (wsize
* 8) / 4;
1206 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1215 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1217 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1222 cpu_physical_memory_read(addr
, buf
, l
);
1224 env
= mon_get_cpu();
1225 if (cpu_memory_rw_debug(ENV_GET_CPU(env
), addr
, buf
, l
, 0) < 0) {
1226 monitor_printf(mon
, " Cannot access memory\n");
1235 v
= ldub_p(buf
+ i
);
1238 v
= lduw_p(buf
+ i
);
1241 v
= (uint32_t)ldl_p(buf
+ i
);
1247 monitor_printf(mon
, " ");
1250 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1253 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1256 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1259 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1262 monitor_printc(mon
, v
);
1267 monitor_printf(mon
, "\n");
1273 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1275 int count
= qdict_get_int(qdict
, "count");
1276 int format
= qdict_get_int(qdict
, "format");
1277 int size
= qdict_get_int(qdict
, "size");
1278 target_long addr
= qdict_get_int(qdict
, "addr");
1280 memory_dump(mon
, count
, format
, size
, addr
, 0);
1283 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1285 int count
= qdict_get_int(qdict
, "count");
1286 int format
= qdict_get_int(qdict
, "format");
1287 int size
= qdict_get_int(qdict
, "size");
1288 hwaddr addr
= qdict_get_int(qdict
, "addr");
1290 memory_dump(mon
, count
, format
, size
, addr
, 1);
1293 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1295 int format
= qdict_get_int(qdict
, "format");
1296 hwaddr val
= qdict_get_int(qdict
, "val");
1300 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1303 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1306 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1310 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1313 monitor_printc(mon
, val
);
1316 monitor_printf(mon
, "\n");
1319 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1323 uint32_t start
= qdict_get_int(qdict
, "start");
1324 uint32_t size
= qdict_get_int(qdict
, "size");
1327 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1328 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1329 MEMTXATTRS_UNSPECIFIED
, NULL
);
1330 /* BSD sum algorithm ('sum' Unix command) */
1331 sum
= (sum
>> 1) | (sum
<< 15);
1334 monitor_printf(mon
, "%05d\n", sum
);
1337 static int mouse_button_state
;
1339 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1341 int dx
, dy
, dz
, button
;
1342 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1343 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1344 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1346 dx
= strtol(dx_str
, NULL
, 0);
1347 dy
= strtol(dy_str
, NULL
, 0);
1348 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1349 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1352 dz
= strtol(dz_str
, NULL
, 0);
1354 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1355 qemu_input_queue_btn(NULL
, button
, true);
1356 qemu_input_event_sync();
1357 qemu_input_queue_btn(NULL
, button
, false);
1360 qemu_input_event_sync();
1363 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1365 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
1366 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1367 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1368 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1370 int button_state
= qdict_get_int(qdict
, "button_state");
1372 if (mouse_button_state
== button_state
) {
1375 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1376 qemu_input_event_sync();
1377 mouse_button_state
= button_state
;
1380 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1382 int size
= qdict_get_int(qdict
, "size");
1383 int addr
= qdict_get_int(qdict
, "addr");
1384 int has_index
= qdict_haskey(qdict
, "index");
1389 int index
= qdict_get_int(qdict
, "index");
1390 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1398 val
= cpu_inb(addr
);
1402 val
= cpu_inw(addr
);
1406 val
= cpu_inl(addr
);
1410 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1411 suffix
, addr
, size
* 2, val
);
1414 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1416 int size
= qdict_get_int(qdict
, "size");
1417 int addr
= qdict_get_int(qdict
, "addr");
1418 int val
= qdict_get_int(qdict
, "val");
1420 addr
&= IOPORTS_MASK
;
1425 cpu_outb(addr
, val
);
1428 cpu_outw(addr
, val
);
1431 cpu_outl(addr
, val
);
1436 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1438 Error
*local_err
= NULL
;
1439 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1441 qemu_boot_set(bootdevice
, &local_err
);
1443 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1444 error_free(local_err
);
1446 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1450 #if defined(TARGET_I386)
1451 static void print_pte(Monitor
*mon
, hwaddr addr
,
1455 #ifdef TARGET_X86_64
1456 if (addr
& (1ULL << 47)) {
1460 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1461 " %c%c%c%c%c%c%c%c%c\n",
1464 pte
& PG_NX_MASK
? 'X' : '-',
1465 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1466 pte
& PG_PSE_MASK
? 'P' : '-',
1467 pte
& PG_DIRTY_MASK
? 'D' : '-',
1468 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1469 pte
& PG_PCD_MASK
? 'C' : '-',
1470 pte
& PG_PWT_MASK
? 'T' : '-',
1471 pte
& PG_USER_MASK
? 'U' : '-',
1472 pte
& PG_RW_MASK
? 'W' : '-');
1475 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
1477 unsigned int l1
, l2
;
1478 uint32_t pgd
, pde
, pte
;
1480 pgd
= env
->cr
[3] & ~0xfff;
1481 for(l1
= 0; l1
< 1024; l1
++) {
1482 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1483 pde
= le32_to_cpu(pde
);
1484 if (pde
& PG_PRESENT_MASK
) {
1485 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1487 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1489 for(l2
= 0; l2
< 1024; l2
++) {
1490 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1491 pte
= le32_to_cpu(pte
);
1492 if (pte
& PG_PRESENT_MASK
) {
1493 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1503 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1505 unsigned int l1
, l2
, l3
;
1506 uint64_t pdpe
, pde
, pte
;
1507 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1509 pdp_addr
= env
->cr
[3] & ~0x1f;
1510 for (l1
= 0; l1
< 4; l1
++) {
1511 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1512 pdpe
= le64_to_cpu(pdpe
);
1513 if (pdpe
& PG_PRESENT_MASK
) {
1514 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1515 for (l2
= 0; l2
< 512; l2
++) {
1516 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1517 pde
= le64_to_cpu(pde
);
1518 if (pde
& PG_PRESENT_MASK
) {
1519 if (pde
& PG_PSE_MASK
) {
1520 /* 2M pages with PAE, CR4.PSE is ignored */
1521 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1522 ~((hwaddr
)(1 << 20) - 1));
1524 pt_addr
= pde
& 0x3fffffffff000ULL
;
1525 for (l3
= 0; l3
< 512; l3
++) {
1526 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1527 pte
= le64_to_cpu(pte
);
1528 if (pte
& PG_PRESENT_MASK
) {
1529 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1542 #ifdef TARGET_X86_64
1543 static void tlb_info_64(Monitor
*mon
, CPUArchState
*env
)
1545 uint64_t l1
, l2
, l3
, l4
;
1546 uint64_t pml4e
, pdpe
, pde
, pte
;
1547 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1549 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1550 for (l1
= 0; l1
< 512; l1
++) {
1551 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1552 pml4e
= le64_to_cpu(pml4e
);
1553 if (pml4e
& PG_PRESENT_MASK
) {
1554 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1555 for (l2
= 0; l2
< 512; l2
++) {
1556 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1557 pdpe
= le64_to_cpu(pdpe
);
1558 if (pdpe
& PG_PRESENT_MASK
) {
1559 if (pdpe
& PG_PSE_MASK
) {
1560 /* 1G pages, CR4.PSE is ignored */
1561 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1562 0x3ffffc0000000ULL
);
1564 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1565 for (l3
= 0; l3
< 512; l3
++) {
1566 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1567 pde
= le64_to_cpu(pde
);
1568 if (pde
& PG_PRESENT_MASK
) {
1569 if (pde
& PG_PSE_MASK
) {
1570 /* 2M pages, CR4.PSE is ignored */
1571 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1573 0x3ffffffe00000ULL
);
1575 pt_addr
= pde
& 0x3fffffffff000ULL
;
1576 for (l4
= 0; l4
< 512; l4
++) {
1577 cpu_physical_memory_read(pt_addr
1580 pte
= le64_to_cpu(pte
);
1581 if (pte
& PG_PRESENT_MASK
) {
1582 print_pte(mon
, (l1
<< 39) +
1584 (l3
<< 21) + (l4
<< 12),
1586 0x3fffffffff000ULL
);
1600 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1604 env
= mon_get_cpu();
1606 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1607 monitor_printf(mon
, "PG disabled\n");
1610 if (env
->cr
[4] & CR4_PAE_MASK
) {
1611 #ifdef TARGET_X86_64
1612 if (env
->hflags
& HF_LMA_MASK
) {
1613 tlb_info_64(mon
, env
);
1617 tlb_info_pae32(mon
, env
);
1620 tlb_info_32(mon
, env
);
1624 static void mem_print(Monitor
*mon
, hwaddr
*pstart
,
1626 hwaddr end
, int prot
)
1629 prot1
= *plast_prot
;
1630 if (prot
!= prot1
) {
1631 if (*pstart
!= -1) {
1632 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
1633 TARGET_FMT_plx
" %c%c%c\n",
1634 *pstart
, end
, end
- *pstart
,
1635 prot1
& PG_USER_MASK
? 'u' : '-',
1637 prot1
& PG_RW_MASK
? 'w' : '-');
1647 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
1649 unsigned int l1
, l2
;
1650 int prot
, last_prot
;
1651 uint32_t pgd
, pde
, pte
;
1654 pgd
= env
->cr
[3] & ~0xfff;
1657 for(l1
= 0; l1
< 1024; l1
++) {
1658 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1659 pde
= le32_to_cpu(pde
);
1661 if (pde
& PG_PRESENT_MASK
) {
1662 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1663 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1664 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1666 for(l2
= 0; l2
< 1024; l2
++) {
1667 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1668 pte
= le32_to_cpu(pte
);
1669 end
= (l1
<< 22) + (l2
<< 12);
1670 if (pte
& PG_PRESENT_MASK
) {
1672 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1676 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1681 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1684 /* Flush last range */
1685 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1688 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1690 unsigned int l1
, l2
, l3
;
1691 int prot
, last_prot
;
1692 uint64_t pdpe
, pde
, pte
;
1693 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1696 pdp_addr
= env
->cr
[3] & ~0x1f;
1699 for (l1
= 0; l1
< 4; l1
++) {
1700 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1701 pdpe
= le64_to_cpu(pdpe
);
1703 if (pdpe
& PG_PRESENT_MASK
) {
1704 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1705 for (l2
= 0; l2
< 512; l2
++) {
1706 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1707 pde
= le64_to_cpu(pde
);
1708 end
= (l1
<< 30) + (l2
<< 21);
1709 if (pde
& PG_PRESENT_MASK
) {
1710 if (pde
& PG_PSE_MASK
) {
1711 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1713 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1715 pt_addr
= pde
& 0x3fffffffff000ULL
;
1716 for (l3
= 0; l3
< 512; l3
++) {
1717 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1718 pte
= le64_to_cpu(pte
);
1719 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
1720 if (pte
& PG_PRESENT_MASK
) {
1721 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
1726 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1731 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1736 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1739 /* Flush last range */
1740 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1744 #ifdef TARGET_X86_64
1745 static void mem_info_64(Monitor
*mon
, CPUArchState
*env
)
1747 int prot
, last_prot
;
1748 uint64_t l1
, l2
, l3
, l4
;
1749 uint64_t pml4e
, pdpe
, pde
, pte
;
1750 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
1752 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1755 for (l1
= 0; l1
< 512; l1
++) {
1756 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1757 pml4e
= le64_to_cpu(pml4e
);
1759 if (pml4e
& PG_PRESENT_MASK
) {
1760 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1761 for (l2
= 0; l2
< 512; l2
++) {
1762 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1763 pdpe
= le64_to_cpu(pdpe
);
1764 end
= (l1
<< 39) + (l2
<< 30);
1765 if (pdpe
& PG_PRESENT_MASK
) {
1766 if (pdpe
& PG_PSE_MASK
) {
1767 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
1770 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1772 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1773 for (l3
= 0; l3
< 512; l3
++) {
1774 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1775 pde
= le64_to_cpu(pde
);
1776 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
1777 if (pde
& PG_PRESENT_MASK
) {
1778 if (pde
& PG_PSE_MASK
) {
1779 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1781 prot
&= pml4e
& pdpe
;
1782 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1784 pt_addr
= pde
& 0x3fffffffff000ULL
;
1785 for (l4
= 0; l4
< 512; l4
++) {
1786 cpu_physical_memory_read(pt_addr
1789 pte
= le64_to_cpu(pte
);
1790 end
= (l1
<< 39) + (l2
<< 30) +
1791 (l3
<< 21) + (l4
<< 12);
1792 if (pte
& PG_PRESENT_MASK
) {
1793 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
1795 prot
&= pml4e
& pdpe
& pde
;
1799 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1804 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1810 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1815 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1818 /* Flush last range */
1819 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 48, 0);
1823 static void hmp_info_mem(Monitor
*mon
, const QDict
*qdict
)
1827 env
= mon_get_cpu();
1829 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1830 monitor_printf(mon
, "PG disabled\n");
1833 if (env
->cr
[4] & CR4_PAE_MASK
) {
1834 #ifdef TARGET_X86_64
1835 if (env
->hflags
& HF_LMA_MASK
) {
1836 mem_info_64(mon
, env
);
1840 mem_info_pae32(mon
, env
);
1843 mem_info_32(mon
, env
);
1848 #if defined(TARGET_SH4)
1850 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1852 monitor_printf(mon
, " tlb%i:\t"
1853 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1854 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1855 "dirty=%hhu writethrough=%hhu\n",
1857 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1858 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1862 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1864 CPUArchState
*env
= mon_get_cpu();
1867 monitor_printf (mon
, "ITLB:\n");
1868 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1869 print_tlb (mon
, i
, &env
->itlb
[i
]);
1870 monitor_printf (mon
, "UTLB:\n");
1871 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1872 print_tlb (mon
, i
, &env
->utlb
[i
]);
1877 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1878 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1880 CPUArchState
*env1
= mon_get_cpu();
1882 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
1886 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1888 mtree_info((fprintf_function
)monitor_printf
, mon
);
1891 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1897 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1898 query_numa_node_mem(node_mem
);
1899 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1900 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1901 monitor_printf(mon
, "node %d cpus:", i
);
1903 if (cpu
->numa_node
== i
) {
1904 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1907 monitor_printf(mon
, "\n");
1908 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1914 #ifdef CONFIG_PROFILER
1919 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1921 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1922 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1923 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1924 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1929 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1931 monitor_printf(mon
, "Internal profiler not compiled\n");
1935 /* Capture support */
1936 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1938 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1943 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1944 monitor_printf(mon
, "[%d]: ", i
);
1945 s
->ops
.info (s
->opaque
);
1949 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1952 int n
= qdict_get_int(qdict
, "n");
1955 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1957 s
->ops
.destroy (s
->opaque
);
1958 QLIST_REMOVE (s
, entries
);
1965 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1967 const char *path
= qdict_get_str(qdict
, "path");
1968 int has_freq
= qdict_haskey(qdict
, "freq");
1969 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1970 int has_bits
= qdict_haskey(qdict
, "bits");
1971 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1972 int has_channels
= qdict_haskey(qdict
, "nchannels");
1973 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1976 s
= g_malloc0 (sizeof (*s
));
1978 freq
= has_freq
? freq
: 44100;
1979 bits
= has_bits
? bits
: 16;
1980 nchannels
= has_channels
? nchannels
: 2;
1982 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1983 monitor_printf(mon
, "Failed to add wave capture\n");
1987 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1990 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1992 qemu_acl
*acl
= qemu_acl_find(name
);
1995 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2000 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
2002 const char *aclname
= qdict_get_str(qdict
, "aclname");
2003 qemu_acl
*acl
= find_acl(mon
, aclname
);
2004 qemu_acl_entry
*entry
;
2008 monitor_printf(mon
, "policy: %s\n",
2009 acl
->defaultDeny
? "deny" : "allow");
2010 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2012 monitor_printf(mon
, "%d: %s %s\n", i
,
2013 entry
->deny
? "deny" : "allow", entry
->match
);
2018 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2020 const char *aclname
= qdict_get_str(qdict
, "aclname");
2021 qemu_acl
*acl
= find_acl(mon
, aclname
);
2024 qemu_acl_reset(acl
);
2025 monitor_printf(mon
, "acl: removed all rules\n");
2029 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2031 const char *aclname
= qdict_get_str(qdict
, "aclname");
2032 const char *policy
= qdict_get_str(qdict
, "policy");
2033 qemu_acl
*acl
= find_acl(mon
, aclname
);
2036 if (strcmp(policy
, "allow") == 0) {
2037 acl
->defaultDeny
= 0;
2038 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2039 } else if (strcmp(policy
, "deny") == 0) {
2040 acl
->defaultDeny
= 1;
2041 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2043 monitor_printf(mon
, "acl: unknown policy '%s', "
2044 "expected 'deny' or 'allow'\n", policy
);
2049 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2051 const char *aclname
= qdict_get_str(qdict
, "aclname");
2052 const char *match
= qdict_get_str(qdict
, "match");
2053 const char *policy
= qdict_get_str(qdict
, "policy");
2054 int has_index
= qdict_haskey(qdict
, "index");
2055 int index
= qdict_get_try_int(qdict
, "index", -1);
2056 qemu_acl
*acl
= find_acl(mon
, aclname
);
2060 if (strcmp(policy
, "allow") == 0) {
2062 } else if (strcmp(policy
, "deny") == 0) {
2065 monitor_printf(mon
, "acl: unknown policy '%s', "
2066 "expected 'deny' or 'allow'\n", policy
);
2070 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2072 ret
= qemu_acl_append(acl
, deny
, match
);
2074 monitor_printf(mon
, "acl: unable to add acl entry\n");
2076 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2080 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2082 const char *aclname
= qdict_get_str(qdict
, "aclname");
2083 const char *match
= qdict_get_str(qdict
, "match");
2084 qemu_acl
*acl
= find_acl(mon
, aclname
);
2088 ret
= qemu_acl_remove(acl
, match
);
2090 monitor_printf(mon
, "acl: no matching acl entry\n");
2092 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2096 #if defined(TARGET_I386)
2097 static void hmp_mce(Monitor
*mon
, const QDict
*qdict
)
2101 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2102 int bank
= qdict_get_int(qdict
, "bank");
2103 uint64_t status
= qdict_get_int(qdict
, "status");
2104 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2105 uint64_t addr
= qdict_get_int(qdict
, "addr");
2106 uint64_t misc
= qdict_get_int(qdict
, "misc");
2107 int flags
= MCE_INJECT_UNCOND_AO
;
2109 if (qdict_get_try_bool(qdict
, "broadcast", 0)) {
2110 flags
|= MCE_INJECT_BROADCAST
;
2112 cs
= qemu_get_cpu(cpu_index
);
2115 cpu_x86_inject_mce(mon
, cpu
, bank
, status
, mcg_status
, addr
, misc
,
2121 void qmp_getfd(const char *fdname
, Error
**errp
)
2126 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
2128 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2132 if (qemu_isdigit(fdname
[0])) {
2134 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2135 "a name not starting with a digit");
2139 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2140 if (strcmp(monfd
->name
, fdname
) != 0) {
2149 monfd
= g_malloc0(sizeof(mon_fd_t
));
2150 monfd
->name
= g_strdup(fdname
);
2153 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2156 void qmp_closefd(const char *fdname
, Error
**errp
)
2160 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2161 if (strcmp(monfd
->name
, fdname
) != 0) {
2165 QLIST_REMOVE(monfd
, next
);
2167 g_free(monfd
->name
);
2172 error_set(errp
, QERR_FD_NOT_FOUND
, fdname
);
2175 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
2177 int saved_vm_running
= runstate_is_running();
2178 const char *name
= qdict_get_str(qdict
, "name");
2180 vm_stop(RUN_STATE_RESTORE_VM
);
2182 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2187 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2191 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2194 if (strcmp(monfd
->name
, fdname
) != 0) {
2200 /* caller takes ownership of fd */
2201 QLIST_REMOVE(monfd
, next
);
2202 g_free(monfd
->name
);
2208 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2212 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2214 MonFdsetFd
*mon_fdset_fd
;
2215 MonFdsetFd
*mon_fdset_fd_next
;
2217 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2218 if ((mon_fdset_fd
->removed
||
2219 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2220 runstate_is_running()) {
2221 close(mon_fdset_fd
->fd
);
2222 g_free(mon_fdset_fd
->opaque
);
2223 QLIST_REMOVE(mon_fdset_fd
, next
);
2224 g_free(mon_fdset_fd
);
2228 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2229 QLIST_REMOVE(mon_fdset
, next
);
2234 static void monitor_fdsets_cleanup(void)
2236 MonFdset
*mon_fdset
;
2237 MonFdset
*mon_fdset_next
;
2239 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2240 monitor_fdset_cleanup(mon_fdset
);
2244 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2245 const char *opaque
, Error
**errp
)
2248 Monitor
*mon
= cur_mon
;
2251 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2253 error_set(errp
, QERR_FD_NOT_SUPPLIED
);
2257 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2258 has_opaque
, opaque
, errp
);
2270 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2272 MonFdset
*mon_fdset
;
2273 MonFdsetFd
*mon_fdset_fd
;
2276 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2277 if (mon_fdset
->id
!= fdset_id
) {
2280 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2282 if (mon_fdset_fd
->fd
!= fd
) {
2285 mon_fdset_fd
->removed
= true;
2288 mon_fdset_fd
->removed
= true;
2291 if (has_fd
&& !mon_fdset_fd
) {
2294 monitor_fdset_cleanup(mon_fdset
);
2300 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2303 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2305 error_set(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2308 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2310 MonFdset
*mon_fdset
;
2311 MonFdsetFd
*mon_fdset_fd
;
2312 FdsetInfoList
*fdset_list
= NULL
;
2314 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2315 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2316 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2318 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2319 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2321 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2322 FdsetFdInfoList
*fdsetfd_info
;
2324 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2325 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2326 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2327 if (mon_fdset_fd
->opaque
) {
2328 fdsetfd_info
->value
->has_opaque
= true;
2329 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2331 fdsetfd_info
->value
->has_opaque
= false;
2334 fdsetfd_info
->next
= fdsetfd_list
;
2335 fdsetfd_list
= fdsetfd_info
;
2338 fdset_info
->value
->fds
= fdsetfd_list
;
2340 fdset_info
->next
= fdset_list
;
2341 fdset_list
= fdset_info
;
2347 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2348 bool has_opaque
, const char *opaque
,
2351 MonFdset
*mon_fdset
= NULL
;
2352 MonFdsetFd
*mon_fdset_fd
;
2356 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2357 /* Break if match found or match impossible due to ordering by ID */
2358 if (fdset_id
<= mon_fdset
->id
) {
2359 if (fdset_id
< mon_fdset
->id
) {
2367 if (mon_fdset
== NULL
) {
2368 int64_t fdset_id_prev
= -1;
2369 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2373 error_set(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2374 "a non-negative value");
2377 /* Use specified fdset ID */
2378 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2379 mon_fdset_cur
= mon_fdset
;
2380 if (fdset_id
< mon_fdset_cur
->id
) {
2385 /* Use first available fdset ID */
2386 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2387 mon_fdset_cur
= mon_fdset
;
2388 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2389 fdset_id_prev
= mon_fdset_cur
->id
;
2396 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2398 mon_fdset
->id
= fdset_id
;
2400 mon_fdset
->id
= fdset_id_prev
+ 1;
2403 /* The fdset list is ordered by fdset ID */
2404 if (!mon_fdset_cur
) {
2405 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2406 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2407 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2409 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2413 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2414 mon_fdset_fd
->fd
= fd
;
2415 mon_fdset_fd
->removed
= false;
2417 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2419 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2421 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2422 fdinfo
->fdset_id
= mon_fdset
->id
;
2423 fdinfo
->fd
= mon_fdset_fd
->fd
;
2428 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2431 MonFdset
*mon_fdset
;
2432 MonFdsetFd
*mon_fdset_fd
;
2435 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2436 if (mon_fdset
->id
!= fdset_id
) {
2439 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2440 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2441 if (mon_fd_flags
== -1) {
2445 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2446 return mon_fdset_fd
->fd
;
2458 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2460 MonFdset
*mon_fdset
;
2461 MonFdsetFd
*mon_fdset_fd_dup
;
2463 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2464 if (mon_fdset
->id
!= fdset_id
) {
2467 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2468 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2472 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2473 mon_fdset_fd_dup
->fd
= dup_fd
;
2474 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2480 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2482 MonFdset
*mon_fdset
;
2483 MonFdsetFd
*mon_fdset_fd_dup
;
2485 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2486 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2487 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2489 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2490 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2491 monitor_fdset_cleanup(mon_fdset
);
2495 return mon_fdset
->id
;
2503 int monitor_fdset_dup_fd_find(int dup_fd
)
2505 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2508 void monitor_fdset_dup_fd_remove(int dup_fd
)
2510 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2513 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2516 Error
*local_err
= NULL
;
2518 if (!qemu_isdigit(fdname
[0]) && mon
) {
2519 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2521 fd
= qemu_parse_fd(fdname
);
2523 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2528 error_propagate(errp
, local_err
);
2537 /* Please update hmp-commands.hx when adding or changing commands */
2538 static mon_cmd_t info_cmds
[] = {
2543 .help
= "show the version of QEMU",
2544 .mhandler
.cmd
= hmp_info_version
,
2550 .help
= "show the network state",
2551 .mhandler
.cmd
= hmp_info_network
,
2557 .help
= "show the character devices",
2558 .mhandler
.cmd
= hmp_info_chardev
,
2562 .args_type
= "nodes:-n,verbose:-v,device:B?",
2563 .params
= "[-n] [-v] [device]",
2564 .help
= "show info of one block device or all block devices "
2565 "(-n: show named nodes; -v: show details)",
2566 .mhandler
.cmd
= hmp_info_block
,
2569 .name
= "blockstats",
2572 .help
= "show block device statistics",
2573 .mhandler
.cmd
= hmp_info_blockstats
,
2576 .name
= "block-jobs",
2579 .help
= "show progress of ongoing block device operations",
2580 .mhandler
.cmd
= hmp_info_block_jobs
,
2583 .name
= "registers",
2586 .help
= "show the cpu registers",
2587 .mhandler
.cmd
= hmp_info_registers
,
2593 .help
= "show infos for each CPU",
2594 .mhandler
.cmd
= hmp_info_cpus
,
2600 .help
= "show the command line history",
2601 .mhandler
.cmd
= hmp_info_history
,
2603 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2604 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2609 .help
= "show the interrupts statistics (if available)",
2611 .mhandler
.cmd
= sun4m_hmp_info_irq
,
2612 #elif defined(TARGET_LM32)
2613 .mhandler
.cmd
= lm32_hmp_info_irq
,
2615 .mhandler
.cmd
= hmp_info_irq
,
2622 .help
= "show i8259 (PIC) state",
2624 .mhandler
.cmd
= sun4m_hmp_info_pic
,
2625 #elif defined(TARGET_LM32)
2626 .mhandler
.cmd
= lm32_hmp_info_pic
,
2628 .mhandler
.cmd
= hmp_info_pic
,
2636 .help
= "show PCI info",
2637 .mhandler
.cmd
= hmp_info_pci
,
2639 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2640 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2645 .help
= "show virtual to physical memory mappings",
2646 .mhandler
.cmd
= hmp_info_tlb
,
2649 #if defined(TARGET_I386)
2654 .help
= "show the active virtual memory mappings",
2655 .mhandler
.cmd
= hmp_info_mem
,
2662 .help
= "show memory tree",
2663 .mhandler
.cmd
= hmp_info_mtree
,
2669 .help
= "show dynamic compiler info",
2670 .mhandler
.cmd
= hmp_info_jit
,
2676 .help
= "show dynamic compiler opcode counters",
2677 .mhandler
.cmd
= hmp_info_opcount
,
2683 .help
= "show KVM information",
2684 .mhandler
.cmd
= hmp_info_kvm
,
2690 .help
= "show NUMA information",
2691 .mhandler
.cmd
= hmp_info_numa
,
2697 .help
= "show guest USB devices",
2698 .mhandler
.cmd
= hmp_info_usb
,
2704 .help
= "show host USB devices",
2705 .mhandler
.cmd
= hmp_info_usbhost
,
2711 .help
= "show profiling information",
2712 .mhandler
.cmd
= hmp_info_profile
,
2718 .help
= "show capture information",
2719 .mhandler
.cmd
= hmp_info_capture
,
2722 .name
= "snapshots",
2725 .help
= "show the currently saved VM snapshots",
2726 .mhandler
.cmd
= hmp_info_snapshots
,
2732 .help
= "show the current VM status (running|paused)",
2733 .mhandler
.cmd
= hmp_info_status
,
2739 .help
= "show which guest mouse is receiving events",
2740 .mhandler
.cmd
= hmp_info_mice
,
2746 .help
= "show the vnc server status",
2747 .mhandler
.cmd
= hmp_info_vnc
,
2749 #if defined(CONFIG_SPICE)
2754 .help
= "show the spice server status",
2755 .mhandler
.cmd
= hmp_info_spice
,
2762 .help
= "show the current VM name",
2763 .mhandler
.cmd
= hmp_info_name
,
2769 .help
= "show the current VM UUID",
2770 .mhandler
.cmd
= hmp_info_uuid
,
2776 .help
= "show CPU statistics",
2777 .mhandler
.cmd
= hmp_info_cpustats
,
2779 #if defined(CONFIG_SLIRP)
2784 .help
= "show user network stack connection states",
2785 .mhandler
.cmd
= hmp_info_usernet
,
2792 .help
= "show migration status",
2793 .mhandler
.cmd
= hmp_info_migrate
,
2796 .name
= "migrate_capabilities",
2799 .help
= "show current migration capabilities",
2800 .mhandler
.cmd
= hmp_info_migrate_capabilities
,
2803 .name
= "migrate_parameters",
2806 .help
= "show current migration parameters",
2807 .mhandler
.cmd
= hmp_info_migrate_parameters
,
2810 .name
= "migrate_cache_size",
2813 .help
= "show current migration xbzrle cache size",
2814 .mhandler
.cmd
= hmp_info_migrate_cache_size
,
2820 .help
= "show balloon information",
2821 .mhandler
.cmd
= hmp_info_balloon
,
2827 .help
= "show device tree",
2828 .mhandler
.cmd
= hmp_info_qtree
,
2834 .help
= "show qdev device model list",
2835 .mhandler
.cmd
= hmp_info_qdm
,
2839 .args_type
= "path:s?",
2841 .help
= "show QOM composition tree",
2842 .mhandler
.cmd
= hmp_info_qom_tree
,
2848 .help
= "show roms",
2849 .mhandler
.cmd
= hmp_info_roms
,
2852 .name
= "trace-events",
2855 .help
= "show available trace-events & their state",
2856 .mhandler
.cmd
= hmp_info_trace_events
,
2862 .help
= "show the TPM device",
2863 .mhandler
.cmd
= hmp_info_tpm
,
2869 .help
= "show memory backends",
2870 .mhandler
.cmd
= hmp_info_memdev
,
2873 .name
= "memory-devices",
2876 .help
= "show memory devices",
2877 .mhandler
.cmd
= hmp_info_memory_devices
,
2884 /* mon_cmds and info_cmds would be sorted at runtime */
2885 static mon_cmd_t mon_cmds
[] = {
2886 #include "hmp-commands.h"
2890 static const mon_cmd_t qmp_cmds
[] = {
2891 #include "qmp-commands-old.h"
2895 /*******************************************************************/
2897 static const char *pch
;
2898 static sigjmp_buf expr_env
;
2903 typedef struct MonitorDef
{
2906 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2910 #if defined(TARGET_I386)
2911 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2913 CPUArchState
*env
= mon_get_cpu();
2914 return env
->eip
+ env
->segs
[R_CS
].base
;
2918 #if defined(TARGET_PPC)
2919 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2921 CPUArchState
*env
= mon_get_cpu();
2926 for (i
= 0; i
< 8; i
++)
2927 u
|= env
->crf
[i
] << (32 - (4 * (i
+ 1)));
2932 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2934 CPUArchState
*env
= mon_get_cpu();
2938 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2940 CPUArchState
*env
= mon_get_cpu();
2944 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2946 CPUArchState
*env
= mon_get_cpu();
2947 return cpu_ppc_load_decr(env
);
2950 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2952 CPUArchState
*env
= mon_get_cpu();
2953 return cpu_ppc_load_tbu(env
);
2956 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2958 CPUArchState
*env
= mon_get_cpu();
2959 return cpu_ppc_load_tbl(env
);
2963 #if defined(TARGET_SPARC)
2964 #ifndef TARGET_SPARC64
2965 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2967 CPUArchState
*env
= mon_get_cpu();
2969 return cpu_get_psr(env
);
2973 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2975 CPUArchState
*env
= mon_get_cpu();
2976 return env
->regwptr
[val
];
2980 static const MonitorDef monitor_defs
[] = {
2983 #define SEG(name, seg) \
2984 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2985 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2986 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2988 { "eax", offsetof(CPUX86State
, regs
[0]) },
2989 { "ecx", offsetof(CPUX86State
, regs
[1]) },
2990 { "edx", offsetof(CPUX86State
, regs
[2]) },
2991 { "ebx", offsetof(CPUX86State
, regs
[3]) },
2992 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
2993 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
2994 { "esi", offsetof(CPUX86State
, regs
[6]) },
2995 { "edi", offsetof(CPUX86State
, regs
[7]) },
2996 #ifdef TARGET_X86_64
2997 { "r8", offsetof(CPUX86State
, regs
[8]) },
2998 { "r9", offsetof(CPUX86State
, regs
[9]) },
2999 { "r10", offsetof(CPUX86State
, regs
[10]) },
3000 { "r11", offsetof(CPUX86State
, regs
[11]) },
3001 { "r12", offsetof(CPUX86State
, regs
[12]) },
3002 { "r13", offsetof(CPUX86State
, regs
[13]) },
3003 { "r14", offsetof(CPUX86State
, regs
[14]) },
3004 { "r15", offsetof(CPUX86State
, regs
[15]) },
3006 { "eflags", offsetof(CPUX86State
, eflags
) },
3007 { "eip", offsetof(CPUX86State
, eip
) },
3014 { "pc", 0, monitor_get_pc
, },
3015 #elif defined(TARGET_PPC)
3016 /* General purpose registers */
3017 { "r0", offsetof(CPUPPCState
, gpr
[0]) },
3018 { "r1", offsetof(CPUPPCState
, gpr
[1]) },
3019 { "r2", offsetof(CPUPPCState
, gpr
[2]) },
3020 { "r3", offsetof(CPUPPCState
, gpr
[3]) },
3021 { "r4", offsetof(CPUPPCState
, gpr
[4]) },
3022 { "r5", offsetof(CPUPPCState
, gpr
[5]) },
3023 { "r6", offsetof(CPUPPCState
, gpr
[6]) },
3024 { "r7", offsetof(CPUPPCState
, gpr
[7]) },
3025 { "r8", offsetof(CPUPPCState
, gpr
[8]) },
3026 { "r9", offsetof(CPUPPCState
, gpr
[9]) },
3027 { "r10", offsetof(CPUPPCState
, gpr
[10]) },
3028 { "r11", offsetof(CPUPPCState
, gpr
[11]) },
3029 { "r12", offsetof(CPUPPCState
, gpr
[12]) },
3030 { "r13", offsetof(CPUPPCState
, gpr
[13]) },
3031 { "r14", offsetof(CPUPPCState
, gpr
[14]) },
3032 { "r15", offsetof(CPUPPCState
, gpr
[15]) },
3033 { "r16", offsetof(CPUPPCState
, gpr
[16]) },
3034 { "r17", offsetof(CPUPPCState
, gpr
[17]) },
3035 { "r18", offsetof(CPUPPCState
, gpr
[18]) },
3036 { "r19", offsetof(CPUPPCState
, gpr
[19]) },
3037 { "r20", offsetof(CPUPPCState
, gpr
[20]) },
3038 { "r21", offsetof(CPUPPCState
, gpr
[21]) },
3039 { "r22", offsetof(CPUPPCState
, gpr
[22]) },
3040 { "r23", offsetof(CPUPPCState
, gpr
[23]) },
3041 { "r24", offsetof(CPUPPCState
, gpr
[24]) },
3042 { "r25", offsetof(CPUPPCState
, gpr
[25]) },
3043 { "r26", offsetof(CPUPPCState
, gpr
[26]) },
3044 { "r27", offsetof(CPUPPCState
, gpr
[27]) },
3045 { "r28", offsetof(CPUPPCState
, gpr
[28]) },
3046 { "r29", offsetof(CPUPPCState
, gpr
[29]) },
3047 { "r30", offsetof(CPUPPCState
, gpr
[30]) },
3048 { "r31", offsetof(CPUPPCState
, gpr
[31]) },
3049 /* Floating point registers */
3050 { "f0", offsetof(CPUPPCState
, fpr
[0]) },
3051 { "f1", offsetof(CPUPPCState
, fpr
[1]) },
3052 { "f2", offsetof(CPUPPCState
, fpr
[2]) },
3053 { "f3", offsetof(CPUPPCState
, fpr
[3]) },
3054 { "f4", offsetof(CPUPPCState
, fpr
[4]) },
3055 { "f5", offsetof(CPUPPCState
, fpr
[5]) },
3056 { "f6", offsetof(CPUPPCState
, fpr
[6]) },
3057 { "f7", offsetof(CPUPPCState
, fpr
[7]) },
3058 { "f8", offsetof(CPUPPCState
, fpr
[8]) },
3059 { "f9", offsetof(CPUPPCState
, fpr
[9]) },
3060 { "f10", offsetof(CPUPPCState
, fpr
[10]) },
3061 { "f11", offsetof(CPUPPCState
, fpr
[11]) },
3062 { "f12", offsetof(CPUPPCState
, fpr
[12]) },
3063 { "f13", offsetof(CPUPPCState
, fpr
[13]) },
3064 { "f14", offsetof(CPUPPCState
, fpr
[14]) },
3065 { "f15", offsetof(CPUPPCState
, fpr
[15]) },
3066 { "f16", offsetof(CPUPPCState
, fpr
[16]) },
3067 { "f17", offsetof(CPUPPCState
, fpr
[17]) },
3068 { "f18", offsetof(CPUPPCState
, fpr
[18]) },
3069 { "f19", offsetof(CPUPPCState
, fpr
[19]) },
3070 { "f20", offsetof(CPUPPCState
, fpr
[20]) },
3071 { "f21", offsetof(CPUPPCState
, fpr
[21]) },
3072 { "f22", offsetof(CPUPPCState
, fpr
[22]) },
3073 { "f23", offsetof(CPUPPCState
, fpr
[23]) },
3074 { "f24", offsetof(CPUPPCState
, fpr
[24]) },
3075 { "f25", offsetof(CPUPPCState
, fpr
[25]) },
3076 { "f26", offsetof(CPUPPCState
, fpr
[26]) },
3077 { "f27", offsetof(CPUPPCState
, fpr
[27]) },
3078 { "f28", offsetof(CPUPPCState
, fpr
[28]) },
3079 { "f29", offsetof(CPUPPCState
, fpr
[29]) },
3080 { "f30", offsetof(CPUPPCState
, fpr
[30]) },
3081 { "f31", offsetof(CPUPPCState
, fpr
[31]) },
3082 { "fpscr", offsetof(CPUPPCState
, fpscr
) },
3083 /* Next instruction pointer */
3084 { "nip|pc", offsetof(CPUPPCState
, nip
) },
3085 { "lr", offsetof(CPUPPCState
, lr
) },
3086 { "ctr", offsetof(CPUPPCState
, ctr
) },
3087 { "decr", 0, &monitor_get_decr
, },
3088 { "ccr", 0, &monitor_get_ccr
, },
3089 /* Machine state register */
3090 { "msr", 0, &monitor_get_msr
, },
3091 { "xer", 0, &monitor_get_xer
, },
3092 { "tbu", 0, &monitor_get_tbu
, },
3093 { "tbl", 0, &monitor_get_tbl
, },
3094 /* Segment registers */
3095 { "sdr1", offsetof(CPUPPCState
, spr
[SPR_SDR1
]) },
3096 { "sr0", offsetof(CPUPPCState
, sr
[0]) },
3097 { "sr1", offsetof(CPUPPCState
, sr
[1]) },
3098 { "sr2", offsetof(CPUPPCState
, sr
[2]) },
3099 { "sr3", offsetof(CPUPPCState
, sr
[3]) },
3100 { "sr4", offsetof(CPUPPCState
, sr
[4]) },
3101 { "sr5", offsetof(CPUPPCState
, sr
[5]) },
3102 { "sr6", offsetof(CPUPPCState
, sr
[6]) },
3103 { "sr7", offsetof(CPUPPCState
, sr
[7]) },
3104 { "sr8", offsetof(CPUPPCState
, sr
[8]) },
3105 { "sr9", offsetof(CPUPPCState
, sr
[9]) },
3106 { "sr10", offsetof(CPUPPCState
, sr
[10]) },
3107 { "sr11", offsetof(CPUPPCState
, sr
[11]) },
3108 { "sr12", offsetof(CPUPPCState
, sr
[12]) },
3109 { "sr13", offsetof(CPUPPCState
, sr
[13]) },
3110 { "sr14", offsetof(CPUPPCState
, sr
[14]) },
3111 { "sr15", offsetof(CPUPPCState
, sr
[15]) },
3112 /* Too lazy to put BATs... */
3113 { "pvr", offsetof(CPUPPCState
, spr
[SPR_PVR
]) },
3115 { "srr0", offsetof(CPUPPCState
, spr
[SPR_SRR0
]) },
3116 { "srr1", offsetof(CPUPPCState
, spr
[SPR_SRR1
]) },
3117 { "dar", offsetof(CPUPPCState
, spr
[SPR_DAR
]) },
3118 { "dsisr", offsetof(CPUPPCState
, spr
[SPR_DSISR
]) },
3119 { "cfar", offsetof(CPUPPCState
, spr
[SPR_CFAR
]) },
3120 { "sprg0", offsetof(CPUPPCState
, spr
[SPR_SPRG0
]) },
3121 { "sprg1", offsetof(CPUPPCState
, spr
[SPR_SPRG1
]) },
3122 { "sprg2", offsetof(CPUPPCState
, spr
[SPR_SPRG2
]) },
3123 { "sprg3", offsetof(CPUPPCState
, spr
[SPR_SPRG3
]) },
3124 { "sprg4", offsetof(CPUPPCState
, spr
[SPR_SPRG4
]) },
3125 { "sprg5", offsetof(CPUPPCState
, spr
[SPR_SPRG5
]) },
3126 { "sprg6", offsetof(CPUPPCState
, spr
[SPR_SPRG6
]) },
3127 { "sprg7", offsetof(CPUPPCState
, spr
[SPR_SPRG7
]) },
3128 { "pid", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID
]) },
3129 { "csrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR0
]) },
3130 { "csrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR1
]) },
3131 { "esr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_ESR
]) },
3132 { "dear", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DEAR
]) },
3133 { "mcsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSR
]) },
3134 { "tsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TSR
]) },
3135 { "tcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TCR
]) },
3136 { "vrsave", offsetof(CPUPPCState
, spr
[SPR_VRSAVE
]) },
3137 { "pir", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PIR
]) },
3138 { "mcsrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR0
]) },
3139 { "mcsrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR1
]) },
3140 { "decar", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DECAR
]) },
3141 { "ivpr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVPR
]) },
3142 { "epcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPCR
]) },
3143 { "sprg8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_SPRG8
]) },
3144 { "ivor0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR0
]) },
3145 { "ivor1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR1
]) },
3146 { "ivor2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR2
]) },
3147 { "ivor3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR3
]) },
3148 { "ivor4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR4
]) },
3149 { "ivor5", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR5
]) },
3150 { "ivor6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR6
]) },
3151 { "ivor7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR7
]) },
3152 { "ivor8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR8
]) },
3153 { "ivor9", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR9
]) },
3154 { "ivor10", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR10
]) },
3155 { "ivor11", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR11
]) },
3156 { "ivor12", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR12
]) },
3157 { "ivor13", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR13
]) },
3158 { "ivor14", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR14
]) },
3159 { "ivor15", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR15
]) },
3160 { "ivor32", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR32
]) },
3161 { "ivor33", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR33
]) },
3162 { "ivor34", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR34
]) },
3163 { "ivor35", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR35
]) },
3164 { "ivor36", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR36
]) },
3165 { "ivor37", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR37
]) },
3166 { "mas0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS0
]) },
3167 { "mas1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS1
]) },
3168 { "mas2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS2
]) },
3169 { "mas3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS3
]) },
3170 { "mas4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS4
]) },
3171 { "mas6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS6
]) },
3172 { "mas7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS7
]) },
3173 { "mmucfg", offsetof(CPUPPCState
, spr
[SPR_MMUCFG
]) },
3174 { "tlb0cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3175 { "tlb1cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3176 { "epr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPR
]) },
3177 { "eplc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPLC
]) },
3178 { "epsc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPSC
]) },
3179 { "svr", offsetof(CPUPPCState
, spr
[SPR_E500_SVR
]) },
3180 { "mcar", offsetof(CPUPPCState
, spr
[SPR_Exxx_MCAR
]) },
3181 { "pid1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID1
]) },
3182 { "pid2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID2
]) },
3183 { "hid0", offsetof(CPUPPCState
, spr
[SPR_HID0
]) },
3185 #elif defined(TARGET_SPARC)
3186 { "g0", offsetof(CPUSPARCState
, gregs
[0]) },
3187 { "g1", offsetof(CPUSPARCState
, gregs
[1]) },
3188 { "g2", offsetof(CPUSPARCState
, gregs
[2]) },
3189 { "g3", offsetof(CPUSPARCState
, gregs
[3]) },
3190 { "g4", offsetof(CPUSPARCState
, gregs
[4]) },
3191 { "g5", offsetof(CPUSPARCState
, gregs
[5]) },
3192 { "g6", offsetof(CPUSPARCState
, gregs
[6]) },
3193 { "g7", offsetof(CPUSPARCState
, gregs
[7]) },
3194 { "o0", 0, monitor_get_reg
},
3195 { "o1", 1, monitor_get_reg
},
3196 { "o2", 2, monitor_get_reg
},
3197 { "o3", 3, monitor_get_reg
},
3198 { "o4", 4, monitor_get_reg
},
3199 { "o5", 5, monitor_get_reg
},
3200 { "o6", 6, monitor_get_reg
},
3201 { "o7", 7, monitor_get_reg
},
3202 { "l0", 8, monitor_get_reg
},
3203 { "l1", 9, monitor_get_reg
},
3204 { "l2", 10, monitor_get_reg
},
3205 { "l3", 11, monitor_get_reg
},
3206 { "l4", 12, monitor_get_reg
},
3207 { "l5", 13, monitor_get_reg
},
3208 { "l6", 14, monitor_get_reg
},
3209 { "l7", 15, monitor_get_reg
},
3210 { "i0", 16, monitor_get_reg
},
3211 { "i1", 17, monitor_get_reg
},
3212 { "i2", 18, monitor_get_reg
},
3213 { "i3", 19, monitor_get_reg
},
3214 { "i4", 20, monitor_get_reg
},
3215 { "i5", 21, monitor_get_reg
},
3216 { "i6", 22, monitor_get_reg
},
3217 { "i7", 23, monitor_get_reg
},
3218 { "pc", offsetof(CPUSPARCState
, pc
) },
3219 { "npc", offsetof(CPUSPARCState
, npc
) },
3220 { "y", offsetof(CPUSPARCState
, y
) },
3221 #ifndef TARGET_SPARC64
3222 { "psr", 0, &monitor_get_psr
, },
3223 { "wim", offsetof(CPUSPARCState
, wim
) },
3225 { "tbr", offsetof(CPUSPARCState
, tbr
) },
3226 { "fsr", offsetof(CPUSPARCState
, fsr
) },
3227 { "f0", offsetof(CPUSPARCState
, fpr
[0].l
.upper
) },
3228 { "f1", offsetof(CPUSPARCState
, fpr
[0].l
.lower
) },
3229 { "f2", offsetof(CPUSPARCState
, fpr
[1].l
.upper
) },
3230 { "f3", offsetof(CPUSPARCState
, fpr
[1].l
.lower
) },
3231 { "f4", offsetof(CPUSPARCState
, fpr
[2].l
.upper
) },
3232 { "f5", offsetof(CPUSPARCState
, fpr
[2].l
.lower
) },
3233 { "f6", offsetof(CPUSPARCState
, fpr
[3].l
.upper
) },
3234 { "f7", offsetof(CPUSPARCState
, fpr
[3].l
.lower
) },
3235 { "f8", offsetof(CPUSPARCState
, fpr
[4].l
.upper
) },
3236 { "f9", offsetof(CPUSPARCState
, fpr
[4].l
.lower
) },
3237 { "f10", offsetof(CPUSPARCState
, fpr
[5].l
.upper
) },
3238 { "f11", offsetof(CPUSPARCState
, fpr
[5].l
.lower
) },
3239 { "f12", offsetof(CPUSPARCState
, fpr
[6].l
.upper
) },
3240 { "f13", offsetof(CPUSPARCState
, fpr
[6].l
.lower
) },
3241 { "f14", offsetof(CPUSPARCState
, fpr
[7].l
.upper
) },
3242 { "f15", offsetof(CPUSPARCState
, fpr
[7].l
.lower
) },
3243 { "f16", offsetof(CPUSPARCState
, fpr
[8].l
.upper
) },
3244 { "f17", offsetof(CPUSPARCState
, fpr
[8].l
.lower
) },
3245 { "f18", offsetof(CPUSPARCState
, fpr
[9].l
.upper
) },
3246 { "f19", offsetof(CPUSPARCState
, fpr
[9].l
.lower
) },
3247 { "f20", offsetof(CPUSPARCState
, fpr
[10].l
.upper
) },
3248 { "f21", offsetof(CPUSPARCState
, fpr
[10].l
.lower
) },
3249 { "f22", offsetof(CPUSPARCState
, fpr
[11].l
.upper
) },
3250 { "f23", offsetof(CPUSPARCState
, fpr
[11].l
.lower
) },
3251 { "f24", offsetof(CPUSPARCState
, fpr
[12].l
.upper
) },
3252 { "f25", offsetof(CPUSPARCState
, fpr
[12].l
.lower
) },
3253 { "f26", offsetof(CPUSPARCState
, fpr
[13].l
.upper
) },
3254 { "f27", offsetof(CPUSPARCState
, fpr
[13].l
.lower
) },
3255 { "f28", offsetof(CPUSPARCState
, fpr
[14].l
.upper
) },
3256 { "f29", offsetof(CPUSPARCState
, fpr
[14].l
.lower
) },
3257 { "f30", offsetof(CPUSPARCState
, fpr
[15].l
.upper
) },
3258 { "f31", offsetof(CPUSPARCState
, fpr
[15].l
.lower
) },
3259 #ifdef TARGET_SPARC64
3260 { "f32", offsetof(CPUSPARCState
, fpr
[16]) },
3261 { "f34", offsetof(CPUSPARCState
, fpr
[17]) },
3262 { "f36", offsetof(CPUSPARCState
, fpr
[18]) },
3263 { "f38", offsetof(CPUSPARCState
, fpr
[19]) },
3264 { "f40", offsetof(CPUSPARCState
, fpr
[20]) },
3265 { "f42", offsetof(CPUSPARCState
, fpr
[21]) },
3266 { "f44", offsetof(CPUSPARCState
, fpr
[22]) },
3267 { "f46", offsetof(CPUSPARCState
, fpr
[23]) },
3268 { "f48", offsetof(CPUSPARCState
, fpr
[24]) },
3269 { "f50", offsetof(CPUSPARCState
, fpr
[25]) },
3270 { "f52", offsetof(CPUSPARCState
, fpr
[26]) },
3271 { "f54", offsetof(CPUSPARCState
, fpr
[27]) },
3272 { "f56", offsetof(CPUSPARCState
, fpr
[28]) },
3273 { "f58", offsetof(CPUSPARCState
, fpr
[29]) },
3274 { "f60", offsetof(CPUSPARCState
, fpr
[30]) },
3275 { "f62", offsetof(CPUSPARCState
, fpr
[31]) },
3276 { "asi", offsetof(CPUSPARCState
, asi
) },
3277 { "pstate", offsetof(CPUSPARCState
, pstate
) },
3278 { "cansave", offsetof(CPUSPARCState
, cansave
) },
3279 { "canrestore", offsetof(CPUSPARCState
, canrestore
) },
3280 { "otherwin", offsetof(CPUSPARCState
, otherwin
) },
3281 { "wstate", offsetof(CPUSPARCState
, wstate
) },
3282 { "cleanwin", offsetof(CPUSPARCState
, cleanwin
) },
3283 { "fprs", offsetof(CPUSPARCState
, fprs
) },
3289 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3290 expr_error(Monitor
*mon
, const char *fmt
, ...)
3294 monitor_vprintf(mon
, fmt
, ap
);
3295 monitor_printf(mon
, "\n");
3297 siglongjmp(expr_env
, 1);
3300 /* return 0 if OK, -1 if not found */
3301 static int get_monitor_def(target_long
*pval
, const char *name
)
3303 const MonitorDef
*md
;
3306 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3307 if (compare_cmd(name
, md
->name
)) {
3308 if (md
->get_value
) {
3309 *pval
= md
->get_value(md
, md
->offset
);
3311 CPUArchState
*env
= mon_get_cpu();
3312 ptr
= (uint8_t *)env
+ md
->offset
;
3315 *pval
= *(int32_t *)ptr
;
3318 *pval
= *(target_long
*)ptr
;
3331 static void next(void)
3335 while (qemu_isspace(*pch
))
3340 static int64_t expr_sum(Monitor
*mon
);
3342 static int64_t expr_unary(Monitor
*mon
)
3351 n
= expr_unary(mon
);
3355 n
= -expr_unary(mon
);
3359 n
= ~expr_unary(mon
);
3365 expr_error(mon
, "')' expected");
3372 expr_error(mon
, "character constant expected");
3376 expr_error(mon
, "missing terminating \' character");
3386 while ((*pch
>= 'a' && *pch
<= 'z') ||
3387 (*pch
>= 'A' && *pch
<= 'Z') ||
3388 (*pch
>= '0' && *pch
<= '9') ||
3389 *pch
== '_' || *pch
== '.') {
3390 if ((q
- buf
) < sizeof(buf
) - 1)
3394 while (qemu_isspace(*pch
))
3397 ret
= get_monitor_def(®
, buf
);
3399 expr_error(mon
, "unknown register");
3404 expr_error(mon
, "unexpected end of expression");
3409 n
= strtoull(pch
, &p
, 0);
3410 if (errno
== ERANGE
) {
3411 expr_error(mon
, "number too large");
3414 expr_error(mon
, "invalid char '%c' in expression", *p
);
3417 while (qemu_isspace(*pch
))
3425 static int64_t expr_prod(Monitor
*mon
)
3430 val
= expr_unary(mon
);
3433 if (op
!= '*' && op
!= '/' && op
!= '%')
3436 val2
= expr_unary(mon
);
3445 expr_error(mon
, "division by zero");
3456 static int64_t expr_logic(Monitor
*mon
)
3461 val
= expr_prod(mon
);
3464 if (op
!= '&' && op
!= '|' && op
!= '^')
3467 val2
= expr_prod(mon
);
3484 static int64_t expr_sum(Monitor
*mon
)
3489 val
= expr_logic(mon
);
3492 if (op
!= '+' && op
!= '-')
3495 val2
= expr_logic(mon
);
3504 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3507 if (sigsetjmp(expr_env
, 0)) {
3511 while (qemu_isspace(*pch
))
3513 *pval
= expr_sum(mon
);
3518 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3520 const char *p
= *pp
;
3524 d
= strtod(p
, &tailp
);
3526 monitor_printf(mon
, "Number expected\n");
3529 if (d
!= d
|| d
- d
!= 0) {
3530 /* NaN or infinity */
3531 monitor_printf(mon
, "Bad number\n");
3540 * Store the command-name in cmdname, and return a pointer to
3541 * the remaining of the command string.
3543 static const char *get_command_name(const char *cmdline
,
3544 char *cmdname
, size_t nlen
)
3547 const char *p
, *pstart
;
3550 while (qemu_isspace(*p
))
3555 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3560 memcpy(cmdname
, pstart
, len
);
3561 cmdname
[len
] = '\0';
3566 * Read key of 'type' into 'key' and return the current
3569 static char *key_get_info(const char *type
, char **key
)
3577 p
= strchr(type
, ':');
3584 str
= g_malloc(len
+ 1);
3585 memcpy(str
, type
, len
);
3592 static int default_fmt_format
= 'x';
3593 static int default_fmt_size
= 4;
3595 static int is_valid_option(const char *c
, const char *typestr
)
3603 typestr
= strstr(typestr
, option
);
3604 return (typestr
!= NULL
);
3607 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3608 const char *cmdname
)
3610 const mon_cmd_t
*cmd
;
3612 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3613 if (compare_cmd(cmdname
, cmd
->name
)) {
3621 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3623 return search_dispatch_table(qmp_cmds
, cmdname
);
3627 * Parse @cmdline according to command table @table.
3628 * If @cmdline is blank, return NULL.
3629 * If it can't be parsed, report to @mon, and return NULL.
3630 * Else, insert command arguments into @qdict, and return the command.
3631 * If a sub-command table exists, and if @cmdline contains an additional string
3632 * for a sub-command, this function will try to search the sub-command table.
3633 * If no additional string for a sub-command is present, this function will
3634 * return the command found in @table.
3635 * Do not assume the returned command points into @table! It doesn't
3636 * when the command is a sub-command.
3638 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3639 const char *cmdline
,
3644 const char *p
, *typestr
;
3646 const mon_cmd_t
*cmd
;
3652 monitor_printf(mon
, "command='%s', start='%d'\n", cmdline
, start
);
3655 /* extract the command name */
3656 p
= get_command_name(cmdline
+ start
, cmdname
, sizeof(cmdname
));
3660 cmd
= search_dispatch_table(table
, cmdname
);
3662 monitor_printf(mon
, "unknown command: '%.*s'\n",
3663 (int)(p
- cmdline
), cmdline
);
3667 /* filter out following useless space */
3668 while (qemu_isspace(*p
)) {
3671 /* search sub command */
3672 if (cmd
->sub_table
!= NULL
) {
3673 /* check if user set additional command */
3677 return monitor_parse_command(mon
, cmdline
, p
- cmdline
,
3678 cmd
->sub_table
, qdict
);
3681 /* parse the parameters */
3682 typestr
= cmd
->args_type
;
3684 typestr
= key_get_info(typestr
, &key
);
3696 while (qemu_isspace(*p
))
3698 if (*typestr
== '?') {
3701 /* no optional string: NULL argument */
3705 ret
= get_str(buf
, sizeof(buf
), &p
);
3709 monitor_printf(mon
, "%s: filename expected\n",
3713 monitor_printf(mon
, "%s: block device name expected\n",
3717 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3722 qdict_put(qdict
, key
, qstring_from_str(buf
));
3727 QemuOptsList
*opts_list
;
3730 opts_list
= qemu_find_opts(key
);
3731 if (!opts_list
|| opts_list
->desc
->name
) {
3734 while (qemu_isspace(*p
)) {
3739 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3742 opts
= qemu_opts_parse(opts_list
, buf
, 1);
3746 qemu_opts_to_qdict(opts
, qdict
);
3747 qemu_opts_del(opts
);
3752 int count
, format
, size
;
3754 while (qemu_isspace(*p
))
3760 if (qemu_isdigit(*p
)) {
3762 while (qemu_isdigit(*p
)) {
3763 count
= count
* 10 + (*p
- '0');
3801 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3802 monitor_printf(mon
, "invalid char in format: '%c'\n",
3807 format
= default_fmt_format
;
3808 if (format
!= 'i') {
3809 /* for 'i', not specifying a size gives -1 as size */
3811 size
= default_fmt_size
;
3812 default_fmt_size
= size
;
3814 default_fmt_format
= format
;
3817 format
= default_fmt_format
;
3818 if (format
!= 'i') {
3819 size
= default_fmt_size
;
3824 qdict_put(qdict
, "count", qint_from_int(count
));
3825 qdict_put(qdict
, "format", qint_from_int(format
));
3826 qdict_put(qdict
, "size", qint_from_int(size
));
3835 while (qemu_isspace(*p
))
3837 if (*typestr
== '?' || *typestr
== '.') {
3838 if (*typestr
== '?') {
3846 while (qemu_isspace(*p
))
3855 if (get_expr(mon
, &val
, &p
))
3857 /* Check if 'i' is greater than 32-bit */
3858 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3859 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3860 monitor_printf(mon
, "integer is for 32-bit values\n");
3862 } else if (c
== 'M') {
3864 monitor_printf(mon
, "enter a positive value\n");
3869 qdict_put(qdict
, key
, qint_from_int(val
));
3877 while (qemu_isspace(*p
)) {
3880 if (*typestr
== '?') {
3886 val
= strtosz(p
, &end
);
3888 monitor_printf(mon
, "invalid size\n");
3891 qdict_put(qdict
, key
, qint_from_int(val
));
3899 while (qemu_isspace(*p
))
3901 if (*typestr
== '?') {
3907 if (get_double(mon
, &val
, &p
) < 0) {
3910 if (p
[0] && p
[1] == 's') {
3913 val
/= 1e3
; p
+= 2; break;
3915 val
/= 1e6
; p
+= 2; break;
3917 val
/= 1e9
; p
+= 2; break;
3920 if (*p
&& !qemu_isspace(*p
)) {
3921 monitor_printf(mon
, "Unknown unit suffix\n");
3924 qdict_put(qdict
, key
, qfloat_from_double(val
));
3932 while (qemu_isspace(*p
)) {
3936 while (qemu_isgraph(*p
)) {
3939 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3941 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3944 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3947 qdict_put(qdict
, key
, qbool_from_int(val
));
3952 const char *tmp
= p
;
3959 while (qemu_isspace(*p
))
3964 if(!is_valid_option(p
, typestr
)) {
3966 monitor_printf(mon
, "%s: unsupported option -%c\n",
3978 qdict_put(qdict
, key
, qbool_from_int(1));
3985 /* package all remaining string */
3988 while (qemu_isspace(*p
)) {
3991 if (*typestr
== '?') {
3994 /* no remaining string: NULL argument */
4000 monitor_printf(mon
, "%s: string expected\n",
4004 qdict_put(qdict
, key
, qstring_from_str(p
));
4010 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
4016 /* check that all arguments were parsed */
4017 while (qemu_isspace(*p
))
4020 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4032 void monitor_set_error(Monitor
*mon
, QError
*qerror
)
4034 /* report only the first error */
4036 mon
->error
= qerror
;
4042 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
4045 const mon_cmd_t
*cmd
;
4047 qdict
= qdict_new();
4049 cmd
= monitor_parse_command(mon
, cmdline
, 0, mon
->cmd_table
, qdict
);
4053 if (handler_is_qobject(cmd
)) {
4054 QObject
*data
= NULL
;
4056 /* XXX: ignores the error code */
4057 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
4058 assert(!monitor_has_error(mon
));
4060 cmd
->user_print(mon
, data
);
4061 qobject_decref(data
);
4064 cmd
->mhandler
.cmd(mon
, qdict
);
4071 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
4073 const char *p
, *pstart
;
4082 p
= pstart
+ strlen(pstart
);
4084 if (len
> sizeof(cmd
) - 2)
4085 len
= sizeof(cmd
) - 2;
4086 memcpy(cmd
, pstart
, len
);
4088 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4089 readline_add_completion(mon
->rs
, cmd
);
4097 static void file_completion(Monitor
*mon
, const char *input
)
4102 char file
[1024], file_prefix
[1024];
4106 p
= strrchr(input
, '/');
4109 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4110 pstrcpy(path
, sizeof(path
), ".");
4112 input_path_len
= p
- input
+ 1;
4113 memcpy(path
, input
, input_path_len
);
4114 if (input_path_len
> sizeof(path
) - 1)
4115 input_path_len
= sizeof(path
) - 1;
4116 path
[input_path_len
] = '\0';
4117 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4119 #ifdef DEBUG_COMPLETION
4120 monitor_printf(mon
, "input='%s' path='%s' prefix='%s'\n",
4121 input
, path
, file_prefix
);
4123 ffs
= opendir(path
);
4132 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4136 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4137 memcpy(file
, input
, input_path_len
);
4138 if (input_path_len
< sizeof(file
))
4139 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4141 /* stat the file to find out if it's a directory.
4142 * In that case add a slash to speed up typing long paths
4144 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
4145 pstrcat(file
, sizeof(file
), "/");
4147 readline_add_completion(mon
->rs
, file
);
4153 static const char *next_arg_type(const char *typestr
)
4155 const char *p
= strchr(typestr
, ':');
4156 return (p
!= NULL
? ++p
: typestr
);
4159 static void add_completion_option(ReadLineState
*rs
, const char *str
,
4162 if (!str
|| !option
) {
4165 if (!strncmp(option
, str
, strlen(str
))) {
4166 readline_add_completion(rs
, option
);
4170 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4173 ChardevBackendInfoList
*list
, *start
;
4179 readline_set_completion_index(rs
, len
);
4181 start
= list
= qmp_query_chardev_backends(NULL
);
4183 const char *chr_name
= list
->value
->name
;
4185 if (!strncmp(chr_name
, str
, len
)) {
4186 readline_add_completion(rs
, chr_name
);
4190 qapi_free_ChardevBackendInfoList(start
);
4193 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4202 readline_set_completion_index(rs
, len
);
4203 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
4204 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
4208 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4218 readline_set_completion_index(rs
, len
);
4219 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
4222 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
4224 name
= object_class_get_name(OBJECT_CLASS(dc
));
4226 if (!dc
->cannot_instantiate_with_device_add_yet
4227 && !strncmp(name
, str
, len
)) {
4228 readline_add_completion(rs
, name
);
4235 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4245 readline_set_completion_index(rs
, len
);
4246 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
4250 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
4251 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
4252 readline_add_completion(rs
, name
);
4259 static void peripheral_device_del_completion(ReadLineState
*rs
,
4260 const char *str
, size_t len
)
4262 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
4263 GSList
*list
, *item
;
4265 list
= qdev_build_hotpluggable_device_list(peripheral
);
4270 for (item
= list
; item
; item
= g_slist_next(item
)) {
4271 DeviceState
*dev
= item
->data
;
4273 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
4274 readline_add_completion(rs
, dev
->id
);
4281 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4284 ChardevInfoList
*list
, *start
;
4290 readline_set_completion_index(rs
, len
);
4292 start
= list
= qmp_query_chardev(NULL
);
4294 ChardevInfo
*chr
= list
->value
;
4296 if (!strncmp(chr
->label
, str
, len
)) {
4297 readline_add_completion(rs
, chr
->label
);
4301 qapi_free_ChardevInfoList(start
);
4304 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
4307 ChardevInfoList
*list
, *start
;
4310 readline_set_completion_index(rs
, len
);
4312 start
= list
= qmp_query_chardev(NULL
);
4314 ChardevInfo
*chr_info
= list
->value
;
4316 if (!strncmp(chr_info
->label
, str
, len
)) {
4317 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
4318 if (chr
&& chr_is_ringbuf(chr
)) {
4319 readline_add_completion(rs
, chr_info
->label
);
4324 qapi_free_ChardevInfoList(start
);
4327 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4332 ringbuf_completion(rs
, str
);
4335 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4344 readline_set_completion_index(rs
, len
);
4345 peripheral_device_del_completion(rs
, str
, len
);
4348 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4350 ObjectPropertyInfoList
*list
, *start
;
4357 readline_set_completion_index(rs
, len
);
4359 start
= list
= qmp_qom_list("/objects", NULL
);
4361 ObjectPropertyInfo
*info
= list
->value
;
4363 if (!strncmp(info
->type
, "child<", 5)
4364 && !strncmp(info
->name
, str
, len
)) {
4365 readline_add_completion(rs
, info
->name
);
4369 qapi_free_ObjectPropertyInfoList(start
);
4372 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4381 sep
= strrchr(str
, '-');
4386 readline_set_completion_index(rs
, len
);
4387 for (i
= 0; i
< Q_KEY_CODE_MAX
; i
++) {
4388 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
4389 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
4394 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4399 readline_set_completion_index(rs
, len
);
4401 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4403 count
= qemu_find_net_clients_except(NULL
, ncs
,
4404 NET_CLIENT_OPTIONS_KIND_NONE
,
4406 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4407 const char *name
= ncs
[i
]->name
;
4408 if (!strncmp(str
, name
, len
)) {
4409 readline_add_completion(rs
, name
);
4412 } else if (nb_args
== 3) {
4413 add_completion_option(rs
, str
, "on");
4414 add_completion_option(rs
, str
, "off");
4418 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4421 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4428 readline_set_completion_index(rs
, len
);
4429 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
4431 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4433 const char *name
= ncs
[i
]->name
;
4434 if (strncmp(str
, name
, len
)) {
4437 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
4439 readline_add_completion(rs
, name
);
4444 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4451 readline_set_completion_index(rs
, strlen(str
));
4452 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
4453 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
4457 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
4463 readline_set_completion_index(rs
, len
);
4466 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
4467 const char *name
= MigrationCapability_lookup
[i
];
4468 if (!strncmp(str
, name
, len
)) {
4469 readline_add_completion(rs
, name
);
4472 } else if (nb_args
== 3) {
4473 add_completion_option(rs
, str
, "on");
4474 add_completion_option(rs
, str
, "off");
4478 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
4484 readline_set_completion_index(rs
, len
);
4487 for (i
= 0; i
< MIGRATION_PARAMETER_MAX
; i
++) {
4488 const char *name
= MigrationParameter_lookup
[i
];
4489 if (!strncmp(str
, name
, len
)) {
4490 readline_add_completion(rs
, name
);
4496 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4504 readline_set_completion_index(rs
, len
);
4505 for (i
= 0; host_net_devices
[i
]; i
++) {
4506 if (!strncmp(host_net_devices
[i
], str
, len
)) {
4507 readline_add_completion(rs
, host_net_devices
[i
]);
4512 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4514 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4518 readline_set_completion_index(rs
, len
);
4520 count
= qemu_find_net_clients_except(NULL
, ncs
,
4521 NET_CLIENT_OPTIONS_KIND_NONE
,
4523 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4527 if (net_hub_id_for_client(ncs
[i
], &id
)) {
4530 snprintf(name
, sizeof(name
), "%d", id
);
4531 if (!strncmp(str
, name
, len
)) {
4532 readline_add_completion(rs
, name
);
4536 } else if (nb_args
== 3) {
4537 count
= qemu_find_net_clients_except(NULL
, ncs
,
4538 NET_CLIENT_OPTIONS_KIND_NIC
,
4540 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4544 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
4545 net_hub_id_for_client(ncs
[i
], &id
)) {
4548 name
= ncs
[i
]->name
;
4549 if (!strncmp(str
, name
, len
)) {
4550 readline_add_completion(rs
, name
);
4557 static void vm_completion(ReadLineState
*rs
, const char *str
)
4560 BlockDriverState
*bs
= NULL
;
4563 readline_set_completion_index(rs
, len
);
4564 while ((bs
= bdrv_next(bs
))) {
4565 SnapshotInfoList
*snapshots
, *snapshot
;
4567 if (!bdrv_can_snapshot(bs
)) {
4570 if (bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
)) {
4573 snapshot
= snapshots
;
4575 char *completion
= snapshot
->value
->name
;
4576 if (!strncmp(str
, completion
, len
)) {
4577 readline_add_completion(rs
, completion
);
4579 completion
= snapshot
->value
->id
;
4580 if (!strncmp(str
, completion
, len
)) {
4581 readline_add_completion(rs
, completion
);
4583 snapshot
= snapshot
->next
;
4585 qapi_free_SnapshotInfoList(snapshots
);
4590 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4593 vm_completion(rs
, str
);
4597 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4600 vm_completion(rs
, str
);
4604 static void monitor_find_completion_by_table(Monitor
*mon
,
4605 const mon_cmd_t
*cmd_table
,
4609 const char *cmdname
;
4611 const char *ptype
, *str
, *name
;
4612 const mon_cmd_t
*cmd
;
4613 BlockDriverState
*bs
;
4616 /* command completion */
4621 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4622 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4623 cmd_completion(mon
, cmdname
, cmd
->name
);
4626 /* find the command */
4627 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4628 if (compare_cmd(args
[0], cmd
->name
)) {
4636 if (cmd
->sub_table
) {
4637 /* do the job again */
4638 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4639 &args
[1], nb_args
- 1);
4642 if (cmd
->command_completion
) {
4643 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4647 ptype
= next_arg_type(cmd
->args_type
);
4648 for(i
= 0; i
< nb_args
- 2; i
++) {
4649 if (*ptype
!= '\0') {
4650 ptype
= next_arg_type(ptype
);
4651 while (*ptype
== '?')
4652 ptype
= next_arg_type(ptype
);
4655 str
= args
[nb_args
- 1];
4656 while (*ptype
== '-' && ptype
[1] != '\0') {
4657 ptype
= next_arg_type(ptype
);
4661 /* file completion */
4662 readline_set_completion_index(mon
->rs
, strlen(str
));
4663 file_completion(mon
, str
);
4666 /* block device name completion */
4667 readline_set_completion_index(mon
->rs
, strlen(str
));
4668 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
4669 name
= bdrv_get_device_name(bs
);
4670 if (str
[0] == '\0' ||
4671 !strncmp(name
, str
, strlen(str
))) {
4672 readline_add_completion(mon
->rs
, name
);
4678 if (!strcmp(cmd
->name
, "help|?")) {
4679 monitor_find_completion_by_table(mon
, cmd_table
,
4680 &args
[1], nb_args
- 1);
4689 static void monitor_find_completion(void *opaque
,
4690 const char *cmdline
)
4692 Monitor
*mon
= opaque
;
4693 char *args
[MAX_ARGS
];
4696 /* 1. parse the cmdline */
4697 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4700 #ifdef DEBUG_COMPLETION
4703 for (i
= 0; i
< nb_args
; i
++) {
4704 monitor_printf(mon
, "arg%d = '%s'\n", i
, args
[i
]);
4709 /* if the line ends with a space, it means we want to complete the
4711 len
= strlen(cmdline
);
4712 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4713 if (nb_args
>= MAX_ARGS
) {
4716 args
[nb_args
++] = g_strdup("");
4719 /* 2. auto complete according to args */
4720 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
4723 free_cmdline_args(args
, nb_args
);
4726 static int monitor_can_read(void *opaque
)
4728 Monitor
*mon
= opaque
;
4730 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4733 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
)
4735 bool is_cap
= cmd
->mhandler
.cmd_new
== do_qmp_capabilities
;
4736 if (is_cap
&& qmp_cmd_mode(mon
)) {
4737 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND
,
4738 "Capabilities negotiation is already complete, command "
4739 "'%s' ignored", cmd
->name
);
4742 if (!is_cap
&& !qmp_cmd_mode(mon
)) {
4743 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND
,
4744 "Expecting capabilities negotiation with "
4745 "'qmp_capabilities' before command '%s'", cmd
->name
);
4752 * Argument validation rules:
4754 * 1. The argument must exist in cmd_args qdict
4755 * 2. The argument type must be the expected one
4757 * Special case: If the argument doesn't exist in cmd_args and
4758 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4759 * checking is skipped for it.
4761 static int check_client_args_type(const QDict
*client_args
,
4762 const QDict
*cmd_args
, int flags
)
4764 const QDictEntry
*ent
;
4766 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4769 const QObject
*client_arg
= qdict_entry_value(ent
);
4770 const char *client_arg_name
= qdict_entry_key(ent
);
4772 obj
= qdict_get(cmd_args
, client_arg_name
);
4774 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4775 /* handler accepts unknowns */
4778 /* client arg doesn't exist */
4779 qerror_report(QERR_INVALID_PARAMETER
, client_arg_name
);
4783 arg_type
= qobject_to_qstring(obj
);
4784 assert(arg_type
!= NULL
);
4786 /* check if argument's type is correct */
4787 switch (qstring_get_str(arg_type
)[0]) {
4791 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4792 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4801 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4802 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4808 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4809 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4810 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4817 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4818 qerror_report(QERR_INVALID_PARAMETER_TYPE
, client_arg_name
,
4824 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4827 /* Any QObject can be passed. */
4832 * These types are not supported by QMP and thus are not
4833 * handled here. Fall through.
4844 * - Check if the client has passed all mandatory args
4845 * - Set special flags for argument validation
4847 static int check_mandatory_args(const QDict
*cmd_args
,
4848 const QDict
*client_args
, int *flags
)
4850 const QDictEntry
*ent
;
4852 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4853 const char *cmd_arg_name
= qdict_entry_key(ent
);
4854 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4855 assert(type
!= NULL
);
4857 if (qstring_get_str(type
)[0] == 'O') {
4858 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4859 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4860 } else if (qstring_get_str(type
)[0] != '-' &&
4861 qstring_get_str(type
)[1] != '?' &&
4862 !qdict_haskey(client_args
, cmd_arg_name
)) {
4863 qerror_report(QERR_MISSING_PARAMETER
, cmd_arg_name
);
4871 static QDict
*qdict_from_args_type(const char *args_type
)
4875 QString
*key
, *type
, *cur_qs
;
4877 assert(args_type
!= NULL
);
4879 qdict
= qdict_new();
4881 if (args_type
== NULL
|| args_type
[0] == '\0') {
4882 /* no args, empty qdict */
4886 key
= qstring_new();
4887 type
= qstring_new();
4892 switch (args_type
[i
]) {
4895 qdict_put(qdict
, qstring_get_str(key
), type
);
4897 if (args_type
[i
] == '\0') {
4900 type
= qstring_new(); /* qdict has ref */
4901 cur_qs
= key
= qstring_new();
4907 qstring_append_chr(cur_qs
, args_type
[i
]);
4917 * Client argument checking rules:
4919 * 1. Client must provide all mandatory arguments
4920 * 2. Each argument provided by the client must be expected
4921 * 3. Each argument provided by the client must have the type expected
4924 static int qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
)
4929 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4932 err
= check_mandatory_args(cmd_args
, client_args
, &flags
);
4937 err
= check_client_args_type(client_args
, cmd_args
, flags
);
4945 * Input object checking rules
4947 * 1. Input object must be a dict
4948 * 2. The "execute" key must exist
4949 * 3. The "execute" key must be a string
4950 * 4. If the "arguments" key exists, it must be a dict
4951 * 5. If the "id" key exists, it can be anything (ie. json-value)
4952 * 6. Any argument not listed above is considered invalid
4954 static QDict
*qmp_check_input_obj(QObject
*input_obj
)
4956 const QDictEntry
*ent
;
4957 int has_exec_key
= 0;
4960 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4961 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "object");
4965 input_dict
= qobject_to_qdict(input_obj
);
4967 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4968 const char *arg_name
= qdict_entry_key(ent
);
4969 const QObject
*arg_obj
= qdict_entry_value(ent
);
4971 if (!strcmp(arg_name
, "execute")) {
4972 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4973 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "execute",
4978 } else if (!strcmp(arg_name
, "arguments")) {
4979 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4980 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER
, "arguments",
4985 qerror_report(QERR_QMP_EXTRA_MEMBER
, arg_name
);
4990 if (!has_exec_key
) {
4991 qerror_report(QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4998 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
5001 QObject
*obj
, *data
;
5002 QDict
*input
, *args
;
5003 const mon_cmd_t
*cmd
;
5004 const char *cmd_name
;
5005 Monitor
*mon
= cur_mon
;
5007 args
= input
= NULL
;
5010 obj
= json_parser_parse(tokens
, NULL
);
5012 // FIXME: should be triggered in json_parser_parse()
5013 qerror_report(QERR_JSON_PARSING
);
5017 input
= qmp_check_input_obj(obj
);
5019 qobject_decref(obj
);
5023 mon
->mc
->id
= qdict_get(input
, "id");
5024 qobject_incref(mon
->mc
->id
);
5026 cmd_name
= qdict_get_str(input
, "execute");
5027 trace_handle_qmp_command(mon
, cmd_name
);
5028 cmd
= qmp_find_cmd(cmd_name
);
5030 qerror_report(ERROR_CLASS_COMMAND_NOT_FOUND
,
5031 "The command %s has not been found", cmd_name
);
5034 if (invalid_qmp_mode(mon
, cmd
)) {
5038 obj
= qdict_get(input
, "arguments");
5042 args
= qobject_to_qdict(obj
);
5046 err
= qmp_check_client_args(cmd
, args
);
5051 if (cmd
->mhandler
.cmd_new(mon
, args
, &data
)) {
5052 /* Command failed... */
5053 if (!monitor_has_error(mon
)) {
5054 /* ... without setting an error, so make one up */
5055 qerror_report(QERR_UNDEFINED_ERROR
);
5060 monitor_protocol_emitter(mon
, data
);
5061 qobject_decref(data
);
5067 * monitor_control_read(): Read and handle QMP input
5069 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
5071 Monitor
*old_mon
= cur_mon
;
5075 json_message_parser_feed(&cur_mon
->mc
->parser
, (const char *) buf
, size
);
5080 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
5082 Monitor
*old_mon
= cur_mon
;
5088 for (i
= 0; i
< size
; i
++)
5089 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
5091 if (size
== 0 || buf
[size
- 1] != 0)
5092 monitor_printf(cur_mon
, "corrupted command\n");
5094 handle_user_command(cur_mon
, (char *)buf
);
5100 static void monitor_command_cb(void *opaque
, const char *cmdline
,
5101 void *readline_opaque
)
5103 Monitor
*mon
= opaque
;
5105 monitor_suspend(mon
);
5106 handle_user_command(mon
, cmdline
);
5107 monitor_resume(mon
);
5110 int monitor_suspend(Monitor
*mon
)
5118 void monitor_resume(Monitor
*mon
)
5122 if (--mon
->suspend_cnt
== 0)
5123 readline_show_prompt(mon
->rs
);
5126 static QObject
*get_qmp_greeting(void)
5128 QObject
*ver
= NULL
;
5130 qmp_marshal_input_query_version(NULL
, NULL
, &ver
);
5131 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
5135 * monitor_control_event(): Print QMP gretting
5137 static void monitor_control_event(void *opaque
, int event
)
5140 Monitor
*mon
= opaque
;
5143 case CHR_EVENT_OPENED
:
5144 mon
->mc
->command_mode
= 0;
5145 data
= get_qmp_greeting();
5146 monitor_json_emitter(mon
, data
);
5147 qobject_decref(data
);
5150 case CHR_EVENT_CLOSED
:
5151 json_message_parser_destroy(&mon
->mc
->parser
);
5152 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5154 monitor_fdsets_cleanup();
5159 static void monitor_event(void *opaque
, int event
)
5161 Monitor
*mon
= opaque
;
5164 case CHR_EVENT_MUX_IN
:
5165 qemu_mutex_lock(&mon
->out_lock
);
5167 qemu_mutex_unlock(&mon
->out_lock
);
5168 if (mon
->reset_seen
) {
5169 readline_restart(mon
->rs
);
5170 monitor_resume(mon
);
5173 mon
->suspend_cnt
= 0;
5177 case CHR_EVENT_MUX_OUT
:
5178 if (mon
->reset_seen
) {
5179 if (mon
->suspend_cnt
== 0) {
5180 monitor_printf(mon
, "\n");
5183 monitor_suspend(mon
);
5187 qemu_mutex_lock(&mon
->out_lock
);
5189 qemu_mutex_unlock(&mon
->out_lock
);
5192 case CHR_EVENT_OPENED
:
5193 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
5194 "information\n", QEMU_VERSION
);
5195 if (!mon
->mux_out
) {
5196 readline_restart(mon
->rs
);
5197 readline_show_prompt(mon
->rs
);
5199 mon
->reset_seen
= 1;
5203 case CHR_EVENT_CLOSED
:
5205 monitor_fdsets_cleanup();
5211 compare_mon_cmd(const void *a
, const void *b
)
5213 return strcmp(((const mon_cmd_t
*)a
)->name
,
5214 ((const mon_cmd_t
*)b
)->name
);
5217 static void sortcmdlist(void)
5220 int elem_size
= sizeof(mon_cmd_t
);
5222 array_num
= sizeof(mon_cmds
)/elem_size
-1;
5223 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
5225 array_num
= sizeof(info_cmds
)/elem_size
-1;
5226 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
5238 /* These functions just adapt the readline interface in a typesafe way. We
5239 * could cast function pointers but that discards compiler checks.
5241 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
5242 const char *fmt
, ...)
5246 monitor_vprintf(opaque
, fmt
, ap
);
5250 static void monitor_readline_flush(void *opaque
)
5252 monitor_flush(opaque
);
5255 static void __attribute__((constructor
)) monitor_lock_init(void)
5257 qemu_mutex_init(&monitor_lock
);
5260 void monitor_init(CharDriverState
*chr
, int flags
)
5262 static int is_first_init
= 1;
5265 if (is_first_init
) {
5266 monitor_qapi_event_init();
5271 mon
= g_malloc(sizeof(*mon
));
5272 monitor_data_init(mon
);
5276 if (flags
& MONITOR_USE_READLINE
) {
5277 mon
->rs
= readline_init(monitor_readline_printf
,
5278 monitor_readline_flush
,
5280 monitor_find_completion
);
5281 monitor_read_command(mon
, 0);
5284 if (monitor_ctrl_mode(mon
)) {
5285 mon
->mc
= g_malloc0(sizeof(MonitorControl
));
5286 /* Control mode requires special handlers */
5287 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
5288 monitor_control_event
, mon
);
5289 qemu_chr_fe_set_echo(chr
, true);
5291 json_message_parser_init(&mon
->mc
->parser
, handle_qmp_command
);
5293 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5294 monitor_event
, mon
);
5297 qemu_mutex_lock(&monitor_lock
);
5298 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5299 qemu_mutex_unlock(&monitor_lock
);
5301 if (!default_mon
|| (flags
& MONITOR_IS_DEFAULT
))
5305 static void bdrv_password_cb(void *opaque
, const char *password
,
5306 void *readline_opaque
)
5308 Monitor
*mon
= opaque
;
5309 BlockDriverState
*bs
= readline_opaque
;
5311 Error
*local_err
= NULL
;
5313 bdrv_add_key(bs
, password
, &local_err
);
5315 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
5316 error_free(local_err
);
5319 if (mon
->password_completion_cb
)
5320 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5322 monitor_read_command(mon
, 1);
5325 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5326 BlockCompletionFunc
*completion_cb
,
5331 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5332 bdrv_get_encrypted_filename(bs
));
5334 mon
->password_completion_cb
= completion_cb
;
5335 mon
->password_opaque
= opaque
;
5337 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5339 if (err
&& completion_cb
)
5340 completion_cb(opaque
, err
);
5345 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
5346 BlockCompletionFunc
*completion_cb
,
5352 blk
= blk_by_name(device
);
5354 monitor_printf(mon
, "Device not found %s\n", device
);
5358 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
5361 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
5364 if (completion_cb
) {
5365 completion_cb(opaque
, 0);
5370 QemuOptsList qemu_mon_opts
= {
5372 .implied_opt_name
= "chardev",
5373 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
5377 .type
= QEMU_OPT_STRING
,
5380 .type
= QEMU_OPT_STRING
,
5383 .type
= QEMU_OPT_BOOL
,
5386 .type
= QEMU_OPT_BOOL
,
5388 { /* end of list */ }
5393 void qmp_rtc_reset_reinjection(Error
**errp
)
5395 error_set(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");