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"
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 HMPCommand
{
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 HMPCommand
*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 QTAILQ_ENTRY(Monitor
) entry
;
199 * The per-monitor lock. We can't access guest memory when holding
205 * Members that are protected by the per-monitor lock
207 QLIST_HEAD(, mon_fd_t
) fds
;
210 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
217 * State used only in the thread "owning" the monitor.
218 * If @use_io_thread, this is @mon_iothread. (This does not actually happen
219 * in the current state of the code.)
220 * Else, it's the main thread.
221 * These members can be safely accessed without locks.
228 JSONMessageParser parser
;
230 * When a client connects, we're in capabilities negotiation mode.
231 * @commands is &qmp_cap_negotiation_commands then. When command
232 * qmp_capabilities succeeds, we go into command mode, and
233 * @command becomes &qmp_commands.
235 QmpCommandList
*commands
;
236 bool capab_offered
[QMP_CAPABILITY__MAX
]; /* capabilities offered */
237 bool capab
[QMP_CAPABILITY__MAX
]; /* offered and accepted */
239 * Protects qmp request/response queue.
240 * Take monitor_lock first when you need both.
242 QemuMutex qmp_queue_lock
;
243 /* Input queue that holds all the parsed QMP requests */
244 GQueue
*qmp_requests
;
247 /* Shared monitor I/O thread */
248 IOThread
*mon_iothread
;
250 /* Bottom half to dispatch the requests received from I/O thread */
251 QEMUBH
*qmp_dispatcher_bh
;
254 /* Owner of the request */
257 * Request object to be handled or Error to be reported
258 * (exactly one of them is non-null)
263 typedef struct QMPRequest QMPRequest
;
265 /* QMP checker flags */
266 #define QMP_ACCEPT_UNKNOWNS 1
268 /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
269 static QemuMutex monitor_lock
;
270 static GHashTable
*monitor_qapi_event_state
;
271 static QTAILQ_HEAD(, Monitor
) mon_list
;
272 static bool monitor_destroyed
;
274 /* Protects mon_fdsets */
275 static QemuMutex mon_fdsets_lock
;
276 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
278 static int mon_refcount
;
280 static HMPCommand hmp_cmds
[];
281 static HMPCommand hmp_info_cmds
[];
283 QmpCommandList qmp_commands
, qmp_cap_negotiation_commands
;
285 __thread Monitor
*cur_mon
;
287 static void monitor_command_cb(void *opaque
, const char *cmdline
,
288 void *readline_opaque
);
291 * Is @mon a QMP monitor?
293 static inline bool monitor_is_qmp(const Monitor
*mon
)
295 return (mon
->flags
& MONITOR_USE_CONTROL
);
299 * Is @mon is using readline?
300 * Note: not all HMP monitors use readline, e.g., gdbserver has a
301 * non-interactive HMP monitor, so readline is not used there.
303 static inline bool monitor_uses_readline(const Monitor
*mon
)
305 return mon
->flags
& MONITOR_USE_READLINE
;
308 static inline bool monitor_is_hmp_non_interactive(const Monitor
*mon
)
310 return !monitor_is_qmp(mon
) && !monitor_uses_readline(mon
);
314 * Return the clock to use for recording an event's time.
315 * It's QEMU_CLOCK_REALTIME, except for qtests it's
316 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
317 * Beware: result is invalid before configure_accelerator().
319 static inline QEMUClockType
monitor_get_event_clock(void)
321 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL
: QEMU_CLOCK_REALTIME
;
325 * Is the current monitor, if any, a QMP monitor?
327 bool monitor_cur_is_qmp(void)
329 return cur_mon
&& monitor_is_qmp(cur_mon
);
332 void monitor_read_command(MonitorHMP
*mon
, int show_prompt
)
337 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
339 readline_show_prompt(mon
->rs
);
342 int monitor_read_password(MonitorHMP
*mon
, ReadLineFunc
*readline_func
,
346 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
347 /* prompt is printed on return from the command handler */
350 monitor_printf(&mon
->common
,
351 "terminal does not support password prompting\n");
356 static void qmp_request_free(QMPRequest
*req
)
358 qobject_unref(req
->req
);
359 error_free(req
->err
);
363 /* Caller must hold mon->qmp.qmp_queue_lock */
364 static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP
*mon
)
366 while (!g_queue_is_empty(mon
->qmp_requests
)) {
367 qmp_request_free(g_queue_pop_head(mon
->qmp_requests
));
371 static void monitor_qmp_cleanup_queues(MonitorQMP
*mon
)
373 qemu_mutex_lock(&mon
->qmp_queue_lock
);
374 monitor_qmp_cleanup_req_queue_locked(mon
);
375 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
379 static void monitor_flush_locked(Monitor
*mon
);
381 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
384 Monitor
*mon
= opaque
;
386 qemu_mutex_lock(&mon
->mon_lock
);
388 monitor_flush_locked(mon
);
389 qemu_mutex_unlock(&mon
->mon_lock
);
393 /* Caller must hold mon->mon_lock */
394 static void monitor_flush_locked(Monitor
*mon
)
400 if (mon
->skip_flush
) {
404 buf
= qstring_get_str(mon
->outbuf
);
405 len
= qstring_get_length(mon
->outbuf
);
407 if (len
&& !mon
->mux_out
) {
408 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
409 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
410 /* all flushed or error */
411 qobject_unref(mon
->outbuf
);
412 mon
->outbuf
= qstring_new();
417 QString
*tmp
= qstring_from_str(buf
+ rc
);
418 qobject_unref(mon
->outbuf
);
421 if (mon
->out_watch
== 0) {
423 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
424 monitor_unblocked
, mon
);
429 void monitor_flush(Monitor
*mon
)
431 qemu_mutex_lock(&mon
->mon_lock
);
432 monitor_flush_locked(mon
);
433 qemu_mutex_unlock(&mon
->mon_lock
);
436 /* flush at every end of line */
437 static int monitor_puts(Monitor
*mon
, const char *str
)
442 qemu_mutex_lock(&mon
->mon_lock
);
443 for (i
= 0; str
[i
]; i
++) {
446 qstring_append_chr(mon
->outbuf
, '\r');
448 qstring_append_chr(mon
->outbuf
, c
);
450 monitor_flush_locked(mon
);
453 qemu_mutex_unlock(&mon
->mon_lock
);
458 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
466 if (monitor_is_qmp(mon
)) {
470 buf
= g_strdup_vprintf(fmt
, ap
);
471 n
= monitor_puts(mon
, buf
);
476 int monitor_printf(Monitor
*mon
, const char *fmt
, ...)
482 ret
= monitor_vprintf(mon
, fmt
, ap
);
487 static void qmp_send_response(MonitorQMP
*mon
, const QDict
*rsp
)
489 const QObject
*data
= QOBJECT(rsp
);
492 json
= mon
->common
.flags
& MONITOR_USE_PRETTY
?
493 qobject_to_json_pretty(data
) : qobject_to_json(data
);
494 assert(json
!= NULL
);
496 qstring_append_chr(json
, '\n');
497 monitor_puts(&mon
->common
, qstring_get_str(json
));
502 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
503 /* Limit guest-triggerable events to 1 per second */
504 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
505 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
506 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
507 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
508 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
509 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
513 * Broadcast an event to all monitors.
514 * @qdict is the event object. Its member "event" must match @event.
515 * Caller must hold monitor_lock.
517 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
522 trace_monitor_protocol_event_emit(event
, qdict
);
523 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
524 if (!monitor_is_qmp(mon
)) {
528 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
529 if (qmp_mon
->commands
!= &qmp_cap_negotiation_commands
) {
530 qmp_send_response(qmp_mon
, qdict
);
535 static void monitor_qapi_event_handler(void *opaque
);
538 * Queue a new event for emission to Monitor instances,
539 * applying any rate limiting if required.
542 monitor_qapi_event_queue_no_reenter(QAPIEvent event
, QDict
*qdict
)
544 MonitorQAPIEventConf
*evconf
;
545 MonitorQAPIEventState
*evstate
;
547 assert(event
< QAPI_EVENT__MAX
);
548 evconf
= &monitor_qapi_event_conf
[event
];
549 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
551 qemu_mutex_lock(&monitor_lock
);
554 /* Unthrottled event */
555 monitor_qapi_event_emit(event
, qdict
);
557 QDict
*data
= qobject_to(QDict
, qdict_get(qdict
, "data"));
558 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
560 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
561 assert(!evstate
|| timer_pending(evstate
->timer
));
565 * Timer is pending for (at least) evconf->rate ns after
566 * last send. Store event for sending when timer fires,
567 * replacing a prior stored event if any.
569 qobject_unref(evstate
->qdict
);
570 evstate
->qdict
= qobject_ref(qdict
);
573 * Last send was (at least) evconf->rate ns ago.
574 * Send immediately, and arm the timer to call
575 * monitor_qapi_event_handler() in evconf->rate ns. Any
576 * events arriving before then will be delayed until then.
578 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
580 monitor_qapi_event_emit(event
, qdict
);
582 evstate
= g_new(MonitorQAPIEventState
, 1);
583 evstate
->event
= event
;
584 evstate
->data
= qobject_ref(data
);
585 evstate
->qdict
= NULL
;
586 evstate
->timer
= timer_new_ns(monitor_get_event_clock(),
587 monitor_qapi_event_handler
,
589 g_hash_table_add(monitor_qapi_event_state
, evstate
);
590 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
594 qemu_mutex_unlock(&monitor_lock
);
597 void qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
600 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
601 * would deadlock on monitor_lock. Work around by queueing
602 * events in thread-local storage.
603 * TODO: remove this, make it re-enter safe.
605 typedef struct MonitorQapiEvent
{
608 QSIMPLEQ_ENTRY(MonitorQapiEvent
) entry
;
610 static __thread
QSIMPLEQ_HEAD(, MonitorQapiEvent
) event_queue
;
611 static __thread
bool reentered
;
612 MonitorQapiEvent
*ev
;
615 QSIMPLEQ_INIT(&event_queue
);
618 ev
= g_new(MonitorQapiEvent
, 1);
619 ev
->qdict
= qobject_ref(qdict
);
621 QSIMPLEQ_INSERT_TAIL(&event_queue
, ev
, entry
);
628 while ((ev
= QSIMPLEQ_FIRST(&event_queue
)) != NULL
) {
629 QSIMPLEQ_REMOVE_HEAD(&event_queue
, entry
);
630 monitor_qapi_event_queue_no_reenter(ev
->event
, ev
->qdict
);
631 qobject_unref(ev
->qdict
);
639 * This function runs evconf->rate ns after sending a throttled
641 * If another event has since been stored, send it.
643 static void monitor_qapi_event_handler(void *opaque
)
645 MonitorQAPIEventState
*evstate
= opaque
;
646 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
648 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
649 qemu_mutex_lock(&monitor_lock
);
651 if (evstate
->qdict
) {
652 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
654 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
655 qobject_unref(evstate
->qdict
);
656 evstate
->qdict
= NULL
;
657 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
659 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
660 qobject_unref(evstate
->data
);
661 timer_free(evstate
->timer
);
665 qemu_mutex_unlock(&monitor_lock
);
668 static unsigned int qapi_event_throttle_hash(const void *key
)
670 const MonitorQAPIEventState
*evstate
= key
;
671 unsigned int hash
= evstate
->event
* 255;
673 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
674 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
677 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
678 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
684 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
686 const MonitorQAPIEventState
*eva
= a
;
687 const MonitorQAPIEventState
*evb
= b
;
689 if (eva
->event
!= evb
->event
) {
693 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
694 return !strcmp(qdict_get_str(eva
->data
, "id"),
695 qdict_get_str(evb
->data
, "id"));
698 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
699 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
700 qdict_get_str(evb
->data
, "node-name"));
706 static void monitor_qapi_event_init(void)
708 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
709 qapi_event_throttle_equal
);
712 static void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
);
714 static void monitor_iothread_init(void);
716 static void monitor_data_init(Monitor
*mon
, int flags
, bool skip_flush
,
719 if (use_io_thread
&& !mon_iothread
) {
720 monitor_iothread_init();
722 qemu_mutex_init(&mon
->mon_lock
);
723 mon
->outbuf
= qstring_new();
724 mon
->skip_flush
= skip_flush
;
725 mon
->use_io_thread
= use_io_thread
;
729 static void monitor_data_destroy_qmp(MonitorQMP
*mon
)
731 json_message_parser_destroy(&mon
->parser
);
732 qemu_mutex_destroy(&mon
->qmp_queue_lock
);
733 monitor_qmp_cleanup_req_queue_locked(mon
);
734 g_queue_free(mon
->qmp_requests
);
737 static void monitor_data_destroy(Monitor
*mon
)
739 g_free(mon
->mon_cpu_path
);
740 qemu_chr_fe_deinit(&mon
->chr
, false);
741 if (monitor_is_qmp(mon
)) {
742 monitor_data_destroy_qmp(container_of(mon
, MonitorQMP
, common
));
744 readline_free(container_of(mon
, MonitorHMP
, common
)->rs
);
746 qobject_unref(mon
->outbuf
);
747 qemu_mutex_destroy(&mon
->mon_lock
);
750 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
751 int64_t cpu_index
, Error
**errp
)
757 monitor_data_init(&hmp
.common
, 0, true, false);
760 cur_mon
= &hmp
.common
;
763 int ret
= monitor_set_cpu(cpu_index
);
766 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
772 handle_hmp_command(&hmp
, command_line
);
775 qemu_mutex_lock(&hmp
.common
.mon_lock
);
776 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
777 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
779 output
= g_strdup("");
781 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
784 monitor_data_destroy(&hmp
.common
);
788 static int compare_cmd(const char *name
, const char *list
)
790 const char *p
, *pstart
;
796 p
= qemu_strchrnul(p
, '|');
797 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
806 static int get_str(char *buf
, int buf_size
, const char **pp
)
814 while (qemu_isspace(*p
)) {
825 while (*p
!= '\0' && *p
!= '\"') {
841 printf("unsupported escape code: '\\%c'\n", c
);
844 if ((q
- buf
) < buf_size
- 1) {
848 if ((q
- buf
) < buf_size
- 1) {
855 printf("unterminated string\n");
860 while (*p
!= '\0' && !qemu_isspace(*p
)) {
861 if ((q
- buf
) < buf_size
- 1) {
874 static void free_cmdline_args(char **args
, int nb_args
)
878 assert(nb_args
<= MAX_ARGS
);
880 for (i
= 0; i
< nb_args
; i
++) {
887 * Parse the command line to get valid args.
888 * @cmdline: command line to be parsed.
889 * @pnb_args: location to store the number of args, must NOT be NULL.
890 * @args: location to store the args, which should be freed by caller, must
893 * Returns 0 on success, negative on failure.
895 * NOTE: this parser is an approximate form of the real command parser. Number
896 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
897 * return with failure.
899 static int parse_cmdline(const char *cmdline
,
900 int *pnb_args
, char **args
)
909 while (qemu_isspace(*p
)) {
915 if (nb_args
>= MAX_ARGS
) {
918 ret
= get_str(buf
, sizeof(buf
), &p
);
922 args
[nb_args
] = g_strdup(buf
);
929 free_cmdline_args(args
, nb_args
);
934 * Can command @cmd be executed in preconfig state?
936 static bool cmd_can_preconfig(const HMPCommand
*cmd
)
942 return strchr(cmd
->flags
, 'p');
945 static void help_cmd_dump_one(Monitor
*mon
,
946 const HMPCommand
*cmd
,
952 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
956 for (i
= 0; i
< prefix_args_nb
; i
++) {
957 monitor_printf(mon
, "%s ", prefix_args
[i
]);
959 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
962 /* @args[@arg_index] is the valid command need to find in @cmds */
963 static void help_cmd_dump(Monitor
*mon
, const HMPCommand
*cmds
,
964 char **args
, int nb_args
, int arg_index
)
966 const HMPCommand
*cmd
;
969 /* No valid arg need to compare with, dump all in *cmds */
970 if (arg_index
>= nb_args
) {
971 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
972 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
977 /* Find one entry to dump */
978 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
979 if (compare_cmd(args
[arg_index
], cmd
->name
) &&
980 ((!runstate_check(RUN_STATE_PRECONFIG
) ||
981 cmd_can_preconfig(cmd
)))) {
982 if (cmd
->sub_table
) {
983 /* continue with next arg */
984 help_cmd_dump(mon
, cmd
->sub_table
,
985 args
, nb_args
, arg_index
+ 1);
987 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
993 /* Command not found */
994 monitor_printf(mon
, "unknown command: '");
995 for (i
= 0; i
<= arg_index
; i
++) {
996 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
1000 static void help_cmd(Monitor
*mon
, const char *name
)
1002 char *args
[MAX_ARGS
];
1005 /* 1. parse user input */
1007 /* special case for log, directly dump and return */
1008 if (!strcmp(name
, "log")) {
1009 const QEMULogItem
*item
;
1010 monitor_printf(mon
, "Log items (comma separated):\n");
1011 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
1012 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
1013 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
1018 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
1023 /* 2. dump the contents according to parsed args */
1024 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
1026 free_cmdline_args(args
, nb_args
);
1029 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
1031 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
1034 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
1036 const char *tp_name
= qdict_get_str(qdict
, "name");
1037 bool new_state
= qdict_get_bool(qdict
, "option");
1038 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1039 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1040 Error
*local_err
= NULL
;
1043 monitor_printf(mon
, "argument vcpu must be positive");
1047 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
1049 error_report_err(local_err
);
1053 #ifdef CONFIG_TRACE_SIMPLE
1054 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
1056 const char *op
= qdict_get_try_str(qdict
, "op");
1057 const char *arg
= qdict_get_try_str(qdict
, "arg");
1060 st_print_trace_file_status();
1061 } else if (!strcmp(op
, "on")) {
1062 st_set_trace_file_enabled(true);
1063 } else if (!strcmp(op
, "off")) {
1064 st_set_trace_file_enabled(false);
1065 } else if (!strcmp(op
, "flush")) {
1066 st_flush_trace_buffer();
1067 } else if (!strcmp(op
, "set")) {
1069 st_set_trace_file(arg
);
1072 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
1073 help_cmd(mon
, "trace-file");
1078 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
1080 help_cmd(mon
, "info");
1083 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
1085 CommandInfoList
*info
, **list
= opaque
;
1087 if (!cmd
->enabled
) {
1091 info
= g_malloc0(sizeof(*info
));
1092 info
->value
= g_malloc0(sizeof(*info
->value
));
1093 info
->value
->name
= g_strdup(cmd
->name
);
1098 CommandInfoList
*qmp_query_commands(Error
**errp
)
1100 CommandInfoList
*list
= NULL
;
1103 assert(monitor_is_qmp(cur_mon
));
1104 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1106 qmp_for_each_command(mon
->commands
, query_commands_cb
, &list
);
1111 EventInfoList
*qmp_query_events(Error
**errp
)
1114 * TODO This deprecated command is the only user of
1115 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
1116 * they should go, too.
1118 EventInfoList
*info
, *ev_list
= NULL
;
1121 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
1122 const char *event_name
= QAPIEvent_str(e
);
1123 assert(event_name
!= NULL
);
1124 info
= g_malloc0(sizeof(*info
));
1125 info
->value
= g_malloc0(sizeof(*info
->value
));
1126 info
->value
->name
= g_strdup(event_name
);
1128 info
->next
= ev_list
;
1136 * Minor hack: generated marshalling suppressed for this command
1137 * ('gen': false in the schema) so we can parse the JSON string
1138 * directly into QObject instead of first parsing it with
1139 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1140 * to QObject with generated output marshallers, every time. Instead,
1141 * we do it in test-qobject-input-visitor.c, just to make sure
1142 * qapi-gen.py's output actually conforms to the schema.
1144 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
1147 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
1150 static void monitor_init_qmp_commands(void)
1153 * Two command lists:
1154 * - qmp_commands contains all QMP commands
1155 * - qmp_cap_negotiation_commands contains just
1156 * "qmp_capabilities", to enforce capability negotiation
1159 qmp_init_marshal(&qmp_commands
);
1161 qmp_register_command(&qmp_commands
, "query-qmp-schema",
1162 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
1163 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
1165 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
1168 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
1169 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
1170 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
1173 static bool qmp_oob_enabled(MonitorQMP
*mon
)
1175 return mon
->capab
[QMP_CAPABILITY_OOB
];
1178 static void monitor_qmp_caps_reset(MonitorQMP
*mon
)
1180 memset(mon
->capab_offered
, 0, sizeof(mon
->capab_offered
));
1181 memset(mon
->capab
, 0, sizeof(mon
->capab
));
1182 mon
->capab_offered
[QMP_CAPABILITY_OOB
] = mon
->common
.use_io_thread
;
1186 * Accept QMP capabilities in @list for @mon.
1187 * On success, set mon->qmp.capab[], and return true.
1188 * On error, set @errp, and return false.
1190 static bool qmp_caps_accept(MonitorQMP
*mon
, QMPCapabilityList
*list
,
1193 GString
*unavailable
= NULL
;
1194 bool capab
[QMP_CAPABILITY__MAX
];
1196 memset(capab
, 0, sizeof(capab
));
1198 for (; list
; list
= list
->next
) {
1199 if (!mon
->capab_offered
[list
->value
]) {
1201 unavailable
= g_string_new(QMPCapability_str(list
->value
));
1203 g_string_append_printf(unavailable
, ", %s",
1204 QMPCapability_str(list
->value
));
1207 capab
[list
->value
] = true;
1211 error_setg(errp
, "Capability %s not available", unavailable
->str
);
1212 g_string_free(unavailable
, true);
1216 memcpy(mon
->capab
, capab
, sizeof(capab
));
1220 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
1225 assert(monitor_is_qmp(cur_mon
));
1226 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1228 if (mon
->commands
== &qmp_commands
) {
1229 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
1230 "Capabilities negotiation is already complete, command "
1235 if (!qmp_caps_accept(mon
, enable
, errp
)) {
1239 mon
->commands
= &qmp_commands
;
1242 /* Set the current CPU defined by the user. Callers must hold BQL. */
1243 int monitor_set_cpu(int cpu_index
)
1247 cpu
= qemu_get_cpu(cpu_index
);
1251 g_free(cur_mon
->mon_cpu_path
);
1252 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
1256 /* Callers must hold BQL. */
1257 static CPUState
*mon_get_cpu_sync(bool synchronize
)
1261 if (cur_mon
->mon_cpu_path
) {
1262 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
1265 g_free(cur_mon
->mon_cpu_path
);
1266 cur_mon
->mon_cpu_path
= NULL
;
1269 if (!cur_mon
->mon_cpu_path
) {
1273 monitor_set_cpu(first_cpu
->cpu_index
);
1277 cpu_synchronize_state(cpu
);
1282 CPUState
*mon_get_cpu(void)
1284 return mon_get_cpu_sync(true);
1287 CPUArchState
*mon_get_cpu_env(void)
1289 CPUState
*cs
= mon_get_cpu();
1291 return cs
? cs
->env_ptr
: NULL
;
1294 int monitor_get_cpu_index(void)
1296 CPUState
*cs
= mon_get_cpu_sync(false);
1298 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
1301 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1303 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
1308 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
1309 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1315 monitor_printf(mon
, "No CPU available\n");
1319 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1324 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1326 if (!tcg_enabled()) {
1327 error_report("JIT information is only available with accel=tcg");
1335 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1337 dump_opcount_info();
1341 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
1343 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
1344 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
1345 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
1346 enum QSPSortBy sort_by
;
1348 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
1349 qsp_report(max
, sort_by
, coalesce
);
1352 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1354 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1363 str
= readline_get_history(hmp_mon
->rs
, i
);
1367 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1372 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1374 CPUState
*cs
= mon_get_cpu();
1377 monitor_printf(mon
, "No CPU available\n");
1380 cpu_dump_statistics(cs
, 0);
1383 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1385 const char *name
= qdict_get_try_str(qdict
, "name");
1386 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1387 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1388 TraceEventInfoList
*events
;
1389 TraceEventInfoList
*elem
;
1390 Error
*local_err
= NULL
;
1396 monitor_printf(mon
, "argument vcpu must be positive");
1400 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1402 error_report_err(local_err
);
1406 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1407 monitor_printf(mon
, "%s : state %u\n",
1409 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1411 qapi_free_TraceEventInfoList(events
);
1414 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1415 bool has_port
, int64_t port
,
1416 bool has_tls_port
, int64_t tls_port
,
1417 bool has_cert_subject
, const char *cert_subject
,
1420 if (strcmp(protocol
, "spice") == 0) {
1421 if (!qemu_using_spice(errp
)) {
1425 if (!has_port
&& !has_tls_port
) {
1426 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1430 if (qemu_spice_migrate_info(hostname
,
1431 has_port
? port
: -1,
1432 has_tls_port
? tls_port
: -1,
1434 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1440 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1443 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1447 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1449 error_report_err(err
);
1453 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1456 const char *items
= qdict_get_str(qdict
, "items");
1458 if (!strcmp(items
, "none")) {
1461 mask
= qemu_str_to_log_mask(items
);
1463 help_cmd(mon
, "log");
1470 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1472 const char *option
= qdict_get_try_str(qdict
, "option");
1473 if (!option
|| !strcmp(option
, "on")) {
1475 } else if (!strcmp(option
, "off")) {
1478 monitor_printf(mon
, "unexpected option %s\n", option
);
1482 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1484 const char *device
= qdict_get_try_str(qdict
, "device");
1486 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1487 if (gdbserver_start(device
) < 0) {
1488 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1490 } else if (strcmp(device
, "none") == 0) {
1491 monitor_printf(mon
, "Disabled gdbserver\n");
1493 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1498 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1500 const char *action
= qdict_get_str(qdict
, "action");
1501 if (select_watchdog_action(action
) == -1) {
1502 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1506 static void monitor_printc(Monitor
*mon
, int c
)
1508 monitor_printf(mon
, "'");
1511 monitor_printf(mon
, "\\'");
1514 monitor_printf(mon
, "\\\\");
1517 monitor_printf(mon
, "\\n");
1520 monitor_printf(mon
, "\\r");
1523 if (c
>= 32 && c
<= 126) {
1524 monitor_printf(mon
, "%c", c
);
1526 monitor_printf(mon
, "\\x%02x", c
);
1530 monitor_printf(mon
, "'");
1533 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1534 hwaddr addr
, int is_physical
)
1536 int l
, line_size
, i
, max_digits
, len
;
1539 CPUState
*cs
= mon_get_cpu();
1541 if (!cs
&& (format
== 'i' || !is_physical
)) {
1542 monitor_printf(mon
, "Can not dump without CPU\n");
1546 if (format
== 'i') {
1547 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
1551 len
= wsize
* count
;
1560 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
1564 max_digits
= (wsize
* 8) / 4;
1568 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
1577 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1579 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1584 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
1585 MemTxResult r
= address_space_read(as
, addr
,
1586 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
1587 if (r
!= MEMTX_OK
) {
1588 monitor_printf(mon
, " Cannot access memory\n");
1592 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
1593 monitor_printf(mon
, " Cannot access memory\n");
1602 v
= ldub_p(buf
+ i
);
1605 v
= lduw_p(buf
+ i
);
1608 v
= (uint32_t)ldl_p(buf
+ i
);
1614 monitor_printf(mon
, " ");
1617 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1620 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1623 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1626 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1629 monitor_printc(mon
, v
);
1634 monitor_printf(mon
, "\n");
1640 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1642 int count
= qdict_get_int(qdict
, "count");
1643 int format
= qdict_get_int(qdict
, "format");
1644 int size
= qdict_get_int(qdict
, "size");
1645 target_long addr
= qdict_get_int(qdict
, "addr");
1647 memory_dump(mon
, count
, format
, size
, addr
, 0);
1650 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1652 int count
= qdict_get_int(qdict
, "count");
1653 int format
= qdict_get_int(qdict
, "format");
1654 int size
= qdict_get_int(qdict
, "size");
1655 hwaddr addr
= qdict_get_int(qdict
, "addr");
1657 memory_dump(mon
, count
, format
, size
, addr
, 1);
1660 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
1662 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
1666 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
1670 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
1671 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
1672 memory_region_unref(mrs
.mr
);
1677 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
1680 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
1682 hwaddr addr
= qdict_get_int(qdict
, "addr");
1683 Error
*local_err
= NULL
;
1684 MemoryRegion
*mr
= NULL
;
1687 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1689 error_report_err(local_err
);
1693 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
1695 addr
, mr
->name
, ptr
);
1697 memory_region_unref(mr
);
1700 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
1702 target_ulong addr
= qdict_get_int(qdict
, "addr");
1704 CPUState
*cs
= mon_get_cpu();
1708 monitor_printf(mon
, "No cpu\n");
1712 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
1714 monitor_printf(mon
, "Unmapped\n");
1716 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
1717 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
1722 static uint64_t vtop(void *ptr
, Error
**errp
)
1726 uintptr_t addr
= (uintptr_t) ptr
;
1727 uintptr_t pagesize
= getpagesize();
1728 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
1731 fd
= open("/proc/self/pagemap", O_RDONLY
);
1733 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
1737 /* Force copy-on-write if necessary. */
1738 atomic_add((uint8_t *)ptr
, 0);
1740 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
1741 error_setg_errno(errp
, errno
, "Cannot read pagemap");
1744 if ((pinfo
& (1ull << 63)) == 0) {
1745 error_setg(errp
, "Page not present");
1748 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
1755 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
1757 hwaddr addr
= qdict_get_int(qdict
, "addr");
1758 Error
*local_err
= NULL
;
1759 MemoryRegion
*mr
= NULL
;
1763 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1765 error_report_err(local_err
);
1769 physaddr
= vtop(ptr
, &local_err
);
1771 error_report_err(local_err
);
1773 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
1774 " (%s) is 0x%" PRIx64
"\n",
1775 addr
, mr
->name
, (uint64_t) physaddr
);
1778 memory_region_unref(mr
);
1782 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1784 int format
= qdict_get_int(qdict
, "format");
1785 hwaddr val
= qdict_get_int(qdict
, "val");
1789 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1792 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1795 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1799 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1802 monitor_printc(mon
, val
);
1805 monitor_printf(mon
, "\n");
1808 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1812 uint32_t start
= qdict_get_int(qdict
, "start");
1813 uint32_t size
= qdict_get_int(qdict
, "size");
1816 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1817 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1818 MEMTXATTRS_UNSPECIFIED
, NULL
);
1819 /* BSD sum algorithm ('sum' Unix command) */
1820 sum
= (sum
>> 1) | (sum
<< 15);
1823 monitor_printf(mon
, "%05d\n", sum
);
1826 static int mouse_button_state
;
1828 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1830 int dx
, dy
, dz
, button
;
1831 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1832 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1833 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1835 dx
= strtol(dx_str
, NULL
, 0);
1836 dy
= strtol(dy_str
, NULL
, 0);
1837 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1838 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1841 dz
= strtol(dz_str
, NULL
, 0);
1843 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1844 qemu_input_queue_btn(NULL
, button
, true);
1845 qemu_input_event_sync();
1846 qemu_input_queue_btn(NULL
, button
, false);
1849 qemu_input_event_sync();
1852 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1854 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1855 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1856 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1857 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1859 int button_state
= qdict_get_int(qdict
, "button_state");
1861 if (mouse_button_state
== button_state
) {
1864 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1865 qemu_input_event_sync();
1866 mouse_button_state
= button_state
;
1869 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1871 int size
= qdict_get_int(qdict
, "size");
1872 int addr
= qdict_get_int(qdict
, "addr");
1873 int has_index
= qdict_haskey(qdict
, "index");
1878 int index
= qdict_get_int(qdict
, "index");
1879 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1887 val
= cpu_inb(addr
);
1891 val
= cpu_inw(addr
);
1895 val
= cpu_inl(addr
);
1899 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1900 suffix
, addr
, size
* 2, val
);
1903 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1905 int size
= qdict_get_int(qdict
, "size");
1906 int addr
= qdict_get_int(qdict
, "addr");
1907 int val
= qdict_get_int(qdict
, "val");
1909 addr
&= IOPORTS_MASK
;
1914 cpu_outb(addr
, val
);
1917 cpu_outw(addr
, val
);
1920 cpu_outl(addr
, val
);
1925 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1927 Error
*local_err
= NULL
;
1928 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1930 qemu_boot_set(bootdevice
, &local_err
);
1932 error_report_err(local_err
);
1934 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1938 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1940 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1941 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1942 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1944 mtree_info(flatview
, dispatch_tree
, owner
);
1947 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1950 NumaNodeMem
*node_mem
;
1951 CpuInfoList
*cpu_list
, *cpu
;
1953 cpu_list
= qmp_query_cpus(&error_abort
);
1954 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
1956 query_numa_node_mem(node_mem
);
1957 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1958 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1959 monitor_printf(mon
, "node %d cpus:", i
);
1960 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
1961 if (cpu
->value
->has_props
&& cpu
->value
->props
->has_node_id
&&
1962 cpu
->value
->props
->node_id
== i
) {
1963 monitor_printf(mon
, " %" PRIi64
, cpu
->value
->CPU
);
1966 monitor_printf(mon
, "\n");
1967 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1968 node_mem
[i
].node_mem
>> 20);
1969 monitor_printf(mon
, "node %d plugged: %" PRId64
" MB\n", i
,
1970 node_mem
[i
].node_plugged_mem
>> 20);
1972 qapi_free_CpuInfoList(cpu_list
);
1976 #ifdef CONFIG_PROFILER
1980 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1982 static int64_t last_cpu_exec_time
;
1983 int64_t cpu_exec_time
;
1986 cpu_exec_time
= tcg_cpu_exec_time();
1987 delta
= cpu_exec_time
- last_cpu_exec_time
;
1989 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1990 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1991 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1992 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1993 last_cpu_exec_time
= cpu_exec_time
;
1997 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1999 monitor_printf(mon
, "Internal profiler not compiled\n");
2003 /* Capture support */
2004 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2006 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
2011 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2012 monitor_printf(mon
, "[%d]: ", i
);
2013 s
->ops
.info (s
->opaque
);
2017 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
2020 int n
= qdict_get_int(qdict
, "n");
2023 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2025 s
->ops
.destroy (s
->opaque
);
2026 QLIST_REMOVE (s
, entries
);
2033 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
2035 const char *path
= qdict_get_str(qdict
, "path");
2036 int has_freq
= qdict_haskey(qdict
, "freq");
2037 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2038 int has_bits
= qdict_haskey(qdict
, "bits");
2039 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2040 int has_channels
= qdict_haskey(qdict
, "nchannels");
2041 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2044 s
= g_malloc0 (sizeof (*s
));
2046 freq
= has_freq
? freq
: 44100;
2047 bits
= has_bits
? bits
: 16;
2048 nchannels
= has_channels
? nchannels
: 2;
2050 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2051 monitor_printf(mon
, "Failed to add wave capture\n");
2055 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2058 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
2063 container
= object_get_objects_root();
2064 obj
= object_resolve_path_component(container
, name
);
2066 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2070 return QAUTHZ_LIST(obj
);
2073 static bool warn_acl
;
2074 static void hmp_warn_acl(void)
2079 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
2080 "commands are deprecated with no replacement. Authorization "
2081 "for VNC should be performed using the pluggable QAuthZ "
2086 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
2088 const char *aclname
= qdict_get_str(qdict
, "aclname");
2089 QAuthZList
*auth
= find_auth(mon
, aclname
);
2090 QAuthZListRuleList
*rules
;
2099 monitor_printf(mon
, "policy: %s\n",
2100 QAuthZListPolicy_str(auth
->policy
));
2102 rules
= auth
->rules
;
2104 QAuthZListRule
*rule
= rules
->value
;
2106 monitor_printf(mon
, "%zu: %s %s\n", i
,
2107 QAuthZListPolicy_str(rule
->policy
),
2109 rules
= rules
->next
;
2113 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2115 const char *aclname
= qdict_get_str(qdict
, "aclname");
2116 QAuthZList
*auth
= find_auth(mon
, aclname
);
2124 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
2125 qapi_free_QAuthZListRuleList(auth
->rules
);
2127 monitor_printf(mon
, "acl: removed all rules\n");
2130 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2132 const char *aclname
= qdict_get_str(qdict
, "aclname");
2133 const char *policy
= qdict_get_str(qdict
, "policy");
2134 QAuthZList
*auth
= find_auth(mon
, aclname
);
2144 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
2146 QAUTHZ_LIST_POLICY_DENY
,
2150 monitor_printf(mon
, "acl: unknown policy '%s', "
2151 "expected 'deny' or 'allow'\n", policy
);
2154 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
2155 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2157 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2162 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
2164 if (strchr(match
, '*')) {
2165 return QAUTHZ_LIST_FORMAT_GLOB
;
2167 return QAUTHZ_LIST_FORMAT_EXACT
;
2171 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2173 const char *aclname
= qdict_get_str(qdict
, "aclname");
2174 const char *match
= qdict_get_str(qdict
, "match");
2175 const char *policystr
= qdict_get_str(qdict
, "policy");
2176 int has_index
= qdict_haskey(qdict
, "index");
2177 int index
= qdict_get_try_int(qdict
, "index", -1);
2178 QAuthZList
*auth
= find_auth(mon
, aclname
);
2180 QAuthZListPolicy policy
;
2181 QAuthZListFormat format
;
2190 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
2192 QAUTHZ_LIST_POLICY_DENY
,
2196 monitor_printf(mon
, "acl: unknown policy '%s', "
2197 "expected 'deny' or 'allow'\n", policystr
);
2201 format
= hmp_acl_get_format(match
);
2203 if (has_index
&& index
== 0) {
2204 monitor_printf(mon
, "acl: unable to add acl entry\n");
2209 i
= qauthz_list_insert_rule(auth
, match
, policy
,
2210 format
, index
- 1, &err
);
2212 i
= qauthz_list_append_rule(auth
, match
, policy
,
2216 monitor_printf(mon
, "acl: unable to add rule: %s",
2217 error_get_pretty(err
));
2220 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
2224 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2226 const char *aclname
= qdict_get_str(qdict
, "aclname");
2227 const char *match
= qdict_get_str(qdict
, "match");
2228 QAuthZList
*auth
= find_auth(mon
, aclname
);
2237 i
= qauthz_list_delete_rule(auth
, match
);
2239 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
2241 monitor_printf(mon
, "acl: no matching acl entry\n");
2245 void qmp_getfd(const char *fdname
, Error
**errp
)
2250 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
2252 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2256 if (qemu_isdigit(fdname
[0])) {
2258 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2259 "a name not starting with a digit");
2263 qemu_mutex_lock(&cur_mon
->mon_lock
);
2264 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2265 if (strcmp(monfd
->name
, fdname
) != 0) {
2271 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2272 /* Make sure close() is outside critical section */
2277 monfd
= g_malloc0(sizeof(mon_fd_t
));
2278 monfd
->name
= g_strdup(fdname
);
2281 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2282 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2285 void qmp_closefd(const char *fdname
, Error
**errp
)
2290 qemu_mutex_lock(&cur_mon
->mon_lock
);
2291 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2292 if (strcmp(monfd
->name
, fdname
) != 0) {
2296 QLIST_REMOVE(monfd
, next
);
2298 g_free(monfd
->name
);
2300 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2301 /* Make sure close() is outside critical section */
2306 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2307 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2310 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2314 qemu_mutex_lock(&mon
->mon_lock
);
2315 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2318 if (strcmp(monfd
->name
, fdname
) != 0) {
2324 /* caller takes ownership of fd */
2325 QLIST_REMOVE(monfd
, next
);
2326 g_free(monfd
->name
);
2328 qemu_mutex_unlock(&mon
->mon_lock
);
2333 qemu_mutex_unlock(&mon
->mon_lock
);
2334 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2338 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2340 MonFdsetFd
*mon_fdset_fd
;
2341 MonFdsetFd
*mon_fdset_fd_next
;
2343 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2344 if ((mon_fdset_fd
->removed
||
2345 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2346 runstate_is_running()) {
2347 close(mon_fdset_fd
->fd
);
2348 g_free(mon_fdset_fd
->opaque
);
2349 QLIST_REMOVE(mon_fdset_fd
, next
);
2350 g_free(mon_fdset_fd
);
2354 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2355 QLIST_REMOVE(mon_fdset
, next
);
2360 static void monitor_fdsets_cleanup(void)
2362 MonFdset
*mon_fdset
;
2363 MonFdset
*mon_fdset_next
;
2365 qemu_mutex_lock(&mon_fdsets_lock
);
2366 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2367 monitor_fdset_cleanup(mon_fdset
);
2369 qemu_mutex_unlock(&mon_fdsets_lock
);
2372 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2373 const char *opaque
, Error
**errp
)
2376 Monitor
*mon
= cur_mon
;
2379 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
2381 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2385 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2386 has_opaque
, opaque
, errp
);
2398 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2400 MonFdset
*mon_fdset
;
2401 MonFdsetFd
*mon_fdset_fd
;
2404 qemu_mutex_lock(&mon_fdsets_lock
);
2405 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2406 if (mon_fdset
->id
!= fdset_id
) {
2409 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2411 if (mon_fdset_fd
->fd
!= fd
) {
2414 mon_fdset_fd
->removed
= true;
2417 mon_fdset_fd
->removed
= true;
2420 if (has_fd
&& !mon_fdset_fd
) {
2423 monitor_fdset_cleanup(mon_fdset
);
2424 qemu_mutex_unlock(&mon_fdsets_lock
);
2429 qemu_mutex_unlock(&mon_fdsets_lock
);
2431 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2434 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2436 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2439 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2441 MonFdset
*mon_fdset
;
2442 MonFdsetFd
*mon_fdset_fd
;
2443 FdsetInfoList
*fdset_list
= NULL
;
2445 qemu_mutex_lock(&mon_fdsets_lock
);
2446 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2447 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2448 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2450 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2451 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2453 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2454 FdsetFdInfoList
*fdsetfd_info
;
2456 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2457 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2458 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2459 if (mon_fdset_fd
->opaque
) {
2460 fdsetfd_info
->value
->has_opaque
= true;
2461 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2463 fdsetfd_info
->value
->has_opaque
= false;
2466 fdsetfd_info
->next
= fdsetfd_list
;
2467 fdsetfd_list
= fdsetfd_info
;
2470 fdset_info
->value
->fds
= fdsetfd_list
;
2472 fdset_info
->next
= fdset_list
;
2473 fdset_list
= fdset_info
;
2475 qemu_mutex_unlock(&mon_fdsets_lock
);
2480 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2481 bool has_opaque
, const char *opaque
,
2484 MonFdset
*mon_fdset
= NULL
;
2485 MonFdsetFd
*mon_fdset_fd
;
2488 qemu_mutex_lock(&mon_fdsets_lock
);
2490 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2491 /* Break if match found or match impossible due to ordering by ID */
2492 if (fdset_id
<= mon_fdset
->id
) {
2493 if (fdset_id
< mon_fdset
->id
) {
2501 if (mon_fdset
== NULL
) {
2502 int64_t fdset_id_prev
= -1;
2503 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2507 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2508 "a non-negative value");
2509 qemu_mutex_unlock(&mon_fdsets_lock
);
2512 /* Use specified fdset ID */
2513 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2514 mon_fdset_cur
= mon_fdset
;
2515 if (fdset_id
< mon_fdset_cur
->id
) {
2520 /* Use first available fdset ID */
2521 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2522 mon_fdset_cur
= mon_fdset
;
2523 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2524 fdset_id_prev
= mon_fdset_cur
->id
;
2531 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2533 mon_fdset
->id
= fdset_id
;
2535 mon_fdset
->id
= fdset_id_prev
+ 1;
2538 /* The fdset list is ordered by fdset ID */
2539 if (!mon_fdset_cur
) {
2540 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2541 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2542 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2544 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2548 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2549 mon_fdset_fd
->fd
= fd
;
2550 mon_fdset_fd
->removed
= false;
2552 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2554 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2556 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2557 fdinfo
->fdset_id
= mon_fdset
->id
;
2558 fdinfo
->fd
= mon_fdset_fd
->fd
;
2560 qemu_mutex_unlock(&mon_fdsets_lock
);
2564 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2569 MonFdset
*mon_fdset
;
2570 MonFdsetFd
*mon_fdset_fd
;
2574 qemu_mutex_lock(&mon_fdsets_lock
);
2575 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2576 if (mon_fdset
->id
!= fdset_id
) {
2579 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2580 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2581 if (mon_fd_flags
== -1) {
2586 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2587 ret
= mon_fdset_fd
->fd
;
2597 qemu_mutex_unlock(&mon_fdsets_lock
);
2602 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2604 MonFdset
*mon_fdset
;
2605 MonFdsetFd
*mon_fdset_fd_dup
;
2607 qemu_mutex_lock(&mon_fdsets_lock
);
2608 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2609 if (mon_fdset
->id
!= fdset_id
) {
2612 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2613 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2617 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2618 mon_fdset_fd_dup
->fd
= dup_fd
;
2619 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2620 qemu_mutex_unlock(&mon_fdsets_lock
);
2625 qemu_mutex_unlock(&mon_fdsets_lock
);
2629 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2631 MonFdset
*mon_fdset
;
2632 MonFdsetFd
*mon_fdset_fd_dup
;
2634 qemu_mutex_lock(&mon_fdsets_lock
);
2635 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2636 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2637 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2639 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2640 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2641 monitor_fdset_cleanup(mon_fdset
);
2645 qemu_mutex_unlock(&mon_fdsets_lock
);
2646 return mon_fdset
->id
;
2653 qemu_mutex_unlock(&mon_fdsets_lock
);
2657 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
2659 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2662 void monitor_fdset_dup_fd_remove(int dup_fd
)
2664 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2667 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2670 Error
*local_err
= NULL
;
2672 if (!qemu_isdigit(fdname
[0]) && mon
) {
2673 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2675 fd
= qemu_parse_fd(fdname
);
2677 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2682 error_propagate(errp
, local_err
);
2691 /* Please update hmp-commands.hx when adding or changing commands */
2692 static HMPCommand hmp_info_cmds
[] = {
2693 #include "hmp-commands-info.h"
2697 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
2698 static HMPCommand hmp_cmds
[] = {
2699 #include "hmp-commands.h"
2703 /*******************************************************************/
2705 static const char *pch
;
2706 static sigjmp_buf expr_env
;
2709 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2710 expr_error(Monitor
*mon
, const char *fmt
, ...)
2714 monitor_vprintf(mon
, fmt
, ap
);
2715 monitor_printf(mon
, "\n");
2717 siglongjmp(expr_env
, 1);
2720 /* return 0 if OK, -1 if not found */
2721 static int get_monitor_def(target_long
*pval
, const char *name
)
2723 const MonitorDef
*md
= target_monitor_defs();
2724 CPUState
*cs
= mon_get_cpu();
2729 if (cs
== NULL
|| md
== NULL
) {
2733 for(; md
->name
!= NULL
; md
++) {
2734 if (compare_cmd(name
, md
->name
)) {
2735 if (md
->get_value
) {
2736 *pval
= md
->get_value(md
, md
->offset
);
2738 CPUArchState
*env
= mon_get_cpu_env();
2739 ptr
= (uint8_t *)env
+ md
->offset
;
2742 *pval
= *(int32_t *)ptr
;
2745 *pval
= *(target_long
*)ptr
;
2756 ret
= target_get_monitor_def(cs
, name
, &tmp
);
2758 *pval
= (target_long
) tmp
;
2764 static void next(void)
2768 while (qemu_isspace(*pch
))
2773 static int64_t expr_sum(Monitor
*mon
);
2775 static int64_t expr_unary(Monitor
*mon
)
2784 n
= expr_unary(mon
);
2788 n
= -expr_unary(mon
);
2792 n
= ~expr_unary(mon
);
2798 expr_error(mon
, "')' expected");
2805 expr_error(mon
, "character constant expected");
2809 expr_error(mon
, "missing terminating \' character");
2819 while ((*pch
>= 'a' && *pch
<= 'z') ||
2820 (*pch
>= 'A' && *pch
<= 'Z') ||
2821 (*pch
>= '0' && *pch
<= '9') ||
2822 *pch
== '_' || *pch
== '.') {
2823 if ((q
- buf
) < sizeof(buf
) - 1)
2827 while (qemu_isspace(*pch
))
2830 ret
= get_monitor_def(®
, buf
);
2832 expr_error(mon
, "unknown register");
2837 expr_error(mon
, "unexpected end of expression");
2842 n
= strtoull(pch
, &p
, 0);
2843 if (errno
== ERANGE
) {
2844 expr_error(mon
, "number too large");
2847 expr_error(mon
, "invalid char '%c' in expression", *p
);
2850 while (qemu_isspace(*pch
))
2858 static int64_t expr_prod(Monitor
*mon
)
2863 val
= expr_unary(mon
);
2866 if (op
!= '*' && op
!= '/' && op
!= '%')
2869 val2
= expr_unary(mon
);
2878 expr_error(mon
, "division by zero");
2889 static int64_t expr_logic(Monitor
*mon
)
2894 val
= expr_prod(mon
);
2897 if (op
!= '&' && op
!= '|' && op
!= '^')
2900 val2
= expr_prod(mon
);
2917 static int64_t expr_sum(Monitor
*mon
)
2922 val
= expr_logic(mon
);
2925 if (op
!= '+' && op
!= '-')
2928 val2
= expr_logic(mon
);
2937 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2940 if (sigsetjmp(expr_env
, 0)) {
2944 while (qemu_isspace(*pch
))
2946 *pval
= expr_sum(mon
);
2951 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2953 const char *p
= *pp
;
2957 d
= strtod(p
, &tailp
);
2959 monitor_printf(mon
, "Number expected\n");
2962 if (d
!= d
|| d
- d
!= 0) {
2963 /* NaN or infinity */
2964 monitor_printf(mon
, "Bad number\n");
2973 * Store the command-name in cmdname, and return a pointer to
2974 * the remaining of the command string.
2976 static const char *get_command_name(const char *cmdline
,
2977 char *cmdname
, size_t nlen
)
2980 const char *p
, *pstart
;
2983 while (qemu_isspace(*p
))
2988 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2993 memcpy(cmdname
, pstart
, len
);
2994 cmdname
[len
] = '\0';
2999 * Read key of 'type' into 'key' and return the current
3002 static char *key_get_info(const char *type
, char **key
)
3010 p
= strchr(type
, ':');
3017 str
= g_malloc(len
+ 1);
3018 memcpy(str
, type
, len
);
3025 static int default_fmt_format
= 'x';
3026 static int default_fmt_size
= 4;
3028 static int is_valid_option(const char *c
, const char *typestr
)
3036 typestr
= strstr(typestr
, option
);
3037 return (typestr
!= NULL
);
3040 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
3041 const char *cmdname
)
3043 const HMPCommand
*cmd
;
3045 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3046 if (compare_cmd(cmdname
, cmd
->name
)) {
3055 * Parse command name from @cmdp according to command table @table.
3056 * If blank, return NULL.
3057 * Else, if no valid command can be found, report to @mon, and return
3059 * Else, change @cmdp to point right behind the name, and return its
3060 * command table entry.
3061 * Do not assume the return value points into @table! It doesn't when
3062 * the command is found in a sub-command table.
3064 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
3065 const char *cmdp_start
,
3069 Monitor
*mon
= &hmp_mon
->common
;
3071 const HMPCommand
*cmd
;
3074 /* extract the command name */
3075 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
3079 cmd
= search_dispatch_table(table
, cmdname
);
3081 monitor_printf(mon
, "unknown command: '%.*s'\n",
3082 (int)(p
- cmdp_start
), cmdp_start
);
3085 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
3086 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
3087 "until after exit_preconfig.\n",
3088 (int)(p
- cmdp_start
), cmdp_start
);
3092 /* filter out following useless space */
3093 while (qemu_isspace(*p
)) {
3098 /* search sub command */
3099 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
3100 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
3107 * Parse arguments for @cmd.
3108 * If it can't be parsed, report to @mon, and return NULL.
3109 * Else, insert command arguments into a QDict, and return it.
3110 * Note: On success, caller has to free the QDict structure.
3113 static QDict
*monitor_parse_arguments(Monitor
*mon
,
3115 const HMPCommand
*cmd
)
3117 const char *typestr
;
3120 const char *p
= *endp
;
3122 QDict
*qdict
= qdict_new();
3124 /* parse the parameters */
3125 typestr
= cmd
->args_type
;
3127 typestr
= key_get_info(typestr
, &key
);
3139 while (qemu_isspace(*p
))
3141 if (*typestr
== '?') {
3144 /* no optional string: NULL argument */
3148 ret
= get_str(buf
, sizeof(buf
), &p
);
3152 monitor_printf(mon
, "%s: filename expected\n",
3156 monitor_printf(mon
, "%s: block device name expected\n",
3160 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
3165 qdict_put_str(qdict
, key
, buf
);
3170 QemuOptsList
*opts_list
;
3173 opts_list
= qemu_find_opts(key
);
3174 if (!opts_list
|| opts_list
->desc
->name
) {
3177 while (qemu_isspace(*p
)) {
3182 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3185 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
3189 qemu_opts_to_qdict(opts
, qdict
);
3190 qemu_opts_del(opts
);
3195 int count
, format
, size
;
3197 while (qemu_isspace(*p
))
3203 if (qemu_isdigit(*p
)) {
3205 while (qemu_isdigit(*p
)) {
3206 count
= count
* 10 + (*p
- '0');
3244 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3245 monitor_printf(mon
, "invalid char in format: '%c'\n",
3250 format
= default_fmt_format
;
3251 if (format
!= 'i') {
3252 /* for 'i', not specifying a size gives -1 as size */
3254 size
= default_fmt_size
;
3255 default_fmt_size
= size
;
3257 default_fmt_format
= format
;
3260 format
= default_fmt_format
;
3261 if (format
!= 'i') {
3262 size
= default_fmt_size
;
3267 qdict_put_int(qdict
, "count", count
);
3268 qdict_put_int(qdict
, "format", format
);
3269 qdict_put_int(qdict
, "size", size
);
3278 while (qemu_isspace(*p
))
3280 if (*typestr
== '?' || *typestr
== '.') {
3281 if (*typestr
== '?') {
3289 while (qemu_isspace(*p
))
3298 if (get_expr(mon
, &val
, &p
))
3300 /* Check if 'i' is greater than 32-bit */
3301 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3302 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3303 monitor_printf(mon
, "integer is for 32-bit values\n");
3305 } else if (c
== 'M') {
3307 monitor_printf(mon
, "enter a positive value\n");
3312 qdict_put_int(qdict
, key
, val
);
3321 while (qemu_isspace(*p
)) {
3324 if (*typestr
== '?') {
3330 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
3331 if (ret
< 0 || val
> INT64_MAX
) {
3332 monitor_printf(mon
, "invalid size\n");
3335 qdict_put_int(qdict
, key
, val
);
3343 while (qemu_isspace(*p
))
3345 if (*typestr
== '?') {
3351 if (get_double(mon
, &val
, &p
) < 0) {
3354 if (p
[0] && p
[1] == 's') {
3357 val
/= 1e3
; p
+= 2; break;
3359 val
/= 1e6
; p
+= 2; break;
3361 val
/= 1e9
; p
+= 2; break;
3364 if (*p
&& !qemu_isspace(*p
)) {
3365 monitor_printf(mon
, "Unknown unit suffix\n");
3368 qdict_put(qdict
, key
, qnum_from_double(val
));
3376 while (qemu_isspace(*p
)) {
3380 while (qemu_isgraph(*p
)) {
3383 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3385 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3388 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3391 qdict_put_bool(qdict
, key
, val
);
3396 const char *tmp
= p
;
3403 while (qemu_isspace(*p
))
3408 if(!is_valid_option(p
, typestr
)) {
3410 monitor_printf(mon
, "%s: unsupported option -%c\n",
3422 qdict_put_bool(qdict
, key
, true);
3429 /* package all remaining string */
3432 while (qemu_isspace(*p
)) {
3435 if (*typestr
== '?') {
3438 /* no remaining string: NULL argument */
3444 monitor_printf(mon
, "%s: string expected\n",
3448 qdict_put_str(qdict
, key
, p
);
3454 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3460 /* check that all arguments were parsed */
3461 while (qemu_isspace(*p
))
3464 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3472 qobject_unref(qdict
);
3477 static void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
3480 const HMPCommand
*cmd
;
3481 const char *cmd_start
= cmdline
;
3483 trace_handle_hmp_command(mon
, cmdline
);
3485 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
3490 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
3492 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
3495 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
3496 (int)(cmdline
- cmd_start
), cmd_start
);
3500 cmd
->cmd(&mon
->common
, qdict
);
3501 qobject_unref(qdict
);
3504 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
3506 const char *p
, *pstart
;
3513 p
= qemu_strchrnul(p
, '|');
3515 if (len
> sizeof(cmd
) - 2)
3516 len
= sizeof(cmd
) - 2;
3517 memcpy(cmd
, pstart
, len
);
3519 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3520 readline_add_completion(mon
->rs
, cmd
);
3528 static void file_completion(MonitorHMP
*mon
, const char *input
)
3533 char file
[1024], file_prefix
[1024];
3537 p
= strrchr(input
, '/');
3540 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3541 pstrcpy(path
, sizeof(path
), ".");
3543 input_path_len
= p
- input
+ 1;
3544 memcpy(path
, input
, input_path_len
);
3545 if (input_path_len
> sizeof(path
) - 1)
3546 input_path_len
= sizeof(path
) - 1;
3547 path
[input_path_len
] = '\0';
3548 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3551 ffs
= opendir(path
);
3560 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3564 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3565 memcpy(file
, input
, input_path_len
);
3566 if (input_path_len
< sizeof(file
))
3567 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3569 /* stat the file to find out if it's a directory.
3570 * In that case add a slash to speed up typing long paths
3572 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3573 pstrcat(file
, sizeof(file
), "/");
3575 readline_add_completion(mon
->rs
, file
);
3581 static const char *next_arg_type(const char *typestr
)
3583 const char *p
= strchr(typestr
, ':');
3584 return (p
!= NULL
? ++p
: typestr
);
3587 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3590 if (!str
|| !option
) {
3593 if (!strncmp(option
, str
, strlen(str
))) {
3594 readline_add_completion(rs
, option
);
3598 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3601 ChardevBackendInfoList
*list
, *start
;
3607 readline_set_completion_index(rs
, len
);
3609 start
= list
= qmp_query_chardev_backends(NULL
);
3611 const char *chr_name
= list
->value
->name
;
3613 if (!strncmp(chr_name
, str
, len
)) {
3614 readline_add_completion(rs
, chr_name
);
3618 qapi_free_ChardevBackendInfoList(start
);
3621 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3630 readline_set_completion_index(rs
, len
);
3631 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
3632 add_completion_option(rs
, str
, NetClientDriver_str(i
));
3636 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3646 readline_set_completion_index(rs
, len
);
3647 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3650 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3652 name
= object_class_get_name(OBJECT_CLASS(dc
));
3654 if (dc
->user_creatable
3655 && !strncmp(name
, str
, len
)) {
3656 readline_add_completion(rs
, name
);
3663 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3673 readline_set_completion_index(rs
, len
);
3674 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3678 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3679 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3680 readline_add_completion(rs
, name
);
3687 static void peripheral_device_del_completion(ReadLineState
*rs
,
3688 const char *str
, size_t len
)
3690 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3691 GSList
*list
, *item
;
3693 list
= qdev_build_hotpluggable_device_list(peripheral
);
3698 for (item
= list
; item
; item
= g_slist_next(item
)) {
3699 DeviceState
*dev
= item
->data
;
3701 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3702 readline_add_completion(rs
, dev
->id
);
3709 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3712 ChardevInfoList
*list
, *start
;
3718 readline_set_completion_index(rs
, len
);
3720 start
= list
= qmp_query_chardev(NULL
);
3722 ChardevInfo
*chr
= list
->value
;
3724 if (!strncmp(chr
->label
, str
, len
)) {
3725 readline_add_completion(rs
, chr
->label
);
3729 qapi_free_ChardevInfoList(start
);
3732 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3735 ChardevInfoList
*list
, *start
;
3738 readline_set_completion_index(rs
, len
);
3740 start
= list
= qmp_query_chardev(NULL
);
3742 ChardevInfo
*chr_info
= list
->value
;
3744 if (!strncmp(chr_info
->label
, str
, len
)) {
3745 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
3746 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
3747 readline_add_completion(rs
, chr_info
->label
);
3752 qapi_free_ChardevInfoList(start
);
3755 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3760 ringbuf_completion(rs
, str
);
3763 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3772 readline_set_completion_index(rs
, len
);
3773 peripheral_device_del_completion(rs
, str
, len
);
3776 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3778 ObjectPropertyInfoList
*list
, *start
;
3785 readline_set_completion_index(rs
, len
);
3787 start
= list
= qmp_qom_list("/objects", NULL
);
3789 ObjectPropertyInfo
*info
= list
->value
;
3791 if (!strncmp(info
->type
, "child<", 5)
3792 && !strncmp(info
->name
, str
, len
)) {
3793 readline_add_completion(rs
, info
->name
);
3797 qapi_free_ObjectPropertyInfoList(start
);
3800 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3809 sep
= strrchr(str
, '-');
3814 readline_set_completion_index(rs
, len
);
3815 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3816 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
3817 readline_add_completion(rs
, QKeyCode_str(i
));
3822 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3827 readline_set_completion_index(rs
, len
);
3829 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3831 count
= qemu_find_net_clients_except(NULL
, ncs
,
3832 NET_CLIENT_DRIVER_NONE
,
3834 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3835 const char *name
= ncs
[i
]->name
;
3836 if (!strncmp(str
, name
, len
)) {
3837 readline_add_completion(rs
, name
);
3840 } else if (nb_args
== 3) {
3841 add_completion_option(rs
, str
, "on");
3842 add_completion_option(rs
, str
, "off");
3846 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3849 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3856 readline_set_completion_index(rs
, len
);
3857 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3859 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3861 const char *name
= ncs
[i
]->name
;
3862 if (strncmp(str
, name
, len
)) {
3865 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3867 readline_add_completion(rs
, name
);
3872 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3877 readline_set_completion_index(rs
, len
);
3879 TraceEventIter iter
;
3881 char *pattern
= g_strdup_printf("%s*", str
);
3882 trace_event_iter_init(&iter
, pattern
);
3883 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3884 readline_add_completion(rs
, trace_event_get_name(ev
));
3890 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3895 readline_set_completion_index(rs
, len
);
3897 TraceEventIter iter
;
3899 char *pattern
= g_strdup_printf("%s*", str
);
3900 trace_event_iter_init(&iter
, pattern
);
3901 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3902 readline_add_completion(rs
, trace_event_get_name(ev
));
3905 } else if (nb_args
== 3) {
3906 add_completion_option(rs
, str
, "on");
3907 add_completion_option(rs
, str
, "off");
3911 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3918 readline_set_completion_index(rs
, strlen(str
));
3919 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
3920 add_completion_option(rs
, str
, WatchdogAction_str(i
));
3924 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3930 readline_set_completion_index(rs
, len
);
3933 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3934 const char *name
= MigrationCapability_str(i
);
3935 if (!strncmp(str
, name
, len
)) {
3936 readline_add_completion(rs
, name
);
3939 } else if (nb_args
== 3) {
3940 add_completion_option(rs
, str
, "on");
3941 add_completion_option(rs
, str
, "off");
3945 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3951 readline_set_completion_index(rs
, len
);
3954 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3955 const char *name
= MigrationParameter_str(i
);
3956 if (!strncmp(str
, name
, len
)) {
3957 readline_add_completion(rs
, name
);
3963 static void vm_completion(ReadLineState
*rs
, const char *str
)
3966 BlockDriverState
*bs
;
3967 BdrvNextIterator it
;
3970 readline_set_completion_index(rs
, len
);
3972 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3973 SnapshotInfoList
*snapshots
, *snapshot
;
3974 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3977 aio_context_acquire(ctx
);
3978 if (bdrv_can_snapshot(bs
)) {
3979 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3981 aio_context_release(ctx
);
3986 snapshot
= snapshots
;
3988 char *completion
= snapshot
->value
->name
;
3989 if (!strncmp(str
, completion
, len
)) {
3990 readline_add_completion(rs
, completion
);
3992 completion
= snapshot
->value
->id
;
3993 if (!strncmp(str
, completion
, len
)) {
3994 readline_add_completion(rs
, completion
);
3996 snapshot
= snapshot
->next
;
3998 qapi_free_SnapshotInfoList(snapshots
);
4003 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4006 vm_completion(rs
, str
);
4010 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4013 vm_completion(rs
, str
);
4017 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
4018 const HMPCommand
*cmd_table
,
4022 const char *cmdname
;
4024 const char *ptype
, *old_ptype
, *str
, *name
;
4025 const HMPCommand
*cmd
;
4026 BlockBackend
*blk
= NULL
;
4029 /* command completion */
4034 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4035 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4036 if (!runstate_check(RUN_STATE_PRECONFIG
) ||
4037 cmd_can_preconfig(cmd
)) {
4038 cmd_completion(mon
, cmdname
, cmd
->name
);
4042 /* find the command */
4043 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4044 if (compare_cmd(args
[0], cmd
->name
) &&
4045 (!runstate_check(RUN_STATE_PRECONFIG
) ||
4046 cmd_can_preconfig(cmd
))) {
4054 if (cmd
->sub_table
) {
4055 /* do the job again */
4056 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4057 &args
[1], nb_args
- 1);
4060 if (cmd
->command_completion
) {
4061 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4065 ptype
= next_arg_type(cmd
->args_type
);
4066 for(i
= 0; i
< nb_args
- 2; i
++) {
4067 if (*ptype
!= '\0') {
4068 ptype
= next_arg_type(ptype
);
4069 while (*ptype
== '?')
4070 ptype
= next_arg_type(ptype
);
4073 str
= args
[nb_args
- 1];
4075 while (*ptype
== '-' && old_ptype
!= ptype
) {
4077 ptype
= next_arg_type(ptype
);
4081 /* file completion */
4082 readline_set_completion_index(mon
->rs
, strlen(str
));
4083 file_completion(mon
, str
);
4086 /* block device name completion */
4087 readline_set_completion_index(mon
->rs
, strlen(str
));
4088 while ((blk
= blk_next(blk
)) != NULL
) {
4089 name
= blk_name(blk
);
4090 if (str
[0] == '\0' ||
4091 !strncmp(name
, str
, strlen(str
))) {
4092 readline_add_completion(mon
->rs
, name
);
4098 if (!strcmp(cmd
->name
, "help|?")) {
4099 monitor_find_completion_by_table(mon
, cmd_table
,
4100 &args
[1], nb_args
- 1);
4109 static void monitor_find_completion(void *opaque
,
4110 const char *cmdline
)
4112 MonitorHMP
*mon
= opaque
;
4113 char *args
[MAX_ARGS
];
4116 /* 1. parse the cmdline */
4117 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4121 /* if the line ends with a space, it means we want to complete the
4123 len
= strlen(cmdline
);
4124 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4125 if (nb_args
>= MAX_ARGS
) {
4128 args
[nb_args
++] = g_strdup("");
4131 /* 2. auto complete according to args */
4132 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
4135 free_cmdline_args(args
, nb_args
);
4138 static int monitor_can_read(void *opaque
)
4140 Monitor
*mon
= opaque
;
4142 return !atomic_mb_read(&mon
->suspend_cnt
);
4146 * Emit QMP response @rsp with ID @id to @mon.
4147 * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
4148 * Nothing is emitted then.
4150 static void monitor_qmp_respond(MonitorQMP
*mon
, QDict
*rsp
)
4153 qmp_send_response(mon
, rsp
);
4157 static void monitor_qmp_dispatch(MonitorQMP
*mon
, QObject
*req
)
4164 cur_mon
= &mon
->common
;
4166 rsp
= qmp_dispatch(mon
->commands
, req
, qmp_oob_enabled(mon
));
4170 if (mon
->commands
== &qmp_cap_negotiation_commands
) {
4171 error
= qdict_get_qdict(rsp
, "error");
4173 && !g_strcmp0(qdict_get_try_str(error
, "class"),
4174 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND
))) {
4175 /* Provide a more useful error message */
4176 qdict_del(error
, "desc");
4177 qdict_put_str(error
, "desc", "Expecting capabilities negotiation"
4178 " with 'qmp_capabilities'");
4182 monitor_qmp_respond(mon
, rsp
);
4187 * Pop a QMP request from a monitor request queue.
4188 * Return the request, or NULL all request queues are empty.
4189 * We are using round-robin fashion to pop the request, to avoid
4190 * processing commands only on a very busy monitor. To achieve that,
4191 * when we process one request on a specific monitor, we put that
4192 * monitor to the end of mon_list queue.
4194 * Note: if the function returned with non-NULL, then the caller will
4195 * be with qmp_mon->qmp_queue_lock held, and the caller is responsible
4198 static QMPRequest
*monitor_qmp_requests_pop_any_with_lock(void)
4200 QMPRequest
*req_obj
= NULL
;
4202 MonitorQMP
*qmp_mon
;
4204 qemu_mutex_lock(&monitor_lock
);
4206 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
4207 if (!monitor_is_qmp(mon
)) {
4211 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
4212 qemu_mutex_lock(&qmp_mon
->qmp_queue_lock
);
4213 req_obj
= g_queue_pop_head(qmp_mon
->qmp_requests
);
4215 /* With the lock of corresponding queue held */
4218 qemu_mutex_unlock(&qmp_mon
->qmp_queue_lock
);
4223 * We found one request on the monitor. Degrade this monitor's
4224 * priority to lowest by re-inserting it to end of queue.
4226 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4227 QTAILQ_INSERT_TAIL(&mon_list
, mon
, entry
);
4230 qemu_mutex_unlock(&monitor_lock
);
4235 static void monitor_qmp_bh_dispatcher(void *data
)
4237 QMPRequest
*req_obj
= monitor_qmp_requests_pop_any_with_lock();
4247 /* qmp_oob_enabled() might change after "qmp_capabilities" */
4248 need_resume
= !qmp_oob_enabled(mon
) ||
4249 mon
->qmp_requests
->length
== QMP_REQ_QUEUE_LEN_MAX
- 1;
4250 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
4252 QDict
*qdict
= qobject_to(QDict
, req_obj
->req
);
4253 QObject
*id
= qdict
? qdict_get(qdict
, "id") : NULL
;
4254 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id
) ?: "");
4255 monitor_qmp_dispatch(mon
, req_obj
->req
);
4257 assert(req_obj
->err
);
4258 rsp
= qmp_error_response(req_obj
->err
);
4259 req_obj
->err
= NULL
;
4260 monitor_qmp_respond(mon
, rsp
);
4265 /* Pairs with the monitor_suspend() in handle_qmp_command() */
4266 monitor_resume(&mon
->common
);
4268 qmp_request_free(req_obj
);
4270 /* Reschedule instead of looping so the main loop stays responsive */
4271 qemu_bh_schedule(qmp_dispatcher_bh
);
4274 static void handle_qmp_command(void *opaque
, QObject
*req
, Error
*err
)
4276 MonitorQMP
*mon
= opaque
;
4279 QMPRequest
*req_obj
;
4281 assert(!req
!= !err
);
4283 qdict
= qobject_to(QDict
, req
);
4285 id
= qdict_get(qdict
, "id");
4286 } /* else will fail qmp_dispatch() */
4288 if (req
&& trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND
)) {
4289 QString
*req_json
= qobject_to_json(req
);
4290 trace_handle_qmp_command(mon
, qstring_get_str(req_json
));
4291 qobject_unref(req_json
);
4294 if (qdict
&& qmp_is_oob(qdict
)) {
4295 /* OOB commands are executed immediately */
4296 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id
) ?: "");
4297 monitor_qmp_dispatch(mon
, req
);
4302 req_obj
= g_new0(QMPRequest
, 1);
4307 /* Protect qmp_requests and fetching its length. */
4308 qemu_mutex_lock(&mon
->qmp_queue_lock
);
4311 * Suspend the monitor when we can't queue more requests after
4312 * this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume
4313 * it. Note that when OOB is disabled, we queue at most one
4314 * command, for backward compatibility.
4316 if (!qmp_oob_enabled(mon
) ||
4317 mon
->qmp_requests
->length
== QMP_REQ_QUEUE_LEN_MAX
- 1) {
4318 monitor_suspend(&mon
->common
);
4322 * Put the request to the end of queue so that requests will be
4323 * handled in time order. Ownership for req_obj, req,
4324 * etc. will be delivered to the handler side.
4326 assert(mon
->qmp_requests
->length
< QMP_REQ_QUEUE_LEN_MAX
);
4327 g_queue_push_tail(mon
->qmp_requests
, req_obj
);
4328 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
4330 /* Kick the dispatcher routine */
4331 qemu_bh_schedule(qmp_dispatcher_bh
);
4334 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4336 MonitorQMP
*mon
= opaque
;
4338 json_message_parser_feed(&mon
->parser
, (const char *) buf
, size
);
4341 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4344 Monitor
*old_mon
= cur_mon
;
4348 mon
= container_of(cur_mon
, MonitorHMP
, common
);
4351 for (i
= 0; i
< size
; i
++)
4352 readline_handle_byte(mon
->rs
, buf
[i
]);
4354 if (size
== 0 || buf
[size
- 1] != 0)
4355 monitor_printf(cur_mon
, "corrupted command\n");
4357 handle_hmp_command(mon
, (char *)buf
);
4363 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4364 void *readline_opaque
)
4366 MonitorHMP
*mon
= opaque
;
4368 monitor_suspend(&mon
->common
);
4369 handle_hmp_command(mon
, cmdline
);
4370 monitor_resume(&mon
->common
);
4373 int monitor_suspend(Monitor
*mon
)
4375 if (monitor_is_hmp_non_interactive(mon
)) {
4379 atomic_inc(&mon
->suspend_cnt
);
4381 if (mon
->use_io_thread
) {
4383 * Kick I/O thread to make sure this takes effect. It'll be
4384 * evaluated again in prepare() of the watch object.
4386 aio_notify(iothread_get_aio_context(mon_iothread
));
4389 trace_monitor_suspend(mon
, 1);
4393 static void monitor_accept_input(void *opaque
)
4395 Monitor
*mon
= opaque
;
4397 qemu_chr_fe_accept_input(&mon
->chr
);
4400 void monitor_resume(Monitor
*mon
)
4402 if (monitor_is_hmp_non_interactive(mon
)) {
4406 if (atomic_dec_fetch(&mon
->suspend_cnt
) == 0) {
4409 if (mon
->use_io_thread
) {
4410 ctx
= iothread_get_aio_context(mon_iothread
);
4412 ctx
= qemu_get_aio_context();
4415 if (!monitor_is_qmp(mon
)) {
4416 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
4417 assert(hmp_mon
->rs
);
4418 readline_show_prompt(hmp_mon
->rs
);
4421 aio_bh_schedule_oneshot(ctx
, monitor_accept_input
, mon
);
4424 trace_monitor_suspend(mon
, -1);
4427 static QDict
*qmp_greeting(MonitorQMP
*mon
)
4429 QList
*cap_list
= qlist_new();
4430 QObject
*ver
= NULL
;
4433 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4435 for (cap
= 0; cap
< QMP_CAPABILITY__MAX
; cap
++) {
4436 if (mon
->capab_offered
[cap
]) {
4437 qlist_append_str(cap_list
, QMPCapability_str(cap
));
4441 return qdict_from_jsonf_nofail(
4442 "{'QMP': {'version': %p, 'capabilities': %p}}",
4446 static void monitor_qmp_event(void *opaque
, int event
)
4449 MonitorQMP
*mon
= opaque
;
4452 case CHR_EVENT_OPENED
:
4453 mon
->commands
= &qmp_cap_negotiation_commands
;
4454 monitor_qmp_caps_reset(mon
);
4455 data
= qmp_greeting(mon
);
4456 qmp_send_response(mon
, data
);
4457 qobject_unref(data
);
4460 case CHR_EVENT_CLOSED
:
4462 * Note: this is only useful when the output of the chardev
4463 * backend is still open. For example, when the backend is
4464 * stdio, it's possible that stdout is still open when stdin
4467 monitor_qmp_cleanup_queues(mon
);
4468 json_message_parser_destroy(&mon
->parser
);
4469 json_message_parser_init(&mon
->parser
, handle_qmp_command
,
4472 monitor_fdsets_cleanup();
4477 static void monitor_event(void *opaque
, int event
)
4479 Monitor
*mon
= opaque
;
4480 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
4483 case CHR_EVENT_MUX_IN
:
4484 qemu_mutex_lock(&mon
->mon_lock
);
4486 qemu_mutex_unlock(&mon
->mon_lock
);
4487 if (mon
->reset_seen
) {
4488 readline_restart(hmp_mon
->rs
);
4489 monitor_resume(mon
);
4492 atomic_mb_set(&mon
->suspend_cnt
, 0);
4496 case CHR_EVENT_MUX_OUT
:
4497 if (mon
->reset_seen
) {
4498 if (atomic_mb_read(&mon
->suspend_cnt
) == 0) {
4499 monitor_printf(mon
, "\n");
4502 monitor_suspend(mon
);
4504 atomic_inc(&mon
->suspend_cnt
);
4506 qemu_mutex_lock(&mon
->mon_lock
);
4508 qemu_mutex_unlock(&mon
->mon_lock
);
4511 case CHR_EVENT_OPENED
:
4512 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4513 "information\n", QEMU_VERSION
);
4514 if (!mon
->mux_out
) {
4515 readline_restart(hmp_mon
->rs
);
4516 readline_show_prompt(hmp_mon
->rs
);
4518 mon
->reset_seen
= 1;
4522 case CHR_EVENT_CLOSED
:
4524 monitor_fdsets_cleanup();
4530 compare_mon_cmd(const void *a
, const void *b
)
4532 return strcmp(((const HMPCommand
*)a
)->name
,
4533 ((const HMPCommand
*)b
)->name
);
4536 static void sortcmdlist(void)
4538 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
4541 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
4542 sizeof(*hmp_info_cmds
),
4546 static void monitor_iothread_init(void)
4548 mon_iothread
= iothread_create("mon_iothread", &error_abort
);
4551 void monitor_init_globals(void)
4553 monitor_init_qmp_commands();
4554 monitor_qapi_event_init();
4556 qemu_mutex_init(&monitor_lock
);
4557 qemu_mutex_init(&mon_fdsets_lock
);
4560 * The dispatcher BH must run in the main loop thread, since we
4561 * have commands assuming that context. It would be nice to get
4562 * rid of those assumptions.
4564 qmp_dispatcher_bh
= aio_bh_new(iohandler_get_aio_context(),
4565 monitor_qmp_bh_dispatcher
,
4569 /* These functions just adapt the readline interface in a typesafe way. We
4570 * could cast function pointers but that discards compiler checks.
4572 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4573 const char *fmt
, ...)
4575 MonitorHMP
*mon
= opaque
;
4578 monitor_vprintf(&mon
->common
, fmt
, ap
);
4582 static void monitor_readline_flush(void *opaque
)
4584 MonitorHMP
*mon
= opaque
;
4585 monitor_flush(&mon
->common
);
4589 * Print to current monitor if we have one, else to stderr.
4591 int error_vprintf(const char *fmt
, va_list ap
)
4593 if (cur_mon
&& !monitor_cur_is_qmp()) {
4594 return monitor_vprintf(cur_mon
, fmt
, ap
);
4596 return vfprintf(stderr
, fmt
, ap
);
4599 int error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
4602 return vfprintf(stderr
, fmt
, ap
);
4604 if (!monitor_cur_is_qmp()) {
4605 return monitor_vprintf(cur_mon
, fmt
, ap
);
4610 static void monitor_list_append(Monitor
*mon
)
4612 qemu_mutex_lock(&monitor_lock
);
4614 * This prevents inserting new monitors during monitor_cleanup().
4615 * A cleaner solution would involve the main thread telling other
4616 * threads to terminate, waiting for their termination.
4618 if (!monitor_destroyed
) {
4619 QTAILQ_INSERT_HEAD(&mon_list
, mon
, entry
);
4622 qemu_mutex_unlock(&monitor_lock
);
4625 monitor_data_destroy(mon
);
4630 static void monitor_qmp_setup_handlers_bh(void *opaque
)
4632 MonitorQMP
*mon
= opaque
;
4633 GMainContext
*context
;
4635 assert(mon
->common
.use_io_thread
);
4636 context
= iothread_get_g_main_context(mon_iothread
);
4638 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
,
4639 monitor_qmp_read
, monitor_qmp_event
,
4640 NULL
, &mon
->common
, context
, true);
4641 monitor_list_append(&mon
->common
);
4644 static void monitor_init_qmp(Chardev
*chr
, int flags
)
4646 MonitorQMP
*mon
= g_new0(MonitorQMP
, 1);
4648 /* Only HMP supports readline */
4649 assert(!(flags
& MONITOR_USE_READLINE
));
4651 /* Note: we run QMP monitor in I/O thread when @chr supports that */
4652 monitor_data_init(&mon
->common
, flags
, false,
4653 qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
));
4655 qemu_mutex_init(&mon
->qmp_queue_lock
);
4656 mon
->qmp_requests
= g_queue_new();
4658 qemu_chr_fe_init(&mon
->common
.chr
, chr
, &error_abort
);
4659 qemu_chr_fe_set_echo(&mon
->common
.chr
, true);
4661 json_message_parser_init(&mon
->parser
, handle_qmp_command
, mon
, NULL
);
4662 if (mon
->common
.use_io_thread
) {
4664 * Make sure the old iowatch is gone. It's possible when
4665 * e.g. the chardev is in client mode, with wait=on.
4667 remove_fd_in_watch(chr
);
4669 * We can't call qemu_chr_fe_set_handlers() directly here
4670 * since chardev might be running in the monitor I/O
4671 * thread. Schedule a bottom half.
4673 aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread
),
4674 monitor_qmp_setup_handlers_bh
, mon
);
4675 /* The bottom half will add @mon to @mon_list */
4677 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
,
4678 monitor_qmp_read
, monitor_qmp_event
,
4679 NULL
, &mon
->common
, NULL
, true);
4680 monitor_list_append(&mon
->common
);
4684 static void monitor_init_hmp(Chardev
*chr
, int flags
)
4686 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
4687 bool use_readline
= flags
& MONITOR_USE_READLINE
;
4689 monitor_data_init(&mon
->common
, flags
, false, false);
4690 qemu_chr_fe_init(&mon
->common
.chr
, chr
, &error_abort
);
4693 mon
->rs
= readline_init(monitor_readline_printf
,
4694 monitor_readline_flush
,
4696 monitor_find_completion
);
4697 monitor_read_command(mon
, 0);
4700 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
4701 monitor_event
, NULL
, &mon
->common
, NULL
, true);
4702 monitor_list_append(&mon
->common
);
4705 void monitor_init(Chardev
*chr
, int flags
)
4707 if (flags
& MONITOR_USE_CONTROL
) {
4708 monitor_init_qmp(chr
, flags
);
4710 monitor_init_hmp(chr
, flags
);
4714 void monitor_cleanup(void)
4717 * We need to explicitly stop the I/O thread (but not destroy it),
4718 * clean up the monitor resources, then destroy the I/O thread since
4719 * we need to unregister from chardev below in
4720 * monitor_data_destroy(), and chardev is not thread-safe yet
4723 iothread_stop(mon_iothread
);
4726 /* Flush output buffers and destroy monitors */
4727 qemu_mutex_lock(&monitor_lock
);
4728 monitor_destroyed
= true;
4729 while (!QTAILQ_EMPTY(&mon_list
)) {
4730 Monitor
*mon
= QTAILQ_FIRST(&mon_list
);
4731 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4732 /* Permit QAPI event emission from character frontend release */
4733 qemu_mutex_unlock(&monitor_lock
);
4735 monitor_data_destroy(mon
);
4736 qemu_mutex_lock(&monitor_lock
);
4739 qemu_mutex_unlock(&monitor_lock
);
4741 /* QEMUBHs needs to be deleted before destroying the I/O thread */
4742 qemu_bh_delete(qmp_dispatcher_bh
);
4743 qmp_dispatcher_bh
= NULL
;
4745 iothread_destroy(mon_iothread
);
4746 mon_iothread
= NULL
;
4750 QemuOptsList qemu_mon_opts
= {
4752 .implied_opt_name
= "chardev",
4753 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4757 .type
= QEMU_OPT_STRING
,
4760 .type
= QEMU_OPT_STRING
,
4763 .type
= QEMU_OPT_BOOL
,
4765 { /* end of list */ }
4769 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4771 MachineState
*ms
= MACHINE(qdev_get_machine());
4772 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4774 if (!mc
->has_hotpluggable_cpus
) {
4775 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4779 return machine_query_hotpluggable_cpus(ms
);