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 monitor_fprintf(FILE *stream
, const char *fmt
, ...)
108 term_vprintf(fmt
, ap
);
113 static int compare_cmd(const char *name
, const char *list
)
115 const char *p
, *pstart
;
123 p
= pstart
+ strlen(pstart
);
124 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
133 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
137 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
138 if (!name
|| !strcmp(name
, cmd
->name
))
139 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
143 static void help_cmd(const char *name
)
145 if (name
&& !strcmp(name
, "info")) {
146 help_cmd1(info_cmds
, "info ", NULL
);
148 help_cmd1(term_cmds
, "", name
);
149 if (name
&& !strcmp(name
, "log")) {
151 term_printf("Log items (comma separated):\n");
152 term_printf("%-10s %s\n", "none", "remove all logs");
153 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
154 term_printf("%-10s %s\n", item
->name
, item
->help
);
160 static void do_help(const char *name
)
165 static void do_commit(void)
169 for (i
= 0; i
< MAX_DISKS
; i
++) {
171 bdrv_commit(bs_table
[i
]);
176 static void do_info(const char *item
)
182 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
183 if (compare_cmd(item
, cmd
->name
))
193 static void do_info_version(void)
195 term_printf("%s\n", QEMU_VERSION
);
198 static void do_info_network(void)
203 for(i
= 0; i
< nb_nics
; i
++) {
205 term_printf("%d: ifname=%s macaddr=", i
, nd
->ifname
);
206 for(j
= 0; j
< 6; j
++) {
209 term_printf("%02x", nd
->macaddr
[j
]);
215 static void do_info_block(void)
220 static void do_info_registers(void)
223 cpu_dump_state(cpu_single_env
, stdout
, monitor_fprintf
,
224 X86_DUMP_FPU
| X86_DUMP_CCOP
);
226 cpu_dump_state(cpu_single_env
, stdout
, monitor_fprintf
,
231 static void do_info_history (void)
238 str
= readline_get_history(i
);
241 term_printf("%d: '%s'\n", i
, str
);
246 static void do_quit(void)
251 static int eject_device(BlockDriverState
*bs
, int force
)
253 if (bdrv_is_inserted(bs
)) {
255 if (!bdrv_is_removable(bs
)) {
256 term_printf("device is not removable\n");
259 if (bdrv_is_locked(bs
)) {
260 term_printf("device is locked\n");
269 static void do_eject(int force
, const char *filename
)
271 BlockDriverState
*bs
;
273 bs
= bdrv_find(filename
);
275 term_printf("device not found\n");
278 eject_device(bs
, force
);
281 static void do_change(const char *device
, const char *filename
)
283 BlockDriverState
*bs
;
287 bs
= bdrv_find(device
);
289 term_printf("device not found\n");
292 if (eject_device(bs
, 0) < 0)
294 bdrv_open(bs
, filename
, 0);
295 if (bdrv_is_encrypted(bs
)) {
296 term_printf("%s is encrypted.\n", device
);
297 for(i
= 0; i
< 3; i
++) {
298 monitor_readline("Password: ", 1, password
, sizeof(password
));
299 if (bdrv_set_key(bs
, password
) == 0)
301 term_printf("invalid password\n");
306 static void do_screen_dump(const char *filename
)
308 vga_screen_dump(filename
);
311 static void do_log(const char *items
)
315 if (!strcmp(items
, "none")) {
318 mask
= cpu_str_to_log_mask(items
);
327 static void do_savevm(const char *filename
)
329 if (qemu_savevm(filename
) < 0)
330 term_printf("I/O error when saving VM to '%s'\n", filename
);
333 static void do_loadvm(const char *filename
)
335 if (qemu_loadvm(filename
) < 0)
336 term_printf("I/O error when loading VM from '%s'\n", filename
);
339 static void do_stop(void)
341 vm_stop(EXCP_INTERRUPT
);
344 static void do_cont(void)
349 #ifdef CONFIG_GDBSTUB
350 static void do_gdbserver(int has_port
, int port
)
353 port
= DEFAULT_GDBSTUB_PORT
;
354 if (gdbserver_start(port
) < 0) {
355 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
357 qemu_printf("Waiting gdb connection on port %d\n", port
);
362 static void term_printc(int c
)
379 if (c
>= 32 && c
<= 126) {
380 term_printf("%c", c
);
382 term_printf("\\x%02x", c
);
389 static void memory_dump(int count
, int format
, int wsize
,
390 target_ulong addr
, int is_physical
)
392 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
402 } else if (wsize
== 4) {
405 /* as default we use the current CS size */
407 if (!(cpu_single_env
->segs
[R_CS
].flags
& DESC_B_MASK
))
411 monitor_disas(addr
, count
, is_physical
, flags
);
420 nb_per_line
= line_size
/ wsize
;
425 max_digits
= (wsize
* 8 + 2) / 3;
429 max_digits
= (wsize
* 8) / 4;
433 max_digits
= (wsize
* 8 * 10 + 32) / 33;
441 term_printf("0x%08x:", addr
);
446 cpu_physical_memory_rw(addr
, buf
, l
, 0);
448 cpu_memory_rw_debug(cpu_single_env
, addr
, buf
, l
, 0);
455 v
= ldub_raw(buf
+ i
);
458 v
= lduw_raw(buf
+ i
);
461 v
= ldl_raw(buf
+ i
);
464 v
= ldq_raw(buf
+ i
);
470 term_printf("%#*llo", max_digits
, v
);
473 term_printf("0x%0*llx", max_digits
, v
);
476 term_printf("%*llu", max_digits
, v
);
479 term_printf("%*lld", max_digits
, v
);
493 static void do_memory_dump(int count
, int format
, int size
, int addr
)
495 memory_dump(count
, format
, size
, addr
, 0);
498 static void do_physical_memory_dump(int count
, int format
, int size
, int addr
)
500 memory_dump(count
, format
, size
, addr
, 1);
503 static void do_print(int count
, int format
, int size
, int val
)
507 term_printf("%#o", val
);
510 term_printf("%#x", val
);
513 term_printf("%u", val
);
517 term_printf("%d", val
);
531 static const KeyDef key_defs
[] = {
554 { 0x0e, "backspace" },
589 { 0x3a, "caps_lock" },
600 { 0x45, "num_lock" },
601 { 0x46, "scroll_lock" },
625 static int get_keycode(const char *key
)
629 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
630 if (!strcmp(key
, p
->name
))
636 static void do_send_key(const char *string
)
639 uint8_t keycodes
[16];
641 int nb_keycodes
, keycode
, i
;
647 while (*p
!= '\0' && *p
!= '-') {
648 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
654 keycode
= get_keycode(keybuf
);
656 term_printf("unknown key: '%s'\n", keybuf
);
659 keycodes
[nb_keycodes
++] = keycode
;
664 /* key down events */
665 for(i
= 0; i
< nb_keycodes
; i
++) {
666 keycode
= keycodes
[i
];
668 kbd_put_keycode(0xe0);
669 kbd_put_keycode(keycode
& 0x7f);
672 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
673 keycode
= keycodes
[i
];
675 kbd_put_keycode(0xe0);
676 kbd_put_keycode(keycode
| 0x80);
680 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
686 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
694 val
= cpu_inb(NULL
, addr
);
698 val
= cpu_inw(NULL
, addr
);
702 val
= cpu_inl(NULL
, addr
);
706 term_printf("port%c[0x%04x] = %#0*x\n",
707 suffix
, addr
, size
* 2, val
);
710 static void do_system_reset(void)
712 qemu_system_reset_request();
715 #if defined(TARGET_I386)
716 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
718 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
721 pte
& PG_GLOBAL_MASK
? 'G' : '-',
722 pte
& PG_PSE_MASK
? 'P' : '-',
723 pte
& PG_DIRTY_MASK
? 'D' : '-',
724 pte
& PG_ACCESSED_MASK
? 'A' : '-',
725 pte
& PG_PCD_MASK
? 'C' : '-',
726 pte
& PG_PWT_MASK
? 'T' : '-',
727 pte
& PG_USER_MASK
? 'U' : '-',
728 pte
& PG_RW_MASK
? 'W' : '-');
731 static void tlb_info(void)
733 CPUState
*env
= cpu_single_env
;
735 uint32_t pgd
, pde
, pte
;
737 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
738 term_printf("PG disabled\n");
741 pgd
= env
->cr
[3] & ~0xfff;
742 for(l1
= 0; l1
< 1024; l1
++) {
743 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
744 pde
= le32_to_cpu(pde
);
745 if (pde
& PG_PRESENT_MASK
) {
746 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
747 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
749 for(l2
= 0; l2
< 1024; l2
++) {
750 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
752 pte
= le32_to_cpu(pte
);
753 if (pte
& PG_PRESENT_MASK
) {
754 print_pte((l1
<< 22) + (l2
<< 12),
764 static void mem_print(uint32_t *pstart
, int *plast_prot
,
765 uint32_t end
, int prot
)
771 term_printf("%08x-%08x %08x %c%c%c\n",
772 *pstart
, end
, end
- *pstart
,
773 prot1
& PG_USER_MASK
? 'u' : '-',
775 prot1
& PG_RW_MASK
? 'w' : '-');
785 static void mem_info(void)
787 CPUState
*env
= cpu_single_env
;
788 int l1
, l2
, prot
, last_prot
;
789 uint32_t pgd
, pde
, pte
, start
, end
;
791 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
792 term_printf("PG disabled\n");
795 pgd
= env
->cr
[3] & ~0xfff;
798 for(l1
= 0; l1
< 1024; l1
++) {
799 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
800 pde
= le32_to_cpu(pde
);
802 if (pde
& PG_PRESENT_MASK
) {
803 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
804 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
805 mem_print(&start
, &last_prot
, end
, prot
);
807 for(l2
= 0; l2
< 1024; l2
++) {
808 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
810 pte
= le32_to_cpu(pte
);
811 end
= (l1
<< 22) + (l2
<< 12);
812 if (pte
& PG_PRESENT_MASK
) {
813 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
817 mem_print(&start
, &last_prot
, end
, prot
);
822 mem_print(&start
, &last_prot
, end
, prot
);
828 static term_cmd_t term_cmds
[] = {
829 { "help|?", "s?", do_help
,
830 "[cmd]", "show the help" },
831 { "commit", "", do_commit
,
832 "", "commit changes to the disk images (if -snapshot is used)" },
833 { "info", "s?", do_info
,
834 "subcommand", "show various information about the system state" },
835 { "q|quit", "", do_quit
,
836 "", "quit the emulator" },
837 { "eject", "-fB", do_eject
,
838 "[-f] device", "eject a removable media (use -f to force it)" },
839 { "change", "BF", do_change
,
840 "device filename", "change a removable media" },
841 { "screendump", "F", do_screen_dump
,
842 "filename", "save screen into PPM image 'filename'" },
843 { "log", "s", do_log
,
844 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
845 { "savevm", "F", do_savevm
,
846 "filename", "save the whole virtual machine state to 'filename'" },
847 { "loadvm", "F", do_loadvm
,
848 "filename", "restore the whole virtual machine state from 'filename'" },
849 { "stop", "", do_stop
,
850 "", "stop emulation", },
851 { "c|cont", "", do_cont
,
852 "", "resume emulation", },
853 #ifdef CONFIG_GDBSTUB
854 { "gdbserver", "i?", do_gdbserver
,
855 "[port]", "start gdbserver session (default port=1234)", },
857 { "x", "/i", do_memory_dump
,
858 "/fmt addr", "virtual memory dump starting at 'addr'", },
859 { "xp", "/i", do_physical_memory_dump
,
860 "/fmt addr", "physical memory dump starting at 'addr'", },
861 { "p|print", "/i", do_print
,
862 "/fmt expr", "print expression value (use $reg for CPU register access)", },
863 { "i", "/ii.", do_ioport_read
,
864 "/fmt addr", "I/O port read" },
866 { "sendkey", "s", do_send_key
,
867 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
868 { "system_reset", "", do_system_reset
,
869 "", "reset the system" },
873 static term_cmd_t info_cmds
[] = {
874 { "version", "", do_info_version
,
875 "", "show the version of qemu" },
876 { "network", "", do_info_network
,
877 "", "show the network state" },
878 { "block", "", do_info_block
,
879 "", "show the block devices" },
880 { "registers", "", do_info_registers
,
881 "", "show the cpu registers" },
882 { "history", "", do_info_history
,
883 "", "show the command line history", },
884 { "irq", "", irq_info
,
885 "", "show the interrupts statistics (if available)", },
886 { "pic", "", pic_info
,
887 "", "show i8259 (PIC) state", },
888 { "pci", "", pci_info
,
889 "", "show PCI info", },
890 #if defined(TARGET_I386)
891 { "tlb", "", tlb_info
,
892 "", "show virtual to physical memory mappings", },
893 { "mem", "", mem_info
,
894 "", "show the active virtual memory mappings", },
899 /*******************************************************************/
901 static const char *pch
;
902 static jmp_buf expr_env
;
904 typedef struct MonitorDef
{
907 int (*get_value
)(struct MonitorDef
*md
, int val
);
910 #if defined(TARGET_I386)
911 static int monitor_get_pc (struct MonitorDef
*md
, int val
)
913 return cpu_single_env
->eip
+ (long)cpu_single_env
->segs
[R_CS
].base
;
917 #if defined(TARGET_PPC)
918 static int monitor_get_ccr (struct MonitorDef
*md
, int val
)
924 for (i
= 0; i
< 8; i
++)
925 u
|= cpu_single_env
->crf
[i
] << (32 - (4 * i
));
930 static int monitor_get_msr (struct MonitorDef
*md
, int val
)
932 return (cpu_single_env
->msr
[MSR_POW
] << MSR_POW
) |
933 (cpu_single_env
->msr
[MSR_ILE
] << MSR_ILE
) |
934 (cpu_single_env
->msr
[MSR_EE
] << MSR_EE
) |
935 (cpu_single_env
->msr
[MSR_PR
] << MSR_PR
) |
936 (cpu_single_env
->msr
[MSR_FP
] << MSR_FP
) |
937 (cpu_single_env
->msr
[MSR_ME
] << MSR_ME
) |
938 (cpu_single_env
->msr
[MSR_FE0
] << MSR_FE0
) |
939 (cpu_single_env
->msr
[MSR_SE
] << MSR_SE
) |
940 (cpu_single_env
->msr
[MSR_BE
] << MSR_BE
) |
941 (cpu_single_env
->msr
[MSR_FE1
] << MSR_FE1
) |
942 (cpu_single_env
->msr
[MSR_IP
] << MSR_IP
) |
943 (cpu_single_env
->msr
[MSR_IR
] << MSR_IR
) |
944 (cpu_single_env
->msr
[MSR_DR
] << MSR_DR
) |
945 (cpu_single_env
->msr
[MSR_RI
] << MSR_RI
) |
946 (cpu_single_env
->msr
[MSR_LE
] << MSR_LE
);
949 static int monitor_get_xer (struct MonitorDef
*md
, int val
)
951 return (cpu_single_env
->xer
[XER_SO
] << XER_SO
) |
952 (cpu_single_env
->xer
[XER_OV
] << XER_OV
) |
953 (cpu_single_env
->xer
[XER_CA
] << XER_CA
) |
954 (cpu_single_env
->xer
[XER_BC
] << XER_BC
);
957 static int monitor_get_decr (struct MonitorDef
*md
, int val
)
959 return cpu_ppc_load_decr(cpu_single_env
);
962 static int monitor_get_tbu (struct MonitorDef
*md
, int val
)
964 return cpu_ppc_load_tbu(cpu_single_env
);
967 static int monitor_get_tbl (struct MonitorDef
*md
, int val
)
969 return cpu_ppc_load_tbl(cpu_single_env
);
973 #if defined(TARGET_SPARC)
974 static int monitor_get_psr (struct MonitorDef
*md
, int val
)
976 return GET_PSR(cpu_single_env
);
979 static int monitor_get_reg(struct MonitorDef
*md
, int val
)
981 return cpu_single_env
->regwptr
[val
];
985 static MonitorDef monitor_defs
[] = {
988 #define SEG(name, seg) \
989 { name, offsetof(CPUState, segs[seg].selector) },\
990 { name ".base", offsetof(CPUState, segs[seg].base) },\
991 { name ".limit", offsetof(CPUState, segs[seg].limit) },
993 { "eax", offsetof(CPUState
, regs
[0]) },
994 { "ecx", offsetof(CPUState
, regs
[1]) },
995 { "edx", offsetof(CPUState
, regs
[2]) },
996 { "ebx", offsetof(CPUState
, regs
[3]) },
997 { "esp|sp", offsetof(CPUState
, regs
[4]) },
998 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
999 { "esi", offsetof(CPUState
, regs
[6]) },
1000 { "edi", offsetof(CPUState
, regs
[7]) },
1001 { "eflags", offsetof(CPUState
, eflags
) },
1002 { "eip", offsetof(CPUState
, eip
) },
1009 { "pc", 0, monitor_get_pc
, },
1010 #elif defined(TARGET_PPC)
1011 { "r0", offsetof(CPUState
, gpr
[0]) },
1012 { "r1", offsetof(CPUState
, gpr
[1]) },
1013 { "r2", offsetof(CPUState
, gpr
[2]) },
1014 { "r3", offsetof(CPUState
, gpr
[3]) },
1015 { "r4", offsetof(CPUState
, gpr
[4]) },
1016 { "r5", offsetof(CPUState
, gpr
[5]) },
1017 { "r6", offsetof(CPUState
, gpr
[6]) },
1018 { "r7", offsetof(CPUState
, gpr
[7]) },
1019 { "r8", offsetof(CPUState
, gpr
[8]) },
1020 { "r9", offsetof(CPUState
, gpr
[9]) },
1021 { "r10", offsetof(CPUState
, gpr
[10]) },
1022 { "r11", offsetof(CPUState
, gpr
[11]) },
1023 { "r12", offsetof(CPUState
, gpr
[12]) },
1024 { "r13", offsetof(CPUState
, gpr
[13]) },
1025 { "r14", offsetof(CPUState
, gpr
[14]) },
1026 { "r15", offsetof(CPUState
, gpr
[15]) },
1027 { "r16", offsetof(CPUState
, gpr
[16]) },
1028 { "r17", offsetof(CPUState
, gpr
[17]) },
1029 { "r18", offsetof(CPUState
, gpr
[18]) },
1030 { "r19", offsetof(CPUState
, gpr
[19]) },
1031 { "r20", offsetof(CPUState
, gpr
[20]) },
1032 { "r21", offsetof(CPUState
, gpr
[21]) },
1033 { "r22", offsetof(CPUState
, gpr
[22]) },
1034 { "r23", offsetof(CPUState
, gpr
[23]) },
1035 { "r24", offsetof(CPUState
, gpr
[24]) },
1036 { "r25", offsetof(CPUState
, gpr
[25]) },
1037 { "r26", offsetof(CPUState
, gpr
[26]) },
1038 { "r27", offsetof(CPUState
, gpr
[27]) },
1039 { "r28", offsetof(CPUState
, gpr
[28]) },
1040 { "r29", offsetof(CPUState
, gpr
[29]) },
1041 { "r30", offsetof(CPUState
, gpr
[30]) },
1042 { "r31", offsetof(CPUState
, gpr
[31]) },
1043 { "nip|pc", offsetof(CPUState
, nip
) },
1044 { "lr", offsetof(CPUState
, lr
) },
1045 { "ctr", offsetof(CPUState
, ctr
) },
1046 { "decr", 0, &monitor_get_decr
, },
1047 { "ccr", 0, &monitor_get_ccr
, },
1048 { "msr", 0, &monitor_get_msr
, },
1049 { "xer", 0, &monitor_get_xer
, },
1050 { "tbu", 0, &monitor_get_tbu
, },
1051 { "tbl", 0, &monitor_get_tbl
, },
1052 { "sdr1", offsetof(CPUState
, sdr1
) },
1053 { "sr0", offsetof(CPUState
, sr
[0]) },
1054 { "sr1", offsetof(CPUState
, sr
[1]) },
1055 { "sr2", offsetof(CPUState
, sr
[2]) },
1056 { "sr3", offsetof(CPUState
, sr
[3]) },
1057 { "sr4", offsetof(CPUState
, sr
[4]) },
1058 { "sr5", offsetof(CPUState
, sr
[5]) },
1059 { "sr6", offsetof(CPUState
, sr
[6]) },
1060 { "sr7", offsetof(CPUState
, sr
[7]) },
1061 { "sr8", offsetof(CPUState
, sr
[8]) },
1062 { "sr9", offsetof(CPUState
, sr
[9]) },
1063 { "sr10", offsetof(CPUState
, sr
[10]) },
1064 { "sr11", offsetof(CPUState
, sr
[11]) },
1065 { "sr12", offsetof(CPUState
, sr
[12]) },
1066 { "sr13", offsetof(CPUState
, sr
[13]) },
1067 { "sr14", offsetof(CPUState
, sr
[14]) },
1068 { "sr15", offsetof(CPUState
, sr
[15]) },
1069 /* Too lazy to put BATs and SPRs ... */
1070 #elif defined(TARGET_SPARC)
1071 { "g0", offsetof(CPUState
, gregs
[0]) },
1072 { "g1", offsetof(CPUState
, gregs
[1]) },
1073 { "g2", offsetof(CPUState
, gregs
[2]) },
1074 { "g3", offsetof(CPUState
, gregs
[3]) },
1075 { "g4", offsetof(CPUState
, gregs
[4]) },
1076 { "g5", offsetof(CPUState
, gregs
[5]) },
1077 { "g6", offsetof(CPUState
, gregs
[6]) },
1078 { "g7", offsetof(CPUState
, gregs
[7]) },
1079 { "o0", 0, monitor_get_reg
},
1080 { "o1", 1, monitor_get_reg
},
1081 { "o2", 2, monitor_get_reg
},
1082 { "o3", 3, monitor_get_reg
},
1083 { "o4", 4, monitor_get_reg
},
1084 { "o5", 5, monitor_get_reg
},
1085 { "o6", 6, monitor_get_reg
},
1086 { "o7", 7, monitor_get_reg
},
1087 { "l0", 8, monitor_get_reg
},
1088 { "l1", 9, monitor_get_reg
},
1089 { "l2", 10, monitor_get_reg
},
1090 { "l3", 11, monitor_get_reg
},
1091 { "l4", 12, monitor_get_reg
},
1092 { "l5", 13, monitor_get_reg
},
1093 { "l6", 14, monitor_get_reg
},
1094 { "l7", 15, monitor_get_reg
},
1095 { "i0", 16, monitor_get_reg
},
1096 { "i1", 17, monitor_get_reg
},
1097 { "i2", 18, monitor_get_reg
},
1098 { "i3", 19, monitor_get_reg
},
1099 { "i4", 20, monitor_get_reg
},
1100 { "i5", 21, monitor_get_reg
},
1101 { "i6", 22, monitor_get_reg
},
1102 { "i7", 23, monitor_get_reg
},
1103 { "pc", offsetof(CPUState
, pc
) },
1104 { "npc", offsetof(CPUState
, npc
) },
1105 { "y", offsetof(CPUState
, y
) },
1106 { "psr", 0, &monitor_get_psr
, },
1107 { "wim", offsetof(CPUState
, wim
) },
1108 { "tbr", offsetof(CPUState
, tbr
) },
1109 { "fsr", offsetof(CPUState
, fsr
) },
1110 { "f0", offsetof(CPUState
, fpr
[0]) },
1111 { "f1", offsetof(CPUState
, fpr
[1]) },
1112 { "f2", offsetof(CPUState
, fpr
[2]) },
1113 { "f3", offsetof(CPUState
, fpr
[3]) },
1114 { "f4", offsetof(CPUState
, fpr
[4]) },
1115 { "f5", offsetof(CPUState
, fpr
[5]) },
1116 { "f6", offsetof(CPUState
, fpr
[6]) },
1117 { "f7", offsetof(CPUState
, fpr
[7]) },
1118 { "f8", offsetof(CPUState
, fpr
[8]) },
1119 { "f9", offsetof(CPUState
, fpr
[9]) },
1120 { "f10", offsetof(CPUState
, fpr
[10]) },
1121 { "f11", offsetof(CPUState
, fpr
[11]) },
1122 { "f12", offsetof(CPUState
, fpr
[12]) },
1123 { "f13", offsetof(CPUState
, fpr
[13]) },
1124 { "f14", offsetof(CPUState
, fpr
[14]) },
1125 { "f15", offsetof(CPUState
, fpr
[15]) },
1126 { "f16", offsetof(CPUState
, fpr
[16]) },
1127 { "f17", offsetof(CPUState
, fpr
[17]) },
1128 { "f18", offsetof(CPUState
, fpr
[18]) },
1129 { "f19", offsetof(CPUState
, fpr
[19]) },
1130 { "f20", offsetof(CPUState
, fpr
[20]) },
1131 { "f21", offsetof(CPUState
, fpr
[21]) },
1132 { "f22", offsetof(CPUState
, fpr
[22]) },
1133 { "f23", offsetof(CPUState
, fpr
[23]) },
1134 { "f24", offsetof(CPUState
, fpr
[24]) },
1135 { "f25", offsetof(CPUState
, fpr
[25]) },
1136 { "f26", offsetof(CPUState
, fpr
[26]) },
1137 { "f27", offsetof(CPUState
, fpr
[27]) },
1138 { "f28", offsetof(CPUState
, fpr
[28]) },
1139 { "f29", offsetof(CPUState
, fpr
[29]) },
1140 { "f30", offsetof(CPUState
, fpr
[30]) },
1141 { "f31", offsetof(CPUState
, fpr
[31]) },
1146 static void expr_error(const char *fmt
)
1150 longjmp(expr_env
, 1);
1153 static int get_monitor_def(int *pval
, const char *name
)
1156 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1157 if (compare_cmd(name
, md
->name
)) {
1158 if (md
->get_value
) {
1159 *pval
= md
->get_value(md
, md
->offset
);
1161 *pval
= *(uint32_t *)((uint8_t *)cpu_single_env
+ md
->offset
);
1169 static void next(void)
1173 while (isspace(*pch
))
1178 static int expr_sum(void);
1180 static int expr_unary(void)
1202 expr_error("')' expected");
1209 expr_error("character constant expected");
1213 expr_error("missing terminating \' character");
1222 while ((*pch
>= 'a' && *pch
<= 'z') ||
1223 (*pch
>= 'A' && *pch
<= 'Z') ||
1224 (*pch
>= '0' && *pch
<= '9') ||
1225 *pch
== '_' || *pch
== '.') {
1226 if ((q
- buf
) < sizeof(buf
) - 1)
1230 while (isspace(*pch
))
1233 if (get_monitor_def(&n
, buf
))
1234 expr_error("unknown register");
1238 expr_error("unexpected end of expression");
1242 n
= strtoul(pch
, &p
, 0);
1244 expr_error("invalid char in expression");
1247 while (isspace(*pch
))
1255 static int expr_prod(void)
1262 if (op
!= '*' && op
!= '/' && op
!= '%')
1265 val2
= expr_unary();
1274 expr_error("division by zero");
1285 static int expr_logic(void)
1292 if (op
!= '&' && op
!= '|' && op
!= '^')
1312 static int expr_sum(void)
1319 if (op
!= '+' && op
!= '-')
1322 val2
= expr_logic();
1331 static int get_expr(int *pval
, const char **pp
)
1334 if (setjmp(expr_env
)) {
1338 while (isspace(*pch
))
1345 static int get_str(char *buf
, int buf_size
, const char **pp
)
1363 while (*p
!= '\0' && *p
!= '\"') {
1379 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1382 if ((q
- buf
) < buf_size
- 1) {
1386 if ((q
- buf
) < buf_size
- 1) {
1393 qemu_printf("unterminated string\n");
1398 while (*p
!= '\0' && !isspace(*p
)) {
1399 if ((q
- buf
) < buf_size
- 1) {
1410 static int default_fmt_format
= 'x';
1411 static int default_fmt_size
= 4;
1415 static void monitor_handle_command(const char *cmdline
)
1417 const char *p
, *pstart
, *typestr
;
1419 int c
, nb_args
, len
, i
, has_arg
;
1423 void *str_allocated
[MAX_ARGS
];
1424 void *args
[MAX_ARGS
];
1427 term_printf("command='%s'\n", cmdline
);
1430 /* extract the command name */
1438 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
1441 if (len
> sizeof(cmdname
) - 1)
1442 len
= sizeof(cmdname
) - 1;
1443 memcpy(cmdname
, pstart
, len
);
1444 cmdname
[len
] = '\0';
1446 /* find the command */
1447 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1448 if (compare_cmd(cmdname
, cmd
->name
))
1451 term_printf("unknown command: '%s'\n", cmdname
);
1455 for(i
= 0; i
< MAX_ARGS
; i
++)
1456 str_allocated
[i
] = NULL
;
1458 /* parse the parameters */
1459 typestr
= cmd
->args_type
;
1476 if (*typestr
== '?') {
1479 /* no optional string: NULL argument */
1484 ret
= get_str(buf
, sizeof(buf
), &p
);
1488 term_printf("%s: filename expected\n", cmdname
);
1491 term_printf("%s: block device name expected\n", cmdname
);
1494 term_printf("%s: string expected\n", cmdname
);
1499 str
= qemu_malloc(strlen(buf
) + 1);
1501 str_allocated
[nb_args
] = str
;
1503 if (nb_args
>= MAX_ARGS
) {
1505 term_printf("%s: too many arguments\n", cmdname
);
1508 args
[nb_args
++] = str
;
1513 int count
, format
, size
;
1523 while (isdigit(*p
)) {
1524 count
= count
* 10 + (*p
- '0');
1562 if (*p
!= '\0' && !isspace(*p
)) {
1563 term_printf("invalid char in format: '%c'\n", *p
);
1567 format
= default_fmt_format
;
1568 if (format
!= 'i') {
1569 /* for 'i', not specifying a size gives -1 as size */
1571 size
= default_fmt_size
;
1573 default_fmt_size
= size
;
1574 default_fmt_format
= format
;
1577 format
= default_fmt_format
;
1578 if (format
!= 'i') {
1579 size
= default_fmt_size
;
1584 if (nb_args
+ 3 > MAX_ARGS
)
1586 args
[nb_args
++] = (void*)count
;
1587 args
[nb_args
++] = (void*)format
;
1588 args
[nb_args
++] = (void*)size
;
1596 if (*typestr
== '?' || *typestr
== '.') {
1598 if (*typestr
== '?') {
1613 if (nb_args
>= MAX_ARGS
)
1615 args
[nb_args
++] = (void *)has_arg
;
1617 if (nb_args
>= MAX_ARGS
)
1623 if (get_expr(&val
, &p
))
1626 if (nb_args
>= MAX_ARGS
)
1628 args
[nb_args
++] = (void *)val
;
1645 term_printf("%s: unsupported option -%c\n",
1652 if (nb_args
>= MAX_ARGS
)
1654 args
[nb_args
++] = (void *)has_option
;
1659 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
1663 /* check that all arguments were parsed */
1667 term_printf("%s: extraneous characters at the end of line\n",
1677 cmd
->handler(args
[0]);
1680 cmd
->handler(args
[0], args
[1]);
1683 cmd
->handler(args
[0], args
[1], args
[2]);
1686 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
1689 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
1692 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
1695 term_printf("unsupported number of arguments: %d\n", nb_args
);
1699 for(i
= 0; i
< MAX_ARGS
; i
++)
1700 qemu_free(str_allocated
[i
]);
1704 static void cmd_completion(const char *name
, const char *list
)
1706 const char *p
, *pstart
;
1715 p
= pstart
+ strlen(pstart
);
1717 if (len
> sizeof(cmd
) - 2)
1718 len
= sizeof(cmd
) - 2;
1719 memcpy(cmd
, pstart
, len
);
1721 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
1722 add_completion(cmd
);
1730 static void file_completion(const char *input
)
1735 char file
[1024], file_prefix
[1024];
1739 p
= strrchr(input
, '/');
1742 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
1745 input_path_len
= p
- input
+ 1;
1746 memcpy(path
, input
, input_path_len
);
1747 if (input_path_len
> sizeof(path
) - 1)
1748 input_path_len
= sizeof(path
) - 1;
1749 path
[input_path_len
] = '\0';
1750 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
1752 #ifdef DEBUG_COMPLETION
1753 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
1755 ffs
= opendir(path
);
1763 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
1764 memcpy(file
, input
, input_path_len
);
1765 strcpy(file
+ input_path_len
, d
->d_name
);
1766 /* stat the file to find out if it's a directory.
1767 * In that case add a slash to speed up typing long paths
1770 if(S_ISDIR(sb
.st_mode
))
1772 add_completion(file
);
1778 static void block_completion_it(void *opaque
, const char *name
)
1780 const char *input
= opaque
;
1782 if (input
[0] == '\0' ||
1783 !strncmp(name
, (char *)input
, strlen(input
))) {
1784 add_completion(name
);
1788 /* NOTE: this parser is an approximate form of the real command parser */
1789 static void parse_cmdline(const char *cmdline
,
1790 int *pnb_args
, char **args
)
1803 if (nb_args
>= MAX_ARGS
)
1805 ret
= get_str(buf
, sizeof(buf
), &p
);
1806 args
[nb_args
] = qemu_strdup(buf
);
1811 *pnb_args
= nb_args
;
1814 void readline_find_completion(const char *cmdline
)
1816 const char *cmdname
;
1817 char *args
[MAX_ARGS
];
1818 int nb_args
, i
, len
;
1819 const char *ptype
, *str
;
1822 parse_cmdline(cmdline
, &nb_args
, args
);
1823 #ifdef DEBUG_COMPLETION
1824 for(i
= 0; i
< nb_args
; i
++) {
1825 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
1829 /* if the line ends with a space, it means we want to complete the
1831 len
= strlen(cmdline
);
1832 if (len
> 0 && isspace(cmdline
[len
- 1])) {
1833 if (nb_args
>= MAX_ARGS
)
1835 args
[nb_args
++] = qemu_strdup("");
1838 /* command completion */
1843 completion_index
= strlen(cmdname
);
1844 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1845 cmd_completion(cmdname
, cmd
->name
);
1848 /* find the command */
1849 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1850 if (compare_cmd(args
[0], cmd
->name
))
1855 ptype
= cmd
->args_type
;
1856 for(i
= 0; i
< nb_args
- 2; i
++) {
1857 if (*ptype
!= '\0') {
1859 while (*ptype
== '?')
1863 str
= args
[nb_args
- 1];
1866 /* file completion */
1867 completion_index
= strlen(str
);
1868 file_completion(str
);
1871 /* block device name completion */
1872 completion_index
= strlen(str
);
1873 bdrv_iterate(block_completion_it
, (void *)str
);
1876 /* XXX: more generic ? */
1877 if (!strcmp(cmd
->name
, "info")) {
1878 completion_index
= strlen(str
);
1879 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
1880 cmd_completion(str
, cmd
->name
);
1888 for(i
= 0; i
< nb_args
; i
++)
1892 static int term_can_read(void *opaque
)
1897 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
1900 for(i
= 0; i
< size
; i
++)
1901 readline_handle_byte(buf
[i
]);
1904 static void monitor_start_input(void);
1906 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
1908 monitor_handle_command(cmdline
);
1909 monitor_start_input();
1912 static void monitor_start_input(void)
1914 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
1917 void monitor_init(CharDriverState
*hd
, int show_banner
)
1921 term_printf("QEMU %s monitor - type 'help' for more information\n",
1924 qemu_chr_add_read_handler(hd
, term_can_read
, term_read
, NULL
);
1925 monitor_start_input();
1928 /* XXX: use threads ? */
1929 /* modal monitor readline */
1930 static int monitor_readline_started
;
1931 static char *monitor_readline_buf
;
1932 static int monitor_readline_buf_size
;
1934 static void monitor_readline_cb(void *opaque
, const char *input
)
1936 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
1937 monitor_readline_started
= 0;
1940 void monitor_readline(const char *prompt
, int is_password
,
1941 char *buf
, int buf_size
)
1944 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
1946 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
1947 monitor_readline_buf
= buf
;
1948 monitor_readline_buf_size
= buf_size
;
1949 monitor_readline_started
= 1;
1950 while (monitor_readline_started
) {