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 * We used to define commands in qmp-commands.hx in addition to the
1013 * QAPI schema. This permitted defining some of them only in certain
1014 * configurations. query-commands has always reflected that (good,
1015 * because it lets QMP clients figure out what's actually available),
1016 * while query-qmp-schema never did (not so good). This function is a
1017 * hack to keep the configuration-specific commands defined exactly as
1018 * before, even though qmp-commands.hx is gone.
1020 * FIXME Educate the QAPI schema on configuration-specific commands,
1021 * and drop this hack.
1023 static void qmp_unregister_commands_hack(void)
1025 #ifndef CONFIG_SPICE
1026 qmp_unregister_command("query-spice");
1029 qmp_unregister_command("rtc-reset-reinjection");
1031 #ifndef TARGET_S390X
1032 qmp_unregister_command("dump-skeys");
1035 qmp_unregister_command("query-gic-capabilities");
1039 static void qmp_init_marshal(void)
1041 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema
,
1043 qmp_register_command("device_add", qmp_device_add
,
1045 qmp_register_command("netdev_add", qmp_netdev_add
,
1048 /* call it after the rest of qapi_init() */
1049 register_module_init(qmp_unregister_commands_hack
, MODULE_INIT_QAPI
);
1052 qapi_init(qmp_init_marshal
);
1054 /* set the current CPU defined by the user */
1055 int monitor_set_cpu(int cpu_index
)
1059 cpu
= qemu_get_cpu(cpu_index
);
1063 cur_mon
->mon_cpu
= cpu
;
1067 CPUState
*mon_get_cpu(void)
1069 if (!cur_mon
->mon_cpu
) {
1072 cpu_synchronize_state(cur_mon
->mon_cpu
);
1073 return cur_mon
->mon_cpu
;
1076 CPUArchState
*mon_get_cpu_env(void)
1078 return mon_get_cpu()->env_ptr
;
1081 int monitor_get_cpu_index(void)
1083 return mon_get_cpu()->cpu_index
;
1086 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1088 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1091 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1093 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1094 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1097 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1099 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1102 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1111 str
= readline_get_history(mon
->rs
, i
);
1114 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1119 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1121 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1124 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1126 const char *name
= qdict_get_try_str(qdict
, "name");
1127 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1128 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1129 TraceEventInfoList
*events
;
1130 TraceEventInfoList
*elem
;
1131 Error
*local_err
= NULL
;
1137 monitor_printf(mon
, "argument vcpu must be positive");
1141 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1143 error_report_err(local_err
);
1147 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1148 monitor_printf(mon
, "%s : state %u\n",
1150 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1152 qapi_free_TraceEventInfoList(events
);
1155 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1156 bool has_port
, int64_t port
,
1157 bool has_tls_port
, int64_t tls_port
,
1158 bool has_cert_subject
, const char *cert_subject
,
1161 if (strcmp(protocol
, "spice") == 0) {
1162 if (!qemu_using_spice(errp
)) {
1166 if (!has_port
&& !has_tls_port
) {
1167 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1171 if (qemu_spice_migrate_info(hostname
,
1172 has_port
? port
: -1,
1173 has_tls_port
? tls_port
: -1,
1175 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1181 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1184 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1188 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1190 error_report_err(err
);
1194 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1197 const char *items
= qdict_get_str(qdict
, "items");
1199 if (!strcmp(items
, "none")) {
1202 mask
= qemu_str_to_log_mask(items
);
1204 help_cmd(mon
, "log");
1211 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1213 const char *option
= qdict_get_try_str(qdict
, "option");
1214 if (!option
|| !strcmp(option
, "on")) {
1216 } else if (!strcmp(option
, "off")) {
1219 monitor_printf(mon
, "unexpected option %s\n", option
);
1223 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1225 const char *device
= qdict_get_try_str(qdict
, "device");
1227 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1228 if (gdbserver_start(device
) < 0) {
1229 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1231 } else if (strcmp(device
, "none") == 0) {
1232 monitor_printf(mon
, "Disabled gdbserver\n");
1234 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1239 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1241 const char *action
= qdict_get_str(qdict
, "action");
1242 if (select_watchdog_action(action
) == -1) {
1243 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1247 static void monitor_printc(Monitor
*mon
, int c
)
1249 monitor_printf(mon
, "'");
1252 monitor_printf(mon
, "\\'");
1255 monitor_printf(mon
, "\\\\");
1258 monitor_printf(mon
, "\\n");
1261 monitor_printf(mon
, "\\r");
1264 if (c
>= 32 && c
<= 126) {
1265 monitor_printf(mon
, "%c", c
);
1267 monitor_printf(mon
, "\\x%02x", c
);
1271 monitor_printf(mon
, "'");
1274 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1275 hwaddr addr
, int is_physical
)
1277 int l
, line_size
, i
, max_digits
, len
;
1281 if (format
== 'i') {
1284 CPUArchState
*env
= mon_get_cpu_env();
1287 } else if (wsize
== 4) {
1290 /* as default we use the current CS size */
1293 #ifdef TARGET_X86_64
1294 if ((env
->efer
& MSR_EFER_LMA
) &&
1295 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1299 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1305 CPUArchState
*env
= mon_get_cpu_env();
1306 flags
= msr_le
<< 16;
1307 flags
|= env
->bfd_mach
;
1309 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1313 len
= wsize
* count
;
1322 max_digits
= (wsize
* 8 + 2) / 3;
1326 max_digits
= (wsize
* 8) / 4;
1330 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1339 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1341 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1346 cpu_physical_memory_read(addr
, buf
, l
);
1348 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1349 monitor_printf(mon
, " Cannot access memory\n");
1358 v
= ldub_p(buf
+ i
);
1361 v
= lduw_p(buf
+ i
);
1364 v
= (uint32_t)ldl_p(buf
+ i
);
1370 monitor_printf(mon
, " ");
1373 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1376 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1379 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1382 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1385 monitor_printc(mon
, v
);
1390 monitor_printf(mon
, "\n");
1396 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1398 int count
= qdict_get_int(qdict
, "count");
1399 int format
= qdict_get_int(qdict
, "format");
1400 int size
= qdict_get_int(qdict
, "size");
1401 target_long addr
= qdict_get_int(qdict
, "addr");
1403 memory_dump(mon
, count
, format
, size
, addr
, 0);
1406 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1408 int count
= qdict_get_int(qdict
, "count");
1409 int format
= qdict_get_int(qdict
, "format");
1410 int size
= qdict_get_int(qdict
, "size");
1411 hwaddr addr
= qdict_get_int(qdict
, "addr");
1413 memory_dump(mon
, count
, format
, size
, addr
, 1);
1416 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1418 int format
= qdict_get_int(qdict
, "format");
1419 hwaddr val
= qdict_get_int(qdict
, "val");
1423 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1426 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1429 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1433 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1436 monitor_printc(mon
, val
);
1439 monitor_printf(mon
, "\n");
1442 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1446 uint32_t start
= qdict_get_int(qdict
, "start");
1447 uint32_t size
= qdict_get_int(qdict
, "size");
1450 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1451 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1452 MEMTXATTRS_UNSPECIFIED
, NULL
);
1453 /* BSD sum algorithm ('sum' Unix command) */
1454 sum
= (sum
>> 1) | (sum
<< 15);
1457 monitor_printf(mon
, "%05d\n", sum
);
1460 static int mouse_button_state
;
1462 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1464 int dx
, dy
, dz
, button
;
1465 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1466 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1467 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1469 dx
= strtol(dx_str
, NULL
, 0);
1470 dy
= strtol(dy_str
, NULL
, 0);
1471 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1472 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1475 dz
= strtol(dz_str
, NULL
, 0);
1477 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1478 qemu_input_queue_btn(NULL
, button
, true);
1479 qemu_input_event_sync();
1480 qemu_input_queue_btn(NULL
, button
, false);
1483 qemu_input_event_sync();
1486 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1488 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1489 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1490 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1491 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1493 int button_state
= qdict_get_int(qdict
, "button_state");
1495 if (mouse_button_state
== button_state
) {
1498 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1499 qemu_input_event_sync();
1500 mouse_button_state
= button_state
;
1503 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1505 int size
= qdict_get_int(qdict
, "size");
1506 int addr
= qdict_get_int(qdict
, "addr");
1507 int has_index
= qdict_haskey(qdict
, "index");
1512 int index
= qdict_get_int(qdict
, "index");
1513 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1521 val
= cpu_inb(addr
);
1525 val
= cpu_inw(addr
);
1529 val
= cpu_inl(addr
);
1533 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1534 suffix
, addr
, size
* 2, val
);
1537 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1539 int size
= qdict_get_int(qdict
, "size");
1540 int addr
= qdict_get_int(qdict
, "addr");
1541 int val
= qdict_get_int(qdict
, "val");
1543 addr
&= IOPORTS_MASK
;
1548 cpu_outb(addr
, val
);
1551 cpu_outw(addr
, val
);
1554 cpu_outl(addr
, val
);
1559 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1561 Error
*local_err
= NULL
;
1562 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1564 qemu_boot_set(bootdevice
, &local_err
);
1566 error_report_err(local_err
);
1568 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1572 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1574 mtree_info((fprintf_function
)monitor_printf
, mon
);
1577 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1583 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1584 query_numa_node_mem(node_mem
);
1585 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1586 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1587 monitor_printf(mon
, "node %d cpus:", i
);
1589 if (cpu
->numa_node
== i
) {
1590 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1593 monitor_printf(mon
, "\n");
1594 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1600 #ifdef CONFIG_PROFILER
1605 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1607 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1608 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1609 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1610 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1615 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1617 monitor_printf(mon
, "Internal profiler not compiled\n");
1621 /* Capture support */
1622 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1624 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1629 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1630 monitor_printf(mon
, "[%d]: ", i
);
1631 s
->ops
.info (s
->opaque
);
1635 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1638 int n
= qdict_get_int(qdict
, "n");
1641 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1643 s
->ops
.destroy (s
->opaque
);
1644 QLIST_REMOVE (s
, entries
);
1651 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1653 const char *path
= qdict_get_str(qdict
, "path");
1654 int has_freq
= qdict_haskey(qdict
, "freq");
1655 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1656 int has_bits
= qdict_haskey(qdict
, "bits");
1657 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1658 int has_channels
= qdict_haskey(qdict
, "nchannels");
1659 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1662 s
= g_malloc0 (sizeof (*s
));
1664 freq
= has_freq
? freq
: 44100;
1665 bits
= has_bits
? bits
: 16;
1666 nchannels
= has_channels
? nchannels
: 2;
1668 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1669 monitor_printf(mon
, "Failed to add wave capture\n");
1673 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1676 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1678 qemu_acl
*acl
= qemu_acl_find(name
);
1681 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1686 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1688 const char *aclname
= qdict_get_str(qdict
, "aclname");
1689 qemu_acl
*acl
= find_acl(mon
, aclname
);
1690 qemu_acl_entry
*entry
;
1694 monitor_printf(mon
, "policy: %s\n",
1695 acl
->defaultDeny
? "deny" : "allow");
1696 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1698 monitor_printf(mon
, "%d: %s %s\n", i
,
1699 entry
->deny
? "deny" : "allow", entry
->match
);
1704 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1706 const char *aclname
= qdict_get_str(qdict
, "aclname");
1707 qemu_acl
*acl
= find_acl(mon
, aclname
);
1710 qemu_acl_reset(acl
);
1711 monitor_printf(mon
, "acl: removed all rules\n");
1715 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1717 const char *aclname
= qdict_get_str(qdict
, "aclname");
1718 const char *policy
= qdict_get_str(qdict
, "policy");
1719 qemu_acl
*acl
= find_acl(mon
, aclname
);
1722 if (strcmp(policy
, "allow") == 0) {
1723 acl
->defaultDeny
= 0;
1724 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1725 } else if (strcmp(policy
, "deny") == 0) {
1726 acl
->defaultDeny
= 1;
1727 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1729 monitor_printf(mon
, "acl: unknown policy '%s', "
1730 "expected 'deny' or 'allow'\n", policy
);
1735 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1737 const char *aclname
= qdict_get_str(qdict
, "aclname");
1738 const char *match
= qdict_get_str(qdict
, "match");
1739 const char *policy
= qdict_get_str(qdict
, "policy");
1740 int has_index
= qdict_haskey(qdict
, "index");
1741 int index
= qdict_get_try_int(qdict
, "index", -1);
1742 qemu_acl
*acl
= find_acl(mon
, aclname
);
1746 if (strcmp(policy
, "allow") == 0) {
1748 } else if (strcmp(policy
, "deny") == 0) {
1751 monitor_printf(mon
, "acl: unknown policy '%s', "
1752 "expected 'deny' or 'allow'\n", policy
);
1756 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1758 ret
= qemu_acl_append(acl
, deny
, match
);
1760 monitor_printf(mon
, "acl: unable to add acl entry\n");
1762 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1766 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1768 const char *aclname
= qdict_get_str(qdict
, "aclname");
1769 const char *match
= qdict_get_str(qdict
, "match");
1770 qemu_acl
*acl
= find_acl(mon
, aclname
);
1774 ret
= qemu_acl_remove(acl
, match
);
1776 monitor_printf(mon
, "acl: no matching acl entry\n");
1778 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1782 void qmp_getfd(const char *fdname
, Error
**errp
)
1787 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1789 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1793 if (qemu_isdigit(fdname
[0])) {
1795 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1796 "a name not starting with a digit");
1800 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1801 if (strcmp(monfd
->name
, fdname
) != 0) {
1810 monfd
= g_malloc0(sizeof(mon_fd_t
));
1811 monfd
->name
= g_strdup(fdname
);
1814 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1817 void qmp_closefd(const char *fdname
, Error
**errp
)
1821 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1822 if (strcmp(monfd
->name
, fdname
) != 0) {
1826 QLIST_REMOVE(monfd
, next
);
1828 g_free(monfd
->name
);
1833 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1836 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1838 int saved_vm_running
= runstate_is_running();
1839 const char *name
= qdict_get_str(qdict
, "name");
1841 vm_stop(RUN_STATE_RESTORE_VM
);
1843 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1848 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1852 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1855 if (strcmp(monfd
->name
, fdname
) != 0) {
1861 /* caller takes ownership of fd */
1862 QLIST_REMOVE(monfd
, next
);
1863 g_free(monfd
->name
);
1869 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1873 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1875 MonFdsetFd
*mon_fdset_fd
;
1876 MonFdsetFd
*mon_fdset_fd_next
;
1878 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1879 if ((mon_fdset_fd
->removed
||
1880 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1881 runstate_is_running()) {
1882 close(mon_fdset_fd
->fd
);
1883 g_free(mon_fdset_fd
->opaque
);
1884 QLIST_REMOVE(mon_fdset_fd
, next
);
1885 g_free(mon_fdset_fd
);
1889 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1890 QLIST_REMOVE(mon_fdset
, next
);
1895 static void monitor_fdsets_cleanup(void)
1897 MonFdset
*mon_fdset
;
1898 MonFdset
*mon_fdset_next
;
1900 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1901 monitor_fdset_cleanup(mon_fdset
);
1905 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1906 const char *opaque
, Error
**errp
)
1909 Monitor
*mon
= cur_mon
;
1912 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1914 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1918 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1919 has_opaque
, opaque
, errp
);
1931 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1933 MonFdset
*mon_fdset
;
1934 MonFdsetFd
*mon_fdset_fd
;
1937 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1938 if (mon_fdset
->id
!= fdset_id
) {
1941 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1943 if (mon_fdset_fd
->fd
!= fd
) {
1946 mon_fdset_fd
->removed
= true;
1949 mon_fdset_fd
->removed
= true;
1952 if (has_fd
&& !mon_fdset_fd
) {
1955 monitor_fdset_cleanup(mon_fdset
);
1961 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1964 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1966 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1969 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1971 MonFdset
*mon_fdset
;
1972 MonFdsetFd
*mon_fdset_fd
;
1973 FdsetInfoList
*fdset_list
= NULL
;
1975 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1976 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1977 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1979 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1980 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1982 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1983 FdsetFdInfoList
*fdsetfd_info
;
1985 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1986 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1987 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1988 if (mon_fdset_fd
->opaque
) {
1989 fdsetfd_info
->value
->has_opaque
= true;
1990 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1992 fdsetfd_info
->value
->has_opaque
= false;
1995 fdsetfd_info
->next
= fdsetfd_list
;
1996 fdsetfd_list
= fdsetfd_info
;
1999 fdset_info
->value
->fds
= fdsetfd_list
;
2001 fdset_info
->next
= fdset_list
;
2002 fdset_list
= fdset_info
;
2008 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2009 bool has_opaque
, const char *opaque
,
2012 MonFdset
*mon_fdset
= NULL
;
2013 MonFdsetFd
*mon_fdset_fd
;
2017 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2018 /* Break if match found or match impossible due to ordering by ID */
2019 if (fdset_id
<= mon_fdset
->id
) {
2020 if (fdset_id
< mon_fdset
->id
) {
2028 if (mon_fdset
== NULL
) {
2029 int64_t fdset_id_prev
= -1;
2030 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2034 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2035 "a non-negative value");
2038 /* Use specified fdset ID */
2039 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2040 mon_fdset_cur
= mon_fdset
;
2041 if (fdset_id
< mon_fdset_cur
->id
) {
2046 /* Use first available fdset ID */
2047 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2048 mon_fdset_cur
= mon_fdset
;
2049 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2050 fdset_id_prev
= mon_fdset_cur
->id
;
2057 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2059 mon_fdset
->id
= fdset_id
;
2061 mon_fdset
->id
= fdset_id_prev
+ 1;
2064 /* The fdset list is ordered by fdset ID */
2065 if (!mon_fdset_cur
) {
2066 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2067 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2068 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2070 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2074 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2075 mon_fdset_fd
->fd
= fd
;
2076 mon_fdset_fd
->removed
= false;
2078 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2080 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2082 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2083 fdinfo
->fdset_id
= mon_fdset
->id
;
2084 fdinfo
->fd
= mon_fdset_fd
->fd
;
2089 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2092 MonFdset
*mon_fdset
;
2093 MonFdsetFd
*mon_fdset_fd
;
2096 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2097 if (mon_fdset
->id
!= fdset_id
) {
2100 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2101 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2102 if (mon_fd_flags
== -1) {
2106 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2107 return mon_fdset_fd
->fd
;
2119 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2121 MonFdset
*mon_fdset
;
2122 MonFdsetFd
*mon_fdset_fd_dup
;
2124 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2125 if (mon_fdset
->id
!= fdset_id
) {
2128 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2129 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2133 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2134 mon_fdset_fd_dup
->fd
= dup_fd
;
2135 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2141 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2143 MonFdset
*mon_fdset
;
2144 MonFdsetFd
*mon_fdset_fd_dup
;
2146 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2147 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2148 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2150 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2151 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2152 monitor_fdset_cleanup(mon_fdset
);
2156 return mon_fdset
->id
;
2164 int monitor_fdset_dup_fd_find(int dup_fd
)
2166 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2169 void monitor_fdset_dup_fd_remove(int dup_fd
)
2171 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2174 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2177 Error
*local_err
= NULL
;
2179 if (!qemu_isdigit(fdname
[0]) && mon
) {
2180 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2182 fd
= qemu_parse_fd(fdname
);
2184 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2189 error_propagate(errp
, local_err
);
2198 /* Please update hmp-commands.hx when adding or changing commands */
2199 static mon_cmd_t info_cmds
[] = {
2200 #include "hmp-commands-info.h"
2204 /* mon_cmds and info_cmds would be sorted at runtime */
2205 static mon_cmd_t mon_cmds
[] = {
2206 #include "hmp-commands.h"
2210 static const mon_cmd_t qmp_cmds
[] = {
2211 #include "qmp-commands-old.h"
2215 /*******************************************************************/
2217 static const char *pch
;
2218 static sigjmp_buf expr_env
;
2221 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2222 expr_error(Monitor
*mon
, const char *fmt
, ...)
2226 monitor_vprintf(mon
, fmt
, ap
);
2227 monitor_printf(mon
, "\n");
2229 siglongjmp(expr_env
, 1);
2232 /* return 0 if OK, -1 if not found */
2233 static int get_monitor_def(target_long
*pval
, const char *name
)
2235 const MonitorDef
*md
= target_monitor_defs();
2244 for(; md
->name
!= NULL
; md
++) {
2245 if (compare_cmd(name
, md
->name
)) {
2246 if (md
->get_value
) {
2247 *pval
= md
->get_value(md
, md
->offset
);
2249 CPUArchState
*env
= mon_get_cpu_env();
2250 ptr
= (uint8_t *)env
+ md
->offset
;
2253 *pval
= *(int32_t *)ptr
;
2256 *pval
= *(target_long
*)ptr
;
2267 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2269 *pval
= (target_long
) tmp
;
2275 static void next(void)
2279 while (qemu_isspace(*pch
))
2284 static int64_t expr_sum(Monitor
*mon
);
2286 static int64_t expr_unary(Monitor
*mon
)
2295 n
= expr_unary(mon
);
2299 n
= -expr_unary(mon
);
2303 n
= ~expr_unary(mon
);
2309 expr_error(mon
, "')' expected");
2316 expr_error(mon
, "character constant expected");
2320 expr_error(mon
, "missing terminating \' character");
2330 while ((*pch
>= 'a' && *pch
<= 'z') ||
2331 (*pch
>= 'A' && *pch
<= 'Z') ||
2332 (*pch
>= '0' && *pch
<= '9') ||
2333 *pch
== '_' || *pch
== '.') {
2334 if ((q
- buf
) < sizeof(buf
) - 1)
2338 while (qemu_isspace(*pch
))
2341 ret
= get_monitor_def(®
, buf
);
2343 expr_error(mon
, "unknown register");
2348 expr_error(mon
, "unexpected end of expression");
2353 n
= strtoull(pch
, &p
, 0);
2354 if (errno
== ERANGE
) {
2355 expr_error(mon
, "number too large");
2358 expr_error(mon
, "invalid char '%c' in expression", *p
);
2361 while (qemu_isspace(*pch
))
2369 static int64_t expr_prod(Monitor
*mon
)
2374 val
= expr_unary(mon
);
2377 if (op
!= '*' && op
!= '/' && op
!= '%')
2380 val2
= expr_unary(mon
);
2389 expr_error(mon
, "division by zero");
2400 static int64_t expr_logic(Monitor
*mon
)
2405 val
= expr_prod(mon
);
2408 if (op
!= '&' && op
!= '|' && op
!= '^')
2411 val2
= expr_prod(mon
);
2428 static int64_t expr_sum(Monitor
*mon
)
2433 val
= expr_logic(mon
);
2436 if (op
!= '+' && op
!= '-')
2439 val2
= expr_logic(mon
);
2448 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2451 if (sigsetjmp(expr_env
, 0)) {
2455 while (qemu_isspace(*pch
))
2457 *pval
= expr_sum(mon
);
2462 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2464 const char *p
= *pp
;
2468 d
= strtod(p
, &tailp
);
2470 monitor_printf(mon
, "Number expected\n");
2473 if (d
!= d
|| d
- d
!= 0) {
2474 /* NaN or infinity */
2475 monitor_printf(mon
, "Bad number\n");
2484 * Store the command-name in cmdname, and return a pointer to
2485 * the remaining of the command string.
2487 static const char *get_command_name(const char *cmdline
,
2488 char *cmdname
, size_t nlen
)
2491 const char *p
, *pstart
;
2494 while (qemu_isspace(*p
))
2499 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2504 memcpy(cmdname
, pstart
, len
);
2505 cmdname
[len
] = '\0';
2510 * Read key of 'type' into 'key' and return the current
2513 static char *key_get_info(const char *type
, char **key
)
2521 p
= strchr(type
, ':');
2528 str
= g_malloc(len
+ 1);
2529 memcpy(str
, type
, len
);
2536 static int default_fmt_format
= 'x';
2537 static int default_fmt_size
= 4;
2539 static int is_valid_option(const char *c
, const char *typestr
)
2547 typestr
= strstr(typestr
, option
);
2548 return (typestr
!= NULL
);
2551 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2552 const char *cmdname
)
2554 const mon_cmd_t
*cmd
;
2556 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2557 if (compare_cmd(cmdname
, cmd
->name
)) {
2565 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2567 return search_dispatch_table(qmp_cmds
, cmdname
);
2571 * Parse command name from @cmdp according to command table @table.
2572 * If blank, return NULL.
2573 * Else, if no valid command can be found, report to @mon, and return
2575 * Else, change @cmdp to point right behind the name, and return its
2576 * command table entry.
2577 * Do not assume the return value points into @table! It doesn't when
2578 * the command is found in a sub-command table.
2580 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2585 const mon_cmd_t
*cmd
;
2588 /* extract the command name */
2589 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2593 cmd
= search_dispatch_table(table
, cmdname
);
2595 monitor_printf(mon
, "unknown command: '%.*s'\n",
2596 (int)(p
- *cmdp
), *cmdp
);
2600 /* filter out following useless space */
2601 while (qemu_isspace(*p
)) {
2606 /* search sub command */
2607 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2608 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2615 * Parse arguments for @cmd.
2616 * If it can't be parsed, report to @mon, and return NULL.
2617 * Else, insert command arguments into a QDict, and return it.
2618 * Note: On success, caller has to free the QDict structure.
2621 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2623 const mon_cmd_t
*cmd
)
2625 const char *typestr
;
2628 const char *p
= *endp
;
2630 QDict
*qdict
= qdict_new();
2632 /* parse the parameters */
2633 typestr
= cmd
->args_type
;
2635 typestr
= key_get_info(typestr
, &key
);
2647 while (qemu_isspace(*p
))
2649 if (*typestr
== '?') {
2652 /* no optional string: NULL argument */
2656 ret
= get_str(buf
, sizeof(buf
), &p
);
2660 monitor_printf(mon
, "%s: filename expected\n",
2664 monitor_printf(mon
, "%s: block device name expected\n",
2668 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2673 qdict_put(qdict
, key
, qstring_from_str(buf
));
2678 QemuOptsList
*opts_list
;
2681 opts_list
= qemu_find_opts(key
);
2682 if (!opts_list
|| opts_list
->desc
->name
) {
2685 while (qemu_isspace(*p
)) {
2690 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2693 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2697 qemu_opts_to_qdict(opts
, qdict
);
2698 qemu_opts_del(opts
);
2703 int count
, format
, size
;
2705 while (qemu_isspace(*p
))
2711 if (qemu_isdigit(*p
)) {
2713 while (qemu_isdigit(*p
)) {
2714 count
= count
* 10 + (*p
- '0');
2752 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2753 monitor_printf(mon
, "invalid char in format: '%c'\n",
2758 format
= default_fmt_format
;
2759 if (format
!= 'i') {
2760 /* for 'i', not specifying a size gives -1 as size */
2762 size
= default_fmt_size
;
2763 default_fmt_size
= size
;
2765 default_fmt_format
= format
;
2768 format
= default_fmt_format
;
2769 if (format
!= 'i') {
2770 size
= default_fmt_size
;
2775 qdict_put(qdict
, "count", qint_from_int(count
));
2776 qdict_put(qdict
, "format", qint_from_int(format
));
2777 qdict_put(qdict
, "size", qint_from_int(size
));
2786 while (qemu_isspace(*p
))
2788 if (*typestr
== '?' || *typestr
== '.') {
2789 if (*typestr
== '?') {
2797 while (qemu_isspace(*p
))
2806 if (get_expr(mon
, &val
, &p
))
2808 /* Check if 'i' is greater than 32-bit */
2809 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2810 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2811 monitor_printf(mon
, "integer is for 32-bit values\n");
2813 } else if (c
== 'M') {
2815 monitor_printf(mon
, "enter a positive value\n");
2820 qdict_put(qdict
, key
, qint_from_int(val
));
2828 while (qemu_isspace(*p
)) {
2831 if (*typestr
== '?') {
2837 val
= qemu_strtosz(p
, &end
);
2839 monitor_printf(mon
, "invalid size\n");
2842 qdict_put(qdict
, key
, qint_from_int(val
));
2850 while (qemu_isspace(*p
))
2852 if (*typestr
== '?') {
2858 if (get_double(mon
, &val
, &p
) < 0) {
2861 if (p
[0] && p
[1] == 's') {
2864 val
/= 1e3
; p
+= 2; break;
2866 val
/= 1e6
; p
+= 2; break;
2868 val
/= 1e9
; p
+= 2; break;
2871 if (*p
&& !qemu_isspace(*p
)) {
2872 monitor_printf(mon
, "Unknown unit suffix\n");
2875 qdict_put(qdict
, key
, qfloat_from_double(val
));
2883 while (qemu_isspace(*p
)) {
2887 while (qemu_isgraph(*p
)) {
2890 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2892 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2895 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2898 qdict_put(qdict
, key
, qbool_from_bool(val
));
2903 const char *tmp
= p
;
2910 while (qemu_isspace(*p
))
2915 if(!is_valid_option(p
, typestr
)) {
2917 monitor_printf(mon
, "%s: unsupported option -%c\n",
2929 qdict_put(qdict
, key
, qbool_from_bool(true));
2936 /* package all remaining string */
2939 while (qemu_isspace(*p
)) {
2942 if (*typestr
== '?') {
2945 /* no remaining string: NULL argument */
2951 monitor_printf(mon
, "%s: string expected\n",
2955 qdict_put(qdict
, key
, qstring_from_str(p
));
2961 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2967 /* check that all arguments were parsed */
2968 while (qemu_isspace(*p
))
2971 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2984 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2987 const mon_cmd_t
*cmd
;
2989 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2994 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2996 monitor_printf(mon
, "Try \"help %s\" for more information\n",
3001 cmd
->mhandler
.cmd(mon
, qdict
);
3005 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3007 const char *p
, *pstart
;
3016 p
= pstart
+ strlen(pstart
);
3018 if (len
> sizeof(cmd
) - 2)
3019 len
= sizeof(cmd
) - 2;
3020 memcpy(cmd
, pstart
, len
);
3022 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3023 readline_add_completion(mon
->rs
, cmd
);
3031 static void file_completion(Monitor
*mon
, const char *input
)
3036 char file
[1024], file_prefix
[1024];
3040 p
= strrchr(input
, '/');
3043 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3044 pstrcpy(path
, sizeof(path
), ".");
3046 input_path_len
= p
- input
+ 1;
3047 memcpy(path
, input
, input_path_len
);
3048 if (input_path_len
> sizeof(path
) - 1)
3049 input_path_len
= sizeof(path
) - 1;
3050 path
[input_path_len
] = '\0';
3051 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3054 ffs
= opendir(path
);
3063 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3067 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3068 memcpy(file
, input
, input_path_len
);
3069 if (input_path_len
< sizeof(file
))
3070 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3072 /* stat the file to find out if it's a directory.
3073 * In that case add a slash to speed up typing long paths
3075 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3076 pstrcat(file
, sizeof(file
), "/");
3078 readline_add_completion(mon
->rs
, file
);
3084 static const char *next_arg_type(const char *typestr
)
3086 const char *p
= strchr(typestr
, ':');
3087 return (p
!= NULL
? ++p
: typestr
);
3090 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3093 if (!str
|| !option
) {
3096 if (!strncmp(option
, str
, strlen(str
))) {
3097 readline_add_completion(rs
, option
);
3101 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3104 ChardevBackendInfoList
*list
, *start
;
3110 readline_set_completion_index(rs
, len
);
3112 start
= list
= qmp_query_chardev_backends(NULL
);
3114 const char *chr_name
= list
->value
->name
;
3116 if (!strncmp(chr_name
, str
, len
)) {
3117 readline_add_completion(rs
, chr_name
);
3121 qapi_free_ChardevBackendInfoList(start
);
3124 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3133 readline_set_completion_index(rs
, len
);
3134 for (i
= 0; NetClientDriver_lookup
[i
]; i
++) {
3135 add_completion_option(rs
, str
, NetClientDriver_lookup
[i
]);
3139 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3149 readline_set_completion_index(rs
, len
);
3150 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3153 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3155 name
= object_class_get_name(OBJECT_CLASS(dc
));
3157 if (!dc
->cannot_instantiate_with_device_add_yet
3158 && !strncmp(name
, str
, len
)) {
3159 readline_add_completion(rs
, name
);
3166 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3176 readline_set_completion_index(rs
, len
);
3177 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3181 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3182 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3183 readline_add_completion(rs
, name
);
3190 static void peripheral_device_del_completion(ReadLineState
*rs
,
3191 const char *str
, size_t len
)
3193 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3194 GSList
*list
, *item
;
3196 list
= qdev_build_hotpluggable_device_list(peripheral
);
3201 for (item
= list
; item
; item
= g_slist_next(item
)) {
3202 DeviceState
*dev
= item
->data
;
3204 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3205 readline_add_completion(rs
, dev
->id
);
3212 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3215 ChardevInfoList
*list
, *start
;
3221 readline_set_completion_index(rs
, len
);
3223 start
= list
= qmp_query_chardev(NULL
);
3225 ChardevInfo
*chr
= list
->value
;
3227 if (!strncmp(chr
->label
, str
, len
)) {
3228 readline_add_completion(rs
, chr
->label
);
3232 qapi_free_ChardevInfoList(start
);
3235 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3238 ChardevInfoList
*list
, *start
;
3241 readline_set_completion_index(rs
, len
);
3243 start
= list
= qmp_query_chardev(NULL
);
3245 ChardevInfo
*chr_info
= list
->value
;
3247 if (!strncmp(chr_info
->label
, str
, len
)) {
3248 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3249 if (chr
&& chr_is_ringbuf(chr
)) {
3250 readline_add_completion(rs
, chr_info
->label
);
3255 qapi_free_ChardevInfoList(start
);
3258 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3263 ringbuf_completion(rs
, str
);
3266 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3275 readline_set_completion_index(rs
, len
);
3276 peripheral_device_del_completion(rs
, str
, len
);
3279 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3281 ObjectPropertyInfoList
*list
, *start
;
3288 readline_set_completion_index(rs
, len
);
3290 start
= list
= qmp_qom_list("/objects", NULL
);
3292 ObjectPropertyInfo
*info
= list
->value
;
3294 if (!strncmp(info
->type
, "child<", 5)
3295 && !strncmp(info
->name
, str
, len
)) {
3296 readline_add_completion(rs
, info
->name
);
3300 qapi_free_ObjectPropertyInfoList(start
);
3303 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3312 sep
= strrchr(str
, '-');
3317 readline_set_completion_index(rs
, len
);
3318 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3319 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3320 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3325 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3330 readline_set_completion_index(rs
, len
);
3332 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3334 count
= qemu_find_net_clients_except(NULL
, ncs
,
3335 NET_CLIENT_DRIVER_NONE
,
3337 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3338 const char *name
= ncs
[i
]->name
;
3339 if (!strncmp(str
, name
, len
)) {
3340 readline_add_completion(rs
, name
);
3343 } else if (nb_args
== 3) {
3344 add_completion_option(rs
, str
, "on");
3345 add_completion_option(rs
, str
, "off");
3349 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3352 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3359 readline_set_completion_index(rs
, len
);
3360 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3362 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3364 const char *name
= ncs
[i
]->name
;
3365 if (strncmp(str
, name
, len
)) {
3368 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3370 readline_add_completion(rs
, name
);
3375 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3380 readline_set_completion_index(rs
, len
);
3383 for (id
= 0; id
< trace_event_count(); id
++) {
3384 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3385 if (!strncmp(str
, event_name
, len
)) {
3386 readline_add_completion(rs
, event_name
);
3392 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3397 readline_set_completion_index(rs
, len
);
3400 for (id
= 0; id
< trace_event_count(); id
++) {
3401 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3402 if (!strncmp(str
, event_name
, len
)) {
3403 readline_add_completion(rs
, event_name
);
3406 } else if (nb_args
== 3) {
3407 add_completion_option(rs
, str
, "on");
3408 add_completion_option(rs
, str
, "off");
3412 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3419 readline_set_completion_index(rs
, strlen(str
));
3420 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3421 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3425 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3431 readline_set_completion_index(rs
, len
);
3434 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3435 const char *name
= MigrationCapability_lookup
[i
];
3436 if (!strncmp(str
, name
, len
)) {
3437 readline_add_completion(rs
, name
);
3440 } else if (nb_args
== 3) {
3441 add_completion_option(rs
, str
, "on");
3442 add_completion_option(rs
, str
, "off");
3446 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3452 readline_set_completion_index(rs
, len
);
3455 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3456 const char *name
= MigrationParameter_lookup
[i
];
3457 if (!strncmp(str
, name
, len
)) {
3458 readline_add_completion(rs
, name
);
3464 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3472 readline_set_completion_index(rs
, len
);
3473 for (i
= 0; host_net_devices
[i
]; i
++) {
3474 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3475 readline_add_completion(rs
, host_net_devices
[i
]);
3480 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3482 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3486 readline_set_completion_index(rs
, len
);
3488 count
= qemu_find_net_clients_except(NULL
, ncs
,
3489 NET_CLIENT_DRIVER_NONE
,
3491 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3495 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3498 snprintf(name
, sizeof(name
), "%d", id
);
3499 if (!strncmp(str
, name
, len
)) {
3500 readline_add_completion(rs
, name
);
3504 } else if (nb_args
== 3) {
3505 count
= qemu_find_net_clients_except(NULL
, ncs
,
3506 NET_CLIENT_DRIVER_NIC
,
3508 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3512 if (ncs
[i
]->info
->type
== NET_CLIENT_DRIVER_HUBPORT
||
3513 net_hub_id_for_client(ncs
[i
], &id
)) {
3516 name
= ncs
[i
]->name
;
3517 if (!strncmp(str
, name
, len
)) {
3518 readline_add_completion(rs
, name
);
3525 static void vm_completion(ReadLineState
*rs
, const char *str
)
3528 BlockDriverState
*bs
;
3529 BdrvNextIterator it
;
3532 readline_set_completion_index(rs
, len
);
3534 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3535 SnapshotInfoList
*snapshots
, *snapshot
;
3536 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3539 aio_context_acquire(ctx
);
3540 if (bdrv_can_snapshot(bs
)) {
3541 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3543 aio_context_release(ctx
);
3548 snapshot
= snapshots
;
3550 char *completion
= snapshot
->value
->name
;
3551 if (!strncmp(str
, completion
, len
)) {
3552 readline_add_completion(rs
, completion
);
3554 completion
= snapshot
->value
->id
;
3555 if (!strncmp(str
, completion
, len
)) {
3556 readline_add_completion(rs
, completion
);
3558 snapshot
= snapshot
->next
;
3560 qapi_free_SnapshotInfoList(snapshots
);
3565 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3568 vm_completion(rs
, str
);
3572 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3575 vm_completion(rs
, str
);
3579 static void monitor_find_completion_by_table(Monitor
*mon
,
3580 const mon_cmd_t
*cmd_table
,
3584 const char *cmdname
;
3586 const char *ptype
, *str
, *name
;
3587 const mon_cmd_t
*cmd
;
3588 BlockBackend
*blk
= NULL
;
3591 /* command completion */
3596 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3597 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3598 cmd_completion(mon
, cmdname
, cmd
->name
);
3601 /* find the command */
3602 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3603 if (compare_cmd(args
[0], cmd
->name
)) {
3611 if (cmd
->sub_table
) {
3612 /* do the job again */
3613 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3614 &args
[1], nb_args
- 1);
3617 if (cmd
->command_completion
) {
3618 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3622 ptype
= next_arg_type(cmd
->args_type
);
3623 for(i
= 0; i
< nb_args
- 2; i
++) {
3624 if (*ptype
!= '\0') {
3625 ptype
= next_arg_type(ptype
);
3626 while (*ptype
== '?')
3627 ptype
= next_arg_type(ptype
);
3630 str
= args
[nb_args
- 1];
3631 while (*ptype
== '-' && ptype
[1] != '\0') {
3632 ptype
= next_arg_type(ptype
);
3636 /* file completion */
3637 readline_set_completion_index(mon
->rs
, strlen(str
));
3638 file_completion(mon
, str
);
3641 /* block device name completion */
3642 readline_set_completion_index(mon
->rs
, strlen(str
));
3643 while ((blk
= blk_next(blk
)) != NULL
) {
3644 name
= blk_name(blk
);
3645 if (str
[0] == '\0' ||
3646 !strncmp(name
, str
, strlen(str
))) {
3647 readline_add_completion(mon
->rs
, name
);
3653 if (!strcmp(cmd
->name
, "help|?")) {
3654 monitor_find_completion_by_table(mon
, cmd_table
,
3655 &args
[1], nb_args
- 1);
3664 static void monitor_find_completion(void *opaque
,
3665 const char *cmdline
)
3667 Monitor
*mon
= opaque
;
3668 char *args
[MAX_ARGS
];
3671 /* 1. parse the cmdline */
3672 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3676 /* if the line ends with a space, it means we want to complete the
3678 len
= strlen(cmdline
);
3679 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3680 if (nb_args
>= MAX_ARGS
) {
3683 args
[nb_args
++] = g_strdup("");
3686 /* 2. auto complete according to args */
3687 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3690 free_cmdline_args(args
, nb_args
);
3693 static int monitor_can_read(void *opaque
)
3695 Monitor
*mon
= opaque
;
3697 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3700 static bool invalid_qmp_mode(const Monitor
*mon
, const char *cmd
,
3703 bool is_cap
= g_str_equal(cmd
, "qmp_capabilities");
3705 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3706 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3707 "Capabilities negotiation is already complete, command "
3708 "'%s' ignored", cmd
);
3711 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3712 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3713 "Expecting capabilities negotiation with "
3714 "'qmp_capabilities' before command '%s'", cmd
);
3721 * Argument validation rules:
3723 * 1. The argument must exist in cmd_args qdict
3724 * 2. The argument type must be the expected one
3726 * Special case: If the argument doesn't exist in cmd_args and
3727 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3728 * checking is skipped for it.
3730 static void check_client_args_type(const QDict
*client_args
,
3731 const QDict
*cmd_args
, int flags
,
3734 const QDictEntry
*ent
;
3736 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3739 const QObject
*client_arg
= qdict_entry_value(ent
);
3740 const char *client_arg_name
= qdict_entry_key(ent
);
3742 obj
= qdict_get(cmd_args
, client_arg_name
);
3744 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3745 /* handler accepts unknowns */
3748 /* client arg doesn't exist */
3749 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3753 arg_type
= qobject_to_qstring(obj
);
3754 assert(arg_type
!= NULL
);
3756 /* check if argument's type is correct */
3757 switch (qstring_get_str(arg_type
)[0]) {
3761 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3762 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3763 client_arg_name
, "string");
3771 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3772 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3773 client_arg_name
, "int");
3778 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3779 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3780 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3781 client_arg_name
, "number");
3787 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3788 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3789 client_arg_name
, "bool");
3794 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3797 /* Any QObject can be passed. */
3802 * These types are not supported by QMP and thus are not
3803 * handled here. Fall through.
3812 * - Check if the client has passed all mandatory args
3813 * - Set special flags for argument validation
3815 static void check_mandatory_args(const QDict
*cmd_args
,
3816 const QDict
*client_args
, int *flags
,
3819 const QDictEntry
*ent
;
3821 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3822 const char *cmd_arg_name
= qdict_entry_key(ent
);
3823 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3824 assert(type
!= NULL
);
3826 if (qstring_get_str(type
)[0] == 'O') {
3827 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3828 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3829 } else if (qstring_get_str(type
)[0] != '-' &&
3830 qstring_get_str(type
)[1] != '?' &&
3831 !qdict_haskey(client_args
, cmd_arg_name
)) {
3832 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3838 static QDict
*qdict_from_args_type(const char *args_type
)
3842 QString
*key
, *type
, *cur_qs
;
3844 assert(args_type
!= NULL
);
3846 qdict
= qdict_new();
3848 if (args_type
== NULL
|| args_type
[0] == '\0') {
3849 /* no args, empty qdict */
3853 key
= qstring_new();
3854 type
= qstring_new();
3859 switch (args_type
[i
]) {
3862 qdict_put(qdict
, qstring_get_str(key
), type
);
3864 if (args_type
[i
] == '\0') {
3867 type
= qstring_new(); /* qdict has ref */
3868 cur_qs
= key
= qstring_new();
3874 qstring_append_chr(cur_qs
, args_type
[i
]);
3884 * Client argument checking rules:
3886 * 1. Client must provide all mandatory arguments
3887 * 2. Each argument provided by the client must be expected
3888 * 3. Each argument provided by the client must have the type expected
3891 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3898 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3901 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3906 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3909 error_propagate(errp
, err
);
3914 * Input object checking rules
3916 * 1. Input object must be a dict
3917 * 2. The "execute" key must exist
3918 * 3. The "execute" key must be a string
3919 * 4. If the "arguments" key exists, it must be a dict
3920 * 5. If the "id" key exists, it can be anything (ie. json-value)
3921 * 6. Any argument not listed above is considered invalid
3923 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3925 const QDictEntry
*ent
;
3926 int has_exec_key
= 0;
3929 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3930 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3934 input_dict
= qobject_to_qdict(input_obj
);
3936 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3937 const char *arg_name
= qdict_entry_key(ent
);
3938 const QObject
*arg_obj
= qdict_entry_value(ent
);
3940 if (!strcmp(arg_name
, "execute")) {
3941 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3942 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3943 "execute", "string");
3947 } else if (!strcmp(arg_name
, "arguments")) {
3948 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3949 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3950 "arguments", "object");
3953 } else if (!strcmp(arg_name
, "id")) {
3954 /* Any string is acceptable as "id", so nothing to check */
3956 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3961 if (!has_exec_key
) {
3962 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3969 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3971 Error
*local_err
= NULL
;
3972 QObject
*obj
, *data
;
3973 QDict
*input
, *args
;
3974 const mon_cmd_t
*cmd
;
3976 const char *cmd_name
;
3977 Monitor
*mon
= cur_mon
;
3979 args
= input
= NULL
;
3982 obj
= json_parser_parse(tokens
, NULL
);
3984 // FIXME: should be triggered in json_parser_parse()
3985 error_setg(&local_err
, QERR_JSON_PARSING
);
3989 input
= qmp_check_input_obj(obj
, &local_err
);
3991 qobject_decref(obj
);
3995 mon
->qmp
.id
= qdict_get(input
, "id");
3996 qobject_incref(mon
->qmp
.id
);
3998 cmd_name
= qdict_get_str(input
, "execute");
3999 trace_handle_qmp_command(mon
, cmd_name
);
4000 cmd
= qmp_find_cmd(cmd_name
);
4001 qcmd
= qmp_find_command(cmd_name
);
4002 if (!qcmd
|| !cmd
) {
4003 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4004 "The command %s has not been found", cmd_name
);
4007 if (invalid_qmp_mode(mon
, cmd_name
, &local_err
)) {
4011 obj
= qdict_get(input
, "arguments");
4015 args
= qobject_to_qdict(obj
);
4019 qmp_check_client_args(cmd
, args
, &local_err
);
4024 qcmd
->fn(args
, &data
, &local_err
);
4027 monitor_protocol_emitter(mon
, data
, local_err
);
4028 qobject_decref(data
);
4029 error_free(local_err
);
4034 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4036 Monitor
*old_mon
= cur_mon
;
4040 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
4045 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4047 Monitor
*old_mon
= cur_mon
;
4053 for (i
= 0; i
< size
; i
++)
4054 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4056 if (size
== 0 || buf
[size
- 1] != 0)
4057 monitor_printf(cur_mon
, "corrupted command\n");
4059 handle_hmp_command(cur_mon
, (char *)buf
);
4065 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4066 void *readline_opaque
)
4068 Monitor
*mon
= opaque
;
4070 monitor_suspend(mon
);
4071 handle_hmp_command(mon
, cmdline
);
4072 monitor_resume(mon
);
4075 int monitor_suspend(Monitor
*mon
)
4083 void monitor_resume(Monitor
*mon
)
4087 if (--mon
->suspend_cnt
== 0)
4088 readline_show_prompt(mon
->rs
);
4091 static QObject
*get_qmp_greeting(void)
4093 QObject
*ver
= NULL
;
4095 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4097 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
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
);