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);
256 if (env->hflags & HF_HALTED_MASK)
257 term_printf(" (halted)");
258 #elif defined(TARGET_PPC)
259 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
261 term_printf(" (halted)");
262 #elif defined(TARGET_SPARC)
263 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
265 term_printf(" (halted)");
271 static void do_cpu_set(int index)
273 if (mon_set_cpu(index) < 0)
274 term_printf("Invalid CPU index\n");
277 static void do_info_jit(void)
279 dump_exec_info(NULL, monitor_fprintf);
282 static void do_info_history (void)
289 str = readline_get_history(i);
292 term_printf("%d: '%s'\n", i, str);
297 static void do_quit(void)
302 static int eject_device(BlockDriverState *bs, int force)
304 if (bdrv_is_inserted(bs)) {
306 if (!bdrv_is_removable(bs)) {
307 term_printf("device is not removable\n");
310 if (bdrv_is_locked(bs)) {
311 term_printf("device is locked\n");
320 static void do_eject(int force, const char *filename)
322 BlockDriverState *bs;
324 bs = bdrv_find(filename);
326 term_printf("device not found\n");
329 eject_device(bs, force);
332 static void do_change(const char *device, const char *filename)
334 BlockDriverState *bs;
338 bs = bdrv_find(device);
340 term_printf("device not found\n");
343 if (eject_device(bs, 0) < 0)
345 bdrv_open(bs, filename, 0);
346 if (bdrv_is_encrypted(bs)) {
347 term_printf("%s is encrypted.\n", device);
348 for(i = 0; i < 3; i++) {
349 monitor_readline("Password: ", 1, password, sizeof(password));
350 if (bdrv_set_key(bs, password) == 0)
352 term_printf("invalid password\n");
357 static void do_screen_dump(const char *filename)
359 vga_screen_dump(filename);
362 static void do_log(const char *items)
366 if (!strcmp(items, "none")) {
369 mask = cpu_str_to_log_mask(items);
378 static void do_savevm(const char *filename)
380 if (qemu_savevm(filename) < 0)
381 term_printf("I/O error when saving VM to '%s'\n", filename);
384 static void do_loadvm(const char *filename)
386 if (qemu_loadvm(filename) < 0)
387 term_printf("I/O error when loading VM from '%s'\n", filename);
390 static void do_stop(void)
392 vm_stop(EXCP_INTERRUPT);
395 static void do_cont(void)
400 #ifdef CONFIG_GDBSTUB
401 static void do_gdbserver(int has_port, int port)
404 port = DEFAULT_GDBSTUB_PORT;
405 if (gdbserver_start(port) < 0) {
406 qemu_printf("Could not open gdbserver socket on port %d\n", port);
408 qemu_printf("Waiting gdb connection on port %d\n", port);
413 static void term_printc(int c)
430 if (c >= 32 && c <= 126) {
431 term_printf("%c", c);
433 term_printf("\\x%02x", c);
440 static void memory_dump(int count, int format, int wsize,
441 target_ulong addr, int is_physical)
444 int nb_per_line, l, line_size, i, max_digits, len;
452 if (!env && !is_physical)
457 } else if (wsize == 4) {
460 /* as default we use the current CS size */
462 if (env && !(env->segs[R_CS].flags & DESC_B_MASK))
466 monitor_disas(env, addr, count, is_physical, flags);
475 nb_per_line = line_size / wsize;
480 max_digits = (wsize * 8 + 2) / 3;
484 max_digits = (wsize * 8) / 4;
488 max_digits = (wsize * 8 * 10 + 32) / 33;
496 term_printf(TARGET_FMT_lx ":", addr);
501 cpu_physical_memory_rw(addr, buf, l, 0);
506 cpu_memory_rw_debug(env, addr, buf, l, 0);
513 v = ldub_raw(buf + i);
516 v = lduw_raw(buf + i);
519 v = (uint32_t)ldl_raw(buf + i);
522 v = ldq_raw(buf + i);
528 term_printf("%#*llo", max_digits, v);
531 term_printf("0x%0*llx", max_digits, v);
534 term_printf("%*llu", max_digits, v);
537 term_printf("%*lld", max_digits, v);
551 #if TARGET_LONG_BITS == 64
552 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
554 #define GET_TLONG(h, l) (l)
557 static void do_memory_dump(int count, int format, int size,
558 uint32_t addrh, uint32_t addrl)
560 target_long addr = GET_TLONG(addrh, addrl);
561 memory_dump(count, format, size, addr, 0);
564 static void do_physical_memory_dump(int count, int format, int size,
565 uint32_t addrh, uint32_t addrl)
568 target_long addr = GET_TLONG(addrh, addrl);
569 memory_dump(count, format, size, addr, 1);
572 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
574 target_long val = GET_TLONG(valh, vall);
575 #if TARGET_LONG_BITS == 32
578 term_printf("%#o", val);
581 term_printf("%#x", val);
584 term_printf("%u", val);
588 term_printf("%d", val);
597 term_printf("%#llo", val);
600 term_printf("%#llx", val);
603 term_printf("%llu", val);
607 term_printf("%lld", val);
617 static void do_sum(uint32_t start, uint32_t size)
624 for(addr = start; addr < (start + size); addr++) {
625 cpu_physical_memory_rw(addr, buf, 1, 0);
626 /* BSD sum algorithm ('sum' Unix command) */
627 sum = (sum >> 1) | (sum << 15);
630 term_printf("%05d\n", sum);
638 static const KeyDef key_defs[] = {
661 { 0x0e, "backspace" },
696 { 0x3a, "caps_lock" },
707 { 0x45, "num_lock" },
708 { 0x46, "scroll_lock" },
732 static int get_keycode(const char *key)
736 for(p = key_defs; p->name != NULL; p++) {
737 if (!strcmp(key, p->name))
743 static void do_send_key(const char *string)
746 uint8_t keycodes[16];
748 int nb_keycodes, keycode, i;
754 while (*p != '\0' && *p != '-') {
755 if ((q - keybuf) < sizeof(keybuf) - 1) {
761 keycode = get_keycode(keybuf);
763 term_printf("unknown key: '%s'\n", keybuf);
766 keycodes[nb_keycodes++] = keycode;
771 /* key down events */
772 for(i = 0; i < nb_keycodes; i++) {
773 keycode = keycodes[i];
775 kbd_put_keycode(0xe0);
776 kbd_put_keycode(keycode & 0x7f);
779 for(i = nb_keycodes - 1; i >= 0; i--) {
780 keycode = keycodes[i];
782 kbd_put_keycode(0xe0);
783 kbd_put_keycode(keycode | 0x80);
787 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
793 cpu_outb(NULL, addr & 0xffff, index & 0xff);
801 val = cpu_inb(NULL, addr);
805 val = cpu_inw(NULL, addr);
809 val = cpu_inl(NULL, addr);
813 term_printf("port%c[0x%04x] = %#0*x\n",
814 suffix, addr, size * 2, val);
817 static void do_system_reset(void)
819 qemu_system_reset_request();
822 static void do_system_powerdown(void)
824 qemu_system_powerdown_request();
827 #if defined(TARGET_I386)
828 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
830 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
833 pte & PG_GLOBAL_MASK ? 'G' : '-',
834 pte & PG_PSE_MASK ? 'P' : '-',
835 pte & PG_DIRTY_MASK ? 'D' : '-',
836 pte & PG_ACCESSED_MASK ? 'A' : '-',
837 pte & PG_PCD_MASK ? 'C' : '-',
838 pte & PG_PWT_MASK ? 'T' : '-',
839 pte & PG_USER_MASK ? 'U' : '-',
840 pte & PG_RW_MASK ? 'W' : '-');
843 static void tlb_info(void)
847 uint32_t pgd, pde, pte;
853 if (!(env->cr[0] & CR0_PG_MASK)) {
854 term_printf("PG disabled\n");
857 pgd = env->cr[3] & ~0xfff;
858 for(l1 = 0; l1 < 1024; l1++) {
859 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
860 pde = le32_to_cpu(pde);
861 if (pde & PG_PRESENT_MASK) {
862 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
863 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
865 for(l2 = 0; l2 < 1024; l2++) {
866 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
868 pte = le32_to_cpu(pte);
869 if (pte & PG_PRESENT_MASK) {
870 print_pte((l1 << 22) + (l2 << 12),
880 static void mem_print(uint32_t *pstart, int *plast_prot,
881 uint32_t end, int prot)
887 term_printf("%08x-%08x %08x %c%c%c\n",
888 *pstart, end, end - *pstart,
889 prot1 & PG_USER_MASK ? 'u' : '-',
891 prot1 & PG_RW_MASK ? 'w' : '-');
901 static void mem_info(void)
904 int l1, l2, prot, last_prot;
905 uint32_t pgd, pde, pte, start, end;
911 if (!(env->cr[0] & CR0_PG_MASK)) {
912 term_printf("PG disabled\n");
915 pgd = env->cr[3] & ~0xfff;
918 for(l1 = 0; l1 < 1024; l1++) {
919 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
920 pde = le32_to_cpu(pde);
922 if (pde & PG_PRESENT_MASK) {
923 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
924 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
925 mem_print(&start, &last_prot, end, prot);
927 for(l2 = 0; l2 < 1024; l2++) {
928 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
930 pte = le32_to_cpu(pte);
931 end = (l1 << 22) + (l2 << 12);
932 if (pte & PG_PRESENT_MASK) {
933 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
937 mem_print(&start, &last_prot, end, prot);
942 mem_print(&start, &last_prot, end, prot);
948 static void do_info_kqemu(void)
956 term_printf("No cpu initialized yet");
959 val = env->kqemu_enabled;
960 term_printf("kqemu support: ");
964 term_printf("disabled\n");
967 term_printf("enabled for user code\n");
970 term_printf("enabled for user and kernel code\n");
974 term_printf("kqemu support: not compiled\n");
978 #ifdef CONFIG_PROFILER
982 int64_t kqemu_exec_count;
984 int64_t kqemu_ret_int_count;
985 int64_t kqemu_ret_excp_count;
986 int64_t kqemu_ret_intr_count;
988 static void do_info_profile(void)
994 term_printf("async time %lld (%0.3f)\n",
995 dev_time, dev_time / (double)ticks_per_sec);
996 term_printf("qemu time %lld (%0.3f)\n",
997 qemu_time, qemu_time / (double)ticks_per_sec);
998 term_printf("kqemu time %lld (%0.3f %0.1f%%) count=%lld int=%lld excp=%lld intr=%lld\n",
999 kqemu_time, kqemu_time / (double)ticks_per_sec,
1000 kqemu_time / (double)total * 100.0,
1002 kqemu_ret_int_count,
1003 kqemu_ret_excp_count,
1004 kqemu_ret_intr_count);
1007 kqemu_exec_count = 0;
1009 kqemu_ret_int_count = 0;
1010 kqemu_ret_excp_count = 0;
1011 kqemu_ret_intr_count = 0;
1013 kqemu_record_dump();
1017 static void do_info_profile(void)
1019 term_printf("Internal profiler not compiled\n");
1023 static term_cmd_t term_cmds[] = {
1024 { "help|?", "s?", do_help,
1025 "[cmd]", "show the help" },
1026 { "commit", "", do_commit,
1027 "", "commit changes to the disk images (if -snapshot is used)" },
1028 { "info", "s?", do_info,
1029 "subcommand", "show various information about the system state" },
1030 { "q|quit", "", do_quit,
1031 "", "quit the emulator" },
1032 { "eject", "-fB", do_eject,
1033 "[-f] device", "eject a removable media (use -f to force it)" },
1034 { "change", "BF", do_change,
1035 "device filename", "change a removable media" },
1036 { "screendump", "F", do_screen_dump,
1037 "filename", "save screen into PPM image 'filename'" },
1038 { "log", "s", do_log,
1039 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1040 { "savevm", "F", do_savevm,
1041 "filename", "save the whole virtual machine state to 'filename'" },
1042 { "loadvm", "F", do_loadvm,
1043 "filename", "restore the whole virtual machine state from 'filename'" },
1044 { "stop", "", do_stop,
1045 "", "stop emulation", },
1046 { "c|cont", "", do_cont,
1047 "", "resume emulation", },
1048 #ifdef CONFIG_GDBSTUB
1049 { "gdbserver", "i?", do_gdbserver,
1050 "[port]", "start gdbserver session (default port=1234)", },
1052 { "x", "/l", do_memory_dump,
1053 "/fmt addr", "virtual memory dump starting at 'addr'", },
1054 { "xp", "/l", do_physical_memory_dump,
1055 "/fmt addr", "physical memory dump starting at 'addr'", },
1056 { "p|print", "/l", do_print,
1057 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1058 { "i", "/ii.", do_ioport_read,
1059 "/fmt addr", "I/O port read" },
1061 { "sendkey", "s", do_send_key,
1062 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1063 { "system_reset", "", do_system_reset,
1064 "", "reset the system" },
1065 { "system_powerdown", "", do_system_powerdown,
1066 "", "send system power down event" },
1067 { "sum", "ii", do_sum,
1068 "addr size", "compute the checksum of a memory region" },
1069 { "usb_add", "s", do_usb_add,
1070 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1071 { "usb_del", "s", do_usb_del,
1072 "device", "remove USB device 'bus.addr'" },
1073 { "cpu", "i", do_cpu_set,
1074 "index", "set the default CPU" },
1078 static term_cmd_t info_cmds[] = {
1079 { "version", "", do_info_version,
1080 "", "show the version of qemu" },
1081 { "network", "", do_info_network,
1082 "", "show the network state" },
1083 { "block", "", do_info_block,
1084 "", "show the block devices" },
1085 { "registers", "", do_info_registers,
1086 "", "show the cpu registers" },
1087 { "cpus", "", do_info_cpus,
1088 "", "show infos for each CPU" },
1089 { "history", "", do_info_history,
1090 "", "show the command line history", },
1091 { "irq", "", irq_info,
1092 "", "show the interrupts statistics (if available)", },
1093 { "pic", "", pic_info,
1094 "", "show i8259 (PIC) state", },
1095 { "pci", "", pci_info,
1096 "", "show PCI info", },
1097 #if defined(TARGET_I386)
1098 { "tlb", "", tlb_info,
1099 "", "show virtual to physical memory mappings", },
1100 { "mem", "", mem_info,
1101 "", "show the active virtual memory mappings", },
1103 { "jit", "", do_info_jit,
1104 "", "show dynamic compiler info", },
1105 { "kqemu", "", do_info_kqemu,
1106 "", "show kqemu information", },
1107 { "usb", "", usb_info,
1108 "", "show guest USB devices", },
1109 { "usbhost", "", usb_host_info,
1110 "", "show host USB devices", },
1111 { "profile", "", do_info_profile,
1112 "", "show profiling information", },
1116 /*******************************************************************/
1118 static const char *pch;
1119 static jmp_buf expr_env;
1124 typedef struct MonitorDef {
1127 target_long (*get_value)(struct MonitorDef *md, int val);
1131 #if defined(TARGET_I386)
1132 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1134 CPUState *env = mon_get_cpu();
1137 return env->eip + env->segs[R_CS].base;
1141 #if defined(TARGET_PPC)
1142 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1144 CPUState *env = mon_get_cpu();
1152 for (i = 0; i < 8; i++)
1153 u |= env->crf[i] << (32 - (4 * i));
1158 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1160 CPUState *env = mon_get_cpu();
1163 return (env->msr[MSR_POW] << MSR_POW) |
1164 (env->msr[MSR_ILE] << MSR_ILE) |
1165 (env->msr[MSR_EE] << MSR_EE) |
1166 (env->msr[MSR_PR] << MSR_PR) |
1167 (env->msr[MSR_FP] << MSR_FP) |
1168 (env->msr[MSR_ME] << MSR_ME) |
1169 (env->msr[MSR_FE0] << MSR_FE0) |
1170 (env->msr[MSR_SE] << MSR_SE) |
1171 (env->msr[MSR_BE] << MSR_BE) |
1172 (env->msr[MSR_FE1] << MSR_FE1) |
1173 (env->msr[MSR_IP] << MSR_IP) |
1174 (env->msr[MSR_IR] << MSR_IR) |
1175 (env->msr[MSR_DR] << MSR_DR) |
1176 (env->msr[MSR_RI] << MSR_RI) |
1177 (env->msr[MSR_LE] << MSR_LE);
1180 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1182 CPUState *env = mon_get_cpu();
1185 return (env->xer[XER_SO] << XER_SO) |
1186 (env->xer[XER_OV] << XER_OV) |
1187 (env->xer[XER_CA] << XER_CA) |
1188 (env->xer[XER_BC] << XER_BC);
1191 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1193 CPUState *env = mon_get_cpu();
1196 return cpu_ppc_load_decr(env);
1199 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1201 CPUState *env = mon_get_cpu();
1204 return cpu_ppc_load_tbu(env);
1207 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1209 CPUState *env = mon_get_cpu();
1212 return cpu_ppc_load_tbl(env);
1216 #if defined(TARGET_SPARC)
1217 #ifndef TARGET_SPARC64
1218 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1220 CPUState *env = mon_get_cpu();
1223 return GET_PSR(env);
1227 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1229 CPUState *env = mon_get_cpu();
1232 return env->regwptr[val];
1236 static MonitorDef monitor_defs[] = {
1239 #define SEG(name, seg) \
1240 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1241 { name ".base", offsetof(CPUState, segs[seg].base) },\
1242 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1244 { "eax", offsetof(CPUState, regs[0]) },
1245 { "ecx", offsetof(CPUState, regs[1]) },
1246 { "edx", offsetof(CPUState, regs[2]) },
1247 { "ebx", offsetof(CPUState, regs[3]) },
1248 { "esp|sp", offsetof(CPUState, regs[4]) },
1249 { "ebp|fp", offsetof(CPUState, regs[5]) },
1250 { "esi", offsetof(CPUState, regs[6]) },
1251 { "edi", offsetof(CPUState, regs[7]) },
1252 #ifdef TARGET_X86_64
1253 { "r8", offsetof(CPUState, regs[8]) },
1254 { "r9", offsetof(CPUState, regs[9]) },
1255 { "r10", offsetof(CPUState, regs[10]) },
1256 { "r11", offsetof(CPUState, regs[11]) },
1257 { "r12", offsetof(CPUState, regs[12]) },
1258 { "r13", offsetof(CPUState, regs[13]) },
1259 { "r14", offsetof(CPUState, regs[14]) },
1260 { "r15", offsetof(CPUState, regs[15]) },
1262 { "eflags", offsetof(CPUState, eflags) },
1263 { "eip", offsetof(CPUState, eip) },
1270 { "pc", 0, monitor_get_pc, },
1271 #elif defined(TARGET_PPC)
1272 { "r0", offsetof(CPUState, gpr[0]) },
1273 { "r1", offsetof(CPUState, gpr[1]) },
1274 { "r2", offsetof(CPUState, gpr[2]) },
1275 { "r3", offsetof(CPUState, gpr[3]) },
1276 { "r4", offsetof(CPUState, gpr[4]) },
1277 { "r5", offsetof(CPUState, gpr[5]) },
1278 { "r6", offsetof(CPUState, gpr[6]) },
1279 { "r7", offsetof(CPUState, gpr[7]) },
1280 { "r8", offsetof(CPUState, gpr[8]) },
1281 { "r9", offsetof(CPUState, gpr[9]) },
1282 { "r10", offsetof(CPUState, gpr[10]) },
1283 { "r11", offsetof(CPUState, gpr[11]) },
1284 { "r12", offsetof(CPUState, gpr[12]) },
1285 { "r13", offsetof(CPUState, gpr[13]) },
1286 { "r14", offsetof(CPUState, gpr[14]) },
1287 { "r15", offsetof(CPUState, gpr[15]) },
1288 { "r16", offsetof(CPUState, gpr[16]) },
1289 { "r17", offsetof(CPUState, gpr[17]) },
1290 { "r18", offsetof(CPUState, gpr[18]) },
1291 { "r19", offsetof(CPUState, gpr[19]) },
1292 { "r20", offsetof(CPUState, gpr[20]) },
1293 { "r21", offsetof(CPUState, gpr[21]) },
1294 { "r22", offsetof(CPUState, gpr[22]) },
1295 { "r23", offsetof(CPUState, gpr[23]) },
1296 { "r24", offsetof(CPUState, gpr[24]) },
1297 { "r25", offsetof(CPUState, gpr[25]) },
1298 { "r26", offsetof(CPUState, gpr[26]) },
1299 { "r27", offsetof(CPUState, gpr[27]) },
1300 { "r28", offsetof(CPUState, gpr[28]) },
1301 { "r29", offsetof(CPUState, gpr[29]) },
1302 { "r30", offsetof(CPUState, gpr[30]) },
1303 { "r31", offsetof(CPUState, gpr[31]) },
1304 { "nip|pc", offsetof(CPUState, nip) },
1305 { "lr", offsetof(CPUState, lr) },
1306 { "ctr", offsetof(CPUState, ctr) },
1307 { "decr", 0, &monitor_get_decr, },
1308 { "ccr", 0, &monitor_get_ccr, },
1309 { "msr", 0, &monitor_get_msr, },
1310 { "xer", 0, &monitor_get_xer, },
1311 { "tbu", 0, &monitor_get_tbu, },
1312 { "tbl", 0, &monitor_get_tbl, },
1313 { "sdr1", offsetof(CPUState, sdr1) },
1314 { "sr0", offsetof(CPUState, sr[0]) },
1315 { "sr1", offsetof(CPUState, sr[1]) },
1316 { "sr2", offsetof(CPUState, sr[2]) },
1317 { "sr3", offsetof(CPUState, sr[3]) },
1318 { "sr4", offsetof(CPUState, sr[4]) },
1319 { "sr5", offsetof(CPUState, sr[5]) },
1320 { "sr6", offsetof(CPUState, sr[6]) },
1321 { "sr7", offsetof(CPUState, sr[7]) },
1322 { "sr8", offsetof(CPUState, sr[8]) },
1323 { "sr9", offsetof(CPUState, sr[9]) },
1324 { "sr10", offsetof(CPUState, sr[10]) },
1325 { "sr11", offsetof(CPUState, sr[11]) },
1326 { "sr12", offsetof(CPUState, sr[12]) },
1327 { "sr13", offsetof(CPUState, sr[13]) },
1328 { "sr14", offsetof(CPUState, sr[14]) },
1329 { "sr15", offsetof(CPUState, sr[15]) },
1330 /* Too lazy to put BATs and SPRs ... */
1331 #elif defined(TARGET_SPARC)
1332 { "g0", offsetof(CPUState, gregs[0]) },
1333 { "g1", offsetof(CPUState, gregs[1]) },
1334 { "g2", offsetof(CPUState, gregs[2]) },
1335 { "g3", offsetof(CPUState, gregs[3]) },
1336 { "g4", offsetof(CPUState, gregs[4]) },
1337 { "g5", offsetof(CPUState, gregs[5]) },
1338 { "g6", offsetof(CPUState, gregs[6]) },
1339 { "g7", offsetof(CPUState, gregs[7]) },
1340 { "o0", 0, monitor_get_reg },
1341 { "o1", 1, monitor_get_reg },
1342 { "o2", 2, monitor_get_reg },
1343 { "o3", 3, monitor_get_reg },
1344 { "o4", 4, monitor_get_reg },
1345 { "o5", 5, monitor_get_reg },
1346 { "o6", 6, monitor_get_reg },
1347 { "o7", 7, monitor_get_reg },
1348 { "l0", 8, monitor_get_reg },
1349 { "l1", 9, monitor_get_reg },
1350 { "l2", 10, monitor_get_reg },
1351 { "l3", 11, monitor_get_reg },
1352 { "l4", 12, monitor_get_reg },
1353 { "l5", 13, monitor_get_reg },
1354 { "l6", 14, monitor_get_reg },
1355 { "l7", 15, monitor_get_reg },
1356 { "i0", 16, monitor_get_reg },
1357 { "i1", 17, monitor_get_reg },
1358 { "i2", 18, monitor_get_reg },
1359 { "i3", 19, monitor_get_reg },
1360 { "i4", 20, monitor_get_reg },
1361 { "i5", 21, monitor_get_reg },
1362 { "i6", 22, monitor_get_reg },
1363 { "i7", 23, monitor_get_reg },
1364 { "pc", offsetof(CPUState, pc) },
1365 { "npc", offsetof(CPUState, npc) },
1366 { "y", offsetof(CPUState, y) },
1367 #ifndef TARGET_SPARC64
1368 { "psr", 0, &monitor_get_psr, },
1369 { "wim", offsetof(CPUState, wim) },
1371 { "tbr", offsetof(CPUState, tbr) },
1372 { "fsr", offsetof(CPUState, fsr) },
1373 { "f0", offsetof(CPUState, fpr[0]) },
1374 { "f1", offsetof(CPUState, fpr[1]) },
1375 { "f2", offsetof(CPUState, fpr[2]) },
1376 { "f3", offsetof(CPUState, fpr[3]) },
1377 { "f4", offsetof(CPUState, fpr[4]) },
1378 { "f5", offsetof(CPUState, fpr[5]) },
1379 { "f6", offsetof(CPUState, fpr[6]) },
1380 { "f7", offsetof(CPUState, fpr[7]) },
1381 { "f8", offsetof(CPUState, fpr[8]) },
1382 { "f9", offsetof(CPUState, fpr[9]) },
1383 { "f10", offsetof(CPUState, fpr[10]) },
1384 { "f11", offsetof(CPUState, fpr[11]) },
1385 { "f12", offsetof(CPUState, fpr[12]) },
1386 { "f13", offsetof(CPUState, fpr[13]) },
1387 { "f14", offsetof(CPUState, fpr[14]) },
1388 { "f15", offsetof(CPUState, fpr[15]) },
1389 { "f16", offsetof(CPUState, fpr[16]) },
1390 { "f17", offsetof(CPUState, fpr[17]) },
1391 { "f18", offsetof(CPUState, fpr[18]) },
1392 { "f19", offsetof(CPUState, fpr[19]) },
1393 { "f20", offsetof(CPUState, fpr[20]) },
1394 { "f21", offsetof(CPUState, fpr[21]) },
1395 { "f22", offsetof(CPUState, fpr[22]) },
1396 { "f23", offsetof(CPUState, fpr[23]) },
1397 { "f24", offsetof(CPUState, fpr[24]) },
1398 { "f25", offsetof(CPUState, fpr[25]) },
1399 { "f26", offsetof(CPUState, fpr[26]) },
1400 { "f27", offsetof(CPUState, fpr[27]) },
1401 { "f28", offsetof(CPUState, fpr[28]) },
1402 { "f29", offsetof(CPUState, fpr[29]) },
1403 { "f30", offsetof(CPUState, fpr[30]) },
1404 { "f31", offsetof(CPUState, fpr[31]) },
1405 #ifdef TARGET_SPARC64
1406 { "f32", offsetof(CPUState, fpr[32]) },
1407 { "f34", offsetof(CPUState, fpr[34]) },
1408 { "f36", offsetof(CPUState, fpr[36]) },
1409 { "f38", offsetof(CPUState, fpr[38]) },
1410 { "f40", offsetof(CPUState, fpr[40]) },
1411 { "f42", offsetof(CPUState, fpr[42]) },
1412 { "f44", offsetof(CPUState, fpr[44]) },
1413 { "f46", offsetof(CPUState, fpr[46]) },
1414 { "f48", offsetof(CPUState, fpr[48]) },
1415 { "f50", offsetof(CPUState, fpr[50]) },
1416 { "f52", offsetof(CPUState, fpr[52]) },
1417 { "f54", offsetof(CPUState, fpr[54]) },
1418 { "f56", offsetof(CPUState, fpr[56]) },
1419 { "f58", offsetof(CPUState, fpr[58]) },
1420 { "f60", offsetof(CPUState, fpr[60]) },
1421 { "f62", offsetof(CPUState, fpr[62]) },
1422 { "asi", offsetof(CPUState, asi) },
1423 { "pstate", offsetof(CPUState, pstate) },
1424 { "cansave", offsetof(CPUState, cansave) },
1425 { "canrestore", offsetof(CPUState, canrestore) },
1426 { "otherwin", offsetof(CPUState, otherwin) },
1427 { "wstate", offsetof(CPUState, wstate) },
1428 { "cleanwin", offsetof(CPUState, cleanwin) },
1429 { "fprs", offsetof(CPUState, fprs) },
1435 static void expr_error(const char *fmt)
1439 longjmp(expr_env, 1);
1442 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1443 static int get_monitor_def(target_long *pval, const char *name)
1448 for(md = monitor_defs; md->name != NULL; md++) {
1449 if (compare_cmd(name, md->name)) {
1450 if (md->get_value) {
1451 *pval = md->get_value(md, md->offset);
1453 CPUState *env = mon_get_cpu();
1456 ptr = (uint8_t *)env + md->offset;
1459 *pval = *(int32_t *)ptr;
1462 *pval = *(target_long *)ptr;
1475 static void next(void)
1479 while (isspace(*pch))
1484 static target_long expr_sum(void);
1486 static target_long expr_unary(void)
1509 expr_error("')' expected");
1516 expr_error("character constant expected");
1520 expr_error("missing terminating \' character");
1529 while ((*pch >= 'a' && *pch <= 'z') ||
1530 (*pch >= 'A' && *pch <= 'Z') ||
1531 (*pch >= '0' && *pch <= '9') ||
1532 *pch == '_' || *pch == '.') {
1533 if ((q - buf) < sizeof(buf) - 1)
1537 while (isspace(*pch))
1540 ret = get_monitor_def(&n, buf);
1542 expr_error("unknown register");
1544 expr_error("no cpu defined");
1548 expr_error("unexpected end of expression");
1552 n = strtoul(pch, &p, 0);
1554 expr_error("invalid char in expression");
1557 while (isspace(*pch))
1565 static target_long expr_prod(void)
1567 target_long val, val2;
1573 if (op != '*' && op != '/' && op != '%')
1576 val2 = expr_unary();
1585 expr_error("division by zero");
1596 static target_long expr_logic(void)
1598 target_long val, val2;
1604 if (op != '&' && op != '|' && op != '^')
1624 static target_long expr_sum(void)
1626 target_long val, val2;
1632 if (op != '+' && op != '-')
1635 val2 = expr_logic();
1644 static int get_expr(target_long *pval, const char **pp)
1647 if (setjmp(expr_env)) {
1651 while (isspace(*pch))
1658 static int get_str(char *buf, int buf_size, const char **pp)
1676 while (*p != '\0' && *p != '\"') {
1692 qemu_printf("unsupported escape code: '\\%c'\n", c);
1695 if ((q - buf) < buf_size - 1) {
1699 if ((q - buf) < buf_size - 1) {
1706 qemu_printf("unterminated string\n");
1711 while (*p != '\0' && !isspace(*p)) {
1712 if ((q - buf) < buf_size - 1) {
1723 static int default_fmt_format = 'x';
1724 static int default_fmt_size = 4;
1728 static void monitor_handle_command(const char *cmdline)
1730 const char *p, *pstart, *typestr;
1732 int c, nb_args, len, i, has_arg;
1736 void *str_allocated[MAX_ARGS];
1737 void *args[MAX_ARGS];
1740 term_printf("command='%s'\n", cmdline);
1743 /* extract the command name */
1751 while (*p != '\0' && *p != '/' && !isspace(*p))
1754 if (len > sizeof(cmdname) - 1)
1755 len = sizeof(cmdname) - 1;
1756 memcpy(cmdname, pstart, len);
1757 cmdname[len] = '\0';
1759 /* find the command */
1760 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1761 if (compare_cmd(cmdname, cmd->name))
1764 term_printf("unknown command: '%s'\n", cmdname);
1768 for(i = 0; i < MAX_ARGS; i++)
1769 str_allocated[i] = NULL;
1771 /* parse the parameters */
1772 typestr = cmd->args_type;
1789 if (*typestr == '?') {
1792 /* no optional string: NULL argument */
1797 ret = get_str(buf, sizeof(buf), &p);
1801 term_printf("%s: filename expected\n", cmdname);
1804 term_printf("%s: block device name expected\n", cmdname);
1807 term_printf("%s: string expected\n", cmdname);
1812 str = qemu_malloc(strlen(buf) + 1);
1814 str_allocated[nb_args] = str;
1816 if (nb_args >= MAX_ARGS) {
1818 term_printf("%s: too many arguments\n", cmdname);
1821 args[nb_args++] = str;
1826 int count, format, size;
1836 while (isdigit(*p)) {
1837 count = count * 10 + (*p - '0');
1875 if (*p != '\0' && !isspace(*p)) {
1876 term_printf("invalid char in format: '%c'\n", *p);
1880 format = default_fmt_format;
1881 if (format != 'i') {
1882 /* for 'i', not specifying a size gives -1 as size */
1884 size = default_fmt_size;
1886 default_fmt_size = size;
1887 default_fmt_format = format;
1890 format = default_fmt_format;
1891 if (format != 'i') {
1892 size = default_fmt_size;
1897 if (nb_args + 3 > MAX_ARGS)
1899 args[nb_args++] = (void*)count;
1900 args[nb_args++] = (void*)format;
1901 args[nb_args++] = (void*)size;
1910 if (*typestr == '?' || *typestr == '.') {
1912 if (*typestr == '?') {
1927 if (nb_args >= MAX_ARGS)
1929 args[nb_args++] = (void *)has_arg;
1931 if (nb_args >= MAX_ARGS)
1937 if (get_expr(&val, &p))
1941 if (nb_args >= MAX_ARGS)
1943 args[nb_args++] = (void *)(int)val;
1945 if ((nb_args + 1) >= MAX_ARGS)
1947 #if TARGET_LONG_BITS == 64
1948 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
1950 args[nb_args++] = (void *)0;
1952 args[nb_args++] = (void *)(int)(val & 0xffffffff);
1970 term_printf("%s: unsupported option -%c\n",
1977 if (nb_args >= MAX_ARGS)
1979 args[nb_args++] = (void *)has_option;
1984 term_printf("%s: unknown type '%c'\n", cmdname, c);
1988 /* check that all arguments were parsed */
1992 term_printf("%s: extraneous characters at the end of line\n",
2002 cmd->handler(args[0]);
2005 cmd->handler(args[0], args[1]);
2008 cmd->handler(args[0], args[1], args[2]);
2011 cmd->handler(args[0], args[1], args[2], args[3]);
2014 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2017 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2020 term_printf("unsupported number of arguments: %d\n", nb_args);
2024 for(i = 0; i < MAX_ARGS; i++)
2025 qemu_free(str_allocated[i]);
2029 static void cmd_completion(const char *name, const char *list)
2031 const char *p, *pstart;
2040 p = pstart + strlen(pstart);
2042 if (len > sizeof(cmd) - 2)
2043 len = sizeof(cmd) - 2;
2044 memcpy(cmd, pstart, len);
2046 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2047 add_completion(cmd);
2055 static void file_completion(const char *input)
2060 char file[1024], file_prefix[1024];
2064 p = strrchr(input, '/');
2067 pstrcpy(file_prefix, sizeof(file_prefix), input);
2070 input_path_len = p - input + 1;
2071 memcpy(path, input, input_path_len);
2072 if (input_path_len > sizeof(path) - 1)
2073 input_path_len = sizeof(path) - 1;
2074 path[input_path_len] = '\0';
2075 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2077 #ifdef DEBUG_COMPLETION
2078 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2080 ffs = opendir(path);
2088 if (strstart(d->d_name, file_prefix, NULL)) {
2089 memcpy(file, input, input_path_len);
2090 strcpy(file + input_path_len, d->d_name);
2091 /* stat the file to find out if it's a directory.
2092 * In that case add a slash to speed up typing long paths
2095 if(S_ISDIR(sb.st_mode))
2097 add_completion(file);
2103 static void block_completion_it(void *opaque, const char *name)
2105 const char *input = opaque;
2107 if (input[0] == '\0' ||
2108 !strncmp(name, (char *)input, strlen(input))) {
2109 add_completion(name);
2113 /* NOTE: this parser is an approximate form of the real command parser */
2114 static void parse_cmdline(const char *cmdline,
2115 int *pnb_args, char **args)
2128 if (nb_args >= MAX_ARGS)
2130 ret = get_str(buf, sizeof(buf), &p);
2131 args[nb_args] = qemu_strdup(buf);
2136 *pnb_args = nb_args;
2139 void readline_find_completion(const char *cmdline)
2141 const char *cmdname;
2142 char *args[MAX_ARGS];
2143 int nb_args, i, len;
2144 const char *ptype, *str;
2147 parse_cmdline(cmdline, &nb_args, args);
2148 #ifdef DEBUG_COMPLETION
2149 for(i = 0; i < nb_args; i++) {
2150 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2154 /* if the line ends with a space, it means we want to complete the
2156 len = strlen(cmdline);
2157 if (len > 0 && isspace(cmdline[len - 1])) {
2158 if (nb_args >= MAX_ARGS)
2160 args[nb_args++] = qemu_strdup("");
2163 /* command completion */
2168 completion_index = strlen(cmdname);
2169 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2170 cmd_completion(cmdname, cmd->name);
2173 /* find the command */
2174 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2175 if (compare_cmd(args[0], cmd->name))
2180 ptype = cmd->args_type;
2181 for(i = 0; i < nb_args - 2; i++) {
2182 if (*ptype != '\0') {
2184 while (*ptype == '?')
2188 str = args[nb_args - 1];
2191 /* file completion */
2192 completion_index = strlen(str);
2193 file_completion(str);
2196 /* block device name completion */
2197 completion_index = strlen(str);
2198 bdrv_iterate(block_completion_it, (void *)str);
2201 /* XXX: more generic ? */
2202 if (!strcmp(cmd->name, "info")) {
2203 completion_index = strlen(str);
2204 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2205 cmd_completion(str, cmd->name);
2213 for(i = 0; i < nb_args; i++)
2217 static int term_can_read(void *opaque)
2222 static void term_read(void *opaque, const uint8_t *buf, int size)
2225 for(i = 0; i < size; i++)
2226 readline_handle_byte(buf[i]);
2229 static void monitor_start_input(void);
2231 static void monitor_handle_command1(void *opaque, const char *cmdline)
2233 monitor_handle_command(cmdline);
2234 monitor_start_input();
2237 static void monitor_start_input(void)
2239 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2242 void monitor_init(CharDriverState *hd, int show_banner)
2246 term_printf("QEMU %s monitor - type 'help' for more information\n",
2249 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2250 monitor_start_input();
2253 /* XXX: use threads ? */
2254 /* modal monitor readline */
2255 static int monitor_readline_started;
2256 static char *monitor_readline_buf;
2257 static int monitor_readline_buf_size;
2259 static void monitor_readline_cb(void *opaque, const char *input)
2261 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2262 monitor_readline_started = 0;
2265 void monitor_readline(const char *prompt, int is_password,
2266 char *buf, int buf_size)
2269 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2271 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2272 monitor_readline_buf = buf;
2273 monitor_readline_buf_size = buf_size;
2274 monitor_readline_started = 1;
2275 while (monitor_readline_started) {