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/config-file.h"
44 #include "qemu/readline.h"
45 #include "ui/console.h"
47 #include "sysemu/blockdev.h"
48 #include "sysemu/block-backend.h"
49 #include "audio/audio.h"
50 #include "disas/disas.h"
51 #include "sysemu/balloon.h"
52 #include "qemu/timer.h"
53 #include "migration/migration.h"
54 #include "sysemu/hw_accel.h"
56 #include "sysemu/tpm.h"
57 #include "qapi/qmp/qerror.h"
58 #include "qapi/qmp/types.h"
59 #include "qapi/qmp/qjson.h"
60 #include "qapi/qmp/json-streamer.h"
61 #include "qapi/qmp/json-parser.h"
62 #include "qom/object_interfaces.h"
63 #include "trace-root.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/qtest.h"
80 #include "sysemu/cpus.h"
81 #include "qemu/cutils.h"
82 #include "qapi/qmp/dispatch.h"
84 #if defined(TARGET_S390X)
85 #include "hw/s390x/storage-keys.h"
92 * 'B' block device name
93 * 's' string (accept optional quote)
94 * 'S' it just appends the rest of the string (accept optional quote)
95 * 'O' option string of the form NAME=VALUE,...
96 * parsed according to QemuOptsList given by its name
97 * Example: 'device:O' uses qemu_device_opts.
98 * Restriction: only lists with empty desc are supported
99 * TODO lift the restriction
101 * 'l' target long (32 or 64 bit)
102 * 'M' Non-negative target long (32 or 64 bit), in user mode the
103 * value is multiplied by 2^20 (think Mebibyte)
104 * 'o' octets (aka bytes)
105 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
106 * K, k suffix, which multiplies the value by 2^60 for suffixes E
107 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
108 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
110 * user mode accepts an optional ms, us, ns suffix,
111 * which divides the value by 1e3, 1e6, 1e9, respectively
112 * '/' optional gdb-like print format (like "/10x")
114 * '?' optional type (for all types, except '/')
115 * '.' other form of optional type (for 'i' and 'l')
117 * user mode accepts "on" or "off"
118 * '-' optional parameter (eg. '-f')
122 typedef struct mon_cmd_t
{
124 const char *args_type
;
127 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
128 /* @sub_table is a list of 2nd level of commands. If it does not exist,
129 * cmd should be used. If it exists, sub_table[?].cmd should be
130 * used, and cmd of 1st level plays the role of help function.
132 struct mon_cmd_t
*sub_table
;
133 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
136 /* file descriptors passed via SCM_RIGHTS */
137 typedef struct mon_fd_t mon_fd_t
;
141 QLIST_ENTRY(mon_fd_t
) next
;
144 /* file descriptor associated with a file descriptor set */
145 typedef struct MonFdsetFd MonFdsetFd
;
150 QLIST_ENTRY(MonFdsetFd
) next
;
153 /* file descriptor set containing fds passed via SCM_RIGHTS */
154 typedef struct MonFdset MonFdset
;
157 QLIST_HEAD(, MonFdsetFd
) fds
;
158 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
159 QLIST_ENTRY(MonFdset
) next
;
163 JSONMessageParser parser
;
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
169 QmpCommandList
*commands
;
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
177 typedef struct MonitorQAPIEventState
{
178 QAPIEvent event
; /* Throttling state for this event type and... */
179 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
180 QEMUTimer
*timer
; /* Timer for handling delayed events */
181 QDict
*qdict
; /* Delayed event (if any) */
182 } MonitorQAPIEventState
;
185 int64_t rate
; /* Minimum time (in ns) between two events */
186 } MonitorQAPIEventConf
;
199 /* Read under either BQL or out_lock, written with BQL+out_lock. */
205 BlockCompletionFunc
*password_completion_cb
;
206 void *password_opaque
;
207 mon_cmd_t
*cmd_table
;
208 QLIST_HEAD(,mon_fd_t
) fds
;
209 QLIST_ENTRY(Monitor
) entry
;
212 /* QMP checker flags */
213 #define QMP_ACCEPT_UNKNOWNS 1
215 /* Protects mon_list, monitor_event_state. */
216 static QemuMutex monitor_lock
;
218 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
219 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
220 static int mon_refcount
;
222 static mon_cmd_t mon_cmds
[];
223 static mon_cmd_t info_cmds
[];
225 QmpCommandList qmp_commands
, qmp_cap_negotiation_commands
;
229 static QEMUClockType event_clock_type
= QEMU_CLOCK_REALTIME
;
231 static void monitor_command_cb(void *opaque
, const char *cmdline
,
232 void *readline_opaque
);
235 * Is @mon a QMP monitor?
237 static inline bool monitor_is_qmp(const Monitor
*mon
)
239 return (mon
->flags
& MONITOR_USE_CONTROL
);
243 * Is the current monitor, if any, a QMP monitor?
245 bool monitor_cur_is_qmp(void)
247 return cur_mon
&& monitor_is_qmp(cur_mon
);
250 void monitor_read_command(Monitor
*mon
, int show_prompt
)
255 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
257 readline_show_prompt(mon
->rs
);
260 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
264 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
265 /* prompt is printed on return from the command handler */
268 monitor_printf(mon
, "terminal does not support password prompting\n");
273 static void monitor_flush_locked(Monitor
*mon
);
275 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
278 Monitor
*mon
= opaque
;
280 qemu_mutex_lock(&mon
->out_lock
);
282 monitor_flush_locked(mon
);
283 qemu_mutex_unlock(&mon
->out_lock
);
287 /* Called with mon->out_lock held. */
288 static void monitor_flush_locked(Monitor
*mon
)
294 if (mon
->skip_flush
) {
298 buf
= qstring_get_str(mon
->outbuf
);
299 len
= qstring_get_length(mon
->outbuf
);
301 if (len
&& !mon
->mux_out
) {
302 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
303 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
304 /* all flushed or error */
305 QDECREF(mon
->outbuf
);
306 mon
->outbuf
= qstring_new();
311 QString
*tmp
= qstring_from_str(buf
+ rc
);
312 QDECREF(mon
->outbuf
);
315 if (mon
->out_watch
== 0) {
317 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
318 monitor_unblocked
, mon
);
323 void monitor_flush(Monitor
*mon
)
325 qemu_mutex_lock(&mon
->out_lock
);
326 monitor_flush_locked(mon
);
327 qemu_mutex_unlock(&mon
->out_lock
);
330 /* flush at every end of line */
331 static void monitor_puts(Monitor
*mon
, const char *str
)
335 qemu_mutex_lock(&mon
->out_lock
);
341 qstring_append_chr(mon
->outbuf
, '\r');
343 qstring_append_chr(mon
->outbuf
, c
);
345 monitor_flush_locked(mon
);
348 qemu_mutex_unlock(&mon
->out_lock
);
351 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
358 if (monitor_is_qmp(mon
)) {
362 buf
= g_strdup_vprintf(fmt
, ap
);
363 monitor_puts(mon
, buf
);
367 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
371 monitor_vprintf(mon
, fmt
, ap
);
375 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
379 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
384 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
388 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
389 qobject_to_json(data
);
390 assert(json
!= NULL
);
392 qstring_append_chr(json
, '\n');
393 monitor_puts(mon
, qstring_get_str(json
));
398 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
399 /* Limit guest-triggerable events to 1 per second */
400 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
401 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
402 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
403 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
404 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
405 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
408 GHashTable
*monitor_qapi_event_state
;
411 * Emits the event to every monitor instance, @event is only used for trace
412 * Called with monitor_lock held.
414 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
418 trace_monitor_protocol_event_emit(event
, qdict
);
419 QLIST_FOREACH(mon
, &mon_list
, entry
) {
420 if (monitor_is_qmp(mon
)
421 && mon
->qmp
.commands
!= &qmp_cap_negotiation_commands
) {
422 monitor_json_emitter(mon
, QOBJECT(qdict
));
427 static void monitor_qapi_event_handler(void *opaque
);
430 * Queue a new event for emission to Monitor instances,
431 * applying any rate limiting if required.
434 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
436 MonitorQAPIEventConf
*evconf
;
437 MonitorQAPIEventState
*evstate
;
439 assert(event
< QAPI_EVENT__MAX
);
440 evconf
= &monitor_qapi_event_conf
[event
];
441 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
443 qemu_mutex_lock(&monitor_lock
);
446 /* Unthrottled event */
447 monitor_qapi_event_emit(event
, qdict
);
449 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
450 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
452 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
453 assert(!evstate
|| timer_pending(evstate
->timer
));
457 * Timer is pending for (at least) evconf->rate ns after
458 * last send. Store event for sending when timer fires,
459 * replacing a prior stored event if any.
461 QDECREF(evstate
->qdict
);
462 evstate
->qdict
= qdict
;
463 QINCREF(evstate
->qdict
);
466 * Last send was (at least) evconf->rate ns ago.
467 * Send immediately, and arm the timer to call
468 * monitor_qapi_event_handler() in evconf->rate ns. Any
469 * events arriving before then will be delayed until then.
471 int64_t now
= qemu_clock_get_ns(event_clock_type
);
473 monitor_qapi_event_emit(event
, qdict
);
475 evstate
= g_new(MonitorQAPIEventState
, 1);
476 evstate
->event
= event
;
477 evstate
->data
= data
;
478 QINCREF(evstate
->data
);
479 evstate
->qdict
= NULL
;
480 evstate
->timer
= timer_new_ns(event_clock_type
,
481 monitor_qapi_event_handler
,
483 g_hash_table_add(monitor_qapi_event_state
, evstate
);
484 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
488 qemu_mutex_unlock(&monitor_lock
);
492 * This function runs evconf->rate ns after sending a throttled
494 * If another event has since been stored, send it.
496 static void monitor_qapi_event_handler(void *opaque
)
498 MonitorQAPIEventState
*evstate
= opaque
;
499 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
501 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
502 qemu_mutex_lock(&monitor_lock
);
504 if (evstate
->qdict
) {
505 int64_t now
= qemu_clock_get_ns(event_clock_type
);
507 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
508 QDECREF(evstate
->qdict
);
509 evstate
->qdict
= NULL
;
510 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
512 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
513 QDECREF(evstate
->data
);
514 timer_free(evstate
->timer
);
518 qemu_mutex_unlock(&monitor_lock
);
521 static unsigned int qapi_event_throttle_hash(const void *key
)
523 const MonitorQAPIEventState
*evstate
= key
;
524 unsigned int hash
= evstate
->event
* 255;
526 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
527 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
530 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
531 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
537 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
539 const MonitorQAPIEventState
*eva
= a
;
540 const MonitorQAPIEventState
*evb
= b
;
542 if (eva
->event
!= evb
->event
) {
546 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
547 return !strcmp(qdict_get_str(eva
->data
, "id"),
548 qdict_get_str(evb
->data
, "id"));
551 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
552 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
553 qdict_get_str(evb
->data
, "node-name"));
559 static void monitor_qapi_event_init(void)
561 if (qtest_enabled()) {
562 event_clock_type
= QEMU_CLOCK_VIRTUAL
;
565 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
566 qapi_event_throttle_equal
);
567 qmp_event_set_func_emit(monitor_qapi_event_queue
);
570 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
572 static void monitor_data_init(Monitor
*mon
)
574 memset(mon
, 0, sizeof(Monitor
));
575 qemu_mutex_init(&mon
->out_lock
);
576 mon
->outbuf
= qstring_new();
577 /* Use *mon_cmds by default. */
578 mon
->cmd_table
= mon_cmds
;
581 static void monitor_data_destroy(Monitor
*mon
)
583 qemu_chr_fe_deinit(&mon
->chr
);
584 if (monitor_is_qmp(mon
)) {
585 json_message_parser_destroy(&mon
->qmp
.parser
);
588 QDECREF(mon
->outbuf
);
589 qemu_mutex_destroy(&mon
->out_lock
);
592 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
593 int64_t cpu_index
, Error
**errp
)
596 Monitor
*old_mon
, hmp
;
598 monitor_data_init(&hmp
);
599 hmp
.skip_flush
= true;
605 int ret
= monitor_set_cpu(cpu_index
);
608 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
614 handle_hmp_command(&hmp
, command_line
);
617 qemu_mutex_lock(&hmp
.out_lock
);
618 if (qstring_get_length(hmp
.outbuf
) > 0) {
619 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
621 output
= g_strdup("");
623 qemu_mutex_unlock(&hmp
.out_lock
);
626 monitor_data_destroy(&hmp
);
630 static int compare_cmd(const char *name
, const char *list
)
632 const char *p
, *pstart
;
640 p
= pstart
+ strlen(pstart
);
641 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
650 static int get_str(char *buf
, int buf_size
, const char **pp
)
658 while (qemu_isspace(*p
)) {
669 while (*p
!= '\0' && *p
!= '\"') {
685 printf("unsupported escape code: '\\%c'\n", c
);
688 if ((q
- buf
) < buf_size
- 1) {
692 if ((q
- buf
) < buf_size
- 1) {
699 printf("unterminated string\n");
704 while (*p
!= '\0' && !qemu_isspace(*p
)) {
705 if ((q
- buf
) < buf_size
- 1) {
718 static void free_cmdline_args(char **args
, int nb_args
)
722 assert(nb_args
<= MAX_ARGS
);
724 for (i
= 0; i
< nb_args
; i
++) {
731 * Parse the command line to get valid args.
732 * @cmdline: command line to be parsed.
733 * @pnb_args: location to store the number of args, must NOT be NULL.
734 * @args: location to store the args, which should be freed by caller, must
737 * Returns 0 on success, negative on failure.
739 * NOTE: this parser is an approximate form of the real command parser. Number
740 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
741 * return with failure.
743 static int parse_cmdline(const char *cmdline
,
744 int *pnb_args
, char **args
)
753 while (qemu_isspace(*p
)) {
759 if (nb_args
>= MAX_ARGS
) {
762 ret
= get_str(buf
, sizeof(buf
), &p
);
766 args
[nb_args
] = g_strdup(buf
);
773 free_cmdline_args(args
, nb_args
);
777 static void help_cmd_dump_one(Monitor
*mon
,
778 const mon_cmd_t
*cmd
,
784 for (i
= 0; i
< prefix_args_nb
; i
++) {
785 monitor_printf(mon
, "%s ", prefix_args
[i
]);
787 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
790 /* @args[@arg_index] is the valid command need to find in @cmds */
791 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
792 char **args
, int nb_args
, int arg_index
)
794 const mon_cmd_t
*cmd
;
796 /* No valid arg need to compare with, dump all in *cmds */
797 if (arg_index
>= nb_args
) {
798 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
799 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
804 /* Find one entry to dump */
805 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
806 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
807 if (cmd
->sub_table
) {
808 /* continue with next arg */
809 help_cmd_dump(mon
, cmd
->sub_table
,
810 args
, nb_args
, arg_index
+ 1);
812 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
819 static void help_cmd(Monitor
*mon
, const char *name
)
821 char *args
[MAX_ARGS
];
824 /* 1. parse user input */
826 /* special case for log, directly dump and return */
827 if (!strcmp(name
, "log")) {
828 const QEMULogItem
*item
;
829 monitor_printf(mon
, "Log items (comma separated):\n");
830 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
831 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
832 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
837 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
842 /* 2. dump the contents according to parsed args */
843 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
845 free_cmdline_args(args
, nb_args
);
848 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
850 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
853 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
855 const char *tp_name
= qdict_get_str(qdict
, "name");
856 bool new_state
= qdict_get_bool(qdict
, "option");
857 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
858 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
859 Error
*local_err
= NULL
;
862 monitor_printf(mon
, "argument vcpu must be positive");
866 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
868 error_report_err(local_err
);
872 #ifdef CONFIG_TRACE_SIMPLE
873 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
875 const char *op
= qdict_get_try_str(qdict
, "op");
876 const char *arg
= qdict_get_try_str(qdict
, "arg");
879 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
880 } else if (!strcmp(op
, "on")) {
881 st_set_trace_file_enabled(true);
882 } else if (!strcmp(op
, "off")) {
883 st_set_trace_file_enabled(false);
884 } else if (!strcmp(op
, "flush")) {
885 st_flush_trace_buffer();
886 } else if (!strcmp(op
, "set")) {
888 st_set_trace_file(arg
);
891 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
892 help_cmd(mon
, "trace-file");
897 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
899 help_cmd(mon
, "info");
902 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
904 CommandInfoList
*info
, **list
= opaque
;
910 info
= g_malloc0(sizeof(*info
));
911 info
->value
= g_malloc0(sizeof(*info
->value
));
912 info
->value
->name
= g_strdup(cmd
->name
);
917 CommandInfoList
*qmp_query_commands(Error
**errp
)
919 CommandInfoList
*list
= NULL
;
921 qmp_for_each_command(cur_mon
->qmp
.commands
, query_commands_cb
, &list
);
926 EventInfoList
*qmp_query_events(Error
**errp
)
928 EventInfoList
*info
, *ev_list
= NULL
;
931 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
932 const char *event_name
= QAPIEvent_lookup
[e
];
933 assert(event_name
!= NULL
);
934 info
= g_malloc0(sizeof(*info
));
935 info
->value
= g_malloc0(sizeof(*info
->value
));
936 info
->value
->name
= g_strdup(event_name
);
938 info
->next
= ev_list
;
946 * Minor hack: generated marshalling suppressed for this command
947 * ('gen': false in the schema) so we can parse the JSON string
948 * directly into QObject instead of first parsing it with
949 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
950 * to QObject with generated output marshallers, every time. Instead,
951 * we do it in test-qobject-input-visitor.c, just to make sure
952 * qapi-introspect.py's output actually conforms to the schema.
954 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
957 *ret_data
= qobject_from_json(qmp_schema_json
, &error_abort
);
961 * We used to define commands in qmp-commands.hx in addition to the
962 * QAPI schema. This permitted defining some of them only in certain
963 * configurations. query-commands has always reflected that (good,
964 * because it lets QMP clients figure out what's actually available),
965 * while query-qmp-schema never did (not so good). This function is a
966 * hack to keep the configuration-specific commands defined exactly as
967 * before, even though qmp-commands.hx is gone.
969 * FIXME Educate the QAPI schema on configuration-specific commands,
970 * and drop this hack.
972 static void qmp_unregister_commands_hack(void)
975 qmp_unregister_command(&qmp_commands
, "query-spice");
977 #ifndef CONFIG_REPLICATION
978 qmp_unregister_command(&qmp_commands
, "xen-set-replication");
979 qmp_unregister_command(&qmp_commands
, "query-xen-replication-status");
980 qmp_unregister_command(&qmp_commands
, "xen-colo-do-checkpoint");
983 qmp_unregister_command(&qmp_commands
, "rtc-reset-reinjection");
986 qmp_unregister_command(&qmp_commands
, "dump-skeys");
989 qmp_unregister_command(&qmp_commands
, "query-gic-capabilities");
991 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
992 qmp_unregister_command(&qmp_commands
, "query-cpu-model-expansion");
994 #if !defined(TARGET_S390X)
995 qmp_unregister_command(&qmp_commands
, "query-cpu-model-baseline");
996 qmp_unregister_command(&qmp_commands
, "query-cpu-model-comparison");
998 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
999 && !defined(TARGET_S390X)
1000 qmp_unregister_command(&qmp_commands
, "query-cpu-definitions");
1004 void monitor_init_qmp_commands(void)
1007 * Two command lists:
1008 * - qmp_commands contains all QMP commands
1009 * - qmp_cap_negotiation_commands contains just
1010 * "qmp_capabilities", to enforce capability negotiation
1013 qmp_init_marshal(&qmp_commands
);
1015 qmp_register_command(&qmp_commands
, "query-qmp-schema",
1016 qmp_query_qmp_schema
,
1018 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
1020 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
1023 qmp_unregister_commands_hack();
1025 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
1026 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
1027 qmp_marshal_qmp_capabilities
, QCO_NO_OPTIONS
);
1030 void qmp_qmp_capabilities(Error
**errp
)
1032 if (cur_mon
->qmp
.commands
== &qmp_commands
) {
1033 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
1034 "Capabilities negotiation is already complete, command "
1039 cur_mon
->qmp
.commands
= &qmp_commands
;
1042 /* set the current CPU defined by the user */
1043 int monitor_set_cpu(int cpu_index
)
1047 cpu
= qemu_get_cpu(cpu_index
);
1051 cur_mon
->mon_cpu
= cpu
;
1055 CPUState
*mon_get_cpu(void)
1057 if (!cur_mon
->mon_cpu
) {
1061 monitor_set_cpu(first_cpu
->cpu_index
);
1063 cpu_synchronize_state(cur_mon
->mon_cpu
);
1064 return cur_mon
->mon_cpu
;
1067 CPUArchState
*mon_get_cpu_env(void)
1069 CPUState
*cs
= mon_get_cpu();
1071 return cs
? cs
->env_ptr
: NULL
;
1074 int monitor_get_cpu_index(void)
1076 CPUState
*cs
= mon_get_cpu();
1078 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
1081 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1083 CPUState
*cs
= mon_get_cpu();
1086 monitor_printf(mon
, "No CPU available\n");
1089 cpu_dump_state(cs
, (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1092 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1094 if (!tcg_enabled()) {
1095 error_report("JIT information is only available with accel=tcg");
1099 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1100 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1103 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1105 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1108 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1117 str
= readline_get_history(mon
->rs
, i
);
1120 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1125 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1127 CPUState
*cs
= mon_get_cpu();
1130 monitor_printf(mon
, "No CPU available\n");
1133 cpu_dump_statistics(cs
, (FILE *)mon
, &monitor_fprintf
, 0);
1136 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1138 const char *name
= qdict_get_try_str(qdict
, "name");
1139 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1140 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1141 TraceEventInfoList
*events
;
1142 TraceEventInfoList
*elem
;
1143 Error
*local_err
= NULL
;
1149 monitor_printf(mon
, "argument vcpu must be positive");
1153 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1155 error_report_err(local_err
);
1159 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1160 monitor_printf(mon
, "%s : state %u\n",
1162 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1164 qapi_free_TraceEventInfoList(events
);
1167 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1168 bool has_port
, int64_t port
,
1169 bool has_tls_port
, int64_t tls_port
,
1170 bool has_cert_subject
, const char *cert_subject
,
1173 if (strcmp(protocol
, "spice") == 0) {
1174 if (!qemu_using_spice(errp
)) {
1178 if (!has_port
&& !has_tls_port
) {
1179 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1183 if (qemu_spice_migrate_info(hostname
,
1184 has_port
? port
: -1,
1185 has_tls_port
? tls_port
: -1,
1187 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1193 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1196 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1200 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1202 error_report_err(err
);
1206 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1209 const char *items
= qdict_get_str(qdict
, "items");
1211 if (!strcmp(items
, "none")) {
1214 mask
= qemu_str_to_log_mask(items
);
1216 help_cmd(mon
, "log");
1223 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1225 const char *option
= qdict_get_try_str(qdict
, "option");
1226 if (!option
|| !strcmp(option
, "on")) {
1228 } else if (!strcmp(option
, "off")) {
1231 monitor_printf(mon
, "unexpected option %s\n", option
);
1235 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1237 const char *device
= qdict_get_try_str(qdict
, "device");
1239 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1240 if (gdbserver_start(device
) < 0) {
1241 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1243 } else if (strcmp(device
, "none") == 0) {
1244 monitor_printf(mon
, "Disabled gdbserver\n");
1246 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1251 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1253 const char *action
= qdict_get_str(qdict
, "action");
1254 if (select_watchdog_action(action
) == -1) {
1255 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1259 static void monitor_printc(Monitor
*mon
, int c
)
1261 monitor_printf(mon
, "'");
1264 monitor_printf(mon
, "\\'");
1267 monitor_printf(mon
, "\\\\");
1270 monitor_printf(mon
, "\\n");
1273 monitor_printf(mon
, "\\r");
1276 if (c
>= 32 && c
<= 126) {
1277 monitor_printf(mon
, "%c", c
);
1279 monitor_printf(mon
, "\\x%02x", c
);
1283 monitor_printf(mon
, "'");
1286 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1287 hwaddr addr
, int is_physical
)
1289 int l
, line_size
, i
, max_digits
, len
;
1292 CPUState
*cs
= mon_get_cpu();
1294 if (!cs
&& (format
== 'i' || !is_physical
)) {
1295 monitor_printf(mon
, "Can not dump without CPU\n");
1299 if (format
== 'i') {
1302 CPUArchState
*env
= mon_get_cpu_env();
1305 } else if (wsize
== 4) {
1308 /* as default we use the current CS size */
1311 #ifdef TARGET_X86_64
1312 if ((env
->efer
& MSR_EFER_LMA
) &&
1313 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1317 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1323 CPUArchState
*env
= mon_get_cpu_env();
1324 flags
= msr_le
<< 16;
1325 flags
|= env
->bfd_mach
;
1327 monitor_disas(mon
, cs
, addr
, count
, is_physical
, flags
);
1331 len
= wsize
* count
;
1340 max_digits
= (wsize
* 8 + 2) / 3;
1344 max_digits
= (wsize
* 8) / 4;
1348 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1357 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1359 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1364 cpu_physical_memory_read(addr
, buf
, l
);
1366 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
1367 monitor_printf(mon
, " Cannot access memory\n");
1376 v
= ldub_p(buf
+ i
);
1379 v
= lduw_p(buf
+ i
);
1382 v
= (uint32_t)ldl_p(buf
+ i
);
1388 monitor_printf(mon
, " ");
1391 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1394 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1397 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1400 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1403 monitor_printc(mon
, v
);
1408 monitor_printf(mon
, "\n");
1414 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1416 int count
= qdict_get_int(qdict
, "count");
1417 int format
= qdict_get_int(qdict
, "format");
1418 int size
= qdict_get_int(qdict
, "size");
1419 target_long addr
= qdict_get_int(qdict
, "addr");
1421 memory_dump(mon
, count
, format
, size
, addr
, 0);
1424 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1426 int count
= qdict_get_int(qdict
, "count");
1427 int format
= qdict_get_int(qdict
, "format");
1428 int size
= qdict_get_int(qdict
, "size");
1429 hwaddr addr
= qdict_get_int(qdict
, "addr");
1431 memory_dump(mon
, count
, format
, size
, addr
, 1);
1434 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
1436 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
1440 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
1444 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
1445 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
1446 memory_region_unref(mrs
.mr
);
1451 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
1454 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
1456 hwaddr addr
= qdict_get_int(qdict
, "addr");
1457 Error
*local_err
= NULL
;
1458 MemoryRegion
*mr
= NULL
;
1461 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1463 error_report_err(local_err
);
1467 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
1469 addr
, mr
->name
, ptr
);
1471 memory_region_unref(mr
);
1475 static uint64_t vtop(void *ptr
, Error
**errp
)
1479 uintptr_t addr
= (uintptr_t) ptr
;
1480 uintptr_t pagesize
= getpagesize();
1481 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
1484 fd
= open("/proc/self/pagemap", O_RDONLY
);
1486 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
1490 /* Force copy-on-write if necessary. */
1491 atomic_add((uint8_t *)ptr
, 0);
1493 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
1494 error_setg_errno(errp
, errno
, "Cannot read pagemap");
1497 if ((pinfo
& (1ull << 63)) == 0) {
1498 error_setg(errp
, "Page not present");
1501 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
1508 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
1510 hwaddr addr
= qdict_get_int(qdict
, "addr");
1511 Error
*local_err
= NULL
;
1512 MemoryRegion
*mr
= NULL
;
1516 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1518 error_report_err(local_err
);
1522 physaddr
= vtop(ptr
, &local_err
);
1524 error_report_err(local_err
);
1526 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
1527 " (%s) is 0x%" PRIx64
"\n",
1528 addr
, mr
->name
, (uint64_t) physaddr
);
1531 memory_region_unref(mr
);
1535 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1537 int format
= qdict_get_int(qdict
, "format");
1538 hwaddr val
= qdict_get_int(qdict
, "val");
1542 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1545 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1548 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1552 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1555 monitor_printc(mon
, val
);
1558 monitor_printf(mon
, "\n");
1561 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1565 uint32_t start
= qdict_get_int(qdict
, "start");
1566 uint32_t size
= qdict_get_int(qdict
, "size");
1569 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1570 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1571 MEMTXATTRS_UNSPECIFIED
, NULL
);
1572 /* BSD sum algorithm ('sum' Unix command) */
1573 sum
= (sum
>> 1) | (sum
<< 15);
1576 monitor_printf(mon
, "%05d\n", sum
);
1579 static int mouse_button_state
;
1581 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1583 int dx
, dy
, dz
, button
;
1584 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1585 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1586 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1588 dx
= strtol(dx_str
, NULL
, 0);
1589 dy
= strtol(dy_str
, NULL
, 0);
1590 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1591 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1594 dz
= strtol(dz_str
, NULL
, 0);
1596 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1597 qemu_input_queue_btn(NULL
, button
, true);
1598 qemu_input_event_sync();
1599 qemu_input_queue_btn(NULL
, button
, false);
1602 qemu_input_event_sync();
1605 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1607 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1608 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1609 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1610 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1612 int button_state
= qdict_get_int(qdict
, "button_state");
1614 if (mouse_button_state
== button_state
) {
1617 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1618 qemu_input_event_sync();
1619 mouse_button_state
= button_state
;
1622 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1624 int size
= qdict_get_int(qdict
, "size");
1625 int addr
= qdict_get_int(qdict
, "addr");
1626 int has_index
= qdict_haskey(qdict
, "index");
1631 int index
= qdict_get_int(qdict
, "index");
1632 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1640 val
= cpu_inb(addr
);
1644 val
= cpu_inw(addr
);
1648 val
= cpu_inl(addr
);
1652 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1653 suffix
, addr
, size
* 2, val
);
1656 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1658 int size
= qdict_get_int(qdict
, "size");
1659 int addr
= qdict_get_int(qdict
, "addr");
1660 int val
= qdict_get_int(qdict
, "val");
1662 addr
&= IOPORTS_MASK
;
1667 cpu_outb(addr
, val
);
1670 cpu_outw(addr
, val
);
1673 cpu_outl(addr
, val
);
1678 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1680 Error
*local_err
= NULL
;
1681 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1683 qemu_boot_set(bootdevice
, &local_err
);
1685 error_report_err(local_err
);
1687 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1691 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1693 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1695 mtree_info((fprintf_function
)monitor_printf
, mon
, flatview
);
1698 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1704 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1705 query_numa_node_mem(node_mem
);
1706 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1707 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1708 monitor_printf(mon
, "node %d cpus:", i
);
1710 if (cpu
->numa_node
== i
) {
1711 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1714 monitor_printf(mon
, "\n");
1715 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1721 #ifdef CONFIG_PROFILER
1726 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1728 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1729 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1730 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1731 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1736 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1738 monitor_printf(mon
, "Internal profiler not compiled\n");
1742 /* Capture support */
1743 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1745 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1750 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1751 monitor_printf(mon
, "[%d]: ", i
);
1752 s
->ops
.info (s
->opaque
);
1756 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1759 int n
= qdict_get_int(qdict
, "n");
1762 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1764 s
->ops
.destroy (s
->opaque
);
1765 QLIST_REMOVE (s
, entries
);
1772 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1774 const char *path
= qdict_get_str(qdict
, "path");
1775 int has_freq
= qdict_haskey(qdict
, "freq");
1776 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1777 int has_bits
= qdict_haskey(qdict
, "bits");
1778 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1779 int has_channels
= qdict_haskey(qdict
, "nchannels");
1780 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1783 s
= g_malloc0 (sizeof (*s
));
1785 freq
= has_freq
? freq
: 44100;
1786 bits
= has_bits
? bits
: 16;
1787 nchannels
= has_channels
? nchannels
: 2;
1789 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1790 monitor_printf(mon
, "Failed to add wave capture\n");
1794 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1797 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1799 qemu_acl
*acl
= qemu_acl_find(name
);
1802 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1807 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1809 const char *aclname
= qdict_get_str(qdict
, "aclname");
1810 qemu_acl
*acl
= find_acl(mon
, aclname
);
1811 qemu_acl_entry
*entry
;
1815 monitor_printf(mon
, "policy: %s\n",
1816 acl
->defaultDeny
? "deny" : "allow");
1817 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1819 monitor_printf(mon
, "%d: %s %s\n", i
,
1820 entry
->deny
? "deny" : "allow", entry
->match
);
1825 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1827 const char *aclname
= qdict_get_str(qdict
, "aclname");
1828 qemu_acl
*acl
= find_acl(mon
, aclname
);
1831 qemu_acl_reset(acl
);
1832 monitor_printf(mon
, "acl: removed all rules\n");
1836 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1838 const char *aclname
= qdict_get_str(qdict
, "aclname");
1839 const char *policy
= qdict_get_str(qdict
, "policy");
1840 qemu_acl
*acl
= find_acl(mon
, aclname
);
1843 if (strcmp(policy
, "allow") == 0) {
1844 acl
->defaultDeny
= 0;
1845 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1846 } else if (strcmp(policy
, "deny") == 0) {
1847 acl
->defaultDeny
= 1;
1848 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1850 monitor_printf(mon
, "acl: unknown policy '%s', "
1851 "expected 'deny' or 'allow'\n", policy
);
1856 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1858 const char *aclname
= qdict_get_str(qdict
, "aclname");
1859 const char *match
= qdict_get_str(qdict
, "match");
1860 const char *policy
= qdict_get_str(qdict
, "policy");
1861 int has_index
= qdict_haskey(qdict
, "index");
1862 int index
= qdict_get_try_int(qdict
, "index", -1);
1863 qemu_acl
*acl
= find_acl(mon
, aclname
);
1867 if (strcmp(policy
, "allow") == 0) {
1869 } else if (strcmp(policy
, "deny") == 0) {
1872 monitor_printf(mon
, "acl: unknown policy '%s', "
1873 "expected 'deny' or 'allow'\n", policy
);
1877 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1879 ret
= qemu_acl_append(acl
, deny
, match
);
1881 monitor_printf(mon
, "acl: unable to add acl entry\n");
1883 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1887 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1889 const char *aclname
= qdict_get_str(qdict
, "aclname");
1890 const char *match
= qdict_get_str(qdict
, "match");
1891 qemu_acl
*acl
= find_acl(mon
, aclname
);
1895 ret
= qemu_acl_remove(acl
, match
);
1897 monitor_printf(mon
, "acl: no matching acl entry\n");
1899 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1903 void qmp_getfd(const char *fdname
, Error
**errp
)
1908 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
1910 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1914 if (qemu_isdigit(fdname
[0])) {
1916 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1917 "a name not starting with a digit");
1921 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1922 if (strcmp(monfd
->name
, fdname
) != 0) {
1931 monfd
= g_malloc0(sizeof(mon_fd_t
));
1932 monfd
->name
= g_strdup(fdname
);
1935 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1938 void qmp_closefd(const char *fdname
, Error
**errp
)
1942 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1943 if (strcmp(monfd
->name
, fdname
) != 0) {
1947 QLIST_REMOVE(monfd
, next
);
1949 g_free(monfd
->name
);
1954 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1957 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1959 int saved_vm_running
= runstate_is_running();
1960 const char *name
= qdict_get_str(qdict
, "name");
1962 vm_stop(RUN_STATE_RESTORE_VM
);
1964 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1969 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1973 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1976 if (strcmp(monfd
->name
, fdname
) != 0) {
1982 /* caller takes ownership of fd */
1983 QLIST_REMOVE(monfd
, next
);
1984 g_free(monfd
->name
);
1990 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1994 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1996 MonFdsetFd
*mon_fdset_fd
;
1997 MonFdsetFd
*mon_fdset_fd_next
;
1999 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2000 if ((mon_fdset_fd
->removed
||
2001 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2002 runstate_is_running()) {
2003 close(mon_fdset_fd
->fd
);
2004 g_free(mon_fdset_fd
->opaque
);
2005 QLIST_REMOVE(mon_fdset_fd
, next
);
2006 g_free(mon_fdset_fd
);
2010 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2011 QLIST_REMOVE(mon_fdset
, next
);
2016 static void monitor_fdsets_cleanup(void)
2018 MonFdset
*mon_fdset
;
2019 MonFdset
*mon_fdset_next
;
2021 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2022 monitor_fdset_cleanup(mon_fdset
);
2026 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2027 const char *opaque
, Error
**errp
)
2030 Monitor
*mon
= cur_mon
;
2033 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
2035 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2039 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2040 has_opaque
, opaque
, errp
);
2052 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2054 MonFdset
*mon_fdset
;
2055 MonFdsetFd
*mon_fdset_fd
;
2058 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2059 if (mon_fdset
->id
!= fdset_id
) {
2062 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2064 if (mon_fdset_fd
->fd
!= fd
) {
2067 mon_fdset_fd
->removed
= true;
2070 mon_fdset_fd
->removed
= true;
2073 if (has_fd
&& !mon_fdset_fd
) {
2076 monitor_fdset_cleanup(mon_fdset
);
2082 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2085 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2087 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2090 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2092 MonFdset
*mon_fdset
;
2093 MonFdsetFd
*mon_fdset_fd
;
2094 FdsetInfoList
*fdset_list
= NULL
;
2096 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2097 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2098 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2100 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2101 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2103 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2104 FdsetFdInfoList
*fdsetfd_info
;
2106 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2107 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2108 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2109 if (mon_fdset_fd
->opaque
) {
2110 fdsetfd_info
->value
->has_opaque
= true;
2111 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2113 fdsetfd_info
->value
->has_opaque
= false;
2116 fdsetfd_info
->next
= fdsetfd_list
;
2117 fdsetfd_list
= fdsetfd_info
;
2120 fdset_info
->value
->fds
= fdsetfd_list
;
2122 fdset_info
->next
= fdset_list
;
2123 fdset_list
= fdset_info
;
2129 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2130 bool has_opaque
, const char *opaque
,
2133 MonFdset
*mon_fdset
= NULL
;
2134 MonFdsetFd
*mon_fdset_fd
;
2138 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2139 /* Break if match found or match impossible due to ordering by ID */
2140 if (fdset_id
<= mon_fdset
->id
) {
2141 if (fdset_id
< mon_fdset
->id
) {
2149 if (mon_fdset
== NULL
) {
2150 int64_t fdset_id_prev
= -1;
2151 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2155 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2156 "a non-negative value");
2159 /* Use specified fdset ID */
2160 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2161 mon_fdset_cur
= mon_fdset
;
2162 if (fdset_id
< mon_fdset_cur
->id
) {
2167 /* Use first available fdset ID */
2168 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2169 mon_fdset_cur
= mon_fdset
;
2170 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2171 fdset_id_prev
= mon_fdset_cur
->id
;
2178 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2180 mon_fdset
->id
= fdset_id
;
2182 mon_fdset
->id
= fdset_id_prev
+ 1;
2185 /* The fdset list is ordered by fdset ID */
2186 if (!mon_fdset_cur
) {
2187 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2188 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2189 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2191 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2195 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2196 mon_fdset_fd
->fd
= fd
;
2197 mon_fdset_fd
->removed
= false;
2199 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2201 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2203 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2204 fdinfo
->fdset_id
= mon_fdset
->id
;
2205 fdinfo
->fd
= mon_fdset_fd
->fd
;
2210 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2213 MonFdset
*mon_fdset
;
2214 MonFdsetFd
*mon_fdset_fd
;
2217 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2218 if (mon_fdset
->id
!= fdset_id
) {
2221 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2222 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2223 if (mon_fd_flags
== -1) {
2227 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2228 return mon_fdset_fd
->fd
;
2240 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2242 MonFdset
*mon_fdset
;
2243 MonFdsetFd
*mon_fdset_fd_dup
;
2245 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2246 if (mon_fdset
->id
!= fdset_id
) {
2249 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2250 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2254 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2255 mon_fdset_fd_dup
->fd
= dup_fd
;
2256 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2262 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2264 MonFdset
*mon_fdset
;
2265 MonFdsetFd
*mon_fdset_fd_dup
;
2267 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2268 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2269 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2271 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2272 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2273 monitor_fdset_cleanup(mon_fdset
);
2277 return mon_fdset
->id
;
2285 int monitor_fdset_dup_fd_find(int dup_fd
)
2287 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2290 void monitor_fdset_dup_fd_remove(int dup_fd
)
2292 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2295 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2298 Error
*local_err
= NULL
;
2300 if (!qemu_isdigit(fdname
[0]) && mon
) {
2301 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2303 fd
= qemu_parse_fd(fdname
);
2305 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2310 error_propagate(errp
, local_err
);
2319 /* Please update hmp-commands.hx when adding or changing commands */
2320 static mon_cmd_t info_cmds
[] = {
2321 #include "hmp-commands-info.h"
2325 /* mon_cmds and info_cmds would be sorted at runtime */
2326 static mon_cmd_t mon_cmds
[] = {
2327 #include "hmp-commands.h"
2331 /*******************************************************************/
2333 static const char *pch
;
2334 static sigjmp_buf expr_env
;
2337 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2338 expr_error(Monitor
*mon
, const char *fmt
, ...)
2342 monitor_vprintf(mon
, fmt
, ap
);
2343 monitor_printf(mon
, "\n");
2345 siglongjmp(expr_env
, 1);
2348 /* return 0 if OK, -1 if not found */
2349 static int get_monitor_def(target_long
*pval
, const char *name
)
2351 const MonitorDef
*md
= target_monitor_defs();
2352 CPUState
*cs
= mon_get_cpu();
2357 if (cs
== NULL
|| md
== NULL
) {
2361 for(; md
->name
!= NULL
; md
++) {
2362 if (compare_cmd(name
, md
->name
)) {
2363 if (md
->get_value
) {
2364 *pval
= md
->get_value(md
, md
->offset
);
2366 CPUArchState
*env
= mon_get_cpu_env();
2367 ptr
= (uint8_t *)env
+ md
->offset
;
2370 *pval
= *(int32_t *)ptr
;
2373 *pval
= *(target_long
*)ptr
;
2384 ret
= target_get_monitor_def(cs
, name
, &tmp
);
2386 *pval
= (target_long
) tmp
;
2392 static void next(void)
2396 while (qemu_isspace(*pch
))
2401 static int64_t expr_sum(Monitor
*mon
);
2403 static int64_t expr_unary(Monitor
*mon
)
2412 n
= expr_unary(mon
);
2416 n
= -expr_unary(mon
);
2420 n
= ~expr_unary(mon
);
2426 expr_error(mon
, "')' expected");
2433 expr_error(mon
, "character constant expected");
2437 expr_error(mon
, "missing terminating \' character");
2447 while ((*pch
>= 'a' && *pch
<= 'z') ||
2448 (*pch
>= 'A' && *pch
<= 'Z') ||
2449 (*pch
>= '0' && *pch
<= '9') ||
2450 *pch
== '_' || *pch
== '.') {
2451 if ((q
- buf
) < sizeof(buf
) - 1)
2455 while (qemu_isspace(*pch
))
2458 ret
= get_monitor_def(®
, buf
);
2460 expr_error(mon
, "unknown register");
2465 expr_error(mon
, "unexpected end of expression");
2470 n
= strtoull(pch
, &p
, 0);
2471 if (errno
== ERANGE
) {
2472 expr_error(mon
, "number too large");
2475 expr_error(mon
, "invalid char '%c' in expression", *p
);
2478 while (qemu_isspace(*pch
))
2486 static int64_t expr_prod(Monitor
*mon
)
2491 val
= expr_unary(mon
);
2494 if (op
!= '*' && op
!= '/' && op
!= '%')
2497 val2
= expr_unary(mon
);
2506 expr_error(mon
, "division by zero");
2517 static int64_t expr_logic(Monitor
*mon
)
2522 val
= expr_prod(mon
);
2525 if (op
!= '&' && op
!= '|' && op
!= '^')
2528 val2
= expr_prod(mon
);
2545 static int64_t expr_sum(Monitor
*mon
)
2550 val
= expr_logic(mon
);
2553 if (op
!= '+' && op
!= '-')
2556 val2
= expr_logic(mon
);
2565 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2568 if (sigsetjmp(expr_env
, 0)) {
2572 while (qemu_isspace(*pch
))
2574 *pval
= expr_sum(mon
);
2579 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2581 const char *p
= *pp
;
2585 d
= strtod(p
, &tailp
);
2587 monitor_printf(mon
, "Number expected\n");
2590 if (d
!= d
|| d
- d
!= 0) {
2591 /* NaN or infinity */
2592 monitor_printf(mon
, "Bad number\n");
2601 * Store the command-name in cmdname, and return a pointer to
2602 * the remaining of the command string.
2604 static const char *get_command_name(const char *cmdline
,
2605 char *cmdname
, size_t nlen
)
2608 const char *p
, *pstart
;
2611 while (qemu_isspace(*p
))
2616 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2621 memcpy(cmdname
, pstart
, len
);
2622 cmdname
[len
] = '\0';
2627 * Read key of 'type' into 'key' and return the current
2630 static char *key_get_info(const char *type
, char **key
)
2638 p
= strchr(type
, ':');
2645 str
= g_malloc(len
+ 1);
2646 memcpy(str
, type
, len
);
2653 static int default_fmt_format
= 'x';
2654 static int default_fmt_size
= 4;
2656 static int is_valid_option(const char *c
, const char *typestr
)
2664 typestr
= strstr(typestr
, option
);
2665 return (typestr
!= NULL
);
2668 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2669 const char *cmdname
)
2671 const mon_cmd_t
*cmd
;
2673 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2674 if (compare_cmd(cmdname
, cmd
->name
)) {
2683 * Parse command name from @cmdp according to command table @table.
2684 * If blank, return NULL.
2685 * Else, if no valid command can be found, report to @mon, and return
2687 * Else, change @cmdp to point right behind the name, and return its
2688 * command table entry.
2689 * Do not assume the return value points into @table! It doesn't when
2690 * the command is found in a sub-command table.
2692 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2697 const mon_cmd_t
*cmd
;
2700 /* extract the command name */
2701 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2705 cmd
= search_dispatch_table(table
, cmdname
);
2707 monitor_printf(mon
, "unknown command: '%.*s'\n",
2708 (int)(p
- *cmdp
), *cmdp
);
2712 /* filter out following useless space */
2713 while (qemu_isspace(*p
)) {
2718 /* search sub command */
2719 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2720 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2727 * Parse arguments for @cmd.
2728 * If it can't be parsed, report to @mon, and return NULL.
2729 * Else, insert command arguments into a QDict, and return it.
2730 * Note: On success, caller has to free the QDict structure.
2733 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2735 const mon_cmd_t
*cmd
)
2737 const char *typestr
;
2740 const char *p
= *endp
;
2742 QDict
*qdict
= qdict_new();
2744 /* parse the parameters */
2745 typestr
= cmd
->args_type
;
2747 typestr
= key_get_info(typestr
, &key
);
2759 while (qemu_isspace(*p
))
2761 if (*typestr
== '?') {
2764 /* no optional string: NULL argument */
2768 ret
= get_str(buf
, sizeof(buf
), &p
);
2772 monitor_printf(mon
, "%s: filename expected\n",
2776 monitor_printf(mon
, "%s: block device name expected\n",
2780 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2785 qdict_put(qdict
, key
, qstring_from_str(buf
));
2790 QemuOptsList
*opts_list
;
2793 opts_list
= qemu_find_opts(key
);
2794 if (!opts_list
|| opts_list
->desc
->name
) {
2797 while (qemu_isspace(*p
)) {
2802 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2805 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2809 qemu_opts_to_qdict(opts
, qdict
);
2810 qemu_opts_del(opts
);
2815 int count
, format
, size
;
2817 while (qemu_isspace(*p
))
2823 if (qemu_isdigit(*p
)) {
2825 while (qemu_isdigit(*p
)) {
2826 count
= count
* 10 + (*p
- '0');
2864 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2865 monitor_printf(mon
, "invalid char in format: '%c'\n",
2870 format
= default_fmt_format
;
2871 if (format
!= 'i') {
2872 /* for 'i', not specifying a size gives -1 as size */
2874 size
= default_fmt_size
;
2875 default_fmt_size
= size
;
2877 default_fmt_format
= format
;
2880 format
= default_fmt_format
;
2881 if (format
!= 'i') {
2882 size
= default_fmt_size
;
2887 qdict_put(qdict
, "count", qint_from_int(count
));
2888 qdict_put(qdict
, "format", qint_from_int(format
));
2889 qdict_put(qdict
, "size", qint_from_int(size
));
2898 while (qemu_isspace(*p
))
2900 if (*typestr
== '?' || *typestr
== '.') {
2901 if (*typestr
== '?') {
2909 while (qemu_isspace(*p
))
2918 if (get_expr(mon
, &val
, &p
))
2920 /* Check if 'i' is greater than 32-bit */
2921 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2922 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2923 monitor_printf(mon
, "integer is for 32-bit values\n");
2925 } else if (c
== 'M') {
2927 monitor_printf(mon
, "enter a positive value\n");
2932 qdict_put(qdict
, key
, qint_from_int(val
));
2941 while (qemu_isspace(*p
)) {
2944 if (*typestr
== '?') {
2950 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
2951 if (ret
< 0 || val
> INT64_MAX
) {
2952 monitor_printf(mon
, "invalid size\n");
2955 qdict_put(qdict
, key
, qint_from_int(val
));
2963 while (qemu_isspace(*p
))
2965 if (*typestr
== '?') {
2971 if (get_double(mon
, &val
, &p
) < 0) {
2974 if (p
[0] && p
[1] == 's') {
2977 val
/= 1e3
; p
+= 2; break;
2979 val
/= 1e6
; p
+= 2; break;
2981 val
/= 1e9
; p
+= 2; break;
2984 if (*p
&& !qemu_isspace(*p
)) {
2985 monitor_printf(mon
, "Unknown unit suffix\n");
2988 qdict_put(qdict
, key
, qfloat_from_double(val
));
2996 while (qemu_isspace(*p
)) {
3000 while (qemu_isgraph(*p
)) {
3003 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3005 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3008 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3011 qdict_put(qdict
, key
, qbool_from_bool(val
));
3016 const char *tmp
= p
;
3023 while (qemu_isspace(*p
))
3028 if(!is_valid_option(p
, typestr
)) {
3030 monitor_printf(mon
, "%s: unsupported option -%c\n",
3042 qdict_put(qdict
, key
, qbool_from_bool(true));
3049 /* package all remaining string */
3052 while (qemu_isspace(*p
)) {
3055 if (*typestr
== '?') {
3058 /* no remaining string: NULL argument */
3064 monitor_printf(mon
, "%s: string expected\n",
3068 qdict_put(qdict
, key
, qstring_from_str(p
));
3074 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3080 /* check that all arguments were parsed */
3081 while (qemu_isspace(*p
))
3084 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3097 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
3100 const mon_cmd_t
*cmd
;
3102 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
3107 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
3109 monitor_printf(mon
, "Try \"help %s\" for more information\n",
3114 cmd
->cmd(mon
, qdict
);
3118 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3120 const char *p
, *pstart
;
3129 p
= pstart
+ strlen(pstart
);
3131 if (len
> sizeof(cmd
) - 2)
3132 len
= sizeof(cmd
) - 2;
3133 memcpy(cmd
, pstart
, len
);
3135 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3136 readline_add_completion(mon
->rs
, cmd
);
3144 static void file_completion(Monitor
*mon
, const char *input
)
3149 char file
[1024], file_prefix
[1024];
3153 p
= strrchr(input
, '/');
3156 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3157 pstrcpy(path
, sizeof(path
), ".");
3159 input_path_len
= p
- input
+ 1;
3160 memcpy(path
, input
, input_path_len
);
3161 if (input_path_len
> sizeof(path
) - 1)
3162 input_path_len
= sizeof(path
) - 1;
3163 path
[input_path_len
] = '\0';
3164 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3167 ffs
= opendir(path
);
3176 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3180 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3181 memcpy(file
, input
, input_path_len
);
3182 if (input_path_len
< sizeof(file
))
3183 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3185 /* stat the file to find out if it's a directory.
3186 * In that case add a slash to speed up typing long paths
3188 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3189 pstrcat(file
, sizeof(file
), "/");
3191 readline_add_completion(mon
->rs
, file
);
3197 static const char *next_arg_type(const char *typestr
)
3199 const char *p
= strchr(typestr
, ':');
3200 return (p
!= NULL
? ++p
: typestr
);
3203 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3206 if (!str
|| !option
) {
3209 if (!strncmp(option
, str
, strlen(str
))) {
3210 readline_add_completion(rs
, option
);
3214 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3217 ChardevBackendInfoList
*list
, *start
;
3223 readline_set_completion_index(rs
, len
);
3225 start
= list
= qmp_query_chardev_backends(NULL
);
3227 const char *chr_name
= list
->value
->name
;
3229 if (!strncmp(chr_name
, str
, len
)) {
3230 readline_add_completion(rs
, chr_name
);
3234 qapi_free_ChardevBackendInfoList(start
);
3237 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3246 readline_set_completion_index(rs
, len
);
3247 for (i
= 0; NetClientDriver_lookup
[i
]; i
++) {
3248 add_completion_option(rs
, str
, NetClientDriver_lookup
[i
]);
3252 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3262 readline_set_completion_index(rs
, len
);
3263 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3266 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3268 name
= object_class_get_name(OBJECT_CLASS(dc
));
3270 if (!dc
->cannot_instantiate_with_device_add_yet
3271 && !strncmp(name
, str
, len
)) {
3272 readline_add_completion(rs
, name
);
3279 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3289 readline_set_completion_index(rs
, len
);
3290 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3294 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3295 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3296 readline_add_completion(rs
, name
);
3303 static void peripheral_device_del_completion(ReadLineState
*rs
,
3304 const char *str
, size_t len
)
3306 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3307 GSList
*list
, *item
;
3309 list
= qdev_build_hotpluggable_device_list(peripheral
);
3314 for (item
= list
; item
; item
= g_slist_next(item
)) {
3315 DeviceState
*dev
= item
->data
;
3317 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3318 readline_add_completion(rs
, dev
->id
);
3325 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3328 ChardevInfoList
*list
, *start
;
3334 readline_set_completion_index(rs
, len
);
3336 start
= list
= qmp_query_chardev(NULL
);
3338 ChardevInfo
*chr
= list
->value
;
3340 if (!strncmp(chr
->label
, str
, len
)) {
3341 readline_add_completion(rs
, chr
->label
);
3345 qapi_free_ChardevInfoList(start
);
3348 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3351 ChardevInfoList
*list
, *start
;
3354 readline_set_completion_index(rs
, len
);
3356 start
= list
= qmp_query_chardev(NULL
);
3358 ChardevInfo
*chr_info
= list
->value
;
3360 if (!strncmp(chr_info
->label
, str
, len
)) {
3361 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
3362 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
3363 readline_add_completion(rs
, chr_info
->label
);
3368 qapi_free_ChardevInfoList(start
);
3371 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3376 ringbuf_completion(rs
, str
);
3379 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3388 readline_set_completion_index(rs
, len
);
3389 peripheral_device_del_completion(rs
, str
, len
);
3392 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3394 ObjectPropertyInfoList
*list
, *start
;
3401 readline_set_completion_index(rs
, len
);
3403 start
= list
= qmp_qom_list("/objects", NULL
);
3405 ObjectPropertyInfo
*info
= list
->value
;
3407 if (!strncmp(info
->type
, "child<", 5)
3408 && !strncmp(info
->name
, str
, len
)) {
3409 readline_add_completion(rs
, info
->name
);
3413 qapi_free_ObjectPropertyInfoList(start
);
3416 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3425 sep
= strrchr(str
, '-');
3430 readline_set_completion_index(rs
, len
);
3431 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3432 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3433 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3438 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3443 readline_set_completion_index(rs
, len
);
3445 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3447 count
= qemu_find_net_clients_except(NULL
, ncs
,
3448 NET_CLIENT_DRIVER_NONE
,
3450 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3451 const char *name
= ncs
[i
]->name
;
3452 if (!strncmp(str
, name
, len
)) {
3453 readline_add_completion(rs
, name
);
3456 } else if (nb_args
== 3) {
3457 add_completion_option(rs
, str
, "on");
3458 add_completion_option(rs
, str
, "off");
3462 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3465 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3472 readline_set_completion_index(rs
, len
);
3473 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3475 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3477 const char *name
= ncs
[i
]->name
;
3478 if (strncmp(str
, name
, len
)) {
3481 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3483 readline_add_completion(rs
, name
);
3488 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3493 readline_set_completion_index(rs
, len
);
3495 TraceEventIter iter
;
3497 char *pattern
= g_strdup_printf("%s*", str
);
3498 trace_event_iter_init(&iter
, pattern
);
3499 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3500 readline_add_completion(rs
, trace_event_get_name(ev
));
3506 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3511 readline_set_completion_index(rs
, len
);
3513 TraceEventIter iter
;
3515 char *pattern
= g_strdup_printf("%s*", str
);
3516 trace_event_iter_init(&iter
, pattern
);
3517 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3518 readline_add_completion(rs
, trace_event_get_name(ev
));
3521 } else if (nb_args
== 3) {
3522 add_completion_option(rs
, str
, "on");
3523 add_completion_option(rs
, str
, "off");
3527 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3534 readline_set_completion_index(rs
, strlen(str
));
3535 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3536 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3540 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3546 readline_set_completion_index(rs
, len
);
3549 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3550 const char *name
= MigrationCapability_lookup
[i
];
3551 if (!strncmp(str
, name
, len
)) {
3552 readline_add_completion(rs
, name
);
3555 } else if (nb_args
== 3) {
3556 add_completion_option(rs
, str
, "on");
3557 add_completion_option(rs
, str
, "off");
3561 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3567 readline_set_completion_index(rs
, len
);
3570 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3571 const char *name
= MigrationParameter_lookup
[i
];
3572 if (!strncmp(str
, name
, len
)) {
3573 readline_add_completion(rs
, name
);
3579 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3587 readline_set_completion_index(rs
, len
);
3588 for (i
= 0; host_net_devices
[i
]; i
++) {
3589 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3590 readline_add_completion(rs
, host_net_devices
[i
]);
3595 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3597 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3601 readline_set_completion_index(rs
, len
);
3603 count
= qemu_find_net_clients_except(NULL
, ncs
,
3604 NET_CLIENT_DRIVER_NONE
,
3606 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3610 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3613 snprintf(name
, sizeof(name
), "%d", id
);
3614 if (!strncmp(str
, name
, len
)) {
3615 readline_add_completion(rs
, name
);
3619 } else if (nb_args
== 3) {
3620 count
= qemu_find_net_clients_except(NULL
, ncs
,
3621 NET_CLIENT_DRIVER_NIC
,
3623 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3627 if (ncs
[i
]->info
->type
== NET_CLIENT_DRIVER_HUBPORT
||
3628 net_hub_id_for_client(ncs
[i
], &id
)) {
3631 name
= ncs
[i
]->name
;
3632 if (!strncmp(str
, name
, len
)) {
3633 readline_add_completion(rs
, name
);
3640 static void vm_completion(ReadLineState
*rs
, const char *str
)
3643 BlockDriverState
*bs
;
3644 BdrvNextIterator it
;
3647 readline_set_completion_index(rs
, len
);
3649 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3650 SnapshotInfoList
*snapshots
, *snapshot
;
3651 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3654 aio_context_acquire(ctx
);
3655 if (bdrv_can_snapshot(bs
)) {
3656 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3658 aio_context_release(ctx
);
3663 snapshot
= snapshots
;
3665 char *completion
= snapshot
->value
->name
;
3666 if (!strncmp(str
, completion
, len
)) {
3667 readline_add_completion(rs
, completion
);
3669 completion
= snapshot
->value
->id
;
3670 if (!strncmp(str
, completion
, len
)) {
3671 readline_add_completion(rs
, completion
);
3673 snapshot
= snapshot
->next
;
3675 qapi_free_SnapshotInfoList(snapshots
);
3680 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3683 vm_completion(rs
, str
);
3687 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3690 vm_completion(rs
, str
);
3694 static void monitor_find_completion_by_table(Monitor
*mon
,
3695 const mon_cmd_t
*cmd_table
,
3699 const char *cmdname
;
3701 const char *ptype
, *str
, *name
;
3702 const mon_cmd_t
*cmd
;
3703 BlockBackend
*blk
= NULL
;
3706 /* command completion */
3711 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3712 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3713 cmd_completion(mon
, cmdname
, cmd
->name
);
3716 /* find the command */
3717 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3718 if (compare_cmd(args
[0], cmd
->name
)) {
3726 if (cmd
->sub_table
) {
3727 /* do the job again */
3728 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3729 &args
[1], nb_args
- 1);
3732 if (cmd
->command_completion
) {
3733 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3737 ptype
= next_arg_type(cmd
->args_type
);
3738 for(i
= 0; i
< nb_args
- 2; i
++) {
3739 if (*ptype
!= '\0') {
3740 ptype
= next_arg_type(ptype
);
3741 while (*ptype
== '?')
3742 ptype
= next_arg_type(ptype
);
3745 str
= args
[nb_args
- 1];
3746 while (*ptype
== '-' && ptype
[1] != '\0') {
3747 ptype
= next_arg_type(ptype
);
3751 /* file completion */
3752 readline_set_completion_index(mon
->rs
, strlen(str
));
3753 file_completion(mon
, str
);
3756 /* block device name completion */
3757 readline_set_completion_index(mon
->rs
, strlen(str
));
3758 while ((blk
= blk_next(blk
)) != NULL
) {
3759 name
= blk_name(blk
);
3760 if (str
[0] == '\0' ||
3761 !strncmp(name
, str
, strlen(str
))) {
3762 readline_add_completion(mon
->rs
, name
);
3768 if (!strcmp(cmd
->name
, "help|?")) {
3769 monitor_find_completion_by_table(mon
, cmd_table
,
3770 &args
[1], nb_args
- 1);
3779 static void monitor_find_completion(void *opaque
,
3780 const char *cmdline
)
3782 Monitor
*mon
= opaque
;
3783 char *args
[MAX_ARGS
];
3786 /* 1. parse the cmdline */
3787 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3791 /* if the line ends with a space, it means we want to complete the
3793 len
= strlen(cmdline
);
3794 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3795 if (nb_args
>= MAX_ARGS
) {
3798 args
[nb_args
++] = g_strdup("");
3801 /* 2. auto complete according to args */
3802 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3805 free_cmdline_args(args
, nb_args
);
3808 static int monitor_can_read(void *opaque
)
3810 Monitor
*mon
= opaque
;
3812 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3815 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3817 QObject
*req
, *rsp
= NULL
, *id
= NULL
;
3818 QDict
*qdict
= NULL
;
3819 Monitor
*mon
= cur_mon
;
3822 req
= json_parser_parse_err(tokens
, NULL
, &err
);
3824 /* json_parser_parse_err() sucks: can fail without setting @err */
3825 error_setg(&err
, QERR_JSON_PARSING
);
3831 qdict
= qobject_to_qdict(req
);
3833 id
= qdict_get(qdict
, "id");
3835 qdict_del(qdict
, "id");
3836 } /* else will fail qmp_dispatch() */
3838 rsp
= qmp_dispatch(cur_mon
->qmp
.commands
, req
);
3840 if (mon
->qmp
.commands
== &qmp_cap_negotiation_commands
) {
3841 qdict
= qdict_get_qdict(qobject_to_qdict(rsp
), "error");
3843 && !g_strcmp0(qdict_get_try_str(qdict
, "class"),
3844 QapiErrorClass_lookup
[ERROR_CLASS_COMMAND_NOT_FOUND
])) {
3845 /* Provide a more useful error message */
3846 qdict_del(qdict
, "desc");
3847 qdict_put(qdict
, "desc",
3848 qstring_from_str("Expecting capabilities negotiation"
3849 " with 'qmp_capabilities'"));
3855 qdict
= qdict_new();
3856 qdict_put_obj(qdict
, "error", qmp_build_error_object(err
));
3858 rsp
= QOBJECT(qdict
);
3863 qdict_put_obj(qobject_to_qdict(rsp
), "id", id
);
3867 monitor_json_emitter(mon
, rsp
);
3871 qobject_decref(rsp
);
3872 qobject_decref(req
);
3875 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3877 Monitor
*old_mon
= cur_mon
;
3881 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3886 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3888 Monitor
*old_mon
= cur_mon
;
3894 for (i
= 0; i
< size
; i
++)
3895 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3897 if (size
== 0 || buf
[size
- 1] != 0)
3898 monitor_printf(cur_mon
, "corrupted command\n");
3900 handle_hmp_command(cur_mon
, (char *)buf
);
3906 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3907 void *readline_opaque
)
3909 Monitor
*mon
= opaque
;
3911 monitor_suspend(mon
);
3912 handle_hmp_command(mon
, cmdline
);
3913 monitor_resume(mon
);
3916 int monitor_suspend(Monitor
*mon
)
3924 void monitor_resume(Monitor
*mon
)
3928 if (--mon
->suspend_cnt
== 0)
3929 readline_show_prompt(mon
->rs
);
3932 static QObject
*get_qmp_greeting(void)
3934 QObject
*ver
= NULL
;
3936 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3938 return qobject_from_jsonf("{'QMP': {'version': %p, 'capabilities': []}}",
3942 static void monitor_qmp_event(void *opaque
, int event
)
3945 Monitor
*mon
= opaque
;
3948 case CHR_EVENT_OPENED
:
3949 mon
->qmp
.commands
= &qmp_cap_negotiation_commands
;
3950 data
= get_qmp_greeting();
3951 monitor_json_emitter(mon
, data
);
3952 qobject_decref(data
);
3955 case CHR_EVENT_CLOSED
:
3956 json_message_parser_destroy(&mon
->qmp
.parser
);
3957 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
3959 monitor_fdsets_cleanup();
3964 static void monitor_event(void *opaque
, int event
)
3966 Monitor
*mon
= opaque
;
3969 case CHR_EVENT_MUX_IN
:
3970 qemu_mutex_lock(&mon
->out_lock
);
3972 qemu_mutex_unlock(&mon
->out_lock
);
3973 if (mon
->reset_seen
) {
3974 readline_restart(mon
->rs
);
3975 monitor_resume(mon
);
3978 mon
->suspend_cnt
= 0;
3982 case CHR_EVENT_MUX_OUT
:
3983 if (mon
->reset_seen
) {
3984 if (mon
->suspend_cnt
== 0) {
3985 monitor_printf(mon
, "\n");
3988 monitor_suspend(mon
);
3992 qemu_mutex_lock(&mon
->out_lock
);
3994 qemu_mutex_unlock(&mon
->out_lock
);
3997 case CHR_EVENT_OPENED
:
3998 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3999 "information\n", QEMU_VERSION
);
4000 if (!mon
->mux_out
) {
4001 readline_restart(mon
->rs
);
4002 readline_show_prompt(mon
->rs
);
4004 mon
->reset_seen
= 1;
4008 case CHR_EVENT_CLOSED
:
4010 monitor_fdsets_cleanup();
4016 compare_mon_cmd(const void *a
, const void *b
)
4018 return strcmp(((const mon_cmd_t
*)a
)->name
,
4019 ((const mon_cmd_t
*)b
)->name
);
4022 static void sortcmdlist(void)
4025 int elem_size
= sizeof(mon_cmd_t
);
4027 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4028 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4030 array_num
= sizeof(info_cmds
)/elem_size
-1;
4031 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4034 /* These functions just adapt the readline interface in a typesafe way. We
4035 * could cast function pointers but that discards compiler checks.
4037 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4038 const char *fmt
, ...)
4042 monitor_vprintf(opaque
, fmt
, ap
);
4046 static void monitor_readline_flush(void *opaque
)
4048 monitor_flush(opaque
);
4052 * Print to current monitor if we have one, else to stderr.
4053 * TODO should return int, so callers can calculate width, but that
4054 * requires surgery to monitor_vprintf(). Left for another day.
4056 void error_vprintf(const char *fmt
, va_list ap
)
4058 if (cur_mon
&& !monitor_cur_is_qmp()) {
4059 monitor_vprintf(cur_mon
, fmt
, ap
);
4061 vfprintf(stderr
, fmt
, ap
);
4065 void error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
4067 if (cur_mon
&& !monitor_cur_is_qmp()) {
4068 monitor_vprintf(cur_mon
, fmt
, ap
);
4069 } else if (!cur_mon
) {
4070 vfprintf(stderr
, fmt
, ap
);
4074 static void __attribute__((constructor
)) monitor_lock_init(void)
4076 qemu_mutex_init(&monitor_lock
);
4079 void monitor_init(Chardev
*chr
, int flags
)
4081 static int is_first_init
= 1;
4084 if (is_first_init
) {
4085 monitor_qapi_event_init();
4090 mon
= g_malloc(sizeof(*mon
));
4091 monitor_data_init(mon
);
4093 qemu_chr_fe_init(&mon
->chr
, chr
, &error_abort
);
4095 if (flags
& MONITOR_USE_READLINE
) {
4096 mon
->rs
= readline_init(monitor_readline_printf
,
4097 monitor_readline_flush
,
4099 monitor_find_completion
);
4100 monitor_read_command(mon
, 0);
4103 if (monitor_is_qmp(mon
)) {
4104 qemu_chr_fe_set_handlers(&mon
->chr
, monitor_can_read
, monitor_qmp_read
,
4105 monitor_qmp_event
, mon
, NULL
, true);
4106 qemu_chr_fe_set_echo(&mon
->chr
, true);
4107 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4109 qemu_chr_fe_set_handlers(&mon
->chr
, monitor_can_read
, monitor_read
,
4110 monitor_event
, mon
, NULL
, true);
4113 qemu_mutex_lock(&monitor_lock
);
4114 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4115 qemu_mutex_unlock(&monitor_lock
);
4118 void monitor_cleanup(void)
4120 Monitor
*mon
, *next
;
4122 qemu_mutex_lock(&monitor_lock
);
4123 QLIST_FOREACH_SAFE(mon
, &mon_list
, entry
, next
) {
4124 QLIST_REMOVE(mon
, entry
);
4125 monitor_data_destroy(mon
);
4128 qemu_mutex_unlock(&monitor_lock
);
4131 static void bdrv_password_cb(void *opaque
, const char *password
,
4132 void *readline_opaque
)
4134 Monitor
*mon
= opaque
;
4135 BlockDriverState
*bs
= readline_opaque
;
4137 Error
*local_err
= NULL
;
4139 bdrv_add_key(bs
, password
, &local_err
);
4141 error_report_err(local_err
);
4144 if (mon
->password_completion_cb
)
4145 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4147 monitor_read_command(mon
, 1);
4150 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4151 BlockCompletionFunc
*completion_cb
,
4156 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4157 bdrv_get_encrypted_filename(bs
));
4159 mon
->password_completion_cb
= completion_cb
;
4160 mon
->password_opaque
= opaque
;
4162 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4164 if (err
&& completion_cb
)
4165 completion_cb(opaque
, err
);
4170 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4171 BlockCompletionFunc
*completion_cb
,
4177 blk
= blk_by_name(device
);
4179 monitor_printf(mon
, "Device not found %s\n", device
);
4183 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4187 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4190 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4193 if (completion_cb
) {
4194 completion_cb(opaque
, 0);
4199 QemuOptsList qemu_mon_opts
= {
4201 .implied_opt_name
= "chardev",
4202 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4206 .type
= QEMU_OPT_STRING
,
4209 .type
= QEMU_OPT_STRING
,
4211 .name
= "default", /* deprecated */
4212 .type
= QEMU_OPT_BOOL
,
4215 .type
= QEMU_OPT_BOOL
,
4217 { /* end of list */ }
4222 void qmp_rtc_reset_reinjection(Error
**errp
)
4224 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4228 #ifndef TARGET_S390X
4229 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4231 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4236 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4238 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");
4243 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4245 MachineState
*ms
= MACHINE(qdev_get_machine());
4246 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4248 if (!mc
->has_hotpluggable_cpus
) {
4249 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4253 return machine_query_hotpluggable_cpus(ms
);