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
26 #include "monitor/qdev.h"
28 #include "hw/i386/pc.h"
29 #include "hw/pci/pci.h"
30 #include "sysemu/watchdog.h"
31 #include "hw/loader.h"
32 #include "exec/gdbstub.h"
34 #include "net/slirp.h"
35 #include "sysemu/char.h"
36 #include "ui/qemu-spice.h"
37 #include "sysemu/sysemu.h"
38 #include "sysemu/numa.h"
39 #include "monitor/monitor.h"
40 #include "qemu/readline.h"
41 #include "ui/console.h"
43 #include "sysemu/blockdev.h"
44 #include "audio/audio.h"
45 #include "disas/disas.h"
46 #include "sysemu/balloon.h"
47 #include "qemu/timer.h"
48 #include "migration/migration.h"
49 #include "sysemu/kvm.h"
51 #include "sysemu/tpm.h"
52 #include "qapi/qmp/qint.h"
53 #include "qapi/qmp/qfloat.h"
54 #include "qapi/qmp/qlist.h"
55 #include "qapi/qmp/qbool.h"
56 #include "qapi/qmp/qstring.h"
57 #include "qapi/qmp/qjson.h"
58 #include "qapi/qmp/json-streamer.h"
59 #include "qapi/qmp/json-parser.h"
60 #include <qom/object_interfaces.h>
61 #include "qemu/osdep.h"
64 #include "trace/control.h"
65 #ifdef CONFIG_TRACE_SIMPLE
66 #include "trace/simple.h"
68 #include "exec/memory.h"
69 #include "exec/cpu_ldst.h"
70 #include "qmp-commands.h"
72 #include "qemu/thread.h"
73 #include "block/qapi.h"
74 #include "qapi/qmp-event.h"
75 #include "qapi-event.h"
76 #include "sysemu/block-backend.h"
78 /* for hmp_info_irq/pic */
79 #if defined(TARGET_SPARC)
80 #include "hw/sparc/sun4m.h"
82 #include "hw/lm32/lm32_pic.h"
88 * 'B' block device name
89 * 's' string (accept optional quote)
90 * 'S' it just appends the rest of the string (accept optional quote)
91 * 'O' option string of the form NAME=VALUE,...
92 * parsed according to QemuOptsList given by its name
93 * Example: 'device:O' uses qemu_device_opts.
94 * Restriction: only lists with empty desc are supported
95 * TODO lift the restriction
97 * 'l' target long (32 or 64 bit)
98 * 'M' Non-negative target long (32 or 64 bit), in user mode the
99 * value is multiplied by 2^20 (think Mebibyte)
100 * 'o' octets (aka bytes)
101 * user mode accepts an optional E, e, P, p, T, t, G, g, M, m,
102 * K, k suffix, which multiplies the value by 2^60 for suffixes E
103 * and e, 2^50 for suffixes P and p, 2^40 for suffixes T and t,
104 * 2^30 for suffixes G and g, 2^20 for M and m, 2^10 for K and k
106 * user mode accepts an optional ms, us, ns suffix,
107 * which divides the value by 1e3, 1e6, 1e9, respectively
108 * '/' optional gdb-like print format (like "/10x")
110 * '?' optional type (for all types, except '/')
111 * '.' other form of optional type (for 'i' and 'l')
113 * user mode accepts "on" or "off"
114 * '-' optional parameter (eg. '-f')
118 typedef struct mon_cmd_t {
120 const char *args_type;
124 void (*cmd)(Monitor *mon, const QDict *qdict);
125 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
127 /* @sub_table is a list of 2nd level of commands. If it do not exist,
128 * mhandler should be used. If it exist, sub_table[?].mhandler should be
129 * used, and mhandler of 1st level plays the role of help function.
131 struct mon_cmd_t *sub_table;
132 void (*command_completion)(ReadLineState *rs, int nb_args, const char *str);
135 /* file descriptors passed via SCM_RIGHTS */
136 typedef struct mon_fd_t mon_fd_t;
140 QLIST_ENTRY(mon_fd_t) next;
143 /* file descriptor associated with a file descriptor set */
144 typedef struct MonFdsetFd MonFdsetFd;
149 QLIST_ENTRY(MonFdsetFd) next;
152 /* file descriptor set containing fds passed via SCM_RIGHTS */
153 typedef struct MonFdset MonFdset;
156 QLIST_HEAD(, MonFdsetFd) fds;
157 QLIST_HEAD(, MonFdsetFd) dup_fds;
158 QLIST_ENTRY(MonFdset) next;
163 JSONMessageParser parser;
165 * When a client connects, we're in capabilities negotiation mode.
166 * When command qmp_capabilities succeeds, we go into command
169 bool in_command_mode; /* are we in command mode? */
173 * To prevent flooding clients, events can be throttled. The
174 * throttling is calculated globally, rather than per-Monitor
177 typedef struct MonitorQAPIEventState {
178 QAPIEvent event; /* Event being tracked */
179 int64_t rate; /* Minimum time (in ns) between two events */
180 int64_t last; /* QEMU_CLOCK_REALTIME value at last emission */
181 QEMUTimer *timer; /* Timer for handling delayed events */
182 QObject *data; /* Event pending delayed dispatch */
183 } MonitorQAPIEventState;
186 CharDriverState *chr;
196 /* Read under either BQL or out_lock, written with BQL+out_lock. */
202 BlockCompletionFunc *password_completion_cb;
203 void *password_opaque;
204 mon_cmd_t *cmd_table;
206 QLIST_HEAD(,mon_fd_t) fds;
207 QLIST_ENTRY(Monitor) entry;
210 /* QMP checker flags */
211 #define QMP_ACCEPT_UNKNOWNS 1
213 /* Protects mon_list, monitor_event_state. */
214 static QemuMutex monitor_lock;
216 static QLIST_HEAD(mon_list, Monitor) mon_list;
217 static QLIST_HEAD(mon_fdsets, MonFdset) mon_fdsets;
218 static int mon_refcount;
220 static mon_cmd_t mon_cmds[];
221 static mon_cmd_t info_cmds[];
223 static const mon_cmd_t qmp_cmds[];
227 static void monitor_command_cb(void *opaque, const char *cmdline,
228 void *readline_opaque);
231 * Is @mon a QMP monitor?
233 static inline bool monitor_is_qmp(const Monitor *mon)
235 return (mon->flags & MONITOR_USE_CONTROL);
239 * Is the current monitor, if any, a QMP monitor?
241 bool monitor_cur_is_qmp(void)
243 return cur_mon && monitor_is_qmp(cur_mon);
246 void monitor_read_command(Monitor *mon, int show_prompt)
251 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
253 readline_show_prompt(mon->rs);
256 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
260 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
261 /* prompt is printed on return from the command handler */
264 monitor_printf(mon, "terminal does not support password prompting\n");
269 static void monitor_flush_locked(Monitor *mon);
271 static gboolean monitor_unblocked(GIOChannel *chan, GIOCondition cond,
274 Monitor *mon = opaque;
276 qemu_mutex_lock(&mon->out_lock);
278 monitor_flush_locked(mon);
279 qemu_mutex_unlock(&mon->out_lock);
283 /* Called with mon->out_lock held. */
284 static void monitor_flush_locked(Monitor *mon)
290 if (mon->skip_flush) {
294 buf = qstring_get_str(mon->outbuf);
295 len = qstring_get_length(mon->outbuf);
297 if (len && !mon->mux_out) {
298 rc = qemu_chr_fe_write(mon->chr, (const uint8_t *) buf, len);
299 if ((rc < 0 && errno != EAGAIN) || (rc == len)) {
300 /* all flushed or error */
301 QDECREF(mon->outbuf);
302 mon->outbuf = qstring_new();
307 QString *tmp = qstring_from_str(buf + rc);
308 QDECREF(mon->outbuf);
311 if (mon->out_watch == 0) {
312 mon->out_watch = qemu_chr_fe_add_watch(mon->chr, G_IO_OUT|G_IO_HUP,
313 monitor_unblocked, mon);
318 void monitor_flush(Monitor *mon)
320 qemu_mutex_lock(&mon->out_lock);
321 monitor_flush_locked(mon);
322 qemu_mutex_unlock(&mon->out_lock);
325 /* flush at every end of line */
326 static void monitor_puts(Monitor *mon, const char *str)
330 qemu_mutex_lock(&mon->out_lock);
336 qstring_append_chr(mon->outbuf, '\r');
338 qstring_append_chr(mon->outbuf, c);
340 monitor_flush_locked(mon);
343 qemu_mutex_unlock(&mon->out_lock);
346 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
353 if (monitor_is_qmp(mon)) {
357 buf = g_strdup_vprintf(fmt, ap);
358 monitor_puts(mon, buf);
362 void monitor_printf(Monitor *mon, const char *fmt, ...)
366 monitor_vprintf(mon, fmt, ap);
370 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
371 const char *fmt, ...)
375 monitor_vprintf((Monitor *)stream, fmt, ap);
380 static void monitor_json_emitter(Monitor *mon, const QObject *data)
384 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
385 qobject_to_json(data);
386 assert(json != NULL);
388 qstring_append_chr(json, '\n');
389 monitor_puts(mon, qstring_get_str(json));
394 static QDict *build_qmp_error_dict(Error *err)
398 obj = qobject_from_jsonf("{ 'error': { 'class': %s, 'desc': %s } }",
399 ErrorClass_lookup[error_get_class(err)],
400 error_get_pretty(err));
402 return qobject_to_qdict(obj);
405 static void monitor_protocol_emitter(Monitor *mon, QObject *data,
410 trace_monitor_protocol_emitter(mon);
413 /* success response */
416 qobject_incref(data);
417 qdict_put_obj(qmp, "return", data);
419 /* return an empty QDict by default */
420 qdict_put(qmp, "return", qdict_new());
424 qmp = build_qmp_error_dict(err);
428 qdict_put_obj(qmp, "id", mon->qmp.id);
432 monitor_json_emitter(mon, QOBJECT(qmp));
437 static MonitorQAPIEventState monitor_qapi_event_state[QAPI_EVENT_MAX];
440 * Emits the event to every monitor instance, @event is only used for trace
441 * Called with monitor_lock held.
443 static void monitor_qapi_event_emit(QAPIEvent event, QObject *data)
447 trace_monitor_protocol_event_emit(event, data);
448 QLIST_FOREACH(mon, &mon_list, entry) {
449 if (monitor_is_qmp(mon) && mon->qmp.in_command_mode) {
450 monitor_json_emitter(mon, data);
456 * Queue a new event for emission to Monitor instances,
457 * applying any rate limiting if required.
460 monitor_qapi_event_queue(QAPIEvent event, QDict *data, Error **errp)
462 MonitorQAPIEventState *evstate;
463 assert(event < QAPI_EVENT_MAX);
464 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
466 evstate = &(monitor_qapi_event_state[event]);
467 trace_monitor_protocol_event_queue(event,
473 /* Rate limit of 0 indicates no throttling */
474 qemu_mutex_lock(&monitor_lock);
475 if (!evstate->rate) {
476 monitor_qapi_event_emit(event, QOBJECT(data));
479 int64_t delta = now - evstate->last;
481 delta < evstate->rate) {
482 /* If there's an existing event pending, replace
483 * it with the new event, otherwise schedule a
484 * timer for delayed emission
487 qobject_decref(evstate->data);
489 int64_t then = evstate->last + evstate->rate;
490 timer_mod_ns(evstate->timer, then);
492 evstate->data = QOBJECT(data);
493 qobject_incref(evstate->data);
495 monitor_qapi_event_emit(event, QOBJECT(data));
499 qemu_mutex_unlock(&monitor_lock);
503 * The callback invoked by QemuTimer when a delayed
504 * event is ready to be emitted
506 static void monitor_qapi_event_handler(void *opaque)
508 MonitorQAPIEventState *evstate = opaque;
509 int64_t now = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
511 trace_monitor_protocol_event_handler(evstate->event,
515 qemu_mutex_lock(&monitor_lock);
517 monitor_qapi_event_emit(evstate->event, evstate->data);
518 qobject_decref(evstate->data);
519 evstate->data = NULL;
522 qemu_mutex_unlock(&monitor_lock);
526 * @event: the event ID to be limited
527 * @rate: the rate limit in milliseconds
529 * Sets a rate limit on a particular event, so no
530 * more than 1 event will be emitted within @rate
534 monitor_qapi_event_throttle(QAPIEvent event, int64_t rate)
536 MonitorQAPIEventState *evstate;
537 assert(event < QAPI_EVENT_MAX);
539 evstate = &(monitor_qapi_event_state[event]);
541 trace_monitor_protocol_event_throttle(event, rate);
542 evstate->event = event;
543 assert(rate * SCALE_MS <= INT64_MAX);
544 evstate->rate = rate * SCALE_MS;
546 evstate->data = NULL;
547 evstate->timer = timer_new(QEMU_CLOCK_REALTIME,
549 monitor_qapi_event_handler,
553 static void monitor_qapi_event_init(void)
555 /* Limit guest-triggerable events to 1 per second */
556 monitor_qapi_event_throttle(QAPI_EVENT_RTC_CHANGE, 1000);
557 monitor_qapi_event_throttle(QAPI_EVENT_WATCHDOG, 1000);
558 monitor_qapi_event_throttle(QAPI_EVENT_BALLOON_CHANGE, 1000);
559 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_REPORT_BAD, 1000);
560 monitor_qapi_event_throttle(QAPI_EVENT_QUORUM_FAILURE, 1000);
561 monitor_qapi_event_throttle(QAPI_EVENT_VSERPORT_CHANGE, 1000);
563 qmp_event_set_func_emit(monitor_qapi_event_queue);
566 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
569 mon->qmp.in_command_mode = true;
573 static void handle_hmp_command(Monitor *mon, const char *cmdline);
575 static void monitor_data_init(Monitor *mon)
577 memset(mon, 0, sizeof(Monitor));
578 qemu_mutex_init(&mon->out_lock);
579 mon->outbuf = qstring_new();
580 /* Use *mon_cmds by default. */
581 mon->cmd_table = mon_cmds;
584 static void monitor_data_destroy(Monitor *mon)
586 QDECREF(mon->outbuf);
587 qemu_mutex_destroy(&mon->out_lock);
590 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
591 int64_t cpu_index, Error **errp)
594 Monitor *old_mon, hmp;
596 monitor_data_init(&hmp);
597 hmp.skip_flush = true;
603 int ret = monitor_set_cpu(cpu_index);
606 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
612 handle_hmp_command(&hmp, command_line);
615 qemu_mutex_lock(&hmp.out_lock);
616 if (qstring_get_length(hmp.outbuf) > 0) {
617 output = g_strdup(qstring_get_str(hmp.outbuf));
619 output = g_strdup("");
621 qemu_mutex_unlock(&hmp.out_lock);
624 monitor_data_destroy(&hmp);
628 static int compare_cmd(const char *name, const char *list)
630 const char *p, *pstart;
638 p = pstart + strlen(pstart);
639 if ((p - pstart) == len && !memcmp(pstart, name, len))
648 static int get_str(char *buf, int buf_size, const char **pp)
656 while (qemu_isspace(*p)) {
667 while (*p != '\0' && *p != '\"') {
683 qemu_printf("unsupported escape code: '\\%c'\n", c);
686 if ((q - buf) < buf_size - 1) {
690 if ((q - buf) < buf_size - 1) {
697 qemu_printf("unterminated string\n");
702 while (*p != '\0' && !qemu_isspace(*p)) {
703 if ((q - buf) < buf_size - 1) {
716 static void free_cmdline_args(char **args, int nb_args)
720 assert(nb_args <= MAX_ARGS);
722 for (i = 0; i < nb_args; i++) {
729 * Parse the command line to get valid args.
730 * @cmdline: command line to be parsed.
731 * @pnb_args: location to store the number of args, must NOT be NULL.
732 * @args: location to store the args, which should be freed by caller, must
735 * Returns 0 on success, negative on failure.
737 * NOTE: this parser is an approximate form of the real command parser. Number
738 * of args have a limit of MAX_ARGS. If cmdline contains more, it will
739 * return with failure.
741 static int parse_cmdline(const char *cmdline,
742 int *pnb_args, char **args)
751 while (qemu_isspace(*p)) {
757 if (nb_args >= MAX_ARGS) {
760 ret = get_str(buf, sizeof(buf), &p);
764 args[nb_args] = g_strdup(buf);
771 free_cmdline_args(args, nb_args);
775 static void help_cmd_dump_one(Monitor *mon,
776 const mon_cmd_t *cmd,
782 for (i = 0; i < prefix_args_nb; i++) {
783 monitor_printf(mon, "%s ", prefix_args[i]);
785 monitor_printf(mon, "%s %s -- %s\n", cmd->name, cmd->params, cmd->help);
788 /* @args[@arg_index] is the valid command need to find in @cmds */
789 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
790 char **args, int nb_args, int arg_index)
792 const mon_cmd_t *cmd;
794 /* No valid arg need to compare with, dump all in *cmds */
795 if (arg_index >= nb_args) {
796 for (cmd = cmds; cmd->name != NULL; cmd++) {
797 help_cmd_dump_one(mon, cmd, args, arg_index);
802 /* Find one entry to dump */
803 for (cmd = cmds; cmd->name != NULL; cmd++) {
804 if (compare_cmd(args[arg_index], cmd->name)) {
805 if (cmd->sub_table) {
806 /* continue with next arg */
807 help_cmd_dump(mon, cmd->sub_table,
808 args, nb_args, arg_index + 1);
810 help_cmd_dump_one(mon, cmd, args, arg_index);
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, mon->cmd_table, 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 Error *local_err = NULL;
857 qmp_trace_event_set_state(tp_name, new_state, true, true, &local_err);
859 error_report_err(local_err);
863 #ifdef CONFIG_TRACE_SIMPLE
864 static void hmp_trace_file(Monitor *mon, const QDict *qdict)
866 const char *op = qdict_get_try_str(qdict, "op");
867 const char *arg = qdict_get_try_str(qdict, "arg");
870 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
871 } else if (!strcmp(op, "on")) {
872 st_set_trace_file_enabled(true);
873 } else if (!strcmp(op, "off")) {
874 st_set_trace_file_enabled(false);
875 } else if (!strcmp(op, "flush")) {
876 st_flush_trace_buffer();
877 } else if (!strcmp(op, "set")) {
879 st_set_trace_file(arg);
882 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
883 help_cmd(mon, "trace-file");
888 static void hmp_info_help(Monitor *mon, const QDict *qdict)
890 help_cmd(mon, "info");
893 CommandInfoList *qmp_query_commands(Error **errp)
895 CommandInfoList *info, *cmd_list = NULL;
896 const mon_cmd_t *cmd;
898 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
899 info = g_malloc0(sizeof(*info));
900 info->value = g_malloc0(sizeof(*info->value));
901 info->value->name = g_strdup(cmd->name);
903 info->next = cmd_list;
910 EventInfoList *qmp_query_events(Error **errp)
912 EventInfoList *info, *ev_list = NULL;
915 for (e = 0 ; e < QAPI_EVENT_MAX ; e++) {
916 const char *event_name = QAPIEvent_lookup[e];
917 assert(event_name != NULL);
918 info = g_malloc0(sizeof(*info));
919 info->value = g_malloc0(sizeof(*info->value));
920 info->value->name = g_strdup(event_name);
922 info->next = ev_list;
929 /* set the current CPU defined by the user */
930 int monitor_set_cpu(int cpu_index)
934 cpu = qemu_get_cpu(cpu_index);
938 cur_mon->mon_cpu = cpu;
942 static CPUArchState *mon_get_cpu(void)
944 if (!cur_mon->mon_cpu) {
947 cpu_synchronize_state(cur_mon->mon_cpu);
948 return cur_mon->mon_cpu->env_ptr;
951 int monitor_get_cpu_index(void)
953 CPUState *cpu = ENV_GET_CPU(mon_get_cpu());
954 return cpu->cpu_index;
957 static void hmp_info_registers(Monitor *mon, const QDict *qdict)
962 cpu = ENV_GET_CPU(env);
963 cpu_dump_state(cpu, (FILE *)mon, monitor_fprintf, CPU_DUMP_FPU);
966 static void hmp_info_jit(Monitor *mon, const QDict *qdict)
968 dump_exec_info((FILE *)mon, monitor_fprintf);
969 dump_drift_info((FILE *)mon, monitor_fprintf);
972 static void hmp_info_opcount(Monitor *mon, const QDict *qdict)
974 dump_opcount_info((FILE *)mon, monitor_fprintf);
977 static void hmp_info_history(Monitor *mon, const QDict *qdict)
986 str = readline_get_history(mon->rs, i);
989 monitor_printf(mon, "%d: '%s'\n", i, str);
994 static void hmp_info_cpustats(Monitor *mon, const QDict *qdict)
1000 cpu = ENV_GET_CPU(env);
1001 cpu_dump_statistics(cpu, (FILE *)mon, &monitor_fprintf, 0);
1004 static void hmp_info_trace_events(Monitor *mon, const QDict *qdict)
1006 TraceEventInfoList *events = qmp_trace_event_get_state("*", NULL);
1007 TraceEventInfoList *elem;
1009 for (elem = events; elem != NULL; elem = elem->next) {
1010 monitor_printf(mon, "%s : state %u\n",
1012 elem->value->state == TRACE_EVENT_STATE_ENABLED ? 1 : 0);
1014 qapi_free_TraceEventInfoList(events);
1017 void qmp_client_migrate_info(const char *protocol, const char *hostname,
1018 bool has_port, int64_t port,
1019 bool has_tls_port, int64_t tls_port,
1020 bool has_cert_subject, const char *cert_subject,
1023 if (strcmp(protocol, "spice") == 0) {
1024 if (!qemu_using_spice(errp)) {
1028 if (!has_port && !has_tls_port) {
1029 error_set(errp, QERR_MISSING_PARAMETER, "port/tls-port");
1033 if (qemu_spice_migrate_info(hostname,
1034 has_port ? port : -1,
1035 has_tls_port ? tls_port : -1,
1037 error_set(errp, QERR_UNDEFINED_ERROR);
1043 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "protocol", "spice");
1046 static void hmp_logfile(Monitor *mon, const QDict *qdict)
1048 qemu_set_log_filename(qdict_get_str(qdict, "filename"));
1051 static void hmp_log(Monitor *mon, const QDict *qdict)
1054 const char *items = qdict_get_str(qdict, "items");
1056 if (!strcmp(items, "none")) {
1059 mask = qemu_str_to_log_mask(items);
1061 help_cmd(mon, "log");
1068 static void hmp_singlestep(Monitor *mon, const QDict *qdict)
1070 const char *option = qdict_get_try_str(qdict, "option");
1071 if (!option || !strcmp(option, "on")) {
1073 } else if (!strcmp(option, "off")) {
1076 monitor_printf(mon, "unexpected option %s\n", option);
1080 static void hmp_gdbserver(Monitor *mon, const QDict *qdict)
1082 const char *device = qdict_get_try_str(qdict, "device");
1084 device = "tcp::" DEFAULT_GDBSTUB_PORT;
1085 if (gdbserver_start(device) < 0) {
1086 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
1088 } else if (strcmp(device, "none") == 0) {
1089 monitor_printf(mon, "Disabled gdbserver\n");
1091 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
1096 static void hmp_watchdog_action(Monitor *mon, const QDict *qdict)
1098 const char *action = qdict_get_str(qdict, "action");
1099 if (select_watchdog_action(action) == -1) {
1100 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
1104 static void monitor_printc(Monitor *mon, int c)
1106 monitor_printf(mon, "'");
1109 monitor_printf(mon, "\\'");
1112 monitor_printf(mon, "\\\\");
1115 monitor_printf(mon, "\\n");
1118 monitor_printf(mon, "\\r");
1121 if (c >= 32 && c <= 126) {
1122 monitor_printf(mon, "%c", c);
1124 monitor_printf(mon, "\\x%02x", c);
1128 monitor_printf(mon, "'");
1131 static void memory_dump(Monitor *mon, int count, int format, int wsize,
1132 hwaddr addr, int is_physical)
1135 int l, line_size, i, max_digits, len;
1139 if (format == 'i') {
1142 env = mon_get_cpu();
1146 } else if (wsize == 4) {
1149 /* as default we use the current CS size */
1152 #ifdef TARGET_X86_64
1153 if ((env->efer & MSR_EFER_LMA) &&
1154 (env->segs[R_CS].flags & DESC_L_MASK))
1158 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1164 flags = msr_le << 16;
1165 flags |= env->bfd_mach;
1167 monitor_disas(mon, env, addr, count, is_physical, flags);
1171 len = wsize * count;
1180 max_digits = (wsize * 8 + 2) / 3;
1184 max_digits = (wsize * 8) / 4;
1188 max_digits = (wsize * 8 * 10 + 32) / 33;
1197 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1199 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1204 cpu_physical_memory_read(addr, buf, l);
1206 env = mon_get_cpu();
1207 if (cpu_memory_rw_debug(ENV_GET_CPU(env), addr, buf, l, 0) < 0) {
1208 monitor_printf(mon, " Cannot access memory\n");
1217 v = ldub_p(buf + i);
1220 v = lduw_p(buf + i);
1223 v = (uint32_t)ldl_p(buf + i);
1229 monitor_printf(mon, " ");
1232 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1235 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1238 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1241 monitor_printf(mon, "%*" PRId64, max_digits, v);
1244 monitor_printc(mon, v);
1249 monitor_printf(mon, "\n");
1255 static void hmp_memory_dump(Monitor *mon, const QDict *qdict)
1257 int count = qdict_get_int(qdict, "count");
1258 int format = qdict_get_int(qdict, "format");
1259 int size = qdict_get_int(qdict, "size");
1260 target_long addr = qdict_get_int(qdict, "addr");
1262 memory_dump(mon, count, format, size, addr, 0);
1265 static void hmp_physical_memory_dump(Monitor *mon, const QDict *qdict)
1267 int count = qdict_get_int(qdict, "count");
1268 int format = qdict_get_int(qdict, "format");
1269 int size = qdict_get_int(qdict, "size");
1270 hwaddr addr = qdict_get_int(qdict, "addr");
1272 memory_dump(mon, count, format, size, addr, 1);
1275 static void do_print(Monitor *mon, const QDict *qdict)
1277 int format = qdict_get_int(qdict, "format");
1278 hwaddr val = qdict_get_int(qdict, "val");
1282 monitor_printf(mon, "%#" HWADDR_PRIo, val);
1285 monitor_printf(mon, "%#" HWADDR_PRIx, val);
1288 monitor_printf(mon, "%" HWADDR_PRIu, val);
1292 monitor_printf(mon, "%" HWADDR_PRId, val);
1295 monitor_printc(mon, val);
1298 monitor_printf(mon, "\n");
1301 static void hmp_sum(Monitor *mon, const QDict *qdict)
1305 uint32_t start = qdict_get_int(qdict, "start");
1306 uint32_t size = qdict_get_int(qdict, "size");
1309 for(addr = start; addr < (start + size); addr++) {
1310 uint8_t val = address_space_ldub(&address_space_memory, addr,
1311 MEMTXATTRS_UNSPECIFIED, NULL);
1312 /* BSD sum algorithm ('sum' Unix command) */
1313 sum = (sum >> 1) | (sum << 15);
1316 monitor_printf(mon, "%05d\n", sum);
1319 static int mouse_button_state;
1321 static void hmp_mouse_move(Monitor *mon, const QDict *qdict)
1323 int dx, dy, dz, button;
1324 const char *dx_str = qdict_get_str(qdict, "dx_str");
1325 const char *dy_str = qdict_get_str(qdict, "dy_str");
1326 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1328 dx = strtol(dx_str, NULL, 0);
1329 dy = strtol(dy_str, NULL, 0);
1330 qemu_input_queue_rel(NULL, INPUT_AXIS_X, dx);
1331 qemu_input_queue_rel(NULL, INPUT_AXIS_Y, dy);
1334 dz = strtol(dz_str, NULL, 0);
1336 button = (dz > 0) ? INPUT_BUTTON_WHEEL_UP : INPUT_BUTTON_WHEEL_DOWN;
1337 qemu_input_queue_btn(NULL, button, true);
1338 qemu_input_event_sync();
1339 qemu_input_queue_btn(NULL, button, false);
1342 qemu_input_event_sync();
1345 static void hmp_mouse_button(Monitor *mon, const QDict *qdict)
1347 static uint32_t bmap[INPUT_BUTTON_MAX] = {
1348 [INPUT_BUTTON_LEFT] = MOUSE_EVENT_LBUTTON,
1349 [INPUT_BUTTON_MIDDLE] = MOUSE_EVENT_MBUTTON,
1350 [INPUT_BUTTON_RIGHT] = MOUSE_EVENT_RBUTTON,
1352 int button_state = qdict_get_int(qdict, "button_state");
1354 if (mouse_button_state == button_state) {
1357 qemu_input_update_buttons(NULL, bmap, mouse_button_state, button_state);
1358 qemu_input_event_sync();
1359 mouse_button_state = button_state;
1362 static void hmp_ioport_read(Monitor *mon, const QDict *qdict)
1364 int size = qdict_get_int(qdict, "size");
1365 int addr = qdict_get_int(qdict, "addr");
1366 int has_index = qdict_haskey(qdict, "index");
1371 int index = qdict_get_int(qdict, "index");
1372 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1380 val = cpu_inb(addr);
1384 val = cpu_inw(addr);
1388 val = cpu_inl(addr);
1392 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1393 suffix, addr, size * 2, val);
1396 static void hmp_ioport_write(Monitor *mon, const QDict *qdict)
1398 int size = qdict_get_int(qdict, "size");
1399 int addr = qdict_get_int(qdict, "addr");
1400 int val = qdict_get_int(qdict, "val");
1402 addr &= IOPORTS_MASK;
1407 cpu_outb(addr, val);
1410 cpu_outw(addr, val);
1413 cpu_outl(addr, val);
1418 static void hmp_boot_set(Monitor *mon, const QDict *qdict)
1420 Error *local_err = NULL;
1421 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1423 qemu_boot_set(bootdevice, &local_err);
1425 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
1426 error_free(local_err);
1428 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1432 #if defined(TARGET_I386)
1433 static void print_pte(Monitor *mon, hwaddr addr,
1437 #ifdef TARGET_X86_64
1438 if (addr & (1ULL << 47)) {
1442 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1443 " %c%c%c%c%c%c%c%c%c\n",
1446 pte & PG_NX_MASK ? 'X' : '-',
1447 pte & PG_GLOBAL_MASK ? 'G' : '-',
1448 pte & PG_PSE_MASK ? 'P' : '-',
1449 pte & PG_DIRTY_MASK ? 'D' : '-',
1450 pte & PG_ACCESSED_MASK ? 'A' : '-',
1451 pte & PG_PCD_MASK ? 'C' : '-',
1452 pte & PG_PWT_MASK ? 'T' : '-',
1453 pte & PG_USER_MASK ? 'U' : '-',
1454 pte & PG_RW_MASK ? 'W' : '-');
1457 static void tlb_info_32(Monitor *mon, CPUArchState *env)
1459 unsigned int l1, l2;
1460 uint32_t pgd, pde, pte;
1462 pgd = env->cr[3] & ~0xfff;
1463 for(l1 = 0; l1 < 1024; l1++) {
1464 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1465 pde = le32_to_cpu(pde);
1466 if (pde & PG_PRESENT_MASK) {
1467 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1469 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1471 for(l2 = 0; l2 < 1024; l2++) {
1472 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1473 pte = le32_to_cpu(pte);
1474 if (pte & PG_PRESENT_MASK) {
1475 print_pte(mon, (l1 << 22) + (l2 << 12),
1485 static void tlb_info_pae32(Monitor *mon, CPUArchState *env)
1487 unsigned int l1, l2, l3;
1488 uint64_t pdpe, pde, pte;
1489 uint64_t pdp_addr, pd_addr, pt_addr;
1491 pdp_addr = env->cr[3] & ~0x1f;
1492 for (l1 = 0; l1 < 4; l1++) {
1493 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1494 pdpe = le64_to_cpu(pdpe);
1495 if (pdpe & PG_PRESENT_MASK) {
1496 pd_addr = pdpe & 0x3fffffffff000ULL;
1497 for (l2 = 0; l2 < 512; l2++) {
1498 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1499 pde = le64_to_cpu(pde);
1500 if (pde & PG_PRESENT_MASK) {
1501 if (pde & PG_PSE_MASK) {
1502 /* 2M pages with PAE, CR4.PSE is ignored */
1503 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1504 ~((hwaddr)(1 << 20) - 1));
1506 pt_addr = pde & 0x3fffffffff000ULL;
1507 for (l3 = 0; l3 < 512; l3++) {
1508 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1509 pte = le64_to_cpu(pte);
1510 if (pte & PG_PRESENT_MASK) {
1511 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1524 #ifdef TARGET_X86_64
1525 static void tlb_info_64(Monitor *mon, CPUArchState *env)
1527 uint64_t l1, l2, l3, l4;
1528 uint64_t pml4e, pdpe, pde, pte;
1529 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1531 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1532 for (l1 = 0; l1 < 512; l1++) {
1533 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1534 pml4e = le64_to_cpu(pml4e);
1535 if (pml4e & PG_PRESENT_MASK) {
1536 pdp_addr = pml4e & 0x3fffffffff000ULL;
1537 for (l2 = 0; l2 < 512; l2++) {
1538 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1539 pdpe = le64_to_cpu(pdpe);
1540 if (pdpe & PG_PRESENT_MASK) {
1541 if (pdpe & PG_PSE_MASK) {
1542 /* 1G pages, CR4.PSE is ignored */
1543 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1544 0x3ffffc0000000ULL);
1546 pd_addr = pdpe & 0x3fffffffff000ULL;
1547 for (l3 = 0; l3 < 512; l3++) {
1548 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1549 pde = le64_to_cpu(pde);
1550 if (pde & PG_PRESENT_MASK) {
1551 if (pde & PG_PSE_MASK) {
1552 /* 2M pages, CR4.PSE is ignored */
1553 print_pte(mon, (l1 << 39) + (l2 << 30) +
1555 0x3ffffffe00000ULL);
1557 pt_addr = pde & 0x3fffffffff000ULL;
1558 for (l4 = 0; l4 < 512; l4++) {
1559 cpu_physical_memory_read(pt_addr
1562 pte = le64_to_cpu(pte);
1563 if (pte & PG_PRESENT_MASK) {
1564 print_pte(mon, (l1 << 39) +
1566 (l3 << 21) + (l4 << 12),
1568 0x3fffffffff000ULL);
1582 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1586 env = mon_get_cpu();
1588 if (!(env->cr[0] & CR0_PG_MASK)) {
1589 monitor_printf(mon, "PG disabled\n");
1592 if (env->cr[4] & CR4_PAE_MASK) {
1593 #ifdef TARGET_X86_64
1594 if (env->hflags & HF_LMA_MASK) {
1595 tlb_info_64(mon, env);
1599 tlb_info_pae32(mon, env);
1602 tlb_info_32(mon, env);
1606 static void mem_print(Monitor *mon, hwaddr *pstart,
1608 hwaddr end, int prot)
1611 prot1 = *plast_prot;
1612 if (prot != prot1) {
1613 if (*pstart != -1) {
1614 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1615 TARGET_FMT_plx " %c%c%c\n",
1616 *pstart, end, end - *pstart,
1617 prot1 & PG_USER_MASK ? 'u' : '-',
1619 prot1 & PG_RW_MASK ? 'w' : '-');
1629 static void mem_info_32(Monitor *mon, CPUArchState *env)
1631 unsigned int l1, l2;
1632 int prot, last_prot;
1633 uint32_t pgd, pde, pte;
1636 pgd = env->cr[3] & ~0xfff;
1639 for(l1 = 0; l1 < 1024; l1++) {
1640 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1641 pde = le32_to_cpu(pde);
1643 if (pde & PG_PRESENT_MASK) {
1644 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1645 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1646 mem_print(mon, &start, &last_prot, end, prot);
1648 for(l2 = 0; l2 < 1024; l2++) {
1649 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1650 pte = le32_to_cpu(pte);
1651 end = (l1 << 22) + (l2 << 12);
1652 if (pte & PG_PRESENT_MASK) {
1654 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1658 mem_print(mon, &start, &last_prot, end, prot);
1663 mem_print(mon, &start, &last_prot, end, prot);
1666 /* Flush last range */
1667 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1670 static void mem_info_pae32(Monitor *mon, CPUArchState *env)
1672 unsigned int l1, l2, l3;
1673 int prot, last_prot;
1674 uint64_t pdpe, pde, pte;
1675 uint64_t pdp_addr, pd_addr, pt_addr;
1678 pdp_addr = env->cr[3] & ~0x1f;
1681 for (l1 = 0; l1 < 4; l1++) {
1682 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1683 pdpe = le64_to_cpu(pdpe);
1685 if (pdpe & PG_PRESENT_MASK) {
1686 pd_addr = pdpe & 0x3fffffffff000ULL;
1687 for (l2 = 0; l2 < 512; l2++) {
1688 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1689 pde = le64_to_cpu(pde);
1690 end = (l1 << 30) + (l2 << 21);
1691 if (pde & PG_PRESENT_MASK) {
1692 if (pde & PG_PSE_MASK) {
1693 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1695 mem_print(mon, &start, &last_prot, end, prot);
1697 pt_addr = pde & 0x3fffffffff000ULL;
1698 for (l3 = 0; l3 < 512; l3++) {
1699 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1700 pte = le64_to_cpu(pte);
1701 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1702 if (pte & PG_PRESENT_MASK) {
1703 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1708 mem_print(mon, &start, &last_prot, end, prot);
1713 mem_print(mon, &start, &last_prot, end, prot);
1718 mem_print(mon, &start, &last_prot, end, prot);
1721 /* Flush last range */
1722 mem_print(mon, &start, &last_prot, (hwaddr)1 << 32, 0);
1726 #ifdef TARGET_X86_64
1727 static void mem_info_64(Monitor *mon, CPUArchState *env)
1729 int prot, last_prot;
1730 uint64_t l1, l2, l3, l4;
1731 uint64_t pml4e, pdpe, pde, pte;
1732 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1734 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1737 for (l1 = 0; l1 < 512; l1++) {
1738 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1739 pml4e = le64_to_cpu(pml4e);
1741 if (pml4e & PG_PRESENT_MASK) {
1742 pdp_addr = pml4e & 0x3fffffffff000ULL;
1743 for (l2 = 0; l2 < 512; l2++) {
1744 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1745 pdpe = le64_to_cpu(pdpe);
1746 end = (l1 << 39) + (l2 << 30);
1747 if (pdpe & PG_PRESENT_MASK) {
1748 if (pdpe & PG_PSE_MASK) {
1749 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1752 mem_print(mon, &start, &last_prot, end, prot);
1754 pd_addr = pdpe & 0x3fffffffff000ULL;
1755 for (l3 = 0; l3 < 512; l3++) {
1756 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1757 pde = le64_to_cpu(pde);
1758 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1759 if (pde & PG_PRESENT_MASK) {
1760 if (pde & PG_PSE_MASK) {
1761 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1763 prot &= pml4e & pdpe;
1764 mem_print(mon, &start, &last_prot, end, prot);
1766 pt_addr = pde & 0x3fffffffff000ULL;
1767 for (l4 = 0; l4 < 512; l4++) {
1768 cpu_physical_memory_read(pt_addr
1771 pte = le64_to_cpu(pte);
1772 end = (l1 << 39) + (l2 << 30) +
1773 (l3 << 21) + (l4 << 12);
1774 if (pte & PG_PRESENT_MASK) {
1775 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1777 prot &= pml4e & pdpe & pde;
1781 mem_print(mon, &start, &last_prot, end, prot);
1786 mem_print(mon, &start, &last_prot, end, prot);
1792 mem_print(mon, &start, &last_prot, end, prot);
1797 mem_print(mon, &start, &last_prot, end, prot);
1800 /* Flush last range */
1801 mem_print(mon, &start, &last_prot, (hwaddr)1 << 48, 0);
1805 static void hmp_info_mem(Monitor *mon, const QDict *qdict)
1809 env = mon_get_cpu();
1811 if (!(env->cr[0] & CR0_PG_MASK)) {
1812 monitor_printf(mon, "PG disabled\n");
1815 if (env->cr[4] & CR4_PAE_MASK) {
1816 #ifdef TARGET_X86_64
1817 if (env->hflags & HF_LMA_MASK) {
1818 mem_info_64(mon, env);
1822 mem_info_pae32(mon, env);
1825 mem_info_32(mon, env);
1830 #if defined(TARGET_SH4)
1832 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1834 monitor_printf(mon, " tlb%i:\t"
1835 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1836 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1837 "dirty=%hhu writethrough=%hhu\n",
1839 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1840 tlb->v, tlb->sh, tlb->c, tlb->pr,
1844 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1846 CPUArchState *env = mon_get_cpu();
1849 monitor_printf (mon, "ITLB:\n");
1850 for (i = 0 ; i < ITLB_SIZE ; i++)
1851 print_tlb (mon, i, &env->itlb[i]);
1852 monitor_printf (mon, "UTLB:\n");
1853 for (i = 0 ; i < UTLB_SIZE ; i++)
1854 print_tlb (mon, i, &env->utlb[i]);
1859 #if defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_XTENSA)
1860 static void hmp_info_tlb(Monitor *mon, const QDict *qdict)
1862 CPUArchState *env1 = mon_get_cpu();
1864 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1868 static void hmp_info_mtree(Monitor *mon, const QDict *qdict)
1870 mtree_info((fprintf_function)monitor_printf, mon);
1873 static void hmp_info_numa(Monitor *mon, const QDict *qdict)
1879 node_mem = g_new0(uint64_t, nb_numa_nodes);
1880 query_numa_node_mem(node_mem);
1881 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1882 for (i = 0; i < nb_numa_nodes; i++) {
1883 monitor_printf(mon, "node %d cpus:", i);
1885 if (cpu->numa_node == i) {
1886 monitor_printf(mon, " %d", cpu->cpu_index);
1889 monitor_printf(mon, "\n");
1890 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1896 #ifdef CONFIG_PROFILER
1901 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1903 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1904 dev_time, dev_time / (double)get_ticks_per_sec());
1905 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1906 tcg_time, tcg_time / (double)get_ticks_per_sec());
1911 static void hmp_info_profile(Monitor *mon, const QDict *qdict)
1913 monitor_printf(mon, "Internal profiler not compiled\n");
1917 /* Capture support */
1918 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1920 static void hmp_info_capture(Monitor *mon, const QDict *qdict)
1925 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1926 monitor_printf(mon, "[%d]: ", i);
1927 s->ops.info (s->opaque);
1931 static void hmp_stopcapture(Monitor *mon, const QDict *qdict)
1934 int n = qdict_get_int(qdict, "n");
1937 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1939 s->ops.destroy (s->opaque);
1940 QLIST_REMOVE (s, entries);
1947 static void hmp_wavcapture(Monitor *mon, const QDict *qdict)
1949 const char *path = qdict_get_str(qdict, "path");
1950 int has_freq = qdict_haskey(qdict, "freq");
1951 int freq = qdict_get_try_int(qdict, "freq", -1);
1952 int has_bits = qdict_haskey(qdict, "bits");
1953 int bits = qdict_get_try_int(qdict, "bits", -1);
1954 int has_channels = qdict_haskey(qdict, "nchannels");
1955 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1958 s = g_malloc0 (sizeof (*s));
1960 freq = has_freq ? freq : 44100;
1961 bits = has_bits ? bits : 16;
1962 nchannels = has_channels ? nchannels : 2;
1964 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1965 monitor_printf(mon, "Failed to add wave capture\n");
1969 QLIST_INSERT_HEAD (&capture_head, s, entries);
1972 static qemu_acl *find_acl(Monitor *mon, const char *name)
1974 qemu_acl *acl = qemu_acl_find(name);
1977 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1982 static void hmp_acl_show(Monitor *mon, const QDict *qdict)
1984 const char *aclname = qdict_get_str(qdict, "aclname");
1985 qemu_acl *acl = find_acl(mon, aclname);
1986 qemu_acl_entry *entry;
1990 monitor_printf(mon, "policy: %s\n",
1991 acl->defaultDeny ? "deny" : "allow");
1992 QTAILQ_FOREACH(entry, &acl->entries, next) {
1994 monitor_printf(mon, "%d: %s %s\n", i,
1995 entry->deny ? "deny" : "allow", entry->match);
2000 static void hmp_acl_reset(Monitor *mon, const QDict *qdict)
2002 const char *aclname = qdict_get_str(qdict, "aclname");
2003 qemu_acl *acl = find_acl(mon, aclname);
2006 qemu_acl_reset(acl);
2007 monitor_printf(mon, "acl: removed all rules\n");
2011 static void hmp_acl_policy(Monitor *mon, const QDict *qdict)
2013 const char *aclname = qdict_get_str(qdict, "aclname");
2014 const char *policy = qdict_get_str(qdict, "policy");
2015 qemu_acl *acl = find_acl(mon, aclname);
2018 if (strcmp(policy, "allow") == 0) {
2019 acl->defaultDeny = 0;
2020 monitor_printf(mon, "acl: policy set to 'allow'\n");
2021 } else if (strcmp(policy, "deny") == 0) {
2022 acl->defaultDeny = 1;
2023 monitor_printf(mon, "acl: policy set to 'deny'\n");
2025 monitor_printf(mon, "acl: unknown policy '%s', "
2026 "expected 'deny' or 'allow'\n", policy);
2031 static void hmp_acl_add(Monitor *mon, const QDict *qdict)
2033 const char *aclname = qdict_get_str(qdict, "aclname");
2034 const char *match = qdict_get_str(qdict, "match");
2035 const char *policy = qdict_get_str(qdict, "policy");
2036 int has_index = qdict_haskey(qdict, "index");
2037 int index = qdict_get_try_int(qdict, "index", -1);
2038 qemu_acl *acl = find_acl(mon, aclname);
2042 if (strcmp(policy, "allow") == 0) {
2044 } else if (strcmp(policy, "deny") == 0) {
2047 monitor_printf(mon, "acl: unknown policy '%s', "
2048 "expected 'deny' or 'allow'\n", policy);
2052 ret = qemu_acl_insert(acl, deny, match, index);
2054 ret = qemu_acl_append(acl, deny, match);
2056 monitor_printf(mon, "acl: unable to add acl entry\n");
2058 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2062 static void hmp_acl_remove(Monitor *mon, const QDict *qdict)
2064 const char *aclname = qdict_get_str(qdict, "aclname");
2065 const char *match = qdict_get_str(qdict, "match");
2066 qemu_acl *acl = find_acl(mon, aclname);
2070 ret = qemu_acl_remove(acl, match);
2072 monitor_printf(mon, "acl: no matching acl entry\n");
2074 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2078 #if defined(TARGET_I386)
2079 static void hmp_mce(Monitor *mon, const QDict *qdict)
2083 int cpu_index = qdict_get_int(qdict, "cpu_index");
2084 int bank = qdict_get_int(qdict, "bank");
2085 uint64_t status = qdict_get_int(qdict, "status");
2086 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2087 uint64_t addr = qdict_get_int(qdict, "addr");
2088 uint64_t misc = qdict_get_int(qdict, "misc");
2089 int flags = MCE_INJECT_UNCOND_AO;
2091 if (qdict_get_try_bool(qdict, "broadcast", false)) {
2092 flags |= MCE_INJECT_BROADCAST;
2094 cs = qemu_get_cpu(cpu_index);
2097 cpu_x86_inject_mce(mon, cpu, bank, status, mcg_status, addr, misc,
2103 void qmp_getfd(const char *fdname, Error **errp)
2108 fd = qemu_chr_fe_get_msgfd(cur_mon->chr);
2110 error_set(errp, QERR_FD_NOT_SUPPLIED);
2114 if (qemu_isdigit(fdname[0])) {
2116 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdname",
2117 "a name not starting with a digit");
2121 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2122 if (strcmp(monfd->name, fdname) != 0) {
2131 monfd = g_malloc0(sizeof(mon_fd_t));
2132 monfd->name = g_strdup(fdname);
2135 QLIST_INSERT_HEAD(&cur_mon->fds, monfd, next);
2138 void qmp_closefd(const char *fdname, Error **errp)
2142 QLIST_FOREACH(monfd, &cur_mon->fds, next) {
2143 if (strcmp(monfd->name, fdname) != 0) {
2147 QLIST_REMOVE(monfd, next);
2149 g_free(monfd->name);
2154 error_set(errp, QERR_FD_NOT_FOUND, fdname);
2157 static void hmp_loadvm(Monitor *mon, const QDict *qdict)
2159 int saved_vm_running = runstate_is_running();
2160 const char *name = qdict_get_str(qdict, "name");
2162 vm_stop(RUN_STATE_RESTORE_VM);
2164 if (load_vmstate(name) == 0 && saved_vm_running) {
2169 int monitor_get_fd(Monitor *mon, const char *fdname, Error **errp)
2173 QLIST_FOREACH(monfd, &mon->fds, next) {
2176 if (strcmp(monfd->name, fdname) != 0) {
2182 /* caller takes ownership of fd */
2183 QLIST_REMOVE(monfd, next);
2184 g_free(monfd->name);
2190 error_setg(errp, "File descriptor named '%s' has not been found", fdname);
2194 static void monitor_fdset_cleanup(MonFdset *mon_fdset)
2196 MonFdsetFd *mon_fdset_fd;
2197 MonFdsetFd *mon_fdset_fd_next;
2199 QLIST_FOREACH_SAFE(mon_fdset_fd, &mon_fdset->fds, next, mon_fdset_fd_next) {
2200 if ((mon_fdset_fd->removed ||
2201 (QLIST_EMPTY(&mon_fdset->dup_fds) && mon_refcount == 0)) &&
2202 runstate_is_running()) {
2203 close(mon_fdset_fd->fd);
2204 g_free(mon_fdset_fd->opaque);
2205 QLIST_REMOVE(mon_fdset_fd, next);
2206 g_free(mon_fdset_fd);
2210 if (QLIST_EMPTY(&mon_fdset->fds) && QLIST_EMPTY(&mon_fdset->dup_fds)) {
2211 QLIST_REMOVE(mon_fdset, next);
2216 static void monitor_fdsets_cleanup(void)
2218 MonFdset *mon_fdset;
2219 MonFdset *mon_fdset_next;
2221 QLIST_FOREACH_SAFE(mon_fdset, &mon_fdsets, next, mon_fdset_next) {
2222 monitor_fdset_cleanup(mon_fdset);
2226 AddfdInfo *qmp_add_fd(bool has_fdset_id, int64_t fdset_id, bool has_opaque,
2227 const char *opaque, Error **errp)
2230 Monitor *mon = cur_mon;
2233 fd = qemu_chr_fe_get_msgfd(mon->chr);
2235 error_set(errp, QERR_FD_NOT_SUPPLIED);
2239 fdinfo = monitor_fdset_add_fd(fd, has_fdset_id, fdset_id,
2240 has_opaque, opaque, errp);
2252 void qmp_remove_fd(int64_t fdset_id, bool has_fd, int64_t fd, Error **errp)
2254 MonFdset *mon_fdset;
2255 MonFdsetFd *mon_fdset_fd;
2258 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2259 if (mon_fdset->id != fdset_id) {
2262 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2264 if (mon_fdset_fd->fd != fd) {
2267 mon_fdset_fd->removed = true;
2270 mon_fdset_fd->removed = true;
2273 if (has_fd && !mon_fdset_fd) {
2276 monitor_fdset_cleanup(mon_fdset);
2282 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64 ", fd:%" PRId64,
2285 snprintf(fd_str, sizeof(fd_str), "fdset-id:%" PRId64, fdset_id);
2287 error_set(errp, QERR_FD_NOT_FOUND, fd_str);
2290 FdsetInfoList *qmp_query_fdsets(Error **errp)
2292 MonFdset *mon_fdset;
2293 MonFdsetFd *mon_fdset_fd;
2294 FdsetInfoList *fdset_list = NULL;
2296 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2297 FdsetInfoList *fdset_info = g_malloc0(sizeof(*fdset_info));
2298 FdsetFdInfoList *fdsetfd_list = NULL;
2300 fdset_info->value = g_malloc0(sizeof(*fdset_info->value));
2301 fdset_info->value->fdset_id = mon_fdset->id;
2303 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2304 FdsetFdInfoList *fdsetfd_info;
2306 fdsetfd_info = g_malloc0(sizeof(*fdsetfd_info));
2307 fdsetfd_info->value = g_malloc0(sizeof(*fdsetfd_info->value));
2308 fdsetfd_info->value->fd = mon_fdset_fd->fd;
2309 if (mon_fdset_fd->opaque) {
2310 fdsetfd_info->value->has_opaque = true;
2311 fdsetfd_info->value->opaque = g_strdup(mon_fdset_fd->opaque);
2313 fdsetfd_info->value->has_opaque = false;
2316 fdsetfd_info->next = fdsetfd_list;
2317 fdsetfd_list = fdsetfd_info;
2320 fdset_info->value->fds = fdsetfd_list;
2322 fdset_info->next = fdset_list;
2323 fdset_list = fdset_info;
2329 AddfdInfo *monitor_fdset_add_fd(int fd, bool has_fdset_id, int64_t fdset_id,
2330 bool has_opaque, const char *opaque,
2333 MonFdset *mon_fdset = NULL;
2334 MonFdsetFd *mon_fdset_fd;
2338 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2339 /* Break if match found or match impossible due to ordering by ID */
2340 if (fdset_id <= mon_fdset->id) {
2341 if (fdset_id < mon_fdset->id) {
2349 if (mon_fdset == NULL) {
2350 int64_t fdset_id_prev = -1;
2351 MonFdset *mon_fdset_cur = QLIST_FIRST(&mon_fdsets);
2355 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "fdset-id",
2356 "a non-negative value");
2359 /* Use specified fdset ID */
2360 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2361 mon_fdset_cur = mon_fdset;
2362 if (fdset_id < mon_fdset_cur->id) {
2367 /* Use first available fdset ID */
2368 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2369 mon_fdset_cur = mon_fdset;
2370 if (fdset_id_prev == mon_fdset_cur->id - 1) {
2371 fdset_id_prev = mon_fdset_cur->id;
2378 mon_fdset = g_malloc0(sizeof(*mon_fdset));
2380 mon_fdset->id = fdset_id;
2382 mon_fdset->id = fdset_id_prev + 1;
2385 /* The fdset list is ordered by fdset ID */
2386 if (!mon_fdset_cur) {
2387 QLIST_INSERT_HEAD(&mon_fdsets, mon_fdset, next);
2388 } else if (mon_fdset->id < mon_fdset_cur->id) {
2389 QLIST_INSERT_BEFORE(mon_fdset_cur, mon_fdset, next);
2391 QLIST_INSERT_AFTER(mon_fdset_cur, mon_fdset, next);
2395 mon_fdset_fd = g_malloc0(sizeof(*mon_fdset_fd));
2396 mon_fdset_fd->fd = fd;
2397 mon_fdset_fd->removed = false;
2399 mon_fdset_fd->opaque = g_strdup(opaque);
2401 QLIST_INSERT_HEAD(&mon_fdset->fds, mon_fdset_fd, next);
2403 fdinfo = g_malloc0(sizeof(*fdinfo));
2404 fdinfo->fdset_id = mon_fdset->id;
2405 fdinfo->fd = mon_fdset_fd->fd;
2410 int monitor_fdset_get_fd(int64_t fdset_id, int flags)
2413 MonFdset *mon_fdset;
2414 MonFdsetFd *mon_fdset_fd;
2417 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2418 if (mon_fdset->id != fdset_id) {
2421 QLIST_FOREACH(mon_fdset_fd, &mon_fdset->fds, next) {
2422 mon_fd_flags = fcntl(mon_fdset_fd->fd, F_GETFL);
2423 if (mon_fd_flags == -1) {
2427 if ((flags & O_ACCMODE) == (mon_fd_flags & O_ACCMODE)) {
2428 return mon_fdset_fd->fd;
2440 int monitor_fdset_dup_fd_add(int64_t fdset_id, int dup_fd)
2442 MonFdset *mon_fdset;
2443 MonFdsetFd *mon_fdset_fd_dup;
2445 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2446 if (mon_fdset->id != fdset_id) {
2449 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2450 if (mon_fdset_fd_dup->fd == dup_fd) {
2454 mon_fdset_fd_dup = g_malloc0(sizeof(*mon_fdset_fd_dup));
2455 mon_fdset_fd_dup->fd = dup_fd;
2456 QLIST_INSERT_HEAD(&mon_fdset->dup_fds, mon_fdset_fd_dup, next);
2462 static int monitor_fdset_dup_fd_find_remove(int dup_fd, bool remove)
2464 MonFdset *mon_fdset;
2465 MonFdsetFd *mon_fdset_fd_dup;
2467 QLIST_FOREACH(mon_fdset, &mon_fdsets, next) {
2468 QLIST_FOREACH(mon_fdset_fd_dup, &mon_fdset->dup_fds, next) {
2469 if (mon_fdset_fd_dup->fd == dup_fd) {
2471 QLIST_REMOVE(mon_fdset_fd_dup, next);
2472 if (QLIST_EMPTY(&mon_fdset->dup_fds)) {
2473 monitor_fdset_cleanup(mon_fdset);
2477 return mon_fdset->id;
2485 int monitor_fdset_dup_fd_find(int dup_fd)
2487 return monitor_fdset_dup_fd_find_remove(dup_fd, false);
2490 void monitor_fdset_dup_fd_remove(int dup_fd)
2492 monitor_fdset_dup_fd_find_remove(dup_fd, true);
2495 int monitor_fd_param(Monitor *mon, const char *fdname, Error **errp)
2498 Error *local_err = NULL;
2500 if (!qemu_isdigit(fdname[0]) && mon) {
2501 fd = monitor_get_fd(mon, fdname, &local_err);
2503 fd = qemu_parse_fd(fdname);
2505 error_setg(&local_err, "Invalid file descriptor number '%s'",
2510 error_propagate(errp, local_err);
2519 /* Please update hmp-commands.hx when adding or changing commands */
2520 static mon_cmd_t info_cmds[] = {
2525 .help = "show the version of QEMU",
2526 .mhandler.cmd = hmp_info_version,
2532 .help = "show the network state",
2533 .mhandler.cmd = hmp_info_network,
2539 .help = "show the character devices",
2540 .mhandler.cmd = hmp_info_chardev,
2544 .args_type = "nodes:-n,verbose:-v,device:B?",
2545 .params = "[-n] [-v] [device]",
2546 .help = "show info of one block device or all block devices "
2547 "(-n: show named nodes; -v: show details)",
2548 .mhandler.cmd = hmp_info_block,
2551 .name = "blockstats",
2554 .help = "show block device statistics",
2555 .mhandler.cmd = hmp_info_blockstats,
2558 .name = "block-jobs",
2561 .help = "show progress of ongoing block device operations",
2562 .mhandler.cmd = hmp_info_block_jobs,
2565 .name = "registers",
2568 .help = "show the cpu registers",
2569 .mhandler.cmd = hmp_info_registers,
2575 .help = "show infos for each CPU",
2576 .mhandler.cmd = hmp_info_cpus,
2582 .help = "show the command line history",
2583 .mhandler.cmd = hmp_info_history,
2585 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2586 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2591 .help = "show the interrupts statistics (if available)",
2593 .mhandler.cmd = sun4m_hmp_info_irq,
2594 #elif defined(TARGET_LM32)
2595 .mhandler.cmd = lm32_hmp_info_irq,
2597 .mhandler.cmd = hmp_info_irq,
2604 .help = "show i8259 (PIC) state",
2606 .mhandler.cmd = sun4m_hmp_info_pic,
2607 #elif defined(TARGET_LM32)
2608 .mhandler.cmd = lm32_hmp_info_pic,
2610 .mhandler.cmd = hmp_info_pic,
2618 .help = "show PCI info",
2619 .mhandler.cmd = hmp_info_pci,
2621 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2622 defined(TARGET_PPC) || defined(TARGET_XTENSA)
2627 .help = "show virtual to physical memory mappings",
2628 .mhandler.cmd = hmp_info_tlb,
2631 #if defined(TARGET_I386)
2636 .help = "show the active virtual memory mappings",
2637 .mhandler.cmd = hmp_info_mem,
2644 .help = "show memory tree",
2645 .mhandler.cmd = hmp_info_mtree,
2651 .help = "show dynamic compiler info",
2652 .mhandler.cmd = hmp_info_jit,
2658 .help = "show dynamic compiler opcode counters",
2659 .mhandler.cmd = hmp_info_opcount,
2665 .help = "show KVM information",
2666 .mhandler.cmd = hmp_info_kvm,
2672 .help = "show NUMA information",
2673 .mhandler.cmd = hmp_info_numa,
2679 .help = "show guest USB devices",
2680 .mhandler.cmd = hmp_info_usb,
2686 .help = "show host USB devices",
2687 .mhandler.cmd = hmp_info_usbhost,
2693 .help = "show profiling information",
2694 .mhandler.cmd = hmp_info_profile,
2700 .help = "show capture information",
2701 .mhandler.cmd = hmp_info_capture,
2704 .name = "snapshots",
2707 .help = "show the currently saved VM snapshots",
2708 .mhandler.cmd = hmp_info_snapshots,
2714 .help = "show the current VM status (running|paused)",
2715 .mhandler.cmd = hmp_info_status,
2721 .help = "show which guest mouse is receiving events",
2722 .mhandler.cmd = hmp_info_mice,
2728 .help = "show the vnc server status",
2729 .mhandler.cmd = hmp_info_vnc,
2731 #if defined(CONFIG_SPICE)
2736 .help = "show the spice server status",
2737 .mhandler.cmd = hmp_info_spice,
2744 .help = "show the current VM name",
2745 .mhandler.cmd = hmp_info_name,
2751 .help = "show the current VM UUID",
2752 .mhandler.cmd = hmp_info_uuid,
2758 .help = "show CPU statistics",
2759 .mhandler.cmd = hmp_info_cpustats,
2761 #if defined(CONFIG_SLIRP)
2766 .help = "show user network stack connection states",
2767 .mhandler.cmd = hmp_info_usernet,
2774 .help = "show migration status",
2775 .mhandler.cmd = hmp_info_migrate,
2778 .name = "migrate_capabilities",
2781 .help = "show current migration capabilities",
2782 .mhandler.cmd = hmp_info_migrate_capabilities,
2785 .name = "migrate_parameters",
2788 .help = "show current migration parameters",
2789 .mhandler.cmd = hmp_info_migrate_parameters,
2792 .name = "migrate_cache_size",
2795 .help = "show current migration xbzrle cache size",
2796 .mhandler.cmd = hmp_info_migrate_cache_size,
2802 .help = "show balloon information",
2803 .mhandler.cmd = hmp_info_balloon,
2809 .help = "show device tree",
2810 .mhandler.cmd = hmp_info_qtree,
2816 .help = "show qdev device model list",
2817 .mhandler.cmd = hmp_info_qdm,
2821 .args_type = "path:s?",
2823 .help = "show QOM composition tree",
2824 .mhandler.cmd = hmp_info_qom_tree,
2830 .help = "show roms",
2831 .mhandler.cmd = hmp_info_roms,
2834 .name = "trace-events",
2837 .help = "show available trace-events & their state",
2838 .mhandler.cmd = hmp_info_trace_events,
2844 .help = "show the TPM device",
2845 .mhandler.cmd = hmp_info_tpm,
2851 .help = "show memory backends",
2852 .mhandler.cmd = hmp_info_memdev,
2855 .name = "memory-devices",
2858 .help = "show memory devices",
2859 .mhandler.cmd = hmp_info_memory_devices,
2863 .args_type = "name:s",
2865 .help = "Show rocker switch",
2866 .mhandler.cmd = hmp_rocker,
2869 .name = "rocker-ports",
2870 .args_type = "name:s",
2872 .help = "Show rocker ports",
2873 .mhandler.cmd = hmp_rocker_ports,
2876 .name = "rocker-of-dpa-flows",
2877 .args_type = "name:s,tbl_id:i?",
2878 .params = "name [tbl_id]",
2879 .help = "Show rocker OF-DPA flow tables",
2880 .mhandler.cmd = hmp_rocker_of_dpa_flows,
2883 .name = "rocker-of-dpa-groups",
2884 .args_type = "name:s,type:i?",
2885 .params = "name [type]",
2886 .help = "Show rocker OF-DPA groups",
2887 .mhandler.cmd = hmp_rocker_of_dpa_groups,
2894 /* mon_cmds and info_cmds would be sorted at runtime */
2895 static mon_cmd_t mon_cmds[] = {
2896 #include "hmp-commands.h"
2900 static const mon_cmd_t qmp_cmds[] = {
2901 #include "qmp-commands-old.h"
2905 /*******************************************************************/
2907 static const char *pch;
2908 static sigjmp_buf expr_env;
2913 typedef struct MonitorDef {
2916 target_long (*get_value)(const struct MonitorDef *md, int val);
2920 #if defined(TARGET_I386)
2921 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2923 CPUArchState *env = mon_get_cpu();
2924 return env->eip + env->segs[R_CS].base;
2928 #if defined(TARGET_PPC)
2929 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2931 CPUArchState *env = mon_get_cpu();
2936 for (i = 0; i < 8; i++)
2937 u |= env->crf[i] << (32 - (4 * (i + 1)));
2942 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2944 CPUArchState *env = mon_get_cpu();
2948 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2950 CPUArchState *env = mon_get_cpu();
2954 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2956 CPUArchState *env = mon_get_cpu();
2957 return cpu_ppc_load_decr(env);
2960 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2962 CPUArchState *env = mon_get_cpu();
2963 return cpu_ppc_load_tbu(env);
2966 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2968 CPUArchState *env = mon_get_cpu();
2969 return cpu_ppc_load_tbl(env);
2973 #if defined(TARGET_SPARC)
2974 #ifndef TARGET_SPARC64
2975 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2977 CPUArchState *env = mon_get_cpu();
2979 return cpu_get_psr(env);
2983 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2985 CPUArchState *env = mon_get_cpu();
2986 return env->regwptr[val];
2990 static const MonitorDef monitor_defs[] = {
2993 #define SEG(name, seg) \
2994 { name, offsetof(CPUX86State, segs[seg].selector), NULL, MD_I32 },\
2995 { name ".base", offsetof(CPUX86State, segs[seg].base) },\
2996 { name ".limit", offsetof(CPUX86State, segs[seg].limit), NULL, MD_I32 },
2998 { "eax", offsetof(CPUX86State, regs[0]) },
2999 { "ecx", offsetof(CPUX86State, regs[1]) },
3000 { "edx", offsetof(CPUX86State, regs[2]) },
3001 { "ebx", offsetof(CPUX86State, regs[3]) },
3002 { "esp|sp", offsetof(CPUX86State, regs[4]) },
3003 { "ebp|fp", offsetof(CPUX86State, regs[5]) },
3004 { "esi", offsetof(CPUX86State, regs[6]) },
3005 { "edi", offsetof(CPUX86State, regs[7]) },
3006 #ifdef TARGET_X86_64
3007 { "r8", offsetof(CPUX86State, regs[8]) },
3008 { "r9", offsetof(CPUX86State, regs[9]) },
3009 { "r10", offsetof(CPUX86State, regs[10]) },
3010 { "r11", offsetof(CPUX86State, regs[11]) },
3011 { "r12", offsetof(CPUX86State, regs[12]) },
3012 { "r13", offsetof(CPUX86State, regs[13]) },
3013 { "r14", offsetof(CPUX86State, regs[14]) },
3014 { "r15", offsetof(CPUX86State, regs[15]) },
3016 { "eflags", offsetof(CPUX86State, eflags) },
3017 { "eip", offsetof(CPUX86State, eip) },
3024 { "pc", 0, monitor_get_pc, },
3025 #elif defined(TARGET_PPC)
3026 /* General purpose registers */
3027 { "r0", offsetof(CPUPPCState, gpr[0]) },
3028 { "r1", offsetof(CPUPPCState, gpr[1]) },
3029 { "r2", offsetof(CPUPPCState, gpr[2]) },
3030 { "r3", offsetof(CPUPPCState, gpr[3]) },
3031 { "r4", offsetof(CPUPPCState, gpr[4]) },
3032 { "r5", offsetof(CPUPPCState, gpr[5]) },
3033 { "r6", offsetof(CPUPPCState, gpr[6]) },
3034 { "r7", offsetof(CPUPPCState, gpr[7]) },
3035 { "r8", offsetof(CPUPPCState, gpr[8]) },
3036 { "r9", offsetof(CPUPPCState, gpr[9]) },
3037 { "r10", offsetof(CPUPPCState, gpr[10]) },
3038 { "r11", offsetof(CPUPPCState, gpr[11]) },
3039 { "r12", offsetof(CPUPPCState, gpr[12]) },
3040 { "r13", offsetof(CPUPPCState, gpr[13]) },
3041 { "r14", offsetof(CPUPPCState, gpr[14]) },
3042 { "r15", offsetof(CPUPPCState, gpr[15]) },
3043 { "r16", offsetof(CPUPPCState, gpr[16]) },
3044 { "r17", offsetof(CPUPPCState, gpr[17]) },
3045 { "r18", offsetof(CPUPPCState, gpr[18]) },
3046 { "r19", offsetof(CPUPPCState, gpr[19]) },
3047 { "r20", offsetof(CPUPPCState, gpr[20]) },
3048 { "r21", offsetof(CPUPPCState, gpr[21]) },
3049 { "r22", offsetof(CPUPPCState, gpr[22]) },
3050 { "r23", offsetof(CPUPPCState, gpr[23]) },
3051 { "r24", offsetof(CPUPPCState, gpr[24]) },
3052 { "r25", offsetof(CPUPPCState, gpr[25]) },
3053 { "r26", offsetof(CPUPPCState, gpr[26]) },
3054 { "r27", offsetof(CPUPPCState, gpr[27]) },
3055 { "r28", offsetof(CPUPPCState, gpr[28]) },
3056 { "r29", offsetof(CPUPPCState, gpr[29]) },
3057 { "r30", offsetof(CPUPPCState, gpr[30]) },
3058 { "r31", offsetof(CPUPPCState, gpr[31]) },
3059 /* Floating point registers */
3060 { "f0", offsetof(CPUPPCState, fpr[0]) },
3061 { "f1", offsetof(CPUPPCState, fpr[1]) },
3062 { "f2", offsetof(CPUPPCState, fpr[2]) },
3063 { "f3", offsetof(CPUPPCState, fpr[3]) },
3064 { "f4", offsetof(CPUPPCState, fpr[4]) },
3065 { "f5", offsetof(CPUPPCState, fpr[5]) },
3066 { "f6", offsetof(CPUPPCState, fpr[6]) },
3067 { "f7", offsetof(CPUPPCState, fpr[7]) },
3068 { "f8", offsetof(CPUPPCState, fpr[8]) },
3069 { "f9", offsetof(CPUPPCState, fpr[9]) },
3070 { "f10", offsetof(CPUPPCState, fpr[10]) },
3071 { "f11", offsetof(CPUPPCState, fpr[11]) },
3072 { "f12", offsetof(CPUPPCState, fpr[12]) },
3073 { "f13", offsetof(CPUPPCState, fpr[13]) },
3074 { "f14", offsetof(CPUPPCState, fpr[14]) },
3075 { "f15", offsetof(CPUPPCState, fpr[15]) },
3076 { "f16", offsetof(CPUPPCState, fpr[16]) },
3077 { "f17", offsetof(CPUPPCState, fpr[17]) },
3078 { "f18", offsetof(CPUPPCState, fpr[18]) },
3079 { "f19", offsetof(CPUPPCState, fpr[19]) },
3080 { "f20", offsetof(CPUPPCState, fpr[20]) },
3081 { "f21", offsetof(CPUPPCState, fpr[21]) },
3082 { "f22", offsetof(CPUPPCState, fpr[22]) },
3083 { "f23", offsetof(CPUPPCState, fpr[23]) },
3084 { "f24", offsetof(CPUPPCState, fpr[24]) },
3085 { "f25", offsetof(CPUPPCState, fpr[25]) },
3086 { "f26", offsetof(CPUPPCState, fpr[26]) },
3087 { "f27", offsetof(CPUPPCState, fpr[27]) },
3088 { "f28", offsetof(CPUPPCState, fpr[28]) },
3089 { "f29", offsetof(CPUPPCState, fpr[29]) },
3090 { "f30", offsetof(CPUPPCState, fpr[30]) },
3091 { "f31", offsetof(CPUPPCState, fpr[31]) },
3092 { "fpscr", offsetof(CPUPPCState, fpscr) },
3093 /* Next instruction pointer */
3094 { "nip|pc", offsetof(CPUPPCState, nip) },
3095 { "lr", offsetof(CPUPPCState, lr) },
3096 { "ctr", offsetof(CPUPPCState, ctr) },
3097 { "decr", 0, &monitor_get_decr, },
3098 { "ccr", 0, &monitor_get_ccr, },
3099 /* Machine state register */
3100 { "msr", 0, &monitor_get_msr, },
3101 { "xer", 0, &monitor_get_xer, },
3102 { "tbu", 0, &monitor_get_tbu, },
3103 { "tbl", 0, &monitor_get_tbl, },
3104 /* Segment registers */
3105 { "sdr1", offsetof(CPUPPCState, spr[SPR_SDR1]) },
3106 { "sr0", offsetof(CPUPPCState, sr[0]) },
3107 { "sr1", offsetof(CPUPPCState, sr[1]) },
3108 { "sr2", offsetof(CPUPPCState, sr[2]) },
3109 { "sr3", offsetof(CPUPPCState, sr[3]) },
3110 { "sr4", offsetof(CPUPPCState, sr[4]) },
3111 { "sr5", offsetof(CPUPPCState, sr[5]) },
3112 { "sr6", offsetof(CPUPPCState, sr[6]) },
3113 { "sr7", offsetof(CPUPPCState, sr[7]) },
3114 { "sr8", offsetof(CPUPPCState, sr[8]) },
3115 { "sr9", offsetof(CPUPPCState, sr[9]) },
3116 { "sr10", offsetof(CPUPPCState, sr[10]) },
3117 { "sr11", offsetof(CPUPPCState, sr[11]) },
3118 { "sr12", offsetof(CPUPPCState, sr[12]) },
3119 { "sr13", offsetof(CPUPPCState, sr[13]) },
3120 { "sr14", offsetof(CPUPPCState, sr[14]) },
3121 { "sr15", offsetof(CPUPPCState, sr[15]) },
3122 /* Too lazy to put BATs... */
3123 { "pvr", offsetof(CPUPPCState, spr[SPR_PVR]) },
3125 { "srr0", offsetof(CPUPPCState, spr[SPR_SRR0]) },
3126 { "srr1", offsetof(CPUPPCState, spr[SPR_SRR1]) },
3127 { "dar", offsetof(CPUPPCState, spr[SPR_DAR]) },
3128 { "dsisr", offsetof(CPUPPCState, spr[SPR_DSISR]) },
3129 { "cfar", offsetof(CPUPPCState, spr[SPR_CFAR]) },
3130 { "sprg0", offsetof(CPUPPCState, spr[SPR_SPRG0]) },
3131 { "sprg1", offsetof(CPUPPCState, spr[SPR_SPRG1]) },
3132 { "sprg2", offsetof(CPUPPCState, spr[SPR_SPRG2]) },
3133 { "sprg3", offsetof(CPUPPCState, spr[SPR_SPRG3]) },
3134 { "sprg4", offsetof(CPUPPCState, spr[SPR_SPRG4]) },
3135 { "sprg5", offsetof(CPUPPCState, spr[SPR_SPRG5]) },
3136 { "sprg6", offsetof(CPUPPCState, spr[SPR_SPRG6]) },
3137 { "sprg7", offsetof(CPUPPCState, spr[SPR_SPRG7]) },
3138 { "pid", offsetof(CPUPPCState, spr[SPR_BOOKE_PID]) },
3139 { "csrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR0]) },
3140 { "csrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_CSRR1]) },
3141 { "esr", offsetof(CPUPPCState, spr[SPR_BOOKE_ESR]) },
3142 { "dear", offsetof(CPUPPCState, spr[SPR_BOOKE_DEAR]) },
3143 { "mcsr", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSR]) },
3144 { "tsr", offsetof(CPUPPCState, spr[SPR_BOOKE_TSR]) },
3145 { "tcr", offsetof(CPUPPCState, spr[SPR_BOOKE_TCR]) },
3146 { "vrsave", offsetof(CPUPPCState, spr[SPR_VRSAVE]) },
3147 { "pir", offsetof(CPUPPCState, spr[SPR_BOOKE_PIR]) },
3148 { "mcsrr0", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR0]) },
3149 { "mcsrr1", offsetof(CPUPPCState, spr[SPR_BOOKE_MCSRR1]) },
3150 { "decar", offsetof(CPUPPCState, spr[SPR_BOOKE_DECAR]) },
3151 { "ivpr", offsetof(CPUPPCState, spr[SPR_BOOKE_IVPR]) },
3152 { "epcr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPCR]) },
3153 { "sprg8", offsetof(CPUPPCState, spr[SPR_BOOKE_SPRG8]) },
3154 { "ivor0", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR0]) },
3155 { "ivor1", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR1]) },
3156 { "ivor2", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR2]) },
3157 { "ivor3", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR3]) },
3158 { "ivor4", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR4]) },
3159 { "ivor5", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR5]) },
3160 { "ivor6", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR6]) },
3161 { "ivor7", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR7]) },
3162 { "ivor8", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR8]) },
3163 { "ivor9", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR9]) },
3164 { "ivor10", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR10]) },
3165 { "ivor11", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR11]) },
3166 { "ivor12", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR12]) },
3167 { "ivor13", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR13]) },
3168 { "ivor14", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR14]) },
3169 { "ivor15", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR15]) },
3170 { "ivor32", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR32]) },
3171 { "ivor33", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR33]) },
3172 { "ivor34", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR34]) },
3173 { "ivor35", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR35]) },
3174 { "ivor36", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR36]) },
3175 { "ivor37", offsetof(CPUPPCState, spr[SPR_BOOKE_IVOR37]) },
3176 { "mas0", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS0]) },
3177 { "mas1", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS1]) },
3178 { "mas2", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS2]) },
3179 { "mas3", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS3]) },
3180 { "mas4", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS4]) },
3181 { "mas6", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS6]) },
3182 { "mas7", offsetof(CPUPPCState, spr[SPR_BOOKE_MAS7]) },
3183 { "mmucfg", offsetof(CPUPPCState, spr[SPR_MMUCFG]) },
3184 { "tlb0cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB0CFG]) },
3185 { "tlb1cfg", offsetof(CPUPPCState, spr[SPR_BOOKE_TLB1CFG]) },
3186 { "epr", offsetof(CPUPPCState, spr[SPR_BOOKE_EPR]) },
3187 { "eplc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPLC]) },
3188 { "epsc", offsetof(CPUPPCState, spr[SPR_BOOKE_EPSC]) },
3189 { "svr", offsetof(CPUPPCState, spr[SPR_E500_SVR]) },
3190 { "mcar", offsetof(CPUPPCState, spr[SPR_Exxx_MCAR]) },
3191 { "pid1", offsetof(CPUPPCState, spr[SPR_BOOKE_PID1]) },
3192 { "pid2", offsetof(CPUPPCState, spr[SPR_BOOKE_PID2]) },
3193 { "hid0", offsetof(CPUPPCState, spr[SPR_HID0]) },
3195 #elif defined(TARGET_SPARC)
3196 { "g0", offsetof(CPUSPARCState, gregs[0]) },
3197 { "g1", offsetof(CPUSPARCState, gregs[1]) },
3198 { "g2", offsetof(CPUSPARCState, gregs[2]) },
3199 { "g3", offsetof(CPUSPARCState, gregs[3]) },
3200 { "g4", offsetof(CPUSPARCState, gregs[4]) },
3201 { "g5", offsetof(CPUSPARCState, gregs[5]) },
3202 { "g6", offsetof(CPUSPARCState, gregs[6]) },
3203 { "g7", offsetof(CPUSPARCState, gregs[7]) },
3204 { "o0", 0, monitor_get_reg },
3205 { "o1", 1, monitor_get_reg },
3206 { "o2", 2, monitor_get_reg },
3207 { "o3", 3, monitor_get_reg },
3208 { "o4", 4, monitor_get_reg },
3209 { "o5", 5, monitor_get_reg },
3210 { "o6", 6, monitor_get_reg },
3211 { "o7", 7, monitor_get_reg },
3212 { "l0", 8, monitor_get_reg },
3213 { "l1", 9, monitor_get_reg },
3214 { "l2", 10, monitor_get_reg },
3215 { "l3", 11, monitor_get_reg },
3216 { "l4", 12, monitor_get_reg },
3217 { "l5", 13, monitor_get_reg },
3218 { "l6", 14, monitor_get_reg },
3219 { "l7", 15, monitor_get_reg },
3220 { "i0", 16, monitor_get_reg },
3221 { "i1", 17, monitor_get_reg },
3222 { "i2", 18, monitor_get_reg },
3223 { "i3", 19, monitor_get_reg },
3224 { "i4", 20, monitor_get_reg },
3225 { "i5", 21, monitor_get_reg },
3226 { "i6", 22, monitor_get_reg },
3227 { "i7", 23, monitor_get_reg },
3228 { "pc", offsetof(CPUSPARCState, pc) },
3229 { "npc", offsetof(CPUSPARCState, npc) },
3230 { "y", offsetof(CPUSPARCState, y) },
3231 #ifndef TARGET_SPARC64
3232 { "psr", 0, &monitor_get_psr, },
3233 { "wim", offsetof(CPUSPARCState, wim) },
3235 { "tbr", offsetof(CPUSPARCState, tbr) },
3236 { "fsr", offsetof(CPUSPARCState, fsr) },
3237 { "f0", offsetof(CPUSPARCState, fpr[0].l.upper) },
3238 { "f1", offsetof(CPUSPARCState, fpr[0].l.lower) },
3239 { "f2", offsetof(CPUSPARCState, fpr[1].l.upper) },
3240 { "f3", offsetof(CPUSPARCState, fpr[1].l.lower) },
3241 { "f4", offsetof(CPUSPARCState, fpr[2].l.upper) },
3242 { "f5", offsetof(CPUSPARCState, fpr[2].l.lower) },
3243 { "f6", offsetof(CPUSPARCState, fpr[3].l.upper) },
3244 { "f7", offsetof(CPUSPARCState, fpr[3].l.lower) },
3245 { "f8", offsetof(CPUSPARCState, fpr[4].l.upper) },
3246 { "f9", offsetof(CPUSPARCState, fpr[4].l.lower) },
3247 { "f10", offsetof(CPUSPARCState, fpr[5].l.upper) },
3248 { "f11", offsetof(CPUSPARCState, fpr[5].l.lower) },
3249 { "f12", offsetof(CPUSPARCState, fpr[6].l.upper) },
3250 { "f13", offsetof(CPUSPARCState, fpr[6].l.lower) },
3251 { "f14", offsetof(CPUSPARCState, fpr[7].l.upper) },
3252 { "f15", offsetof(CPUSPARCState, fpr[7].l.lower) },
3253 { "f16", offsetof(CPUSPARCState, fpr[8].l.upper) },
3254 { "f17", offsetof(CPUSPARCState, fpr[8].l.lower) },
3255 { "f18", offsetof(CPUSPARCState, fpr[9].l.upper) },
3256 { "f19", offsetof(CPUSPARCState, fpr[9].l.lower) },
3257 { "f20", offsetof(CPUSPARCState, fpr[10].l.upper) },
3258 { "f21", offsetof(CPUSPARCState, fpr[10].l.lower) },
3259 { "f22", offsetof(CPUSPARCState, fpr[11].l.upper) },
3260 { "f23", offsetof(CPUSPARCState, fpr[11].l.lower) },
3261 { "f24", offsetof(CPUSPARCState, fpr[12].l.upper) },
3262 { "f25", offsetof(CPUSPARCState, fpr[12].l.lower) },
3263 { "f26", offsetof(CPUSPARCState, fpr[13].l.upper) },
3264 { "f27", offsetof(CPUSPARCState, fpr[13].l.lower) },
3265 { "f28", offsetof(CPUSPARCState, fpr[14].l.upper) },
3266 { "f29", offsetof(CPUSPARCState, fpr[14].l.lower) },
3267 { "f30", offsetof(CPUSPARCState, fpr[15].l.upper) },
3268 { "f31", offsetof(CPUSPARCState, fpr[15].l.lower) },
3269 #ifdef TARGET_SPARC64
3270 { "f32", offsetof(CPUSPARCState, fpr[16]) },
3271 { "f34", offsetof(CPUSPARCState, fpr[17]) },
3272 { "f36", offsetof(CPUSPARCState, fpr[18]) },
3273 { "f38", offsetof(CPUSPARCState, fpr[19]) },
3274 { "f40", offsetof(CPUSPARCState, fpr[20]) },
3275 { "f42", offsetof(CPUSPARCState, fpr[21]) },
3276 { "f44", offsetof(CPUSPARCState, fpr[22]) },
3277 { "f46", offsetof(CPUSPARCState, fpr[23]) },
3278 { "f48", offsetof(CPUSPARCState, fpr[24]) },
3279 { "f50", offsetof(CPUSPARCState, fpr[25]) },
3280 { "f52", offsetof(CPUSPARCState, fpr[26]) },
3281 { "f54", offsetof(CPUSPARCState, fpr[27]) },
3282 { "f56", offsetof(CPUSPARCState, fpr[28]) },
3283 { "f58", offsetof(CPUSPARCState, fpr[29]) },
3284 { "f60", offsetof(CPUSPARCState, fpr[30]) },
3285 { "f62", offsetof(CPUSPARCState, fpr[31]) },
3286 { "asi", offsetof(CPUSPARCState, asi) },
3287 { "pstate", offsetof(CPUSPARCState, pstate) },
3288 { "cansave", offsetof(CPUSPARCState, cansave) },
3289 { "canrestore", offsetof(CPUSPARCState, canrestore) },
3290 { "otherwin", offsetof(CPUSPARCState, otherwin) },
3291 { "wstate", offsetof(CPUSPARCState, wstate) },
3292 { "cleanwin", offsetof(CPUSPARCState, cleanwin) },
3293 { "fprs", offsetof(CPUSPARCState, fprs) },
3299 static void GCC_FMT_ATTR(2, 3) QEMU_NORETURN
3300 expr_error(Monitor *mon, const char *fmt, ...)
3304 monitor_vprintf(mon, fmt, ap);
3305 monitor_printf(mon, "\n");
3307 siglongjmp(expr_env, 1);
3310 /* return 0 if OK, -1 if not found */
3311 static int get_monitor_def(target_long *pval, const char *name)
3313 const MonitorDef *md;
3316 for(md = monitor_defs; md->name != NULL; md++) {
3317 if (compare_cmd(name, md->name)) {
3318 if (md->get_value) {
3319 *pval = md->get_value(md, md->offset);
3321 CPUArchState *env = mon_get_cpu();
3322 ptr = (uint8_t *)env + md->offset;
3325 *pval = *(int32_t *)ptr;
3328 *pval = *(target_long *)ptr;
3341 static void next(void)
3345 while (qemu_isspace(*pch))
3350 static int64_t expr_sum(Monitor *mon);
3352 static int64_t expr_unary(Monitor *mon)
3361 n = expr_unary(mon);
3365 n = -expr_unary(mon);
3369 n = ~expr_unary(mon);
3375 expr_error(mon, "')' expected");
3382 expr_error(mon, "character constant expected");
3386 expr_error(mon, "missing terminating \' character");
3396 while ((*pch >= 'a' && *pch <= 'z') ||
3397 (*pch >= 'A' && *pch <= 'Z') ||
3398 (*pch >= '0' && *pch <= '9') ||
3399 *pch == '_' || *pch == '.') {
3400 if ((q - buf) < sizeof(buf) - 1)
3404 while (qemu_isspace(*pch))
3407 ret = get_monitor_def(®, buf);
3409 expr_error(mon, "unknown register");
3414 expr_error(mon, "unexpected end of expression");
3419 n = strtoull(pch, &p, 0);
3420 if (errno == ERANGE) {
3421 expr_error(mon, "number too large");
3424 expr_error(mon, "invalid char '%c' in expression", *p);
3427 while (qemu_isspace(*pch))
3435 static int64_t expr_prod(Monitor *mon)
3440 val = expr_unary(mon);
3443 if (op != '*' && op != '/' && op != '%')
3446 val2 = expr_unary(mon);
3455 expr_error(mon, "division by zero");
3466 static int64_t expr_logic(Monitor *mon)
3471 val = expr_prod(mon);
3474 if (op != '&' && op != '|' && op != '^')
3477 val2 = expr_prod(mon);
3494 static int64_t expr_sum(Monitor *mon)
3499 val = expr_logic(mon);
3502 if (op != '+' && op != '-')
3505 val2 = expr_logic(mon);
3514 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3517 if (sigsetjmp(expr_env, 0)) {
3521 while (qemu_isspace(*pch))
3523 *pval = expr_sum(mon);
3528 static int get_double(Monitor *mon, double *pval, const char **pp)
3530 const char *p = *pp;
3534 d = strtod(p, &tailp);
3536 monitor_printf(mon, "Number expected\n");
3539 if (d != d || d - d != 0) {
3540 /* NaN or infinity */
3541 monitor_printf(mon, "Bad number\n");
3550 * Store the command-name in cmdname, and return a pointer to
3551 * the remaining of the command string.
3553 static const char *get_command_name(const char *cmdline,
3554 char *cmdname, size_t nlen)
3557 const char *p, *pstart;
3560 while (qemu_isspace(*p))
3565 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3570 memcpy(cmdname, pstart, len);
3571 cmdname[len] = '\0';
3576 * Read key of 'type' into 'key' and return the current
3579 static char *key_get_info(const char *type, char **key)
3587 p = strchr(type, ':');
3594 str = g_malloc(len + 1);
3595 memcpy(str, type, len);
3602 static int default_fmt_format = 'x';
3603 static int default_fmt_size = 4;
3605 static int is_valid_option(const char *c, const char *typestr)
3613 typestr = strstr(typestr, option);
3614 return (typestr != NULL);
3617 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3618 const char *cmdname)
3620 const mon_cmd_t *cmd;
3622 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3623 if (compare_cmd(cmdname, cmd->name)) {
3631 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3633 return search_dispatch_table(qmp_cmds, cmdname);
3637 * Parse command name from @cmdp according to command table @table.
3638 * If blank, return NULL.
3639 * Else, if no valid command can be found, report to @mon, and return
3641 * Else, change @cmdp to point right behind the name, and return its
3642 * command table entry.
3643 * Do not assume the return value points into @table! It doesn't when
3644 * the command is found in a sub-command table.
3646 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3651 const mon_cmd_t *cmd;
3654 /* extract the command name */
3655 p = get_command_name(*cmdp, cmdname, sizeof(cmdname));
3659 cmd = search_dispatch_table(table, cmdname);
3661 monitor_printf(mon, "unknown command: '%.*s'\n",
3662 (int)(p - *cmdp), *cmdp);
3666 /* filter out following useless space */
3667 while (qemu_isspace(*p)) {
3672 /* search sub command */
3673 if (cmd->sub_table != NULL && *p != '\0') {
3674 return monitor_parse_command(mon, cmdp, cmd->sub_table);
3681 * Parse arguments for @cmd.
3682 * If it can't be parsed, report to @mon, and return NULL.
3683 * Else, insert command arguments into a QDict, and return it.
3684 * Note: On success, caller has to free the QDict structure.
3687 static QDict *monitor_parse_arguments(Monitor *mon,
3689 const mon_cmd_t *cmd)
3691 const char *typestr;
3694 const char *p = *endp;
3696 QDict *qdict = qdict_new();
3698 /* parse the parameters */
3699 typestr = cmd->args_type;
3701 typestr = key_get_info(typestr, &key);
3713 while (qemu_isspace(*p))
3715 if (*typestr == '?') {
3718 /* no optional string: NULL argument */
3722 ret = get_str(buf, sizeof(buf), &p);
3726 monitor_printf(mon, "%s: filename expected\n",
3730 monitor_printf(mon, "%s: block device name expected\n",
3734 monitor_printf(mon, "%s: string expected\n", cmd->name);
3739 qdict_put(qdict, key, qstring_from_str(buf));
3744 QemuOptsList *opts_list;
3747 opts_list = qemu_find_opts(key);
3748 if (!opts_list || opts_list->desc->name) {
3751 while (qemu_isspace(*p)) {
3756 if (get_str(buf, sizeof(buf), &p) < 0) {
3759 opts = qemu_opts_parse(opts_list, buf, 1);
3763 qemu_opts_to_qdict(opts, qdict);
3764 qemu_opts_del(opts);
3769 int count, format, size;
3771 while (qemu_isspace(*p))
3777 if (qemu_isdigit(*p)) {
3779 while (qemu_isdigit(*p)) {
3780 count = count * 10 + (*p - '0');
3818 if (*p != '\0' && !qemu_isspace(*p)) {
3819 monitor_printf(mon, "invalid char in format: '%c'\n",
3824 format = default_fmt_format;
3825 if (format != 'i') {
3826 /* for 'i', not specifying a size gives -1 as size */
3828 size = default_fmt_size;
3829 default_fmt_size = size;
3831 default_fmt_format = format;
3834 format = default_fmt_format;
3835 if (format != 'i') {
3836 size = default_fmt_size;
3841 qdict_put(qdict, "count", qint_from_int(count));
3842 qdict_put(qdict, "format", qint_from_int(format));
3843 qdict_put(qdict, "size", qint_from_int(size));
3852 while (qemu_isspace(*p))
3854 if (*typestr == '?' || *typestr == '.') {
3855 if (*typestr == '?') {
3863 while (qemu_isspace(*p))
3872 if (get_expr(mon, &val, &p))
3874 /* Check if 'i' is greater than 32-bit */
3875 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3876 monitor_printf(mon, "\'%s\' has failed: ", cmd->name);
3877 monitor_printf(mon, "integer is for 32-bit values\n");
3879 } else if (c == 'M') {
3881 monitor_printf(mon, "enter a positive value\n");
3886 qdict_put(qdict, key, qint_from_int(val));
3894 while (qemu_isspace(*p)) {
3897 if (*typestr == '?') {
3903 val = strtosz(p, &end);
3905 monitor_printf(mon, "invalid size\n");
3908 qdict_put(qdict, key, qint_from_int(val));
3916 while (qemu_isspace(*p))
3918 if (*typestr == '?') {
3924 if (get_double(mon, &val, &p) < 0) {
3927 if (p[0] && p[1] == 's') {
3930 val /= 1e3; p += 2; break;
3932 val /= 1e6; p += 2; break;
3934 val /= 1e9; p += 2; break;
3937 if (*p && !qemu_isspace(*p)) {
3938 monitor_printf(mon, "Unknown unit suffix\n");
3941 qdict_put(qdict, key, qfloat_from_double(val));
3949 while (qemu_isspace(*p)) {
3953 while (qemu_isgraph(*p)) {
3956 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3958 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3961 monitor_printf(mon, "Expected 'on' or 'off'\n");
3964 qdict_put(qdict, key, qbool_from_bool(val));
3969 const char *tmp = p;
3976 while (qemu_isspace(*p))
3981 if(!is_valid_option(p, typestr)) {
3983 monitor_printf(mon, "%s: unsupported option -%c\n",
3995 qdict_put(qdict, key, qbool_from_bool(true));
4002 /* package all remaining string */
4005 while (qemu_isspace(*p)) {
4008 if (*typestr == '?') {
4011 /* no remaining string: NULL argument */
4017 monitor_printf(mon, "%s: string expected\n",
4021 qdict_put(qdict, key, qstring_from_str(p));
4027 monitor_printf(mon, "%s: unknown type '%c'\n", cmd->name, c);
4033 /* check that all arguments were parsed */
4034 while (qemu_isspace(*p))
4037 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
4050 void monitor_set_error(Monitor *mon, QError *qerror)
4052 /* report only the first error */
4054 mon->error = qerror;
4060 static void handle_hmp_command(Monitor *mon, const char *cmdline)
4063 const mon_cmd_t *cmd;
4065 cmd = monitor_parse_command(mon, &cmdline, mon->cmd_table);
4070 qdict = monitor_parse_arguments(mon, &cmdline, cmd);
4072 monitor_printf(mon, "Try \"help %s\" for more information\n",
4077 cmd->mhandler.cmd(mon, qdict);
4081 static void cmd_completion(Monitor *mon, const char *name, const char *list)
4083 const char *p, *pstart;
4092 p = pstart + strlen(pstart);
4094 if (len > sizeof(cmd) - 2)
4095 len = sizeof(cmd) - 2;
4096 memcpy(cmd, pstart, len);
4098 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
4099 readline_add_completion(mon->rs, cmd);
4107 static void file_completion(Monitor *mon, const char *input)
4112 char file[1024], file_prefix[1024];
4116 p = strrchr(input, '/');
4119 pstrcpy(file_prefix, sizeof(file_prefix), input);
4120 pstrcpy(path, sizeof(path), ".");
4122 input_path_len = p - input + 1;
4123 memcpy(path, input, input_path_len);
4124 if (input_path_len > sizeof(path) - 1)
4125 input_path_len = sizeof(path) - 1;
4126 path[input_path_len] = '\0';
4127 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
4130 ffs = opendir(path);
4139 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
4143 if (strstart(d->d_name, file_prefix, NULL)) {
4144 memcpy(file, input, input_path_len);
4145 if (input_path_len < sizeof(file))
4146 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
4148 /* stat the file to find out if it's a directory.
4149 * In that case add a slash to speed up typing long paths
4151 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
4152 pstrcat(file, sizeof(file), "/");
4154 readline_add_completion(mon->rs, file);
4160 static const char *next_arg_type(const char *typestr)
4162 const char *p = strchr(typestr, ':');
4163 return (p != NULL ? ++p : typestr);
4166 static void add_completion_option(ReadLineState *rs, const char *str,
4169 if (!str || !option) {
4172 if (!strncmp(option, str, strlen(str))) {
4173 readline_add_completion(rs, option);
4177 void chardev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4180 ChardevBackendInfoList *list, *start;
4186 readline_set_completion_index(rs, len);
4188 start = list = qmp_query_chardev_backends(NULL);
4190 const char *chr_name = list->value->name;
4192 if (!strncmp(chr_name, str, len)) {
4193 readline_add_completion(rs, chr_name);
4197 qapi_free_ChardevBackendInfoList(start);
4200 void netdev_add_completion(ReadLineState *rs, int nb_args, const char *str)
4209 readline_set_completion_index(rs, len);
4210 for (i = 0; NetClientOptionsKind_lookup[i]; i++) {
4211 add_completion_option(rs, str, NetClientOptionsKind_lookup[i]);
4215 void device_add_completion(ReadLineState *rs, int nb_args, const char *str)
4225 readline_set_completion_index(rs, len);
4226 list = elt = object_class_get_list(TYPE_DEVICE, false);
4229 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
4231 name = object_class_get_name(OBJECT_CLASS(dc));
4233 if (!dc->cannot_instantiate_with_device_add_yet
4234 && !strncmp(name, str, len)) {
4235 readline_add_completion(rs, name);
4242 void object_add_completion(ReadLineState *rs, int nb_args, const char *str)
4252 readline_set_completion_index(rs, len);
4253 list = elt = object_class_get_list(TYPE_USER_CREATABLE, false);
4257 name = object_class_get_name(OBJECT_CLASS(elt->data));
4258 if (!strncmp(name, str, len) && strcmp(name, TYPE_USER_CREATABLE)) {
4259 readline_add_completion(rs, name);
4266 static void peripheral_device_del_completion(ReadLineState *rs,
4267 const char *str, size_t len)
4269 Object *peripheral = container_get(qdev_get_machine(), "/peripheral");
4270 GSList *list, *item;
4272 list = qdev_build_hotpluggable_device_list(peripheral);
4277 for (item = list; item; item = g_slist_next(item)) {
4278 DeviceState *dev = item->data;
4280 if (dev->id && !strncmp(str, dev->id, len)) {
4281 readline_add_completion(rs, dev->id);
4288 void chardev_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4291 ChardevInfoList *list, *start;
4297 readline_set_completion_index(rs, len);
4299 start = list = qmp_query_chardev(NULL);
4301 ChardevInfo *chr = list->value;
4303 if (!strncmp(chr->label, str, len)) {
4304 readline_add_completion(rs, chr->label);
4308 qapi_free_ChardevInfoList(start);
4311 static void ringbuf_completion(ReadLineState *rs, const char *str)
4314 ChardevInfoList *list, *start;
4317 readline_set_completion_index(rs, len);
4319 start = list = qmp_query_chardev(NULL);
4321 ChardevInfo *chr_info = list->value;
4323 if (!strncmp(chr_info->label, str, len)) {
4324 CharDriverState *chr = qemu_chr_find(chr_info->label);
4325 if (chr && chr_is_ringbuf(chr)) {
4326 readline_add_completion(rs, chr_info->label);
4331 qapi_free_ChardevInfoList(start);
4334 void ringbuf_write_completion(ReadLineState *rs, int nb_args, const char *str)
4339 ringbuf_completion(rs, str);
4342 void device_del_completion(ReadLineState *rs, int nb_args, const char *str)
4351 readline_set_completion_index(rs, len);
4352 peripheral_device_del_completion(rs, str, len);
4355 void object_del_completion(ReadLineState *rs, int nb_args, const char *str)
4357 ObjectPropertyInfoList *list, *start;
4364 readline_set_completion_index(rs, len);
4366 start = list = qmp_qom_list("/objects", NULL);
4368 ObjectPropertyInfo *info = list->value;
4370 if (!strncmp(info->type, "child<", 5)
4371 && !strncmp(info->name, str, len)) {
4372 readline_add_completion(rs, info->name);
4376 qapi_free_ObjectPropertyInfoList(start);
4379 void sendkey_completion(ReadLineState *rs, int nb_args, const char *str)
4388 sep = strrchr(str, '-');
4393 readline_set_completion_index(rs, len);
4394 for (i = 0; i < Q_KEY_CODE_MAX; i++) {
4395 if (!strncmp(str, QKeyCode_lookup[i], len)) {
4396 readline_add_completion(rs, QKeyCode_lookup[i]);
4401 void set_link_completion(ReadLineState *rs, int nb_args, const char *str)
4406 readline_set_completion_index(rs, len);
4408 NetClientState *ncs[MAX_QUEUE_NUM];
4410 count = qemu_find_net_clients_except(NULL, ncs,
4411 NET_CLIENT_OPTIONS_KIND_NONE,
4413 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4414 const char *name = ncs[i]->name;
4415 if (!strncmp(str, name, len)) {
4416 readline_add_completion(rs, name);
4419 } else if (nb_args == 3) {
4420 add_completion_option(rs, str, "on");
4421 add_completion_option(rs, str, "off");
4425 void netdev_del_completion(ReadLineState *rs, int nb_args, const char *str)
4428 NetClientState *ncs[MAX_QUEUE_NUM];
4435 readline_set_completion_index(rs, len);
4436 count = qemu_find_net_clients_except(NULL, ncs, NET_CLIENT_OPTIONS_KIND_NIC,
4438 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4440 const char *name = ncs[i]->name;
4441 if (strncmp(str, name, len)) {
4444 opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), name);
4446 readline_add_completion(rs, name);
4451 void watchdog_action_completion(ReadLineState *rs, int nb_args, const char *str)
4458 readline_set_completion_index(rs, strlen(str));
4459 for (i = 0; WatchdogExpirationAction_lookup[i]; i++) {
4460 add_completion_option(rs, str, WatchdogExpirationAction_lookup[i]);
4464 void migrate_set_capability_completion(ReadLineState *rs, int nb_args,
4470 readline_set_completion_index(rs, len);
4473 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
4474 const char *name = MigrationCapability_lookup[i];
4475 if (!strncmp(str, name, len)) {
4476 readline_add_completion(rs, name);
4479 } else if (nb_args == 3) {
4480 add_completion_option(rs, str, "on");
4481 add_completion_option(rs, str, "off");
4485 void migrate_set_parameter_completion(ReadLineState *rs, int nb_args,
4491 readline_set_completion_index(rs, len);
4494 for (i = 0; i < MIGRATION_PARAMETER_MAX; i++) {
4495 const char *name = MigrationParameter_lookup[i];
4496 if (!strncmp(str, name, len)) {
4497 readline_add_completion(rs, name);
4503 void host_net_add_completion(ReadLineState *rs, int nb_args, const char *str)
4511 readline_set_completion_index(rs, len);
4512 for (i = 0; host_net_devices[i]; i++) {
4513 if (!strncmp(host_net_devices[i], str, len)) {
4514 readline_add_completion(rs, host_net_devices[i]);
4519 void host_net_remove_completion(ReadLineState *rs, int nb_args, const char *str)
4521 NetClientState *ncs[MAX_QUEUE_NUM];
4525 readline_set_completion_index(rs, len);
4527 count = qemu_find_net_clients_except(NULL, ncs,
4528 NET_CLIENT_OPTIONS_KIND_NONE,
4530 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4534 if (net_hub_id_for_client(ncs[i], &id)) {
4537 snprintf(name, sizeof(name), "%d", id);
4538 if (!strncmp(str, name, len)) {
4539 readline_add_completion(rs, name);
4543 } else if (nb_args == 3) {
4544 count = qemu_find_net_clients_except(NULL, ncs,
4545 NET_CLIENT_OPTIONS_KIND_NIC,
4547 for (i = 0; i < MIN(count, MAX_QUEUE_NUM); i++) {
4551 if (ncs[i]->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT ||
4552 net_hub_id_for_client(ncs[i], &id)) {
4555 name = ncs[i]->name;
4556 if (!strncmp(str, name, len)) {
4557 readline_add_completion(rs, name);
4564 static void vm_completion(ReadLineState *rs, const char *str)
4567 BlockDriverState *bs = NULL;
4570 readline_set_completion_index(rs, len);
4571 while ((bs = bdrv_next(bs))) {
4572 SnapshotInfoList *snapshots, *snapshot;
4574 if (!bdrv_can_snapshot(bs)) {
4577 if (bdrv_query_snapshot_info_list(bs, &snapshots, NULL)) {
4580 snapshot = snapshots;
4582 char *completion = snapshot->value->name;
4583 if (!strncmp(str, completion, len)) {
4584 readline_add_completion(rs, completion);
4586 completion = snapshot->value->id;
4587 if (!strncmp(str, completion, len)) {
4588 readline_add_completion(rs, completion);
4590 snapshot = snapshot->next;
4592 qapi_free_SnapshotInfoList(snapshots);
4597 void delvm_completion(ReadLineState *rs, int nb_args, const char *str)
4600 vm_completion(rs, str);
4604 void loadvm_completion(ReadLineState *rs, int nb_args, const char *str)
4607 vm_completion(rs, str);
4611 static void monitor_find_completion_by_table(Monitor *mon,
4612 const mon_cmd_t *cmd_table,
4616 const char *cmdname;
4618 const char *ptype, *str, *name;
4619 const mon_cmd_t *cmd;
4620 BlockDriverState *bs;
4623 /* command completion */
4628 readline_set_completion_index(mon->rs, strlen(cmdname));
4629 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
4630 cmd_completion(mon, cmdname, cmd->name);
4633 /* find the command */
4634 for (cmd = cmd_table; cmd->name != NULL; cmd++) {
4635 if (compare_cmd(args[0], cmd->name)) {
4643 if (cmd->sub_table) {
4644 /* do the job again */
4645 monitor_find_completion_by_table(mon, cmd->sub_table,
4646 &args[1], nb_args - 1);
4649 if (cmd->command_completion) {
4650 cmd->command_completion(mon->rs, nb_args, args[nb_args - 1]);
4654 ptype = next_arg_type(cmd->args_type);
4655 for(i = 0; i < nb_args - 2; i++) {
4656 if (*ptype != '\0') {
4657 ptype = next_arg_type(ptype);
4658 while (*ptype == '?')
4659 ptype = next_arg_type(ptype);
4662 str = args[nb_args - 1];
4663 while (*ptype == '-' && ptype[1] != '\0') {
4664 ptype = next_arg_type(ptype);
4668 /* file completion */
4669 readline_set_completion_index(mon->rs, strlen(str));
4670 file_completion(mon, str);
4673 /* block device name completion */
4674 readline_set_completion_index(mon->rs, strlen(str));
4675 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
4676 name = bdrv_get_device_name(bs);
4677 if (str[0] == '\0' ||
4678 !strncmp(name, str, strlen(str))) {
4679 readline_add_completion(mon->rs, name);
4685 if (!strcmp(cmd->name, "help|?")) {
4686 monitor_find_completion_by_table(mon, cmd_table,
4687 &args[1], nb_args - 1);
4696 static void monitor_find_completion(void *opaque,
4697 const char *cmdline)
4699 Monitor *mon = opaque;
4700 char *args[MAX_ARGS];
4703 /* 1. parse the cmdline */
4704 if (parse_cmdline(cmdline, &nb_args, args) < 0) {
4708 /* if the line ends with a space, it means we want to complete the
4710 len = strlen(cmdline);
4711 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
4712 if (nb_args >= MAX_ARGS) {
4715 args[nb_args++] = g_strdup("");
4718 /* 2. auto complete according to args */
4719 monitor_find_completion_by_table(mon, mon->cmd_table, args, nb_args);
4722 free_cmdline_args(args, nb_args);
4725 static int monitor_can_read(void *opaque)
4727 Monitor *mon = opaque;
4729 return (mon->suspend_cnt == 0) ? 1 : 0;
4732 static bool invalid_qmp_mode(const Monitor *mon, const mon_cmd_t *cmd,
4735 bool is_cap = cmd->mhandler.cmd_new == do_qmp_capabilities;
4737 if (is_cap && mon->qmp.in_command_mode) {
4738 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4739 "Capabilities negotiation is already complete, command "
4740 "'%s' ignored", cmd->name);
4743 if (!is_cap && !mon->qmp.in_command_mode) {
4744 error_set(errp, ERROR_CLASS_COMMAND_NOT_FOUND,
4745 "Expecting capabilities negotiation with "
4746 "'qmp_capabilities' before command '%s'", cmd->name);
4753 * Argument validation rules:
4755 * 1. The argument must exist in cmd_args qdict
4756 * 2. The argument type must be the expected one
4758 * Special case: If the argument doesn't exist in cmd_args and
4759 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4760 * checking is skipped for it.
4762 static void check_client_args_type(const QDict *client_args,
4763 const QDict *cmd_args, int flags,
4766 const QDictEntry *ent;
4768 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4771 const QObject *client_arg = qdict_entry_value(ent);
4772 const char *client_arg_name = qdict_entry_key(ent);
4774 obj = qdict_get(cmd_args, client_arg_name);
4776 if (flags & QMP_ACCEPT_UNKNOWNS) {
4777 /* handler accepts unknowns */
4780 /* client arg doesn't exist */
4781 error_set(errp, QERR_INVALID_PARAMETER, client_arg_name);
4785 arg_type = qobject_to_qstring(obj);
4786 assert(arg_type != NULL);
4788 /* check if argument's type is correct */
4789 switch (qstring_get_str(arg_type)[0]) {
4793 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4794 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4795 client_arg_name, "string");
4803 if (qobject_type(client_arg) != QTYPE_QINT) {
4804 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4805 client_arg_name, "int");
4810 if (qobject_type(client_arg) != QTYPE_QINT &&
4811 qobject_type(client_arg) != QTYPE_QFLOAT) {
4812 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4813 client_arg_name, "number");
4819 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4820 error_set(errp, QERR_INVALID_PARAMETER_TYPE,
4821 client_arg_name, "bool");
4826 assert(flags & QMP_ACCEPT_UNKNOWNS);
4829 /* Any QObject can be passed. */
4834 * These types are not supported by QMP and thus are not
4835 * handled here. Fall through.
4844 * - Check if the client has passed all mandatory args
4845 * - Set special flags for argument validation
4847 static void check_mandatory_args(const QDict *cmd_args,
4848 const QDict *client_args, int *flags,
4851 const QDictEntry *ent;
4853 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4854 const char *cmd_arg_name = qdict_entry_key(ent);
4855 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4856 assert(type != NULL);
4858 if (qstring_get_str(type)[0] == 'O') {
4859 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4860 *flags |= QMP_ACCEPT_UNKNOWNS;
4861 } else if (qstring_get_str(type)[0] != '-' &&
4862 qstring_get_str(type)[1] != '?' &&
4863 !qdict_haskey(client_args, cmd_arg_name)) {
4864 error_set(errp, QERR_MISSING_PARAMETER, cmd_arg_name);
4870 static QDict *qdict_from_args_type(const char *args_type)
4874 QString *key, *type, *cur_qs;
4876 assert(args_type != NULL);
4878 qdict = qdict_new();
4880 if (args_type == NULL || args_type[0] == '\0') {
4881 /* no args, empty qdict */
4885 key = qstring_new();
4886 type = qstring_new();
4891 switch (args_type[i]) {
4894 qdict_put(qdict, qstring_get_str(key), type);
4896 if (args_type[i] == '\0') {
4899 type = qstring_new(); /* qdict has ref */
4900 cur_qs = key = qstring_new();
4906 qstring_append_chr(cur_qs, args_type[i]);
4916 * Client argument checking rules:
4918 * 1. Client must provide all mandatory arguments
4919 * 2. Each argument provided by the client must be expected
4920 * 3. Each argument provided by the client must have the type expected
4923 static void qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args,
4930 cmd_args = qdict_from_args_type(cmd->args_type);
4933 check_mandatory_args(cmd_args, client_args, &flags, &err);
4938 check_client_args_type(client_args, cmd_args, flags, &err);
4941 error_propagate(errp, err);
4946 * Input object checking rules
4948 * 1. Input object must be a dict
4949 * 2. The "execute" key must exist
4950 * 3. The "execute" key must be a string
4951 * 4. If the "arguments" key exists, it must be a dict
4952 * 5. If the "id" key exists, it can be anything (ie. json-value)
4953 * 6. Any argument not listed above is considered invalid
4955 static QDict *qmp_check_input_obj(QObject *input_obj, Error **errp)
4957 const QDictEntry *ent;
4958 int has_exec_key = 0;
4961 if (qobject_type(input_obj) != QTYPE_QDICT) {
4962 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "object");
4966 input_dict = qobject_to_qdict(input_obj);
4968 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4969 const char *arg_name = qdict_entry_key(ent);
4970 const QObject *arg_obj = qdict_entry_value(ent);
4972 if (!strcmp(arg_name, "execute")) {
4973 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4974 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4975 "execute", "string");
4979 } else if (!strcmp(arg_name, "arguments")) {
4980 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4981 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT_MEMBER,
4982 "arguments", "object");
4985 } else if (!strcmp(arg_name, "id")) {
4986 /* Any string is acceptable as "id", so nothing to check */
4988 error_set(errp, QERR_QMP_EXTRA_MEMBER, arg_name);
4993 if (!has_exec_key) {
4994 error_set(errp, QERR_QMP_BAD_INPUT_OBJECT, "execute");
5001 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
5003 Error *local_err = NULL;
5004 QObject *obj, *data;
5005 QDict *input, *args;
5006 const mon_cmd_t *cmd;
5007 const char *cmd_name;
5008 Monitor *mon = cur_mon;
5010 args = input = NULL;
5013 obj = json_parser_parse(tokens, NULL);
5015 // FIXME: should be triggered in json_parser_parse()
5016 error_set(&local_err, QERR_JSON_PARSING);
5020 input = qmp_check_input_obj(obj, &local_err);
5022 qobject_decref(obj);
5026 mon->qmp.id = qdict_get(input, "id");
5027 qobject_incref(mon->qmp.id);
5029 cmd_name = qdict_get_str(input, "execute");
5030 trace_handle_qmp_command(mon, cmd_name);
5031 cmd = qmp_find_cmd(cmd_name);
5033 error_set(&local_err, ERROR_CLASS_COMMAND_NOT_FOUND,
5034 "The command %s has not been found", cmd_name);
5037 if (invalid_qmp_mode(mon, cmd, &local_err)) {
5041 obj = qdict_get(input, "arguments");
5045 args = qobject_to_qdict(obj);
5049 qmp_check_client_args(cmd, args, &local_err);
5054 if (cmd->mhandler.cmd_new(mon, args, &data)) {
5055 /* Command failed... */
5057 /* ... without setting an error, so make one up */
5058 error_set(&local_err, QERR_UNDEFINED_ERROR);
5062 error_set(&local_err, mon->error->err_class, "%s",
5063 mon->error->err_msg);
5067 monitor_protocol_emitter(mon, data, local_err);
5068 qobject_decref(data);
5069 QDECREF(mon->error);
5075 static void monitor_qmp_read(void *opaque, const uint8_t *buf, int size)
5077 Monitor *old_mon = cur_mon;
5081 json_message_parser_feed(&cur_mon->qmp.parser, (const char *) buf, size);
5086 static void monitor_read(void *opaque, const uint8_t *buf, int size)
5088 Monitor *old_mon = cur_mon;
5094 for (i = 0; i < size; i++)
5095 readline_handle_byte(cur_mon->rs, buf[i]);
5097 if (size == 0 || buf[size - 1] != 0)
5098 monitor_printf(cur_mon, "corrupted command\n");
5100 handle_hmp_command(cur_mon, (char *)buf);
5106 static void monitor_command_cb(void *opaque, const char *cmdline,
5107 void *readline_opaque)
5109 Monitor *mon = opaque;
5111 monitor_suspend(mon);
5112 handle_hmp_command(mon, cmdline);
5113 monitor_resume(mon);
5116 int monitor_suspend(Monitor *mon)
5124 void monitor_resume(Monitor *mon)
5128 if (--mon->suspend_cnt == 0)
5129 readline_show_prompt(mon->rs);
5132 static QObject *get_qmp_greeting(void)
5134 QObject *ver = NULL;
5136 qmp_marshal_input_query_version(NULL, NULL, &ver);
5137 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
5140 static void monitor_qmp_event(void *opaque, int event)
5143 Monitor *mon = opaque;
5146 case CHR_EVENT_OPENED:
5147 mon->qmp.in_command_mode = false;
5148 data = get_qmp_greeting();
5149 monitor_json_emitter(mon, data);
5150 qobject_decref(data);
5153 case CHR_EVENT_CLOSED:
5154 json_message_parser_destroy(&mon->qmp.parser);
5155 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
5157 monitor_fdsets_cleanup();
5162 static void monitor_event(void *opaque, int event)
5164 Monitor *mon = opaque;
5167 case CHR_EVENT_MUX_IN:
5168 qemu_mutex_lock(&mon->out_lock);
5170 qemu_mutex_unlock(&mon->out_lock);
5171 if (mon->reset_seen) {
5172 readline_restart(mon->rs);
5173 monitor_resume(mon);
5176 mon->suspend_cnt = 0;
5180 case CHR_EVENT_MUX_OUT:
5181 if (mon->reset_seen) {
5182 if (mon->suspend_cnt == 0) {
5183 monitor_printf(mon, "\n");
5186 monitor_suspend(mon);
5190 qemu_mutex_lock(&mon->out_lock);
5192 qemu_mutex_unlock(&mon->out_lock);
5195 case CHR_EVENT_OPENED:
5196 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
5197 "information\n", QEMU_VERSION);
5198 if (!mon->mux_out) {
5199 readline_restart(mon->rs);
5200 readline_show_prompt(mon->rs);
5202 mon->reset_seen = 1;
5206 case CHR_EVENT_CLOSED:
5208 monitor_fdsets_cleanup();
5214 compare_mon_cmd(const void *a, const void *b)
5216 return strcmp(((const mon_cmd_t *)a)->name,
5217 ((const mon_cmd_t *)b)->name);
5220 static void sortcmdlist(void)
5223 int elem_size = sizeof(mon_cmd_t);
5225 array_num = sizeof(mon_cmds)/elem_size-1;
5226 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
5228 array_num = sizeof(info_cmds)/elem_size-1;
5229 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
5241 /* These functions just adapt the readline interface in a typesafe way. We
5242 * could cast function pointers but that discards compiler checks.
5244 static void GCC_FMT_ATTR(2, 3) monitor_readline_printf(void *opaque,
5245 const char *fmt, ...)
5249 monitor_vprintf(opaque, fmt, ap);
5253 static void monitor_readline_flush(void *opaque)
5255 monitor_flush(opaque);
5258 static void __attribute__((constructor)) monitor_lock_init(void)
5260 qemu_mutex_init(&monitor_lock);
5263 void monitor_init(CharDriverState *chr, int flags)
5265 static int is_first_init = 1;
5268 if (is_first_init) {
5269 monitor_qapi_event_init();
5274 mon = g_malloc(sizeof(*mon));
5275 monitor_data_init(mon);
5279 if (flags & MONITOR_USE_READLINE) {
5280 mon->rs = readline_init(monitor_readline_printf,
5281 monitor_readline_flush,
5283 monitor_find_completion);
5284 monitor_read_command(mon, 0);
5287 if (monitor_is_qmp(mon)) {
5288 qemu_chr_add_handlers(chr, monitor_can_read, monitor_qmp_read,
5289 monitor_qmp_event, mon);
5290 qemu_chr_fe_set_echo(chr, true);
5291 json_message_parser_init(&mon->qmp.parser, handle_qmp_command);
5293 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
5294 monitor_event, mon);
5297 qemu_mutex_lock(&monitor_lock);
5298 QLIST_INSERT_HEAD(&mon_list, mon, entry);
5299 qemu_mutex_unlock(&monitor_lock);
5302 static void bdrv_password_cb(void *opaque, const char *password,
5303 void *readline_opaque)
5305 Monitor *mon = opaque;
5306 BlockDriverState *bs = readline_opaque;
5308 Error *local_err = NULL;
5310 bdrv_add_key(bs, password, &local_err);
5312 monitor_printf(mon, "%s\n", error_get_pretty(local_err));
5313 error_free(local_err);
5316 if (mon->password_completion_cb)
5317 mon->password_completion_cb(mon->password_opaque, ret);
5319 monitor_read_command(mon, 1);
5322 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
5323 BlockCompletionFunc *completion_cb,
5328 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
5329 bdrv_get_encrypted_filename(bs));
5331 mon->password_completion_cb = completion_cb;
5332 mon->password_opaque = opaque;
5334 err = monitor_read_password(mon, bdrv_password_cb, bs);
5336 if (err && completion_cb)
5337 completion_cb(opaque, err);
5342 int monitor_read_block_device_key(Monitor *mon, const char *device,
5343 BlockCompletionFunc *completion_cb,
5349 blk = blk_by_name(device);
5351 monitor_printf(mon, "Device not found %s\n", device);
5355 bdrv_add_key(blk_bs(blk), NULL, &err);
5358 return monitor_read_bdrv_key_start(mon, blk_bs(blk), completion_cb, opaque);
5361 if (completion_cb) {
5362 completion_cb(opaque, 0);
5367 QemuOptsList qemu_mon_opts = {
5369 .implied_opt_name = "chardev",
5370 .head = QTAILQ_HEAD_INITIALIZER(qemu_mon_opts.head),
5374 .type = QEMU_OPT_STRING,
5377 .type = QEMU_OPT_STRING,
5380 .type = QEMU_OPT_BOOL,
5383 .type = QEMU_OPT_BOOL,
5385 { /* end of list */ }
5390 void qmp_rtc_reset_reinjection(Error **errp)
5392 error_set(errp, QERR_FEATURE_DISABLED, "rtc-reset-reinjection");