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"
81 #include "qemu/cutils.h"
83 /* for hmp_info_irq/pic */
84 #if defined(TARGET_SPARC)
85 #include "hw/sparc/sun4m.h"
87 #include "hw/lm32/lm32_pic.h"
89 #if defined(TARGET_S390X)
90 #include "hw/s390x/storage-keys.h"
97 * 'B' block device name
98 * 's' string (accept optional quote)
99 * 'S' it just appends the rest of the string (accept optional quote)
100 * 'O' option string of the form NAME=VALUE,...
101 * parsed according to QemuOptsList given by its name
102 * Example: 'device:O' uses qemu_device_opts.
103 * Restriction: only lists with empty desc are supported
104 * TODO lift the restriction
106 * 'l' target long (32 or 64 bit)
107 * 'M' Non-negative target long (32 or 64 bit), in user mode the
108 * value is multiplied by 2^20 (think Mebibyte)
109 * 'o' octets (aka bytes)
110 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
111 * K, k suffix, which multiplies the value by 2^60 for suffixes E
112 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
113 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
115 * user mode accepts an optional ms, us, ns suffix,
116 * which divides the value by 1e3, 1e6, 1e9, respectively
117 * '/' optional gdb-like print format (like "/10x")
119 * '?' optional type (for all types, except '/')
120 * '.' other form of optional type (for 'i' and 'l')
122 * user mode accepts "on" or "off"
123 * '-' optional parameter (eg. '-f')
127 typedef struct mon_cmd_t
{
129 const char *args_type
;
133 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
134 void (*cmd_new
)(QDict
*params
, QObject
**ret_data
, Error
**errp
);
136 /* @sub_table is a list of 2nd level of commands. If it do not exist,
137 * mhandler should be used. If it exist, sub_table[?].mhandler should be
138 * used, and mhandler of 1st level plays the role of help function.
140 struct mon_cmd_t
*sub_table
;
141 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
144 /* file descriptors passed via SCM_RIGHTS */
145 typedef struct mon_fd_t mon_fd_t
;
149 QLIST_ENTRY(mon_fd_t
) next
;
152 /* file descriptor associated with a file descriptor set */
153 typedef struct MonFdsetFd MonFdsetFd
;
158 QLIST_ENTRY(MonFdsetFd
) next
;
161 /* file descriptor set containing fds passed via SCM_RIGHTS */
162 typedef struct MonFdset MonFdset
;
165 QLIST_HEAD(, MonFdsetFd
) fds
;
166 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
167 QLIST_ENTRY(MonFdset
) next
;
172 JSONMessageParser parser
;
174 * When a client connects, we're in capabilities negotiation mode.
175 * When command qmp_capabilities succeeds, we go into command
178 bool in_command_mode
; /* are we in command mode? */
182 * To prevent flooding clients, events can be throttled. The
183 * throttling is calculated globally, rather than per-Monitor
186 typedef struct MonitorQAPIEventState
{
187 QAPIEvent event
; /* Throttling state for this event type and... */
188 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
189 QEMUTimer
*timer
; /* Timer for handling delayed events */
190 QDict
*qdict
; /* Delayed event (if any) */
191 } MonitorQAPIEventState
;
194 int64_t rate
; /* Minimum time (in ns) between two events */
195 } MonitorQAPIEventConf
;
198 CharDriverState
*chr
;
208 /* Read under either BQL or out_lock, written with BQL+out_lock. */
214 BlockCompletionFunc
*password_completion_cb
;
215 void *password_opaque
;
216 mon_cmd_t
*cmd_table
;
217 QLIST_HEAD(,mon_fd_t
) fds
;
218 QLIST_ENTRY(Monitor
) entry
;
221 /* QMP checker flags */
222 #define QMP_ACCEPT_UNKNOWNS 1
224 /* Protects mon_list, monitor_event_state. */
225 static QemuMutex monitor_lock
;
227 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
228 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
229 static int mon_refcount
;
231 static mon_cmd_t mon_cmds
[];
232 static mon_cmd_t info_cmds
[];
234 static const mon_cmd_t qmp_cmds
[];
238 static QEMUClockType event_clock_type
= QEMU_CLOCK_REALTIME
;
240 static void monitor_command_cb(void *opaque
, const char *cmdline
,
241 void *readline_opaque
);
244 * Is @mon a QMP monitor?
246 static inline bool monitor_is_qmp(const Monitor
*mon
)
248 return (mon
->flags
& MONITOR_USE_CONTROL
);
252 * Is the current monitor, if any, a QMP monitor?
254 bool monitor_cur_is_qmp(void)
256 return cur_mon
&& monitor_is_qmp(cur_mon
);
259 void monitor_read_command(Monitor
*mon
, int show_prompt
)
264 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
266 readline_show_prompt(mon
->rs
);
269 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
273 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
274 /* prompt is printed on return from the command handler */
277 monitor_printf(mon
, "terminal does not support password prompting\n");
282 static void monitor_flush_locked(Monitor
*mon
);
284 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
287 Monitor
*mon
= opaque
;
289 qemu_mutex_lock(&mon
->out_lock
);
291 monitor_flush_locked(mon
);
292 qemu_mutex_unlock(&mon
->out_lock
);
296 /* Called with mon->out_lock held. */
297 static void monitor_flush_locked(Monitor
*mon
)
303 if (mon
->skip_flush
) {
307 buf
= qstring_get_str(mon
->outbuf
);
308 len
= qstring_get_length(mon
->outbuf
);
310 if (len
&& !mon
->mux_out
) {
311 rc
= qemu_chr_fe_write(mon
->chr
, (const uint8_t *) buf
, len
);
312 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
313 /* all flushed or error */
314 QDECREF(mon
->outbuf
);
315 mon
->outbuf
= qstring_new();
320 QString
*tmp
= qstring_from_str(buf
+ rc
);
321 QDECREF(mon
->outbuf
);
324 if (mon
->out_watch
== 0) {
325 mon
->out_watch
= qemu_chr_fe_add_watch(mon
->chr
, G_IO_OUT
|G_IO_HUP
,
326 monitor_unblocked
, mon
);
331 void monitor_flush(Monitor
*mon
)
333 qemu_mutex_lock(&mon
->out_lock
);
334 monitor_flush_locked(mon
);
335 qemu_mutex_unlock(&mon
->out_lock
);
338 /* flush at every end of line */
339 static void monitor_puts(Monitor
*mon
, const char *str
)
343 qemu_mutex_lock(&mon
->out_lock
);
349 qstring_append_chr(mon
->outbuf
, '\r');
351 qstring_append_chr(mon
->outbuf
, c
);
353 monitor_flush_locked(mon
);
356 qemu_mutex_unlock(&mon
->out_lock
);
359 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
366 if (monitor_is_qmp(mon
)) {
370 buf
= g_strdup_vprintf(fmt
, ap
);
371 monitor_puts(mon
, buf
);
375 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
379 monitor_vprintf(mon
, fmt
, ap
);
383 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
387 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
392 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
396 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
397 qobject_to_json(data
);
398 assert(json
!= NULL
);
400 qstring_append_chr(json
, '\n');
401 monitor_puts(mon
, qstring_get_str(json
));
406 static QDict
*build_qmp_error_dict(Error
*err
)
410 obj
= qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
411 QapiErrorClass_lookup
[error_get_class(err
)],
412 error_get_pretty(err
));
414 return qobject_to_qdict(obj
);
417 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
,
422 trace_monitor_protocol_emitter(mon
);
425 /* success response */
428 qobject_incref(data
);
429 qdict_put_obj(qmp
, "return", data
);
431 /* return an empty QDict by default */
432 qdict_put(qmp
, "return", qdict_new());
436 qmp
= build_qmp_error_dict(err
);
440 qdict_put_obj(qmp
, "id", mon
->qmp
.id
);
444 monitor_json_emitter(mon
, QOBJECT(qmp
));
449 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
450 /* Limit guest-triggerable events to 1 per second */
451 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
452 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
453 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
454 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
455 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
456 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
459 GHashTable
*monitor_qapi_event_state
;
462 * Emits the event to every monitor instance, @event is only used for trace
463 * Called with monitor_lock held.
465 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
469 trace_monitor_protocol_event_emit(event
, qdict
);
470 QLIST_FOREACH(mon
, &mon_list
, entry
) {
471 if (monitor_is_qmp(mon
) && mon
->qmp
.in_command_mode
) {
472 monitor_json_emitter(mon
, QOBJECT(qdict
));
477 static void monitor_qapi_event_handler(void *opaque
);
480 * Queue a new event for emission to Monitor instances,
481 * applying any rate limiting if required.
484 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
486 MonitorQAPIEventConf
*evconf
;
487 MonitorQAPIEventState
*evstate
;
489 assert(event
< QAPI_EVENT__MAX
);
490 evconf
= &monitor_qapi_event_conf
[event
];
491 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
493 qemu_mutex_lock(&monitor_lock
);
496 /* Unthrottled event */
497 monitor_qapi_event_emit(event
, qdict
);
499 QDict
*data
= qobject_to_qdict(qdict_get(qdict
, "data"));
500 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
502 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
503 assert(!evstate
|| timer_pending(evstate
->timer
));
507 * Timer is pending for (at least) evconf->rate ns after
508 * last send. Store event for sending when timer fires,
509 * replacing a prior stored event if any.
511 QDECREF(evstate
->qdict
);
512 evstate
->qdict
= qdict
;
513 QINCREF(evstate
->qdict
);
516 * Last send was (at least) evconf->rate ns ago.
517 * Send immediately, and arm the timer to call
518 * monitor_qapi_event_handler() in evconf->rate ns. Any
519 * events arriving before then will be delayed until then.
521 int64_t now
= qemu_clock_get_ns(event_clock_type
);
523 monitor_qapi_event_emit(event
, qdict
);
525 evstate
= g_new(MonitorQAPIEventState
, 1);
526 evstate
->event
= event
;
527 evstate
->data
= data
;
528 QINCREF(evstate
->data
);
529 evstate
->qdict
= NULL
;
530 evstate
->timer
= timer_new_ns(event_clock_type
,
531 monitor_qapi_event_handler
,
533 g_hash_table_add(monitor_qapi_event_state
, evstate
);
534 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
538 qemu_mutex_unlock(&monitor_lock
);
542 * This function runs evconf->rate ns after sending a throttled
544 * If another event has since been stored, send it.
546 static void monitor_qapi_event_handler(void *opaque
)
548 MonitorQAPIEventState
*evstate
= opaque
;
549 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
551 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
552 qemu_mutex_lock(&monitor_lock
);
554 if (evstate
->qdict
) {
555 int64_t now
= qemu_clock_get_ns(event_clock_type
);
557 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
558 QDECREF(evstate
->qdict
);
559 evstate
->qdict
= NULL
;
560 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
562 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
563 QDECREF(evstate
->data
);
564 timer_free(evstate
->timer
);
568 qemu_mutex_unlock(&monitor_lock
);
571 static unsigned int qapi_event_throttle_hash(const void *key
)
573 const MonitorQAPIEventState
*evstate
= key
;
574 unsigned int hash
= evstate
->event
* 255;
576 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
577 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
580 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
581 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
587 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
589 const MonitorQAPIEventState
*eva
= a
;
590 const MonitorQAPIEventState
*evb
= b
;
592 if (eva
->event
!= evb
->event
) {
596 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
597 return !strcmp(qdict_get_str(eva
->data
, "id"),
598 qdict_get_str(evb
->data
, "id"));
601 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
602 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
603 qdict_get_str(evb
->data
, "node-name"));
609 static void monitor_qapi_event_init(void)
611 if (qtest_enabled()) {
612 event_clock_type
= QEMU_CLOCK_VIRTUAL
;
615 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
616 qapi_event_throttle_equal
);
617 qmp_event_set_func_emit(monitor_qapi_event_queue
);
620 static void qmp_capabilities(QDict
*params
, QObject
**ret_data
, Error
**errp
)
622 cur_mon
->qmp
.in_command_mode
= true;
625 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
627 static void monitor_data_init(Monitor
*mon
)
629 memset(mon
, 0, sizeof(Monitor
));
630 qemu_mutex_init(&mon
->out_lock
);
631 mon
->outbuf
= qstring_new();
632 /* Use *mon_cmds by default. */
633 mon
->cmd_table
= mon_cmds
;
636 static void monitor_data_destroy(Monitor
*mon
)
638 QDECREF(mon
->outbuf
);
639 qemu_mutex_destroy(&mon
->out_lock
);
642 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
643 int64_t cpu_index
, Error
**errp
)
646 Monitor
*old_mon
, hmp
;
648 monitor_data_init(&hmp
);
649 hmp
.skip_flush
= true;
655 int ret
= monitor_set_cpu(cpu_index
);
658 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
664 handle_hmp_command(&hmp
, command_line
);
667 qemu_mutex_lock(&hmp
.out_lock
);
668 if (qstring_get_length(hmp
.outbuf
) > 0) {
669 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
671 output
= g_strdup("");
673 qemu_mutex_unlock(&hmp
.out_lock
);
676 monitor_data_destroy(&hmp
);
680 static int compare_cmd(const char *name
, const char *list
)
682 const char *p
, *pstart
;
690 p
= pstart
+ strlen(pstart
);
691 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
700 static int get_str(char *buf
, int buf_size
, const char **pp
)
708 while (qemu_isspace(*p
)) {
719 while (*p
!= '\0' && *p
!= '\"') {
735 printf("unsupported escape code: '\\%c'\n", c
);
738 if ((q
- buf
) < buf_size
- 1) {
742 if ((q
- buf
) < buf_size
- 1) {
749 printf("unterminated string\n");
754 while (*p
!= '\0' && !qemu_isspace(*p
)) {
755 if ((q
- buf
) < buf_size
- 1) {
768 static void free_cmdline_args(char **args
, int nb_args
)
772 assert(nb_args
<= MAX_ARGS
);
774 for (i
= 0; i
< nb_args
; i
++) {
781 * Parse the command line to get valid args.
782 * @cmdline: command line to be parsed.
783 * @pnb_args: location to store the number of args, must NOT be NULL.
784 * @args: location to store the args, which should be freed by caller, must
787 * Returns 0 on success, negative on failure.
789 * NOTE: this parser is an approximate form of the real command parser. Number
790 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
791 * return with failure.
793 static int parse_cmdline(const char *cmdline
,
794 int *pnb_args
, char **args
)
803 while (qemu_isspace(*p
)) {
809 if (nb_args
>= MAX_ARGS
) {
812 ret
= get_str(buf
, sizeof(buf
), &p
);
816 args
[nb_args
] = g_strdup(buf
);
823 free_cmdline_args(args
, nb_args
);
827 static void help_cmd_dump_one(Monitor
*mon
,
828 const mon_cmd_t
*cmd
,
834 for (i
= 0; i
< prefix_args_nb
; i
++) {
835 monitor_printf(mon
, "%s ", prefix_args
[i
]);
837 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
840 /* @args[@arg_index] is the valid command need to find in @cmds */
841 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
842 char **args
, int nb_args
, int arg_index
)
844 const mon_cmd_t
*cmd
;
846 /* No valid arg need to compare with, dump all in *cmds */
847 if (arg_index
>= nb_args
) {
848 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
849 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
854 /* Find one entry to dump */
855 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
856 if (compare_cmd(args
[arg_index
], cmd
->name
)) {
857 if (cmd
->sub_table
) {
858 /* continue with next arg */
859 help_cmd_dump(mon
, cmd
->sub_table
,
860 args
, nb_args
, arg_index
+ 1);
862 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
869 static void help_cmd(Monitor
*mon
, const char *name
)
871 char *args
[MAX_ARGS
];
874 /* 1. parse user input */
876 /* special case for log, directly dump and return */
877 if (!strcmp(name
, "log")) {
878 const QEMULogItem
*item
;
879 monitor_printf(mon
, "Log items (comma separated):\n");
880 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
881 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
882 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
887 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
892 /* 2. dump the contents according to parsed args */
893 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
895 free_cmdline_args(args
, nb_args
);
898 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
900 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
903 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
905 const char *tp_name
= qdict_get_str(qdict
, "name");
906 bool new_state
= qdict_get_bool(qdict
, "option");
907 Error
*local_err
= NULL
;
909 qmp_trace_event_set_state(tp_name
, new_state
, true, true, &local_err
);
911 error_report_err(local_err
);
915 #ifdef CONFIG_TRACE_SIMPLE
916 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
918 const char *op
= qdict_get_try_str(qdict
, "op");
919 const char *arg
= qdict_get_try_str(qdict
, "arg");
922 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
923 } else if (!strcmp(op
, "on")) {
924 st_set_trace_file_enabled(true);
925 } else if (!strcmp(op
, "off")) {
926 st_set_trace_file_enabled(false);
927 } else if (!strcmp(op
, "flush")) {
928 st_flush_trace_buffer();
929 } else if (!strcmp(op
, "set")) {
931 st_set_trace_file(arg
);
934 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
935 help_cmd(mon
, "trace-file");
940 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
942 help_cmd(mon
, "info");
945 CommandInfoList
*qmp_query_commands(Error
**errp
)
947 CommandInfoList
*info
, *cmd_list
= NULL
;
948 const mon_cmd_t
*cmd
;
950 for (cmd
= qmp_cmds
; cmd
->name
!= NULL
; cmd
++) {
951 info
= g_malloc0(sizeof(*info
));
952 info
->value
= g_malloc0(sizeof(*info
->value
));
953 info
->value
->name
= g_strdup(cmd
->name
);
955 info
->next
= cmd_list
;
962 EventInfoList
*qmp_query_events(Error
**errp
)
964 EventInfoList
*info
, *ev_list
= NULL
;
967 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
968 const char *event_name
= QAPIEvent_lookup
[e
];
969 assert(event_name
!= NULL
);
970 info
= g_malloc0(sizeof(*info
));
971 info
->value
= g_malloc0(sizeof(*info
->value
));
972 info
->value
->name
= g_strdup(event_name
);
974 info
->next
= ev_list
;
982 * Minor hack: generated marshalling suppressed for this command
983 * ('gen': false in the schema) so we can parse the JSON string
984 * directly into QObject instead of first parsing it with
985 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
986 * to QObject with generated output marshallers, every time. Instead,
987 * we do it in test-qmp-input-visitor.c, just to make sure
988 * qapi-introspect.py's output actually conforms to the schema.
990 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
993 *ret_data
= qobject_from_json(qmp_schema_json
);
996 /* set the current CPU defined by the user */
997 int monitor_set_cpu(int cpu_index
)
1001 cpu
= qemu_get_cpu(cpu_index
);
1005 cur_mon
->mon_cpu
= cpu
;
1009 CPUState
*mon_get_cpu(void)
1011 if (!cur_mon
->mon_cpu
) {
1014 cpu_synchronize_state(cur_mon
->mon_cpu
);
1015 return cur_mon
->mon_cpu
;
1018 CPUArchState
*mon_get_cpu_env(void)
1020 return mon_get_cpu()->env_ptr
;
1023 int monitor_get_cpu_index(void)
1025 return mon_get_cpu()->cpu_index
;
1028 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1030 cpu_dump_state(mon_get_cpu(), (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1033 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1035 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1036 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1039 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1041 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1044 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1053 str
= readline_get_history(mon
->rs
, i
);
1056 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1061 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1063 cpu_dump_statistics(mon_get_cpu(), (FILE *)mon
, &monitor_fprintf
, 0);
1066 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1068 TraceEventInfoList
*events
= qmp_trace_event_get_state("*", NULL
);
1069 TraceEventInfoList
*elem
;
1071 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1072 monitor_printf(mon
, "%s : state %u\n",
1074 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1076 qapi_free_TraceEventInfoList(events
);
1079 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1080 bool has_port
, int64_t port
,
1081 bool has_tls_port
, int64_t tls_port
,
1082 bool has_cert_subject
, const char *cert_subject
,
1085 if (strcmp(protocol
, "spice") == 0) {
1086 if (!qemu_using_spice(errp
)) {
1090 if (!has_port
&& !has_tls_port
) {
1091 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1095 if (qemu_spice_migrate_info(hostname
,
1096 has_port
? port
: -1,
1097 has_tls_port
? tls_port
: -1,
1099 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1105 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1108 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1110 qemu_set_log_filename(qdict_get_str(qdict
, "filename"));
1113 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1116 const char *items
= qdict_get_str(qdict
, "items");
1118 if (!strcmp(items
, "none")) {
1121 mask
= qemu_str_to_log_mask(items
);
1123 help_cmd(mon
, "log");
1130 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1132 const char *option
= qdict_get_try_str(qdict
, "option");
1133 if (!option
|| !strcmp(option
, "on")) {
1135 } else if (!strcmp(option
, "off")) {
1138 monitor_printf(mon
, "unexpected option %s\n", option
);
1142 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1144 const char *device
= qdict_get_try_str(qdict
, "device");
1146 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1147 if (gdbserver_start(device
) < 0) {
1148 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1150 } else if (strcmp(device
, "none") == 0) {
1151 monitor_printf(mon
, "Disabled gdbserver\n");
1153 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1158 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1160 const char *action
= qdict_get_str(qdict
, "action");
1161 if (select_watchdog_action(action
) == -1) {
1162 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1166 static void monitor_printc(Monitor
*mon
, int c
)
1168 monitor_printf(mon
, "'");
1171 monitor_printf(mon
, "\\'");
1174 monitor_printf(mon
, "\\\\");
1177 monitor_printf(mon
, "\\n");
1180 monitor_printf(mon
, "\\r");
1183 if (c
>= 32 && c
<= 126) {
1184 monitor_printf(mon
, "%c", c
);
1186 monitor_printf(mon
, "\\x%02x", c
);
1190 monitor_printf(mon
, "'");
1193 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1194 hwaddr addr
, int is_physical
)
1196 int l
, line_size
, i
, max_digits
, len
;
1200 if (format
== 'i') {
1203 CPUArchState
*env
= mon_get_cpu_env();
1206 } else if (wsize
== 4) {
1209 /* as default we use the current CS size */
1212 #ifdef TARGET_X86_64
1213 if ((env
->efer
& MSR_EFER_LMA
) &&
1214 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
1218 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
1224 CPUArchState
*env
= mon_get_cpu_env();
1225 flags
= msr_le
<< 16;
1226 flags
|= env
->bfd_mach
;
1228 monitor_disas(mon
, mon_get_cpu(), addr
, count
, is_physical
, flags
);
1232 len
= wsize
* count
;
1241 max_digits
= (wsize
* 8 + 2) / 3;
1245 max_digits
= (wsize
* 8) / 4;
1249 max_digits
= (wsize
* 8 * 10 + 32) / 33;
1258 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1260 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1265 cpu_physical_memory_read(addr
, buf
, l
);
1267 if (cpu_memory_rw_debug(mon_get_cpu(), addr
, buf
, l
, 0) < 0) {
1268 monitor_printf(mon
, " Cannot access memory\n");
1277 v
= ldub_p(buf
+ i
);
1280 v
= lduw_p(buf
+ i
);
1283 v
= (uint32_t)ldl_p(buf
+ i
);
1289 monitor_printf(mon
, " ");
1292 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1295 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1298 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1301 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1304 monitor_printc(mon
, v
);
1309 monitor_printf(mon
, "\n");
1315 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1317 int count
= qdict_get_int(qdict
, "count");
1318 int format
= qdict_get_int(qdict
, "format");
1319 int size
= qdict_get_int(qdict
, "size");
1320 target_long addr
= qdict_get_int(qdict
, "addr");
1322 memory_dump(mon
, count
, format
, size
, addr
, 0);
1325 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1327 int count
= qdict_get_int(qdict
, "count");
1328 int format
= qdict_get_int(qdict
, "format");
1329 int size
= qdict_get_int(qdict
, "size");
1330 hwaddr addr
= qdict_get_int(qdict
, "addr");
1332 memory_dump(mon
, count
, format
, size
, addr
, 1);
1335 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1337 int format
= qdict_get_int(qdict
, "format");
1338 hwaddr val
= qdict_get_int(qdict
, "val");
1342 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1345 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1348 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1352 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1355 monitor_printc(mon
, val
);
1358 monitor_printf(mon
, "\n");
1361 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1365 uint32_t start
= qdict_get_int(qdict
, "start");
1366 uint32_t size
= qdict_get_int(qdict
, "size");
1369 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1370 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1371 MEMTXATTRS_UNSPECIFIED
, NULL
);
1372 /* BSD sum algorithm ('sum' Unix command) */
1373 sum
= (sum
>> 1) | (sum
<< 15);
1376 monitor_printf(mon
, "%05d\n", sum
);
1379 static int mouse_button_state
;
1381 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1383 int dx
, dy
, dz
, button
;
1384 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1385 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1386 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1388 dx
= strtol(dx_str
, NULL
, 0);
1389 dy
= strtol(dy_str
, NULL
, 0);
1390 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1391 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1394 dz
= strtol(dz_str
, NULL
, 0);
1396 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1397 qemu_input_queue_btn(NULL
, button
, true);
1398 qemu_input_event_sync();
1399 qemu_input_queue_btn(NULL
, button
, false);
1402 qemu_input_event_sync();
1405 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1407 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1408 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1409 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1410 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1412 int button_state
= qdict_get_int(qdict
, "button_state");
1414 if (mouse_button_state
== button_state
) {
1417 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1418 qemu_input_event_sync();
1419 mouse_button_state
= button_state
;
1422 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1424 int size
= qdict_get_int(qdict
, "size");
1425 int addr
= qdict_get_int(qdict
, "addr");
1426 int has_index
= qdict_haskey(qdict
, "index");
1431 int index
= qdict_get_int(qdict
, "index");
1432 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1440 val
= cpu_inb(addr
);
1444 val
= cpu_inw(addr
);
1448 val
= cpu_inl(addr
);
1452 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1453 suffix
, addr
, size
* 2, val
);
1456 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1458 int size
= qdict_get_int(qdict
, "size");
1459 int addr
= qdict_get_int(qdict
, "addr");
1460 int val
= qdict_get_int(qdict
, "val");
1462 addr
&= IOPORTS_MASK
;
1467 cpu_outb(addr
, val
);
1470 cpu_outw(addr
, val
);
1473 cpu_outl(addr
, val
);
1478 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1480 Error
*local_err
= NULL
;
1481 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1483 qemu_boot_set(bootdevice
, &local_err
);
1485 error_report_err(local_err
);
1487 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1491 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1493 mtree_info((fprintf_function
)monitor_printf
, mon
);
1496 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1502 node_mem
= g_new0(uint64_t, nb_numa_nodes
);
1503 query_numa_node_mem(node_mem
);
1504 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1505 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1506 monitor_printf(mon
, "node %d cpus:", i
);
1508 if (cpu
->numa_node
== i
) {
1509 monitor_printf(mon
, " %d", cpu
->cpu_index
);
1512 monitor_printf(mon
, "\n");
1513 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1519 #ifdef CONFIG_PROFILER
1524 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1526 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1527 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1528 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1529 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
1534 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1536 monitor_printf(mon
, "Internal profiler not compiled\n");
1540 /* Capture support */
1541 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1543 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1548 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1549 monitor_printf(mon
, "[%d]: ", i
);
1550 s
->ops
.info (s
->opaque
);
1554 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1557 int n
= qdict_get_int(qdict
, "n");
1560 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1562 s
->ops
.destroy (s
->opaque
);
1563 QLIST_REMOVE (s
, entries
);
1570 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1572 const char *path
= qdict_get_str(qdict
, "path");
1573 int has_freq
= qdict_haskey(qdict
, "freq");
1574 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1575 int has_bits
= qdict_haskey(qdict
, "bits");
1576 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1577 int has_channels
= qdict_haskey(qdict
, "nchannels");
1578 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1581 s
= g_malloc0 (sizeof (*s
));
1583 freq
= has_freq
? freq
: 44100;
1584 bits
= has_bits
? bits
: 16;
1585 nchannels
= has_channels
? nchannels
: 2;
1587 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1588 monitor_printf(mon
, "Failed to add wave capture\n");
1592 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1595 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1597 qemu_acl
*acl
= qemu_acl_find(name
);
1600 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1605 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1607 const char *aclname
= qdict_get_str(qdict
, "aclname");
1608 qemu_acl
*acl
= find_acl(mon
, aclname
);
1609 qemu_acl_entry
*entry
;
1613 monitor_printf(mon
, "policy: %s\n",
1614 acl
->defaultDeny
? "deny" : "allow");
1615 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1617 monitor_printf(mon
, "%d: %s %s\n", i
,
1618 entry
->deny
? "deny" : "allow", entry
->match
);
1623 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1625 const char *aclname
= qdict_get_str(qdict
, "aclname");
1626 qemu_acl
*acl
= find_acl(mon
, aclname
);
1629 qemu_acl_reset(acl
);
1630 monitor_printf(mon
, "acl: removed all rules\n");
1634 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1636 const char *aclname
= qdict_get_str(qdict
, "aclname");
1637 const char *policy
= qdict_get_str(qdict
, "policy");
1638 qemu_acl
*acl
= find_acl(mon
, aclname
);
1641 if (strcmp(policy
, "allow") == 0) {
1642 acl
->defaultDeny
= 0;
1643 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1644 } else if (strcmp(policy
, "deny") == 0) {
1645 acl
->defaultDeny
= 1;
1646 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1648 monitor_printf(mon
, "acl: unknown policy '%s', "
1649 "expected 'deny' or 'allow'\n", policy
);
1654 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1656 const char *aclname
= qdict_get_str(qdict
, "aclname");
1657 const char *match
= qdict_get_str(qdict
, "match");
1658 const char *policy
= qdict_get_str(qdict
, "policy");
1659 int has_index
= qdict_haskey(qdict
, "index");
1660 int index
= qdict_get_try_int(qdict
, "index", -1);
1661 qemu_acl
*acl
= find_acl(mon
, aclname
);
1665 if (strcmp(policy
, "allow") == 0) {
1667 } else if (strcmp(policy
, "deny") == 0) {
1670 monitor_printf(mon
, "acl: unknown policy '%s', "
1671 "expected 'deny' or 'allow'\n", policy
);
1675 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1677 ret
= qemu_acl_append(acl
, deny
, match
);
1679 monitor_printf(mon
, "acl: unable to add acl entry\n");
1681 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1685 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1687 const char *aclname
= qdict_get_str(qdict
, "aclname");
1688 const char *match
= qdict_get_str(qdict
, "match");
1689 qemu_acl
*acl
= find_acl(mon
, aclname
);
1693 ret
= qemu_acl_remove(acl
, match
);
1695 monitor_printf(mon
, "acl: no matching acl entry\n");
1697 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1701 void qmp_getfd(const char *fdname
, Error
**errp
)
1706 fd
= qemu_chr_fe_get_msgfd(cur_mon
->chr
);
1708 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1712 if (qemu_isdigit(fdname
[0])) {
1714 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
1715 "a name not starting with a digit");
1719 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1720 if (strcmp(monfd
->name
, fdname
) != 0) {
1729 monfd
= g_malloc0(sizeof(mon_fd_t
));
1730 monfd
->name
= g_strdup(fdname
);
1733 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
1736 void qmp_closefd(const char *fdname
, Error
**errp
)
1740 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
1741 if (strcmp(monfd
->name
, fdname
) != 0) {
1745 QLIST_REMOVE(monfd
, next
);
1747 g_free(monfd
->name
);
1752 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
1755 static void hmp_loadvm(Monitor
*mon
, const QDict
*qdict
)
1757 int saved_vm_running
= runstate_is_running();
1758 const char *name
= qdict_get_str(qdict
, "name");
1760 vm_stop(RUN_STATE_RESTORE_VM
);
1762 if (load_vmstate(name
) == 0 && saved_vm_running
) {
1767 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
1771 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1774 if (strcmp(monfd
->name
, fdname
) != 0) {
1780 /* caller takes ownership of fd */
1781 QLIST_REMOVE(monfd
, next
);
1782 g_free(monfd
->name
);
1788 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
1792 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
1794 MonFdsetFd
*mon_fdset_fd
;
1795 MonFdsetFd
*mon_fdset_fd_next
;
1797 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
1798 if ((mon_fdset_fd
->removed
||
1799 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
1800 runstate_is_running()) {
1801 close(mon_fdset_fd
->fd
);
1802 g_free(mon_fdset_fd
->opaque
);
1803 QLIST_REMOVE(mon_fdset_fd
, next
);
1804 g_free(mon_fdset_fd
);
1808 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
1809 QLIST_REMOVE(mon_fdset
, next
);
1814 static void monitor_fdsets_cleanup(void)
1816 MonFdset
*mon_fdset
;
1817 MonFdset
*mon_fdset_next
;
1819 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
1820 monitor_fdset_cleanup(mon_fdset
);
1824 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
1825 const char *opaque
, Error
**errp
)
1828 Monitor
*mon
= cur_mon
;
1831 fd
= qemu_chr_fe_get_msgfd(mon
->chr
);
1833 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
1837 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
1838 has_opaque
, opaque
, errp
);
1850 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
1852 MonFdset
*mon_fdset
;
1853 MonFdsetFd
*mon_fdset_fd
;
1856 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1857 if (mon_fdset
->id
!= fdset_id
) {
1860 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1862 if (mon_fdset_fd
->fd
!= fd
) {
1865 mon_fdset_fd
->removed
= true;
1868 mon_fdset_fd
->removed
= true;
1871 if (has_fd
&& !mon_fdset_fd
) {
1874 monitor_fdset_cleanup(mon_fdset
);
1880 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
1883 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
1885 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
1888 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
1890 MonFdset
*mon_fdset
;
1891 MonFdsetFd
*mon_fdset_fd
;
1892 FdsetInfoList
*fdset_list
= NULL
;
1894 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1895 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
1896 FdsetFdInfoList
*fdsetfd_list
= NULL
;
1898 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
1899 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
1901 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
1902 FdsetFdInfoList
*fdsetfd_info
;
1904 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
1905 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
1906 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
1907 if (mon_fdset_fd
->opaque
) {
1908 fdsetfd_info
->value
->has_opaque
= true;
1909 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
1911 fdsetfd_info
->value
->has_opaque
= false;
1914 fdsetfd_info
->next
= fdsetfd_list
;
1915 fdsetfd_list
= fdsetfd_info
;
1918 fdset_info
->value
->fds
= fdsetfd_list
;
1920 fdset_info
->next
= fdset_list
;
1921 fdset_list
= fdset_info
;
1927 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
1928 bool has_opaque
, const char *opaque
,
1931 MonFdset
*mon_fdset
= NULL
;
1932 MonFdsetFd
*mon_fdset_fd
;
1936 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1937 /* Break if match found or match impossible due to ordering by ID */
1938 if (fdset_id
<= mon_fdset
->id
) {
1939 if (fdset_id
< mon_fdset
->id
) {
1947 if (mon_fdset
== NULL
) {
1948 int64_t fdset_id_prev
= -1;
1949 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
1953 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
1954 "a non-negative value");
1957 /* Use specified fdset ID */
1958 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1959 mon_fdset_cur
= mon_fdset
;
1960 if (fdset_id
< mon_fdset_cur
->id
) {
1965 /* Use first available fdset ID */
1966 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
1967 mon_fdset_cur
= mon_fdset
;
1968 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
1969 fdset_id_prev
= mon_fdset_cur
->id
;
1976 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
1978 mon_fdset
->id
= fdset_id
;
1980 mon_fdset
->id
= fdset_id_prev
+ 1;
1983 /* The fdset list is ordered by fdset ID */
1984 if (!mon_fdset_cur
) {
1985 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
1986 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
1987 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
1989 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
1993 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
1994 mon_fdset_fd
->fd
= fd
;
1995 mon_fdset_fd
->removed
= false;
1997 mon_fdset_fd
->opaque
= g_strdup(opaque
);
1999 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2001 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2002 fdinfo
->fdset_id
= mon_fdset
->id
;
2003 fdinfo
->fd
= mon_fdset_fd
->fd
;
2008 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2011 MonFdset
*mon_fdset
;
2012 MonFdsetFd
*mon_fdset_fd
;
2015 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2016 if (mon_fdset
->id
!= fdset_id
) {
2019 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2020 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2021 if (mon_fd_flags
== -1) {
2025 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2026 return mon_fdset_fd
->fd
;
2038 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2040 MonFdset
*mon_fdset
;
2041 MonFdsetFd
*mon_fdset_fd_dup
;
2043 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2044 if (mon_fdset
->id
!= fdset_id
) {
2047 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2048 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2052 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2053 mon_fdset_fd_dup
->fd
= dup_fd
;
2054 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2060 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2062 MonFdset
*mon_fdset
;
2063 MonFdsetFd
*mon_fdset_fd_dup
;
2065 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2066 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2067 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2069 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2070 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2071 monitor_fdset_cleanup(mon_fdset
);
2075 return mon_fdset
->id
;
2083 int monitor_fdset_dup_fd_find(int dup_fd
)
2085 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2088 void monitor_fdset_dup_fd_remove(int dup_fd
)
2090 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2093 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2096 Error
*local_err
= NULL
;
2098 if (!qemu_isdigit(fdname
[0]) && mon
) {
2099 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2101 fd
= qemu_parse_fd(fdname
);
2103 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2108 error_propagate(errp
, local_err
);
2117 /* Please update hmp-commands.hx when adding or changing commands */
2118 static mon_cmd_t info_cmds
[] = {
2119 #include "hmp-commands-info.h"
2123 /* mon_cmds and info_cmds would be sorted at runtime */
2124 static mon_cmd_t mon_cmds
[] = {
2125 #include "hmp-commands.h"
2129 static const mon_cmd_t qmp_cmds
[] = {
2130 #include "qmp-commands-old.h"
2134 /*******************************************************************/
2136 static const char *pch
;
2137 static sigjmp_buf expr_env
;
2140 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2141 expr_error(Monitor
*mon
, const char *fmt
, ...)
2145 monitor_vprintf(mon
, fmt
, ap
);
2146 monitor_printf(mon
, "\n");
2148 siglongjmp(expr_env
, 1);
2151 /* return 0 if OK, -1 if not found */
2152 static int get_monitor_def(target_long
*pval
, const char *name
)
2154 const MonitorDef
*md
= target_monitor_defs();
2163 for(; md
->name
!= NULL
; md
++) {
2164 if (compare_cmd(name
, md
->name
)) {
2165 if (md
->get_value
) {
2166 *pval
= md
->get_value(md
, md
->offset
);
2168 CPUArchState
*env
= mon_get_cpu_env();
2169 ptr
= (uint8_t *)env
+ md
->offset
;
2172 *pval
= *(int32_t *)ptr
;
2175 *pval
= *(target_long
*)ptr
;
2186 ret
= target_get_monitor_def(mon_get_cpu(), name
, &tmp
);
2188 *pval
= (target_long
) tmp
;
2194 static void next(void)
2198 while (qemu_isspace(*pch
))
2203 static int64_t expr_sum(Monitor
*mon
);
2205 static int64_t expr_unary(Monitor
*mon
)
2214 n
= expr_unary(mon
);
2218 n
= -expr_unary(mon
);
2222 n
= ~expr_unary(mon
);
2228 expr_error(mon
, "')' expected");
2235 expr_error(mon
, "character constant expected");
2239 expr_error(mon
, "missing terminating \' character");
2249 while ((*pch
>= 'a' && *pch
<= 'z') ||
2250 (*pch
>= 'A' && *pch
<= 'Z') ||
2251 (*pch
>= '0' && *pch
<= '9') ||
2252 *pch
== '_' || *pch
== '.') {
2253 if ((q
- buf
) < sizeof(buf
) - 1)
2257 while (qemu_isspace(*pch
))
2260 ret
= get_monitor_def(®
, buf
);
2262 expr_error(mon
, "unknown register");
2267 expr_error(mon
, "unexpected end of expression");
2272 n
= strtoull(pch
, &p
, 0);
2273 if (errno
== ERANGE
) {
2274 expr_error(mon
, "number too large");
2277 expr_error(mon
, "invalid char '%c' in expression", *p
);
2280 while (qemu_isspace(*pch
))
2288 static int64_t expr_prod(Monitor
*mon
)
2293 val
= expr_unary(mon
);
2296 if (op
!= '*' && op
!= '/' && op
!= '%')
2299 val2
= expr_unary(mon
);
2308 expr_error(mon
, "division by zero");
2319 static int64_t expr_logic(Monitor
*mon
)
2324 val
= expr_prod(mon
);
2327 if (op
!= '&' && op
!= '|' && op
!= '^')
2330 val2
= expr_prod(mon
);
2347 static int64_t expr_sum(Monitor
*mon
)
2352 val
= expr_logic(mon
);
2355 if (op
!= '+' && op
!= '-')
2358 val2
= expr_logic(mon
);
2367 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2370 if (sigsetjmp(expr_env
, 0)) {
2374 while (qemu_isspace(*pch
))
2376 *pval
= expr_sum(mon
);
2381 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2383 const char *p
= *pp
;
2387 d
= strtod(p
, &tailp
);
2389 monitor_printf(mon
, "Number expected\n");
2392 if (d
!= d
|| d
- d
!= 0) {
2393 /* NaN or infinity */
2394 monitor_printf(mon
, "Bad number\n");
2403 * Store the command-name in cmdname, and return a pointer to
2404 * the remaining of the command string.
2406 static const char *get_command_name(const char *cmdline
,
2407 char *cmdname
, size_t nlen
)
2410 const char *p
, *pstart
;
2413 while (qemu_isspace(*p
))
2418 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2423 memcpy(cmdname
, pstart
, len
);
2424 cmdname
[len
] = '\0';
2429 * Read key of 'type' into 'key' and return the current
2432 static char *key_get_info(const char *type
, char **key
)
2440 p
= strchr(type
, ':');
2447 str
= g_malloc(len
+ 1);
2448 memcpy(str
, type
, len
);
2455 static int default_fmt_format
= 'x';
2456 static int default_fmt_size
= 4;
2458 static int is_valid_option(const char *c
, const char *typestr
)
2466 typestr
= strstr(typestr
, option
);
2467 return (typestr
!= NULL
);
2470 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2471 const char *cmdname
)
2473 const mon_cmd_t
*cmd
;
2475 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2476 if (compare_cmd(cmdname
, cmd
->name
)) {
2484 static const mon_cmd_t
*qmp_find_cmd(const char *cmdname
)
2486 return search_dispatch_table(qmp_cmds
, cmdname
);
2490 * Parse command name from @cmdp according to command table @table.
2491 * If blank, return NULL.
2492 * Else, if no valid command can be found, report to @mon, and return
2494 * Else, change @cmdp to point right behind the name, and return its
2495 * command table entry.
2496 * Do not assume the return value points into @table! It doesn't when
2497 * the command is found in a sub-command table.
2499 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2504 const mon_cmd_t
*cmd
;
2507 /* extract the command name */
2508 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2512 cmd
= search_dispatch_table(table
, cmdname
);
2514 monitor_printf(mon
, "unknown command: '%.*s'\n",
2515 (int)(p
- *cmdp
), *cmdp
);
2519 /* filter out following useless space */
2520 while (qemu_isspace(*p
)) {
2525 /* search sub command */
2526 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2527 return monitor_parse_command(mon
, cmdp
, cmd
->sub_table
);
2534 * Parse arguments for @cmd.
2535 * If it can't be parsed, report to @mon, and return NULL.
2536 * Else, insert command arguments into a QDict, and return it.
2537 * Note: On success, caller has to free the QDict structure.
2540 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2542 const mon_cmd_t
*cmd
)
2544 const char *typestr
;
2547 const char *p
= *endp
;
2549 QDict
*qdict
= qdict_new();
2551 /* parse the parameters */
2552 typestr
= cmd
->args_type
;
2554 typestr
= key_get_info(typestr
, &key
);
2566 while (qemu_isspace(*p
))
2568 if (*typestr
== '?') {
2571 /* no optional string: NULL argument */
2575 ret
= get_str(buf
, sizeof(buf
), &p
);
2579 monitor_printf(mon
, "%s: filename expected\n",
2583 monitor_printf(mon
, "%s: block device name expected\n",
2587 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2592 qdict_put(qdict
, key
, qstring_from_str(buf
));
2597 QemuOptsList
*opts_list
;
2600 opts_list
= qemu_find_opts(key
);
2601 if (!opts_list
|| opts_list
->desc
->name
) {
2604 while (qemu_isspace(*p
)) {
2609 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2612 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2616 qemu_opts_to_qdict(opts
, qdict
);
2617 qemu_opts_del(opts
);
2622 int count
, format
, size
;
2624 while (qemu_isspace(*p
))
2630 if (qemu_isdigit(*p
)) {
2632 while (qemu_isdigit(*p
)) {
2633 count
= count
* 10 + (*p
- '0');
2671 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2672 monitor_printf(mon
, "invalid char in format: '%c'\n",
2677 format
= default_fmt_format
;
2678 if (format
!= 'i') {
2679 /* for 'i', not specifying a size gives -1 as size */
2681 size
= default_fmt_size
;
2682 default_fmt_size
= size
;
2684 default_fmt_format
= format
;
2687 format
= default_fmt_format
;
2688 if (format
!= 'i') {
2689 size
= default_fmt_size
;
2694 qdict_put(qdict
, "count", qint_from_int(count
));
2695 qdict_put(qdict
, "format", qint_from_int(format
));
2696 qdict_put(qdict
, "size", qint_from_int(size
));
2705 while (qemu_isspace(*p
))
2707 if (*typestr
== '?' || *typestr
== '.') {
2708 if (*typestr
== '?') {
2716 while (qemu_isspace(*p
))
2725 if (get_expr(mon
, &val
, &p
))
2727 /* Check if 'i' is greater than 32-bit */
2728 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2729 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
2730 monitor_printf(mon
, "integer is for 32-bit values\n");
2732 } else if (c
== 'M') {
2734 monitor_printf(mon
, "enter a positive value\n");
2739 qdict_put(qdict
, key
, qint_from_int(val
));
2747 while (qemu_isspace(*p
)) {
2750 if (*typestr
== '?') {
2756 val
= qemu_strtosz(p
, &end
);
2758 monitor_printf(mon
, "invalid size\n");
2761 qdict_put(qdict
, key
, qint_from_int(val
));
2769 while (qemu_isspace(*p
))
2771 if (*typestr
== '?') {
2777 if (get_double(mon
, &val
, &p
) < 0) {
2780 if (p
[0] && p
[1] == 's') {
2783 val
/= 1e3
; p
+= 2; break;
2785 val
/= 1e6
; p
+= 2; break;
2787 val
/= 1e9
; p
+= 2; break;
2790 if (*p
&& !qemu_isspace(*p
)) {
2791 monitor_printf(mon
, "Unknown unit suffix\n");
2794 qdict_put(qdict
, key
, qfloat_from_double(val
));
2802 while (qemu_isspace(*p
)) {
2806 while (qemu_isgraph(*p
)) {
2809 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
2811 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
2814 monitor_printf(mon
, "Expected 'on' or 'off'\n");
2817 qdict_put(qdict
, key
, qbool_from_bool(val
));
2822 const char *tmp
= p
;
2829 while (qemu_isspace(*p
))
2834 if(!is_valid_option(p
, typestr
)) {
2836 monitor_printf(mon
, "%s: unsupported option -%c\n",
2848 qdict_put(qdict
, key
, qbool_from_bool(true));
2855 /* package all remaining string */
2858 while (qemu_isspace(*p
)) {
2861 if (*typestr
== '?') {
2864 /* no remaining string: NULL argument */
2870 monitor_printf(mon
, "%s: string expected\n",
2874 qdict_put(qdict
, key
, qstring_from_str(p
));
2880 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
2886 /* check that all arguments were parsed */
2887 while (qemu_isspace(*p
))
2890 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2903 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
2906 const mon_cmd_t
*cmd
;
2908 cmd
= monitor_parse_command(mon
, &cmdline
, mon
->cmd_table
);
2913 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
2915 monitor_printf(mon
, "Try \"help %s\" for more information\n",
2920 cmd
->mhandler
.cmd(mon
, qdict
);
2924 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
2926 const char *p
, *pstart
;
2935 p
= pstart
+ strlen(pstart
);
2937 if (len
> sizeof(cmd
) - 2)
2938 len
= sizeof(cmd
) - 2;
2939 memcpy(cmd
, pstart
, len
);
2941 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2942 readline_add_completion(mon
->rs
, cmd
);
2950 static void file_completion(Monitor
*mon
, const char *input
)
2955 char file
[1024], file_prefix
[1024];
2959 p
= strrchr(input
, '/');
2962 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2963 pstrcpy(path
, sizeof(path
), ".");
2965 input_path_len
= p
- input
+ 1;
2966 memcpy(path
, input
, input_path_len
);
2967 if (input_path_len
> sizeof(path
) - 1)
2968 input_path_len
= sizeof(path
) - 1;
2969 path
[input_path_len
] = '\0';
2970 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2973 ffs
= opendir(path
);
2982 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
2986 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2987 memcpy(file
, input
, input_path_len
);
2988 if (input_path_len
< sizeof(file
))
2989 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2991 /* stat the file to find out if it's a directory.
2992 * In that case add a slash to speed up typing long paths
2994 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
2995 pstrcat(file
, sizeof(file
), "/");
2997 readline_add_completion(mon
->rs
, file
);
3003 static const char *next_arg_type(const char *typestr
)
3005 const char *p
= strchr(typestr
, ':');
3006 return (p
!= NULL
? ++p
: typestr
);
3009 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3012 if (!str
|| !option
) {
3015 if (!strncmp(option
, str
, strlen(str
))) {
3016 readline_add_completion(rs
, option
);
3020 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3023 ChardevBackendInfoList
*list
, *start
;
3029 readline_set_completion_index(rs
, len
);
3031 start
= list
= qmp_query_chardev_backends(NULL
);
3033 const char *chr_name
= list
->value
->name
;
3035 if (!strncmp(chr_name
, str
, len
)) {
3036 readline_add_completion(rs
, chr_name
);
3040 qapi_free_ChardevBackendInfoList(start
);
3043 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3052 readline_set_completion_index(rs
, len
);
3053 for (i
= 0; NetClientOptionsKind_lookup
[i
]; i
++) {
3054 add_completion_option(rs
, str
, NetClientOptionsKind_lookup
[i
]);
3058 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3068 readline_set_completion_index(rs
, len
);
3069 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3072 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3074 name
= object_class_get_name(OBJECT_CLASS(dc
));
3076 if (!dc
->cannot_instantiate_with_device_add_yet
3077 && !strncmp(name
, str
, len
)) {
3078 readline_add_completion(rs
, name
);
3085 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3095 readline_set_completion_index(rs
, len
);
3096 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3100 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3101 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3102 readline_add_completion(rs
, name
);
3109 static void peripheral_device_del_completion(ReadLineState
*rs
,
3110 const char *str
, size_t len
)
3112 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3113 GSList
*list
, *item
;
3115 list
= qdev_build_hotpluggable_device_list(peripheral
);
3120 for (item
= list
; item
; item
= g_slist_next(item
)) {
3121 DeviceState
*dev
= item
->data
;
3123 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3124 readline_add_completion(rs
, dev
->id
);
3131 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3134 ChardevInfoList
*list
, *start
;
3140 readline_set_completion_index(rs
, len
);
3142 start
= list
= qmp_query_chardev(NULL
);
3144 ChardevInfo
*chr
= list
->value
;
3146 if (!strncmp(chr
->label
, str
, len
)) {
3147 readline_add_completion(rs
, chr
->label
);
3151 qapi_free_ChardevInfoList(start
);
3154 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3157 ChardevInfoList
*list
, *start
;
3160 readline_set_completion_index(rs
, len
);
3162 start
= list
= qmp_query_chardev(NULL
);
3164 ChardevInfo
*chr_info
= list
->value
;
3166 if (!strncmp(chr_info
->label
, str
, len
)) {
3167 CharDriverState
*chr
= qemu_chr_find(chr_info
->label
);
3168 if (chr
&& chr_is_ringbuf(chr
)) {
3169 readline_add_completion(rs
, chr_info
->label
);
3174 qapi_free_ChardevInfoList(start
);
3177 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3182 ringbuf_completion(rs
, str
);
3185 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3194 readline_set_completion_index(rs
, len
);
3195 peripheral_device_del_completion(rs
, str
, len
);
3198 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3200 ObjectPropertyInfoList
*list
, *start
;
3207 readline_set_completion_index(rs
, len
);
3209 start
= list
= qmp_qom_list("/objects", NULL
);
3211 ObjectPropertyInfo
*info
= list
->value
;
3213 if (!strncmp(info
->type
, "child<", 5)
3214 && !strncmp(info
->name
, str
, len
)) {
3215 readline_add_completion(rs
, info
->name
);
3219 qapi_free_ObjectPropertyInfoList(start
);
3222 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3231 sep
= strrchr(str
, '-');
3236 readline_set_completion_index(rs
, len
);
3237 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3238 if (!strncmp(str
, QKeyCode_lookup
[i
], len
)) {
3239 readline_add_completion(rs
, QKeyCode_lookup
[i
]);
3244 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3249 readline_set_completion_index(rs
, len
);
3251 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3253 count
= qemu_find_net_clients_except(NULL
, ncs
,
3254 NET_CLIENT_OPTIONS_KIND_NONE
,
3256 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3257 const char *name
= ncs
[i
]->name
;
3258 if (!strncmp(str
, name
, len
)) {
3259 readline_add_completion(rs
, name
);
3262 } else if (nb_args
== 3) {
3263 add_completion_option(rs
, str
, "on");
3264 add_completion_option(rs
, str
, "off");
3268 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3271 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3278 readline_set_completion_index(rs
, len
);
3279 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_OPTIONS_KIND_NIC
,
3281 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3283 const char *name
= ncs
[i
]->name
;
3284 if (strncmp(str
, name
, len
)) {
3287 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3289 readline_add_completion(rs
, name
);
3294 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3299 readline_set_completion_index(rs
, len
);
3302 for (id
= 0; id
< trace_event_count(); id
++) {
3303 const char *event_name
= trace_event_get_name(trace_event_id(id
));
3304 if (!strncmp(str
, event_name
, len
)) {
3305 readline_add_completion(rs
, event_name
);
3308 } else if (nb_args
== 3) {
3309 add_completion_option(rs
, str
, "on");
3310 add_completion_option(rs
, str
, "off");
3314 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3321 readline_set_completion_index(rs
, strlen(str
));
3322 for (i
= 0; WatchdogExpirationAction_lookup
[i
]; i
++) {
3323 add_completion_option(rs
, str
, WatchdogExpirationAction_lookup
[i
]);
3327 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3333 readline_set_completion_index(rs
, len
);
3336 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3337 const char *name
= MigrationCapability_lookup
[i
];
3338 if (!strncmp(str
, name
, len
)) {
3339 readline_add_completion(rs
, name
);
3342 } else if (nb_args
== 3) {
3343 add_completion_option(rs
, str
, "on");
3344 add_completion_option(rs
, str
, "off");
3348 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3354 readline_set_completion_index(rs
, len
);
3357 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3358 const char *name
= MigrationParameter_lookup
[i
];
3359 if (!strncmp(str
, name
, len
)) {
3360 readline_add_completion(rs
, name
);
3366 void host_net_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3374 readline_set_completion_index(rs
, len
);
3375 for (i
= 0; host_net_devices
[i
]; i
++) {
3376 if (!strncmp(host_net_devices
[i
], str
, len
)) {
3377 readline_add_completion(rs
, host_net_devices
[i
]);
3382 void host_net_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3384 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3388 readline_set_completion_index(rs
, len
);
3390 count
= qemu_find_net_clients_except(NULL
, ncs
,
3391 NET_CLIENT_OPTIONS_KIND_NONE
,
3393 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3397 if (net_hub_id_for_client(ncs
[i
], &id
)) {
3400 snprintf(name
, sizeof(name
), "%d", id
);
3401 if (!strncmp(str
, name
, len
)) {
3402 readline_add_completion(rs
, name
);
3406 } else if (nb_args
== 3) {
3407 count
= qemu_find_net_clients_except(NULL
, ncs
,
3408 NET_CLIENT_OPTIONS_KIND_NIC
,
3410 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3414 if (ncs
[i
]->info
->type
== NET_CLIENT_OPTIONS_KIND_HUBPORT
||
3415 net_hub_id_for_client(ncs
[i
], &id
)) {
3418 name
= ncs
[i
]->name
;
3419 if (!strncmp(str
, name
, len
)) {
3420 readline_add_completion(rs
, name
);
3427 static void vm_completion(ReadLineState
*rs
, const char *str
)
3430 BlockDriverState
*bs
;
3431 BdrvNextIterator
*it
= NULL
;
3434 readline_set_completion_index(rs
, len
);
3436 while ((it
= bdrv_next(it
, &bs
))) {
3437 SnapshotInfoList
*snapshots
, *snapshot
;
3438 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3441 aio_context_acquire(ctx
);
3442 if (bdrv_can_snapshot(bs
)) {
3443 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3445 aio_context_release(ctx
);
3450 snapshot
= snapshots
;
3452 char *completion
= snapshot
->value
->name
;
3453 if (!strncmp(str
, completion
, len
)) {
3454 readline_add_completion(rs
, completion
);
3456 completion
= snapshot
->value
->id
;
3457 if (!strncmp(str
, completion
, len
)) {
3458 readline_add_completion(rs
, completion
);
3460 snapshot
= snapshot
->next
;
3462 qapi_free_SnapshotInfoList(snapshots
);
3467 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3470 vm_completion(rs
, str
);
3474 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3477 vm_completion(rs
, str
);
3481 static void monitor_find_completion_by_table(Monitor
*mon
,
3482 const mon_cmd_t
*cmd_table
,
3486 const char *cmdname
;
3488 const char *ptype
, *str
, *name
;
3489 const mon_cmd_t
*cmd
;
3490 BlockBackend
*blk
= NULL
;
3493 /* command completion */
3498 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3499 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3500 cmd_completion(mon
, cmdname
, cmd
->name
);
3503 /* find the command */
3504 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3505 if (compare_cmd(args
[0], cmd
->name
)) {
3513 if (cmd
->sub_table
) {
3514 /* do the job again */
3515 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3516 &args
[1], nb_args
- 1);
3519 if (cmd
->command_completion
) {
3520 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3524 ptype
= next_arg_type(cmd
->args_type
);
3525 for(i
= 0; i
< nb_args
- 2; i
++) {
3526 if (*ptype
!= '\0') {
3527 ptype
= next_arg_type(ptype
);
3528 while (*ptype
== '?')
3529 ptype
= next_arg_type(ptype
);
3532 str
= args
[nb_args
- 1];
3533 while (*ptype
== '-' && ptype
[1] != '\0') {
3534 ptype
= next_arg_type(ptype
);
3538 /* file completion */
3539 readline_set_completion_index(mon
->rs
, strlen(str
));
3540 file_completion(mon
, str
);
3543 /* block device name completion */
3544 readline_set_completion_index(mon
->rs
, strlen(str
));
3545 while ((blk
= blk_next(blk
)) != NULL
) {
3546 name
= blk_name(blk
);
3547 if (str
[0] == '\0' ||
3548 !strncmp(name
, str
, strlen(str
))) {
3549 readline_add_completion(mon
->rs
, name
);
3555 if (!strcmp(cmd
->name
, "help|?")) {
3556 monitor_find_completion_by_table(mon
, cmd_table
,
3557 &args
[1], nb_args
- 1);
3566 static void monitor_find_completion(void *opaque
,
3567 const char *cmdline
)
3569 Monitor
*mon
= opaque
;
3570 char *args
[MAX_ARGS
];
3573 /* 1. parse the cmdline */
3574 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3578 /* if the line ends with a space, it means we want to complete the
3580 len
= strlen(cmdline
);
3581 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3582 if (nb_args
>= MAX_ARGS
) {
3585 args
[nb_args
++] = g_strdup("");
3588 /* 2. auto complete according to args */
3589 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
3592 free_cmdline_args(args
, nb_args
);
3595 static int monitor_can_read(void *opaque
)
3597 Monitor
*mon
= opaque
;
3599 return (mon
->suspend_cnt
== 0) ? 1 : 0;
3602 static bool invalid_qmp_mode(const Monitor
*mon
, const mon_cmd_t
*cmd
,
3605 bool is_cap
= cmd
->mhandler
.cmd_new
== qmp_capabilities
;
3607 if (is_cap
&& mon
->qmp
.in_command_mode
) {
3608 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3609 "Capabilities negotiation is already complete, command "
3610 "'%s' ignored", cmd
->name
);
3613 if (!is_cap
&& !mon
->qmp
.in_command_mode
) {
3614 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3615 "Expecting capabilities negotiation with "
3616 "'qmp_capabilities' before command '%s'", cmd
->name
);
3623 * Argument validation rules:
3625 * 1. The argument must exist in cmd_args qdict
3626 * 2. The argument type must be the expected one
3628 * Special case: If the argument doesn't exist in cmd_args and
3629 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
3630 * checking is skipped for it.
3632 static void check_client_args_type(const QDict
*client_args
,
3633 const QDict
*cmd_args
, int flags
,
3636 const QDictEntry
*ent
;
3638 for (ent
= qdict_first(client_args
); ent
;ent
= qdict_next(client_args
,ent
)){
3641 const QObject
*client_arg
= qdict_entry_value(ent
);
3642 const char *client_arg_name
= qdict_entry_key(ent
);
3644 obj
= qdict_get(cmd_args
, client_arg_name
);
3646 if (flags
& QMP_ACCEPT_UNKNOWNS
) {
3647 /* handler accepts unknowns */
3650 /* client arg doesn't exist */
3651 error_setg(errp
, QERR_INVALID_PARAMETER
, client_arg_name
);
3655 arg_type
= qobject_to_qstring(obj
);
3656 assert(arg_type
!= NULL
);
3658 /* check if argument's type is correct */
3659 switch (qstring_get_str(arg_type
)[0]) {
3663 if (qobject_type(client_arg
) != QTYPE_QSTRING
) {
3664 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3665 client_arg_name
, "string");
3673 if (qobject_type(client_arg
) != QTYPE_QINT
) {
3674 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3675 client_arg_name
, "int");
3680 if (qobject_type(client_arg
) != QTYPE_QINT
&&
3681 qobject_type(client_arg
) != QTYPE_QFLOAT
) {
3682 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3683 client_arg_name
, "number");
3689 if (qobject_type(client_arg
) != QTYPE_QBOOL
) {
3690 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
,
3691 client_arg_name
, "bool");
3696 assert(flags
& QMP_ACCEPT_UNKNOWNS
);
3699 /* Any QObject can be passed. */
3704 * These types are not supported by QMP and thus are not
3705 * handled here. Fall through.
3714 * - Check if the client has passed all mandatory args
3715 * - Set special flags for argument validation
3717 static void check_mandatory_args(const QDict
*cmd_args
,
3718 const QDict
*client_args
, int *flags
,
3721 const QDictEntry
*ent
;
3723 for (ent
= qdict_first(cmd_args
); ent
; ent
= qdict_next(cmd_args
, ent
)) {
3724 const char *cmd_arg_name
= qdict_entry_key(ent
);
3725 QString
*type
= qobject_to_qstring(qdict_entry_value(ent
));
3726 assert(type
!= NULL
);
3728 if (qstring_get_str(type
)[0] == 'O') {
3729 assert((*flags
& QMP_ACCEPT_UNKNOWNS
) == 0);
3730 *flags
|= QMP_ACCEPT_UNKNOWNS
;
3731 } else if (qstring_get_str(type
)[0] != '-' &&
3732 qstring_get_str(type
)[1] != '?' &&
3733 !qdict_haskey(client_args
, cmd_arg_name
)) {
3734 error_setg(errp
, QERR_MISSING_PARAMETER
, cmd_arg_name
);
3740 static QDict
*qdict_from_args_type(const char *args_type
)
3744 QString
*key
, *type
, *cur_qs
;
3746 assert(args_type
!= NULL
);
3748 qdict
= qdict_new();
3750 if (args_type
== NULL
|| args_type
[0] == '\0') {
3751 /* no args, empty qdict */
3755 key
= qstring_new();
3756 type
= qstring_new();
3761 switch (args_type
[i
]) {
3764 qdict_put(qdict
, qstring_get_str(key
), type
);
3766 if (args_type
[i
] == '\0') {
3769 type
= qstring_new(); /* qdict has ref */
3770 cur_qs
= key
= qstring_new();
3776 qstring_append_chr(cur_qs
, args_type
[i
]);
3786 * Client argument checking rules:
3788 * 1. Client must provide all mandatory arguments
3789 * 2. Each argument provided by the client must be expected
3790 * 3. Each argument provided by the client must have the type expected
3793 static void qmp_check_client_args(const mon_cmd_t
*cmd
, QDict
*client_args
,
3800 cmd_args
= qdict_from_args_type(cmd
->args_type
);
3803 check_mandatory_args(cmd_args
, client_args
, &flags
, &err
);
3808 check_client_args_type(client_args
, cmd_args
, flags
, &err
);
3811 error_propagate(errp
, err
);
3816 * Input object checking rules
3818 * 1. Input object must be a dict
3819 * 2. The "execute" key must exist
3820 * 3. The "execute" key must be a string
3821 * 4. If the "arguments" key exists, it must be a dict
3822 * 5. If the "id" key exists, it can be anything (ie. json-value)
3823 * 6. Any argument not listed above is considered invalid
3825 static QDict
*qmp_check_input_obj(QObject
*input_obj
, Error
**errp
)
3827 const QDictEntry
*ent
;
3828 int has_exec_key
= 0;
3831 if (qobject_type(input_obj
) != QTYPE_QDICT
) {
3832 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "object");
3836 input_dict
= qobject_to_qdict(input_obj
);
3838 for (ent
= qdict_first(input_dict
); ent
; ent
= qdict_next(input_dict
, ent
)){
3839 const char *arg_name
= qdict_entry_key(ent
);
3840 const QObject
*arg_obj
= qdict_entry_value(ent
);
3842 if (!strcmp(arg_name
, "execute")) {
3843 if (qobject_type(arg_obj
) != QTYPE_QSTRING
) {
3844 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3845 "execute", "string");
3849 } else if (!strcmp(arg_name
, "arguments")) {
3850 if (qobject_type(arg_obj
) != QTYPE_QDICT
) {
3851 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT_MEMBER
,
3852 "arguments", "object");
3855 } else if (!strcmp(arg_name
, "id")) {
3856 /* Any string is acceptable as "id", so nothing to check */
3858 error_setg(errp
, QERR_QMP_EXTRA_MEMBER
, arg_name
);
3863 if (!has_exec_key
) {
3864 error_setg(errp
, QERR_QMP_BAD_INPUT_OBJECT
, "execute");
3871 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
3873 Error
*local_err
= NULL
;
3874 QObject
*obj
, *data
;
3875 QDict
*input
, *args
;
3876 const mon_cmd_t
*cmd
;
3877 const char *cmd_name
;
3878 Monitor
*mon
= cur_mon
;
3880 args
= input
= NULL
;
3883 obj
= json_parser_parse(tokens
, NULL
);
3885 // FIXME: should be triggered in json_parser_parse()
3886 error_setg(&local_err
, QERR_JSON_PARSING
);
3890 input
= qmp_check_input_obj(obj
, &local_err
);
3892 qobject_decref(obj
);
3896 mon
->qmp
.id
= qdict_get(input
, "id");
3897 qobject_incref(mon
->qmp
.id
);
3899 cmd_name
= qdict_get_str(input
, "execute");
3900 trace_handle_qmp_command(mon
, cmd_name
);
3901 cmd
= qmp_find_cmd(cmd_name
);
3903 error_set(&local_err
, ERROR_CLASS_COMMAND_NOT_FOUND
,
3904 "The command %s has not been found", cmd_name
);
3907 if (invalid_qmp_mode(mon
, cmd
, &local_err
)) {
3911 obj
= qdict_get(input
, "arguments");
3915 args
= qobject_to_qdict(obj
);
3919 qmp_check_client_args(cmd
, args
, &local_err
);
3924 cmd
->mhandler
.cmd_new(args
, &data
, &local_err
);
3927 monitor_protocol_emitter(mon
, data
, local_err
);
3928 qobject_decref(data
);
3929 error_free(local_err
);
3934 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
3936 Monitor
*old_mon
= cur_mon
;
3940 json_message_parser_feed(&cur_mon
->qmp
.parser
, (const char *) buf
, size
);
3945 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3947 Monitor
*old_mon
= cur_mon
;
3953 for (i
= 0; i
< size
; i
++)
3954 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3956 if (size
== 0 || buf
[size
- 1] != 0)
3957 monitor_printf(cur_mon
, "corrupted command\n");
3959 handle_hmp_command(cur_mon
, (char *)buf
);
3965 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3966 void *readline_opaque
)
3968 Monitor
*mon
= opaque
;
3970 monitor_suspend(mon
);
3971 handle_hmp_command(mon
, cmdline
);
3972 monitor_resume(mon
);
3975 int monitor_suspend(Monitor
*mon
)
3983 void monitor_resume(Monitor
*mon
)
3987 if (--mon
->suspend_cnt
== 0)
3988 readline_show_prompt(mon
->rs
);
3991 static QObject
*get_qmp_greeting(void)
3993 QObject
*ver
= NULL
;
3995 qmp_marshal_query_version(NULL
, &ver
, NULL
);
3996 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver
);
3999 static void monitor_qmp_event(void *opaque
, int event
)
4002 Monitor
*mon
= opaque
;
4005 case CHR_EVENT_OPENED
:
4006 mon
->qmp
.in_command_mode
= false;
4007 data
= get_qmp_greeting();
4008 monitor_json_emitter(mon
, data
);
4009 qobject_decref(data
);
4012 case CHR_EVENT_CLOSED
:
4013 json_message_parser_destroy(&mon
->qmp
.parser
);
4014 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4016 monitor_fdsets_cleanup();
4021 static void monitor_event(void *opaque
, int event
)
4023 Monitor
*mon
= opaque
;
4026 case CHR_EVENT_MUX_IN
:
4027 qemu_mutex_lock(&mon
->out_lock
);
4029 qemu_mutex_unlock(&mon
->out_lock
);
4030 if (mon
->reset_seen
) {
4031 readline_restart(mon
->rs
);
4032 monitor_resume(mon
);
4035 mon
->suspend_cnt
= 0;
4039 case CHR_EVENT_MUX_OUT
:
4040 if (mon
->reset_seen
) {
4041 if (mon
->suspend_cnt
== 0) {
4042 monitor_printf(mon
, "\n");
4045 monitor_suspend(mon
);
4049 qemu_mutex_lock(&mon
->out_lock
);
4051 qemu_mutex_unlock(&mon
->out_lock
);
4054 case CHR_EVENT_OPENED
:
4055 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4056 "information\n", QEMU_VERSION
);
4057 if (!mon
->mux_out
) {
4058 readline_restart(mon
->rs
);
4059 readline_show_prompt(mon
->rs
);
4061 mon
->reset_seen
= 1;
4065 case CHR_EVENT_CLOSED
:
4067 monitor_fdsets_cleanup();
4073 compare_mon_cmd(const void *a
, const void *b
)
4075 return strcmp(((const mon_cmd_t
*)a
)->name
,
4076 ((const mon_cmd_t
*)b
)->name
);
4079 static void sortcmdlist(void)
4082 int elem_size
= sizeof(mon_cmd_t
);
4084 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4085 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4087 array_num
= sizeof(info_cmds
)/elem_size
-1;
4088 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4100 /* These functions just adapt the readline interface in a typesafe way. We
4101 * could cast function pointers but that discards compiler checks.
4103 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4104 const char *fmt
, ...)
4108 monitor_vprintf(opaque
, fmt
, ap
);
4112 static void monitor_readline_flush(void *opaque
)
4114 monitor_flush(opaque
);
4117 static void __attribute__((constructor
)) monitor_lock_init(void)
4119 qemu_mutex_init(&monitor_lock
);
4122 void monitor_init(CharDriverState
*chr
, int flags
)
4124 static int is_first_init
= 1;
4127 if (is_first_init
) {
4128 monitor_qapi_event_init();
4133 mon
= g_malloc(sizeof(*mon
));
4134 monitor_data_init(mon
);
4138 if (flags
& MONITOR_USE_READLINE
) {
4139 mon
->rs
= readline_init(monitor_readline_printf
,
4140 monitor_readline_flush
,
4142 monitor_find_completion
);
4143 monitor_read_command(mon
, 0);
4146 if (monitor_is_qmp(mon
)) {
4147 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_qmp_read
,
4148 monitor_qmp_event
, mon
);
4149 qemu_chr_fe_set_echo(chr
, true);
4150 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4152 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
4153 monitor_event
, mon
);
4156 qemu_mutex_lock(&monitor_lock
);
4157 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
4158 qemu_mutex_unlock(&monitor_lock
);
4161 static void bdrv_password_cb(void *opaque
, const char *password
,
4162 void *readline_opaque
)
4164 Monitor
*mon
= opaque
;
4165 BlockDriverState
*bs
= readline_opaque
;
4167 Error
*local_err
= NULL
;
4169 bdrv_add_key(bs
, password
, &local_err
);
4171 error_report_err(local_err
);
4174 if (mon
->password_completion_cb
)
4175 mon
->password_completion_cb(mon
->password_opaque
, ret
);
4177 monitor_read_command(mon
, 1);
4180 int monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
4181 BlockCompletionFunc
*completion_cb
,
4186 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
4187 bdrv_get_encrypted_filename(bs
));
4189 mon
->password_completion_cb
= completion_cb
;
4190 mon
->password_opaque
= opaque
;
4192 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
4194 if (err
&& completion_cb
)
4195 completion_cb(opaque
, err
);
4200 int monitor_read_block_device_key(Monitor
*mon
, const char *device
,
4201 BlockCompletionFunc
*completion_cb
,
4207 blk
= blk_by_name(device
);
4209 monitor_printf(mon
, "Device not found %s\n", device
);
4213 monitor_printf(mon
, "Device '%s' has no medium\n", device
);
4217 bdrv_add_key(blk_bs(blk
), NULL
, &err
);
4220 return monitor_read_bdrv_key_start(mon
, blk_bs(blk
), completion_cb
, opaque
);
4223 if (completion_cb
) {
4224 completion_cb(opaque
, 0);
4229 QemuOptsList qemu_mon_opts
= {
4231 .implied_opt_name
= "chardev",
4232 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4236 .type
= QEMU_OPT_STRING
,
4239 .type
= QEMU_OPT_STRING
,
4242 .type
= QEMU_OPT_BOOL
,
4245 .type
= QEMU_OPT_BOOL
,
4247 { /* end of list */ }
4252 void qmp_rtc_reset_reinjection(Error
**errp
)
4254 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4258 #ifndef TARGET_S390X
4259 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4261 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4266 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4268 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");