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_uuid(void)
257 term_printf(UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1], qemu_uuid
[2],
258 qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5], qemu_uuid
[6],
259 qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9], qemu_uuid
[10],
260 qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13], qemu_uuid
[14],
264 static void do_info_block(void)
269 static void do_info_blockstats(void)
274 /* get the current CPU defined by the user */
275 static int mon_set_cpu(int cpu_index
)
279 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
280 if (env
->cpu_index
== cpu_index
) {
288 static CPUState
*mon_get_cpu(void)
296 static void do_info_registers(void)
303 cpu_dump_state(env
, NULL
, monitor_fprintf
,
306 cpu_dump_state(env
, NULL
, monitor_fprintf
,
311 static void do_info_cpus(void)
315 /* just to set the default cpu if not already done */
318 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
319 term_printf("%c CPU #%d:",
320 (env
== mon_cpu
) ? '*' : ' ',
322 #if defined(TARGET_I386)
323 term_printf(" pc=0x" TARGET_FMT_lx
, env
->eip
+ env
->segs
[R_CS
].base
);
324 #elif defined(TARGET_PPC)
325 term_printf(" nip=0x" TARGET_FMT_lx
, env
->nip
);
326 #elif defined(TARGET_SPARC)
327 term_printf(" pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
, env
->pc
, env
->npc
);
328 #elif defined(TARGET_MIPS)
329 term_printf(" PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
332 term_printf(" (halted)");
337 static void do_cpu_set(int index
)
339 if (mon_set_cpu(index
) < 0)
340 term_printf("Invalid CPU index\n");
343 static void do_info_jit(void)
345 dump_exec_info(NULL
, monitor_fprintf
);
348 static void do_info_history (void)
355 str
= readline_get_history(i
);
358 term_printf("%d: '%s'\n", i
, str
);
363 #if defined(TARGET_PPC)
364 /* XXX: not implemented in other targets */
365 static void do_info_cpu_stats (void)
370 cpu_dump_statistics(env
, NULL
, &monitor_fprintf
, 0);
374 static void do_quit(void)
379 static int eject_device(BlockDriverState
*bs
, int force
)
381 if (bdrv_is_inserted(bs
)) {
383 if (!bdrv_is_removable(bs
)) {
384 term_printf("device is not removable\n");
387 if (bdrv_is_locked(bs
)) {
388 term_printf("device is locked\n");
397 static void do_eject(int force
, const char *filename
)
399 BlockDriverState
*bs
;
401 bs
= bdrv_find(filename
);
403 term_printf("device not found\n");
406 eject_device(bs
, force
);
409 static void do_change_block(const char *device
, const char *filename
, const char *fmt
)
411 BlockDriverState
*bs
;
412 BlockDriver
*drv
= NULL
;
414 bs
= bdrv_find(device
);
416 term_printf("device not found\n");
420 drv
= bdrv_find_format(fmt
);
422 term_printf("invalid format %s\n", fmt
);
426 if (eject_device(bs
, 0) < 0)
428 bdrv_open2(bs
, filename
, 0, drv
);
429 qemu_key_check(bs
, filename
);
432 static void do_change_vnc(const char *target
, const char *arg
)
434 if (strcmp(target
, "passwd") == 0 ||
435 strcmp(target
, "password") == 0) {
438 strncpy(password
, arg
, sizeof(password
));
439 password
[sizeof(password
) - 1] = '\0';
441 monitor_readline("Password: ", 1, password
, sizeof(password
));
442 if (vnc_display_password(NULL
, password
) < 0)
443 term_printf("could not set VNC server password\n");
445 if (vnc_display_open(NULL
, target
) < 0)
446 term_printf("could not start VNC server on %s\n", target
);
450 static void do_change(const char *device
, const char *target
, const char *arg
)
452 if (strcmp(device
, "vnc") == 0) {
453 do_change_vnc(target
, arg
);
455 do_change_block(device
, target
, arg
);
459 static void do_screen_dump(const char *filename
)
461 vga_hw_screen_dump(filename
);
464 static void do_logfile(const char *filename
)
466 cpu_set_log_filename(filename
);
469 static void do_log(const char *items
)
473 if (!strcmp(items
, "none")) {
476 mask
= cpu_str_to_log_mask(items
);
485 static void do_stop(void)
487 vm_stop(EXCP_INTERRUPT
);
490 static void do_cont(void)
495 #ifdef CONFIG_GDBSTUB
496 static void do_gdbserver(const char *port
)
499 port
= DEFAULT_GDBSTUB_PORT
;
500 if (gdbserver_start(port
) < 0) {
501 qemu_printf("Could not open gdbserver socket on port '%s'\n", port
);
503 qemu_printf("Waiting gdb connection on port '%s'\n", port
);
508 static void term_printc(int c
)
525 if (c
>= 32 && c
<= 126) {
526 term_printf("%c", c
);
528 term_printf("\\x%02x", c
);
535 static void memory_dump(int count
, int format
, int wsize
,
536 target_phys_addr_t addr
, int is_physical
)
539 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
547 if (!env
&& !is_physical
)
552 } else if (wsize
== 4) {
555 /* as default we use the current CS size */
559 if ((env
->efer
& MSR_EFER_LMA
) &&
560 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
564 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
569 monitor_disas(env
, addr
, count
, is_physical
, flags
);
578 nb_per_line
= line_size
/ wsize
;
583 max_digits
= (wsize
* 8 + 2) / 3;
587 max_digits
= (wsize
* 8) / 4;
591 max_digits
= (wsize
* 8 * 10 + 32) / 33;
600 term_printf(TARGET_FMT_plx
":", addr
);
602 term_printf(TARGET_FMT_lx
":", (target_ulong
)addr
);
607 cpu_physical_memory_rw(addr
, buf
, l
, 0);
612 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
613 term_printf(" Cannot access memory\n");
622 v
= ldub_raw(buf
+ i
);
625 v
= lduw_raw(buf
+ i
);
628 v
= (uint32_t)ldl_raw(buf
+ i
);
631 v
= ldq_raw(buf
+ i
);
637 term_printf("%#*" PRIo64
, max_digits
, v
);
640 term_printf("0x%0*" PRIx64
, max_digits
, v
);
643 term_printf("%*" PRIu64
, max_digits
, v
);
646 term_printf("%*" PRId64
, max_digits
, v
);
660 #if TARGET_LONG_BITS == 64
661 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
663 #define GET_TLONG(h, l) (l)
666 static void do_memory_dump(int count
, int format
, int size
,
667 uint32_t addrh
, uint32_t addrl
)
669 target_long addr
= GET_TLONG(addrh
, addrl
);
670 memory_dump(count
, format
, size
, addr
, 0);
673 #if TARGET_PHYS_ADDR_BITS > 32
674 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
676 #define GET_TPHYSADDR(h, l) (l)
679 static void do_physical_memory_dump(int count
, int format
, int size
,
680 uint32_t addrh
, uint32_t addrl
)
683 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
684 memory_dump(count
, format
, size
, addr
, 1);
687 static void do_print(int count
, int format
, int size
, unsigned int valh
, unsigned int vall
)
689 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
690 #if TARGET_PHYS_ADDR_BITS == 32
693 term_printf("%#o", val
);
696 term_printf("%#x", val
);
699 term_printf("%u", val
);
703 term_printf("%d", val
);
712 term_printf("%#" PRIo64
, val
);
715 term_printf("%#" PRIx64
, val
);
718 term_printf("%" PRIu64
, val
);
722 term_printf("%" PRId64
, val
);
732 static void do_memory_save(unsigned int valh
, unsigned int vall
,
733 uint32_t size
, const char *filename
)
736 target_long addr
= GET_TLONG(valh
, vall
);
745 f
= fopen(filename
, "wb");
747 term_printf("could not open '%s'\n", filename
);
754 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
755 fwrite(buf
, 1, l
, f
);
762 static void do_physical_memory_save(unsigned int valh
, unsigned int vall
,
763 uint32_t size
, const char *filename
)
768 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
770 f
= fopen(filename
, "wb");
772 term_printf("could not open '%s'\n", filename
);
779 cpu_physical_memory_rw(addr
, buf
, l
, 0);
780 fwrite(buf
, 1, l
, f
);
788 static void do_sum(uint32_t start
, uint32_t size
)
795 for(addr
= start
; addr
< (start
+ size
); addr
++) {
796 cpu_physical_memory_rw(addr
, buf
, 1, 0);
797 /* BSD sum algorithm ('sum' Unix command) */
798 sum
= (sum
>> 1) | (sum
<< 15);
801 term_printf("%05d\n", sum
);
809 static const KeyDef key_defs
[] = {
836 { 0x0e, "backspace" },
873 { 0x37, "asterisk" },
876 { 0x3a, "caps_lock" },
887 { 0x45, "num_lock" },
888 { 0x46, "scroll_lock" },
890 { 0xb5, "kp_divide" },
891 { 0x37, "kp_multiply" },
892 { 0x4a, "kp_subtract" },
894 { 0x9c, "kp_enter" },
895 { 0x53, "kp_decimal" },
928 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
948 static int get_keycode(const char *key
)
954 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
955 if (!strcmp(key
, p
->name
))
958 if (strstart(key
, "0x", NULL
)) {
959 ret
= strtoul(key
, &endp
, 0);
960 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
966 #define MAX_KEYCODES 16
967 static uint8_t keycodes
[MAX_KEYCODES
];
968 static int nb_pending_keycodes
;
969 static QEMUTimer
*key_timer
;
971 static void release_keys(void *opaque
)
975 while (nb_pending_keycodes
> 0) {
976 nb_pending_keycodes
--;
977 keycode
= keycodes
[nb_pending_keycodes
];
979 kbd_put_keycode(0xe0);
980 kbd_put_keycode(keycode
| 0x80);
984 static void do_sendkey(const char *string
, int has_hold_time
, int hold_time
)
986 char keyname_buf
[16];
988 int keyname_len
, keycode
, i
;
990 if (nb_pending_keycodes
> 0) {
991 qemu_del_timer(key_timer
);
998 separator
= strchr(string
, '-');
999 keyname_len
= separator
? separator
- string
: strlen(string
);
1000 if (keyname_len
> 0) {
1001 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1002 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1003 term_printf("invalid key: '%s...'\n", keyname_buf
);
1006 if (i
== MAX_KEYCODES
) {
1007 term_printf("too many keys\n");
1010 keyname_buf
[keyname_len
] = 0;
1011 keycode
= get_keycode(keyname_buf
);
1013 term_printf("unknown key: '%s'\n", keyname_buf
);
1016 keycodes
[i
++] = keycode
;
1020 string
= separator
+ 1;
1022 nb_pending_keycodes
= i
;
1023 /* key down events */
1024 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1025 keycode
= keycodes
[i
];
1027 kbd_put_keycode(0xe0);
1028 kbd_put_keycode(keycode
& 0x7f);
1030 /* delayed key up events */
1031 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1032 muldiv64(ticks_per_sec
, hold_time
, 1000));
1035 static int mouse_button_state
;
1037 static void do_mouse_move(const char *dx_str
, const char *dy_str
,
1041 dx
= strtol(dx_str
, NULL
, 0);
1042 dy
= strtol(dy_str
, NULL
, 0);
1045 dz
= strtol(dz_str
, NULL
, 0);
1046 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1049 static void do_mouse_button(int button_state
)
1051 mouse_button_state
= button_state
;
1052 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1055 static void do_ioport_read(int count
, int format
, int size
, int addr
, int has_index
, int index
)
1061 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1069 val
= cpu_inb(NULL
, addr
);
1073 val
= cpu_inw(NULL
, addr
);
1077 val
= cpu_inl(NULL
, addr
);
1081 term_printf("port%c[0x%04x] = %#0*x\n",
1082 suffix
, addr
, size
* 2, val
);
1085 /* boot_set handler */
1086 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1087 static void *boot_opaque
;
1089 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1091 qemu_boot_set_handler
= func
;
1092 boot_opaque
= opaque
;
1095 static void do_boot_set(const char *bootdevice
)
1099 if (qemu_boot_set_handler
) {
1100 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1102 term_printf("boot device list now set to %s\n", bootdevice
);
1104 term_printf("setting boot device list failed with error %i\n", res
);
1106 term_printf("no function defined to set boot device list for this architecture\n");
1110 static void do_system_reset(void)
1112 qemu_system_reset_request();
1115 static void do_system_powerdown(void)
1117 qemu_system_powerdown_request();
1120 #if defined(TARGET_I386)
1121 static void print_pte(uint32_t addr
, uint32_t pte
, uint32_t mask
)
1123 term_printf("%08x: %08x %c%c%c%c%c%c%c%c\n",
1126 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1127 pte
& PG_PSE_MASK
? 'P' : '-',
1128 pte
& PG_DIRTY_MASK
? 'D' : '-',
1129 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1130 pte
& PG_PCD_MASK
? 'C' : '-',
1131 pte
& PG_PWT_MASK
? 'T' : '-',
1132 pte
& PG_USER_MASK
? 'U' : '-',
1133 pte
& PG_RW_MASK
? 'W' : '-');
1136 static void tlb_info(void)
1140 uint32_t pgd
, pde
, pte
;
1142 env
= mon_get_cpu();
1146 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1147 term_printf("PG disabled\n");
1150 pgd
= env
->cr
[3] & ~0xfff;
1151 for(l1
= 0; l1
< 1024; l1
++) {
1152 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1153 pde
= le32_to_cpu(pde
);
1154 if (pde
& PG_PRESENT_MASK
) {
1155 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1156 print_pte((l1
<< 22), pde
, ~((1 << 20) - 1));
1158 for(l2
= 0; l2
< 1024; l2
++) {
1159 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1160 (uint8_t *)&pte
, 4);
1161 pte
= le32_to_cpu(pte
);
1162 if (pte
& PG_PRESENT_MASK
) {
1163 print_pte((l1
<< 22) + (l2
<< 12),
1173 static void mem_print(uint32_t *pstart
, int *plast_prot
,
1174 uint32_t end
, int prot
)
1177 prot1
= *plast_prot
;
1178 if (prot
!= prot1
) {
1179 if (*pstart
!= -1) {
1180 term_printf("%08x-%08x %08x %c%c%c\n",
1181 *pstart
, end
, end
- *pstart
,
1182 prot1
& PG_USER_MASK
? 'u' : '-',
1184 prot1
& PG_RW_MASK
? 'w' : '-');
1194 static void mem_info(void)
1197 int l1
, l2
, prot
, last_prot
;
1198 uint32_t pgd
, pde
, pte
, start
, end
;
1200 env
= mon_get_cpu();
1204 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1205 term_printf("PG disabled\n");
1208 pgd
= env
->cr
[3] & ~0xfff;
1211 for(l1
= 0; l1
< 1024; l1
++) {
1212 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1213 pde
= le32_to_cpu(pde
);
1215 if (pde
& PG_PRESENT_MASK
) {
1216 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1217 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1218 mem_print(&start
, &last_prot
, end
, prot
);
1220 for(l2
= 0; l2
< 1024; l2
++) {
1221 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1222 (uint8_t *)&pte
, 4);
1223 pte
= le32_to_cpu(pte
);
1224 end
= (l1
<< 22) + (l2
<< 12);
1225 if (pte
& PG_PRESENT_MASK
) {
1226 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1230 mem_print(&start
, &last_prot
, end
, prot
);
1235 mem_print(&start
, &last_prot
, end
, prot
);
1241 static void do_info_kqemu(void)
1247 env
= mon_get_cpu();
1249 term_printf("No cpu initialized yet");
1252 val
= env
->kqemu_enabled
;
1253 term_printf("kqemu support: ");
1257 term_printf("disabled\n");
1260 term_printf("enabled for user code\n");
1263 term_printf("enabled for user and kernel code\n");
1267 term_printf("kqemu support: not compiled\n");
1271 static void do_info_kvm(void)
1274 term_printf("kvm support: ");
1276 term_printf("enabled\n");
1278 term_printf("disabled\n");
1280 term_printf("kvm support: not compiled\n");
1284 #ifdef CONFIG_PROFILER
1288 int64_t kqemu_exec_count
;
1290 int64_t kqemu_ret_int_count
;
1291 int64_t kqemu_ret_excp_count
;
1292 int64_t kqemu_ret_intr_count
;
1294 static void do_info_profile(void)
1300 term_printf("async time %" PRId64
" (%0.3f)\n",
1301 dev_time
, dev_time
/ (double)ticks_per_sec
);
1302 term_printf("qemu time %" PRId64
" (%0.3f)\n",
1303 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1304 term_printf("kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%" PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%" PRId64
"\n",
1305 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1306 kqemu_time
/ (double)total
* 100.0,
1308 kqemu_ret_int_count
,
1309 kqemu_ret_excp_count
,
1310 kqemu_ret_intr_count
);
1313 kqemu_exec_count
= 0;
1315 kqemu_ret_int_count
= 0;
1316 kqemu_ret_excp_count
= 0;
1317 kqemu_ret_intr_count
= 0;
1319 kqemu_record_dump();
1323 static void do_info_profile(void)
1325 term_printf("Internal profiler not compiled\n");
1329 /* Capture support */
1330 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1332 static void do_info_capture (void)
1337 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1338 term_printf ("[%d]: ", i
);
1339 s
->ops
.info (s
->opaque
);
1343 static void do_stop_capture (int n
)
1348 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1350 s
->ops
.destroy (s
->opaque
);
1351 LIST_REMOVE (s
, entries
);
1359 static void do_wav_capture (const char *path
,
1360 int has_freq
, int freq
,
1361 int has_bits
, int bits
,
1362 int has_channels
, int nchannels
)
1366 s
= qemu_mallocz (sizeof (*s
));
1368 term_printf ("Not enough memory to add wave capture\n");
1372 freq
= has_freq
? freq
: 44100;
1373 bits
= has_bits
? bits
: 16;
1374 nchannels
= has_channels
? nchannels
: 2;
1376 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1377 term_printf ("Faied to add wave capture\n");
1380 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1384 #if defined(TARGET_I386)
1385 static void do_inject_nmi(int cpu_index
)
1389 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1390 if (env
->cpu_index
== cpu_index
) {
1391 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1397 static void do_balloon(int value
)
1399 ram_addr_t target
= value
;
1400 qemu_balloon(target
<< 20);
1403 static void do_info_balloon(void)
1407 actual
= qemu_balloon_status();
1408 if (kvm_enabled() && !kvm_has_sync_mmu())
1409 term_printf("Using KVM without synchronous MMU, ballooning disabled\n");
1410 else if (actual
== 0)
1411 term_printf("Ballooning not activated in VM\n");
1413 term_printf("balloon: actual=%d\n", (int)(actual
>> 20));
1416 static const term_cmd_t term_cmds
[] = {
1417 { "help|?", "s?", do_help
,
1418 "[cmd]", "show the help" },
1419 { "commit", "s", do_commit
,
1420 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1421 { "info", "s?", do_info
,
1422 "subcommand", "show various information about the system state" },
1423 { "q|quit", "", do_quit
,
1424 "", "quit the emulator" },
1425 { "eject", "-fB", do_eject
,
1426 "[-f] device", "eject a removable medium (use -f to force it)" },
1427 { "change", "BFs?", do_change
,
1428 "device filename [format]", "change a removable medium, optional format" },
1429 { "screendump", "F", do_screen_dump
,
1430 "filename", "save screen into PPM image 'filename'" },
1431 { "logfile", "F", do_logfile
,
1432 "filename", "output logs to 'filename'" },
1433 { "log", "s", do_log
,
1434 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1435 { "savevm", "s?", do_savevm
,
1436 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1437 { "loadvm", "s", do_loadvm
,
1438 "tag|id", "restore a VM snapshot from its tag or id" },
1439 { "delvm", "s", do_delvm
,
1440 "tag|id", "delete a VM snapshot from its tag or id" },
1441 { "stop", "", do_stop
,
1442 "", "stop emulation", },
1443 { "c|cont", "", do_cont
,
1444 "", "resume emulation", },
1445 #ifdef CONFIG_GDBSTUB
1446 { "gdbserver", "s?", do_gdbserver
,
1447 "[port]", "start gdbserver session (default port=1234)", },
1449 { "x", "/l", do_memory_dump
,
1450 "/fmt addr", "virtual memory dump starting at 'addr'", },
1451 { "xp", "/l", do_physical_memory_dump
,
1452 "/fmt addr", "physical memory dump starting at 'addr'", },
1453 { "p|print", "/l", do_print
,
1454 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1455 { "i", "/ii.", do_ioport_read
,
1456 "/fmt addr", "I/O port read" },
1458 { "sendkey", "si?", do_sendkey
,
1459 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1460 { "system_reset", "", do_system_reset
,
1461 "", "reset the system" },
1462 { "system_powerdown", "", do_system_powerdown
,
1463 "", "send system power down event" },
1464 { "sum", "ii", do_sum
,
1465 "addr size", "compute the checksum of a memory region" },
1466 { "usb_add", "s", do_usb_add
,
1467 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1468 { "usb_del", "s", do_usb_del
,
1469 "device", "remove USB device 'bus.addr'" },
1470 { "cpu", "i", do_cpu_set
,
1471 "index", "set the default CPU" },
1472 { "mouse_move", "sss?", do_mouse_move
,
1473 "dx dy [dz]", "send mouse move events" },
1474 { "mouse_button", "i", do_mouse_button
,
1475 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1476 { "mouse_set", "i", do_mouse_set
,
1477 "index", "set which mouse device receives events" },
1479 { "wavcapture", "si?i?i?", do_wav_capture
,
1480 "path [frequency bits channels]",
1481 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1483 { "stopcapture", "i", do_stop_capture
,
1484 "capture index", "stop capture" },
1485 { "memsave", "lis", do_memory_save
,
1486 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1487 { "pmemsave", "lis", do_physical_memory_save
,
1488 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1489 { "boot_set", "s", do_boot_set
,
1490 "bootdevice", "define new values for the boot device list" },
1491 #if defined(TARGET_I386)
1492 { "nmi", "i", do_inject_nmi
,
1493 "cpu", "inject an NMI on the given CPU", },
1495 { "migrate", "-ds", do_migrate
,
1496 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1497 { "migrate_cancel", "", do_migrate_cancel
,
1498 "", "cancel the current VM migration" },
1499 { "migrate_set_speed", "s", do_migrate_set_speed
,
1500 "value", "set maximum speed (in bytes) for migrations" },
1501 { "balloon", "i", do_balloon
,
1502 "target", "request VM to change it's memory allocation (in MB)" },
1506 static const term_cmd_t info_cmds
[] = {
1507 { "version", "", do_info_version
,
1508 "", "show the version of qemu" },
1509 { "network", "", do_info_network
,
1510 "", "show the network state" },
1511 { "chardev", "", qemu_chr_info
,
1512 "", "show the character devices" },
1513 { "block", "", do_info_block
,
1514 "", "show the block devices" },
1515 { "blockstats", "", do_info_blockstats
,
1516 "", "show block device statistics" },
1517 { "registers", "", do_info_registers
,
1518 "", "show the cpu registers" },
1519 { "cpus", "", do_info_cpus
,
1520 "", "show infos for each CPU" },
1521 { "history", "", do_info_history
,
1522 "", "show the command line history", },
1523 { "irq", "", irq_info
,
1524 "", "show the interrupts statistics (if available)", },
1525 { "pic", "", pic_info
,
1526 "", "show i8259 (PIC) state", },
1527 { "pci", "", pci_info
,
1528 "", "show PCI info", },
1529 #if defined(TARGET_I386)
1530 { "tlb", "", tlb_info
,
1531 "", "show virtual to physical memory mappings", },
1532 { "mem", "", mem_info
,
1533 "", "show the active virtual memory mappings", },
1535 { "jit", "", do_info_jit
,
1536 "", "show dynamic compiler info", },
1537 { "kqemu", "", do_info_kqemu
,
1538 "", "show kqemu information", },
1539 { "kvm", "", do_info_kvm
,
1540 "", "show kvm information", },
1541 { "usb", "", usb_info
,
1542 "", "show guest USB devices", },
1543 { "usbhost", "", usb_host_info
,
1544 "", "show host USB devices", },
1545 { "profile", "", do_info_profile
,
1546 "", "show profiling information", },
1547 { "capture", "", do_info_capture
,
1548 "", "show capture information" },
1549 { "snapshots", "", do_info_snapshots
,
1550 "", "show the currently saved VM snapshots" },
1551 { "pcmcia", "", pcmcia_info
,
1552 "", "show guest PCMCIA status" },
1553 { "mice", "", do_info_mice
,
1554 "", "show which guest mouse is receiving events" },
1555 { "vnc", "", do_info_vnc
,
1556 "", "show the vnc server status"},
1557 { "name", "", do_info_name
,
1558 "", "show the current VM name" },
1559 { "uuid", "", do_info_uuid
,
1560 "", "show the current VM UUID" },
1561 #if defined(TARGET_PPC)
1562 { "cpustats", "", do_info_cpu_stats
,
1563 "", "show CPU statistics", },
1565 #if defined(CONFIG_SLIRP)
1566 { "slirp", "", do_info_slirp
,
1567 "", "show SLIRP statistics", },
1569 { "migrate", "", do_info_migrate
, "", "show migration status" },
1570 { "balloon", "", do_info_balloon
,
1571 "", "show balloon information" },
1575 /*******************************************************************/
1577 static const char *pch
;
1578 static jmp_buf expr_env
;
1583 typedef struct MonitorDef
{
1586 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1590 #if defined(TARGET_I386)
1591 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1593 CPUState
*env
= mon_get_cpu();
1596 return env
->eip
+ env
->segs
[R_CS
].base
;
1600 #if defined(TARGET_PPC)
1601 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1603 CPUState
*env
= mon_get_cpu();
1611 for (i
= 0; i
< 8; i
++)
1612 u
|= env
->crf
[i
] << (32 - (4 * i
));
1617 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1619 CPUState
*env
= mon_get_cpu();
1625 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1627 CPUState
*env
= mon_get_cpu();
1633 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1635 CPUState
*env
= mon_get_cpu();
1638 return cpu_ppc_load_decr(env
);
1641 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1643 CPUState
*env
= mon_get_cpu();
1646 return cpu_ppc_load_tbu(env
);
1649 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1651 CPUState
*env
= mon_get_cpu();
1654 return cpu_ppc_load_tbl(env
);
1658 #if defined(TARGET_SPARC)
1659 #ifndef TARGET_SPARC64
1660 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1662 CPUState
*env
= mon_get_cpu();
1665 return GET_PSR(env
);
1669 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1671 CPUState
*env
= mon_get_cpu();
1674 return env
->regwptr
[val
];
1678 static const MonitorDef monitor_defs
[] = {
1681 #define SEG(name, seg) \
1682 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1683 { name ".base", offsetof(CPUState, segs[seg].base) },\
1684 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1686 { "eax", offsetof(CPUState
, regs
[0]) },
1687 { "ecx", offsetof(CPUState
, regs
[1]) },
1688 { "edx", offsetof(CPUState
, regs
[2]) },
1689 { "ebx", offsetof(CPUState
, regs
[3]) },
1690 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1691 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1692 { "esi", offsetof(CPUState
, regs
[6]) },
1693 { "edi", offsetof(CPUState
, regs
[7]) },
1694 #ifdef TARGET_X86_64
1695 { "r8", offsetof(CPUState
, regs
[8]) },
1696 { "r9", offsetof(CPUState
, regs
[9]) },
1697 { "r10", offsetof(CPUState
, regs
[10]) },
1698 { "r11", offsetof(CPUState
, regs
[11]) },
1699 { "r12", offsetof(CPUState
, regs
[12]) },
1700 { "r13", offsetof(CPUState
, regs
[13]) },
1701 { "r14", offsetof(CPUState
, regs
[14]) },
1702 { "r15", offsetof(CPUState
, regs
[15]) },
1704 { "eflags", offsetof(CPUState
, eflags
) },
1705 { "eip", offsetof(CPUState
, eip
) },
1712 { "pc", 0, monitor_get_pc
, },
1713 #elif defined(TARGET_PPC)
1714 /* General purpose registers */
1715 { "r0", offsetof(CPUState
, gpr
[0]) },
1716 { "r1", offsetof(CPUState
, gpr
[1]) },
1717 { "r2", offsetof(CPUState
, gpr
[2]) },
1718 { "r3", offsetof(CPUState
, gpr
[3]) },
1719 { "r4", offsetof(CPUState
, gpr
[4]) },
1720 { "r5", offsetof(CPUState
, gpr
[5]) },
1721 { "r6", offsetof(CPUState
, gpr
[6]) },
1722 { "r7", offsetof(CPUState
, gpr
[7]) },
1723 { "r8", offsetof(CPUState
, gpr
[8]) },
1724 { "r9", offsetof(CPUState
, gpr
[9]) },
1725 { "r10", offsetof(CPUState
, gpr
[10]) },
1726 { "r11", offsetof(CPUState
, gpr
[11]) },
1727 { "r12", offsetof(CPUState
, gpr
[12]) },
1728 { "r13", offsetof(CPUState
, gpr
[13]) },
1729 { "r14", offsetof(CPUState
, gpr
[14]) },
1730 { "r15", offsetof(CPUState
, gpr
[15]) },
1731 { "r16", offsetof(CPUState
, gpr
[16]) },
1732 { "r17", offsetof(CPUState
, gpr
[17]) },
1733 { "r18", offsetof(CPUState
, gpr
[18]) },
1734 { "r19", offsetof(CPUState
, gpr
[19]) },
1735 { "r20", offsetof(CPUState
, gpr
[20]) },
1736 { "r21", offsetof(CPUState
, gpr
[21]) },
1737 { "r22", offsetof(CPUState
, gpr
[22]) },
1738 { "r23", offsetof(CPUState
, gpr
[23]) },
1739 { "r24", offsetof(CPUState
, gpr
[24]) },
1740 { "r25", offsetof(CPUState
, gpr
[25]) },
1741 { "r26", offsetof(CPUState
, gpr
[26]) },
1742 { "r27", offsetof(CPUState
, gpr
[27]) },
1743 { "r28", offsetof(CPUState
, gpr
[28]) },
1744 { "r29", offsetof(CPUState
, gpr
[29]) },
1745 { "r30", offsetof(CPUState
, gpr
[30]) },
1746 { "r31", offsetof(CPUState
, gpr
[31]) },
1747 /* Floating point registers */
1748 { "f0", offsetof(CPUState
, fpr
[0]) },
1749 { "f1", offsetof(CPUState
, fpr
[1]) },
1750 { "f2", offsetof(CPUState
, fpr
[2]) },
1751 { "f3", offsetof(CPUState
, fpr
[3]) },
1752 { "f4", offsetof(CPUState
, fpr
[4]) },
1753 { "f5", offsetof(CPUState
, fpr
[5]) },
1754 { "f6", offsetof(CPUState
, fpr
[6]) },
1755 { "f7", offsetof(CPUState
, fpr
[7]) },
1756 { "f8", offsetof(CPUState
, fpr
[8]) },
1757 { "f9", offsetof(CPUState
, fpr
[9]) },
1758 { "f10", offsetof(CPUState
, fpr
[10]) },
1759 { "f11", offsetof(CPUState
, fpr
[11]) },
1760 { "f12", offsetof(CPUState
, fpr
[12]) },
1761 { "f13", offsetof(CPUState
, fpr
[13]) },
1762 { "f14", offsetof(CPUState
, fpr
[14]) },
1763 { "f15", offsetof(CPUState
, fpr
[15]) },
1764 { "f16", offsetof(CPUState
, fpr
[16]) },
1765 { "f17", offsetof(CPUState
, fpr
[17]) },
1766 { "f18", offsetof(CPUState
, fpr
[18]) },
1767 { "f19", offsetof(CPUState
, fpr
[19]) },
1768 { "f20", offsetof(CPUState
, fpr
[20]) },
1769 { "f21", offsetof(CPUState
, fpr
[21]) },
1770 { "f22", offsetof(CPUState
, fpr
[22]) },
1771 { "f23", offsetof(CPUState
, fpr
[23]) },
1772 { "f24", offsetof(CPUState
, fpr
[24]) },
1773 { "f25", offsetof(CPUState
, fpr
[25]) },
1774 { "f26", offsetof(CPUState
, fpr
[26]) },
1775 { "f27", offsetof(CPUState
, fpr
[27]) },
1776 { "f28", offsetof(CPUState
, fpr
[28]) },
1777 { "f29", offsetof(CPUState
, fpr
[29]) },
1778 { "f30", offsetof(CPUState
, fpr
[30]) },
1779 { "f31", offsetof(CPUState
, fpr
[31]) },
1780 { "fpscr", offsetof(CPUState
, fpscr
) },
1781 /* Next instruction pointer */
1782 { "nip|pc", offsetof(CPUState
, nip
) },
1783 { "lr", offsetof(CPUState
, lr
) },
1784 { "ctr", offsetof(CPUState
, ctr
) },
1785 { "decr", 0, &monitor_get_decr
, },
1786 { "ccr", 0, &monitor_get_ccr
, },
1787 /* Machine state register */
1788 { "msr", 0, &monitor_get_msr
, },
1789 { "xer", 0, &monitor_get_xer
, },
1790 { "tbu", 0, &monitor_get_tbu
, },
1791 { "tbl", 0, &monitor_get_tbl
, },
1792 #if defined(TARGET_PPC64)
1793 /* Address space register */
1794 { "asr", offsetof(CPUState
, asr
) },
1796 /* Segment registers */
1797 { "sdr1", offsetof(CPUState
, sdr1
) },
1798 { "sr0", offsetof(CPUState
, sr
[0]) },
1799 { "sr1", offsetof(CPUState
, sr
[1]) },
1800 { "sr2", offsetof(CPUState
, sr
[2]) },
1801 { "sr3", offsetof(CPUState
, sr
[3]) },
1802 { "sr4", offsetof(CPUState
, sr
[4]) },
1803 { "sr5", offsetof(CPUState
, sr
[5]) },
1804 { "sr6", offsetof(CPUState
, sr
[6]) },
1805 { "sr7", offsetof(CPUState
, sr
[7]) },
1806 { "sr8", offsetof(CPUState
, sr
[8]) },
1807 { "sr9", offsetof(CPUState
, sr
[9]) },
1808 { "sr10", offsetof(CPUState
, sr
[10]) },
1809 { "sr11", offsetof(CPUState
, sr
[11]) },
1810 { "sr12", offsetof(CPUState
, sr
[12]) },
1811 { "sr13", offsetof(CPUState
, sr
[13]) },
1812 { "sr14", offsetof(CPUState
, sr
[14]) },
1813 { "sr15", offsetof(CPUState
, sr
[15]) },
1814 /* Too lazy to put BATs and SPRs ... */
1815 #elif defined(TARGET_SPARC)
1816 { "g0", offsetof(CPUState
, gregs
[0]) },
1817 { "g1", offsetof(CPUState
, gregs
[1]) },
1818 { "g2", offsetof(CPUState
, gregs
[2]) },
1819 { "g3", offsetof(CPUState
, gregs
[3]) },
1820 { "g4", offsetof(CPUState
, gregs
[4]) },
1821 { "g5", offsetof(CPUState
, gregs
[5]) },
1822 { "g6", offsetof(CPUState
, gregs
[6]) },
1823 { "g7", offsetof(CPUState
, gregs
[7]) },
1824 { "o0", 0, monitor_get_reg
},
1825 { "o1", 1, monitor_get_reg
},
1826 { "o2", 2, monitor_get_reg
},
1827 { "o3", 3, monitor_get_reg
},
1828 { "o4", 4, monitor_get_reg
},
1829 { "o5", 5, monitor_get_reg
},
1830 { "o6", 6, monitor_get_reg
},
1831 { "o7", 7, monitor_get_reg
},
1832 { "l0", 8, monitor_get_reg
},
1833 { "l1", 9, monitor_get_reg
},
1834 { "l2", 10, monitor_get_reg
},
1835 { "l3", 11, monitor_get_reg
},
1836 { "l4", 12, monitor_get_reg
},
1837 { "l5", 13, monitor_get_reg
},
1838 { "l6", 14, monitor_get_reg
},
1839 { "l7", 15, monitor_get_reg
},
1840 { "i0", 16, monitor_get_reg
},
1841 { "i1", 17, monitor_get_reg
},
1842 { "i2", 18, monitor_get_reg
},
1843 { "i3", 19, monitor_get_reg
},
1844 { "i4", 20, monitor_get_reg
},
1845 { "i5", 21, monitor_get_reg
},
1846 { "i6", 22, monitor_get_reg
},
1847 { "i7", 23, monitor_get_reg
},
1848 { "pc", offsetof(CPUState
, pc
) },
1849 { "npc", offsetof(CPUState
, npc
) },
1850 { "y", offsetof(CPUState
, y
) },
1851 #ifndef TARGET_SPARC64
1852 { "psr", 0, &monitor_get_psr
, },
1853 { "wim", offsetof(CPUState
, wim
) },
1855 { "tbr", offsetof(CPUState
, tbr
) },
1856 { "fsr", offsetof(CPUState
, fsr
) },
1857 { "f0", offsetof(CPUState
, fpr
[0]) },
1858 { "f1", offsetof(CPUState
, fpr
[1]) },
1859 { "f2", offsetof(CPUState
, fpr
[2]) },
1860 { "f3", offsetof(CPUState
, fpr
[3]) },
1861 { "f4", offsetof(CPUState
, fpr
[4]) },
1862 { "f5", offsetof(CPUState
, fpr
[5]) },
1863 { "f6", offsetof(CPUState
, fpr
[6]) },
1864 { "f7", offsetof(CPUState
, fpr
[7]) },
1865 { "f8", offsetof(CPUState
, fpr
[8]) },
1866 { "f9", offsetof(CPUState
, fpr
[9]) },
1867 { "f10", offsetof(CPUState
, fpr
[10]) },
1868 { "f11", offsetof(CPUState
, fpr
[11]) },
1869 { "f12", offsetof(CPUState
, fpr
[12]) },
1870 { "f13", offsetof(CPUState
, fpr
[13]) },
1871 { "f14", offsetof(CPUState
, fpr
[14]) },
1872 { "f15", offsetof(CPUState
, fpr
[15]) },
1873 { "f16", offsetof(CPUState
, fpr
[16]) },
1874 { "f17", offsetof(CPUState
, fpr
[17]) },
1875 { "f18", offsetof(CPUState
, fpr
[18]) },
1876 { "f19", offsetof(CPUState
, fpr
[19]) },
1877 { "f20", offsetof(CPUState
, fpr
[20]) },
1878 { "f21", offsetof(CPUState
, fpr
[21]) },
1879 { "f22", offsetof(CPUState
, fpr
[22]) },
1880 { "f23", offsetof(CPUState
, fpr
[23]) },
1881 { "f24", offsetof(CPUState
, fpr
[24]) },
1882 { "f25", offsetof(CPUState
, fpr
[25]) },
1883 { "f26", offsetof(CPUState
, fpr
[26]) },
1884 { "f27", offsetof(CPUState
, fpr
[27]) },
1885 { "f28", offsetof(CPUState
, fpr
[28]) },
1886 { "f29", offsetof(CPUState
, fpr
[29]) },
1887 { "f30", offsetof(CPUState
, fpr
[30]) },
1888 { "f31", offsetof(CPUState
, fpr
[31]) },
1889 #ifdef TARGET_SPARC64
1890 { "f32", offsetof(CPUState
, fpr
[32]) },
1891 { "f34", offsetof(CPUState
, fpr
[34]) },
1892 { "f36", offsetof(CPUState
, fpr
[36]) },
1893 { "f38", offsetof(CPUState
, fpr
[38]) },
1894 { "f40", offsetof(CPUState
, fpr
[40]) },
1895 { "f42", offsetof(CPUState
, fpr
[42]) },
1896 { "f44", offsetof(CPUState
, fpr
[44]) },
1897 { "f46", offsetof(CPUState
, fpr
[46]) },
1898 { "f48", offsetof(CPUState
, fpr
[48]) },
1899 { "f50", offsetof(CPUState
, fpr
[50]) },
1900 { "f52", offsetof(CPUState
, fpr
[52]) },
1901 { "f54", offsetof(CPUState
, fpr
[54]) },
1902 { "f56", offsetof(CPUState
, fpr
[56]) },
1903 { "f58", offsetof(CPUState
, fpr
[58]) },
1904 { "f60", offsetof(CPUState
, fpr
[60]) },
1905 { "f62", offsetof(CPUState
, fpr
[62]) },
1906 { "asi", offsetof(CPUState
, asi
) },
1907 { "pstate", offsetof(CPUState
, pstate
) },
1908 { "cansave", offsetof(CPUState
, cansave
) },
1909 { "canrestore", offsetof(CPUState
, canrestore
) },
1910 { "otherwin", offsetof(CPUState
, otherwin
) },
1911 { "wstate", offsetof(CPUState
, wstate
) },
1912 { "cleanwin", offsetof(CPUState
, cleanwin
) },
1913 { "fprs", offsetof(CPUState
, fprs
) },
1919 static void expr_error(const char *fmt
)
1923 longjmp(expr_env
, 1);
1926 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
1927 static int get_monitor_def(target_long
*pval
, const char *name
)
1929 const MonitorDef
*md
;
1932 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
1933 if (compare_cmd(name
, md
->name
)) {
1934 if (md
->get_value
) {
1935 *pval
= md
->get_value(md
, md
->offset
);
1937 CPUState
*env
= mon_get_cpu();
1940 ptr
= (uint8_t *)env
+ md
->offset
;
1943 *pval
= *(int32_t *)ptr
;
1946 *pval
= *(target_long
*)ptr
;
1959 static void next(void)
1963 while (qemu_isspace(*pch
))
1968 static int64_t expr_sum(void);
1970 static int64_t expr_unary(void)
1993 expr_error("')' expected");
2000 expr_error("character constant expected");
2004 expr_error("missing terminating \' character");
2014 while ((*pch
>= 'a' && *pch
<= 'z') ||
2015 (*pch
>= 'A' && *pch
<= 'Z') ||
2016 (*pch
>= '0' && *pch
<= '9') ||
2017 *pch
== '_' || *pch
== '.') {
2018 if ((q
- buf
) < sizeof(buf
) - 1)
2022 while (qemu_isspace(*pch
))
2025 ret
= get_monitor_def(®
, buf
);
2027 expr_error("unknown register");
2029 expr_error("no cpu defined");
2034 expr_error("unexpected end of expression");
2038 #if TARGET_PHYS_ADDR_BITS > 32
2039 n
= strtoull(pch
, &p
, 0);
2041 n
= strtoul(pch
, &p
, 0);
2044 expr_error("invalid char in expression");
2047 while (qemu_isspace(*pch
))
2055 static int64_t expr_prod(void)
2063 if (op
!= '*' && op
!= '/' && op
!= '%')
2066 val2
= expr_unary();
2075 expr_error("division by zero");
2086 static int64_t expr_logic(void)
2094 if (op
!= '&' && op
!= '|' && op
!= '^')
2114 static int64_t expr_sum(void)
2122 if (op
!= '+' && op
!= '-')
2125 val2
= expr_logic();
2134 static int get_expr(int64_t *pval
, const char **pp
)
2137 if (setjmp(expr_env
)) {
2141 while (qemu_isspace(*pch
))
2148 static int get_str(char *buf
, int buf_size
, const char **pp
)
2156 while (qemu_isspace(*p
))
2166 while (*p
!= '\0' && *p
!= '\"') {
2182 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2185 if ((q
- buf
) < buf_size
- 1) {
2189 if ((q
- buf
) < buf_size
- 1) {
2196 qemu_printf("unterminated string\n");
2201 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2202 if ((q
- buf
) < buf_size
- 1) {
2213 static int default_fmt_format
= 'x';
2214 static int default_fmt_size
= 4;
2218 static void monitor_handle_command(const char *cmdline
)
2220 const char *p
, *pstart
, *typestr
;
2222 int c
, nb_args
, len
, i
, has_arg
;
2223 const term_cmd_t
*cmd
;
2226 void *str_allocated
[MAX_ARGS
];
2227 void *args
[MAX_ARGS
];
2228 void (*handler_0
)(void);
2229 void (*handler_1
)(void *arg0
);
2230 void (*handler_2
)(void *arg0
, void *arg1
);
2231 void (*handler_3
)(void *arg0
, void *arg1
, void *arg2
);
2232 void (*handler_4
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
);
2233 void (*handler_5
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2235 void (*handler_6
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2236 void *arg4
, void *arg5
);
2237 void (*handler_7
)(void *arg0
, void *arg1
, void *arg2
, void *arg3
,
2238 void *arg4
, void *arg5
, void *arg6
);
2241 term_printf("command='%s'\n", cmdline
);
2244 /* extract the command name */
2247 while (qemu_isspace(*p
))
2252 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2255 if (len
> sizeof(cmdname
) - 1)
2256 len
= sizeof(cmdname
) - 1;
2257 memcpy(cmdname
, pstart
, len
);
2258 cmdname
[len
] = '\0';
2260 /* find the command */
2261 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2262 if (compare_cmd(cmdname
, cmd
->name
))
2265 term_printf("unknown command: '%s'\n", cmdname
);
2269 for(i
= 0; i
< MAX_ARGS
; i
++)
2270 str_allocated
[i
] = NULL
;
2272 /* parse the parameters */
2273 typestr
= cmd
->args_type
;
2288 while (qemu_isspace(*p
))
2290 if (*typestr
== '?') {
2293 /* no optional string: NULL argument */
2298 ret
= get_str(buf
, sizeof(buf
), &p
);
2302 term_printf("%s: filename expected\n", cmdname
);
2305 term_printf("%s: block device name expected\n", cmdname
);
2308 term_printf("%s: string expected\n", cmdname
);
2313 str
= qemu_malloc(strlen(buf
) + 1);
2314 pstrcpy(str
, sizeof(buf
), buf
);
2315 str_allocated
[nb_args
] = str
;
2317 if (nb_args
>= MAX_ARGS
) {
2319 term_printf("%s: too many arguments\n", cmdname
);
2322 args
[nb_args
++] = str
;
2327 int count
, format
, size
;
2329 while (qemu_isspace(*p
))
2335 if (qemu_isdigit(*p
)) {
2337 while (qemu_isdigit(*p
)) {
2338 count
= count
* 10 + (*p
- '0');
2376 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2377 term_printf("invalid char in format: '%c'\n", *p
);
2381 format
= default_fmt_format
;
2382 if (format
!= 'i') {
2383 /* for 'i', not specifying a size gives -1 as size */
2385 size
= default_fmt_size
;
2386 default_fmt_size
= size
;
2388 default_fmt_format
= format
;
2391 format
= default_fmt_format
;
2392 if (format
!= 'i') {
2393 size
= default_fmt_size
;
2398 if (nb_args
+ 3 > MAX_ARGS
)
2400 args
[nb_args
++] = (void*)(long)count
;
2401 args
[nb_args
++] = (void*)(long)format
;
2402 args
[nb_args
++] = (void*)(long)size
;
2410 while (qemu_isspace(*p
))
2412 if (*typestr
== '?' || *typestr
== '.') {
2413 if (*typestr
== '?') {
2421 while (qemu_isspace(*p
))
2429 if (nb_args
>= MAX_ARGS
)
2431 args
[nb_args
++] = (void *)(long)has_arg
;
2433 if (nb_args
>= MAX_ARGS
)
2439 if (get_expr(&val
, &p
))
2443 if (nb_args
>= MAX_ARGS
)
2445 args
[nb_args
++] = (void *)(long)val
;
2447 if ((nb_args
+ 1) >= MAX_ARGS
)
2449 #if TARGET_PHYS_ADDR_BITS > 32
2450 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2452 args
[nb_args
++] = (void *)0;
2454 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2466 while (qemu_isspace(*p
))
2472 term_printf("%s: unsupported option -%c\n",
2479 if (nb_args
>= MAX_ARGS
)
2481 args
[nb_args
++] = (void *)(long)has_option
;
2486 term_printf("%s: unknown type '%c'\n", cmdname
, c
);
2490 /* check that all arguments were parsed */
2491 while (qemu_isspace(*p
))
2494 term_printf("%s: extraneous characters at the end of line\n",
2501 handler_0
= cmd
->handler
;
2505 handler_1
= cmd
->handler
;
2509 handler_2
= cmd
->handler
;
2510 handler_2(args
[0], args
[1]);
2513 handler_3
= cmd
->handler
;
2514 handler_3(args
[0], args
[1], args
[2]);
2517 handler_4
= cmd
->handler
;
2518 handler_4(args
[0], args
[1], args
[2], args
[3]);
2521 handler_5
= cmd
->handler
;
2522 handler_5(args
[0], args
[1], args
[2], args
[3], args
[4]);
2525 handler_6
= cmd
->handler
;
2526 handler_6(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2529 handler_7
= cmd
->handler
;
2530 handler_7(args
[0], args
[1], args
[2], args
[3], args
[4], args
[5], args
[6]);
2533 term_printf("unsupported number of arguments: %d\n", nb_args
);
2537 for(i
= 0; i
< MAX_ARGS
; i
++)
2538 qemu_free(str_allocated
[i
]);
2542 static void cmd_completion(const char *name
, const char *list
)
2544 const char *p
, *pstart
;
2553 p
= pstart
+ strlen(pstart
);
2555 if (len
> sizeof(cmd
) - 2)
2556 len
= sizeof(cmd
) - 2;
2557 memcpy(cmd
, pstart
, len
);
2559 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2560 add_completion(cmd
);
2568 static void file_completion(const char *input
)
2573 char file
[1024], file_prefix
[1024];
2577 p
= strrchr(input
, '/');
2580 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2581 pstrcpy(path
, sizeof(path
), ".");
2583 input_path_len
= p
- input
+ 1;
2584 memcpy(path
, input
, input_path_len
);
2585 if (input_path_len
> sizeof(path
) - 1)
2586 input_path_len
= sizeof(path
) - 1;
2587 path
[input_path_len
] = '\0';
2588 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2590 #ifdef DEBUG_COMPLETION
2591 term_printf("input='%s' path='%s' prefix='%s'\n", input
, path
, file_prefix
);
2593 ffs
= opendir(path
);
2601 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2602 memcpy(file
, input
, input_path_len
);
2603 if (input_path_len
< sizeof(file
))
2604 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2606 /* stat the file to find out if it's a directory.
2607 * In that case add a slash to speed up typing long paths
2610 if(S_ISDIR(sb
.st_mode
))
2611 pstrcat(file
, sizeof(file
), "/");
2612 add_completion(file
);
2618 static void block_completion_it(void *opaque
, const char *name
)
2620 const char *input
= opaque
;
2622 if (input
[0] == '\0' ||
2623 !strncmp(name
, (char *)input
, strlen(input
))) {
2624 add_completion(name
);
2628 /* NOTE: this parser is an approximate form of the real command parser */
2629 static void parse_cmdline(const char *cmdline
,
2630 int *pnb_args
, char **args
)
2639 while (qemu_isspace(*p
))
2643 if (nb_args
>= MAX_ARGS
)
2645 ret
= get_str(buf
, sizeof(buf
), &p
);
2646 args
[nb_args
] = qemu_strdup(buf
);
2651 *pnb_args
= nb_args
;
2654 void readline_find_completion(const char *cmdline
)
2656 const char *cmdname
;
2657 char *args
[MAX_ARGS
];
2658 int nb_args
, i
, len
;
2659 const char *ptype
, *str
;
2660 const term_cmd_t
*cmd
;
2663 parse_cmdline(cmdline
, &nb_args
, args
);
2664 #ifdef DEBUG_COMPLETION
2665 for(i
= 0; i
< nb_args
; i
++) {
2666 term_printf("arg%d = '%s'\n", i
, (char *)args
[i
]);
2670 /* if the line ends with a space, it means we want to complete the
2672 len
= strlen(cmdline
);
2673 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2674 if (nb_args
>= MAX_ARGS
)
2676 args
[nb_args
++] = qemu_strdup("");
2679 /* command completion */
2684 completion_index
= strlen(cmdname
);
2685 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2686 cmd_completion(cmdname
, cmd
->name
);
2689 /* find the command */
2690 for(cmd
= term_cmds
; cmd
->name
!= NULL
; cmd
++) {
2691 if (compare_cmd(args
[0], cmd
->name
))
2696 ptype
= cmd
->args_type
;
2697 for(i
= 0; i
< nb_args
- 2; i
++) {
2698 if (*ptype
!= '\0') {
2700 while (*ptype
== '?')
2704 str
= args
[nb_args
- 1];
2707 /* file completion */
2708 completion_index
= strlen(str
);
2709 file_completion(str
);
2712 /* block device name completion */
2713 completion_index
= strlen(str
);
2714 bdrv_iterate(block_completion_it
, (void *)str
);
2717 /* XXX: more generic ? */
2718 if (!strcmp(cmd
->name
, "info")) {
2719 completion_index
= strlen(str
);
2720 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2721 cmd_completion(str
, cmd
->name
);
2723 } else if (!strcmp(cmd
->name
, "sendkey")) {
2724 completion_index
= strlen(str
);
2725 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2726 cmd_completion(str
, key
->name
);
2734 for(i
= 0; i
< nb_args
; i
++)
2738 static int term_can_read(void *opaque
)
2743 static void term_read(void *opaque
, const uint8_t *buf
, int size
)
2746 for(i
= 0; i
< size
; i
++)
2747 readline_handle_byte(buf
[i
]);
2750 static int monitor_suspended
;
2752 static void monitor_handle_command1(void *opaque
, const char *cmdline
)
2754 monitor_handle_command(cmdline
);
2755 if (!monitor_suspended
)
2756 monitor_start_input();
2758 monitor_suspended
= 2;
2761 void monitor_suspend(void)
2763 monitor_suspended
= 1;
2766 void monitor_resume(void)
2768 if (monitor_suspended
== 2)
2769 monitor_start_input();
2770 monitor_suspended
= 0;
2773 static void monitor_start_input(void)
2775 readline_start("(qemu) ", 0, monitor_handle_command1
, NULL
);
2778 static void term_event(void *opaque
, int event
)
2780 if (event
!= CHR_EVENT_RESET
)
2784 term_printf("QEMU %s monitor - type 'help' for more information\n",
2786 monitor_start_input();
2789 static int is_first_init
= 1;
2791 void monitor_init(CharDriverState
*hd
, int show_banner
)
2795 if (is_first_init
) {
2796 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
2799 for (i
= 0; i
< MAX_MON
; i
++) {
2800 monitor_hd
[i
] = NULL
;
2804 for (i
= 0; i
< MAX_MON
; i
++) {
2805 if (monitor_hd
[i
] == NULL
) {
2811 hide_banner
= !show_banner
;
2813 qemu_chr_add_handlers(hd
, term_can_read
, term_read
, term_event
, NULL
);
2815 readline_start("", 0, monitor_handle_command1
, NULL
);
2818 /* XXX: use threads ? */
2819 /* modal monitor readline */
2820 static int monitor_readline_started
;
2821 static char *monitor_readline_buf
;
2822 static int monitor_readline_buf_size
;
2824 static void monitor_readline_cb(void *opaque
, const char *input
)
2826 pstrcpy(monitor_readline_buf
, monitor_readline_buf_size
, input
);
2827 monitor_readline_started
= 0;
2830 void monitor_readline(const char *prompt
, int is_password
,
2831 char *buf
, int buf_size
)
2834 int old_focus
[MAX_MON
];
2837 for (i
= 0; i
< MAX_MON
; i
++) {
2839 if (monitor_hd
[i
]) {
2840 old_focus
[i
] = monitor_hd
[i
]->focus
;
2841 monitor_hd
[i
]->focus
= 0;
2842 qemu_chr_send_event(monitor_hd
[i
], CHR_EVENT_FOCUS
);
2847 readline_start(prompt
, is_password
, monitor_readline_cb
, NULL
);
2848 monitor_readline_buf
= buf
;
2849 monitor_readline_buf_size
= buf_size
;
2850 monitor_readline_started
= 1;
2851 while (monitor_readline_started
) {
2854 /* restore original focus */
2856 for (i
= 0; i
< MAX_MON
; i
++)
2858 monitor_hd
[i
]->focus
= old_focus
[i
];