4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "hw/pcmcia.h"
31 #include "qemu-char.h"
35 #include "audio/audio.h"
39 #include "qemu-timer.h"
40 #include "migration.h"
46 //#define DEBUG_COMPLETION
52 * 'B' block device name
53 * 's' string (accept optional quote)
55 * 'l' target long (32 or 64 bit)
56 * '/' optional gdb-like print format (like "/10x")
58 * '?' optional type (for 'F', 's' and 'i')
62 typedef struct term_cmd_t
{
64 const char *args_type
;
71 static CharDriverState
*monitor_hd
[MAX_MON
];
72 static int hide_banner
;
74 static const term_cmd_t term_cmds
[];
75 static const term_cmd_t info_cmds
[];
77 static uint8_t term_outbuf
[1024];
78 static int term_outbuf_index
;
80 static void monitor_start_input(void);
82 static CPUState
*mon_cpu
= NULL
;
87 if (term_outbuf_index
> 0) {
88 for (i
= 0; i
< MAX_MON
; i
++)
89 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
90 qemu_chr_write(monitor_hd
[i
], term_outbuf
, term_outbuf_index
);
91 term_outbuf_index
= 0;
95 /* flush at every end of line or if the buffer is full */
96 void term_puts(const char *str
)
104 term_outbuf
[term_outbuf_index
++] = '\r';
105 term_outbuf
[term_outbuf_index
++] = c
;
106 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
112 void term_vprintf(const char *fmt
, va_list ap
)
115 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
119 void term_printf(const char *fmt
, ...)
123 term_vprintf(fmt
, ap
);
127 void term_print_filename(const char *filename
)
131 for (i
= 0; filename
[i
]; i
++) {
132 switch (filename
[i
]) {
136 term_printf("\\%c", filename
[i
]);
148 term_printf("%c", filename
[i
]);
154 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
158 term_vprintf(fmt
, ap
);
163 static int compare_cmd(const char *name
, const char *list
)
165 const char *p
, *pstart
;
173 p
= pstart
+ strlen(pstart
);
174 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
183 static void help_cmd1(const term_cmd_t
*cmds
, const char *prefix
, const char *name
)
185 const term_cmd_t
*cmd
;
187 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
188 if (!name
|| !strcmp(name
, cmd
->name
))
189 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
193 static void help_cmd(const char *name
)
195 if (name
&& !strcmp(name
, "info")) {
196 help_cmd1(info_cmds
, "info ", NULL
);
198 help_cmd1(term_cmds
, "", name
);
199 if (name
&& !strcmp(name
, "log")) {
200 const CPULogItem
*item
;
201 term_printf("Log items (comma separated):\n");
202 term_printf("%-10s %s\n", "none", "remove all logs");
203 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
204 term_printf("%-10s %s\n", item
->name
, item
->help
);
210 static void do_help(const char *name
)
215 static void do_commit(const char *device
)
219 all_devices
= !strcmp(device
, "all");
220 for (i
= 0; i
< nb_drives
; i
++) {
222 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
223 bdrv_commit(drives_table
[i
].bdrv
);
227 static void do_info(const char *item
)
229 const term_cmd_t
*cmd
;
230 void (*handler
)(void);
234 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
235 if (compare_cmd(item
, cmd
->name
))
242 handler
= cmd
->handler
;
246 static void do_info_version(void)
248 term_printf("%s\n", QEMU_VERSION
);
251 static void do_info_name(void)
254 term_printf("%s\n", qemu_name
);
257 #if defined(TARGET_I386)
258 static void do_info_hpet(void)
260 term_printf("HPET is %s by QEMU\n", (no_hpet
) ? "disabled" : "enabled");
264 static void do_info_uuid(void)
266 term_printf(UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1], qemu_uuid
[2],
267 qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5], qemu_uuid
[6],
268 qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9], qemu_uuid
[10],
269 qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13], qemu_uuid
[14],
273 static void do_info_block(void)
278 static void do_info_blockstats(void)
283 /* get the current CPU defined by the user */
284 static int mon_set_cpu(int cpu_index
)
288 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
289 if (env
->cpu_index
== cpu_index
) {
297 static CPUState
*mon_get_cpu(void)
303 kvm_save_registers(mon_cpu
);
308 static void do_info_registers(void)
315 cpu_dump_state(env
, NULL
, monitor_fprintf
,
318 cpu_dump_state(env
, NULL
, monitor_fprintf
,
323 static void do_info_cpus(void)
327 /* just to set the default cpu if not already done */
330 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
331 kvm_save_registers(env
);
332 term_printf("%c CPU #%d:",
333 (env
== mon_cpu
) ? '*' : ' ',
335 #if defined(TARGET_I386)
336 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
337 #elif defined(TARGET_PPC)
338 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
339 #elif defined(TARGET_SPARC)
340 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
341 #elif defined(TARGET_MIPS)
342 term_printf(" PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
345 term_printf(" (halted)");
346 term_printf(" thread_id=%d", env
->thread_id
);
351 static void do_cpu_set(int index
)
353 if (mon_set_cpu(index
) < 0)
354 term_printf("Invalid CPU index\n");
357 static void do_cpu_set_nr(int value
, const char *status
)
361 if (!strcmp(status
, "online"))
363 else if (!strcmp(status
, "offline"))
366 term_printf("invalid status: %s\n", status
);
369 #if defined(TARGET_I386) || defined(TARGET_X86_64)
370 qemu_system_cpu_hot_add(value
, state
);
374 static void do_info_jit(void)
376 dump_exec_info(NULL
, monitor_fprintf
);
379 static void do_info_history (void)
386 str
= readline_get_history(i
);
389 term_printf("%d: '%s'\n", i
, str
);
394 #if defined(TARGET_PPC)
395 /* XXX: not implemented in other targets */
396 static void do_info_cpu_stats (void)
401 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
405 static void do_quit(void)
410 static int eject_device(BlockDriverState
*bs
, int force
)
412 if (bdrv_is_inserted(bs
)) {
414 if (!bdrv_is_removable(bs
)) {
415 term_printf("device is not removable\n");
418 if (bdrv_is_locked(bs
)) {
419 term_printf("device is locked\n");
428 static void do_eject(int force
, const char *filename
)
430 BlockDriverState
*bs
;
432 bs
= bdrv_find(filename
);
434 term_printf("device not found\n");
437 eject_device(bs
, force
);
440 static void do_change_block(const char *device
, const char *filename
, const char *fmt
)
442 BlockDriverState
*bs
;
443 BlockDriver
*drv
= NULL
;
445 bs
= bdrv_find(device
);
447 term_printf("device not found\n");
451 drv
= bdrv_find_format(fmt
);
453 term_printf("invalid format %s\n", fmt
);
457 if (eject_device(bs
, 0) < 0)
459 bdrv_open2(bs
, filename
, 0, drv
);
460 qemu_key_check(bs
, filename
);
463 static void do_change_vnc(const char *target
, const char *arg
)
465 if (strcmp(target
, "passwd") == 0 ||
466 strcmp(target
, "password") == 0) {
469 strncpy(password
, arg
, sizeof(password
));
470 password
[sizeof(password
) - 1] = '\0';
472 monitor_readline("Password: ", 1, password
, sizeof(password
));
473 if (vnc_display_password(NULL
, password
) < 0)
474 term_printf("could not set VNC server password\n");
476 if (vnc_display_open(NULL
, target
) < 0)
477 term_printf("could not start VNC server on %s\n", target
);
481 static void do_change(const char *device
, const char *target
, const char *arg
)
483 if (strcmp(device
, "vnc") == 0) {
484 do_change_vnc(target
, arg
);
486 do_change_block(device
, target
, arg
);
490 static void do_screen_dump(const char *filename
)
492 vga_hw_screen_dump(filename
);
495 static void do_logfile(const char *filename
)
497 cpu_set_log_filename(filename
);
500 static void do_log(const char *items
)
504 if (!strcmp(items
, "none")) {
507 mask
= cpu_str_to_log_mask(items
);
516 static void do_stop(void)
518 vm_stop(EXCP_INTERRUPT
);
521 static void do_cont(void)
526 #ifdef CONFIG_GDBSTUB
527 static void do_gdbserver(const char *port
)
530 port
= DEFAULT_GDBSTUB_PORT
;
531 if (gdbserver_start(port
) < 0) {
532 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
534 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
539 static void term_printc(int c
)
556 if (c
>= 32 && c
<= 126) {
557 term_printf("%c", c
);
559 term_printf("\\x%02x", c
);
566 static void memory_dump(int count
, int format
, int wsize
,
567 target_phys_addr_t addr
, int is_physical
)
570 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
578 if (!env
&& !is_physical
)
583 } else if (wsize
== 4) {
586 /* as default we use the current CS size */
590 if ((env
->efer
& MSR_EFER_LMA
) &&
591 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
595 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
600 monitor_disas(env
, addr
, count
, is_physical
, flags
);
609 nb_per_line
= line_size
/ wsize
;
614 max_digits
= (wsize
* 8 + 2) / 3;
618 max_digits
= (wsize
* 8) / 4;
622 max_digits
= (wsize
* 8 * 10 + 32) / 33;
631 term_printf(TARGET_FMT_plx
":", addr
);
633 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
638 cpu_physical_memory_rw(addr
, buf
, l
, 0);
643 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
644 term_printf(" Cannot access memory\n");
653 v
= ldub_raw(buf
+ i
);
656 v
= lduw_raw(buf
+ i
);
659 v
= (uint32_t)ldl_raw(buf
+ i
);
662 v
= ldq_raw(buf
+ i
);
668 term_printf("%#*" PRIo64
, max_digits
, v
);
671 term_printf("0x%0*" PRIx64
, max_digits
, v
);
674 term_printf("%*" PRIu64
, max_digits
, v
);
677 term_printf("%*" PRId64
, max_digits
, v
);
691 #if TARGET_LONG_BITS == 64
692 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
694 #define GET_TLONG(h, l) (l)
697 static void do_memory_dump(int count
, int format
, int size
,
698 uint32_t addrh
, uint32_t addrl
)
700 target_long addr
= GET_TLONG(addrh
, addrl
);
701 memory_dump(count
, format
, size
, addr
, 0);
704 #if TARGET_PHYS_ADDR_BITS > 32
705 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
707 #define GET_TPHYSADDR(h, l) (l)
710 static void do_physical_memory_dump(int count
, int format
, int size
,
711 uint32_t addrh
, uint32_t addrl
)
714 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
715 memory_dump(count
, format
, size
, addr
, 1);
718 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
720 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
721 #if TARGET_PHYS_ADDR_BITS == 32
724 term_printf("%#o", val
);
727 term_printf("%#x", val
);
730 term_printf("%u", val
);
734 term_printf("%d", val
);
743 term_printf("%#" PRIo64
, val
);
746 term_printf("%#" PRIx64
, val
);
749 term_printf("%" PRIu64
, val
);
753 term_printf("%" PRId64
, val
);
763 static void do_memory_save(unsigned int valh
, unsigned int vall
,
764 uint32_t size
, const char *filename
)
767 target_long addr
= GET_TLONG(valh
, vall
);
776 f
= fopen(filename
, "wb");
778 term_printf("could not open '%s'\n", filename
);
785 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
786 fwrite(buf
, 1, l
, f
);
793 static void do_physical_memory_save(unsigned int valh
, unsigned int vall
,
794 uint32_t size
, const char *filename
)
799 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
801 f
= fopen(filename
, "wb");
803 term_printf("could not open '%s'\n", filename
);
810 cpu_physical_memory_rw(addr
, buf
, l
, 0);
811 fwrite(buf
, 1, l
, f
);
819 static void do_sum(uint32_t start
, uint32_t size
)
826 for(addr
= start
; addr
< (start
+ size
); addr
++) {
827 cpu_physical_memory_rw(addr
, buf
, 1, 0);
828 /* BSD sum algorithm ('sum' Unix command) */
829 sum
= (sum
>> 1) | (sum
<< 15);
832 term_printf("%05d\n", sum
);
840 static const KeyDef key_defs
[] = {
867 { 0x0e, "backspace" },
904 { 0x37, "asterisk" },
907 { 0x3a, "caps_lock" },
918 { 0x45, "num_lock" },
919 { 0x46, "scroll_lock" },
921 { 0xb5, "kp_divide" },
922 { 0x37, "kp_multiply" },
923 { 0x4a, "kp_subtract" },
925 { 0x9c, "kp_enter" },
926 { 0x53, "kp_decimal" },
959 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
979 static int get_keycode(const char *key
)
985 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
986 if (!strcmp(key
, p
->name
))
989 if (strstart(key
, "0x", NULL
)) {
990 ret
= strtoul(key
, &endp
, 0);
991 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
997 #define MAX_KEYCODES 16
998 static uint8_t keycodes
[MAX_KEYCODES
];
999 static int nb_pending_keycodes
;
1000 static QEMUTimer
*key_timer
;
1002 static void release_keys(void *opaque
)
1006 while (nb_pending_keycodes
> 0) {
1007 nb_pending_keycodes
--;
1008 keycode
= keycodes
[nb_pending_keycodes
];
1010 kbd_put_keycode(0xe0);
1011 kbd_put_keycode(keycode
| 0x80);
1015 static void do_sendkey(const char *string
, int has_hold_time
, int hold_time
)
1017 char keyname_buf
[16];
1019 int keyname_len
, keycode
, i
;
1021 if (nb_pending_keycodes
> 0) {
1022 qemu_del_timer(key_timer
);
1029 separator
= strchr(string
, '-');
1030 keyname_len
= separator
? separator
- string
: strlen(string
);
1031 if (keyname_len
> 0) {
1032 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1033 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1034 term_printf("invalid key: '%s...'\n", keyname_buf
);
1037 if (i
== MAX_KEYCODES
) {
1038 term_printf("too many keys\n");
1041 keyname_buf
[keyname_len
] = 0;
1042 keycode
= get_keycode(keyname_buf
);
1044 term_printf("unknown key: '%s'\n", keyname_buf
);
1047 keycodes
[i
++] = keycode
;
1051 string
= separator
+ 1;
1053 nb_pending_keycodes
= i
;
1054 /* key down events */
1055 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1056 keycode
= keycodes
[i
];
1058 kbd_put_keycode(0xe0);
1059 kbd_put_keycode(keycode
& 0x7f);
1061 /* delayed key up events */
1062 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1063 muldiv64(ticks_per_sec
, hold_time
, 1000));
1066 static int mouse_button_state
;
1068 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
1072 dx
= strtol(dx_str
, NULL
, 0);
1073 dy
= strtol(dy_str
, NULL
, 0);
1076 dz
= strtol(dz_str
, NULL
, 0);
1077 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1080 static void do_mouse_button(int button_state
)
1082 mouse_button_state
= button_state
;
1083 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1086 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1092 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1100 val
= cpu_inb(NULL
, addr
);
1104 val
= cpu_inw(NULL
, addr
);
1108 val
= cpu_inl(NULL
, addr
);
1112 term_printf("port%c[0x%04x] = %#0*x\n",
1113 suffix
, addr
, size
* 2, val
);
1116 /* boot_set handler */
1117 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1118 static void *boot_opaque
;
1120 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1122 qemu_boot_set_handler
= func
;
1123 boot_opaque
= opaque
;
1126 static void do_boot_set(const char *bootdevice
)
1130 if (qemu_boot_set_handler
) {
1131 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1133 term_printf("boot device list now set to %s\n", bootdevice
);
1135 term_printf("setting boot device list failed with error %i\n", res
);
1137 term_printf("no function defined to set boot device list for this architecture\n");
1141 static void do_system_reset(void)
1143 qemu_system_reset_request();
1146 static void do_system_powerdown(void)
1148 qemu_system_powerdown_request();
1151 #if defined(TARGET_I386)
1152 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1154 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1157 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1158 pte
& PG_PSE_MASK
? 'P' : '-',
1159 pte
& PG_DIRTY_MASK
? 'D' : '-',
1160 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1161 pte
& PG_PCD_MASK
? 'C' : '-',
1162 pte
& PG_PWT_MASK
? 'T' : '-',
1163 pte
& PG_USER_MASK
? 'U' : '-',
1164 pte
& PG_RW_MASK
? 'W' : '-');
1167 static void tlb_info(void)
1171 uint32_t pgd
, pde
, pte
;
1173 env
= mon_get_cpu();
1177 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1178 term_printf("PG disabled\n");
1181 pgd
= env
->cr
[3] & ~0xfff;
1182 for(l1
= 0; l1
< 1024; l1
++) {
1183 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1184 pde
= le32_to_cpu(pde
);
1185 if (pde
& PG_PRESENT_MASK
) {
1186 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1187 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1189 for(l2
= 0; l2
< 1024; l2
++) {
1190 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1191 (uint8_t *)&pte
, 4);
1192 pte
= le32_to_cpu(pte
);
1193 if (pte
& PG_PRESENT_MASK
) {
1194 print_pte((l1
<< 22) + (l2
<< 12),
1204 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1205 uint32_t end
, int prot
)
1208 prot1
= *plast_prot
;
1209 if (prot
!= prot1
) {
1210 if (*pstart
!= -1) {
1211 term_printf("%08x-%08x %08x %c%c%c\n",
1212 *pstart
, end
, end
- *pstart
,
1213 prot1
& PG_USER_MASK
? 'u' : '-',
1215 prot1
& PG_RW_MASK
? 'w' : '-');
1225 static void mem_info(void)
1228 int l1
, l2
, prot
, last_prot
;
1229 uint32_t pgd
, pde
, pte
, start
, end
;
1231 env
= mon_get_cpu();
1235 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1236 term_printf("PG disabled\n");
1239 pgd
= env
->cr
[3] & ~0xfff;
1242 for(l1
= 0; l1
< 1024; l1
++) {
1243 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1244 pde
= le32_to_cpu(pde
);
1246 if (pde
& PG_PRESENT_MASK
) {
1247 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1248 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1249 mem_print(&start
, &last_prot
, end
, prot
);
1251 for(l2
= 0; l2
< 1024; l2
++) {
1252 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1253 (uint8_t *)&pte
, 4);
1254 pte
= le32_to_cpu(pte
);
1255 end
= (l1
<< 22) + (l2
<< 12);
1256 if (pte
& PG_PRESENT_MASK
) {
1257 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1261 mem_print(&start
, &last_prot
, end
, prot
);
1266 mem_print(&start
, &last_prot
, end
, prot
);
1272 static void do_info_kqemu(void)
1278 env
= mon_get_cpu();
1280 term_printf("No cpu initialized yet");
1283 val
= env
->kqemu_enabled
;
1284 term_printf("kqemu support: ");
1288 term_printf("disabled\n");
1291 term_printf("enabled for user code\n");
1294 term_printf("enabled for user and kernel code\n");
1298 term_printf("kqemu support: not compiled\n");
1302 static void do_info_kvm(void)
1304 #if defined(USE_KVM) || defined(CONFIG_KVM)
1305 term_printf("kvm support: ");
1307 term_printf("enabled\n");
1309 term_printf("disabled\n");
1311 term_printf("kvm support: not compiled\n");
1315 #ifdef CONFIG_PROFILER
1319 int64_t kqemu_exec_count
;
1321 int64_t kqemu_ret_int_count
;
1322 int64_t kqemu_ret_excp_count
;
1323 int64_t kqemu_ret_intr_count
;
1325 static void do_info_profile(void)
1331 term_printf("async time %" PRId64
" (%0.3f)\n",
1332 dev_time
, dev_time
/ (double)ticks_per_sec
);
1333 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1334 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1335 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1336 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1337 kqemu_time
/ (double)total
* 100.0,
1339 kqemu_ret_int_count
,
1340 kqemu_ret_excp_count
,
1341 kqemu_ret_intr_count
);
1344 kqemu_exec_count
= 0;
1346 kqemu_ret_int_count
= 0;
1347 kqemu_ret_excp_count
= 0;
1348 kqemu_ret_intr_count
= 0;
1350 kqemu_record_dump();
1354 static void do_info_profile(void)
1356 term_printf("Internal profiler not compiled\n");
1360 /* Capture support */
1361 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1363 static void do_info_capture (void)
1368 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1369 term_printf ("[%d]: ", i
);
1370 s
->ops
.info (s
->opaque
);
1374 static void do_stop_capture (int n
)
1379 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1381 s
->ops
.destroy (s
->opaque
);
1382 LIST_REMOVE (s
, entries
);
1390 static void do_wav_capture (const char *path
,
1391 int has_freq
, int freq
,
1392 int has_bits
, int bits
,
1393 int has_channels
, int nchannels
)
1397 s
= qemu_mallocz (sizeof (*s
));
1399 term_printf ("Not enough memory to add wave capture\n");
1403 freq
= has_freq
? freq
: 44100;
1404 bits
= has_bits
? bits
: 16;
1405 nchannels
= has_channels
? nchannels
: 2;
1407 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1408 term_printf ("Faied to add wave capture\n");
1411 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1415 #if defined(TARGET_I386)
1416 static void do_inject_nmi(int cpu_index
)
1420 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1421 if (env
->cpu_index
== cpu_index
) {
1423 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1425 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1431 static void do_info_status(void)
1434 term_printf("VM status: running\n");
1436 term_printf("VM status: paused\n");
1440 static void do_balloon(int value
)
1442 ram_addr_t target
= value
;
1443 qemu_balloon(target
<< 20);
1446 static void do_info_balloon(void)
1450 actual
= qemu_balloon_status();
1451 if (kvm_enabled() && !kvm_has_sync_mmu())
1452 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1453 else if (actual
== 0)
1454 term_printf("Ballooning not activated in VM\n");
1456 term_printf("balloon: actual=%d\n", (int)(actual
>> 20));
1459 static const term_cmd_t term_cmds
[] = {
1460 { "help|?", "s?", do_help
,
1461 "[cmd]", "show the help" },
1462 { "commit", "s", do_commit
,
1463 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1464 { "info", "s?", do_info
,
1465 "subcommand", "show various information about the system state" },
1466 { "q|quit", "", do_quit
,
1467 "", "quit the emulator" },
1468 { "eject", "-fB", do_eject
,
1469 "[-f] device", "eject a removable medium (use -f to force it)" },
1470 { "change", "BFs?", do_change
,
1471 "device filename [format]", "change a removable medium, optional format" },
1472 { "screendump", "F", do_screen_dump
,
1473 "filename", "save screen into PPM image 'filename'" },
1474 { "logfile", "F", do_logfile
,
1475 "filename", "output logs to 'filename'" },
1476 { "log", "s", do_log
,
1477 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1478 { "savevm", "s?", do_savevm
,
1479 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1480 { "loadvm", "s", do_loadvm
,
1481 "tag|id", "restore a VM snapshot from its tag or id" },
1482 { "delvm", "s", do_delvm
,
1483 "tag|id", "delete a VM snapshot from its tag or id" },
1484 { "stop", "", do_stop
,
1485 "", "stop emulation", },
1486 { "c|cont", "", do_cont
,
1487 "", "resume emulation", },
1488 #ifdef CONFIG_GDBSTUB
1489 { "gdbserver", "s?", do_gdbserver
,
1490 "[port]", "start gdbserver session (default port=1234)", },
1492 { "x", "/l", do_memory_dump
,
1493 "/fmt addr", "virtual memory dump starting at 'addr'", },
1494 { "xp", "/l", do_physical_memory_dump
,
1495 "/fmt addr", "physical memory dump starting at 'addr'", },
1496 { "p|print", "/l", do_print
,
1497 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1498 { "i", "/ii.", do_ioport_read
,
1499 "/fmt addr", "I/O port read" },
1501 { "sendkey", "si?", do_sendkey
,
1502 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1503 { "system_reset", "", do_system_reset
,
1504 "", "reset the system" },
1505 { "system_powerdown", "", do_system_powerdown
,
1506 "", "send system power down event" },
1507 { "sum", "ii", do_sum
,
1508 "addr size", "compute the checksum of a memory region" },
1509 { "usb_add", "s", do_usb_add
,
1510 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1511 { "usb_del", "s", do_usb_del
,
1512 "device", "remove USB device 'bus.addr'" },
1513 { "cpu", "i", do_cpu_set
,
1514 "index", "set the default CPU" },
1515 { "mouse_move", "sss?", do_mouse_move
,
1516 "dx dy [dz]", "send mouse move events" },
1517 { "mouse_button", "i", do_mouse_button
,
1518 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1519 { "mouse_set", "i", do_mouse_set
,
1520 "index", "set which mouse device receives events" },
1522 { "wavcapture", "si?i?i?", do_wav_capture
,
1523 "path [frequency bits channels]",
1524 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1526 { "stopcapture", "i", do_stop_capture
,
1527 "capture index", "stop capture" },
1528 { "memsave", "lis", do_memory_save
,
1529 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1530 { "pmemsave", "lis", do_physical_memory_save
,
1531 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1532 { "boot_set", "s", do_boot_set
,
1533 "bootdevice", "define new values for the boot device list" },
1534 #if defined(TARGET_I386)
1535 { "nmi", "i", do_inject_nmi
,
1536 "cpu", "inject an NMI on the given CPU", },
1538 { "migrate", "-ds", do_migrate
,
1539 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1540 { "migrate_cancel", "", do_migrate_cancel
,
1541 "", "cancel the current VM migration" },
1542 { "migrate_set_speed", "s", do_migrate_set_speed
,
1543 "value", "set maximum speed (in bytes) for migrations" },
1544 { "balloon", "i", do_balloon
,
1545 "target", "request VM to change it's memory allocation (in MB)" },
1546 { "cpu_set", "is", do_cpu_set_nr
, "cpu [online|offline]", "change cpu state" },
1547 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1548 { "drive_add", "iss", drive_hot_add
, "pcibus pcidevfn [file=file][,if=type][,bus=n]\n"
1549 "[,unit=m][,media=d][index=i]\n"
1550 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1551 "[snapshot=on|off][,cache=on|off]",
1552 "add drive to PCI storage controller" },
1553 { "pci_add", "iss", device_hot_add
, "bus nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1554 { "pci_del", "ii", device_hot_remove
, "bus slot-number", "hot remove PCI device" },
1559 static const term_cmd_t info_cmds
[] = {
1560 { "version", "", do_info_version
,
1561 "", "show the version of qemu" },
1562 { "network", "", do_info_network
,
1563 "", "show the network state" },
1564 { "chardev", "", qemu_chr_info
,
1565 "", "show the character devices" },
1566 { "block", "", do_info_block
,
1567 "", "show the block devices" },
1568 { "blockstats", "", do_info_blockstats
,
1569 "", "show block device statistics" },
1570 { "registers", "", do_info_registers
,
1571 "", "show the cpu registers" },
1572 { "cpus", "", do_info_cpus
,
1573 "", "show infos for each CPU" },
1574 { "history", "", do_info_history
,
1575 "", "show the command line history", },
1576 { "irq", "", irq_info
,
1577 "", "show the interrupts statistics (if available)", },
1578 { "pic", "", pic_info
,
1579 "", "show i8259 (PIC) state", },
1580 { "pci", "", pci_info
,
1581 "", "show PCI info", },
1582 #if defined(TARGET_I386)
1583 { "tlb", "", tlb_info
,
1584 "", "show virtual to physical memory mappings", },
1585 { "mem", "", mem_info
,
1586 "", "show the active virtual memory mappings", },
1587 { "hpet", "", do_info_hpet
,
1588 "", "show state of HPET", },
1590 { "jit", "", do_info_jit
,
1591 "", "show dynamic compiler info", },
1592 { "kqemu", "", do_info_kqemu
,
1593 "", "show kqemu information", },
1594 { "kvm", "", do_info_kvm
,
1595 "", "show kvm information", },
1596 { "usb", "", usb_info
,
1597 "", "show guest USB devices", },
1598 { "usbhost", "", usb_host_info
,
1599 "", "show host USB devices", },
1600 { "profile", "", do_info_profile
,
1601 "", "show profiling information", },
1602 { "capture", "", do_info_capture
,
1603 "", "show capture information" },
1604 { "snapshots", "", do_info_snapshots
,
1605 "", "show the currently saved VM snapshots" },
1606 { "status", "", do_info_status
,
1607 "", "show the current VM status (running|paused)" },
1608 { "pcmcia", "", pcmcia_info
,
1609 "", "show guest PCMCIA status" },
1610 { "mice", "", do_info_mice
,
1611 "", "show which guest mouse is receiving events" },
1612 { "vnc", "", do_info_vnc
,
1613 "", "show the vnc server status"},
1614 { "name", "", do_info_name
,
1615 "", "show the current VM name" },
1616 { "uuid", "", do_info_uuid
,
1617 "", "show the current VM UUID" },
1618 #if defined(TARGET_PPC)
1619 { "cpustats", "", do_info_cpu_stats
,
1620 "", "show CPU statistics", },
1622 #if defined(CONFIG_SLIRP)
1623 { "slirp", "", do_info_slirp
,
1624 "", "show SLIRP statistics", },
1626 { "migrate", "", do_info_migrate
, "", "show migration status" },
1627 { "balloon", "", do_info_balloon
,
1628 "", "show balloon information" },
1632 /*******************************************************************/
1634 static const char *pch
;
1635 static jmp_buf expr_env
;
1640 typedef struct MonitorDef
{
1643 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1647 #if defined(TARGET_I386)
1648 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1650 CPUState
*env
= mon_get_cpu();
1653 return env
->eip
+ env
->segs
[R_CS
].base
;
1657 #if defined(TARGET_PPC)
1658 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1660 CPUState
*env
= mon_get_cpu();
1668 for (i
= 0; i
< 8; i
++)
1669 u
|= env
->crf
[i
] << (32 - (4 * i
));
1674 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1676 CPUState
*env
= mon_get_cpu();
1682 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1684 CPUState
*env
= mon_get_cpu();
1690 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1692 CPUState
*env
= mon_get_cpu();
1695 return cpu_ppc_load_decr(env
);
1698 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1700 CPUState
*env
= mon_get_cpu();
1703 return cpu_ppc_load_tbu(env
);
1706 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1708 CPUState
*env
= mon_get_cpu();
1711 return cpu_ppc_load_tbl(env
);
1715 #if defined(TARGET_SPARC)
1716 #ifndef TARGET_SPARC64
1717 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1719 CPUState
*env
= mon_get_cpu();
1722 return GET_PSR(env
);
1726 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1728 CPUState
*env
= mon_get_cpu();
1731 return env
->regwptr
[val
];
1735 static const MonitorDef monitor_defs
[] = {
1738 #define SEG(name, seg) \
1739 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1740 { name ".base", offsetof(CPUState, segs[seg].base) },\
1741 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1743 { "eax", offsetof(CPUState
, regs
[0]) },
1744 { "ecx", offsetof(CPUState
, regs
[1]) },
1745 { "edx", offsetof(CPUState
, regs
[2]) },
1746 { "ebx", offsetof(CPUState
, regs
[3]) },
1747 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1748 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1749 { "esi", offsetof(CPUState
, regs
[6]) },
1750 { "edi", offsetof(CPUState
, regs
[7]) },
1751 #ifdef TARGET_X86_64
1752 { "r8", offsetof(CPUState
, regs
[8]) },
1753 { "r9", offsetof(CPUState
, regs
[9]) },
1754 { "r10", offsetof(CPUState
, regs
[10]) },
1755 { "r11", offsetof(CPUState
, regs
[11]) },
1756 { "r12", offsetof(CPUState
, regs
[12]) },
1757 { "r13", offsetof(CPUState
, regs
[13]) },
1758 { "r14", offsetof(CPUState
, regs
[14]) },
1759 { "r15", offsetof(CPUState
, regs
[15]) },
1761 { "eflags", offsetof(CPUState
, eflags
) },
1762 { "eip", offsetof(CPUState
, eip
) },
1769 { "pc", 0, monitor_get_pc
, },
1770 #elif defined(TARGET_PPC)
1771 /* General purpose registers */
1772 { "r0", offsetof(CPUState
, gpr
[0]) },
1773 { "r1", offsetof(CPUState
, gpr
[1]) },
1774 { "r2", offsetof(CPUState
, gpr
[2]) },
1775 { "r3", offsetof(CPUState
, gpr
[3]) },
1776 { "r4", offsetof(CPUState
, gpr
[4]) },
1777 { "r5", offsetof(CPUState
, gpr
[5]) },
1778 { "r6", offsetof(CPUState
, gpr
[6]) },
1779 { "r7", offsetof(CPUState
, gpr
[7]) },
1780 { "r8", offsetof(CPUState
, gpr
[8]) },
1781 { "r9", offsetof(CPUState
, gpr
[9]) },
1782 { "r10", offsetof(CPUState
, gpr
[10]) },
1783 { "r11", offsetof(CPUState
, gpr
[11]) },
1784 { "r12", offsetof(CPUState
, gpr
[12]) },
1785 { "r13", offsetof(CPUState
, gpr
[13]) },
1786 { "r14", offsetof(CPUState
, gpr
[14]) },
1787 { "r15", offsetof(CPUState
, gpr
[15]) },
1788 { "r16", offsetof(CPUState
, gpr
[16]) },
1789 { "r17", offsetof(CPUState
, gpr
[17]) },
1790 { "r18", offsetof(CPUState
, gpr
[18]) },
1791 { "r19", offsetof(CPUState
, gpr
[19]) },
1792 { "r20", offsetof(CPUState
, gpr
[20]) },
1793 { "r21", offsetof(CPUState
, gpr
[21]) },
1794 { "r22", offsetof(CPUState
, gpr
[22]) },
1795 { "r23", offsetof(CPUState
, gpr
[23]) },
1796 { "r24", offsetof(CPUState
, gpr
[24]) },
1797 { "r25", offsetof(CPUState
, gpr
[25]) },
1798 { "r26", offsetof(CPUState
, gpr
[26]) },
1799 { "r27", offsetof(CPUState
, gpr
[27]) },
1800 { "r28", offsetof(CPUState
, gpr
[28]) },
1801 { "r29", offsetof(CPUState
, gpr
[29]) },
1802 { "r30", offsetof(CPUState
, gpr
[30]) },
1803 { "r31", offsetof(CPUState
, gpr
[31]) },
1804 /* Floating point registers */
1805 { "f0", offsetof(CPUState
, fpr
[0]) },
1806 { "f1", offsetof(CPUState
, fpr
[1]) },
1807 { "f2", offsetof(CPUState
, fpr
[2]) },
1808 { "f3", offsetof(CPUState
, fpr
[3]) },
1809 { "f4", offsetof(CPUState
, fpr
[4]) },
1810 { "f5", offsetof(CPUState
, fpr
[5]) },
1811 { "f6", offsetof(CPUState
, fpr
[6]) },
1812 { "f7", offsetof(CPUState
, fpr
[7]) },
1813 { "f8", offsetof(CPUState
, fpr
[8]) },
1814 { "f9", offsetof(CPUState
, fpr
[9]) },
1815 { "f10", offsetof(CPUState
, fpr
[10]) },
1816 { "f11", offsetof(CPUState
, fpr
[11]) },
1817 { "f12", offsetof(CPUState
, fpr
[12]) },
1818 { "f13", offsetof(CPUState
, fpr
[13]) },
1819 { "f14", offsetof(CPUState
, fpr
[14]) },
1820 { "f15", offsetof(CPUState
, fpr
[15]) },
1821 { "f16", offsetof(CPUState
, fpr
[16]) },
1822 { "f17", offsetof(CPUState
, fpr
[17]) },
1823 { "f18", offsetof(CPUState
, fpr
[18]) },
1824 { "f19", offsetof(CPUState
, fpr
[19]) },
1825 { "f20", offsetof(CPUState
, fpr
[20]) },
1826 { "f21", offsetof(CPUState
, fpr
[21]) },
1827 { "f22", offsetof(CPUState
, fpr
[22]) },
1828 { "f23", offsetof(CPUState
, fpr
[23]) },
1829 { "f24", offsetof(CPUState
, fpr
[24]) },
1830 { "f25", offsetof(CPUState
, fpr
[25]) },
1831 { "f26", offsetof(CPUState
, fpr
[26]) },
1832 { "f27", offsetof(CPUState
, fpr
[27]) },
1833 { "f28", offsetof(CPUState
, fpr
[28]) },
1834 { "f29", offsetof(CPUState
, fpr
[29]) },
1835 { "f30", offsetof(CPUState
, fpr
[30]) },
1836 { "f31", offsetof(CPUState
, fpr
[31]) },
1837 { "fpscr", offsetof(CPUState
, fpscr
) },
1838 /* Next instruction pointer */
1839 { "nip|pc", offsetof(CPUState
, nip
) },
1840 { "lr", offsetof(CPUState
, lr
) },
1841 { "ctr", offsetof(CPUState
, ctr
) },
1842 { "decr", 0, &monitor_get_decr
, },
1843 { "ccr", 0, &monitor_get_ccr
, },
1844 /* Machine state register */
1845 { "msr", 0, &monitor_get_msr
, },
1846 { "xer", 0, &monitor_get_xer
, },
1847 { "tbu", 0, &monitor_get_tbu
, },
1848 { "tbl", 0, &monitor_get_tbl
, },
1849 #if defined(TARGET_PPC64)
1850 /* Address space register */
1851 { "asr", offsetof(CPUState
, asr
) },
1853 /* Segment registers */
1854 { "sdr1", offsetof(CPUState
, sdr1
) },
1855 { "sr0", offsetof(CPUState
, sr
[0]) },
1856 { "sr1", offsetof(CPUState
, sr
[1]) },
1857 { "sr2", offsetof(CPUState
, sr
[2]) },
1858 { "sr3", offsetof(CPUState
, sr
[3]) },
1859 { "sr4", offsetof(CPUState
, sr
[4]) },
1860 { "sr5", offsetof(CPUState
, sr
[5]) },
1861 { "sr6", offsetof(CPUState
, sr
[6]) },
1862 { "sr7", offsetof(CPUState
, sr
[7]) },
1863 { "sr8", offsetof(CPUState
, sr
[8]) },
1864 { "sr9", offsetof(CPUState
, sr
[9]) },
1865 { "sr10", offsetof(CPUState
, sr
[10]) },
1866 { "sr11", offsetof(CPUState
, sr
[11]) },
1867 { "sr12", offsetof(CPUState
, sr
[12]) },
1868 { "sr13", offsetof(CPUState
, sr
[13]) },
1869 { "sr14", offsetof(CPUState
, sr
[14]) },
1870 { "sr15", offsetof(CPUState
, sr
[15]) },
1871 /* Too lazy to put BATs and SPRs ... */
1872 #elif defined(TARGET_SPARC)
1873 { "g0", offsetof(CPUState
, gregs
[0]) },
1874 { "g1", offsetof(CPUState
, gregs
[1]) },
1875 { "g2", offsetof(CPUState
, gregs
[2]) },
1876 { "g3", offsetof(CPUState
, gregs
[3]) },
1877 { "g4", offsetof(CPUState
, gregs
[4]) },
1878 { "g5", offsetof(CPUState
, gregs
[5]) },
1879 { "g6", offsetof(CPUState
, gregs
[6]) },
1880 { "g7", offsetof(CPUState
, gregs
[7]) },
1881 { "o0", 0, monitor_get_reg
},
1882 { "o1", 1, monitor_get_reg
},
1883 { "o2", 2, monitor_get_reg
},
1884 { "o3", 3, monitor_get_reg
},
1885 { "o4", 4, monitor_get_reg
},
1886 { "o5", 5, monitor_get_reg
},
1887 { "o6", 6, monitor_get_reg
},
1888 { "o7", 7, monitor_get_reg
},
1889 { "l0", 8, monitor_get_reg
},
1890 { "l1", 9, monitor_get_reg
},
1891 { "l2", 10, monitor_get_reg
},
1892 { "l3", 11, monitor_get_reg
},
1893 { "l4", 12, monitor_get_reg
},
1894 { "l5", 13, monitor_get_reg
},
1895 { "l6", 14, monitor_get_reg
},
1896 { "l7", 15, monitor_get_reg
},
1897 { "i0", 16, monitor_get_reg
},
1898 { "i1", 17, monitor_get_reg
},
1899 { "i2", 18, monitor_get_reg
},
1900 { "i3", 19, monitor_get_reg
},
1901 { "i4", 20, monitor_get_reg
},
1902 { "i5", 21, monitor_get_reg
},
1903 { "i6", 22, monitor_get_reg
},
1904 { "i7", 23, monitor_get_reg
},
1905 { "pc", offsetof(CPUState
, pc
) },
1906 { "npc", offsetof(CPUState
, npc
) },
1907 { "y", offsetof(CPUState
, y
) },
1908 #ifndef TARGET_SPARC64
1909 { "psr", 0, &monitor_get_psr
, },
1910 { "wim", offsetof(CPUState
, wim
) },
1912 { "tbr", offsetof(CPUState
, tbr
) },
1913 { "fsr", offsetof(CPUState
, fsr
) },
1914 { "f0", offsetof(CPUState
, fpr
[0]) },
1915 { "f1", offsetof(CPUState
, fpr
[1]) },
1916 { "f2", offsetof(CPUState
, fpr
[2]) },
1917 { "f3", offsetof(CPUState
, fpr
[3]) },
1918 { "f4", offsetof(CPUState
, fpr
[4]) },
1919 { "f5", offsetof(CPUState
, fpr
[5]) },
1920 { "f6", offsetof(CPUState
, fpr
[6]) },
1921 { "f7", offsetof(CPUState
, fpr
[7]) },
1922 { "f8", offsetof(CPUState
, fpr
[8]) },
1923 { "f9", offsetof(CPUState
, fpr
[9]) },
1924 { "f10", offsetof(CPUState
, fpr
[10]) },
1925 { "f11", offsetof(CPUState
, fpr
[11]) },
1926 { "f12", offsetof(CPUState
, fpr
[12]) },
1927 { "f13", offsetof(CPUState
, fpr
[13]) },
1928 { "f14", offsetof(CPUState
, fpr
[14]) },
1929 { "f15", offsetof(CPUState
, fpr
[15]) },
1930 { "f16", offsetof(CPUState
, fpr
[16]) },
1931 { "f17", offsetof(CPUState
, fpr
[17]) },
1932 { "f18", offsetof(CPUState
, fpr
[18]) },
1933 { "f19", offsetof(CPUState
, fpr
[19]) },
1934 { "f20", offsetof(CPUState
, fpr
[20]) },
1935 { "f21", offsetof(CPUState
, fpr
[21]) },
1936 { "f22", offsetof(CPUState
, fpr
[22]) },
1937 { "f23", offsetof(CPUState
, fpr
[23]) },
1938 { "f24", offsetof(CPUState
, fpr
[24]) },
1939 { "f25", offsetof(CPUState
, fpr
[25]) },
1940 { "f26", offsetof(CPUState
, fpr
[26]) },
1941 { "f27", offsetof(CPUState
, fpr
[27]) },
1942 { "f28", offsetof(CPUState
, fpr
[28]) },
1943 { "f29", offsetof(CPUState
, fpr
[29]) },
1944 { "f30", offsetof(CPUState
, fpr
[30]) },
1945 { "f31", offsetof(CPUState
, fpr
[31]) },
1946 #ifdef TARGET_SPARC64
1947 { "f32", offsetof(CPUState
, fpr
[32]) },
1948 { "f34", offsetof(CPUState
, fpr
[34]) },
1949 { "f36", offsetof(CPUState
, fpr
[36]) },
1950 { "f38", offsetof(CPUState
, fpr
[38]) },
1951 { "f40", offsetof(CPUState
, fpr
[40]) },
1952 { "f42", offsetof(CPUState
, fpr
[42]) },
1953 { "f44", offsetof(CPUState
, fpr
[44]) },
1954 { "f46", offsetof(CPUState
, fpr
[46]) },
1955 { "f48", offsetof(CPUState
, fpr
[48]) },
1956 { "f50", offsetof(CPUState
, fpr
[50]) },
1957 { "f52", offsetof(CPUState
, fpr
[52]) },
1958 { "f54", offsetof(CPUState
, fpr
[54]) },
1959 { "f56", offsetof(CPUState
, fpr
[56]) },
1960 { "f58", offsetof(CPUState
, fpr
[58]) },
1961 { "f60", offsetof(CPUState
, fpr
[60]) },
1962 { "f62", offsetof(CPUState
, fpr
[62]) },
1963 { "asi", offsetof(CPUState
, asi
) },
1964 { "pstate", offsetof(CPUState
, pstate
) },
1965 { "cansave", offsetof(CPUState
, cansave
) },
1966 { "canrestore", offsetof(CPUState
, canrestore
) },
1967 { "otherwin", offsetof(CPUState
, otherwin
) },
1968 { "wstate", offsetof(CPUState
, wstate
) },
1969 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1970 { "fprs", offsetof(CPUState
, fprs
) },
1976 static void expr_error(const char *fmt
)
1980 longjmp(expr_env
, 1);
1983 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1984 static int get_monitor_def(target_long
*pval
, const char *name
)
1986 const MonitorDef
*md
;
1989 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1990 if (compare_cmd(name
, md
->name
)) {
1991 if (md
->get_value
) {
1992 *pval
= md
->get_value(md
, md
->offset
);
1994 CPUState
*env
= mon_get_cpu();
1997 ptr
= (uint8_t *)env
+ md
->offset
;
2000 *pval
= *(int32_t *)ptr
;
2003 *pval
= *(target_long
*)ptr
;
2016 static void next(void)
2020 while (qemu_isspace(*pch
))
2025 static int64_t expr_sum(void);
2027 static int64_t expr_unary(void)
2050 expr_error("')' expected");
2057 expr_error("character constant expected");
2061 expr_error("missing terminating \' character");
2071 while ((*pch
>= 'a' && *pch
<= 'z') ||
2072 (*pch
>= 'A' && *pch
<= 'Z') ||
2073 (*pch
>= '0' && *pch
<= '9') ||
2074 *pch
== '_' || *pch
== '.') {
2075 if ((q
- buf
) < sizeof(buf
) - 1)
2079 while (qemu_isspace(*pch
))
2082 ret
= get_monitor_def(®
, buf
);
2084 expr_error("unknown register");
2086 expr_error("no cpu defined");
2091 expr_error("unexpected end of expression");
2095 #if TARGET_PHYS_ADDR_BITS > 32
2096 n
= strtoull(pch
, &p
, 0);
2098 n
= strtoul(pch
, &p
, 0);
2101 expr_error("invalid char in expression");
2104 while (qemu_isspace(*pch
))
2112 static int64_t expr_prod(void)
2120 if (op
!= '*' && op
!= '/' && op
!= '%')
2123 val2
= expr_unary();
2132 expr_error("division by zero");
2143 static int64_t expr_logic(void)
2151 if (op
!= '&' && op
!= '|' && op
!= '^')
2171 static int64_t expr_sum(void)
2179 if (op
!= '+' && op
!= '-')
2182 val2
= expr_logic();
2191 static int get_expr(int64_t *pval
, const char **pp
)
2194 if (setjmp(expr_env
)) {
2198 while (qemu_isspace(*pch
))
2205 static int get_str(char *buf
, int buf_size
, const char **pp
)
2213 while (qemu_isspace(*p
))
2223 while (*p
!= '\0' && *p
!= '\"') {
2239 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2242 if ((q
- buf
) < buf_size
- 1) {
2246 if ((q
- buf
) < buf_size
- 1) {
2253 qemu_printf("unterminated string\n");
2258 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2259 if ((q
- buf
) < buf_size
- 1) {
2270 static int default_fmt_format
= 'x';
2271 static int default_fmt_size
= 4;
2275 static void monitor_handle_command(const char *cmdline
)
2277 const char *p
, *pstart
, *typestr
;
2279 int c
, nb_args
, len
, i
, has_arg
;
2280 const term_cmd_t
*cmd
;
2283 void *str_allocated
[MAX_ARGS
];
2284 void *args
[MAX_ARGS
];
2285 void (*handler_0
)(void);
2286 void (*handler_1
)(void *arg0
);
2287 void (*handler_2
)(void *arg0
, void *arg1
);
2288 void (*handler_3
)(void *arg0
, void *arg1
, void *arg2
);
2289 void (*handler_4
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
);
2290 void (*handler_5
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2292 void (*handler_6
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2293 void *arg4
, void *arg5
);
2294 void (*handler_7
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2295 void *arg4
, void *arg5
, void *arg6
);
2298 term_printf("command='%s'\n", cmdline
);
2301 /* extract the command name */
2304 while (qemu_isspace(*p
))
2309 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2312 if (len
> sizeof(cmdname
) - 1)
2313 len
= sizeof(cmdname
) - 1;
2314 memcpy(cmdname
, pstart
, len
);
2315 cmdname
[len
] = '\0';
2317 /* find the command */
2318 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2319 if (compare_cmd(cmdname
, cmd
->name
))
2322 term_printf("unknown command: '%s'\n", cmdname
);
2326 for(i
= 0; i
< MAX_ARGS
; i
++)
2327 str_allocated
[i
] = NULL
;
2329 /* parse the parameters */
2330 typestr
= cmd
->args_type
;
2345 while (qemu_isspace(*p
))
2347 if (*typestr
== '?') {
2350 /* no optional string: NULL argument */
2355 ret
= get_str(buf
, sizeof(buf
), &p
);
2359 term_printf("%s: filename expected\n", cmdname
);
2362 term_printf("%s: block device name expected\n", cmdname
);
2365 term_printf("%s: string expected\n", cmdname
);
2370 str
= qemu_malloc(strlen(buf
) + 1);
2371 pstrcpy(str
, sizeof(buf
), buf
);
2372 str_allocated
[nb_args
] = str
;
2374 if (nb_args
>= MAX_ARGS
) {
2376 term_printf("%s: too many arguments\n", cmdname
);
2379 args
[nb_args
++] = str
;
2384 int count
, format
, size
;
2386 while (qemu_isspace(*p
))
2392 if (qemu_isdigit(*p
)) {
2394 while (qemu_isdigit(*p
)) {
2395 count
= count
* 10 + (*p
- '0');
2433 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2434 term_printf("invalid char in format: '%c'\n", *p
);
2438 format
= default_fmt_format
;
2439 if (format
!= 'i') {
2440 /* for 'i', not specifying a size gives -1 as size */
2442 size
= default_fmt_size
;
2443 default_fmt_size
= size
;
2445 default_fmt_format
= format
;
2448 format
= default_fmt_format
;
2449 if (format
!= 'i') {
2450 size
= default_fmt_size
;
2455 if (nb_args
+ 3 > MAX_ARGS
)
2457 args
[nb_args
++] = (void*)(long)count
;
2458 args
[nb_args
++] = (void*)(long)format
;
2459 args
[nb_args
++] = (void*)(long)size
;
2467 while (qemu_isspace(*p
))
2469 if (*typestr
== '?' || *typestr
== '.') {
2470 if (*typestr
== '?') {
2478 while (qemu_isspace(*p
))
2486 if (nb_args
>= MAX_ARGS
)
2488 args
[nb_args
++] = (void *)(long)has_arg
;
2490 if (nb_args
>= MAX_ARGS
)
2496 if (get_expr(&val
, &p
))
2500 if (nb_args
>= MAX_ARGS
)
2502 args
[nb_args
++] = (void *)(long)val
;
2504 if ((nb_args
+ 1) >= MAX_ARGS
)
2506 #if TARGET_PHYS_ADDR_BITS > 32
2507 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2509 args
[nb_args
++] = (void *)0;
2511 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2523 while (qemu_isspace(*p
))
2529 term_printf("%s: unsupported option -%c\n",
2536 if (nb_args
>= MAX_ARGS
)
2538 args
[nb_args
++] = (void *)(long)has_option
;
2543 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2547 /* check that all arguments were parsed */
2548 while (qemu_isspace(*p
))
2551 term_printf("%s: extraneous characters at the end of line\n",
2558 handler_0
= cmd
->handler
;
2562 handler_1
= cmd
->handler
;
2566 handler_2
= cmd
->handler
;
2567 handler_2(args
[0], args
[1]);
2570 handler_3
= cmd
->handler
;
2571 handler_3(args
[0], args
[1], args
[2]);
2574 handler_4
= cmd
->handler
;
2575 handler_4(args
[0], args
[1], args
[2], args
[3]);
2578 handler_5
= cmd
->handler
;
2579 handler_5(args
[0], args
[1], args
[2], args
[3], args
[4]);
2582 handler_6
= cmd
->handler
;
2583 handler_6(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2586 handler_7
= cmd
->handler
;
2587 handler_7(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2590 term_printf("unsupported number of arguments: %d\n", nb_args
);
2594 for(i
= 0; i
< MAX_ARGS
; i
++)
2595 qemu_free(str_allocated
[i
]);
2599 static void cmd_completion(const char *name
, const char *list
)
2601 const char *p
, *pstart
;
2610 p
= pstart
+ strlen(pstart
);
2612 if (len
> sizeof(cmd
) - 2)
2613 len
= sizeof(cmd
) - 2;
2614 memcpy(cmd
, pstart
, len
);
2616 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2617 add_completion(cmd
);
2625 static void file_completion(const char *input
)
2630 char file
[1024], file_prefix
[1024];
2634 p
= strrchr(input
, '/');
2637 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2638 pstrcpy(path
, sizeof(path
), ".");
2640 input_path_len
= p
- input
+ 1;
2641 memcpy(path
, input
, input_path_len
);
2642 if (input_path_len
> sizeof(path
) - 1)
2643 input_path_len
= sizeof(path
) - 1;
2644 path
[input_path_len
] = '\0';
2645 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2647 #ifdef DEBUG_COMPLETION
2648 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2650 ffs
= opendir(path
);
2658 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2659 memcpy(file
, input
, input_path_len
);
2660 if (input_path_len
< sizeof(file
))
2661 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2663 /* stat the file to find out if it's a directory.
2664 * In that case add a slash to speed up typing long paths
2667 if(S_ISDIR(sb
.st_mode
))
2668 pstrcat(file
, sizeof(file
), "/");
2669 add_completion(file
);
2675 static void block_completion_it(void *opaque
, const char *name
)
2677 const char *input
= opaque
;
2679 if (input
[0] == '\0' ||
2680 !strncmp(name
, (char *)input
, strlen(input
))) {
2681 add_completion(name
);
2685 /* NOTE: this parser is an approximate form of the real command parser */
2686 static void parse_cmdline(const char *cmdline
,
2687 int *pnb_args
, char **args
)
2696 while (qemu_isspace(*p
))
2700 if (nb_args
>= MAX_ARGS
)
2702 ret
= get_str(buf
, sizeof(buf
), &p
);
2703 args
[nb_args
] = qemu_strdup(buf
);
2708 *pnb_args
= nb_args
;
2711 void readline_find_completion(const char *cmdline
)
2713 const char *cmdname
;
2714 char *args
[MAX_ARGS
];
2715 int nb_args
, i
, len
;
2716 const char *ptype
, *str
;
2717 const term_cmd_t
*cmd
;
2720 parse_cmdline(cmdline
, &nb_args
, args
);
2721 #ifdef DEBUG_COMPLETION
2722 for(i
= 0; i
< nb_args
; i
++) {
2723 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2727 /* if the line ends with a space, it means we want to complete the
2729 len
= strlen(cmdline
);
2730 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2731 if (nb_args
>= MAX_ARGS
)
2733 args
[nb_args
++] = qemu_strdup("");
2736 /* command completion */
2741 completion_index
= strlen(cmdname
);
2742 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2743 cmd_completion(cmdname
, cmd
->name
);
2746 /* find the command */
2747 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2748 if (compare_cmd(args
[0], cmd
->name
))
2753 ptype
= cmd
->args_type
;
2754 for(i
= 0; i
< nb_args
- 2; i
++) {
2755 if (*ptype
!= '\0') {
2757 while (*ptype
== '?')
2761 str
= args
[nb_args
- 1];
2764 /* file completion */
2765 completion_index
= strlen(str
);
2766 file_completion(str
);
2769 /* block device name completion */
2770 completion_index
= strlen(str
);
2771 bdrv_iterate(block_completion_it
, (void *)str
);
2774 /* XXX: more generic ? */
2775 if (!strcmp(cmd
->name
, "info")) {
2776 completion_index
= strlen(str
);
2777 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2778 cmd_completion(str
, cmd
->name
);
2780 } else if (!strcmp(cmd
->name
, "sendkey")) {
2781 completion_index
= strlen(str
);
2782 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2783 cmd_completion(str
, key
->name
);
2791 for(i
= 0; i
< nb_args
; i
++)
2795 static int term_can_read(void *opaque
)
2800 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2803 for(i
= 0; i
< size
; i
++)
2804 readline_handle_byte(buf
[i
]);
2807 static int monitor_suspended
;
2809 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2811 monitor_handle_command(cmdline
);
2812 if (!monitor_suspended
)
2813 monitor_start_input();
2815 monitor_suspended
= 2;
2818 void monitor_suspend(void)
2820 monitor_suspended
= 1;
2823 void monitor_resume(void)
2825 if (monitor_suspended
== 2)
2826 monitor_start_input();
2827 monitor_suspended
= 0;
2830 static void monitor_start_input(void)
2832 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2835 static void term_event(void *opaque
, int event
)
2837 if (event
!= CHR_EVENT_RESET
)
2841 term_printf("QEMU %s monitor - type 'help' for more information\n",
2843 monitor_start_input();
2846 static int is_first_init
= 1;
2848 void monitor_init(CharDriverState
*hd
, int show_banner
)
2852 if (is_first_init
) {
2853 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
2856 for (i
= 0; i
< MAX_MON
; i
++) {
2857 monitor_hd
[i
] = NULL
;
2861 for (i
= 0; i
< MAX_MON
; i
++) {
2862 if (monitor_hd
[i
] == NULL
) {
2868 hide_banner
= !show_banner
;
2870 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2872 readline_start("", 0, monitor_handle_command1
, NULL
);
2875 /* XXX: use threads ? */
2876 /* modal monitor readline */
2877 static int monitor_readline_started
;
2878 static char *monitor_readline_buf
;
2879 static int monitor_readline_buf_size
;
2881 static void monitor_readline_cb(void *opaque
, const char *input
)
2883 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2884 monitor_readline_started
= 0;
2887 void monitor_readline(const char *prompt
, int is_password
,
2888 char *buf
, int buf_size
)
2891 int old_focus
[MAX_MON
];
2894 for (i
= 0; i
< MAX_MON
; i
++) {
2896 if (monitor_hd
[i
]) {
2897 old_focus
[i
] = monitor_hd
[i
]->focus
;
2898 monitor_hd
[i
]->focus
= 0;
2899 qemu_chr_send_event(monitor_hd
[i
], CHR_EVENT_FOCUS
);
2904 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2905 monitor_readline_buf
= buf
;
2906 monitor_readline_buf_size
= buf_size
;
2907 monitor_readline_started
= 1;
2908 while (monitor_readline_started
) {
2911 /* restore original focus */
2913 for (i
= 0; i
< MAX_MON
; i
++)
2915 monitor_hd
[i
]->focus
= old_focus
[i
];