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
,
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)
260 static int eject_device(BlockDriverState
*bs
, int force
)
262 if (bdrv_is_inserted(bs
)) {
264 if (!bdrv_is_removable(bs
)) {
265 term_printf("device is not removable\n");
268 if (bdrv_is_locked(bs
)) {
269 term_printf("device is locked\n");
278 static void do_eject(int force
, const char *filename
)
280 BlockDriverState
*bs
;
282 bs
= bdrv_find(filename
);
284 term_printf("device not found\n");
287 eject_device(bs
, force
);
290 static void do_change(const char *device
, const char *filename
)
292 BlockDriverState
*bs
;
296 bs
= bdrv_find(device
);
298 term_printf("device not found\n");
301 if (eject_device(bs
, 0) < 0)
303 bdrv_open(bs
, filename
, 0);
304 if (bdrv_is_encrypted(bs
)) {
305 term_printf("%s is encrypted.\n", device
);
306 for(i
= 0; i
< 3; i
++) {
307 monitor_readline("Password: ", 1, password
, sizeof(password
));
308 if (bdrv_set_key(bs
, password
) == 0)
310 term_printf("invalid password\n");
315 static void do_screen_dump(const char *filename
)
317 vga_screen_dump(filename
);
320 static void do_log(const char *items
)
324 if (!strcmp(items
, "none")) {
327 mask
= cpu_str_to_log_mask(items
);
336 static void do_savevm(const char *filename
)
338 if (qemu_savevm(filename
) < 0)
339 term_printf("I/O error when saving VM to '%s'\n", filename
);
342 static void do_loadvm(const char *filename
)
344 if (qemu_loadvm(filename
) < 0)
345 term_printf("I/O error when loading VM from '%s'\n", filename
);
348 static void do_stop(void)
350 vm_stop(EXCP_INTERRUPT
);
353 static void do_cont(void)
358 #ifdef CONFIG_GDBSTUB
359 static void do_gdbserver(int has_port
, int port
)
362 port
= DEFAULT_GDBSTUB_PORT
;
363 if (gdbserver_start(port
) < 0) {
364 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
366 qemu_printf("Waiting gdb connection on port %d\n", port
);
371 static void term_printc(int c
)
388 if (c
>= 32 && c
<= 126) {
389 term_printf("%c", c
);
391 term_printf("\\x%02x", c
);
398 static void memory_dump(int count
, int format
, int wsize
,
399 target_ulong addr
, int is_physical
)
401 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
411 } else if (wsize
== 4) {
414 /* as default we use the current CS size */
416 if (!(cpu_single_env
->segs
[R_CS
].flags
& DESC_B_MASK
))
420 monitor_disas(addr
, count
, is_physical
, flags
);
429 nb_per_line
= line_size
/ wsize
;
434 max_digits
= (wsize
* 8 + 2) / 3;
438 max_digits
= (wsize
* 8) / 4;
442 max_digits
= (wsize
* 8 * 10 + 32) / 33;
450 term_printf(TARGET_FMT_lx
":", addr
);
455 cpu_physical_memory_rw(addr
, buf
, l
, 0);
457 cpu_memory_rw_debug(cpu_single_env
, addr
, buf
, l
, 0);
464 v
= ldub_raw(buf
+ i
);
467 v
= lduw_raw(buf
+ i
);
470 v
= (uint32_t)ldl_raw(buf
+ i
);
473 v
= ldq_raw(buf
+ i
);
479 term_printf("%#*llo", max_digits
, v
);
482 term_printf("0x%0*llx", max_digits
, v
);
485 term_printf("%*llu", max_digits
, v
);
488 term_printf("%*lld", max_digits
, v
);
502 #if TARGET_LONG_BITS == 64
503 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
505 #define GET_TLONG(h, l) (l)
508 static void do_memory_dump(int count
, int format
, int size
,
509 uint32_t addrh
, uint32_t addrl
)
511 target_long addr
= GET_TLONG(addrh
, addrl
);
512 memory_dump(count
, format
, size
, addr
, 0);
515 static void do_physical_memory_dump(int count
, int format
, int size
,
516 uint32_t addrh
, uint32_t addrl
)
519 target_long addr
= GET_TLONG(addrh
, addrl
);
520 memory_dump(count
, format
, size
, addr
, 1);
523 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
525 target_long val
= GET_TLONG(valh
, vall
);
526 #if TARGET_LONG_BITS == 32
529 term_printf("%#o", val
);
532 term_printf("%#x", val
);
535 term_printf("%u", val
);
539 term_printf("%d", val
);
548 term_printf("%#llo", val
);
551 term_printf("%#llx", val
);
554 term_printf("%llu", val
);
558 term_printf("%lld", val
);
568 static void do_sum(uint32_t start
, uint32_t size
)
575 for(addr
= start
; addr
< (start
+ size
); addr
++) {
576 cpu_physical_memory_rw(addr
, buf
, 1, 0);
577 /* BSD sum algorithm ('sum' Unix command) */
578 sum
= (sum
>> 1) | (sum
<< 15);
581 term_printf("%05d\n", sum
);
589 static const KeyDef key_defs
[] = {
612 { 0x0e, "backspace" },
647 { 0x3a, "caps_lock" },
658 { 0x45, "num_lock" },
659 { 0x46, "scroll_lock" },
683 static int get_keycode(const char *key
)
687 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
688 if (!strcmp(key
, p
->name
))
694 static void do_send_key(const char *string
)
697 uint8_t keycodes
[16];
699 int nb_keycodes
, keycode
, i
;
705 while (*p
!= '\0' && *p
!= '-') {
706 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
712 keycode
= get_keycode(keybuf
);
714 term_printf("unknown key: '%s'\n", keybuf
);
717 keycodes
[nb_keycodes
++] = keycode
;
722 /* key down events */
723 for(i
= 0; i
< nb_keycodes
; i
++) {
724 keycode
= keycodes
[i
];
726 kbd_put_keycode(0xe0);
727 kbd_put_keycode(keycode
& 0x7f);
730 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
731 keycode
= keycodes
[i
];
733 kbd_put_keycode(0xe0);
734 kbd_put_keycode(keycode
| 0x80);
738 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
744 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
752 val
= cpu_inb(NULL
, addr
);
756 val
= cpu_inw(NULL
, addr
);
760 val
= cpu_inl(NULL
, addr
);
764 term_printf("port%c[0x%04x] = %#0*x\n",
765 suffix
, addr
, size
* 2, val
);
768 static void do_system_reset(void)
770 qemu_system_reset_request();
773 static void do_system_powerdown(void)
775 qemu_system_powerdown_request();
778 #if defined(TARGET_I386)
779 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
781 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
784 pte
& PG_GLOBAL_MASK
? 'G' : '-',
785 pte
& PG_PSE_MASK
? 'P' : '-',
786 pte
& PG_DIRTY_MASK
? 'D' : '-',
787 pte
& PG_ACCESSED_MASK
? 'A' : '-',
788 pte
& PG_PCD_MASK
? 'C' : '-',
789 pte
& PG_PWT_MASK
? 'T' : '-',
790 pte
& PG_USER_MASK
? 'U' : '-',
791 pte
& PG_RW_MASK
? 'W' : '-');
794 static void tlb_info(void)
796 CPUState
*env
= cpu_single_env
;
798 uint32_t pgd
, pde
, pte
;
800 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
801 term_printf("PG disabled\n");
804 pgd
= env
->cr
[3] & ~0xfff;
805 for(l1
= 0; l1
< 1024; l1
++) {
806 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
807 pde
= le32_to_cpu(pde
);
808 if (pde
& PG_PRESENT_MASK
) {
809 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
810 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
812 for(l2
= 0; l2
< 1024; l2
++) {
813 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
815 pte
= le32_to_cpu(pte
);
816 if (pte
& PG_PRESENT_MASK
) {
817 print_pte((l1
<< 22) + (l2
<< 12),
827 static void mem_print(uint32_t *pstart
, int *plast_prot
,
828 uint32_t end
, int prot
)
834 term_printf("%08x-%08x %08x %c%c%c\n",
835 *pstart
, end
, end
- *pstart
,
836 prot1
& PG_USER_MASK
? 'u' : '-',
838 prot1
& PG_RW_MASK
? 'w' : '-');
848 static void mem_info(void)
850 CPUState
*env
= cpu_single_env
;
851 int l1
, l2
, prot
, last_prot
;
852 uint32_t pgd
, pde
, pte
, start
, end
;
854 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
855 term_printf("PG disabled\n");
858 pgd
= env
->cr
[3] & ~0xfff;
861 for(l1
= 0; l1
< 1024; l1
++) {
862 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
863 pde
= le32_to_cpu(pde
);
865 if (pde
& PG_PRESENT_MASK
) {
866 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
867 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
868 mem_print(&start
, &last_prot
, end
, prot
);
870 for(l2
= 0; l2
< 1024; l2
++) {
871 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
873 pte
= le32_to_cpu(pte
);
874 end
= (l1
<< 22) + (l2
<< 12);
875 if (pte
& PG_PRESENT_MASK
) {
876 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
880 mem_print(&start
, &last_prot
, end
, prot
);
885 mem_print(&start
, &last_prot
, end
, prot
);
891 static void do_info_kqemu(void)
897 val
= cpu_single_env
->kqemu_enabled
;
898 term_printf("kqemu is %s\n", val
? "enabled" : "disabled");
900 term_printf("kqemu support is not compiled\n");
905 static term_cmd_t term_cmds
[] = {
906 { "help|?", "s?", do_help
,
907 "[cmd]", "show the help" },
908 { "commit", "", do_commit
,
909 "", "commit changes to the disk images (if -snapshot is used)" },
910 { "info", "s?", do_info
,
911 "subcommand", "show various information about the system state" },
912 { "q|quit", "", do_quit
,
913 "", "quit the emulator" },
914 { "eject", "-fB", do_eject
,
915 "[-f] device", "eject a removable media (use -f to force it)" },
916 { "change", "BF", do_change
,
917 "device filename", "change a removable media" },
918 { "screendump", "F", do_screen_dump
,
919 "filename", "save screen into PPM image 'filename'" },
920 { "log", "s", do_log
,
921 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
922 { "savevm", "F", do_savevm
,
923 "filename", "save the whole virtual machine state to 'filename'" },
924 { "loadvm", "F", do_loadvm
,
925 "filename", "restore the whole virtual machine state from 'filename'" },
926 { "stop", "", do_stop
,
927 "", "stop emulation", },
928 { "c|cont", "", do_cont
,
929 "", "resume emulation", },
930 #ifdef CONFIG_GDBSTUB
931 { "gdbserver", "i?", do_gdbserver
,
932 "[port]", "start gdbserver session (default port=1234)", },
934 { "x", "/l", do_memory_dump
,
935 "/fmt addr", "virtual memory dump starting at 'addr'", },
936 { "xp", "/l", do_physical_memory_dump
,
937 "/fmt addr", "physical memory dump starting at 'addr'", },
938 { "p|print", "/l", do_print
,
939 "/fmt expr", "print expression value (use $reg for CPU register access)", },
940 { "i", "/ii.", do_ioport_read
,
941 "/fmt addr", "I/O port read" },
943 { "sendkey", "s", do_send_key
,
944 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
945 { "system_reset", "", do_system_reset
,
946 "", "reset the system" },
947 { "system_powerdown", "", do_system_powerdown
,
948 "", "send system power down event" },
949 { "sum", "ii", do_sum
,
950 "addr size", "compute the checksum of a memory region" },
954 static term_cmd_t info_cmds
[] = {
955 { "version", "", do_info_version
,
956 "", "show the version of qemu" },
957 { "network", "", do_info_network
,
958 "", "show the network state" },
959 { "block", "", do_info_block
,
960 "", "show the block devices" },
961 { "registers", "", do_info_registers
,
962 "", "show the cpu registers" },
963 { "history", "", do_info_history
,
964 "", "show the command line history", },
965 { "irq", "", irq_info
,
966 "", "show the interrupts statistics (if available)", },
967 { "pic", "", pic_info
,
968 "", "show i8259 (PIC) state", },
969 { "pci", "", pci_info
,
970 "", "show PCI info", },
971 #if defined(TARGET_I386)
972 { "tlb", "", tlb_info
,
973 "", "show virtual to physical memory mappings", },
974 { "mem", "", mem_info
,
975 "", "show the active virtual memory mappings", },
977 { "jit", "", do_info_jit
,
978 "", "show dynamic compiler info", },
979 { "kqemu", "", do_info_kqemu
,
980 "", "show kqemu information", },
984 /*******************************************************************/
986 static const char *pch
;
987 static jmp_buf expr_env
;
992 typedef struct MonitorDef
{
995 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
999 #if defined(TARGET_I386)
1000 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
1002 return cpu_single_env
->eip
+ cpu_single_env
->segs
[R_CS
].base
;
1006 #if defined(TARGET_PPC)
1007 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
1013 for (i
= 0; i
< 8; i
++)
1014 u
|= cpu_single_env
->crf
[i
] << (32 - (4 * i
));
1019 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
1021 return (cpu_single_env
->msr
[MSR_POW
] << MSR_POW
) |
1022 (cpu_single_env
->msr
[MSR_ILE
] << MSR_ILE
) |
1023 (cpu_single_env
->msr
[MSR_EE
] << MSR_EE
) |
1024 (cpu_single_env
->msr
[MSR_PR
] << MSR_PR
) |
1025 (cpu_single_env
->msr
[MSR_FP
] << MSR_FP
) |
1026 (cpu_single_env
->msr
[MSR_ME
] << MSR_ME
) |
1027 (cpu_single_env
->msr
[MSR_FE0
] << MSR_FE0
) |
1028 (cpu_single_env
->msr
[MSR_SE
] << MSR_SE
) |
1029 (cpu_single_env
->msr
[MSR_BE
] << MSR_BE
) |
1030 (cpu_single_env
->msr
[MSR_FE1
] << MSR_FE1
) |
1031 (cpu_single_env
->msr
[MSR_IP
] << MSR_IP
) |
1032 (cpu_single_env
->msr
[MSR_IR
] << MSR_IR
) |
1033 (cpu_single_env
->msr
[MSR_DR
] << MSR_DR
) |
1034 (cpu_single_env
->msr
[MSR_RI
] << MSR_RI
) |
1035 (cpu_single_env
->msr
[MSR_LE
] << MSR_LE
);
1038 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
1040 return (cpu_single_env
->xer
[XER_SO
] << XER_SO
) |
1041 (cpu_single_env
->xer
[XER_OV
] << XER_OV
) |
1042 (cpu_single_env
->xer
[XER_CA
] << XER_CA
) |
1043 (cpu_single_env
->xer
[XER_BC
] << XER_BC
);
1046 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1048 return cpu_ppc_load_decr(cpu_single_env
);
1051 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1053 return cpu_ppc_load_tbu(cpu_single_env
);
1056 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1058 return cpu_ppc_load_tbl(cpu_single_env
);
1062 #if defined(TARGET_SPARC)
1063 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1065 return GET_PSR(cpu_single_env
);
1068 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1070 return cpu_single_env
->regwptr
[val
];
1074 static MonitorDef monitor_defs
[] = {
1077 #define SEG(name, seg) \
1078 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1079 { name ".base", offsetof(CPUState, segs[seg].base) },\
1080 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1082 { "eax", offsetof(CPUState
, regs
[0]) },
1083 { "ecx", offsetof(CPUState
, regs
[1]) },
1084 { "edx", offsetof(CPUState
, regs
[2]) },
1085 { "ebx", offsetof(CPUState
, regs
[3]) },
1086 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1087 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1088 { "esi", offsetof(CPUState
, regs
[6]) },
1089 { "edi", offsetof(CPUState
, regs
[7]) },
1090 #ifdef TARGET_X86_64
1091 { "r8", offsetof(CPUState
, regs
[8]) },
1092 { "r9", offsetof(CPUState
, regs
[9]) },
1093 { "r10", offsetof(CPUState
, regs
[10]) },
1094 { "r11", offsetof(CPUState
, regs
[11]) },
1095 { "r12", offsetof(CPUState
, regs
[12]) },
1096 { "r13", offsetof(CPUState
, regs
[13]) },
1097 { "r14", offsetof(CPUState
, regs
[14]) },
1098 { "r15", offsetof(CPUState
, regs
[15]) },
1100 { "eflags", offsetof(CPUState
, eflags
) },
1101 { "eip", offsetof(CPUState
, eip
) },
1108 { "pc", 0, monitor_get_pc
, },
1109 #elif defined(TARGET_PPC)
1110 { "r0", offsetof(CPUState
, gpr
[0]) },
1111 { "r1", offsetof(CPUState
, gpr
[1]) },
1112 { "r2", offsetof(CPUState
, gpr
[2]) },
1113 { "r3", offsetof(CPUState
, gpr
[3]) },
1114 { "r4", offsetof(CPUState
, gpr
[4]) },
1115 { "r5", offsetof(CPUState
, gpr
[5]) },
1116 { "r6", offsetof(CPUState
, gpr
[6]) },
1117 { "r7", offsetof(CPUState
, gpr
[7]) },
1118 { "r8", offsetof(CPUState
, gpr
[8]) },
1119 { "r9", offsetof(CPUState
, gpr
[9]) },
1120 { "r10", offsetof(CPUState
, gpr
[10]) },
1121 { "r11", offsetof(CPUState
, gpr
[11]) },
1122 { "r12", offsetof(CPUState
, gpr
[12]) },
1123 { "r13", offsetof(CPUState
, gpr
[13]) },
1124 { "r14", offsetof(CPUState
, gpr
[14]) },
1125 { "r15", offsetof(CPUState
, gpr
[15]) },
1126 { "r16", offsetof(CPUState
, gpr
[16]) },
1127 { "r17", offsetof(CPUState
, gpr
[17]) },
1128 { "r18", offsetof(CPUState
, gpr
[18]) },
1129 { "r19", offsetof(CPUState
, gpr
[19]) },
1130 { "r20", offsetof(CPUState
, gpr
[20]) },
1131 { "r21", offsetof(CPUState
, gpr
[21]) },
1132 { "r22", offsetof(CPUState
, gpr
[22]) },
1133 { "r23", offsetof(CPUState
, gpr
[23]) },
1134 { "r24", offsetof(CPUState
, gpr
[24]) },
1135 { "r25", offsetof(CPUState
, gpr
[25]) },
1136 { "r26", offsetof(CPUState
, gpr
[26]) },
1137 { "r27", offsetof(CPUState
, gpr
[27]) },
1138 { "r28", offsetof(CPUState
, gpr
[28]) },
1139 { "r29", offsetof(CPUState
, gpr
[29]) },
1140 { "r30", offsetof(CPUState
, gpr
[30]) },
1141 { "r31", offsetof(CPUState
, gpr
[31]) },
1142 { "nip|pc", offsetof(CPUState
, nip
) },
1143 { "lr", offsetof(CPUState
, lr
) },
1144 { "ctr", offsetof(CPUState
, ctr
) },
1145 { "decr", 0, &monitor_get_decr
, },
1146 { "ccr", 0, &monitor_get_ccr
, },
1147 { "msr", 0, &monitor_get_msr
, },
1148 { "xer", 0, &monitor_get_xer
, },
1149 { "tbu", 0, &monitor_get_tbu
, },
1150 { "tbl", 0, &monitor_get_tbl
, },
1151 { "sdr1", offsetof(CPUState
, sdr1
) },
1152 { "sr0", offsetof(CPUState
, sr
[0]) },
1153 { "sr1", offsetof(CPUState
, sr
[1]) },
1154 { "sr2", offsetof(CPUState
, sr
[2]) },
1155 { "sr3", offsetof(CPUState
, sr
[3]) },
1156 { "sr4", offsetof(CPUState
, sr
[4]) },
1157 { "sr5", offsetof(CPUState
, sr
[5]) },
1158 { "sr6", offsetof(CPUState
, sr
[6]) },
1159 { "sr7", offsetof(CPUState
, sr
[7]) },
1160 { "sr8", offsetof(CPUState
, sr
[8]) },
1161 { "sr9", offsetof(CPUState
, sr
[9]) },
1162 { "sr10", offsetof(CPUState
, sr
[10]) },
1163 { "sr11", offsetof(CPUState
, sr
[11]) },
1164 { "sr12", offsetof(CPUState
, sr
[12]) },
1165 { "sr13", offsetof(CPUState
, sr
[13]) },
1166 { "sr14", offsetof(CPUState
, sr
[14]) },
1167 { "sr15", offsetof(CPUState
, sr
[15]) },
1168 /* Too lazy to put BATs and SPRs ... */
1169 #elif defined(TARGET_SPARC)
1170 { "g0", offsetof(CPUState
, gregs
[0]) },
1171 { "g1", offsetof(CPUState
, gregs
[1]) },
1172 { "g2", offsetof(CPUState
, gregs
[2]) },
1173 { "g3", offsetof(CPUState
, gregs
[3]) },
1174 { "g4", offsetof(CPUState
, gregs
[4]) },
1175 { "g5", offsetof(CPUState
, gregs
[5]) },
1176 { "g6", offsetof(CPUState
, gregs
[6]) },
1177 { "g7", offsetof(CPUState
, gregs
[7]) },
1178 { "o0", 0, monitor_get_reg
},
1179 { "o1", 1, monitor_get_reg
},
1180 { "o2", 2, monitor_get_reg
},
1181 { "o3", 3, monitor_get_reg
},
1182 { "o4", 4, monitor_get_reg
},
1183 { "o5", 5, monitor_get_reg
},
1184 { "o6", 6, monitor_get_reg
},
1185 { "o7", 7, monitor_get_reg
},
1186 { "l0", 8, monitor_get_reg
},
1187 { "l1", 9, monitor_get_reg
},
1188 { "l2", 10, monitor_get_reg
},
1189 { "l3", 11, monitor_get_reg
},
1190 { "l4", 12, monitor_get_reg
},
1191 { "l5", 13, monitor_get_reg
},
1192 { "l6", 14, monitor_get_reg
},
1193 { "l7", 15, monitor_get_reg
},
1194 { "i0", 16, monitor_get_reg
},
1195 { "i1", 17, monitor_get_reg
},
1196 { "i2", 18, monitor_get_reg
},
1197 { "i3", 19, monitor_get_reg
},
1198 { "i4", 20, monitor_get_reg
},
1199 { "i5", 21, monitor_get_reg
},
1200 { "i6", 22, monitor_get_reg
},
1201 { "i7", 23, monitor_get_reg
},
1202 { "pc", offsetof(CPUState
, pc
) },
1203 { "npc", offsetof(CPUState
, npc
) },
1204 { "y", offsetof(CPUState
, y
) },
1205 { "psr", 0, &monitor_get_psr
, },
1206 { "wim", offsetof(CPUState
, wim
) },
1207 { "tbr", offsetof(CPUState
, tbr
) },
1208 { "fsr", offsetof(CPUState
, fsr
) },
1209 { "f0", offsetof(CPUState
, fpr
[0]) },
1210 { "f1", offsetof(CPUState
, fpr
[1]) },
1211 { "f2", offsetof(CPUState
, fpr
[2]) },
1212 { "f3", offsetof(CPUState
, fpr
[3]) },
1213 { "f4", offsetof(CPUState
, fpr
[4]) },
1214 { "f5", offsetof(CPUState
, fpr
[5]) },
1215 { "f6", offsetof(CPUState
, fpr
[6]) },
1216 { "f7", offsetof(CPUState
, fpr
[7]) },
1217 { "f8", offsetof(CPUState
, fpr
[8]) },
1218 { "f9", offsetof(CPUState
, fpr
[9]) },
1219 { "f10", offsetof(CPUState
, fpr
[10]) },
1220 { "f11", offsetof(CPUState
, fpr
[11]) },
1221 { "f12", offsetof(CPUState
, fpr
[12]) },
1222 { "f13", offsetof(CPUState
, fpr
[13]) },
1223 { "f14", offsetof(CPUState
, fpr
[14]) },
1224 { "f15", offsetof(CPUState
, fpr
[15]) },
1225 { "f16", offsetof(CPUState
, fpr
[16]) },
1226 { "f17", offsetof(CPUState
, fpr
[17]) },
1227 { "f18", offsetof(CPUState
, fpr
[18]) },
1228 { "f19", offsetof(CPUState
, fpr
[19]) },
1229 { "f20", offsetof(CPUState
, fpr
[20]) },
1230 { "f21", offsetof(CPUState
, fpr
[21]) },
1231 { "f22", offsetof(CPUState
, fpr
[22]) },
1232 { "f23", offsetof(CPUState
, fpr
[23]) },
1233 { "f24", offsetof(CPUState
, fpr
[24]) },
1234 { "f25", offsetof(CPUState
, fpr
[25]) },
1235 { "f26", offsetof(CPUState
, fpr
[26]) },
1236 { "f27", offsetof(CPUState
, fpr
[27]) },
1237 { "f28", offsetof(CPUState
, fpr
[28]) },
1238 { "f29", offsetof(CPUState
, fpr
[29]) },
1239 { "f30", offsetof(CPUState
, fpr
[30]) },
1240 { "f31", offsetof(CPUState
, fpr
[31]) },
1245 static void expr_error(const char *fmt
)
1249 longjmp(expr_env
, 1);
1252 static int get_monitor_def(target_long
*pval
, const char *name
)
1257 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1258 if (compare_cmd(name
, md
->name
)) {
1259 if (md
->get_value
) {
1260 *pval
= md
->get_value(md
, md
->offset
);
1262 ptr
= (uint8_t *)cpu_single_env
+ md
->offset
;
1265 *pval
= *(int32_t *)ptr
;
1268 *pval
= *(target_long
*)ptr
;
1281 static void next(void)
1285 while (isspace(*pch
))
1290 static target_long
expr_sum(void);
1292 static target_long
expr_unary(void)
1314 expr_error("')' expected");
1321 expr_error("character constant expected");
1325 expr_error("missing terminating \' character");
1334 while ((*pch
>= 'a' && *pch
<= 'z') ||
1335 (*pch
>= 'A' && *pch
<= 'Z') ||
1336 (*pch
>= '0' && *pch
<= '9') ||
1337 *pch
== '_' || *pch
== '.') {
1338 if ((q
- buf
) < sizeof(buf
) - 1)
1342 while (isspace(*pch
))
1345 if (get_monitor_def(&n
, buf
))
1346 expr_error("unknown register");
1350 expr_error("unexpected end of expression");
1354 n
= strtoul(pch
, &p
, 0);
1356 expr_error("invalid char in expression");
1359 while (isspace(*pch
))
1367 static target_long
expr_prod(void)
1369 target_long val
, val2
;
1375 if (op
!= '*' && op
!= '/' && op
!= '%')
1378 val2
= expr_unary();
1387 expr_error("division by zero");
1398 static target_long
expr_logic(void)
1400 target_long val
, val2
;
1406 if (op
!= '&' && op
!= '|' && op
!= '^')
1426 static target_long
expr_sum(void)
1428 target_long val
, val2
;
1434 if (op
!= '+' && op
!= '-')
1437 val2
= expr_logic();
1446 static int get_expr(target_long
*pval
, const char **pp
)
1449 if (setjmp(expr_env
)) {
1453 while (isspace(*pch
))
1460 static int get_str(char *buf
, int buf_size
, const char **pp
)
1478 while (*p
!= '\0' && *p
!= '\"') {
1494 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1497 if ((q
- buf
) < buf_size
- 1) {
1501 if ((q
- buf
) < buf_size
- 1) {
1508 qemu_printf("unterminated string\n");
1513 while (*p
!= '\0' && !isspace(*p
)) {
1514 if ((q
- buf
) < buf_size
- 1) {
1525 static int default_fmt_format
= 'x';
1526 static int default_fmt_size
= 4;
1530 static void monitor_handle_command(const char *cmdline
)
1532 const char *p
, *pstart
, *typestr
;
1534 int c
, nb_args
, len
, i
, has_arg
;
1538 void *str_allocated
[MAX_ARGS
];
1539 void *args
[MAX_ARGS
];
1542 term_printf("command='%s'\n", cmdline
);
1545 /* extract the command name */
1553 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
1556 if (len
> sizeof(cmdname
) - 1)
1557 len
= sizeof(cmdname
) - 1;
1558 memcpy(cmdname
, pstart
, len
);
1559 cmdname
[len
] = '\0';
1561 /* find the command */
1562 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1563 if (compare_cmd(cmdname
, cmd
->name
))
1566 term_printf("unknown command: '%s'\n", cmdname
);
1570 for(i
= 0; i
< MAX_ARGS
; i
++)
1571 str_allocated
[i
] = NULL
;
1573 /* parse the parameters */
1574 typestr
= cmd
->args_type
;
1591 if (*typestr
== '?') {
1594 /* no optional string: NULL argument */
1599 ret
= get_str(buf
, sizeof(buf
), &p
);
1603 term_printf("%s: filename expected\n", cmdname
);
1606 term_printf("%s: block device name expected\n", cmdname
);
1609 term_printf("%s: string expected\n", cmdname
);
1614 str
= qemu_malloc(strlen(buf
) + 1);
1616 str_allocated
[nb_args
] = str
;
1618 if (nb_args
>= MAX_ARGS
) {
1620 term_printf("%s: too many arguments\n", cmdname
);
1623 args
[nb_args
++] = str
;
1628 int count
, format
, size
;
1638 while (isdigit(*p
)) {
1639 count
= count
* 10 + (*p
- '0');
1677 if (*p
!= '\0' && !isspace(*p
)) {
1678 term_printf("invalid char in format: '%c'\n", *p
);
1682 format
= default_fmt_format
;
1683 if (format
!= 'i') {
1684 /* for 'i', not specifying a size gives -1 as size */
1686 size
= default_fmt_size
;
1688 default_fmt_size
= size
;
1689 default_fmt_format
= format
;
1692 format
= default_fmt_format
;
1693 if (format
!= 'i') {
1694 size
= default_fmt_size
;
1699 if (nb_args
+ 3 > MAX_ARGS
)
1701 args
[nb_args
++] = (void*)count
;
1702 args
[nb_args
++] = (void*)format
;
1703 args
[nb_args
++] = (void*)size
;
1712 if (*typestr
== '?' || *typestr
== '.') {
1714 if (*typestr
== '?') {
1729 if (nb_args
>= MAX_ARGS
)
1731 args
[nb_args
++] = (void *)has_arg
;
1733 if (nb_args
>= MAX_ARGS
)
1739 if (get_expr(&val
, &p
))
1743 if (nb_args
>= MAX_ARGS
)
1745 args
[nb_args
++] = (void *)(int)val
;
1747 if ((nb_args
+ 1) >= MAX_ARGS
)
1749 #if TARGET_LONG_BITS == 64
1750 args
[nb_args
++] = (void *)(int)((val
>> 32) & 0xffffffff);
1752 args
[nb_args
++] = (void *)0;
1754 args
[nb_args
++] = (void *)(int)(val
& 0xffffffff);
1772 term_printf("%s: unsupported option -%c\n",
1779 if (nb_args
>= MAX_ARGS
)
1781 args
[nb_args
++] = (void *)has_option
;
1786 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
1790 /* check that all arguments were parsed */
1794 term_printf("%s: extraneous characters at the end of line\n",
1804 cmd
->handler(args
[0]);
1807 cmd
->handler(args
[0], args
[1]);
1810 cmd
->handler(args
[0], args
[1], args
[2]);
1813 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
1816 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
1819 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
1822 term_printf("unsupported number of arguments: %d\n", nb_args
);
1826 for(i
= 0; i
< MAX_ARGS
; i
++)
1827 qemu_free(str_allocated
[i
]);
1831 static void cmd_completion(const char *name
, const char *list
)
1833 const char *p
, *pstart
;
1842 p
= pstart
+ strlen(pstart
);
1844 if (len
> sizeof(cmd
) - 2)
1845 len
= sizeof(cmd
) - 2;
1846 memcpy(cmd
, pstart
, len
);
1848 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1849 add_completion(cmd
);
1857 static void file_completion(const char *input
)
1862 char file
[1024], file_prefix
[1024];
1866 p
= strrchr(input
, '/');
1869 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1872 input_path_len
= p
- input
+ 1;
1873 memcpy(path
, input
, input_path_len
);
1874 if (input_path_len
> sizeof(path
) - 1)
1875 input_path_len
= sizeof(path
) - 1;
1876 path
[input_path_len
] = '\0';
1877 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1879 #ifdef DEBUG_COMPLETION
1880 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
1882 ffs
= opendir(path
);
1890 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1891 memcpy(file
, input
, input_path_len
);
1892 strcpy(file
+ input_path_len
, d
->d_name
);
1893 /* stat the file to find out if it's a directory.
1894 * In that case add a slash to speed up typing long paths
1897 if(S_ISDIR(sb
.st_mode
))
1899 add_completion(file
);
1905 static void block_completion_it(void *opaque
, const char *name
)
1907 const char *input
= opaque
;
1909 if (input
[0] == '\0' ||
1910 !strncmp(name
, (char *)input
, strlen(input
))) {
1911 add_completion(name
);
1915 /* NOTE: this parser is an approximate form of the real command parser */
1916 static void parse_cmdline(const char *cmdline
,
1917 int *pnb_args
, char **args
)
1930 if (nb_args
>= MAX_ARGS
)
1932 ret
= get_str(buf
, sizeof(buf
), &p
);
1933 args
[nb_args
] = qemu_strdup(buf
);
1938 *pnb_args
= nb_args
;
1941 void readline_find_completion(const char *cmdline
)
1943 const char *cmdname
;
1944 char *args
[MAX_ARGS
];
1945 int nb_args
, i
, len
;
1946 const char *ptype
, *str
;
1949 parse_cmdline(cmdline
, &nb_args
, args
);
1950 #ifdef DEBUG_COMPLETION
1951 for(i
= 0; i
< nb_args
; i
++) {
1952 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
1956 /* if the line ends with a space, it means we want to complete the
1958 len
= strlen(cmdline
);
1959 if (len
> 0 && isspace(cmdline
[len
- 1])) {
1960 if (nb_args
>= MAX_ARGS
)
1962 args
[nb_args
++] = qemu_strdup("");
1965 /* command completion */
1970 completion_index
= strlen(cmdname
);
1971 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1972 cmd_completion(cmdname
, cmd
->name
);
1975 /* find the command */
1976 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1977 if (compare_cmd(args
[0], cmd
->name
))
1982 ptype
= cmd
->args_type
;
1983 for(i
= 0; i
< nb_args
- 2; i
++) {
1984 if (*ptype
!= '\0') {
1986 while (*ptype
== '?')
1990 str
= args
[nb_args
- 1];
1993 /* file completion */
1994 completion_index
= strlen(str
);
1995 file_completion(str
);
1998 /* block device name completion */
1999 completion_index
= strlen(str
);
2000 bdrv_iterate(block_completion_it
, (void *)str
);
2003 /* XXX: more generic ? */
2004 if (!strcmp(cmd
->name
, "info")) {
2005 completion_index
= strlen(str
);
2006 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2007 cmd_completion(str
, cmd
->name
);
2015 for(i
= 0; i
< nb_args
; i
++)
2019 static int term_can_read(void *opaque
)
2024 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2027 for(i
= 0; i
< size
; i
++)
2028 readline_handle_byte(buf
[i
]);
2031 static void monitor_start_input(void);
2033 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2035 monitor_handle_command(cmdline
);
2036 monitor_start_input();
2039 static void monitor_start_input(void)
2041 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2044 void monitor_init(CharDriverState
*hd
, int show_banner
)
2048 term_printf("QEMU %s monitor - type 'help' for more information\n",
2051 qemu_chr_add_read_handler(hd
, term_can_read
, term_read
, NULL
);
2052 monitor_start_input();
2055 /* XXX: use threads ? */
2056 /* modal monitor readline */
2057 static int monitor_readline_started
;
2058 static char *monitor_readline_buf
;
2059 static int monitor_readline_buf_size
;
2061 static void monitor_readline_cb(void *opaque
, const char *input
)
2063 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2064 monitor_readline_started
= 0;
2067 void monitor_readline(const char *prompt
, int is_password
,
2068 char *buf
, int buf_size
)
2071 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
2073 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2074 monitor_readline_buf
= buf
;
2075 monitor_readline_buf_size
= buf_size
;
2076 monitor_readline_started
= 1;
2077 while (monitor_readline_started
) {