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
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "net/slirp.h"
36 #include "qemu-char.h"
37 #include "ui/qemu-spice.h"
43 #include "audio/audio.h"
46 #include "qemu-timer.h"
47 #include "migration.h"
56 #include "json-streamer.h"
57 #include "json-parser.h"
61 #include "trace/control.h"
62 #ifdef CONFIG_TRACE_SIMPLE
63 #include "trace/simple.h"
65 #include "ui/qemu-spice.h"
67 #include "qmp-commands.h"
70 /* for pic/irq_info */
71 #if defined(TARGET_SPARC)
74 #include "hw/lm32_pic.h"
77 //#define DEBUG_COMPLETION
83 * 'B' block device name
84 * 's' string (accept optional quote)
85 * 'O' option string of the form NAME=VALUE,...
86 * parsed according to QemuOptsList given by its name
87 * Example: 'device:O' uses qemu_device_opts.
88 * Restriction: only lists with empty desc are supported
89 * TODO lift the restriction
91 * 'l' target long (32 or 64 bit)
92 * 'M' just like 'l', except in user mode the value is
93 * multiplied by 2^20 (think Mebibyte)
94 * 'o' octets (aka bytes)
95 * user mode accepts an optional T, t, G, g, M, m, K, k
96 * suffix, which multiplies the value by 2^40 for
97 * suffixes T and t, 2^30 for suffixes G and g, 2^20 for
98 * M and m, 2^10 for K and k
100 * user mode accepts an optional ms, us, ns suffix,
101 * which divides the value by 1e3, 1e6, 1e9, respectively
102 * '/' optional gdb-like print format (like "/10x")
104 * '?' optional type (for all types, except '/')
105 * '.' other form of optional type (for 'i' and 'l')
107 * user mode accepts "on" or "off"
108 * '-' optional parameter (eg. '-f')
112 typedef struct MonitorCompletionData MonitorCompletionData;
113 struct MonitorCompletionData {
115 void (*user_print)(Monitor *mon, const QObject *data);
118 typedef struct mon_cmd_t {
120 const char *args_type;
123 void (*user_print)(Monitor *mon, const QObject *data);
125 void (*info)(Monitor *mon);
126 void (*cmd)(Monitor *mon, const QDict *qdict);
127 int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
128 int (*cmd_async)(Monitor *mon, const QDict *params,
129 MonitorCompletion *cb, void *opaque);
135 /* file descriptors passed via SCM_RIGHTS */
136 typedef struct mon_fd_t mon_fd_t;
140 QLIST_ENTRY(mon_fd_t) next;
143 typedef struct MonitorControl {
145 JSONMessageParser parser;
150 CharDriverState *chr;
155 uint8_t outbuf[1024];
160 BlockDriverCompletionFunc *password_completion_cb;
161 void *password_opaque;
162 #ifdef CONFIG_DEBUG_MONITOR
166 QLIST_HEAD(,mon_fd_t) fds;
167 QLIST_ENTRY(Monitor) entry;
170 #ifdef CONFIG_DEBUG_MONITOR
171 #define MON_DEBUG(fmt, ...) do { \
172 fprintf(stderr, "Monitor: "); \
173 fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
175 static inline void mon_print_count_inc(Monitor *mon)
177 mon->print_calls_nr++;
180 static inline void mon_print_count_init(Monitor *mon)
182 mon->print_calls_nr = 0;
185 static inline int mon_print_count_get(const Monitor *mon)
187 return mon->print_calls_nr;
190 #else /* !CONFIG_DEBUG_MONITOR */
191 #define MON_DEBUG(fmt, ...) do { } while (0)
192 static inline void mon_print_count_inc(Monitor *mon) { }
193 static inline void mon_print_count_init(Monitor *mon) { }
194 static inline int mon_print_count_get(const Monitor *mon) { return 0; }
195 #endif /* CONFIG_DEBUG_MONITOR */
197 /* QMP checker flags */
198 #define QMP_ACCEPT_UNKNOWNS 1
200 static QLIST_HEAD(mon_list, Monitor) mon_list;
202 static mon_cmd_t mon_cmds[];
203 static mon_cmd_t info_cmds[];
205 static const mon_cmd_t qmp_cmds[];
208 Monitor *default_mon;
210 static void monitor_command_cb(Monitor *mon, const char *cmdline,
213 static inline int qmp_cmd_mode(const Monitor *mon)
215 return (mon->mc ? mon->mc->command_mode : 0);
218 /* Return true if in control mode, false otherwise */
219 static inline int monitor_ctrl_mode(const Monitor *mon)
221 return (mon->flags & MONITOR_USE_CONTROL);
224 /* Return non-zero iff we have a current monitor, and it is in QMP mode. */
225 int monitor_cur_is_qmp(void)
227 return cur_mon && monitor_ctrl_mode(cur_mon);
230 void monitor_read_command(Monitor *mon, int show_prompt)
235 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
237 readline_show_prompt(mon->rs);
240 int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
243 if (monitor_ctrl_mode(mon)) {
244 qerror_report(QERR_MISSING_PARAMETER, "password");
246 } else if (mon->rs) {
247 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
248 /* prompt is printed on return from the command handler */
251 monitor_printf(mon, "terminal does not support password prompting\n");
256 void monitor_flush(Monitor *mon)
258 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
259 qemu_chr_fe_write(mon->chr, mon->outbuf, mon->outbuf_index);
260 mon->outbuf_index = 0;
264 /* flush at every end of line or if the buffer is full */
265 static void monitor_puts(Monitor *mon, const char *str)
274 mon->outbuf[mon->outbuf_index++] = '\r';
275 mon->outbuf[mon->outbuf_index++] = c;
276 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
282 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
289 mon_print_count_inc(mon);
291 if (monitor_ctrl_mode(mon)) {
295 vsnprintf(buf, sizeof(buf), fmt, ap);
296 monitor_puts(mon, buf);
299 void monitor_printf(Monitor *mon, const char *fmt, ...)
303 monitor_vprintf(mon, fmt, ap);
307 void monitor_print_filename(Monitor *mon, const char *filename)
311 for (i = 0; filename[i]; i++) {
312 switch (filename[i]) {
316 monitor_printf(mon, "\\%c", filename[i]);
319 monitor_printf(mon, "\\t");
322 monitor_printf(mon, "\\r");
325 monitor_printf(mon, "\\n");
328 monitor_printf(mon, "%c", filename[i]);
334 static int GCC_FMT_ATTR(2, 3) monitor_fprintf(FILE *stream,
335 const char *fmt, ...)
339 monitor_vprintf((Monitor *)stream, fmt, ap);
344 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
346 static inline int handler_is_qobject(const mon_cmd_t *cmd)
348 return cmd->user_print != NULL;
351 static inline bool handler_is_async(const mon_cmd_t *cmd)
353 return cmd->flags & MONITOR_CMD_ASYNC;
356 static inline int monitor_has_error(const Monitor *mon)
358 return mon->error != NULL;
361 static void monitor_json_emitter(Monitor *mon, const QObject *data)
365 json = mon->flags & MONITOR_USE_PRETTY ? qobject_to_json_pretty(data) :
366 qobject_to_json(data);
367 assert(json != NULL);
369 qstring_append_chr(json, '\n');
370 monitor_puts(mon, qstring_get_str(json));
375 static void monitor_protocol_emitter(Monitor *mon, QObject *data)
379 trace_monitor_protocol_emitter(mon);
383 if (!monitor_has_error(mon)) {
384 /* success response */
386 qobject_incref(data);
387 qdict_put_obj(qmp, "return", data);
389 /* return an empty QDict by default */
390 qdict_put(qmp, "return", qdict_new());
394 qdict_put(mon->error->error, "desc", qerror_human(mon->error));
395 qdict_put(qmp, "error", mon->error->error);
396 QINCREF(mon->error->error);
402 qdict_put_obj(qmp, "id", mon->mc->id);
406 monitor_json_emitter(mon, QOBJECT(qmp));
410 static void timestamp_put(QDict *qdict)
416 err = qemu_gettimeofday(&tv);
420 obj = qobject_from_jsonf("{ 'seconds': %" PRId64 ", "
421 "'microseconds': %" PRId64 " }",
422 (int64_t) tv.tv_sec, (int64_t) tv.tv_usec);
423 qdict_put_obj(qdict, "timestamp", obj);
427 * monitor_protocol_event(): Generate a Monitor event
429 * Event-specific data can be emitted through the (optional) 'data' parameter.
431 void monitor_protocol_event(MonitorEvent event, QObject *data)
434 const char *event_name;
437 assert(event < QEVENT_MAX);
440 case QEVENT_SHUTDOWN:
441 event_name = "SHUTDOWN";
444 event_name = "RESET";
446 case QEVENT_POWERDOWN:
447 event_name = "POWERDOWN";
453 event_name = "RESUME";
455 case QEVENT_VNC_CONNECTED:
456 event_name = "VNC_CONNECTED";
458 case QEVENT_VNC_INITIALIZED:
459 event_name = "VNC_INITIALIZED";
461 case QEVENT_VNC_DISCONNECTED:
462 event_name = "VNC_DISCONNECTED";
464 case QEVENT_BLOCK_IO_ERROR:
465 event_name = "BLOCK_IO_ERROR";
467 case QEVENT_RTC_CHANGE:
468 event_name = "RTC_CHANGE";
470 case QEVENT_WATCHDOG:
471 event_name = "WATCHDOG";
473 case QEVENT_SPICE_CONNECTED:
474 event_name = "SPICE_CONNECTED";
476 case QEVENT_SPICE_INITIALIZED:
477 event_name = "SPICE_INITIALIZED";
479 case QEVENT_SPICE_DISCONNECTED:
480 event_name = "SPICE_DISCONNECTED";
482 case QEVENT_BLOCK_JOB_COMPLETED:
483 event_name = "BLOCK_JOB_COMPLETED";
485 case QEVENT_BLOCK_JOB_CANCELLED:
486 event_name = "BLOCK_JOB_CANCELLED";
488 case QEVENT_DEVICE_TRAY_MOVED:
489 event_name = "DEVICE_TRAY_MOVED";
498 qdict_put(qmp, "event", qstring_from_str(event_name));
500 qobject_incref(data);
501 qdict_put_obj(qmp, "data", data);
504 QLIST_FOREACH(mon, &mon_list, entry) {
505 if (monitor_ctrl_mode(mon) && qmp_cmd_mode(mon)) {
506 monitor_json_emitter(mon, QOBJECT(qmp));
512 static int do_qmp_capabilities(Monitor *mon, const QDict *params,
515 /* Will setup QMP capabilities in the future */
516 if (monitor_ctrl_mode(mon)) {
517 mon->mc->command_mode = 1;
523 static void handle_user_command(Monitor *mon, const char *cmdline);
525 char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
526 int64_t cpu_index, Error **errp)
529 Monitor *old_mon, hmp;
530 CharDriverState mchar;
532 memset(&hmp, 0, sizeof(hmp));
533 qemu_chr_init_mem(&mchar);
540 int ret = monitor_set_cpu(cpu_index);
543 error_set(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
549 handle_user_command(&hmp, command_line);
552 if (qemu_chr_mem_osize(hmp.chr) > 0) {
553 QString *str = qemu_chr_mem_to_qs(hmp.chr);
554 output = g_strdup(qstring_get_str(str));
557 output = g_strdup("");
561 qemu_chr_close_mem(hmp.chr);
565 static int compare_cmd(const char *name, const char *list)
567 const char *p, *pstart;
575 p = pstart + strlen(pstart);
576 if ((p - pstart) == len && !memcmp(pstart, name, len))
585 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
586 const char *prefix, const char *name)
588 const mon_cmd_t *cmd;
590 for(cmd = cmds; cmd->name != NULL; cmd++) {
591 if (!name || !strcmp(name, cmd->name))
592 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
593 cmd->params, cmd->help);
597 static void help_cmd(Monitor *mon, const char *name)
599 if (name && !strcmp(name, "info")) {
600 help_cmd_dump(mon, info_cmds, "info ", NULL);
602 help_cmd_dump(mon, mon_cmds, "", name);
603 if (name && !strcmp(name, "log")) {
604 const CPULogItem *item;
605 monitor_printf(mon, "Log items (comma separated):\n");
606 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
607 for(item = cpu_log_items; item->mask != 0; item++) {
608 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
614 static void do_help_cmd(Monitor *mon, const QDict *qdict)
616 help_cmd(mon, qdict_get_try_str(qdict, "name"));
619 static void do_trace_event_set_state(Monitor *mon, const QDict *qdict)
621 const char *tp_name = qdict_get_str(qdict, "name");
622 bool new_state = qdict_get_bool(qdict, "option");
623 int ret = trace_event_set_state(tp_name, new_state);
626 monitor_printf(mon, "unknown event name \"%s\"\n", tp_name);
630 #ifdef CONFIG_TRACE_SIMPLE
631 static void do_trace_file(Monitor *mon, const QDict *qdict)
633 const char *op = qdict_get_try_str(qdict, "op");
634 const char *arg = qdict_get_try_str(qdict, "arg");
637 st_print_trace_file_status((FILE *)mon, &monitor_fprintf);
638 } else if (!strcmp(op, "on")) {
639 st_set_trace_file_enabled(true);
640 } else if (!strcmp(op, "off")) {
641 st_set_trace_file_enabled(false);
642 } else if (!strcmp(op, "flush")) {
643 st_flush_trace_buffer();
644 } else if (!strcmp(op, "set")) {
646 st_set_trace_file(arg);
649 monitor_printf(mon, "unexpected argument \"%s\"\n", op);
650 help_cmd(mon, "trace-file");
655 static void user_monitor_complete(void *opaque, QObject *ret_data)
657 MonitorCompletionData *data = (MonitorCompletionData *)opaque;
660 data->user_print(data->mon, ret_data);
662 monitor_resume(data->mon);
666 static void qmp_monitor_complete(void *opaque, QObject *ret_data)
668 monitor_protocol_emitter(opaque, ret_data);
671 static int qmp_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
674 return cmd->mhandler.cmd_async(mon, params, qmp_monitor_complete, mon);
677 static void user_async_cmd_handler(Monitor *mon, const mon_cmd_t *cmd,
682 MonitorCompletionData *cb_data = g_malloc(sizeof(*cb_data));
684 cb_data->user_print = cmd->user_print;
685 monitor_suspend(mon);
686 ret = cmd->mhandler.cmd_async(mon, params,
687 user_monitor_complete, cb_data);
694 static void do_info(Monitor *mon, const QDict *qdict)
696 const mon_cmd_t *cmd;
697 const char *item = qdict_get_try_str(qdict, "item");
703 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
704 if (compare_cmd(item, cmd->name))
708 if (cmd->name == NULL) {
712 cmd->mhandler.info(mon);
716 help_cmd(mon, "info");
719 CommandInfoList *qmp_query_commands(Error **errp)
721 CommandInfoList *info, *cmd_list = NULL;
722 const mon_cmd_t *cmd;
724 for (cmd = qmp_cmds; cmd->name != NULL; cmd++) {
725 info = g_malloc0(sizeof(*info));
726 info->value = g_malloc0(sizeof(*info->value));
727 info->value->name = g_strdup(cmd->name);
729 info->next = cmd_list;
736 /* set the current CPU defined by the user */
737 int monitor_set_cpu(int cpu_index)
741 for(env = first_cpu; env != NULL; env = env->next_cpu) {
742 if (env->cpu_index == cpu_index) {
743 cur_mon->mon_cpu = env;
750 static CPUState *mon_get_cpu(void)
752 if (!cur_mon->mon_cpu) {
755 cpu_synchronize_state(cur_mon->mon_cpu);
756 return cur_mon->mon_cpu;
759 int monitor_get_cpu_index(void)
761 return mon_get_cpu()->cpu_index;
764 static void do_info_registers(Monitor *mon)
769 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
772 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
777 static void do_info_jit(Monitor *mon)
779 dump_exec_info((FILE *)mon, monitor_fprintf);
782 static void do_info_history(Monitor *mon)
791 str = readline_get_history(mon->rs, i);
794 monitor_printf(mon, "%d: '%s'\n", i, str);
799 #if defined(TARGET_PPC)
800 /* XXX: not implemented in other targets */
801 static void do_info_cpu_stats(Monitor *mon)
806 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
810 #if defined(CONFIG_TRACE_SIMPLE)
811 static void do_info_trace(Monitor *mon)
813 st_print_trace((FILE *)mon, &monitor_fprintf);
817 static void do_trace_print_events(Monitor *mon)
819 trace_print_events((FILE *)mon, &monitor_fprintf);
822 static int add_graphics_client(Monitor *mon, const QDict *qdict, QObject **ret_data)
824 const char *protocol = qdict_get_str(qdict, "protocol");
825 const char *fdname = qdict_get_str(qdict, "fdname");
828 if (strcmp(protocol, "spice") == 0) {
829 int fd = monitor_get_fd(mon, fdname);
830 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
831 int tls = qdict_get_try_bool(qdict, "tls", 0);
833 /* correct one? spice isn't a device ,,, */
834 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
837 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
842 } else if (strcmp(protocol, "vnc") == 0) {
843 int fd = monitor_get_fd(mon, fdname);
844 int skipauth = qdict_get_try_bool(qdict, "skipauth", 0);
845 vnc_display_add_client(NULL, fd, skipauth);
848 } else if ((s = qemu_chr_find(protocol)) != NULL) {
849 int fd = monitor_get_fd(mon, fdname);
850 if (qemu_chr_add_client(s, fd) < 0) {
851 qerror_report(QERR_ADD_CLIENT_FAILED);
857 qerror_report(QERR_INVALID_PARAMETER, "protocol");
861 static int client_migrate_info(Monitor *mon, const QDict *qdict,
862 MonitorCompletion cb, void *opaque)
864 const char *protocol = qdict_get_str(qdict, "protocol");
865 const char *hostname = qdict_get_str(qdict, "hostname");
866 const char *subject = qdict_get_try_str(qdict, "cert-subject");
867 int port = qdict_get_try_int(qdict, "port", -1);
868 int tls_port = qdict_get_try_int(qdict, "tls-port", -1);
871 if (strcmp(protocol, "spice") == 0) {
873 qerror_report(QERR_DEVICE_NOT_ACTIVE, "spice");
877 ret = qemu_spice_migrate_info(hostname, port, tls_port, subject,
880 qerror_report(QERR_UNDEFINED_ERROR);
886 qerror_report(QERR_INVALID_PARAMETER, "protocol");
890 static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data)
892 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
896 static void do_logfile(Monitor *mon, const QDict *qdict)
898 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
901 static void do_log(Monitor *mon, const QDict *qdict)
904 const char *items = qdict_get_str(qdict, "items");
906 if (!strcmp(items, "none")) {
909 mask = cpu_str_to_log_mask(items);
911 help_cmd(mon, "log");
918 static void do_singlestep(Monitor *mon, const QDict *qdict)
920 const char *option = qdict_get_try_str(qdict, "option");
921 if (!option || !strcmp(option, "on")) {
923 } else if (!strcmp(option, "off")) {
926 monitor_printf(mon, "unexpected option %s\n", option);
930 static void do_gdbserver(Monitor *mon, const QDict *qdict)
932 const char *device = qdict_get_try_str(qdict, "device");
934 device = "tcp::" DEFAULT_GDBSTUB_PORT;
935 if (gdbserver_start(device) < 0) {
936 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
938 } else if (strcmp(device, "none") == 0) {
939 monitor_printf(mon, "Disabled gdbserver\n");
941 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
946 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
948 const char *action = qdict_get_str(qdict, "action");
949 if (select_watchdog_action(action) == -1) {
950 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
954 static void monitor_printc(Monitor *mon, int c)
956 monitor_printf(mon, "'");
959 monitor_printf(mon, "\\'");
962 monitor_printf(mon, "\\\\");
965 monitor_printf(mon, "\\n");
968 monitor_printf(mon, "\\r");
971 if (c >= 32 && c <= 126) {
972 monitor_printf(mon, "%c", c);
974 monitor_printf(mon, "\\x%02x", c);
978 monitor_printf(mon, "'");
981 static void memory_dump(Monitor *mon, int count, int format, int wsize,
982 target_phys_addr_t addr, int is_physical)
985 int l, line_size, i, max_digits, len;
996 } else if (wsize == 4) {
999 /* as default we use the current CS size */
1002 #ifdef TARGET_X86_64
1003 if ((env->efer & MSR_EFER_LMA) &&
1004 (env->segs[R_CS].flags & DESC_L_MASK))
1008 if (!(env->segs[R_CS].flags & DESC_B_MASK))
1013 monitor_disas(mon, env, addr, count, is_physical, flags);
1017 len = wsize * count;
1026 max_digits = (wsize * 8 + 2) / 3;
1030 max_digits = (wsize * 8) / 4;
1034 max_digits = (wsize * 8 * 10 + 32) / 33;
1043 monitor_printf(mon, TARGET_FMT_plx ":", addr);
1045 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
1050 cpu_physical_memory_read(addr, buf, l);
1052 env = mon_get_cpu();
1053 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
1054 monitor_printf(mon, " Cannot access memory\n");
1063 v = ldub_raw(buf + i);
1066 v = lduw_raw(buf + i);
1069 v = (uint32_t)ldl_raw(buf + i);
1072 v = ldq_raw(buf + i);
1075 monitor_printf(mon, " ");
1078 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
1081 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
1084 monitor_printf(mon, "%*" PRIu64, max_digits, v);
1087 monitor_printf(mon, "%*" PRId64, max_digits, v);
1090 monitor_printc(mon, v);
1095 monitor_printf(mon, "\n");
1101 static void do_memory_dump(Monitor *mon, const QDict *qdict)
1103 int count = qdict_get_int(qdict, "count");
1104 int format = qdict_get_int(qdict, "format");
1105 int size = qdict_get_int(qdict, "size");
1106 target_long addr = qdict_get_int(qdict, "addr");
1108 memory_dump(mon, count, format, size, addr, 0);
1111 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
1113 int count = qdict_get_int(qdict, "count");
1114 int format = qdict_get_int(qdict, "format");
1115 int size = qdict_get_int(qdict, "size");
1116 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
1118 memory_dump(mon, count, format, size, addr, 1);
1121 static void do_print(Monitor *mon, const QDict *qdict)
1123 int format = qdict_get_int(qdict, "format");
1124 target_phys_addr_t val = qdict_get_int(qdict, "val");
1126 #if TARGET_PHYS_ADDR_BITS == 32
1129 monitor_printf(mon, "%#o", val);
1132 monitor_printf(mon, "%#x", val);
1135 monitor_printf(mon, "%u", val);
1139 monitor_printf(mon, "%d", val);
1142 monitor_printc(mon, val);
1148 monitor_printf(mon, "%#" PRIo64, val);
1151 monitor_printf(mon, "%#" PRIx64, val);
1154 monitor_printf(mon, "%" PRIu64, val);
1158 monitor_printf(mon, "%" PRId64, val);
1161 monitor_printc(mon, val);
1165 monitor_printf(mon, "\n");
1168 static void do_sum(Monitor *mon, const QDict *qdict)
1172 uint32_t start = qdict_get_int(qdict, "start");
1173 uint32_t size = qdict_get_int(qdict, "size");
1176 for(addr = start; addr < (start + size); addr++) {
1177 uint8_t val = ldub_phys(addr);
1178 /* BSD sum algorithm ('sum' Unix command) */
1179 sum = (sum >> 1) | (sum << 15);
1182 monitor_printf(mon, "%05d\n", sum);
1190 static const KeyDef key_defs[] = {
1192 { 0x36, "shift_r" },
1197 { 0xe4, "altgr_r" },
1217 { 0x0e, "backspace" },
1230 { 0x1a, "bracket_left" },
1231 { 0x1b, "bracket_right" },
1243 { 0x27, "semicolon" },
1244 { 0x28, "apostrophe" },
1245 { 0x29, "grave_accent" },
1247 { 0x2b, "backslash" },
1259 { 0x37, "asterisk" },
1262 { 0x3a, "caps_lock" },
1273 { 0x45, "num_lock" },
1274 { 0x46, "scroll_lock" },
1276 { 0xb5, "kp_divide" },
1277 { 0x37, "kp_multiply" },
1278 { 0x4a, "kp_subtract" },
1280 { 0x9c, "kp_enter" },
1281 { 0x53, "kp_decimal" },
1314 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1329 { 0xfe, "compose" },
1334 static int get_keycode(const char *key)
1340 for(p = key_defs; p->name != NULL; p++) {
1341 if (!strcmp(key, p->name))
1344 if (strstart(key, "0x", NULL)) {
1345 ret = strtoul(key, &endp, 0);
1346 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1352 #define MAX_KEYCODES 16
1353 static uint8_t keycodes[MAX_KEYCODES];
1354 static int nb_pending_keycodes;
1355 static QEMUTimer *key_timer;
1357 static void release_keys(void *opaque)
1361 while (nb_pending_keycodes > 0) {
1362 nb_pending_keycodes--;
1363 keycode = keycodes[nb_pending_keycodes];
1365 kbd_put_keycode(0xe0);
1366 kbd_put_keycode(keycode | 0x80);
1370 static void do_sendkey(Monitor *mon, const QDict *qdict)
1372 char keyname_buf[16];
1374 int keyname_len, keycode, i;
1375 const char *string = qdict_get_str(qdict, "string");
1376 int has_hold_time = qdict_haskey(qdict, "hold_time");
1377 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1379 if (nb_pending_keycodes > 0) {
1380 qemu_del_timer(key_timer);
1387 separator = strchr(string, '-');
1388 keyname_len = separator ? separator - string : strlen(string);
1389 if (keyname_len > 0) {
1390 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1391 if (keyname_len > sizeof(keyname_buf) - 1) {
1392 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1395 if (i == MAX_KEYCODES) {
1396 monitor_printf(mon, "too many keys\n");
1399 keyname_buf[keyname_len] = 0;
1400 keycode = get_keycode(keyname_buf);
1402 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1405 keycodes[i++] = keycode;
1409 string = separator + 1;
1411 nb_pending_keycodes = i;
1412 /* key down events */
1413 for (i = 0; i < nb_pending_keycodes; i++) {
1414 keycode = keycodes[i];
1416 kbd_put_keycode(0xe0);
1417 kbd_put_keycode(keycode & 0x7f);
1419 /* delayed key up events */
1420 qemu_mod_timer(key_timer, qemu_get_clock_ns(vm_clock) +
1421 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1424 static int mouse_button_state;
1426 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1429 const char *dx_str = qdict_get_str(qdict, "dx_str");
1430 const char *dy_str = qdict_get_str(qdict, "dy_str");
1431 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1432 dx = strtol(dx_str, NULL, 0);
1433 dy = strtol(dy_str, NULL, 0);
1436 dz = strtol(dz_str, NULL, 0);
1437 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1440 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1442 int button_state = qdict_get_int(qdict, "button_state");
1443 mouse_button_state = button_state;
1444 kbd_mouse_event(0, 0, 0, mouse_button_state);
1447 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1449 int size = qdict_get_int(qdict, "size");
1450 int addr = qdict_get_int(qdict, "addr");
1451 int has_index = qdict_haskey(qdict, "index");
1456 int index = qdict_get_int(qdict, "index");
1457 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1465 val = cpu_inb(addr);
1469 val = cpu_inw(addr);
1473 val = cpu_inl(addr);
1477 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1478 suffix, addr, size * 2, val);
1481 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1483 int size = qdict_get_int(qdict, "size");
1484 int addr = qdict_get_int(qdict, "addr");
1485 int val = qdict_get_int(qdict, "val");
1487 addr &= IOPORTS_MASK;
1492 cpu_outb(addr, val);
1495 cpu_outw(addr, val);
1498 cpu_outl(addr, val);
1503 static void do_boot_set(Monitor *mon, const QDict *qdict)
1506 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1508 res = qemu_boot_set(bootdevice);
1510 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1511 } else if (res > 0) {
1512 monitor_printf(mon, "setting boot device list failed\n");
1514 monitor_printf(mon, "no function defined to set boot device list for "
1515 "this architecture\n");
1519 #if defined(TARGET_I386)
1520 static void print_pte(Monitor *mon, target_phys_addr_t addr,
1521 target_phys_addr_t pte,
1522 target_phys_addr_t mask)
1524 #ifdef TARGET_X86_64
1525 if (addr & (1ULL << 47)) {
1529 monitor_printf(mon, TARGET_FMT_plx ": " TARGET_FMT_plx
1530 " %c%c%c%c%c%c%c%c%c\n",
1533 pte & PG_NX_MASK ? 'X' : '-',
1534 pte & PG_GLOBAL_MASK ? 'G' : '-',
1535 pte & PG_PSE_MASK ? 'P' : '-',
1536 pte & PG_DIRTY_MASK ? 'D' : '-',
1537 pte & PG_ACCESSED_MASK ? 'A' : '-',
1538 pte & PG_PCD_MASK ? 'C' : '-',
1539 pte & PG_PWT_MASK ? 'T' : '-',
1540 pte & PG_USER_MASK ? 'U' : '-',
1541 pte & PG_RW_MASK ? 'W' : '-');
1544 static void tlb_info_32(Monitor *mon, CPUState *env)
1546 unsigned int l1, l2;
1547 uint32_t pgd, pde, pte;
1549 pgd = env->cr[3] & ~0xfff;
1550 for(l1 = 0; l1 < 1024; l1++) {
1551 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1552 pde = le32_to_cpu(pde);
1553 if (pde & PG_PRESENT_MASK) {
1554 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1556 print_pte(mon, (l1 << 22), pde, ~((1 << 21) - 1));
1558 for(l2 = 0; l2 < 1024; l2++) {
1559 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1560 pte = le32_to_cpu(pte);
1561 if (pte & PG_PRESENT_MASK) {
1562 print_pte(mon, (l1 << 22) + (l2 << 12),
1572 static void tlb_info_pae32(Monitor *mon, CPUState *env)
1574 unsigned int l1, l2, l3;
1575 uint64_t pdpe, pde, pte;
1576 uint64_t pdp_addr, pd_addr, pt_addr;
1578 pdp_addr = env->cr[3] & ~0x1f;
1579 for (l1 = 0; l1 < 4; l1++) {
1580 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1581 pdpe = le64_to_cpu(pdpe);
1582 if (pdpe & PG_PRESENT_MASK) {
1583 pd_addr = pdpe & 0x3fffffffff000ULL;
1584 for (l2 = 0; l2 < 512; l2++) {
1585 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1586 pde = le64_to_cpu(pde);
1587 if (pde & PG_PRESENT_MASK) {
1588 if (pde & PG_PSE_MASK) {
1589 /* 2M pages with PAE, CR4.PSE is ignored */
1590 print_pte(mon, (l1 << 30 ) + (l2 << 21), pde,
1591 ~((target_phys_addr_t)(1 << 20) - 1));
1593 pt_addr = pde & 0x3fffffffff000ULL;
1594 for (l3 = 0; l3 < 512; l3++) {
1595 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1596 pte = le64_to_cpu(pte);
1597 if (pte & PG_PRESENT_MASK) {
1598 print_pte(mon, (l1 << 30 ) + (l2 << 21)
1601 ~(target_phys_addr_t)0xfff);
1611 #ifdef TARGET_X86_64
1612 static void tlb_info_64(Monitor *mon, CPUState *env)
1614 uint64_t l1, l2, l3, l4;
1615 uint64_t pml4e, pdpe, pde, pte;
1616 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr;
1618 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1619 for (l1 = 0; l1 < 512; l1++) {
1620 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1621 pml4e = le64_to_cpu(pml4e);
1622 if (pml4e & PG_PRESENT_MASK) {
1623 pdp_addr = pml4e & 0x3fffffffff000ULL;
1624 for (l2 = 0; l2 < 512; l2++) {
1625 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1626 pdpe = le64_to_cpu(pdpe);
1627 if (pdpe & PG_PRESENT_MASK) {
1628 if (pdpe & PG_PSE_MASK) {
1629 /* 1G pages, CR4.PSE is ignored */
1630 print_pte(mon, (l1 << 39) + (l2 << 30), pdpe,
1631 0x3ffffc0000000ULL);
1633 pd_addr = pdpe & 0x3fffffffff000ULL;
1634 for (l3 = 0; l3 < 512; l3++) {
1635 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1636 pde = le64_to_cpu(pde);
1637 if (pde & PG_PRESENT_MASK) {
1638 if (pde & PG_PSE_MASK) {
1639 /* 2M pages, CR4.PSE is ignored */
1640 print_pte(mon, (l1 << 39) + (l2 << 30) +
1642 0x3ffffffe00000ULL);
1644 pt_addr = pde & 0x3fffffffff000ULL;
1645 for (l4 = 0; l4 < 512; l4++) {
1646 cpu_physical_memory_read(pt_addr
1649 pte = le64_to_cpu(pte);
1650 if (pte & PG_PRESENT_MASK) {
1651 print_pte(mon, (l1 << 39) +
1653 (l3 << 21) + (l4 << 12),
1655 0x3fffffffff000ULL);
1669 static void tlb_info(Monitor *mon)
1673 env = mon_get_cpu();
1675 if (!(env->cr[0] & CR0_PG_MASK)) {
1676 monitor_printf(mon, "PG disabled\n");
1679 if (env->cr[4] & CR4_PAE_MASK) {
1680 #ifdef TARGET_X86_64
1681 if (env->hflags & HF_LMA_MASK) {
1682 tlb_info_64(mon, env);
1686 tlb_info_pae32(mon, env);
1689 tlb_info_32(mon, env);
1693 static void mem_print(Monitor *mon, target_phys_addr_t *pstart,
1695 target_phys_addr_t end, int prot)
1698 prot1 = *plast_prot;
1699 if (prot != prot1) {
1700 if (*pstart != -1) {
1701 monitor_printf(mon, TARGET_FMT_plx "-" TARGET_FMT_plx " "
1702 TARGET_FMT_plx " %c%c%c\n",
1703 *pstart, end, end - *pstart,
1704 prot1 & PG_USER_MASK ? 'u' : '-',
1706 prot1 & PG_RW_MASK ? 'w' : '-');
1716 static void mem_info_32(Monitor *mon, CPUState *env)
1718 unsigned int l1, l2;
1719 int prot, last_prot;
1720 uint32_t pgd, pde, pte;
1721 target_phys_addr_t start, end;
1723 pgd = env->cr[3] & ~0xfff;
1726 for(l1 = 0; l1 < 1024; l1++) {
1727 cpu_physical_memory_read(pgd + l1 * 4, &pde, 4);
1728 pde = le32_to_cpu(pde);
1730 if (pde & PG_PRESENT_MASK) {
1731 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1732 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1733 mem_print(mon, &start, &last_prot, end, prot);
1735 for(l2 = 0; l2 < 1024; l2++) {
1736 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4, &pte, 4);
1737 pte = le32_to_cpu(pte);
1738 end = (l1 << 22) + (l2 << 12);
1739 if (pte & PG_PRESENT_MASK) {
1741 (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1745 mem_print(mon, &start, &last_prot, end, prot);
1750 mem_print(mon, &start, &last_prot, end, prot);
1753 /* Flush last range */
1754 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
1757 static void mem_info_pae32(Monitor *mon, CPUState *env)
1759 unsigned int l1, l2, l3;
1760 int prot, last_prot;
1761 uint64_t pdpe, pde, pte;
1762 uint64_t pdp_addr, pd_addr, pt_addr;
1763 target_phys_addr_t start, end;
1765 pdp_addr = env->cr[3] & ~0x1f;
1768 for (l1 = 0; l1 < 4; l1++) {
1769 cpu_physical_memory_read(pdp_addr + l1 * 8, &pdpe, 8);
1770 pdpe = le64_to_cpu(pdpe);
1772 if (pdpe & PG_PRESENT_MASK) {
1773 pd_addr = pdpe & 0x3fffffffff000ULL;
1774 for (l2 = 0; l2 < 512; l2++) {
1775 cpu_physical_memory_read(pd_addr + l2 * 8, &pde, 8);
1776 pde = le64_to_cpu(pde);
1777 end = (l1 << 30) + (l2 << 21);
1778 if (pde & PG_PRESENT_MASK) {
1779 if (pde & PG_PSE_MASK) {
1780 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1782 mem_print(mon, &start, &last_prot, end, prot);
1784 pt_addr = pde & 0x3fffffffff000ULL;
1785 for (l3 = 0; l3 < 512; l3++) {
1786 cpu_physical_memory_read(pt_addr + l3 * 8, &pte, 8);
1787 pte = le64_to_cpu(pte);
1788 end = (l1 << 30) + (l2 << 21) + (l3 << 12);
1789 if (pte & PG_PRESENT_MASK) {
1790 prot = pte & pde & (PG_USER_MASK | PG_RW_MASK |
1795 mem_print(mon, &start, &last_prot, end, prot);
1800 mem_print(mon, &start, &last_prot, end, prot);
1805 mem_print(mon, &start, &last_prot, end, prot);
1808 /* Flush last range */
1809 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 32, 0);
1813 #ifdef TARGET_X86_64
1814 static void mem_info_64(Monitor *mon, CPUState *env)
1816 int prot, last_prot;
1817 uint64_t l1, l2, l3, l4;
1818 uint64_t pml4e, pdpe, pde, pte;
1819 uint64_t pml4_addr, pdp_addr, pd_addr, pt_addr, start, end;
1821 pml4_addr = env->cr[3] & 0x3fffffffff000ULL;
1824 for (l1 = 0; l1 < 512; l1++) {
1825 cpu_physical_memory_read(pml4_addr + l1 * 8, &pml4e, 8);
1826 pml4e = le64_to_cpu(pml4e);
1828 if (pml4e & PG_PRESENT_MASK) {
1829 pdp_addr = pml4e & 0x3fffffffff000ULL;
1830 for (l2 = 0; l2 < 512; l2++) {
1831 cpu_physical_memory_read(pdp_addr + l2 * 8, &pdpe, 8);
1832 pdpe = le64_to_cpu(pdpe);
1833 end = (l1 << 39) + (l2 << 30);
1834 if (pdpe & PG_PRESENT_MASK) {
1835 if (pdpe & PG_PSE_MASK) {
1836 prot = pdpe & (PG_USER_MASK | PG_RW_MASK |
1839 mem_print(mon, &start, &last_prot, end, prot);
1841 pd_addr = pdpe & 0x3fffffffff000ULL;
1842 for (l3 = 0; l3 < 512; l3++) {
1843 cpu_physical_memory_read(pd_addr + l3 * 8, &pde, 8);
1844 pde = le64_to_cpu(pde);
1845 end = (l1 << 39) + (l2 << 30) + (l3 << 21);
1846 if (pde & PG_PRESENT_MASK) {
1847 if (pde & PG_PSE_MASK) {
1848 prot = pde & (PG_USER_MASK | PG_RW_MASK |
1850 prot &= pml4e & pdpe;
1851 mem_print(mon, &start, &last_prot, end, prot);
1853 pt_addr = pde & 0x3fffffffff000ULL;
1854 for (l4 = 0; l4 < 512; l4++) {
1855 cpu_physical_memory_read(pt_addr
1858 pte = le64_to_cpu(pte);
1859 end = (l1 << 39) + (l2 << 30) +
1860 (l3 << 21) + (l4 << 12);
1861 if (pte & PG_PRESENT_MASK) {
1862 prot = pte & (PG_USER_MASK | PG_RW_MASK |
1864 prot &= pml4e & pdpe & pde;
1868 mem_print(mon, &start, &last_prot, end, prot);
1873 mem_print(mon, &start, &last_prot, end, prot);
1879 mem_print(mon, &start, &last_prot, end, prot);
1884 mem_print(mon, &start, &last_prot, end, prot);
1887 /* Flush last range */
1888 mem_print(mon, &start, &last_prot, (target_phys_addr_t)1 << 48, 0);
1892 static void mem_info(Monitor *mon)
1896 env = mon_get_cpu();
1898 if (!(env->cr[0] & CR0_PG_MASK)) {
1899 monitor_printf(mon, "PG disabled\n");
1902 if (env->cr[4] & CR4_PAE_MASK) {
1903 #ifdef TARGET_X86_64
1904 if (env->hflags & HF_LMA_MASK) {
1905 mem_info_64(mon, env);
1909 mem_info_pae32(mon, env);
1912 mem_info_32(mon, env);
1917 #if defined(TARGET_SH4)
1919 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1921 monitor_printf(mon, " tlb%i:\t"
1922 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1923 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1924 "dirty=%hhu writethrough=%hhu\n",
1926 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1927 tlb->v, tlb->sh, tlb->c, tlb->pr,
1931 static void tlb_info(Monitor *mon)
1933 CPUState *env = mon_get_cpu();
1936 monitor_printf (mon, "ITLB:\n");
1937 for (i = 0 ; i < ITLB_SIZE ; i++)
1938 print_tlb (mon, i, &env->itlb[i]);
1939 monitor_printf (mon, "UTLB:\n");
1940 for (i = 0 ; i < UTLB_SIZE ; i++)
1941 print_tlb (mon, i, &env->utlb[i]);
1946 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
1947 static void tlb_info(Monitor *mon)
1949 CPUState *env1 = mon_get_cpu();
1951 dump_mmu((FILE*)mon, (fprintf_function)monitor_printf, env1);
1955 static void do_info_mtree(Monitor *mon)
1957 mtree_info((fprintf_function)monitor_printf, mon);
1960 static void do_info_numa(Monitor *mon)
1965 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1966 for (i = 0; i < nb_numa_nodes; i++) {
1967 monitor_printf(mon, "node %d cpus:", i);
1968 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1969 if (env->numa_node == i) {
1970 monitor_printf(mon, " %d", env->cpu_index);
1973 monitor_printf(mon, "\n");
1974 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1979 #ifdef CONFIG_PROFILER
1984 static void do_info_profile(Monitor *mon)
1990 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1991 dev_time, dev_time / (double)get_ticks_per_sec());
1992 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1993 qemu_time, qemu_time / (double)get_ticks_per_sec());
1998 static void do_info_profile(Monitor *mon)
2000 monitor_printf(mon, "Internal profiler not compiled\n");
2004 /* Capture support */
2005 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
2007 static void do_info_capture(Monitor *mon)
2012 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2013 monitor_printf(mon, "[%d]: ", i);
2014 s->ops.info (s->opaque);
2019 static void do_stop_capture(Monitor *mon, const QDict *qdict)
2022 int n = qdict_get_int(qdict, "n");
2025 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
2027 s->ops.destroy (s->opaque);
2028 QLIST_REMOVE (s, entries);
2035 static void do_wav_capture(Monitor *mon, const QDict *qdict)
2037 const char *path = qdict_get_str(qdict, "path");
2038 int has_freq = qdict_haskey(qdict, "freq");
2039 int freq = qdict_get_try_int(qdict, "freq", -1);
2040 int has_bits = qdict_haskey(qdict, "bits");
2041 int bits = qdict_get_try_int(qdict, "bits", -1);
2042 int has_channels = qdict_haskey(qdict, "nchannels");
2043 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
2046 s = g_malloc0 (sizeof (*s));
2048 freq = has_freq ? freq : 44100;
2049 bits = has_bits ? bits : 16;
2050 nchannels = has_channels ? nchannels : 2;
2052 if (wav_start_capture (s, path, freq, bits, nchannels)) {
2053 monitor_printf(mon, "Failed to add wave capture\n");
2057 QLIST_INSERT_HEAD (&capture_head, s, entries);
2061 static qemu_acl *find_acl(Monitor *mon, const char *name)
2063 qemu_acl *acl = qemu_acl_find(name);
2066 monitor_printf(mon, "acl: unknown list '%s'\n", name);
2071 static void do_acl_show(Monitor *mon, const QDict *qdict)
2073 const char *aclname = qdict_get_str(qdict, "aclname");
2074 qemu_acl *acl = find_acl(mon, aclname);
2075 qemu_acl_entry *entry;
2079 monitor_printf(mon, "policy: %s\n",
2080 acl->defaultDeny ? "deny" : "allow");
2081 QTAILQ_FOREACH(entry, &acl->entries, next) {
2083 monitor_printf(mon, "%d: %s %s\n", i,
2084 entry->deny ? "deny" : "allow", entry->match);
2089 static void do_acl_reset(Monitor *mon, const QDict *qdict)
2091 const char *aclname = qdict_get_str(qdict, "aclname");
2092 qemu_acl *acl = find_acl(mon, aclname);
2095 qemu_acl_reset(acl);
2096 monitor_printf(mon, "acl: removed all rules\n");
2100 static void do_acl_policy(Monitor *mon, const QDict *qdict)
2102 const char *aclname = qdict_get_str(qdict, "aclname");
2103 const char *policy = qdict_get_str(qdict, "policy");
2104 qemu_acl *acl = find_acl(mon, aclname);
2107 if (strcmp(policy, "allow") == 0) {
2108 acl->defaultDeny = 0;
2109 monitor_printf(mon, "acl: policy set to 'allow'\n");
2110 } else if (strcmp(policy, "deny") == 0) {
2111 acl->defaultDeny = 1;
2112 monitor_printf(mon, "acl: policy set to 'deny'\n");
2114 monitor_printf(mon, "acl: unknown policy '%s', "
2115 "expected 'deny' or 'allow'\n", policy);
2120 static void do_acl_add(Monitor *mon, const QDict *qdict)
2122 const char *aclname = qdict_get_str(qdict, "aclname");
2123 const char *match = qdict_get_str(qdict, "match");
2124 const char *policy = qdict_get_str(qdict, "policy");
2125 int has_index = qdict_haskey(qdict, "index");
2126 int index = qdict_get_try_int(qdict, "index", -1);
2127 qemu_acl *acl = find_acl(mon, aclname);
2131 if (strcmp(policy, "allow") == 0) {
2133 } else if (strcmp(policy, "deny") == 0) {
2136 monitor_printf(mon, "acl: unknown policy '%s', "
2137 "expected 'deny' or 'allow'\n", policy);
2141 ret = qemu_acl_insert(acl, deny, match, index);
2143 ret = qemu_acl_append(acl, deny, match);
2145 monitor_printf(mon, "acl: unable to add acl entry\n");
2147 monitor_printf(mon, "acl: added rule at position %d\n", ret);
2151 static void do_acl_remove(Monitor *mon, const QDict *qdict)
2153 const char *aclname = qdict_get_str(qdict, "aclname");
2154 const char *match = qdict_get_str(qdict, "match");
2155 qemu_acl *acl = find_acl(mon, aclname);
2159 ret = qemu_acl_remove(acl, match);
2161 monitor_printf(mon, "acl: no matching acl entry\n");
2163 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
2167 #if defined(TARGET_I386)
2168 static void do_inject_mce(Monitor *mon, const QDict *qdict)
2171 int cpu_index = qdict_get_int(qdict, "cpu_index");
2172 int bank = qdict_get_int(qdict, "bank");
2173 uint64_t status = qdict_get_int(qdict, "status");
2174 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
2175 uint64_t addr = qdict_get_int(qdict, "addr");
2176 uint64_t misc = qdict_get_int(qdict, "misc");
2177 int flags = MCE_INJECT_UNCOND_AO;
2179 if (qdict_get_try_bool(qdict, "broadcast", 0)) {
2180 flags |= MCE_INJECT_BROADCAST;
2182 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu) {
2183 if (cenv->cpu_index == cpu_index) {
2184 cpu_x86_inject_mce(mon, cenv, bank, status, mcg_status, addr, misc,
2192 static int do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2194 const char *fdname = qdict_get_str(qdict, "fdname");
2198 fd = qemu_chr_fe_get_msgfd(mon->chr);
2200 qerror_report(QERR_FD_NOT_SUPPLIED);
2204 if (qemu_isdigit(fdname[0])) {
2205 qerror_report(QERR_INVALID_PARAMETER_VALUE, "fdname",
2206 "a name not starting with a digit");
2210 QLIST_FOREACH(monfd, &mon->fds, next) {
2211 if (strcmp(monfd->name, fdname) != 0) {
2220 monfd = g_malloc0(sizeof(mon_fd_t));
2221 monfd->name = g_strdup(fdname);
2224 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
2228 static int do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
2230 const char *fdname = qdict_get_str(qdict, "fdname");
2233 QLIST_FOREACH(monfd, &mon->fds, next) {
2234 if (strcmp(monfd->name, fdname) != 0) {
2238 QLIST_REMOVE(monfd, next);
2240 g_free(monfd->name);
2245 qerror_report(QERR_FD_NOT_FOUND, fdname);
2249 static void do_loadvm(Monitor *mon, const QDict *qdict)
2251 int saved_vm_running = runstate_is_running();
2252 const char *name = qdict_get_str(qdict, "name");
2254 vm_stop(RUN_STATE_RESTORE_VM);
2256 if (load_vmstate(name) == 0 && saved_vm_running) {
2261 int monitor_get_fd(Monitor *mon, const char *fdname)
2265 QLIST_FOREACH(monfd, &mon->fds, next) {
2268 if (strcmp(monfd->name, fdname) != 0) {
2274 /* caller takes ownership of fd */
2275 QLIST_REMOVE(monfd, next);
2276 g_free(monfd->name);
2285 /* mon_cmds and info_cmds would be sorted at runtime */
2286 static mon_cmd_t mon_cmds[] = {
2287 #include "hmp-commands.h"
2291 /* Please update hmp-commands.hx when adding or changing commands */
2292 static mon_cmd_t info_cmds[] = {
2297 .help = "show the version of QEMU",
2298 .mhandler.info = hmp_info_version,
2304 .help = "show the network state",
2305 .mhandler.info = do_info_network,
2311 .help = "show the character devices",
2312 .mhandler.info = hmp_info_chardev,
2318 .help = "show the block devices",
2319 .mhandler.info = hmp_info_block,
2322 .name = "blockstats",
2325 .help = "show block device statistics",
2326 .mhandler.info = hmp_info_blockstats,
2329 .name = "block-jobs",
2332 .help = "show progress of ongoing block device operations",
2333 .mhandler.info = hmp_info_block_jobs,
2336 .name = "registers",
2339 .help = "show the cpu registers",
2340 .mhandler.info = do_info_registers,
2346 .help = "show infos for each CPU",
2347 .mhandler.info = hmp_info_cpus,
2353 .help = "show the command line history",
2354 .mhandler.info = do_info_history,
2356 #if defined(TARGET_I386) || defined(TARGET_PPC) || defined(TARGET_MIPS) || \
2357 defined(TARGET_LM32) || (defined(TARGET_SPARC) && !defined(TARGET_SPARC64))
2362 .help = "show the interrupts statistics (if available)",
2364 .mhandler.info = sun4m_irq_info,
2365 #elif defined(TARGET_LM32)
2366 .mhandler.info = lm32_irq_info,
2368 .mhandler.info = irq_info,
2375 .help = "show i8259 (PIC) state",
2377 .mhandler.info = sun4m_pic_info,
2378 #elif defined(TARGET_LM32)
2379 .mhandler.info = lm32_do_pic_info,
2381 .mhandler.info = pic_info,
2389 .help = "show PCI info",
2390 .mhandler.info = hmp_info_pci,
2392 #if defined(TARGET_I386) || defined(TARGET_SH4) || defined(TARGET_SPARC) || \
2398 .help = "show virtual to physical memory mappings",
2399 .mhandler.info = tlb_info,
2402 #if defined(TARGET_I386)
2407 .help = "show the active virtual memory mappings",
2408 .mhandler.info = mem_info,
2415 .help = "show memory tree",
2416 .mhandler.info = do_info_mtree,
2422 .help = "show dynamic compiler info",
2423 .mhandler.info = do_info_jit,
2429 .help = "show KVM information",
2430 .mhandler.info = hmp_info_kvm,
2436 .help = "show NUMA information",
2437 .mhandler.info = do_info_numa,
2443 .help = "show guest USB devices",
2444 .mhandler.info = usb_info,
2450 .help = "show host USB devices",
2451 .mhandler.info = usb_host_info,
2457 .help = "show profiling information",
2458 .mhandler.info = do_info_profile,
2464 .help = "show capture information",
2465 .mhandler.info = do_info_capture,
2468 .name = "snapshots",
2471 .help = "show the currently saved VM snapshots",
2472 .mhandler.info = do_info_snapshots,
2478 .help = "show the current VM status (running|paused)",
2479 .mhandler.info = hmp_info_status,
2485 .help = "show guest PCMCIA status",
2486 .mhandler.info = pcmcia_info,
2492 .help = "show which guest mouse is receiving events",
2493 .mhandler.info = hmp_info_mice,
2499 .help = "show the vnc server status",
2500 .mhandler.info = hmp_info_vnc,
2502 #if defined(CONFIG_SPICE)
2507 .help = "show the spice server status",
2508 .mhandler.info = hmp_info_spice,
2515 .help = "show the current VM name",
2516 .mhandler.info = hmp_info_name,
2522 .help = "show the current VM UUID",
2523 .mhandler.info = hmp_info_uuid,
2525 #if defined(TARGET_PPC)
2530 .help = "show CPU statistics",
2531 .mhandler.info = do_info_cpu_stats,
2534 #if defined(CONFIG_SLIRP)
2539 .help = "show user network stack connection states",
2540 .mhandler.info = do_info_usernet,
2547 .help = "show migration status",
2548 .mhandler.info = hmp_info_migrate,
2554 .help = "show balloon information",
2555 .mhandler.info = hmp_info_balloon,
2561 .help = "show device tree",
2562 .mhandler.info = do_info_qtree,
2568 .help = "show qdev device model list",
2569 .mhandler.info = do_info_qdm,
2575 .help = "show roms",
2576 .mhandler.info = do_info_roms,
2578 #if defined(CONFIG_TRACE_SIMPLE)
2583 .help = "show current contents of trace buffer",
2584 .mhandler.info = do_info_trace,
2588 .name = "trace-events",
2591 .help = "show available trace-events & their state",
2592 .mhandler.info = do_trace_print_events,
2599 static const mon_cmd_t qmp_cmds[] = {
2600 #include "qmp-commands-old.h"
2604 /*******************************************************************/
2606 static const char *pch;
2607 static jmp_buf expr_env;
2612 typedef struct MonitorDef {
2615 target_long (*get_value)(const struct MonitorDef *md, int val);
2619 #if defined(TARGET_I386)
2620 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2622 CPUState *env = mon_get_cpu();
2623 return env->eip + env->segs[R_CS].base;
2627 #if defined(TARGET_PPC)
2628 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2630 CPUState *env = mon_get_cpu();
2635 for (i = 0; i < 8; i++)
2636 u |= env->crf[i] << (32 - (4 * i));
2641 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2643 CPUState *env = mon_get_cpu();
2647 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2649 CPUState *env = mon_get_cpu();
2653 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2655 CPUState *env = mon_get_cpu();
2656 return cpu_ppc_load_decr(env);
2659 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2661 CPUState *env = mon_get_cpu();
2662 return cpu_ppc_load_tbu(env);
2665 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2667 CPUState *env = mon_get_cpu();
2668 return cpu_ppc_load_tbl(env);
2672 #if defined(TARGET_SPARC)
2673 #ifndef TARGET_SPARC64
2674 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2676 CPUState *env = mon_get_cpu();
2678 return cpu_get_psr(env);
2682 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2684 CPUState *env = mon_get_cpu();
2685 return env->regwptr[val];
2689 static const MonitorDef monitor_defs[] = {
2692 #define SEG(name, seg) \
2693 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2694 { name ".base", offsetof(CPUState, segs[seg].base) },\
2695 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2697 { "eax", offsetof(CPUState, regs[0]) },
2698 { "ecx", offsetof(CPUState, regs[1]) },
2699 { "edx", offsetof(CPUState, regs[2]) },
2700 { "ebx", offsetof(CPUState, regs[3]) },
2701 { "esp|sp", offsetof(CPUState, regs[4]) },
2702 { "ebp|fp", offsetof(CPUState, regs[5]) },
2703 { "esi", offsetof(CPUState, regs[6]) },
2704 { "edi", offsetof(CPUState, regs[7]) },
2705 #ifdef TARGET_X86_64
2706 { "r8", offsetof(CPUState, regs[8]) },
2707 { "r9", offsetof(CPUState, regs[9]) },
2708 { "r10", offsetof(CPUState, regs[10]) },
2709 { "r11", offsetof(CPUState, regs[11]) },
2710 { "r12", offsetof(CPUState, regs[12]) },
2711 { "r13", offsetof(CPUState, regs[13]) },
2712 { "r14", offsetof(CPUState, regs[14]) },
2713 { "r15", offsetof(CPUState, regs[15]) },
2715 { "eflags", offsetof(CPUState, eflags) },
2716 { "eip", offsetof(CPUState, eip) },
2723 { "pc", 0, monitor_get_pc, },
2724 #elif defined(TARGET_PPC)
2725 /* General purpose registers */
2726 { "r0", offsetof(CPUState, gpr[0]) },
2727 { "r1", offsetof(CPUState, gpr[1]) },
2728 { "r2", offsetof(CPUState, gpr[2]) },
2729 { "r3", offsetof(CPUState, gpr[3]) },
2730 { "r4", offsetof(CPUState, gpr[4]) },
2731 { "r5", offsetof(CPUState, gpr[5]) },
2732 { "r6", offsetof(CPUState, gpr[6]) },
2733 { "r7", offsetof(CPUState, gpr[7]) },
2734 { "r8", offsetof(CPUState, gpr[8]) },
2735 { "r9", offsetof(CPUState, gpr[9]) },
2736 { "r10", offsetof(CPUState, gpr[10]) },
2737 { "r11", offsetof(CPUState, gpr[11]) },
2738 { "r12", offsetof(CPUState, gpr[12]) },
2739 { "r13", offsetof(CPUState, gpr[13]) },
2740 { "r14", offsetof(CPUState, gpr[14]) },
2741 { "r15", offsetof(CPUState, gpr[15]) },
2742 { "r16", offsetof(CPUState, gpr[16]) },
2743 { "r17", offsetof(CPUState, gpr[17]) },
2744 { "r18", offsetof(CPUState, gpr[18]) },
2745 { "r19", offsetof(CPUState, gpr[19]) },
2746 { "r20", offsetof(CPUState, gpr[20]) },
2747 { "r21", offsetof(CPUState, gpr[21]) },
2748 { "r22", offsetof(CPUState, gpr[22]) },
2749 { "r23", offsetof(CPUState, gpr[23]) },
2750 { "r24", offsetof(CPUState, gpr[24]) },
2751 { "r25", offsetof(CPUState, gpr[25]) },
2752 { "r26", offsetof(CPUState, gpr[26]) },
2753 { "r27", offsetof(CPUState, gpr[27]) },
2754 { "r28", offsetof(CPUState, gpr[28]) },
2755 { "r29", offsetof(CPUState, gpr[29]) },
2756 { "r30", offsetof(CPUState, gpr[30]) },
2757 { "r31", offsetof(CPUState, gpr[31]) },
2758 /* Floating point registers */
2759 { "f0", offsetof(CPUState, fpr[0]) },
2760 { "f1", offsetof(CPUState, fpr[1]) },
2761 { "f2", offsetof(CPUState, fpr[2]) },
2762 { "f3", offsetof(CPUState, fpr[3]) },
2763 { "f4", offsetof(CPUState, fpr[4]) },
2764 { "f5", offsetof(CPUState, fpr[5]) },
2765 { "f6", offsetof(CPUState, fpr[6]) },
2766 { "f7", offsetof(CPUState, fpr[7]) },
2767 { "f8", offsetof(CPUState, fpr[8]) },
2768 { "f9", offsetof(CPUState, fpr[9]) },
2769 { "f10", offsetof(CPUState, fpr[10]) },
2770 { "f11", offsetof(CPUState, fpr[11]) },
2771 { "f12", offsetof(CPUState, fpr[12]) },
2772 { "f13", offsetof(CPUState, fpr[13]) },
2773 { "f14", offsetof(CPUState, fpr[14]) },
2774 { "f15", offsetof(CPUState, fpr[15]) },
2775 { "f16", offsetof(CPUState, fpr[16]) },
2776 { "f17", offsetof(CPUState, fpr[17]) },
2777 { "f18", offsetof(CPUState, fpr[18]) },
2778 { "f19", offsetof(CPUState, fpr[19]) },
2779 { "f20", offsetof(CPUState, fpr[20]) },
2780 { "f21", offsetof(CPUState, fpr[21]) },
2781 { "f22", offsetof(CPUState, fpr[22]) },
2782 { "f23", offsetof(CPUState, fpr[23]) },
2783 { "f24", offsetof(CPUState, fpr[24]) },
2784 { "f25", offsetof(CPUState, fpr[25]) },
2785 { "f26", offsetof(CPUState, fpr[26]) },
2786 { "f27", offsetof(CPUState, fpr[27]) },
2787 { "f28", offsetof(CPUState, fpr[28]) },
2788 { "f29", offsetof(CPUState, fpr[29]) },
2789 { "f30", offsetof(CPUState, fpr[30]) },
2790 { "f31", offsetof(CPUState, fpr[31]) },
2791 { "fpscr", offsetof(CPUState, fpscr) },
2792 /* Next instruction pointer */
2793 { "nip|pc", offsetof(CPUState, nip) },
2794 { "lr", offsetof(CPUState, lr) },
2795 { "ctr", offsetof(CPUState, ctr) },
2796 { "decr", 0, &monitor_get_decr, },
2797 { "ccr", 0, &monitor_get_ccr, },
2798 /* Machine state register */
2799 { "msr", 0, &monitor_get_msr, },
2800 { "xer", 0, &monitor_get_xer, },
2801 { "tbu", 0, &monitor_get_tbu, },
2802 { "tbl", 0, &monitor_get_tbl, },
2803 #if defined(TARGET_PPC64)
2804 /* Address space register */
2805 { "asr", offsetof(CPUState, asr) },
2807 /* Segment registers */
2808 { "sdr1", offsetof(CPUState, spr[SPR_SDR1]) },
2809 { "sr0", offsetof(CPUState, sr[0]) },
2810 { "sr1", offsetof(CPUState, sr[1]) },
2811 { "sr2", offsetof(CPUState, sr[2]) },
2812 { "sr3", offsetof(CPUState, sr[3]) },
2813 { "sr4", offsetof(CPUState, sr[4]) },
2814 { "sr5", offsetof(CPUState, sr[5]) },
2815 { "sr6", offsetof(CPUState, sr[6]) },
2816 { "sr7", offsetof(CPUState, sr[7]) },
2817 { "sr8", offsetof(CPUState, sr[8]) },
2818 { "sr9", offsetof(CPUState, sr[9]) },
2819 { "sr10", offsetof(CPUState, sr[10]) },
2820 { "sr11", offsetof(CPUState, sr[11]) },
2821 { "sr12", offsetof(CPUState, sr[12]) },
2822 { "sr13", offsetof(CPUState, sr[13]) },
2823 { "sr14", offsetof(CPUState, sr[14]) },
2824 { "sr15", offsetof(CPUState, sr[15]) },
2825 /* Too lazy to put BATs... */
2826 { "pvr", offsetof(CPUState, spr[SPR_PVR]) },
2828 { "srr0", offsetof(CPUState, spr[SPR_SRR0]) },
2829 { "srr1", offsetof(CPUState, spr[SPR_SRR1]) },
2830 { "sprg0", offsetof(CPUState, spr[SPR_SPRG0]) },
2831 { "sprg1", offsetof(CPUState, spr[SPR_SPRG1]) },
2832 { "sprg2", offsetof(CPUState, spr[SPR_SPRG2]) },
2833 { "sprg3", offsetof(CPUState, spr[SPR_SPRG3]) },
2834 { "sprg4", offsetof(CPUState, spr[SPR_SPRG4]) },
2835 { "sprg5", offsetof(CPUState, spr[SPR_SPRG5]) },
2836 { "sprg6", offsetof(CPUState, spr[SPR_SPRG6]) },
2837 { "sprg7", offsetof(CPUState, spr[SPR_SPRG7]) },
2838 { "pid", offsetof(CPUState, spr[SPR_BOOKE_PID]) },
2839 { "csrr0", offsetof(CPUState, spr[SPR_BOOKE_CSRR0]) },
2840 { "csrr1", offsetof(CPUState, spr[SPR_BOOKE_CSRR1]) },
2841 { "esr", offsetof(CPUState, spr[SPR_BOOKE_ESR]) },
2842 { "dear", offsetof(CPUState, spr[SPR_BOOKE_DEAR]) },
2843 { "mcsr", offsetof(CPUState, spr[SPR_BOOKE_MCSR]) },
2844 { "tsr", offsetof(CPUState, spr[SPR_BOOKE_TSR]) },
2845 { "tcr", offsetof(CPUState, spr[SPR_BOOKE_TCR]) },
2846 { "vrsave", offsetof(CPUState, spr[SPR_VRSAVE]) },
2847 { "pir", offsetof(CPUState, spr[SPR_BOOKE_PIR]) },
2848 { "mcsrr0", offsetof(CPUState, spr[SPR_BOOKE_MCSRR0]) },
2849 { "mcsrr1", offsetof(CPUState, spr[SPR_BOOKE_MCSRR1]) },
2850 { "decar", offsetof(CPUState, spr[SPR_BOOKE_DECAR]) },
2851 { "ivpr", offsetof(CPUState, spr[SPR_BOOKE_IVPR]) },
2852 { "epcr", offsetof(CPUState, spr[SPR_BOOKE_EPCR]) },
2853 { "sprg8", offsetof(CPUState, spr[SPR_BOOKE_SPRG8]) },
2854 { "ivor0", offsetof(CPUState, spr[SPR_BOOKE_IVOR0]) },
2855 { "ivor1", offsetof(CPUState, spr[SPR_BOOKE_IVOR1]) },
2856 { "ivor2", offsetof(CPUState, spr[SPR_BOOKE_IVOR2]) },
2857 { "ivor3", offsetof(CPUState, spr[SPR_BOOKE_IVOR3]) },
2858 { "ivor4", offsetof(CPUState, spr[SPR_BOOKE_IVOR4]) },
2859 { "ivor5", offsetof(CPUState, spr[SPR_BOOKE_IVOR5]) },
2860 { "ivor6", offsetof(CPUState, spr[SPR_BOOKE_IVOR6]) },
2861 { "ivor7", offsetof(CPUState, spr[SPR_BOOKE_IVOR7]) },
2862 { "ivor8", offsetof(CPUState, spr[SPR_BOOKE_IVOR8]) },
2863 { "ivor9", offsetof(CPUState, spr[SPR_BOOKE_IVOR9]) },
2864 { "ivor10", offsetof(CPUState, spr[SPR_BOOKE_IVOR10]) },
2865 { "ivor11", offsetof(CPUState, spr[SPR_BOOKE_IVOR11]) },
2866 { "ivor12", offsetof(CPUState, spr[SPR_BOOKE_IVOR12]) },
2867 { "ivor13", offsetof(CPUState, spr[SPR_BOOKE_IVOR13]) },
2868 { "ivor14", offsetof(CPUState, spr[SPR_BOOKE_IVOR14]) },
2869 { "ivor15", offsetof(CPUState, spr[SPR_BOOKE_IVOR15]) },
2870 { "ivor32", offsetof(CPUState, spr[SPR_BOOKE_IVOR32]) },
2871 { "ivor33", offsetof(CPUState, spr[SPR_BOOKE_IVOR33]) },
2872 { "ivor34", offsetof(CPUState, spr[SPR_BOOKE_IVOR34]) },
2873 { "ivor35", offsetof(CPUState, spr[SPR_BOOKE_IVOR35]) },
2874 { "ivor36", offsetof(CPUState, spr[SPR_BOOKE_IVOR36]) },
2875 { "ivor37", offsetof(CPUState, spr[SPR_BOOKE_IVOR37]) },
2876 { "mas0", offsetof(CPUState, spr[SPR_BOOKE_MAS0]) },
2877 { "mas1", offsetof(CPUState, spr[SPR_BOOKE_MAS1]) },
2878 { "mas2", offsetof(CPUState, spr[SPR_BOOKE_MAS2]) },
2879 { "mas3", offsetof(CPUState, spr[SPR_BOOKE_MAS3]) },
2880 { "mas4", offsetof(CPUState, spr[SPR_BOOKE_MAS4]) },
2881 { "mas6", offsetof(CPUState, spr[SPR_BOOKE_MAS6]) },
2882 { "mas7", offsetof(CPUState, spr[SPR_BOOKE_MAS7]) },
2883 { "mmucfg", offsetof(CPUState, spr[SPR_MMUCFG]) },
2884 { "tlb0cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB0CFG]) },
2885 { "tlb1cfg", offsetof(CPUState, spr[SPR_BOOKE_TLB1CFG]) },
2886 { "epr", offsetof(CPUState, spr[SPR_BOOKE_EPR]) },
2887 { "eplc", offsetof(CPUState, spr[SPR_BOOKE_EPLC]) },
2888 { "epsc", offsetof(CPUState, spr[SPR_BOOKE_EPSC]) },
2889 { "svr", offsetof(CPUState, spr[SPR_E500_SVR]) },
2890 { "mcar", offsetof(CPUState, spr[SPR_Exxx_MCAR]) },
2891 { "pid1", offsetof(CPUState, spr[SPR_BOOKE_PID1]) },
2892 { "pid2", offsetof(CPUState, spr[SPR_BOOKE_PID2]) },
2893 { "hid0", offsetof(CPUState, spr[SPR_HID0]) },
2895 #elif defined(TARGET_SPARC)
2896 { "g0", offsetof(CPUState, gregs[0]) },
2897 { "g1", offsetof(CPUState, gregs[1]) },
2898 { "g2", offsetof(CPUState, gregs[2]) },
2899 { "g3", offsetof(CPUState, gregs[3]) },
2900 { "g4", offsetof(CPUState, gregs[4]) },
2901 { "g5", offsetof(CPUState, gregs[5]) },
2902 { "g6", offsetof(CPUState, gregs[6]) },
2903 { "g7", offsetof(CPUState, gregs[7]) },
2904 { "o0", 0, monitor_get_reg },
2905 { "o1", 1, monitor_get_reg },
2906 { "o2", 2, monitor_get_reg },
2907 { "o3", 3, monitor_get_reg },
2908 { "o4", 4, monitor_get_reg },
2909 { "o5", 5, monitor_get_reg },
2910 { "o6", 6, monitor_get_reg },
2911 { "o7", 7, monitor_get_reg },
2912 { "l0", 8, monitor_get_reg },
2913 { "l1", 9, monitor_get_reg },
2914 { "l2", 10, monitor_get_reg },
2915 { "l3", 11, monitor_get_reg },
2916 { "l4", 12, monitor_get_reg },
2917 { "l5", 13, monitor_get_reg },
2918 { "l6", 14, monitor_get_reg },
2919 { "l7", 15, monitor_get_reg },
2920 { "i0", 16, monitor_get_reg },
2921 { "i1", 17, monitor_get_reg },
2922 { "i2", 18, monitor_get_reg },
2923 { "i3", 19, monitor_get_reg },
2924 { "i4", 20, monitor_get_reg },
2925 { "i5", 21, monitor_get_reg },
2926 { "i6", 22, monitor_get_reg },
2927 { "i7", 23, monitor_get_reg },
2928 { "pc", offsetof(CPUState, pc) },
2929 { "npc", offsetof(CPUState, npc) },
2930 { "y", offsetof(CPUState, y) },
2931 #ifndef TARGET_SPARC64
2932 { "psr", 0, &monitor_get_psr, },
2933 { "wim", offsetof(CPUState, wim) },
2935 { "tbr", offsetof(CPUState, tbr) },
2936 { "fsr", offsetof(CPUState, fsr) },
2937 { "f0", offsetof(CPUState, fpr[0].l.upper) },
2938 { "f1", offsetof(CPUState, fpr[0].l.lower) },
2939 { "f2", offsetof(CPUState, fpr[1].l.upper) },
2940 { "f3", offsetof(CPUState, fpr[1].l.lower) },
2941 { "f4", offsetof(CPUState, fpr[2].l.upper) },
2942 { "f5", offsetof(CPUState, fpr[2].l.lower) },
2943 { "f6", offsetof(CPUState, fpr[3].l.upper) },
2944 { "f7", offsetof(CPUState, fpr[3].l.lower) },
2945 { "f8", offsetof(CPUState, fpr[4].l.upper) },
2946 { "f9", offsetof(CPUState, fpr[4].l.lower) },
2947 { "f10", offsetof(CPUState, fpr[5].l.upper) },
2948 { "f11", offsetof(CPUState, fpr[5].l.lower) },
2949 { "f12", offsetof(CPUState, fpr[6].l.upper) },
2950 { "f13", offsetof(CPUState, fpr[6].l.lower) },
2951 { "f14", offsetof(CPUState, fpr[7].l.upper) },
2952 { "f15", offsetof(CPUState, fpr[7].l.lower) },
2953 { "f16", offsetof(CPUState, fpr[8].l.upper) },
2954 { "f17", offsetof(CPUState, fpr[8].l.lower) },
2955 { "f18", offsetof(CPUState, fpr[9].l.upper) },
2956 { "f19", offsetof(CPUState, fpr[9].l.lower) },
2957 { "f20", offsetof(CPUState, fpr[10].l.upper) },
2958 { "f21", offsetof(CPUState, fpr[10].l.lower) },
2959 { "f22", offsetof(CPUState, fpr[11].l.upper) },
2960 { "f23", offsetof(CPUState, fpr[11].l.lower) },
2961 { "f24", offsetof(CPUState, fpr[12].l.upper) },
2962 { "f25", offsetof(CPUState, fpr[12].l.lower) },
2963 { "f26", offsetof(CPUState, fpr[13].l.upper) },
2964 { "f27", offsetof(CPUState, fpr[13].l.lower) },
2965 { "f28", offsetof(CPUState, fpr[14].l.upper) },
2966 { "f29", offsetof(CPUState, fpr[14].l.lower) },
2967 { "f30", offsetof(CPUState, fpr[15].l.upper) },
2968 { "f31", offsetof(CPUState, fpr[15].l.lower) },
2969 #ifdef TARGET_SPARC64
2970 { "f32", offsetof(CPUState, fpr[16]) },
2971 { "f34", offsetof(CPUState, fpr[17]) },
2972 { "f36", offsetof(CPUState, fpr[18]) },
2973 { "f38", offsetof(CPUState, fpr[19]) },
2974 { "f40", offsetof(CPUState, fpr[20]) },
2975 { "f42", offsetof(CPUState, fpr[21]) },
2976 { "f44", offsetof(CPUState, fpr[22]) },
2977 { "f46", offsetof(CPUState, fpr[23]) },
2978 { "f48", offsetof(CPUState, fpr[24]) },
2979 { "f50", offsetof(CPUState, fpr[25]) },
2980 { "f52", offsetof(CPUState, fpr[26]) },
2981 { "f54", offsetof(CPUState, fpr[27]) },
2982 { "f56", offsetof(CPUState, fpr[28]) },
2983 { "f58", offsetof(CPUState, fpr[29]) },
2984 { "f60", offsetof(CPUState, fpr[30]) },
2985 { "f62", offsetof(CPUState, fpr[31]) },
2986 { "asi", offsetof(CPUState, asi) },
2987 { "pstate", offsetof(CPUState, pstate) },
2988 { "cansave", offsetof(CPUState, cansave) },
2989 { "canrestore", offsetof(CPUState, canrestore) },
2990 { "otherwin", offsetof(CPUState, otherwin) },
2991 { "wstate", offsetof(CPUState, wstate) },
2992 { "cleanwin", offsetof(CPUState, cleanwin) },
2993 { "fprs", offsetof(CPUState, fprs) },
2999 static void expr_error(Monitor *mon, const char *msg)
3001 monitor_printf(mon, "%s\n", msg);
3002 longjmp(expr_env, 1);
3005 /* return 0 if OK, -1 if not found */
3006 static int get_monitor_def(target_long *pval, const char *name)
3008 const MonitorDef *md;
3011 for(md = monitor_defs; md->name != NULL; md++) {
3012 if (compare_cmd(name, md->name)) {
3013 if (md->get_value) {
3014 *pval = md->get_value(md, md->offset);
3016 CPUState *env = mon_get_cpu();
3017 ptr = (uint8_t *)env + md->offset;
3020 *pval = *(int32_t *)ptr;
3023 *pval = *(target_long *)ptr;
3036 static void next(void)
3040 while (qemu_isspace(*pch))
3045 static int64_t expr_sum(Monitor *mon);
3047 static int64_t expr_unary(Monitor *mon)
3056 n = expr_unary(mon);
3060 n = -expr_unary(mon);
3064 n = ~expr_unary(mon);
3070 expr_error(mon, "')' expected");
3077 expr_error(mon, "character constant expected");
3081 expr_error(mon, "missing terminating \' character");
3091 while ((*pch >= 'a' && *pch <= 'z') ||
3092 (*pch >= 'A' && *pch <= 'Z') ||
3093 (*pch >= '0' && *pch <= '9') ||
3094 *pch == '_' || *pch == '.') {
3095 if ((q - buf) < sizeof(buf) - 1)
3099 while (qemu_isspace(*pch))
3102 ret = get_monitor_def(®, buf);
3104 expr_error(mon, "unknown register");
3109 expr_error(mon, "unexpected end of expression");
3113 #if TARGET_PHYS_ADDR_BITS > 32
3114 n = strtoull(pch, &p, 0);
3116 n = strtoul(pch, &p, 0);
3119 expr_error(mon, "invalid char in expression");
3122 while (qemu_isspace(*pch))
3130 static int64_t expr_prod(Monitor *mon)
3135 val = expr_unary(mon);
3138 if (op != '*' && op != '/' && op != '%')
3141 val2 = expr_unary(mon);
3150 expr_error(mon, "division by zero");
3161 static int64_t expr_logic(Monitor *mon)
3166 val = expr_prod(mon);
3169 if (op != '&' && op != '|' && op != '^')
3172 val2 = expr_prod(mon);
3189 static int64_t expr_sum(Monitor *mon)
3194 val = expr_logic(mon);
3197 if (op != '+' && op != '-')
3200 val2 = expr_logic(mon);
3209 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
3212 if (setjmp(expr_env)) {
3216 while (qemu_isspace(*pch))
3218 *pval = expr_sum(mon);
3223 static int get_double(Monitor *mon, double *pval, const char **pp)
3225 const char *p = *pp;
3229 d = strtod(p, &tailp);
3231 monitor_printf(mon, "Number expected\n");
3234 if (d != d || d - d != 0) {
3235 /* NaN or infinity */
3236 monitor_printf(mon, "Bad number\n");
3244 static int get_str(char *buf, int buf_size, const char **pp)
3252 while (qemu_isspace(*p))
3262 while (*p != '\0' && *p != '\"') {
3278 qemu_printf("unsupported escape code: '\\%c'\n", c);
3281 if ((q - buf) < buf_size - 1) {
3285 if ((q - buf) < buf_size - 1) {
3292 qemu_printf("unterminated string\n");
3297 while (*p != '\0' && !qemu_isspace(*p)) {
3298 if ((q - buf) < buf_size - 1) {
3310 * Store the command-name in cmdname, and return a pointer to
3311 * the remaining of the command string.
3313 static const char *get_command_name(const char *cmdline,
3314 char *cmdname, size_t nlen)
3317 const char *p, *pstart;
3320 while (qemu_isspace(*p))
3325 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
3330 memcpy(cmdname, pstart, len);
3331 cmdname[len] = '\0';
3336 * Read key of 'type' into 'key' and return the current
3339 static char *key_get_info(const char *type, char **key)
3347 p = strchr(type, ':');
3354 str = g_malloc(len + 1);
3355 memcpy(str, type, len);
3362 static int default_fmt_format = 'x';
3363 static int default_fmt_size = 4;
3367 static int is_valid_option(const char *c, const char *typestr)
3375 typestr = strstr(typestr, option);
3376 return (typestr != NULL);
3379 static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
3380 const char *cmdname)
3382 const mon_cmd_t *cmd;
3384 for (cmd = disp_table; cmd->name != NULL; cmd++) {
3385 if (compare_cmd(cmdname, cmd->name)) {
3393 static const mon_cmd_t *monitor_find_command(const char *cmdname)
3395 return search_dispatch_table(mon_cmds, cmdname);
3398 static const mon_cmd_t *qmp_find_cmd(const char *cmdname)
3400 return search_dispatch_table(qmp_cmds, cmdname);
3403 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
3404 const char *cmdline,
3407 const char *p, *typestr;
3409 const mon_cmd_t *cmd;
3415 monitor_printf(mon, "command='%s'\n", cmdline);
3418 /* extract the command name */
3419 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3423 cmd = monitor_find_command(cmdname);
3425 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
3429 /* parse the parameters */
3430 typestr = cmd->args_type;
3432 typestr = key_get_info(typestr, &key);
3444 while (qemu_isspace(*p))
3446 if (*typestr == '?') {
3449 /* no optional string: NULL argument */
3453 ret = get_str(buf, sizeof(buf), &p);
3457 monitor_printf(mon, "%s: filename expected\n",
3461 monitor_printf(mon, "%s: block device name expected\n",
3465 monitor_printf(mon, "%s: string expected\n", cmdname);
3470 qdict_put(qdict, key, qstring_from_str(buf));
3475 QemuOptsList *opts_list;
3478 opts_list = qemu_find_opts(key);
3479 if (!opts_list || opts_list->desc->name) {
3482 while (qemu_isspace(*p)) {
3487 if (get_str(buf, sizeof(buf), &p) < 0) {
3490 opts = qemu_opts_parse(opts_list, buf, 1);
3494 qemu_opts_to_qdict(opts, qdict);
3495 qemu_opts_del(opts);
3500 int count, format, size;
3502 while (qemu_isspace(*p))
3508 if (qemu_isdigit(*p)) {
3510 while (qemu_isdigit(*p)) {
3511 count = count * 10 + (*p - '0');
3549 if (*p != '\0' && !qemu_isspace(*p)) {
3550 monitor_printf(mon, "invalid char in format: '%c'\n",
3555 format = default_fmt_format;
3556 if (format != 'i') {
3557 /* for 'i', not specifying a size gives -1 as size */
3559 size = default_fmt_size;
3560 default_fmt_size = size;
3562 default_fmt_format = format;
3565 format = default_fmt_format;
3566 if (format != 'i') {
3567 size = default_fmt_size;
3572 qdict_put(qdict, "count", qint_from_int(count));
3573 qdict_put(qdict, "format", qint_from_int(format));
3574 qdict_put(qdict, "size", qint_from_int(size));
3583 while (qemu_isspace(*p))
3585 if (*typestr == '?' || *typestr == '.') {
3586 if (*typestr == '?') {
3594 while (qemu_isspace(*p))
3603 if (get_expr(mon, &val, &p))
3605 /* Check if 'i' is greater than 32-bit */
3606 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3607 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3608 monitor_printf(mon, "integer is for 32-bit values\n");
3610 } else if (c == 'M') {
3613 qdict_put(qdict, key, qint_from_int(val));
3621 while (qemu_isspace(*p)) {
3624 if (*typestr == '?') {
3630 val = strtosz(p, &end);
3632 monitor_printf(mon, "invalid size\n");
3635 qdict_put(qdict, key, qint_from_int(val));
3643 while (qemu_isspace(*p))
3645 if (*typestr == '?') {
3651 if (get_double(mon, &val, &p) < 0) {
3654 if (p[0] && p[1] == 's') {
3657 val /= 1e3; p += 2; break;
3659 val /= 1e6; p += 2; break;
3661 val /= 1e9; p += 2; break;
3664 if (*p && !qemu_isspace(*p)) {
3665 monitor_printf(mon, "Unknown unit suffix\n");
3668 qdict_put(qdict, key, qfloat_from_double(val));
3676 while (qemu_isspace(*p)) {
3680 while (qemu_isgraph(*p)) {
3683 if (p - beg == 2 && !memcmp(beg, "on", p - beg)) {
3685 } else if (p - beg == 3 && !memcmp(beg, "off", p - beg)) {
3688 monitor_printf(mon, "Expected 'on' or 'off'\n");
3691 qdict_put(qdict, key, qbool_from_int(val));
3696 const char *tmp = p;
3703 while (qemu_isspace(*p))
3708 if(!is_valid_option(p, typestr)) {
3710 monitor_printf(mon, "%s: unsupported option -%c\n",
3722 qdict_put(qdict, key, qbool_from_int(1));
3729 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3735 /* check that all arguments were parsed */
3736 while (qemu_isspace(*p))
3739 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3751 void monitor_set_error(Monitor *mon, QError *qerror)
3753 /* report only the first error */
3755 mon->error = qerror;
3757 MON_DEBUG("Additional error report at %s:%d\n",
3758 qerror->file, qerror->linenr);
3763 static void handler_audit(Monitor *mon, const mon_cmd_t *cmd, int ret)
3765 if (ret && !monitor_has_error(mon)) {
3767 * If it returns failure, it must have passed on error.
3769 * Action: Report an internal error to the client if in QMP.
3771 qerror_report(QERR_UNDEFINED_ERROR);
3772 MON_DEBUG("command '%s' returned failure but did not pass an error\n",
3776 #ifdef CONFIG_DEBUG_MONITOR
3777 if (!ret && monitor_has_error(mon)) {
3779 * If it returns success, it must not have passed an error.
3781 * Action: Report the passed error to the client.
3783 MON_DEBUG("command '%s' returned success but passed an error\n",
3787 if (mon_print_count_get(mon) > 0 && strcmp(cmd->name, "info") != 0) {
3789 * Handlers should not call Monitor print functions.
3791 * Action: Ignore them in QMP.
3793 * (XXX: we don't check any 'info' or 'query' command here
3794 * because the user print function _is_ called by do_info(), hence
3795 * we will trigger this check. This problem will go away when we
3796 * make 'query' commands real and kill do_info())
3798 MON_DEBUG("command '%s' called print functions %d time(s)\n",
3799 cmd->name, mon_print_count_get(mon));
3804 static void handle_user_command(Monitor *mon, const char *cmdline)
3807 const mon_cmd_t *cmd;
3809 qdict = qdict_new();
3811 cmd = monitor_parse_command(mon, cmdline, qdict);
3815 if (handler_is_async(cmd)) {
3816 user_async_cmd_handler(mon, cmd, qdict);
3817 } else if (handler_is_qobject(cmd)) {
3818 QObject *data = NULL;
3820 /* XXX: ignores the error code */
3821 cmd->mhandler.cmd_new(mon, qdict, &data);
3822 assert(!monitor_has_error(mon));
3824 cmd->user_print(mon, data);
3825 qobject_decref(data);
3828 cmd->mhandler.cmd(mon, qdict);
3835 static void cmd_completion(const char *name, const char *list)
3837 const char *p, *pstart;
3846 p = pstart + strlen(pstart);
3848 if (len > sizeof(cmd) - 2)
3849 len = sizeof(cmd) - 2;
3850 memcpy(cmd, pstart, len);
3852 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3853 readline_add_completion(cur_mon->rs, cmd);
3861 static void file_completion(const char *input)
3866 char file[1024], file_prefix[1024];
3870 p = strrchr(input, '/');
3873 pstrcpy(file_prefix, sizeof(file_prefix), input);
3874 pstrcpy(path, sizeof(path), ".");
3876 input_path_len = p - input + 1;
3877 memcpy(path, input, input_path_len);
3878 if (input_path_len > sizeof(path) - 1)
3879 input_path_len = sizeof(path) - 1;
3880 path[input_path_len] = '\0';
3881 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3883 #ifdef DEBUG_COMPLETION
3884 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3885 input, path, file_prefix);
3887 ffs = opendir(path);
3896 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) {
3900 if (strstart(d->d_name, file_prefix, NULL)) {
3901 memcpy(file, input, input_path_len);
3902 if (input_path_len < sizeof(file))
3903 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3905 /* stat the file to find out if it's a directory.
3906 * In that case add a slash to speed up typing long paths
3908 if (stat(file, &sb) == 0 && S_ISDIR(sb.st_mode)) {
3909 pstrcat(file, sizeof(file), "/");
3911 readline_add_completion(cur_mon->rs, file);
3917 static void block_completion_it(void *opaque, BlockDriverState *bs)
3919 const char *name = bdrv_get_device_name(bs);
3920 const char *input = opaque;
3922 if (input[0] == '\0' ||
3923 !strncmp(name, (char *)input, strlen(input))) {
3924 readline_add_completion(cur_mon->rs, name);
3928 /* NOTE: this parser is an approximate form of the real command parser */
3929 static void parse_cmdline(const char *cmdline,
3930 int *pnb_args, char **args)
3939 while (qemu_isspace(*p))
3943 if (nb_args >= MAX_ARGS)
3945 ret = get_str(buf, sizeof(buf), &p);
3946 args[nb_args] = g_strdup(buf);
3951 *pnb_args = nb_args;
3954 static const char *next_arg_type(const char *typestr)
3956 const char *p = strchr(typestr, ':');
3957 return (p != NULL ? ++p : typestr);
3960 static void monitor_find_completion(const char *cmdline)
3962 const char *cmdname;
3963 char *args[MAX_ARGS];
3964 int nb_args, i, len;
3965 const char *ptype, *str;
3966 const mon_cmd_t *cmd;
3969 parse_cmdline(cmdline, &nb_args, args);
3970 #ifdef DEBUG_COMPLETION
3971 for(i = 0; i < nb_args; i++) {
3972 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3976 /* if the line ends with a space, it means we want to complete the
3978 len = strlen(cmdline);
3979 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3980 if (nb_args >= MAX_ARGS) {
3983 args[nb_args++] = g_strdup("");
3986 /* command completion */
3991 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3992 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3993 cmd_completion(cmdname, cmd->name);
3996 /* find the command */
3997 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3998 if (compare_cmd(args[0], cmd->name)) {
4006 ptype = next_arg_type(cmd->args_type);
4007 for(i = 0; i < nb_args - 2; i++) {
4008 if (*ptype != '\0') {
4009 ptype = next_arg_type(ptype);
4010 while (*ptype == '?')
4011 ptype = next_arg_type(ptype);
4014 str = args[nb_args - 1];
4015 if (*ptype == '-' && ptype[1] != '\0') {
4016 ptype = next_arg_type(ptype);
4020 /* file completion */
4021 readline_set_completion_index(cur_mon->rs, strlen(str));
4022 file_completion(str);
4025 /* block device name completion */
4026 readline_set_completion_index(cur_mon->rs, strlen(str));
4027 bdrv_iterate(block_completion_it, (void *)str);
4030 /* XXX: more generic ? */
4031 if (!strcmp(cmd->name, "info")) {
4032 readline_set_completion_index(cur_mon->rs, strlen(str));
4033 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
4034 cmd_completion(str, cmd->name);
4036 } else if (!strcmp(cmd->name, "sendkey")) {
4037 char *sep = strrchr(str, '-');
4040 readline_set_completion_index(cur_mon->rs, strlen(str));
4041 for(key = key_defs; key->name != NULL; key++) {
4042 cmd_completion(str, key->name);
4044 } else if (!strcmp(cmd->name, "help|?")) {
4045 readline_set_completion_index(cur_mon->rs, strlen(str));
4046 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
4047 cmd_completion(str, cmd->name);
4057 for (i = 0; i < nb_args; i++) {
4062 static int monitor_can_read(void *opaque)
4064 Monitor *mon = opaque;
4066 return (mon->suspend_cnt == 0) ? 1 : 0;
4069 static int invalid_qmp_mode(const Monitor *mon, const char *cmd_name)
4071 int is_cap = compare_cmd(cmd_name, "qmp_capabilities");
4072 return (qmp_cmd_mode(mon) ? is_cap : !is_cap);
4076 * Argument validation rules:
4078 * 1. The argument must exist in cmd_args qdict
4079 * 2. The argument type must be the expected one
4081 * Special case: If the argument doesn't exist in cmd_args and
4082 * the QMP_ACCEPT_UNKNOWNS flag is set, then the
4083 * checking is skipped for it.
4085 static int check_client_args_type(const QDict *client_args,
4086 const QDict *cmd_args, int flags)
4088 const QDictEntry *ent;
4090 for (ent = qdict_first(client_args); ent;ent = qdict_next(client_args,ent)){
4093 const QObject *client_arg = qdict_entry_value(ent);
4094 const char *client_arg_name = qdict_entry_key(ent);
4096 obj = qdict_get(cmd_args, client_arg_name);
4098 if (flags & QMP_ACCEPT_UNKNOWNS) {
4099 /* handler accepts unknowns */
4102 /* client arg doesn't exist */
4103 qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
4107 arg_type = qobject_to_qstring(obj);
4108 assert(arg_type != NULL);
4110 /* check if argument's type is correct */
4111 switch (qstring_get_str(arg_type)[0]) {
4115 if (qobject_type(client_arg) != QTYPE_QSTRING) {
4116 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4125 if (qobject_type(client_arg) != QTYPE_QINT) {
4126 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4132 if (qobject_type(client_arg) != QTYPE_QINT &&
4133 qobject_type(client_arg) != QTYPE_QFLOAT) {
4134 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4141 if (qobject_type(client_arg) != QTYPE_QBOOL) {
4142 qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
4148 assert(flags & QMP_ACCEPT_UNKNOWNS);
4153 * These types are not supported by QMP and thus are not
4154 * handled here. Fall through.
4165 * - Check if the client has passed all mandatory args
4166 * - Set special flags for argument validation
4168 static int check_mandatory_args(const QDict *cmd_args,
4169 const QDict *client_args, int *flags)
4171 const QDictEntry *ent;
4173 for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
4174 const char *cmd_arg_name = qdict_entry_key(ent);
4175 QString *type = qobject_to_qstring(qdict_entry_value(ent));
4176 assert(type != NULL);
4178 if (qstring_get_str(type)[0] == 'O') {
4179 assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
4180 *flags |= QMP_ACCEPT_UNKNOWNS;
4181 } else if (qstring_get_str(type)[0] != '-' &&
4182 qstring_get_str(type)[1] != '?' &&
4183 !qdict_haskey(client_args, cmd_arg_name)) {
4184 qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
4192 static QDict *qdict_from_args_type(const char *args_type)
4196 QString *key, *type, *cur_qs;
4198 assert(args_type != NULL);
4200 qdict = qdict_new();
4202 if (args_type == NULL || args_type[0] == '\0') {
4203 /* no args, empty qdict */
4207 key = qstring_new();
4208 type = qstring_new();
4213 switch (args_type[i]) {
4216 qdict_put(qdict, qstring_get_str(key), type);
4218 if (args_type[i] == '\0') {
4221 type = qstring_new(); /* qdict has ref */
4222 cur_qs = key = qstring_new();
4228 qstring_append_chr(cur_qs, args_type[i]);
4238 * Client argument checking rules:
4240 * 1. Client must provide all mandatory arguments
4241 * 2. Each argument provided by the client must be expected
4242 * 3. Each argument provided by the client must have the type expected
4245 static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args)
4250 cmd_args = qdict_from_args_type(cmd->args_type);
4253 err = check_mandatory_args(cmd_args, client_args, &flags);
4258 err = check_client_args_type(client_args, cmd_args, flags);
4266 * Input object checking rules
4268 * 1. Input object must be a dict
4269 * 2. The "execute" key must exist
4270 * 3. The "execute" key must be a string
4271 * 4. If the "arguments" key exists, it must be a dict
4272 * 5. If the "id" key exists, it can be anything (ie. json-value)
4273 * 6. Any argument not listed above is considered invalid
4275 static QDict *qmp_check_input_obj(QObject *input_obj)
4277 const QDictEntry *ent;
4278 int has_exec_key = 0;
4281 if (qobject_type(input_obj) != QTYPE_QDICT) {
4282 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
4286 input_dict = qobject_to_qdict(input_obj);
4288 for (ent = qdict_first(input_dict); ent; ent = qdict_next(input_dict, ent)){
4289 const char *arg_name = qdict_entry_key(ent);
4290 const QObject *arg_obj = qdict_entry_value(ent);
4292 if (!strcmp(arg_name, "execute")) {
4293 if (qobject_type(arg_obj) != QTYPE_QSTRING) {
4294 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
4299 } else if (!strcmp(arg_name, "arguments")) {
4300 if (qobject_type(arg_obj) != QTYPE_QDICT) {
4301 qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
4305 } else if (!strcmp(arg_name, "id")) {
4306 /* FIXME: check duplicated IDs for async commands */
4308 qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
4313 if (!has_exec_key) {
4314 qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
4321 static void qmp_call_cmd(Monitor *mon, const mon_cmd_t *cmd,
4322 const QDict *params)
4325 QObject *data = NULL;
4327 mon_print_count_init(mon);
4329 ret = cmd->mhandler.cmd_new(mon, params, &data);
4330 handler_audit(mon, cmd, ret);
4331 monitor_protocol_emitter(mon, data);
4332 qobject_decref(data);
4335 static void handle_qmp_command(JSONMessageParser *parser, QList *tokens)
4339 QDict *input, *args;
4340 const mon_cmd_t *cmd;
4341 const char *cmd_name;
4342 Monitor *mon = cur_mon;
4344 args = input = NULL;
4346 obj = json_parser_parse(tokens, NULL);
4348 // FIXME: should be triggered in json_parser_parse()
4349 qerror_report(QERR_JSON_PARSING);
4353 input = qmp_check_input_obj(obj);
4355 qobject_decref(obj);
4359 mon->mc->id = qdict_get(input, "id");
4360 qobject_incref(mon->mc->id);
4362 cmd_name = qdict_get_str(input, "execute");
4363 trace_handle_qmp_command(mon, cmd_name);
4364 if (invalid_qmp_mode(mon, cmd_name)) {
4365 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4369 cmd = qmp_find_cmd(cmd_name);
4371 qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
4375 obj = qdict_get(input, "arguments");
4379 args = qobject_to_qdict(obj);
4383 err = qmp_check_client_args(cmd, args);
4388 if (handler_is_async(cmd)) {
4389 err = qmp_async_cmd_handler(mon, cmd, args);
4391 /* emit the error response */
4395 qmp_call_cmd(mon, cmd, args);
4401 monitor_protocol_emitter(mon, NULL);
4408 * monitor_control_read(): Read and handle QMP input
4410 static void monitor_control_read(void *opaque, const uint8_t *buf, int size)
4412 Monitor *old_mon = cur_mon;
4416 json_message_parser_feed(&cur_mon->mc->parser, (const char *) buf, size);
4421 static void monitor_read(void *opaque, const uint8_t *buf, int size)
4423 Monitor *old_mon = cur_mon;
4429 for (i = 0; i < size; i++)
4430 readline_handle_byte(cur_mon->rs, buf[i]);
4432 if (size == 0 || buf[size - 1] != 0)
4433 monitor_printf(cur_mon, "corrupted command\n");
4435 handle_user_command(cur_mon, (char *)buf);
4441 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
4443 monitor_suspend(mon);
4444 handle_user_command(mon, cmdline);
4445 monitor_resume(mon);
4448 int monitor_suspend(Monitor *mon)
4456 void monitor_resume(Monitor *mon)
4460 if (--mon->suspend_cnt == 0)
4461 readline_show_prompt(mon->rs);
4464 static QObject *get_qmp_greeting(void)
4466 QObject *ver = NULL;
4468 qmp_marshal_input_query_version(NULL, NULL, &ver);
4469 return qobject_from_jsonf("{'QMP':{'version': %p,'capabilities': []}}",ver);
4473 * monitor_control_event(): Print QMP gretting
4475 static void monitor_control_event(void *opaque, int event)
4478 Monitor *mon = opaque;
4481 case CHR_EVENT_OPENED:
4482 mon->mc->command_mode = 0;
4483 json_message_parser_init(&mon->mc->parser, handle_qmp_command);
4484 data = get_qmp_greeting();
4485 monitor_json_emitter(mon, data);
4486 qobject_decref(data);
4488 case CHR_EVENT_CLOSED:
4489 json_message_parser_destroy(&mon->mc->parser);
4494 static void monitor_event(void *opaque, int event)
4496 Monitor *mon = opaque;
4499 case CHR_EVENT_MUX_IN:
4501 if (mon->reset_seen) {
4502 readline_restart(mon->rs);
4503 monitor_resume(mon);
4506 mon->suspend_cnt = 0;
4510 case CHR_EVENT_MUX_OUT:
4511 if (mon->reset_seen) {
4512 if (mon->suspend_cnt == 0) {
4513 monitor_printf(mon, "\n");
4516 monitor_suspend(mon);
4523 case CHR_EVENT_OPENED:
4524 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
4525 "information\n", QEMU_VERSION);
4526 if (!mon->mux_out) {
4527 readline_show_prompt(mon->rs);
4529 mon->reset_seen = 1;
4535 compare_mon_cmd(const void *a, const void *b)
4537 return strcmp(((const mon_cmd_t *)a)->name,
4538 ((const mon_cmd_t *)b)->name);
4541 static void sortcmdlist(void)
4544 int elem_size = sizeof(mon_cmd_t);
4546 array_num = sizeof(mon_cmds)/elem_size-1;
4547 qsort((void *)mon_cmds, array_num, elem_size, compare_mon_cmd);
4549 array_num = sizeof(info_cmds)/elem_size-1;
4550 qsort((void *)info_cmds, array_num, elem_size, compare_mon_cmd);
4562 void monitor_init(CharDriverState *chr, int flags)
4564 static int is_first_init = 1;
4567 if (is_first_init) {
4568 key_timer = qemu_new_timer_ns(vm_clock, release_keys, NULL);
4572 mon = g_malloc0(sizeof(*mon));
4576 if (flags & MONITOR_USE_READLINE) {
4577 mon->rs = readline_init(mon, monitor_find_completion);
4578 monitor_read_command(mon, 0);
4581 if (monitor_ctrl_mode(mon)) {
4582 mon->mc = g_malloc0(sizeof(MonitorControl));
4583 /* Control mode requires special handlers */
4584 qemu_chr_add_handlers(chr, monitor_can_read, monitor_control_read,
4585 monitor_control_event, mon);
4586 qemu_chr_fe_set_echo(chr, true);
4588 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read,
4589 monitor_event, mon);
4592 QLIST_INSERT_HEAD(&mon_list, mon, entry);
4593 if (!default_mon || (flags & MONITOR_IS_DEFAULT))
4599 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
4601 BlockDriverState *bs = opaque;
4604 if (bdrv_set_key(bs, password) != 0) {
4605 monitor_printf(mon, "invalid password\n");
4608 if (mon->password_completion_cb)
4609 mon->password_completion_cb(mon->password_opaque, ret);
4611 monitor_read_command(mon, 1);
4614 ReadLineState *monitor_get_rs(Monitor *mon)
4619 int monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
4620 BlockDriverCompletionFunc *completion_cb,
4625 if (!bdrv_key_required(bs)) {
4627 completion_cb(opaque, 0);
4631 if (monitor_ctrl_mode(mon)) {
4632 qerror_report(QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
4633 bdrv_get_encrypted_filename(bs));
4637 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
4638 bdrv_get_encrypted_filename(bs));
4640 mon->password_completion_cb = completion_cb;
4641 mon->password_opaque = opaque;
4643 err = monitor_read_password(mon, bdrv_password_cb, bs);
4645 if (err && completion_cb)
4646 completion_cb(opaque, err);
4651 int monitor_read_block_device_key(Monitor *mon, const char *device,
4652 BlockDriverCompletionFunc *completion_cb,
4655 BlockDriverState *bs;
4657 bs = bdrv_find(device);
4659 monitor_printf(mon, "Device not found %s\n", device);
4663 return monitor_read_bdrv_key_start(mon, bs, completion_cb, opaque);