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 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
962 CommandInfoList
*info
, **list
= opaque
;
968 info
= g_malloc0(sizeof(*info
));
969 info
->value
= g_malloc0(sizeof(*info
->value
));
970 info
->value
->name
= g_strdup(cmd
->name
);
975 CommandInfoList
*qmp_query_commands(Error
**errp
)
977 CommandInfoList
*list
= NULL
;
979 qmp_for_each_command(query_commands_cb
, &list
);
984 EventInfoList
*qmp_query_events(Error
**errp
)
986 EventInfoList
*info
, *ev_list
= NULL
;
989 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
990 const char *event_name
= QAPIEvent_lookup
[e
];
991 assert(event_name
!= NULL
);
992 info
= g_malloc0(sizeof(*info
));
993 info
->value
= g_malloc0(sizeof(*info
->value
));
994 info
->value
->name
= g_strdup(event_name
);
996 info
->next
= ev_list
;
1004 * Minor hack: generated marshalling suppressed for this command
1005 * ('gen': false in the schema) so we can parse the JSON string
1006 * directly into QObject instead of first parsing it with
1007 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1008 * to QObject with generated output marshallers, every time. Instead,
1009 * we do it in test-qmp-input-visitor.c, just to make sure
1010 * qapi-introspect.py's output actually conforms to the schema.
1012 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
1015 *ret_data
= qobject_from_json(qmp_schema_json
);
1019 * We used to define commands in qmp-commands.hx in addition to the
1020 * QAPI schema. This permitted defining some of them only in certain
1021 * configurations. query-commands has always reflected that (good,
1022 * because it lets QMP clients figure out what's actually available),
1023 * while query-qmp-schema never did (not so good). This function is a
1024 * hack to keep the configuration-specific commands defined exactly as
1025 * before, even though qmp-commands.hx is gone.
1027 * FIXME Educate the QAPI schema on configuration-specific commands,
1028 * and drop this hack.
1030 static void qmp_unregister_commands_hack(void)
1032 #ifndef CONFIG_SPICE
1033 qmp_unregister_command("query-spice");
1036 qmp_unregister_command("rtc-reset-reinjection");
1038 #ifndef TARGET_S390X
1039 qmp_unregister_command("dump-skeys");
1042 qmp_unregister_command("query-gic-capabilities");
1046 static void qmp_init_marshal(void)
1048 qmp_register_command("query-qmp-schema", qmp_query_qmp_schema
,
1050 qmp_register_command("device_add", qmp_device_add
,
1052 qmp_register_command("netdev_add", qmp_netdev_add
,
1055 /* call it after the rest of qapi_init() */
1056 register_module_init(qmp_unregister_commands_hack
, MODULE_INIT_QAPI
);
1059 qapi_init(qmp_init_marshal
);
1061 /* set the current CPU defined by the user */
1062 int monitor_set_cpu(int cpu_index
)
1066 cpu
= qemu_get_cpu(cpu_index
);
1070 cur_mon
->mon_cpu
= cpu
;
1074 CPUState
*mon_get_cpu(void)
1076 if (!cur_mon
->mon_cpu
) {
1079 cpu_synchronize_state(cur_mon
->mon_cpu
);
1080 return cur_mon
->mon_cpu
;
1083 CPUArchState
*mon_get_cpu_env(void)
1085 return mon_get_cpu()->env_ptr
;
1088 int monitor_get_cpu_index(void)
1090 return mon_get_cpu()->cpu_index
;
1093 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1095 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1098 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1100 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1101 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1104 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1106 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1109 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1118 str
= readline_get_history(mon
->rs
, i
);
1121 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1126 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1128 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1131 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1133 const char *name
= qdict_get_try_str(qdict
, "name");
1134 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1135 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1136 TraceEventInfoList
*events
;
1137 TraceEventInfoList
*elem
;
1138 Error
*local_err
= NULL
;
1144 monitor_printf(mon
, "argument vcpu must be positive");
1148 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1150 error_report_err(local_err
);
1154 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1155 monitor_printf(mon
, "%s : state %u\n",
1157 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1159 qapi_free_TraceEventInfoList(events
);
1162 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1163 bool has_port
, int64_t port
,
1164 bool has_tls_port
, int64_t tls_port
,
1165 bool has_cert_subject
, const char *cert_subject
,
1168 if (strcmp(protocol
, "spice") == 0) {
1169 if (!qemu_using_spice(errp
)) {
1173 if (!has_port
&& !has_tls_port
) {
1174 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1178 if (qemu_spice_migrate_info(hostname
,
1179 has_port
? port
: -1,
1180 has_tls_port
? tls_port
: -1,
1182 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1188 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1191 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1195 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1197 error_report_err(err
);
1201 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1204 const char *items
= qdict_get_str(qdict
, "items");
1206 if (!strcmp(items
, "none")) {
1209 mask
= qemu_str_to_log_mask(items
);
1211 help_cmd(mon
, "log");
1218 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1220 const char *option
= qdict_get_try_str(qdict
, "option");
1221 if (!option
|| !strcmp(option
, "on")) {
1223 } else if (!strcmp(option
, "off")) {
1226 monitor_printf(mon
, "unexpected option %s\n", option
);
1230 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1232 const char *device
= qdict_get_try_str(qdict
, "device");
1234 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1235 if (gdbserver_start(device
) < 0) {
1236 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1238 } else if (strcmp(device
, "none") == 0) {
1239 monitor_printf(mon
, "Disabled gdbserver\n");
1241 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1246 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1248 const char *action
= qdict_get_str(qdict
, "action");
1249 if (select_watchdog_action(action
) == -1) {
1250 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1254 static void monitor_printc(Monitor
*mon
, int c
)
1256 monitor_printf(mon
, "'");
1259 monitor_printf(mon
, "\\'");
1262 monitor_printf(mon
, "\\\\");
1265 monitor_printf(mon
, "\\n");
1268 monitor_printf(mon
, "\\r");
1271 if (c
>= 32 && c
<= 126) {
1272 monitor_printf(mon
, "%c", c
);
1274 monitor_printf(mon
, "\\x%02x", c
);
1278 monitor_printf(mon
, "'");
1281 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1282 hwaddr addr
, int is_physical
)
1284 int l
, line_size
, i
, max_digits
, len
;
1288 if (format
== 'i') {
1291 CPUArchState
*env
= mon_get_cpu_env();
1294 } else if (wsize
== 4) {
1297 /* as default we use the current CS size */
1300 #ifdef TARGET_X86_64
1301 if ((env
->efer
& MSR_EFER_LMA
) &&
1302 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1306 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1312 CPUArchState
*env
= mon_get_cpu_env();
1313 flags
= msr_le
<< 16;
1314 flags
|= env
->bfd_mach
;
1316 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1320 len
= wsize
* count
;
1329 max_digits
= (wsize
* 8 + 2) / 3;
1333 max_digits
= (wsize
* 8) / 4;
1337 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1346 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1348 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1353 cpu_physical_memory_read(addr
, buf
, l
);
1355 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1356 monitor_printf(mon
, " Cannot access memory\n");
1365 v
= ldub_p(buf
+ i
);
1368 v
= lduw_p(buf
+ i
);
1371 v
= (uint32_t)ldl_p(buf
+ i
);
1377 monitor_printf(mon
, " ");
1380 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1383 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1386 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1389 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1392 monitor_printc(mon
, v
);
1397 monitor_printf(mon
, "\n");
1403 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1405 int count
= qdict_get_int(qdict
, "count");
1406 int format
= qdict_get_int(qdict
, "format");
1407 int size
= qdict_get_int(qdict
, "size");
1408 target_long addr
= qdict_get_int(qdict
, "addr");
1410 memory_dump(mon
, count
, format
, size
, addr
, 0);
1413 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1415 int count
= qdict_get_int(qdict
, "count");
1416 int format
= qdict_get_int(qdict
, "format");
1417 int size
= qdict_get_int(qdict
, "size");
1418 hwaddr addr
= qdict_get_int(qdict
, "addr");
1420 memory_dump(mon
, count
, format
, size
, addr
, 1);
1423 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1425 int format
= qdict_get_int(qdict
, "format");
1426 hwaddr val
= qdict_get_int(qdict
, "val");
1430 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1433 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1436 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1440 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1443 monitor_printc(mon
, val
);
1446 monitor_printf(mon
, "\n");
1449 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1453 uint32_t start
= qdict_get_int(qdict
, "start");
1454 uint32_t size
= qdict_get_int(qdict
, "size");
1457 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1458 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1459 MEMTXATTRS_UNSPECIFIED
, NULL
);
1460 /* BSD sum algorithm ('sum' Unix command) */
1461 sum
= (sum
>> 1) | (sum
<< 15);
1464 monitor_printf(mon
, "%05d\n", sum
);
1467 static int mouse_button_state
;
1469 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1471 int dx
, dy
, dz
, button
;
1472 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1473 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1474 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1476 dx
= strtol(dx_str
, NULL
, 0);
1477 dy
= strtol(dy_str
, NULL
, 0);
1478 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1479 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1482 dz
= strtol(dz_str
, NULL
, 0);
1484 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1485 qemu_input_queue_btn(NULL
, button
, true);
1486 qemu_input_event_sync();
1487 qemu_input_queue_btn(NULL
, button
, false);
1490 qemu_input_event_sync();
1493 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1495 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1496 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1497 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1498 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1500 int button_state
= qdict_get_int(qdict
, "button_state");
1502 if (mouse_button_state
== button_state
) {
1505 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1506 qemu_input_event_sync();
1507 mouse_button_state
= button_state
;
1510 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1512 int size
= qdict_get_int(qdict
, "size");
1513 int addr
= qdict_get_int(qdict
, "addr");
1514 int has_index
= qdict_haskey(qdict
, "index");
1519 int index
= qdict_get_int(qdict
, "index");
1520 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1528 val
= cpu_inb(addr
);
1532 val
= cpu_inw(addr
);
1536 val
= cpu_inl(addr
);
1540 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1541 suffix
, addr
, size
* 2, val
);
1544 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1546 int size
= qdict_get_int(qdict
, "size");
1547 int addr
= qdict_get_int(qdict
, "addr");
1548 int val
= qdict_get_int(qdict
, "val");
1550 addr
&= IOPORTS_MASK
;
1555 cpu_outb(addr
, val
);
1558 cpu_outw(addr
, val
);
1561 cpu_outl(addr
, val
);
1566 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1568 Error
*local_err
= NULL
;
1569 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1571 qemu_boot_set(bootdevice
, &local_err
);
1573 error_report_err(local_err
);
1575 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1579 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1581 mtree_info((fprintf_function
)monitor_printf
, mon
);
1584 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1590 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1591 query_numa_node_mem(node_mem
);
1592 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1593 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1594 monitor_printf(mon
, "node %d cpus:", i
);
1596 if (cpu
->numa_node
== i
) {
1597 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1600 monitor_printf(mon
, "\n");
1601 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1607 #ifdef CONFIG_PROFILER
1612 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1614 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1615 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1616 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1617 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1622 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1624 monitor_printf(mon
, "Internal profiler not compiled\n");
1628 /* Capture support */
1629 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1631 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1636 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1637 monitor_printf(mon
, "[%d]: ", i
);
1638 s
->ops
.info (s
->opaque
);
1642 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1645 int n
= qdict_get_int(qdict
, "n");
1648 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1650 s
->ops
.destroy (s
->opaque
);
1651 QLIST_REMOVE (s
, entries
);
1658 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1660 const char *path
= qdict_get_str(qdict
, "path");
1661 int has_freq
= qdict_haskey(qdict
, "freq");
1662 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1663 int has_bits
= qdict_haskey(qdict
, "bits");
1664 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1665 int has_channels
= qdict_haskey(qdict
, "nchannels");
1666 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1669 s
= g_malloc0 (sizeof (*s
));
1671 freq
= has_freq
? freq
: 44100;
1672 bits
= has_bits
? bits
: 16;
1673 nchannels
= has_channels
? nchannels
: 2;
1675 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1676 monitor_printf(mon
, "Failed to add wave capture\n");
1680 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1683 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1685 qemu_acl
*acl
= qemu_acl_find(name
);
1688 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1693 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1695 const char *aclname
= qdict_get_str(qdict
, "aclname");
1696 qemu_acl
*acl
= find_acl(mon
, aclname
);
1697 qemu_acl_entry
*entry
;
1701 monitor_printf(mon
, "policy: %s\n",
1702 acl
->defaultDeny
? "deny" : "allow");
1703 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1705 monitor_printf(mon
, "%d: %s %s\n", i
,
1706 entry
->deny
? "deny" : "allow", entry
->match
);
1711 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1713 const char *aclname
= qdict_get_str(qdict
, "aclname");
1714 qemu_acl
*acl
= find_acl(mon
, aclname
);
1717 qemu_acl_reset(acl
);
1718 monitor_printf(mon
, "acl: removed all rules\n");
1722 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1724 const char *aclname
= qdict_get_str(qdict
, "aclname");
1725 const char *policy
= qdict_get_str(qdict
, "policy");
1726 qemu_acl
*acl
= find_acl(mon
, aclname
);
1729 if (strcmp(policy
, "allow") == 0) {
1730 acl
->defaultDeny
= 0;
1731 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1732 } else if (strcmp(policy
, "deny") == 0) {
1733 acl
->defaultDeny
= 1;
1734 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1736 monitor_printf(mon
, "acl: unknown policy '%s', "
1737 "expected 'deny' or 'allow'\n", policy
);
1742 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1744 const char *aclname
= qdict_get_str(qdict
, "aclname");
1745 const char *match
= qdict_get_str(qdict
, "match");
1746 const char *policy
= qdict_get_str(qdict
, "policy");
1747 int has_index
= qdict_haskey(qdict
, "index");
1748 int index
= qdict_get_try_int(qdict
, "index", -1);
1749 qemu_acl
*acl
= find_acl(mon
, aclname
);
1753 if (strcmp(policy
, "allow") == 0) {
1755 } else if (strcmp(policy
, "deny") == 0) {
1758 monitor_printf(mon
, "acl: unknown policy '%s', "
1759 "expected 'deny' or 'allow'\n", policy
);
1763 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1765 ret
= qemu_acl_append(acl
, deny
, match
);
1767 monitor_printf(mon
, "acl: unable to add acl entry\n");
1769 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1773 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1775 const char *aclname
= qdict_get_str(qdict
, "aclname");
1776 const char *match
= qdict_get_str(qdict
, "match");
1777 qemu_acl
*acl
= find_acl(mon
, aclname
);
1781 ret
= qemu_acl_remove(acl
, match
);
1783 monitor_printf(mon
, "acl: no matching acl entry\n");
1785 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1789 void qmp_getfd(const char *fdname
, Error
**errp
)
1794 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1796 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1800 if (qemu_isdigit(fdname
[0])) {
1802 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1803 "a name not starting with a digit");
1807 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1808 if (strcmp(monfd
->name
, fdname
) != 0) {
1817 monfd
= g_malloc0(sizeof(mon_fd_t
));
1818 monfd
->name
= g_strdup(fdname
);
1821 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1824 void qmp_closefd(const char *fdname
, Error
**errp
)
1828 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1829 if (strcmp(monfd
->name
, fdname
) != 0) {
1833 QLIST_REMOVE(monfd
, next
);
1835 g_free(monfd
->name
);
1840 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1843 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1845 int saved_vm_running
= runstate_is_running();
1846 const char *name
= qdict_get_str(qdict
, "name");
1848 vm_stop(RUN_STATE_RESTORE_VM
);
1850 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1855 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1859 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1862 if (strcmp(monfd
->name
, fdname
) != 0) {
1868 /* caller takes ownership of fd */
1869 QLIST_REMOVE(monfd
, next
);
1870 g_free(monfd
->name
);
1876 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1880 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1882 MonFdsetFd
*mon_fdset_fd
;
1883 MonFdsetFd
*mon_fdset_fd_next
;
1885 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1886 if ((mon_fdset_fd
->removed
||
1887 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1888 runstate_is_running()) {
1889 close(mon_fdset_fd
->fd
);
1890 g_free(mon_fdset_fd
->opaque
);
1891 QLIST_REMOVE(mon_fdset_fd
, next
);
1892 g_free(mon_fdset_fd
);
1896 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1897 QLIST_REMOVE(mon_fdset
, next
);
1902 static void monitor_fdsets_cleanup(void)
1904 MonFdset
*mon_fdset
;
1905 MonFdset
*mon_fdset_next
;
1907 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1908 monitor_fdset_cleanup(mon_fdset
);
1912 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1913 const char *opaque
, Error
**errp
)
1916 Monitor
*mon
= cur_mon
;
1919 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1921 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1925 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1926 has_opaque
, opaque
, errp
);
1938 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1940 MonFdset
*mon_fdset
;
1941 MonFdsetFd
*mon_fdset_fd
;
1944 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1945 if (mon_fdset
->id
!= fdset_id
) {
1948 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1950 if (mon_fdset_fd
->fd
!= fd
) {
1953 mon_fdset_fd
->removed
= true;
1956 mon_fdset_fd
->removed
= true;
1959 if (has_fd
&& !mon_fdset_fd
) {
1962 monitor_fdset_cleanup(mon_fdset
);
1968 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1971 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1973 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1976 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1978 MonFdset
*mon_fdset
;
1979 MonFdsetFd
*mon_fdset_fd
;
1980 FdsetInfoList
*fdset_list
= NULL
;
1982 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1983 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1984 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1986 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1987 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1989 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1990 FdsetFdInfoList
*fdsetfd_info
;
1992 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1993 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1994 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1995 if (mon_fdset_fd
->opaque
) {
1996 fdsetfd_info
->value
->has_opaque
= true;
1997 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1999 fdsetfd_info
->value
->has_opaque
= false;
2002 fdsetfd_info
->next
= fdsetfd_list
;
2003 fdsetfd_list
= fdsetfd_info
;
2006 fdset_info
->value
->fds
= fdsetfd_list
;
2008 fdset_info
->next
= fdset_list
;
2009 fdset_list
= fdset_info
;
2015 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2016 bool has_opaque
, const char *opaque
,
2019 MonFdset
*mon_fdset
= NULL
;
2020 MonFdsetFd
*mon_fdset_fd
;
2024 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2025 /* Break if match found or match impossible due to ordering by ID */
2026 if (fdset_id
<= mon_fdset
->id
) {
2027 if (fdset_id
< mon_fdset
->id
) {
2035 if (mon_fdset
== NULL
) {
2036 int64_t fdset_id_prev
= -1;
2037 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2041 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2042 "a non-negative value");
2045 /* Use specified fdset ID */
2046 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2047 mon_fdset_cur
= mon_fdset
;
2048 if (fdset_id
< mon_fdset_cur
->id
) {
2053 /* Use first available fdset ID */
2054 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2055 mon_fdset_cur
= mon_fdset
;
2056 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2057 fdset_id_prev
= mon_fdset_cur
->id
;
2064 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2066 mon_fdset
->id
= fdset_id
;
2068 mon_fdset
->id
= fdset_id_prev
+ 1;
2071 /* The fdset list is ordered by fdset ID */
2072 if (!mon_fdset_cur
) {
2073 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2074 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2075 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2077 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2081 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2082 mon_fdset_fd
->fd
= fd
;
2083 mon_fdset_fd
->removed
= false;
2085 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2087 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2089 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2090 fdinfo
->fdset_id
= mon_fdset
->id
;
2091 fdinfo
->fd
= mon_fdset_fd
->fd
;
2096 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2099 MonFdset
*mon_fdset
;
2100 MonFdsetFd
*mon_fdset_fd
;
2103 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2104 if (mon_fdset
->id
!= fdset_id
) {
2107 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2108 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2109 if (mon_fd_flags
== -1) {
2113 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2114 return mon_fdset_fd
->fd
;
2126 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2128 MonFdset
*mon_fdset
;
2129 MonFdsetFd
*mon_fdset_fd_dup
;
2131 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2132 if (mon_fdset
->id
!= fdset_id
) {
2135 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2136 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2140 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2141 mon_fdset_fd_dup
->fd
= dup_fd
;
2142 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2148 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2150 MonFdset
*mon_fdset
;
2151 MonFdsetFd
*mon_fdset_fd_dup
;
2153 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2154 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2155 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2157 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2158 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2159 monitor_fdset_cleanup(mon_fdset
);
2163 return mon_fdset
->id
;
2171 int monitor_fdset_dup_fd_find(int dup_fd
)
2173 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2176 void monitor_fdset_dup_fd_remove(int dup_fd
)
2178 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2181 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2184 Error
*local_err
= NULL
;
2186 if (!qemu_isdigit(fdname
[0]) && mon
) {
2187 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2189 fd
= qemu_parse_fd(fdname
);
2191 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2196 error_propagate(errp
, local_err
);
2205 /* Please update hmp-commands.hx when adding or changing commands */
2206 static mon_cmd_t info_cmds
[] = {
2207 #include "hmp-commands-info.h"
2211 /* mon_cmds and info_cmds would be sorted at runtime */
2212 static mon_cmd_t mon_cmds
[] = {
2213 #include "hmp-commands.h"
2217 static const mon_cmd_t qmp_cmds
[] = {
2218 #include "qmp-commands-old.h"
2222 /*******************************************************************/
2224 static const char *pch
;
2225 static sigjmp_buf expr_env
;
2228 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2229 expr_error(Monitor
*mon
, const char *fmt
, ...)
2233 monitor_vprintf(mon
, fmt
, ap
);
2234 monitor_printf(mon
, "\n");
2236 siglongjmp(expr_env
, 1);
2239 /* return 0 if OK, -1 if not found */
2240 static int get_monitor_def(target_long
*pval
, const char *name
)
2242 const MonitorDef
*md
= target_monitor_defs();
2251 for(; md
->name
!= NULL
; md
++) {
2252 if (compare_cmd(name
, md
->name
)) {
2253 if (md
->get_value
) {
2254 *pval
= md
->get_value(md
, md
->offset
);
2256 CPUArchState
*env
= mon_get_cpu_env();
2257 ptr
= (uint8_t *)env
+ md
->offset
;
2260 *pval
= *(int32_t *)ptr
;
2263 *pval
= *(target_long
*)ptr
;
2274 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2276 *pval
= (target_long
) tmp
;
2282 static void next(void)
2286 while (qemu_isspace(*pch
))
2291 static int64_t expr_sum(Monitor
*mon
);
2293 static int64_t expr_unary(Monitor
*mon
)
2302 n
= expr_unary(mon
);
2306 n
= -expr_unary(mon
);
2310 n
= ~expr_unary(mon
);
2316 expr_error(mon
, "')' expected");
2323 expr_error(mon
, "character constant expected");
2327 expr_error(mon
, "missing terminating \' character");
2337 while ((*pch
>= 'a' && *pch
<= 'z') ||
2338 (*pch
>= 'A' && *pch
<= 'Z') ||
2339 (*pch
>= '0' && *pch
<= '9') ||
2340 *pch
== '_' || *pch
== '.') {
2341 if ((q
- buf
) < sizeof(buf
) - 1)
2345 while (qemu_isspace(*pch
))
2348 ret
= get_monitor_def(®
, buf
);
2350 expr_error(mon
, "unknown register");
2355 expr_error(mon
, "unexpected end of expression");
2360 n
= strtoull(pch
, &p
, 0);
2361 if (errno
== ERANGE
) {
2362 expr_error(mon
, "number too large");
2365 expr_error(mon
, "invalid char '%c' in expression", *p
);
2368 while (qemu_isspace(*pch
))
2376 static int64_t expr_prod(Monitor
*mon
)
2381 val
= expr_unary(mon
);
2384 if (op
!= '*' && op
!= '/' && op
!= '%')
2387 val2
= expr_unary(mon
);
2396 expr_error(mon
, "division by zero");
2407 static int64_t expr_logic(Monitor
*mon
)
2412 val
= expr_prod(mon
);
2415 if (op
!= '&' && op
!= '|' && op
!= '^')
2418 val2
= expr_prod(mon
);
2435 static int64_t expr_sum(Monitor
*mon
)
2440 val
= expr_logic(mon
);
2443 if (op
!= '+' && op
!= '-')
2446 val2
= expr_logic(mon
);
2455 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2458 if (sigsetjmp(expr_env
, 0)) {
2462 while (qemu_isspace(*pch
))
2464 *pval
= expr_sum(mon
);
2469 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2471 const char *p
= *pp
;
2475 d
= strtod(p
, &tailp
);
2477 monitor_printf(mon
, "Number expected\n");
2480 if (d
!= d
|| d
- d
!= 0) {
2481 /* NaN or infinity */
2482 monitor_printf(mon
, "Bad number\n");
2491 * Store the command-name in cmdname, and return a pointer to
2492 * the remaining of the command string.
2494 static const char *get_command_name(const char *cmdline
,
2495 char *cmdname
, size_t nlen
)
2498 const char *p
, *pstart
;
2501 while (qemu_isspace(*p
))
2506 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2511 memcpy(cmdname
, pstart
, len
);
2512 cmdname
[len
] = '\0';
2517 * Read key of 'type' into 'key' and return the current
2520 static char *key_get_info(const char *type
, char **key
)
2528 p
= strchr(type
, ':');
2535 str
= g_malloc(len
+ 1);
2536 memcpy(str
, type
, len
);
2543 static int default_fmt_format
= 'x';
2544 static int default_fmt_size
= 4;
2546 static int is_valid_option(const char *c
, const char *typestr
)
2554 typestr
= strstr(typestr
, option
);
2555 return (typestr
!= NULL
);
2558 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2559 const char *cmdname
)
2561 const mon_cmd_t
*cmd
;
2563 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2564 if (compare_cmd(cmdname
, cmd
->name
)) {
2572 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2574 return search_dispatch_table(qmp_cmds
, cmdname
);
2578 * Parse command name from @cmdp according to command table @table.
2579 * If blank, return NULL.
2580 * Else, if no valid command can be found, report to @mon, and return
2582 * Else, change @cmdp to point right behind the name, and return its
2583 * command table entry.
2584 * Do not assume the return value points into @table! It doesn't when
2585 * the command is found in a sub-command table.
2587 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2592 const mon_cmd_t
*cmd
;
2595 /* extract the command name */
2596 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2600 cmd
= search_dispatch_table(table
, cmdname
);
2602 monitor_printf(mon
, "unknown command: '%.*s'\n",
2603 (int)(p
- *cmdp
), *cmdp
);
2607 /* filter out following useless space */
2608 while (qemu_isspace(*p
)) {
2613 /* search sub command */
2614 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2615 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2622 * Parse arguments for @cmd.
2623 * If it can't be parsed, report to @mon, and return NULL.
2624 * Else, insert command arguments into a QDict, and return it.
2625 * Note: On success, caller has to free the QDict structure.
2628 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2630 const mon_cmd_t
*cmd
)
2632 const char *typestr
;
2635 const char *p
= *endp
;
2637 QDict
*qdict
= qdict_new();
2639 /* parse the parameters */
2640 typestr
= cmd
->args_type
;
2642 typestr
= key_get_info(typestr
, &key
);
2654 while (qemu_isspace(*p
))
2656 if (*typestr
== '?') {
2659 /* no optional string: NULL argument */
2663 ret
= get_str(buf
, sizeof(buf
), &p
);
2667 monitor_printf(mon
, "%s: filename expected\n",
2671 monitor_printf(mon
, "%s: block device name expected\n",
2675 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2680 qdict_put(qdict
, key
, qstring_from_str(buf
));
2685 QemuOptsList
*opts_list
;
2688 opts_list
= qemu_find_opts(key
);
2689 if (!opts_list
|| opts_list
->desc
->name
) {
2692 while (qemu_isspace(*p
)) {
2697 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2700 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2704 qemu_opts_to_qdict(opts
, qdict
);
2705 qemu_opts_del(opts
);
2710 int count
, format
, size
;
2712 while (qemu_isspace(*p
))
2718 if (qemu_isdigit(*p
)) {
2720 while (qemu_isdigit(*p
)) {
2721 count
= count
* 10 + (*p
- '0');
2759 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2760 monitor_printf(mon
, "invalid char in format: '%c'\n",
2765 format
= default_fmt_format
;
2766 if (format
!= 'i') {
2767 /* for 'i', not specifying a size gives -1 as size */
2769 size
= default_fmt_size
;
2770 default_fmt_size
= size
;
2772 default_fmt_format
= format
;
2775 format
= default_fmt_format
;
2776 if (format
!= 'i') {
2777 size
= default_fmt_size
;
2782 qdict_put(qdict
, "count", qint_from_int(count
));
2783 qdict_put(qdict
, "format", qint_from_int(format
));
2784 qdict_put(qdict
, "size", qint_from_int(size
));
2793 while (qemu_isspace(*p
))
2795 if (*typestr
== '?' || *typestr
== '.') {
2796 if (*typestr
== '?') {
2804 while (qemu_isspace(*p
))
2813 if (get_expr(mon
, &val
, &p
))
2815 /* Check if 'i' is greater than 32-bit */
2816 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2817 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2818 monitor_printf(mon
, "integer is for 32-bit values\n");
2820 } else if (c
== 'M') {
2822 monitor_printf(mon
, "enter a positive value\n");
2827 qdict_put(qdict
, key
, qint_from_int(val
));
2835 while (qemu_isspace(*p
)) {
2838 if (*typestr
== '?') {
2844 val
= qemu_strtosz(p
, &end
);
2846 monitor_printf(mon
, "invalid size\n");
2849 qdict_put(qdict
, key
, qint_from_int(val
));
2857 while (qemu_isspace(*p
))
2859 if (*typestr
== '?') {
2865 if (get_double(mon
, &val
, &p
) < 0) {
2868 if (p
[0] && p
[1] == 's') {
2871 val
/= 1e3
; p
+= 2; break;
2873 val
/= 1e6
; p
+= 2; break;
2875 val
/= 1e9
; p
+= 2; break;
2878 if (*p
&& !qemu_isspace(*p
)) {
2879 monitor_printf(mon
, "Unknown unit suffix\n");
2882 qdict_put(qdict
, key
, qfloat_from_double(val
));
2890 while (qemu_isspace(*p
)) {
2894 while (qemu_isgraph(*p
)) {
2897 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2899 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2902 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2905 qdict_put(qdict
, key
, qbool_from_bool(val
));
2910 const char *tmp
= p
;
2917 while (qemu_isspace(*p
))
2922 if(!is_valid_option(p
, typestr
)) {
2924 monitor_printf(mon
, "%s: unsupported option -%c\n",
2936 qdict_put(qdict
, key
, qbool_from_bool(true));
2943 /* package all remaining string */
2946 while (qemu_isspace(*p
)) {
2949 if (*typestr
== '?') {
2952 /* no remaining string: NULL argument */
2958 monitor_printf(mon
, "%s: string expected\n",
2962 qdict_put(qdict
, key
, qstring_from_str(p
));
2968 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2974 /* check that all arguments were parsed */
2975 while (qemu_isspace(*p
))
2978 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2991 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2994 const mon_cmd_t
*cmd
;
2996 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
3001 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
3003 monitor_printf(mon
, "Try \"help %s\" for more information\n",
3008 cmd
->mhandler
.cmd(mon
, qdict
);
3012 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3014 const char *p
, *pstart
;
3023 p
= pstart
+ strlen(pstart
);
3025 if (len
> sizeof(cmd
) - 2)
3026 len
= sizeof(cmd
) - 2;
3027 memcpy(cmd
, pstart
, len
);
3029 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3030 readline_add_completion(mon
->rs
, cmd
);
3038 static void file_completion(Monitor
*mon
, const char *input
)
3043 char file
[1024], file_prefix
[1024];
3047 p
= strrchr(input
, '/');
3050 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3051 pstrcpy(path
, sizeof(path
), ".");
3053 input_path_len
= p
- input
+ 1;
3054 memcpy(path
, input
, input_path_len
);
3055 if (input_path_len
> sizeof(path
) - 1)
3056 input_path_len
= sizeof(path
) - 1;
3057 path
[input_path_len
] = '\0';
3058 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3061 ffs
= opendir(path
);
3070 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3074 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3075 memcpy(file
, input
, input_path_len
);
3076 if (input_path_len
< sizeof(file
))
3077 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3079 /* stat the file to find out if it's a directory.
3080 * In that case add a slash to speed up typing long paths
3082 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3083 pstrcat(file
, sizeof(file
), "/");
3085 readline_add_completion(mon
->rs
, file
);
3091 static const char *next_arg_type(const char *typestr
)
3093 const char *p
= strchr(typestr
, ':');
3094 return (p
!= NULL
? ++p
: typestr
);
3097 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3100 if (!str
|| !option
) {
3103 if (!strncmp(option
, str
, strlen(str
))) {
3104 readline_add_completion(rs
, option
);
3108 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3111 ChardevBackendInfoList
*list
, *start
;
3117 readline_set_completion_index(rs
, len
);
3119 start
= list
= qmp_query_chardev_backends(NULL
);
3121 const char *chr_name
= list
->value
->name
;
3123 if (!strncmp(chr_name
, str
, len
)) {
3124 readline_add_completion(rs
, chr_name
);
3128 qapi_free_ChardevBackendInfoList(start
);
3131 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3140 readline_set_completion_index(rs
, len
);
3141 for (i
= 0; NetClientDriver_lookup
[i
]; i
++) {
3142 add_completion_option(rs
, str
, NetClientDriver_lookup
[i
]);
3146 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3156 readline_set_completion_index(rs
, len
);
3157 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3160 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3162 name
= object_class_get_name(OBJECT_CLASS(dc
));
3164 if (!dc
->cannot_instantiate_with_device_add_yet
3165 && !strncmp(name
, str
, len
)) {
3166 readline_add_completion(rs
, name
);
3173 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3183 readline_set_completion_index(rs
, len
);
3184 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3188 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3189 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3190 readline_add_completion(rs
, name
);
3197 static void peripheral_device_del_completion(ReadLineState
*rs
,
3198 const char *str
, size_t len
)
3200 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3201 GSList
*list
, *item
;
3203 list
= qdev_build_hotpluggable_device_list(peripheral
);
3208 for (item
= list
; item
; item
= g_slist_next(item
)) {
3209 DeviceState
*dev
= item
->data
;
3211 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3212 readline_add_completion(rs
, dev
->id
);
3219 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3222 ChardevInfoList
*list
, *start
;
3228 readline_set_completion_index(rs
, len
);
3230 start
= list
= qmp_query_chardev(NULL
);
3232 ChardevInfo
*chr
= list
->value
;
3234 if (!strncmp(chr
->label
, str
, len
)) {
3235 readline_add_completion(rs
, chr
->label
);
3239 qapi_free_ChardevInfoList(start
);
3242 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3245 ChardevInfoList
*list
, *start
;
3248 readline_set_completion_index(rs
, len
);
3250 start
= list
= qmp_query_chardev(NULL
);
3252 ChardevInfo
*chr_info
= list
->value
;
3254 if (!strncmp(chr_info
->label
, str
, len
)) {
3255 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3256 if (chr
&& chr_is_ringbuf(chr
)) {
3257 readline_add_completion(rs
, chr_info
->label
);
3262 qapi_free_ChardevInfoList(start
);
3265 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3270 ringbuf_completion(rs
, str
);
3273 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3282 readline_set_completion_index(rs
, len
);
3283 peripheral_device_del_completion(rs
, str
, len
);
3286 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3288 ObjectPropertyInfoList
*list
, *start
;
3295 readline_set_completion_index(rs
, len
);
3297 start
= list
= qmp_qom_list("/objects", NULL
);
3299 ObjectPropertyInfo
*info
= list
->value
;
3301 if (!strncmp(info
->type
, "child<", 5)
3302 && !strncmp(info
->name
, str
, len
)) {
3303 readline_add_completion(rs
, info
->name
);
3307 qapi_free_ObjectPropertyInfoList(start
);
3310 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3319 sep
= strrchr(str
, '-');
3324 readline_set_completion_index(rs
, len
);
3325 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3326 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3327 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3332 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3337 readline_set_completion_index(rs
, len
);
3339 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3341 count
= qemu_find_net_clients_except(NULL
, ncs
,
3342 NET_CLIENT_DRIVER_NONE
,
3344 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3345 const char *name
= ncs
[i
]->name
;
3346 if (!strncmp(str
, name
, len
)) {
3347 readline_add_completion(rs
, name
);
3350 } else if (nb_args
== 3) {
3351 add_completion_option(rs
, str
, "on");
3352 add_completion_option(rs
, str
, "off");
3356 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3359 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3366 readline_set_completion_index(rs
, len
);
3367 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3369 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3371 const char *name
= ncs
[i
]->name
;
3372 if (strncmp(str
, name
, len
)) {
3375 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3377 readline_add_completion(rs
, name
);
3382 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3387 readline_set_completion_index(rs
, len
);
3390 for (id
= 0; id
< trace_event_count(); id
++) {
3391 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3392 if (!strncmp(str
, event_name
, len
)) {
3393 readline_add_completion(rs
, event_name
);
3399 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3404 readline_set_completion_index(rs
, len
);
3407 for (id
= 0; id
< trace_event_count(); id
++) {
3408 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3409 if (!strncmp(str
, event_name
, len
)) {
3410 readline_add_completion(rs
, event_name
);
3413 } else if (nb_args
== 3) {
3414 add_completion_option(rs
, str
, "on");
3415 add_completion_option(rs
, str
, "off");
3419 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3426 readline_set_completion_index(rs
, strlen(str
));
3427 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3428 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3432 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3438 readline_set_completion_index(rs
, len
);
3441 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3442 const char *name
= MigrationCapability_lookup
[i
];
3443 if (!strncmp(str
, name
, len
)) {
3444 readline_add_completion(rs
, name
);
3447 } else if (nb_args
== 3) {
3448 add_completion_option(rs
, str
, "on");
3449 add_completion_option(rs
, str
, "off");
3453 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3459 readline_set_completion_index(rs
, len
);
3462 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3463 const char *name
= MigrationParameter_lookup
[i
];
3464 if (!strncmp(str
, name
, len
)) {
3465 readline_add_completion(rs
, name
);
3471 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3479 readline_set_completion_index(rs
, len
);
3480 for (i
= 0; host_net_devices
[i
]; i
++) {
3481 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3482 readline_add_completion(rs
, host_net_devices
[i
]);
3487 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3489 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3493 readline_set_completion_index(rs
, len
);
3495 count
= qemu_find_net_clients_except(NULL
, ncs
,
3496 NET_CLIENT_DRIVER_NONE
,
3498 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3502 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3505 snprintf(name
, sizeof(name
), "%d", id
);
3506 if (!strncmp(str
, name
, len
)) {
3507 readline_add_completion(rs
, name
);
3511 } else if (nb_args
== 3) {
3512 count
= qemu_find_net_clients_except(NULL
, ncs
,
3513 NET_CLIENT_DRIVER_NIC
,
3515 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3519 if (ncs
[i
]->info
->type
== NET_CLIENT_DRIVER_HUBPORT
||
3520 net_hub_id_for_client(ncs
[i
], &id
)) {
3523 name
= ncs
[i
]->name
;
3524 if (!strncmp(str
, name
, len
)) {
3525 readline_add_completion(rs
, name
);
3532 static void vm_completion(ReadLineState
*rs
, const char *str
)
3535 BlockDriverState
*bs
;
3536 BdrvNextIterator it
;
3539 readline_set_completion_index(rs
, len
);
3541 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3542 SnapshotInfoList
*snapshots
, *snapshot
;
3543 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3546 aio_context_acquire(ctx
);
3547 if (bdrv_can_snapshot(bs
)) {
3548 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3550 aio_context_release(ctx
);
3555 snapshot
= snapshots
;
3557 char *completion
= snapshot
->value
->name
;
3558 if (!strncmp(str
, completion
, len
)) {
3559 readline_add_completion(rs
, completion
);
3561 completion
= snapshot
->value
->id
;
3562 if (!strncmp(str
, completion
, len
)) {
3563 readline_add_completion(rs
, completion
);
3565 snapshot
= snapshot
->next
;
3567 qapi_free_SnapshotInfoList(snapshots
);
3572 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3575 vm_completion(rs
, str
);
3579 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3582 vm_completion(rs
, str
);
3586 static void monitor_find_completion_by_table(Monitor
*mon
,
3587 const mon_cmd_t
*cmd_table
,
3591 const char *cmdname
;
3593 const char *ptype
, *str
, *name
;
3594 const mon_cmd_t
*cmd
;
3595 BlockBackend
*blk
= NULL
;
3598 /* command completion */
3603 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3604 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3605 cmd_completion(mon
, cmdname
, cmd
->name
);
3608 /* find the command */
3609 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3610 if (compare_cmd(args
[0], cmd
->name
)) {
3618 if (cmd
->sub_table
) {
3619 /* do the job again */
3620 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3621 &args
[1], nb_args
- 1);
3624 if (cmd
->command_completion
) {
3625 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3629 ptype
= next_arg_type(cmd
->args_type
);
3630 for(i
= 0; i
< nb_args
- 2; i
++) {
3631 if (*ptype
!= '\0') {
3632 ptype
= next_arg_type(ptype
);
3633 while (*ptype
== '?')
3634 ptype
= next_arg_type(ptype
);
3637 str
= args
[nb_args
- 1];
3638 while (*ptype
== '-' && ptype
[1] != '\0') {
3639 ptype
= next_arg_type(ptype
);
3643 /* file completion */
3644 readline_set_completion_index(mon
->rs
, strlen(str
));
3645 file_completion(mon
, str
);
3648 /* block device name completion */
3649 readline_set_completion_index(mon
->rs
, strlen(str
));
3650 while ((blk
= blk_next(blk
)) != NULL
) {
3651 name
= blk_name(blk
);
3652 if (str
[0] == '\0' ||
3653 !strncmp(name
, str
, strlen(str
))) {
3654 readline_add_completion(mon
->rs
, name
);
3660 if (!strcmp(cmd
->name
, "help|?")) {
3661 monitor_find_completion_by_table(mon
, cmd_table
,
3662 &args
[1], nb_args
- 1);
3671 static void monitor_find_completion(void *opaque
,
3672 const char *cmdline
)
3674 Monitor
*mon
= opaque
;
3675 char *args
[MAX_ARGS
];
3678 /* 1. parse the cmdline */
3679 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3683 /* if the line ends with a space, it means we want to complete the
3685 len
= strlen(cmdline
);
3686 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3687 if (nb_args
>= MAX_ARGS
) {
3690 args
[nb_args
++] = g_strdup("");
3693 /* 2. auto complete according to args */
3694 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3697 free_cmdline_args(args
, nb_args
);
3700 static int monitor_can_read(void *opaque
)
3702 Monitor
*mon
= opaque
;
3704 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3707 static bool invalid_qmp_mode(const Monitor
*mon
, const char *cmd
,
3710 bool is_cap
= g_str_equal(cmd
, "qmp_capabilities");
3712 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3713 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3714 "Capabilities negotiation is already complete, command "
3715 "'%s' ignored", cmd
);
3718 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3719 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3720 "Expecting capabilities negotiation with "
3721 "'qmp_capabilities' before command '%s'", cmd
);
3728 * Argument validation rules:
3730 * 1. The argument must exist in cmd_args qdict
3731 * 2. The argument type must be the expected one
3733 * Special case: If the argument doesn't exist in cmd_args and
3734 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3735 * checking is skipped for it.
3737 static void check_client_args_type(const QDict
*client_args
,
3738 const QDict
*cmd_args
, int flags
,
3741 const QDictEntry
*ent
;
3743 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3746 const QObject
*client_arg
= qdict_entry_value(ent
);
3747 const char *client_arg_name
= qdict_entry_key(ent
);
3749 obj
= qdict_get(cmd_args
, client_arg_name
);
3751 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3752 /* handler accepts unknowns */
3755 /* client arg doesn't exist */
3756 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3760 arg_type
= qobject_to_qstring(obj
);
3761 assert(arg_type
!= NULL
);
3763 /* check if argument's type is correct */
3764 switch (qstring_get_str(arg_type
)[0]) {
3768 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3769 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3770 client_arg_name
, "string");
3778 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3779 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3780 client_arg_name
, "int");
3785 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3786 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3787 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3788 client_arg_name
, "number");
3794 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3795 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3796 client_arg_name
, "bool");
3801 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3804 /* Any QObject can be passed. */
3809 * These types are not supported by QMP and thus are not
3810 * handled here. Fall through.
3819 * - Check if the client has passed all mandatory args
3820 * - Set special flags for argument validation
3822 static void check_mandatory_args(const QDict
*cmd_args
,
3823 const QDict
*client_args
, int *flags
,
3826 const QDictEntry
*ent
;
3828 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3829 const char *cmd_arg_name
= qdict_entry_key(ent
);
3830 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3831 assert(type
!= NULL
);
3833 if (qstring_get_str(type
)[0] == 'O') {
3834 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3835 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3836 } else if (qstring_get_str(type
)[0] != '-' &&
3837 qstring_get_str(type
)[1] != '?' &&
3838 !qdict_haskey(client_args
, cmd_arg_name
)) {
3839 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3845 static QDict
*qdict_from_args_type(const char *args_type
)
3849 QString
*key
, *type
, *cur_qs
;
3851 assert(args_type
!= NULL
);
3853 qdict
= qdict_new();
3855 if (args_type
== NULL
|| args_type
[0] == '\0') {
3856 /* no args, empty qdict */
3860 key
= qstring_new();
3861 type
= qstring_new();
3866 switch (args_type
[i
]) {
3869 qdict_put(qdict
, qstring_get_str(key
), type
);
3871 if (args_type
[i
] == '\0') {
3874 type
= qstring_new(); /* qdict has ref */
3875 cur_qs
= key
= qstring_new();
3881 qstring_append_chr(cur_qs
, args_type
[i
]);
3891 * Client argument checking rules:
3893 * 1. Client must provide all mandatory arguments
3894 * 2. Each argument provided by the client must be expected
3895 * 3. Each argument provided by the client must have the type expected
3898 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3905 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3908 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3913 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3916 error_propagate(errp
, err
);
3921 * Input object checking rules
3923 * 1. Input object must be a dict
3924 * 2. The "execute" key must exist
3925 * 3. The "execute" key must be a string
3926 * 4. If the "arguments" key exists, it must be a dict
3927 * 5. If the "id" key exists, it can be anything (ie. json-value)
3928 * 6. Any argument not listed above is considered invalid
3930 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3932 const QDictEntry
*ent
;
3933 int has_exec_key
= 0;
3936 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3937 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3941 input_dict
= qobject_to_qdict(input_obj
);
3943 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3944 const char *arg_name
= qdict_entry_key(ent
);
3945 const QObject
*arg_obj
= qdict_entry_value(ent
);
3947 if (!strcmp(arg_name
, "execute")) {
3948 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3949 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3950 "execute", "string");
3954 } else if (!strcmp(arg_name
, "arguments")) {
3955 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3956 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3957 "arguments", "object");
3960 } else if (!strcmp(arg_name
, "id")) {
3961 /* Any string is acceptable as "id", so nothing to check */
3963 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3968 if (!has_exec_key
) {
3969 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3976 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3978 Error
*local_err
= NULL
;
3979 QObject
*obj
, *data
;
3980 QDict
*input
, *args
;
3981 const mon_cmd_t
*cmd
;
3983 const char *cmd_name
;
3984 Monitor
*mon
= cur_mon
;
3986 args
= input
= NULL
;
3989 obj
= json_parser_parse(tokens
, NULL
);
3991 // FIXME: should be triggered in json_parser_parse()
3992 error_setg(&local_err
, QERR_JSON_PARSING
);
3996 input
= qmp_check_input_obj(obj
, &local_err
);
3998 qobject_decref(obj
);
4002 mon
->qmp
.id
= qdict_get(input
, "id");
4003 qobject_incref(mon
->qmp
.id
);
4005 cmd_name
= qdict_get_str(input
, "execute");
4006 trace_handle_qmp_command(mon
, cmd_name
);
4007 cmd
= qmp_find_cmd(cmd_name
);
4008 qcmd
= qmp_find_command(cmd_name
);
4009 if (!qcmd
|| !cmd
) {
4010 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
4011 "The command %s has not been found", cmd_name
);
4014 if (invalid_qmp_mode(mon
, cmd_name
, &local_err
)) {
4018 obj
= qdict_get(input
, "arguments");
4022 args
= qobject_to_qdict(obj
);
4026 qmp_check_client_args(cmd
, args
, &local_err
);
4031 qcmd
->fn(args
, &data
, &local_err
);
4034 monitor_protocol_emitter(mon
, data
, local_err
);
4035 qobject_decref(data
);
4036 error_free(local_err
);
4041 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4043 Monitor
*old_mon
= cur_mon
;
4047 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
4052 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4054 Monitor
*old_mon
= cur_mon
;
4060 for (i
= 0; i
< size
; i
++)
4061 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4063 if (size
== 0 || buf
[size
- 1] != 0)
4064 monitor_printf(cur_mon
, "corrupted command\n");
4066 handle_hmp_command(cur_mon
, (char *)buf
);
4072 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4073 void *readline_opaque
)
4075 Monitor
*mon
= opaque
;
4077 monitor_suspend(mon
);
4078 handle_hmp_command(mon
, cmdline
);
4079 monitor_resume(mon
);
4082 int monitor_suspend(Monitor
*mon
)
4090 void monitor_resume(Monitor
*mon
)
4094 if (--mon
->suspend_cnt
== 0)
4095 readline_show_prompt(mon
->rs
);
4098 static QObject
*get_qmp_greeting(void)
4100 QObject
*ver
= NULL
;
4102 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4104 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
4108 static void monitor_qmp_event(void *opaque
, int event
)
4111 Monitor
*mon
= opaque
;
4114 case CHR_EVENT_OPENED
:
4115 mon
->qmp
.in_command_mode
= false;
4116 data
= get_qmp_greeting();
4117 monitor_json_emitter(mon
, data
);
4118 qobject_decref(data
);
4121 case CHR_EVENT_CLOSED
:
4122 json_message_parser_destroy(&mon
->qmp
.parser
);
4123 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4125 monitor_fdsets_cleanup();
4130 static void monitor_event(void *opaque
, int event
)
4132 Monitor
*mon
= opaque
;
4135 case CHR_EVENT_MUX_IN
:
4136 qemu_mutex_lock(&mon
->out_lock
);
4138 qemu_mutex_unlock(&mon
->out_lock
);
4139 if (mon
->reset_seen
) {
4140 readline_restart(mon
->rs
);
4141 monitor_resume(mon
);
4144 mon
->suspend_cnt
= 0;
4148 case CHR_EVENT_MUX_OUT
:
4149 if (mon
->reset_seen
) {
4150 if (mon
->suspend_cnt
== 0) {
4151 monitor_printf(mon
, "\n");
4154 monitor_suspend(mon
);
4158 qemu_mutex_lock(&mon
->out_lock
);
4160 qemu_mutex_unlock(&mon
->out_lock
);
4163 case CHR_EVENT_OPENED
:
4164 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4165 "information\n", QEMU_VERSION
);
4166 if (!mon
->mux_out
) {
4167 readline_restart(mon
->rs
);
4168 readline_show_prompt(mon
->rs
);
4170 mon
->reset_seen
= 1;
4174 case CHR_EVENT_CLOSED
:
4176 monitor_fdsets_cleanup();
4182 compare_mon_cmd(const void *a
, const void *b
)
4184 return strcmp(((const mon_cmd_t
*)a
)->name
,
4185 ((const mon_cmd_t
*)b
)->name
);
4188 static void sortcmdlist(void)
4191 int elem_size
= sizeof(mon_cmd_t
);
4193 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4194 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4196 array_num
= sizeof(info_cmds
)/elem_size
-1;
4197 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4200 /* These functions just adapt the readline interface in a typesafe way. We
4201 * could cast function pointers but that discards compiler checks.
4203 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4204 const char *fmt
, ...)
4208 monitor_vprintf(opaque
, fmt
, ap
);
4212 static void monitor_readline_flush(void *opaque
)
4214 monitor_flush(opaque
);
4217 static void __attribute__((constructor
)) monitor_lock_init(void)
4219 qemu_mutex_init(&monitor_lock
);
4222 void monitor_init(CharDriverState
*chr
, int flags
)
4224 static int is_first_init
= 1;
4227 if (is_first_init
) {
4228 monitor_qapi_event_init();
4233 mon
= g_malloc(sizeof(*mon
));
4234 monitor_data_init(mon
);
4238 if (flags
& MONITOR_USE_READLINE
) {
4239 mon
->rs
= readline_init(monitor_readline_printf
,
4240 monitor_readline_flush
,
4242 monitor_find_completion
);
4243 monitor_read_command(mon
, 0);
4246 if (monitor_is_qmp(mon
)) {
4247 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4248 monitor_qmp_event
, mon
);
4249 qemu_chr_fe_set_echo(chr
, true);
4250 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4252 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4253 monitor_event
, mon
);
4256 qemu_mutex_lock(&monitor_lock
);
4257 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4258 qemu_mutex_unlock(&monitor_lock
);
4261 void monitor_cleanup(void)
4263 Monitor
*mon
, *next
;
4265 qemu_mutex_lock(&monitor_lock
);
4266 QLIST_FOREACH_SAFE(mon
, &mon_list
, entry
, next
) {
4267 QLIST_REMOVE(mon
, entry
);
4268 monitor_data_destroy(mon
);
4271 qemu_mutex_unlock(&monitor_lock
);
4274 static void bdrv_password_cb(void *opaque
, const char *password
,
4275 void *readline_opaque
)
4277 Monitor
*mon
= opaque
;
4278 BlockDriverState
*bs
= readline_opaque
;
4280 Error
*local_err
= NULL
;
4282 bdrv_add_key(bs
, password
, &local_err
);
4284 error_report_err(local_err
);
4287 if (mon
->password_completion_cb
)
4288 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4290 monitor_read_command(mon
, 1);
4293 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4294 BlockCompletionFunc
*completion_cb
,
4299 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4300 bdrv_get_encrypted_filename(bs
));
4302 mon
->password_completion_cb
= completion_cb
;
4303 mon
->password_opaque
= opaque
;
4305 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4307 if (err
&& completion_cb
)
4308 completion_cb(opaque
, err
);
4313 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4314 BlockCompletionFunc
*completion_cb
,
4320 blk
= blk_by_name(device
);
4322 monitor_printf(mon
, "Device not found %s\n", device
);
4326 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4330 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4333 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4336 if (completion_cb
) {
4337 completion_cb(opaque
, 0);
4342 QemuOptsList qemu_mon_opts
= {
4344 .implied_opt_name
= "chardev",
4345 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4349 .type
= QEMU_OPT_STRING
,
4352 .type
= QEMU_OPT_STRING
,
4355 .type
= QEMU_OPT_BOOL
,
4358 .type
= QEMU_OPT_BOOL
,
4360 { /* end of list */ }
4365 void qmp_rtc_reset_reinjection(Error
**errp
)
4367 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4371 #ifndef TARGET_S390X
4372 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4374 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4379 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4381 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");
4386 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4388 MachineState
*ms
= MACHINE(qdev_get_machine());
4389 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4391 if (!mc
->query_hotpluggable_cpus
) {
4392 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4396 return mc
->query_hotpluggable_cpus(ms
);