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
27 #include "migration.h"
32 //#define DEBUG_COMPLETION
35 #define offsetof(type, field) ((size_t) &((type *)0)->field)
42 * 'B' block device name
43 * 's' string (accept optional quote)
45 * 'l' target long (32 or 64 bit)
46 * '/' optional gdb-like print format (like "/10x")
47 * 'A' pass the rest of cmdline as one argument (for subcommands).
49 * '?' optional type (for 'F', 's' and 'i')
53 typedef struct term_cmd_t
{
55 const char *args_type
;
61 static CharDriverState
*monitor_hd
;
63 static term_cmd_t term_cmds
[];
64 static term_cmd_t info_cmds
[];
65 static term_cmd_t migration_cmds
[];
66 static term_cmd_t migration_set_cmds
[];
68 static char term_outbuf
[1024];
69 static int term_outbuf_index
;
71 static void monitor_start_input(void);
72 static void monitor_handle_command(const term_cmd_t
*cmds
,
76 CPUState
*mon_cpu
= NULL
;
80 if (term_outbuf_index
> 0) {
81 qemu_chr_write(monitor_hd
, term_outbuf
, term_outbuf_index
);
82 term_outbuf_index
= 0;
86 /* flush at every end of line or if the buffer is full */
87 void term_puts(const char *str
)
95 term_outbuf
[term_outbuf_index
++] = '\r';
96 term_outbuf
[term_outbuf_index
++] = c
;
97 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
103 void term_vprintf(const char *fmt
, va_list ap
)
106 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
110 void term_printf(const char *fmt
, ...)
114 term_vprintf(fmt
, ap
);
118 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
122 term_vprintf(fmt
, ap
);
127 static int compare_cmd(const char *name
, const char *list
)
129 const char *p
, *pstart
;
137 p
= pstart
+ strlen(pstart
);
138 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
147 static void help_cmd1(term_cmd_t
*cmds
, const char *prefix
, const char *name
)
151 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
152 if (!name
|| !strcmp(name
, cmd
->name
))
153 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
157 static void help_cmd(const char *name
)
159 if (name
&& !strcmp(name
, "info")) {
160 help_cmd1(info_cmds
, "info ", NULL
);
162 help_cmd1(term_cmds
, "", name
);
163 if (name
&& !strcmp(name
, "log")) {
165 term_printf("Log items (comma separated):\n");
166 term_printf("%-10s %s\n", "none", "remove all logs");
167 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
168 term_printf("%-10s %s\n", item
->name
, item
->help
);
174 static void do_help(const char *name
)
179 static void do_commit(void)
183 for (i
= 0; i
< MAX_DISKS
; i
++) {
185 bdrv_commit(bs_table
[i
]);
190 static void do_info(const char *item
)
196 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
197 if (compare_cmd(item
, cmd
->name
))
207 static void do_info_version(void)
209 term_printf("%s\n", QEMU_VERSION
);
212 static void do_info_block(void)
217 /* get the current CPU defined by the user */
218 int mon_set_cpu(int cpu_index
)
222 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
223 if (env
->cpu_index
== cpu_index
) {
231 CPUState
*mon_get_cpu(void)
239 static void do_info_registers(void)
246 cpu_dump_state(env
, NULL
, monitor_fprintf
,
249 cpu_dump_state(env
, NULL
, monitor_fprintf
,
254 static void do_info_cpus(void)
258 /* just to set the default cpu if not already done */
261 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
262 term_printf("%c CPU #%d:",
263 (env
== mon_cpu
) ? '*' : ' ',
265 #if defined(TARGET_I386)
266 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
267 if (env
->hflags
& HF_HALTED_MASK
)
268 term_printf(" (halted)");
269 #elif defined(TARGET_PPC)
270 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
272 term_printf(" (halted)");
273 #elif defined(TARGET_SPARC)
274 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
276 term_printf(" (halted)");
282 static void do_cpu_set(int index
)
284 if (mon_set_cpu(index
) < 0)
285 term_printf("Invalid CPU index\n");
288 static void do_info_jit(void)
290 dump_exec_info(NULL
, monitor_fprintf
);
293 static void do_info_history (void)
300 str
= readline_get_history(i
);
303 term_printf("%d: '%s'\n", i
, str
);
308 static void do_quit(void)
313 static int eject_device(BlockDriverState
*bs
, int force
)
315 if (bdrv_is_inserted(bs
)) {
317 if (!bdrv_is_removable(bs
)) {
318 term_printf("device is not removable\n");
321 if (bdrv_is_locked(bs
)) {
322 term_printf("device is locked\n");
331 static void do_eject(int force
, const char *filename
)
333 BlockDriverState
*bs
;
335 bs
= bdrv_find(filename
);
337 term_printf("device not found\n");
340 eject_device(bs
, force
);
344 int vmdk_snapshot_create(BlockDriverState
*bs
);
346 static void do_snapshot(const char *hda
, const char *hdb
,
347 const char *hdc
, const char *hdd
)
350 if (!strcmp(hda
,"hda")) {
351 if (vmdk_snapshot_create(bs_table
[0]) == -1)
352 term_printf("hda snapshot fail\n");
356 if (!strcmp(hdb
,"hdb")) {
358 if (vmdk_snapshot_create(bs_table
[1]) == -1)
359 term_printf("hdb snapshot fail\n");
361 term_printf("hdb not exist\n");
367 if (!strcmp(hdc
,"hdc")) {
369 if (vmdk_snapshot_create(bs_table
[2]) == -1)
370 term_printf("hdc snapshot fail\n");
372 term_printf("hdc not exist\n");
378 if (!strcmp(hdd
,"hdd")) {
380 if (vmdk_snapshot_create(bs_table
[3]) == -1)
381 term_printf("hdd snapshot fail\n");
383 term_printf("hdd not exist\n");
389 static void do_change(const char *device
, const char *filename
)
391 BlockDriverState
*bs
;
395 bs
= bdrv_find(device
);
397 term_printf("device not found\n");
400 if (eject_device(bs
, 0) < 0)
402 bdrv_open(bs
, filename
, 0);
403 if (bdrv_is_encrypted(bs
)) {
404 term_printf("%s is encrypted.\n", device
);
405 for(i
= 0; i
< 3; i
++) {
406 monitor_readline("Password: ", 1, password
, sizeof(password
));
407 if (bdrv_set_key(bs
, password
) == 0)
409 term_printf("invalid password\n");
414 static void do_screen_dump(const char *filename
)
416 vga_hw_screen_dump(filename
);
419 static void do_log(const char *items
)
423 if (!strcmp(items
, "none")) {
426 mask
= cpu_str_to_log_mask(items
);
435 static void do_savevm(const char *filename
)
437 if (qemu_savevm(filename
, &qemu_savevm_method_file
) < 0)
438 term_printf("I/O error when saving VM to '%s'\n", filename
);
441 static void do_loadvm(const char *filename
)
443 if (qemu_loadvm(filename
, &qemu_savevm_method_file
) < 0)
444 term_printf("I/O error when loading VM from '%s'\n", filename
);
447 static void do_stop(void)
449 vm_stop(EXCP_INTERRUPT
);
452 static void do_cont(void)
457 #ifdef CONFIG_GDBSTUB
458 static void do_gdbserver(int has_port
, int port
)
461 port
= DEFAULT_GDBSTUB_PORT
;
462 if (gdbserver_start(port
) < 0) {
463 qemu_printf("Could not open gdbserver socket on port %d\n", port
);
465 qemu_printf("Waiting gdb connection on port %d\n", port
);
470 static void term_printc(int c
)
487 if (c
>= 32 && c
<= 126) {
488 term_printf("%c", c
);
490 term_printf("\\x%02x", c
);
497 static void memory_dump(int count
, int format
, int wsize
,
498 target_ulong addr
, int is_physical
)
501 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
509 if (!env
&& !is_physical
)
514 } else if (wsize
== 4) {
517 /* as default we use the current CS size */
521 if ((env
->efer
& MSR_EFER_LMA
) &&
522 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
526 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
531 monitor_disas(env
, addr
, count
, is_physical
, flags
);
540 nb_per_line
= line_size
/ wsize
;
545 max_digits
= (wsize
* 8 + 2) / 3;
549 max_digits
= (wsize
* 8) / 4;
553 max_digits
= (wsize
* 8 * 10 + 32) / 33;
561 term_printf(TARGET_FMT_lx
":", addr
);
566 cpu_physical_memory_rw(addr
, buf
, l
, 0);
571 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
578 v
= ldub_raw(buf
+ i
);
581 v
= lduw_raw(buf
+ i
);
584 v
= (uint32_t)ldl_raw(buf
+ i
);
587 v
= ldq_raw(buf
+ i
);
593 term_printf("%#*" PRIo64
, max_digits
, v
);
596 term_printf("0x%0*" PRIx64
, max_digits
, v
);
599 term_printf("%*" PRIu64
, max_digits
, v
);
602 term_printf("%*" PRId64
, max_digits
, v
);
616 #if TARGET_LONG_BITS == 64
617 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
619 #define GET_TLONG(h, l) (l)
622 static void do_memory_dump(int count
, int format
, int size
,
623 uint32_t addrh
, uint32_t addrl
)
625 target_long addr
= GET_TLONG(addrh
, addrl
);
626 memory_dump(count
, format
, size
, addr
, 0);
629 static void do_physical_memory_dump(int count
, int format
, int size
,
630 uint32_t addrh
, uint32_t addrl
)
633 target_long addr
= GET_TLONG(addrh
, addrl
);
634 memory_dump(count
, format
, size
, addr
, 1);
637 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
639 target_long val
= GET_TLONG(valh
, vall
);
640 #if TARGET_LONG_BITS == 32
643 term_printf("%#o", val
);
646 term_printf("%#x", val
);
649 term_printf("%u", val
);
653 term_printf("%d", val
);
662 term_printf("%#" PRIo64
, val
);
665 term_printf("%#" PRIx64
, val
);
668 term_printf("%" PRIu64
, val
);
672 term_printf("%" PRId64
, val
);
682 static void do_sum(uint32_t start
, uint32_t size
)
689 for(addr
= start
; addr
< (start
+ size
); addr
++) {
690 cpu_physical_memory_rw(addr
, buf
, 1, 0);
691 /* BSD sum algorithm ('sum' Unix command) */
692 sum
= (sum
>> 1) | (sum
<< 15);
695 term_printf("%05d\n", sum
);
703 static const KeyDef key_defs
[] = {
728 { 0x0e, "backspace" },
763 { 0x3a, "caps_lock" },
774 { 0x45, "num_lock" },
775 { 0x46, "scroll_lock" },
777 { 0xb5, "kp_divide" },
778 { 0x37, "kp_multiply" },
779 { 0x4a, "kp_substract" },
781 { 0x9c, "kp_enter" },
782 { 0x53, "kp_decimal" },
817 static int get_keycode(const char *key
)
823 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
824 if (!strcmp(key
, p
->name
))
827 if (strstart(key
, "0x", NULL
)) {
828 ret
= strtoul(key
, &endp
, 0);
829 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
835 static void do_send_key(const char *string
)
838 uint8_t keycodes
[16];
840 int nb_keycodes
, keycode
, i
;
846 while (*p
!= '\0' && *p
!= '-') {
847 if ((q
- keybuf
) < sizeof(keybuf
) - 1) {
853 keycode
= get_keycode(keybuf
);
855 term_printf("unknown key: '%s'\n", keybuf
);
858 keycodes
[nb_keycodes
++] = keycode
;
863 /* key down events */
864 for(i
= 0; i
< nb_keycodes
; i
++) {
865 keycode
= keycodes
[i
];
867 kbd_put_keycode(0xe0);
868 kbd_put_keycode(keycode
& 0x7f);
871 for(i
= nb_keycodes
- 1; i
>= 0; i
--) {
872 keycode
= keycodes
[i
];
874 kbd_put_keycode(0xe0);
875 kbd_put_keycode(keycode
| 0x80);
879 static int mouse_button_state
;
881 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
885 dx
= strtol(dx_str
, NULL
, 0);
886 dy
= strtol(dy_str
, NULL
, 0);
889 dz
= strtol(dz_str
, NULL
, 0);
890 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
893 static void do_mouse_button(int button_state
)
895 mouse_button_state
= button_state
;
896 kbd_mouse_event(0, 0, 0, mouse_button_state
);
899 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
905 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
913 val
= cpu_inb(NULL
, addr
);
917 val
= cpu_inw(NULL
, addr
);
921 val
= cpu_inl(NULL
, addr
);
925 term_printf("port%c[0x%04x] = %#0*x\n",
926 suffix
, addr
, size
* 2, val
);
929 static void do_system_reset(void)
931 qemu_system_reset_request();
934 static void do_system_powerdown(void)
936 qemu_system_powerdown_request();
939 #if defined(TARGET_I386)
940 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
942 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
945 pte
& PG_GLOBAL_MASK
? 'G' : '-',
946 pte
& PG_PSE_MASK
? 'P' : '-',
947 pte
& PG_DIRTY_MASK
? 'D' : '-',
948 pte
& PG_ACCESSED_MASK
? 'A' : '-',
949 pte
& PG_PCD_MASK
? 'C' : '-',
950 pte
& PG_PWT_MASK
? 'T' : '-',
951 pte
& PG_USER_MASK
? 'U' : '-',
952 pte
& PG_RW_MASK
? 'W' : '-');
955 static void tlb_info(void)
959 uint32_t pgd
, pde
, pte
;
965 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
966 term_printf("PG disabled\n");
969 pgd
= env
->cr
[3] & ~0xfff;
970 for(l1
= 0; l1
< 1024; l1
++) {
971 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
972 pde
= le32_to_cpu(pde
);
973 if (pde
& PG_PRESENT_MASK
) {
974 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
975 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
977 for(l2
= 0; l2
< 1024; l2
++) {
978 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
980 pte
= le32_to_cpu(pte
);
981 if (pte
& PG_PRESENT_MASK
) {
982 print_pte((l1
<< 22) + (l2
<< 12),
992 static void mem_print(uint32_t *pstart
, int *plast_prot
,
993 uint32_t end
, int prot
)
999 term_printf("%08x-%08x %08x %c%c%c\n",
1000 *pstart
, end
, end
- *pstart
,
1001 prot1
& PG_USER_MASK
? 'u' : '-',
1003 prot1
& PG_RW_MASK
? 'w' : '-');
1013 static void mem_info(void)
1016 int l1
, l2
, prot
, last_prot
;
1017 uint32_t pgd
, pde
, pte
, start
, end
;
1019 env
= mon_get_cpu();
1023 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1024 term_printf("PG disabled\n");
1027 pgd
= env
->cr
[3] & ~0xfff;
1030 for(l1
= 0; l1
< 1024; l1
++) {
1031 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1032 pde
= le32_to_cpu(pde
);
1034 if (pde
& PG_PRESENT_MASK
) {
1035 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1036 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1037 mem_print(&start
, &last_prot
, end
, prot
);
1039 for(l2
= 0; l2
< 1024; l2
++) {
1040 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1041 (uint8_t *)&pte
, 4);
1042 pte
= le32_to_cpu(pte
);
1043 end
= (l1
<< 22) + (l2
<< 12);
1044 if (pte
& PG_PRESENT_MASK
) {
1045 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1049 mem_print(&start
, &last_prot
, end
, prot
);
1054 mem_print(&start
, &last_prot
, end
, prot
);
1060 static void do_info_kqemu(void)
1066 env
= mon_get_cpu();
1068 term_printf("No cpu initialized yet");
1071 val
= env
->kqemu_enabled
;
1072 term_printf("kqemu support: ");
1076 term_printf("disabled\n");
1079 term_printf("enabled for user code\n");
1082 term_printf("enabled for user and kernel code\n");
1086 term_printf("kqemu support: not compiled\n");
1090 #ifdef CONFIG_PROFILER
1094 int64_t kqemu_exec_count
;
1096 int64_t kqemu_ret_int_count
;
1097 int64_t kqemu_ret_excp_count
;
1098 int64_t kqemu_ret_intr_count
;
1100 static void do_info_profile(void)
1106 term_printf("async time %" PRId64
" (%0.3f)\n",
1107 dev_time
, dev_time
/ (double)ticks_per_sec
);
1108 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1109 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1110 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1111 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1112 kqemu_time
/ (double)total
* 100.0,
1114 kqemu_ret_int_count
,
1115 kqemu_ret_excp_count
,
1116 kqemu_ret_intr_count
);
1119 kqemu_exec_count
= 0;
1121 kqemu_ret_int_count
= 0;
1122 kqemu_ret_excp_count
= 0;
1123 kqemu_ret_intr_count
= 0;
1125 kqemu_record_dump();
1129 static void do_info_profile(void)
1131 term_printf("Internal profiler not compiled\n");
1135 /* Capture support */
1136 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1138 static void do_info_capture (void)
1143 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1144 term_printf ("[%d]: ", i
);
1145 s
->ops
.info (s
->opaque
);
1149 static void do_stop_capture (int n
)
1154 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1156 s
->ops
.destroy (s
->opaque
);
1157 LIST_REMOVE (s
, entries
);
1164 static void do_migration(const char *subcmdline
)
1166 if (subcmdline
[0] == '\0')
1167 subcmdline
= "help";
1168 monitor_handle_command(migration_cmds
, subcmdline
);
1171 static void do_migration_help(const char *name
)
1173 help_cmd1(migration_cmds
, "migration ", name
);
1176 static void do_migration_set(const char *subcmdline
)
1178 if (subcmdline
[0] == '\0')
1179 subcmdline
= "help";
1180 monitor_handle_command(migration_set_cmds
, subcmdline
);
1183 static void do_migration_set_help(const char *name
)
1185 help_cmd1(migration_set_cmds
, "migration set ", name
);
1189 int wav_start_capture (CaptureState
*s
, const char *path
, int freq
,
1190 int bits
, int nchannels
);
1192 static void do_wav_capture (const char *path
,
1193 int has_freq
, int freq
,
1194 int has_bits
, int bits
,
1195 int has_channels
, int nchannels
)
1199 s
= qemu_mallocz (sizeof (*s
));
1201 term_printf ("Not enough memory to add wave capture\n");
1205 freq
= has_freq
? freq
: 44100;
1206 bits
= has_bits
? bits
: 16;
1207 nchannels
= has_channels
? nchannels
: 2;
1209 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1210 term_printf ("Faied to add wave capture\n");
1213 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1217 static term_cmd_t term_cmds
[] = {
1218 { "help|?", "s?", do_help
,
1219 "[cmd]", "show the help" },
1220 { "commit", "", do_commit
,
1221 "", "commit changes to the disk images (if -snapshot is used)" },
1222 { "info", "s?", do_info
,
1223 "subcommand", "show various information about the system state" },
1224 { "q|quit", "", do_quit
,
1225 "", "quit the emulator" },
1226 { "eject", "-fB", do_eject
,
1227 "[-f] device", "eject a removable media (use -f to force it)" },
1228 { "change", "BF", do_change
,
1229 "device filename", "change a removable media" },
1230 { "screendump", "F", do_screen_dump
,
1231 "filename", "save screen into PPM image 'filename'" },
1232 { "log", "s", do_log
,
1233 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1234 { "savevm", "F", do_savevm
,
1235 "filename", "save the whole virtual machine state to 'filename'" },
1236 { "loadvm", "F", do_loadvm
,
1237 "filename", "restore the whole virtual machine state from 'filename'" },
1238 { "stop", "", do_stop
,
1239 "", "stop emulation", },
1240 { "c|cont", "", do_cont
,
1241 "", "resume emulation", },
1242 #ifdef CONFIG_GDBSTUB
1243 { "gdbserver", "i?", do_gdbserver
,
1244 "[port]", "start gdbserver session (default port=1234)", },
1246 { "x", "/l", do_memory_dump
,
1247 "/fmt addr", "virtual memory dump starting at 'addr'", },
1248 { "xp", "/l", do_physical_memory_dump
,
1249 "/fmt addr", "physical memory dump starting at 'addr'", },
1250 { "p|print", "/l", do_print
,
1251 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1252 { "i", "/ii.", do_ioport_read
,
1253 "/fmt addr", "I/O port read" },
1255 { "sendkey", "s", do_send_key
,
1256 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
1257 { "system_reset", "", do_system_reset
,
1258 "", "reset the system" },
1259 { "system_powerdown", "", do_system_powerdown
,
1260 "", "send system power down event" },
1261 { "sum", "ii", do_sum
,
1262 "addr size", "compute the checksum of a memory region" },
1263 { "usb_add", "s", do_usb_add
,
1264 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1265 { "usb_del", "s", do_usb_del
,
1266 "device", "remove USB device 'bus.addr'" },
1267 { "cpu", "i", do_cpu_set
,
1268 "index", "set the default CPU" },
1269 { "mouse_move", "sss?", do_mouse_move
,
1270 "dx dy [dz]", "send mouse move events" },
1271 { "mouse_button", "i", do_mouse_button
,
1272 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1274 { "wavcapture", "si?i?i?", do_wav_capture
,
1275 "path [frequency bits channels]",
1276 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1278 { "stopcapture", "i", do_stop_capture
,
1279 "capture index", "stop capture" },
1280 { "create_snapshot", "ss?s?s?", do_snapshot
,
1281 "hda [hdb] [hdc] [hdd]", "create snapshot of one or more images (VMDK format)" },
1282 { "migration", "A", do_migration
, "subcommand|help",
1283 "start/stop/manage migrations"},
1287 static term_cmd_t info_cmds
[] = {
1288 { "version", "", do_info_version
,
1289 "", "show the version of qemu" },
1290 { "network", "", do_info_network
,
1291 "", "show the network state" },
1292 { "block", "", do_info_block
,
1293 "", "show the block devices" },
1294 { "registers", "", do_info_registers
,
1295 "", "show the cpu registers" },
1296 { "cpus", "", do_info_cpus
,
1297 "", "show infos for each CPU" },
1298 { "history", "", do_info_history
,
1299 "", "show the command line history", },
1300 { "irq", "", irq_info
,
1301 "", "show the interrupts statistics (if available)", },
1302 { "pic", "", pic_info
,
1303 "", "show i8259 (PIC) state", },
1304 { "pci", "", pci_info
,
1305 "", "show PCI info", },
1306 #if defined(TARGET_I386)
1307 { "tlb", "", tlb_info
,
1308 "", "show virtual to physical memory mappings", },
1309 { "mem", "", mem_info
,
1310 "", "show the active virtual memory mappings", },
1312 { "jit", "", do_info_jit
,
1313 "", "show dynamic compiler info", },
1314 { "kqemu", "", do_info_kqemu
,
1315 "", "show kqemu information", },
1316 { "usb", "", usb_info
,
1317 "", "show guest USB devices", },
1318 { "usbhost", "", usb_host_info
,
1319 "", "show host USB devices", },
1320 { "profile", "", do_info_profile
,
1321 "", "show profiling information", },
1322 { "capture", "", do_info_capture
,
1323 "show capture information" },
1328 static term_cmd_t migration_cmds
[] = {
1329 { "listen", "s?s?", do_migration_listen
,
1330 "[local_host:port [remote_host:port]]", "listen to a port" },
1331 { "connect", "s?s?", do_migration_connect
,
1332 "[local_host:port [remote_host:port]]", "connect to a port"},
1333 { "getfd", "i", do_migration_getfd
, "fd (socket)",
1334 "get established connection"},
1335 { "start", "s", do_migration_start
, "online|offline" ,
1336 "start the migration proccess"},
1337 { "cancel", "", do_migration_cancel
, "",
1338 "cancel an ongoing migration procces"},
1339 { "status", "", do_migration_status
, "", "get migration status/progress"},
1340 { "set", "A", do_migration_set
, "params", "set migration parameters"},
1341 { "show", "", do_migration_show
, "", "show migration parameters"},
1342 { "help", "s?", do_migration_help
, "[subcommand]", "show help message"},
1346 static term_cmd_t migration_set_cmds
[] = {
1347 { "rate", "iii", do_migration_set_rate
, "min max offline", "bandwidth params" },
1348 { "total_time", "i", do_migration_set_total_time
, "seconds", "max migration time"},
1349 { "help", "s?", do_migration_set_help
, "[subcommand]", "show help message"},
1353 /*******************************************************************/
1355 static const char *pch
;
1356 static jmp_buf expr_env
;
1361 typedef struct MonitorDef
{
1364 target_long (*get_value
)(struct MonitorDef
*md
, int val
);
1368 #if defined(TARGET_I386)
1369 static target_long
monitor_get_pc (struct MonitorDef
*md
, int val
)
1371 CPUState
*env
= mon_get_cpu();
1374 return env
->eip
+ env
->segs
[R_CS
].base
;
1378 #if defined(TARGET_PPC)
1379 static target_long
monitor_get_ccr (struct MonitorDef
*md
, int val
)
1381 CPUState
*env
= mon_get_cpu();
1389 for (i
= 0; i
< 8; i
++)
1390 u
|= env
->crf
[i
] << (32 - (4 * i
));
1395 static target_long
monitor_get_msr (struct MonitorDef
*md
, int val
)
1397 CPUState
*env
= mon_get_cpu();
1400 return (env
->msr
[MSR_POW
] << MSR_POW
) |
1401 (env
->msr
[MSR_ILE
] << MSR_ILE
) |
1402 (env
->msr
[MSR_EE
] << MSR_EE
) |
1403 (env
->msr
[MSR_PR
] << MSR_PR
) |
1404 (env
->msr
[MSR_FP
] << MSR_FP
) |
1405 (env
->msr
[MSR_ME
] << MSR_ME
) |
1406 (env
->msr
[MSR_FE0
] << MSR_FE0
) |
1407 (env
->msr
[MSR_SE
] << MSR_SE
) |
1408 (env
->msr
[MSR_BE
] << MSR_BE
) |
1409 (env
->msr
[MSR_FE1
] << MSR_FE1
) |
1410 (env
->msr
[MSR_IP
] << MSR_IP
) |
1411 (env
->msr
[MSR_IR
] << MSR_IR
) |
1412 (env
->msr
[MSR_DR
] << MSR_DR
) |
1413 (env
->msr
[MSR_RI
] << MSR_RI
) |
1414 (env
->msr
[MSR_LE
] << MSR_LE
);
1417 static target_long
monitor_get_xer (struct MonitorDef
*md
, int val
)
1419 CPUState
*env
= mon_get_cpu();
1422 return (env
->xer
[XER_SO
] << XER_SO
) |
1423 (env
->xer
[XER_OV
] << XER_OV
) |
1424 (env
->xer
[XER_CA
] << XER_CA
) |
1425 (env
->xer
[XER_BC
] << XER_BC
);
1428 static target_long
monitor_get_decr (struct MonitorDef
*md
, int val
)
1430 CPUState
*env
= mon_get_cpu();
1433 return cpu_ppc_load_decr(env
);
1436 static target_long
monitor_get_tbu (struct MonitorDef
*md
, int val
)
1438 CPUState
*env
= mon_get_cpu();
1441 return cpu_ppc_load_tbu(env
);
1444 static target_long
monitor_get_tbl (struct MonitorDef
*md
, int val
)
1446 CPUState
*env
= mon_get_cpu();
1449 return cpu_ppc_load_tbl(env
);
1453 #if defined(TARGET_SPARC)
1454 #ifndef TARGET_SPARC64
1455 static target_long
monitor_get_psr (struct MonitorDef
*md
, int val
)
1457 CPUState
*env
= mon_get_cpu();
1460 return GET_PSR(env
);
1464 static target_long
monitor_get_reg(struct MonitorDef
*md
, int val
)
1466 CPUState
*env
= mon_get_cpu();
1469 return env
->regwptr
[val
];
1473 static MonitorDef monitor_defs
[] = {
1476 #define SEG(name, seg) \
1477 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1478 { name ".base", offsetof(CPUState, segs[seg].base) },\
1479 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1481 { "eax", offsetof(CPUState
, regs
[0]) },
1482 { "ecx", offsetof(CPUState
, regs
[1]) },
1483 { "edx", offsetof(CPUState
, regs
[2]) },
1484 { "ebx", offsetof(CPUState
, regs
[3]) },
1485 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1486 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1487 { "esi", offsetof(CPUState
, regs
[6]) },
1488 { "edi", offsetof(CPUState
, regs
[7]) },
1489 #ifdef TARGET_X86_64
1490 { "r8", offsetof(CPUState
, regs
[8]) },
1491 { "r9", offsetof(CPUState
, regs
[9]) },
1492 { "r10", offsetof(CPUState
, regs
[10]) },
1493 { "r11", offsetof(CPUState
, regs
[11]) },
1494 { "r12", offsetof(CPUState
, regs
[12]) },
1495 { "r13", offsetof(CPUState
, regs
[13]) },
1496 { "r14", offsetof(CPUState
, regs
[14]) },
1497 { "r15", offsetof(CPUState
, regs
[15]) },
1499 { "eflags", offsetof(CPUState
, eflags
) },
1500 { "eip", offsetof(CPUState
, eip
) },
1507 { "pc", 0, monitor_get_pc
, },
1508 #elif defined(TARGET_PPC)
1509 { "r0", offsetof(CPUState
, gpr
[0]) },
1510 { "r1", offsetof(CPUState
, gpr
[1]) },
1511 { "r2", offsetof(CPUState
, gpr
[2]) },
1512 { "r3", offsetof(CPUState
, gpr
[3]) },
1513 { "r4", offsetof(CPUState
, gpr
[4]) },
1514 { "r5", offsetof(CPUState
, gpr
[5]) },
1515 { "r6", offsetof(CPUState
, gpr
[6]) },
1516 { "r7", offsetof(CPUState
, gpr
[7]) },
1517 { "r8", offsetof(CPUState
, gpr
[8]) },
1518 { "r9", offsetof(CPUState
, gpr
[9]) },
1519 { "r10", offsetof(CPUState
, gpr
[10]) },
1520 { "r11", offsetof(CPUState
, gpr
[11]) },
1521 { "r12", offsetof(CPUState
, gpr
[12]) },
1522 { "r13", offsetof(CPUState
, gpr
[13]) },
1523 { "r14", offsetof(CPUState
, gpr
[14]) },
1524 { "r15", offsetof(CPUState
, gpr
[15]) },
1525 { "r16", offsetof(CPUState
, gpr
[16]) },
1526 { "r17", offsetof(CPUState
, gpr
[17]) },
1527 { "r18", offsetof(CPUState
, gpr
[18]) },
1528 { "r19", offsetof(CPUState
, gpr
[19]) },
1529 { "r20", offsetof(CPUState
, gpr
[20]) },
1530 { "r21", offsetof(CPUState
, gpr
[21]) },
1531 { "r22", offsetof(CPUState
, gpr
[22]) },
1532 { "r23", offsetof(CPUState
, gpr
[23]) },
1533 { "r24", offsetof(CPUState
, gpr
[24]) },
1534 { "r25", offsetof(CPUState
, gpr
[25]) },
1535 { "r26", offsetof(CPUState
, gpr
[26]) },
1536 { "r27", offsetof(CPUState
, gpr
[27]) },
1537 { "r28", offsetof(CPUState
, gpr
[28]) },
1538 { "r29", offsetof(CPUState
, gpr
[29]) },
1539 { "r30", offsetof(CPUState
, gpr
[30]) },
1540 { "r31", offsetof(CPUState
, gpr
[31]) },
1541 { "nip|pc", offsetof(CPUState
, nip
) },
1542 { "lr", offsetof(CPUState
, lr
) },
1543 { "ctr", offsetof(CPUState
, ctr
) },
1544 { "decr", 0, &monitor_get_decr
, },
1545 { "ccr", 0, &monitor_get_ccr
, },
1546 { "msr", 0, &monitor_get_msr
, },
1547 { "xer", 0, &monitor_get_xer
, },
1548 { "tbu", 0, &monitor_get_tbu
, },
1549 { "tbl", 0, &monitor_get_tbl
, },
1550 { "sdr1", offsetof(CPUState
, sdr1
) },
1551 { "sr0", offsetof(CPUState
, sr
[0]) },
1552 { "sr1", offsetof(CPUState
, sr
[1]) },
1553 { "sr2", offsetof(CPUState
, sr
[2]) },
1554 { "sr3", offsetof(CPUState
, sr
[3]) },
1555 { "sr4", offsetof(CPUState
, sr
[4]) },
1556 { "sr5", offsetof(CPUState
, sr
[5]) },
1557 { "sr6", offsetof(CPUState
, sr
[6]) },
1558 { "sr7", offsetof(CPUState
, sr
[7]) },
1559 { "sr8", offsetof(CPUState
, sr
[8]) },
1560 { "sr9", offsetof(CPUState
, sr
[9]) },
1561 { "sr10", offsetof(CPUState
, sr
[10]) },
1562 { "sr11", offsetof(CPUState
, sr
[11]) },
1563 { "sr12", offsetof(CPUState
, sr
[12]) },
1564 { "sr13", offsetof(CPUState
, sr
[13]) },
1565 { "sr14", offsetof(CPUState
, sr
[14]) },
1566 { "sr15", offsetof(CPUState
, sr
[15]) },
1567 /* Too lazy to put BATs and SPRs ... */
1568 #elif defined(TARGET_SPARC)
1569 { "g0", offsetof(CPUState
, gregs
[0]) },
1570 { "g1", offsetof(CPUState
, gregs
[1]) },
1571 { "g2", offsetof(CPUState
, gregs
[2]) },
1572 { "g3", offsetof(CPUState
, gregs
[3]) },
1573 { "g4", offsetof(CPUState
, gregs
[4]) },
1574 { "g5", offsetof(CPUState
, gregs
[5]) },
1575 { "g6", offsetof(CPUState
, gregs
[6]) },
1576 { "g7", offsetof(CPUState
, gregs
[7]) },
1577 { "o0", 0, monitor_get_reg
},
1578 { "o1", 1, monitor_get_reg
},
1579 { "o2", 2, monitor_get_reg
},
1580 { "o3", 3, monitor_get_reg
},
1581 { "o4", 4, monitor_get_reg
},
1582 { "o5", 5, monitor_get_reg
},
1583 { "o6", 6, monitor_get_reg
},
1584 { "o7", 7, monitor_get_reg
},
1585 { "l0", 8, monitor_get_reg
},
1586 { "l1", 9, monitor_get_reg
},
1587 { "l2", 10, monitor_get_reg
},
1588 { "l3", 11, monitor_get_reg
},
1589 { "l4", 12, monitor_get_reg
},
1590 { "l5", 13, monitor_get_reg
},
1591 { "l6", 14, monitor_get_reg
},
1592 { "l7", 15, monitor_get_reg
},
1593 { "i0", 16, monitor_get_reg
},
1594 { "i1", 17, monitor_get_reg
},
1595 { "i2", 18, monitor_get_reg
},
1596 { "i3", 19, monitor_get_reg
},
1597 { "i4", 20, monitor_get_reg
},
1598 { "i5", 21, monitor_get_reg
},
1599 { "i6", 22, monitor_get_reg
},
1600 { "i7", 23, monitor_get_reg
},
1601 { "pc", offsetof(CPUState
, pc
) },
1602 { "npc", offsetof(CPUState
, npc
) },
1603 { "y", offsetof(CPUState
, y
) },
1604 #ifndef TARGET_SPARC64
1605 { "psr", 0, &monitor_get_psr
, },
1606 { "wim", offsetof(CPUState
, wim
) },
1608 { "tbr", offsetof(CPUState
, tbr
) },
1609 { "fsr", offsetof(CPUState
, fsr
) },
1610 { "f0", offsetof(CPUState
, fpr
[0]) },
1611 { "f1", offsetof(CPUState
, fpr
[1]) },
1612 { "f2", offsetof(CPUState
, fpr
[2]) },
1613 { "f3", offsetof(CPUState
, fpr
[3]) },
1614 { "f4", offsetof(CPUState
, fpr
[4]) },
1615 { "f5", offsetof(CPUState
, fpr
[5]) },
1616 { "f6", offsetof(CPUState
, fpr
[6]) },
1617 { "f7", offsetof(CPUState
, fpr
[7]) },
1618 { "f8", offsetof(CPUState
, fpr
[8]) },
1619 { "f9", offsetof(CPUState
, fpr
[9]) },
1620 { "f10", offsetof(CPUState
, fpr
[10]) },
1621 { "f11", offsetof(CPUState
, fpr
[11]) },
1622 { "f12", offsetof(CPUState
, fpr
[12]) },
1623 { "f13", offsetof(CPUState
, fpr
[13]) },
1624 { "f14", offsetof(CPUState
, fpr
[14]) },
1625 { "f15", offsetof(CPUState
, fpr
[15]) },
1626 { "f16", offsetof(CPUState
, fpr
[16]) },
1627 { "f17", offsetof(CPUState
, fpr
[17]) },
1628 { "f18", offsetof(CPUState
, fpr
[18]) },
1629 { "f19", offsetof(CPUState
, fpr
[19]) },
1630 { "f20", offsetof(CPUState
, fpr
[20]) },
1631 { "f21", offsetof(CPUState
, fpr
[21]) },
1632 { "f22", offsetof(CPUState
, fpr
[22]) },
1633 { "f23", offsetof(CPUState
, fpr
[23]) },
1634 { "f24", offsetof(CPUState
, fpr
[24]) },
1635 { "f25", offsetof(CPUState
, fpr
[25]) },
1636 { "f26", offsetof(CPUState
, fpr
[26]) },
1637 { "f27", offsetof(CPUState
, fpr
[27]) },
1638 { "f28", offsetof(CPUState
, fpr
[28]) },
1639 { "f29", offsetof(CPUState
, fpr
[29]) },
1640 { "f30", offsetof(CPUState
, fpr
[30]) },
1641 { "f31", offsetof(CPUState
, fpr
[31]) },
1642 #ifdef TARGET_SPARC64
1643 { "f32", offsetof(CPUState
, fpr
[32]) },
1644 { "f34", offsetof(CPUState
, fpr
[34]) },
1645 { "f36", offsetof(CPUState
, fpr
[36]) },
1646 { "f38", offsetof(CPUState
, fpr
[38]) },
1647 { "f40", offsetof(CPUState
, fpr
[40]) },
1648 { "f42", offsetof(CPUState
, fpr
[42]) },
1649 { "f44", offsetof(CPUState
, fpr
[44]) },
1650 { "f46", offsetof(CPUState
, fpr
[46]) },
1651 { "f48", offsetof(CPUState
, fpr
[48]) },
1652 { "f50", offsetof(CPUState
, fpr
[50]) },
1653 { "f52", offsetof(CPUState
, fpr
[52]) },
1654 { "f54", offsetof(CPUState
, fpr
[54]) },
1655 { "f56", offsetof(CPUState
, fpr
[56]) },
1656 { "f58", offsetof(CPUState
, fpr
[58]) },
1657 { "f60", offsetof(CPUState
, fpr
[60]) },
1658 { "f62", offsetof(CPUState
, fpr
[62]) },
1659 { "asi", offsetof(CPUState
, asi
) },
1660 { "pstate", offsetof(CPUState
, pstate
) },
1661 { "cansave", offsetof(CPUState
, cansave
) },
1662 { "canrestore", offsetof(CPUState
, canrestore
) },
1663 { "otherwin", offsetof(CPUState
, otherwin
) },
1664 { "wstate", offsetof(CPUState
, wstate
) },
1665 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1666 { "fprs", offsetof(CPUState
, fprs
) },
1672 static void expr_error(const char *fmt
)
1676 longjmp(expr_env
, 1);
1679 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1680 static int get_monitor_def(target_long
*pval
, const char *name
)
1685 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1686 if (compare_cmd(name
, md
->name
)) {
1687 if (md
->get_value
) {
1688 *pval
= md
->get_value(md
, md
->offset
);
1690 CPUState
*env
= mon_get_cpu();
1693 ptr
= (uint8_t *)env
+ md
->offset
;
1696 *pval
= *(int32_t *)ptr
;
1699 *pval
= *(target_long
*)ptr
;
1712 static void next(void)
1716 while (isspace(*pch
))
1721 static target_long
expr_sum(void);
1723 static target_long
expr_unary(void)
1746 expr_error("')' expected");
1753 expr_error("character constant expected");
1757 expr_error("missing terminating \' character");
1766 while ((*pch
>= 'a' && *pch
<= 'z') ||
1767 (*pch
>= 'A' && *pch
<= 'Z') ||
1768 (*pch
>= '0' && *pch
<= '9') ||
1769 *pch
== '_' || *pch
== '.') {
1770 if ((q
- buf
) < sizeof(buf
) - 1)
1774 while (isspace(*pch
))
1777 ret
= get_monitor_def(&n
, buf
);
1779 expr_error("unknown register");
1781 expr_error("no cpu defined");
1785 expr_error("unexpected end of expression");
1789 #if TARGET_LONG_BITS == 64
1790 n
= strtoull(pch
, &p
, 0);
1792 n
= strtoul(pch
, &p
, 0);
1795 expr_error("invalid char in expression");
1798 while (isspace(*pch
))
1806 static target_long
expr_prod(void)
1808 target_long val
, val2
;
1814 if (op
!= '*' && op
!= '/' && op
!= '%')
1817 val2
= expr_unary();
1826 expr_error("division by zero");
1837 static target_long
expr_logic(void)
1839 target_long val
, val2
;
1845 if (op
!= '&' && op
!= '|' && op
!= '^')
1865 static target_long
expr_sum(void)
1867 target_long val
, val2
;
1873 if (op
!= '+' && op
!= '-')
1876 val2
= expr_logic();
1885 static int get_expr(target_long
*pval
, const char **pp
)
1888 if (setjmp(expr_env
)) {
1892 while (isspace(*pch
))
1899 static int get_str(char *buf
, int buf_size
, const char **pp
)
1917 while (*p
!= '\0' && *p
!= '\"') {
1933 qemu_printf("unsupported escape code: '\\%c'\n", c
);
1936 if ((q
- buf
) < buf_size
- 1) {
1940 if ((q
- buf
) < buf_size
- 1) {
1947 qemu_printf("unterminated string\n");
1952 while (*p
!= '\0' && !isspace(*p
)) {
1953 if ((q
- buf
) < buf_size
- 1) {
1964 static int default_fmt_format
= 'x';
1965 static int default_fmt_size
= 4;
1969 static void monitor_handle_command(const term_cmd_t
*cmds
, const char *cmdline
)
1971 const char *p
, *pstart
, *typestr
;
1973 int c
, nb_args
, len
, i
, has_arg
;
1977 void *str_allocated
[MAX_ARGS
];
1978 void *args
[MAX_ARGS
];
1981 term_printf("command='%s'\n", cmdline
);
1984 /* extract the command name */
1992 while (*p
!= '\0' && *p
!= '/' && !isspace(*p
))
1995 if (len
> sizeof(cmdname
) - 1)
1996 len
= sizeof(cmdname
) - 1;
1997 memcpy(cmdname
, pstart
, len
);
1998 cmdname
[len
] = '\0';
2000 /* find the command */
2001 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
2002 if (compare_cmd(cmdname
, cmd
->name
))
2005 term_printf("unknown command: '%s'\n", cmdname
);
2009 for(i
= 0; i
< MAX_ARGS
; i
++)
2010 str_allocated
[i
] = NULL
;
2012 /* parse the parameters */
2013 typestr
= cmd
->args_type
;
2016 while (isspace(*p
)) /* eat whitespaces */
2030 if (*typestr
== '?') {
2033 /* no optional string: NULL argument */
2038 ret
= get_str(buf
, sizeof(buf
), &p
);
2042 term_printf("%s: filename expected\n", cmdname
);
2045 term_printf("%s: block device name expected\n", cmdname
);
2048 term_printf("%s: string expected\n", cmdname
);
2053 str
= qemu_malloc(strlen(buf
) + 1);
2055 str_allocated
[nb_args
] = str
;
2057 if (nb_args
>= MAX_ARGS
) {
2059 term_printf("%s: too many arguments\n", cmdname
);
2062 args
[nb_args
++] = str
;
2067 int count
, format
, size
;
2075 while (isdigit(*p
)) {
2076 count
= count
* 10 + (*p
- '0');
2114 if (*p
!= '\0' && !isspace(*p
)) {
2115 term_printf("invalid char in format: '%c'\n", *p
);
2119 format
= default_fmt_format
;
2120 if (format
!= 'i') {
2121 /* for 'i', not specifying a size gives -1 as size */
2123 size
= default_fmt_size
;
2125 default_fmt_size
= size
;
2126 default_fmt_format
= format
;
2129 format
= default_fmt_format
;
2130 if (format
!= 'i') {
2131 size
= default_fmt_size
;
2136 if (nb_args
+ 3 > MAX_ARGS
)
2138 args
[nb_args
++] = (void*)count
;
2139 args
[nb_args
++] = (void*)format
;
2140 args
[nb_args
++] = (void*)size
;
2147 if (*typestr
== '?' || *typestr
== '.') {
2148 if (*typestr
== '?') {
2164 if (nb_args
>= MAX_ARGS
)
2166 args
[nb_args
++] = (void *)has_arg
;
2168 if (nb_args
>= MAX_ARGS
)
2174 if (get_expr(&val
, &p
))
2178 if (nb_args
>= MAX_ARGS
)
2180 args
[nb_args
++] = (void *)(int)val
;
2182 if ((nb_args
+ 1) >= MAX_ARGS
)
2184 #if TARGET_LONG_BITS == 64
2185 args
[nb_args
++] = (void *)(int)((val
>> 32) & 0xffffffff);
2187 args
[nb_args
++] = (void *)0;
2189 args
[nb_args
++] = (void *)(int)(val
& 0xffffffff);
2194 args
[nb_args
++] = p
;
2195 while (*p
) /* goto end of cmdline */
2210 term_printf("%s: unsupported option -%c\n",
2217 if (nb_args
>= MAX_ARGS
)
2219 args
[nb_args
++] = (void *)has_option
;
2224 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2228 /* check that all arguments were parsed */
2232 term_printf("%s: extraneous characters at the end of line\n",
2240 CPUState
*env
=mon_get_cpu();
2242 kvm_save_registers(env
);
2251 cmd
->handler(args
[0]);
2254 cmd
->handler(args
[0], args
[1]);
2257 cmd
->handler(args
[0], args
[1], args
[2]);
2260 cmd
->handler(args
[0], args
[1], args
[2], args
[3]);
2263 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4]);
2266 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2269 cmd
->handler(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2272 term_printf("unsupported number of arguments: %d\n", nb_args
);
2276 for(i
= 0; i
< MAX_ARGS
; i
++)
2277 qemu_free(str_allocated
[i
]);
2281 static void cmd_completion(const char *name
, const char *list
)
2283 const char *p
, *pstart
;
2292 p
= pstart
+ strlen(pstart
);
2294 if (len
> sizeof(cmd
) - 2)
2295 len
= sizeof(cmd
) - 2;
2296 memcpy(cmd
, pstart
, len
);
2298 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2299 add_completion(cmd
);
2307 static void file_completion(const char *input
)
2312 char file
[1024], file_prefix
[1024];
2316 p
= strrchr(input
, '/');
2319 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2322 input_path_len
= p
- input
+ 1;
2323 memcpy(path
, input
, input_path_len
);
2324 if (input_path_len
> sizeof(path
) - 1)
2325 input_path_len
= sizeof(path
) - 1;
2326 path
[input_path_len
] = '\0';
2327 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2329 #ifdef DEBUG_COMPLETION
2330 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2332 ffs
= opendir(path
);
2340 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2341 memcpy(file
, input
, input_path_len
);
2342 strcpy(file
+ input_path_len
, d
->d_name
);
2343 /* stat the file to find out if it's a directory.
2344 * In that case add a slash to speed up typing long paths
2347 if(S_ISDIR(sb
.st_mode
))
2349 add_completion(file
);
2355 static void block_completion_it(void *opaque
, const char *name
)
2357 const char *input
= opaque
;
2359 if (input
[0] == '\0' ||
2360 !strncmp(name
, (char *)input
, strlen(input
))) {
2361 add_completion(name
);
2365 /* NOTE: this parser is an approximate form of the real command parser */
2366 static void parse_cmdline(const char *cmdline
,
2367 int *pnb_args
, char **args
)
2380 if (nb_args
>= MAX_ARGS
)
2382 ret
= get_str(buf
, sizeof(buf
), &p
);
2383 args
[nb_args
] = qemu_strdup(buf
);
2388 *pnb_args
= nb_args
;
2391 void readline_find_completion(const char *cmdline
)
2393 const char *cmdname
;
2394 char *args
[MAX_ARGS
];
2395 int nb_args
, i
, len
;
2396 const char *ptype
, *str
;
2400 parse_cmdline(cmdline
, &nb_args
, args
);
2401 #ifdef DEBUG_COMPLETION
2402 for(i
= 0; i
< nb_args
; i
++) {
2403 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2407 /* if the line ends with a space, it means we want to complete the
2409 len
= strlen(cmdline
);
2410 if (len
> 0 && isspace(cmdline
[len
- 1])) {
2411 if (nb_args
>= MAX_ARGS
)
2413 args
[nb_args
++] = qemu_strdup("");
2416 /* command completion */
2421 completion_index
= strlen(cmdname
);
2422 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2423 cmd_completion(cmdname
, cmd
->name
);
2426 /* find the command */
2427 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2428 if (compare_cmd(args
[0], cmd
->name
))
2433 ptype
= cmd
->args_type
;
2434 for(i
= 0; i
< nb_args
- 2; i
++) {
2435 if (*ptype
!= '\0') {
2437 while (*ptype
== '?')
2441 str
= args
[nb_args
- 1];
2444 /* file completion */
2445 completion_index
= strlen(str
);
2446 file_completion(str
);
2449 /* block device name completion */
2450 completion_index
= strlen(str
);
2451 bdrv_iterate(block_completion_it
, (void *)str
);
2455 /* XXX: more generic ? */
2456 if (!strcmp(cmd
->name
, "info")) {
2457 completion_index
= strlen(str
);
2458 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2459 cmd_completion(str
, cmd
->name
);
2461 } else if (!strcmp(cmd
->name
, "sendkey")) {
2462 completion_index
= strlen(str
);
2463 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2464 cmd_completion(str
, key
->name
);
2466 } else if (!strcmp(cmd
->name
, "migration")) {
2467 completion_index
= strlen(str
);
2468 for(cmd
= migration_cmds
; cmd
->name
!= NULL
; cmd
++) {
2469 cmd_completion(str
, cmd
->name
);
2477 for(i
= 0; i
< nb_args
; i
++)
2481 static int term_can_read(void *opaque
)
2486 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2489 for(i
= 0; i
< size
; i
++)
2490 readline_handle_byte(buf
[i
]);
2493 static void monitor_start_input(void);
2495 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2497 monitor_handle_command(term_cmds
, cmdline
);
2498 monitor_start_input();
2501 static void monitor_start_input(void)
2503 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2506 void monitor_init(CharDriverState
*hd
, int show_banner
)
2510 term_printf("QEMU %s monitor - type 'help' for more information\n",
2513 qemu_chr_add_read_handler(hd
, term_can_read
, term_read
, NULL
);
2514 monitor_start_input();
2517 /* XXX: use threads ? */
2518 /* modal monitor readline */
2519 static int monitor_readline_started
;
2520 static char *monitor_readline_buf
;
2521 static int monitor_readline_buf_size
;
2523 static void monitor_readline_cb(void *opaque
, const char *input
)
2525 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2526 monitor_readline_started
= 0;
2529 void monitor_readline(const char *prompt
, int is_password
,
2530 char *buf
, int buf_size
)
2533 qemu_chr_send_event(monitor_hd
, CHR_EVENT_FOCUS
);
2535 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2536 monitor_readline_buf
= buf
;
2537 monitor_readline_buf_size
= buf_size
;
2538 monitor_readline_started
= 1;
2539 while (monitor_readline_started
) {