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"
85 #if defined(TARGET_S390X)
86 #include "hw/s390x/storage-keys.h"
93 * 'B' block device name
94 * 's' string (accept optional quote)
95 * 'S' it just appends the rest of the string (accept optional quote)
96 * 'O' option string of the form NAME=VALUE,...
97 * parsed according to QemuOptsList given by its name
98 * Example: 'device:O' uses qemu_device_opts.
99 * Restriction: only lists with empty desc are supported
100 * TODO lift the restriction
102 * 'l' target long (32 or 64 bit)
103 * 'M' Non-negative target long (32 or 64 bit), in user mode the
104 * value is multiplied by 2^20 (think Mebibyte)
105 * 'o' octets (aka bytes)
106 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
107 * K, k suffix, which multiplies the value by 2^60 for suffixes E
108 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
109 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
111 * user mode accepts an optional ms, us, ns suffix,
112 * which divides the value by 1e3, 1e6, 1e9, respectively
113 * '/' optional gdb-like print format (like "/10x")
115 * '?' optional type (for all types, except '/')
116 * '.' other form of optional type (for 'i' and 'l')
118 * user mode accepts "on" or "off"
119 * '-' optional parameter (eg. '-f')
123 typedef struct mon_cmd_t
{
125 const char *args_type
;
129 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
130 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
132 /* @sub_table is a list of 2nd level of commands. If it do not exist,
133 * mhandler should be used. If it exist, sub_table[?].mhandler should be
134 * used, and mhandler of 1st level plays the role of help function.
136 struct mon_cmd_t
*sub_table
;
137 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
140 /* file descriptors passed via SCM_RIGHTS */
141 typedef struct mon_fd_t mon_fd_t
;
145 QLIST_ENTRY(mon_fd_t
) next
;
148 /* file descriptor associated with a file descriptor set */
149 typedef struct MonFdsetFd MonFdsetFd
;
154 QLIST_ENTRY(MonFdsetFd
) next
;
157 /* file descriptor set containing fds passed via SCM_RIGHTS */
158 typedef struct MonFdset MonFdset
;
161 QLIST_HEAD(, MonFdsetFd
) fds
;
162 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
163 QLIST_ENTRY(MonFdset
) next
;
168 JSONMessageParser parser
;
170 * When a client connects, we're in capabilities negotiation mode.
171 * When command qmp_capabilities succeeds, we go into command
174 bool in_command_mode
; /* are we in command mode? */
178 * To prevent flooding clients, events can be throttled. The
179 * throttling is calculated globally, rather than per-Monitor
182 typedef struct MonitorQAPIEventState
{
183 QAPIEvent event
; /* Event being tracked */
184 int64_t rate
; /* Minimum time (in ns) between two events */
185 int64_t last
; /* QEMU_CLOCK_REALTIME value at last emission */
186 QEMUTimer
*timer
; /* Timer for handling delayed events */
187 QObject
*data
; /* Event pending delayed dispatch */
188 } MonitorQAPIEventState
;
191 CharDriverState
*chr
;
201 /* Read under either BQL or out_lock, written with BQL+out_lock. */
207 BlockCompletionFunc
*password_completion_cb
;
208 void *password_opaque
;
209 mon_cmd_t
*cmd_table
;
210 QLIST_HEAD(,mon_fd_t
) fds
;
211 QLIST_ENTRY(Monitor
) entry
;
214 /* QMP checker flags */
215 #define QMP_ACCEPT_UNKNOWNS 1
217 /* Protects mon_list, monitor_event_state. */
218 static QemuMutex monitor_lock
;
220 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
221 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
222 static int mon_refcount
;
224 static mon_cmd_t mon_cmds
[];
225 static mon_cmd_t info_cmds
[];
227 static const mon_cmd_t qmp_cmds
[];
231 static void monitor_command_cb(void *opaque
, const char *cmdline
,
232 void *readline_opaque
);
235 * Is @mon a QMP monitor?
237 static inline bool monitor_is_qmp(const Monitor
*mon
)
239 return (mon
->flags
& MONITOR_USE_CONTROL
);
243 * Is the current monitor, if any, a QMP monitor?
245 bool monitor_cur_is_qmp(void)
247 return cur_mon
&& monitor_is_qmp(cur_mon
);
250 void monitor_read_command(Monitor
*mon
, int show_prompt
)
255 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
257 readline_show_prompt(mon
->rs
);
260 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
264 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
265 /* prompt is printed on return from the command handler */
268 monitor_printf(mon
, "terminal does not support password prompting\n");
273 static void monitor_flush_locked(Monitor
*mon
);
275 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
278 Monitor
*mon
= opaque
;
280 qemu_mutex_lock(&mon
->out_lock
);
282 monitor_flush_locked(mon
);
283 qemu_mutex_unlock(&mon
->out_lock
);
287 /* Called with mon->out_lock held. */
288 static void monitor_flush_locked(Monitor
*mon
)
294 if (mon
->skip_flush
) {
298 buf
= qstring_get_str(mon
->outbuf
);
299 len
= qstring_get_length(mon
->outbuf
);
301 if (len
&& !mon
->mux_out
) {
302 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
303 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
304 /* all flushed or error */
305 QDECREF(mon
->outbuf
);
306 mon
->outbuf
= qstring_new();
311 QString
*tmp
= qstring_from_str(buf
+ rc
);
312 QDECREF(mon
->outbuf
);
315 if (mon
->out_watch
== 0) {
316 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
317 monitor_unblocked
, mon
);
322 void monitor_flush(Monitor
*mon
)
324 qemu_mutex_lock(&mon
->out_lock
);
325 monitor_flush_locked(mon
);
326 qemu_mutex_unlock(&mon
->out_lock
);
329 /* flush at every end of line */
330 static void monitor_puts(Monitor
*mon
, const char *str
)
334 qemu_mutex_lock(&mon
->out_lock
);
340 qstring_append_chr(mon
->outbuf
, '\r');
342 qstring_append_chr(mon
->outbuf
, c
);
344 monitor_flush_locked(mon
);
347 qemu_mutex_unlock(&mon
->out_lock
);
350 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
357 if (monitor_is_qmp(mon
)) {
361 buf
= g_strdup_vprintf(fmt
, ap
);
362 monitor_puts(mon
, buf
);
366 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
370 monitor_vprintf(mon
, fmt
, ap
);
374 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream
,
375 const char *fmt
, ...)
379 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
384 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
388 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
389 qobject_to_json(data
);
390 assert(json
!= NULL
);
392 qstring_append_chr(json
, '\n');
393 monitor_puts(mon
, qstring_get_str(json
));
398 static QDict
*build_qmp_error_dict(Error
*err
)
402 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
403 ErrorClass_lookup
[error_get_class(err
)],
404 error_get_pretty(err
));
406 return qobject_to_qdict(obj
);
409 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
414 trace_monitor_protocol_emitter(mon
);
417 /* success response */
420 qobject_incref(data
);
421 qdict_put_obj(qmp
, "return", data
);
423 /* return an empty QDict by default */
424 qdict_put(qmp
, "return", qdict_new());
428 qmp
= build_qmp_error_dict(err
);
432 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
436 monitor_json_emitter(mon
, QOBJECT(qmp
));
441 static MonitorQAPIEventState monitor_qapi_event_state
[QAPI_EVENT_MAX
];
444 * Emits the event to every monitor instance, @event is only used for trace
445 * Called with monitor_lock held.
447 static void monitor_qapi_event_emit(QAPIEvent event
, QObject
*data
)
451 trace_monitor_protocol_event_emit(event
, data
);
452 QLIST_FOREACH(mon
, &mon_list
, entry
) {
453 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
454 monitor_json_emitter(mon
, data
);
460 * Queue a new event for emission to Monitor instances,
461 * applying any rate limiting if required.
464 monitor_qapi_event_queue(QAPIEvent event
, QDict
*data
, Error
**errp
)
466 MonitorQAPIEventState
*evstate
;
467 assert(event
< QAPI_EVENT_MAX
);
468 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
470 evstate
= &(monitor_qapi_event_state
[event
]);
471 trace_monitor_protocol_event_queue(event
,
477 /* Rate limit of 0 indicates no throttling */
478 qemu_mutex_lock(&monitor_lock
);
479 if (!evstate
->rate
) {
480 monitor_qapi_event_emit(event
, QOBJECT(data
));
483 int64_t delta
= now
- evstate
->last
;
485 delta
< evstate
->rate
) {
486 /* If there's an existing event pending, replace
487 * it with the new event, otherwise schedule a
488 * timer for delayed emission
491 qobject_decref(evstate
->data
);
493 int64_t then
= evstate
->last
+ evstate
->rate
;
494 timer_mod_ns(evstate
->timer
, then
);
496 evstate
->data
= QOBJECT(data
);
497 qobject_incref(evstate
->data
);
499 monitor_qapi_event_emit(event
, QOBJECT(data
));
503 qemu_mutex_unlock(&monitor_lock
);
507 * The callback invoked by QemuTimer when a delayed
508 * event is ready to be emitted
510 static void monitor_qapi_event_handler(void *opaque
)
512 MonitorQAPIEventState
*evstate
= opaque
;
513 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
515 trace_monitor_protocol_event_handler(evstate
->event
,
519 qemu_mutex_lock(&monitor_lock
);
521 monitor_qapi_event_emit(evstate
->event
, evstate
->data
);
522 qobject_decref(evstate
->data
);
523 evstate
->data
= NULL
;
526 qemu_mutex_unlock(&monitor_lock
);
530 * @event: the event ID to be limited
531 * @rate: the rate limit in milliseconds
533 * Sets a rate limit on a particular event, so no
534 * more than 1 event will be emitted within @rate
538 monitor_qapi_event_throttle(QAPIEvent event
, int64_t rate
)
540 MonitorQAPIEventState
*evstate
;
541 assert(event
< QAPI_EVENT_MAX
);
543 evstate
= &(monitor_qapi_event_state
[event
]);
545 trace_monitor_protocol_event_throttle(event
, rate
);
546 evstate
->event
= event
;
547 assert(rate
* SCALE_MS
<= INT64_MAX
);
548 evstate
->rate
= rate
* SCALE_MS
;
550 evstate
->data
= NULL
;
551 evstate
->timer
= timer_new(QEMU_CLOCK_REALTIME
,
553 monitor_qapi_event_handler
,
557 static void monitor_qapi_event_init(void)
559 /* Limit guest-triggerable events to 1 per second */
560 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE
, 1000);
561 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG
, 1000);
562 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE
, 1000);
563 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD
, 1000);
564 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE
, 1000);
565 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE
, 1000);
567 qmp_event_set_func_emit(monitor_qapi_event_queue
);
570 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
572 cur_mon
->qmp
.in_command_mode
= true;
575 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
577 static void monitor_data_init(Monitor
*mon
)
579 memset(mon
, 0, sizeof(Monitor
));
580 qemu_mutex_init(&mon
->out_lock
);
581 mon
->outbuf
= qstring_new();
582 /* Use *mon_cmds by default. */
583 mon
->cmd_table
= mon_cmds
;
586 static void monitor_data_destroy(Monitor
*mon
)
588 QDECREF(mon
->outbuf
);
589 qemu_mutex_destroy(&mon
->out_lock
);
592 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
593 int64_t cpu_index
, Error
**errp
)
596 Monitor
*old_mon
, hmp
;
598 monitor_data_init(&hmp
);
599 hmp
.skip_flush
= true;
605 int ret
= monitor_set_cpu(cpu_index
);
608 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
614 handle_hmp_command(&hmp
, command_line
);
617 qemu_mutex_lock(&hmp
.out_lock
);
618 if (qstring_get_length(hmp
.outbuf
) > 0) {
619 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
621 output
= g_strdup("");
623 qemu_mutex_unlock(&hmp
.out_lock
);
626 monitor_data_destroy(&hmp
);
630 static int compare_cmd(const char *name
, const char *list
)
632 const char *p
, *pstart
;
640 p
= pstart
+ strlen(pstart
);
641 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
650 static int get_str(char *buf
, int buf_size
, const char **pp
)
658 while (qemu_isspace(*p
)) {
669 while (*p
!= '\0' && *p
!= '\"') {
685 printf("unsupported escape code: '\\%c'\n", c
);
688 if ((q
- buf
) < buf_size
- 1) {
692 if ((q
- buf
) < buf_size
- 1) {
699 printf("unterminated string\n");
704 while (*p
!= '\0' && !qemu_isspace(*p
)) {
705 if ((q
- buf
) < buf_size
- 1) {
718 static void free_cmdline_args(char **args
, int nb_args
)
722 assert(nb_args
<= MAX_ARGS
);
724 for (i
= 0; i
< nb_args
; i
++) {
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
737 * Returns 0 on success, negative on failure.
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
743 static int parse_cmdline(const char *cmdline
,
744 int *pnb_args
, char **args
)
753 while (qemu_isspace(*p
)) {
759 if (nb_args
>= MAX_ARGS
) {
762 ret
= get_str(buf
, sizeof(buf
), &p
);
766 args
[nb_args
] = g_strdup(buf
);
773 free_cmdline_args(args
, nb_args
);
777 static void help_cmd_dump_one(Monitor
*mon
,
778 const mon_cmd_t
*cmd
,
784 for (i
= 0; i
< prefix_args_nb
; i
++) {
785 monitor_printf(mon
, "%s ", prefix_args
[i
]);
787 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
790 /* @args[@arg_index] is the valid command need to find in @cmds */
791 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
792 char **args
, int nb_args
, int arg_index
)
794 const mon_cmd_t
*cmd
;
796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index
>= nb_args
) {
798 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
799 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
804 /* Find one entry to dump */
805 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
806 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
807 if (cmd
->sub_table
) {
808 /* continue with next arg */
809 help_cmd_dump(mon
, cmd
->sub_table
,
810 args
, nb_args
, arg_index
+ 1);
812 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
819 static void help_cmd(Monitor
*mon
, const char *name
)
821 char *args
[MAX_ARGS
];
824 /* 1. parse user input */
826 /* special case for log, directly dump and return */
827 if (!strcmp(name
, "log")) {
828 const QEMULogItem
*item
;
829 monitor_printf(mon
, "Log items (comma separated):\n");
830 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
831 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
832 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
837 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
845 free_cmdline_args(args
, nb_args
);
848 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
850 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
853 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
855 const char *tp_name
= qdict_get_str(qdict
, "name");
856 bool new_state
= qdict_get_bool(qdict
, "option");
857 Error
*local_err
= NULL
;
859 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
861 error_report_err(local_err
);
865 #ifdef CONFIG_TRACE_SIMPLE
866 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
868 const char *op
= qdict_get_try_str(qdict
, "op");
869 const char *arg
= qdict_get_try_str(qdict
, "arg");
872 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
873 } else if (!strcmp(op
, "on")) {
874 st_set_trace_file_enabled(true);
875 } else if (!strcmp(op
, "off")) {
876 st_set_trace_file_enabled(false);
877 } else if (!strcmp(op
, "flush")) {
878 st_flush_trace_buffer();
879 } else if (!strcmp(op
, "set")) {
881 st_set_trace_file(arg
);
884 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
885 help_cmd(mon
, "trace-file");
890 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
892 help_cmd(mon
, "info");
895 CommandInfoList
*qmp_query_commands(Error
**errp
)
897 CommandInfoList
*info
, *cmd_list
= NULL
;
898 const mon_cmd_t
*cmd
;
900 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
901 info
= g_malloc0(sizeof(*info
));
902 info
->value
= g_malloc0(sizeof(*info
->value
));
903 info
->value
->name
= g_strdup(cmd
->name
);
905 info
->next
= cmd_list
;
912 EventInfoList
*qmp_query_events(Error
**errp
)
914 EventInfoList
*info
, *ev_list
= NULL
;
917 for (e
= 0 ; e
< QAPI_EVENT_MAX
; e
++) {
918 const char *event_name
= QAPIEvent_lookup
[e
];
919 assert(event_name
!= NULL
);
920 info
= g_malloc0(sizeof(*info
));
921 info
->value
= g_malloc0(sizeof(*info
->value
));
922 info
->value
->name
= g_strdup(event_name
);
924 info
->next
= ev_list
;
931 /* set the current CPU defined by the user */
932 int monitor_set_cpu(int cpu_index
)
936 cpu
= qemu_get_cpu(cpu_index
);
940 cur_mon
->mon_cpu
= cpu
;
944 static CPUState
*mon_get_cpu(void)
946 if (!cur_mon
->mon_cpu
) {
949 cpu_synchronize_state(cur_mon
->mon_cpu
);
950 return cur_mon
->mon_cpu
;
953 static CPUArchState
*mon_get_cpu_env(void)
955 return mon_get_cpu()->env_ptr
;
958 int monitor_get_cpu_index(void)
960 return mon_get_cpu()->cpu_index
;
963 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
965 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
968 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
970 dump_exec_info((FILE *)mon
, monitor_fprintf
);
971 dump_drift_info((FILE *)mon
, monitor_fprintf
);
974 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
976 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
979 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
988 str
= readline_get_history(mon
->rs
, i
);
991 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
996 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
998 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1001 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1003 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1004 TraceEventInfoList
*elem
;
1006 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1007 monitor_printf(mon
, "%s : state %u\n",
1009 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1011 qapi_free_TraceEventInfoList(events
);
1014 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1015 bool has_port
, int64_t port
,
1016 bool has_tls_port
, int64_t tls_port
,
1017 bool has_cert_subject
, const char *cert_subject
,
1020 if (strcmp(protocol
, "spice") == 0) {
1021 if (!qemu_using_spice(errp
)) {
1025 if (!has_port
&& !has_tls_port
) {
1026 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1030 if (qemu_spice_migrate_info(hostname
,
1031 has_port
? port
: -1,
1032 has_tls_port
? tls_port
: -1,
1034 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1040 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1043 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1045 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1048 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1051 const char *items
= qdict_get_str(qdict
, "items");
1053 if (!strcmp(items
, "none")) {
1056 mask
= qemu_str_to_log_mask(items
);
1058 help_cmd(mon
, "log");
1065 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1067 const char *option
= qdict_get_try_str(qdict
, "option");
1068 if (!option
|| !strcmp(option
, "on")) {
1070 } else if (!strcmp(option
, "off")) {
1073 monitor_printf(mon
, "unexpected option %s\n", option
);
1077 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1079 const char *device
= qdict_get_try_str(qdict
, "device");
1081 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1082 if (gdbserver_start(device
) < 0) {
1083 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1085 } else if (strcmp(device
, "none") == 0) {
1086 monitor_printf(mon
, "Disabled gdbserver\n");
1088 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1093 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1095 const char *action
= qdict_get_str(qdict
, "action");
1096 if (select_watchdog_action(action
) == -1) {
1097 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1101 static void monitor_printc(Monitor
*mon
, int c
)
1103 monitor_printf(mon
, "'");
1106 monitor_printf(mon
, "\\'");
1109 monitor_printf(mon
, "\\\\");
1112 monitor_printf(mon
, "\\n");
1115 monitor_printf(mon
, "\\r");
1118 if (c
>= 32 && c
<= 126) {
1119 monitor_printf(mon
, "%c", c
);
1121 monitor_printf(mon
, "\\x%02x", c
);
1125 monitor_printf(mon
, "'");
1128 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1129 hwaddr addr
, int is_physical
)
1131 int l
, line_size
, i
, max_digits
, len
;
1135 if (format
== 'i') {
1138 CPUArchState
*env
= mon_get_cpu_env();
1141 } else if (wsize
== 4) {
1144 /* as default we use the current CS size */
1147 #ifdef TARGET_X86_64
1148 if ((env
->efer
& MSR_EFER_LMA
) &&
1149 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1153 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1159 CPUArchState
*env
= mon_get_cpu_env();
1160 flags
= msr_le
<< 16;
1161 flags
|= env
->bfd_mach
;
1163 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1167 len
= wsize
* count
;
1176 max_digits
= (wsize
* 8 + 2) / 3;
1180 max_digits
= (wsize
* 8) / 4;
1184 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1193 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1195 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1200 cpu_physical_memory_read(addr
, buf
, l
);
1202 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1203 monitor_printf(mon
, " Cannot access memory\n");
1212 v
= ldub_p(buf
+ i
);
1215 v
= lduw_p(buf
+ i
);
1218 v
= (uint32_t)ldl_p(buf
+ i
);
1224 monitor_printf(mon
, " ");
1227 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1230 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1233 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1236 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1239 monitor_printc(mon
, v
);
1244 monitor_printf(mon
, "\n");
1250 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1252 int count
= qdict_get_int(qdict
, "count");
1253 int format
= qdict_get_int(qdict
, "format");
1254 int size
= qdict_get_int(qdict
, "size");
1255 target_long addr
= qdict_get_int(qdict
, "addr");
1257 memory_dump(mon
, count
, format
, size
, addr
, 0);
1260 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1262 int count
= qdict_get_int(qdict
, "count");
1263 int format
= qdict_get_int(qdict
, "format");
1264 int size
= qdict_get_int(qdict
, "size");
1265 hwaddr addr
= qdict_get_int(qdict
, "addr");
1267 memory_dump(mon
, count
, format
, size
, addr
, 1);
1270 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1272 int format
= qdict_get_int(qdict
, "format");
1273 hwaddr val
= qdict_get_int(qdict
, "val");
1277 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1280 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1283 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1287 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1290 monitor_printc(mon
, val
);
1293 monitor_printf(mon
, "\n");
1296 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1300 uint32_t start
= qdict_get_int(qdict
, "start");
1301 uint32_t size
= qdict_get_int(qdict
, "size");
1304 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1305 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1306 MEMTXATTRS_UNSPECIFIED
, NULL
);
1307 /* BSD sum algorithm ('sum' Unix command) */
1308 sum
= (sum
>> 1) | (sum
<< 15);
1311 monitor_printf(mon
, "%05d\n", sum
);
1314 static int mouse_button_state
;
1316 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1318 int dx
, dy
, dz
, button
;
1319 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1320 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1321 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1323 dx
= strtol(dx_str
, NULL
, 0);
1324 dy
= strtol(dy_str
, NULL
, 0);
1325 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1326 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1329 dz
= strtol(dz_str
, NULL
, 0);
1331 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1332 qemu_input_queue_btn(NULL
, button
, true);
1333 qemu_input_event_sync();
1334 qemu_input_queue_btn(NULL
, button
, false);
1337 qemu_input_event_sync();
1340 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1342 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
1343 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1344 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1345 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1347 int button_state
= qdict_get_int(qdict
, "button_state");
1349 if (mouse_button_state
== button_state
) {
1352 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1353 qemu_input_event_sync();
1354 mouse_button_state
= button_state
;
1357 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1359 int size
= qdict_get_int(qdict
, "size");
1360 int addr
= qdict_get_int(qdict
, "addr");
1361 int has_index
= qdict_haskey(qdict
, "index");
1366 int index
= qdict_get_int(qdict
, "index");
1367 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1375 val
= cpu_inb(addr
);
1379 val
= cpu_inw(addr
);
1383 val
= cpu_inl(addr
);
1387 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1388 suffix
, addr
, size
* 2, val
);
1391 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1393 int size
= qdict_get_int(qdict
, "size");
1394 int addr
= qdict_get_int(qdict
, "addr");
1395 int val
= qdict_get_int(qdict
, "val");
1397 addr
&= IOPORTS_MASK
;
1402 cpu_outb(addr
, val
);
1405 cpu_outw(addr
, val
);
1408 cpu_outl(addr
, val
);
1413 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1415 Error
*local_err
= NULL
;
1416 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1418 qemu_boot_set(bootdevice
, &local_err
);
1420 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1421 error_free(local_err
);
1423 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1427 #if defined(TARGET_I386)
1428 static void print_pte(Monitor
*mon
, hwaddr addr
,
1432 #ifdef TARGET_X86_64
1433 if (addr
& (1ULL << 47)) {
1437 monitor_printf(mon
, TARGET_FMT_plx
": " TARGET_FMT_plx
1438 " %c%c%c%c%c%c%c%c%c\n",
1441 pte
& PG_NX_MASK
? 'X' : '-',
1442 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1443 pte
& PG_PSE_MASK
? 'P' : '-',
1444 pte
& PG_DIRTY_MASK
? 'D' : '-',
1445 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1446 pte
& PG_PCD_MASK
? 'C' : '-',
1447 pte
& PG_PWT_MASK
? 'T' : '-',
1448 pte
& PG_USER_MASK
? 'U' : '-',
1449 pte
& PG_RW_MASK
? 'W' : '-');
1452 static void tlb_info_32(Monitor
*mon
, CPUArchState
*env
)
1454 unsigned int l1
, l2
;
1455 uint32_t pgd
, pde
, pte
;
1457 pgd
= env
->cr
[3] & ~0xfff;
1458 for(l1
= 0; l1
< 1024; l1
++) {
1459 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1460 pde
= le32_to_cpu(pde
);
1461 if (pde
& PG_PRESENT_MASK
) {
1462 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1464 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 21) - 1));
1466 for(l2
= 0; l2
< 1024; l2
++) {
1467 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1468 pte
= le32_to_cpu(pte
);
1469 if (pte
& PG_PRESENT_MASK
) {
1470 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1480 static void tlb_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1482 unsigned int l1
, l2
, l3
;
1483 uint64_t pdpe
, pde
, pte
;
1484 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1486 pdp_addr
= env
->cr
[3] & ~0x1f;
1487 for (l1
= 0; l1
< 4; l1
++) {
1488 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1489 pdpe
= le64_to_cpu(pdpe
);
1490 if (pdpe
& PG_PRESENT_MASK
) {
1491 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1492 for (l2
= 0; l2
< 512; l2
++) {
1493 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1494 pde
= le64_to_cpu(pde
);
1495 if (pde
& PG_PRESENT_MASK
) {
1496 if (pde
& PG_PSE_MASK
) {
1497 /* 2M pages with PAE, CR4.PSE is ignored */
1498 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21), pde
,
1499 ~((hwaddr
)(1 << 20) - 1));
1501 pt_addr
= pde
& 0x3fffffffff000ULL
;
1502 for (l3
= 0; l3
< 512; l3
++) {
1503 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1504 pte
= le64_to_cpu(pte
);
1505 if (pte
& PG_PRESENT_MASK
) {
1506 print_pte(mon
, (l1
<< 30 ) + (l2
<< 21)
1519 #ifdef TARGET_X86_64
1520 static void tlb_info_64(Monitor
*mon
, CPUArchState
*env
)
1522 uint64_t l1
, l2
, l3
, l4
;
1523 uint64_t pml4e
, pdpe
, pde
, pte
;
1524 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
;
1526 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1527 for (l1
= 0; l1
< 512; l1
++) {
1528 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1529 pml4e
= le64_to_cpu(pml4e
);
1530 if (pml4e
& PG_PRESENT_MASK
) {
1531 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1532 for (l2
= 0; l2
< 512; l2
++) {
1533 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1534 pdpe
= le64_to_cpu(pdpe
);
1535 if (pdpe
& PG_PRESENT_MASK
) {
1536 if (pdpe
& PG_PSE_MASK
) {
1537 /* 1G pages, CR4.PSE is ignored */
1538 print_pte(mon
, (l1
<< 39) + (l2
<< 30), pdpe
,
1539 0x3ffffc0000000ULL
);
1541 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1542 for (l3
= 0; l3
< 512; l3
++) {
1543 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1544 pde
= le64_to_cpu(pde
);
1545 if (pde
& PG_PRESENT_MASK
) {
1546 if (pde
& PG_PSE_MASK
) {
1547 /* 2M pages, CR4.PSE is ignored */
1548 print_pte(mon
, (l1
<< 39) + (l2
<< 30) +
1550 0x3ffffffe00000ULL
);
1552 pt_addr
= pde
& 0x3fffffffff000ULL
;
1553 for (l4
= 0; l4
< 512; l4
++) {
1554 cpu_physical_memory_read(pt_addr
1557 pte
= le64_to_cpu(pte
);
1558 if (pte
& PG_PRESENT_MASK
) {
1559 print_pte(mon
, (l1
<< 39) +
1561 (l3
<< 21) + (l4
<< 12),
1563 0x3fffffffff000ULL
);
1577 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1581 env
= mon_get_cpu_env();
1583 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1584 monitor_printf(mon
, "PG disabled\n");
1587 if (env
->cr
[4] & CR4_PAE_MASK
) {
1588 #ifdef TARGET_X86_64
1589 if (env
->hflags
& HF_LMA_MASK
) {
1590 tlb_info_64(mon
, env
);
1594 tlb_info_pae32(mon
, env
);
1597 tlb_info_32(mon
, env
);
1601 static void mem_print(Monitor
*mon
, hwaddr
*pstart
,
1603 hwaddr end
, int prot
)
1606 prot1
= *plast_prot
;
1607 if (prot
!= prot1
) {
1608 if (*pstart
!= -1) {
1609 monitor_printf(mon
, TARGET_FMT_plx
"-" TARGET_FMT_plx
" "
1610 TARGET_FMT_plx
" %c%c%c\n",
1611 *pstart
, end
, end
- *pstart
,
1612 prot1
& PG_USER_MASK
? 'u' : '-',
1614 prot1
& PG_RW_MASK
? 'w' : '-');
1624 static void mem_info_32(Monitor
*mon
, CPUArchState
*env
)
1626 unsigned int l1
, l2
;
1627 int prot
, last_prot
;
1628 uint32_t pgd
, pde
, pte
;
1631 pgd
= env
->cr
[3] & ~0xfff;
1634 for(l1
= 0; l1
< 1024; l1
++) {
1635 cpu_physical_memory_read(pgd
+ l1
* 4, &pde
, 4);
1636 pde
= le32_to_cpu(pde
);
1638 if (pde
& PG_PRESENT_MASK
) {
1639 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1640 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1641 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1643 for(l2
= 0; l2
< 1024; l2
++) {
1644 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4, &pte
, 4);
1645 pte
= le32_to_cpu(pte
);
1646 end
= (l1
<< 22) + (l2
<< 12);
1647 if (pte
& PG_PRESENT_MASK
) {
1649 (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1653 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1658 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1661 /* Flush last range */
1662 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1665 static void mem_info_pae32(Monitor
*mon
, CPUArchState
*env
)
1667 unsigned int l1
, l2
, l3
;
1668 int prot
, last_prot
;
1669 uint64_t pdpe
, pde
, pte
;
1670 uint64_t pdp_addr
, pd_addr
, pt_addr
;
1673 pdp_addr
= env
->cr
[3] & ~0x1f;
1676 for (l1
= 0; l1
< 4; l1
++) {
1677 cpu_physical_memory_read(pdp_addr
+ l1
* 8, &pdpe
, 8);
1678 pdpe
= le64_to_cpu(pdpe
);
1680 if (pdpe
& PG_PRESENT_MASK
) {
1681 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1682 for (l2
= 0; l2
< 512; l2
++) {
1683 cpu_physical_memory_read(pd_addr
+ l2
* 8, &pde
, 8);
1684 pde
= le64_to_cpu(pde
);
1685 end
= (l1
<< 30) + (l2
<< 21);
1686 if (pde
& PG_PRESENT_MASK
) {
1687 if (pde
& PG_PSE_MASK
) {
1688 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1690 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1692 pt_addr
= pde
& 0x3fffffffff000ULL
;
1693 for (l3
= 0; l3
< 512; l3
++) {
1694 cpu_physical_memory_read(pt_addr
+ l3
* 8, &pte
, 8);
1695 pte
= le64_to_cpu(pte
);
1696 end
= (l1
<< 30) + (l2
<< 21) + (l3
<< 12);
1697 if (pte
& PG_PRESENT_MASK
) {
1698 prot
= pte
& pde
& (PG_USER_MASK
| PG_RW_MASK
|
1703 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1708 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1713 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1716 /* Flush last range */
1717 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 32, 0);
1721 #ifdef TARGET_X86_64
1722 static void mem_info_64(Monitor
*mon
, CPUArchState
*env
)
1724 int prot
, last_prot
;
1725 uint64_t l1
, l2
, l3
, l4
;
1726 uint64_t pml4e
, pdpe
, pde
, pte
;
1727 uint64_t pml4_addr
, pdp_addr
, pd_addr
, pt_addr
, start
, end
;
1729 pml4_addr
= env
->cr
[3] & 0x3fffffffff000ULL
;
1732 for (l1
= 0; l1
< 512; l1
++) {
1733 cpu_physical_memory_read(pml4_addr
+ l1
* 8, &pml4e
, 8);
1734 pml4e
= le64_to_cpu(pml4e
);
1736 if (pml4e
& PG_PRESENT_MASK
) {
1737 pdp_addr
= pml4e
& 0x3fffffffff000ULL
;
1738 for (l2
= 0; l2
< 512; l2
++) {
1739 cpu_physical_memory_read(pdp_addr
+ l2
* 8, &pdpe
, 8);
1740 pdpe
= le64_to_cpu(pdpe
);
1741 end
= (l1
<< 39) + (l2
<< 30);
1742 if (pdpe
& PG_PRESENT_MASK
) {
1743 if (pdpe
& PG_PSE_MASK
) {
1744 prot
= pdpe
& (PG_USER_MASK
| PG_RW_MASK
|
1747 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1749 pd_addr
= pdpe
& 0x3fffffffff000ULL
;
1750 for (l3
= 0; l3
< 512; l3
++) {
1751 cpu_physical_memory_read(pd_addr
+ l3
* 8, &pde
, 8);
1752 pde
= le64_to_cpu(pde
);
1753 end
= (l1
<< 39) + (l2
<< 30) + (l3
<< 21);
1754 if (pde
& PG_PRESENT_MASK
) {
1755 if (pde
& PG_PSE_MASK
) {
1756 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
|
1758 prot
&= pml4e
& pdpe
;
1759 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1761 pt_addr
= pde
& 0x3fffffffff000ULL
;
1762 for (l4
= 0; l4
< 512; l4
++) {
1763 cpu_physical_memory_read(pt_addr
1766 pte
= le64_to_cpu(pte
);
1767 end
= (l1
<< 39) + (l2
<< 30) +
1768 (l3
<< 21) + (l4
<< 12);
1769 if (pte
& PG_PRESENT_MASK
) {
1770 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
|
1772 prot
&= pml4e
& pdpe
& pde
;
1776 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1781 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1787 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1792 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1795 /* Flush last range */
1796 mem_print(mon
, &start
, &last_prot
, (hwaddr
)1 << 48, 0);
1800 static void hmp_info_mem(Monitor
*mon
, const QDict
*qdict
)
1804 env
= mon_get_cpu_env();
1806 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1807 monitor_printf(mon
, "PG disabled\n");
1810 if (env
->cr
[4] & CR4_PAE_MASK
) {
1811 #ifdef TARGET_X86_64
1812 if (env
->hflags
& HF_LMA_MASK
) {
1813 mem_info_64(mon
, env
);
1817 mem_info_pae32(mon
, env
);
1820 mem_info_32(mon
, env
);
1825 #if defined(TARGET_SH4)
1827 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1829 monitor_printf(mon
, " tlb%i:\t"
1830 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1831 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1832 "dirty=%hhu writethrough=%hhu\n",
1834 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1835 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1839 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1841 CPUArchState
*env
= mon_get_cpu_env();
1844 monitor_printf (mon
, "ITLB:\n");
1845 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1846 print_tlb (mon
, i
, &env
->itlb
[i
]);
1847 monitor_printf (mon
, "UTLB:\n");
1848 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1849 print_tlb (mon
, i
, &env
->utlb
[i
]);
1854 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1855 static void hmp_info_tlb(Monitor
*mon
, const QDict
*qdict
)
1857 CPUArchState
*env1
= mon_get_cpu_env();
1859 dump_mmu((FILE*)mon
, (fprintf_function
)monitor_printf
, env1
);
1863 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1865 mtree_info((fprintf_function
)monitor_printf
, mon
);
1868 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1874 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1875 query_numa_node_mem(node_mem
);
1876 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1877 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1878 monitor_printf(mon
, "node %d cpus:", i
);
1880 if (cpu
->numa_node
== i
) {
1881 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1884 monitor_printf(mon
, "\n");
1885 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1891 #ifdef CONFIG_PROFILER
1896 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1898 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1899 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1900 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1901 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1906 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1908 monitor_printf(mon
, "Internal profiler not compiled\n");
1912 /* Capture support */
1913 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1915 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1920 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1921 monitor_printf(mon
, "[%d]: ", i
);
1922 s
->ops
.info (s
->opaque
);
1926 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1929 int n
= qdict_get_int(qdict
, "n");
1932 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1934 s
->ops
.destroy (s
->opaque
);
1935 QLIST_REMOVE (s
, entries
);
1942 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1944 const char *path
= qdict_get_str(qdict
, "path");
1945 int has_freq
= qdict_haskey(qdict
, "freq");
1946 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1947 int has_bits
= qdict_haskey(qdict
, "bits");
1948 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1949 int has_channels
= qdict_haskey(qdict
, "nchannels");
1950 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1953 s
= g_malloc0 (sizeof (*s
));
1955 freq
= has_freq
? freq
: 44100;
1956 bits
= has_bits
? bits
: 16;
1957 nchannels
= has_channels
? nchannels
: 2;
1959 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1960 monitor_printf(mon
, "Failed to add wave capture\n");
1964 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1967 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1969 qemu_acl
*acl
= qemu_acl_find(name
);
1972 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1977 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1979 const char *aclname
= qdict_get_str(qdict
, "aclname");
1980 qemu_acl
*acl
= find_acl(mon
, aclname
);
1981 qemu_acl_entry
*entry
;
1985 monitor_printf(mon
, "policy: %s\n",
1986 acl
->defaultDeny
? "deny" : "allow");
1987 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1989 monitor_printf(mon
, "%d: %s %s\n", i
,
1990 entry
->deny
? "deny" : "allow", entry
->match
);
1995 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1997 const char *aclname
= qdict_get_str(qdict
, "aclname");
1998 qemu_acl
*acl
= find_acl(mon
, aclname
);
2001 qemu_acl_reset(acl
);
2002 monitor_printf(mon
, "acl: removed all rules\n");
2006 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2008 const char *aclname
= qdict_get_str(qdict
, "aclname");
2009 const char *policy
= qdict_get_str(qdict
, "policy");
2010 qemu_acl
*acl
= find_acl(mon
, aclname
);
2013 if (strcmp(policy
, "allow") == 0) {
2014 acl
->defaultDeny
= 0;
2015 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2016 } else if (strcmp(policy
, "deny") == 0) {
2017 acl
->defaultDeny
= 1;
2018 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2020 monitor_printf(mon
, "acl: unknown policy '%s', "
2021 "expected 'deny' or 'allow'\n", policy
);
2026 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2028 const char *aclname
= qdict_get_str(qdict
, "aclname");
2029 const char *match
= qdict_get_str(qdict
, "match");
2030 const char *policy
= qdict_get_str(qdict
, "policy");
2031 int has_index
= qdict_haskey(qdict
, "index");
2032 int index
= qdict_get_try_int(qdict
, "index", -1);
2033 qemu_acl
*acl
= find_acl(mon
, aclname
);
2037 if (strcmp(policy
, "allow") == 0) {
2039 } else if (strcmp(policy
, "deny") == 0) {
2042 monitor_printf(mon
, "acl: unknown policy '%s', "
2043 "expected 'deny' or 'allow'\n", policy
);
2047 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2049 ret
= qemu_acl_append(acl
, deny
, match
);
2051 monitor_printf(mon
, "acl: unable to add acl entry\n");
2053 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2057 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2059 const char *aclname
= qdict_get_str(qdict
, "aclname");
2060 const char *match
= qdict_get_str(qdict
, "match");
2061 qemu_acl
*acl
= find_acl(mon
, aclname
);
2065 ret
= qemu_acl_remove(acl
, match
);
2067 monitor_printf(mon
, "acl: no matching acl entry\n");
2069 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2073 #if defined(TARGET_I386)
2074 static void hmp_mce(Monitor
*mon
, const QDict
*qdict
)
2078 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
2079 int bank
= qdict_get_int(qdict
, "bank");
2080 uint64_t status
= qdict_get_int(qdict
, "status");
2081 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
2082 uint64_t addr
= qdict_get_int(qdict
, "addr");
2083 uint64_t misc
= qdict_get_int(qdict
, "misc");
2084 int flags
= MCE_INJECT_UNCOND_AO
;
2086 if (qdict_get_try_bool(qdict
, "broadcast", false)) {
2087 flags
|= MCE_INJECT_BROADCAST
;
2089 cs
= qemu_get_cpu(cpu_index
);
2092 cpu_x86_inject_mce(mon
, cpu
, bank
, status
, mcg_status
, addr
, misc
,
2098 void qmp_getfd(const char *fdname
, Error
**errp
)
2103 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
2105 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2109 if (qemu_isdigit(fdname
[0])) {
2111 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2112 "a name not starting with a digit");
2116 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2117 if (strcmp(monfd
->name
, fdname
) != 0) {
2126 monfd
= g_malloc0(sizeof(mon_fd_t
));
2127 monfd
->name
= g_strdup(fdname
);
2130 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2133 void qmp_closefd(const char *fdname
, Error
**errp
)
2137 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2138 if (strcmp(monfd
->name
, fdname
) != 0) {
2142 QLIST_REMOVE(monfd
, next
);
2144 g_free(monfd
->name
);
2149 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2152 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
2154 int saved_vm_running
= runstate_is_running();
2155 const char *name
= qdict_get_str(qdict
, "name");
2157 vm_stop(RUN_STATE_RESTORE_VM
);
2159 if (load_vmstate(name
) == 0 && saved_vm_running
) {
2164 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2168 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2171 if (strcmp(monfd
->name
, fdname
) != 0) {
2177 /* caller takes ownership of fd */
2178 QLIST_REMOVE(monfd
, next
);
2179 g_free(monfd
->name
);
2185 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2189 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2191 MonFdsetFd
*mon_fdset_fd
;
2192 MonFdsetFd
*mon_fdset_fd_next
;
2194 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2195 if ((mon_fdset_fd
->removed
||
2196 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2197 runstate_is_running()) {
2198 close(mon_fdset_fd
->fd
);
2199 g_free(mon_fdset_fd
->opaque
);
2200 QLIST_REMOVE(mon_fdset_fd
, next
);
2201 g_free(mon_fdset_fd
);
2205 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2206 QLIST_REMOVE(mon_fdset
, next
);
2211 static void monitor_fdsets_cleanup(void)
2213 MonFdset
*mon_fdset
;
2214 MonFdset
*mon_fdset_next
;
2216 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2217 monitor_fdset_cleanup(mon_fdset
);
2221 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2222 const char *opaque
, Error
**errp
)
2225 Monitor
*mon
= cur_mon
;
2228 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
2230 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2234 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2235 has_opaque
, opaque
, errp
);
2247 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2249 MonFdset
*mon_fdset
;
2250 MonFdsetFd
*mon_fdset_fd
;
2253 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2254 if (mon_fdset
->id
!= fdset_id
) {
2257 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2259 if (mon_fdset_fd
->fd
!= fd
) {
2262 mon_fdset_fd
->removed
= true;
2265 mon_fdset_fd
->removed
= true;
2268 if (has_fd
&& !mon_fdset_fd
) {
2271 monitor_fdset_cleanup(mon_fdset
);
2277 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2280 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2282 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2285 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2287 MonFdset
*mon_fdset
;
2288 MonFdsetFd
*mon_fdset_fd
;
2289 FdsetInfoList
*fdset_list
= NULL
;
2291 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2292 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2293 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2295 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2296 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2298 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2299 FdsetFdInfoList
*fdsetfd_info
;
2301 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2302 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2303 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2304 if (mon_fdset_fd
->opaque
) {
2305 fdsetfd_info
->value
->has_opaque
= true;
2306 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2308 fdsetfd_info
->value
->has_opaque
= false;
2311 fdsetfd_info
->next
= fdsetfd_list
;
2312 fdsetfd_list
= fdsetfd_info
;
2315 fdset_info
->value
->fds
= fdsetfd_list
;
2317 fdset_info
->next
= fdset_list
;
2318 fdset_list
= fdset_info
;
2324 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2325 bool has_opaque
, const char *opaque
,
2328 MonFdset
*mon_fdset
= NULL
;
2329 MonFdsetFd
*mon_fdset_fd
;
2333 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2334 /* Break if match found or match impossible due to ordering by ID */
2335 if (fdset_id
<= mon_fdset
->id
) {
2336 if (fdset_id
< mon_fdset
->id
) {
2344 if (mon_fdset
== NULL
) {
2345 int64_t fdset_id_prev
= -1;
2346 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2350 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2351 "a non-negative value");
2354 /* Use specified fdset ID */
2355 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2356 mon_fdset_cur
= mon_fdset
;
2357 if (fdset_id
< mon_fdset_cur
->id
) {
2362 /* Use first available fdset ID */
2363 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2364 mon_fdset_cur
= mon_fdset
;
2365 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2366 fdset_id_prev
= mon_fdset_cur
->id
;
2373 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2375 mon_fdset
->id
= fdset_id
;
2377 mon_fdset
->id
= fdset_id_prev
+ 1;
2380 /* The fdset list is ordered by fdset ID */
2381 if (!mon_fdset_cur
) {
2382 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2383 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2384 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2386 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2390 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2391 mon_fdset_fd
->fd
= fd
;
2392 mon_fdset_fd
->removed
= false;
2394 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2396 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2398 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2399 fdinfo
->fdset_id
= mon_fdset
->id
;
2400 fdinfo
->fd
= mon_fdset_fd
->fd
;
2405 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2408 MonFdset
*mon_fdset
;
2409 MonFdsetFd
*mon_fdset_fd
;
2412 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2413 if (mon_fdset
->id
!= fdset_id
) {
2416 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2417 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2418 if (mon_fd_flags
== -1) {
2422 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2423 return mon_fdset_fd
->fd
;
2435 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2437 MonFdset
*mon_fdset
;
2438 MonFdsetFd
*mon_fdset_fd_dup
;
2440 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2441 if (mon_fdset
->id
!= fdset_id
) {
2444 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2445 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2449 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2450 mon_fdset_fd_dup
->fd
= dup_fd
;
2451 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2457 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2459 MonFdset
*mon_fdset
;
2460 MonFdsetFd
*mon_fdset_fd_dup
;
2462 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2463 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2464 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2466 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2467 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2468 monitor_fdset_cleanup(mon_fdset
);
2472 return mon_fdset
->id
;
2480 int monitor_fdset_dup_fd_find(int dup_fd
)
2482 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2485 void monitor_fdset_dup_fd_remove(int dup_fd
)
2487 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2490 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2493 Error
*local_err
= NULL
;
2495 if (!qemu_isdigit(fdname
[0]) && mon
) {
2496 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2498 fd
= qemu_parse_fd(fdname
);
2500 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2505 error_propagate(errp
, local_err
);
2514 /* Please update hmp-commands.hx when adding or changing commands */
2515 static mon_cmd_t info_cmds
[] = {
2516 #include "hmp-commands-info.h"
2520 /* mon_cmds and info_cmds would be sorted at runtime */
2521 static mon_cmd_t mon_cmds
[] = {
2522 #include "hmp-commands.h"
2526 static const mon_cmd_t qmp_cmds
[] = {
2527 #include "qmp-commands-old.h"
2531 /*******************************************************************/
2533 static const char *pch
;
2534 static sigjmp_buf expr_env
;
2539 typedef struct MonitorDef
{
2542 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2546 #if defined(TARGET_I386)
2547 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2549 CPUArchState
*env
= mon_get_cpu_env();
2550 return env
->eip
+ env
->segs
[R_CS
].base
;
2554 #if defined(TARGET_PPC)
2555 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2557 CPUArchState
*env
= mon_get_cpu_env();
2562 for (i
= 0; i
< 8; i
++)
2563 u
|= env
->crf
[i
] << (32 - (4 * (i
+ 1)));
2568 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2570 CPUArchState
*env
= mon_get_cpu_env();
2574 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2576 CPUArchState
*env
= mon_get_cpu_env();
2580 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2582 CPUArchState
*env
= mon_get_cpu_env();
2583 return cpu_ppc_load_decr(env
);
2586 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2588 CPUArchState
*env
= mon_get_cpu_env();
2589 return cpu_ppc_load_tbu(env
);
2592 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2594 CPUArchState
*env
= mon_get_cpu_env();
2595 return cpu_ppc_load_tbl(env
);
2599 #if defined(TARGET_SPARC)
2600 #ifndef TARGET_SPARC64
2601 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2603 CPUArchState
*env
= mon_get_cpu_env();
2605 return cpu_get_psr(env
);
2609 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2611 CPUArchState
*env
= mon_get_cpu_env();
2612 return env
->regwptr
[val
];
2616 static const MonitorDef monitor_defs
[] = {
2619 #define SEG(name, seg) \
2620 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2621 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2622 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2624 { "eax", offsetof(CPUX86State
, regs
[0]) },
2625 { "ecx", offsetof(CPUX86State
, regs
[1]) },
2626 { "edx", offsetof(CPUX86State
, regs
[2]) },
2627 { "ebx", offsetof(CPUX86State
, regs
[3]) },
2628 { "esp|sp", offsetof(CPUX86State
, regs
[4]) },
2629 { "ebp|fp", offsetof(CPUX86State
, regs
[5]) },
2630 { "esi", offsetof(CPUX86State
, regs
[6]) },
2631 { "edi", offsetof(CPUX86State
, regs
[7]) },
2632 #ifdef TARGET_X86_64
2633 { "r8", offsetof(CPUX86State
, regs
[8]) },
2634 { "r9", offsetof(CPUX86State
, regs
[9]) },
2635 { "r10", offsetof(CPUX86State
, regs
[10]) },
2636 { "r11", offsetof(CPUX86State
, regs
[11]) },
2637 { "r12", offsetof(CPUX86State
, regs
[12]) },
2638 { "r13", offsetof(CPUX86State
, regs
[13]) },
2639 { "r14", offsetof(CPUX86State
, regs
[14]) },
2640 { "r15", offsetof(CPUX86State
, regs
[15]) },
2642 { "eflags", offsetof(CPUX86State
, eflags
) },
2643 { "eip", offsetof(CPUX86State
, eip
) },
2650 { "pc", 0, monitor_get_pc
, },
2651 #elif defined(TARGET_PPC)
2652 /* General purpose registers */
2653 { "r0", offsetof(CPUPPCState
, gpr
[0]) },
2654 { "r1", offsetof(CPUPPCState
, gpr
[1]) },
2655 { "r2", offsetof(CPUPPCState
, gpr
[2]) },
2656 { "r3", offsetof(CPUPPCState
, gpr
[3]) },
2657 { "r4", offsetof(CPUPPCState
, gpr
[4]) },
2658 { "r5", offsetof(CPUPPCState
, gpr
[5]) },
2659 { "r6", offsetof(CPUPPCState
, gpr
[6]) },
2660 { "r7", offsetof(CPUPPCState
, gpr
[7]) },
2661 { "r8", offsetof(CPUPPCState
, gpr
[8]) },
2662 { "r9", offsetof(CPUPPCState
, gpr
[9]) },
2663 { "r10", offsetof(CPUPPCState
, gpr
[10]) },
2664 { "r11", offsetof(CPUPPCState
, gpr
[11]) },
2665 { "r12", offsetof(CPUPPCState
, gpr
[12]) },
2666 { "r13", offsetof(CPUPPCState
, gpr
[13]) },
2667 { "r14", offsetof(CPUPPCState
, gpr
[14]) },
2668 { "r15", offsetof(CPUPPCState
, gpr
[15]) },
2669 { "r16", offsetof(CPUPPCState
, gpr
[16]) },
2670 { "r17", offsetof(CPUPPCState
, gpr
[17]) },
2671 { "r18", offsetof(CPUPPCState
, gpr
[18]) },
2672 { "r19", offsetof(CPUPPCState
, gpr
[19]) },
2673 { "r20", offsetof(CPUPPCState
, gpr
[20]) },
2674 { "r21", offsetof(CPUPPCState
, gpr
[21]) },
2675 { "r22", offsetof(CPUPPCState
, gpr
[22]) },
2676 { "r23", offsetof(CPUPPCState
, gpr
[23]) },
2677 { "r24", offsetof(CPUPPCState
, gpr
[24]) },
2678 { "r25", offsetof(CPUPPCState
, gpr
[25]) },
2679 { "r26", offsetof(CPUPPCState
, gpr
[26]) },
2680 { "r27", offsetof(CPUPPCState
, gpr
[27]) },
2681 { "r28", offsetof(CPUPPCState
, gpr
[28]) },
2682 { "r29", offsetof(CPUPPCState
, gpr
[29]) },
2683 { "r30", offsetof(CPUPPCState
, gpr
[30]) },
2684 { "r31", offsetof(CPUPPCState
, gpr
[31]) },
2685 /* Floating point registers */
2686 { "f0", offsetof(CPUPPCState
, fpr
[0]) },
2687 { "f1", offsetof(CPUPPCState
, fpr
[1]) },
2688 { "f2", offsetof(CPUPPCState
, fpr
[2]) },
2689 { "f3", offsetof(CPUPPCState
, fpr
[3]) },
2690 { "f4", offsetof(CPUPPCState
, fpr
[4]) },
2691 { "f5", offsetof(CPUPPCState
, fpr
[5]) },
2692 { "f6", offsetof(CPUPPCState
, fpr
[6]) },
2693 { "f7", offsetof(CPUPPCState
, fpr
[7]) },
2694 { "f8", offsetof(CPUPPCState
, fpr
[8]) },
2695 { "f9", offsetof(CPUPPCState
, fpr
[9]) },
2696 { "f10", offsetof(CPUPPCState
, fpr
[10]) },
2697 { "f11", offsetof(CPUPPCState
, fpr
[11]) },
2698 { "f12", offsetof(CPUPPCState
, fpr
[12]) },
2699 { "f13", offsetof(CPUPPCState
, fpr
[13]) },
2700 { "f14", offsetof(CPUPPCState
, fpr
[14]) },
2701 { "f15", offsetof(CPUPPCState
, fpr
[15]) },
2702 { "f16", offsetof(CPUPPCState
, fpr
[16]) },
2703 { "f17", offsetof(CPUPPCState
, fpr
[17]) },
2704 { "f18", offsetof(CPUPPCState
, fpr
[18]) },
2705 { "f19", offsetof(CPUPPCState
, fpr
[19]) },
2706 { "f20", offsetof(CPUPPCState
, fpr
[20]) },
2707 { "f21", offsetof(CPUPPCState
, fpr
[21]) },
2708 { "f22", offsetof(CPUPPCState
, fpr
[22]) },
2709 { "f23", offsetof(CPUPPCState
, fpr
[23]) },
2710 { "f24", offsetof(CPUPPCState
, fpr
[24]) },
2711 { "f25", offsetof(CPUPPCState
, fpr
[25]) },
2712 { "f26", offsetof(CPUPPCState
, fpr
[26]) },
2713 { "f27", offsetof(CPUPPCState
, fpr
[27]) },
2714 { "f28", offsetof(CPUPPCState
, fpr
[28]) },
2715 { "f29", offsetof(CPUPPCState
, fpr
[29]) },
2716 { "f30", offsetof(CPUPPCState
, fpr
[30]) },
2717 { "f31", offsetof(CPUPPCState
, fpr
[31]) },
2718 { "fpscr", offsetof(CPUPPCState
, fpscr
) },
2719 /* Next instruction pointer */
2720 { "nip|pc", offsetof(CPUPPCState
, nip
) },
2721 { "lr", offsetof(CPUPPCState
, lr
) },
2722 { "ctr", offsetof(CPUPPCState
, ctr
) },
2723 { "decr", 0, &monitor_get_decr
, },
2724 { "ccr", 0, &monitor_get_ccr
, },
2725 /* Machine state register */
2726 { "msr", 0, &monitor_get_msr
, },
2727 { "xer", 0, &monitor_get_xer
, },
2728 { "tbu", 0, &monitor_get_tbu
, },
2729 { "tbl", 0, &monitor_get_tbl
, },
2730 /* Segment registers */
2731 { "sdr1", offsetof(CPUPPCState
, spr
[SPR_SDR1
]) },
2732 { "sr0", offsetof(CPUPPCState
, sr
[0]) },
2733 { "sr1", offsetof(CPUPPCState
, sr
[1]) },
2734 { "sr2", offsetof(CPUPPCState
, sr
[2]) },
2735 { "sr3", offsetof(CPUPPCState
, sr
[3]) },
2736 { "sr4", offsetof(CPUPPCState
, sr
[4]) },
2737 { "sr5", offsetof(CPUPPCState
, sr
[5]) },
2738 { "sr6", offsetof(CPUPPCState
, sr
[6]) },
2739 { "sr7", offsetof(CPUPPCState
, sr
[7]) },
2740 { "sr8", offsetof(CPUPPCState
, sr
[8]) },
2741 { "sr9", offsetof(CPUPPCState
, sr
[9]) },
2742 { "sr10", offsetof(CPUPPCState
, sr
[10]) },
2743 { "sr11", offsetof(CPUPPCState
, sr
[11]) },
2744 { "sr12", offsetof(CPUPPCState
, sr
[12]) },
2745 { "sr13", offsetof(CPUPPCState
, sr
[13]) },
2746 { "sr14", offsetof(CPUPPCState
, sr
[14]) },
2747 { "sr15", offsetof(CPUPPCState
, sr
[15]) },
2748 /* Too lazy to put BATs... */
2749 { "pvr", offsetof(CPUPPCState
, spr
[SPR_PVR
]) },
2751 { "srr0", offsetof(CPUPPCState
, spr
[SPR_SRR0
]) },
2752 { "srr1", offsetof(CPUPPCState
, spr
[SPR_SRR1
]) },
2753 { "dar", offsetof(CPUPPCState
, spr
[SPR_DAR
]) },
2754 { "dsisr", offsetof(CPUPPCState
, spr
[SPR_DSISR
]) },
2755 { "cfar", offsetof(CPUPPCState
, spr
[SPR_CFAR
]) },
2756 { "sprg0", offsetof(CPUPPCState
, spr
[SPR_SPRG0
]) },
2757 { "sprg1", offsetof(CPUPPCState
, spr
[SPR_SPRG1
]) },
2758 { "sprg2", offsetof(CPUPPCState
, spr
[SPR_SPRG2
]) },
2759 { "sprg3", offsetof(CPUPPCState
, spr
[SPR_SPRG3
]) },
2760 { "sprg4", offsetof(CPUPPCState
, spr
[SPR_SPRG4
]) },
2761 { "sprg5", offsetof(CPUPPCState
, spr
[SPR_SPRG5
]) },
2762 { "sprg6", offsetof(CPUPPCState
, spr
[SPR_SPRG6
]) },
2763 { "sprg7", offsetof(CPUPPCState
, spr
[SPR_SPRG7
]) },
2764 { "pid", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID
]) },
2765 { "csrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR0
]) },
2766 { "csrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_CSRR1
]) },
2767 { "esr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_ESR
]) },
2768 { "dear", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DEAR
]) },
2769 { "mcsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSR
]) },
2770 { "tsr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TSR
]) },
2771 { "tcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TCR
]) },
2772 { "vrsave", offsetof(CPUPPCState
, spr
[SPR_VRSAVE
]) },
2773 { "pir", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PIR
]) },
2774 { "mcsrr0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR0
]) },
2775 { "mcsrr1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MCSRR1
]) },
2776 { "decar", offsetof(CPUPPCState
, spr
[SPR_BOOKE_DECAR
]) },
2777 { "ivpr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVPR
]) },
2778 { "epcr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPCR
]) },
2779 { "sprg8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_SPRG8
]) },
2780 { "ivor0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR0
]) },
2781 { "ivor1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR1
]) },
2782 { "ivor2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR2
]) },
2783 { "ivor3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR3
]) },
2784 { "ivor4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR4
]) },
2785 { "ivor5", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR5
]) },
2786 { "ivor6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR6
]) },
2787 { "ivor7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR7
]) },
2788 { "ivor8", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR8
]) },
2789 { "ivor9", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR9
]) },
2790 { "ivor10", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR10
]) },
2791 { "ivor11", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR11
]) },
2792 { "ivor12", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR12
]) },
2793 { "ivor13", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR13
]) },
2794 { "ivor14", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR14
]) },
2795 { "ivor15", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR15
]) },
2796 { "ivor32", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR32
]) },
2797 { "ivor33", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR33
]) },
2798 { "ivor34", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR34
]) },
2799 { "ivor35", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR35
]) },
2800 { "ivor36", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR36
]) },
2801 { "ivor37", offsetof(CPUPPCState
, spr
[SPR_BOOKE_IVOR37
]) },
2802 { "mas0", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS0
]) },
2803 { "mas1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS1
]) },
2804 { "mas2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS2
]) },
2805 { "mas3", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS3
]) },
2806 { "mas4", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS4
]) },
2807 { "mas6", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS6
]) },
2808 { "mas7", offsetof(CPUPPCState
, spr
[SPR_BOOKE_MAS7
]) },
2809 { "mmucfg", offsetof(CPUPPCState
, spr
[SPR_MMUCFG
]) },
2810 { "tlb0cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB0CFG
]) },
2811 { "tlb1cfg", offsetof(CPUPPCState
, spr
[SPR_BOOKE_TLB1CFG
]) },
2812 { "epr", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPR
]) },
2813 { "eplc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPLC
]) },
2814 { "epsc", offsetof(CPUPPCState
, spr
[SPR_BOOKE_EPSC
]) },
2815 { "svr", offsetof(CPUPPCState
, spr
[SPR_E500_SVR
]) },
2816 { "mcar", offsetof(CPUPPCState
, spr
[SPR_Exxx_MCAR
]) },
2817 { "pid1", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID1
]) },
2818 { "pid2", offsetof(CPUPPCState
, spr
[SPR_BOOKE_PID2
]) },
2819 { "hid0", offsetof(CPUPPCState
, spr
[SPR_HID0
]) },
2821 #elif defined(TARGET_SPARC)
2822 { "g0", offsetof(CPUSPARCState
, gregs
[0]) },
2823 { "g1", offsetof(CPUSPARCState
, gregs
[1]) },
2824 { "g2", offsetof(CPUSPARCState
, gregs
[2]) },
2825 { "g3", offsetof(CPUSPARCState
, gregs
[3]) },
2826 { "g4", offsetof(CPUSPARCState
, gregs
[4]) },
2827 { "g5", offsetof(CPUSPARCState
, gregs
[5]) },
2828 { "g6", offsetof(CPUSPARCState
, gregs
[6]) },
2829 { "g7", offsetof(CPUSPARCState
, gregs
[7]) },
2830 { "o0", 0, monitor_get_reg
},
2831 { "o1", 1, monitor_get_reg
},
2832 { "o2", 2, monitor_get_reg
},
2833 { "o3", 3, monitor_get_reg
},
2834 { "o4", 4, monitor_get_reg
},
2835 { "o5", 5, monitor_get_reg
},
2836 { "o6", 6, monitor_get_reg
},
2837 { "o7", 7, monitor_get_reg
},
2838 { "l0", 8, monitor_get_reg
},
2839 { "l1", 9, monitor_get_reg
},
2840 { "l2", 10, monitor_get_reg
},
2841 { "l3", 11, monitor_get_reg
},
2842 { "l4", 12, monitor_get_reg
},
2843 { "l5", 13, monitor_get_reg
},
2844 { "l6", 14, monitor_get_reg
},
2845 { "l7", 15, monitor_get_reg
},
2846 { "i0", 16, monitor_get_reg
},
2847 { "i1", 17, monitor_get_reg
},
2848 { "i2", 18, monitor_get_reg
},
2849 { "i3", 19, monitor_get_reg
},
2850 { "i4", 20, monitor_get_reg
},
2851 { "i5", 21, monitor_get_reg
},
2852 { "i6", 22, monitor_get_reg
},
2853 { "i7", 23, monitor_get_reg
},
2854 { "pc", offsetof(CPUSPARCState
, pc
) },
2855 { "npc", offsetof(CPUSPARCState
, npc
) },
2856 { "y", offsetof(CPUSPARCState
, y
) },
2857 #ifndef TARGET_SPARC64
2858 { "psr", 0, &monitor_get_psr
, },
2859 { "wim", offsetof(CPUSPARCState
, wim
) },
2861 { "tbr", offsetof(CPUSPARCState
, tbr
) },
2862 { "fsr", offsetof(CPUSPARCState
, fsr
) },
2863 { "f0", offsetof(CPUSPARCState
, fpr
[0].l
.upper
) },
2864 { "f1", offsetof(CPUSPARCState
, fpr
[0].l
.lower
) },
2865 { "f2", offsetof(CPUSPARCState
, fpr
[1].l
.upper
) },
2866 { "f3", offsetof(CPUSPARCState
, fpr
[1].l
.lower
) },
2867 { "f4", offsetof(CPUSPARCState
, fpr
[2].l
.upper
) },
2868 { "f5", offsetof(CPUSPARCState
, fpr
[2].l
.lower
) },
2869 { "f6", offsetof(CPUSPARCState
, fpr
[3].l
.upper
) },
2870 { "f7", offsetof(CPUSPARCState
, fpr
[3].l
.lower
) },
2871 { "f8", offsetof(CPUSPARCState
, fpr
[4].l
.upper
) },
2872 { "f9", offsetof(CPUSPARCState
, fpr
[4].l
.lower
) },
2873 { "f10", offsetof(CPUSPARCState
, fpr
[5].l
.upper
) },
2874 { "f11", offsetof(CPUSPARCState
, fpr
[5].l
.lower
) },
2875 { "f12", offsetof(CPUSPARCState
, fpr
[6].l
.upper
) },
2876 { "f13", offsetof(CPUSPARCState
, fpr
[6].l
.lower
) },
2877 { "f14", offsetof(CPUSPARCState
, fpr
[7].l
.upper
) },
2878 { "f15", offsetof(CPUSPARCState
, fpr
[7].l
.lower
) },
2879 { "f16", offsetof(CPUSPARCState
, fpr
[8].l
.upper
) },
2880 { "f17", offsetof(CPUSPARCState
, fpr
[8].l
.lower
) },
2881 { "f18", offsetof(CPUSPARCState
, fpr
[9].l
.upper
) },
2882 { "f19", offsetof(CPUSPARCState
, fpr
[9].l
.lower
) },
2883 { "f20", offsetof(CPUSPARCState
, fpr
[10].l
.upper
) },
2884 { "f21", offsetof(CPUSPARCState
, fpr
[10].l
.lower
) },
2885 { "f22", offsetof(CPUSPARCState
, fpr
[11].l
.upper
) },
2886 { "f23", offsetof(CPUSPARCState
, fpr
[11].l
.lower
) },
2887 { "f24", offsetof(CPUSPARCState
, fpr
[12].l
.upper
) },
2888 { "f25", offsetof(CPUSPARCState
, fpr
[12].l
.lower
) },
2889 { "f26", offsetof(CPUSPARCState
, fpr
[13].l
.upper
) },
2890 { "f27", offsetof(CPUSPARCState
, fpr
[13].l
.lower
) },
2891 { "f28", offsetof(CPUSPARCState
, fpr
[14].l
.upper
) },
2892 { "f29", offsetof(CPUSPARCState
, fpr
[14].l
.lower
) },
2893 { "f30", offsetof(CPUSPARCState
, fpr
[15].l
.upper
) },
2894 { "f31", offsetof(CPUSPARCState
, fpr
[15].l
.lower
) },
2895 #ifdef TARGET_SPARC64
2896 { "f32", offsetof(CPUSPARCState
, fpr
[16]) },
2897 { "f34", offsetof(CPUSPARCState
, fpr
[17]) },
2898 { "f36", offsetof(CPUSPARCState
, fpr
[18]) },
2899 { "f38", offsetof(CPUSPARCState
, fpr
[19]) },
2900 { "f40", offsetof(CPUSPARCState
, fpr
[20]) },
2901 { "f42", offsetof(CPUSPARCState
, fpr
[21]) },
2902 { "f44", offsetof(CPUSPARCState
, fpr
[22]) },
2903 { "f46", offsetof(CPUSPARCState
, fpr
[23]) },
2904 { "f48", offsetof(CPUSPARCState
, fpr
[24]) },
2905 { "f50", offsetof(CPUSPARCState
, fpr
[25]) },
2906 { "f52", offsetof(CPUSPARCState
, fpr
[26]) },
2907 { "f54", offsetof(CPUSPARCState
, fpr
[27]) },
2908 { "f56", offsetof(CPUSPARCState
, fpr
[28]) },
2909 { "f58", offsetof(CPUSPARCState
, fpr
[29]) },
2910 { "f60", offsetof(CPUSPARCState
, fpr
[30]) },
2911 { "f62", offsetof(CPUSPARCState
, fpr
[31]) },
2912 { "asi", offsetof(CPUSPARCState
, asi
) },
2913 { "pstate", offsetof(CPUSPARCState
, pstate
) },
2914 { "cansave", offsetof(CPUSPARCState
, cansave
) },
2915 { "canrestore", offsetof(CPUSPARCState
, canrestore
) },
2916 { "otherwin", offsetof(CPUSPARCState
, otherwin
) },
2917 { "wstate", offsetof(CPUSPARCState
, wstate
) },
2918 { "cleanwin", offsetof(CPUSPARCState
, cleanwin
) },
2919 { "fprs", offsetof(CPUSPARCState
, fprs
) },
2925 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2926 expr_error(Monitor
*mon
, const char *fmt
, ...)
2930 monitor_vprintf(mon
, fmt
, ap
);
2931 monitor_printf(mon
, "\n");
2933 siglongjmp(expr_env
, 1);
2936 /* return 0 if OK, -1 if not found */
2937 static int get_monitor_def(target_long
*pval
, const char *name
)
2939 const MonitorDef
*md
;
2942 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2943 if (compare_cmd(name
, md
->name
)) {
2944 if (md
->get_value
) {
2945 *pval
= md
->get_value(md
, md
->offset
);
2947 CPUArchState
*env
= mon_get_cpu_env();
2948 ptr
= (uint8_t *)env
+ md
->offset
;
2951 *pval
= *(int32_t *)ptr
;
2954 *pval
= *(target_long
*)ptr
;
2967 static void next(void)
2971 while (qemu_isspace(*pch
))
2976 static int64_t expr_sum(Monitor
*mon
);
2978 static int64_t expr_unary(Monitor
*mon
)
2987 n
= expr_unary(mon
);
2991 n
= -expr_unary(mon
);
2995 n
= ~expr_unary(mon
);
3001 expr_error(mon
, "')' expected");
3008 expr_error(mon
, "character constant expected");
3012 expr_error(mon
, "missing terminating \' character");
3022 while ((*pch
>= 'a' && *pch
<= 'z') ||
3023 (*pch
>= 'A' && *pch
<= 'Z') ||
3024 (*pch
>= '0' && *pch
<= '9') ||
3025 *pch
== '_' || *pch
== '.') {
3026 if ((q
- buf
) < sizeof(buf
) - 1)
3030 while (qemu_isspace(*pch
))
3033 ret
= get_monitor_def(®
, buf
);
3035 expr_error(mon
, "unknown register");
3040 expr_error(mon
, "unexpected end of expression");
3045 n
= strtoull(pch
, &p
, 0);
3046 if (errno
== ERANGE
) {
3047 expr_error(mon
, "number too large");
3050 expr_error(mon
, "invalid char '%c' in expression", *p
);
3053 while (qemu_isspace(*pch
))
3061 static int64_t expr_prod(Monitor
*mon
)
3066 val
= expr_unary(mon
);
3069 if (op
!= '*' && op
!= '/' && op
!= '%')
3072 val2
= expr_unary(mon
);
3081 expr_error(mon
, "division by zero");
3092 static int64_t expr_logic(Monitor
*mon
)
3097 val
= expr_prod(mon
);
3100 if (op
!= '&' && op
!= '|' && op
!= '^')
3103 val2
= expr_prod(mon
);
3120 static int64_t expr_sum(Monitor
*mon
)
3125 val
= expr_logic(mon
);
3128 if (op
!= '+' && op
!= '-')
3131 val2
= expr_logic(mon
);
3140 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
3143 if (sigsetjmp(expr_env
, 0)) {
3147 while (qemu_isspace(*pch
))
3149 *pval
= expr_sum(mon
);
3154 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
3156 const char *p
= *pp
;
3160 d
= strtod(p
, &tailp
);
3162 monitor_printf(mon
, "Number expected\n");
3165 if (d
!= d
|| d
- d
!= 0) {
3166 /* NaN or infinity */
3167 monitor_printf(mon
, "Bad number\n");
3176 * Store the command-name in cmdname, and return a pointer to
3177 * the remaining of the command string.
3179 static const char *get_command_name(const char *cmdline
,
3180 char *cmdname
, size_t nlen
)
3183 const char *p
, *pstart
;
3186 while (qemu_isspace(*p
))
3191 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
3196 memcpy(cmdname
, pstart
, len
);
3197 cmdname
[len
] = '\0';
3202 * Read key of 'type' into 'key' and return the current
3205 static char *key_get_info(const char *type
, char **key
)
3213 p
= strchr(type
, ':');
3220 str
= g_malloc(len
+ 1);
3221 memcpy(str
, type
, len
);
3228 static int default_fmt_format
= 'x';
3229 static int default_fmt_size
= 4;
3231 static int is_valid_option(const char *c
, const char *typestr
)
3239 typestr
= strstr(typestr
, option
);
3240 return (typestr
!= NULL
);
3243 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3244 const char *cmdname
)
3246 const mon_cmd_t
*cmd
;
3248 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3249 if (compare_cmd(cmdname
, cmd
->name
)) {
3257 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
3259 return search_dispatch_table(qmp_cmds
, cmdname
);
3263 * Parse command name from @cmdp according to command table @table.
3264 * If blank, return NULL.
3265 * Else, if no valid command can be found, report to @mon, and return
3267 * Else, change @cmdp to point right behind the name, and return its
3268 * command table entry.
3269 * Do not assume the return value points into @table! It doesn't when
3270 * the command is found in a sub-command table.
3272 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3277 const mon_cmd_t
*cmd
;
3280 /* extract the command name */
3281 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
3285 cmd
= search_dispatch_table(table
, cmdname
);
3287 monitor_printf(mon
, "unknown command: '%.*s'\n",
3288 (int)(p
- *cmdp
), *cmdp
);
3292 /* filter out following useless space */
3293 while (qemu_isspace(*p
)) {
3298 /* search sub command */
3299 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
3300 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
3307 * Parse arguments for @cmd.
3308 * If it can't be parsed, report to @mon, and return NULL.
3309 * Else, insert command arguments into a QDict, and return it.
3310 * Note: On success, caller has to free the QDict structure.
3313 static QDict
*monitor_parse_arguments(Monitor
*mon
,
3315 const mon_cmd_t
*cmd
)
3317 const char *typestr
;
3320 const char *p
= *endp
;
3322 QDict
*qdict
= qdict_new();
3324 /* parse the parameters */
3325 typestr
= cmd
->args_type
;
3327 typestr
= key_get_info(typestr
, &key
);
3339 while (qemu_isspace(*p
))
3341 if (*typestr
== '?') {
3344 /* no optional string: NULL argument */
3348 ret
= get_str(buf
, sizeof(buf
), &p
);
3352 monitor_printf(mon
, "%s: filename expected\n",
3356 monitor_printf(mon
, "%s: block device name expected\n",
3360 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
3365 qdict_put(qdict
, key
, qstring_from_str(buf
));
3370 QemuOptsList
*opts_list
;
3373 opts_list
= qemu_find_opts(key
);
3374 if (!opts_list
|| opts_list
->desc
->name
) {
3377 while (qemu_isspace(*p
)) {
3382 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3385 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
3389 qemu_opts_to_qdict(opts
, qdict
);
3390 qemu_opts_del(opts
);
3395 int count
, format
, size
;
3397 while (qemu_isspace(*p
))
3403 if (qemu_isdigit(*p
)) {
3405 while (qemu_isdigit(*p
)) {
3406 count
= count
* 10 + (*p
- '0');
3444 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3445 monitor_printf(mon
, "invalid char in format: '%c'\n",
3450 format
= default_fmt_format
;
3451 if (format
!= 'i') {
3452 /* for 'i', not specifying a size gives -1 as size */
3454 size
= default_fmt_size
;
3455 default_fmt_size
= size
;
3457 default_fmt_format
= format
;
3460 format
= default_fmt_format
;
3461 if (format
!= 'i') {
3462 size
= default_fmt_size
;
3467 qdict_put(qdict
, "count", qint_from_int(count
));
3468 qdict_put(qdict
, "format", qint_from_int(format
));
3469 qdict_put(qdict
, "size", qint_from_int(size
));
3478 while (qemu_isspace(*p
))
3480 if (*typestr
== '?' || *typestr
== '.') {
3481 if (*typestr
== '?') {
3489 while (qemu_isspace(*p
))
3498 if (get_expr(mon
, &val
, &p
))
3500 /* Check if 'i' is greater than 32-bit */
3501 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3502 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3503 monitor_printf(mon
, "integer is for 32-bit values\n");
3505 } else if (c
== 'M') {
3507 monitor_printf(mon
, "enter a positive value\n");
3512 qdict_put(qdict
, key
, qint_from_int(val
));
3520 while (qemu_isspace(*p
)) {
3523 if (*typestr
== '?') {
3529 val
= strtosz(p
, &end
);
3531 monitor_printf(mon
, "invalid size\n");
3534 qdict_put(qdict
, key
, qint_from_int(val
));
3542 while (qemu_isspace(*p
))
3544 if (*typestr
== '?') {
3550 if (get_double(mon
, &val
, &p
) < 0) {
3553 if (p
[0] && p
[1] == 's') {
3556 val
/= 1e3
; p
+= 2; break;
3558 val
/= 1e6
; p
+= 2; break;
3560 val
/= 1e9
; p
+= 2; break;
3563 if (*p
&& !qemu_isspace(*p
)) {
3564 monitor_printf(mon
, "Unknown unit suffix\n");
3567 qdict_put(qdict
, key
, qfloat_from_double(val
));
3575 while (qemu_isspace(*p
)) {
3579 while (qemu_isgraph(*p
)) {
3582 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3584 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3587 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3590 qdict_put(qdict
, key
, qbool_from_bool(val
));
3595 const char *tmp
= p
;
3602 while (qemu_isspace(*p
))
3607 if(!is_valid_option(p
, typestr
)) {
3609 monitor_printf(mon
, "%s: unsupported option -%c\n",
3621 qdict_put(qdict
, key
, qbool_from_bool(true));
3628 /* package all remaining string */
3631 while (qemu_isspace(*p
)) {
3634 if (*typestr
== '?') {
3637 /* no remaining string: NULL argument */
3643 monitor_printf(mon
, "%s: string expected\n",
3647 qdict_put(qdict
, key
, qstring_from_str(p
));
3653 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3659 /* check that all arguments were parsed */
3660 while (qemu_isspace(*p
))
3663 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3676 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
3679 const mon_cmd_t
*cmd
;
3681 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
3686 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
3688 monitor_printf(mon
, "Try \"help %s\" for more information\n",
3693 cmd
->mhandler
.cmd(mon
, qdict
);
3697 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3699 const char *p
, *pstart
;
3708 p
= pstart
+ strlen(pstart
);
3710 if (len
> sizeof(cmd
) - 2)
3711 len
= sizeof(cmd
) - 2;
3712 memcpy(cmd
, pstart
, len
);
3714 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3715 readline_add_completion(mon
->rs
, cmd
);
3723 static void file_completion(Monitor
*mon
, const char *input
)
3728 char file
[1024], file_prefix
[1024];
3732 p
= strrchr(input
, '/');
3735 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3736 pstrcpy(path
, sizeof(path
), ".");
3738 input_path_len
= p
- input
+ 1;
3739 memcpy(path
, input
, input_path_len
);
3740 if (input_path_len
> sizeof(path
) - 1)
3741 input_path_len
= sizeof(path
) - 1;
3742 path
[input_path_len
] = '\0';
3743 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3746 ffs
= opendir(path
);
3755 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3759 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3760 memcpy(file
, input
, input_path_len
);
3761 if (input_path_len
< sizeof(file
))
3762 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3764 /* stat the file to find out if it's a directory.
3765 * In that case add a slash to speed up typing long paths
3767 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3768 pstrcat(file
, sizeof(file
), "/");
3770 readline_add_completion(mon
->rs
, file
);
3776 static const char *next_arg_type(const char *typestr
)
3778 const char *p
= strchr(typestr
, ':');
3779 return (p
!= NULL
? ++p
: typestr
);
3782 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3785 if (!str
|| !option
) {
3788 if (!strncmp(option
, str
, strlen(str
))) {
3789 readline_add_completion(rs
, option
);
3793 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3796 ChardevBackendInfoList
*list
, *start
;
3802 readline_set_completion_index(rs
, len
);
3804 start
= list
= qmp_query_chardev_backends(NULL
);
3806 const char *chr_name
= list
->value
->name
;
3808 if (!strncmp(chr_name
, str
, len
)) {
3809 readline_add_completion(rs
, chr_name
);
3813 qapi_free_ChardevBackendInfoList(start
);
3816 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3825 readline_set_completion_index(rs
, len
);
3826 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
3827 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3831 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3841 readline_set_completion_index(rs
, len
);
3842 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3845 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3847 name
= object_class_get_name(OBJECT_CLASS(dc
));
3849 if (!dc
->cannot_instantiate_with_device_add_yet
3850 && !strncmp(name
, str
, len
)) {
3851 readline_add_completion(rs
, name
);
3858 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3868 readline_set_completion_index(rs
, len
);
3869 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3873 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3874 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3875 readline_add_completion(rs
, name
);
3882 static void peripheral_device_del_completion(ReadLineState
*rs
,
3883 const char *str
, size_t len
)
3885 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3886 GSList
*list
, *item
;
3888 list
= qdev_build_hotpluggable_device_list(peripheral
);
3893 for (item
= list
; item
; item
= g_slist_next(item
)) {
3894 DeviceState
*dev
= item
->data
;
3896 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3897 readline_add_completion(rs
, dev
->id
);
3904 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3907 ChardevInfoList
*list
, *start
;
3913 readline_set_completion_index(rs
, len
);
3915 start
= list
= qmp_query_chardev(NULL
);
3917 ChardevInfo
*chr
= list
->value
;
3919 if (!strncmp(chr
->label
, str
, len
)) {
3920 readline_add_completion(rs
, chr
->label
);
3924 qapi_free_ChardevInfoList(start
);
3927 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3930 ChardevInfoList
*list
, *start
;
3933 readline_set_completion_index(rs
, len
);
3935 start
= list
= qmp_query_chardev(NULL
);
3937 ChardevInfo
*chr_info
= list
->value
;
3939 if (!strncmp(chr_info
->label
, str
, len
)) {
3940 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3941 if (chr
&& chr_is_ringbuf(chr
)) {
3942 readline_add_completion(rs
, chr_info
->label
);
3947 qapi_free_ChardevInfoList(start
);
3950 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3955 ringbuf_completion(rs
, str
);
3958 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3967 readline_set_completion_index(rs
, len
);
3968 peripheral_device_del_completion(rs
, str
, len
);
3971 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3973 ObjectPropertyInfoList
*list
, *start
;
3980 readline_set_completion_index(rs
, len
);
3982 start
= list
= qmp_qom_list("/objects", NULL
);
3984 ObjectPropertyInfo
*info
= list
->value
;
3986 if (!strncmp(info
->type
, "child<", 5)
3987 && !strncmp(info
->name
, str
, len
)) {
3988 readline_add_completion(rs
, info
->name
);
3992 qapi_free_ObjectPropertyInfoList(start
);
3995 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4004 sep
= strrchr(str
, '-');
4009 readline_set_completion_index(rs
, len
);
4010 for (i
= 0; i
< Q_KEY_CODE_MAX
; i
++) {
4011 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
4012 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
4017 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4022 readline_set_completion_index(rs
, len
);
4024 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4026 count
= qemu_find_net_clients_except(NULL
, ncs
,
4027 NET_CLIENT_OPTIONS_KIND_NONE
,
4029 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4030 const char *name
= ncs
[i
]->name
;
4031 if (!strncmp(str
, name
, len
)) {
4032 readline_add_completion(rs
, name
);
4035 } else if (nb_args
== 3) {
4036 add_completion_option(rs
, str
, "on");
4037 add_completion_option(rs
, str
, "off");
4041 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4044 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4051 readline_set_completion_index(rs
, len
);
4052 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
4054 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4056 const char *name
= ncs
[i
]->name
;
4057 if (strncmp(str
, name
, len
)) {
4060 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
4062 readline_add_completion(rs
, name
);
4067 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4072 readline_set_completion_index(rs
, len
);
4075 for (id
= 0; id
< trace_event_count(); id
++) {
4076 const char *event_name
= trace_event_get_name(trace_event_id(id
));
4077 if (!strncmp(str
, event_name
, len
)) {
4078 readline_add_completion(rs
, event_name
);
4081 } else if (nb_args
== 3) {
4082 add_completion_option(rs
, str
, "on");
4083 add_completion_option(rs
, str
, "off");
4087 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4094 readline_set_completion_index(rs
, strlen(str
));
4095 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
4096 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
4100 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
4106 readline_set_completion_index(rs
, len
);
4109 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
4110 const char *name
= MigrationCapability_lookup
[i
];
4111 if (!strncmp(str
, name
, len
)) {
4112 readline_add_completion(rs
, name
);
4115 } else if (nb_args
== 3) {
4116 add_completion_option(rs
, str
, "on");
4117 add_completion_option(rs
, str
, "off");
4121 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
4127 readline_set_completion_index(rs
, len
);
4130 for (i
= 0; i
< MIGRATION_PARAMETER_MAX
; i
++) {
4131 const char *name
= MigrationParameter_lookup
[i
];
4132 if (!strncmp(str
, name
, len
)) {
4133 readline_add_completion(rs
, name
);
4139 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4147 readline_set_completion_index(rs
, len
);
4148 for (i
= 0; host_net_devices
[i
]; i
++) {
4149 if (!strncmp(host_net_devices
[i
], str
, len
)) {
4150 readline_add_completion(rs
, host_net_devices
[i
]);
4155 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4157 NetClientState
*ncs
[MAX_QUEUE_NUM
];
4161 readline_set_completion_index(rs
, len
);
4163 count
= qemu_find_net_clients_except(NULL
, ncs
,
4164 NET_CLIENT_OPTIONS_KIND_NONE
,
4166 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4170 if (net_hub_id_for_client(ncs
[i
], &id
)) {
4173 snprintf(name
, sizeof(name
), "%d", id
);
4174 if (!strncmp(str
, name
, len
)) {
4175 readline_add_completion(rs
, name
);
4179 } else if (nb_args
== 3) {
4180 count
= qemu_find_net_clients_except(NULL
, ncs
,
4181 NET_CLIENT_OPTIONS_KIND_NIC
,
4183 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
4187 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
4188 net_hub_id_for_client(ncs
[i
], &id
)) {
4191 name
= ncs
[i
]->name
;
4192 if (!strncmp(str
, name
, len
)) {
4193 readline_add_completion(rs
, name
);
4200 static void vm_completion(ReadLineState
*rs
, const char *str
)
4203 BlockDriverState
*bs
= NULL
;
4206 readline_set_completion_index(rs
, len
);
4207 while ((bs
= bdrv_next(bs
))) {
4208 SnapshotInfoList
*snapshots
, *snapshot
;
4210 if (!bdrv_can_snapshot(bs
)) {
4213 if (bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
)) {
4216 snapshot
= snapshots
;
4218 char *completion
= snapshot
->value
->name
;
4219 if (!strncmp(str
, completion
, len
)) {
4220 readline_add_completion(rs
, completion
);
4222 completion
= snapshot
->value
->id
;
4223 if (!strncmp(str
, completion
, len
)) {
4224 readline_add_completion(rs
, completion
);
4226 snapshot
= snapshot
->next
;
4228 qapi_free_SnapshotInfoList(snapshots
);
4233 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4236 vm_completion(rs
, str
);
4240 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4243 vm_completion(rs
, str
);
4247 static void monitor_find_completion_by_table(Monitor
*mon
,
4248 const mon_cmd_t
*cmd_table
,
4252 const char *cmdname
;
4254 const char *ptype
, *str
, *name
;
4255 const mon_cmd_t
*cmd
;
4256 BlockDriverState
*bs
;
4259 /* command completion */
4264 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4265 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4266 cmd_completion(mon
, cmdname
, cmd
->name
);
4269 /* find the command */
4270 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4271 if (compare_cmd(args
[0], cmd
->name
)) {
4279 if (cmd
->sub_table
) {
4280 /* do the job again */
4281 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4282 &args
[1], nb_args
- 1);
4285 if (cmd
->command_completion
) {
4286 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4290 ptype
= next_arg_type(cmd
->args_type
);
4291 for(i
= 0; i
< nb_args
- 2; i
++) {
4292 if (*ptype
!= '\0') {
4293 ptype
= next_arg_type(ptype
);
4294 while (*ptype
== '?')
4295 ptype
= next_arg_type(ptype
);
4298 str
= args
[nb_args
- 1];
4299 while (*ptype
== '-' && ptype
[1] != '\0') {
4300 ptype
= next_arg_type(ptype
);
4304 /* file completion */
4305 readline_set_completion_index(mon
->rs
, strlen(str
));
4306 file_completion(mon
, str
);
4309 /* block device name completion */
4310 readline_set_completion_index(mon
->rs
, strlen(str
));
4311 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
4312 name
= bdrv_get_device_name(bs
);
4313 if (str
[0] == '\0' ||
4314 !strncmp(name
, str
, strlen(str
))) {
4315 readline_add_completion(mon
->rs
, name
);
4321 if (!strcmp(cmd
->name
, "help|?")) {
4322 monitor_find_completion_by_table(mon
, cmd_table
,
4323 &args
[1], nb_args
- 1);
4332 static void monitor_find_completion(void *opaque
,
4333 const char *cmdline
)
4335 Monitor
*mon
= opaque
;
4336 char *args
[MAX_ARGS
];
4339 /* 1. parse the cmdline */
4340 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4344 /* if the line ends with a space, it means we want to complete the
4346 len
= strlen(cmdline
);
4347 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4348 if (nb_args
>= MAX_ARGS
) {
4351 args
[nb_args
++] = g_strdup("");
4354 /* 2. auto complete according to args */
4355 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
4358 free_cmdline_args(args
, nb_args
);
4361 static int monitor_can_read(void *opaque
)
4363 Monitor
*mon
= opaque
;
4365 return (mon
->suspend_cnt
== 0) ? 1 : 0;
4368 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
4371 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
4373 if (is_cap
&& mon
->qmp
.in_command_mode
) {
4374 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4375 "Capabilities negotiation is already complete, command "
4376 "'%s' ignored", cmd
->name
);
4379 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
4380 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4381 "Expecting capabilities negotiation with "
4382 "'qmp_capabilities' before command '%s'", cmd
->name
);
4389 * Argument validation rules:
4391 * 1. The argument must exist in cmd_args qdict
4392 * 2. The argument type must be the expected one
4394 * Special case: If the argument doesn't exist in cmd_args and
4395 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4396 * checking is skipped for it.
4398 static void check_client_args_type(const QDict
*client_args
,
4399 const QDict
*cmd_args
, int flags
,
4402 const QDictEntry
*ent
;
4404 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
4407 const QObject
*client_arg
= qdict_entry_value(ent
);
4408 const char *client_arg_name
= qdict_entry_key(ent
);
4410 obj
= qdict_get(cmd_args
, client_arg_name
);
4412 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
4413 /* handler accepts unknowns */
4416 /* client arg doesn't exist */
4417 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
4421 arg_type
= qobject_to_qstring(obj
);
4422 assert(arg_type
!= NULL
);
4424 /* check if argument's type is correct */
4425 switch (qstring_get_str(arg_type
)[0]) {
4429 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
4430 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4431 client_arg_name
, "string");
4439 if (qobject_type(client_arg
) != QTYPE_QINT
) {
4440 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4441 client_arg_name
, "int");
4446 if (qobject_type(client_arg
) != QTYPE_QINT
&&
4447 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
4448 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4449 client_arg_name
, "number");
4455 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
4456 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
4457 client_arg_name
, "bool");
4462 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
4465 /* Any QObject can be passed. */
4470 * These types are not supported by QMP and thus are not
4471 * handled here. Fall through.
4480 * - Check if the client has passed all mandatory args
4481 * - Set special flags for argument validation
4483 static void check_mandatory_args(const QDict
*cmd_args
,
4484 const QDict
*client_args
, int *flags
,
4487 const QDictEntry
*ent
;
4489 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
4490 const char *cmd_arg_name
= qdict_entry_key(ent
);
4491 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
4492 assert(type
!= NULL
);
4494 if (qstring_get_str(type
)[0] == 'O') {
4495 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
4496 *flags
|= QMP_ACCEPT_UNKNOWNS
;
4497 } else if (qstring_get_str(type
)[0] != '-' &&
4498 qstring_get_str(type
)[1] != '?' &&
4499 !qdict_haskey(client_args
, cmd_arg_name
)) {
4500 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
4506 static QDict
*qdict_from_args_type(const char *args_type
)
4510 QString
*key
, *type
, *cur_qs
;
4512 assert(args_type
!= NULL
);
4514 qdict
= qdict_new();
4516 if (args_type
== NULL
|| args_type
[0] == '\0') {
4517 /* no args, empty qdict */
4521 key
= qstring_new();
4522 type
= qstring_new();
4527 switch (args_type
[i
]) {
4530 qdict_put(qdict
, qstring_get_str(key
), type
);
4532 if (args_type
[i
] == '\0') {
4535 type
= qstring_new(); /* qdict has ref */
4536 cur_qs
= key
= qstring_new();
4542 qstring_append_chr(cur_qs
, args_type
[i
]);
4552 * Client argument checking rules:
4554 * 1. Client must provide all mandatory arguments
4555 * 2. Each argument provided by the client must be expected
4556 * 3. Each argument provided by the client must have the type expected
4559 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
4566 cmd_args
= qdict_from_args_type(cmd
->args_type
);
4569 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
4574 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
4577 error_propagate(errp
, err
);
4582 * Input object checking rules
4584 * 1. Input object must be a dict
4585 * 2. The "execute" key must exist
4586 * 3. The "execute" key must be a string
4587 * 4. If the "arguments" key exists, it must be a dict
4588 * 5. If the "id" key exists, it can be anything (ie. json-value)
4589 * 6. Any argument not listed above is considered invalid
4591 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
4593 const QDictEntry
*ent
;
4594 int has_exec_key
= 0;
4597 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
4598 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
4602 input_dict
= qobject_to_qdict(input_obj
);
4604 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
4605 const char *arg_name
= qdict_entry_key(ent
);
4606 const QObject
*arg_obj
= qdict_entry_value(ent
);
4608 if (!strcmp(arg_name
, "execute")) {
4609 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
4610 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
4611 "execute", "string");
4615 } else if (!strcmp(arg_name
, "arguments")) {
4616 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
4617 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
4618 "arguments", "object");
4621 } else if (!strcmp(arg_name
, "id")) {
4622 /* Any string is acceptable as "id", so nothing to check */
4624 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
4629 if (!has_exec_key
) {
4630 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
4637 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
4639 Error
*local_err
= NULL
;
4640 QObject
*obj
, *data
;
4641 QDict
*input
, *args
;
4642 const mon_cmd_t
*cmd
;
4643 const char *cmd_name
;
4644 Monitor
*mon
= cur_mon
;
4646 args
= input
= NULL
;
4649 obj
= json_parser_parse(tokens
, NULL
);
4651 // FIXME: should be triggered in json_parser_parse()
4652 error_setg(&local_err
, QERR_JSON_PARSING
);
4656 input
= qmp_check_input_obj(obj
, &local_err
);
4658 qobject_decref(obj
);
4662 mon
->qmp
.id
= qdict_get(input
, "id");
4663 qobject_incref(mon
->qmp
.id
);
4665 cmd_name
= qdict_get_str(input
, "execute");
4666 trace_handle_qmp_command(mon
, cmd_name
);
4667 cmd
= qmp_find_cmd(cmd_name
);
4669 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4670 "The command %s has not been found", cmd_name
);
4673 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
4677 obj
= qdict_get(input
, "arguments");
4681 args
= qobject_to_qdict(obj
);
4685 qmp_check_client_args(cmd
, args
, &local_err
);
4690 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
4693 monitor_protocol_emitter(mon
, data
, local_err
);
4694 qobject_decref(data
);
4699 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4701 Monitor
*old_mon
= cur_mon
;
4705 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
4710 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4712 Monitor
*old_mon
= cur_mon
;
4718 for (i
= 0; i
< size
; i
++)
4719 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4721 if (size
== 0 || buf
[size
- 1] != 0)
4722 monitor_printf(cur_mon
, "corrupted command\n");
4724 handle_hmp_command(cur_mon
, (char *)buf
);
4730 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4731 void *readline_opaque
)
4733 Monitor
*mon
= opaque
;
4735 monitor_suspend(mon
);
4736 handle_hmp_command(mon
, cmdline
);
4737 monitor_resume(mon
);
4740 int monitor_suspend(Monitor
*mon
)
4748 void monitor_resume(Monitor
*mon
)
4752 if (--mon
->suspend_cnt
== 0)
4753 readline_show_prompt(mon
->rs
);
4756 static QObject
*get_qmp_greeting(void)
4758 QObject
*ver
= NULL
;
4760 qmp_marshal_input_query_version(NULL
, &ver
, NULL
);
4761 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4764 static void monitor_qmp_event(void *opaque
, int event
)
4767 Monitor
*mon
= opaque
;
4770 case CHR_EVENT_OPENED
:
4771 mon
->qmp
.in_command_mode
= false;
4772 data
= get_qmp_greeting();
4773 monitor_json_emitter(mon
, data
);
4774 qobject_decref(data
);
4777 case CHR_EVENT_CLOSED
:
4778 json_message_parser_destroy(&mon
->qmp
.parser
);
4779 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4781 monitor_fdsets_cleanup();
4786 static void monitor_event(void *opaque
, int event
)
4788 Monitor
*mon
= opaque
;
4791 case CHR_EVENT_MUX_IN
:
4792 qemu_mutex_lock(&mon
->out_lock
);
4794 qemu_mutex_unlock(&mon
->out_lock
);
4795 if (mon
->reset_seen
) {
4796 readline_restart(mon
->rs
);
4797 monitor_resume(mon
);
4800 mon
->suspend_cnt
= 0;
4804 case CHR_EVENT_MUX_OUT
:
4805 if (mon
->reset_seen
) {
4806 if (mon
->suspend_cnt
== 0) {
4807 monitor_printf(mon
, "\n");
4810 monitor_suspend(mon
);
4814 qemu_mutex_lock(&mon
->out_lock
);
4816 qemu_mutex_unlock(&mon
->out_lock
);
4819 case CHR_EVENT_OPENED
:
4820 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4821 "information\n", QEMU_VERSION
);
4822 if (!mon
->mux_out
) {
4823 readline_restart(mon
->rs
);
4824 readline_show_prompt(mon
->rs
);
4826 mon
->reset_seen
= 1;
4830 case CHR_EVENT_CLOSED
:
4832 monitor_fdsets_cleanup();
4838 compare_mon_cmd(const void *a
, const void *b
)
4840 return strcmp(((const mon_cmd_t
*)a
)->name
,
4841 ((const mon_cmd_t
*)b
)->name
);
4844 static void sortcmdlist(void)
4847 int elem_size
= sizeof(mon_cmd_t
);
4849 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4850 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4852 array_num
= sizeof(info_cmds
)/elem_size
-1;
4853 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4865 /* These functions just adapt the readline interface in a typesafe way. We
4866 * could cast function pointers but that discards compiler checks.
4868 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4869 const char *fmt
, ...)
4873 monitor_vprintf(opaque
, fmt
, ap
);
4877 static void monitor_readline_flush(void *opaque
)
4879 monitor_flush(opaque
);
4882 static void __attribute__((constructor
)) monitor_lock_init(void)
4884 qemu_mutex_init(&monitor_lock
);
4887 void monitor_init(CharDriverState
*chr
, int flags
)
4889 static int is_first_init
= 1;
4892 if (is_first_init
) {
4893 monitor_qapi_event_init();
4898 mon
= g_malloc(sizeof(*mon
));
4899 monitor_data_init(mon
);
4903 if (flags
& MONITOR_USE_READLINE
) {
4904 mon
->rs
= readline_init(monitor_readline_printf
,
4905 monitor_readline_flush
,
4907 monitor_find_completion
);
4908 monitor_read_command(mon
, 0);
4911 if (monitor_is_qmp(mon
)) {
4912 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4913 monitor_qmp_event
, mon
);
4914 qemu_chr_fe_set_echo(chr
, true);
4915 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4917 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4918 monitor_event
, mon
);
4921 qemu_mutex_lock(&monitor_lock
);
4922 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4923 qemu_mutex_unlock(&monitor_lock
);
4926 static void bdrv_password_cb(void *opaque
, const char *password
,
4927 void *readline_opaque
)
4929 Monitor
*mon
= opaque
;
4930 BlockDriverState
*bs
= readline_opaque
;
4932 Error
*local_err
= NULL
;
4934 bdrv_add_key(bs
, password
, &local_err
);
4936 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
4937 error_free(local_err
);
4940 if (mon
->password_completion_cb
)
4941 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4943 monitor_read_command(mon
, 1);
4946 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4947 BlockCompletionFunc
*completion_cb
,
4952 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4953 bdrv_get_encrypted_filename(bs
));
4955 mon
->password_completion_cb
= completion_cb
;
4956 mon
->password_opaque
= opaque
;
4958 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4960 if (err
&& completion_cb
)
4961 completion_cb(opaque
, err
);
4966 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4967 BlockCompletionFunc
*completion_cb
,
4973 blk
= blk_by_name(device
);
4975 monitor_printf(mon
, "Device not found %s\n", device
);
4979 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4982 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4985 if (completion_cb
) {
4986 completion_cb(opaque
, 0);
4991 QemuOptsList qemu_mon_opts
= {
4993 .implied_opt_name
= "chardev",
4994 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4998 .type
= QEMU_OPT_STRING
,
5001 .type
= QEMU_OPT_STRING
,
5004 .type
= QEMU_OPT_BOOL
,
5007 .type
= QEMU_OPT_BOOL
,
5009 { /* end of list */ }
5014 void qmp_rtc_reset_reinjection(Error
**errp
)
5016 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
5020 #ifndef TARGET_S390X
5021 void qmp_dump_skeys(const char *filename
, Error
**errp
)
5023 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");