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"
27 #include "monitor/qdev.h"
29 #include "hw/i386/pc.h"
30 #include "hw/pci/pci.h"
31 #include "sysemu/watchdog.h"
32 #include "hw/loader.h"
33 #include "exec/gdbstub.h"
35 #include "net/slirp.h"
36 #include "sysemu/char.h"
37 #include "ui/qemu-spice.h"
38 #include "sysemu/sysemu.h"
39 #include "sysemu/numa.h"
40 #include "monitor/monitor.h"
41 #include "qemu/readline.h"
42 #include "ui/console.h"
44 #include "sysemu/blockdev.h"
45 #include "sysemu/block-backend.h"
46 #include "audio/audio.h"
47 #include "disas/disas.h"
48 #include "sysemu/balloon.h"
49 #include "qemu/timer.h"
50 #include "migration/migration.h"
51 #include "sysemu/kvm.h"
53 #include "sysemu/tpm.h"
54 #include "qapi/qmp/qerror.h"
55 #include "qapi/qmp/qint.h"
56 #include "qapi/qmp/qfloat.h"
57 #include "qapi/qmp/qlist.h"
58 #include "qapi/qmp/qbool.h"
59 #include "qapi/qmp/qstring.h"
60 #include "qapi/qmp/qjson.h"
61 #include "qapi/qmp/json-streamer.h"
62 #include "qapi/qmp/json-parser.h"
63 #include <qom/object_interfaces.h>
66 #include "trace/control.h"
67 #include "monitor/hmp-target.h"
68 #ifdef CONFIG_TRACE_SIMPLE
69 #include "trace/simple.h"
71 #include "exec/memory.h"
72 #include "qmp-commands.h"
74 #include "qemu/thread.h"
75 #include "block/qapi.h"
76 #include "qapi/qmp-event.h"
77 #include "qapi-event.h"
78 #include "qmp-introspect.h"
79 #include "sysemu/block-backend.h"
80 #include "sysemu/qtest.h"
82 /* for hmp_info_irq/pic */
83 #if defined(TARGET_SPARC)
84 #include "hw/sparc/sun4m.h"
86 #include "hw/lm32/lm32_pic.h"
88 #if defined(TARGET_S390X)
89 #include "hw/s390x/storage-keys.h"
96 * 'B' block device name
97 * 's' string (accept optional quote)
98 * 'S' it just appends the rest of the string (accept optional quote)
99 * 'O' option string of the form NAME=VALUE,...
100 * parsed according to QemuOptsList given by its name
101 * Example: 'device:O' uses qemu_device_opts.
102 * Restriction: only lists with empty desc are supported
103 * TODO lift the restriction
105 * 'l' target long (32 or 64 bit)
106 * 'M' Non-negative target long (32 or 64 bit), in user mode the
107 * value is multiplied by 2^20 (think Mebibyte)
108 * 'o' octets (aka bytes)
109 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
110 * K, k suffix, which multiplies the value by 2^60 for suffixes E
111 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
112 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
114 * user mode accepts an optional ms, us, ns suffix,
115 * which divides the value by 1e3, 1e6, 1e9, respectively
116 * '/' optional gdb-like print format (like "/10x")
118 * '?' optional type (for all types, except '/')
119 * '.' other form of optional type (for 'i' and 'l')
121 * user mode accepts "on" or "off"
122 * '-' optional parameter (eg. '-f')
126 typedef struct mon_cmd_t
{
128 const char *args_type
;
132 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
133 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
135 /* @sub_table is a list of 2nd level of commands. If it do not exist,
136 * mhandler should be used. If it exist, sub_table[?].mhandler should be
137 * used, and mhandler of 1st level plays the role of help function.
139 struct mon_cmd_t
*sub_table
;
140 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
143 /* file descriptors passed via SCM_RIGHTS */
144 typedef struct mon_fd_t mon_fd_t
;
148 QLIST_ENTRY(mon_fd_t
) next
;
151 /* file descriptor associated with a file descriptor set */
152 typedef struct MonFdsetFd MonFdsetFd
;
157 QLIST_ENTRY(MonFdsetFd
) next
;
160 /* file descriptor set containing fds passed via SCM_RIGHTS */
161 typedef struct MonFdset MonFdset
;
164 QLIST_HEAD(, MonFdsetFd
) fds
;
165 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
166 QLIST_ENTRY(MonFdset
) next
;
171 JSONMessageParser parser
;
173 * When a client connects, we're in capabilities negotiation mode.
174 * When command qmp_capabilities succeeds, we go into command
177 bool in_command_mode
; /* are we in command mode? */
181 * To prevent flooding clients, events can be throttled. The
182 * throttling is calculated globally, rather than per-Monitor
185 typedef struct MonitorQAPIEventState
{
186 QAPIEvent event
; /* Throttling state for this event type and... */
187 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
188 QEMUTimer
*timer
; /* Timer for handling delayed events */
189 QDict
*qdict
; /* Delayed event (if any) */
190 } MonitorQAPIEventState
;
193 int64_t rate
; /* Minimum time (in ns) between two events */
194 } MonitorQAPIEventConf
;
197 CharDriverState
*chr
;
207 /* Read under either BQL or out_lock, written with BQL+out_lock. */
213 BlockCompletionFunc
*password_completion_cb
;
214 void *password_opaque
;
215 mon_cmd_t
*cmd_table
;
216 QLIST_HEAD(,mon_fd_t
) fds
;
217 QLIST_ENTRY(Monitor
) entry
;
220 /* QMP checker flags */
221 #define QMP_ACCEPT_UNKNOWNS 1
223 /* Protects mon_list, monitor_event_state. */
224 static QemuMutex monitor_lock
;
226 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
227 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
228 static int mon_refcount
;
230 static mon_cmd_t mon_cmds
[];
231 static mon_cmd_t info_cmds
[];
233 static const mon_cmd_t qmp_cmds
[];
237 static QEMUClockType event_clock_type
= QEMU_CLOCK_REALTIME
;
239 static void monitor_command_cb(void *opaque
, const char *cmdline
,
240 void *readline_opaque
);
243 * Is @mon a QMP monitor?
245 static inline bool monitor_is_qmp(const Monitor
*mon
)
247 return (mon
->flags
& MONITOR_USE_CONTROL
);
251 * Is the current monitor, if any, a QMP monitor?
253 bool monitor_cur_is_qmp(void)
255 return cur_mon
&& monitor_is_qmp(cur_mon
);
258 void monitor_read_command(Monitor
*mon
, int show_prompt
)
263 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
265 readline_show_prompt(mon
->rs
);
268 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
272 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
273 /* prompt is printed on return from the command handler */
276 monitor_printf(mon
, "terminal does not support password prompting\n");
281 static void monitor_flush_locked(Monitor
*mon
);
283 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
286 Monitor
*mon
= opaque
;
288 qemu_mutex_lock(&mon
->out_lock
);
290 monitor_flush_locked(mon
);
291 qemu_mutex_unlock(&mon
->out_lock
);
295 /* Called with mon->out_lock held. */
296 static void monitor_flush_locked(Monitor
*mon
)
302 if (mon
->skip_flush
) {
306 buf
= qstring_get_str(mon
->outbuf
);
307 len
= qstring_get_length(mon
->outbuf
);
309 if (len
&& !mon
->mux_out
) {
310 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
311 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
312 /* all flushed or error */
313 QDECREF(mon
->outbuf
);
314 mon
->outbuf
= qstring_new();
319 QString
*tmp
= qstring_from_str(buf
+ rc
);
320 QDECREF(mon
->outbuf
);
323 if (mon
->out_watch
== 0) {
324 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
325 monitor_unblocked
, mon
);
330 void monitor_flush(Monitor
*mon
)
332 qemu_mutex_lock(&mon
->out_lock
);
333 monitor_flush_locked(mon
);
334 qemu_mutex_unlock(&mon
->out_lock
);
337 /* flush at every end of line */
338 static void monitor_puts(Monitor
*mon
, const char *str
)
342 qemu_mutex_lock(&mon
->out_lock
);
348 qstring_append_chr(mon
->outbuf
, '\r');
350 qstring_append_chr(mon
->outbuf
, c
);
352 monitor_flush_locked(mon
);
355 qemu_mutex_unlock(&mon
->out_lock
);
358 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
365 if (monitor_is_qmp(mon
)) {
369 buf
= g_strdup_vprintf(fmt
, ap
);
370 monitor_puts(mon
, buf
);
374 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
378 monitor_vprintf(mon
, fmt
, ap
);
382 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
386 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
391 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
395 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
396 qobject_to_json(data
);
397 assert(json
!= NULL
);
399 qstring_append_chr(json
, '\n');
400 monitor_puts(mon
, qstring_get_str(json
));
405 static QDict
*build_qmp_error_dict(Error
*err
)
409 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
410 QapiErrorClass_lookup
[error_get_class(err
)],
411 error_get_pretty(err
));
413 return qobject_to_qdict(obj
);
416 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
421 trace_monitor_protocol_emitter(mon
);
424 /* success response */
427 qobject_incref(data
);
428 qdict_put_obj(qmp
, "return", data
);
430 /* return an empty QDict by default */
431 qdict_put(qmp
, "return", qdict_new());
435 qmp
= build_qmp_error_dict(err
);
439 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
443 monitor_json_emitter(mon
, QOBJECT(qmp
));
448 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
449 /* Limit guest-triggerable events to 1 per second */
450 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
451 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
452 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
453 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
454 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
455 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
458 GHashTable
*monitor_qapi_event_state
;
461 * Emits the event to every monitor instance, @event is only used for trace
462 * Called with monitor_lock held.
464 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
468 trace_monitor_protocol_event_emit(event
, qdict
);
469 QLIST_FOREACH(mon
, &mon_list
, entry
) {
470 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
471 monitor_json_emitter(mon
, QOBJECT(qdict
));
476 static void monitor_qapi_event_handler(void *opaque
);
479 * Queue a new event for emission to Monitor instances,
480 * applying any rate limiting if required.
483 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
485 MonitorQAPIEventConf
*evconf
;
486 MonitorQAPIEventState
*evstate
;
488 assert(event
< QAPI_EVENT__MAX
);
489 evconf
= &monitor_qapi_event_conf
[event
];
490 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
492 qemu_mutex_lock(&monitor_lock
);
495 /* Unthrottled event */
496 monitor_qapi_event_emit(event
, qdict
);
498 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
499 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
501 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
502 assert(!evstate
|| timer_pending(evstate
->timer
));
506 * Timer is pending for (at least) evconf->rate ns after
507 * last send. Store event for sending when timer fires,
508 * replacing a prior stored event if any.
510 QDECREF(evstate
->qdict
);
511 evstate
->qdict
= qdict
;
512 QINCREF(evstate
->qdict
);
515 * Last send was (at least) evconf->rate ns ago.
516 * Send immediately, and arm the timer to call
517 * monitor_qapi_event_handler() in evconf->rate ns. Any
518 * events arriving before then will be delayed until then.
520 int64_t now
= qemu_clock_get_ns(event_clock_type
);
522 monitor_qapi_event_emit(event
, qdict
);
524 evstate
= g_new(MonitorQAPIEventState
, 1);
525 evstate
->event
= event
;
526 evstate
->data
= data
;
527 QINCREF(evstate
->data
);
528 evstate
->qdict
= NULL
;
529 evstate
->timer
= timer_new_ns(event_clock_type
,
530 monitor_qapi_event_handler
,
532 g_hash_table_add(monitor_qapi_event_state
, evstate
);
533 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
537 qemu_mutex_unlock(&monitor_lock
);
541 * This function runs evconf->rate ns after sending a throttled
543 * If another event has since been stored, send it.
545 static void monitor_qapi_event_handler(void *opaque
)
547 MonitorQAPIEventState
*evstate
= opaque
;
548 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
550 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
551 qemu_mutex_lock(&monitor_lock
);
553 if (evstate
->qdict
) {
554 int64_t now
= qemu_clock_get_ns(event_clock_type
);
556 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
557 QDECREF(evstate
->qdict
);
558 evstate
->qdict
= NULL
;
559 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
561 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
562 QDECREF(evstate
->data
);
563 timer_free(evstate
->timer
);
567 qemu_mutex_unlock(&monitor_lock
);
570 static unsigned int qapi_event_throttle_hash(const void *key
)
572 const MonitorQAPIEventState
*evstate
= key
;
573 unsigned int hash
= evstate
->event
* 255;
575 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
576 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
579 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
580 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
586 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
588 const MonitorQAPIEventState
*eva
= a
;
589 const MonitorQAPIEventState
*evb
= b
;
591 if (eva
->event
!= evb
->event
) {
595 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
596 return !strcmp(qdict_get_str(eva
->data
, "id"),
597 qdict_get_str(evb
->data
, "id"));
600 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
601 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
602 qdict_get_str(evb
->data
, "node-name"));
608 static void monitor_qapi_event_init(void)
610 if (qtest_enabled()) {
611 event_clock_type
= QEMU_CLOCK_VIRTUAL
;
614 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
615 qapi_event_throttle_equal
);
616 qmp_event_set_func_emit(monitor_qapi_event_queue
);
619 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
621 cur_mon
->qmp
.in_command_mode
= true;
624 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
626 static void monitor_data_init(Monitor
*mon
)
628 memset(mon
, 0, sizeof(Monitor
));
629 qemu_mutex_init(&mon
->out_lock
);
630 mon
->outbuf
= qstring_new();
631 /* Use *mon_cmds by default. */
632 mon
->cmd_table
= mon_cmds
;
635 static void monitor_data_destroy(Monitor
*mon
)
637 QDECREF(mon
->outbuf
);
638 qemu_mutex_destroy(&mon
->out_lock
);
641 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
642 int64_t cpu_index
, Error
**errp
)
645 Monitor
*old_mon
, hmp
;
647 monitor_data_init(&hmp
);
648 hmp
.skip_flush
= true;
654 int ret
= monitor_set_cpu(cpu_index
);
657 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
663 handle_hmp_command(&hmp
, command_line
);
666 qemu_mutex_lock(&hmp
.out_lock
);
667 if (qstring_get_length(hmp
.outbuf
) > 0) {
668 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
670 output
= g_strdup("");
672 qemu_mutex_unlock(&hmp
.out_lock
);
675 monitor_data_destroy(&hmp
);
679 static int compare_cmd(const char *name
, const char *list
)
681 const char *p
, *pstart
;
689 p
= pstart
+ strlen(pstart
);
690 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
699 static int get_str(char *buf
, int buf_size
, const char **pp
)
707 while (qemu_isspace(*p
)) {
718 while (*p
!= '\0' && *p
!= '\"') {
734 printf("unsupported escape code: '\\%c'\n", c
);
737 if ((q
- buf
) < buf_size
- 1) {
741 if ((q
- buf
) < buf_size
- 1) {
748 printf("unterminated string\n");
753 while (*p
!= '\0' && !qemu_isspace(*p
)) {
754 if ((q
- buf
) < buf_size
- 1) {
767 static void free_cmdline_args(char **args
, int nb_args
)
771 assert(nb_args
<= MAX_ARGS
);
773 for (i
= 0; i
< nb_args
; i
++) {
780 * Parse the command line to get valid args.
781 * @cmdline: command line to be parsed.
782 * @pnb_args: location to store the number of args, must NOT be NULL.
783 * @args: location to store the args, which should be freed by caller, must
786 * Returns 0 on success, negative on failure.
788 * NOTE: this parser is an approximate form of the real command parser. Number
789 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
790 * return with failure.
792 static int parse_cmdline(const char *cmdline
,
793 int *pnb_args
, char **args
)
802 while (qemu_isspace(*p
)) {
808 if (nb_args
>= MAX_ARGS
) {
811 ret
= get_str(buf
, sizeof(buf
), &p
);
815 args
[nb_args
] = g_strdup(buf
);
822 free_cmdline_args(args
, nb_args
);
826 static void help_cmd_dump_one(Monitor
*mon
,
827 const mon_cmd_t
*cmd
,
833 for (i
= 0; i
< prefix_args_nb
; i
++) {
834 monitor_printf(mon
, "%s ", prefix_args
[i
]);
836 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
839 /* @args[@arg_index] is the valid command need to find in @cmds */
840 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
841 char **args
, int nb_args
, int arg_index
)
843 const mon_cmd_t
*cmd
;
845 /* No valid arg need to compare with, dump all in *cmds */
846 if (arg_index
>= nb_args
) {
847 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
848 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
853 /* Find one entry to dump */
854 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
855 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
856 if (cmd
->sub_table
) {
857 /* continue with next arg */
858 help_cmd_dump(mon
, cmd
->sub_table
,
859 args
, nb_args
, arg_index
+ 1);
861 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
868 static void help_cmd(Monitor
*mon
, const char *name
)
870 char *args
[MAX_ARGS
];
873 /* 1. parse user input */
875 /* special case for log, directly dump and return */
876 if (!strcmp(name
, "log")) {
877 const QEMULogItem
*item
;
878 monitor_printf(mon
, "Log items (comma separated):\n");
879 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
880 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
881 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
886 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
891 /* 2. dump the contents according to parsed args */
892 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
894 free_cmdline_args(args
, nb_args
);
897 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
899 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
902 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
904 const char *tp_name
= qdict_get_str(qdict
, "name");
905 bool new_state
= qdict_get_bool(qdict
, "option");
906 Error
*local_err
= NULL
;
908 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
910 error_report_err(local_err
);
914 #ifdef CONFIG_TRACE_SIMPLE
915 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
917 const char *op
= qdict_get_try_str(qdict
, "op");
918 const char *arg
= qdict_get_try_str(qdict
, "arg");
921 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
922 } else if (!strcmp(op
, "on")) {
923 st_set_trace_file_enabled(true);
924 } else if (!strcmp(op
, "off")) {
925 st_set_trace_file_enabled(false);
926 } else if (!strcmp(op
, "flush")) {
927 st_flush_trace_buffer();
928 } else if (!strcmp(op
, "set")) {
930 st_set_trace_file(arg
);
933 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
934 help_cmd(mon
, "trace-file");
939 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
941 help_cmd(mon
, "info");
944 CommandInfoList
*qmp_query_commands(Error
**errp
)
946 CommandInfoList
*info
, *cmd_list
= NULL
;
947 const mon_cmd_t
*cmd
;
949 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
950 info
= g_malloc0(sizeof(*info
));
951 info
->value
= g_malloc0(sizeof(*info
->value
));
952 info
->value
->name
= g_strdup(cmd
->name
);
954 info
->next
= cmd_list
;
961 EventInfoList
*qmp_query_events(Error
**errp
)
963 EventInfoList
*info
, *ev_list
= NULL
;
966 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
967 const char *event_name
= QAPIEvent_lookup
[e
];
968 assert(event_name
!= NULL
);
969 info
= g_malloc0(sizeof(*info
));
970 info
->value
= g_malloc0(sizeof(*info
->value
));
971 info
->value
->name
= g_strdup(event_name
);
973 info
->next
= ev_list
;
981 * Minor hack: generated marshalling suppressed for this command
982 * ('gen': false in the schema) so we can parse the JSON string
983 * directly into QObject instead of first parsing it with
984 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
985 * to QObject with generated output marshallers, every time. Instead,
986 * we do it in test-qmp-input-visitor.c, just to make sure
987 * qapi-introspect.py's output actually conforms to the schema.
989 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
992 *ret_data
= qobject_from_json(qmp_schema_json
);
995 /* set the current CPU defined by the user */
996 int monitor_set_cpu(int cpu_index
)
1000 cpu
= qemu_get_cpu(cpu_index
);
1004 cur_mon
->mon_cpu
= cpu
;
1008 CPUState
*mon_get_cpu(void)
1010 if (!cur_mon
->mon_cpu
) {
1013 cpu_synchronize_state(cur_mon
->mon_cpu
);
1014 return cur_mon
->mon_cpu
;
1017 CPUArchState
*mon_get_cpu_env(void)
1019 return mon_get_cpu()->env_ptr
;
1022 int monitor_get_cpu_index(void)
1024 return mon_get_cpu()->cpu_index
;
1027 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1029 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1032 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1034 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1035 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1038 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1040 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1043 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1052 str
= readline_get_history(mon
->rs
, i
);
1055 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1060 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1062 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1065 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1067 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1068 TraceEventInfoList
*elem
;
1070 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1071 monitor_printf(mon
, "%s : state %u\n",
1073 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1075 qapi_free_TraceEventInfoList(events
);
1078 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1079 bool has_port
, int64_t port
,
1080 bool has_tls_port
, int64_t tls_port
,
1081 bool has_cert_subject
, const char *cert_subject
,
1084 if (strcmp(protocol
, "spice") == 0) {
1085 if (!qemu_using_spice(errp
)) {
1089 if (!has_port
&& !has_tls_port
) {
1090 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1094 if (qemu_spice_migrate_info(hostname
,
1095 has_port
? port
: -1,
1096 has_tls_port
? tls_port
: -1,
1098 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1104 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1107 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1109 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1112 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1115 const char *items
= qdict_get_str(qdict
, "items");
1117 if (!strcmp(items
, "none")) {
1120 mask
= qemu_str_to_log_mask(items
);
1122 help_cmd(mon
, "log");
1129 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1131 const char *option
= qdict_get_try_str(qdict
, "option");
1132 if (!option
|| !strcmp(option
, "on")) {
1134 } else if (!strcmp(option
, "off")) {
1137 monitor_printf(mon
, "unexpected option %s\n", option
);
1141 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1143 const char *device
= qdict_get_try_str(qdict
, "device");
1145 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1146 if (gdbserver_start(device
) < 0) {
1147 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1149 } else if (strcmp(device
, "none") == 0) {
1150 monitor_printf(mon
, "Disabled gdbserver\n");
1152 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1157 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1159 const char *action
= qdict_get_str(qdict
, "action");
1160 if (select_watchdog_action(action
) == -1) {
1161 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1165 static void monitor_printc(Monitor
*mon
, int c
)
1167 monitor_printf(mon
, "'");
1170 monitor_printf(mon
, "\\'");
1173 monitor_printf(mon
, "\\\\");
1176 monitor_printf(mon
, "\\n");
1179 monitor_printf(mon
, "\\r");
1182 if (c
>= 32 && c
<= 126) {
1183 monitor_printf(mon
, "%c", c
);
1185 monitor_printf(mon
, "\\x%02x", c
);
1189 monitor_printf(mon
, "'");
1192 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1193 hwaddr addr
, int is_physical
)
1195 int l
, line_size
, i
, max_digits
, len
;
1199 if (format
== 'i') {
1202 CPUArchState
*env
= mon_get_cpu_env();
1205 } else if (wsize
== 4) {
1208 /* as default we use the current CS size */
1211 #ifdef TARGET_X86_64
1212 if ((env
->efer
& MSR_EFER_LMA
) &&
1213 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1217 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1223 CPUArchState
*env
= mon_get_cpu_env();
1224 flags
= msr_le
<< 16;
1225 flags
|= env
->bfd_mach
;
1227 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1231 len
= wsize
* count
;
1240 max_digits
= (wsize
* 8 + 2) / 3;
1244 max_digits
= (wsize
* 8) / 4;
1248 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1257 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1259 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1264 cpu_physical_memory_read(addr
, buf
, l
);
1266 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1267 monitor_printf(mon
, " Cannot access memory\n");
1276 v
= ldub_p(buf
+ i
);
1279 v
= lduw_p(buf
+ i
);
1282 v
= (uint32_t)ldl_p(buf
+ i
);
1288 monitor_printf(mon
, " ");
1291 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1294 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1297 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1300 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1303 monitor_printc(mon
, v
);
1308 monitor_printf(mon
, "\n");
1314 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1316 int count
= qdict_get_int(qdict
, "count");
1317 int format
= qdict_get_int(qdict
, "format");
1318 int size
= qdict_get_int(qdict
, "size");
1319 target_long addr
= qdict_get_int(qdict
, "addr");
1321 memory_dump(mon
, count
, format
, size
, addr
, 0);
1324 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1326 int count
= qdict_get_int(qdict
, "count");
1327 int format
= qdict_get_int(qdict
, "format");
1328 int size
= qdict_get_int(qdict
, "size");
1329 hwaddr addr
= qdict_get_int(qdict
, "addr");
1331 memory_dump(mon
, count
, format
, size
, addr
, 1);
1334 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1336 int format
= qdict_get_int(qdict
, "format");
1337 hwaddr val
= qdict_get_int(qdict
, "val");
1341 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1344 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1347 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1351 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1354 monitor_printc(mon
, val
);
1357 monitor_printf(mon
, "\n");
1360 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1364 uint32_t start
= qdict_get_int(qdict
, "start");
1365 uint32_t size
= qdict_get_int(qdict
, "size");
1368 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1369 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1370 MEMTXATTRS_UNSPECIFIED
, NULL
);
1371 /* BSD sum algorithm ('sum' Unix command) */
1372 sum
= (sum
>> 1) | (sum
<< 15);
1375 monitor_printf(mon
, "%05d\n", sum
);
1378 static int mouse_button_state
;
1380 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1382 int dx
, dy
, dz
, button
;
1383 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1384 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1385 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1387 dx
= strtol(dx_str
, NULL
, 0);
1388 dy
= strtol(dy_str
, NULL
, 0);
1389 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1390 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1393 dz
= strtol(dz_str
, NULL
, 0);
1395 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1396 qemu_input_queue_btn(NULL
, button
, true);
1397 qemu_input_event_sync();
1398 qemu_input_queue_btn(NULL
, button
, false);
1401 qemu_input_event_sync();
1404 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1406 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1407 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1408 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1409 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1411 int button_state
= qdict_get_int(qdict
, "button_state");
1413 if (mouse_button_state
== button_state
) {
1416 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1417 qemu_input_event_sync();
1418 mouse_button_state
= button_state
;
1421 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1423 int size
= qdict_get_int(qdict
, "size");
1424 int addr
= qdict_get_int(qdict
, "addr");
1425 int has_index
= qdict_haskey(qdict
, "index");
1430 int index
= qdict_get_int(qdict
, "index");
1431 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1439 val
= cpu_inb(addr
);
1443 val
= cpu_inw(addr
);
1447 val
= cpu_inl(addr
);
1451 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1452 suffix
, addr
, size
* 2, val
);
1455 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1457 int size
= qdict_get_int(qdict
, "size");
1458 int addr
= qdict_get_int(qdict
, "addr");
1459 int val
= qdict_get_int(qdict
, "val");
1461 addr
&= IOPORTS_MASK
;
1466 cpu_outb(addr
, val
);
1469 cpu_outw(addr
, val
);
1472 cpu_outl(addr
, val
);
1477 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1479 Error
*local_err
= NULL
;
1480 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1482 qemu_boot_set(bootdevice
, &local_err
);
1484 error_report_err(local_err
);
1486 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1490 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1492 mtree_info((fprintf_function
)monitor_printf
, mon
);
1495 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1501 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1502 query_numa_node_mem(node_mem
);
1503 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1504 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1505 monitor_printf(mon
, "node %d cpus:", i
);
1507 if (cpu
->numa_node
== i
) {
1508 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1511 monitor_printf(mon
, "\n");
1512 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1518 #ifdef CONFIG_PROFILER
1523 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1525 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1526 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1527 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1528 tcg_time
, tcg_time
/ (double)get_ticks_per_sec());
1533 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1535 monitor_printf(mon
, "Internal profiler not compiled\n");
1539 /* Capture support */
1540 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1542 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1547 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1548 monitor_printf(mon
, "[%d]: ", i
);
1549 s
->ops
.info (s
->opaque
);
1553 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1556 int n
= qdict_get_int(qdict
, "n");
1559 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1561 s
->ops
.destroy (s
->opaque
);
1562 QLIST_REMOVE (s
, entries
);
1569 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1571 const char *path
= qdict_get_str(qdict
, "path");
1572 int has_freq
= qdict_haskey(qdict
, "freq");
1573 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1574 int has_bits
= qdict_haskey(qdict
, "bits");
1575 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1576 int has_channels
= qdict_haskey(qdict
, "nchannels");
1577 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1580 s
= g_malloc0 (sizeof (*s
));
1582 freq
= has_freq
? freq
: 44100;
1583 bits
= has_bits
? bits
: 16;
1584 nchannels
= has_channels
? nchannels
: 2;
1586 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1587 monitor_printf(mon
, "Failed to add wave capture\n");
1591 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1594 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1596 qemu_acl
*acl
= qemu_acl_find(name
);
1599 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1604 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1606 const char *aclname
= qdict_get_str(qdict
, "aclname");
1607 qemu_acl
*acl
= find_acl(mon
, aclname
);
1608 qemu_acl_entry
*entry
;
1612 monitor_printf(mon
, "policy: %s\n",
1613 acl
->defaultDeny
? "deny" : "allow");
1614 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1616 monitor_printf(mon
, "%d: %s %s\n", i
,
1617 entry
->deny
? "deny" : "allow", entry
->match
);
1622 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1624 const char *aclname
= qdict_get_str(qdict
, "aclname");
1625 qemu_acl
*acl
= find_acl(mon
, aclname
);
1628 qemu_acl_reset(acl
);
1629 monitor_printf(mon
, "acl: removed all rules\n");
1633 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1635 const char *aclname
= qdict_get_str(qdict
, "aclname");
1636 const char *policy
= qdict_get_str(qdict
, "policy");
1637 qemu_acl
*acl
= find_acl(mon
, aclname
);
1640 if (strcmp(policy
, "allow") == 0) {
1641 acl
->defaultDeny
= 0;
1642 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1643 } else if (strcmp(policy
, "deny") == 0) {
1644 acl
->defaultDeny
= 1;
1645 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1647 monitor_printf(mon
, "acl: unknown policy '%s', "
1648 "expected 'deny' or 'allow'\n", policy
);
1653 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1655 const char *aclname
= qdict_get_str(qdict
, "aclname");
1656 const char *match
= qdict_get_str(qdict
, "match");
1657 const char *policy
= qdict_get_str(qdict
, "policy");
1658 int has_index
= qdict_haskey(qdict
, "index");
1659 int index
= qdict_get_try_int(qdict
, "index", -1);
1660 qemu_acl
*acl
= find_acl(mon
, aclname
);
1664 if (strcmp(policy
, "allow") == 0) {
1666 } else if (strcmp(policy
, "deny") == 0) {
1669 monitor_printf(mon
, "acl: unknown policy '%s', "
1670 "expected 'deny' or 'allow'\n", policy
);
1674 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1676 ret
= qemu_acl_append(acl
, deny
, match
);
1678 monitor_printf(mon
, "acl: unable to add acl entry\n");
1680 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1684 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1686 const char *aclname
= qdict_get_str(qdict
, "aclname");
1687 const char *match
= qdict_get_str(qdict
, "match");
1688 qemu_acl
*acl
= find_acl(mon
, aclname
);
1692 ret
= qemu_acl_remove(acl
, match
);
1694 monitor_printf(mon
, "acl: no matching acl entry\n");
1696 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1700 void qmp_getfd(const char *fdname
, Error
**errp
)
1705 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1707 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1711 if (qemu_isdigit(fdname
[0])) {
1713 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1714 "a name not starting with a digit");
1718 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1719 if (strcmp(monfd
->name
, fdname
) != 0) {
1728 monfd
= g_malloc0(sizeof(mon_fd_t
));
1729 monfd
->name
= g_strdup(fdname
);
1732 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1735 void qmp_closefd(const char *fdname
, Error
**errp
)
1739 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1740 if (strcmp(monfd
->name
, fdname
) != 0) {
1744 QLIST_REMOVE(monfd
, next
);
1746 g_free(monfd
->name
);
1751 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1754 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1756 int saved_vm_running
= runstate_is_running();
1757 const char *name
= qdict_get_str(qdict
, "name");
1759 vm_stop(RUN_STATE_RESTORE_VM
);
1761 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1766 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1770 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1773 if (strcmp(monfd
->name
, fdname
) != 0) {
1779 /* caller takes ownership of fd */
1780 QLIST_REMOVE(monfd
, next
);
1781 g_free(monfd
->name
);
1787 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1791 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1793 MonFdsetFd
*mon_fdset_fd
;
1794 MonFdsetFd
*mon_fdset_fd_next
;
1796 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1797 if ((mon_fdset_fd
->removed
||
1798 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1799 runstate_is_running()) {
1800 close(mon_fdset_fd
->fd
);
1801 g_free(mon_fdset_fd
->opaque
);
1802 QLIST_REMOVE(mon_fdset_fd
, next
);
1803 g_free(mon_fdset_fd
);
1807 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1808 QLIST_REMOVE(mon_fdset
, next
);
1813 static void monitor_fdsets_cleanup(void)
1815 MonFdset
*mon_fdset
;
1816 MonFdset
*mon_fdset_next
;
1818 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1819 monitor_fdset_cleanup(mon_fdset
);
1823 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1824 const char *opaque
, Error
**errp
)
1827 Monitor
*mon
= cur_mon
;
1830 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1832 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1836 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1837 has_opaque
, opaque
, errp
);
1849 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1851 MonFdset
*mon_fdset
;
1852 MonFdsetFd
*mon_fdset_fd
;
1855 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1856 if (mon_fdset
->id
!= fdset_id
) {
1859 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1861 if (mon_fdset_fd
->fd
!= fd
) {
1864 mon_fdset_fd
->removed
= true;
1867 mon_fdset_fd
->removed
= true;
1870 if (has_fd
&& !mon_fdset_fd
) {
1873 monitor_fdset_cleanup(mon_fdset
);
1879 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1882 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1884 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1887 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1889 MonFdset
*mon_fdset
;
1890 MonFdsetFd
*mon_fdset_fd
;
1891 FdsetInfoList
*fdset_list
= NULL
;
1893 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1894 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1895 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1897 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1898 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1900 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1901 FdsetFdInfoList
*fdsetfd_info
;
1903 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1904 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1905 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1906 if (mon_fdset_fd
->opaque
) {
1907 fdsetfd_info
->value
->has_opaque
= true;
1908 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1910 fdsetfd_info
->value
->has_opaque
= false;
1913 fdsetfd_info
->next
= fdsetfd_list
;
1914 fdsetfd_list
= fdsetfd_info
;
1917 fdset_info
->value
->fds
= fdsetfd_list
;
1919 fdset_info
->next
= fdset_list
;
1920 fdset_list
= fdset_info
;
1926 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1927 bool has_opaque
, const char *opaque
,
1930 MonFdset
*mon_fdset
= NULL
;
1931 MonFdsetFd
*mon_fdset_fd
;
1935 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1936 /* Break if match found or match impossible due to ordering by ID */
1937 if (fdset_id
<= mon_fdset
->id
) {
1938 if (fdset_id
< mon_fdset
->id
) {
1946 if (mon_fdset
== NULL
) {
1947 int64_t fdset_id_prev
= -1;
1948 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1952 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1953 "a non-negative value");
1956 /* Use specified fdset ID */
1957 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1958 mon_fdset_cur
= mon_fdset
;
1959 if (fdset_id
< mon_fdset_cur
->id
) {
1964 /* Use first available fdset ID */
1965 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1966 mon_fdset_cur
= mon_fdset
;
1967 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1968 fdset_id_prev
= mon_fdset_cur
->id
;
1975 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1977 mon_fdset
->id
= fdset_id
;
1979 mon_fdset
->id
= fdset_id_prev
+ 1;
1982 /* The fdset list is ordered by fdset ID */
1983 if (!mon_fdset_cur
) {
1984 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1985 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1986 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1988 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1992 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1993 mon_fdset_fd
->fd
= fd
;
1994 mon_fdset_fd
->removed
= false;
1996 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1998 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2000 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2001 fdinfo
->fdset_id
= mon_fdset
->id
;
2002 fdinfo
->fd
= mon_fdset_fd
->fd
;
2007 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2010 MonFdset
*mon_fdset
;
2011 MonFdsetFd
*mon_fdset_fd
;
2014 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2015 if (mon_fdset
->id
!= fdset_id
) {
2018 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2019 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2020 if (mon_fd_flags
== -1) {
2024 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2025 return mon_fdset_fd
->fd
;
2037 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2039 MonFdset
*mon_fdset
;
2040 MonFdsetFd
*mon_fdset_fd_dup
;
2042 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2043 if (mon_fdset
->id
!= fdset_id
) {
2046 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2047 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2051 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2052 mon_fdset_fd_dup
->fd
= dup_fd
;
2053 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2059 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2061 MonFdset
*mon_fdset
;
2062 MonFdsetFd
*mon_fdset_fd_dup
;
2064 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2065 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2066 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2068 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2069 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2070 monitor_fdset_cleanup(mon_fdset
);
2074 return mon_fdset
->id
;
2082 int monitor_fdset_dup_fd_find(int dup_fd
)
2084 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2087 void monitor_fdset_dup_fd_remove(int dup_fd
)
2089 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2092 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2095 Error
*local_err
= NULL
;
2097 if (!qemu_isdigit(fdname
[0]) && mon
) {
2098 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2100 fd
= qemu_parse_fd(fdname
);
2102 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2107 error_propagate(errp
, local_err
);
2116 /* Please update hmp-commands.hx when adding or changing commands */
2117 static mon_cmd_t info_cmds
[] = {
2118 #include "hmp-commands-info.h"
2122 /* mon_cmds and info_cmds would be sorted at runtime */
2123 static mon_cmd_t mon_cmds
[] = {
2124 #include "hmp-commands.h"
2128 static const mon_cmd_t qmp_cmds
[] = {
2129 #include "qmp-commands-old.h"
2133 /*******************************************************************/
2135 static const char *pch
;
2136 static sigjmp_buf expr_env
;
2139 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2140 expr_error(Monitor
*mon
, const char *fmt
, ...)
2144 monitor_vprintf(mon
, fmt
, ap
);
2145 monitor_printf(mon
, "\n");
2147 siglongjmp(expr_env
, 1);
2150 /* return 0 if OK, -1 if not found */
2151 static int get_monitor_def(target_long
*pval
, const char *name
)
2153 const MonitorDef
*md
= target_monitor_defs();
2162 for(; md
->name
!= NULL
; md
++) {
2163 if (compare_cmd(name
, md
->name
)) {
2164 if (md
->get_value
) {
2165 *pval
= md
->get_value(md
, md
->offset
);
2167 CPUArchState
*env
= mon_get_cpu_env();
2168 ptr
= (uint8_t *)env
+ md
->offset
;
2171 *pval
= *(int32_t *)ptr
;
2174 *pval
= *(target_long
*)ptr
;
2185 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2187 *pval
= (target_long
) tmp
;
2193 static void next(void)
2197 while (qemu_isspace(*pch
))
2202 static int64_t expr_sum(Monitor
*mon
);
2204 static int64_t expr_unary(Monitor
*mon
)
2213 n
= expr_unary(mon
);
2217 n
= -expr_unary(mon
);
2221 n
= ~expr_unary(mon
);
2227 expr_error(mon
, "')' expected");
2234 expr_error(mon
, "character constant expected");
2238 expr_error(mon
, "missing terminating \' character");
2248 while ((*pch
>= 'a' && *pch
<= 'z') ||
2249 (*pch
>= 'A' && *pch
<= 'Z') ||
2250 (*pch
>= '0' && *pch
<= '9') ||
2251 *pch
== '_' || *pch
== '.') {
2252 if ((q
- buf
) < sizeof(buf
) - 1)
2256 while (qemu_isspace(*pch
))
2259 ret
= get_monitor_def(®
, buf
);
2261 expr_error(mon
, "unknown register");
2266 expr_error(mon
, "unexpected end of expression");
2271 n
= strtoull(pch
, &p
, 0);
2272 if (errno
== ERANGE
) {
2273 expr_error(mon
, "number too large");
2276 expr_error(mon
, "invalid char '%c' in expression", *p
);
2279 while (qemu_isspace(*pch
))
2287 static int64_t expr_prod(Monitor
*mon
)
2292 val
= expr_unary(mon
);
2295 if (op
!= '*' && op
!= '/' && op
!= '%')
2298 val2
= expr_unary(mon
);
2307 expr_error(mon
, "division by zero");
2318 static int64_t expr_logic(Monitor
*mon
)
2323 val
= expr_prod(mon
);
2326 if (op
!= '&' && op
!= '|' && op
!= '^')
2329 val2
= expr_prod(mon
);
2346 static int64_t expr_sum(Monitor
*mon
)
2351 val
= expr_logic(mon
);
2354 if (op
!= '+' && op
!= '-')
2357 val2
= expr_logic(mon
);
2366 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2369 if (sigsetjmp(expr_env
, 0)) {
2373 while (qemu_isspace(*pch
))
2375 *pval
= expr_sum(mon
);
2380 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2382 const char *p
= *pp
;
2386 d
= strtod(p
, &tailp
);
2388 monitor_printf(mon
, "Number expected\n");
2391 if (d
!= d
|| d
- d
!= 0) {
2392 /* NaN or infinity */
2393 monitor_printf(mon
, "Bad number\n");
2402 * Store the command-name in cmdname, and return a pointer to
2403 * the remaining of the command string.
2405 static const char *get_command_name(const char *cmdline
,
2406 char *cmdname
, size_t nlen
)
2409 const char *p
, *pstart
;
2412 while (qemu_isspace(*p
))
2417 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2422 memcpy(cmdname
, pstart
, len
);
2423 cmdname
[len
] = '\0';
2428 * Read key of 'type' into 'key' and return the current
2431 static char *key_get_info(const char *type
, char **key
)
2439 p
= strchr(type
, ':');
2446 str
= g_malloc(len
+ 1);
2447 memcpy(str
, type
, len
);
2454 static int default_fmt_format
= 'x';
2455 static int default_fmt_size
= 4;
2457 static int is_valid_option(const char *c
, const char *typestr
)
2465 typestr
= strstr(typestr
, option
);
2466 return (typestr
!= NULL
);
2469 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2470 const char *cmdname
)
2472 const mon_cmd_t
*cmd
;
2474 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2475 if (compare_cmd(cmdname
, cmd
->name
)) {
2483 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2485 return search_dispatch_table(qmp_cmds
, cmdname
);
2489 * Parse command name from @cmdp according to command table @table.
2490 * If blank, return NULL.
2491 * Else, if no valid command can be found, report to @mon, and return
2493 * Else, change @cmdp to point right behind the name, and return its
2494 * command table entry.
2495 * Do not assume the return value points into @table! It doesn't when
2496 * the command is found in a sub-command table.
2498 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2503 const mon_cmd_t
*cmd
;
2506 /* extract the command name */
2507 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2511 cmd
= search_dispatch_table(table
, cmdname
);
2513 monitor_printf(mon
, "unknown command: '%.*s'\n",
2514 (int)(p
- *cmdp
), *cmdp
);
2518 /* filter out following useless space */
2519 while (qemu_isspace(*p
)) {
2524 /* search sub command */
2525 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2526 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2533 * Parse arguments for @cmd.
2534 * If it can't be parsed, report to @mon, and return NULL.
2535 * Else, insert command arguments into a QDict, and return it.
2536 * Note: On success, caller has to free the QDict structure.
2539 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2541 const mon_cmd_t
*cmd
)
2543 const char *typestr
;
2546 const char *p
= *endp
;
2548 QDict
*qdict
= qdict_new();
2550 /* parse the parameters */
2551 typestr
= cmd
->args_type
;
2553 typestr
= key_get_info(typestr
, &key
);
2565 while (qemu_isspace(*p
))
2567 if (*typestr
== '?') {
2570 /* no optional string: NULL argument */
2574 ret
= get_str(buf
, sizeof(buf
), &p
);
2578 monitor_printf(mon
, "%s: filename expected\n",
2582 monitor_printf(mon
, "%s: block device name expected\n",
2586 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2591 qdict_put(qdict
, key
, qstring_from_str(buf
));
2596 QemuOptsList
*opts_list
;
2599 opts_list
= qemu_find_opts(key
);
2600 if (!opts_list
|| opts_list
->desc
->name
) {
2603 while (qemu_isspace(*p
)) {
2608 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2611 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2615 qemu_opts_to_qdict(opts
, qdict
);
2616 qemu_opts_del(opts
);
2621 int count
, format
, size
;
2623 while (qemu_isspace(*p
))
2629 if (qemu_isdigit(*p
)) {
2631 while (qemu_isdigit(*p
)) {
2632 count
= count
* 10 + (*p
- '0');
2670 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2671 monitor_printf(mon
, "invalid char in format: '%c'\n",
2676 format
= default_fmt_format
;
2677 if (format
!= 'i') {
2678 /* for 'i', not specifying a size gives -1 as size */
2680 size
= default_fmt_size
;
2681 default_fmt_size
= size
;
2683 default_fmt_format
= format
;
2686 format
= default_fmt_format
;
2687 if (format
!= 'i') {
2688 size
= default_fmt_size
;
2693 qdict_put(qdict
, "count", qint_from_int(count
));
2694 qdict_put(qdict
, "format", qint_from_int(format
));
2695 qdict_put(qdict
, "size", qint_from_int(size
));
2704 while (qemu_isspace(*p
))
2706 if (*typestr
== '?' || *typestr
== '.') {
2707 if (*typestr
== '?') {
2715 while (qemu_isspace(*p
))
2724 if (get_expr(mon
, &val
, &p
))
2726 /* Check if 'i' is greater than 32-bit */
2727 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2728 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2729 monitor_printf(mon
, "integer is for 32-bit values\n");
2731 } else if (c
== 'M') {
2733 monitor_printf(mon
, "enter a positive value\n");
2738 qdict_put(qdict
, key
, qint_from_int(val
));
2746 while (qemu_isspace(*p
)) {
2749 if (*typestr
== '?') {
2755 val
= qemu_strtosz(p
, &end
);
2757 monitor_printf(mon
, "invalid size\n");
2760 qdict_put(qdict
, key
, qint_from_int(val
));
2768 while (qemu_isspace(*p
))
2770 if (*typestr
== '?') {
2776 if (get_double(mon
, &val
, &p
) < 0) {
2779 if (p
[0] && p
[1] == 's') {
2782 val
/= 1e3
; p
+= 2; break;
2784 val
/= 1e6
; p
+= 2; break;
2786 val
/= 1e9
; p
+= 2; break;
2789 if (*p
&& !qemu_isspace(*p
)) {
2790 monitor_printf(mon
, "Unknown unit suffix\n");
2793 qdict_put(qdict
, key
, qfloat_from_double(val
));
2801 while (qemu_isspace(*p
)) {
2805 while (qemu_isgraph(*p
)) {
2808 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2810 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2813 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2816 qdict_put(qdict
, key
, qbool_from_bool(val
));
2821 const char *tmp
= p
;
2828 while (qemu_isspace(*p
))
2833 if(!is_valid_option(p
, typestr
)) {
2835 monitor_printf(mon
, "%s: unsupported option -%c\n",
2847 qdict_put(qdict
, key
, qbool_from_bool(true));
2854 /* package all remaining string */
2857 while (qemu_isspace(*p
)) {
2860 if (*typestr
== '?') {
2863 /* no remaining string: NULL argument */
2869 monitor_printf(mon
, "%s: string expected\n",
2873 qdict_put(qdict
, key
, qstring_from_str(p
));
2879 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2885 /* check that all arguments were parsed */
2886 while (qemu_isspace(*p
))
2889 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2902 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2905 const mon_cmd_t
*cmd
;
2907 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2912 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2914 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2919 cmd
->mhandler
.cmd(mon
, qdict
);
2923 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2925 const char *p
, *pstart
;
2934 p
= pstart
+ strlen(pstart
);
2936 if (len
> sizeof(cmd
) - 2)
2937 len
= sizeof(cmd
) - 2;
2938 memcpy(cmd
, pstart
, len
);
2940 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2941 readline_add_completion(mon
->rs
, cmd
);
2949 static void file_completion(Monitor
*mon
, const char *input
)
2954 char file
[1024], file_prefix
[1024];
2958 p
= strrchr(input
, '/');
2961 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2962 pstrcpy(path
, sizeof(path
), ".");
2964 input_path_len
= p
- input
+ 1;
2965 memcpy(path
, input
, input_path_len
);
2966 if (input_path_len
> sizeof(path
) - 1)
2967 input_path_len
= sizeof(path
) - 1;
2968 path
[input_path_len
] = '\0';
2969 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2972 ffs
= opendir(path
);
2981 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
2985 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2986 memcpy(file
, input
, input_path_len
);
2987 if (input_path_len
< sizeof(file
))
2988 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2990 /* stat the file to find out if it's a directory.
2991 * In that case add a slash to speed up typing long paths
2993 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
2994 pstrcat(file
, sizeof(file
), "/");
2996 readline_add_completion(mon
->rs
, file
);
3002 static const char *next_arg_type(const char *typestr
)
3004 const char *p
= strchr(typestr
, ':');
3005 return (p
!= NULL
? ++p
: typestr
);
3008 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3011 if (!str
|| !option
) {
3014 if (!strncmp(option
, str
, strlen(str
))) {
3015 readline_add_completion(rs
, option
);
3019 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3022 ChardevBackendInfoList
*list
, *start
;
3028 readline_set_completion_index(rs
, len
);
3030 start
= list
= qmp_query_chardev_backends(NULL
);
3032 const char *chr_name
= list
->value
->name
;
3034 if (!strncmp(chr_name
, str
, len
)) {
3035 readline_add_completion(rs
, chr_name
);
3039 qapi_free_ChardevBackendInfoList(start
);
3042 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3051 readline_set_completion_index(rs
, len
);
3052 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
3053 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3057 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3067 readline_set_completion_index(rs
, len
);
3068 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3071 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3073 name
= object_class_get_name(OBJECT_CLASS(dc
));
3075 if (!dc
->cannot_instantiate_with_device_add_yet
3076 && !strncmp(name
, str
, len
)) {
3077 readline_add_completion(rs
, name
);
3084 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3094 readline_set_completion_index(rs
, len
);
3095 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3099 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3100 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3101 readline_add_completion(rs
, name
);
3108 static void peripheral_device_del_completion(ReadLineState
*rs
,
3109 const char *str
, size_t len
)
3111 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3112 GSList
*list
, *item
;
3114 list
= qdev_build_hotpluggable_device_list(peripheral
);
3119 for (item
= list
; item
; item
= g_slist_next(item
)) {
3120 DeviceState
*dev
= item
->data
;
3122 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3123 readline_add_completion(rs
, dev
->id
);
3130 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3133 ChardevInfoList
*list
, *start
;
3139 readline_set_completion_index(rs
, len
);
3141 start
= list
= qmp_query_chardev(NULL
);
3143 ChardevInfo
*chr
= list
->value
;
3145 if (!strncmp(chr
->label
, str
, len
)) {
3146 readline_add_completion(rs
, chr
->label
);
3150 qapi_free_ChardevInfoList(start
);
3153 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3156 ChardevInfoList
*list
, *start
;
3159 readline_set_completion_index(rs
, len
);
3161 start
= list
= qmp_query_chardev(NULL
);
3163 ChardevInfo
*chr_info
= list
->value
;
3165 if (!strncmp(chr_info
->label
, str
, len
)) {
3166 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3167 if (chr
&& chr_is_ringbuf(chr
)) {
3168 readline_add_completion(rs
, chr_info
->label
);
3173 qapi_free_ChardevInfoList(start
);
3176 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3181 ringbuf_completion(rs
, str
);
3184 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3193 readline_set_completion_index(rs
, len
);
3194 peripheral_device_del_completion(rs
, str
, len
);
3197 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3199 ObjectPropertyInfoList
*list
, *start
;
3206 readline_set_completion_index(rs
, len
);
3208 start
= list
= qmp_qom_list("/objects", NULL
);
3210 ObjectPropertyInfo
*info
= list
->value
;
3212 if (!strncmp(info
->type
, "child<", 5)
3213 && !strncmp(info
->name
, str
, len
)) {
3214 readline_add_completion(rs
, info
->name
);
3218 qapi_free_ObjectPropertyInfoList(start
);
3221 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3230 sep
= strrchr(str
, '-');
3235 readline_set_completion_index(rs
, len
);
3236 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3237 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3238 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3243 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3248 readline_set_completion_index(rs
, len
);
3250 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3252 count
= qemu_find_net_clients_except(NULL
, ncs
,
3253 NET_CLIENT_OPTIONS_KIND_NONE
,
3255 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3256 const char *name
= ncs
[i
]->name
;
3257 if (!strncmp(str
, name
, len
)) {
3258 readline_add_completion(rs
, name
);
3261 } else if (nb_args
== 3) {
3262 add_completion_option(rs
, str
, "on");
3263 add_completion_option(rs
, str
, "off");
3267 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3270 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3277 readline_set_completion_index(rs
, len
);
3278 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
3280 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3282 const char *name
= ncs
[i
]->name
;
3283 if (strncmp(str
, name
, len
)) {
3286 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3288 readline_add_completion(rs
, name
);
3293 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3298 readline_set_completion_index(rs
, len
);
3301 for (id
= 0; id
< trace_event_count(); id
++) {
3302 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3303 if (!strncmp(str
, event_name
, len
)) {
3304 readline_add_completion(rs
, event_name
);
3307 } else if (nb_args
== 3) {
3308 add_completion_option(rs
, str
, "on");
3309 add_completion_option(rs
, str
, "off");
3313 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3320 readline_set_completion_index(rs
, strlen(str
));
3321 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3322 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3326 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3332 readline_set_completion_index(rs
, len
);
3335 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3336 const char *name
= MigrationCapability_lookup
[i
];
3337 if (!strncmp(str
, name
, len
)) {
3338 readline_add_completion(rs
, name
);
3341 } else if (nb_args
== 3) {
3342 add_completion_option(rs
, str
, "on");
3343 add_completion_option(rs
, str
, "off");
3347 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3353 readline_set_completion_index(rs
, len
);
3356 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3357 const char *name
= MigrationParameter_lookup
[i
];
3358 if (!strncmp(str
, name
, len
)) {
3359 readline_add_completion(rs
, name
);
3365 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3373 readline_set_completion_index(rs
, len
);
3374 for (i
= 0; host_net_devices
[i
]; i
++) {
3375 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3376 readline_add_completion(rs
, host_net_devices
[i
]);
3381 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3383 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3387 readline_set_completion_index(rs
, len
);
3389 count
= qemu_find_net_clients_except(NULL
, ncs
,
3390 NET_CLIENT_OPTIONS_KIND_NONE
,
3392 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3396 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3399 snprintf(name
, sizeof(name
), "%d", id
);
3400 if (!strncmp(str
, name
, len
)) {
3401 readline_add_completion(rs
, name
);
3405 } else if (nb_args
== 3) {
3406 count
= qemu_find_net_clients_except(NULL
, ncs
,
3407 NET_CLIENT_OPTIONS_KIND_NIC
,
3409 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3413 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
3414 net_hub_id_for_client(ncs
[i
], &id
)) {
3417 name
= ncs
[i
]->name
;
3418 if (!strncmp(str
, name
, len
)) {
3419 readline_add_completion(rs
, name
);
3426 static void vm_completion(ReadLineState
*rs
, const char *str
)
3429 BlockDriverState
*bs
= NULL
;
3432 readline_set_completion_index(rs
, len
);
3433 while ((bs
= bdrv_next(bs
))) {
3434 SnapshotInfoList
*snapshots
, *snapshot
;
3435 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3438 aio_context_acquire(ctx
);
3439 if (bdrv_can_snapshot(bs
)) {
3440 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3442 aio_context_release(ctx
);
3447 snapshot
= snapshots
;
3449 char *completion
= snapshot
->value
->name
;
3450 if (!strncmp(str
, completion
, len
)) {
3451 readline_add_completion(rs
, completion
);
3453 completion
= snapshot
->value
->id
;
3454 if (!strncmp(str
, completion
, len
)) {
3455 readline_add_completion(rs
, completion
);
3457 snapshot
= snapshot
->next
;
3459 qapi_free_SnapshotInfoList(snapshots
);
3464 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3467 vm_completion(rs
, str
);
3471 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3474 vm_completion(rs
, str
);
3478 static void monitor_find_completion_by_table(Monitor
*mon
,
3479 const mon_cmd_t
*cmd_table
,
3483 const char *cmdname
;
3485 const char *ptype
, *str
, *name
;
3486 const mon_cmd_t
*cmd
;
3487 BlockBackend
*blk
= NULL
;
3490 /* command completion */
3495 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3496 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3497 cmd_completion(mon
, cmdname
, cmd
->name
);
3500 /* find the command */
3501 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3502 if (compare_cmd(args
[0], cmd
->name
)) {
3510 if (cmd
->sub_table
) {
3511 /* do the job again */
3512 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3513 &args
[1], nb_args
- 1);
3516 if (cmd
->command_completion
) {
3517 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3521 ptype
= next_arg_type(cmd
->args_type
);
3522 for(i
= 0; i
< nb_args
- 2; i
++) {
3523 if (*ptype
!= '\0') {
3524 ptype
= next_arg_type(ptype
);
3525 while (*ptype
== '?')
3526 ptype
= next_arg_type(ptype
);
3529 str
= args
[nb_args
- 1];
3530 while (*ptype
== '-' && ptype
[1] != '\0') {
3531 ptype
= next_arg_type(ptype
);
3535 /* file completion */
3536 readline_set_completion_index(mon
->rs
, strlen(str
));
3537 file_completion(mon
, str
);
3540 /* block device name completion */
3541 readline_set_completion_index(mon
->rs
, strlen(str
));
3542 while ((blk
= blk_next(blk
)) != NULL
) {
3543 name
= blk_name(blk
);
3544 if (str
[0] == '\0' ||
3545 !strncmp(name
, str
, strlen(str
))) {
3546 readline_add_completion(mon
->rs
, name
);
3552 if (!strcmp(cmd
->name
, "help|?")) {
3553 monitor_find_completion_by_table(mon
, cmd_table
,
3554 &args
[1], nb_args
- 1);
3563 static void monitor_find_completion(void *opaque
,
3564 const char *cmdline
)
3566 Monitor
*mon
= opaque
;
3567 char *args
[MAX_ARGS
];
3570 /* 1. parse the cmdline */
3571 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3575 /* if the line ends with a space, it means we want to complete the
3577 len
= strlen(cmdline
);
3578 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3579 if (nb_args
>= MAX_ARGS
) {
3582 args
[nb_args
++] = g_strdup("");
3585 /* 2. auto complete according to args */
3586 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3589 free_cmdline_args(args
, nb_args
);
3592 static int monitor_can_read(void *opaque
)
3594 Monitor
*mon
= opaque
;
3596 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3599 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
3602 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
3604 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3605 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3606 "Capabilities negotiation is already complete, command "
3607 "'%s' ignored", cmd
->name
);
3610 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3611 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3612 "Expecting capabilities negotiation with "
3613 "'qmp_capabilities' before command '%s'", cmd
->name
);
3620 * Argument validation rules:
3622 * 1. The argument must exist in cmd_args qdict
3623 * 2. The argument type must be the expected one
3625 * Special case: If the argument doesn't exist in cmd_args and
3626 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3627 * checking is skipped for it.
3629 static void check_client_args_type(const QDict
*client_args
,
3630 const QDict
*cmd_args
, int flags
,
3633 const QDictEntry
*ent
;
3635 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3638 const QObject
*client_arg
= qdict_entry_value(ent
);
3639 const char *client_arg_name
= qdict_entry_key(ent
);
3641 obj
= qdict_get(cmd_args
, client_arg_name
);
3643 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3644 /* handler accepts unknowns */
3647 /* client arg doesn't exist */
3648 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3652 arg_type
= qobject_to_qstring(obj
);
3653 assert(arg_type
!= NULL
);
3655 /* check if argument's type is correct */
3656 switch (qstring_get_str(arg_type
)[0]) {
3660 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3661 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3662 client_arg_name
, "string");
3670 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3671 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3672 client_arg_name
, "int");
3677 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3678 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3679 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3680 client_arg_name
, "number");
3686 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3687 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3688 client_arg_name
, "bool");
3693 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3696 /* Any QObject can be passed. */
3701 * These types are not supported by QMP and thus are not
3702 * handled here. Fall through.
3711 * - Check if the client has passed all mandatory args
3712 * - Set special flags for argument validation
3714 static void check_mandatory_args(const QDict
*cmd_args
,
3715 const QDict
*client_args
, int *flags
,
3718 const QDictEntry
*ent
;
3720 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3721 const char *cmd_arg_name
= qdict_entry_key(ent
);
3722 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3723 assert(type
!= NULL
);
3725 if (qstring_get_str(type
)[0] == 'O') {
3726 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3727 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3728 } else if (qstring_get_str(type
)[0] != '-' &&
3729 qstring_get_str(type
)[1] != '?' &&
3730 !qdict_haskey(client_args
, cmd_arg_name
)) {
3731 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3737 static QDict
*qdict_from_args_type(const char *args_type
)
3741 QString
*key
, *type
, *cur_qs
;
3743 assert(args_type
!= NULL
);
3745 qdict
= qdict_new();
3747 if (args_type
== NULL
|| args_type
[0] == '\0') {
3748 /* no args, empty qdict */
3752 key
= qstring_new();
3753 type
= qstring_new();
3758 switch (args_type
[i
]) {
3761 qdict_put(qdict
, qstring_get_str(key
), type
);
3763 if (args_type
[i
] == '\0') {
3766 type
= qstring_new(); /* qdict has ref */
3767 cur_qs
= key
= qstring_new();
3773 qstring_append_chr(cur_qs
, args_type
[i
]);
3783 * Client argument checking rules:
3785 * 1. Client must provide all mandatory arguments
3786 * 2. Each argument provided by the client must be expected
3787 * 3. Each argument provided by the client must have the type expected
3790 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3797 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3800 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3805 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3808 error_propagate(errp
, err
);
3813 * Input object checking rules
3815 * 1. Input object must be a dict
3816 * 2. The "execute" key must exist
3817 * 3. The "execute" key must be a string
3818 * 4. If the "arguments" key exists, it must be a dict
3819 * 5. If the "id" key exists, it can be anything (ie. json-value)
3820 * 6. Any argument not listed above is considered invalid
3822 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3824 const QDictEntry
*ent
;
3825 int has_exec_key
= 0;
3828 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3829 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3833 input_dict
= qobject_to_qdict(input_obj
);
3835 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3836 const char *arg_name
= qdict_entry_key(ent
);
3837 const QObject
*arg_obj
= qdict_entry_value(ent
);
3839 if (!strcmp(arg_name
, "execute")) {
3840 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3841 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3842 "execute", "string");
3846 } else if (!strcmp(arg_name
, "arguments")) {
3847 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3848 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3849 "arguments", "object");
3852 } else if (!strcmp(arg_name
, "id")) {
3853 /* Any string is acceptable as "id", so nothing to check */
3855 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3860 if (!has_exec_key
) {
3861 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3868 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3870 Error
*local_err
= NULL
;
3871 QObject
*obj
, *data
;
3872 QDict
*input
, *args
;
3873 const mon_cmd_t
*cmd
;
3874 const char *cmd_name
;
3875 Monitor
*mon
= cur_mon
;
3877 args
= input
= NULL
;
3880 obj
= json_parser_parse(tokens
, NULL
);
3882 // FIXME: should be triggered in json_parser_parse()
3883 error_setg(&local_err
, QERR_JSON_PARSING
);
3887 input
= qmp_check_input_obj(obj
, &local_err
);
3889 qobject_decref(obj
);
3893 mon
->qmp
.id
= qdict_get(input
, "id");
3894 qobject_incref(mon
->qmp
.id
);
3896 cmd_name
= qdict_get_str(input
, "execute");
3897 trace_handle_qmp_command(mon
, cmd_name
);
3898 cmd
= qmp_find_cmd(cmd_name
);
3900 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3901 "The command %s has not been found", cmd_name
);
3904 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
3908 obj
= qdict_get(input
, "arguments");
3912 args
= qobject_to_qdict(obj
);
3916 qmp_check_client_args(cmd
, args
, &local_err
);
3921 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
3924 monitor_protocol_emitter(mon
, data
, local_err
);
3925 qobject_decref(data
);
3926 error_free(local_err
);
3931 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3933 Monitor
*old_mon
= cur_mon
;
3937 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3942 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3944 Monitor
*old_mon
= cur_mon
;
3950 for (i
= 0; i
< size
; i
++)
3951 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3953 if (size
== 0 || buf
[size
- 1] != 0)
3954 monitor_printf(cur_mon
, "corrupted command\n");
3956 handle_hmp_command(cur_mon
, (char *)buf
);
3962 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3963 void *readline_opaque
)
3965 Monitor
*mon
= opaque
;
3967 monitor_suspend(mon
);
3968 handle_hmp_command(mon
, cmdline
);
3969 monitor_resume(mon
);
3972 int monitor_suspend(Monitor
*mon
)
3980 void monitor_resume(Monitor
*mon
)
3984 if (--mon
->suspend_cnt
== 0)
3985 readline_show_prompt(mon
->rs
);
3988 static QObject
*get_qmp_greeting(void)
3990 QObject
*ver
= NULL
;
3992 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3993 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
3996 static void monitor_qmp_event(void *opaque
, int event
)
3999 Monitor
*mon
= opaque
;
4002 case CHR_EVENT_OPENED
:
4003 mon
->qmp
.in_command_mode
= false;
4004 data
= get_qmp_greeting();
4005 monitor_json_emitter(mon
, data
);
4006 qobject_decref(data
);
4009 case CHR_EVENT_CLOSED
:
4010 json_message_parser_destroy(&mon
->qmp
.parser
);
4011 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4013 monitor_fdsets_cleanup();
4018 static void monitor_event(void *opaque
, int event
)
4020 Monitor
*mon
= opaque
;
4023 case CHR_EVENT_MUX_IN
:
4024 qemu_mutex_lock(&mon
->out_lock
);
4026 qemu_mutex_unlock(&mon
->out_lock
);
4027 if (mon
->reset_seen
) {
4028 readline_restart(mon
->rs
);
4029 monitor_resume(mon
);
4032 mon
->suspend_cnt
= 0;
4036 case CHR_EVENT_MUX_OUT
:
4037 if (mon
->reset_seen
) {
4038 if (mon
->suspend_cnt
== 0) {
4039 monitor_printf(mon
, "\n");
4042 monitor_suspend(mon
);
4046 qemu_mutex_lock(&mon
->out_lock
);
4048 qemu_mutex_unlock(&mon
->out_lock
);
4051 case CHR_EVENT_OPENED
:
4052 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4053 "information\n", QEMU_VERSION
);
4054 if (!mon
->mux_out
) {
4055 readline_restart(mon
->rs
);
4056 readline_show_prompt(mon
->rs
);
4058 mon
->reset_seen
= 1;
4062 case CHR_EVENT_CLOSED
:
4064 monitor_fdsets_cleanup();
4070 compare_mon_cmd(const void *a
, const void *b
)
4072 return strcmp(((const mon_cmd_t
*)a
)->name
,
4073 ((const mon_cmd_t
*)b
)->name
);
4076 static void sortcmdlist(void)
4079 int elem_size
= sizeof(mon_cmd_t
);
4081 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4082 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4084 array_num
= sizeof(info_cmds
)/elem_size
-1;
4085 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4097 /* These functions just adapt the readline interface in a typesafe way. We
4098 * could cast function pointers but that discards compiler checks.
4100 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4101 const char *fmt
, ...)
4105 monitor_vprintf(opaque
, fmt
, ap
);
4109 static void monitor_readline_flush(void *opaque
)
4111 monitor_flush(opaque
);
4114 static void __attribute__((constructor
)) monitor_lock_init(void)
4116 qemu_mutex_init(&monitor_lock
);
4119 void monitor_init(CharDriverState
*chr
, int flags
)
4121 static int is_first_init
= 1;
4124 if (is_first_init
) {
4125 monitor_qapi_event_init();
4130 mon
= g_malloc(sizeof(*mon
));
4131 monitor_data_init(mon
);
4135 if (flags
& MONITOR_USE_READLINE
) {
4136 mon
->rs
= readline_init(monitor_readline_printf
,
4137 monitor_readline_flush
,
4139 monitor_find_completion
);
4140 monitor_read_command(mon
, 0);
4143 if (monitor_is_qmp(mon
)) {
4144 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4145 monitor_qmp_event
, mon
);
4146 qemu_chr_fe_set_echo(chr
, true);
4147 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4149 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4150 monitor_event
, mon
);
4153 qemu_mutex_lock(&monitor_lock
);
4154 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4155 qemu_mutex_unlock(&monitor_lock
);
4158 static void bdrv_password_cb(void *opaque
, const char *password
,
4159 void *readline_opaque
)
4161 Monitor
*mon
= opaque
;
4162 BlockDriverState
*bs
= readline_opaque
;
4164 Error
*local_err
= NULL
;
4166 bdrv_add_key(bs
, password
, &local_err
);
4168 error_report_err(local_err
);
4171 if (mon
->password_completion_cb
)
4172 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4174 monitor_read_command(mon
, 1);
4177 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4178 BlockCompletionFunc
*completion_cb
,
4183 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4184 bdrv_get_encrypted_filename(bs
));
4186 mon
->password_completion_cb
= completion_cb
;
4187 mon
->password_opaque
= opaque
;
4189 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4191 if (err
&& completion_cb
)
4192 completion_cb(opaque
, err
);
4197 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4198 BlockCompletionFunc
*completion_cb
,
4204 blk
= blk_by_name(device
);
4206 monitor_printf(mon
, "Device not found %s\n", device
);
4210 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4214 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4217 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4220 if (completion_cb
) {
4221 completion_cb(opaque
, 0);
4226 QemuOptsList qemu_mon_opts
= {
4228 .implied_opt_name
= "chardev",
4229 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4233 .type
= QEMU_OPT_STRING
,
4236 .type
= QEMU_OPT_STRING
,
4239 .type
= QEMU_OPT_BOOL
,
4242 .type
= QEMU_OPT_BOOL
,
4244 { /* end of list */ }
4249 void qmp_rtc_reset_reinjection(Error
**errp
)
4251 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4255 #ifndef TARGET_S390X
4256 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4258 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");