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
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
30 #include "monitor/qdev.h"
32 #include "hw/pci/pci.h"
33 #include "sysemu/watchdog.h"
34 #include "hw/loader.h"
35 #include "exec/gdbstub.h"
37 #include "net/slirp.h"
38 #include "chardev/char-fe.h"
39 #include "chardev/char-io.h"
40 #include "chardev/char-mux.h"
41 #include "ui/qemu-spice.h"
42 #include "sysemu/numa.h"
43 #include "monitor/monitor.h"
44 #include "qemu/config-file.h"
45 #include "qemu/ctype.h"
46 #include "qemu/readline.h"
47 #include "ui/console.h"
49 #include "sysemu/block-backend.h"
50 #include "audio/audio.h"
51 #include "disas/disas.h"
52 #include "sysemu/balloon.h"
53 #include "qemu/timer.h"
54 #include "sysemu/hw_accel.h"
55 #include "authz/list.h"
56 #include "qapi/util.h"
57 #include "sysemu/tcg.h"
58 #include "sysemu/tpm.h"
59 #include "qapi/qmp/qdict.h"
60 #include "qapi/qmp/qerror.h"
61 #include "qapi/qmp/qnum.h"
62 #include "qapi/qmp/qstring.h"
63 #include "qapi/qmp/qjson.h"
64 #include "qapi/qmp/json-parser.h"
65 #include "qapi/qmp/qlist.h"
66 #include "qom/object_interfaces.h"
67 #include "trace-root.h"
68 #include "trace/control.h"
69 #include "monitor/hmp-target.h"
70 #ifdef CONFIG_TRACE_SIMPLE
71 #include "trace/simple.h"
73 #include "exec/memory.h"
74 #include "exec/exec-all.h"
76 #include "qemu/option.h"
78 #include "qemu/thread.h"
79 #include "block/qapi.h"
80 #include "qapi/qapi-commands.h"
81 #include "qapi/qapi-emit-events.h"
82 #include "qapi/error.h"
83 #include "qapi/qmp-event.h"
84 #include "qapi/qapi-introspect.h"
85 #include "sysemu/qtest.h"
86 #include "sysemu/cpus.h"
87 #include "sysemu/iothread.h"
88 #include "qemu/cutils.h"
91 #if defined(TARGET_S390X)
92 #include "hw/s390x/storage-keys.h"
93 #include "hw/s390x/storage-attributes.h"
100 * 'B' block device name
101 * 's' string (accept optional quote)
102 * 'S' it just appends the rest of the string (accept optional quote)
103 * 'O' option string of the form NAME=VALUE,...
104 * parsed according to QemuOptsList given by its name
105 * Example: 'device:O' uses qemu_device_opts.
106 * Restriction: only lists with empty desc are supported
107 * TODO lift the restriction
109 * 'l' target long (32 or 64 bit)
110 * 'M' Non-negative target long (32 or 64 bit), in user mode the
111 * value is multiplied by 2^20 (think Mebibyte)
112 * 'o' octets (aka bytes)
113 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
114 * K, k suffix, which multiplies the value by 2^60 for suffixes E
115 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
116 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
118 * user mode accepts an optional ms, us, ns suffix,
119 * which divides the value by 1e3, 1e6, 1e9, respectively
120 * '/' optional gdb-like print format (like "/10x")
122 * '?' optional type (for all types, except '/')
123 * '.' other form of optional type (for 'i' and 'l')
125 * user mode accepts "on" or "off"
126 * '-' optional parameter (eg. '-f')
130 typedef struct mon_cmd_t
{
132 const char *args_type
;
135 const char *flags
; /* p=preconfig */
136 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
137 /* @sub_table is a list of 2nd level of commands. If it does not exist,
138 * cmd should be used. If it exists, sub_table[?].cmd should be
139 * used, and cmd of 1st level plays the role of help function.
141 struct mon_cmd_t
*sub_table
;
142 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
145 /* file descriptors passed via SCM_RIGHTS */
146 typedef struct mon_fd_t mon_fd_t
;
150 QLIST_ENTRY(mon_fd_t
) next
;
153 /* file descriptor associated with a file descriptor set */
154 typedef struct MonFdsetFd MonFdsetFd
;
159 QLIST_ENTRY(MonFdsetFd
) next
;
162 /* file descriptor set containing fds passed via SCM_RIGHTS */
163 typedef struct MonFdset MonFdset
;
166 QLIST_HEAD(, MonFdsetFd
) fds
;
167 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
168 QLIST_ENTRY(MonFdset
) next
;
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
176 typedef struct MonitorQAPIEventState
{
177 QAPIEvent event
; /* Throttling state for this event type and... */
178 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
179 QEMUTimer
*timer
; /* Timer for handling delayed events */
180 QDict
*qdict
; /* Delayed event (if any) */
181 } MonitorQAPIEventState
;
184 int64_t rate
; /* Minimum time (in ns) between two events */
185 } MonitorQAPIEventConf
;
191 int suspend_cnt
; /* Needs to be accessed atomically */
196 * State used only in the thread "owning" the monitor.
197 * If @use_io_thread, this is @mon_iothread.
198 * Else, it's the main thread.
199 * These members can be safely accessed without locks.
204 mon_cmd_t
*cmd_table
;
205 QTAILQ_ENTRY(Monitor
) entry
;
208 * The per-monitor lock. We can't access guest memory when holding
214 * Members that are protected by the per-monitor lock
216 QLIST_HEAD(, mon_fd_t
) fds
;
219 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
225 JSONMessageParser parser
;
227 * When a client connects, we're in capabilities negotiation mode.
228 * @commands is &qmp_cap_negotiation_commands then. When command
229 * qmp_capabilities succeeds, we go into command mode, and
230 * @command becomes &qmp_commands.
232 QmpCommandList
*commands
;
233 bool capab_offered
[QMP_CAPABILITY__MAX
]; /* capabilities offered */
234 bool capab
[QMP_CAPABILITY__MAX
]; /* offered and accepted */
236 * Protects qmp request/response queue.
237 * Take monitor_lock first when you need both.
239 QemuMutex qmp_queue_lock
;
240 /* Input queue that holds all the parsed QMP requests */
241 GQueue
*qmp_requests
;
244 /* Shared monitor I/O thread */
245 IOThread
*mon_iothread
;
247 /* Bottom half to dispatch the requests received from I/O thread */
248 QEMUBH
*qmp_dispatcher_bh
;
251 /* Owner of the request */
254 * Request object to be handled or Error to be reported
255 * (exactly one of them is non-null)
260 typedef struct QMPRequest QMPRequest
;
262 /* QMP checker flags */
263 #define QMP_ACCEPT_UNKNOWNS 1
265 /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
266 static QemuMutex monitor_lock
;
267 static GHashTable
*monitor_qapi_event_state
;
268 static QTAILQ_HEAD(, Monitor
) mon_list
;
269 static bool monitor_destroyed
;
271 /* Protects mon_fdsets */
272 static QemuMutex mon_fdsets_lock
;
273 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
275 static int mon_refcount
;
277 static mon_cmd_t mon_cmds
[];
278 static mon_cmd_t info_cmds
[];
280 QmpCommandList qmp_commands
, qmp_cap_negotiation_commands
;
282 __thread Monitor
*cur_mon
;
284 static void monitor_command_cb(void *opaque
, const char *cmdline
,
285 void *readline_opaque
);
288 * Is @mon a QMP monitor?
290 static inline bool monitor_is_qmp(const Monitor
*mon
)
292 return (mon
->flags
& MONITOR_USE_CONTROL
);
296 * Is @mon is using readline?
297 * Note: not all HMP monitors use readline, e.g., gdbserver has a
298 * non-interactive HMP monitor, so readline is not used there.
300 static inline bool monitor_uses_readline(const Monitor
*mon
)
302 return mon
->flags
& MONITOR_USE_READLINE
;
305 static inline bool monitor_is_hmp_non_interactive(const Monitor
*mon
)
307 return !monitor_is_qmp(mon
) && !monitor_uses_readline(mon
);
311 * Return the clock to use for recording an event's time.
312 * It's QEMU_CLOCK_REALTIME, except for qtests it's
313 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
314 * Beware: result is invalid before configure_accelerator().
316 static inline QEMUClockType
monitor_get_event_clock(void)
318 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL
: QEMU_CLOCK_REALTIME
;
322 * Is the current monitor, if any, a QMP monitor?
324 bool monitor_cur_is_qmp(void)
326 return cur_mon
&& monitor_is_qmp(cur_mon
);
329 void monitor_read_command(Monitor
*mon
, int show_prompt
)
334 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
336 readline_show_prompt(mon
->rs
);
339 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
343 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
344 /* prompt is printed on return from the command handler */
347 monitor_printf(mon
, "terminal does not support password prompting\n");
352 static void qmp_request_free(QMPRequest
*req
)
354 qobject_unref(req
->req
);
355 error_free(req
->err
);
359 /* Caller must hold mon->qmp.qmp_queue_lock */
360 static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP
*mon
)
362 while (!g_queue_is_empty(mon
->qmp_requests
)) {
363 qmp_request_free(g_queue_pop_head(mon
->qmp_requests
));
367 static void monitor_qmp_cleanup_queues(MonitorQMP
*mon
)
369 qemu_mutex_lock(&mon
->qmp_queue_lock
);
370 monitor_qmp_cleanup_req_queue_locked(mon
);
371 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
375 static void monitor_flush_locked(Monitor
*mon
);
377 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
380 Monitor
*mon
= opaque
;
382 qemu_mutex_lock(&mon
->mon_lock
);
384 monitor_flush_locked(mon
);
385 qemu_mutex_unlock(&mon
->mon_lock
);
389 /* Caller must hold mon->mon_lock */
390 static void monitor_flush_locked(Monitor
*mon
)
396 if (mon
->skip_flush
) {
400 buf
= qstring_get_str(mon
->outbuf
);
401 len
= qstring_get_length(mon
->outbuf
);
403 if (len
&& !mon
->mux_out
) {
404 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
405 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
406 /* all flushed or error */
407 qobject_unref(mon
->outbuf
);
408 mon
->outbuf
= qstring_new();
413 QString
*tmp
= qstring_from_str(buf
+ rc
);
414 qobject_unref(mon
->outbuf
);
417 if (mon
->out_watch
== 0) {
419 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
420 monitor_unblocked
, mon
);
425 void monitor_flush(Monitor
*mon
)
427 qemu_mutex_lock(&mon
->mon_lock
);
428 monitor_flush_locked(mon
);
429 qemu_mutex_unlock(&mon
->mon_lock
);
432 /* flush at every end of line */
433 static int monitor_puts(Monitor
*mon
, const char *str
)
438 qemu_mutex_lock(&mon
->mon_lock
);
439 for (i
= 0; str
[i
]; i
++) {
442 qstring_append_chr(mon
->outbuf
, '\r');
444 qstring_append_chr(mon
->outbuf
, c
);
446 monitor_flush_locked(mon
);
449 qemu_mutex_unlock(&mon
->mon_lock
);
454 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
462 if (monitor_is_qmp(mon
)) {
466 buf
= g_strdup_vprintf(fmt
, ap
);
467 n
= monitor_puts(mon
, buf
);
472 int monitor_printf(Monitor
*mon
, const char *fmt
, ...)
478 ret
= monitor_vprintf(mon
, fmt
, ap
);
483 static void qmp_send_response(MonitorQMP
*mon
, const QDict
*rsp
)
485 const QObject
*data
= QOBJECT(rsp
);
488 json
= mon
->common
.flags
& MONITOR_USE_PRETTY
?
489 qobject_to_json_pretty(data
) : qobject_to_json(data
);
490 assert(json
!= NULL
);
492 qstring_append_chr(json
, '\n');
493 monitor_puts(&mon
->common
, qstring_get_str(json
));
498 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
499 /* Limit guest-triggerable events to 1 per second */
500 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
501 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
502 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
503 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
504 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
505 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
509 * Broadcast an event to all monitors.
510 * @qdict is the event object. Its member "event" must match @event.
511 * Caller must hold monitor_lock.
513 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
518 trace_monitor_protocol_event_emit(event
, qdict
);
519 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
520 if (!monitor_is_qmp(mon
)) {
524 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
525 if (qmp_mon
->commands
!= &qmp_cap_negotiation_commands
) {
526 qmp_send_response(qmp_mon
, qdict
);
531 static void monitor_qapi_event_handler(void *opaque
);
534 * Queue a new event for emission to Monitor instances,
535 * applying any rate limiting if required.
538 monitor_qapi_event_queue_no_reenter(QAPIEvent event
, QDict
*qdict
)
540 MonitorQAPIEventConf
*evconf
;
541 MonitorQAPIEventState
*evstate
;
543 assert(event
< QAPI_EVENT__MAX
);
544 evconf
= &monitor_qapi_event_conf
[event
];
545 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
547 qemu_mutex_lock(&monitor_lock
);
550 /* Unthrottled event */
551 monitor_qapi_event_emit(event
, qdict
);
553 QDict
*data
= qobject_to(QDict
, qdict_get(qdict
, "data"));
554 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
556 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
557 assert(!evstate
|| timer_pending(evstate
->timer
));
561 * Timer is pending for (at least) evconf->rate ns after
562 * last send. Store event for sending when timer fires,
563 * replacing a prior stored event if any.
565 qobject_unref(evstate
->qdict
);
566 evstate
->qdict
= qobject_ref(qdict
);
569 * Last send was (at least) evconf->rate ns ago.
570 * Send immediately, and arm the timer to call
571 * monitor_qapi_event_handler() in evconf->rate ns. Any
572 * events arriving before then will be delayed until then.
574 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
576 monitor_qapi_event_emit(event
, qdict
);
578 evstate
= g_new(MonitorQAPIEventState
, 1);
579 evstate
->event
= event
;
580 evstate
->data
= qobject_ref(data
);
581 evstate
->qdict
= NULL
;
582 evstate
->timer
= timer_new_ns(monitor_get_event_clock(),
583 monitor_qapi_event_handler
,
585 g_hash_table_add(monitor_qapi_event_state
, evstate
);
586 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
590 qemu_mutex_unlock(&monitor_lock
);
593 void qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
596 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
597 * would deadlock on monitor_lock. Work around by queueing
598 * events in thread-local storage.
599 * TODO: remove this, make it re-enter safe.
601 typedef struct MonitorQapiEvent
{
604 QSIMPLEQ_ENTRY(MonitorQapiEvent
) entry
;
606 static __thread
QSIMPLEQ_HEAD(, MonitorQapiEvent
) event_queue
;
607 static __thread
bool reentered
;
608 MonitorQapiEvent
*ev
;
611 QSIMPLEQ_INIT(&event_queue
);
614 ev
= g_new(MonitorQapiEvent
, 1);
615 ev
->qdict
= qobject_ref(qdict
);
617 QSIMPLEQ_INSERT_TAIL(&event_queue
, ev
, entry
);
624 while ((ev
= QSIMPLEQ_FIRST(&event_queue
)) != NULL
) {
625 QSIMPLEQ_REMOVE_HEAD(&event_queue
, entry
);
626 monitor_qapi_event_queue_no_reenter(ev
->event
, ev
->qdict
);
627 qobject_unref(ev
->qdict
);
635 * This function runs evconf->rate ns after sending a throttled
637 * If another event has since been stored, send it.
639 static void monitor_qapi_event_handler(void *opaque
)
641 MonitorQAPIEventState
*evstate
= opaque
;
642 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
644 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
645 qemu_mutex_lock(&monitor_lock
);
647 if (evstate
->qdict
) {
648 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
650 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
651 qobject_unref(evstate
->qdict
);
652 evstate
->qdict
= NULL
;
653 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
655 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
656 qobject_unref(evstate
->data
);
657 timer_free(evstate
->timer
);
661 qemu_mutex_unlock(&monitor_lock
);
664 static unsigned int qapi_event_throttle_hash(const void *key
)
666 const MonitorQAPIEventState
*evstate
= key
;
667 unsigned int hash
= evstate
->event
* 255;
669 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
670 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
673 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
674 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
680 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
682 const MonitorQAPIEventState
*eva
= a
;
683 const MonitorQAPIEventState
*evb
= b
;
685 if (eva
->event
!= evb
->event
) {
689 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
690 return !strcmp(qdict_get_str(eva
->data
, "id"),
691 qdict_get_str(evb
->data
, "id"));
694 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
695 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
696 qdict_get_str(evb
->data
, "node-name"));
702 static void monitor_qapi_event_init(void)
704 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
705 qapi_event_throttle_equal
);
708 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
710 static void monitor_iothread_init(void);
712 static void monitor_data_init(Monitor
*mon
, int flags
, bool skip_flush
,
715 if (use_io_thread
&& !mon_iothread
) {
716 monitor_iothread_init();
718 qemu_mutex_init(&mon
->mon_lock
);
719 mon
->outbuf
= qstring_new();
720 /* Use *mon_cmds by default. */
721 mon
->cmd_table
= mon_cmds
;
722 mon
->skip_flush
= skip_flush
;
723 mon
->use_io_thread
= use_io_thread
;
727 static void monitor_data_destroy_qmp(MonitorQMP
*mon
)
729 json_message_parser_destroy(&mon
->parser
);
730 qemu_mutex_destroy(&mon
->qmp_queue_lock
);
731 monitor_qmp_cleanup_req_queue_locked(mon
);
732 g_queue_free(mon
->qmp_requests
);
735 static void monitor_data_destroy(Monitor
*mon
)
737 g_free(mon
->mon_cpu_path
);
738 qemu_chr_fe_deinit(&mon
->chr
, false);
739 if (monitor_is_qmp(mon
)) {
740 monitor_data_destroy_qmp(container_of(mon
, MonitorQMP
, common
));
742 readline_free(mon
->rs
);
743 qobject_unref(mon
->outbuf
);
744 qemu_mutex_destroy(&mon
->mon_lock
);
747 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
748 int64_t cpu_index
, Error
**errp
)
754 monitor_data_init(&hmp
, 0, true, false);
760 int ret
= monitor_set_cpu(cpu_index
);
763 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
769 handle_hmp_command(&hmp
, command_line
);
772 qemu_mutex_lock(&hmp
.mon_lock
);
773 if (qstring_get_length(hmp
.outbuf
) > 0) {
774 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
776 output
= g_strdup("");
778 qemu_mutex_unlock(&hmp
.mon_lock
);
781 monitor_data_destroy(&hmp
);
785 static int compare_cmd(const char *name
, const char *list
)
787 const char *p
, *pstart
;
793 p
= qemu_strchrnul(p
, '|');
794 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
803 static int get_str(char *buf
, int buf_size
, const char **pp
)
811 while (qemu_isspace(*p
)) {
822 while (*p
!= '\0' && *p
!= '\"') {
838 printf("unsupported escape code: '\\%c'\n", c
);
841 if ((q
- buf
) < buf_size
- 1) {
845 if ((q
- buf
) < buf_size
- 1) {
852 printf("unterminated string\n");
857 while (*p
!= '\0' && !qemu_isspace(*p
)) {
858 if ((q
- buf
) < buf_size
- 1) {
871 static void free_cmdline_args(char **args
, int nb_args
)
875 assert(nb_args
<= MAX_ARGS
);
877 for (i
= 0; i
< nb_args
; i
++) {
884 * Parse the command line to get valid args.
885 * @cmdline: command line to be parsed.
886 * @pnb_args: location to store the number of args, must NOT be NULL.
887 * @args: location to store the args, which should be freed by caller, must
890 * Returns 0 on success, negative on failure.
892 * NOTE: this parser is an approximate form of the real command parser. Number
893 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
894 * return with failure.
896 static int parse_cmdline(const char *cmdline
,
897 int *pnb_args
, char **args
)
906 while (qemu_isspace(*p
)) {
912 if (nb_args
>= MAX_ARGS
) {
915 ret
= get_str(buf
, sizeof(buf
), &p
);
919 args
[nb_args
] = g_strdup(buf
);
926 free_cmdline_args(args
, nb_args
);
931 * Can command @cmd be executed in preconfig state?
933 static bool cmd_can_preconfig(const mon_cmd_t
*cmd
)
939 return strchr(cmd
->flags
, 'p');
942 static void help_cmd_dump_one(Monitor
*mon
,
943 const mon_cmd_t
*cmd
,
949 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
953 for (i
= 0; i
< prefix_args_nb
; i
++) {
954 monitor_printf(mon
, "%s ", prefix_args
[i
]);
956 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
959 /* @args[@arg_index] is the valid command need to find in @cmds */
960 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
961 char **args
, int nb_args
, int arg_index
)
963 const mon_cmd_t
*cmd
;
966 /* No valid arg need to compare with, dump all in *cmds */
967 if (arg_index
>= nb_args
) {
968 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
969 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
974 /* Find one entry to dump */
975 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
976 if (compare_cmd(args
[arg_index
], cmd
->name
) &&
977 ((!runstate_check(RUN_STATE_PRECONFIG
) ||
978 cmd_can_preconfig(cmd
)))) {
979 if (cmd
->sub_table
) {
980 /* continue with next arg */
981 help_cmd_dump(mon
, cmd
->sub_table
,
982 args
, nb_args
, arg_index
+ 1);
984 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
990 /* Command not found */
991 monitor_printf(mon
, "unknown command: '");
992 for (i
= 0; i
<= arg_index
; i
++) {
993 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
997 static void help_cmd(Monitor
*mon
, const char *name
)
999 char *args
[MAX_ARGS
];
1002 /* 1. parse user input */
1004 /* special case for log, directly dump and return */
1005 if (!strcmp(name
, "log")) {
1006 const QEMULogItem
*item
;
1007 monitor_printf(mon
, "Log items (comma separated):\n");
1008 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
1009 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
1010 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
1015 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
1020 /* 2. dump the contents according to parsed args */
1021 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
1023 free_cmdline_args(args
, nb_args
);
1026 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
1028 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
1031 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
1033 const char *tp_name
= qdict_get_str(qdict
, "name");
1034 bool new_state
= qdict_get_bool(qdict
, "option");
1035 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1036 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1037 Error
*local_err
= NULL
;
1040 monitor_printf(mon
, "argument vcpu must be positive");
1044 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
1046 error_report_err(local_err
);
1050 #ifdef CONFIG_TRACE_SIMPLE
1051 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
1053 const char *op
= qdict_get_try_str(qdict
, "op");
1054 const char *arg
= qdict_get_try_str(qdict
, "arg");
1057 st_print_trace_file_status();
1058 } else if (!strcmp(op
, "on")) {
1059 st_set_trace_file_enabled(true);
1060 } else if (!strcmp(op
, "off")) {
1061 st_set_trace_file_enabled(false);
1062 } else if (!strcmp(op
, "flush")) {
1063 st_flush_trace_buffer();
1064 } else if (!strcmp(op
, "set")) {
1066 st_set_trace_file(arg
);
1069 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
1070 help_cmd(mon
, "trace-file");
1075 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
1077 help_cmd(mon
, "info");
1080 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
1082 CommandInfoList
*info
, **list
= opaque
;
1084 if (!cmd
->enabled
) {
1088 info
= g_malloc0(sizeof(*info
));
1089 info
->value
= g_malloc0(sizeof(*info
->value
));
1090 info
->value
->name
= g_strdup(cmd
->name
);
1095 CommandInfoList
*qmp_query_commands(Error
**errp
)
1097 CommandInfoList
*list
= NULL
;
1100 assert(monitor_is_qmp(cur_mon
));
1101 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1103 qmp_for_each_command(mon
->commands
, query_commands_cb
, &list
);
1108 EventInfoList
*qmp_query_events(Error
**errp
)
1111 * TODO This deprecated command is the only user of
1112 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
1113 * they should go, too.
1115 EventInfoList
*info
, *ev_list
= NULL
;
1118 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
1119 const char *event_name
= QAPIEvent_str(e
);
1120 assert(event_name
!= NULL
);
1121 info
= g_malloc0(sizeof(*info
));
1122 info
->value
= g_malloc0(sizeof(*info
->value
));
1123 info
->value
->name
= g_strdup(event_name
);
1125 info
->next
= ev_list
;
1133 * Minor hack: generated marshalling suppressed for this command
1134 * ('gen': false in the schema) so we can parse the JSON string
1135 * directly into QObject instead of first parsing it with
1136 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1137 * to QObject with generated output marshallers, every time. Instead,
1138 * we do it in test-qobject-input-visitor.c, just to make sure
1139 * qapi-gen.py's output actually conforms to the schema.
1141 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
1144 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
1147 static void monitor_init_qmp_commands(void)
1150 * Two command lists:
1151 * - qmp_commands contains all QMP commands
1152 * - qmp_cap_negotiation_commands contains just
1153 * "qmp_capabilities", to enforce capability negotiation
1156 qmp_init_marshal(&qmp_commands
);
1158 qmp_register_command(&qmp_commands
, "query-qmp-schema",
1159 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
1160 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
1162 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
1165 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
1166 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
1167 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
1170 static bool qmp_oob_enabled(MonitorQMP
*mon
)
1172 return mon
->capab
[QMP_CAPABILITY_OOB
];
1175 static void monitor_qmp_caps_reset(MonitorQMP
*mon
)
1177 memset(mon
->capab_offered
, 0, sizeof(mon
->capab_offered
));
1178 memset(mon
->capab
, 0, sizeof(mon
->capab
));
1179 mon
->capab_offered
[QMP_CAPABILITY_OOB
] = mon
->common
.use_io_thread
;
1183 * Accept QMP capabilities in @list for @mon.
1184 * On success, set mon->qmp.capab[], and return true.
1185 * On error, set @errp, and return false.
1187 static bool qmp_caps_accept(MonitorQMP
*mon
, QMPCapabilityList
*list
,
1190 GString
*unavailable
= NULL
;
1191 bool capab
[QMP_CAPABILITY__MAX
];
1193 memset(capab
, 0, sizeof(capab
));
1195 for (; list
; list
= list
->next
) {
1196 if (!mon
->capab_offered
[list
->value
]) {
1198 unavailable
= g_string_new(QMPCapability_str(list
->value
));
1200 g_string_append_printf(unavailable
, ", %s",
1201 QMPCapability_str(list
->value
));
1204 capab
[list
->value
] = true;
1208 error_setg(errp
, "Capability %s not available", unavailable
->str
);
1209 g_string_free(unavailable
, true);
1213 memcpy(mon
->capab
, capab
, sizeof(capab
));
1217 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
1222 assert(monitor_is_qmp(cur_mon
));
1223 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1225 if (mon
->commands
== &qmp_commands
) {
1226 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
1227 "Capabilities negotiation is already complete, command "
1232 if (!qmp_caps_accept(mon
, enable
, errp
)) {
1236 mon
->commands
= &qmp_commands
;
1239 /* Set the current CPU defined by the user. Callers must hold BQL. */
1240 int monitor_set_cpu(int cpu_index
)
1244 cpu
= qemu_get_cpu(cpu_index
);
1248 g_free(cur_mon
->mon_cpu_path
);
1249 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
1253 /* Callers must hold BQL. */
1254 static CPUState
*mon_get_cpu_sync(bool synchronize
)
1258 if (cur_mon
->mon_cpu_path
) {
1259 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
1262 g_free(cur_mon
->mon_cpu_path
);
1263 cur_mon
->mon_cpu_path
= NULL
;
1266 if (!cur_mon
->mon_cpu_path
) {
1270 monitor_set_cpu(first_cpu
->cpu_index
);
1274 cpu_synchronize_state(cpu
);
1279 CPUState
*mon_get_cpu(void)
1281 return mon_get_cpu_sync(true);
1284 CPUArchState
*mon_get_cpu_env(void)
1286 CPUState
*cs
= mon_get_cpu();
1288 return cs
? cs
->env_ptr
: NULL
;
1291 int monitor_get_cpu_index(void)
1293 CPUState
*cs
= mon_get_cpu_sync(false);
1295 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
1298 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1300 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
1305 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
1306 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1312 monitor_printf(mon
, "No CPU available\n");
1316 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1321 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1323 if (!tcg_enabled()) {
1324 error_report("JIT information is only available with accel=tcg");
1332 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1334 dump_opcount_info();
1338 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
1340 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
1341 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
1342 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
1343 enum QSPSortBy sort_by
;
1345 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
1346 qsp_report(max
, sort_by
, coalesce
);
1349 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1358 str
= readline_get_history(mon
->rs
, i
);
1361 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1366 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1368 CPUState
*cs
= mon_get_cpu();
1371 monitor_printf(mon
, "No CPU available\n");
1374 cpu_dump_statistics(cs
, 0);
1377 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1379 const char *name
= qdict_get_try_str(qdict
, "name");
1380 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1381 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1382 TraceEventInfoList
*events
;
1383 TraceEventInfoList
*elem
;
1384 Error
*local_err
= NULL
;
1390 monitor_printf(mon
, "argument vcpu must be positive");
1394 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1396 error_report_err(local_err
);
1400 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1401 monitor_printf(mon
, "%s : state %u\n",
1403 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1405 qapi_free_TraceEventInfoList(events
);
1408 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1409 bool has_port
, int64_t port
,
1410 bool has_tls_port
, int64_t tls_port
,
1411 bool has_cert_subject
, const char *cert_subject
,
1414 if (strcmp(protocol
, "spice") == 0) {
1415 if (!qemu_using_spice(errp
)) {
1419 if (!has_port
&& !has_tls_port
) {
1420 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1424 if (qemu_spice_migrate_info(hostname
,
1425 has_port
? port
: -1,
1426 has_tls_port
? tls_port
: -1,
1428 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1434 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1437 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1441 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1443 error_report_err(err
);
1447 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1450 const char *items
= qdict_get_str(qdict
, "items");
1452 if (!strcmp(items
, "none")) {
1455 mask
= qemu_str_to_log_mask(items
);
1457 help_cmd(mon
, "log");
1464 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1466 const char *option
= qdict_get_try_str(qdict
, "option");
1467 if (!option
|| !strcmp(option
, "on")) {
1469 } else if (!strcmp(option
, "off")) {
1472 monitor_printf(mon
, "unexpected option %s\n", option
);
1476 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1478 const char *device
= qdict_get_try_str(qdict
, "device");
1480 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1481 if (gdbserver_start(device
) < 0) {
1482 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1484 } else if (strcmp(device
, "none") == 0) {
1485 monitor_printf(mon
, "Disabled gdbserver\n");
1487 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1492 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1494 const char *action
= qdict_get_str(qdict
, "action");
1495 if (select_watchdog_action(action
) == -1) {
1496 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1500 static void monitor_printc(Monitor
*mon
, int c
)
1502 monitor_printf(mon
, "'");
1505 monitor_printf(mon
, "\\'");
1508 monitor_printf(mon
, "\\\\");
1511 monitor_printf(mon
, "\\n");
1514 monitor_printf(mon
, "\\r");
1517 if (c
>= 32 && c
<= 126) {
1518 monitor_printf(mon
, "%c", c
);
1520 monitor_printf(mon
, "\\x%02x", c
);
1524 monitor_printf(mon
, "'");
1527 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1528 hwaddr addr
, int is_physical
)
1530 int l
, line_size
, i
, max_digits
, len
;
1533 CPUState
*cs
= mon_get_cpu();
1535 if (!cs
&& (format
== 'i' || !is_physical
)) {
1536 monitor_printf(mon
, "Can not dump without CPU\n");
1540 if (format
== 'i') {
1541 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
1545 len
= wsize
* count
;
1554 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
1558 max_digits
= (wsize
* 8) / 4;
1562 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
1571 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1573 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1578 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
1579 MemTxResult r
= address_space_read(as
, addr
,
1580 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
1581 if (r
!= MEMTX_OK
) {
1582 monitor_printf(mon
, " Cannot access memory\n");
1586 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
1587 monitor_printf(mon
, " Cannot access memory\n");
1596 v
= ldub_p(buf
+ i
);
1599 v
= lduw_p(buf
+ i
);
1602 v
= (uint32_t)ldl_p(buf
+ i
);
1608 monitor_printf(mon
, " ");
1611 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1614 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1617 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1620 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1623 monitor_printc(mon
, v
);
1628 monitor_printf(mon
, "\n");
1634 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1636 int count
= qdict_get_int(qdict
, "count");
1637 int format
= qdict_get_int(qdict
, "format");
1638 int size
= qdict_get_int(qdict
, "size");
1639 target_long addr
= qdict_get_int(qdict
, "addr");
1641 memory_dump(mon
, count
, format
, size
, addr
, 0);
1644 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1646 int count
= qdict_get_int(qdict
, "count");
1647 int format
= qdict_get_int(qdict
, "format");
1648 int size
= qdict_get_int(qdict
, "size");
1649 hwaddr addr
= qdict_get_int(qdict
, "addr");
1651 memory_dump(mon
, count
, format
, size
, addr
, 1);
1654 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
1656 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
1660 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
1664 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
1665 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
1666 memory_region_unref(mrs
.mr
);
1671 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
1674 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
1676 hwaddr addr
= qdict_get_int(qdict
, "addr");
1677 Error
*local_err
= NULL
;
1678 MemoryRegion
*mr
= NULL
;
1681 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1683 error_report_err(local_err
);
1687 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
1689 addr
, mr
->name
, ptr
);
1691 memory_region_unref(mr
);
1694 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
1696 target_ulong addr
= qdict_get_int(qdict
, "addr");
1698 CPUState
*cs
= mon_get_cpu();
1702 monitor_printf(mon
, "No cpu\n");
1706 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
1708 monitor_printf(mon
, "Unmapped\n");
1710 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
1711 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
1716 static uint64_t vtop(void *ptr
, Error
**errp
)
1720 uintptr_t addr
= (uintptr_t) ptr
;
1721 uintptr_t pagesize
= getpagesize();
1722 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
1725 fd
= open("/proc/self/pagemap", O_RDONLY
);
1727 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
1731 /* Force copy-on-write if necessary. */
1732 atomic_add((uint8_t *)ptr
, 0);
1734 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
1735 error_setg_errno(errp
, errno
, "Cannot read pagemap");
1738 if ((pinfo
& (1ull << 63)) == 0) {
1739 error_setg(errp
, "Page not present");
1742 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
1749 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
1751 hwaddr addr
= qdict_get_int(qdict
, "addr");
1752 Error
*local_err
= NULL
;
1753 MemoryRegion
*mr
= NULL
;
1757 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1759 error_report_err(local_err
);
1763 physaddr
= vtop(ptr
, &local_err
);
1765 error_report_err(local_err
);
1767 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
1768 " (%s) is 0x%" PRIx64
"\n",
1769 addr
, mr
->name
, (uint64_t) physaddr
);
1772 memory_region_unref(mr
);
1776 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1778 int format
= qdict_get_int(qdict
, "format");
1779 hwaddr val
= qdict_get_int(qdict
, "val");
1783 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1786 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1789 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1793 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1796 monitor_printc(mon
, val
);
1799 monitor_printf(mon
, "\n");
1802 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1806 uint32_t start
= qdict_get_int(qdict
, "start");
1807 uint32_t size
= qdict_get_int(qdict
, "size");
1810 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1811 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1812 MEMTXATTRS_UNSPECIFIED
, NULL
);
1813 /* BSD sum algorithm ('sum' Unix command) */
1814 sum
= (sum
>> 1) | (sum
<< 15);
1817 monitor_printf(mon
, "%05d\n", sum
);
1820 static int mouse_button_state
;
1822 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1824 int dx
, dy
, dz
, button
;
1825 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1826 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1827 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1829 dx
= strtol(dx_str
, NULL
, 0);
1830 dy
= strtol(dy_str
, NULL
, 0);
1831 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1832 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1835 dz
= strtol(dz_str
, NULL
, 0);
1837 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1838 qemu_input_queue_btn(NULL
, button
, true);
1839 qemu_input_event_sync();
1840 qemu_input_queue_btn(NULL
, button
, false);
1843 qemu_input_event_sync();
1846 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1848 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1849 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1850 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1851 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1853 int button_state
= qdict_get_int(qdict
, "button_state");
1855 if (mouse_button_state
== button_state
) {
1858 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1859 qemu_input_event_sync();
1860 mouse_button_state
= button_state
;
1863 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1865 int size
= qdict_get_int(qdict
, "size");
1866 int addr
= qdict_get_int(qdict
, "addr");
1867 int has_index
= qdict_haskey(qdict
, "index");
1872 int index
= qdict_get_int(qdict
, "index");
1873 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1881 val
= cpu_inb(addr
);
1885 val
= cpu_inw(addr
);
1889 val
= cpu_inl(addr
);
1893 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1894 suffix
, addr
, size
* 2, val
);
1897 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1899 int size
= qdict_get_int(qdict
, "size");
1900 int addr
= qdict_get_int(qdict
, "addr");
1901 int val
= qdict_get_int(qdict
, "val");
1903 addr
&= IOPORTS_MASK
;
1908 cpu_outb(addr
, val
);
1911 cpu_outw(addr
, val
);
1914 cpu_outl(addr
, val
);
1919 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1921 Error
*local_err
= NULL
;
1922 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1924 qemu_boot_set(bootdevice
, &local_err
);
1926 error_report_err(local_err
);
1928 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1932 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1934 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1935 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1936 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1938 mtree_info(flatview
, dispatch_tree
, owner
);
1941 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1944 NumaNodeMem
*node_mem
;
1945 CpuInfoList
*cpu_list
, *cpu
;
1947 cpu_list
= qmp_query_cpus(&error_abort
);
1948 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
1950 query_numa_node_mem(node_mem
);
1951 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1952 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1953 monitor_printf(mon
, "node %d cpus:", i
);
1954 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
1955 if (cpu
->value
->has_props
&& cpu
->value
->props
->has_node_id
&&
1956 cpu
->value
->props
->node_id
== i
) {
1957 monitor_printf(mon
, " %" PRIi64
, cpu
->value
->CPU
);
1960 monitor_printf(mon
, "\n");
1961 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1962 node_mem
[i
].node_mem
>> 20);
1963 monitor_printf(mon
, "node %d plugged: %" PRId64
" MB\n", i
,
1964 node_mem
[i
].node_plugged_mem
>> 20);
1966 qapi_free_CpuInfoList(cpu_list
);
1970 #ifdef CONFIG_PROFILER
1974 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1976 static int64_t last_cpu_exec_time
;
1977 int64_t cpu_exec_time
;
1980 cpu_exec_time
= tcg_cpu_exec_time();
1981 delta
= cpu_exec_time
- last_cpu_exec_time
;
1983 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1984 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1985 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1986 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1987 last_cpu_exec_time
= cpu_exec_time
;
1991 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1993 monitor_printf(mon
, "Internal profiler not compiled\n");
1997 /* Capture support */
1998 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2000 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
2005 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2006 monitor_printf(mon
, "[%d]: ", i
);
2007 s
->ops
.info (s
->opaque
);
2011 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
2014 int n
= qdict_get_int(qdict
, "n");
2017 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2019 s
->ops
.destroy (s
->opaque
);
2020 QLIST_REMOVE (s
, entries
);
2027 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
2029 const char *path
= qdict_get_str(qdict
, "path");
2030 int has_freq
= qdict_haskey(qdict
, "freq");
2031 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2032 int has_bits
= qdict_haskey(qdict
, "bits");
2033 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2034 int has_channels
= qdict_haskey(qdict
, "nchannels");
2035 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2038 s
= g_malloc0 (sizeof (*s
));
2040 freq
= has_freq
? freq
: 44100;
2041 bits
= has_bits
? bits
: 16;
2042 nchannels
= has_channels
? nchannels
: 2;
2044 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2045 monitor_printf(mon
, "Failed to add wave capture\n");
2049 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2052 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
2057 container
= object_get_objects_root();
2058 obj
= object_resolve_path_component(container
, name
);
2060 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2064 return QAUTHZ_LIST(obj
);
2067 static bool warn_acl
;
2068 static void hmp_warn_acl(void)
2073 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
2074 "commands are deprecated with no replacement. Authorization "
2075 "for VNC should be performed using the pluggable QAuthZ "
2080 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
2082 const char *aclname
= qdict_get_str(qdict
, "aclname");
2083 QAuthZList
*auth
= find_auth(mon
, aclname
);
2084 QAuthZListRuleList
*rules
;
2093 monitor_printf(mon
, "policy: %s\n",
2094 QAuthZListPolicy_str(auth
->policy
));
2096 rules
= auth
->rules
;
2098 QAuthZListRule
*rule
= rules
->value
;
2100 monitor_printf(mon
, "%zu: %s %s\n", i
,
2101 QAuthZListPolicy_str(rule
->policy
),
2103 rules
= rules
->next
;
2107 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2109 const char *aclname
= qdict_get_str(qdict
, "aclname");
2110 QAuthZList
*auth
= find_auth(mon
, aclname
);
2118 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
2119 qapi_free_QAuthZListRuleList(auth
->rules
);
2121 monitor_printf(mon
, "acl: removed all rules\n");
2124 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2126 const char *aclname
= qdict_get_str(qdict
, "aclname");
2127 const char *policy
= qdict_get_str(qdict
, "policy");
2128 QAuthZList
*auth
= find_auth(mon
, aclname
);
2138 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
2140 QAUTHZ_LIST_POLICY_DENY
,
2144 monitor_printf(mon
, "acl: unknown policy '%s', "
2145 "expected 'deny' or 'allow'\n", policy
);
2148 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
2149 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2151 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2156 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
2158 if (strchr(match
, '*')) {
2159 return QAUTHZ_LIST_FORMAT_GLOB
;
2161 return QAUTHZ_LIST_FORMAT_EXACT
;
2165 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2167 const char *aclname
= qdict_get_str(qdict
, "aclname");
2168 const char *match
= qdict_get_str(qdict
, "match");
2169 const char *policystr
= qdict_get_str(qdict
, "policy");
2170 int has_index
= qdict_haskey(qdict
, "index");
2171 int index
= qdict_get_try_int(qdict
, "index", -1);
2172 QAuthZList
*auth
= find_auth(mon
, aclname
);
2174 QAuthZListPolicy policy
;
2175 QAuthZListFormat format
;
2184 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
2186 QAUTHZ_LIST_POLICY_DENY
,
2190 monitor_printf(mon
, "acl: unknown policy '%s', "
2191 "expected 'deny' or 'allow'\n", policystr
);
2195 format
= hmp_acl_get_format(match
);
2197 if (has_index
&& index
== 0) {
2198 monitor_printf(mon
, "acl: unable to add acl entry\n");
2203 i
= qauthz_list_insert_rule(auth
, match
, policy
,
2204 format
, index
- 1, &err
);
2206 i
= qauthz_list_append_rule(auth
, match
, policy
,
2210 monitor_printf(mon
, "acl: unable to add rule: %s",
2211 error_get_pretty(err
));
2214 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
2218 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2220 const char *aclname
= qdict_get_str(qdict
, "aclname");
2221 const char *match
= qdict_get_str(qdict
, "match");
2222 QAuthZList
*auth
= find_auth(mon
, aclname
);
2231 i
= qauthz_list_delete_rule(auth
, match
);
2233 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
2235 monitor_printf(mon
, "acl: no matching acl entry\n");
2239 void qmp_getfd(const char *fdname
, Error
**errp
)
2244 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
2246 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2250 if (qemu_isdigit(fdname
[0])) {
2252 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2253 "a name not starting with a digit");
2257 qemu_mutex_lock(&cur_mon
->mon_lock
);
2258 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2259 if (strcmp(monfd
->name
, fdname
) != 0) {
2265 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2266 /* Make sure close() is outside critical section */
2271 monfd
= g_malloc0(sizeof(mon_fd_t
));
2272 monfd
->name
= g_strdup(fdname
);
2275 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2276 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2279 void qmp_closefd(const char *fdname
, Error
**errp
)
2284 qemu_mutex_lock(&cur_mon
->mon_lock
);
2285 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2286 if (strcmp(monfd
->name
, fdname
) != 0) {
2290 QLIST_REMOVE(monfd
, next
);
2292 g_free(monfd
->name
);
2294 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2295 /* Make sure close() is outside critical section */
2300 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2301 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2304 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2308 qemu_mutex_lock(&mon
->mon_lock
);
2309 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2312 if (strcmp(monfd
->name
, fdname
) != 0) {
2318 /* caller takes ownership of fd */
2319 QLIST_REMOVE(monfd
, next
);
2320 g_free(monfd
->name
);
2322 qemu_mutex_unlock(&mon
->mon_lock
);
2327 qemu_mutex_unlock(&mon
->mon_lock
);
2328 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2332 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2334 MonFdsetFd
*mon_fdset_fd
;
2335 MonFdsetFd
*mon_fdset_fd_next
;
2337 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2338 if ((mon_fdset_fd
->removed
||
2339 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2340 runstate_is_running()) {
2341 close(mon_fdset_fd
->fd
);
2342 g_free(mon_fdset_fd
->opaque
);
2343 QLIST_REMOVE(mon_fdset_fd
, next
);
2344 g_free(mon_fdset_fd
);
2348 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2349 QLIST_REMOVE(mon_fdset
, next
);
2354 static void monitor_fdsets_cleanup(void)
2356 MonFdset
*mon_fdset
;
2357 MonFdset
*mon_fdset_next
;
2359 qemu_mutex_lock(&mon_fdsets_lock
);
2360 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2361 monitor_fdset_cleanup(mon_fdset
);
2363 qemu_mutex_unlock(&mon_fdsets_lock
);
2366 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2367 const char *opaque
, Error
**errp
)
2370 Monitor
*mon
= cur_mon
;
2373 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
2375 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2379 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2380 has_opaque
, opaque
, errp
);
2392 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2394 MonFdset
*mon_fdset
;
2395 MonFdsetFd
*mon_fdset_fd
;
2398 qemu_mutex_lock(&mon_fdsets_lock
);
2399 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2400 if (mon_fdset
->id
!= fdset_id
) {
2403 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2405 if (mon_fdset_fd
->fd
!= fd
) {
2408 mon_fdset_fd
->removed
= true;
2411 mon_fdset_fd
->removed
= true;
2414 if (has_fd
&& !mon_fdset_fd
) {
2417 monitor_fdset_cleanup(mon_fdset
);
2418 qemu_mutex_unlock(&mon_fdsets_lock
);
2423 qemu_mutex_unlock(&mon_fdsets_lock
);
2425 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2428 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2430 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2433 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2435 MonFdset
*mon_fdset
;
2436 MonFdsetFd
*mon_fdset_fd
;
2437 FdsetInfoList
*fdset_list
= NULL
;
2439 qemu_mutex_lock(&mon_fdsets_lock
);
2440 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2441 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2442 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2444 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2445 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2447 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2448 FdsetFdInfoList
*fdsetfd_info
;
2450 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2451 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2452 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2453 if (mon_fdset_fd
->opaque
) {
2454 fdsetfd_info
->value
->has_opaque
= true;
2455 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2457 fdsetfd_info
->value
->has_opaque
= false;
2460 fdsetfd_info
->next
= fdsetfd_list
;
2461 fdsetfd_list
= fdsetfd_info
;
2464 fdset_info
->value
->fds
= fdsetfd_list
;
2466 fdset_info
->next
= fdset_list
;
2467 fdset_list
= fdset_info
;
2469 qemu_mutex_unlock(&mon_fdsets_lock
);
2474 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2475 bool has_opaque
, const char *opaque
,
2478 MonFdset
*mon_fdset
= NULL
;
2479 MonFdsetFd
*mon_fdset_fd
;
2482 qemu_mutex_lock(&mon_fdsets_lock
);
2484 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2485 /* Break if match found or match impossible due to ordering by ID */
2486 if (fdset_id
<= mon_fdset
->id
) {
2487 if (fdset_id
< mon_fdset
->id
) {
2495 if (mon_fdset
== NULL
) {
2496 int64_t fdset_id_prev
= -1;
2497 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2501 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2502 "a non-negative value");
2503 qemu_mutex_unlock(&mon_fdsets_lock
);
2506 /* Use specified fdset ID */
2507 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2508 mon_fdset_cur
= mon_fdset
;
2509 if (fdset_id
< mon_fdset_cur
->id
) {
2514 /* Use first available fdset ID */
2515 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2516 mon_fdset_cur
= mon_fdset
;
2517 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2518 fdset_id_prev
= mon_fdset_cur
->id
;
2525 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2527 mon_fdset
->id
= fdset_id
;
2529 mon_fdset
->id
= fdset_id_prev
+ 1;
2532 /* The fdset list is ordered by fdset ID */
2533 if (!mon_fdset_cur
) {
2534 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2535 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2536 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2538 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2542 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2543 mon_fdset_fd
->fd
= fd
;
2544 mon_fdset_fd
->removed
= false;
2546 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2548 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2550 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2551 fdinfo
->fdset_id
= mon_fdset
->id
;
2552 fdinfo
->fd
= mon_fdset_fd
->fd
;
2554 qemu_mutex_unlock(&mon_fdsets_lock
);
2558 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2563 MonFdset
*mon_fdset
;
2564 MonFdsetFd
*mon_fdset_fd
;
2568 qemu_mutex_lock(&mon_fdsets_lock
);
2569 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2570 if (mon_fdset
->id
!= fdset_id
) {
2573 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2574 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2575 if (mon_fd_flags
== -1) {
2580 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2581 ret
= mon_fdset_fd
->fd
;
2591 qemu_mutex_unlock(&mon_fdsets_lock
);
2596 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2598 MonFdset
*mon_fdset
;
2599 MonFdsetFd
*mon_fdset_fd_dup
;
2601 qemu_mutex_lock(&mon_fdsets_lock
);
2602 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2603 if (mon_fdset
->id
!= fdset_id
) {
2606 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2607 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2611 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2612 mon_fdset_fd_dup
->fd
= dup_fd
;
2613 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2614 qemu_mutex_unlock(&mon_fdsets_lock
);
2619 qemu_mutex_unlock(&mon_fdsets_lock
);
2623 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2625 MonFdset
*mon_fdset
;
2626 MonFdsetFd
*mon_fdset_fd_dup
;
2628 qemu_mutex_lock(&mon_fdsets_lock
);
2629 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2630 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2631 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2633 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2634 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2635 monitor_fdset_cleanup(mon_fdset
);
2639 qemu_mutex_unlock(&mon_fdsets_lock
);
2640 return mon_fdset
->id
;
2647 qemu_mutex_unlock(&mon_fdsets_lock
);
2651 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
2653 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2656 void monitor_fdset_dup_fd_remove(int dup_fd
)
2658 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2661 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2664 Error
*local_err
= NULL
;
2666 if (!qemu_isdigit(fdname
[0]) && mon
) {
2667 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2669 fd
= qemu_parse_fd(fdname
);
2671 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2676 error_propagate(errp
, local_err
);
2685 /* Please update hmp-commands.hx when adding or changing commands */
2686 static mon_cmd_t info_cmds
[] = {
2687 #include "hmp-commands-info.h"
2691 /* mon_cmds and info_cmds would be sorted at runtime */
2692 static mon_cmd_t mon_cmds
[] = {
2693 #include "hmp-commands.h"
2697 /*******************************************************************/
2699 static const char *pch
;
2700 static sigjmp_buf expr_env
;
2703 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2704 expr_error(Monitor
*mon
, const char *fmt
, ...)
2708 monitor_vprintf(mon
, fmt
, ap
);
2709 monitor_printf(mon
, "\n");
2711 siglongjmp(expr_env
, 1);
2714 /* return 0 if OK, -1 if not found */
2715 static int get_monitor_def(target_long
*pval
, const char *name
)
2717 const MonitorDef
*md
= target_monitor_defs();
2718 CPUState
*cs
= mon_get_cpu();
2723 if (cs
== NULL
|| md
== NULL
) {
2727 for(; md
->name
!= NULL
; md
++) {
2728 if (compare_cmd(name
, md
->name
)) {
2729 if (md
->get_value
) {
2730 *pval
= md
->get_value(md
, md
->offset
);
2732 CPUArchState
*env
= mon_get_cpu_env();
2733 ptr
= (uint8_t *)env
+ md
->offset
;
2736 *pval
= *(int32_t *)ptr
;
2739 *pval
= *(target_long
*)ptr
;
2750 ret
= target_get_monitor_def(cs
, name
, &tmp
);
2752 *pval
= (target_long
) tmp
;
2758 static void next(void)
2762 while (qemu_isspace(*pch
))
2767 static int64_t expr_sum(Monitor
*mon
);
2769 static int64_t expr_unary(Monitor
*mon
)
2778 n
= expr_unary(mon
);
2782 n
= -expr_unary(mon
);
2786 n
= ~expr_unary(mon
);
2792 expr_error(mon
, "')' expected");
2799 expr_error(mon
, "character constant expected");
2803 expr_error(mon
, "missing terminating \' character");
2813 while ((*pch
>= 'a' && *pch
<= 'z') ||
2814 (*pch
>= 'A' && *pch
<= 'Z') ||
2815 (*pch
>= '0' && *pch
<= '9') ||
2816 *pch
== '_' || *pch
== '.') {
2817 if ((q
- buf
) < sizeof(buf
) - 1)
2821 while (qemu_isspace(*pch
))
2824 ret
= get_monitor_def(®
, buf
);
2826 expr_error(mon
, "unknown register");
2831 expr_error(mon
, "unexpected end of expression");
2836 n
= strtoull(pch
, &p
, 0);
2837 if (errno
== ERANGE
) {
2838 expr_error(mon
, "number too large");
2841 expr_error(mon
, "invalid char '%c' in expression", *p
);
2844 while (qemu_isspace(*pch
))
2852 static int64_t expr_prod(Monitor
*mon
)
2857 val
= expr_unary(mon
);
2860 if (op
!= '*' && op
!= '/' && op
!= '%')
2863 val2
= expr_unary(mon
);
2872 expr_error(mon
, "division by zero");
2883 static int64_t expr_logic(Monitor
*mon
)
2888 val
= expr_prod(mon
);
2891 if (op
!= '&' && op
!= '|' && op
!= '^')
2894 val2
= expr_prod(mon
);
2911 static int64_t expr_sum(Monitor
*mon
)
2916 val
= expr_logic(mon
);
2919 if (op
!= '+' && op
!= '-')
2922 val2
= expr_logic(mon
);
2931 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2934 if (sigsetjmp(expr_env
, 0)) {
2938 while (qemu_isspace(*pch
))
2940 *pval
= expr_sum(mon
);
2945 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2947 const char *p
= *pp
;
2951 d
= strtod(p
, &tailp
);
2953 monitor_printf(mon
, "Number expected\n");
2956 if (d
!= d
|| d
- d
!= 0) {
2957 /* NaN or infinity */
2958 monitor_printf(mon
, "Bad number\n");
2967 * Store the command-name in cmdname, and return a pointer to
2968 * the remaining of the command string.
2970 static const char *get_command_name(const char *cmdline
,
2971 char *cmdname
, size_t nlen
)
2974 const char *p
, *pstart
;
2977 while (qemu_isspace(*p
))
2982 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2987 memcpy(cmdname
, pstart
, len
);
2988 cmdname
[len
] = '\0';
2993 * Read key of 'type' into 'key' and return the current
2996 static char *key_get_info(const char *type
, char **key
)
3004 p
= strchr(type
, ':');
3011 str
= g_malloc(len
+ 1);
3012 memcpy(str
, type
, len
);
3019 static int default_fmt_format
= 'x';
3020 static int default_fmt_size
= 4;
3022 static int is_valid_option(const char *c
, const char *typestr
)
3030 typestr
= strstr(typestr
, option
);
3031 return (typestr
!= NULL
);
3034 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3035 const char *cmdname
)
3037 const mon_cmd_t
*cmd
;
3039 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3040 if (compare_cmd(cmdname
, cmd
->name
)) {
3049 * Parse command name from @cmdp according to command table @table.
3050 * If blank, return NULL.
3051 * Else, if no valid command can be found, report to @mon, and return
3053 * Else, change @cmdp to point right behind the name, and return its
3054 * command table entry.
3055 * Do not assume the return value points into @table! It doesn't when
3056 * the command is found in a sub-command table.
3058 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3059 const char *cmdp_start
,
3064 const mon_cmd_t
*cmd
;
3067 /* extract the command name */
3068 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
3072 cmd
= search_dispatch_table(table
, cmdname
);
3074 monitor_printf(mon
, "unknown command: '%.*s'\n",
3075 (int)(p
- cmdp_start
), cmdp_start
);
3078 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
3079 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
3080 "until after exit_preconfig.\n",
3081 (int)(p
- cmdp_start
), cmdp_start
);
3085 /* filter out following useless space */
3086 while (qemu_isspace(*p
)) {
3091 /* search sub command */
3092 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
3093 return monitor_parse_command(mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
3100 * Parse arguments for @cmd.
3101 * If it can't be parsed, report to @mon, and return NULL.
3102 * Else, insert command arguments into a QDict, and return it.
3103 * Note: On success, caller has to free the QDict structure.
3106 static QDict
*monitor_parse_arguments(Monitor
*mon
,
3108 const mon_cmd_t
*cmd
)
3110 const char *typestr
;
3113 const char *p
= *endp
;
3115 QDict
*qdict
= qdict_new();
3117 /* parse the parameters */
3118 typestr
= cmd
->args_type
;
3120 typestr
= key_get_info(typestr
, &key
);
3132 while (qemu_isspace(*p
))
3134 if (*typestr
== '?') {
3137 /* no optional string: NULL argument */
3141 ret
= get_str(buf
, sizeof(buf
), &p
);
3145 monitor_printf(mon
, "%s: filename expected\n",
3149 monitor_printf(mon
, "%s: block device name expected\n",
3153 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
3158 qdict_put_str(qdict
, key
, buf
);
3163 QemuOptsList
*opts_list
;
3166 opts_list
= qemu_find_opts(key
);
3167 if (!opts_list
|| opts_list
->desc
->name
) {
3170 while (qemu_isspace(*p
)) {
3175 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3178 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
3182 qemu_opts_to_qdict(opts
, qdict
);
3183 qemu_opts_del(opts
);
3188 int count
, format
, size
;
3190 while (qemu_isspace(*p
))
3196 if (qemu_isdigit(*p
)) {
3198 while (qemu_isdigit(*p
)) {
3199 count
= count
* 10 + (*p
- '0');
3237 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3238 monitor_printf(mon
, "invalid char in format: '%c'\n",
3243 format
= default_fmt_format
;
3244 if (format
!= 'i') {
3245 /* for 'i', not specifying a size gives -1 as size */
3247 size
= default_fmt_size
;
3248 default_fmt_size
= size
;
3250 default_fmt_format
= format
;
3253 format
= default_fmt_format
;
3254 if (format
!= 'i') {
3255 size
= default_fmt_size
;
3260 qdict_put_int(qdict
, "count", count
);
3261 qdict_put_int(qdict
, "format", format
);
3262 qdict_put_int(qdict
, "size", size
);
3271 while (qemu_isspace(*p
))
3273 if (*typestr
== '?' || *typestr
== '.') {
3274 if (*typestr
== '?') {
3282 while (qemu_isspace(*p
))
3291 if (get_expr(mon
, &val
, &p
))
3293 /* Check if 'i' is greater than 32-bit */
3294 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3295 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3296 monitor_printf(mon
, "integer is for 32-bit values\n");
3298 } else if (c
== 'M') {
3300 monitor_printf(mon
, "enter a positive value\n");
3305 qdict_put_int(qdict
, key
, val
);
3314 while (qemu_isspace(*p
)) {
3317 if (*typestr
== '?') {
3323 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
3324 if (ret
< 0 || val
> INT64_MAX
) {
3325 monitor_printf(mon
, "invalid size\n");
3328 qdict_put_int(qdict
, key
, val
);
3336 while (qemu_isspace(*p
))
3338 if (*typestr
== '?') {
3344 if (get_double(mon
, &val
, &p
) < 0) {
3347 if (p
[0] && p
[1] == 's') {
3350 val
/= 1e3
; p
+= 2; break;
3352 val
/= 1e6
; p
+= 2; break;
3354 val
/= 1e9
; p
+= 2; break;
3357 if (*p
&& !qemu_isspace(*p
)) {
3358 monitor_printf(mon
, "Unknown unit suffix\n");
3361 qdict_put(qdict
, key
, qnum_from_double(val
));
3369 while (qemu_isspace(*p
)) {
3373 while (qemu_isgraph(*p
)) {
3376 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3378 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3381 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3384 qdict_put_bool(qdict
, key
, val
);
3389 const char *tmp
= p
;
3396 while (qemu_isspace(*p
))
3401 if(!is_valid_option(p
, typestr
)) {
3403 monitor_printf(mon
, "%s: unsupported option -%c\n",
3415 qdict_put_bool(qdict
, key
, true);
3422 /* package all remaining string */
3425 while (qemu_isspace(*p
)) {
3428 if (*typestr
== '?') {
3431 /* no remaining string: NULL argument */
3437 monitor_printf(mon
, "%s: string expected\n",
3441 qdict_put_str(qdict
, key
, p
);
3447 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3453 /* check that all arguments were parsed */
3454 while (qemu_isspace(*p
))
3457 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3465 qobject_unref(qdict
);
3470 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
3473 const mon_cmd_t
*cmd
;
3474 const char *cmd_start
= cmdline
;
3476 trace_handle_hmp_command(mon
, cmdline
);
3478 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, mon
->cmd_table
);
3483 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
3485 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
3488 monitor_printf(mon
, "Try \"help %.*s\" for more information\n",
3489 (int)(cmdline
- cmd_start
), cmd_start
);
3493 cmd
->cmd(mon
, qdict
);
3494 qobject_unref(qdict
);
3497 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3499 const char *p
, *pstart
;
3506 p
= qemu_strchrnul(p
, '|');
3508 if (len
> sizeof(cmd
) - 2)
3509 len
= sizeof(cmd
) - 2;
3510 memcpy(cmd
, pstart
, len
);
3512 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3513 readline_add_completion(mon
->rs
, cmd
);
3521 static void file_completion(Monitor
*mon
, const char *input
)
3526 char file
[1024], file_prefix
[1024];
3530 p
= strrchr(input
, '/');
3533 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3534 pstrcpy(path
, sizeof(path
), ".");
3536 input_path_len
= p
- input
+ 1;
3537 memcpy(path
, input
, input_path_len
);
3538 if (input_path_len
> sizeof(path
) - 1)
3539 input_path_len
= sizeof(path
) - 1;
3540 path
[input_path_len
] = '\0';
3541 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3544 ffs
= opendir(path
);
3553 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3557 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3558 memcpy(file
, input
, input_path_len
);
3559 if (input_path_len
< sizeof(file
))
3560 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3562 /* stat the file to find out if it's a directory.
3563 * In that case add a slash to speed up typing long paths
3565 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3566 pstrcat(file
, sizeof(file
), "/");
3568 readline_add_completion(mon
->rs
, file
);
3574 static const char *next_arg_type(const char *typestr
)
3576 const char *p
= strchr(typestr
, ':');
3577 return (p
!= NULL
? ++p
: typestr
);
3580 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3583 if (!str
|| !option
) {
3586 if (!strncmp(option
, str
, strlen(str
))) {
3587 readline_add_completion(rs
, option
);
3591 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3594 ChardevBackendInfoList
*list
, *start
;
3600 readline_set_completion_index(rs
, len
);
3602 start
= list
= qmp_query_chardev_backends(NULL
);
3604 const char *chr_name
= list
->value
->name
;
3606 if (!strncmp(chr_name
, str
, len
)) {
3607 readline_add_completion(rs
, chr_name
);
3611 qapi_free_ChardevBackendInfoList(start
);
3614 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3623 readline_set_completion_index(rs
, len
);
3624 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
3625 add_completion_option(rs
, str
, NetClientDriver_str(i
));
3629 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3639 readline_set_completion_index(rs
, len
);
3640 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3643 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3645 name
= object_class_get_name(OBJECT_CLASS(dc
));
3647 if (dc
->user_creatable
3648 && !strncmp(name
, str
, len
)) {
3649 readline_add_completion(rs
, name
);
3656 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3666 readline_set_completion_index(rs
, len
);
3667 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3671 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3672 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3673 readline_add_completion(rs
, name
);
3680 static void peripheral_device_del_completion(ReadLineState
*rs
,
3681 const char *str
, size_t len
)
3683 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3684 GSList
*list
, *item
;
3686 list
= qdev_build_hotpluggable_device_list(peripheral
);
3691 for (item
= list
; item
; item
= g_slist_next(item
)) {
3692 DeviceState
*dev
= item
->data
;
3694 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3695 readline_add_completion(rs
, dev
->id
);
3702 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3705 ChardevInfoList
*list
, *start
;
3711 readline_set_completion_index(rs
, len
);
3713 start
= list
= qmp_query_chardev(NULL
);
3715 ChardevInfo
*chr
= list
->value
;
3717 if (!strncmp(chr
->label
, str
, len
)) {
3718 readline_add_completion(rs
, chr
->label
);
3722 qapi_free_ChardevInfoList(start
);
3725 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3728 ChardevInfoList
*list
, *start
;
3731 readline_set_completion_index(rs
, len
);
3733 start
= list
= qmp_query_chardev(NULL
);
3735 ChardevInfo
*chr_info
= list
->value
;
3737 if (!strncmp(chr_info
->label
, str
, len
)) {
3738 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
3739 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
3740 readline_add_completion(rs
, chr_info
->label
);
3745 qapi_free_ChardevInfoList(start
);
3748 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3753 ringbuf_completion(rs
, str
);
3756 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3765 readline_set_completion_index(rs
, len
);
3766 peripheral_device_del_completion(rs
, str
, len
);
3769 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3771 ObjectPropertyInfoList
*list
, *start
;
3778 readline_set_completion_index(rs
, len
);
3780 start
= list
= qmp_qom_list("/objects", NULL
);
3782 ObjectPropertyInfo
*info
= list
->value
;
3784 if (!strncmp(info
->type
, "child<", 5)
3785 && !strncmp(info
->name
, str
, len
)) {
3786 readline_add_completion(rs
, info
->name
);
3790 qapi_free_ObjectPropertyInfoList(start
);
3793 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3802 sep
= strrchr(str
, '-');
3807 readline_set_completion_index(rs
, len
);
3808 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3809 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
3810 readline_add_completion(rs
, QKeyCode_str(i
));
3815 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3820 readline_set_completion_index(rs
, len
);
3822 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3824 count
= qemu_find_net_clients_except(NULL
, ncs
,
3825 NET_CLIENT_DRIVER_NONE
,
3827 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3828 const char *name
= ncs
[i
]->name
;
3829 if (!strncmp(str
, name
, len
)) {
3830 readline_add_completion(rs
, name
);
3833 } else if (nb_args
== 3) {
3834 add_completion_option(rs
, str
, "on");
3835 add_completion_option(rs
, str
, "off");
3839 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3842 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3849 readline_set_completion_index(rs
, len
);
3850 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3852 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3854 const char *name
= ncs
[i
]->name
;
3855 if (strncmp(str
, name
, len
)) {
3858 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3860 readline_add_completion(rs
, name
);
3865 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3870 readline_set_completion_index(rs
, len
);
3872 TraceEventIter iter
;
3874 char *pattern
= g_strdup_printf("%s*", str
);
3875 trace_event_iter_init(&iter
, pattern
);
3876 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3877 readline_add_completion(rs
, trace_event_get_name(ev
));
3883 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3888 readline_set_completion_index(rs
, len
);
3890 TraceEventIter iter
;
3892 char *pattern
= g_strdup_printf("%s*", str
);
3893 trace_event_iter_init(&iter
, pattern
);
3894 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3895 readline_add_completion(rs
, trace_event_get_name(ev
));
3898 } else if (nb_args
== 3) {
3899 add_completion_option(rs
, str
, "on");
3900 add_completion_option(rs
, str
, "off");
3904 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3911 readline_set_completion_index(rs
, strlen(str
));
3912 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
3913 add_completion_option(rs
, str
, WatchdogAction_str(i
));
3917 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3923 readline_set_completion_index(rs
, len
);
3926 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3927 const char *name
= MigrationCapability_str(i
);
3928 if (!strncmp(str
, name
, len
)) {
3929 readline_add_completion(rs
, name
);
3932 } else if (nb_args
== 3) {
3933 add_completion_option(rs
, str
, "on");
3934 add_completion_option(rs
, str
, "off");
3938 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3944 readline_set_completion_index(rs
, len
);
3947 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3948 const char *name
= MigrationParameter_str(i
);
3949 if (!strncmp(str
, name
, len
)) {
3950 readline_add_completion(rs
, name
);
3956 static void vm_completion(ReadLineState
*rs
, const char *str
)
3959 BlockDriverState
*bs
;
3960 BdrvNextIterator it
;
3963 readline_set_completion_index(rs
, len
);
3965 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3966 SnapshotInfoList
*snapshots
, *snapshot
;
3967 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3970 aio_context_acquire(ctx
);
3971 if (bdrv_can_snapshot(bs
)) {
3972 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3974 aio_context_release(ctx
);
3979 snapshot
= snapshots
;
3981 char *completion
= snapshot
->value
->name
;
3982 if (!strncmp(str
, completion
, len
)) {
3983 readline_add_completion(rs
, completion
);
3985 completion
= snapshot
->value
->id
;
3986 if (!strncmp(str
, completion
, len
)) {
3987 readline_add_completion(rs
, completion
);
3989 snapshot
= snapshot
->next
;
3991 qapi_free_SnapshotInfoList(snapshots
);
3996 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3999 vm_completion(rs
, str
);
4003 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4006 vm_completion(rs
, str
);
4010 static void monitor_find_completion_by_table(Monitor
*mon
,
4011 const mon_cmd_t
*cmd_table
,
4015 const char *cmdname
;
4017 const char *ptype
, *old_ptype
, *str
, *name
;
4018 const mon_cmd_t
*cmd
;
4019 BlockBackend
*blk
= NULL
;
4022 /* command completion */
4027 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4028 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4029 if (!runstate_check(RUN_STATE_PRECONFIG
) ||
4030 cmd_can_preconfig(cmd
)) {
4031 cmd_completion(mon
, cmdname
, cmd
->name
);
4035 /* find the command */
4036 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4037 if (compare_cmd(args
[0], cmd
->name
) &&
4038 (!runstate_check(RUN_STATE_PRECONFIG
) ||
4039 cmd_can_preconfig(cmd
))) {
4047 if (cmd
->sub_table
) {
4048 /* do the job again */
4049 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4050 &args
[1], nb_args
- 1);
4053 if (cmd
->command_completion
) {
4054 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4058 ptype
= next_arg_type(cmd
->args_type
);
4059 for(i
= 0; i
< nb_args
- 2; i
++) {
4060 if (*ptype
!= '\0') {
4061 ptype
= next_arg_type(ptype
);
4062 while (*ptype
== '?')
4063 ptype
= next_arg_type(ptype
);
4066 str
= args
[nb_args
- 1];
4068 while (*ptype
== '-' && old_ptype
!= ptype
) {
4070 ptype
= next_arg_type(ptype
);
4074 /* file completion */
4075 readline_set_completion_index(mon
->rs
, strlen(str
));
4076 file_completion(mon
, str
);
4079 /* block device name completion */
4080 readline_set_completion_index(mon
->rs
, strlen(str
));
4081 while ((blk
= blk_next(blk
)) != NULL
) {
4082 name
= blk_name(blk
);
4083 if (str
[0] == '\0' ||
4084 !strncmp(name
, str
, strlen(str
))) {
4085 readline_add_completion(mon
->rs
, name
);
4091 if (!strcmp(cmd
->name
, "help|?")) {
4092 monitor_find_completion_by_table(mon
, cmd_table
,
4093 &args
[1], nb_args
- 1);
4102 static void monitor_find_completion(void *opaque
,
4103 const char *cmdline
)
4105 Monitor
*mon
= opaque
;
4106 char *args
[MAX_ARGS
];
4109 /* 1. parse the cmdline */
4110 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4114 /* if the line ends with a space, it means we want to complete the
4116 len
= strlen(cmdline
);
4117 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4118 if (nb_args
>= MAX_ARGS
) {
4121 args
[nb_args
++] = g_strdup("");
4124 /* 2. auto complete according to args */
4125 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
4128 free_cmdline_args(args
, nb_args
);
4131 static int monitor_can_read(void *opaque
)
4133 Monitor
*mon
= opaque
;
4135 return !atomic_mb_read(&mon
->suspend_cnt
);
4139 * Emit QMP response @rsp with ID @id to @mon.
4140 * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
4141 * Nothing is emitted then.
4143 static void monitor_qmp_respond(MonitorQMP
*mon
, QDict
*rsp
)
4146 qmp_send_response(mon
, rsp
);
4150 static void monitor_qmp_dispatch(MonitorQMP
*mon
, QObject
*req
)
4157 cur_mon
= &mon
->common
;
4159 rsp
= qmp_dispatch(mon
->commands
, req
, qmp_oob_enabled(mon
));
4163 if (mon
->commands
== &qmp_cap_negotiation_commands
) {
4164 error
= qdict_get_qdict(rsp
, "error");
4166 && !g_strcmp0(qdict_get_try_str(error
, "class"),
4167 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND
))) {
4168 /* Provide a more useful error message */
4169 qdict_del(error
, "desc");
4170 qdict_put_str(error
, "desc", "Expecting capabilities negotiation"
4171 " with 'qmp_capabilities'");
4175 monitor_qmp_respond(mon
, rsp
);
4180 * Pop a QMP request from a monitor request queue.
4181 * Return the request, or NULL all request queues are empty.
4182 * We are using round-robin fashion to pop the request, to avoid
4183 * processing commands only on a very busy monitor. To achieve that,
4184 * when we process one request on a specific monitor, we put that
4185 * monitor to the end of mon_list queue.
4187 * Note: if the function returned with non-NULL, then the caller will
4188 * be with qmp_mon->qmp_queue_lock held, and the caller is responsible
4191 static QMPRequest
*monitor_qmp_requests_pop_any_with_lock(void)
4193 QMPRequest
*req_obj
= NULL
;
4195 MonitorQMP
*qmp_mon
;
4197 qemu_mutex_lock(&monitor_lock
);
4199 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
4200 if (!monitor_is_qmp(mon
)) {
4204 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
4205 qemu_mutex_lock(&qmp_mon
->qmp_queue_lock
);
4206 req_obj
= g_queue_pop_head(qmp_mon
->qmp_requests
);
4208 /* With the lock of corresponding queue held */
4211 qemu_mutex_unlock(&qmp_mon
->qmp_queue_lock
);
4216 * We found one request on the monitor. Degrade this monitor's
4217 * priority to lowest by re-inserting it to end of queue.
4219 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4220 QTAILQ_INSERT_TAIL(&mon_list
, mon
, entry
);
4223 qemu_mutex_unlock(&monitor_lock
);
4228 static void monitor_qmp_bh_dispatcher(void *data
)
4230 QMPRequest
*req_obj
= monitor_qmp_requests_pop_any_with_lock();
4240 /* qmp_oob_enabled() might change after "qmp_capabilities" */
4241 need_resume
= !qmp_oob_enabled(mon
) ||
4242 mon
->qmp_requests
->length
== QMP_REQ_QUEUE_LEN_MAX
- 1;
4243 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
4245 QDict
*qdict
= qobject_to(QDict
, req_obj
->req
);
4246 QObject
*id
= qdict
? qdict_get(qdict
, "id") : NULL
;
4247 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id
) ?: "");
4248 monitor_qmp_dispatch(mon
, req_obj
->req
);
4250 assert(req_obj
->err
);
4251 rsp
= qmp_error_response(req_obj
->err
);
4252 req_obj
->err
= NULL
;
4253 monitor_qmp_respond(mon
, rsp
);
4258 /* Pairs with the monitor_suspend() in handle_qmp_command() */
4259 monitor_resume(&mon
->common
);
4261 qmp_request_free(req_obj
);
4263 /* Reschedule instead of looping so the main loop stays responsive */
4264 qemu_bh_schedule(qmp_dispatcher_bh
);
4267 static void handle_qmp_command(void *opaque
, QObject
*req
, Error
*err
)
4269 MonitorQMP
*mon
= opaque
;
4272 QMPRequest
*req_obj
;
4274 assert(!req
!= !err
);
4276 qdict
= qobject_to(QDict
, req
);
4278 id
= qdict_get(qdict
, "id");
4279 } /* else will fail qmp_dispatch() */
4281 if (req
&& trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND
)) {
4282 QString
*req_json
= qobject_to_json(req
);
4283 trace_handle_qmp_command(mon
, qstring_get_str(req_json
));
4284 qobject_unref(req_json
);
4287 if (qdict
&& qmp_is_oob(qdict
)) {
4288 /* OOB commands are executed immediately */
4289 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id
) ?: "");
4290 monitor_qmp_dispatch(mon
, req
);
4295 req_obj
= g_new0(QMPRequest
, 1);
4300 /* Protect qmp_requests and fetching its length. */
4301 qemu_mutex_lock(&mon
->qmp_queue_lock
);
4304 * Suspend the monitor when we can't queue more requests after
4305 * this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume
4306 * it. Note that when OOB is disabled, we queue at most one
4307 * command, for backward compatibility.
4309 if (!qmp_oob_enabled(mon
) ||
4310 mon
->qmp_requests
->length
== QMP_REQ_QUEUE_LEN_MAX
- 1) {
4311 monitor_suspend(&mon
->common
);
4315 * Put the request to the end of queue so that requests will be
4316 * handled in time order. Ownership for req_obj, req,
4317 * etc. will be delivered to the handler side.
4319 assert(mon
->qmp_requests
->length
< QMP_REQ_QUEUE_LEN_MAX
);
4320 g_queue_push_tail(mon
->qmp_requests
, req_obj
);
4321 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
4323 /* Kick the dispatcher routine */
4324 qemu_bh_schedule(qmp_dispatcher_bh
);
4327 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4329 MonitorQMP
*mon
= opaque
;
4331 json_message_parser_feed(&mon
->parser
, (const char *) buf
, size
);
4334 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4336 Monitor
*old_mon
= cur_mon
;
4342 for (i
= 0; i
< size
; i
++)
4343 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4345 if (size
== 0 || buf
[size
- 1] != 0)
4346 monitor_printf(cur_mon
, "corrupted command\n");
4348 handle_hmp_command(cur_mon
, (char *)buf
);
4354 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4355 void *readline_opaque
)
4357 Monitor
*mon
= opaque
;
4359 monitor_suspend(mon
);
4360 handle_hmp_command(mon
, cmdline
);
4361 monitor_resume(mon
);
4364 int monitor_suspend(Monitor
*mon
)
4366 if (monitor_is_hmp_non_interactive(mon
)) {
4370 atomic_inc(&mon
->suspend_cnt
);
4372 if (mon
->use_io_thread
) {
4374 * Kick I/O thread to make sure this takes effect. It'll be
4375 * evaluated again in prepare() of the watch object.
4377 aio_notify(iothread_get_aio_context(mon_iothread
));
4380 trace_monitor_suspend(mon
, 1);
4384 static void monitor_accept_input(void *opaque
)
4386 Monitor
*mon
= opaque
;
4388 qemu_chr_fe_accept_input(&mon
->chr
);
4391 void monitor_resume(Monitor
*mon
)
4393 if (monitor_is_hmp_non_interactive(mon
)) {
4397 if (atomic_dec_fetch(&mon
->suspend_cnt
) == 0) {
4400 if (mon
->use_io_thread
) {
4401 ctx
= iothread_get_aio_context(mon_iothread
);
4403 ctx
= qemu_get_aio_context();
4406 if (!monitor_is_qmp(mon
)) {
4408 readline_show_prompt(mon
->rs
);
4411 aio_bh_schedule_oneshot(ctx
, monitor_accept_input
, mon
);
4414 trace_monitor_suspend(mon
, -1);
4417 static QDict
*qmp_greeting(MonitorQMP
*mon
)
4419 QList
*cap_list
= qlist_new();
4420 QObject
*ver
= NULL
;
4423 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4425 for (cap
= 0; cap
< QMP_CAPABILITY__MAX
; cap
++) {
4426 if (mon
->capab_offered
[cap
]) {
4427 qlist_append_str(cap_list
, QMPCapability_str(cap
));
4431 return qdict_from_jsonf_nofail(
4432 "{'QMP': {'version': %p, 'capabilities': %p}}",
4436 static void monitor_qmp_event(void *opaque
, int event
)
4439 MonitorQMP
*mon
= opaque
;
4442 case CHR_EVENT_OPENED
:
4443 mon
->commands
= &qmp_cap_negotiation_commands
;
4444 monitor_qmp_caps_reset(mon
);
4445 data
= qmp_greeting(mon
);
4446 qmp_send_response(mon
, data
);
4447 qobject_unref(data
);
4450 case CHR_EVENT_CLOSED
:
4452 * Note: this is only useful when the output of the chardev
4453 * backend is still open. For example, when the backend is
4454 * stdio, it's possible that stdout is still open when stdin
4457 monitor_qmp_cleanup_queues(mon
);
4458 json_message_parser_destroy(&mon
->parser
);
4459 json_message_parser_init(&mon
->parser
, handle_qmp_command
,
4462 monitor_fdsets_cleanup();
4467 static void monitor_event(void *opaque
, int event
)
4469 Monitor
*mon
= opaque
;
4472 case CHR_EVENT_MUX_IN
:
4473 qemu_mutex_lock(&mon
->mon_lock
);
4475 qemu_mutex_unlock(&mon
->mon_lock
);
4476 if (mon
->reset_seen
) {
4477 readline_restart(mon
->rs
);
4478 monitor_resume(mon
);
4481 atomic_mb_set(&mon
->suspend_cnt
, 0);
4485 case CHR_EVENT_MUX_OUT
:
4486 if (mon
->reset_seen
) {
4487 if (atomic_mb_read(&mon
->suspend_cnt
) == 0) {
4488 monitor_printf(mon
, "\n");
4491 monitor_suspend(mon
);
4493 atomic_inc(&mon
->suspend_cnt
);
4495 qemu_mutex_lock(&mon
->mon_lock
);
4497 qemu_mutex_unlock(&mon
->mon_lock
);
4500 case CHR_EVENT_OPENED
:
4501 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4502 "information\n", QEMU_VERSION
);
4503 if (!mon
->mux_out
) {
4504 readline_restart(mon
->rs
);
4505 readline_show_prompt(mon
->rs
);
4507 mon
->reset_seen
= 1;
4511 case CHR_EVENT_CLOSED
:
4513 monitor_fdsets_cleanup();
4519 compare_mon_cmd(const void *a
, const void *b
)
4521 return strcmp(((const mon_cmd_t
*)a
)->name
,
4522 ((const mon_cmd_t
*)b
)->name
);
4525 static void sortcmdlist(void)
4528 int elem_size
= sizeof(mon_cmd_t
);
4530 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4531 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4533 array_num
= sizeof(info_cmds
)/elem_size
-1;
4534 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4537 static void monitor_iothread_init(void)
4539 mon_iothread
= iothread_create("mon_iothread", &error_abort
);
4542 void monitor_init_globals(void)
4544 monitor_init_qmp_commands();
4545 monitor_qapi_event_init();
4547 qemu_mutex_init(&monitor_lock
);
4548 qemu_mutex_init(&mon_fdsets_lock
);
4551 * The dispatcher BH must run in the main loop thread, since we
4552 * have commands assuming that context. It would be nice to get
4553 * rid of those assumptions.
4555 qmp_dispatcher_bh
= aio_bh_new(iohandler_get_aio_context(),
4556 monitor_qmp_bh_dispatcher
,
4560 /* These functions just adapt the readline interface in a typesafe way. We
4561 * could cast function pointers but that discards compiler checks.
4563 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4564 const char *fmt
, ...)
4568 monitor_vprintf(opaque
, fmt
, ap
);
4572 static void monitor_readline_flush(void *opaque
)
4574 monitor_flush(opaque
);
4578 * Print to current monitor if we have one, else to stderr.
4580 int error_vprintf(const char *fmt
, va_list ap
)
4582 if (cur_mon
&& !monitor_cur_is_qmp()) {
4583 return monitor_vprintf(cur_mon
, fmt
, ap
);
4585 return vfprintf(stderr
, fmt
, ap
);
4588 int error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
4591 return vfprintf(stderr
, fmt
, ap
);
4593 if (!monitor_cur_is_qmp()) {
4594 return monitor_vprintf(cur_mon
, fmt
, ap
);
4599 static void monitor_list_append(Monitor
*mon
)
4601 qemu_mutex_lock(&monitor_lock
);
4603 * This prevents inserting new monitors during monitor_cleanup().
4604 * A cleaner solution would involve the main thread telling other
4605 * threads to terminate, waiting for their termination.
4607 if (!monitor_destroyed
) {
4608 QTAILQ_INSERT_HEAD(&mon_list
, mon
, entry
);
4611 qemu_mutex_unlock(&monitor_lock
);
4614 monitor_data_destroy(mon
);
4619 static void monitor_qmp_setup_handlers_bh(void *opaque
)
4621 MonitorQMP
*mon
= opaque
;
4622 GMainContext
*context
;
4624 assert(mon
->common
.use_io_thread
);
4625 context
= iothread_get_g_main_context(mon_iothread
);
4627 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
,
4628 monitor_qmp_read
, monitor_qmp_event
,
4629 NULL
, &mon
->common
, context
, true);
4630 monitor_list_append(&mon
->common
);
4633 static void monitor_init_qmp(Chardev
*chr
, int flags
)
4635 MonitorQMP
*mon
= g_new0(MonitorQMP
, 1);
4637 /* Only HMP supports readline */
4638 assert(!(flags
& MONITOR_USE_READLINE
));
4640 /* Note: we run QMP monitor in I/O thread when @chr supports that */
4641 monitor_data_init(&mon
->common
, flags
, false,
4642 qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
));
4644 qemu_mutex_init(&mon
->qmp_queue_lock
);
4645 mon
->qmp_requests
= g_queue_new();
4647 qemu_chr_fe_init(&mon
->common
.chr
, chr
, &error_abort
);
4648 qemu_chr_fe_set_echo(&mon
->common
.chr
, true);
4650 json_message_parser_init(&mon
->parser
, handle_qmp_command
, mon
, NULL
);
4651 if (mon
->common
.use_io_thread
) {
4653 * Make sure the old iowatch is gone. It's possible when
4654 * e.g. the chardev is in client mode, with wait=on.
4656 remove_fd_in_watch(chr
);
4658 * We can't call qemu_chr_fe_set_handlers() directly here
4659 * since chardev might be running in the monitor I/O
4660 * thread. Schedule a bottom half.
4662 aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread
),
4663 monitor_qmp_setup_handlers_bh
, mon
);
4664 /* The bottom half will add @mon to @mon_list */
4666 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
,
4667 monitor_qmp_read
, monitor_qmp_event
,
4668 NULL
, &mon
->common
, NULL
, true);
4669 monitor_list_append(&mon
->common
);
4673 static void monitor_init_hmp(Chardev
*chr
, int flags
)
4675 Monitor
*mon
= g_new0(Monitor
, 1);
4676 bool use_readline
= flags
& MONITOR_USE_READLINE
;
4678 monitor_data_init(mon
, flags
, false, false);
4679 qemu_chr_fe_init(&mon
->chr
, chr
, &error_abort
);
4682 mon
->rs
= readline_init(monitor_readline_printf
,
4683 monitor_readline_flush
,
4685 monitor_find_completion
);
4686 monitor_read_command(mon
, 0);
4689 qemu_chr_fe_set_handlers(&mon
->chr
, monitor_can_read
, monitor_read
,
4690 monitor_event
, NULL
, mon
, NULL
, true);
4691 monitor_list_append(mon
);
4694 void monitor_init(Chardev
*chr
, int flags
)
4696 if (flags
& MONITOR_USE_CONTROL
) {
4697 monitor_init_qmp(chr
, flags
);
4699 monitor_init_hmp(chr
, flags
);
4703 void monitor_cleanup(void)
4706 * We need to explicitly stop the I/O thread (but not destroy it),
4707 * clean up the monitor resources, then destroy the I/O thread since
4708 * we need to unregister from chardev below in
4709 * monitor_data_destroy(), and chardev is not thread-safe yet
4712 iothread_stop(mon_iothread
);
4715 /* Flush output buffers and destroy monitors */
4716 qemu_mutex_lock(&monitor_lock
);
4717 monitor_destroyed
= true;
4718 while (!QTAILQ_EMPTY(&mon_list
)) {
4719 Monitor
*mon
= QTAILQ_FIRST(&mon_list
);
4720 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4721 /* Permit QAPI event emission from character frontend release */
4722 qemu_mutex_unlock(&monitor_lock
);
4724 monitor_data_destroy(mon
);
4725 qemu_mutex_lock(&monitor_lock
);
4728 qemu_mutex_unlock(&monitor_lock
);
4730 /* QEMUBHs needs to be deleted before destroying the I/O thread */
4731 qemu_bh_delete(qmp_dispatcher_bh
);
4732 qmp_dispatcher_bh
= NULL
;
4734 iothread_destroy(mon_iothread
);
4735 mon_iothread
= NULL
;
4739 QemuOptsList qemu_mon_opts
= {
4741 .implied_opt_name
= "chardev",
4742 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4746 .type
= QEMU_OPT_STRING
,
4749 .type
= QEMU_OPT_STRING
,
4752 .type
= QEMU_OPT_BOOL
,
4754 { /* end of list */ }
4758 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4760 MachineState
*ms
= MACHINE(qdev_get_machine());
4761 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4763 if (!mc
->has_hotpluggable_cpus
) {
4764 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4768 return machine_query_hotpluggable_cpus(ms
);