4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/pcmcia.h"
31 #include "qemu-char.h"
37 #include "audio/audio.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
46 //#define DEBUG_COMPLETION
52 * 'B' block device name
53 * 's' string (accept optional quote)
55 * 'l' target long (32 or 64 bit)
56 * '/' optional gdb-like print format (like "/10x")
58 * '?' optional type (for 'F', 's' and 'i')
62 typedef struct mon_cmd_t {
64 const char *args_type;
72 LIST_ENTRY(Monitor) entry;
75 static LIST_HEAD(mon_list, Monitor) mon_list;
77 static const mon_cmd_t mon_cmds[];
78 static const mon_cmd_t info_cmds[];
80 static uint8_t term_outbuf[1024];
81 static int term_outbuf_index;
82 static BlockDriverCompletionFunc *password_completion_cb;
83 static void *password_opaque;
85 Monitor *cur_mon = NULL;
87 static void monitor_start_input(void);
89 static CPUState *mon_cpu = NULL;
91 static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
94 readline_start("Password: ", 1, readline_func, opaque);
97 void monitor_flush(Monitor *mon)
101 if (term_outbuf_index > 0) {
102 LIST_FOREACH(m, &mon_list, entry) {
103 if (m->chr->focus == 0)
104 qemu_chr_write(m->chr, term_outbuf, term_outbuf_index);
106 term_outbuf_index = 0;
110 /* flush at every end of line or if the buffer is full */
111 static void monitor_puts(Monitor *mon, const char *str)
119 term_outbuf[term_outbuf_index++] = '\r';
120 term_outbuf[term_outbuf_index++] = c;
121 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
127 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
130 vsnprintf(buf, sizeof(buf), fmt, ap);
131 monitor_puts(mon, buf);
134 void monitor_printf(Monitor *mon, const char *fmt, ...)
138 monitor_vprintf(mon, fmt, ap);
142 void monitor_print_filename(Monitor *mon, const char *filename)
146 for (i = 0; filename[i]; i++) {
147 switch (filename[i]) {
151 monitor_printf(mon, "\\%c", filename[i]);
154 monitor_printf(mon, "\\t");
157 monitor_printf(mon, "\\r");
160 monitor_printf(mon, "\\n");
163 monitor_printf(mon, "%c", filename[i]);
169 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
173 monitor_vprintf((Monitor *)stream, fmt, ap);
178 static int compare_cmd(const char *name, const char *list)
180 const char *p, *pstart;
188 p = pstart + strlen(pstart);
189 if ((p - pstart) == len && !memcmp(pstart, name, len))
198 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
199 const char *prefix, const char *name)
201 const mon_cmd_t *cmd;
203 for(cmd = cmds; cmd->name != NULL; cmd++) {
204 if (!name || !strcmp(name, cmd->name))
205 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
206 cmd->params, cmd->help);
210 static void help_cmd(Monitor *mon, const char *name)
212 if (name && !strcmp(name, "info")) {
213 help_cmd_dump(mon, info_cmds, "info ", NULL);
215 help_cmd_dump(mon, mon_cmds, "", name);
216 if (name && !strcmp(name, "log")) {
217 const CPULogItem *item;
218 monitor_printf(mon, "Log items (comma separated):\n");
219 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
220 for(item = cpu_log_items; item->mask != 0; item++) {
221 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
227 static void do_commit(Monitor *mon, const char *device)
231 all_devices = !strcmp(device, "all");
232 for (i = 0; i < nb_drives; i++) {
234 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
235 bdrv_commit(drives_table[i].bdrv);
239 static void do_info(Monitor *mon, const char *item)
241 const mon_cmd_t *cmd;
242 void (*handler)(Monitor *);
246 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
247 if (compare_cmd(item, cmd->name))
251 help_cmd(mon, "info");
254 handler = cmd->handler;
258 static void do_info_version(Monitor *mon)
260 monitor_printf(mon, "%s\n", QEMU_VERSION);
263 static void do_info_name(Monitor *mon)
266 monitor_printf(mon, "%s\n", qemu_name);
269 #if defined(TARGET_I386)
270 static void do_info_hpet(Monitor *mon)
272 monitor_printf(mon, "HPET is %s by QEMU\n",
273 (no_hpet) ? "disabled" : "enabled");
277 static void do_info_uuid(Monitor *mon)
279 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
280 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
281 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
282 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
283 qemu_uuid[14], qemu_uuid[15]);
286 /* get the current CPU defined by the user */
287 static int mon_set_cpu(int cpu_index)
291 for(env = first_cpu; env != NULL; env = env->next_cpu) {
292 if (env->cpu_index == cpu_index) {
300 static CPUState *mon_get_cpu(void)
308 static void do_info_registers(Monitor *mon)
315 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
318 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
323 static void do_info_cpus(Monitor *mon)
327 /* just to set the default cpu if not already done */
330 for(env = first_cpu; env != NULL; env = env->next_cpu) {
331 monitor_printf(mon, "%c CPU #%d:",
332 (env == mon_cpu) ? '*' : ' ',
334 #if defined(TARGET_I386)
335 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
336 env->eip + env->segs[R_CS].base);
337 #elif defined(TARGET_PPC)
338 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
339 #elif defined(TARGET_SPARC)
340 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
342 #elif defined(TARGET_MIPS)
343 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
346 monitor_printf(mon, " (halted)");
347 monitor_printf(mon, "\n");
351 static void do_cpu_set(Monitor *mon, int index)
353 if (mon_set_cpu(index) < 0)
354 monitor_printf(mon, "Invalid CPU index\n");
357 static void do_info_jit(Monitor *mon)
359 dump_exec_info((FILE *)mon, monitor_fprintf);
362 static void do_info_history(Monitor *mon)
369 str = readline_get_history(i);
372 monitor_printf(mon, "%d: '%s'\n", i, str);
377 #if defined(TARGET_PPC)
378 /* XXX: not implemented in other targets */
379 static void do_info_cpu_stats(Monitor *mon)
384 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
388 static void do_quit(Monitor *mon)
393 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
395 if (bdrv_is_inserted(bs)) {
397 if (!bdrv_is_removable(bs)) {
398 monitor_printf(mon, "device is not removable\n");
401 if (bdrv_is_locked(bs)) {
402 monitor_printf(mon, "device is locked\n");
411 static void do_eject(Monitor *mon, int force, const char *filename)
413 BlockDriverState *bs;
415 bs = bdrv_find(filename);
417 monitor_printf(mon, "device not found\n");
420 eject_device(mon, bs, force);
423 static void do_change_block(Monitor *mon, const char *device,
424 const char *filename, const char *fmt)
426 BlockDriverState *bs;
427 BlockDriver *drv = NULL;
429 bs = bdrv_find(device);
431 monitor_printf(mon, "device not found\n");
435 drv = bdrv_find_format(fmt);
437 monitor_printf(mon, "invalid format %s\n", fmt);
441 if (eject_device(mon, bs, 0) < 0)
443 bdrv_open2(bs, filename, 0, drv);
444 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
447 static void change_vnc_password_cb(Monitor *mon, const char *password,
450 if (vnc_display_password(NULL, password) < 0)
451 monitor_printf(mon, "could not set VNC server password\n");
453 monitor_start_input();
456 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
458 if (strcmp(target, "passwd") == 0 ||
459 strcmp(target, "password") == 0) {
462 strncpy(password, arg, sizeof(password));
463 password[sizeof(password) - 1] = '\0';
464 change_vnc_password_cb(mon, password, NULL);
466 monitor_read_password(mon, change_vnc_password_cb, NULL);
469 if (vnc_display_open(NULL, target) < 0)
470 monitor_printf(mon, "could not start VNC server on %s\n", target);
474 static void do_change(Monitor *mon, const char *device, const char *target,
477 if (strcmp(device, "vnc") == 0) {
478 do_change_vnc(mon, target, arg);
480 do_change_block(mon, device, target, arg);
484 static void do_screen_dump(Monitor *mon, const char *filename)
486 vga_hw_screen_dump(filename);
489 static void do_logfile(Monitor *mon, const char *filename)
491 cpu_set_log_filename(filename);
494 static void do_log(Monitor *mon, const char *items)
498 if (!strcmp(items, "none")) {
501 mask = cpu_str_to_log_mask(items);
503 help_cmd(mon, "log");
510 static void do_stop(Monitor *mon)
512 vm_stop(EXCP_INTERRUPT);
515 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
517 struct bdrv_iterate_context {
522 static void do_cont(Monitor *mon)
524 struct bdrv_iterate_context context = { mon, 0 };
526 bdrv_iterate(encrypted_bdrv_it, &context);
527 /* only resume the vm if all keys are set and valid */
532 static void bdrv_key_cb(void *opaque, int err)
534 Monitor *mon = opaque;
536 /* another key was set successfully, retry to continue */
541 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
543 struct bdrv_iterate_context *context = opaque;
545 if (!context->err && bdrv_key_required(bs)) {
546 context->err = -EBUSY;
547 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
552 #ifdef CONFIG_GDBSTUB
553 static void do_gdbserver(Monitor *mon, const char *port)
556 port = DEFAULT_GDBSTUB_PORT;
557 if (gdbserver_start(port) < 0) {
558 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
561 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
566 static void monitor_printc(Monitor *mon, int c)
568 monitor_printf(mon, "'");
571 monitor_printf(mon, "\\'");
574 monitor_printf(mon, "\\\\");
577 monitor_printf(mon, "\\n");
580 monitor_printf(mon, "\\r");
583 if (c >= 32 && c <= 126) {
584 monitor_printf(mon, "%c", c);
586 monitor_printf(mon, "\\x%02x", c);
590 monitor_printf(mon, "'");
593 static void memory_dump(Monitor *mon, int count, int format, int wsize,
594 target_phys_addr_t addr, int is_physical)
597 int nb_per_line, l, line_size, i, max_digits, len;
605 if (!env && !is_physical)
610 } else if (wsize == 4) {
613 /* as default we use the current CS size */
617 if ((env->efer & MSR_EFER_LMA) &&
618 (env->segs[R_CS].flags & DESC_L_MASK))
622 if (!(env->segs[R_CS].flags & DESC_B_MASK))
627 monitor_disas(mon, env, addr, count, is_physical, flags);
636 nb_per_line = line_size / wsize;
641 max_digits = (wsize * 8 + 2) / 3;
645 max_digits = (wsize * 8) / 4;
649 max_digits = (wsize * 8 * 10 + 32) / 33;
658 monitor_printf(mon, TARGET_FMT_plx ":", addr);
660 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
665 cpu_physical_memory_rw(addr, buf, l, 0);
670 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
671 monitor_printf(mon, " Cannot access memory\n");
680 v = ldub_raw(buf + i);
683 v = lduw_raw(buf + i);
686 v = (uint32_t)ldl_raw(buf + i);
689 v = ldq_raw(buf + i);
692 monitor_printf(mon, " ");
695 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
698 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
701 monitor_printf(mon, "%*" PRIu64, max_digits, v);
704 monitor_printf(mon, "%*" PRId64, max_digits, v);
707 monitor_printc(mon, v);
712 monitor_printf(mon, "\n");
718 #if TARGET_LONG_BITS == 64
719 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
721 #define GET_TLONG(h, l) (l)
724 static void do_memory_dump(Monitor *mon, int count, int format, int size,
725 uint32_t addrh, uint32_t addrl)
727 target_long addr = GET_TLONG(addrh, addrl);
728 memory_dump(mon, count, format, size, addr, 0);
731 #if TARGET_PHYS_ADDR_BITS > 32
732 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
734 #define GET_TPHYSADDR(h, l) (l)
737 static void do_physical_memory_dump(Monitor *mon, int count, int format,
738 int size, uint32_t addrh, uint32_t addrl)
741 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
742 memory_dump(mon, count, format, size, addr, 1);
745 static void do_print(Monitor *mon, int count, int format, int size,
746 unsigned int valh, unsigned int vall)
748 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
749 #if TARGET_PHYS_ADDR_BITS == 32
752 monitor_printf(mon, "%#o", val);
755 monitor_printf(mon, "%#x", val);
758 monitor_printf(mon, "%u", val);
762 monitor_printf(mon, "%d", val);
765 monitor_printc(mon, val);
771 monitor_printf(mon, "%#" PRIo64, val);
774 monitor_printf(mon, "%#" PRIx64, val);
777 monitor_printf(mon, "%" PRIu64, val);
781 monitor_printf(mon, "%" PRId64, val);
784 monitor_printc(mon, val);
788 monitor_printf(mon, "\n");
791 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
792 uint32_t size, const char *filename)
795 target_long addr = GET_TLONG(valh, vall);
804 f = fopen(filename, "wb");
806 monitor_printf(mon, "could not open '%s'\n", filename);
813 cpu_memory_rw_debug(env, addr, buf, l, 0);
814 fwrite(buf, 1, l, f);
821 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
822 unsigned int vall, uint32_t size,
823 const char *filename)
828 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
830 f = fopen(filename, "wb");
832 monitor_printf(mon, "could not open '%s'\n", filename);
839 cpu_physical_memory_rw(addr, buf, l, 0);
840 fwrite(buf, 1, l, f);
848 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
855 for(addr = start; addr < (start + size); addr++) {
856 cpu_physical_memory_rw(addr, buf, 1, 0);
857 /* BSD sum algorithm ('sum' Unix command) */
858 sum = (sum >> 1) | (sum << 15);
861 monitor_printf(mon, "%05d\n", sum);
869 static const KeyDef key_defs[] = {
896 { 0x0e, "backspace" },
933 { 0x37, "asterisk" },
936 { 0x3a, "caps_lock" },
947 { 0x45, "num_lock" },
948 { 0x46, "scroll_lock" },
950 { 0xb5, "kp_divide" },
951 { 0x37, "kp_multiply" },
952 { 0x4a, "kp_subtract" },
954 { 0x9c, "kp_enter" },
955 { 0x53, "kp_decimal" },
988 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1003 { 0xfe, "compose" },
1008 static int get_keycode(const char *key)
1014 for(p = key_defs; p->name != NULL; p++) {
1015 if (!strcmp(key, p->name))
1018 if (strstart(key, "0x", NULL)) {
1019 ret = strtoul(key, &endp, 0);
1020 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1026 #define MAX_KEYCODES 16
1027 static uint8_t keycodes[MAX_KEYCODES];
1028 static int nb_pending_keycodes;
1029 static QEMUTimer *key_timer;
1031 static void release_keys(void *opaque)
1035 while (nb_pending_keycodes > 0) {
1036 nb_pending_keycodes--;
1037 keycode = keycodes[nb_pending_keycodes];
1039 kbd_put_keycode(0xe0);
1040 kbd_put_keycode(keycode | 0x80);
1044 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1047 char keyname_buf[16];
1049 int keyname_len, keycode, i;
1051 if (nb_pending_keycodes > 0) {
1052 qemu_del_timer(key_timer);
1059 separator = strchr(string, '-');
1060 keyname_len = separator ? separator - string : strlen(string);
1061 if (keyname_len > 0) {
1062 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1063 if (keyname_len > sizeof(keyname_buf) - 1) {
1064 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1067 if (i == MAX_KEYCODES) {
1068 monitor_printf(mon, "too many keys\n");
1071 keyname_buf[keyname_len] = 0;
1072 keycode = get_keycode(keyname_buf);
1074 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1077 keycodes[i++] = keycode;
1081 string = separator + 1;
1083 nb_pending_keycodes = i;
1084 /* key down events */
1085 for (i = 0; i < nb_pending_keycodes; i++) {
1086 keycode = keycodes[i];
1088 kbd_put_keycode(0xe0);
1089 kbd_put_keycode(keycode & 0x7f);
1091 /* delayed key up events */
1092 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1093 muldiv64(ticks_per_sec, hold_time, 1000));
1096 static int mouse_button_state;
1098 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1102 dx = strtol(dx_str, NULL, 0);
1103 dy = strtol(dy_str, NULL, 0);
1106 dz = strtol(dz_str, NULL, 0);
1107 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1110 static void do_mouse_button(Monitor *mon, int button_state)
1112 mouse_button_state = button_state;
1113 kbd_mouse_event(0, 0, 0, mouse_button_state);
1116 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1117 int addr, int has_index, int index)
1123 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1131 val = cpu_inb(NULL, addr);
1135 val = cpu_inw(NULL, addr);
1139 val = cpu_inl(NULL, addr);
1143 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1144 suffix, addr, size * 2, val);
1147 /* boot_set handler */
1148 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1149 static void *boot_opaque;
1151 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1153 qemu_boot_set_handler = func;
1154 boot_opaque = opaque;
1157 static void do_boot_set(Monitor *mon, const char *bootdevice)
1161 if (qemu_boot_set_handler) {
1162 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1164 monitor_printf(mon, "boot device list now set to %s\n",
1167 monitor_printf(mon, "setting boot device list failed with "
1170 monitor_printf(mon, "no function defined to set boot device list for "
1171 "this architecture\n");
1175 static void do_system_reset(Monitor *mon)
1177 qemu_system_reset_request();
1180 static void do_system_powerdown(Monitor *mon)
1182 qemu_system_powerdown_request();
1185 #if defined(TARGET_I386)
1186 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1188 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1191 pte & PG_GLOBAL_MASK ? 'G' : '-',
1192 pte & PG_PSE_MASK ? 'P' : '-',
1193 pte & PG_DIRTY_MASK ? 'D' : '-',
1194 pte & PG_ACCESSED_MASK ? 'A' : '-',
1195 pte & PG_PCD_MASK ? 'C' : '-',
1196 pte & PG_PWT_MASK ? 'T' : '-',
1197 pte & PG_USER_MASK ? 'U' : '-',
1198 pte & PG_RW_MASK ? 'W' : '-');
1201 static void tlb_info(Monitor *mon)
1205 uint32_t pgd, pde, pte;
1207 env = mon_get_cpu();
1211 if (!(env->cr[0] & CR0_PG_MASK)) {
1212 monitor_printf(mon, "PG disabled\n");
1215 pgd = env->cr[3] & ~0xfff;
1216 for(l1 = 0; l1 < 1024; l1++) {
1217 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1218 pde = le32_to_cpu(pde);
1219 if (pde & PG_PRESENT_MASK) {
1220 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1221 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1223 for(l2 = 0; l2 < 1024; l2++) {
1224 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1225 (uint8_t *)&pte, 4);
1226 pte = le32_to_cpu(pte);
1227 if (pte & PG_PRESENT_MASK) {
1228 print_pte(mon, (l1 << 22) + (l2 << 12),
1238 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1239 uint32_t end, int prot)
1242 prot1 = *plast_prot;
1243 if (prot != prot1) {
1244 if (*pstart != -1) {
1245 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1246 *pstart, end, end - *pstart,
1247 prot1 & PG_USER_MASK ? 'u' : '-',
1249 prot1 & PG_RW_MASK ? 'w' : '-');
1259 static void mem_info(Monitor *mon)
1262 int l1, l2, prot, last_prot;
1263 uint32_t pgd, pde, pte, start, end;
1265 env = mon_get_cpu();
1269 if (!(env->cr[0] & CR0_PG_MASK)) {
1270 monitor_printf(mon, "PG disabled\n");
1273 pgd = env->cr[3] & ~0xfff;
1276 for(l1 = 0; l1 < 1024; l1++) {
1277 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1278 pde = le32_to_cpu(pde);
1280 if (pde & PG_PRESENT_MASK) {
1281 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1282 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1283 mem_print(mon, &start, &last_prot, end, prot);
1285 for(l2 = 0; l2 < 1024; l2++) {
1286 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1287 (uint8_t *)&pte, 4);
1288 pte = le32_to_cpu(pte);
1289 end = (l1 << 22) + (l2 << 12);
1290 if (pte & PG_PRESENT_MASK) {
1291 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1295 mem_print(mon, &start, &last_prot, end, prot);
1300 mem_print(mon, &start, &last_prot, end, prot);
1306 #if defined(TARGET_SH4)
1308 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1310 monitor_printf(mon, " tlb%i:\t"
1311 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1312 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1313 "dirty=%hhu writethrough=%hhu\n",
1315 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1316 tlb->v, tlb->sh, tlb->c, tlb->pr,
1320 static void tlb_info(Monitor *mon)
1322 CPUState *env = mon_get_cpu();
1325 monitor_printf (mon, "ITLB:\n");
1326 for (i = 0 ; i < ITLB_SIZE ; i++)
1327 print_tlb (mon, i, &env->itlb[i]);
1328 monitor_printf (mon, "UTLB:\n");
1329 for (i = 0 ; i < UTLB_SIZE ; i++)
1330 print_tlb (mon, i, &env->utlb[i]);
1335 static void do_info_kqemu(Monitor *mon)
1341 env = mon_get_cpu();
1343 monitor_printf(mon, "No cpu initialized yet");
1346 val = env->kqemu_enabled;
1347 monitor_printf(mon, "kqemu support: ");
1351 monitor_printf(mon, "disabled\n");
1354 monitor_printf(mon, "enabled for user code\n");
1357 monitor_printf(mon, "enabled for user and kernel code\n");
1361 monitor_printf(mon, "kqemu support: not compiled\n");
1365 static void do_info_kvm(Monitor *mon)
1368 monitor_printf(mon, "kvm support: ");
1370 monitor_printf(mon, "enabled\n");
1372 monitor_printf(mon, "disabled\n");
1374 monitor_printf(mon, "kvm support: not compiled\n");
1378 #ifdef CONFIG_PROFILER
1382 int64_t kqemu_exec_count;
1384 int64_t kqemu_ret_int_count;
1385 int64_t kqemu_ret_excp_count;
1386 int64_t kqemu_ret_intr_count;
1388 static void do_info_profile(Monitor *mon)
1394 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1395 dev_time, dev_time / (double)ticks_per_sec);
1396 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1397 qemu_time, qemu_time / (double)ticks_per_sec);
1398 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1399 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1401 kqemu_time, kqemu_time / (double)ticks_per_sec,
1402 kqemu_time / (double)total * 100.0,
1404 kqemu_ret_int_count,
1405 kqemu_ret_excp_count,
1406 kqemu_ret_intr_count);
1409 kqemu_exec_count = 0;
1411 kqemu_ret_int_count = 0;
1412 kqemu_ret_excp_count = 0;
1413 kqemu_ret_intr_count = 0;
1415 kqemu_record_dump();
1419 static void do_info_profile(Monitor *mon)
1421 monitor_printf(mon, "Internal profiler not compiled\n");
1425 /* Capture support */
1426 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1428 static void do_info_capture(Monitor *mon)
1433 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1434 monitor_printf(mon, "[%d]: ", i);
1435 s->ops.info (s->opaque);
1439 static void do_stop_capture(Monitor *mon, int n)
1444 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1446 s->ops.destroy (s->opaque);
1447 LIST_REMOVE (s, entries);
1455 static void do_wav_capture(Monitor *mon, const char *path,
1456 int has_freq, int freq,
1457 int has_bits, int bits,
1458 int has_channels, int nchannels)
1462 s = qemu_mallocz (sizeof (*s));
1464 freq = has_freq ? freq : 44100;
1465 bits = has_bits ? bits : 16;
1466 nchannels = has_channels ? nchannels : 2;
1468 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1469 monitor_printf(mon, "Faied to add wave capture\n");
1472 LIST_INSERT_HEAD (&capture_head, s, entries);
1476 #if defined(TARGET_I386)
1477 static void do_inject_nmi(Monitor *mon, int cpu_index)
1481 for (env = first_cpu; env != NULL; env = env->next_cpu)
1482 if (env->cpu_index == cpu_index) {
1483 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1489 static void do_info_status(Monitor *mon)
1492 monitor_printf(mon, "VM status: running\n");
1494 monitor_printf(mon, "VM status: paused\n");
1498 static void do_balloon(Monitor *mon, int value)
1500 ram_addr_t target = value;
1501 qemu_balloon(target << 20);
1504 static void do_info_balloon(Monitor *mon)
1508 actual = qemu_balloon_status();
1509 if (kvm_enabled() && !kvm_has_sync_mmu())
1510 monitor_printf(mon, "Using KVM without synchronous MMU, "
1511 "ballooning disabled\n");
1512 else if (actual == 0)
1513 monitor_printf(mon, "Ballooning not activated in VM\n");
1515 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1518 /* Please update qemu-doc.texi when adding or changing commands */
1519 static const mon_cmd_t mon_cmds[] = {
1520 { "help|?", "s?", help_cmd,
1521 "[cmd]", "show the help" },
1522 { "commit", "s", do_commit,
1523 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1524 { "info", "s?", do_info,
1525 "subcommand", "show various information about the system state" },
1526 { "q|quit", "", do_quit,
1527 "", "quit the emulator" },
1528 { "eject", "-fB", do_eject,
1529 "[-f] device", "eject a removable medium (use -f to force it)" },
1530 { "change", "BFs?", do_change,
1531 "device filename [format]", "change a removable medium, optional format" },
1532 { "screendump", "F", do_screen_dump,
1533 "filename", "save screen into PPM image 'filename'" },
1534 { "logfile", "F", do_logfile,
1535 "filename", "output logs to 'filename'" },
1536 { "log", "s", do_log,
1537 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1538 { "savevm", "s?", do_savevm,
1539 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1540 { "loadvm", "s", do_loadvm,
1541 "tag|id", "restore a VM snapshot from its tag or id" },
1542 { "delvm", "s", do_delvm,
1543 "tag|id", "delete a VM snapshot from its tag or id" },
1544 { "stop", "", do_stop,
1545 "", "stop emulation", },
1546 { "c|cont", "", do_cont,
1547 "", "resume emulation", },
1548 #ifdef CONFIG_GDBSTUB
1549 { "gdbserver", "s?", do_gdbserver,
1550 "[port]", "start gdbserver session (default port=1234)", },
1552 { "x", "/l", do_memory_dump,
1553 "/fmt addr", "virtual memory dump starting at 'addr'", },
1554 { "xp", "/l", do_physical_memory_dump,
1555 "/fmt addr", "physical memory dump starting at 'addr'", },
1556 { "p|print", "/l", do_print,
1557 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1558 { "i", "/ii.", do_ioport_read,
1559 "/fmt addr", "I/O port read" },
1561 { "sendkey", "si?", do_sendkey,
1562 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1563 { "system_reset", "", do_system_reset,
1564 "", "reset the system" },
1565 { "system_powerdown", "", do_system_powerdown,
1566 "", "send system power down event" },
1567 { "sum", "ii", do_sum,
1568 "addr size", "compute the checksum of a memory region" },
1569 { "usb_add", "s", do_usb_add,
1570 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1571 { "usb_del", "s", do_usb_del,
1572 "device", "remove USB device 'bus.addr'" },
1573 { "cpu", "i", do_cpu_set,
1574 "index", "set the default CPU" },
1575 { "mouse_move", "sss?", do_mouse_move,
1576 "dx dy [dz]", "send mouse move events" },
1577 { "mouse_button", "i", do_mouse_button,
1578 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1579 { "mouse_set", "i", do_mouse_set,
1580 "index", "set which mouse device receives events" },
1582 { "wavcapture", "si?i?i?", do_wav_capture,
1583 "path [frequency bits channels]",
1584 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1586 { "stopcapture", "i", do_stop_capture,
1587 "capture index", "stop capture" },
1588 { "memsave", "lis", do_memory_save,
1589 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1590 { "pmemsave", "lis", do_physical_memory_save,
1591 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1592 { "boot_set", "s", do_boot_set,
1593 "bootdevice", "define new values for the boot device list" },
1594 #if defined(TARGET_I386)
1595 { "nmi", "i", do_inject_nmi,
1596 "cpu", "inject an NMI on the given CPU", },
1598 { "migrate", "-ds", do_migrate,
1599 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1600 { "migrate_cancel", "", do_migrate_cancel,
1601 "", "cancel the current VM migration" },
1602 { "migrate_set_speed", "s", do_migrate_set_speed,
1603 "value", "set maximum speed (in bytes) for migrations" },
1604 #if defined(TARGET_I386)
1605 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1606 "[file=file][,if=type][,bus=n]\n"
1607 "[,unit=m][,media=d][index=i]\n"
1608 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1609 "[snapshot=on|off][,cache=on|off]",
1610 "add drive to PCI storage controller" },
1611 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1612 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1613 { "host_net_add", "ss", net_host_device_add,
1614 "[tap,user,socket,vde] options", "add host VLAN client" },
1615 { "host_net_remove", "is", net_host_device_remove,
1616 "vlan_id name", "remove host VLAN client" },
1618 { "balloon", "i", do_balloon,
1619 "target", "request VM to change it's memory allocation (in MB)" },
1620 { "set_link", "ss", do_set_link,
1621 "name [up|down]", "change the link status of a network adapter" },
1625 /* Please update qemu-doc.texi when adding or changing commands */
1626 static const mon_cmd_t info_cmds[] = {
1627 { "version", "", do_info_version,
1628 "", "show the version of QEMU" },
1629 { "network", "", do_info_network,
1630 "", "show the network state" },
1631 { "chardev", "", qemu_chr_info,
1632 "", "show the character devices" },
1633 { "block", "", bdrv_info,
1634 "", "show the block devices" },
1635 { "blockstats", "", bdrv_info_stats,
1636 "", "show block device statistics" },
1637 { "registers", "", do_info_registers,
1638 "", "show the cpu registers" },
1639 { "cpus", "", do_info_cpus,
1640 "", "show infos for each CPU" },
1641 { "history", "", do_info_history,
1642 "", "show the command line history", },
1643 { "irq", "", irq_info,
1644 "", "show the interrupts statistics (if available)", },
1645 { "pic", "", pic_info,
1646 "", "show i8259 (PIC) state", },
1647 { "pci", "", pci_info,
1648 "", "show PCI info", },
1649 #if defined(TARGET_I386) || defined(TARGET_SH4)
1650 { "tlb", "", tlb_info,
1651 "", "show virtual to physical memory mappings", },
1653 #if defined(TARGET_I386)
1654 { "mem", "", mem_info,
1655 "", "show the active virtual memory mappings", },
1656 { "hpet", "", do_info_hpet,
1657 "", "show state of HPET", },
1659 { "jit", "", do_info_jit,
1660 "", "show dynamic compiler info", },
1661 { "kqemu", "", do_info_kqemu,
1662 "", "show KQEMU information", },
1663 { "kvm", "", do_info_kvm,
1664 "", "show KVM information", },
1665 { "usb", "", usb_info,
1666 "", "show guest USB devices", },
1667 { "usbhost", "", usb_host_info,
1668 "", "show host USB devices", },
1669 { "profile", "", do_info_profile,
1670 "", "show profiling information", },
1671 { "capture", "", do_info_capture,
1672 "", "show capture information" },
1673 { "snapshots", "", do_info_snapshots,
1674 "", "show the currently saved VM snapshots" },
1675 { "status", "", do_info_status,
1676 "", "show the current VM status (running|paused)" },
1677 { "pcmcia", "", pcmcia_info,
1678 "", "show guest PCMCIA status" },
1679 { "mice", "", do_info_mice,
1680 "", "show which guest mouse is receiving events" },
1681 { "vnc", "", do_info_vnc,
1682 "", "show the vnc server status"},
1683 { "name", "", do_info_name,
1684 "", "show the current VM name" },
1685 { "uuid", "", do_info_uuid,
1686 "", "show the current VM UUID" },
1687 #if defined(TARGET_PPC)
1688 { "cpustats", "", do_info_cpu_stats,
1689 "", "show CPU statistics", },
1691 #if defined(CONFIG_SLIRP)
1692 { "slirp", "", do_info_slirp,
1693 "", "show SLIRP statistics", },
1695 { "migrate", "", do_info_migrate, "", "show migration status" },
1696 { "balloon", "", do_info_balloon,
1697 "", "show balloon information" },
1701 /*******************************************************************/
1703 static const char *pch;
1704 static jmp_buf expr_env;
1709 typedef struct MonitorDef {
1712 target_long (*get_value)(const struct MonitorDef *md, int val);
1716 #if defined(TARGET_I386)
1717 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1719 CPUState *env = mon_get_cpu();
1722 return env->eip + env->segs[R_CS].base;
1726 #if defined(TARGET_PPC)
1727 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1729 CPUState *env = mon_get_cpu();
1737 for (i = 0; i < 8; i++)
1738 u |= env->crf[i] << (32 - (4 * i));
1743 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1745 CPUState *env = mon_get_cpu();
1751 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1753 CPUState *env = mon_get_cpu();
1759 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1761 CPUState *env = mon_get_cpu();
1764 return cpu_ppc_load_decr(env);
1767 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1769 CPUState *env = mon_get_cpu();
1772 return cpu_ppc_load_tbu(env);
1775 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1777 CPUState *env = mon_get_cpu();
1780 return cpu_ppc_load_tbl(env);
1784 #if defined(TARGET_SPARC)
1785 #ifndef TARGET_SPARC64
1786 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1788 CPUState *env = mon_get_cpu();
1791 return GET_PSR(env);
1795 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1797 CPUState *env = mon_get_cpu();
1800 return env->regwptr[val];
1804 static const MonitorDef monitor_defs[] = {
1807 #define SEG(name, seg) \
1808 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1809 { name ".base", offsetof(CPUState, segs[seg].base) },\
1810 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1812 { "eax", offsetof(CPUState, regs[0]) },
1813 { "ecx", offsetof(CPUState, regs[1]) },
1814 { "edx", offsetof(CPUState, regs[2]) },
1815 { "ebx", offsetof(CPUState, regs[3]) },
1816 { "esp|sp", offsetof(CPUState, regs[4]) },
1817 { "ebp|fp", offsetof(CPUState, regs[5]) },
1818 { "esi", offsetof(CPUState, regs[6]) },
1819 { "edi", offsetof(CPUState, regs[7]) },
1820 #ifdef TARGET_X86_64
1821 { "r8", offsetof(CPUState, regs[8]) },
1822 { "r9", offsetof(CPUState, regs[9]) },
1823 { "r10", offsetof(CPUState, regs[10]) },
1824 { "r11", offsetof(CPUState, regs[11]) },
1825 { "r12", offsetof(CPUState, regs[12]) },
1826 { "r13", offsetof(CPUState, regs[13]) },
1827 { "r14", offsetof(CPUState, regs[14]) },
1828 { "r15", offsetof(CPUState, regs[15]) },
1830 { "eflags", offsetof(CPUState, eflags) },
1831 { "eip", offsetof(CPUState, eip) },
1838 { "pc", 0, monitor_get_pc, },
1839 #elif defined(TARGET_PPC)
1840 /* General purpose registers */
1841 { "r0", offsetof(CPUState, gpr[0]) },
1842 { "r1", offsetof(CPUState, gpr[1]) },
1843 { "r2", offsetof(CPUState, gpr[2]) },
1844 { "r3", offsetof(CPUState, gpr[3]) },
1845 { "r4", offsetof(CPUState, gpr[4]) },
1846 { "r5", offsetof(CPUState, gpr[5]) },
1847 { "r6", offsetof(CPUState, gpr[6]) },
1848 { "r7", offsetof(CPUState, gpr[7]) },
1849 { "r8", offsetof(CPUState, gpr[8]) },
1850 { "r9", offsetof(CPUState, gpr[9]) },
1851 { "r10", offsetof(CPUState, gpr[10]) },
1852 { "r11", offsetof(CPUState, gpr[11]) },
1853 { "r12", offsetof(CPUState, gpr[12]) },
1854 { "r13", offsetof(CPUState, gpr[13]) },
1855 { "r14", offsetof(CPUState, gpr[14]) },
1856 { "r15", offsetof(CPUState, gpr[15]) },
1857 { "r16", offsetof(CPUState, gpr[16]) },
1858 { "r17", offsetof(CPUState, gpr[17]) },
1859 { "r18", offsetof(CPUState, gpr[18]) },
1860 { "r19", offsetof(CPUState, gpr[19]) },
1861 { "r20", offsetof(CPUState, gpr[20]) },
1862 { "r21", offsetof(CPUState, gpr[21]) },
1863 { "r22", offsetof(CPUState, gpr[22]) },
1864 { "r23", offsetof(CPUState, gpr[23]) },
1865 { "r24", offsetof(CPUState, gpr[24]) },
1866 { "r25", offsetof(CPUState, gpr[25]) },
1867 { "r26", offsetof(CPUState, gpr[26]) },
1868 { "r27", offsetof(CPUState, gpr[27]) },
1869 { "r28", offsetof(CPUState, gpr[28]) },
1870 { "r29", offsetof(CPUState, gpr[29]) },
1871 { "r30", offsetof(CPUState, gpr[30]) },
1872 { "r31", offsetof(CPUState, gpr[31]) },
1873 /* Floating point registers */
1874 { "f0", offsetof(CPUState, fpr[0]) },
1875 { "f1", offsetof(CPUState, fpr[1]) },
1876 { "f2", offsetof(CPUState, fpr[2]) },
1877 { "f3", offsetof(CPUState, fpr[3]) },
1878 { "f4", offsetof(CPUState, fpr[4]) },
1879 { "f5", offsetof(CPUState, fpr[5]) },
1880 { "f6", offsetof(CPUState, fpr[6]) },
1881 { "f7", offsetof(CPUState, fpr[7]) },
1882 { "f8", offsetof(CPUState, fpr[8]) },
1883 { "f9", offsetof(CPUState, fpr[9]) },
1884 { "f10", offsetof(CPUState, fpr[10]) },
1885 { "f11", offsetof(CPUState, fpr[11]) },
1886 { "f12", offsetof(CPUState, fpr[12]) },
1887 { "f13", offsetof(CPUState, fpr[13]) },
1888 { "f14", offsetof(CPUState, fpr[14]) },
1889 { "f15", offsetof(CPUState, fpr[15]) },
1890 { "f16", offsetof(CPUState, fpr[16]) },
1891 { "f17", offsetof(CPUState, fpr[17]) },
1892 { "f18", offsetof(CPUState, fpr[18]) },
1893 { "f19", offsetof(CPUState, fpr[19]) },
1894 { "f20", offsetof(CPUState, fpr[20]) },
1895 { "f21", offsetof(CPUState, fpr[21]) },
1896 { "f22", offsetof(CPUState, fpr[22]) },
1897 { "f23", offsetof(CPUState, fpr[23]) },
1898 { "f24", offsetof(CPUState, fpr[24]) },
1899 { "f25", offsetof(CPUState, fpr[25]) },
1900 { "f26", offsetof(CPUState, fpr[26]) },
1901 { "f27", offsetof(CPUState, fpr[27]) },
1902 { "f28", offsetof(CPUState, fpr[28]) },
1903 { "f29", offsetof(CPUState, fpr[29]) },
1904 { "f30", offsetof(CPUState, fpr[30]) },
1905 { "f31", offsetof(CPUState, fpr[31]) },
1906 { "fpscr", offsetof(CPUState, fpscr) },
1907 /* Next instruction pointer */
1908 { "nip|pc", offsetof(CPUState, nip) },
1909 { "lr", offsetof(CPUState, lr) },
1910 { "ctr", offsetof(CPUState, ctr) },
1911 { "decr", 0, &monitor_get_decr, },
1912 { "ccr", 0, &monitor_get_ccr, },
1913 /* Machine state register */
1914 { "msr", 0, &monitor_get_msr, },
1915 { "xer", 0, &monitor_get_xer, },
1916 { "tbu", 0, &monitor_get_tbu, },
1917 { "tbl", 0, &monitor_get_tbl, },
1918 #if defined(TARGET_PPC64)
1919 /* Address space register */
1920 { "asr", offsetof(CPUState, asr) },
1922 /* Segment registers */
1923 { "sdr1", offsetof(CPUState, sdr1) },
1924 { "sr0", offsetof(CPUState, sr[0]) },
1925 { "sr1", offsetof(CPUState, sr[1]) },
1926 { "sr2", offsetof(CPUState, sr[2]) },
1927 { "sr3", offsetof(CPUState, sr[3]) },
1928 { "sr4", offsetof(CPUState, sr[4]) },
1929 { "sr5", offsetof(CPUState, sr[5]) },
1930 { "sr6", offsetof(CPUState, sr[6]) },
1931 { "sr7", offsetof(CPUState, sr[7]) },
1932 { "sr8", offsetof(CPUState, sr[8]) },
1933 { "sr9", offsetof(CPUState, sr[9]) },
1934 { "sr10", offsetof(CPUState, sr[10]) },
1935 { "sr11", offsetof(CPUState, sr[11]) },
1936 { "sr12", offsetof(CPUState, sr[12]) },
1937 { "sr13", offsetof(CPUState, sr[13]) },
1938 { "sr14", offsetof(CPUState, sr[14]) },
1939 { "sr15", offsetof(CPUState, sr[15]) },
1940 /* Too lazy to put BATs and SPRs ... */
1941 #elif defined(TARGET_SPARC)
1942 { "g0", offsetof(CPUState, gregs[0]) },
1943 { "g1", offsetof(CPUState, gregs[1]) },
1944 { "g2", offsetof(CPUState, gregs[2]) },
1945 { "g3", offsetof(CPUState, gregs[3]) },
1946 { "g4", offsetof(CPUState, gregs[4]) },
1947 { "g5", offsetof(CPUState, gregs[5]) },
1948 { "g6", offsetof(CPUState, gregs[6]) },
1949 { "g7", offsetof(CPUState, gregs[7]) },
1950 { "o0", 0, monitor_get_reg },
1951 { "o1", 1, monitor_get_reg },
1952 { "o2", 2, monitor_get_reg },
1953 { "o3", 3, monitor_get_reg },
1954 { "o4", 4, monitor_get_reg },
1955 { "o5", 5, monitor_get_reg },
1956 { "o6", 6, monitor_get_reg },
1957 { "o7", 7, monitor_get_reg },
1958 { "l0", 8, monitor_get_reg },
1959 { "l1", 9, monitor_get_reg },
1960 { "l2", 10, monitor_get_reg },
1961 { "l3", 11, monitor_get_reg },
1962 { "l4", 12, monitor_get_reg },
1963 { "l5", 13, monitor_get_reg },
1964 { "l6", 14, monitor_get_reg },
1965 { "l7", 15, monitor_get_reg },
1966 { "i0", 16, monitor_get_reg },
1967 { "i1", 17, monitor_get_reg },
1968 { "i2", 18, monitor_get_reg },
1969 { "i3", 19, monitor_get_reg },
1970 { "i4", 20, monitor_get_reg },
1971 { "i5", 21, monitor_get_reg },
1972 { "i6", 22, monitor_get_reg },
1973 { "i7", 23, monitor_get_reg },
1974 { "pc", offsetof(CPUState, pc) },
1975 { "npc", offsetof(CPUState, npc) },
1976 { "y", offsetof(CPUState, y) },
1977 #ifndef TARGET_SPARC64
1978 { "psr", 0, &monitor_get_psr, },
1979 { "wim", offsetof(CPUState, wim) },
1981 { "tbr", offsetof(CPUState, tbr) },
1982 { "fsr", offsetof(CPUState, fsr) },
1983 { "f0", offsetof(CPUState, fpr[0]) },
1984 { "f1", offsetof(CPUState, fpr[1]) },
1985 { "f2", offsetof(CPUState, fpr[2]) },
1986 { "f3", offsetof(CPUState, fpr[3]) },
1987 { "f4", offsetof(CPUState, fpr[4]) },
1988 { "f5", offsetof(CPUState, fpr[5]) },
1989 { "f6", offsetof(CPUState, fpr[6]) },
1990 { "f7", offsetof(CPUState, fpr[7]) },
1991 { "f8", offsetof(CPUState, fpr[8]) },
1992 { "f9", offsetof(CPUState, fpr[9]) },
1993 { "f10", offsetof(CPUState, fpr[10]) },
1994 { "f11", offsetof(CPUState, fpr[11]) },
1995 { "f12", offsetof(CPUState, fpr[12]) },
1996 { "f13", offsetof(CPUState, fpr[13]) },
1997 { "f14", offsetof(CPUState, fpr[14]) },
1998 { "f15", offsetof(CPUState, fpr[15]) },
1999 { "f16", offsetof(CPUState, fpr[16]) },
2000 { "f17", offsetof(CPUState, fpr[17]) },
2001 { "f18", offsetof(CPUState, fpr[18]) },
2002 { "f19", offsetof(CPUState, fpr[19]) },
2003 { "f20", offsetof(CPUState, fpr[20]) },
2004 { "f21", offsetof(CPUState, fpr[21]) },
2005 { "f22", offsetof(CPUState, fpr[22]) },
2006 { "f23", offsetof(CPUState, fpr[23]) },
2007 { "f24", offsetof(CPUState, fpr[24]) },
2008 { "f25", offsetof(CPUState, fpr[25]) },
2009 { "f26", offsetof(CPUState, fpr[26]) },
2010 { "f27", offsetof(CPUState, fpr[27]) },
2011 { "f28", offsetof(CPUState, fpr[28]) },
2012 { "f29", offsetof(CPUState, fpr[29]) },
2013 { "f30", offsetof(CPUState, fpr[30]) },
2014 { "f31", offsetof(CPUState, fpr[31]) },
2015 #ifdef TARGET_SPARC64
2016 { "f32", offsetof(CPUState, fpr[32]) },
2017 { "f34", offsetof(CPUState, fpr[34]) },
2018 { "f36", offsetof(CPUState, fpr[36]) },
2019 { "f38", offsetof(CPUState, fpr[38]) },
2020 { "f40", offsetof(CPUState, fpr[40]) },
2021 { "f42", offsetof(CPUState, fpr[42]) },
2022 { "f44", offsetof(CPUState, fpr[44]) },
2023 { "f46", offsetof(CPUState, fpr[46]) },
2024 { "f48", offsetof(CPUState, fpr[48]) },
2025 { "f50", offsetof(CPUState, fpr[50]) },
2026 { "f52", offsetof(CPUState, fpr[52]) },
2027 { "f54", offsetof(CPUState, fpr[54]) },
2028 { "f56", offsetof(CPUState, fpr[56]) },
2029 { "f58", offsetof(CPUState, fpr[58]) },
2030 { "f60", offsetof(CPUState, fpr[60]) },
2031 { "f62", offsetof(CPUState, fpr[62]) },
2032 { "asi", offsetof(CPUState, asi) },
2033 { "pstate", offsetof(CPUState, pstate) },
2034 { "cansave", offsetof(CPUState, cansave) },
2035 { "canrestore", offsetof(CPUState, canrestore) },
2036 { "otherwin", offsetof(CPUState, otherwin) },
2037 { "wstate", offsetof(CPUState, wstate) },
2038 { "cleanwin", offsetof(CPUState, cleanwin) },
2039 { "fprs", offsetof(CPUState, fprs) },
2045 static void expr_error(Monitor *mon, const char *msg)
2047 monitor_printf(mon, "%s\n", msg);
2048 longjmp(expr_env, 1);
2051 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2052 static int get_monitor_def(target_long *pval, const char *name)
2054 const MonitorDef *md;
2057 for(md = monitor_defs; md->name != NULL; md++) {
2058 if (compare_cmd(name, md->name)) {
2059 if (md->get_value) {
2060 *pval = md->get_value(md, md->offset);
2062 CPUState *env = mon_get_cpu();
2065 ptr = (uint8_t *)env + md->offset;
2068 *pval = *(int32_t *)ptr;
2071 *pval = *(target_long *)ptr;
2084 static void next(void)
2088 while (qemu_isspace(*pch))
2093 static int64_t expr_sum(Monitor *mon);
2095 static int64_t expr_unary(Monitor *mon)
2104 n = expr_unary(mon);
2108 n = -expr_unary(mon);
2112 n = ~expr_unary(mon);
2118 expr_error(mon, "')' expected");
2125 expr_error(mon, "character constant expected");
2129 expr_error(mon, "missing terminating \' character");
2139 while ((*pch >= 'a' && *pch <= 'z') ||
2140 (*pch >= 'A' && *pch <= 'Z') ||
2141 (*pch >= '0' && *pch <= '9') ||
2142 *pch == '_' || *pch == '.') {
2143 if ((q - buf) < sizeof(buf) - 1)
2147 while (qemu_isspace(*pch))
2150 ret = get_monitor_def(®, buf);
2152 expr_error(mon, "unknown register");
2154 expr_error(mon, "no cpu defined");
2159 expr_error(mon, "unexpected end of expression");
2163 #if TARGET_PHYS_ADDR_BITS > 32
2164 n = strtoull(pch, &p, 0);
2166 n = strtoul(pch, &p, 0);
2169 expr_error(mon, "invalid char in expression");
2172 while (qemu_isspace(*pch))
2180 static int64_t expr_prod(Monitor *mon)
2185 val = expr_unary(mon);
2188 if (op != '*' && op != '/' && op != '%')
2191 val2 = expr_unary(mon);
2200 expr_error(mon, "division by zero");
2211 static int64_t expr_logic(Monitor *mon)
2216 val = expr_prod(mon);
2219 if (op != '&' && op != '|' && op != '^')
2222 val2 = expr_prod(mon);
2239 static int64_t expr_sum(Monitor *mon)
2244 val = expr_logic(mon);
2247 if (op != '+' && op != '-')
2250 val2 = expr_logic(mon);
2259 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2262 if (setjmp(expr_env)) {
2266 while (qemu_isspace(*pch))
2268 *pval = expr_sum(mon);
2273 static int get_str(char *buf, int buf_size, const char **pp)
2281 while (qemu_isspace(*p))
2291 while (*p != '\0' && *p != '\"') {
2307 qemu_printf("unsupported escape code: '\\%c'\n", c);
2310 if ((q - buf) < buf_size - 1) {
2314 if ((q - buf) < buf_size - 1) {
2321 qemu_printf("unterminated string\n");
2326 while (*p != '\0' && !qemu_isspace(*p)) {
2327 if ((q - buf) < buf_size - 1) {
2338 static int default_fmt_format = 'x';
2339 static int default_fmt_size = 4;
2343 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2345 const char *p, *pstart, *typestr;
2347 int c, nb_args, len, i, has_arg;
2348 const mon_cmd_t *cmd;
2351 void *str_allocated[MAX_ARGS];
2352 void *args[MAX_ARGS];
2353 void (*handler_0)(Monitor *mon);
2354 void (*handler_1)(Monitor *mon, void *arg0);
2355 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2356 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2357 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2359 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2360 void *arg3, void *arg4);
2361 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2362 void *arg3, void *arg4, void *arg5);
2363 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2364 void *arg3, void *arg4, void *arg5, void *arg6);
2367 monitor_printf(mon, "command='%s'\n", cmdline);
2370 /* extract the command name */
2373 while (qemu_isspace(*p))
2378 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2381 if (len > sizeof(cmdname) - 1)
2382 len = sizeof(cmdname) - 1;
2383 memcpy(cmdname, pstart, len);
2384 cmdname[len] = '\0';
2386 /* find the command */
2387 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2388 if (compare_cmd(cmdname, cmd->name))
2391 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2395 for(i = 0; i < MAX_ARGS; i++)
2396 str_allocated[i] = NULL;
2398 /* parse the parameters */
2399 typestr = cmd->args_type;
2414 while (qemu_isspace(*p))
2416 if (*typestr == '?') {
2419 /* no optional string: NULL argument */
2424 ret = get_str(buf, sizeof(buf), &p);
2428 monitor_printf(mon, "%s: filename expected\n",
2432 monitor_printf(mon, "%s: block device name expected\n",
2436 monitor_printf(mon, "%s: string expected\n", cmdname);
2441 str = qemu_malloc(strlen(buf) + 1);
2442 pstrcpy(str, sizeof(buf), buf);
2443 str_allocated[nb_args] = str;
2445 if (nb_args >= MAX_ARGS) {
2447 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2450 args[nb_args++] = str;
2455 int count, format, size;
2457 while (qemu_isspace(*p))
2463 if (qemu_isdigit(*p)) {
2465 while (qemu_isdigit(*p)) {
2466 count = count * 10 + (*p - '0');
2504 if (*p != '\0' && !qemu_isspace(*p)) {
2505 monitor_printf(mon, "invalid char in format: '%c'\n",
2510 format = default_fmt_format;
2511 if (format != 'i') {
2512 /* for 'i', not specifying a size gives -1 as size */
2514 size = default_fmt_size;
2515 default_fmt_size = size;
2517 default_fmt_format = format;
2520 format = default_fmt_format;
2521 if (format != 'i') {
2522 size = default_fmt_size;
2527 if (nb_args + 3 > MAX_ARGS)
2529 args[nb_args++] = (void*)(long)count;
2530 args[nb_args++] = (void*)(long)format;
2531 args[nb_args++] = (void*)(long)size;
2539 while (qemu_isspace(*p))
2541 if (*typestr == '?' || *typestr == '.') {
2542 if (*typestr == '?') {
2550 while (qemu_isspace(*p))
2558 if (nb_args >= MAX_ARGS)
2560 args[nb_args++] = (void *)(long)has_arg;
2562 if (nb_args >= MAX_ARGS)
2568 if (get_expr(mon, &val, &p))
2572 if (nb_args >= MAX_ARGS)
2574 args[nb_args++] = (void *)(long)val;
2576 if ((nb_args + 1) >= MAX_ARGS)
2578 #if TARGET_PHYS_ADDR_BITS > 32
2579 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2581 args[nb_args++] = (void *)0;
2583 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2595 while (qemu_isspace(*p))
2601 monitor_printf(mon, "%s: unsupported option -%c\n",
2608 if (nb_args >= MAX_ARGS)
2610 args[nb_args++] = (void *)(long)has_option;
2615 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2619 /* check that all arguments were parsed */
2620 while (qemu_isspace(*p))
2623 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2630 handler_0 = cmd->handler;
2634 handler_1 = cmd->handler;
2635 handler_1(mon, args[0]);
2638 handler_2 = cmd->handler;
2639 handler_2(mon, args[0], args[1]);
2642 handler_3 = cmd->handler;
2643 handler_3(mon, args[0], args[1], args[2]);
2646 handler_4 = cmd->handler;
2647 handler_4(mon, args[0], args[1], args[2], args[3]);
2650 handler_5 = cmd->handler;
2651 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2654 handler_6 = cmd->handler;
2655 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2658 handler_7 = cmd->handler;
2659 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2663 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2667 for(i = 0; i < MAX_ARGS; i++)
2668 qemu_free(str_allocated[i]);
2672 static void cmd_completion(const char *name, const char *list)
2674 const char *p, *pstart;
2683 p = pstart + strlen(pstart);
2685 if (len > sizeof(cmd) - 2)
2686 len = sizeof(cmd) - 2;
2687 memcpy(cmd, pstart, len);
2689 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2690 readline_add_completion(cmd);
2698 static void file_completion(const char *input)
2703 char file[1024], file_prefix[1024];
2707 p = strrchr(input, '/');
2710 pstrcpy(file_prefix, sizeof(file_prefix), input);
2711 pstrcpy(path, sizeof(path), ".");
2713 input_path_len = p - input + 1;
2714 memcpy(path, input, input_path_len);
2715 if (input_path_len > sizeof(path) - 1)
2716 input_path_len = sizeof(path) - 1;
2717 path[input_path_len] = '\0';
2718 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2720 #ifdef DEBUG_COMPLETION
2721 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2722 input, path, file_prefix);
2724 ffs = opendir(path);
2732 if (strstart(d->d_name, file_prefix, NULL)) {
2733 memcpy(file, input, input_path_len);
2734 if (input_path_len < sizeof(file))
2735 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2737 /* stat the file to find out if it's a directory.
2738 * In that case add a slash to speed up typing long paths
2741 if(S_ISDIR(sb.st_mode))
2742 pstrcat(file, sizeof(file), "/");
2743 readline_add_completion(file);
2749 static void block_completion_it(void *opaque, BlockDriverState *bs)
2751 const char *name = bdrv_get_device_name(bs);
2752 const char *input = opaque;
2754 if (input[0] == '\0' ||
2755 !strncmp(name, (char *)input, strlen(input))) {
2756 readline_add_completion(name);
2760 /* NOTE: this parser is an approximate form of the real command parser */
2761 static void parse_cmdline(const char *cmdline,
2762 int *pnb_args, char **args)
2771 while (qemu_isspace(*p))
2775 if (nb_args >= MAX_ARGS)
2777 ret = get_str(buf, sizeof(buf), &p);
2778 args[nb_args] = qemu_strdup(buf);
2783 *pnb_args = nb_args;
2786 void readline_find_completion(const char *cmdline)
2788 const char *cmdname;
2789 char *args[MAX_ARGS];
2790 int nb_args, i, len;
2791 const char *ptype, *str;
2792 const mon_cmd_t *cmd;
2795 parse_cmdline(cmdline, &nb_args, args);
2796 #ifdef DEBUG_COMPLETION
2797 for(i = 0; i < nb_args; i++) {
2798 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2802 /* if the line ends with a space, it means we want to complete the
2804 len = strlen(cmdline);
2805 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2806 if (nb_args >= MAX_ARGS)
2808 args[nb_args++] = qemu_strdup("");
2811 /* command completion */
2816 readline_set_completion_index(strlen(cmdname));
2817 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2818 cmd_completion(cmdname, cmd->name);
2821 /* find the command */
2822 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2823 if (compare_cmd(args[0], cmd->name))
2828 ptype = cmd->args_type;
2829 for(i = 0; i < nb_args - 2; i++) {
2830 if (*ptype != '\0') {
2832 while (*ptype == '?')
2836 str = args[nb_args - 1];
2839 /* file completion */
2840 readline_set_completion_index(strlen(str));
2841 file_completion(str);
2844 /* block device name completion */
2845 readline_set_completion_index(strlen(str));
2846 bdrv_iterate(block_completion_it, (void *)str);
2849 /* XXX: more generic ? */
2850 if (!strcmp(cmd->name, "info")) {
2851 readline_set_completion_index(strlen(str));
2852 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2853 cmd_completion(str, cmd->name);
2855 } else if (!strcmp(cmd->name, "sendkey")) {
2856 readline_set_completion_index(strlen(str));
2857 for(key = key_defs; key->name != NULL; key++) {
2858 cmd_completion(str, key->name);
2866 for(i = 0; i < nb_args; i++)
2870 static int term_can_read(void *opaque)
2875 static void term_read(void *opaque, const uint8_t *buf, int size)
2879 for (i = 0; i < size; i++)
2880 readline_handle_byte(buf[i]);
2883 static int monitor_suspended;
2885 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
2887 monitor_handle_command(mon, cmdline);
2888 if (!monitor_suspended)
2889 readline_show_prompt();
2891 monitor_suspended = 2;
2894 void monitor_suspend(Monitor *mon)
2896 monitor_suspended = 1;
2899 void monitor_resume(Monitor *mon)
2901 if (monitor_suspended == 2)
2902 monitor_start_input();
2903 monitor_suspended = 0;
2906 static void monitor_start_input(void)
2908 readline_start("(qemu) ", 0, monitor_command_cb, NULL);
2909 readline_show_prompt();
2912 static void term_event(void *opaque, int event)
2914 Monitor *mon = opaque;
2916 if (event != CHR_EVENT_RESET)
2919 monitor_printf(mon, "QEMU %s monitor - type 'help' for more information\n",
2921 monitor_start_input();
2924 static int is_first_init = 1;
2926 void monitor_init(CharDriverState *chr)
2930 if (is_first_init) {
2931 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2935 mon = qemu_mallocz(sizeof(*mon));
2939 qemu_chr_add_handlers(chr, term_can_read, term_read, term_event, mon);
2941 LIST_INSERT_HEAD(&mon_list, mon, entry);
2945 readline_start("", 0, monitor_command_cb, NULL);
2948 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
2950 BlockDriverState *bs = opaque;
2953 if (bdrv_set_key(bs, password) != 0) {
2954 monitor_printf(mon, "invalid password\n");
2957 if (password_completion_cb)
2958 password_completion_cb(password_opaque, ret);
2960 monitor_start_input();
2963 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
2964 BlockDriverCompletionFunc *completion_cb,
2967 if (!bdrv_key_required(bs)) {
2969 completion_cb(opaque, 0);
2973 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2974 bdrv_get_encrypted_filename(bs));
2976 password_completion_cb = completion_cb;
2977 password_opaque = opaque;
2979 monitor_read_password(mon, bdrv_password_cb, bs);