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)
86 term_outbuf[term_outbuf_index++] = '\r';
87 term_outbuf[term_outbuf_index++] = c;
88 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
94 void term_vprintf(const char *fmt, va_list ap)
97 vsnprintf(buf, sizeof(buf), fmt, ap);
101 void term_printf(const char *fmt, ...)
105 term_vprintf(fmt, ap);
109 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
113 term_vprintf(fmt, ap);
118 static int compare_cmd(const char *name, const char *list)
120 const char *p, *pstart;
128 p = pstart + strlen(pstart);
129 if ((p - pstart) == len && !memcmp(pstart, name, len))
138 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
142 for(cmd = cmds; cmd->name != NULL; cmd++) {
143 if (!name || !strcmp(name, cmd->name))
144 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
148 static void help_cmd(const char *name)
150 if (name && !strcmp(name, "info")) {
151 help_cmd1(info_cmds, "info ", NULL);
153 help_cmd1(term_cmds, "", name);
154 if (name && !strcmp(name, "log")) {
156 term_printf("Log items (comma separated):\n");
157 term_printf("%-10s %s\n", "none", "remove all logs");
158 for(item = cpu_log_items; item->mask != 0; item++) {
159 term_printf("%-10s %s\n", item->name, item->help);
165 static void do_help(const char *name)
170 static void do_commit(void)
174 for (i = 0; i < MAX_DISKS; i++) {
176 bdrv_commit(bs_table[i]);
181 static void do_info(const char *item)
187 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
188 if (compare_cmd(item, cmd->name))
198 static void do_info_version(void)
200 term_printf("%s\n", QEMU_VERSION);
203 static void do_info_block(void)
208 /* get the current CPU defined by the user */
209 int mon_set_cpu(int cpu_index)
213 for(env = first_cpu; env != NULL; env = env->next_cpu) {
214 if (env->cpu_index == cpu_index) {
222 CPUState *mon_get_cpu(void)
230 static void do_info_registers(void)
237 cpu_dump_state(env, NULL, monitor_fprintf,
240 cpu_dump_state(env, NULL, monitor_fprintf,
245 static void do_info_cpus(void)
249 /* just to set the default cpu if not already done */
252 for(env = first_cpu; env != NULL; env = env->next_cpu) {
253 term_printf("%c CPU #%d:",
254 (env == mon_cpu) ? '*' : ' ',
256 #if defined(TARGET_I386)
257 term_printf(" pc=0x" TARGET_FMT_lx, env->eip + env->segs[R_CS].base);
258 if (env->hflags & HF_HALTED_MASK)
259 term_printf(" (halted)");
260 #elif defined(TARGET_PPC)
261 term_printf(" nip=0x" TARGET_FMT_lx, env->nip);
263 term_printf(" (halted)");
264 #elif defined(TARGET_SPARC)
265 term_printf(" pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx, env->pc, env->npc);
267 term_printf(" (halted)");
273 static void do_cpu_set(int index)
275 if (mon_set_cpu(index) < 0)
276 term_printf("Invalid CPU index\n");
279 static void do_info_jit(void)
281 dump_exec_info(NULL, monitor_fprintf);
284 static void do_info_history (void)
291 str = readline_get_history(i);
294 term_printf("%d: '%s'\n", i, str);
299 static void do_quit(void)
304 static int eject_device(BlockDriverState *bs, int force)
306 if (bdrv_is_inserted(bs)) {
308 if (!bdrv_is_removable(bs)) {
309 term_printf("device is not removable\n");
312 if (bdrv_is_locked(bs)) {
313 term_printf("device is locked\n");
322 static void do_eject(int force, const char *filename)
324 BlockDriverState *bs;
326 bs = bdrv_find(filename);
328 term_printf("device not found\n");
331 eject_device(bs, force);
334 static void do_change(const char *device, const char *filename)
336 BlockDriverState *bs;
340 bs = bdrv_find(device);
342 term_printf("device not found\n");
345 if (eject_device(bs, 0) < 0)
347 bdrv_open(bs, filename, 0);
348 if (bdrv_is_encrypted(bs)) {
349 term_printf("%s is encrypted.\n", device);
350 for(i = 0; i < 3; i++) {
351 monitor_readline("Password: ", 1, password, sizeof(password));
352 if (bdrv_set_key(bs, password) == 0)
354 term_printf("invalid password\n");
359 static void do_screen_dump(const char *filename)
361 vga_hw_screen_dump(filename);
364 static void do_log(const char *items)
368 if (!strcmp(items, "none")) {
371 mask = cpu_str_to_log_mask(items);
380 static void do_savevm(const char *filename)
382 if (qemu_savevm(filename) < 0)
383 term_printf("I/O error when saving VM to '%s'\n", filename);
386 static void do_loadvm(const char *filename)
388 if (qemu_loadvm(filename) < 0)
389 term_printf("I/O error when loading VM from '%s'\n", filename);
392 static void do_stop(void)
394 vm_stop(EXCP_INTERRUPT);
397 static void do_cont(void)
402 #ifdef CONFIG_GDBSTUB
403 static void do_gdbserver(int has_port, int port)
406 port = DEFAULT_GDBSTUB_PORT;
407 if (gdbserver_start(port) < 0) {
408 qemu_printf("Could not open gdbserver socket on port %d\n", port);
410 qemu_printf("Waiting gdb connection on port %d\n", port);
415 static void term_printc(int c)
432 if (c >= 32 && c <= 126) {
433 term_printf("%c", c);
435 term_printf("\\x%02x", c);
442 static void memory_dump(int count, int format, int wsize,
443 target_ulong addr, int is_physical)
446 int nb_per_line, l, line_size, i, max_digits, len;
454 if (!env && !is_physical)
459 } else if (wsize == 4) {
462 /* as default we use the current CS size */
466 if ((env->efer & MSR_EFER_LMA) &&
467 (env->segs[R_CS].flags & DESC_L_MASK))
471 if (!(env->segs[R_CS].flags & DESC_B_MASK))
476 monitor_disas(env, addr, count, is_physical, flags);
485 nb_per_line = line_size / wsize;
490 max_digits = (wsize * 8 + 2) / 3;
494 max_digits = (wsize * 8) / 4;
498 max_digits = (wsize * 8 * 10 + 32) / 33;
506 term_printf(TARGET_FMT_lx ":", addr);
511 cpu_physical_memory_rw(addr, buf, l, 0);
516 cpu_memory_rw_debug(env, addr, buf, l, 0);
523 v = ldub_raw(buf + i);
526 v = lduw_raw(buf + i);
529 v = (uint32_t)ldl_raw(buf + i);
532 v = ldq_raw(buf + i);
538 term_printf("%#*" PRIo64, max_digits, v);
541 term_printf("0x%0*" PRIx64, max_digits, v);
544 term_printf("%*" PRIu64, max_digits, v);
547 term_printf("%*" PRId64, max_digits, v);
561 #if TARGET_LONG_BITS == 64
562 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
564 #define GET_TLONG(h, l) (l)
567 static void do_memory_dump(int count, int format, int size,
568 uint32_t addrh, uint32_t addrl)
570 target_long addr = GET_TLONG(addrh, addrl);
571 memory_dump(count, format, size, addr, 0);
574 static void do_physical_memory_dump(int count, int format, int size,
575 uint32_t addrh, uint32_t addrl)
578 target_long addr = GET_TLONG(addrh, addrl);
579 memory_dump(count, format, size, addr, 1);
582 static void do_print(int count, int format, int size, unsigned int valh, unsigned int vall)
584 target_long val = GET_TLONG(valh, vall);
585 #if TARGET_LONG_BITS == 32
588 term_printf("%#o", val);
591 term_printf("%#x", val);
594 term_printf("%u", val);
598 term_printf("%d", val);
607 term_printf("%#" PRIo64, val);
610 term_printf("%#" PRIx64, val);
613 term_printf("%" PRIu64, val);
617 term_printf("%" PRId64, val);
627 static void do_sum(uint32_t start, uint32_t size)
634 for(addr = start; addr < (start + size); addr++) {
635 cpu_physical_memory_rw(addr, buf, 1, 0);
636 /* BSD sum algorithm ('sum' Unix command) */
637 sum = (sum >> 1) | (sum << 15);
640 term_printf("%05d\n", sum);
648 static const KeyDef key_defs[] = {
673 { 0x0e, "backspace" },
708 { 0x3a, "caps_lock" },
719 { 0x45, "num_lock" },
720 { 0x46, "scroll_lock" },
722 { 0xb5, "kp_divide" },
723 { 0x37, "kp_multiply" },
724 { 0x4a, "kp_substract" },
726 { 0x9c, "kp_enter" },
727 { 0x53, "kp_decimal" },
762 static int get_keycode(const char *key)
768 for(p = key_defs; p->name != NULL; p++) {
769 if (!strcmp(key, p->name))
772 if (strstart(key, "0x", NULL)) {
773 ret = strtoul(key, &endp, 0);
774 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
780 static void do_send_key(const char *string)
783 uint8_t keycodes[16];
785 int nb_keycodes, keycode, i;
791 while (*p != '\0' && *p != '-') {
792 if ((q - keybuf) < sizeof(keybuf) - 1) {
798 keycode = get_keycode(keybuf);
800 term_printf("unknown key: '%s'\n", keybuf);
803 keycodes[nb_keycodes++] = keycode;
808 /* key down events */
809 for(i = 0; i < nb_keycodes; i++) {
810 keycode = keycodes[i];
812 kbd_put_keycode(0xe0);
813 kbd_put_keycode(keycode & 0x7f);
816 for(i = nb_keycodes - 1; i >= 0; i--) {
817 keycode = keycodes[i];
819 kbd_put_keycode(0xe0);
820 kbd_put_keycode(keycode | 0x80);
824 static int mouse_button_state;
826 static void do_mouse_move(const char *dx_str, const char *dy_str,
830 dx = strtol(dx_str, NULL, 0);
831 dy = strtol(dy_str, NULL, 0);
834 dz = strtol(dz_str, NULL, 0);
835 kbd_mouse_event(dx, dy, dz, mouse_button_state);
838 static void do_mouse_button(int button_state)
840 mouse_button_state = button_state;
841 kbd_mouse_event(0, 0, 0, mouse_button_state);
844 static void do_ioport_read(int count, int format, int size, int addr, int has_index, int index)
850 cpu_outb(NULL, addr & 0xffff, index & 0xff);
858 val = cpu_inb(NULL, addr);
862 val = cpu_inw(NULL, addr);
866 val = cpu_inl(NULL, addr);
870 term_printf("port%c[0x%04x] = %#0*x\n",
871 suffix, addr, size * 2, val);
874 static void do_system_reset(void)
876 qemu_system_reset_request();
879 static void do_system_powerdown(void)
881 qemu_system_powerdown_request();
884 #if defined(TARGET_I386)
885 static void print_pte(uint32_t addr, uint32_t pte, uint32_t mask)
887 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
890 pte & PG_GLOBAL_MASK ? 'G' : '-',
891 pte & PG_PSE_MASK ? 'P' : '-',
892 pte & PG_DIRTY_MASK ? 'D' : '-',
893 pte & PG_ACCESSED_MASK ? 'A' : '-',
894 pte & PG_PCD_MASK ? 'C' : '-',
895 pte & PG_PWT_MASK ? 'T' : '-',
896 pte & PG_USER_MASK ? 'U' : '-',
897 pte & PG_RW_MASK ? 'W' : '-');
900 static void tlb_info(void)
904 uint32_t pgd, pde, pte;
910 if (!(env->cr[0] & CR0_PG_MASK)) {
911 term_printf("PG disabled\n");
914 pgd = env->cr[3] & ~0xfff;
915 for(l1 = 0; l1 < 1024; l1++) {
916 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
917 pde = le32_to_cpu(pde);
918 if (pde & PG_PRESENT_MASK) {
919 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
920 print_pte((l1 << 22), pde, ~((1 << 20) - 1));
922 for(l2 = 0; l2 < 1024; l2++) {
923 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
925 pte = le32_to_cpu(pte);
926 if (pte & PG_PRESENT_MASK) {
927 print_pte((l1 << 22) + (l2 << 12),
937 static void mem_print(uint32_t *pstart, int *plast_prot,
938 uint32_t end, int prot)
944 term_printf("%08x-%08x %08x %c%c%c\n",
945 *pstart, end, end - *pstart,
946 prot1 & PG_USER_MASK ? 'u' : '-',
948 prot1 & PG_RW_MASK ? 'w' : '-');
958 static void mem_info(void)
961 int l1, l2, prot, last_prot;
962 uint32_t pgd, pde, pte, start, end;
968 if (!(env->cr[0] & CR0_PG_MASK)) {
969 term_printf("PG disabled\n");
972 pgd = env->cr[3] & ~0xfff;
975 for(l1 = 0; l1 < 1024; l1++) {
976 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
977 pde = le32_to_cpu(pde);
979 if (pde & PG_PRESENT_MASK) {
980 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
981 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
982 mem_print(&start, &last_prot, end, prot);
984 for(l2 = 0; l2 < 1024; l2++) {
985 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
987 pte = le32_to_cpu(pte);
988 end = (l1 << 22) + (l2 << 12);
989 if (pte & PG_PRESENT_MASK) {
990 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
994 mem_print(&start, &last_prot, end, prot);
999 mem_print(&start, &last_prot, end, prot);
1005 static void do_info_kqemu(void)
1011 env = mon_get_cpu();
1013 term_printf("No cpu initialized yet");
1016 val = env->kqemu_enabled;
1017 term_printf("kqemu support: ");
1021 term_printf("disabled\n");
1024 term_printf("enabled for user code\n");
1027 term_printf("enabled for user and kernel code\n");
1031 term_printf("kqemu support: not compiled\n");
1035 #ifdef CONFIG_PROFILER
1039 int64_t kqemu_exec_count;
1041 int64_t kqemu_ret_int_count;
1042 int64_t kqemu_ret_excp_count;
1043 int64_t kqemu_ret_intr_count;
1045 static void do_info_profile(void)
1051 term_printf("async time %" PRId64 " (%0.3f)\n",
1052 dev_time, dev_time / (double)ticks_per_sec);
1053 term_printf("qemu time %" PRId64 " (%0.3f)\n",
1054 qemu_time, qemu_time / (double)ticks_per_sec);
1055 term_printf("kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%" PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%" PRId64 "\n",
1056 kqemu_time, kqemu_time / (double)ticks_per_sec,
1057 kqemu_time / (double)total * 100.0,
1059 kqemu_ret_int_count,
1060 kqemu_ret_excp_count,
1061 kqemu_ret_intr_count);
1064 kqemu_exec_count = 0;
1066 kqemu_ret_int_count = 0;
1067 kqemu_ret_excp_count = 0;
1068 kqemu_ret_intr_count = 0;
1070 kqemu_record_dump();
1074 static void do_info_profile(void)
1076 term_printf("Internal profiler not compiled\n");
1080 static term_cmd_t term_cmds[] = {
1081 { "help|?", "s?", do_help,
1082 "[cmd]", "show the help" },
1083 { "commit", "", do_commit,
1084 "", "commit changes to the disk images (if -snapshot is used)" },
1085 { "info", "s?", do_info,
1086 "subcommand", "show various information about the system state" },
1087 { "q|quit", "", do_quit,
1088 "", "quit the emulator" },
1089 { "eject", "-fB", do_eject,
1090 "[-f] device", "eject a removable media (use -f to force it)" },
1091 { "change", "BF", do_change,
1092 "device filename", "change a removable media" },
1093 { "screendump", "F", do_screen_dump,
1094 "filename", "save screen into PPM image 'filename'" },
1095 { "log", "s", do_log,
1096 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1097 { "savevm", "F", do_savevm,
1098 "filename", "save the whole virtual machine state to 'filename'" },
1099 { "loadvm", "F", do_loadvm,
1100 "filename", "restore the whole virtual machine state from 'filename'" },
1101 { "stop", "", do_stop,
1102 "", "stop emulation", },
1103 { "c|cont", "", do_cont,
1104 "", "resume emulation", },
1105 #ifdef CONFIG_GDBSTUB
1106 { "gdbserver", "i?", do_gdbserver,
1107 "[port]", "start gdbserver session (default port=1234)", },
1109 { "x", "/l", do_memory_dump,
1110 "/fmt addr", "virtual memory dump starting at 'addr'", },
1111 { "xp", "/l", do_physical_memory_dump,
1112 "/fmt addr", "physical memory dump starting at 'addr'", },
1113 { "p|print", "/l", do_print,
1114 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1115 { "i", "/ii.", do_ioport_read,
1116 "/fmt addr", "I/O port read" },
1118 { "sendkey", "s", do_send_key,
1119 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1120 { "system_reset", "", do_system_reset,
1121 "", "reset the system" },
1122 { "system_powerdown", "", do_system_powerdown,
1123 "", "send system power down event" },
1124 { "sum", "ii", do_sum,
1125 "addr size", "compute the checksum of a memory region" },
1126 { "usb_add", "s", do_usb_add,
1127 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1128 { "usb_del", "s", do_usb_del,
1129 "device", "remove USB device 'bus.addr'" },
1130 { "cpu", "i", do_cpu_set,
1131 "index", "set the default CPU" },
1132 { "mouse_move", "sss?", do_mouse_move,
1133 "dx dy [dz]", "send mouse move events" },
1134 { "mouse_button", "i", do_mouse_button,
1135 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1139 static term_cmd_t info_cmds[] = {
1140 { "version", "", do_info_version,
1141 "", "show the version of qemu" },
1142 { "network", "", do_info_network,
1143 "", "show the network state" },
1144 { "block", "", do_info_block,
1145 "", "show the block devices" },
1146 { "registers", "", do_info_registers,
1147 "", "show the cpu registers" },
1148 { "cpus", "", do_info_cpus,
1149 "", "show infos for each CPU" },
1150 { "history", "", do_info_history,
1151 "", "show the command line history", },
1152 { "irq", "", irq_info,
1153 "", "show the interrupts statistics (if available)", },
1154 { "pic", "", pic_info,
1155 "", "show i8259 (PIC) state", },
1156 { "pci", "", pci_info,
1157 "", "show PCI info", },
1158 #if defined(TARGET_I386)
1159 { "tlb", "", tlb_info,
1160 "", "show virtual to physical memory mappings", },
1161 { "mem", "", mem_info,
1162 "", "show the active virtual memory mappings", },
1164 { "jit", "", do_info_jit,
1165 "", "show dynamic compiler info", },
1166 { "kqemu", "", do_info_kqemu,
1167 "", "show kqemu information", },
1168 { "usb", "", usb_info,
1169 "", "show guest USB devices", },
1170 { "usbhost", "", usb_host_info,
1171 "", "show host USB devices", },
1172 { "profile", "", do_info_profile,
1173 "", "show profiling information", },
1177 /*******************************************************************/
1179 static const char *pch;
1180 static jmp_buf expr_env;
1185 typedef struct MonitorDef {
1188 target_long (*get_value)(struct MonitorDef *md, int val);
1192 #if defined(TARGET_I386)
1193 static target_long monitor_get_pc (struct MonitorDef *md, int val)
1195 CPUState *env = mon_get_cpu();
1198 return env->eip + env->segs[R_CS].base;
1202 #if defined(TARGET_PPC)
1203 static target_long monitor_get_ccr (struct MonitorDef *md, int val)
1205 CPUState *env = mon_get_cpu();
1213 for (i = 0; i < 8; i++)
1214 u |= env->crf[i] << (32 - (4 * i));
1219 static target_long monitor_get_msr (struct MonitorDef *md, int val)
1221 CPUState *env = mon_get_cpu();
1224 return (env->msr[MSR_POW] << MSR_POW) |
1225 (env->msr[MSR_ILE] << MSR_ILE) |
1226 (env->msr[MSR_EE] << MSR_EE) |
1227 (env->msr[MSR_PR] << MSR_PR) |
1228 (env->msr[MSR_FP] << MSR_FP) |
1229 (env->msr[MSR_ME] << MSR_ME) |
1230 (env->msr[MSR_FE0] << MSR_FE0) |
1231 (env->msr[MSR_SE] << MSR_SE) |
1232 (env->msr[MSR_BE] << MSR_BE) |
1233 (env->msr[MSR_FE1] << MSR_FE1) |
1234 (env->msr[MSR_IP] << MSR_IP) |
1235 (env->msr[MSR_IR] << MSR_IR) |
1236 (env->msr[MSR_DR] << MSR_DR) |
1237 (env->msr[MSR_RI] << MSR_RI) |
1238 (env->msr[MSR_LE] << MSR_LE);
1241 static target_long monitor_get_xer (struct MonitorDef *md, int val)
1243 CPUState *env = mon_get_cpu();
1246 return (env->xer[XER_SO] << XER_SO) |
1247 (env->xer[XER_OV] << XER_OV) |
1248 (env->xer[XER_CA] << XER_CA) |
1249 (env->xer[XER_BC] << XER_BC);
1252 static target_long monitor_get_decr (struct MonitorDef *md, int val)
1254 CPUState *env = mon_get_cpu();
1257 return cpu_ppc_load_decr(env);
1260 static target_long monitor_get_tbu (struct MonitorDef *md, int val)
1262 CPUState *env = mon_get_cpu();
1265 return cpu_ppc_load_tbu(env);
1268 static target_long monitor_get_tbl (struct MonitorDef *md, int val)
1270 CPUState *env = mon_get_cpu();
1273 return cpu_ppc_load_tbl(env);
1277 #if defined(TARGET_SPARC)
1278 #ifndef TARGET_SPARC64
1279 static target_long monitor_get_psr (struct MonitorDef *md, int val)
1281 CPUState *env = mon_get_cpu();
1284 return GET_PSR(env);
1288 static target_long monitor_get_reg(struct MonitorDef *md, int val)
1290 CPUState *env = mon_get_cpu();
1293 return env->regwptr[val];
1297 static MonitorDef monitor_defs[] = {
1300 #define SEG(name, seg) \
1301 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1302 { name ".base", offsetof(CPUState, segs[seg].base) },\
1303 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1305 { "eax", offsetof(CPUState, regs[0]) },
1306 { "ecx", offsetof(CPUState, regs[1]) },
1307 { "edx", offsetof(CPUState, regs[2]) },
1308 { "ebx", offsetof(CPUState, regs[3]) },
1309 { "esp|sp", offsetof(CPUState, regs[4]) },
1310 { "ebp|fp", offsetof(CPUState, regs[5]) },
1311 { "esi", offsetof(CPUState, regs[6]) },
1312 { "edi", offsetof(CPUState, regs[7]) },
1313 #ifdef TARGET_X86_64
1314 { "r8", offsetof(CPUState, regs[8]) },
1315 { "r9", offsetof(CPUState, regs[9]) },
1316 { "r10", offsetof(CPUState, regs[10]) },
1317 { "r11", offsetof(CPUState, regs[11]) },
1318 { "r12", offsetof(CPUState, regs[12]) },
1319 { "r13", offsetof(CPUState, regs[13]) },
1320 { "r14", offsetof(CPUState, regs[14]) },
1321 { "r15", offsetof(CPUState, regs[15]) },
1323 { "eflags", offsetof(CPUState, eflags) },
1324 { "eip", offsetof(CPUState, eip) },
1331 { "pc", 0, monitor_get_pc, },
1332 #elif defined(TARGET_PPC)
1333 { "r0", offsetof(CPUState, gpr[0]) },
1334 { "r1", offsetof(CPUState, gpr[1]) },
1335 { "r2", offsetof(CPUState, gpr[2]) },
1336 { "r3", offsetof(CPUState, gpr[3]) },
1337 { "r4", offsetof(CPUState, gpr[4]) },
1338 { "r5", offsetof(CPUState, gpr[5]) },
1339 { "r6", offsetof(CPUState, gpr[6]) },
1340 { "r7", offsetof(CPUState, gpr[7]) },
1341 { "r8", offsetof(CPUState, gpr[8]) },
1342 { "r9", offsetof(CPUState, gpr[9]) },
1343 { "r10", offsetof(CPUState, gpr[10]) },
1344 { "r11", offsetof(CPUState, gpr[11]) },
1345 { "r12", offsetof(CPUState, gpr[12]) },
1346 { "r13", offsetof(CPUState, gpr[13]) },
1347 { "r14", offsetof(CPUState, gpr[14]) },
1348 { "r15", offsetof(CPUState, gpr[15]) },
1349 { "r16", offsetof(CPUState, gpr[16]) },
1350 { "r17", offsetof(CPUState, gpr[17]) },
1351 { "r18", offsetof(CPUState, gpr[18]) },
1352 { "r19", offsetof(CPUState, gpr[19]) },
1353 { "r20", offsetof(CPUState, gpr[20]) },
1354 { "r21", offsetof(CPUState, gpr[21]) },
1355 { "r22", offsetof(CPUState, gpr[22]) },
1356 { "r23", offsetof(CPUState, gpr[23]) },
1357 { "r24", offsetof(CPUState, gpr[24]) },
1358 { "r25", offsetof(CPUState, gpr[25]) },
1359 { "r26", offsetof(CPUState, gpr[26]) },
1360 { "r27", offsetof(CPUState, gpr[27]) },
1361 { "r28", offsetof(CPUState, gpr[28]) },
1362 { "r29", offsetof(CPUState, gpr[29]) },
1363 { "r30", offsetof(CPUState, gpr[30]) },
1364 { "r31", offsetof(CPUState, gpr[31]) },
1365 { "nip|pc", offsetof(CPUState, nip) },
1366 { "lr", offsetof(CPUState, lr) },
1367 { "ctr", offsetof(CPUState, ctr) },
1368 { "decr", 0, &monitor_get_decr, },
1369 { "ccr", 0, &monitor_get_ccr, },
1370 { "msr", 0, &monitor_get_msr, },
1371 { "xer", 0, &monitor_get_xer, },
1372 { "tbu", 0, &monitor_get_tbu, },
1373 { "tbl", 0, &monitor_get_tbl, },
1374 { "sdr1", offsetof(CPUState, sdr1) },
1375 { "sr0", offsetof(CPUState, sr[0]) },
1376 { "sr1", offsetof(CPUState, sr[1]) },
1377 { "sr2", offsetof(CPUState, sr[2]) },
1378 { "sr3", offsetof(CPUState, sr[3]) },
1379 { "sr4", offsetof(CPUState, sr[4]) },
1380 { "sr5", offsetof(CPUState, sr[5]) },
1381 { "sr6", offsetof(CPUState, sr[6]) },
1382 { "sr7", offsetof(CPUState, sr[7]) },
1383 { "sr8", offsetof(CPUState, sr[8]) },
1384 { "sr9", offsetof(CPUState, sr[9]) },
1385 { "sr10", offsetof(CPUState, sr[10]) },
1386 { "sr11", offsetof(CPUState, sr[11]) },
1387 { "sr12", offsetof(CPUState, sr[12]) },
1388 { "sr13", offsetof(CPUState, sr[13]) },
1389 { "sr14", offsetof(CPUState, sr[14]) },
1390 { "sr15", offsetof(CPUState, sr[15]) },
1391 /* Too lazy to put BATs and SPRs ... */
1392 #elif defined(TARGET_SPARC)
1393 { "g0", offsetof(CPUState, gregs[0]) },
1394 { "g1", offsetof(CPUState, gregs[1]) },
1395 { "g2", offsetof(CPUState, gregs[2]) },
1396 { "g3", offsetof(CPUState, gregs[3]) },
1397 { "g4", offsetof(CPUState, gregs[4]) },
1398 { "g5", offsetof(CPUState, gregs[5]) },
1399 { "g6", offsetof(CPUState, gregs[6]) },
1400 { "g7", offsetof(CPUState, gregs[7]) },
1401 { "o0", 0, monitor_get_reg },
1402 { "o1", 1, monitor_get_reg },
1403 { "o2", 2, monitor_get_reg },
1404 { "o3", 3, monitor_get_reg },
1405 { "o4", 4, monitor_get_reg },
1406 { "o5", 5, monitor_get_reg },
1407 { "o6", 6, monitor_get_reg },
1408 { "o7", 7, monitor_get_reg },
1409 { "l0", 8, monitor_get_reg },
1410 { "l1", 9, monitor_get_reg },
1411 { "l2", 10, monitor_get_reg },
1412 { "l3", 11, monitor_get_reg },
1413 { "l4", 12, monitor_get_reg },
1414 { "l5", 13, monitor_get_reg },
1415 { "l6", 14, monitor_get_reg },
1416 { "l7", 15, monitor_get_reg },
1417 { "i0", 16, monitor_get_reg },
1418 { "i1", 17, monitor_get_reg },
1419 { "i2", 18, monitor_get_reg },
1420 { "i3", 19, monitor_get_reg },
1421 { "i4", 20, monitor_get_reg },
1422 { "i5", 21, monitor_get_reg },
1423 { "i6", 22, monitor_get_reg },
1424 { "i7", 23, monitor_get_reg },
1425 { "pc", offsetof(CPUState, pc) },
1426 { "npc", offsetof(CPUState, npc) },
1427 { "y", offsetof(CPUState, y) },
1428 #ifndef TARGET_SPARC64
1429 { "psr", 0, &monitor_get_psr, },
1430 { "wim", offsetof(CPUState, wim) },
1432 { "tbr", offsetof(CPUState, tbr) },
1433 { "fsr", offsetof(CPUState, fsr) },
1434 { "f0", offsetof(CPUState, fpr[0]) },
1435 { "f1", offsetof(CPUState, fpr[1]) },
1436 { "f2", offsetof(CPUState, fpr[2]) },
1437 { "f3", offsetof(CPUState, fpr[3]) },
1438 { "f4", offsetof(CPUState, fpr[4]) },
1439 { "f5", offsetof(CPUState, fpr[5]) },
1440 { "f6", offsetof(CPUState, fpr[6]) },
1441 { "f7", offsetof(CPUState, fpr[7]) },
1442 { "f8", offsetof(CPUState, fpr[8]) },
1443 { "f9", offsetof(CPUState, fpr[9]) },
1444 { "f10", offsetof(CPUState, fpr[10]) },
1445 { "f11", offsetof(CPUState, fpr[11]) },
1446 { "f12", offsetof(CPUState, fpr[12]) },
1447 { "f13", offsetof(CPUState, fpr[13]) },
1448 { "f14", offsetof(CPUState, fpr[14]) },
1449 { "f15", offsetof(CPUState, fpr[15]) },
1450 { "f16", offsetof(CPUState, fpr[16]) },
1451 { "f17", offsetof(CPUState, fpr[17]) },
1452 { "f18", offsetof(CPUState, fpr[18]) },
1453 { "f19", offsetof(CPUState, fpr[19]) },
1454 { "f20", offsetof(CPUState, fpr[20]) },
1455 { "f21", offsetof(CPUState, fpr[21]) },
1456 { "f22", offsetof(CPUState, fpr[22]) },
1457 { "f23", offsetof(CPUState, fpr[23]) },
1458 { "f24", offsetof(CPUState, fpr[24]) },
1459 { "f25", offsetof(CPUState, fpr[25]) },
1460 { "f26", offsetof(CPUState, fpr[26]) },
1461 { "f27", offsetof(CPUState, fpr[27]) },
1462 { "f28", offsetof(CPUState, fpr[28]) },
1463 { "f29", offsetof(CPUState, fpr[29]) },
1464 { "f30", offsetof(CPUState, fpr[30]) },
1465 { "f31", offsetof(CPUState, fpr[31]) },
1466 #ifdef TARGET_SPARC64
1467 { "f32", offsetof(CPUState, fpr[32]) },
1468 { "f34", offsetof(CPUState, fpr[34]) },
1469 { "f36", offsetof(CPUState, fpr[36]) },
1470 { "f38", offsetof(CPUState, fpr[38]) },
1471 { "f40", offsetof(CPUState, fpr[40]) },
1472 { "f42", offsetof(CPUState, fpr[42]) },
1473 { "f44", offsetof(CPUState, fpr[44]) },
1474 { "f46", offsetof(CPUState, fpr[46]) },
1475 { "f48", offsetof(CPUState, fpr[48]) },
1476 { "f50", offsetof(CPUState, fpr[50]) },
1477 { "f52", offsetof(CPUState, fpr[52]) },
1478 { "f54", offsetof(CPUState, fpr[54]) },
1479 { "f56", offsetof(CPUState, fpr[56]) },
1480 { "f58", offsetof(CPUState, fpr[58]) },
1481 { "f60", offsetof(CPUState, fpr[60]) },
1482 { "f62", offsetof(CPUState, fpr[62]) },
1483 { "asi", offsetof(CPUState, asi) },
1484 { "pstate", offsetof(CPUState, pstate) },
1485 { "cansave", offsetof(CPUState, cansave) },
1486 { "canrestore", offsetof(CPUState, canrestore) },
1487 { "otherwin", offsetof(CPUState, otherwin) },
1488 { "wstate", offsetof(CPUState, wstate) },
1489 { "cleanwin", offsetof(CPUState, cleanwin) },
1490 { "fprs", offsetof(CPUState, fprs) },
1496 static void expr_error(const char *fmt)
1500 longjmp(expr_env, 1);
1503 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1504 static int get_monitor_def(target_long *pval, const char *name)
1509 for(md = monitor_defs; md->name != NULL; md++) {
1510 if (compare_cmd(name, md->name)) {
1511 if (md->get_value) {
1512 *pval = md->get_value(md, md->offset);
1514 CPUState *env = mon_get_cpu();
1517 ptr = (uint8_t *)env + md->offset;
1520 *pval = *(int32_t *)ptr;
1523 *pval = *(target_long *)ptr;
1536 static void next(void)
1540 while (isspace(*pch))
1545 static target_long expr_sum(void);
1547 static target_long expr_unary(void)
1570 expr_error("')' expected");
1577 expr_error("character constant expected");
1581 expr_error("missing terminating \' character");
1590 while ((*pch >= 'a' && *pch <= 'z') ||
1591 (*pch >= 'A' && *pch <= 'Z') ||
1592 (*pch >= '0' && *pch <= '9') ||
1593 *pch == '_' || *pch == '.') {
1594 if ((q - buf) < sizeof(buf) - 1)
1598 while (isspace(*pch))
1601 ret = get_monitor_def(&n, buf);
1603 expr_error("unknown register");
1605 expr_error("no cpu defined");
1609 expr_error("unexpected end of expression");
1613 #if TARGET_LONG_BITS == 64
1614 n = strtoull(pch, &p, 0);
1616 n = strtoul(pch, &p, 0);
1619 expr_error("invalid char in expression");
1622 while (isspace(*pch))
1630 static target_long expr_prod(void)
1632 target_long val, val2;
1638 if (op != '*' && op != '/' && op != '%')
1641 val2 = expr_unary();
1650 expr_error("division by zero");
1661 static target_long expr_logic(void)
1663 target_long val, val2;
1669 if (op != '&' && op != '|' && op != '^')
1689 static target_long expr_sum(void)
1691 target_long val, val2;
1697 if (op != '+' && op != '-')
1700 val2 = expr_logic();
1709 static int get_expr(target_long *pval, const char **pp)
1712 if (setjmp(expr_env)) {
1716 while (isspace(*pch))
1723 static int get_str(char *buf, int buf_size, const char **pp)
1741 while (*p != '\0' && *p != '\"') {
1757 qemu_printf("unsupported escape code: '\\%c'\n", c);
1760 if ((q - buf) < buf_size - 1) {
1764 if ((q - buf) < buf_size - 1) {
1771 qemu_printf("unterminated string\n");
1776 while (*p != '\0' && !isspace(*p)) {
1777 if ((q - buf) < buf_size - 1) {
1788 static int default_fmt_format = 'x';
1789 static int default_fmt_size = 4;
1793 static void monitor_handle_command(const char *cmdline)
1795 const char *p, *pstart, *typestr;
1797 int c, nb_args, len, i, has_arg;
1801 void *str_allocated[MAX_ARGS];
1802 void *args[MAX_ARGS];
1805 term_printf("command='%s'\n", cmdline);
1808 /* extract the command name */
1816 while (*p != '\0' && *p != '/' && !isspace(*p))
1819 if (len > sizeof(cmdname) - 1)
1820 len = sizeof(cmdname) - 1;
1821 memcpy(cmdname, pstart, len);
1822 cmdname[len] = '\0';
1824 /* find the command */
1825 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1826 if (compare_cmd(cmdname, cmd->name))
1829 term_printf("unknown command: '%s'\n", cmdname);
1833 for(i = 0; i < MAX_ARGS; i++)
1834 str_allocated[i] = NULL;
1836 /* parse the parameters */
1837 typestr = cmd->args_type;
1854 if (*typestr == '?') {
1857 /* no optional string: NULL argument */
1862 ret = get_str(buf, sizeof(buf), &p);
1866 term_printf("%s: filename expected\n", cmdname);
1869 term_printf("%s: block device name expected\n", cmdname);
1872 term_printf("%s: string expected\n", cmdname);
1877 str = qemu_malloc(strlen(buf) + 1);
1879 str_allocated[nb_args] = str;
1881 if (nb_args >= MAX_ARGS) {
1883 term_printf("%s: too many arguments\n", cmdname);
1886 args[nb_args++] = str;
1891 int count, format, size;
1901 while (isdigit(*p)) {
1902 count = count * 10 + (*p - '0');
1940 if (*p != '\0' && !isspace(*p)) {
1941 term_printf("invalid char in format: '%c'\n", *p);
1945 format = default_fmt_format;
1946 if (format != 'i') {
1947 /* for 'i', not specifying a size gives -1 as size */
1949 size = default_fmt_size;
1951 default_fmt_size = size;
1952 default_fmt_format = format;
1955 format = default_fmt_format;
1956 if (format != 'i') {
1957 size = default_fmt_size;
1962 if (nb_args + 3 > MAX_ARGS)
1964 args[nb_args++] = (void*)count;
1965 args[nb_args++] = (void*)format;
1966 args[nb_args++] = (void*)size;
1975 if (*typestr == '?' || *typestr == '.') {
1976 if (*typestr == '?') {
1992 if (nb_args >= MAX_ARGS)
1994 args[nb_args++] = (void *)has_arg;
1996 if (nb_args >= MAX_ARGS)
2002 if (get_expr(&val, &p))
2006 if (nb_args >= MAX_ARGS)
2008 args[nb_args++] = (void *)(int)val;
2010 if ((nb_args + 1) >= MAX_ARGS)
2012 #if TARGET_LONG_BITS == 64
2013 args[nb_args++] = (void *)(int)((val >> 32) & 0xffffffff);
2015 args[nb_args++] = (void *)0;
2017 args[nb_args++] = (void *)(int)(val & 0xffffffff);
2035 term_printf("%s: unsupported option -%c\n",
2042 if (nb_args >= MAX_ARGS)
2044 args[nb_args++] = (void *)has_option;
2049 term_printf("%s: unknown type '%c'\n", cmdname, c);
2053 /* check that all arguments were parsed */
2057 term_printf("%s: extraneous characters at the end of line\n",
2067 cmd->handler(args[0]);
2070 cmd->handler(args[0], args[1]);
2073 cmd->handler(args[0], args[1], args[2]);
2076 cmd->handler(args[0], args[1], args[2], args[3]);
2079 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
2082 cmd->handler(args[0], args[1], args[2], args[3], args[4], args[5]);
2085 term_printf("unsupported number of arguments: %d\n", nb_args);
2089 for(i = 0; i < MAX_ARGS; i++)
2090 qemu_free(str_allocated[i]);
2094 static void cmd_completion(const char *name, const char *list)
2096 const char *p, *pstart;
2105 p = pstart + strlen(pstart);
2107 if (len > sizeof(cmd) - 2)
2108 len = sizeof(cmd) - 2;
2109 memcpy(cmd, pstart, len);
2111 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2112 add_completion(cmd);
2120 static void file_completion(const char *input)
2125 char file[1024], file_prefix[1024];
2129 p = strrchr(input, '/');
2132 pstrcpy(file_prefix, sizeof(file_prefix), input);
2135 input_path_len = p - input + 1;
2136 memcpy(path, input, input_path_len);
2137 if (input_path_len > sizeof(path) - 1)
2138 input_path_len = sizeof(path) - 1;
2139 path[input_path_len] = '\0';
2140 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2142 #ifdef DEBUG_COMPLETION
2143 term_printf("input='%s' path='%s' prefix='%s'\n", input, path, file_prefix);
2145 ffs = opendir(path);
2153 if (strstart(d->d_name, file_prefix, NULL)) {
2154 memcpy(file, input, input_path_len);
2155 strcpy(file + input_path_len, d->d_name);
2156 /* stat the file to find out if it's a directory.
2157 * In that case add a slash to speed up typing long paths
2160 if(S_ISDIR(sb.st_mode))
2162 add_completion(file);
2168 static void block_completion_it(void *opaque, const char *name)
2170 const char *input = opaque;
2172 if (input[0] == '\0' ||
2173 !strncmp(name, (char *)input, strlen(input))) {
2174 add_completion(name);
2178 /* NOTE: this parser is an approximate form of the real command parser */
2179 static void parse_cmdline(const char *cmdline,
2180 int *pnb_args, char **args)
2193 if (nb_args >= MAX_ARGS)
2195 ret = get_str(buf, sizeof(buf), &p);
2196 args[nb_args] = qemu_strdup(buf);
2201 *pnb_args = nb_args;
2204 void readline_find_completion(const char *cmdline)
2206 const char *cmdname;
2207 char *args[MAX_ARGS];
2208 int nb_args, i, len;
2209 const char *ptype, *str;
2213 parse_cmdline(cmdline, &nb_args, args);
2214 #ifdef DEBUG_COMPLETION
2215 for(i = 0; i < nb_args; i++) {
2216 term_printf("arg%d = '%s'\n", i, (char *)args[i]);
2220 /* if the line ends with a space, it means we want to complete the
2222 len = strlen(cmdline);
2223 if (len > 0 && isspace(cmdline[len - 1])) {
2224 if (nb_args >= MAX_ARGS)
2226 args[nb_args++] = qemu_strdup("");
2229 /* command completion */
2234 completion_index = strlen(cmdname);
2235 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2236 cmd_completion(cmdname, cmd->name);
2239 /* find the command */
2240 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
2241 if (compare_cmd(args[0], cmd->name))
2246 ptype = cmd->args_type;
2247 for(i = 0; i < nb_args - 2; i++) {
2248 if (*ptype != '\0') {
2250 while (*ptype == '?')
2254 str = args[nb_args - 1];
2257 /* file completion */
2258 completion_index = strlen(str);
2259 file_completion(str);
2262 /* block device name completion */
2263 completion_index = strlen(str);
2264 bdrv_iterate(block_completion_it, (void *)str);
2267 /* XXX: more generic ? */
2268 if (!strcmp(cmd->name, "info")) {
2269 completion_index = strlen(str);
2270 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2271 cmd_completion(str, cmd->name);
2273 } else if (!strcmp(cmd->name, "sendkey")) {
2274 completion_index = strlen(str);
2275 for(key = key_defs; key->name != NULL; key++) {
2276 cmd_completion(str, key->name);
2284 for(i = 0; i < nb_args; i++)
2288 static int term_can_read(void *opaque)
2293 static void term_read(void *opaque, const uint8_t *buf, int size)
2296 for(i = 0; i < size; i++)
2297 readline_handle_byte(buf[i]);
2300 static void monitor_start_input(void);
2302 static void monitor_handle_command1(void *opaque, const char *cmdline)
2304 monitor_handle_command(cmdline);
2305 monitor_start_input();
2308 static void monitor_start_input(void)
2310 readline_start("(qemu) ", 0, monitor_handle_command1, NULL);
2313 void monitor_init(CharDriverState *hd, int show_banner)
2317 term_printf("QEMU %s monitor - type 'help' for more information\n",
2320 qemu_chr_add_read_handler(hd, term_can_read, term_read, NULL);
2321 monitor_start_input();
2324 /* XXX: use threads ? */
2325 /* modal monitor readline */
2326 static int monitor_readline_started;
2327 static char *monitor_readline_buf;
2328 static int monitor_readline_buf_size;
2330 static void monitor_readline_cb(void *opaque, const char *input)
2332 pstrcpy(monitor_readline_buf, monitor_readline_buf_size, input);
2333 monitor_readline_started = 0;
2336 void monitor_readline(const char *prompt, int is_password,
2337 char *buf, int buf_size)
2340 qemu_chr_send_event(monitor_hd, CHR_EVENT_FOCUS);
2342 readline_start(prompt, is_password, monitor_readline_cb, NULL);
2343 monitor_readline_buf = buf;
2344 monitor_readline_buf_size = buf_size;
2345 monitor_readline_started = 1;
2346 while (monitor_readline_started) {