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/readline.h"
46 #include "ui/console.h"
48 #include "sysemu/block-backend.h"
49 #include "audio/audio.h"
50 #include "disas/disas.h"
51 #include "sysemu/balloon.h"
52 #include "qemu/timer.h"
53 #include "sysemu/hw_accel.h"
55 #include "sysemu/tpm.h"
56 #include "qapi/qmp/qdict.h"
57 #include "qapi/qmp/qerror.h"
58 #include "qapi/qmp/qnum.h"
59 #include "qapi/qmp/qstring.h"
60 #include "qapi/qmp/qjson.h"
61 #include "qapi/qmp/json-streamer.h"
62 #include "qapi/qmp/json-parser.h"
63 #include "qapi/qmp/qlist.h"
64 #include "qom/object_interfaces.h"
65 #include "trace-root.h"
66 #include "trace/control.h"
67 #include "monitor/hmp-target.h"
68 #ifdef CONFIG_TRACE_SIMPLE
69 #include "trace/simple.h"
71 #include "exec/memory.h"
72 #include "exec/exec-all.h"
74 #include "qemu/option.h"
76 #include "qemu/thread.h"
77 #include "block/qapi.h"
78 #include "qapi/qapi-commands.h"
79 #include "qapi/qapi-events.h"
80 #include "qapi/error.h"
81 #include "qapi/qmp-event.h"
82 #include "qapi/qapi-introspect.h"
83 #include "sysemu/qtest.h"
84 #include "sysemu/cpus.h"
85 #include "sysemu/iothread.h"
86 #include "qemu/cutils.h"
88 #if defined(TARGET_S390X)
89 #include "hw/s390x/storage-keys.h"
90 #include "hw/s390x/storage-attributes.h"
97 * 'B' block device name
98 * 's' string (accept optional quote)
99 * 'S' it just appends the rest of the string (accept optional quote)
100 * 'O' option string of the form NAME=VALUE,...
101 * parsed according to QemuOptsList given by its name
102 * Example: 'device:O' uses qemu_device_opts.
103 * Restriction: only lists with empty desc are supported
104 * TODO lift the restriction
106 * 'l' target long (32 or 64 bit)
107 * 'M' Non-negative target long (32 or 64 bit), in user mode the
108 * value is multiplied by 2^20 (think Mebibyte)
109 * 'o' octets (aka bytes)
110 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
111 * K, k suffix, which multiplies the value by 2^60 for suffixes E
112 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
113 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
115 * user mode accepts an optional ms, us, ns suffix,
116 * which divides the value by 1e3, 1e6, 1e9, respectively
117 * '/' optional gdb-like print format (like "/10x")
119 * '?' optional type (for all types, except '/')
120 * '.' other form of optional type (for 'i' and 'l')
122 * user mode accepts "on" or "off"
123 * '-' optional parameter (eg. '-f')
127 typedef struct mon_cmd_t
{
129 const char *args_type
;
132 const char *flags
; /* p=preconfig */
133 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
134 /* @sub_table is a list of 2nd level of commands. If it does not exist,
135 * cmd should be used. If it exists, sub_table[?].cmd should be
136 * used, and cmd of 1st level plays the role of help function.
138 struct mon_cmd_t
*sub_table
;
139 void (*command_completion
)(ReadLineState
*rs
, int nb_args
, const char *str
);
142 /* file descriptors passed via SCM_RIGHTS */
143 typedef struct mon_fd_t mon_fd_t
;
147 QLIST_ENTRY(mon_fd_t
) next
;
150 /* file descriptor associated with a file descriptor set */
151 typedef struct MonFdsetFd MonFdsetFd
;
156 QLIST_ENTRY(MonFdsetFd
) next
;
159 /* file descriptor set containing fds passed via SCM_RIGHTS */
160 typedef struct MonFdset MonFdset
;
163 QLIST_HEAD(, MonFdsetFd
) fds
;
164 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
165 QLIST_ENTRY(MonFdset
) next
;
169 JSONMessageParser parser
;
171 * When a client connects, we're in capabilities negotiation mode.
172 * @commands is &qmp_cap_negotiation_commands then. When command
173 * qmp_capabilities succeeds, we go into command mode, and
174 * @command becomes &qmp_commands.
176 QmpCommandList
*commands
;
177 bool capab_offered
[QMP_CAPABILITY__MAX
]; /* capabilities offered */
178 bool capab
[QMP_CAPABILITY__MAX
]; /* offered and accepted */
180 * Protects qmp request/response queue.
181 * Take monitor_lock first when you need both.
183 QemuMutex qmp_queue_lock
;
184 /* Input queue that holds all the parsed QMP requests */
185 GQueue
*qmp_requests
;
186 /* Output queue contains all the QMP responses in order */
187 GQueue
*qmp_responses
;
191 * To prevent flooding clients, events can be throttled. The
192 * throttling is calculated globally, rather than per-Monitor
195 typedef struct MonitorQAPIEventState
{
196 QAPIEvent event
; /* Throttling state for this event type and... */
197 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
198 QEMUTimer
*timer
; /* Timer for handling delayed events */
199 QDict
*qdict
; /* Delayed event (if any) */
200 } MonitorQAPIEventState
;
203 int64_t rate
; /* Minimum time (in ns) between two events */
204 } MonitorQAPIEventConf
;
210 int suspend_cnt
; /* Needs to be accessed atomically */
215 * State used only in the thread "owning" the monitor.
216 * If @use_io_thread, this is @mon_iothread.
217 * Else, it's the main thread.
218 * These members can be safely accessed without locks.
224 BlockCompletionFunc
*password_completion_cb
;
225 void *password_opaque
;
226 mon_cmd_t
*cmd_table
;
227 QTAILQ_ENTRY(Monitor
) entry
;
230 * The per-monitor lock. We can't access guest memory when holding
236 * Members that are protected by the per-monitor lock
238 QLIST_HEAD(, mon_fd_t
) fds
;
241 /* Read under either BQL or mon_lock, written with BQL+mon_lock. */
245 /* Shared monitor I/O thread */
246 IOThread
*mon_iothread
;
248 /* Bottom half to dispatch the requests received from I/O thread */
249 QEMUBH
*qmp_dispatcher_bh
;
251 /* Bottom half to deliver the responses back to clients */
252 QEMUBH
*qmp_respond_bh
;
255 /* Owner of the request */
257 /* "id" field of the request */
260 * Request object to be handled or Error to be reported
261 * (exactly one of them is non-null)
266 * Whether we need to resume the monitor afterward. This flag is
267 * used to emulate the old QMP server behavior that the current
268 * command must be completed before execution of the next one.
272 typedef struct QMPRequest QMPRequest
;
274 /* QMP checker flags */
275 #define QMP_ACCEPT_UNKNOWNS 1
277 /* Protects mon_list, monitor_qapi_event_state. */
278 static QemuMutex monitor_lock
;
279 static GHashTable
*monitor_qapi_event_state
;
280 static QTAILQ_HEAD(mon_list
, Monitor
) mon_list
;
282 /* Protects mon_fdsets */
283 static QemuMutex mon_fdsets_lock
;
284 static QLIST_HEAD(mon_fdsets
, MonFdset
) mon_fdsets
;
286 static int mon_refcount
;
288 static mon_cmd_t mon_cmds
[];
289 static mon_cmd_t info_cmds
[];
291 QmpCommandList qmp_commands
, qmp_cap_negotiation_commands
;
295 static void monitor_command_cb(void *opaque
, const char *cmdline
,
296 void *readline_opaque
);
299 * Is @mon a QMP monitor?
301 static inline bool monitor_is_qmp(const Monitor
*mon
)
303 return (mon
->flags
& MONITOR_USE_CONTROL
);
307 * Is @mon is using readline?
308 * Note: not all HMP monitors use readline, e.g., gdbserver has a
309 * non-interactive HMP monitor, so readline is not used there.
311 static inline bool monitor_uses_readline(const Monitor
*mon
)
313 return mon
->flags
& MONITOR_USE_READLINE
;
316 static inline bool monitor_is_hmp_non_interactive(const Monitor
*mon
)
318 return !monitor_is_qmp(mon
) && !monitor_uses_readline(mon
);
322 * Return the clock to use for recording an event's time.
323 * It's QEMU_CLOCK_REALTIME, except for qtests it's
324 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
325 * Beware: result is invalid before configure_accelerator().
327 static inline QEMUClockType
monitor_get_event_clock(void)
329 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL
: QEMU_CLOCK_REALTIME
;
333 * Is the current monitor, if any, a QMP monitor?
335 bool monitor_cur_is_qmp(void)
337 return cur_mon
&& monitor_is_qmp(cur_mon
);
340 void monitor_read_command(Monitor
*mon
, int show_prompt
)
345 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
347 readline_show_prompt(mon
->rs
);
350 int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
354 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
355 /* prompt is printed on return from the command handler */
358 monitor_printf(mon
, "terminal does not support password prompting\n");
363 static void qmp_request_free(QMPRequest
*req
)
365 qobject_unref(req
->id
);
366 qobject_unref(req
->req
);
367 error_free(req
->err
);
371 /* Caller must hold mon->qmp.qmp_queue_lock */
372 static void monitor_qmp_cleanup_req_queue_locked(Monitor
*mon
)
374 while (!g_queue_is_empty(mon
->qmp
.qmp_requests
)) {
375 qmp_request_free(g_queue_pop_head(mon
->qmp
.qmp_requests
));
379 /* Caller must hold the mon->qmp.qmp_queue_lock */
380 static void monitor_qmp_cleanup_resp_queue_locked(Monitor
*mon
)
382 while (!g_queue_is_empty(mon
->qmp
.qmp_responses
)) {
383 qobject_unref((QDict
*)g_queue_pop_head(mon
->qmp
.qmp_responses
));
387 static void monitor_qmp_cleanup_queues(Monitor
*mon
)
389 qemu_mutex_lock(&mon
->qmp
.qmp_queue_lock
);
390 monitor_qmp_cleanup_req_queue_locked(mon
);
391 monitor_qmp_cleanup_resp_queue_locked(mon
);
392 qemu_mutex_unlock(&mon
->qmp
.qmp_queue_lock
);
396 static void monitor_flush_locked(Monitor
*mon
);
398 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
401 Monitor
*mon
= opaque
;
403 qemu_mutex_lock(&mon
->mon_lock
);
405 monitor_flush_locked(mon
);
406 qemu_mutex_unlock(&mon
->mon_lock
);
410 /* Caller must hold mon->mon_lock */
411 static void monitor_flush_locked(Monitor
*mon
)
417 if (mon
->skip_flush
) {
421 buf
= qstring_get_str(mon
->outbuf
);
422 len
= qstring_get_length(mon
->outbuf
);
424 if (len
&& !mon
->mux_out
) {
425 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
426 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
427 /* all flushed or error */
428 qobject_unref(mon
->outbuf
);
429 mon
->outbuf
= qstring_new();
434 QString
*tmp
= qstring_from_str(buf
+ rc
);
435 qobject_unref(mon
->outbuf
);
438 if (mon
->out_watch
== 0) {
440 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
441 monitor_unblocked
, mon
);
446 void monitor_flush(Monitor
*mon
)
448 qemu_mutex_lock(&mon
->mon_lock
);
449 monitor_flush_locked(mon
);
450 qemu_mutex_unlock(&mon
->mon_lock
);
453 /* flush at every end of line */
454 static void monitor_puts(Monitor
*mon
, const char *str
)
458 qemu_mutex_lock(&mon
->mon_lock
);
464 qstring_append_chr(mon
->outbuf
, '\r');
466 qstring_append_chr(mon
->outbuf
, c
);
468 monitor_flush_locked(mon
);
471 qemu_mutex_unlock(&mon
->mon_lock
);
474 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
481 if (monitor_is_qmp(mon
)) {
485 buf
= g_strdup_vprintf(fmt
, ap
);
486 monitor_puts(mon
, buf
);
490 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
494 monitor_vprintf(mon
, fmt
, ap
);
498 int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
502 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
507 static void qmp_send_response(Monitor
*mon
, QDict
*rsp
)
509 QObject
*data
= QOBJECT(rsp
);
512 json
= mon
->flags
& MONITOR_USE_PRETTY
? qobject_to_json_pretty(data
) :
513 qobject_to_json(data
);
514 assert(json
!= NULL
);
516 qstring_append_chr(json
, '\n');
517 monitor_puts(mon
, qstring_get_str(json
));
522 static void qmp_queue_response(Monitor
*mon
, QDict
*rsp
)
524 if (mon
->use_io_thread
) {
526 * Push a reference to the response queue. The I/O thread
527 * drains that queue and emits.
529 qemu_mutex_lock(&mon
->qmp
.qmp_queue_lock
);
530 g_queue_push_tail(mon
->qmp
.qmp_responses
, qobject_ref(rsp
));
531 qemu_mutex_unlock(&mon
->qmp
.qmp_queue_lock
);
532 qemu_bh_schedule(qmp_respond_bh
);
535 * Not using monitor I/O thread, i.e. we are in the main thread.
538 qmp_send_response(mon
, rsp
);
546 typedef struct QMPResponse QMPResponse
;
548 static QDict
*monitor_qmp_response_pop_one(Monitor
*mon
)
552 qemu_mutex_lock(&mon
->qmp
.qmp_queue_lock
);
553 data
= g_queue_pop_head(mon
->qmp
.qmp_responses
);
554 qemu_mutex_unlock(&mon
->qmp
.qmp_queue_lock
);
559 static void monitor_qmp_response_flush(Monitor
*mon
)
563 while ((data
= monitor_qmp_response_pop_one(mon
))) {
564 qmp_send_response(mon
, data
);
570 * Pop a QMPResponse from any monitor's response queue into @response.
571 * Return false if all the queues are empty; else true.
573 static bool monitor_qmp_response_pop_any(QMPResponse
*response
)
578 qemu_mutex_lock(&monitor_lock
);
579 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
580 data
= monitor_qmp_response_pop_one(mon
);
583 response
->data
= data
;
587 qemu_mutex_unlock(&monitor_lock
);
591 static void monitor_qmp_bh_responder(void *opaque
)
593 QMPResponse response
;
595 while (monitor_qmp_response_pop_any(&response
)) {
596 qmp_send_response(response
.mon
, response
.data
);
597 qobject_unref(response
.data
);
601 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
602 /* Limit guest-triggerable events to 1 per second */
603 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
604 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
605 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
606 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
607 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
608 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
612 * Broadcast an event to all monitors.
613 * @qdict is the event object. Its member "event" must match @event.
614 * Caller must hold monitor_lock.
616 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
620 trace_monitor_protocol_event_emit(event
, qdict
);
621 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
622 if (monitor_is_qmp(mon
)
623 && mon
->qmp
.commands
!= &qmp_cap_negotiation_commands
) {
624 qmp_queue_response(mon
, qdict
);
629 static void monitor_qapi_event_handler(void *opaque
);
632 * Queue a new event for emission to Monitor instances,
633 * applying any rate limiting if required.
636 monitor_qapi_event_queue(QAPIEvent event
, QDict
*qdict
, Error
**errp
)
638 MonitorQAPIEventConf
*evconf
;
639 MonitorQAPIEventState
*evstate
;
641 assert(event
< QAPI_EVENT__MAX
);
642 evconf
= &monitor_qapi_event_conf
[event
];
643 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
645 qemu_mutex_lock(&monitor_lock
);
648 /* Unthrottled event */
649 monitor_qapi_event_emit(event
, qdict
);
651 QDict
*data
= qobject_to(QDict
, qdict_get(qdict
, "data"));
652 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
654 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
655 assert(!evstate
|| timer_pending(evstate
->timer
));
659 * Timer is pending for (at least) evconf->rate ns after
660 * last send. Store event for sending when timer fires,
661 * replacing a prior stored event if any.
663 qobject_unref(evstate
->qdict
);
664 evstate
->qdict
= qobject_ref(qdict
);
667 * Last send was (at least) evconf->rate ns ago.
668 * Send immediately, and arm the timer to call
669 * monitor_qapi_event_handler() in evconf->rate ns. Any
670 * events arriving before then will be delayed until then.
672 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
674 monitor_qapi_event_emit(event
, qdict
);
676 evstate
= g_new(MonitorQAPIEventState
, 1);
677 evstate
->event
= event
;
678 evstate
->data
= qobject_ref(data
);
679 evstate
->qdict
= NULL
;
680 evstate
->timer
= timer_new_ns(monitor_get_event_clock(),
681 monitor_qapi_event_handler
,
683 g_hash_table_add(monitor_qapi_event_state
, evstate
);
684 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
688 qemu_mutex_unlock(&monitor_lock
);
692 * This function runs evconf->rate ns after sending a throttled
694 * If another event has since been stored, send it.
696 static void monitor_qapi_event_handler(void *opaque
)
698 MonitorQAPIEventState
*evstate
= opaque
;
699 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
701 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
702 qemu_mutex_lock(&monitor_lock
);
704 if (evstate
->qdict
) {
705 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
707 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
708 qobject_unref(evstate
->qdict
);
709 evstate
->qdict
= NULL
;
710 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
712 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
713 qobject_unref(evstate
->data
);
714 timer_free(evstate
->timer
);
718 qemu_mutex_unlock(&monitor_lock
);
721 static unsigned int qapi_event_throttle_hash(const void *key
)
723 const MonitorQAPIEventState
*evstate
= key
;
724 unsigned int hash
= evstate
->event
* 255;
726 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
727 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
730 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
731 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
737 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
739 const MonitorQAPIEventState
*eva
= a
;
740 const MonitorQAPIEventState
*evb
= b
;
742 if (eva
->event
!= evb
->event
) {
746 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
747 return !strcmp(qdict_get_str(eva
->data
, "id"),
748 qdict_get_str(evb
->data
, "id"));
751 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
752 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
753 qdict_get_str(evb
->data
, "node-name"));
759 static void monitor_qapi_event_init(void)
761 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
762 qapi_event_throttle_equal
);
763 qmp_event_set_func_emit(monitor_qapi_event_queue
);
766 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
);
768 static void monitor_data_init(Monitor
*mon
, bool skip_flush
,
771 memset(mon
, 0, sizeof(Monitor
));
772 qemu_mutex_init(&mon
->mon_lock
);
773 qemu_mutex_init(&mon
->qmp
.qmp_queue_lock
);
774 mon
->outbuf
= qstring_new();
775 /* Use *mon_cmds by default. */
776 mon
->cmd_table
= mon_cmds
;
777 mon
->skip_flush
= skip_flush
;
778 mon
->use_io_thread
= use_io_thread
;
779 mon
->qmp
.qmp_requests
= g_queue_new();
780 mon
->qmp
.qmp_responses
= g_queue_new();
783 static void monitor_data_destroy(Monitor
*mon
)
785 g_free(mon
->mon_cpu_path
);
786 qemu_chr_fe_deinit(&mon
->chr
, false);
787 if (monitor_is_qmp(mon
)) {
788 json_message_parser_destroy(&mon
->qmp
.parser
);
790 readline_free(mon
->rs
);
791 qobject_unref(mon
->outbuf
);
792 qemu_mutex_destroy(&mon
->mon_lock
);
793 qemu_mutex_destroy(&mon
->qmp
.qmp_queue_lock
);
794 monitor_qmp_cleanup_req_queue_locked(mon
);
795 monitor_qmp_cleanup_resp_queue_locked(mon
);
796 g_queue_free(mon
->qmp
.qmp_requests
);
797 g_queue_free(mon
->qmp
.qmp_responses
);
800 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
801 int64_t cpu_index
, Error
**errp
)
804 Monitor
*old_mon
, hmp
;
806 monitor_data_init(&hmp
, true, false);
812 int ret
= monitor_set_cpu(cpu_index
);
815 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
821 handle_hmp_command(&hmp
, command_line
);
824 qemu_mutex_lock(&hmp
.mon_lock
);
825 if (qstring_get_length(hmp
.outbuf
) > 0) {
826 output
= g_strdup(qstring_get_str(hmp
.outbuf
));
828 output
= g_strdup("");
830 qemu_mutex_unlock(&hmp
.mon_lock
);
833 monitor_data_destroy(&hmp
);
837 static int compare_cmd(const char *name
, const char *list
)
839 const char *p
, *pstart
;
845 p
= qemu_strchrnul(p
, '|');
846 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
855 static int get_str(char *buf
, int buf_size
, const char **pp
)
863 while (qemu_isspace(*p
)) {
874 while (*p
!= '\0' && *p
!= '\"') {
890 printf("unsupported escape code: '\\%c'\n", c
);
893 if ((q
- buf
) < buf_size
- 1) {
897 if ((q
- buf
) < buf_size
- 1) {
904 printf("unterminated string\n");
909 while (*p
!= '\0' && !qemu_isspace(*p
)) {
910 if ((q
- buf
) < buf_size
- 1) {
923 static void free_cmdline_args(char **args
, int nb_args
)
927 assert(nb_args
<= MAX_ARGS
);
929 for (i
= 0; i
< nb_args
; i
++) {
936 * Parse the command line to get valid args.
937 * @cmdline: command line to be parsed.
938 * @pnb_args: location to store the number of args, must NOT be NULL.
939 * @args: location to store the args, which should be freed by caller, must
942 * Returns 0 on success, negative on failure.
944 * NOTE: this parser is an approximate form of the real command parser. Number
945 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
946 * return with failure.
948 static int parse_cmdline(const char *cmdline
,
949 int *pnb_args
, char **args
)
958 while (qemu_isspace(*p
)) {
964 if (nb_args
>= MAX_ARGS
) {
967 ret
= get_str(buf
, sizeof(buf
), &p
);
971 args
[nb_args
] = g_strdup(buf
);
978 free_cmdline_args(args
, nb_args
);
983 * Can command @cmd be executed in preconfig state?
985 static bool cmd_can_preconfig(const mon_cmd_t
*cmd
)
991 return strchr(cmd
->flags
, 'p');
994 static void help_cmd_dump_one(Monitor
*mon
,
995 const mon_cmd_t
*cmd
,
1001 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
1005 for (i
= 0; i
< prefix_args_nb
; i
++) {
1006 monitor_printf(mon
, "%s ", prefix_args
[i
]);
1008 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
1011 /* @args[@arg_index] is the valid command need to find in @cmds */
1012 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
1013 char **args
, int nb_args
, int arg_index
)
1015 const mon_cmd_t
*cmd
;
1017 /* No valid arg need to compare with, dump all in *cmds */
1018 if (arg_index
>= nb_args
) {
1019 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
1020 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
1025 /* Find one entry to dump */
1026 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
1027 if (compare_cmd(args
[arg_index
], cmd
->name
) &&
1028 ((!runstate_check(RUN_STATE_PRECONFIG
) ||
1029 cmd_can_preconfig(cmd
)))) {
1030 if (cmd
->sub_table
) {
1031 /* continue with next arg */
1032 help_cmd_dump(mon
, cmd
->sub_table
,
1033 args
, nb_args
, arg_index
+ 1);
1035 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
1042 static void help_cmd(Monitor
*mon
, const char *name
)
1044 char *args
[MAX_ARGS
];
1047 /* 1. parse user input */
1049 /* special case for log, directly dump and return */
1050 if (!strcmp(name
, "log")) {
1051 const QEMULogItem
*item
;
1052 monitor_printf(mon
, "Log items (comma separated):\n");
1053 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
1054 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
1055 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
1060 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
1065 /* 2. dump the contents according to parsed args */
1066 help_cmd_dump(mon
, mon
->cmd_table
, args
, nb_args
, 0);
1068 free_cmdline_args(args
, nb_args
);
1071 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
1073 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
1076 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
1078 const char *tp_name
= qdict_get_str(qdict
, "name");
1079 bool new_state
= qdict_get_bool(qdict
, "option");
1080 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1081 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1082 Error
*local_err
= NULL
;
1085 monitor_printf(mon
, "argument vcpu must be positive");
1089 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
1091 error_report_err(local_err
);
1095 #ifdef CONFIG_TRACE_SIMPLE
1096 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
1098 const char *op
= qdict_get_try_str(qdict
, "op");
1099 const char *arg
= qdict_get_try_str(qdict
, "arg");
1102 st_print_trace_file_status((FILE *)mon
, &monitor_fprintf
);
1103 } else if (!strcmp(op
, "on")) {
1104 st_set_trace_file_enabled(true);
1105 } else if (!strcmp(op
, "off")) {
1106 st_set_trace_file_enabled(false);
1107 } else if (!strcmp(op
, "flush")) {
1108 st_flush_trace_buffer();
1109 } else if (!strcmp(op
, "set")) {
1111 st_set_trace_file(arg
);
1114 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
1115 help_cmd(mon
, "trace-file");
1120 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
1122 help_cmd(mon
, "info");
1125 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
1127 CommandInfoList
*info
, **list
= opaque
;
1129 if (!cmd
->enabled
) {
1133 info
= g_malloc0(sizeof(*info
));
1134 info
->value
= g_malloc0(sizeof(*info
->value
));
1135 info
->value
->name
= g_strdup(cmd
->name
);
1140 CommandInfoList
*qmp_query_commands(Error
**errp
)
1142 CommandInfoList
*list
= NULL
;
1144 qmp_for_each_command(cur_mon
->qmp
.commands
, query_commands_cb
, &list
);
1149 EventInfoList
*qmp_query_events(Error
**errp
)
1151 EventInfoList
*info
, *ev_list
= NULL
;
1154 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
1155 const char *event_name
= QAPIEvent_str(e
);
1156 assert(event_name
!= NULL
);
1157 info
= g_malloc0(sizeof(*info
));
1158 info
->value
= g_malloc0(sizeof(*info
->value
));
1159 info
->value
->name
= g_strdup(event_name
);
1161 info
->next
= ev_list
;
1169 * Minor hack: generated marshalling suppressed for this command
1170 * ('gen': false in the schema) so we can parse the JSON string
1171 * directly into QObject instead of first parsing it with
1172 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
1173 * to QObject with generated output marshallers, every time. Instead,
1174 * we do it in test-qobject-input-visitor.c, just to make sure
1175 * qapi-gen.py's output actually conforms to the schema.
1177 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
1180 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
1184 * We used to define commands in qmp-commands.hx in addition to the
1185 * QAPI schema. This permitted defining some of them only in certain
1186 * configurations. query-commands has always reflected that (good,
1187 * because it lets QMP clients figure out what's actually available),
1188 * while query-qmp-schema never did (not so good). This function is a
1189 * hack to keep the configuration-specific commands defined exactly as
1190 * before, even though qmp-commands.hx is gone.
1192 * FIXME Educate the QAPI schema on configuration-specific commands,
1193 * and drop this hack.
1195 static void qmp_unregister_commands_hack(void)
1197 #ifndef CONFIG_REPLICATION
1198 qmp_unregister_command(&qmp_commands
, "xen-set-replication");
1199 qmp_unregister_command(&qmp_commands
, "query-xen-replication-status");
1200 qmp_unregister_command(&qmp_commands
, "xen-colo-do-checkpoint");
1203 qmp_unregister_command(&qmp_commands
, "rtc-reset-reinjection");
1204 qmp_unregister_command(&qmp_commands
, "query-sev");
1205 qmp_unregister_command(&qmp_commands
, "query-sev-launch-measure");
1206 qmp_unregister_command(&qmp_commands
, "query-sev-capabilities");
1208 #ifndef TARGET_S390X
1209 qmp_unregister_command(&qmp_commands
, "dump-skeys");
1212 qmp_unregister_command(&qmp_commands
, "query-gic-capabilities");
1214 #if !defined(TARGET_S390X) && !defined(TARGET_I386)
1215 qmp_unregister_command(&qmp_commands
, "query-cpu-model-expansion");
1217 #if !defined(TARGET_S390X)
1218 qmp_unregister_command(&qmp_commands
, "query-cpu-model-baseline");
1219 qmp_unregister_command(&qmp_commands
, "query-cpu-model-comparison");
1221 #if !defined(TARGET_PPC) && !defined(TARGET_ARM) && !defined(TARGET_I386) \
1222 && !defined(TARGET_S390X)
1223 qmp_unregister_command(&qmp_commands
, "query-cpu-definitions");
1227 static void monitor_init_qmp_commands(void)
1230 * Two command lists:
1231 * - qmp_commands contains all QMP commands
1232 * - qmp_cap_negotiation_commands contains just
1233 * "qmp_capabilities", to enforce capability negotiation
1236 qmp_init_marshal(&qmp_commands
);
1238 qmp_register_command(&qmp_commands
, "query-qmp-schema",
1239 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
1240 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
1242 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
1245 qmp_unregister_commands_hack();
1247 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
1248 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
1249 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
1252 static bool qmp_oob_enabled(Monitor
*mon
)
1254 return mon
->qmp
.capab
[QMP_CAPABILITY_OOB
];
1257 static void monitor_qmp_caps_reset(Monitor
*mon
)
1259 memset(mon
->qmp
.capab_offered
, 0, sizeof(mon
->qmp
.capab_offered
));
1260 memset(mon
->qmp
.capab
, 0, sizeof(mon
->qmp
.capab
));
1261 mon
->qmp
.capab_offered
[QMP_CAPABILITY_OOB
] = mon
->use_io_thread
;
1265 * Accept QMP capabilities in @list for @mon.
1266 * On success, set mon->qmp.capab[], and return true.
1267 * On error, set @errp, and return false.
1269 static bool qmp_caps_accept(Monitor
*mon
, QMPCapabilityList
*list
,
1272 GString
*unavailable
= NULL
;
1273 bool capab
[QMP_CAPABILITY__MAX
];
1275 memset(capab
, 0, sizeof(capab
));
1277 for (; list
; list
= list
->next
) {
1278 if (!mon
->qmp
.capab_offered
[list
->value
]) {
1280 unavailable
= g_string_new(QMPCapability_str(list
->value
));
1282 g_string_append_printf(unavailable
, ", %s",
1283 QMPCapability_str(list
->value
));
1286 capab
[list
->value
] = true;
1290 error_setg(errp
, "Capability %s not available", unavailable
->str
);
1291 g_string_free(unavailable
, true);
1295 memcpy(mon
->qmp
.capab
, capab
, sizeof(capab
));
1299 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
1302 if (cur_mon
->qmp
.commands
== &qmp_commands
) {
1303 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
1304 "Capabilities negotiation is already complete, command "
1309 if (!qmp_caps_accept(cur_mon
, enable
, errp
)) {
1313 cur_mon
->qmp
.commands
= &qmp_commands
;
1316 /* Set the current CPU defined by the user. Callers must hold BQL. */
1317 int monitor_set_cpu(int cpu_index
)
1321 cpu
= qemu_get_cpu(cpu_index
);
1325 g_free(cur_mon
->mon_cpu_path
);
1326 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
1330 /* Callers must hold BQL. */
1331 static CPUState
*mon_get_cpu_sync(bool synchronize
)
1335 if (cur_mon
->mon_cpu_path
) {
1336 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
1339 g_free(cur_mon
->mon_cpu_path
);
1340 cur_mon
->mon_cpu_path
= NULL
;
1343 if (!cur_mon
->mon_cpu_path
) {
1347 monitor_set_cpu(first_cpu
->cpu_index
);
1351 cpu_synchronize_state(cpu
);
1356 CPUState
*mon_get_cpu(void)
1358 return mon_get_cpu_sync(true);
1361 CPUArchState
*mon_get_cpu_env(void)
1363 CPUState
*cs
= mon_get_cpu();
1365 return cs
? cs
->env_ptr
: NULL
;
1368 int monitor_get_cpu_index(void)
1370 CPUState
*cs
= mon_get_cpu_sync(false);
1372 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
1375 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1377 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
1382 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
1383 cpu_dump_state(cs
, (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1389 monitor_printf(mon
, "No CPU available\n");
1393 cpu_dump_state(cs
, (FILE *)mon
, monitor_fprintf
, CPU_DUMP_FPU
);
1398 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1400 if (!tcg_enabled()) {
1401 error_report("JIT information is only available with accel=tcg");
1405 dump_exec_info((FILE *)mon
, monitor_fprintf
);
1406 dump_drift_info((FILE *)mon
, monitor_fprintf
);
1409 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1411 dump_opcount_info((FILE *)mon
, monitor_fprintf
);
1415 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1424 str
= readline_get_history(mon
->rs
, i
);
1427 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1432 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1434 CPUState
*cs
= mon_get_cpu();
1437 monitor_printf(mon
, "No CPU available\n");
1440 cpu_dump_statistics(cs
, (FILE *)mon
, &monitor_fprintf
, 0);
1443 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1445 const char *name
= qdict_get_try_str(qdict
, "name");
1446 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1447 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1448 TraceEventInfoList
*events
;
1449 TraceEventInfoList
*elem
;
1450 Error
*local_err
= NULL
;
1456 monitor_printf(mon
, "argument vcpu must be positive");
1460 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1462 error_report_err(local_err
);
1466 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1467 monitor_printf(mon
, "%s : state %u\n",
1469 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1471 qapi_free_TraceEventInfoList(events
);
1474 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1475 bool has_port
, int64_t port
,
1476 bool has_tls_port
, int64_t tls_port
,
1477 bool has_cert_subject
, const char *cert_subject
,
1480 if (strcmp(protocol
, "spice") == 0) {
1481 if (!qemu_using_spice(errp
)) {
1485 if (!has_port
&& !has_tls_port
) {
1486 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1490 if (qemu_spice_migrate_info(hostname
,
1491 has_port
? port
: -1,
1492 has_tls_port
? tls_port
: -1,
1494 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1500 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1503 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1507 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1509 error_report_err(err
);
1513 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1516 const char *items
= qdict_get_str(qdict
, "items");
1518 if (!strcmp(items
, "none")) {
1521 mask
= qemu_str_to_log_mask(items
);
1523 help_cmd(mon
, "log");
1530 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1532 const char *option
= qdict_get_try_str(qdict
, "option");
1533 if (!option
|| !strcmp(option
, "on")) {
1535 } else if (!strcmp(option
, "off")) {
1538 monitor_printf(mon
, "unexpected option %s\n", option
);
1542 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1544 const char *device
= qdict_get_try_str(qdict
, "device");
1546 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1547 if (gdbserver_start(device
) < 0) {
1548 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1550 } else if (strcmp(device
, "none") == 0) {
1551 monitor_printf(mon
, "Disabled gdbserver\n");
1553 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1558 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1560 const char *action
= qdict_get_str(qdict
, "action");
1561 if (select_watchdog_action(action
) == -1) {
1562 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1566 static void monitor_printc(Monitor
*mon
, int c
)
1568 monitor_printf(mon
, "'");
1571 monitor_printf(mon
, "\\'");
1574 monitor_printf(mon
, "\\\\");
1577 monitor_printf(mon
, "\\n");
1580 monitor_printf(mon
, "\\r");
1583 if (c
>= 32 && c
<= 126) {
1584 monitor_printf(mon
, "%c", c
);
1586 monitor_printf(mon
, "\\x%02x", c
);
1590 monitor_printf(mon
, "'");
1593 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1594 hwaddr addr
, int is_physical
)
1596 int l
, line_size
, i
, max_digits
, len
;
1599 CPUState
*cs
= mon_get_cpu();
1601 if (!cs
&& (format
== 'i' || !is_physical
)) {
1602 monitor_printf(mon
, "Can not dump without CPU\n");
1606 if (format
== 'i') {
1607 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
1611 len
= wsize
* count
;
1620 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
1624 max_digits
= (wsize
* 8) / 4;
1628 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
1637 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1639 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1644 cpu_physical_memory_read(addr
, buf
, l
);
1646 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
1647 monitor_printf(mon
, " Cannot access memory\n");
1656 v
= ldub_p(buf
+ i
);
1659 v
= lduw_p(buf
+ i
);
1662 v
= (uint32_t)ldl_p(buf
+ i
);
1668 monitor_printf(mon
, " ");
1671 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1674 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1677 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1680 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1683 monitor_printc(mon
, v
);
1688 monitor_printf(mon
, "\n");
1694 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1696 int count
= qdict_get_int(qdict
, "count");
1697 int format
= qdict_get_int(qdict
, "format");
1698 int size
= qdict_get_int(qdict
, "size");
1699 target_long addr
= qdict_get_int(qdict
, "addr");
1701 memory_dump(mon
, count
, format
, size
, addr
, 0);
1704 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1706 int count
= qdict_get_int(qdict
, "count");
1707 int format
= qdict_get_int(qdict
, "format");
1708 int size
= qdict_get_int(qdict
, "size");
1709 hwaddr addr
= qdict_get_int(qdict
, "addr");
1711 memory_dump(mon
, count
, format
, size
, addr
, 1);
1714 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
1716 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
1720 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
1724 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
1725 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
1726 memory_region_unref(mrs
.mr
);
1731 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
1734 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
1736 hwaddr addr
= qdict_get_int(qdict
, "addr");
1737 Error
*local_err
= NULL
;
1738 MemoryRegion
*mr
= NULL
;
1741 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1743 error_report_err(local_err
);
1747 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
1749 addr
, mr
->name
, ptr
);
1751 memory_region_unref(mr
);
1755 static uint64_t vtop(void *ptr
, Error
**errp
)
1759 uintptr_t addr
= (uintptr_t) ptr
;
1760 uintptr_t pagesize
= getpagesize();
1761 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
1764 fd
= open("/proc/self/pagemap", O_RDONLY
);
1766 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
1770 /* Force copy-on-write if necessary. */
1771 atomic_add((uint8_t *)ptr
, 0);
1773 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
1774 error_setg_errno(errp
, errno
, "Cannot read pagemap");
1777 if ((pinfo
& (1ull << 63)) == 0) {
1778 error_setg(errp
, "Page not present");
1781 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
1788 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
1790 hwaddr addr
= qdict_get_int(qdict
, "addr");
1791 Error
*local_err
= NULL
;
1792 MemoryRegion
*mr
= NULL
;
1796 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1798 error_report_err(local_err
);
1802 physaddr
= vtop(ptr
, &local_err
);
1804 error_report_err(local_err
);
1806 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
1807 " (%s) is 0x%" PRIx64
"\n",
1808 addr
, mr
->name
, (uint64_t) physaddr
);
1811 memory_region_unref(mr
);
1815 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1817 int format
= qdict_get_int(qdict
, "format");
1818 hwaddr val
= qdict_get_int(qdict
, "val");
1822 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1825 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1828 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1832 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1835 monitor_printc(mon
, val
);
1838 monitor_printf(mon
, "\n");
1841 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1845 uint32_t start
= qdict_get_int(qdict
, "start");
1846 uint32_t size
= qdict_get_int(qdict
, "size");
1849 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1850 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1851 MEMTXATTRS_UNSPECIFIED
, NULL
);
1852 /* BSD sum algorithm ('sum' Unix command) */
1853 sum
= (sum
>> 1) | (sum
<< 15);
1856 monitor_printf(mon
, "%05d\n", sum
);
1859 static int mouse_button_state
;
1861 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1863 int dx
, dy
, dz
, button
;
1864 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1865 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1866 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1868 dx
= strtol(dx_str
, NULL
, 0);
1869 dy
= strtol(dy_str
, NULL
, 0);
1870 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1871 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1874 dz
= strtol(dz_str
, NULL
, 0);
1876 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1877 qemu_input_queue_btn(NULL
, button
, true);
1878 qemu_input_event_sync();
1879 qemu_input_queue_btn(NULL
, button
, false);
1882 qemu_input_event_sync();
1885 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1887 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1888 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1889 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1890 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1892 int button_state
= qdict_get_int(qdict
, "button_state");
1894 if (mouse_button_state
== button_state
) {
1897 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1898 qemu_input_event_sync();
1899 mouse_button_state
= button_state
;
1902 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1904 int size
= qdict_get_int(qdict
, "size");
1905 int addr
= qdict_get_int(qdict
, "addr");
1906 int has_index
= qdict_haskey(qdict
, "index");
1911 int index
= qdict_get_int(qdict
, "index");
1912 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1920 val
= cpu_inb(addr
);
1924 val
= cpu_inw(addr
);
1928 val
= cpu_inl(addr
);
1932 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1933 suffix
, addr
, size
* 2, val
);
1936 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1938 int size
= qdict_get_int(qdict
, "size");
1939 int addr
= qdict_get_int(qdict
, "addr");
1940 int val
= qdict_get_int(qdict
, "val");
1942 addr
&= IOPORTS_MASK
;
1947 cpu_outb(addr
, val
);
1950 cpu_outw(addr
, val
);
1953 cpu_outl(addr
, val
);
1958 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1960 Error
*local_err
= NULL
;
1961 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1963 qemu_boot_set(bootdevice
, &local_err
);
1965 error_report_err(local_err
);
1967 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1971 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1973 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1974 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1975 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1977 mtree_info((fprintf_function
)monitor_printf
, mon
, flatview
, dispatch_tree
,
1981 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1984 NumaNodeMem
*node_mem
;
1985 CpuInfoList
*cpu_list
, *cpu
;
1987 cpu_list
= qmp_query_cpus(&error_abort
);
1988 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
1990 query_numa_node_mem(node_mem
);
1991 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1992 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1993 monitor_printf(mon
, "node %d cpus:", i
);
1994 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
1995 if (cpu
->value
->has_props
&& cpu
->value
->props
->has_node_id
&&
1996 cpu
->value
->props
->node_id
== i
) {
1997 monitor_printf(mon
, " %" PRIi64
, cpu
->value
->CPU
);
2000 monitor_printf(mon
, "\n");
2001 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
2002 node_mem
[i
].node_mem
>> 20);
2003 monitor_printf(mon
, "node %d plugged: %" PRId64
" MB\n", i
,
2004 node_mem
[i
].node_plugged_mem
>> 20);
2006 qapi_free_CpuInfoList(cpu_list
);
2010 #ifdef CONFIG_PROFILER
2015 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
2017 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
2018 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
2019 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
2020 tcg_time
, tcg_time
/ (double)NANOSECONDS_PER_SECOND
);
2025 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
2027 monitor_printf(mon
, "Internal profiler not compiled\n");
2031 /* Capture support */
2032 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
2034 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
2039 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2040 monitor_printf(mon
, "[%d]: ", i
);
2041 s
->ops
.info (s
->opaque
);
2045 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
2048 int n
= qdict_get_int(qdict
, "n");
2051 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
2053 s
->ops
.destroy (s
->opaque
);
2054 QLIST_REMOVE (s
, entries
);
2061 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
2063 const char *path
= qdict_get_str(qdict
, "path");
2064 int has_freq
= qdict_haskey(qdict
, "freq");
2065 int freq
= qdict_get_try_int(qdict
, "freq", -1);
2066 int has_bits
= qdict_haskey(qdict
, "bits");
2067 int bits
= qdict_get_try_int(qdict
, "bits", -1);
2068 int has_channels
= qdict_haskey(qdict
, "nchannels");
2069 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
2072 s
= g_malloc0 (sizeof (*s
));
2074 freq
= has_freq
? freq
: 44100;
2075 bits
= has_bits
? bits
: 16;
2076 nchannels
= has_channels
? nchannels
: 2;
2078 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
2079 monitor_printf(mon
, "Failed to add wave capture\n");
2083 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
2086 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
2088 qemu_acl
*acl
= qemu_acl_find(name
);
2091 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
2096 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
2098 const char *aclname
= qdict_get_str(qdict
, "aclname");
2099 qemu_acl
*acl
= find_acl(mon
, aclname
);
2100 qemu_acl_entry
*entry
;
2104 monitor_printf(mon
, "policy: %s\n",
2105 acl
->defaultDeny
? "deny" : "allow");
2106 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
2108 monitor_printf(mon
, "%d: %s %s\n", i
,
2109 entry
->deny
? "deny" : "allow", entry
->match
);
2114 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
2116 const char *aclname
= qdict_get_str(qdict
, "aclname");
2117 qemu_acl
*acl
= find_acl(mon
, aclname
);
2120 qemu_acl_reset(acl
);
2121 monitor_printf(mon
, "acl: removed all rules\n");
2125 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
2127 const char *aclname
= qdict_get_str(qdict
, "aclname");
2128 const char *policy
= qdict_get_str(qdict
, "policy");
2129 qemu_acl
*acl
= find_acl(mon
, aclname
);
2132 if (strcmp(policy
, "allow") == 0) {
2133 acl
->defaultDeny
= 0;
2134 monitor_printf(mon
, "acl: policy set to 'allow'\n");
2135 } else if (strcmp(policy
, "deny") == 0) {
2136 acl
->defaultDeny
= 1;
2137 monitor_printf(mon
, "acl: policy set to 'deny'\n");
2139 monitor_printf(mon
, "acl: unknown policy '%s', "
2140 "expected 'deny' or 'allow'\n", policy
);
2145 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
2147 const char *aclname
= qdict_get_str(qdict
, "aclname");
2148 const char *match
= qdict_get_str(qdict
, "match");
2149 const char *policy
= qdict_get_str(qdict
, "policy");
2150 int has_index
= qdict_haskey(qdict
, "index");
2151 int index
= qdict_get_try_int(qdict
, "index", -1);
2152 qemu_acl
*acl
= find_acl(mon
, aclname
);
2156 if (strcmp(policy
, "allow") == 0) {
2158 } else if (strcmp(policy
, "deny") == 0) {
2161 monitor_printf(mon
, "acl: unknown policy '%s', "
2162 "expected 'deny' or 'allow'\n", policy
);
2166 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
2168 ret
= qemu_acl_append(acl
, deny
, match
);
2170 monitor_printf(mon
, "acl: unable to add acl entry\n");
2172 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
2176 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2178 const char *aclname
= qdict_get_str(qdict
, "aclname");
2179 const char *match
= qdict_get_str(qdict
, "match");
2180 qemu_acl
*acl
= find_acl(mon
, aclname
);
2184 ret
= qemu_acl_remove(acl
, match
);
2186 monitor_printf(mon
, "acl: no matching acl entry\n");
2188 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
2192 void qmp_getfd(const char *fdname
, Error
**errp
)
2197 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
2199 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2203 if (qemu_isdigit(fdname
[0])) {
2205 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2206 "a name not starting with a digit");
2210 qemu_mutex_lock(&cur_mon
->mon_lock
);
2211 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2212 if (strcmp(monfd
->name
, fdname
) != 0) {
2218 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2219 /* Make sure close() is outside critical section */
2224 monfd
= g_malloc0(sizeof(mon_fd_t
));
2225 monfd
->name
= g_strdup(fdname
);
2228 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2229 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2232 void qmp_closefd(const char *fdname
, Error
**errp
)
2237 qemu_mutex_lock(&cur_mon
->mon_lock
);
2238 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2239 if (strcmp(monfd
->name
, fdname
) != 0) {
2243 QLIST_REMOVE(monfd
, next
);
2245 g_free(monfd
->name
);
2247 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2248 /* Make sure close() is outside critical section */
2253 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2254 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2257 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2261 qemu_mutex_lock(&mon
->mon_lock
);
2262 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2265 if (strcmp(monfd
->name
, fdname
) != 0) {
2271 /* caller takes ownership of fd */
2272 QLIST_REMOVE(monfd
, next
);
2273 g_free(monfd
->name
);
2275 qemu_mutex_unlock(&mon
->mon_lock
);
2280 qemu_mutex_unlock(&mon
->mon_lock
);
2281 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2285 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2287 MonFdsetFd
*mon_fdset_fd
;
2288 MonFdsetFd
*mon_fdset_fd_next
;
2290 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2291 if ((mon_fdset_fd
->removed
||
2292 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2293 runstate_is_running()) {
2294 close(mon_fdset_fd
->fd
);
2295 g_free(mon_fdset_fd
->opaque
);
2296 QLIST_REMOVE(mon_fdset_fd
, next
);
2297 g_free(mon_fdset_fd
);
2301 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2302 QLIST_REMOVE(mon_fdset
, next
);
2307 static void monitor_fdsets_cleanup(void)
2309 MonFdset
*mon_fdset
;
2310 MonFdset
*mon_fdset_next
;
2312 qemu_mutex_lock(&mon_fdsets_lock
);
2313 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2314 monitor_fdset_cleanup(mon_fdset
);
2316 qemu_mutex_unlock(&mon_fdsets_lock
);
2319 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2320 const char *opaque
, Error
**errp
)
2323 Monitor
*mon
= cur_mon
;
2326 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
2328 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2332 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2333 has_opaque
, opaque
, errp
);
2345 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2347 MonFdset
*mon_fdset
;
2348 MonFdsetFd
*mon_fdset_fd
;
2351 qemu_mutex_lock(&mon_fdsets_lock
);
2352 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2353 if (mon_fdset
->id
!= fdset_id
) {
2356 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2358 if (mon_fdset_fd
->fd
!= fd
) {
2361 mon_fdset_fd
->removed
= true;
2364 mon_fdset_fd
->removed
= true;
2367 if (has_fd
&& !mon_fdset_fd
) {
2370 monitor_fdset_cleanup(mon_fdset
);
2371 qemu_mutex_unlock(&mon_fdsets_lock
);
2376 qemu_mutex_unlock(&mon_fdsets_lock
);
2378 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2381 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2383 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2386 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2388 MonFdset
*mon_fdset
;
2389 MonFdsetFd
*mon_fdset_fd
;
2390 FdsetInfoList
*fdset_list
= NULL
;
2392 qemu_mutex_lock(&mon_fdsets_lock
);
2393 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2394 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2395 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2397 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2398 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2400 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2401 FdsetFdInfoList
*fdsetfd_info
;
2403 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2404 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2405 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2406 if (mon_fdset_fd
->opaque
) {
2407 fdsetfd_info
->value
->has_opaque
= true;
2408 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2410 fdsetfd_info
->value
->has_opaque
= false;
2413 fdsetfd_info
->next
= fdsetfd_list
;
2414 fdsetfd_list
= fdsetfd_info
;
2417 fdset_info
->value
->fds
= fdsetfd_list
;
2419 fdset_info
->next
= fdset_list
;
2420 fdset_list
= fdset_info
;
2422 qemu_mutex_unlock(&mon_fdsets_lock
);
2427 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2428 bool has_opaque
, const char *opaque
,
2431 MonFdset
*mon_fdset
= NULL
;
2432 MonFdsetFd
*mon_fdset_fd
;
2435 qemu_mutex_lock(&mon_fdsets_lock
);
2437 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2438 /* Break if match found or match impossible due to ordering by ID */
2439 if (fdset_id
<= mon_fdset
->id
) {
2440 if (fdset_id
< mon_fdset
->id
) {
2448 if (mon_fdset
== NULL
) {
2449 int64_t fdset_id_prev
= -1;
2450 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2454 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2455 "a non-negative value");
2456 qemu_mutex_unlock(&mon_fdsets_lock
);
2459 /* Use specified fdset ID */
2460 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2461 mon_fdset_cur
= mon_fdset
;
2462 if (fdset_id
< mon_fdset_cur
->id
) {
2467 /* Use first available fdset ID */
2468 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2469 mon_fdset_cur
= mon_fdset
;
2470 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2471 fdset_id_prev
= mon_fdset_cur
->id
;
2478 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2480 mon_fdset
->id
= fdset_id
;
2482 mon_fdset
->id
= fdset_id_prev
+ 1;
2485 /* The fdset list is ordered by fdset ID */
2486 if (!mon_fdset_cur
) {
2487 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2488 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2489 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2491 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2495 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2496 mon_fdset_fd
->fd
= fd
;
2497 mon_fdset_fd
->removed
= false;
2499 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2501 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2503 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2504 fdinfo
->fdset_id
= mon_fdset
->id
;
2505 fdinfo
->fd
= mon_fdset_fd
->fd
;
2507 qemu_mutex_unlock(&mon_fdsets_lock
);
2511 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2516 MonFdset
*mon_fdset
;
2517 MonFdsetFd
*mon_fdset_fd
;
2521 qemu_mutex_lock(&mon_fdsets_lock
);
2522 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2523 if (mon_fdset
->id
!= fdset_id
) {
2526 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2527 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2528 if (mon_fd_flags
== -1) {
2533 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2534 ret
= mon_fdset_fd
->fd
;
2544 qemu_mutex_unlock(&mon_fdsets_lock
);
2549 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2551 MonFdset
*mon_fdset
;
2552 MonFdsetFd
*mon_fdset_fd_dup
;
2554 qemu_mutex_lock(&mon_fdsets_lock
);
2555 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2556 if (mon_fdset
->id
!= fdset_id
) {
2559 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2560 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2564 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2565 mon_fdset_fd_dup
->fd
= dup_fd
;
2566 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2567 qemu_mutex_unlock(&mon_fdsets_lock
);
2572 qemu_mutex_unlock(&mon_fdsets_lock
);
2576 static int monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2578 MonFdset
*mon_fdset
;
2579 MonFdsetFd
*mon_fdset_fd_dup
;
2581 qemu_mutex_lock(&mon_fdsets_lock
);
2582 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2583 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2584 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2586 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2587 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2588 monitor_fdset_cleanup(mon_fdset
);
2592 qemu_mutex_unlock(&mon_fdsets_lock
);
2593 return mon_fdset
->id
;
2600 qemu_mutex_unlock(&mon_fdsets_lock
);
2604 int monitor_fdset_dup_fd_find(int dup_fd
)
2606 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2609 void monitor_fdset_dup_fd_remove(int dup_fd
)
2611 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2614 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2617 Error
*local_err
= NULL
;
2619 if (!qemu_isdigit(fdname
[0]) && mon
) {
2620 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2622 fd
= qemu_parse_fd(fdname
);
2624 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2629 error_propagate(errp
, local_err
);
2638 /* Please update hmp-commands.hx when adding or changing commands */
2639 static mon_cmd_t info_cmds
[] = {
2640 #include "hmp-commands-info.h"
2644 /* mon_cmds and info_cmds would be sorted at runtime */
2645 static mon_cmd_t mon_cmds
[] = {
2646 #include "hmp-commands.h"
2650 /*******************************************************************/
2652 static const char *pch
;
2653 static sigjmp_buf expr_env
;
2656 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2657 expr_error(Monitor
*mon
, const char *fmt
, ...)
2661 monitor_vprintf(mon
, fmt
, ap
);
2662 monitor_printf(mon
, "\n");
2664 siglongjmp(expr_env
, 1);
2667 /* return 0 if OK, -1 if not found */
2668 static int get_monitor_def(target_long
*pval
, const char *name
)
2670 const MonitorDef
*md
= target_monitor_defs();
2671 CPUState
*cs
= mon_get_cpu();
2676 if (cs
== NULL
|| md
== NULL
) {
2680 for(; md
->name
!= NULL
; md
++) {
2681 if (compare_cmd(name
, md
->name
)) {
2682 if (md
->get_value
) {
2683 *pval
= md
->get_value(md
, md
->offset
);
2685 CPUArchState
*env
= mon_get_cpu_env();
2686 ptr
= (uint8_t *)env
+ md
->offset
;
2689 *pval
= *(int32_t *)ptr
;
2692 *pval
= *(target_long
*)ptr
;
2703 ret
= target_get_monitor_def(cs
, name
, &tmp
);
2705 *pval
= (target_long
) tmp
;
2711 static void next(void)
2715 while (qemu_isspace(*pch
))
2720 static int64_t expr_sum(Monitor
*mon
);
2722 static int64_t expr_unary(Monitor
*mon
)
2731 n
= expr_unary(mon
);
2735 n
= -expr_unary(mon
);
2739 n
= ~expr_unary(mon
);
2745 expr_error(mon
, "')' expected");
2752 expr_error(mon
, "character constant expected");
2756 expr_error(mon
, "missing terminating \' character");
2766 while ((*pch
>= 'a' && *pch
<= 'z') ||
2767 (*pch
>= 'A' && *pch
<= 'Z') ||
2768 (*pch
>= '0' && *pch
<= '9') ||
2769 *pch
== '_' || *pch
== '.') {
2770 if ((q
- buf
) < sizeof(buf
) - 1)
2774 while (qemu_isspace(*pch
))
2777 ret
= get_monitor_def(®
, buf
);
2779 expr_error(mon
, "unknown register");
2784 expr_error(mon
, "unexpected end of expression");
2789 n
= strtoull(pch
, &p
, 0);
2790 if (errno
== ERANGE
) {
2791 expr_error(mon
, "number too large");
2794 expr_error(mon
, "invalid char '%c' in expression", *p
);
2797 while (qemu_isspace(*pch
))
2805 static int64_t expr_prod(Monitor
*mon
)
2810 val
= expr_unary(mon
);
2813 if (op
!= '*' && op
!= '/' && op
!= '%')
2816 val2
= expr_unary(mon
);
2825 expr_error(mon
, "division by zero");
2836 static int64_t expr_logic(Monitor
*mon
)
2841 val
= expr_prod(mon
);
2844 if (op
!= '&' && op
!= '|' && op
!= '^')
2847 val2
= expr_prod(mon
);
2864 static int64_t expr_sum(Monitor
*mon
)
2869 val
= expr_logic(mon
);
2872 if (op
!= '+' && op
!= '-')
2875 val2
= expr_logic(mon
);
2884 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2887 if (sigsetjmp(expr_env
, 0)) {
2891 while (qemu_isspace(*pch
))
2893 *pval
= expr_sum(mon
);
2898 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2900 const char *p
= *pp
;
2904 d
= strtod(p
, &tailp
);
2906 monitor_printf(mon
, "Number expected\n");
2909 if (d
!= d
|| d
- d
!= 0) {
2910 /* NaN or infinity */
2911 monitor_printf(mon
, "Bad number\n");
2920 * Store the command-name in cmdname, and return a pointer to
2921 * the remaining of the command string.
2923 static const char *get_command_name(const char *cmdline
,
2924 char *cmdname
, size_t nlen
)
2927 const char *p
, *pstart
;
2930 while (qemu_isspace(*p
))
2935 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2940 memcpy(cmdname
, pstart
, len
);
2941 cmdname
[len
] = '\0';
2946 * Read key of 'type' into 'key' and return the current
2949 static char *key_get_info(const char *type
, char **key
)
2957 p
= strchr(type
, ':');
2964 str
= g_malloc(len
+ 1);
2965 memcpy(str
, type
, len
);
2972 static int default_fmt_format
= 'x';
2973 static int default_fmt_size
= 4;
2975 static int is_valid_option(const char *c
, const char *typestr
)
2983 typestr
= strstr(typestr
, option
);
2984 return (typestr
!= NULL
);
2987 static const mon_cmd_t
*search_dispatch_table(const mon_cmd_t
*disp_table
,
2988 const char *cmdname
)
2990 const mon_cmd_t
*cmd
;
2992 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2993 if (compare_cmd(cmdname
, cmd
->name
)) {
3002 * Parse command name from @cmdp according to command table @table.
3003 * If blank, return NULL.
3004 * Else, if no valid command can be found, report to @mon, and return
3006 * Else, change @cmdp to point right behind the name, and return its
3007 * command table entry.
3008 * Do not assume the return value points into @table! It doesn't when
3009 * the command is found in a sub-command table.
3011 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3012 const char *cmdp_start
,
3017 const mon_cmd_t
*cmd
;
3020 /* extract the command name */
3021 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
3025 cmd
= search_dispatch_table(table
, cmdname
);
3027 monitor_printf(mon
, "unknown command: '%.*s'\n",
3028 (int)(p
- cmdp_start
), cmdp_start
);
3031 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
3032 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
3033 "until after exit_preconfig.\n",
3034 (int)(p
- cmdp_start
), cmdp_start
);
3038 /* filter out following useless space */
3039 while (qemu_isspace(*p
)) {
3044 /* search sub command */
3045 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
3046 return monitor_parse_command(mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
3053 * Parse arguments for @cmd.
3054 * If it can't be parsed, report to @mon, and return NULL.
3055 * Else, insert command arguments into a QDict, and return it.
3056 * Note: On success, caller has to free the QDict structure.
3059 static QDict
*monitor_parse_arguments(Monitor
*mon
,
3061 const mon_cmd_t
*cmd
)
3063 const char *typestr
;
3066 const char *p
= *endp
;
3068 QDict
*qdict
= qdict_new();
3070 /* parse the parameters */
3071 typestr
= cmd
->args_type
;
3073 typestr
= key_get_info(typestr
, &key
);
3085 while (qemu_isspace(*p
))
3087 if (*typestr
== '?') {
3090 /* no optional string: NULL argument */
3094 ret
= get_str(buf
, sizeof(buf
), &p
);
3098 monitor_printf(mon
, "%s: filename expected\n",
3102 monitor_printf(mon
, "%s: block device name expected\n",
3106 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
3111 qdict_put_str(qdict
, key
, buf
);
3116 QemuOptsList
*opts_list
;
3119 opts_list
= qemu_find_opts(key
);
3120 if (!opts_list
|| opts_list
->desc
->name
) {
3123 while (qemu_isspace(*p
)) {
3128 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
3131 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
3135 qemu_opts_to_qdict(opts
, qdict
);
3136 qemu_opts_del(opts
);
3141 int count
, format
, size
;
3143 while (qemu_isspace(*p
))
3149 if (qemu_isdigit(*p
)) {
3151 while (qemu_isdigit(*p
)) {
3152 count
= count
* 10 + (*p
- '0');
3190 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3191 monitor_printf(mon
, "invalid char in format: '%c'\n",
3196 format
= default_fmt_format
;
3197 if (format
!= 'i') {
3198 /* for 'i', not specifying a size gives -1 as size */
3200 size
= default_fmt_size
;
3201 default_fmt_size
= size
;
3203 default_fmt_format
= format
;
3206 format
= default_fmt_format
;
3207 if (format
!= 'i') {
3208 size
= default_fmt_size
;
3213 qdict_put_int(qdict
, "count", count
);
3214 qdict_put_int(qdict
, "format", format
);
3215 qdict_put_int(qdict
, "size", size
);
3224 while (qemu_isspace(*p
))
3226 if (*typestr
== '?' || *typestr
== '.') {
3227 if (*typestr
== '?') {
3235 while (qemu_isspace(*p
))
3244 if (get_expr(mon
, &val
, &p
))
3246 /* Check if 'i' is greater than 32-bit */
3247 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3248 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3249 monitor_printf(mon
, "integer is for 32-bit values\n");
3251 } else if (c
== 'M') {
3253 monitor_printf(mon
, "enter a positive value\n");
3258 qdict_put_int(qdict
, key
, val
);
3267 while (qemu_isspace(*p
)) {
3270 if (*typestr
== '?') {
3276 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
3277 if (ret
< 0 || val
> INT64_MAX
) {
3278 monitor_printf(mon
, "invalid size\n");
3281 qdict_put_int(qdict
, key
, val
);
3289 while (qemu_isspace(*p
))
3291 if (*typestr
== '?') {
3297 if (get_double(mon
, &val
, &p
) < 0) {
3300 if (p
[0] && p
[1] == 's') {
3303 val
/= 1e3
; p
+= 2; break;
3305 val
/= 1e6
; p
+= 2; break;
3307 val
/= 1e9
; p
+= 2; break;
3310 if (*p
&& !qemu_isspace(*p
)) {
3311 monitor_printf(mon
, "Unknown unit suffix\n");
3314 qdict_put(qdict
, key
, qnum_from_double(val
));
3322 while (qemu_isspace(*p
)) {
3326 while (qemu_isgraph(*p
)) {
3329 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3331 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3334 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3337 qdict_put_bool(qdict
, key
, val
);
3342 const char *tmp
= p
;
3349 while (qemu_isspace(*p
))
3354 if(!is_valid_option(p
, typestr
)) {
3356 monitor_printf(mon
, "%s: unsupported option -%c\n",
3368 qdict_put_bool(qdict
, key
, true);
3375 /* package all remaining string */
3378 while (qemu_isspace(*p
)) {
3381 if (*typestr
== '?') {
3384 /* no remaining string: NULL argument */
3390 monitor_printf(mon
, "%s: string expected\n",
3394 qdict_put_str(qdict
, key
, p
);
3400 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3406 /* check that all arguments were parsed */
3407 while (qemu_isspace(*p
))
3410 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3418 qobject_unref(qdict
);
3423 static void handle_hmp_command(Monitor
*mon
, const char *cmdline
)
3426 const mon_cmd_t
*cmd
;
3427 const char *cmd_start
= cmdline
;
3429 trace_handle_hmp_command(mon
, cmdline
);
3431 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, mon
->cmd_table
);
3436 qdict
= monitor_parse_arguments(mon
, &cmdline
, cmd
);
3438 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
3441 monitor_printf(mon
, "Try \"help %.*s\" for more information\n",
3442 (int)(cmdline
- cmd_start
), cmd_start
);
3446 cmd
->cmd(mon
, qdict
);
3447 qobject_unref(qdict
);
3450 static void cmd_completion(Monitor
*mon
, const char *name
, const char *list
)
3452 const char *p
, *pstart
;
3459 p
= qemu_strchrnul(p
, '|');
3461 if (len
> sizeof(cmd
) - 2)
3462 len
= sizeof(cmd
) - 2;
3463 memcpy(cmd
, pstart
, len
);
3465 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3466 readline_add_completion(mon
->rs
, cmd
);
3474 static void file_completion(Monitor
*mon
, const char *input
)
3479 char file
[1024], file_prefix
[1024];
3483 p
= strrchr(input
, '/');
3486 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3487 pstrcpy(path
, sizeof(path
), ".");
3489 input_path_len
= p
- input
+ 1;
3490 memcpy(path
, input
, input_path_len
);
3491 if (input_path_len
> sizeof(path
) - 1)
3492 input_path_len
= sizeof(path
) - 1;
3493 path
[input_path_len
] = '\0';
3494 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3497 ffs
= opendir(path
);
3506 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3510 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3511 memcpy(file
, input
, input_path_len
);
3512 if (input_path_len
< sizeof(file
))
3513 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3515 /* stat the file to find out if it's a directory.
3516 * In that case add a slash to speed up typing long paths
3518 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3519 pstrcat(file
, sizeof(file
), "/");
3521 readline_add_completion(mon
->rs
, file
);
3527 static const char *next_arg_type(const char *typestr
)
3529 const char *p
= strchr(typestr
, ':');
3530 return (p
!= NULL
? ++p
: typestr
);
3533 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3536 if (!str
|| !option
) {
3539 if (!strncmp(option
, str
, strlen(str
))) {
3540 readline_add_completion(rs
, option
);
3544 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3547 ChardevBackendInfoList
*list
, *start
;
3553 readline_set_completion_index(rs
, len
);
3555 start
= list
= qmp_query_chardev_backends(NULL
);
3557 const char *chr_name
= list
->value
->name
;
3559 if (!strncmp(chr_name
, str
, len
)) {
3560 readline_add_completion(rs
, chr_name
);
3564 qapi_free_ChardevBackendInfoList(start
);
3567 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3576 readline_set_completion_index(rs
, len
);
3577 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
3578 add_completion_option(rs
, str
, NetClientDriver_str(i
));
3582 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3592 readline_set_completion_index(rs
, len
);
3593 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3596 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3598 name
= object_class_get_name(OBJECT_CLASS(dc
));
3600 if (dc
->user_creatable
3601 && !strncmp(name
, str
, len
)) {
3602 readline_add_completion(rs
, name
);
3609 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3619 readline_set_completion_index(rs
, len
);
3620 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3624 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3625 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3626 readline_add_completion(rs
, name
);
3633 static void peripheral_device_del_completion(ReadLineState
*rs
,
3634 const char *str
, size_t len
)
3636 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3637 GSList
*list
, *item
;
3639 list
= qdev_build_hotpluggable_device_list(peripheral
);
3644 for (item
= list
; item
; item
= g_slist_next(item
)) {
3645 DeviceState
*dev
= item
->data
;
3647 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3648 readline_add_completion(rs
, dev
->id
);
3655 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3658 ChardevInfoList
*list
, *start
;
3664 readline_set_completion_index(rs
, len
);
3666 start
= list
= qmp_query_chardev(NULL
);
3668 ChardevInfo
*chr
= list
->value
;
3670 if (!strncmp(chr
->label
, str
, len
)) {
3671 readline_add_completion(rs
, chr
->label
);
3675 qapi_free_ChardevInfoList(start
);
3678 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3681 ChardevInfoList
*list
, *start
;
3684 readline_set_completion_index(rs
, len
);
3686 start
= list
= qmp_query_chardev(NULL
);
3688 ChardevInfo
*chr_info
= list
->value
;
3690 if (!strncmp(chr_info
->label
, str
, len
)) {
3691 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
3692 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
3693 readline_add_completion(rs
, chr_info
->label
);
3698 qapi_free_ChardevInfoList(start
);
3701 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3706 ringbuf_completion(rs
, str
);
3709 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3718 readline_set_completion_index(rs
, len
);
3719 peripheral_device_del_completion(rs
, str
, len
);
3722 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3724 ObjectPropertyInfoList
*list
, *start
;
3731 readline_set_completion_index(rs
, len
);
3733 start
= list
= qmp_qom_list("/objects", NULL
);
3735 ObjectPropertyInfo
*info
= list
->value
;
3737 if (!strncmp(info
->type
, "child<", 5)
3738 && !strncmp(info
->name
, str
, len
)) {
3739 readline_add_completion(rs
, info
->name
);
3743 qapi_free_ObjectPropertyInfoList(start
);
3746 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3755 sep
= strrchr(str
, '-');
3760 readline_set_completion_index(rs
, len
);
3761 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3762 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
3763 readline_add_completion(rs
, QKeyCode_str(i
));
3768 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3773 readline_set_completion_index(rs
, len
);
3775 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3777 count
= qemu_find_net_clients_except(NULL
, ncs
,
3778 NET_CLIENT_DRIVER_NONE
,
3780 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3781 const char *name
= ncs
[i
]->name
;
3782 if (!strncmp(str
, name
, len
)) {
3783 readline_add_completion(rs
, name
);
3786 } else if (nb_args
== 3) {
3787 add_completion_option(rs
, str
, "on");
3788 add_completion_option(rs
, str
, "off");
3792 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3795 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3802 readline_set_completion_index(rs
, len
);
3803 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3805 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3807 const char *name
= ncs
[i
]->name
;
3808 if (strncmp(str
, name
, len
)) {
3811 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3813 readline_add_completion(rs
, name
);
3818 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3823 readline_set_completion_index(rs
, len
);
3825 TraceEventIter iter
;
3827 char *pattern
= g_strdup_printf("%s*", str
);
3828 trace_event_iter_init(&iter
, pattern
);
3829 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3830 readline_add_completion(rs
, trace_event_get_name(ev
));
3836 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3841 readline_set_completion_index(rs
, len
);
3843 TraceEventIter iter
;
3845 char *pattern
= g_strdup_printf("%s*", str
);
3846 trace_event_iter_init(&iter
, pattern
);
3847 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3848 readline_add_completion(rs
, trace_event_get_name(ev
));
3851 } else if (nb_args
== 3) {
3852 add_completion_option(rs
, str
, "on");
3853 add_completion_option(rs
, str
, "off");
3857 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3864 readline_set_completion_index(rs
, strlen(str
));
3865 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
3866 add_completion_option(rs
, str
, WatchdogAction_str(i
));
3870 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3876 readline_set_completion_index(rs
, len
);
3879 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3880 const char *name
= MigrationCapability_str(i
);
3881 if (!strncmp(str
, name
, len
)) {
3882 readline_add_completion(rs
, name
);
3885 } else if (nb_args
== 3) {
3886 add_completion_option(rs
, str
, "on");
3887 add_completion_option(rs
, str
, "off");
3891 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3897 readline_set_completion_index(rs
, len
);
3900 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3901 const char *name
= MigrationParameter_str(i
);
3902 if (!strncmp(str
, name
, len
)) {
3903 readline_add_completion(rs
, name
);
3909 static void vm_completion(ReadLineState
*rs
, const char *str
)
3912 BlockDriverState
*bs
;
3913 BdrvNextIterator it
;
3916 readline_set_completion_index(rs
, len
);
3918 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3919 SnapshotInfoList
*snapshots
, *snapshot
;
3920 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3923 aio_context_acquire(ctx
);
3924 if (bdrv_can_snapshot(bs
)) {
3925 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3927 aio_context_release(ctx
);
3932 snapshot
= snapshots
;
3934 char *completion
= snapshot
->value
->name
;
3935 if (!strncmp(str
, completion
, len
)) {
3936 readline_add_completion(rs
, completion
);
3938 completion
= snapshot
->value
->id
;
3939 if (!strncmp(str
, completion
, len
)) {
3940 readline_add_completion(rs
, completion
);
3942 snapshot
= snapshot
->next
;
3944 qapi_free_SnapshotInfoList(snapshots
);
3949 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3952 vm_completion(rs
, str
);
3956 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3959 vm_completion(rs
, str
);
3963 static void monitor_find_completion_by_table(Monitor
*mon
,
3964 const mon_cmd_t
*cmd_table
,
3968 const char *cmdname
;
3970 const char *ptype
, *old_ptype
, *str
, *name
;
3971 const mon_cmd_t
*cmd
;
3972 BlockBackend
*blk
= NULL
;
3975 /* command completion */
3980 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3981 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3982 if (!runstate_check(RUN_STATE_PRECONFIG
) ||
3983 cmd_can_preconfig(cmd
)) {
3984 cmd_completion(mon
, cmdname
, cmd
->name
);
3988 /* find the command */
3989 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3990 if (compare_cmd(args
[0], cmd
->name
) &&
3991 (!runstate_check(RUN_STATE_PRECONFIG
) ||
3992 cmd_can_preconfig(cmd
))) {
4000 if (cmd
->sub_table
) {
4001 /* do the job again */
4002 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
4003 &args
[1], nb_args
- 1);
4006 if (cmd
->command_completion
) {
4007 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
4011 ptype
= next_arg_type(cmd
->args_type
);
4012 for(i
= 0; i
< nb_args
- 2; i
++) {
4013 if (*ptype
!= '\0') {
4014 ptype
= next_arg_type(ptype
);
4015 while (*ptype
== '?')
4016 ptype
= next_arg_type(ptype
);
4019 str
= args
[nb_args
- 1];
4021 while (*ptype
== '-' && old_ptype
!= ptype
) {
4023 ptype
= next_arg_type(ptype
);
4027 /* file completion */
4028 readline_set_completion_index(mon
->rs
, strlen(str
));
4029 file_completion(mon
, str
);
4032 /* block device name completion */
4033 readline_set_completion_index(mon
->rs
, strlen(str
));
4034 while ((blk
= blk_next(blk
)) != NULL
) {
4035 name
= blk_name(blk
);
4036 if (str
[0] == '\0' ||
4037 !strncmp(name
, str
, strlen(str
))) {
4038 readline_add_completion(mon
->rs
, name
);
4044 if (!strcmp(cmd
->name
, "help|?")) {
4045 monitor_find_completion_by_table(mon
, cmd_table
,
4046 &args
[1], nb_args
- 1);
4055 static void monitor_find_completion(void *opaque
,
4056 const char *cmdline
)
4058 Monitor
*mon
= opaque
;
4059 char *args
[MAX_ARGS
];
4062 /* 1. parse the cmdline */
4063 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
4067 /* if the line ends with a space, it means we want to complete the
4069 len
= strlen(cmdline
);
4070 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
4071 if (nb_args
>= MAX_ARGS
) {
4074 args
[nb_args
++] = g_strdup("");
4077 /* 2. auto complete according to args */
4078 monitor_find_completion_by_table(mon
, mon
->cmd_table
, args
, nb_args
);
4081 free_cmdline_args(args
, nb_args
);
4084 static int monitor_can_read(void *opaque
)
4086 Monitor
*mon
= opaque
;
4088 return !atomic_mb_read(&mon
->suspend_cnt
);
4092 * Emit QMP response @rsp with ID @id to @mon.
4093 * Null @rsp can only happen for commands with QCO_NO_SUCCESS_RESP.
4094 * Nothing is emitted then.
4096 static void monitor_qmp_respond(Monitor
*mon
, QDict
*rsp
, QObject
*id
)
4100 qdict_put_obj(rsp
, "id", qobject_ref(id
));
4103 qmp_queue_response(mon
, rsp
);
4107 static void monitor_qmp_dispatch(Monitor
*mon
, QObject
*req
, QObject
*id
)
4116 rsp
= qmp_dispatch(mon
->qmp
.commands
, req
, qmp_oob_enabled(mon
));
4120 if (mon
->qmp
.commands
== &qmp_cap_negotiation_commands
) {
4121 error
= qdict_get_qdict(rsp
, "error");
4123 && !g_strcmp0(qdict_get_try_str(error
, "class"),
4124 QapiErrorClass_str(ERROR_CLASS_COMMAND_NOT_FOUND
))) {
4125 /* Provide a more useful error message */
4126 qdict_del(error
, "desc");
4127 qdict_put_str(error
, "desc", "Expecting capabilities negotiation"
4128 " with 'qmp_capabilities'");
4132 monitor_qmp_respond(mon
, rsp
, id
);
4137 * Pop a QMP request from a monitor request queue.
4138 * Return the request, or NULL all request queues are empty.
4139 * We are using round-robin fashion to pop the request, to avoid
4140 * processing commands only on a very busy monitor. To achieve that,
4141 * when we process one request on a specific monitor, we put that
4142 * monitor to the end of mon_list queue.
4144 static QMPRequest
*monitor_qmp_requests_pop_any(void)
4146 QMPRequest
*req_obj
= NULL
;
4149 qemu_mutex_lock(&monitor_lock
);
4151 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
4152 qemu_mutex_lock(&mon
->qmp
.qmp_queue_lock
);
4153 req_obj
= g_queue_pop_head(mon
->qmp
.qmp_requests
);
4154 qemu_mutex_unlock(&mon
->qmp
.qmp_queue_lock
);
4162 * We found one request on the monitor. Degrade this monitor's
4163 * priority to lowest by re-inserting it to end of queue.
4165 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4166 QTAILQ_INSERT_TAIL(&mon_list
, mon
, entry
);
4169 qemu_mutex_unlock(&monitor_lock
);
4174 static void monitor_qmp_bh_dispatcher(void *data
)
4176 QMPRequest
*req_obj
= monitor_qmp_requests_pop_any();
4184 trace_monitor_qmp_cmd_in_band(qobject_get_try_str(req_obj
->id
) ?: "");
4185 monitor_qmp_dispatch(req_obj
->mon
, req_obj
->req
, req_obj
->id
);
4187 assert(req_obj
->err
);
4188 rsp
= qmp_error_response(req_obj
->err
);
4189 req_obj
->err
= NULL
;
4190 monitor_qmp_respond(req_obj
->mon
, rsp
, NULL
);
4194 if (req_obj
->need_resume
) {
4195 /* Pairs with the monitor_suspend() in handle_qmp_command() */
4196 monitor_resume(req_obj
->mon
);
4198 qmp_request_free(req_obj
);
4200 /* Reschedule instead of looping so the main loop stays responsive */
4201 qemu_bh_schedule(qmp_dispatcher_bh
);
4204 #define QMP_REQ_QUEUE_LEN_MAX (8)
4206 static void handle_qmp_command(JSONMessageParser
*parser
, GQueue
*tokens
)
4208 QObject
*req
, *id
= NULL
;
4210 MonitorQMP
*mon_qmp
= container_of(parser
, MonitorQMP
, parser
);
4211 Monitor
*mon
= container_of(mon_qmp
, Monitor
, qmp
);
4213 QMPRequest
*req_obj
;
4215 req
= json_parser_parse_err(tokens
, NULL
, &err
);
4217 /* json_parser_parse_err() sucks: can fail without setting @err */
4218 error_setg(&err
, QERR_JSON_PARSING
);
4221 qdict
= qobject_to(QDict
, req
);
4223 id
= qobject_ref(qdict_get(qdict
, "id"));
4224 qdict_del(qdict
, "id");
4225 } /* else will fail qmp_dispatch() */
4227 if (req
&& trace_event_get_state_backends(TRACE_HANDLE_QMP_COMMAND
)) {
4228 QString
*req_json
= qobject_to_json(req
);
4229 trace_handle_qmp_command(mon
, qstring_get_str(req_json
));
4230 qobject_unref(req_json
);
4233 if (qdict
&& qmp_is_oob(qdict
)) {
4234 /* OOB commands are executed immediately */
4235 trace_monitor_qmp_cmd_out_of_band(qobject_get_try_str(id
)
4237 monitor_qmp_dispatch(mon
, req
, id
);
4241 req_obj
= g_new0(QMPRequest
, 1);
4246 req_obj
->need_resume
= false;
4248 /* Protect qmp_requests and fetching its length. */
4249 qemu_mutex_lock(&mon
->qmp
.qmp_queue_lock
);
4252 * If OOB is not enabled on the current monitor, we'll emulate the
4253 * old behavior that we won't process the current monitor any more
4254 * until it has responded. This helps make sure that as long as
4255 * OOB is not enabled, the server will never drop any command.
4257 if (!qmp_oob_enabled(mon
)) {
4258 monitor_suspend(mon
);
4259 req_obj
->need_resume
= true;
4261 /* Drop the request if queue is full. */
4262 if (mon
->qmp
.qmp_requests
->length
>= QMP_REQ_QUEUE_LEN_MAX
) {
4263 qemu_mutex_unlock(&mon
->qmp
.qmp_queue_lock
);
4265 * FIXME @id's scope is just @mon, and broadcasting it is
4266 * wrong. If another monitor's client has a command with
4267 * the same ID in flight, the event will incorrectly claim
4268 * that command was dropped.
4270 qapi_event_send_command_dropped(id
,
4271 COMMAND_DROP_REASON_QUEUE_FULL
,
4273 qmp_request_free(req_obj
);
4279 * Put the request to the end of queue so that requests will be
4280 * handled in time order. Ownership for req_obj, req, id,
4281 * etc. will be delivered to the handler side.
4283 g_queue_push_tail(mon
->qmp
.qmp_requests
, req_obj
);
4284 qemu_mutex_unlock(&mon
->qmp
.qmp_queue_lock
);
4286 /* Kick the dispatcher routine */
4287 qemu_bh_schedule(qmp_dispatcher_bh
);
4290 static void monitor_qmp_read(void *opaque
, const uint8_t *buf
, int size
)
4292 Monitor
*mon
= opaque
;
4294 json_message_parser_feed(&mon
->qmp
.parser
, (const char *) buf
, size
);
4297 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
4299 Monitor
*old_mon
= cur_mon
;
4305 for (i
= 0; i
< size
; i
++)
4306 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
4308 if (size
== 0 || buf
[size
- 1] != 0)
4309 monitor_printf(cur_mon
, "corrupted command\n");
4311 handle_hmp_command(cur_mon
, (char *)buf
);
4317 static void monitor_command_cb(void *opaque
, const char *cmdline
,
4318 void *readline_opaque
)
4320 Monitor
*mon
= opaque
;
4322 monitor_suspend(mon
);
4323 handle_hmp_command(mon
, cmdline
);
4324 monitor_resume(mon
);
4327 int monitor_suspend(Monitor
*mon
)
4329 if (monitor_is_hmp_non_interactive(mon
)) {
4333 atomic_inc(&mon
->suspend_cnt
);
4335 if (monitor_is_qmp(mon
)) {
4337 * Kick I/O thread to make sure this takes effect. It'll be
4338 * evaluated again in prepare() of the watch object.
4340 aio_notify(iothread_get_aio_context(mon_iothread
));
4343 trace_monitor_suspend(mon
, 1);
4347 void monitor_resume(Monitor
*mon
)
4349 if (monitor_is_hmp_non_interactive(mon
)) {
4353 if (atomic_dec_fetch(&mon
->suspend_cnt
) == 0) {
4354 if (monitor_is_qmp(mon
)) {
4356 * For QMP monitors that are running in the I/O thread,
4357 * let's kick the thread in case it's sleeping.
4359 if (mon
->use_io_thread
) {
4360 aio_notify(iothread_get_aio_context(mon_iothread
));
4364 readline_show_prompt(mon
->rs
);
4367 trace_monitor_suspend(mon
, -1);
4370 static QDict
*qmp_greeting(Monitor
*mon
)
4372 QList
*cap_list
= qlist_new();
4373 QObject
*ver
= NULL
;
4376 qmp_marshal_query_version(NULL
, &ver
, NULL
);
4378 for (cap
= 0; cap
< QMP_CAPABILITY__MAX
; cap
++) {
4379 if (mon
->qmp
.capab_offered
[cap
]) {
4380 qlist_append_str(cap_list
, QMPCapability_str(cap
));
4384 return qdict_from_jsonf_nofail(
4385 "{'QMP': {'version': %p, 'capabilities': %p}}",
4389 static void monitor_qmp_event(void *opaque
, int event
)
4392 Monitor
*mon
= opaque
;
4395 case CHR_EVENT_OPENED
:
4396 mon
->qmp
.commands
= &qmp_cap_negotiation_commands
;
4397 monitor_qmp_caps_reset(mon
);
4398 data
= qmp_greeting(mon
);
4399 qmp_queue_response(mon
, data
);
4400 qobject_unref(data
);
4403 case CHR_EVENT_CLOSED
:
4405 * Note: this is only useful when the output of the chardev
4406 * backend is still open. For example, when the backend is
4407 * stdio, it's possible that stdout is still open when stdin
4410 monitor_qmp_response_flush(mon
);
4411 monitor_qmp_cleanup_queues(mon
);
4412 json_message_parser_destroy(&mon
->qmp
.parser
);
4413 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4415 monitor_fdsets_cleanup();
4420 static void monitor_event(void *opaque
, int event
)
4422 Monitor
*mon
= opaque
;
4425 case CHR_EVENT_MUX_IN
:
4426 qemu_mutex_lock(&mon
->mon_lock
);
4428 qemu_mutex_unlock(&mon
->mon_lock
);
4429 if (mon
->reset_seen
) {
4430 readline_restart(mon
->rs
);
4431 monitor_resume(mon
);
4434 atomic_mb_set(&mon
->suspend_cnt
, 0);
4438 case CHR_EVENT_MUX_OUT
:
4439 if (mon
->reset_seen
) {
4440 if (atomic_mb_read(&mon
->suspend_cnt
) == 0) {
4441 monitor_printf(mon
, "\n");
4444 monitor_suspend(mon
);
4446 atomic_inc(&mon
->suspend_cnt
);
4448 qemu_mutex_lock(&mon
->mon_lock
);
4450 qemu_mutex_unlock(&mon
->mon_lock
);
4453 case CHR_EVENT_OPENED
:
4454 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4455 "information\n", QEMU_VERSION
);
4456 if (!mon
->mux_out
) {
4457 readline_restart(mon
->rs
);
4458 readline_show_prompt(mon
->rs
);
4460 mon
->reset_seen
= 1;
4464 case CHR_EVENT_CLOSED
:
4466 monitor_fdsets_cleanup();
4472 compare_mon_cmd(const void *a
, const void *b
)
4474 return strcmp(((const mon_cmd_t
*)a
)->name
,
4475 ((const mon_cmd_t
*)b
)->name
);
4478 static void sortcmdlist(void)
4481 int elem_size
= sizeof(mon_cmd_t
);
4483 array_num
= sizeof(mon_cmds
)/elem_size
-1;
4484 qsort((void *)mon_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4486 array_num
= sizeof(info_cmds
)/elem_size
-1;
4487 qsort((void *)info_cmds
, array_num
, elem_size
, compare_mon_cmd
);
4490 static GMainContext
*monitor_get_io_context(void)
4492 return iothread_get_g_main_context(mon_iothread
);
4495 static AioContext
*monitor_get_aio_context(void)
4497 return iothread_get_aio_context(mon_iothread
);
4500 static void monitor_iothread_init(void)
4502 mon_iothread
= iothread_create("mon_iothread", &error_abort
);
4505 * The dispatcher BH must run in the main loop thread, since we
4506 * have commands assuming that context. It would be nice to get
4507 * rid of those assumptions.
4509 qmp_dispatcher_bh
= aio_bh_new(iohandler_get_aio_context(),
4510 monitor_qmp_bh_dispatcher
,
4514 * The responder BH must be run in the monitor I/O thread, so that
4515 * monitors that are using the I/O thread have their output
4516 * written by the I/O thread.
4518 qmp_respond_bh
= aio_bh_new(monitor_get_aio_context(),
4519 monitor_qmp_bh_responder
,
4523 void monitor_init_globals(void)
4525 monitor_init_qmp_commands();
4526 monitor_qapi_event_init();
4528 qemu_mutex_init(&monitor_lock
);
4529 qemu_mutex_init(&mon_fdsets_lock
);
4530 monitor_iothread_init();
4533 /* These functions just adapt the readline interface in a typesafe way. We
4534 * could cast function pointers but that discards compiler checks.
4536 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4537 const char *fmt
, ...)
4541 monitor_vprintf(opaque
, fmt
, ap
);
4545 static void monitor_readline_flush(void *opaque
)
4547 monitor_flush(opaque
);
4551 * Print to current monitor if we have one, else to stderr.
4552 * TODO should return int, so callers can calculate width, but that
4553 * requires surgery to monitor_vprintf(). Left for another day.
4555 void error_vprintf(const char *fmt
, va_list ap
)
4557 if (cur_mon
&& !monitor_cur_is_qmp()) {
4558 monitor_vprintf(cur_mon
, fmt
, ap
);
4560 vfprintf(stderr
, fmt
, ap
);
4564 void error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
4566 if (cur_mon
&& !monitor_cur_is_qmp()) {
4567 monitor_vprintf(cur_mon
, fmt
, ap
);
4568 } else if (!cur_mon
) {
4569 vfprintf(stderr
, fmt
, ap
);
4573 static void monitor_list_append(Monitor
*mon
)
4575 qemu_mutex_lock(&monitor_lock
);
4576 QTAILQ_INSERT_HEAD(&mon_list
, mon
, entry
);
4577 qemu_mutex_unlock(&monitor_lock
);
4580 static void monitor_qmp_setup_handlers_bh(void *opaque
)
4582 Monitor
*mon
= opaque
;
4583 GMainContext
*context
;
4585 if (mon
->use_io_thread
) {
4586 /* Use @mon_iothread context */
4587 context
= monitor_get_io_context();
4590 /* Use default main loop context */
4594 qemu_chr_fe_set_handlers(&mon
->chr
, monitor_can_read
, monitor_qmp_read
,
4595 monitor_qmp_event
, NULL
, mon
, context
, true);
4596 monitor_list_append(mon
);
4599 void monitor_init(Chardev
*chr
, int flags
)
4601 Monitor
*mon
= g_malloc(sizeof(*mon
));
4602 bool use_readline
= flags
& MONITOR_USE_READLINE
;
4603 bool use_oob
= flags
& MONITOR_USE_OOB
;
4606 if (CHARDEV_IS_MUX(chr
)) {
4607 error_report("Monitor out-of-band is not supported with "
4608 "MUX typed chardev backend");
4612 error_report("Monitor out-of-band is only supported by QMP");
4617 monitor_data_init(mon
, false, use_oob
);
4619 qemu_chr_fe_init(&mon
->chr
, chr
, &error_abort
);
4622 mon
->rs
= readline_init(monitor_readline_printf
,
4623 monitor_readline_flush
,
4625 monitor_find_completion
);
4626 monitor_read_command(mon
, 0);
4629 if (monitor_is_qmp(mon
)) {
4630 qemu_chr_fe_set_echo(&mon
->chr
, true);
4631 json_message_parser_init(&mon
->qmp
.parser
, handle_qmp_command
);
4632 if (mon
->use_io_thread
) {
4634 * Make sure the old iowatch is gone. It's possible when
4635 * e.g. the chardev is in client mode, with wait=on.
4637 remove_fd_in_watch(chr
);
4639 * We can't call qemu_chr_fe_set_handlers() directly here
4640 * since chardev might be running in the monitor I/O
4641 * thread. Schedule a bottom half.
4643 aio_bh_schedule_oneshot(monitor_get_aio_context(),
4644 monitor_qmp_setup_handlers_bh
, mon
);
4645 /* The bottom half will add @mon to @mon_list */
4648 qemu_chr_fe_set_handlers(&mon
->chr
, monitor_can_read
,
4649 monitor_qmp_read
, monitor_qmp_event
,
4650 NULL
, mon
, NULL
, true);
4653 qemu_chr_fe_set_handlers(&mon
->chr
, monitor_can_read
, monitor_read
,
4654 monitor_event
, NULL
, mon
, NULL
, true);
4657 monitor_list_append(mon
);
4660 void monitor_cleanup(void)
4662 Monitor
*mon
, *next
;
4665 * We need to explicitly stop the I/O thread (but not destroy it),
4666 * clean up the monitor resources, then destroy the I/O thread since
4667 * we need to unregister from chardev below in
4668 * monitor_data_destroy(), and chardev is not thread-safe yet
4670 iothread_stop(mon_iothread
);
4673 * Flush all response queues. Note that even after this flush,
4674 * data may remain in output buffers.
4676 monitor_qmp_bh_responder(NULL
);
4678 /* Flush output buffers and destroy monitors */
4679 qemu_mutex_lock(&monitor_lock
);
4680 QTAILQ_FOREACH_SAFE(mon
, &mon_list
, entry
, next
) {
4681 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4683 monitor_data_destroy(mon
);
4686 qemu_mutex_unlock(&monitor_lock
);
4688 /* QEMUBHs needs to be deleted before destroying the I/O thread */
4689 qemu_bh_delete(qmp_dispatcher_bh
);
4690 qmp_dispatcher_bh
= NULL
;
4691 qemu_bh_delete(qmp_respond_bh
);
4692 qmp_respond_bh
= NULL
;
4694 iothread_destroy(mon_iothread
);
4695 mon_iothread
= NULL
;
4698 QemuOptsList qemu_mon_opts
= {
4700 .implied_opt_name
= "chardev",
4701 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4705 .type
= QEMU_OPT_STRING
,
4708 .type
= QEMU_OPT_STRING
,
4711 .type
= QEMU_OPT_BOOL
,
4714 .type
= QEMU_OPT_BOOL
,
4716 { /* end of list */ }
4721 void qmp_rtc_reset_reinjection(Error
**errp
)
4723 error_setg(errp
, QERR_FEATURE_DISABLED
, "rtc-reset-reinjection");
4726 SevInfo
*qmp_query_sev(Error
**errp
)
4728 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-sev");
4732 SevLaunchMeasureInfo
*qmp_query_sev_launch_measure(Error
**errp
)
4734 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-sev-launch-measure");
4738 SevCapability
*qmp_query_sev_capabilities(Error
**errp
)
4740 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-sev-capabilities");
4745 #ifndef TARGET_S390X
4746 void qmp_dump_skeys(const char *filename
, Error
**errp
)
4748 error_setg(errp
, QERR_FEATURE_DISABLED
, "dump-skeys");
4753 GICCapabilityList
*qmp_query_gic_capabilities(Error
**errp
)
4755 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-gic-capabilities");
4760 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4762 MachineState
*ms
= MACHINE(qdev_get_machine());
4763 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4765 if (!mc
->has_hotpluggable_cpus
) {
4766 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4770 return machine_query_hotpluggable_cpus(ms
);