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 #include "monitor/hmp-target.h"
67 #ifdef CONFIG_TRACE_SIMPLE
68 #include "trace/simple.h"
70 #include "exec/memory.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 "qmp-introspect.h"
78 #include "sysemu/block-backend.h"
80 /* for hmp_info_irq/pic */
81 #if defined(TARGET_SPARC)
82 #include "hw/sparc/sun4m.h"
84 #include "hw/lm32/lm32_pic.h"
86 #if defined(TARGET_S390X)
87 #include "hw/s390x/storage-keys.h"
94 * 'B' block device name
95 * 's' string (accept optional quote)
96 * 'S' it just appends the rest of the string (accept optional quote)
97 * 'O' option string of the form NAME=VALUE,...
98 * parsed according to QemuOptsList given by its name
99 * Example: 'device:O' uses qemu_device_opts.
100 * Restriction: only lists with empty desc are supported
101 * TODO lift the restriction
103 * 'l' target long (32 or 64 bit)
104 * 'M' Non-negative target long (32 or 64 bit), in user mode the
105 * value is multiplied by 2^20 (think Mebibyte)
106 * 'o' octets (aka bytes)
107 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
108 * K, k suffix, which multiplies the value by 2^60 for suffixes E
109 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
110 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
112 * user mode accepts an optional ms, us, ns suffix,
113 * which divides the value by 1e3, 1e6, 1e9, respectively
114 * '/' optional gdb-like print format (like "/10x")
116 * '?' optional type (for all types, except '/')
117 * '.' other form of optional type (for 'i' and 'l')
119 * user mode accepts "on" or "off"
120 * '-' optional parameter (eg. '-f')
124 typedef struct mon_cmd_t
{
126 const char *args_type
;
130 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
131 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
133 /* @sub_table is a list of 2nd level of commands. If it do not exist,
134 * mhandler should be used. If it exist, sub_table[?].mhandler should be
135 * used, and mhandler of 1st level plays the role of help function.
137 struct mon_cmd_t
*sub_table
;
138 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
141 /* file descriptors passed via SCM_RIGHTS */
142 typedef struct mon_fd_t mon_fd_t
;
146 QLIST_ENTRY(mon_fd_t
) next
;
149 /* file descriptor associated with a file descriptor set */
150 typedef struct MonFdsetFd MonFdsetFd
;
155 QLIST_ENTRY(MonFdsetFd
) next
;
158 /* file descriptor set containing fds passed via SCM_RIGHTS */
159 typedef struct MonFdset MonFdset
;
162 QLIST_HEAD(, MonFdsetFd
) fds
;
163 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
164 QLIST_ENTRY(MonFdset
) next
;
169 JSONMessageParser parser
;
171 * When a client connects, we're in capabilities negotiation mode.
172 * When command qmp_capabilities succeeds, we go into command
175 bool in_command_mode
; /* are we in command mode? */
179 * To prevent flooding clients, events can be throttled. The
180 * throttling is calculated globally, rather than per-Monitor
183 typedef struct MonitorQAPIEventState
{
184 QAPIEvent event
; /* Event being tracked */
185 int64_t rate
; /* Minimum time (in ns) between two events */
186 int64_t last
; /* QEMU_CLOCK_REALTIME value at last emission */
187 QEMUTimer
*timer
; /* Timer for handling delayed events */
188 QObject
*data
; /* Event pending delayed dispatch */
189 } MonitorQAPIEventState
;
192 CharDriverState
*chr
;
202 /* Read under either BQL or out_lock, written with BQL+out_lock. */
208 BlockCompletionFunc
*password_completion_cb
;
209 void *password_opaque
;
210 mon_cmd_t
*cmd_table
;
211 QLIST_HEAD(,mon_fd_t
) fds
;
212 QLIST_ENTRY(Monitor
) entry
;
215 /* QMP checker flags */
216 #define QMP_ACCEPT_UNKNOWNS 1
218 /* Protects mon_list, monitor_event_state. */
219 static QemuMutex monitor_lock
;
221 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
222 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
223 static int mon_refcount
;
225 static mon_cmd_t mon_cmds
[];
226 static mon_cmd_t info_cmds
[];
228 static const mon_cmd_t qmp_cmds
[];
232 static void monitor_command_cb(void *opaque
, const char *cmdline
,
233 void *readline_opaque
);
236 * Is @mon a QMP monitor?
238 static inline bool monitor_is_qmp(const Monitor
*mon
)
240 return (mon
->flags
& MONITOR_USE_CONTROL
);
244 * Is the current monitor, if any, a QMP monitor?
246 bool monitor_cur_is_qmp(void)
248 return cur_mon
&& monitor_is_qmp(cur_mon
);
251 void monitor_read_command(Monitor
*mon
, int show_prompt
)
256 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
258 readline_show_prompt(mon
->rs
);
261 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
265 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
266 /* prompt is printed on return from the command handler */
269 monitor_printf(mon
, "terminal does not support password prompting\n");
274 static void monitor_flush_locked(Monitor
*mon
);
276 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
279 Monitor
*mon
= opaque
;
281 qemu_mutex_lock(&mon
->out_lock
);
283 monitor_flush_locked(mon
);
284 qemu_mutex_unlock(&mon
->out_lock
);
288 /* Called with mon->out_lock held. */
289 static void monitor_flush_locked(Monitor
*mon
)
295 if (mon
->skip_flush
) {
299 buf
= qstring_get_str(mon
->outbuf
);
300 len
= qstring_get_length(mon
->outbuf
);
302 if (len
&& !mon
->mux_out
) {
303 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
304 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
305 /* all flushed or error */
306 QDECREF(mon
->outbuf
);
307 mon
->outbuf
= qstring_new();
312 QString
*tmp
= qstring_from_str(buf
+ rc
);
313 QDECREF(mon
->outbuf
);
316 if (mon
->out_watch
== 0) {
317 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
318 monitor_unblocked
, mon
);
323 void monitor_flush(Monitor
*mon
)
325 qemu_mutex_lock(&mon
->out_lock
);
326 monitor_flush_locked(mon
);
327 qemu_mutex_unlock(&mon
->out_lock
);
330 /* flush at every end of line */
331 static void monitor_puts(Monitor
*mon
, const char *str
)
335 qemu_mutex_lock(&mon
->out_lock
);
341 qstring_append_chr(mon
->outbuf
, '\r');
343 qstring_append_chr(mon
->outbuf
, c
);
345 monitor_flush_locked(mon
);
348 qemu_mutex_unlock(&mon
->out_lock
);
351 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
358 if (monitor_is_qmp(mon
)) {
362 buf
= g_strdup_vprintf(fmt
, ap
);
363 monitor_puts(mon
, buf
);
367 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
371 monitor_vprintf(mon
, fmt
, ap
);
375 int monitor_fprintf(FILE *stream
, 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
;
932 * Minor hack: generated marshalling suppressed for this command
933 * ('gen': false in the schema) so we can parse the JSON string
934 * directly into QObject instead of first parsing it with
935 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
936 * to QObject with generated output marshallers, every time. Instead,
937 * we do it in test-qmp-input-visitor.c, just to make sure
938 * qapi-introspect.py's output actually conforms to the schema.
940 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
943 *ret_data
= qobject_from_json(qmp_schema_json
);
946 /* set the current CPU defined by the user */
947 int monitor_set_cpu(int cpu_index
)
951 cpu
= qemu_get_cpu(cpu_index
);
955 cur_mon
->mon_cpu
= cpu
;
959 CPUState
*mon_get_cpu(void)
961 if (!cur_mon
->mon_cpu
) {
964 cpu_synchronize_state(cur_mon
->mon_cpu
);
965 return cur_mon
->mon_cpu
;
968 CPUArchState
*mon_get_cpu_env(void)
970 return mon_get_cpu()->env_ptr
;
973 int monitor_get_cpu_index(void)
975 return mon_get_cpu()->cpu_index
;
978 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
980 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
983 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
985 dump_exec_info((FILE *)mon
, monitor_fprintf
);
986 dump_drift_info((FILE *)mon
, monitor_fprintf
);
989 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
991 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
994 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1003 str
= readline_get_history(mon
->rs
, i
);
1006 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1011 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1013 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1016 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1018 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1019 TraceEventInfoList
*elem
;
1021 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1022 monitor_printf(mon
, "%s : state %u\n",
1024 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1026 qapi_free_TraceEventInfoList(events
);
1029 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1030 bool has_port
, int64_t port
,
1031 bool has_tls_port
, int64_t tls_port
,
1032 bool has_cert_subject
, const char *cert_subject
,
1035 if (strcmp(protocol
, "spice") == 0) {
1036 if (!qemu_using_spice(errp
)) {
1040 if (!has_port
&& !has_tls_port
) {
1041 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1045 if (qemu_spice_migrate_info(hostname
,
1046 has_port
? port
: -1,
1047 has_tls_port
? tls_port
: -1,
1049 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1055 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1058 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1060 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1063 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1066 const char *items
= qdict_get_str(qdict
, "items");
1068 if (!strcmp(items
, "none")) {
1071 mask
= qemu_str_to_log_mask(items
);
1073 help_cmd(mon
, "log");
1080 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1082 const char *option
= qdict_get_try_str(qdict
, "option");
1083 if (!option
|| !strcmp(option
, "on")) {
1085 } else if (!strcmp(option
, "off")) {
1088 monitor_printf(mon
, "unexpected option %s\n", option
);
1092 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1094 const char *device
= qdict_get_try_str(qdict
, "device");
1096 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1097 if (gdbserver_start(device
) < 0) {
1098 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1100 } else if (strcmp(device
, "none") == 0) {
1101 monitor_printf(mon
, "Disabled gdbserver\n");
1103 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1108 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1110 const char *action
= qdict_get_str(qdict
, "action");
1111 if (select_watchdog_action(action
) == -1) {
1112 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1116 static void monitor_printc(Monitor
*mon
, int c
)
1118 monitor_printf(mon
, "'");
1121 monitor_printf(mon
, "\\'");
1124 monitor_printf(mon
, "\\\\");
1127 monitor_printf(mon
, "\\n");
1130 monitor_printf(mon
, "\\r");
1133 if (c
>= 32 && c
<= 126) {
1134 monitor_printf(mon
, "%c", c
);
1136 monitor_printf(mon
, "\\x%02x", c
);
1140 monitor_printf(mon
, "'");
1143 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1144 hwaddr addr
, int is_physical
)
1146 int l
, line_size
, i
, max_digits
, len
;
1150 if (format
== 'i') {
1153 CPUArchState
*env
= mon_get_cpu_env();
1156 } else if (wsize
== 4) {
1159 /* as default we use the current CS size */
1162 #ifdef TARGET_X86_64
1163 if ((env
->efer
& MSR_EFER_LMA
) &&
1164 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1168 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1174 CPUArchState
*env
= mon_get_cpu_env();
1175 flags
= msr_le
<< 16;
1176 flags
|= env
->bfd_mach
;
1178 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1182 len
= wsize
* count
;
1191 max_digits
= (wsize
* 8 + 2) / 3;
1195 max_digits
= (wsize
* 8) / 4;
1199 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1208 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1210 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1215 cpu_physical_memory_read(addr
, buf
, l
);
1217 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1218 monitor_printf(mon
, " Cannot access memory\n");
1227 v
= ldub_p(buf
+ i
);
1230 v
= lduw_p(buf
+ i
);
1233 v
= (uint32_t)ldl_p(buf
+ i
);
1239 monitor_printf(mon
, " ");
1242 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1245 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1248 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1251 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1254 monitor_printc(mon
, v
);
1259 monitor_printf(mon
, "\n");
1265 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1267 int count
= qdict_get_int(qdict
, "count");
1268 int format
= qdict_get_int(qdict
, "format");
1269 int size
= qdict_get_int(qdict
, "size");
1270 target_long addr
= qdict_get_int(qdict
, "addr");
1272 memory_dump(mon
, count
, format
, size
, addr
, 0);
1275 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1277 int count
= qdict_get_int(qdict
, "count");
1278 int format
= qdict_get_int(qdict
, "format");
1279 int size
= qdict_get_int(qdict
, "size");
1280 hwaddr addr
= qdict_get_int(qdict
, "addr");
1282 memory_dump(mon
, count
, format
, size
, addr
, 1);
1285 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1287 int format
= qdict_get_int(qdict
, "format");
1288 hwaddr val
= qdict_get_int(qdict
, "val");
1292 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1295 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1298 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1302 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1305 monitor_printc(mon
, val
);
1308 monitor_printf(mon
, "\n");
1311 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1315 uint32_t start
= qdict_get_int(qdict
, "start");
1316 uint32_t size
= qdict_get_int(qdict
, "size");
1319 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1320 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1321 MEMTXATTRS_UNSPECIFIED
, NULL
);
1322 /* BSD sum algorithm ('sum' Unix command) */
1323 sum
= (sum
>> 1) | (sum
<< 15);
1326 monitor_printf(mon
, "%05d\n", sum
);
1329 static int mouse_button_state
;
1331 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1333 int dx
, dy
, dz
, button
;
1334 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1335 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1336 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1338 dx
= strtol(dx_str
, NULL
, 0);
1339 dy
= strtol(dy_str
, NULL
, 0);
1340 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1341 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1344 dz
= strtol(dz_str
, NULL
, 0);
1346 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1347 qemu_input_queue_btn(NULL
, button
, true);
1348 qemu_input_event_sync();
1349 qemu_input_queue_btn(NULL
, button
, false);
1352 qemu_input_event_sync();
1355 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1357 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
1358 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1359 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1360 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1362 int button_state
= qdict_get_int(qdict
, "button_state");
1364 if (mouse_button_state
== button_state
) {
1367 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1368 qemu_input_event_sync();
1369 mouse_button_state
= button_state
;
1372 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1374 int size
= qdict_get_int(qdict
, "size");
1375 int addr
= qdict_get_int(qdict
, "addr");
1376 int has_index
= qdict_haskey(qdict
, "index");
1381 int index
= qdict_get_int(qdict
, "index");
1382 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1390 val
= cpu_inb(addr
);
1394 val
= cpu_inw(addr
);
1398 val
= cpu_inl(addr
);
1402 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1403 suffix
, addr
, size
* 2, val
);
1406 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1408 int size
= qdict_get_int(qdict
, "size");
1409 int addr
= qdict_get_int(qdict
, "addr");
1410 int val
= qdict_get_int(qdict
, "val");
1412 addr
&= IOPORTS_MASK
;
1417 cpu_outb(addr
, val
);
1420 cpu_outw(addr
, val
);
1423 cpu_outl(addr
, val
);
1428 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1430 Error
*local_err
= NULL
;
1431 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1433 qemu_boot_set(bootdevice
, &local_err
);
1435 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1436 error_free(local_err
);
1438 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1442 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1444 mtree_info((fprintf_function
)monitor_printf
, mon
);
1447 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1453 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1454 query_numa_node_mem(node_mem
);
1455 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1456 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1457 monitor_printf(mon
, "node %d cpus:", i
);
1459 if (cpu
->numa_node
== i
) {
1460 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1463 monitor_printf(mon
, "\n");
1464 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1470 #ifdef CONFIG_PROFILER
1475 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1477 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1478 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1479 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1480 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1485 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1487 monitor_printf(mon
, "Internal profiler not compiled\n");
1491 /* Capture support */
1492 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1494 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1499 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1500 monitor_printf(mon
, "[%d]: ", i
);
1501 s
->ops
.info (s
->opaque
);
1505 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1508 int n
= qdict_get_int(qdict
, "n");
1511 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1513 s
->ops
.destroy (s
->opaque
);
1514 QLIST_REMOVE (s
, entries
);
1521 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1523 const char *path
= qdict_get_str(qdict
, "path");
1524 int has_freq
= qdict_haskey(qdict
, "freq");
1525 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1526 int has_bits
= qdict_haskey(qdict
, "bits");
1527 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1528 int has_channels
= qdict_haskey(qdict
, "nchannels");
1529 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1532 s
= g_malloc0 (sizeof (*s
));
1534 freq
= has_freq
? freq
: 44100;
1535 bits
= has_bits
? bits
: 16;
1536 nchannels
= has_channels
? nchannels
: 2;
1538 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1539 monitor_printf(mon
, "Failed to add wave capture\n");
1543 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1546 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1548 qemu_acl
*acl
= qemu_acl_find(name
);
1551 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1556 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1558 const char *aclname
= qdict_get_str(qdict
, "aclname");
1559 qemu_acl
*acl
= find_acl(mon
, aclname
);
1560 qemu_acl_entry
*entry
;
1564 monitor_printf(mon
, "policy: %s\n",
1565 acl
->defaultDeny
? "deny" : "allow");
1566 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1568 monitor_printf(mon
, "%d: %s %s\n", i
,
1569 entry
->deny
? "deny" : "allow", entry
->match
);
1574 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1576 const char *aclname
= qdict_get_str(qdict
, "aclname");
1577 qemu_acl
*acl
= find_acl(mon
, aclname
);
1580 qemu_acl_reset(acl
);
1581 monitor_printf(mon
, "acl: removed all rules\n");
1585 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1587 const char *aclname
= qdict_get_str(qdict
, "aclname");
1588 const char *policy
= qdict_get_str(qdict
, "policy");
1589 qemu_acl
*acl
= find_acl(mon
, aclname
);
1592 if (strcmp(policy
, "allow") == 0) {
1593 acl
->defaultDeny
= 0;
1594 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1595 } else if (strcmp(policy
, "deny") == 0) {
1596 acl
->defaultDeny
= 1;
1597 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1599 monitor_printf(mon
, "acl: unknown policy '%s', "
1600 "expected 'deny' or 'allow'\n", policy
);
1605 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1607 const char *aclname
= qdict_get_str(qdict
, "aclname");
1608 const char *match
= qdict_get_str(qdict
, "match");
1609 const char *policy
= qdict_get_str(qdict
, "policy");
1610 int has_index
= qdict_haskey(qdict
, "index");
1611 int index
= qdict_get_try_int(qdict
, "index", -1);
1612 qemu_acl
*acl
= find_acl(mon
, aclname
);
1616 if (strcmp(policy
, "allow") == 0) {
1618 } else if (strcmp(policy
, "deny") == 0) {
1621 monitor_printf(mon
, "acl: unknown policy '%s', "
1622 "expected 'deny' or 'allow'\n", policy
);
1626 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1628 ret
= qemu_acl_append(acl
, deny
, match
);
1630 monitor_printf(mon
, "acl: unable to add acl entry\n");
1632 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1636 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1638 const char *aclname
= qdict_get_str(qdict
, "aclname");
1639 const char *match
= qdict_get_str(qdict
, "match");
1640 qemu_acl
*acl
= find_acl(mon
, aclname
);
1644 ret
= qemu_acl_remove(acl
, match
);
1646 monitor_printf(mon
, "acl: no matching acl entry\n");
1648 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1652 void qmp_getfd(const char *fdname
, Error
**errp
)
1657 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1659 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1663 if (qemu_isdigit(fdname
[0])) {
1665 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1666 "a name not starting with a digit");
1670 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1671 if (strcmp(monfd
->name
, fdname
) != 0) {
1680 monfd
= g_malloc0(sizeof(mon_fd_t
));
1681 monfd
->name
= g_strdup(fdname
);
1684 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1687 void qmp_closefd(const char *fdname
, Error
**errp
)
1691 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1692 if (strcmp(monfd
->name
, fdname
) != 0) {
1696 QLIST_REMOVE(monfd
, next
);
1698 g_free(monfd
->name
);
1703 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1706 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1708 int saved_vm_running
= runstate_is_running();
1709 const char *name
= qdict_get_str(qdict
, "name");
1711 vm_stop(RUN_STATE_RESTORE_VM
);
1713 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1718 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1722 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1725 if (strcmp(monfd
->name
, fdname
) != 0) {
1731 /* caller takes ownership of fd */
1732 QLIST_REMOVE(monfd
, next
);
1733 g_free(monfd
->name
);
1739 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1743 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1745 MonFdsetFd
*mon_fdset_fd
;
1746 MonFdsetFd
*mon_fdset_fd_next
;
1748 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1749 if ((mon_fdset_fd
->removed
||
1750 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1751 runstate_is_running()) {
1752 close(mon_fdset_fd
->fd
);
1753 g_free(mon_fdset_fd
->opaque
);
1754 QLIST_REMOVE(mon_fdset_fd
, next
);
1755 g_free(mon_fdset_fd
);
1759 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1760 QLIST_REMOVE(mon_fdset
, next
);
1765 static void monitor_fdsets_cleanup(void)
1767 MonFdset
*mon_fdset
;
1768 MonFdset
*mon_fdset_next
;
1770 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1771 monitor_fdset_cleanup(mon_fdset
);
1775 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1776 const char *opaque
, Error
**errp
)
1779 Monitor
*mon
= cur_mon
;
1782 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1784 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1788 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1789 has_opaque
, opaque
, errp
);
1801 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1803 MonFdset
*mon_fdset
;
1804 MonFdsetFd
*mon_fdset_fd
;
1807 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1808 if (mon_fdset
->id
!= fdset_id
) {
1811 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1813 if (mon_fdset_fd
->fd
!= fd
) {
1816 mon_fdset_fd
->removed
= true;
1819 mon_fdset_fd
->removed
= true;
1822 if (has_fd
&& !mon_fdset_fd
) {
1825 monitor_fdset_cleanup(mon_fdset
);
1831 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1834 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1836 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1839 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1841 MonFdset
*mon_fdset
;
1842 MonFdsetFd
*mon_fdset_fd
;
1843 FdsetInfoList
*fdset_list
= NULL
;
1845 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1846 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1847 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1849 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1850 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1852 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1853 FdsetFdInfoList
*fdsetfd_info
;
1855 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1856 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1857 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1858 if (mon_fdset_fd
->opaque
) {
1859 fdsetfd_info
->value
->has_opaque
= true;
1860 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1862 fdsetfd_info
->value
->has_opaque
= false;
1865 fdsetfd_info
->next
= fdsetfd_list
;
1866 fdsetfd_list
= fdsetfd_info
;
1869 fdset_info
->value
->fds
= fdsetfd_list
;
1871 fdset_info
->next
= fdset_list
;
1872 fdset_list
= fdset_info
;
1878 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1879 bool has_opaque
, const char *opaque
,
1882 MonFdset
*mon_fdset
= NULL
;
1883 MonFdsetFd
*mon_fdset_fd
;
1887 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1888 /* Break if match found or match impossible due to ordering by ID */
1889 if (fdset_id
<= mon_fdset
->id
) {
1890 if (fdset_id
< mon_fdset
->id
) {
1898 if (mon_fdset
== NULL
) {
1899 int64_t fdset_id_prev
= -1;
1900 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1904 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1905 "a non-negative value");
1908 /* Use specified fdset ID */
1909 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1910 mon_fdset_cur
= mon_fdset
;
1911 if (fdset_id
< mon_fdset_cur
->id
) {
1916 /* Use first available fdset ID */
1917 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1918 mon_fdset_cur
= mon_fdset
;
1919 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1920 fdset_id_prev
= mon_fdset_cur
->id
;
1927 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1929 mon_fdset
->id
= fdset_id
;
1931 mon_fdset
->id
= fdset_id_prev
+ 1;
1934 /* The fdset list is ordered by fdset ID */
1935 if (!mon_fdset_cur
) {
1936 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1937 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1938 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1940 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1944 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1945 mon_fdset_fd
->fd
= fd
;
1946 mon_fdset_fd
->removed
= false;
1948 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1950 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1952 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1953 fdinfo
->fdset_id
= mon_fdset
->id
;
1954 fdinfo
->fd
= mon_fdset_fd
->fd
;
1959 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1962 MonFdset
*mon_fdset
;
1963 MonFdsetFd
*mon_fdset_fd
;
1966 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1967 if (mon_fdset
->id
!= fdset_id
) {
1970 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1971 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
1972 if (mon_fd_flags
== -1) {
1976 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
1977 return mon_fdset_fd
->fd
;
1989 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
1991 MonFdset
*mon_fdset
;
1992 MonFdsetFd
*mon_fdset_fd_dup
;
1994 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1995 if (mon_fdset
->id
!= fdset_id
) {
1998 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
1999 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2003 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2004 mon_fdset_fd_dup
->fd
= dup_fd
;
2005 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2011 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2013 MonFdset
*mon_fdset
;
2014 MonFdsetFd
*mon_fdset_fd_dup
;
2016 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2017 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2018 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2020 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2021 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2022 monitor_fdset_cleanup(mon_fdset
);
2026 return mon_fdset
->id
;
2034 int monitor_fdset_dup_fd_find(int dup_fd
)
2036 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2039 void monitor_fdset_dup_fd_remove(int dup_fd
)
2041 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2044 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2047 Error
*local_err
= NULL
;
2049 if (!qemu_isdigit(fdname
[0]) && mon
) {
2050 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2052 fd
= qemu_parse_fd(fdname
);
2054 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2059 error_propagate(errp
, local_err
);
2068 /* Please update hmp-commands.hx when adding or changing commands */
2069 static mon_cmd_t info_cmds
[] = {
2070 #include "hmp-commands-info.h"
2074 /* mon_cmds and info_cmds would be sorted at runtime */
2075 static mon_cmd_t mon_cmds
[] = {
2076 #include "hmp-commands.h"
2080 static const mon_cmd_t qmp_cmds
[] = {
2081 #include "qmp-commands-old.h"
2085 /*******************************************************************/
2087 static const char *pch
;
2088 static sigjmp_buf expr_env
;
2091 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2092 expr_error(Monitor
*mon
, const char *fmt
, ...)
2096 monitor_vprintf(mon
, fmt
, ap
);
2097 monitor_printf(mon
, "\n");
2099 siglongjmp(expr_env
, 1);
2102 /* return 0 if OK, -1 if not found */
2103 static int get_monitor_def(target_long
*pval
, const char *name
)
2105 const MonitorDef
*md
= target_monitor_defs();
2112 for(; md
->name
!= NULL
; md
++) {
2113 if (compare_cmd(name
, md
->name
)) {
2114 if (md
->get_value
) {
2115 *pval
= md
->get_value(md
, md
->offset
);
2117 CPUArchState
*env
= mon_get_cpu_env();
2118 ptr
= (uint8_t *)env
+ md
->offset
;
2121 *pval
= *(int32_t *)ptr
;
2124 *pval
= *(target_long
*)ptr
;
2137 static void next(void)
2141 while (qemu_isspace(*pch
))
2146 static int64_t expr_sum(Monitor
*mon
);
2148 static int64_t expr_unary(Monitor
*mon
)
2157 n
= expr_unary(mon
);
2161 n
= -expr_unary(mon
);
2165 n
= ~expr_unary(mon
);
2171 expr_error(mon
, "')' expected");
2178 expr_error(mon
, "character constant expected");
2182 expr_error(mon
, "missing terminating \' character");
2192 while ((*pch
>= 'a' && *pch
<= 'z') ||
2193 (*pch
>= 'A' && *pch
<= 'Z') ||
2194 (*pch
>= '0' && *pch
<= '9') ||
2195 *pch
== '_' || *pch
== '.') {
2196 if ((q
- buf
) < sizeof(buf
) - 1)
2200 while (qemu_isspace(*pch
))
2203 ret
= get_monitor_def(®
, buf
);
2205 expr_error(mon
, "unknown register");
2210 expr_error(mon
, "unexpected end of expression");
2215 n
= strtoull(pch
, &p
, 0);
2216 if (errno
== ERANGE
) {
2217 expr_error(mon
, "number too large");
2220 expr_error(mon
, "invalid char '%c' in expression", *p
);
2223 while (qemu_isspace(*pch
))
2231 static int64_t expr_prod(Monitor
*mon
)
2236 val
= expr_unary(mon
);
2239 if (op
!= '*' && op
!= '/' && op
!= '%')
2242 val2
= expr_unary(mon
);
2251 expr_error(mon
, "division by zero");
2262 static int64_t expr_logic(Monitor
*mon
)
2267 val
= expr_prod(mon
);
2270 if (op
!= '&' && op
!= '|' && op
!= '^')
2273 val2
= expr_prod(mon
);
2290 static int64_t expr_sum(Monitor
*mon
)
2295 val
= expr_logic(mon
);
2298 if (op
!= '+' && op
!= '-')
2301 val2
= expr_logic(mon
);
2310 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2313 if (sigsetjmp(expr_env
, 0)) {
2317 while (qemu_isspace(*pch
))
2319 *pval
= expr_sum(mon
);
2324 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2326 const char *p
= *pp
;
2330 d
= strtod(p
, &tailp
);
2332 monitor_printf(mon
, "Number expected\n");
2335 if (d
!= d
|| d
- d
!= 0) {
2336 /* NaN or infinity */
2337 monitor_printf(mon
, "Bad number\n");
2346 * Store the command-name in cmdname, and return a pointer to
2347 * the remaining of the command string.
2349 static const char *get_command_name(const char *cmdline
,
2350 char *cmdname
, size_t nlen
)
2353 const char *p
, *pstart
;
2356 while (qemu_isspace(*p
))
2361 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2366 memcpy(cmdname
, pstart
, len
);
2367 cmdname
[len
] = '\0';
2372 * Read key of 'type' into 'key' and return the current
2375 static char *key_get_info(const char *type
, char **key
)
2383 p
= strchr(type
, ':');
2390 str
= g_malloc(len
+ 1);
2391 memcpy(str
, type
, len
);
2398 static int default_fmt_format
= 'x';
2399 static int default_fmt_size
= 4;
2401 static int is_valid_option(const char *c
, const char *typestr
)
2409 typestr
= strstr(typestr
, option
);
2410 return (typestr
!= NULL
);
2413 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2414 const char *cmdname
)
2416 const mon_cmd_t
*cmd
;
2418 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2419 if (compare_cmd(cmdname
, cmd
->name
)) {
2427 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2429 return search_dispatch_table(qmp_cmds
, cmdname
);
2433 * Parse command name from @cmdp according to command table @table.
2434 * If blank, return NULL.
2435 * Else, if no valid command can be found, report to @mon, and return
2437 * Else, change @cmdp to point right behind the name, and return its
2438 * command table entry.
2439 * Do not assume the return value points into @table! It doesn't when
2440 * the command is found in a sub-command table.
2442 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2447 const mon_cmd_t
*cmd
;
2450 /* extract the command name */
2451 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2455 cmd
= search_dispatch_table(table
, cmdname
);
2457 monitor_printf(mon
, "unknown command: '%.*s'\n",
2458 (int)(p
- *cmdp
), *cmdp
);
2462 /* filter out following useless space */
2463 while (qemu_isspace(*p
)) {
2468 /* search sub command */
2469 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2470 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2477 * Parse arguments for @cmd.
2478 * If it can't be parsed, report to @mon, and return NULL.
2479 * Else, insert command arguments into a QDict, and return it.
2480 * Note: On success, caller has to free the QDict structure.
2483 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2485 const mon_cmd_t
*cmd
)
2487 const char *typestr
;
2490 const char *p
= *endp
;
2492 QDict
*qdict
= qdict_new();
2494 /* parse the parameters */
2495 typestr
= cmd
->args_type
;
2497 typestr
= key_get_info(typestr
, &key
);
2509 while (qemu_isspace(*p
))
2511 if (*typestr
== '?') {
2514 /* no optional string: NULL argument */
2518 ret
= get_str(buf
, sizeof(buf
), &p
);
2522 monitor_printf(mon
, "%s: filename expected\n",
2526 monitor_printf(mon
, "%s: block device name expected\n",
2530 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2535 qdict_put(qdict
, key
, qstring_from_str(buf
));
2540 QemuOptsList
*opts_list
;
2543 opts_list
= qemu_find_opts(key
);
2544 if (!opts_list
|| opts_list
->desc
->name
) {
2547 while (qemu_isspace(*p
)) {
2552 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2555 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2559 qemu_opts_to_qdict(opts
, qdict
);
2560 qemu_opts_del(opts
);
2565 int count
, format
, size
;
2567 while (qemu_isspace(*p
))
2573 if (qemu_isdigit(*p
)) {
2575 while (qemu_isdigit(*p
)) {
2576 count
= count
* 10 + (*p
- '0');
2614 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2615 monitor_printf(mon
, "invalid char in format: '%c'\n",
2620 format
= default_fmt_format
;
2621 if (format
!= 'i') {
2622 /* for 'i', not specifying a size gives -1 as size */
2624 size
= default_fmt_size
;
2625 default_fmt_size
= size
;
2627 default_fmt_format
= format
;
2630 format
= default_fmt_format
;
2631 if (format
!= 'i') {
2632 size
= default_fmt_size
;
2637 qdict_put(qdict
, "count", qint_from_int(count
));
2638 qdict_put(qdict
, "format", qint_from_int(format
));
2639 qdict_put(qdict
, "size", qint_from_int(size
));
2648 while (qemu_isspace(*p
))
2650 if (*typestr
== '?' || *typestr
== '.') {
2651 if (*typestr
== '?') {
2659 while (qemu_isspace(*p
))
2668 if (get_expr(mon
, &val
, &p
))
2670 /* Check if 'i' is greater than 32-bit */
2671 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2672 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2673 monitor_printf(mon
, "integer is for 32-bit values\n");
2675 } else if (c
== 'M') {
2677 monitor_printf(mon
, "enter a positive value\n");
2682 qdict_put(qdict
, key
, qint_from_int(val
));
2690 while (qemu_isspace(*p
)) {
2693 if (*typestr
== '?') {
2699 val
= qemu_strtosz(p
, &end
);
2701 monitor_printf(mon
, "invalid size\n");
2704 qdict_put(qdict
, key
, qint_from_int(val
));
2712 while (qemu_isspace(*p
))
2714 if (*typestr
== '?') {
2720 if (get_double(mon
, &val
, &p
) < 0) {
2723 if (p
[0] && p
[1] == 's') {
2726 val
/= 1e3
; p
+= 2; break;
2728 val
/= 1e6
; p
+= 2; break;
2730 val
/= 1e9
; p
+= 2; break;
2733 if (*p
&& !qemu_isspace(*p
)) {
2734 monitor_printf(mon
, "Unknown unit suffix\n");
2737 qdict_put(qdict
, key
, qfloat_from_double(val
));
2745 while (qemu_isspace(*p
)) {
2749 while (qemu_isgraph(*p
)) {
2752 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2754 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2757 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2760 qdict_put(qdict
, key
, qbool_from_bool(val
));
2765 const char *tmp
= p
;
2772 while (qemu_isspace(*p
))
2777 if(!is_valid_option(p
, typestr
)) {
2779 monitor_printf(mon
, "%s: unsupported option -%c\n",
2791 qdict_put(qdict
, key
, qbool_from_bool(true));
2798 /* package all remaining string */
2801 while (qemu_isspace(*p
)) {
2804 if (*typestr
== '?') {
2807 /* no remaining string: NULL argument */
2813 monitor_printf(mon
, "%s: string expected\n",
2817 qdict_put(qdict
, key
, qstring_from_str(p
));
2823 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2829 /* check that all arguments were parsed */
2830 while (qemu_isspace(*p
))
2833 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2846 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2849 const mon_cmd_t
*cmd
;
2851 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2856 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2858 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2863 cmd
->mhandler
.cmd(mon
, qdict
);
2867 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2869 const char *p
, *pstart
;
2878 p
= pstart
+ strlen(pstart
);
2880 if (len
> sizeof(cmd
) - 2)
2881 len
= sizeof(cmd
) - 2;
2882 memcpy(cmd
, pstart
, len
);
2884 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2885 readline_add_completion(mon
->rs
, cmd
);
2893 static void file_completion(Monitor
*mon
, const char *input
)
2898 char file
[1024], file_prefix
[1024];
2902 p
= strrchr(input
, '/');
2905 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2906 pstrcpy(path
, sizeof(path
), ".");
2908 input_path_len
= p
- input
+ 1;
2909 memcpy(path
, input
, input_path_len
);
2910 if (input_path_len
> sizeof(path
) - 1)
2911 input_path_len
= sizeof(path
) - 1;
2912 path
[input_path_len
] = '\0';
2913 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2916 ffs
= opendir(path
);
2925 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
2929 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2930 memcpy(file
, input
, input_path_len
);
2931 if (input_path_len
< sizeof(file
))
2932 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2934 /* stat the file to find out if it's a directory.
2935 * In that case add a slash to speed up typing long paths
2937 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
2938 pstrcat(file
, sizeof(file
), "/");
2940 readline_add_completion(mon
->rs
, file
);
2946 static const char *next_arg_type(const char *typestr
)
2948 const char *p
= strchr(typestr
, ':');
2949 return (p
!= NULL
? ++p
: typestr
);
2952 static void add_completion_option(ReadLineState
*rs
, const char *str
,
2955 if (!str
|| !option
) {
2958 if (!strncmp(option
, str
, strlen(str
))) {
2959 readline_add_completion(rs
, option
);
2963 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2966 ChardevBackendInfoList
*list
, *start
;
2972 readline_set_completion_index(rs
, len
);
2974 start
= list
= qmp_query_chardev_backends(NULL
);
2976 const char *chr_name
= list
->value
->name
;
2978 if (!strncmp(chr_name
, str
, len
)) {
2979 readline_add_completion(rs
, chr_name
);
2983 qapi_free_ChardevBackendInfoList(start
);
2986 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
2995 readline_set_completion_index(rs
, len
);
2996 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
2997 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3001 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3011 readline_set_completion_index(rs
, len
);
3012 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3015 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3017 name
= object_class_get_name(OBJECT_CLASS(dc
));
3019 if (!dc
->cannot_instantiate_with_device_add_yet
3020 && !strncmp(name
, str
, len
)) {
3021 readline_add_completion(rs
, name
);
3028 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3038 readline_set_completion_index(rs
, len
);
3039 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3043 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3044 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3045 readline_add_completion(rs
, name
);
3052 static void peripheral_device_del_completion(ReadLineState
*rs
,
3053 const char *str
, size_t len
)
3055 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3056 GSList
*list
, *item
;
3058 list
= qdev_build_hotpluggable_device_list(peripheral
);
3063 for (item
= list
; item
; item
= g_slist_next(item
)) {
3064 DeviceState
*dev
= item
->data
;
3066 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3067 readline_add_completion(rs
, dev
->id
);
3074 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3077 ChardevInfoList
*list
, *start
;
3083 readline_set_completion_index(rs
, len
);
3085 start
= list
= qmp_query_chardev(NULL
);
3087 ChardevInfo
*chr
= list
->value
;
3089 if (!strncmp(chr
->label
, str
, len
)) {
3090 readline_add_completion(rs
, chr
->label
);
3094 qapi_free_ChardevInfoList(start
);
3097 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3100 ChardevInfoList
*list
, *start
;
3103 readline_set_completion_index(rs
, len
);
3105 start
= list
= qmp_query_chardev(NULL
);
3107 ChardevInfo
*chr_info
= list
->value
;
3109 if (!strncmp(chr_info
->label
, str
, len
)) {
3110 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3111 if (chr
&& chr_is_ringbuf(chr
)) {
3112 readline_add_completion(rs
, chr_info
->label
);
3117 qapi_free_ChardevInfoList(start
);
3120 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3125 ringbuf_completion(rs
, str
);
3128 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3137 readline_set_completion_index(rs
, len
);
3138 peripheral_device_del_completion(rs
, str
, len
);
3141 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3143 ObjectPropertyInfoList
*list
, *start
;
3150 readline_set_completion_index(rs
, len
);
3152 start
= list
= qmp_qom_list("/objects", NULL
);
3154 ObjectPropertyInfo
*info
= list
->value
;
3156 if (!strncmp(info
->type
, "child<", 5)
3157 && !strncmp(info
->name
, str
, len
)) {
3158 readline_add_completion(rs
, info
->name
);
3162 qapi_free_ObjectPropertyInfoList(start
);
3165 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3174 sep
= strrchr(str
, '-');
3179 readline_set_completion_index(rs
, len
);
3180 for (i
= 0; i
< Q_KEY_CODE_MAX
; i
++) {
3181 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3182 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3187 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3192 readline_set_completion_index(rs
, len
);
3194 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3196 count
= qemu_find_net_clients_except(NULL
, ncs
,
3197 NET_CLIENT_OPTIONS_KIND_NONE
,
3199 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3200 const char *name
= ncs
[i
]->name
;
3201 if (!strncmp(str
, name
, len
)) {
3202 readline_add_completion(rs
, name
);
3205 } else if (nb_args
== 3) {
3206 add_completion_option(rs
, str
, "on");
3207 add_completion_option(rs
, str
, "off");
3211 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3214 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3221 readline_set_completion_index(rs
, len
);
3222 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
3224 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3226 const char *name
= ncs
[i
]->name
;
3227 if (strncmp(str
, name
, len
)) {
3230 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3232 readline_add_completion(rs
, name
);
3237 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3242 readline_set_completion_index(rs
, len
);
3245 for (id
= 0; id
< trace_event_count(); id
++) {
3246 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3247 if (!strncmp(str
, event_name
, len
)) {
3248 readline_add_completion(rs
, event_name
);
3251 } else if (nb_args
== 3) {
3252 add_completion_option(rs
, str
, "on");
3253 add_completion_option(rs
, str
, "off");
3257 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3264 readline_set_completion_index(rs
, strlen(str
));
3265 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3266 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3270 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3276 readline_set_completion_index(rs
, len
);
3279 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
3280 const char *name
= MigrationCapability_lookup
[i
];
3281 if (!strncmp(str
, name
, len
)) {
3282 readline_add_completion(rs
, name
);
3285 } else if (nb_args
== 3) {
3286 add_completion_option(rs
, str
, "on");
3287 add_completion_option(rs
, str
, "off");
3291 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3297 readline_set_completion_index(rs
, len
);
3300 for (i
= 0; i
< MIGRATION_PARAMETER_MAX
; i
++) {
3301 const char *name
= MigrationParameter_lookup
[i
];
3302 if (!strncmp(str
, name
, len
)) {
3303 readline_add_completion(rs
, name
);
3309 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3317 readline_set_completion_index(rs
, len
);
3318 for (i
= 0; host_net_devices
[i
]; i
++) {
3319 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3320 readline_add_completion(rs
, host_net_devices
[i
]);
3325 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3327 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3331 readline_set_completion_index(rs
, len
);
3333 count
= qemu_find_net_clients_except(NULL
, ncs
,
3334 NET_CLIENT_OPTIONS_KIND_NONE
,
3336 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3340 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3343 snprintf(name
, sizeof(name
), "%d", id
);
3344 if (!strncmp(str
, name
, len
)) {
3345 readline_add_completion(rs
, name
);
3349 } else if (nb_args
== 3) {
3350 count
= qemu_find_net_clients_except(NULL
, ncs
,
3351 NET_CLIENT_OPTIONS_KIND_NIC
,
3353 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3357 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
3358 net_hub_id_for_client(ncs
[i
], &id
)) {
3361 name
= ncs
[i
]->name
;
3362 if (!strncmp(str
, name
, len
)) {
3363 readline_add_completion(rs
, name
);
3370 static void vm_completion(ReadLineState
*rs
, const char *str
)
3373 BlockDriverState
*bs
= NULL
;
3376 readline_set_completion_index(rs
, len
);
3377 while ((bs
= bdrv_next(bs
))) {
3378 SnapshotInfoList
*snapshots
, *snapshot
;
3380 if (!bdrv_can_snapshot(bs
)) {
3383 if (bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
)) {
3386 snapshot
= snapshots
;
3388 char *completion
= snapshot
->value
->name
;
3389 if (!strncmp(str
, completion
, len
)) {
3390 readline_add_completion(rs
, completion
);
3392 completion
= snapshot
->value
->id
;
3393 if (!strncmp(str
, completion
, len
)) {
3394 readline_add_completion(rs
, completion
);
3396 snapshot
= snapshot
->next
;
3398 qapi_free_SnapshotInfoList(snapshots
);
3403 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3406 vm_completion(rs
, str
);
3410 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3413 vm_completion(rs
, str
);
3417 static void monitor_find_completion_by_table(Monitor
*mon
,
3418 const mon_cmd_t
*cmd_table
,
3422 const char *cmdname
;
3424 const char *ptype
, *str
, *name
;
3425 const mon_cmd_t
*cmd
;
3426 BlockDriverState
*bs
;
3429 /* command completion */
3434 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3435 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3436 cmd_completion(mon
, cmdname
, cmd
->name
);
3439 /* find the command */
3440 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3441 if (compare_cmd(args
[0], cmd
->name
)) {
3449 if (cmd
->sub_table
) {
3450 /* do the job again */
3451 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3452 &args
[1], nb_args
- 1);
3455 if (cmd
->command_completion
) {
3456 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3460 ptype
= next_arg_type(cmd
->args_type
);
3461 for(i
= 0; i
< nb_args
- 2; i
++) {
3462 if (*ptype
!= '\0') {
3463 ptype
= next_arg_type(ptype
);
3464 while (*ptype
== '?')
3465 ptype
= next_arg_type(ptype
);
3468 str
= args
[nb_args
- 1];
3469 while (*ptype
== '-' && ptype
[1] != '\0') {
3470 ptype
= next_arg_type(ptype
);
3474 /* file completion */
3475 readline_set_completion_index(mon
->rs
, strlen(str
));
3476 file_completion(mon
, str
);
3479 /* block device name completion */
3480 readline_set_completion_index(mon
->rs
, strlen(str
));
3481 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3482 name
= bdrv_get_device_name(bs
);
3483 if (str
[0] == '\0' ||
3484 !strncmp(name
, str
, strlen(str
))) {
3485 readline_add_completion(mon
->rs
, name
);
3491 if (!strcmp(cmd
->name
, "help|?")) {
3492 monitor_find_completion_by_table(mon
, cmd_table
,
3493 &args
[1], nb_args
- 1);
3502 static void monitor_find_completion(void *opaque
,
3503 const char *cmdline
)
3505 Monitor
*mon
= opaque
;
3506 char *args
[MAX_ARGS
];
3509 /* 1. parse the cmdline */
3510 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3514 /* if the line ends with a space, it means we want to complete the
3516 len
= strlen(cmdline
);
3517 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3518 if (nb_args
>= MAX_ARGS
) {
3521 args
[nb_args
++] = g_strdup("");
3524 /* 2. auto complete according to args */
3525 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3528 free_cmdline_args(args
, nb_args
);
3531 static int monitor_can_read(void *opaque
)
3533 Monitor
*mon
= opaque
;
3535 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3538 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
3541 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
3543 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3544 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3545 "Capabilities negotiation is already complete, command "
3546 "'%s' ignored", cmd
->name
);
3549 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3550 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3551 "Expecting capabilities negotiation with "
3552 "'qmp_capabilities' before command '%s'", cmd
->name
);
3559 * Argument validation rules:
3561 * 1. The argument must exist in cmd_args qdict
3562 * 2. The argument type must be the expected one
3564 * Special case: If the argument doesn't exist in cmd_args and
3565 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3566 * checking is skipped for it.
3568 static void check_client_args_type(const QDict
*client_args
,
3569 const QDict
*cmd_args
, int flags
,
3572 const QDictEntry
*ent
;
3574 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3577 const QObject
*client_arg
= qdict_entry_value(ent
);
3578 const char *client_arg_name
= qdict_entry_key(ent
);
3580 obj
= qdict_get(cmd_args
, client_arg_name
);
3582 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3583 /* handler accepts unknowns */
3586 /* client arg doesn't exist */
3587 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3591 arg_type
= qobject_to_qstring(obj
);
3592 assert(arg_type
!= NULL
);
3594 /* check if argument's type is correct */
3595 switch (qstring_get_str(arg_type
)[0]) {
3599 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3600 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3601 client_arg_name
, "string");
3609 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3610 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3611 client_arg_name
, "int");
3616 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3617 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3618 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3619 client_arg_name
, "number");
3625 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3626 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3627 client_arg_name
, "bool");
3632 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3635 /* Any QObject can be passed. */
3640 * These types are not supported by QMP and thus are not
3641 * handled here. Fall through.
3650 * - Check if the client has passed all mandatory args
3651 * - Set special flags for argument validation
3653 static void check_mandatory_args(const QDict
*cmd_args
,
3654 const QDict
*client_args
, int *flags
,
3657 const QDictEntry
*ent
;
3659 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3660 const char *cmd_arg_name
= qdict_entry_key(ent
);
3661 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3662 assert(type
!= NULL
);
3664 if (qstring_get_str(type
)[0] == 'O') {
3665 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3666 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3667 } else if (qstring_get_str(type
)[0] != '-' &&
3668 qstring_get_str(type
)[1] != '?' &&
3669 !qdict_haskey(client_args
, cmd_arg_name
)) {
3670 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3676 static QDict
*qdict_from_args_type(const char *args_type
)
3680 QString
*key
, *type
, *cur_qs
;
3682 assert(args_type
!= NULL
);
3684 qdict
= qdict_new();
3686 if (args_type
== NULL
|| args_type
[0] == '\0') {
3687 /* no args, empty qdict */
3691 key
= qstring_new();
3692 type
= qstring_new();
3697 switch (args_type
[i
]) {
3700 qdict_put(qdict
, qstring_get_str(key
), type
);
3702 if (args_type
[i
] == '\0') {
3705 type
= qstring_new(); /* qdict has ref */
3706 cur_qs
= key
= qstring_new();
3712 qstring_append_chr(cur_qs
, args_type
[i
]);
3722 * Client argument checking rules:
3724 * 1. Client must provide all mandatory arguments
3725 * 2. Each argument provided by the client must be expected
3726 * 3. Each argument provided by the client must have the type expected
3729 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3736 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3739 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3744 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3747 error_propagate(errp
, err
);
3752 * Input object checking rules
3754 * 1. Input object must be a dict
3755 * 2. The "execute" key must exist
3756 * 3. The "execute" key must be a string
3757 * 4. If the "arguments" key exists, it must be a dict
3758 * 5. If the "id" key exists, it can be anything (ie. json-value)
3759 * 6. Any argument not listed above is considered invalid
3761 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3763 const QDictEntry
*ent
;
3764 int has_exec_key
= 0;
3767 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3768 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3772 input_dict
= qobject_to_qdict(input_obj
);
3774 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3775 const char *arg_name
= qdict_entry_key(ent
);
3776 const QObject
*arg_obj
= qdict_entry_value(ent
);
3778 if (!strcmp(arg_name
, "execute")) {
3779 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3780 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3781 "execute", "string");
3785 } else if (!strcmp(arg_name
, "arguments")) {
3786 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3787 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3788 "arguments", "object");
3791 } else if (!strcmp(arg_name
, "id")) {
3792 /* Any string is acceptable as "id", so nothing to check */
3794 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3799 if (!has_exec_key
) {
3800 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3807 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
3809 Error
*local_err
= NULL
;
3810 QObject
*obj
, *data
;
3811 QDict
*input
, *args
;
3812 const mon_cmd_t
*cmd
;
3813 const char *cmd_name
;
3814 Monitor
*mon
= cur_mon
;
3816 args
= input
= NULL
;
3819 obj
= json_parser_parse(tokens
, NULL
);
3821 // FIXME: should be triggered in json_parser_parse()
3822 error_setg(&local_err
, QERR_JSON_PARSING
);
3826 input
= qmp_check_input_obj(obj
, &local_err
);
3828 qobject_decref(obj
);
3832 mon
->qmp
.id
= qdict_get(input
, "id");
3833 qobject_incref(mon
->qmp
.id
);
3835 cmd_name
= qdict_get_str(input
, "execute");
3836 trace_handle_qmp_command(mon
, cmd_name
);
3837 cmd
= qmp_find_cmd(cmd_name
);
3839 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3840 "The command %s has not been found", cmd_name
);
3843 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
3847 obj
= qdict_get(input
, "arguments");
3851 args
= qobject_to_qdict(obj
);
3855 qmp_check_client_args(cmd
, args
, &local_err
);
3860 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
3863 monitor_protocol_emitter(mon
, data
, local_err
);
3864 qobject_decref(data
);
3869 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3871 Monitor
*old_mon
= cur_mon
;
3875 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3880 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3882 Monitor
*old_mon
= cur_mon
;
3888 for (i
= 0; i
< size
; i
++)
3889 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3891 if (size
== 0 || buf
[size
- 1] != 0)
3892 monitor_printf(cur_mon
, "corrupted command\n");
3894 handle_hmp_command(cur_mon
, (char *)buf
);
3900 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3901 void *readline_opaque
)
3903 Monitor
*mon
= opaque
;
3905 monitor_suspend(mon
);
3906 handle_hmp_command(mon
, cmdline
);
3907 monitor_resume(mon
);
3910 int monitor_suspend(Monitor
*mon
)
3918 void monitor_resume(Monitor
*mon
)
3922 if (--mon
->suspend_cnt
== 0)
3923 readline_show_prompt(mon
->rs
);
3926 static QObject
*get_qmp_greeting(void)
3928 QObject
*ver
= NULL
;
3930 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3931 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
3934 static void monitor_qmp_event(void *opaque
, int event
)
3937 Monitor
*mon
= opaque
;
3940 case CHR_EVENT_OPENED
:
3941 mon
->qmp
.in_command_mode
= false;
3942 data
= get_qmp_greeting();
3943 monitor_json_emitter(mon
, data
);
3944 qobject_decref(data
);
3947 case CHR_EVENT_CLOSED
:
3948 json_message_parser_destroy(&mon
->qmp
.parser
);
3949 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
3951 monitor_fdsets_cleanup();
3956 static void monitor_event(void *opaque
, int event
)
3958 Monitor
*mon
= opaque
;
3961 case CHR_EVENT_MUX_IN
:
3962 qemu_mutex_lock(&mon
->out_lock
);
3964 qemu_mutex_unlock(&mon
->out_lock
);
3965 if (mon
->reset_seen
) {
3966 readline_restart(mon
->rs
);
3967 monitor_resume(mon
);
3970 mon
->suspend_cnt
= 0;
3974 case CHR_EVENT_MUX_OUT
:
3975 if (mon
->reset_seen
) {
3976 if (mon
->suspend_cnt
== 0) {
3977 monitor_printf(mon
, "\n");
3980 monitor_suspend(mon
);
3984 qemu_mutex_lock(&mon
->out_lock
);
3986 qemu_mutex_unlock(&mon
->out_lock
);
3989 case CHR_EVENT_OPENED
:
3990 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3991 "information\n", QEMU_VERSION
);
3992 if (!mon
->mux_out
) {
3993 readline_restart(mon
->rs
);
3994 readline_show_prompt(mon
->rs
);
3996 mon
->reset_seen
= 1;
4000 case CHR_EVENT_CLOSED
:
4002 monitor_fdsets_cleanup();
4008 compare_mon_cmd(const void *a
, const void *b
)
4010 return strcmp(((const mon_cmd_t
*)a
)->name
,
4011 ((const mon_cmd_t
*)b
)->name
);
4014 static void sortcmdlist(void)
4017 int elem_size
= sizeof(mon_cmd_t
);
4019 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4020 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4022 array_num
= sizeof(info_cmds
)/elem_size
-1;
4023 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4035 /* These functions just adapt the readline interface in a typesafe way. We
4036 * could cast function pointers but that discards compiler checks.
4038 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4039 const char *fmt
, ...)
4043 monitor_vprintf(opaque
, fmt
, ap
);
4047 static void monitor_readline_flush(void *opaque
)
4049 monitor_flush(opaque
);
4052 static void __attribute__((constructor
)) monitor_lock_init(void)
4054 qemu_mutex_init(&monitor_lock
);
4057 void monitor_init(CharDriverState
*chr
, int flags
)
4059 static int is_first_init
= 1;
4062 if (is_first_init
) {
4063 monitor_qapi_event_init();
4068 mon
= g_malloc(sizeof(*mon
));
4069 monitor_data_init(mon
);
4073 if (flags
& MONITOR_USE_READLINE
) {
4074 mon
->rs
= readline_init(monitor_readline_printf
,
4075 monitor_readline_flush
,
4077 monitor_find_completion
);
4078 monitor_read_command(mon
, 0);
4081 if (monitor_is_qmp(mon
)) {
4082 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4083 monitor_qmp_event
, mon
);
4084 qemu_chr_fe_set_echo(chr
, true);
4085 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4087 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4088 monitor_event
, mon
);
4091 qemu_mutex_lock(&monitor_lock
);
4092 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4093 qemu_mutex_unlock(&monitor_lock
);
4096 static void bdrv_password_cb(void *opaque
, const char *password
,
4097 void *readline_opaque
)
4099 Monitor
*mon
= opaque
;
4100 BlockDriverState
*bs
= readline_opaque
;
4102 Error
*local_err
= NULL
;
4104 bdrv_add_key(bs
, password
, &local_err
);
4106 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
4107 error_free(local_err
);
4110 if (mon
->password_completion_cb
)
4111 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4113 monitor_read_command(mon
, 1);
4116 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4117 BlockCompletionFunc
*completion_cb
,
4122 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4123 bdrv_get_encrypted_filename(bs
));
4125 mon
->password_completion_cb
= completion_cb
;
4126 mon
->password_opaque
= opaque
;
4128 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4130 if (err
&& completion_cb
)
4131 completion_cb(opaque
, err
);
4136 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4137 BlockCompletionFunc
*completion_cb
,
4143 blk
= blk_by_name(device
);
4145 monitor_printf(mon
, "Device not found %s\n", device
);
4149 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4153 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4156 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4159 if (completion_cb
) {
4160 completion_cb(opaque
, 0);
4165 QemuOptsList qemu_mon_opts
= {
4167 .implied_opt_name
= "chardev",
4168 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4172 .type
= QEMU_OPT_STRING
,
4175 .type
= QEMU_OPT_STRING
,
4178 .type
= QEMU_OPT_BOOL
,
4181 .type
= QEMU_OPT_BOOL
,
4183 { /* end of list */ }
4188 void qmp_rtc_reset_reinjection(Error
**errp
)
4190 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4194 #ifndef TARGET_S390X
4195 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4197 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");