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"
44 //#define DEBUG_COMPLETION
50 * 'B' block device name
51 * 's' string (accept optional quote)
53 * 'l' target long (32 or 64 bit)
54 * '/' optional gdb-like print format (like "/10x")
56 * '?' optional type (for 'F', 's' and 'i')
60 typedef struct term_cmd_t
{
62 const char *args_type
;
69 static CharDriverState
*monitor_hd
[MAX_MON
];
70 static int hide_banner
;
72 static const term_cmd_t term_cmds
[];
73 static const term_cmd_t info_cmds
[];
75 static uint8_t term_outbuf
[1024];
76 static int term_outbuf_index
;
78 static void monitor_start_input(void);
80 static CPUState
*mon_cpu
= NULL
;
85 if (term_outbuf_index
> 0) {
86 for (i
= 0; i
< MAX_MON
; i
++)
87 if (monitor_hd
[i
] && monitor_hd
[i
]->focus
== 0)
88 qemu_chr_write(monitor_hd
[i
], term_outbuf
, term_outbuf_index
);
89 term_outbuf_index
= 0;
93 /* flush at every end of line or if the buffer is full */
94 void term_puts(const char *str
)
102 term_outbuf
[term_outbuf_index
++] = '\r';
103 term_outbuf
[term_outbuf_index
++] = c
;
104 if (term_outbuf_index
>= (sizeof(term_outbuf
) - 1) ||
110 void term_vprintf(const char *fmt
, va_list ap
)
113 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
117 void term_printf(const char *fmt
, ...)
121 term_vprintf(fmt
, ap
);
125 void term_print_filename(const char *filename
)
129 for (i
= 0; filename
[i
]; i
++) {
130 switch (filename
[i
]) {
134 term_printf("\\%c", filename
[i
]);
146 term_printf("%c", filename
[i
]);
152 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
156 term_vprintf(fmt
, ap
);
161 static int compare_cmd(const char *name
, const char *list
)
163 const char *p
, *pstart
;
171 p
= pstart
+ strlen(pstart
);
172 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
181 static void help_cmd1(const term_cmd_t
*cmds
, const char *prefix
, const char *name
)
183 const term_cmd_t
*cmd
;
185 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
186 if (!name
|| !strcmp(name
, cmd
->name
))
187 term_printf("%s%s %s -- %s\n", prefix
, cmd
->name
, cmd
->params
, cmd
->help
);
191 static void help_cmd(const char *name
)
193 if (name
&& !strcmp(name
, "info")) {
194 help_cmd1(info_cmds
, "info ", NULL
);
196 help_cmd1(term_cmds
, "", name
);
197 if (name
&& !strcmp(name
, "log")) {
198 const CPULogItem
*item
;
199 term_printf("Log items (comma separated):\n");
200 term_printf("%-10s %s\n", "none", "remove all logs");
201 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
202 term_printf("%-10s %s\n", item
->name
, item
->help
);
208 static void do_help(const char *name
)
213 static void do_commit(const char *device
)
217 all_devices
= !strcmp(device
, "all");
218 for (i
= 0; i
< nb_drives
; i
++) {
220 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
221 bdrv_commit(drives_table
[i
].bdrv
);
225 static void do_info(const char *item
)
227 const term_cmd_t
*cmd
;
228 void (*handler
)(void);
232 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
233 if (compare_cmd(item
, cmd
->name
))
240 handler
= cmd
->handler
;
244 static void do_info_version(void)
246 term_printf("%s\n", QEMU_VERSION
);
249 static void do_info_name(void)
252 term_printf("%s\n", qemu_name
);
255 static void do_info_hpet(void)
257 term_printf("HPET is %s by QEMU\n", (no_hpet
) ? "disabled" : "enabled");
260 static void do_info_uuid(void)
262 term_printf(UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1], qemu_uuid
[2],
263 qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5], qemu_uuid
[6],
264 qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9], qemu_uuid
[10],
265 qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13], qemu_uuid
[14],
269 static void do_info_block(void)
274 static void do_info_blockstats(void)
279 /* get the current CPU defined by the user */
280 static int mon_set_cpu(int cpu_index
)
284 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
285 if (env
->cpu_index
== cpu_index
) {
293 static CPUState
*mon_get_cpu(void)
301 static void do_info_registers(void)
308 cpu_dump_state(env
, NULL
, monitor_fprintf
,
311 cpu_dump_state(env
, NULL
, monitor_fprintf
,
316 static void do_info_cpus(void)
320 /* just to set the default cpu if not already done */
323 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
324 term_printf("%c CPU #%d:",
325 (env
== mon_cpu
) ? '*' : ' ',
327 #if defined(TARGET_I386)
328 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
329 #elif defined(TARGET_PPC)
330 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
331 #elif defined(TARGET_SPARC)
332 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
333 #elif defined(TARGET_MIPS)
334 term_printf(" PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
337 term_printf(" (halted)");
342 static void do_cpu_set(int index
)
344 if (mon_set_cpu(index
) < 0)
345 term_printf("Invalid CPU index\n");
348 static void do_info_jit(void)
350 dump_exec_info(NULL
, monitor_fprintf
);
353 static void do_info_history (void)
360 str
= readline_get_history(i
);
363 term_printf("%d: '%s'\n", i
, str
);
368 #if defined(TARGET_PPC)
369 /* XXX: not implemented in other targets */
370 static void do_info_cpu_stats (void)
375 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
379 static void do_quit(void)
384 static int eject_device(BlockDriverState
*bs
, int force
)
386 if (bdrv_is_inserted(bs
)) {
388 if (!bdrv_is_removable(bs
)) {
389 term_printf("device is not removable\n");
392 if (bdrv_is_locked(bs
)) {
393 term_printf("device is locked\n");
402 static void do_eject(int force
, const char *filename
)
404 BlockDriverState
*bs
;
406 bs
= bdrv_find(filename
);
408 term_printf("device not found\n");
411 eject_device(bs
, force
);
414 static void do_change_block(const char *device
, const char *filename
, const char *fmt
)
416 BlockDriverState
*bs
;
417 BlockDriver
*drv
= NULL
;
419 bs
= bdrv_find(device
);
421 term_printf("device not found\n");
425 drv
= bdrv_find_format(fmt
);
427 term_printf("invalid format %s\n", fmt
);
431 if (eject_device(bs
, 0) < 0)
433 bdrv_open2(bs
, filename
, 0, drv
);
434 qemu_key_check(bs
, filename
);
437 static void do_change_vnc(const char *target
, const char *arg
)
439 if (strcmp(target
, "passwd") == 0 ||
440 strcmp(target
, "password") == 0) {
443 strncpy(password
, arg
, sizeof(password
));
444 password
[sizeof(password
) - 1] = '\0';
446 monitor_readline("Password: ", 1, password
, sizeof(password
));
447 if (vnc_display_password(NULL
, password
) < 0)
448 term_printf("could not set VNC server password\n");
450 if (vnc_display_open(NULL
, target
) < 0)
451 term_printf("could not start VNC server on %s\n", target
);
455 static void do_change(const char *device
, const char *target
, const char *arg
)
457 if (strcmp(device
, "vnc") == 0) {
458 do_change_vnc(target
, arg
);
460 do_change_block(device
, target
, arg
);
464 static void do_screen_dump(const char *filename
)
466 vga_hw_screen_dump(filename
);
469 static void do_logfile(const char *filename
)
471 cpu_set_log_filename(filename
);
474 static void do_log(const char *items
)
478 if (!strcmp(items
, "none")) {
481 mask
= cpu_str_to_log_mask(items
);
490 static void do_stop(void)
492 vm_stop(EXCP_INTERRUPT
);
495 static void do_cont(void)
500 #ifdef CONFIG_GDBSTUB
501 static void do_gdbserver(const char *port
)
504 port
= DEFAULT_GDBSTUB_PORT
;
505 if (gdbserver_start(port
) < 0) {
506 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
508 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
513 static void term_printc(int c
)
530 if (c
>= 32 && c
<= 126) {
531 term_printf("%c", c
);
533 term_printf("\\x%02x", c
);
540 static void memory_dump(int count
, int format
, int wsize
,
541 target_phys_addr_t addr
, int is_physical
)
544 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
552 if (!env
&& !is_physical
)
557 } else if (wsize
== 4) {
560 /* as default we use the current CS size */
564 if ((env
->efer
& MSR_EFER_LMA
) &&
565 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
569 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
574 monitor_disas(env
, addr
, count
, is_physical
, flags
);
583 nb_per_line
= line_size
/ wsize
;
588 max_digits
= (wsize
* 8 + 2) / 3;
592 max_digits
= (wsize
* 8) / 4;
596 max_digits
= (wsize
* 8 * 10 + 32) / 33;
605 term_printf(TARGET_FMT_plx
":", addr
);
607 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
612 cpu_physical_memory_rw(addr
, buf
, l
, 0);
617 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
618 term_printf(" Cannot access memory\n");
627 v
= ldub_raw(buf
+ i
);
630 v
= lduw_raw(buf
+ i
);
633 v
= (uint32_t)ldl_raw(buf
+ i
);
636 v
= ldq_raw(buf
+ i
);
642 term_printf("%#*" PRIo64
, max_digits
, v
);
645 term_printf("0x%0*" PRIx64
, max_digits
, v
);
648 term_printf("%*" PRIu64
, max_digits
, v
);
651 term_printf("%*" PRId64
, max_digits
, v
);
665 #if TARGET_LONG_BITS == 64
666 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
668 #define GET_TLONG(h, l) (l)
671 static void do_memory_dump(int count
, int format
, int size
,
672 uint32_t addrh
, uint32_t addrl
)
674 target_long addr
= GET_TLONG(addrh
, addrl
);
675 memory_dump(count
, format
, size
, addr
, 0);
678 #if TARGET_PHYS_ADDR_BITS > 32
679 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
681 #define GET_TPHYSADDR(h, l) (l)
684 static void do_physical_memory_dump(int count
, int format
, int size
,
685 uint32_t addrh
, uint32_t addrl
)
688 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
689 memory_dump(count
, format
, size
, addr
, 1);
692 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
694 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
695 #if TARGET_PHYS_ADDR_BITS == 32
698 term_printf("%#o", val
);
701 term_printf("%#x", val
);
704 term_printf("%u", val
);
708 term_printf("%d", val
);
717 term_printf("%#" PRIo64
, val
);
720 term_printf("%#" PRIx64
, val
);
723 term_printf("%" PRIu64
, val
);
727 term_printf("%" PRId64
, val
);
737 static void do_memory_save(unsigned int valh
, unsigned int vall
,
738 uint32_t size
, const char *filename
)
741 target_long addr
= GET_TLONG(valh
, vall
);
750 f
= fopen(filename
, "wb");
752 term_printf("could not open '%s'\n", filename
);
759 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
760 fwrite(buf
, 1, l
, f
);
767 static void do_physical_memory_save(unsigned int valh
, unsigned int vall
,
768 uint32_t size
, const char *filename
)
773 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
775 f
= fopen(filename
, "wb");
777 term_printf("could not open '%s'\n", filename
);
784 cpu_physical_memory_rw(addr
, buf
, l
, 0);
785 fwrite(buf
, 1, l
, f
);
793 static void do_sum(uint32_t start
, uint32_t size
)
800 for(addr
= start
; addr
< (start
+ size
); addr
++) {
801 cpu_physical_memory_rw(addr
, buf
, 1, 0);
802 /* BSD sum algorithm ('sum' Unix command) */
803 sum
= (sum
>> 1) | (sum
<< 15);
806 term_printf("%05d\n", sum
);
814 static const KeyDef key_defs
[] = {
841 { 0x0e, "backspace" },
878 { 0x37, "asterisk" },
881 { 0x3a, "caps_lock" },
892 { 0x45, "num_lock" },
893 { 0x46, "scroll_lock" },
895 { 0xb5, "kp_divide" },
896 { 0x37, "kp_multiply" },
897 { 0x4a, "kp_subtract" },
899 { 0x9c, "kp_enter" },
900 { 0x53, "kp_decimal" },
933 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
953 static int get_keycode(const char *key
)
959 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
960 if (!strcmp(key
, p
->name
))
963 if (strstart(key
, "0x", NULL
)) {
964 ret
= strtoul(key
, &endp
, 0);
965 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
971 #define MAX_KEYCODES 16
972 static uint8_t keycodes
[MAX_KEYCODES
];
973 static int nb_pending_keycodes
;
974 static QEMUTimer
*key_timer
;
976 static void release_keys(void *opaque
)
980 while (nb_pending_keycodes
> 0) {
981 nb_pending_keycodes
--;
982 keycode
= keycodes
[nb_pending_keycodes
];
984 kbd_put_keycode(0xe0);
985 kbd_put_keycode(keycode
| 0x80);
989 static void do_sendkey(const char *string
, int has_hold_time
, int hold_time
)
991 char keyname_buf
[16];
993 int keyname_len
, keycode
, i
;
995 if (nb_pending_keycodes
> 0) {
996 qemu_del_timer(key_timer
);
1003 separator
= strchr(string
, '-');
1004 keyname_len
= separator
? separator
- string
: strlen(string
);
1005 if (keyname_len
> 0) {
1006 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1007 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1008 term_printf("invalid key: '%s...'\n", keyname_buf
);
1011 if (i
== MAX_KEYCODES
) {
1012 term_printf("too many keys\n");
1015 keyname_buf
[keyname_len
] = 0;
1016 keycode
= get_keycode(keyname_buf
);
1018 term_printf("unknown key: '%s'\n", keyname_buf
);
1021 keycodes
[i
++] = keycode
;
1025 string
= separator
+ 1;
1027 nb_pending_keycodes
= i
;
1028 /* key down events */
1029 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1030 keycode
= keycodes
[i
];
1032 kbd_put_keycode(0xe0);
1033 kbd_put_keycode(keycode
& 0x7f);
1035 /* delayed key up events */
1036 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1037 muldiv64(ticks_per_sec
, hold_time
, 1000));
1040 static int mouse_button_state
;
1042 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
1046 dx
= strtol(dx_str
, NULL
, 0);
1047 dy
= strtol(dy_str
, NULL
, 0);
1050 dz
= strtol(dz_str
, NULL
, 0);
1051 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1054 static void do_mouse_button(int button_state
)
1056 mouse_button_state
= button_state
;
1057 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1060 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1066 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1074 val
= cpu_inb(NULL
, addr
);
1078 val
= cpu_inw(NULL
, addr
);
1082 val
= cpu_inl(NULL
, addr
);
1086 term_printf("port%c[0x%04x] = %#0*x\n",
1087 suffix
, addr
, size
* 2, val
);
1090 /* boot_set handler */
1091 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1092 static void *boot_opaque
;
1094 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1096 qemu_boot_set_handler
= func
;
1097 boot_opaque
= opaque
;
1100 static void do_boot_set(const char *bootdevice
)
1104 if (qemu_boot_set_handler
) {
1105 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1107 term_printf("boot device list now set to %s\n", bootdevice
);
1109 term_printf("setting boot device list failed with error %i\n", res
);
1111 term_printf("no function defined to set boot device list for this architecture\n");
1115 static void do_system_reset(void)
1117 qemu_system_reset_request();
1120 static void do_system_powerdown(void)
1122 qemu_system_powerdown_request();
1125 #if defined(TARGET_I386)
1126 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1128 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1131 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1132 pte
& PG_PSE_MASK
? 'P' : '-',
1133 pte
& PG_DIRTY_MASK
? 'D' : '-',
1134 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1135 pte
& PG_PCD_MASK
? 'C' : '-',
1136 pte
& PG_PWT_MASK
? 'T' : '-',
1137 pte
& PG_USER_MASK
? 'U' : '-',
1138 pte
& PG_RW_MASK
? 'W' : '-');
1141 static void tlb_info(void)
1145 uint32_t pgd
, pde
, pte
;
1147 env
= mon_get_cpu();
1151 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1152 term_printf("PG disabled\n");
1155 pgd
= env
->cr
[3] & ~0xfff;
1156 for(l1
= 0; l1
< 1024; l1
++) {
1157 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1158 pde
= le32_to_cpu(pde
);
1159 if (pde
& PG_PRESENT_MASK
) {
1160 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1161 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1163 for(l2
= 0; l2
< 1024; l2
++) {
1164 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1165 (uint8_t *)&pte
, 4);
1166 pte
= le32_to_cpu(pte
);
1167 if (pte
& PG_PRESENT_MASK
) {
1168 print_pte((l1
<< 22) + (l2
<< 12),
1178 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1179 uint32_t end
, int prot
)
1182 prot1
= *plast_prot
;
1183 if (prot
!= prot1
) {
1184 if (*pstart
!= -1) {
1185 term_printf("%08x-%08x %08x %c%c%c\n",
1186 *pstart
, end
, end
- *pstart
,
1187 prot1
& PG_USER_MASK
? 'u' : '-',
1189 prot1
& PG_RW_MASK
? 'w' : '-');
1199 static void mem_info(void)
1202 int l1
, l2
, prot
, last_prot
;
1203 uint32_t pgd
, pde
, pte
, start
, end
;
1205 env
= mon_get_cpu();
1209 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1210 term_printf("PG disabled\n");
1213 pgd
= env
->cr
[3] & ~0xfff;
1216 for(l1
= 0; l1
< 1024; l1
++) {
1217 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1218 pde
= le32_to_cpu(pde
);
1220 if (pde
& PG_PRESENT_MASK
) {
1221 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1222 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1223 mem_print(&start
, &last_prot
, end
, prot
);
1225 for(l2
= 0; l2
< 1024; l2
++) {
1226 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1227 (uint8_t *)&pte
, 4);
1228 pte
= le32_to_cpu(pte
);
1229 end
= (l1
<< 22) + (l2
<< 12);
1230 if (pte
& PG_PRESENT_MASK
) {
1231 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1235 mem_print(&start
, &last_prot
, end
, prot
);
1240 mem_print(&start
, &last_prot
, end
, prot
);
1246 static void do_info_kqemu(void)
1252 env
= mon_get_cpu();
1254 term_printf("No cpu initialized yet");
1257 val
= env
->kqemu_enabled
;
1258 term_printf("kqemu support: ");
1262 term_printf("disabled\n");
1265 term_printf("enabled for user code\n");
1268 term_printf("enabled for user and kernel code\n");
1272 term_printf("kqemu support: not compiled\n");
1276 static void do_info_kvm(void)
1279 term_printf("kvm support: ");
1281 term_printf("enabled\n");
1283 term_printf("disabled\n");
1285 term_printf("kvm support: not compiled\n");
1289 #ifdef CONFIG_PROFILER
1293 int64_t kqemu_exec_count
;
1295 int64_t kqemu_ret_int_count
;
1296 int64_t kqemu_ret_excp_count
;
1297 int64_t kqemu_ret_intr_count
;
1299 static void do_info_profile(void)
1305 term_printf("async time %" PRId64
" (%0.3f)\n",
1306 dev_time
, dev_time
/ (double)ticks_per_sec
);
1307 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1308 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1309 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1310 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1311 kqemu_time
/ (double)total
* 100.0,
1313 kqemu_ret_int_count
,
1314 kqemu_ret_excp_count
,
1315 kqemu_ret_intr_count
);
1318 kqemu_exec_count
= 0;
1320 kqemu_ret_int_count
= 0;
1321 kqemu_ret_excp_count
= 0;
1322 kqemu_ret_intr_count
= 0;
1324 kqemu_record_dump();
1328 static void do_info_profile(void)
1330 term_printf("Internal profiler not compiled\n");
1334 /* Capture support */
1335 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1337 static void do_info_capture (void)
1342 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1343 term_printf ("[%d]: ", i
);
1344 s
->ops
.info (s
->opaque
);
1348 static void do_stop_capture (int n
)
1353 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1355 s
->ops
.destroy (s
->opaque
);
1356 LIST_REMOVE (s
, entries
);
1364 static void do_wav_capture (const char *path
,
1365 int has_freq
, int freq
,
1366 int has_bits
, int bits
,
1367 int has_channels
, int nchannels
)
1371 s
= qemu_mallocz (sizeof (*s
));
1373 term_printf ("Not enough memory to add wave capture\n");
1377 freq
= has_freq
? freq
: 44100;
1378 bits
= has_bits
? bits
: 16;
1379 nchannels
= has_channels
? nchannels
: 2;
1381 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1382 term_printf ("Faied to add wave capture\n");
1385 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1389 #if defined(TARGET_I386)
1390 static void do_inject_nmi(int cpu_index
)
1394 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1395 if (env
->cpu_index
== cpu_index
) {
1396 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1402 static void do_balloon(int value
)
1404 ram_addr_t target
= value
;
1405 qemu_balloon(target
<< 20);
1408 static void do_info_balloon(void)
1412 actual
= qemu_balloon_status();
1413 if (kvm_enabled() && !kvm_has_sync_mmu())
1414 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1415 else if (actual
== 0)
1416 term_printf("Ballooning not activated in VM\n");
1418 term_printf("balloon: actual=%d\n", (int)(actual
>> 20));
1421 static const term_cmd_t term_cmds
[] = {
1422 { "help|?", "s?", do_help
,
1423 "[cmd]", "show the help" },
1424 { "commit", "s", do_commit
,
1425 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1426 { "info", "s?", do_info
,
1427 "subcommand", "show various information about the system state" },
1428 { "q|quit", "", do_quit
,
1429 "", "quit the emulator" },
1430 { "eject", "-fB", do_eject
,
1431 "[-f] device", "eject a removable medium (use -f to force it)" },
1432 { "change", "BFs?", do_change
,
1433 "device filename [format]", "change a removable medium, optional format" },
1434 { "screendump", "F", do_screen_dump
,
1435 "filename", "save screen into PPM image 'filename'" },
1436 { "logfile", "F", do_logfile
,
1437 "filename", "output logs to 'filename'" },
1438 { "log", "s", do_log
,
1439 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1440 { "savevm", "s?", do_savevm
,
1441 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1442 { "loadvm", "s", do_loadvm
,
1443 "tag|id", "restore a VM snapshot from its tag or id" },
1444 { "delvm", "s", do_delvm
,
1445 "tag|id", "delete a VM snapshot from its tag or id" },
1446 { "stop", "", do_stop
,
1447 "", "stop emulation", },
1448 { "c|cont", "", do_cont
,
1449 "", "resume emulation", },
1450 #ifdef CONFIG_GDBSTUB
1451 { "gdbserver", "s?", do_gdbserver
,
1452 "[port]", "start gdbserver session (default port=1234)", },
1454 { "x", "/l", do_memory_dump
,
1455 "/fmt addr", "virtual memory dump starting at 'addr'", },
1456 { "xp", "/l", do_physical_memory_dump
,
1457 "/fmt addr", "physical memory dump starting at 'addr'", },
1458 { "p|print", "/l", do_print
,
1459 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1460 { "i", "/ii.", do_ioport_read
,
1461 "/fmt addr", "I/O port read" },
1463 { "sendkey", "si?", do_sendkey
,
1464 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1465 { "system_reset", "", do_system_reset
,
1466 "", "reset the system" },
1467 { "system_powerdown", "", do_system_powerdown
,
1468 "", "send system power down event" },
1469 { "sum", "ii", do_sum
,
1470 "addr size", "compute the checksum of a memory region" },
1471 { "usb_add", "s", do_usb_add
,
1472 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1473 { "usb_del", "s", do_usb_del
,
1474 "device", "remove USB device 'bus.addr'" },
1475 { "cpu", "i", do_cpu_set
,
1476 "index", "set the default CPU" },
1477 { "mouse_move", "sss?", do_mouse_move
,
1478 "dx dy [dz]", "send mouse move events" },
1479 { "mouse_button", "i", do_mouse_button
,
1480 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1481 { "mouse_set", "i", do_mouse_set
,
1482 "index", "set which mouse device receives events" },
1484 { "wavcapture", "si?i?i?", do_wav_capture
,
1485 "path [frequency bits channels]",
1486 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1488 { "stopcapture", "i", do_stop_capture
,
1489 "capture index", "stop capture" },
1490 { "memsave", "lis", do_memory_save
,
1491 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1492 { "pmemsave", "lis", do_physical_memory_save
,
1493 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1494 { "boot_set", "s", do_boot_set
,
1495 "bootdevice", "define new values for the boot device list" },
1496 #if defined(TARGET_I386)
1497 { "nmi", "i", do_inject_nmi
,
1498 "cpu", "inject an NMI on the given CPU", },
1500 { "migrate", "-ds", do_migrate
,
1501 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1502 { "migrate_cancel", "", do_migrate_cancel
,
1503 "", "cancel the current VM migration" },
1504 { "migrate_set_speed", "s", do_migrate_set_speed
,
1505 "value", "set maximum speed (in bytes) for migrations" },
1506 { "balloon", "i", do_balloon
,
1507 "target", "request VM to change it's memory allocation (in MB)" },
1511 static const term_cmd_t info_cmds
[] = {
1512 { "version", "", do_info_version
,
1513 "", "show the version of qemu" },
1514 { "network", "", do_info_network
,
1515 "", "show the network state" },
1516 { "chardev", "", qemu_chr_info
,
1517 "", "show the character devices" },
1518 { "block", "", do_info_block
,
1519 "", "show the block devices" },
1520 { "blockstats", "", do_info_blockstats
,
1521 "", "show block device statistics" },
1522 { "registers", "", do_info_registers
,
1523 "", "show the cpu registers" },
1524 { "cpus", "", do_info_cpus
,
1525 "", "show infos for each CPU" },
1526 { "history", "", do_info_history
,
1527 "", "show the command line history", },
1528 { "irq", "", irq_info
,
1529 "", "show the interrupts statistics (if available)", },
1530 { "pic", "", pic_info
,
1531 "", "show i8259 (PIC) state", },
1532 { "pci", "", pci_info
,
1533 "", "show PCI info", },
1534 #if defined(TARGET_I386)
1535 { "tlb", "", tlb_info
,
1536 "", "show virtual to physical memory mappings", },
1537 { "mem", "", mem_info
,
1538 "", "show the active virtual memory mappings", },
1539 { "hpet", "", do_info_hpet
,
1540 "", "show state of HPET", },
1542 { "jit", "", do_info_jit
,
1543 "", "show dynamic compiler info", },
1544 { "kqemu", "", do_info_kqemu
,
1545 "", "show kqemu information", },
1546 { "kvm", "", do_info_kvm
,
1547 "", "show kvm information", },
1548 { "usb", "", usb_info
,
1549 "", "show guest USB devices", },
1550 { "usbhost", "", usb_host_info
,
1551 "", "show host USB devices", },
1552 { "profile", "", do_info_profile
,
1553 "", "show profiling information", },
1554 { "capture", "", do_info_capture
,
1555 "", "show capture information" },
1556 { "snapshots", "", do_info_snapshots
,
1557 "", "show the currently saved VM snapshots" },
1558 { "pcmcia", "", pcmcia_info
,
1559 "", "show guest PCMCIA status" },
1560 { "mice", "", do_info_mice
,
1561 "", "show which guest mouse is receiving events" },
1562 { "vnc", "", do_info_vnc
,
1563 "", "show the vnc server status"},
1564 { "name", "", do_info_name
,
1565 "", "show the current VM name" },
1566 { "uuid", "", do_info_uuid
,
1567 "", "show the current VM UUID" },
1568 #if defined(TARGET_PPC)
1569 { "cpustats", "", do_info_cpu_stats
,
1570 "", "show CPU statistics", },
1572 #if defined(CONFIG_SLIRP)
1573 { "slirp", "", do_info_slirp
,
1574 "", "show SLIRP statistics", },
1576 { "migrate", "", do_info_migrate
, "", "show migration status" },
1577 { "balloon", "", do_info_balloon
,
1578 "", "show balloon information" },
1582 /*******************************************************************/
1584 static const char *pch
;
1585 static jmp_buf expr_env
;
1590 typedef struct MonitorDef
{
1593 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1597 #if defined(TARGET_I386)
1598 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1600 CPUState
*env
= mon_get_cpu();
1603 return env
->eip
+ env
->segs
[R_CS
].base
;
1607 #if defined(TARGET_PPC)
1608 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1610 CPUState
*env
= mon_get_cpu();
1618 for (i
= 0; i
< 8; i
++)
1619 u
|= env
->crf
[i
] << (32 - (4 * i
));
1624 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1626 CPUState
*env
= mon_get_cpu();
1632 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1634 CPUState
*env
= mon_get_cpu();
1640 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1642 CPUState
*env
= mon_get_cpu();
1645 return cpu_ppc_load_decr(env
);
1648 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1650 CPUState
*env
= mon_get_cpu();
1653 return cpu_ppc_load_tbu(env
);
1656 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1658 CPUState
*env
= mon_get_cpu();
1661 return cpu_ppc_load_tbl(env
);
1665 #if defined(TARGET_SPARC)
1666 #ifndef TARGET_SPARC64
1667 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1669 CPUState
*env
= mon_get_cpu();
1672 return GET_PSR(env
);
1676 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1678 CPUState
*env
= mon_get_cpu();
1681 return env
->regwptr
[val
];
1685 static const MonitorDef monitor_defs
[] = {
1688 #define SEG(name, seg) \
1689 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1690 { name ".base", offsetof(CPUState, segs[seg].base) },\
1691 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1693 { "eax", offsetof(CPUState
, regs
[0]) },
1694 { "ecx", offsetof(CPUState
, regs
[1]) },
1695 { "edx", offsetof(CPUState
, regs
[2]) },
1696 { "ebx", offsetof(CPUState
, regs
[3]) },
1697 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1698 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1699 { "esi", offsetof(CPUState
, regs
[6]) },
1700 { "edi", offsetof(CPUState
, regs
[7]) },
1701 #ifdef TARGET_X86_64
1702 { "r8", offsetof(CPUState
, regs
[8]) },
1703 { "r9", offsetof(CPUState
, regs
[9]) },
1704 { "r10", offsetof(CPUState
, regs
[10]) },
1705 { "r11", offsetof(CPUState
, regs
[11]) },
1706 { "r12", offsetof(CPUState
, regs
[12]) },
1707 { "r13", offsetof(CPUState
, regs
[13]) },
1708 { "r14", offsetof(CPUState
, regs
[14]) },
1709 { "r15", offsetof(CPUState
, regs
[15]) },
1711 { "eflags", offsetof(CPUState
, eflags
) },
1712 { "eip", offsetof(CPUState
, eip
) },
1719 { "pc", 0, monitor_get_pc
, },
1720 #elif defined(TARGET_PPC)
1721 /* General purpose registers */
1722 { "r0", offsetof(CPUState
, gpr
[0]) },
1723 { "r1", offsetof(CPUState
, gpr
[1]) },
1724 { "r2", offsetof(CPUState
, gpr
[2]) },
1725 { "r3", offsetof(CPUState
, gpr
[3]) },
1726 { "r4", offsetof(CPUState
, gpr
[4]) },
1727 { "r5", offsetof(CPUState
, gpr
[5]) },
1728 { "r6", offsetof(CPUState
, gpr
[6]) },
1729 { "r7", offsetof(CPUState
, gpr
[7]) },
1730 { "r8", offsetof(CPUState
, gpr
[8]) },
1731 { "r9", offsetof(CPUState
, gpr
[9]) },
1732 { "r10", offsetof(CPUState
, gpr
[10]) },
1733 { "r11", offsetof(CPUState
, gpr
[11]) },
1734 { "r12", offsetof(CPUState
, gpr
[12]) },
1735 { "r13", offsetof(CPUState
, gpr
[13]) },
1736 { "r14", offsetof(CPUState
, gpr
[14]) },
1737 { "r15", offsetof(CPUState
, gpr
[15]) },
1738 { "r16", offsetof(CPUState
, gpr
[16]) },
1739 { "r17", offsetof(CPUState
, gpr
[17]) },
1740 { "r18", offsetof(CPUState
, gpr
[18]) },
1741 { "r19", offsetof(CPUState
, gpr
[19]) },
1742 { "r20", offsetof(CPUState
, gpr
[20]) },
1743 { "r21", offsetof(CPUState
, gpr
[21]) },
1744 { "r22", offsetof(CPUState
, gpr
[22]) },
1745 { "r23", offsetof(CPUState
, gpr
[23]) },
1746 { "r24", offsetof(CPUState
, gpr
[24]) },
1747 { "r25", offsetof(CPUState
, gpr
[25]) },
1748 { "r26", offsetof(CPUState
, gpr
[26]) },
1749 { "r27", offsetof(CPUState
, gpr
[27]) },
1750 { "r28", offsetof(CPUState
, gpr
[28]) },
1751 { "r29", offsetof(CPUState
, gpr
[29]) },
1752 { "r30", offsetof(CPUState
, gpr
[30]) },
1753 { "r31", offsetof(CPUState
, gpr
[31]) },
1754 /* Floating point registers */
1755 { "f0", offsetof(CPUState
, fpr
[0]) },
1756 { "f1", offsetof(CPUState
, fpr
[1]) },
1757 { "f2", offsetof(CPUState
, fpr
[2]) },
1758 { "f3", offsetof(CPUState
, fpr
[3]) },
1759 { "f4", offsetof(CPUState
, fpr
[4]) },
1760 { "f5", offsetof(CPUState
, fpr
[5]) },
1761 { "f6", offsetof(CPUState
, fpr
[6]) },
1762 { "f7", offsetof(CPUState
, fpr
[7]) },
1763 { "f8", offsetof(CPUState
, fpr
[8]) },
1764 { "f9", offsetof(CPUState
, fpr
[9]) },
1765 { "f10", offsetof(CPUState
, fpr
[10]) },
1766 { "f11", offsetof(CPUState
, fpr
[11]) },
1767 { "f12", offsetof(CPUState
, fpr
[12]) },
1768 { "f13", offsetof(CPUState
, fpr
[13]) },
1769 { "f14", offsetof(CPUState
, fpr
[14]) },
1770 { "f15", offsetof(CPUState
, fpr
[15]) },
1771 { "f16", offsetof(CPUState
, fpr
[16]) },
1772 { "f17", offsetof(CPUState
, fpr
[17]) },
1773 { "f18", offsetof(CPUState
, fpr
[18]) },
1774 { "f19", offsetof(CPUState
, fpr
[19]) },
1775 { "f20", offsetof(CPUState
, fpr
[20]) },
1776 { "f21", offsetof(CPUState
, fpr
[21]) },
1777 { "f22", offsetof(CPUState
, fpr
[22]) },
1778 { "f23", offsetof(CPUState
, fpr
[23]) },
1779 { "f24", offsetof(CPUState
, fpr
[24]) },
1780 { "f25", offsetof(CPUState
, fpr
[25]) },
1781 { "f26", offsetof(CPUState
, fpr
[26]) },
1782 { "f27", offsetof(CPUState
, fpr
[27]) },
1783 { "f28", offsetof(CPUState
, fpr
[28]) },
1784 { "f29", offsetof(CPUState
, fpr
[29]) },
1785 { "f30", offsetof(CPUState
, fpr
[30]) },
1786 { "f31", offsetof(CPUState
, fpr
[31]) },
1787 { "fpscr", offsetof(CPUState
, fpscr
) },
1788 /* Next instruction pointer */
1789 { "nip|pc", offsetof(CPUState
, nip
) },
1790 { "lr", offsetof(CPUState
, lr
) },
1791 { "ctr", offsetof(CPUState
, ctr
) },
1792 { "decr", 0, &monitor_get_decr
, },
1793 { "ccr", 0, &monitor_get_ccr
, },
1794 /* Machine state register */
1795 { "msr", 0, &monitor_get_msr
, },
1796 { "xer", 0, &monitor_get_xer
, },
1797 { "tbu", 0, &monitor_get_tbu
, },
1798 { "tbl", 0, &monitor_get_tbl
, },
1799 #if defined(TARGET_PPC64)
1800 /* Address space register */
1801 { "asr", offsetof(CPUState
, asr
) },
1803 /* Segment registers */
1804 { "sdr1", offsetof(CPUState
, sdr1
) },
1805 { "sr0", offsetof(CPUState
, sr
[0]) },
1806 { "sr1", offsetof(CPUState
, sr
[1]) },
1807 { "sr2", offsetof(CPUState
, sr
[2]) },
1808 { "sr3", offsetof(CPUState
, sr
[3]) },
1809 { "sr4", offsetof(CPUState
, sr
[4]) },
1810 { "sr5", offsetof(CPUState
, sr
[5]) },
1811 { "sr6", offsetof(CPUState
, sr
[6]) },
1812 { "sr7", offsetof(CPUState
, sr
[7]) },
1813 { "sr8", offsetof(CPUState
, sr
[8]) },
1814 { "sr9", offsetof(CPUState
, sr
[9]) },
1815 { "sr10", offsetof(CPUState
, sr
[10]) },
1816 { "sr11", offsetof(CPUState
, sr
[11]) },
1817 { "sr12", offsetof(CPUState
, sr
[12]) },
1818 { "sr13", offsetof(CPUState
, sr
[13]) },
1819 { "sr14", offsetof(CPUState
, sr
[14]) },
1820 { "sr15", offsetof(CPUState
, sr
[15]) },
1821 /* Too lazy to put BATs and SPRs ... */
1822 #elif defined(TARGET_SPARC)
1823 { "g0", offsetof(CPUState
, gregs
[0]) },
1824 { "g1", offsetof(CPUState
, gregs
[1]) },
1825 { "g2", offsetof(CPUState
, gregs
[2]) },
1826 { "g3", offsetof(CPUState
, gregs
[3]) },
1827 { "g4", offsetof(CPUState
, gregs
[4]) },
1828 { "g5", offsetof(CPUState
, gregs
[5]) },
1829 { "g6", offsetof(CPUState
, gregs
[6]) },
1830 { "g7", offsetof(CPUState
, gregs
[7]) },
1831 { "o0", 0, monitor_get_reg
},
1832 { "o1", 1, monitor_get_reg
},
1833 { "o2", 2, monitor_get_reg
},
1834 { "o3", 3, monitor_get_reg
},
1835 { "o4", 4, monitor_get_reg
},
1836 { "o5", 5, monitor_get_reg
},
1837 { "o6", 6, monitor_get_reg
},
1838 { "o7", 7, monitor_get_reg
},
1839 { "l0", 8, monitor_get_reg
},
1840 { "l1", 9, monitor_get_reg
},
1841 { "l2", 10, monitor_get_reg
},
1842 { "l3", 11, monitor_get_reg
},
1843 { "l4", 12, monitor_get_reg
},
1844 { "l5", 13, monitor_get_reg
},
1845 { "l6", 14, monitor_get_reg
},
1846 { "l7", 15, monitor_get_reg
},
1847 { "i0", 16, monitor_get_reg
},
1848 { "i1", 17, monitor_get_reg
},
1849 { "i2", 18, monitor_get_reg
},
1850 { "i3", 19, monitor_get_reg
},
1851 { "i4", 20, monitor_get_reg
},
1852 { "i5", 21, monitor_get_reg
},
1853 { "i6", 22, monitor_get_reg
},
1854 { "i7", 23, monitor_get_reg
},
1855 { "pc", offsetof(CPUState
, pc
) },
1856 { "npc", offsetof(CPUState
, npc
) },
1857 { "y", offsetof(CPUState
, y
) },
1858 #ifndef TARGET_SPARC64
1859 { "psr", 0, &monitor_get_psr
, },
1860 { "wim", offsetof(CPUState
, wim
) },
1862 { "tbr", offsetof(CPUState
, tbr
) },
1863 { "fsr", offsetof(CPUState
, fsr
) },
1864 { "f0", offsetof(CPUState
, fpr
[0]) },
1865 { "f1", offsetof(CPUState
, fpr
[1]) },
1866 { "f2", offsetof(CPUState
, fpr
[2]) },
1867 { "f3", offsetof(CPUState
, fpr
[3]) },
1868 { "f4", offsetof(CPUState
, fpr
[4]) },
1869 { "f5", offsetof(CPUState
, fpr
[5]) },
1870 { "f6", offsetof(CPUState
, fpr
[6]) },
1871 { "f7", offsetof(CPUState
, fpr
[7]) },
1872 { "f8", offsetof(CPUState
, fpr
[8]) },
1873 { "f9", offsetof(CPUState
, fpr
[9]) },
1874 { "f10", offsetof(CPUState
, fpr
[10]) },
1875 { "f11", offsetof(CPUState
, fpr
[11]) },
1876 { "f12", offsetof(CPUState
, fpr
[12]) },
1877 { "f13", offsetof(CPUState
, fpr
[13]) },
1878 { "f14", offsetof(CPUState
, fpr
[14]) },
1879 { "f15", offsetof(CPUState
, fpr
[15]) },
1880 { "f16", offsetof(CPUState
, fpr
[16]) },
1881 { "f17", offsetof(CPUState
, fpr
[17]) },
1882 { "f18", offsetof(CPUState
, fpr
[18]) },
1883 { "f19", offsetof(CPUState
, fpr
[19]) },
1884 { "f20", offsetof(CPUState
, fpr
[20]) },
1885 { "f21", offsetof(CPUState
, fpr
[21]) },
1886 { "f22", offsetof(CPUState
, fpr
[22]) },
1887 { "f23", offsetof(CPUState
, fpr
[23]) },
1888 { "f24", offsetof(CPUState
, fpr
[24]) },
1889 { "f25", offsetof(CPUState
, fpr
[25]) },
1890 { "f26", offsetof(CPUState
, fpr
[26]) },
1891 { "f27", offsetof(CPUState
, fpr
[27]) },
1892 { "f28", offsetof(CPUState
, fpr
[28]) },
1893 { "f29", offsetof(CPUState
, fpr
[29]) },
1894 { "f30", offsetof(CPUState
, fpr
[30]) },
1895 { "f31", offsetof(CPUState
, fpr
[31]) },
1896 #ifdef TARGET_SPARC64
1897 { "f32", offsetof(CPUState
, fpr
[32]) },
1898 { "f34", offsetof(CPUState
, fpr
[34]) },
1899 { "f36", offsetof(CPUState
, fpr
[36]) },
1900 { "f38", offsetof(CPUState
, fpr
[38]) },
1901 { "f40", offsetof(CPUState
, fpr
[40]) },
1902 { "f42", offsetof(CPUState
, fpr
[42]) },
1903 { "f44", offsetof(CPUState
, fpr
[44]) },
1904 { "f46", offsetof(CPUState
, fpr
[46]) },
1905 { "f48", offsetof(CPUState
, fpr
[48]) },
1906 { "f50", offsetof(CPUState
, fpr
[50]) },
1907 { "f52", offsetof(CPUState
, fpr
[52]) },
1908 { "f54", offsetof(CPUState
, fpr
[54]) },
1909 { "f56", offsetof(CPUState
, fpr
[56]) },
1910 { "f58", offsetof(CPUState
, fpr
[58]) },
1911 { "f60", offsetof(CPUState
, fpr
[60]) },
1912 { "f62", offsetof(CPUState
, fpr
[62]) },
1913 { "asi", offsetof(CPUState
, asi
) },
1914 { "pstate", offsetof(CPUState
, pstate
) },
1915 { "cansave", offsetof(CPUState
, cansave
) },
1916 { "canrestore", offsetof(CPUState
, canrestore
) },
1917 { "otherwin", offsetof(CPUState
, otherwin
) },
1918 { "wstate", offsetof(CPUState
, wstate
) },
1919 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1920 { "fprs", offsetof(CPUState
, fprs
) },
1926 static void expr_error(const char *fmt
)
1930 longjmp(expr_env
, 1);
1933 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1934 static int get_monitor_def(target_long
*pval
, const char *name
)
1936 const MonitorDef
*md
;
1939 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1940 if (compare_cmd(name
, md
->name
)) {
1941 if (md
->get_value
) {
1942 *pval
= md
->get_value(md
, md
->offset
);
1944 CPUState
*env
= mon_get_cpu();
1947 ptr
= (uint8_t *)env
+ md
->offset
;
1950 *pval
= *(int32_t *)ptr
;
1953 *pval
= *(target_long
*)ptr
;
1966 static void next(void)
1970 while (qemu_isspace(*pch
))
1975 static int64_t expr_sum(void);
1977 static int64_t expr_unary(void)
2000 expr_error("')' expected");
2007 expr_error("character constant expected");
2011 expr_error("missing terminating \' character");
2021 while ((*pch
>= 'a' && *pch
<= 'z') ||
2022 (*pch
>= 'A' && *pch
<= 'Z') ||
2023 (*pch
>= '0' && *pch
<= '9') ||
2024 *pch
== '_' || *pch
== '.') {
2025 if ((q
- buf
) < sizeof(buf
) - 1)
2029 while (qemu_isspace(*pch
))
2032 ret
= get_monitor_def(®
, buf
);
2034 expr_error("unknown register");
2036 expr_error("no cpu defined");
2041 expr_error("unexpected end of expression");
2045 #if TARGET_PHYS_ADDR_BITS > 32
2046 n
= strtoull(pch
, &p
, 0);
2048 n
= strtoul(pch
, &p
, 0);
2051 expr_error("invalid char in expression");
2054 while (qemu_isspace(*pch
))
2062 static int64_t expr_prod(void)
2070 if (op
!= '*' && op
!= '/' && op
!= '%')
2073 val2
= expr_unary();
2082 expr_error("division by zero");
2093 static int64_t expr_logic(void)
2101 if (op
!= '&' && op
!= '|' && op
!= '^')
2121 static int64_t expr_sum(void)
2129 if (op
!= '+' && op
!= '-')
2132 val2
= expr_logic();
2141 static int get_expr(int64_t *pval
, const char **pp
)
2144 if (setjmp(expr_env
)) {
2148 while (qemu_isspace(*pch
))
2155 static int get_str(char *buf
, int buf_size
, const char **pp
)
2163 while (qemu_isspace(*p
))
2173 while (*p
!= '\0' && *p
!= '\"') {
2189 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2192 if ((q
- buf
) < buf_size
- 1) {
2196 if ((q
- buf
) < buf_size
- 1) {
2203 qemu_printf("unterminated string\n");
2208 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2209 if ((q
- buf
) < buf_size
- 1) {
2220 static int default_fmt_format
= 'x';
2221 static int default_fmt_size
= 4;
2225 static void monitor_handle_command(const char *cmdline
)
2227 const char *p
, *pstart
, *typestr
;
2229 int c
, nb_args
, len
, i
, has_arg
;
2230 const term_cmd_t
*cmd
;
2233 void *str_allocated
[MAX_ARGS
];
2234 void *args
[MAX_ARGS
];
2235 void (*handler_0
)(void);
2236 void (*handler_1
)(void *arg0
);
2237 void (*handler_2
)(void *arg0
, void *arg1
);
2238 void (*handler_3
)(void *arg0
, void *arg1
, void *arg2
);
2239 void (*handler_4
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
);
2240 void (*handler_5
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2242 void (*handler_6
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2243 void *arg4
, void *arg5
);
2244 void (*handler_7
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2245 void *arg4
, void *arg5
, void *arg6
);
2248 term_printf("command='%s'\n", cmdline
);
2251 /* extract the command name */
2254 while (qemu_isspace(*p
))
2259 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2262 if (len
> sizeof(cmdname
) - 1)
2263 len
= sizeof(cmdname
) - 1;
2264 memcpy(cmdname
, pstart
, len
);
2265 cmdname
[len
] = '\0';
2267 /* find the command */
2268 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2269 if (compare_cmd(cmdname
, cmd
->name
))
2272 term_printf("unknown command: '%s'\n", cmdname
);
2276 for(i
= 0; i
< MAX_ARGS
; i
++)
2277 str_allocated
[i
] = NULL
;
2279 /* parse the parameters */
2280 typestr
= cmd
->args_type
;
2295 while (qemu_isspace(*p
))
2297 if (*typestr
== '?') {
2300 /* no optional string: NULL argument */
2305 ret
= get_str(buf
, sizeof(buf
), &p
);
2309 term_printf("%s: filename expected\n", cmdname
);
2312 term_printf("%s: block device name expected\n", cmdname
);
2315 term_printf("%s: string expected\n", cmdname
);
2320 str
= qemu_malloc(strlen(buf
) + 1);
2321 pstrcpy(str
, sizeof(buf
), buf
);
2322 str_allocated
[nb_args
] = str
;
2324 if (nb_args
>= MAX_ARGS
) {
2326 term_printf("%s: too many arguments\n", cmdname
);
2329 args
[nb_args
++] = str
;
2334 int count
, format
, size
;
2336 while (qemu_isspace(*p
))
2342 if (qemu_isdigit(*p
)) {
2344 while (qemu_isdigit(*p
)) {
2345 count
= count
* 10 + (*p
- '0');
2383 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2384 term_printf("invalid char in format: '%c'\n", *p
);
2388 format
= default_fmt_format
;
2389 if (format
!= 'i') {
2390 /* for 'i', not specifying a size gives -1 as size */
2392 size
= default_fmt_size
;
2393 default_fmt_size
= size
;
2395 default_fmt_format
= format
;
2398 format
= default_fmt_format
;
2399 if (format
!= 'i') {
2400 size
= default_fmt_size
;
2405 if (nb_args
+ 3 > MAX_ARGS
)
2407 args
[nb_args
++] = (void*)(long)count
;
2408 args
[nb_args
++] = (void*)(long)format
;
2409 args
[nb_args
++] = (void*)(long)size
;
2417 while (qemu_isspace(*p
))
2419 if (*typestr
== '?' || *typestr
== '.') {
2420 if (*typestr
== '?') {
2428 while (qemu_isspace(*p
))
2436 if (nb_args
>= MAX_ARGS
)
2438 args
[nb_args
++] = (void *)(long)has_arg
;
2440 if (nb_args
>= MAX_ARGS
)
2446 if (get_expr(&val
, &p
))
2450 if (nb_args
>= MAX_ARGS
)
2452 args
[nb_args
++] = (void *)(long)val
;
2454 if ((nb_args
+ 1) >= MAX_ARGS
)
2456 #if TARGET_PHYS_ADDR_BITS > 32
2457 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2459 args
[nb_args
++] = (void *)0;
2461 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2473 while (qemu_isspace(*p
))
2479 term_printf("%s: unsupported option -%c\n",
2486 if (nb_args
>= MAX_ARGS
)
2488 args
[nb_args
++] = (void *)(long)has_option
;
2493 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2497 /* check that all arguments were parsed */
2498 while (qemu_isspace(*p
))
2501 term_printf("%s: extraneous characters at the end of line\n",
2508 handler_0
= cmd
->handler
;
2512 handler_1
= cmd
->handler
;
2516 handler_2
= cmd
->handler
;
2517 handler_2(args
[0], args
[1]);
2520 handler_3
= cmd
->handler
;
2521 handler_3(args
[0], args
[1], args
[2]);
2524 handler_4
= cmd
->handler
;
2525 handler_4(args
[0], args
[1], args
[2], args
[3]);
2528 handler_5
= cmd
->handler
;
2529 handler_5(args
[0], args
[1], args
[2], args
[3], args
[4]);
2532 handler_6
= cmd
->handler
;
2533 handler_6(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2536 handler_7
= cmd
->handler
;
2537 handler_7(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2540 term_printf("unsupported number of arguments: %d\n", nb_args
);
2544 for(i
= 0; i
< MAX_ARGS
; i
++)
2545 qemu_free(str_allocated
[i
]);
2549 static void cmd_completion(const char *name
, const char *list
)
2551 const char *p
, *pstart
;
2560 p
= pstart
+ strlen(pstart
);
2562 if (len
> sizeof(cmd
) - 2)
2563 len
= sizeof(cmd
) - 2;
2564 memcpy(cmd
, pstart
, len
);
2566 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2567 add_completion(cmd
);
2575 static void file_completion(const char *input
)
2580 char file
[1024], file_prefix
[1024];
2584 p
= strrchr(input
, '/');
2587 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2588 pstrcpy(path
, sizeof(path
), ".");
2590 input_path_len
= p
- input
+ 1;
2591 memcpy(path
, input
, input_path_len
);
2592 if (input_path_len
> sizeof(path
) - 1)
2593 input_path_len
= sizeof(path
) - 1;
2594 path
[input_path_len
] = '\0';
2595 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2597 #ifdef DEBUG_COMPLETION
2598 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2600 ffs
= opendir(path
);
2608 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2609 memcpy(file
, input
, input_path_len
);
2610 if (input_path_len
< sizeof(file
))
2611 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2613 /* stat the file to find out if it's a directory.
2614 * In that case add a slash to speed up typing long paths
2617 if(S_ISDIR(sb
.st_mode
))
2618 pstrcat(file
, sizeof(file
), "/");
2619 add_completion(file
);
2625 static void block_completion_it(void *opaque
, const char *name
)
2627 const char *input
= opaque
;
2629 if (input
[0] == '\0' ||
2630 !strncmp(name
, (char *)input
, strlen(input
))) {
2631 add_completion(name
);
2635 /* NOTE: this parser is an approximate form of the real command parser */
2636 static void parse_cmdline(const char *cmdline
,
2637 int *pnb_args
, char **args
)
2646 while (qemu_isspace(*p
))
2650 if (nb_args
>= MAX_ARGS
)
2652 ret
= get_str(buf
, sizeof(buf
), &p
);
2653 args
[nb_args
] = qemu_strdup(buf
);
2658 *pnb_args
= nb_args
;
2661 void readline_find_completion(const char *cmdline
)
2663 const char *cmdname
;
2664 char *args
[MAX_ARGS
];
2665 int nb_args
, i
, len
;
2666 const char *ptype
, *str
;
2667 const term_cmd_t
*cmd
;
2670 parse_cmdline(cmdline
, &nb_args
, args
);
2671 #ifdef DEBUG_COMPLETION
2672 for(i
= 0; i
< nb_args
; i
++) {
2673 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2677 /* if the line ends with a space, it means we want to complete the
2679 len
= strlen(cmdline
);
2680 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2681 if (nb_args
>= MAX_ARGS
)
2683 args
[nb_args
++] = qemu_strdup("");
2686 /* command completion */
2691 completion_index
= strlen(cmdname
);
2692 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2693 cmd_completion(cmdname
, cmd
->name
);
2696 /* find the command */
2697 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2698 if (compare_cmd(args
[0], cmd
->name
))
2703 ptype
= cmd
->args_type
;
2704 for(i
= 0; i
< nb_args
- 2; i
++) {
2705 if (*ptype
!= '\0') {
2707 while (*ptype
== '?')
2711 str
= args
[nb_args
- 1];
2714 /* file completion */
2715 completion_index
= strlen(str
);
2716 file_completion(str
);
2719 /* block device name completion */
2720 completion_index
= strlen(str
);
2721 bdrv_iterate(block_completion_it
, (void *)str
);
2724 /* XXX: more generic ? */
2725 if (!strcmp(cmd
->name
, "info")) {
2726 completion_index
= strlen(str
);
2727 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2728 cmd_completion(str
, cmd
->name
);
2730 } else if (!strcmp(cmd
->name
, "sendkey")) {
2731 completion_index
= strlen(str
);
2732 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2733 cmd_completion(str
, key
->name
);
2741 for(i
= 0; i
< nb_args
; i
++)
2745 static int term_can_read(void *opaque
)
2750 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2753 for(i
= 0; i
< size
; i
++)
2754 readline_handle_byte(buf
[i
]);
2757 static int monitor_suspended
;
2759 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2761 monitor_handle_command(cmdline
);
2762 if (!monitor_suspended
)
2763 monitor_start_input();
2765 monitor_suspended
= 2;
2768 void monitor_suspend(void)
2770 monitor_suspended
= 1;
2773 void monitor_resume(void)
2775 if (monitor_suspended
== 2)
2776 monitor_start_input();
2777 monitor_suspended
= 0;
2780 static void monitor_start_input(void)
2782 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2785 static void term_event(void *opaque
, int event
)
2787 if (event
!= CHR_EVENT_RESET
)
2791 term_printf("QEMU %s monitor - type 'help' for more information\n",
2793 monitor_start_input();
2796 static int is_first_init
= 1;
2798 void monitor_init(CharDriverState
*hd
, int show_banner
)
2802 if (is_first_init
) {
2803 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
2806 for (i
= 0; i
< MAX_MON
; i
++) {
2807 monitor_hd
[i
] = NULL
;
2811 for (i
= 0; i
< MAX_MON
; i
++) {
2812 if (monitor_hd
[i
] == NULL
) {
2818 hide_banner
= !show_banner
;
2820 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2822 readline_start("", 0, monitor_handle_command1
, NULL
);
2825 /* XXX: use threads ? */
2826 /* modal monitor readline */
2827 static int monitor_readline_started
;
2828 static char *monitor_readline_buf
;
2829 static int monitor_readline_buf_size
;
2831 static void monitor_readline_cb(void *opaque
, const char *input
)
2833 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2834 monitor_readline_started
= 0;
2837 void monitor_readline(const char *prompt
, int is_password
,
2838 char *buf
, int buf_size
)
2841 int old_focus
[MAX_MON
];
2844 for (i
= 0; i
< MAX_MON
; i
++) {
2846 if (monitor_hd
[i
]) {
2847 old_focus
[i
] = monitor_hd
[i
]->focus
;
2848 monitor_hd
[i
]->focus
= 0;
2849 qemu_chr_send_event(monitor_hd
[i
], CHR_EVENT_FOCUS
);
2854 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2855 monitor_readline_buf
= buf
;
2856 monitor_readline_buf_size
= buf_size
;
2857 monitor_readline_started
= 1;
2858 while (monitor_readline_started
) {
2861 /* restore original focus */
2863 for (i
= 0; i
< MAX_MON
; i
++)
2865 monitor_hd
[i
]->focus
= old_focus
[i
];