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 * '/' optional gdb-like print format (like "/10x")
44 * '?' optional type (for 'F', 's' and 'i')
48 typedef struct term_cmd_t {
50 const char *args_type;
56 static CharDriverState *monitor_hd;
58 static term_cmd_t term_cmds[];
59 static term_cmd_t info_cmds[];
61 static char term_outbuf[1024];
62 static int term_outbuf_index;
64 static void monitor_start_input(void);
68 if (term_outbuf_index > 0) {
69 qemu_chr_write(monitor_hd, term_outbuf, term_outbuf_index);
70 term_outbuf_index = 0;
74 /* flush at every end of line or if the buffer is full */
75 void term_puts(const char *str)
82 term_outbuf[term_outbuf_index++] = c;
83 if (term_outbuf_index >= sizeof(term_outbuf) ||
89 void term_vprintf(const char *fmt, va_list ap)
92 vsnprintf(buf, sizeof(buf), fmt, ap);
96 void term_printf(const char *fmt, ...)
100 term_vprintf(fmt, ap);
104 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
108 term_vprintf(fmt, ap);
113 static int compare_cmd(const char *name, const char *list)
115 const char *p, *pstart;
123 p = pstart + strlen(pstart);
124 if ((p - pstart) == len && !memcmp(pstart, name, len))
133 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
137 for(cmd = cmds; cmd->name != NULL; cmd++) {
138 if (!name || !strcmp(name, cmd->name))
139 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
143 static void help_cmd(const char *name)
145 if (name && !strcmp(name, "info")) {
146 help_cmd1(info_cmds, "info ", NULL);
148 help_cmd1(term_cmds, "", name);
149 if (name && !strcmp(name, "log")) {
151 term_printf("Log items (comma separated):\n");
152 term_printf("%-10s %s\n", "none", "remove all logs");
153 for(item = cpu_log_items; item->mask != 0; item++) {
154 term_printf("%-10s %s\n", item->name, item->help);
160 static void do_help(const char *name)
165 static void do_commit(void)
169 for (i = 0; i < MAX_DISKS; i++) {
171 bdrv_commit(bs_table[i]);
176 static void do_info(const char *item)
182 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
183 if (compare_cmd(item, cmd->name))
193 static void do_info_version(void)
195 term_printf("%s\n", QEMU_VERSION);
198 static void do_info_network(void)
203 for(i = 0; i < nb_nics; i++) {
205 term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
206 for(j = 0; j < 6; j++) {
209 term_printf("%02x", nd->macaddr[j]);
215 static void do_info_block(void)
220 static void do_info_registers(void)
223 cpu_dump_state(cpu_single_env, NULL, monitor_fprintf,
224 X86_DUMP_FPU | X86_DUMP_CCOP);
226 cpu_dump_state(cpu_single_env, NULL, monitor_fprintf,
231 static void do_info_jit(void)
233 dump_exec_info(NULL, monitor_fprintf);
236 static void do_info_history (void)
243 str = readline_get_history(i);
246 term_printf("%d: '%s'\n", i, str);
251 static void do_quit(void)
256 static int eject_device(BlockDriverState *bs, int force)
258 if (bdrv_is_inserted(bs)) {
260 if (!bdrv_is_removable(bs)) {
261 term_printf("device is not removable\n");
264 if (bdrv_is_locked(bs)) {
265 term_printf("device is locked\n");
274 static void do_eject(int force, const char *filename)
276 BlockDriverState *bs;
278 bs = bdrv_find(filename);
280 term_printf("device not found\n");
283 eject_device(bs, force);
286 static void do_change(const char *device, const char *filename)
288 BlockDriverState *bs;
292 bs = bdrv_find(device);
294 term_printf("device not found\n");
297 if (eject_device(bs, 0) < 0)
299 bdrv_open(bs, filename, 0);
300 if (bdrv_is_encrypted(bs)) {
301 term_printf("%s is encrypted.\n", device);
302 for(i = 0; i < 3; i++) {
303 monitor_readline("Password: ", 1, password, sizeof(password));
304 if (bdrv_set_key(bs, password) == 0)
306 term_printf("invalid password\n");
311 static void do_screen_dump(const char *filename)
313 vga_screen_dump(filename);
316 static void do_log(const char *items)
320 if (!strcmp(items, "none")) {
323 mask = cpu_str_to_log_mask(items);
332 static void do_savevm(const char *filename)
334 if (qemu_savevm(filename) < 0)
335 term_printf("I/O error when saving VM to '%s'\n", filename);
338 static void do_loadvm(const char *filename)
340 if (qemu_loadvm(filename) < 0)
341 term_printf("I/O error when loading VM from '%s'\n", filename);
344 static void do_stop(void)
346 vm_stop(EXCP_INTERRUPT);
349 static void do_cont(void)
354 #ifdef CONFIG_GDBSTUB
355 static void do_gdbserver(int has_port, int port)
358 port = DEFAULT_GDBSTUB_PORT;
359 if (gdbserver_start(port) < 0) {
360 qemu_printf("Could not open gdbserver socket on port %d\n", port);
362 qemu_printf("Waiting gdb connection on port %d\n", port);
367 static void term_printc(int c)
384 if (c >= 32 && c <= 126) {
385 term_printf("%c", c);
387 term_printf("\\x%02x", c);
394 static void memory_dump(int count, int format, int wsize,
395 target_ulong addr, int is_physical)
397 int nb_per_line, l, line_size, i, max_digits, len;
407 } else if (wsize == 4) {
410 /* as default we use the current CS size */
412 if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
416 monitor_disas(addr, count, is_physical, flags);
425 nb_per_line = line_size / wsize;
430 max_digits = (wsize * 8 + 2) / 3;
434 max_digits = (wsize * 8) / 4;
438 max_digits = (wsize * 8 * 10 + 32) / 33;
446 term_printf(TARGET_FMT_lx ":", addr);
451 cpu_physical_memory_rw(addr, buf, l, 0);
453 cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
460 v = ldub_raw(buf + i);
463 v = lduw_raw(buf + i);
466 v = ldl_raw(buf + i);
469 v = ldq_raw(buf + i);
475 term_printf("%#*llo", max_digits, v);
478 term_printf("0x%0*llx", max_digits, v);
481 term_printf("%*llu", max_digits, v);
484 term_printf("%*lld", max_digits, v);
498 static void do_memory_dump(int count, int format, int size, int addr)
500 memory_dump(count, format, size, addr, 0);
503 static void do_physical_memory_dump(int count, int format, int size, int addr)
505 memory_dump(count, format, size, addr, 1);
508 static void do_print(int count, int format, int size, int val)
512 term_printf("%#o", val);
515 term_printf("%#x", val);
518 term_printf("%u", val);
522 term_printf("%d", val);
536 static const KeyDef key_defs[] = {
559 { 0x0e, "backspace" },
594 { 0x3a, "caps_lock" },
605 { 0x45, "num_lock" },
606 { 0x46, "scroll_lock" },
630 static int get_keycode(const char *key)
634 for(p = key_defs; p->name != NULL; p++) {
635 if (!strcmp(key, p->name))
641 static void do_send_key(const char *string)
644 uint8_t keycodes[16];
646 int nb_keycodes, keycode, i;
652 while (*p != '\0' && *p != '-') {
653 if ((q - keybuf) < sizeof(keybuf) - 1) {
659 keycode = get_keycode(keybuf);
661 term_printf("unknown key: '%s'\n", keybuf);
664 keycodes[nb_keycodes++] = keycode;
669 /* key down events */
670 for(i = 0; i < nb_keycodes; i++) {
671 keycode = keycodes[i];
673 kbd_put_keycode(0xe0);
674 kbd_put_keycode(keycode & 0x7f);
677 for(i = nb_keycodes - 1; i >= 0; i--) {
678 keycode = keycodes[i];
680 kbd_put_keycode(0xe0);
681 kbd_put_keycode(keycode | 0x80);
685 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
691 cpu_outb(NULL, addr & 0xffff, index & 0xff);
699 val = cpu_inb(NULL, addr);
703 val = cpu_inw(NULL, addr);
707 val = cpu_inl(NULL, addr);
711 term_printf("port%c[0x%04x] = %#0*x\n",
712 suffix, addr, size * 2, val);
715 static void do_system_reset(void)
717 qemu_system_reset_request();
720 #if defined(TARGET_I386)
721 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
723 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
726 pte & PG_GLOBAL_MASK ? 'G' : '-',
727 pte & PG_PSE_MASK ? 'P' : '-',
728 pte & PG_DIRTY_MASK ? 'D' : '-',
729 pte & PG_ACCESSED_MASK ? 'A' : '-',
730 pte & PG_PCD_MASK ? 'C' : '-',
731 pte & PG_PWT_MASK ? 'T' : '-',
732 pte & PG_USER_MASK ? 'U' : '-',
733 pte & PG_RW_MASK ? 'W' : '-');
736 static void tlb_info(void)
738 CPUState *env = cpu_single_env;
740 uint32_t pgd, pde, pte;
742 if (!(env->cr[0] & CR0_PG_MASK)) {
743 term_printf("PG disabled\n");
746 pgd = env->cr[3] & ~0xfff;
747 for(l1 = 0; l1 < 1024; l1++) {
748 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
749 pde = le32_to_cpu(pde);
750 if (pde & PG_PRESENT_MASK) {
751 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
752 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
754 for(l2 = 0; l2 < 1024; l2++) {
755 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
757 pte = le32_to_cpu(pte);
758 if (pte & PG_PRESENT_MASK) {
759 print_pte((l1 << 22) + (l2 << 12),
769 static void mem_print(uint32_t *pstart, int *plast_prot,
770 uint32_t end, int prot)
776 term_printf("%08x-%08x %08x %c%c%c\n",
777 *pstart, end, end - *pstart,
778 prot1 & PG_USER_MASK ? 'u' : '-',
780 prot1 & PG_RW_MASK ? 'w' : '-');
790 static void mem_info(void)
792 CPUState *env = cpu_single_env;
793 int l1, l2, prot, last_prot;
794 uint32_t pgd, pde, pte, start, end;
796 if (!(env->cr[0] & CR0_PG_MASK)) {
797 term_printf("PG disabled\n");
800 pgd = env->cr[3] & ~0xfff;
803 for(l1 = 0; l1 < 1024; l1++) {
804 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
805 pde = le32_to_cpu(pde);
807 if (pde & PG_PRESENT_MASK) {
808 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
809 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
810 mem_print(&start, &last_prot, end, prot);
812 for(l2 = 0; l2 < 1024; l2++) {
813 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
815 pte = le32_to_cpu(pte);
816 end = (l1 << 22) + (l2 << 12);
817 if (pte & PG_PRESENT_MASK) {
818 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
822 mem_print(&start, &last_prot, end, prot);
827 mem_print(&start, &last_prot, end, prot);
833 static term_cmd_t term_cmds[] = {
834 { "help|?", "s?", do_help,
835 "[cmd]", "show the help" },
836 { "commit", "", do_commit,
837 "", "commit changes to the disk images (if -snapshot is used)" },
838 { "info", "s?", do_info,
839 "subcommand", "show various information about the system state" },
840 { "q|quit", "", do_quit,
841 "", "quit the emulator" },
842 { "eject", "-fB", do_eject,
843 "[-f] device", "eject a removable media (use -f to force it)" },
844 { "change", "BF", do_change,
845 "device filename", "change a removable media" },
846 { "screendump", "F", do_screen_dump,
847 "filename", "save screen into PPM image 'filename'" },
848 { "log", "s", do_log,
849 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
850 { "savevm", "F", do_savevm,
851 "filename", "save the whole virtual machine state to 'filename'" },
852 { "loadvm", "F", do_loadvm,
853 "filename", "restore the whole virtual machine state from 'filename'" },
854 { "stop", "", do_stop,
855 "", "stop emulation", },
856 { "c|cont", "", do_cont,
857 "", "resume emulation", },
858 #ifdef CONFIG_GDBSTUB
859 { "gdbserver", "i?", do_gdbserver,
860 "[port]", "start gdbserver session (default port=1234)", },
862 { "x", "/i", do_memory_dump,
863 "/fmt addr", "virtual memory dump starting at 'addr'", },
864 { "xp", "/i", do_physical_memory_dump,
865 "/fmt addr", "physical memory dump starting at 'addr'", },
866 { "p|print", "/i", do_print,
867 "/fmt expr", "print expression value (use $reg for CPU register access)", },
868 { "i", "/ii.", do_ioport_read,
869 "/fmt addr", "I/O port read" },
871 { "sendkey", "s", do_send_key,
872 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
873 { "system_reset", "", do_system_reset,
874 "", "reset the system" },
878 static term_cmd_t info_cmds[] = {
879 { "version", "", do_info_version,
880 "", "show the version of qemu" },
881 { "network", "", do_info_network,
882 "", "show the network state" },
883 { "block", "", do_info_block,
884 "", "show the block devices" },
885 { "registers", "", do_info_registers,
886 "", "show the cpu registers" },
887 { "history", "", do_info_history,
888 "", "show the command line history", },
889 { "irq", "", irq_info,
890 "", "show the interrupts statistics (if available)", },
891 { "pic", "", pic_info,
892 "", "show i8259 (PIC) state", },
893 { "pci", "", pci_info,
894 "", "show PCI info", },
895 #if defined(TARGET_I386)
896 { "tlb", "", tlb_info,
897 "", "show virtual to physical memory mappings", },
898 { "mem", "", mem_info,
899 "", "show the active virtual memory mappings", },
901 { "jit", "", do_info_jit,
902 "", "show dynamic compiler info", },
906 /*******************************************************************/
908 static const char *pch;
909 static jmp_buf expr_env;
911 typedef struct MonitorDef {
914 int (*get_value)(struct MonitorDef *md, int val);
917 #if defined(TARGET_I386)
918 static int monitor_get_pc (struct MonitorDef *md, int val)
920 return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base;
924 #if defined(TARGET_PPC)
925 static int monitor_get_ccr (struct MonitorDef *md, int val)
931 for (i = 0; i < 8; i++)
932 u |= cpu_single_env->crf[i] << (32 - (4 * i));
937 static int monitor_get_msr (struct MonitorDef *md, int val)
939 return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
940 (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
941 (cpu_single_env->msr[MSR_EE] << MSR_EE) |
942 (cpu_single_env->msr[MSR_PR] << MSR_PR) |
943 (cpu_single_env->msr[MSR_FP] << MSR_FP) |
944 (cpu_single_env->msr[MSR_ME] << MSR_ME) |
945 (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
946 (cpu_single_env->msr[MSR_SE] << MSR_SE) |
947 (cpu_single_env->msr[MSR_BE] << MSR_BE) |
948 (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
949 (cpu_single_env->msr[MSR_IP] << MSR_IP) |
950 (cpu_single_env->msr[MSR_IR] << MSR_IR) |
951 (cpu_single_env->msr[MSR_DR] << MSR_DR) |
952 (cpu_single_env->msr[MSR_RI] << MSR_RI) |
953 (cpu_single_env->msr[MSR_LE] << MSR_LE);
956 static int monitor_get_xer (struct MonitorDef *md, int val)
958 return (cpu_single_env->xer[XER_SO] << XER_SO) |
959 (cpu_single_env->xer[XER_OV] << XER_OV) |
960 (cpu_single_env->xer[XER_CA] << XER_CA) |
961 (cpu_single_env->xer[XER_BC] << XER_BC);
964 static int monitor_get_decr (struct MonitorDef *md, int val)
966 return cpu_ppc_load_decr(cpu_single_env);
969 static int monitor_get_tbu (struct MonitorDef *md, int val)
971 return cpu_ppc_load_tbu(cpu_single_env);
974 static int monitor_get_tbl (struct MonitorDef *md, int val)
976 return cpu_ppc_load_tbl(cpu_single_env);
980 #if defined(TARGET_SPARC)
981 static int monitor_get_psr (struct MonitorDef *md, int val)
983 return GET_PSR(cpu_single_env);
986 static int monitor_get_reg(struct MonitorDef *md, int val)
988 return cpu_single_env->regwptr[val];
992 static MonitorDef monitor_defs[] = {
995 #define SEG(name, seg) \
996 { name, offsetof(CPUState, segs[seg].selector) },\
997 { name ".base", offsetof(CPUState, segs[seg].base) },\
998 { name ".limit", offsetof(CPUState, segs[seg].limit) },
1000 { "eax", offsetof(CPUState, regs[0]) },
1001 { "ecx", offsetof(CPUState, regs[1]) },
1002 { "edx", offsetof(CPUState, regs[2]) },
1003 { "ebx", offsetof(CPUState, regs[3]) },
1004 { "esp|sp", offsetof(CPUState, regs[4]) },
1005 { "ebp|fp", offsetof(CPUState, regs[5]) },
1006 { "esi", offsetof(CPUState, regs[6]) },
1007 { "edi", offsetof(CPUState, regs[7]) },
1008 { "eflags", offsetof(CPUState, eflags) },
1009 { "eip", offsetof(CPUState, eip) },
1016 { "pc", 0, monitor_get_pc, },
1017 #elif defined(TARGET_PPC)
1018 { "r0", offsetof(CPUState, gpr[0]) },
1019 { "r1", offsetof(CPUState, gpr[1]) },
1020 { "r2", offsetof(CPUState, gpr[2]) },
1021 { "r3", offsetof(CPUState, gpr[3]) },
1022 { "r4", offsetof(CPUState, gpr[4]) },
1023 { "r5", offsetof(CPUState, gpr[5]) },
1024 { "r6", offsetof(CPUState, gpr[6]) },
1025 { "r7", offsetof(CPUState, gpr[7]) },
1026 { "r8", offsetof(CPUState, gpr[8]) },
1027 { "r9", offsetof(CPUState, gpr[9]) },
1028 { "r10", offsetof(CPUState, gpr[10]) },
1029 { "r11", offsetof(CPUState, gpr[11]) },
1030 { "r12", offsetof(CPUState, gpr[12]) },
1031 { "r13", offsetof(CPUState, gpr[13]) },
1032 { "r14", offsetof(CPUState, gpr[14]) },
1033 { "r15", offsetof(CPUState, gpr[15]) },
1034 { "r16", offsetof(CPUState, gpr[16]) },
1035 { "r17", offsetof(CPUState, gpr[17]) },
1036 { "r18", offsetof(CPUState, gpr[18]) },
1037 { "r19", offsetof(CPUState, gpr[19]) },
1038 { "r20", offsetof(CPUState, gpr[20]) },
1039 { "r21", offsetof(CPUState, gpr[21]) },
1040 { "r22", offsetof(CPUState, gpr[22]) },
1041 { "r23", offsetof(CPUState, gpr[23]) },
1042 { "r24", offsetof(CPUState, gpr[24]) },
1043 { "r25", offsetof(CPUState, gpr[25]) },
1044 { "r26", offsetof(CPUState, gpr[26]) },
1045 { "r27", offsetof(CPUState, gpr[27]) },
1046 { "r28", offsetof(CPUState, gpr[28]) },
1047 { "r29", offsetof(CPUState, gpr[29]) },
1048 { "r30", offsetof(CPUState, gpr[30]) },
1049 { "r31", offsetof(CPUState, gpr[31]) },
1050 { "nip|pc", offsetof(CPUState, nip) },
1051 { "lr", offsetof(CPUState, lr) },
1052 { "ctr", offsetof(CPUState, ctr) },
1053 { "decr", 0, &monitor_get_decr, },
1054 { "ccr", 0, &monitor_get_ccr, },
1055 { "msr", 0, &monitor_get_msr, },
1056 { "xer", 0, &monitor_get_xer, },
1057 { "tbu", 0, &monitor_get_tbu, },
1058 { "tbl", 0, &monitor_get_tbl, },
1059 { "sdr1", offsetof(CPUState, sdr1) },
1060 { "sr0", offsetof(CPUState, sr[0]) },
1061 { "sr1", offsetof(CPUState, sr[1]) },
1062 { "sr2", offsetof(CPUState, sr[2]) },
1063 { "sr3", offsetof(CPUState, sr[3]) },
1064 { "sr4", offsetof(CPUState, sr[4]) },
1065 { "sr5", offsetof(CPUState, sr[5]) },
1066 { "sr6", offsetof(CPUState, sr[6]) },
1067 { "sr7", offsetof(CPUState, sr[7]) },
1068 { "sr8", offsetof(CPUState, sr[8]) },
1069 { "sr9", offsetof(CPUState, sr[9]) },
1070 { "sr10", offsetof(CPUState, sr[10]) },
1071 { "sr11", offsetof(CPUState, sr[11]) },
1072 { "sr12", offsetof(CPUState, sr[12]) },
1073 { "sr13", offsetof(CPUState, sr[13]) },
1074 { "sr14", offsetof(CPUState, sr[14]) },
1075 { "sr15", offsetof(CPUState, sr[15]) },
1076 /* Too lazy to put BATs and SPRs ... */
1077 #elif defined(TARGET_SPARC)
1078 { "g0", offsetof(CPUState, gregs[0]) },
1079 { "g1", offsetof(CPUState, gregs[1]) },
1080 { "g2", offsetof(CPUState, gregs[2]) },
1081 { "g3", offsetof(CPUState, gregs[3]) },
1082 { "g4", offsetof(CPUState, gregs[4]) },
1083 { "g5", offsetof(CPUState, gregs[5]) },
1084 { "g6", offsetof(CPUState, gregs[6]) },
1085 { "g7", offsetof(CPUState, gregs[7]) },
1086 { "o0", 0, monitor_get_reg },
1087 { "o1", 1, monitor_get_reg },
1088 { "o2", 2, monitor_get_reg },
1089 { "o3", 3, monitor_get_reg },
1090 { "o4", 4, monitor_get_reg },
1091 { "o5", 5, monitor_get_reg },
1092 { "o6", 6, monitor_get_reg },
1093 { "o7", 7, monitor_get_reg },
1094 { "l0", 8, monitor_get_reg },
1095 { "l1", 9, monitor_get_reg },
1096 { "l2", 10, monitor_get_reg },
1097 { "l3", 11, monitor_get_reg },
1098 { "l4", 12, monitor_get_reg },
1099 { "l5", 13, monitor_get_reg },
1100 { "l6", 14, monitor_get_reg },
1101 { "l7", 15, monitor_get_reg },
1102 { "i0", 16, monitor_get_reg },
1103 { "i1", 17, monitor_get_reg },
1104 { "i2", 18, monitor_get_reg },
1105 { "i3", 19, monitor_get_reg },
1106 { "i4", 20, monitor_get_reg },
1107 { "i5", 21, monitor_get_reg },
1108 { "i6", 22, monitor_get_reg },
1109 { "i7", 23, monitor_get_reg },
1110 { "pc", offsetof(CPUState, pc) },
1111 { "npc", offsetof(CPUState, npc) },
1112 { "y", offsetof(CPUState, y) },
1113 { "psr", 0, &monitor_get_psr, },
1114 { "wim", offsetof(CPUState, wim) },
1115 { "tbr", offsetof(CPUState, tbr) },
1116 { "fsr", offsetof(CPUState, fsr) },
1117 { "f0", offsetof(CPUState, fpr[0]) },
1118 { "f1", offsetof(CPUState, fpr[1]) },
1119 { "f2", offsetof(CPUState, fpr[2]) },
1120 { "f3", offsetof(CPUState, fpr[3]) },
1121 { "f4", offsetof(CPUState, fpr[4]) },
1122 { "f5", offsetof(CPUState, fpr[5]) },
1123 { "f6", offsetof(CPUState, fpr[6]) },
1124 { "f7", offsetof(CPUState, fpr[7]) },
1125 { "f8", offsetof(CPUState, fpr[8]) },
1126 { "f9", offsetof(CPUState, fpr[9]) },
1127 { "f10", offsetof(CPUState, fpr[10]) },
1128 { "f11", offsetof(CPUState, fpr[11]) },
1129 { "f12", offsetof(CPUState, fpr[12]) },
1130 { "f13", offsetof(CPUState, fpr[13]) },
1131 { "f14", offsetof(CPUState, fpr[14]) },
1132 { "f15", offsetof(CPUState, fpr[15]) },
1133 { "f16", offsetof(CPUState, fpr[16]) },
1134 { "f17", offsetof(CPUState, fpr[17]) },
1135 { "f18", offsetof(CPUState, fpr[18]) },
1136 { "f19", offsetof(CPUState, fpr[19]) },
1137 { "f20", offsetof(CPUState, fpr[20]) },
1138 { "f21", offsetof(CPUState, fpr[21]) },
1139 { "f22", offsetof(CPUState, fpr[22]) },
1140 { "f23", offsetof(CPUState, fpr[23]) },
1141 { "f24", offsetof(CPUState, fpr[24]) },
1142 { "f25", offsetof(CPUState, fpr[25]) },
1143 { "f26", offsetof(CPUState, fpr[26]) },
1144 { "f27", offsetof(CPUState, fpr[27]) },
1145 { "f28", offsetof(CPUState, fpr[28]) },
1146 { "f29", offsetof(CPUState, fpr[29]) },
1147 { "f30", offsetof(CPUState, fpr[30]) },
1148 { "f31", offsetof(CPUState, fpr[31]) },
1153 static void expr_error(const char *fmt)
1157 longjmp(expr_env, 1);
1160 static int get_monitor_def(int *pval, const char *name)
1163 for(md = monitor_defs; md->name != NULL; md++) {
1164 if (compare_cmd(name, md->name)) {
1165 if (md->get_value) {
1166 *pval = md->get_value(md, md->offset);
1168 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
1176 static void next(void)
1180 while (isspace(*pch))
1185 static int expr_sum(void);
1187 static int expr_unary(void)
1209 expr_error("')' expected");
1216 expr_error("character constant expected");
1220 expr_error("missing terminating \' character");
1229 while ((*pch >= 'a' && *pch <= 'z') ||
1230 (*pch >= 'A' && *pch <= 'Z') ||
1231 (*pch >= '0' && *pch <= '9') ||
1232 *pch == '_' || *pch == '.') {
1233 if ((q - buf) < sizeof(buf) - 1)
1237 while (isspace(*pch))
1240 if (get_monitor_def(&n, buf))
1241 expr_error("unknown register");
1245 expr_error("unexpected end of expression");
1249 n = strtoul(pch, &p, 0);
1251 expr_error("invalid char in expression");
1254 while (isspace(*pch))
1262 static int expr_prod(void)
1269 if (op != '*' && op != '/' && op != '%')
1272 val2 = expr_unary();
1281 expr_error("division by zero");
1292 static int expr_logic(void)
1299 if (op != '&' && op != '|' && op != '^')
1319 static int expr_sum(void)
1326 if (op != '+' && op != '-')
1329 val2 = expr_logic();
1338 static int get_expr(int *pval, const char **pp)
1341 if (setjmp(expr_env)) {
1345 while (isspace(*pch))
1352 static int get_str(char *buf, int buf_size, const char **pp)
1370 while (*p != '\0' && *p != '\"') {
1386 qemu_printf("unsupported escape code: '\\%c'\n", c);
1389 if ((q - buf) < buf_size - 1) {
1393 if ((q - buf) < buf_size - 1) {
1400 qemu_printf("unterminated string\n");
1405 while (*p != '\0' && !isspace(*p)) {
1406 if ((q - buf) < buf_size - 1) {
1417 static int default_fmt_format = 'x';
1418 static int default_fmt_size = 4;
1422 static void monitor_handle_command(const char *cmdline)
1424 const char *p, *pstart, *typestr;
1426 int c, nb_args, len, i, has_arg;
1430 void *str_allocated[MAX_ARGS];
1431 void *args[MAX_ARGS];
1434 term_printf("command='%s'\n", cmdline);
1437 /* extract the command name */
1445 while (*p != '\0' && *p != '/' && !isspace(*p))
1448 if (len > sizeof(cmdname) - 1)
1449 len = sizeof(cmdname) - 1;
1450 memcpy(cmdname, pstart, len);
1451 cmdname[len] = '\0';
1453 /* find the command */
1454 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1455 if (compare_cmd(cmdname, cmd->name))
1458 term_printf("unknown command: '%s'\n", cmdname);
1462 for(i = 0; i < MAX_ARGS; i++)
1463 str_allocated[i] = NULL;
1465 /* parse the parameters */
1466 typestr = cmd->args_type;
1483 if (*typestr == '?') {
1486 /* no optional string: NULL argument */
1491 ret = get_str(buf, sizeof(buf), &p);
1495 term_printf("%s: filename expected\n", cmdname);
1498 term_printf("%s: block device name expected\n", cmdname);
1501 term_printf("%s: string expected\n", cmdname);
1506 str = qemu_malloc(strlen(buf) + 1);
1508 str_allocated[nb_args] = str;
1510 if (nb_args >= MAX_ARGS) {
1512 term_printf("%s: too many arguments\n", cmdname);
1515 args[nb_args++] = str;
1520 int count, format, size;
1530 while (isdigit(*p)) {
1531 count = count * 10 + (*p - '0');
1569 if (*p != '\0' && !isspace(*p)) {
1570 term_printf("invalid char in format: '%c'\n", *p);
1574 format = default_fmt_format;
1575 if (format != 'i') {
1576 /* for 'i', not specifying a size gives -1 as size */
1578 size = default_fmt_size;
1580 default_fmt_size = size;
1581 default_fmt_format = format;
1584 format = default_fmt_format;
1585 if (format != 'i') {
1586 size = default_fmt_size;
1591 if (nb_args + 3 > MAX_ARGS)
1593 args[nb_args++] = (void*)count;
1594 args[nb_args++] = (void*)format;
1595 args[nb_args++] = (void*)size;
1603 if (*typestr == '?' || *typestr == '.') {
1605 if (*typestr == '?') {
1620 if (nb_args >= MAX_ARGS)
1622 args[nb_args++] = (void *)has_arg;
1624 if (nb_args >= MAX_ARGS)
1630 if (get_expr(&val, &p))
1633 if (nb_args >= MAX_ARGS)
1635 args[nb_args++] = (void *)val;
1652 term_printf("%s: unsupported option -%c\n",
1659 if (nb_args >= MAX_ARGS)
1661 args[nb_args++] = (void *)has_option;
1666 term_printf("%s: unknown type '%c'\n", cmdname, c);
1670 /* check that all arguments were parsed */
1674 term_printf("%s: extraneous characters at the end of line\n",
1684 cmd->handler(args[0]);
1687 cmd->handler(args[0], args[1]);
1690 cmd->handler(args[0], args[1], args[2]);
1693 cmd->handler(args[0], args[1], args[2], args[3]);
1696 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1699 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
1702 term_printf("unsupported number of arguments: %d\n", nb_args);
1706 for(i = 0; i < MAX_ARGS; i++)
1707 qemu_free(str_allocated[i]);
1711 static void cmd_completion(const char *name, const char *list)
1713 const char *p, *pstart;
1722 p = pstart + strlen(pstart);
1724 if (len > sizeof(cmd) - 2)
1725 len = sizeof(cmd) - 2;
1726 memcpy(cmd, pstart, len);
1728 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
1729 add_completion(cmd);
1737 static void file_completion(const char *input)
1742 char file[1024], file_prefix[1024];
1746 p = strrchr(input, '/');
1749 pstrcpy(file_prefix, sizeof(file_prefix), input);
1752 input_path_len = p - input + 1;
1753 memcpy(path, input, input_path_len);
1754 if (input_path_len > sizeof(path) - 1)
1755 input_path_len = sizeof(path) - 1;
1756 path[input_path_len] = '\0';
1757 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
1759 #ifdef DEBUG_COMPLETION
1760 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
1762 ffs = opendir(path);
1770 if (strstart(d->d_name, file_prefix, NULL)) {
1771 memcpy(file, input, input_path_len);
1772 strcpy(file + input_path_len, d->d_name);
1773 /* stat the file to find out if it's a directory.
1774 * In that case add a slash to speed up typing long paths
1777 if(S_ISDIR(sb.st_mode))
1779 add_completion(file);
1785 static void block_completion_it(void *opaque, const char *name)
1787 const char *input = opaque;
1789 if (input[0] == '\0' ||
1790 !strncmp(name, (char *)input, strlen(input))) {
1791 add_completion(name);
1795 /* NOTE: this parser is an approximate form of the real command parser */
1796 static void parse_cmdline(const char *cmdline,
1797 int *pnb_args, char **args)
1810 if (nb_args >= MAX_ARGS)
1812 ret = get_str(buf, sizeof(buf), &p);
1813 args[nb_args] = qemu_strdup(buf);
1818 *pnb_args = nb_args;
1821 void readline_find_completion(const char *cmdline)
1823 const char *cmdname;
1824 char *args[MAX_ARGS];
1825 int nb_args, i, len;
1826 const char *ptype, *str;
1829 parse_cmdline(cmdline, &nb_args, args);
1830 #ifdef DEBUG_COMPLETION
1831 for(i = 0; i < nb_args; i++) {
1832 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
1836 /* if the line ends with a space, it means we want to complete the
1838 len = strlen(cmdline);
1839 if (len > 0 && isspace(cmdline[len - 1])) {
1840 if (nb_args >= MAX_ARGS)
1842 args[nb_args++] = qemu_strdup("");
1845 /* command completion */
1850 completion_index = strlen(cmdname);
1851 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1852 cmd_completion(cmdname, cmd->name);
1855 /* find the command */
1856 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1857 if (compare_cmd(args[0], cmd->name))
1862 ptype = cmd->args_type;
1863 for(i = 0; i < nb_args - 2; i++) {
1864 if (*ptype != '\0') {
1866 while (*ptype == '?')
1870 str = args[nb_args - 1];
1873 /* file completion */
1874 completion_index = strlen(str);
1875 file_completion(str);
1878 /* block device name completion */
1879 completion_index = strlen(str);
1880 bdrv_iterate(block_completion_it, (void *)str);
1883 /* XXX: more generic ? */
1884 if (!strcmp(cmd->name, "info")) {
1885 completion_index = strlen(str);
1886 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
1887 cmd_completion(str, cmd->name);
1895 for(i = 0; i < nb_args; i++)
1899 static int term_can_read(void *opaque)
1904 static void term_read(void *opaque, const uint8_t *buf, int size)
1907 for(i = 0; i < size; i++)
1908 readline_handle_byte(buf[i]);
1911 static void monitor_start_input(void);
1913 static void monitor_handle_command1(void *opaque, const char *cmdline)
1915 monitor_handle_command(cmdline);
1916 monitor_start_input();
1919 static void monitor_start_input(void)
1921 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
1924 void monitor_init(CharDriverState *hd, int show_banner)
1928 term_printf("QEMU %s monitor - type 'help' for more information\n",
1931 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
1932 monitor_start_input();
1935 /* XXX: use threads ? */
1936 /* modal monitor readline */
1937 static int monitor_readline_started;
1938 static char *monitor_readline_buf;
1939 static int monitor_readline_buf_size;
1941 static void monitor_readline_cb(void *opaque, const char *input)
1943 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
1944 monitor_readline_started = 0;
1947 void monitor_readline(const char *prompt, int is_password,
1948 char *buf, int buf_size)
1951 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
1953 readline_start(prompt, is_password, monitor_readline_cb, NULL);
1954 monitor_readline_buf = buf;
1955 monitor_readline_buf_size = buf_size;
1956 monitor_readline_started = 1;
1957 while (monitor_readline_started) {