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
24 #include "qemu/osdep.h"
26 #include "qemu-common.h"
29 #include "monitor/qdev.h"
31 #include "hw/i386/pc.h"
32 #include "hw/pci/pci.h"
33 #include "sysemu/watchdog.h"
34 #include "hw/loader.h"
35 #include "exec/gdbstub.h"
37 #include "net/slirp.h"
38 #include "sysemu/char.h"
39 #include "ui/qemu-spice.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/numa.h"
42 #include "monitor/monitor.h"
43 #include "qemu/readline.h"
44 #include "ui/console.h"
46 #include "sysemu/blockdev.h"
47 #include "sysemu/block-backend.h"
48 #include "audio/audio.h"
49 #include "disas/disas.h"
50 #include "sysemu/balloon.h"
51 #include "qemu/timer.h"
52 #include "migration/migration.h"
53 #include "sysemu/kvm.h"
55 #include "sysemu/tpm.h"
56 #include "qapi/qmp/qerror.h"
57 #include "qapi/qmp/types.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"
64 #include "trace/control.h"
65 #include "monitor/hmp-target.h"
66 #ifdef CONFIG_TRACE_SIMPLE
67 #include "trace/simple.h"
69 #include "exec/memory.h"
70 #include "exec/exec-all.h"
72 #include "qmp-commands.h"
74 #include "qemu/thread.h"
75 #include "block/qapi.h"
76 #include "qapi/qmp-event.h"
77 #include "qapi-event.h"
78 #include "qmp-introspect.h"
79 #include "sysemu/block-backend.h"
80 #include "sysemu/qtest.h"
81 #include "qemu/cutils.h"
82 #include "qapi/qmp/dispatch.h"
84 #if defined(TARGET_S390X)
85 #include "hw/s390x/storage-keys.h"
92 * 'B' block device name
93 * 's' string (accept optional quote)
94 * 'S' it just appends the rest of the string (accept optional quote)
95 * 'O' option string of the form NAME=VALUE,...
96 * parsed according to QemuOptsList given by its name
97 * Example: 'device:O' uses qemu_device_opts.
98 * Restriction: only lists with empty desc are supported
99 * TODO lift the restriction
101 * 'l' target long (32 or 64 bit)
102 * 'M' Non-negative target long (32 or 64 bit), in user mode the
103 * value is multiplied by 2^20 (think Mebibyte)
104 * 'o' octets (aka bytes)
105 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
106 * K, k suffix, which multiplies the value by 2^60 for suffixes E
107 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
108 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
110 * user mode accepts an optional ms, us, ns suffix,
111 * which divides the value by 1e3, 1e6, 1e9, respectively
112 * '/' optional gdb-like print format (like "/10x")
114 * '?' optional type (for all types, except '/')
115 * '.' other form of optional type (for 'i' and 'l')
117 * user mode accepts "on" or "off"
118 * '-' optional parameter (eg. '-f')
122 typedef struct mon_cmd_t
{
124 const char *args_type
;
127 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
128 /* @sub_table is a list of 2nd level of commands. If it does not exist,
129 * cmd should be used. If it exists, sub_table[?].cmd should be
130 * used, and cmd of 1st level plays the role of help function.
132 struct mon_cmd_t
*sub_table
;
133 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
136 /* file descriptors passed via SCM_RIGHTS */
137 typedef struct mon_fd_t mon_fd_t
;
141 QLIST_ENTRY(mon_fd_t
) next
;
144 /* file descriptor associated with a file descriptor set */
145 typedef struct MonFdsetFd MonFdsetFd
;
150 QLIST_ENTRY(MonFdsetFd
) next
;
153 /* file descriptor set containing fds passed via SCM_RIGHTS */
154 typedef struct MonFdset MonFdset
;
157 QLIST_HEAD(, MonFdsetFd
) fds
;
158 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
159 QLIST_ENTRY(MonFdset
) next
;
163 JSONMessageParser parser
;
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
169 bool in_command_mode
; /* are we in command mode? */
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
177 typedef struct MonitorQAPIEventState
{
178 QAPIEvent event
; /* Throttling state for this event type and... */
179 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
180 QEMUTimer
*timer
; /* Timer for handling delayed events */
181 QDict
*qdict
; /* Delayed event (if any) */
182 } MonitorQAPIEventState
;
185 int64_t rate
; /* Minimum time (in ns) between two events */
186 } MonitorQAPIEventConf
;
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
205 BlockCompletionFunc
*password_completion_cb
;
206 void *password_opaque
;
207 mon_cmd_t
*cmd_table
;
208 QLIST_HEAD(,mon_fd_t
) fds
;
209 QLIST_ENTRY(Monitor
) entry
;
212 /* QMP checker flags */
213 #define QMP_ACCEPT_UNKNOWNS 1
215 /* Protects mon_list, monitor_event_state. */
216 static QemuMutex monitor_lock
;
218 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
219 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
220 static int mon_refcount
;
222 static mon_cmd_t mon_cmds
[];
223 static mon_cmd_t info_cmds
[];
227 static QEMUClockType event_clock_type
= QEMU_CLOCK_REALTIME
;
229 static void monitor_command_cb(void *opaque
, const char *cmdline
,
230 void *readline_opaque
);
233 * Is @mon a QMP monitor?
235 static inline bool monitor_is_qmp(const Monitor
*mon
)
237 return (mon
->flags
& MONITOR_USE_CONTROL
);
241 * Is the current monitor, if any, a QMP monitor?
243 bool monitor_cur_is_qmp(void)
245 return cur_mon
&& monitor_is_qmp(cur_mon
);
248 void monitor_read_command(Monitor
*mon
, int show_prompt
)
253 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
255 readline_show_prompt(mon
->rs
);
258 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
262 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
263 /* prompt is printed on return from the command handler */
266 monitor_printf(mon
, "terminal does not support password prompting\n");
271 static void monitor_flush_locked(Monitor
*mon
);
273 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
276 Monitor
*mon
= opaque
;
278 qemu_mutex_lock(&mon
->out_lock
);
280 monitor_flush_locked(mon
);
281 qemu_mutex_unlock(&mon
->out_lock
);
285 /* Called with mon->out_lock held. */
286 static void monitor_flush_locked(Monitor
*mon
)
292 if (mon
->skip_flush
) {
296 buf
= qstring_get_str(mon
->outbuf
);
297 len
= qstring_get_length(mon
->outbuf
);
299 if (len
&& !mon
->mux_out
) {
300 rc
= qemu_chr_fe_write(mon
->chr
.chr
, (const uint8_t *) buf
, len
);
301 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
302 /* all flushed or error */
303 QDECREF(mon
->outbuf
);
304 mon
->outbuf
= qstring_new();
309 QString
*tmp
= qstring_from_str(buf
+ rc
);
310 QDECREF(mon
->outbuf
);
313 if (mon
->out_watch
== 0) {
315 qemu_chr_fe_add_watch(mon
->chr
.chr
, G_IO_OUT
| G_IO_HUP
,
316 monitor_unblocked
, mon
);
321 void monitor_flush(Monitor
*mon
)
323 qemu_mutex_lock(&mon
->out_lock
);
324 monitor_flush_locked(mon
);
325 qemu_mutex_unlock(&mon
->out_lock
);
328 /* flush at every end of line */
329 static void monitor_puts(Monitor
*mon
, const char *str
)
333 qemu_mutex_lock(&mon
->out_lock
);
339 qstring_append_chr(mon
->outbuf
, '\r');
341 qstring_append_chr(mon
->outbuf
, c
);
343 monitor_flush_locked(mon
);
346 qemu_mutex_unlock(&mon
->out_lock
);
349 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
356 if (monitor_is_qmp(mon
)) {
360 buf
= g_strdup_vprintf(fmt
, ap
);
361 monitor_puts(mon
, buf
);
365 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
369 monitor_vprintf(mon
, fmt
, ap
);
373 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
377 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
382 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
386 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
387 qobject_to_json(data
);
388 assert(json
!= NULL
);
390 qstring_append_chr(json
, '\n');
391 monitor_puts(mon
, qstring_get_str(json
));
396 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
397 /* Limit guest-triggerable events to 1 per second */
398 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
399 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
400 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
401 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
402 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
403 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
406 GHashTable
*monitor_qapi_event_state
;
409 * Emits the event to every monitor instance, @event is only used for trace
410 * Called with monitor_lock held.
412 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
416 trace_monitor_protocol_event_emit(event
, qdict
);
417 QLIST_FOREACH(mon
, &mon_list
, entry
) {
418 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
419 monitor_json_emitter(mon
, QOBJECT(qdict
));
424 static void monitor_qapi_event_handler(void *opaque
);
427 * Queue a new event for emission to Monitor instances,
428 * applying any rate limiting if required.
431 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
433 MonitorQAPIEventConf
*evconf
;
434 MonitorQAPIEventState
*evstate
;
436 assert(event
< QAPI_EVENT__MAX
);
437 evconf
= &monitor_qapi_event_conf
[event
];
438 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
440 qemu_mutex_lock(&monitor_lock
);
443 /* Unthrottled event */
444 monitor_qapi_event_emit(event
, qdict
);
446 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
447 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
449 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
450 assert(!evstate
|| timer_pending(evstate
->timer
));
454 * Timer is pending for (at least) evconf->rate ns after
455 * last send. Store event for sending when timer fires,
456 * replacing a prior stored event if any.
458 QDECREF(evstate
->qdict
);
459 evstate
->qdict
= qdict
;
460 QINCREF(evstate
->qdict
);
463 * Last send was (at least) evconf->rate ns ago.
464 * Send immediately, and arm the timer to call
465 * monitor_qapi_event_handler() in evconf->rate ns. Any
466 * events arriving before then will be delayed until then.
468 int64_t now
= qemu_clock_get_ns(event_clock_type
);
470 monitor_qapi_event_emit(event
, qdict
);
472 evstate
= g_new(MonitorQAPIEventState
, 1);
473 evstate
->event
= event
;
474 evstate
->data
= data
;
475 QINCREF(evstate
->data
);
476 evstate
->qdict
= NULL
;
477 evstate
->timer
= timer_new_ns(event_clock_type
,
478 monitor_qapi_event_handler
,
480 g_hash_table_add(monitor_qapi_event_state
, evstate
);
481 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
485 qemu_mutex_unlock(&monitor_lock
);
489 * This function runs evconf->rate ns after sending a throttled
491 * If another event has since been stored, send it.
493 static void monitor_qapi_event_handler(void *opaque
)
495 MonitorQAPIEventState
*evstate
= opaque
;
496 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
498 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
499 qemu_mutex_lock(&monitor_lock
);
501 if (evstate
->qdict
) {
502 int64_t now
= qemu_clock_get_ns(event_clock_type
);
504 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
505 QDECREF(evstate
->qdict
);
506 evstate
->qdict
= NULL
;
507 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
509 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
510 QDECREF(evstate
->data
);
511 timer_free(evstate
->timer
);
515 qemu_mutex_unlock(&monitor_lock
);
518 static unsigned int qapi_event_throttle_hash(const void *key
)
520 const MonitorQAPIEventState
*evstate
= key
;
521 unsigned int hash
= evstate
->event
* 255;
523 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
524 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
527 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
528 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
534 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
536 const MonitorQAPIEventState
*eva
= a
;
537 const MonitorQAPIEventState
*evb
= b
;
539 if (eva
->event
!= evb
->event
) {
543 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
544 return !strcmp(qdict_get_str(eva
->data
, "id"),
545 qdict_get_str(evb
->data
, "id"));
548 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
549 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
550 qdict_get_str(evb
->data
, "node-name"));
556 static void monitor_qapi_event_init(void)
558 if (qtest_enabled()) {
559 event_clock_type
= QEMU_CLOCK_VIRTUAL
;
562 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
563 qapi_event_throttle_equal
);
564 qmp_event_set_func_emit(monitor_qapi_event_queue
);
567 void qmp_qmp_capabilities(Error
**errp
)
569 cur_mon
->qmp
.in_command_mode
= true;
572 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
574 static void monitor_data_init(Monitor
*mon
)
576 memset(mon
, 0, sizeof(Monitor
));
577 qemu_mutex_init(&mon
->out_lock
);
578 mon
->outbuf
= qstring_new();
579 /* Use *mon_cmds by default. */
580 mon
->cmd_table
= mon_cmds
;
583 static void monitor_data_destroy(Monitor
*mon
)
586 qemu_chr_add_handlers(mon
->chr
.chr
, NULL
, NULL
, NULL
, NULL
);
588 if (monitor_is_qmp(mon
)) {
589 json_message_parser_destroy(&mon
->qmp
.parser
);
592 QDECREF(mon
->outbuf
);
593 qemu_mutex_destroy(&mon
->out_lock
);
596 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
597 int64_t cpu_index
, Error
**errp
)
600 Monitor
*old_mon
, hmp
;
602 monitor_data_init(&hmp
);
603 hmp
.skip_flush
= true;
609 int ret
= monitor_set_cpu(cpu_index
);
612 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
618 handle_hmp_command(&hmp
, command_line
);
621 qemu_mutex_lock(&hmp
.out_lock
);
622 if (qstring_get_length(hmp
.outbuf
) > 0) {
623 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
625 output
= g_strdup("");
627 qemu_mutex_unlock(&hmp
.out_lock
);
630 monitor_data_destroy(&hmp
);
634 static int compare_cmd(const char *name
, const char *list
)
636 const char *p
, *pstart
;
644 p
= pstart
+ strlen(pstart
);
645 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
654 static int get_str(char *buf
, int buf_size
, const char **pp
)
662 while (qemu_isspace(*p
)) {
673 while (*p
!= '\0' && *p
!= '\"') {
689 printf("unsupported escape code: '\\%c'\n", c
);
692 if ((q
- buf
) < buf_size
- 1) {
696 if ((q
- buf
) < buf_size
- 1) {
703 printf("unterminated string\n");
708 while (*p
!= '\0' && !qemu_isspace(*p
)) {
709 if ((q
- buf
) < buf_size
- 1) {
722 static void free_cmdline_args(char **args
, int nb_args
)
726 assert(nb_args
<= MAX_ARGS
);
728 for (i
= 0; i
< nb_args
; i
++) {
735 * Parse the command line to get valid args.
736 * @cmdline: command line to be parsed.
737 * @pnb_args: location to store the number of args, must NOT be NULL.
738 * @args: location to store the args, which should be freed by caller, must
741 * Returns 0 on success, negative on failure.
743 * NOTE: this parser is an approximate form of the real command parser. Number
744 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
745 * return with failure.
747 static int parse_cmdline(const char *cmdline
,
748 int *pnb_args
, char **args
)
757 while (qemu_isspace(*p
)) {
763 if (nb_args
>= MAX_ARGS
) {
766 ret
= get_str(buf
, sizeof(buf
), &p
);
770 args
[nb_args
] = g_strdup(buf
);
777 free_cmdline_args(args
, nb_args
);
781 static void help_cmd_dump_one(Monitor
*mon
,
782 const mon_cmd_t
*cmd
,
788 for (i
= 0; i
< prefix_args_nb
; i
++) {
789 monitor_printf(mon
, "%s ", prefix_args
[i
]);
791 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
794 /* @args[@arg_index] is the valid command need to find in @cmds */
795 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
796 char **args
, int nb_args
, int arg_index
)
798 const mon_cmd_t
*cmd
;
800 /* No valid arg need to compare with, dump all in *cmds */
801 if (arg_index
>= nb_args
) {
802 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
803 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
808 /* Find one entry to dump */
809 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
810 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
811 if (cmd
->sub_table
) {
812 /* continue with next arg */
813 help_cmd_dump(mon
, cmd
->sub_table
,
814 args
, nb_args
, arg_index
+ 1);
816 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
823 static void help_cmd(Monitor
*mon
, const char *name
)
825 char *args
[MAX_ARGS
];
828 /* 1. parse user input */
830 /* special case for log, directly dump and return */
831 if (!strcmp(name
, "log")) {
832 const QEMULogItem
*item
;
833 monitor_printf(mon
, "Log items (comma separated):\n");
834 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
835 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
836 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
841 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
846 /* 2. dump the contents according to parsed args */
847 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
849 free_cmdline_args(args
, nb_args
);
852 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
854 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
857 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
859 const char *tp_name
= qdict_get_str(qdict
, "name");
860 bool new_state
= qdict_get_bool(qdict
, "option");
861 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
862 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
863 Error
*local_err
= NULL
;
866 monitor_printf(mon
, "argument vcpu must be positive");
870 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
872 error_report_err(local_err
);
876 #ifdef CONFIG_TRACE_SIMPLE
877 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
879 const char *op
= qdict_get_try_str(qdict
, "op");
880 const char *arg
= qdict_get_try_str(qdict
, "arg");
883 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
884 } else if (!strcmp(op
, "on")) {
885 st_set_trace_file_enabled(true);
886 } else if (!strcmp(op
, "off")) {
887 st_set_trace_file_enabled(false);
888 } else if (!strcmp(op
, "flush")) {
889 st_flush_trace_buffer();
890 } else if (!strcmp(op
, "set")) {
892 st_set_trace_file(arg
);
895 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
896 help_cmd(mon
, "trace-file");
901 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
903 help_cmd(mon
, "info");
906 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
908 CommandInfoList
*info
, **list
= opaque
;
914 info
= g_malloc0(sizeof(*info
));
915 info
->value
= g_malloc0(sizeof(*info
->value
));
916 info
->value
->name
= g_strdup(cmd
->name
);
921 CommandInfoList
*qmp_query_commands(Error
**errp
)
923 CommandInfoList
*list
= NULL
;
925 qmp_for_each_command(query_commands_cb
, &list
);
930 EventInfoList
*qmp_query_events(Error
**errp
)
932 EventInfoList
*info
, *ev_list
= NULL
;
935 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
936 const char *event_name
= QAPIEvent_lookup
[e
];
937 assert(event_name
!= NULL
);
938 info
= g_malloc0(sizeof(*info
));
939 info
->value
= g_malloc0(sizeof(*info
->value
));
940 info
->value
->name
= g_strdup(event_name
);
942 info
->next
= ev_list
;
950 * Minor hack: generated marshalling suppressed for this command
951 * ('gen': false in the schema) so we can parse the JSON string
952 * directly into QObject instead of first parsing it with
953 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
954 * to QObject with generated output marshallers, every time. Instead,
955 * we do it in test-qmp-input-visitor.c, just to make sure
956 * qapi-introspect.py's output actually conforms to the schema.
958 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
961 *ret_data
= qobject_from_json(qmp_schema_json
);
965 * We used to define commands in qmp-commands.hx in addition to the
966 * QAPI schema. This permitted defining some of them only in certain
967 * configurations. query-commands has always reflected that (good,
968 * because it lets QMP clients figure out what's actually available),
969 * while query-qmp-schema never did (not so good). This function is a
970 * hack to keep the configuration-specific commands defined exactly as
971 * before, even though qmp-commands.hx is gone.
973 * FIXME Educate the QAPI schema on configuration-specific commands,
974 * and drop this hack.
976 static void qmp_unregister_commands_hack(void)
979 qmp_unregister_command("query-spice");
982 qmp_unregister_command("rtc-reset-reinjection");
985 qmp_unregister_command("dump-skeys");
988 qmp_unregister_command("query-gic-capabilities");
990 #if !defined(TARGET_S390X)
991 qmp_unregister_command("query-cpu-model-expansion");
992 qmp_unregister_command("query-cpu-model-baseline");
993 qmp_unregister_command("query-cpu-model-comparison");
995 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
996 && !defined(TARGET_S390X)
997 qmp_unregister_command("query-cpu-definitions");
1001 static void qmp_init_marshal(void)
1003 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema
,
1005 qmp_register_command("device_add", qmp_device_add
,
1007 qmp_register_command("netdev_add", qmp_netdev_add
,
1010 /* call it after the rest of qapi_init() */
1011 register_module_init(qmp_unregister_commands_hack
, MODULE_INIT_QAPI
);
1014 qapi_init(qmp_init_marshal
);
1016 /* set the current CPU defined by the user */
1017 int monitor_set_cpu(int cpu_index
)
1021 cpu
= qemu_get_cpu(cpu_index
);
1025 cur_mon
->mon_cpu
= cpu
;
1029 CPUState
*mon_get_cpu(void)
1031 if (!cur_mon
->mon_cpu
) {
1032 monitor_set_cpu(first_cpu
->cpu_index
);
1034 cpu_synchronize_state(cur_mon
->mon_cpu
);
1035 return cur_mon
->mon_cpu
;
1038 CPUArchState
*mon_get_cpu_env(void)
1040 return mon_get_cpu()->env_ptr
;
1043 int monitor_get_cpu_index(void)
1045 return mon_get_cpu()->cpu_index
;
1048 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1050 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1053 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1055 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1056 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1059 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1061 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1064 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1073 str
= readline_get_history(mon
->rs
, i
);
1076 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1081 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1083 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1086 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1088 const char *name
= qdict_get_try_str(qdict
, "name");
1089 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1090 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1091 TraceEventInfoList
*events
;
1092 TraceEventInfoList
*elem
;
1093 Error
*local_err
= NULL
;
1099 monitor_printf(mon
, "argument vcpu must be positive");
1103 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1105 error_report_err(local_err
);
1109 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1110 monitor_printf(mon
, "%s : state %u\n",
1112 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1114 qapi_free_TraceEventInfoList(events
);
1117 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1118 bool has_port
, int64_t port
,
1119 bool has_tls_port
, int64_t tls_port
,
1120 bool has_cert_subject
, const char *cert_subject
,
1123 if (strcmp(protocol
, "spice") == 0) {
1124 if (!qemu_using_spice(errp
)) {
1128 if (!has_port
&& !has_tls_port
) {
1129 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1133 if (qemu_spice_migrate_info(hostname
,
1134 has_port
? port
: -1,
1135 has_tls_port
? tls_port
: -1,
1137 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1143 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1146 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1150 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1152 error_report_err(err
);
1156 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1159 const char *items
= qdict_get_str(qdict
, "items");
1161 if (!strcmp(items
, "none")) {
1164 mask
= qemu_str_to_log_mask(items
);
1166 help_cmd(mon
, "log");
1173 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1175 const char *option
= qdict_get_try_str(qdict
, "option");
1176 if (!option
|| !strcmp(option
, "on")) {
1178 } else if (!strcmp(option
, "off")) {
1181 monitor_printf(mon
, "unexpected option %s\n", option
);
1185 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1187 const char *device
= qdict_get_try_str(qdict
, "device");
1189 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1190 if (gdbserver_start(device
) < 0) {
1191 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1193 } else if (strcmp(device
, "none") == 0) {
1194 monitor_printf(mon
, "Disabled gdbserver\n");
1196 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1201 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1203 const char *action
= qdict_get_str(qdict
, "action");
1204 if (select_watchdog_action(action
) == -1) {
1205 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1209 static void monitor_printc(Monitor
*mon
, int c
)
1211 monitor_printf(mon
, "'");
1214 monitor_printf(mon
, "\\'");
1217 monitor_printf(mon
, "\\\\");
1220 monitor_printf(mon
, "\\n");
1223 monitor_printf(mon
, "\\r");
1226 if (c
>= 32 && c
<= 126) {
1227 monitor_printf(mon
, "%c", c
);
1229 monitor_printf(mon
, "\\x%02x", c
);
1233 monitor_printf(mon
, "'");
1236 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1237 hwaddr addr
, int is_physical
)
1239 int l
, line_size
, i
, max_digits
, len
;
1243 if (format
== 'i') {
1246 CPUArchState
*env
= mon_get_cpu_env();
1249 } else if (wsize
== 4) {
1252 /* as default we use the current CS size */
1255 #ifdef TARGET_X86_64
1256 if ((env
->efer
& MSR_EFER_LMA
) &&
1257 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1261 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1267 CPUArchState
*env
= mon_get_cpu_env();
1268 flags
= msr_le
<< 16;
1269 flags
|= env
->bfd_mach
;
1271 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1275 len
= wsize
* count
;
1284 max_digits
= (wsize
* 8 + 2) / 3;
1288 max_digits
= (wsize
* 8) / 4;
1292 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1301 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1303 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1308 cpu_physical_memory_read(addr
, buf
, l
);
1310 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1311 monitor_printf(mon
, " Cannot access memory\n");
1320 v
= ldub_p(buf
+ i
);
1323 v
= lduw_p(buf
+ i
);
1326 v
= (uint32_t)ldl_p(buf
+ i
);
1332 monitor_printf(mon
, " ");
1335 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1338 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1341 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1344 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1347 monitor_printc(mon
, v
);
1352 monitor_printf(mon
, "\n");
1358 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1360 int count
= qdict_get_int(qdict
, "count");
1361 int format
= qdict_get_int(qdict
, "format");
1362 int size
= qdict_get_int(qdict
, "size");
1363 target_long addr
= qdict_get_int(qdict
, "addr");
1365 memory_dump(mon
, count
, format
, size
, addr
, 0);
1368 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1370 int count
= qdict_get_int(qdict
, "count");
1371 int format
= qdict_get_int(qdict
, "format");
1372 int size
= qdict_get_int(qdict
, "size");
1373 hwaddr addr
= qdict_get_int(qdict
, "addr");
1375 memory_dump(mon
, count
, format
, size
, addr
, 1);
1378 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1380 int format
= qdict_get_int(qdict
, "format");
1381 hwaddr val
= qdict_get_int(qdict
, "val");
1385 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1388 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1391 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1395 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1398 monitor_printc(mon
, val
);
1401 monitor_printf(mon
, "\n");
1404 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1408 uint32_t start
= qdict_get_int(qdict
, "start");
1409 uint32_t size
= qdict_get_int(qdict
, "size");
1412 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1413 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1414 MEMTXATTRS_UNSPECIFIED
, NULL
);
1415 /* BSD sum algorithm ('sum' Unix command) */
1416 sum
= (sum
>> 1) | (sum
<< 15);
1419 monitor_printf(mon
, "%05d\n", sum
);
1422 static int mouse_button_state
;
1424 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1426 int dx
, dy
, dz
, button
;
1427 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1428 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1429 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1431 dx
= strtol(dx_str
, NULL
, 0);
1432 dy
= strtol(dy_str
, NULL
, 0);
1433 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1434 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1437 dz
= strtol(dz_str
, NULL
, 0);
1439 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1440 qemu_input_queue_btn(NULL
, button
, true);
1441 qemu_input_event_sync();
1442 qemu_input_queue_btn(NULL
, button
, false);
1445 qemu_input_event_sync();
1448 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1450 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1451 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1452 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1453 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1455 int button_state
= qdict_get_int(qdict
, "button_state");
1457 if (mouse_button_state
== button_state
) {
1460 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1461 qemu_input_event_sync();
1462 mouse_button_state
= button_state
;
1465 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1467 int size
= qdict_get_int(qdict
, "size");
1468 int addr
= qdict_get_int(qdict
, "addr");
1469 int has_index
= qdict_haskey(qdict
, "index");
1474 int index
= qdict_get_int(qdict
, "index");
1475 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1483 val
= cpu_inb(addr
);
1487 val
= cpu_inw(addr
);
1491 val
= cpu_inl(addr
);
1495 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1496 suffix
, addr
, size
* 2, val
);
1499 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1501 int size
= qdict_get_int(qdict
, "size");
1502 int addr
= qdict_get_int(qdict
, "addr");
1503 int val
= qdict_get_int(qdict
, "val");
1505 addr
&= IOPORTS_MASK
;
1510 cpu_outb(addr
, val
);
1513 cpu_outw(addr
, val
);
1516 cpu_outl(addr
, val
);
1521 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1523 Error
*local_err
= NULL
;
1524 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1526 qemu_boot_set(bootdevice
, &local_err
);
1528 error_report_err(local_err
);
1530 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1534 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1536 mtree_info((fprintf_function
)monitor_printf
, mon
);
1539 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1545 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1546 query_numa_node_mem(node_mem
);
1547 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1548 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1549 monitor_printf(mon
, "node %d cpus:", i
);
1551 if (cpu
->numa_node
== i
) {
1552 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1555 monitor_printf(mon
, "\n");
1556 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1562 #ifdef CONFIG_PROFILER
1567 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1569 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1570 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1571 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1572 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1577 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1579 monitor_printf(mon
, "Internal profiler not compiled\n");
1583 /* Capture support */
1584 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1586 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1591 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1592 monitor_printf(mon
, "[%d]: ", i
);
1593 s
->ops
.info (s
->opaque
);
1597 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1600 int n
= qdict_get_int(qdict
, "n");
1603 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1605 s
->ops
.destroy (s
->opaque
);
1606 QLIST_REMOVE (s
, entries
);
1613 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1615 const char *path
= qdict_get_str(qdict
, "path");
1616 int has_freq
= qdict_haskey(qdict
, "freq");
1617 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1618 int has_bits
= qdict_haskey(qdict
, "bits");
1619 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1620 int has_channels
= qdict_haskey(qdict
, "nchannels");
1621 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1624 s
= g_malloc0 (sizeof (*s
));
1626 freq
= has_freq
? freq
: 44100;
1627 bits
= has_bits
? bits
: 16;
1628 nchannels
= has_channels
? nchannels
: 2;
1630 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1631 monitor_printf(mon
, "Failed to add wave capture\n");
1635 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1638 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1640 qemu_acl
*acl
= qemu_acl_find(name
);
1643 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1648 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1650 const char *aclname
= qdict_get_str(qdict
, "aclname");
1651 qemu_acl
*acl
= find_acl(mon
, aclname
);
1652 qemu_acl_entry
*entry
;
1656 monitor_printf(mon
, "policy: %s\n",
1657 acl
->defaultDeny
? "deny" : "allow");
1658 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1660 monitor_printf(mon
, "%d: %s %s\n", i
,
1661 entry
->deny
? "deny" : "allow", entry
->match
);
1666 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1668 const char *aclname
= qdict_get_str(qdict
, "aclname");
1669 qemu_acl
*acl
= find_acl(mon
, aclname
);
1672 qemu_acl_reset(acl
);
1673 monitor_printf(mon
, "acl: removed all rules\n");
1677 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1679 const char *aclname
= qdict_get_str(qdict
, "aclname");
1680 const char *policy
= qdict_get_str(qdict
, "policy");
1681 qemu_acl
*acl
= find_acl(mon
, aclname
);
1684 if (strcmp(policy
, "allow") == 0) {
1685 acl
->defaultDeny
= 0;
1686 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1687 } else if (strcmp(policy
, "deny") == 0) {
1688 acl
->defaultDeny
= 1;
1689 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1691 monitor_printf(mon
, "acl: unknown policy '%s', "
1692 "expected 'deny' or 'allow'\n", policy
);
1697 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1699 const char *aclname
= qdict_get_str(qdict
, "aclname");
1700 const char *match
= qdict_get_str(qdict
, "match");
1701 const char *policy
= qdict_get_str(qdict
, "policy");
1702 int has_index
= qdict_haskey(qdict
, "index");
1703 int index
= qdict_get_try_int(qdict
, "index", -1);
1704 qemu_acl
*acl
= find_acl(mon
, aclname
);
1708 if (strcmp(policy
, "allow") == 0) {
1710 } else if (strcmp(policy
, "deny") == 0) {
1713 monitor_printf(mon
, "acl: unknown policy '%s', "
1714 "expected 'deny' or 'allow'\n", policy
);
1718 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1720 ret
= qemu_acl_append(acl
, deny
, match
);
1722 monitor_printf(mon
, "acl: unable to add acl entry\n");
1724 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1728 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1730 const char *aclname
= qdict_get_str(qdict
, "aclname");
1731 const char *match
= qdict_get_str(qdict
, "match");
1732 qemu_acl
*acl
= find_acl(mon
, aclname
);
1736 ret
= qemu_acl_remove(acl
, match
);
1738 monitor_printf(mon
, "acl: no matching acl entry\n");
1740 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1744 void qmp_getfd(const char *fdname
, Error
**errp
)
1749 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
.chr
);
1751 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1755 if (qemu_isdigit(fdname
[0])) {
1757 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1758 "a name not starting with a digit");
1762 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1763 if (strcmp(monfd
->name
, fdname
) != 0) {
1772 monfd
= g_malloc0(sizeof(mon_fd_t
));
1773 monfd
->name
= g_strdup(fdname
);
1776 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1779 void qmp_closefd(const char *fdname
, Error
**errp
)
1783 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1784 if (strcmp(monfd
->name
, fdname
) != 0) {
1788 QLIST_REMOVE(monfd
, next
);
1790 g_free(monfd
->name
);
1795 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1798 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1800 int saved_vm_running
= runstate_is_running();
1801 const char *name
= qdict_get_str(qdict
, "name");
1803 vm_stop(RUN_STATE_RESTORE_VM
);
1805 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1810 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1814 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1817 if (strcmp(monfd
->name
, fdname
) != 0) {
1823 /* caller takes ownership of fd */
1824 QLIST_REMOVE(monfd
, next
);
1825 g_free(monfd
->name
);
1831 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1835 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1837 MonFdsetFd
*mon_fdset_fd
;
1838 MonFdsetFd
*mon_fdset_fd_next
;
1840 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1841 if ((mon_fdset_fd
->removed
||
1842 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1843 runstate_is_running()) {
1844 close(mon_fdset_fd
->fd
);
1845 g_free(mon_fdset_fd
->opaque
);
1846 QLIST_REMOVE(mon_fdset_fd
, next
);
1847 g_free(mon_fdset_fd
);
1851 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1852 QLIST_REMOVE(mon_fdset
, next
);
1857 static void monitor_fdsets_cleanup(void)
1859 MonFdset
*mon_fdset
;
1860 MonFdset
*mon_fdset_next
;
1862 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1863 monitor_fdset_cleanup(mon_fdset
);
1867 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1868 const char *opaque
, Error
**errp
)
1871 Monitor
*mon
= cur_mon
;
1874 fd
= qemu_chr_fe_get_msgfd(mon
->chr
.chr
);
1876 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1880 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1881 has_opaque
, opaque
, errp
);
1893 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1895 MonFdset
*mon_fdset
;
1896 MonFdsetFd
*mon_fdset_fd
;
1899 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1900 if (mon_fdset
->id
!= fdset_id
) {
1903 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1905 if (mon_fdset_fd
->fd
!= fd
) {
1908 mon_fdset_fd
->removed
= true;
1911 mon_fdset_fd
->removed
= true;
1914 if (has_fd
&& !mon_fdset_fd
) {
1917 monitor_fdset_cleanup(mon_fdset
);
1923 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1926 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1928 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1931 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1933 MonFdset
*mon_fdset
;
1934 MonFdsetFd
*mon_fdset_fd
;
1935 FdsetInfoList
*fdset_list
= NULL
;
1937 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1938 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1939 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1941 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1942 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1944 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1945 FdsetFdInfoList
*fdsetfd_info
;
1947 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1948 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1949 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1950 if (mon_fdset_fd
->opaque
) {
1951 fdsetfd_info
->value
->has_opaque
= true;
1952 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1954 fdsetfd_info
->value
->has_opaque
= false;
1957 fdsetfd_info
->next
= fdsetfd_list
;
1958 fdsetfd_list
= fdsetfd_info
;
1961 fdset_info
->value
->fds
= fdsetfd_list
;
1963 fdset_info
->next
= fdset_list
;
1964 fdset_list
= fdset_info
;
1970 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1971 bool has_opaque
, const char *opaque
,
1974 MonFdset
*mon_fdset
= NULL
;
1975 MonFdsetFd
*mon_fdset_fd
;
1979 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1980 /* Break if match found or match impossible due to ordering by ID */
1981 if (fdset_id
<= mon_fdset
->id
) {
1982 if (fdset_id
< mon_fdset
->id
) {
1990 if (mon_fdset
== NULL
) {
1991 int64_t fdset_id_prev
= -1;
1992 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1996 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1997 "a non-negative value");
2000 /* Use specified fdset ID */
2001 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2002 mon_fdset_cur
= mon_fdset
;
2003 if (fdset_id
< mon_fdset_cur
->id
) {
2008 /* Use first available fdset ID */
2009 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2010 mon_fdset_cur
= mon_fdset
;
2011 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2012 fdset_id_prev
= mon_fdset_cur
->id
;
2019 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2021 mon_fdset
->id
= fdset_id
;
2023 mon_fdset
->id
= fdset_id_prev
+ 1;
2026 /* The fdset list is ordered by fdset ID */
2027 if (!mon_fdset_cur
) {
2028 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2029 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2030 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2032 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2036 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2037 mon_fdset_fd
->fd
= fd
;
2038 mon_fdset_fd
->removed
= false;
2040 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2042 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2044 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2045 fdinfo
->fdset_id
= mon_fdset
->id
;
2046 fdinfo
->fd
= mon_fdset_fd
->fd
;
2051 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2054 MonFdset
*mon_fdset
;
2055 MonFdsetFd
*mon_fdset_fd
;
2058 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2059 if (mon_fdset
->id
!= fdset_id
) {
2062 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2063 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2064 if (mon_fd_flags
== -1) {
2068 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2069 return mon_fdset_fd
->fd
;
2081 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2083 MonFdset
*mon_fdset
;
2084 MonFdsetFd
*mon_fdset_fd_dup
;
2086 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2087 if (mon_fdset
->id
!= fdset_id
) {
2090 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2091 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2095 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2096 mon_fdset_fd_dup
->fd
= dup_fd
;
2097 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2103 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2105 MonFdset
*mon_fdset
;
2106 MonFdsetFd
*mon_fdset_fd_dup
;
2108 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2109 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2110 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2112 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2113 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2114 monitor_fdset_cleanup(mon_fdset
);
2118 return mon_fdset
->id
;
2126 int monitor_fdset_dup_fd_find(int dup_fd
)
2128 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2131 void monitor_fdset_dup_fd_remove(int dup_fd
)
2133 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2136 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2139 Error
*local_err
= NULL
;
2141 if (!qemu_isdigit(fdname
[0]) && mon
) {
2142 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2144 fd
= qemu_parse_fd(fdname
);
2146 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2151 error_propagate(errp
, local_err
);
2160 /* Please update hmp-commands.hx when adding or changing commands */
2161 static mon_cmd_t info_cmds
[] = {
2162 #include "hmp-commands-info.h"
2166 /* mon_cmds and info_cmds would be sorted at runtime */
2167 static mon_cmd_t mon_cmds
[] = {
2168 #include "hmp-commands.h"
2172 /*******************************************************************/
2174 static const char *pch
;
2175 static sigjmp_buf expr_env
;
2178 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2179 expr_error(Monitor
*mon
, const char *fmt
, ...)
2183 monitor_vprintf(mon
, fmt
, ap
);
2184 monitor_printf(mon
, "\n");
2186 siglongjmp(expr_env
, 1);
2189 /* return 0 if OK, -1 if not found */
2190 static int get_monitor_def(target_long
*pval
, const char *name
)
2192 const MonitorDef
*md
= target_monitor_defs();
2201 for(; md
->name
!= NULL
; md
++) {
2202 if (compare_cmd(name
, md
->name
)) {
2203 if (md
->get_value
) {
2204 *pval
= md
->get_value(md
, md
->offset
);
2206 CPUArchState
*env
= mon_get_cpu_env();
2207 ptr
= (uint8_t *)env
+ md
->offset
;
2210 *pval
= *(int32_t *)ptr
;
2213 *pval
= *(target_long
*)ptr
;
2224 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2226 *pval
= (target_long
) tmp
;
2232 static void next(void)
2236 while (qemu_isspace(*pch
))
2241 static int64_t expr_sum(Monitor
*mon
);
2243 static int64_t expr_unary(Monitor
*mon
)
2252 n
= expr_unary(mon
);
2256 n
= -expr_unary(mon
);
2260 n
= ~expr_unary(mon
);
2266 expr_error(mon
, "')' expected");
2273 expr_error(mon
, "character constant expected");
2277 expr_error(mon
, "missing terminating \' character");
2287 while ((*pch
>= 'a' && *pch
<= 'z') ||
2288 (*pch
>= 'A' && *pch
<= 'Z') ||
2289 (*pch
>= '0' && *pch
<= '9') ||
2290 *pch
== '_' || *pch
== '.') {
2291 if ((q
- buf
) < sizeof(buf
) - 1)
2295 while (qemu_isspace(*pch
))
2298 ret
= get_monitor_def(®
, buf
);
2300 expr_error(mon
, "unknown register");
2305 expr_error(mon
, "unexpected end of expression");
2310 n
= strtoull(pch
, &p
, 0);
2311 if (errno
== ERANGE
) {
2312 expr_error(mon
, "number too large");
2315 expr_error(mon
, "invalid char '%c' in expression", *p
);
2318 while (qemu_isspace(*pch
))
2326 static int64_t expr_prod(Monitor
*mon
)
2331 val
= expr_unary(mon
);
2334 if (op
!= '*' && op
!= '/' && op
!= '%')
2337 val2
= expr_unary(mon
);
2346 expr_error(mon
, "division by zero");
2357 static int64_t expr_logic(Monitor
*mon
)
2362 val
= expr_prod(mon
);
2365 if (op
!= '&' && op
!= '|' && op
!= '^')
2368 val2
= expr_prod(mon
);
2385 static int64_t expr_sum(Monitor
*mon
)
2390 val
= expr_logic(mon
);
2393 if (op
!= '+' && op
!= '-')
2396 val2
= expr_logic(mon
);
2405 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2408 if (sigsetjmp(expr_env
, 0)) {
2412 while (qemu_isspace(*pch
))
2414 *pval
= expr_sum(mon
);
2419 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2421 const char *p
= *pp
;
2425 d
= strtod(p
, &tailp
);
2427 monitor_printf(mon
, "Number expected\n");
2430 if (d
!= d
|| d
- d
!= 0) {
2431 /* NaN or infinity */
2432 monitor_printf(mon
, "Bad number\n");
2441 * Store the command-name in cmdname, and return a pointer to
2442 * the remaining of the command string.
2444 static const char *get_command_name(const char *cmdline
,
2445 char *cmdname
, size_t nlen
)
2448 const char *p
, *pstart
;
2451 while (qemu_isspace(*p
))
2456 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2461 memcpy(cmdname
, pstart
, len
);
2462 cmdname
[len
] = '\0';
2467 * Read key of 'type' into 'key' and return the current
2470 static char *key_get_info(const char *type
, char **key
)
2478 p
= strchr(type
, ':');
2485 str
= g_malloc(len
+ 1);
2486 memcpy(str
, type
, len
);
2493 static int default_fmt_format
= 'x';
2494 static int default_fmt_size
= 4;
2496 static int is_valid_option(const char *c
, const char *typestr
)
2504 typestr
= strstr(typestr
, option
);
2505 return (typestr
!= NULL
);
2508 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2509 const char *cmdname
)
2511 const mon_cmd_t
*cmd
;
2513 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2514 if (compare_cmd(cmdname
, cmd
->name
)) {
2523 * Parse command name from @cmdp according to command table @table.
2524 * If blank, return NULL.
2525 * Else, if no valid command can be found, report to @mon, and return
2527 * Else, change @cmdp to point right behind the name, and return its
2528 * command table entry.
2529 * Do not assume the return value points into @table! It doesn't when
2530 * the command is found in a sub-command table.
2532 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2537 const mon_cmd_t
*cmd
;
2540 /* extract the command name */
2541 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2545 cmd
= search_dispatch_table(table
, cmdname
);
2547 monitor_printf(mon
, "unknown command: '%.*s'\n",
2548 (int)(p
- *cmdp
), *cmdp
);
2552 /* filter out following useless space */
2553 while (qemu_isspace(*p
)) {
2558 /* search sub command */
2559 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2560 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2567 * Parse arguments for @cmd.
2568 * If it can't be parsed, report to @mon, and return NULL.
2569 * Else, insert command arguments into a QDict, and return it.
2570 * Note: On success, caller has to free the QDict structure.
2573 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2575 const mon_cmd_t
*cmd
)
2577 const char *typestr
;
2580 const char *p
= *endp
;
2582 QDict
*qdict
= qdict_new();
2584 /* parse the parameters */
2585 typestr
= cmd
->args_type
;
2587 typestr
= key_get_info(typestr
, &key
);
2599 while (qemu_isspace(*p
))
2601 if (*typestr
== '?') {
2604 /* no optional string: NULL argument */
2608 ret
= get_str(buf
, sizeof(buf
), &p
);
2612 monitor_printf(mon
, "%s: filename expected\n",
2616 monitor_printf(mon
, "%s: block device name expected\n",
2620 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2625 qdict_put(qdict
, key
, qstring_from_str(buf
));
2630 QemuOptsList
*opts_list
;
2633 opts_list
= qemu_find_opts(key
);
2634 if (!opts_list
|| opts_list
->desc
->name
) {
2637 while (qemu_isspace(*p
)) {
2642 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2645 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2649 qemu_opts_to_qdict(opts
, qdict
);
2650 qemu_opts_del(opts
);
2655 int count
, format
, size
;
2657 while (qemu_isspace(*p
))
2663 if (qemu_isdigit(*p
)) {
2665 while (qemu_isdigit(*p
)) {
2666 count
= count
* 10 + (*p
- '0');
2704 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2705 monitor_printf(mon
, "invalid char in format: '%c'\n",
2710 format
= default_fmt_format
;
2711 if (format
!= 'i') {
2712 /* for 'i', not specifying a size gives -1 as size */
2714 size
= default_fmt_size
;
2715 default_fmt_size
= size
;
2717 default_fmt_format
= format
;
2720 format
= default_fmt_format
;
2721 if (format
!= 'i') {
2722 size
= default_fmt_size
;
2727 qdict_put(qdict
, "count", qint_from_int(count
));
2728 qdict_put(qdict
, "format", qint_from_int(format
));
2729 qdict_put(qdict
, "size", qint_from_int(size
));
2738 while (qemu_isspace(*p
))
2740 if (*typestr
== '?' || *typestr
== '.') {
2741 if (*typestr
== '?') {
2749 while (qemu_isspace(*p
))
2758 if (get_expr(mon
, &val
, &p
))
2760 /* Check if 'i' is greater than 32-bit */
2761 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2762 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2763 monitor_printf(mon
, "integer is for 32-bit values\n");
2765 } else if (c
== 'M') {
2767 monitor_printf(mon
, "enter a positive value\n");
2772 qdict_put(qdict
, key
, qint_from_int(val
));
2780 while (qemu_isspace(*p
)) {
2783 if (*typestr
== '?') {
2789 val
= qemu_strtosz(p
, &end
);
2791 monitor_printf(mon
, "invalid size\n");
2794 qdict_put(qdict
, key
, qint_from_int(val
));
2802 while (qemu_isspace(*p
))
2804 if (*typestr
== '?') {
2810 if (get_double(mon
, &val
, &p
) < 0) {
2813 if (p
[0] && p
[1] == 's') {
2816 val
/= 1e3
; p
+= 2; break;
2818 val
/= 1e6
; p
+= 2; break;
2820 val
/= 1e9
; p
+= 2; break;
2823 if (*p
&& !qemu_isspace(*p
)) {
2824 monitor_printf(mon
, "Unknown unit suffix\n");
2827 qdict_put(qdict
, key
, qfloat_from_double(val
));
2835 while (qemu_isspace(*p
)) {
2839 while (qemu_isgraph(*p
)) {
2842 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2844 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2847 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2850 qdict_put(qdict
, key
, qbool_from_bool(val
));
2855 const char *tmp
= p
;
2862 while (qemu_isspace(*p
))
2867 if(!is_valid_option(p
, typestr
)) {
2869 monitor_printf(mon
, "%s: unsupported option -%c\n",
2881 qdict_put(qdict
, key
, qbool_from_bool(true));
2888 /* package all remaining string */
2891 while (qemu_isspace(*p
)) {
2894 if (*typestr
== '?') {
2897 /* no remaining string: NULL argument */
2903 monitor_printf(mon
, "%s: string expected\n",
2907 qdict_put(qdict
, key
, qstring_from_str(p
));
2913 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2919 /* check that all arguments were parsed */
2920 while (qemu_isspace(*p
))
2923 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2936 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2939 const mon_cmd_t
*cmd
;
2941 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2946 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2948 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2953 cmd
->cmd(mon
, qdict
);
2957 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2959 const char *p
, *pstart
;
2968 p
= pstart
+ strlen(pstart
);
2970 if (len
> sizeof(cmd
) - 2)
2971 len
= sizeof(cmd
) - 2;
2972 memcpy(cmd
, pstart
, len
);
2974 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2975 readline_add_completion(mon
->rs
, cmd
);
2983 static void file_completion(Monitor
*mon
, const char *input
)
2988 char file
[1024], file_prefix
[1024];
2992 p
= strrchr(input
, '/');
2995 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2996 pstrcpy(path
, sizeof(path
), ".");
2998 input_path_len
= p
- input
+ 1;
2999 memcpy(path
, input
, input_path_len
);
3000 if (input_path_len
> sizeof(path
) - 1)
3001 input_path_len
= sizeof(path
) - 1;
3002 path
[input_path_len
] = '\0';
3003 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3006 ffs
= opendir(path
);
3015 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3019 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3020 memcpy(file
, input
, input_path_len
);
3021 if (input_path_len
< sizeof(file
))
3022 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3024 /* stat the file to find out if it's a directory.
3025 * In that case add a slash to speed up typing long paths
3027 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3028 pstrcat(file
, sizeof(file
), "/");
3030 readline_add_completion(mon
->rs
, file
);
3036 static const char *next_arg_type(const char *typestr
)
3038 const char *p
= strchr(typestr
, ':');
3039 return (p
!= NULL
? ++p
: typestr
);
3042 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3045 if (!str
|| !option
) {
3048 if (!strncmp(option
, str
, strlen(str
))) {
3049 readline_add_completion(rs
, option
);
3053 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3056 ChardevBackendInfoList
*list
, *start
;
3062 readline_set_completion_index(rs
, len
);
3064 start
= list
= qmp_query_chardev_backends(NULL
);
3066 const char *chr_name
= list
->value
->name
;
3068 if (!strncmp(chr_name
, str
, len
)) {
3069 readline_add_completion(rs
, chr_name
);
3073 qapi_free_ChardevBackendInfoList(start
);
3076 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3085 readline_set_completion_index(rs
, len
);
3086 for (i
= 0; NetClientDriver_lookup
[i
]; i
++) {
3087 add_completion_option(rs
, str
, NetClientDriver_lookup
[i
]);
3091 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3101 readline_set_completion_index(rs
, len
);
3102 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3105 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3107 name
= object_class_get_name(OBJECT_CLASS(dc
));
3109 if (!dc
->cannot_instantiate_with_device_add_yet
3110 && !strncmp(name
, str
, len
)) {
3111 readline_add_completion(rs
, name
);
3118 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3128 readline_set_completion_index(rs
, len
);
3129 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3133 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3134 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3135 readline_add_completion(rs
, name
);
3142 static void peripheral_device_del_completion(ReadLineState
*rs
,
3143 const char *str
, size_t len
)
3145 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3146 GSList
*list
, *item
;
3148 list
= qdev_build_hotpluggable_device_list(peripheral
);
3153 for (item
= list
; item
; item
= g_slist_next(item
)) {
3154 DeviceState
*dev
= item
->data
;
3156 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3157 readline_add_completion(rs
, dev
->id
);
3164 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3167 ChardevInfoList
*list
, *start
;
3173 readline_set_completion_index(rs
, len
);
3175 start
= list
= qmp_query_chardev(NULL
);
3177 ChardevInfo
*chr
= list
->value
;
3179 if (!strncmp(chr
->label
, str
, len
)) {
3180 readline_add_completion(rs
, chr
->label
);
3184 qapi_free_ChardevInfoList(start
);
3187 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3190 ChardevInfoList
*list
, *start
;
3193 readline_set_completion_index(rs
, len
);
3195 start
= list
= qmp_query_chardev(NULL
);
3197 ChardevInfo
*chr_info
= list
->value
;
3199 if (!strncmp(chr_info
->label
, str
, len
)) {
3200 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3201 if (chr
&& chr_is_ringbuf(chr
)) {
3202 readline_add_completion(rs
, chr_info
->label
);
3207 qapi_free_ChardevInfoList(start
);
3210 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3215 ringbuf_completion(rs
, str
);
3218 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3227 readline_set_completion_index(rs
, len
);
3228 peripheral_device_del_completion(rs
, str
, len
);
3231 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3233 ObjectPropertyInfoList
*list
, *start
;
3240 readline_set_completion_index(rs
, len
);
3242 start
= list
= qmp_qom_list("/objects", NULL
);
3244 ObjectPropertyInfo
*info
= list
->value
;
3246 if (!strncmp(info
->type
, "child<", 5)
3247 && !strncmp(info
->name
, str
, len
)) {
3248 readline_add_completion(rs
, info
->name
);
3252 qapi_free_ObjectPropertyInfoList(start
);
3255 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3264 sep
= strrchr(str
, '-');
3269 readline_set_completion_index(rs
, len
);
3270 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3271 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3272 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3277 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3282 readline_set_completion_index(rs
, len
);
3284 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3286 count
= qemu_find_net_clients_except(NULL
, ncs
,
3287 NET_CLIENT_DRIVER_NONE
,
3289 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3290 const char *name
= ncs
[i
]->name
;
3291 if (!strncmp(str
, name
, len
)) {
3292 readline_add_completion(rs
, name
);
3295 } else if (nb_args
== 3) {
3296 add_completion_option(rs
, str
, "on");
3297 add_completion_option(rs
, str
, "off");
3301 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3304 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3311 readline_set_completion_index(rs
, len
);
3312 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3314 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3316 const char *name
= ncs
[i
]->name
;
3317 if (strncmp(str
, name
, len
)) {
3320 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3322 readline_add_completion(rs
, name
);
3327 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3332 readline_set_completion_index(rs
, len
);
3334 TraceEventIter iter
;
3336 char *pattern
= g_strdup_printf("%s*", str
);
3337 trace_event_iter_init(&iter
, pattern
);
3338 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3339 readline_add_completion(rs
, trace_event_get_name(ev
));
3345 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3350 readline_set_completion_index(rs
, len
);
3352 TraceEventIter iter
;
3354 char *pattern
= g_strdup_printf("%s*", str
);
3355 trace_event_iter_init(&iter
, pattern
);
3356 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3357 readline_add_completion(rs
, trace_event_get_name(ev
));
3360 } else if (nb_args
== 3) {
3361 add_completion_option(rs
, str
, "on");
3362 add_completion_option(rs
, str
, "off");
3366 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3373 readline_set_completion_index(rs
, strlen(str
));
3374 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3375 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3379 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3385 readline_set_completion_index(rs
, len
);
3388 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3389 const char *name
= MigrationCapability_lookup
[i
];
3390 if (!strncmp(str
, name
, len
)) {
3391 readline_add_completion(rs
, name
);
3394 } else if (nb_args
== 3) {
3395 add_completion_option(rs
, str
, "on");
3396 add_completion_option(rs
, str
, "off");
3400 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3406 readline_set_completion_index(rs
, len
);
3409 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3410 const char *name
= MigrationParameter_lookup
[i
];
3411 if (!strncmp(str
, name
, len
)) {
3412 readline_add_completion(rs
, name
);
3418 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3426 readline_set_completion_index(rs
, len
);
3427 for (i
= 0; host_net_devices
[i
]; i
++) {
3428 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3429 readline_add_completion(rs
, host_net_devices
[i
]);
3434 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3436 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3440 readline_set_completion_index(rs
, len
);
3442 count
= qemu_find_net_clients_except(NULL
, ncs
,
3443 NET_CLIENT_DRIVER_NONE
,
3445 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3449 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3452 snprintf(name
, sizeof(name
), "%d", id
);
3453 if (!strncmp(str
, name
, len
)) {
3454 readline_add_completion(rs
, name
);
3458 } else if (nb_args
== 3) {
3459 count
= qemu_find_net_clients_except(NULL
, ncs
,
3460 NET_CLIENT_DRIVER_NIC
,
3462 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3466 if (ncs
[i
]->info
->type
== NET_CLIENT_DRIVER_HUBPORT
||
3467 net_hub_id_for_client(ncs
[i
], &id
)) {
3470 name
= ncs
[i
]->name
;
3471 if (!strncmp(str
, name
, len
)) {
3472 readline_add_completion(rs
, name
);
3479 static void vm_completion(ReadLineState
*rs
, const char *str
)
3482 BlockDriverState
*bs
;
3483 BdrvNextIterator it
;
3486 readline_set_completion_index(rs
, len
);
3488 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3489 SnapshotInfoList
*snapshots
, *snapshot
;
3490 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3493 aio_context_acquire(ctx
);
3494 if (bdrv_can_snapshot(bs
)) {
3495 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3497 aio_context_release(ctx
);
3502 snapshot
= snapshots
;
3504 char *completion
= snapshot
->value
->name
;
3505 if (!strncmp(str
, completion
, len
)) {
3506 readline_add_completion(rs
, completion
);
3508 completion
= snapshot
->value
->id
;
3509 if (!strncmp(str
, completion
, len
)) {
3510 readline_add_completion(rs
, completion
);
3512 snapshot
= snapshot
->next
;
3514 qapi_free_SnapshotInfoList(snapshots
);
3519 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3522 vm_completion(rs
, str
);
3526 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3529 vm_completion(rs
, str
);
3533 static void monitor_find_completion_by_table(Monitor
*mon
,
3534 const mon_cmd_t
*cmd_table
,
3538 const char *cmdname
;
3540 const char *ptype
, *str
, *name
;
3541 const mon_cmd_t
*cmd
;
3542 BlockBackend
*blk
= NULL
;
3545 /* command completion */
3550 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3551 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3552 cmd_completion(mon
, cmdname
, cmd
->name
);
3555 /* find the command */
3556 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3557 if (compare_cmd(args
[0], cmd
->name
)) {
3565 if (cmd
->sub_table
) {
3566 /* do the job again */
3567 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3568 &args
[1], nb_args
- 1);
3571 if (cmd
->command_completion
) {
3572 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3576 ptype
= next_arg_type(cmd
->args_type
);
3577 for(i
= 0; i
< nb_args
- 2; i
++) {
3578 if (*ptype
!= '\0') {
3579 ptype
= next_arg_type(ptype
);
3580 while (*ptype
== '?')
3581 ptype
= next_arg_type(ptype
);
3584 str
= args
[nb_args
- 1];
3585 while (*ptype
== '-' && ptype
[1] != '\0') {
3586 ptype
= next_arg_type(ptype
);
3590 /* file completion */
3591 readline_set_completion_index(mon
->rs
, strlen(str
));
3592 file_completion(mon
, str
);
3595 /* block device name completion */
3596 readline_set_completion_index(mon
->rs
, strlen(str
));
3597 while ((blk
= blk_next(blk
)) != NULL
) {
3598 name
= blk_name(blk
);
3599 if (str
[0] == '\0' ||
3600 !strncmp(name
, str
, strlen(str
))) {
3601 readline_add_completion(mon
->rs
, name
);
3607 if (!strcmp(cmd
->name
, "help|?")) {
3608 monitor_find_completion_by_table(mon
, cmd_table
,
3609 &args
[1], nb_args
- 1);
3618 static void monitor_find_completion(void *opaque
,
3619 const char *cmdline
)
3621 Monitor
*mon
= opaque
;
3622 char *args
[MAX_ARGS
];
3625 /* 1. parse the cmdline */
3626 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3630 /* if the line ends with a space, it means we want to complete the
3632 len
= strlen(cmdline
);
3633 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3634 if (nb_args
>= MAX_ARGS
) {
3637 args
[nb_args
++] = g_strdup("");
3640 /* 2. auto complete according to args */
3641 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3644 free_cmdline_args(args
, nb_args
);
3647 static int monitor_can_read(void *opaque
)
3649 Monitor
*mon
= opaque
;
3651 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3654 static bool invalid_qmp_mode(const Monitor
*mon
, const char *cmd
,
3657 bool is_cap
= g_str_equal(cmd
, "qmp_capabilities");
3659 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3660 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3661 "Capabilities negotiation is already complete, command "
3662 "'%s' ignored", cmd
);
3665 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3666 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3667 "Expecting capabilities negotiation with "
3668 "'qmp_capabilities' before command '%s'", cmd
);
3675 * Input object checking rules
3677 * 1. Input object must be a dict
3678 * 2. The "execute" key must exist
3679 * 3. The "execute" key must be a string
3680 * 4. If the "arguments" key exists, it must be a dict
3681 * 5. If the "id" key exists, it can be anything (ie. json-value)
3682 * 6. Any argument not listed above is considered invalid
3684 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3686 const QDictEntry
*ent
;
3687 int has_exec_key
= 0;
3690 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3691 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3695 input_dict
= qobject_to_qdict(input_obj
);
3697 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3698 const char *arg_name
= qdict_entry_key(ent
);
3699 const QObject
*arg_obj
= qdict_entry_value(ent
);
3701 if (!strcmp(arg_name
, "execute")) {
3702 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3703 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3704 "execute", "string");
3708 } else if (!strcmp(arg_name
, "arguments")) {
3709 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3710 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3711 "arguments", "object");
3714 } else if (!strcmp(arg_name
, "id")) {
3715 /* Any string is acceptable as "id", so nothing to check */
3717 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3722 if (!has_exec_key
) {
3723 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3730 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3732 QObject
*req
, *rsp
= NULL
, *id
= NULL
;
3733 QDict
*qdict
= NULL
;
3734 const char *cmd_name
;
3735 Monitor
*mon
= cur_mon
;
3738 req
= json_parser_parse_err(tokens
, NULL
, &err
);
3739 if (err
|| !req
|| qobject_type(req
) != QTYPE_QDICT
) {
3741 error_setg(&err
, QERR_JSON_PARSING
);
3746 qdict
= qmp_check_input_obj(req
, &err
);
3751 id
= qdict_get(qdict
, "id");
3753 qdict_del(qdict
, "id");
3755 cmd_name
= qdict_get_str(qdict
, "execute");
3756 trace_handle_qmp_command(mon
, cmd_name
);
3758 if (invalid_qmp_mode(mon
, cmd_name
, &err
)) {
3762 rsp
= qmp_dispatch(req
);
3766 qdict
= qdict_new();
3767 qdict_put_obj(qdict
, "error", qmp_build_error_object(err
));
3769 rsp
= QOBJECT(qdict
);
3774 qdict_put_obj(qobject_to_qdict(rsp
), "id", id
);
3778 monitor_json_emitter(mon
, rsp
);
3782 qobject_decref(rsp
);
3783 qobject_decref(req
);
3786 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3788 Monitor
*old_mon
= cur_mon
;
3792 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3797 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3799 Monitor
*old_mon
= cur_mon
;
3805 for (i
= 0; i
< size
; i
++)
3806 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3808 if (size
== 0 || buf
[size
- 1] != 0)
3809 monitor_printf(cur_mon
, "corrupted command\n");
3811 handle_hmp_command(cur_mon
, (char *)buf
);
3817 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3818 void *readline_opaque
)
3820 Monitor
*mon
= opaque
;
3822 monitor_suspend(mon
);
3823 handle_hmp_command(mon
, cmdline
);
3824 monitor_resume(mon
);
3827 int monitor_suspend(Monitor
*mon
)
3835 void monitor_resume(Monitor
*mon
)
3839 if (--mon
->suspend_cnt
== 0)
3840 readline_show_prompt(mon
->rs
);
3843 static QObject
*get_qmp_greeting(void)
3845 QObject
*ver
= NULL
;
3847 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3849 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3853 static void monitor_qmp_event(void *opaque
, int event
)
3856 Monitor
*mon
= opaque
;
3859 case CHR_EVENT_OPENED
:
3860 mon
->qmp
.in_command_mode
= false;
3861 data
= get_qmp_greeting();
3862 monitor_json_emitter(mon
, data
);
3863 qobject_decref(data
);
3866 case CHR_EVENT_CLOSED
:
3867 json_message_parser_destroy(&mon
->qmp
.parser
);
3868 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
3870 monitor_fdsets_cleanup();
3875 static void monitor_event(void *opaque
, int event
)
3877 Monitor
*mon
= opaque
;
3880 case CHR_EVENT_MUX_IN
:
3881 qemu_mutex_lock(&mon
->out_lock
);
3883 qemu_mutex_unlock(&mon
->out_lock
);
3884 if (mon
->reset_seen
) {
3885 readline_restart(mon
->rs
);
3886 monitor_resume(mon
);
3889 mon
->suspend_cnt
= 0;
3893 case CHR_EVENT_MUX_OUT
:
3894 if (mon
->reset_seen
) {
3895 if (mon
->suspend_cnt
== 0) {
3896 monitor_printf(mon
, "\n");
3899 monitor_suspend(mon
);
3903 qemu_mutex_lock(&mon
->out_lock
);
3905 qemu_mutex_unlock(&mon
->out_lock
);
3908 case CHR_EVENT_OPENED
:
3909 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3910 "information\n", QEMU_VERSION
);
3911 if (!mon
->mux_out
) {
3912 readline_restart(mon
->rs
);
3913 readline_show_prompt(mon
->rs
);
3915 mon
->reset_seen
= 1;
3919 case CHR_EVENT_CLOSED
:
3921 monitor_fdsets_cleanup();
3927 compare_mon_cmd(const void *a
, const void *b
)
3929 return strcmp(((const mon_cmd_t
*)a
)->name
,
3930 ((const mon_cmd_t
*)b
)->name
);
3933 static void sortcmdlist(void)
3936 int elem_size
= sizeof(mon_cmd_t
);
3938 array_num
= sizeof(mon_cmds
)/elem_size
-1;
3939 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
3941 array_num
= sizeof(info_cmds
)/elem_size
-1;
3942 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
3945 /* These functions just adapt the readline interface in a typesafe way. We
3946 * could cast function pointers but that discards compiler checks.
3948 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
3949 const char *fmt
, ...)
3953 monitor_vprintf(opaque
, fmt
, ap
);
3957 static void monitor_readline_flush(void *opaque
)
3959 monitor_flush(opaque
);
3962 static void __attribute__((constructor
)) monitor_lock_init(void)
3964 qemu_mutex_init(&monitor_lock
);
3967 void monitor_init(CharDriverState
*chr
, int flags
)
3969 static int is_first_init
= 1;
3972 if (is_first_init
) {
3973 monitor_qapi_event_init();
3978 mon
= g_malloc(sizeof(*mon
));
3979 monitor_data_init(mon
);
3981 qemu_chr_fe_init(&mon
->chr
, chr
, &error_abort
);
3983 if (flags
& MONITOR_USE_READLINE
) {
3984 mon
->rs
= readline_init(monitor_readline_printf
,
3985 monitor_readline_flush
,
3987 monitor_find_completion
);
3988 monitor_read_command(mon
, 0);
3991 if (monitor_is_qmp(mon
)) {
3992 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
3993 monitor_qmp_event
, mon
);
3994 qemu_chr_fe_set_echo(chr
, true);
3995 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
3997 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
3998 monitor_event
, mon
);
4001 qemu_mutex_lock(&monitor_lock
);
4002 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4003 qemu_mutex_unlock(&monitor_lock
);
4006 void monitor_cleanup(void)
4008 Monitor
*mon
, *next
;
4010 qemu_mutex_lock(&monitor_lock
);
4011 QLIST_FOREACH_SAFE(mon
, &mon_list
, entry
, next
) {
4012 QLIST_REMOVE(mon
, entry
);
4013 monitor_data_destroy(mon
);
4016 qemu_mutex_unlock(&monitor_lock
);
4019 static void bdrv_password_cb(void *opaque
, const char *password
,
4020 void *readline_opaque
)
4022 Monitor
*mon
= opaque
;
4023 BlockDriverState
*bs
= readline_opaque
;
4025 Error
*local_err
= NULL
;
4027 bdrv_add_key(bs
, password
, &local_err
);
4029 error_report_err(local_err
);
4032 if (mon
->password_completion_cb
)
4033 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4035 monitor_read_command(mon
, 1);
4038 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4039 BlockCompletionFunc
*completion_cb
,
4044 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4045 bdrv_get_encrypted_filename(bs
));
4047 mon
->password_completion_cb
= completion_cb
;
4048 mon
->password_opaque
= opaque
;
4050 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4052 if (err
&& completion_cb
)
4053 completion_cb(opaque
, err
);
4058 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4059 BlockCompletionFunc
*completion_cb
,
4065 blk
= blk_by_name(device
);
4067 monitor_printf(mon
, "Device not found %s\n", device
);
4071 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4075 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4078 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4081 if (completion_cb
) {
4082 completion_cb(opaque
, 0);
4087 QemuOptsList qemu_mon_opts
= {
4089 .implied_opt_name
= "chardev",
4090 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4094 .type
= QEMU_OPT_STRING
,
4097 .type
= QEMU_OPT_STRING
,
4100 .type
= QEMU_OPT_BOOL
,
4103 .type
= QEMU_OPT_BOOL
,
4105 { /* end of list */ }
4110 void qmp_rtc_reset_reinjection(Error
**errp
)
4112 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4116 #ifndef TARGET_S390X
4117 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4119 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4124 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4126 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");
4131 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4133 MachineState
*ms
= MACHINE(qdev_get_machine());
4134 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4136 if (!mc
->query_hotpluggable_cpus
) {
4137 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4141 return mc
->query_hotpluggable_cpus(ms
);