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 "qemu-char.h"
41 #include "audio/audio.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
55 //#define DEBUG_COMPLETION
61 * 'B' block device name
62 * 's' string (accept optional quote)
64 * 'l' target long (32 or 64 bit)
65 * '/' optional gdb-like print format (like "/10x")
67 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
73 typedef struct mon_cmd_t {
75 const char *args_type;
78 void (*user_print)(Monitor *mon, const QObject *data);
80 void (*info)(Monitor *mon);
81 void (*info_new)(Monitor *mon, QObject **ret_data);
82 void (*cmd)(Monitor *mon, const QDict *qdict);
83 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
87 /* file descriptors passed via SCM_RIGHTS */
88 typedef struct mon_fd_t mon_fd_t;
92 QLIST_ENTRY(mon_fd_t) next;
101 uint8_t outbuf[1024];
105 BlockDriverCompletionFunc *password_completion_cb;
106 void *password_opaque;
108 QLIST_HEAD(,mon_fd_t) fds;
109 QLIST_ENTRY(Monitor) entry;
112 static QLIST_HEAD(mon_list, Monitor) mon_list;
114 static const mon_cmd_t mon_cmds[];
115 static const mon_cmd_t info_cmds[];
117 Monitor *cur_mon = NULL;
119 static void monitor_command_cb(Monitor *mon, const char *cmdline,
122 /* Return true if in control mode, false otherwise */
123 static inline int monitor_ctrl_mode(const Monitor *mon)
125 return (mon->flags & MONITOR_USE_CONTROL);
128 static void monitor_read_command(Monitor *mon, int show_prompt)
130 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
132 readline_show_prompt(mon->rs);
135 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
139 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
140 /* prompt is printed on return from the command handler */
143 monitor_printf(mon, "terminal does not support password prompting\n");
148 void monitor_flush(Monitor *mon)
150 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
151 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
152 mon->outbuf_index = 0;
156 /* flush at every end of line or if the buffer is full */
157 static void monitor_puts(Monitor *mon, const char *str)
169 mon->outbuf[mon->outbuf_index++] = '\r';
170 mon->outbuf[mon->outbuf_index++] = c;
171 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
177 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
180 vsnprintf(buf, sizeof(buf), fmt, ap);
181 monitor_puts(mon, buf);
184 void monitor_printf(Monitor *mon, const char *fmt, ...)
188 monitor_vprintf(mon, fmt, ap);
192 void monitor_print_filename(Monitor *mon, const char *filename)
196 for (i = 0; filename[i]; i++) {
197 switch (filename[i]) {
201 monitor_printf(mon, "\\%c", filename[i]);
204 monitor_printf(mon, "\\t");
207 monitor_printf(mon, "\\r");
210 monitor_printf(mon, "\\n");
213 monitor_printf(mon, "%c", filename[i]);
219 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
223 monitor_vprintf((Monitor *)stream, fmt, ap);
228 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
230 static inline int monitor_handler_ported(const mon_cmd_t *cmd)
232 return cmd->user_print != NULL;
235 static inline int monitor_has_error(const Monitor *mon)
237 return mon->error != NULL;
240 static void monitor_print_qobject(Monitor *mon, const QObject *data)
242 switch (qobject_type(data)) {
244 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
247 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
250 monitor_printf(mon, "ERROR: unsupported type: %d",
255 monitor_puts(mon, "\n");
258 static int compare_cmd(const char *name, const char *list)
260 const char *p, *pstart;
268 p = pstart + strlen(pstart);
269 if ((p - pstart) == len && !memcmp(pstart, name, len))
278 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
279 const char *prefix, const char *name)
281 const mon_cmd_t *cmd;
283 for(cmd = cmds; cmd->name != NULL; cmd++) {
284 if (!name || !strcmp(name, cmd->name))
285 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
286 cmd->params, cmd->help);
290 static void help_cmd(Monitor *mon, const char *name)
292 if (name && !strcmp(name, "info")) {
293 help_cmd_dump(mon, info_cmds, "info ", NULL);
295 help_cmd_dump(mon, mon_cmds, "", name);
296 if (name && !strcmp(name, "log")) {
297 const CPULogItem *item;
298 monitor_printf(mon, "Log items (comma separated):\n");
299 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
300 for(item = cpu_log_items; item->mask != 0; item++) {
301 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
307 static void do_help_cmd(Monitor *mon, const QDict *qdict)
309 help_cmd(mon, qdict_get_try_str(qdict, "name"));
312 static void do_commit(Monitor *mon, const QDict *qdict)
316 const char *device = qdict_get_str(qdict, "device");
318 all_devices = !strcmp(device, "all");
319 QTAILQ_FOREACH(dinfo, &drives, next) {
321 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
323 bdrv_commit(dinfo->bdrv);
327 static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
329 const mon_cmd_t *cmd;
330 const char *item = qdict_get_try_str(qdict, "item");
335 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
336 if (compare_cmd(item, cmd->name))
340 if (cmd->name == NULL)
343 if (monitor_handler_ported(cmd)) {
344 cmd->mhandler.info_new(mon, ret_data);
346 cmd->user_print(mon, *ret_data);
348 cmd->mhandler.info(mon);
354 help_cmd(mon, "info");
358 * do_info_version(): Show QEMU version
360 static void do_info_version(Monitor *mon, QObject **ret_data)
362 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
365 static void do_info_name(Monitor *mon)
368 monitor_printf(mon, "%s\n", qemu_name);
372 * do_info_commands(): List QMP available commands
374 * Return a QList of QStrings.
376 static void do_info_commands(Monitor *mon, QObject **ret_data)
379 const mon_cmd_t *cmd;
381 cmd_list = qlist_new();
383 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
384 if (monitor_handler_ported(cmd) && !compare_cmd(cmd->name, "info")) {
385 qlist_append(cmd_list, qstring_from_str(cmd->name));
389 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
390 if (monitor_handler_ported(cmd)) {
392 snprintf(buf, sizeof(buf), "query-%s", cmd->name);
393 qlist_append(cmd_list, qstring_from_str(buf));
397 *ret_data = QOBJECT(cmd_list);
400 #if defined(TARGET_I386)
401 static void do_info_hpet(Monitor *mon)
403 monitor_printf(mon, "HPET is %s by QEMU\n",
404 (no_hpet) ? "disabled" : "enabled");
408 static void do_info_uuid(Monitor *mon)
410 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
411 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
412 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
413 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
414 qemu_uuid[14], qemu_uuid[15]);
417 /* get the current CPU defined by the user */
418 static int mon_set_cpu(int cpu_index)
422 for(env = first_cpu; env != NULL; env = env->next_cpu) {
423 if (env->cpu_index == cpu_index) {
424 cur_mon->mon_cpu = env;
431 static CPUState *mon_get_cpu(void)
433 if (!cur_mon->mon_cpu) {
436 cpu_synchronize_state(cur_mon->mon_cpu);
437 return cur_mon->mon_cpu;
440 static void do_info_registers(Monitor *mon)
447 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
450 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
455 static void print_cpu_iter(QObject *obj, void *opaque)
459 Monitor *mon = opaque;
461 assert(qobject_type(obj) == QTYPE_QDICT);
462 cpu = qobject_to_qdict(obj);
464 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
467 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
469 #if defined(TARGET_I386)
470 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
471 (target_ulong) qdict_get_int(cpu, "pc"));
472 #elif defined(TARGET_PPC)
473 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
474 (target_long) qdict_get_int(cpu, "nip"));
475 #elif defined(TARGET_SPARC)
476 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
477 (target_long) qdict_get_int(cpu, "pc"));
478 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
479 (target_long) qdict_get_int(cpu, "npc"));
480 #elif defined(TARGET_MIPS)
481 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
482 (target_long) qdict_get_int(cpu, "PC"));
485 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
486 monitor_printf(mon, " (halted)");
488 monitor_printf(mon, "\n");
491 static void monitor_print_cpus(Monitor *mon, const QObject *data)
495 assert(qobject_type(data) == QTYPE_QLIST);
496 cpu_list = qobject_to_qlist(data);
497 qlist_iter(cpu_list, print_cpu_iter, mon);
501 * do_info_cpus(): Show CPU information
503 * Return a QList with a QDict for each CPU.
507 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
508 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
510 static void do_info_cpus(Monitor *mon, QObject **ret_data)
515 cpu_list = qlist_new();
517 /* just to set the default cpu if not already done */
520 for(env = first_cpu; env != NULL; env = env->next_cpu) {
522 QDict *cpu = qdict_new();
524 cpu_synchronize_state(env);
526 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
527 answer = (env == mon->mon_cpu) ? "yes" : "no";
528 qdict_put(cpu, "current", qstring_from_str(answer));
530 #if defined(TARGET_I386)
531 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
532 #elif defined(TARGET_PPC)
533 qdict_put(cpu, "nip", qint_from_int(env->nip));
534 #elif defined(TARGET_SPARC)
535 qdict_put(cpu, "pc", qint_from_int(env->pc));
536 qdict_put(cpu, "npc", qint_from_int(env->npc));
537 #elif defined(TARGET_MIPS)
538 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
540 answer = env->halted ? "yes" : "no";
541 qdict_put(cpu, "halted", qstring_from_str(answer));
543 qlist_append(cpu_list, cpu);
546 *ret_data = QOBJECT(cpu_list);
549 static void do_cpu_set(Monitor *mon, const QDict *qdict)
551 int index = qdict_get_int(qdict, "index");
552 if (mon_set_cpu(index) < 0)
553 monitor_printf(mon, "Invalid CPU index\n");
556 static void do_info_jit(Monitor *mon)
558 dump_exec_info((FILE *)mon, monitor_fprintf);
561 static void do_info_history(Monitor *mon)
570 str = readline_get_history(mon->rs, i);
573 monitor_printf(mon, "%d: '%s'\n", i, str);
578 #if defined(TARGET_PPC)
579 /* XXX: not implemented in other targets */
580 static void do_info_cpu_stats(Monitor *mon)
585 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
590 * do_quit(): Quit QEMU execution
592 static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
597 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
599 if (bdrv_is_inserted(bs)) {
601 if (!bdrv_is_removable(bs)) {
602 monitor_printf(mon, "device is not removable\n");
605 if (bdrv_is_locked(bs)) {
606 monitor_printf(mon, "device is locked\n");
615 static void do_eject(Monitor *mon, const QDict *qdict, QObject **ret_data)
617 BlockDriverState *bs;
618 int force = qdict_get_int(qdict, "force");
619 const char *filename = qdict_get_str(qdict, "filename");
621 bs = bdrv_find(filename);
623 monitor_printf(mon, "device not found\n");
626 eject_device(mon, bs, force);
629 static void do_change_block(Monitor *mon, const char *device,
630 const char *filename, const char *fmt)
632 BlockDriverState *bs;
633 BlockDriver *drv = NULL;
635 bs = bdrv_find(device);
637 monitor_printf(mon, "device not found\n");
641 drv = bdrv_find_whitelisted_format(fmt);
643 monitor_printf(mon, "invalid format %s\n", fmt);
647 if (eject_device(mon, bs, 0) < 0)
649 bdrv_open2(bs, filename, 0, drv);
650 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
653 static void change_vnc_password_cb(Monitor *mon, const char *password,
656 if (vnc_display_password(NULL, password) < 0)
657 monitor_printf(mon, "could not set VNC server password\n");
659 monitor_read_command(mon, 1);
662 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
664 if (strcmp(target, "passwd") == 0 ||
665 strcmp(target, "password") == 0) {
668 strncpy(password, arg, sizeof(password));
669 password[sizeof(password) - 1] = '\0';
670 change_vnc_password_cb(mon, password, NULL);
672 monitor_read_password(mon, change_vnc_password_cb, NULL);
675 if (vnc_display_open(NULL, target) < 0)
676 monitor_printf(mon, "could not start VNC server on %s\n", target);
680 static void do_change(Monitor *mon, const QDict *qdict)
682 const char *device = qdict_get_str(qdict, "device");
683 const char *target = qdict_get_str(qdict, "target");
684 const char *arg = qdict_get_try_str(qdict, "arg");
685 if (strcmp(device, "vnc") == 0) {
686 do_change_vnc(mon, target, arg);
688 do_change_block(mon, device, target, arg);
692 static void do_screen_dump(Monitor *mon, const QDict *qdict)
694 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
697 static void do_logfile(Monitor *mon, const QDict *qdict)
699 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
702 static void do_log(Monitor *mon, const QDict *qdict)
705 const char *items = qdict_get_str(qdict, "items");
707 if (!strcmp(items, "none")) {
710 mask = cpu_str_to_log_mask(items);
712 help_cmd(mon, "log");
719 static void do_singlestep(Monitor *mon, const QDict *qdict)
721 const char *option = qdict_get_try_str(qdict, "option");
722 if (!option || !strcmp(option, "on")) {
724 } else if (!strcmp(option, "off")) {
727 monitor_printf(mon, "unexpected option %s\n", option);
732 * do_stop(): Stop VM execution
734 static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
736 vm_stop(EXCP_INTERRUPT);
739 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
741 struct bdrv_iterate_context {
747 * do_cont(): Resume emulation.
749 static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
751 struct bdrv_iterate_context context = { mon, 0 };
753 bdrv_iterate(encrypted_bdrv_it, &context);
754 /* only resume the vm if all keys are set and valid */
759 static void bdrv_key_cb(void *opaque, int err)
761 Monitor *mon = opaque;
763 /* another key was set successfully, retry to continue */
765 do_cont(mon, NULL, NULL);
768 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
770 struct bdrv_iterate_context *context = opaque;
772 if (!context->err && bdrv_key_required(bs)) {
773 context->err = -EBUSY;
774 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
779 static void do_gdbserver(Monitor *mon, const QDict *qdict)
781 const char *device = qdict_get_try_str(qdict, "device");
783 device = "tcp::" DEFAULT_GDBSTUB_PORT;
784 if (gdbserver_start(device) < 0) {
785 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
787 } else if (strcmp(device, "none") == 0) {
788 monitor_printf(mon, "Disabled gdbserver\n");
790 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
795 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
797 const char *action = qdict_get_str(qdict, "action");
798 if (select_watchdog_action(action) == -1) {
799 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
803 static void monitor_printc(Monitor *mon, int c)
805 monitor_printf(mon, "'");
808 monitor_printf(mon, "\\'");
811 monitor_printf(mon, "\\\\");
814 monitor_printf(mon, "\\n");
817 monitor_printf(mon, "\\r");
820 if (c >= 32 && c <= 126) {
821 monitor_printf(mon, "%c", c);
823 monitor_printf(mon, "\\x%02x", c);
827 monitor_printf(mon, "'");
830 static void memory_dump(Monitor *mon, int count, int format, int wsize,
831 target_phys_addr_t addr, int is_physical)
834 int nb_per_line, l, line_size, i, max_digits, len;
842 if (!env && !is_physical)
847 } else if (wsize == 4) {
850 /* as default we use the current CS size */
854 if ((env->efer & MSR_EFER_LMA) &&
855 (env->segs[R_CS].flags & DESC_L_MASK))
859 if (!(env->segs[R_CS].flags & DESC_B_MASK))
864 monitor_disas(mon, env, addr, count, is_physical, flags);
873 nb_per_line = line_size / wsize;
878 max_digits = (wsize * 8 + 2) / 3;
882 max_digits = (wsize * 8) / 4;
886 max_digits = (wsize * 8 * 10 + 32) / 33;
895 monitor_printf(mon, TARGET_FMT_plx ":", addr);
897 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
902 cpu_physical_memory_rw(addr, buf, l, 0);
907 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
908 monitor_printf(mon, " Cannot access memory\n");
917 v = ldub_raw(buf + i);
920 v = lduw_raw(buf + i);
923 v = (uint32_t)ldl_raw(buf + i);
926 v = ldq_raw(buf + i);
929 monitor_printf(mon, " ");
932 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
935 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
938 monitor_printf(mon, "%*" PRIu64, max_digits, v);
941 monitor_printf(mon, "%*" PRId64, max_digits, v);
944 monitor_printc(mon, v);
949 monitor_printf(mon, "\n");
955 static void do_memory_dump(Monitor *mon, const QDict *qdict)
957 int count = qdict_get_int(qdict, "count");
958 int format = qdict_get_int(qdict, "format");
959 int size = qdict_get_int(qdict, "size");
960 target_long addr = qdict_get_int(qdict, "addr");
962 memory_dump(mon, count, format, size, addr, 0);
965 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
967 int count = qdict_get_int(qdict, "count");
968 int format = qdict_get_int(qdict, "format");
969 int size = qdict_get_int(qdict, "size");
970 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
972 memory_dump(mon, count, format, size, addr, 1);
975 static void do_print(Monitor *mon, const QDict *qdict)
977 int format = qdict_get_int(qdict, "format");
978 target_phys_addr_t val = qdict_get_int(qdict, "val");
980 #if TARGET_PHYS_ADDR_BITS == 32
983 monitor_printf(mon, "%#o", val);
986 monitor_printf(mon, "%#x", val);
989 monitor_printf(mon, "%u", val);
993 monitor_printf(mon, "%d", val);
996 monitor_printc(mon, val);
1002 monitor_printf(mon, "%#" PRIo64, val);
1005 monitor_printf(mon, "%#" PRIx64, val);
1008 monitor_printf(mon, "%" PRIu64, val);
1012 monitor_printf(mon, "%" PRId64, val);
1015 monitor_printc(mon, val);
1019 monitor_printf(mon, "\n");
1022 static void do_memory_save(Monitor *mon, const QDict *qdict, QObject **ret_data)
1025 uint32_t size = qdict_get_int(qdict, "size");
1026 const char *filename = qdict_get_str(qdict, "filename");
1027 target_long addr = qdict_get_int(qdict, "val");
1032 env = mon_get_cpu();
1036 f = fopen(filename, "wb");
1038 monitor_printf(mon, "could not open '%s'\n", filename);
1045 cpu_memory_rw_debug(env, addr, buf, l, 0);
1046 fwrite(buf, 1, l, f);
1053 static void do_physical_memory_save(Monitor *mon, const QDict *qdict,
1059 uint32_t size = qdict_get_int(qdict, "size");
1060 const char *filename = qdict_get_str(qdict, "filename");
1061 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1063 f = fopen(filename, "wb");
1065 monitor_printf(mon, "could not open '%s'\n", filename);
1072 cpu_physical_memory_rw(addr, buf, l, 0);
1073 fwrite(buf, 1, l, f);
1081 static void do_sum(Monitor *mon, const QDict *qdict)
1086 uint32_t start = qdict_get_int(qdict, "start");
1087 uint32_t size = qdict_get_int(qdict, "size");
1090 for(addr = start; addr < (start + size); addr++) {
1091 cpu_physical_memory_rw(addr, buf, 1, 0);
1092 /* BSD sum algorithm ('sum' Unix command) */
1093 sum = (sum >> 1) | (sum << 15);
1096 monitor_printf(mon, "%05d\n", sum);
1104 static const KeyDef key_defs[] = {
1106 { 0x36, "shift_r" },
1111 { 0xe4, "altgr_r" },
1131 { 0x0e, "backspace" },
1168 { 0x37, "asterisk" },
1171 { 0x3a, "caps_lock" },
1182 { 0x45, "num_lock" },
1183 { 0x46, "scroll_lock" },
1185 { 0xb5, "kp_divide" },
1186 { 0x37, "kp_multiply" },
1187 { 0x4a, "kp_subtract" },
1189 { 0x9c, "kp_enter" },
1190 { 0x53, "kp_decimal" },
1223 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1238 { 0xfe, "compose" },
1243 static int get_keycode(const char *key)
1249 for(p = key_defs; p->name != NULL; p++) {
1250 if (!strcmp(key, p->name))
1253 if (strstart(key, "0x", NULL)) {
1254 ret = strtoul(key, &endp, 0);
1255 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1261 #define MAX_KEYCODES 16
1262 static uint8_t keycodes[MAX_KEYCODES];
1263 static int nb_pending_keycodes;
1264 static QEMUTimer *key_timer;
1266 static void release_keys(void *opaque)
1270 while (nb_pending_keycodes > 0) {
1271 nb_pending_keycodes--;
1272 keycode = keycodes[nb_pending_keycodes];
1274 kbd_put_keycode(0xe0);
1275 kbd_put_keycode(keycode | 0x80);
1279 static void do_sendkey(Monitor *mon, const QDict *qdict)
1281 char keyname_buf[16];
1283 int keyname_len, keycode, i;
1284 const char *string = qdict_get_str(qdict, "string");
1285 int has_hold_time = qdict_haskey(qdict, "hold_time");
1286 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1288 if (nb_pending_keycodes > 0) {
1289 qemu_del_timer(key_timer);
1296 separator = strchr(string, '-');
1297 keyname_len = separator ? separator - string : strlen(string);
1298 if (keyname_len > 0) {
1299 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1300 if (keyname_len > sizeof(keyname_buf) - 1) {
1301 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1304 if (i == MAX_KEYCODES) {
1305 monitor_printf(mon, "too many keys\n");
1308 keyname_buf[keyname_len] = 0;
1309 keycode = get_keycode(keyname_buf);
1311 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1314 keycodes[i++] = keycode;
1318 string = separator + 1;
1320 nb_pending_keycodes = i;
1321 /* key down events */
1322 for (i = 0; i < nb_pending_keycodes; i++) {
1323 keycode = keycodes[i];
1325 kbd_put_keycode(0xe0);
1326 kbd_put_keycode(keycode & 0x7f);
1328 /* delayed key up events */
1329 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1330 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1333 static int mouse_button_state;
1335 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1338 const char *dx_str = qdict_get_str(qdict, "dx_str");
1339 const char *dy_str = qdict_get_str(qdict, "dy_str");
1340 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1341 dx = strtol(dx_str, NULL, 0);
1342 dy = strtol(dy_str, NULL, 0);
1345 dz = strtol(dz_str, NULL, 0);
1346 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1349 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1351 int button_state = qdict_get_int(qdict, "button_state");
1352 mouse_button_state = button_state;
1353 kbd_mouse_event(0, 0, 0, mouse_button_state);
1356 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1358 int size = qdict_get_int(qdict, "size");
1359 int addr = qdict_get_int(qdict, "addr");
1360 int has_index = qdict_haskey(qdict, "index");
1365 int index = qdict_get_int(qdict, "index");
1366 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1374 val = cpu_inb(addr);
1378 val = cpu_inw(addr);
1382 val = cpu_inl(addr);
1386 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1387 suffix, addr, size * 2, val);
1390 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1392 int size = qdict_get_int(qdict, "size");
1393 int addr = qdict_get_int(qdict, "addr");
1394 int val = qdict_get_int(qdict, "val");
1396 addr &= IOPORTS_MASK;
1401 cpu_outb(addr, val);
1404 cpu_outw(addr, val);
1407 cpu_outl(addr, val);
1412 static void do_boot_set(Monitor *mon, const QDict *qdict)
1415 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1417 res = qemu_boot_set(bootdevice);
1419 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1420 } else if (res > 0) {
1421 monitor_printf(mon, "setting boot device list failed\n");
1423 monitor_printf(mon, "no function defined to set boot device list for "
1424 "this architecture\n");
1429 * do_system_reset(): Issue a machine reset
1431 static void do_system_reset(Monitor *mon, const QDict *qdict,
1434 qemu_system_reset_request();
1438 * do_system_powerdown(): Issue a machine powerdown
1440 static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1443 qemu_system_powerdown_request();
1446 #if defined(TARGET_I386)
1447 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1449 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1452 pte & PG_GLOBAL_MASK ? 'G' : '-',
1453 pte & PG_PSE_MASK ? 'P' : '-',
1454 pte & PG_DIRTY_MASK ? 'D' : '-',
1455 pte & PG_ACCESSED_MASK ? 'A' : '-',
1456 pte & PG_PCD_MASK ? 'C' : '-',
1457 pte & PG_PWT_MASK ? 'T' : '-',
1458 pte & PG_USER_MASK ? 'U' : '-',
1459 pte & PG_RW_MASK ? 'W' : '-');
1462 static void tlb_info(Monitor *mon)
1466 uint32_t pgd, pde, pte;
1468 env = mon_get_cpu();
1472 if (!(env->cr[0] & CR0_PG_MASK)) {
1473 monitor_printf(mon, "PG disabled\n");
1476 pgd = env->cr[3] & ~0xfff;
1477 for(l1 = 0; l1 < 1024; l1++) {
1478 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1479 pde = le32_to_cpu(pde);
1480 if (pde & PG_PRESENT_MASK) {
1481 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1482 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1484 for(l2 = 0; l2 < 1024; l2++) {
1485 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1486 (uint8_t *)&pte, 4);
1487 pte = le32_to_cpu(pte);
1488 if (pte & PG_PRESENT_MASK) {
1489 print_pte(mon, (l1 << 22) + (l2 << 12),
1499 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1500 uint32_t end, int prot)
1503 prot1 = *plast_prot;
1504 if (prot != prot1) {
1505 if (*pstart != -1) {
1506 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1507 *pstart, end, end - *pstart,
1508 prot1 & PG_USER_MASK ? 'u' : '-',
1510 prot1 & PG_RW_MASK ? 'w' : '-');
1520 static void mem_info(Monitor *mon)
1523 int l1, l2, prot, last_prot;
1524 uint32_t pgd, pde, pte, start, end;
1526 env = mon_get_cpu();
1530 if (!(env->cr[0] & CR0_PG_MASK)) {
1531 monitor_printf(mon, "PG disabled\n");
1534 pgd = env->cr[3] & ~0xfff;
1537 for(l1 = 0; l1 < 1024; l1++) {
1538 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1539 pde = le32_to_cpu(pde);
1541 if (pde & PG_PRESENT_MASK) {
1542 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1543 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1544 mem_print(mon, &start, &last_prot, end, prot);
1546 for(l2 = 0; l2 < 1024; l2++) {
1547 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1548 (uint8_t *)&pte, 4);
1549 pte = le32_to_cpu(pte);
1550 end = (l1 << 22) + (l2 << 12);
1551 if (pte & PG_PRESENT_MASK) {
1552 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1556 mem_print(mon, &start, &last_prot, end, prot);
1561 mem_print(mon, &start, &last_prot, end, prot);
1567 #if defined(TARGET_SH4)
1569 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1571 monitor_printf(mon, " tlb%i:\t"
1572 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1573 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1574 "dirty=%hhu writethrough=%hhu\n",
1576 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1577 tlb->v, tlb->sh, tlb->c, tlb->pr,
1581 static void tlb_info(Monitor *mon)
1583 CPUState *env = mon_get_cpu();
1586 monitor_printf (mon, "ITLB:\n");
1587 for (i = 0 ; i < ITLB_SIZE ; i++)
1588 print_tlb (mon, i, &env->itlb[i]);
1589 monitor_printf (mon, "UTLB:\n");
1590 for (i = 0 ; i < UTLB_SIZE ; i++)
1591 print_tlb (mon, i, &env->utlb[i]);
1596 static void do_info_kvm(Monitor *mon)
1599 monitor_printf(mon, "kvm support: ");
1601 monitor_printf(mon, "enabled\n");
1603 monitor_printf(mon, "disabled\n");
1605 monitor_printf(mon, "kvm support: not compiled\n");
1609 static void do_info_numa(Monitor *mon)
1614 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1615 for (i = 0; i < nb_numa_nodes; i++) {
1616 monitor_printf(mon, "node %d cpus:", i);
1617 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1618 if (env->numa_node == i) {
1619 monitor_printf(mon, " %d", env->cpu_index);
1622 monitor_printf(mon, "\n");
1623 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1628 #ifdef CONFIG_PROFILER
1633 static void do_info_profile(Monitor *mon)
1639 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1640 dev_time, dev_time / (double)get_ticks_per_sec());
1641 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1642 qemu_time, qemu_time / (double)get_ticks_per_sec());
1647 static void do_info_profile(Monitor *mon)
1649 monitor_printf(mon, "Internal profiler not compiled\n");
1653 /* Capture support */
1654 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1656 static void do_info_capture(Monitor *mon)
1661 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1662 monitor_printf(mon, "[%d]: ", i);
1663 s->ops.info (s->opaque);
1668 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1671 int n = qdict_get_int(qdict, "n");
1674 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1676 s->ops.destroy (s->opaque);
1677 QLIST_REMOVE (s, entries);
1684 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1686 const char *path = qdict_get_str(qdict, "path");
1687 int has_freq = qdict_haskey(qdict, "freq");
1688 int freq = qdict_get_try_int(qdict, "freq", -1);
1689 int has_bits = qdict_haskey(qdict, "bits");
1690 int bits = qdict_get_try_int(qdict, "bits", -1);
1691 int has_channels = qdict_haskey(qdict, "nchannels");
1692 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1695 s = qemu_mallocz (sizeof (*s));
1697 freq = has_freq ? freq : 44100;
1698 bits = has_bits ? bits : 16;
1699 nchannels = has_channels ? nchannels : 2;
1701 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1702 monitor_printf(mon, "Faied to add wave capture\n");
1705 QLIST_INSERT_HEAD (&capture_head, s, entries);
1709 #if defined(TARGET_I386)
1710 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1713 int cpu_index = qdict_get_int(qdict, "cpu_index");
1715 for (env = first_cpu; env != NULL; env = env->next_cpu)
1716 if (env->cpu_index == cpu_index) {
1717 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1723 static void do_info_status(Monitor *mon)
1727 monitor_printf(mon, "VM status: running (single step mode)\n");
1729 monitor_printf(mon, "VM status: running\n");
1732 monitor_printf(mon, "VM status: paused\n");
1736 * do_balloon(): Request VM to change its memory allocation
1738 static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1740 int value = qdict_get_int(qdict, "value");
1741 ram_addr_t target = value;
1742 qemu_balloon(target << 20);
1745 static void monitor_print_balloon(Monitor *mon, const QObject *data)
1747 monitor_printf(mon, "balloon: actual=%d\n",
1748 (int)qint_get_int(qobject_to_qint(data)));
1752 * do_info_balloon(): Balloon information
1754 static void do_info_balloon(Monitor *mon, QObject **ret_data)
1758 actual = qemu_balloon_status();
1759 if (kvm_enabled() && !kvm_has_sync_mmu())
1760 qemu_error_new(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
1761 else if (actual == 0)
1762 qemu_error_new(QERR_DEVICE_NOT_ACTIVE, "balloon");
1764 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
1767 static qemu_acl *find_acl(Monitor *mon, const char *name)
1769 qemu_acl *acl = qemu_acl_find(name);
1772 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1777 static void do_acl_show(Monitor *mon, const QDict *qdict)
1779 const char *aclname = qdict_get_str(qdict, "aclname");
1780 qemu_acl *acl = find_acl(mon, aclname);
1781 qemu_acl_entry *entry;
1785 monitor_printf(mon, "policy: %s\n",
1786 acl->defaultDeny ? "deny" : "allow");
1787 QTAILQ_FOREACH(entry, &acl->entries, next) {
1789 monitor_printf(mon, "%d: %s %s\n", i,
1790 entry->deny ? "deny" : "allow", entry->match);
1795 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1797 const char *aclname = qdict_get_str(qdict, "aclname");
1798 qemu_acl *acl = find_acl(mon, aclname);
1801 qemu_acl_reset(acl);
1802 monitor_printf(mon, "acl: removed all rules\n");
1806 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1808 const char *aclname = qdict_get_str(qdict, "aclname");
1809 const char *policy = qdict_get_str(qdict, "policy");
1810 qemu_acl *acl = find_acl(mon, aclname);
1813 if (strcmp(policy, "allow") == 0) {
1814 acl->defaultDeny = 0;
1815 monitor_printf(mon, "acl: policy set to 'allow'\n");
1816 } else if (strcmp(policy, "deny") == 0) {
1817 acl->defaultDeny = 1;
1818 monitor_printf(mon, "acl: policy set to 'deny'\n");
1820 monitor_printf(mon, "acl: unknown policy '%s', "
1821 "expected 'deny' or 'allow'\n", policy);
1826 static void do_acl_add(Monitor *mon, const QDict *qdict)
1828 const char *aclname = qdict_get_str(qdict, "aclname");
1829 const char *match = qdict_get_str(qdict, "match");
1830 const char *policy = qdict_get_str(qdict, "policy");
1831 int has_index = qdict_haskey(qdict, "index");
1832 int index = qdict_get_try_int(qdict, "index", -1);
1833 qemu_acl *acl = find_acl(mon, aclname);
1837 if (strcmp(policy, "allow") == 0) {
1839 } else if (strcmp(policy, "deny") == 0) {
1842 monitor_printf(mon, "acl: unknown policy '%s', "
1843 "expected 'deny' or 'allow'\n", policy);
1847 ret = qemu_acl_insert(acl, deny, match, index);
1849 ret = qemu_acl_append(acl, deny, match);
1851 monitor_printf(mon, "acl: unable to add acl entry\n");
1853 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1857 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1859 const char *aclname = qdict_get_str(qdict, "aclname");
1860 const char *match = qdict_get_str(qdict, "match");
1861 qemu_acl *acl = find_acl(mon, aclname);
1865 ret = qemu_acl_remove(acl, match);
1867 monitor_printf(mon, "acl: no matching acl entry\n");
1869 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1873 #if defined(TARGET_I386)
1874 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1877 int cpu_index = qdict_get_int(qdict, "cpu_index");
1878 int bank = qdict_get_int(qdict, "bank");
1879 uint64_t status = qdict_get_int(qdict, "status");
1880 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1881 uint64_t addr = qdict_get_int(qdict, "addr");
1882 uint64_t misc = qdict_get_int(qdict, "misc");
1884 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1885 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1886 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1892 static void do_getfd(Monitor *mon, const QDict *qdict, QObject **ret_data)
1894 const char *fdname = qdict_get_str(qdict, "fdname");
1898 fd = qemu_chr_get_msgfd(mon->chr);
1900 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1904 if (qemu_isdigit(fdname[0])) {
1905 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1911 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1916 QLIST_FOREACH(monfd, &mon->fds, next) {
1917 if (strcmp(monfd->name, fdname) != 0) {
1926 monfd = qemu_mallocz(sizeof(mon_fd_t));
1927 monfd->name = qemu_strdup(fdname);
1930 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1933 static void do_closefd(Monitor *mon, const QDict *qdict, QObject **ret_data)
1935 const char *fdname = qdict_get_str(qdict, "fdname");
1938 QLIST_FOREACH(monfd, &mon->fds, next) {
1939 if (strcmp(monfd->name, fdname) != 0) {
1943 QLIST_REMOVE(monfd, next);
1945 qemu_free(monfd->name);
1950 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1954 static void do_loadvm(Monitor *mon, const QDict *qdict)
1956 int saved_vm_running = vm_running;
1957 const char *name = qdict_get_str(qdict, "name");
1961 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1965 int monitor_get_fd(Monitor *mon, const char *fdname)
1969 QLIST_FOREACH(monfd, &mon->fds, next) {
1972 if (strcmp(monfd->name, fdname) != 0) {
1978 /* caller takes ownership of fd */
1979 QLIST_REMOVE(monfd, next);
1980 qemu_free(monfd->name);
1989 static const mon_cmd_t mon_cmds[] = {
1990 #include "qemu-monitor.h"
1994 /* Please update qemu-monitor.hx when adding or changing commands */
1995 static const mon_cmd_t info_cmds[] = {
2000 .help = "show the version of QEMU",
2001 .user_print = monitor_print_qobject,
2002 .mhandler.info_new = do_info_version,
2008 .help = "list QMP available commands",
2009 .user_print = monitor_user_noop,
2010 .mhandler.info_new = do_info_commands,
2016 .help = "show the network state",
2017 .mhandler.info = do_info_network,
2023 .help = "show the character devices",
2024 .mhandler.info = qemu_chr_info,
2030 .help = "show the block devices",
2031 .mhandler.info = bdrv_info,
2034 .name = "blockstats",
2037 .help = "show block device statistics",
2038 .mhandler.info = bdrv_info_stats,
2041 .name = "registers",
2044 .help = "show the cpu registers",
2045 .mhandler.info = do_info_registers,
2051 .help = "show infos for each CPU",
2052 .user_print = monitor_print_cpus,
2053 .mhandler.info_new = do_info_cpus,
2059 .help = "show the command line history",
2060 .mhandler.info = do_info_history,
2066 .help = "show the interrupts statistics (if available)",
2067 .mhandler.info = irq_info,
2073 .help = "show i8259 (PIC) state",
2074 .mhandler.info = pic_info,
2080 .help = "show PCI info",
2081 .mhandler.info = pci_info,
2083 #if defined(TARGET_I386) || defined(TARGET_SH4)
2088 .help = "show virtual to physical memory mappings",
2089 .mhandler.info = tlb_info,
2092 #if defined(TARGET_I386)
2097 .help = "show the active virtual memory mappings",
2098 .mhandler.info = mem_info,
2104 .help = "show state of HPET",
2105 .mhandler.info = do_info_hpet,
2112 .help = "show dynamic compiler info",
2113 .mhandler.info = do_info_jit,
2119 .help = "show KVM information",
2120 .mhandler.info = do_info_kvm,
2126 .help = "show NUMA information",
2127 .mhandler.info = do_info_numa,
2133 .help = "show guest USB devices",
2134 .mhandler.info = usb_info,
2140 .help = "show host USB devices",
2141 .mhandler.info = usb_host_info,
2147 .help = "show profiling information",
2148 .mhandler.info = do_info_profile,
2154 .help = "show capture information",
2155 .mhandler.info = do_info_capture,
2158 .name = "snapshots",
2161 .help = "show the currently saved VM snapshots",
2162 .mhandler.info = do_info_snapshots,
2168 .help = "show the current VM status (running|paused)",
2169 .mhandler.info = do_info_status,
2175 .help = "show guest PCMCIA status",
2176 .mhandler.info = pcmcia_info,
2182 .help = "show which guest mouse is receiving events",
2183 .mhandler.info = do_info_mice,
2189 .help = "show the vnc server status",
2190 .mhandler.info = do_info_vnc,
2196 .help = "show the current VM name",
2197 .mhandler.info = do_info_name,
2203 .help = "show the current VM UUID",
2204 .mhandler.info = do_info_uuid,
2206 #if defined(TARGET_PPC)
2211 .help = "show CPU statistics",
2212 .mhandler.info = do_info_cpu_stats,
2215 #if defined(CONFIG_SLIRP)
2220 .help = "show user network stack connection states",
2221 .mhandler.info = do_info_usernet,
2228 .help = "show migration status",
2229 .mhandler.info = do_info_migrate,
2235 .help = "show balloon information",
2236 .user_print = monitor_print_balloon,
2237 .mhandler.info_new = do_info_balloon,
2243 .help = "show device tree",
2244 .mhandler.info = do_info_qtree,
2250 .help = "show qdev device model list",
2251 .mhandler.info = do_info_qdm,
2257 .help = "show roms",
2258 .mhandler.info = do_info_roms,
2265 /*******************************************************************/
2267 static const char *pch;
2268 static jmp_buf expr_env;
2273 typedef struct MonitorDef {
2276 target_long (*get_value)(const struct MonitorDef *md, int val);
2280 #if defined(TARGET_I386)
2281 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2283 CPUState *env = mon_get_cpu();
2286 return env->eip + env->segs[R_CS].base;
2290 #if defined(TARGET_PPC)
2291 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2293 CPUState *env = mon_get_cpu();
2301 for (i = 0; i < 8; i++)
2302 u |= env->crf[i] << (32 - (4 * i));
2307 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2309 CPUState *env = mon_get_cpu();
2315 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2317 CPUState *env = mon_get_cpu();
2323 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2325 CPUState *env = mon_get_cpu();
2328 return cpu_ppc_load_decr(env);
2331 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2333 CPUState *env = mon_get_cpu();
2336 return cpu_ppc_load_tbu(env);
2339 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2341 CPUState *env = mon_get_cpu();
2344 return cpu_ppc_load_tbl(env);
2348 #if defined(TARGET_SPARC)
2349 #ifndef TARGET_SPARC64
2350 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2352 CPUState *env = mon_get_cpu();
2355 return GET_PSR(env);
2359 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2361 CPUState *env = mon_get_cpu();
2364 return env->regwptr[val];
2368 static const MonitorDef monitor_defs[] = {
2371 #define SEG(name, seg) \
2372 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2373 { name ".base", offsetof(CPUState, segs[seg].base) },\
2374 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2376 { "eax", offsetof(CPUState, regs[0]) },
2377 { "ecx", offsetof(CPUState, regs[1]) },
2378 { "edx", offsetof(CPUState, regs[2]) },
2379 { "ebx", offsetof(CPUState, regs[3]) },
2380 { "esp|sp", offsetof(CPUState, regs[4]) },
2381 { "ebp|fp", offsetof(CPUState, regs[5]) },
2382 { "esi", offsetof(CPUState, regs[6]) },
2383 { "edi", offsetof(CPUState, regs[7]) },
2384 #ifdef TARGET_X86_64
2385 { "r8", offsetof(CPUState, regs[8]) },
2386 { "r9", offsetof(CPUState, regs[9]) },
2387 { "r10", offsetof(CPUState, regs[10]) },
2388 { "r11", offsetof(CPUState, regs[11]) },
2389 { "r12", offsetof(CPUState, regs[12]) },
2390 { "r13", offsetof(CPUState, regs[13]) },
2391 { "r14", offsetof(CPUState, regs[14]) },
2392 { "r15", offsetof(CPUState, regs[15]) },
2394 { "eflags", offsetof(CPUState, eflags) },
2395 { "eip", offsetof(CPUState, eip) },
2402 { "pc", 0, monitor_get_pc, },
2403 #elif defined(TARGET_PPC)
2404 /* General purpose registers */
2405 { "r0", offsetof(CPUState, gpr[0]) },
2406 { "r1", offsetof(CPUState, gpr[1]) },
2407 { "r2", offsetof(CPUState, gpr[2]) },
2408 { "r3", offsetof(CPUState, gpr[3]) },
2409 { "r4", offsetof(CPUState, gpr[4]) },
2410 { "r5", offsetof(CPUState, gpr[5]) },
2411 { "r6", offsetof(CPUState, gpr[6]) },
2412 { "r7", offsetof(CPUState, gpr[7]) },
2413 { "r8", offsetof(CPUState, gpr[8]) },
2414 { "r9", offsetof(CPUState, gpr[9]) },
2415 { "r10", offsetof(CPUState, gpr[10]) },
2416 { "r11", offsetof(CPUState, gpr[11]) },
2417 { "r12", offsetof(CPUState, gpr[12]) },
2418 { "r13", offsetof(CPUState, gpr[13]) },
2419 { "r14", offsetof(CPUState, gpr[14]) },
2420 { "r15", offsetof(CPUState, gpr[15]) },
2421 { "r16", offsetof(CPUState, gpr[16]) },
2422 { "r17", offsetof(CPUState, gpr[17]) },
2423 { "r18", offsetof(CPUState, gpr[18]) },
2424 { "r19", offsetof(CPUState, gpr[19]) },
2425 { "r20", offsetof(CPUState, gpr[20]) },
2426 { "r21", offsetof(CPUState, gpr[21]) },
2427 { "r22", offsetof(CPUState, gpr[22]) },
2428 { "r23", offsetof(CPUState, gpr[23]) },
2429 { "r24", offsetof(CPUState, gpr[24]) },
2430 { "r25", offsetof(CPUState, gpr[25]) },
2431 { "r26", offsetof(CPUState, gpr[26]) },
2432 { "r27", offsetof(CPUState, gpr[27]) },
2433 { "r28", offsetof(CPUState, gpr[28]) },
2434 { "r29", offsetof(CPUState, gpr[29]) },
2435 { "r30", offsetof(CPUState, gpr[30]) },
2436 { "r31", offsetof(CPUState, gpr[31]) },
2437 /* Floating point registers */
2438 { "f0", offsetof(CPUState, fpr[0]) },
2439 { "f1", offsetof(CPUState, fpr[1]) },
2440 { "f2", offsetof(CPUState, fpr[2]) },
2441 { "f3", offsetof(CPUState, fpr[3]) },
2442 { "f4", offsetof(CPUState, fpr[4]) },
2443 { "f5", offsetof(CPUState, fpr[5]) },
2444 { "f6", offsetof(CPUState, fpr[6]) },
2445 { "f7", offsetof(CPUState, fpr[7]) },
2446 { "f8", offsetof(CPUState, fpr[8]) },
2447 { "f9", offsetof(CPUState, fpr[9]) },
2448 { "f10", offsetof(CPUState, fpr[10]) },
2449 { "f11", offsetof(CPUState, fpr[11]) },
2450 { "f12", offsetof(CPUState, fpr[12]) },
2451 { "f13", offsetof(CPUState, fpr[13]) },
2452 { "f14", offsetof(CPUState, fpr[14]) },
2453 { "f15", offsetof(CPUState, fpr[15]) },
2454 { "f16", offsetof(CPUState, fpr[16]) },
2455 { "f17", offsetof(CPUState, fpr[17]) },
2456 { "f18", offsetof(CPUState, fpr[18]) },
2457 { "f19", offsetof(CPUState, fpr[19]) },
2458 { "f20", offsetof(CPUState, fpr[20]) },
2459 { "f21", offsetof(CPUState, fpr[21]) },
2460 { "f22", offsetof(CPUState, fpr[22]) },
2461 { "f23", offsetof(CPUState, fpr[23]) },
2462 { "f24", offsetof(CPUState, fpr[24]) },
2463 { "f25", offsetof(CPUState, fpr[25]) },
2464 { "f26", offsetof(CPUState, fpr[26]) },
2465 { "f27", offsetof(CPUState, fpr[27]) },
2466 { "f28", offsetof(CPUState, fpr[28]) },
2467 { "f29", offsetof(CPUState, fpr[29]) },
2468 { "f30", offsetof(CPUState, fpr[30]) },
2469 { "f31", offsetof(CPUState, fpr[31]) },
2470 { "fpscr", offsetof(CPUState, fpscr) },
2471 /* Next instruction pointer */
2472 { "nip|pc", offsetof(CPUState, nip) },
2473 { "lr", offsetof(CPUState, lr) },
2474 { "ctr", offsetof(CPUState, ctr) },
2475 { "decr", 0, &monitor_get_decr, },
2476 { "ccr", 0, &monitor_get_ccr, },
2477 /* Machine state register */
2478 { "msr", 0, &monitor_get_msr, },
2479 { "xer", 0, &monitor_get_xer, },
2480 { "tbu", 0, &monitor_get_tbu, },
2481 { "tbl", 0, &monitor_get_tbl, },
2482 #if defined(TARGET_PPC64)
2483 /* Address space register */
2484 { "asr", offsetof(CPUState, asr) },
2486 /* Segment registers */
2487 { "sdr1", offsetof(CPUState, sdr1) },
2488 { "sr0", offsetof(CPUState, sr[0]) },
2489 { "sr1", offsetof(CPUState, sr[1]) },
2490 { "sr2", offsetof(CPUState, sr[2]) },
2491 { "sr3", offsetof(CPUState, sr[3]) },
2492 { "sr4", offsetof(CPUState, sr[4]) },
2493 { "sr5", offsetof(CPUState, sr[5]) },
2494 { "sr6", offsetof(CPUState, sr[6]) },
2495 { "sr7", offsetof(CPUState, sr[7]) },
2496 { "sr8", offsetof(CPUState, sr[8]) },
2497 { "sr9", offsetof(CPUState, sr[9]) },
2498 { "sr10", offsetof(CPUState, sr[10]) },
2499 { "sr11", offsetof(CPUState, sr[11]) },
2500 { "sr12", offsetof(CPUState, sr[12]) },
2501 { "sr13", offsetof(CPUState, sr[13]) },
2502 { "sr14", offsetof(CPUState, sr[14]) },
2503 { "sr15", offsetof(CPUState, sr[15]) },
2504 /* Too lazy to put BATs and SPRs ... */
2505 #elif defined(TARGET_SPARC)
2506 { "g0", offsetof(CPUState, gregs[0]) },
2507 { "g1", offsetof(CPUState, gregs[1]) },
2508 { "g2", offsetof(CPUState, gregs[2]) },
2509 { "g3", offsetof(CPUState, gregs[3]) },
2510 { "g4", offsetof(CPUState, gregs[4]) },
2511 { "g5", offsetof(CPUState, gregs[5]) },
2512 { "g6", offsetof(CPUState, gregs[6]) },
2513 { "g7", offsetof(CPUState, gregs[7]) },
2514 { "o0", 0, monitor_get_reg },
2515 { "o1", 1, monitor_get_reg },
2516 { "o2", 2, monitor_get_reg },
2517 { "o3", 3, monitor_get_reg },
2518 { "o4", 4, monitor_get_reg },
2519 { "o5", 5, monitor_get_reg },
2520 { "o6", 6, monitor_get_reg },
2521 { "o7", 7, monitor_get_reg },
2522 { "l0", 8, monitor_get_reg },
2523 { "l1", 9, monitor_get_reg },
2524 { "l2", 10, monitor_get_reg },
2525 { "l3", 11, monitor_get_reg },
2526 { "l4", 12, monitor_get_reg },
2527 { "l5", 13, monitor_get_reg },
2528 { "l6", 14, monitor_get_reg },
2529 { "l7", 15, monitor_get_reg },
2530 { "i0", 16, monitor_get_reg },
2531 { "i1", 17, monitor_get_reg },
2532 { "i2", 18, monitor_get_reg },
2533 { "i3", 19, monitor_get_reg },
2534 { "i4", 20, monitor_get_reg },
2535 { "i5", 21, monitor_get_reg },
2536 { "i6", 22, monitor_get_reg },
2537 { "i7", 23, monitor_get_reg },
2538 { "pc", offsetof(CPUState, pc) },
2539 { "npc", offsetof(CPUState, npc) },
2540 { "y", offsetof(CPUState, y) },
2541 #ifndef TARGET_SPARC64
2542 { "psr", 0, &monitor_get_psr, },
2543 { "wim", offsetof(CPUState, wim) },
2545 { "tbr", offsetof(CPUState, tbr) },
2546 { "fsr", offsetof(CPUState, fsr) },
2547 { "f0", offsetof(CPUState, fpr[0]) },
2548 { "f1", offsetof(CPUState, fpr[1]) },
2549 { "f2", offsetof(CPUState, fpr[2]) },
2550 { "f3", offsetof(CPUState, fpr[3]) },
2551 { "f4", offsetof(CPUState, fpr[4]) },
2552 { "f5", offsetof(CPUState, fpr[5]) },
2553 { "f6", offsetof(CPUState, fpr[6]) },
2554 { "f7", offsetof(CPUState, fpr[7]) },
2555 { "f8", offsetof(CPUState, fpr[8]) },
2556 { "f9", offsetof(CPUState, fpr[9]) },
2557 { "f10", offsetof(CPUState, fpr[10]) },
2558 { "f11", offsetof(CPUState, fpr[11]) },
2559 { "f12", offsetof(CPUState, fpr[12]) },
2560 { "f13", offsetof(CPUState, fpr[13]) },
2561 { "f14", offsetof(CPUState, fpr[14]) },
2562 { "f15", offsetof(CPUState, fpr[15]) },
2563 { "f16", offsetof(CPUState, fpr[16]) },
2564 { "f17", offsetof(CPUState, fpr[17]) },
2565 { "f18", offsetof(CPUState, fpr[18]) },
2566 { "f19", offsetof(CPUState, fpr[19]) },
2567 { "f20", offsetof(CPUState, fpr[20]) },
2568 { "f21", offsetof(CPUState, fpr[21]) },
2569 { "f22", offsetof(CPUState, fpr[22]) },
2570 { "f23", offsetof(CPUState, fpr[23]) },
2571 { "f24", offsetof(CPUState, fpr[24]) },
2572 { "f25", offsetof(CPUState, fpr[25]) },
2573 { "f26", offsetof(CPUState, fpr[26]) },
2574 { "f27", offsetof(CPUState, fpr[27]) },
2575 { "f28", offsetof(CPUState, fpr[28]) },
2576 { "f29", offsetof(CPUState, fpr[29]) },
2577 { "f30", offsetof(CPUState, fpr[30]) },
2578 { "f31", offsetof(CPUState, fpr[31]) },
2579 #ifdef TARGET_SPARC64
2580 { "f32", offsetof(CPUState, fpr[32]) },
2581 { "f34", offsetof(CPUState, fpr[34]) },
2582 { "f36", offsetof(CPUState, fpr[36]) },
2583 { "f38", offsetof(CPUState, fpr[38]) },
2584 { "f40", offsetof(CPUState, fpr[40]) },
2585 { "f42", offsetof(CPUState, fpr[42]) },
2586 { "f44", offsetof(CPUState, fpr[44]) },
2587 { "f46", offsetof(CPUState, fpr[46]) },
2588 { "f48", offsetof(CPUState, fpr[48]) },
2589 { "f50", offsetof(CPUState, fpr[50]) },
2590 { "f52", offsetof(CPUState, fpr[52]) },
2591 { "f54", offsetof(CPUState, fpr[54]) },
2592 { "f56", offsetof(CPUState, fpr[56]) },
2593 { "f58", offsetof(CPUState, fpr[58]) },
2594 { "f60", offsetof(CPUState, fpr[60]) },
2595 { "f62", offsetof(CPUState, fpr[62]) },
2596 { "asi", offsetof(CPUState, asi) },
2597 { "pstate", offsetof(CPUState, pstate) },
2598 { "cansave", offsetof(CPUState, cansave) },
2599 { "canrestore", offsetof(CPUState, canrestore) },
2600 { "otherwin", offsetof(CPUState, otherwin) },
2601 { "wstate", offsetof(CPUState, wstate) },
2602 { "cleanwin", offsetof(CPUState, cleanwin) },
2603 { "fprs", offsetof(CPUState, fprs) },
2609 static void expr_error(Monitor *mon, const char *msg)
2611 monitor_printf(mon, "%s\n", msg);
2612 longjmp(expr_env, 1);
2615 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2616 static int get_monitor_def(target_long *pval, const char *name)
2618 const MonitorDef *md;
2621 for(md = monitor_defs; md->name != NULL; md++) {
2622 if (compare_cmd(name, md->name)) {
2623 if (md->get_value) {
2624 *pval = md->get_value(md, md->offset);
2626 CPUState *env = mon_get_cpu();
2629 ptr = (uint8_t *)env + md->offset;
2632 *pval = *(int32_t *)ptr;
2635 *pval = *(target_long *)ptr;
2648 static void next(void)
2652 while (qemu_isspace(*pch))
2657 static int64_t expr_sum(Monitor *mon);
2659 static int64_t expr_unary(Monitor *mon)
2668 n = expr_unary(mon);
2672 n = -expr_unary(mon);
2676 n = ~expr_unary(mon);
2682 expr_error(mon, "')' expected");
2689 expr_error(mon, "character constant expected");
2693 expr_error(mon, "missing terminating \' character");
2703 while ((*pch >= 'a' && *pch <= 'z') ||
2704 (*pch >= 'A' && *pch <= 'Z') ||
2705 (*pch >= '0' && *pch <= '9') ||
2706 *pch == '_' || *pch == '.') {
2707 if ((q - buf) < sizeof(buf) - 1)
2711 while (qemu_isspace(*pch))
2714 ret = get_monitor_def(®, buf);
2716 expr_error(mon, "unknown register");
2718 expr_error(mon, "no cpu defined");
2723 expr_error(mon, "unexpected end of expression");
2727 #if TARGET_PHYS_ADDR_BITS > 32
2728 n = strtoull(pch, &p, 0);
2730 n = strtoul(pch, &p, 0);
2733 expr_error(mon, "invalid char in expression");
2736 while (qemu_isspace(*pch))
2744 static int64_t expr_prod(Monitor *mon)
2749 val = expr_unary(mon);
2752 if (op != '*' && op != '/' && op != '%')
2755 val2 = expr_unary(mon);
2764 expr_error(mon, "division by zero");
2775 static int64_t expr_logic(Monitor *mon)
2780 val = expr_prod(mon);
2783 if (op != '&' && op != '|' && op != '^')
2786 val2 = expr_prod(mon);
2803 static int64_t expr_sum(Monitor *mon)
2808 val = expr_logic(mon);
2811 if (op != '+' && op != '-')
2814 val2 = expr_logic(mon);
2823 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2826 if (setjmp(expr_env)) {
2830 while (qemu_isspace(*pch))
2832 *pval = expr_sum(mon);
2837 static int get_str(char *buf, int buf_size, const char **pp)
2845 while (qemu_isspace(*p))
2855 while (*p != '\0' && *p != '\"') {
2871 qemu_printf("unsupported escape code: '\\%c'\n", c);
2874 if ((q - buf) < buf_size - 1) {
2878 if ((q - buf) < buf_size - 1) {
2885 qemu_printf("unterminated string\n");
2890 while (*p != '\0' && !qemu_isspace(*p)) {
2891 if ((q - buf) < buf_size - 1) {
2903 * Store the command-name in cmdname, and return a pointer to
2904 * the remaining of the command string.
2906 static const char *get_command_name(const char *cmdline,
2907 char *cmdname, size_t nlen)
2910 const char *p, *pstart;
2913 while (qemu_isspace(*p))
2918 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2923 memcpy(cmdname, pstart, len);
2924 cmdname[len] = '\0';
2929 * Read key of 'type' into 'key' and return the current
2932 static char *key_get_info(const char *type, char **key)
2940 p = strchr(type, ':');
2947 str = qemu_malloc(len + 1);
2948 memcpy(str, type, len);
2955 static int default_fmt_format = 'x';
2956 static int default_fmt_size = 4;
2960 static int is_valid_option(const char *c, const char *typestr)
2968 typestr = strstr(typestr, option);
2969 return (typestr != NULL);
2972 static const mon_cmd_t *monitor_find_command(const char *cmdname)
2974 const mon_cmd_t *cmd;
2976 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
2977 if (compare_cmd(cmdname, cmd->name)) {
2985 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2986 const char *cmdline,
2989 const char *p, *typestr;
2991 const mon_cmd_t *cmd;
2997 monitor_printf(mon, "command='%s'\n", cmdline);
3000 /* extract the command name */
3001 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
3005 cmd = monitor_find_command(cmdname);
3007 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
3011 /* parse the parameters */
3012 typestr = cmd->args_type;
3014 typestr = key_get_info(typestr, &key);
3026 while (qemu_isspace(*p))
3028 if (*typestr == '?') {
3031 /* no optional string: NULL argument */
3035 ret = get_str(buf, sizeof(buf), &p);
3039 monitor_printf(mon, "%s: filename expected\n",
3043 monitor_printf(mon, "%s: block device name expected\n",
3047 monitor_printf(mon, "%s: string expected\n", cmdname);
3052 qdict_put(qdict, key, qstring_from_str(buf));
3057 int count, format, size;
3059 while (qemu_isspace(*p))
3065 if (qemu_isdigit(*p)) {
3067 while (qemu_isdigit(*p)) {
3068 count = count * 10 + (*p - '0');
3106 if (*p != '\0' && !qemu_isspace(*p)) {
3107 monitor_printf(mon, "invalid char in format: '%c'\n",
3112 format = default_fmt_format;
3113 if (format != 'i') {
3114 /* for 'i', not specifying a size gives -1 as size */
3116 size = default_fmt_size;
3117 default_fmt_size = size;
3119 default_fmt_format = format;
3122 format = default_fmt_format;
3123 if (format != 'i') {
3124 size = default_fmt_size;
3129 qdict_put(qdict, "count", qint_from_int(count));
3130 qdict_put(qdict, "format", qint_from_int(format));
3131 qdict_put(qdict, "size", qint_from_int(size));
3139 while (qemu_isspace(*p))
3141 if (*typestr == '?' || *typestr == '.') {
3142 if (*typestr == '?') {
3150 while (qemu_isspace(*p))
3159 if (get_expr(mon, &val, &p))
3161 /* Check if 'i' is greater than 32-bit */
3162 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3163 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3164 monitor_printf(mon, "integer is for 32-bit values\n");
3167 qdict_put(qdict, key, qint_from_int(val));
3172 const char *tmp = p;
3173 int has_option, skip_key = 0;
3179 while (qemu_isspace(*p))
3185 if(!is_valid_option(p, typestr)) {
3187 monitor_printf(mon, "%s: unsupported option -%c\n",
3201 qdict_put(qdict, key, qint_from_int(has_option));
3206 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3212 /* check that all arguments were parsed */
3213 while (qemu_isspace(*p))
3216 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3228 static void monitor_print_error(Monitor *mon)
3230 qerror_print(mon->error);
3231 QDECREF(mon->error);
3235 static void monitor_call_handler(Monitor *mon, const mon_cmd_t *cmd,
3236 const QDict *params)
3238 QObject *data = NULL;
3240 cmd->mhandler.cmd_new(mon, params, &data);
3242 cmd->user_print(mon, data);
3244 qobject_decref(data);
3247 static void handle_user_command(Monitor *mon, const char *cmdline)
3250 const mon_cmd_t *cmd;
3252 qdict = qdict_new();
3254 cmd = monitor_parse_command(mon, cmdline, qdict);
3258 qemu_errors_to_mon(mon);
3260 if (monitor_handler_ported(cmd)) {
3261 monitor_call_handler(mon, cmd, qdict);
3263 cmd->mhandler.cmd(mon, qdict);
3266 if (monitor_has_error(mon))
3267 monitor_print_error(mon);
3269 qemu_errors_to_previous();
3275 static void cmd_completion(const char *name, const char *list)
3277 const char *p, *pstart;
3286 p = pstart + strlen(pstart);
3288 if (len > sizeof(cmd) - 2)
3289 len = sizeof(cmd) - 2;
3290 memcpy(cmd, pstart, len);
3292 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3293 readline_add_completion(cur_mon->rs, cmd);
3301 static void file_completion(const char *input)
3306 char file[1024], file_prefix[1024];
3310 p = strrchr(input, '/');
3313 pstrcpy(file_prefix, sizeof(file_prefix), input);
3314 pstrcpy(path, sizeof(path), ".");
3316 input_path_len = p - input + 1;
3317 memcpy(path, input, input_path_len);
3318 if (input_path_len > sizeof(path) - 1)
3319 input_path_len = sizeof(path) - 1;
3320 path[input_path_len] = '\0';
3321 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3323 #ifdef DEBUG_COMPLETION
3324 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3325 input, path, file_prefix);
3327 ffs = opendir(path);
3335 if (strstart(d->d_name, file_prefix, NULL)) {
3336 memcpy(file, input, input_path_len);
3337 if (input_path_len < sizeof(file))
3338 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3340 /* stat the file to find out if it's a directory.
3341 * In that case add a slash to speed up typing long paths
3344 if(S_ISDIR(sb.st_mode))
3345 pstrcat(file, sizeof(file), "/");
3346 readline_add_completion(cur_mon->rs, file);
3352 static void block_completion_it(void *opaque, BlockDriverState *bs)
3354 const char *name = bdrv_get_device_name(bs);
3355 const char *input = opaque;
3357 if (input[0] == '\0' ||
3358 !strncmp(name, (char *)input, strlen(input))) {
3359 readline_add_completion(cur_mon->rs, name);
3363 /* NOTE: this parser is an approximate form of the real command parser */
3364 static void parse_cmdline(const char *cmdline,
3365 int *pnb_args, char **args)
3374 while (qemu_isspace(*p))
3378 if (nb_args >= MAX_ARGS)
3380 ret = get_str(buf, sizeof(buf), &p);
3381 args[nb_args] = qemu_strdup(buf);
3386 *pnb_args = nb_args;
3389 static const char *next_arg_type(const char *typestr)
3391 const char *p = strchr(typestr, ':');
3392 return (p != NULL ? ++p : typestr);
3395 static void monitor_find_completion(const char *cmdline)
3397 const char *cmdname;
3398 char *args[MAX_ARGS];
3399 int nb_args, i, len;
3400 const char *ptype, *str;
3401 const mon_cmd_t *cmd;
3404 parse_cmdline(cmdline, &nb_args, args);
3405 #ifdef DEBUG_COMPLETION
3406 for(i = 0; i < nb_args; i++) {
3407 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3411 /* if the line ends with a space, it means we want to complete the
3413 len = strlen(cmdline);
3414 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3415 if (nb_args >= MAX_ARGS)
3417 args[nb_args++] = qemu_strdup("");
3420 /* command completion */
3425 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3426 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3427 cmd_completion(cmdname, cmd->name);
3430 /* find the command */
3431 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3432 if (compare_cmd(args[0], cmd->name))
3437 ptype = next_arg_type(cmd->args_type);
3438 for(i = 0; i < nb_args - 2; i++) {
3439 if (*ptype != '\0') {
3440 ptype = next_arg_type(ptype);
3441 while (*ptype == '?')
3442 ptype = next_arg_type(ptype);
3445 str = args[nb_args - 1];
3446 if (*ptype == '-' && ptype[1] != '\0') {
3451 /* file completion */
3452 readline_set_completion_index(cur_mon->rs, strlen(str));
3453 file_completion(str);
3456 /* block device name completion */
3457 readline_set_completion_index(cur_mon->rs, strlen(str));
3458 bdrv_iterate(block_completion_it, (void *)str);
3461 /* XXX: more generic ? */
3462 if (!strcmp(cmd->name, "info")) {
3463 readline_set_completion_index(cur_mon->rs, strlen(str));
3464 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3465 cmd_completion(str, cmd->name);
3467 } else if (!strcmp(cmd->name, "sendkey")) {
3468 char *sep = strrchr(str, '-');
3471 readline_set_completion_index(cur_mon->rs, strlen(str));
3472 for(key = key_defs; key->name != NULL; key++) {
3473 cmd_completion(str, key->name);
3475 } else if (!strcmp(cmd->name, "help|?")) {
3476 readline_set_completion_index(cur_mon->rs, strlen(str));
3477 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3478 cmd_completion(str, cmd->name);
3486 for(i = 0; i < nb_args; i++)
3490 static int monitor_can_read(void *opaque)
3492 Monitor *mon = opaque;
3494 return (mon->suspend_cnt == 0) ? 128 : 0;
3497 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3499 Monitor *old_mon = cur_mon;
3505 for (i = 0; i < size; i++)
3506 readline_handle_byte(cur_mon->rs, buf[i]);
3508 if (size == 0 || buf[size - 1] != 0)
3509 monitor_printf(cur_mon, "corrupted command\n");
3511 handle_user_command(cur_mon, (char *)buf);
3517 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3519 monitor_suspend(mon);
3520 handle_user_command(mon, cmdline);
3521 monitor_resume(mon);
3524 int monitor_suspend(Monitor *mon)
3532 void monitor_resume(Monitor *mon)
3536 if (--mon->suspend_cnt == 0)
3537 readline_show_prompt(mon->rs);
3540 static void monitor_event(void *opaque, int event)
3542 Monitor *mon = opaque;
3545 case CHR_EVENT_MUX_IN:
3547 if (mon->reset_seen) {
3548 readline_restart(mon->rs);
3549 monitor_resume(mon);
3552 mon->suspend_cnt = 0;
3556 case CHR_EVENT_MUX_OUT:
3557 if (mon->reset_seen) {
3558 if (mon->suspend_cnt == 0) {
3559 monitor_printf(mon, "\n");
3562 monitor_suspend(mon);
3569 case CHR_EVENT_OPENED:
3570 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3571 "information\n", QEMU_VERSION);
3572 if (!mon->mux_out) {
3573 readline_show_prompt(mon->rs);
3575 mon->reset_seen = 1;
3589 const char *monitor_cmdline_parse(const char *cmdline, int *flags)
3593 if (strstart(cmdline, "control,", &dev)) {
3594 if (strstart(dev, "vc", NULL)) {
3595 fprintf(stderr, "qemu: control mode is for low-level interaction ");
3596 fprintf(stderr, "cannot be used with device 'vc'\n");
3599 *flags &= ~MONITOR_USE_READLINE;
3600 *flags |= MONITOR_USE_CONTROL;
3607 void monitor_init(CharDriverState *chr, int flags)
3609 static int is_first_init = 1;
3612 if (is_first_init) {
3613 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3617 mon = qemu_mallocz(sizeof(*mon));
3621 if (flags & MONITOR_USE_READLINE) {
3622 mon->rs = readline_init(mon, monitor_find_completion);
3623 monitor_read_command(mon, 0);
3626 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3629 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3630 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3634 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3636 BlockDriverState *bs = opaque;
3639 if (bdrv_set_key(bs, password) != 0) {
3640 monitor_printf(mon, "invalid password\n");
3643 if (mon->password_completion_cb)
3644 mon->password_completion_cb(mon->password_opaque, ret);
3646 monitor_read_command(mon, 1);
3649 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3650 BlockDriverCompletionFunc *completion_cb,
3655 if (!bdrv_key_required(bs)) {
3657 completion_cb(opaque, 0);
3661 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3662 bdrv_get_encrypted_filename(bs));
3664 mon->password_completion_cb = completion_cb;
3665 mon->password_opaque = opaque;
3667 err = monitor_read_password(mon, bdrv_password_cb, bs);
3669 if (err && completion_cb)
3670 completion_cb(opaque, err);
3673 typedef struct QemuErrorSink QemuErrorSink;
3674 struct QemuErrorSink {
3683 QemuErrorSink *previous;
3686 static QemuErrorSink *qemu_error_sink;
3688 void qemu_errors_to_file(FILE *fp)
3690 QemuErrorSink *sink;
3692 sink = qemu_mallocz(sizeof(*sink));
3693 sink->dest = ERR_SINK_FILE;
3695 sink->previous = qemu_error_sink;
3696 qemu_error_sink = sink;
3699 void qemu_errors_to_mon(Monitor *mon)
3701 QemuErrorSink *sink;
3703 sink = qemu_mallocz(sizeof(*sink));
3704 sink->dest = ERR_SINK_MONITOR;
3706 sink->previous = qemu_error_sink;
3707 qemu_error_sink = sink;
3710 void qemu_errors_to_previous(void)
3712 QemuErrorSink *sink;
3714 assert(qemu_error_sink != NULL);
3715 sink = qemu_error_sink;
3716 qemu_error_sink = sink->previous;
3720 void qemu_error(const char *fmt, ...)
3724 assert(qemu_error_sink != NULL);
3725 switch (qemu_error_sink->dest) {
3727 va_start(args, fmt);
3728 vfprintf(qemu_error_sink->fp, fmt, args);
3731 case ERR_SINK_MONITOR:
3732 va_start(args, fmt);
3733 monitor_vprintf(qemu_error_sink->mon, fmt, args);
3739 void qemu_error_internal(const char *file, int linenr, const char *func,
3740 const char *fmt, ...)
3745 assert(qemu_error_sink != NULL);
3748 qerror = qerror_from_info(file, linenr, func, fmt, &va);
3751 switch (qemu_error_sink->dest) {
3753 qerror_print(qerror);
3756 case ERR_SINK_MONITOR:
3757 assert(qemu_error_sink->mon->error == NULL);
3758 qemu_error_sink->mon->error = qerror;