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/qint.h"
58 #include "qapi/qmp/qfloat.h"
59 #include "qapi/qmp/qlist.h"
60 #include "qapi/qmp/qbool.h"
61 #include "qapi/qmp/qstring.h"
62 #include "qapi/qmp/qjson.h"
63 #include "qapi/qmp/json-streamer.h"
64 #include "qapi/qmp/json-parser.h"
65 #include <qom/object_interfaces.h>
68 #include "trace/control.h"
69 #include "monitor/hmp-target.h"
70 #ifdef CONFIG_TRACE_SIMPLE
71 #include "trace/simple.h"
73 #include "exec/memory.h"
74 #include "exec/exec-all.h"
76 #include "qmp-commands.h"
78 #include "qemu/thread.h"
79 #include "block/qapi.h"
80 #include "qapi/qmp-event.h"
81 #include "qapi-event.h"
82 #include "qmp-introspect.h"
83 #include "sysemu/block-backend.h"
84 #include "sysemu/qtest.h"
85 #include "qemu/cutils.h"
87 /* for hmp_info_irq/pic */
88 #if defined(TARGET_SPARC)
89 #include "hw/sparc/sun4m.h"
91 #include "hw/lm32/lm32_pic.h"
93 #if defined(TARGET_S390X)
94 #include "hw/s390x/storage-keys.h"
101 * 'B' block device name
102 * 's' string (accept optional quote)
103 * 'S' it just appends the rest of the string (accept optional quote)
104 * 'O' option string of the form NAME=VALUE,...
105 * parsed according to QemuOptsList given by its name
106 * Example: 'device:O' uses qemu_device_opts.
107 * Restriction: only lists with empty desc are supported
108 * TODO lift the restriction
110 * 'l' target long (32 or 64 bit)
111 * 'M' Non-negative target long (32 or 64 bit), in user mode the
112 * value is multiplied by 2^20 (think Mebibyte)
113 * 'o' octets (aka bytes)
114 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
115 * K, k suffix, which multiplies the value by 2^60 for suffixes E
116 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
117 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
119 * user mode accepts an optional ms, us, ns suffix,
120 * which divides the value by 1e3, 1e6, 1e9, respectively
121 * '/' optional gdb-like print format (like "/10x")
123 * '?' optional type (for all types, except '/')
124 * '.' other form of optional type (for 'i' and 'l')
126 * user mode accepts "on" or "off"
127 * '-' optional parameter (eg. '-f')
131 typedef struct mon_cmd_t
{
133 const char *args_type
;
137 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
138 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
140 /* @sub_table is a list of 2nd level of commands. If it do not exist,
141 * mhandler should be used. If it exist, sub_table[?].mhandler should be
142 * used, and mhandler of 1st level plays the role of help function.
144 struct mon_cmd_t
*sub_table
;
145 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
148 /* file descriptors passed via SCM_RIGHTS */
149 typedef struct mon_fd_t mon_fd_t
;
153 QLIST_ENTRY(mon_fd_t
) next
;
156 /* file descriptor associated with a file descriptor set */
157 typedef struct MonFdsetFd MonFdsetFd
;
162 QLIST_ENTRY(MonFdsetFd
) next
;
165 /* file descriptor set containing fds passed via SCM_RIGHTS */
166 typedef struct MonFdset MonFdset
;
169 QLIST_HEAD(, MonFdsetFd
) fds
;
170 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
171 QLIST_ENTRY(MonFdset
) next
;
176 JSONMessageParser parser
;
178 * When a client connects, we're in capabilities negotiation mode.
179 * When command qmp_capabilities succeeds, we go into command
182 bool in_command_mode
; /* are we in command mode? */
186 * To prevent flooding clients, events can be throttled. The
187 * throttling is calculated globally, rather than per-Monitor
190 typedef struct MonitorQAPIEventState
{
191 QAPIEvent event
; /* Throttling state for this event type and... */
192 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
193 QEMUTimer
*timer
; /* Timer for handling delayed events */
194 QDict
*qdict
; /* Delayed event (if any) */
195 } MonitorQAPIEventState
;
198 int64_t rate
; /* Minimum time (in ns) between two events */
199 } MonitorQAPIEventConf
;
202 CharDriverState
*chr
;
212 /* Read under either BQL or out_lock, written with BQL+out_lock. */
218 BlockCompletionFunc
*password_completion_cb
;
219 void *password_opaque
;
220 mon_cmd_t
*cmd_table
;
221 QLIST_HEAD(,mon_fd_t
) fds
;
222 QLIST_ENTRY(Monitor
) entry
;
225 /* QMP checker flags */
226 #define QMP_ACCEPT_UNKNOWNS 1
228 /* Protects mon_list, monitor_event_state. */
229 static QemuMutex monitor_lock
;
231 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
232 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
233 static int mon_refcount
;
235 static mon_cmd_t mon_cmds
[];
236 static mon_cmd_t info_cmds
[];
238 static const mon_cmd_t qmp_cmds
[];
242 static QEMUClockType event_clock_type
= QEMU_CLOCK_REALTIME
;
244 static void monitor_command_cb(void *opaque
, const char *cmdline
,
245 void *readline_opaque
);
248 * Is @mon a QMP monitor?
250 static inline bool monitor_is_qmp(const Monitor
*mon
)
252 return (mon
->flags
& MONITOR_USE_CONTROL
);
256 * Is the current monitor, if any, a QMP monitor?
258 bool monitor_cur_is_qmp(void)
260 return cur_mon
&& monitor_is_qmp(cur_mon
);
263 void monitor_read_command(Monitor
*mon
, int show_prompt
)
268 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
270 readline_show_prompt(mon
->rs
);
273 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
277 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
278 /* prompt is printed on return from the command handler */
281 monitor_printf(mon
, "terminal does not support password prompting\n");
286 static void monitor_flush_locked(Monitor
*mon
);
288 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
291 Monitor
*mon
= opaque
;
293 qemu_mutex_lock(&mon
->out_lock
);
295 monitor_flush_locked(mon
);
296 qemu_mutex_unlock(&mon
->out_lock
);
300 /* Called with mon->out_lock held. */
301 static void monitor_flush_locked(Monitor
*mon
)
307 if (mon
->skip_flush
) {
311 buf
= qstring_get_str(mon
->outbuf
);
312 len
= qstring_get_length(mon
->outbuf
);
314 if (len
&& !mon
->mux_out
) {
315 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
316 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
317 /* all flushed or error */
318 QDECREF(mon
->outbuf
);
319 mon
->outbuf
= qstring_new();
324 QString
*tmp
= qstring_from_str(buf
+ rc
);
325 QDECREF(mon
->outbuf
);
328 if (mon
->out_watch
== 0) {
329 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
330 monitor_unblocked
, mon
);
335 void monitor_flush(Monitor
*mon
)
337 qemu_mutex_lock(&mon
->out_lock
);
338 monitor_flush_locked(mon
);
339 qemu_mutex_unlock(&mon
->out_lock
);
342 /* flush at every end of line */
343 static void monitor_puts(Monitor
*mon
, const char *str
)
347 qemu_mutex_lock(&mon
->out_lock
);
353 qstring_append_chr(mon
->outbuf
, '\r');
355 qstring_append_chr(mon
->outbuf
, c
);
357 monitor_flush_locked(mon
);
360 qemu_mutex_unlock(&mon
->out_lock
);
363 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
370 if (monitor_is_qmp(mon
)) {
374 buf
= g_strdup_vprintf(fmt
, ap
);
375 monitor_puts(mon
, buf
);
379 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
383 monitor_vprintf(mon
, fmt
, ap
);
387 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
391 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
396 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
400 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
401 qobject_to_json(data
);
402 assert(json
!= NULL
);
404 qstring_append_chr(json
, '\n');
405 monitor_puts(mon
, qstring_get_str(json
));
410 static QDict
*build_qmp_error_dict(Error
*err
)
414 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
415 QapiErrorClass_lookup
[error_get_class(err
)],
416 error_get_pretty(err
));
418 return qobject_to_qdict(obj
);
421 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
426 trace_monitor_protocol_emitter(mon
);
429 /* success response */
432 qobject_incref(data
);
433 qdict_put_obj(qmp
, "return", data
);
435 /* return an empty QDict by default */
436 qdict_put(qmp
, "return", qdict_new());
440 qmp
= build_qmp_error_dict(err
);
444 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
448 monitor_json_emitter(mon
, QOBJECT(qmp
));
453 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
454 /* Limit guest-triggerable events to 1 per second */
455 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
456 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
457 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
458 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
459 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
460 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
463 GHashTable
*monitor_qapi_event_state
;
466 * Emits the event to every monitor instance, @event is only used for trace
467 * Called with monitor_lock held.
469 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
473 trace_monitor_protocol_event_emit(event
, qdict
);
474 QLIST_FOREACH(mon
, &mon_list
, entry
) {
475 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
476 monitor_json_emitter(mon
, QOBJECT(qdict
));
481 static void monitor_qapi_event_handler(void *opaque
);
484 * Queue a new event for emission to Monitor instances,
485 * applying any rate limiting if required.
488 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
490 MonitorQAPIEventConf
*evconf
;
491 MonitorQAPIEventState
*evstate
;
493 assert(event
< QAPI_EVENT__MAX
);
494 evconf
= &monitor_qapi_event_conf
[event
];
495 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
497 qemu_mutex_lock(&monitor_lock
);
500 /* Unthrottled event */
501 monitor_qapi_event_emit(event
, qdict
);
503 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
504 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
506 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
507 assert(!evstate
|| timer_pending(evstate
->timer
));
511 * Timer is pending for (at least) evconf->rate ns after
512 * last send. Store event for sending when timer fires,
513 * replacing a prior stored event if any.
515 QDECREF(evstate
->qdict
);
516 evstate
->qdict
= qdict
;
517 QINCREF(evstate
->qdict
);
520 * Last send was (at least) evconf->rate ns ago.
521 * Send immediately, and arm the timer to call
522 * monitor_qapi_event_handler() in evconf->rate ns. Any
523 * events arriving before then will be delayed until then.
525 int64_t now
= qemu_clock_get_ns(event_clock_type
);
527 monitor_qapi_event_emit(event
, qdict
);
529 evstate
= g_new(MonitorQAPIEventState
, 1);
530 evstate
->event
= event
;
531 evstate
->data
= data
;
532 QINCREF(evstate
->data
);
533 evstate
->qdict
= NULL
;
534 evstate
->timer
= timer_new_ns(event_clock_type
,
535 monitor_qapi_event_handler
,
537 g_hash_table_add(monitor_qapi_event_state
, evstate
);
538 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
542 qemu_mutex_unlock(&monitor_lock
);
546 * This function runs evconf->rate ns after sending a throttled
548 * If another event has since been stored, send it.
550 static void monitor_qapi_event_handler(void *opaque
)
552 MonitorQAPIEventState
*evstate
= opaque
;
553 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
555 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
556 qemu_mutex_lock(&monitor_lock
);
558 if (evstate
->qdict
) {
559 int64_t now
= qemu_clock_get_ns(event_clock_type
);
561 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
562 QDECREF(evstate
->qdict
);
563 evstate
->qdict
= NULL
;
564 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
566 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
567 QDECREF(evstate
->data
);
568 timer_free(evstate
->timer
);
572 qemu_mutex_unlock(&monitor_lock
);
575 static unsigned int qapi_event_throttle_hash(const void *key
)
577 const MonitorQAPIEventState
*evstate
= key
;
578 unsigned int hash
= evstate
->event
* 255;
580 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
581 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
584 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
585 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
591 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
593 const MonitorQAPIEventState
*eva
= a
;
594 const MonitorQAPIEventState
*evb
= b
;
596 if (eva
->event
!= evb
->event
) {
600 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
601 return !strcmp(qdict_get_str(eva
->data
, "id"),
602 qdict_get_str(evb
->data
, "id"));
605 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
606 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
607 qdict_get_str(evb
->data
, "node-name"));
613 static void monitor_qapi_event_init(void)
615 if (qtest_enabled()) {
616 event_clock_type
= QEMU_CLOCK_VIRTUAL
;
619 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
620 qapi_event_throttle_equal
);
621 qmp_event_set_func_emit(monitor_qapi_event_queue
);
624 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
626 cur_mon
->qmp
.in_command_mode
= true;
629 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
631 static void monitor_data_init(Monitor
*mon
)
633 memset(mon
, 0, sizeof(Monitor
));
634 qemu_mutex_init(&mon
->out_lock
);
635 mon
->outbuf
= qstring_new();
636 /* Use *mon_cmds by default. */
637 mon
->cmd_table
= mon_cmds
;
640 static void monitor_data_destroy(Monitor
*mon
)
642 QDECREF(mon
->outbuf
);
643 qemu_mutex_destroy(&mon
->out_lock
);
646 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
647 int64_t cpu_index
, Error
**errp
)
650 Monitor
*old_mon
, hmp
;
652 monitor_data_init(&hmp
);
653 hmp
.skip_flush
= true;
659 int ret
= monitor_set_cpu(cpu_index
);
662 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
668 handle_hmp_command(&hmp
, command_line
);
671 qemu_mutex_lock(&hmp
.out_lock
);
672 if (qstring_get_length(hmp
.outbuf
) > 0) {
673 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
675 output
= g_strdup("");
677 qemu_mutex_unlock(&hmp
.out_lock
);
680 monitor_data_destroy(&hmp
);
684 static int compare_cmd(const char *name
, const char *list
)
686 const char *p
, *pstart
;
694 p
= pstart
+ strlen(pstart
);
695 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
704 static int get_str(char *buf
, int buf_size
, const char **pp
)
712 while (qemu_isspace(*p
)) {
723 while (*p
!= '\0' && *p
!= '\"') {
739 printf("unsupported escape code: '\\%c'\n", c
);
742 if ((q
- buf
) < buf_size
- 1) {
746 if ((q
- buf
) < buf_size
- 1) {
753 printf("unterminated string\n");
758 while (*p
!= '\0' && !qemu_isspace(*p
)) {
759 if ((q
- buf
) < buf_size
- 1) {
772 static void free_cmdline_args(char **args
, int nb_args
)
776 assert(nb_args
<= MAX_ARGS
);
778 for (i
= 0; i
< nb_args
; i
++) {
785 * Parse the command line to get valid args.
786 * @cmdline: command line to be parsed.
787 * @pnb_args: location to store the number of args, must NOT be NULL.
788 * @args: location to store the args, which should be freed by caller, must
791 * Returns 0 on success, negative on failure.
793 * NOTE: this parser is an approximate form of the real command parser. Number
794 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
795 * return with failure.
797 static int parse_cmdline(const char *cmdline
,
798 int *pnb_args
, char **args
)
807 while (qemu_isspace(*p
)) {
813 if (nb_args
>= MAX_ARGS
) {
816 ret
= get_str(buf
, sizeof(buf
), &p
);
820 args
[nb_args
] = g_strdup(buf
);
827 free_cmdline_args(args
, nb_args
);
831 static void help_cmd_dump_one(Monitor
*mon
,
832 const mon_cmd_t
*cmd
,
838 for (i
= 0; i
< prefix_args_nb
; i
++) {
839 monitor_printf(mon
, "%s ", prefix_args
[i
]);
841 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
844 /* @args[@arg_index] is the valid command need to find in @cmds */
845 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
846 char **args
, int nb_args
, int arg_index
)
848 const mon_cmd_t
*cmd
;
850 /* No valid arg need to compare with, dump all in *cmds */
851 if (arg_index
>= nb_args
) {
852 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
853 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
858 /* Find one entry to dump */
859 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
860 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
861 if (cmd
->sub_table
) {
862 /* continue with next arg */
863 help_cmd_dump(mon
, cmd
->sub_table
,
864 args
, nb_args
, arg_index
+ 1);
866 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
873 static void help_cmd(Monitor
*mon
, const char *name
)
875 char *args
[MAX_ARGS
];
878 /* 1. parse user input */
880 /* special case for log, directly dump and return */
881 if (!strcmp(name
, "log")) {
882 const QEMULogItem
*item
;
883 monitor_printf(mon
, "Log items (comma separated):\n");
884 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
885 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
886 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
891 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
896 /* 2. dump the contents according to parsed args */
897 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
899 free_cmdline_args(args
, nb_args
);
902 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
904 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
907 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
909 const char *tp_name
= qdict_get_str(qdict
, "name");
910 bool new_state
= qdict_get_bool(qdict
, "option");
911 Error
*local_err
= NULL
;
913 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
915 error_report_err(local_err
);
919 #ifdef CONFIG_TRACE_SIMPLE
920 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
922 const char *op
= qdict_get_try_str(qdict
, "op");
923 const char *arg
= qdict_get_try_str(qdict
, "arg");
926 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
927 } else if (!strcmp(op
, "on")) {
928 st_set_trace_file_enabled(true);
929 } else if (!strcmp(op
, "off")) {
930 st_set_trace_file_enabled(false);
931 } else if (!strcmp(op
, "flush")) {
932 st_flush_trace_buffer();
933 } else if (!strcmp(op
, "set")) {
935 st_set_trace_file(arg
);
938 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
939 help_cmd(mon
, "trace-file");
944 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
946 help_cmd(mon
, "info");
949 CommandInfoList
*qmp_query_commands(Error
**errp
)
951 CommandInfoList
*info
, *cmd_list
= NULL
;
952 const mon_cmd_t
*cmd
;
954 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
955 info
= g_malloc0(sizeof(*info
));
956 info
->value
= g_malloc0(sizeof(*info
->value
));
957 info
->value
->name
= g_strdup(cmd
->name
);
959 info
->next
= cmd_list
;
966 EventInfoList
*qmp_query_events(Error
**errp
)
968 EventInfoList
*info
, *ev_list
= NULL
;
971 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
972 const char *event_name
= QAPIEvent_lookup
[e
];
973 assert(event_name
!= NULL
);
974 info
= g_malloc0(sizeof(*info
));
975 info
->value
= g_malloc0(sizeof(*info
->value
));
976 info
->value
->name
= g_strdup(event_name
);
978 info
->next
= ev_list
;
986 * Minor hack: generated marshalling suppressed for this command
987 * ('gen': false in the schema) so we can parse the JSON string
988 * directly into QObject instead of first parsing it with
989 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
990 * to QObject with generated output marshallers, every time. Instead,
991 * we do it in test-qmp-input-visitor.c, just to make sure
992 * qapi-introspect.py's output actually conforms to the schema.
994 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
997 *ret_data
= qobject_from_json(qmp_schema_json
);
1000 /* set the current CPU defined by the user */
1001 int monitor_set_cpu(int cpu_index
)
1005 cpu
= qemu_get_cpu(cpu_index
);
1009 cur_mon
->mon_cpu
= cpu
;
1013 CPUState
*mon_get_cpu(void)
1015 if (!cur_mon
->mon_cpu
) {
1018 cpu_synchronize_state(cur_mon
->mon_cpu
);
1019 return cur_mon
->mon_cpu
;
1022 CPUArchState
*mon_get_cpu_env(void)
1024 return mon_get_cpu()->env_ptr
;
1027 int monitor_get_cpu_index(void)
1029 return mon_get_cpu()->cpu_index
;
1032 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1034 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1037 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1039 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1040 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1043 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1045 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1048 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1057 str
= readline_get_history(mon
->rs
, i
);
1060 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1065 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1067 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1070 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1072 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1073 TraceEventInfoList
*elem
;
1075 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1076 monitor_printf(mon
, "%s : state %u\n",
1078 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1080 qapi_free_TraceEventInfoList(events
);
1083 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1084 bool has_port
, int64_t port
,
1085 bool has_tls_port
, int64_t tls_port
,
1086 bool has_cert_subject
, const char *cert_subject
,
1089 if (strcmp(protocol
, "spice") == 0) {
1090 if (!qemu_using_spice(errp
)) {
1094 if (!has_port
&& !has_tls_port
) {
1095 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1099 if (qemu_spice_migrate_info(hostname
,
1100 has_port
? port
: -1,
1101 has_tls_port
? tls_port
: -1,
1103 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1109 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1112 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1114 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1117 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1120 const char *items
= qdict_get_str(qdict
, "items");
1122 if (!strcmp(items
, "none")) {
1125 mask
= qemu_str_to_log_mask(items
);
1127 help_cmd(mon
, "log");
1134 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1136 const char *option
= qdict_get_try_str(qdict
, "option");
1137 if (!option
|| !strcmp(option
, "on")) {
1139 } else if (!strcmp(option
, "off")) {
1142 monitor_printf(mon
, "unexpected option %s\n", option
);
1146 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1148 const char *device
= qdict_get_try_str(qdict
, "device");
1150 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1151 if (gdbserver_start(device
) < 0) {
1152 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1154 } else if (strcmp(device
, "none") == 0) {
1155 monitor_printf(mon
, "Disabled gdbserver\n");
1157 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1162 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1164 const char *action
= qdict_get_str(qdict
, "action");
1165 if (select_watchdog_action(action
) == -1) {
1166 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1170 static void monitor_printc(Monitor
*mon
, int c
)
1172 monitor_printf(mon
, "'");
1175 monitor_printf(mon
, "\\'");
1178 monitor_printf(mon
, "\\\\");
1181 monitor_printf(mon
, "\\n");
1184 monitor_printf(mon
, "\\r");
1187 if (c
>= 32 && c
<= 126) {
1188 monitor_printf(mon
, "%c", c
);
1190 monitor_printf(mon
, "\\x%02x", c
);
1194 monitor_printf(mon
, "'");
1197 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1198 hwaddr addr
, int is_physical
)
1200 int l
, line_size
, i
, max_digits
, len
;
1204 if (format
== 'i') {
1207 CPUArchState
*env
= mon_get_cpu_env();
1210 } else if (wsize
== 4) {
1213 /* as default we use the current CS size */
1216 #ifdef TARGET_X86_64
1217 if ((env
->efer
& MSR_EFER_LMA
) &&
1218 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1222 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1228 CPUArchState
*env
= mon_get_cpu_env();
1229 flags
= msr_le
<< 16;
1230 flags
|= env
->bfd_mach
;
1232 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1236 len
= wsize
* count
;
1245 max_digits
= (wsize
* 8 + 2) / 3;
1249 max_digits
= (wsize
* 8) / 4;
1253 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1262 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1264 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1269 cpu_physical_memory_read(addr
, buf
, l
);
1271 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1272 monitor_printf(mon
, " Cannot access memory\n");
1281 v
= ldub_p(buf
+ i
);
1284 v
= lduw_p(buf
+ i
);
1287 v
= (uint32_t)ldl_p(buf
+ i
);
1293 monitor_printf(mon
, " ");
1296 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1299 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1302 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1305 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1308 monitor_printc(mon
, v
);
1313 monitor_printf(mon
, "\n");
1319 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1321 int count
= qdict_get_int(qdict
, "count");
1322 int format
= qdict_get_int(qdict
, "format");
1323 int size
= qdict_get_int(qdict
, "size");
1324 target_long addr
= qdict_get_int(qdict
, "addr");
1326 memory_dump(mon
, count
, format
, size
, addr
, 0);
1329 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1331 int count
= qdict_get_int(qdict
, "count");
1332 int format
= qdict_get_int(qdict
, "format");
1333 int size
= qdict_get_int(qdict
, "size");
1334 hwaddr addr
= qdict_get_int(qdict
, "addr");
1336 memory_dump(mon
, count
, format
, size
, addr
, 1);
1339 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1341 int format
= qdict_get_int(qdict
, "format");
1342 hwaddr val
= qdict_get_int(qdict
, "val");
1346 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1349 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1352 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1356 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1359 monitor_printc(mon
, val
);
1362 monitor_printf(mon
, "\n");
1365 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1369 uint32_t start
= qdict_get_int(qdict
, "start");
1370 uint32_t size
= qdict_get_int(qdict
, "size");
1373 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1374 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1375 MEMTXATTRS_UNSPECIFIED
, NULL
);
1376 /* BSD sum algorithm ('sum' Unix command) */
1377 sum
= (sum
>> 1) | (sum
<< 15);
1380 monitor_printf(mon
, "%05d\n", sum
);
1383 static int mouse_button_state
;
1385 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1387 int dx
, dy
, dz
, button
;
1388 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1389 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1390 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1392 dx
= strtol(dx_str
, NULL
, 0);
1393 dy
= strtol(dy_str
, NULL
, 0);
1394 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1395 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1398 dz
= strtol(dz_str
, NULL
, 0);
1400 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1401 qemu_input_queue_btn(NULL
, button
, true);
1402 qemu_input_event_sync();
1403 qemu_input_queue_btn(NULL
, button
, false);
1406 qemu_input_event_sync();
1409 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1411 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1412 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1413 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1414 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1416 int button_state
= qdict_get_int(qdict
, "button_state");
1418 if (mouse_button_state
== button_state
) {
1421 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1422 qemu_input_event_sync();
1423 mouse_button_state
= button_state
;
1426 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1428 int size
= qdict_get_int(qdict
, "size");
1429 int addr
= qdict_get_int(qdict
, "addr");
1430 int has_index
= qdict_haskey(qdict
, "index");
1435 int index
= qdict_get_int(qdict
, "index");
1436 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1444 val
= cpu_inb(addr
);
1448 val
= cpu_inw(addr
);
1452 val
= cpu_inl(addr
);
1456 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1457 suffix
, addr
, size
* 2, val
);
1460 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1462 int size
= qdict_get_int(qdict
, "size");
1463 int addr
= qdict_get_int(qdict
, "addr");
1464 int val
= qdict_get_int(qdict
, "val");
1466 addr
&= IOPORTS_MASK
;
1471 cpu_outb(addr
, val
);
1474 cpu_outw(addr
, val
);
1477 cpu_outl(addr
, val
);
1482 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1484 Error
*local_err
= NULL
;
1485 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1487 qemu_boot_set(bootdevice
, &local_err
);
1489 error_report_err(local_err
);
1491 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1495 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1497 mtree_info((fprintf_function
)monitor_printf
, mon
);
1500 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1506 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1507 query_numa_node_mem(node_mem
);
1508 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1509 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1510 monitor_printf(mon
, "node %d cpus:", i
);
1512 if (cpu
->numa_node
== i
) {
1513 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1516 monitor_printf(mon
, "\n");
1517 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1523 #ifdef CONFIG_PROFILER
1528 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1530 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1531 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1532 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1533 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1538 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1540 monitor_printf(mon
, "Internal profiler not compiled\n");
1544 /* Capture support */
1545 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1547 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1552 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1553 monitor_printf(mon
, "[%d]: ", i
);
1554 s
->ops
.info (s
->opaque
);
1558 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1561 int n
= qdict_get_int(qdict
, "n");
1564 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1566 s
->ops
.destroy (s
->opaque
);
1567 QLIST_REMOVE (s
, entries
);
1574 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1576 const char *path
= qdict_get_str(qdict
, "path");
1577 int has_freq
= qdict_haskey(qdict
, "freq");
1578 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1579 int has_bits
= qdict_haskey(qdict
, "bits");
1580 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1581 int has_channels
= qdict_haskey(qdict
, "nchannels");
1582 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1585 s
= g_malloc0 (sizeof (*s
));
1587 freq
= has_freq
? freq
: 44100;
1588 bits
= has_bits
? bits
: 16;
1589 nchannels
= has_channels
? nchannels
: 2;
1591 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1592 monitor_printf(mon
, "Failed to add wave capture\n");
1596 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1599 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1601 qemu_acl
*acl
= qemu_acl_find(name
);
1604 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1609 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1611 const char *aclname
= qdict_get_str(qdict
, "aclname");
1612 qemu_acl
*acl
= find_acl(mon
, aclname
);
1613 qemu_acl_entry
*entry
;
1617 monitor_printf(mon
, "policy: %s\n",
1618 acl
->defaultDeny
? "deny" : "allow");
1619 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1621 monitor_printf(mon
, "%d: %s %s\n", i
,
1622 entry
->deny
? "deny" : "allow", entry
->match
);
1627 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1629 const char *aclname
= qdict_get_str(qdict
, "aclname");
1630 qemu_acl
*acl
= find_acl(mon
, aclname
);
1633 qemu_acl_reset(acl
);
1634 monitor_printf(mon
, "acl: removed all rules\n");
1638 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1640 const char *aclname
= qdict_get_str(qdict
, "aclname");
1641 const char *policy
= qdict_get_str(qdict
, "policy");
1642 qemu_acl
*acl
= find_acl(mon
, aclname
);
1645 if (strcmp(policy
, "allow") == 0) {
1646 acl
->defaultDeny
= 0;
1647 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1648 } else if (strcmp(policy
, "deny") == 0) {
1649 acl
->defaultDeny
= 1;
1650 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1652 monitor_printf(mon
, "acl: unknown policy '%s', "
1653 "expected 'deny' or 'allow'\n", policy
);
1658 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1660 const char *aclname
= qdict_get_str(qdict
, "aclname");
1661 const char *match
= qdict_get_str(qdict
, "match");
1662 const char *policy
= qdict_get_str(qdict
, "policy");
1663 int has_index
= qdict_haskey(qdict
, "index");
1664 int index
= qdict_get_try_int(qdict
, "index", -1);
1665 qemu_acl
*acl
= find_acl(mon
, aclname
);
1669 if (strcmp(policy
, "allow") == 0) {
1671 } else if (strcmp(policy
, "deny") == 0) {
1674 monitor_printf(mon
, "acl: unknown policy '%s', "
1675 "expected 'deny' or 'allow'\n", policy
);
1679 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1681 ret
= qemu_acl_append(acl
, deny
, match
);
1683 monitor_printf(mon
, "acl: unable to add acl entry\n");
1685 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1689 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1691 const char *aclname
= qdict_get_str(qdict
, "aclname");
1692 const char *match
= qdict_get_str(qdict
, "match");
1693 qemu_acl
*acl
= find_acl(mon
, aclname
);
1697 ret
= qemu_acl_remove(acl
, match
);
1699 monitor_printf(mon
, "acl: no matching acl entry\n");
1701 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1705 void qmp_getfd(const char *fdname
, Error
**errp
)
1710 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1712 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1716 if (qemu_isdigit(fdname
[0])) {
1718 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1719 "a name not starting with a digit");
1723 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1724 if (strcmp(monfd
->name
, fdname
) != 0) {
1733 monfd
= g_malloc0(sizeof(mon_fd_t
));
1734 monfd
->name
= g_strdup(fdname
);
1737 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1740 void qmp_closefd(const char *fdname
, Error
**errp
)
1744 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1745 if (strcmp(monfd
->name
, fdname
) != 0) {
1749 QLIST_REMOVE(monfd
, next
);
1751 g_free(monfd
->name
);
1756 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1759 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1761 int saved_vm_running
= runstate_is_running();
1762 const char *name
= qdict_get_str(qdict
, "name");
1764 vm_stop(RUN_STATE_RESTORE_VM
);
1766 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1771 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1775 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1778 if (strcmp(monfd
->name
, fdname
) != 0) {
1784 /* caller takes ownership of fd */
1785 QLIST_REMOVE(monfd
, next
);
1786 g_free(monfd
->name
);
1792 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1796 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1798 MonFdsetFd
*mon_fdset_fd
;
1799 MonFdsetFd
*mon_fdset_fd_next
;
1801 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1802 if ((mon_fdset_fd
->removed
||
1803 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1804 runstate_is_running()) {
1805 close(mon_fdset_fd
->fd
);
1806 g_free(mon_fdset_fd
->opaque
);
1807 QLIST_REMOVE(mon_fdset_fd
, next
);
1808 g_free(mon_fdset_fd
);
1812 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1813 QLIST_REMOVE(mon_fdset
, next
);
1818 static void monitor_fdsets_cleanup(void)
1820 MonFdset
*mon_fdset
;
1821 MonFdset
*mon_fdset_next
;
1823 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1824 monitor_fdset_cleanup(mon_fdset
);
1828 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1829 const char *opaque
, Error
**errp
)
1832 Monitor
*mon
= cur_mon
;
1835 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1837 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1841 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1842 has_opaque
, opaque
, errp
);
1854 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1856 MonFdset
*mon_fdset
;
1857 MonFdsetFd
*mon_fdset_fd
;
1860 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1861 if (mon_fdset
->id
!= fdset_id
) {
1864 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1866 if (mon_fdset_fd
->fd
!= fd
) {
1869 mon_fdset_fd
->removed
= true;
1872 mon_fdset_fd
->removed
= true;
1875 if (has_fd
&& !mon_fdset_fd
) {
1878 monitor_fdset_cleanup(mon_fdset
);
1884 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1887 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1889 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1892 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1894 MonFdset
*mon_fdset
;
1895 MonFdsetFd
*mon_fdset_fd
;
1896 FdsetInfoList
*fdset_list
= NULL
;
1898 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1899 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1900 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1902 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1903 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1905 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1906 FdsetFdInfoList
*fdsetfd_info
;
1908 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1909 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1910 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1911 if (mon_fdset_fd
->opaque
) {
1912 fdsetfd_info
->value
->has_opaque
= true;
1913 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1915 fdsetfd_info
->value
->has_opaque
= false;
1918 fdsetfd_info
->next
= fdsetfd_list
;
1919 fdsetfd_list
= fdsetfd_info
;
1922 fdset_info
->value
->fds
= fdsetfd_list
;
1924 fdset_info
->next
= fdset_list
;
1925 fdset_list
= fdset_info
;
1931 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1932 bool has_opaque
, const char *opaque
,
1935 MonFdset
*mon_fdset
= NULL
;
1936 MonFdsetFd
*mon_fdset_fd
;
1940 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1941 /* Break if match found or match impossible due to ordering by ID */
1942 if (fdset_id
<= mon_fdset
->id
) {
1943 if (fdset_id
< mon_fdset
->id
) {
1951 if (mon_fdset
== NULL
) {
1952 int64_t fdset_id_prev
= -1;
1953 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1957 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1958 "a non-negative value");
1961 /* Use specified fdset ID */
1962 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1963 mon_fdset_cur
= mon_fdset
;
1964 if (fdset_id
< mon_fdset_cur
->id
) {
1969 /* Use first available fdset ID */
1970 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1971 mon_fdset_cur
= mon_fdset
;
1972 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1973 fdset_id_prev
= mon_fdset_cur
->id
;
1980 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1982 mon_fdset
->id
= fdset_id
;
1984 mon_fdset
->id
= fdset_id_prev
+ 1;
1987 /* The fdset list is ordered by fdset ID */
1988 if (!mon_fdset_cur
) {
1989 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1990 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1991 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1993 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1997 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1998 mon_fdset_fd
->fd
= fd
;
1999 mon_fdset_fd
->removed
= false;
2001 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2003 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2005 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2006 fdinfo
->fdset_id
= mon_fdset
->id
;
2007 fdinfo
->fd
= mon_fdset_fd
->fd
;
2012 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2015 MonFdset
*mon_fdset
;
2016 MonFdsetFd
*mon_fdset_fd
;
2019 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2020 if (mon_fdset
->id
!= fdset_id
) {
2023 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2024 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2025 if (mon_fd_flags
== -1) {
2029 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2030 return mon_fdset_fd
->fd
;
2042 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2044 MonFdset
*mon_fdset
;
2045 MonFdsetFd
*mon_fdset_fd_dup
;
2047 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2048 if (mon_fdset
->id
!= fdset_id
) {
2051 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2052 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2056 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2057 mon_fdset_fd_dup
->fd
= dup_fd
;
2058 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2064 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2066 MonFdset
*mon_fdset
;
2067 MonFdsetFd
*mon_fdset_fd_dup
;
2069 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2070 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2071 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2073 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2074 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2075 monitor_fdset_cleanup(mon_fdset
);
2079 return mon_fdset
->id
;
2087 int monitor_fdset_dup_fd_find(int dup_fd
)
2089 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2092 void monitor_fdset_dup_fd_remove(int dup_fd
)
2094 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2097 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2100 Error
*local_err
= NULL
;
2102 if (!qemu_isdigit(fdname
[0]) && mon
) {
2103 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2105 fd
= qemu_parse_fd(fdname
);
2107 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2112 error_propagate(errp
, local_err
);
2121 /* Please update hmp-commands.hx when adding or changing commands */
2122 static mon_cmd_t info_cmds
[] = {
2123 #include "hmp-commands-info.h"
2127 /* mon_cmds and info_cmds would be sorted at runtime */
2128 static mon_cmd_t mon_cmds
[] = {
2129 #include "hmp-commands.h"
2133 static const mon_cmd_t qmp_cmds
[] = {
2134 #include "qmp-commands-old.h"
2138 /*******************************************************************/
2140 static const char *pch
;
2141 static sigjmp_buf expr_env
;
2144 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2145 expr_error(Monitor
*mon
, const char *fmt
, ...)
2149 monitor_vprintf(mon
, fmt
, ap
);
2150 monitor_printf(mon
, "\n");
2152 siglongjmp(expr_env
, 1);
2155 /* return 0 if OK, -1 if not found */
2156 static int get_monitor_def(target_long
*pval
, const char *name
)
2158 const MonitorDef
*md
= target_monitor_defs();
2167 for(; md
->name
!= NULL
; md
++) {
2168 if (compare_cmd(name
, md
->name
)) {
2169 if (md
->get_value
) {
2170 *pval
= md
->get_value(md
, md
->offset
);
2172 CPUArchState
*env
= mon_get_cpu_env();
2173 ptr
= (uint8_t *)env
+ md
->offset
;
2176 *pval
= *(int32_t *)ptr
;
2179 *pval
= *(target_long
*)ptr
;
2190 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2192 *pval
= (target_long
) tmp
;
2198 static void next(void)
2202 while (qemu_isspace(*pch
))
2207 static int64_t expr_sum(Monitor
*mon
);
2209 static int64_t expr_unary(Monitor
*mon
)
2218 n
= expr_unary(mon
);
2222 n
= -expr_unary(mon
);
2226 n
= ~expr_unary(mon
);
2232 expr_error(mon
, "')' expected");
2239 expr_error(mon
, "character constant expected");
2243 expr_error(mon
, "missing terminating \' character");
2253 while ((*pch
>= 'a' && *pch
<= 'z') ||
2254 (*pch
>= 'A' && *pch
<= 'Z') ||
2255 (*pch
>= '0' && *pch
<= '9') ||
2256 *pch
== '_' || *pch
== '.') {
2257 if ((q
- buf
) < sizeof(buf
) - 1)
2261 while (qemu_isspace(*pch
))
2264 ret
= get_monitor_def(®
, buf
);
2266 expr_error(mon
, "unknown register");
2271 expr_error(mon
, "unexpected end of expression");
2276 n
= strtoull(pch
, &p
, 0);
2277 if (errno
== ERANGE
) {
2278 expr_error(mon
, "number too large");
2281 expr_error(mon
, "invalid char '%c' in expression", *p
);
2284 while (qemu_isspace(*pch
))
2292 static int64_t expr_prod(Monitor
*mon
)
2297 val
= expr_unary(mon
);
2300 if (op
!= '*' && op
!= '/' && op
!= '%')
2303 val2
= expr_unary(mon
);
2312 expr_error(mon
, "division by zero");
2323 static int64_t expr_logic(Monitor
*mon
)
2328 val
= expr_prod(mon
);
2331 if (op
!= '&' && op
!= '|' && op
!= '^')
2334 val2
= expr_prod(mon
);
2351 static int64_t expr_sum(Monitor
*mon
)
2356 val
= expr_logic(mon
);
2359 if (op
!= '+' && op
!= '-')
2362 val2
= expr_logic(mon
);
2371 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2374 if (sigsetjmp(expr_env
, 0)) {
2378 while (qemu_isspace(*pch
))
2380 *pval
= expr_sum(mon
);
2385 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2387 const char *p
= *pp
;
2391 d
= strtod(p
, &tailp
);
2393 monitor_printf(mon
, "Number expected\n");
2396 if (d
!= d
|| d
- d
!= 0) {
2397 /* NaN or infinity */
2398 monitor_printf(mon
, "Bad number\n");
2407 * Store the command-name in cmdname, and return a pointer to
2408 * the remaining of the command string.
2410 static const char *get_command_name(const char *cmdline
,
2411 char *cmdname
, size_t nlen
)
2414 const char *p
, *pstart
;
2417 while (qemu_isspace(*p
))
2422 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2427 memcpy(cmdname
, pstart
, len
);
2428 cmdname
[len
] = '\0';
2433 * Read key of 'type' into 'key' and return the current
2436 static char *key_get_info(const char *type
, char **key
)
2444 p
= strchr(type
, ':');
2451 str
= g_malloc(len
+ 1);
2452 memcpy(str
, type
, len
);
2459 static int default_fmt_format
= 'x';
2460 static int default_fmt_size
= 4;
2462 static int is_valid_option(const char *c
, const char *typestr
)
2470 typestr
= strstr(typestr
, option
);
2471 return (typestr
!= NULL
);
2474 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2475 const char *cmdname
)
2477 const mon_cmd_t
*cmd
;
2479 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2480 if (compare_cmd(cmdname
, cmd
->name
)) {
2488 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2490 return search_dispatch_table(qmp_cmds
, cmdname
);
2494 * Parse command name from @cmdp according to command table @table.
2495 * If blank, return NULL.
2496 * Else, if no valid command can be found, report to @mon, and return
2498 * Else, change @cmdp to point right behind the name, and return its
2499 * command table entry.
2500 * Do not assume the return value points into @table! It doesn't when
2501 * the command is found in a sub-command table.
2503 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2508 const mon_cmd_t
*cmd
;
2511 /* extract the command name */
2512 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2516 cmd
= search_dispatch_table(table
, cmdname
);
2518 monitor_printf(mon
, "unknown command: '%.*s'\n",
2519 (int)(p
- *cmdp
), *cmdp
);
2523 /* filter out following useless space */
2524 while (qemu_isspace(*p
)) {
2529 /* search sub command */
2530 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2531 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2538 * Parse arguments for @cmd.
2539 * If it can't be parsed, report to @mon, and return NULL.
2540 * Else, insert command arguments into a QDict, and return it.
2541 * Note: On success, caller has to free the QDict structure.
2544 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2546 const mon_cmd_t
*cmd
)
2548 const char *typestr
;
2551 const char *p
= *endp
;
2553 QDict
*qdict
= qdict_new();
2555 /* parse the parameters */
2556 typestr
= cmd
->args_type
;
2558 typestr
= key_get_info(typestr
, &key
);
2570 while (qemu_isspace(*p
))
2572 if (*typestr
== '?') {
2575 /* no optional string: NULL argument */
2579 ret
= get_str(buf
, sizeof(buf
), &p
);
2583 monitor_printf(mon
, "%s: filename expected\n",
2587 monitor_printf(mon
, "%s: block device name expected\n",
2591 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2596 qdict_put(qdict
, key
, qstring_from_str(buf
));
2601 QemuOptsList
*opts_list
;
2604 opts_list
= qemu_find_opts(key
);
2605 if (!opts_list
|| opts_list
->desc
->name
) {
2608 while (qemu_isspace(*p
)) {
2613 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2616 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2620 qemu_opts_to_qdict(opts
, qdict
);
2621 qemu_opts_del(opts
);
2626 int count
, format
, size
;
2628 while (qemu_isspace(*p
))
2634 if (qemu_isdigit(*p
)) {
2636 while (qemu_isdigit(*p
)) {
2637 count
= count
* 10 + (*p
- '0');
2675 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2676 monitor_printf(mon
, "invalid char in format: '%c'\n",
2681 format
= default_fmt_format
;
2682 if (format
!= 'i') {
2683 /* for 'i', not specifying a size gives -1 as size */
2685 size
= default_fmt_size
;
2686 default_fmt_size
= size
;
2688 default_fmt_format
= format
;
2691 format
= default_fmt_format
;
2692 if (format
!= 'i') {
2693 size
= default_fmt_size
;
2698 qdict_put(qdict
, "count", qint_from_int(count
));
2699 qdict_put(qdict
, "format", qint_from_int(format
));
2700 qdict_put(qdict
, "size", qint_from_int(size
));
2709 while (qemu_isspace(*p
))
2711 if (*typestr
== '?' || *typestr
== '.') {
2712 if (*typestr
== '?') {
2720 while (qemu_isspace(*p
))
2729 if (get_expr(mon
, &val
, &p
))
2731 /* Check if 'i' is greater than 32-bit */
2732 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2733 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2734 monitor_printf(mon
, "integer is for 32-bit values\n");
2736 } else if (c
== 'M') {
2738 monitor_printf(mon
, "enter a positive value\n");
2743 qdict_put(qdict
, key
, qint_from_int(val
));
2751 while (qemu_isspace(*p
)) {
2754 if (*typestr
== '?') {
2760 val
= qemu_strtosz(p
, &end
);
2762 monitor_printf(mon
, "invalid size\n");
2765 qdict_put(qdict
, key
, qint_from_int(val
));
2773 while (qemu_isspace(*p
))
2775 if (*typestr
== '?') {
2781 if (get_double(mon
, &val
, &p
) < 0) {
2784 if (p
[0] && p
[1] == 's') {
2787 val
/= 1e3
; p
+= 2; break;
2789 val
/= 1e6
; p
+= 2; break;
2791 val
/= 1e9
; p
+= 2; break;
2794 if (*p
&& !qemu_isspace(*p
)) {
2795 monitor_printf(mon
, "Unknown unit suffix\n");
2798 qdict_put(qdict
, key
, qfloat_from_double(val
));
2806 while (qemu_isspace(*p
)) {
2810 while (qemu_isgraph(*p
)) {
2813 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2815 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2818 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2821 qdict_put(qdict
, key
, qbool_from_bool(val
));
2826 const char *tmp
= p
;
2833 while (qemu_isspace(*p
))
2838 if(!is_valid_option(p
, typestr
)) {
2840 monitor_printf(mon
, "%s: unsupported option -%c\n",
2852 qdict_put(qdict
, key
, qbool_from_bool(true));
2859 /* package all remaining string */
2862 while (qemu_isspace(*p
)) {
2865 if (*typestr
== '?') {
2868 /* no remaining string: NULL argument */
2874 monitor_printf(mon
, "%s: string expected\n",
2878 qdict_put(qdict
, key
, qstring_from_str(p
));
2884 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2890 /* check that all arguments were parsed */
2891 while (qemu_isspace(*p
))
2894 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2907 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2910 const mon_cmd_t
*cmd
;
2912 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2917 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2919 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2924 cmd
->mhandler
.cmd(mon
, qdict
);
2928 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2930 const char *p
, *pstart
;
2939 p
= pstart
+ strlen(pstart
);
2941 if (len
> sizeof(cmd
) - 2)
2942 len
= sizeof(cmd
) - 2;
2943 memcpy(cmd
, pstart
, len
);
2945 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2946 readline_add_completion(mon
->rs
, cmd
);
2954 static void file_completion(Monitor
*mon
, const char *input
)
2959 char file
[1024], file_prefix
[1024];
2963 p
= strrchr(input
, '/');
2966 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2967 pstrcpy(path
, sizeof(path
), ".");
2969 input_path_len
= p
- input
+ 1;
2970 memcpy(path
, input
, input_path_len
);
2971 if (input_path_len
> sizeof(path
) - 1)
2972 input_path_len
= sizeof(path
) - 1;
2973 path
[input_path_len
] = '\0';
2974 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2977 ffs
= opendir(path
);
2986 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
2990 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2991 memcpy(file
, input
, input_path_len
);
2992 if (input_path_len
< sizeof(file
))
2993 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2995 /* stat the file to find out if it's a directory.
2996 * In that case add a slash to speed up typing long paths
2998 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
2999 pstrcat(file
, sizeof(file
), "/");
3001 readline_add_completion(mon
->rs
, file
);
3007 static const char *next_arg_type(const char *typestr
)
3009 const char *p
= strchr(typestr
, ':');
3010 return (p
!= NULL
? ++p
: typestr
);
3013 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3016 if (!str
|| !option
) {
3019 if (!strncmp(option
, str
, strlen(str
))) {
3020 readline_add_completion(rs
, option
);
3024 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3027 ChardevBackendInfoList
*list
, *start
;
3033 readline_set_completion_index(rs
, len
);
3035 start
= list
= qmp_query_chardev_backends(NULL
);
3037 const char *chr_name
= list
->value
->name
;
3039 if (!strncmp(chr_name
, str
, len
)) {
3040 readline_add_completion(rs
, chr_name
);
3044 qapi_free_ChardevBackendInfoList(start
);
3047 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3056 readline_set_completion_index(rs
, len
);
3057 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
3058 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3062 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3072 readline_set_completion_index(rs
, len
);
3073 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3076 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3078 name
= object_class_get_name(OBJECT_CLASS(dc
));
3080 if (!dc
->cannot_instantiate_with_device_add_yet
3081 && !strncmp(name
, str
, len
)) {
3082 readline_add_completion(rs
, name
);
3089 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3099 readline_set_completion_index(rs
, len
);
3100 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3104 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3105 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3106 readline_add_completion(rs
, name
);
3113 static void peripheral_device_del_completion(ReadLineState
*rs
,
3114 const char *str
, size_t len
)
3116 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3117 GSList
*list
, *item
;
3119 list
= qdev_build_hotpluggable_device_list(peripheral
);
3124 for (item
= list
; item
; item
= g_slist_next(item
)) {
3125 DeviceState
*dev
= item
->data
;
3127 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3128 readline_add_completion(rs
, dev
->id
);
3135 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3138 ChardevInfoList
*list
, *start
;
3144 readline_set_completion_index(rs
, len
);
3146 start
= list
= qmp_query_chardev(NULL
);
3148 ChardevInfo
*chr
= list
->value
;
3150 if (!strncmp(chr
->label
, str
, len
)) {
3151 readline_add_completion(rs
, chr
->label
);
3155 qapi_free_ChardevInfoList(start
);
3158 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3161 ChardevInfoList
*list
, *start
;
3164 readline_set_completion_index(rs
, len
);
3166 start
= list
= qmp_query_chardev(NULL
);
3168 ChardevInfo
*chr_info
= list
->value
;
3170 if (!strncmp(chr_info
->label
, str
, len
)) {
3171 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3172 if (chr
&& chr_is_ringbuf(chr
)) {
3173 readline_add_completion(rs
, chr_info
->label
);
3178 qapi_free_ChardevInfoList(start
);
3181 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3186 ringbuf_completion(rs
, str
);
3189 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3198 readline_set_completion_index(rs
, len
);
3199 peripheral_device_del_completion(rs
, str
, len
);
3202 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3204 ObjectPropertyInfoList
*list
, *start
;
3211 readline_set_completion_index(rs
, len
);
3213 start
= list
= qmp_qom_list("/objects", NULL
);
3215 ObjectPropertyInfo
*info
= list
->value
;
3217 if (!strncmp(info
->type
, "child<", 5)
3218 && !strncmp(info
->name
, str
, len
)) {
3219 readline_add_completion(rs
, info
->name
);
3223 qapi_free_ObjectPropertyInfoList(start
);
3226 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3235 sep
= strrchr(str
, '-');
3240 readline_set_completion_index(rs
, len
);
3241 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3242 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3243 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3248 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3253 readline_set_completion_index(rs
, len
);
3255 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3257 count
= qemu_find_net_clients_except(NULL
, ncs
,
3258 NET_CLIENT_OPTIONS_KIND_NONE
,
3260 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3261 const char *name
= ncs
[i
]->name
;
3262 if (!strncmp(str
, name
, len
)) {
3263 readline_add_completion(rs
, name
);
3266 } else if (nb_args
== 3) {
3267 add_completion_option(rs
, str
, "on");
3268 add_completion_option(rs
, str
, "off");
3272 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3275 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3282 readline_set_completion_index(rs
, len
);
3283 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
3285 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3287 const char *name
= ncs
[i
]->name
;
3288 if (strncmp(str
, name
, len
)) {
3291 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3293 readline_add_completion(rs
, name
);
3298 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3303 readline_set_completion_index(rs
, len
);
3306 for (id
= 0; id
< trace_event_count(); id
++) {
3307 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3308 if (!strncmp(str
, event_name
, len
)) {
3309 readline_add_completion(rs
, event_name
);
3312 } else if (nb_args
== 3) {
3313 add_completion_option(rs
, str
, "on");
3314 add_completion_option(rs
, str
, "off");
3318 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3325 readline_set_completion_index(rs
, strlen(str
));
3326 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3327 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3331 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3337 readline_set_completion_index(rs
, len
);
3340 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3341 const char *name
= MigrationCapability_lookup
[i
];
3342 if (!strncmp(str
, name
, len
)) {
3343 readline_add_completion(rs
, name
);
3346 } else if (nb_args
== 3) {
3347 add_completion_option(rs
, str
, "on");
3348 add_completion_option(rs
, str
, "off");
3352 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3358 readline_set_completion_index(rs
, len
);
3361 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3362 const char *name
= MigrationParameter_lookup
[i
];
3363 if (!strncmp(str
, name
, len
)) {
3364 readline_add_completion(rs
, name
);
3370 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3378 readline_set_completion_index(rs
, len
);
3379 for (i
= 0; host_net_devices
[i
]; i
++) {
3380 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3381 readline_add_completion(rs
, host_net_devices
[i
]);
3386 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3388 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3392 readline_set_completion_index(rs
, len
);
3394 count
= qemu_find_net_clients_except(NULL
, ncs
,
3395 NET_CLIENT_OPTIONS_KIND_NONE
,
3397 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3401 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3404 snprintf(name
, sizeof(name
), "%d", id
);
3405 if (!strncmp(str
, name
, len
)) {
3406 readline_add_completion(rs
, name
);
3410 } else if (nb_args
== 3) {
3411 count
= qemu_find_net_clients_except(NULL
, ncs
,
3412 NET_CLIENT_OPTIONS_KIND_NIC
,
3414 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3418 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
3419 net_hub_id_for_client(ncs
[i
], &id
)) {
3422 name
= ncs
[i
]->name
;
3423 if (!strncmp(str
, name
, len
)) {
3424 readline_add_completion(rs
, name
);
3431 static void vm_completion(ReadLineState
*rs
, const char *str
)
3434 BlockDriverState
*bs
;
3435 BdrvNextIterator
*it
= NULL
;
3438 readline_set_completion_index(rs
, len
);
3440 while ((it
= bdrv_next(it
, &bs
))) {
3441 SnapshotInfoList
*snapshots
, *snapshot
;
3442 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3445 aio_context_acquire(ctx
);
3446 if (bdrv_can_snapshot(bs
)) {
3447 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3449 aio_context_release(ctx
);
3454 snapshot
= snapshots
;
3456 char *completion
= snapshot
->value
->name
;
3457 if (!strncmp(str
, completion
, len
)) {
3458 readline_add_completion(rs
, completion
);
3460 completion
= snapshot
->value
->id
;
3461 if (!strncmp(str
, completion
, len
)) {
3462 readline_add_completion(rs
, completion
);
3464 snapshot
= snapshot
->next
;
3466 qapi_free_SnapshotInfoList(snapshots
);
3471 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3474 vm_completion(rs
, str
);
3478 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3481 vm_completion(rs
, str
);
3485 static void monitor_find_completion_by_table(Monitor
*mon
,
3486 const mon_cmd_t
*cmd_table
,
3490 const char *cmdname
;
3492 const char *ptype
, *str
, *name
;
3493 const mon_cmd_t
*cmd
;
3494 BlockBackend
*blk
= NULL
;
3497 /* command completion */
3502 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3503 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3504 cmd_completion(mon
, cmdname
, cmd
->name
);
3507 /* find the command */
3508 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3509 if (compare_cmd(args
[0], cmd
->name
)) {
3517 if (cmd
->sub_table
) {
3518 /* do the job again */
3519 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3520 &args
[1], nb_args
- 1);
3523 if (cmd
->command_completion
) {
3524 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3528 ptype
= next_arg_type(cmd
->args_type
);
3529 for(i
= 0; i
< nb_args
- 2; i
++) {
3530 if (*ptype
!= '\0') {
3531 ptype
= next_arg_type(ptype
);
3532 while (*ptype
== '?')
3533 ptype
= next_arg_type(ptype
);
3536 str
= args
[nb_args
- 1];
3537 while (*ptype
== '-' && ptype
[1] != '\0') {
3538 ptype
= next_arg_type(ptype
);
3542 /* file completion */
3543 readline_set_completion_index(mon
->rs
, strlen(str
));
3544 file_completion(mon
, str
);
3547 /* block device name completion */
3548 readline_set_completion_index(mon
->rs
, strlen(str
));
3549 while ((blk
= blk_next(blk
)) != NULL
) {
3550 name
= blk_name(blk
);
3551 if (str
[0] == '\0' ||
3552 !strncmp(name
, str
, strlen(str
))) {
3553 readline_add_completion(mon
->rs
, name
);
3559 if (!strcmp(cmd
->name
, "help|?")) {
3560 monitor_find_completion_by_table(mon
, cmd_table
,
3561 &args
[1], nb_args
- 1);
3570 static void monitor_find_completion(void *opaque
,
3571 const char *cmdline
)
3573 Monitor
*mon
= opaque
;
3574 char *args
[MAX_ARGS
];
3577 /* 1. parse the cmdline */
3578 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3582 /* if the line ends with a space, it means we want to complete the
3584 len
= strlen(cmdline
);
3585 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3586 if (nb_args
>= MAX_ARGS
) {
3589 args
[nb_args
++] = g_strdup("");
3592 /* 2. auto complete according to args */
3593 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3596 free_cmdline_args(args
, nb_args
);
3599 static int monitor_can_read(void *opaque
)
3601 Monitor
*mon
= opaque
;
3603 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3606 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
3609 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
3611 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3612 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3613 "Capabilities negotiation is already complete, command "
3614 "'%s' ignored", cmd
->name
);
3617 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3618 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3619 "Expecting capabilities negotiation with "
3620 "'qmp_capabilities' before command '%s'", cmd
->name
);
3627 * Argument validation rules:
3629 * 1. The argument must exist in cmd_args qdict
3630 * 2. The argument type must be the expected one
3632 * Special case: If the argument doesn't exist in cmd_args and
3633 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3634 * checking is skipped for it.
3636 static void check_client_args_type(const QDict
*client_args
,
3637 const QDict
*cmd_args
, int flags
,
3640 const QDictEntry
*ent
;
3642 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3645 const QObject
*client_arg
= qdict_entry_value(ent
);
3646 const char *client_arg_name
= qdict_entry_key(ent
);
3648 obj
= qdict_get(cmd_args
, client_arg_name
);
3650 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3651 /* handler accepts unknowns */
3654 /* client arg doesn't exist */
3655 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3659 arg_type
= qobject_to_qstring(obj
);
3660 assert(arg_type
!= NULL
);
3662 /* check if argument's type is correct */
3663 switch (qstring_get_str(arg_type
)[0]) {
3667 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3668 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3669 client_arg_name
, "string");
3677 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3678 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3679 client_arg_name
, "int");
3684 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3685 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3686 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3687 client_arg_name
, "number");
3693 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3694 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3695 client_arg_name
, "bool");
3700 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3703 /* Any QObject can be passed. */
3708 * These types are not supported by QMP and thus are not
3709 * handled here. Fall through.
3718 * - Check if the client has passed all mandatory args
3719 * - Set special flags for argument validation
3721 static void check_mandatory_args(const QDict
*cmd_args
,
3722 const QDict
*client_args
, int *flags
,
3725 const QDictEntry
*ent
;
3727 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3728 const char *cmd_arg_name
= qdict_entry_key(ent
);
3729 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3730 assert(type
!= NULL
);
3732 if (qstring_get_str(type
)[0] == 'O') {
3733 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3734 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3735 } else if (qstring_get_str(type
)[0] != '-' &&
3736 qstring_get_str(type
)[1] != '?' &&
3737 !qdict_haskey(client_args
, cmd_arg_name
)) {
3738 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3744 static QDict
*qdict_from_args_type(const char *args_type
)
3748 QString
*key
, *type
, *cur_qs
;
3750 assert(args_type
!= NULL
);
3752 qdict
= qdict_new();
3754 if (args_type
== NULL
|| args_type
[0] == '\0') {
3755 /* no args, empty qdict */
3759 key
= qstring_new();
3760 type
= qstring_new();
3765 switch (args_type
[i
]) {
3768 qdict_put(qdict
, qstring_get_str(key
), type
);
3770 if (args_type
[i
] == '\0') {
3773 type
= qstring_new(); /* qdict has ref */
3774 cur_qs
= key
= qstring_new();
3780 qstring_append_chr(cur_qs
, args_type
[i
]);
3790 * Client argument checking rules:
3792 * 1. Client must provide all mandatory arguments
3793 * 2. Each argument provided by the client must be expected
3794 * 3. Each argument provided by the client must have the type expected
3797 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3804 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3807 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3812 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3815 error_propagate(errp
, err
);
3820 * Input object checking rules
3822 * 1. Input object must be a dict
3823 * 2. The "execute" key must exist
3824 * 3. The "execute" key must be a string
3825 * 4. If the "arguments" key exists, it must be a dict
3826 * 5. If the "id" key exists, it can be anything (ie. json-value)
3827 * 6. Any argument not listed above is considered invalid
3829 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3831 const QDictEntry
*ent
;
3832 int has_exec_key
= 0;
3835 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3836 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3840 input_dict
= qobject_to_qdict(input_obj
);
3842 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3843 const char *arg_name
= qdict_entry_key(ent
);
3844 const QObject
*arg_obj
= qdict_entry_value(ent
);
3846 if (!strcmp(arg_name
, "execute")) {
3847 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3848 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3849 "execute", "string");
3853 } else if (!strcmp(arg_name
, "arguments")) {
3854 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3855 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3856 "arguments", "object");
3859 } else if (!strcmp(arg_name
, "id")) {
3860 /* Any string is acceptable as "id", so nothing to check */
3862 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3867 if (!has_exec_key
) {
3868 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3875 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3877 Error
*local_err
= NULL
;
3878 QObject
*obj
, *data
;
3879 QDict
*input
, *args
;
3880 const mon_cmd_t
*cmd
;
3881 const char *cmd_name
;
3882 Monitor
*mon
= cur_mon
;
3884 args
= input
= NULL
;
3887 obj
= json_parser_parse(tokens
, NULL
);
3889 // FIXME: should be triggered in json_parser_parse()
3890 error_setg(&local_err
, QERR_JSON_PARSING
);
3894 input
= qmp_check_input_obj(obj
, &local_err
);
3896 qobject_decref(obj
);
3900 mon
->qmp
.id
= qdict_get(input
, "id");
3901 qobject_incref(mon
->qmp
.id
);
3903 cmd_name
= qdict_get_str(input
, "execute");
3904 trace_handle_qmp_command(mon
, cmd_name
);
3905 cmd
= qmp_find_cmd(cmd_name
);
3907 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3908 "The command %s has not been found", cmd_name
);
3911 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
3915 obj
= qdict_get(input
, "arguments");
3919 args
= qobject_to_qdict(obj
);
3923 qmp_check_client_args(cmd
, args
, &local_err
);
3928 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
3931 monitor_protocol_emitter(mon
, data
, local_err
);
3932 qobject_decref(data
);
3933 error_free(local_err
);
3938 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3940 Monitor
*old_mon
= cur_mon
;
3944 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3949 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3951 Monitor
*old_mon
= cur_mon
;
3957 for (i
= 0; i
< size
; i
++)
3958 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3960 if (size
== 0 || buf
[size
- 1] != 0)
3961 monitor_printf(cur_mon
, "corrupted command\n");
3963 handle_hmp_command(cur_mon
, (char *)buf
);
3969 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3970 void *readline_opaque
)
3972 Monitor
*mon
= opaque
;
3974 monitor_suspend(mon
);
3975 handle_hmp_command(mon
, cmdline
);
3976 monitor_resume(mon
);
3979 int monitor_suspend(Monitor
*mon
)
3987 void monitor_resume(Monitor
*mon
)
3991 if (--mon
->suspend_cnt
== 0)
3992 readline_show_prompt(mon
->rs
);
3995 static QObject
*get_qmp_greeting(void)
3997 QObject
*ver
= NULL
;
3999 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4000 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
4003 static void monitor_qmp_event(void *opaque
, int event
)
4006 Monitor
*mon
= opaque
;
4009 case CHR_EVENT_OPENED
:
4010 mon
->qmp
.in_command_mode
= false;
4011 data
= get_qmp_greeting();
4012 monitor_json_emitter(mon
, data
);
4013 qobject_decref(data
);
4016 case CHR_EVENT_CLOSED
:
4017 json_message_parser_destroy(&mon
->qmp
.parser
);
4018 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4020 monitor_fdsets_cleanup();
4025 static void monitor_event(void *opaque
, int event
)
4027 Monitor
*mon
= opaque
;
4030 case CHR_EVENT_MUX_IN
:
4031 qemu_mutex_lock(&mon
->out_lock
);
4033 qemu_mutex_unlock(&mon
->out_lock
);
4034 if (mon
->reset_seen
) {
4035 readline_restart(mon
->rs
);
4036 monitor_resume(mon
);
4039 mon
->suspend_cnt
= 0;
4043 case CHR_EVENT_MUX_OUT
:
4044 if (mon
->reset_seen
) {
4045 if (mon
->suspend_cnt
== 0) {
4046 monitor_printf(mon
, "\n");
4049 monitor_suspend(mon
);
4053 qemu_mutex_lock(&mon
->out_lock
);
4055 qemu_mutex_unlock(&mon
->out_lock
);
4058 case CHR_EVENT_OPENED
:
4059 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4060 "information\n", QEMU_VERSION
);
4061 if (!mon
->mux_out
) {
4062 readline_restart(mon
->rs
);
4063 readline_show_prompt(mon
->rs
);
4065 mon
->reset_seen
= 1;
4069 case CHR_EVENT_CLOSED
:
4071 monitor_fdsets_cleanup();
4077 compare_mon_cmd(const void *a
, const void *b
)
4079 return strcmp(((const mon_cmd_t
*)a
)->name
,
4080 ((const mon_cmd_t
*)b
)->name
);
4083 static void sortcmdlist(void)
4086 int elem_size
= sizeof(mon_cmd_t
);
4088 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4089 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4091 array_num
= sizeof(info_cmds
)/elem_size
-1;
4092 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4104 /* These functions just adapt the readline interface in a typesafe way. We
4105 * could cast function pointers but that discards compiler checks.
4107 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4108 const char *fmt
, ...)
4112 monitor_vprintf(opaque
, fmt
, ap
);
4116 static void monitor_readline_flush(void *opaque
)
4118 monitor_flush(opaque
);
4121 static void __attribute__((constructor
)) monitor_lock_init(void)
4123 qemu_mutex_init(&monitor_lock
);
4126 void monitor_init(CharDriverState
*chr
, int flags
)
4128 static int is_first_init
= 1;
4131 if (is_first_init
) {
4132 monitor_qapi_event_init();
4137 mon
= g_malloc(sizeof(*mon
));
4138 monitor_data_init(mon
);
4142 if (flags
& MONITOR_USE_READLINE
) {
4143 mon
->rs
= readline_init(monitor_readline_printf
,
4144 monitor_readline_flush
,
4146 monitor_find_completion
);
4147 monitor_read_command(mon
, 0);
4150 if (monitor_is_qmp(mon
)) {
4151 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4152 monitor_qmp_event
, mon
);
4153 qemu_chr_fe_set_echo(chr
, true);
4154 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4156 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4157 monitor_event
, mon
);
4160 qemu_mutex_lock(&monitor_lock
);
4161 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4162 qemu_mutex_unlock(&monitor_lock
);
4165 static void bdrv_password_cb(void *opaque
, const char *password
,
4166 void *readline_opaque
)
4168 Monitor
*mon
= opaque
;
4169 BlockDriverState
*bs
= readline_opaque
;
4171 Error
*local_err
= NULL
;
4173 bdrv_add_key(bs
, password
, &local_err
);
4175 error_report_err(local_err
);
4178 if (mon
->password_completion_cb
)
4179 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4181 monitor_read_command(mon
, 1);
4184 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4185 BlockCompletionFunc
*completion_cb
,
4190 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4191 bdrv_get_encrypted_filename(bs
));
4193 mon
->password_completion_cb
= completion_cb
;
4194 mon
->password_opaque
= opaque
;
4196 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4198 if (err
&& completion_cb
)
4199 completion_cb(opaque
, err
);
4204 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4205 BlockCompletionFunc
*completion_cb
,
4211 blk
= blk_by_name(device
);
4213 monitor_printf(mon
, "Device not found %s\n", device
);
4217 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4221 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4224 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4227 if (completion_cb
) {
4228 completion_cb(opaque
, 0);
4233 QemuOptsList qemu_mon_opts
= {
4235 .implied_opt_name
= "chardev",
4236 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4240 .type
= QEMU_OPT_STRING
,
4243 .type
= QEMU_OPT_STRING
,
4246 .type
= QEMU_OPT_BOOL
,
4249 .type
= QEMU_OPT_BOOL
,
4251 { /* end of list */ }
4256 void qmp_rtc_reset_reinjection(Error
**errp
)
4258 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4262 #ifndef TARGET_S390X
4263 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4265 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4270 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4272 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");