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"
27 #include "monitor/qdev.h"
29 #include "hw/i386/pc.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
35 #include "net/slirp.h"
36 #include "sysemu/char.h"
37 #include "ui/qemu-spice.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/numa.h"
40 #include "monitor/monitor.h"
41 #include "qemu/readline.h"
42 #include "ui/console.h"
44 #include "sysemu/blockdev.h"
45 #include "audio/audio.h"
46 #include "disas/disas.h"
47 #include "sysemu/balloon.h"
48 #include "qemu/timer.h"
49 #include "migration/migration.h"
50 #include "sysemu/kvm.h"
52 #include "sysemu/tpm.h"
53 #include "qapi/qmp/qerror.h"
54 #include "qapi/qmp/qint.h"
55 #include "qapi/qmp/qfloat.h"
56 #include "qapi/qmp/qlist.h"
57 #include "qapi/qmp/qbool.h"
58 #include "qapi/qmp/qstring.h"
59 #include "qapi/qmp/qjson.h"
60 #include "qapi/qmp/json-streamer.h"
61 #include "qapi/qmp/json-parser.h"
62 #include <qom/object_interfaces.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 QapiErrorClass_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"));
575 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
576 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
582 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
584 const MonitorQAPIEventState
*eva
= a
;
585 const MonitorQAPIEventState
*evb
= b
;
587 if (eva
->event
!= evb
->event
) {
591 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
592 return !strcmp(qdict_get_str(eva
->data
, "id"),
593 qdict_get_str(evb
->data
, "id"));
596 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
597 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
598 qdict_get_str(evb
->data
, "node-name"));
604 static void monitor_qapi_event_init(void)
606 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
607 qapi_event_throttle_equal
);
608 qmp_event_set_func_emit(monitor_qapi_event_queue
);
611 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
613 cur_mon
->qmp
.in_command_mode
= true;
616 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
618 static void monitor_data_init(Monitor
*mon
)
620 memset(mon
, 0, sizeof(Monitor
));
621 qemu_mutex_init(&mon
->out_lock
);
622 mon
->outbuf
= qstring_new();
623 /* Use *mon_cmds by default. */
624 mon
->cmd_table
= mon_cmds
;
627 static void monitor_data_destroy(Monitor
*mon
)
629 QDECREF(mon
->outbuf
);
630 qemu_mutex_destroy(&mon
->out_lock
);
633 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
634 int64_t cpu_index
, Error
**errp
)
637 Monitor
*old_mon
, hmp
;
639 monitor_data_init(&hmp
);
640 hmp
.skip_flush
= true;
646 int ret
= monitor_set_cpu(cpu_index
);
649 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
655 handle_hmp_command(&hmp
, command_line
);
658 qemu_mutex_lock(&hmp
.out_lock
);
659 if (qstring_get_length(hmp
.outbuf
) > 0) {
660 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
662 output
= g_strdup("");
664 qemu_mutex_unlock(&hmp
.out_lock
);
667 monitor_data_destroy(&hmp
);
671 static int compare_cmd(const char *name
, const char *list
)
673 const char *p
, *pstart
;
681 p
= pstart
+ strlen(pstart
);
682 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
691 static int get_str(char *buf
, int buf_size
, const char **pp
)
699 while (qemu_isspace(*p
)) {
710 while (*p
!= '\0' && *p
!= '\"') {
726 printf("unsupported escape code: '\\%c'\n", c
);
729 if ((q
- buf
) < buf_size
- 1) {
733 if ((q
- buf
) < buf_size
- 1) {
740 printf("unterminated string\n");
745 while (*p
!= '\0' && !qemu_isspace(*p
)) {
746 if ((q
- buf
) < buf_size
- 1) {
759 static void free_cmdline_args(char **args
, int nb_args
)
763 assert(nb_args
<= MAX_ARGS
);
765 for (i
= 0; i
< nb_args
; i
++) {
772 * Parse the command line to get valid args.
773 * @cmdline: command line to be parsed.
774 * @pnb_args: location to store the number of args, must NOT be NULL.
775 * @args: location to store the args, which should be freed by caller, must
778 * Returns 0 on success, negative on failure.
780 * NOTE: this parser is an approximate form of the real command parser. Number
781 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
782 * return with failure.
784 static int parse_cmdline(const char *cmdline
,
785 int *pnb_args
, char **args
)
794 while (qemu_isspace(*p
)) {
800 if (nb_args
>= MAX_ARGS
) {
803 ret
= get_str(buf
, sizeof(buf
), &p
);
807 args
[nb_args
] = g_strdup(buf
);
814 free_cmdline_args(args
, nb_args
);
818 static void help_cmd_dump_one(Monitor
*mon
,
819 const mon_cmd_t
*cmd
,
825 for (i
= 0; i
< prefix_args_nb
; i
++) {
826 monitor_printf(mon
, "%s ", prefix_args
[i
]);
828 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
831 /* @args[@arg_index] is the valid command need to find in @cmds */
832 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
833 char **args
, int nb_args
, int arg_index
)
835 const mon_cmd_t
*cmd
;
837 /* No valid arg need to compare with, dump all in *cmds */
838 if (arg_index
>= nb_args
) {
839 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
840 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
845 /* Find one entry to dump */
846 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
847 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
848 if (cmd
->sub_table
) {
849 /* continue with next arg */
850 help_cmd_dump(mon
, cmd
->sub_table
,
851 args
, nb_args
, arg_index
+ 1);
853 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
860 static void help_cmd(Monitor
*mon
, const char *name
)
862 char *args
[MAX_ARGS
];
865 /* 1. parse user input */
867 /* special case for log, directly dump and return */
868 if (!strcmp(name
, "log")) {
869 const QEMULogItem
*item
;
870 monitor_printf(mon
, "Log items (comma separated):\n");
871 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
872 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
873 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
878 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
883 /* 2. dump the contents according to parsed args */
884 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
886 free_cmdline_args(args
, nb_args
);
889 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
891 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
894 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
896 const char *tp_name
= qdict_get_str(qdict
, "name");
897 bool new_state
= qdict_get_bool(qdict
, "option");
898 Error
*local_err
= NULL
;
900 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
902 error_report_err(local_err
);
906 #ifdef CONFIG_TRACE_SIMPLE
907 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
909 const char *op
= qdict_get_try_str(qdict
, "op");
910 const char *arg
= qdict_get_try_str(qdict
, "arg");
913 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
914 } else if (!strcmp(op
, "on")) {
915 st_set_trace_file_enabled(true);
916 } else if (!strcmp(op
, "off")) {
917 st_set_trace_file_enabled(false);
918 } else if (!strcmp(op
, "flush")) {
919 st_flush_trace_buffer();
920 } else if (!strcmp(op
, "set")) {
922 st_set_trace_file(arg
);
925 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
926 help_cmd(mon
, "trace-file");
931 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
933 help_cmd(mon
, "info");
936 CommandInfoList
*qmp_query_commands(Error
**errp
)
938 CommandInfoList
*info
, *cmd_list
= NULL
;
939 const mon_cmd_t
*cmd
;
941 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
942 info
= g_malloc0(sizeof(*info
));
943 info
->value
= g_malloc0(sizeof(*info
->value
));
944 info
->value
->name
= g_strdup(cmd
->name
);
946 info
->next
= cmd_list
;
953 EventInfoList
*qmp_query_events(Error
**errp
)
955 EventInfoList
*info
, *ev_list
= NULL
;
958 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
959 const char *event_name
= QAPIEvent_lookup
[e
];
960 assert(event_name
!= NULL
);
961 info
= g_malloc0(sizeof(*info
));
962 info
->value
= g_malloc0(sizeof(*info
->value
));
963 info
->value
->name
= g_strdup(event_name
);
965 info
->next
= ev_list
;
973 * Minor hack: generated marshalling suppressed for this command
974 * ('gen': false in the schema) so we can parse the JSON string
975 * directly into QObject instead of first parsing it with
976 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
977 * to QObject with generated output marshallers, every time. Instead,
978 * we do it in test-qmp-input-visitor.c, just to make sure
979 * qapi-introspect.py's output actually conforms to the schema.
981 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
984 *ret_data
= qobject_from_json(qmp_schema_json
);
987 /* set the current CPU defined by the user */
988 int monitor_set_cpu(int cpu_index
)
992 cpu
= qemu_get_cpu(cpu_index
);
996 cur_mon
->mon_cpu
= cpu
;
1000 CPUState
*mon_get_cpu(void)
1002 if (!cur_mon
->mon_cpu
) {
1005 cpu_synchronize_state(cur_mon
->mon_cpu
);
1006 return cur_mon
->mon_cpu
;
1009 CPUArchState
*mon_get_cpu_env(void)
1011 return mon_get_cpu()->env_ptr
;
1014 int monitor_get_cpu_index(void)
1016 return mon_get_cpu()->cpu_index
;
1019 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1021 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1024 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1026 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1027 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1030 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1032 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1035 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1044 str
= readline_get_history(mon
->rs
, i
);
1047 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1052 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1054 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1057 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1059 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1060 TraceEventInfoList
*elem
;
1062 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1063 monitor_printf(mon
, "%s : state %u\n",
1065 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1067 qapi_free_TraceEventInfoList(events
);
1070 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1071 bool has_port
, int64_t port
,
1072 bool has_tls_port
, int64_t tls_port
,
1073 bool has_cert_subject
, const char *cert_subject
,
1076 if (strcmp(protocol
, "spice") == 0) {
1077 if (!qemu_using_spice(errp
)) {
1081 if (!has_port
&& !has_tls_port
) {
1082 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1086 if (qemu_spice_migrate_info(hostname
,
1087 has_port
? port
: -1,
1088 has_tls_port
? tls_port
: -1,
1090 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1096 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1099 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1101 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1104 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1107 const char *items
= qdict_get_str(qdict
, "items");
1109 if (!strcmp(items
, "none")) {
1112 mask
= qemu_str_to_log_mask(items
);
1114 help_cmd(mon
, "log");
1121 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1123 const char *option
= qdict_get_try_str(qdict
, "option");
1124 if (!option
|| !strcmp(option
, "on")) {
1126 } else if (!strcmp(option
, "off")) {
1129 monitor_printf(mon
, "unexpected option %s\n", option
);
1133 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1135 const char *device
= qdict_get_try_str(qdict
, "device");
1137 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1138 if (gdbserver_start(device
) < 0) {
1139 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1141 } else if (strcmp(device
, "none") == 0) {
1142 monitor_printf(mon
, "Disabled gdbserver\n");
1144 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1149 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1151 const char *action
= qdict_get_str(qdict
, "action");
1152 if (select_watchdog_action(action
) == -1) {
1153 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1157 static void monitor_printc(Monitor
*mon
, int c
)
1159 monitor_printf(mon
, "'");
1162 monitor_printf(mon
, "\\'");
1165 monitor_printf(mon
, "\\\\");
1168 monitor_printf(mon
, "\\n");
1171 monitor_printf(mon
, "\\r");
1174 if (c
>= 32 && c
<= 126) {
1175 monitor_printf(mon
, "%c", c
);
1177 monitor_printf(mon
, "\\x%02x", c
);
1181 monitor_printf(mon
, "'");
1184 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1185 hwaddr addr
, int is_physical
)
1187 int l
, line_size
, i
, max_digits
, len
;
1191 if (format
== 'i') {
1194 CPUArchState
*env
= mon_get_cpu_env();
1197 } else if (wsize
== 4) {
1200 /* as default we use the current CS size */
1203 #ifdef TARGET_X86_64
1204 if ((env
->efer
& MSR_EFER_LMA
) &&
1205 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1209 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1215 CPUArchState
*env
= mon_get_cpu_env();
1216 flags
= msr_le
<< 16;
1217 flags
|= env
->bfd_mach
;
1219 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1223 len
= wsize
* count
;
1232 max_digits
= (wsize
* 8 + 2) / 3;
1236 max_digits
= (wsize
* 8) / 4;
1240 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1249 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1251 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1256 cpu_physical_memory_read(addr
, buf
, l
);
1258 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1259 monitor_printf(mon
, " Cannot access memory\n");
1268 v
= ldub_p(buf
+ i
);
1271 v
= lduw_p(buf
+ i
);
1274 v
= (uint32_t)ldl_p(buf
+ i
);
1280 monitor_printf(mon
, " ");
1283 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1286 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1289 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1292 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1295 monitor_printc(mon
, v
);
1300 monitor_printf(mon
, "\n");
1306 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1308 int count
= qdict_get_int(qdict
, "count");
1309 int format
= qdict_get_int(qdict
, "format");
1310 int size
= qdict_get_int(qdict
, "size");
1311 target_long addr
= qdict_get_int(qdict
, "addr");
1313 memory_dump(mon
, count
, format
, size
, addr
, 0);
1316 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1318 int count
= qdict_get_int(qdict
, "count");
1319 int format
= qdict_get_int(qdict
, "format");
1320 int size
= qdict_get_int(qdict
, "size");
1321 hwaddr addr
= qdict_get_int(qdict
, "addr");
1323 memory_dump(mon
, count
, format
, size
, addr
, 1);
1326 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1328 int format
= qdict_get_int(qdict
, "format");
1329 hwaddr val
= qdict_get_int(qdict
, "val");
1333 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1336 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1339 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1343 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1346 monitor_printc(mon
, val
);
1349 monitor_printf(mon
, "\n");
1352 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1356 uint32_t start
= qdict_get_int(qdict
, "start");
1357 uint32_t size
= qdict_get_int(qdict
, "size");
1360 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1361 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1362 MEMTXATTRS_UNSPECIFIED
, NULL
);
1363 /* BSD sum algorithm ('sum' Unix command) */
1364 sum
= (sum
>> 1) | (sum
<< 15);
1367 monitor_printf(mon
, "%05d\n", sum
);
1370 static int mouse_button_state
;
1372 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1374 int dx
, dy
, dz
, button
;
1375 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1376 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1377 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1379 dx
= strtol(dx_str
, NULL
, 0);
1380 dy
= strtol(dy_str
, NULL
, 0);
1381 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1382 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1385 dz
= strtol(dz_str
, NULL
, 0);
1387 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1388 qemu_input_queue_btn(NULL
, button
, true);
1389 qemu_input_event_sync();
1390 qemu_input_queue_btn(NULL
, button
, false);
1393 qemu_input_event_sync();
1396 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1398 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1399 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1400 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1401 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1403 int button_state
= qdict_get_int(qdict
, "button_state");
1405 if (mouse_button_state
== button_state
) {
1408 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1409 qemu_input_event_sync();
1410 mouse_button_state
= button_state
;
1413 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1415 int size
= qdict_get_int(qdict
, "size");
1416 int addr
= qdict_get_int(qdict
, "addr");
1417 int has_index
= qdict_haskey(qdict
, "index");
1422 int index
= qdict_get_int(qdict
, "index");
1423 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1431 val
= cpu_inb(addr
);
1435 val
= cpu_inw(addr
);
1439 val
= cpu_inl(addr
);
1443 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1444 suffix
, addr
, size
* 2, val
);
1447 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1449 int size
= qdict_get_int(qdict
, "size");
1450 int addr
= qdict_get_int(qdict
, "addr");
1451 int val
= qdict_get_int(qdict
, "val");
1453 addr
&= IOPORTS_MASK
;
1458 cpu_outb(addr
, val
);
1461 cpu_outw(addr
, val
);
1464 cpu_outl(addr
, val
);
1469 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1471 Error
*local_err
= NULL
;
1472 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1474 qemu_boot_set(bootdevice
, &local_err
);
1476 error_report_err(local_err
);
1478 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1482 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1484 mtree_info((fprintf_function
)monitor_printf
, mon
);
1487 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1493 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1494 query_numa_node_mem(node_mem
);
1495 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1496 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1497 monitor_printf(mon
, "node %d cpus:", i
);
1499 if (cpu
->numa_node
== i
) {
1500 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1503 monitor_printf(mon
, "\n");
1504 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1510 #ifdef CONFIG_PROFILER
1515 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1517 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1518 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1519 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1520 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1525 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1527 monitor_printf(mon
, "Internal profiler not compiled\n");
1531 /* Capture support */
1532 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1534 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1539 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1540 monitor_printf(mon
, "[%d]: ", i
);
1541 s
->ops
.info (s
->opaque
);
1545 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1548 int n
= qdict_get_int(qdict
, "n");
1551 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1553 s
->ops
.destroy (s
->opaque
);
1554 QLIST_REMOVE (s
, entries
);
1561 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1563 const char *path
= qdict_get_str(qdict
, "path");
1564 int has_freq
= qdict_haskey(qdict
, "freq");
1565 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1566 int has_bits
= qdict_haskey(qdict
, "bits");
1567 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1568 int has_channels
= qdict_haskey(qdict
, "nchannels");
1569 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1572 s
= g_malloc0 (sizeof (*s
));
1574 freq
= has_freq
? freq
: 44100;
1575 bits
= has_bits
? bits
: 16;
1576 nchannels
= has_channels
? nchannels
: 2;
1578 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1579 monitor_printf(mon
, "Failed to add wave capture\n");
1583 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1586 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1588 qemu_acl
*acl
= qemu_acl_find(name
);
1591 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1596 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1598 const char *aclname
= qdict_get_str(qdict
, "aclname");
1599 qemu_acl
*acl
= find_acl(mon
, aclname
);
1600 qemu_acl_entry
*entry
;
1604 monitor_printf(mon
, "policy: %s\n",
1605 acl
->defaultDeny
? "deny" : "allow");
1606 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1608 monitor_printf(mon
, "%d: %s %s\n", i
,
1609 entry
->deny
? "deny" : "allow", entry
->match
);
1614 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1616 const char *aclname
= qdict_get_str(qdict
, "aclname");
1617 qemu_acl
*acl
= find_acl(mon
, aclname
);
1620 qemu_acl_reset(acl
);
1621 monitor_printf(mon
, "acl: removed all rules\n");
1625 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1627 const char *aclname
= qdict_get_str(qdict
, "aclname");
1628 const char *policy
= qdict_get_str(qdict
, "policy");
1629 qemu_acl
*acl
= find_acl(mon
, aclname
);
1632 if (strcmp(policy
, "allow") == 0) {
1633 acl
->defaultDeny
= 0;
1634 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1635 } else if (strcmp(policy
, "deny") == 0) {
1636 acl
->defaultDeny
= 1;
1637 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1639 monitor_printf(mon
, "acl: unknown policy '%s', "
1640 "expected 'deny' or 'allow'\n", policy
);
1645 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1647 const char *aclname
= qdict_get_str(qdict
, "aclname");
1648 const char *match
= qdict_get_str(qdict
, "match");
1649 const char *policy
= qdict_get_str(qdict
, "policy");
1650 int has_index
= qdict_haskey(qdict
, "index");
1651 int index
= qdict_get_try_int(qdict
, "index", -1);
1652 qemu_acl
*acl
= find_acl(mon
, aclname
);
1656 if (strcmp(policy
, "allow") == 0) {
1658 } else if (strcmp(policy
, "deny") == 0) {
1661 monitor_printf(mon
, "acl: unknown policy '%s', "
1662 "expected 'deny' or 'allow'\n", policy
);
1666 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1668 ret
= qemu_acl_append(acl
, deny
, match
);
1670 monitor_printf(mon
, "acl: unable to add acl entry\n");
1672 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1676 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1678 const char *aclname
= qdict_get_str(qdict
, "aclname");
1679 const char *match
= qdict_get_str(qdict
, "match");
1680 qemu_acl
*acl
= find_acl(mon
, aclname
);
1684 ret
= qemu_acl_remove(acl
, match
);
1686 monitor_printf(mon
, "acl: no matching acl entry\n");
1688 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1692 void qmp_getfd(const char *fdname
, Error
**errp
)
1697 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1699 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1703 if (qemu_isdigit(fdname
[0])) {
1705 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1706 "a name not starting with a digit");
1710 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1711 if (strcmp(monfd
->name
, fdname
) != 0) {
1720 monfd
= g_malloc0(sizeof(mon_fd_t
));
1721 monfd
->name
= g_strdup(fdname
);
1724 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1727 void qmp_closefd(const char *fdname
, Error
**errp
)
1731 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1732 if (strcmp(monfd
->name
, fdname
) != 0) {
1736 QLIST_REMOVE(monfd
, next
);
1738 g_free(monfd
->name
);
1743 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1746 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1748 int saved_vm_running
= runstate_is_running();
1749 const char *name
= qdict_get_str(qdict
, "name");
1751 vm_stop(RUN_STATE_RESTORE_VM
);
1753 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1758 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1762 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1765 if (strcmp(monfd
->name
, fdname
) != 0) {
1771 /* caller takes ownership of fd */
1772 QLIST_REMOVE(monfd
, next
);
1773 g_free(monfd
->name
);
1779 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1783 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1785 MonFdsetFd
*mon_fdset_fd
;
1786 MonFdsetFd
*mon_fdset_fd_next
;
1788 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1789 if ((mon_fdset_fd
->removed
||
1790 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1791 runstate_is_running()) {
1792 close(mon_fdset_fd
->fd
);
1793 g_free(mon_fdset_fd
->opaque
);
1794 QLIST_REMOVE(mon_fdset_fd
, next
);
1795 g_free(mon_fdset_fd
);
1799 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1800 QLIST_REMOVE(mon_fdset
, next
);
1805 static void monitor_fdsets_cleanup(void)
1807 MonFdset
*mon_fdset
;
1808 MonFdset
*mon_fdset_next
;
1810 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1811 monitor_fdset_cleanup(mon_fdset
);
1815 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1816 const char *opaque
, Error
**errp
)
1819 Monitor
*mon
= cur_mon
;
1822 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1824 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1828 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1829 has_opaque
, opaque
, errp
);
1841 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1843 MonFdset
*mon_fdset
;
1844 MonFdsetFd
*mon_fdset_fd
;
1847 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1848 if (mon_fdset
->id
!= fdset_id
) {
1851 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1853 if (mon_fdset_fd
->fd
!= fd
) {
1856 mon_fdset_fd
->removed
= true;
1859 mon_fdset_fd
->removed
= true;
1862 if (has_fd
&& !mon_fdset_fd
) {
1865 monitor_fdset_cleanup(mon_fdset
);
1871 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1874 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1876 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1879 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1881 MonFdset
*mon_fdset
;
1882 MonFdsetFd
*mon_fdset_fd
;
1883 FdsetInfoList
*fdset_list
= NULL
;
1885 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1886 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1887 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1889 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1890 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1892 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1893 FdsetFdInfoList
*fdsetfd_info
;
1895 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1896 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1897 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1898 if (mon_fdset_fd
->opaque
) {
1899 fdsetfd_info
->value
->has_opaque
= true;
1900 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1902 fdsetfd_info
->value
->has_opaque
= false;
1905 fdsetfd_info
->next
= fdsetfd_list
;
1906 fdsetfd_list
= fdsetfd_info
;
1909 fdset_info
->value
->fds
= fdsetfd_list
;
1911 fdset_info
->next
= fdset_list
;
1912 fdset_list
= fdset_info
;
1918 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1919 bool has_opaque
, const char *opaque
,
1922 MonFdset
*mon_fdset
= NULL
;
1923 MonFdsetFd
*mon_fdset_fd
;
1927 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1928 /* Break if match found or match impossible due to ordering by ID */
1929 if (fdset_id
<= mon_fdset
->id
) {
1930 if (fdset_id
< mon_fdset
->id
) {
1938 if (mon_fdset
== NULL
) {
1939 int64_t fdset_id_prev
= -1;
1940 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1944 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1945 "a non-negative value");
1948 /* Use specified fdset ID */
1949 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1950 mon_fdset_cur
= mon_fdset
;
1951 if (fdset_id
< mon_fdset_cur
->id
) {
1956 /* Use first available fdset ID */
1957 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1958 mon_fdset_cur
= mon_fdset
;
1959 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1960 fdset_id_prev
= mon_fdset_cur
->id
;
1967 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1969 mon_fdset
->id
= fdset_id
;
1971 mon_fdset
->id
= fdset_id_prev
+ 1;
1974 /* The fdset list is ordered by fdset ID */
1975 if (!mon_fdset_cur
) {
1976 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1977 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1978 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1980 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1984 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1985 mon_fdset_fd
->fd
= fd
;
1986 mon_fdset_fd
->removed
= false;
1988 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1990 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
1992 fdinfo
= g_malloc0(sizeof(*fdinfo
));
1993 fdinfo
->fdset_id
= mon_fdset
->id
;
1994 fdinfo
->fd
= mon_fdset_fd
->fd
;
1999 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2002 MonFdset
*mon_fdset
;
2003 MonFdsetFd
*mon_fdset_fd
;
2006 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2007 if (mon_fdset
->id
!= fdset_id
) {
2010 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2011 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2012 if (mon_fd_flags
== -1) {
2016 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2017 return mon_fdset_fd
->fd
;
2029 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2031 MonFdset
*mon_fdset
;
2032 MonFdsetFd
*mon_fdset_fd_dup
;
2034 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2035 if (mon_fdset
->id
!= fdset_id
) {
2038 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2039 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2043 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2044 mon_fdset_fd_dup
->fd
= dup_fd
;
2045 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2051 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2053 MonFdset
*mon_fdset
;
2054 MonFdsetFd
*mon_fdset_fd_dup
;
2056 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2057 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2058 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2060 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2061 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2062 monitor_fdset_cleanup(mon_fdset
);
2066 return mon_fdset
->id
;
2074 int monitor_fdset_dup_fd_find(int dup_fd
)
2076 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2079 void monitor_fdset_dup_fd_remove(int dup_fd
)
2081 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2084 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2087 Error
*local_err
= NULL
;
2089 if (!qemu_isdigit(fdname
[0]) && mon
) {
2090 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2092 fd
= qemu_parse_fd(fdname
);
2094 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2099 error_propagate(errp
, local_err
);
2108 /* Please update hmp-commands.hx when adding or changing commands */
2109 static mon_cmd_t info_cmds
[] = {
2110 #include "hmp-commands-info.h"
2114 /* mon_cmds and info_cmds would be sorted at runtime */
2115 static mon_cmd_t mon_cmds
[] = {
2116 #include "hmp-commands.h"
2120 static const mon_cmd_t qmp_cmds
[] = {
2121 #include "qmp-commands-old.h"
2125 /*******************************************************************/
2127 static const char *pch
;
2128 static sigjmp_buf expr_env
;
2131 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2132 expr_error(Monitor
*mon
, const char *fmt
, ...)
2136 monitor_vprintf(mon
, fmt
, ap
);
2137 monitor_printf(mon
, "\n");
2139 siglongjmp(expr_env
, 1);
2142 /* return 0 if OK, -1 if not found */
2143 static int get_monitor_def(target_long
*pval
, const char *name
)
2145 const MonitorDef
*md
= target_monitor_defs();
2154 for(; md
->name
!= NULL
; md
++) {
2155 if (compare_cmd(name
, md
->name
)) {
2156 if (md
->get_value
) {
2157 *pval
= md
->get_value(md
, md
->offset
);
2159 CPUArchState
*env
= mon_get_cpu_env();
2160 ptr
= (uint8_t *)env
+ md
->offset
;
2163 *pval
= *(int32_t *)ptr
;
2166 *pval
= *(target_long
*)ptr
;
2177 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2179 *pval
= (target_long
) tmp
;
2185 static void next(void)
2189 while (qemu_isspace(*pch
))
2194 static int64_t expr_sum(Monitor
*mon
);
2196 static int64_t expr_unary(Monitor
*mon
)
2205 n
= expr_unary(mon
);
2209 n
= -expr_unary(mon
);
2213 n
= ~expr_unary(mon
);
2219 expr_error(mon
, "')' expected");
2226 expr_error(mon
, "character constant expected");
2230 expr_error(mon
, "missing terminating \' character");
2240 while ((*pch
>= 'a' && *pch
<= 'z') ||
2241 (*pch
>= 'A' && *pch
<= 'Z') ||
2242 (*pch
>= '0' && *pch
<= '9') ||
2243 *pch
== '_' || *pch
== '.') {
2244 if ((q
- buf
) < sizeof(buf
) - 1)
2248 while (qemu_isspace(*pch
))
2251 ret
= get_monitor_def(®
, buf
);
2253 expr_error(mon
, "unknown register");
2258 expr_error(mon
, "unexpected end of expression");
2263 n
= strtoull(pch
, &p
, 0);
2264 if (errno
== ERANGE
) {
2265 expr_error(mon
, "number too large");
2268 expr_error(mon
, "invalid char '%c' in expression", *p
);
2271 while (qemu_isspace(*pch
))
2279 static int64_t expr_prod(Monitor
*mon
)
2284 val
= expr_unary(mon
);
2287 if (op
!= '*' && op
!= '/' && op
!= '%')
2290 val2
= expr_unary(mon
);
2299 expr_error(mon
, "division by zero");
2310 static int64_t expr_logic(Monitor
*mon
)
2315 val
= expr_prod(mon
);
2318 if (op
!= '&' && op
!= '|' && op
!= '^')
2321 val2
= expr_prod(mon
);
2338 static int64_t expr_sum(Monitor
*mon
)
2343 val
= expr_logic(mon
);
2346 if (op
!= '+' && op
!= '-')
2349 val2
= expr_logic(mon
);
2358 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2361 if (sigsetjmp(expr_env
, 0)) {
2365 while (qemu_isspace(*pch
))
2367 *pval
= expr_sum(mon
);
2372 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2374 const char *p
= *pp
;
2378 d
= strtod(p
, &tailp
);
2380 monitor_printf(mon
, "Number expected\n");
2383 if (d
!= d
|| d
- d
!= 0) {
2384 /* NaN or infinity */
2385 monitor_printf(mon
, "Bad number\n");
2394 * Store the command-name in cmdname, and return a pointer to
2395 * the remaining of the command string.
2397 static const char *get_command_name(const char *cmdline
,
2398 char *cmdname
, size_t nlen
)
2401 const char *p
, *pstart
;
2404 while (qemu_isspace(*p
))
2409 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2414 memcpy(cmdname
, pstart
, len
);
2415 cmdname
[len
] = '\0';
2420 * Read key of 'type' into 'key' and return the current
2423 static char *key_get_info(const char *type
, char **key
)
2431 p
= strchr(type
, ':');
2438 str
= g_malloc(len
+ 1);
2439 memcpy(str
, type
, len
);
2446 static int default_fmt_format
= 'x';
2447 static int default_fmt_size
= 4;
2449 static int is_valid_option(const char *c
, const char *typestr
)
2457 typestr
= strstr(typestr
, option
);
2458 return (typestr
!= NULL
);
2461 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2462 const char *cmdname
)
2464 const mon_cmd_t
*cmd
;
2466 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2467 if (compare_cmd(cmdname
, cmd
->name
)) {
2475 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2477 return search_dispatch_table(qmp_cmds
, cmdname
);
2481 * Parse command name from @cmdp according to command table @table.
2482 * If blank, return NULL.
2483 * Else, if no valid command can be found, report to @mon, and return
2485 * Else, change @cmdp to point right behind the name, and return its
2486 * command table entry.
2487 * Do not assume the return value points into @table! It doesn't when
2488 * the command is found in a sub-command table.
2490 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2495 const mon_cmd_t
*cmd
;
2498 /* extract the command name */
2499 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2503 cmd
= search_dispatch_table(table
, cmdname
);
2505 monitor_printf(mon
, "unknown command: '%.*s'\n",
2506 (int)(p
- *cmdp
), *cmdp
);
2510 /* filter out following useless space */
2511 while (qemu_isspace(*p
)) {
2516 /* search sub command */
2517 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2518 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2525 * Parse arguments for @cmd.
2526 * If it can't be parsed, report to @mon, and return NULL.
2527 * Else, insert command arguments into a QDict, and return it.
2528 * Note: On success, caller has to free the QDict structure.
2531 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2533 const mon_cmd_t
*cmd
)
2535 const char *typestr
;
2538 const char *p
= *endp
;
2540 QDict
*qdict
= qdict_new();
2542 /* parse the parameters */
2543 typestr
= cmd
->args_type
;
2545 typestr
= key_get_info(typestr
, &key
);
2557 while (qemu_isspace(*p
))
2559 if (*typestr
== '?') {
2562 /* no optional string: NULL argument */
2566 ret
= get_str(buf
, sizeof(buf
), &p
);
2570 monitor_printf(mon
, "%s: filename expected\n",
2574 monitor_printf(mon
, "%s: block device name expected\n",
2578 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2583 qdict_put(qdict
, key
, qstring_from_str(buf
));
2588 QemuOptsList
*opts_list
;
2591 opts_list
= qemu_find_opts(key
);
2592 if (!opts_list
|| opts_list
->desc
->name
) {
2595 while (qemu_isspace(*p
)) {
2600 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2603 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2607 qemu_opts_to_qdict(opts
, qdict
);
2608 qemu_opts_del(opts
);
2613 int count
, format
, size
;
2615 while (qemu_isspace(*p
))
2621 if (qemu_isdigit(*p
)) {
2623 while (qemu_isdigit(*p
)) {
2624 count
= count
* 10 + (*p
- '0');
2662 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2663 monitor_printf(mon
, "invalid char in format: '%c'\n",
2668 format
= default_fmt_format
;
2669 if (format
!= 'i') {
2670 /* for 'i', not specifying a size gives -1 as size */
2672 size
= default_fmt_size
;
2673 default_fmt_size
= size
;
2675 default_fmt_format
= format
;
2678 format
= default_fmt_format
;
2679 if (format
!= 'i') {
2680 size
= default_fmt_size
;
2685 qdict_put(qdict
, "count", qint_from_int(count
));
2686 qdict_put(qdict
, "format", qint_from_int(format
));
2687 qdict_put(qdict
, "size", qint_from_int(size
));
2696 while (qemu_isspace(*p
))
2698 if (*typestr
== '?' || *typestr
== '.') {
2699 if (*typestr
== '?') {
2707 while (qemu_isspace(*p
))
2716 if (get_expr(mon
, &val
, &p
))
2718 /* Check if 'i' is greater than 32-bit */
2719 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2720 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2721 monitor_printf(mon
, "integer is for 32-bit values\n");
2723 } else if (c
== 'M') {
2725 monitor_printf(mon
, "enter a positive value\n");
2730 qdict_put(qdict
, key
, qint_from_int(val
));
2738 while (qemu_isspace(*p
)) {
2741 if (*typestr
== '?') {
2747 val
= qemu_strtosz(p
, &end
);
2749 monitor_printf(mon
, "invalid size\n");
2752 qdict_put(qdict
, key
, qint_from_int(val
));
2760 while (qemu_isspace(*p
))
2762 if (*typestr
== '?') {
2768 if (get_double(mon
, &val
, &p
) < 0) {
2771 if (p
[0] && p
[1] == 's') {
2774 val
/= 1e3
; p
+= 2; break;
2776 val
/= 1e6
; p
+= 2; break;
2778 val
/= 1e9
; p
+= 2; break;
2781 if (*p
&& !qemu_isspace(*p
)) {
2782 monitor_printf(mon
, "Unknown unit suffix\n");
2785 qdict_put(qdict
, key
, qfloat_from_double(val
));
2793 while (qemu_isspace(*p
)) {
2797 while (qemu_isgraph(*p
)) {
2800 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2802 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2805 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2808 qdict_put(qdict
, key
, qbool_from_bool(val
));
2813 const char *tmp
= p
;
2820 while (qemu_isspace(*p
))
2825 if(!is_valid_option(p
, typestr
)) {
2827 monitor_printf(mon
, "%s: unsupported option -%c\n",
2839 qdict_put(qdict
, key
, qbool_from_bool(true));
2846 /* package all remaining string */
2849 while (qemu_isspace(*p
)) {
2852 if (*typestr
== '?') {
2855 /* no remaining string: NULL argument */
2861 monitor_printf(mon
, "%s: string expected\n",
2865 qdict_put(qdict
, key
, qstring_from_str(p
));
2871 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2877 /* check that all arguments were parsed */
2878 while (qemu_isspace(*p
))
2881 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2894 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2897 const mon_cmd_t
*cmd
;
2899 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2904 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2906 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2911 cmd
->mhandler
.cmd(mon
, qdict
);
2915 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2917 const char *p
, *pstart
;
2926 p
= pstart
+ strlen(pstart
);
2928 if (len
> sizeof(cmd
) - 2)
2929 len
= sizeof(cmd
) - 2;
2930 memcpy(cmd
, pstart
, len
);
2932 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2933 readline_add_completion(mon
->rs
, cmd
);
2941 static void file_completion(Monitor
*mon
, const char *input
)
2946 char file
[1024], file_prefix
[1024];
2950 p
= strrchr(input
, '/');
2953 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2954 pstrcpy(path
, sizeof(path
), ".");
2956 input_path_len
= p
- input
+ 1;
2957 memcpy(path
, input
, input_path_len
);
2958 if (input_path_len
> sizeof(path
) - 1)
2959 input_path_len
= sizeof(path
) - 1;
2960 path
[input_path_len
] = '\0';
2961 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2964 ffs
= opendir(path
);
2973 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
2977 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2978 memcpy(file
, input
, input_path_len
);
2979 if (input_path_len
< sizeof(file
))
2980 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2982 /* stat the file to find out if it's a directory.
2983 * In that case add a slash to speed up typing long paths
2985 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
2986 pstrcat(file
, sizeof(file
), "/");
2988 readline_add_completion(mon
->rs
, file
);
2994 static const char *next_arg_type(const char *typestr
)
2996 const char *p
= strchr(typestr
, ':');
2997 return (p
!= NULL
? ++p
: typestr
);
3000 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3003 if (!str
|| !option
) {
3006 if (!strncmp(option
, str
, strlen(str
))) {
3007 readline_add_completion(rs
, option
);
3011 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3014 ChardevBackendInfoList
*list
, *start
;
3020 readline_set_completion_index(rs
, len
);
3022 start
= list
= qmp_query_chardev_backends(NULL
);
3024 const char *chr_name
= list
->value
->name
;
3026 if (!strncmp(chr_name
, str
, len
)) {
3027 readline_add_completion(rs
, chr_name
);
3031 qapi_free_ChardevBackendInfoList(start
);
3034 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3043 readline_set_completion_index(rs
, len
);
3044 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
3045 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3049 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3059 readline_set_completion_index(rs
, len
);
3060 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3063 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3065 name
= object_class_get_name(OBJECT_CLASS(dc
));
3067 if (!dc
->cannot_instantiate_with_device_add_yet
3068 && !strncmp(name
, str
, len
)) {
3069 readline_add_completion(rs
, name
);
3076 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3086 readline_set_completion_index(rs
, len
);
3087 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3091 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3092 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3093 readline_add_completion(rs
, name
);
3100 static void peripheral_device_del_completion(ReadLineState
*rs
,
3101 const char *str
, size_t len
)
3103 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3104 GSList
*list
, *item
;
3106 list
= qdev_build_hotpluggable_device_list(peripheral
);
3111 for (item
= list
; item
; item
= g_slist_next(item
)) {
3112 DeviceState
*dev
= item
->data
;
3114 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3115 readline_add_completion(rs
, dev
->id
);
3122 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3125 ChardevInfoList
*list
, *start
;
3131 readline_set_completion_index(rs
, len
);
3133 start
= list
= qmp_query_chardev(NULL
);
3135 ChardevInfo
*chr
= list
->value
;
3137 if (!strncmp(chr
->label
, str
, len
)) {
3138 readline_add_completion(rs
, chr
->label
);
3142 qapi_free_ChardevInfoList(start
);
3145 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3148 ChardevInfoList
*list
, *start
;
3151 readline_set_completion_index(rs
, len
);
3153 start
= list
= qmp_query_chardev(NULL
);
3155 ChardevInfo
*chr_info
= list
->value
;
3157 if (!strncmp(chr_info
->label
, str
, len
)) {
3158 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3159 if (chr
&& chr_is_ringbuf(chr
)) {
3160 readline_add_completion(rs
, chr_info
->label
);
3165 qapi_free_ChardevInfoList(start
);
3168 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3173 ringbuf_completion(rs
, str
);
3176 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3185 readline_set_completion_index(rs
, len
);
3186 peripheral_device_del_completion(rs
, str
, len
);
3189 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3191 ObjectPropertyInfoList
*list
, *start
;
3198 readline_set_completion_index(rs
, len
);
3200 start
= list
= qmp_qom_list("/objects", NULL
);
3202 ObjectPropertyInfo
*info
= list
->value
;
3204 if (!strncmp(info
->type
, "child<", 5)
3205 && !strncmp(info
->name
, str
, len
)) {
3206 readline_add_completion(rs
, info
->name
);
3210 qapi_free_ObjectPropertyInfoList(start
);
3213 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3222 sep
= strrchr(str
, '-');
3227 readline_set_completion_index(rs
, len
);
3228 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3229 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3230 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3235 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3240 readline_set_completion_index(rs
, len
);
3242 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3244 count
= qemu_find_net_clients_except(NULL
, ncs
,
3245 NET_CLIENT_OPTIONS_KIND_NONE
,
3247 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3248 const char *name
= ncs
[i
]->name
;
3249 if (!strncmp(str
, name
, len
)) {
3250 readline_add_completion(rs
, name
);
3253 } else if (nb_args
== 3) {
3254 add_completion_option(rs
, str
, "on");
3255 add_completion_option(rs
, str
, "off");
3259 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3262 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3269 readline_set_completion_index(rs
, len
);
3270 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
3272 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3274 const char *name
= ncs
[i
]->name
;
3275 if (strncmp(str
, name
, len
)) {
3278 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3280 readline_add_completion(rs
, name
);
3285 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3290 readline_set_completion_index(rs
, len
);
3293 for (id
= 0; id
< trace_event_count(); id
++) {
3294 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3295 if (!strncmp(str
, event_name
, len
)) {
3296 readline_add_completion(rs
, event_name
);
3299 } else if (nb_args
== 3) {
3300 add_completion_option(rs
, str
, "on");
3301 add_completion_option(rs
, str
, "off");
3305 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3312 readline_set_completion_index(rs
, strlen(str
));
3313 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3314 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3318 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3324 readline_set_completion_index(rs
, len
);
3327 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3328 const char *name
= MigrationCapability_lookup
[i
];
3329 if (!strncmp(str
, name
, len
)) {
3330 readline_add_completion(rs
, name
);
3333 } else if (nb_args
== 3) {
3334 add_completion_option(rs
, str
, "on");
3335 add_completion_option(rs
, str
, "off");
3339 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3345 readline_set_completion_index(rs
, len
);
3348 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3349 const char *name
= MigrationParameter_lookup
[i
];
3350 if (!strncmp(str
, name
, len
)) {
3351 readline_add_completion(rs
, name
);
3357 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3365 readline_set_completion_index(rs
, len
);
3366 for (i
= 0; host_net_devices
[i
]; i
++) {
3367 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3368 readline_add_completion(rs
, host_net_devices
[i
]);
3373 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3375 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3379 readline_set_completion_index(rs
, len
);
3381 count
= qemu_find_net_clients_except(NULL
, ncs
,
3382 NET_CLIENT_OPTIONS_KIND_NONE
,
3384 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3388 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3391 snprintf(name
, sizeof(name
), "%d", id
);
3392 if (!strncmp(str
, name
, len
)) {
3393 readline_add_completion(rs
, name
);
3397 } else if (nb_args
== 3) {
3398 count
= qemu_find_net_clients_except(NULL
, ncs
,
3399 NET_CLIENT_OPTIONS_KIND_NIC
,
3401 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3405 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
3406 net_hub_id_for_client(ncs
[i
], &id
)) {
3409 name
= ncs
[i
]->name
;
3410 if (!strncmp(str
, name
, len
)) {
3411 readline_add_completion(rs
, name
);
3418 static void vm_completion(ReadLineState
*rs
, const char *str
)
3421 BlockDriverState
*bs
= NULL
;
3424 readline_set_completion_index(rs
, len
);
3425 while ((bs
= bdrv_next(bs
))) {
3426 SnapshotInfoList
*snapshots
, *snapshot
;
3427 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3430 aio_context_acquire(ctx
);
3431 if (bdrv_can_snapshot(bs
)) {
3432 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3434 aio_context_release(ctx
);
3439 snapshot
= snapshots
;
3441 char *completion
= snapshot
->value
->name
;
3442 if (!strncmp(str
, completion
, len
)) {
3443 readline_add_completion(rs
, completion
);
3445 completion
= snapshot
->value
->id
;
3446 if (!strncmp(str
, completion
, len
)) {
3447 readline_add_completion(rs
, completion
);
3449 snapshot
= snapshot
->next
;
3451 qapi_free_SnapshotInfoList(snapshots
);
3456 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3459 vm_completion(rs
, str
);
3463 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3466 vm_completion(rs
, str
);
3470 static void monitor_find_completion_by_table(Monitor
*mon
,
3471 const mon_cmd_t
*cmd_table
,
3475 const char *cmdname
;
3477 const char *ptype
, *str
, *name
;
3478 const mon_cmd_t
*cmd
;
3479 BlockDriverState
*bs
;
3482 /* command completion */
3487 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3488 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3489 cmd_completion(mon
, cmdname
, cmd
->name
);
3492 /* find the command */
3493 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3494 if (compare_cmd(args
[0], cmd
->name
)) {
3502 if (cmd
->sub_table
) {
3503 /* do the job again */
3504 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3505 &args
[1], nb_args
- 1);
3508 if (cmd
->command_completion
) {
3509 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3513 ptype
= next_arg_type(cmd
->args_type
);
3514 for(i
= 0; i
< nb_args
- 2; i
++) {
3515 if (*ptype
!= '\0') {
3516 ptype
= next_arg_type(ptype
);
3517 while (*ptype
== '?')
3518 ptype
= next_arg_type(ptype
);
3521 str
= args
[nb_args
- 1];
3522 while (*ptype
== '-' && ptype
[1] != '\0') {
3523 ptype
= next_arg_type(ptype
);
3527 /* file completion */
3528 readline_set_completion_index(mon
->rs
, strlen(str
));
3529 file_completion(mon
, str
);
3532 /* block device name completion */
3533 readline_set_completion_index(mon
->rs
, strlen(str
));
3534 for (bs
= bdrv_next(NULL
); bs
; bs
= bdrv_next(bs
)) {
3535 name
= bdrv_get_device_name(bs
);
3536 if (str
[0] == '\0' ||
3537 !strncmp(name
, str
, strlen(str
))) {
3538 readline_add_completion(mon
->rs
, name
);
3544 if (!strcmp(cmd
->name
, "help|?")) {
3545 monitor_find_completion_by_table(mon
, cmd_table
,
3546 &args
[1], nb_args
- 1);
3555 static void monitor_find_completion(void *opaque
,
3556 const char *cmdline
)
3558 Monitor
*mon
= opaque
;
3559 char *args
[MAX_ARGS
];
3562 /* 1. parse the cmdline */
3563 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3567 /* if the line ends with a space, it means we want to complete the
3569 len
= strlen(cmdline
);
3570 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3571 if (nb_args
>= MAX_ARGS
) {
3574 args
[nb_args
++] = g_strdup("");
3577 /* 2. auto complete according to args */
3578 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3581 free_cmdline_args(args
, nb_args
);
3584 static int monitor_can_read(void *opaque
)
3586 Monitor
*mon
= opaque
;
3588 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3591 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
3594 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
3596 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3597 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3598 "Capabilities negotiation is already complete, command "
3599 "'%s' ignored", cmd
->name
);
3602 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3603 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3604 "Expecting capabilities negotiation with "
3605 "'qmp_capabilities' before command '%s'", cmd
->name
);
3612 * Argument validation rules:
3614 * 1. The argument must exist in cmd_args qdict
3615 * 2. The argument type must be the expected one
3617 * Special case: If the argument doesn't exist in cmd_args and
3618 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3619 * checking is skipped for it.
3621 static void check_client_args_type(const QDict
*client_args
,
3622 const QDict
*cmd_args
, int flags
,
3625 const QDictEntry
*ent
;
3627 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3630 const QObject
*client_arg
= qdict_entry_value(ent
);
3631 const char *client_arg_name
= qdict_entry_key(ent
);
3633 obj
= qdict_get(cmd_args
, client_arg_name
);
3635 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3636 /* handler accepts unknowns */
3639 /* client arg doesn't exist */
3640 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3644 arg_type
= qobject_to_qstring(obj
);
3645 assert(arg_type
!= NULL
);
3647 /* check if argument's type is correct */
3648 switch (qstring_get_str(arg_type
)[0]) {
3652 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3653 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3654 client_arg_name
, "string");
3662 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3663 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3664 client_arg_name
, "int");
3669 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3670 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3671 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3672 client_arg_name
, "number");
3678 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3679 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3680 client_arg_name
, "bool");
3685 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3688 /* Any QObject can be passed. */
3693 * These types are not supported by QMP and thus are not
3694 * handled here. Fall through.
3703 * - Check if the client has passed all mandatory args
3704 * - Set special flags for argument validation
3706 static void check_mandatory_args(const QDict
*cmd_args
,
3707 const QDict
*client_args
, int *flags
,
3710 const QDictEntry
*ent
;
3712 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3713 const char *cmd_arg_name
= qdict_entry_key(ent
);
3714 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3715 assert(type
!= NULL
);
3717 if (qstring_get_str(type
)[0] == 'O') {
3718 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3719 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3720 } else if (qstring_get_str(type
)[0] != '-' &&
3721 qstring_get_str(type
)[1] != '?' &&
3722 !qdict_haskey(client_args
, cmd_arg_name
)) {
3723 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3729 static QDict
*qdict_from_args_type(const char *args_type
)
3733 QString
*key
, *type
, *cur_qs
;
3735 assert(args_type
!= NULL
);
3737 qdict
= qdict_new();
3739 if (args_type
== NULL
|| args_type
[0] == '\0') {
3740 /* no args, empty qdict */
3744 key
= qstring_new();
3745 type
= qstring_new();
3750 switch (args_type
[i
]) {
3753 qdict_put(qdict
, qstring_get_str(key
), type
);
3755 if (args_type
[i
] == '\0') {
3758 type
= qstring_new(); /* qdict has ref */
3759 cur_qs
= key
= qstring_new();
3765 qstring_append_chr(cur_qs
, args_type
[i
]);
3775 * Client argument checking rules:
3777 * 1. Client must provide all mandatory arguments
3778 * 2. Each argument provided by the client must be expected
3779 * 3. Each argument provided by the client must have the type expected
3782 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3789 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3792 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3797 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3800 error_propagate(errp
, err
);
3805 * Input object checking rules
3807 * 1. Input object must be a dict
3808 * 2. The "execute" key must exist
3809 * 3. The "execute" key must be a string
3810 * 4. If the "arguments" key exists, it must be a dict
3811 * 5. If the "id" key exists, it can be anything (ie. json-value)
3812 * 6. Any argument not listed above is considered invalid
3814 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3816 const QDictEntry
*ent
;
3817 int has_exec_key
= 0;
3820 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3821 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3825 input_dict
= qobject_to_qdict(input_obj
);
3827 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3828 const char *arg_name
= qdict_entry_key(ent
);
3829 const QObject
*arg_obj
= qdict_entry_value(ent
);
3831 if (!strcmp(arg_name
, "execute")) {
3832 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3833 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3834 "execute", "string");
3838 } else if (!strcmp(arg_name
, "arguments")) {
3839 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3840 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3841 "arguments", "object");
3844 } else if (!strcmp(arg_name
, "id")) {
3845 /* Any string is acceptable as "id", so nothing to check */
3847 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3852 if (!has_exec_key
) {
3853 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3860 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3862 Error
*local_err
= NULL
;
3863 QObject
*obj
, *data
;
3864 QDict
*input
, *args
;
3865 const mon_cmd_t
*cmd
;
3866 const char *cmd_name
;
3867 Monitor
*mon
= cur_mon
;
3869 args
= input
= NULL
;
3872 obj
= json_parser_parse(tokens
, NULL
);
3874 // FIXME: should be triggered in json_parser_parse()
3875 error_setg(&local_err
, QERR_JSON_PARSING
);
3879 input
= qmp_check_input_obj(obj
, &local_err
);
3881 qobject_decref(obj
);
3885 mon
->qmp
.id
= qdict_get(input
, "id");
3886 qobject_incref(mon
->qmp
.id
);
3888 cmd_name
= qdict_get_str(input
, "execute");
3889 trace_handle_qmp_command(mon
, cmd_name
);
3890 cmd
= qmp_find_cmd(cmd_name
);
3892 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3893 "The command %s has not been found", cmd_name
);
3896 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
3900 obj
= qdict_get(input
, "arguments");
3904 args
= qobject_to_qdict(obj
);
3908 qmp_check_client_args(cmd
, args
, &local_err
);
3913 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
3916 monitor_protocol_emitter(mon
, data
, local_err
);
3917 qobject_decref(data
);
3918 error_free(local_err
);
3923 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3925 Monitor
*old_mon
= cur_mon
;
3929 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3934 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3936 Monitor
*old_mon
= cur_mon
;
3942 for (i
= 0; i
< size
; i
++)
3943 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3945 if (size
== 0 || buf
[size
- 1] != 0)
3946 monitor_printf(cur_mon
, "corrupted command\n");
3948 handle_hmp_command(cur_mon
, (char *)buf
);
3954 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3955 void *readline_opaque
)
3957 Monitor
*mon
= opaque
;
3959 monitor_suspend(mon
);
3960 handle_hmp_command(mon
, cmdline
);
3961 monitor_resume(mon
);
3964 int monitor_suspend(Monitor
*mon
)
3972 void monitor_resume(Monitor
*mon
)
3976 if (--mon
->suspend_cnt
== 0)
3977 readline_show_prompt(mon
->rs
);
3980 static QObject
*get_qmp_greeting(void)
3982 QObject
*ver
= NULL
;
3984 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3985 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
3988 static void monitor_qmp_event(void *opaque
, int event
)
3991 Monitor
*mon
= opaque
;
3994 case CHR_EVENT_OPENED
:
3995 mon
->qmp
.in_command_mode
= false;
3996 data
= get_qmp_greeting();
3997 monitor_json_emitter(mon
, data
);
3998 qobject_decref(data
);
4001 case CHR_EVENT_CLOSED
:
4002 json_message_parser_destroy(&mon
->qmp
.parser
);
4003 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4005 monitor_fdsets_cleanup();
4010 static void monitor_event(void *opaque
, int event
)
4012 Monitor
*mon
= opaque
;
4015 case CHR_EVENT_MUX_IN
:
4016 qemu_mutex_lock(&mon
->out_lock
);
4018 qemu_mutex_unlock(&mon
->out_lock
);
4019 if (mon
->reset_seen
) {
4020 readline_restart(mon
->rs
);
4021 monitor_resume(mon
);
4024 mon
->suspend_cnt
= 0;
4028 case CHR_EVENT_MUX_OUT
:
4029 if (mon
->reset_seen
) {
4030 if (mon
->suspend_cnt
== 0) {
4031 monitor_printf(mon
, "\n");
4034 monitor_suspend(mon
);
4038 qemu_mutex_lock(&mon
->out_lock
);
4040 qemu_mutex_unlock(&mon
->out_lock
);
4043 case CHR_EVENT_OPENED
:
4044 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4045 "information\n", QEMU_VERSION
);
4046 if (!mon
->mux_out
) {
4047 readline_restart(mon
->rs
);
4048 readline_show_prompt(mon
->rs
);
4050 mon
->reset_seen
= 1;
4054 case CHR_EVENT_CLOSED
:
4056 monitor_fdsets_cleanup();
4062 compare_mon_cmd(const void *a
, const void *b
)
4064 return strcmp(((const mon_cmd_t
*)a
)->name
,
4065 ((const mon_cmd_t
*)b
)->name
);
4068 static void sortcmdlist(void)
4071 int elem_size
= sizeof(mon_cmd_t
);
4073 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4074 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4076 array_num
= sizeof(info_cmds
)/elem_size
-1;
4077 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4089 /* These functions just adapt the readline interface in a typesafe way. We
4090 * could cast function pointers but that discards compiler checks.
4092 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4093 const char *fmt
, ...)
4097 monitor_vprintf(opaque
, fmt
, ap
);
4101 static void monitor_readline_flush(void *opaque
)
4103 monitor_flush(opaque
);
4106 static void __attribute__((constructor
)) monitor_lock_init(void)
4108 qemu_mutex_init(&monitor_lock
);
4111 void monitor_init(CharDriverState
*chr
, int flags
)
4113 static int is_first_init
= 1;
4116 if (is_first_init
) {
4117 monitor_qapi_event_init();
4122 mon
= g_malloc(sizeof(*mon
));
4123 monitor_data_init(mon
);
4127 if (flags
& MONITOR_USE_READLINE
) {
4128 mon
->rs
= readline_init(monitor_readline_printf
,
4129 monitor_readline_flush
,
4131 monitor_find_completion
);
4132 monitor_read_command(mon
, 0);
4135 if (monitor_is_qmp(mon
)) {
4136 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4137 monitor_qmp_event
, mon
);
4138 qemu_chr_fe_set_echo(chr
, true);
4139 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4141 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4142 monitor_event
, mon
);
4145 qemu_mutex_lock(&monitor_lock
);
4146 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4147 qemu_mutex_unlock(&monitor_lock
);
4150 static void bdrv_password_cb(void *opaque
, const char *password
,
4151 void *readline_opaque
)
4153 Monitor
*mon
= opaque
;
4154 BlockDriverState
*bs
= readline_opaque
;
4156 Error
*local_err
= NULL
;
4158 bdrv_add_key(bs
, password
, &local_err
);
4160 error_report_err(local_err
);
4163 if (mon
->password_completion_cb
)
4164 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4166 monitor_read_command(mon
, 1);
4169 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4170 BlockCompletionFunc
*completion_cb
,
4175 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4176 bdrv_get_encrypted_filename(bs
));
4178 mon
->password_completion_cb
= completion_cb
;
4179 mon
->password_opaque
= opaque
;
4181 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4183 if (err
&& completion_cb
)
4184 completion_cb(opaque
, err
);
4189 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4190 BlockCompletionFunc
*completion_cb
,
4196 blk
= blk_by_name(device
);
4198 monitor_printf(mon
, "Device not found %s\n", device
);
4202 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4206 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4209 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4212 if (completion_cb
) {
4213 completion_cb(opaque
, 0);
4218 QemuOptsList qemu_mon_opts
= {
4220 .implied_opt_name
= "chardev",
4221 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4225 .type
= QEMU_OPT_STRING
,
4228 .type
= QEMU_OPT_STRING
,
4231 .type
= QEMU_OPT_BOOL
,
4234 .type
= QEMU_OPT_BOOL
,
4236 { /* end of list */ }
4241 void qmp_rtc_reset_reinjection(Error
**errp
)
4243 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4247 #ifndef TARGET_S390X
4248 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4250 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");