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"
32 #include "qemu-char.h"
38 #include "audio/audio.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
50 //#define DEBUG_COMPLETION
56 * 'B' block device name
57 * 's' string (accept optional quote)
59 * 'l' target long (32 or 64 bit)
60 * '/' optional gdb-like print format (like "/10x")
62 * '?' optional type (for 'F', 's' and 'i')
66 typedef struct mon_cmd_t
{
68 const char *args_type
;
82 BlockDriverCompletionFunc
*password_completion_cb
;
83 void *password_opaque
;
84 LIST_ENTRY(Monitor
) entry
;
87 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
89 static const mon_cmd_t mon_cmds
[];
90 static const mon_cmd_t info_cmds
[];
92 Monitor
*cur_mon
= NULL
;
94 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
97 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
99 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
101 readline_show_prompt(mon
->rs
);
104 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
108 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
109 /* prompt is printed on return from the command handler */
112 monitor_printf(mon
, "terminal does not support password prompting\n");
117 void monitor_flush(Monitor
*mon
)
119 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
120 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
121 mon
->outbuf_index
= 0;
125 /* flush at every end of line or if the buffer is full */
126 static void monitor_puts(Monitor
*mon
, const char *str
)
138 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
139 mon
->outbuf
[mon
->outbuf_index
++] = c
;
140 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
146 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
149 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
150 monitor_puts(mon
, buf
);
153 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
157 monitor_vprintf(mon
, fmt
, ap
);
161 void monitor_print_filename(Monitor
*mon
, const char *filename
)
165 for (i
= 0; filename
[i
]; i
++) {
166 switch (filename
[i
]) {
170 monitor_printf(mon
, "\\%c", filename
[i
]);
173 monitor_printf(mon
, "\\t");
176 monitor_printf(mon
, "\\r");
179 monitor_printf(mon
, "\\n");
182 monitor_printf(mon
, "%c", filename
[i
]);
188 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
192 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
197 static int compare_cmd(const char *name
, const char *list
)
199 const char *p
, *pstart
;
207 p
= pstart
+ strlen(pstart
);
208 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
217 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
218 const char *prefix
, const char *name
)
220 const mon_cmd_t
*cmd
;
222 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
223 if (!name
|| !strcmp(name
, cmd
->name
))
224 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
225 cmd
->params
, cmd
->help
);
229 static void help_cmd(Monitor
*mon
, const char *name
)
231 if (name
&& !strcmp(name
, "info")) {
232 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
234 help_cmd_dump(mon
, mon_cmds
, "", name
);
235 if (name
&& !strcmp(name
, "log")) {
236 const CPULogItem
*item
;
237 monitor_printf(mon
, "Log items (comma separated):\n");
238 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
239 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
240 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
246 static void do_commit(Monitor
*mon
, const char *device
)
250 all_devices
= !strcmp(device
, "all");
251 for (i
= 0; i
< nb_drives
; i
++) {
253 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
254 bdrv_commit(drives_table
[i
].bdrv
);
258 static void do_info(Monitor
*mon
, const char *item
)
260 const mon_cmd_t
*cmd
;
261 void (*handler
)(Monitor
*);
265 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
266 if (compare_cmd(item
, cmd
->name
))
270 help_cmd(mon
, "info");
273 handler
= cmd
->handler
;
277 static void do_info_version(Monitor
*mon
)
279 monitor_printf(mon
, "%s\n", QEMU_VERSION
);
282 static void do_info_name(Monitor
*mon
)
285 monitor_printf(mon
, "%s\n", qemu_name
);
288 #if defined(TARGET_I386)
289 static void do_info_hpet(Monitor
*mon
)
291 monitor_printf(mon
, "HPET is %s by QEMU\n",
292 (no_hpet
) ? "disabled" : "enabled");
296 static void do_info_uuid(Monitor
*mon
)
298 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
299 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
300 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
301 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
302 qemu_uuid
[14], qemu_uuid
[15]);
305 /* get the current CPU defined by the user */
306 static int mon_set_cpu(int cpu_index
)
310 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
311 if (env
->cpu_index
== cpu_index
) {
312 cur_mon
->mon_cpu
= env
;
319 static CPUState
*mon_get_cpu(void)
321 if (!cur_mon
->mon_cpu
) {
324 cpu_synchronize_state(cur_mon
->mon_cpu
, 0);
325 return cur_mon
->mon_cpu
;
328 static void do_info_registers(Monitor
*mon
)
335 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
338 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
343 static void do_info_cpus(Monitor
*mon
)
347 /* just to set the default cpu if not already done */
350 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
351 cpu_synchronize_state(env
, 0);
352 monitor_printf(mon
, "%c CPU #%d:",
353 (env
== mon
->mon_cpu
) ? '*' : ' ',
355 #if defined(TARGET_I386)
356 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
357 env
->eip
+ env
->segs
[R_CS
].base
);
358 #elif defined(TARGET_PPC)
359 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
360 #elif defined(TARGET_SPARC)
361 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
363 #elif defined(TARGET_MIPS)
364 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
367 monitor_printf(mon
, " (halted)");
368 monitor_printf(mon
," thread_id=%d", env
->thread_id
);
369 monitor_printf(mon
, "\n");
373 static void do_cpu_set(Monitor
*mon
, int index
)
375 if (mon_set_cpu(index
) < 0)
376 monitor_printf(mon
, "Invalid CPU index\n");
379 static void do_cpu_set_nr(Monitor
*mon
, int value
, const char *status
)
383 if (!strcmp(status
, "online"))
385 else if (!strcmp(status
, "offline"))
388 monitor_printf(mon
, "invalid status: %s\n", status
);
391 #if defined(TARGET_I386) || defined(TARGET_X86_64)
392 qemu_system_cpu_hot_add(value
, state
);
396 static void do_info_jit(Monitor
*mon
)
398 dump_exec_info((FILE *)mon
, monitor_fprintf
);
401 static void do_info_history(Monitor
*mon
)
410 str
= readline_get_history(mon
->rs
, i
);
413 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
418 #if defined(TARGET_PPC)
419 /* XXX: not implemented in other targets */
420 static void do_info_cpu_stats(Monitor
*mon
)
425 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
429 static void do_quit(Monitor
*mon
)
434 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
436 if (bdrv_is_inserted(bs
)) {
438 if (!bdrv_is_removable(bs
)) {
439 monitor_printf(mon
, "device is not removable\n");
442 if (bdrv_is_locked(bs
)) {
443 monitor_printf(mon
, "device is locked\n");
452 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
454 BlockDriverState
*bs
;
456 bs
= bdrv_find(filename
);
458 monitor_printf(mon
, "device not found\n");
461 eject_device(mon
, bs
, force
);
464 static void do_change_block(Monitor
*mon
, const char *device
,
465 const char *filename
, const char *fmt
)
467 BlockDriverState
*bs
;
468 BlockDriver
*drv
= NULL
;
470 bs
= bdrv_find(device
);
472 monitor_printf(mon
, "device not found\n");
476 drv
= bdrv_find_format(fmt
);
478 monitor_printf(mon
, "invalid format %s\n", fmt
);
482 if (eject_device(mon
, bs
, 0) < 0)
484 bdrv_open2(bs
, filename
, 0, drv
);
485 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
488 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
491 if (vnc_display_password(NULL
, password
) < 0)
492 monitor_printf(mon
, "could not set VNC server password\n");
494 monitor_read_command(mon
, 1);
497 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
499 if (strcmp(target
, "passwd") == 0 ||
500 strcmp(target
, "password") == 0) {
503 strncpy(password
, arg
, sizeof(password
));
504 password
[sizeof(password
) - 1] = '\0';
505 change_vnc_password_cb(mon
, password
, NULL
);
507 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
510 if (vnc_display_open(NULL
, target
) < 0)
511 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
515 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
518 if (strcmp(device
, "vnc") == 0) {
519 do_change_vnc(mon
, target
, arg
);
521 do_change_block(mon
, device
, target
, arg
);
525 static void do_screen_dump(Monitor
*mon
, const char *filename
)
527 vga_hw_screen_dump(filename
);
530 static void do_logfile(Monitor
*mon
, const char *filename
)
532 cpu_set_log_filename(filename
);
535 static void do_log(Monitor
*mon
, const char *items
)
539 if (!strcmp(items
, "none")) {
542 mask
= cpu_str_to_log_mask(items
);
544 help_cmd(mon
, "log");
551 static void do_singlestep(Monitor
*mon
, const char *option
)
553 if (!option
|| !strcmp(option
, "on")) {
555 } else if (!strcmp(option
, "off")) {
558 monitor_printf(mon
, "unexpected option %s\n", option
);
562 static void do_stop(Monitor
*mon
)
564 vm_stop(EXCP_INTERRUPT
);
567 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
569 struct bdrv_iterate_context
{
574 static void do_cont(Monitor
*mon
)
576 struct bdrv_iterate_context context
= { mon
, 0 };
578 bdrv_iterate(encrypted_bdrv_it
, &context
);
579 /* only resume the vm if all keys are set and valid */
584 static void bdrv_key_cb(void *opaque
, int err
)
586 Monitor
*mon
= opaque
;
588 /* another key was set successfully, retry to continue */
593 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
595 struct bdrv_iterate_context
*context
= opaque
;
597 if (!context
->err
&& bdrv_key_required(bs
)) {
598 context
->err
= -EBUSY
;
599 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
604 #ifdef CONFIG_GDBSTUB
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",
621 static void monitor_printc(Monitor
*mon
, int c
)
623 monitor_printf(mon
, "'");
626 monitor_printf(mon
, "\\'");
629 monitor_printf(mon
, "\\\\");
632 monitor_printf(mon
, "\\n");
635 monitor_printf(mon
, "\\r");
638 if (c
>= 32 && c
<= 126) {
639 monitor_printf(mon
, "%c", c
);
641 monitor_printf(mon
, "\\x%02x", c
);
645 monitor_printf(mon
, "'");
648 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
649 target_phys_addr_t addr
, int is_physical
)
652 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
660 if (!env
&& !is_physical
)
665 } else if (wsize
== 4) {
668 /* as default we use the current CS size */
672 if ((env
->efer
& MSR_EFER_LMA
) &&
673 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
677 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
682 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
691 nb_per_line
= line_size
/ wsize
;
696 max_digits
= (wsize
* 8 + 2) / 3;
700 max_digits
= (wsize
* 8) / 4;
704 max_digits
= (wsize
* 8 * 10 + 32) / 33;
713 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
715 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
720 cpu_physical_memory_rw(addr
, buf
, l
, 0);
725 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
726 monitor_printf(mon
, " Cannot access memory\n");
735 v
= ldub_raw(buf
+ i
);
738 v
= lduw_raw(buf
+ i
);
741 v
= (uint32_t)ldl_raw(buf
+ i
);
744 v
= ldq_raw(buf
+ i
);
747 monitor_printf(mon
, " ");
750 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
753 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
756 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
759 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
762 monitor_printc(mon
, v
);
767 monitor_printf(mon
, "\n");
773 #if TARGET_LONG_BITS == 64
774 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
776 #define GET_TLONG(h, l) (l)
779 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
780 uint32_t addrh
, uint32_t addrl
)
782 target_long addr
= GET_TLONG(addrh
, addrl
);
783 memory_dump(mon
, count
, format
, size
, addr
, 0);
786 #if TARGET_PHYS_ADDR_BITS > 32
787 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
789 #define GET_TPHYSADDR(h, l) (l)
792 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
793 int size
, uint32_t addrh
, uint32_t addrl
)
796 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
797 memory_dump(mon
, count
, format
, size
, addr
, 1);
800 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
801 unsigned int valh
, unsigned int vall
)
803 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
804 #if TARGET_PHYS_ADDR_BITS == 32
807 monitor_printf(mon
, "%#o", val
);
810 monitor_printf(mon
, "%#x", val
);
813 monitor_printf(mon
, "%u", val
);
817 monitor_printf(mon
, "%d", val
);
820 monitor_printc(mon
, val
);
826 monitor_printf(mon
, "%#" PRIo64
, val
);
829 monitor_printf(mon
, "%#" PRIx64
, val
);
832 monitor_printf(mon
, "%" PRIu64
, val
);
836 monitor_printf(mon
, "%" PRId64
, val
);
839 monitor_printc(mon
, val
);
843 monitor_printf(mon
, "\n");
846 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
847 uint32_t size
, const char *filename
)
850 target_long addr
= GET_TLONG(valh
, vall
);
859 f
= fopen(filename
, "wb");
861 monitor_printf(mon
, "could not open '%s'\n", filename
);
868 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
869 fwrite(buf
, 1, l
, f
);
876 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
877 unsigned int vall
, uint32_t size
,
878 const char *filename
)
883 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
885 f
= fopen(filename
, "wb");
887 monitor_printf(mon
, "could not open '%s'\n", filename
);
894 cpu_physical_memory_rw(addr
, buf
, l
, 0);
895 fwrite(buf
, 1, l
, f
);
903 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
910 for(addr
= start
; addr
< (start
+ size
); addr
++) {
911 cpu_physical_memory_rw(addr
, buf
, 1, 0);
912 /* BSD sum algorithm ('sum' Unix command) */
913 sum
= (sum
>> 1) | (sum
<< 15);
916 monitor_printf(mon
, "%05d\n", sum
);
924 static const KeyDef key_defs
[] = {
951 { 0x0e, "backspace" },
988 { 0x37, "asterisk" },
991 { 0x3a, "caps_lock" },
1002 { 0x45, "num_lock" },
1003 { 0x46, "scroll_lock" },
1005 { 0xb5, "kp_divide" },
1006 { 0x37, "kp_multiply" },
1007 { 0x4a, "kp_subtract" },
1009 { 0x9c, "kp_enter" },
1010 { 0x53, "kp_decimal" },
1043 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1058 { 0xfe, "compose" },
1063 static int get_keycode(const char *key
)
1069 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1070 if (!strcmp(key
, p
->name
))
1073 if (strstart(key
, "0x", NULL
)) {
1074 ret
= strtoul(key
, &endp
, 0);
1075 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1081 #define MAX_KEYCODES 16
1082 static uint8_t keycodes
[MAX_KEYCODES
];
1083 static int nb_pending_keycodes
;
1084 static QEMUTimer
*key_timer
;
1086 static void release_keys(void *opaque
)
1090 while (nb_pending_keycodes
> 0) {
1091 nb_pending_keycodes
--;
1092 keycode
= keycodes
[nb_pending_keycodes
];
1094 kbd_put_keycode(0xe0);
1095 kbd_put_keycode(keycode
| 0x80);
1099 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1102 char keyname_buf
[16];
1104 int keyname_len
, keycode
, i
;
1106 if (nb_pending_keycodes
> 0) {
1107 qemu_del_timer(key_timer
);
1114 separator
= strchr(string
, '-');
1115 keyname_len
= separator
? separator
- string
: strlen(string
);
1116 if (keyname_len
> 0) {
1117 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1118 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1119 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1122 if (i
== MAX_KEYCODES
) {
1123 monitor_printf(mon
, "too many keys\n");
1126 keyname_buf
[keyname_len
] = 0;
1127 keycode
= get_keycode(keyname_buf
);
1129 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1132 keycodes
[i
++] = keycode
;
1136 string
= separator
+ 1;
1138 nb_pending_keycodes
= i
;
1139 /* key down events */
1140 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1141 keycode
= keycodes
[i
];
1143 kbd_put_keycode(0xe0);
1144 kbd_put_keycode(keycode
& 0x7f);
1146 /* delayed key up events */
1147 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1148 muldiv64(ticks_per_sec
, hold_time
, 1000));
1151 static int mouse_button_state
;
1153 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1157 dx
= strtol(dx_str
, NULL
, 0);
1158 dy
= strtol(dy_str
, NULL
, 0);
1161 dz
= strtol(dz_str
, NULL
, 0);
1162 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1165 static void do_mouse_button(Monitor
*mon
, int button_state
)
1167 mouse_button_state
= button_state
;
1168 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1171 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1172 int addr
, int has_index
, int index
)
1178 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1186 val
= cpu_inb(NULL
, addr
);
1190 val
= cpu_inw(NULL
, addr
);
1194 val
= cpu_inl(NULL
, addr
);
1198 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1199 suffix
, addr
, size
* 2, val
);
1202 /* boot_set handler */
1203 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1204 static void *boot_opaque
;
1206 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1208 qemu_boot_set_handler
= func
;
1209 boot_opaque
= opaque
;
1212 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1216 if (qemu_boot_set_handler
) {
1217 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1219 monitor_printf(mon
, "boot device list now set to %s\n",
1222 monitor_printf(mon
, "setting boot device list failed with "
1225 monitor_printf(mon
, "no function defined to set boot device list for "
1226 "this architecture\n");
1230 static void do_system_reset(Monitor
*mon
)
1232 qemu_system_reset_request();
1235 static void do_system_powerdown(Monitor
*mon
)
1237 qemu_system_powerdown_request();
1240 #if defined(TARGET_I386)
1241 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1243 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1246 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1247 pte
& PG_PSE_MASK
? 'P' : '-',
1248 pte
& PG_DIRTY_MASK
? 'D' : '-',
1249 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1250 pte
& PG_PCD_MASK
? 'C' : '-',
1251 pte
& PG_PWT_MASK
? 'T' : '-',
1252 pte
& PG_USER_MASK
? 'U' : '-',
1253 pte
& PG_RW_MASK
? 'W' : '-');
1256 static void tlb_info(Monitor
*mon
)
1260 uint32_t pgd
, pde
, pte
;
1262 env
= mon_get_cpu();
1266 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1267 monitor_printf(mon
, "PG disabled\n");
1270 pgd
= env
->cr
[3] & ~0xfff;
1271 for(l1
= 0; l1
< 1024; l1
++) {
1272 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1273 pde
= le32_to_cpu(pde
);
1274 if (pde
& PG_PRESENT_MASK
) {
1275 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1276 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1278 for(l2
= 0; l2
< 1024; l2
++) {
1279 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1280 (uint8_t *)&pte
, 4);
1281 pte
= le32_to_cpu(pte
);
1282 if (pte
& PG_PRESENT_MASK
) {
1283 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1293 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1294 uint32_t end
, int prot
)
1297 prot1
= *plast_prot
;
1298 if (prot
!= prot1
) {
1299 if (*pstart
!= -1) {
1300 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1301 *pstart
, end
, end
- *pstart
,
1302 prot1
& PG_USER_MASK
? 'u' : '-',
1304 prot1
& PG_RW_MASK
? 'w' : '-');
1314 static void mem_info(Monitor
*mon
)
1317 int l1
, l2
, prot
, last_prot
;
1318 uint32_t pgd
, pde
, pte
, start
, end
;
1320 env
= mon_get_cpu();
1324 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1325 monitor_printf(mon
, "PG disabled\n");
1328 pgd
= env
->cr
[3] & ~0xfff;
1331 for(l1
= 0; l1
< 1024; l1
++) {
1332 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1333 pde
= le32_to_cpu(pde
);
1335 if (pde
& PG_PRESENT_MASK
) {
1336 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1337 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1338 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1340 for(l2
= 0; l2
< 1024; l2
++) {
1341 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1342 (uint8_t *)&pte
, 4);
1343 pte
= le32_to_cpu(pte
);
1344 end
= (l1
<< 22) + (l2
<< 12);
1345 if (pte
& PG_PRESENT_MASK
) {
1346 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1350 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1355 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1361 #if defined(TARGET_SH4)
1363 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1365 monitor_printf(mon
, " tlb%i:\t"
1366 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1367 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1368 "dirty=%hhu writethrough=%hhu\n",
1370 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1371 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1375 static void tlb_info(Monitor
*mon
)
1377 CPUState
*env
= mon_get_cpu();
1380 monitor_printf (mon
, "ITLB:\n");
1381 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1382 print_tlb (mon
, i
, &env
->itlb
[i
]);
1383 monitor_printf (mon
, "UTLB:\n");
1384 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1385 print_tlb (mon
, i
, &env
->utlb
[i
]);
1390 static void do_info_kqemu(Monitor
*mon
)
1396 env
= mon_get_cpu();
1398 monitor_printf(mon
, "No cpu initialized yet");
1401 val
= env
->kqemu_enabled
;
1402 monitor_printf(mon
, "kqemu support: ");
1406 monitor_printf(mon
, "disabled\n");
1409 monitor_printf(mon
, "enabled for user code\n");
1412 monitor_printf(mon
, "enabled for user and kernel code\n");
1416 monitor_printf(mon
, "kqemu support: not compiled\n");
1420 static void do_info_kvm(Monitor
*mon
)
1422 #if defined(USE_KVM) || defined(CONFIG_KVM)
1423 monitor_printf(mon
, "kvm support: ");
1425 monitor_printf(mon
, "enabled\n");
1427 monitor_printf(mon
, "disabled\n");
1429 monitor_printf(mon
, "kvm support: not compiled\n");
1433 #ifdef CONFIG_PROFILER
1437 int64_t kqemu_exec_count
;
1439 int64_t kqemu_ret_int_count
;
1440 int64_t kqemu_ret_excp_count
;
1441 int64_t kqemu_ret_intr_count
;
1443 static void do_info_profile(Monitor
*mon
)
1449 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1450 dev_time
, dev_time
/ (double)ticks_per_sec
);
1451 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1452 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1453 monitor_printf(mon
, "kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%"
1454 PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%"
1456 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1457 kqemu_time
/ (double)total
* 100.0,
1459 kqemu_ret_int_count
,
1460 kqemu_ret_excp_count
,
1461 kqemu_ret_intr_count
);
1464 kqemu_exec_count
= 0;
1466 kqemu_ret_int_count
= 0;
1467 kqemu_ret_excp_count
= 0;
1468 kqemu_ret_intr_count
= 0;
1470 kqemu_record_dump();
1474 static void do_info_profile(Monitor
*mon
)
1476 monitor_printf(mon
, "Internal profiler not compiled\n");
1480 /* Capture support */
1481 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1483 static void do_info_capture(Monitor
*mon
)
1488 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1489 monitor_printf(mon
, "[%d]: ", i
);
1490 s
->ops
.info (s
->opaque
);
1494 static void do_stop_capture(Monitor
*mon
, int n
)
1499 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1501 s
->ops
.destroy (s
->opaque
);
1502 LIST_REMOVE (s
, entries
);
1510 static void do_wav_capture(Monitor
*mon
, const char *path
,
1511 int has_freq
, int freq
,
1512 int has_bits
, int bits
,
1513 int has_channels
, int nchannels
)
1517 s
= qemu_mallocz (sizeof (*s
));
1519 freq
= has_freq
? freq
: 44100;
1520 bits
= has_bits
? bits
: 16;
1521 nchannels
= has_channels
? nchannels
: 2;
1523 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1524 monitor_printf(mon
, "Faied to add wave capture\n");
1527 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1531 #if defined(TARGET_I386)
1532 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1536 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1537 if (env
->cpu_index
== cpu_index
) {
1539 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1541 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1547 static void do_info_status(Monitor
*mon
)
1551 monitor_printf(mon
, "VM status: running (single step mode)\n");
1553 monitor_printf(mon
, "VM status: running\n");
1556 monitor_printf(mon
, "VM status: paused\n");
1560 static void do_balloon(Monitor
*mon
, int value
)
1562 ram_addr_t target
= value
;
1563 qemu_balloon(target
<< 20);
1566 static void do_info_balloon(Monitor
*mon
)
1570 actual
= qemu_balloon_status();
1571 if (kvm_enabled() && !kvm_has_sync_mmu())
1572 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1573 "ballooning disabled\n");
1574 else if (actual
== 0)
1575 monitor_printf(mon
, "Ballooning not activated in VM\n");
1577 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1580 static void do_acl(Monitor
*mon
,
1581 const char *command
,
1582 const char *aclname
,
1589 acl
= qemu_acl_find(aclname
);
1591 monitor_printf(mon
, "acl: unknown list '%s'\n", aclname
);
1595 if (strcmp(command
, "show") == 0) {
1597 qemu_acl_entry
*entry
;
1598 monitor_printf(mon
, "policy: %s\n",
1599 acl
->defaultDeny
? "deny" : "allow");
1600 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1602 monitor_printf(mon
, "%d: %s %s\n", i
,
1603 entry
->deny
? "deny" : "allow",
1606 } else if (strcmp(command
, "reset") == 0) {
1607 qemu_acl_reset(acl
);
1608 monitor_printf(mon
, "acl: removed all rules\n");
1609 } else if (strcmp(command
, "policy") == 0) {
1611 monitor_printf(mon
, "acl: missing policy parameter\n");
1615 if (strcmp(match
, "allow") == 0) {
1616 acl
->defaultDeny
= 0;
1617 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1618 } else if (strcmp(match
, "deny") == 0) {
1619 acl
->defaultDeny
= 1;
1620 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1622 monitor_printf(mon
, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match
);
1624 } else if ((strcmp(command
, "allow") == 0) ||
1625 (strcmp(command
, "deny") == 0)) {
1626 int deny
= strcmp(command
, "deny") == 0 ? 1 : 0;
1630 monitor_printf(mon
, "acl: missing match parameter\n");
1635 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1637 ret
= qemu_acl_append(acl
, deny
, match
);
1639 monitor_printf(mon
, "acl: unable to add acl entry\n");
1641 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1642 } else if (strcmp(command
, "remove") == 0) {
1646 monitor_printf(mon
, "acl: missing match parameter\n");
1650 ret
= qemu_acl_remove(acl
, match
);
1652 monitor_printf(mon
, "acl: no matching acl entry\n");
1654 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1656 monitor_printf(mon
, "acl: unknown command '%s'\n", command
);
1660 /* Please update qemu-doc.texi when adding or changing commands */
1661 static const mon_cmd_t mon_cmds
[] = {
1662 { "help|?", "s?", help_cmd
,
1663 "[cmd]", "show the help" },
1664 { "commit", "s", do_commit
,
1665 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1666 { "info", "s?", do_info
,
1667 "subcommand", "show various information about the system state" },
1668 { "q|quit", "", do_quit
,
1669 "", "quit the emulator" },
1670 { "eject", "-fB", do_eject
,
1671 "[-f] device", "eject a removable medium (use -f to force it)" },
1672 { "change", "BFs?", do_change
,
1673 "device filename [format]", "change a removable medium, optional format" },
1674 { "screendump", "F", do_screen_dump
,
1675 "filename", "save screen into PPM image 'filename'" },
1676 { "logfile", "F", do_logfile
,
1677 "filename", "output logs to 'filename'" },
1678 { "log", "s", do_log
,
1679 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1680 { "savevm", "s?", do_savevm
,
1681 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1682 { "loadvm", "s", do_loadvm
,
1683 "tag|id", "restore a VM snapshot from its tag or id" },
1684 { "delvm", "s", do_delvm
,
1685 "tag|id", "delete a VM snapshot from its tag or id" },
1686 { "singlestep", "s?", do_singlestep
,
1687 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1688 { "stop", "", do_stop
,
1689 "", "stop emulation", },
1690 { "c|cont", "", do_cont
,
1691 "", "resume emulation", },
1692 #ifdef CONFIG_GDBSTUB
1693 { "gdbserver", "s?", do_gdbserver
,
1694 "[port]", "start gdbserver session (default port=1234)", },
1696 { "x", "/l", do_memory_dump
,
1697 "/fmt addr", "virtual memory dump starting at 'addr'", },
1698 { "xp", "/l", do_physical_memory_dump
,
1699 "/fmt addr", "physical memory dump starting at 'addr'", },
1700 { "p|print", "/l", do_print
,
1701 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1702 { "i", "/ii.", do_ioport_read
,
1703 "/fmt addr", "I/O port read" },
1705 { "sendkey", "si?", do_sendkey
,
1706 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1707 { "system_reset", "", do_system_reset
,
1708 "", "reset the system" },
1709 { "system_powerdown", "", do_system_powerdown
,
1710 "", "send system power down event" },
1711 { "sum", "ii", do_sum
,
1712 "addr size", "compute the checksum of a memory region" },
1713 { "usb_add", "s", do_usb_add
,
1714 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1715 { "usb_del", "s", do_usb_del
,
1716 "device", "remove USB device 'bus.addr'" },
1717 { "cpu", "i", do_cpu_set
,
1718 "index", "set the default CPU" },
1719 { "mouse_move", "sss?", do_mouse_move
,
1720 "dx dy [dz]", "send mouse move events" },
1721 { "mouse_button", "i", do_mouse_button
,
1722 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1723 { "mouse_set", "i", do_mouse_set
,
1724 "index", "set which mouse device receives events" },
1726 { "wavcapture", "si?i?i?", do_wav_capture
,
1727 "path [frequency bits channels]",
1728 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1730 { "stopcapture", "i", do_stop_capture
,
1731 "capture index", "stop capture" },
1732 { "memsave", "lis", do_memory_save
,
1733 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1734 { "pmemsave", "lis", do_physical_memory_save
,
1735 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1736 { "boot_set", "s", do_boot_set
,
1737 "bootdevice", "define new values for the boot device list" },
1738 #if defined(TARGET_I386)
1739 { "nmi", "i", do_inject_nmi
,
1740 "cpu", "inject an NMI on the given CPU", },
1742 { "migrate", "-ds", do_migrate
,
1743 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1744 { "migrate_cancel", "", do_migrate_cancel
,
1745 "", "cancel the current VM migration" },
1746 { "migrate_set_speed", "s", do_migrate_set_speed
,
1747 "value", "set maximum speed (in bytes) for migrations" },
1748 #if defined(TARGET_I386) || defined(TARGET_X86_64)
1749 { "drive_add", "ss", drive_hot_add
, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1750 "[file=file][,if=type][,bus=n]\n"
1751 "[,unit=m][,media=d][index=i]\n"
1752 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1753 "[snapshot=on|off][,cache=on|off]",
1754 "add drive to PCI storage controller" },
1755 { "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" },
1756 { "pci_del", "s", pci_device_hot_remove
, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1757 { "host_net_add", "ss", net_host_device_add
,
1758 "[tap,user,socket,vde] options", "add host VLAN client" },
1759 { "host_net_remove", "is", net_host_device_remove
,
1760 "vlan_id name", "remove host VLAN client" },
1762 { "balloon", "i", do_balloon
,
1763 "target", "request VM to change it's memory allocation (in MB)" },
1764 { "set_link", "ss", do_set_link
,
1765 "name [up|down]", "change the link status of a network adapter" },
1766 { "acl", "sss?i?", do_acl
, "<command> <aclname> [<match>] [<index>]\n",
1767 "acl show vnc.username\n"
1768 "acl policy vnc.username deny\n"
1769 "acl allow vnc.username fred\n"
1770 "acl deny vnc.username bob\n"
1771 "acl reset vnc.username\n" },
1772 { "set_link", "ss", do_set_link
, "name [up|down]" },
1773 { "cpu_set", "is", do_cpu_set_nr
, "cpu [online|offline]", "change cpu state" },
1777 /* Please update qemu-doc.texi when adding or changing commands */
1778 static const mon_cmd_t info_cmds
[] = {
1779 { "version", "", do_info_version
,
1780 "", "show the version of QEMU" },
1781 { "network", "", do_info_network
,
1782 "", "show the network state" },
1783 { "chardev", "", qemu_chr_info
,
1784 "", "show the character devices" },
1785 { "block", "", bdrv_info
,
1786 "", "show the block devices" },
1787 { "blockstats", "", bdrv_info_stats
,
1788 "", "show block device statistics" },
1789 { "registers", "", do_info_registers
,
1790 "", "show the cpu registers" },
1791 { "cpus", "", do_info_cpus
,
1792 "", "show infos for each CPU" },
1793 { "history", "", do_info_history
,
1794 "", "show the command line history", },
1795 { "irq", "", irq_info
,
1796 "", "show the interrupts statistics (if available)", },
1797 { "pic", "", pic_info
,
1798 "", "show i8259 (PIC) state", },
1799 { "pci", "", pci_info
,
1800 "", "show PCI info", },
1801 #if defined(TARGET_I386) || defined(TARGET_SH4)
1802 { "tlb", "", tlb_info
,
1803 "", "show virtual to physical memory mappings", },
1805 #if defined(TARGET_I386)
1806 { "mem", "", mem_info
,
1807 "", "show the active virtual memory mappings", },
1808 { "hpet", "", do_info_hpet
,
1809 "", "show state of HPET", },
1811 { "jit", "", do_info_jit
,
1812 "", "show dynamic compiler info", },
1813 { "kqemu", "", do_info_kqemu
,
1814 "", "show KQEMU information", },
1815 { "kvm", "", do_info_kvm
,
1816 "", "show KVM information", },
1817 { "usb", "", usb_info
,
1818 "", "show guest USB devices", },
1819 { "usbhost", "", usb_host_info
,
1820 "", "show host USB devices", },
1821 { "profile", "", do_info_profile
,
1822 "", "show profiling information", },
1823 { "capture", "", do_info_capture
,
1824 "", "show capture information" },
1825 { "snapshots", "", do_info_snapshots
,
1826 "", "show the currently saved VM snapshots" },
1827 { "status", "", do_info_status
,
1828 "", "show the current VM status (running|paused)" },
1829 { "pcmcia", "", pcmcia_info
,
1830 "", "show guest PCMCIA status" },
1831 { "mice", "", do_info_mice
,
1832 "", "show which guest mouse is receiving events" },
1833 { "vnc", "", do_info_vnc
,
1834 "", "show the vnc server status"},
1835 { "name", "", do_info_name
,
1836 "", "show the current VM name" },
1837 { "uuid", "", do_info_uuid
,
1838 "", "show the current VM UUID" },
1839 #if defined(TARGET_PPC)
1840 { "cpustats", "", do_info_cpu_stats
,
1841 "", "show CPU statistics", },
1843 #if defined(CONFIG_SLIRP)
1844 { "slirp", "", do_info_slirp
,
1845 "", "show SLIRP statistics", },
1847 { "migrate", "", do_info_migrate
, "", "show migration status" },
1848 { "balloon", "", do_info_balloon
,
1849 "", "show balloon information" },
1853 /*******************************************************************/
1855 static const char *pch
;
1856 static jmp_buf expr_env
;
1861 typedef struct MonitorDef
{
1864 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1868 #if defined(TARGET_I386)
1869 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1871 CPUState
*env
= mon_get_cpu();
1874 return env
->eip
+ env
->segs
[R_CS
].base
;
1878 #if defined(TARGET_PPC)
1879 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1881 CPUState
*env
= mon_get_cpu();
1889 for (i
= 0; i
< 8; i
++)
1890 u
|= env
->crf
[i
] << (32 - (4 * i
));
1895 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1897 CPUState
*env
= mon_get_cpu();
1903 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1905 CPUState
*env
= mon_get_cpu();
1911 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1913 CPUState
*env
= mon_get_cpu();
1916 return cpu_ppc_load_decr(env
);
1919 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1921 CPUState
*env
= mon_get_cpu();
1924 return cpu_ppc_load_tbu(env
);
1927 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1929 CPUState
*env
= mon_get_cpu();
1932 return cpu_ppc_load_tbl(env
);
1936 #if defined(TARGET_SPARC)
1937 #ifndef TARGET_SPARC64
1938 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1940 CPUState
*env
= mon_get_cpu();
1943 return GET_PSR(env
);
1947 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1949 CPUState
*env
= mon_get_cpu();
1952 return env
->regwptr
[val
];
1956 static const MonitorDef monitor_defs
[] = {
1959 #define SEG(name, seg) \
1960 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1961 { name ".base", offsetof(CPUState, segs[seg].base) },\
1962 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1964 { "eax", offsetof(CPUState
, regs
[0]) },
1965 { "ecx", offsetof(CPUState
, regs
[1]) },
1966 { "edx", offsetof(CPUState
, regs
[2]) },
1967 { "ebx", offsetof(CPUState
, regs
[3]) },
1968 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1969 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1970 { "esi", offsetof(CPUState
, regs
[6]) },
1971 { "edi", offsetof(CPUState
, regs
[7]) },
1972 #ifdef TARGET_X86_64
1973 { "r8", offsetof(CPUState
, regs
[8]) },
1974 { "r9", offsetof(CPUState
, regs
[9]) },
1975 { "r10", offsetof(CPUState
, regs
[10]) },
1976 { "r11", offsetof(CPUState
, regs
[11]) },
1977 { "r12", offsetof(CPUState
, regs
[12]) },
1978 { "r13", offsetof(CPUState
, regs
[13]) },
1979 { "r14", offsetof(CPUState
, regs
[14]) },
1980 { "r15", offsetof(CPUState
, regs
[15]) },
1982 { "eflags", offsetof(CPUState
, eflags
) },
1983 { "eip", offsetof(CPUState
, eip
) },
1990 { "pc", 0, monitor_get_pc
, },
1991 #elif defined(TARGET_PPC)
1992 /* General purpose registers */
1993 { "r0", offsetof(CPUState
, gpr
[0]) },
1994 { "r1", offsetof(CPUState
, gpr
[1]) },
1995 { "r2", offsetof(CPUState
, gpr
[2]) },
1996 { "r3", offsetof(CPUState
, gpr
[3]) },
1997 { "r4", offsetof(CPUState
, gpr
[4]) },
1998 { "r5", offsetof(CPUState
, gpr
[5]) },
1999 { "r6", offsetof(CPUState
, gpr
[6]) },
2000 { "r7", offsetof(CPUState
, gpr
[7]) },
2001 { "r8", offsetof(CPUState
, gpr
[8]) },
2002 { "r9", offsetof(CPUState
, gpr
[9]) },
2003 { "r10", offsetof(CPUState
, gpr
[10]) },
2004 { "r11", offsetof(CPUState
, gpr
[11]) },
2005 { "r12", offsetof(CPUState
, gpr
[12]) },
2006 { "r13", offsetof(CPUState
, gpr
[13]) },
2007 { "r14", offsetof(CPUState
, gpr
[14]) },
2008 { "r15", offsetof(CPUState
, gpr
[15]) },
2009 { "r16", offsetof(CPUState
, gpr
[16]) },
2010 { "r17", offsetof(CPUState
, gpr
[17]) },
2011 { "r18", offsetof(CPUState
, gpr
[18]) },
2012 { "r19", offsetof(CPUState
, gpr
[19]) },
2013 { "r20", offsetof(CPUState
, gpr
[20]) },
2014 { "r21", offsetof(CPUState
, gpr
[21]) },
2015 { "r22", offsetof(CPUState
, gpr
[22]) },
2016 { "r23", offsetof(CPUState
, gpr
[23]) },
2017 { "r24", offsetof(CPUState
, gpr
[24]) },
2018 { "r25", offsetof(CPUState
, gpr
[25]) },
2019 { "r26", offsetof(CPUState
, gpr
[26]) },
2020 { "r27", offsetof(CPUState
, gpr
[27]) },
2021 { "r28", offsetof(CPUState
, gpr
[28]) },
2022 { "r29", offsetof(CPUState
, gpr
[29]) },
2023 { "r30", offsetof(CPUState
, gpr
[30]) },
2024 { "r31", offsetof(CPUState
, gpr
[31]) },
2025 /* Floating point registers */
2026 { "f0", offsetof(CPUState
, fpr
[0]) },
2027 { "f1", offsetof(CPUState
, fpr
[1]) },
2028 { "f2", offsetof(CPUState
, fpr
[2]) },
2029 { "f3", offsetof(CPUState
, fpr
[3]) },
2030 { "f4", offsetof(CPUState
, fpr
[4]) },
2031 { "f5", offsetof(CPUState
, fpr
[5]) },
2032 { "f6", offsetof(CPUState
, fpr
[6]) },
2033 { "f7", offsetof(CPUState
, fpr
[7]) },
2034 { "f8", offsetof(CPUState
, fpr
[8]) },
2035 { "f9", offsetof(CPUState
, fpr
[9]) },
2036 { "f10", offsetof(CPUState
, fpr
[10]) },
2037 { "f11", offsetof(CPUState
, fpr
[11]) },
2038 { "f12", offsetof(CPUState
, fpr
[12]) },
2039 { "f13", offsetof(CPUState
, fpr
[13]) },
2040 { "f14", offsetof(CPUState
, fpr
[14]) },
2041 { "f15", offsetof(CPUState
, fpr
[15]) },
2042 { "f16", offsetof(CPUState
, fpr
[16]) },
2043 { "f17", offsetof(CPUState
, fpr
[17]) },
2044 { "f18", offsetof(CPUState
, fpr
[18]) },
2045 { "f19", offsetof(CPUState
, fpr
[19]) },
2046 { "f20", offsetof(CPUState
, fpr
[20]) },
2047 { "f21", offsetof(CPUState
, fpr
[21]) },
2048 { "f22", offsetof(CPUState
, fpr
[22]) },
2049 { "f23", offsetof(CPUState
, fpr
[23]) },
2050 { "f24", offsetof(CPUState
, fpr
[24]) },
2051 { "f25", offsetof(CPUState
, fpr
[25]) },
2052 { "f26", offsetof(CPUState
, fpr
[26]) },
2053 { "f27", offsetof(CPUState
, fpr
[27]) },
2054 { "f28", offsetof(CPUState
, fpr
[28]) },
2055 { "f29", offsetof(CPUState
, fpr
[29]) },
2056 { "f30", offsetof(CPUState
, fpr
[30]) },
2057 { "f31", offsetof(CPUState
, fpr
[31]) },
2058 { "fpscr", offsetof(CPUState
, fpscr
) },
2059 /* Next instruction pointer */
2060 { "nip|pc", offsetof(CPUState
, nip
) },
2061 { "lr", offsetof(CPUState
, lr
) },
2062 { "ctr", offsetof(CPUState
, ctr
) },
2063 { "decr", 0, &monitor_get_decr
, },
2064 { "ccr", 0, &monitor_get_ccr
, },
2065 /* Machine state register */
2066 { "msr", 0, &monitor_get_msr
, },
2067 { "xer", 0, &monitor_get_xer
, },
2068 { "tbu", 0, &monitor_get_tbu
, },
2069 { "tbl", 0, &monitor_get_tbl
, },
2070 #if defined(TARGET_PPC64)
2071 /* Address space register */
2072 { "asr", offsetof(CPUState
, asr
) },
2074 /* Segment registers */
2075 { "sdr1", offsetof(CPUState
, sdr1
) },
2076 { "sr0", offsetof(CPUState
, sr
[0]) },
2077 { "sr1", offsetof(CPUState
, sr
[1]) },
2078 { "sr2", offsetof(CPUState
, sr
[2]) },
2079 { "sr3", offsetof(CPUState
, sr
[3]) },
2080 { "sr4", offsetof(CPUState
, sr
[4]) },
2081 { "sr5", offsetof(CPUState
, sr
[5]) },
2082 { "sr6", offsetof(CPUState
, sr
[6]) },
2083 { "sr7", offsetof(CPUState
, sr
[7]) },
2084 { "sr8", offsetof(CPUState
, sr
[8]) },
2085 { "sr9", offsetof(CPUState
, sr
[9]) },
2086 { "sr10", offsetof(CPUState
, sr
[10]) },
2087 { "sr11", offsetof(CPUState
, sr
[11]) },
2088 { "sr12", offsetof(CPUState
, sr
[12]) },
2089 { "sr13", offsetof(CPUState
, sr
[13]) },
2090 { "sr14", offsetof(CPUState
, sr
[14]) },
2091 { "sr15", offsetof(CPUState
, sr
[15]) },
2092 /* Too lazy to put BATs and SPRs ... */
2093 #elif defined(TARGET_SPARC)
2094 { "g0", offsetof(CPUState
, gregs
[0]) },
2095 { "g1", offsetof(CPUState
, gregs
[1]) },
2096 { "g2", offsetof(CPUState
, gregs
[2]) },
2097 { "g3", offsetof(CPUState
, gregs
[3]) },
2098 { "g4", offsetof(CPUState
, gregs
[4]) },
2099 { "g5", offsetof(CPUState
, gregs
[5]) },
2100 { "g6", offsetof(CPUState
, gregs
[6]) },
2101 { "g7", offsetof(CPUState
, gregs
[7]) },
2102 { "o0", 0, monitor_get_reg
},
2103 { "o1", 1, monitor_get_reg
},
2104 { "o2", 2, monitor_get_reg
},
2105 { "o3", 3, monitor_get_reg
},
2106 { "o4", 4, monitor_get_reg
},
2107 { "o5", 5, monitor_get_reg
},
2108 { "o6", 6, monitor_get_reg
},
2109 { "o7", 7, monitor_get_reg
},
2110 { "l0", 8, monitor_get_reg
},
2111 { "l1", 9, monitor_get_reg
},
2112 { "l2", 10, monitor_get_reg
},
2113 { "l3", 11, monitor_get_reg
},
2114 { "l4", 12, monitor_get_reg
},
2115 { "l5", 13, monitor_get_reg
},
2116 { "l6", 14, monitor_get_reg
},
2117 { "l7", 15, monitor_get_reg
},
2118 { "i0", 16, monitor_get_reg
},
2119 { "i1", 17, monitor_get_reg
},
2120 { "i2", 18, monitor_get_reg
},
2121 { "i3", 19, monitor_get_reg
},
2122 { "i4", 20, monitor_get_reg
},
2123 { "i5", 21, monitor_get_reg
},
2124 { "i6", 22, monitor_get_reg
},
2125 { "i7", 23, monitor_get_reg
},
2126 { "pc", offsetof(CPUState
, pc
) },
2127 { "npc", offsetof(CPUState
, npc
) },
2128 { "y", offsetof(CPUState
, y
) },
2129 #ifndef TARGET_SPARC64
2130 { "psr", 0, &monitor_get_psr
, },
2131 { "wim", offsetof(CPUState
, wim
) },
2133 { "tbr", offsetof(CPUState
, tbr
) },
2134 { "fsr", offsetof(CPUState
, fsr
) },
2135 { "f0", offsetof(CPUState
, fpr
[0]) },
2136 { "f1", offsetof(CPUState
, fpr
[1]) },
2137 { "f2", offsetof(CPUState
, fpr
[2]) },
2138 { "f3", offsetof(CPUState
, fpr
[3]) },
2139 { "f4", offsetof(CPUState
, fpr
[4]) },
2140 { "f5", offsetof(CPUState
, fpr
[5]) },
2141 { "f6", offsetof(CPUState
, fpr
[6]) },
2142 { "f7", offsetof(CPUState
, fpr
[7]) },
2143 { "f8", offsetof(CPUState
, fpr
[8]) },
2144 { "f9", offsetof(CPUState
, fpr
[9]) },
2145 { "f10", offsetof(CPUState
, fpr
[10]) },
2146 { "f11", offsetof(CPUState
, fpr
[11]) },
2147 { "f12", offsetof(CPUState
, fpr
[12]) },
2148 { "f13", offsetof(CPUState
, fpr
[13]) },
2149 { "f14", offsetof(CPUState
, fpr
[14]) },
2150 { "f15", offsetof(CPUState
, fpr
[15]) },
2151 { "f16", offsetof(CPUState
, fpr
[16]) },
2152 { "f17", offsetof(CPUState
, fpr
[17]) },
2153 { "f18", offsetof(CPUState
, fpr
[18]) },
2154 { "f19", offsetof(CPUState
, fpr
[19]) },
2155 { "f20", offsetof(CPUState
, fpr
[20]) },
2156 { "f21", offsetof(CPUState
, fpr
[21]) },
2157 { "f22", offsetof(CPUState
, fpr
[22]) },
2158 { "f23", offsetof(CPUState
, fpr
[23]) },
2159 { "f24", offsetof(CPUState
, fpr
[24]) },
2160 { "f25", offsetof(CPUState
, fpr
[25]) },
2161 { "f26", offsetof(CPUState
, fpr
[26]) },
2162 { "f27", offsetof(CPUState
, fpr
[27]) },
2163 { "f28", offsetof(CPUState
, fpr
[28]) },
2164 { "f29", offsetof(CPUState
, fpr
[29]) },
2165 { "f30", offsetof(CPUState
, fpr
[30]) },
2166 { "f31", offsetof(CPUState
, fpr
[31]) },
2167 #ifdef TARGET_SPARC64
2168 { "f32", offsetof(CPUState
, fpr
[32]) },
2169 { "f34", offsetof(CPUState
, fpr
[34]) },
2170 { "f36", offsetof(CPUState
, fpr
[36]) },
2171 { "f38", offsetof(CPUState
, fpr
[38]) },
2172 { "f40", offsetof(CPUState
, fpr
[40]) },
2173 { "f42", offsetof(CPUState
, fpr
[42]) },
2174 { "f44", offsetof(CPUState
, fpr
[44]) },
2175 { "f46", offsetof(CPUState
, fpr
[46]) },
2176 { "f48", offsetof(CPUState
, fpr
[48]) },
2177 { "f50", offsetof(CPUState
, fpr
[50]) },
2178 { "f52", offsetof(CPUState
, fpr
[52]) },
2179 { "f54", offsetof(CPUState
, fpr
[54]) },
2180 { "f56", offsetof(CPUState
, fpr
[56]) },
2181 { "f58", offsetof(CPUState
, fpr
[58]) },
2182 { "f60", offsetof(CPUState
, fpr
[60]) },
2183 { "f62", offsetof(CPUState
, fpr
[62]) },
2184 { "asi", offsetof(CPUState
, asi
) },
2185 { "pstate", offsetof(CPUState
, pstate
) },
2186 { "cansave", offsetof(CPUState
, cansave
) },
2187 { "canrestore", offsetof(CPUState
, canrestore
) },
2188 { "otherwin", offsetof(CPUState
, otherwin
) },
2189 { "wstate", offsetof(CPUState
, wstate
) },
2190 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2191 { "fprs", offsetof(CPUState
, fprs
) },
2197 static void expr_error(Monitor
*mon
, const char *msg
)
2199 monitor_printf(mon
, "%s\n", msg
);
2200 longjmp(expr_env
, 1);
2203 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2204 static int get_monitor_def(target_long
*pval
, const char *name
)
2206 const MonitorDef
*md
;
2209 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2210 if (compare_cmd(name
, md
->name
)) {
2211 if (md
->get_value
) {
2212 *pval
= md
->get_value(md
, md
->offset
);
2214 CPUState
*env
= mon_get_cpu();
2217 ptr
= (uint8_t *)env
+ md
->offset
;
2220 *pval
= *(int32_t *)ptr
;
2223 *pval
= *(target_long
*)ptr
;
2236 static void next(void)
2240 while (qemu_isspace(*pch
))
2245 static int64_t expr_sum(Monitor
*mon
);
2247 static int64_t expr_unary(Monitor
*mon
)
2256 n
= expr_unary(mon
);
2260 n
= -expr_unary(mon
);
2264 n
= ~expr_unary(mon
);
2270 expr_error(mon
, "')' expected");
2277 expr_error(mon
, "character constant expected");
2281 expr_error(mon
, "missing terminating \' character");
2291 while ((*pch
>= 'a' && *pch
<= 'z') ||
2292 (*pch
>= 'A' && *pch
<= 'Z') ||
2293 (*pch
>= '0' && *pch
<= '9') ||
2294 *pch
== '_' || *pch
== '.') {
2295 if ((q
- buf
) < sizeof(buf
) - 1)
2299 while (qemu_isspace(*pch
))
2302 ret
= get_monitor_def(®
, buf
);
2304 expr_error(mon
, "unknown register");
2306 expr_error(mon
, "no cpu defined");
2311 expr_error(mon
, "unexpected end of expression");
2315 #if TARGET_PHYS_ADDR_BITS > 32
2316 n
= strtoull(pch
, &p
, 0);
2318 n
= strtoul(pch
, &p
, 0);
2321 expr_error(mon
, "invalid char in expression");
2324 while (qemu_isspace(*pch
))
2332 static int64_t expr_prod(Monitor
*mon
)
2337 val
= expr_unary(mon
);
2340 if (op
!= '*' && op
!= '/' && op
!= '%')
2343 val2
= expr_unary(mon
);
2352 expr_error(mon
, "division by zero");
2363 static int64_t expr_logic(Monitor
*mon
)
2368 val
= expr_prod(mon
);
2371 if (op
!= '&' && op
!= '|' && op
!= '^')
2374 val2
= expr_prod(mon
);
2391 static int64_t expr_sum(Monitor
*mon
)
2396 val
= expr_logic(mon
);
2399 if (op
!= '+' && op
!= '-')
2402 val2
= expr_logic(mon
);
2411 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2414 if (setjmp(expr_env
)) {
2418 while (qemu_isspace(*pch
))
2420 *pval
= expr_sum(mon
);
2425 static int get_str(char *buf
, int buf_size
, const char **pp
)
2433 while (qemu_isspace(*p
))
2443 while (*p
!= '\0' && *p
!= '\"') {
2459 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2462 if ((q
- buf
) < buf_size
- 1) {
2466 if ((q
- buf
) < buf_size
- 1) {
2473 qemu_printf("unterminated string\n");
2478 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2479 if ((q
- buf
) < buf_size
- 1) {
2490 static int default_fmt_format
= 'x';
2491 static int default_fmt_size
= 4;
2495 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2497 const char *p
, *pstart
, *typestr
;
2499 int c
, nb_args
, len
, i
, has_arg
;
2500 const mon_cmd_t
*cmd
;
2503 void *str_allocated
[MAX_ARGS
];
2504 void *args
[MAX_ARGS
];
2505 void (*handler_0
)(Monitor
*mon
);
2506 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2507 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2508 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2509 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2511 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2512 void *arg3
, void *arg4
);
2513 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2514 void *arg3
, void *arg4
, void *arg5
);
2515 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2516 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2519 monitor_printf(mon
, "command='%s'\n", cmdline
);
2522 /* extract the command name */
2525 while (qemu_isspace(*p
))
2530 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2533 if (len
> sizeof(cmdname
) - 1)
2534 len
= sizeof(cmdname
) - 1;
2535 memcpy(cmdname
, pstart
, len
);
2536 cmdname
[len
] = '\0';
2538 /* find the command */
2539 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2540 if (compare_cmd(cmdname
, cmd
->name
))
2543 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2547 for(i
= 0; i
< MAX_ARGS
; i
++)
2548 str_allocated
[i
] = NULL
;
2550 /* parse the parameters */
2551 typestr
= cmd
->args_type
;
2566 while (qemu_isspace(*p
))
2568 if (*typestr
== '?') {
2571 /* no optional string: NULL argument */
2576 ret
= get_str(buf
, sizeof(buf
), &p
);
2580 monitor_printf(mon
, "%s: filename expected\n",
2584 monitor_printf(mon
, "%s: block device name expected\n",
2588 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2593 str
= qemu_malloc(strlen(buf
) + 1);
2594 pstrcpy(str
, sizeof(buf
), buf
);
2595 str_allocated
[nb_args
] = str
;
2597 if (nb_args
>= MAX_ARGS
) {
2599 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2602 args
[nb_args
++] = str
;
2607 int count
, format
, size
;
2609 while (qemu_isspace(*p
))
2615 if (qemu_isdigit(*p
)) {
2617 while (qemu_isdigit(*p
)) {
2618 count
= count
* 10 + (*p
- '0');
2656 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2657 monitor_printf(mon
, "invalid char in format: '%c'\n",
2662 format
= default_fmt_format
;
2663 if (format
!= 'i') {
2664 /* for 'i', not specifying a size gives -1 as size */
2666 size
= default_fmt_size
;
2667 default_fmt_size
= size
;
2669 default_fmt_format
= format
;
2672 format
= default_fmt_format
;
2673 if (format
!= 'i') {
2674 size
= default_fmt_size
;
2679 if (nb_args
+ 3 > MAX_ARGS
)
2681 args
[nb_args
++] = (void*)(long)count
;
2682 args
[nb_args
++] = (void*)(long)format
;
2683 args
[nb_args
++] = (void*)(long)size
;
2691 while (qemu_isspace(*p
))
2693 if (*typestr
== '?' || *typestr
== '.') {
2694 if (*typestr
== '?') {
2702 while (qemu_isspace(*p
))
2710 if (nb_args
>= MAX_ARGS
)
2712 args
[nb_args
++] = (void *)(long)has_arg
;
2714 if (nb_args
>= MAX_ARGS
)
2720 if (get_expr(mon
, &val
, &p
))
2724 if (nb_args
>= MAX_ARGS
)
2726 args
[nb_args
++] = (void *)(long)val
;
2728 if ((nb_args
+ 1) >= MAX_ARGS
)
2730 #if TARGET_PHYS_ADDR_BITS > 32
2731 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2733 args
[nb_args
++] = (void *)0;
2735 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2747 while (qemu_isspace(*p
))
2753 monitor_printf(mon
, "%s: unsupported option -%c\n",
2760 if (nb_args
>= MAX_ARGS
)
2762 args
[nb_args
++] = (void *)(long)has_option
;
2767 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2771 /* check that all arguments were parsed */
2772 while (qemu_isspace(*p
))
2775 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2782 handler_0
= cmd
->handler
;
2786 handler_1
= cmd
->handler
;
2787 handler_1(mon
, args
[0]);
2790 handler_2
= cmd
->handler
;
2791 handler_2(mon
, args
[0], args
[1]);
2794 handler_3
= cmd
->handler
;
2795 handler_3(mon
, args
[0], args
[1], args
[2]);
2798 handler_4
= cmd
->handler
;
2799 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2802 handler_5
= cmd
->handler
;
2803 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2806 handler_6
= cmd
->handler
;
2807 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2810 handler_7
= cmd
->handler
;
2811 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2815 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2819 for(i
= 0; i
< MAX_ARGS
; i
++)
2820 qemu_free(str_allocated
[i
]);
2824 static void cmd_completion(const char *name
, const char *list
)
2826 const char *p
, *pstart
;
2835 p
= pstart
+ strlen(pstart
);
2837 if (len
> sizeof(cmd
) - 2)
2838 len
= sizeof(cmd
) - 2;
2839 memcpy(cmd
, pstart
, len
);
2841 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2842 readline_add_completion(cur_mon
->rs
, cmd
);
2850 static void file_completion(const char *input
)
2855 char file
[1024], file_prefix
[1024];
2859 p
= strrchr(input
, '/');
2862 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2863 pstrcpy(path
, sizeof(path
), ".");
2865 input_path_len
= p
- input
+ 1;
2866 memcpy(path
, input
, input_path_len
);
2867 if (input_path_len
> sizeof(path
) - 1)
2868 input_path_len
= sizeof(path
) - 1;
2869 path
[input_path_len
] = '\0';
2870 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2872 #ifdef DEBUG_COMPLETION
2873 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2874 input
, path
, file_prefix
);
2876 ffs
= opendir(path
);
2884 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2885 memcpy(file
, input
, input_path_len
);
2886 if (input_path_len
< sizeof(file
))
2887 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2889 /* stat the file to find out if it's a directory.
2890 * In that case add a slash to speed up typing long paths
2893 if(S_ISDIR(sb
.st_mode
))
2894 pstrcat(file
, sizeof(file
), "/");
2895 readline_add_completion(cur_mon
->rs
, file
);
2901 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2903 const char *name
= bdrv_get_device_name(bs
);
2904 const char *input
= opaque
;
2906 if (input
[0] == '\0' ||
2907 !strncmp(name
, (char *)input
, strlen(input
))) {
2908 readline_add_completion(cur_mon
->rs
, name
);
2912 /* NOTE: this parser is an approximate form of the real command parser */
2913 static void parse_cmdline(const char *cmdline
,
2914 int *pnb_args
, char **args
)
2923 while (qemu_isspace(*p
))
2927 if (nb_args
>= MAX_ARGS
)
2929 ret
= get_str(buf
, sizeof(buf
), &p
);
2930 args
[nb_args
] = qemu_strdup(buf
);
2935 *pnb_args
= nb_args
;
2938 static void monitor_find_completion(const char *cmdline
)
2940 const char *cmdname
;
2941 char *args
[MAX_ARGS
];
2942 int nb_args
, i
, len
;
2943 const char *ptype
, *str
;
2944 const mon_cmd_t
*cmd
;
2947 parse_cmdline(cmdline
, &nb_args
, args
);
2948 #ifdef DEBUG_COMPLETION
2949 for(i
= 0; i
< nb_args
; i
++) {
2950 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2954 /* if the line ends with a space, it means we want to complete the
2956 len
= strlen(cmdline
);
2957 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2958 if (nb_args
>= MAX_ARGS
)
2960 args
[nb_args
++] = qemu_strdup("");
2963 /* command completion */
2968 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
2969 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2970 cmd_completion(cmdname
, cmd
->name
);
2973 /* find the command */
2974 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2975 if (compare_cmd(args
[0], cmd
->name
))
2980 ptype
= cmd
->args_type
;
2981 for(i
= 0; i
< nb_args
- 2; i
++) {
2982 if (*ptype
!= '\0') {
2984 while (*ptype
== '?')
2988 str
= args
[nb_args
- 1];
2991 /* file completion */
2992 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
2993 file_completion(str
);
2996 /* block device name completion */
2997 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
2998 bdrv_iterate(block_completion_it
, (void *)str
);
3001 /* XXX: more generic ? */
3002 if (!strcmp(cmd
->name
, "info")) {
3003 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3004 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3005 cmd_completion(str
, cmd
->name
);
3007 } else if (!strcmp(cmd
->name
, "sendkey")) {
3008 char *sep
= strrchr(str
, '-');
3011 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3012 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3013 cmd_completion(str
, key
->name
);
3021 for(i
= 0; i
< nb_args
; i
++)
3025 static int monitor_can_read(void *opaque
)
3027 Monitor
*mon
= opaque
;
3029 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3032 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3034 Monitor
*old_mon
= cur_mon
;
3040 for (i
= 0; i
< size
; i
++)
3041 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3043 if (size
== 0 || buf
[size
- 1] != 0)
3044 monitor_printf(cur_mon
, "corrupted command\n");
3046 monitor_handle_command(cur_mon
, (char *)buf
);
3052 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3054 monitor_suspend(mon
);
3055 monitor_handle_command(mon
, cmdline
);
3056 monitor_resume(mon
);
3059 int monitor_suspend(Monitor
*mon
)
3067 void monitor_resume(Monitor
*mon
)
3071 if (--mon
->suspend_cnt
== 0)
3072 readline_show_prompt(mon
->rs
);
3075 static void monitor_event(void *opaque
, int event
)
3077 Monitor
*mon
= opaque
;
3080 case CHR_EVENT_MUX_IN
:
3081 readline_restart(mon
->rs
);
3082 monitor_resume(mon
);
3086 case CHR_EVENT_MUX_OUT
:
3087 if (mon
->suspend_cnt
== 0)
3088 monitor_printf(mon
, "\n");
3090 monitor_suspend(mon
);
3093 case CHR_EVENT_RESET
:
3094 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3095 "information\n", QEMU_VERSION
);
3096 if (mon
->chr
->focus
== 0)
3097 readline_show_prompt(mon
->rs
);
3111 void monitor_init(CharDriverState
*chr
, int flags
)
3113 static int is_first_init
= 1;
3116 if (is_first_init
) {
3117 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3121 mon
= qemu_mallocz(sizeof(*mon
));
3125 if (mon
->chr
->focus
!= 0)
3126 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3127 if (flags
& MONITOR_USE_READLINE
) {
3128 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3129 monitor_read_command(mon
, 0);
3132 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3135 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3136 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3140 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3142 BlockDriverState
*bs
= opaque
;
3145 if (bdrv_set_key(bs
, password
) != 0) {
3146 monitor_printf(mon
, "invalid password\n");
3149 if (mon
->password_completion_cb
)
3150 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3152 monitor_read_command(mon
, 1);
3155 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3156 BlockDriverCompletionFunc
*completion_cb
,
3161 if (!bdrv_key_required(bs
)) {
3163 completion_cb(opaque
, 0);
3167 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3168 bdrv_get_encrypted_filename(bs
));
3170 mon
->password_completion_cb
= completion_cb
;
3171 mon
->password_opaque
= opaque
;
3173 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3175 if (err
&& completion_cb
)
3176 completion_cb(opaque
, err
);