4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 //#define DEBUG_COMPLETION
32 #define offsetof(type, field) ((size_t) &((type *)0)->field)
39 * 'B' block device name
40 * 's' string (accept optional quote)
42 * 'l' target long (32 or 64 bit)
43 * '/' optional gdb-like print format (like "/10x")
45 * '?' optional type (for 'F', 's' and 'i')
49 typedef struct term_cmd_t
{
51 const char *args_type
;
57 static CharDriverState
*monitor_hd
;
58 static int hide_banner
;
60 static term_cmd_t term_cmds
[];
61 static term_cmd_t info_cmds
[];
63 static char term_outbuf
[1024];
64 static int term_outbuf_index
;
66 static void monitor_start_input(void);
68 CPUState
*mon_cpu
= NULL
;
72 if (term_outbuf_index
> 0) {
73 qemu_chr_write(monitor_hd
, term_outbuf
, term_outbuf_index
);
74 term_outbuf_index
= 0;
78 /* flush at every end of line or if the buffer is full */
79 void term_puts(const char *str
)
87 term_outbuf
[term_outbuf_index
++] = '\r';
88 term_outbuf
[term_outbuf_index
++] = c
;
89 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
95 void term_vprintf(const char *fmt
, va_list ap
)
98 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
102 void term_printf(const char *fmt
, ...)
106 term_vprintf(fmt
, ap
);
110 void term_print_filename(const char *filename
)
114 for (i
= 0; filename
[i
]; i
++) {
115 switch (filename
[i
]) {
119 term_printf("\\%c", filename
[i
]);
131 term_printf("%c", filename
[i
]);
137 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
141 term_vprintf(fmt
, ap
);
146 static int compare_cmd(const char *name
, const char *list
)
148 const char *p
, *pstart
;
156 p
= pstart
+ strlen(pstart
);
157 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
166 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
170 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
171 if (!name
|| !strcmp(name
, cmd
->name
))
172 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
176 static void help_cmd(const char *name
)
178 if (name
&& !strcmp(name
, "info")) {
179 help_cmd1(info_cmds
, "info ", NULL
);
181 help_cmd1(term_cmds
, "", name
);
182 if (name
&& !strcmp(name
, "log")) {
184 term_printf("Log items (comma separated):\n");
185 term_printf("%-10s %s\n", "none", "remove all logs");
186 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
187 term_printf("%-10s %s\n", item
->name
, item
->help
);
193 static void do_help(const char *name
)
198 static void do_commit(const char *device
)
202 all_devices
= !strcmp(device
, "all");
203 for (i
= 0; i
< MAX_DISKS
; i
++) {
206 !strcmp(bdrv_get_device_name(bs_table
[i
]), device
))
207 bdrv_commit(bs_table
[i
]);
212 static void do_info(const char *item
)
218 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
219 if (compare_cmd(item
, cmd
->name
))
229 static void do_info_version(void)
231 term_printf("%s\n", QEMU_VERSION
);
234 static void do_info_block(void)
239 /* get the current CPU defined by the user */
240 int mon_set_cpu(int cpu_index
)
244 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
245 if (env
->cpu_index
== cpu_index
) {
253 CPUState
*mon_get_cpu(void)
261 static void do_info_registers(void)
268 cpu_dump_state(env
, NULL
, monitor_fprintf
,
271 cpu_dump_state(env
, NULL
, monitor_fprintf
,
276 static void do_info_cpus(void)
280 /* just to set the default cpu if not already done */
283 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
284 term_printf("%c CPU #%d:",
285 (env
== mon_cpu
) ? '*' : ' ',
287 #if defined(TARGET_I386)
288 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
289 if (env
->hflags
& HF_HALTED_MASK
)
290 term_printf(" (halted)");
291 #elif defined(TARGET_PPC)
292 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
294 term_printf(" (halted)");
295 #elif defined(TARGET_SPARC)
296 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
298 term_printf(" (halted)");
304 static void do_cpu_set(int index
)
306 if (mon_set_cpu(index
) < 0)
307 term_printf("Invalid CPU index\n");
310 static void do_info_jit(void)
312 dump_exec_info(NULL
, monitor_fprintf
);
315 static void do_info_history (void)
322 str
= readline_get_history(i
);
325 term_printf("%d: '%s'\n", i
, str
);
330 static void do_quit(void)
335 static int eject_device(BlockDriverState
*bs
, int force
)
337 if (bdrv_is_inserted(bs
)) {
339 if (!bdrv_is_removable(bs
)) {
340 term_printf("device is not removable\n");
343 if (bdrv_is_locked(bs
)) {
344 term_printf("device is locked\n");
353 static void do_eject(int force
, const char *filename
)
355 BlockDriverState
*bs
;
357 bs
= bdrv_find(filename
);
359 term_printf("device not found\n");
362 eject_device(bs
, force
);
365 static void do_change(const char *device
, const char *filename
)
367 BlockDriverState
*bs
;
371 bs
= bdrv_find(device
);
373 term_printf("device not found\n");
376 if (eject_device(bs
, 0) < 0)
378 bdrv_open(bs
, filename
, 0);
379 if (bdrv_is_encrypted(bs
)) {
380 term_printf("%s is encrypted.\n", device
);
381 for(i
= 0; i
< 3; i
++) {
382 monitor_readline("Password: ", 1, password
, sizeof(password
));
383 if (bdrv_set_key(bs
, password
) == 0)
385 term_printf("invalid password\n");
390 static void do_screen_dump(const char *filename
)
392 vga_hw_screen_dump(filename
);
395 static void do_log(const char *items
)
399 if (!strcmp(items
, "none")) {
402 mask
= cpu_str_to_log_mask(items
);
411 static void do_stop(void)
413 vm_stop(EXCP_INTERRUPT
);
416 static void do_cont(void)
421 #ifdef CONFIG_GDBSTUB
422 static void do_gdbserver(int has_port
, int port
)
425 port
= DEFAULT_GDBSTUB_PORT
;
426 if (gdbserver_start_port(port
) < 0) {
427 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
429 qemu_printf("Waiting gdb connection on port %d\n", port
);
434 static void term_printc(int c
)
451 if (c
>= 32 && c
<= 126) {
452 term_printf("%c", c
);
454 term_printf("\\x%02x", c
);
461 static void memory_dump(int count
, int format
, int wsize
,
462 target_ulong addr
, int is_physical
)
465 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
473 if (!env
&& !is_physical
)
478 } else if (wsize
== 4) {
481 /* as default we use the current CS size */
485 if ((env
->efer
& MSR_EFER_LMA
) &&
486 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
490 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
495 monitor_disas(env
, addr
, count
, is_physical
, flags
);
504 nb_per_line
= line_size
/ wsize
;
509 max_digits
= (wsize
* 8 + 2) / 3;
513 max_digits
= (wsize
* 8) / 4;
517 max_digits
= (wsize
* 8 * 10 + 32) / 33;
525 term_printf(TARGET_FMT_lx
":", addr
);
530 cpu_physical_memory_rw(addr
, buf
, l
, 0);
535 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
542 v
= ldub_raw(buf
+ i
);
545 v
= lduw_raw(buf
+ i
);
548 v
= (uint32_t)ldl_raw(buf
+ i
);
551 v
= ldq_raw(buf
+ i
);
557 term_printf("%#*" PRIo64
, max_digits
, v
);
560 term_printf("0x%0*" PRIx64
, max_digits
, v
);
563 term_printf("%*" PRIu64
, max_digits
, v
);
566 term_printf("%*" PRId64
, max_digits
, v
);
580 #if TARGET_LONG_BITS == 64
581 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
583 #define GET_TLONG(h, l) (l)
586 static void do_memory_dump(int count
, int format
, int size
,
587 uint32_t addrh
, uint32_t addrl
)
589 target_long addr
= GET_TLONG(addrh
, addrl
);
590 memory_dump(count
, format
, size
, addr
, 0);
593 static void do_physical_memory_dump(int count
, int format
, int size
,
594 uint32_t addrh
, uint32_t addrl
)
597 target_long addr
= GET_TLONG(addrh
, addrl
);
598 memory_dump(count
, format
, size
, addr
, 1);
601 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
603 target_long val
= GET_TLONG(valh
, vall
);
604 #if TARGET_LONG_BITS == 32
607 term_printf("%#o", val
);
610 term_printf("%#x", val
);
613 term_printf("%u", val
);
617 term_printf("%d", val
);
626 term_printf("%#" PRIo64
, val
);
629 term_printf("%#" PRIx64
, val
);
632 term_printf("%" PRIu64
, val
);
636 term_printf("%" PRId64
, val
);
646 static void do_memory_save(unsigned int valh
, unsigned int vall
,
647 uint32_t size
, const char *filename
)
650 target_long addr
= GET_TLONG(valh
, vall
);
659 f
= fopen(filename
, "wb");
661 term_printf("could not open '%s'\n", filename
);
668 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
669 fwrite(buf
, 1, l
, f
);
676 static void do_sum(uint32_t start
, uint32_t size
)
683 for(addr
= start
; addr
< (start
+ size
); addr
++) {
684 cpu_physical_memory_rw(addr
, buf
, 1, 0);
685 /* BSD sum algorithm ('sum' Unix command) */
686 sum
= (sum
>> 1) | (sum
<< 15);
689 term_printf("%05d\n", sum
);
697 static const KeyDef key_defs
[] = {
722 { 0x0e, "backspace" },
757 { 0x3a, "caps_lock" },
768 { 0x45, "num_lock" },
769 { 0x46, "scroll_lock" },
771 { 0xb5, "kp_divide" },
772 { 0x37, "kp_multiply" },
773 { 0x4a, "kp_substract" },
775 { 0x9c, "kp_enter" },
776 { 0x53, "kp_decimal" },
811 static int get_keycode(const char *key
)
817 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
818 if (!strcmp(key
, p
->name
))
821 if (strstart(key
, "0x", NULL
)) {
822 ret
= strtoul(key
, &endp
, 0);
823 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
829 static void do_send_key(const char *string
)
832 uint8_t keycodes
[16];
834 int nb_keycodes
, keycode
, i
;
840 while (*p
!= '\0' && *p
!= '-') {
841 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
847 keycode
= get_keycode(keybuf
);
849 term_printf("unknown key: '%s'\n", keybuf
);
852 keycodes
[nb_keycodes
++] = keycode
;
857 /* key down events */
858 for(i
= 0; i
< nb_keycodes
; i
++) {
859 keycode
= keycodes
[i
];
861 kbd_put_keycode(0xe0);
862 kbd_put_keycode(keycode
& 0x7f);
865 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
866 keycode
= keycodes
[i
];
868 kbd_put_keycode(0xe0);
869 kbd_put_keycode(keycode
| 0x80);
873 static int mouse_button_state
;
875 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
879 dx
= strtol(dx_str
, NULL
, 0);
880 dy
= strtol(dy_str
, NULL
, 0);
883 dz
= strtol(dz_str
, NULL
, 0);
884 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
887 static void do_mouse_button(int button_state
)
889 mouse_button_state
= button_state
;
890 kbd_mouse_event(0, 0, 0, mouse_button_state
);
893 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
899 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
907 val
= cpu_inb(NULL
, addr
);
911 val
= cpu_inw(NULL
, addr
);
915 val
= cpu_inl(NULL
, addr
);
919 term_printf("port%c[0x%04x] = %#0*x\n",
920 suffix
, addr
, size
* 2, val
);
923 static void do_system_reset(void)
925 qemu_system_reset_request();
928 static void do_system_powerdown(void)
930 qemu_system_powerdown_request();
933 #if defined(TARGET_I386)
934 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
936 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
939 pte
& PG_GLOBAL_MASK
? 'G' : '-',
940 pte
& PG_PSE_MASK
? 'P' : '-',
941 pte
& PG_DIRTY_MASK
? 'D' : '-',
942 pte
& PG_ACCESSED_MASK
? 'A' : '-',
943 pte
& PG_PCD_MASK
? 'C' : '-',
944 pte
& PG_PWT_MASK
? 'T' : '-',
945 pte
& PG_USER_MASK
? 'U' : '-',
946 pte
& PG_RW_MASK
? 'W' : '-');
949 static void tlb_info(void)
953 uint32_t pgd
, pde
, pte
;
959 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
960 term_printf("PG disabled\n");
963 pgd
= env
->cr
[3] & ~0xfff;
964 for(l1
= 0; l1
< 1024; l1
++) {
965 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
966 pde
= le32_to_cpu(pde
);
967 if (pde
& PG_PRESENT_MASK
) {
968 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
969 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
971 for(l2
= 0; l2
< 1024; l2
++) {
972 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
974 pte
= le32_to_cpu(pte
);
975 if (pte
& PG_PRESENT_MASK
) {
976 print_pte((l1
<< 22) + (l2
<< 12),
986 static void mem_print(uint32_t *pstart
, int *plast_prot
,
987 uint32_t end
, int prot
)
993 term_printf("%08x-%08x %08x %c%c%c\n",
994 *pstart
, end
, end
- *pstart
,
995 prot1
& PG_USER_MASK
? 'u' : '-',
997 prot1
& PG_RW_MASK
? 'w' : '-');
1007 static void mem_info(void)
1010 int l1
, l2
, prot
, last_prot
;
1011 uint32_t pgd
, pde
, pte
, start
, end
;
1013 env
= mon_get_cpu();
1017 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1018 term_printf("PG disabled\n");
1021 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
);
1028 if (pde
& PG_PRESENT_MASK
) {
1029 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1030 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1031 mem_print(&start
, &last_prot
, end
, prot
);
1033 for(l2
= 0; l2
< 1024; l2
++) {
1034 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1035 (uint8_t *)&pte
, 4);
1036 pte
= le32_to_cpu(pte
);
1037 end
= (l1
<< 22) + (l2
<< 12);
1038 if (pte
& PG_PRESENT_MASK
) {
1039 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1043 mem_print(&start
, &last_prot
, end
, prot
);
1048 mem_print(&start
, &last_prot
, end
, prot
);
1054 static void do_info_kqemu(void)
1060 env
= mon_get_cpu();
1062 term_printf("No cpu initialized yet");
1065 val
= env
->kqemu_enabled
;
1066 term_printf("kqemu support: ");
1070 term_printf("disabled\n");
1073 term_printf("enabled for user code\n");
1076 term_printf("enabled for user and kernel code\n");
1080 term_printf("kqemu support: not compiled\n");
1084 #ifdef CONFIG_PROFILER
1088 int64_t kqemu_exec_count
;
1090 int64_t kqemu_ret_int_count
;
1091 int64_t kqemu_ret_excp_count
;
1092 int64_t kqemu_ret_intr_count
;
1094 static void do_info_profile(void)
1100 term_printf("async time %" PRId64
" (%0.3f)\n",
1101 dev_time
, dev_time
/ (double)ticks_per_sec
);
1102 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1103 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1104 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1105 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1106 kqemu_time
/ (double)total
* 100.0,
1108 kqemu_ret_int_count
,
1109 kqemu_ret_excp_count
,
1110 kqemu_ret_intr_count
);
1113 kqemu_exec_count
= 0;
1115 kqemu_ret_int_count
= 0;
1116 kqemu_ret_excp_count
= 0;
1117 kqemu_ret_intr_count
= 0;
1119 kqemu_record_dump();
1123 static void do_info_profile(void)
1125 term_printf("Internal profiler not compiled\n");
1129 /* Capture support */
1130 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1132 static void do_info_capture (void)
1137 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1138 term_printf ("[%d]: ", i
);
1139 s
->ops
.info (s
->opaque
);
1143 static void do_stop_capture (int n
)
1148 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1150 s
->ops
.destroy (s
->opaque
);
1151 LIST_REMOVE (s
, entries
);
1159 int wav_start_capture (CaptureState
*s
, const char *path
, int freq
,
1160 int bits
, int nchannels
);
1162 static void do_wav_capture (const char *path
,
1163 int has_freq
, int freq
,
1164 int has_bits
, int bits
,
1165 int has_channels
, int nchannels
)
1169 s
= qemu_mallocz (sizeof (*s
));
1171 term_printf ("Not enough memory to add wave capture\n");
1175 freq
= has_freq
? freq
: 44100;
1176 bits
= has_bits
? bits
: 16;
1177 nchannels
= has_channels
? nchannels
: 2;
1179 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1180 term_printf ("Faied to add wave capture\n");
1183 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1187 static term_cmd_t term_cmds
[] = {
1188 { "help|?", "s?", do_help
,
1189 "[cmd]", "show the help" },
1190 { "commit", "s", do_commit
,
1191 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1192 { "info", "s?", do_info
,
1193 "subcommand", "show various information about the system state" },
1194 { "q|quit", "", do_quit
,
1195 "", "quit the emulator" },
1196 { "eject", "-fB", do_eject
,
1197 "[-f] device", "eject a removable media (use -f to force it)" },
1198 { "change", "BF", do_change
,
1199 "device filename", "change a removable media" },
1200 { "screendump", "F", do_screen_dump
,
1201 "filename", "save screen into PPM image 'filename'" },
1202 { "log", "s", do_log
,
1203 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1204 { "savevm", "s?", do_savevm
,
1205 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1206 { "loadvm", "s", do_loadvm
,
1207 "tag|id", "restore a VM snapshot from its tag or id" },
1208 { "delvm", "s", do_delvm
,
1209 "tag|id", "delete a VM snapshot from its tag or id" },
1210 { "stop", "", do_stop
,
1211 "", "stop emulation", },
1212 { "c|cont", "", do_cont
,
1213 "", "resume emulation", },
1214 #ifdef CONFIG_GDBSTUB
1215 { "gdbserver", "i?", do_gdbserver
,
1216 "[port]", "start gdbserver session (default port=1234)", },
1218 { "x", "/l", do_memory_dump
,
1219 "/fmt addr", "virtual memory dump starting at 'addr'", },
1220 { "xp", "/l", do_physical_memory_dump
,
1221 "/fmt addr", "physical memory dump starting at 'addr'", },
1222 { "p|print", "/l", do_print
,
1223 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1224 { "i", "/ii.", do_ioport_read
,
1225 "/fmt addr", "I/O port read" },
1227 { "sendkey", "s", do_send_key
,
1228 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1229 { "system_reset", "", do_system_reset
,
1230 "", "reset the system" },
1231 { "system_powerdown", "", do_system_powerdown
,
1232 "", "send system power down event" },
1233 { "sum", "ii", do_sum
,
1234 "addr size", "compute the checksum of a memory region" },
1235 { "usb_add", "s", do_usb_add
,
1236 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1237 { "usb_del", "s", do_usb_del
,
1238 "device", "remove USB device 'bus.addr'" },
1239 { "cpu", "i", do_cpu_set
,
1240 "index", "set the default CPU" },
1241 { "mouse_move", "sss?", do_mouse_move
,
1242 "dx dy [dz]", "send mouse move events" },
1243 { "mouse_button", "i", do_mouse_button
,
1244 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1245 { "mouse_set", "i", do_mouse_set
,
1246 "index", "set which mouse device receives events" },
1248 { "wavcapture", "si?i?i?", do_wav_capture
,
1249 "path [frequency bits channels]",
1250 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1252 { "stopcapture", "i", do_stop_capture
,
1253 "capture index", "stop capture" },
1254 { "memsave", "lis", do_memory_save
,
1255 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1259 static term_cmd_t info_cmds
[] = {
1260 { "version", "", do_info_version
,
1261 "", "show the version of qemu" },
1262 { "network", "", do_info_network
,
1263 "", "show the network state" },
1264 { "block", "", do_info_block
,
1265 "", "show the block devices" },
1266 { "registers", "", do_info_registers
,
1267 "", "show the cpu registers" },
1268 { "cpus", "", do_info_cpus
,
1269 "", "show infos for each CPU" },
1270 { "history", "", do_info_history
,
1271 "", "show the command line history", },
1272 { "irq", "", irq_info
,
1273 "", "show the interrupts statistics (if available)", },
1274 { "pic", "", pic_info
,
1275 "", "show i8259 (PIC) state", },
1276 { "pci", "", pci_info
,
1277 "", "show PCI info", },
1278 #if defined(TARGET_I386)
1279 { "tlb", "", tlb_info
,
1280 "", "show virtual to physical memory mappings", },
1281 { "mem", "", mem_info
,
1282 "", "show the active virtual memory mappings", },
1284 { "jit", "", do_info_jit
,
1285 "", "show dynamic compiler info", },
1286 { "kqemu", "", do_info_kqemu
,
1287 "", "show kqemu information", },
1288 { "usb", "", usb_info
,
1289 "", "show guest USB devices", },
1290 { "usbhost", "", usb_host_info
,
1291 "", "show host USB devices", },
1292 { "profile", "", do_info_profile
,
1293 "", "show profiling information", },
1294 { "capture", "", do_info_capture
,
1295 "", "show capture information" },
1296 { "snapshots", "", do_info_snapshots
,
1297 "", "show the currently saved VM snapshots" },
1298 { "mice", "", do_info_mice
,
1299 "", "show which guest mouse is receiving events" },
1300 { "vnc", "", do_info_vnc
,
1301 "", "show the vnc server status"},
1305 /*******************************************************************/
1307 static const char *pch
;
1308 static jmp_buf expr_env
;
1313 typedef struct MonitorDef
{
1316 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
1320 #if defined(TARGET_I386)
1321 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
1323 CPUState
*env
= mon_get_cpu();
1326 return env
->eip
+ env
->segs
[R_CS
].base
;
1330 #if defined(TARGET_PPC)
1331 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
1333 CPUState
*env
= mon_get_cpu();
1341 for (i
= 0; i
< 8; i
++)
1342 u
|= env
->crf
[i
] << (32 - (4 * i
));
1347 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
1349 CPUState
*env
= mon_get_cpu();
1352 return (env
->msr
[MSR_POW
] << MSR_POW
) |
1353 (env
->msr
[MSR_ILE
] << MSR_ILE
) |
1354 (env
->msr
[MSR_EE
] << MSR_EE
) |
1355 (env
->msr
[MSR_PR
] << MSR_PR
) |
1356 (env
->msr
[MSR_FP
] << MSR_FP
) |
1357 (env
->msr
[MSR_ME
] << MSR_ME
) |
1358 (env
->msr
[MSR_FE0
] << MSR_FE0
) |
1359 (env
->msr
[MSR_SE
] << MSR_SE
) |
1360 (env
->msr
[MSR_BE
] << MSR_BE
) |
1361 (env
->msr
[MSR_FE1
] << MSR_FE1
) |
1362 (env
->msr
[MSR_IP
] << MSR_IP
) |
1363 (env
->msr
[MSR_IR
] << MSR_IR
) |
1364 (env
->msr
[MSR_DR
] << MSR_DR
) |
1365 (env
->msr
[MSR_RI
] << MSR_RI
) |
1366 (env
->msr
[MSR_LE
] << MSR_LE
);
1369 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
1371 CPUState
*env
= mon_get_cpu();
1374 return (env
->xer
[XER_SO
] << XER_SO
) |
1375 (env
->xer
[XER_OV
] << XER_OV
) |
1376 (env
->xer
[XER_CA
] << XER_CA
) |
1377 (env
->xer
[XER_BC
] << XER_BC
);
1380 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1382 CPUState
*env
= mon_get_cpu();
1385 return cpu_ppc_load_decr(env
);
1388 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1390 CPUState
*env
= mon_get_cpu();
1393 return cpu_ppc_load_tbu(env
);
1396 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1398 CPUState
*env
= mon_get_cpu();
1401 return cpu_ppc_load_tbl(env
);
1405 #if defined(TARGET_SPARC)
1406 #ifndef TARGET_SPARC64
1407 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1409 CPUState
*env
= mon_get_cpu();
1412 return GET_PSR(env
);
1416 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1418 CPUState
*env
= mon_get_cpu();
1421 return env
->regwptr
[val
];
1425 static MonitorDef monitor_defs
[] = {
1428 #define SEG(name, seg) \
1429 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1430 { name ".base", offsetof(CPUState, segs[seg].base) },\
1431 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1433 { "eax", offsetof(CPUState
, regs
[0]) },
1434 { "ecx", offsetof(CPUState
, regs
[1]) },
1435 { "edx", offsetof(CPUState
, regs
[2]) },
1436 { "ebx", offsetof(CPUState
, regs
[3]) },
1437 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1438 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1439 { "esi", offsetof(CPUState
, regs
[6]) },
1440 { "edi", offsetof(CPUState
, regs
[7]) },
1441 #ifdef TARGET_X86_64
1442 { "r8", offsetof(CPUState
, regs
[8]) },
1443 { "r9", offsetof(CPUState
, regs
[9]) },
1444 { "r10", offsetof(CPUState
, regs
[10]) },
1445 { "r11", offsetof(CPUState
, regs
[11]) },
1446 { "r12", offsetof(CPUState
, regs
[12]) },
1447 { "r13", offsetof(CPUState
, regs
[13]) },
1448 { "r14", offsetof(CPUState
, regs
[14]) },
1449 { "r15", offsetof(CPUState
, regs
[15]) },
1451 { "eflags", offsetof(CPUState
, eflags
) },
1452 { "eip", offsetof(CPUState
, eip
) },
1459 { "pc", 0, monitor_get_pc
, },
1460 #elif defined(TARGET_PPC)
1461 { "r0", offsetof(CPUState
, gpr
[0]) },
1462 { "r1", offsetof(CPUState
, gpr
[1]) },
1463 { "r2", offsetof(CPUState
, gpr
[2]) },
1464 { "r3", offsetof(CPUState
, gpr
[3]) },
1465 { "r4", offsetof(CPUState
, gpr
[4]) },
1466 { "r5", offsetof(CPUState
, gpr
[5]) },
1467 { "r6", offsetof(CPUState
, gpr
[6]) },
1468 { "r7", offsetof(CPUState
, gpr
[7]) },
1469 { "r8", offsetof(CPUState
, gpr
[8]) },
1470 { "r9", offsetof(CPUState
, gpr
[9]) },
1471 { "r10", offsetof(CPUState
, gpr
[10]) },
1472 { "r11", offsetof(CPUState
, gpr
[11]) },
1473 { "r12", offsetof(CPUState
, gpr
[12]) },
1474 { "r13", offsetof(CPUState
, gpr
[13]) },
1475 { "r14", offsetof(CPUState
, gpr
[14]) },
1476 { "r15", offsetof(CPUState
, gpr
[15]) },
1477 { "r16", offsetof(CPUState
, gpr
[16]) },
1478 { "r17", offsetof(CPUState
, gpr
[17]) },
1479 { "r18", offsetof(CPUState
, gpr
[18]) },
1480 { "r19", offsetof(CPUState
, gpr
[19]) },
1481 { "r20", offsetof(CPUState
, gpr
[20]) },
1482 { "r21", offsetof(CPUState
, gpr
[21]) },
1483 { "r22", offsetof(CPUState
, gpr
[22]) },
1484 { "r23", offsetof(CPUState
, gpr
[23]) },
1485 { "r24", offsetof(CPUState
, gpr
[24]) },
1486 { "r25", offsetof(CPUState
, gpr
[25]) },
1487 { "r26", offsetof(CPUState
, gpr
[26]) },
1488 { "r27", offsetof(CPUState
, gpr
[27]) },
1489 { "r28", offsetof(CPUState
, gpr
[28]) },
1490 { "r29", offsetof(CPUState
, gpr
[29]) },
1491 { "r30", offsetof(CPUState
, gpr
[30]) },
1492 { "r31", offsetof(CPUState
, gpr
[31]) },
1493 { "nip|pc", offsetof(CPUState
, nip
) },
1494 { "lr", offsetof(CPUState
, lr
) },
1495 { "ctr", offsetof(CPUState
, ctr
) },
1496 { "decr", 0, &monitor_get_decr
, },
1497 { "ccr", 0, &monitor_get_ccr
, },
1498 { "msr", 0, &monitor_get_msr
, },
1499 { "xer", 0, &monitor_get_xer
, },
1500 { "tbu", 0, &monitor_get_tbu
, },
1501 { "tbl", 0, &monitor_get_tbl
, },
1502 { "sdr1", offsetof(CPUState
, sdr1
) },
1503 { "sr0", offsetof(CPUState
, sr
[0]) },
1504 { "sr1", offsetof(CPUState
, sr
[1]) },
1505 { "sr2", offsetof(CPUState
, sr
[2]) },
1506 { "sr3", offsetof(CPUState
, sr
[3]) },
1507 { "sr4", offsetof(CPUState
, sr
[4]) },
1508 { "sr5", offsetof(CPUState
, sr
[5]) },
1509 { "sr6", offsetof(CPUState
, sr
[6]) },
1510 { "sr7", offsetof(CPUState
, sr
[7]) },
1511 { "sr8", offsetof(CPUState
, sr
[8]) },
1512 { "sr9", offsetof(CPUState
, sr
[9]) },
1513 { "sr10", offsetof(CPUState
, sr
[10]) },
1514 { "sr11", offsetof(CPUState
, sr
[11]) },
1515 { "sr12", offsetof(CPUState
, sr
[12]) },
1516 { "sr13", offsetof(CPUState
, sr
[13]) },
1517 { "sr14", offsetof(CPUState
, sr
[14]) },
1518 { "sr15", offsetof(CPUState
, sr
[15]) },
1519 /* Too lazy to put BATs and SPRs ... */
1520 #elif defined(TARGET_SPARC)
1521 { "g0", offsetof(CPUState
, gregs
[0]) },
1522 { "g1", offsetof(CPUState
, gregs
[1]) },
1523 { "g2", offsetof(CPUState
, gregs
[2]) },
1524 { "g3", offsetof(CPUState
, gregs
[3]) },
1525 { "g4", offsetof(CPUState
, gregs
[4]) },
1526 { "g5", offsetof(CPUState
, gregs
[5]) },
1527 { "g6", offsetof(CPUState
, gregs
[6]) },
1528 { "g7", offsetof(CPUState
, gregs
[7]) },
1529 { "o0", 0, monitor_get_reg
},
1530 { "o1", 1, monitor_get_reg
},
1531 { "o2", 2, monitor_get_reg
},
1532 { "o3", 3, monitor_get_reg
},
1533 { "o4", 4, monitor_get_reg
},
1534 { "o5", 5, monitor_get_reg
},
1535 { "o6", 6, monitor_get_reg
},
1536 { "o7", 7, monitor_get_reg
},
1537 { "l0", 8, monitor_get_reg
},
1538 { "l1", 9, monitor_get_reg
},
1539 { "l2", 10, monitor_get_reg
},
1540 { "l3", 11, monitor_get_reg
},
1541 { "l4", 12, monitor_get_reg
},
1542 { "l5", 13, monitor_get_reg
},
1543 { "l6", 14, monitor_get_reg
},
1544 { "l7", 15, monitor_get_reg
},
1545 { "i0", 16, monitor_get_reg
},
1546 { "i1", 17, monitor_get_reg
},
1547 { "i2", 18, monitor_get_reg
},
1548 { "i3", 19, monitor_get_reg
},
1549 { "i4", 20, monitor_get_reg
},
1550 { "i5", 21, monitor_get_reg
},
1551 { "i6", 22, monitor_get_reg
},
1552 { "i7", 23, monitor_get_reg
},
1553 { "pc", offsetof(CPUState
, pc
) },
1554 { "npc", offsetof(CPUState
, npc
) },
1555 { "y", offsetof(CPUState
, y
) },
1556 #ifndef TARGET_SPARC64
1557 { "psr", 0, &monitor_get_psr
, },
1558 { "wim", offsetof(CPUState
, wim
) },
1560 { "tbr", offsetof(CPUState
, tbr
) },
1561 { "fsr", offsetof(CPUState
, fsr
) },
1562 { "f0", offsetof(CPUState
, fpr
[0]) },
1563 { "f1", offsetof(CPUState
, fpr
[1]) },
1564 { "f2", offsetof(CPUState
, fpr
[2]) },
1565 { "f3", offsetof(CPUState
, fpr
[3]) },
1566 { "f4", offsetof(CPUState
, fpr
[4]) },
1567 { "f5", offsetof(CPUState
, fpr
[5]) },
1568 { "f6", offsetof(CPUState
, fpr
[6]) },
1569 { "f7", offsetof(CPUState
, fpr
[7]) },
1570 { "f8", offsetof(CPUState
, fpr
[8]) },
1571 { "f9", offsetof(CPUState
, fpr
[9]) },
1572 { "f10", offsetof(CPUState
, fpr
[10]) },
1573 { "f11", offsetof(CPUState
, fpr
[11]) },
1574 { "f12", offsetof(CPUState
, fpr
[12]) },
1575 { "f13", offsetof(CPUState
, fpr
[13]) },
1576 { "f14", offsetof(CPUState
, fpr
[14]) },
1577 { "f15", offsetof(CPUState
, fpr
[15]) },
1578 { "f16", offsetof(CPUState
, fpr
[16]) },
1579 { "f17", offsetof(CPUState
, fpr
[17]) },
1580 { "f18", offsetof(CPUState
, fpr
[18]) },
1581 { "f19", offsetof(CPUState
, fpr
[19]) },
1582 { "f20", offsetof(CPUState
, fpr
[20]) },
1583 { "f21", offsetof(CPUState
, fpr
[21]) },
1584 { "f22", offsetof(CPUState
, fpr
[22]) },
1585 { "f23", offsetof(CPUState
, fpr
[23]) },
1586 { "f24", offsetof(CPUState
, fpr
[24]) },
1587 { "f25", offsetof(CPUState
, fpr
[25]) },
1588 { "f26", offsetof(CPUState
, fpr
[26]) },
1589 { "f27", offsetof(CPUState
, fpr
[27]) },
1590 { "f28", offsetof(CPUState
, fpr
[28]) },
1591 { "f29", offsetof(CPUState
, fpr
[29]) },
1592 { "f30", offsetof(CPUState
, fpr
[30]) },
1593 { "f31", offsetof(CPUState
, fpr
[31]) },
1594 #ifdef TARGET_SPARC64
1595 { "f32", offsetof(CPUState
, fpr
[32]) },
1596 { "f34", offsetof(CPUState
, fpr
[34]) },
1597 { "f36", offsetof(CPUState
, fpr
[36]) },
1598 { "f38", offsetof(CPUState
, fpr
[38]) },
1599 { "f40", offsetof(CPUState
, fpr
[40]) },
1600 { "f42", offsetof(CPUState
, fpr
[42]) },
1601 { "f44", offsetof(CPUState
, fpr
[44]) },
1602 { "f46", offsetof(CPUState
, fpr
[46]) },
1603 { "f48", offsetof(CPUState
, fpr
[48]) },
1604 { "f50", offsetof(CPUState
, fpr
[50]) },
1605 { "f52", offsetof(CPUState
, fpr
[52]) },
1606 { "f54", offsetof(CPUState
, fpr
[54]) },
1607 { "f56", offsetof(CPUState
, fpr
[56]) },
1608 { "f58", offsetof(CPUState
, fpr
[58]) },
1609 { "f60", offsetof(CPUState
, fpr
[60]) },
1610 { "f62", offsetof(CPUState
, fpr
[62]) },
1611 { "asi", offsetof(CPUState
, asi
) },
1612 { "pstate", offsetof(CPUState
, pstate
) },
1613 { "cansave", offsetof(CPUState
, cansave
) },
1614 { "canrestore", offsetof(CPUState
, canrestore
) },
1615 { "otherwin", offsetof(CPUState
, otherwin
) },
1616 { "wstate", offsetof(CPUState
, wstate
) },
1617 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1618 { "fprs", offsetof(CPUState
, fprs
) },
1624 static void expr_error(const char *fmt
)
1628 longjmp(expr_env
, 1);
1631 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1632 static int get_monitor_def(target_long
*pval
, const char *name
)
1637 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1638 if (compare_cmd(name
, md
->name
)) {
1639 if (md
->get_value
) {
1640 *pval
= md
->get_value(md
, md
->offset
);
1642 CPUState
*env
= mon_get_cpu();
1645 ptr
= (uint8_t *)env
+ md
->offset
;
1648 *pval
= *(int32_t *)ptr
;
1651 *pval
= *(target_long
*)ptr
;
1664 static void next(void)
1668 while (isspace(*pch
))
1673 static target_long
expr_sum(void);
1675 static target_long
expr_unary(void)
1698 expr_error("')' expected");
1705 expr_error("character constant expected");
1709 expr_error("missing terminating \' character");
1718 while ((*pch
>= 'a' && *pch
<= 'z') ||
1719 (*pch
>= 'A' && *pch
<= 'Z') ||
1720 (*pch
>= '0' && *pch
<= '9') ||
1721 *pch
== '_' || *pch
== '.') {
1722 if ((q
- buf
) < sizeof(buf
) - 1)
1726 while (isspace(*pch
))
1729 ret
= get_monitor_def(&n
, buf
);
1731 expr_error("unknown register");
1733 expr_error("no cpu defined");
1737 expr_error("unexpected end of expression");
1741 #if TARGET_LONG_BITS == 64
1742 n
= strtoull(pch
, &p
, 0);
1744 n
= strtoul(pch
, &p
, 0);
1747 expr_error("invalid char in expression");
1750 while (isspace(*pch
))
1758 static target_long
expr_prod(void)
1760 target_long val
, val2
;
1766 if (op
!= '*' && op
!= '/' && op
!= '%')
1769 val2
= expr_unary();
1778 expr_error("division by zero");
1789 static target_long
expr_logic(void)
1791 target_long val
, val2
;
1797 if (op
!= '&' && op
!= '|' && op
!= '^')
1817 static target_long
expr_sum(void)
1819 target_long val
, val2
;
1825 if (op
!= '+' && op
!= '-')
1828 val2
= expr_logic();
1837 static int get_expr(target_long
*pval
, const char **pp
)
1840 if (setjmp(expr_env
)) {
1844 while (isspace(*pch
))
1851 static int get_str(char *buf
, int buf_size
, const char **pp
)
1869 while (*p
!= '\0' && *p
!= '\"') {
1885 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1888 if ((q
- buf
) < buf_size
- 1) {
1892 if ((q
- buf
) < buf_size
- 1) {
1899 qemu_printf("unterminated string\n");
1904 while (*p
!= '\0' && !isspace(*p
)) {
1905 if ((q
- buf
) < buf_size
- 1) {
1916 static int default_fmt_format
= 'x';
1917 static int default_fmt_size
= 4;
1921 static void monitor_handle_command(const char *cmdline
)
1923 const char *p
, *pstart
, *typestr
;
1925 int c
, nb_args
, len
, i
, has_arg
;
1929 void *str_allocated
[MAX_ARGS
];
1930 void *args
[MAX_ARGS
];
1933 term_printf("command='%s'\n", cmdline
);
1936 /* extract the command name */
1944 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
1947 if (len
> sizeof(cmdname
) - 1)
1948 len
= sizeof(cmdname
) - 1;
1949 memcpy(cmdname
, pstart
, len
);
1950 cmdname
[len
] = '\0';
1952 /* find the command */
1953 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
1954 if (compare_cmd(cmdname
, cmd
->name
))
1957 term_printf("unknown command: '%s'\n", cmdname
);
1961 for(i
= 0; i
< MAX_ARGS
; i
++)
1962 str_allocated
[i
] = NULL
;
1964 /* parse the parameters */
1965 typestr
= cmd
->args_type
;
1982 if (*typestr
== '?') {
1985 /* no optional string: NULL argument */
1990 ret
= get_str(buf
, sizeof(buf
), &p
);
1994 term_printf("%s: filename expected\n", cmdname
);
1997 term_printf("%s: block device name expected\n", cmdname
);
2000 term_printf("%s: string expected\n", cmdname
);
2005 str
= qemu_malloc(strlen(buf
) + 1);
2007 str_allocated
[nb_args
] = str
;
2009 if (nb_args
>= MAX_ARGS
) {
2011 term_printf("%s: too many arguments\n", cmdname
);
2014 args
[nb_args
++] = str
;
2019 int count
, format
, size
;
2029 while (isdigit(*p
)) {
2030 count
= count
* 10 + (*p
- '0');
2068 if (*p
!= '\0' && !isspace(*p
)) {
2069 term_printf("invalid char in format: '%c'\n", *p
);
2073 format
= default_fmt_format
;
2074 if (format
!= 'i') {
2075 /* for 'i', not specifying a size gives -1 as size */
2077 size
= default_fmt_size
;
2079 default_fmt_size
= size
;
2080 default_fmt_format
= format
;
2083 format
= default_fmt_format
;
2084 if (format
!= 'i') {
2085 size
= default_fmt_size
;
2090 if (nb_args
+ 3 > MAX_ARGS
)
2092 args
[nb_args
++] = (void*)count
;
2093 args
[nb_args
++] = (void*)format
;
2094 args
[nb_args
++] = (void*)size
;
2103 if (*typestr
== '?' || *typestr
== '.') {
2104 if (*typestr
== '?') {
2120 if (nb_args
>= MAX_ARGS
)
2122 args
[nb_args
++] = (void *)has_arg
;
2124 if (nb_args
>= MAX_ARGS
)
2130 if (get_expr(&val
, &p
))
2134 if (nb_args
>= MAX_ARGS
)
2136 args
[nb_args
++] = (void *)(int)val
;
2138 if ((nb_args
+ 1) >= MAX_ARGS
)
2140 #if TARGET_LONG_BITS == 64
2141 args
[nb_args
++] = (void *)(int)((val
>> 32) & 0xffffffff);
2143 args
[nb_args
++] = (void *)0;
2145 args
[nb_args
++] = (void *)(int)(val
& 0xffffffff);
2163 term_printf("%s: unsupported option -%c\n",
2170 if (nb_args
>= MAX_ARGS
)
2172 args
[nb_args
++] = (void *)has_option
;
2177 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2181 /* check that all arguments were parsed */
2185 term_printf("%s: extraneous characters at the end of line\n",
2195 cmd
->handler(args
[0]);
2198 cmd
->handler(args
[0], args
[1]);
2201 cmd
->handler(args
[0], args
[1], args
[2]);
2204 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
2207 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
2210 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2213 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2216 term_printf("unsupported number of arguments: %d\n", nb_args
);
2220 for(i
= 0; i
< MAX_ARGS
; i
++)
2221 qemu_free(str_allocated
[i
]);
2225 static void cmd_completion(const char *name
, const char *list
)
2227 const char *p
, *pstart
;
2236 p
= pstart
+ strlen(pstart
);
2238 if (len
> sizeof(cmd
) - 2)
2239 len
= sizeof(cmd
) - 2;
2240 memcpy(cmd
, pstart
, len
);
2242 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2243 add_completion(cmd
);
2251 static void file_completion(const char *input
)
2256 char file
[1024], file_prefix
[1024];
2260 p
= strrchr(input
, '/');
2263 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2266 input_path_len
= p
- input
+ 1;
2267 memcpy(path
, input
, input_path_len
);
2268 if (input_path_len
> sizeof(path
) - 1)
2269 input_path_len
= sizeof(path
) - 1;
2270 path
[input_path_len
] = '\0';
2271 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2273 #ifdef DEBUG_COMPLETION
2274 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2276 ffs
= opendir(path
);
2284 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2285 memcpy(file
, input
, input_path_len
);
2286 strcpy(file
+ input_path_len
, d
->d_name
);
2287 /* stat the file to find out if it's a directory.
2288 * In that case add a slash to speed up typing long paths
2291 if(S_ISDIR(sb
.st_mode
))
2293 add_completion(file
);
2299 static void block_completion_it(void *opaque
, const char *name
)
2301 const char *input
= opaque
;
2303 if (input
[0] == '\0' ||
2304 !strncmp(name
, (char *)input
, strlen(input
))) {
2305 add_completion(name
);
2309 /* NOTE: this parser is an approximate form of the real command parser */
2310 static void parse_cmdline(const char *cmdline
,
2311 int *pnb_args
, char **args
)
2324 if (nb_args
>= MAX_ARGS
)
2326 ret
= get_str(buf
, sizeof(buf
), &p
);
2327 args
[nb_args
] = qemu_strdup(buf
);
2332 *pnb_args
= nb_args
;
2335 void readline_find_completion(const char *cmdline
)
2337 const char *cmdname
;
2338 char *args
[MAX_ARGS
];
2339 int nb_args
, i
, len
;
2340 const char *ptype
, *str
;
2344 parse_cmdline(cmdline
, &nb_args
, args
);
2345 #ifdef DEBUG_COMPLETION
2346 for(i
= 0; i
< nb_args
; i
++) {
2347 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2351 /* if the line ends with a space, it means we want to complete the
2353 len
= strlen(cmdline
);
2354 if (len
> 0 && isspace(cmdline
[len
- 1])) {
2355 if (nb_args
>= MAX_ARGS
)
2357 args
[nb_args
++] = qemu_strdup("");
2360 /* command completion */
2365 completion_index
= strlen(cmdname
);
2366 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2367 cmd_completion(cmdname
, cmd
->name
);
2370 /* find the command */
2371 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2372 if (compare_cmd(args
[0], cmd
->name
))
2377 ptype
= cmd
->args_type
;
2378 for(i
= 0; i
< nb_args
- 2; i
++) {
2379 if (*ptype
!= '\0') {
2381 while (*ptype
== '?')
2385 str
= args
[nb_args
- 1];
2388 /* file completion */
2389 completion_index
= strlen(str
);
2390 file_completion(str
);
2393 /* block device name completion */
2394 completion_index
= strlen(str
);
2395 bdrv_iterate(block_completion_it
, (void *)str
);
2398 /* XXX: more generic ? */
2399 if (!strcmp(cmd
->name
, "info")) {
2400 completion_index
= strlen(str
);
2401 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2402 cmd_completion(str
, cmd
->name
);
2404 } else if (!strcmp(cmd
->name
, "sendkey")) {
2405 completion_index
= strlen(str
);
2406 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2407 cmd_completion(str
, key
->name
);
2415 for(i
= 0; i
< nb_args
; i
++)
2419 static int term_can_read(void *opaque
)
2424 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2427 for(i
= 0; i
< size
; i
++)
2428 readline_handle_byte(buf
[i
]);
2431 static void monitor_start_input(void);
2433 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2435 monitor_handle_command(cmdline
);
2436 monitor_start_input();
2439 static void monitor_start_input(void)
2441 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2444 static void term_event(void *opaque
, int event
)
2446 if (event
!= CHR_EVENT_RESET
)
2450 term_printf("QEMU %s monitor - type 'help' for more information\n",
2452 monitor_start_input();
2455 void monitor_init(CharDriverState
*hd
, int show_banner
)
2458 hide_banner
= !show_banner
;
2460 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2463 /* XXX: use threads ? */
2464 /* modal monitor readline */
2465 static int monitor_readline_started
;
2466 static char *monitor_readline_buf
;
2467 static int monitor_readline_buf_size
;
2469 static void monitor_readline_cb(void *opaque
, const char *input
)
2471 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2472 monitor_readline_started
= 0;
2475 void monitor_readline(const char *prompt
, int is_password
,
2476 char *buf
, int buf_size
)
2479 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
2481 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2482 monitor_readline_buf
= buf
;
2483 monitor_readline_buf_size
= buf_size
;
2484 monitor_readline_started
= 1;
2485 while (monitor_readline_started
) {