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 "monitor-internal.h"
27 #include "qemu/units.h"
31 #include "monitor/qdev.h"
33 #include "hw/pci/pci.h"
34 #include "sysemu/watchdog.h"
35 #include "hw/loader.h"
36 #include "exec/gdbstub.h"
38 #include "net/slirp.h"
39 #include "chardev/char-mux.h"
40 #include "ui/qemu-spice.h"
41 #include "sysemu/numa.h"
42 #include "qemu/config-file.h"
43 #include "qemu/ctype.h"
44 #include "ui/console.h"
46 #include "sysemu/block-backend.h"
47 #include "audio/audio.h"
48 #include "disas/disas.h"
49 #include "sysemu/balloon.h"
50 #include "qemu/timer.h"
51 #include "sysemu/hw_accel.h"
52 #include "authz/list.h"
53 #include "qapi/util.h"
54 #include "sysemu/tcg.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 "qom/object_interfaces.h"
62 #include "trace/control.h"
63 #include "monitor/hmp-target.h"
64 #ifdef CONFIG_TRACE_SIMPLE
65 #include "trace/simple.h"
67 #include "exec/memory.h"
68 #include "exec/exec-all.h"
70 #include "qemu/option.h"
72 #include "qemu/thread.h"
73 #include "block/qapi.h"
74 #include "qapi/qapi-commands.h"
75 #include "qapi/qapi-emit-events.h"
76 #include "qapi/error.h"
77 #include "qapi/qmp-event.h"
78 #include "qapi/qapi-introspect.h"
79 #include "sysemu/qtest.h"
80 #include "sysemu/cpus.h"
81 #include "qemu/cutils.h"
84 #if defined(TARGET_S390X)
85 #include "hw/s390x/storage-keys.h"
86 #include "hw/s390x/storage-attributes.h"
89 /* file descriptors passed via SCM_RIGHTS */
90 typedef struct mon_fd_t mon_fd_t
;
94 QLIST_ENTRY(mon_fd_t
) next
;
97 /* file descriptor associated with a file descriptor set */
98 typedef struct MonFdsetFd MonFdsetFd
;
103 QLIST_ENTRY(MonFdsetFd
) next
;
106 /* file descriptor set containing fds passed via SCM_RIGHTS */
107 typedef struct MonFdset MonFdset
;
110 QLIST_HEAD(, MonFdsetFd
) fds
;
111 QLIST_HEAD(, MonFdsetFd
) dup_fds
;
112 QLIST_ENTRY(MonFdset
) next
;
116 * To prevent flooding clients, events can be throttled. The
117 * throttling is calculated globally, rather than per-Monitor
120 typedef struct MonitorQAPIEventState
{
121 QAPIEvent event
; /* Throttling state for this event type and... */
122 QDict
*data
; /* ... data, see qapi_event_throttle_equal() */
123 QEMUTimer
*timer
; /* Timer for handling delayed events */
124 QDict
*qdict
; /* Delayed event (if any) */
125 } MonitorQAPIEventState
;
128 int64_t rate
; /* Minimum time (in ns) between two events */
129 } MonitorQAPIEventConf
;
131 /* Shared monitor I/O thread */
132 IOThread
*mon_iothread
;
134 /* Bottom half to dispatch the requests received from I/O thread */
135 QEMUBH
*qmp_dispatcher_bh
;
137 /* QMP checker flags */
138 #define QMP_ACCEPT_UNKNOWNS 1
140 /* Protects mon_list, monitor_qapi_event_state, monitor_destroyed. */
141 QemuMutex monitor_lock
;
142 static GHashTable
*monitor_qapi_event_state
;
143 MonitorList mon_list
;
144 static bool monitor_destroyed
;
146 /* Protects mon_fdsets */
147 static QemuMutex mon_fdsets_lock
;
148 static QLIST_HEAD(, MonFdset
) mon_fdsets
;
152 static HMPCommand hmp_cmds
[];
153 static HMPCommand hmp_info_cmds
[];
155 __thread Monitor
*cur_mon
;
157 static void monitor_command_cb(void *opaque
, const char *cmdline
,
158 void *readline_opaque
);
161 * Is @mon is using readline?
162 * Note: not all HMP monitors use readline, e.g., gdbserver has a
163 * non-interactive HMP monitor, so readline is not used there.
165 static inline bool monitor_uses_readline(const Monitor
*mon
)
167 return mon
->flags
& MONITOR_USE_READLINE
;
170 static inline bool monitor_is_hmp_non_interactive(const Monitor
*mon
)
172 return !monitor_is_qmp(mon
) && !monitor_uses_readline(mon
);
176 * Return the clock to use for recording an event's time.
177 * It's QEMU_CLOCK_REALTIME, except for qtests it's
178 * QEMU_CLOCK_VIRTUAL, to support testing rate limits.
179 * Beware: result is invalid before configure_accelerator().
181 static inline QEMUClockType
monitor_get_event_clock(void)
183 return qtest_enabled() ? QEMU_CLOCK_VIRTUAL
: QEMU_CLOCK_REALTIME
;
187 * Is the current monitor, if any, a QMP monitor?
189 bool monitor_cur_is_qmp(void)
191 return cur_mon
&& monitor_is_qmp(cur_mon
);
194 void monitor_read_command(MonitorHMP
*mon
, int show_prompt
)
199 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
201 readline_show_prompt(mon
->rs
);
204 int monitor_read_password(MonitorHMP
*mon
, ReadLineFunc
*readline_func
,
208 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
209 /* prompt is printed on return from the command handler */
212 monitor_printf(&mon
->common
,
213 "terminal does not support password prompting\n");
219 static void monitor_flush_locked(Monitor
*mon
);
221 static gboolean
monitor_unblocked(GIOChannel
*chan
, GIOCondition cond
,
224 Monitor
*mon
= opaque
;
226 qemu_mutex_lock(&mon
->mon_lock
);
228 monitor_flush_locked(mon
);
229 qemu_mutex_unlock(&mon
->mon_lock
);
233 /* Caller must hold mon->mon_lock */
234 static void monitor_flush_locked(Monitor
*mon
)
240 if (mon
->skip_flush
) {
244 buf
= qstring_get_str(mon
->outbuf
);
245 len
= qstring_get_length(mon
->outbuf
);
247 if (len
&& !mon
->mux_out
) {
248 rc
= qemu_chr_fe_write(&mon
->chr
, (const uint8_t *) buf
, len
);
249 if ((rc
< 0 && errno
!= EAGAIN
) || (rc
== len
)) {
250 /* all flushed or error */
251 qobject_unref(mon
->outbuf
);
252 mon
->outbuf
= qstring_new();
257 QString
*tmp
= qstring_from_str(buf
+ rc
);
258 qobject_unref(mon
->outbuf
);
261 if (mon
->out_watch
== 0) {
263 qemu_chr_fe_add_watch(&mon
->chr
, G_IO_OUT
| G_IO_HUP
,
264 monitor_unblocked
, mon
);
269 void monitor_flush(Monitor
*mon
)
271 qemu_mutex_lock(&mon
->mon_lock
);
272 monitor_flush_locked(mon
);
273 qemu_mutex_unlock(&mon
->mon_lock
);
276 /* flush at every end of line */
277 int monitor_puts(Monitor
*mon
, const char *str
)
282 qemu_mutex_lock(&mon
->mon_lock
);
283 for (i
= 0; str
[i
]; i
++) {
286 qstring_append_chr(mon
->outbuf
, '\r');
288 qstring_append_chr(mon
->outbuf
, c
);
290 monitor_flush_locked(mon
);
293 qemu_mutex_unlock(&mon
->mon_lock
);
298 int monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
306 if (monitor_is_qmp(mon
)) {
310 buf
= g_strdup_vprintf(fmt
, ap
);
311 n
= monitor_puts(mon
, buf
);
316 int monitor_printf(Monitor
*mon
, const char *fmt
, ...)
322 ret
= monitor_vprintf(mon
, fmt
, ap
);
327 static MonitorQAPIEventConf monitor_qapi_event_conf
[QAPI_EVENT__MAX
] = {
328 /* Limit guest-triggerable events to 1 per second */
329 [QAPI_EVENT_RTC_CHANGE
] = { 1000 * SCALE_MS
},
330 [QAPI_EVENT_WATCHDOG
] = { 1000 * SCALE_MS
},
331 [QAPI_EVENT_BALLOON_CHANGE
] = { 1000 * SCALE_MS
},
332 [QAPI_EVENT_QUORUM_REPORT_BAD
] = { 1000 * SCALE_MS
},
333 [QAPI_EVENT_QUORUM_FAILURE
] = { 1000 * SCALE_MS
},
334 [QAPI_EVENT_VSERPORT_CHANGE
] = { 1000 * SCALE_MS
},
338 * Broadcast an event to all monitors.
339 * @qdict is the event object. Its member "event" must match @event.
340 * Caller must hold monitor_lock.
342 static void monitor_qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
347 trace_monitor_protocol_event_emit(event
, qdict
);
348 QTAILQ_FOREACH(mon
, &mon_list
, entry
) {
349 if (!monitor_is_qmp(mon
)) {
353 qmp_mon
= container_of(mon
, MonitorQMP
, common
);
354 if (qmp_mon
->commands
!= &qmp_cap_negotiation_commands
) {
355 qmp_send_response(qmp_mon
, qdict
);
360 static void monitor_qapi_event_handler(void *opaque
);
363 * Queue a new event for emission to Monitor instances,
364 * applying any rate limiting if required.
367 monitor_qapi_event_queue_no_reenter(QAPIEvent event
, QDict
*qdict
)
369 MonitorQAPIEventConf
*evconf
;
370 MonitorQAPIEventState
*evstate
;
372 assert(event
< QAPI_EVENT__MAX
);
373 evconf
= &monitor_qapi_event_conf
[event
];
374 trace_monitor_protocol_event_queue(event
, qdict
, evconf
->rate
);
376 qemu_mutex_lock(&monitor_lock
);
379 /* Unthrottled event */
380 monitor_qapi_event_emit(event
, qdict
);
382 QDict
*data
= qobject_to(QDict
, qdict_get(qdict
, "data"));
383 MonitorQAPIEventState key
= { .event
= event
, .data
= data
};
385 evstate
= g_hash_table_lookup(monitor_qapi_event_state
, &key
);
386 assert(!evstate
|| timer_pending(evstate
->timer
));
390 * Timer is pending for (at least) evconf->rate ns after
391 * last send. Store event for sending when timer fires,
392 * replacing a prior stored event if any.
394 qobject_unref(evstate
->qdict
);
395 evstate
->qdict
= qobject_ref(qdict
);
398 * Last send was (at least) evconf->rate ns ago.
399 * Send immediately, and arm the timer to call
400 * monitor_qapi_event_handler() in evconf->rate ns. Any
401 * events arriving before then will be delayed until then.
403 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
405 monitor_qapi_event_emit(event
, qdict
);
407 evstate
= g_new(MonitorQAPIEventState
, 1);
408 evstate
->event
= event
;
409 evstate
->data
= qobject_ref(data
);
410 evstate
->qdict
= NULL
;
411 evstate
->timer
= timer_new_ns(monitor_get_event_clock(),
412 monitor_qapi_event_handler
,
414 g_hash_table_add(monitor_qapi_event_state
, evstate
);
415 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
419 qemu_mutex_unlock(&monitor_lock
);
422 void qapi_event_emit(QAPIEvent event
, QDict
*qdict
)
425 * monitor_qapi_event_queue_no_reenter() is not reentrant: it
426 * would deadlock on monitor_lock. Work around by queueing
427 * events in thread-local storage.
428 * TODO: remove this, make it re-enter safe.
430 typedef struct MonitorQapiEvent
{
433 QSIMPLEQ_ENTRY(MonitorQapiEvent
) entry
;
435 static __thread
QSIMPLEQ_HEAD(, MonitorQapiEvent
) event_queue
;
436 static __thread
bool reentered
;
437 MonitorQapiEvent
*ev
;
440 QSIMPLEQ_INIT(&event_queue
);
443 ev
= g_new(MonitorQapiEvent
, 1);
444 ev
->qdict
= qobject_ref(qdict
);
446 QSIMPLEQ_INSERT_TAIL(&event_queue
, ev
, entry
);
453 while ((ev
= QSIMPLEQ_FIRST(&event_queue
)) != NULL
) {
454 QSIMPLEQ_REMOVE_HEAD(&event_queue
, entry
);
455 monitor_qapi_event_queue_no_reenter(ev
->event
, ev
->qdict
);
456 qobject_unref(ev
->qdict
);
464 * This function runs evconf->rate ns after sending a throttled
466 * If another event has since been stored, send it.
468 static void monitor_qapi_event_handler(void *opaque
)
470 MonitorQAPIEventState
*evstate
= opaque
;
471 MonitorQAPIEventConf
*evconf
= &monitor_qapi_event_conf
[evstate
->event
];
473 trace_monitor_protocol_event_handler(evstate
->event
, evstate
->qdict
);
474 qemu_mutex_lock(&monitor_lock
);
476 if (evstate
->qdict
) {
477 int64_t now
= qemu_clock_get_ns(monitor_get_event_clock());
479 monitor_qapi_event_emit(evstate
->event
, evstate
->qdict
);
480 qobject_unref(evstate
->qdict
);
481 evstate
->qdict
= NULL
;
482 timer_mod_ns(evstate
->timer
, now
+ evconf
->rate
);
484 g_hash_table_remove(monitor_qapi_event_state
, evstate
);
485 qobject_unref(evstate
->data
);
486 timer_free(evstate
->timer
);
490 qemu_mutex_unlock(&monitor_lock
);
493 static unsigned int qapi_event_throttle_hash(const void *key
)
495 const MonitorQAPIEventState
*evstate
= key
;
496 unsigned int hash
= evstate
->event
* 255;
498 if (evstate
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
499 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "id"));
502 if (evstate
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
503 hash
+= g_str_hash(qdict_get_str(evstate
->data
, "node-name"));
509 static gboolean
qapi_event_throttle_equal(const void *a
, const void *b
)
511 const MonitorQAPIEventState
*eva
= a
;
512 const MonitorQAPIEventState
*evb
= b
;
514 if (eva
->event
!= evb
->event
) {
518 if (eva
->event
== QAPI_EVENT_VSERPORT_CHANGE
) {
519 return !strcmp(qdict_get_str(eva
->data
, "id"),
520 qdict_get_str(evb
->data
, "id"));
523 if (eva
->event
== QAPI_EVENT_QUORUM_REPORT_BAD
) {
524 return !strcmp(qdict_get_str(eva
->data
, "node-name"),
525 qdict_get_str(evb
->data
, "node-name"));
531 static void monitor_qapi_event_init(void)
533 monitor_qapi_event_state
= g_hash_table_new(qapi_event_throttle_hash
,
534 qapi_event_throttle_equal
);
537 static void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
);
539 static void monitor_iothread_init(void);
541 void monitor_data_init(Monitor
*mon
, int flags
, bool skip_flush
,
544 if (use_io_thread
&& !mon_iothread
) {
545 monitor_iothread_init();
547 qemu_mutex_init(&mon
->mon_lock
);
548 mon
->outbuf
= qstring_new();
549 mon
->skip_flush
= skip_flush
;
550 mon
->use_io_thread
= use_io_thread
;
554 static void monitor_data_destroy(Monitor
*mon
)
556 g_free(mon
->mon_cpu_path
);
557 qemu_chr_fe_deinit(&mon
->chr
, false);
558 if (monitor_is_qmp(mon
)) {
559 monitor_data_destroy_qmp(container_of(mon
, MonitorQMP
, common
));
561 readline_free(container_of(mon
, MonitorHMP
, common
)->rs
);
563 qobject_unref(mon
->outbuf
);
564 qemu_mutex_destroy(&mon
->mon_lock
);
567 char *qmp_human_monitor_command(const char *command_line
, bool has_cpu_index
,
568 int64_t cpu_index
, Error
**errp
)
574 monitor_data_init(&hmp
.common
, 0, true, false);
577 cur_mon
= &hmp
.common
;
580 int ret
= monitor_set_cpu(cpu_index
);
583 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "cpu-index",
589 handle_hmp_command(&hmp
, command_line
);
592 qemu_mutex_lock(&hmp
.common
.mon_lock
);
593 if (qstring_get_length(hmp
.common
.outbuf
) > 0) {
594 output
= g_strdup(qstring_get_str(hmp
.common
.outbuf
));
596 output
= g_strdup("");
598 qemu_mutex_unlock(&hmp
.common
.mon_lock
);
601 monitor_data_destroy(&hmp
.common
);
605 static int compare_cmd(const char *name
, const char *list
)
607 const char *p
, *pstart
;
613 p
= qemu_strchrnul(p
, '|');
614 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
623 static int get_str(char *buf
, int buf_size
, const char **pp
)
631 while (qemu_isspace(*p
)) {
642 while (*p
!= '\0' && *p
!= '\"') {
658 printf("unsupported escape code: '\\%c'\n", c
);
661 if ((q
- buf
) < buf_size
- 1) {
665 if ((q
- buf
) < buf_size
- 1) {
672 printf("unterminated string\n");
677 while (*p
!= '\0' && !qemu_isspace(*p
)) {
678 if ((q
- buf
) < buf_size
- 1) {
691 static void free_cmdline_args(char **args
, int nb_args
)
695 assert(nb_args
<= MAX_ARGS
);
697 for (i
= 0; i
< nb_args
; i
++) {
704 * Parse the command line to get valid args.
705 * @cmdline: command line to be parsed.
706 * @pnb_args: location to store the number of args, must NOT be NULL.
707 * @args: location to store the args, which should be freed by caller, must
710 * Returns 0 on success, negative on failure.
712 * NOTE: this parser is an approximate form of the real command parser. Number
713 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
714 * return with failure.
716 static int parse_cmdline(const char *cmdline
,
717 int *pnb_args
, char **args
)
726 while (qemu_isspace(*p
)) {
732 if (nb_args
>= MAX_ARGS
) {
735 ret
= get_str(buf
, sizeof(buf
), &p
);
739 args
[nb_args
] = g_strdup(buf
);
746 free_cmdline_args(args
, nb_args
);
751 * Can command @cmd be executed in preconfig state?
753 static bool cmd_can_preconfig(const HMPCommand
*cmd
)
759 return strchr(cmd
->flags
, 'p');
762 static void help_cmd_dump_one(Monitor
*mon
,
763 const HMPCommand
*cmd
,
769 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
773 for (i
= 0; i
< prefix_args_nb
; i
++) {
774 monitor_printf(mon
, "%s ", prefix_args
[i
]);
776 monitor_printf(mon
, "%s %s -- %s\n", cmd
->name
, cmd
->params
, cmd
->help
);
779 /* @args[@arg_index] is the valid command need to find in @cmds */
780 static void help_cmd_dump(Monitor
*mon
, const HMPCommand
*cmds
,
781 char **args
, int nb_args
, int arg_index
)
783 const HMPCommand
*cmd
;
786 /* No valid arg need to compare with, dump all in *cmds */
787 if (arg_index
>= nb_args
) {
788 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
789 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
794 /* Find one entry to dump */
795 for (cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
796 if (compare_cmd(args
[arg_index
], cmd
->name
) &&
797 ((!runstate_check(RUN_STATE_PRECONFIG
) ||
798 cmd_can_preconfig(cmd
)))) {
799 if (cmd
->sub_table
) {
800 /* continue with next arg */
801 help_cmd_dump(mon
, cmd
->sub_table
,
802 args
, nb_args
, arg_index
+ 1);
804 help_cmd_dump_one(mon
, cmd
, args
, arg_index
);
810 /* Command not found */
811 monitor_printf(mon
, "unknown command: '");
812 for (i
= 0; i
<= arg_index
; i
++) {
813 monitor_printf(mon
, "%s%s", args
[i
], i
== arg_index
? "'\n" : " ");
817 static void help_cmd(Monitor
*mon
, const char *name
)
819 char *args
[MAX_ARGS
];
822 /* 1. parse user input */
824 /* special case for log, directly dump and return */
825 if (!strcmp(name
, "log")) {
826 const QEMULogItem
*item
;
827 monitor_printf(mon
, "Log items (comma separated):\n");
828 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
829 for (item
= qemu_log_items
; item
->mask
!= 0; item
++) {
830 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
835 if (parse_cmdline(name
, &nb_args
, args
) < 0) {
840 /* 2. dump the contents according to parsed args */
841 help_cmd_dump(mon
, hmp_cmds
, args
, nb_args
, 0);
843 free_cmdline_args(args
, nb_args
);
846 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
848 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
851 static void hmp_trace_event(Monitor
*mon
, const QDict
*qdict
)
853 const char *tp_name
= qdict_get_str(qdict
, "name");
854 bool new_state
= qdict_get_bool(qdict
, "option");
855 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
856 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
857 Error
*local_err
= NULL
;
860 monitor_printf(mon
, "argument vcpu must be positive");
864 qmp_trace_event_set_state(tp_name
, new_state
, true, true, has_vcpu
, vcpu
, &local_err
);
866 error_report_err(local_err
);
870 #ifdef CONFIG_TRACE_SIMPLE
871 static void hmp_trace_file(Monitor
*mon
, const QDict
*qdict
)
873 const char *op
= qdict_get_try_str(qdict
, "op");
874 const char *arg
= qdict_get_try_str(qdict
, "arg");
877 st_print_trace_file_status();
878 } else if (!strcmp(op
, "on")) {
879 st_set_trace_file_enabled(true);
880 } else if (!strcmp(op
, "off")) {
881 st_set_trace_file_enabled(false);
882 } else if (!strcmp(op
, "flush")) {
883 st_flush_trace_buffer();
884 } else if (!strcmp(op
, "set")) {
886 st_set_trace_file(arg
);
889 monitor_printf(mon
, "unexpected argument \"%s\"\n", op
);
890 help_cmd(mon
, "trace-file");
895 static void hmp_info_help(Monitor
*mon
, const QDict
*qdict
)
897 help_cmd(mon
, "info");
900 static void query_commands_cb(QmpCommand
*cmd
, void *opaque
)
902 CommandInfoList
*info
, **list
= opaque
;
908 info
= g_malloc0(sizeof(*info
));
909 info
->value
= g_malloc0(sizeof(*info
->value
));
910 info
->value
->name
= g_strdup(cmd
->name
);
915 CommandInfoList
*qmp_query_commands(Error
**errp
)
917 CommandInfoList
*list
= NULL
;
920 assert(monitor_is_qmp(cur_mon
));
921 mon
= container_of(cur_mon
, MonitorQMP
, common
);
923 qmp_for_each_command(mon
->commands
, query_commands_cb
, &list
);
928 EventInfoList
*qmp_query_events(Error
**errp
)
931 * TODO This deprecated command is the only user of
932 * QAPIEvent_str() and QAPIEvent_lookup[]. When the command goes,
933 * they should go, too.
935 EventInfoList
*info
, *ev_list
= NULL
;
938 for (e
= 0 ; e
< QAPI_EVENT__MAX
; e
++) {
939 const char *event_name
= QAPIEvent_str(e
);
940 assert(event_name
!= NULL
);
941 info
= g_malloc0(sizeof(*info
));
942 info
->value
= g_malloc0(sizeof(*info
->value
));
943 info
->value
->name
= g_strdup(event_name
);
945 info
->next
= ev_list
;
953 * Minor hack: generated marshalling suppressed for this command
954 * ('gen': false in the schema) so we can parse the JSON string
955 * directly into QObject instead of first parsing it with
956 * visit_type_SchemaInfoList() into a SchemaInfoList, then marshal it
957 * to QObject with generated output marshallers, every time. Instead,
958 * we do it in test-qobject-input-visitor.c, just to make sure
959 * qapi-gen.py's output actually conforms to the schema.
961 static void qmp_query_qmp_schema(QDict
*qdict
, QObject
**ret_data
,
964 *ret_data
= qobject_from_qlit(&qmp_schema_qlit
);
967 static void monitor_init_qmp_commands(void)
971 * - qmp_commands contains all QMP commands
972 * - qmp_cap_negotiation_commands contains just
973 * "qmp_capabilities", to enforce capability negotiation
976 qmp_init_marshal(&qmp_commands
);
978 qmp_register_command(&qmp_commands
, "query-qmp-schema",
979 qmp_query_qmp_schema
, QCO_ALLOW_PRECONFIG
);
980 qmp_register_command(&qmp_commands
, "device_add", qmp_device_add
,
982 qmp_register_command(&qmp_commands
, "netdev_add", qmp_netdev_add
,
985 QTAILQ_INIT(&qmp_cap_negotiation_commands
);
986 qmp_register_command(&qmp_cap_negotiation_commands
, "qmp_capabilities",
987 qmp_marshal_qmp_capabilities
, QCO_ALLOW_PRECONFIG
);
991 * Accept QMP capabilities in @list for @mon.
992 * On success, set mon->qmp.capab[], and return true.
993 * On error, set @errp, and return false.
995 static bool qmp_caps_accept(MonitorQMP
*mon
, QMPCapabilityList
*list
,
998 GString
*unavailable
= NULL
;
999 bool capab
[QMP_CAPABILITY__MAX
];
1001 memset(capab
, 0, sizeof(capab
));
1003 for (; list
; list
= list
->next
) {
1004 if (!mon
->capab_offered
[list
->value
]) {
1006 unavailable
= g_string_new(QMPCapability_str(list
->value
));
1008 g_string_append_printf(unavailable
, ", %s",
1009 QMPCapability_str(list
->value
));
1012 capab
[list
->value
] = true;
1016 error_setg(errp
, "Capability %s not available", unavailable
->str
);
1017 g_string_free(unavailable
, true);
1021 memcpy(mon
->capab
, capab
, sizeof(capab
));
1025 void qmp_qmp_capabilities(bool has_enable
, QMPCapabilityList
*enable
,
1030 assert(monitor_is_qmp(cur_mon
));
1031 mon
= container_of(cur_mon
, MonitorQMP
, common
);
1033 if (mon
->commands
== &qmp_commands
) {
1034 error_set(errp
, ERROR_CLASS_COMMAND_NOT_FOUND
,
1035 "Capabilities negotiation is already complete, command "
1040 if (!qmp_caps_accept(mon
, enable
, errp
)) {
1044 mon
->commands
= &qmp_commands
;
1047 /* Set the current CPU defined by the user. Callers must hold BQL. */
1048 int monitor_set_cpu(int cpu_index
)
1052 cpu
= qemu_get_cpu(cpu_index
);
1056 g_free(cur_mon
->mon_cpu_path
);
1057 cur_mon
->mon_cpu_path
= object_get_canonical_path(OBJECT(cpu
));
1061 /* Callers must hold BQL. */
1062 static CPUState
*mon_get_cpu_sync(bool synchronize
)
1066 if (cur_mon
->mon_cpu_path
) {
1067 cpu
= (CPUState
*) object_resolve_path_type(cur_mon
->mon_cpu_path
,
1070 g_free(cur_mon
->mon_cpu_path
);
1071 cur_mon
->mon_cpu_path
= NULL
;
1074 if (!cur_mon
->mon_cpu_path
) {
1078 monitor_set_cpu(first_cpu
->cpu_index
);
1082 cpu_synchronize_state(cpu
);
1087 CPUState
*mon_get_cpu(void)
1089 return mon_get_cpu_sync(true);
1092 CPUArchState
*mon_get_cpu_env(void)
1094 CPUState
*cs
= mon_get_cpu();
1096 return cs
? cs
->env_ptr
: NULL
;
1099 int monitor_get_cpu_index(void)
1101 CPUState
*cs
= mon_get_cpu_sync(false);
1103 return cs
? cs
->cpu_index
: UNASSIGNED_CPU_INDEX
;
1106 static void hmp_info_registers(Monitor
*mon
, const QDict
*qdict
)
1108 bool all_cpus
= qdict_get_try_bool(qdict
, "cpustate_all", false);
1113 monitor_printf(mon
, "\nCPU#%d\n", cs
->cpu_index
);
1114 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1120 monitor_printf(mon
, "No CPU available\n");
1124 cpu_dump_state(cs
, NULL
, CPU_DUMP_FPU
);
1129 static void hmp_info_jit(Monitor
*mon
, const QDict
*qdict
)
1131 if (!tcg_enabled()) {
1132 error_report("JIT information is only available with accel=tcg");
1140 static void hmp_info_opcount(Monitor
*mon
, const QDict
*qdict
)
1142 dump_opcount_info();
1146 static void hmp_info_sync_profile(Monitor
*mon
, const QDict
*qdict
)
1148 int64_t max
= qdict_get_try_int(qdict
, "max", 10);
1149 bool mean
= qdict_get_try_bool(qdict
, "mean", false);
1150 bool coalesce
= !qdict_get_try_bool(qdict
, "no_coalesce", false);
1151 enum QSPSortBy sort_by
;
1153 sort_by
= mean
? QSP_SORT_BY_AVG_WAIT_TIME
: QSP_SORT_BY_TOTAL_WAIT_TIME
;
1154 qsp_report(max
, sort_by
, coalesce
);
1157 static void hmp_info_history(Monitor
*mon
, const QDict
*qdict
)
1159 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
1168 str
= readline_get_history(hmp_mon
->rs
, i
);
1172 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
1177 static void hmp_info_cpustats(Monitor
*mon
, const QDict
*qdict
)
1179 CPUState
*cs
= mon_get_cpu();
1182 monitor_printf(mon
, "No CPU available\n");
1185 cpu_dump_statistics(cs
, 0);
1188 static void hmp_info_trace_events(Monitor
*mon
, const QDict
*qdict
)
1190 const char *name
= qdict_get_try_str(qdict
, "name");
1191 bool has_vcpu
= qdict_haskey(qdict
, "vcpu");
1192 int vcpu
= qdict_get_try_int(qdict
, "vcpu", 0);
1193 TraceEventInfoList
*events
;
1194 TraceEventInfoList
*elem
;
1195 Error
*local_err
= NULL
;
1201 monitor_printf(mon
, "argument vcpu must be positive");
1205 events
= qmp_trace_event_get_state(name
, has_vcpu
, vcpu
, &local_err
);
1207 error_report_err(local_err
);
1211 for (elem
= events
; elem
!= NULL
; elem
= elem
->next
) {
1212 monitor_printf(mon
, "%s : state %u\n",
1214 elem
->value
->state
== TRACE_EVENT_STATE_ENABLED
? 1 : 0);
1216 qapi_free_TraceEventInfoList(events
);
1219 void qmp_client_migrate_info(const char *protocol
, const char *hostname
,
1220 bool has_port
, int64_t port
,
1221 bool has_tls_port
, int64_t tls_port
,
1222 bool has_cert_subject
, const char *cert_subject
,
1225 if (strcmp(protocol
, "spice") == 0) {
1226 if (!qemu_using_spice(errp
)) {
1230 if (!has_port
&& !has_tls_port
) {
1231 error_setg(errp
, QERR_MISSING_PARAMETER
, "port/tls-port");
1235 if (qemu_spice_migrate_info(hostname
,
1236 has_port
? port
: -1,
1237 has_tls_port
? tls_port
: -1,
1239 error_setg(errp
, QERR_UNDEFINED_ERROR
);
1245 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "protocol", "spice");
1248 static void hmp_logfile(Monitor
*mon
, const QDict
*qdict
)
1252 qemu_set_log_filename(qdict_get_str(qdict
, "filename"), &err
);
1254 error_report_err(err
);
1258 static void hmp_log(Monitor
*mon
, const QDict
*qdict
)
1261 const char *items
= qdict_get_str(qdict
, "items");
1263 if (!strcmp(items
, "none")) {
1266 mask
= qemu_str_to_log_mask(items
);
1268 help_cmd(mon
, "log");
1275 static void hmp_singlestep(Monitor
*mon
, const QDict
*qdict
)
1277 const char *option
= qdict_get_try_str(qdict
, "option");
1278 if (!option
|| !strcmp(option
, "on")) {
1280 } else if (!strcmp(option
, "off")) {
1283 monitor_printf(mon
, "unexpected option %s\n", option
);
1287 static void hmp_gdbserver(Monitor
*mon
, const QDict
*qdict
)
1289 const char *device
= qdict_get_try_str(qdict
, "device");
1291 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
1292 if (gdbserver_start(device
) < 0) {
1293 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
1295 } else if (strcmp(device
, "none") == 0) {
1296 monitor_printf(mon
, "Disabled gdbserver\n");
1298 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
1303 static void hmp_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
1305 const char *action
= qdict_get_str(qdict
, "action");
1306 if (select_watchdog_action(action
) == -1) {
1307 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
1311 static void monitor_printc(Monitor
*mon
, int c
)
1313 monitor_printf(mon
, "'");
1316 monitor_printf(mon
, "\\'");
1319 monitor_printf(mon
, "\\\\");
1322 monitor_printf(mon
, "\\n");
1325 monitor_printf(mon
, "\\r");
1328 if (c
>= 32 && c
<= 126) {
1329 monitor_printf(mon
, "%c", c
);
1331 monitor_printf(mon
, "\\x%02x", c
);
1335 monitor_printf(mon
, "'");
1338 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
1339 hwaddr addr
, int is_physical
)
1341 int l
, line_size
, i
, max_digits
, len
;
1344 CPUState
*cs
= mon_get_cpu();
1346 if (!cs
&& (format
== 'i' || !is_physical
)) {
1347 monitor_printf(mon
, "Can not dump without CPU\n");
1351 if (format
== 'i') {
1352 monitor_disas(mon
, cs
, addr
, count
, is_physical
);
1356 len
= wsize
* count
;
1365 max_digits
= DIV_ROUND_UP(wsize
* 8, 3);
1369 max_digits
= (wsize
* 8) / 4;
1373 max_digits
= DIV_ROUND_UP(wsize
* 8 * 10, 33);
1382 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
1384 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
1389 AddressSpace
*as
= cs
? cs
->as
: &address_space_memory
;
1390 MemTxResult r
= address_space_read(as
, addr
,
1391 MEMTXATTRS_UNSPECIFIED
, buf
, l
);
1392 if (r
!= MEMTX_OK
) {
1393 monitor_printf(mon
, " Cannot access memory\n");
1397 if (cpu_memory_rw_debug(cs
, addr
, buf
, l
, 0) < 0) {
1398 monitor_printf(mon
, " Cannot access memory\n");
1407 v
= ldub_p(buf
+ i
);
1410 v
= lduw_p(buf
+ i
);
1413 v
= (uint32_t)ldl_p(buf
+ i
);
1419 monitor_printf(mon
, " ");
1422 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
1425 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
1428 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
1431 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1434 monitor_printc(mon
, v
);
1439 monitor_printf(mon
, "\n");
1445 static void hmp_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1447 int count
= qdict_get_int(qdict
, "count");
1448 int format
= qdict_get_int(qdict
, "format");
1449 int size
= qdict_get_int(qdict
, "size");
1450 target_long addr
= qdict_get_int(qdict
, "addr");
1452 memory_dump(mon
, count
, format
, size
, addr
, 0);
1455 static void hmp_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1457 int count
= qdict_get_int(qdict
, "count");
1458 int format
= qdict_get_int(qdict
, "format");
1459 int size
= qdict_get_int(qdict
, "size");
1460 hwaddr addr
= qdict_get_int(qdict
, "addr");
1462 memory_dump(mon
, count
, format
, size
, addr
, 1);
1465 static void *gpa2hva(MemoryRegion
**p_mr
, hwaddr addr
, Error
**errp
)
1467 MemoryRegionSection mrs
= memory_region_find(get_system_memory(),
1471 error_setg(errp
, "No memory is mapped at address 0x%" HWADDR_PRIx
, addr
);
1475 if (!memory_region_is_ram(mrs
.mr
) && !memory_region_is_romd(mrs
.mr
)) {
1476 error_setg(errp
, "Memory at address 0x%" HWADDR_PRIx
"is not RAM", addr
);
1477 memory_region_unref(mrs
.mr
);
1482 return qemu_map_ram_ptr(mrs
.mr
->ram_block
, mrs
.offset_within_region
);
1485 static void hmp_gpa2hva(Monitor
*mon
, const QDict
*qdict
)
1487 hwaddr addr
= qdict_get_int(qdict
, "addr");
1488 Error
*local_err
= NULL
;
1489 MemoryRegion
*mr
= NULL
;
1492 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1494 error_report_err(local_err
);
1498 monitor_printf(mon
, "Host virtual address for 0x%" HWADDR_PRIx
1500 addr
, mr
->name
, ptr
);
1502 memory_region_unref(mr
);
1505 static void hmp_gva2gpa(Monitor
*mon
, const QDict
*qdict
)
1507 target_ulong addr
= qdict_get_int(qdict
, "addr");
1509 CPUState
*cs
= mon_get_cpu();
1513 monitor_printf(mon
, "No cpu\n");
1517 gpa
= cpu_get_phys_page_attrs_debug(cs
, addr
& TARGET_PAGE_MASK
, &attrs
);
1519 monitor_printf(mon
, "Unmapped\n");
1521 monitor_printf(mon
, "gpa: %#" HWADDR_PRIx
"\n",
1522 gpa
+ (addr
& ~TARGET_PAGE_MASK
));
1527 static uint64_t vtop(void *ptr
, Error
**errp
)
1531 uintptr_t addr
= (uintptr_t) ptr
;
1532 uintptr_t pagesize
= getpagesize();
1533 off_t offset
= addr
/ pagesize
* sizeof(pinfo
);
1536 fd
= open("/proc/self/pagemap", O_RDONLY
);
1538 error_setg_errno(errp
, errno
, "Cannot open /proc/self/pagemap");
1542 /* Force copy-on-write if necessary. */
1543 atomic_add((uint8_t *)ptr
, 0);
1545 if (pread(fd
, &pinfo
, sizeof(pinfo
), offset
) != sizeof(pinfo
)) {
1546 error_setg_errno(errp
, errno
, "Cannot read pagemap");
1549 if ((pinfo
& (1ull << 63)) == 0) {
1550 error_setg(errp
, "Page not present");
1553 ret
= ((pinfo
& 0x007fffffffffffffull
) * pagesize
) | (addr
& (pagesize
- 1));
1560 static void hmp_gpa2hpa(Monitor
*mon
, const QDict
*qdict
)
1562 hwaddr addr
= qdict_get_int(qdict
, "addr");
1563 Error
*local_err
= NULL
;
1564 MemoryRegion
*mr
= NULL
;
1568 ptr
= gpa2hva(&mr
, addr
, &local_err
);
1570 error_report_err(local_err
);
1574 physaddr
= vtop(ptr
, &local_err
);
1576 error_report_err(local_err
);
1578 monitor_printf(mon
, "Host physical address for 0x%" HWADDR_PRIx
1579 " (%s) is 0x%" PRIx64
"\n",
1580 addr
, mr
->name
, (uint64_t) physaddr
);
1583 memory_region_unref(mr
);
1587 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1589 int format
= qdict_get_int(qdict
, "format");
1590 hwaddr val
= qdict_get_int(qdict
, "val");
1594 monitor_printf(mon
, "%#" HWADDR_PRIo
, val
);
1597 monitor_printf(mon
, "%#" HWADDR_PRIx
, val
);
1600 monitor_printf(mon
, "%" HWADDR_PRIu
, val
);
1604 monitor_printf(mon
, "%" HWADDR_PRId
, val
);
1607 monitor_printc(mon
, val
);
1610 monitor_printf(mon
, "\n");
1613 static void hmp_sum(Monitor
*mon
, const QDict
*qdict
)
1617 uint32_t start
= qdict_get_int(qdict
, "start");
1618 uint32_t size
= qdict_get_int(qdict
, "size");
1621 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1622 uint8_t val
= address_space_ldub(&address_space_memory
, addr
,
1623 MEMTXATTRS_UNSPECIFIED
, NULL
);
1624 /* BSD sum algorithm ('sum' Unix command) */
1625 sum
= (sum
>> 1) | (sum
<< 15);
1628 monitor_printf(mon
, "%05d\n", sum
);
1631 static int mouse_button_state
;
1633 static void hmp_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1635 int dx
, dy
, dz
, button
;
1636 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1637 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1638 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1640 dx
= strtol(dx_str
, NULL
, 0);
1641 dy
= strtol(dy_str
, NULL
, 0);
1642 qemu_input_queue_rel(NULL
, INPUT_AXIS_X
, dx
);
1643 qemu_input_queue_rel(NULL
, INPUT_AXIS_Y
, dy
);
1646 dz
= strtol(dz_str
, NULL
, 0);
1648 button
= (dz
> 0) ? INPUT_BUTTON_WHEEL_UP
: INPUT_BUTTON_WHEEL_DOWN
;
1649 qemu_input_queue_btn(NULL
, button
, true);
1650 qemu_input_event_sync();
1651 qemu_input_queue_btn(NULL
, button
, false);
1654 qemu_input_event_sync();
1657 static void hmp_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1659 static uint32_t bmap
[INPUT_BUTTON__MAX
] = {
1660 [INPUT_BUTTON_LEFT
] = MOUSE_EVENT_LBUTTON
,
1661 [INPUT_BUTTON_MIDDLE
] = MOUSE_EVENT_MBUTTON
,
1662 [INPUT_BUTTON_RIGHT
] = MOUSE_EVENT_RBUTTON
,
1664 int button_state
= qdict_get_int(qdict
, "button_state");
1666 if (mouse_button_state
== button_state
) {
1669 qemu_input_update_buttons(NULL
, bmap
, mouse_button_state
, button_state
);
1670 qemu_input_event_sync();
1671 mouse_button_state
= button_state
;
1674 static void hmp_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1676 int size
= qdict_get_int(qdict
, "size");
1677 int addr
= qdict_get_int(qdict
, "addr");
1678 int has_index
= qdict_haskey(qdict
, "index");
1683 int index
= qdict_get_int(qdict
, "index");
1684 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1692 val
= cpu_inb(addr
);
1696 val
= cpu_inw(addr
);
1700 val
= cpu_inl(addr
);
1704 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1705 suffix
, addr
, size
* 2, val
);
1708 static void hmp_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1710 int size
= qdict_get_int(qdict
, "size");
1711 int addr
= qdict_get_int(qdict
, "addr");
1712 int val
= qdict_get_int(qdict
, "val");
1714 addr
&= IOPORTS_MASK
;
1719 cpu_outb(addr
, val
);
1722 cpu_outw(addr
, val
);
1725 cpu_outl(addr
, val
);
1730 static void hmp_boot_set(Monitor
*mon
, const QDict
*qdict
)
1732 Error
*local_err
= NULL
;
1733 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1735 qemu_boot_set(bootdevice
, &local_err
);
1737 error_report_err(local_err
);
1739 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1743 static void hmp_info_mtree(Monitor
*mon
, const QDict
*qdict
)
1745 bool flatview
= qdict_get_try_bool(qdict
, "flatview", false);
1746 bool dispatch_tree
= qdict_get_try_bool(qdict
, "dispatch_tree", false);
1747 bool owner
= qdict_get_try_bool(qdict
, "owner", false);
1749 mtree_info(flatview
, dispatch_tree
, owner
);
1752 static void hmp_info_numa(Monitor
*mon
, const QDict
*qdict
)
1755 NumaNodeMem
*node_mem
;
1756 CpuInfoList
*cpu_list
, *cpu
;
1758 cpu_list
= qmp_query_cpus(&error_abort
);
1759 node_mem
= g_new0(NumaNodeMem
, nb_numa_nodes
);
1761 query_numa_node_mem(node_mem
);
1762 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1763 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1764 monitor_printf(mon
, "node %d cpus:", i
);
1765 for (cpu
= cpu_list
; cpu
; cpu
= cpu
->next
) {
1766 if (cpu
->value
->has_props
&& cpu
->value
->props
->has_node_id
&&
1767 cpu
->value
->props
->node_id
== i
) {
1768 monitor_printf(mon
, " %" PRIi64
, cpu
->value
->CPU
);
1771 monitor_printf(mon
, "\n");
1772 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1773 node_mem
[i
].node_mem
>> 20);
1774 monitor_printf(mon
, "node %d plugged: %" PRId64
" MB\n", i
,
1775 node_mem
[i
].node_plugged_mem
>> 20);
1777 qapi_free_CpuInfoList(cpu_list
);
1781 #ifdef CONFIG_PROFILER
1785 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1787 static int64_t last_cpu_exec_time
;
1788 int64_t cpu_exec_time
;
1791 cpu_exec_time
= tcg_cpu_exec_time();
1792 delta
= cpu_exec_time
- last_cpu_exec_time
;
1794 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1795 dev_time
, dev_time
/ (double)NANOSECONDS_PER_SECOND
);
1796 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1797 delta
, delta
/ (double)NANOSECONDS_PER_SECOND
);
1798 last_cpu_exec_time
= cpu_exec_time
;
1802 static void hmp_info_profile(Monitor
*mon
, const QDict
*qdict
)
1804 monitor_printf(mon
, "Internal profiler not compiled\n");
1808 /* Capture support */
1809 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1811 static void hmp_info_capture(Monitor
*mon
, const QDict
*qdict
)
1816 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1817 monitor_printf(mon
, "[%d]: ", i
);
1818 s
->ops
.info (s
->opaque
);
1822 static void hmp_stopcapture(Monitor
*mon
, const QDict
*qdict
)
1825 int n
= qdict_get_int(qdict
, "n");
1828 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1830 s
->ops
.destroy (s
->opaque
);
1831 QLIST_REMOVE (s
, entries
);
1838 static void hmp_wavcapture(Monitor
*mon
, const QDict
*qdict
)
1840 const char *path
= qdict_get_str(qdict
, "path");
1841 int has_freq
= qdict_haskey(qdict
, "freq");
1842 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1843 int has_bits
= qdict_haskey(qdict
, "bits");
1844 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1845 int has_channels
= qdict_haskey(qdict
, "nchannels");
1846 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1849 s
= g_malloc0 (sizeof (*s
));
1851 freq
= has_freq
? freq
: 44100;
1852 bits
= has_bits
? bits
: 16;
1853 nchannels
= has_channels
? nchannels
: 2;
1855 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1856 monitor_printf(mon
, "Failed to add wave capture\n");
1860 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1863 static QAuthZList
*find_auth(Monitor
*mon
, const char *name
)
1868 container
= object_get_objects_root();
1869 obj
= object_resolve_path_component(container
, name
);
1871 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1875 return QAUTHZ_LIST(obj
);
1878 static bool warn_acl
;
1879 static void hmp_warn_acl(void)
1884 error_report("The acl_show, acl_reset, acl_policy, acl_add, acl_remove "
1885 "commands are deprecated with no replacement. Authorization "
1886 "for VNC should be performed using the pluggable QAuthZ "
1891 static void hmp_acl_show(Monitor
*mon
, const QDict
*qdict
)
1893 const char *aclname
= qdict_get_str(qdict
, "aclname");
1894 QAuthZList
*auth
= find_auth(mon
, aclname
);
1895 QAuthZListRuleList
*rules
;
1904 monitor_printf(mon
, "policy: %s\n",
1905 QAuthZListPolicy_str(auth
->policy
));
1907 rules
= auth
->rules
;
1909 QAuthZListRule
*rule
= rules
->value
;
1911 monitor_printf(mon
, "%zu: %s %s\n", i
,
1912 QAuthZListPolicy_str(rule
->policy
),
1914 rules
= rules
->next
;
1918 static void hmp_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1920 const char *aclname
= qdict_get_str(qdict
, "aclname");
1921 QAuthZList
*auth
= find_auth(mon
, aclname
);
1929 auth
->policy
= QAUTHZ_LIST_POLICY_DENY
;
1930 qapi_free_QAuthZListRuleList(auth
->rules
);
1932 monitor_printf(mon
, "acl: removed all rules\n");
1935 static void hmp_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1937 const char *aclname
= qdict_get_str(qdict
, "aclname");
1938 const char *policy
= qdict_get_str(qdict
, "policy");
1939 QAuthZList
*auth
= find_auth(mon
, aclname
);
1949 val
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1951 QAUTHZ_LIST_POLICY_DENY
,
1955 monitor_printf(mon
, "acl: unknown policy '%s', "
1956 "expected 'deny' or 'allow'\n", policy
);
1959 if (auth
->policy
== QAUTHZ_LIST_POLICY_ALLOW
) {
1960 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1962 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1967 static QAuthZListFormat
hmp_acl_get_format(const char *match
)
1969 if (strchr(match
, '*')) {
1970 return QAUTHZ_LIST_FORMAT_GLOB
;
1972 return QAUTHZ_LIST_FORMAT_EXACT
;
1976 static void hmp_acl_add(Monitor
*mon
, const QDict
*qdict
)
1978 const char *aclname
= qdict_get_str(qdict
, "aclname");
1979 const char *match
= qdict_get_str(qdict
, "match");
1980 const char *policystr
= qdict_get_str(qdict
, "policy");
1981 int has_index
= qdict_haskey(qdict
, "index");
1982 int index
= qdict_get_try_int(qdict
, "index", -1);
1983 QAuthZList
*auth
= find_auth(mon
, aclname
);
1985 QAuthZListPolicy policy
;
1986 QAuthZListFormat format
;
1995 policy
= qapi_enum_parse(&QAuthZListPolicy_lookup
,
1997 QAUTHZ_LIST_POLICY_DENY
,
2001 monitor_printf(mon
, "acl: unknown policy '%s', "
2002 "expected 'deny' or 'allow'\n", policystr
);
2006 format
= hmp_acl_get_format(match
);
2008 if (has_index
&& index
== 0) {
2009 monitor_printf(mon
, "acl: unable to add acl entry\n");
2014 i
= qauthz_list_insert_rule(auth
, match
, policy
,
2015 format
, index
- 1, &err
);
2017 i
= qauthz_list_append_rule(auth
, match
, policy
,
2021 monitor_printf(mon
, "acl: unable to add rule: %s",
2022 error_get_pretty(err
));
2025 monitor_printf(mon
, "acl: added rule at position %zu\n", i
+ 1);
2029 static void hmp_acl_remove(Monitor
*mon
, const QDict
*qdict
)
2031 const char *aclname
= qdict_get_str(qdict
, "aclname");
2032 const char *match
= qdict_get_str(qdict
, "match");
2033 QAuthZList
*auth
= find_auth(mon
, aclname
);
2042 i
= qauthz_list_delete_rule(auth
, match
);
2044 monitor_printf(mon
, "acl: removed rule at position %zu\n", i
+ 1);
2046 monitor_printf(mon
, "acl: no matching acl entry\n");
2050 void qmp_getfd(const char *fdname
, Error
**errp
)
2055 fd
= qemu_chr_fe_get_msgfd(&cur_mon
->chr
);
2057 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2061 if (qemu_isdigit(fdname
[0])) {
2063 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdname",
2064 "a name not starting with a digit");
2068 qemu_mutex_lock(&cur_mon
->mon_lock
);
2069 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2070 if (strcmp(monfd
->name
, fdname
) != 0) {
2076 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2077 /* Make sure close() is outside critical section */
2082 monfd
= g_malloc0(sizeof(mon_fd_t
));
2083 monfd
->name
= g_strdup(fdname
);
2086 QLIST_INSERT_HEAD(&cur_mon
->fds
, monfd
, next
);
2087 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2090 void qmp_closefd(const char *fdname
, Error
**errp
)
2095 qemu_mutex_lock(&cur_mon
->mon_lock
);
2096 QLIST_FOREACH(monfd
, &cur_mon
->fds
, next
) {
2097 if (strcmp(monfd
->name
, fdname
) != 0) {
2101 QLIST_REMOVE(monfd
, next
);
2103 g_free(monfd
->name
);
2105 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2106 /* Make sure close() is outside critical section */
2111 qemu_mutex_unlock(&cur_mon
->mon_lock
);
2112 error_setg(errp
, QERR_FD_NOT_FOUND
, fdname
);
2115 int monitor_get_fd(Monitor
*mon
, const char *fdname
, Error
**errp
)
2119 qemu_mutex_lock(&mon
->mon_lock
);
2120 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2123 if (strcmp(monfd
->name
, fdname
) != 0) {
2129 /* caller takes ownership of fd */
2130 QLIST_REMOVE(monfd
, next
);
2131 g_free(monfd
->name
);
2133 qemu_mutex_unlock(&mon
->mon_lock
);
2138 qemu_mutex_unlock(&mon
->mon_lock
);
2139 error_setg(errp
, "File descriptor named '%s' has not been found", fdname
);
2143 static void monitor_fdset_cleanup(MonFdset
*mon_fdset
)
2145 MonFdsetFd
*mon_fdset_fd
;
2146 MonFdsetFd
*mon_fdset_fd_next
;
2148 QLIST_FOREACH_SAFE(mon_fdset_fd
, &mon_fdset
->fds
, next
, mon_fdset_fd_next
) {
2149 if ((mon_fdset_fd
->removed
||
2150 (QLIST_EMPTY(&mon_fdset
->dup_fds
) && mon_refcount
== 0)) &&
2151 runstate_is_running()) {
2152 close(mon_fdset_fd
->fd
);
2153 g_free(mon_fdset_fd
->opaque
);
2154 QLIST_REMOVE(mon_fdset_fd
, next
);
2155 g_free(mon_fdset_fd
);
2159 if (QLIST_EMPTY(&mon_fdset
->fds
) && QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2160 QLIST_REMOVE(mon_fdset
, next
);
2165 void monitor_fdsets_cleanup(void)
2167 MonFdset
*mon_fdset
;
2168 MonFdset
*mon_fdset_next
;
2170 qemu_mutex_lock(&mon_fdsets_lock
);
2171 QLIST_FOREACH_SAFE(mon_fdset
, &mon_fdsets
, next
, mon_fdset_next
) {
2172 monitor_fdset_cleanup(mon_fdset
);
2174 qemu_mutex_unlock(&mon_fdsets_lock
);
2177 AddfdInfo
*qmp_add_fd(bool has_fdset_id
, int64_t fdset_id
, bool has_opaque
,
2178 const char *opaque
, Error
**errp
)
2181 Monitor
*mon
= cur_mon
;
2184 fd
= qemu_chr_fe_get_msgfd(&mon
->chr
);
2186 error_setg(errp
, QERR_FD_NOT_SUPPLIED
);
2190 fdinfo
= monitor_fdset_add_fd(fd
, has_fdset_id
, fdset_id
,
2191 has_opaque
, opaque
, errp
);
2203 void qmp_remove_fd(int64_t fdset_id
, bool has_fd
, int64_t fd
, Error
**errp
)
2205 MonFdset
*mon_fdset
;
2206 MonFdsetFd
*mon_fdset_fd
;
2209 qemu_mutex_lock(&mon_fdsets_lock
);
2210 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2211 if (mon_fdset
->id
!= fdset_id
) {
2214 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2216 if (mon_fdset_fd
->fd
!= fd
) {
2219 mon_fdset_fd
->removed
= true;
2222 mon_fdset_fd
->removed
= true;
2225 if (has_fd
&& !mon_fdset_fd
) {
2228 monitor_fdset_cleanup(mon_fdset
);
2229 qemu_mutex_unlock(&mon_fdsets_lock
);
2234 qemu_mutex_unlock(&mon_fdsets_lock
);
2236 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
", fd:%" PRId64
,
2239 snprintf(fd_str
, sizeof(fd_str
), "fdset-id:%" PRId64
, fdset_id
);
2241 error_setg(errp
, QERR_FD_NOT_FOUND
, fd_str
);
2244 FdsetInfoList
*qmp_query_fdsets(Error
**errp
)
2246 MonFdset
*mon_fdset
;
2247 MonFdsetFd
*mon_fdset_fd
;
2248 FdsetInfoList
*fdset_list
= NULL
;
2250 qemu_mutex_lock(&mon_fdsets_lock
);
2251 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2252 FdsetInfoList
*fdset_info
= g_malloc0(sizeof(*fdset_info
));
2253 FdsetFdInfoList
*fdsetfd_list
= NULL
;
2255 fdset_info
->value
= g_malloc0(sizeof(*fdset_info
->value
));
2256 fdset_info
->value
->fdset_id
= mon_fdset
->id
;
2258 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2259 FdsetFdInfoList
*fdsetfd_info
;
2261 fdsetfd_info
= g_malloc0(sizeof(*fdsetfd_info
));
2262 fdsetfd_info
->value
= g_malloc0(sizeof(*fdsetfd_info
->value
));
2263 fdsetfd_info
->value
->fd
= mon_fdset_fd
->fd
;
2264 if (mon_fdset_fd
->opaque
) {
2265 fdsetfd_info
->value
->has_opaque
= true;
2266 fdsetfd_info
->value
->opaque
= g_strdup(mon_fdset_fd
->opaque
);
2268 fdsetfd_info
->value
->has_opaque
= false;
2271 fdsetfd_info
->next
= fdsetfd_list
;
2272 fdsetfd_list
= fdsetfd_info
;
2275 fdset_info
->value
->fds
= fdsetfd_list
;
2277 fdset_info
->next
= fdset_list
;
2278 fdset_list
= fdset_info
;
2280 qemu_mutex_unlock(&mon_fdsets_lock
);
2285 AddfdInfo
*monitor_fdset_add_fd(int fd
, bool has_fdset_id
, int64_t fdset_id
,
2286 bool has_opaque
, const char *opaque
,
2289 MonFdset
*mon_fdset
= NULL
;
2290 MonFdsetFd
*mon_fdset_fd
;
2293 qemu_mutex_lock(&mon_fdsets_lock
);
2295 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2296 /* Break if match found or match impossible due to ordering by ID */
2297 if (fdset_id
<= mon_fdset
->id
) {
2298 if (fdset_id
< mon_fdset
->id
) {
2306 if (mon_fdset
== NULL
) {
2307 int64_t fdset_id_prev
= -1;
2308 MonFdset
*mon_fdset_cur
= QLIST_FIRST(&mon_fdsets
);
2312 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "fdset-id",
2313 "a non-negative value");
2314 qemu_mutex_unlock(&mon_fdsets_lock
);
2317 /* Use specified fdset ID */
2318 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2319 mon_fdset_cur
= mon_fdset
;
2320 if (fdset_id
< mon_fdset_cur
->id
) {
2325 /* Use first available fdset ID */
2326 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2327 mon_fdset_cur
= mon_fdset
;
2328 if (fdset_id_prev
== mon_fdset_cur
->id
- 1) {
2329 fdset_id_prev
= mon_fdset_cur
->id
;
2336 mon_fdset
= g_malloc0(sizeof(*mon_fdset
));
2338 mon_fdset
->id
= fdset_id
;
2340 mon_fdset
->id
= fdset_id_prev
+ 1;
2343 /* The fdset list is ordered by fdset ID */
2344 if (!mon_fdset_cur
) {
2345 QLIST_INSERT_HEAD(&mon_fdsets
, mon_fdset
, next
);
2346 } else if (mon_fdset
->id
< mon_fdset_cur
->id
) {
2347 QLIST_INSERT_BEFORE(mon_fdset_cur
, mon_fdset
, next
);
2349 QLIST_INSERT_AFTER(mon_fdset_cur
, mon_fdset
, next
);
2353 mon_fdset_fd
= g_malloc0(sizeof(*mon_fdset_fd
));
2354 mon_fdset_fd
->fd
= fd
;
2355 mon_fdset_fd
->removed
= false;
2357 mon_fdset_fd
->opaque
= g_strdup(opaque
);
2359 QLIST_INSERT_HEAD(&mon_fdset
->fds
, mon_fdset_fd
, next
);
2361 fdinfo
= g_malloc0(sizeof(*fdinfo
));
2362 fdinfo
->fdset_id
= mon_fdset
->id
;
2363 fdinfo
->fd
= mon_fdset_fd
->fd
;
2365 qemu_mutex_unlock(&mon_fdsets_lock
);
2369 int monitor_fdset_get_fd(int64_t fdset_id
, int flags
)
2374 MonFdset
*mon_fdset
;
2375 MonFdsetFd
*mon_fdset_fd
;
2379 qemu_mutex_lock(&mon_fdsets_lock
);
2380 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2381 if (mon_fdset
->id
!= fdset_id
) {
2384 QLIST_FOREACH(mon_fdset_fd
, &mon_fdset
->fds
, next
) {
2385 mon_fd_flags
= fcntl(mon_fdset_fd
->fd
, F_GETFL
);
2386 if (mon_fd_flags
== -1) {
2391 if ((flags
& O_ACCMODE
) == (mon_fd_flags
& O_ACCMODE
)) {
2392 ret
= mon_fdset_fd
->fd
;
2402 qemu_mutex_unlock(&mon_fdsets_lock
);
2407 int monitor_fdset_dup_fd_add(int64_t fdset_id
, int dup_fd
)
2409 MonFdset
*mon_fdset
;
2410 MonFdsetFd
*mon_fdset_fd_dup
;
2412 qemu_mutex_lock(&mon_fdsets_lock
);
2413 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2414 if (mon_fdset
->id
!= fdset_id
) {
2417 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2418 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2422 mon_fdset_fd_dup
= g_malloc0(sizeof(*mon_fdset_fd_dup
));
2423 mon_fdset_fd_dup
->fd
= dup_fd
;
2424 QLIST_INSERT_HEAD(&mon_fdset
->dup_fds
, mon_fdset_fd_dup
, next
);
2425 qemu_mutex_unlock(&mon_fdsets_lock
);
2430 qemu_mutex_unlock(&mon_fdsets_lock
);
2434 static int64_t monitor_fdset_dup_fd_find_remove(int dup_fd
, bool remove
)
2436 MonFdset
*mon_fdset
;
2437 MonFdsetFd
*mon_fdset_fd_dup
;
2439 qemu_mutex_lock(&mon_fdsets_lock
);
2440 QLIST_FOREACH(mon_fdset
, &mon_fdsets
, next
) {
2441 QLIST_FOREACH(mon_fdset_fd_dup
, &mon_fdset
->dup_fds
, next
) {
2442 if (mon_fdset_fd_dup
->fd
== dup_fd
) {
2444 QLIST_REMOVE(mon_fdset_fd_dup
, next
);
2445 if (QLIST_EMPTY(&mon_fdset
->dup_fds
)) {
2446 monitor_fdset_cleanup(mon_fdset
);
2450 qemu_mutex_unlock(&mon_fdsets_lock
);
2451 return mon_fdset
->id
;
2458 qemu_mutex_unlock(&mon_fdsets_lock
);
2462 int64_t monitor_fdset_dup_fd_find(int dup_fd
)
2464 return monitor_fdset_dup_fd_find_remove(dup_fd
, false);
2467 void monitor_fdset_dup_fd_remove(int dup_fd
)
2469 monitor_fdset_dup_fd_find_remove(dup_fd
, true);
2472 int monitor_fd_param(Monitor
*mon
, const char *fdname
, Error
**errp
)
2475 Error
*local_err
= NULL
;
2477 if (!qemu_isdigit(fdname
[0]) && mon
) {
2478 fd
= monitor_get_fd(mon
, fdname
, &local_err
);
2480 fd
= qemu_parse_fd(fdname
);
2482 error_setg(&local_err
, "Invalid file descriptor number '%s'",
2487 error_propagate(errp
, local_err
);
2496 /* Please update hmp-commands.hx when adding or changing commands */
2497 static HMPCommand hmp_info_cmds
[] = {
2498 #include "hmp-commands-info.h"
2502 /* hmp_cmds and hmp_info_cmds would be sorted at runtime */
2503 static HMPCommand hmp_cmds
[] = {
2504 #include "hmp-commands.h"
2508 /*******************************************************************/
2510 static const char *pch
;
2511 static sigjmp_buf expr_env
;
2514 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
2515 expr_error(Monitor
*mon
, const char *fmt
, ...)
2519 monitor_vprintf(mon
, fmt
, ap
);
2520 monitor_printf(mon
, "\n");
2522 siglongjmp(expr_env
, 1);
2525 /* return 0 if OK, -1 if not found */
2526 static int get_monitor_def(target_long
*pval
, const char *name
)
2528 const MonitorDef
*md
= target_monitor_defs();
2529 CPUState
*cs
= mon_get_cpu();
2534 if (cs
== NULL
|| md
== NULL
) {
2538 for(; md
->name
!= NULL
; md
++) {
2539 if (compare_cmd(name
, md
->name
)) {
2540 if (md
->get_value
) {
2541 *pval
= md
->get_value(md
, md
->offset
);
2543 CPUArchState
*env
= mon_get_cpu_env();
2544 ptr
= (uint8_t *)env
+ md
->offset
;
2547 *pval
= *(int32_t *)ptr
;
2550 *pval
= *(target_long
*)ptr
;
2561 ret
= target_get_monitor_def(cs
, name
, &tmp
);
2563 *pval
= (target_long
) tmp
;
2569 static void next(void)
2573 while (qemu_isspace(*pch
))
2578 static int64_t expr_sum(Monitor
*mon
);
2580 static int64_t expr_unary(Monitor
*mon
)
2589 n
= expr_unary(mon
);
2593 n
= -expr_unary(mon
);
2597 n
= ~expr_unary(mon
);
2603 expr_error(mon
, "')' expected");
2610 expr_error(mon
, "character constant expected");
2614 expr_error(mon
, "missing terminating \' character");
2624 while ((*pch
>= 'a' && *pch
<= 'z') ||
2625 (*pch
>= 'A' && *pch
<= 'Z') ||
2626 (*pch
>= '0' && *pch
<= '9') ||
2627 *pch
== '_' || *pch
== '.') {
2628 if ((q
- buf
) < sizeof(buf
) - 1)
2632 while (qemu_isspace(*pch
))
2635 ret
= get_monitor_def(®
, buf
);
2637 expr_error(mon
, "unknown register");
2642 expr_error(mon
, "unexpected end of expression");
2647 n
= strtoull(pch
, &p
, 0);
2648 if (errno
== ERANGE
) {
2649 expr_error(mon
, "number too large");
2652 expr_error(mon
, "invalid char '%c' in expression", *p
);
2655 while (qemu_isspace(*pch
))
2663 static int64_t expr_prod(Monitor
*mon
)
2668 val
= expr_unary(mon
);
2671 if (op
!= '*' && op
!= '/' && op
!= '%')
2674 val2
= expr_unary(mon
);
2683 expr_error(mon
, "division by zero");
2694 static int64_t expr_logic(Monitor
*mon
)
2699 val
= expr_prod(mon
);
2702 if (op
!= '&' && op
!= '|' && op
!= '^')
2705 val2
= expr_prod(mon
);
2722 static int64_t expr_sum(Monitor
*mon
)
2727 val
= expr_logic(mon
);
2730 if (op
!= '+' && op
!= '-')
2733 val2
= expr_logic(mon
);
2742 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2745 if (sigsetjmp(expr_env
, 0)) {
2749 while (qemu_isspace(*pch
))
2751 *pval
= expr_sum(mon
);
2756 static int get_double(Monitor
*mon
, double *pval
, const char **pp
)
2758 const char *p
= *pp
;
2762 d
= strtod(p
, &tailp
);
2764 monitor_printf(mon
, "Number expected\n");
2767 if (d
!= d
|| d
- d
!= 0) {
2768 /* NaN or infinity */
2769 monitor_printf(mon
, "Bad number\n");
2778 * Store the command-name in cmdname, and return a pointer to
2779 * the remaining of the command string.
2781 static const char *get_command_name(const char *cmdline
,
2782 char *cmdname
, size_t nlen
)
2785 const char *p
, *pstart
;
2788 while (qemu_isspace(*p
))
2793 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2798 memcpy(cmdname
, pstart
, len
);
2799 cmdname
[len
] = '\0';
2804 * Read key of 'type' into 'key' and return the current
2807 static char *key_get_info(const char *type
, char **key
)
2815 p
= strchr(type
, ':');
2822 str
= g_malloc(len
+ 1);
2823 memcpy(str
, type
, len
);
2830 static int default_fmt_format
= 'x';
2831 static int default_fmt_size
= 4;
2833 static int is_valid_option(const char *c
, const char *typestr
)
2841 typestr
= strstr(typestr
, option
);
2842 return (typestr
!= NULL
);
2845 static const HMPCommand
*search_dispatch_table(const HMPCommand
*disp_table
,
2846 const char *cmdname
)
2848 const HMPCommand
*cmd
;
2850 for (cmd
= disp_table
; cmd
->name
!= NULL
; cmd
++) {
2851 if (compare_cmd(cmdname
, cmd
->name
)) {
2860 * Parse command name from @cmdp according to command table @table.
2861 * If blank, return NULL.
2862 * Else, if no valid command can be found, report to @mon, and return
2864 * Else, change @cmdp to point right behind the name, and return its
2865 * command table entry.
2866 * Do not assume the return value points into @table! It doesn't when
2867 * the command is found in a sub-command table.
2869 static const HMPCommand
*monitor_parse_command(MonitorHMP
*hmp_mon
,
2870 const char *cmdp_start
,
2874 Monitor
*mon
= &hmp_mon
->common
;
2876 const HMPCommand
*cmd
;
2879 /* extract the command name */
2880 p
= get_command_name(*cmdp
, cmdname
, sizeof(cmdname
));
2884 cmd
= search_dispatch_table(table
, cmdname
);
2886 monitor_printf(mon
, "unknown command: '%.*s'\n",
2887 (int)(p
- cmdp_start
), cmdp_start
);
2890 if (runstate_check(RUN_STATE_PRECONFIG
) && !cmd_can_preconfig(cmd
)) {
2891 monitor_printf(mon
, "Command '%.*s' not available with -preconfig "
2892 "until after exit_preconfig.\n",
2893 (int)(p
- cmdp_start
), cmdp_start
);
2897 /* filter out following useless space */
2898 while (qemu_isspace(*p
)) {
2903 /* search sub command */
2904 if (cmd
->sub_table
!= NULL
&& *p
!= '\0') {
2905 return monitor_parse_command(hmp_mon
, cmdp_start
, cmdp
, cmd
->sub_table
);
2912 * Parse arguments for @cmd.
2913 * If it can't be parsed, report to @mon, and return NULL.
2914 * Else, insert command arguments into a QDict, and return it.
2915 * Note: On success, caller has to free the QDict structure.
2918 static QDict
*monitor_parse_arguments(Monitor
*mon
,
2920 const HMPCommand
*cmd
)
2922 const char *typestr
;
2925 const char *p
= *endp
;
2927 QDict
*qdict
= qdict_new();
2929 /* parse the parameters */
2930 typestr
= cmd
->args_type
;
2932 typestr
= key_get_info(typestr
, &key
);
2944 while (qemu_isspace(*p
))
2946 if (*typestr
== '?') {
2949 /* no optional string: NULL argument */
2953 ret
= get_str(buf
, sizeof(buf
), &p
);
2957 monitor_printf(mon
, "%s: filename expected\n",
2961 monitor_printf(mon
, "%s: block device name expected\n",
2965 monitor_printf(mon
, "%s: string expected\n", cmd
->name
);
2970 qdict_put_str(qdict
, key
, buf
);
2975 QemuOptsList
*opts_list
;
2978 opts_list
= qemu_find_opts(key
);
2979 if (!opts_list
|| opts_list
->desc
->name
) {
2982 while (qemu_isspace(*p
)) {
2987 if (get_str(buf
, sizeof(buf
), &p
) < 0) {
2990 opts
= qemu_opts_parse_noisily(opts_list
, buf
, true);
2994 qemu_opts_to_qdict(opts
, qdict
);
2995 qemu_opts_del(opts
);
3000 int count
, format
, size
;
3002 while (qemu_isspace(*p
))
3008 if (qemu_isdigit(*p
)) {
3010 while (qemu_isdigit(*p
)) {
3011 count
= count
* 10 + (*p
- '0');
3049 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3050 monitor_printf(mon
, "invalid char in format: '%c'\n",
3055 format
= default_fmt_format
;
3056 if (format
!= 'i') {
3057 /* for 'i', not specifying a size gives -1 as size */
3059 size
= default_fmt_size
;
3060 default_fmt_size
= size
;
3062 default_fmt_format
= format
;
3065 format
= default_fmt_format
;
3066 if (format
!= 'i') {
3067 size
= default_fmt_size
;
3072 qdict_put_int(qdict
, "count", count
);
3073 qdict_put_int(qdict
, "format", format
);
3074 qdict_put_int(qdict
, "size", size
);
3083 while (qemu_isspace(*p
))
3085 if (*typestr
== '?' || *typestr
== '.') {
3086 if (*typestr
== '?') {
3094 while (qemu_isspace(*p
))
3103 if (get_expr(mon
, &val
, &p
))
3105 /* Check if 'i' is greater than 32-bit */
3106 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3107 monitor_printf(mon
, "\'%s\' has failed: ", cmd
->name
);
3108 monitor_printf(mon
, "integer is for 32-bit values\n");
3110 } else if (c
== 'M') {
3112 monitor_printf(mon
, "enter a positive value\n");
3117 qdict_put_int(qdict
, key
, val
);
3126 while (qemu_isspace(*p
)) {
3129 if (*typestr
== '?') {
3135 ret
= qemu_strtosz_MiB(p
, &end
, &val
);
3136 if (ret
< 0 || val
> INT64_MAX
) {
3137 monitor_printf(mon
, "invalid size\n");
3140 qdict_put_int(qdict
, key
, val
);
3148 while (qemu_isspace(*p
))
3150 if (*typestr
== '?') {
3156 if (get_double(mon
, &val
, &p
) < 0) {
3159 if (p
[0] && p
[1] == 's') {
3162 val
/= 1e3
; p
+= 2; break;
3164 val
/= 1e6
; p
+= 2; break;
3166 val
/= 1e9
; p
+= 2; break;
3169 if (*p
&& !qemu_isspace(*p
)) {
3170 monitor_printf(mon
, "Unknown unit suffix\n");
3173 qdict_put(qdict
, key
, qnum_from_double(val
));
3181 while (qemu_isspace(*p
)) {
3185 while (qemu_isgraph(*p
)) {
3188 if (p
- beg
== 2 && !memcmp(beg
, "on", p
- beg
)) {
3190 } else if (p
- beg
== 3 && !memcmp(beg
, "off", p
- beg
)) {
3193 monitor_printf(mon
, "Expected 'on' or 'off'\n");
3196 qdict_put_bool(qdict
, key
, val
);
3201 const char *tmp
= p
;
3208 while (qemu_isspace(*p
))
3213 if(!is_valid_option(p
, typestr
)) {
3215 monitor_printf(mon
, "%s: unsupported option -%c\n",
3227 qdict_put_bool(qdict
, key
, true);
3234 /* package all remaining string */
3237 while (qemu_isspace(*p
)) {
3240 if (*typestr
== '?') {
3243 /* no remaining string: NULL argument */
3249 monitor_printf(mon
, "%s: string expected\n",
3253 qdict_put_str(qdict
, key
, p
);
3259 monitor_printf(mon
, "%s: unknown type '%c'\n", cmd
->name
, c
);
3265 /* check that all arguments were parsed */
3266 while (qemu_isspace(*p
))
3269 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3277 qobject_unref(qdict
);
3282 static void handle_hmp_command(MonitorHMP
*mon
, const char *cmdline
)
3285 const HMPCommand
*cmd
;
3286 const char *cmd_start
= cmdline
;
3288 trace_handle_hmp_command(mon
, cmdline
);
3290 cmd
= monitor_parse_command(mon
, cmdline
, &cmdline
, hmp_cmds
);
3295 qdict
= monitor_parse_arguments(&mon
->common
, &cmdline
, cmd
);
3297 while (cmdline
> cmd_start
&& qemu_isspace(cmdline
[-1])) {
3300 monitor_printf(&mon
->common
, "Try \"help %.*s\" for more information\n",
3301 (int)(cmdline
- cmd_start
), cmd_start
);
3305 cmd
->cmd(&mon
->common
, qdict
);
3306 qobject_unref(qdict
);
3309 static void cmd_completion(MonitorHMP
*mon
, const char *name
, const char *list
)
3311 const char *p
, *pstart
;
3318 p
= qemu_strchrnul(p
, '|');
3320 if (len
> sizeof(cmd
) - 2)
3321 len
= sizeof(cmd
) - 2;
3322 memcpy(cmd
, pstart
, len
);
3324 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3325 readline_add_completion(mon
->rs
, cmd
);
3333 static void file_completion(MonitorHMP
*mon
, const char *input
)
3338 char file
[1024], file_prefix
[1024];
3342 p
= strrchr(input
, '/');
3345 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3346 pstrcpy(path
, sizeof(path
), ".");
3348 input_path_len
= p
- input
+ 1;
3349 memcpy(path
, input
, input_path_len
);
3350 if (input_path_len
> sizeof(path
) - 1)
3351 input_path_len
= sizeof(path
) - 1;
3352 path
[input_path_len
] = '\0';
3353 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3356 ffs
= opendir(path
);
3365 if (strcmp(d
->d_name
, ".") == 0 || strcmp(d
->d_name
, "..") == 0) {
3369 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3370 memcpy(file
, input
, input_path_len
);
3371 if (input_path_len
< sizeof(file
))
3372 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3374 /* stat the file to find out if it's a directory.
3375 * In that case add a slash to speed up typing long paths
3377 if (stat(file
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) {
3378 pstrcat(file
, sizeof(file
), "/");
3380 readline_add_completion(mon
->rs
, file
);
3386 static const char *next_arg_type(const char *typestr
)
3388 const char *p
= strchr(typestr
, ':');
3389 return (p
!= NULL
? ++p
: typestr
);
3392 static void add_completion_option(ReadLineState
*rs
, const char *str
,
3395 if (!str
|| !option
) {
3398 if (!strncmp(option
, str
, strlen(str
))) {
3399 readline_add_completion(rs
, option
);
3403 void chardev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3406 ChardevBackendInfoList
*list
, *start
;
3412 readline_set_completion_index(rs
, len
);
3414 start
= list
= qmp_query_chardev_backends(NULL
);
3416 const char *chr_name
= list
->value
->name
;
3418 if (!strncmp(chr_name
, str
, len
)) {
3419 readline_add_completion(rs
, chr_name
);
3423 qapi_free_ChardevBackendInfoList(start
);
3426 void netdev_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3435 readline_set_completion_index(rs
, len
);
3436 for (i
= 0; i
< NET_CLIENT_DRIVER__MAX
; i
++) {
3437 add_completion_option(rs
, str
, NetClientDriver_str(i
));
3441 void device_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3451 readline_set_completion_index(rs
, len
);
3452 list
= elt
= object_class_get_list(TYPE_DEVICE
, false);
3455 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
3457 name
= object_class_get_name(OBJECT_CLASS(dc
));
3459 if (dc
->user_creatable
3460 && !strncmp(name
, str
, len
)) {
3461 readline_add_completion(rs
, name
);
3468 void object_add_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3478 readline_set_completion_index(rs
, len
);
3479 list
= elt
= object_class_get_list(TYPE_USER_CREATABLE
, false);
3483 name
= object_class_get_name(OBJECT_CLASS(elt
->data
));
3484 if (!strncmp(name
, str
, len
) && strcmp(name
, TYPE_USER_CREATABLE
)) {
3485 readline_add_completion(rs
, name
);
3492 static void peripheral_device_del_completion(ReadLineState
*rs
,
3493 const char *str
, size_t len
)
3495 Object
*peripheral
= container_get(qdev_get_machine(), "/peripheral");
3496 GSList
*list
, *item
;
3498 list
= qdev_build_hotpluggable_device_list(peripheral
);
3503 for (item
= list
; item
; item
= g_slist_next(item
)) {
3504 DeviceState
*dev
= item
->data
;
3506 if (dev
->id
&& !strncmp(str
, dev
->id
, len
)) {
3507 readline_add_completion(rs
, dev
->id
);
3514 void chardev_remove_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3517 ChardevInfoList
*list
, *start
;
3523 readline_set_completion_index(rs
, len
);
3525 start
= list
= qmp_query_chardev(NULL
);
3527 ChardevInfo
*chr
= list
->value
;
3529 if (!strncmp(chr
->label
, str
, len
)) {
3530 readline_add_completion(rs
, chr
->label
);
3534 qapi_free_ChardevInfoList(start
);
3537 static void ringbuf_completion(ReadLineState
*rs
, const char *str
)
3540 ChardevInfoList
*list
, *start
;
3543 readline_set_completion_index(rs
, len
);
3545 start
= list
= qmp_query_chardev(NULL
);
3547 ChardevInfo
*chr_info
= list
->value
;
3549 if (!strncmp(chr_info
->label
, str
, len
)) {
3550 Chardev
*chr
= qemu_chr_find(chr_info
->label
);
3551 if (chr
&& CHARDEV_IS_RINGBUF(chr
)) {
3552 readline_add_completion(rs
, chr_info
->label
);
3557 qapi_free_ChardevInfoList(start
);
3560 void ringbuf_write_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3565 ringbuf_completion(rs
, str
);
3568 void device_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3577 readline_set_completion_index(rs
, len
);
3578 peripheral_device_del_completion(rs
, str
, len
);
3581 void object_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3583 ObjectPropertyInfoList
*list
, *start
;
3590 readline_set_completion_index(rs
, len
);
3592 start
= list
= qmp_qom_list("/objects", NULL
);
3594 ObjectPropertyInfo
*info
= list
->value
;
3596 if (!strncmp(info
->type
, "child<", 5)
3597 && !strncmp(info
->name
, str
, len
)) {
3598 readline_add_completion(rs
, info
->name
);
3602 qapi_free_ObjectPropertyInfoList(start
);
3605 void sendkey_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3614 sep
= strrchr(str
, '-');
3619 readline_set_completion_index(rs
, len
);
3620 for (i
= 0; i
< Q_KEY_CODE__MAX
; i
++) {
3621 if (!strncmp(str
, QKeyCode_str(i
), len
)) {
3622 readline_add_completion(rs
, QKeyCode_str(i
));
3627 void set_link_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3632 readline_set_completion_index(rs
, len
);
3634 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3636 count
= qemu_find_net_clients_except(NULL
, ncs
,
3637 NET_CLIENT_DRIVER_NONE
,
3639 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3640 const char *name
= ncs
[i
]->name
;
3641 if (!strncmp(str
, name
, len
)) {
3642 readline_add_completion(rs
, name
);
3645 } else if (nb_args
== 3) {
3646 add_completion_option(rs
, str
, "on");
3647 add_completion_option(rs
, str
, "off");
3651 void netdev_del_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3654 NetClientState
*ncs
[MAX_QUEUE_NUM
];
3661 readline_set_completion_index(rs
, len
);
3662 count
= qemu_find_net_clients_except(NULL
, ncs
, NET_CLIENT_DRIVER_NIC
,
3664 for (i
= 0; i
< MIN(count
, MAX_QUEUE_NUM
); i
++) {
3666 const char *name
= ncs
[i
]->name
;
3667 if (strncmp(str
, name
, len
)) {
3670 opts
= qemu_opts_find(qemu_find_opts_err("netdev", NULL
), name
);
3672 readline_add_completion(rs
, name
);
3677 void info_trace_events_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3682 readline_set_completion_index(rs
, len
);
3684 TraceEventIter iter
;
3686 char *pattern
= g_strdup_printf("%s*", str
);
3687 trace_event_iter_init(&iter
, pattern
);
3688 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3689 readline_add_completion(rs
, trace_event_get_name(ev
));
3695 void trace_event_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3700 readline_set_completion_index(rs
, len
);
3702 TraceEventIter iter
;
3704 char *pattern
= g_strdup_printf("%s*", str
);
3705 trace_event_iter_init(&iter
, pattern
);
3706 while ((ev
= trace_event_iter_next(&iter
)) != NULL
) {
3707 readline_add_completion(rs
, trace_event_get_name(ev
));
3710 } else if (nb_args
== 3) {
3711 add_completion_option(rs
, str
, "on");
3712 add_completion_option(rs
, str
, "off");
3716 void watchdog_action_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3723 readline_set_completion_index(rs
, strlen(str
));
3724 for (i
= 0; i
< WATCHDOG_ACTION__MAX
; i
++) {
3725 add_completion_option(rs
, str
, WatchdogAction_str(i
));
3729 void migrate_set_capability_completion(ReadLineState
*rs
, int nb_args
,
3735 readline_set_completion_index(rs
, len
);
3738 for (i
= 0; i
< MIGRATION_CAPABILITY__MAX
; i
++) {
3739 const char *name
= MigrationCapability_str(i
);
3740 if (!strncmp(str
, name
, len
)) {
3741 readline_add_completion(rs
, name
);
3744 } else if (nb_args
== 3) {
3745 add_completion_option(rs
, str
, "on");
3746 add_completion_option(rs
, str
, "off");
3750 void migrate_set_parameter_completion(ReadLineState
*rs
, int nb_args
,
3756 readline_set_completion_index(rs
, len
);
3759 for (i
= 0; i
< MIGRATION_PARAMETER__MAX
; i
++) {
3760 const char *name
= MigrationParameter_str(i
);
3761 if (!strncmp(str
, name
, len
)) {
3762 readline_add_completion(rs
, name
);
3768 static void vm_completion(ReadLineState
*rs
, const char *str
)
3771 BlockDriverState
*bs
;
3772 BdrvNextIterator it
;
3775 readline_set_completion_index(rs
, len
);
3777 for (bs
= bdrv_first(&it
); bs
; bs
= bdrv_next(&it
)) {
3778 SnapshotInfoList
*snapshots
, *snapshot
;
3779 AioContext
*ctx
= bdrv_get_aio_context(bs
);
3782 aio_context_acquire(ctx
);
3783 if (bdrv_can_snapshot(bs
)) {
3784 ok
= bdrv_query_snapshot_info_list(bs
, &snapshots
, NULL
) == 0;
3786 aio_context_release(ctx
);
3791 snapshot
= snapshots
;
3793 char *completion
= snapshot
->value
->name
;
3794 if (!strncmp(str
, completion
, len
)) {
3795 readline_add_completion(rs
, completion
);
3797 completion
= snapshot
->value
->id
;
3798 if (!strncmp(str
, completion
, len
)) {
3799 readline_add_completion(rs
, completion
);
3801 snapshot
= snapshot
->next
;
3803 qapi_free_SnapshotInfoList(snapshots
);
3808 void delvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3811 vm_completion(rs
, str
);
3815 void loadvm_completion(ReadLineState
*rs
, int nb_args
, const char *str
)
3818 vm_completion(rs
, str
);
3822 static void monitor_find_completion_by_table(MonitorHMP
*mon
,
3823 const HMPCommand
*cmd_table
,
3827 const char *cmdname
;
3829 const char *ptype
, *old_ptype
, *str
, *name
;
3830 const HMPCommand
*cmd
;
3831 BlockBackend
*blk
= NULL
;
3834 /* command completion */
3839 readline_set_completion_index(mon
->rs
, strlen(cmdname
));
3840 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3841 if (!runstate_check(RUN_STATE_PRECONFIG
) ||
3842 cmd_can_preconfig(cmd
)) {
3843 cmd_completion(mon
, cmdname
, cmd
->name
);
3847 /* find the command */
3848 for (cmd
= cmd_table
; cmd
->name
!= NULL
; cmd
++) {
3849 if (compare_cmd(args
[0], cmd
->name
) &&
3850 (!runstate_check(RUN_STATE_PRECONFIG
) ||
3851 cmd_can_preconfig(cmd
))) {
3859 if (cmd
->sub_table
) {
3860 /* do the job again */
3861 monitor_find_completion_by_table(mon
, cmd
->sub_table
,
3862 &args
[1], nb_args
- 1);
3865 if (cmd
->command_completion
) {
3866 cmd
->command_completion(mon
->rs
, nb_args
, args
[nb_args
- 1]);
3870 ptype
= next_arg_type(cmd
->args_type
);
3871 for(i
= 0; i
< nb_args
- 2; i
++) {
3872 if (*ptype
!= '\0') {
3873 ptype
= next_arg_type(ptype
);
3874 while (*ptype
== '?')
3875 ptype
= next_arg_type(ptype
);
3878 str
= args
[nb_args
- 1];
3880 while (*ptype
== '-' && old_ptype
!= ptype
) {
3882 ptype
= next_arg_type(ptype
);
3886 /* file completion */
3887 readline_set_completion_index(mon
->rs
, strlen(str
));
3888 file_completion(mon
, str
);
3891 /* block device name completion */
3892 readline_set_completion_index(mon
->rs
, strlen(str
));
3893 while ((blk
= blk_next(blk
)) != NULL
) {
3894 name
= blk_name(blk
);
3895 if (str
[0] == '\0' ||
3896 !strncmp(name
, str
, strlen(str
))) {
3897 readline_add_completion(mon
->rs
, name
);
3903 if (!strcmp(cmd
->name
, "help|?")) {
3904 monitor_find_completion_by_table(mon
, cmd_table
,
3905 &args
[1], nb_args
- 1);
3914 static void monitor_find_completion(void *opaque
,
3915 const char *cmdline
)
3917 MonitorHMP
*mon
= opaque
;
3918 char *args
[MAX_ARGS
];
3921 /* 1. parse the cmdline */
3922 if (parse_cmdline(cmdline
, &nb_args
, args
) < 0) {
3926 /* if the line ends with a space, it means we want to complete the
3928 len
= strlen(cmdline
);
3929 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3930 if (nb_args
>= MAX_ARGS
) {
3933 args
[nb_args
++] = g_strdup("");
3936 /* 2. auto complete according to args */
3937 monitor_find_completion_by_table(mon
, hmp_cmds
, args
, nb_args
);
3940 free_cmdline_args(args
, nb_args
);
3943 int monitor_can_read(void *opaque
)
3945 Monitor
*mon
= opaque
;
3947 return !atomic_mb_read(&mon
->suspend_cnt
);
3950 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3953 Monitor
*old_mon
= cur_mon
;
3957 mon
= container_of(cur_mon
, MonitorHMP
, common
);
3960 for (i
= 0; i
< size
; i
++)
3961 readline_handle_byte(mon
->rs
, buf
[i
]);
3963 if (size
== 0 || buf
[size
- 1] != 0)
3964 monitor_printf(cur_mon
, "corrupted command\n");
3966 handle_hmp_command(mon
, (char *)buf
);
3972 static void monitor_command_cb(void *opaque
, const char *cmdline
,
3973 void *readline_opaque
)
3975 MonitorHMP
*mon
= opaque
;
3977 monitor_suspend(&mon
->common
);
3978 handle_hmp_command(mon
, cmdline
);
3979 monitor_resume(&mon
->common
);
3982 int monitor_suspend(Monitor
*mon
)
3984 if (monitor_is_hmp_non_interactive(mon
)) {
3988 atomic_inc(&mon
->suspend_cnt
);
3990 if (mon
->use_io_thread
) {
3992 * Kick I/O thread to make sure this takes effect. It'll be
3993 * evaluated again in prepare() of the watch object.
3995 aio_notify(iothread_get_aio_context(mon_iothread
));
3998 trace_monitor_suspend(mon
, 1);
4002 static void monitor_accept_input(void *opaque
)
4004 Monitor
*mon
= opaque
;
4006 qemu_chr_fe_accept_input(&mon
->chr
);
4009 void monitor_resume(Monitor
*mon
)
4011 if (monitor_is_hmp_non_interactive(mon
)) {
4015 if (atomic_dec_fetch(&mon
->suspend_cnt
) == 0) {
4018 if (mon
->use_io_thread
) {
4019 ctx
= iothread_get_aio_context(mon_iothread
);
4021 ctx
= qemu_get_aio_context();
4024 if (!monitor_is_qmp(mon
)) {
4025 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
4026 assert(hmp_mon
->rs
);
4027 readline_show_prompt(hmp_mon
->rs
);
4030 aio_bh_schedule_oneshot(ctx
, monitor_accept_input
, mon
);
4033 trace_monitor_suspend(mon
, -1);
4036 static void monitor_event(void *opaque
, int event
)
4038 Monitor
*mon
= opaque
;
4039 MonitorHMP
*hmp_mon
= container_of(mon
, MonitorHMP
, common
);
4042 case CHR_EVENT_MUX_IN
:
4043 qemu_mutex_lock(&mon
->mon_lock
);
4045 qemu_mutex_unlock(&mon
->mon_lock
);
4046 if (mon
->reset_seen
) {
4047 readline_restart(hmp_mon
->rs
);
4048 monitor_resume(mon
);
4051 atomic_mb_set(&mon
->suspend_cnt
, 0);
4055 case CHR_EVENT_MUX_OUT
:
4056 if (mon
->reset_seen
) {
4057 if (atomic_mb_read(&mon
->suspend_cnt
) == 0) {
4058 monitor_printf(mon
, "\n");
4061 monitor_suspend(mon
);
4063 atomic_inc(&mon
->suspend_cnt
);
4065 qemu_mutex_lock(&mon
->mon_lock
);
4067 qemu_mutex_unlock(&mon
->mon_lock
);
4070 case CHR_EVENT_OPENED
:
4071 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
4072 "information\n", QEMU_VERSION
);
4073 if (!mon
->mux_out
) {
4074 readline_restart(hmp_mon
->rs
);
4075 readline_show_prompt(hmp_mon
->rs
);
4077 mon
->reset_seen
= 1;
4081 case CHR_EVENT_CLOSED
:
4083 monitor_fdsets_cleanup();
4089 compare_mon_cmd(const void *a
, const void *b
)
4091 return strcmp(((const HMPCommand
*)a
)->name
,
4092 ((const HMPCommand
*)b
)->name
);
4095 static void sortcmdlist(void)
4097 qsort(hmp_cmds
, ARRAY_SIZE(hmp_cmds
) - 1,
4100 qsort(hmp_info_cmds
, ARRAY_SIZE(hmp_info_cmds
) - 1,
4101 sizeof(*hmp_info_cmds
),
4105 static void monitor_iothread_init(void)
4107 mon_iothread
= iothread_create("mon_iothread", &error_abort
);
4110 void monitor_init_globals(void)
4112 monitor_init_qmp_commands();
4113 monitor_qapi_event_init();
4115 qemu_mutex_init(&monitor_lock
);
4116 qemu_mutex_init(&mon_fdsets_lock
);
4119 * The dispatcher BH must run in the main loop thread, since we
4120 * have commands assuming that context. It would be nice to get
4121 * rid of those assumptions.
4123 qmp_dispatcher_bh
= aio_bh_new(iohandler_get_aio_context(),
4124 monitor_qmp_bh_dispatcher
,
4128 /* These functions just adapt the readline interface in a typesafe way. We
4129 * could cast function pointers but that discards compiler checks.
4131 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque
,
4132 const char *fmt
, ...)
4134 MonitorHMP
*mon
= opaque
;
4137 monitor_vprintf(&mon
->common
, fmt
, ap
);
4141 static void monitor_readline_flush(void *opaque
)
4143 MonitorHMP
*mon
= opaque
;
4144 monitor_flush(&mon
->common
);
4148 * Print to current monitor if we have one, else to stderr.
4150 int error_vprintf(const char *fmt
, va_list ap
)
4152 if (cur_mon
&& !monitor_cur_is_qmp()) {
4153 return monitor_vprintf(cur_mon
, fmt
, ap
);
4155 return vfprintf(stderr
, fmt
, ap
);
4158 int error_vprintf_unless_qmp(const char *fmt
, va_list ap
)
4161 return vfprintf(stderr
, fmt
, ap
);
4163 if (!monitor_cur_is_qmp()) {
4164 return monitor_vprintf(cur_mon
, fmt
, ap
);
4169 void monitor_list_append(Monitor
*mon
)
4171 qemu_mutex_lock(&monitor_lock
);
4173 * This prevents inserting new monitors during monitor_cleanup().
4174 * A cleaner solution would involve the main thread telling other
4175 * threads to terminate, waiting for their termination.
4177 if (!monitor_destroyed
) {
4178 QTAILQ_INSERT_HEAD(&mon_list
, mon
, entry
);
4181 qemu_mutex_unlock(&monitor_lock
);
4184 monitor_data_destroy(mon
);
4189 static void monitor_init_hmp(Chardev
*chr
, int flags
)
4191 MonitorHMP
*mon
= g_new0(MonitorHMP
, 1);
4192 bool use_readline
= flags
& MONITOR_USE_READLINE
;
4194 monitor_data_init(&mon
->common
, flags
, false, false);
4195 qemu_chr_fe_init(&mon
->common
.chr
, chr
, &error_abort
);
4198 mon
->rs
= readline_init(monitor_readline_printf
,
4199 monitor_readline_flush
,
4201 monitor_find_completion
);
4202 monitor_read_command(mon
, 0);
4205 qemu_chr_fe_set_handlers(&mon
->common
.chr
, monitor_can_read
, monitor_read
,
4206 monitor_event
, NULL
, &mon
->common
, NULL
, true);
4207 monitor_list_append(&mon
->common
);
4210 void monitor_init(Chardev
*chr
, int flags
)
4212 if (flags
& MONITOR_USE_CONTROL
) {
4213 monitor_init_qmp(chr
, flags
);
4215 monitor_init_hmp(chr
, flags
);
4219 void monitor_cleanup(void)
4222 * We need to explicitly stop the I/O thread (but not destroy it),
4223 * clean up the monitor resources, then destroy the I/O thread since
4224 * we need to unregister from chardev below in
4225 * monitor_data_destroy(), and chardev is not thread-safe yet
4228 iothread_stop(mon_iothread
);
4231 /* Flush output buffers and destroy monitors */
4232 qemu_mutex_lock(&monitor_lock
);
4233 monitor_destroyed
= true;
4234 while (!QTAILQ_EMPTY(&mon_list
)) {
4235 Monitor
*mon
= QTAILQ_FIRST(&mon_list
);
4236 QTAILQ_REMOVE(&mon_list
, mon
, entry
);
4237 /* Permit QAPI event emission from character frontend release */
4238 qemu_mutex_unlock(&monitor_lock
);
4240 monitor_data_destroy(mon
);
4241 qemu_mutex_lock(&monitor_lock
);
4244 qemu_mutex_unlock(&monitor_lock
);
4246 /* QEMUBHs needs to be deleted before destroying the I/O thread */
4247 qemu_bh_delete(qmp_dispatcher_bh
);
4248 qmp_dispatcher_bh
= NULL
;
4250 iothread_destroy(mon_iothread
);
4251 mon_iothread
= NULL
;
4255 QemuOptsList qemu_mon_opts
= {
4257 .implied_opt_name
= "chardev",
4258 .head
= QTAILQ_HEAD_INITIALIZER(qemu_mon_opts
.head
),
4262 .type
= QEMU_OPT_STRING
,
4265 .type
= QEMU_OPT_STRING
,
4268 .type
= QEMU_OPT_BOOL
,
4270 { /* end of list */ }
4274 HotpluggableCPUList
*qmp_query_hotpluggable_cpus(Error
**errp
)
4276 MachineState
*ms
= MACHINE(qdev_get_machine());
4277 MachineClass
*mc
= MACHINE_GET_CLASS(ms
);
4279 if (!mc
->has_hotpluggable_cpus
) {
4280 error_setg(errp
, QERR_FEATURE_DISABLED
, "query-hotpluggable-cpus");
4284 return machine_query_hotpluggable_cpus(ms
);