4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 //#define DEBUG_COMPLETION
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
39 * 'B' block device name
40 * 's' string (accept optional quote)
42 * '/' optional gdb-like print format (like "/10x")
44 * '?' optional type (for 'F', 's' and 'i')
48 typedef struct term_cmd_t
{
50 const char *args_type
;
56 static CharDriverState
*monitor_hd
;
58 static term_cmd_t term_cmds
[];
59 static term_cmd_t info_cmds
[];
61 static char term_outbuf
[1024];
62 static int term_outbuf_index
;
64 static void monitor_start_input(void);
68 if (term_outbuf_index
> 0) {
69 qemu_chr_write(monitor_hd
, term_outbuf
, term_outbuf_index
);
70 term_outbuf_index
= 0;
74 /* flush at every end of line or if the buffer is full */
75 void term_puts(const char *str
)
82 term_outbuf
[term_outbuf_index
++] = c
;
83 if (term_outbuf_index
>= sizeof(term_outbuf
) ||
89 void term_vprintf(const char *fmt
, va_list ap
)
92 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
96 void term_printf(const char *fmt
, ...)
100 term_vprintf(fmt
, ap
);
104 static int compare_cmd(const char *name
, const char *list
)
106 const char *p
, *pstart
;
114 p
= pstart
+ strlen(pstart
);
115 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
124 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
128 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
129 if (!name
|| !strcmp(name
, cmd
->name
))
130 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
134 static void help_cmd(const char *name
)
136 if (name
&& !strcmp(name
, "info")) {
137 help_cmd1(info_cmds
, "info ", NULL
);
139 help_cmd1(term_cmds
, "", name
);
140 if (name
&& !strcmp(name
, "log")) {
142 term_printf("Log items (comma separated):\n");
143 term_printf("%-10s %s\n", "none", "remove all logs");
144 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
145 term_printf("%-10s %s\n", item
->name
, item
->help
);
151 static void do_help(const char *name
)
156 static void do_commit(void)
160 for (i
= 0; i
< MAX_DISKS
; i
++) {
162 bdrv_commit(bs_table
[i
]);
167 static void do_info(const char *item
)
173 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
174 if (compare_cmd(item
, cmd
->name
))
184 static void do_info_network(void)
189 for(i
= 0; i
< nb_nics
; i
++) {
191 term_printf("%d: ifname=%s macaddr=", i
, nd
->ifname
);
192 for(j
= 0; j
< 6; j
++) {
195 term_printf("%02x", nd
->macaddr
[j
]);
201 static void do_info_block(void)
206 static void do_info_registers(void)
209 cpu_dump_state(cpu_single_env
, stdout
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
211 cpu_dump_state(cpu_single_env
, stdout
, 0);
215 static void do_info_history (void)
222 str
= readline_get_history(i
);
225 term_printf("%d: '%s'\n", i
, str
);
229 static void do_quit(void)
234 static int eject_device(BlockDriverState
*bs
, int force
)
236 if (bdrv_is_inserted(bs
)) {
238 if (!bdrv_is_removable(bs
)) {
239 term_printf("device is not removable\n");
242 if (bdrv_is_locked(bs
)) {
243 term_printf("device is locked\n");
252 static void do_eject(int force
, const char *filename
)
254 BlockDriverState
*bs
;
256 bs
= bdrv_find(filename
);
258 term_printf("device not found\n");
261 eject_device(bs
, force
);
264 static void do_change(const char *device
, const char *filename
)
266 BlockDriverState
*bs
;
270 bs
= bdrv_find(device
);
272 term_printf("device not found\n");
275 if (eject_device(bs
, 0) < 0)
277 bdrv_open(bs
, filename
, 0);
278 if (bdrv_is_encrypted(bs
)) {
279 term_printf("%s is encrypted.\n", device
);
280 for(i
= 0; i
< 3; i
++) {
281 monitor_readline("Password: ", 1, password
, sizeof(password
));
282 if (bdrv_set_key(bs
, password
) == 0)
284 term_printf("invalid password\n");
289 static void do_screen_dump(const char *filename
)
291 vga_screen_dump(filename
);
294 static void do_log(const char *items
)
298 if (!strcmp(items
, "none")) {
301 mask
= cpu_str_to_log_mask(items
);
310 static void do_savevm(const char *filename
)
312 if (qemu_savevm(filename
) < 0)
313 term_printf("I/O error when saving VM to '%s'\n", filename
);
316 static void do_loadvm(const char *filename
)
318 if (qemu_loadvm(filename
) < 0)
319 term_printf("I/O error when loading VM from '%s'\n", filename
);
322 static void do_stop(void)
324 vm_stop(EXCP_INTERRUPT
);
327 static void do_cont(void)
332 #ifdef CONFIG_GDBSTUB
333 static void do_gdbserver(int has_port
, int port
)
336 port
= DEFAULT_GDBSTUB_PORT
;
337 if (gdbserver_start(port
) < 0) {
338 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
340 qemu_printf("Waiting gdb connection on port %d\n", port
);
345 static void term_printc(int c
)
362 if (c
>= 32 && c
<= 126) {
363 term_printf("%c", c
);
365 term_printf("\\x%02x", c
);
372 static void memory_dump(int count
, int format
, int wsize
,
373 target_ulong addr
, int is_physical
)
375 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
385 } else if (wsize
== 4) {
388 /* as default we use the current CS size */
390 if (!(cpu_single_env
->segs
[R_CS
].flags
& DESC_B_MASK
))
394 monitor_disas(addr
, count
, is_physical
, flags
);
403 nb_per_line
= line_size
/ wsize
;
408 max_digits
= (wsize
* 8 + 2) / 3;
412 max_digits
= (wsize
* 8) / 4;
416 max_digits
= (wsize
* 8 * 10 + 32) / 33;
424 term_printf("0x%08x:", addr
);
429 cpu_physical_memory_rw(addr
, buf
, l
, 0);
431 cpu_memory_rw_debug(cpu_single_env
, addr
, buf
, l
, 0);
438 v
= ldub_raw(buf
+ i
);
441 v
= lduw_raw(buf
+ i
);
444 v
= ldl_raw(buf
+ i
);
447 v
= ldq_raw(buf
+ i
);
453 term_printf("%#*llo", max_digits
, v
);
456 term_printf("0x%0*llx", max_digits
, v
);
459 term_printf("%*llu", max_digits
, v
);
462 term_printf("%*lld", max_digits
, v
);
476 static void do_memory_dump(int count
, int format
, int size
, int addr
)
478 memory_dump(count
, format
, size
, addr
, 0);
481 static void do_physical_memory_dump(int count
, int format
, int size
, int addr
)
483 memory_dump(count
, format
, size
, addr
, 1);
486 static void do_print(int count
, int format
, int size
, int val
)
490 term_printf("%#o", val
);
493 term_printf("%#x", val
);
496 term_printf("%u", val
);
500 term_printf("%d", val
);
514 static const KeyDef key_defs
[] = {
537 { 0x0e, "backspace" },
572 { 0x3a, "caps_lock" },
583 { 0x45, "num_lock" },
584 { 0x46, "scroll_lock" },
608 static int get_keycode(const char *key
)
612 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
613 if (!strcmp(key
, p
->name
))
619 static void do_send_key(const char *string
)
622 uint8_t keycodes
[16];
624 int nb_keycodes
, keycode
, i
;
630 while (*p
!= '\0' && *p
!= '-') {
631 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
637 keycode
= get_keycode(keybuf
);
639 term_printf("unknown key: '%s'\n", keybuf
);
642 keycodes
[nb_keycodes
++] = keycode
;
647 /* key down events */
648 for(i
= 0; i
< nb_keycodes
; i
++) {
649 keycode
= keycodes
[i
];
651 kbd_put_keycode(0xe0);
652 kbd_put_keycode(keycode
& 0x7f);
655 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
656 keycode
= keycodes
[i
];
658 kbd_put_keycode(0xe0);
659 kbd_put_keycode(keycode
| 0x80);
663 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
669 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
677 val
= cpu_inb(NULL
, addr
);
681 val
= cpu_inw(NULL
, addr
);
685 val
= cpu_inl(NULL
, addr
);
689 term_printf("port%c[0x%04x] = %#0*x\n",
690 suffix
, addr
, size
* 2, val
);
693 static void do_system_reset(void)
695 qemu_system_reset_request();
698 #if defined(TARGET_I386)
699 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
701 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
704 pte
& PG_GLOBAL_MASK
? 'G' : '-',
705 pte
& PG_PSE_MASK
? 'P' : '-',
706 pte
& PG_DIRTY_MASK
? 'D' : '-',
707 pte
& PG_ACCESSED_MASK
? 'A' : '-',
708 pte
& PG_PCD_MASK
? 'C' : '-',
709 pte
& PG_PWT_MASK
? 'T' : '-',
710 pte
& PG_USER_MASK
? 'U' : '-',
711 pte
& PG_RW_MASK
? 'W' : '-');
714 static void tlb_info(void)
716 CPUState
*env
= cpu_single_env
;
718 uint32_t pgd
, pde
, pte
;
720 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
721 term_printf("PG disabled\n");
724 pgd
= env
->cr
[3] & ~0xfff;
725 for(l1
= 0; l1
< 1024; l1
++) {
726 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
727 pde
= le32_to_cpu(pde
);
728 if (pde
& PG_PRESENT_MASK
) {
729 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
730 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
732 for(l2
= 0; l2
< 1024; l2
++) {
733 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
735 pte
= le32_to_cpu(pte
);
736 if (pte
& PG_PRESENT_MASK
) {
737 print_pte((l1
<< 22) + (l2
<< 12),
747 static void mem_print(uint32_t *pstart
, int *plast_prot
,
748 uint32_t end
, int prot
)
750 if (prot
!= *plast_prot
) {
752 term_printf("%08x-%08x %08x %c%c%c\n",
753 *pstart
, end
, end
- *pstart
,
754 prot
& PG_USER_MASK
? 'u' : '-',
756 prot
& PG_RW_MASK
? 'w' : '-');
766 static void mem_info(void)
768 CPUState
*env
= cpu_single_env
;
769 int l1
, l2
, prot
, last_prot
;
770 uint32_t pgd
, pde
, pte
, start
, end
;
772 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
773 term_printf("PG disabled\n");
776 pgd
= env
->cr
[3] & ~0xfff;
779 for(l1
= 0; l1
< 1024; l1
++) {
780 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
781 pde
= le32_to_cpu(pde
);
783 if (pde
& PG_PRESENT_MASK
) {
784 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
785 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
786 mem_print(&start
, &last_prot
, end
, prot
);
788 for(l2
= 0; l2
< 1024; l2
++) {
789 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
791 pte
= le32_to_cpu(pte
);
792 end
= (l1
<< 22) + (l2
<< 12);
793 if (pte
& PG_PRESENT_MASK
) {
794 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
798 mem_print(&start
, &last_prot
, end
, prot
);
803 mem_print(&start
, &last_prot
, end
, prot
);
809 static term_cmd_t term_cmds
[] = {
810 { "help|?", "s?", do_help
,
811 "[cmd]", "show the help" },
812 { "commit", "", do_commit
,
813 "", "commit changes to the disk images (if -snapshot is used)" },
814 { "info", "s?", do_info
,
815 "subcommand", "show various information about the system state" },
816 { "q|quit", "", do_quit
,
817 "", "quit the emulator" },
818 { "eject", "-fB", do_eject
,
819 "[-f] device", "eject a removable media (use -f to force it)" },
820 { "change", "BF", do_change
,
821 "device filename", "change a removable media" },
822 { "screendump", "F", do_screen_dump
,
823 "filename", "save screen into PPM image 'filename'" },
824 { "log", "s", do_log
,
825 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
826 { "savevm", "F", do_savevm
,
827 "filename", "save the whole virtual machine state to 'filename'" },
828 { "loadvm", "F", do_loadvm
,
829 "filename", "restore the whole virtual machine state from 'filename'" },
830 { "stop", "", do_stop
,
831 "", "stop emulation", },
832 { "c|cont", "", do_cont
,
833 "", "resume emulation", },
834 #ifdef CONFIG_GDBSTUB
835 { "gdbserver", "i?", do_gdbserver
,
836 "[port]", "start gdbserver session (default port=1234)", },
838 { "x", "/i", do_memory_dump
,
839 "/fmt addr", "virtual memory dump starting at 'addr'", },
840 { "xp", "/i", do_physical_memory_dump
,
841 "/fmt addr", "physical memory dump starting at 'addr'", },
842 { "p|print", "/i", do_print
,
843 "/fmt expr", "print expression value (use $reg for CPU register access)", },
844 { "i", "/ii.", do_ioport_read
,
845 "/fmt addr", "I/O port read" },
847 { "sendkey", "s", do_send_key
,
848 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
849 { "system_reset", "", do_system_reset
,
850 "", "reset the system" },
854 static term_cmd_t info_cmds
[] = {
855 { "network", "", do_info_network
,
856 "", "show the network state" },
857 { "block", "", do_info_block
,
858 "", "show the block devices" },
859 { "registers", "", do_info_registers
,
860 "", "show the cpu registers" },
861 { "history", "", do_info_history
,
862 "", "show the command line history", },
863 { "irq", "", irq_info
,
864 "", "show the interrupts statistics (if available)", },
865 { "pic", "", pic_info
,
866 "", "show i8259 (PIC) state", },
867 { "pci", "", pci_info
,
868 "", "show PCI info", },
869 #if defined(TARGET_I386)
870 { "tlb", "", tlb_info
,
871 "", "show virtual to physical memory mappings", },
872 { "mem", "", mem_info
,
873 "", "show the active virtual memory mappings", },
878 /*******************************************************************/
880 static const char *pch
;
881 static jmp_buf expr_env
;
883 typedef struct MonitorDef
{
886 int (*get_value
)(struct MonitorDef
*md
, int val
);
889 #if defined(TARGET_I386)
890 static int monitor_get_pc (struct MonitorDef
*md
, int val
)
892 return cpu_single_env
->eip
+ (long)cpu_single_env
->segs
[R_CS
].base
;
896 #if defined(TARGET_PPC)
897 static int monitor_get_ccr (struct MonitorDef
*md
, int val
)
903 for (i
= 0; i
< 8; i
++)
904 u
|= cpu_single_env
->crf
[i
] << (32 - (4 * i
));
909 static int monitor_get_msr (struct MonitorDef
*md
, int val
)
911 return (cpu_single_env
->msr
[MSR_POW
] << MSR_POW
) |
912 (cpu_single_env
->msr
[MSR_ILE
] << MSR_ILE
) |
913 (cpu_single_env
->msr
[MSR_EE
] << MSR_EE
) |
914 (cpu_single_env
->msr
[MSR_PR
] << MSR_PR
) |
915 (cpu_single_env
->msr
[MSR_FP
] << MSR_FP
) |
916 (cpu_single_env
->msr
[MSR_ME
] << MSR_ME
) |
917 (cpu_single_env
->msr
[MSR_FE0
] << MSR_FE0
) |
918 (cpu_single_env
->msr
[MSR_SE
] << MSR_SE
) |
919 (cpu_single_env
->msr
[MSR_BE
] << MSR_BE
) |
920 (cpu_single_env
->msr
[MSR_FE1
] << MSR_FE1
) |
921 (cpu_single_env
->msr
[MSR_IP
] << MSR_IP
) |
922 (cpu_single_env
->msr
[MSR_IR
] << MSR_IR
) |
923 (cpu_single_env
->msr
[MSR_DR
] << MSR_DR
) |
924 (cpu_single_env
->msr
[MSR_RI
] << MSR_RI
) |
925 (cpu_single_env
->msr
[MSR_LE
] << MSR_LE
);
928 static int monitor_get_xer (struct MonitorDef
*md
, int val
)
930 return (cpu_single_env
->xer
[XER_SO
] << XER_SO
) |
931 (cpu_single_env
->xer
[XER_OV
] << XER_OV
) |
932 (cpu_single_env
->xer
[XER_CA
] << XER_CA
) |
933 (cpu_single_env
->xer
[XER_BC
] << XER_BC
);
936 static int monitor_get_decr (struct MonitorDef
*md
, int val
)
938 return cpu_ppc_load_decr(cpu_single_env
);
941 static int monitor_get_tbu (struct MonitorDef
*md
, int val
)
943 return cpu_ppc_load_tbu(cpu_single_env
);
946 static int monitor_get_tbl (struct MonitorDef
*md
, int val
)
948 return cpu_ppc_load_tbl(cpu_single_env
);
952 #if defined(TARGET_SPARC)
953 static int monitor_get_psr (struct MonitorDef
*md
, int val
)
955 return GET_PSR(cpu_single_env
);
958 static int monitor_get_reg(struct MonitorDef
*md
, int val
)
960 return cpu_single_env
->regwptr
[val
];
964 static MonitorDef monitor_defs
[] = {
967 #define SEG(name, seg) \
968 { name, offsetof(CPUState, segs[seg].selector) },\
969 { name ".base", offsetof(CPUState, segs[seg].base) },\
970 { name ".limit", offsetof(CPUState, segs[seg].limit) },
972 { "eax", offsetof(CPUState
, regs
[0]) },
973 { "ecx", offsetof(CPUState
, regs
[1]) },
974 { "edx", offsetof(CPUState
, regs
[2]) },
975 { "ebx", offsetof(CPUState
, regs
[3]) },
976 { "esp|sp", offsetof(CPUState
, regs
[4]) },
977 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
978 { "esi", offsetof(CPUState
, regs
[6]) },
979 { "edi", offsetof(CPUState
, regs
[7]) },
980 { "eflags", offsetof(CPUState
, eflags
) },
981 { "eip", offsetof(CPUState
, eip
) },
988 { "pc", 0, monitor_get_pc
, },
989 #elif defined(TARGET_PPC)
990 { "r0", offsetof(CPUState
, gpr
[0]) },
991 { "r1", offsetof(CPUState
, gpr
[1]) },
992 { "r2", offsetof(CPUState
, gpr
[2]) },
993 { "r3", offsetof(CPUState
, gpr
[3]) },
994 { "r4", offsetof(CPUState
, gpr
[4]) },
995 { "r5", offsetof(CPUState
, gpr
[5]) },
996 { "r6", offsetof(CPUState
, gpr
[6]) },
997 { "r7", offsetof(CPUState
, gpr
[7]) },
998 { "r8", offsetof(CPUState
, gpr
[8]) },
999 { "r9", offsetof(CPUState
, gpr
[9]) },
1000 { "r10", offsetof(CPUState
, gpr
[10]) },
1001 { "r11", offsetof(CPUState
, gpr
[11]) },
1002 { "r12", offsetof(CPUState
, gpr
[12]) },
1003 { "r13", offsetof(CPUState
, gpr
[13]) },
1004 { "r14", offsetof(CPUState
, gpr
[14]) },
1005 { "r15", offsetof(CPUState
, gpr
[15]) },
1006 { "r16", offsetof(CPUState
, gpr
[16]) },
1007 { "r17", offsetof(CPUState
, gpr
[17]) },
1008 { "r18", offsetof(CPUState
, gpr
[18]) },
1009 { "r19", offsetof(CPUState
, gpr
[19]) },
1010 { "r20", offsetof(CPUState
, gpr
[20]) },
1011 { "r21", offsetof(CPUState
, gpr
[21]) },
1012 { "r22", offsetof(CPUState
, gpr
[22]) },
1013 { "r23", offsetof(CPUState
, gpr
[23]) },
1014 { "r24", offsetof(CPUState
, gpr
[24]) },
1015 { "r25", offsetof(CPUState
, gpr
[25]) },
1016 { "r26", offsetof(CPUState
, gpr
[26]) },
1017 { "r27", offsetof(CPUState
, gpr
[27]) },
1018 { "r28", offsetof(CPUState
, gpr
[28]) },
1019 { "r29", offsetof(CPUState
, gpr
[29]) },
1020 { "r30", offsetof(CPUState
, gpr
[30]) },
1021 { "r31", offsetof(CPUState
, gpr
[31]) },
1022 { "nip|pc", offsetof(CPUState
, nip
) },
1023 { "lr", offsetof(CPUState
, lr
) },
1024 { "ctr", offsetof(CPUState
, ctr
) },
1025 { "decr", 0, &monitor_get_decr
, },
1026 { "ccr", 0, &monitor_get_ccr
, },
1027 { "msr", 0, &monitor_get_msr
, },
1028 { "xer", 0, &monitor_get_xer
, },
1029 { "tbu", 0, &monitor_get_tbu
, },
1030 { "tbl", 0, &monitor_get_tbl
, },
1031 { "sdr1", offsetof(CPUState
, sdr1
) },
1032 { "sr0", offsetof(CPUState
, sr
[0]) },
1033 { "sr1", offsetof(CPUState
, sr
[1]) },
1034 { "sr2", offsetof(CPUState
, sr
[2]) },
1035 { "sr3", offsetof(CPUState
, sr
[3]) },
1036 { "sr4", offsetof(CPUState
, sr
[4]) },
1037 { "sr5", offsetof(CPUState
, sr
[5]) },
1038 { "sr6", offsetof(CPUState
, sr
[6]) },
1039 { "sr7", offsetof(CPUState
, sr
[7]) },
1040 { "sr8", offsetof(CPUState
, sr
[8]) },
1041 { "sr9", offsetof(CPUState
, sr
[9]) },
1042 { "sr10", offsetof(CPUState
, sr
[10]) },
1043 { "sr11", offsetof(CPUState
, sr
[11]) },
1044 { "sr12", offsetof(CPUState
, sr
[12]) },
1045 { "sr13", offsetof(CPUState
, sr
[13]) },
1046 { "sr14", offsetof(CPUState
, sr
[14]) },
1047 { "sr15", offsetof(CPUState
, sr
[15]) },
1048 /* Too lazy to put BATs and SPRs ... */
1049 #elif defined(TARGET_SPARC)
1050 { "g0", offsetof(CPUState
, gregs
[0]) },
1051 { "g1", offsetof(CPUState
, gregs
[1]) },
1052 { "g2", offsetof(CPUState
, gregs
[2]) },
1053 { "g3", offsetof(CPUState
, gregs
[3]) },
1054 { "g4", offsetof(CPUState
, gregs
[4]) },
1055 { "g5", offsetof(CPUState
, gregs
[5]) },
1056 { "g6", offsetof(CPUState
, gregs
[6]) },
1057 { "g7", offsetof(CPUState
, gregs
[7]) },
1058 { "o0", 0, monitor_get_reg
},
1059 { "o1", 1, monitor_get_reg
},
1060 { "o2", 2, monitor_get_reg
},
1061 { "o3", 3, monitor_get_reg
},
1062 { "o4", 4, monitor_get_reg
},
1063 { "o5", 5, monitor_get_reg
},
1064 { "o6", 6, monitor_get_reg
},
1065 { "o7", 7, monitor_get_reg
},
1066 { "l0", 8, monitor_get_reg
},
1067 { "l1", 9, monitor_get_reg
},
1068 { "l2", 10, monitor_get_reg
},
1069 { "l3", 11, monitor_get_reg
},
1070 { "l4", 12, monitor_get_reg
},
1071 { "l5", 13, monitor_get_reg
},
1072 { "l6", 14, monitor_get_reg
},
1073 { "l7", 15, monitor_get_reg
},
1074 { "i0", 16, monitor_get_reg
},
1075 { "i1", 17, monitor_get_reg
},
1076 { "i2", 18, monitor_get_reg
},
1077 { "i3", 19, monitor_get_reg
},
1078 { "i4", 20, monitor_get_reg
},
1079 { "i5", 21, monitor_get_reg
},
1080 { "i6", 22, monitor_get_reg
},
1081 { "i7", 23, monitor_get_reg
},
1082 { "pc", offsetof(CPUState
, pc
) },
1083 { "npc", offsetof(CPUState
, npc
) },
1084 { "y", offsetof(CPUState
, y
) },
1085 { "psr", 0, &monitor_get_psr
, },
1086 { "wim", offsetof(CPUState
, wim
) },
1087 { "tbr", offsetof(CPUState
, tbr
) },
1088 { "fsr", offsetof(CPUState
, fsr
) },
1089 { "f0", offsetof(CPUState
, fpr
[0]) },
1090 { "f1", offsetof(CPUState
, fpr
[1]) },
1091 { "f2", offsetof(CPUState
, fpr
[2]) },
1092 { "f3", offsetof(CPUState
, fpr
[3]) },
1093 { "f4", offsetof(CPUState
, fpr
[4]) },
1094 { "f5", offsetof(CPUState
, fpr
[5]) },
1095 { "f6", offsetof(CPUState
, fpr
[6]) },
1096 { "f7", offsetof(CPUState
, fpr
[7]) },
1097 { "f8", offsetof(CPUState
, fpr
[8]) },
1098 { "f9", offsetof(CPUState
, fpr
[9]) },
1099 { "f10", offsetof(CPUState
, fpr
[10]) },
1100 { "f11", offsetof(CPUState
, fpr
[11]) },
1101 { "f12", offsetof(CPUState
, fpr
[12]) },
1102 { "f13", offsetof(CPUState
, fpr
[13]) },
1103 { "f14", offsetof(CPUState
, fpr
[14]) },
1104 { "f15", offsetof(CPUState
, fpr
[15]) },
1105 { "f16", offsetof(CPUState
, fpr
[16]) },
1106 { "f17", offsetof(CPUState
, fpr
[17]) },
1107 { "f18", offsetof(CPUState
, fpr
[18]) },
1108 { "f19", offsetof(CPUState
, fpr
[19]) },
1109 { "f20", offsetof(CPUState
, fpr
[20]) },
1110 { "f21", offsetof(CPUState
, fpr
[21]) },
1111 { "f22", offsetof(CPUState
, fpr
[22]) },
1112 { "f23", offsetof(CPUState
, fpr
[23]) },
1113 { "f24", offsetof(CPUState
, fpr
[24]) },
1114 { "f25", offsetof(CPUState
, fpr
[25]) },
1115 { "f26", offsetof(CPUState
, fpr
[26]) },
1116 { "f27", offsetof(CPUState
, fpr
[27]) },
1117 { "f28", offsetof(CPUState
, fpr
[28]) },
1118 { "f29", offsetof(CPUState
, fpr
[29]) },
1119 { "f30", offsetof(CPUState
, fpr
[30]) },
1120 { "f31", offsetof(CPUState
, fpr
[31]) },
1125 static void expr_error(const char *fmt
)
1129 longjmp(expr_env
, 1);
1132 static int get_monitor_def(int *pval
, const char *name
)
1135 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1136 if (compare_cmd(name
, md
->name
)) {
1137 if (md
->get_value
) {
1138 *pval
= md
->get_value(md
, md
->offset
);
1140 *pval
= *(uint32_t *)((uint8_t *)cpu_single_env
+ md
->offset
);
1148 static void next(void)
1152 while (isspace(*pch
))
1157 static int expr_sum(void);
1159 static int expr_unary(void)
1181 expr_error("')' expected");
1188 expr_error("character constant expected");
1192 expr_error("missing terminating \' character");
1201 while ((*pch
>= 'a' && *pch
<= 'z') ||
1202 (*pch
>= 'A' && *pch
<= 'Z') ||
1203 (*pch
>= '0' && *pch
<= '9') ||
1204 *pch
== '_' || *pch
== '.') {
1205 if ((q
- buf
) < sizeof(buf
) - 1)
1209 while (isspace(*pch
))
1212 if (get_monitor_def(&n
, buf
))
1213 expr_error("unknown register");
1217 expr_error("unexpected end of expression");
1221 n
= strtoul(pch
, &p
, 0);
1223 expr_error("invalid char in expression");
1226 while (isspace(*pch
))
1234 static int expr_prod(void)
1241 if (op
!= '*' && op
!= '/' && op
!= '%')
1244 val2
= expr_unary();
1253 expr_error("division by zero");
1264 static int expr_logic(void)
1271 if (op
!= '&' && op
!= '|' && op
!= '^')
1291 static int expr_sum(void)
1298 if (op
!= '+' && op
!= '-')
1301 val2
= expr_logic();
1310 static int get_expr(int *pval
, const char **pp
)
1313 if (setjmp(expr_env
)) {
1317 while (isspace(*pch
))
1324 static int get_str(char *buf
, int buf_size
, const char **pp
)
1342 while (*p
!= '\0' && *p
!= '\"') {
1358 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1361 if ((q
- buf
) < buf_size
- 1) {
1365 if ((q
- buf
) < buf_size
- 1) {
1372 qemu_printf("unterminated string\n");
1377 while (*p
!= '\0' && !isspace(*p
)) {
1378 if ((q
- buf
) < buf_size
- 1) {
1389 static int default_fmt_format
= 'x';
1390 static int default_fmt_size
= 4;
1394 static void monitor_handle_command(const char *cmdline
)
1396 const char *p
, *pstart
, *typestr
;
1398 int c
, nb_args
, len
, i
, has_arg
;
1402 void *str_allocated
[MAX_ARGS
];
1403 void *args
[MAX_ARGS
];
1406 term_printf("command='%s'\n", cmdline
);
1409 /* extract the command name */
1417 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
1420 if (len
> sizeof(cmdname
) - 1)
1421 len
= sizeof(cmdname
) - 1;
1422 memcpy(cmdname
, pstart
, len
);
1423 cmdname
[len
] = '\0';
1425 /* find the command */
1426 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1427 if (compare_cmd(cmdname
, cmd
->name
))
1430 term_printf("unknown command: '%s'\n", cmdname
);
1434 for(i
= 0; i
< MAX_ARGS
; i
++)
1435 str_allocated
[i
] = NULL
;
1437 /* parse the parameters */
1438 typestr
= cmd
->args_type
;
1455 if (*typestr
== '?') {
1458 /* no optional string: NULL argument */
1463 ret
= get_str(buf
, sizeof(buf
), &p
);
1467 term_printf("%s: filename expected\n", cmdname
);
1470 term_printf("%s: block device name expected\n", cmdname
);
1473 term_printf("%s: string expected\n", cmdname
);
1478 str
= qemu_malloc(strlen(buf
) + 1);
1480 str_allocated
[nb_args
] = str
;
1482 if (nb_args
>= MAX_ARGS
) {
1484 term_printf("%s: too many arguments\n", cmdname
);
1487 args
[nb_args
++] = str
;
1492 int count
, format
, size
;
1502 while (isdigit(*p
)) {
1503 count
= count
* 10 + (*p
- '0');
1541 if (*p
!= '\0' && !isspace(*p
)) {
1542 term_printf("invalid char in format: '%c'\n", *p
);
1546 format
= default_fmt_format
;
1547 if (format
!= 'i') {
1548 /* for 'i', not specifying a size gives -1 as size */
1550 size
= default_fmt_size
;
1552 default_fmt_size
= size
;
1553 default_fmt_format
= format
;
1556 format
= default_fmt_format
;
1557 if (format
!= 'i') {
1558 size
= default_fmt_size
;
1563 if (nb_args
+ 3 > MAX_ARGS
)
1565 args
[nb_args
++] = (void*)count
;
1566 args
[nb_args
++] = (void*)format
;
1567 args
[nb_args
++] = (void*)size
;
1575 if (*typestr
== '?' || *typestr
== '.') {
1577 if (*typestr
== '?') {
1592 if (nb_args
>= MAX_ARGS
)
1594 args
[nb_args
++] = (void *)has_arg
;
1596 if (nb_args
>= MAX_ARGS
)
1602 if (get_expr(&val
, &p
))
1605 if (nb_args
>= MAX_ARGS
)
1607 args
[nb_args
++] = (void *)val
;
1624 term_printf("%s: unsupported option -%c\n",
1631 if (nb_args
>= MAX_ARGS
)
1633 args
[nb_args
++] = (void *)has_option
;
1638 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
1642 /* check that all arguments were parsed */
1646 term_printf("%s: extraneous characters at the end of line\n",
1656 cmd
->handler(args
[0]);
1659 cmd
->handler(args
[0], args
[1]);
1662 cmd
->handler(args
[0], args
[1], args
[2]);
1665 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
1668 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
1671 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
1674 term_printf("unsupported number of arguments: %d\n", nb_args
);
1678 for(i
= 0; i
< MAX_ARGS
; i
++)
1679 qemu_free(str_allocated
[i
]);
1683 static void cmd_completion(const char *name
, const char *list
)
1685 const char *p
, *pstart
;
1694 p
= pstart
+ strlen(pstart
);
1696 if (len
> sizeof(cmd
) - 2)
1697 len
= sizeof(cmd
) - 2;
1698 memcpy(cmd
, pstart
, len
);
1700 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1701 add_completion(cmd
);
1709 static void file_completion(const char *input
)
1714 char file
[1024], file_prefix
[1024];
1718 p
= strrchr(input
, '/');
1721 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1724 input_path_len
= p
- input
+ 1;
1725 memcpy(path
, input
, input_path_len
);
1726 if (input_path_len
> sizeof(path
) - 1)
1727 input_path_len
= sizeof(path
) - 1;
1728 path
[input_path_len
] = '\0';
1729 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1731 #ifdef DEBUG_COMPLETION
1732 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
1734 ffs
= opendir(path
);
1742 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1743 memcpy(file
, input
, input_path_len
);
1744 strcpy(file
+ input_path_len
, d
->d_name
);
1745 /* stat the file to find out if it's a directory.
1746 * In that case add a slash to speed up typing long paths
1749 if(S_ISDIR(sb
.st_mode
))
1751 add_completion(file
);
1757 static void block_completion_it(void *opaque
, const char *name
)
1759 const char *input
= opaque
;
1761 if (input
[0] == '\0' ||
1762 !strncmp(name
, (char *)input
, strlen(input
))) {
1763 add_completion(name
);
1767 /* NOTE: this parser is an approximate form of the real command parser */
1768 static void parse_cmdline(const char *cmdline
,
1769 int *pnb_args
, char **args
)
1782 if (nb_args
>= MAX_ARGS
)
1784 ret
= get_str(buf
, sizeof(buf
), &p
);
1785 args
[nb_args
] = qemu_strdup(buf
);
1790 *pnb_args
= nb_args
;
1793 void readline_find_completion(const char *cmdline
)
1795 const char *cmdname
;
1796 char *args
[MAX_ARGS
];
1797 int nb_args
, i
, len
;
1798 const char *ptype
, *str
;
1801 parse_cmdline(cmdline
, &nb_args
, args
);
1802 #ifdef DEBUG_COMPLETION
1803 for(i
= 0; i
< nb_args
; i
++) {
1804 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
1808 /* if the line ends with a space, it means we want to complete the
1810 len
= strlen(cmdline
);
1811 if (len
> 0 && isspace(cmdline
[len
- 1])) {
1812 if (nb_args
>= MAX_ARGS
)
1814 args
[nb_args
++] = qemu_strdup("");
1817 /* command completion */
1822 completion_index
= strlen(cmdname
);
1823 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1824 cmd_completion(cmdname
, cmd
->name
);
1827 /* find the command */
1828 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1829 if (compare_cmd(args
[0], cmd
->name
))
1834 ptype
= cmd
->args_type
;
1835 for(i
= 0; i
< nb_args
- 2; i
++) {
1836 if (*ptype
!= '\0') {
1838 while (*ptype
== '?')
1842 str
= args
[nb_args
- 1];
1845 /* file completion */
1846 completion_index
= strlen(str
);
1847 file_completion(str
);
1850 /* block device name completion */
1851 completion_index
= strlen(str
);
1852 bdrv_iterate(block_completion_it
, (void *)str
);
1858 for(i
= 0; i
< nb_args
; i
++)
1862 static int term_can_read(void *opaque
)
1867 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
1870 for(i
= 0; i
< size
; i
++)
1871 readline_handle_byte(buf
[i
]);
1874 static void monitor_start_input(void);
1876 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
1878 monitor_handle_command(cmdline
);
1879 monitor_start_input();
1882 static void monitor_start_input(void)
1884 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
1887 void monitor_init(CharDriverState
*hd
, int show_banner
)
1891 term_printf("QEMU %s monitor - type 'help' for more information\n",
1894 qemu_chr_add_read_handler(hd
, term_can_read
, term_read
, NULL
);
1895 monitor_start_input();
1898 /* XXX: use threads ? */
1899 /* modal monitor readline */
1900 static int monitor_readline_started
;
1901 static char *monitor_readline_buf
;
1902 static int monitor_readline_buf_size
;
1904 static void monitor_readline_cb(void *opaque
, const char *input
)
1906 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
1907 monitor_readline_started
= 0;
1910 void monitor_readline(const char *prompt
, int is_password
,
1911 char *buf
, int buf_size
)
1914 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
1916 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
1917 monitor_readline_buf
= buf
;
1918 monitor_readline_buf_size
= buf_size
;
1919 monitor_readline_started
= 1;
1920 while (monitor_readline_started
) {