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
;
59 static CharDriverState
*monitor_hd
;
60 static int hide_banner
;
62 static term_cmd_t term_cmds
[];
63 static term_cmd_t info_cmds
[];
65 static char term_outbuf
[1024];
66 static int term_outbuf_index
;
68 static void monitor_start_input(void);
70 CPUState
*mon_cpu
= NULL
;
74 if (term_outbuf_index
> 0) {
75 qemu_chr_write(monitor_hd
, term_outbuf
, term_outbuf_index
);
76 term_outbuf_index
= 0;
80 /* flush at every end of line or if the buffer is full */
81 void term_puts(const char *str
)
89 term_outbuf
[term_outbuf_index
++] = '\r';
90 term_outbuf
[term_outbuf_index
++] = c
;
91 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
97 void term_vprintf(const char *fmt
, va_list ap
)
100 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
104 void term_printf(const char *fmt
, ...)
108 term_vprintf(fmt
, ap
);
112 void term_print_filename(const char *filename
)
116 for (i
= 0; filename
[i
]; i
++) {
117 switch (filename
[i
]) {
121 term_printf("\\%c", filename
[i
]);
133 term_printf("%c", filename
[i
]);
139 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
143 term_vprintf(fmt
, ap
);
148 static int compare_cmd(const char *name
, const char *list
)
150 const char *p
, *pstart
;
158 p
= pstart
+ strlen(pstart
);
159 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
168 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
172 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
173 if (!name
|| !strcmp(name
, cmd
->name
))
174 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
178 static void help_cmd(const char *name
)
180 if (name
&& !strcmp(name
, "info")) {
181 help_cmd1(info_cmds
, "info ", NULL
);
183 help_cmd1(term_cmds
, "", name
);
184 if (name
&& !strcmp(name
, "log")) {
186 term_printf("Log items (comma separated):\n");
187 term_printf("%-10s %s\n", "none", "remove all logs");
188 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
189 term_printf("%-10s %s\n", item
->name
, item
->help
);
195 static void do_help(const char *name
)
200 static void do_commit(const char *device
)
204 all_devices
= !strcmp(device
, "all");
205 for (i
= 0; i
< MAX_DISKS
; i
++) {
208 !strcmp(bdrv_get_device_name(bs_table
[i
]), device
))
209 bdrv_commit(bs_table
[i
]);
214 static void do_info(const char *item
)
220 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
221 if (compare_cmd(item
, cmd
->name
))
231 static void do_info_version(void)
233 term_printf("%s\n", QEMU_VERSION
);
236 static void do_info_block(void)
241 /* get the current CPU defined by the user */
242 int mon_set_cpu(int cpu_index
)
246 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
247 if (env
->cpu_index
== cpu_index
) {
255 CPUState
*mon_get_cpu(void)
262 kvm_save_registers(mon_cpu
);
268 static void do_info_registers(void)
275 cpu_dump_state(env
, NULL
, monitor_fprintf
,
278 cpu_dump_state(env
, NULL
, monitor_fprintf
,
283 static void do_info_cpus(void)
287 /* just to set the default cpu if not already done */
290 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
291 term_printf("%c CPU #%d:",
292 (env
== mon_cpu
) ? '*' : ' ',
294 #if defined(TARGET_I386)
295 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
296 if (env
->hflags
& HF_HALTED_MASK
)
297 term_printf(" (halted)");
298 #elif defined(TARGET_PPC)
299 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
301 term_printf(" (halted)");
302 #elif defined(TARGET_SPARC)
303 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
305 term_printf(" (halted)");
311 static void do_cpu_set(int index
)
313 if (mon_set_cpu(index
) < 0)
314 term_printf("Invalid CPU index\n");
317 static void do_info_jit(void)
319 dump_exec_info(NULL
, monitor_fprintf
);
322 static void do_info_history (void)
329 str
= readline_get_history(i
);
332 term_printf("%d: '%s'\n", i
, str
);
337 static void do_quit(void)
342 static int eject_device(BlockDriverState
*bs
, int force
)
344 if (bdrv_is_inserted(bs
)) {
346 if (!bdrv_is_removable(bs
)) {
347 term_printf("device is not removable\n");
350 if (bdrv_is_locked(bs
)) {
351 term_printf("device is locked\n");
360 static void do_eject(int force
, const char *filename
)
362 BlockDriverState
*bs
;
364 bs
= bdrv_find(filename
);
366 term_printf("device not found\n");
369 eject_device(bs
, force
);
372 #define USAGE_STR "Usage: read_disk_io hd[a/b/c/d]\n"
373 DiskIOStatistics
vmdk_io_statistics(BlockDriverState
*bs
);
375 static void do_io_statistics(const char *hdx
)
377 DiskIOStatistics io
[4];
379 if ((strcmp(hdx
,"hda") != 0) && (strcmp(hdx
,"hdb") != 0) &&
380 (strcmp(hdx
,"hdc") != 0) && (strcmp(hdx
,"hdd") != 0)) {
381 term_printf(USAGE_STR
);
387 term_printf("---------- hda io statistics ----------\n");
388 io
[0] = vmdk_io_statistics(bs_table
[0]);
389 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
390 io
[0].read_byte_counter
, io
[0].write_byte_counter
);
393 term_printf("---------- hdb io statistics ----------\n");
395 io
[1] = vmdk_io_statistics(bs_table
[1]);
396 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
397 io
[1].read_byte_counter
, io
[1].write_byte_counter
);
399 term_printf("hdb not exist\n");
403 term_printf("---------- hdc io statistics ----------\n");
405 io
[2] = vmdk_io_statistics(bs_table
[2]);
406 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
407 io
[2].read_byte_counter
, io
[2].write_byte_counter
);
409 term_printf("hdc not exist\n");
413 term_printf("---------- hdd io statistics ----------\n");
415 io
[3] = vmdk_io_statistics(bs_table
[3]);
416 term_printf("read: %" PRIu64
" \nwrite: %" PRIu64
" \n",
417 io
[3].read_byte_counter
, io
[3].write_byte_counter
);
419 term_printf("hdd not exist\n");
425 static void do_change(const char *device
, const char *filename
)
427 BlockDriverState
*bs
;
431 bs
= bdrv_find(device
);
433 term_printf("device not found\n");
436 if (eject_device(bs
, 0) < 0)
438 bdrv_open(bs
, filename
, 0);
439 if (bdrv_is_encrypted(bs
)) {
440 term_printf("%s is encrypted.\n", device
);
441 for(i
= 0; i
< 3; i
++) {
442 monitor_readline("Password: ", 1, password
, sizeof(password
));
443 if (bdrv_set_key(bs
, password
) == 0)
445 term_printf("invalid password\n");
450 static void do_screen_dump(const char *filename
)
452 vga_hw_screen_dump(filename
);
455 static void do_log(const char *items
)
459 if (!strcmp(items
, "none")) {
462 mask
= cpu_str_to_log_mask(items
);
471 static void do_stop(void)
473 vm_stop(EXCP_INTERRUPT
);
476 static void do_cont(void)
481 #ifdef CONFIG_GDBSTUB
482 static void do_gdbserver(int has_port
, int port
)
485 port
= DEFAULT_GDBSTUB_PORT
;
486 if (gdbserver_start_port(port
) < 0) {
487 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
489 qemu_printf("Waiting gdb connection on port %d\n", port
);
494 static void term_printc(int c
)
511 if (c
>= 32 && c
<= 126) {
512 term_printf("%c", c
);
514 term_printf("\\x%02x", c
);
521 static void memory_dump(int count
, int format
, int wsize
,
522 target_ulong addr
, int is_physical
)
525 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
533 if (!env
&& !is_physical
)
538 } else if (wsize
== 4) {
541 /* as default we use the current CS size */
545 if ((env
->efer
& MSR_EFER_LMA
) &&
546 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
550 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
555 monitor_disas(env
, addr
, count
, is_physical
, flags
);
564 nb_per_line
= line_size
/ wsize
;
569 max_digits
= (wsize
* 8 + 2) / 3;
573 max_digits
= (wsize
* 8) / 4;
577 max_digits
= (wsize
* 8 * 10 + 32) / 33;
585 term_printf(TARGET_FMT_lx
":", addr
);
590 cpu_physical_memory_rw(addr
, buf
, l
, 0);
595 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
602 v
= ldub_raw(buf
+ i
);
605 v
= lduw_raw(buf
+ i
);
608 v
= (uint32_t)ldl_raw(buf
+ i
);
611 v
= ldq_raw(buf
+ i
);
617 term_printf("%#*" PRIo64
, max_digits
, v
);
620 term_printf("0x%0*" PRIx64
, max_digits
, v
);
623 term_printf("%*" PRIu64
, max_digits
, v
);
626 term_printf("%*" PRId64
, max_digits
, v
);
640 #if TARGET_LONG_BITS == 64
641 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
643 #define GET_TLONG(h, l) (l)
646 static void do_memory_dump(int count
, int format
, int size
,
647 uint32_t addrh
, uint32_t addrl
)
649 target_long addr
= GET_TLONG(addrh
, addrl
);
650 memory_dump(count
, format
, size
, addr
, 0);
653 static void do_physical_memory_dump(int count
, int format
, int size
,
654 uint32_t addrh
, uint32_t addrl
)
657 target_long addr
= GET_TLONG(addrh
, addrl
);
658 memory_dump(count
, format
, size
, addr
, 1);
661 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
663 target_long val
= GET_TLONG(valh
, vall
);
664 #if TARGET_LONG_BITS == 32
667 term_printf("%#o", val
);
670 term_printf("%#x", val
);
673 term_printf("%u", val
);
677 term_printf("%d", val
);
686 term_printf("%#" PRIo64
, val
);
689 term_printf("%#" PRIx64
, val
);
692 term_printf("%" PRIu64
, val
);
696 term_printf("%" PRId64
, val
);
706 static void do_memory_save(unsigned int valh
, unsigned int vall
,
707 uint32_t size
, const char *filename
)
710 target_long addr
= GET_TLONG(valh
, vall
);
719 f
= fopen(filename
, "wb");
721 term_printf("could not open '%s'\n", filename
);
728 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
729 fwrite(buf
, 1, l
, f
);
736 static void do_sum(uint32_t start
, uint32_t size
)
743 for(addr
= start
; addr
< (start
+ size
); addr
++) {
744 cpu_physical_memory_rw(addr
, buf
, 1, 0);
745 /* BSD sum algorithm ('sum' Unix command) */
746 sum
= (sum
>> 1) | (sum
<< 15);
749 term_printf("%05d\n", sum
);
757 static const KeyDef key_defs
[] = {
782 { 0x0e, "backspace" },
817 { 0x3a, "caps_lock" },
828 { 0x45, "num_lock" },
829 { 0x46, "scroll_lock" },
831 { 0xb5, "kp_divide" },
832 { 0x37, "kp_multiply" },
833 { 0x4a, "kp_substract" },
835 { 0x9c, "kp_enter" },
836 { 0x53, "kp_decimal" },
871 static int get_keycode(const char *key
)
877 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
878 if (!strcmp(key
, p
->name
))
881 if (strstart(key
, "0x", NULL
)) {
882 ret
= strtoul(key
, &endp
, 0);
883 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
889 static void do_send_key(const char *string
)
892 uint8_t keycodes
[16];
894 int nb_keycodes
, keycode
, i
;
900 while (*p
!= '\0' && *p
!= '-') {
901 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
907 keycode
= get_keycode(keybuf
);
909 term_printf("unknown key: '%s'\n", keybuf
);
912 keycodes
[nb_keycodes
++] = keycode
;
917 /* key down events */
918 for(i
= 0; i
< nb_keycodes
; i
++) {
919 keycode
= keycodes
[i
];
921 kbd_put_keycode(0xe0);
922 kbd_put_keycode(keycode
& 0x7f);
925 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
926 keycode
= keycodes
[i
];
928 kbd_put_keycode(0xe0);
929 kbd_put_keycode(keycode
| 0x80);
933 static int mouse_button_state
;
935 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
939 dx
= strtol(dx_str
, NULL
, 0);
940 dy
= strtol(dy_str
, NULL
, 0);
943 dz
= strtol(dz_str
, NULL
, 0);
944 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
947 static void do_mouse_button(int button_state
)
949 mouse_button_state
= button_state
;
950 kbd_mouse_event(0, 0, 0, mouse_button_state
);
953 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
959 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
967 val
= cpu_inb(NULL
, addr
);
971 val
= cpu_inw(NULL
, addr
);
975 val
= cpu_inl(NULL
, addr
);
979 term_printf("port%c[0x%04x] = %#0*x\n",
980 suffix
, addr
, size
* 2, val
);
983 static void do_system_reset(void)
985 qemu_system_reset_request();
988 static void do_system_powerdown(void)
990 qemu_system_powerdown_request();
993 #if defined(TARGET_I386)
994 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
996 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
999 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1000 pte
& PG_PSE_MASK
? 'P' : '-',
1001 pte
& PG_DIRTY_MASK
? 'D' : '-',
1002 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1003 pte
& PG_PCD_MASK
? 'C' : '-',
1004 pte
& PG_PWT_MASK
? 'T' : '-',
1005 pte
& PG_USER_MASK
? 'U' : '-',
1006 pte
& PG_RW_MASK
? 'W' : '-');
1009 static void tlb_info(void)
1013 uint32_t pgd
, pde
, pte
;
1015 env
= mon_get_cpu();
1019 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1020 term_printf("PG disabled\n");
1023 pgd
= env
->cr
[3] & ~0xfff;
1024 for(l1
= 0; l1
< 1024; l1
++) {
1025 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1026 pde
= le32_to_cpu(pde
);
1027 if (pde
& PG_PRESENT_MASK
) {
1028 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1029 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1031 for(l2
= 0; l2
< 1024; l2
++) {
1032 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1033 (uint8_t *)&pte
, 4);
1034 pte
= le32_to_cpu(pte
);
1035 if (pte
& PG_PRESENT_MASK
) {
1036 print_pte((l1
<< 22) + (l2
<< 12),
1046 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1047 uint32_t end
, int prot
)
1050 prot1
= *plast_prot
;
1051 if (prot
!= prot1
) {
1052 if (*pstart
!= -1) {
1053 term_printf("%08x-%08x %08x %c%c%c\n",
1054 *pstart
, end
, end
- *pstart
,
1055 prot1
& PG_USER_MASK
? 'u' : '-',
1057 prot1
& PG_RW_MASK
? 'w' : '-');
1067 static void mem_info(void)
1070 int l1
, l2
, prot
, last_prot
;
1071 uint32_t pgd
, pde
, pte
, start
, end
;
1073 env
= mon_get_cpu();
1077 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1078 term_printf("PG disabled\n");
1081 pgd
= env
->cr
[3] & ~0xfff;
1084 for(l1
= 0; l1
< 1024; l1
++) {
1085 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1086 pde
= le32_to_cpu(pde
);
1088 if (pde
& PG_PRESENT_MASK
) {
1089 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1090 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1091 mem_print(&start
, &last_prot
, end
, prot
);
1093 for(l2
= 0; l2
< 1024; l2
++) {
1094 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1095 (uint8_t *)&pte
, 4);
1096 pte
= le32_to_cpu(pte
);
1097 end
= (l1
<< 22) + (l2
<< 12);
1098 if (pte
& PG_PRESENT_MASK
) {
1099 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1103 mem_print(&start
, &last_prot
, end
, prot
);
1108 mem_print(&start
, &last_prot
, end
, prot
);
1114 static void do_info_kqemu(void)
1120 env
= mon_get_cpu();
1122 term_printf("No cpu initialized yet");
1125 val
= env
->kqemu_enabled
;
1126 term_printf("kqemu support: ");
1130 term_printf("disabled\n");
1133 term_printf("enabled for user code\n");
1136 term_printf("enabled for user and kernel code\n");
1140 term_printf("kqemu support: not compiled\n");
1144 #ifdef CONFIG_PROFILER
1148 int64_t kqemu_exec_count
;
1150 int64_t kqemu_ret_int_count
;
1151 int64_t kqemu_ret_excp_count
;
1152 int64_t kqemu_ret_intr_count
;
1154 static void do_info_profile(void)
1160 term_printf("async time %" PRId64
" (%0.3f)\n",
1161 dev_time
, dev_time
/ (double)ticks_per_sec
);
1162 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1163 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1164 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1165 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1166 kqemu_time
/ (double)total
* 100.0,
1168 kqemu_ret_int_count
,
1169 kqemu_ret_excp_count
,
1170 kqemu_ret_intr_count
);
1173 kqemu_exec_count
= 0;
1175 kqemu_ret_int_count
= 0;
1176 kqemu_ret_excp_count
= 0;
1177 kqemu_ret_intr_count
= 0;
1179 kqemu_record_dump();
1183 static void do_info_profile(void)
1185 term_printf("Internal profiler not compiled\n");
1189 /* Capture support */
1190 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1192 static void do_info_capture (void)
1197 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1198 term_printf ("[%d]: ", i
);
1199 s
->ops
.info (s
->opaque
);
1203 static void do_stop_capture (int n
)
1208 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1210 s
->ops
.destroy (s
->opaque
);
1211 LIST_REMOVE (s
, entries
);
1219 int wav_start_capture (CaptureState
*s
, const char *path
, int freq
,
1220 int bits
, int nchannels
);
1222 static void do_wav_capture (const char *path
,
1223 int has_freq
, int freq
,
1224 int has_bits
, int bits
,
1225 int has_channels
, int nchannels
)
1229 s
= qemu_mallocz (sizeof (*s
));
1231 term_printf ("Not enough memory to add wave capture\n");
1235 freq
= has_freq
? freq
: 44100;
1236 bits
= has_bits
? bits
: 16;
1237 nchannels
= has_channels
? nchannels
: 2;
1239 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1240 term_printf ("Faied to add wave capture\n");
1243 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1247 static term_cmd_t term_cmds
[] = {
1248 { "help|?", "s?", do_help
,
1249 "[cmd]", "show the help" },
1250 { "commit", "s", do_commit
,
1251 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1252 { "info", "s?", do_info
,
1253 "subcommand", "show various information about the system state" },
1254 { "q|quit", "", do_quit
,
1255 "", "quit the emulator" },
1256 { "eject", "-fB", do_eject
,
1257 "[-f] device", "eject a removable media (use -f to force it)" },
1258 { "change", "BF", do_change
,
1259 "device filename", "change a removable media" },
1260 { "screendump", "F", do_screen_dump
,
1261 "filename", "save screen into PPM image 'filename'" },
1262 { "log", "s", do_log
,
1263 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1264 { "savevm", "s?", do_savevm
,
1265 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1266 { "loadvm", "s", do_loadvm
,
1267 "tag|id", "restore a VM snapshot from its tag or id" },
1268 { "delvm", "s", do_delvm
,
1269 "tag|id", "delete a VM snapshot from its tag or id" },
1270 { "stop", "", do_stop
,
1271 "", "stop emulation", },
1272 { "c|cont", "", do_cont
,
1273 "", "resume emulation", },
1274 #ifdef CONFIG_GDBSTUB
1275 { "gdbserver", "i?", do_gdbserver
,
1276 "[port]", "start gdbserver session (default port=1234)", },
1278 { "x", "/l", do_memory_dump
,
1279 "/fmt addr", "virtual memory dump starting at 'addr'", },
1280 { "xp", "/l", do_physical_memory_dump
,
1281 "/fmt addr", "physical memory dump starting at 'addr'", },
1282 { "p|print", "/l", do_print
,
1283 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1284 { "i", "/ii.", do_ioport_read
,
1285 "/fmt addr", "I/O port read" },
1287 { "sendkey", "s", do_send_key
,
1288 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1289 { "system_reset", "", do_system_reset
,
1290 "", "reset the system" },
1291 { "system_powerdown", "", do_system_powerdown
,
1292 "", "send system power down event" },
1293 { "sum", "ii", do_sum
,
1294 "addr size", "compute the checksum of a memory region" },
1295 { "usb_add", "s", do_usb_add
,
1296 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1297 { "usb_del", "s", do_usb_del
,
1298 "device", "remove USB device 'bus.addr'" },
1299 { "cpu", "i", do_cpu_set
,
1300 "index", "set the default CPU" },
1301 { "mouse_move", "sss?", do_mouse_move
,
1302 "dx dy [dz]", "send mouse move events" },
1303 { "mouse_button", "i", do_mouse_button
,
1304 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1305 { "mouse_set", "i", do_mouse_set
,
1306 "index", "set which mouse device receives events" },
1308 { "wavcapture", "si?i?i?", do_wav_capture
,
1309 "path [frequency bits channels]",
1310 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1312 { "stopcapture", "i", do_stop_capture
,
1313 "capture index", "stop capture" },
1314 { "memsave", "lis", do_memory_save
,
1315 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'" },
1316 { "read_disk_io", "s", do_io_statistics
,
1317 "hdx", "read disk I/O statistics (VMDK format)" },
1318 { "migrate", "-ds", do_migrate
,
1319 "[-d] command", "migrate the VM using command (use -d to not wait for command to complete)" },
1320 { "migrate_cancel", "", do_migrate_cancel
,
1321 "", "cancel the current VM migration" },
1322 { "migrate_set_speed", "s", do_migrate_set_speed
,
1323 "value", "set maximum speed (in bytes) for migrations" },
1327 static term_cmd_t info_cmds
[] = {
1328 { "version", "", do_info_version
,
1329 "", "show the version of qemu" },
1330 { "network", "", do_info_network
,
1331 "", "show the network state" },
1332 { "block", "", do_info_block
,
1333 "", "show the block devices" },
1334 { "registers", "", do_info_registers
,
1335 "", "show the cpu registers" },
1336 { "cpus", "", do_info_cpus
,
1337 "", "show infos for each CPU" },
1338 { "history", "", do_info_history
,
1339 "", "show the command line history", },
1340 { "irq", "", irq_info
,
1341 "", "show the interrupts statistics (if available)", },
1342 { "pic", "", pic_info
,
1343 "", "show i8259 (PIC) state", },
1344 { "pci", "", pci_info
,
1345 "", "show PCI info", },
1346 #if defined(TARGET_I386)
1347 { "tlb", "", tlb_info
,
1348 "", "show virtual to physical memory mappings", },
1349 { "mem", "", mem_info
,
1350 "", "show the active virtual memory mappings", },
1352 { "jit", "", do_info_jit
,
1353 "", "show dynamic compiler info", },
1354 { "kqemu", "", do_info_kqemu
,
1355 "", "show kqemu information", },
1356 { "usb", "", usb_info
,
1357 "", "show guest USB devices", },
1358 { "usbhost", "", usb_host_info
,
1359 "", "show host USB devices", },
1360 { "profile", "", do_info_profile
,
1361 "", "show profiling information", },
1362 { "capture", "", do_info_capture
,
1363 "", "show capture information" },
1364 { "snapshots", "", do_info_snapshots
,
1365 "", "show the currently saved VM snapshots" },
1366 { "mice", "", do_info_mice
,
1367 "", "show which guest mouse is receiving events" },
1368 { "vnc", "", do_info_vnc
,
1369 "", "show the vnc server status"},
1370 { "migration", "", do_info_migration
,
1371 "", "show migration information" },
1375 /*******************************************************************/
1377 static const char *pch
;
1378 static jmp_buf expr_env
;
1383 typedef struct MonitorDef
{
1386 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
1390 #if defined(TARGET_I386)
1391 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
1393 CPUState
*env
= mon_get_cpu();
1396 return env
->eip
+ env
->segs
[R_CS
].base
;
1400 #if defined(TARGET_PPC)
1401 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
1403 CPUState
*env
= mon_get_cpu();
1411 for (i
= 0; i
< 8; i
++)
1412 u
|= env
->crf
[i
] << (32 - (4 * i
));
1417 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
1419 CPUState
*env
= mon_get_cpu();
1422 return (env
->msr
[MSR_POW
] << MSR_POW
) |
1423 (env
->msr
[MSR_ILE
] << MSR_ILE
) |
1424 (env
->msr
[MSR_EE
] << MSR_EE
) |
1425 (env
->msr
[MSR_PR
] << MSR_PR
) |
1426 (env
->msr
[MSR_FP
] << MSR_FP
) |
1427 (env
->msr
[MSR_ME
] << MSR_ME
) |
1428 (env
->msr
[MSR_FE0
] << MSR_FE0
) |
1429 (env
->msr
[MSR_SE
] << MSR_SE
) |
1430 (env
->msr
[MSR_BE
] << MSR_BE
) |
1431 (env
->msr
[MSR_FE1
] << MSR_FE1
) |
1432 (env
->msr
[MSR_IP
] << MSR_IP
) |
1433 (env
->msr
[MSR_IR
] << MSR_IR
) |
1434 (env
->msr
[MSR_DR
] << MSR_DR
) |
1435 (env
->msr
[MSR_RI
] << MSR_RI
) |
1436 (env
->msr
[MSR_LE
] << MSR_LE
);
1439 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
1441 CPUState
*env
= mon_get_cpu();
1444 return (env
->xer
[XER_SO
] << XER_SO
) |
1445 (env
->xer
[XER_OV
] << XER_OV
) |
1446 (env
->xer
[XER_CA
] << XER_CA
) |
1447 (env
->xer
[XER_BC
] << XER_BC
);
1450 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1452 CPUState
*env
= mon_get_cpu();
1455 return cpu_ppc_load_decr(env
);
1458 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1460 CPUState
*env
= mon_get_cpu();
1463 return cpu_ppc_load_tbu(env
);
1466 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1468 CPUState
*env
= mon_get_cpu();
1471 return cpu_ppc_load_tbl(env
);
1475 #if defined(TARGET_SPARC)
1476 #ifndef TARGET_SPARC64
1477 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1479 CPUState
*env
= mon_get_cpu();
1482 return GET_PSR(env
);
1486 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1488 CPUState
*env
= mon_get_cpu();
1491 return env
->regwptr
[val
];
1495 static MonitorDef monitor_defs
[] = {
1498 #define SEG(name, seg) \
1499 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1500 { name ".base", offsetof(CPUState, segs[seg].base) },\
1501 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1503 { "eax", offsetof(CPUState
, regs
[0]) },
1504 { "ecx", offsetof(CPUState
, regs
[1]) },
1505 { "edx", offsetof(CPUState
, regs
[2]) },
1506 { "ebx", offsetof(CPUState
, regs
[3]) },
1507 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1508 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1509 { "esi", offsetof(CPUState
, regs
[6]) },
1510 { "edi", offsetof(CPUState
, regs
[7]) },
1511 #ifdef TARGET_X86_64
1512 { "r8", offsetof(CPUState
, regs
[8]) },
1513 { "r9", offsetof(CPUState
, regs
[9]) },
1514 { "r10", offsetof(CPUState
, regs
[10]) },
1515 { "r11", offsetof(CPUState
, regs
[11]) },
1516 { "r12", offsetof(CPUState
, regs
[12]) },
1517 { "r13", offsetof(CPUState
, regs
[13]) },
1518 { "r14", offsetof(CPUState
, regs
[14]) },
1519 { "r15", offsetof(CPUState
, regs
[15]) },
1521 { "eflags", offsetof(CPUState
, eflags
) },
1522 { "eip", offsetof(CPUState
, eip
) },
1529 { "pc", 0, monitor_get_pc
, },
1530 #elif defined(TARGET_PPC)
1531 { "r0", offsetof(CPUState
, gpr
[0]) },
1532 { "r1", offsetof(CPUState
, gpr
[1]) },
1533 { "r2", offsetof(CPUState
, gpr
[2]) },
1534 { "r3", offsetof(CPUState
, gpr
[3]) },
1535 { "r4", offsetof(CPUState
, gpr
[4]) },
1536 { "r5", offsetof(CPUState
, gpr
[5]) },
1537 { "r6", offsetof(CPUState
, gpr
[6]) },
1538 { "r7", offsetof(CPUState
, gpr
[7]) },
1539 { "r8", offsetof(CPUState
, gpr
[8]) },
1540 { "r9", offsetof(CPUState
, gpr
[9]) },
1541 { "r10", offsetof(CPUState
, gpr
[10]) },
1542 { "r11", offsetof(CPUState
, gpr
[11]) },
1543 { "r12", offsetof(CPUState
, gpr
[12]) },
1544 { "r13", offsetof(CPUState
, gpr
[13]) },
1545 { "r14", offsetof(CPUState
, gpr
[14]) },
1546 { "r15", offsetof(CPUState
, gpr
[15]) },
1547 { "r16", offsetof(CPUState
, gpr
[16]) },
1548 { "r17", offsetof(CPUState
, gpr
[17]) },
1549 { "r18", offsetof(CPUState
, gpr
[18]) },
1550 { "r19", offsetof(CPUState
, gpr
[19]) },
1551 { "r20", offsetof(CPUState
, gpr
[20]) },
1552 { "r21", offsetof(CPUState
, gpr
[21]) },
1553 { "r22", offsetof(CPUState
, gpr
[22]) },
1554 { "r23", offsetof(CPUState
, gpr
[23]) },
1555 { "r24", offsetof(CPUState
, gpr
[24]) },
1556 { "r25", offsetof(CPUState
, gpr
[25]) },
1557 { "r26", offsetof(CPUState
, gpr
[26]) },
1558 { "r27", offsetof(CPUState
, gpr
[27]) },
1559 { "r28", offsetof(CPUState
, gpr
[28]) },
1560 { "r29", offsetof(CPUState
, gpr
[29]) },
1561 { "r30", offsetof(CPUState
, gpr
[30]) },
1562 { "r31", offsetof(CPUState
, gpr
[31]) },
1563 { "nip|pc", offsetof(CPUState
, nip
) },
1564 { "lr", offsetof(CPUState
, lr
) },
1565 { "ctr", offsetof(CPUState
, ctr
) },
1566 { "decr", 0, &monitor_get_decr
, },
1567 { "ccr", 0, &monitor_get_ccr
, },
1568 { "msr", 0, &monitor_get_msr
, },
1569 { "xer", 0, &monitor_get_xer
, },
1570 { "tbu", 0, &monitor_get_tbu
, },
1571 { "tbl", 0, &monitor_get_tbl
, },
1572 { "sdr1", offsetof(CPUState
, sdr1
) },
1573 { "sr0", offsetof(CPUState
, sr
[0]) },
1574 { "sr1", offsetof(CPUState
, sr
[1]) },
1575 { "sr2", offsetof(CPUState
, sr
[2]) },
1576 { "sr3", offsetof(CPUState
, sr
[3]) },
1577 { "sr4", offsetof(CPUState
, sr
[4]) },
1578 { "sr5", offsetof(CPUState
, sr
[5]) },
1579 { "sr6", offsetof(CPUState
, sr
[6]) },
1580 { "sr7", offsetof(CPUState
, sr
[7]) },
1581 { "sr8", offsetof(CPUState
, sr
[8]) },
1582 { "sr9", offsetof(CPUState
, sr
[9]) },
1583 { "sr10", offsetof(CPUState
, sr
[10]) },
1584 { "sr11", offsetof(CPUState
, sr
[11]) },
1585 { "sr12", offsetof(CPUState
, sr
[12]) },
1586 { "sr13", offsetof(CPUState
, sr
[13]) },
1587 { "sr14", offsetof(CPUState
, sr
[14]) },
1588 { "sr15", offsetof(CPUState
, sr
[15]) },
1589 /* Too lazy to put BATs and SPRs ... */
1590 #elif defined(TARGET_SPARC)
1591 { "g0", offsetof(CPUState
, gregs
[0]) },
1592 { "g1", offsetof(CPUState
, gregs
[1]) },
1593 { "g2", offsetof(CPUState
, gregs
[2]) },
1594 { "g3", offsetof(CPUState
, gregs
[3]) },
1595 { "g4", offsetof(CPUState
, gregs
[4]) },
1596 { "g5", offsetof(CPUState
, gregs
[5]) },
1597 { "g6", offsetof(CPUState
, gregs
[6]) },
1598 { "g7", offsetof(CPUState
, gregs
[7]) },
1599 { "o0", 0, monitor_get_reg
},
1600 { "o1", 1, monitor_get_reg
},
1601 { "o2", 2, monitor_get_reg
},
1602 { "o3", 3, monitor_get_reg
},
1603 { "o4", 4, monitor_get_reg
},
1604 { "o5", 5, monitor_get_reg
},
1605 { "o6", 6, monitor_get_reg
},
1606 { "o7", 7, monitor_get_reg
},
1607 { "l0", 8, monitor_get_reg
},
1608 { "l1", 9, monitor_get_reg
},
1609 { "l2", 10, monitor_get_reg
},
1610 { "l3", 11, monitor_get_reg
},
1611 { "l4", 12, monitor_get_reg
},
1612 { "l5", 13, monitor_get_reg
},
1613 { "l6", 14, monitor_get_reg
},
1614 { "l7", 15, monitor_get_reg
},
1615 { "i0", 16, monitor_get_reg
},
1616 { "i1", 17, monitor_get_reg
},
1617 { "i2", 18, monitor_get_reg
},
1618 { "i3", 19, monitor_get_reg
},
1619 { "i4", 20, monitor_get_reg
},
1620 { "i5", 21, monitor_get_reg
},
1621 { "i6", 22, monitor_get_reg
},
1622 { "i7", 23, monitor_get_reg
},
1623 { "pc", offsetof(CPUState
, pc
) },
1624 { "npc", offsetof(CPUState
, npc
) },
1625 { "y", offsetof(CPUState
, y
) },
1626 #ifndef TARGET_SPARC64
1627 { "psr", 0, &monitor_get_psr
, },
1628 { "wim", offsetof(CPUState
, wim
) },
1630 { "tbr", offsetof(CPUState
, tbr
) },
1631 { "fsr", offsetof(CPUState
, fsr
) },
1632 { "f0", offsetof(CPUState
, fpr
[0]) },
1633 { "f1", offsetof(CPUState
, fpr
[1]) },
1634 { "f2", offsetof(CPUState
, fpr
[2]) },
1635 { "f3", offsetof(CPUState
, fpr
[3]) },
1636 { "f4", offsetof(CPUState
, fpr
[4]) },
1637 { "f5", offsetof(CPUState
, fpr
[5]) },
1638 { "f6", offsetof(CPUState
, fpr
[6]) },
1639 { "f7", offsetof(CPUState
, fpr
[7]) },
1640 { "f8", offsetof(CPUState
, fpr
[8]) },
1641 { "f9", offsetof(CPUState
, fpr
[9]) },
1642 { "f10", offsetof(CPUState
, fpr
[10]) },
1643 { "f11", offsetof(CPUState
, fpr
[11]) },
1644 { "f12", offsetof(CPUState
, fpr
[12]) },
1645 { "f13", offsetof(CPUState
, fpr
[13]) },
1646 { "f14", offsetof(CPUState
, fpr
[14]) },
1647 { "f15", offsetof(CPUState
, fpr
[15]) },
1648 { "f16", offsetof(CPUState
, fpr
[16]) },
1649 { "f17", offsetof(CPUState
, fpr
[17]) },
1650 { "f18", offsetof(CPUState
, fpr
[18]) },
1651 { "f19", offsetof(CPUState
, fpr
[19]) },
1652 { "f20", offsetof(CPUState
, fpr
[20]) },
1653 { "f21", offsetof(CPUState
, fpr
[21]) },
1654 { "f22", offsetof(CPUState
, fpr
[22]) },
1655 { "f23", offsetof(CPUState
, fpr
[23]) },
1656 { "f24", offsetof(CPUState
, fpr
[24]) },
1657 { "f25", offsetof(CPUState
, fpr
[25]) },
1658 { "f26", offsetof(CPUState
, fpr
[26]) },
1659 { "f27", offsetof(CPUState
, fpr
[27]) },
1660 { "f28", offsetof(CPUState
, fpr
[28]) },
1661 { "f29", offsetof(CPUState
, fpr
[29]) },
1662 { "f30", offsetof(CPUState
, fpr
[30]) },
1663 { "f31", offsetof(CPUState
, fpr
[31]) },
1664 #ifdef TARGET_SPARC64
1665 { "f32", offsetof(CPUState
, fpr
[32]) },
1666 { "f34", offsetof(CPUState
, fpr
[34]) },
1667 { "f36", offsetof(CPUState
, fpr
[36]) },
1668 { "f38", offsetof(CPUState
, fpr
[38]) },
1669 { "f40", offsetof(CPUState
, fpr
[40]) },
1670 { "f42", offsetof(CPUState
, fpr
[42]) },
1671 { "f44", offsetof(CPUState
, fpr
[44]) },
1672 { "f46", offsetof(CPUState
, fpr
[46]) },
1673 { "f48", offsetof(CPUState
, fpr
[48]) },
1674 { "f50", offsetof(CPUState
, fpr
[50]) },
1675 { "f52", offsetof(CPUState
, fpr
[52]) },
1676 { "f54", offsetof(CPUState
, fpr
[54]) },
1677 { "f56", offsetof(CPUState
, fpr
[56]) },
1678 { "f58", offsetof(CPUState
, fpr
[58]) },
1679 { "f60", offsetof(CPUState
, fpr
[60]) },
1680 { "f62", offsetof(CPUState
, fpr
[62]) },
1681 { "asi", offsetof(CPUState
, asi
) },
1682 { "pstate", offsetof(CPUState
, pstate
) },
1683 { "cansave", offsetof(CPUState
, cansave
) },
1684 { "canrestore", offsetof(CPUState
, canrestore
) },
1685 { "otherwin", offsetof(CPUState
, otherwin
) },
1686 { "wstate", offsetof(CPUState
, wstate
) },
1687 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1688 { "fprs", offsetof(CPUState
, fprs
) },
1694 static void expr_error(const char *fmt
)
1698 longjmp(expr_env
, 1);
1701 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1702 static int get_monitor_def(target_long
*pval
, const char *name
)
1707 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1708 if (compare_cmd(name
, md
->name
)) {
1709 if (md
->get_value
) {
1710 *pval
= md
->get_value(md
, md
->offset
);
1712 CPUState
*env
= mon_get_cpu();
1715 ptr
= (uint8_t *)env
+ md
->offset
;
1718 *pval
= *(int32_t *)ptr
;
1721 *pval
= *(target_long
*)ptr
;
1734 static void next(void)
1738 while (isspace(*pch
))
1743 static target_long
expr_sum(void);
1745 static target_long
expr_unary(void)
1768 expr_error("')' expected");
1775 expr_error("character constant expected");
1779 expr_error("missing terminating \' character");
1788 while ((*pch
>= 'a' && *pch
<= 'z') ||
1789 (*pch
>= 'A' && *pch
<= 'Z') ||
1790 (*pch
>= '0' && *pch
<= '9') ||
1791 *pch
== '_' || *pch
== '.') {
1792 if ((q
- buf
) < sizeof(buf
) - 1)
1796 while (isspace(*pch
))
1799 ret
= get_monitor_def(&n
, buf
);
1801 expr_error("unknown register");
1803 expr_error("no cpu defined");
1807 expr_error("unexpected end of expression");
1811 #if TARGET_LONG_BITS == 64
1812 n
= strtoull(pch
, &p
, 0);
1814 n
= strtoul(pch
, &p
, 0);
1817 expr_error("invalid char in expression");
1820 while (isspace(*pch
))
1828 static target_long
expr_prod(void)
1830 target_long val
, val2
;
1836 if (op
!= '*' && op
!= '/' && op
!= '%')
1839 val2
= expr_unary();
1848 expr_error("division by zero");
1859 static target_long
expr_logic(void)
1861 target_long val
, val2
;
1867 if (op
!= '&' && op
!= '|' && op
!= '^')
1887 static target_long
expr_sum(void)
1889 target_long val
, val2
;
1895 if (op
!= '+' && op
!= '-')
1898 val2
= expr_logic();
1907 static int get_expr(target_long
*pval
, const char **pp
)
1910 if (setjmp(expr_env
)) {
1914 while (isspace(*pch
))
1921 static int get_str(char *buf
, int buf_size
, const char **pp
)
1939 while (*p
!= '\0' && *p
!= '\"') {
1955 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1958 if ((q
- buf
) < buf_size
- 1) {
1962 if ((q
- buf
) < buf_size
- 1) {
1969 qemu_printf("unterminated string\n");
1974 while (*p
!= '\0' && !isspace(*p
)) {
1975 if ((q
- buf
) < buf_size
- 1) {
1986 static int default_fmt_format
= 'x';
1987 static int default_fmt_size
= 4;
1991 static void monitor_handle_command(const char *cmdline
)
1993 const char *p
, *pstart
, *typestr
;
1995 int c
, nb_args
, len
, i
, has_arg
;
1999 void *str_allocated
[MAX_ARGS
];
2000 void *args
[MAX_ARGS
];
2003 term_printf("command='%s'\n", cmdline
);
2006 /* extract the command name */
2014 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
2017 if (len
> sizeof(cmdname
) - 1)
2018 len
= sizeof(cmdname
) - 1;
2019 memcpy(cmdname
, pstart
, len
);
2020 cmdname
[len
] = '\0';
2022 /* find the command */
2023 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2024 if (compare_cmd(cmdname
, cmd
->name
))
2027 term_printf("unknown command: '%s'\n", cmdname
);
2031 for(i
= 0; i
< MAX_ARGS
; i
++)
2032 str_allocated
[i
] = NULL
;
2034 /* parse the parameters */
2035 typestr
= cmd
->args_type
;
2052 if (*typestr
== '?') {
2055 /* no optional string: NULL argument */
2060 ret
= get_str(buf
, sizeof(buf
), &p
);
2064 term_printf("%s: filename expected\n", cmdname
);
2067 term_printf("%s: block device name expected\n", cmdname
);
2070 term_printf("%s: string expected\n", cmdname
);
2075 str
= qemu_malloc(strlen(buf
) + 1);
2077 str_allocated
[nb_args
] = str
;
2079 if (nb_args
>= MAX_ARGS
) {
2081 term_printf("%s: too many arguments\n", cmdname
);
2084 args
[nb_args
++] = str
;
2089 int count
, format
, size
;
2099 while (isdigit(*p
)) {
2100 count
= count
* 10 + (*p
- '0');
2138 if (*p
!= '\0' && !isspace(*p
)) {
2139 term_printf("invalid char in format: '%c'\n", *p
);
2143 format
= default_fmt_format
;
2144 if (format
!= 'i') {
2145 /* for 'i', not specifying a size gives -1 as size */
2147 size
= default_fmt_size
;
2149 default_fmt_size
= size
;
2150 default_fmt_format
= format
;
2153 format
= default_fmt_format
;
2154 if (format
!= 'i') {
2155 size
= default_fmt_size
;
2160 if (nb_args
+ 3 > MAX_ARGS
)
2162 args
[nb_args
++] = (void*)count
;
2163 args
[nb_args
++] = (void*)format
;
2164 args
[nb_args
++] = (void*)size
;
2173 if (*typestr
== '?' || *typestr
== '.') {
2174 if (*typestr
== '?') {
2190 if (nb_args
>= MAX_ARGS
)
2192 args
[nb_args
++] = (void *)has_arg
;
2194 if (nb_args
>= MAX_ARGS
)
2200 if (get_expr(&val
, &p
))
2204 if (nb_args
>= MAX_ARGS
)
2206 args
[nb_args
++] = (void *)(int)val
;
2208 if ((nb_args
+ 1) >= MAX_ARGS
)
2210 #if TARGET_LONG_BITS == 64
2211 args
[nb_args
++] = (void *)(int)((val
>> 32) & 0xffffffff);
2213 args
[nb_args
++] = (void *)0;
2215 args
[nb_args
++] = (void *)(int)(val
& 0xffffffff);
2233 term_printf("%s: unsupported option -%c\n",
2240 if (nb_args
>= MAX_ARGS
)
2242 args
[nb_args
++] = (void *)has_option
;
2247 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2251 /* check that all arguments were parsed */
2255 term_printf("%s: extraneous characters at the end of line\n",
2265 cmd
->handler(args
[0]);
2268 cmd
->handler(args
[0], args
[1]);
2271 cmd
->handler(args
[0], args
[1], args
[2]);
2274 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
2277 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
2280 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2283 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2286 term_printf("unsupported number of arguments: %d\n", nb_args
);
2290 for(i
= 0; i
< MAX_ARGS
; i
++)
2291 qemu_free(str_allocated
[i
]);
2295 static void cmd_completion(const char *name
, const char *list
)
2297 const char *p
, *pstart
;
2306 p
= pstart
+ strlen(pstart
);
2308 if (len
> sizeof(cmd
) - 2)
2309 len
= sizeof(cmd
) - 2;
2310 memcpy(cmd
, pstart
, len
);
2312 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2313 add_completion(cmd
);
2321 static void file_completion(const char *input
)
2326 char file
[1024], file_prefix
[1024];
2330 p
= strrchr(input
, '/');
2333 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2336 input_path_len
= p
- input
+ 1;
2337 memcpy(path
, input
, input_path_len
);
2338 if (input_path_len
> sizeof(path
) - 1)
2339 input_path_len
= sizeof(path
) - 1;
2340 path
[input_path_len
] = '\0';
2341 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2343 #ifdef DEBUG_COMPLETION
2344 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2346 ffs
= opendir(path
);
2354 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2355 memcpy(file
, input
, input_path_len
);
2356 strcpy(file
+ input_path_len
, d
->d_name
);
2357 /* stat the file to find out if it's a directory.
2358 * In that case add a slash to speed up typing long paths
2361 if(S_ISDIR(sb
.st_mode
))
2363 add_completion(file
);
2369 static void block_completion_it(void *opaque
, const char *name
)
2371 const char *input
= opaque
;
2373 if (input
[0] == '\0' ||
2374 !strncmp(name
, (char *)input
, strlen(input
))) {
2375 add_completion(name
);
2379 /* NOTE: this parser is an approximate form of the real command parser */
2380 static void parse_cmdline(const char *cmdline
,
2381 int *pnb_args
, char **args
)
2394 if (nb_args
>= MAX_ARGS
)
2396 ret
= get_str(buf
, sizeof(buf
), &p
);
2397 args
[nb_args
] = qemu_strdup(buf
);
2402 *pnb_args
= nb_args
;
2405 void readline_find_completion(const char *cmdline
)
2407 const char *cmdname
;
2408 char *args
[MAX_ARGS
];
2409 int nb_args
, i
, len
;
2410 const char *ptype
, *str
;
2414 parse_cmdline(cmdline
, &nb_args
, args
);
2415 #ifdef DEBUG_COMPLETION
2416 for(i
= 0; i
< nb_args
; i
++) {
2417 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2421 /* if the line ends with a space, it means we want to complete the
2423 len
= strlen(cmdline
);
2424 if (len
> 0 && isspace(cmdline
[len
- 1])) {
2425 if (nb_args
>= MAX_ARGS
)
2427 args
[nb_args
++] = qemu_strdup("");
2430 /* command completion */
2435 completion_index
= strlen(cmdname
);
2436 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2437 cmd_completion(cmdname
, cmd
->name
);
2440 /* find the command */
2441 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2442 if (compare_cmd(args
[0], cmd
->name
))
2447 ptype
= cmd
->args_type
;
2448 for(i
= 0; i
< nb_args
- 2; i
++) {
2449 if (*ptype
!= '\0') {
2451 while (*ptype
== '?')
2455 str
= args
[nb_args
- 1];
2458 /* file completion */
2459 completion_index
= strlen(str
);
2460 file_completion(str
);
2463 /* block device name completion */
2464 completion_index
= strlen(str
);
2465 bdrv_iterate(block_completion_it
, (void *)str
);
2468 /* XXX: more generic ? */
2469 if (!strcmp(cmd
->name
, "info")) {
2470 completion_index
= strlen(str
);
2471 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2472 cmd_completion(str
, cmd
->name
);
2474 } else if (!strcmp(cmd
->name
, "sendkey")) {
2475 completion_index
= strlen(str
);
2476 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2477 cmd_completion(str
, key
->name
);
2485 for(i
= 0; i
< nb_args
; i
++)
2489 static int term_can_read(void *opaque
)
2494 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2497 for(i
= 0; i
< size
; i
++)
2498 readline_handle_byte(buf
[i
]);
2501 static int monitor_suspended
;
2503 void monitor_suspend(void)
2505 monitor_suspended
= 1;
2508 void monitor_resume(void)
2510 monitor_suspended
= 0;
2511 monitor_start_input();
2514 static void monitor_start_input(void);
2516 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2518 monitor_handle_command(cmdline
);
2519 if (!monitor_suspended
)
2520 monitor_start_input();
2523 static void monitor_start_input(void)
2525 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2528 static void term_event(void *opaque
, int event
)
2530 if (event
!= CHR_EVENT_RESET
)
2534 term_printf("QEMU %s monitor - type 'help' for more information\n",
2536 monitor_start_input();
2539 void monitor_init(CharDriverState
*hd
, int show_banner
)
2542 hide_banner
= !show_banner
;
2544 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2547 /* XXX: use threads ? */
2548 /* modal monitor readline */
2549 static int monitor_readline_started
;
2550 static char *monitor_readline_buf
;
2551 static int monitor_readline_buf_size
;
2553 static void monitor_readline_cb(void *opaque
, const char *input
)
2555 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2556 monitor_readline_started
= 0;
2559 void monitor_readline(const char *prompt
, int is_password
,
2560 char *buf
, int buf_size
)
2563 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
2565 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2566 monitor_readline_buf
= buf
;
2567 monitor_readline_buf_size
= buf_size
;
2568 monitor_readline_started
= 1;
2569 while (monitor_readline_started
) {