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
; /* Throttling state for this event type and... */
185 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
186 QEMUTimer
*timer
; /* Timer for handling delayed events */
187 QDict
*qdict
; /* Delayed event (if any) */
188 } MonitorQAPIEventState
;
191 int64_t rate
; /* Minimum time (in ns) between two events */
192 } MonitorQAPIEventConf
;
195 CharDriverState
*chr
;
205 /* Read under either BQL or out_lock, written with BQL+out_lock. */
211 BlockCompletionFunc
*password_completion_cb
;
212 void *password_opaque
;
213 mon_cmd_t
*cmd_table
;
214 QLIST_HEAD(,mon_fd_t
) fds
;
215 QLIST_ENTRY(Monitor
) entry
;
218 /* QMP checker flags */
219 #define QMP_ACCEPT_UNKNOWNS 1
221 /* Protects mon_list, monitor_event_state. */
222 static QemuMutex monitor_lock
;
224 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
225 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
226 static int mon_refcount
;
228 static mon_cmd_t mon_cmds
[];
229 static mon_cmd_t info_cmds
[];
231 static const mon_cmd_t qmp_cmds
[];
235 static void monitor_command_cb(void *opaque
, const char *cmdline
,
236 void *readline_opaque
);
239 * Is @mon a QMP monitor?
241 static inline bool monitor_is_qmp(const Monitor
*mon
)
243 return (mon
->flags
& MONITOR_USE_CONTROL
);
247 * Is the current monitor, if any, a QMP monitor?
249 bool monitor_cur_is_qmp(void)
251 return cur_mon
&& monitor_is_qmp(cur_mon
);
254 void monitor_read_command(Monitor
*mon
, int show_prompt
)
259 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
261 readline_show_prompt(mon
->rs
);
264 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
268 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
269 /* prompt is printed on return from the command handler */
272 monitor_printf(mon
, "terminal does not support password prompting\n");
277 static void monitor_flush_locked(Monitor
*mon
);
279 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
282 Monitor
*mon
= opaque
;
284 qemu_mutex_lock(&mon
->out_lock
);
286 monitor_flush_locked(mon
);
287 qemu_mutex_unlock(&mon
->out_lock
);
291 /* Called with mon->out_lock held. */
292 static void monitor_flush_locked(Monitor
*mon
)
298 if (mon
->skip_flush
) {
302 buf
= qstring_get_str(mon
->outbuf
);
303 len
= qstring_get_length(mon
->outbuf
);
305 if (len
&& !mon
->mux_out
) {
306 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
307 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
308 /* all flushed or error */
309 QDECREF(mon
->outbuf
);
310 mon
->outbuf
= qstring_new();
315 QString
*tmp
= qstring_from_str(buf
+ rc
);
316 QDECREF(mon
->outbuf
);
319 if (mon
->out_watch
== 0) {
320 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
321 monitor_unblocked
, mon
);
326 void monitor_flush(Monitor
*mon
)
328 qemu_mutex_lock(&mon
->out_lock
);
329 monitor_flush_locked(mon
);
330 qemu_mutex_unlock(&mon
->out_lock
);
333 /* flush at every end of line */
334 static void monitor_puts(Monitor
*mon
, const char *str
)
338 qemu_mutex_lock(&mon
->out_lock
);
344 qstring_append_chr(mon
->outbuf
, '\r');
346 qstring_append_chr(mon
->outbuf
, c
);
348 monitor_flush_locked(mon
);
351 qemu_mutex_unlock(&mon
->out_lock
);
354 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
361 if (monitor_is_qmp(mon
)) {
365 buf
= g_strdup_vprintf(fmt
, ap
);
366 monitor_puts(mon
, buf
);
370 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
374 monitor_vprintf(mon
, fmt
, ap
);
378 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
382 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
387 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
391 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
392 qobject_to_json(data
);
393 assert(json
!= NULL
);
395 qstring_append_chr(json
, '\n');
396 monitor_puts(mon
, qstring_get_str(json
));
401 static QDict
*build_qmp_error_dict(Error
*err
)
405 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
406 ErrorClass_lookup
[error_get_class(err
)],
407 error_get_pretty(err
));
409 return qobject_to_qdict(obj
);
412 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
417 trace_monitor_protocol_emitter(mon
);
420 /* success response */
423 qobject_incref(data
);
424 qdict_put_obj(qmp
, "return", data
);
426 /* return an empty QDict by default */
427 qdict_put(qmp
, "return", qdict_new());
431 qmp
= build_qmp_error_dict(err
);
435 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
439 monitor_json_emitter(mon
, QOBJECT(qmp
));
444 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT_MAX
] = {
445 /* Limit guest-triggerable events to 1 per second */
446 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
447 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
448 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
449 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
450 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
451 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
454 GHashTable
*monitor_qapi_event_state
;
457 * Emits the event to every monitor instance, @event is only used for trace
458 * Called with monitor_lock held.
460 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
464 trace_monitor_protocol_event_emit(event
, qdict
);
465 QLIST_FOREACH(mon
, &mon_list
, entry
) {
466 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
467 monitor_json_emitter(mon
, QOBJECT(qdict
));
472 static void monitor_qapi_event_handler(void *opaque
);
475 * Queue a new event for emission to Monitor instances,
476 * applying any rate limiting if required.
479 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
481 MonitorQAPIEventConf
*evconf
;
482 MonitorQAPIEventState
*evstate
;
484 assert(event
< QAPI_EVENT_MAX
);
485 evconf
= &monitor_qapi_event_conf
[event
];
486 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
488 qemu_mutex_lock(&monitor_lock
);
491 /* Unthrottled event */
492 monitor_qapi_event_emit(event
, qdict
);
494 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
495 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
497 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
498 assert(!evstate
|| timer_pending(evstate
->timer
));
502 * Timer is pending for (at least) evconf->rate ns after
503 * last send. Store event for sending when timer fires,
504 * replacing a prior stored event if any.
506 QDECREF(evstate
->qdict
);
507 evstate
->qdict
= qdict
;
508 QINCREF(evstate
->qdict
);
511 * Last send was (at least) evconf->rate ns ago.
512 * Send immediately, and arm the timer to call
513 * monitor_qapi_event_handler() in evconf->rate ns. Any
514 * events arriving before then will be delayed until then.
516 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
518 monitor_qapi_event_emit(event
, qdict
);
520 evstate
= g_new(MonitorQAPIEventState
, 1);
521 evstate
->event
= event
;
522 evstate
->data
= data
;
523 QINCREF(evstate
->data
);
524 evstate
->qdict
= NULL
;
525 evstate
->timer
= timer_new_ns(QEMU_CLOCK_REALTIME
,
526 monitor_qapi_event_handler
,
528 g_hash_table_add(monitor_qapi_event_state
, evstate
);
529 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
533 qemu_mutex_unlock(&monitor_lock
);
537 * This function runs evconf->rate ns after sending a throttled
539 * If another event has since been stored, send it.
541 static void monitor_qapi_event_handler(void *opaque
)
543 MonitorQAPIEventState
*evstate
= opaque
;
544 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
546 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
547 qemu_mutex_lock(&monitor_lock
);
549 if (evstate
->qdict
) {
550 int64_t now
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
552 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
553 QDECREF(evstate
->qdict
);
554 evstate
->qdict
= NULL
;
555 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
557 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
558 QDECREF(evstate
->data
);
559 timer_free(evstate
->timer
);
563 qemu_mutex_unlock(&monitor_lock
);
566 static unsigned int qapi_event_throttle_hash(const void *key
)
568 const MonitorQAPIEventState
*evstate
= key
;
569 unsigned int hash
= evstate
->event
* 255;
571 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
572 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
578 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
580 const MonitorQAPIEventState
*eva
= a
;
581 const MonitorQAPIEventState
*evb
= b
;
583 if (eva
->event
!= evb
->event
) {
587 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
588 return !strcmp(qdict_get_str(eva
->data
, "id"),
589 qdict_get_str(evb
->data
, "id"));
595 static void monitor_qapi_event_init(void)
597 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
598 qapi_event_throttle_equal
);
599 qmp_event_set_func_emit(monitor_qapi_event_queue
);
602 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
604 cur_mon
->qmp
.in_command_mode
= true;
607 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
609 static void monitor_data_init(Monitor
*mon
)
611 memset(mon
, 0, sizeof(Monitor
));
612 qemu_mutex_init(&mon
->out_lock
);
613 mon
->outbuf
= qstring_new();
614 /* Use *mon_cmds by default. */
615 mon
->cmd_table
= mon_cmds
;
618 static void monitor_data_destroy(Monitor
*mon
)
620 QDECREF(mon
->outbuf
);
621 qemu_mutex_destroy(&mon
->out_lock
);
624 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
625 int64_t cpu_index
, Error
**errp
)
628 Monitor
*old_mon
, hmp
;
630 monitor_data_init(&hmp
);
631 hmp
.skip_flush
= true;
637 int ret
= monitor_set_cpu(cpu_index
);
640 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
646 handle_hmp_command(&hmp
, command_line
);
649 qemu_mutex_lock(&hmp
.out_lock
);
650 if (qstring_get_length(hmp
.outbuf
) > 0) {
651 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
653 output
= g_strdup("");
655 qemu_mutex_unlock(&hmp
.out_lock
);
658 monitor_data_destroy(&hmp
);
662 static int compare_cmd(const char *name
, const char *list
)
664 const char *p
, *pstart
;
672 p
= pstart
+ strlen(pstart
);
673 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
682 static int get_str(char *buf
, int buf_size
, const char **pp
)
690 while (qemu_isspace(*p
)) {
701 while (*p
!= '\0' && *p
!= '\"') {
717 printf("unsupported escape code: '\\%c'\n", c
);
720 if ((q
- buf
) < buf_size
- 1) {
724 if ((q
- buf
) < buf_size
- 1) {
731 printf("unterminated string\n");
736 while (*p
!= '\0' && !qemu_isspace(*p
)) {
737 if ((q
- buf
) < buf_size
- 1) {
750 static void free_cmdline_args(char **args
, int nb_args
)
754 assert(nb_args
<= MAX_ARGS
);
756 for (i
= 0; i
< nb_args
; i
++) {
763 * Parse the command line to get valid args.
764 * @cmdline: command line to be parsed.
765 * @pnb_args: location to store the number of args, must NOT be NULL.
766 * @args: location to store the args, which should be freed by caller, must
769 * Returns 0 on success, negative on failure.
771 * NOTE: this parser is an approximate form of the real command parser. Number
772 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
773 * return with failure.
775 static int parse_cmdline(const char *cmdline
,
776 int *pnb_args
, char **args
)
785 while (qemu_isspace(*p
)) {
791 if (nb_args
>= MAX_ARGS
) {
794 ret
= get_str(buf
, sizeof(buf
), &p
);
798 args
[nb_args
] = g_strdup(buf
);
805 free_cmdline_args(args
, nb_args
);
809 static void help_cmd_dump_one(Monitor
*mon
,
810 const mon_cmd_t
*cmd
,
816 for (i
= 0; i
< prefix_args_nb
; i
++) {
817 monitor_printf(mon
, "%s ", prefix_args
[i
]);
819 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
822 /* @args[@arg_index] is the valid command need to find in @cmds */
823 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
824 char **args
, int nb_args
, int arg_index
)
826 const mon_cmd_t
*cmd
;
828 /* No valid arg need to compare with, dump all in *cmds */
829 if (arg_index
>= nb_args
) {
830 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
831 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
836 /* Find one entry to dump */
837 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
838 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
839 if (cmd
->sub_table
) {
840 /* continue with next arg */
841 help_cmd_dump(mon
, cmd
->sub_table
,
842 args
, nb_args
, arg_index
+ 1);
844 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
851 static void help_cmd(Monitor
*mon
, const char *name
)
853 char *args
[MAX_ARGS
];
856 /* 1. parse user input */
858 /* special case for log, directly dump and return */
859 if (!strcmp(name
, "log")) {
860 const QEMULogItem
*item
;
861 monitor_printf(mon
, "Log items (comma separated):\n");
862 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
863 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
864 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
869 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
874 /* 2. dump the contents according to parsed args */
875 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
877 free_cmdline_args(args
, nb_args
);
880 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
882 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
885 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
887 const char *tp_name
= qdict_get_str(qdict
, "name");
888 bool new_state
= qdict_get_bool(qdict
, "option");
889 Error
*local_err
= NULL
;
891 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
893 error_report_err(local_err
);
897 #ifdef CONFIG_TRACE_SIMPLE
898 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
900 const char *op
= qdict_get_try_str(qdict
, "op");
901 const char *arg
= qdict_get_try_str(qdict
, "arg");
904 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
905 } else if (!strcmp(op
, "on")) {
906 st_set_trace_file_enabled(true);
907 } else if (!strcmp(op
, "off")) {
908 st_set_trace_file_enabled(false);
909 } else if (!strcmp(op
, "flush")) {
910 st_flush_trace_buffer();
911 } else if (!strcmp(op
, "set")) {
913 st_set_trace_file(arg
);
916 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
917 help_cmd(mon
, "trace-file");
922 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
924 help_cmd(mon
, "info");
927 CommandInfoList
*qmp_query_commands(Error
**errp
)
929 CommandInfoList
*info
, *cmd_list
= NULL
;
930 const mon_cmd_t
*cmd
;
932 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
933 info
= g_malloc0(sizeof(*info
));
934 info
->value
= g_malloc0(sizeof(*info
->value
));
935 info
->value
->name
= g_strdup(cmd
->name
);
937 info
->next
= cmd_list
;
944 EventInfoList
*qmp_query_events(Error
**errp
)
946 EventInfoList
*info
, *ev_list
= NULL
;
949 for (e
= 0 ; e
< QAPI_EVENT_MAX
; e
++) {
950 const char *event_name
= QAPIEvent_lookup
[e
];
951 assert(event_name
!= NULL
);
952 info
= g_malloc0(sizeof(*info
));
953 info
->value
= g_malloc0(sizeof(*info
->value
));
954 info
->value
->name
= g_strdup(event_name
);
956 info
->next
= ev_list
;
964 * Minor hack: generated marshalling suppressed for this command
965 * ('gen': false in the schema) so we can parse the JSON string
966 * directly into QObject instead of first parsing it with
967 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
968 * to QObject with generated output marshallers, every time. Instead,
969 * we do it in test-qmp-input-visitor.c, just to make sure
970 * qapi-introspect.py's output actually conforms to the schema.
972 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
975 *ret_data
= qobject_from_json(qmp_schema_json
);
978 /* set the current CPU defined by the user */
979 int monitor_set_cpu(int cpu_index
)
983 cpu
= qemu_get_cpu(cpu_index
);
987 cur_mon
->mon_cpu
= cpu
;
991 CPUState
*mon_get_cpu(void)
993 if (!cur_mon
->mon_cpu
) {
996 cpu_synchronize_state(cur_mon
->mon_cpu
);
997 return cur_mon
->mon_cpu
;
1000 CPUArchState
*mon_get_cpu_env(void)
1002 return mon_get_cpu()->env_ptr
;
1005 int monitor_get_cpu_index(void)
1007 return mon_get_cpu()->cpu_index
;
1010 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1012 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1015 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1017 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1018 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1021 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1023 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1026 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1035 str
= readline_get_history(mon
->rs
, i
);
1038 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1043 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1045 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1048 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1050 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1051 TraceEventInfoList
*elem
;
1053 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1054 monitor_printf(mon
, "%s : state %u\n",
1056 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1058 qapi_free_TraceEventInfoList(events
);
1061 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1062 bool has_port
, int64_t port
,
1063 bool has_tls_port
, int64_t tls_port
,
1064 bool has_cert_subject
, const char *cert_subject
,
1067 if (strcmp(protocol
, "spice") == 0) {
1068 if (!qemu_using_spice(errp
)) {
1072 if (!has_port
&& !has_tls_port
) {
1073 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1077 if (qemu_spice_migrate_info(hostname
,
1078 has_port
? port
: -1,
1079 has_tls_port
? tls_port
: -1,
1081 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1087 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1090 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1092 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1095 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1098 const char *items
= qdict_get_str(qdict
, "items");
1100 if (!strcmp(items
, "none")) {
1103 mask
= qemu_str_to_log_mask(items
);
1105 help_cmd(mon
, "log");
1112 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1114 const char *option
= qdict_get_try_str(qdict
, "option");
1115 if (!option
|| !strcmp(option
, "on")) {
1117 } else if (!strcmp(option
, "off")) {
1120 monitor_printf(mon
, "unexpected option %s\n", option
);
1124 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1126 const char *device
= qdict_get_try_str(qdict
, "device");
1128 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1129 if (gdbserver_start(device
) < 0) {
1130 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1132 } else if (strcmp(device
, "none") == 0) {
1133 monitor_printf(mon
, "Disabled gdbserver\n");
1135 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1140 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1142 const char *action
= qdict_get_str(qdict
, "action");
1143 if (select_watchdog_action(action
) == -1) {
1144 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1148 static void monitor_printc(Monitor
*mon
, int c
)
1150 monitor_printf(mon
, "'");
1153 monitor_printf(mon
, "\\'");
1156 monitor_printf(mon
, "\\\\");
1159 monitor_printf(mon
, "\\n");
1162 monitor_printf(mon
, "\\r");
1165 if (c
>= 32 && c
<= 126) {
1166 monitor_printf(mon
, "%c", c
);
1168 monitor_printf(mon
, "\\x%02x", c
);
1172 monitor_printf(mon
, "'");
1175 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1176 hwaddr addr
, int is_physical
)
1178 int l
, line_size
, i
, max_digits
, len
;
1182 if (format
== 'i') {
1185 CPUArchState
*env
= mon_get_cpu_env();
1188 } else if (wsize
== 4) {
1191 /* as default we use the current CS size */
1194 #ifdef TARGET_X86_64
1195 if ((env
->efer
& MSR_EFER_LMA
) &&
1196 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1200 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1206 CPUArchState
*env
= mon_get_cpu_env();
1207 flags
= msr_le
<< 16;
1208 flags
|= env
->bfd_mach
;
1210 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1214 len
= wsize
* count
;
1223 max_digits
= (wsize
* 8 + 2) / 3;
1227 max_digits
= (wsize
* 8) / 4;
1231 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1240 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1242 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1247 cpu_physical_memory_read(addr
, buf
, l
);
1249 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1250 monitor_printf(mon
, " Cannot access memory\n");
1259 v
= ldub_p(buf
+ i
);
1262 v
= lduw_p(buf
+ i
);
1265 v
= (uint32_t)ldl_p(buf
+ i
);
1271 monitor_printf(mon
, " ");
1274 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1277 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1280 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1283 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1286 monitor_printc(mon
, v
);
1291 monitor_printf(mon
, "\n");
1297 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1299 int count
= qdict_get_int(qdict
, "count");
1300 int format
= qdict_get_int(qdict
, "format");
1301 int size
= qdict_get_int(qdict
, "size");
1302 target_long addr
= qdict_get_int(qdict
, "addr");
1304 memory_dump(mon
, count
, format
, size
, addr
, 0);
1307 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1309 int count
= qdict_get_int(qdict
, "count");
1310 int format
= qdict_get_int(qdict
, "format");
1311 int size
= qdict_get_int(qdict
, "size");
1312 hwaddr addr
= qdict_get_int(qdict
, "addr");
1314 memory_dump(mon
, count
, format
, size
, addr
, 1);
1317 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1319 int format
= qdict_get_int(qdict
, "format");
1320 hwaddr val
= qdict_get_int(qdict
, "val");
1324 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1327 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1330 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1334 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1337 monitor_printc(mon
, val
);
1340 monitor_printf(mon
, "\n");
1343 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1347 uint32_t start
= qdict_get_int(qdict
, "start");
1348 uint32_t size
= qdict_get_int(qdict
, "size");
1351 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1352 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1353 MEMTXATTRS_UNSPECIFIED
, NULL
);
1354 /* BSD sum algorithm ('sum' Unix command) */
1355 sum
= (sum
>> 1) | (sum
<< 15);
1358 monitor_printf(mon
, "%05d\n", sum
);
1361 static int mouse_button_state
;
1363 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1365 int dx
, dy
, dz
, button
;
1366 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1367 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1368 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1370 dx
= strtol(dx_str
, NULL
, 0);
1371 dy
= strtol(dy_str
, NULL
, 0);
1372 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1373 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1376 dz
= strtol(dz_str
, NULL
, 0);
1378 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1379 qemu_input_queue_btn(NULL
, button
, true);
1380 qemu_input_event_sync();
1381 qemu_input_queue_btn(NULL
, button
, false);
1384 qemu_input_event_sync();
1387 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1389 static uint32_t bmap
[INPUT_BUTTON_MAX
] = {
1390 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1391 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1392 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1394 int button_state
= qdict_get_int(qdict
, "button_state");
1396 if (mouse_button_state
== button_state
) {
1399 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1400 qemu_input_event_sync();
1401 mouse_button_state
= button_state
;
1404 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1406 int size
= qdict_get_int(qdict
, "size");
1407 int addr
= qdict_get_int(qdict
, "addr");
1408 int has_index
= qdict_haskey(qdict
, "index");
1413 int index
= qdict_get_int(qdict
, "index");
1414 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1422 val
= cpu_inb(addr
);
1426 val
= cpu_inw(addr
);
1430 val
= cpu_inl(addr
);
1434 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1435 suffix
, addr
, size
* 2, val
);
1438 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1440 int size
= qdict_get_int(qdict
, "size");
1441 int addr
= qdict_get_int(qdict
, "addr");
1442 int val
= qdict_get_int(qdict
, "val");
1444 addr
&= IOPORTS_MASK
;
1449 cpu_outb(addr
, val
);
1452 cpu_outw(addr
, val
);
1455 cpu_outl(addr
, val
);
1460 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1462 Error
*local_err
= NULL
;
1463 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1465 qemu_boot_set(bootdevice
, &local_err
);
1467 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
1468 error_free(local_err
);
1470 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1474 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1476 mtree_info((fprintf_function
)monitor_printf
, mon
);
1479 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1485 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1486 query_numa_node_mem(node_mem
);
1487 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1488 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1489 monitor_printf(mon
, "node %d cpus:", i
);
1491 if (cpu
->numa_node
== i
) {
1492 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1495 monitor_printf(mon
, "\n");
1496 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1502 #ifdef CONFIG_PROFILER
1507 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1509 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1510 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1511 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1512 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1517 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1519 monitor_printf(mon
, "Internal profiler not compiled\n");
1523 /* Capture support */
1524 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1526 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1531 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1532 monitor_printf(mon
, "[%d]: ", i
);
1533 s
->ops
.info (s
->opaque
);
1537 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1540 int n
= qdict_get_int(qdict
, "n");
1543 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1545 s
->ops
.destroy (s
->opaque
);
1546 QLIST_REMOVE (s
, entries
);
1553 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1555 const char *path
= qdict_get_str(qdict
, "path");
1556 int has_freq
= qdict_haskey(qdict
, "freq");
1557 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1558 int has_bits
= qdict_haskey(qdict
, "bits");
1559 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1560 int has_channels
= qdict_haskey(qdict
, "nchannels");
1561 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1564 s
= g_malloc0 (sizeof (*s
));
1566 freq
= has_freq
? freq
: 44100;
1567 bits
= has_bits
? bits
: 16;
1568 nchannels
= has_channels
? nchannels
: 2;
1570 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1571 monitor_printf(mon
, "Failed to add wave capture\n");
1575 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1578 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1580 qemu_acl
*acl
= qemu_acl_find(name
);
1583 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1588 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1590 const char *aclname
= qdict_get_str(qdict
, "aclname");
1591 qemu_acl
*acl
= find_acl(mon
, aclname
);
1592 qemu_acl_entry
*entry
;
1596 monitor_printf(mon
, "policy: %s\n",
1597 acl
->defaultDeny
? "deny" : "allow");
1598 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1600 monitor_printf(mon
, "%d: %s %s\n", i
,
1601 entry
->deny
? "deny" : "allow", entry
->match
);
1606 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1608 const char *aclname
= qdict_get_str(qdict
, "aclname");
1609 qemu_acl
*acl
= find_acl(mon
, aclname
);
1612 qemu_acl_reset(acl
);
1613 monitor_printf(mon
, "acl: removed all rules\n");
1617 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1619 const char *aclname
= qdict_get_str(qdict
, "aclname");
1620 const char *policy
= qdict_get_str(qdict
, "policy");
1621 qemu_acl
*acl
= find_acl(mon
, aclname
);
1624 if (strcmp(policy
, "allow") == 0) {
1625 acl
->defaultDeny
= 0;
1626 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1627 } else if (strcmp(policy
, "deny") == 0) {
1628 acl
->defaultDeny
= 1;
1629 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1631 monitor_printf(mon
, "acl: unknown policy '%s', "
1632 "expected 'deny' or 'allow'\n", policy
);
1637 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1639 const char *aclname
= qdict_get_str(qdict
, "aclname");
1640 const char *match
= qdict_get_str(qdict
, "match");
1641 const char *policy
= qdict_get_str(qdict
, "policy");
1642 int has_index
= qdict_haskey(qdict
, "index");
1643 int index
= qdict_get_try_int(qdict
, "index", -1);
1644 qemu_acl
*acl
= find_acl(mon
, aclname
);
1648 if (strcmp(policy
, "allow") == 0) {
1650 } else if (strcmp(policy
, "deny") == 0) {
1653 monitor_printf(mon
, "acl: unknown policy '%s', "
1654 "expected 'deny' or 'allow'\n", policy
);
1658 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1660 ret
= qemu_acl_append(acl
, deny
, match
);
1662 monitor_printf(mon
, "acl: unable to add acl entry\n");
1664 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1668 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1670 const char *aclname
= qdict_get_str(qdict
, "aclname");
1671 const char *match
= qdict_get_str(qdict
, "match");
1672 qemu_acl
*acl
= find_acl(mon
, aclname
);
1676 ret
= qemu_acl_remove(acl
, match
);
1678 monitor_printf(mon
, "acl: no matching acl entry\n");
1680 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1684 void qmp_getfd(const char *fdname
, Error
**errp
)
1689 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1691 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1695 if (qemu_isdigit(fdname
[0])) {
1697 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1698 "a name not starting with a digit");
1702 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1703 if (strcmp(monfd
->name
, fdname
) != 0) {
1712 monfd
= g_malloc0(sizeof(mon_fd_t
));
1713 monfd
->name
= g_strdup(fdname
);
1716 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1719 void qmp_closefd(const char *fdname
, Error
**errp
)
1723 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1724 if (strcmp(monfd
->name
, fdname
) != 0) {
1728 QLIST_REMOVE(monfd
, next
);
1730 g_free(monfd
->name
);
1735 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1738 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1740 int saved_vm_running
= runstate_is_running();
1741 const char *name
= qdict_get_str(qdict
, "name");
1743 vm_stop(RUN_STATE_RESTORE_VM
);
1745 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1750 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1754 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1757 if (strcmp(monfd
->name
, fdname
) != 0) {
1763 /* caller takes ownership of fd */
1764 QLIST_REMOVE(monfd
, next
);
1765 g_free(monfd
->name
);
1771 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1775 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1777 MonFdsetFd
*mon_fdset_fd
;
1778 MonFdsetFd
*mon_fdset_fd_next
;
1780 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1781 if ((mon_fdset_fd
->removed
||
1782 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1783 runstate_is_running()) {
1784 close(mon_fdset_fd
->fd
);
1785 g_free(mon_fdset_fd
->opaque
);
1786 QLIST_REMOVE(mon_fdset_fd
, next
);
1787 g_free(mon_fdset_fd
);
1791 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1792 QLIST_REMOVE(mon_fdset
, next
);
1797 static void monitor_fdsets_cleanup(void)
1799 MonFdset
*mon_fdset
;
1800 MonFdset
*mon_fdset_next
;
1802 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1803 monitor_fdset_cleanup(mon_fdset
);
1807 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1808 const char *opaque
, Error
**errp
)
1811 Monitor
*mon
= cur_mon
;
1814 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1816 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1820 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1821 has_opaque
, opaque
, errp
);
1833 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1835 MonFdset
*mon_fdset
;
1836 MonFdsetFd
*mon_fdset_fd
;
1839 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1840 if (mon_fdset
->id
!= fdset_id
) {
1843 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1845 if (mon_fdset_fd
->fd
!= fd
) {
1848 mon_fdset_fd
->removed
= true;
1851 mon_fdset_fd
->removed
= true;
1854 if (has_fd
&& !mon_fdset_fd
) {
1857 monitor_fdset_cleanup(mon_fdset
);
1863 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1866 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1868 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1871 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1873 MonFdset
*mon_fdset
;
1874 MonFdsetFd
*mon_fdset_fd
;
1875 FdsetInfoList
*fdset_list
= NULL
;
1877 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1878 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1879 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1881 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1882 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1884 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1885 FdsetFdInfoList
*fdsetfd_info
;
1887 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1888 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1889 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1890 if (mon_fdset_fd
->opaque
) {
1891 fdsetfd_info
->value
->has_opaque
= true;
1892 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1894 fdsetfd_info
->value
->has_opaque
= false;
1897 fdsetfd_info
->next
= fdsetfd_list
;
1898 fdsetfd_list
= fdsetfd_info
;
1901 fdset_info
->value
->fds
= fdsetfd_list
;
1903 fdset_info
->next
= fdset_list
;
1904 fdset_list
= fdset_info
;
1910 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1911 bool has_opaque
, const char *opaque
,
1914 MonFdset
*mon_fdset
= NULL
;
1915 MonFdsetFd
*mon_fdset_fd
;
1919 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1920 /* Break if match found or match impossible due to ordering by ID */
1921 if (fdset_id
<= mon_fdset
->id
) {
1922 if (fdset_id
< mon_fdset
->id
) {
1930 if (mon_fdset
== NULL
) {
1931 int64_t fdset_id_prev
= -1;
1932 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1936 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1937 "a non-negative value");
1940 /* Use specified fdset ID */
1941 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1942 mon_fdset_cur
= mon_fdset
;
1943 if (fdset_id
< mon_fdset_cur
->id
) {
1948 /* Use first available fdset ID */
1949 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1950 mon_fdset_cur
= mon_fdset
;
1951 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1952 fdset_id_prev
= mon_fdset_cur
->id
;
1959 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1961 mon_fdset
->id
= fdset_id
;
1963 mon_fdset
->id
= fdset_id_prev
+ 1;
1966 /* The fdset list is ordered by fdset ID */
1967 if (!mon_fdset_cur
) {
1968 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1969 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1970 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1972 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1976 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1977 mon_fdset_fd
->fd
= fd
;
1978 mon_fdset_fd
->removed
= false;
1980 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1982 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1984 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1985 fdinfo
->fdset_id
= mon_fdset
->id
;
1986 fdinfo
->fd
= mon_fdset_fd
->fd
;
1991 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
1994 MonFdset
*mon_fdset
;
1995 MonFdsetFd
*mon_fdset_fd
;
1998 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1999 if (mon_fdset
->id
!= fdset_id
) {
2002 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2003 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2004 if (mon_fd_flags
== -1) {
2008 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2009 return mon_fdset_fd
->fd
;
2021 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2023 MonFdset
*mon_fdset
;
2024 MonFdsetFd
*mon_fdset_fd_dup
;
2026 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2027 if (mon_fdset
->id
!= fdset_id
) {
2030 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2031 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2035 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2036 mon_fdset_fd_dup
->fd
= dup_fd
;
2037 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2043 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2045 MonFdset
*mon_fdset
;
2046 MonFdsetFd
*mon_fdset_fd_dup
;
2048 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2049 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2050 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2052 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2053 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2054 monitor_fdset_cleanup(mon_fdset
);
2058 return mon_fdset
->id
;
2066 int monitor_fdset_dup_fd_find(int dup_fd
)
2068 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2071 void monitor_fdset_dup_fd_remove(int dup_fd
)
2073 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2076 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2079 Error
*local_err
= NULL
;
2081 if (!qemu_isdigit(fdname
[0]) && mon
) {
2082 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2084 fd
= qemu_parse_fd(fdname
);
2086 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2091 error_propagate(errp
, local_err
);
2100 /* Please update hmp-commands.hx when adding or changing commands */
2101 static mon_cmd_t info_cmds
[] = {
2102 #include "hmp-commands-info.h"
2106 /* mon_cmds and info_cmds would be sorted at runtime */
2107 static mon_cmd_t mon_cmds
[] = {
2108 #include "hmp-commands.h"
2112 static const mon_cmd_t qmp_cmds
[] = {
2113 #include "qmp-commands-old.h"
2117 /*******************************************************************/
2119 static const char *pch
;
2120 static sigjmp_buf expr_env
;
2123 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2124 expr_error(Monitor
*mon
, const char *fmt
, ...)
2128 monitor_vprintf(mon
, fmt
, ap
);
2129 monitor_printf(mon
, "\n");
2131 siglongjmp(expr_env
, 1);
2134 /* return 0 if OK, -1 if not found */
2135 static int get_monitor_def(target_long
*pval
, const char *name
)
2137 const MonitorDef
*md
= target_monitor_defs();
2146 for(; md
->name
!= NULL
; md
++) {
2147 if (compare_cmd(name
, md
->name
)) {
2148 if (md
->get_value
) {
2149 *pval
= md
->get_value(md
, md
->offset
);
2151 CPUArchState
*env
= mon_get_cpu_env();
2152 ptr
= (uint8_t *)env
+ md
->offset
;
2155 *pval
= *(int32_t *)ptr
;
2158 *pval
= *(target_long
*)ptr
;
2169 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2171 *pval
= (target_long
) tmp
;
2177 static void next(void)
2181 while (qemu_isspace(*pch
))
2186 static int64_t expr_sum(Monitor
*mon
);
2188 static int64_t expr_unary(Monitor
*mon
)
2197 n
= expr_unary(mon
);
2201 n
= -expr_unary(mon
);
2205 n
= ~expr_unary(mon
);
2211 expr_error(mon
, "')' expected");
2218 expr_error(mon
, "character constant expected");
2222 expr_error(mon
, "missing terminating \' character");
2232 while ((*pch
>= 'a' && *pch
<= 'z') ||
2233 (*pch
>= 'A' && *pch
<= 'Z') ||
2234 (*pch
>= '0' && *pch
<= '9') ||
2235 *pch
== '_' || *pch
== '.') {
2236 if ((q
- buf
) < sizeof(buf
) - 1)
2240 while (qemu_isspace(*pch
))
2243 ret
= get_monitor_def(®
, buf
);
2245 expr_error(mon
, "unknown register");
2250 expr_error(mon
, "unexpected end of expression");
2255 n
= strtoull(pch
, &p
, 0);
2256 if (errno
== ERANGE
) {
2257 expr_error(mon
, "number too large");
2260 expr_error(mon
, "invalid char '%c' in expression", *p
);
2263 while (qemu_isspace(*pch
))
2271 static int64_t expr_prod(Monitor
*mon
)
2276 val
= expr_unary(mon
);
2279 if (op
!= '*' && op
!= '/' && op
!= '%')
2282 val2
= expr_unary(mon
);
2291 expr_error(mon
, "division by zero");
2302 static int64_t expr_logic(Monitor
*mon
)
2307 val
= expr_prod(mon
);
2310 if (op
!= '&' && op
!= '|' && op
!= '^')
2313 val2
= expr_prod(mon
);
2330 static int64_t expr_sum(Monitor
*mon
)
2335 val
= expr_logic(mon
);
2338 if (op
!= '+' && op
!= '-')
2341 val2
= expr_logic(mon
);
2350 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2353 if (sigsetjmp(expr_env
, 0)) {
2357 while (qemu_isspace(*pch
))
2359 *pval
= expr_sum(mon
);
2364 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2366 const char *p
= *pp
;
2370 d
= strtod(p
, &tailp
);
2372 monitor_printf(mon
, "Number expected\n");
2375 if (d
!= d
|| d
- d
!= 0) {
2376 /* NaN or infinity */
2377 monitor_printf(mon
, "Bad number\n");
2386 * Store the command-name in cmdname, and return a pointer to
2387 * the remaining of the command string.
2389 static const char *get_command_name(const char *cmdline
,
2390 char *cmdname
, size_t nlen
)
2393 const char *p
, *pstart
;
2396 while (qemu_isspace(*p
))
2401 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2406 memcpy(cmdname
, pstart
, len
);
2407 cmdname
[len
] = '\0';
2412 * Read key of 'type' into 'key' and return the current
2415 static char *key_get_info(const char *type
, char **key
)
2423 p
= strchr(type
, ':');
2430 str
= g_malloc(len
+ 1);
2431 memcpy(str
, type
, len
);
2438 static int default_fmt_format
= 'x';
2439 static int default_fmt_size
= 4;
2441 static int is_valid_option(const char *c
, const char *typestr
)
2449 typestr
= strstr(typestr
, option
);
2450 return (typestr
!= NULL
);
2453 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2454 const char *cmdname
)
2456 const mon_cmd_t
*cmd
;
2458 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2459 if (compare_cmd(cmdname
, cmd
->name
)) {
2467 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2469 return search_dispatch_table(qmp_cmds
, cmdname
);
2473 * Parse command name from @cmdp according to command table @table.
2474 * If blank, return NULL.
2475 * Else, if no valid command can be found, report to @mon, and return
2477 * Else, change @cmdp to point right behind the name, and return its
2478 * command table entry.
2479 * Do not assume the return value points into @table! It doesn't when
2480 * the command is found in a sub-command table.
2482 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2487 const mon_cmd_t
*cmd
;
2490 /* extract the command name */
2491 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2495 cmd
= search_dispatch_table(table
, cmdname
);
2497 monitor_printf(mon
, "unknown command: '%.*s'\n",
2498 (int)(p
- *cmdp
), *cmdp
);
2502 /* filter out following useless space */
2503 while (qemu_isspace(*p
)) {
2508 /* search sub command */
2509 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2510 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2517 * Parse arguments for @cmd.
2518 * If it can't be parsed, report to @mon, and return NULL.
2519 * Else, insert command arguments into a QDict, and return it.
2520 * Note: On success, caller has to free the QDict structure.
2523 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2525 const mon_cmd_t
*cmd
)
2527 const char *typestr
;
2530 const char *p
= *endp
;
2532 QDict
*qdict
= qdict_new();
2534 /* parse the parameters */
2535 typestr
= cmd
->args_type
;
2537 typestr
= key_get_info(typestr
, &key
);
2549 while (qemu_isspace(*p
))
2551 if (*typestr
== '?') {
2554 /* no optional string: NULL argument */
2558 ret
= get_str(buf
, sizeof(buf
), &p
);
2562 monitor_printf(mon
, "%s: filename expected\n",
2566 monitor_printf(mon
, "%s: block device name expected\n",
2570 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2575 qdict_put(qdict
, key
, qstring_from_str(buf
));
2580 QemuOptsList
*opts_list
;
2583 opts_list
= qemu_find_opts(key
);
2584 if (!opts_list
|| opts_list
->desc
->name
) {
2587 while (qemu_isspace(*p
)) {
2592 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2595 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2599 qemu_opts_to_qdict(opts
, qdict
);
2600 qemu_opts_del(opts
);
2605 int count
, format
, size
;
2607 while (qemu_isspace(*p
))
2613 if (qemu_isdigit(*p
)) {
2615 while (qemu_isdigit(*p
)) {
2616 count
= count
* 10 + (*p
- '0');
2654 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2655 monitor_printf(mon
, "invalid char in format: '%c'\n",
2660 format
= default_fmt_format
;
2661 if (format
!= 'i') {
2662 /* for 'i', not specifying a size gives -1 as size */
2664 size
= default_fmt_size
;
2665 default_fmt_size
= size
;
2667 default_fmt_format
= format
;
2670 format
= default_fmt_format
;
2671 if (format
!= 'i') {
2672 size
= default_fmt_size
;
2677 qdict_put(qdict
, "count", qint_from_int(count
));
2678 qdict_put(qdict
, "format", qint_from_int(format
));
2679 qdict_put(qdict
, "size", qint_from_int(size
));
2688 while (qemu_isspace(*p
))
2690 if (*typestr
== '?' || *typestr
== '.') {
2691 if (*typestr
== '?') {
2699 while (qemu_isspace(*p
))
2708 if (get_expr(mon
, &val
, &p
))
2710 /* Check if 'i' is greater than 32-bit */
2711 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2712 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2713 monitor_printf(mon
, "integer is for 32-bit values\n");
2715 } else if (c
== 'M') {
2717 monitor_printf(mon
, "enter a positive value\n");
2722 qdict_put(qdict
, key
, qint_from_int(val
));
2730 while (qemu_isspace(*p
)) {
2733 if (*typestr
== '?') {
2739 val
= qemu_strtosz(p
, &end
);
2741 monitor_printf(mon
, "invalid size\n");
2744 qdict_put(qdict
, key
, qint_from_int(val
));
2752 while (qemu_isspace(*p
))
2754 if (*typestr
== '?') {
2760 if (get_double(mon
, &val
, &p
) < 0) {
2763 if (p
[0] && p
[1] == 's') {
2766 val
/= 1e3
; p
+= 2; break;
2768 val
/= 1e6
; p
+= 2; break;
2770 val
/= 1e9
; p
+= 2; break;
2773 if (*p
&& !qemu_isspace(*p
)) {
2774 monitor_printf(mon
, "Unknown unit suffix\n");
2777 qdict_put(qdict
, key
, qfloat_from_double(val
));
2785 while (qemu_isspace(*p
)) {
2789 while (qemu_isgraph(*p
)) {
2792 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2794 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2797 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2800 qdict_put(qdict
, key
, qbool_from_bool(val
));
2805 const char *tmp
= p
;
2812 while (qemu_isspace(*p
))
2817 if(!is_valid_option(p
, typestr
)) {
2819 monitor_printf(mon
, "%s: unsupported option -%c\n",
2831 qdict_put(qdict
, key
, qbool_from_bool(true));
2838 /* package all remaining string */
2841 while (qemu_isspace(*p
)) {
2844 if (*typestr
== '?') {
2847 /* no remaining string: NULL argument */
2853 monitor_printf(mon
, "%s: string expected\n",
2857 qdict_put(qdict
, key
, qstring_from_str(p
));
2863 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2869 /* check that all arguments were parsed */
2870 while (qemu_isspace(*p
))
2873 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2886 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2889 const mon_cmd_t
*cmd
;
2891 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2896 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2898 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2903 cmd
->mhandler
.cmd(mon
, qdict
);
2907 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2909 const char *p
, *pstart
;
2918 p
= pstart
+ strlen(pstart
);
2920 if (len
> sizeof(cmd
) - 2)
2921 len
= sizeof(cmd
) - 2;
2922 memcpy(cmd
, pstart
, len
);
2924 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2925 readline_add_completion(mon
->rs
, cmd
);
2933 static void file_completion(Monitor
*mon
, const char *input
)
2938 char file
[1024], file_prefix
[1024];
2942 p
= strrchr(input
, '/');
2945 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2946 pstrcpy(path
, sizeof(path
), ".");
2948 input_path_len
= p
- input
+ 1;
2949 memcpy(path
, input
, input_path_len
);
2950 if (input_path_len
> sizeof(path
) - 1)
2951 input_path_len
= sizeof(path
) - 1;
2952 path
[input_path_len
] = '\0';
2953 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2956 ffs
= opendir(path
);
2965 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
2969 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2970 memcpy(file
, input
, input_path_len
);
2971 if (input_path_len
< sizeof(file
))
2972 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2974 /* stat the file to find out if it's a directory.
2975 * In that case add a slash to speed up typing long paths
2977 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
2978 pstrcat(file
, sizeof(file
), "/");
2980 readline_add_completion(mon
->rs
, file
);
2986 static const char *next_arg_type(const char *typestr
)
2988 const char *p
= strchr(typestr
, ':');
2989 return (p
!= NULL
? ++p
: typestr
);
2992 static void add_completion_option(ReadLineState
*rs
, const char *str
,
2995 if (!str
|| !option
) {
2998 if (!strncmp(option
, str
, strlen(str
))) {
2999 readline_add_completion(rs
, option
);
3003 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3006 ChardevBackendInfoList
*list
, *start
;
3012 readline_set_completion_index(rs
, len
);
3014 start
= list
= qmp_query_chardev_backends(NULL
);
3016 const char *chr_name
= list
->value
->name
;
3018 if (!strncmp(chr_name
, str
, len
)) {
3019 readline_add_completion(rs
, chr_name
);
3023 qapi_free_ChardevBackendInfoList(start
);
3026 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3035 readline_set_completion_index(rs
, len
);
3036 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
3037 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3041 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3051 readline_set_completion_index(rs
, len
);
3052 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3055 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3057 name
= object_class_get_name(OBJECT_CLASS(dc
));
3059 if (!dc
->cannot_instantiate_with_device_add_yet
3060 && !strncmp(name
, str
, len
)) {
3061 readline_add_completion(rs
, name
);
3068 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3078 readline_set_completion_index(rs
, len
);
3079 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3083 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3084 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3085 readline_add_completion(rs
, name
);
3092 static void peripheral_device_del_completion(ReadLineState
*rs
,
3093 const char *str
, size_t len
)
3095 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3096 GSList
*list
, *item
;
3098 list
= qdev_build_hotpluggable_device_list(peripheral
);
3103 for (item
= list
; item
; item
= g_slist_next(item
)) {
3104 DeviceState
*dev
= item
->data
;
3106 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3107 readline_add_completion(rs
, dev
->id
);
3114 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3117 ChardevInfoList
*list
, *start
;
3123 readline_set_completion_index(rs
, len
);
3125 start
= list
= qmp_query_chardev(NULL
);
3127 ChardevInfo
*chr
= list
->value
;
3129 if (!strncmp(chr
->label
, str
, len
)) {
3130 readline_add_completion(rs
, chr
->label
);
3134 qapi_free_ChardevInfoList(start
);
3137 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3140 ChardevInfoList
*list
, *start
;
3143 readline_set_completion_index(rs
, len
);
3145 start
= list
= qmp_query_chardev(NULL
);
3147 ChardevInfo
*chr_info
= list
->value
;
3149 if (!strncmp(chr_info
->label
, str
, len
)) {
3150 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3151 if (chr
&& chr_is_ringbuf(chr
)) {
3152 readline_add_completion(rs
, chr_info
->label
);
3157 qapi_free_ChardevInfoList(start
);
3160 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3165 ringbuf_completion(rs
, str
);
3168 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3177 readline_set_completion_index(rs
, len
);
3178 peripheral_device_del_completion(rs
, str
, len
);
3181 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3183 ObjectPropertyInfoList
*list
, *start
;
3190 readline_set_completion_index(rs
, len
);
3192 start
= list
= qmp_qom_list("/objects", NULL
);
3194 ObjectPropertyInfo
*info
= list
->value
;
3196 if (!strncmp(info
->type
, "child<", 5)
3197 && !strncmp(info
->name
, str
, len
)) {
3198 readline_add_completion(rs
, info
->name
);
3202 qapi_free_ObjectPropertyInfoList(start
);
3205 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3214 sep
= strrchr(str
, '-');
3219 readline_set_completion_index(rs
, len
);
3220 for (i
= 0; i
< Q_KEY_CODE_MAX
; i
++) {
3221 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3222 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3227 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3232 readline_set_completion_index(rs
, len
);
3234 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3236 count
= qemu_find_net_clients_except(NULL
, ncs
,
3237 NET_CLIENT_OPTIONS_KIND_NONE
,
3239 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3240 const char *name
= ncs
[i
]->name
;
3241 if (!strncmp(str
, name
, len
)) {
3242 readline_add_completion(rs
, name
);
3245 } else if (nb_args
== 3) {
3246 add_completion_option(rs
, str
, "on");
3247 add_completion_option(rs
, str
, "off");
3251 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3254 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3261 readline_set_completion_index(rs
, len
);
3262 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
3264 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3266 const char *name
= ncs
[i
]->name
;
3267 if (strncmp(str
, name
, len
)) {
3270 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3272 readline_add_completion(rs
, name
);
3277 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3282 readline_set_completion_index(rs
, len
);
3285 for (id
= 0; id
< trace_event_count(); id
++) {
3286 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3287 if (!strncmp(str
, event_name
, len
)) {
3288 readline_add_completion(rs
, event_name
);
3291 } else if (nb_args
== 3) {
3292 add_completion_option(rs
, str
, "on");
3293 add_completion_option(rs
, str
, "off");
3297 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3304 readline_set_completion_index(rs
, strlen(str
));
3305 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3306 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3310 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3316 readline_set_completion_index(rs
, len
);
3319 for (i
= 0; i
< MIGRATION_CAPABILITY_MAX
; i
++) {
3320 const char *name
= MigrationCapability_lookup
[i
];
3321 if (!strncmp(str
, name
, len
)) {
3322 readline_add_completion(rs
, name
);
3325 } else if (nb_args
== 3) {
3326 add_completion_option(rs
, str
, "on");
3327 add_completion_option(rs
, str
, "off");
3331 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3337 readline_set_completion_index(rs
, len
);
3340 for (i
= 0; i
< MIGRATION_PARAMETER_MAX
; i
++) {
3341 const char *name
= MigrationParameter_lookup
[i
];
3342 if (!strncmp(str
, name
, len
)) {
3343 readline_add_completion(rs
, name
);
3349 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3357 readline_set_completion_index(rs
, len
);
3358 for (i
= 0; host_net_devices
[i
]; i
++) {
3359 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3360 readline_add_completion(rs
, host_net_devices
[i
]);
3365 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3367 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3371 readline_set_completion_index(rs
, len
);
3373 count
= qemu_find_net_clients_except(NULL
, ncs
,
3374 NET_CLIENT_OPTIONS_KIND_NONE
,
3376 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3380 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3383 snprintf(name
, sizeof(name
), "%d", id
);
3384 if (!strncmp(str
, name
, len
)) {
3385 readline_add_completion(rs
, name
);
3389 } else if (nb_args
== 3) {
3390 count
= qemu_find_net_clients_except(NULL
, ncs
,
3391 NET_CLIENT_OPTIONS_KIND_NIC
,
3393 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3397 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
3398 net_hub_id_for_client(ncs
[i
], &id
)) {
3401 name
= ncs
[i
]->name
;
3402 if (!strncmp(str
, name
, len
)) {
3403 readline_add_completion(rs
, name
);
3410 static void vm_completion(ReadLineState
*rs
, const char *str
)
3413 BlockDriverState
*bs
= NULL
;
3416 readline_set_completion_index(rs
, len
);
3417 while ((bs
= bdrv_next(bs
))) {
3418 SnapshotInfoList
*snapshots
, *snapshot
;
3419 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3422 aio_context_acquire(ctx
);
3423 if (bdrv_can_snapshot(bs
)) {
3424 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3426 aio_context_release(ctx
);
3431 snapshot
= snapshots
;
3433 char *completion
= snapshot
->value
->name
;
3434 if (!strncmp(str
, completion
, len
)) {
3435 readline_add_completion(rs
, completion
);
3437 completion
= snapshot
->value
->id
;
3438 if (!strncmp(str
, completion
, len
)) {
3439 readline_add_completion(rs
, completion
);
3441 snapshot
= snapshot
->next
;
3443 qapi_free_SnapshotInfoList(snapshots
);
3448 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3451 vm_completion(rs
, str
);
3455 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3458 vm_completion(rs
, str
);
3462 static void monitor_find_completion_by_table(Monitor
*mon
,
3463 const mon_cmd_t
*cmd_table
,
3467 const char *cmdname
;
3469 const char *ptype
, *str
, *name
;
3470 const mon_cmd_t
*cmd
;
3471 BlockDriverState
*bs
;
3474 /* command completion */
3479 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3480 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3481 cmd_completion(mon
, cmdname
, cmd
->name
);
3484 /* find the command */
3485 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3486 if (compare_cmd(args
[0], cmd
->name
)) {
3494 if (cmd
->sub_table
) {
3495 /* do the job again */
3496 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3497 &args
[1], nb_args
- 1);
3500 if (cmd
->command_completion
) {
3501 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3505 ptype
= next_arg_type(cmd
->args_type
);
3506 for(i
= 0; i
< nb_args
- 2; i
++) {
3507 if (*ptype
!= '\0') {
3508 ptype
= next_arg_type(ptype
);
3509 while (*ptype
== '?')
3510 ptype
= next_arg_type(ptype
);
3513 str
= args
[nb_args
- 1];
3514 while (*ptype
== '-' && ptype
[1] != '\0') {
3515 ptype
= next_arg_type(ptype
);
3519 /* file completion */
3520 readline_set_completion_index(mon
->rs
, strlen(str
));
3521 file_completion(mon
, str
);
3524 /* block device name completion */
3525 readline_set_completion_index(mon
->rs
, strlen(str
));
3526 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3527 name
= bdrv_get_device_name(bs
);
3528 if (str
[0] == '\0' ||
3529 !strncmp(name
, str
, strlen(str
))) {
3530 readline_add_completion(mon
->rs
, name
);
3536 if (!strcmp(cmd
->name
, "help|?")) {
3537 monitor_find_completion_by_table(mon
, cmd_table
,
3538 &args
[1], nb_args
- 1);
3547 static void monitor_find_completion(void *opaque
,
3548 const char *cmdline
)
3550 Monitor
*mon
= opaque
;
3551 char *args
[MAX_ARGS
];
3554 /* 1. parse the cmdline */
3555 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3559 /* if the line ends with a space, it means we want to complete the
3561 len
= strlen(cmdline
);
3562 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3563 if (nb_args
>= MAX_ARGS
) {
3566 args
[nb_args
++] = g_strdup("");
3569 /* 2. auto complete according to args */
3570 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3573 free_cmdline_args(args
, nb_args
);
3576 static int monitor_can_read(void *opaque
)
3578 Monitor
*mon
= opaque
;
3580 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3583 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
3586 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
3588 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3589 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3590 "Capabilities negotiation is already complete, command "
3591 "'%s' ignored", cmd
->name
);
3594 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3595 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3596 "Expecting capabilities negotiation with "
3597 "'qmp_capabilities' before command '%s'", cmd
->name
);
3604 * Argument validation rules:
3606 * 1. The argument must exist in cmd_args qdict
3607 * 2. The argument type must be the expected one
3609 * Special case: If the argument doesn't exist in cmd_args and
3610 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3611 * checking is skipped for it.
3613 static void check_client_args_type(const QDict
*client_args
,
3614 const QDict
*cmd_args
, int flags
,
3617 const QDictEntry
*ent
;
3619 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3622 const QObject
*client_arg
= qdict_entry_value(ent
);
3623 const char *client_arg_name
= qdict_entry_key(ent
);
3625 obj
= qdict_get(cmd_args
, client_arg_name
);
3627 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3628 /* handler accepts unknowns */
3631 /* client arg doesn't exist */
3632 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3636 arg_type
= qobject_to_qstring(obj
);
3637 assert(arg_type
!= NULL
);
3639 /* check if argument's type is correct */
3640 switch (qstring_get_str(arg_type
)[0]) {
3644 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3645 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3646 client_arg_name
, "string");
3654 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3655 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3656 client_arg_name
, "int");
3661 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3662 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3663 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3664 client_arg_name
, "number");
3670 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3671 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3672 client_arg_name
, "bool");
3677 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3680 /* Any QObject can be passed. */
3685 * These types are not supported by QMP and thus are not
3686 * handled here. Fall through.
3695 * - Check if the client has passed all mandatory args
3696 * - Set special flags for argument validation
3698 static void check_mandatory_args(const QDict
*cmd_args
,
3699 const QDict
*client_args
, int *flags
,
3702 const QDictEntry
*ent
;
3704 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3705 const char *cmd_arg_name
= qdict_entry_key(ent
);
3706 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3707 assert(type
!= NULL
);
3709 if (qstring_get_str(type
)[0] == 'O') {
3710 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3711 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3712 } else if (qstring_get_str(type
)[0] != '-' &&
3713 qstring_get_str(type
)[1] != '?' &&
3714 !qdict_haskey(client_args
, cmd_arg_name
)) {
3715 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3721 static QDict
*qdict_from_args_type(const char *args_type
)
3725 QString
*key
, *type
, *cur_qs
;
3727 assert(args_type
!= NULL
);
3729 qdict
= qdict_new();
3731 if (args_type
== NULL
|| args_type
[0] == '\0') {
3732 /* no args, empty qdict */
3736 key
= qstring_new();
3737 type
= qstring_new();
3742 switch (args_type
[i
]) {
3745 qdict_put(qdict
, qstring_get_str(key
), type
);
3747 if (args_type
[i
] == '\0') {
3750 type
= qstring_new(); /* qdict has ref */
3751 cur_qs
= key
= qstring_new();
3757 qstring_append_chr(cur_qs
, args_type
[i
]);
3767 * Client argument checking rules:
3769 * 1. Client must provide all mandatory arguments
3770 * 2. Each argument provided by the client must be expected
3771 * 3. Each argument provided by the client must have the type expected
3774 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3781 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3784 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3789 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3792 error_propagate(errp
, err
);
3797 * Input object checking rules
3799 * 1. Input object must be a dict
3800 * 2. The "execute" key must exist
3801 * 3. The "execute" key must be a string
3802 * 4. If the "arguments" key exists, it must be a dict
3803 * 5. If the "id" key exists, it can be anything (ie. json-value)
3804 * 6. Any argument not listed above is considered invalid
3806 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3808 const QDictEntry
*ent
;
3809 int has_exec_key
= 0;
3812 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3813 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3817 input_dict
= qobject_to_qdict(input_obj
);
3819 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3820 const char *arg_name
= qdict_entry_key(ent
);
3821 const QObject
*arg_obj
= qdict_entry_value(ent
);
3823 if (!strcmp(arg_name
, "execute")) {
3824 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3825 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3826 "execute", "string");
3830 } else if (!strcmp(arg_name
, "arguments")) {
3831 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3832 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3833 "arguments", "object");
3836 } else if (!strcmp(arg_name
, "id")) {
3837 /* Any string is acceptable as "id", so nothing to check */
3839 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3844 if (!has_exec_key
) {
3845 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3852 static void handle_qmp_command(JSONMessageParser
*parser
, QList
*tokens
)
3854 Error
*local_err
= NULL
;
3855 QObject
*obj
, *data
;
3856 QDict
*input
, *args
;
3857 const mon_cmd_t
*cmd
;
3858 const char *cmd_name
;
3859 Monitor
*mon
= cur_mon
;
3861 args
= input
= NULL
;
3864 obj
= json_parser_parse(tokens
, NULL
);
3866 // FIXME: should be triggered in json_parser_parse()
3867 error_setg(&local_err
, QERR_JSON_PARSING
);
3871 input
= qmp_check_input_obj(obj
, &local_err
);
3873 qobject_decref(obj
);
3877 mon
->qmp
.id
= qdict_get(input
, "id");
3878 qobject_incref(mon
->qmp
.id
);
3880 cmd_name
= qdict_get_str(input
, "execute");
3881 trace_handle_qmp_command(mon
, cmd_name
);
3882 cmd
= qmp_find_cmd(cmd_name
);
3884 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3885 "The command %s has not been found", cmd_name
);
3888 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
3892 obj
= qdict_get(input
, "arguments");
3896 args
= qobject_to_qdict(obj
);
3900 qmp_check_client_args(cmd
, args
, &local_err
);
3905 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
3908 monitor_protocol_emitter(mon
, data
, local_err
);
3909 qobject_decref(data
);
3914 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3916 Monitor
*old_mon
= cur_mon
;
3920 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3925 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3927 Monitor
*old_mon
= cur_mon
;
3933 for (i
= 0; i
< size
; i
++)
3934 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3936 if (size
== 0 || buf
[size
- 1] != 0)
3937 monitor_printf(cur_mon
, "corrupted command\n");
3939 handle_hmp_command(cur_mon
, (char *)buf
);
3945 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3946 void *readline_opaque
)
3948 Monitor
*mon
= opaque
;
3950 monitor_suspend(mon
);
3951 handle_hmp_command(mon
, cmdline
);
3952 monitor_resume(mon
);
3955 int monitor_suspend(Monitor
*mon
)
3963 void monitor_resume(Monitor
*mon
)
3967 if (--mon
->suspend_cnt
== 0)
3968 readline_show_prompt(mon
->rs
);
3971 static QObject
*get_qmp_greeting(void)
3973 QObject
*ver
= NULL
;
3975 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3976 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
3979 static void monitor_qmp_event(void *opaque
, int event
)
3982 Monitor
*mon
= opaque
;
3985 case CHR_EVENT_OPENED
:
3986 mon
->qmp
.in_command_mode
= false;
3987 data
= get_qmp_greeting();
3988 monitor_json_emitter(mon
, data
);
3989 qobject_decref(data
);
3992 case CHR_EVENT_CLOSED
:
3993 json_message_parser_destroy(&mon
->qmp
.parser
);
3994 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
3996 monitor_fdsets_cleanup();
4001 static void monitor_event(void *opaque
, int event
)
4003 Monitor
*mon
= opaque
;
4006 case CHR_EVENT_MUX_IN
:
4007 qemu_mutex_lock(&mon
->out_lock
);
4009 qemu_mutex_unlock(&mon
->out_lock
);
4010 if (mon
->reset_seen
) {
4011 readline_restart(mon
->rs
);
4012 monitor_resume(mon
);
4015 mon
->suspend_cnt
= 0;
4019 case CHR_EVENT_MUX_OUT
:
4020 if (mon
->reset_seen
) {
4021 if (mon
->suspend_cnt
== 0) {
4022 monitor_printf(mon
, "\n");
4025 monitor_suspend(mon
);
4029 qemu_mutex_lock(&mon
->out_lock
);
4031 qemu_mutex_unlock(&mon
->out_lock
);
4034 case CHR_EVENT_OPENED
:
4035 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4036 "information\n", QEMU_VERSION
);
4037 if (!mon
->mux_out
) {
4038 readline_restart(mon
->rs
);
4039 readline_show_prompt(mon
->rs
);
4041 mon
->reset_seen
= 1;
4045 case CHR_EVENT_CLOSED
:
4047 monitor_fdsets_cleanup();
4053 compare_mon_cmd(const void *a
, const void *b
)
4055 return strcmp(((const mon_cmd_t
*)a
)->name
,
4056 ((const mon_cmd_t
*)b
)->name
);
4059 static void sortcmdlist(void)
4062 int elem_size
= sizeof(mon_cmd_t
);
4064 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4065 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4067 array_num
= sizeof(info_cmds
)/elem_size
-1;
4068 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4080 /* These functions just adapt the readline interface in a typesafe way. We
4081 * could cast function pointers but that discards compiler checks.
4083 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4084 const char *fmt
, ...)
4088 monitor_vprintf(opaque
, fmt
, ap
);
4092 static void monitor_readline_flush(void *opaque
)
4094 monitor_flush(opaque
);
4097 static void __attribute__((constructor
)) monitor_lock_init(void)
4099 qemu_mutex_init(&monitor_lock
);
4102 void monitor_init(CharDriverState
*chr
, int flags
)
4104 static int is_first_init
= 1;
4107 if (is_first_init
) {
4108 monitor_qapi_event_init();
4113 mon
= g_malloc(sizeof(*mon
));
4114 monitor_data_init(mon
);
4118 if (flags
& MONITOR_USE_READLINE
) {
4119 mon
->rs
= readline_init(monitor_readline_printf
,
4120 monitor_readline_flush
,
4122 monitor_find_completion
);
4123 monitor_read_command(mon
, 0);
4126 if (monitor_is_qmp(mon
)) {
4127 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4128 monitor_qmp_event
, mon
);
4129 qemu_chr_fe_set_echo(chr
, true);
4130 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4132 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4133 monitor_event
, mon
);
4136 qemu_mutex_lock(&monitor_lock
);
4137 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4138 qemu_mutex_unlock(&monitor_lock
);
4141 static void bdrv_password_cb(void *opaque
, const char *password
,
4142 void *readline_opaque
)
4144 Monitor
*mon
= opaque
;
4145 BlockDriverState
*bs
= readline_opaque
;
4147 Error
*local_err
= NULL
;
4149 bdrv_add_key(bs
, password
, &local_err
);
4151 monitor_printf(mon
, "%s\n", error_get_pretty(local_err
));
4152 error_free(local_err
);
4155 if (mon
->password_completion_cb
)
4156 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4158 monitor_read_command(mon
, 1);
4161 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4162 BlockCompletionFunc
*completion_cb
,
4167 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4168 bdrv_get_encrypted_filename(bs
));
4170 mon
->password_completion_cb
= completion_cb
;
4171 mon
->password_opaque
= opaque
;
4173 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4175 if (err
&& completion_cb
)
4176 completion_cb(opaque
, err
);
4181 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4182 BlockCompletionFunc
*completion_cb
,
4188 blk
= blk_by_name(device
);
4190 monitor_printf(mon
, "Device not found %s\n", device
);
4194 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4198 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4201 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4204 if (completion_cb
) {
4205 completion_cb(opaque
, 0);
4210 QemuOptsList qemu_mon_opts
= {
4212 .implied_opt_name
= "chardev",
4213 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4217 .type
= QEMU_OPT_STRING
,
4220 .type
= QEMU_OPT_STRING
,
4223 .type
= QEMU_OPT_BOOL
,
4226 .type
= QEMU_OPT_BOOL
,
4228 { /* end of list */ }
4233 void qmp_rtc_reset_reinjection(Error
**errp
)
4235 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4239 #ifndef TARGET_S390X
4240 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4242 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");