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
31 //#define DEBUG_COMPLETION
34 #define offsetof(type, field) ((size_t) &((type *)0)->field)
41 * 'B' block device name
42 * 's' string (accept optional quote)
44 * 'l' target long (32 or 64 bit)
45 * '/' optional gdb-like print format (like "/10x")
47 * '?' optional type (for 'F', 's' and 'i')
51 typedef struct term_cmd_t
{
53 const char *args_type
;
60 static CharDriverState
*monitor_hd
[MAX_MON
];
61 static int hide_banner
;
63 static term_cmd_t term_cmds
[];
64 static term_cmd_t info_cmds
[];
66 static char term_outbuf
[1024];
67 static int term_outbuf_index
;
69 static void monitor_start_input(void);
71 CPUState
*mon_cpu
= NULL
;
76 if (term_outbuf_index
> 0) {
77 for (i
= 0; i
< MAX_MON
; i
++)
78 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
79 qemu_chr_write(monitor_hd
[i
], term_outbuf
, term_outbuf_index
);
80 term_outbuf_index
= 0;
84 /* flush at every end of line or if the buffer is full */
85 void term_puts(const char *str
)
93 term_outbuf
[term_outbuf_index
++] = '\r';
94 term_outbuf
[term_outbuf_index
++] = c
;
95 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
101 void term_vprintf(const char *fmt
, va_list ap
)
104 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
108 void term_printf(const char *fmt
, ...)
112 term_vprintf(fmt
, ap
);
116 void term_print_filename(const char *filename
)
120 for (i
= 0; filename
[i
]; i
++) {
121 switch (filename
[i
]) {
125 term_printf("\\%c", filename
[i
]);
137 term_printf("%c", filename
[i
]);
143 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
147 term_vprintf(fmt
, ap
);
152 static int compare_cmd(const char *name
, const char *list
)
154 const char *p
, *pstart
;
162 p
= pstart
+ strlen(pstart
);
163 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
172 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
176 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
177 if (!name
|| !strcmp(name
, cmd
->name
))
178 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
182 static void help_cmd(const char *name
)
184 if (name
&& !strcmp(name
, "info")) {
185 help_cmd1(info_cmds
, "info ", NULL
);
187 help_cmd1(term_cmds
, "", name
);
188 if (name
&& !strcmp(name
, "log")) {
190 term_printf("Log items (comma separated):\n");
191 term_printf("%-10s %s\n", "none", "remove all logs");
192 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
193 term_printf("%-10s %s\n", item
->name
, item
->help
);
199 static void do_help(const char *name
)
204 static void do_commit(const char *device
)
208 all_devices
= !strcmp(device
, "all");
209 for (i
= 0; i
< MAX_DISKS
; i
++) {
212 !strcmp(bdrv_get_device_name(bs_table
[i
]), device
))
213 bdrv_commit(bs_table
[i
]);
217 if (all_devices
|| !strcmp(bdrv_get_device_name(mtd_bdrv
), device
))
218 bdrv_commit(mtd_bdrv
);
221 static void do_info(const char *item
)
227 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
228 if (compare_cmd(item
, cmd
->name
))
238 static void do_info_version(void)
240 term_printf("%s\n", QEMU_VERSION
);
243 static void do_info_name(void)
246 term_printf("%s\n", qemu_name
);
249 static void do_info_block(void)
254 /* get the current CPU defined by the user */
255 int mon_set_cpu(int cpu_index
)
259 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
260 if (env
->cpu_index
== cpu_index
) {
268 CPUState
*mon_get_cpu(void)
275 kvm_save_registers(mon_cpu
);
281 static void do_info_registers(void)
288 cpu_dump_state(env
, NULL
, monitor_fprintf
,
291 cpu_dump_state(env
, NULL
, monitor_fprintf
,
296 static void do_info_cpus(void)
300 /* just to set the default cpu if not already done */
303 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
304 term_printf("%c CPU #%d:",
305 (env
== mon_cpu
) ? '*' : ' ',
307 #if defined(TARGET_I386)
308 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
309 if (env
->hflags
& HF_HALTED_MASK
)
310 term_printf(" (halted)");
311 #elif defined(TARGET_PPC)
312 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
314 term_printf(" (halted)");
315 #elif defined(TARGET_SPARC)
316 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
318 term_printf(" (halted)");
319 #elif defined(TARGET_MIPS)
320 term_printf(" PC=0x" TARGET_FMT_lx
, env
->PC
[env
->current_tc
]);
322 term_printf(" (halted)");
328 static void do_cpu_set(int index
)
330 if (mon_set_cpu(index
) < 0)
331 term_printf("Invalid CPU index\n");
334 static void do_info_jit(void)
336 dump_exec_info(NULL
, monitor_fprintf
);
339 static void do_info_history (void)
346 str
= readline_get_history(i
);
349 term_printf("%d: '%s'\n", i
, str
);
354 #if defined(TARGET_PPC)
355 /* XXX: not implemented in other targets */
356 static void do_info_cpu_stats (void)
361 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
365 static void do_quit(void)
370 static int eject_device(BlockDriverState
*bs
, int force
)
372 if (bdrv_is_inserted(bs
)) {
374 if (!bdrv_is_removable(bs
)) {
375 term_printf("device is not removable\n");
378 if (bdrv_is_locked(bs
)) {
379 term_printf("device is locked\n");
388 static void do_eject(int force
, const char *filename
)
390 BlockDriverState
*bs
;
392 bs
= bdrv_find(filename
);
394 term_printf("device not found\n");
397 eject_device(bs
, force
);
400 #define USAGE_STR "Usage: read_disk_io hd[a/b/c/d]\n"
401 DiskIOStatistics
vmdk_io_statistics(BlockDriverState
*bs
);
403 static void do_io_statistics(const char *hdx
)
405 DiskIOStatistics io
[4];
407 if ((strcmp(hdx
,"hda") != 0) && (strcmp(hdx
,"hdb") != 0) &&
408 (strcmp(hdx
,"hdc") != 0) && (strcmp(hdx
,"hdd") != 0)) {
409 term_printf(USAGE_STR
);
415 term_printf("---------- hda io statistics ----------\n");
416 io
[0] = vmdk_io_statistics(bs_table
[0]);
417 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
418 io
[0].read_byte_counter
, io
[0].write_byte_counter
);
421 term_printf("---------- hdb io statistics ----------\n");
423 io
[1] = vmdk_io_statistics(bs_table
[1]);
424 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
425 io
[1].read_byte_counter
, io
[1].write_byte_counter
);
427 term_printf("hdb not exist\n");
431 term_printf("---------- hdc io statistics ----------\n");
433 io
[2] = vmdk_io_statistics(bs_table
[2]);
434 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
435 io
[2].read_byte_counter
, io
[2].write_byte_counter
);
437 term_printf("hdc not exist\n");
441 term_printf("---------- hdd io statistics ----------\n");
443 io
[3] = vmdk_io_statistics(bs_table
[3]);
444 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
445 io
[3].read_byte_counter
, io
[3].write_byte_counter
);
447 term_printf("hdd not exist\n");
453 static void do_change_block(const char *device
, const char *filename
)
455 BlockDriverState
*bs
;
457 bs
= bdrv_find(device
);
459 term_printf("device not found\n");
462 if (eject_device(bs
, 0) < 0)
464 bdrv_open(bs
, filename
, 0);
465 qemu_key_check(bs
, filename
);
468 static void do_change_vnc(const char *target
)
470 if (strcmp(target
, "passwd") == 0 ||
471 strcmp(target
, "password") == 0) {
473 monitor_readline("Password: ", 1, password
, sizeof(password
)-1);
474 password
[sizeof(password
)-1] = '\0';
475 if (vnc_display_password(NULL
, password
) < 0)
476 term_printf("could not set VNC server password\n");
478 if (vnc_display_open(NULL
, target
) < 0)
479 term_printf("could not start VNC server on %s\n", target
);
483 static void do_change(const char *device
, const char *target
)
485 if (strcmp(device
, "vnc") == 0) {
486 do_change_vnc(target
);
488 do_change_block(device
, target
);
492 static void do_screen_dump(const char *filename
)
494 vga_hw_screen_dump(filename
);
497 static void do_logfile(const char *filename
)
499 cpu_set_log_filename(filename
);
502 static void do_log(const char *items
)
506 if (!strcmp(items
, "none")) {
509 mask
= cpu_str_to_log_mask(items
);
518 static void do_stop(void)
520 vm_stop(EXCP_INTERRUPT
);
523 static void do_cont(void)
528 #ifdef CONFIG_GDBSTUB
529 static void do_gdbserver(const char *port
)
532 port
= DEFAULT_GDBSTUB_PORT
;
533 if (gdbserver_start(port
) < 0) {
534 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
536 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
541 static void term_printc(int c
)
558 if (c
>= 32 && c
<= 126) {
559 term_printf("%c", c
);
561 term_printf("\\x%02x", c
);
568 static void memory_dump(int count
, int format
, int wsize
,
569 target_phys_addr_t addr
, int is_physical
)
572 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
580 if (!env
&& !is_physical
)
585 } else if (wsize
== 4) {
588 /* as default we use the current CS size */
592 if ((env
->efer
& MSR_EFER_LMA
) &&
593 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
597 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
602 monitor_disas(env
, addr
, count
, is_physical
, flags
);
611 nb_per_line
= line_size
/ wsize
;
616 max_digits
= (wsize
* 8 + 2) / 3;
620 max_digits
= (wsize
* 8) / 4;
624 max_digits
= (wsize
* 8 * 10 + 32) / 33;
633 term_printf(TARGET_FMT_plx
":", addr
);
635 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
640 cpu_physical_memory_rw(addr
, buf
, l
, 0);
645 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
652 v
= ldub_raw(buf
+ i
);
655 v
= lduw_raw(buf
+ i
);
658 v
= (uint32_t)ldl_raw(buf
+ i
);
661 v
= ldq_raw(buf
+ i
);
667 term_printf("%#*" PRIo64
, max_digits
, v
);
670 term_printf("0x%0*" PRIx64
, max_digits
, v
);
673 term_printf("%*" PRIu64
, max_digits
, v
);
676 term_printf("%*" PRId64
, max_digits
, v
);
690 #if TARGET_LONG_BITS == 64
691 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
693 #define GET_TLONG(h, l) (l)
696 static void do_memory_dump(int count
, int format
, int size
,
697 uint32_t addrh
, uint32_t addrl
)
699 target_long addr
= GET_TLONG(addrh
, addrl
);
700 memory_dump(count
, format
, size
, addr
, 0);
703 #if TARGET_PHYS_ADDR_BITS > 32
704 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
706 #define GET_TPHYSADDR(h, l) (l)
709 static void do_physical_memory_dump(int count
, int format
, int size
,
710 uint32_t addrh
, uint32_t addrl
)
713 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
714 memory_dump(count
, format
, size
, addr
, 1);
717 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
719 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
720 #if TARGET_PHYS_ADDR_BITS == 32
723 term_printf("%#o", val
);
726 term_printf("%#x", val
);
729 term_printf("%u", val
);
733 term_printf("%d", val
);
742 term_printf("%#" PRIo64
, val
);
745 term_printf("%#" PRIx64
, val
);
748 term_printf("%" PRIu64
, val
);
752 term_printf("%" PRId64
, val
);
762 static void do_memory_save(unsigned int valh
, unsigned int vall
,
763 uint32_t size
, const char *filename
)
766 target_long addr
= GET_TLONG(valh
, vall
);
775 f
= fopen(filename
, "wb");
777 term_printf("could not open '%s'\n", filename
);
784 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
785 fwrite(buf
, 1, l
, f
);
792 static void do_sum(uint32_t start
, uint32_t size
)
799 for(addr
= start
; addr
< (start
+ size
); addr
++) {
800 cpu_physical_memory_rw(addr
, buf
, 1, 0);
801 /* BSD sum algorithm ('sum' Unix command) */
802 sum
= (sum
>> 1) | (sum
<< 15);
805 term_printf("%05d\n", sum
);
813 static const KeyDef key_defs
[] = {
838 { 0x0e, "backspace" },
873 { 0x3a, "caps_lock" },
884 { 0x45, "num_lock" },
885 { 0x46, "scroll_lock" },
887 { 0xb5, "kp_divide" },
888 { 0x37, "kp_multiply" },
889 { 0x4a, "kp_subtract" },
891 { 0x9c, "kp_enter" },
892 { 0x53, "kp_decimal" },
927 static int get_keycode(const char *key
)
933 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
934 if (!strcmp(key
, p
->name
))
937 if (strstart(key
, "0x", NULL
)) {
938 ret
= strtoul(key
, &endp
, 0);
939 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
945 static void do_send_key(const char *string
)
948 uint8_t keycodes
[16];
950 int nb_keycodes
, keycode
, i
;
956 while (*p
!= '\0' && *p
!= '-') {
957 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
963 keycode
= get_keycode(keybuf
);
965 term_printf("unknown key: '%s'\n", keybuf
);
968 keycodes
[nb_keycodes
++] = keycode
;
973 /* key down events */
974 for(i
= 0; i
< nb_keycodes
; i
++) {
975 keycode
= keycodes
[i
];
977 kbd_put_keycode(0xe0);
978 kbd_put_keycode(keycode
& 0x7f);
981 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
982 keycode
= keycodes
[i
];
984 kbd_put_keycode(0xe0);
985 kbd_put_keycode(keycode
| 0x80);
989 static int mouse_button_state
;
991 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
995 dx
= strtol(dx_str
, NULL
, 0);
996 dy
= strtol(dy_str
, NULL
, 0);
999 dz
= strtol(dz_str
, NULL
, 0);
1000 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1003 static void do_mouse_button(int button_state
)
1005 mouse_button_state
= button_state
;
1006 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1009 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1015 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1023 val
= cpu_inb(NULL
, addr
);
1027 val
= cpu_inw(NULL
, addr
);
1031 val
= cpu_inl(NULL
, addr
);
1035 term_printf("port%c[0x%04x] = %#0*x\n",
1036 suffix
, addr
, size
* 2, val
);
1039 static void do_system_reset(void)
1041 qemu_system_reset_request();
1044 static void do_system_powerdown(void)
1046 qemu_system_powerdown_request();
1049 #if defined(TARGET_I386)
1050 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1052 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1055 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1056 pte
& PG_PSE_MASK
? 'P' : '-',
1057 pte
& PG_DIRTY_MASK
? 'D' : '-',
1058 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1059 pte
& PG_PCD_MASK
? 'C' : '-',
1060 pte
& PG_PWT_MASK
? 'T' : '-',
1061 pte
& PG_USER_MASK
? 'U' : '-',
1062 pte
& PG_RW_MASK
? 'W' : '-');
1065 static void tlb_info(void)
1069 uint32_t pgd
, pde
, pte
;
1071 env
= mon_get_cpu();
1075 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1076 term_printf("PG disabled\n");
1079 pgd
= env
->cr
[3] & ~0xfff;
1080 for(l1
= 0; l1
< 1024; l1
++) {
1081 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1082 pde
= le32_to_cpu(pde
);
1083 if (pde
& PG_PRESENT_MASK
) {
1084 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1085 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1087 for(l2
= 0; l2
< 1024; l2
++) {
1088 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1089 (uint8_t *)&pte
, 4);
1090 pte
= le32_to_cpu(pte
);
1091 if (pte
& PG_PRESENT_MASK
) {
1092 print_pte((l1
<< 22) + (l2
<< 12),
1102 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1103 uint32_t end
, int prot
)
1106 prot1
= *plast_prot
;
1107 if (prot
!= prot1
) {
1108 if (*pstart
!= -1) {
1109 term_printf("%08x-%08x %08x %c%c%c\n",
1110 *pstart
, end
, end
- *pstart
,
1111 prot1
& PG_USER_MASK
? 'u' : '-',
1113 prot1
& PG_RW_MASK
? 'w' : '-');
1123 static void mem_info(void)
1126 int l1
, l2
, prot
, last_prot
;
1127 uint32_t pgd
, pde
, pte
, start
, end
;
1129 env
= mon_get_cpu();
1133 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1134 term_printf("PG disabled\n");
1137 pgd
= env
->cr
[3] & ~0xfff;
1140 for(l1
= 0; l1
< 1024; l1
++) {
1141 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1142 pde
= le32_to_cpu(pde
);
1144 if (pde
& PG_PRESENT_MASK
) {
1145 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1146 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1147 mem_print(&start
, &last_prot
, end
, prot
);
1149 for(l2
= 0; l2
< 1024; l2
++) {
1150 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1151 (uint8_t *)&pte
, 4);
1152 pte
= le32_to_cpu(pte
);
1153 end
= (l1
<< 22) + (l2
<< 12);
1154 if (pte
& PG_PRESENT_MASK
) {
1155 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1159 mem_print(&start
, &last_prot
, end
, prot
);
1164 mem_print(&start
, &last_prot
, end
, prot
);
1170 static void do_info_kqemu(void)
1176 env
= mon_get_cpu();
1178 term_printf("No cpu initialized yet");
1181 val
= env
->kqemu_enabled
;
1182 term_printf("kqemu support: ");
1186 term_printf("disabled\n");
1189 term_printf("enabled for user code\n");
1192 term_printf("enabled for user and kernel code\n");
1196 term_printf("kqemu support: not compiled\n");
1200 #ifdef CONFIG_PROFILER
1204 int64_t kqemu_exec_count
;
1206 int64_t kqemu_ret_int_count
;
1207 int64_t kqemu_ret_excp_count
;
1208 int64_t kqemu_ret_intr_count
;
1210 static void do_info_profile(void)
1216 term_printf("async time %" PRId64
" (%0.3f)\n",
1217 dev_time
, dev_time
/ (double)ticks_per_sec
);
1218 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1219 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1220 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1221 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1222 kqemu_time
/ (double)total
* 100.0,
1224 kqemu_ret_int_count
,
1225 kqemu_ret_excp_count
,
1226 kqemu_ret_intr_count
);
1229 kqemu_exec_count
= 0;
1231 kqemu_ret_int_count
= 0;
1232 kqemu_ret_excp_count
= 0;
1233 kqemu_ret_intr_count
= 0;
1235 kqemu_record_dump();
1239 static void do_info_profile(void)
1241 term_printf("Internal profiler not compiled\n");
1245 /* Capture support */
1246 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1248 static void do_info_capture (void)
1253 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1254 term_printf ("[%d]: ", i
);
1255 s
->ops
.info (s
->opaque
);
1259 static void do_stop_capture (int n
)
1264 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1266 s
->ops
.destroy (s
->opaque
);
1267 LIST_REMOVE (s
, entries
);
1275 int wav_start_capture (CaptureState
*s
, const char *path
, int freq
,
1276 int bits
, int nchannels
);
1278 static void do_wav_capture (const char *path
,
1279 int has_freq
, int freq
,
1280 int has_bits
, int bits
,
1281 int has_channels
, int nchannels
)
1285 s
= qemu_mallocz (sizeof (*s
));
1287 term_printf ("Not enough memory to add wave capture\n");
1291 freq
= has_freq
? freq
: 44100;
1292 bits
= has_bits
? bits
: 16;
1293 nchannels
= has_channels
? nchannels
: 2;
1295 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1296 term_printf ("Faied to add wave capture\n");
1299 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1303 static term_cmd_t term_cmds
[] = {
1304 { "help|?", "s?", do_help
,
1305 "[cmd]", "show the help" },
1306 { "commit", "s", do_commit
,
1307 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1308 { "info", "s?", do_info
,
1309 "subcommand", "show various information about the system state" },
1310 { "q|quit", "", do_quit
,
1311 "", "quit the emulator" },
1312 { "eject", "-fB", do_eject
,
1313 "[-f] device", "eject a removable medium (use -f to force it)" },
1314 { "change", "BF", do_change
,
1315 "device filename", "change a removable medium" },
1316 { "screendump", "F", do_screen_dump
,
1317 "filename", "save screen into PPM image 'filename'" },
1318 { "logfile", "s", do_logfile
,
1319 "filename", "output logs to 'filename'" },
1320 { "log", "s", do_log
,
1321 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1322 { "savevm", "s?", do_savevm
,
1323 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1324 { "loadvm", "s", do_loadvm
,
1325 "tag|id", "restore a VM snapshot from its tag or id" },
1326 { "delvm", "s", do_delvm
,
1327 "tag|id", "delete a VM snapshot from its tag or id" },
1328 { "stop", "", do_stop
,
1329 "", "stop emulation", },
1330 { "c|cont", "", do_cont
,
1331 "", "resume emulation", },
1332 #ifdef CONFIG_GDBSTUB
1333 { "gdbserver", "s?", do_gdbserver
,
1334 "[port]", "start gdbserver session (default port=1234)", },
1336 { "x", "/l", do_memory_dump
,
1337 "/fmt addr", "virtual memory dump starting at 'addr'", },
1338 { "xp", "/l", do_physical_memory_dump
,
1339 "/fmt addr", "physical memory dump starting at 'addr'", },
1340 { "p|print", "/l", do_print
,
1341 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1342 { "i", "/ii.", do_ioport_read
,
1343 "/fmt addr", "I/O port read" },
1345 { "sendkey", "s", do_send_key
,
1346 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1347 { "system_reset", "", do_system_reset
,
1348 "", "reset the system" },
1349 { "system_powerdown", "", do_system_powerdown
,
1350 "", "send system power down event" },
1351 { "sum", "ii", do_sum
,
1352 "addr size", "compute the checksum of a memory region" },
1353 { "usb_add", "s", do_usb_add
,
1354 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1355 { "usb_del", "s", do_usb_del
,
1356 "device", "remove USB device 'bus.addr'" },
1357 { "cpu", "i", do_cpu_set
,
1358 "index", "set the default CPU" },
1359 { "mouse_move", "sss?", do_mouse_move
,
1360 "dx dy [dz]", "send mouse move events" },
1361 { "mouse_button", "i", do_mouse_button
,
1362 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1363 { "mouse_set", "i", do_mouse_set
,
1364 "index", "set which mouse device receives events" },
1366 { "wavcapture", "si?i?i?", do_wav_capture
,
1367 "path [frequency bits channels]",
1368 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1370 { "stopcapture", "i", do_stop_capture
,
1371 "capture index", "stop capture" },
1372 { "memsave", "lis", do_memory_save
,
1373 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1374 { "read_disk_io", "s", do_io_statistics
,
1375 "hdx", "read disk I/O statistics (VMDK format)" },
1376 { "migrate", "-ds", do_migrate
,
1377 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1378 { "migrate_cancel", "", do_migrate_cancel
,
1379 "", "cancel the current VM migration" },
1380 { "migrate_set_speed", "s", do_migrate_set_speed
,
1381 "value", "set maximum speed (in bytes) for migrations" },
1385 static term_cmd_t info_cmds
[] = {
1386 { "version", "", do_info_version
,
1387 "", "show the version of qemu" },
1388 { "network", "", do_info_network
,
1389 "", "show the network state" },
1390 { "block", "", do_info_block
,
1391 "", "show the block devices" },
1392 { "registers", "", do_info_registers
,
1393 "", "show the cpu registers" },
1394 { "cpus", "", do_info_cpus
,
1395 "", "show infos for each CPU" },
1396 { "history", "", do_info_history
,
1397 "", "show the command line history", },
1398 { "irq", "", irq_info
,
1399 "", "show the interrupts statistics (if available)", },
1400 { "pic", "", pic_info
,
1401 "", "show i8259 (PIC) state", },
1402 { "pci", "", pci_info
,
1403 "", "show PCI info", },
1404 #if defined(TARGET_I386)
1405 { "tlb", "", tlb_info
,
1406 "", "show virtual to physical memory mappings", },
1407 { "mem", "", mem_info
,
1408 "", "show the active virtual memory mappings", },
1410 { "jit", "", do_info_jit
,
1411 "", "show dynamic compiler info", },
1412 { "kqemu", "", do_info_kqemu
,
1413 "", "show kqemu information", },
1414 { "usb", "", usb_info
,
1415 "", "show guest USB devices", },
1416 { "usbhost", "", usb_host_info
,
1417 "", "show host USB devices", },
1418 { "profile", "", do_info_profile
,
1419 "", "show profiling information", },
1420 { "capture", "", do_info_capture
,
1421 "", "show capture information" },
1422 { "snapshots", "", do_info_snapshots
,
1423 "", "show the currently saved VM snapshots" },
1424 { "pcmcia", "", pcmcia_info
,
1425 "", "show guest PCMCIA status" },
1426 { "mice", "", do_info_mice
,
1427 "", "show which guest mouse is receiving events" },
1428 { "vnc", "", do_info_vnc
,
1429 "", "show the vnc server status"},
1430 { "name", "", do_info_name
,
1431 "", "show the current VM name" },
1432 #if defined(TARGET_PPC)
1433 { "cpustats", "", do_info_cpu_stats
,
1434 "", "show CPU statistics", },
1436 { "migration", "", do_info_migration
,
1437 "", "show migration information" },
1441 /*******************************************************************/
1443 static const char *pch
;
1444 static jmp_buf expr_env
;
1449 typedef struct MonitorDef
{
1452 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
1456 #if defined(TARGET_I386)
1457 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
1459 CPUState
*env
= mon_get_cpu();
1462 return env
->eip
+ env
->segs
[R_CS
].base
;
1466 #if defined(TARGET_PPC)
1467 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
1469 CPUState
*env
= mon_get_cpu();
1477 for (i
= 0; i
< 8; i
++)
1478 u
|= env
->crf
[i
] << (32 - (4 * i
));
1483 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
1485 CPUState
*env
= mon_get_cpu();
1488 return do_load_msr(env
);
1491 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
1493 CPUState
*env
= mon_get_cpu();
1496 return ppc_load_xer(env
);
1499 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1501 CPUState
*env
= mon_get_cpu();
1504 return cpu_ppc_load_decr(env
);
1507 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1509 CPUState
*env
= mon_get_cpu();
1512 return cpu_ppc_load_tbu(env
);
1515 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1517 CPUState
*env
= mon_get_cpu();
1520 return cpu_ppc_load_tbl(env
);
1524 #if defined(TARGET_SPARC)
1525 #ifndef TARGET_SPARC64
1526 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1528 CPUState
*env
= mon_get_cpu();
1531 return GET_PSR(env
);
1535 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1537 CPUState
*env
= mon_get_cpu();
1540 return env
->regwptr
[val
];
1544 static MonitorDef monitor_defs
[] = {
1547 #define SEG(name, seg) \
1548 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1549 { name ".base", offsetof(CPUState, segs[seg].base) },\
1550 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1552 { "eax", offsetof(CPUState
, regs
[0]) },
1553 { "ecx", offsetof(CPUState
, regs
[1]) },
1554 { "edx", offsetof(CPUState
, regs
[2]) },
1555 { "ebx", offsetof(CPUState
, regs
[3]) },
1556 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1557 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1558 { "esi", offsetof(CPUState
, regs
[6]) },
1559 { "edi", offsetof(CPUState
, regs
[7]) },
1560 #ifdef TARGET_X86_64
1561 { "r8", offsetof(CPUState
, regs
[8]) },
1562 { "r9", offsetof(CPUState
, regs
[9]) },
1563 { "r10", offsetof(CPUState
, regs
[10]) },
1564 { "r11", offsetof(CPUState
, regs
[11]) },
1565 { "r12", offsetof(CPUState
, regs
[12]) },
1566 { "r13", offsetof(CPUState
, regs
[13]) },
1567 { "r14", offsetof(CPUState
, regs
[14]) },
1568 { "r15", offsetof(CPUState
, regs
[15]) },
1570 { "eflags", offsetof(CPUState
, eflags
) },
1571 { "eip", offsetof(CPUState
, eip
) },
1578 { "pc", 0, monitor_get_pc
, },
1579 #elif defined(TARGET_PPC)
1580 /* General purpose registers */
1581 { "r0", offsetof(CPUState
, gpr
[0]) },
1582 { "r1", offsetof(CPUState
, gpr
[1]) },
1583 { "r2", offsetof(CPUState
, gpr
[2]) },
1584 { "r3", offsetof(CPUState
, gpr
[3]) },
1585 { "r4", offsetof(CPUState
, gpr
[4]) },
1586 { "r5", offsetof(CPUState
, gpr
[5]) },
1587 { "r6", offsetof(CPUState
, gpr
[6]) },
1588 { "r7", offsetof(CPUState
, gpr
[7]) },
1589 { "r8", offsetof(CPUState
, gpr
[8]) },
1590 { "r9", offsetof(CPUState
, gpr
[9]) },
1591 { "r10", offsetof(CPUState
, gpr
[10]) },
1592 { "r11", offsetof(CPUState
, gpr
[11]) },
1593 { "r12", offsetof(CPUState
, gpr
[12]) },
1594 { "r13", offsetof(CPUState
, gpr
[13]) },
1595 { "r14", offsetof(CPUState
, gpr
[14]) },
1596 { "r15", offsetof(CPUState
, gpr
[15]) },
1597 { "r16", offsetof(CPUState
, gpr
[16]) },
1598 { "r17", offsetof(CPUState
, gpr
[17]) },
1599 { "r18", offsetof(CPUState
, gpr
[18]) },
1600 { "r19", offsetof(CPUState
, gpr
[19]) },
1601 { "r20", offsetof(CPUState
, gpr
[20]) },
1602 { "r21", offsetof(CPUState
, gpr
[21]) },
1603 { "r22", offsetof(CPUState
, gpr
[22]) },
1604 { "r23", offsetof(CPUState
, gpr
[23]) },
1605 { "r24", offsetof(CPUState
, gpr
[24]) },
1606 { "r25", offsetof(CPUState
, gpr
[25]) },
1607 { "r26", offsetof(CPUState
, gpr
[26]) },
1608 { "r27", offsetof(CPUState
, gpr
[27]) },
1609 { "r28", offsetof(CPUState
, gpr
[28]) },
1610 { "r29", offsetof(CPUState
, gpr
[29]) },
1611 { "r30", offsetof(CPUState
, gpr
[30]) },
1612 { "r31", offsetof(CPUState
, gpr
[31]) },
1613 /* Floating point registers */
1614 { "f0", offsetof(CPUState
, fpr
[0]) },
1615 { "f1", offsetof(CPUState
, fpr
[1]) },
1616 { "f2", offsetof(CPUState
, fpr
[2]) },
1617 { "f3", offsetof(CPUState
, fpr
[3]) },
1618 { "f4", offsetof(CPUState
, fpr
[4]) },
1619 { "f5", offsetof(CPUState
, fpr
[5]) },
1620 { "f6", offsetof(CPUState
, fpr
[6]) },
1621 { "f7", offsetof(CPUState
, fpr
[7]) },
1622 { "f8", offsetof(CPUState
, fpr
[8]) },
1623 { "f9", offsetof(CPUState
, fpr
[9]) },
1624 { "f10", offsetof(CPUState
, fpr
[10]) },
1625 { "f11", offsetof(CPUState
, fpr
[11]) },
1626 { "f12", offsetof(CPUState
, fpr
[12]) },
1627 { "f13", offsetof(CPUState
, fpr
[13]) },
1628 { "f14", offsetof(CPUState
, fpr
[14]) },
1629 { "f15", offsetof(CPUState
, fpr
[15]) },
1630 { "f16", offsetof(CPUState
, fpr
[16]) },
1631 { "f17", offsetof(CPUState
, fpr
[17]) },
1632 { "f18", offsetof(CPUState
, fpr
[18]) },
1633 { "f19", offsetof(CPUState
, fpr
[19]) },
1634 { "f20", offsetof(CPUState
, fpr
[20]) },
1635 { "f21", offsetof(CPUState
, fpr
[21]) },
1636 { "f22", offsetof(CPUState
, fpr
[22]) },
1637 { "f23", offsetof(CPUState
, fpr
[23]) },
1638 { "f24", offsetof(CPUState
, fpr
[24]) },
1639 { "f25", offsetof(CPUState
, fpr
[25]) },
1640 { "f26", offsetof(CPUState
, fpr
[26]) },
1641 { "f27", offsetof(CPUState
, fpr
[27]) },
1642 { "f28", offsetof(CPUState
, fpr
[28]) },
1643 { "f29", offsetof(CPUState
, fpr
[29]) },
1644 { "f30", offsetof(CPUState
, fpr
[30]) },
1645 { "f31", offsetof(CPUState
, fpr
[31]) },
1646 { "fpscr", offsetof(CPUState
, fpscr
) },
1647 /* Next instruction pointer */
1648 { "nip|pc", offsetof(CPUState
, nip
) },
1649 { "lr", offsetof(CPUState
, lr
) },
1650 { "ctr", offsetof(CPUState
, ctr
) },
1651 { "decr", 0, &monitor_get_decr
, },
1652 { "ccr", 0, &monitor_get_ccr
, },
1653 /* Machine state register */
1654 { "msr", 0, &monitor_get_msr
, },
1655 { "xer", 0, &monitor_get_xer
, },
1656 { "tbu", 0, &monitor_get_tbu
, },
1657 { "tbl", 0, &monitor_get_tbl
, },
1658 #if defined(TARGET_PPC64)
1659 /* Address space register */
1660 { "asr", offsetof(CPUState
, asr
) },
1662 /* Segment registers */
1663 { "sdr1", offsetof(CPUState
, sdr1
) },
1664 { "sr0", offsetof(CPUState
, sr
[0]) },
1665 { "sr1", offsetof(CPUState
, sr
[1]) },
1666 { "sr2", offsetof(CPUState
, sr
[2]) },
1667 { "sr3", offsetof(CPUState
, sr
[3]) },
1668 { "sr4", offsetof(CPUState
, sr
[4]) },
1669 { "sr5", offsetof(CPUState
, sr
[5]) },
1670 { "sr6", offsetof(CPUState
, sr
[6]) },
1671 { "sr7", offsetof(CPUState
, sr
[7]) },
1672 { "sr8", offsetof(CPUState
, sr
[8]) },
1673 { "sr9", offsetof(CPUState
, sr
[9]) },
1674 { "sr10", offsetof(CPUState
, sr
[10]) },
1675 { "sr11", offsetof(CPUState
, sr
[11]) },
1676 { "sr12", offsetof(CPUState
, sr
[12]) },
1677 { "sr13", offsetof(CPUState
, sr
[13]) },
1678 { "sr14", offsetof(CPUState
, sr
[14]) },
1679 { "sr15", offsetof(CPUState
, sr
[15]) },
1680 /* Too lazy to put BATs and SPRs ... */
1681 #elif defined(TARGET_SPARC)
1682 { "g0", offsetof(CPUState
, gregs
[0]) },
1683 { "g1", offsetof(CPUState
, gregs
[1]) },
1684 { "g2", offsetof(CPUState
, gregs
[2]) },
1685 { "g3", offsetof(CPUState
, gregs
[3]) },
1686 { "g4", offsetof(CPUState
, gregs
[4]) },
1687 { "g5", offsetof(CPUState
, gregs
[5]) },
1688 { "g6", offsetof(CPUState
, gregs
[6]) },
1689 { "g7", offsetof(CPUState
, gregs
[7]) },
1690 { "o0", 0, monitor_get_reg
},
1691 { "o1", 1, monitor_get_reg
},
1692 { "o2", 2, monitor_get_reg
},
1693 { "o3", 3, monitor_get_reg
},
1694 { "o4", 4, monitor_get_reg
},
1695 { "o5", 5, monitor_get_reg
},
1696 { "o6", 6, monitor_get_reg
},
1697 { "o7", 7, monitor_get_reg
},
1698 { "l0", 8, monitor_get_reg
},
1699 { "l1", 9, monitor_get_reg
},
1700 { "l2", 10, monitor_get_reg
},
1701 { "l3", 11, monitor_get_reg
},
1702 { "l4", 12, monitor_get_reg
},
1703 { "l5", 13, monitor_get_reg
},
1704 { "l6", 14, monitor_get_reg
},
1705 { "l7", 15, monitor_get_reg
},
1706 { "i0", 16, monitor_get_reg
},
1707 { "i1", 17, monitor_get_reg
},
1708 { "i2", 18, monitor_get_reg
},
1709 { "i3", 19, monitor_get_reg
},
1710 { "i4", 20, monitor_get_reg
},
1711 { "i5", 21, monitor_get_reg
},
1712 { "i6", 22, monitor_get_reg
},
1713 { "i7", 23, monitor_get_reg
},
1714 { "pc", offsetof(CPUState
, pc
) },
1715 { "npc", offsetof(CPUState
, npc
) },
1716 { "y", offsetof(CPUState
, y
) },
1717 #ifndef TARGET_SPARC64
1718 { "psr", 0, &monitor_get_psr
, },
1719 { "wim", offsetof(CPUState
, wim
) },
1721 { "tbr", offsetof(CPUState
, tbr
) },
1722 { "fsr", offsetof(CPUState
, fsr
) },
1723 { "f0", offsetof(CPUState
, fpr
[0]) },
1724 { "f1", offsetof(CPUState
, fpr
[1]) },
1725 { "f2", offsetof(CPUState
, fpr
[2]) },
1726 { "f3", offsetof(CPUState
, fpr
[3]) },
1727 { "f4", offsetof(CPUState
, fpr
[4]) },
1728 { "f5", offsetof(CPUState
, fpr
[5]) },
1729 { "f6", offsetof(CPUState
, fpr
[6]) },
1730 { "f7", offsetof(CPUState
, fpr
[7]) },
1731 { "f8", offsetof(CPUState
, fpr
[8]) },
1732 { "f9", offsetof(CPUState
, fpr
[9]) },
1733 { "f10", offsetof(CPUState
, fpr
[10]) },
1734 { "f11", offsetof(CPUState
, fpr
[11]) },
1735 { "f12", offsetof(CPUState
, fpr
[12]) },
1736 { "f13", offsetof(CPUState
, fpr
[13]) },
1737 { "f14", offsetof(CPUState
, fpr
[14]) },
1738 { "f15", offsetof(CPUState
, fpr
[15]) },
1739 { "f16", offsetof(CPUState
, fpr
[16]) },
1740 { "f17", offsetof(CPUState
, fpr
[17]) },
1741 { "f18", offsetof(CPUState
, fpr
[18]) },
1742 { "f19", offsetof(CPUState
, fpr
[19]) },
1743 { "f20", offsetof(CPUState
, fpr
[20]) },
1744 { "f21", offsetof(CPUState
, fpr
[21]) },
1745 { "f22", offsetof(CPUState
, fpr
[22]) },
1746 { "f23", offsetof(CPUState
, fpr
[23]) },
1747 { "f24", offsetof(CPUState
, fpr
[24]) },
1748 { "f25", offsetof(CPUState
, fpr
[25]) },
1749 { "f26", offsetof(CPUState
, fpr
[26]) },
1750 { "f27", offsetof(CPUState
, fpr
[27]) },
1751 { "f28", offsetof(CPUState
, fpr
[28]) },
1752 { "f29", offsetof(CPUState
, fpr
[29]) },
1753 { "f30", offsetof(CPUState
, fpr
[30]) },
1754 { "f31", offsetof(CPUState
, fpr
[31]) },
1755 #ifdef TARGET_SPARC64
1756 { "f32", offsetof(CPUState
, fpr
[32]) },
1757 { "f34", offsetof(CPUState
, fpr
[34]) },
1758 { "f36", offsetof(CPUState
, fpr
[36]) },
1759 { "f38", offsetof(CPUState
, fpr
[38]) },
1760 { "f40", offsetof(CPUState
, fpr
[40]) },
1761 { "f42", offsetof(CPUState
, fpr
[42]) },
1762 { "f44", offsetof(CPUState
, fpr
[44]) },
1763 { "f46", offsetof(CPUState
, fpr
[46]) },
1764 { "f48", offsetof(CPUState
, fpr
[48]) },
1765 { "f50", offsetof(CPUState
, fpr
[50]) },
1766 { "f52", offsetof(CPUState
, fpr
[52]) },
1767 { "f54", offsetof(CPUState
, fpr
[54]) },
1768 { "f56", offsetof(CPUState
, fpr
[56]) },
1769 { "f58", offsetof(CPUState
, fpr
[58]) },
1770 { "f60", offsetof(CPUState
, fpr
[60]) },
1771 { "f62", offsetof(CPUState
, fpr
[62]) },
1772 { "asi", offsetof(CPUState
, asi
) },
1773 { "pstate", offsetof(CPUState
, pstate
) },
1774 { "cansave", offsetof(CPUState
, cansave
) },
1775 { "canrestore", offsetof(CPUState
, canrestore
) },
1776 { "otherwin", offsetof(CPUState
, otherwin
) },
1777 { "wstate", offsetof(CPUState
, wstate
) },
1778 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1779 { "fprs", offsetof(CPUState
, fprs
) },
1785 static void expr_error(const char *fmt
)
1789 longjmp(expr_env
, 1);
1792 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1793 static int get_monitor_def(target_long
*pval
, const char *name
)
1798 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1799 if (compare_cmd(name
, md
->name
)) {
1800 if (md
->get_value
) {
1801 *pval
= md
->get_value(md
, md
->offset
);
1803 CPUState
*env
= mon_get_cpu();
1806 ptr
= (uint8_t *)env
+ md
->offset
;
1809 *pval
= *(int32_t *)ptr
;
1812 *pval
= *(target_long
*)ptr
;
1825 static void next(void)
1829 while (isspace(*pch
))
1834 static int64_t expr_sum(void);
1836 static int64_t expr_unary(void)
1859 expr_error("')' expected");
1866 expr_error("character constant expected");
1870 expr_error("missing terminating \' character");
1880 while ((*pch
>= 'a' && *pch
<= 'z') ||
1881 (*pch
>= 'A' && *pch
<= 'Z') ||
1882 (*pch
>= '0' && *pch
<= '9') ||
1883 *pch
== '_' || *pch
== '.') {
1884 if ((q
- buf
) < sizeof(buf
) - 1)
1888 while (isspace(*pch
))
1891 ret
= get_monitor_def(®
, buf
);
1893 expr_error("unknown register");
1895 expr_error("no cpu defined");
1900 expr_error("unexpected end of expression");
1904 #if TARGET_PHYS_ADDR_BITS > 32
1905 n
= strtoull(pch
, &p
, 0);
1907 n
= strtoul(pch
, &p
, 0);
1910 expr_error("invalid char in expression");
1913 while (isspace(*pch
))
1921 static int64_t expr_prod(void)
1929 if (op
!= '*' && op
!= '/' && op
!= '%')
1932 val2
= expr_unary();
1941 expr_error("division by zero");
1952 static int64_t expr_logic(void)
1960 if (op
!= '&' && op
!= '|' && op
!= '^')
1980 static int64_t expr_sum(void)
1988 if (op
!= '+' && op
!= '-')
1991 val2
= expr_logic();
2000 static int get_expr(int64_t *pval
, const char **pp
)
2003 if (setjmp(expr_env
)) {
2007 while (isspace(*pch
))
2014 static int get_str(char *buf
, int buf_size
, const char **pp
)
2032 while (*p
!= '\0' && *p
!= '\"') {
2048 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2051 if ((q
- buf
) < buf_size
- 1) {
2055 if ((q
- buf
) < buf_size
- 1) {
2062 qemu_printf("unterminated string\n");
2067 while (*p
!= '\0' && !isspace(*p
)) {
2068 if ((q
- buf
) < buf_size
- 1) {
2079 static int default_fmt_format
= 'x';
2080 static int default_fmt_size
= 4;
2084 static void monitor_handle_command(const char *cmdline
)
2086 const char *p
, *pstart
, *typestr
;
2088 int c
, nb_args
, len
, i
, has_arg
;
2092 void *str_allocated
[MAX_ARGS
];
2093 void *args
[MAX_ARGS
];
2096 term_printf("command='%s'\n", cmdline
);
2099 /* extract the command name */
2107 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
2110 if (len
> sizeof(cmdname
) - 1)
2111 len
= sizeof(cmdname
) - 1;
2112 memcpy(cmdname
, pstart
, len
);
2113 cmdname
[len
] = '\0';
2115 /* find the command */
2116 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2117 if (compare_cmd(cmdname
, cmd
->name
))
2120 term_printf("unknown command: '%s'\n", cmdname
);
2124 for(i
= 0; i
< MAX_ARGS
; i
++)
2125 str_allocated
[i
] = NULL
;
2127 /* parse the parameters */
2128 typestr
= cmd
->args_type
;
2145 if (*typestr
== '?') {
2148 /* no optional string: NULL argument */
2153 ret
= get_str(buf
, sizeof(buf
), &p
);
2157 term_printf("%s: filename expected\n", cmdname
);
2160 term_printf("%s: block device name expected\n", cmdname
);
2163 term_printf("%s: string expected\n", cmdname
);
2168 str
= qemu_malloc(strlen(buf
) + 1);
2170 str_allocated
[nb_args
] = str
;
2172 if (nb_args
>= MAX_ARGS
) {
2174 term_printf("%s: too many arguments\n", cmdname
);
2177 args
[nb_args
++] = str
;
2182 int count
, format
, size
;
2192 while (isdigit(*p
)) {
2193 count
= count
* 10 + (*p
- '0');
2231 if (*p
!= '\0' && !isspace(*p
)) {
2232 term_printf("invalid char in format: '%c'\n", *p
);
2236 format
= default_fmt_format
;
2237 if (format
!= 'i') {
2238 /* for 'i', not specifying a size gives -1 as size */
2240 size
= default_fmt_size
;
2242 default_fmt_size
= size
;
2243 default_fmt_format
= format
;
2246 format
= default_fmt_format
;
2247 if (format
!= 'i') {
2248 size
= default_fmt_size
;
2253 if (nb_args
+ 3 > MAX_ARGS
)
2255 args
[nb_args
++] = (void*)(long)count
;
2256 args
[nb_args
++] = (void*)(long)format
;
2257 args
[nb_args
++] = (void*)(long)size
;
2267 if (*typestr
== '?' || *typestr
== '.') {
2268 if (*typestr
== '?') {
2284 if (nb_args
>= MAX_ARGS
)
2286 args
[nb_args
++] = (void *)(long)has_arg
;
2288 if (nb_args
>= MAX_ARGS
)
2294 if (get_expr(&val
, &p
))
2298 if (nb_args
>= MAX_ARGS
)
2300 args
[nb_args
++] = (void *)(long)val
;
2302 if ((nb_args
+ 1) >= MAX_ARGS
)
2304 #if TARGET_PHYS_ADDR_BITS > 32
2305 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2307 args
[nb_args
++] = (void *)0;
2309 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2327 term_printf("%s: unsupported option -%c\n",
2334 if (nb_args
>= MAX_ARGS
)
2336 args
[nb_args
++] = (void *)(long)has_option
;
2341 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2345 /* check that all arguments were parsed */
2349 term_printf("%s: extraneous characters at the end of line\n",
2359 cmd
->handler(args
[0]);
2362 cmd
->handler(args
[0], args
[1]);
2365 cmd
->handler(args
[0], args
[1], args
[2]);
2368 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
2371 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
2374 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2377 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2380 term_printf("unsupported number of arguments: %d\n", nb_args
);
2384 for(i
= 0; i
< MAX_ARGS
; i
++)
2385 qemu_free(str_allocated
[i
]);
2389 static void cmd_completion(const char *name
, const char *list
)
2391 const char *p
, *pstart
;
2400 p
= pstart
+ strlen(pstart
);
2402 if (len
> sizeof(cmd
) - 2)
2403 len
= sizeof(cmd
) - 2;
2404 memcpy(cmd
, pstart
, len
);
2406 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2407 add_completion(cmd
);
2415 static void file_completion(const char *input
)
2420 char file
[1024], file_prefix
[1024];
2424 p
= strrchr(input
, '/');
2427 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2430 input_path_len
= p
- input
+ 1;
2431 memcpy(path
, input
, input_path_len
);
2432 if (input_path_len
> sizeof(path
) - 1)
2433 input_path_len
= sizeof(path
) - 1;
2434 path
[input_path_len
] = '\0';
2435 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2437 #ifdef DEBUG_COMPLETION
2438 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2440 ffs
= opendir(path
);
2448 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2449 memcpy(file
, input
, input_path_len
);
2450 strcpy(file
+ input_path_len
, d
->d_name
);
2451 /* stat the file to find out if it's a directory.
2452 * In that case add a slash to speed up typing long paths
2455 if(S_ISDIR(sb
.st_mode
))
2457 add_completion(file
);
2463 static void block_completion_it(void *opaque
, const char *name
)
2465 const char *input
= opaque
;
2467 if (input
[0] == '\0' ||
2468 !strncmp(name
, (char *)input
, strlen(input
))) {
2469 add_completion(name
);
2473 /* NOTE: this parser is an approximate form of the real command parser */
2474 static void parse_cmdline(const char *cmdline
,
2475 int *pnb_args
, char **args
)
2488 if (nb_args
>= MAX_ARGS
)
2490 ret
= get_str(buf
, sizeof(buf
), &p
);
2491 args
[nb_args
] = qemu_strdup(buf
);
2496 *pnb_args
= nb_args
;
2499 void readline_find_completion(const char *cmdline
)
2501 const char *cmdname
;
2502 char *args
[MAX_ARGS
];
2503 int nb_args
, i
, len
;
2504 const char *ptype
, *str
;
2508 parse_cmdline(cmdline
, &nb_args
, args
);
2509 #ifdef DEBUG_COMPLETION
2510 for(i
= 0; i
< nb_args
; i
++) {
2511 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2515 /* if the line ends with a space, it means we want to complete the
2517 len
= strlen(cmdline
);
2518 if (len
> 0 && isspace(cmdline
[len
- 1])) {
2519 if (nb_args
>= MAX_ARGS
)
2521 args
[nb_args
++] = qemu_strdup("");
2524 /* command completion */
2529 completion_index
= strlen(cmdname
);
2530 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2531 cmd_completion(cmdname
, cmd
->name
);
2534 /* find the command */
2535 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2536 if (compare_cmd(args
[0], cmd
->name
))
2541 ptype
= cmd
->args_type
;
2542 for(i
= 0; i
< nb_args
- 2; i
++) {
2543 if (*ptype
!= '\0') {
2545 while (*ptype
== '?')
2549 str
= args
[nb_args
- 1];
2552 /* file completion */
2553 completion_index
= strlen(str
);
2554 file_completion(str
);
2557 /* block device name completion */
2558 completion_index
= strlen(str
);
2559 bdrv_iterate(block_completion_it
, (void *)str
);
2562 /* XXX: more generic ? */
2563 if (!strcmp(cmd
->name
, "info")) {
2564 completion_index
= strlen(str
);
2565 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2566 cmd_completion(str
, cmd
->name
);
2568 } else if (!strcmp(cmd
->name
, "sendkey")) {
2569 completion_index
= strlen(str
);
2570 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2571 cmd_completion(str
, key
->name
);
2579 for(i
= 0; i
< nb_args
; i
++)
2583 static int term_can_read(void *opaque
)
2588 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2591 for(i
= 0; i
< size
; i
++)
2592 readline_handle_byte(buf
[i
]);
2595 static int monitor_suspended
;
2597 void monitor_suspend(void)
2599 monitor_suspended
= 1;
2602 void monitor_resume(void)
2604 monitor_suspended
= 0;
2605 monitor_start_input();
2608 static void monitor_start_input(void);
2610 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2612 monitor_handle_command(cmdline
);
2613 if (!monitor_suspended
)
2614 monitor_start_input();
2617 static void monitor_start_input(void)
2619 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2622 static void term_event(void *opaque
, int event
)
2624 if (event
!= CHR_EVENT_RESET
)
2628 term_printf("QEMU %s monitor - type 'help' for more information\n",
2630 monitor_start_input();
2633 static int is_first_init
= 1;
2635 void monitor_init(CharDriverState
*hd
, int show_banner
)
2639 if (is_first_init
) {
2640 for (i
= 0; i
< MAX_MON
; i
++) {
2641 monitor_hd
[i
] = NULL
;
2645 for (i
= 0; i
< MAX_MON
; i
++) {
2646 if (monitor_hd
[i
] == NULL
) {
2652 hide_banner
= !show_banner
;
2654 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2657 /* XXX: use threads ? */
2658 /* modal monitor readline */
2659 static int monitor_readline_started
;
2660 static char *monitor_readline_buf
;
2661 static int monitor_readline_buf_size
;
2663 static void monitor_readline_cb(void *opaque
, const char *input
)
2665 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2666 monitor_readline_started
= 0;
2669 void monitor_readline(const char *prompt
, int is_password
,
2670 char *buf
, int buf_size
)
2675 for (i
= 0; i
< MAX_MON
; i
++)
2676 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
2677 qemu_chr_send_event(monitor_hd
[i
], CHR_EVENT_FOCUS
);
2679 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2680 monitor_readline_buf
= buf
;
2681 monitor_readline_buf_size
= buf_size
;
2682 monitor_readline_started
= 1;
2683 while (monitor_readline_started
) {