4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu/osdep.h"
26 #include "qemu-common.h"
29 #include "monitor/qdev.h"
31 #include "hw/i386/pc.h"
32 #include "hw/pci/pci.h"
33 #include "sysemu/watchdog.h"
34 #include "hw/loader.h"
35 #include "exec/gdbstub.h"
37 #include "net/slirp.h"
38 #include "sysemu/char.h"
39 #include "ui/qemu-spice.h"
40 #include "sysemu/sysemu.h"
41 #include "sysemu/numa.h"
42 #include "monitor/monitor.h"
43 #include "qemu/readline.h"
44 #include "ui/console.h"
46 #include "sysemu/blockdev.h"
47 #include "sysemu/block-backend.h"
48 #include "audio/audio.h"
49 #include "disas/disas.h"
50 #include "sysemu/balloon.h"
51 #include "qemu/timer.h"
52 #include "migration/migration.h"
53 #include "sysemu/kvm.h"
55 #include "sysemu/tpm.h"
56 #include "qapi/qmp/qerror.h"
57 #include "qapi/qmp/types.h"
58 #include "qapi/qmp/qjson.h"
59 #include "qapi/qmp/json-streamer.h"
60 #include "qapi/qmp/json-parser.h"
61 #include "qom/object_interfaces.h"
64 #include "trace/control.h"
65 #include "monitor/hmp-target.h"
66 #ifdef CONFIG_TRACE_SIMPLE
67 #include "trace/simple.h"
69 #include "exec/memory.h"
70 #include "exec/exec-all.h"
72 #include "qmp-commands.h"
74 #include "qemu/thread.h"
75 #include "block/qapi.h"
76 #include "qapi/qmp-event.h"
77 #include "qapi-event.h"
78 #include "qmp-introspect.h"
79 #include "sysemu/block-backend.h"
80 #include "sysemu/qtest.h"
81 #include "qemu/cutils.h"
82 #include "qapi/qmp/dispatch.h"
84 /* for hmp_info_irq/pic */
85 #if defined(TARGET_SPARC)
86 #include "hw/sparc/sun4m.h"
88 #include "hw/lm32/lm32_pic.h"
90 #if defined(TARGET_S390X)
91 #include "hw/s390x/storage-keys.h"
98 * 'B' block device name
99 * 's' string (accept optional quote)
100 * 'S' it just appends the rest of the string (accept optional quote)
101 * 'O' option string of the form NAME=VALUE,...
102 * parsed according to QemuOptsList given by its name
103 * Example: 'device:O' uses qemu_device_opts.
104 * Restriction: only lists with empty desc are supported
105 * TODO lift the restriction
107 * 'l' target long (32 or 64 bit)
108 * 'M' Non-negative target long (32 or 64 bit), in user mode the
109 * value is multiplied by 2^20 (think Mebibyte)
110 * 'o' octets (aka bytes)
111 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
112 * K, k suffix, which multiplies the value by 2^60 for suffixes E
113 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
114 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
116 * user mode accepts an optional ms, us, ns suffix,
117 * which divides the value by 1e3, 1e6, 1e9, respectively
118 * '/' optional gdb-like print format (like "/10x")
120 * '?' optional type (for all types, except '/')
121 * '.' other form of optional type (for 'i' and 'l')
123 * user mode accepts "on" or "off"
124 * '-' optional parameter (eg. '-f')
128 typedef struct mon_cmd_t
{
130 const char *args_type
;
134 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
135 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
137 /* @sub_table is a list of 2nd level of commands. If it do not exist,
138 * mhandler should be used. If it exist, sub_table[?].mhandler should be
139 * used, and mhandler of 1st level plays the role of help function.
141 struct mon_cmd_t
*sub_table
;
142 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
145 /* file descriptors passed via SCM_RIGHTS */
146 typedef struct mon_fd_t mon_fd_t
;
150 QLIST_ENTRY(mon_fd_t
) next
;
153 /* file descriptor associated with a file descriptor set */
154 typedef struct MonFdsetFd MonFdsetFd
;
159 QLIST_ENTRY(MonFdsetFd
) next
;
162 /* file descriptor set containing fds passed via SCM_RIGHTS */
163 typedef struct MonFdset MonFdset
;
166 QLIST_HEAD(, MonFdsetFd
) fds
;
167 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
168 QLIST_ENTRY(MonFdset
) next
;
173 JSONMessageParser parser
;
175 * When a client connects, we're in capabilities negotiation mode.
176 * When command qmp_capabilities succeeds, we go into command
179 bool in_command_mode
; /* are we in command mode? */
183 * To prevent flooding clients, events can be throttled. The
184 * throttling is calculated globally, rather than per-Monitor
187 typedef struct MonitorQAPIEventState
{
188 QAPIEvent event
; /* Throttling state for this event type and... */
189 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
190 QEMUTimer
*timer
; /* Timer for handling delayed events */
191 QDict
*qdict
; /* Delayed event (if any) */
192 } MonitorQAPIEventState
;
195 int64_t rate
; /* Minimum time (in ns) between two events */
196 } MonitorQAPIEventConf
;
199 CharDriverState
*chr
;
209 /* Read under either BQL or out_lock, written with BQL+out_lock. */
215 BlockCompletionFunc
*password_completion_cb
;
216 void *password_opaque
;
217 mon_cmd_t
*cmd_table
;
218 QLIST_HEAD(,mon_fd_t
) fds
;
219 QLIST_ENTRY(Monitor
) entry
;
222 /* QMP checker flags */
223 #define QMP_ACCEPT_UNKNOWNS 1
225 /* Protects mon_list, monitor_event_state. */
226 static QemuMutex monitor_lock
;
228 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
229 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
230 static int mon_refcount
;
232 static mon_cmd_t mon_cmds
[];
233 static mon_cmd_t info_cmds
[];
235 static const mon_cmd_t qmp_cmds
[];
239 static QEMUClockType event_clock_type
= QEMU_CLOCK_REALTIME
;
241 static void monitor_command_cb(void *opaque
, const char *cmdline
,
242 void *readline_opaque
);
245 * Is @mon a QMP monitor?
247 static inline bool monitor_is_qmp(const Monitor
*mon
)
249 return (mon
->flags
& MONITOR_USE_CONTROL
);
253 * Is the current monitor, if any, a QMP monitor?
255 bool monitor_cur_is_qmp(void)
257 return cur_mon
&& monitor_is_qmp(cur_mon
);
260 void monitor_read_command(Monitor
*mon
, int show_prompt
)
265 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
267 readline_show_prompt(mon
->rs
);
270 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
274 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
275 /* prompt is printed on return from the command handler */
278 monitor_printf(mon
, "terminal does not support password prompting\n");
283 static void monitor_flush_locked(Monitor
*mon
);
285 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
288 Monitor
*mon
= opaque
;
290 qemu_mutex_lock(&mon
->out_lock
);
292 monitor_flush_locked(mon
);
293 qemu_mutex_unlock(&mon
->out_lock
);
297 /* Called with mon->out_lock held. */
298 static void monitor_flush_locked(Monitor
*mon
)
304 if (mon
->skip_flush
) {
308 buf
= qstring_get_str(mon
->outbuf
);
309 len
= qstring_get_length(mon
->outbuf
);
311 if (len
&& !mon
->mux_out
) {
312 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
313 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
314 /* all flushed or error */
315 QDECREF(mon
->outbuf
);
316 mon
->outbuf
= qstring_new();
321 QString
*tmp
= qstring_from_str(buf
+ rc
);
322 QDECREF(mon
->outbuf
);
325 if (mon
->out_watch
== 0) {
326 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
327 monitor_unblocked
, mon
);
332 void monitor_flush(Monitor
*mon
)
334 qemu_mutex_lock(&mon
->out_lock
);
335 monitor_flush_locked(mon
);
336 qemu_mutex_unlock(&mon
->out_lock
);
339 /* flush at every end of line */
340 static void monitor_puts(Monitor
*mon
, const char *str
)
344 qemu_mutex_lock(&mon
->out_lock
);
350 qstring_append_chr(mon
->outbuf
, '\r');
352 qstring_append_chr(mon
->outbuf
, c
);
354 monitor_flush_locked(mon
);
357 qemu_mutex_unlock(&mon
->out_lock
);
360 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
367 if (monitor_is_qmp(mon
)) {
371 buf
= g_strdup_vprintf(fmt
, ap
);
372 monitor_puts(mon
, buf
);
376 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
380 monitor_vprintf(mon
, fmt
, ap
);
384 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
388 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
393 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
397 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
398 qobject_to_json(data
);
399 assert(json
!= NULL
);
401 qstring_append_chr(json
, '\n');
402 monitor_puts(mon
, qstring_get_str(json
));
407 static QDict
*build_qmp_error_dict(Error
*err
)
411 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
412 QapiErrorClass_lookup
[error_get_class(err
)],
413 error_get_pretty(err
));
415 return qobject_to_qdict(obj
);
418 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
423 trace_monitor_protocol_emitter(mon
);
426 /* success response */
429 qobject_incref(data
);
430 qdict_put_obj(qmp
, "return", data
);
432 /* return an empty QDict by default */
433 qdict_put(qmp
, "return", qdict_new());
437 qmp
= build_qmp_error_dict(err
);
441 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
445 monitor_json_emitter(mon
, QOBJECT(qmp
));
450 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
451 /* Limit guest-triggerable events to 1 per second */
452 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
453 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
454 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
455 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
456 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
457 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
460 GHashTable
*monitor_qapi_event_state
;
463 * Emits the event to every monitor instance, @event is only used for trace
464 * Called with monitor_lock held.
466 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
470 trace_monitor_protocol_event_emit(event
, qdict
);
471 QLIST_FOREACH(mon
, &mon_list
, entry
) {
472 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
473 monitor_json_emitter(mon
, QOBJECT(qdict
));
478 static void monitor_qapi_event_handler(void *opaque
);
481 * Queue a new event for emission to Monitor instances,
482 * applying any rate limiting if required.
485 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
487 MonitorQAPIEventConf
*evconf
;
488 MonitorQAPIEventState
*evstate
;
490 assert(event
< QAPI_EVENT__MAX
);
491 evconf
= &monitor_qapi_event_conf
[event
];
492 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
494 qemu_mutex_lock(&monitor_lock
);
497 /* Unthrottled event */
498 monitor_qapi_event_emit(event
, qdict
);
500 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
501 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
503 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
504 assert(!evstate
|| timer_pending(evstate
->timer
));
508 * Timer is pending for (at least) evconf->rate ns after
509 * last send. Store event for sending when timer fires,
510 * replacing a prior stored event if any.
512 QDECREF(evstate
->qdict
);
513 evstate
->qdict
= qdict
;
514 QINCREF(evstate
->qdict
);
517 * Last send was (at least) evconf->rate ns ago.
518 * Send immediately, and arm the timer to call
519 * monitor_qapi_event_handler() in evconf->rate ns. Any
520 * events arriving before then will be delayed until then.
522 int64_t now
= qemu_clock_get_ns(event_clock_type
);
524 monitor_qapi_event_emit(event
, qdict
);
526 evstate
= g_new(MonitorQAPIEventState
, 1);
527 evstate
->event
= event
;
528 evstate
->data
= data
;
529 QINCREF(evstate
->data
);
530 evstate
->qdict
= NULL
;
531 evstate
->timer
= timer_new_ns(event_clock_type
,
532 monitor_qapi_event_handler
,
534 g_hash_table_add(monitor_qapi_event_state
, evstate
);
535 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
539 qemu_mutex_unlock(&monitor_lock
);
543 * This function runs evconf->rate ns after sending a throttled
545 * If another event has since been stored, send it.
547 static void monitor_qapi_event_handler(void *opaque
)
549 MonitorQAPIEventState
*evstate
= opaque
;
550 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
552 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
553 qemu_mutex_lock(&monitor_lock
);
555 if (evstate
->qdict
) {
556 int64_t now
= qemu_clock_get_ns(event_clock_type
);
558 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
559 QDECREF(evstate
->qdict
);
560 evstate
->qdict
= NULL
;
561 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
563 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
564 QDECREF(evstate
->data
);
565 timer_free(evstate
->timer
);
569 qemu_mutex_unlock(&monitor_lock
);
572 static unsigned int qapi_event_throttle_hash(const void *key
)
574 const MonitorQAPIEventState
*evstate
= key
;
575 unsigned int hash
= evstate
->event
* 255;
577 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
578 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
581 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
582 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
588 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
590 const MonitorQAPIEventState
*eva
= a
;
591 const MonitorQAPIEventState
*evb
= b
;
593 if (eva
->event
!= evb
->event
) {
597 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
598 return !strcmp(qdict_get_str(eva
->data
, "id"),
599 qdict_get_str(evb
->data
, "id"));
602 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
603 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
604 qdict_get_str(evb
->data
, "node-name"));
610 static void monitor_qapi_event_init(void)
612 if (qtest_enabled()) {
613 event_clock_type
= QEMU_CLOCK_VIRTUAL
;
616 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
617 qapi_event_throttle_equal
);
618 qmp_event_set_func_emit(monitor_qapi_event_queue
);
621 void qmp_qmp_capabilities(Error
**errp
)
623 cur_mon
->qmp
.in_command_mode
= true;
626 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
628 static void monitor_data_init(Monitor
*mon
)
630 memset(mon
, 0, sizeof(Monitor
));
631 qemu_mutex_init(&mon
->out_lock
);
632 mon
->outbuf
= qstring_new();
633 /* Use *mon_cmds by default. */
634 mon
->cmd_table
= mon_cmds
;
637 static void monitor_data_destroy(Monitor
*mon
)
640 qemu_chr_add_handlers(mon
->chr
, NULL
, NULL
, NULL
, NULL
);
642 if (monitor_is_qmp(mon
)) {
643 json_message_parser_destroy(&mon
->qmp
.parser
);
646 QDECREF(mon
->outbuf
);
647 qemu_mutex_destroy(&mon
->out_lock
);
650 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
651 int64_t cpu_index
, Error
**errp
)
654 Monitor
*old_mon
, hmp
;
656 monitor_data_init(&hmp
);
657 hmp
.skip_flush
= true;
663 int ret
= monitor_set_cpu(cpu_index
);
666 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
672 handle_hmp_command(&hmp
, command_line
);
675 qemu_mutex_lock(&hmp
.out_lock
);
676 if (qstring_get_length(hmp
.outbuf
) > 0) {
677 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
679 output
= g_strdup("");
681 qemu_mutex_unlock(&hmp
.out_lock
);
684 monitor_data_destroy(&hmp
);
688 static int compare_cmd(const char *name
, const char *list
)
690 const char *p
, *pstart
;
698 p
= pstart
+ strlen(pstart
);
699 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
708 static int get_str(char *buf
, int buf_size
, const char **pp
)
716 while (qemu_isspace(*p
)) {
727 while (*p
!= '\0' && *p
!= '\"') {
743 printf("unsupported escape code: '\\%c'\n", c
);
746 if ((q
- buf
) < buf_size
- 1) {
750 if ((q
- buf
) < buf_size
- 1) {
757 printf("unterminated string\n");
762 while (*p
!= '\0' && !qemu_isspace(*p
)) {
763 if ((q
- buf
) < buf_size
- 1) {
776 static void free_cmdline_args(char **args
, int nb_args
)
780 assert(nb_args
<= MAX_ARGS
);
782 for (i
= 0; i
< nb_args
; i
++) {
789 * Parse the command line to get valid args.
790 * @cmdline: command line to be parsed.
791 * @pnb_args: location to store the number of args, must NOT be NULL.
792 * @args: location to store the args, which should be freed by caller, must
795 * Returns 0 on success, negative on failure.
797 * NOTE: this parser is an approximate form of the real command parser. Number
798 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
799 * return with failure.
801 static int parse_cmdline(const char *cmdline
,
802 int *pnb_args
, char **args
)
811 while (qemu_isspace(*p
)) {
817 if (nb_args
>= MAX_ARGS
) {
820 ret
= get_str(buf
, sizeof(buf
), &p
);
824 args
[nb_args
] = g_strdup(buf
);
831 free_cmdline_args(args
, nb_args
);
835 static void help_cmd_dump_one(Monitor
*mon
,
836 const mon_cmd_t
*cmd
,
842 for (i
= 0; i
< prefix_args_nb
; i
++) {
843 monitor_printf(mon
, "%s ", prefix_args
[i
]);
845 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
848 /* @args[@arg_index] is the valid command need to find in @cmds */
849 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
850 char **args
, int nb_args
, int arg_index
)
852 const mon_cmd_t
*cmd
;
854 /* No valid arg need to compare with, dump all in *cmds */
855 if (arg_index
>= nb_args
) {
856 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
857 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
862 /* Find one entry to dump */
863 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
864 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
865 if (cmd
->sub_table
) {
866 /* continue with next arg */
867 help_cmd_dump(mon
, cmd
->sub_table
,
868 args
, nb_args
, arg_index
+ 1);
870 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
877 static void help_cmd(Monitor
*mon
, const char *name
)
879 char *args
[MAX_ARGS
];
882 /* 1. parse user input */
884 /* special case for log, directly dump and return */
885 if (!strcmp(name
, "log")) {
886 const QEMULogItem
*item
;
887 monitor_printf(mon
, "Log items (comma separated):\n");
888 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
889 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
890 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
895 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
900 /* 2. dump the contents according to parsed args */
901 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
903 free_cmdline_args(args
, nb_args
);
906 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
908 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
911 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
913 const char *tp_name
= qdict_get_str(qdict
, "name");
914 bool new_state
= qdict_get_bool(qdict
, "option");
915 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
916 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
917 Error
*local_err
= NULL
;
920 monitor_printf(mon
, "argument vcpu must be positive");
924 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
926 error_report_err(local_err
);
930 #ifdef CONFIG_TRACE_SIMPLE
931 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
933 const char *op
= qdict_get_try_str(qdict
, "op");
934 const char *arg
= qdict_get_try_str(qdict
, "arg");
937 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
938 } else if (!strcmp(op
, "on")) {
939 st_set_trace_file_enabled(true);
940 } else if (!strcmp(op
, "off")) {
941 st_set_trace_file_enabled(false);
942 } else if (!strcmp(op
, "flush")) {
943 st_flush_trace_buffer();
944 } else if (!strcmp(op
, "set")) {
946 st_set_trace_file(arg
);
949 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
950 help_cmd(mon
, "trace-file");
955 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
957 help_cmd(mon
, "info");
960 CommandInfoList
*qmp_query_commands(Error
**errp
)
962 CommandInfoList
*info
, *cmd_list
= NULL
;
963 const mon_cmd_t
*cmd
;
965 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
966 info
= g_malloc0(sizeof(*info
));
967 info
->value
= g_malloc0(sizeof(*info
->value
));
968 info
->value
->name
= g_strdup(cmd
->name
);
970 info
->next
= cmd_list
;
977 EventInfoList
*qmp_query_events(Error
**errp
)
979 EventInfoList
*info
, *ev_list
= NULL
;
982 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
983 const char *event_name
= QAPIEvent_lookup
[e
];
984 assert(event_name
!= NULL
);
985 info
= g_malloc0(sizeof(*info
));
986 info
->value
= g_malloc0(sizeof(*info
->value
));
987 info
->value
->name
= g_strdup(event_name
);
989 info
->next
= ev_list
;
997 * Minor hack: generated marshalling suppressed for this command
998 * ('gen': false in the schema) so we can parse the JSON string
999 * directly into QObject instead of first parsing it with
1000 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1001 * to QObject with generated output marshallers, every time. Instead,
1002 * we do it in test-qmp-input-visitor.c, just to make sure
1003 * qapi-introspect.py's output actually conforms to the schema.
1005 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
1008 *ret_data
= qobject_from_json(qmp_schema_json
);
1012 * Note: right now, this function is never called. It will be called
1013 * shortly when we stop using QAPI 'middle mode'. The rest of this
1014 * comment is written as if that was the case already.
1016 * We used to define commands in qmp-commands.hx in addition to the
1017 * QAPI schema. This permitted defining some of them only in certain
1018 * configurations. query-commands has always reflected that (good,
1019 * because it lets QMP clients figure out what's actually available),
1020 * while query-qmp-schema never did (not so good). This function is a
1021 * hack to keep the configuration-specific commands defined exactly as
1022 * before, even though qmp-commands.hx is gone.
1024 * FIXME Educate the QAPI schema on configuration-specific commands,
1025 * and drop this hack.
1027 static void qmp_unregister_commands_hack(void)
1029 #ifndef CONFIG_SPICE
1030 qmp_unregister_command("query-spice");
1033 qmp_unregister_command("rtc-reset-reinjection");
1035 #ifndef TARGET_S390X
1036 qmp_unregister_command("dump-skeys");
1039 qmp_unregister_command("query-gic-capabilities");
1043 static void qmp_init_marshal(void)
1045 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema
,
1047 qmp_register_command("device_add", qmp_device_add
,
1049 qmp_register_command("netdev_add", qmp_netdev_add
,
1052 /* call it after the rest of qapi_init() */
1053 register_module_init(qmp_unregister_commands_hack
, MODULE_INIT_QAPI
);
1056 qapi_init(qmp_init_marshal
);
1058 /* set the current CPU defined by the user */
1059 int monitor_set_cpu(int cpu_index
)
1063 cpu
= qemu_get_cpu(cpu_index
);
1067 cur_mon
->mon_cpu
= cpu
;
1071 CPUState
*mon_get_cpu(void)
1073 if (!cur_mon
->mon_cpu
) {
1076 cpu_synchronize_state(cur_mon
->mon_cpu
);
1077 return cur_mon
->mon_cpu
;
1080 CPUArchState
*mon_get_cpu_env(void)
1082 return mon_get_cpu()->env_ptr
;
1085 int monitor_get_cpu_index(void)
1087 return mon_get_cpu()->cpu_index
;
1090 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1092 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1095 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1097 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1098 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1101 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1103 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1106 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1115 str
= readline_get_history(mon
->rs
, i
);
1118 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1123 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1125 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1128 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1130 const char *name
= qdict_get_try_str(qdict
, "name");
1131 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1132 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1133 TraceEventInfoList
*events
;
1134 TraceEventInfoList
*elem
;
1135 Error
*local_err
= NULL
;
1141 monitor_printf(mon
, "argument vcpu must be positive");
1145 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1147 error_report_err(local_err
);
1151 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1152 monitor_printf(mon
, "%s : state %u\n",
1154 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1156 qapi_free_TraceEventInfoList(events
);
1159 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1160 bool has_port
, int64_t port
,
1161 bool has_tls_port
, int64_t tls_port
,
1162 bool has_cert_subject
, const char *cert_subject
,
1165 if (strcmp(protocol
, "spice") == 0) {
1166 if (!qemu_using_spice(errp
)) {
1170 if (!has_port
&& !has_tls_port
) {
1171 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1175 if (qemu_spice_migrate_info(hostname
,
1176 has_port
? port
: -1,
1177 has_tls_port
? tls_port
: -1,
1179 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1185 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1188 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1192 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1194 error_report_err(err
);
1198 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1201 const char *items
= qdict_get_str(qdict
, "items");
1203 if (!strcmp(items
, "none")) {
1206 mask
= qemu_str_to_log_mask(items
);
1208 help_cmd(mon
, "log");
1215 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1217 const char *option
= qdict_get_try_str(qdict
, "option");
1218 if (!option
|| !strcmp(option
, "on")) {
1220 } else if (!strcmp(option
, "off")) {
1223 monitor_printf(mon
, "unexpected option %s\n", option
);
1227 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1229 const char *device
= qdict_get_try_str(qdict
, "device");
1231 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1232 if (gdbserver_start(device
) < 0) {
1233 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1235 } else if (strcmp(device
, "none") == 0) {
1236 monitor_printf(mon
, "Disabled gdbserver\n");
1238 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1243 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1245 const char *action
= qdict_get_str(qdict
, "action");
1246 if (select_watchdog_action(action
) == -1) {
1247 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1251 static void monitor_printc(Monitor
*mon
, int c
)
1253 monitor_printf(mon
, "'");
1256 monitor_printf(mon
, "\\'");
1259 monitor_printf(mon
, "\\\\");
1262 monitor_printf(mon
, "\\n");
1265 monitor_printf(mon
, "\\r");
1268 if (c
>= 32 && c
<= 126) {
1269 monitor_printf(mon
, "%c", c
);
1271 monitor_printf(mon
, "\\x%02x", c
);
1275 monitor_printf(mon
, "'");
1278 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1279 hwaddr addr
, int is_physical
)
1281 int l
, line_size
, i
, max_digits
, len
;
1285 if (format
== 'i') {
1288 CPUArchState
*env
= mon_get_cpu_env();
1291 } else if (wsize
== 4) {
1294 /* as default we use the current CS size */
1297 #ifdef TARGET_X86_64
1298 if ((env
->efer
& MSR_EFER_LMA
) &&
1299 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1303 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1309 CPUArchState
*env
= mon_get_cpu_env();
1310 flags
= msr_le
<< 16;
1311 flags
|= env
->bfd_mach
;
1313 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1317 len
= wsize
* count
;
1326 max_digits
= (wsize
* 8 + 2) / 3;
1330 max_digits
= (wsize
* 8) / 4;
1334 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1343 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1345 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1350 cpu_physical_memory_read(addr
, buf
, l
);
1352 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1353 monitor_printf(mon
, " Cannot access memory\n");
1362 v
= ldub_p(buf
+ i
);
1365 v
= lduw_p(buf
+ i
);
1368 v
= (uint32_t)ldl_p(buf
+ i
);
1374 monitor_printf(mon
, " ");
1377 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1380 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1383 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1386 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1389 monitor_printc(mon
, v
);
1394 monitor_printf(mon
, "\n");
1400 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1402 int count
= qdict_get_int(qdict
, "count");
1403 int format
= qdict_get_int(qdict
, "format");
1404 int size
= qdict_get_int(qdict
, "size");
1405 target_long addr
= qdict_get_int(qdict
, "addr");
1407 memory_dump(mon
, count
, format
, size
, addr
, 0);
1410 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1412 int count
= qdict_get_int(qdict
, "count");
1413 int format
= qdict_get_int(qdict
, "format");
1414 int size
= qdict_get_int(qdict
, "size");
1415 hwaddr addr
= qdict_get_int(qdict
, "addr");
1417 memory_dump(mon
, count
, format
, size
, addr
, 1);
1420 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1422 int format
= qdict_get_int(qdict
, "format");
1423 hwaddr val
= qdict_get_int(qdict
, "val");
1427 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1430 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1433 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1437 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1440 monitor_printc(mon
, val
);
1443 monitor_printf(mon
, "\n");
1446 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1450 uint32_t start
= qdict_get_int(qdict
, "start");
1451 uint32_t size
= qdict_get_int(qdict
, "size");
1454 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1455 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1456 MEMTXATTRS_UNSPECIFIED
, NULL
);
1457 /* BSD sum algorithm ('sum' Unix command) */
1458 sum
= (sum
>> 1) | (sum
<< 15);
1461 monitor_printf(mon
, "%05d\n", sum
);
1464 static int mouse_button_state
;
1466 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1468 int dx
, dy
, dz
, button
;
1469 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1470 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1471 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1473 dx
= strtol(dx_str
, NULL
, 0);
1474 dy
= strtol(dy_str
, NULL
, 0);
1475 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1476 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1479 dz
= strtol(dz_str
, NULL
, 0);
1481 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1482 qemu_input_queue_btn(NULL
, button
, true);
1483 qemu_input_event_sync();
1484 qemu_input_queue_btn(NULL
, button
, false);
1487 qemu_input_event_sync();
1490 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1492 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1493 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1494 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1495 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1497 int button_state
= qdict_get_int(qdict
, "button_state");
1499 if (mouse_button_state
== button_state
) {
1502 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1503 qemu_input_event_sync();
1504 mouse_button_state
= button_state
;
1507 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1509 int size
= qdict_get_int(qdict
, "size");
1510 int addr
= qdict_get_int(qdict
, "addr");
1511 int has_index
= qdict_haskey(qdict
, "index");
1516 int index
= qdict_get_int(qdict
, "index");
1517 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1525 val
= cpu_inb(addr
);
1529 val
= cpu_inw(addr
);
1533 val
= cpu_inl(addr
);
1537 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1538 suffix
, addr
, size
* 2, val
);
1541 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1543 int size
= qdict_get_int(qdict
, "size");
1544 int addr
= qdict_get_int(qdict
, "addr");
1545 int val
= qdict_get_int(qdict
, "val");
1547 addr
&= IOPORTS_MASK
;
1552 cpu_outb(addr
, val
);
1555 cpu_outw(addr
, val
);
1558 cpu_outl(addr
, val
);
1563 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1565 Error
*local_err
= NULL
;
1566 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1568 qemu_boot_set(bootdevice
, &local_err
);
1570 error_report_err(local_err
);
1572 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1576 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1578 mtree_info((fprintf_function
)monitor_printf
, mon
);
1581 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1587 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1588 query_numa_node_mem(node_mem
);
1589 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1590 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1591 monitor_printf(mon
, "node %d cpus:", i
);
1593 if (cpu
->numa_node
== i
) {
1594 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1597 monitor_printf(mon
, "\n");
1598 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1604 #ifdef CONFIG_PROFILER
1609 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1611 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1612 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1613 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1614 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1619 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1621 monitor_printf(mon
, "Internal profiler not compiled\n");
1625 /* Capture support */
1626 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1628 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1633 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1634 monitor_printf(mon
, "[%d]: ", i
);
1635 s
->ops
.info (s
->opaque
);
1639 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1642 int n
= qdict_get_int(qdict
, "n");
1645 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1647 s
->ops
.destroy (s
->opaque
);
1648 QLIST_REMOVE (s
, entries
);
1655 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1657 const char *path
= qdict_get_str(qdict
, "path");
1658 int has_freq
= qdict_haskey(qdict
, "freq");
1659 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1660 int has_bits
= qdict_haskey(qdict
, "bits");
1661 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1662 int has_channels
= qdict_haskey(qdict
, "nchannels");
1663 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1666 s
= g_malloc0 (sizeof (*s
));
1668 freq
= has_freq
? freq
: 44100;
1669 bits
= has_bits
? bits
: 16;
1670 nchannels
= has_channels
? nchannels
: 2;
1672 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1673 monitor_printf(mon
, "Failed to add wave capture\n");
1677 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1680 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1682 qemu_acl
*acl
= qemu_acl_find(name
);
1685 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1690 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1692 const char *aclname
= qdict_get_str(qdict
, "aclname");
1693 qemu_acl
*acl
= find_acl(mon
, aclname
);
1694 qemu_acl_entry
*entry
;
1698 monitor_printf(mon
, "policy: %s\n",
1699 acl
->defaultDeny
? "deny" : "allow");
1700 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1702 monitor_printf(mon
, "%d: %s %s\n", i
,
1703 entry
->deny
? "deny" : "allow", entry
->match
);
1708 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1710 const char *aclname
= qdict_get_str(qdict
, "aclname");
1711 qemu_acl
*acl
= find_acl(mon
, aclname
);
1714 qemu_acl_reset(acl
);
1715 monitor_printf(mon
, "acl: removed all rules\n");
1719 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1721 const char *aclname
= qdict_get_str(qdict
, "aclname");
1722 const char *policy
= qdict_get_str(qdict
, "policy");
1723 qemu_acl
*acl
= find_acl(mon
, aclname
);
1726 if (strcmp(policy
, "allow") == 0) {
1727 acl
->defaultDeny
= 0;
1728 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1729 } else if (strcmp(policy
, "deny") == 0) {
1730 acl
->defaultDeny
= 1;
1731 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1733 monitor_printf(mon
, "acl: unknown policy '%s', "
1734 "expected 'deny' or 'allow'\n", policy
);
1739 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1741 const char *aclname
= qdict_get_str(qdict
, "aclname");
1742 const char *match
= qdict_get_str(qdict
, "match");
1743 const char *policy
= qdict_get_str(qdict
, "policy");
1744 int has_index
= qdict_haskey(qdict
, "index");
1745 int index
= qdict_get_try_int(qdict
, "index", -1);
1746 qemu_acl
*acl
= find_acl(mon
, aclname
);
1750 if (strcmp(policy
, "allow") == 0) {
1752 } else if (strcmp(policy
, "deny") == 0) {
1755 monitor_printf(mon
, "acl: unknown policy '%s', "
1756 "expected 'deny' or 'allow'\n", policy
);
1760 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1762 ret
= qemu_acl_append(acl
, deny
, match
);
1764 monitor_printf(mon
, "acl: unable to add acl entry\n");
1766 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1770 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1772 const char *aclname
= qdict_get_str(qdict
, "aclname");
1773 const char *match
= qdict_get_str(qdict
, "match");
1774 qemu_acl
*acl
= find_acl(mon
, aclname
);
1778 ret
= qemu_acl_remove(acl
, match
);
1780 monitor_printf(mon
, "acl: no matching acl entry\n");
1782 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1786 void qmp_getfd(const char *fdname
, Error
**errp
)
1791 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1793 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1797 if (qemu_isdigit(fdname
[0])) {
1799 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1800 "a name not starting with a digit");
1804 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1805 if (strcmp(monfd
->name
, fdname
) != 0) {
1814 monfd
= g_malloc0(sizeof(mon_fd_t
));
1815 monfd
->name
= g_strdup(fdname
);
1818 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1821 void qmp_closefd(const char *fdname
, Error
**errp
)
1825 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1826 if (strcmp(monfd
->name
, fdname
) != 0) {
1830 QLIST_REMOVE(monfd
, next
);
1832 g_free(monfd
->name
);
1837 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1840 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1842 int saved_vm_running
= runstate_is_running();
1843 const char *name
= qdict_get_str(qdict
, "name");
1845 vm_stop(RUN_STATE_RESTORE_VM
);
1847 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1852 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1856 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1859 if (strcmp(monfd
->name
, fdname
) != 0) {
1865 /* caller takes ownership of fd */
1866 QLIST_REMOVE(monfd
, next
);
1867 g_free(monfd
->name
);
1873 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1877 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1879 MonFdsetFd
*mon_fdset_fd
;
1880 MonFdsetFd
*mon_fdset_fd_next
;
1882 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1883 if ((mon_fdset_fd
->removed
||
1884 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1885 runstate_is_running()) {
1886 close(mon_fdset_fd
->fd
);
1887 g_free(mon_fdset_fd
->opaque
);
1888 QLIST_REMOVE(mon_fdset_fd
, next
);
1889 g_free(mon_fdset_fd
);
1893 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1894 QLIST_REMOVE(mon_fdset
, next
);
1899 static void monitor_fdsets_cleanup(void)
1901 MonFdset
*mon_fdset
;
1902 MonFdset
*mon_fdset_next
;
1904 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1905 monitor_fdset_cleanup(mon_fdset
);
1909 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1910 const char *opaque
, Error
**errp
)
1913 Monitor
*mon
= cur_mon
;
1916 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1918 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1922 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1923 has_opaque
, opaque
, errp
);
1935 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1937 MonFdset
*mon_fdset
;
1938 MonFdsetFd
*mon_fdset_fd
;
1941 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1942 if (mon_fdset
->id
!= fdset_id
) {
1945 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1947 if (mon_fdset_fd
->fd
!= fd
) {
1950 mon_fdset_fd
->removed
= true;
1953 mon_fdset_fd
->removed
= true;
1956 if (has_fd
&& !mon_fdset_fd
) {
1959 monitor_fdset_cleanup(mon_fdset
);
1965 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1968 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1970 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1973 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1975 MonFdset
*mon_fdset
;
1976 MonFdsetFd
*mon_fdset_fd
;
1977 FdsetInfoList
*fdset_list
= NULL
;
1979 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1980 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1981 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1983 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1984 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1986 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1987 FdsetFdInfoList
*fdsetfd_info
;
1989 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1990 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1991 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1992 if (mon_fdset_fd
->opaque
) {
1993 fdsetfd_info
->value
->has_opaque
= true;
1994 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1996 fdsetfd_info
->value
->has_opaque
= false;
1999 fdsetfd_info
->next
= fdsetfd_list
;
2000 fdsetfd_list
= fdsetfd_info
;
2003 fdset_info
->value
->fds
= fdsetfd_list
;
2005 fdset_info
->next
= fdset_list
;
2006 fdset_list
= fdset_info
;
2012 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2013 bool has_opaque
, const char *opaque
,
2016 MonFdset
*mon_fdset
= NULL
;
2017 MonFdsetFd
*mon_fdset_fd
;
2021 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2022 /* Break if match found or match impossible due to ordering by ID */
2023 if (fdset_id
<= mon_fdset
->id
) {
2024 if (fdset_id
< mon_fdset
->id
) {
2032 if (mon_fdset
== NULL
) {
2033 int64_t fdset_id_prev
= -1;
2034 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2038 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2039 "a non-negative value");
2042 /* Use specified fdset ID */
2043 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2044 mon_fdset_cur
= mon_fdset
;
2045 if (fdset_id
< mon_fdset_cur
->id
) {
2050 /* Use first available fdset ID */
2051 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2052 mon_fdset_cur
= mon_fdset
;
2053 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2054 fdset_id_prev
= mon_fdset_cur
->id
;
2061 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2063 mon_fdset
->id
= fdset_id
;
2065 mon_fdset
->id
= fdset_id_prev
+ 1;
2068 /* The fdset list is ordered by fdset ID */
2069 if (!mon_fdset_cur
) {
2070 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2071 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2072 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2074 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2078 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2079 mon_fdset_fd
->fd
= fd
;
2080 mon_fdset_fd
->removed
= false;
2082 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2084 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2086 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2087 fdinfo
->fdset_id
= mon_fdset
->id
;
2088 fdinfo
->fd
= mon_fdset_fd
->fd
;
2093 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2096 MonFdset
*mon_fdset
;
2097 MonFdsetFd
*mon_fdset_fd
;
2100 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2101 if (mon_fdset
->id
!= fdset_id
) {
2104 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2105 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2106 if (mon_fd_flags
== -1) {
2110 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2111 return mon_fdset_fd
->fd
;
2123 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2125 MonFdset
*mon_fdset
;
2126 MonFdsetFd
*mon_fdset_fd_dup
;
2128 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2129 if (mon_fdset
->id
!= fdset_id
) {
2132 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2133 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2137 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2138 mon_fdset_fd_dup
->fd
= dup_fd
;
2139 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2145 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2147 MonFdset
*mon_fdset
;
2148 MonFdsetFd
*mon_fdset_fd_dup
;
2150 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2151 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2152 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2154 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2155 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2156 monitor_fdset_cleanup(mon_fdset
);
2160 return mon_fdset
->id
;
2168 int monitor_fdset_dup_fd_find(int dup_fd
)
2170 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2173 void monitor_fdset_dup_fd_remove(int dup_fd
)
2175 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2178 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2181 Error
*local_err
= NULL
;
2183 if (!qemu_isdigit(fdname
[0]) && mon
) {
2184 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2186 fd
= qemu_parse_fd(fdname
);
2188 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2193 error_propagate(errp
, local_err
);
2202 /* Please update hmp-commands.hx when adding or changing commands */
2203 static mon_cmd_t info_cmds
[] = {
2204 #include "hmp-commands-info.h"
2208 /* mon_cmds and info_cmds would be sorted at runtime */
2209 static mon_cmd_t mon_cmds
[] = {
2210 #include "hmp-commands.h"
2214 static const mon_cmd_t qmp_cmds
[] = {
2215 #include "qmp-commands-old.h"
2219 /*******************************************************************/
2221 static const char *pch
;
2222 static sigjmp_buf expr_env
;
2225 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2226 expr_error(Monitor
*mon
, const char *fmt
, ...)
2230 monitor_vprintf(mon
, fmt
, ap
);
2231 monitor_printf(mon
, "\n");
2233 siglongjmp(expr_env
, 1);
2236 /* return 0 if OK, -1 if not found */
2237 static int get_monitor_def(target_long
*pval
, const char *name
)
2239 const MonitorDef
*md
= target_monitor_defs();
2248 for(; md
->name
!= NULL
; md
++) {
2249 if (compare_cmd(name
, md
->name
)) {
2250 if (md
->get_value
) {
2251 *pval
= md
->get_value(md
, md
->offset
);
2253 CPUArchState
*env
= mon_get_cpu_env();
2254 ptr
= (uint8_t *)env
+ md
->offset
;
2257 *pval
= *(int32_t *)ptr
;
2260 *pval
= *(target_long
*)ptr
;
2271 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2273 *pval
= (target_long
) tmp
;
2279 static void next(void)
2283 while (qemu_isspace(*pch
))
2288 static int64_t expr_sum(Monitor
*mon
);
2290 static int64_t expr_unary(Monitor
*mon
)
2299 n
= expr_unary(mon
);
2303 n
= -expr_unary(mon
);
2307 n
= ~expr_unary(mon
);
2313 expr_error(mon
, "')' expected");
2320 expr_error(mon
, "character constant expected");
2324 expr_error(mon
, "missing terminating \' character");
2334 while ((*pch
>= 'a' && *pch
<= 'z') ||
2335 (*pch
>= 'A' && *pch
<= 'Z') ||
2336 (*pch
>= '0' && *pch
<= '9') ||
2337 *pch
== '_' || *pch
== '.') {
2338 if ((q
- buf
) < sizeof(buf
) - 1)
2342 while (qemu_isspace(*pch
))
2345 ret
= get_monitor_def(®
, buf
);
2347 expr_error(mon
, "unknown register");
2352 expr_error(mon
, "unexpected end of expression");
2357 n
= strtoull(pch
, &p
, 0);
2358 if (errno
== ERANGE
) {
2359 expr_error(mon
, "number too large");
2362 expr_error(mon
, "invalid char '%c' in expression", *p
);
2365 while (qemu_isspace(*pch
))
2373 static int64_t expr_prod(Monitor
*mon
)
2378 val
= expr_unary(mon
);
2381 if (op
!= '*' && op
!= '/' && op
!= '%')
2384 val2
= expr_unary(mon
);
2393 expr_error(mon
, "division by zero");
2404 static int64_t expr_logic(Monitor
*mon
)
2409 val
= expr_prod(mon
);
2412 if (op
!= '&' && op
!= '|' && op
!= '^')
2415 val2
= expr_prod(mon
);
2432 static int64_t expr_sum(Monitor
*mon
)
2437 val
= expr_logic(mon
);
2440 if (op
!= '+' && op
!= '-')
2443 val2
= expr_logic(mon
);
2452 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2455 if (sigsetjmp(expr_env
, 0)) {
2459 while (qemu_isspace(*pch
))
2461 *pval
= expr_sum(mon
);
2466 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2468 const char *p
= *pp
;
2472 d
= strtod(p
, &tailp
);
2474 monitor_printf(mon
, "Number expected\n");
2477 if (d
!= d
|| d
- d
!= 0) {
2478 /* NaN or infinity */
2479 monitor_printf(mon
, "Bad number\n");
2488 * Store the command-name in cmdname, and return a pointer to
2489 * the remaining of the command string.
2491 static const char *get_command_name(const char *cmdline
,
2492 char *cmdname
, size_t nlen
)
2495 const char *p
, *pstart
;
2498 while (qemu_isspace(*p
))
2503 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2508 memcpy(cmdname
, pstart
, len
);
2509 cmdname
[len
] = '\0';
2514 * Read key of 'type' into 'key' and return the current
2517 static char *key_get_info(const char *type
, char **key
)
2525 p
= strchr(type
, ':');
2532 str
= g_malloc(len
+ 1);
2533 memcpy(str
, type
, len
);
2540 static int default_fmt_format
= 'x';
2541 static int default_fmt_size
= 4;
2543 static int is_valid_option(const char *c
, const char *typestr
)
2551 typestr
= strstr(typestr
, option
);
2552 return (typestr
!= NULL
);
2555 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2556 const char *cmdname
)
2558 const mon_cmd_t
*cmd
;
2560 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2561 if (compare_cmd(cmdname
, cmd
->name
)) {
2569 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2571 return search_dispatch_table(qmp_cmds
, cmdname
);
2575 * Parse command name from @cmdp according to command table @table.
2576 * If blank, return NULL.
2577 * Else, if no valid command can be found, report to @mon, and return
2579 * Else, change @cmdp to point right behind the name, and return its
2580 * command table entry.
2581 * Do not assume the return value points into @table! It doesn't when
2582 * the command is found in a sub-command table.
2584 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2589 const mon_cmd_t
*cmd
;
2592 /* extract the command name */
2593 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2597 cmd
= search_dispatch_table(table
, cmdname
);
2599 monitor_printf(mon
, "unknown command: '%.*s'\n",
2600 (int)(p
- *cmdp
), *cmdp
);
2604 /* filter out following useless space */
2605 while (qemu_isspace(*p
)) {
2610 /* search sub command */
2611 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2612 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2619 * Parse arguments for @cmd.
2620 * If it can't be parsed, report to @mon, and return NULL.
2621 * Else, insert command arguments into a QDict, and return it.
2622 * Note: On success, caller has to free the QDict structure.
2625 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2627 const mon_cmd_t
*cmd
)
2629 const char *typestr
;
2632 const char *p
= *endp
;
2634 QDict
*qdict
= qdict_new();
2636 /* parse the parameters */
2637 typestr
= cmd
->args_type
;
2639 typestr
= key_get_info(typestr
, &key
);
2651 while (qemu_isspace(*p
))
2653 if (*typestr
== '?') {
2656 /* no optional string: NULL argument */
2660 ret
= get_str(buf
, sizeof(buf
), &p
);
2664 monitor_printf(mon
, "%s: filename expected\n",
2668 monitor_printf(mon
, "%s: block device name expected\n",
2672 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2677 qdict_put(qdict
, key
, qstring_from_str(buf
));
2682 QemuOptsList
*opts_list
;
2685 opts_list
= qemu_find_opts(key
);
2686 if (!opts_list
|| opts_list
->desc
->name
) {
2689 while (qemu_isspace(*p
)) {
2694 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2697 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2701 qemu_opts_to_qdict(opts
, qdict
);
2702 qemu_opts_del(opts
);
2707 int count
, format
, size
;
2709 while (qemu_isspace(*p
))
2715 if (qemu_isdigit(*p
)) {
2717 while (qemu_isdigit(*p
)) {
2718 count
= count
* 10 + (*p
- '0');
2756 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2757 monitor_printf(mon
, "invalid char in format: '%c'\n",
2762 format
= default_fmt_format
;
2763 if (format
!= 'i') {
2764 /* for 'i', not specifying a size gives -1 as size */
2766 size
= default_fmt_size
;
2767 default_fmt_size
= size
;
2769 default_fmt_format
= format
;
2772 format
= default_fmt_format
;
2773 if (format
!= 'i') {
2774 size
= default_fmt_size
;
2779 qdict_put(qdict
, "count", qint_from_int(count
));
2780 qdict_put(qdict
, "format", qint_from_int(format
));
2781 qdict_put(qdict
, "size", qint_from_int(size
));
2790 while (qemu_isspace(*p
))
2792 if (*typestr
== '?' || *typestr
== '.') {
2793 if (*typestr
== '?') {
2801 while (qemu_isspace(*p
))
2810 if (get_expr(mon
, &val
, &p
))
2812 /* Check if 'i' is greater than 32-bit */
2813 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2814 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2815 monitor_printf(mon
, "integer is for 32-bit values\n");
2817 } else if (c
== 'M') {
2819 monitor_printf(mon
, "enter a positive value\n");
2824 qdict_put(qdict
, key
, qint_from_int(val
));
2832 while (qemu_isspace(*p
)) {
2835 if (*typestr
== '?') {
2841 val
= qemu_strtosz(p
, &end
);
2843 monitor_printf(mon
, "invalid size\n");
2846 qdict_put(qdict
, key
, qint_from_int(val
));
2854 while (qemu_isspace(*p
))
2856 if (*typestr
== '?') {
2862 if (get_double(mon
, &val
, &p
) < 0) {
2865 if (p
[0] && p
[1] == 's') {
2868 val
/= 1e3
; p
+= 2; break;
2870 val
/= 1e6
; p
+= 2; break;
2872 val
/= 1e9
; p
+= 2; break;
2875 if (*p
&& !qemu_isspace(*p
)) {
2876 monitor_printf(mon
, "Unknown unit suffix\n");
2879 qdict_put(qdict
, key
, qfloat_from_double(val
));
2887 while (qemu_isspace(*p
)) {
2891 while (qemu_isgraph(*p
)) {
2894 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2896 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2899 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2902 qdict_put(qdict
, key
, qbool_from_bool(val
));
2907 const char *tmp
= p
;
2914 while (qemu_isspace(*p
))
2919 if(!is_valid_option(p
, typestr
)) {
2921 monitor_printf(mon
, "%s: unsupported option -%c\n",
2933 qdict_put(qdict
, key
, qbool_from_bool(true));
2940 /* package all remaining string */
2943 while (qemu_isspace(*p
)) {
2946 if (*typestr
== '?') {
2949 /* no remaining string: NULL argument */
2955 monitor_printf(mon
, "%s: string expected\n",
2959 qdict_put(qdict
, key
, qstring_from_str(p
));
2965 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2971 /* check that all arguments were parsed */
2972 while (qemu_isspace(*p
))
2975 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2988 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2991 const mon_cmd_t
*cmd
;
2993 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2998 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
3000 monitor_printf(mon
, "Try \"help %s\" for more information\n",
3005 cmd
->mhandler
.cmd(mon
, qdict
);
3009 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3011 const char *p
, *pstart
;
3020 p
= pstart
+ strlen(pstart
);
3022 if (len
> sizeof(cmd
) - 2)
3023 len
= sizeof(cmd
) - 2;
3024 memcpy(cmd
, pstart
, len
);
3026 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3027 readline_add_completion(mon
->rs
, cmd
);
3035 static void file_completion(Monitor
*mon
, const char *input
)
3040 char file
[1024], file_prefix
[1024];
3044 p
= strrchr(input
, '/');
3047 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3048 pstrcpy(path
, sizeof(path
), ".");
3050 input_path_len
= p
- input
+ 1;
3051 memcpy(path
, input
, input_path_len
);
3052 if (input_path_len
> sizeof(path
) - 1)
3053 input_path_len
= sizeof(path
) - 1;
3054 path
[input_path_len
] = '\0';
3055 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3058 ffs
= opendir(path
);
3067 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3071 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3072 memcpy(file
, input
, input_path_len
);
3073 if (input_path_len
< sizeof(file
))
3074 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3076 /* stat the file to find out if it's a directory.
3077 * In that case add a slash to speed up typing long paths
3079 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3080 pstrcat(file
, sizeof(file
), "/");
3082 readline_add_completion(mon
->rs
, file
);
3088 static const char *next_arg_type(const char *typestr
)
3090 const char *p
= strchr(typestr
, ':');
3091 return (p
!= NULL
? ++p
: typestr
);
3094 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3097 if (!str
|| !option
) {
3100 if (!strncmp(option
, str
, strlen(str
))) {
3101 readline_add_completion(rs
, option
);
3105 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3108 ChardevBackendInfoList
*list
, *start
;
3114 readline_set_completion_index(rs
, len
);
3116 start
= list
= qmp_query_chardev_backends(NULL
);
3118 const char *chr_name
= list
->value
->name
;
3120 if (!strncmp(chr_name
, str
, len
)) {
3121 readline_add_completion(rs
, chr_name
);
3125 qapi_free_ChardevBackendInfoList(start
);
3128 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3137 readline_set_completion_index(rs
, len
);
3138 for (i
= 0; NetClientDriver_lookup
[i
]; i
++) {
3139 add_completion_option(rs
, str
, NetClientDriver_lookup
[i
]);
3143 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3153 readline_set_completion_index(rs
, len
);
3154 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3157 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3159 name
= object_class_get_name(OBJECT_CLASS(dc
));
3161 if (!dc
->cannot_instantiate_with_device_add_yet
3162 && !strncmp(name
, str
, len
)) {
3163 readline_add_completion(rs
, name
);
3170 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3180 readline_set_completion_index(rs
, len
);
3181 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3185 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3186 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3187 readline_add_completion(rs
, name
);
3194 static void peripheral_device_del_completion(ReadLineState
*rs
,
3195 const char *str
, size_t len
)
3197 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3198 GSList
*list
, *item
;
3200 list
= qdev_build_hotpluggable_device_list(peripheral
);
3205 for (item
= list
; item
; item
= g_slist_next(item
)) {
3206 DeviceState
*dev
= item
->data
;
3208 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3209 readline_add_completion(rs
, dev
->id
);
3216 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3219 ChardevInfoList
*list
, *start
;
3225 readline_set_completion_index(rs
, len
);
3227 start
= list
= qmp_query_chardev(NULL
);
3229 ChardevInfo
*chr
= list
->value
;
3231 if (!strncmp(chr
->label
, str
, len
)) {
3232 readline_add_completion(rs
, chr
->label
);
3236 qapi_free_ChardevInfoList(start
);
3239 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3242 ChardevInfoList
*list
, *start
;
3245 readline_set_completion_index(rs
, len
);
3247 start
= list
= qmp_query_chardev(NULL
);
3249 ChardevInfo
*chr_info
= list
->value
;
3251 if (!strncmp(chr_info
->label
, str
, len
)) {
3252 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3253 if (chr
&& chr_is_ringbuf(chr
)) {
3254 readline_add_completion(rs
, chr_info
->label
);
3259 qapi_free_ChardevInfoList(start
);
3262 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3267 ringbuf_completion(rs
, str
);
3270 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3279 readline_set_completion_index(rs
, len
);
3280 peripheral_device_del_completion(rs
, str
, len
);
3283 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3285 ObjectPropertyInfoList
*list
, *start
;
3292 readline_set_completion_index(rs
, len
);
3294 start
= list
= qmp_qom_list("/objects", NULL
);
3296 ObjectPropertyInfo
*info
= list
->value
;
3298 if (!strncmp(info
->type
, "child<", 5)
3299 && !strncmp(info
->name
, str
, len
)) {
3300 readline_add_completion(rs
, info
->name
);
3304 qapi_free_ObjectPropertyInfoList(start
);
3307 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3316 sep
= strrchr(str
, '-');
3321 readline_set_completion_index(rs
, len
);
3322 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3323 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3324 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3329 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3334 readline_set_completion_index(rs
, len
);
3336 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3338 count
= qemu_find_net_clients_except(NULL
, ncs
,
3339 NET_CLIENT_DRIVER_NONE
,
3341 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3342 const char *name
= ncs
[i
]->name
;
3343 if (!strncmp(str
, name
, len
)) {
3344 readline_add_completion(rs
, name
);
3347 } else if (nb_args
== 3) {
3348 add_completion_option(rs
, str
, "on");
3349 add_completion_option(rs
, str
, "off");
3353 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3356 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3363 readline_set_completion_index(rs
, len
);
3364 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3366 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3368 const char *name
= ncs
[i
]->name
;
3369 if (strncmp(str
, name
, len
)) {
3372 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3374 readline_add_completion(rs
, name
);
3379 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3384 readline_set_completion_index(rs
, len
);
3387 for (id
= 0; id
< trace_event_count(); id
++) {
3388 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3389 if (!strncmp(str
, event_name
, len
)) {
3390 readline_add_completion(rs
, event_name
);
3396 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3401 readline_set_completion_index(rs
, len
);
3404 for (id
= 0; id
< trace_event_count(); id
++) {
3405 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3406 if (!strncmp(str
, event_name
, len
)) {
3407 readline_add_completion(rs
, event_name
);
3410 } else if (nb_args
== 3) {
3411 add_completion_option(rs
, str
, "on");
3412 add_completion_option(rs
, str
, "off");
3416 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3423 readline_set_completion_index(rs
, strlen(str
));
3424 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3425 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3429 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3435 readline_set_completion_index(rs
, len
);
3438 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3439 const char *name
= MigrationCapability_lookup
[i
];
3440 if (!strncmp(str
, name
, len
)) {
3441 readline_add_completion(rs
, name
);
3444 } else if (nb_args
== 3) {
3445 add_completion_option(rs
, str
, "on");
3446 add_completion_option(rs
, str
, "off");
3450 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3456 readline_set_completion_index(rs
, len
);
3459 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3460 const char *name
= MigrationParameter_lookup
[i
];
3461 if (!strncmp(str
, name
, len
)) {
3462 readline_add_completion(rs
, name
);
3468 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3476 readline_set_completion_index(rs
, len
);
3477 for (i
= 0; host_net_devices
[i
]; i
++) {
3478 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3479 readline_add_completion(rs
, host_net_devices
[i
]);
3484 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3486 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3490 readline_set_completion_index(rs
, len
);
3492 count
= qemu_find_net_clients_except(NULL
, ncs
,
3493 NET_CLIENT_DRIVER_NONE
,
3495 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3499 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3502 snprintf(name
, sizeof(name
), "%d", id
);
3503 if (!strncmp(str
, name
, len
)) {
3504 readline_add_completion(rs
, name
);
3508 } else if (nb_args
== 3) {
3509 count
= qemu_find_net_clients_except(NULL
, ncs
,
3510 NET_CLIENT_DRIVER_NIC
,
3512 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3516 if (ncs
[i
]->info
->type
== NET_CLIENT_DRIVER_HUBPORT
||
3517 net_hub_id_for_client(ncs
[i
], &id
)) {
3520 name
= ncs
[i
]->name
;
3521 if (!strncmp(str
, name
, len
)) {
3522 readline_add_completion(rs
, name
);
3529 static void vm_completion(ReadLineState
*rs
, const char *str
)
3532 BlockDriverState
*bs
;
3533 BdrvNextIterator it
;
3536 readline_set_completion_index(rs
, len
);
3538 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3539 SnapshotInfoList
*snapshots
, *snapshot
;
3540 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3543 aio_context_acquire(ctx
);
3544 if (bdrv_can_snapshot(bs
)) {
3545 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3547 aio_context_release(ctx
);
3552 snapshot
= snapshots
;
3554 char *completion
= snapshot
->value
->name
;
3555 if (!strncmp(str
, completion
, len
)) {
3556 readline_add_completion(rs
, completion
);
3558 completion
= snapshot
->value
->id
;
3559 if (!strncmp(str
, completion
, len
)) {
3560 readline_add_completion(rs
, completion
);
3562 snapshot
= snapshot
->next
;
3564 qapi_free_SnapshotInfoList(snapshots
);
3569 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3572 vm_completion(rs
, str
);
3576 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3579 vm_completion(rs
, str
);
3583 static void monitor_find_completion_by_table(Monitor
*mon
,
3584 const mon_cmd_t
*cmd_table
,
3588 const char *cmdname
;
3590 const char *ptype
, *str
, *name
;
3591 const mon_cmd_t
*cmd
;
3592 BlockBackend
*blk
= NULL
;
3595 /* command completion */
3600 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3601 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3602 cmd_completion(mon
, cmdname
, cmd
->name
);
3605 /* find the command */
3606 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3607 if (compare_cmd(args
[0], cmd
->name
)) {
3615 if (cmd
->sub_table
) {
3616 /* do the job again */
3617 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3618 &args
[1], nb_args
- 1);
3621 if (cmd
->command_completion
) {
3622 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3626 ptype
= next_arg_type(cmd
->args_type
);
3627 for(i
= 0; i
< nb_args
- 2; i
++) {
3628 if (*ptype
!= '\0') {
3629 ptype
= next_arg_type(ptype
);
3630 while (*ptype
== '?')
3631 ptype
= next_arg_type(ptype
);
3634 str
= args
[nb_args
- 1];
3635 while (*ptype
== '-' && ptype
[1] != '\0') {
3636 ptype
= next_arg_type(ptype
);
3640 /* file completion */
3641 readline_set_completion_index(mon
->rs
, strlen(str
));
3642 file_completion(mon
, str
);
3645 /* block device name completion */
3646 readline_set_completion_index(mon
->rs
, strlen(str
));
3647 while ((blk
= blk_next(blk
)) != NULL
) {
3648 name
= blk_name(blk
);
3649 if (str
[0] == '\0' ||
3650 !strncmp(name
, str
, strlen(str
))) {
3651 readline_add_completion(mon
->rs
, name
);
3657 if (!strcmp(cmd
->name
, "help|?")) {
3658 monitor_find_completion_by_table(mon
, cmd_table
,
3659 &args
[1], nb_args
- 1);
3668 static void monitor_find_completion(void *opaque
,
3669 const char *cmdline
)
3671 Monitor
*mon
= opaque
;
3672 char *args
[MAX_ARGS
];
3675 /* 1. parse the cmdline */
3676 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3680 /* if the line ends with a space, it means we want to complete the
3682 len
= strlen(cmdline
);
3683 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3684 if (nb_args
>= MAX_ARGS
) {
3687 args
[nb_args
++] = g_strdup("");
3690 /* 2. auto complete according to args */
3691 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3694 free_cmdline_args(args
, nb_args
);
3697 static int monitor_can_read(void *opaque
)
3699 Monitor
*mon
= opaque
;
3701 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3704 static bool invalid_qmp_mode(const Monitor
*mon
, const char *cmd
,
3707 bool is_cap
= g_str_equal(cmd
, "qmp_capabilities");
3709 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3710 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3711 "Capabilities negotiation is already complete, command "
3712 "'%s' ignored", cmd
);
3715 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3716 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3717 "Expecting capabilities negotiation with "
3718 "'qmp_capabilities' before command '%s'", cmd
);
3725 * Argument validation rules:
3727 * 1. The argument must exist in cmd_args qdict
3728 * 2. The argument type must be the expected one
3730 * Special case: If the argument doesn't exist in cmd_args and
3731 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3732 * checking is skipped for it.
3734 static void check_client_args_type(const QDict
*client_args
,
3735 const QDict
*cmd_args
, int flags
,
3738 const QDictEntry
*ent
;
3740 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3743 const QObject
*client_arg
= qdict_entry_value(ent
);
3744 const char *client_arg_name
= qdict_entry_key(ent
);
3746 obj
= qdict_get(cmd_args
, client_arg_name
);
3748 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3749 /* handler accepts unknowns */
3752 /* client arg doesn't exist */
3753 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3757 arg_type
= qobject_to_qstring(obj
);
3758 assert(arg_type
!= NULL
);
3760 /* check if argument's type is correct */
3761 switch (qstring_get_str(arg_type
)[0]) {
3765 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3766 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3767 client_arg_name
, "string");
3775 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3776 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3777 client_arg_name
, "int");
3782 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3783 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3784 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3785 client_arg_name
, "number");
3791 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3792 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3793 client_arg_name
, "bool");
3798 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3801 /* Any QObject can be passed. */
3806 * These types are not supported by QMP and thus are not
3807 * handled here. Fall through.
3816 * - Check if the client has passed all mandatory args
3817 * - Set special flags for argument validation
3819 static void check_mandatory_args(const QDict
*cmd_args
,
3820 const QDict
*client_args
, int *flags
,
3823 const QDictEntry
*ent
;
3825 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3826 const char *cmd_arg_name
= qdict_entry_key(ent
);
3827 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3828 assert(type
!= NULL
);
3830 if (qstring_get_str(type
)[0] == 'O') {
3831 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3832 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3833 } else if (qstring_get_str(type
)[0] != '-' &&
3834 qstring_get_str(type
)[1] != '?' &&
3835 !qdict_haskey(client_args
, cmd_arg_name
)) {
3836 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3842 static QDict
*qdict_from_args_type(const char *args_type
)
3846 QString
*key
, *type
, *cur_qs
;
3848 assert(args_type
!= NULL
);
3850 qdict
= qdict_new();
3852 if (args_type
== NULL
|| args_type
[0] == '\0') {
3853 /* no args, empty qdict */
3857 key
= qstring_new();
3858 type
= qstring_new();
3863 switch (args_type
[i
]) {
3866 qdict_put(qdict
, qstring_get_str(key
), type
);
3868 if (args_type
[i
] == '\0') {
3871 type
= qstring_new(); /* qdict has ref */
3872 cur_qs
= key
= qstring_new();
3878 qstring_append_chr(cur_qs
, args_type
[i
]);
3888 * Client argument checking rules:
3890 * 1. Client must provide all mandatory arguments
3891 * 2. Each argument provided by the client must be expected
3892 * 3. Each argument provided by the client must have the type expected
3895 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3902 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3905 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3910 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3913 error_propagate(errp
, err
);
3918 * Input object checking rules
3920 * 1. Input object must be a dict
3921 * 2. The "execute" key must exist
3922 * 3. The "execute" key must be a string
3923 * 4. If the "arguments" key exists, it must be a dict
3924 * 5. If the "id" key exists, it can be anything (ie. json-value)
3925 * 6. Any argument not listed above is considered invalid
3927 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3929 const QDictEntry
*ent
;
3930 int has_exec_key
= 0;
3933 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3934 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3938 input_dict
= qobject_to_qdict(input_obj
);
3940 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3941 const char *arg_name
= qdict_entry_key(ent
);
3942 const QObject
*arg_obj
= qdict_entry_value(ent
);
3944 if (!strcmp(arg_name
, "execute")) {
3945 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3946 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3947 "execute", "string");
3951 } else if (!strcmp(arg_name
, "arguments")) {
3952 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3953 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3954 "arguments", "object");
3957 } else if (!strcmp(arg_name
, "id")) {
3958 /* Any string is acceptable as "id", so nothing to check */
3960 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3965 if (!has_exec_key
) {
3966 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3973 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3975 Error
*local_err
= NULL
;
3976 QObject
*obj
, *data
;
3977 QDict
*input
, *args
;
3978 const mon_cmd_t
*cmd
;
3979 const char *cmd_name
;
3980 Monitor
*mon
= cur_mon
;
3982 args
= input
= NULL
;
3985 obj
= json_parser_parse(tokens
, NULL
);
3987 // FIXME: should be triggered in json_parser_parse()
3988 error_setg(&local_err
, QERR_JSON_PARSING
);
3992 input
= qmp_check_input_obj(obj
, &local_err
);
3994 qobject_decref(obj
);
3998 mon
->qmp
.id
= qdict_get(input
, "id");
3999 qobject_incref(mon
->qmp
.id
);
4001 cmd_name
= qdict_get_str(input
, "execute");
4002 trace_handle_qmp_command(mon
, cmd_name
);
4003 cmd
= qmp_find_cmd(cmd_name
);
4005 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4006 "The command %s has not been found", cmd_name
);
4009 if (invalid_qmp_mode(mon
, cmd_name
, &local_err
)) {
4013 obj
= qdict_get(input
, "arguments");
4017 args
= qobject_to_qdict(obj
);
4021 qmp_check_client_args(cmd
, args
, &local_err
);
4026 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
4029 monitor_protocol_emitter(mon
, data
, local_err
);
4030 qobject_decref(data
);
4031 error_free(local_err
);
4036 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4038 Monitor
*old_mon
= cur_mon
;
4042 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
4047 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4049 Monitor
*old_mon
= cur_mon
;
4055 for (i
= 0; i
< size
; i
++)
4056 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4058 if (size
== 0 || buf
[size
- 1] != 0)
4059 monitor_printf(cur_mon
, "corrupted command\n");
4061 handle_hmp_command(cur_mon
, (char *)buf
);
4067 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4068 void *readline_opaque
)
4070 Monitor
*mon
= opaque
;
4072 monitor_suspend(mon
);
4073 handle_hmp_command(mon
, cmdline
);
4074 monitor_resume(mon
);
4077 int monitor_suspend(Monitor
*mon
)
4085 void monitor_resume(Monitor
*mon
)
4089 if (--mon
->suspend_cnt
== 0)
4090 readline_show_prompt(mon
->rs
);
4093 static QObject
*get_qmp_greeting(void)
4095 QObject
*ver
= NULL
;
4097 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4098 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4101 static void monitor_qmp_event(void *opaque
, int event
)
4104 Monitor
*mon
= opaque
;
4107 case CHR_EVENT_OPENED
:
4108 mon
->qmp
.in_command_mode
= false;
4109 data
= get_qmp_greeting();
4110 monitor_json_emitter(mon
, data
);
4111 qobject_decref(data
);
4114 case CHR_EVENT_CLOSED
:
4115 json_message_parser_destroy(&mon
->qmp
.parser
);
4116 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4118 monitor_fdsets_cleanup();
4123 static void monitor_event(void *opaque
, int event
)
4125 Monitor
*mon
= opaque
;
4128 case CHR_EVENT_MUX_IN
:
4129 qemu_mutex_lock(&mon
->out_lock
);
4131 qemu_mutex_unlock(&mon
->out_lock
);
4132 if (mon
->reset_seen
) {
4133 readline_restart(mon
->rs
);
4134 monitor_resume(mon
);
4137 mon
->suspend_cnt
= 0;
4141 case CHR_EVENT_MUX_OUT
:
4142 if (mon
->reset_seen
) {
4143 if (mon
->suspend_cnt
== 0) {
4144 monitor_printf(mon
, "\n");
4147 monitor_suspend(mon
);
4151 qemu_mutex_lock(&mon
->out_lock
);
4153 qemu_mutex_unlock(&mon
->out_lock
);
4156 case CHR_EVENT_OPENED
:
4157 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4158 "information\n", QEMU_VERSION
);
4159 if (!mon
->mux_out
) {
4160 readline_restart(mon
->rs
);
4161 readline_show_prompt(mon
->rs
);
4163 mon
->reset_seen
= 1;
4167 case CHR_EVENT_CLOSED
:
4169 monitor_fdsets_cleanup();
4175 compare_mon_cmd(const void *a
, const void *b
)
4177 return strcmp(((const mon_cmd_t
*)a
)->name
,
4178 ((const mon_cmd_t
*)b
)->name
);
4181 static void sortcmdlist(void)
4184 int elem_size
= sizeof(mon_cmd_t
);
4186 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4187 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4189 array_num
= sizeof(info_cmds
)/elem_size
-1;
4190 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4193 /* These functions just adapt the readline interface in a typesafe way. We
4194 * could cast function pointers but that discards compiler checks.
4196 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4197 const char *fmt
, ...)
4201 monitor_vprintf(opaque
, fmt
, ap
);
4205 static void monitor_readline_flush(void *opaque
)
4207 monitor_flush(opaque
);
4210 static void __attribute__((constructor
)) monitor_lock_init(void)
4212 qemu_mutex_init(&monitor_lock
);
4215 void monitor_init(CharDriverState
*chr
, int flags
)
4217 static int is_first_init
= 1;
4220 if (is_first_init
) {
4221 monitor_qapi_event_init();
4226 mon
= g_malloc(sizeof(*mon
));
4227 monitor_data_init(mon
);
4231 if (flags
& MONITOR_USE_READLINE
) {
4232 mon
->rs
= readline_init(monitor_readline_printf
,
4233 monitor_readline_flush
,
4235 monitor_find_completion
);
4236 monitor_read_command(mon
, 0);
4239 if (monitor_is_qmp(mon
)) {
4240 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4241 monitor_qmp_event
, mon
);
4242 qemu_chr_fe_set_echo(chr
, true);
4243 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4245 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4246 monitor_event
, mon
);
4249 qemu_mutex_lock(&monitor_lock
);
4250 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4251 qemu_mutex_unlock(&monitor_lock
);
4254 void monitor_cleanup(void)
4256 Monitor
*mon
, *next
;
4258 qemu_mutex_lock(&monitor_lock
);
4259 QLIST_FOREACH_SAFE(mon
, &mon_list
, entry
, next
) {
4260 QLIST_REMOVE(mon
, entry
);
4261 monitor_data_destroy(mon
);
4264 qemu_mutex_unlock(&monitor_lock
);
4267 static void bdrv_password_cb(void *opaque
, const char *password
,
4268 void *readline_opaque
)
4270 Monitor
*mon
= opaque
;
4271 BlockDriverState
*bs
= readline_opaque
;
4273 Error
*local_err
= NULL
;
4275 bdrv_add_key(bs
, password
, &local_err
);
4277 error_report_err(local_err
);
4280 if (mon
->password_completion_cb
)
4281 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4283 monitor_read_command(mon
, 1);
4286 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4287 BlockCompletionFunc
*completion_cb
,
4292 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4293 bdrv_get_encrypted_filename(bs
));
4295 mon
->password_completion_cb
= completion_cb
;
4296 mon
->password_opaque
= opaque
;
4298 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4300 if (err
&& completion_cb
)
4301 completion_cb(opaque
, err
);
4306 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4307 BlockCompletionFunc
*completion_cb
,
4313 blk
= blk_by_name(device
);
4315 monitor_printf(mon
, "Device not found %s\n", device
);
4319 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4323 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4326 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4329 if (completion_cb
) {
4330 completion_cb(opaque
, 0);
4335 QemuOptsList qemu_mon_opts
= {
4337 .implied_opt_name
= "chardev",
4338 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4342 .type
= QEMU_OPT_STRING
,
4345 .type
= QEMU_OPT_STRING
,
4348 .type
= QEMU_OPT_BOOL
,
4351 .type
= QEMU_OPT_BOOL
,
4353 { /* end of list */ }
4358 void qmp_rtc_reset_reinjection(Error
**errp
)
4360 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4364 #ifndef TARGET_S390X
4365 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4367 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4372 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4374 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");
4379 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4381 MachineState
*ms
= MACHINE(qdev_get_machine());
4382 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4384 if (!mc
->query_hotpluggable_cpus
) {
4385 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4389 return mc
->query_hotpluggable_cpus(ms
);