4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
26 #include "qemu/units.h"
30 #include "monitor/qdev.h"
32 #include "hw/pci/pci.h"
33 #include "sysemu/watchdog.h"
34 #include "hw/loader.h"
35 #include "exec/gdbstub.h"
37 #include "net/slirp.h"
38 #include "chardev/char-fe.h"
39 #include "chardev/char-io.h"
40 #include "chardev/char-mux.h"
41 #include "ui/qemu-spice.h"
42 #include "sysemu/numa.h"
43 #include "monitor/monitor.h"
44 #include "qemu/config-file.h"
45 #include "qemu/ctype.h"
46 #include "qemu/readline.h"
47 #include "ui/console.h"
49 #include "sysemu/block-backend.h"
50 #include "audio/audio.h"
51 #include "disas/disas.h"
52 #include "sysemu/balloon.h"
53 #include "qemu/timer.h"
54 #include "sysemu/hw_accel.h"
55 #include "authz/list.h"
56 #include "qapi/util.h"
57 #include "sysemu/tcg.h"
58 #include "sysemu/tpm.h"
59 #include "qapi/qmp/qdict.h"
60 #include "qapi/qmp/qerror.h"
61 #include "qapi/qmp/qnum.h"
62 #include "qapi/qmp/qstring.h"
63 #include "qapi/qmp/qjson.h"
64 #include "qapi/qmp/json-parser.h"
65 #include "qapi/qmp/qlist.h"
66 #include "qom/object_interfaces.h"
67 #include "trace-root.h"
68 #include "trace/control.h"
69 #include "monitor/hmp-target.h"
70 #ifdef CONFIG_TRACE_SIMPLE
71 #include "trace/simple.h"
73 #include "exec/memory.h"
74 #include "exec/exec-all.h"
76 #include "qemu/option.h"
78 #include "qemu/thread.h"
79 #include "block/qapi.h"
80 #include "qapi/qapi-commands.h"
81 #include "qapi/qapi-emit-events.h"
82 #include "qapi/error.h"
83 #include "qapi/qmp-event.h"
84 #include "qapi/qapi-introspect.h"
85 #include "sysemu/qtest.h"
86 #include "sysemu/cpus.h"
87 #include "sysemu/iothread.h"
88 #include "qemu/cutils.h"
91 #if defined(TARGET_S390X)
92 #include "hw/s390x/storage-keys.h"
93 #include "hw/s390x/storage-attributes.h"
100 * 'B' block device name
101 * 's' string (accept optional quote)
102 * 'S' it just appends the rest of the string (accept optional quote)
103 * 'O' option string of the form NAME=VALUE,...
104 * parsed according to QemuOptsList given by its name
105 * Example: 'device:O' uses qemu_device_opts.
106 * Restriction: only lists with empty desc are supported
107 * TODO lift the restriction
109 * 'l' target long (32 or 64 bit)
110 * 'M' Non-negative target long (32 or 64 bit), in user mode the
111 * value is multiplied by 2^20 (think Mebibyte)
112 * 'o' octets (aka bytes)
113 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
114 * K, k suffix, which multiplies the value by 2^60 for suffixes E
115 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
116 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
118 * user mode accepts an optional ms, us, ns suffix,
119 * which divides the value by 1e3, 1e6, 1e9, respectively
120 * '/' optional gdb-like print format (like "/10x")
122 * '?' optional type (for all types, except '/')
123 * '.' other form of optional type (for 'i' and 'l')
125 * user mode accepts "on" or "off"
126 * '-' optional parameter (eg. '-f')
130 typedef struct mon_cmd_t
{
132 const char *args_type
;
135 const char *flags
; /* p=preconfig */
136 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
137 /* @sub_table is a list of 2nd level of commands. If it does not exist,
138 * cmd should be used. If it exists, sub_table[?].cmd should be
139 * used, and cmd of 1st level plays the role of help function.
141 struct mon_cmd_t
*sub_table
;
142 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
145 /* file descriptors passed via SCM_RIGHTS */
146 typedef struct mon_fd_t mon_fd_t
;
150 QLIST_ENTRY(mon_fd_t
) next
;
153 /* file descriptor associated with a file descriptor set */
154 typedef struct MonFdsetFd MonFdsetFd
;
159 QLIST_ENTRY(MonFdsetFd
) next
;
162 /* file descriptor set containing fds passed via SCM_RIGHTS */
163 typedef struct MonFdset MonFdset
;
166 QLIST_HEAD(, MonFdsetFd
) fds
;
167 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
168 QLIST_ENTRY(MonFdset
) next
;
172 * To prevent flooding clients, events can be throttled. The
173 * throttling is calculated globally, rather than per-Monitor
176 typedef struct MonitorQAPIEventState
{
177 QAPIEvent event
; /* Throttling state for this event type and... */
178 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
179 QEMUTimer
*timer
; /* Timer for handling delayed events */
180 QDict
*qdict
; /* Delayed event (if any) */
181 } MonitorQAPIEventState
;
184 int64_t rate
; /* Minimum time (in ns) between two events */
185 } MonitorQAPIEventConf
;
191 int suspend_cnt
; /* Needs to be accessed atomically */
196 mon_cmd_t
*cmd_table
;
197 QTAILQ_ENTRY(Monitor
) entry
;
200 * The per-monitor lock. We can't access guest memory when holding
206 * Members that are protected by the per-monitor lock
208 QLIST_HEAD(, mon_fd_t
) fds
;
211 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
218 * State used only in the thread "owning" the monitor.
219 * If @use_io_thread, this is @mon_iothread. (This does not actually happen
220 * in the current state of the code.)
221 * Else, it's the main thread.
222 * These members can be safely accessed without locks.
229 JSONMessageParser parser
;
231 * When a client connects, we're in capabilities negotiation mode.
232 * @commands is &qmp_cap_negotiation_commands then. When command
233 * qmp_capabilities succeeds, we go into command mode, and
234 * @command becomes &qmp_commands.
236 QmpCommandList
*commands
;
237 bool capab_offered
[QMP_CAPABILITY__MAX
]; /* capabilities offered */
238 bool capab
[QMP_CAPABILITY__MAX
]; /* offered and accepted */
240 * Protects qmp request/response queue.
241 * Take monitor_lock first when you need both.
243 QemuMutex qmp_queue_lock
;
244 /* Input queue that holds all the parsed QMP requests */
245 GQueue
*qmp_requests
;
248 /* Shared monitor I/O thread */
249 IOThread
*mon_iothread
;
251 /* Bottom half to dispatch the requests received from I/O thread */
252 QEMUBH
*qmp_dispatcher_bh
;
255 /* Owner of the request */
258 * Request object to be handled or Error to be reported
259 * (exactly one of them is non-null)
264 typedef struct QMPRequest QMPRequest
;
266 /* QMP checker flags */
267 #define QMP_ACCEPT_UNKNOWNS 1
269 /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
270 static QemuMutex monitor_lock
;
271 static GHashTable
*monitor_qapi_event_state
;
272 static QTAILQ_HEAD(, Monitor
) mon_list
;
273 static bool monitor_destroyed
;
275 /* Protects mon_fdsets */
276 static QemuMutex mon_fdsets_lock
;
277 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
279 static int mon_refcount
;
281 static mon_cmd_t mon_cmds
[];
282 static mon_cmd_t info_cmds
[];
284 QmpCommandList qmp_commands
, qmp_cap_negotiation_commands
;
286 __thread Monitor
*cur_mon
;
288 static void monitor_command_cb(void *opaque
, const char *cmdline
,
289 void *readline_opaque
);
292 * Is @mon a QMP monitor?
294 static inline bool monitor_is_qmp(const Monitor
*mon
)
296 return (mon
->flags
& MONITOR_USE_CONTROL
);
300 * Is @mon is using readline?
301 * Note: not all HMP monitors use readline, e.g., gdbserver has a
302 * non-interactive HMP monitor, so readline is not used there.
304 static inline bool monitor_uses_readline(const Monitor
*mon
)
306 return mon
->flags
& MONITOR_USE_READLINE
;
309 static inline bool monitor_is_hmp_non_interactive(const Monitor
*mon
)
311 return !monitor_is_qmp(mon
) && !monitor_uses_readline(mon
);
315 * Return the clock to use for recording an event's time.
316 * It's QEMU_CLOCK_REALTIME, except for qtests it's
317 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
318 * Beware: result is invalid before configure_accelerator().
320 static inline QEMUClockType
monitor_get_event_clock(void)
322 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL
: QEMU_CLOCK_REALTIME
;
326 * Is the current monitor, if any, a QMP monitor?
328 bool monitor_cur_is_qmp(void)
330 return cur_mon
&& monitor_is_qmp(cur_mon
);
333 void monitor_read_command(MonitorHMP
*mon
, int show_prompt
)
338 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
340 readline_show_prompt(mon
->rs
);
343 int monitor_read_password(MonitorHMP
*mon
, ReadLineFunc
*readline_func
,
347 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
348 /* prompt is printed on return from the command handler */
351 monitor_printf(&mon
->common
,
352 "terminal does not support password prompting\n");
357 static void qmp_request_free(QMPRequest
*req
)
359 qobject_unref(req
->req
);
360 error_free(req
->err
);
364 /* Caller must hold mon->qmp.qmp_queue_lock */
365 static void monitor_qmp_cleanup_req_queue_locked(MonitorQMP
*mon
)
367 while (!g_queue_is_empty(mon
->qmp_requests
)) {
368 qmp_request_free(g_queue_pop_head(mon
->qmp_requests
));
372 static void monitor_qmp_cleanup_queues(MonitorQMP
*mon
)
374 qemu_mutex_lock(&mon
->qmp_queue_lock
);
375 monitor_qmp_cleanup_req_queue_locked(mon
);
376 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
380 static void monitor_flush_locked(Monitor
*mon
);
382 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
385 Monitor
*mon
= opaque
;
387 qemu_mutex_lock(&mon
->mon_lock
);
389 monitor_flush_locked(mon
);
390 qemu_mutex_unlock(&mon
->mon_lock
);
394 /* Caller must hold mon->mon_lock */
395 static void monitor_flush_locked(Monitor
*mon
)
401 if (mon
->skip_flush
) {
405 buf
= qstring_get_str(mon
->outbuf
);
406 len
= qstring_get_length(mon
->outbuf
);
408 if (len
&& !mon
->mux_out
) {
409 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
410 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
411 /* all flushed or error */
412 qobject_unref(mon
->outbuf
);
413 mon
->outbuf
= qstring_new();
418 QString
*tmp
= qstring_from_str(buf
+ rc
);
419 qobject_unref(mon
->outbuf
);
422 if (mon
->out_watch
== 0) {
424 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
425 monitor_unblocked
, mon
);
430 void monitor_flush(Monitor
*mon
)
432 qemu_mutex_lock(&mon
->mon_lock
);
433 monitor_flush_locked(mon
);
434 qemu_mutex_unlock(&mon
->mon_lock
);
437 /* flush at every end of line */
438 static int monitor_puts(Monitor
*mon
, const char *str
)
443 qemu_mutex_lock(&mon
->mon_lock
);
444 for (i
= 0; str
[i
]; i
++) {
447 qstring_append_chr(mon
->outbuf
, '\r');
449 qstring_append_chr(mon
->outbuf
, c
);
451 monitor_flush_locked(mon
);
454 qemu_mutex_unlock(&mon
->mon_lock
);
459 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
467 if (monitor_is_qmp(mon
)) {
471 buf
= g_strdup_vprintf(fmt
, ap
);
472 n
= monitor_puts(mon
, buf
);
477 int monitor_printf(Monitor
*mon
, const char *fmt
, ...)
483 ret
= monitor_vprintf(mon
, fmt
, ap
);
488 static void qmp_send_response(MonitorQMP
*mon
, const QDict
*rsp
)
490 const QObject
*data
= QOBJECT(rsp
);
493 json
= mon
->common
.flags
& MONITOR_USE_PRETTY
?
494 qobject_to_json_pretty(data
) : qobject_to_json(data
);
495 assert(json
!= NULL
);
497 qstring_append_chr(json
, '\n');
498 monitor_puts(&mon
->common
, qstring_get_str(json
));
503 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
504 /* Limit guest-triggerable events to 1 per second */
505 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
506 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
507 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
508 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
509 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
510 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
514 * Broadcast an event to all monitors.
515 * @qdict is the event object. Its member "event" must match @event.
516 * Caller must hold monitor_lock.
518 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
523 trace_monitor_protocol_event_emit(event
, qdict
);
524 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
525 if (!monitor_is_qmp(mon
)) {
529 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
530 if (qmp_mon
->commands
!= &qmp_cap_negotiation_commands
) {
531 qmp_send_response(qmp_mon
, qdict
);
536 static void monitor_qapi_event_handler(void *opaque
);
539 * Queue a new event for emission to Monitor instances,
540 * applying any rate limiting if required.
543 monitor_qapi_event_queue_no_reenter(QAPIEvent event
, QDict
*qdict
)
545 MonitorQAPIEventConf
*evconf
;
546 MonitorQAPIEventState
*evstate
;
548 assert(event
< QAPI_EVENT__MAX
);
549 evconf
= &monitor_qapi_event_conf
[event
];
550 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
552 qemu_mutex_lock(&monitor_lock
);
555 /* Unthrottled event */
556 monitor_qapi_event_emit(event
, qdict
);
558 QDict
*data
= qobject_to(QDict
, qdict_get(qdict
, "data"));
559 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
561 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
562 assert(!evstate
|| timer_pending(evstate
->timer
));
566 * Timer is pending for (at least) evconf->rate ns after
567 * last send. Store event for sending when timer fires,
568 * replacing a prior stored event if any.
570 qobject_unref(evstate
->qdict
);
571 evstate
->qdict
= qobject_ref(qdict
);
574 * Last send was (at least) evconf->rate ns ago.
575 * Send immediately, and arm the timer to call
576 * monitor_qapi_event_handler() in evconf->rate ns. Any
577 * events arriving before then will be delayed until then.
579 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
581 monitor_qapi_event_emit(event
, qdict
);
583 evstate
= g_new(MonitorQAPIEventState
, 1);
584 evstate
->event
= event
;
585 evstate
->data
= qobject_ref(data
);
586 evstate
->qdict
= NULL
;
587 evstate
->timer
= timer_new_ns(monitor_get_event_clock(),
588 monitor_qapi_event_handler
,
590 g_hash_table_add(monitor_qapi_event_state
, evstate
);
591 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
595 qemu_mutex_unlock(&monitor_lock
);
598 void qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
601 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
602 * would deadlock on monitor_lock. Work around by queueing
603 * events in thread-local storage.
604 * TODO: remove this, make it re-enter safe.
606 typedef struct MonitorQapiEvent
{
609 QSIMPLEQ_ENTRY(MonitorQapiEvent
) entry
;
611 static __thread
QSIMPLEQ_HEAD(, MonitorQapiEvent
) event_queue
;
612 static __thread
bool reentered
;
613 MonitorQapiEvent
*ev
;
616 QSIMPLEQ_INIT(&event_queue
);
619 ev
= g_new(MonitorQapiEvent
, 1);
620 ev
->qdict
= qobject_ref(qdict
);
622 QSIMPLEQ_INSERT_TAIL(&event_queue
, ev
, entry
);
629 while ((ev
= QSIMPLEQ_FIRST(&event_queue
)) != NULL
) {
630 QSIMPLEQ_REMOVE_HEAD(&event_queue
, entry
);
631 monitor_qapi_event_queue_no_reenter(ev
->event
, ev
->qdict
);
632 qobject_unref(ev
->qdict
);
640 * This function runs evconf->rate ns after sending a throttled
642 * If another event has since been stored, send it.
644 static void monitor_qapi_event_handler(void *opaque
)
646 MonitorQAPIEventState
*evstate
= opaque
;
647 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
649 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
650 qemu_mutex_lock(&monitor_lock
);
652 if (evstate
->qdict
) {
653 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
655 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
656 qobject_unref(evstate
->qdict
);
657 evstate
->qdict
= NULL
;
658 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
660 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
661 qobject_unref(evstate
->data
);
662 timer_free(evstate
->timer
);
666 qemu_mutex_unlock(&monitor_lock
);
669 static unsigned int qapi_event_throttle_hash(const void *key
)
671 const MonitorQAPIEventState
*evstate
= key
;
672 unsigned int hash
= evstate
->event
* 255;
674 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
675 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
678 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
679 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
685 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
687 const MonitorQAPIEventState
*eva
= a
;
688 const MonitorQAPIEventState
*evb
= b
;
690 if (eva
->event
!= evb
->event
) {
694 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
695 return !strcmp(qdict_get_str(eva
->data
, "id"),
696 qdict_get_str(evb
->data
, "id"));
699 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
700 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
701 qdict_get_str(evb
->data
, "node-name"));
707 static void monitor_qapi_event_init(void)
709 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
710 qapi_event_throttle_equal
);
713 static void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
);
715 static void monitor_iothread_init(void);
717 static void monitor_data_init(Monitor
*mon
, int flags
, bool skip_flush
,
720 if (use_io_thread
&& !mon_iothread
) {
721 monitor_iothread_init();
723 qemu_mutex_init(&mon
->mon_lock
);
724 mon
->outbuf
= qstring_new();
725 /* Use *mon_cmds by default. */
726 mon
->cmd_table
= mon_cmds
;
727 mon
->skip_flush
= skip_flush
;
728 mon
->use_io_thread
= use_io_thread
;
732 static void monitor_data_destroy_qmp(MonitorQMP
*mon
)
734 json_message_parser_destroy(&mon
->parser
);
735 qemu_mutex_destroy(&mon
->qmp_queue_lock
);
736 monitor_qmp_cleanup_req_queue_locked(mon
);
737 g_queue_free(mon
->qmp_requests
);
740 static void monitor_data_destroy(Monitor
*mon
)
742 g_free(mon
->mon_cpu_path
);
743 qemu_chr_fe_deinit(&mon
->chr
, false);
744 if (monitor_is_qmp(mon
)) {
745 monitor_data_destroy_qmp(container_of(mon
, MonitorQMP
, common
));
747 readline_free(container_of(mon
, MonitorHMP
, common
)->rs
);
749 qobject_unref(mon
->outbuf
);
750 qemu_mutex_destroy(&mon
->mon_lock
);
753 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
754 int64_t cpu_index
, Error
**errp
)
760 monitor_data_init(&hmp
.common
, 0, true, false);
763 cur_mon
= &hmp
.common
;
766 int ret
= monitor_set_cpu(cpu_index
);
769 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
775 handle_hmp_command(&hmp
, command_line
);
778 qemu_mutex_lock(&hmp
.common
.mon_lock
);
779 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
780 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
782 output
= g_strdup("");
784 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
787 monitor_data_destroy(&hmp
.common
);
791 static int compare_cmd(const char *name
, const char *list
)
793 const char *p
, *pstart
;
799 p
= qemu_strchrnul(p
, '|');
800 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
809 static int get_str(char *buf
, int buf_size
, const char **pp
)
817 while (qemu_isspace(*p
)) {
828 while (*p
!= '\0' && *p
!= '\"') {
844 printf("unsupported escape code: '\\%c'\n", c
);
847 if ((q
- buf
) < buf_size
- 1) {
851 if ((q
- buf
) < buf_size
- 1) {
858 printf("unterminated string\n");
863 while (*p
!= '\0' && !qemu_isspace(*p
)) {
864 if ((q
- buf
) < buf_size
- 1) {
877 static void free_cmdline_args(char **args
, int nb_args
)
881 assert(nb_args
<= MAX_ARGS
);
883 for (i
= 0; i
< nb_args
; i
++) {
890 * Parse the command line to get valid args.
891 * @cmdline: command line to be parsed.
892 * @pnb_args: location to store the number of args, must NOT be NULL.
893 * @args: location to store the args, which should be freed by caller, must
896 * Returns 0 on success, negative on failure.
898 * NOTE: this parser is an approximate form of the real command parser. Number
899 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
900 * return with failure.
902 static int parse_cmdline(const char *cmdline
,
903 int *pnb_args
, char **args
)
912 while (qemu_isspace(*p
)) {
918 if (nb_args
>= MAX_ARGS
) {
921 ret
= get_str(buf
, sizeof(buf
), &p
);
925 args
[nb_args
] = g_strdup(buf
);
932 free_cmdline_args(args
, nb_args
);
937 * Can command @cmd be executed in preconfig state?
939 static bool cmd_can_preconfig(const mon_cmd_t
*cmd
)
945 return strchr(cmd
->flags
, 'p');
948 static void help_cmd_dump_one(Monitor
*mon
,
949 const mon_cmd_t
*cmd
,
955 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
959 for (i
= 0; i
< prefix_args_nb
; i
++) {
960 monitor_printf(mon
, "%s ", prefix_args
[i
]);
962 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
965 /* @args[@arg_index] is the valid command need to find in @cmds */
966 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
967 char **args
, int nb_args
, int arg_index
)
969 const mon_cmd_t
*cmd
;
972 /* No valid arg need to compare with, dump all in *cmds */
973 if (arg_index
>= nb_args
) {
974 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
975 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
980 /* Find one entry to dump */
981 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
982 if (compare_cmd(args
[arg_index
], cmd
->name
) &&
983 ((!runstate_check(RUN_STATE_PRECONFIG
) ||
984 cmd_can_preconfig(cmd
)))) {
985 if (cmd
->sub_table
) {
986 /* continue with next arg */
987 help_cmd_dump(mon
, cmd
->sub_table
,
988 args
, nb_args
, arg_index
+ 1);
990 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
996 /* Command not found */
997 monitor_printf(mon
, "unknown command: '");
998 for (i
= 0; i
<= arg_index
; i
++) {
999 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
1003 static void help_cmd(Monitor
*mon
, const char *name
)
1005 char *args
[MAX_ARGS
];
1008 /* 1. parse user input */
1010 /* special case for log, directly dump and return */
1011 if (!strcmp(name
, "log")) {
1012 const QEMULogItem
*item
;
1013 monitor_printf(mon
, "Log items (comma separated):\n");
1014 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
1015 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
1016 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
1021 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
1026 /* 2. dump the contents according to parsed args */
1027 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
1029 free_cmdline_args(args
, nb_args
);
1032 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
1034 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
1037 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
1039 const char *tp_name
= qdict_get_str(qdict
, "name");
1040 bool new_state
= qdict_get_bool(qdict
, "option");
1041 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1042 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1043 Error
*local_err
= NULL
;
1046 monitor_printf(mon
, "argument vcpu must be positive");
1050 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
1052 error_report_err(local_err
);
1056 #ifdef CONFIG_TRACE_SIMPLE
1057 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
1059 const char *op
= qdict_get_try_str(qdict
, "op");
1060 const char *arg
= qdict_get_try_str(qdict
, "arg");
1063 st_print_trace_file_status();
1064 } else if (!strcmp(op
, "on")) {
1065 st_set_trace_file_enabled(true);
1066 } else if (!strcmp(op
, "off")) {
1067 st_set_trace_file_enabled(false);
1068 } else if (!strcmp(op
, "flush")) {
1069 st_flush_trace_buffer();
1070 } else if (!strcmp(op
, "set")) {
1072 st_set_trace_file(arg
);
1075 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
1076 help_cmd(mon
, "trace-file");
1081 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
1083 help_cmd(mon
, "info");
1086 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
1088 CommandInfoList
*info
, **list
= opaque
;
1090 if (!cmd
->enabled
) {
1094 info
= g_malloc0(sizeof(*info
));
1095 info
->value
= g_malloc0(sizeof(*info
->value
));
1096 info
->value
->name
= g_strdup(cmd
->name
);
1101 CommandInfoList
*qmp_query_commands(Error
**errp
)
1103 CommandInfoList
*list
= NULL
;
1106 assert(monitor_is_qmp(cur_mon
));
1107 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1109 qmp_for_each_command(mon
->commands
, query_commands_cb
, &list
);
1114 EventInfoList
*qmp_query_events(Error
**errp
)
1117 * TODO This deprecated command is the only user of
1118 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
1119 * they should go, too.
1121 EventInfoList
*info
, *ev_list
= NULL
;
1124 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
1125 const char *event_name
= QAPIEvent_str(e
);
1126 assert(event_name
!= NULL
);
1127 info
= g_malloc0(sizeof(*info
));
1128 info
->value
= g_malloc0(sizeof(*info
->value
));
1129 info
->value
->name
= g_strdup(event_name
);
1131 info
->next
= ev_list
;
1139 * Minor hack: generated marshalling suppressed for this command
1140 * ('gen': false in the schema) so we can parse the JSON string
1141 * directly into QObject instead of first parsing it with
1142 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1143 * to QObject with generated output marshallers, every time. Instead,
1144 * we do it in test-qobject-input-visitor.c, just to make sure
1145 * qapi-gen.py's output actually conforms to the schema.
1147 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
1150 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
1153 static void monitor_init_qmp_commands(void)
1156 * Two command lists:
1157 * - qmp_commands contains all QMP commands
1158 * - qmp_cap_negotiation_commands contains just
1159 * "qmp_capabilities", to enforce capability negotiation
1162 qmp_init_marshal(&qmp_commands
);
1164 qmp_register_command(&qmp_commands
, "query-qmp-schema",
1165 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
1166 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
1168 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
1171 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
1172 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
1173 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
1176 static bool qmp_oob_enabled(MonitorQMP
*mon
)
1178 return mon
->capab
[QMP_CAPABILITY_OOB
];
1181 static void monitor_qmp_caps_reset(MonitorQMP
*mon
)
1183 memset(mon
->capab_offered
, 0, sizeof(mon
->capab_offered
));
1184 memset(mon
->capab
, 0, sizeof(mon
->capab
));
1185 mon
->capab_offered
[QMP_CAPABILITY_OOB
] = mon
->common
.use_io_thread
;
1189 * Accept QMP capabilities in @list for @mon.
1190 * On success, set mon->qmp.capab[], and return true.
1191 * On error, set @errp, and return false.
1193 static bool qmp_caps_accept(MonitorQMP
*mon
, QMPCapabilityList
*list
,
1196 GString
*unavailable
= NULL
;
1197 bool capab
[QMP_CAPABILITY__MAX
];
1199 memset(capab
, 0, sizeof(capab
));
1201 for (; list
; list
= list
->next
) {
1202 if (!mon
->capab_offered
[list
->value
]) {
1204 unavailable
= g_string_new(QMPCapability_str(list
->value
));
1206 g_string_append_printf(unavailable
, ", %s",
1207 QMPCapability_str(list
->value
));
1210 capab
[list
->value
] = true;
1214 error_setg(errp
, "Capability %s not available", unavailable
->str
);
1215 g_string_free(unavailable
, true);
1219 memcpy(mon
->capab
, capab
, sizeof(capab
));
1223 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
1228 assert(monitor_is_qmp(cur_mon
));
1229 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1231 if (mon
->commands
== &qmp_commands
) {
1232 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
1233 "Capabilities negotiation is already complete, command "
1238 if (!qmp_caps_accept(mon
, enable
, errp
)) {
1242 mon
->commands
= &qmp_commands
;
1245 /* Set the current CPU defined by the user. Callers must hold BQL. */
1246 int monitor_set_cpu(int cpu_index
)
1250 cpu
= qemu_get_cpu(cpu_index
);
1254 g_free(cur_mon
->mon_cpu_path
);
1255 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
1259 /* Callers must hold BQL. */
1260 static CPUState
*mon_get_cpu_sync(bool synchronize
)
1264 if (cur_mon
->mon_cpu_path
) {
1265 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
1268 g_free(cur_mon
->mon_cpu_path
);
1269 cur_mon
->mon_cpu_path
= NULL
;
1272 if (!cur_mon
->mon_cpu_path
) {
1276 monitor_set_cpu(first_cpu
->cpu_index
);
1280 cpu_synchronize_state(cpu
);
1285 CPUState
*mon_get_cpu(void)
1287 return mon_get_cpu_sync(true);
1290 CPUArchState
*mon_get_cpu_env(void)
1292 CPUState
*cs
= mon_get_cpu();
1294 return cs
? cs
->env_ptr
: NULL
;
1297 int monitor_get_cpu_index(void)
1299 CPUState
*cs
= mon_get_cpu_sync(false);
1301 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
1304 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1306 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
1311 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
1312 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1318 monitor_printf(mon
, "No CPU available\n");
1322 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1327 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1329 if (!tcg_enabled()) {
1330 error_report("JIT information is only available with accel=tcg");
1338 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1340 dump_opcount_info();
1344 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
1346 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
1347 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
1348 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
1349 enum QSPSortBy sort_by
;
1351 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
1352 qsp_report(max
, sort_by
, coalesce
);
1355 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1357 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1366 str
= readline_get_history(hmp_mon
->rs
, i
);
1370 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1375 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1377 CPUState
*cs
= mon_get_cpu();
1380 monitor_printf(mon
, "No CPU available\n");
1383 cpu_dump_statistics(cs
, 0);
1386 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1388 const char *name
= qdict_get_try_str(qdict
, "name");
1389 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1390 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1391 TraceEventInfoList
*events
;
1392 TraceEventInfoList
*elem
;
1393 Error
*local_err
= NULL
;
1399 monitor_printf(mon
, "argument vcpu must be positive");
1403 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1405 error_report_err(local_err
);
1409 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1410 monitor_printf(mon
, "%s : state %u\n",
1412 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1414 qapi_free_TraceEventInfoList(events
);
1417 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1418 bool has_port
, int64_t port
,
1419 bool has_tls_port
, int64_t tls_port
,
1420 bool has_cert_subject
, const char *cert_subject
,
1423 if (strcmp(protocol
, "spice") == 0) {
1424 if (!qemu_using_spice(errp
)) {
1428 if (!has_port
&& !has_tls_port
) {
1429 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1433 if (qemu_spice_migrate_info(hostname
,
1434 has_port
? port
: -1,
1435 has_tls_port
? tls_port
: -1,
1437 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1443 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1446 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1450 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1452 error_report_err(err
);
1456 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1459 const char *items
= qdict_get_str(qdict
, "items");
1461 if (!strcmp(items
, "none")) {
1464 mask
= qemu_str_to_log_mask(items
);
1466 help_cmd(mon
, "log");
1473 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1475 const char *option
= qdict_get_try_str(qdict
, "option");
1476 if (!option
|| !strcmp(option
, "on")) {
1478 } else if (!strcmp(option
, "off")) {
1481 monitor_printf(mon
, "unexpected option %s\n", option
);
1485 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1487 const char *device
= qdict_get_try_str(qdict
, "device");
1489 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1490 if (gdbserver_start(device
) < 0) {
1491 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1493 } else if (strcmp(device
, "none") == 0) {
1494 monitor_printf(mon
, "Disabled gdbserver\n");
1496 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1501 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1503 const char *action
= qdict_get_str(qdict
, "action");
1504 if (select_watchdog_action(action
) == -1) {
1505 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1509 static void monitor_printc(Monitor
*mon
, int c
)
1511 monitor_printf(mon
, "'");
1514 monitor_printf(mon
, "\\'");
1517 monitor_printf(mon
, "\\\\");
1520 monitor_printf(mon
, "\\n");
1523 monitor_printf(mon
, "\\r");
1526 if (c
>= 32 && c
<= 126) {
1527 monitor_printf(mon
, "%c", c
);
1529 monitor_printf(mon
, "\\x%02x", c
);
1533 monitor_printf(mon
, "'");
1536 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1537 hwaddr addr
, int is_physical
)
1539 int l
, line_size
, i
, max_digits
, len
;
1542 CPUState
*cs
= mon_get_cpu();
1544 if (!cs
&& (format
== 'i' || !is_physical
)) {
1545 monitor_printf(mon
, "Can not dump without CPU\n");
1549 if (format
== 'i') {
1550 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
1554 len
= wsize
* count
;
1563 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
1567 max_digits
= (wsize
* 8) / 4;
1571 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
1580 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1582 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1587 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
1588 MemTxResult r
= address_space_read(as
, addr
,
1589 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
1590 if (r
!= MEMTX_OK
) {
1591 monitor_printf(mon
, " Cannot access memory\n");
1595 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
1596 monitor_printf(mon
, " Cannot access memory\n");
1605 v
= ldub_p(buf
+ i
);
1608 v
= lduw_p(buf
+ i
);
1611 v
= (uint32_t)ldl_p(buf
+ i
);
1617 monitor_printf(mon
, " ");
1620 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1623 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1626 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1629 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1632 monitor_printc(mon
, v
);
1637 monitor_printf(mon
, "\n");
1643 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1645 int count
= qdict_get_int(qdict
, "count");
1646 int format
= qdict_get_int(qdict
, "format");
1647 int size
= qdict_get_int(qdict
, "size");
1648 target_long addr
= qdict_get_int(qdict
, "addr");
1650 memory_dump(mon
, count
, format
, size
, addr
, 0);
1653 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1655 int count
= qdict_get_int(qdict
, "count");
1656 int format
= qdict_get_int(qdict
, "format");
1657 int size
= qdict_get_int(qdict
, "size");
1658 hwaddr addr
= qdict_get_int(qdict
, "addr");
1660 memory_dump(mon
, count
, format
, size
, addr
, 1);
1663 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
1665 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
1669 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
1673 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
1674 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
1675 memory_region_unref(mrs
.mr
);
1680 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
1683 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
1685 hwaddr addr
= qdict_get_int(qdict
, "addr");
1686 Error
*local_err
= NULL
;
1687 MemoryRegion
*mr
= NULL
;
1690 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1692 error_report_err(local_err
);
1696 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
1698 addr
, mr
->name
, ptr
);
1700 memory_region_unref(mr
);
1703 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
1705 target_ulong addr
= qdict_get_int(qdict
, "addr");
1707 CPUState
*cs
= mon_get_cpu();
1711 monitor_printf(mon
, "No cpu\n");
1715 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
1717 monitor_printf(mon
, "Unmapped\n");
1719 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
1720 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
1725 static uint64_t vtop(void *ptr
, Error
**errp
)
1729 uintptr_t addr
= (uintptr_t) ptr
;
1730 uintptr_t pagesize
= getpagesize();
1731 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
1734 fd
= open("/proc/self/pagemap", O_RDONLY
);
1736 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
1740 /* Force copy-on-write if necessary. */
1741 atomic_add((uint8_t *)ptr
, 0);
1743 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
1744 error_setg_errno(errp
, errno
, "Cannot read pagemap");
1747 if ((pinfo
& (1ull << 63)) == 0) {
1748 error_setg(errp
, "Page not present");
1751 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
1758 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
1760 hwaddr addr
= qdict_get_int(qdict
, "addr");
1761 Error
*local_err
= NULL
;
1762 MemoryRegion
*mr
= NULL
;
1766 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1768 error_report_err(local_err
);
1772 physaddr
= vtop(ptr
, &local_err
);
1774 error_report_err(local_err
);
1776 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
1777 " (%s) is 0x%" PRIx64
"\n",
1778 addr
, mr
->name
, (uint64_t) physaddr
);
1781 memory_region_unref(mr
);
1785 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1787 int format
= qdict_get_int(qdict
, "format");
1788 hwaddr val
= qdict_get_int(qdict
, "val");
1792 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1795 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1798 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1802 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1805 monitor_printc(mon
, val
);
1808 monitor_printf(mon
, "\n");
1811 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1815 uint32_t start
= qdict_get_int(qdict
, "start");
1816 uint32_t size
= qdict_get_int(qdict
, "size");
1819 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1820 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1821 MEMTXATTRS_UNSPECIFIED
, NULL
);
1822 /* BSD sum algorithm ('sum' Unix command) */
1823 sum
= (sum
>> 1) | (sum
<< 15);
1826 monitor_printf(mon
, "%05d\n", sum
);
1829 static int mouse_button_state
;
1831 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1833 int dx
, dy
, dz
, button
;
1834 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1835 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1836 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1838 dx
= strtol(dx_str
, NULL
, 0);
1839 dy
= strtol(dy_str
, NULL
, 0);
1840 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1841 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1844 dz
= strtol(dz_str
, NULL
, 0);
1846 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1847 qemu_input_queue_btn(NULL
, button
, true);
1848 qemu_input_event_sync();
1849 qemu_input_queue_btn(NULL
, button
, false);
1852 qemu_input_event_sync();
1855 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1857 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1858 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1859 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1860 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1862 int button_state
= qdict_get_int(qdict
, "button_state");
1864 if (mouse_button_state
== button_state
) {
1867 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1868 qemu_input_event_sync();
1869 mouse_button_state
= button_state
;
1872 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1874 int size
= qdict_get_int(qdict
, "size");
1875 int addr
= qdict_get_int(qdict
, "addr");
1876 int has_index
= qdict_haskey(qdict
, "index");
1881 int index
= qdict_get_int(qdict
, "index");
1882 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1890 val
= cpu_inb(addr
);
1894 val
= cpu_inw(addr
);
1898 val
= cpu_inl(addr
);
1902 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1903 suffix
, addr
, size
* 2, val
);
1906 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1908 int size
= qdict_get_int(qdict
, "size");
1909 int addr
= qdict_get_int(qdict
, "addr");
1910 int val
= qdict_get_int(qdict
, "val");
1912 addr
&= IOPORTS_MASK
;
1917 cpu_outb(addr
, val
);
1920 cpu_outw(addr
, val
);
1923 cpu_outl(addr
, val
);
1928 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1930 Error
*local_err
= NULL
;
1931 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1933 qemu_boot_set(bootdevice
, &local_err
);
1935 error_report_err(local_err
);
1937 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1941 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1943 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1944 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1945 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1947 mtree_info(flatview
, dispatch_tree
, owner
);
1950 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1953 NumaNodeMem
*node_mem
;
1954 CpuInfoList
*cpu_list
, *cpu
;
1956 cpu_list
= qmp_query_cpus(&error_abort
);
1957 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
1959 query_numa_node_mem(node_mem
);
1960 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1961 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1962 monitor_printf(mon
, "node %d cpus:", i
);
1963 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
1964 if (cpu
->value
->has_props
&& cpu
->value
->props
->has_node_id
&&
1965 cpu
->value
->props
->node_id
== i
) {
1966 monitor_printf(mon
, " %" PRIi64
, cpu
->value
->CPU
);
1969 monitor_printf(mon
, "\n");
1970 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1971 node_mem
[i
].node_mem
>> 20);
1972 monitor_printf(mon
, "node %d plugged: %" PRId64
" MB\n", i
,
1973 node_mem
[i
].node_plugged_mem
>> 20);
1975 qapi_free_CpuInfoList(cpu_list
);
1979 #ifdef CONFIG_PROFILER
1983 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1985 static int64_t last_cpu_exec_time
;
1986 int64_t cpu_exec_time
;
1989 cpu_exec_time
= tcg_cpu_exec_time();
1990 delta
= cpu_exec_time
- last_cpu_exec_time
;
1992 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1993 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1994 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1995 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1996 last_cpu_exec_time
= cpu_exec_time
;
2000 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
2002 monitor_printf(mon
, "Internal profiler not compiled\n");
2006 /* Capture support */
2007 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2009 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
2014 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2015 monitor_printf(mon
, "[%d]: ", i
);
2016 s
->ops
.info (s
->opaque
);
2020 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
2023 int n
= qdict_get_int(qdict
, "n");
2026 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2028 s
->ops
.destroy (s
->opaque
);
2029 QLIST_REMOVE (s
, entries
);
2036 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
2038 const char *path
= qdict_get_str(qdict
, "path");
2039 int has_freq
= qdict_haskey(qdict
, "freq");
2040 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2041 int has_bits
= qdict_haskey(qdict
, "bits");
2042 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2043 int has_channels
= qdict_haskey(qdict
, "nchannels");
2044 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2047 s
= g_malloc0 (sizeof (*s
));
2049 freq
= has_freq
? freq
: 44100;
2050 bits
= has_bits
? bits
: 16;
2051 nchannels
= has_channels
? nchannels
: 2;
2053 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2054 monitor_printf(mon
, "Failed to add wave capture\n");
2058 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2061 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
2066 container
= object_get_objects_root();
2067 obj
= object_resolve_path_component(container
, name
);
2069 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2073 return QAUTHZ_LIST(obj
);
2076 static bool warn_acl
;
2077 static void hmp_warn_acl(void)
2082 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
2083 "commands are deprecated with no replacement. Authorization "
2084 "for VNC should be performed using the pluggable QAuthZ "
2089 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
2091 const char *aclname
= qdict_get_str(qdict
, "aclname");
2092 QAuthZList
*auth
= find_auth(mon
, aclname
);
2093 QAuthZListRuleList
*rules
;
2102 monitor_printf(mon
, "policy: %s\n",
2103 QAuthZListPolicy_str(auth
->policy
));
2105 rules
= auth
->rules
;
2107 QAuthZListRule
*rule
= rules
->value
;
2109 monitor_printf(mon
, "%zu: %s %s\n", i
,
2110 QAuthZListPolicy_str(rule
->policy
),
2112 rules
= rules
->next
;
2116 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2118 const char *aclname
= qdict_get_str(qdict
, "aclname");
2119 QAuthZList
*auth
= find_auth(mon
, aclname
);
2127 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
2128 qapi_free_QAuthZListRuleList(auth
->rules
);
2130 monitor_printf(mon
, "acl: removed all rules\n");
2133 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2135 const char *aclname
= qdict_get_str(qdict
, "aclname");
2136 const char *policy
= qdict_get_str(qdict
, "policy");
2137 QAuthZList
*auth
= find_auth(mon
, aclname
);
2147 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
2149 QAUTHZ_LIST_POLICY_DENY
,
2153 monitor_printf(mon
, "acl: unknown policy '%s', "
2154 "expected 'deny' or 'allow'\n", policy
);
2157 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
2158 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2160 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2165 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
2167 if (strchr(match
, '*')) {
2168 return QAUTHZ_LIST_FORMAT_GLOB
;
2170 return QAUTHZ_LIST_FORMAT_EXACT
;
2174 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2176 const char *aclname
= qdict_get_str(qdict
, "aclname");
2177 const char *match
= qdict_get_str(qdict
, "match");
2178 const char *policystr
= qdict_get_str(qdict
, "policy");
2179 int has_index
= qdict_haskey(qdict
, "index");
2180 int index
= qdict_get_try_int(qdict
, "index", -1);
2181 QAuthZList
*auth
= find_auth(mon
, aclname
);
2183 QAuthZListPolicy policy
;
2184 QAuthZListFormat format
;
2193 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
2195 QAUTHZ_LIST_POLICY_DENY
,
2199 monitor_printf(mon
, "acl: unknown policy '%s', "
2200 "expected 'deny' or 'allow'\n", policystr
);
2204 format
= hmp_acl_get_format(match
);
2206 if (has_index
&& index
== 0) {
2207 monitor_printf(mon
, "acl: unable to add acl entry\n");
2212 i
= qauthz_list_insert_rule(auth
, match
, policy
,
2213 format
, index
- 1, &err
);
2215 i
= qauthz_list_append_rule(auth
, match
, policy
,
2219 monitor_printf(mon
, "acl: unable to add rule: %s",
2220 error_get_pretty(err
));
2223 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
2227 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2229 const char *aclname
= qdict_get_str(qdict
, "aclname");
2230 const char *match
= qdict_get_str(qdict
, "match");
2231 QAuthZList
*auth
= find_auth(mon
, aclname
);
2240 i
= qauthz_list_delete_rule(auth
, match
);
2242 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
2244 monitor_printf(mon
, "acl: no matching acl entry\n");
2248 void qmp_getfd(const char *fdname
, Error
**errp
)
2253 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
2255 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2259 if (qemu_isdigit(fdname
[0])) {
2261 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2262 "a name not starting with a digit");
2266 qemu_mutex_lock(&cur_mon
->mon_lock
);
2267 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2268 if (strcmp(monfd
->name
, fdname
) != 0) {
2274 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2275 /* Make sure close() is outside critical section */
2280 monfd
= g_malloc0(sizeof(mon_fd_t
));
2281 monfd
->name
= g_strdup(fdname
);
2284 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2285 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2288 void qmp_closefd(const char *fdname
, Error
**errp
)
2293 qemu_mutex_lock(&cur_mon
->mon_lock
);
2294 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2295 if (strcmp(monfd
->name
, fdname
) != 0) {
2299 QLIST_REMOVE(monfd
, next
);
2301 g_free(monfd
->name
);
2303 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2304 /* Make sure close() is outside critical section */
2309 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2310 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2313 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2317 qemu_mutex_lock(&mon
->mon_lock
);
2318 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2321 if (strcmp(monfd
->name
, fdname
) != 0) {
2327 /* caller takes ownership of fd */
2328 QLIST_REMOVE(monfd
, next
);
2329 g_free(monfd
->name
);
2331 qemu_mutex_unlock(&mon
->mon_lock
);
2336 qemu_mutex_unlock(&mon
->mon_lock
);
2337 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2341 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2343 MonFdsetFd
*mon_fdset_fd
;
2344 MonFdsetFd
*mon_fdset_fd_next
;
2346 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2347 if ((mon_fdset_fd
->removed
||
2348 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2349 runstate_is_running()) {
2350 close(mon_fdset_fd
->fd
);
2351 g_free(mon_fdset_fd
->opaque
);
2352 QLIST_REMOVE(mon_fdset_fd
, next
);
2353 g_free(mon_fdset_fd
);
2357 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2358 QLIST_REMOVE(mon_fdset
, next
);
2363 static void monitor_fdsets_cleanup(void)
2365 MonFdset
*mon_fdset
;
2366 MonFdset
*mon_fdset_next
;
2368 qemu_mutex_lock(&mon_fdsets_lock
);
2369 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2370 monitor_fdset_cleanup(mon_fdset
);
2372 qemu_mutex_unlock(&mon_fdsets_lock
);
2375 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2376 const char *opaque
, Error
**errp
)
2379 Monitor
*mon
= cur_mon
;
2382 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
2384 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2388 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2389 has_opaque
, opaque
, errp
);
2401 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2403 MonFdset
*mon_fdset
;
2404 MonFdsetFd
*mon_fdset_fd
;
2407 qemu_mutex_lock(&mon_fdsets_lock
);
2408 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2409 if (mon_fdset
->id
!= fdset_id
) {
2412 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2414 if (mon_fdset_fd
->fd
!= fd
) {
2417 mon_fdset_fd
->removed
= true;
2420 mon_fdset_fd
->removed
= true;
2423 if (has_fd
&& !mon_fdset_fd
) {
2426 monitor_fdset_cleanup(mon_fdset
);
2427 qemu_mutex_unlock(&mon_fdsets_lock
);
2432 qemu_mutex_unlock(&mon_fdsets_lock
);
2434 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2437 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2439 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2442 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2444 MonFdset
*mon_fdset
;
2445 MonFdsetFd
*mon_fdset_fd
;
2446 FdsetInfoList
*fdset_list
= NULL
;
2448 qemu_mutex_lock(&mon_fdsets_lock
);
2449 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2450 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2451 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2453 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2454 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2456 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2457 FdsetFdInfoList
*fdsetfd_info
;
2459 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2460 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2461 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2462 if (mon_fdset_fd
->opaque
) {
2463 fdsetfd_info
->value
->has_opaque
= true;
2464 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2466 fdsetfd_info
->value
->has_opaque
= false;
2469 fdsetfd_info
->next
= fdsetfd_list
;
2470 fdsetfd_list
= fdsetfd_info
;
2473 fdset_info
->value
->fds
= fdsetfd_list
;
2475 fdset_info
->next
= fdset_list
;
2476 fdset_list
= fdset_info
;
2478 qemu_mutex_unlock(&mon_fdsets_lock
);
2483 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2484 bool has_opaque
, const char *opaque
,
2487 MonFdset
*mon_fdset
= NULL
;
2488 MonFdsetFd
*mon_fdset_fd
;
2491 qemu_mutex_lock(&mon_fdsets_lock
);
2493 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2494 /* Break if match found or match impossible due to ordering by ID */
2495 if (fdset_id
<= mon_fdset
->id
) {
2496 if (fdset_id
< mon_fdset
->id
) {
2504 if (mon_fdset
== NULL
) {
2505 int64_t fdset_id_prev
= -1;
2506 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2510 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2511 "a non-negative value");
2512 qemu_mutex_unlock(&mon_fdsets_lock
);
2515 /* Use specified fdset ID */
2516 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2517 mon_fdset_cur
= mon_fdset
;
2518 if (fdset_id
< mon_fdset_cur
->id
) {
2523 /* Use first available fdset ID */
2524 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2525 mon_fdset_cur
= mon_fdset
;
2526 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2527 fdset_id_prev
= mon_fdset_cur
->id
;
2534 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2536 mon_fdset
->id
= fdset_id
;
2538 mon_fdset
->id
= fdset_id_prev
+ 1;
2541 /* The fdset list is ordered by fdset ID */
2542 if (!mon_fdset_cur
) {
2543 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2544 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2545 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2547 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2551 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2552 mon_fdset_fd
->fd
= fd
;
2553 mon_fdset_fd
->removed
= false;
2555 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2557 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2559 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2560 fdinfo
->fdset_id
= mon_fdset
->id
;
2561 fdinfo
->fd
= mon_fdset_fd
->fd
;
2563 qemu_mutex_unlock(&mon_fdsets_lock
);
2567 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2572 MonFdset
*mon_fdset
;
2573 MonFdsetFd
*mon_fdset_fd
;
2577 qemu_mutex_lock(&mon_fdsets_lock
);
2578 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2579 if (mon_fdset
->id
!= fdset_id
) {
2582 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2583 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2584 if (mon_fd_flags
== -1) {
2589 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2590 ret
= mon_fdset_fd
->fd
;
2600 qemu_mutex_unlock(&mon_fdsets_lock
);
2605 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2607 MonFdset
*mon_fdset
;
2608 MonFdsetFd
*mon_fdset_fd_dup
;
2610 qemu_mutex_lock(&mon_fdsets_lock
);
2611 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2612 if (mon_fdset
->id
!= fdset_id
) {
2615 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2616 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2620 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2621 mon_fdset_fd_dup
->fd
= dup_fd
;
2622 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2623 qemu_mutex_unlock(&mon_fdsets_lock
);
2628 qemu_mutex_unlock(&mon_fdsets_lock
);
2632 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2634 MonFdset
*mon_fdset
;
2635 MonFdsetFd
*mon_fdset_fd_dup
;
2637 qemu_mutex_lock(&mon_fdsets_lock
);
2638 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2639 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2640 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2642 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2643 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2644 monitor_fdset_cleanup(mon_fdset
);
2648 qemu_mutex_unlock(&mon_fdsets_lock
);
2649 return mon_fdset
->id
;
2656 qemu_mutex_unlock(&mon_fdsets_lock
);
2660 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
2662 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2665 void monitor_fdset_dup_fd_remove(int dup_fd
)
2667 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2670 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2673 Error
*local_err
= NULL
;
2675 if (!qemu_isdigit(fdname
[0]) && mon
) {
2676 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2678 fd
= qemu_parse_fd(fdname
);
2680 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2685 error_propagate(errp
, local_err
);
2694 /* Please update hmp-commands.hx when adding or changing commands */
2695 static mon_cmd_t info_cmds
[] = {
2696 #include "hmp-commands-info.h"
2700 /* mon_cmds and info_cmds would be sorted at runtime */
2701 static mon_cmd_t mon_cmds
[] = {
2702 #include "hmp-commands.h"
2706 /*******************************************************************/
2708 static const char *pch
;
2709 static sigjmp_buf expr_env
;
2712 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2713 expr_error(Monitor
*mon
, const char *fmt
, ...)
2717 monitor_vprintf(mon
, fmt
, ap
);
2718 monitor_printf(mon
, "\n");
2720 siglongjmp(expr_env
, 1);
2723 /* return 0 if OK, -1 if not found */
2724 static int get_monitor_def(target_long
*pval
, const char *name
)
2726 const MonitorDef
*md
= target_monitor_defs();
2727 CPUState
*cs
= mon_get_cpu();
2732 if (cs
== NULL
|| md
== NULL
) {
2736 for(; md
->name
!= NULL
; md
++) {
2737 if (compare_cmd(name
, md
->name
)) {
2738 if (md
->get_value
) {
2739 *pval
= md
->get_value(md
, md
->offset
);
2741 CPUArchState
*env
= mon_get_cpu_env();
2742 ptr
= (uint8_t *)env
+ md
->offset
;
2745 *pval
= *(int32_t *)ptr
;
2748 *pval
= *(target_long
*)ptr
;
2759 ret
= target_get_monitor_def(cs
, name
, &tmp
);
2761 *pval
= (target_long
) tmp
;
2767 static void next(void)
2771 while (qemu_isspace(*pch
))
2776 static int64_t expr_sum(Monitor
*mon
);
2778 static int64_t expr_unary(Monitor
*mon
)
2787 n
= expr_unary(mon
);
2791 n
= -expr_unary(mon
);
2795 n
= ~expr_unary(mon
);
2801 expr_error(mon
, "')' expected");
2808 expr_error(mon
, "character constant expected");
2812 expr_error(mon
, "missing terminating \' character");
2822 while ((*pch
>= 'a' && *pch
<= 'z') ||
2823 (*pch
>= 'A' && *pch
<= 'Z') ||
2824 (*pch
>= '0' && *pch
<= '9') ||
2825 *pch
== '_' || *pch
== '.') {
2826 if ((q
- buf
) < sizeof(buf
) - 1)
2830 while (qemu_isspace(*pch
))
2833 ret
= get_monitor_def(®
, buf
);
2835 expr_error(mon
, "unknown register");
2840 expr_error(mon
, "unexpected end of expression");
2845 n
= strtoull(pch
, &p
, 0);
2846 if (errno
== ERANGE
) {
2847 expr_error(mon
, "number too large");
2850 expr_error(mon
, "invalid char '%c' in expression", *p
);
2853 while (qemu_isspace(*pch
))
2861 static int64_t expr_prod(Monitor
*mon
)
2866 val
= expr_unary(mon
);
2869 if (op
!= '*' && op
!= '/' && op
!= '%')
2872 val2
= expr_unary(mon
);
2881 expr_error(mon
, "division by zero");
2892 static int64_t expr_logic(Monitor
*mon
)
2897 val
= expr_prod(mon
);
2900 if (op
!= '&' && op
!= '|' && op
!= '^')
2903 val2
= expr_prod(mon
);
2920 static int64_t expr_sum(Monitor
*mon
)
2925 val
= expr_logic(mon
);
2928 if (op
!= '+' && op
!= '-')
2931 val2
= expr_logic(mon
);
2940 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2943 if (sigsetjmp(expr_env
, 0)) {
2947 while (qemu_isspace(*pch
))
2949 *pval
= expr_sum(mon
);
2954 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2956 const char *p
= *pp
;
2960 d
= strtod(p
, &tailp
);
2962 monitor_printf(mon
, "Number expected\n");
2965 if (d
!= d
|| d
- d
!= 0) {
2966 /* NaN or infinity */
2967 monitor_printf(mon
, "Bad number\n");
2976 * Store the command-name in cmdname, and return a pointer to
2977 * the remaining of the command string.
2979 static const char *get_command_name(const char *cmdline
,
2980 char *cmdname
, size_t nlen
)
2983 const char *p
, *pstart
;
2986 while (qemu_isspace(*p
))
2991 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2996 memcpy(cmdname
, pstart
, len
);
2997 cmdname
[len
] = '\0';
3002 * Read key of 'type' into 'key' and return the current
3005 static char *key_get_info(const char *type
, char **key
)
3013 p
= strchr(type
, ':');
3020 str
= g_malloc(len
+ 1);
3021 memcpy(str
, type
, len
);
3028 static int default_fmt_format
= 'x';
3029 static int default_fmt_size
= 4;
3031 static int is_valid_option(const char *c
, const char *typestr
)
3039 typestr
= strstr(typestr
, option
);
3040 return (typestr
!= NULL
);
3043 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
3044 const char *cmdname
)
3046 const mon_cmd_t
*cmd
;
3048 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
3049 if (compare_cmd(cmdname
, cmd
->name
)) {
3058 * Parse command name from @cmdp according to command table @table.
3059 * If blank, return NULL.
3060 * Else, if no valid command can be found, report to @mon, and return
3062 * Else, change @cmdp to point right behind the name, and return its
3063 * command table entry.
3064 * Do not assume the return value points into @table! It doesn't when
3065 * the command is found in a sub-command table.
3067 static const mon_cmd_t
*monitor_parse_command(MonitorHMP
*hmp_mon
,
3068 const char *cmdp_start
,
3072 Monitor
*mon
= &hmp_mon
->common
;
3074 const mon_cmd_t
*cmd
;
3077 /* extract the command name */
3078 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
3082 cmd
= search_dispatch_table(table
, cmdname
);
3084 monitor_printf(mon
, "unknown command: '%.*s'\n",
3085 (int)(p
- cmdp_start
), cmdp_start
);
3088 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
3089 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
3090 "until after exit_preconfig.\n",
3091 (int)(p
- cmdp_start
), cmdp_start
);
3095 /* filter out following useless space */
3096 while (qemu_isspace(*p
)) {
3101 /* search sub command */
3102 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
3103 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
3110 * Parse arguments for @cmd.
3111 * If it can't be parsed, report to @mon, and return NULL.
3112 * Else, insert command arguments into a QDict, and return it.
3113 * Note: On success, caller has to free the QDict structure.
3116 static QDict
*monitor_parse_arguments(Monitor
*mon
,
3118 const mon_cmd_t
*cmd
)
3120 const char *typestr
;
3123 const char *p
= *endp
;
3125 QDict
*qdict
= qdict_new();
3127 /* parse the parameters */
3128 typestr
= cmd
->args_type
;
3130 typestr
= key_get_info(typestr
, &key
);
3142 while (qemu_isspace(*p
))
3144 if (*typestr
== '?') {
3147 /* no optional string: NULL argument */
3151 ret
= get_str(buf
, sizeof(buf
), &p
);
3155 monitor_printf(mon
, "%s: filename expected\n",
3159 monitor_printf(mon
, "%s: block device name expected\n",
3163 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
3168 qdict_put_str(qdict
, key
, buf
);
3173 QemuOptsList
*opts_list
;
3176 opts_list
= qemu_find_opts(key
);
3177 if (!opts_list
|| opts_list
->desc
->name
) {
3180 while (qemu_isspace(*p
)) {
3185 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3188 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
3192 qemu_opts_to_qdict(opts
, qdict
);
3193 qemu_opts_del(opts
);
3198 int count
, format
, size
;
3200 while (qemu_isspace(*p
))
3206 if (qemu_isdigit(*p
)) {
3208 while (qemu_isdigit(*p
)) {
3209 count
= count
* 10 + (*p
- '0');
3247 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3248 monitor_printf(mon
, "invalid char in format: '%c'\n",
3253 format
= default_fmt_format
;
3254 if (format
!= 'i') {
3255 /* for 'i', not specifying a size gives -1 as size */
3257 size
= default_fmt_size
;
3258 default_fmt_size
= size
;
3260 default_fmt_format
= format
;
3263 format
= default_fmt_format
;
3264 if (format
!= 'i') {
3265 size
= default_fmt_size
;
3270 qdict_put_int(qdict
, "count", count
);
3271 qdict_put_int(qdict
, "format", format
);
3272 qdict_put_int(qdict
, "size", size
);
3281 while (qemu_isspace(*p
))
3283 if (*typestr
== '?' || *typestr
== '.') {
3284 if (*typestr
== '?') {
3292 while (qemu_isspace(*p
))
3301 if (get_expr(mon
, &val
, &p
))
3303 /* Check if 'i' is greater than 32-bit */
3304 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3305 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3306 monitor_printf(mon
, "integer is for 32-bit values\n");
3308 } else if (c
== 'M') {
3310 monitor_printf(mon
, "enter a positive value\n");
3315 qdict_put_int(qdict
, key
, val
);
3324 while (qemu_isspace(*p
)) {
3327 if (*typestr
== '?') {
3333 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
3334 if (ret
< 0 || val
> INT64_MAX
) {
3335 monitor_printf(mon
, "invalid size\n");
3338 qdict_put_int(qdict
, key
, val
);
3346 while (qemu_isspace(*p
))
3348 if (*typestr
== '?') {
3354 if (get_double(mon
, &val
, &p
) < 0) {
3357 if (p
[0] && p
[1] == 's') {
3360 val
/= 1e3
; p
+= 2; break;
3362 val
/= 1e6
; p
+= 2; break;
3364 val
/= 1e9
; p
+= 2; break;
3367 if (*p
&& !qemu_isspace(*p
)) {
3368 monitor_printf(mon
, "Unknown unit suffix\n");
3371 qdict_put(qdict
, key
, qnum_from_double(val
));
3379 while (qemu_isspace(*p
)) {
3383 while (qemu_isgraph(*p
)) {
3386 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3388 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3391 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3394 qdict_put_bool(qdict
, key
, val
);
3399 const char *tmp
= p
;
3406 while (qemu_isspace(*p
))
3411 if(!is_valid_option(p
, typestr
)) {
3413 monitor_printf(mon
, "%s: unsupported option -%c\n",
3425 qdict_put_bool(qdict
, key
, true);
3432 /* package all remaining string */
3435 while (qemu_isspace(*p
)) {
3438 if (*typestr
== '?') {
3441 /* no remaining string: NULL argument */
3447 monitor_printf(mon
, "%s: string expected\n",
3451 qdict_put_str(qdict
, key
, p
);
3457 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3463 /* check that all arguments were parsed */
3464 while (qemu_isspace(*p
))
3467 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3475 qobject_unref(qdict
);
3480 static void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
3483 const mon_cmd_t
*cmd
;
3484 const char *cmd_start
= cmdline
;
3486 trace_handle_hmp_command(mon
, cmdline
);
3488 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, mon
->common
.cmd_table
);
3493 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
3495 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
3498 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
3499 (int)(cmdline
- cmd_start
), cmd_start
);
3503 cmd
->cmd(&mon
->common
, qdict
);
3504 qobject_unref(qdict
);
3507 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
3509 const char *p
, *pstart
;
3516 p
= qemu_strchrnul(p
, '|');
3518 if (len
> sizeof(cmd
) - 2)
3519 len
= sizeof(cmd
) - 2;
3520 memcpy(cmd
, pstart
, len
);
3522 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3523 readline_add_completion(mon
->rs
, cmd
);
3531 static void file_completion(MonitorHMP
*mon
, const char *input
)
3536 char file
[1024], file_prefix
[1024];
3540 p
= strrchr(input
, '/');
3543 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3544 pstrcpy(path
, sizeof(path
), ".");
3546 input_path_len
= p
- input
+ 1;
3547 memcpy(path
, input
, input_path_len
);
3548 if (input_path_len
> sizeof(path
) - 1)
3549 input_path_len
= sizeof(path
) - 1;
3550 path
[input_path_len
] = '\0';
3551 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3554 ffs
= opendir(path
);
3563 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3567 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3568 memcpy(file
, input
, input_path_len
);
3569 if (input_path_len
< sizeof(file
))
3570 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3572 /* stat the file to find out if it's a directory.
3573 * In that case add a slash to speed up typing long paths
3575 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3576 pstrcat(file
, sizeof(file
), "/");
3578 readline_add_completion(mon
->rs
, file
);
3584 static const char *next_arg_type(const char *typestr
)
3586 const char *p
= strchr(typestr
, ':');
3587 return (p
!= NULL
? ++p
: typestr
);
3590 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3593 if (!str
|| !option
) {
3596 if (!strncmp(option
, str
, strlen(str
))) {
3597 readline_add_completion(rs
, option
);
3601 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3604 ChardevBackendInfoList
*list
, *start
;
3610 readline_set_completion_index(rs
, len
);
3612 start
= list
= qmp_query_chardev_backends(NULL
);
3614 const char *chr_name
= list
->value
->name
;
3616 if (!strncmp(chr_name
, str
, len
)) {
3617 readline_add_completion(rs
, chr_name
);
3621 qapi_free_ChardevBackendInfoList(start
);
3624 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3633 readline_set_completion_index(rs
, len
);
3634 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
3635 add_completion_option(rs
, str
, NetClientDriver_str(i
));
3639 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3649 readline_set_completion_index(rs
, len
);
3650 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3653 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3655 name
= object_class_get_name(OBJECT_CLASS(dc
));
3657 if (dc
->user_creatable
3658 && !strncmp(name
, str
, len
)) {
3659 readline_add_completion(rs
, name
);
3666 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3676 readline_set_completion_index(rs
, len
);
3677 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3681 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3682 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3683 readline_add_completion(rs
, name
);
3690 static void peripheral_device_del_completion(ReadLineState
*rs
,
3691 const char *str
, size_t len
)
3693 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3694 GSList
*list
, *item
;
3696 list
= qdev_build_hotpluggable_device_list(peripheral
);
3701 for (item
= list
; item
; item
= g_slist_next(item
)) {
3702 DeviceState
*dev
= item
->data
;
3704 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3705 readline_add_completion(rs
, dev
->id
);
3712 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3715 ChardevInfoList
*list
, *start
;
3721 readline_set_completion_index(rs
, len
);
3723 start
= list
= qmp_query_chardev(NULL
);
3725 ChardevInfo
*chr
= list
->value
;
3727 if (!strncmp(chr
->label
, str
, len
)) {
3728 readline_add_completion(rs
, chr
->label
);
3732 qapi_free_ChardevInfoList(start
);
3735 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3738 ChardevInfoList
*list
, *start
;
3741 readline_set_completion_index(rs
, len
);
3743 start
= list
= qmp_query_chardev(NULL
);
3745 ChardevInfo
*chr_info
= list
->value
;
3747 if (!strncmp(chr_info
->label
, str
, len
)) {
3748 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
3749 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
3750 readline_add_completion(rs
, chr_info
->label
);
3755 qapi_free_ChardevInfoList(start
);
3758 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3763 ringbuf_completion(rs
, str
);
3766 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3775 readline_set_completion_index(rs
, len
);
3776 peripheral_device_del_completion(rs
, str
, len
);
3779 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3781 ObjectPropertyInfoList
*list
, *start
;
3788 readline_set_completion_index(rs
, len
);
3790 start
= list
= qmp_qom_list("/objects", NULL
);
3792 ObjectPropertyInfo
*info
= list
->value
;
3794 if (!strncmp(info
->type
, "child<", 5)
3795 && !strncmp(info
->name
, str
, len
)) {
3796 readline_add_completion(rs
, info
->name
);
3800 qapi_free_ObjectPropertyInfoList(start
);
3803 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3812 sep
= strrchr(str
, '-');
3817 readline_set_completion_index(rs
, len
);
3818 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3819 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
3820 readline_add_completion(rs
, QKeyCode_str(i
));
3825 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3830 readline_set_completion_index(rs
, len
);
3832 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3834 count
= qemu_find_net_clients_except(NULL
, ncs
,
3835 NET_CLIENT_DRIVER_NONE
,
3837 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3838 const char *name
= ncs
[i
]->name
;
3839 if (!strncmp(str
, name
, len
)) {
3840 readline_add_completion(rs
, name
);
3843 } else if (nb_args
== 3) {
3844 add_completion_option(rs
, str
, "on");
3845 add_completion_option(rs
, str
, "off");
3849 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3852 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3859 readline_set_completion_index(rs
, len
);
3860 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3862 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3864 const char *name
= ncs
[i
]->name
;
3865 if (strncmp(str
, name
, len
)) {
3868 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3870 readline_add_completion(rs
, name
);
3875 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3880 readline_set_completion_index(rs
, len
);
3882 TraceEventIter iter
;
3884 char *pattern
= g_strdup_printf("%s*", str
);
3885 trace_event_iter_init(&iter
, pattern
);
3886 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3887 readline_add_completion(rs
, trace_event_get_name(ev
));
3893 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3898 readline_set_completion_index(rs
, len
);
3900 TraceEventIter iter
;
3902 char *pattern
= g_strdup_printf("%s*", str
);
3903 trace_event_iter_init(&iter
, pattern
);
3904 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3905 readline_add_completion(rs
, trace_event_get_name(ev
));
3908 } else if (nb_args
== 3) {
3909 add_completion_option(rs
, str
, "on");
3910 add_completion_option(rs
, str
, "off");
3914 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3921 readline_set_completion_index(rs
, strlen(str
));
3922 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
3923 add_completion_option(rs
, str
, WatchdogAction_str(i
));
3927 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3933 readline_set_completion_index(rs
, len
);
3936 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3937 const char *name
= MigrationCapability_str(i
);
3938 if (!strncmp(str
, name
, len
)) {
3939 readline_add_completion(rs
, name
);
3942 } else if (nb_args
== 3) {
3943 add_completion_option(rs
, str
, "on");
3944 add_completion_option(rs
, str
, "off");
3948 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3954 readline_set_completion_index(rs
, len
);
3957 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3958 const char *name
= MigrationParameter_str(i
);
3959 if (!strncmp(str
, name
, len
)) {
3960 readline_add_completion(rs
, name
);
3966 static void vm_completion(ReadLineState
*rs
, const char *str
)
3969 BlockDriverState
*bs
;
3970 BdrvNextIterator it
;
3973 readline_set_completion_index(rs
, len
);
3975 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3976 SnapshotInfoList
*snapshots
, *snapshot
;
3977 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3980 aio_context_acquire(ctx
);
3981 if (bdrv_can_snapshot(bs
)) {
3982 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3984 aio_context_release(ctx
);
3989 snapshot
= snapshots
;
3991 char *completion
= snapshot
->value
->name
;
3992 if (!strncmp(str
, completion
, len
)) {
3993 readline_add_completion(rs
, completion
);
3995 completion
= snapshot
->value
->id
;
3996 if (!strncmp(str
, completion
, len
)) {
3997 readline_add_completion(rs
, completion
);
3999 snapshot
= snapshot
->next
;
4001 qapi_free_SnapshotInfoList(snapshots
);
4006 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4009 vm_completion(rs
, str
);
4013 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
4016 vm_completion(rs
, str
);
4020 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
4021 const mon_cmd_t
*cmd_table
,
4025 const char *cmdname
;
4027 const char *ptype
, *old_ptype
, *str
, *name
;
4028 const mon_cmd_t
*cmd
;
4029 BlockBackend
*blk
= NULL
;
4032 /* command completion */
4037 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
4038 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4039 if (!runstate_check(RUN_STATE_PRECONFIG
) ||
4040 cmd_can_preconfig(cmd
)) {
4041 cmd_completion(mon
, cmdname
, cmd
->name
);
4045 /* find the command */
4046 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
4047 if (compare_cmd(args
[0], cmd
->name
) &&
4048 (!runstate_check(RUN_STATE_PRECONFIG
) ||
4049 cmd_can_preconfig(cmd
))) {
4057 if (cmd
->sub_table
) {
4058 /* do the job again */
4059 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4060 &args
[1], nb_args
- 1);
4063 if (cmd
->command_completion
) {
4064 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4068 ptype
= next_arg_type(cmd
->args_type
);
4069 for(i
= 0; i
< nb_args
- 2; i
++) {
4070 if (*ptype
!= '\0') {
4071 ptype
= next_arg_type(ptype
);
4072 while (*ptype
== '?')
4073 ptype
= next_arg_type(ptype
);
4076 str
= args
[nb_args
- 1];
4078 while (*ptype
== '-' && old_ptype
!= ptype
) {
4080 ptype
= next_arg_type(ptype
);
4084 /* file completion */
4085 readline_set_completion_index(mon
->rs
, strlen(str
));
4086 file_completion(mon
, str
);
4089 /* block device name completion */
4090 readline_set_completion_index(mon
->rs
, strlen(str
));
4091 while ((blk
= blk_next(blk
)) != NULL
) {
4092 name
= blk_name(blk
);
4093 if (str
[0] == '\0' ||
4094 !strncmp(name
, str
, strlen(str
))) {
4095 readline_add_completion(mon
->rs
, name
);
4101 if (!strcmp(cmd
->name
, "help|?")) {
4102 monitor_find_completion_by_table(mon
, cmd_table
,
4103 &args
[1], nb_args
- 1);
4112 static void monitor_find_completion(void *opaque
,
4113 const char *cmdline
)
4115 MonitorHMP
*mon
= opaque
;
4116 char *args
[MAX_ARGS
];
4119 /* 1. parse the cmdline */
4120 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4124 /* if the line ends with a space, it means we want to complete the
4126 len
= strlen(cmdline
);
4127 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4128 if (nb_args
>= MAX_ARGS
) {
4131 args
[nb_args
++] = g_strdup("");
4134 /* 2. auto complete according to args */
4135 monitor_find_completion_by_table(mon
, mon
->common
.cmd_table
, args
, nb_args
);
4138 free_cmdline_args(args
, nb_args
);
4141 static int monitor_can_read(void *opaque
)
4143 Monitor
*mon
= opaque
;
4145 return !atomic_mb_read(&mon
->suspend_cnt
);
4149 * Emit QMP response @rsp with ID @id to @mon.
4150 * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
4151 * Nothing is emitted then.
4153 static void monitor_qmp_respond(MonitorQMP
*mon
, QDict
*rsp
)
4156 qmp_send_response(mon
, rsp
);
4160 static void monitor_qmp_dispatch(MonitorQMP
*mon
, QObject
*req
)
4167 cur_mon
= &mon
->common
;
4169 rsp
= qmp_dispatch(mon
->commands
, req
, qmp_oob_enabled(mon
));
4173 if (mon
->commands
== &qmp_cap_negotiation_commands
) {
4174 error
= qdict_get_qdict(rsp
, "error");
4176 && !g_strcmp0(qdict_get_try_str(error
, "class"),
4177 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND
))) {
4178 /* Provide a more useful error message */
4179 qdict_del(error
, "desc");
4180 qdict_put_str(error
, "desc", "Expecting capabilities negotiation"
4181 " with 'qmp_capabilities'");
4185 monitor_qmp_respond(mon
, rsp
);
4190 * Pop a QMP request from a monitor request queue.
4191 * Return the request, or NULL all request queues are empty.
4192 * We are using round-robin fashion to pop the request, to avoid
4193 * processing commands only on a very busy monitor. To achieve that,
4194 * when we process one request on a specific monitor, we put that
4195 * monitor to the end of mon_list queue.
4197 * Note: if the function returned with non-NULL, then the caller will
4198 * be with qmp_mon->qmp_queue_lock held, and the caller is responsible
4201 static QMPRequest
*monitor_qmp_requests_pop_any_with_lock(void)
4203 QMPRequest
*req_obj
= NULL
;
4205 MonitorQMP
*qmp_mon
;
4207 qemu_mutex_lock(&monitor_lock
);
4209 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
4210 if (!monitor_is_qmp(mon
)) {
4214 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
4215 qemu_mutex_lock(&qmp_mon
->qmp_queue_lock
);
4216 req_obj
= g_queue_pop_head(qmp_mon
->qmp_requests
);
4218 /* With the lock of corresponding queue held */
4221 qemu_mutex_unlock(&qmp_mon
->qmp_queue_lock
);
4226 * We found one request on the monitor. Degrade this monitor's
4227 * priority to lowest by re-inserting it to end of queue.
4229 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4230 QTAILQ_INSERT_TAIL(&mon_list
, mon
, entry
);
4233 qemu_mutex_unlock(&monitor_lock
);
4238 static void monitor_qmp_bh_dispatcher(void *data
)
4240 QMPRequest
*req_obj
= monitor_qmp_requests_pop_any_with_lock();
4250 /* qmp_oob_enabled() might change after "qmp_capabilities" */
4251 need_resume
= !qmp_oob_enabled(mon
) ||
4252 mon
->qmp_requests
->length
== QMP_REQ_QUEUE_LEN_MAX
- 1;
4253 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
4255 QDict
*qdict
= qobject_to(QDict
, req_obj
->req
);
4256 QObject
*id
= qdict
? qdict_get(qdict
, "id") : NULL
;
4257 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(id
) ?: "");
4258 monitor_qmp_dispatch(mon
, req_obj
->req
);
4260 assert(req_obj
->err
);
4261 rsp
= qmp_error_response(req_obj
->err
);
4262 req_obj
->err
= NULL
;
4263 monitor_qmp_respond(mon
, rsp
);
4268 /* Pairs with the monitor_suspend() in handle_qmp_command() */
4269 monitor_resume(&mon
->common
);
4271 qmp_request_free(req_obj
);
4273 /* Reschedule instead of looping so the main loop stays responsive */
4274 qemu_bh_schedule(qmp_dispatcher_bh
);
4277 static void handle_qmp_command(void *opaque
, QObject
*req
, Error
*err
)
4279 MonitorQMP
*mon
= opaque
;
4282 QMPRequest
*req_obj
;
4284 assert(!req
!= !err
);
4286 qdict
= qobject_to(QDict
, req
);
4288 id
= qdict_get(qdict
, "id");
4289 } /* else will fail qmp_dispatch() */
4291 if (req
&& trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND
)) {
4292 QString
*req_json
= qobject_to_json(req
);
4293 trace_handle_qmp_command(mon
, qstring_get_str(req_json
));
4294 qobject_unref(req_json
);
4297 if (qdict
&& qmp_is_oob(qdict
)) {
4298 /* OOB commands are executed immediately */
4299 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id
) ?: "");
4300 monitor_qmp_dispatch(mon
, req
);
4305 req_obj
= g_new0(QMPRequest
, 1);
4310 /* Protect qmp_requests and fetching its length. */
4311 qemu_mutex_lock(&mon
->qmp_queue_lock
);
4314 * Suspend the monitor when we can't queue more requests after
4315 * this one. Dequeuing in monitor_qmp_bh_dispatcher() will resume
4316 * it. Note that when OOB is disabled, we queue at most one
4317 * command, for backward compatibility.
4319 if (!qmp_oob_enabled(mon
) ||
4320 mon
->qmp_requests
->length
== QMP_REQ_QUEUE_LEN_MAX
- 1) {
4321 monitor_suspend(&mon
->common
);
4325 * Put the request to the end of queue so that requests will be
4326 * handled in time order. Ownership for req_obj, req,
4327 * etc. will be delivered to the handler side.
4329 assert(mon
->qmp_requests
->length
< QMP_REQ_QUEUE_LEN_MAX
);
4330 g_queue_push_tail(mon
->qmp_requests
, req_obj
);
4331 qemu_mutex_unlock(&mon
->qmp_queue_lock
);
4333 /* Kick the dispatcher routine */
4334 qemu_bh_schedule(qmp_dispatcher_bh
);
4337 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4339 MonitorQMP
*mon
= opaque
;
4341 json_message_parser_feed(&mon
->parser
, (const char *) buf
, size
);
4344 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4347 Monitor
*old_mon
= cur_mon
;
4351 mon
= container_of(cur_mon
, MonitorHMP
, common
);
4354 for (i
= 0; i
< size
; i
++)
4355 readline_handle_byte(mon
->rs
, buf
[i
]);
4357 if (size
== 0 || buf
[size
- 1] != 0)
4358 monitor_printf(cur_mon
, "corrupted command\n");
4360 handle_hmp_command(mon
, (char *)buf
);
4366 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4367 void *readline_opaque
)
4369 MonitorHMP
*mon
= opaque
;
4371 monitor_suspend(&mon
->common
);
4372 handle_hmp_command(mon
, cmdline
);
4373 monitor_resume(&mon
->common
);
4376 int monitor_suspend(Monitor
*mon
)
4378 if (monitor_is_hmp_non_interactive(mon
)) {
4382 atomic_inc(&mon
->suspend_cnt
);
4384 if (mon
->use_io_thread
) {
4386 * Kick I/O thread to make sure this takes effect. It'll be
4387 * evaluated again in prepare() of the watch object.
4389 aio_notify(iothread_get_aio_context(mon_iothread
));
4392 trace_monitor_suspend(mon
, 1);
4396 static void monitor_accept_input(void *opaque
)
4398 Monitor
*mon
= opaque
;
4400 qemu_chr_fe_accept_input(&mon
->chr
);
4403 void monitor_resume(Monitor
*mon
)
4405 if (monitor_is_hmp_non_interactive(mon
)) {
4409 if (atomic_dec_fetch(&mon
->suspend_cnt
) == 0) {
4412 if (mon
->use_io_thread
) {
4413 ctx
= iothread_get_aio_context(mon_iothread
);
4415 ctx
= qemu_get_aio_context();
4418 if (!monitor_is_qmp(mon
)) {
4419 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
4420 assert(hmp_mon
->rs
);
4421 readline_show_prompt(hmp_mon
->rs
);
4424 aio_bh_schedule_oneshot(ctx
, monitor_accept_input
, mon
);
4427 trace_monitor_suspend(mon
, -1);
4430 static QDict
*qmp_greeting(MonitorQMP
*mon
)
4432 QList
*cap_list
= qlist_new();
4433 QObject
*ver
= NULL
;
4436 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4438 for (cap
= 0; cap
< QMP_CAPABILITY__MAX
; cap
++) {
4439 if (mon
->capab_offered
[cap
]) {
4440 qlist_append_str(cap_list
, QMPCapability_str(cap
));
4444 return qdict_from_jsonf_nofail(
4445 "{'QMP': {'version': %p, 'capabilities': %p}}",
4449 static void monitor_qmp_event(void *opaque
, int event
)
4452 MonitorQMP
*mon
= opaque
;
4455 case CHR_EVENT_OPENED
:
4456 mon
->commands
= &qmp_cap_negotiation_commands
;
4457 monitor_qmp_caps_reset(mon
);
4458 data
= qmp_greeting(mon
);
4459 qmp_send_response(mon
, data
);
4460 qobject_unref(data
);
4463 case CHR_EVENT_CLOSED
:
4465 * Note: this is only useful when the output of the chardev
4466 * backend is still open. For example, when the backend is
4467 * stdio, it's possible that stdout is still open when stdin
4470 monitor_qmp_cleanup_queues(mon
);
4471 json_message_parser_destroy(&mon
->parser
);
4472 json_message_parser_init(&mon
->parser
, handle_qmp_command
,
4475 monitor_fdsets_cleanup();
4480 static void monitor_event(void *opaque
, int event
)
4482 Monitor
*mon
= opaque
;
4483 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
4486 case CHR_EVENT_MUX_IN
:
4487 qemu_mutex_lock(&mon
->mon_lock
);
4489 qemu_mutex_unlock(&mon
->mon_lock
);
4490 if (mon
->reset_seen
) {
4491 readline_restart(hmp_mon
->rs
);
4492 monitor_resume(mon
);
4495 atomic_mb_set(&mon
->suspend_cnt
, 0);
4499 case CHR_EVENT_MUX_OUT
:
4500 if (mon
->reset_seen
) {
4501 if (atomic_mb_read(&mon
->suspend_cnt
) == 0) {
4502 monitor_printf(mon
, "\n");
4505 monitor_suspend(mon
);
4507 atomic_inc(&mon
->suspend_cnt
);
4509 qemu_mutex_lock(&mon
->mon_lock
);
4511 qemu_mutex_unlock(&mon
->mon_lock
);
4514 case CHR_EVENT_OPENED
:
4515 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4516 "information\n", QEMU_VERSION
);
4517 if (!mon
->mux_out
) {
4518 readline_restart(hmp_mon
->rs
);
4519 readline_show_prompt(hmp_mon
->rs
);
4521 mon
->reset_seen
= 1;
4525 case CHR_EVENT_CLOSED
:
4527 monitor_fdsets_cleanup();
4533 compare_mon_cmd(const void *a
, const void *b
)
4535 return strcmp(((const mon_cmd_t
*)a
)->name
,
4536 ((const mon_cmd_t
*)b
)->name
);
4539 static void sortcmdlist(void)
4542 int elem_size
= sizeof(mon_cmd_t
);
4544 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4545 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4547 array_num
= sizeof(info_cmds
)/elem_size
-1;
4548 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4551 static void monitor_iothread_init(void)
4553 mon_iothread
= iothread_create("mon_iothread", &error_abort
);
4556 void monitor_init_globals(void)
4558 monitor_init_qmp_commands();
4559 monitor_qapi_event_init();
4561 qemu_mutex_init(&monitor_lock
);
4562 qemu_mutex_init(&mon_fdsets_lock
);
4565 * The dispatcher BH must run in the main loop thread, since we
4566 * have commands assuming that context. It would be nice to get
4567 * rid of those assumptions.
4569 qmp_dispatcher_bh
= aio_bh_new(iohandler_get_aio_context(),
4570 monitor_qmp_bh_dispatcher
,
4574 /* These functions just adapt the readline interface in a typesafe way. We
4575 * could cast function pointers but that discards compiler checks.
4577 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4578 const char *fmt
, ...)
4580 MonitorHMP
*mon
= opaque
;
4583 monitor_vprintf(&mon
->common
, fmt
, ap
);
4587 static void monitor_readline_flush(void *opaque
)
4589 MonitorHMP
*mon
= opaque
;
4590 monitor_flush(&mon
->common
);
4594 * Print to current monitor if we have one, else to stderr.
4596 int error_vprintf(const char *fmt
, va_list ap
)
4598 if (cur_mon
&& !monitor_cur_is_qmp()) {
4599 return monitor_vprintf(cur_mon
, fmt
, ap
);
4601 return vfprintf(stderr
, fmt
, ap
);
4604 int error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
4607 return vfprintf(stderr
, fmt
, ap
);
4609 if (!monitor_cur_is_qmp()) {
4610 return monitor_vprintf(cur_mon
, fmt
, ap
);
4615 static void monitor_list_append(Monitor
*mon
)
4617 qemu_mutex_lock(&monitor_lock
);
4619 * This prevents inserting new monitors during monitor_cleanup().
4620 * A cleaner solution would involve the main thread telling other
4621 * threads to terminate, waiting for their termination.
4623 if (!monitor_destroyed
) {
4624 QTAILQ_INSERT_HEAD(&mon_list
, mon
, entry
);
4627 qemu_mutex_unlock(&monitor_lock
);
4630 monitor_data_destroy(mon
);
4635 static void monitor_qmp_setup_handlers_bh(void *opaque
)
4637 MonitorQMP
*mon
= opaque
;
4638 GMainContext
*context
;
4640 assert(mon
->common
.use_io_thread
);
4641 context
= iothread_get_g_main_context(mon_iothread
);
4643 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
,
4644 monitor_qmp_read
, monitor_qmp_event
,
4645 NULL
, &mon
->common
, context
, true);
4646 monitor_list_append(&mon
->common
);
4649 static void monitor_init_qmp(Chardev
*chr
, int flags
)
4651 MonitorQMP
*mon
= g_new0(MonitorQMP
, 1);
4653 /* Only HMP supports readline */
4654 assert(!(flags
& MONITOR_USE_READLINE
));
4656 /* Note: we run QMP monitor in I/O thread when @chr supports that */
4657 monitor_data_init(&mon
->common
, flags
, false,
4658 qemu_chr_has_feature(chr
, QEMU_CHAR_FEATURE_GCONTEXT
));
4660 qemu_mutex_init(&mon
->qmp_queue_lock
);
4661 mon
->qmp_requests
= g_queue_new();
4663 qemu_chr_fe_init(&mon
->common
.chr
, chr
, &error_abort
);
4664 qemu_chr_fe_set_echo(&mon
->common
.chr
, true);
4666 json_message_parser_init(&mon
->parser
, handle_qmp_command
, mon
, NULL
);
4667 if (mon
->common
.use_io_thread
) {
4669 * Make sure the old iowatch is gone. It's possible when
4670 * e.g. the chardev is in client mode, with wait=on.
4672 remove_fd_in_watch(chr
);
4674 * We can't call qemu_chr_fe_set_handlers() directly here
4675 * since chardev might be running in the monitor I/O
4676 * thread. Schedule a bottom half.
4678 aio_bh_schedule_oneshot(iothread_get_aio_context(mon_iothread
),
4679 monitor_qmp_setup_handlers_bh
, mon
);
4680 /* The bottom half will add @mon to @mon_list */
4682 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
,
4683 monitor_qmp_read
, monitor_qmp_event
,
4684 NULL
, &mon
->common
, NULL
, true);
4685 monitor_list_append(&mon
->common
);
4689 static void monitor_init_hmp(Chardev
*chr
, int flags
)
4691 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
4692 bool use_readline
= flags
& MONITOR_USE_READLINE
;
4694 monitor_data_init(&mon
->common
, flags
, false, false);
4695 qemu_chr_fe_init(&mon
->common
.chr
, chr
, &error_abort
);
4698 mon
->rs
= readline_init(monitor_readline_printf
,
4699 monitor_readline_flush
,
4701 monitor_find_completion
);
4702 monitor_read_command(mon
, 0);
4705 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
4706 monitor_event
, NULL
, &mon
->common
, NULL
, true);
4707 monitor_list_append(&mon
->common
);
4710 void monitor_init(Chardev
*chr
, int flags
)
4712 if (flags
& MONITOR_USE_CONTROL
) {
4713 monitor_init_qmp(chr
, flags
);
4715 monitor_init_hmp(chr
, flags
);
4719 void monitor_cleanup(void)
4722 * We need to explicitly stop the I/O thread (but not destroy it),
4723 * clean up the monitor resources, then destroy the I/O thread since
4724 * we need to unregister from chardev below in
4725 * monitor_data_destroy(), and chardev is not thread-safe yet
4728 iothread_stop(mon_iothread
);
4731 /* Flush output buffers and destroy monitors */
4732 qemu_mutex_lock(&monitor_lock
);
4733 monitor_destroyed
= true;
4734 while (!QTAILQ_EMPTY(&mon_list
)) {
4735 Monitor
*mon
= QTAILQ_FIRST(&mon_list
);
4736 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4737 /* Permit QAPI event emission from character frontend release */
4738 qemu_mutex_unlock(&monitor_lock
);
4740 monitor_data_destroy(mon
);
4741 qemu_mutex_lock(&monitor_lock
);
4744 qemu_mutex_unlock(&monitor_lock
);
4746 /* QEMUBHs needs to be deleted before destroying the I/O thread */
4747 qemu_bh_delete(qmp_dispatcher_bh
);
4748 qmp_dispatcher_bh
= NULL
;
4750 iothread_destroy(mon_iothread
);
4751 mon_iothread
= NULL
;
4755 QemuOptsList qemu_mon_opts
= {
4757 .implied_opt_name
= "chardev",
4758 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4762 .type
= QEMU_OPT_STRING
,
4765 .type
= QEMU_OPT_STRING
,
4768 .type
= QEMU_OPT_BOOL
,
4770 { /* end of list */ }
4774 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4776 MachineState
*ms
= MACHINE(qdev_get_machine());
4777 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4779 if (!mc
->has_hotpluggable_cpus
) {
4780 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4784 return machine_query_hotpluggable_cpus(ms
);