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/qerror.h"
53 #include "qapi/qmp/qint.h"
54 #include "qapi/qmp/qfloat.h"
55 #include "qapi/qmp/qlist.h"
56 #include "qapi/qmp/qbool.h"
57 #include "qapi/qmp/qstring.h"
58 #include "qapi/qmp/qjson.h"
59 #include "qapi/qmp/json-streamer.h"
60 #include "qapi/qmp/json-parser.h"
61 #include <qom/object_interfaces.h>
62 #include "qemu/osdep.h"
65 #include "trace/control.h"
66 #ifdef CONFIG_TRACE_SIMPLE
67 #include "trace/simple.h"
69 #include "exec/memory.h"
70 #include "exec/cpu_ldst.h"
71 #include "qmp-commands.h"
73 #include "qemu/thread.h"
74 #include "block/qapi.h"
75 #include "qapi/qmp-event.h"
76 #include "qapi-event.h"
77 #include "sysemu/block-backend.h"
79 /* for hmp_info_irq/pic */
80 #if defined(TARGET_SPARC)
81 #include "hw/sparc/sun4m.h"
83 #include "hw/lm32/lm32_pic.h"
89 * 'B' block device name
90 * 's' string (accept optional quote)
91 * 'S' it just appends the rest of the string (accept optional quote)
92 * 'O' option string of the form NAME=VALUE,...
93 * parsed according to QemuOptsList given by its name
94 * Example: 'device:O' uses qemu_device_opts.
95 * Restriction: only lists with empty desc are supported
96 * TODO lift the restriction
98 * 'l' target long (32 or 64 bit)
99 * 'M' Non-negative target long (32 or 64 bit), in user mode the
100 * value is multiplied by 2^20 (think Mebibyte)
101 * 'o' octets (aka bytes)
102 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
103 * K, k suffix, which multiplies the value by 2^60 for suffixes E
104 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
105 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
107 * user mode accepts an optional ms, us, ns suffix,
108 * which divides the value by 1e3, 1e6, 1e9, respectively
109 * '/' optional gdb-like print format (like "/10x")
111 * '?' optional type (for all types, except '/')
112 * '.' other form of optional type (for 'i' and 'l')
114 * user mode accepts "on" or "off"
115 * '-' optional parameter (eg. '-f')
119 typedef struct mon_cmd_t
{
121 const char *args_type
;
125 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
126 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
128 /* @sub_table is a list of 2nd level of commands. If it do not exist,
129 * mhandler should be used. If it exist, sub_table[?].mhandler should be
130 * used, and mhandler of 1st level plays the role of help function.
132 struct mon_cmd_t
*sub_table
;
133 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
136 /* file descriptors passed via SCM_RIGHTS */
137 typedef struct mon_fd_t mon_fd_t
;
141 QLIST_ENTRY(mon_fd_t
) next
;
144 /* file descriptor associated with a file descriptor set */
145 typedef struct MonFdsetFd MonFdsetFd
;
150 QLIST_ENTRY(MonFdsetFd
) next
;
153 /* file descriptor set containing fds passed via SCM_RIGHTS */
154 typedef struct MonFdset MonFdset
;
157 QLIST_HEAD(, MonFdsetFd
) fds
;
158 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
159 QLIST_ENTRY(MonFdset
) next
;
164 JSONMessageParser parser
;
166 * When a client connects, we're in capabilities negotiation mode.
167 * When command qmp_capabilities succeeds, we go into command
170 bool in_command_mode
; /* are we in command mode? */
174 * To prevent flooding clients, events can be throttled. The
175 * throttling is calculated globally, rather than per-Monitor
178 typedef struct MonitorQAPIEventState
{
179 QAPIEvent event
; /* Event being tracked */
180 int64_t rate
; /* Minimum time (in ns) between two events */
181 int64_t last
; /* QEMU_CLOCK_REALTIME value at last emission */
182 QEMUTimer
*timer
; /* Timer for handling delayed events */
183 QObject
*data
; /* Event pending delayed dispatch */
184 } MonitorQAPIEventState
;
187 CharDriverState
*chr
;
197 /* Read under either BQL or out_lock, written with BQL+out_lock. */
203 BlockCompletionFunc
*password_completion_cb
;
204 void *password_opaque
;
205 mon_cmd_t
*cmd_table
;
206 QLIST_HEAD(,mon_fd_t
) fds
;
207 QLIST_ENTRY(Monitor
) entry
;
210 /* QMP checker flags */
211 #define QMP_ACCEPT_UNKNOWNS 1
213 /* Protects mon_list, monitor_event_state. */
214 static QemuMutex monitor_lock
;
216 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
217 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
218 static int mon_refcount
;
220 static mon_cmd_t mon_cmds
[];
221 static mon_cmd_t info_cmds
[];
223 static const mon_cmd_t qmp_cmds
[];
227 static void monitor_command_cb(void *opaque
, const char *cmdline
,
228 void *readline_opaque
);
231 * Is @mon a QMP monitor?
233 static inline bool monitor_is_qmp(const Monitor
*mon
)
235 return (mon
->flags
& MONITOR_USE_CONTROL
);
239 * Is the current monitor, if any, a QMP monitor?
241 bool monitor_cur_is_qmp(void)
243 return cur_mon
&& monitor_is_qmp(cur_mon
);
246 void monitor_read_command(Monitor
*mon
, int show_prompt
)
251 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
253 readline_show_prompt(mon
->rs
);
256 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
260 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
261 /* prompt is printed on return from the command handler */
264 monitor_printf(mon
, "terminal does not support password prompting\n");
269 static void monitor_flush_locked(Monitor
*mon
);
271 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
274 Monitor
*mon
= opaque
;
276 qemu_mutex_lock(&mon
->out_lock
);
278 monitor_flush_locked(mon
);
279 qemu_mutex_unlock(&mon
->out_lock
);
283 /* Called with mon->out_lock held. */
284 static void monitor_flush_locked(Monitor
*mon
)
290 if (mon
->skip_flush
) {
294 buf
= qstring_get_str(mon
->outbuf
);
295 len
= qstring_get_length(mon
->outbuf
);
297 if (len
&& !mon
->mux_out
) {
298 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
299 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
300 /* all flushed or error */
301 QDECREF(mon
->outbuf
);
302 mon
->outbuf
= qstring_new();
307 QString
*tmp
= qstring_from_str(buf
+ rc
);
308 QDECREF(mon
->outbuf
);
311 if (mon
->out_watch
== 0) {
312 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
313 monitor_unblocked
, mon
);
318 void monitor_flush(Monitor
*mon
)
320 qemu_mutex_lock(&mon
->out_lock
);
321 monitor_flush_locked(mon
);
322 qemu_mutex_unlock(&mon
->out_lock
);
325 /* flush at every end of line */
326 static void monitor_puts(Monitor
*mon
, const char *str
)
330 qemu_mutex_lock(&mon
->out_lock
);
336 qstring_append_chr(mon
->outbuf
, '\r');
338 qstring_append_chr(mon
->outbuf
, c
);
340 monitor_flush_locked(mon
);
343 qemu_mutex_unlock(&mon
->out_lock
);
346 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
353 if (monitor_is_qmp(mon
)) {
357 buf
= g_strdup_vprintf(fmt
, ap
);
358 monitor_puts(mon
, buf
);
362 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
366 monitor_vprintf(mon
, fmt
, ap
);
370 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
371 const char *fmt
, ...)
375 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
380 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
384 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
385 qobject_to_json(data
);
386 assert(json
!= NULL
);
388 qstring_append_chr(json
, '\n');
389 monitor_puts(mon
, qstring_get_str(json
));
394 static QDict
*build_qmp_error_dict(Error
*err
)
398 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
399 ErrorClass_lookup
[error_get_class(err
)],
400 error_get_pretty(err
));
402 return qobject_to_qdict(obj
);
405 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
410 trace_monitor_protocol_emitter(mon
);
413 /* success response */
416 qobject_incref(data
);
417 qdict_put_obj(qmp
, "return", data
);
419 /* return an empty QDict by default */
420 qdict_put(qmp
, "return", qdict_new());
424 qmp
= build_qmp_error_dict(err
);
428 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
432 monitor_json_emitter(mon
, QOBJECT(qmp
));
437 static MonitorQAPIEventState monitor_qapi_event_state
[QAPI_EVENT_MAX
];
440 * Emits the event to every monitor instance, @event is only used for trace
441 * Called with monitor_lock held.
443 static void monitor_qapi_event_emit(QAPIEvent event
, QObject
*data
)
447 trace_monitor_protocol_event_emit(event
, data
);
448 QLIST_FOREACH(mon
, &mon_list
, entry
) {
449 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
450 monitor_json_emitter(mon
, data
);
456 * Queue a new event for emission to Monitor instances,
457 * applying any rate limiting if required.
460 monitor_qapi_event_queue(QAPIEvent event
, QDict
*data
, Error
**errp
)
462 MonitorQAPIEventState
*evstate
;
463 assert(event
< QAPI_EVENT_MAX
);
464 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
466 evstate
= &(monitor_qapi_event_state
[event
]);
467 trace_monitor_protocol_event_queue(event
,
473 /* Rate limit of 0 indicates no throttling */
474 qemu_mutex_lock(&monitor_lock
);
475 if (!evstate
->rate
) {
476 monitor_qapi_event_emit(event
, QOBJECT(data
));
479 int64_t delta
= now
- evstate
->last
;
481 delta
< evstate
->rate
) {
482 /* If there's an existing event pending, replace
483 * it with the new event, otherwise schedule a
484 * timer for delayed emission
487 qobject_decref(evstate
->data
);
489 int64_t then
= evstate
->last
+ evstate
->rate
;
490 timer_mod_ns(evstate
->timer
, then
);
492 evstate
->data
= QOBJECT(data
);
493 qobject_incref(evstate
->data
);
495 monitor_qapi_event_emit(event
, QOBJECT(data
));
499 qemu_mutex_unlock(&monitor_lock
);
503 * The callback invoked by QemuTimer when a delayed
504 * event is ready to be emitted
506 static void monitor_qapi_event_handler(void *opaque
)
508 MonitorQAPIEventState
*evstate
= opaque
;
509 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
511 trace_monitor_protocol_event_handler(evstate
->event
,
515 qemu_mutex_lock(&monitor_lock
);
517 monitor_qapi_event_emit(evstate
->event
, evstate
->data
);
518 qobject_decref(evstate
->data
);
519 evstate
->data
= NULL
;
522 qemu_mutex_unlock(&monitor_lock
);
526 * @event: the event ID to be limited
527 * @rate: the rate limit in milliseconds
529 * Sets a rate limit on a particular event, so no
530 * more than 1 event will be emitted within @rate
534 monitor_qapi_event_throttle(QAPIEvent event
, int64_t rate
)
536 MonitorQAPIEventState
*evstate
;
537 assert(event
< QAPI_EVENT_MAX
);
539 evstate
= &(monitor_qapi_event_state
[event
]);
541 trace_monitor_protocol_event_throttle(event
, rate
);
542 evstate
->event
= event
;
543 assert(rate
* SCALE_MS
<= INT64_MAX
);
544 evstate
->rate
= rate
* SCALE_MS
;
546 evstate
->data
= NULL
;
547 evstate
->timer
= timer_new(QEMU_CLOCK_REALTIME
,
549 monitor_qapi_event_handler
,
553 static void monitor_qapi_event_init(void)
555 /* Limit guest-triggerable events to 1 per second */
556 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE
, 1000);
557 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG
, 1000);
558 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE
, 1000);
559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD
, 1000);
560 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE
, 1000);
561 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE
, 1000);
563 qmp_event_set_func_emit(monitor_qapi_event_queue
);
566 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
568 cur_mon
->qmp
.in_command_mode
= true;
571 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
573 static void monitor_data_init(Monitor
*mon
)
575 memset(mon
, 0, sizeof(Monitor
));
576 qemu_mutex_init(&mon
->out_lock
);
577 mon
->outbuf
= qstring_new();
578 /* Use *mon_cmds by default. */
579 mon
->cmd_table
= mon_cmds
;
582 static void monitor_data_destroy(Monitor
*mon
)
584 QDECREF(mon
->outbuf
);
585 qemu_mutex_destroy(&mon
->out_lock
);
588 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
589 int64_t cpu_index
, Error
**errp
)
592 Monitor
*old_mon
, hmp
;
594 monitor_data_init(&hmp
);
595 hmp
.skip_flush
= true;
601 int ret
= monitor_set_cpu(cpu_index
);
604 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
610 handle_hmp_command(&hmp
, command_line
);
613 qemu_mutex_lock(&hmp
.out_lock
);
614 if (qstring_get_length(hmp
.outbuf
) > 0) {
615 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
617 output
= g_strdup("");
619 qemu_mutex_unlock(&hmp
.out_lock
);
622 monitor_data_destroy(&hmp
);
626 static int compare_cmd(const char *name
, const char *list
)
628 const char *p
, *pstart
;
636 p
= pstart
+ strlen(pstart
);
637 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
646 static int get_str(char *buf
, int buf_size
, const char **pp
)
654 while (qemu_isspace(*p
)) {
665 while (*p
!= '\0' && *p
!= '\"') {
681 qemu_printf("unsupported escape code: '\\%c'\n", c
);
684 if ((q
- buf
) < buf_size
- 1) {
688 if ((q
- buf
) < buf_size
- 1) {
695 qemu_printf("unterminated string\n");
700 while (*p
!= '\0' && !qemu_isspace(*p
)) {
701 if ((q
- buf
) < buf_size
- 1) {
714 static void free_cmdline_args(char **args
, int nb_args
)
718 assert(nb_args
<= MAX_ARGS
);
720 for (i
= 0; i
< nb_args
; i
++) {
727 * Parse the command line to get valid args.
728 * @cmdline: command line to be parsed.
729 * @pnb_args: location to store the number of args, must NOT be NULL.
730 * @args: location to store the args, which should be freed by caller, must
733 * Returns 0 on success, negative on failure.
735 * NOTE: this parser is an approximate form of the real command parser. Number
736 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
737 * return with failure.
739 static int parse_cmdline(const char *cmdline
,
740 int *pnb_args
, char **args
)
749 while (qemu_isspace(*p
)) {
755 if (nb_args
>= MAX_ARGS
) {
758 ret
= get_str(buf
, sizeof(buf
), &p
);
762 args
[nb_args
] = g_strdup(buf
);
769 free_cmdline_args(args
, nb_args
);
773 static void help_cmd_dump_one(Monitor
*mon
,
774 const mon_cmd_t
*cmd
,
780 for (i
= 0; i
< prefix_args_nb
; i
++) {
781 monitor_printf(mon
, "%s ", prefix_args
[i
]);
783 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
786 /* @args[@arg_index] is the valid command need to find in @cmds */
787 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
788 char **args
, int nb_args
, int arg_index
)
790 const mon_cmd_t
*cmd
;
792 /* No valid arg need to compare with, dump all in *cmds */
793 if (arg_index
>= nb_args
) {
794 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
795 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
800 /* Find one entry to dump */
801 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
802 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
803 if (cmd
->sub_table
) {
804 /* continue with next arg */
805 help_cmd_dump(mon
, cmd
->sub_table
,
806 args
, nb_args
, arg_index
+ 1);
808 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
815 static void help_cmd(Monitor
*mon
, const char *name
)
817 char *args
[MAX_ARGS
];
820 /* 1. parse user input */
822 /* special case for log, directly dump and return */
823 if (!strcmp(name
, "log")) {
824 const QEMULogItem
*item
;
825 monitor_printf(mon
, "Log items (comma separated):\n");
826 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
827 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
828 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
833 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
838 /* 2. dump the contents according to parsed args */
839 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
841 free_cmdline_args(args
, nb_args
);
844 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
846 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
849 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
851 const char *tp_name
= qdict_get_str(qdict
, "name");
852 bool new_state
= qdict_get_bool(qdict
, "option");
853 Error
*local_err
= NULL
;
855 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
857 error_report_err(local_err
);
861 #ifdef CONFIG_TRACE_SIMPLE
862 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
864 const char *op
= qdict_get_try_str(qdict
, "op");
865 const char *arg
= qdict_get_try_str(qdict
, "arg");
868 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
869 } else if (!strcmp(op
, "on")) {
870 st_set_trace_file_enabled(true);
871 } else if (!strcmp(op
, "off")) {
872 st_set_trace_file_enabled(false);
873 } else if (!strcmp(op
, "flush")) {
874 st_flush_trace_buffer();
875 } else if (!strcmp(op
, "set")) {
877 st_set_trace_file(arg
);
880 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
881 help_cmd(mon
, "trace-file");
886 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
888 help_cmd(mon
, "info");
891 CommandInfoList
*qmp_query_commands(Error
**errp
)
893 CommandInfoList
*info
, *cmd_list
= NULL
;
894 const mon_cmd_t
*cmd
;
896 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
897 info
= g_malloc0(sizeof(*info
));
898 info
->value
= g_malloc0(sizeof(*info
->value
));
899 info
->value
->name
= g_strdup(cmd
->name
);
901 info
->next
= cmd_list
;
908 EventInfoList
*qmp_query_events(Error
**errp
)
910 EventInfoList
*info
, *ev_list
= NULL
;
913 for (e
= 0 ; e
< QAPI_EVENT_MAX
; e
++) {
914 const char *event_name
= QAPIEvent_lookup
[e
];
915 assert(event_name
!= NULL
);
916 info
= g_malloc0(sizeof(*info
));
917 info
->value
= g_malloc0(sizeof(*info
->value
));
918 info
->value
->name
= g_strdup(event_name
);
920 info
->next
= ev_list
;
927 /* set the current CPU defined by the user */
928 int monitor_set_cpu(int cpu_index
)
932 cpu
= qemu_get_cpu(cpu_index
);
936 cur_mon
->mon_cpu
= cpu
;
940 static CPUState
*mon_get_cpu(void)
942 if (!cur_mon
->mon_cpu
) {
945 cpu_synchronize_state(cur_mon
->mon_cpu
);
946 return cur_mon
->mon_cpu
;
949 static CPUArchState
*mon_get_cpu_env(void)
951 return mon_get_cpu()->env_ptr
;
954 int monitor_get_cpu_index(void)
956 return mon_get_cpu()->cpu_index
;
959 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
961 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
964 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
966 dump_exec_info((FILE *)mon
, monitor_fprintf
);
967 dump_drift_info((FILE *)mon
, monitor_fprintf
);
970 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
972 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
975 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
984 str
= readline_get_history(mon
->rs
, i
);
987 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
992 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
994 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
997 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
999 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1000 TraceEventInfoList
*elem
;
1002 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1003 monitor_printf(mon
, "%s : state %u\n",
1005 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1007 qapi_free_TraceEventInfoList(events
);
1010 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1011 bool has_port
, int64_t port
,
1012 bool has_tls_port
, int64_t tls_port
,
1013 bool has_cert_subject
, const char *cert_subject
,
1016 if (strcmp(protocol
, "spice") == 0) {
1017 if (!qemu_using_spice(errp
)) {
1021 if (!has_port
&& !has_tls_port
) {
1022 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1026 if (qemu_spice_migrate_info(hostname
,
1027 has_port
? port
: -1,
1028 has_tls_port
? tls_port
: -1,
1030 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1036 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1039 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1041 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1044 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1047 const char *items
= qdict_get_str(qdict
, "items");
1049 if (!strcmp(items
, "none")) {
1052 mask
= qemu_str_to_log_mask(items
);
1054 help_cmd(mon
, "log");
1061 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1063 const char *option
= qdict_get_try_str(qdict
, "option");
1064 if (!option
|| !strcmp(option
, "on")) {
1066 } else if (!strcmp(option
, "off")) {
1069 monitor_printf(mon
, "unexpected option %s\n", option
);
1073 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1075 const char *device
= qdict_get_try_str(qdict
, "device");
1077 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1078 if (gdbserver_start(device
) < 0) {
1079 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1081 } else if (strcmp(device
, "none") == 0) {
1082 monitor_printf(mon
, "Disabled gdbserver\n");
1084 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1089 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1091 const char *action
= qdict_get_str(qdict
, "action");
1092 if (select_watchdog_action(action
) == -1) {
1093 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1097 static void monitor_printc(Monitor
*mon
, int c
)
1099 monitor_printf(mon
, "'");
1102 monitor_printf(mon
, "\\'");
1105 monitor_printf(mon
, "\\\\");
1108 monitor_printf(mon
, "\\n");
1111 monitor_printf(mon
, "\\r");
1114 if (c
>= 32 && c
<= 126) {
1115 monitor_printf(mon
, "%c", c
);
1117 monitor_printf(mon
, "\\x%02x", c
);
1121 monitor_printf(mon
, "'");
1124 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1125 hwaddr addr
, int is_physical
)
1127 int l
, line_size
, i
, max_digits
, len
;
1131 if (format
== 'i') {
1134 CPUArchState
*env
= mon_get_cpu_env();
1137 } else if (wsize
== 4) {
1140 /* as default we use the current CS size */
1143 #ifdef TARGET_X86_64
1144 if ((env
->efer
& MSR_EFER_LMA
) &&
1145 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1149 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1155 CPUArchState
*env
= mon_get_cpu_env();
1156 flags
= msr_le
<< 16;
1157 flags
|= env
->bfd_mach
;
1159 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1163 len
= wsize
* count
;
1172 max_digits
= (wsize
* 8 + 2) / 3;
1176 max_digits
= (wsize
* 8) / 4;
1180 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1189 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1191 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1196 cpu_physical_memory_read(addr
, buf
, l
);
1198 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1199 monitor_printf(mon
, " Cannot access memory\n");
1208 v
= ldub_p(buf
+ i
);
1211 v
= lduw_p(buf
+ i
);
1214 v
= (uint32_t)ldl_p(buf
+ i
);
1220 monitor_printf(mon
, " ");
1223 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1226 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1229 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1232 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1235 monitor_printc(mon
, v
);
1240 monitor_printf(mon
, "\n");
1246 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1248 int count
= qdict_get_int(qdict
, "count");
1249 int format
= qdict_get_int(qdict
, "format");
1250 int size
= qdict_get_int(qdict
, "size");
1251 target_long addr
= qdict_get_int(qdict
, "addr");
1253 memory_dump(mon
, count
, format
, size
, addr
, 0);
1256 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1258 int count
= qdict_get_int(qdict
, "count");
1259 int format
= qdict_get_int(qdict
, "format");
1260 int size
= qdict_get_int(qdict
, "size");
1261 hwaddr addr
= qdict_get_int(qdict
, "addr");
1263 memory_dump(mon
, count
, format
, size
, addr
, 1);
1266 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1268 int format
= qdict_get_int(qdict
, "format");
1269 hwaddr val
= qdict_get_int(qdict
, "val");
1273 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1276 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1279 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1283 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1286 monitor_printc(mon
, val
);
1289 monitor_printf(mon
, "\n");
1292 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1296 uint32_t start
= qdict_get_int(qdict
, "start");
1297 uint32_t size
= qdict_get_int(qdict
, "size");
1300 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1301 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1302 MEMTXATTRS_UNSPECIFIED
, NULL
);
1303 /* BSD sum algorithm ('sum' Unix command) */
1304 sum
= (sum
>> 1) | (sum
<< 15);
1307 monitor_printf(mon
, "%05d\n", sum
);
1310 static int mouse_button_state
;
1312 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1314 int dx
, dy
, dz
, button
;
1315 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1316 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1317 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1319 dx
= strtol(dx_str
, NULL
, 0);
1320 dy
= strtol(dy_str
, NULL
, 0);
1321 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1322 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1325 dz
= strtol(dz_str
, NULL
, 0);
1327 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1328 qemu_input_queue_btn(NULL
, button
, true);
1329 qemu_input_event_sync();
1330 qemu_input_queue_btn(NULL
, button
, false);
1333 qemu_input_event_sync();
1336 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1338 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
1339 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1340 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1341 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1343 int button_state
= qdict_get_int(qdict
, "button_state");
1345 if (mouse_button_state
== button_state
) {
1348 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1349 qemu_input_event_sync();
1350 mouse_button_state
= button_state
;
1353 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1355 int size
= qdict_get_int(qdict
, "size");
1356 int addr
= qdict_get_int(qdict
, "addr");
1357 int has_index
= qdict_haskey(qdict
, "index");
1362 int index
= qdict_get_int(qdict
, "index");
1363 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1371 val
= cpu_inb(addr
);
1375 val
= cpu_inw(addr
);
1379 val
= cpu_inl(addr
);
1383 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1384 suffix
, addr
, size
* 2, val
);
1387 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1389 int size
= qdict_get_int(qdict
, "size");
1390 int addr
= qdict_get_int(qdict
, "addr");
1391 int val
= qdict_get_int(qdict
, "val");
1393 addr
&= IOPORTS_MASK
;
1398 cpu_outb(addr
, val
);
1401 cpu_outw(addr
, val
);
1404 cpu_outl(addr
, val
);
1409 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1411 Error
*local_err
= NULL
;
1412 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1414 qemu_boot_set(bootdevice
, &local_err
);
1416 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1417 error_free(local_err
);
1419 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1423 #if defined(TARGET_I386)
1424 static void print_pte(Monitor
*mon
, hwaddr addr
,
1428 #ifdef TARGET_X86_64
1429 if (addr
& (1ULL << 47)) {
1433 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1434 " %c%c%c%c%c%c%c%c%c\n",
1437 pte
& PG_NX_MASK
? 'X' : '-',
1438 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1439 pte
& PG_PSE_MASK
? 'P' : '-',
1440 pte
& PG_DIRTY_MASK
? 'D' : '-',
1441 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1442 pte
& PG_PCD_MASK
? 'C' : '-',
1443 pte
& PG_PWT_MASK
? 'T' : '-',
1444 pte
& PG_USER_MASK
? 'U' : '-',
1445 pte
& PG_RW_MASK
? 'W' : '-');
1448 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
1450 unsigned int l1
, l2
;
1451 uint32_t pgd
, pde
, pte
;
1453 pgd
= env
->cr
[3] & ~0xfff;
1454 for(l1
= 0; l1
< 1024; l1
++) {
1455 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1456 pde
= le32_to_cpu(pde
);
1457 if (pde
& PG_PRESENT_MASK
) {
1458 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1460 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1462 for(l2
= 0; l2
< 1024; l2
++) {
1463 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1464 pte
= le32_to_cpu(pte
);
1465 if (pte
& PG_PRESENT_MASK
) {
1466 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1476 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1478 unsigned int l1
, l2
, l3
;
1479 uint64_t pdpe
, pde
, pte
;
1480 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1482 pdp_addr
= env
->cr
[3] & ~0x1f;
1483 for (l1
= 0; l1
< 4; l1
++) {
1484 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1485 pdpe
= le64_to_cpu(pdpe
);
1486 if (pdpe
& PG_PRESENT_MASK
) {
1487 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1488 for (l2
= 0; l2
< 512; l2
++) {
1489 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1490 pde
= le64_to_cpu(pde
);
1491 if (pde
& PG_PRESENT_MASK
) {
1492 if (pde
& PG_PSE_MASK
) {
1493 /* 2M pages with PAE, CR4.PSE is ignored */
1494 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1495 ~((hwaddr
)(1 << 20) - 1));
1497 pt_addr
= pde
& 0x3fffffffff000ULL
;
1498 for (l3
= 0; l3
< 512; l3
++) {
1499 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1500 pte
= le64_to_cpu(pte
);
1501 if (pte
& PG_PRESENT_MASK
) {
1502 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1515 #ifdef TARGET_X86_64
1516 static void tlb_info_64(Monitor
*mon
, CPUArchState
*env
)
1518 uint64_t l1
, l2
, l3
, l4
;
1519 uint64_t pml4e
, pdpe
, pde
, pte
;
1520 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1522 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1523 for (l1
= 0; l1
< 512; l1
++) {
1524 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1525 pml4e
= le64_to_cpu(pml4e
);
1526 if (pml4e
& PG_PRESENT_MASK
) {
1527 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1528 for (l2
= 0; l2
< 512; l2
++) {
1529 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1530 pdpe
= le64_to_cpu(pdpe
);
1531 if (pdpe
& PG_PRESENT_MASK
) {
1532 if (pdpe
& PG_PSE_MASK
) {
1533 /* 1G pages, CR4.PSE is ignored */
1534 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1535 0x3ffffc0000000ULL
);
1537 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1538 for (l3
= 0; l3
< 512; l3
++) {
1539 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1540 pde
= le64_to_cpu(pde
);
1541 if (pde
& PG_PRESENT_MASK
) {
1542 if (pde
& PG_PSE_MASK
) {
1543 /* 2M pages, CR4.PSE is ignored */
1544 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1546 0x3ffffffe00000ULL
);
1548 pt_addr
= pde
& 0x3fffffffff000ULL
;
1549 for (l4
= 0; l4
< 512; l4
++) {
1550 cpu_physical_memory_read(pt_addr
1553 pte
= le64_to_cpu(pte
);
1554 if (pte
& PG_PRESENT_MASK
) {
1555 print_pte(mon
, (l1
<< 39) +
1557 (l3
<< 21) + (l4
<< 12),
1559 0x3fffffffff000ULL
);
1573 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1577 env
= mon_get_cpu_env();
1579 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1580 monitor_printf(mon
, "PG disabled\n");
1583 if (env
->cr
[4] & CR4_PAE_MASK
) {
1584 #ifdef TARGET_X86_64
1585 if (env
->hflags
& HF_LMA_MASK
) {
1586 tlb_info_64(mon
, env
);
1590 tlb_info_pae32(mon
, env
);
1593 tlb_info_32(mon
, env
);
1597 static void mem_print(Monitor
*mon
, hwaddr
*pstart
,
1599 hwaddr end
, int prot
)
1602 prot1
= *plast_prot
;
1603 if (prot
!= prot1
) {
1604 if (*pstart
!= -1) {
1605 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
1606 TARGET_FMT_plx
" %c%c%c\n",
1607 *pstart
, end
, end
- *pstart
,
1608 prot1
& PG_USER_MASK
? 'u' : '-',
1610 prot1
& PG_RW_MASK
? 'w' : '-');
1620 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
1622 unsigned int l1
, l2
;
1623 int prot
, last_prot
;
1624 uint32_t pgd
, pde
, pte
;
1627 pgd
= env
->cr
[3] & ~0xfff;
1630 for(l1
= 0; l1
< 1024; l1
++) {
1631 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1632 pde
= le32_to_cpu(pde
);
1634 if (pde
& PG_PRESENT_MASK
) {
1635 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1636 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1637 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1639 for(l2
= 0; l2
< 1024; l2
++) {
1640 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1641 pte
= le32_to_cpu(pte
);
1642 end
= (l1
<< 22) + (l2
<< 12);
1643 if (pte
& PG_PRESENT_MASK
) {
1645 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1649 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1654 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1657 /* Flush last range */
1658 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1661 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1663 unsigned int l1
, l2
, l3
;
1664 int prot
, last_prot
;
1665 uint64_t pdpe
, pde
, pte
;
1666 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1669 pdp_addr
= env
->cr
[3] & ~0x1f;
1672 for (l1
= 0; l1
< 4; l1
++) {
1673 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1674 pdpe
= le64_to_cpu(pdpe
);
1676 if (pdpe
& PG_PRESENT_MASK
) {
1677 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1678 for (l2
= 0; l2
< 512; l2
++) {
1679 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1680 pde
= le64_to_cpu(pde
);
1681 end
= (l1
<< 30) + (l2
<< 21);
1682 if (pde
& PG_PRESENT_MASK
) {
1683 if (pde
& PG_PSE_MASK
) {
1684 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1686 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1688 pt_addr
= pde
& 0x3fffffffff000ULL
;
1689 for (l3
= 0; l3
< 512; l3
++) {
1690 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1691 pte
= le64_to_cpu(pte
);
1692 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
1693 if (pte
& PG_PRESENT_MASK
) {
1694 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
1699 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1704 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1709 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1712 /* Flush last range */
1713 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1717 #ifdef TARGET_X86_64
1718 static void mem_info_64(Monitor
*mon
, CPUArchState
*env
)
1720 int prot
, last_prot
;
1721 uint64_t l1
, l2
, l3
, l4
;
1722 uint64_t pml4e
, pdpe
, pde
, pte
;
1723 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
1725 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1728 for (l1
= 0; l1
< 512; l1
++) {
1729 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1730 pml4e
= le64_to_cpu(pml4e
);
1732 if (pml4e
& PG_PRESENT_MASK
) {
1733 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1734 for (l2
= 0; l2
< 512; l2
++) {
1735 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1736 pdpe
= le64_to_cpu(pdpe
);
1737 end
= (l1
<< 39) + (l2
<< 30);
1738 if (pdpe
& PG_PRESENT_MASK
) {
1739 if (pdpe
& PG_PSE_MASK
) {
1740 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
1743 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1745 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1746 for (l3
= 0; l3
< 512; l3
++) {
1747 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1748 pde
= le64_to_cpu(pde
);
1749 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
1750 if (pde
& PG_PRESENT_MASK
) {
1751 if (pde
& PG_PSE_MASK
) {
1752 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1754 prot
&= pml4e
& pdpe
;
1755 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1757 pt_addr
= pde
& 0x3fffffffff000ULL
;
1758 for (l4
= 0; l4
< 512; l4
++) {
1759 cpu_physical_memory_read(pt_addr
1762 pte
= le64_to_cpu(pte
);
1763 end
= (l1
<< 39) + (l2
<< 30) +
1764 (l3
<< 21) + (l4
<< 12);
1765 if (pte
& PG_PRESENT_MASK
) {
1766 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
1768 prot
&= pml4e
& pdpe
& pde
;
1772 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1777 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1783 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1788 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1791 /* Flush last range */
1792 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 48, 0);
1796 static void hmp_info_mem(Monitor
*mon
, const QDict
*qdict
)
1800 env
= mon_get_cpu_env();
1802 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1803 monitor_printf(mon
, "PG disabled\n");
1806 if (env
->cr
[4] & CR4_PAE_MASK
) {
1807 #ifdef TARGET_X86_64
1808 if (env
->hflags
& HF_LMA_MASK
) {
1809 mem_info_64(mon
, env
);
1813 mem_info_pae32(mon
, env
);
1816 mem_info_32(mon
, env
);
1821 #if defined(TARGET_SH4)
1823 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1825 monitor_printf(mon
, " tlb%i:\t"
1826 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1827 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1828 "dirty=%hhu writethrough=%hhu\n",
1830 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1831 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1835 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1837 CPUArchState
*env
= mon_get_cpu_env();
1840 monitor_printf (mon
, "ITLB:\n");
1841 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1842 print_tlb (mon
, i
, &env
->itlb
[i
]);
1843 monitor_printf (mon
, "UTLB:\n");
1844 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1845 print_tlb (mon
, i
, &env
->utlb
[i
]);
1850 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1851 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1853 CPUArchState
*env1
= mon_get_cpu_env();
1855 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
1859 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1861 mtree_info((fprintf_function
)monitor_printf
, mon
);
1864 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1870 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1871 query_numa_node_mem(node_mem
);
1872 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1873 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1874 monitor_printf(mon
, "node %d cpus:", i
);
1876 if (cpu
->numa_node
== i
) {
1877 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1880 monitor_printf(mon
, "\n");
1881 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1887 #ifdef CONFIG_PROFILER
1892 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1894 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1895 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1896 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1897 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1902 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1904 monitor_printf(mon
, "Internal profiler not compiled\n");
1908 /* Capture support */
1909 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1911 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1916 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1917 monitor_printf(mon
, "[%d]: ", i
);
1918 s
->ops
.info (s
->opaque
);
1922 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1925 int n
= qdict_get_int(qdict
, "n");
1928 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1930 s
->ops
.destroy (s
->opaque
);
1931 QLIST_REMOVE (s
, entries
);
1938 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1940 const char *path
= qdict_get_str(qdict
, "path");
1941 int has_freq
= qdict_haskey(qdict
, "freq");
1942 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1943 int has_bits
= qdict_haskey(qdict
, "bits");
1944 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1945 int has_channels
= qdict_haskey(qdict
, "nchannels");
1946 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1949 s
= g_malloc0 (sizeof (*s
));
1951 freq
= has_freq
? freq
: 44100;
1952 bits
= has_bits
? bits
: 16;
1953 nchannels
= has_channels
? nchannels
: 2;
1955 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1956 monitor_printf(mon
, "Failed to add wave capture\n");
1960 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1963 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1965 qemu_acl
*acl
= qemu_acl_find(name
);
1968 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1973 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1975 const char *aclname
= qdict_get_str(qdict
, "aclname");
1976 qemu_acl
*acl
= find_acl(mon
, aclname
);
1977 qemu_acl_entry
*entry
;
1981 monitor_printf(mon
, "policy: %s\n",
1982 acl
->defaultDeny
? "deny" : "allow");
1983 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1985 monitor_printf(mon
, "%d: %s %s\n", i
,
1986 entry
->deny
? "deny" : "allow", entry
->match
);
1991 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1993 const char *aclname
= qdict_get_str(qdict
, "aclname");
1994 qemu_acl
*acl
= find_acl(mon
, aclname
);
1997 qemu_acl_reset(acl
);
1998 monitor_printf(mon
, "acl: removed all rules\n");
2002 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2004 const char *aclname
= qdict_get_str(qdict
, "aclname");
2005 const char *policy
= qdict_get_str(qdict
, "policy");
2006 qemu_acl
*acl
= find_acl(mon
, aclname
);
2009 if (strcmp(policy
, "allow") == 0) {
2010 acl
->defaultDeny
= 0;
2011 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2012 } else if (strcmp(policy
, "deny") == 0) {
2013 acl
->defaultDeny
= 1;
2014 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2016 monitor_printf(mon
, "acl: unknown policy '%s', "
2017 "expected 'deny' or 'allow'\n", policy
);
2022 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2024 const char *aclname
= qdict_get_str(qdict
, "aclname");
2025 const char *match
= qdict_get_str(qdict
, "match");
2026 const char *policy
= qdict_get_str(qdict
, "policy");
2027 int has_index
= qdict_haskey(qdict
, "index");
2028 int index
= qdict_get_try_int(qdict
, "index", -1);
2029 qemu_acl
*acl
= find_acl(mon
, aclname
);
2033 if (strcmp(policy
, "allow") == 0) {
2035 } else if (strcmp(policy
, "deny") == 0) {
2038 monitor_printf(mon
, "acl: unknown policy '%s', "
2039 "expected 'deny' or 'allow'\n", policy
);
2043 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2045 ret
= qemu_acl_append(acl
, deny
, match
);
2047 monitor_printf(mon
, "acl: unable to add acl entry\n");
2049 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2053 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2055 const char *aclname
= qdict_get_str(qdict
, "aclname");
2056 const char *match
= qdict_get_str(qdict
, "match");
2057 qemu_acl
*acl
= find_acl(mon
, aclname
);
2061 ret
= qemu_acl_remove(acl
, match
);
2063 monitor_printf(mon
, "acl: no matching acl entry\n");
2065 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2069 #if defined(TARGET_I386)
2070 static void hmp_mce(Monitor
*mon
, const QDict
*qdict
)
2074 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2075 int bank
= qdict_get_int(qdict
, "bank");
2076 uint64_t status
= qdict_get_int(qdict
, "status");
2077 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2078 uint64_t addr
= qdict_get_int(qdict
, "addr");
2079 uint64_t misc
= qdict_get_int(qdict
, "misc");
2080 int flags
= MCE_INJECT_UNCOND_AO
;
2082 if (qdict_get_try_bool(qdict
, "broadcast", false)) {
2083 flags
|= MCE_INJECT_BROADCAST
;
2085 cs
= qemu_get_cpu(cpu_index
);
2088 cpu_x86_inject_mce(mon
, cpu
, bank
, status
, mcg_status
, addr
, misc
,
2094 void qmp_getfd(const char *fdname
, Error
**errp
)
2099 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
2101 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2105 if (qemu_isdigit(fdname
[0])) {
2107 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2108 "a name not starting with a digit");
2112 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2113 if (strcmp(monfd
->name
, fdname
) != 0) {
2122 monfd
= g_malloc0(sizeof(mon_fd_t
));
2123 monfd
->name
= g_strdup(fdname
);
2126 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2129 void qmp_closefd(const char *fdname
, Error
**errp
)
2133 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2134 if (strcmp(monfd
->name
, fdname
) != 0) {
2138 QLIST_REMOVE(monfd
, next
);
2140 g_free(monfd
->name
);
2145 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2148 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
2150 int saved_vm_running
= runstate_is_running();
2151 const char *name
= qdict_get_str(qdict
, "name");
2153 vm_stop(RUN_STATE_RESTORE_VM
);
2155 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2160 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2164 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2167 if (strcmp(monfd
->name
, fdname
) != 0) {
2173 /* caller takes ownership of fd */
2174 QLIST_REMOVE(monfd
, next
);
2175 g_free(monfd
->name
);
2181 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2185 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2187 MonFdsetFd
*mon_fdset_fd
;
2188 MonFdsetFd
*mon_fdset_fd_next
;
2190 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2191 if ((mon_fdset_fd
->removed
||
2192 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2193 runstate_is_running()) {
2194 close(mon_fdset_fd
->fd
);
2195 g_free(mon_fdset_fd
->opaque
);
2196 QLIST_REMOVE(mon_fdset_fd
, next
);
2197 g_free(mon_fdset_fd
);
2201 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2202 QLIST_REMOVE(mon_fdset
, next
);
2207 static void monitor_fdsets_cleanup(void)
2209 MonFdset
*mon_fdset
;
2210 MonFdset
*mon_fdset_next
;
2212 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2213 monitor_fdset_cleanup(mon_fdset
);
2217 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2218 const char *opaque
, Error
**errp
)
2221 Monitor
*mon
= cur_mon
;
2224 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2226 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2230 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2231 has_opaque
, opaque
, errp
);
2243 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2245 MonFdset
*mon_fdset
;
2246 MonFdsetFd
*mon_fdset_fd
;
2249 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2250 if (mon_fdset
->id
!= fdset_id
) {
2253 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2255 if (mon_fdset_fd
->fd
!= fd
) {
2258 mon_fdset_fd
->removed
= true;
2261 mon_fdset_fd
->removed
= true;
2264 if (has_fd
&& !mon_fdset_fd
) {
2267 monitor_fdset_cleanup(mon_fdset
);
2273 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2276 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2278 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2281 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2283 MonFdset
*mon_fdset
;
2284 MonFdsetFd
*mon_fdset_fd
;
2285 FdsetInfoList
*fdset_list
= NULL
;
2287 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2288 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2289 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2291 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2292 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2294 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2295 FdsetFdInfoList
*fdsetfd_info
;
2297 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2298 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2299 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2300 if (mon_fdset_fd
->opaque
) {
2301 fdsetfd_info
->value
->has_opaque
= true;
2302 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2304 fdsetfd_info
->value
->has_opaque
= false;
2307 fdsetfd_info
->next
= fdsetfd_list
;
2308 fdsetfd_list
= fdsetfd_info
;
2311 fdset_info
->value
->fds
= fdsetfd_list
;
2313 fdset_info
->next
= fdset_list
;
2314 fdset_list
= fdset_info
;
2320 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2321 bool has_opaque
, const char *opaque
,
2324 MonFdset
*mon_fdset
= NULL
;
2325 MonFdsetFd
*mon_fdset_fd
;
2329 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2330 /* Break if match found or match impossible due to ordering by ID */
2331 if (fdset_id
<= mon_fdset
->id
) {
2332 if (fdset_id
< mon_fdset
->id
) {
2340 if (mon_fdset
== NULL
) {
2341 int64_t fdset_id_prev
= -1;
2342 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2346 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2347 "a non-negative value");
2350 /* Use specified fdset ID */
2351 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2352 mon_fdset_cur
= mon_fdset
;
2353 if (fdset_id
< mon_fdset_cur
->id
) {
2358 /* Use first available fdset ID */
2359 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2360 mon_fdset_cur
= mon_fdset
;
2361 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2362 fdset_id_prev
= mon_fdset_cur
->id
;
2369 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2371 mon_fdset
->id
= fdset_id
;
2373 mon_fdset
->id
= fdset_id_prev
+ 1;
2376 /* The fdset list is ordered by fdset ID */
2377 if (!mon_fdset_cur
) {
2378 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2379 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2380 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2382 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2386 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2387 mon_fdset_fd
->fd
= fd
;
2388 mon_fdset_fd
->removed
= false;
2390 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2392 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2394 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2395 fdinfo
->fdset_id
= mon_fdset
->id
;
2396 fdinfo
->fd
= mon_fdset_fd
->fd
;
2401 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2404 MonFdset
*mon_fdset
;
2405 MonFdsetFd
*mon_fdset_fd
;
2408 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2409 if (mon_fdset
->id
!= fdset_id
) {
2412 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2413 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2414 if (mon_fd_flags
== -1) {
2418 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2419 return mon_fdset_fd
->fd
;
2431 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2433 MonFdset
*mon_fdset
;
2434 MonFdsetFd
*mon_fdset_fd_dup
;
2436 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2437 if (mon_fdset
->id
!= fdset_id
) {
2440 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2441 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2445 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2446 mon_fdset_fd_dup
->fd
= dup_fd
;
2447 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2453 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2455 MonFdset
*mon_fdset
;
2456 MonFdsetFd
*mon_fdset_fd_dup
;
2458 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2459 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2460 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2462 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2463 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2464 monitor_fdset_cleanup(mon_fdset
);
2468 return mon_fdset
->id
;
2476 int monitor_fdset_dup_fd_find(int dup_fd
)
2478 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2481 void monitor_fdset_dup_fd_remove(int dup_fd
)
2483 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2486 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2489 Error
*local_err
= NULL
;
2491 if (!qemu_isdigit(fdname
[0]) && mon
) {
2492 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2494 fd
= qemu_parse_fd(fdname
);
2496 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2501 error_propagate(errp
, local_err
);
2510 /* Please update hmp-commands.hx when adding or changing commands */
2511 static mon_cmd_t info_cmds
[] = {
2516 .help
= "show the version of QEMU",
2517 .mhandler
.cmd
= hmp_info_version
,
2523 .help
= "show the network state",
2524 .mhandler
.cmd
= hmp_info_network
,
2530 .help
= "show the character devices",
2531 .mhandler
.cmd
= hmp_info_chardev
,
2535 .args_type
= "nodes:-n,verbose:-v,device:B?",
2536 .params
= "[-n] [-v] [device]",
2537 .help
= "show info of one block device or all block devices "
2538 "(-n: show named nodes; -v: show details)",
2539 .mhandler
.cmd
= hmp_info_block
,
2542 .name
= "blockstats",
2545 .help
= "show block device statistics",
2546 .mhandler
.cmd
= hmp_info_blockstats
,
2549 .name
= "block-jobs",
2552 .help
= "show progress of ongoing block device operations",
2553 .mhandler
.cmd
= hmp_info_block_jobs
,
2556 .name
= "registers",
2559 .help
= "show the cpu registers",
2560 .mhandler
.cmd
= hmp_info_registers
,
2566 .help
= "show infos for each CPU",
2567 .mhandler
.cmd
= hmp_info_cpus
,
2573 .help
= "show the command line history",
2574 .mhandler
.cmd
= hmp_info_history
,
2576 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2577 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2582 .help
= "show the interrupts statistics (if available)",
2584 .mhandler
.cmd
= sun4m_hmp_info_irq
,
2585 #elif defined(TARGET_LM32)
2586 .mhandler
.cmd
= lm32_hmp_info_irq
,
2588 .mhandler
.cmd
= hmp_info_irq
,
2595 .help
= "show i8259 (PIC) state",
2597 .mhandler
.cmd
= sun4m_hmp_info_pic
,
2598 #elif defined(TARGET_LM32)
2599 .mhandler
.cmd
= lm32_hmp_info_pic
,
2601 .mhandler
.cmd
= hmp_info_pic
,
2609 .help
= "show PCI info",
2610 .mhandler
.cmd
= hmp_info_pci
,
2612 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2613 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2618 .help
= "show virtual to physical memory mappings",
2619 .mhandler
.cmd
= hmp_info_tlb
,
2622 #if defined(TARGET_I386)
2627 .help
= "show the active virtual memory mappings",
2628 .mhandler
.cmd
= hmp_info_mem
,
2635 .help
= "show memory tree",
2636 .mhandler
.cmd
= hmp_info_mtree
,
2642 .help
= "show dynamic compiler info",
2643 .mhandler
.cmd
= hmp_info_jit
,
2649 .help
= "show dynamic compiler opcode counters",
2650 .mhandler
.cmd
= hmp_info_opcount
,
2656 .help
= "show KVM information",
2657 .mhandler
.cmd
= hmp_info_kvm
,
2663 .help
= "show NUMA information",
2664 .mhandler
.cmd
= hmp_info_numa
,
2670 .help
= "show guest USB devices",
2671 .mhandler
.cmd
= hmp_info_usb
,
2677 .help
= "show host USB devices",
2678 .mhandler
.cmd
= hmp_info_usbhost
,
2684 .help
= "show profiling information",
2685 .mhandler
.cmd
= hmp_info_profile
,
2691 .help
= "show capture information",
2692 .mhandler
.cmd
= hmp_info_capture
,
2695 .name
= "snapshots",
2698 .help
= "show the currently saved VM snapshots",
2699 .mhandler
.cmd
= hmp_info_snapshots
,
2705 .help
= "show the current VM status (running|paused)",
2706 .mhandler
.cmd
= hmp_info_status
,
2712 .help
= "show which guest mouse is receiving events",
2713 .mhandler
.cmd
= hmp_info_mice
,
2719 .help
= "show the vnc server status",
2720 .mhandler
.cmd
= hmp_info_vnc
,
2722 #if defined(CONFIG_SPICE)
2727 .help
= "show the spice server status",
2728 .mhandler
.cmd
= hmp_info_spice
,
2735 .help
= "show the current VM name",
2736 .mhandler
.cmd
= hmp_info_name
,
2742 .help
= "show the current VM UUID",
2743 .mhandler
.cmd
= hmp_info_uuid
,
2749 .help
= "show CPU statistics",
2750 .mhandler
.cmd
= hmp_info_cpustats
,
2752 #if defined(CONFIG_SLIRP)
2757 .help
= "show user network stack connection states",
2758 .mhandler
.cmd
= hmp_info_usernet
,
2765 .help
= "show migration status",
2766 .mhandler
.cmd
= hmp_info_migrate
,
2769 .name
= "migrate_capabilities",
2772 .help
= "show current migration capabilities",
2773 .mhandler
.cmd
= hmp_info_migrate_capabilities
,
2776 .name
= "migrate_parameters",
2779 .help
= "show current migration parameters",
2780 .mhandler
.cmd
= hmp_info_migrate_parameters
,
2783 .name
= "migrate_cache_size",
2786 .help
= "show current migration xbzrle cache size",
2787 .mhandler
.cmd
= hmp_info_migrate_cache_size
,
2793 .help
= "show balloon information",
2794 .mhandler
.cmd
= hmp_info_balloon
,
2800 .help
= "show device tree",
2801 .mhandler
.cmd
= hmp_info_qtree
,
2807 .help
= "show qdev device model list",
2808 .mhandler
.cmd
= hmp_info_qdm
,
2812 .args_type
= "path:s?",
2814 .help
= "show QOM composition tree",
2815 .mhandler
.cmd
= hmp_info_qom_tree
,
2821 .help
= "show roms",
2822 .mhandler
.cmd
= hmp_info_roms
,
2825 .name
= "trace-events",
2828 .help
= "show available trace-events & their state",
2829 .mhandler
.cmd
= hmp_info_trace_events
,
2835 .help
= "show the TPM device",
2836 .mhandler
.cmd
= hmp_info_tpm
,
2842 .help
= "show memory backends",
2843 .mhandler
.cmd
= hmp_info_memdev
,
2846 .name
= "memory-devices",
2849 .help
= "show memory devices",
2850 .mhandler
.cmd
= hmp_info_memory_devices
,
2854 .args_type
= "name:s",
2856 .help
= "Show rocker switch",
2857 .mhandler
.cmd
= hmp_rocker
,
2860 .name
= "rocker-ports",
2861 .args_type
= "name:s",
2863 .help
= "Show rocker ports",
2864 .mhandler
.cmd
= hmp_rocker_ports
,
2867 .name
= "rocker-of-dpa-flows",
2868 .args_type
= "name:s,tbl_id:i?",
2869 .params
= "name [tbl_id]",
2870 .help
= "Show rocker OF-DPA flow tables",
2871 .mhandler
.cmd
= hmp_rocker_of_dpa_flows
,
2874 .name
= "rocker-of-dpa-groups",
2875 .args_type
= "name:s,type:i?",
2876 .params
= "name [type]",
2877 .help
= "Show rocker OF-DPA groups",
2878 .mhandler
.cmd
= hmp_rocker_of_dpa_groups
,
2885 /* mon_cmds and info_cmds would be sorted at runtime */
2886 static mon_cmd_t mon_cmds
[] = {
2887 #include "hmp-commands.h"
2891 static const mon_cmd_t qmp_cmds
[] = {
2892 #include "qmp-commands-old.h"
2896 /*******************************************************************/
2898 static const char *pch
;
2899 static sigjmp_buf expr_env
;
2904 typedef struct MonitorDef
{
2907 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2911 #if defined(TARGET_I386)
2912 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2914 CPUArchState
*env
= mon_get_cpu_env();
2915 return env
->eip
+ env
->segs
[R_CS
].base
;
2919 #if defined(TARGET_PPC)
2920 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2922 CPUArchState
*env
= mon_get_cpu_env();
2927 for (i
= 0; i
< 8; i
++)
2928 u
|= env
->crf
[i
] << (32 - (4 * (i
+ 1)));
2933 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2935 CPUArchState
*env
= mon_get_cpu_env();
2939 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2941 CPUArchState
*env
= mon_get_cpu_env();
2945 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2947 CPUArchState
*env
= mon_get_cpu_env();
2948 return cpu_ppc_load_decr(env
);
2951 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2953 CPUArchState
*env
= mon_get_cpu_env();
2954 return cpu_ppc_load_tbu(env
);
2957 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2959 CPUArchState
*env
= mon_get_cpu_env();
2960 return cpu_ppc_load_tbl(env
);
2964 #if defined(TARGET_SPARC)
2965 #ifndef TARGET_SPARC64
2966 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2968 CPUArchState
*env
= mon_get_cpu_env();
2970 return cpu_get_psr(env
);
2974 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2976 CPUArchState
*env
= mon_get_cpu_env();
2977 return env
->regwptr
[val
];
2981 static const MonitorDef monitor_defs
[] = {
2984 #define SEG(name, seg) \
2985 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2986 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2987 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2989 { "eax", offsetof(CPUX86State
, regs
[0]) },
2990 { "ecx", offsetof(CPUX86State
, regs
[1]) },
2991 { "edx", offsetof(CPUX86State
, regs
[2]) },
2992 { "ebx", offsetof(CPUX86State
, regs
[3]) },
2993 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
2994 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
2995 { "esi", offsetof(CPUX86State
, regs
[6]) },
2996 { "edi", offsetof(CPUX86State
, regs
[7]) },
2997 #ifdef TARGET_X86_64
2998 { "r8", offsetof(CPUX86State
, regs
[8]) },
2999 { "r9", offsetof(CPUX86State
, regs
[9]) },
3000 { "r10", offsetof(CPUX86State
, regs
[10]) },
3001 { "r11", offsetof(CPUX86State
, regs
[11]) },
3002 { "r12", offsetof(CPUX86State
, regs
[12]) },
3003 { "r13", offsetof(CPUX86State
, regs
[13]) },
3004 { "r14", offsetof(CPUX86State
, regs
[14]) },
3005 { "r15", offsetof(CPUX86State
, regs
[15]) },
3007 { "eflags", offsetof(CPUX86State
, eflags
) },
3008 { "eip", offsetof(CPUX86State
, eip
) },
3015 { "pc", 0, monitor_get_pc
, },
3016 #elif defined(TARGET_PPC)
3017 /* General purpose registers */
3018 { "r0", offsetof(CPUPPCState
, gpr
[0]) },
3019 { "r1", offsetof(CPUPPCState
, gpr
[1]) },
3020 { "r2", offsetof(CPUPPCState
, gpr
[2]) },
3021 { "r3", offsetof(CPUPPCState
, gpr
[3]) },
3022 { "r4", offsetof(CPUPPCState
, gpr
[4]) },
3023 { "r5", offsetof(CPUPPCState
, gpr
[5]) },
3024 { "r6", offsetof(CPUPPCState
, gpr
[6]) },
3025 { "r7", offsetof(CPUPPCState
, gpr
[7]) },
3026 { "r8", offsetof(CPUPPCState
, gpr
[8]) },
3027 { "r9", offsetof(CPUPPCState
, gpr
[9]) },
3028 { "r10", offsetof(CPUPPCState
, gpr
[10]) },
3029 { "r11", offsetof(CPUPPCState
, gpr
[11]) },
3030 { "r12", offsetof(CPUPPCState
, gpr
[12]) },
3031 { "r13", offsetof(CPUPPCState
, gpr
[13]) },
3032 { "r14", offsetof(CPUPPCState
, gpr
[14]) },
3033 { "r15", offsetof(CPUPPCState
, gpr
[15]) },
3034 { "r16", offsetof(CPUPPCState
, gpr
[16]) },
3035 { "r17", offsetof(CPUPPCState
, gpr
[17]) },
3036 { "r18", offsetof(CPUPPCState
, gpr
[18]) },
3037 { "r19", offsetof(CPUPPCState
, gpr
[19]) },
3038 { "r20", offsetof(CPUPPCState
, gpr
[20]) },
3039 { "r21", offsetof(CPUPPCState
, gpr
[21]) },
3040 { "r22", offsetof(CPUPPCState
, gpr
[22]) },
3041 { "r23", offsetof(CPUPPCState
, gpr
[23]) },
3042 { "r24", offsetof(CPUPPCState
, gpr
[24]) },
3043 { "r25", offsetof(CPUPPCState
, gpr
[25]) },
3044 { "r26", offsetof(CPUPPCState
, gpr
[26]) },
3045 { "r27", offsetof(CPUPPCState
, gpr
[27]) },
3046 { "r28", offsetof(CPUPPCState
, gpr
[28]) },
3047 { "r29", offsetof(CPUPPCState
, gpr
[29]) },
3048 { "r30", offsetof(CPUPPCState
, gpr
[30]) },
3049 { "r31", offsetof(CPUPPCState
, gpr
[31]) },
3050 /* Floating point registers */
3051 { "f0", offsetof(CPUPPCState
, fpr
[0]) },
3052 { "f1", offsetof(CPUPPCState
, fpr
[1]) },
3053 { "f2", offsetof(CPUPPCState
, fpr
[2]) },
3054 { "f3", offsetof(CPUPPCState
, fpr
[3]) },
3055 { "f4", offsetof(CPUPPCState
, fpr
[4]) },
3056 { "f5", offsetof(CPUPPCState
, fpr
[5]) },
3057 { "f6", offsetof(CPUPPCState
, fpr
[6]) },
3058 { "f7", offsetof(CPUPPCState
, fpr
[7]) },
3059 { "f8", offsetof(CPUPPCState
, fpr
[8]) },
3060 { "f9", offsetof(CPUPPCState
, fpr
[9]) },
3061 { "f10", offsetof(CPUPPCState
, fpr
[10]) },
3062 { "f11", offsetof(CPUPPCState
, fpr
[11]) },
3063 { "f12", offsetof(CPUPPCState
, fpr
[12]) },
3064 { "f13", offsetof(CPUPPCState
, fpr
[13]) },
3065 { "f14", offsetof(CPUPPCState
, fpr
[14]) },
3066 { "f15", offsetof(CPUPPCState
, fpr
[15]) },
3067 { "f16", offsetof(CPUPPCState
, fpr
[16]) },
3068 { "f17", offsetof(CPUPPCState
, fpr
[17]) },
3069 { "f18", offsetof(CPUPPCState
, fpr
[18]) },
3070 { "f19", offsetof(CPUPPCState
, fpr
[19]) },
3071 { "f20", offsetof(CPUPPCState
, fpr
[20]) },
3072 { "f21", offsetof(CPUPPCState
, fpr
[21]) },
3073 { "f22", offsetof(CPUPPCState
, fpr
[22]) },
3074 { "f23", offsetof(CPUPPCState
, fpr
[23]) },
3075 { "f24", offsetof(CPUPPCState
, fpr
[24]) },
3076 { "f25", offsetof(CPUPPCState
, fpr
[25]) },
3077 { "f26", offsetof(CPUPPCState
, fpr
[26]) },
3078 { "f27", offsetof(CPUPPCState
, fpr
[27]) },
3079 { "f28", offsetof(CPUPPCState
, fpr
[28]) },
3080 { "f29", offsetof(CPUPPCState
, fpr
[29]) },
3081 { "f30", offsetof(CPUPPCState
, fpr
[30]) },
3082 { "f31", offsetof(CPUPPCState
, fpr
[31]) },
3083 { "fpscr", offsetof(CPUPPCState
, fpscr
) },
3084 /* Next instruction pointer */
3085 { "nip|pc", offsetof(CPUPPCState
, nip
) },
3086 { "lr", offsetof(CPUPPCState
, lr
) },
3087 { "ctr", offsetof(CPUPPCState
, ctr
) },
3088 { "decr", 0, &monitor_get_decr
, },
3089 { "ccr", 0, &monitor_get_ccr
, },
3090 /* Machine state register */
3091 { "msr", 0, &monitor_get_msr
, },
3092 { "xer", 0, &monitor_get_xer
, },
3093 { "tbu", 0, &monitor_get_tbu
, },
3094 { "tbl", 0, &monitor_get_tbl
, },
3095 /* Segment registers */
3096 { "sdr1", offsetof(CPUPPCState
, spr
[SPR_SDR1
]) },
3097 { "sr0", offsetof(CPUPPCState
, sr
[0]) },
3098 { "sr1", offsetof(CPUPPCState
, sr
[1]) },
3099 { "sr2", offsetof(CPUPPCState
, sr
[2]) },
3100 { "sr3", offsetof(CPUPPCState
, sr
[3]) },
3101 { "sr4", offsetof(CPUPPCState
, sr
[4]) },
3102 { "sr5", offsetof(CPUPPCState
, sr
[5]) },
3103 { "sr6", offsetof(CPUPPCState
, sr
[6]) },
3104 { "sr7", offsetof(CPUPPCState
, sr
[7]) },
3105 { "sr8", offsetof(CPUPPCState
, sr
[8]) },
3106 { "sr9", offsetof(CPUPPCState
, sr
[9]) },
3107 { "sr10", offsetof(CPUPPCState
, sr
[10]) },
3108 { "sr11", offsetof(CPUPPCState
, sr
[11]) },
3109 { "sr12", offsetof(CPUPPCState
, sr
[12]) },
3110 { "sr13", offsetof(CPUPPCState
, sr
[13]) },
3111 { "sr14", offsetof(CPUPPCState
, sr
[14]) },
3112 { "sr15", offsetof(CPUPPCState
, sr
[15]) },
3113 /* Too lazy to put BATs... */
3114 { "pvr", offsetof(CPUPPCState
, spr
[SPR_PVR
]) },
3116 { "srr0", offsetof(CPUPPCState
, spr
[SPR_SRR0
]) },
3117 { "srr1", offsetof(CPUPPCState
, spr
[SPR_SRR1
]) },
3118 { "dar", offsetof(CPUPPCState
, spr
[SPR_DAR
]) },
3119 { "dsisr", offsetof(CPUPPCState
, spr
[SPR_DSISR
]) },
3120 { "cfar", offsetof(CPUPPCState
, spr
[SPR_CFAR
]) },
3121 { "sprg0", offsetof(CPUPPCState
, spr
[SPR_SPRG0
]) },
3122 { "sprg1", offsetof(CPUPPCState
, spr
[SPR_SPRG1
]) },
3123 { "sprg2", offsetof(CPUPPCState
, spr
[SPR_SPRG2
]) },
3124 { "sprg3", offsetof(CPUPPCState
, spr
[SPR_SPRG3
]) },
3125 { "sprg4", offsetof(CPUPPCState
, spr
[SPR_SPRG4
]) },
3126 { "sprg5", offsetof(CPUPPCState
, spr
[SPR_SPRG5
]) },
3127 { "sprg6", offsetof(CPUPPCState
, spr
[SPR_SPRG6
]) },
3128 { "sprg7", offsetof(CPUPPCState
, spr
[SPR_SPRG7
]) },
3129 { "pid", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID
]) },
3130 { "csrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR0
]) },
3131 { "csrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR1
]) },
3132 { "esr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_ESR
]) },
3133 { "dear", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DEAR
]) },
3134 { "mcsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSR
]) },
3135 { "tsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TSR
]) },
3136 { "tcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TCR
]) },
3137 { "vrsave", offsetof(CPUPPCState
, spr
[SPR_VRSAVE
]) },
3138 { "pir", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PIR
]) },
3139 { "mcsrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR0
]) },
3140 { "mcsrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR1
]) },
3141 { "decar", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DECAR
]) },
3142 { "ivpr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVPR
]) },
3143 { "epcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPCR
]) },
3144 { "sprg8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_SPRG8
]) },
3145 { "ivor0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR0
]) },
3146 { "ivor1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR1
]) },
3147 { "ivor2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR2
]) },
3148 { "ivor3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR3
]) },
3149 { "ivor4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR4
]) },
3150 { "ivor5", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR5
]) },
3151 { "ivor6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR6
]) },
3152 { "ivor7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR7
]) },
3153 { "ivor8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR8
]) },
3154 { "ivor9", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR9
]) },
3155 { "ivor10", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR10
]) },
3156 { "ivor11", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR11
]) },
3157 { "ivor12", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR12
]) },
3158 { "ivor13", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR13
]) },
3159 { "ivor14", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR14
]) },
3160 { "ivor15", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR15
]) },
3161 { "ivor32", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR32
]) },
3162 { "ivor33", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR33
]) },
3163 { "ivor34", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR34
]) },
3164 { "ivor35", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR35
]) },
3165 { "ivor36", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR36
]) },
3166 { "ivor37", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR37
]) },
3167 { "mas0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS0
]) },
3168 { "mas1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS1
]) },
3169 { "mas2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS2
]) },
3170 { "mas3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS3
]) },
3171 { "mas4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS4
]) },
3172 { "mas6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS6
]) },
3173 { "mas7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS7
]) },
3174 { "mmucfg", offsetof(CPUPPCState
, spr
[SPR_MMUCFG
]) },
3175 { "tlb0cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB0CFG
]) },
3176 { "tlb1cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB1CFG
]) },
3177 { "epr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPR
]) },
3178 { "eplc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPLC
]) },
3179 { "epsc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPSC
]) },
3180 { "svr", offsetof(CPUPPCState
, spr
[SPR_E500_SVR
]) },
3181 { "mcar", offsetof(CPUPPCState
, spr
[SPR_Exxx_MCAR
]) },
3182 { "pid1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID1
]) },
3183 { "pid2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID2
]) },
3184 { "hid0", offsetof(CPUPPCState
, spr
[SPR_HID0
]) },
3186 #elif defined(TARGET_SPARC)
3187 { "g0", offsetof(CPUSPARCState
, gregs
[0]) },
3188 { "g1", offsetof(CPUSPARCState
, gregs
[1]) },
3189 { "g2", offsetof(CPUSPARCState
, gregs
[2]) },
3190 { "g3", offsetof(CPUSPARCState
, gregs
[3]) },
3191 { "g4", offsetof(CPUSPARCState
, gregs
[4]) },
3192 { "g5", offsetof(CPUSPARCState
, gregs
[5]) },
3193 { "g6", offsetof(CPUSPARCState
, gregs
[6]) },
3194 { "g7", offsetof(CPUSPARCState
, gregs
[7]) },
3195 { "o0", 0, monitor_get_reg
},
3196 { "o1", 1, monitor_get_reg
},
3197 { "o2", 2, monitor_get_reg
},
3198 { "o3", 3, monitor_get_reg
},
3199 { "o4", 4, monitor_get_reg
},
3200 { "o5", 5, monitor_get_reg
},
3201 { "o6", 6, monitor_get_reg
},
3202 { "o7", 7, monitor_get_reg
},
3203 { "l0", 8, monitor_get_reg
},
3204 { "l1", 9, monitor_get_reg
},
3205 { "l2", 10, monitor_get_reg
},
3206 { "l3", 11, monitor_get_reg
},
3207 { "l4", 12, monitor_get_reg
},
3208 { "l5", 13, monitor_get_reg
},
3209 { "l6", 14, monitor_get_reg
},
3210 { "l7", 15, monitor_get_reg
},
3211 { "i0", 16, monitor_get_reg
},
3212 { "i1", 17, monitor_get_reg
},
3213 { "i2", 18, monitor_get_reg
},
3214 { "i3", 19, monitor_get_reg
},
3215 { "i4", 20, monitor_get_reg
},
3216 { "i5", 21, monitor_get_reg
},
3217 { "i6", 22, monitor_get_reg
},
3218 { "i7", 23, monitor_get_reg
},
3219 { "pc", offsetof(CPUSPARCState
, pc
) },
3220 { "npc", offsetof(CPUSPARCState
, npc
) },
3221 { "y", offsetof(CPUSPARCState
, y
) },
3222 #ifndef TARGET_SPARC64
3223 { "psr", 0, &monitor_get_psr
, },
3224 { "wim", offsetof(CPUSPARCState
, wim
) },
3226 { "tbr", offsetof(CPUSPARCState
, tbr
) },
3227 { "fsr", offsetof(CPUSPARCState
, fsr
) },
3228 { "f0", offsetof(CPUSPARCState
, fpr
[0].l
.upper
) },
3229 { "f1", offsetof(CPUSPARCState
, fpr
[0].l
.lower
) },
3230 { "f2", offsetof(CPUSPARCState
, fpr
[1].l
.upper
) },
3231 { "f3", offsetof(CPUSPARCState
, fpr
[1].l
.lower
) },
3232 { "f4", offsetof(CPUSPARCState
, fpr
[2].l
.upper
) },
3233 { "f5", offsetof(CPUSPARCState
, fpr
[2].l
.lower
) },
3234 { "f6", offsetof(CPUSPARCState
, fpr
[3].l
.upper
) },
3235 { "f7", offsetof(CPUSPARCState
, fpr
[3].l
.lower
) },
3236 { "f8", offsetof(CPUSPARCState
, fpr
[4].l
.upper
) },
3237 { "f9", offsetof(CPUSPARCState
, fpr
[4].l
.lower
) },
3238 { "f10", offsetof(CPUSPARCState
, fpr
[5].l
.upper
) },
3239 { "f11", offsetof(CPUSPARCState
, fpr
[5].l
.lower
) },
3240 { "f12", offsetof(CPUSPARCState
, fpr
[6].l
.upper
) },
3241 { "f13", offsetof(CPUSPARCState
, fpr
[6].l
.lower
) },
3242 { "f14", offsetof(CPUSPARCState
, fpr
[7].l
.upper
) },
3243 { "f15", offsetof(CPUSPARCState
, fpr
[7].l
.lower
) },
3244 { "f16", offsetof(CPUSPARCState
, fpr
[8].l
.upper
) },
3245 { "f17", offsetof(CPUSPARCState
, fpr
[8].l
.lower
) },
3246 { "f18", offsetof(CPUSPARCState
, fpr
[9].l
.upper
) },
3247 { "f19", offsetof(CPUSPARCState
, fpr
[9].l
.lower
) },
3248 { "f20", offsetof(CPUSPARCState
, fpr
[10].l
.upper
) },
3249 { "f21", offsetof(CPUSPARCState
, fpr
[10].l
.lower
) },
3250 { "f22", offsetof(CPUSPARCState
, fpr
[11].l
.upper
) },
3251 { "f23", offsetof(CPUSPARCState
, fpr
[11].l
.lower
) },
3252 { "f24", offsetof(CPUSPARCState
, fpr
[12].l
.upper
) },
3253 { "f25", offsetof(CPUSPARCState
, fpr
[12].l
.lower
) },
3254 { "f26", offsetof(CPUSPARCState
, fpr
[13].l
.upper
) },
3255 { "f27", offsetof(CPUSPARCState
, fpr
[13].l
.lower
) },
3256 { "f28", offsetof(CPUSPARCState
, fpr
[14].l
.upper
) },
3257 { "f29", offsetof(CPUSPARCState
, fpr
[14].l
.lower
) },
3258 { "f30", offsetof(CPUSPARCState
, fpr
[15].l
.upper
) },
3259 { "f31", offsetof(CPUSPARCState
, fpr
[15].l
.lower
) },
3260 #ifdef TARGET_SPARC64
3261 { "f32", offsetof(CPUSPARCState
, fpr
[16]) },
3262 { "f34", offsetof(CPUSPARCState
, fpr
[17]) },
3263 { "f36", offsetof(CPUSPARCState
, fpr
[18]) },
3264 { "f38", offsetof(CPUSPARCState
, fpr
[19]) },
3265 { "f40", offsetof(CPUSPARCState
, fpr
[20]) },
3266 { "f42", offsetof(CPUSPARCState
, fpr
[21]) },
3267 { "f44", offsetof(CPUSPARCState
, fpr
[22]) },
3268 { "f46", offsetof(CPUSPARCState
, fpr
[23]) },
3269 { "f48", offsetof(CPUSPARCState
, fpr
[24]) },
3270 { "f50", offsetof(CPUSPARCState
, fpr
[25]) },
3271 { "f52", offsetof(CPUSPARCState
, fpr
[26]) },
3272 { "f54", offsetof(CPUSPARCState
, fpr
[27]) },
3273 { "f56", offsetof(CPUSPARCState
, fpr
[28]) },
3274 { "f58", offsetof(CPUSPARCState
, fpr
[29]) },
3275 { "f60", offsetof(CPUSPARCState
, fpr
[30]) },
3276 { "f62", offsetof(CPUSPARCState
, fpr
[31]) },
3277 { "asi", offsetof(CPUSPARCState
, asi
) },
3278 { "pstate", offsetof(CPUSPARCState
, pstate
) },
3279 { "cansave", offsetof(CPUSPARCState
, cansave
) },
3280 { "canrestore", offsetof(CPUSPARCState
, canrestore
) },
3281 { "otherwin", offsetof(CPUSPARCState
, otherwin
) },
3282 { "wstate", offsetof(CPUSPARCState
, wstate
) },
3283 { "cleanwin", offsetof(CPUSPARCState
, cleanwin
) },
3284 { "fprs", offsetof(CPUSPARCState
, fprs
) },
3290 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3291 expr_error(Monitor
*mon
, const char *fmt
, ...)
3295 monitor_vprintf(mon
, fmt
, ap
);
3296 monitor_printf(mon
, "\n");
3298 siglongjmp(expr_env
, 1);
3301 /* return 0 if OK, -1 if not found */
3302 static int get_monitor_def(target_long
*pval
, const char *name
)
3304 const MonitorDef
*md
;
3307 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
3308 if (compare_cmd(name
, md
->name
)) {
3309 if (md
->get_value
) {
3310 *pval
= md
->get_value(md
, md
->offset
);
3312 CPUArchState
*env
= mon_get_cpu_env();
3313 ptr
= (uint8_t *)env
+ md
->offset
;
3316 *pval
= *(int32_t *)ptr
;
3319 *pval
= *(target_long
*)ptr
;
3332 static void next(void)
3336 while (qemu_isspace(*pch
))
3341 static int64_t expr_sum(Monitor
*mon
);
3343 static int64_t expr_unary(Monitor
*mon
)
3352 n
= expr_unary(mon
);
3356 n
= -expr_unary(mon
);
3360 n
= ~expr_unary(mon
);
3366 expr_error(mon
, "')' expected");
3373 expr_error(mon
, "character constant expected");
3377 expr_error(mon
, "missing terminating \' character");
3387 while ((*pch
>= 'a' && *pch
<= 'z') ||
3388 (*pch
>= 'A' && *pch
<= 'Z') ||
3389 (*pch
>= '0' && *pch
<= '9') ||
3390 *pch
== '_' || *pch
== '.') {
3391 if ((q
- buf
) < sizeof(buf
) - 1)
3395 while (qemu_isspace(*pch
))
3398 ret
= get_monitor_def(®
, buf
);
3400 expr_error(mon
, "unknown register");
3405 expr_error(mon
, "unexpected end of expression");
3410 n
= strtoull(pch
, &p
, 0);
3411 if (errno
== ERANGE
) {
3412 expr_error(mon
, "number too large");
3415 expr_error(mon
, "invalid char '%c' in expression", *p
);
3418 while (qemu_isspace(*pch
))
3426 static int64_t expr_prod(Monitor
*mon
)
3431 val
= expr_unary(mon
);
3434 if (op
!= '*' && op
!= '/' && op
!= '%')
3437 val2
= expr_unary(mon
);
3446 expr_error(mon
, "division by zero");
3457 static int64_t expr_logic(Monitor
*mon
)
3462 val
= expr_prod(mon
);
3465 if (op
!= '&' && op
!= '|' && op
!= '^')
3468 val2
= expr_prod(mon
);
3485 static int64_t expr_sum(Monitor
*mon
)
3490 val
= expr_logic(mon
);
3493 if (op
!= '+' && op
!= '-')
3496 val2
= expr_logic(mon
);
3505 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3508 if (sigsetjmp(expr_env
, 0)) {
3512 while (qemu_isspace(*pch
))
3514 *pval
= expr_sum(mon
);
3519 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3521 const char *p
= *pp
;
3525 d
= strtod(p
, &tailp
);
3527 monitor_printf(mon
, "Number expected\n");
3530 if (d
!= d
|| d
- d
!= 0) {
3531 /* NaN or infinity */
3532 monitor_printf(mon
, "Bad number\n");
3541 * Store the command-name in cmdname, and return a pointer to
3542 * the remaining of the command string.
3544 static const char *get_command_name(const char *cmdline
,
3545 char *cmdname
, size_t nlen
)
3548 const char *p
, *pstart
;
3551 while (qemu_isspace(*p
))
3556 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3561 memcpy(cmdname
, pstart
, len
);
3562 cmdname
[len
] = '\0';
3567 * Read key of 'type' into 'key' and return the current
3570 static char *key_get_info(const char *type
, char **key
)
3578 p
= strchr(type
, ':');
3585 str
= g_malloc(len
+ 1);
3586 memcpy(str
, type
, len
);
3593 static int default_fmt_format
= 'x';
3594 static int default_fmt_size
= 4;
3596 static int is_valid_option(const char *c
, const char *typestr
)
3604 typestr
= strstr(typestr
, option
);
3605 return (typestr
!= NULL
);
3608 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3609 const char *cmdname
)
3611 const mon_cmd_t
*cmd
;
3613 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3614 if (compare_cmd(cmdname
, cmd
->name
)) {
3622 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3624 return search_dispatch_table(qmp_cmds
, cmdname
);
3628 * Parse command name from @cmdp according to command table @table.
3629 * If blank, return NULL.
3630 * Else, if no valid command can be found, report to @mon, and return
3632 * Else, change @cmdp to point right behind the name, and return its
3633 * command table entry.
3634 * Do not assume the return value points into @table! It doesn't when
3635 * the command is found in a sub-command table.
3637 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3642 const mon_cmd_t
*cmd
;
3645 /* extract the command name */
3646 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
3650 cmd
= search_dispatch_table(table
, cmdname
);
3652 monitor_printf(mon
, "unknown command: '%.*s'\n",
3653 (int)(p
- *cmdp
), *cmdp
);
3657 /* filter out following useless space */
3658 while (qemu_isspace(*p
)) {
3663 /* search sub command */
3664 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
3665 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
3672 * Parse arguments for @cmd.
3673 * If it can't be parsed, report to @mon, and return NULL.
3674 * Else, insert command arguments into a QDict, and return it.
3675 * Note: On success, caller has to free the QDict structure.
3678 static QDict
*monitor_parse_arguments(Monitor
*mon
,
3680 const mon_cmd_t
*cmd
)
3682 const char *typestr
;
3685 const char *p
= *endp
;
3687 QDict
*qdict
= qdict_new();
3689 /* parse the parameters */
3690 typestr
= cmd
->args_type
;
3692 typestr
= key_get_info(typestr
, &key
);
3704 while (qemu_isspace(*p
))
3706 if (*typestr
== '?') {
3709 /* no optional string: NULL argument */
3713 ret
= get_str(buf
, sizeof(buf
), &p
);
3717 monitor_printf(mon
, "%s: filename expected\n",
3721 monitor_printf(mon
, "%s: block device name expected\n",
3725 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
3730 qdict_put(qdict
, key
, qstring_from_str(buf
));
3735 QemuOptsList
*opts_list
;
3738 opts_list
= qemu_find_opts(key
);
3739 if (!opts_list
|| opts_list
->desc
->name
) {
3742 while (qemu_isspace(*p
)) {
3747 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3750 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
3754 qemu_opts_to_qdict(opts
, qdict
);
3755 qemu_opts_del(opts
);
3760 int count
, format
, size
;
3762 while (qemu_isspace(*p
))
3768 if (qemu_isdigit(*p
)) {
3770 while (qemu_isdigit(*p
)) {
3771 count
= count
* 10 + (*p
- '0');
3809 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3810 monitor_printf(mon
, "invalid char in format: '%c'\n",
3815 format
= default_fmt_format
;
3816 if (format
!= 'i') {
3817 /* for 'i', not specifying a size gives -1 as size */
3819 size
= default_fmt_size
;
3820 default_fmt_size
= size
;
3822 default_fmt_format
= format
;
3825 format
= default_fmt_format
;
3826 if (format
!= 'i') {
3827 size
= default_fmt_size
;
3832 qdict_put(qdict
, "count", qint_from_int(count
));
3833 qdict_put(qdict
, "format", qint_from_int(format
));
3834 qdict_put(qdict
, "size", qint_from_int(size
));
3843 while (qemu_isspace(*p
))
3845 if (*typestr
== '?' || *typestr
== '.') {
3846 if (*typestr
== '?') {
3854 while (qemu_isspace(*p
))
3863 if (get_expr(mon
, &val
, &p
))
3865 /* Check if 'i' is greater than 32-bit */
3866 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3867 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3868 monitor_printf(mon
, "integer is for 32-bit values\n");
3870 } else if (c
== 'M') {
3872 monitor_printf(mon
, "enter a positive value\n");
3877 qdict_put(qdict
, key
, qint_from_int(val
));
3885 while (qemu_isspace(*p
)) {
3888 if (*typestr
== '?') {
3894 val
= strtosz(p
, &end
);
3896 monitor_printf(mon
, "invalid size\n");
3899 qdict_put(qdict
, key
, qint_from_int(val
));
3907 while (qemu_isspace(*p
))
3909 if (*typestr
== '?') {
3915 if (get_double(mon
, &val
, &p
) < 0) {
3918 if (p
[0] && p
[1] == 's') {
3921 val
/= 1e3
; p
+= 2; break;
3923 val
/= 1e6
; p
+= 2; break;
3925 val
/= 1e9
; p
+= 2; break;
3928 if (*p
&& !qemu_isspace(*p
)) {
3929 monitor_printf(mon
, "Unknown unit suffix\n");
3932 qdict_put(qdict
, key
, qfloat_from_double(val
));
3940 while (qemu_isspace(*p
)) {
3944 while (qemu_isgraph(*p
)) {
3947 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3949 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3952 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3955 qdict_put(qdict
, key
, qbool_from_bool(val
));
3960 const char *tmp
= p
;
3967 while (qemu_isspace(*p
))
3972 if(!is_valid_option(p
, typestr
)) {
3974 monitor_printf(mon
, "%s: unsupported option -%c\n",
3986 qdict_put(qdict
, key
, qbool_from_bool(true));
3993 /* package all remaining string */
3996 while (qemu_isspace(*p
)) {
3999 if (*typestr
== '?') {
4002 /* no remaining string: NULL argument */
4008 monitor_printf(mon
, "%s: string expected\n",
4012 qdict_put(qdict
, key
, qstring_from_str(p
));
4018 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
4024 /* check that all arguments were parsed */
4025 while (qemu_isspace(*p
))
4028 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
4041 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
4044 const mon_cmd_t
*cmd
;
4046 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
4051 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
4053 monitor_printf(mon
, "Try \"help %s\" for more information\n",
4058 cmd
->mhandler
.cmd(mon
, qdict
);
4062 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
4064 const char *p
, *pstart
;
4073 p
= pstart
+ strlen(pstart
);
4075 if (len
> sizeof(cmd
) - 2)
4076 len
= sizeof(cmd
) - 2;
4077 memcpy(cmd
, pstart
, len
);
4079 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
4080 readline_add_completion(mon
->rs
, cmd
);
4088 static void file_completion(Monitor
*mon
, const char *input
)
4093 char file
[1024], file_prefix
[1024];
4097 p
= strrchr(input
, '/');
4100 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
4101 pstrcpy(path
, sizeof(path
), ".");
4103 input_path_len
= p
- input
+ 1;
4104 memcpy(path
, input
, input_path_len
);
4105 if (input_path_len
> sizeof(path
) - 1)
4106 input_path_len
= sizeof(path
) - 1;
4107 path
[input_path_len
] = '\0';
4108 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
4111 ffs
= opendir(path
);
4120 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
4124 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
4125 memcpy(file
, input
, input_path_len
);
4126 if (input_path_len
< sizeof(file
))
4127 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
4129 /* stat the file to find out if it's a directory.
4130 * In that case add a slash to speed up typing long paths
4132 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
4133 pstrcat(file
, sizeof(file
), "/");
4135 readline_add_completion(mon
->rs
, file
);
4141 static const char *next_arg_type(const char *typestr
)
4143 const char *p
= strchr(typestr
, ':');
4144 return (p
!= NULL
? ++p
: typestr
);
4147 static void add_completion_option(ReadLineState
*rs
, const char *str
,
4150 if (!str
|| !option
) {
4153 if (!strncmp(option
, str
, strlen(str
))) {
4154 readline_add_completion(rs
, option
);
4158 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4161 ChardevBackendInfoList
*list
, *start
;
4167 readline_set_completion_index(rs
, len
);
4169 start
= list
= qmp_query_chardev_backends(NULL
);
4171 const char *chr_name
= list
->value
->name
;
4173 if (!strncmp(chr_name
, str
, len
)) {
4174 readline_add_completion(rs
, chr_name
);
4178 qapi_free_ChardevBackendInfoList(start
);
4181 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4190 readline_set_completion_index(rs
, len
);
4191 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
4192 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
4196 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4206 readline_set_completion_index(rs
, len
);
4207 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
4210 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
4212 name
= object_class_get_name(OBJECT_CLASS(dc
));
4214 if (!dc
->cannot_instantiate_with_device_add_yet
4215 && !strncmp(name
, str
, len
)) {
4216 readline_add_completion(rs
, name
);
4223 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4233 readline_set_completion_index(rs
, len
);
4234 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
4238 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
4239 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
4240 readline_add_completion(rs
, name
);
4247 static void peripheral_device_del_completion(ReadLineState
*rs
,
4248 const char *str
, size_t len
)
4250 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
4251 GSList
*list
, *item
;
4253 list
= qdev_build_hotpluggable_device_list(peripheral
);
4258 for (item
= list
; item
; item
= g_slist_next(item
)) {
4259 DeviceState
*dev
= item
->data
;
4261 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
4262 readline_add_completion(rs
, dev
->id
);
4269 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4272 ChardevInfoList
*list
, *start
;
4278 readline_set_completion_index(rs
, len
);
4280 start
= list
= qmp_query_chardev(NULL
);
4282 ChardevInfo
*chr
= list
->value
;
4284 if (!strncmp(chr
->label
, str
, len
)) {
4285 readline_add_completion(rs
, chr
->label
);
4289 qapi_free_ChardevInfoList(start
);
4292 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
4295 ChardevInfoList
*list
, *start
;
4298 readline_set_completion_index(rs
, len
);
4300 start
= list
= qmp_query_chardev(NULL
);
4302 ChardevInfo
*chr_info
= list
->value
;
4304 if (!strncmp(chr_info
->label
, str
, len
)) {
4305 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
4306 if (chr
&& chr_is_ringbuf(chr
)) {
4307 readline_add_completion(rs
, chr_info
->label
);
4312 qapi_free_ChardevInfoList(start
);
4315 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4320 ringbuf_completion(rs
, str
);
4323 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4332 readline_set_completion_index(rs
, len
);
4333 peripheral_device_del_completion(rs
, str
, len
);
4336 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4338 ObjectPropertyInfoList
*list
, *start
;
4345 readline_set_completion_index(rs
, len
);
4347 start
= list
= qmp_qom_list("/objects", NULL
);
4349 ObjectPropertyInfo
*info
= list
->value
;
4351 if (!strncmp(info
->type
, "child<", 5)
4352 && !strncmp(info
->name
, str
, len
)) {
4353 readline_add_completion(rs
, info
->name
);
4357 qapi_free_ObjectPropertyInfoList(start
);
4360 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4369 sep
= strrchr(str
, '-');
4374 readline_set_completion_index(rs
, len
);
4375 for (i
= 0; i
< Q_KEY_CODE_MAX
; i
++) {
4376 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
4377 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
4382 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4387 readline_set_completion_index(rs
, len
);
4389 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4391 count
= qemu_find_net_clients_except(NULL
, ncs
,
4392 NET_CLIENT_OPTIONS_KIND_NONE
,
4394 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4395 const char *name
= ncs
[i
]->name
;
4396 if (!strncmp(str
, name
, len
)) {
4397 readline_add_completion(rs
, name
);
4400 } else if (nb_args
== 3) {
4401 add_completion_option(rs
, str
, "on");
4402 add_completion_option(rs
, str
, "off");
4406 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4409 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4416 readline_set_completion_index(rs
, len
);
4417 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
4419 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4421 const char *name
= ncs
[i
]->name
;
4422 if (strncmp(str
, name
, len
)) {
4425 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
4427 readline_add_completion(rs
, name
);
4432 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4439 readline_set_completion_index(rs
, strlen(str
));
4440 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
4441 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
4445 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
4451 readline_set_completion_index(rs
, len
);
4454 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
4455 const char *name
= MigrationCapability_lookup
[i
];
4456 if (!strncmp(str
, name
, len
)) {
4457 readline_add_completion(rs
, name
);
4460 } else if (nb_args
== 3) {
4461 add_completion_option(rs
, str
, "on");
4462 add_completion_option(rs
, str
, "off");
4466 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
4472 readline_set_completion_index(rs
, len
);
4475 for (i
= 0; i
< MIGRATION_PARAMETER_MAX
; i
++) {
4476 const char *name
= MigrationParameter_lookup
[i
];
4477 if (!strncmp(str
, name
, len
)) {
4478 readline_add_completion(rs
, name
);
4484 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4492 readline_set_completion_index(rs
, len
);
4493 for (i
= 0; host_net_devices
[i
]; i
++) {
4494 if (!strncmp(host_net_devices
[i
], str
, len
)) {
4495 readline_add_completion(rs
, host_net_devices
[i
]);
4500 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4502 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4506 readline_set_completion_index(rs
, len
);
4508 count
= qemu_find_net_clients_except(NULL
, ncs
,
4509 NET_CLIENT_OPTIONS_KIND_NONE
,
4511 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4515 if (net_hub_id_for_client(ncs
[i
], &id
)) {
4518 snprintf(name
, sizeof(name
), "%d", id
);
4519 if (!strncmp(str
, name
, len
)) {
4520 readline_add_completion(rs
, name
);
4524 } else if (nb_args
== 3) {
4525 count
= qemu_find_net_clients_except(NULL
, ncs
,
4526 NET_CLIENT_OPTIONS_KIND_NIC
,
4528 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4532 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
4533 net_hub_id_for_client(ncs
[i
], &id
)) {
4536 name
= ncs
[i
]->name
;
4537 if (!strncmp(str
, name
, len
)) {
4538 readline_add_completion(rs
, name
);
4545 static void vm_completion(ReadLineState
*rs
, const char *str
)
4548 BlockDriverState
*bs
= NULL
;
4551 readline_set_completion_index(rs
, len
);
4552 while ((bs
= bdrv_next(bs
))) {
4553 SnapshotInfoList
*snapshots
, *snapshot
;
4555 if (!bdrv_can_snapshot(bs
)) {
4558 if (bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
)) {
4561 snapshot
= snapshots
;
4563 char *completion
= snapshot
->value
->name
;
4564 if (!strncmp(str
, completion
, len
)) {
4565 readline_add_completion(rs
, completion
);
4567 completion
= snapshot
->value
->id
;
4568 if (!strncmp(str
, completion
, len
)) {
4569 readline_add_completion(rs
, completion
);
4571 snapshot
= snapshot
->next
;
4573 qapi_free_SnapshotInfoList(snapshots
);
4578 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4581 vm_completion(rs
, str
);
4585 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4588 vm_completion(rs
, str
);
4592 static void monitor_find_completion_by_table(Monitor
*mon
,
4593 const mon_cmd_t
*cmd_table
,
4597 const char *cmdname
;
4599 const char *ptype
, *str
, *name
;
4600 const mon_cmd_t
*cmd
;
4601 BlockDriverState
*bs
;
4604 /* command completion */
4609 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4610 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4611 cmd_completion(mon
, cmdname
, cmd
->name
);
4614 /* find the command */
4615 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4616 if (compare_cmd(args
[0], cmd
->name
)) {
4624 if (cmd
->sub_table
) {
4625 /* do the job again */
4626 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4627 &args
[1], nb_args
- 1);
4630 if (cmd
->command_completion
) {
4631 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4635 ptype
= next_arg_type(cmd
->args_type
);
4636 for(i
= 0; i
< nb_args
- 2; i
++) {
4637 if (*ptype
!= '\0') {
4638 ptype
= next_arg_type(ptype
);
4639 while (*ptype
== '?')
4640 ptype
= next_arg_type(ptype
);
4643 str
= args
[nb_args
- 1];
4644 while (*ptype
== '-' && ptype
[1] != '\0') {
4645 ptype
= next_arg_type(ptype
);
4649 /* file completion */
4650 readline_set_completion_index(mon
->rs
, strlen(str
));
4651 file_completion(mon
, str
);
4654 /* block device name completion */
4655 readline_set_completion_index(mon
->rs
, strlen(str
));
4656 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
4657 name
= bdrv_get_device_name(bs
);
4658 if (str
[0] == '\0' ||
4659 !strncmp(name
, str
, strlen(str
))) {
4660 readline_add_completion(mon
->rs
, name
);
4666 if (!strcmp(cmd
->name
, "help|?")) {
4667 monitor_find_completion_by_table(mon
, cmd_table
,
4668 &args
[1], nb_args
- 1);
4677 static void monitor_find_completion(void *opaque
,
4678 const char *cmdline
)
4680 Monitor
*mon
= opaque
;
4681 char *args
[MAX_ARGS
];
4684 /* 1. parse the cmdline */
4685 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4689 /* if the line ends with a space, it means we want to complete the
4691 len
= strlen(cmdline
);
4692 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4693 if (nb_args
>= MAX_ARGS
) {
4696 args
[nb_args
++] = g_strdup("");
4699 /* 2. auto complete according to args */
4700 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
4703 free_cmdline_args(args
, nb_args
);
4706 static int monitor_can_read(void *opaque
)
4708 Monitor
*mon
= opaque
;
4710 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4713 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
4716 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
4718 if (is_cap
&& mon
->qmp
.in_command_mode
) {
4719 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4720 "Capabilities negotiation is already complete, command "
4721 "'%s' ignored", cmd
->name
);
4724 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
4725 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4726 "Expecting capabilities negotiation with "
4727 "'qmp_capabilities' before command '%s'", cmd
->name
);
4734 * Argument validation rules:
4736 * 1. The argument must exist in cmd_args qdict
4737 * 2. The argument type must be the expected one
4739 * Special case: If the argument doesn't exist in cmd_args and
4740 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4741 * checking is skipped for it.
4743 static void check_client_args_type(const QDict
*client_args
,
4744 const QDict
*cmd_args
, int flags
,
4747 const QDictEntry
*ent
;
4749 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4752 const QObject
*client_arg
= qdict_entry_value(ent
);
4753 const char *client_arg_name
= qdict_entry_key(ent
);
4755 obj
= qdict_get(cmd_args
, client_arg_name
);
4757 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4758 /* handler accepts unknowns */
4761 /* client arg doesn't exist */
4762 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
4766 arg_type
= qobject_to_qstring(obj
);
4767 assert(arg_type
!= NULL
);
4769 /* check if argument's type is correct */
4770 switch (qstring_get_str(arg_type
)[0]) {
4774 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4775 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4776 client_arg_name
, "string");
4784 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4785 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4786 client_arg_name
, "int");
4791 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4792 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4793 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4794 client_arg_name
, "number");
4800 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4801 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4802 client_arg_name
, "bool");
4807 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4810 /* Any QObject can be passed. */
4815 * These types are not supported by QMP and thus are not
4816 * handled here. Fall through.
4825 * - Check if the client has passed all mandatory args
4826 * - Set special flags for argument validation
4828 static void check_mandatory_args(const QDict
*cmd_args
,
4829 const QDict
*client_args
, int *flags
,
4832 const QDictEntry
*ent
;
4834 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4835 const char *cmd_arg_name
= qdict_entry_key(ent
);
4836 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4837 assert(type
!= NULL
);
4839 if (qstring_get_str(type
)[0] == 'O') {
4840 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4841 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4842 } else if (qstring_get_str(type
)[0] != '-' &&
4843 qstring_get_str(type
)[1] != '?' &&
4844 !qdict_haskey(client_args
, cmd_arg_name
)) {
4845 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
4851 static QDict
*qdict_from_args_type(const char *args_type
)
4855 QString
*key
, *type
, *cur_qs
;
4857 assert(args_type
!= NULL
);
4859 qdict
= qdict_new();
4861 if (args_type
== NULL
|| args_type
[0] == '\0') {
4862 /* no args, empty qdict */
4866 key
= qstring_new();
4867 type
= qstring_new();
4872 switch (args_type
[i
]) {
4875 qdict_put(qdict
, qstring_get_str(key
), type
);
4877 if (args_type
[i
] == '\0') {
4880 type
= qstring_new(); /* qdict has ref */
4881 cur_qs
= key
= qstring_new();
4887 qstring_append_chr(cur_qs
, args_type
[i
]);
4897 * Client argument checking rules:
4899 * 1. Client must provide all mandatory arguments
4900 * 2. Each argument provided by the client must be expected
4901 * 3. Each argument provided by the client must have the type expected
4904 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
4911 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4914 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
4919 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
4922 error_propagate(errp
, err
);
4927 * Input object checking rules
4929 * 1. Input object must be a dict
4930 * 2. The "execute" key must exist
4931 * 3. The "execute" key must be a string
4932 * 4. If the "arguments" key exists, it must be a dict
4933 * 5. If the "id" key exists, it can be anything (ie. json-value)
4934 * 6. Any argument not listed above is considered invalid
4936 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
4938 const QDictEntry
*ent
;
4939 int has_exec_key
= 0;
4942 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4943 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
4947 input_dict
= qobject_to_qdict(input_obj
);
4949 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4950 const char *arg_name
= qdict_entry_key(ent
);
4951 const QObject
*arg_obj
= qdict_entry_value(ent
);
4953 if (!strcmp(arg_name
, "execute")) {
4954 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4955 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
4956 "execute", "string");
4960 } else if (!strcmp(arg_name
, "arguments")) {
4961 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4962 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
4963 "arguments", "object");
4966 } else if (!strcmp(arg_name
, "id")) {
4967 /* Any string is acceptable as "id", so nothing to check */
4969 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
4974 if (!has_exec_key
) {
4975 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4982 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4984 Error
*local_err
= NULL
;
4985 QObject
*obj
, *data
;
4986 QDict
*input
, *args
;
4987 const mon_cmd_t
*cmd
;
4988 const char *cmd_name
;
4989 Monitor
*mon
= cur_mon
;
4991 args
= input
= NULL
;
4994 obj
= json_parser_parse(tokens
, NULL
);
4996 // FIXME: should be triggered in json_parser_parse()
4997 error_setg(&local_err
, QERR_JSON_PARSING
);
5001 input
= qmp_check_input_obj(obj
, &local_err
);
5003 qobject_decref(obj
);
5007 mon
->qmp
.id
= qdict_get(input
, "id");
5008 qobject_incref(mon
->qmp
.id
);
5010 cmd_name
= qdict_get_str(input
, "execute");
5011 trace_handle_qmp_command(mon
, cmd_name
);
5012 cmd
= qmp_find_cmd(cmd_name
);
5014 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
5015 "The command %s has not been found", cmd_name
);
5018 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
5022 obj
= qdict_get(input
, "arguments");
5026 args
= qobject_to_qdict(obj
);
5030 qmp_check_client_args(cmd
, args
, &local_err
);
5035 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
5038 monitor_protocol_emitter(mon
, data
, local_err
);
5039 qobject_decref(data
);
5044 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
5046 Monitor
*old_mon
= cur_mon
;
5050 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
5055 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
5057 Monitor
*old_mon
= cur_mon
;
5063 for (i
= 0; i
< size
; i
++)
5064 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
5066 if (size
== 0 || buf
[size
- 1] != 0)
5067 monitor_printf(cur_mon
, "corrupted command\n");
5069 handle_hmp_command(cur_mon
, (char *)buf
);
5075 static void monitor_command_cb(void *opaque
, const char *cmdline
,
5076 void *readline_opaque
)
5078 Monitor
*mon
= opaque
;
5080 monitor_suspend(mon
);
5081 handle_hmp_command(mon
, cmdline
);
5082 monitor_resume(mon
);
5085 int monitor_suspend(Monitor
*mon
)
5093 void monitor_resume(Monitor
*mon
)
5097 if (--mon
->suspend_cnt
== 0)
5098 readline_show_prompt(mon
->rs
);
5101 static QObject
*get_qmp_greeting(void)
5103 QObject
*ver
= NULL
;
5105 qmp_marshal_input_query_version(NULL
, &ver
, NULL
);
5106 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
5109 static void monitor_qmp_event(void *opaque
, int event
)
5112 Monitor
*mon
= opaque
;
5115 case CHR_EVENT_OPENED
:
5116 mon
->qmp
.in_command_mode
= false;
5117 data
= get_qmp_greeting();
5118 monitor_json_emitter(mon
, data
);
5119 qobject_decref(data
);
5122 case CHR_EVENT_CLOSED
:
5123 json_message_parser_destroy(&mon
->qmp
.parser
);
5124 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
5126 monitor_fdsets_cleanup();
5131 static void monitor_event(void *opaque
, int event
)
5133 Monitor
*mon
= opaque
;
5136 case CHR_EVENT_MUX_IN
:
5137 qemu_mutex_lock(&mon
->out_lock
);
5139 qemu_mutex_unlock(&mon
->out_lock
);
5140 if (mon
->reset_seen
) {
5141 readline_restart(mon
->rs
);
5142 monitor_resume(mon
);
5145 mon
->suspend_cnt
= 0;
5149 case CHR_EVENT_MUX_OUT
:
5150 if (mon
->reset_seen
) {
5151 if (mon
->suspend_cnt
== 0) {
5152 monitor_printf(mon
, "\n");
5155 monitor_suspend(mon
);
5159 qemu_mutex_lock(&mon
->out_lock
);
5161 qemu_mutex_unlock(&mon
->out_lock
);
5164 case CHR_EVENT_OPENED
:
5165 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
5166 "information\n", QEMU_VERSION
);
5167 if (!mon
->mux_out
) {
5168 readline_restart(mon
->rs
);
5169 readline_show_prompt(mon
->rs
);
5171 mon
->reset_seen
= 1;
5175 case CHR_EVENT_CLOSED
:
5177 monitor_fdsets_cleanup();
5183 compare_mon_cmd(const void *a
, const void *b
)
5185 return strcmp(((const mon_cmd_t
*)a
)->name
,
5186 ((const mon_cmd_t
*)b
)->name
);
5189 static void sortcmdlist(void)
5192 int elem_size
= sizeof(mon_cmd_t
);
5194 array_num
= sizeof(mon_cmds
)/elem_size
-1;
5195 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
5197 array_num
= sizeof(info_cmds
)/elem_size
-1;
5198 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
5210 /* These functions just adapt the readline interface in a typesafe way. We
5211 * could cast function pointers but that discards compiler checks.
5213 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
5214 const char *fmt
, ...)
5218 monitor_vprintf(opaque
, fmt
, ap
);
5222 static void monitor_readline_flush(void *opaque
)
5224 monitor_flush(opaque
);
5227 static void __attribute__((constructor
)) monitor_lock_init(void)
5229 qemu_mutex_init(&monitor_lock
);
5232 void monitor_init(CharDriverState
*chr
, int flags
)
5234 static int is_first_init
= 1;
5237 if (is_first_init
) {
5238 monitor_qapi_event_init();
5243 mon
= g_malloc(sizeof(*mon
));
5244 monitor_data_init(mon
);
5248 if (flags
& MONITOR_USE_READLINE
) {
5249 mon
->rs
= readline_init(monitor_readline_printf
,
5250 monitor_readline_flush
,
5252 monitor_find_completion
);
5253 monitor_read_command(mon
, 0);
5256 if (monitor_is_qmp(mon
)) {
5257 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
5258 monitor_qmp_event
, mon
);
5259 qemu_chr_fe_set_echo(chr
, true);
5260 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
5262 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
5263 monitor_event
, mon
);
5266 qemu_mutex_lock(&monitor_lock
);
5267 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
5268 qemu_mutex_unlock(&monitor_lock
);
5271 static void bdrv_password_cb(void *opaque
, const char *password
,
5272 void *readline_opaque
)
5274 Monitor
*mon
= opaque
;
5275 BlockDriverState
*bs
= readline_opaque
;
5277 Error
*local_err
= NULL
;
5279 bdrv_add_key(bs
, password
, &local_err
);
5281 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
5282 error_free(local_err
);
5285 if (mon
->password_completion_cb
)
5286 mon
->password_completion_cb(mon
->password_opaque
, ret
);
5288 monitor_read_command(mon
, 1);
5291 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
5292 BlockCompletionFunc
*completion_cb
,
5297 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
5298 bdrv_get_encrypted_filename(bs
));
5300 mon
->password_completion_cb
= completion_cb
;
5301 mon
->password_opaque
= opaque
;
5303 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
5305 if (err
&& completion_cb
)
5306 completion_cb(opaque
, err
);
5311 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
5312 BlockCompletionFunc
*completion_cb
,
5318 blk
= blk_by_name(device
);
5320 monitor_printf(mon
, "Device not found %s\n", device
);
5324 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
5327 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
5330 if (completion_cb
) {
5331 completion_cb(opaque
, 0);
5336 QemuOptsList qemu_mon_opts
= {
5338 .implied_opt_name
= "chardev",
5339 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
5343 .type
= QEMU_OPT_STRING
,
5346 .type
= QEMU_OPT_STRING
,
5349 .type
= QEMU_OPT_BOOL
,
5352 .type
= QEMU_OPT_BOOL
,
5354 { /* end of list */ }
5359 void qmp_rtc_reset_reinjection(Error
**errp
)
5361 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");