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
29 //#define DEBUG_COMPLETION
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
39 * 'B' block device name
40 * 's' string (accept optional quote)
42 * 'l' target long (32 or 64 bit)
43 * '/' optional gdb-like print format (like "/10x")
45 * '?' optional type (for 'F', 's' and 'i')
49 typedef struct term_cmd_t {
51 const char *args_type;
57 static CharDriverState *monitor_hd;
59 static term_cmd_t term_cmds[];
60 static term_cmd_t info_cmds[];
62 static char term_outbuf[1024];
63 static int term_outbuf_index;
65 static void monitor_start_input(void);
67 CPUState *mon_cpu = NULL;
71 if (term_outbuf_index > 0) {
72 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
73 term_outbuf_index = 0;
77 /* flush at every end of line or if the buffer is full */
78 void term_puts(const char *str)
85 term_outbuf[term_outbuf_index++] = c;
86 if (term_outbuf_index >= sizeof(term_outbuf) ||
92 void term_vprintf(const char *fmt, va_list ap)
95 vsnprintf(buf, sizeof(buf), fmt, ap);
99 void term_printf(const char *fmt, ...)
103 term_vprintf(fmt, ap);
107 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
111 term_vprintf(fmt, ap);
116 static int compare_cmd(const char *name, const char *list)
118 const char *p, *pstart;
126 p = pstart + strlen(pstart);
127 if ((p - pstart) == len && !memcmp(pstart, name, len))
136 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
140 for(cmd = cmds; cmd->name != NULL; cmd++) {
141 if (!name || !strcmp(name, cmd->name))
142 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
146 static void help_cmd(const char *name)
148 if (name && !strcmp(name, "info")) {
149 help_cmd1(info_cmds, "info ", NULL);
151 help_cmd1(term_cmds, "", name);
152 if (name && !strcmp(name, "log")) {
154 term_printf("Log items (comma separated):\n");
155 term_printf("%-10s %s\n", "none", "remove all logs");
156 for(item = cpu_log_items; item->mask != 0; item++) {
157 term_printf("%-10s %s\n", item->name, item->help);
163 static void do_help(const char *name)
168 static void do_commit(void)
172 for (i = 0; i < MAX_DISKS; i++) {
174 bdrv_commit(bs_table[i]);
179 static void do_info(const char *item)
185 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
186 if (compare_cmd(item, cmd->name))
196 static void do_info_version(void)
198 term_printf("%s\n", QEMU_VERSION);
201 static void do_info_block(void)
206 /* get the current CPU defined by the user */
207 int mon_set_cpu(int cpu_index)
211 for(env = first_cpu; env != NULL; env = env->next_cpu) {
212 if (env->cpu_index == cpu_index) {
220 CPUState *mon_get_cpu(void)
228 static void do_info_registers(void)
235 cpu_dump_state(env, NULL, monitor_fprintf,
238 cpu_dump_state(env, NULL, monitor_fprintf,
243 static void do_info_cpus(void)
247 /* just to set the default cpu if not already done */
250 for(env = first_cpu; env != NULL; env = env->next_cpu) {
251 term_printf("%c CPU #%d:",
252 (env == mon_cpu) ? '*' : ' ',
254 #if defined(TARGET_I386)
255 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
257 term_printf(" (halted)");
263 static void do_cpu_set(int index)
265 if (mon_set_cpu(index) < 0)
266 term_printf("Invalid CPU index\n");
269 static void do_info_jit(void)
271 dump_exec_info(NULL, monitor_fprintf);
274 static void do_info_history (void)
281 str = readline_get_history(i);
284 term_printf("%d: '%s'\n", i, str);
289 static void do_quit(void)
297 static int eject_device(BlockDriverState *bs, int force)
299 if (bdrv_is_inserted(bs)) {
301 if (!bdrv_is_removable(bs)) {
302 term_printf("device is not removable\n");
305 if (bdrv_is_locked(bs)) {
306 term_printf("device is locked\n");
315 static void do_eject(int force, const char *filename)
317 BlockDriverState *bs;
319 bs = bdrv_find(filename);
321 term_printf("device not found\n");
324 eject_device(bs, force);
327 static void do_change(const char *device, const char *filename)
329 BlockDriverState *bs;
333 bs = bdrv_find(device);
335 term_printf("device not found\n");
338 if (eject_device(bs, 0) < 0)
340 bdrv_open(bs, filename, 0);
341 if (bdrv_is_encrypted(bs)) {
342 term_printf("%s is encrypted.\n", device);
343 for(i = 0; i < 3; i++) {
344 monitor_readline("Password: ", 1, password, sizeof(password));
345 if (bdrv_set_key(bs, password) == 0)
347 term_printf("invalid password\n");
352 static void do_screen_dump(const char *filename)
354 vga_screen_dump(filename);
357 static void do_log(const char *items)
361 if (!strcmp(items, "none")) {
364 mask = cpu_str_to_log_mask(items);
373 static void do_savevm(const char *filename)
375 if (qemu_savevm(filename) < 0)
376 term_printf("I/O error when saving VM to '%s'\n", filename);
379 static void do_loadvm(const char *filename)
381 if (qemu_loadvm(filename) < 0)
382 term_printf("I/O error when loading VM from '%s'\n", filename);
385 static void do_stop(void)
387 vm_stop(EXCP_INTERRUPT);
390 static void do_cont(void)
395 #ifdef CONFIG_GDBSTUB
396 static void do_gdbserver(int has_port, int port)
399 port = DEFAULT_GDBSTUB_PORT;
400 if (gdbserver_start(port) < 0) {
401 qemu_printf("Could not open gdbserver socket on port %d\n", port);
403 qemu_printf("Waiting gdb connection on port %d\n", port);
408 static void term_printc(int c)
425 if (c >= 32 && c <= 126) {
426 term_printf("%c", c);
428 term_printf("\\x%02x", c);
435 static void memory_dump(int count, int format, int wsize,
436 target_ulong addr, int is_physical)
439 int nb_per_line, l, line_size, i, max_digits, len;
447 if (!env && !is_physical)
452 } else if (wsize == 4) {
455 /* as default we use the current CS size */
457 if (env && !(env->segs[R_CS].flags & DESC_B_MASK))
461 monitor_disas(env, addr, count, is_physical, flags);
470 nb_per_line = line_size / wsize;
475 max_digits = (wsize * 8 + 2) / 3;
479 max_digits = (wsize * 8) / 4;
483 max_digits = (wsize * 8 * 10 + 32) / 33;
491 term_printf(TARGET_FMT_lx ":", addr);
496 cpu_physical_memory_rw(addr, buf, l, 0);
501 cpu_memory_rw_debug(env, addr, buf, l, 0);
508 v = ldub_raw(buf + i);
511 v = lduw_raw(buf + i);
514 v = (uint32_t)ldl_raw(buf + i);
517 v = ldq_raw(buf + i);
523 term_printf("%#*llo", max_digits, v);
526 term_printf("0x%0*llx", max_digits, v);
529 term_printf("%*llu", max_digits, v);
532 term_printf("%*lld", max_digits, v);
546 #if TARGET_LONG_BITS == 64
547 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
549 #define GET_TLONG(h, l) (l)
552 static void do_memory_dump(int count, int format, int size,
553 uint32_t addrh, uint32_t addrl)
555 target_long addr = GET_TLONG(addrh, addrl);
556 memory_dump(count, format, size, addr, 0);
559 static void do_physical_memory_dump(int count, int format, int size,
560 uint32_t addrh, uint32_t addrl)
563 target_long addr = GET_TLONG(addrh, addrl);
564 memory_dump(count, format, size, addr, 1);
567 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
569 target_long val = GET_TLONG(valh, vall);
570 #if TARGET_LONG_BITS == 32
573 term_printf("%#o", val);
576 term_printf("%#x", val);
579 term_printf("%u", val);
583 term_printf("%d", val);
592 term_printf("%#llo", val);
595 term_printf("%#llx", val);
598 term_printf("%llu", val);
602 term_printf("%lld", val);
612 static void do_sum(uint32_t start, uint32_t size)
619 for(addr = start; addr < (start + size); addr++) {
620 cpu_physical_memory_rw(addr, buf, 1, 0);
621 /* BSD sum algorithm ('sum' Unix command) */
622 sum = (sum >> 1) | (sum << 15);
625 term_printf("%05d\n", sum);
633 static const KeyDef key_defs[] = {
656 { 0x0e, "backspace" },
691 { 0x3a, "caps_lock" },
702 { 0x45, "num_lock" },
703 { 0x46, "scroll_lock" },
727 static int get_keycode(const char *key)
731 for(p = key_defs; p->name != NULL; p++) {
732 if (!strcmp(key, p->name))
738 static void do_send_key(const char *string)
741 uint8_t keycodes[16];
743 int nb_keycodes, keycode, i;
749 while (*p != '\0' && *p != '-') {
750 if ((q - keybuf) < sizeof(keybuf) - 1) {
756 keycode = get_keycode(keybuf);
758 term_printf("unknown key: '%s'\n", keybuf);
761 keycodes[nb_keycodes++] = keycode;
766 /* key down events */
767 for(i = 0; i < nb_keycodes; i++) {
768 keycode = keycodes[i];
770 kbd_put_keycode(0xe0);
771 kbd_put_keycode(keycode & 0x7f);
774 for(i = nb_keycodes - 1; i >= 0; i--) {
775 keycode = keycodes[i];
777 kbd_put_keycode(0xe0);
778 kbd_put_keycode(keycode | 0x80);
782 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
788 cpu_outb(NULL, addr & 0xffff, index & 0xff);
796 val = cpu_inb(NULL, addr);
800 val = cpu_inw(NULL, addr);
804 val = cpu_inl(NULL, addr);
808 term_printf("port%c[0x%04x] = %#0*x\n",
809 suffix, addr, size * 2, val);
812 static void do_system_reset(void)
814 qemu_system_reset_request();
817 static void do_system_powerdown(void)
819 qemu_system_powerdown_request();
822 #if defined(TARGET_I386)
823 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
825 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
828 pte & PG_GLOBAL_MASK ? 'G' : '-',
829 pte & PG_PSE_MASK ? 'P' : '-',
830 pte & PG_DIRTY_MASK ? 'D' : '-',
831 pte & PG_ACCESSED_MASK ? 'A' : '-',
832 pte & PG_PCD_MASK ? 'C' : '-',
833 pte & PG_PWT_MASK ? 'T' : '-',
834 pte & PG_USER_MASK ? 'U' : '-',
835 pte & PG_RW_MASK ? 'W' : '-');
838 static void tlb_info(void)
842 uint32_t pgd, pde, pte;
848 if (!(env->cr[0] & CR0_PG_MASK)) {
849 term_printf("PG disabled\n");
852 pgd = env->cr[3] & ~0xfff;
853 for(l1 = 0; l1 < 1024; l1++) {
854 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
855 pde = le32_to_cpu(pde);
856 if (pde & PG_PRESENT_MASK) {
857 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
858 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
860 for(l2 = 0; l2 < 1024; l2++) {
861 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
863 pte = le32_to_cpu(pte);
864 if (pte & PG_PRESENT_MASK) {
865 print_pte((l1 << 22) + (l2 << 12),
875 static void mem_print(uint32_t *pstart, int *plast_prot,
876 uint32_t end, int prot)
882 term_printf("%08x-%08x %08x %c%c%c\n",
883 *pstart, end, end - *pstart,
884 prot1 & PG_USER_MASK ? 'u' : '-',
886 prot1 & PG_RW_MASK ? 'w' : '-');
896 static void mem_info(void)
899 int l1, l2, prot, last_prot;
900 uint32_t pgd, pde, pte, start, end;
906 if (!(env->cr[0] & CR0_PG_MASK)) {
907 term_printf("PG disabled\n");
910 pgd = env->cr[3] & ~0xfff;
913 for(l1 = 0; l1 < 1024; l1++) {
914 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
915 pde = le32_to_cpu(pde);
917 if (pde & PG_PRESENT_MASK) {
918 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
919 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
920 mem_print(&start, &last_prot, end, prot);
922 for(l2 = 0; l2 < 1024; l2++) {
923 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
925 pte = le32_to_cpu(pte);
926 end = (l1 << 22) + (l2 << 12);
927 if (pte & PG_PRESENT_MASK) {
928 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
932 mem_print(&start, &last_prot, end, prot);
937 mem_print(&start, &last_prot, end, prot);
943 static void do_info_kqemu(void)
951 term_printf("No cpu initialized yet");
954 val = env->kqemu_enabled;
955 term_printf("kqemu is %s\n", val ? "enabled" : "disabled");
957 term_printf("kqemu support is not compiled\n");
961 static term_cmd_t term_cmds[] = {
962 { "help|?", "s?", do_help,
963 "[cmd]", "show the help" },
964 { "commit", "", do_commit,
965 "", "commit changes to the disk images (if -snapshot is used)" },
966 { "info", "s?", do_info,
967 "subcommand", "show various information about the system state" },
968 { "q|quit", "", do_quit,
969 "", "quit the emulator" },
970 { "eject", "-fB", do_eject,
971 "[-f] device", "eject a removable media (use -f to force it)" },
972 { "change", "BF", do_change,
973 "device filename", "change a removable media" },
974 { "screendump", "F", do_screen_dump,
975 "filename", "save screen into PPM image 'filename'" },
976 { "log", "s", do_log,
977 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
978 { "savevm", "F", do_savevm,
979 "filename", "save the whole virtual machine state to 'filename'" },
980 { "loadvm", "F", do_loadvm,
981 "filename", "restore the whole virtual machine state from 'filename'" },
982 { "stop", "", do_stop,
983 "", "stop emulation", },
984 { "c|cont", "", do_cont,
985 "", "resume emulation", },
986 #ifdef CONFIG_GDBSTUB
987 { "gdbserver", "i?", do_gdbserver,
988 "[port]", "start gdbserver session (default port=1234)", },
990 { "x", "/l", do_memory_dump,
991 "/fmt addr", "virtual memory dump starting at 'addr'", },
992 { "xp", "/l", do_physical_memory_dump,
993 "/fmt addr", "physical memory dump starting at 'addr'", },
994 { "p|print", "/l", do_print,
995 "/fmt expr", "print expression value (use $reg for CPU register access)", },
996 { "i", "/ii.", do_ioport_read,
997 "/fmt addr", "I/O port read" },
999 { "sendkey", "s", do_send_key,
1000 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1001 { "system_reset", "", do_system_reset,
1002 "", "reset the system" },
1003 { "system_powerdown", "", do_system_powerdown,
1004 "", "send system power down event" },
1005 { "sum", "ii", do_sum,
1006 "addr size", "compute the checksum of a memory region" },
1007 { "usb_add", "s", do_usb_add,
1008 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1009 { "usb_del", "s", do_usb_del,
1010 "device", "remove USB device 'bus.addr'" },
1011 { "cpu", "i", do_cpu_set,
1012 "index", "set the default CPU" },
1016 static term_cmd_t info_cmds[] = {
1017 { "version", "", do_info_version,
1018 "", "show the version of qemu" },
1019 { "network", "", do_info_network,
1020 "", "show the network state" },
1021 { "block", "", do_info_block,
1022 "", "show the block devices" },
1023 { "registers", "", do_info_registers,
1024 "", "show the cpu registers" },
1025 { "cpus", "", do_info_cpus,
1026 "", "show infos for each CPU" },
1027 { "history", "", do_info_history,
1028 "", "show the command line history", },
1029 { "irq", "", irq_info,
1030 "", "show the interrupts statistics (if available)", },
1031 { "pic", "", pic_info,
1032 "", "show i8259 (PIC) state", },
1033 { "pci", "", pci_info,
1034 "", "show PCI info", },
1035 #if defined(TARGET_I386)
1036 { "tlb", "", tlb_info,
1037 "", "show virtual to physical memory mappings", },
1038 { "mem", "", mem_info,
1039 "", "show the active virtual memory mappings", },
1041 { "jit", "", do_info_jit,
1042 "", "show dynamic compiler info", },
1043 { "kqemu", "", do_info_kqemu,
1044 "", "show kqemu information", },
1045 { "usb", "", usb_info,
1046 "", "show guest USB devices", },
1047 { "usbhost", "", usb_host_info,
1048 "", "show host USB devices", },
1052 /*******************************************************************/
1054 static const char *pch;
1055 static jmp_buf expr_env;
1060 typedef struct MonitorDef {
1063 target_long (*get_value)(struct MonitorDef *md, int val);
1067 #if defined(TARGET_I386)
1068 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1070 CPUState *env = mon_get_cpu();
1073 return env->eip + env->segs[R_CS].base;
1077 #if defined(TARGET_PPC)
1078 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1080 CPUState *env = mon_get_cpu();
1088 for (i = 0; i < 8; i++)
1089 u |= env->crf[i] << (32 - (4 * i));
1094 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1096 CPUState *env = mon_get_cpu();
1099 return (env->msr[MSR_POW] << MSR_POW) |
1100 (env->msr[MSR_ILE] << MSR_ILE) |
1101 (env->msr[MSR_EE] << MSR_EE) |
1102 (env->msr[MSR_PR] << MSR_PR) |
1103 (env->msr[MSR_FP] << MSR_FP) |
1104 (env->msr[MSR_ME] << MSR_ME) |
1105 (env->msr[MSR_FE0] << MSR_FE0) |
1106 (env->msr[MSR_SE] << MSR_SE) |
1107 (env->msr[MSR_BE] << MSR_BE) |
1108 (env->msr[MSR_FE1] << MSR_FE1) |
1109 (env->msr[MSR_IP] << MSR_IP) |
1110 (env->msr[MSR_IR] << MSR_IR) |
1111 (env->msr[MSR_DR] << MSR_DR) |
1112 (env->msr[MSR_RI] << MSR_RI) |
1113 (env->msr[MSR_LE] << MSR_LE);
1116 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1118 CPUState *env = mon_get_cpu();
1121 return (env->xer[XER_SO] << XER_SO) |
1122 (env->xer[XER_OV] << XER_OV) |
1123 (env->xer[XER_CA] << XER_CA) |
1124 (env->xer[XER_BC] << XER_BC);
1127 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1129 CPUState *env = mon_get_cpu();
1132 return cpu_ppc_load_decr(env);
1135 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1137 CPUState *env = mon_get_cpu();
1140 return cpu_ppc_load_tbu(env);
1143 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1145 CPUState *env = mon_get_cpu();
1148 return cpu_ppc_load_tbl(env);
1152 #if defined(TARGET_SPARC)
1153 #ifndef TARGET_SPARC64
1154 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1156 CPUState *env = mon_get_cpu();
1159 return GET_PSR(env);
1163 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1165 CPUState *env = mon_get_cpu();
1168 return env->regwptr[val];
1172 static MonitorDef monitor_defs[] = {
1175 #define SEG(name, seg) \
1176 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1177 { name ".base", offsetof(CPUState, segs[seg].base) },\
1178 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1180 { "eax", offsetof(CPUState, regs[0]) },
1181 { "ecx", offsetof(CPUState, regs[1]) },
1182 { "edx", offsetof(CPUState, regs[2]) },
1183 { "ebx", offsetof(CPUState, regs[3]) },
1184 { "esp|sp", offsetof(CPUState, regs[4]) },
1185 { "ebp|fp", offsetof(CPUState, regs[5]) },
1186 { "esi", offsetof(CPUState, regs[6]) },
1187 { "edi", offsetof(CPUState, regs[7]) },
1188 #ifdef TARGET_X86_64
1189 { "r8", offsetof(CPUState, regs[8]) },
1190 { "r9", offsetof(CPUState, regs[9]) },
1191 { "r10", offsetof(CPUState, regs[10]) },
1192 { "r11", offsetof(CPUState, regs[11]) },
1193 { "r12", offsetof(CPUState, regs[12]) },
1194 { "r13", offsetof(CPUState, regs[13]) },
1195 { "r14", offsetof(CPUState, regs[14]) },
1196 { "r15", offsetof(CPUState, regs[15]) },
1198 { "eflags", offsetof(CPUState, eflags) },
1199 { "eip", offsetof(CPUState, eip) },
1206 { "pc", 0, monitor_get_pc, },
1207 #elif defined(TARGET_PPC)
1208 { "r0", offsetof(CPUState, gpr[0]) },
1209 { "r1", offsetof(CPUState, gpr[1]) },
1210 { "r2", offsetof(CPUState, gpr[2]) },
1211 { "r3", offsetof(CPUState, gpr[3]) },
1212 { "r4", offsetof(CPUState, gpr[4]) },
1213 { "r5", offsetof(CPUState, gpr[5]) },
1214 { "r6", offsetof(CPUState, gpr[6]) },
1215 { "r7", offsetof(CPUState, gpr[7]) },
1216 { "r8", offsetof(CPUState, gpr[8]) },
1217 { "r9", offsetof(CPUState, gpr[9]) },
1218 { "r10", offsetof(CPUState, gpr[10]) },
1219 { "r11", offsetof(CPUState, gpr[11]) },
1220 { "r12", offsetof(CPUState, gpr[12]) },
1221 { "r13", offsetof(CPUState, gpr[13]) },
1222 { "r14", offsetof(CPUState, gpr[14]) },
1223 { "r15", offsetof(CPUState, gpr[15]) },
1224 { "r16", offsetof(CPUState, gpr[16]) },
1225 { "r17", offsetof(CPUState, gpr[17]) },
1226 { "r18", offsetof(CPUState, gpr[18]) },
1227 { "r19", offsetof(CPUState, gpr[19]) },
1228 { "r20", offsetof(CPUState, gpr[20]) },
1229 { "r21", offsetof(CPUState, gpr[21]) },
1230 { "r22", offsetof(CPUState, gpr[22]) },
1231 { "r23", offsetof(CPUState, gpr[23]) },
1232 { "r24", offsetof(CPUState, gpr[24]) },
1233 { "r25", offsetof(CPUState, gpr[25]) },
1234 { "r26", offsetof(CPUState, gpr[26]) },
1235 { "r27", offsetof(CPUState, gpr[27]) },
1236 { "r28", offsetof(CPUState, gpr[28]) },
1237 { "r29", offsetof(CPUState, gpr[29]) },
1238 { "r30", offsetof(CPUState, gpr[30]) },
1239 { "r31", offsetof(CPUState, gpr[31]) },
1240 { "nip|pc", offsetof(CPUState, nip) },
1241 { "lr", offsetof(CPUState, lr) },
1242 { "ctr", offsetof(CPUState, ctr) },
1243 { "decr", 0, &monitor_get_decr, },
1244 { "ccr", 0, &monitor_get_ccr, },
1245 { "msr", 0, &monitor_get_msr, },
1246 { "xer", 0, &monitor_get_xer, },
1247 { "tbu", 0, &monitor_get_tbu, },
1248 { "tbl", 0, &monitor_get_tbl, },
1249 { "sdr1", offsetof(CPUState, sdr1) },
1250 { "sr0", offsetof(CPUState, sr[0]) },
1251 { "sr1", offsetof(CPUState, sr[1]) },
1252 { "sr2", offsetof(CPUState, sr[2]) },
1253 { "sr3", offsetof(CPUState, sr[3]) },
1254 { "sr4", offsetof(CPUState, sr[4]) },
1255 { "sr5", offsetof(CPUState, sr[5]) },
1256 { "sr6", offsetof(CPUState, sr[6]) },
1257 { "sr7", offsetof(CPUState, sr[7]) },
1258 { "sr8", offsetof(CPUState, sr[8]) },
1259 { "sr9", offsetof(CPUState, sr[9]) },
1260 { "sr10", offsetof(CPUState, sr[10]) },
1261 { "sr11", offsetof(CPUState, sr[11]) },
1262 { "sr12", offsetof(CPUState, sr[12]) },
1263 { "sr13", offsetof(CPUState, sr[13]) },
1264 { "sr14", offsetof(CPUState, sr[14]) },
1265 { "sr15", offsetof(CPUState, sr[15]) },
1266 /* Too lazy to put BATs and SPRs ... */
1267 #elif defined(TARGET_SPARC)
1268 { "g0", offsetof(CPUState, gregs[0]) },
1269 { "g1", offsetof(CPUState, gregs[1]) },
1270 { "g2", offsetof(CPUState, gregs[2]) },
1271 { "g3", offsetof(CPUState, gregs[3]) },
1272 { "g4", offsetof(CPUState, gregs[4]) },
1273 { "g5", offsetof(CPUState, gregs[5]) },
1274 { "g6", offsetof(CPUState, gregs[6]) },
1275 { "g7", offsetof(CPUState, gregs[7]) },
1276 { "o0", 0, monitor_get_reg },
1277 { "o1", 1, monitor_get_reg },
1278 { "o2", 2, monitor_get_reg },
1279 { "o3", 3, monitor_get_reg },
1280 { "o4", 4, monitor_get_reg },
1281 { "o5", 5, monitor_get_reg },
1282 { "o6", 6, monitor_get_reg },
1283 { "o7", 7, monitor_get_reg },
1284 { "l0", 8, monitor_get_reg },
1285 { "l1", 9, monitor_get_reg },
1286 { "l2", 10, monitor_get_reg },
1287 { "l3", 11, monitor_get_reg },
1288 { "l4", 12, monitor_get_reg },
1289 { "l5", 13, monitor_get_reg },
1290 { "l6", 14, monitor_get_reg },
1291 { "l7", 15, monitor_get_reg },
1292 { "i0", 16, monitor_get_reg },
1293 { "i1", 17, monitor_get_reg },
1294 { "i2", 18, monitor_get_reg },
1295 { "i3", 19, monitor_get_reg },
1296 { "i4", 20, monitor_get_reg },
1297 { "i5", 21, monitor_get_reg },
1298 { "i6", 22, monitor_get_reg },
1299 { "i7", 23, monitor_get_reg },
1300 { "pc", offsetof(CPUState, pc) },
1301 { "npc", offsetof(CPUState, npc) },
1302 { "y", offsetof(CPUState, y) },
1303 #ifndef TARGET_SPARC64
1304 { "psr", 0, &monitor_get_psr, },
1305 { "wim", offsetof(CPUState, wim) },
1307 { "tbr", offsetof(CPUState, tbr) },
1308 { "fsr", offsetof(CPUState, fsr) },
1309 { "f0", offsetof(CPUState, fpr[0]) },
1310 { "f1", offsetof(CPUState, fpr[1]) },
1311 { "f2", offsetof(CPUState, fpr[2]) },
1312 { "f3", offsetof(CPUState, fpr[3]) },
1313 { "f4", offsetof(CPUState, fpr[4]) },
1314 { "f5", offsetof(CPUState, fpr[5]) },
1315 { "f6", offsetof(CPUState, fpr[6]) },
1316 { "f7", offsetof(CPUState, fpr[7]) },
1317 { "f8", offsetof(CPUState, fpr[8]) },
1318 { "f9", offsetof(CPUState, fpr[9]) },
1319 { "f10", offsetof(CPUState, fpr[10]) },
1320 { "f11", offsetof(CPUState, fpr[11]) },
1321 { "f12", offsetof(CPUState, fpr[12]) },
1322 { "f13", offsetof(CPUState, fpr[13]) },
1323 { "f14", offsetof(CPUState, fpr[14]) },
1324 { "f15", offsetof(CPUState, fpr[15]) },
1325 { "f16", offsetof(CPUState, fpr[16]) },
1326 { "f17", offsetof(CPUState, fpr[17]) },
1327 { "f18", offsetof(CPUState, fpr[18]) },
1328 { "f19", offsetof(CPUState, fpr[19]) },
1329 { "f20", offsetof(CPUState, fpr[20]) },
1330 { "f21", offsetof(CPUState, fpr[21]) },
1331 { "f22", offsetof(CPUState, fpr[22]) },
1332 { "f23", offsetof(CPUState, fpr[23]) },
1333 { "f24", offsetof(CPUState, fpr[24]) },
1334 { "f25", offsetof(CPUState, fpr[25]) },
1335 { "f26", offsetof(CPUState, fpr[26]) },
1336 { "f27", offsetof(CPUState, fpr[27]) },
1337 { "f28", offsetof(CPUState, fpr[28]) },
1338 { "f29", offsetof(CPUState, fpr[29]) },
1339 { "f30", offsetof(CPUState, fpr[30]) },
1340 { "f31", offsetof(CPUState, fpr[31]) },
1341 #ifdef TARGET_SPARC64
1342 { "f32", offsetof(CPUState, fpr[32]) },
1343 { "f34", offsetof(CPUState, fpr[34]) },
1344 { "f36", offsetof(CPUState, fpr[36]) },
1345 { "f38", offsetof(CPUState, fpr[38]) },
1346 { "f40", offsetof(CPUState, fpr[40]) },
1347 { "f42", offsetof(CPUState, fpr[42]) },
1348 { "f44", offsetof(CPUState, fpr[44]) },
1349 { "f46", offsetof(CPUState, fpr[46]) },
1350 { "f48", offsetof(CPUState, fpr[48]) },
1351 { "f50", offsetof(CPUState, fpr[50]) },
1352 { "f52", offsetof(CPUState, fpr[52]) },
1353 { "f54", offsetof(CPUState, fpr[54]) },
1354 { "f56", offsetof(CPUState, fpr[56]) },
1355 { "f58", offsetof(CPUState, fpr[58]) },
1356 { "f60", offsetof(CPUState, fpr[60]) },
1357 { "f62", offsetof(CPUState, fpr[62]) },
1358 { "asi", offsetof(CPUState, asi) },
1359 { "pstate", offsetof(CPUState, pstate) },
1360 { "cansave", offsetof(CPUState, cansave) },
1361 { "canrestore", offsetof(CPUState, canrestore) },
1362 { "otherwin", offsetof(CPUState, otherwin) },
1363 { "wstate", offsetof(CPUState, wstate) },
1364 { "cleanwin", offsetof(CPUState, cleanwin) },
1365 { "fprs", offsetof(CPUState, fprs) },
1371 static void expr_error(const char *fmt)
1375 longjmp(expr_env, 1);
1378 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1379 static int get_monitor_def(target_long *pval, const char *name)
1384 for(md = monitor_defs; md->name != NULL; md++) {
1385 if (compare_cmd(name, md->name)) {
1386 if (md->get_value) {
1387 *pval = md->get_value(md, md->offset);
1389 CPUState *env = mon_get_cpu();
1392 ptr = (uint8_t *)env + md->offset;
1395 *pval = *(int32_t *)ptr;
1398 *pval = *(target_long *)ptr;
1411 static void next(void)
1415 while (isspace(*pch))
1420 static target_long expr_sum(void);
1422 static target_long expr_unary(void)
1445 expr_error("')' expected");
1452 expr_error("character constant expected");
1456 expr_error("missing terminating \' character");
1465 while ((*pch >= 'a' && *pch <= 'z') ||
1466 (*pch >= 'A' && *pch <= 'Z') ||
1467 (*pch >= '0' && *pch <= '9') ||
1468 *pch == '_' || *pch == '.') {
1469 if ((q - buf) < sizeof(buf) - 1)
1473 while (isspace(*pch))
1476 ret = get_monitor_def(&n, buf);
1478 expr_error("unknown register");
1480 expr_error("no cpu defined");
1484 expr_error("unexpected end of expression");
1488 n = strtoul(pch, &p, 0);
1490 expr_error("invalid char in expression");
1493 while (isspace(*pch))
1501 static target_long expr_prod(void)
1503 target_long val, val2;
1509 if (op != '*' && op != '/' && op != '%')
1512 val2 = expr_unary();
1521 expr_error("division by zero");
1532 static target_long expr_logic(void)
1534 target_long val, val2;
1540 if (op != '&' && op != '|' && op != '^')
1560 static target_long expr_sum(void)
1562 target_long val, val2;
1568 if (op != '+' && op != '-')
1571 val2 = expr_logic();
1580 static int get_expr(target_long *pval, const char **pp)
1583 if (setjmp(expr_env)) {
1587 while (isspace(*pch))
1594 static int get_str(char *buf, int buf_size, const char **pp)
1612 while (*p != '\0' && *p != '\"') {
1628 qemu_printf("unsupported escape code: '\\%c'\n", c);
1631 if ((q - buf) < buf_size - 1) {
1635 if ((q - buf) < buf_size - 1) {
1642 qemu_printf("unterminated string\n");
1647 while (*p != '\0' && !isspace(*p)) {
1648 if ((q - buf) < buf_size - 1) {
1659 static int default_fmt_format = 'x';
1660 static int default_fmt_size = 4;
1664 static void monitor_handle_command(const char *cmdline)
1666 const char *p, *pstart, *typestr;
1668 int c, nb_args, len, i, has_arg;
1672 void *str_allocated[MAX_ARGS];
1673 void *args[MAX_ARGS];
1676 term_printf("command='%s'\n", cmdline);
1679 /* extract the command name */
1687 while (*p != '\0' && *p != '/' && !isspace(*p))
1690 if (len > sizeof(cmdname) - 1)
1691 len = sizeof(cmdname) - 1;
1692 memcpy(cmdname, pstart, len);
1693 cmdname[len] = '\0';
1695 /* find the command */
1696 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1697 if (compare_cmd(cmdname, cmd->name))
1700 term_printf("unknown command: '%s'\n", cmdname);
1704 for(i = 0; i < MAX_ARGS; i++)
1705 str_allocated[i] = NULL;
1707 /* parse the parameters */
1708 typestr = cmd->args_type;
1725 if (*typestr == '?') {
1728 /* no optional string: NULL argument */
1733 ret = get_str(buf, sizeof(buf), &p);
1737 term_printf("%s: filename expected\n", cmdname);
1740 term_printf("%s: block device name expected\n", cmdname);
1743 term_printf("%s: string expected\n", cmdname);
1748 str = qemu_malloc(strlen(buf) + 1);
1750 str_allocated[nb_args] = str;
1752 if (nb_args >= MAX_ARGS) {
1754 term_printf("%s: too many arguments\n", cmdname);
1757 args[nb_args++] = str;
1762 int count, format, size;
1772 while (isdigit(*p)) {
1773 count = count * 10 + (*p - '0');
1811 if (*p != '\0' && !isspace(*p)) {
1812 term_printf("invalid char in format: '%c'\n", *p);
1816 format = default_fmt_format;
1817 if (format != 'i') {
1818 /* for 'i', not specifying a size gives -1 as size */
1820 size = default_fmt_size;
1822 default_fmt_size = size;
1823 default_fmt_format = format;
1826 format = default_fmt_format;
1827 if (format != 'i') {
1828 size = default_fmt_size;
1833 if (nb_args + 3 > MAX_ARGS)
1835 args[nb_args++] = (void*)count;
1836 args[nb_args++] = (void*)format;
1837 args[nb_args++] = (void*)size;
1846 if (*typestr == '?' || *typestr == '.') {
1848 if (*typestr == '?') {
1863 if (nb_args >= MAX_ARGS)
1865 args[nb_args++] = (void *)has_arg;
1867 if (nb_args >= MAX_ARGS)
1873 if (get_expr(&val, &p))
1877 if (nb_args >= MAX_ARGS)
1879 args[nb_args++] = (void *)(int)val;
1881 if ((nb_args + 1) >= MAX_ARGS)
1883 #if TARGET_LONG_BITS == 64
1884 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1886 args[nb_args++] = (void *)0;
1888 args[nb_args++] = (void *)(int)(val & 0xffffffff);
1906 term_printf("%s: unsupported option -%c\n",
1913 if (nb_args >= MAX_ARGS)
1915 args[nb_args++] = (void *)has_option;
1920 term_printf("%s: unknown type '%c'\n", cmdname, c);
1924 /* check that all arguments were parsed */
1928 term_printf("%s: extraneous characters at the end of line\n",
1938 cmd->handler(args[0]);
1941 cmd->handler(args[0], args[1]);
1944 cmd->handler(args[0], args[1], args[2]);
1947 cmd->handler(args[0], args[1], args[2], args[3]);
1950 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1953 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1956 term_printf("unsupported number of arguments: %d\n", nb_args);
1960 for(i = 0; i < MAX_ARGS; i++)
1961 qemu_free(str_allocated[i]);
1965 static void cmd_completion(const char *name, const char *list)
1967 const char *p, *pstart;
1976 p = pstart + strlen(pstart);
1978 if (len > sizeof(cmd) - 2)
1979 len = sizeof(cmd) - 2;
1980 memcpy(cmd, pstart, len);
1982 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1983 add_completion(cmd);
1991 static void file_completion(const char *input)
1996 char file[1024], file_prefix[1024];
2000 p = strrchr(input, '/');
2003 pstrcpy(file_prefix, sizeof(file_prefix), input);
2006 input_path_len = p - input + 1;
2007 memcpy(path, input, input_path_len);
2008 if (input_path_len > sizeof(path) - 1)
2009 input_path_len = sizeof(path) - 1;
2010 path[input_path_len] = '\0';
2011 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2013 #ifdef DEBUG_COMPLETION
2014 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2016 ffs = opendir(path);
2024 if (strstart(d->d_name, file_prefix, NULL)) {
2025 memcpy(file, input, input_path_len);
2026 strcpy(file + input_path_len, d->d_name);
2027 /* stat the file to find out if it's a directory.
2028 * In that case add a slash to speed up typing long paths
2031 if(S_ISDIR(sb.st_mode))
2033 add_completion(file);
2039 static void block_completion_it(void *opaque, const char *name)
2041 const char *input = opaque;
2043 if (input[0] == '\0' ||
2044 !strncmp(name, (char *)input, strlen(input))) {
2045 add_completion(name);
2049 /* NOTE: this parser is an approximate form of the real command parser */
2050 static void parse_cmdline(const char *cmdline,
2051 int *pnb_args, char **args)
2064 if (nb_args >= MAX_ARGS)
2066 ret = get_str(buf, sizeof(buf), &p);
2067 args[nb_args] = qemu_strdup(buf);
2072 *pnb_args = nb_args;
2075 void readline_find_completion(const char *cmdline)
2077 const char *cmdname;
2078 char *args[MAX_ARGS];
2079 int nb_args, i, len;
2080 const char *ptype, *str;
2083 parse_cmdline(cmdline, &nb_args, args);
2084 #ifdef DEBUG_COMPLETION
2085 for(i = 0; i < nb_args; i++) {
2086 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2090 /* if the line ends with a space, it means we want to complete the
2092 len = strlen(cmdline);
2093 if (len > 0 && isspace(cmdline[len - 1])) {
2094 if (nb_args >= MAX_ARGS)
2096 args[nb_args++] = qemu_strdup("");
2099 /* command completion */
2104 completion_index = strlen(cmdname);
2105 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2106 cmd_completion(cmdname, cmd->name);
2109 /* find the command */
2110 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2111 if (compare_cmd(args[0], cmd->name))
2116 ptype = cmd->args_type;
2117 for(i = 0; i < nb_args - 2; i++) {
2118 if (*ptype != '\0') {
2120 while (*ptype == '?')
2124 str = args[nb_args - 1];
2127 /* file completion */
2128 completion_index = strlen(str);
2129 file_completion(str);
2132 /* block device name completion */
2133 completion_index = strlen(str);
2134 bdrv_iterate(block_completion_it, (void *)str);
2137 /* XXX: more generic ? */
2138 if (!strcmp(cmd->name, "info")) {
2139 completion_index = strlen(str);
2140 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2141 cmd_completion(str, cmd->name);
2149 for(i = 0; i < nb_args; i++)
2153 static int term_can_read(void *opaque)
2158 static void term_read(void *opaque, const uint8_t *buf, int size)
2161 for(i = 0; i < size; i++)
2162 readline_handle_byte(buf[i]);
2165 static void monitor_start_input(void);
2167 static void monitor_handle_command1(void *opaque, const char *cmdline)
2169 monitor_handle_command(cmdline);
2170 monitor_start_input();
2173 static void monitor_start_input(void)
2175 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2178 void monitor_init(CharDriverState *hd, int show_banner)
2182 term_printf("QEMU %s monitor - type 'help' for more information\n",
2185 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2186 monitor_start_input();
2189 /* XXX: use threads ? */
2190 /* modal monitor readline */
2191 static int monitor_readline_started;
2192 static char *monitor_readline_buf;
2193 static int monitor_readline_buf_size;
2195 static void monitor_readline_cb(void *opaque, const char *input)
2197 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2198 monitor_readline_started = 0;
2201 void monitor_readline(const char *prompt, int is_password,
2202 char *buf, int buf_size)
2205 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2207 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2208 monitor_readline_buf = buf;
2209 monitor_readline_buf_size = buf_size;
2210 monitor_readline_started = 1;
2211 while (monitor_readline_started) {