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
26 #include "hw/pcmcia.h"
31 #include "qemu-char.h"
35 #include "audio/audio.h"
38 #include "qemu-timer.h"
41 //#define DEBUG_COMPLETION
47 * 'B' block device name
48 * 's' string (accept optional quote)
50 * 'l' target long (32 or 64 bit)
51 * '/' optional gdb-like print format (like "/10x")
53 * '?' optional type (for 'F', 's' and 'i')
57 typedef struct term_cmd_t
{
59 const char *args_type
;
66 static CharDriverState
*monitor_hd
[MAX_MON
];
67 static int hide_banner
;
69 static term_cmd_t term_cmds
[];
70 static term_cmd_t info_cmds
[];
72 static uint8_t term_outbuf
[1024];
73 static int term_outbuf_index
;
75 static void monitor_start_input(void);
77 CPUState
*mon_cpu
= NULL
;
82 if (term_outbuf_index
> 0) {
83 for (i
= 0; i
< MAX_MON
; i
++)
84 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
85 qemu_chr_write(monitor_hd
[i
], term_outbuf
, term_outbuf_index
);
86 term_outbuf_index
= 0;
90 /* flush at every end of line or if the buffer is full */
91 void term_puts(const char *str
)
99 term_outbuf
[term_outbuf_index
++] = '\r';
100 term_outbuf
[term_outbuf_index
++] = c
;
101 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
107 void term_vprintf(const char *fmt
, va_list ap
)
110 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
114 void term_printf(const char *fmt
, ...)
118 term_vprintf(fmt
, ap
);
122 void term_print_filename(const char *filename
)
126 for (i
= 0; filename
[i
]; i
++) {
127 switch (filename
[i
]) {
131 term_printf("\\%c", filename
[i
]);
143 term_printf("%c", filename
[i
]);
149 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
153 term_vprintf(fmt
, ap
);
158 static int compare_cmd(const char *name
, const char *list
)
160 const char *p
, *pstart
;
168 p
= pstart
+ strlen(pstart
);
169 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
178 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
182 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
183 if (!name
|| !strcmp(name
, cmd
->name
))
184 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
188 static void help_cmd(const char *name
)
190 if (name
&& !strcmp(name
, "info")) {
191 help_cmd1(info_cmds
, "info ", NULL
);
193 help_cmd1(term_cmds
, "", name
);
194 if (name
&& !strcmp(name
, "log")) {
196 term_printf("Log items (comma separated):\n");
197 term_printf("%-10s %s\n", "none", "remove all logs");
198 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
199 term_printf("%-10s %s\n", item
->name
, item
->help
);
205 static void do_help(const char *name
)
210 static void do_commit(const char *device
)
214 all_devices
= !strcmp(device
, "all");
215 for (i
= 0; i
< nb_drives
; i
++) {
217 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
218 bdrv_commit(drives_table
[i
].bdrv
);
222 static void do_info(const char *item
)
225 void (*handler
)(void);
229 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
230 if (compare_cmd(item
, cmd
->name
))
237 handler
= cmd
->handler
;
241 static void do_info_version(void)
243 term_printf("%s\n", QEMU_VERSION
);
246 static void do_info_name(void)
249 term_printf("%s\n", qemu_name
);
252 static void do_info_block(void)
257 static void do_info_blockstats(void)
262 /* get the current CPU defined by the user */
263 static int mon_set_cpu(int cpu_index
)
267 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
268 if (env
->cpu_index
== cpu_index
) {
276 static CPUState
*mon_get_cpu(void)
284 static void do_info_registers(void)
291 cpu_dump_state(env
, NULL
, monitor_fprintf
,
294 cpu_dump_state(env
, NULL
, monitor_fprintf
,
299 static void do_info_cpus(void)
303 /* just to set the default cpu if not already done */
306 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
307 term_printf("%c CPU #%d:",
308 (env
== mon_cpu
) ? '*' : ' ',
310 #if defined(TARGET_I386)
311 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
312 #elif defined(TARGET_PPC)
313 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
314 #elif defined(TARGET_SPARC)
315 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
316 #elif defined(TARGET_MIPS)
317 term_printf(" PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
320 term_printf(" (halted)");
325 static void do_cpu_set(int index
)
327 if (mon_set_cpu(index
) < 0)
328 term_printf("Invalid CPU index\n");
331 static void do_info_jit(void)
333 dump_exec_info(NULL
, monitor_fprintf
);
336 static void do_info_history (void)
343 str
= readline_get_history(i
);
346 term_printf("%d: '%s'\n", i
, str
);
351 #if defined(TARGET_PPC)
352 /* XXX: not implemented in other targets */
353 static void do_info_cpu_stats (void)
358 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
362 static void do_quit(void)
367 static int eject_device(BlockDriverState
*bs
, int force
)
369 if (bdrv_is_inserted(bs
)) {
371 if (!bdrv_is_removable(bs
)) {
372 term_printf("device is not removable\n");
375 if (bdrv_is_locked(bs
)) {
376 term_printf("device is locked\n");
385 static void do_eject(int force
, const char *filename
)
387 BlockDriverState
*bs
;
389 bs
= bdrv_find(filename
);
391 term_printf("device not found\n");
394 eject_device(bs
, force
);
397 static void do_change_block(const char *device
, const char *filename
, const char *fmt
)
399 BlockDriverState
*bs
;
400 BlockDriver
*drv
= NULL
;
402 bs
= bdrv_find(device
);
404 term_printf("device not found\n");
408 drv
= bdrv_find_format(fmt
);
410 term_printf("invalid format %s\n", fmt
);
414 if (eject_device(bs
, 0) < 0)
416 bdrv_open2(bs
, filename
, 0, drv
);
417 qemu_key_check(bs
, filename
);
420 static void do_change_vnc(const char *target
)
422 if (strcmp(target
, "passwd") == 0 ||
423 strcmp(target
, "password") == 0) {
425 monitor_readline("Password: ", 1, password
, sizeof(password
)-1);
426 password
[sizeof(password
)-1] = '\0';
427 if (vnc_display_password(NULL
, password
) < 0)
428 term_printf("could not set VNC server password\n");
430 if (vnc_display_open(NULL
, target
) < 0)
431 term_printf("could not start VNC server on %s\n", target
);
435 static void do_change(const char *device
, const char *target
, const char *fmt
)
437 if (strcmp(device
, "vnc") == 0) {
438 do_change_vnc(target
);
440 do_change_block(device
, target
, fmt
);
444 static void do_screen_dump(const char *filename
)
446 vga_hw_screen_dump(filename
);
449 static void do_logfile(const char *filename
)
451 cpu_set_log_filename(filename
);
454 static void do_log(const char *items
)
458 if (!strcmp(items
, "none")) {
461 mask
= cpu_str_to_log_mask(items
);
470 static void do_stop(void)
472 vm_stop(EXCP_INTERRUPT
);
475 static void do_cont(void)
480 #ifdef CONFIG_GDBSTUB
481 static void do_gdbserver(const char *port
)
484 port
= DEFAULT_GDBSTUB_PORT
;
485 if (gdbserver_start(port
) < 0) {
486 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
488 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
493 static void term_printc(int c
)
510 if (c
>= 32 && c
<= 126) {
511 term_printf("%c", c
);
513 term_printf("\\x%02x", c
);
520 static void memory_dump(int count
, int format
, int wsize
,
521 target_phys_addr_t addr
, int is_physical
)
524 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
532 if (!env
&& !is_physical
)
537 } else if (wsize
== 4) {
540 /* as default we use the current CS size */
544 if ((env
->efer
& MSR_EFER_LMA
) &&
545 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
549 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
554 monitor_disas(env
, addr
, count
, is_physical
, flags
);
563 nb_per_line
= line_size
/ wsize
;
568 max_digits
= (wsize
* 8 + 2) / 3;
572 max_digits
= (wsize
* 8) / 4;
576 max_digits
= (wsize
* 8 * 10 + 32) / 33;
585 term_printf(TARGET_FMT_plx
":", addr
);
587 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
592 cpu_physical_memory_rw(addr
, buf
, l
, 0);
597 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
598 term_printf(" Cannot access memory\n");
607 v
= ldub_raw(buf
+ i
);
610 v
= lduw_raw(buf
+ i
);
613 v
= (uint32_t)ldl_raw(buf
+ i
);
616 v
= ldq_raw(buf
+ i
);
622 term_printf("%#*" PRIo64
, max_digits
, v
);
625 term_printf("0x%0*" PRIx64
, max_digits
, v
);
628 term_printf("%*" PRIu64
, max_digits
, v
);
631 term_printf("%*" PRId64
, max_digits
, v
);
645 #if TARGET_LONG_BITS == 64
646 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
648 #define GET_TLONG(h, l) (l)
651 static void do_memory_dump(int count
, int format
, int size
,
652 uint32_t addrh
, uint32_t addrl
)
654 target_long addr
= GET_TLONG(addrh
, addrl
);
655 memory_dump(count
, format
, size
, addr
, 0);
658 #if TARGET_PHYS_ADDR_BITS > 32
659 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
661 #define GET_TPHYSADDR(h, l) (l)
664 static void do_physical_memory_dump(int count
, int format
, int size
,
665 uint32_t addrh
, uint32_t addrl
)
668 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
669 memory_dump(count
, format
, size
, addr
, 1);
672 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
674 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
675 #if TARGET_PHYS_ADDR_BITS == 32
678 term_printf("%#o", val
);
681 term_printf("%#x", val
);
684 term_printf("%u", val
);
688 term_printf("%d", val
);
697 term_printf("%#" PRIo64
, val
);
700 term_printf("%#" PRIx64
, val
);
703 term_printf("%" PRIu64
, val
);
707 term_printf("%" PRId64
, val
);
717 static void do_memory_save(unsigned int valh
, unsigned int vall
,
718 uint32_t size
, const char *filename
)
721 target_long addr
= GET_TLONG(valh
, vall
);
730 f
= fopen(filename
, "wb");
732 term_printf("could not open '%s'\n", filename
);
739 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
740 fwrite(buf
, 1, l
, f
);
747 static void do_physical_memory_save(unsigned int valh
, unsigned int vall
,
748 uint32_t size
, const char *filename
)
753 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
755 f
= fopen(filename
, "wb");
757 term_printf("could not open '%s'\n", filename
);
764 cpu_physical_memory_rw(addr
, buf
, l
, 0);
765 fwrite(buf
, 1, l
, f
);
773 static void do_sum(uint32_t start
, uint32_t size
)
780 for(addr
= start
; addr
< (start
+ size
); addr
++) {
781 cpu_physical_memory_rw(addr
, buf
, 1, 0);
782 /* BSD sum algorithm ('sum' Unix command) */
783 sum
= (sum
>> 1) | (sum
<< 15);
786 term_printf("%05d\n", sum
);
794 static const KeyDef key_defs
[] = {
821 { 0x0e, "backspace" },
855 { 0x37, "asterisk" },
858 { 0x3a, "caps_lock" },
869 { 0x45, "num_lock" },
870 { 0x46, "scroll_lock" },
872 { 0xb5, "kp_divide" },
873 { 0x37, "kp_multiply" },
874 { 0x4a, "kp_subtract" },
876 { 0x9c, "kp_enter" },
877 { 0x53, "kp_decimal" },
910 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
930 static int get_keycode(const char *key
)
936 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
937 if (!strcmp(key
, p
->name
))
940 if (strstart(key
, "0x", NULL
)) {
941 ret
= strtoul(key
, &endp
, 0);
942 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
948 #define MAX_KEYCODES 16
949 static uint8_t keycodes
[MAX_KEYCODES
];
950 static int nb_pending_keycodes
;
951 static QEMUTimer
*key_timer
;
953 static void release_keys(void *opaque
)
957 while (nb_pending_keycodes
> 0) {
958 nb_pending_keycodes
--;
959 keycode
= keycodes
[nb_pending_keycodes
];
961 kbd_put_keycode(0xe0);
962 kbd_put_keycode(keycode
| 0x80);
966 static void do_sendkey(const char *string
, int has_hold_time
, int hold_time
)
968 char keyname_buf
[16];
970 int keyname_len
, keycode
, i
;
972 if (nb_pending_keycodes
> 0) {
973 qemu_del_timer(key_timer
);
980 separator
= strchr(string
, '-');
981 keyname_len
= separator
? separator
- string
: strlen(string
);
982 if (keyname_len
> 0) {
983 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
984 if (keyname_len
> sizeof(keyname_buf
) - 1) {
985 term_printf("invalid key: '%s...'\n", keyname_buf
);
988 if (i
== MAX_KEYCODES
) {
989 term_printf("too many keys\n");
992 keyname_buf
[keyname_len
] = 0;
993 keycode
= get_keycode(keyname_buf
);
995 term_printf("unknown key: '%s'\n", keyname_buf
);
998 keycodes
[i
++] = keycode
;
1002 string
= separator
+ 1;
1004 nb_pending_keycodes
= i
;
1005 /* key down events */
1006 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1007 keycode
= keycodes
[i
];
1009 kbd_put_keycode(0xe0);
1010 kbd_put_keycode(keycode
& 0x7f);
1012 /* delayed key up events */
1013 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1014 muldiv64(ticks_per_sec
, hold_time
, 1000));
1017 static int mouse_button_state
;
1019 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
1023 dx
= strtol(dx_str
, NULL
, 0);
1024 dy
= strtol(dy_str
, NULL
, 0);
1027 dz
= strtol(dz_str
, NULL
, 0);
1028 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1031 static void do_mouse_button(int button_state
)
1033 mouse_button_state
= button_state
;
1034 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1037 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1043 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1051 val
= cpu_inb(NULL
, addr
);
1055 val
= cpu_inw(NULL
, addr
);
1059 val
= cpu_inl(NULL
, addr
);
1063 term_printf("port%c[0x%04x] = %#0*x\n",
1064 suffix
, addr
, size
* 2, val
);
1067 /* boot_set handler */
1068 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1069 static void *boot_opaque
;
1071 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1073 qemu_boot_set_handler
= func
;
1074 boot_opaque
= opaque
;
1077 static void do_boot_set(const char *bootdevice
)
1081 if (qemu_boot_set_handler
) {
1082 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1084 term_printf("boot device list now set to %s\n", bootdevice
);
1086 term_printf("setting boot device list failed with error %i\n", res
);
1088 term_printf("no function defined to set boot device list for this architecture\n");
1092 static void do_system_reset(void)
1094 qemu_system_reset_request();
1097 static void do_system_powerdown(void)
1099 qemu_system_powerdown_request();
1102 #if defined(TARGET_I386)
1103 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1105 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1108 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1109 pte
& PG_PSE_MASK
? 'P' : '-',
1110 pte
& PG_DIRTY_MASK
? 'D' : '-',
1111 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1112 pte
& PG_PCD_MASK
? 'C' : '-',
1113 pte
& PG_PWT_MASK
? 'T' : '-',
1114 pte
& PG_USER_MASK
? 'U' : '-',
1115 pte
& PG_RW_MASK
? 'W' : '-');
1118 static void tlb_info(void)
1122 uint32_t pgd
, pde
, pte
;
1124 env
= mon_get_cpu();
1128 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1129 term_printf("PG disabled\n");
1132 pgd
= env
->cr
[3] & ~0xfff;
1133 for(l1
= 0; l1
< 1024; l1
++) {
1134 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1135 pde
= le32_to_cpu(pde
);
1136 if (pde
& PG_PRESENT_MASK
) {
1137 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1138 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1140 for(l2
= 0; l2
< 1024; l2
++) {
1141 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1142 (uint8_t *)&pte
, 4);
1143 pte
= le32_to_cpu(pte
);
1144 if (pte
& PG_PRESENT_MASK
) {
1145 print_pte((l1
<< 22) + (l2
<< 12),
1155 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1156 uint32_t end
, int prot
)
1159 prot1
= *plast_prot
;
1160 if (prot
!= prot1
) {
1161 if (*pstart
!= -1) {
1162 term_printf("%08x-%08x %08x %c%c%c\n",
1163 *pstart
, end
, end
- *pstart
,
1164 prot1
& PG_USER_MASK
? 'u' : '-',
1166 prot1
& PG_RW_MASK
? 'w' : '-');
1176 static void mem_info(void)
1179 int l1
, l2
, prot
, last_prot
;
1180 uint32_t pgd
, pde
, pte
, start
, end
;
1182 env
= mon_get_cpu();
1186 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1187 term_printf("PG disabled\n");
1190 pgd
= env
->cr
[3] & ~0xfff;
1193 for(l1
= 0; l1
< 1024; l1
++) {
1194 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1195 pde
= le32_to_cpu(pde
);
1197 if (pde
& PG_PRESENT_MASK
) {
1198 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1199 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1200 mem_print(&start
, &last_prot
, end
, prot
);
1202 for(l2
= 0; l2
< 1024; l2
++) {
1203 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1204 (uint8_t *)&pte
, 4);
1205 pte
= le32_to_cpu(pte
);
1206 end
= (l1
<< 22) + (l2
<< 12);
1207 if (pte
& PG_PRESENT_MASK
) {
1208 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1212 mem_print(&start
, &last_prot
, end
, prot
);
1217 mem_print(&start
, &last_prot
, end
, prot
);
1223 static void do_info_kqemu(void)
1229 env
= mon_get_cpu();
1231 term_printf("No cpu initialized yet");
1234 val
= env
->kqemu_enabled
;
1235 term_printf("kqemu support: ");
1239 term_printf("disabled\n");
1242 term_printf("enabled for user code\n");
1245 term_printf("enabled for user and kernel code\n");
1249 term_printf("kqemu support: not compiled\n");
1253 #ifdef CONFIG_PROFILER
1257 int64_t kqemu_exec_count
;
1259 int64_t kqemu_ret_int_count
;
1260 int64_t kqemu_ret_excp_count
;
1261 int64_t kqemu_ret_intr_count
;
1263 static void do_info_profile(void)
1269 term_printf("async time %" PRId64
" (%0.3f)\n",
1270 dev_time
, dev_time
/ (double)ticks_per_sec
);
1271 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1272 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1273 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1274 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1275 kqemu_time
/ (double)total
* 100.0,
1277 kqemu_ret_int_count
,
1278 kqemu_ret_excp_count
,
1279 kqemu_ret_intr_count
);
1282 kqemu_exec_count
= 0;
1284 kqemu_ret_int_count
= 0;
1285 kqemu_ret_excp_count
= 0;
1286 kqemu_ret_intr_count
= 0;
1288 kqemu_record_dump();
1292 static void do_info_profile(void)
1294 term_printf("Internal profiler not compiled\n");
1298 /* Capture support */
1299 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1301 static void do_info_capture (void)
1306 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1307 term_printf ("[%d]: ", i
);
1308 s
->ops
.info (s
->opaque
);
1312 static void do_stop_capture (int n
)
1317 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1319 s
->ops
.destroy (s
->opaque
);
1320 LIST_REMOVE (s
, entries
);
1328 int wav_start_capture (CaptureState
*s
, const char *path
, int freq
,
1329 int bits
, int nchannels
);
1331 static void do_wav_capture (const char *path
,
1332 int has_freq
, int freq
,
1333 int has_bits
, int bits
,
1334 int has_channels
, int nchannels
)
1338 s
= qemu_mallocz (sizeof (*s
));
1340 term_printf ("Not enough memory to add wave capture\n");
1344 freq
= has_freq
? freq
: 44100;
1345 bits
= has_bits
? bits
: 16;
1346 nchannels
= has_channels
? nchannels
: 2;
1348 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1349 term_printf ("Faied to add wave capture\n");
1352 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1356 #if defined(TARGET_I386)
1357 static void do_inject_nmi(int cpu_index
)
1361 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1362 if (env
->cpu_index
== cpu_index
) {
1363 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1369 static term_cmd_t term_cmds
[] = {
1370 { "help|?", "s?", do_help
,
1371 "[cmd]", "show the help" },
1372 { "commit", "s", do_commit
,
1373 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1374 { "info", "s?", do_info
,
1375 "subcommand", "show various information about the system state" },
1376 { "q|quit", "", do_quit
,
1377 "", "quit the emulator" },
1378 { "eject", "-fB", do_eject
,
1379 "[-f] device", "eject a removable medium (use -f to force it)" },
1380 { "change", "BFs?", do_change
,
1381 "device filename [format]", "change a removable medium, optional format" },
1382 { "screendump", "F", do_screen_dump
,
1383 "filename", "save screen into PPM image 'filename'" },
1384 { "logfile", "F", do_logfile
,
1385 "filename", "output logs to 'filename'" },
1386 { "log", "s", do_log
,
1387 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1388 { "savevm", "s?", do_savevm
,
1389 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1390 { "loadvm", "s", do_loadvm
,
1391 "tag|id", "restore a VM snapshot from its tag or id" },
1392 { "delvm", "s", do_delvm
,
1393 "tag|id", "delete a VM snapshot from its tag or id" },
1394 { "stop", "", do_stop
,
1395 "", "stop emulation", },
1396 { "c|cont", "", do_cont
,
1397 "", "resume emulation", },
1398 #ifdef CONFIG_GDBSTUB
1399 { "gdbserver", "s?", do_gdbserver
,
1400 "[port]", "start gdbserver session (default port=1234)", },
1402 { "x", "/l", do_memory_dump
,
1403 "/fmt addr", "virtual memory dump starting at 'addr'", },
1404 { "xp", "/l", do_physical_memory_dump
,
1405 "/fmt addr", "physical memory dump starting at 'addr'", },
1406 { "p|print", "/l", do_print
,
1407 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1408 { "i", "/ii.", do_ioport_read
,
1409 "/fmt addr", "I/O port read" },
1411 { "sendkey", "si?", do_sendkey
,
1412 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1413 { "system_reset", "", do_system_reset
,
1414 "", "reset the system" },
1415 { "system_powerdown", "", do_system_powerdown
,
1416 "", "send system power down event" },
1417 { "sum", "ii", do_sum
,
1418 "addr size", "compute the checksum of a memory region" },
1419 { "usb_add", "s", do_usb_add
,
1420 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1421 { "usb_del", "s", do_usb_del
,
1422 "device", "remove USB device 'bus.addr'" },
1423 { "cpu", "i", do_cpu_set
,
1424 "index", "set the default CPU" },
1425 { "mouse_move", "sss?", do_mouse_move
,
1426 "dx dy [dz]", "send mouse move events" },
1427 { "mouse_button", "i", do_mouse_button
,
1428 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1429 { "mouse_set", "i", do_mouse_set
,
1430 "index", "set which mouse device receives events" },
1432 { "wavcapture", "si?i?i?", do_wav_capture
,
1433 "path [frequency bits channels]",
1434 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1436 { "stopcapture", "i", do_stop_capture
,
1437 "capture index", "stop capture" },
1438 { "memsave", "lis", do_memory_save
,
1439 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1440 { "pmemsave", "lis", do_physical_memory_save
,
1441 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1442 { "boot_set", "s", do_boot_set
,
1443 "bootdevice", "define new values for the boot device list" },
1444 #if defined(TARGET_I386)
1445 { "nmi", "i", do_inject_nmi
,
1446 "cpu", "inject an NMI on the given CPU", },
1451 static term_cmd_t info_cmds
[] = {
1452 { "version", "", do_info_version
,
1453 "", "show the version of qemu" },
1454 { "network", "", do_info_network
,
1455 "", "show the network state" },
1456 { "block", "", do_info_block
,
1457 "", "show the block devices" },
1458 { "blockstats", "", do_info_blockstats
,
1459 "", "show block device statistics" },
1460 { "registers", "", do_info_registers
,
1461 "", "show the cpu registers" },
1462 { "cpus", "", do_info_cpus
,
1463 "", "show infos for each CPU" },
1464 { "history", "", do_info_history
,
1465 "", "show the command line history", },
1466 { "irq", "", irq_info
,
1467 "", "show the interrupts statistics (if available)", },
1468 { "pic", "", pic_info
,
1469 "", "show i8259 (PIC) state", },
1470 { "pci", "", pci_info
,
1471 "", "show PCI info", },
1472 #if defined(TARGET_I386)
1473 { "tlb", "", tlb_info
,
1474 "", "show virtual to physical memory mappings", },
1475 { "mem", "", mem_info
,
1476 "", "show the active virtual memory mappings", },
1478 { "jit", "", do_info_jit
,
1479 "", "show dynamic compiler info", },
1480 { "kqemu", "", do_info_kqemu
,
1481 "", "show kqemu information", },
1482 { "usb", "", usb_info
,
1483 "", "show guest USB devices", },
1484 { "usbhost", "", usb_host_info
,
1485 "", "show host USB devices", },
1486 { "profile", "", do_info_profile
,
1487 "", "show profiling information", },
1488 { "capture", "", do_info_capture
,
1489 "", "show capture information" },
1490 { "snapshots", "", do_info_snapshots
,
1491 "", "show the currently saved VM snapshots" },
1492 { "pcmcia", "", pcmcia_info
,
1493 "", "show guest PCMCIA status" },
1494 { "mice", "", do_info_mice
,
1495 "", "show which guest mouse is receiving events" },
1496 { "vnc", "", do_info_vnc
,
1497 "", "show the vnc server status"},
1498 { "name", "", do_info_name
,
1499 "", "show the current VM name" },
1500 #if defined(TARGET_PPC)
1501 { "cpustats", "", do_info_cpu_stats
,
1502 "", "show CPU statistics", },
1504 #if defined(CONFIG_SLIRP)
1505 { "slirp", "", do_info_slirp
,
1506 "", "show SLIRP statistics", },
1511 /*******************************************************************/
1513 static const char *pch
;
1514 static jmp_buf expr_env
;
1519 typedef struct MonitorDef
{
1522 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
1526 #if defined(TARGET_I386)
1527 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
1529 CPUState
*env
= mon_get_cpu();
1532 return env
->eip
+ env
->segs
[R_CS
].base
;
1536 #if defined(TARGET_PPC)
1537 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
1539 CPUState
*env
= mon_get_cpu();
1547 for (i
= 0; i
< 8; i
++)
1548 u
|= env
->crf
[i
] << (32 - (4 * i
));
1553 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
1555 CPUState
*env
= mon_get_cpu();
1561 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
1563 CPUState
*env
= mon_get_cpu();
1566 return ppc_load_xer(env
);
1569 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1571 CPUState
*env
= mon_get_cpu();
1574 return cpu_ppc_load_decr(env
);
1577 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1579 CPUState
*env
= mon_get_cpu();
1582 return cpu_ppc_load_tbu(env
);
1585 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1587 CPUState
*env
= mon_get_cpu();
1590 return cpu_ppc_load_tbl(env
);
1594 #if defined(TARGET_SPARC)
1595 #ifndef TARGET_SPARC64
1596 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1598 CPUState
*env
= mon_get_cpu();
1601 return GET_PSR(env
);
1605 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1607 CPUState
*env
= mon_get_cpu();
1610 return env
->regwptr
[val
];
1614 static MonitorDef monitor_defs
[] = {
1617 #define SEG(name, seg) \
1618 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1619 { name ".base", offsetof(CPUState, segs[seg].base) },\
1620 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1622 { "eax", offsetof(CPUState
, regs
[0]) },
1623 { "ecx", offsetof(CPUState
, regs
[1]) },
1624 { "edx", offsetof(CPUState
, regs
[2]) },
1625 { "ebx", offsetof(CPUState
, regs
[3]) },
1626 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1627 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1628 { "esi", offsetof(CPUState
, regs
[6]) },
1629 { "edi", offsetof(CPUState
, regs
[7]) },
1630 #ifdef TARGET_X86_64
1631 { "r8", offsetof(CPUState
, regs
[8]) },
1632 { "r9", offsetof(CPUState
, regs
[9]) },
1633 { "r10", offsetof(CPUState
, regs
[10]) },
1634 { "r11", offsetof(CPUState
, regs
[11]) },
1635 { "r12", offsetof(CPUState
, regs
[12]) },
1636 { "r13", offsetof(CPUState
, regs
[13]) },
1637 { "r14", offsetof(CPUState
, regs
[14]) },
1638 { "r15", offsetof(CPUState
, regs
[15]) },
1640 { "eflags", offsetof(CPUState
, eflags
) },
1641 { "eip", offsetof(CPUState
, eip
) },
1648 { "pc", 0, monitor_get_pc
, },
1649 #elif defined(TARGET_PPC)
1650 /* General purpose registers */
1651 { "r0", offsetof(CPUState
, gpr
[0]) },
1652 { "r1", offsetof(CPUState
, gpr
[1]) },
1653 { "r2", offsetof(CPUState
, gpr
[2]) },
1654 { "r3", offsetof(CPUState
, gpr
[3]) },
1655 { "r4", offsetof(CPUState
, gpr
[4]) },
1656 { "r5", offsetof(CPUState
, gpr
[5]) },
1657 { "r6", offsetof(CPUState
, gpr
[6]) },
1658 { "r7", offsetof(CPUState
, gpr
[7]) },
1659 { "r8", offsetof(CPUState
, gpr
[8]) },
1660 { "r9", offsetof(CPUState
, gpr
[9]) },
1661 { "r10", offsetof(CPUState
, gpr
[10]) },
1662 { "r11", offsetof(CPUState
, gpr
[11]) },
1663 { "r12", offsetof(CPUState
, gpr
[12]) },
1664 { "r13", offsetof(CPUState
, gpr
[13]) },
1665 { "r14", offsetof(CPUState
, gpr
[14]) },
1666 { "r15", offsetof(CPUState
, gpr
[15]) },
1667 { "r16", offsetof(CPUState
, gpr
[16]) },
1668 { "r17", offsetof(CPUState
, gpr
[17]) },
1669 { "r18", offsetof(CPUState
, gpr
[18]) },
1670 { "r19", offsetof(CPUState
, gpr
[19]) },
1671 { "r20", offsetof(CPUState
, gpr
[20]) },
1672 { "r21", offsetof(CPUState
, gpr
[21]) },
1673 { "r22", offsetof(CPUState
, gpr
[22]) },
1674 { "r23", offsetof(CPUState
, gpr
[23]) },
1675 { "r24", offsetof(CPUState
, gpr
[24]) },
1676 { "r25", offsetof(CPUState
, gpr
[25]) },
1677 { "r26", offsetof(CPUState
, gpr
[26]) },
1678 { "r27", offsetof(CPUState
, gpr
[27]) },
1679 { "r28", offsetof(CPUState
, gpr
[28]) },
1680 { "r29", offsetof(CPUState
, gpr
[29]) },
1681 { "r30", offsetof(CPUState
, gpr
[30]) },
1682 { "r31", offsetof(CPUState
, gpr
[31]) },
1683 /* Floating point registers */
1684 { "f0", offsetof(CPUState
, fpr
[0]) },
1685 { "f1", offsetof(CPUState
, fpr
[1]) },
1686 { "f2", offsetof(CPUState
, fpr
[2]) },
1687 { "f3", offsetof(CPUState
, fpr
[3]) },
1688 { "f4", offsetof(CPUState
, fpr
[4]) },
1689 { "f5", offsetof(CPUState
, fpr
[5]) },
1690 { "f6", offsetof(CPUState
, fpr
[6]) },
1691 { "f7", offsetof(CPUState
, fpr
[7]) },
1692 { "f8", offsetof(CPUState
, fpr
[8]) },
1693 { "f9", offsetof(CPUState
, fpr
[9]) },
1694 { "f10", offsetof(CPUState
, fpr
[10]) },
1695 { "f11", offsetof(CPUState
, fpr
[11]) },
1696 { "f12", offsetof(CPUState
, fpr
[12]) },
1697 { "f13", offsetof(CPUState
, fpr
[13]) },
1698 { "f14", offsetof(CPUState
, fpr
[14]) },
1699 { "f15", offsetof(CPUState
, fpr
[15]) },
1700 { "f16", offsetof(CPUState
, fpr
[16]) },
1701 { "f17", offsetof(CPUState
, fpr
[17]) },
1702 { "f18", offsetof(CPUState
, fpr
[18]) },
1703 { "f19", offsetof(CPUState
, fpr
[19]) },
1704 { "f20", offsetof(CPUState
, fpr
[20]) },
1705 { "f21", offsetof(CPUState
, fpr
[21]) },
1706 { "f22", offsetof(CPUState
, fpr
[22]) },
1707 { "f23", offsetof(CPUState
, fpr
[23]) },
1708 { "f24", offsetof(CPUState
, fpr
[24]) },
1709 { "f25", offsetof(CPUState
, fpr
[25]) },
1710 { "f26", offsetof(CPUState
, fpr
[26]) },
1711 { "f27", offsetof(CPUState
, fpr
[27]) },
1712 { "f28", offsetof(CPUState
, fpr
[28]) },
1713 { "f29", offsetof(CPUState
, fpr
[29]) },
1714 { "f30", offsetof(CPUState
, fpr
[30]) },
1715 { "f31", offsetof(CPUState
, fpr
[31]) },
1716 { "fpscr", offsetof(CPUState
, fpscr
) },
1717 /* Next instruction pointer */
1718 { "nip|pc", offsetof(CPUState
, nip
) },
1719 { "lr", offsetof(CPUState
, lr
) },
1720 { "ctr", offsetof(CPUState
, ctr
) },
1721 { "decr", 0, &monitor_get_decr
, },
1722 { "ccr", 0, &monitor_get_ccr
, },
1723 /* Machine state register */
1724 { "msr", 0, &monitor_get_msr
, },
1725 { "xer", 0, &monitor_get_xer
, },
1726 { "tbu", 0, &monitor_get_tbu
, },
1727 { "tbl", 0, &monitor_get_tbl
, },
1728 #if defined(TARGET_PPC64)
1729 /* Address space register */
1730 { "asr", offsetof(CPUState
, asr
) },
1732 /* Segment registers */
1733 { "sdr1", offsetof(CPUState
, sdr1
) },
1734 { "sr0", offsetof(CPUState
, sr
[0]) },
1735 { "sr1", offsetof(CPUState
, sr
[1]) },
1736 { "sr2", offsetof(CPUState
, sr
[2]) },
1737 { "sr3", offsetof(CPUState
, sr
[3]) },
1738 { "sr4", offsetof(CPUState
, sr
[4]) },
1739 { "sr5", offsetof(CPUState
, sr
[5]) },
1740 { "sr6", offsetof(CPUState
, sr
[6]) },
1741 { "sr7", offsetof(CPUState
, sr
[7]) },
1742 { "sr8", offsetof(CPUState
, sr
[8]) },
1743 { "sr9", offsetof(CPUState
, sr
[9]) },
1744 { "sr10", offsetof(CPUState
, sr
[10]) },
1745 { "sr11", offsetof(CPUState
, sr
[11]) },
1746 { "sr12", offsetof(CPUState
, sr
[12]) },
1747 { "sr13", offsetof(CPUState
, sr
[13]) },
1748 { "sr14", offsetof(CPUState
, sr
[14]) },
1749 { "sr15", offsetof(CPUState
, sr
[15]) },
1750 /* Too lazy to put BATs and SPRs ... */
1751 #elif defined(TARGET_SPARC)
1752 { "g0", offsetof(CPUState
, gregs
[0]) },
1753 { "g1", offsetof(CPUState
, gregs
[1]) },
1754 { "g2", offsetof(CPUState
, gregs
[2]) },
1755 { "g3", offsetof(CPUState
, gregs
[3]) },
1756 { "g4", offsetof(CPUState
, gregs
[4]) },
1757 { "g5", offsetof(CPUState
, gregs
[5]) },
1758 { "g6", offsetof(CPUState
, gregs
[6]) },
1759 { "g7", offsetof(CPUState
, gregs
[7]) },
1760 { "o0", 0, monitor_get_reg
},
1761 { "o1", 1, monitor_get_reg
},
1762 { "o2", 2, monitor_get_reg
},
1763 { "o3", 3, monitor_get_reg
},
1764 { "o4", 4, monitor_get_reg
},
1765 { "o5", 5, monitor_get_reg
},
1766 { "o6", 6, monitor_get_reg
},
1767 { "o7", 7, monitor_get_reg
},
1768 { "l0", 8, monitor_get_reg
},
1769 { "l1", 9, monitor_get_reg
},
1770 { "l2", 10, monitor_get_reg
},
1771 { "l3", 11, monitor_get_reg
},
1772 { "l4", 12, monitor_get_reg
},
1773 { "l5", 13, monitor_get_reg
},
1774 { "l6", 14, monitor_get_reg
},
1775 { "l7", 15, monitor_get_reg
},
1776 { "i0", 16, monitor_get_reg
},
1777 { "i1", 17, monitor_get_reg
},
1778 { "i2", 18, monitor_get_reg
},
1779 { "i3", 19, monitor_get_reg
},
1780 { "i4", 20, monitor_get_reg
},
1781 { "i5", 21, monitor_get_reg
},
1782 { "i6", 22, monitor_get_reg
},
1783 { "i7", 23, monitor_get_reg
},
1784 { "pc", offsetof(CPUState
, pc
) },
1785 { "npc", offsetof(CPUState
, npc
) },
1786 { "y", offsetof(CPUState
, y
) },
1787 #ifndef TARGET_SPARC64
1788 { "psr", 0, &monitor_get_psr
, },
1789 { "wim", offsetof(CPUState
, wim
) },
1791 { "tbr", offsetof(CPUState
, tbr
) },
1792 { "fsr", offsetof(CPUState
, fsr
) },
1793 { "f0", offsetof(CPUState
, fpr
[0]) },
1794 { "f1", offsetof(CPUState
, fpr
[1]) },
1795 { "f2", offsetof(CPUState
, fpr
[2]) },
1796 { "f3", offsetof(CPUState
, fpr
[3]) },
1797 { "f4", offsetof(CPUState
, fpr
[4]) },
1798 { "f5", offsetof(CPUState
, fpr
[5]) },
1799 { "f6", offsetof(CPUState
, fpr
[6]) },
1800 { "f7", offsetof(CPUState
, fpr
[7]) },
1801 { "f8", offsetof(CPUState
, fpr
[8]) },
1802 { "f9", offsetof(CPUState
, fpr
[9]) },
1803 { "f10", offsetof(CPUState
, fpr
[10]) },
1804 { "f11", offsetof(CPUState
, fpr
[11]) },
1805 { "f12", offsetof(CPUState
, fpr
[12]) },
1806 { "f13", offsetof(CPUState
, fpr
[13]) },
1807 { "f14", offsetof(CPUState
, fpr
[14]) },
1808 { "f15", offsetof(CPUState
, fpr
[15]) },
1809 { "f16", offsetof(CPUState
, fpr
[16]) },
1810 { "f17", offsetof(CPUState
, fpr
[17]) },
1811 { "f18", offsetof(CPUState
, fpr
[18]) },
1812 { "f19", offsetof(CPUState
, fpr
[19]) },
1813 { "f20", offsetof(CPUState
, fpr
[20]) },
1814 { "f21", offsetof(CPUState
, fpr
[21]) },
1815 { "f22", offsetof(CPUState
, fpr
[22]) },
1816 { "f23", offsetof(CPUState
, fpr
[23]) },
1817 { "f24", offsetof(CPUState
, fpr
[24]) },
1818 { "f25", offsetof(CPUState
, fpr
[25]) },
1819 { "f26", offsetof(CPUState
, fpr
[26]) },
1820 { "f27", offsetof(CPUState
, fpr
[27]) },
1821 { "f28", offsetof(CPUState
, fpr
[28]) },
1822 { "f29", offsetof(CPUState
, fpr
[29]) },
1823 { "f30", offsetof(CPUState
, fpr
[30]) },
1824 { "f31", offsetof(CPUState
, fpr
[31]) },
1825 #ifdef TARGET_SPARC64
1826 { "f32", offsetof(CPUState
, fpr
[32]) },
1827 { "f34", offsetof(CPUState
, fpr
[34]) },
1828 { "f36", offsetof(CPUState
, fpr
[36]) },
1829 { "f38", offsetof(CPUState
, fpr
[38]) },
1830 { "f40", offsetof(CPUState
, fpr
[40]) },
1831 { "f42", offsetof(CPUState
, fpr
[42]) },
1832 { "f44", offsetof(CPUState
, fpr
[44]) },
1833 { "f46", offsetof(CPUState
, fpr
[46]) },
1834 { "f48", offsetof(CPUState
, fpr
[48]) },
1835 { "f50", offsetof(CPUState
, fpr
[50]) },
1836 { "f52", offsetof(CPUState
, fpr
[52]) },
1837 { "f54", offsetof(CPUState
, fpr
[54]) },
1838 { "f56", offsetof(CPUState
, fpr
[56]) },
1839 { "f58", offsetof(CPUState
, fpr
[58]) },
1840 { "f60", offsetof(CPUState
, fpr
[60]) },
1841 { "f62", offsetof(CPUState
, fpr
[62]) },
1842 { "asi", offsetof(CPUState
, asi
) },
1843 { "pstate", offsetof(CPUState
, pstate
) },
1844 { "cansave", offsetof(CPUState
, cansave
) },
1845 { "canrestore", offsetof(CPUState
, canrestore
) },
1846 { "otherwin", offsetof(CPUState
, otherwin
) },
1847 { "wstate", offsetof(CPUState
, wstate
) },
1848 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1849 { "fprs", offsetof(CPUState
, fprs
) },
1855 static void expr_error(const char *fmt
)
1859 longjmp(expr_env
, 1);
1862 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1863 static int get_monitor_def(target_long
*pval
, const char *name
)
1868 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1869 if (compare_cmd(name
, md
->name
)) {
1870 if (md
->get_value
) {
1871 *pval
= md
->get_value(md
, md
->offset
);
1873 CPUState
*env
= mon_get_cpu();
1876 ptr
= (uint8_t *)env
+ md
->offset
;
1879 *pval
= *(int32_t *)ptr
;
1882 *pval
= *(target_long
*)ptr
;
1895 static void next(void)
1899 while (isspace(*pch
))
1904 static int64_t expr_sum(void);
1906 static int64_t expr_unary(void)
1929 expr_error("')' expected");
1936 expr_error("character constant expected");
1940 expr_error("missing terminating \' character");
1950 while ((*pch
>= 'a' && *pch
<= 'z') ||
1951 (*pch
>= 'A' && *pch
<= 'Z') ||
1952 (*pch
>= '0' && *pch
<= '9') ||
1953 *pch
== '_' || *pch
== '.') {
1954 if ((q
- buf
) < sizeof(buf
) - 1)
1958 while (isspace(*pch
))
1961 ret
= get_monitor_def(®
, buf
);
1963 expr_error("unknown register");
1965 expr_error("no cpu defined");
1970 expr_error("unexpected end of expression");
1974 #if TARGET_PHYS_ADDR_BITS > 32
1975 n
= strtoull(pch
, &p
, 0);
1977 n
= strtoul(pch
, &p
, 0);
1980 expr_error("invalid char in expression");
1983 while (isspace(*pch
))
1991 static int64_t expr_prod(void)
1999 if (op
!= '*' && op
!= '/' && op
!= '%')
2002 val2
= expr_unary();
2011 expr_error("division by zero");
2022 static int64_t expr_logic(void)
2030 if (op
!= '&' && op
!= '|' && op
!= '^')
2050 static int64_t expr_sum(void)
2058 if (op
!= '+' && op
!= '-')
2061 val2
= expr_logic();
2070 static int get_expr(int64_t *pval
, const char **pp
)
2073 if (setjmp(expr_env
)) {
2077 while (isspace(*pch
))
2084 static int get_str(char *buf
, int buf_size
, const char **pp
)
2102 while (*p
!= '\0' && *p
!= '\"') {
2118 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2121 if ((q
- buf
) < buf_size
- 1) {
2125 if ((q
- buf
) < buf_size
- 1) {
2132 qemu_printf("unterminated string\n");
2137 while (*p
!= '\0' && !isspace(*p
)) {
2138 if ((q
- buf
) < buf_size
- 1) {
2149 static int default_fmt_format
= 'x';
2150 static int default_fmt_size
= 4;
2154 static void monitor_handle_command(const char *cmdline
)
2156 const char *p
, *pstart
, *typestr
;
2158 int c
, nb_args
, len
, i
, has_arg
;
2162 void *str_allocated
[MAX_ARGS
];
2163 void *args
[MAX_ARGS
];
2164 void (*handler_0
)(void);
2165 void (*handler_1
)(void *arg0
);
2166 void (*handler_2
)(void *arg0
, void *arg1
);
2167 void (*handler_3
)(void *arg0
, void *arg1
, void *arg2
);
2168 void (*handler_4
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
);
2169 void (*handler_5
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2171 void (*handler_6
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2172 void *arg4
, void *arg5
);
2173 void (*handler_7
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2174 void *arg4
, void *arg5
, void *arg6
);
2177 term_printf("command='%s'\n", cmdline
);
2180 /* extract the command name */
2188 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
2191 if (len
> sizeof(cmdname
) - 1)
2192 len
= sizeof(cmdname
) - 1;
2193 memcpy(cmdname
, pstart
, len
);
2194 cmdname
[len
] = '\0';
2196 /* find the command */
2197 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2198 if (compare_cmd(cmdname
, cmd
->name
))
2201 term_printf("unknown command: '%s'\n", cmdname
);
2205 for(i
= 0; i
< MAX_ARGS
; i
++)
2206 str_allocated
[i
] = NULL
;
2208 /* parse the parameters */
2209 typestr
= cmd
->args_type
;
2226 if (*typestr
== '?') {
2229 /* no optional string: NULL argument */
2234 ret
= get_str(buf
, sizeof(buf
), &p
);
2238 term_printf("%s: filename expected\n", cmdname
);
2241 term_printf("%s: block device name expected\n", cmdname
);
2244 term_printf("%s: string expected\n", cmdname
);
2249 str
= qemu_malloc(strlen(buf
) + 1);
2250 pstrcpy(str
, sizeof(buf
), buf
);
2251 str_allocated
[nb_args
] = str
;
2253 if (nb_args
>= MAX_ARGS
) {
2255 term_printf("%s: too many arguments\n", cmdname
);
2258 args
[nb_args
++] = str
;
2263 int count
, format
, size
;
2273 while (isdigit(*p
)) {
2274 count
= count
* 10 + (*p
- '0');
2312 if (*p
!= '\0' && !isspace(*p
)) {
2313 term_printf("invalid char in format: '%c'\n", *p
);
2317 format
= default_fmt_format
;
2318 if (format
!= 'i') {
2319 /* for 'i', not specifying a size gives -1 as size */
2321 size
= default_fmt_size
;
2323 default_fmt_size
= size
;
2324 default_fmt_format
= format
;
2327 format
= default_fmt_format
;
2328 if (format
!= 'i') {
2329 size
= default_fmt_size
;
2334 if (nb_args
+ 3 > MAX_ARGS
)
2336 args
[nb_args
++] = (void*)(long)count
;
2337 args
[nb_args
++] = (void*)(long)format
;
2338 args
[nb_args
++] = (void*)(long)size
;
2348 if (*typestr
== '?' || *typestr
== '.') {
2349 if (*typestr
== '?') {
2365 if (nb_args
>= MAX_ARGS
)
2367 args
[nb_args
++] = (void *)(long)has_arg
;
2369 if (nb_args
>= MAX_ARGS
)
2375 if (get_expr(&val
, &p
))
2379 if (nb_args
>= MAX_ARGS
)
2381 args
[nb_args
++] = (void *)(long)val
;
2383 if ((nb_args
+ 1) >= MAX_ARGS
)
2385 #if TARGET_PHYS_ADDR_BITS > 32
2386 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2388 args
[nb_args
++] = (void *)0;
2390 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2408 term_printf("%s: unsupported option -%c\n",
2415 if (nb_args
>= MAX_ARGS
)
2417 args
[nb_args
++] = (void *)(long)has_option
;
2422 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2426 /* check that all arguments were parsed */
2430 term_printf("%s: extraneous characters at the end of line\n",
2437 handler_0
= cmd
->handler
;
2441 handler_1
= cmd
->handler
;
2445 handler_2
= cmd
->handler
;
2446 handler_2(args
[0], args
[1]);
2449 handler_3
= cmd
->handler
;
2450 handler_3(args
[0], args
[1], args
[2]);
2453 handler_4
= cmd
->handler
;
2454 handler_4(args
[0], args
[1], args
[2], args
[3]);
2457 handler_5
= cmd
->handler
;
2458 handler_5(args
[0], args
[1], args
[2], args
[3], args
[4]);
2461 handler_6
= cmd
->handler
;
2462 handler_6(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2465 handler_7
= cmd
->handler
;
2466 handler_7(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2469 term_printf("unsupported number of arguments: %d\n", nb_args
);
2473 for(i
= 0; i
< MAX_ARGS
; i
++)
2474 qemu_free(str_allocated
[i
]);
2478 static void cmd_completion(const char *name
, const char *list
)
2480 const char *p
, *pstart
;
2489 p
= pstart
+ strlen(pstart
);
2491 if (len
> sizeof(cmd
) - 2)
2492 len
= sizeof(cmd
) - 2;
2493 memcpy(cmd
, pstart
, len
);
2495 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2496 add_completion(cmd
);
2504 static void file_completion(const char *input
)
2509 char file
[1024], file_prefix
[1024];
2513 p
= strrchr(input
, '/');
2516 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2517 pstrcpy(path
, sizeof(path
), ".");
2519 input_path_len
= p
- input
+ 1;
2520 memcpy(path
, input
, input_path_len
);
2521 if (input_path_len
> sizeof(path
) - 1)
2522 input_path_len
= sizeof(path
) - 1;
2523 path
[input_path_len
] = '\0';
2524 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2526 #ifdef DEBUG_COMPLETION
2527 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2529 ffs
= opendir(path
);
2537 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2538 memcpy(file
, input
, input_path_len
);
2539 if (input_path_len
< sizeof(file
))
2540 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2542 /* stat the file to find out if it's a directory.
2543 * In that case add a slash to speed up typing long paths
2546 if(S_ISDIR(sb
.st_mode
))
2547 pstrcat(file
, sizeof(file
), "/");
2548 add_completion(file
);
2554 static void block_completion_it(void *opaque
, const char *name
)
2556 const char *input
= opaque
;
2558 if (input
[0] == '\0' ||
2559 !strncmp(name
, (char *)input
, strlen(input
))) {
2560 add_completion(name
);
2564 /* NOTE: this parser is an approximate form of the real command parser */
2565 static void parse_cmdline(const char *cmdline
,
2566 int *pnb_args
, char **args
)
2579 if (nb_args
>= MAX_ARGS
)
2581 ret
= get_str(buf
, sizeof(buf
), &p
);
2582 args
[nb_args
] = qemu_strdup(buf
);
2587 *pnb_args
= nb_args
;
2590 void readline_find_completion(const char *cmdline
)
2592 const char *cmdname
;
2593 char *args
[MAX_ARGS
];
2594 int nb_args
, i
, len
;
2595 const char *ptype
, *str
;
2599 parse_cmdline(cmdline
, &nb_args
, args
);
2600 #ifdef DEBUG_COMPLETION
2601 for(i
= 0; i
< nb_args
; i
++) {
2602 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2606 /* if the line ends with a space, it means we want to complete the
2608 len
= strlen(cmdline
);
2609 if (len
> 0 && isspace(cmdline
[len
- 1])) {
2610 if (nb_args
>= MAX_ARGS
)
2612 args
[nb_args
++] = qemu_strdup("");
2615 /* command completion */
2620 completion_index
= strlen(cmdname
);
2621 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2622 cmd_completion(cmdname
, cmd
->name
);
2625 /* find the command */
2626 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2627 if (compare_cmd(args
[0], cmd
->name
))
2632 ptype
= cmd
->args_type
;
2633 for(i
= 0; i
< nb_args
- 2; i
++) {
2634 if (*ptype
!= '\0') {
2636 while (*ptype
== '?')
2640 str
= args
[nb_args
- 1];
2643 /* file completion */
2644 completion_index
= strlen(str
);
2645 file_completion(str
);
2648 /* block device name completion */
2649 completion_index
= strlen(str
);
2650 bdrv_iterate(block_completion_it
, (void *)str
);
2653 /* XXX: more generic ? */
2654 if (!strcmp(cmd
->name
, "info")) {
2655 completion_index
= strlen(str
);
2656 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2657 cmd_completion(str
, cmd
->name
);
2659 } else if (!strcmp(cmd
->name
, "sendkey")) {
2660 completion_index
= strlen(str
);
2661 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2662 cmd_completion(str
, key
->name
);
2670 for(i
= 0; i
< nb_args
; i
++)
2674 static int term_can_read(void *opaque
)
2679 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2682 for(i
= 0; i
< size
; i
++)
2683 readline_handle_byte(buf
[i
]);
2686 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2688 monitor_handle_command(cmdline
);
2689 monitor_start_input();
2692 static void monitor_start_input(void)
2694 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2697 static void term_event(void *opaque
, int event
)
2699 if (event
!= CHR_EVENT_RESET
)
2703 term_printf("QEMU %s monitor - type 'help' for more information\n",
2705 monitor_start_input();
2708 static int is_first_init
= 1;
2710 void monitor_init(CharDriverState
*hd
, int show_banner
)
2714 if (is_first_init
) {
2715 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
2718 for (i
= 0; i
< MAX_MON
; i
++) {
2719 monitor_hd
[i
] = NULL
;
2723 for (i
= 0; i
< MAX_MON
; i
++) {
2724 if (monitor_hd
[i
] == NULL
) {
2730 hide_banner
= !show_banner
;
2732 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2734 readline_start("", 0, monitor_handle_command1
, NULL
);
2737 /* XXX: use threads ? */
2738 /* modal monitor readline */
2739 static int monitor_readline_started
;
2740 static char *monitor_readline_buf
;
2741 static int monitor_readline_buf_size
;
2743 static void monitor_readline_cb(void *opaque
, const char *input
)
2745 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2746 monitor_readline_started
= 0;
2749 void monitor_readline(const char *prompt
, int is_password
,
2750 char *buf
, int buf_size
)
2753 int old_focus
[MAX_MON
];
2756 for (i
= 0; i
< MAX_MON
; i
++) {
2758 if (monitor_hd
[i
]) {
2759 old_focus
[i
] = monitor_hd
[i
]->focus
;
2760 monitor_hd
[i
]->focus
= 0;
2761 qemu_chr_send_event(monitor_hd
[i
], CHR_EVENT_FOCUS
);
2766 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2767 monitor_readline_buf
= buf
;
2768 monitor_readline_buf_size
= buf_size
;
2769 monitor_readline_started
= 1;
2770 while (monitor_readline_started
) {
2773 /* restore original focus */
2775 for (i
= 0; i
< MAX_MON
; i
++)
2777 monitor_hd
[i
]->focus
= old_focus
[i
];