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"
49 //#define DEBUG_COMPLETION
55 * 'B' block device name
56 * 's' string (accept optional quote)
58 * 'l' target long (32 or 64 bit)
59 * '/' optional gdb-like print format (like "/10x")
61 * '?' optional type (for 'F', 's' and 'i')
65 typedef struct mon_cmd_t
{
67 const char *args_type
;
73 /* file descriptors passed via SCM_RIGHTS */
74 typedef struct mon_fd_t mon_fd_t
;
78 LIST_ENTRY(mon_fd_t
) next
;
89 BlockDriverCompletionFunc
*password_completion_cb
;
90 void *password_opaque
;
91 LIST_HEAD(,mon_fd_t
) fds
;
92 LIST_ENTRY(Monitor
) entry
;
95 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
97 static const mon_cmd_t mon_cmds
[];
98 static const mon_cmd_t info_cmds
[];
100 Monitor
*cur_mon
= NULL
;
102 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
105 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
107 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
109 readline_show_prompt(mon
->rs
);
112 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
116 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
117 /* prompt is printed on return from the command handler */
120 monitor_printf(mon
, "terminal does not support password prompting\n");
125 void monitor_flush(Monitor
*mon
)
127 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
128 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
129 mon
->outbuf_index
= 0;
133 /* flush at every end of line or if the buffer is full */
134 static void monitor_puts(Monitor
*mon
, const char *str
)
146 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
147 mon
->outbuf
[mon
->outbuf_index
++] = c
;
148 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
154 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
157 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
158 monitor_puts(mon
, buf
);
161 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
165 monitor_vprintf(mon
, fmt
, ap
);
169 void monitor_print_filename(Monitor
*mon
, const char *filename
)
173 for (i
= 0; filename
[i
]; i
++) {
174 switch (filename
[i
]) {
178 monitor_printf(mon
, "\\%c", filename
[i
]);
181 monitor_printf(mon
, "\\t");
184 monitor_printf(mon
, "\\r");
187 monitor_printf(mon
, "\\n");
190 monitor_printf(mon
, "%c", filename
[i
]);
196 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
200 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
205 static int compare_cmd(const char *name
, const char *list
)
207 const char *p
, *pstart
;
215 p
= pstart
+ strlen(pstart
);
216 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
225 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
226 const char *prefix
, const char *name
)
228 const mon_cmd_t
*cmd
;
230 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
231 if (!name
|| !strcmp(name
, cmd
->name
))
232 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
233 cmd
->params
, cmd
->help
);
237 static void help_cmd(Monitor
*mon
, const char *name
)
239 if (name
&& !strcmp(name
, "info")) {
240 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
242 help_cmd_dump(mon
, mon_cmds
, "", name
);
243 if (name
&& !strcmp(name
, "log")) {
244 const CPULogItem
*item
;
245 monitor_printf(mon
, "Log items (comma separated):\n");
246 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
247 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
248 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
254 static void do_commit(Monitor
*mon
, const char *device
)
259 all_devices
= !strcmp(device
, "all");
260 TAILQ_FOREACH(dinfo
, &drives
, next
) {
262 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
264 bdrv_commit(dinfo
->bdrv
);
268 static void do_info(Monitor
*mon
, const char *item
)
270 const mon_cmd_t
*cmd
;
271 void (*handler
)(Monitor
*);
275 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
276 if (compare_cmd(item
, cmd
->name
))
280 help_cmd(mon
, "info");
283 handler
= cmd
->handler
;
287 static void do_info_version(Monitor
*mon
)
289 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
292 static void do_info_name(Monitor
*mon
)
295 monitor_printf(mon
, "%s\n", qemu_name
);
298 #if defined(TARGET_I386)
299 static void do_info_hpet(Monitor
*mon
)
301 monitor_printf(mon
, "HPET is %s by QEMU\n",
302 (no_hpet
) ? "disabled" : "enabled");
306 static void do_info_uuid(Monitor
*mon
)
308 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
309 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
310 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
311 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
312 qemu_uuid
[14], qemu_uuid
[15]);
315 /* get the current CPU defined by the user */
316 static int mon_set_cpu(int cpu_index
)
320 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
321 if (env
->cpu_index
== cpu_index
) {
322 cur_mon
->mon_cpu
= env
;
329 static CPUState
*mon_get_cpu(void)
331 if (!cur_mon
->mon_cpu
) {
334 cpu_synchronize_state(cur_mon
->mon_cpu
, 0);
335 return cur_mon
->mon_cpu
;
338 static void do_info_registers(Monitor
*mon
)
345 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
348 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
353 static void do_info_cpus(Monitor
*mon
)
357 /* just to set the default cpu if not already done */
360 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
361 cpu_synchronize_state(env
, 0);
362 monitor_printf(mon
, "%c CPU #%d:",
363 (env
== mon
->mon_cpu
) ? '*' : ' ',
365 #if defined(TARGET_I386)
366 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
367 env
->eip
+ env
->segs
[R_CS
].base
);
368 #elif defined(TARGET_PPC)
369 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
370 #elif defined(TARGET_SPARC)
371 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
373 #elif defined(TARGET_MIPS)
374 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
377 monitor_printf(mon
, " (halted)");
378 monitor_printf(mon
, "\n");
382 static void do_cpu_set(Monitor
*mon
, int index
)
384 if (mon_set_cpu(index
) < 0)
385 monitor_printf(mon
, "Invalid CPU index\n");
388 static void do_info_jit(Monitor
*mon
)
390 dump_exec_info((FILE *)mon
, monitor_fprintf
);
393 static void do_info_history(Monitor
*mon
)
402 str
= readline_get_history(mon
->rs
, i
);
405 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
410 #if defined(TARGET_PPC)
411 /* XXX: not implemented in other targets */
412 static void do_info_cpu_stats(Monitor
*mon
)
417 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
421 static void do_quit(Monitor
*mon
)
426 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
428 if (bdrv_is_inserted(bs
)) {
430 if (!bdrv_is_removable(bs
)) {
431 monitor_printf(mon
, "device is not removable\n");
434 if (bdrv_is_locked(bs
)) {
435 monitor_printf(mon
, "device is locked\n");
444 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
446 BlockDriverState
*bs
;
448 bs
= bdrv_find(filename
);
450 monitor_printf(mon
, "device not found\n");
453 eject_device(mon
, bs
, force
);
456 static void do_change_block(Monitor
*mon
, const char *device
,
457 const char *filename
, const char *fmt
)
459 BlockDriverState
*bs
;
460 BlockDriver
*drv
= NULL
;
462 bs
= bdrv_find(device
);
464 monitor_printf(mon
, "device not found\n");
468 drv
= bdrv_find_format(fmt
);
470 monitor_printf(mon
, "invalid format %s\n", fmt
);
474 if (eject_device(mon
, bs
, 0) < 0)
476 bdrv_open2(bs
, filename
, 0, drv
);
477 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
480 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
483 if (vnc_display_password(NULL
, password
) < 0)
484 monitor_printf(mon
, "could not set VNC server password\n");
486 monitor_read_command(mon
, 1);
489 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
491 if (strcmp(target
, "passwd") == 0 ||
492 strcmp(target
, "password") == 0) {
495 strncpy(password
, arg
, sizeof(password
));
496 password
[sizeof(password
) - 1] = '\0';
497 change_vnc_password_cb(mon
, password
, NULL
);
499 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
502 if (vnc_display_open(NULL
, target
) < 0)
503 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
507 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
510 if (strcmp(device
, "vnc") == 0) {
511 do_change_vnc(mon
, target
, arg
);
513 do_change_block(mon
, device
, target
, arg
);
517 static void do_screen_dump(Monitor
*mon
, const char *filename
)
519 vga_hw_screen_dump(filename
);
522 static void do_logfile(Monitor
*mon
, const char *filename
)
524 cpu_set_log_filename(filename
);
527 static void do_log(Monitor
*mon
, const char *items
)
531 if (!strcmp(items
, "none")) {
534 mask
= cpu_str_to_log_mask(items
);
536 help_cmd(mon
, "log");
543 static void do_singlestep(Monitor
*mon
, const char *option
)
545 if (!option
|| !strcmp(option
, "on")) {
547 } else if (!strcmp(option
, "off")) {
550 monitor_printf(mon
, "unexpected option %s\n", option
);
554 static void do_stop(Monitor
*mon
)
556 vm_stop(EXCP_INTERRUPT
);
559 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
561 struct bdrv_iterate_context
{
566 static void do_cont(Monitor
*mon
)
568 struct bdrv_iterate_context context
= { mon
, 0 };
570 bdrv_iterate(encrypted_bdrv_it
, &context
);
571 /* only resume the vm if all keys are set and valid */
576 static void bdrv_key_cb(void *opaque
, int err
)
578 Monitor
*mon
= opaque
;
580 /* another key was set successfully, retry to continue */
585 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
587 struct bdrv_iterate_context
*context
= opaque
;
589 if (!context
->err
&& bdrv_key_required(bs
)) {
590 context
->err
= -EBUSY
;
591 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
596 static void do_gdbserver(Monitor
*mon
, const char *device
)
599 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
600 if (gdbserver_start(device
) < 0) {
601 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
603 } else if (strcmp(device
, "none") == 0) {
604 monitor_printf(mon
, "Disabled gdbserver\n");
606 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
611 static void do_watchdog_action(Monitor
*mon
, const char *action
)
613 if (select_watchdog_action(action
) == -1) {
614 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
618 static void monitor_printc(Monitor
*mon
, int c
)
620 monitor_printf(mon
, "'");
623 monitor_printf(mon
, "\\'");
626 monitor_printf(mon
, "\\\\");
629 monitor_printf(mon
, "\\n");
632 monitor_printf(mon
, "\\r");
635 if (c
>= 32 && c
<= 126) {
636 monitor_printf(mon
, "%c", c
);
638 monitor_printf(mon
, "\\x%02x", c
);
642 monitor_printf(mon
, "'");
645 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
646 target_phys_addr_t addr
, int is_physical
)
649 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
657 if (!env
&& !is_physical
)
662 } else if (wsize
== 4) {
665 /* as default we use the current CS size */
669 if ((env
->efer
& MSR_EFER_LMA
) &&
670 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
674 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
679 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
688 nb_per_line
= line_size
/ wsize
;
693 max_digits
= (wsize
* 8 + 2) / 3;
697 max_digits
= (wsize
* 8) / 4;
701 max_digits
= (wsize
* 8 * 10 + 32) / 33;
710 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
712 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
717 cpu_physical_memory_rw(addr
, buf
, l
, 0);
722 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
723 monitor_printf(mon
, " Cannot access memory\n");
732 v
= ldub_raw(buf
+ i
);
735 v
= lduw_raw(buf
+ i
);
738 v
= (uint32_t)ldl_raw(buf
+ i
);
741 v
= ldq_raw(buf
+ i
);
744 monitor_printf(mon
, " ");
747 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
750 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
753 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
756 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
759 monitor_printc(mon
, v
);
764 monitor_printf(mon
, "\n");
770 #if TARGET_LONG_BITS == 64
771 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
773 #define GET_TLONG(h, l) (l)
776 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
777 uint32_t addrh
, uint32_t addrl
)
779 target_long addr
= GET_TLONG(addrh
, addrl
);
780 memory_dump(mon
, count
, format
, size
, addr
, 0);
783 #if TARGET_PHYS_ADDR_BITS > 32
784 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
786 #define GET_TPHYSADDR(h, l) (l)
789 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
790 int size
, uint32_t addrh
, uint32_t addrl
)
793 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
794 memory_dump(mon
, count
, format
, size
, addr
, 1);
797 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
798 unsigned int valh
, unsigned int vall
)
800 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
801 #if TARGET_PHYS_ADDR_BITS == 32
804 monitor_printf(mon
, "%#o", val
);
807 monitor_printf(mon
, "%#x", val
);
810 monitor_printf(mon
, "%u", val
);
814 monitor_printf(mon
, "%d", val
);
817 monitor_printc(mon
, val
);
823 monitor_printf(mon
, "%#" PRIo64
, val
);
826 monitor_printf(mon
, "%#" PRIx64
, val
);
829 monitor_printf(mon
, "%" PRIu64
, val
);
833 monitor_printf(mon
, "%" PRId64
, val
);
836 monitor_printc(mon
, val
);
840 monitor_printf(mon
, "\n");
843 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
844 uint32_t size
, const char *filename
)
847 target_long addr
= GET_TLONG(valh
, vall
);
856 f
= fopen(filename
, "wb");
858 monitor_printf(mon
, "could not open '%s'\n", filename
);
865 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
866 fwrite(buf
, 1, l
, f
);
873 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
874 unsigned int vall
, uint32_t size
,
875 const char *filename
)
880 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
882 f
= fopen(filename
, "wb");
884 monitor_printf(mon
, "could not open '%s'\n", filename
);
891 cpu_physical_memory_rw(addr
, buf
, l
, 0);
892 fwrite(buf
, 1, l
, f
);
900 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
907 for(addr
= start
; addr
< (start
+ size
); addr
++) {
908 cpu_physical_memory_rw(addr
, buf
, 1, 0);
909 /* BSD sum algorithm ('sum' Unix command) */
910 sum
= (sum
>> 1) | (sum
<< 15);
913 monitor_printf(mon
, "%05d\n", sum
);
921 static const KeyDef key_defs
[] = {
948 { 0x0e, "backspace" },
985 { 0x37, "asterisk" },
988 { 0x3a, "caps_lock" },
999 { 0x45, "num_lock" },
1000 { 0x46, "scroll_lock" },
1002 { 0xb5, "kp_divide" },
1003 { 0x37, "kp_multiply" },
1004 { 0x4a, "kp_subtract" },
1006 { 0x9c, "kp_enter" },
1007 { 0x53, "kp_decimal" },
1040 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1055 { 0xfe, "compose" },
1060 static int get_keycode(const char *key
)
1066 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1067 if (!strcmp(key
, p
->name
))
1070 if (strstart(key
, "0x", NULL
)) {
1071 ret
= strtoul(key
, &endp
, 0);
1072 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1078 #define MAX_KEYCODES 16
1079 static uint8_t keycodes
[MAX_KEYCODES
];
1080 static int nb_pending_keycodes
;
1081 static QEMUTimer
*key_timer
;
1083 static void release_keys(void *opaque
)
1087 while (nb_pending_keycodes
> 0) {
1088 nb_pending_keycodes
--;
1089 keycode
= keycodes
[nb_pending_keycodes
];
1091 kbd_put_keycode(0xe0);
1092 kbd_put_keycode(keycode
| 0x80);
1096 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1099 char keyname_buf
[16];
1101 int keyname_len
, keycode
, i
;
1103 if (nb_pending_keycodes
> 0) {
1104 qemu_del_timer(key_timer
);
1111 separator
= strchr(string
, '-');
1112 keyname_len
= separator
? separator
- string
: strlen(string
);
1113 if (keyname_len
> 0) {
1114 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1115 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1116 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1119 if (i
== MAX_KEYCODES
) {
1120 monitor_printf(mon
, "too many keys\n");
1123 keyname_buf
[keyname_len
] = 0;
1124 keycode
= get_keycode(keyname_buf
);
1126 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1129 keycodes
[i
++] = keycode
;
1133 string
= separator
+ 1;
1135 nb_pending_keycodes
= i
;
1136 /* key down events */
1137 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1138 keycode
= keycodes
[i
];
1140 kbd_put_keycode(0xe0);
1141 kbd_put_keycode(keycode
& 0x7f);
1143 /* delayed key up events */
1144 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1145 muldiv64(ticks_per_sec
, hold_time
, 1000));
1148 static int mouse_button_state
;
1150 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1154 dx
= strtol(dx_str
, NULL
, 0);
1155 dy
= strtol(dy_str
, NULL
, 0);
1158 dz
= strtol(dz_str
, NULL
, 0);
1159 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1162 static void do_mouse_button(Monitor
*mon
, int button_state
)
1164 mouse_button_state
= button_state
;
1165 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1168 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1169 int addr
, int has_index
, int index
)
1175 cpu_outb(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1183 val
= cpu_inb(NULL
, addr
);
1187 val
= cpu_inw(NULL
, addr
);
1191 val
= cpu_inl(NULL
, addr
);
1195 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1196 suffix
, addr
, size
* 2, val
);
1199 static void do_ioport_write(Monitor
*mon
, int count
, int format
, int size
,
1202 addr
&= IOPORTS_MASK
;
1207 cpu_outb(NULL
, addr
, val
);
1210 cpu_outw(NULL
, addr
, val
);
1213 cpu_outl(NULL
, addr
, val
);
1218 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1222 res
= qemu_boot_set(bootdevice
);
1224 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1225 } else if (res
> 0) {
1226 monitor_printf(mon
, "setting boot device list failed\n");
1228 monitor_printf(mon
, "no function defined to set boot device list for "
1229 "this architecture\n");
1233 static void do_system_reset(Monitor
*mon
)
1235 qemu_system_reset_request();
1238 static void do_system_powerdown(Monitor
*mon
)
1240 qemu_system_powerdown_request();
1243 #if defined(TARGET_I386)
1244 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1246 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1249 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1250 pte
& PG_PSE_MASK
? 'P' : '-',
1251 pte
& PG_DIRTY_MASK
? 'D' : '-',
1252 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1253 pte
& PG_PCD_MASK
? 'C' : '-',
1254 pte
& PG_PWT_MASK
? 'T' : '-',
1255 pte
& PG_USER_MASK
? 'U' : '-',
1256 pte
& PG_RW_MASK
? 'W' : '-');
1259 static void tlb_info(Monitor
*mon
)
1263 uint32_t pgd
, pde
, pte
;
1265 env
= mon_get_cpu();
1269 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1270 monitor_printf(mon
, "PG disabled\n");
1273 pgd
= env
->cr
[3] & ~0xfff;
1274 for(l1
= 0; l1
< 1024; l1
++) {
1275 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1276 pde
= le32_to_cpu(pde
);
1277 if (pde
& PG_PRESENT_MASK
) {
1278 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1279 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1281 for(l2
= 0; l2
< 1024; l2
++) {
1282 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1283 (uint8_t *)&pte
, 4);
1284 pte
= le32_to_cpu(pte
);
1285 if (pte
& PG_PRESENT_MASK
) {
1286 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1296 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1297 uint32_t end
, int prot
)
1300 prot1
= *plast_prot
;
1301 if (prot
!= prot1
) {
1302 if (*pstart
!= -1) {
1303 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1304 *pstart
, end
, end
- *pstart
,
1305 prot1
& PG_USER_MASK
? 'u' : '-',
1307 prot1
& PG_RW_MASK
? 'w' : '-');
1317 static void mem_info(Monitor
*mon
)
1320 int l1
, l2
, prot
, last_prot
;
1321 uint32_t pgd
, pde
, pte
, start
, end
;
1323 env
= mon_get_cpu();
1327 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1328 monitor_printf(mon
, "PG disabled\n");
1331 pgd
= env
->cr
[3] & ~0xfff;
1334 for(l1
= 0; l1
< 1024; l1
++) {
1335 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1336 pde
= le32_to_cpu(pde
);
1338 if (pde
& PG_PRESENT_MASK
) {
1339 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1340 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1341 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1343 for(l2
= 0; l2
< 1024; l2
++) {
1344 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1345 (uint8_t *)&pte
, 4);
1346 pte
= le32_to_cpu(pte
);
1347 end
= (l1
<< 22) + (l2
<< 12);
1348 if (pte
& PG_PRESENT_MASK
) {
1349 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1353 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1358 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1364 #if defined(TARGET_SH4)
1366 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1368 monitor_printf(mon
, " tlb%i:\t"
1369 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1370 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1371 "dirty=%hhu writethrough=%hhu\n",
1373 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1374 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1378 static void tlb_info(Monitor
*mon
)
1380 CPUState
*env
= mon_get_cpu();
1383 monitor_printf (mon
, "ITLB:\n");
1384 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1385 print_tlb (mon
, i
, &env
->itlb
[i
]);
1386 monitor_printf (mon
, "UTLB:\n");
1387 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1388 print_tlb (mon
, i
, &env
->utlb
[i
]);
1393 static void do_info_kvm(Monitor
*mon
)
1396 monitor_printf(mon
, "kvm support: ");
1398 monitor_printf(mon
, "enabled\n");
1400 monitor_printf(mon
, "disabled\n");
1402 monitor_printf(mon
, "kvm support: not compiled\n");
1406 static void do_info_numa(Monitor
*mon
)
1411 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1412 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1413 monitor_printf(mon
, "node %d cpus:", i
);
1414 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1415 if (env
->numa_node
== i
) {
1416 monitor_printf(mon
, " %d", env
->cpu_index
);
1419 monitor_printf(mon
, "\n");
1420 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1425 #ifdef CONFIG_PROFILER
1427 static void do_info_profile(Monitor
*mon
)
1433 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1434 dev_time
, dev_time
/ (double)ticks_per_sec
);
1435 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1436 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1441 static void do_info_profile(Monitor
*mon
)
1443 monitor_printf(mon
, "Internal profiler not compiled\n");
1447 /* Capture support */
1448 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1450 static void do_info_capture(Monitor
*mon
)
1455 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1456 monitor_printf(mon
, "[%d]: ", i
);
1457 s
->ops
.info (s
->opaque
);
1462 static void do_stop_capture(Monitor
*mon
, int n
)
1467 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1469 s
->ops
.destroy (s
->opaque
);
1470 LIST_REMOVE (s
, entries
);
1477 static void do_wav_capture(Monitor
*mon
, const char *path
,
1478 int has_freq
, int freq
,
1479 int has_bits
, int bits
,
1480 int has_channels
, int nchannels
)
1484 s
= qemu_mallocz (sizeof (*s
));
1486 freq
= has_freq
? freq
: 44100;
1487 bits
= has_bits
? bits
: 16;
1488 nchannels
= has_channels
? nchannels
: 2;
1490 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1491 monitor_printf(mon
, "Faied to add wave capture\n");
1494 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1498 #if defined(TARGET_I386)
1499 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1503 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1504 if (env
->cpu_index
== cpu_index
) {
1505 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1511 static void do_info_status(Monitor
*mon
)
1515 monitor_printf(mon
, "VM status: running (single step mode)\n");
1517 monitor_printf(mon
, "VM status: running\n");
1520 monitor_printf(mon
, "VM status: paused\n");
1524 static void do_balloon(Monitor
*mon
, int value
)
1526 ram_addr_t target
= value
;
1527 qemu_balloon(target
<< 20);
1530 static void do_info_balloon(Monitor
*mon
)
1534 actual
= qemu_balloon_status();
1535 if (kvm_enabled() && !kvm_has_sync_mmu())
1536 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1537 "ballooning disabled\n");
1538 else if (actual
== 0)
1539 monitor_printf(mon
, "Ballooning not activated in VM\n");
1541 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1544 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1546 qemu_acl
*acl
= qemu_acl_find(name
);
1549 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1554 static void do_acl_show(Monitor
*mon
, const char *aclname
)
1556 qemu_acl
*acl
= find_acl(mon
, aclname
);
1557 qemu_acl_entry
*entry
;
1561 monitor_printf(mon
, "policy: %s\n",
1562 acl
->defaultDeny
? "deny" : "allow");
1563 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1565 monitor_printf(mon
, "%d: %s %s\n", i
,
1566 entry
->deny
? "deny" : "allow", entry
->match
);
1571 static void do_acl_reset(Monitor
*mon
, const char *aclname
)
1573 qemu_acl
*acl
= find_acl(mon
, aclname
);
1576 qemu_acl_reset(acl
);
1577 monitor_printf(mon
, "acl: removed all rules\n");
1581 static void do_acl_policy(Monitor
*mon
, const char *aclname
,
1584 qemu_acl
*acl
= find_acl(mon
, aclname
);
1587 if (strcmp(policy
, "allow") == 0) {
1588 acl
->defaultDeny
= 0;
1589 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1590 } else if (strcmp(policy
, "deny") == 0) {
1591 acl
->defaultDeny
= 1;
1592 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1594 monitor_printf(mon
, "acl: unknown policy '%s', "
1595 "expected 'deny' or 'allow'\n", policy
);
1600 static void do_acl_add(Monitor
*mon
, const char *aclname
,
1601 const char *match
, const char *policy
,
1602 int has_index
, int index
)
1604 qemu_acl
*acl
= find_acl(mon
, aclname
);
1608 if (strcmp(policy
, "allow") == 0) {
1610 } else if (strcmp(policy
, "deny") == 0) {
1613 monitor_printf(mon
, "acl: unknown policy '%s', "
1614 "expected 'deny' or 'allow'\n", policy
);
1618 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1620 ret
= qemu_acl_append(acl
, deny
, match
);
1622 monitor_printf(mon
, "acl: unable to add acl entry\n");
1624 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1628 static void do_acl_remove(Monitor
*mon
, const char *aclname
, const char *match
)
1630 qemu_acl
*acl
= find_acl(mon
, aclname
);
1634 ret
= qemu_acl_remove(acl
, match
);
1636 monitor_printf(mon
, "acl: no matching acl entry\n");
1638 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1642 #if defined(TARGET_I386)
1643 static void do_inject_mce(Monitor
*mon
,
1644 int cpu_index
, int bank
,
1645 unsigned status_hi
, unsigned status_lo
,
1646 unsigned mcg_status_hi
, unsigned mcg_status_lo
,
1647 unsigned addr_hi
, unsigned addr_lo
,
1648 unsigned misc_hi
, unsigned misc_lo
)
1651 uint64_t status
= ((uint64_t)status_hi
<< 32) | status_lo
;
1652 uint64_t mcg_status
= ((uint64_t)mcg_status_hi
<< 32) | mcg_status_lo
;
1653 uint64_t addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1654 uint64_t misc
= ((uint64_t)misc_hi
<< 32) | misc_lo
;
1656 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1657 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1658 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1664 static void do_getfd(Monitor
*mon
, const char *fdname
)
1669 fd
= qemu_chr_get_msgfd(mon
->chr
);
1671 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1675 if (qemu_isdigit(fdname
[0])) {
1676 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1682 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1687 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1688 if (strcmp(monfd
->name
, fdname
) != 0) {
1697 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1698 monfd
->name
= qemu_strdup(fdname
);
1701 LIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1704 static void do_closefd(Monitor
*mon
, const char *fdname
)
1708 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1709 if (strcmp(monfd
->name
, fdname
) != 0) {
1713 LIST_REMOVE(monfd
, next
);
1715 qemu_free(monfd
->name
);
1720 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1724 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1728 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1731 if (strcmp(monfd
->name
, fdname
) != 0) {
1737 /* caller takes ownership of fd */
1738 LIST_REMOVE(monfd
, next
);
1739 qemu_free(monfd
->name
);
1748 static const mon_cmd_t mon_cmds
[] = {
1749 #include "qemu-monitor.h"
1753 /* Please update qemu-monitor.hx when adding or changing commands */
1754 static const mon_cmd_t info_cmds
[] = {
1755 { "version", "", do_info_version
,
1756 "", "show the version of QEMU" },
1757 { "network", "", do_info_network
,
1758 "", "show the network state" },
1759 { "chardev", "", qemu_chr_info
,
1760 "", "show the character devices" },
1761 { "block", "", bdrv_info
,
1762 "", "show the block devices" },
1763 { "blockstats", "", bdrv_info_stats
,
1764 "", "show block device statistics" },
1765 { "registers", "", do_info_registers
,
1766 "", "show the cpu registers" },
1767 { "cpus", "", do_info_cpus
,
1768 "", "show infos for each CPU" },
1769 { "history", "", do_info_history
,
1770 "", "show the command line history", },
1771 { "irq", "", irq_info
,
1772 "", "show the interrupts statistics (if available)", },
1773 { "pic", "", pic_info
,
1774 "", "show i8259 (PIC) state", },
1775 { "pci", "", pci_info
,
1776 "", "show PCI info", },
1777 #if defined(TARGET_I386) || defined(TARGET_SH4)
1778 { "tlb", "", tlb_info
,
1779 "", "show virtual to physical memory mappings", },
1781 #if defined(TARGET_I386)
1782 { "mem", "", mem_info
,
1783 "", "show the active virtual memory mappings", },
1784 { "hpet", "", do_info_hpet
,
1785 "", "show state of HPET", },
1787 { "jit", "", do_info_jit
,
1788 "", "show dynamic compiler info", },
1789 { "kvm", "", do_info_kvm
,
1790 "", "show KVM information", },
1791 { "numa", "", do_info_numa
,
1792 "", "show NUMA information", },
1793 { "usb", "", usb_info
,
1794 "", "show guest USB devices", },
1795 { "usbhost", "", usb_host_info
,
1796 "", "show host USB devices", },
1797 { "profile", "", do_info_profile
,
1798 "", "show profiling information", },
1799 { "capture", "", do_info_capture
,
1800 "", "show capture information" },
1801 { "snapshots", "", do_info_snapshots
,
1802 "", "show the currently saved VM snapshots" },
1803 { "status", "", do_info_status
,
1804 "", "show the current VM status (running|paused)" },
1805 { "pcmcia", "", pcmcia_info
,
1806 "", "show guest PCMCIA status" },
1807 { "mice", "", do_info_mice
,
1808 "", "show which guest mouse is receiving events" },
1809 { "vnc", "", do_info_vnc
,
1810 "", "show the vnc server status"},
1811 { "name", "", do_info_name
,
1812 "", "show the current VM name" },
1813 { "uuid", "", do_info_uuid
,
1814 "", "show the current VM UUID" },
1815 #if defined(TARGET_PPC)
1816 { "cpustats", "", do_info_cpu_stats
,
1817 "", "show CPU statistics", },
1819 #if defined(CONFIG_SLIRP)
1820 { "usernet", "", do_info_usernet
,
1821 "", "show user network stack connection states", },
1823 { "migrate", "", do_info_migrate
, "", "show migration status" },
1824 { "balloon", "", do_info_balloon
,
1825 "", "show balloon information" },
1826 { "qtree", "", do_info_qtree
,
1827 "", "show device tree" },
1828 { "qdm", "", do_info_qdm
,
1829 "", "show qdev device model list" },
1833 /*******************************************************************/
1835 static const char *pch
;
1836 static jmp_buf expr_env
;
1841 typedef struct MonitorDef
{
1844 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1848 #if defined(TARGET_I386)
1849 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1851 CPUState
*env
= mon_get_cpu();
1854 return env
->eip
+ env
->segs
[R_CS
].base
;
1858 #if defined(TARGET_PPC)
1859 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1861 CPUState
*env
= mon_get_cpu();
1869 for (i
= 0; i
< 8; i
++)
1870 u
|= env
->crf
[i
] << (32 - (4 * i
));
1875 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1877 CPUState
*env
= mon_get_cpu();
1883 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1885 CPUState
*env
= mon_get_cpu();
1891 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1893 CPUState
*env
= mon_get_cpu();
1896 return cpu_ppc_load_decr(env
);
1899 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1901 CPUState
*env
= mon_get_cpu();
1904 return cpu_ppc_load_tbu(env
);
1907 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1909 CPUState
*env
= mon_get_cpu();
1912 return cpu_ppc_load_tbl(env
);
1916 #if defined(TARGET_SPARC)
1917 #ifndef TARGET_SPARC64
1918 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1920 CPUState
*env
= mon_get_cpu();
1923 return GET_PSR(env
);
1927 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1929 CPUState
*env
= mon_get_cpu();
1932 return env
->regwptr
[val
];
1936 static const MonitorDef monitor_defs
[] = {
1939 #define SEG(name, seg) \
1940 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1941 { name ".base", offsetof(CPUState, segs[seg].base) },\
1942 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1944 { "eax", offsetof(CPUState
, regs
[0]) },
1945 { "ecx", offsetof(CPUState
, regs
[1]) },
1946 { "edx", offsetof(CPUState
, regs
[2]) },
1947 { "ebx", offsetof(CPUState
, regs
[3]) },
1948 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1949 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1950 { "esi", offsetof(CPUState
, regs
[6]) },
1951 { "edi", offsetof(CPUState
, regs
[7]) },
1952 #ifdef TARGET_X86_64
1953 { "r8", offsetof(CPUState
, regs
[8]) },
1954 { "r9", offsetof(CPUState
, regs
[9]) },
1955 { "r10", offsetof(CPUState
, regs
[10]) },
1956 { "r11", offsetof(CPUState
, regs
[11]) },
1957 { "r12", offsetof(CPUState
, regs
[12]) },
1958 { "r13", offsetof(CPUState
, regs
[13]) },
1959 { "r14", offsetof(CPUState
, regs
[14]) },
1960 { "r15", offsetof(CPUState
, regs
[15]) },
1962 { "eflags", offsetof(CPUState
, eflags
) },
1963 { "eip", offsetof(CPUState
, eip
) },
1970 { "pc", 0, monitor_get_pc
, },
1971 #elif defined(TARGET_PPC)
1972 /* General purpose registers */
1973 { "r0", offsetof(CPUState
, gpr
[0]) },
1974 { "r1", offsetof(CPUState
, gpr
[1]) },
1975 { "r2", offsetof(CPUState
, gpr
[2]) },
1976 { "r3", offsetof(CPUState
, gpr
[3]) },
1977 { "r4", offsetof(CPUState
, gpr
[4]) },
1978 { "r5", offsetof(CPUState
, gpr
[5]) },
1979 { "r6", offsetof(CPUState
, gpr
[6]) },
1980 { "r7", offsetof(CPUState
, gpr
[7]) },
1981 { "r8", offsetof(CPUState
, gpr
[8]) },
1982 { "r9", offsetof(CPUState
, gpr
[9]) },
1983 { "r10", offsetof(CPUState
, gpr
[10]) },
1984 { "r11", offsetof(CPUState
, gpr
[11]) },
1985 { "r12", offsetof(CPUState
, gpr
[12]) },
1986 { "r13", offsetof(CPUState
, gpr
[13]) },
1987 { "r14", offsetof(CPUState
, gpr
[14]) },
1988 { "r15", offsetof(CPUState
, gpr
[15]) },
1989 { "r16", offsetof(CPUState
, gpr
[16]) },
1990 { "r17", offsetof(CPUState
, gpr
[17]) },
1991 { "r18", offsetof(CPUState
, gpr
[18]) },
1992 { "r19", offsetof(CPUState
, gpr
[19]) },
1993 { "r20", offsetof(CPUState
, gpr
[20]) },
1994 { "r21", offsetof(CPUState
, gpr
[21]) },
1995 { "r22", offsetof(CPUState
, gpr
[22]) },
1996 { "r23", offsetof(CPUState
, gpr
[23]) },
1997 { "r24", offsetof(CPUState
, gpr
[24]) },
1998 { "r25", offsetof(CPUState
, gpr
[25]) },
1999 { "r26", offsetof(CPUState
, gpr
[26]) },
2000 { "r27", offsetof(CPUState
, gpr
[27]) },
2001 { "r28", offsetof(CPUState
, gpr
[28]) },
2002 { "r29", offsetof(CPUState
, gpr
[29]) },
2003 { "r30", offsetof(CPUState
, gpr
[30]) },
2004 { "r31", offsetof(CPUState
, gpr
[31]) },
2005 /* Floating point registers */
2006 { "f0", offsetof(CPUState
, fpr
[0]) },
2007 { "f1", offsetof(CPUState
, fpr
[1]) },
2008 { "f2", offsetof(CPUState
, fpr
[2]) },
2009 { "f3", offsetof(CPUState
, fpr
[3]) },
2010 { "f4", offsetof(CPUState
, fpr
[4]) },
2011 { "f5", offsetof(CPUState
, fpr
[5]) },
2012 { "f6", offsetof(CPUState
, fpr
[6]) },
2013 { "f7", offsetof(CPUState
, fpr
[7]) },
2014 { "f8", offsetof(CPUState
, fpr
[8]) },
2015 { "f9", offsetof(CPUState
, fpr
[9]) },
2016 { "f10", offsetof(CPUState
, fpr
[10]) },
2017 { "f11", offsetof(CPUState
, fpr
[11]) },
2018 { "f12", offsetof(CPUState
, fpr
[12]) },
2019 { "f13", offsetof(CPUState
, fpr
[13]) },
2020 { "f14", offsetof(CPUState
, fpr
[14]) },
2021 { "f15", offsetof(CPUState
, fpr
[15]) },
2022 { "f16", offsetof(CPUState
, fpr
[16]) },
2023 { "f17", offsetof(CPUState
, fpr
[17]) },
2024 { "f18", offsetof(CPUState
, fpr
[18]) },
2025 { "f19", offsetof(CPUState
, fpr
[19]) },
2026 { "f20", offsetof(CPUState
, fpr
[20]) },
2027 { "f21", offsetof(CPUState
, fpr
[21]) },
2028 { "f22", offsetof(CPUState
, fpr
[22]) },
2029 { "f23", offsetof(CPUState
, fpr
[23]) },
2030 { "f24", offsetof(CPUState
, fpr
[24]) },
2031 { "f25", offsetof(CPUState
, fpr
[25]) },
2032 { "f26", offsetof(CPUState
, fpr
[26]) },
2033 { "f27", offsetof(CPUState
, fpr
[27]) },
2034 { "f28", offsetof(CPUState
, fpr
[28]) },
2035 { "f29", offsetof(CPUState
, fpr
[29]) },
2036 { "f30", offsetof(CPUState
, fpr
[30]) },
2037 { "f31", offsetof(CPUState
, fpr
[31]) },
2038 { "fpscr", offsetof(CPUState
, fpscr
) },
2039 /* Next instruction pointer */
2040 { "nip|pc", offsetof(CPUState
, nip
) },
2041 { "lr", offsetof(CPUState
, lr
) },
2042 { "ctr", offsetof(CPUState
, ctr
) },
2043 { "decr", 0, &monitor_get_decr
, },
2044 { "ccr", 0, &monitor_get_ccr
, },
2045 /* Machine state register */
2046 { "msr", 0, &monitor_get_msr
, },
2047 { "xer", 0, &monitor_get_xer
, },
2048 { "tbu", 0, &monitor_get_tbu
, },
2049 { "tbl", 0, &monitor_get_tbl
, },
2050 #if defined(TARGET_PPC64)
2051 /* Address space register */
2052 { "asr", offsetof(CPUState
, asr
) },
2054 /* Segment registers */
2055 { "sdr1", offsetof(CPUState
, sdr1
) },
2056 { "sr0", offsetof(CPUState
, sr
[0]) },
2057 { "sr1", offsetof(CPUState
, sr
[1]) },
2058 { "sr2", offsetof(CPUState
, sr
[2]) },
2059 { "sr3", offsetof(CPUState
, sr
[3]) },
2060 { "sr4", offsetof(CPUState
, sr
[4]) },
2061 { "sr5", offsetof(CPUState
, sr
[5]) },
2062 { "sr6", offsetof(CPUState
, sr
[6]) },
2063 { "sr7", offsetof(CPUState
, sr
[7]) },
2064 { "sr8", offsetof(CPUState
, sr
[8]) },
2065 { "sr9", offsetof(CPUState
, sr
[9]) },
2066 { "sr10", offsetof(CPUState
, sr
[10]) },
2067 { "sr11", offsetof(CPUState
, sr
[11]) },
2068 { "sr12", offsetof(CPUState
, sr
[12]) },
2069 { "sr13", offsetof(CPUState
, sr
[13]) },
2070 { "sr14", offsetof(CPUState
, sr
[14]) },
2071 { "sr15", offsetof(CPUState
, sr
[15]) },
2072 /* Too lazy to put BATs and SPRs ... */
2073 #elif defined(TARGET_SPARC)
2074 { "g0", offsetof(CPUState
, gregs
[0]) },
2075 { "g1", offsetof(CPUState
, gregs
[1]) },
2076 { "g2", offsetof(CPUState
, gregs
[2]) },
2077 { "g3", offsetof(CPUState
, gregs
[3]) },
2078 { "g4", offsetof(CPUState
, gregs
[4]) },
2079 { "g5", offsetof(CPUState
, gregs
[5]) },
2080 { "g6", offsetof(CPUState
, gregs
[6]) },
2081 { "g7", offsetof(CPUState
, gregs
[7]) },
2082 { "o0", 0, monitor_get_reg
},
2083 { "o1", 1, monitor_get_reg
},
2084 { "o2", 2, monitor_get_reg
},
2085 { "o3", 3, monitor_get_reg
},
2086 { "o4", 4, monitor_get_reg
},
2087 { "o5", 5, monitor_get_reg
},
2088 { "o6", 6, monitor_get_reg
},
2089 { "o7", 7, monitor_get_reg
},
2090 { "l0", 8, monitor_get_reg
},
2091 { "l1", 9, monitor_get_reg
},
2092 { "l2", 10, monitor_get_reg
},
2093 { "l3", 11, monitor_get_reg
},
2094 { "l4", 12, monitor_get_reg
},
2095 { "l5", 13, monitor_get_reg
},
2096 { "l6", 14, monitor_get_reg
},
2097 { "l7", 15, monitor_get_reg
},
2098 { "i0", 16, monitor_get_reg
},
2099 { "i1", 17, monitor_get_reg
},
2100 { "i2", 18, monitor_get_reg
},
2101 { "i3", 19, monitor_get_reg
},
2102 { "i4", 20, monitor_get_reg
},
2103 { "i5", 21, monitor_get_reg
},
2104 { "i6", 22, monitor_get_reg
},
2105 { "i7", 23, monitor_get_reg
},
2106 { "pc", offsetof(CPUState
, pc
) },
2107 { "npc", offsetof(CPUState
, npc
) },
2108 { "y", offsetof(CPUState
, y
) },
2109 #ifndef TARGET_SPARC64
2110 { "psr", 0, &monitor_get_psr
, },
2111 { "wim", offsetof(CPUState
, wim
) },
2113 { "tbr", offsetof(CPUState
, tbr
) },
2114 { "fsr", offsetof(CPUState
, fsr
) },
2115 { "f0", offsetof(CPUState
, fpr
[0]) },
2116 { "f1", offsetof(CPUState
, fpr
[1]) },
2117 { "f2", offsetof(CPUState
, fpr
[2]) },
2118 { "f3", offsetof(CPUState
, fpr
[3]) },
2119 { "f4", offsetof(CPUState
, fpr
[4]) },
2120 { "f5", offsetof(CPUState
, fpr
[5]) },
2121 { "f6", offsetof(CPUState
, fpr
[6]) },
2122 { "f7", offsetof(CPUState
, fpr
[7]) },
2123 { "f8", offsetof(CPUState
, fpr
[8]) },
2124 { "f9", offsetof(CPUState
, fpr
[9]) },
2125 { "f10", offsetof(CPUState
, fpr
[10]) },
2126 { "f11", offsetof(CPUState
, fpr
[11]) },
2127 { "f12", offsetof(CPUState
, fpr
[12]) },
2128 { "f13", offsetof(CPUState
, fpr
[13]) },
2129 { "f14", offsetof(CPUState
, fpr
[14]) },
2130 { "f15", offsetof(CPUState
, fpr
[15]) },
2131 { "f16", offsetof(CPUState
, fpr
[16]) },
2132 { "f17", offsetof(CPUState
, fpr
[17]) },
2133 { "f18", offsetof(CPUState
, fpr
[18]) },
2134 { "f19", offsetof(CPUState
, fpr
[19]) },
2135 { "f20", offsetof(CPUState
, fpr
[20]) },
2136 { "f21", offsetof(CPUState
, fpr
[21]) },
2137 { "f22", offsetof(CPUState
, fpr
[22]) },
2138 { "f23", offsetof(CPUState
, fpr
[23]) },
2139 { "f24", offsetof(CPUState
, fpr
[24]) },
2140 { "f25", offsetof(CPUState
, fpr
[25]) },
2141 { "f26", offsetof(CPUState
, fpr
[26]) },
2142 { "f27", offsetof(CPUState
, fpr
[27]) },
2143 { "f28", offsetof(CPUState
, fpr
[28]) },
2144 { "f29", offsetof(CPUState
, fpr
[29]) },
2145 { "f30", offsetof(CPUState
, fpr
[30]) },
2146 { "f31", offsetof(CPUState
, fpr
[31]) },
2147 #ifdef TARGET_SPARC64
2148 { "f32", offsetof(CPUState
, fpr
[32]) },
2149 { "f34", offsetof(CPUState
, fpr
[34]) },
2150 { "f36", offsetof(CPUState
, fpr
[36]) },
2151 { "f38", offsetof(CPUState
, fpr
[38]) },
2152 { "f40", offsetof(CPUState
, fpr
[40]) },
2153 { "f42", offsetof(CPUState
, fpr
[42]) },
2154 { "f44", offsetof(CPUState
, fpr
[44]) },
2155 { "f46", offsetof(CPUState
, fpr
[46]) },
2156 { "f48", offsetof(CPUState
, fpr
[48]) },
2157 { "f50", offsetof(CPUState
, fpr
[50]) },
2158 { "f52", offsetof(CPUState
, fpr
[52]) },
2159 { "f54", offsetof(CPUState
, fpr
[54]) },
2160 { "f56", offsetof(CPUState
, fpr
[56]) },
2161 { "f58", offsetof(CPUState
, fpr
[58]) },
2162 { "f60", offsetof(CPUState
, fpr
[60]) },
2163 { "f62", offsetof(CPUState
, fpr
[62]) },
2164 { "asi", offsetof(CPUState
, asi
) },
2165 { "pstate", offsetof(CPUState
, pstate
) },
2166 { "cansave", offsetof(CPUState
, cansave
) },
2167 { "canrestore", offsetof(CPUState
, canrestore
) },
2168 { "otherwin", offsetof(CPUState
, otherwin
) },
2169 { "wstate", offsetof(CPUState
, wstate
) },
2170 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2171 { "fprs", offsetof(CPUState
, fprs
) },
2177 static void expr_error(Monitor
*mon
, const char *msg
)
2179 monitor_printf(mon
, "%s\n", msg
);
2180 longjmp(expr_env
, 1);
2183 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2184 static int get_monitor_def(target_long
*pval
, const char *name
)
2186 const MonitorDef
*md
;
2189 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2190 if (compare_cmd(name
, md
->name
)) {
2191 if (md
->get_value
) {
2192 *pval
= md
->get_value(md
, md
->offset
);
2194 CPUState
*env
= mon_get_cpu();
2197 ptr
= (uint8_t *)env
+ md
->offset
;
2200 *pval
= *(int32_t *)ptr
;
2203 *pval
= *(target_long
*)ptr
;
2216 static void next(void)
2220 while (qemu_isspace(*pch
))
2225 static int64_t expr_sum(Monitor
*mon
);
2227 static int64_t expr_unary(Monitor
*mon
)
2236 n
= expr_unary(mon
);
2240 n
= -expr_unary(mon
);
2244 n
= ~expr_unary(mon
);
2250 expr_error(mon
, "')' expected");
2257 expr_error(mon
, "character constant expected");
2261 expr_error(mon
, "missing terminating \' character");
2271 while ((*pch
>= 'a' && *pch
<= 'z') ||
2272 (*pch
>= 'A' && *pch
<= 'Z') ||
2273 (*pch
>= '0' && *pch
<= '9') ||
2274 *pch
== '_' || *pch
== '.') {
2275 if ((q
- buf
) < sizeof(buf
) - 1)
2279 while (qemu_isspace(*pch
))
2282 ret
= get_monitor_def(®
, buf
);
2284 expr_error(mon
, "unknown register");
2286 expr_error(mon
, "no cpu defined");
2291 expr_error(mon
, "unexpected end of expression");
2295 #if TARGET_PHYS_ADDR_BITS > 32
2296 n
= strtoull(pch
, &p
, 0);
2298 n
= strtoul(pch
, &p
, 0);
2301 expr_error(mon
, "invalid char in expression");
2304 while (qemu_isspace(*pch
))
2312 static int64_t expr_prod(Monitor
*mon
)
2317 val
= expr_unary(mon
);
2320 if (op
!= '*' && op
!= '/' && op
!= '%')
2323 val2
= expr_unary(mon
);
2332 expr_error(mon
, "division by zero");
2343 static int64_t expr_logic(Monitor
*mon
)
2348 val
= expr_prod(mon
);
2351 if (op
!= '&' && op
!= '|' && op
!= '^')
2354 val2
= expr_prod(mon
);
2371 static int64_t expr_sum(Monitor
*mon
)
2376 val
= expr_logic(mon
);
2379 if (op
!= '+' && op
!= '-')
2382 val2
= expr_logic(mon
);
2391 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2394 if (setjmp(expr_env
)) {
2398 while (qemu_isspace(*pch
))
2400 *pval
= expr_sum(mon
);
2405 static int get_str(char *buf
, int buf_size
, const char **pp
)
2413 while (qemu_isspace(*p
))
2423 while (*p
!= '\0' && *p
!= '\"') {
2439 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2442 if ((q
- buf
) < buf_size
- 1) {
2446 if ((q
- buf
) < buf_size
- 1) {
2453 qemu_printf("unterminated string\n");
2458 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2459 if ((q
- buf
) < buf_size
- 1) {
2471 * Store the command-name in cmdname, and return a pointer to
2472 * the remaining of the command string.
2474 static const char *get_command_name(const char *cmdline
,
2475 char *cmdname
, size_t nlen
)
2478 const char *p
, *pstart
;
2481 while (qemu_isspace(*p
))
2486 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2491 memcpy(cmdname
, pstart
, len
);
2492 cmdname
[len
] = '\0';
2496 static int default_fmt_format
= 'x';
2497 static int default_fmt_size
= 4;
2501 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2503 const char *p
, *typestr
;
2504 int c
, nb_args
, i
, has_arg
;
2505 const mon_cmd_t
*cmd
;
2508 void *str_allocated
[MAX_ARGS
];
2509 void *args
[MAX_ARGS
];
2510 void (*handler_0
)(Monitor
*mon
);
2511 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2512 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2513 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2514 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2516 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2517 void *arg3
, void *arg4
);
2518 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2519 void *arg3
, void *arg4
, void *arg5
);
2520 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2521 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2522 void (*handler_8
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2523 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2525 void (*handler_9
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2526 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2527 void *arg7
, void *arg8
);
2528 void (*handler_10
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2529 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2530 void *arg7
, void *arg8
, void *arg9
);
2533 monitor_printf(mon
, "command='%s'\n", cmdline
);
2536 /* extract the command name */
2537 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2541 /* find the command */
2542 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2543 if (compare_cmd(cmdname
, cmd
->name
))
2547 if (cmd
->name
== NULL
) {
2548 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2552 for(i
= 0; i
< MAX_ARGS
; i
++)
2553 str_allocated
[i
] = NULL
;
2555 /* parse the parameters */
2556 typestr
= cmd
->args_type
;
2571 while (qemu_isspace(*p
))
2573 if (*typestr
== '?') {
2576 /* no optional string: NULL argument */
2581 ret
= get_str(buf
, sizeof(buf
), &p
);
2585 monitor_printf(mon
, "%s: filename expected\n",
2589 monitor_printf(mon
, "%s: block device name expected\n",
2593 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2598 str
= qemu_malloc(strlen(buf
) + 1);
2599 pstrcpy(str
, sizeof(buf
), buf
);
2600 str_allocated
[nb_args
] = str
;
2602 if (nb_args
>= MAX_ARGS
) {
2604 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2607 args
[nb_args
++] = str
;
2612 int count
, format
, size
;
2614 while (qemu_isspace(*p
))
2620 if (qemu_isdigit(*p
)) {
2622 while (qemu_isdigit(*p
)) {
2623 count
= count
* 10 + (*p
- '0');
2661 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2662 monitor_printf(mon
, "invalid char in format: '%c'\n",
2667 format
= default_fmt_format
;
2668 if (format
!= 'i') {
2669 /* for 'i', not specifying a size gives -1 as size */
2671 size
= default_fmt_size
;
2672 default_fmt_size
= size
;
2674 default_fmt_format
= format
;
2677 format
= default_fmt_format
;
2678 if (format
!= 'i') {
2679 size
= default_fmt_size
;
2684 if (nb_args
+ 3 > MAX_ARGS
)
2686 args
[nb_args
++] = (void*)(long)count
;
2687 args
[nb_args
++] = (void*)(long)format
;
2688 args
[nb_args
++] = (void*)(long)size
;
2696 while (qemu_isspace(*p
))
2698 if (*typestr
== '?' || *typestr
== '.') {
2699 if (*typestr
== '?') {
2707 while (qemu_isspace(*p
))
2715 if (nb_args
>= MAX_ARGS
)
2717 args
[nb_args
++] = (void *)(long)has_arg
;
2719 if (nb_args
>= MAX_ARGS
)
2725 if (get_expr(mon
, &val
, &p
))
2729 if (nb_args
>= MAX_ARGS
)
2731 args
[nb_args
++] = (void *)(long)val
;
2733 if ((nb_args
+ 1) >= MAX_ARGS
)
2735 #if TARGET_PHYS_ADDR_BITS > 32
2736 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2738 args
[nb_args
++] = (void *)0;
2740 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2752 while (qemu_isspace(*p
))
2758 monitor_printf(mon
, "%s: unsupported option -%c\n",
2765 if (nb_args
>= MAX_ARGS
)
2767 args
[nb_args
++] = (void *)(long)has_option
;
2772 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2776 /* check that all arguments were parsed */
2777 while (qemu_isspace(*p
))
2780 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2787 handler_0
= cmd
->handler
;
2791 handler_1
= cmd
->handler
;
2792 handler_1(mon
, args
[0]);
2795 handler_2
= cmd
->handler
;
2796 handler_2(mon
, args
[0], args
[1]);
2799 handler_3
= cmd
->handler
;
2800 handler_3(mon
, args
[0], args
[1], args
[2]);
2803 handler_4
= cmd
->handler
;
2804 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2807 handler_5
= cmd
->handler
;
2808 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2811 handler_6
= cmd
->handler
;
2812 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2815 handler_7
= cmd
->handler
;
2816 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2820 handler_8
= cmd
->handler
;
2821 handler_8(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2825 handler_9
= cmd
->handler
;
2826 handler_9(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2827 args
[6], args
[7], args
[8]);
2830 handler_10
= cmd
->handler
;
2831 handler_10(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2832 args
[6], args
[7], args
[8], args
[9]);
2835 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2839 for(i
= 0; i
< MAX_ARGS
; i
++)
2840 qemu_free(str_allocated
[i
]);
2843 static void cmd_completion(const char *name
, const char *list
)
2845 const char *p
, *pstart
;
2854 p
= pstart
+ strlen(pstart
);
2856 if (len
> sizeof(cmd
) - 2)
2857 len
= sizeof(cmd
) - 2;
2858 memcpy(cmd
, pstart
, len
);
2860 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2861 readline_add_completion(cur_mon
->rs
, cmd
);
2869 static void file_completion(const char *input
)
2874 char file
[1024], file_prefix
[1024];
2878 p
= strrchr(input
, '/');
2881 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2882 pstrcpy(path
, sizeof(path
), ".");
2884 input_path_len
= p
- input
+ 1;
2885 memcpy(path
, input
, input_path_len
);
2886 if (input_path_len
> sizeof(path
) - 1)
2887 input_path_len
= sizeof(path
) - 1;
2888 path
[input_path_len
] = '\0';
2889 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2891 #ifdef DEBUG_COMPLETION
2892 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2893 input
, path
, file_prefix
);
2895 ffs
= opendir(path
);
2903 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2904 memcpy(file
, input
, input_path_len
);
2905 if (input_path_len
< sizeof(file
))
2906 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2908 /* stat the file to find out if it's a directory.
2909 * In that case add a slash to speed up typing long paths
2912 if(S_ISDIR(sb
.st_mode
))
2913 pstrcat(file
, sizeof(file
), "/");
2914 readline_add_completion(cur_mon
->rs
, file
);
2920 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2922 const char *name
= bdrv_get_device_name(bs
);
2923 const char *input
= opaque
;
2925 if (input
[0] == '\0' ||
2926 !strncmp(name
, (char *)input
, strlen(input
))) {
2927 readline_add_completion(cur_mon
->rs
, name
);
2931 /* NOTE: this parser is an approximate form of the real command parser */
2932 static void parse_cmdline(const char *cmdline
,
2933 int *pnb_args
, char **args
)
2942 while (qemu_isspace(*p
))
2946 if (nb_args
>= MAX_ARGS
)
2948 ret
= get_str(buf
, sizeof(buf
), &p
);
2949 args
[nb_args
] = qemu_strdup(buf
);
2954 *pnb_args
= nb_args
;
2957 static void monitor_find_completion(const char *cmdline
)
2959 const char *cmdname
;
2960 char *args
[MAX_ARGS
];
2961 int nb_args
, i
, len
;
2962 const char *ptype
, *str
;
2963 const mon_cmd_t
*cmd
;
2966 parse_cmdline(cmdline
, &nb_args
, args
);
2967 #ifdef DEBUG_COMPLETION
2968 for(i
= 0; i
< nb_args
; i
++) {
2969 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2973 /* if the line ends with a space, it means we want to complete the
2975 len
= strlen(cmdline
);
2976 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2977 if (nb_args
>= MAX_ARGS
)
2979 args
[nb_args
++] = qemu_strdup("");
2982 /* command completion */
2987 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
2988 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2989 cmd_completion(cmdname
, cmd
->name
);
2992 /* find the command */
2993 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2994 if (compare_cmd(args
[0], cmd
->name
))
2999 ptype
= cmd
->args_type
;
3000 for(i
= 0; i
< nb_args
- 2; i
++) {
3001 if (*ptype
!= '\0') {
3003 while (*ptype
== '?')
3007 str
= args
[nb_args
- 1];
3008 if (*ptype
== '-' && ptype
[1] != '\0') {
3013 /* file completion */
3014 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3015 file_completion(str
);
3018 /* block device name completion */
3019 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3020 bdrv_iterate(block_completion_it
, (void *)str
);
3023 /* XXX: more generic ? */
3024 if (!strcmp(cmd
->name
, "info")) {
3025 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3026 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3027 cmd_completion(str
, cmd
->name
);
3029 } else if (!strcmp(cmd
->name
, "sendkey")) {
3030 char *sep
= strrchr(str
, '-');
3033 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3034 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3035 cmd_completion(str
, key
->name
);
3037 } else if (!strcmp(cmd
->name
, "help|?")) {
3038 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3039 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3040 cmd_completion(str
, cmd
->name
);
3048 for(i
= 0; i
< nb_args
; i
++)
3052 static int monitor_can_read(void *opaque
)
3054 Monitor
*mon
= opaque
;
3056 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3059 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3061 Monitor
*old_mon
= cur_mon
;
3067 for (i
= 0; i
< size
; i
++)
3068 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3070 if (size
== 0 || buf
[size
- 1] != 0)
3071 monitor_printf(cur_mon
, "corrupted command\n");
3073 monitor_handle_command(cur_mon
, (char *)buf
);
3079 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3081 monitor_suspend(mon
);
3082 monitor_handle_command(mon
, cmdline
);
3083 monitor_resume(mon
);
3086 int monitor_suspend(Monitor
*mon
)
3094 void monitor_resume(Monitor
*mon
)
3098 if (--mon
->suspend_cnt
== 0)
3099 readline_show_prompt(mon
->rs
);
3102 static void monitor_event(void *opaque
, int event
)
3104 Monitor
*mon
= opaque
;
3107 case CHR_EVENT_MUX_IN
:
3108 readline_restart(mon
->rs
);
3109 monitor_resume(mon
);
3113 case CHR_EVENT_MUX_OUT
:
3114 if (mon
->suspend_cnt
== 0)
3115 monitor_printf(mon
, "\n");
3117 monitor_suspend(mon
);
3120 case CHR_EVENT_RESET
:
3121 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3122 "information\n", QEMU_VERSION
);
3123 if (mon
->chr
->focus
== 0)
3124 readline_show_prompt(mon
->rs
);
3138 void monitor_init(CharDriverState
*chr
, int flags
)
3140 static int is_first_init
= 1;
3143 if (is_first_init
) {
3144 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3148 mon
= qemu_mallocz(sizeof(*mon
));
3152 if (mon
->chr
->focus
!= 0)
3153 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3154 if (flags
& MONITOR_USE_READLINE
) {
3155 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3156 monitor_read_command(mon
, 0);
3159 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3162 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3163 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3167 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3169 BlockDriverState
*bs
= opaque
;
3172 if (bdrv_set_key(bs
, password
) != 0) {
3173 monitor_printf(mon
, "invalid password\n");
3176 if (mon
->password_completion_cb
)
3177 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3179 monitor_read_command(mon
, 1);
3182 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3183 BlockDriverCompletionFunc
*completion_cb
,
3188 if (!bdrv_key_required(bs
)) {
3190 completion_cb(opaque
, 0);
3194 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3195 bdrv_get_encrypted_filename(bs
));
3197 mon
->password_completion_cb
= completion_cb
;
3198 mon
->password_opaque
= opaque
;
3200 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3202 if (err
&& completion_cb
)
3203 completion_cb(opaque
, err
);