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);
69 if (term_outbuf_index
> 0) {
70 qemu_chr_write(monitor_hd
, term_outbuf
, term_outbuf_index
);
71 term_outbuf_index
= 0;
75 /* flush at every end of line or if the buffer is full */
76 void term_puts(const char *str
)
83 term_outbuf
[term_outbuf_index
++] = c
;
84 if (term_outbuf_index
>= sizeof(term_outbuf
) ||
90 void term_vprintf(const char *fmt
, va_list ap
)
93 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
97 void term_printf(const char *fmt
, ...)
101 term_vprintf(fmt
, ap
);
105 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
109 term_vprintf(fmt
, ap
);
114 static int compare_cmd(const char *name
, const char *list
)
116 const char *p
, *pstart
;
124 p
= pstart
+ strlen(pstart
);
125 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
134 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
138 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
139 if (!name
|| !strcmp(name
, cmd
->name
))
140 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
144 static void help_cmd(const char *name
)
146 if (name
&& !strcmp(name
, "info")) {
147 help_cmd1(info_cmds
, "info ", NULL
);
149 help_cmd1(term_cmds
, "", name
);
150 if (name
&& !strcmp(name
, "log")) {
152 term_printf("Log items (comma separated):\n");
153 term_printf("%-10s %s\n", "none", "remove all logs");
154 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
155 term_printf("%-10s %s\n", item
->name
, item
->help
);
161 static void do_help(const char *name
)
166 static void do_commit(void)
170 for (i
= 0; i
< MAX_DISKS
; i
++) {
172 bdrv_commit(bs_table
[i
]);
177 static void do_info(const char *item
)
183 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
184 if (compare_cmd(item
, cmd
->name
))
194 static void do_info_version(void)
196 term_printf("%s\n", QEMU_VERSION
);
199 static void do_info_network(void)
204 for(i
= 0; i
< nb_nics
; i
++) {
206 term_printf("%d: ifname=%s macaddr=", i
, nd
->ifname
);
207 for(j
= 0; j
< 6; j
++) {
210 term_printf("%02x", nd
->macaddr
[j
]);
216 static void do_info_block(void)
221 static void do_info_registers(void)
224 cpu_dump_state(cpu_single_env
, NULL
, monitor_fprintf
,
225 X86_DUMP_FPU
| X86_DUMP_CCOP
);
227 cpu_dump_state(cpu_single_env
, NULL
, monitor_fprintf
,
232 static void do_info_jit(void)
234 dump_exec_info(NULL
, monitor_fprintf
);
237 static void do_info_history (void)
244 str
= readline_get_history(i
);
247 term_printf("%d: '%s'\n", i
, str
);
252 static void do_quit(void)
257 static int eject_device(BlockDriverState
*bs
, int force
)
259 if (bdrv_is_inserted(bs
)) {
261 if (!bdrv_is_removable(bs
)) {
262 term_printf("device is not removable\n");
265 if (bdrv_is_locked(bs
)) {
266 term_printf("device is locked\n");
275 static void do_eject(int force
, const char *filename
)
277 BlockDriverState
*bs
;
279 bs
= bdrv_find(filename
);
281 term_printf("device not found\n");
284 eject_device(bs
, force
);
287 static void do_change(const char *device
, const char *filename
)
289 BlockDriverState
*bs
;
293 bs
= bdrv_find(device
);
295 term_printf("device not found\n");
298 if (eject_device(bs
, 0) < 0)
300 bdrv_open(bs
, filename
, 0);
301 if (bdrv_is_encrypted(bs
)) {
302 term_printf("%s is encrypted.\n", device
);
303 for(i
= 0; i
< 3; i
++) {
304 monitor_readline("Password: ", 1, password
, sizeof(password
));
305 if (bdrv_set_key(bs
, password
) == 0)
307 term_printf("invalid password\n");
312 static void do_screen_dump(const char *filename
)
314 vga_screen_dump(filename
);
317 static void do_log(const char *items
)
321 if (!strcmp(items
, "none")) {
324 mask
= cpu_str_to_log_mask(items
);
333 static void do_savevm(const char *filename
)
335 if (qemu_savevm(filename
) < 0)
336 term_printf("I/O error when saving VM to '%s'\n", filename
);
339 static void do_loadvm(const char *filename
)
341 if (qemu_loadvm(filename
) < 0)
342 term_printf("I/O error when loading VM from '%s'\n", filename
);
345 static void do_stop(void)
347 vm_stop(EXCP_INTERRUPT
);
350 static void do_cont(void)
355 #ifdef CONFIG_GDBSTUB
356 static void do_gdbserver(int has_port
, int port
)
359 port
= DEFAULT_GDBSTUB_PORT
;
360 if (gdbserver_start(port
) < 0) {
361 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
363 qemu_printf("Waiting gdb connection on port %d\n", port
);
368 static void term_printc(int c
)
385 if (c
>= 32 && c
<= 126) {
386 term_printf("%c", c
);
388 term_printf("\\x%02x", c
);
395 static void memory_dump(int count
, int format
, int wsize
,
396 target_ulong addr
, int is_physical
)
398 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
408 } else if (wsize
== 4) {
411 /* as default we use the current CS size */
413 if (!(cpu_single_env
->segs
[R_CS
].flags
& DESC_B_MASK
))
417 monitor_disas(addr
, count
, is_physical
, flags
);
426 nb_per_line
= line_size
/ wsize
;
431 max_digits
= (wsize
* 8 + 2) / 3;
435 max_digits
= (wsize
* 8) / 4;
439 max_digits
= (wsize
* 8 * 10 + 32) / 33;
447 term_printf(TARGET_FMT_lx
":", addr
);
452 cpu_physical_memory_rw(addr
, buf
, l
, 0);
454 cpu_memory_rw_debug(cpu_single_env
, addr
, buf
, l
, 0);
461 v
= ldub_raw(buf
+ i
);
464 v
= lduw_raw(buf
+ i
);
467 v
= (uint32_t)ldl_raw(buf
+ i
);
470 v
= ldq_raw(buf
+ i
);
476 term_printf("%#*llo", max_digits
, v
);
479 term_printf("0x%0*llx", max_digits
, v
);
482 term_printf("%*llu", max_digits
, v
);
485 term_printf("%*lld", max_digits
, v
);
499 #if TARGET_LONG_BITS == 64
500 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
502 #define GET_TLONG(h, l) (l)
505 static void do_memory_dump(int count
, int format
, int size
,
506 uint32_t addrh
, uint32_t addrl
)
508 target_long addr
= GET_TLONG(addrh
, addrl
);
509 memory_dump(count
, format
, size
, addr
, 0);
512 static void do_physical_memory_dump(int count
, int format
, int size
,
513 uint32_t addrh
, uint32_t addrl
)
516 target_long addr
= GET_TLONG(addrh
, addrl
);
517 memory_dump(count
, format
, size
, addr
, 1);
520 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
522 target_long val
= GET_TLONG(valh
, vall
);
523 #if TARGET_LONG_BITS == 32
526 term_printf("%#o", val
);
529 term_printf("%#x", val
);
532 term_printf("%u", val
);
536 term_printf("%d", val
);
545 term_printf("%#llo", val
);
548 term_printf("%#llx", val
);
551 term_printf("%llu", val
);
555 term_printf("%lld", val
);
570 static const KeyDef key_defs
[] = {
593 { 0x0e, "backspace" },
628 { 0x3a, "caps_lock" },
639 { 0x45, "num_lock" },
640 { 0x46, "scroll_lock" },
664 static int get_keycode(const char *key
)
668 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
669 if (!strcmp(key
, p
->name
))
675 static void do_send_key(const char *string
)
678 uint8_t keycodes
[16];
680 int nb_keycodes
, keycode
, i
;
686 while (*p
!= '\0' && *p
!= '-') {
687 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
693 keycode
= get_keycode(keybuf
);
695 term_printf("unknown key: '%s'\n", keybuf
);
698 keycodes
[nb_keycodes
++] = keycode
;
703 /* key down events */
704 for(i
= 0; i
< nb_keycodes
; i
++) {
705 keycode
= keycodes
[i
];
707 kbd_put_keycode(0xe0);
708 kbd_put_keycode(keycode
& 0x7f);
711 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
712 keycode
= keycodes
[i
];
714 kbd_put_keycode(0xe0);
715 kbd_put_keycode(keycode
| 0x80);
719 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
725 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
733 val
= cpu_inb(NULL
, addr
);
737 val
= cpu_inw(NULL
, addr
);
741 val
= cpu_inl(NULL
, addr
);
745 term_printf("port%c[0x%04x] = %#0*x\n",
746 suffix
, addr
, size
* 2, val
);
749 static void do_system_reset(void)
751 qemu_system_reset_request();
754 #if defined(TARGET_I386)
755 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
757 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
760 pte
& PG_GLOBAL_MASK
? 'G' : '-',
761 pte
& PG_PSE_MASK
? 'P' : '-',
762 pte
& PG_DIRTY_MASK
? 'D' : '-',
763 pte
& PG_ACCESSED_MASK
? 'A' : '-',
764 pte
& PG_PCD_MASK
? 'C' : '-',
765 pte
& PG_PWT_MASK
? 'T' : '-',
766 pte
& PG_USER_MASK
? 'U' : '-',
767 pte
& PG_RW_MASK
? 'W' : '-');
770 static void tlb_info(void)
772 CPUState
*env
= cpu_single_env
;
774 uint32_t pgd
, pde
, pte
;
776 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
777 term_printf("PG disabled\n");
780 pgd
= env
->cr
[3] & ~0xfff;
781 for(l1
= 0; l1
< 1024; l1
++) {
782 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
783 pde
= le32_to_cpu(pde
);
784 if (pde
& PG_PRESENT_MASK
) {
785 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
786 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
788 for(l2
= 0; l2
< 1024; l2
++) {
789 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
791 pte
= le32_to_cpu(pte
);
792 if (pte
& PG_PRESENT_MASK
) {
793 print_pte((l1
<< 22) + (l2
<< 12),
803 static void mem_print(uint32_t *pstart
, int *plast_prot
,
804 uint32_t end
, int prot
)
810 term_printf("%08x-%08x %08x %c%c%c\n",
811 *pstart
, end
, end
- *pstart
,
812 prot1
& PG_USER_MASK
? 'u' : '-',
814 prot1
& PG_RW_MASK
? 'w' : '-');
824 static void mem_info(void)
826 CPUState
*env
= cpu_single_env
;
827 int l1
, l2
, prot
, last_prot
;
828 uint32_t pgd
, pde
, pte
, start
, end
;
830 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
831 term_printf("PG disabled\n");
834 pgd
= env
->cr
[3] & ~0xfff;
837 for(l1
= 0; l1
< 1024; l1
++) {
838 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
839 pde
= le32_to_cpu(pde
);
841 if (pde
& PG_PRESENT_MASK
) {
842 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
843 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
844 mem_print(&start
, &last_prot
, end
, prot
);
846 for(l2
= 0; l2
< 1024; l2
++) {
847 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
849 pte
= le32_to_cpu(pte
);
850 end
= (l1
<< 22) + (l2
<< 12);
851 if (pte
& PG_PRESENT_MASK
) {
852 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
856 mem_print(&start
, &last_prot
, end
, prot
);
861 mem_print(&start
, &last_prot
, end
, prot
);
867 static term_cmd_t term_cmds
[] = {
868 { "help|?", "s?", do_help
,
869 "[cmd]", "show the help" },
870 { "commit", "", do_commit
,
871 "", "commit changes to the disk images (if -snapshot is used)" },
872 { "info", "s?", do_info
,
873 "subcommand", "show various information about the system state" },
874 { "q|quit", "", do_quit
,
875 "", "quit the emulator" },
876 { "eject", "-fB", do_eject
,
877 "[-f] device", "eject a removable media (use -f to force it)" },
878 { "change", "BF", do_change
,
879 "device filename", "change a removable media" },
880 { "screendump", "F", do_screen_dump
,
881 "filename", "save screen into PPM image 'filename'" },
882 { "log", "s", do_log
,
883 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
884 { "savevm", "F", do_savevm
,
885 "filename", "save the whole virtual machine state to 'filename'" },
886 { "loadvm", "F", do_loadvm
,
887 "filename", "restore the whole virtual machine state from 'filename'" },
888 { "stop", "", do_stop
,
889 "", "stop emulation", },
890 { "c|cont", "", do_cont
,
891 "", "resume emulation", },
892 #ifdef CONFIG_GDBSTUB
893 { "gdbserver", "i?", do_gdbserver
,
894 "[port]", "start gdbserver session (default port=1234)", },
896 { "x", "/l", do_memory_dump
,
897 "/fmt addr", "virtual memory dump starting at 'addr'", },
898 { "xp", "/l", do_physical_memory_dump
,
899 "/fmt addr", "physical memory dump starting at 'addr'", },
900 { "p|print", "/l", do_print
,
901 "/fmt expr", "print expression value (use $reg for CPU register access)", },
902 { "i", "/ii.", do_ioport_read
,
903 "/fmt addr", "I/O port read" },
905 { "sendkey", "s", do_send_key
,
906 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
907 { "system_reset", "", do_system_reset
,
908 "", "reset the system" },
912 static term_cmd_t info_cmds
[] = {
913 { "version", "", do_info_version
,
914 "", "show the version of qemu" },
915 { "network", "", do_info_network
,
916 "", "show the network state" },
917 { "block", "", do_info_block
,
918 "", "show the block devices" },
919 { "registers", "", do_info_registers
,
920 "", "show the cpu registers" },
921 { "history", "", do_info_history
,
922 "", "show the command line history", },
923 { "irq", "", irq_info
,
924 "", "show the interrupts statistics (if available)", },
925 { "pic", "", pic_info
,
926 "", "show i8259 (PIC) state", },
927 { "pci", "", pci_info
,
928 "", "show PCI info", },
929 #if defined(TARGET_I386)
930 { "tlb", "", tlb_info
,
931 "", "show virtual to physical memory mappings", },
932 { "mem", "", mem_info
,
933 "", "show the active virtual memory mappings", },
935 { "jit", "", do_info_jit
,
936 "", "show dynamic compiler info", },
940 /*******************************************************************/
942 static const char *pch
;
943 static jmp_buf expr_env
;
948 typedef struct MonitorDef
{
951 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
955 #if defined(TARGET_I386)
956 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
958 return cpu_single_env
->eip
+ cpu_single_env
->segs
[R_CS
].base
;
962 #if defined(TARGET_PPC)
963 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
969 for (i
= 0; i
< 8; i
++)
970 u
|= cpu_single_env
->crf
[i
] << (32 - (4 * i
));
975 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
977 return (cpu_single_env
->msr
[MSR_POW
] << MSR_POW
) |
978 (cpu_single_env
->msr
[MSR_ILE
] << MSR_ILE
) |
979 (cpu_single_env
->msr
[MSR_EE
] << MSR_EE
) |
980 (cpu_single_env
->msr
[MSR_PR
] << MSR_PR
) |
981 (cpu_single_env
->msr
[MSR_FP
] << MSR_FP
) |
982 (cpu_single_env
->msr
[MSR_ME
] << MSR_ME
) |
983 (cpu_single_env
->msr
[MSR_FE0
] << MSR_FE0
) |
984 (cpu_single_env
->msr
[MSR_SE
] << MSR_SE
) |
985 (cpu_single_env
->msr
[MSR_BE
] << MSR_BE
) |
986 (cpu_single_env
->msr
[MSR_FE1
] << MSR_FE1
) |
987 (cpu_single_env
->msr
[MSR_IP
] << MSR_IP
) |
988 (cpu_single_env
->msr
[MSR_IR
] << MSR_IR
) |
989 (cpu_single_env
->msr
[MSR_DR
] << MSR_DR
) |
990 (cpu_single_env
->msr
[MSR_RI
] << MSR_RI
) |
991 (cpu_single_env
->msr
[MSR_LE
] << MSR_LE
);
994 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
996 return (cpu_single_env
->xer
[XER_SO
] << XER_SO
) |
997 (cpu_single_env
->xer
[XER_OV
] << XER_OV
) |
998 (cpu_single_env
->xer
[XER_CA
] << XER_CA
) |
999 (cpu_single_env
->xer
[XER_BC
] << XER_BC
);
1002 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1004 return cpu_ppc_load_decr(cpu_single_env
);
1007 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1009 return cpu_ppc_load_tbu(cpu_single_env
);
1012 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1014 return cpu_ppc_load_tbl(cpu_single_env
);
1018 #if defined(TARGET_SPARC)
1019 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1021 return GET_PSR(cpu_single_env
);
1024 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1026 return cpu_single_env
->regwptr
[val
];
1030 static MonitorDef monitor_defs
[] = {
1033 #define SEG(name, seg) \
1034 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1035 { name ".base", offsetof(CPUState, segs[seg].base) },\
1036 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1038 { "eax", offsetof(CPUState
, regs
[0]) },
1039 { "ecx", offsetof(CPUState
, regs
[1]) },
1040 { "edx", offsetof(CPUState
, regs
[2]) },
1041 { "ebx", offsetof(CPUState
, regs
[3]) },
1042 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1043 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1044 { "esi", offsetof(CPUState
, regs
[6]) },
1045 { "edi", offsetof(CPUState
, regs
[7]) },
1046 #ifdef TARGET_X86_64
1047 { "r8", offsetof(CPUState
, regs
[8]) },
1048 { "r9", offsetof(CPUState
, regs
[9]) },
1049 { "r10", offsetof(CPUState
, regs
[10]) },
1050 { "r11", offsetof(CPUState
, regs
[11]) },
1051 { "r12", offsetof(CPUState
, regs
[12]) },
1052 { "r13", offsetof(CPUState
, regs
[13]) },
1053 { "r14", offsetof(CPUState
, regs
[14]) },
1054 { "r15", offsetof(CPUState
, regs
[15]) },
1056 { "eflags", offsetof(CPUState
, eflags
) },
1057 { "eip", offsetof(CPUState
, eip
) },
1064 { "pc", 0, monitor_get_pc
, },
1065 #elif defined(TARGET_PPC)
1066 { "r0", offsetof(CPUState
, gpr
[0]) },
1067 { "r1", offsetof(CPUState
, gpr
[1]) },
1068 { "r2", offsetof(CPUState
, gpr
[2]) },
1069 { "r3", offsetof(CPUState
, gpr
[3]) },
1070 { "r4", offsetof(CPUState
, gpr
[4]) },
1071 { "r5", offsetof(CPUState
, gpr
[5]) },
1072 { "r6", offsetof(CPUState
, gpr
[6]) },
1073 { "r7", offsetof(CPUState
, gpr
[7]) },
1074 { "r8", offsetof(CPUState
, gpr
[8]) },
1075 { "r9", offsetof(CPUState
, gpr
[9]) },
1076 { "r10", offsetof(CPUState
, gpr
[10]) },
1077 { "r11", offsetof(CPUState
, gpr
[11]) },
1078 { "r12", offsetof(CPUState
, gpr
[12]) },
1079 { "r13", offsetof(CPUState
, gpr
[13]) },
1080 { "r14", offsetof(CPUState
, gpr
[14]) },
1081 { "r15", offsetof(CPUState
, gpr
[15]) },
1082 { "r16", offsetof(CPUState
, gpr
[16]) },
1083 { "r17", offsetof(CPUState
, gpr
[17]) },
1084 { "r18", offsetof(CPUState
, gpr
[18]) },
1085 { "r19", offsetof(CPUState
, gpr
[19]) },
1086 { "r20", offsetof(CPUState
, gpr
[20]) },
1087 { "r21", offsetof(CPUState
, gpr
[21]) },
1088 { "r22", offsetof(CPUState
, gpr
[22]) },
1089 { "r23", offsetof(CPUState
, gpr
[23]) },
1090 { "r24", offsetof(CPUState
, gpr
[24]) },
1091 { "r25", offsetof(CPUState
, gpr
[25]) },
1092 { "r26", offsetof(CPUState
, gpr
[26]) },
1093 { "r27", offsetof(CPUState
, gpr
[27]) },
1094 { "r28", offsetof(CPUState
, gpr
[28]) },
1095 { "r29", offsetof(CPUState
, gpr
[29]) },
1096 { "r30", offsetof(CPUState
, gpr
[30]) },
1097 { "r31", offsetof(CPUState
, gpr
[31]) },
1098 { "nip|pc", offsetof(CPUState
, nip
) },
1099 { "lr", offsetof(CPUState
, lr
) },
1100 { "ctr", offsetof(CPUState
, ctr
) },
1101 { "decr", 0, &monitor_get_decr
, },
1102 { "ccr", 0, &monitor_get_ccr
, },
1103 { "msr", 0, &monitor_get_msr
, },
1104 { "xer", 0, &monitor_get_xer
, },
1105 { "tbu", 0, &monitor_get_tbu
, },
1106 { "tbl", 0, &monitor_get_tbl
, },
1107 { "sdr1", offsetof(CPUState
, sdr1
) },
1108 { "sr0", offsetof(CPUState
, sr
[0]) },
1109 { "sr1", offsetof(CPUState
, sr
[1]) },
1110 { "sr2", offsetof(CPUState
, sr
[2]) },
1111 { "sr3", offsetof(CPUState
, sr
[3]) },
1112 { "sr4", offsetof(CPUState
, sr
[4]) },
1113 { "sr5", offsetof(CPUState
, sr
[5]) },
1114 { "sr6", offsetof(CPUState
, sr
[6]) },
1115 { "sr7", offsetof(CPUState
, sr
[7]) },
1116 { "sr8", offsetof(CPUState
, sr
[8]) },
1117 { "sr9", offsetof(CPUState
, sr
[9]) },
1118 { "sr10", offsetof(CPUState
, sr
[10]) },
1119 { "sr11", offsetof(CPUState
, sr
[11]) },
1120 { "sr12", offsetof(CPUState
, sr
[12]) },
1121 { "sr13", offsetof(CPUState
, sr
[13]) },
1122 { "sr14", offsetof(CPUState
, sr
[14]) },
1123 { "sr15", offsetof(CPUState
, sr
[15]) },
1124 /* Too lazy to put BATs and SPRs ... */
1125 #elif defined(TARGET_SPARC)
1126 { "g0", offsetof(CPUState
, gregs
[0]) },
1127 { "g1", offsetof(CPUState
, gregs
[1]) },
1128 { "g2", offsetof(CPUState
, gregs
[2]) },
1129 { "g3", offsetof(CPUState
, gregs
[3]) },
1130 { "g4", offsetof(CPUState
, gregs
[4]) },
1131 { "g5", offsetof(CPUState
, gregs
[5]) },
1132 { "g6", offsetof(CPUState
, gregs
[6]) },
1133 { "g7", offsetof(CPUState
, gregs
[7]) },
1134 { "o0", 0, monitor_get_reg
},
1135 { "o1", 1, monitor_get_reg
},
1136 { "o2", 2, monitor_get_reg
},
1137 { "o3", 3, monitor_get_reg
},
1138 { "o4", 4, monitor_get_reg
},
1139 { "o5", 5, monitor_get_reg
},
1140 { "o6", 6, monitor_get_reg
},
1141 { "o7", 7, monitor_get_reg
},
1142 { "l0", 8, monitor_get_reg
},
1143 { "l1", 9, monitor_get_reg
},
1144 { "l2", 10, monitor_get_reg
},
1145 { "l3", 11, monitor_get_reg
},
1146 { "l4", 12, monitor_get_reg
},
1147 { "l5", 13, monitor_get_reg
},
1148 { "l6", 14, monitor_get_reg
},
1149 { "l7", 15, monitor_get_reg
},
1150 { "i0", 16, monitor_get_reg
},
1151 { "i1", 17, monitor_get_reg
},
1152 { "i2", 18, monitor_get_reg
},
1153 { "i3", 19, monitor_get_reg
},
1154 { "i4", 20, monitor_get_reg
},
1155 { "i5", 21, monitor_get_reg
},
1156 { "i6", 22, monitor_get_reg
},
1157 { "i7", 23, monitor_get_reg
},
1158 { "pc", offsetof(CPUState
, pc
) },
1159 { "npc", offsetof(CPUState
, npc
) },
1160 { "y", offsetof(CPUState
, y
) },
1161 { "psr", 0, &monitor_get_psr
, },
1162 { "wim", offsetof(CPUState
, wim
) },
1163 { "tbr", offsetof(CPUState
, tbr
) },
1164 { "fsr", offsetof(CPUState
, fsr
) },
1165 { "f0", offsetof(CPUState
, fpr
[0]) },
1166 { "f1", offsetof(CPUState
, fpr
[1]) },
1167 { "f2", offsetof(CPUState
, fpr
[2]) },
1168 { "f3", offsetof(CPUState
, fpr
[3]) },
1169 { "f4", offsetof(CPUState
, fpr
[4]) },
1170 { "f5", offsetof(CPUState
, fpr
[5]) },
1171 { "f6", offsetof(CPUState
, fpr
[6]) },
1172 { "f7", offsetof(CPUState
, fpr
[7]) },
1173 { "f8", offsetof(CPUState
, fpr
[8]) },
1174 { "f9", offsetof(CPUState
, fpr
[9]) },
1175 { "f10", offsetof(CPUState
, fpr
[10]) },
1176 { "f11", offsetof(CPUState
, fpr
[11]) },
1177 { "f12", offsetof(CPUState
, fpr
[12]) },
1178 { "f13", offsetof(CPUState
, fpr
[13]) },
1179 { "f14", offsetof(CPUState
, fpr
[14]) },
1180 { "f15", offsetof(CPUState
, fpr
[15]) },
1181 { "f16", offsetof(CPUState
, fpr
[16]) },
1182 { "f17", offsetof(CPUState
, fpr
[17]) },
1183 { "f18", offsetof(CPUState
, fpr
[18]) },
1184 { "f19", offsetof(CPUState
, fpr
[19]) },
1185 { "f20", offsetof(CPUState
, fpr
[20]) },
1186 { "f21", offsetof(CPUState
, fpr
[21]) },
1187 { "f22", offsetof(CPUState
, fpr
[22]) },
1188 { "f23", offsetof(CPUState
, fpr
[23]) },
1189 { "f24", offsetof(CPUState
, fpr
[24]) },
1190 { "f25", offsetof(CPUState
, fpr
[25]) },
1191 { "f26", offsetof(CPUState
, fpr
[26]) },
1192 { "f27", offsetof(CPUState
, fpr
[27]) },
1193 { "f28", offsetof(CPUState
, fpr
[28]) },
1194 { "f29", offsetof(CPUState
, fpr
[29]) },
1195 { "f30", offsetof(CPUState
, fpr
[30]) },
1196 { "f31", offsetof(CPUState
, fpr
[31]) },
1201 static void expr_error(const char *fmt
)
1205 longjmp(expr_env
, 1);
1208 static int get_monitor_def(target_long
*pval
, const char *name
)
1213 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1214 if (compare_cmd(name
, md
->name
)) {
1215 if (md
->get_value
) {
1216 *pval
= md
->get_value(md
, md
->offset
);
1218 ptr
= (uint8_t *)cpu_single_env
+ md
->offset
;
1221 *pval
= *(int32_t *)ptr
;
1224 *pval
= *(target_long
*)ptr
;
1237 static void next(void)
1241 while (isspace(*pch
))
1246 static target_long
expr_sum(void);
1248 static target_long
expr_unary(void)
1270 expr_error("')' expected");
1277 expr_error("character constant expected");
1281 expr_error("missing terminating \' character");
1290 while ((*pch
>= 'a' && *pch
<= 'z') ||
1291 (*pch
>= 'A' && *pch
<= 'Z') ||
1292 (*pch
>= '0' && *pch
<= '9') ||
1293 *pch
== '_' || *pch
== '.') {
1294 if ((q
- buf
) < sizeof(buf
) - 1)
1298 while (isspace(*pch
))
1301 if (get_monitor_def(&n
, buf
))
1302 expr_error("unknown register");
1306 expr_error("unexpected end of expression");
1310 n
= strtoul(pch
, &p
, 0);
1312 expr_error("invalid char in expression");
1315 while (isspace(*pch
))
1323 static target_long
expr_prod(void)
1325 target_long val
, val2
;
1331 if (op
!= '*' && op
!= '/' && op
!= '%')
1334 val2
= expr_unary();
1343 expr_error("division by zero");
1354 static target_long
expr_logic(void)
1356 target_long val
, val2
;
1362 if (op
!= '&' && op
!= '|' && op
!= '^')
1382 static target_long
expr_sum(void)
1384 target_long val
, val2
;
1390 if (op
!= '+' && op
!= '-')
1393 val2
= expr_logic();
1402 static int get_expr(target_long
*pval
, const char **pp
)
1405 if (setjmp(expr_env
)) {
1409 while (isspace(*pch
))
1416 static int get_str(char *buf
, int buf_size
, const char **pp
)
1434 while (*p
!= '\0' && *p
!= '\"') {
1450 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1453 if ((q
- buf
) < buf_size
- 1) {
1457 if ((q
- buf
) < buf_size
- 1) {
1464 qemu_printf("unterminated string\n");
1469 while (*p
!= '\0' && !isspace(*p
)) {
1470 if ((q
- buf
) < buf_size
- 1) {
1481 static int default_fmt_format
= 'x';
1482 static int default_fmt_size
= 4;
1486 static void monitor_handle_command(const char *cmdline
)
1488 const char *p
, *pstart
, *typestr
;
1490 int c
, nb_args
, len
, i
, has_arg
;
1494 void *str_allocated
[MAX_ARGS
];
1495 void *args
[MAX_ARGS
];
1498 term_printf("command='%s'\n", cmdline
);
1501 /* extract the command name */
1509 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
1512 if (len
> sizeof(cmdname
) - 1)
1513 len
= sizeof(cmdname
) - 1;
1514 memcpy(cmdname
, pstart
, len
);
1515 cmdname
[len
] = '\0';
1517 /* find the command */
1518 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1519 if (compare_cmd(cmdname
, cmd
->name
))
1522 term_printf("unknown command: '%s'\n", cmdname
);
1526 for(i
= 0; i
< MAX_ARGS
; i
++)
1527 str_allocated
[i
] = NULL
;
1529 /* parse the parameters */
1530 typestr
= cmd
->args_type
;
1547 if (*typestr
== '?') {
1550 /* no optional string: NULL argument */
1555 ret
= get_str(buf
, sizeof(buf
), &p
);
1559 term_printf("%s: filename expected\n", cmdname
);
1562 term_printf("%s: block device name expected\n", cmdname
);
1565 term_printf("%s: string expected\n", cmdname
);
1570 str
= qemu_malloc(strlen(buf
) + 1);
1572 str_allocated
[nb_args
] = str
;
1574 if (nb_args
>= MAX_ARGS
) {
1576 term_printf("%s: too many arguments\n", cmdname
);
1579 args
[nb_args
++] = str
;
1584 int count
, format
, size
;
1594 while (isdigit(*p
)) {
1595 count
= count
* 10 + (*p
- '0');
1633 if (*p
!= '\0' && !isspace(*p
)) {
1634 term_printf("invalid char in format: '%c'\n", *p
);
1638 format
= default_fmt_format
;
1639 if (format
!= 'i') {
1640 /* for 'i', not specifying a size gives -1 as size */
1642 size
= default_fmt_size
;
1644 default_fmt_size
= size
;
1645 default_fmt_format
= format
;
1648 format
= default_fmt_format
;
1649 if (format
!= 'i') {
1650 size
= default_fmt_size
;
1655 if (nb_args
+ 3 > MAX_ARGS
)
1657 args
[nb_args
++] = (void*)count
;
1658 args
[nb_args
++] = (void*)format
;
1659 args
[nb_args
++] = (void*)size
;
1668 if (*typestr
== '?' || *typestr
== '.') {
1670 if (*typestr
== '?') {
1685 if (nb_args
>= MAX_ARGS
)
1687 args
[nb_args
++] = (void *)has_arg
;
1689 if (nb_args
>= MAX_ARGS
)
1695 if (get_expr(&val
, &p
))
1699 if (nb_args
>= MAX_ARGS
)
1701 args
[nb_args
++] = (void *)(int)val
;
1703 if ((nb_args
+ 1) >= MAX_ARGS
)
1705 #if TARGET_LONG_BITS == 64
1706 args
[nb_args
++] = (void *)(int)((val
>> 32) & 0xffffffff);
1708 args
[nb_args
++] = (void *)0;
1710 args
[nb_args
++] = (void *)(int)(val
& 0xffffffff);
1728 term_printf("%s: unsupported option -%c\n",
1735 if (nb_args
>= MAX_ARGS
)
1737 args
[nb_args
++] = (void *)has_option
;
1742 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
1746 /* check that all arguments were parsed */
1750 term_printf("%s: extraneous characters at the end of line\n",
1760 cmd
->handler(args
[0]);
1763 cmd
->handler(args
[0], args
[1]);
1766 cmd
->handler(args
[0], args
[1], args
[2]);
1769 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
1772 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
1775 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
1778 term_printf("unsupported number of arguments: %d\n", nb_args
);
1782 for(i
= 0; i
< MAX_ARGS
; i
++)
1783 qemu_free(str_allocated
[i
]);
1787 static void cmd_completion(const char *name
, const char *list
)
1789 const char *p
, *pstart
;
1798 p
= pstart
+ strlen(pstart
);
1800 if (len
> sizeof(cmd
) - 2)
1801 len
= sizeof(cmd
) - 2;
1802 memcpy(cmd
, pstart
, len
);
1804 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1805 add_completion(cmd
);
1813 static void file_completion(const char *input
)
1818 char file
[1024], file_prefix
[1024];
1822 p
= strrchr(input
, '/');
1825 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1828 input_path_len
= p
- input
+ 1;
1829 memcpy(path
, input
, input_path_len
);
1830 if (input_path_len
> sizeof(path
) - 1)
1831 input_path_len
= sizeof(path
) - 1;
1832 path
[input_path_len
] = '\0';
1833 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1835 #ifdef DEBUG_COMPLETION
1836 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
1838 ffs
= opendir(path
);
1846 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1847 memcpy(file
, input
, input_path_len
);
1848 strcpy(file
+ input_path_len
, d
->d_name
);
1849 /* stat the file to find out if it's a directory.
1850 * In that case add a slash to speed up typing long paths
1853 if(S_ISDIR(sb
.st_mode
))
1855 add_completion(file
);
1861 static void block_completion_it(void *opaque
, const char *name
)
1863 const char *input
= opaque
;
1865 if (input
[0] == '\0' ||
1866 !strncmp(name
, (char *)input
, strlen(input
))) {
1867 add_completion(name
);
1871 /* NOTE: this parser is an approximate form of the real command parser */
1872 static void parse_cmdline(const char *cmdline
,
1873 int *pnb_args
, char **args
)
1886 if (nb_args
>= MAX_ARGS
)
1888 ret
= get_str(buf
, sizeof(buf
), &p
);
1889 args
[nb_args
] = qemu_strdup(buf
);
1894 *pnb_args
= nb_args
;
1897 void readline_find_completion(const char *cmdline
)
1899 const char *cmdname
;
1900 char *args
[MAX_ARGS
];
1901 int nb_args
, i
, len
;
1902 const char *ptype
, *str
;
1905 parse_cmdline(cmdline
, &nb_args
, args
);
1906 #ifdef DEBUG_COMPLETION
1907 for(i
= 0; i
< nb_args
; i
++) {
1908 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
1912 /* if the line ends with a space, it means we want to complete the
1914 len
= strlen(cmdline
);
1915 if (len
> 0 && isspace(cmdline
[len
- 1])) {
1916 if (nb_args
>= MAX_ARGS
)
1918 args
[nb_args
++] = qemu_strdup("");
1921 /* command completion */
1926 completion_index
= strlen(cmdname
);
1927 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1928 cmd_completion(cmdname
, cmd
->name
);
1931 /* find the command */
1932 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1933 if (compare_cmd(args
[0], cmd
->name
))
1938 ptype
= cmd
->args_type
;
1939 for(i
= 0; i
< nb_args
- 2; i
++) {
1940 if (*ptype
!= '\0') {
1942 while (*ptype
== '?')
1946 str
= args
[nb_args
- 1];
1949 /* file completion */
1950 completion_index
= strlen(str
);
1951 file_completion(str
);
1954 /* block device name completion */
1955 completion_index
= strlen(str
);
1956 bdrv_iterate(block_completion_it
, (void *)str
);
1959 /* XXX: more generic ? */
1960 if (!strcmp(cmd
->name
, "info")) {
1961 completion_index
= strlen(str
);
1962 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
1963 cmd_completion(str
, cmd
->name
);
1971 for(i
= 0; i
< nb_args
; i
++)
1975 static int term_can_read(void *opaque
)
1980 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
1983 for(i
= 0; i
< size
; i
++)
1984 readline_handle_byte(buf
[i
]);
1987 static void monitor_start_input(void);
1989 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
1991 monitor_handle_command(cmdline
);
1992 monitor_start_input();
1995 static void monitor_start_input(void)
1997 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2000 void monitor_init(CharDriverState
*hd
, int show_banner
)
2004 term_printf("QEMU %s monitor - type 'help' for more information\n",
2007 qemu_chr_add_read_handler(hd
, term_can_read
, term_read
, NULL
);
2008 monitor_start_input();
2011 /* XXX: use threads ? */
2012 /* modal monitor readline */
2013 static int monitor_readline_started
;
2014 static char *monitor_readline_buf
;
2015 static int monitor_readline_buf_size
;
2017 static void monitor_readline_cb(void *opaque
, const char *input
)
2019 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2020 monitor_readline_started
= 0;
2023 void monitor_readline(const char *prompt
, int is_password
,
2024 char *buf
, int buf_size
)
2027 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
2029 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2030 monitor_readline_buf
= buf
;
2031 monitor_readline_buf_size
= buf_size
;
2032 monitor_readline_started
= 1;
2033 while (monitor_readline_started
) {