4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "hw/pcmcia.h"
30 #include "hw/watchdog.h"
33 #include "qemu-char.h"
39 #include "audio/audio.h"
42 #include "qemu-timer.h"
43 #include "migration.h"
51 //#define DEBUG_COMPLETION
57 * 'B' block device name
58 * 's' string (accept optional quote)
60 * 'l' target long (32 or 64 bit)
61 * '/' optional gdb-like print format (like "/10x")
63 * '?' optional type (for 'F', 's' and 'i')
67 typedef struct mon_cmd_t
{
69 const char *args_type
;
83 BlockDriverCompletionFunc
*password_completion_cb
;
84 void *password_opaque
;
85 LIST_ENTRY(Monitor
) entry
;
88 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
90 static const mon_cmd_t mon_cmds
[];
91 static const mon_cmd_t info_cmds
[];
93 Monitor
*cur_mon
= NULL
;
95 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
98 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
100 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
102 readline_show_prompt(mon
->rs
);
105 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
109 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
110 /* prompt is printed on return from the command handler */
113 monitor_printf(mon
, "terminal does not support password prompting\n");
118 void monitor_flush(Monitor
*mon
)
120 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
121 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
122 mon
->outbuf_index
= 0;
126 /* flush at every end of line or if the buffer is full */
127 static void monitor_puts(Monitor
*mon
, const char *str
)
139 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
140 mon
->outbuf
[mon
->outbuf_index
++] = c
;
141 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
147 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
150 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
151 monitor_puts(mon
, buf
);
154 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
158 monitor_vprintf(mon
, fmt
, ap
);
162 void monitor_print_filename(Monitor
*mon
, const char *filename
)
166 for (i
= 0; filename
[i
]; i
++) {
167 switch (filename
[i
]) {
171 monitor_printf(mon
, "\\%c", filename
[i
]);
174 monitor_printf(mon
, "\\t");
177 monitor_printf(mon
, "\\r");
180 monitor_printf(mon
, "\\n");
183 monitor_printf(mon
, "%c", filename
[i
]);
189 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
193 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
198 static int compare_cmd(const char *name
, const char *list
)
200 const char *p
, *pstart
;
208 p
= pstart
+ strlen(pstart
);
209 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
218 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
219 const char *prefix
, const char *name
)
221 const mon_cmd_t
*cmd
;
223 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
224 if (!name
|| !strcmp(name
, cmd
->name
))
225 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
226 cmd
->params
, cmd
->help
);
230 static void help_cmd(Monitor
*mon
, const char *name
)
232 if (name
&& !strcmp(name
, "info")) {
233 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
235 help_cmd_dump(mon
, mon_cmds
, "", name
);
236 if (name
&& !strcmp(name
, "log")) {
237 const CPULogItem
*item
;
238 monitor_printf(mon
, "Log items (comma separated):\n");
239 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
240 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
241 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
247 static void do_commit(Monitor
*mon
, const char *device
)
251 all_devices
= !strcmp(device
, "all");
252 for (i
= 0; i
< nb_drives
; i
++) {
254 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
255 bdrv_commit(drives_table
[i
].bdrv
);
259 static void do_info(Monitor
*mon
, const char *item
)
261 const mon_cmd_t
*cmd
;
262 void (*handler
)(Monitor
*);
266 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
267 if (compare_cmd(item
, cmd
->name
))
271 help_cmd(mon
, "info");
274 handler
= cmd
->handler
;
278 static void do_info_version(Monitor
*mon
)
280 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
283 static void do_info_name(Monitor
*mon
)
286 monitor_printf(mon
, "%s\n", qemu_name
);
289 #if defined(TARGET_I386)
290 static void do_info_hpet(Monitor
*mon
)
292 monitor_printf(mon
, "HPET is %s by QEMU\n",
293 (no_hpet
) ? "disabled" : "enabled");
297 static void do_info_uuid(Monitor
*mon
)
299 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
300 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
301 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
302 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
303 qemu_uuid
[14], qemu_uuid
[15]);
306 /* get the current CPU defined by the user */
307 static int mon_set_cpu(int cpu_index
)
311 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
312 if (env
->cpu_index
== cpu_index
) {
313 cur_mon
->mon_cpu
= env
;
320 static CPUState
*mon_get_cpu(void)
322 if (!cur_mon
->mon_cpu
) {
325 cpu_synchronize_state(cur_mon
->mon_cpu
, 0);
326 return cur_mon
->mon_cpu
;
329 static void do_info_registers(Monitor
*mon
)
336 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
339 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
344 static void do_info_cpus(Monitor
*mon
)
348 /* just to set the default cpu if not already done */
351 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
352 cpu_synchronize_state(env
, 0);
353 monitor_printf(mon
, "%c CPU #%d:",
354 (env
== mon
->mon_cpu
) ? '*' : ' ',
356 #if defined(TARGET_I386)
357 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
358 env
->eip
+ env
->segs
[R_CS
].base
);
359 #elif defined(TARGET_PPC)
360 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
361 #elif defined(TARGET_SPARC)
362 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
364 #elif defined(TARGET_MIPS)
365 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
368 monitor_printf(mon
, " (halted)");
369 monitor_printf(mon
," thread_id=%d", env
->thread_id
);
370 monitor_printf(mon
, "\n");
374 static void do_cpu_set(Monitor
*mon
, int index
)
376 if (mon_set_cpu(index
) < 0)
377 monitor_printf(mon
, "Invalid CPU index\n");
380 static void do_cpu_set_nr(Monitor
*mon
, int value
, const char *status
)
384 if (!strcmp(status
, "online"))
386 else if (!strcmp(status
, "offline"))
389 monitor_printf(mon
, "invalid status: %s\n", status
);
392 #if defined(TARGET_I386) || defined(TARGET_X86_64)
393 qemu_system_cpu_hot_add(value
, state
);
397 static void do_info_jit(Monitor
*mon
)
399 dump_exec_info((FILE *)mon
, monitor_fprintf
);
402 static void do_info_history(Monitor
*mon
)
411 str
= readline_get_history(mon
->rs
, i
);
414 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
419 #if defined(TARGET_PPC)
420 /* XXX: not implemented in other targets */
421 static void do_info_cpu_stats(Monitor
*mon
)
426 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
430 static void do_quit(Monitor
*mon
)
435 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
437 if (bdrv_is_inserted(bs
)) {
439 if (!bdrv_is_removable(bs
)) {
440 monitor_printf(mon
, "device is not removable\n");
443 if (bdrv_is_locked(bs
)) {
444 monitor_printf(mon
, "device is locked\n");
453 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
455 BlockDriverState
*bs
;
457 bs
= bdrv_find(filename
);
459 monitor_printf(mon
, "device not found\n");
462 eject_device(mon
, bs
, force
);
465 static void do_change_block(Monitor
*mon
, const char *device
,
466 const char *filename
, const char *fmt
)
468 BlockDriverState
*bs
;
469 BlockDriver
*drv
= NULL
;
471 bs
= bdrv_find(device
);
473 monitor_printf(mon
, "device not found\n");
477 drv
= bdrv_find_format(fmt
);
479 monitor_printf(mon
, "invalid format %s\n", fmt
);
483 if (eject_device(mon
, bs
, 0) < 0)
485 bdrv_open2(bs
, filename
, 0, drv
);
486 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
489 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
492 if (vnc_display_password(NULL
, password
) < 0)
493 monitor_printf(mon
, "could not set VNC server password\n");
495 monitor_read_command(mon
, 1);
498 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
500 if (strcmp(target
, "passwd") == 0 ||
501 strcmp(target
, "password") == 0) {
504 strncpy(password
, arg
, sizeof(password
));
505 password
[sizeof(password
) - 1] = '\0';
506 change_vnc_password_cb(mon
, password
, NULL
);
508 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
511 if (vnc_display_open(NULL
, target
) < 0)
512 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
516 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
519 if (strcmp(device
, "vnc") == 0) {
520 do_change_vnc(mon
, target
, arg
);
522 do_change_block(mon
, device
, target
, arg
);
526 static void do_screen_dump(Monitor
*mon
, const char *filename
)
528 vga_hw_screen_dump(filename
);
531 static void do_logfile(Monitor
*mon
, const char *filename
)
533 cpu_set_log_filename(filename
);
536 static void do_log(Monitor
*mon
, const char *items
)
540 if (!strcmp(items
, "none")) {
543 mask
= cpu_str_to_log_mask(items
);
545 help_cmd(mon
, "log");
552 static void do_singlestep(Monitor
*mon
, const char *option
)
554 if (!option
|| !strcmp(option
, "on")) {
556 } else if (!strcmp(option
, "off")) {
559 monitor_printf(mon
, "unexpected option %s\n", option
);
563 static void do_stop(Monitor
*mon
)
565 vm_stop(EXCP_INTERRUPT
);
568 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
570 struct bdrv_iterate_context
{
575 static void do_cont(Monitor
*mon
)
577 struct bdrv_iterate_context context
= { mon
, 0 };
579 bdrv_iterate(encrypted_bdrv_it
, &context
);
580 /* only resume the vm if all keys are set and valid */
585 static void bdrv_key_cb(void *opaque
, int err
)
587 Monitor
*mon
= opaque
;
589 /* another key was set successfully, retry to continue */
594 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
596 struct bdrv_iterate_context
*context
= opaque
;
598 if (!context
->err
&& bdrv_key_required(bs
)) {
599 context
->err
= -EBUSY
;
600 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
605 static void do_gdbserver(Monitor
*mon
, const char *device
)
608 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
609 if (gdbserver_start(device
) < 0) {
610 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
612 } else if (strcmp(device
, "none") == 0) {
613 monitor_printf(mon
, "Disabled gdbserver\n");
615 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
620 static void do_watchdog_action(Monitor
*mon
, const char *action
)
622 if (select_watchdog_action(action
) == -1) {
623 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
627 static void monitor_printc(Monitor
*mon
, int c
)
629 monitor_printf(mon
, "'");
632 monitor_printf(mon
, "\\'");
635 monitor_printf(mon
, "\\\\");
638 monitor_printf(mon
, "\\n");
641 monitor_printf(mon
, "\\r");
644 if (c
>= 32 && c
<= 126) {
645 monitor_printf(mon
, "%c", c
);
647 monitor_printf(mon
, "\\x%02x", c
);
651 monitor_printf(mon
, "'");
654 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
655 target_phys_addr_t addr
, int is_physical
)
658 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
666 if (!env
&& !is_physical
)
671 } else if (wsize
== 4) {
674 /* as default we use the current CS size */
678 if ((env
->efer
& MSR_EFER_LMA
) &&
679 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
683 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
688 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
697 nb_per_line
= line_size
/ wsize
;
702 max_digits
= (wsize
* 8 + 2) / 3;
706 max_digits
= (wsize
* 8) / 4;
710 max_digits
= (wsize
* 8 * 10 + 32) / 33;
719 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
721 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
726 cpu_physical_memory_rw(addr
, buf
, l
, 0);
731 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
732 monitor_printf(mon
, " Cannot access memory\n");
741 v
= ldub_raw(buf
+ i
);
744 v
= lduw_raw(buf
+ i
);
747 v
= (uint32_t)ldl_raw(buf
+ i
);
750 v
= ldq_raw(buf
+ i
);
753 monitor_printf(mon
, " ");
756 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
759 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
762 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
765 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
768 monitor_printc(mon
, v
);
773 monitor_printf(mon
, "\n");
779 #if TARGET_LONG_BITS == 64
780 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
782 #define GET_TLONG(h, l) (l)
785 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
786 uint32_t addrh
, uint32_t addrl
)
788 target_long addr
= GET_TLONG(addrh
, addrl
);
789 memory_dump(mon
, count
, format
, size
, addr
, 0);
792 #if TARGET_PHYS_ADDR_BITS > 32
793 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
795 #define GET_TPHYSADDR(h, l) (l)
798 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
799 int size
, uint32_t addrh
, uint32_t addrl
)
802 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
803 memory_dump(mon
, count
, format
, size
, addr
, 1);
806 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
807 unsigned int valh
, unsigned int vall
)
809 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
810 #if TARGET_PHYS_ADDR_BITS == 32
813 monitor_printf(mon
, "%#o", val
);
816 monitor_printf(mon
, "%#x", val
);
819 monitor_printf(mon
, "%u", val
);
823 monitor_printf(mon
, "%d", val
);
826 monitor_printc(mon
, val
);
832 monitor_printf(mon
, "%#" PRIo64
, val
);
835 monitor_printf(mon
, "%#" PRIx64
, val
);
838 monitor_printf(mon
, "%" PRIu64
, val
);
842 monitor_printf(mon
, "%" PRId64
, val
);
845 monitor_printc(mon
, val
);
849 monitor_printf(mon
, "\n");
852 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
853 uint32_t size
, const char *filename
)
856 target_long addr
= GET_TLONG(valh
, vall
);
865 f
= fopen(filename
, "wb");
867 monitor_printf(mon
, "could not open '%s'\n", filename
);
874 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
875 fwrite(buf
, 1, l
, f
);
882 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
883 unsigned int vall
, uint32_t size
,
884 const char *filename
)
889 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
891 f
= fopen(filename
, "wb");
893 monitor_printf(mon
, "could not open '%s'\n", filename
);
900 cpu_physical_memory_rw(addr
, buf
, l
, 0);
901 fwrite(buf
, 1, l
, f
);
909 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
916 for(addr
= start
; addr
< (start
+ size
); addr
++) {
917 cpu_physical_memory_rw(addr
, buf
, 1, 0);
918 /* BSD sum algorithm ('sum' Unix command) */
919 sum
= (sum
>> 1) | (sum
<< 15);
922 monitor_printf(mon
, "%05d\n", sum
);
930 static const KeyDef key_defs
[] = {
957 { 0x0e, "backspace" },
994 { 0x37, "asterisk" },
997 { 0x3a, "caps_lock" },
1008 { 0x45, "num_lock" },
1009 { 0x46, "scroll_lock" },
1011 { 0xb5, "kp_divide" },
1012 { 0x37, "kp_multiply" },
1013 { 0x4a, "kp_subtract" },
1015 { 0x9c, "kp_enter" },
1016 { 0x53, "kp_decimal" },
1049 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1064 { 0xfe, "compose" },
1069 static int get_keycode(const char *key
)
1075 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1076 if (!strcmp(key
, p
->name
))
1079 if (strstart(key
, "0x", NULL
)) {
1080 ret
= strtoul(key
, &endp
, 0);
1081 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1087 #define MAX_KEYCODES 16
1088 static uint8_t keycodes
[MAX_KEYCODES
];
1089 static int nb_pending_keycodes
;
1090 static QEMUTimer
*key_timer
;
1092 static void release_keys(void *opaque
)
1096 while (nb_pending_keycodes
> 0) {
1097 nb_pending_keycodes
--;
1098 keycode
= keycodes
[nb_pending_keycodes
];
1100 kbd_put_keycode(0xe0);
1101 kbd_put_keycode(keycode
| 0x80);
1105 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1108 char keyname_buf
[16];
1110 int keyname_len
, keycode
, i
;
1112 if (nb_pending_keycodes
> 0) {
1113 qemu_del_timer(key_timer
);
1120 separator
= strchr(string
, '-');
1121 keyname_len
= separator
? separator
- string
: strlen(string
);
1122 if (keyname_len
> 0) {
1123 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1124 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1125 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1128 if (i
== MAX_KEYCODES
) {
1129 monitor_printf(mon
, "too many keys\n");
1132 keyname_buf
[keyname_len
] = 0;
1133 keycode
= get_keycode(keyname_buf
);
1135 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1138 keycodes
[i
++] = keycode
;
1142 string
= separator
+ 1;
1144 nb_pending_keycodes
= i
;
1145 /* key down events */
1146 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1147 keycode
= keycodes
[i
];
1149 kbd_put_keycode(0xe0);
1150 kbd_put_keycode(keycode
& 0x7f);
1152 /* delayed key up events */
1153 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1154 muldiv64(ticks_per_sec
, hold_time
, 1000));
1157 static int mouse_button_state
;
1159 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1163 dx
= strtol(dx_str
, NULL
, 0);
1164 dy
= strtol(dy_str
, NULL
, 0);
1167 dz
= strtol(dz_str
, NULL
, 0);
1168 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1171 static void do_mouse_button(Monitor
*mon
, int button_state
)
1173 mouse_button_state
= button_state
;
1174 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1177 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1178 int addr
, int has_index
, int index
)
1184 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1192 val
= cpu_inb(NULL
, addr
);
1196 val
= cpu_inw(NULL
, addr
);
1200 val
= cpu_inl(NULL
, addr
);
1204 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1205 suffix
, addr
, size
* 2, val
);
1208 /* boot_set handler */
1209 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1210 static void *boot_opaque
;
1212 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1214 qemu_boot_set_handler
= func
;
1215 boot_opaque
= opaque
;
1218 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1222 if (qemu_boot_set_handler
) {
1223 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1225 monitor_printf(mon
, "boot device list now set to %s\n",
1228 monitor_printf(mon
, "setting boot device list failed with "
1231 monitor_printf(mon
, "no function defined to set boot device list for "
1232 "this architecture\n");
1236 static void do_system_reset(Monitor
*mon
)
1238 qemu_system_reset_request();
1241 static void do_system_powerdown(Monitor
*mon
)
1243 qemu_system_powerdown_request();
1246 #if defined(TARGET_I386)
1247 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1249 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1252 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1253 pte
& PG_PSE_MASK
? 'P' : '-',
1254 pte
& PG_DIRTY_MASK
? 'D' : '-',
1255 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1256 pte
& PG_PCD_MASK
? 'C' : '-',
1257 pte
& PG_PWT_MASK
? 'T' : '-',
1258 pte
& PG_USER_MASK
? 'U' : '-',
1259 pte
& PG_RW_MASK
? 'W' : '-');
1262 static void tlb_info(Monitor
*mon
)
1266 uint32_t pgd
, pde
, pte
;
1268 env
= mon_get_cpu();
1272 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1273 monitor_printf(mon
, "PG disabled\n");
1276 pgd
= env
->cr
[3] & ~0xfff;
1277 for(l1
= 0; l1
< 1024; l1
++) {
1278 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1279 pde
= le32_to_cpu(pde
);
1280 if (pde
& PG_PRESENT_MASK
) {
1281 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1282 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1284 for(l2
= 0; l2
< 1024; l2
++) {
1285 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1286 (uint8_t *)&pte
, 4);
1287 pte
= le32_to_cpu(pte
);
1288 if (pte
& PG_PRESENT_MASK
) {
1289 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1299 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1300 uint32_t end
, int prot
)
1303 prot1
= *plast_prot
;
1304 if (prot
!= prot1
) {
1305 if (*pstart
!= -1) {
1306 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1307 *pstart
, end
, end
- *pstart
,
1308 prot1
& PG_USER_MASK
? 'u' : '-',
1310 prot1
& PG_RW_MASK
? 'w' : '-');
1320 static void mem_info(Monitor
*mon
)
1323 int l1
, l2
, prot
, last_prot
;
1324 uint32_t pgd
, pde
, pte
, start
, end
;
1326 env
= mon_get_cpu();
1330 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1331 monitor_printf(mon
, "PG disabled\n");
1334 pgd
= env
->cr
[3] & ~0xfff;
1337 for(l1
= 0; l1
< 1024; l1
++) {
1338 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1339 pde
= le32_to_cpu(pde
);
1341 if (pde
& PG_PRESENT_MASK
) {
1342 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1343 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1344 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1346 for(l2
= 0; l2
< 1024; l2
++) {
1347 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1348 (uint8_t *)&pte
, 4);
1349 pte
= le32_to_cpu(pte
);
1350 end
= (l1
<< 22) + (l2
<< 12);
1351 if (pte
& PG_PRESENT_MASK
) {
1352 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1356 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1361 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1367 #if defined(TARGET_SH4)
1369 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1371 monitor_printf(mon
, " tlb%i:\t"
1372 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1373 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1374 "dirty=%hhu writethrough=%hhu\n",
1376 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1377 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1381 static void tlb_info(Monitor
*mon
)
1383 CPUState
*env
= mon_get_cpu();
1386 monitor_printf (mon
, "ITLB:\n");
1387 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1388 print_tlb (mon
, i
, &env
->itlb
[i
]);
1389 monitor_printf (mon
, "UTLB:\n");
1390 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1391 print_tlb (mon
, i
, &env
->utlb
[i
]);
1396 static void do_info_kqemu(Monitor
*mon
)
1402 env
= mon_get_cpu();
1404 monitor_printf(mon
, "No cpu initialized yet");
1407 val
= env
->kqemu_enabled
;
1408 monitor_printf(mon
, "kqemu support: ");
1412 monitor_printf(mon
, "disabled\n");
1415 monitor_printf(mon
, "enabled for user code\n");
1418 monitor_printf(mon
, "enabled for user and kernel code\n");
1422 monitor_printf(mon
, "kqemu support: not compiled\n");
1426 static void do_info_kvm(Monitor
*mon
)
1428 #if defined(USE_KVM) || defined(CONFIG_KVM)
1429 monitor_printf(mon
, "kvm support: ");
1431 monitor_printf(mon
, "enabled\n");
1433 monitor_printf(mon
, "disabled\n");
1435 monitor_printf(mon
, "kvm support: not compiled\n");
1439 static void do_info_numa(Monitor
*mon
)
1444 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1445 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1446 monitor_printf(mon
, "node %d cpus:", i
);
1447 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1448 if (env
->numa_node
== i
) {
1449 monitor_printf(mon
, " %d", env
->cpu_index
);
1452 monitor_printf(mon
, "\n");
1453 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1458 #ifdef CONFIG_PROFILER
1462 int64_t kqemu_exec_count
;
1464 int64_t kqemu_ret_int_count
;
1465 int64_t kqemu_ret_excp_count
;
1466 int64_t kqemu_ret_intr_count
;
1468 static void do_info_profile(Monitor
*mon
)
1474 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1475 dev_time
, dev_time
/ (double)ticks_per_sec
);
1476 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1477 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1478 monitor_printf(mon
, "kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%"
1479 PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%"
1481 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1482 kqemu_time
/ (double)total
* 100.0,
1484 kqemu_ret_int_count
,
1485 kqemu_ret_excp_count
,
1486 kqemu_ret_intr_count
);
1489 kqemu_exec_count
= 0;
1491 kqemu_ret_int_count
= 0;
1492 kqemu_ret_excp_count
= 0;
1493 kqemu_ret_intr_count
= 0;
1495 kqemu_record_dump();
1499 static void do_info_profile(Monitor
*mon
)
1501 monitor_printf(mon
, "Internal profiler not compiled\n");
1505 /* Capture support */
1506 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1508 static void do_info_capture(Monitor
*mon
)
1513 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1514 monitor_printf(mon
, "[%d]: ", i
);
1515 s
->ops
.info (s
->opaque
);
1519 static void do_stop_capture(Monitor
*mon
, int n
)
1524 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1526 s
->ops
.destroy (s
->opaque
);
1527 LIST_REMOVE (s
, entries
);
1535 static void do_wav_capture(Monitor
*mon
, const char *path
,
1536 int has_freq
, int freq
,
1537 int has_bits
, int bits
,
1538 int has_channels
, int nchannels
)
1542 s
= qemu_mallocz (sizeof (*s
));
1544 freq
= has_freq
? freq
: 44100;
1545 bits
= has_bits
? bits
: 16;
1546 nchannels
= has_channels
? nchannels
: 2;
1548 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1549 monitor_printf(mon
, "Faied to add wave capture\n");
1552 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1556 #if defined(TARGET_I386)
1557 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1561 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1562 if (env
->cpu_index
== cpu_index
) {
1564 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1566 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1572 static void do_info_status(Monitor
*mon
)
1576 monitor_printf(mon
, "VM status: running (single step mode)\n");
1578 monitor_printf(mon
, "VM status: running\n");
1581 monitor_printf(mon
, "VM status: paused\n");
1585 static void do_balloon(Monitor
*mon
, int value
)
1587 ram_addr_t target
= value
;
1588 qemu_balloon(target
<< 20);
1591 static void do_info_balloon(Monitor
*mon
)
1595 actual
= qemu_balloon_status();
1596 if (kvm_enabled() && !kvm_has_sync_mmu())
1597 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1598 "ballooning disabled\n");
1599 else if (actual
== 0)
1600 monitor_printf(mon
, "Ballooning not activated in VM\n");
1602 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1605 static void do_acl(Monitor
*mon
,
1606 const char *command
,
1607 const char *aclname
,
1614 acl
= qemu_acl_find(aclname
);
1616 monitor_printf(mon
, "acl: unknown list '%s'\n", aclname
);
1620 if (strcmp(command
, "show") == 0) {
1622 qemu_acl_entry
*entry
;
1623 monitor_printf(mon
, "policy: %s\n",
1624 acl
->defaultDeny
? "deny" : "allow");
1625 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1627 monitor_printf(mon
, "%d: %s %s\n", i
,
1628 entry
->deny
? "deny" : "allow",
1631 } else if (strcmp(command
, "reset") == 0) {
1632 qemu_acl_reset(acl
);
1633 monitor_printf(mon
, "acl: removed all rules\n");
1634 } else if (strcmp(command
, "policy") == 0) {
1636 monitor_printf(mon
, "acl: missing policy parameter\n");
1640 if (strcmp(match
, "allow") == 0) {
1641 acl
->defaultDeny
= 0;
1642 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1643 } else if (strcmp(match
, "deny") == 0) {
1644 acl
->defaultDeny
= 1;
1645 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1647 monitor_printf(mon
, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match
);
1649 } else if ((strcmp(command
, "allow") == 0) ||
1650 (strcmp(command
, "deny") == 0)) {
1651 int deny
= strcmp(command
, "deny") == 0 ? 1 : 0;
1655 monitor_printf(mon
, "acl: missing match parameter\n");
1660 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1662 ret
= qemu_acl_append(acl
, deny
, match
);
1664 monitor_printf(mon
, "acl: unable to add acl entry\n");
1666 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1667 } else if (strcmp(command
, "remove") == 0) {
1671 monitor_printf(mon
, "acl: missing match parameter\n");
1675 ret
= qemu_acl_remove(acl
, match
);
1677 monitor_printf(mon
, "acl: no matching acl entry\n");
1679 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1681 monitor_printf(mon
, "acl: unknown command '%s'\n", command
);
1685 /* Please update qemu-doc.texi when adding or changing commands */
1686 static const mon_cmd_t mon_cmds
[] = {
1687 { "help|?", "s?", help_cmd
,
1688 "[cmd]", "show the help" },
1689 { "commit", "s", do_commit
,
1690 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1691 { "info", "s?", do_info
,
1692 "[subcommand]", "show various information about the system state" },
1693 { "q|quit", "", do_quit
,
1694 "", "quit the emulator" },
1695 { "eject", "-fB", do_eject
,
1696 "[-f] device", "eject a removable medium (use -f to force it)" },
1697 { "change", "BFs?", do_change
,
1698 "device filename [format]", "change a removable medium, optional format" },
1699 { "screendump", "F", do_screen_dump
,
1700 "filename", "save screen into PPM image 'filename'" },
1701 { "logfile", "F", do_logfile
,
1702 "filename", "output logs to 'filename'" },
1703 { "log", "s", do_log
,
1704 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1705 { "savevm", "s?", do_savevm
,
1706 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1707 { "loadvm", "s", do_loadvm
,
1708 "tag|id", "restore a VM snapshot from its tag or id" },
1709 { "delvm", "s", do_delvm
,
1710 "tag|id", "delete a VM snapshot from its tag or id" },
1711 { "singlestep", "s?", do_singlestep
,
1712 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1713 { "stop", "", do_stop
,
1714 "", "stop emulation", },
1715 { "c|cont", "", do_cont
,
1716 "", "resume emulation", },
1717 { "gdbserver", "s?", do_gdbserver
,
1718 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
1719 { "x", "/l", do_memory_dump
,
1720 "/fmt addr", "virtual memory dump starting at 'addr'", },
1721 { "xp", "/l", do_physical_memory_dump
,
1722 "/fmt addr", "physical memory dump starting at 'addr'", },
1723 { "p|print", "/l", do_print
,
1724 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1725 { "i", "/ii.", do_ioport_read
,
1726 "/fmt addr", "I/O port read" },
1728 { "sendkey", "si?", do_sendkey
,
1729 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1730 { "system_reset", "", do_system_reset
,
1731 "", "reset the system" },
1732 { "system_powerdown", "", do_system_powerdown
,
1733 "", "send system power down event" },
1734 { "sum", "ii", do_sum
,
1735 "addr size", "compute the checksum of a memory region" },
1736 { "usb_add", "s", do_usb_add
,
1737 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1738 { "usb_del", "s", do_usb_del
,
1739 "device", "remove USB device 'bus.addr'" },
1740 { "cpu", "i", do_cpu_set
,
1741 "index", "set the default CPU" },
1742 { "mouse_move", "sss?", do_mouse_move
,
1743 "dx dy [dz]", "send mouse move events" },
1744 { "mouse_button", "i", do_mouse_button
,
1745 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1746 { "mouse_set", "i", do_mouse_set
,
1747 "index", "set which mouse device receives events" },
1749 { "wavcapture", "si?i?i?", do_wav_capture
,
1750 "path [frequency [bits [channels]]]",
1751 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1753 { "stopcapture", "i", do_stop_capture
,
1754 "capture index", "stop capture" },
1755 { "memsave", "lis", do_memory_save
,
1756 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1757 { "pmemsave", "lis", do_physical_memory_save
,
1758 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1759 { "boot_set", "s", do_boot_set
,
1760 "bootdevice", "define new values for the boot device list" },
1761 #if defined(TARGET_I386)
1762 { "nmi", "i", do_inject_nmi
,
1763 "cpu", "inject an NMI on the given CPU", },
1765 { "migrate", "-ds", do_migrate
,
1766 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1767 { "migrate_cancel", "", do_migrate_cancel
,
1768 "", "cancel the current VM migration" },
1769 { "migrate_set_speed", "s", do_migrate_set_speed
,
1770 "value", "set maximum speed (in bytes) for migrations" },
1771 #if defined(TARGET_I386)
1772 { "drive_add", "ss", drive_hot_add
, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1773 "[file=file][,if=type][,bus=n]\n"
1774 "[,unit=m][,media=d][index=i]\n"
1775 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1776 "[snapshot=on|off][,cache=on|off]",
1777 "add drive to PCI storage controller" },
1778 { "pci_add", "sss", pci_device_hot_add
, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage|host [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]... [host=02:00.0[,name=string][,dma=none]", "hot-add PCI device" },
1779 { "pci_del", "s", pci_device_hot_remove
, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1781 { "host_net_add", "ss?", net_host_device_add
,
1782 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1783 { "host_net_remove", "is", net_host_device_remove
,
1784 "vlan_id name", "remove host VLAN client" },
1786 { "host_net_redir", "ss?", net_slirp_redir
,
1787 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
1788 "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
1789 "host_net_redir list -- show all redirections" },
1791 { "balloon", "i", do_balloon
,
1792 "target", "request VM to change it's memory allocation (in MB)" },
1793 { "set_link", "ss", do_set_link
,
1794 "name up|down", "change the link status of a network adapter" },
1795 { "watchdog_action", "s", do_watchdog_action
,
1796 "[reset|shutdown|poweroff|pause|debug|none]", "change watchdog action" },
1797 { "acl", "sss?i?", do_acl
, "<command> <aclname> [<match> [<index>]]\n",
1798 "acl show vnc.username\n"
1799 "acl policy vnc.username deny\n"
1800 "acl allow vnc.username fred\n"
1801 "acl deny vnc.username bob\n"
1802 "acl reset vnc.username\n" },
1803 { "cpu_set", "is", do_cpu_set_nr
, "cpu [online|offline]", "change cpu state" },
1807 /* Please update qemu-doc.texi when adding or changing commands */
1808 static const mon_cmd_t info_cmds
[] = {
1809 { "version", "", do_info_version
,
1810 "", "show the version of QEMU" },
1811 { "network", "", do_info_network
,
1812 "", "show the network state" },
1813 { "chardev", "", qemu_chr_info
,
1814 "", "show the character devices" },
1815 { "block", "", bdrv_info
,
1816 "", "show the block devices" },
1817 { "blockstats", "", bdrv_info_stats
,
1818 "", "show block device statistics" },
1819 { "registers", "", do_info_registers
,
1820 "", "show the cpu registers" },
1821 { "cpus", "", do_info_cpus
,
1822 "", "show infos for each CPU" },
1823 { "history", "", do_info_history
,
1824 "", "show the command line history", },
1825 { "irq", "", irq_info
,
1826 "", "show the interrupts statistics (if available)", },
1827 { "pic", "", pic_info
,
1828 "", "show i8259 (PIC) state", },
1829 { "pci", "", pci_info
,
1830 "", "show PCI info", },
1831 #if defined(TARGET_I386) || defined(TARGET_SH4)
1832 { "tlb", "", tlb_info
,
1833 "", "show virtual to physical memory mappings", },
1835 #if defined(TARGET_I386)
1836 { "mem", "", mem_info
,
1837 "", "show the active virtual memory mappings", },
1838 { "hpet", "", do_info_hpet
,
1839 "", "show state of HPET", },
1841 { "jit", "", do_info_jit
,
1842 "", "show dynamic compiler info", },
1843 { "kqemu", "", do_info_kqemu
,
1844 "", "show KQEMU information", },
1845 { "kvm", "", do_info_kvm
,
1846 "", "show KVM information", },
1847 { "numa", "", do_info_numa
,
1848 "", "show NUMA information", },
1849 { "usb", "", usb_info
,
1850 "", "show guest USB devices", },
1851 { "usbhost", "", usb_host_info
,
1852 "", "show host USB devices", },
1853 { "profile", "", do_info_profile
,
1854 "", "show profiling information", },
1855 { "capture", "", do_info_capture
,
1856 "", "show capture information" },
1857 { "snapshots", "", do_info_snapshots
,
1858 "", "show the currently saved VM snapshots" },
1859 { "status", "", do_info_status
,
1860 "", "show the current VM status (running|paused)" },
1861 { "pcmcia", "", pcmcia_info
,
1862 "", "show guest PCMCIA status" },
1863 { "mice", "", do_info_mice
,
1864 "", "show which guest mouse is receiving events" },
1865 { "vnc", "", do_info_vnc
,
1866 "", "show the vnc server status"},
1867 { "name", "", do_info_name
,
1868 "", "show the current VM name" },
1869 { "uuid", "", do_info_uuid
,
1870 "", "show the current VM UUID" },
1871 #if defined(TARGET_PPC)
1872 { "cpustats", "", do_info_cpu_stats
,
1873 "", "show CPU statistics", },
1875 #if defined(CONFIG_SLIRP)
1876 { "slirp", "", do_info_slirp
,
1877 "", "show SLIRP statistics", },
1879 { "migrate", "", do_info_migrate
, "", "show migration status" },
1880 { "balloon", "", do_info_balloon
,
1881 "", "show balloon information" },
1885 /*******************************************************************/
1887 static const char *pch
;
1888 static jmp_buf expr_env
;
1893 typedef struct MonitorDef
{
1896 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1900 #if defined(TARGET_I386)
1901 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1903 CPUState
*env
= mon_get_cpu();
1906 return env
->eip
+ env
->segs
[R_CS
].base
;
1910 #if defined(TARGET_PPC)
1911 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1913 CPUState
*env
= mon_get_cpu();
1921 for (i
= 0; i
< 8; i
++)
1922 u
|= env
->crf
[i
] << (32 - (4 * i
));
1927 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1929 CPUState
*env
= mon_get_cpu();
1935 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1937 CPUState
*env
= mon_get_cpu();
1943 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1945 CPUState
*env
= mon_get_cpu();
1948 return cpu_ppc_load_decr(env
);
1951 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1953 CPUState
*env
= mon_get_cpu();
1956 return cpu_ppc_load_tbu(env
);
1959 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1961 CPUState
*env
= mon_get_cpu();
1964 return cpu_ppc_load_tbl(env
);
1968 #if defined(TARGET_SPARC)
1969 #ifndef TARGET_SPARC64
1970 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1972 CPUState
*env
= mon_get_cpu();
1975 return GET_PSR(env
);
1979 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1981 CPUState
*env
= mon_get_cpu();
1984 return env
->regwptr
[val
];
1988 static const MonitorDef monitor_defs
[] = {
1991 #define SEG(name, seg) \
1992 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1993 { name ".base", offsetof(CPUState, segs[seg].base) },\
1994 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1996 { "eax", offsetof(CPUState
, regs
[0]) },
1997 { "ecx", offsetof(CPUState
, regs
[1]) },
1998 { "edx", offsetof(CPUState
, regs
[2]) },
1999 { "ebx", offsetof(CPUState
, regs
[3]) },
2000 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2001 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2002 { "esi", offsetof(CPUState
, regs
[6]) },
2003 { "edi", offsetof(CPUState
, regs
[7]) },
2004 #ifdef TARGET_X86_64
2005 { "r8", offsetof(CPUState
, regs
[8]) },
2006 { "r9", offsetof(CPUState
, regs
[9]) },
2007 { "r10", offsetof(CPUState
, regs
[10]) },
2008 { "r11", offsetof(CPUState
, regs
[11]) },
2009 { "r12", offsetof(CPUState
, regs
[12]) },
2010 { "r13", offsetof(CPUState
, regs
[13]) },
2011 { "r14", offsetof(CPUState
, regs
[14]) },
2012 { "r15", offsetof(CPUState
, regs
[15]) },
2014 { "eflags", offsetof(CPUState
, eflags
) },
2015 { "eip", offsetof(CPUState
, eip
) },
2022 { "pc", 0, monitor_get_pc
, },
2023 #elif defined(TARGET_PPC)
2024 /* General purpose registers */
2025 { "r0", offsetof(CPUState
, gpr
[0]) },
2026 { "r1", offsetof(CPUState
, gpr
[1]) },
2027 { "r2", offsetof(CPUState
, gpr
[2]) },
2028 { "r3", offsetof(CPUState
, gpr
[3]) },
2029 { "r4", offsetof(CPUState
, gpr
[4]) },
2030 { "r5", offsetof(CPUState
, gpr
[5]) },
2031 { "r6", offsetof(CPUState
, gpr
[6]) },
2032 { "r7", offsetof(CPUState
, gpr
[7]) },
2033 { "r8", offsetof(CPUState
, gpr
[8]) },
2034 { "r9", offsetof(CPUState
, gpr
[9]) },
2035 { "r10", offsetof(CPUState
, gpr
[10]) },
2036 { "r11", offsetof(CPUState
, gpr
[11]) },
2037 { "r12", offsetof(CPUState
, gpr
[12]) },
2038 { "r13", offsetof(CPUState
, gpr
[13]) },
2039 { "r14", offsetof(CPUState
, gpr
[14]) },
2040 { "r15", offsetof(CPUState
, gpr
[15]) },
2041 { "r16", offsetof(CPUState
, gpr
[16]) },
2042 { "r17", offsetof(CPUState
, gpr
[17]) },
2043 { "r18", offsetof(CPUState
, gpr
[18]) },
2044 { "r19", offsetof(CPUState
, gpr
[19]) },
2045 { "r20", offsetof(CPUState
, gpr
[20]) },
2046 { "r21", offsetof(CPUState
, gpr
[21]) },
2047 { "r22", offsetof(CPUState
, gpr
[22]) },
2048 { "r23", offsetof(CPUState
, gpr
[23]) },
2049 { "r24", offsetof(CPUState
, gpr
[24]) },
2050 { "r25", offsetof(CPUState
, gpr
[25]) },
2051 { "r26", offsetof(CPUState
, gpr
[26]) },
2052 { "r27", offsetof(CPUState
, gpr
[27]) },
2053 { "r28", offsetof(CPUState
, gpr
[28]) },
2054 { "r29", offsetof(CPUState
, gpr
[29]) },
2055 { "r30", offsetof(CPUState
, gpr
[30]) },
2056 { "r31", offsetof(CPUState
, gpr
[31]) },
2057 /* Floating point registers */
2058 { "f0", offsetof(CPUState
, fpr
[0]) },
2059 { "f1", offsetof(CPUState
, fpr
[1]) },
2060 { "f2", offsetof(CPUState
, fpr
[2]) },
2061 { "f3", offsetof(CPUState
, fpr
[3]) },
2062 { "f4", offsetof(CPUState
, fpr
[4]) },
2063 { "f5", offsetof(CPUState
, fpr
[5]) },
2064 { "f6", offsetof(CPUState
, fpr
[6]) },
2065 { "f7", offsetof(CPUState
, fpr
[7]) },
2066 { "f8", offsetof(CPUState
, fpr
[8]) },
2067 { "f9", offsetof(CPUState
, fpr
[9]) },
2068 { "f10", offsetof(CPUState
, fpr
[10]) },
2069 { "f11", offsetof(CPUState
, fpr
[11]) },
2070 { "f12", offsetof(CPUState
, fpr
[12]) },
2071 { "f13", offsetof(CPUState
, fpr
[13]) },
2072 { "f14", offsetof(CPUState
, fpr
[14]) },
2073 { "f15", offsetof(CPUState
, fpr
[15]) },
2074 { "f16", offsetof(CPUState
, fpr
[16]) },
2075 { "f17", offsetof(CPUState
, fpr
[17]) },
2076 { "f18", offsetof(CPUState
, fpr
[18]) },
2077 { "f19", offsetof(CPUState
, fpr
[19]) },
2078 { "f20", offsetof(CPUState
, fpr
[20]) },
2079 { "f21", offsetof(CPUState
, fpr
[21]) },
2080 { "f22", offsetof(CPUState
, fpr
[22]) },
2081 { "f23", offsetof(CPUState
, fpr
[23]) },
2082 { "f24", offsetof(CPUState
, fpr
[24]) },
2083 { "f25", offsetof(CPUState
, fpr
[25]) },
2084 { "f26", offsetof(CPUState
, fpr
[26]) },
2085 { "f27", offsetof(CPUState
, fpr
[27]) },
2086 { "f28", offsetof(CPUState
, fpr
[28]) },
2087 { "f29", offsetof(CPUState
, fpr
[29]) },
2088 { "f30", offsetof(CPUState
, fpr
[30]) },
2089 { "f31", offsetof(CPUState
, fpr
[31]) },
2090 { "fpscr", offsetof(CPUState
, fpscr
) },
2091 /* Next instruction pointer */
2092 { "nip|pc", offsetof(CPUState
, nip
) },
2093 { "lr", offsetof(CPUState
, lr
) },
2094 { "ctr", offsetof(CPUState
, ctr
) },
2095 { "decr", 0, &monitor_get_decr
, },
2096 { "ccr", 0, &monitor_get_ccr
, },
2097 /* Machine state register */
2098 { "msr", 0, &monitor_get_msr
, },
2099 { "xer", 0, &monitor_get_xer
, },
2100 { "tbu", 0, &monitor_get_tbu
, },
2101 { "tbl", 0, &monitor_get_tbl
, },
2102 #if defined(TARGET_PPC64)
2103 /* Address space register */
2104 { "asr", offsetof(CPUState
, asr
) },
2106 /* Segment registers */
2107 { "sdr1", offsetof(CPUState
, sdr1
) },
2108 { "sr0", offsetof(CPUState
, sr
[0]) },
2109 { "sr1", offsetof(CPUState
, sr
[1]) },
2110 { "sr2", offsetof(CPUState
, sr
[2]) },
2111 { "sr3", offsetof(CPUState
, sr
[3]) },
2112 { "sr4", offsetof(CPUState
, sr
[4]) },
2113 { "sr5", offsetof(CPUState
, sr
[5]) },
2114 { "sr6", offsetof(CPUState
, sr
[6]) },
2115 { "sr7", offsetof(CPUState
, sr
[7]) },
2116 { "sr8", offsetof(CPUState
, sr
[8]) },
2117 { "sr9", offsetof(CPUState
, sr
[9]) },
2118 { "sr10", offsetof(CPUState
, sr
[10]) },
2119 { "sr11", offsetof(CPUState
, sr
[11]) },
2120 { "sr12", offsetof(CPUState
, sr
[12]) },
2121 { "sr13", offsetof(CPUState
, sr
[13]) },
2122 { "sr14", offsetof(CPUState
, sr
[14]) },
2123 { "sr15", offsetof(CPUState
, sr
[15]) },
2124 /* Too lazy to put BATs and SPRs ... */
2125 #elif defined(TARGET_SPARC)
2126 { "g0", offsetof(CPUState
, gregs
[0]) },
2127 { "g1", offsetof(CPUState
, gregs
[1]) },
2128 { "g2", offsetof(CPUState
, gregs
[2]) },
2129 { "g3", offsetof(CPUState
, gregs
[3]) },
2130 { "g4", offsetof(CPUState
, gregs
[4]) },
2131 { "g5", offsetof(CPUState
, gregs
[5]) },
2132 { "g6", offsetof(CPUState
, gregs
[6]) },
2133 { "g7", offsetof(CPUState
, gregs
[7]) },
2134 { "o0", 0, monitor_get_reg
},
2135 { "o1", 1, monitor_get_reg
},
2136 { "o2", 2, monitor_get_reg
},
2137 { "o3", 3, monitor_get_reg
},
2138 { "o4", 4, monitor_get_reg
},
2139 { "o5", 5, monitor_get_reg
},
2140 { "o6", 6, monitor_get_reg
},
2141 { "o7", 7, monitor_get_reg
},
2142 { "l0", 8, monitor_get_reg
},
2143 { "l1", 9, monitor_get_reg
},
2144 { "l2", 10, monitor_get_reg
},
2145 { "l3", 11, monitor_get_reg
},
2146 { "l4", 12, monitor_get_reg
},
2147 { "l5", 13, monitor_get_reg
},
2148 { "l6", 14, monitor_get_reg
},
2149 { "l7", 15, monitor_get_reg
},
2150 { "i0", 16, monitor_get_reg
},
2151 { "i1", 17, monitor_get_reg
},
2152 { "i2", 18, monitor_get_reg
},
2153 { "i3", 19, monitor_get_reg
},
2154 { "i4", 20, monitor_get_reg
},
2155 { "i5", 21, monitor_get_reg
},
2156 { "i6", 22, monitor_get_reg
},
2157 { "i7", 23, monitor_get_reg
},
2158 { "pc", offsetof(CPUState
, pc
) },
2159 { "npc", offsetof(CPUState
, npc
) },
2160 { "y", offsetof(CPUState
, y
) },
2161 #ifndef TARGET_SPARC64
2162 { "psr", 0, &monitor_get_psr
, },
2163 { "wim", offsetof(CPUState
, wim
) },
2165 { "tbr", offsetof(CPUState
, tbr
) },
2166 { "fsr", offsetof(CPUState
, fsr
) },
2167 { "f0", offsetof(CPUState
, fpr
[0]) },
2168 { "f1", offsetof(CPUState
, fpr
[1]) },
2169 { "f2", offsetof(CPUState
, fpr
[2]) },
2170 { "f3", offsetof(CPUState
, fpr
[3]) },
2171 { "f4", offsetof(CPUState
, fpr
[4]) },
2172 { "f5", offsetof(CPUState
, fpr
[5]) },
2173 { "f6", offsetof(CPUState
, fpr
[6]) },
2174 { "f7", offsetof(CPUState
, fpr
[7]) },
2175 { "f8", offsetof(CPUState
, fpr
[8]) },
2176 { "f9", offsetof(CPUState
, fpr
[9]) },
2177 { "f10", offsetof(CPUState
, fpr
[10]) },
2178 { "f11", offsetof(CPUState
, fpr
[11]) },
2179 { "f12", offsetof(CPUState
, fpr
[12]) },
2180 { "f13", offsetof(CPUState
, fpr
[13]) },
2181 { "f14", offsetof(CPUState
, fpr
[14]) },
2182 { "f15", offsetof(CPUState
, fpr
[15]) },
2183 { "f16", offsetof(CPUState
, fpr
[16]) },
2184 { "f17", offsetof(CPUState
, fpr
[17]) },
2185 { "f18", offsetof(CPUState
, fpr
[18]) },
2186 { "f19", offsetof(CPUState
, fpr
[19]) },
2187 { "f20", offsetof(CPUState
, fpr
[20]) },
2188 { "f21", offsetof(CPUState
, fpr
[21]) },
2189 { "f22", offsetof(CPUState
, fpr
[22]) },
2190 { "f23", offsetof(CPUState
, fpr
[23]) },
2191 { "f24", offsetof(CPUState
, fpr
[24]) },
2192 { "f25", offsetof(CPUState
, fpr
[25]) },
2193 { "f26", offsetof(CPUState
, fpr
[26]) },
2194 { "f27", offsetof(CPUState
, fpr
[27]) },
2195 { "f28", offsetof(CPUState
, fpr
[28]) },
2196 { "f29", offsetof(CPUState
, fpr
[29]) },
2197 { "f30", offsetof(CPUState
, fpr
[30]) },
2198 { "f31", offsetof(CPUState
, fpr
[31]) },
2199 #ifdef TARGET_SPARC64
2200 { "f32", offsetof(CPUState
, fpr
[32]) },
2201 { "f34", offsetof(CPUState
, fpr
[34]) },
2202 { "f36", offsetof(CPUState
, fpr
[36]) },
2203 { "f38", offsetof(CPUState
, fpr
[38]) },
2204 { "f40", offsetof(CPUState
, fpr
[40]) },
2205 { "f42", offsetof(CPUState
, fpr
[42]) },
2206 { "f44", offsetof(CPUState
, fpr
[44]) },
2207 { "f46", offsetof(CPUState
, fpr
[46]) },
2208 { "f48", offsetof(CPUState
, fpr
[48]) },
2209 { "f50", offsetof(CPUState
, fpr
[50]) },
2210 { "f52", offsetof(CPUState
, fpr
[52]) },
2211 { "f54", offsetof(CPUState
, fpr
[54]) },
2212 { "f56", offsetof(CPUState
, fpr
[56]) },
2213 { "f58", offsetof(CPUState
, fpr
[58]) },
2214 { "f60", offsetof(CPUState
, fpr
[60]) },
2215 { "f62", offsetof(CPUState
, fpr
[62]) },
2216 { "asi", offsetof(CPUState
, asi
) },
2217 { "pstate", offsetof(CPUState
, pstate
) },
2218 { "cansave", offsetof(CPUState
, cansave
) },
2219 { "canrestore", offsetof(CPUState
, canrestore
) },
2220 { "otherwin", offsetof(CPUState
, otherwin
) },
2221 { "wstate", offsetof(CPUState
, wstate
) },
2222 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2223 { "fprs", offsetof(CPUState
, fprs
) },
2229 static void expr_error(Monitor
*mon
, const char *msg
)
2231 monitor_printf(mon
, "%s\n", msg
);
2232 longjmp(expr_env
, 1);
2235 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2236 static int get_monitor_def(target_long
*pval
, const char *name
)
2238 const MonitorDef
*md
;
2241 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2242 if (compare_cmd(name
, md
->name
)) {
2243 if (md
->get_value
) {
2244 *pval
= md
->get_value(md
, md
->offset
);
2246 CPUState
*env
= mon_get_cpu();
2249 ptr
= (uint8_t *)env
+ md
->offset
;
2252 *pval
= *(int32_t *)ptr
;
2255 *pval
= *(target_long
*)ptr
;
2268 static void next(void)
2272 while (qemu_isspace(*pch
))
2277 static int64_t expr_sum(Monitor
*mon
);
2279 static int64_t expr_unary(Monitor
*mon
)
2288 n
= expr_unary(mon
);
2292 n
= -expr_unary(mon
);
2296 n
= ~expr_unary(mon
);
2302 expr_error(mon
, "')' expected");
2309 expr_error(mon
, "character constant expected");
2313 expr_error(mon
, "missing terminating \' character");
2323 while ((*pch
>= 'a' && *pch
<= 'z') ||
2324 (*pch
>= 'A' && *pch
<= 'Z') ||
2325 (*pch
>= '0' && *pch
<= '9') ||
2326 *pch
== '_' || *pch
== '.') {
2327 if ((q
- buf
) < sizeof(buf
) - 1)
2331 while (qemu_isspace(*pch
))
2334 ret
= get_monitor_def(®
, buf
);
2336 expr_error(mon
, "unknown register");
2338 expr_error(mon
, "no cpu defined");
2343 expr_error(mon
, "unexpected end of expression");
2347 #if TARGET_PHYS_ADDR_BITS > 32
2348 n
= strtoull(pch
, &p
, 0);
2350 n
= strtoul(pch
, &p
, 0);
2353 expr_error(mon
, "invalid char in expression");
2356 while (qemu_isspace(*pch
))
2364 static int64_t expr_prod(Monitor
*mon
)
2369 val
= expr_unary(mon
);
2372 if (op
!= '*' && op
!= '/' && op
!= '%')
2375 val2
= expr_unary(mon
);
2384 expr_error(mon
, "division by zero");
2395 static int64_t expr_logic(Monitor
*mon
)
2400 val
= expr_prod(mon
);
2403 if (op
!= '&' && op
!= '|' && op
!= '^')
2406 val2
= expr_prod(mon
);
2423 static int64_t expr_sum(Monitor
*mon
)
2428 val
= expr_logic(mon
);
2431 if (op
!= '+' && op
!= '-')
2434 val2
= expr_logic(mon
);
2443 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2446 if (setjmp(expr_env
)) {
2450 while (qemu_isspace(*pch
))
2452 *pval
= expr_sum(mon
);
2457 static int get_str(char *buf
, int buf_size
, const char **pp
)
2465 while (qemu_isspace(*p
))
2475 while (*p
!= '\0' && *p
!= '\"') {
2491 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2494 if ((q
- buf
) < buf_size
- 1) {
2498 if ((q
- buf
) < buf_size
- 1) {
2505 qemu_printf("unterminated string\n");
2510 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2511 if ((q
- buf
) < buf_size
- 1) {
2522 static int default_fmt_format
= 'x';
2523 static int default_fmt_size
= 4;
2527 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2529 const char *p
, *pstart
, *typestr
;
2531 int c
, nb_args
, len
, i
, has_arg
;
2532 const mon_cmd_t
*cmd
;
2535 void *str_allocated
[MAX_ARGS
];
2536 void *args
[MAX_ARGS
];
2537 void (*handler_0
)(Monitor
*mon
);
2538 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2539 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2540 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2541 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2543 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2544 void *arg3
, void *arg4
);
2545 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2546 void *arg3
, void *arg4
, void *arg5
);
2547 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2548 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2551 monitor_printf(mon
, "command='%s'\n", cmdline
);
2554 /* extract the command name */
2557 while (qemu_isspace(*p
))
2562 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2565 if (len
> sizeof(cmdname
) - 1)
2566 len
= sizeof(cmdname
) - 1;
2567 memcpy(cmdname
, pstart
, len
);
2568 cmdname
[len
] = '\0';
2570 /* find the command */
2571 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2572 if (compare_cmd(cmdname
, cmd
->name
))
2575 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2579 for(i
= 0; i
< MAX_ARGS
; i
++)
2580 str_allocated
[i
] = NULL
;
2582 /* parse the parameters */
2583 typestr
= cmd
->args_type
;
2598 while (qemu_isspace(*p
))
2600 if (*typestr
== '?') {
2603 /* no optional string: NULL argument */
2608 ret
= get_str(buf
, sizeof(buf
), &p
);
2612 monitor_printf(mon
, "%s: filename expected\n",
2616 monitor_printf(mon
, "%s: block device name expected\n",
2620 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2625 str
= qemu_malloc(strlen(buf
) + 1);
2626 pstrcpy(str
, sizeof(buf
), buf
);
2627 str_allocated
[nb_args
] = str
;
2629 if (nb_args
>= MAX_ARGS
) {
2631 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2634 args
[nb_args
++] = str
;
2639 int count
, format
, size
;
2641 while (qemu_isspace(*p
))
2647 if (qemu_isdigit(*p
)) {
2649 while (qemu_isdigit(*p
)) {
2650 count
= count
* 10 + (*p
- '0');
2688 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2689 monitor_printf(mon
, "invalid char in format: '%c'\n",
2694 format
= default_fmt_format
;
2695 if (format
!= 'i') {
2696 /* for 'i', not specifying a size gives -1 as size */
2698 size
= default_fmt_size
;
2699 default_fmt_size
= size
;
2701 default_fmt_format
= format
;
2704 format
= default_fmt_format
;
2705 if (format
!= 'i') {
2706 size
= default_fmt_size
;
2711 if (nb_args
+ 3 > MAX_ARGS
)
2713 args
[nb_args
++] = (void*)(long)count
;
2714 args
[nb_args
++] = (void*)(long)format
;
2715 args
[nb_args
++] = (void*)(long)size
;
2723 while (qemu_isspace(*p
))
2725 if (*typestr
== '?' || *typestr
== '.') {
2726 if (*typestr
== '?') {
2734 while (qemu_isspace(*p
))
2742 if (nb_args
>= MAX_ARGS
)
2744 args
[nb_args
++] = (void *)(long)has_arg
;
2746 if (nb_args
>= MAX_ARGS
)
2752 if (get_expr(mon
, &val
, &p
))
2756 if (nb_args
>= MAX_ARGS
)
2758 args
[nb_args
++] = (void *)(long)val
;
2760 if ((nb_args
+ 1) >= MAX_ARGS
)
2762 #if TARGET_PHYS_ADDR_BITS > 32
2763 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2765 args
[nb_args
++] = (void *)0;
2767 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2779 while (qemu_isspace(*p
))
2785 monitor_printf(mon
, "%s: unsupported option -%c\n",
2792 if (nb_args
>= MAX_ARGS
)
2794 args
[nb_args
++] = (void *)(long)has_option
;
2799 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2803 /* check that all arguments were parsed */
2804 while (qemu_isspace(*p
))
2807 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2814 handler_0
= cmd
->handler
;
2818 handler_1
= cmd
->handler
;
2819 handler_1(mon
, args
[0]);
2822 handler_2
= cmd
->handler
;
2823 handler_2(mon
, args
[0], args
[1]);
2826 handler_3
= cmd
->handler
;
2827 handler_3(mon
, args
[0], args
[1], args
[2]);
2830 handler_4
= cmd
->handler
;
2831 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2834 handler_5
= cmd
->handler
;
2835 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2838 handler_6
= cmd
->handler
;
2839 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2842 handler_7
= cmd
->handler
;
2843 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2847 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2851 for(i
= 0; i
< MAX_ARGS
; i
++)
2852 qemu_free(str_allocated
[i
]);
2856 static void cmd_completion(const char *name
, const char *list
)
2858 const char *p
, *pstart
;
2867 p
= pstart
+ strlen(pstart
);
2869 if (len
> sizeof(cmd
) - 2)
2870 len
= sizeof(cmd
) - 2;
2871 memcpy(cmd
, pstart
, len
);
2873 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2874 readline_add_completion(cur_mon
->rs
, cmd
);
2882 static void file_completion(const char *input
)
2887 char file
[1024], file_prefix
[1024];
2891 p
= strrchr(input
, '/');
2894 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2895 pstrcpy(path
, sizeof(path
), ".");
2897 input_path_len
= p
- input
+ 1;
2898 memcpy(path
, input
, input_path_len
);
2899 if (input_path_len
> sizeof(path
) - 1)
2900 input_path_len
= sizeof(path
) - 1;
2901 path
[input_path_len
] = '\0';
2902 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2904 #ifdef DEBUG_COMPLETION
2905 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2906 input
, path
, file_prefix
);
2908 ffs
= opendir(path
);
2916 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2917 memcpy(file
, input
, input_path_len
);
2918 if (input_path_len
< sizeof(file
))
2919 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2921 /* stat the file to find out if it's a directory.
2922 * In that case add a slash to speed up typing long paths
2925 if(S_ISDIR(sb
.st_mode
))
2926 pstrcat(file
, sizeof(file
), "/");
2927 readline_add_completion(cur_mon
->rs
, file
);
2933 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2935 const char *name
= bdrv_get_device_name(bs
);
2936 const char *input
= opaque
;
2938 if (input
[0] == '\0' ||
2939 !strncmp(name
, (char *)input
, strlen(input
))) {
2940 readline_add_completion(cur_mon
->rs
, name
);
2944 /* NOTE: this parser is an approximate form of the real command parser */
2945 static void parse_cmdline(const char *cmdline
,
2946 int *pnb_args
, char **args
)
2955 while (qemu_isspace(*p
))
2959 if (nb_args
>= MAX_ARGS
)
2961 ret
= get_str(buf
, sizeof(buf
), &p
);
2962 args
[nb_args
] = qemu_strdup(buf
);
2967 *pnb_args
= nb_args
;
2970 static void monitor_find_completion(const char *cmdline
)
2972 const char *cmdname
;
2973 char *args
[MAX_ARGS
];
2974 int nb_args
, i
, len
;
2975 const char *ptype
, *str
;
2976 const mon_cmd_t
*cmd
;
2979 parse_cmdline(cmdline
, &nb_args
, args
);
2980 #ifdef DEBUG_COMPLETION
2981 for(i
= 0; i
< nb_args
; i
++) {
2982 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2986 /* if the line ends with a space, it means we want to complete the
2988 len
= strlen(cmdline
);
2989 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2990 if (nb_args
>= MAX_ARGS
)
2992 args
[nb_args
++] = qemu_strdup("");
2995 /* command completion */
3000 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3001 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3002 cmd_completion(cmdname
, cmd
->name
);
3005 /* find the command */
3006 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3007 if (compare_cmd(args
[0], cmd
->name
))
3012 ptype
= cmd
->args_type
;
3013 for(i
= 0; i
< nb_args
- 2; i
++) {
3014 if (*ptype
!= '\0') {
3016 while (*ptype
== '?')
3020 str
= args
[nb_args
- 1];
3023 /* file completion */
3024 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3025 file_completion(str
);
3028 /* block device name completion */
3029 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3030 bdrv_iterate(block_completion_it
, (void *)str
);
3033 /* XXX: more generic ? */
3034 if (!strcmp(cmd
->name
, "info")) {
3035 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3036 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3037 cmd_completion(str
, cmd
->name
);
3039 } else if (!strcmp(cmd
->name
, "sendkey")) {
3040 char *sep
= strrchr(str
, '-');
3043 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3044 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3045 cmd_completion(str
, key
->name
);
3053 for(i
= 0; i
< nb_args
; i
++)
3057 static int monitor_can_read(void *opaque
)
3059 Monitor
*mon
= opaque
;
3061 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3064 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3066 Monitor
*old_mon
= cur_mon
;
3072 for (i
= 0; i
< size
; i
++)
3073 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3075 if (size
== 0 || buf
[size
- 1] != 0)
3076 monitor_printf(cur_mon
, "corrupted command\n");
3078 monitor_handle_command(cur_mon
, (char *)buf
);
3084 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3086 monitor_suspend(mon
);
3087 monitor_handle_command(mon
, cmdline
);
3088 monitor_resume(mon
);
3091 int monitor_suspend(Monitor
*mon
)
3099 void monitor_resume(Monitor
*mon
)
3103 if (--mon
->suspend_cnt
== 0)
3104 readline_show_prompt(mon
->rs
);
3107 static void monitor_event(void *opaque
, int event
)
3109 Monitor
*mon
= opaque
;
3112 case CHR_EVENT_MUX_IN
:
3113 readline_restart(mon
->rs
);
3114 monitor_resume(mon
);
3118 case CHR_EVENT_MUX_OUT
:
3119 if (mon
->suspend_cnt
== 0)
3120 monitor_printf(mon
, "\n");
3122 monitor_suspend(mon
);
3125 case CHR_EVENT_RESET
:
3126 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3127 "information\n", QEMU_VERSION
);
3128 if (mon
->chr
->focus
== 0)
3129 readline_show_prompt(mon
->rs
);
3143 void monitor_init(CharDriverState
*chr
, int flags
)
3145 static int is_first_init
= 1;
3148 if (is_first_init
) {
3149 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3153 mon
= qemu_mallocz(sizeof(*mon
));
3157 if (mon
->chr
->focus
!= 0)
3158 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3159 if (flags
& MONITOR_USE_READLINE
) {
3160 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3161 monitor_read_command(mon
, 0);
3164 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3167 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3168 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3172 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3174 BlockDriverState
*bs
= opaque
;
3177 if (bdrv_set_key(bs
, password
) != 0) {
3178 monitor_printf(mon
, "invalid password\n");
3181 if (mon
->password_completion_cb
)
3182 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3184 monitor_read_command(mon
, 1);
3187 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3188 BlockDriverCompletionFunc
*completion_cb
,
3193 if (!bdrv_key_required(bs
)) {
3195 completion_cb(opaque
, 0);
3199 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3200 bdrv_get_encrypted_filename(bs
));
3202 mon
->password_completion_cb
= completion_cb
;
3203 mon
->password_opaque
= opaque
;
3205 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3207 if (err
&& completion_cb
)
3208 completion_cb(opaque
, err
);