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
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
34 #include "qemu-char.h"
40 #include "audio/audio.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
52 //#define DEBUG_COMPLETION
58 * 'B' block device name
59 * 's' string (accept optional quote)
61 * 'l' target long (32 or 64 bit)
62 * '/' optional gdb-like print format (like "/10x")
64 * '?' optional type (for 'F', 's' and 'i')
68 typedef struct mon_cmd_t
{
70 const char *args_type
;
84 BlockDriverCompletionFunc
*password_completion_cb
;
85 void *password_opaque
;
86 LIST_ENTRY(Monitor
) entry
;
89 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
91 static const mon_cmd_t mon_cmds
[];
92 static const mon_cmd_t info_cmds
[];
94 Monitor
*cur_mon
= NULL
;
96 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
99 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
101 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
103 readline_show_prompt(mon
->rs
);
106 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
110 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
111 /* prompt is printed on return from the command handler */
114 monitor_printf(mon
, "terminal does not support password prompting\n");
119 void monitor_flush(Monitor
*mon
)
121 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
122 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
123 mon
->outbuf_index
= 0;
127 /* flush at every end of line or if the buffer is full */
128 static void monitor_puts(Monitor
*mon
, const char *str
)
140 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
141 mon
->outbuf
[mon
->outbuf_index
++] = c
;
142 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
148 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
151 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
152 monitor_puts(mon
, buf
);
155 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
159 monitor_vprintf(mon
, fmt
, ap
);
163 void monitor_print_filename(Monitor
*mon
, const char *filename
)
167 for (i
= 0; filename
[i
]; i
++) {
168 switch (filename
[i
]) {
172 monitor_printf(mon
, "\\%c", filename
[i
]);
175 monitor_printf(mon
, "\\t");
178 monitor_printf(mon
, "\\r");
181 monitor_printf(mon
, "\\n");
184 monitor_printf(mon
, "%c", filename
[i
]);
190 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
194 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
199 static int compare_cmd(const char *name
, const char *list
)
201 const char *p
, *pstart
;
209 p
= pstart
+ strlen(pstart
);
210 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
219 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
220 const char *prefix
, const char *name
)
222 const mon_cmd_t
*cmd
;
224 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
225 if (!name
|| !strcmp(name
, cmd
->name
))
226 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
227 cmd
->params
, cmd
->help
);
231 static void help_cmd(Monitor
*mon
, const char *name
)
233 if (name
&& !strcmp(name
, "info")) {
234 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
236 help_cmd_dump(mon
, mon_cmds
, "", name
);
237 if (name
&& !strcmp(name
, "log")) {
238 const CPULogItem
*item
;
239 monitor_printf(mon
, "Log items (comma separated):\n");
240 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
241 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
242 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
248 static void do_commit(Monitor
*mon
, const char *device
)
252 all_devices
= !strcmp(device
, "all");
253 for (i
= 0; i
< nb_drives
; i
++) {
255 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
256 bdrv_commit(drives_table
[i
].bdrv
);
260 static void do_info(Monitor
*mon
, const char *item
)
262 const mon_cmd_t
*cmd
;
263 void (*handler
)(Monitor
*);
267 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
268 if (compare_cmd(item
, cmd
->name
))
272 help_cmd(mon
, "info");
275 handler
= cmd
->handler
;
279 static void do_info_version(Monitor
*mon
)
281 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
284 static void do_info_name(Monitor
*mon
)
287 monitor_printf(mon
, "%s\n", qemu_name
);
290 #if defined(TARGET_I386)
291 static void do_info_hpet(Monitor
*mon
)
293 monitor_printf(mon
, "HPET is %s by QEMU\n",
294 (no_hpet
) ? "disabled" : "enabled");
298 static void do_info_uuid(Monitor
*mon
)
300 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
301 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
302 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
303 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
304 qemu_uuid
[14], qemu_uuid
[15]);
307 /* get the current CPU defined by the user */
308 static int mon_set_cpu(int cpu_index
)
312 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
313 if (env
->cpu_index
== cpu_index
) {
314 cur_mon
->mon_cpu
= env
;
321 static CPUState
*mon_get_cpu(void)
323 if (!cur_mon
->mon_cpu
) {
326 cpu_synchronize_state(cur_mon
->mon_cpu
, 0);
327 return cur_mon
->mon_cpu
;
330 static void do_info_registers(Monitor
*mon
)
337 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
340 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
345 static void do_info_cpus(Monitor
*mon
)
349 /* just to set the default cpu if not already done */
352 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
353 cpu_synchronize_state(env
, 0);
354 monitor_printf(mon
, "%c CPU #%d:",
355 (env
== mon
->mon_cpu
) ? '*' : ' ',
357 #if defined(TARGET_I386)
358 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
359 env
->eip
+ env
->segs
[R_CS
].base
);
360 #elif defined(TARGET_PPC)
361 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
362 #elif defined(TARGET_SPARC)
363 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
365 #elif defined(TARGET_MIPS)
366 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
369 monitor_printf(mon
, " (halted)");
370 monitor_printf(mon
," thread_id=%d", env
->thread_id
);
371 monitor_printf(mon
, "\n");
375 static void do_cpu_set(Monitor
*mon
, int index
)
377 if (mon_set_cpu(index
) < 0)
378 monitor_printf(mon
, "Invalid CPU index\n");
381 static void do_cpu_set_nr(Monitor
*mon
, int value
, const char *status
)
385 if (!strcmp(status
, "online"))
387 else if (!strcmp(status
, "offline"))
390 monitor_printf(mon
, "invalid status: %s\n", status
);
393 #if defined(TARGET_I386) || defined(TARGET_X86_64)
394 qemu_system_cpu_hot_add(value
, state
);
398 static void do_info_jit(Monitor
*mon
)
400 dump_exec_info((FILE *)mon
, monitor_fprintf
);
403 static void do_info_history(Monitor
*mon
)
412 str
= readline_get_history(mon
->rs
, i
);
415 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
420 #if defined(TARGET_PPC)
421 /* XXX: not implemented in other targets */
422 static void do_info_cpu_stats(Monitor
*mon
)
427 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
431 static void do_quit(Monitor
*mon
)
436 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
438 if (bdrv_is_inserted(bs
)) {
440 if (!bdrv_is_removable(bs
)) {
441 monitor_printf(mon
, "device is not removable\n");
444 if (bdrv_is_locked(bs
)) {
445 monitor_printf(mon
, "device is locked\n");
454 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
456 BlockDriverState
*bs
;
458 bs
= bdrv_find(filename
);
460 monitor_printf(mon
, "device not found\n");
463 eject_device(mon
, bs
, force
);
466 static void do_change_block(Monitor
*mon
, const char *device
,
467 const char *filename
, const char *fmt
)
469 BlockDriverState
*bs
;
470 BlockDriver
*drv
= NULL
;
472 bs
= bdrv_find(device
);
474 monitor_printf(mon
, "device not found\n");
478 drv
= bdrv_find_format(fmt
);
480 monitor_printf(mon
, "invalid format %s\n", fmt
);
484 if (eject_device(mon
, bs
, 0) < 0)
486 bdrv_open2(bs
, filename
, 0, drv
);
487 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
490 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
493 if (vnc_display_password(NULL
, password
) < 0)
494 monitor_printf(mon
, "could not set VNC server password\n");
496 monitor_read_command(mon
, 1);
499 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
501 if (strcmp(target
, "passwd") == 0 ||
502 strcmp(target
, "password") == 0) {
505 strncpy(password
, arg
, sizeof(password
));
506 password
[sizeof(password
) - 1] = '\0';
507 change_vnc_password_cb(mon
, password
, NULL
);
509 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
512 if (vnc_display_open(NULL
, target
) < 0)
513 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
517 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
520 if (strcmp(device
, "vnc") == 0) {
521 do_change_vnc(mon
, target
, arg
);
523 do_change_block(mon
, device
, target
, arg
);
527 static void do_screen_dump(Monitor
*mon
, const char *filename
)
529 vga_hw_screen_dump(filename
);
532 static void do_logfile(Monitor
*mon
, const char *filename
)
534 cpu_set_log_filename(filename
);
537 static void do_log(Monitor
*mon
, const char *items
)
541 if (!strcmp(items
, "none")) {
544 mask
= cpu_str_to_log_mask(items
);
546 help_cmd(mon
, "log");
553 static void do_singlestep(Monitor
*mon
, const char *option
)
555 if (!option
|| !strcmp(option
, "on")) {
557 } else if (!strcmp(option
, "off")) {
560 monitor_printf(mon
, "unexpected option %s\n", option
);
564 static void do_stop(Monitor
*mon
)
566 vm_stop(EXCP_INTERRUPT
);
569 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
571 struct bdrv_iterate_context
{
576 static void do_cont(Monitor
*mon
)
578 struct bdrv_iterate_context context
= { mon
, 0 };
580 bdrv_iterate(encrypted_bdrv_it
, &context
);
581 /* only resume the vm if all keys are set and valid */
586 static void bdrv_key_cb(void *opaque
, int err
)
588 Monitor
*mon
= opaque
;
590 /* another key was set successfully, retry to continue */
595 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
597 struct bdrv_iterate_context
*context
= opaque
;
599 if (!context
->err
&& bdrv_key_required(bs
)) {
600 context
->err
= -EBUSY
;
601 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
606 static void do_gdbserver(Monitor
*mon
, const char *device
)
609 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
610 if (gdbserver_start(device
) < 0) {
611 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
613 } else if (strcmp(device
, "none") == 0) {
614 monitor_printf(mon
, "Disabled gdbserver\n");
616 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
621 static void do_watchdog_action(Monitor
*mon
, const char *action
)
623 if (select_watchdog_action(action
) == -1) {
624 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
628 static void monitor_printc(Monitor
*mon
, int c
)
630 monitor_printf(mon
, "'");
633 monitor_printf(mon
, "\\'");
636 monitor_printf(mon
, "\\\\");
639 monitor_printf(mon
, "\\n");
642 monitor_printf(mon
, "\\r");
645 if (c
>= 32 && c
<= 126) {
646 monitor_printf(mon
, "%c", c
);
648 monitor_printf(mon
, "\\x%02x", c
);
652 monitor_printf(mon
, "'");
655 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
656 target_phys_addr_t addr
, int is_physical
)
659 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
667 if (!env
&& !is_physical
)
672 } else if (wsize
== 4) {
675 /* as default we use the current CS size */
679 if ((env
->efer
& MSR_EFER_LMA
) &&
680 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
684 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
689 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
698 nb_per_line
= line_size
/ wsize
;
703 max_digits
= (wsize
* 8 + 2) / 3;
707 max_digits
= (wsize
* 8) / 4;
711 max_digits
= (wsize
* 8 * 10 + 32) / 33;
720 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
722 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
727 cpu_physical_memory_rw(addr
, buf
, l
, 0);
732 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
733 monitor_printf(mon
, " Cannot access memory\n");
742 v
= ldub_raw(buf
+ i
);
745 v
= lduw_raw(buf
+ i
);
748 v
= (uint32_t)ldl_raw(buf
+ i
);
751 v
= ldq_raw(buf
+ i
);
754 monitor_printf(mon
, " ");
757 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
760 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
763 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
766 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
769 monitor_printc(mon
, v
);
774 monitor_printf(mon
, "\n");
780 #if TARGET_LONG_BITS == 64
781 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
783 #define GET_TLONG(h, l) (l)
786 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
787 uint32_t addrh
, uint32_t addrl
)
789 target_long addr
= GET_TLONG(addrh
, addrl
);
790 memory_dump(mon
, count
, format
, size
, addr
, 0);
793 #if TARGET_PHYS_ADDR_BITS > 32
794 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
796 #define GET_TPHYSADDR(h, l) (l)
799 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
800 int size
, uint32_t addrh
, uint32_t addrl
)
803 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
804 memory_dump(mon
, count
, format
, size
, addr
, 1);
807 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
808 unsigned int valh
, unsigned int vall
)
810 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
811 #if TARGET_PHYS_ADDR_BITS == 32
814 monitor_printf(mon
, "%#o", val
);
817 monitor_printf(mon
, "%#x", val
);
820 monitor_printf(mon
, "%u", val
);
824 monitor_printf(mon
, "%d", val
);
827 monitor_printc(mon
, val
);
833 monitor_printf(mon
, "%#" PRIo64
, val
);
836 monitor_printf(mon
, "%#" PRIx64
, val
);
839 monitor_printf(mon
, "%" PRIu64
, val
);
843 monitor_printf(mon
, "%" PRId64
, val
);
846 monitor_printc(mon
, val
);
850 monitor_printf(mon
, "\n");
853 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
854 uint32_t size
, const char *filename
)
857 target_long addr
= GET_TLONG(valh
, vall
);
866 f
= fopen(filename
, "wb");
868 monitor_printf(mon
, "could not open '%s'\n", filename
);
875 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
876 fwrite(buf
, 1, l
, f
);
883 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
884 unsigned int vall
, uint32_t size
,
885 const char *filename
)
890 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
892 f
= fopen(filename
, "wb");
894 monitor_printf(mon
, "could not open '%s'\n", filename
);
901 cpu_physical_memory_rw(addr
, buf
, l
, 0);
902 fwrite(buf
, 1, l
, f
);
910 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
917 for(addr
= start
; addr
< (start
+ size
); addr
++) {
918 cpu_physical_memory_rw(addr
, buf
, 1, 0);
919 /* BSD sum algorithm ('sum' Unix command) */
920 sum
= (sum
>> 1) | (sum
<< 15);
923 monitor_printf(mon
, "%05d\n", sum
);
931 static const KeyDef key_defs
[] = {
958 { 0x0e, "backspace" },
995 { 0x37, "asterisk" },
998 { 0x3a, "caps_lock" },
1009 { 0x45, "num_lock" },
1010 { 0x46, "scroll_lock" },
1012 { 0xb5, "kp_divide" },
1013 { 0x37, "kp_multiply" },
1014 { 0x4a, "kp_subtract" },
1016 { 0x9c, "kp_enter" },
1017 { 0x53, "kp_decimal" },
1050 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1065 { 0xfe, "compose" },
1070 static int get_keycode(const char *key
)
1076 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1077 if (!strcmp(key
, p
->name
))
1080 if (strstart(key
, "0x", NULL
)) {
1081 ret
= strtoul(key
, &endp
, 0);
1082 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1088 #define MAX_KEYCODES 16
1089 static uint8_t keycodes
[MAX_KEYCODES
];
1090 static int nb_pending_keycodes
;
1091 static QEMUTimer
*key_timer
;
1093 static void release_keys(void *opaque
)
1097 while (nb_pending_keycodes
> 0) {
1098 nb_pending_keycodes
--;
1099 keycode
= keycodes
[nb_pending_keycodes
];
1101 kbd_put_keycode(0xe0);
1102 kbd_put_keycode(keycode
| 0x80);
1106 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1109 char keyname_buf
[16];
1111 int keyname_len
, keycode
, i
;
1113 if (nb_pending_keycodes
> 0) {
1114 qemu_del_timer(key_timer
);
1121 separator
= strchr(string
, '-');
1122 keyname_len
= separator
? separator
- string
: strlen(string
);
1123 if (keyname_len
> 0) {
1124 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1125 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1126 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1129 if (i
== MAX_KEYCODES
) {
1130 monitor_printf(mon
, "too many keys\n");
1133 keyname_buf
[keyname_len
] = 0;
1134 keycode
= get_keycode(keyname_buf
);
1136 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1139 keycodes
[i
++] = keycode
;
1143 string
= separator
+ 1;
1145 nb_pending_keycodes
= i
;
1146 /* key down events */
1147 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1148 keycode
= keycodes
[i
];
1150 kbd_put_keycode(0xe0);
1151 kbd_put_keycode(keycode
& 0x7f);
1153 /* delayed key up events */
1154 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1155 muldiv64(ticks_per_sec
, hold_time
, 1000));
1158 static int mouse_button_state
;
1160 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1164 dx
= strtol(dx_str
, NULL
, 0);
1165 dy
= strtol(dy_str
, NULL
, 0);
1168 dz
= strtol(dz_str
, NULL
, 0);
1169 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1172 static void do_mouse_button(Monitor
*mon
, int button_state
)
1174 mouse_button_state
= button_state
;
1175 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1178 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1179 int addr
, int has_index
, int index
)
1185 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1193 val
= cpu_inb(NULL
, addr
);
1197 val
= cpu_inw(NULL
, addr
);
1201 val
= cpu_inl(NULL
, addr
);
1205 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1206 suffix
, addr
, size
* 2, val
);
1209 /* boot_set handler */
1210 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1211 static void *boot_opaque
;
1213 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1215 qemu_boot_set_handler
= func
;
1216 boot_opaque
= opaque
;
1219 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1223 if (qemu_boot_set_handler
) {
1224 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1226 monitor_printf(mon
, "boot device list now set to %s\n",
1229 monitor_printf(mon
, "setting boot device list failed with "
1232 monitor_printf(mon
, "no function defined to set boot device list for "
1233 "this architecture\n");
1237 static void do_system_reset(Monitor
*mon
)
1239 qemu_system_reset_request();
1242 static void do_system_powerdown(Monitor
*mon
)
1244 qemu_system_powerdown_request();
1247 #if defined(TARGET_I386)
1248 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1250 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1253 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1254 pte
& PG_PSE_MASK
? 'P' : '-',
1255 pte
& PG_DIRTY_MASK
? 'D' : '-',
1256 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1257 pte
& PG_PCD_MASK
? 'C' : '-',
1258 pte
& PG_PWT_MASK
? 'T' : '-',
1259 pte
& PG_USER_MASK
? 'U' : '-',
1260 pte
& PG_RW_MASK
? 'W' : '-');
1263 static void tlb_info(Monitor
*mon
)
1267 uint32_t pgd
, pde
, pte
;
1269 env
= mon_get_cpu();
1273 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1274 monitor_printf(mon
, "PG disabled\n");
1277 pgd
= env
->cr
[3] & ~0xfff;
1278 for(l1
= 0; l1
< 1024; l1
++) {
1279 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1280 pde
= le32_to_cpu(pde
);
1281 if (pde
& PG_PRESENT_MASK
) {
1282 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1283 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1285 for(l2
= 0; l2
< 1024; l2
++) {
1286 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1287 (uint8_t *)&pte
, 4);
1288 pte
= le32_to_cpu(pte
);
1289 if (pte
& PG_PRESENT_MASK
) {
1290 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1300 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1301 uint32_t end
, int prot
)
1304 prot1
= *plast_prot
;
1305 if (prot
!= prot1
) {
1306 if (*pstart
!= -1) {
1307 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1308 *pstart
, end
, end
- *pstart
,
1309 prot1
& PG_USER_MASK
? 'u' : '-',
1311 prot1
& PG_RW_MASK
? 'w' : '-');
1321 static void mem_info(Monitor
*mon
)
1324 int l1
, l2
, prot
, last_prot
;
1325 uint32_t pgd
, pde
, pte
, start
, end
;
1327 env
= mon_get_cpu();
1331 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1332 monitor_printf(mon
, "PG disabled\n");
1335 pgd
= env
->cr
[3] & ~0xfff;
1338 for(l1
= 0; l1
< 1024; l1
++) {
1339 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1340 pde
= le32_to_cpu(pde
);
1342 if (pde
& PG_PRESENT_MASK
) {
1343 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1344 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1345 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1347 for(l2
= 0; l2
< 1024; l2
++) {
1348 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1349 (uint8_t *)&pte
, 4);
1350 pte
= le32_to_cpu(pte
);
1351 end
= (l1
<< 22) + (l2
<< 12);
1352 if (pte
& PG_PRESENT_MASK
) {
1353 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1357 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1362 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1368 #if defined(TARGET_SH4)
1370 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1372 monitor_printf(mon
, " tlb%i:\t"
1373 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1374 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1375 "dirty=%hhu writethrough=%hhu\n",
1377 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1378 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1382 static void tlb_info(Monitor
*mon
)
1384 CPUState
*env
= mon_get_cpu();
1387 monitor_printf (mon
, "ITLB:\n");
1388 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1389 print_tlb (mon
, i
, &env
->itlb
[i
]);
1390 monitor_printf (mon
, "UTLB:\n");
1391 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1392 print_tlb (mon
, i
, &env
->utlb
[i
]);
1397 static void do_info_kqemu(Monitor
*mon
)
1403 env
= mon_get_cpu();
1405 monitor_printf(mon
, "No cpu initialized yet");
1408 val
= env
->kqemu_enabled
;
1409 monitor_printf(mon
, "kqemu support: ");
1413 monitor_printf(mon
, "disabled\n");
1416 monitor_printf(mon
, "enabled for user code\n");
1419 monitor_printf(mon
, "enabled for user and kernel code\n");
1423 monitor_printf(mon
, "kqemu support: not compiled\n");
1427 static void do_info_kvm(Monitor
*mon
)
1429 #if defined(USE_KVM) || defined(CONFIG_KVM)
1430 monitor_printf(mon
, "kvm support: ");
1432 monitor_printf(mon
, "enabled\n");
1434 monitor_printf(mon
, "disabled\n");
1436 monitor_printf(mon
, "kvm support: not compiled\n");
1440 static void do_info_numa(Monitor
*mon
)
1445 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1446 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1447 monitor_printf(mon
, "node %d cpus:", i
);
1448 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1449 if (env
->numa_node
== i
) {
1450 monitor_printf(mon
, " %d", env
->cpu_index
);
1453 monitor_printf(mon
, "\n");
1454 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1459 #ifdef CONFIG_PROFILER
1463 int64_t kqemu_exec_count
;
1465 int64_t kqemu_ret_int_count
;
1466 int64_t kqemu_ret_excp_count
;
1467 int64_t kqemu_ret_intr_count
;
1469 static void do_info_profile(Monitor
*mon
)
1475 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1476 dev_time
, dev_time
/ (double)ticks_per_sec
);
1477 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1478 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1479 monitor_printf(mon
, "kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%"
1480 PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%"
1482 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1483 kqemu_time
/ (double)total
* 100.0,
1485 kqemu_ret_int_count
,
1486 kqemu_ret_excp_count
,
1487 kqemu_ret_intr_count
);
1490 kqemu_exec_count
= 0;
1492 kqemu_ret_int_count
= 0;
1493 kqemu_ret_excp_count
= 0;
1494 kqemu_ret_intr_count
= 0;
1496 kqemu_record_dump();
1500 static void do_info_profile(Monitor
*mon
)
1502 monitor_printf(mon
, "Internal profiler not compiled\n");
1506 /* Capture support */
1507 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1509 static void do_info_capture(Monitor
*mon
)
1514 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1515 monitor_printf(mon
, "[%d]: ", i
);
1516 s
->ops
.info (s
->opaque
);
1521 static void do_stop_capture(Monitor
*mon
, int n
)
1526 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1528 s
->ops
.destroy (s
->opaque
);
1529 LIST_REMOVE (s
, entries
);
1536 static void do_wav_capture(Monitor
*mon
, const char *path
,
1537 int has_freq
, int freq
,
1538 int has_bits
, int bits
,
1539 int has_channels
, int nchannels
)
1543 s
= qemu_mallocz (sizeof (*s
));
1545 freq
= has_freq
? freq
: 44100;
1546 bits
= has_bits
? bits
: 16;
1547 nchannels
= has_channels
? nchannels
: 2;
1549 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1550 monitor_printf(mon
, "Faied to add wave capture\n");
1553 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1557 #if defined(TARGET_I386)
1558 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1562 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1563 if (env
->cpu_index
== cpu_index
) {
1565 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1567 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1573 static void do_info_status(Monitor
*mon
)
1577 monitor_printf(mon
, "VM status: running (single step mode)\n");
1579 monitor_printf(mon
, "VM status: running\n");
1582 monitor_printf(mon
, "VM status: paused\n");
1586 static void do_balloon(Monitor
*mon
, int value
)
1588 ram_addr_t target
= value
;
1589 qemu_balloon(target
<< 20);
1592 static void do_info_balloon(Monitor
*mon
)
1596 actual
= qemu_balloon_status();
1597 if (kvm_enabled() && !kvm_has_sync_mmu())
1598 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1599 "ballooning disabled\n");
1600 else if (actual
== 0)
1601 monitor_printf(mon
, "Ballooning not activated in VM\n");
1603 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1606 static void do_acl(Monitor
*mon
,
1607 const char *command
,
1608 const char *aclname
,
1615 acl
= qemu_acl_find(aclname
);
1617 monitor_printf(mon
, "acl: unknown list '%s'\n", aclname
);
1621 if (strcmp(command
, "show") == 0) {
1623 qemu_acl_entry
*entry
;
1624 monitor_printf(mon
, "policy: %s\n",
1625 acl
->defaultDeny
? "deny" : "allow");
1626 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1628 monitor_printf(mon
, "%d: %s %s\n", i
,
1629 entry
->deny
? "deny" : "allow",
1632 } else if (strcmp(command
, "reset") == 0) {
1633 qemu_acl_reset(acl
);
1634 monitor_printf(mon
, "acl: removed all rules\n");
1635 } else if (strcmp(command
, "policy") == 0) {
1637 monitor_printf(mon
, "acl: missing policy parameter\n");
1641 if (strcmp(match
, "allow") == 0) {
1642 acl
->defaultDeny
= 0;
1643 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1644 } else if (strcmp(match
, "deny") == 0) {
1645 acl
->defaultDeny
= 1;
1646 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1648 monitor_printf(mon
, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match
);
1650 } else if ((strcmp(command
, "allow") == 0) ||
1651 (strcmp(command
, "deny") == 0)) {
1652 int deny
= strcmp(command
, "deny") == 0 ? 1 : 0;
1656 monitor_printf(mon
, "acl: missing match parameter\n");
1661 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1663 ret
= qemu_acl_append(acl
, deny
, match
);
1665 monitor_printf(mon
, "acl: unable to add acl entry\n");
1667 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1668 } else if (strcmp(command
, "remove") == 0) {
1672 monitor_printf(mon
, "acl: missing match parameter\n");
1676 ret
= qemu_acl_remove(acl
, match
);
1678 monitor_printf(mon
, "acl: no matching acl entry\n");
1680 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1682 monitor_printf(mon
, "acl: unknown command '%s'\n", command
);
1686 static const mon_cmd_t mon_cmds
[] = {
1687 #include "qemu-monitor.h"
1691 /* Please update qemu-monitor.hx when adding or changing commands */
1692 static const mon_cmd_t info_cmds
[] = {
1693 { "version", "", do_info_version
,
1694 "", "show the version of QEMU" },
1695 { "network", "", do_info_network
,
1696 "", "show the network state" },
1697 { "chardev", "", qemu_chr_info
,
1698 "", "show the character devices" },
1699 { "block", "", bdrv_info
,
1700 "", "show the block devices" },
1701 { "blockstats", "", bdrv_info_stats
,
1702 "", "show block device statistics" },
1703 { "registers", "", do_info_registers
,
1704 "", "show the cpu registers" },
1705 { "cpus", "", do_info_cpus
,
1706 "", "show infos for each CPU" },
1707 { "history", "", do_info_history
,
1708 "", "show the command line history", },
1709 { "irq", "", irq_info
,
1710 "", "show the interrupts statistics (if available)", },
1711 { "pic", "", pic_info
,
1712 "", "show i8259 (PIC) state", },
1713 { "pci", "", pci_info
,
1714 "", "show PCI info", },
1715 #if defined(TARGET_I386) || defined(TARGET_SH4)
1716 { "tlb", "", tlb_info
,
1717 "", "show virtual to physical memory mappings", },
1719 #if defined(TARGET_I386)
1720 { "mem", "", mem_info
,
1721 "", "show the active virtual memory mappings", },
1722 { "hpet", "", do_info_hpet
,
1723 "", "show state of HPET", },
1725 { "jit", "", do_info_jit
,
1726 "", "show dynamic compiler info", },
1727 { "kqemu", "", do_info_kqemu
,
1728 "", "show KQEMU information", },
1729 { "kvm", "", do_info_kvm
,
1730 "", "show KVM information", },
1731 { "numa", "", do_info_numa
,
1732 "", "show NUMA information", },
1733 { "usb", "", usb_info
,
1734 "", "show guest USB devices", },
1735 { "usbhost", "", usb_host_info
,
1736 "", "show host USB devices", },
1737 { "profile", "", do_info_profile
,
1738 "", "show profiling information", },
1739 { "capture", "", do_info_capture
,
1740 "", "show capture information" },
1741 { "snapshots", "", do_info_snapshots
,
1742 "", "show the currently saved VM snapshots" },
1743 { "status", "", do_info_status
,
1744 "", "show the current VM status (running|paused)" },
1745 { "pcmcia", "", pcmcia_info
,
1746 "", "show guest PCMCIA status" },
1747 { "mice", "", do_info_mice
,
1748 "", "show which guest mouse is receiving events" },
1749 { "vnc", "", do_info_vnc
,
1750 "", "show the vnc server status"},
1751 { "name", "", do_info_name
,
1752 "", "show the current VM name" },
1753 { "uuid", "", do_info_uuid
,
1754 "", "show the current VM UUID" },
1755 #if defined(TARGET_PPC)
1756 { "cpustats", "", do_info_cpu_stats
,
1757 "", "show CPU statistics", },
1759 #if defined(CONFIG_SLIRP)
1760 { "usernet", "", do_info_usernet
,
1761 "", "show user network stack connection states", },
1763 { "migrate", "", do_info_migrate
, "", "show migration status" },
1764 { "balloon", "", do_info_balloon
,
1765 "", "show balloon information" },
1766 { "qtree", "", do_info_qtree
,
1767 "", "show device tree" },
1771 /*******************************************************************/
1773 static const char *pch
;
1774 static jmp_buf expr_env
;
1779 typedef struct MonitorDef
{
1782 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1786 #if defined(TARGET_I386)
1787 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1789 CPUState
*env
= mon_get_cpu();
1792 return env
->eip
+ env
->segs
[R_CS
].base
;
1796 #if defined(TARGET_PPC)
1797 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1799 CPUState
*env
= mon_get_cpu();
1807 for (i
= 0; i
< 8; i
++)
1808 u
|= env
->crf
[i
] << (32 - (4 * i
));
1813 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1815 CPUState
*env
= mon_get_cpu();
1821 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1823 CPUState
*env
= mon_get_cpu();
1829 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1831 CPUState
*env
= mon_get_cpu();
1834 return cpu_ppc_load_decr(env
);
1837 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1839 CPUState
*env
= mon_get_cpu();
1842 return cpu_ppc_load_tbu(env
);
1845 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1847 CPUState
*env
= mon_get_cpu();
1850 return cpu_ppc_load_tbl(env
);
1854 #if defined(TARGET_SPARC)
1855 #ifndef TARGET_SPARC64
1856 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1858 CPUState
*env
= mon_get_cpu();
1861 return GET_PSR(env
);
1865 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1867 CPUState
*env
= mon_get_cpu();
1870 return env
->regwptr
[val
];
1874 static const MonitorDef monitor_defs
[] = {
1877 #define SEG(name, seg) \
1878 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1879 { name ".base", offsetof(CPUState, segs[seg].base) },\
1880 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1882 { "eax", offsetof(CPUState
, regs
[0]) },
1883 { "ecx", offsetof(CPUState
, regs
[1]) },
1884 { "edx", offsetof(CPUState
, regs
[2]) },
1885 { "ebx", offsetof(CPUState
, regs
[3]) },
1886 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1887 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1888 { "esi", offsetof(CPUState
, regs
[6]) },
1889 { "edi", offsetof(CPUState
, regs
[7]) },
1890 #ifdef TARGET_X86_64
1891 { "r8", offsetof(CPUState
, regs
[8]) },
1892 { "r9", offsetof(CPUState
, regs
[9]) },
1893 { "r10", offsetof(CPUState
, regs
[10]) },
1894 { "r11", offsetof(CPUState
, regs
[11]) },
1895 { "r12", offsetof(CPUState
, regs
[12]) },
1896 { "r13", offsetof(CPUState
, regs
[13]) },
1897 { "r14", offsetof(CPUState
, regs
[14]) },
1898 { "r15", offsetof(CPUState
, regs
[15]) },
1900 { "eflags", offsetof(CPUState
, eflags
) },
1901 { "eip", offsetof(CPUState
, eip
) },
1908 { "pc", 0, monitor_get_pc
, },
1909 #elif defined(TARGET_PPC)
1910 /* General purpose registers */
1911 { "r0", offsetof(CPUState
, gpr
[0]) },
1912 { "r1", offsetof(CPUState
, gpr
[1]) },
1913 { "r2", offsetof(CPUState
, gpr
[2]) },
1914 { "r3", offsetof(CPUState
, gpr
[3]) },
1915 { "r4", offsetof(CPUState
, gpr
[4]) },
1916 { "r5", offsetof(CPUState
, gpr
[5]) },
1917 { "r6", offsetof(CPUState
, gpr
[6]) },
1918 { "r7", offsetof(CPUState
, gpr
[7]) },
1919 { "r8", offsetof(CPUState
, gpr
[8]) },
1920 { "r9", offsetof(CPUState
, gpr
[9]) },
1921 { "r10", offsetof(CPUState
, gpr
[10]) },
1922 { "r11", offsetof(CPUState
, gpr
[11]) },
1923 { "r12", offsetof(CPUState
, gpr
[12]) },
1924 { "r13", offsetof(CPUState
, gpr
[13]) },
1925 { "r14", offsetof(CPUState
, gpr
[14]) },
1926 { "r15", offsetof(CPUState
, gpr
[15]) },
1927 { "r16", offsetof(CPUState
, gpr
[16]) },
1928 { "r17", offsetof(CPUState
, gpr
[17]) },
1929 { "r18", offsetof(CPUState
, gpr
[18]) },
1930 { "r19", offsetof(CPUState
, gpr
[19]) },
1931 { "r20", offsetof(CPUState
, gpr
[20]) },
1932 { "r21", offsetof(CPUState
, gpr
[21]) },
1933 { "r22", offsetof(CPUState
, gpr
[22]) },
1934 { "r23", offsetof(CPUState
, gpr
[23]) },
1935 { "r24", offsetof(CPUState
, gpr
[24]) },
1936 { "r25", offsetof(CPUState
, gpr
[25]) },
1937 { "r26", offsetof(CPUState
, gpr
[26]) },
1938 { "r27", offsetof(CPUState
, gpr
[27]) },
1939 { "r28", offsetof(CPUState
, gpr
[28]) },
1940 { "r29", offsetof(CPUState
, gpr
[29]) },
1941 { "r30", offsetof(CPUState
, gpr
[30]) },
1942 { "r31", offsetof(CPUState
, gpr
[31]) },
1943 /* Floating point registers */
1944 { "f0", offsetof(CPUState
, fpr
[0]) },
1945 { "f1", offsetof(CPUState
, fpr
[1]) },
1946 { "f2", offsetof(CPUState
, fpr
[2]) },
1947 { "f3", offsetof(CPUState
, fpr
[3]) },
1948 { "f4", offsetof(CPUState
, fpr
[4]) },
1949 { "f5", offsetof(CPUState
, fpr
[5]) },
1950 { "f6", offsetof(CPUState
, fpr
[6]) },
1951 { "f7", offsetof(CPUState
, fpr
[7]) },
1952 { "f8", offsetof(CPUState
, fpr
[8]) },
1953 { "f9", offsetof(CPUState
, fpr
[9]) },
1954 { "f10", offsetof(CPUState
, fpr
[10]) },
1955 { "f11", offsetof(CPUState
, fpr
[11]) },
1956 { "f12", offsetof(CPUState
, fpr
[12]) },
1957 { "f13", offsetof(CPUState
, fpr
[13]) },
1958 { "f14", offsetof(CPUState
, fpr
[14]) },
1959 { "f15", offsetof(CPUState
, fpr
[15]) },
1960 { "f16", offsetof(CPUState
, fpr
[16]) },
1961 { "f17", offsetof(CPUState
, fpr
[17]) },
1962 { "f18", offsetof(CPUState
, fpr
[18]) },
1963 { "f19", offsetof(CPUState
, fpr
[19]) },
1964 { "f20", offsetof(CPUState
, fpr
[20]) },
1965 { "f21", offsetof(CPUState
, fpr
[21]) },
1966 { "f22", offsetof(CPUState
, fpr
[22]) },
1967 { "f23", offsetof(CPUState
, fpr
[23]) },
1968 { "f24", offsetof(CPUState
, fpr
[24]) },
1969 { "f25", offsetof(CPUState
, fpr
[25]) },
1970 { "f26", offsetof(CPUState
, fpr
[26]) },
1971 { "f27", offsetof(CPUState
, fpr
[27]) },
1972 { "f28", offsetof(CPUState
, fpr
[28]) },
1973 { "f29", offsetof(CPUState
, fpr
[29]) },
1974 { "f30", offsetof(CPUState
, fpr
[30]) },
1975 { "f31", offsetof(CPUState
, fpr
[31]) },
1976 { "fpscr", offsetof(CPUState
, fpscr
) },
1977 /* Next instruction pointer */
1978 { "nip|pc", offsetof(CPUState
, nip
) },
1979 { "lr", offsetof(CPUState
, lr
) },
1980 { "ctr", offsetof(CPUState
, ctr
) },
1981 { "decr", 0, &monitor_get_decr
, },
1982 { "ccr", 0, &monitor_get_ccr
, },
1983 /* Machine state register */
1984 { "msr", 0, &monitor_get_msr
, },
1985 { "xer", 0, &monitor_get_xer
, },
1986 { "tbu", 0, &monitor_get_tbu
, },
1987 { "tbl", 0, &monitor_get_tbl
, },
1988 #if defined(TARGET_PPC64)
1989 /* Address space register */
1990 { "asr", offsetof(CPUState
, asr
) },
1992 /* Segment registers */
1993 { "sdr1", offsetof(CPUState
, sdr1
) },
1994 { "sr0", offsetof(CPUState
, sr
[0]) },
1995 { "sr1", offsetof(CPUState
, sr
[1]) },
1996 { "sr2", offsetof(CPUState
, sr
[2]) },
1997 { "sr3", offsetof(CPUState
, sr
[3]) },
1998 { "sr4", offsetof(CPUState
, sr
[4]) },
1999 { "sr5", offsetof(CPUState
, sr
[5]) },
2000 { "sr6", offsetof(CPUState
, sr
[6]) },
2001 { "sr7", offsetof(CPUState
, sr
[7]) },
2002 { "sr8", offsetof(CPUState
, sr
[8]) },
2003 { "sr9", offsetof(CPUState
, sr
[9]) },
2004 { "sr10", offsetof(CPUState
, sr
[10]) },
2005 { "sr11", offsetof(CPUState
, sr
[11]) },
2006 { "sr12", offsetof(CPUState
, sr
[12]) },
2007 { "sr13", offsetof(CPUState
, sr
[13]) },
2008 { "sr14", offsetof(CPUState
, sr
[14]) },
2009 { "sr15", offsetof(CPUState
, sr
[15]) },
2010 /* Too lazy to put BATs and SPRs ... */
2011 #elif defined(TARGET_SPARC)
2012 { "g0", offsetof(CPUState
, gregs
[0]) },
2013 { "g1", offsetof(CPUState
, gregs
[1]) },
2014 { "g2", offsetof(CPUState
, gregs
[2]) },
2015 { "g3", offsetof(CPUState
, gregs
[3]) },
2016 { "g4", offsetof(CPUState
, gregs
[4]) },
2017 { "g5", offsetof(CPUState
, gregs
[5]) },
2018 { "g6", offsetof(CPUState
, gregs
[6]) },
2019 { "g7", offsetof(CPUState
, gregs
[7]) },
2020 { "o0", 0, monitor_get_reg
},
2021 { "o1", 1, monitor_get_reg
},
2022 { "o2", 2, monitor_get_reg
},
2023 { "o3", 3, monitor_get_reg
},
2024 { "o4", 4, monitor_get_reg
},
2025 { "o5", 5, monitor_get_reg
},
2026 { "o6", 6, monitor_get_reg
},
2027 { "o7", 7, monitor_get_reg
},
2028 { "l0", 8, monitor_get_reg
},
2029 { "l1", 9, monitor_get_reg
},
2030 { "l2", 10, monitor_get_reg
},
2031 { "l3", 11, monitor_get_reg
},
2032 { "l4", 12, monitor_get_reg
},
2033 { "l5", 13, monitor_get_reg
},
2034 { "l6", 14, monitor_get_reg
},
2035 { "l7", 15, monitor_get_reg
},
2036 { "i0", 16, monitor_get_reg
},
2037 { "i1", 17, monitor_get_reg
},
2038 { "i2", 18, monitor_get_reg
},
2039 { "i3", 19, monitor_get_reg
},
2040 { "i4", 20, monitor_get_reg
},
2041 { "i5", 21, monitor_get_reg
},
2042 { "i6", 22, monitor_get_reg
},
2043 { "i7", 23, monitor_get_reg
},
2044 { "pc", offsetof(CPUState
, pc
) },
2045 { "npc", offsetof(CPUState
, npc
) },
2046 { "y", offsetof(CPUState
, y
) },
2047 #ifndef TARGET_SPARC64
2048 { "psr", 0, &monitor_get_psr
, },
2049 { "wim", offsetof(CPUState
, wim
) },
2051 { "tbr", offsetof(CPUState
, tbr
) },
2052 { "fsr", offsetof(CPUState
, fsr
) },
2053 { "f0", offsetof(CPUState
, fpr
[0]) },
2054 { "f1", offsetof(CPUState
, fpr
[1]) },
2055 { "f2", offsetof(CPUState
, fpr
[2]) },
2056 { "f3", offsetof(CPUState
, fpr
[3]) },
2057 { "f4", offsetof(CPUState
, fpr
[4]) },
2058 { "f5", offsetof(CPUState
, fpr
[5]) },
2059 { "f6", offsetof(CPUState
, fpr
[6]) },
2060 { "f7", offsetof(CPUState
, fpr
[7]) },
2061 { "f8", offsetof(CPUState
, fpr
[8]) },
2062 { "f9", offsetof(CPUState
, fpr
[9]) },
2063 { "f10", offsetof(CPUState
, fpr
[10]) },
2064 { "f11", offsetof(CPUState
, fpr
[11]) },
2065 { "f12", offsetof(CPUState
, fpr
[12]) },
2066 { "f13", offsetof(CPUState
, fpr
[13]) },
2067 { "f14", offsetof(CPUState
, fpr
[14]) },
2068 { "f15", offsetof(CPUState
, fpr
[15]) },
2069 { "f16", offsetof(CPUState
, fpr
[16]) },
2070 { "f17", offsetof(CPUState
, fpr
[17]) },
2071 { "f18", offsetof(CPUState
, fpr
[18]) },
2072 { "f19", offsetof(CPUState
, fpr
[19]) },
2073 { "f20", offsetof(CPUState
, fpr
[20]) },
2074 { "f21", offsetof(CPUState
, fpr
[21]) },
2075 { "f22", offsetof(CPUState
, fpr
[22]) },
2076 { "f23", offsetof(CPUState
, fpr
[23]) },
2077 { "f24", offsetof(CPUState
, fpr
[24]) },
2078 { "f25", offsetof(CPUState
, fpr
[25]) },
2079 { "f26", offsetof(CPUState
, fpr
[26]) },
2080 { "f27", offsetof(CPUState
, fpr
[27]) },
2081 { "f28", offsetof(CPUState
, fpr
[28]) },
2082 { "f29", offsetof(CPUState
, fpr
[29]) },
2083 { "f30", offsetof(CPUState
, fpr
[30]) },
2084 { "f31", offsetof(CPUState
, fpr
[31]) },
2085 #ifdef TARGET_SPARC64
2086 { "f32", offsetof(CPUState
, fpr
[32]) },
2087 { "f34", offsetof(CPUState
, fpr
[34]) },
2088 { "f36", offsetof(CPUState
, fpr
[36]) },
2089 { "f38", offsetof(CPUState
, fpr
[38]) },
2090 { "f40", offsetof(CPUState
, fpr
[40]) },
2091 { "f42", offsetof(CPUState
, fpr
[42]) },
2092 { "f44", offsetof(CPUState
, fpr
[44]) },
2093 { "f46", offsetof(CPUState
, fpr
[46]) },
2094 { "f48", offsetof(CPUState
, fpr
[48]) },
2095 { "f50", offsetof(CPUState
, fpr
[50]) },
2096 { "f52", offsetof(CPUState
, fpr
[52]) },
2097 { "f54", offsetof(CPUState
, fpr
[54]) },
2098 { "f56", offsetof(CPUState
, fpr
[56]) },
2099 { "f58", offsetof(CPUState
, fpr
[58]) },
2100 { "f60", offsetof(CPUState
, fpr
[60]) },
2101 { "f62", offsetof(CPUState
, fpr
[62]) },
2102 { "asi", offsetof(CPUState
, asi
) },
2103 { "pstate", offsetof(CPUState
, pstate
) },
2104 { "cansave", offsetof(CPUState
, cansave
) },
2105 { "canrestore", offsetof(CPUState
, canrestore
) },
2106 { "otherwin", offsetof(CPUState
, otherwin
) },
2107 { "wstate", offsetof(CPUState
, wstate
) },
2108 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2109 { "fprs", offsetof(CPUState
, fprs
) },
2115 static void expr_error(Monitor
*mon
, const char *msg
)
2117 monitor_printf(mon
, "%s\n", msg
);
2118 longjmp(expr_env
, 1);
2121 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2122 static int get_monitor_def(target_long
*pval
, const char *name
)
2124 const MonitorDef
*md
;
2127 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2128 if (compare_cmd(name
, md
->name
)) {
2129 if (md
->get_value
) {
2130 *pval
= md
->get_value(md
, md
->offset
);
2132 CPUState
*env
= mon_get_cpu();
2135 ptr
= (uint8_t *)env
+ md
->offset
;
2138 *pval
= *(int32_t *)ptr
;
2141 *pval
= *(target_long
*)ptr
;
2154 static void next(void)
2158 while (qemu_isspace(*pch
))
2163 static int64_t expr_sum(Monitor
*mon
);
2165 static int64_t expr_unary(Monitor
*mon
)
2174 n
= expr_unary(mon
);
2178 n
= -expr_unary(mon
);
2182 n
= ~expr_unary(mon
);
2188 expr_error(mon
, "')' expected");
2195 expr_error(mon
, "character constant expected");
2199 expr_error(mon
, "missing terminating \' character");
2209 while ((*pch
>= 'a' && *pch
<= 'z') ||
2210 (*pch
>= 'A' && *pch
<= 'Z') ||
2211 (*pch
>= '0' && *pch
<= '9') ||
2212 *pch
== '_' || *pch
== '.') {
2213 if ((q
- buf
) < sizeof(buf
) - 1)
2217 while (qemu_isspace(*pch
))
2220 ret
= get_monitor_def(®
, buf
);
2222 expr_error(mon
, "unknown register");
2224 expr_error(mon
, "no cpu defined");
2229 expr_error(mon
, "unexpected end of expression");
2233 #if TARGET_PHYS_ADDR_BITS > 32
2234 n
= strtoull(pch
, &p
, 0);
2236 n
= strtoul(pch
, &p
, 0);
2239 expr_error(mon
, "invalid char in expression");
2242 while (qemu_isspace(*pch
))
2250 static int64_t expr_prod(Monitor
*mon
)
2255 val
= expr_unary(mon
);
2258 if (op
!= '*' && op
!= '/' && op
!= '%')
2261 val2
= expr_unary(mon
);
2270 expr_error(mon
, "division by zero");
2281 static int64_t expr_logic(Monitor
*mon
)
2286 val
= expr_prod(mon
);
2289 if (op
!= '&' && op
!= '|' && op
!= '^')
2292 val2
= expr_prod(mon
);
2309 static int64_t expr_sum(Monitor
*mon
)
2314 val
= expr_logic(mon
);
2317 if (op
!= '+' && op
!= '-')
2320 val2
= expr_logic(mon
);
2329 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2332 if (setjmp(expr_env
)) {
2336 while (qemu_isspace(*pch
))
2338 *pval
= expr_sum(mon
);
2343 static int get_str(char *buf
, int buf_size
, const char **pp
)
2351 while (qemu_isspace(*p
))
2361 while (*p
!= '\0' && *p
!= '\"') {
2377 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2380 if ((q
- buf
) < buf_size
- 1) {
2384 if ((q
- buf
) < buf_size
- 1) {
2391 qemu_printf("unterminated string\n");
2396 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2397 if ((q
- buf
) < buf_size
- 1) {
2409 * Store the command-name in cmdname, and return a pointer to
2410 * the remaining of the command string.
2412 static const char *get_command_name(const char *cmdline
,
2413 char *cmdname
, size_t nlen
)
2416 const char *p
, *pstart
;
2419 while (qemu_isspace(*p
))
2424 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2429 memcpy(cmdname
, pstart
, len
);
2430 cmdname
[len
] = '\0';
2434 static int default_fmt_format
= 'x';
2435 static int default_fmt_size
= 4;
2439 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2441 const char *p
, *typestr
;
2442 int c
, nb_args
, i
, has_arg
;
2443 const mon_cmd_t
*cmd
;
2446 void *str_allocated
[MAX_ARGS
];
2447 void *args
[MAX_ARGS
];
2448 void (*handler_0
)(Monitor
*mon
);
2449 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2450 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2451 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2452 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2454 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2455 void *arg3
, void *arg4
);
2456 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2457 void *arg3
, void *arg4
, void *arg5
);
2458 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2459 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2462 monitor_printf(mon
, "command='%s'\n", cmdline
);
2465 /* extract the command name */
2466 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2470 /* find the command */
2471 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2472 if (compare_cmd(cmdname
, cmd
->name
))
2476 if (cmd
->name
== NULL
) {
2477 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2481 for(i
= 0; i
< MAX_ARGS
; i
++)
2482 str_allocated
[i
] = NULL
;
2484 /* parse the parameters */
2485 typestr
= cmd
->args_type
;
2500 while (qemu_isspace(*p
))
2502 if (*typestr
== '?') {
2505 /* no optional string: NULL argument */
2510 ret
= get_str(buf
, sizeof(buf
), &p
);
2514 monitor_printf(mon
, "%s: filename expected\n",
2518 monitor_printf(mon
, "%s: block device name expected\n",
2522 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2527 str
= qemu_malloc(strlen(buf
) + 1);
2528 pstrcpy(str
, sizeof(buf
), buf
);
2529 str_allocated
[nb_args
] = str
;
2531 if (nb_args
>= MAX_ARGS
) {
2533 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2536 args
[nb_args
++] = str
;
2541 int count
, format
, size
;
2543 while (qemu_isspace(*p
))
2549 if (qemu_isdigit(*p
)) {
2551 while (qemu_isdigit(*p
)) {
2552 count
= count
* 10 + (*p
- '0');
2590 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2591 monitor_printf(mon
, "invalid char in format: '%c'\n",
2596 format
= default_fmt_format
;
2597 if (format
!= 'i') {
2598 /* for 'i', not specifying a size gives -1 as size */
2600 size
= default_fmt_size
;
2601 default_fmt_size
= size
;
2603 default_fmt_format
= format
;
2606 format
= default_fmt_format
;
2607 if (format
!= 'i') {
2608 size
= default_fmt_size
;
2613 if (nb_args
+ 3 > MAX_ARGS
)
2615 args
[nb_args
++] = (void*)(long)count
;
2616 args
[nb_args
++] = (void*)(long)format
;
2617 args
[nb_args
++] = (void*)(long)size
;
2625 while (qemu_isspace(*p
))
2627 if (*typestr
== '?' || *typestr
== '.') {
2628 if (*typestr
== '?') {
2636 while (qemu_isspace(*p
))
2644 if (nb_args
>= MAX_ARGS
)
2646 args
[nb_args
++] = (void *)(long)has_arg
;
2648 if (nb_args
>= MAX_ARGS
)
2654 if (get_expr(mon
, &val
, &p
))
2658 if (nb_args
>= MAX_ARGS
)
2660 args
[nb_args
++] = (void *)(long)val
;
2662 if ((nb_args
+ 1) >= MAX_ARGS
)
2664 #if TARGET_PHYS_ADDR_BITS > 32
2665 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2667 args
[nb_args
++] = (void *)0;
2669 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2681 while (qemu_isspace(*p
))
2687 monitor_printf(mon
, "%s: unsupported option -%c\n",
2694 if (nb_args
>= MAX_ARGS
)
2696 args
[nb_args
++] = (void *)(long)has_option
;
2701 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2705 /* check that all arguments were parsed */
2706 while (qemu_isspace(*p
))
2709 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2716 handler_0
= cmd
->handler
;
2720 handler_1
= cmd
->handler
;
2721 handler_1(mon
, args
[0]);
2724 handler_2
= cmd
->handler
;
2725 handler_2(mon
, args
[0], args
[1]);
2728 handler_3
= cmd
->handler
;
2729 handler_3(mon
, args
[0], args
[1], args
[2]);
2732 handler_4
= cmd
->handler
;
2733 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2736 handler_5
= cmd
->handler
;
2737 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2740 handler_6
= cmd
->handler
;
2741 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2744 handler_7
= cmd
->handler
;
2745 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2749 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2753 for(i
= 0; i
< MAX_ARGS
; i
++)
2754 qemu_free(str_allocated
[i
]);
2757 static void cmd_completion(const char *name
, const char *list
)
2759 const char *p
, *pstart
;
2768 p
= pstart
+ strlen(pstart
);
2770 if (len
> sizeof(cmd
) - 2)
2771 len
= sizeof(cmd
) - 2;
2772 memcpy(cmd
, pstart
, len
);
2774 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2775 readline_add_completion(cur_mon
->rs
, cmd
);
2783 static void file_completion(const char *input
)
2788 char file
[1024], file_prefix
[1024];
2792 p
= strrchr(input
, '/');
2795 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2796 pstrcpy(path
, sizeof(path
), ".");
2798 input_path_len
= p
- input
+ 1;
2799 memcpy(path
, input
, input_path_len
);
2800 if (input_path_len
> sizeof(path
) - 1)
2801 input_path_len
= sizeof(path
) - 1;
2802 path
[input_path_len
] = '\0';
2803 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2805 #ifdef DEBUG_COMPLETION
2806 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2807 input
, path
, file_prefix
);
2809 ffs
= opendir(path
);
2817 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2818 memcpy(file
, input
, input_path_len
);
2819 if (input_path_len
< sizeof(file
))
2820 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2822 /* stat the file to find out if it's a directory.
2823 * In that case add a slash to speed up typing long paths
2826 if(S_ISDIR(sb
.st_mode
))
2827 pstrcat(file
, sizeof(file
), "/");
2828 readline_add_completion(cur_mon
->rs
, file
);
2834 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2836 const char *name
= bdrv_get_device_name(bs
);
2837 const char *input
= opaque
;
2839 if (input
[0] == '\0' ||
2840 !strncmp(name
, (char *)input
, strlen(input
))) {
2841 readline_add_completion(cur_mon
->rs
, name
);
2845 /* NOTE: this parser is an approximate form of the real command parser */
2846 static void parse_cmdline(const char *cmdline
,
2847 int *pnb_args
, char **args
)
2856 while (qemu_isspace(*p
))
2860 if (nb_args
>= MAX_ARGS
)
2862 ret
= get_str(buf
, sizeof(buf
), &p
);
2863 args
[nb_args
] = qemu_strdup(buf
);
2868 *pnb_args
= nb_args
;
2871 static void monitor_find_completion(const char *cmdline
)
2873 const char *cmdname
;
2874 char *args
[MAX_ARGS
];
2875 int nb_args
, i
, len
;
2876 const char *ptype
, *str
;
2877 const mon_cmd_t
*cmd
;
2880 parse_cmdline(cmdline
, &nb_args
, args
);
2881 #ifdef DEBUG_COMPLETION
2882 for(i
= 0; i
< nb_args
; i
++) {
2883 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2887 /* if the line ends with a space, it means we want to complete the
2889 len
= strlen(cmdline
);
2890 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2891 if (nb_args
>= MAX_ARGS
)
2893 args
[nb_args
++] = qemu_strdup("");
2896 /* command completion */
2901 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
2902 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2903 cmd_completion(cmdname
, cmd
->name
);
2906 /* find the command */
2907 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2908 if (compare_cmd(args
[0], cmd
->name
))
2913 ptype
= cmd
->args_type
;
2914 for(i
= 0; i
< nb_args
- 2; i
++) {
2915 if (*ptype
!= '\0') {
2917 while (*ptype
== '?')
2921 str
= args
[nb_args
- 1];
2924 /* file completion */
2925 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
2926 file_completion(str
);
2929 /* block device name completion */
2930 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
2931 bdrv_iterate(block_completion_it
, (void *)str
);
2934 /* XXX: more generic ? */
2935 if (!strcmp(cmd
->name
, "info")) {
2936 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
2937 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
2938 cmd_completion(str
, cmd
->name
);
2940 } else if (!strcmp(cmd
->name
, "sendkey")) {
2941 char *sep
= strrchr(str
, '-');
2944 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
2945 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
2946 cmd_completion(str
, key
->name
);
2954 for(i
= 0; i
< nb_args
; i
++)
2958 static int monitor_can_read(void *opaque
)
2960 Monitor
*mon
= opaque
;
2962 return (mon
->suspend_cnt
== 0) ? 128 : 0;
2965 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
2967 Monitor
*old_mon
= cur_mon
;
2973 for (i
= 0; i
< size
; i
++)
2974 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
2976 if (size
== 0 || buf
[size
- 1] != 0)
2977 monitor_printf(cur_mon
, "corrupted command\n");
2979 monitor_handle_command(cur_mon
, (char *)buf
);
2985 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
2987 monitor_suspend(mon
);
2988 monitor_handle_command(mon
, cmdline
);
2989 monitor_resume(mon
);
2992 int monitor_suspend(Monitor
*mon
)
3000 void monitor_resume(Monitor
*mon
)
3004 if (--mon
->suspend_cnt
== 0)
3005 readline_show_prompt(mon
->rs
);
3008 static void monitor_event(void *opaque
, int event
)
3010 Monitor
*mon
= opaque
;
3013 case CHR_EVENT_MUX_IN
:
3014 readline_restart(mon
->rs
);
3015 monitor_resume(mon
);
3019 case CHR_EVENT_MUX_OUT
:
3020 if (mon
->suspend_cnt
== 0)
3021 monitor_printf(mon
, "\n");
3023 monitor_suspend(mon
);
3026 case CHR_EVENT_RESET
:
3027 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3028 "information\n", QEMU_VERSION
);
3029 if (mon
->chr
->focus
== 0)
3030 readline_show_prompt(mon
->rs
);
3044 void monitor_init(CharDriverState
*chr
, int flags
)
3046 static int is_first_init
= 1;
3049 if (is_first_init
) {
3050 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3054 mon
= qemu_mallocz(sizeof(*mon
));
3058 if (mon
->chr
->focus
!= 0)
3059 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3060 if (flags
& MONITOR_USE_READLINE
) {
3061 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3062 monitor_read_command(mon
, 0);
3065 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3068 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3069 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3073 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3075 BlockDriverState
*bs
= opaque
;
3078 if (bdrv_set_key(bs
, password
) != 0) {
3079 monitor_printf(mon
, "invalid password\n");
3082 if (mon
->password_completion_cb
)
3083 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3085 monitor_read_command(mon
, 1);
3088 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3089 BlockDriverCompletionFunc
*completion_cb
,
3094 if (!bdrv_key_required(bs
)) {
3096 completion_cb(opaque
, 0);
3100 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3101 bdrv_get_encrypted_filename(bs
));
3103 mon
->password_completion_cb
= completion_cb
;
3104 mon
->password_opaque
= opaque
;
3106 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3108 if (err
&& completion_cb
)
3109 completion_cb(opaque
, err
);