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
;
76 /* file descriptors passed via SCM_RIGHTS */
77 typedef struct mon_fd_t mon_fd_t
;
81 LIST_ENTRY(mon_fd_t
) next
;
92 BlockDriverCompletionFunc
*password_completion_cb
;
93 void *password_opaque
;
94 LIST_HEAD(,mon_fd_t
) fds
;
95 LIST_ENTRY(Monitor
) entry
;
98 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
100 static const mon_cmd_t mon_cmds
[];
101 static const mon_cmd_t info_cmds
[];
103 Monitor
*cur_mon
= NULL
;
105 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
108 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
110 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
112 readline_show_prompt(mon
->rs
);
115 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
119 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
120 /* prompt is printed on return from the command handler */
123 monitor_printf(mon
, "terminal does not support password prompting\n");
128 void monitor_flush(Monitor
*mon
)
130 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
131 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
132 mon
->outbuf_index
= 0;
136 /* flush at every end of line or if the buffer is full */
137 static void monitor_puts(Monitor
*mon
, const char *str
)
149 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
150 mon
->outbuf
[mon
->outbuf_index
++] = c
;
151 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
157 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
160 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
161 monitor_puts(mon
, buf
);
164 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
168 monitor_vprintf(mon
, fmt
, ap
);
172 void monitor_print_filename(Monitor
*mon
, const char *filename
)
176 for (i
= 0; filename
[i
]; i
++) {
177 switch (filename
[i
]) {
181 monitor_printf(mon
, "\\%c", filename
[i
]);
184 monitor_printf(mon
, "\\t");
187 monitor_printf(mon
, "\\r");
190 monitor_printf(mon
, "\\n");
193 monitor_printf(mon
, "%c", filename
[i
]);
199 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
203 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
208 static int compare_cmd(const char *name
, const char *list
)
210 const char *p
, *pstart
;
218 p
= pstart
+ strlen(pstart
);
219 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
228 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
229 const char *prefix
, const char *name
)
231 const mon_cmd_t
*cmd
;
233 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
234 if (!name
|| !strcmp(name
, cmd
->name
))
235 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
236 cmd
->params
, cmd
->help
);
240 static void help_cmd(Monitor
*mon
, const char *name
)
242 if (name
&& !strcmp(name
, "info")) {
243 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
245 help_cmd_dump(mon
, mon_cmds
, "", name
);
246 if (name
&& !strcmp(name
, "log")) {
247 const CPULogItem
*item
;
248 monitor_printf(mon
, "Log items (comma separated):\n");
249 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
250 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
251 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
257 static void do_help_cmd(Monitor
*mon
, const char *name
)
262 static void do_commit(Monitor
*mon
, const char *device
)
267 all_devices
= !strcmp(device
, "all");
268 TAILQ_FOREACH(dinfo
, &drives
, next
) {
270 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
272 bdrv_commit(dinfo
->bdrv
);
276 static void do_info(Monitor
*mon
, const char *item
)
278 const mon_cmd_t
*cmd
;
279 void (*handler
)(Monitor
*);
283 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
284 if (compare_cmd(item
, cmd
->name
))
288 help_cmd(mon
, "info");
291 handler
= cmd
->handler
;
295 static void do_info_version(Monitor
*mon
)
297 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
300 static void do_info_name(Monitor
*mon
)
303 monitor_printf(mon
, "%s\n", qemu_name
);
306 #if defined(TARGET_I386)
307 static void do_info_hpet(Monitor
*mon
)
309 monitor_printf(mon
, "HPET is %s by QEMU\n",
310 (no_hpet
) ? "disabled" : "enabled");
314 static void do_info_uuid(Monitor
*mon
)
316 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
317 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
318 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
319 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
320 qemu_uuid
[14], qemu_uuid
[15]);
323 /* get the current CPU defined by the user */
324 static int mon_set_cpu(int cpu_index
)
328 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
329 if (env
->cpu_index
== cpu_index
) {
330 cur_mon
->mon_cpu
= env
;
337 static CPUState
*mon_get_cpu(void)
339 if (!cur_mon
->mon_cpu
) {
342 cpu_synchronize_state(cur_mon
->mon_cpu
);
343 return cur_mon
->mon_cpu
;
346 static void do_info_registers(Monitor
*mon
)
353 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
356 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
361 static void do_info_cpus(Monitor
*mon
)
365 /* just to set the default cpu if not already done */
368 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
369 cpu_synchronize_state(env
);
370 monitor_printf(mon
, "%c CPU #%d:",
371 (env
== mon
->mon_cpu
) ? '*' : ' ',
373 #if defined(TARGET_I386)
374 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
375 env
->eip
+ env
->segs
[R_CS
].base
);
376 #elif defined(TARGET_PPC)
377 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
378 #elif defined(TARGET_SPARC)
379 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
381 #elif defined(TARGET_MIPS)
382 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
385 monitor_printf(mon
, " (halted)");
386 monitor_printf(mon
, "\n");
390 static void do_cpu_set(Monitor
*mon
, int index
)
392 if (mon_set_cpu(index
) < 0)
393 monitor_printf(mon
, "Invalid CPU index\n");
396 static void do_info_jit(Monitor
*mon
)
398 dump_exec_info((FILE *)mon
, monitor_fprintf
);
401 static void do_info_history(Monitor
*mon
)
410 str
= readline_get_history(mon
->rs
, i
);
413 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
418 #if defined(TARGET_PPC)
419 /* XXX: not implemented in other targets */
420 static void do_info_cpu_stats(Monitor
*mon
)
425 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
429 static void do_quit(Monitor
*mon
)
434 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
436 if (bdrv_is_inserted(bs
)) {
438 if (!bdrv_is_removable(bs
)) {
439 monitor_printf(mon
, "device is not removable\n");
442 if (bdrv_is_locked(bs
)) {
443 monitor_printf(mon
, "device is locked\n");
452 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
454 BlockDriverState
*bs
;
456 bs
= bdrv_find(filename
);
458 monitor_printf(mon
, "device not found\n");
461 eject_device(mon
, bs
, force
);
464 static void do_change_block(Monitor
*mon
, const char *device
,
465 const char *filename
, const char *fmt
)
467 BlockDriverState
*bs
;
468 BlockDriver
*drv
= NULL
;
470 bs
= bdrv_find(device
);
472 monitor_printf(mon
, "device not found\n");
476 drv
= bdrv_find_format(fmt
);
478 monitor_printf(mon
, "invalid format %s\n", fmt
);
482 if (eject_device(mon
, bs
, 0) < 0)
484 bdrv_open2(bs
, filename
, 0, drv
);
485 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
488 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
491 if (vnc_display_password(NULL
, password
) < 0)
492 monitor_printf(mon
, "could not set VNC server password\n");
494 monitor_read_command(mon
, 1);
497 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
499 if (strcmp(target
, "passwd") == 0 ||
500 strcmp(target
, "password") == 0) {
503 strncpy(password
, arg
, sizeof(password
));
504 password
[sizeof(password
) - 1] = '\0';
505 change_vnc_password_cb(mon
, password
, NULL
);
507 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
510 if (vnc_display_open(NULL
, target
) < 0)
511 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
515 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
518 if (strcmp(device
, "vnc") == 0) {
519 do_change_vnc(mon
, target
, arg
);
521 do_change_block(mon
, device
, target
, arg
);
525 static void do_screen_dump(Monitor
*mon
, const char *filename
)
527 vga_hw_screen_dump(filename
);
530 static void do_logfile(Monitor
*mon
, const char *filename
)
532 cpu_set_log_filename(filename
);
535 static void do_log(Monitor
*mon
, const char *items
)
539 if (!strcmp(items
, "none")) {
542 mask
= cpu_str_to_log_mask(items
);
544 help_cmd(mon
, "log");
551 static void do_singlestep(Monitor
*mon
, const char *option
)
553 if (!option
|| !strcmp(option
, "on")) {
555 } else if (!strcmp(option
, "off")) {
558 monitor_printf(mon
, "unexpected option %s\n", option
);
562 static void do_stop(Monitor
*mon
)
564 vm_stop(EXCP_INTERRUPT
);
567 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
569 struct bdrv_iterate_context
{
574 static void do_cont(Monitor
*mon
)
576 struct bdrv_iterate_context context
= { mon
, 0 };
578 bdrv_iterate(encrypted_bdrv_it
, &context
);
579 /* only resume the vm if all keys are set and valid */
584 static void bdrv_key_cb(void *opaque
, int err
)
586 Monitor
*mon
= opaque
;
588 /* another key was set successfully, retry to continue */
593 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
595 struct bdrv_iterate_context
*context
= opaque
;
597 if (!context
->err
&& bdrv_key_required(bs
)) {
598 context
->err
= -EBUSY
;
599 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
604 static void do_gdbserver(Monitor
*mon
, const char *device
)
607 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
608 if (gdbserver_start(device
) < 0) {
609 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
611 } else if (strcmp(device
, "none") == 0) {
612 monitor_printf(mon
, "Disabled gdbserver\n");
614 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
619 static void do_watchdog_action(Monitor
*mon
, const char *action
)
621 if (select_watchdog_action(action
) == -1) {
622 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
626 static void monitor_printc(Monitor
*mon
, int c
)
628 monitor_printf(mon
, "'");
631 monitor_printf(mon
, "\\'");
634 monitor_printf(mon
, "\\\\");
637 monitor_printf(mon
, "\\n");
640 monitor_printf(mon
, "\\r");
643 if (c
>= 32 && c
<= 126) {
644 monitor_printf(mon
, "%c", c
);
646 monitor_printf(mon
, "\\x%02x", c
);
650 monitor_printf(mon
, "'");
653 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
654 target_phys_addr_t addr
, int is_physical
)
657 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
665 if (!env
&& !is_physical
)
670 } else if (wsize
== 4) {
673 /* as default we use the current CS size */
677 if ((env
->efer
& MSR_EFER_LMA
) &&
678 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
682 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
687 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
696 nb_per_line
= line_size
/ wsize
;
701 max_digits
= (wsize
* 8 + 2) / 3;
705 max_digits
= (wsize
* 8) / 4;
709 max_digits
= (wsize
* 8 * 10 + 32) / 33;
718 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
720 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
725 cpu_physical_memory_rw(addr
, buf
, l
, 0);
730 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
731 monitor_printf(mon
, " Cannot access memory\n");
740 v
= ldub_raw(buf
+ i
);
743 v
= lduw_raw(buf
+ i
);
746 v
= (uint32_t)ldl_raw(buf
+ i
);
749 v
= ldq_raw(buf
+ i
);
752 monitor_printf(mon
, " ");
755 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
758 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
761 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
764 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
767 monitor_printc(mon
, v
);
772 monitor_printf(mon
, "\n");
778 #if TARGET_LONG_BITS == 64
779 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
781 #define GET_TLONG(h, l) (l)
784 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
785 uint32_t addrh
, uint32_t addrl
)
787 target_long addr
= GET_TLONG(addrh
, addrl
);
788 memory_dump(mon
, count
, format
, size
, addr
, 0);
791 #if TARGET_PHYS_ADDR_BITS > 32
792 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
794 #define GET_TPHYSADDR(h, l) (l)
797 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
798 int size
, uint32_t addrh
, uint32_t addrl
)
801 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
802 memory_dump(mon
, count
, format
, size
, addr
, 1);
805 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
806 unsigned int valh
, unsigned int vall
)
808 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
809 #if TARGET_PHYS_ADDR_BITS == 32
812 monitor_printf(mon
, "%#o", val
);
815 monitor_printf(mon
, "%#x", val
);
818 monitor_printf(mon
, "%u", val
);
822 monitor_printf(mon
, "%d", val
);
825 monitor_printc(mon
, val
);
831 monitor_printf(mon
, "%#" PRIo64
, val
);
834 monitor_printf(mon
, "%#" PRIx64
, val
);
837 monitor_printf(mon
, "%" PRIu64
, val
);
841 monitor_printf(mon
, "%" PRId64
, val
);
844 monitor_printc(mon
, val
);
848 monitor_printf(mon
, "\n");
851 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
852 uint32_t size
, const char *filename
)
855 target_long addr
= GET_TLONG(valh
, vall
);
864 f
= fopen(filename
, "wb");
866 monitor_printf(mon
, "could not open '%s'\n", filename
);
873 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
874 fwrite(buf
, 1, l
, f
);
881 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
882 unsigned int vall
, uint32_t size
,
883 const char *filename
)
888 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
890 f
= fopen(filename
, "wb");
892 monitor_printf(mon
, "could not open '%s'\n", filename
);
899 cpu_physical_memory_rw(addr
, buf
, l
, 0);
900 fwrite(buf
, 1, l
, f
);
908 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
915 for(addr
= start
; addr
< (start
+ size
); addr
++) {
916 cpu_physical_memory_rw(addr
, buf
, 1, 0);
917 /* BSD sum algorithm ('sum' Unix command) */
918 sum
= (sum
>> 1) | (sum
<< 15);
921 monitor_printf(mon
, "%05d\n", sum
);
929 static const KeyDef key_defs
[] = {
956 { 0x0e, "backspace" },
993 { 0x37, "asterisk" },
996 { 0x3a, "caps_lock" },
1007 { 0x45, "num_lock" },
1008 { 0x46, "scroll_lock" },
1010 { 0xb5, "kp_divide" },
1011 { 0x37, "kp_multiply" },
1012 { 0x4a, "kp_subtract" },
1014 { 0x9c, "kp_enter" },
1015 { 0x53, "kp_decimal" },
1048 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1063 { 0xfe, "compose" },
1068 static int get_keycode(const char *key
)
1074 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1075 if (!strcmp(key
, p
->name
))
1078 if (strstart(key
, "0x", NULL
)) {
1079 ret
= strtoul(key
, &endp
, 0);
1080 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1086 #define MAX_KEYCODES 16
1087 static uint8_t keycodes
[MAX_KEYCODES
];
1088 static int nb_pending_keycodes
;
1089 static QEMUTimer
*key_timer
;
1091 static void release_keys(void *opaque
)
1095 while (nb_pending_keycodes
> 0) {
1096 nb_pending_keycodes
--;
1097 keycode
= keycodes
[nb_pending_keycodes
];
1099 kbd_put_keycode(0xe0);
1100 kbd_put_keycode(keycode
| 0x80);
1104 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1107 char keyname_buf
[16];
1109 int keyname_len
, keycode
, i
;
1111 if (nb_pending_keycodes
> 0) {
1112 qemu_del_timer(key_timer
);
1119 separator
= strchr(string
, '-');
1120 keyname_len
= separator
? separator
- string
: strlen(string
);
1121 if (keyname_len
> 0) {
1122 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1123 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1124 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1127 if (i
== MAX_KEYCODES
) {
1128 monitor_printf(mon
, "too many keys\n");
1131 keyname_buf
[keyname_len
] = 0;
1132 keycode
= get_keycode(keyname_buf
);
1134 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1137 keycodes
[i
++] = keycode
;
1141 string
= separator
+ 1;
1143 nb_pending_keycodes
= i
;
1144 /* key down events */
1145 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1146 keycode
= keycodes
[i
];
1148 kbd_put_keycode(0xe0);
1149 kbd_put_keycode(keycode
& 0x7f);
1151 /* delayed key up events */
1152 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1153 muldiv64(ticks_per_sec
, hold_time
, 1000));
1156 static int mouse_button_state
;
1158 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1162 dx
= strtol(dx_str
, NULL
, 0);
1163 dy
= strtol(dy_str
, NULL
, 0);
1166 dz
= strtol(dz_str
, NULL
, 0);
1167 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1170 static void do_mouse_button(Monitor
*mon
, int button_state
)
1172 mouse_button_state
= button_state
;
1173 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1176 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1177 int addr
, int has_index
, int index
)
1183 cpu_outb(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1191 val
= cpu_inb(NULL
, addr
);
1195 val
= cpu_inw(NULL
, addr
);
1199 val
= cpu_inl(NULL
, addr
);
1203 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1204 suffix
, addr
, size
* 2, val
);
1207 static void do_ioport_write(Monitor
*mon
, int count
, int format
, int size
,
1210 addr
&= IOPORTS_MASK
;
1215 cpu_outb(NULL
, addr
, val
);
1218 cpu_outw(NULL
, addr
, val
);
1221 cpu_outl(NULL
, addr
, val
);
1226 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1230 res
= qemu_boot_set(bootdevice
);
1232 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1233 } else if (res
> 0) {
1234 monitor_printf(mon
, "setting boot device list failed\n");
1236 monitor_printf(mon
, "no function defined to set boot device list for "
1237 "this architecture\n");
1241 static void do_system_reset(Monitor
*mon
)
1243 qemu_system_reset_request();
1246 static void do_system_powerdown(Monitor
*mon
)
1248 qemu_system_powerdown_request();
1251 #if defined(TARGET_I386)
1252 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1254 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1257 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1258 pte
& PG_PSE_MASK
? 'P' : '-',
1259 pte
& PG_DIRTY_MASK
? 'D' : '-',
1260 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1261 pte
& PG_PCD_MASK
? 'C' : '-',
1262 pte
& PG_PWT_MASK
? 'T' : '-',
1263 pte
& PG_USER_MASK
? 'U' : '-',
1264 pte
& PG_RW_MASK
? 'W' : '-');
1267 static void tlb_info(Monitor
*mon
)
1271 uint32_t pgd
, pde
, pte
;
1273 env
= mon_get_cpu();
1277 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1278 monitor_printf(mon
, "PG disabled\n");
1281 pgd
= env
->cr
[3] & ~0xfff;
1282 for(l1
= 0; l1
< 1024; l1
++) {
1283 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1284 pde
= le32_to_cpu(pde
);
1285 if (pde
& PG_PRESENT_MASK
) {
1286 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1287 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1289 for(l2
= 0; l2
< 1024; l2
++) {
1290 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1291 (uint8_t *)&pte
, 4);
1292 pte
= le32_to_cpu(pte
);
1293 if (pte
& PG_PRESENT_MASK
) {
1294 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1304 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1305 uint32_t end
, int prot
)
1308 prot1
= *plast_prot
;
1309 if (prot
!= prot1
) {
1310 if (*pstart
!= -1) {
1311 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1312 *pstart
, end
, end
- *pstart
,
1313 prot1
& PG_USER_MASK
? 'u' : '-',
1315 prot1
& PG_RW_MASK
? 'w' : '-');
1325 static void mem_info(Monitor
*mon
)
1328 int l1
, l2
, prot
, last_prot
;
1329 uint32_t pgd
, pde
, pte
, start
, end
;
1331 env
= mon_get_cpu();
1335 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1336 monitor_printf(mon
, "PG disabled\n");
1339 pgd
= env
->cr
[3] & ~0xfff;
1342 for(l1
= 0; l1
< 1024; l1
++) {
1343 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1344 pde
= le32_to_cpu(pde
);
1346 if (pde
& PG_PRESENT_MASK
) {
1347 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1348 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1349 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1351 for(l2
= 0; l2
< 1024; l2
++) {
1352 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1353 (uint8_t *)&pte
, 4);
1354 pte
= le32_to_cpu(pte
);
1355 end
= (l1
<< 22) + (l2
<< 12);
1356 if (pte
& PG_PRESENT_MASK
) {
1357 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1361 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1366 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1372 #if defined(TARGET_SH4)
1374 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1376 monitor_printf(mon
, " tlb%i:\t"
1377 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1378 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1379 "dirty=%hhu writethrough=%hhu\n",
1381 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1382 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1386 static void tlb_info(Monitor
*mon
)
1388 CPUState
*env
= mon_get_cpu();
1391 monitor_printf (mon
, "ITLB:\n");
1392 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1393 print_tlb (mon
, i
, &env
->itlb
[i
]);
1394 monitor_printf (mon
, "UTLB:\n");
1395 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1396 print_tlb (mon
, i
, &env
->utlb
[i
]);
1401 static void do_info_kvm(Monitor
*mon
)
1404 monitor_printf(mon
, "kvm support: ");
1406 monitor_printf(mon
, "enabled\n");
1408 monitor_printf(mon
, "disabled\n");
1410 monitor_printf(mon
, "kvm support: not compiled\n");
1414 static void do_info_numa(Monitor
*mon
)
1419 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1420 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1421 monitor_printf(mon
, "node %d cpus:", i
);
1422 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1423 if (env
->numa_node
== i
) {
1424 monitor_printf(mon
, " %d", env
->cpu_index
);
1427 monitor_printf(mon
, "\n");
1428 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1433 #ifdef CONFIG_PROFILER
1435 static void do_info_profile(Monitor
*mon
)
1441 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1442 dev_time
, dev_time
/ (double)ticks_per_sec
);
1443 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1444 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1449 static void do_info_profile(Monitor
*mon
)
1451 monitor_printf(mon
, "Internal profiler not compiled\n");
1455 /* Capture support */
1456 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1458 static void do_info_capture(Monitor
*mon
)
1463 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1464 monitor_printf(mon
, "[%d]: ", i
);
1465 s
->ops
.info (s
->opaque
);
1470 static void do_stop_capture(Monitor
*mon
, int n
)
1475 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1477 s
->ops
.destroy (s
->opaque
);
1478 LIST_REMOVE (s
, entries
);
1485 static void do_wav_capture(Monitor
*mon
, const char *path
,
1486 int has_freq
, int freq
,
1487 int has_bits
, int bits
,
1488 int has_channels
, int nchannels
)
1492 s
= qemu_mallocz (sizeof (*s
));
1494 freq
= has_freq
? freq
: 44100;
1495 bits
= has_bits
? bits
: 16;
1496 nchannels
= has_channels
? nchannels
: 2;
1498 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1499 monitor_printf(mon
, "Faied to add wave capture\n");
1502 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1506 #if defined(TARGET_I386)
1507 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1511 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1512 if (env
->cpu_index
== cpu_index
) {
1513 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1519 static void do_info_status(Monitor
*mon
)
1523 monitor_printf(mon
, "VM status: running (single step mode)\n");
1525 monitor_printf(mon
, "VM status: running\n");
1528 monitor_printf(mon
, "VM status: paused\n");
1532 static void do_balloon(Monitor
*mon
, int value
)
1534 ram_addr_t target
= value
;
1535 qemu_balloon(target
<< 20);
1538 static void do_info_balloon(Monitor
*mon
)
1542 actual
= qemu_balloon_status();
1543 if (kvm_enabled() && !kvm_has_sync_mmu())
1544 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1545 "ballooning disabled\n");
1546 else if (actual
== 0)
1547 monitor_printf(mon
, "Ballooning not activated in VM\n");
1549 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1552 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1554 qemu_acl
*acl
= qemu_acl_find(name
);
1557 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1562 static void do_acl_show(Monitor
*mon
, const char *aclname
)
1564 qemu_acl
*acl
= find_acl(mon
, aclname
);
1565 qemu_acl_entry
*entry
;
1569 monitor_printf(mon
, "policy: %s\n",
1570 acl
->defaultDeny
? "deny" : "allow");
1571 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1573 monitor_printf(mon
, "%d: %s %s\n", i
,
1574 entry
->deny
? "deny" : "allow", entry
->match
);
1579 static void do_acl_reset(Monitor
*mon
, const char *aclname
)
1581 qemu_acl
*acl
= find_acl(mon
, aclname
);
1584 qemu_acl_reset(acl
);
1585 monitor_printf(mon
, "acl: removed all rules\n");
1589 static void do_acl_policy(Monitor
*mon
, const char *aclname
,
1592 qemu_acl
*acl
= find_acl(mon
, aclname
);
1595 if (strcmp(policy
, "allow") == 0) {
1596 acl
->defaultDeny
= 0;
1597 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1598 } else if (strcmp(policy
, "deny") == 0) {
1599 acl
->defaultDeny
= 1;
1600 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1602 monitor_printf(mon
, "acl: unknown policy '%s', "
1603 "expected 'deny' or 'allow'\n", policy
);
1608 static void do_acl_add(Monitor
*mon
, const char *aclname
,
1609 const char *match
, const char *policy
,
1610 int has_index
, int index
)
1612 qemu_acl
*acl
= find_acl(mon
, aclname
);
1616 if (strcmp(policy
, "allow") == 0) {
1618 } else if (strcmp(policy
, "deny") == 0) {
1621 monitor_printf(mon
, "acl: unknown policy '%s', "
1622 "expected 'deny' or 'allow'\n", policy
);
1626 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1628 ret
= qemu_acl_append(acl
, deny
, match
);
1630 monitor_printf(mon
, "acl: unable to add acl entry\n");
1632 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1636 static void do_acl_remove(Monitor
*mon
, const char *aclname
, const char *match
)
1638 qemu_acl
*acl
= find_acl(mon
, aclname
);
1642 ret
= qemu_acl_remove(acl
, match
);
1644 monitor_printf(mon
, "acl: no matching acl entry\n");
1646 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1650 #if defined(TARGET_I386)
1651 static void do_inject_mce(Monitor
*mon
,
1652 int cpu_index
, int bank
,
1653 unsigned status_hi
, unsigned status_lo
,
1654 unsigned mcg_status_hi
, unsigned mcg_status_lo
,
1655 unsigned addr_hi
, unsigned addr_lo
,
1656 unsigned misc_hi
, unsigned misc_lo
)
1659 uint64_t status
= ((uint64_t)status_hi
<< 32) | status_lo
;
1660 uint64_t mcg_status
= ((uint64_t)mcg_status_hi
<< 32) | mcg_status_lo
;
1661 uint64_t addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1662 uint64_t misc
= ((uint64_t)misc_hi
<< 32) | misc_lo
;
1664 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1665 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1666 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1672 static void do_getfd(Monitor
*mon
, const char *fdname
)
1677 fd
= qemu_chr_get_msgfd(mon
->chr
);
1679 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1683 if (qemu_isdigit(fdname
[0])) {
1684 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1690 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1695 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1696 if (strcmp(monfd
->name
, fdname
) != 0) {
1705 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1706 monfd
->name
= qemu_strdup(fdname
);
1709 LIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1712 static void do_closefd(Monitor
*mon
, const char *fdname
)
1716 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1717 if (strcmp(monfd
->name
, fdname
) != 0) {
1721 LIST_REMOVE(monfd
, next
);
1723 qemu_free(monfd
->name
);
1728 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1732 static void do_loadvm(Monitor
*mon
, const char *name
)
1734 int saved_vm_running
= vm_running
;
1738 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1742 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1746 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1749 if (strcmp(monfd
->name
, fdname
) != 0) {
1755 /* caller takes ownership of fd */
1756 LIST_REMOVE(monfd
, next
);
1757 qemu_free(monfd
->name
);
1766 static const mon_cmd_t mon_cmds
[] = {
1767 #include "qemu-monitor.h"
1771 /* Please update qemu-monitor.hx when adding or changing commands */
1772 static const mon_cmd_t info_cmds
[] = {
1773 { "version", "", do_info_version
,
1774 "", "show the version of QEMU" },
1775 { "network", "", do_info_network
,
1776 "", "show the network state" },
1777 { "chardev", "", qemu_chr_info
,
1778 "", "show the character devices" },
1779 { "block", "", bdrv_info
,
1780 "", "show the block devices" },
1781 { "blockstats", "", bdrv_info_stats
,
1782 "", "show block device statistics" },
1783 { "registers", "", do_info_registers
,
1784 "", "show the cpu registers" },
1785 { "cpus", "", do_info_cpus
,
1786 "", "show infos for each CPU" },
1787 { "history", "", do_info_history
,
1788 "", "show the command line history", },
1789 { "irq", "", irq_info
,
1790 "", "show the interrupts statistics (if available)", },
1791 { "pic", "", pic_info
,
1792 "", "show i8259 (PIC) state", },
1793 { "pci", "", pci_info
,
1794 "", "show PCI info", },
1795 #if defined(TARGET_I386) || defined(TARGET_SH4)
1796 { "tlb", "", tlb_info
,
1797 "", "show virtual to physical memory mappings", },
1799 #if defined(TARGET_I386)
1800 { "mem", "", mem_info
,
1801 "", "show the active virtual memory mappings", },
1802 { "hpet", "", do_info_hpet
,
1803 "", "show state of HPET", },
1805 { "jit", "", do_info_jit
,
1806 "", "show dynamic compiler info", },
1807 { "kvm", "", do_info_kvm
,
1808 "", "show KVM information", },
1809 { "numa", "", do_info_numa
,
1810 "", "show NUMA information", },
1811 { "usb", "", usb_info
,
1812 "", "show guest USB devices", },
1813 { "usbhost", "", usb_host_info
,
1814 "", "show host USB devices", },
1815 { "profile", "", do_info_profile
,
1816 "", "show profiling information", },
1817 { "capture", "", do_info_capture
,
1818 "", "show capture information" },
1819 { "snapshots", "", do_info_snapshots
,
1820 "", "show the currently saved VM snapshots" },
1821 { "status", "", do_info_status
,
1822 "", "show the current VM status (running|paused)" },
1823 { "pcmcia", "", pcmcia_info
,
1824 "", "show guest PCMCIA status" },
1825 { "mice", "", do_info_mice
,
1826 "", "show which guest mouse is receiving events" },
1827 { "vnc", "", do_info_vnc
,
1828 "", "show the vnc server status"},
1829 { "name", "", do_info_name
,
1830 "", "show the current VM name" },
1831 { "uuid", "", do_info_uuid
,
1832 "", "show the current VM UUID" },
1833 #if defined(TARGET_PPC)
1834 { "cpustats", "", do_info_cpu_stats
,
1835 "", "show CPU statistics", },
1837 #if defined(CONFIG_SLIRP)
1838 { "usernet", "", do_info_usernet
,
1839 "", "show user network stack connection states", },
1841 { "migrate", "", do_info_migrate
, "", "show migration status" },
1842 { "balloon", "", do_info_balloon
,
1843 "", "show balloon information" },
1844 { "qtree", "", do_info_qtree
,
1845 "", "show device tree" },
1846 { "qdm", "", do_info_qdm
,
1847 "", "show qdev device model list" },
1851 /*******************************************************************/
1853 static const char *pch
;
1854 static jmp_buf expr_env
;
1859 typedef struct MonitorDef
{
1862 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1866 #if defined(TARGET_I386)
1867 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1869 CPUState
*env
= mon_get_cpu();
1872 return env
->eip
+ env
->segs
[R_CS
].base
;
1876 #if defined(TARGET_PPC)
1877 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1879 CPUState
*env
= mon_get_cpu();
1887 for (i
= 0; i
< 8; i
++)
1888 u
|= env
->crf
[i
] << (32 - (4 * i
));
1893 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1895 CPUState
*env
= mon_get_cpu();
1901 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1903 CPUState
*env
= mon_get_cpu();
1909 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1911 CPUState
*env
= mon_get_cpu();
1914 return cpu_ppc_load_decr(env
);
1917 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1919 CPUState
*env
= mon_get_cpu();
1922 return cpu_ppc_load_tbu(env
);
1925 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1927 CPUState
*env
= mon_get_cpu();
1930 return cpu_ppc_load_tbl(env
);
1934 #if defined(TARGET_SPARC)
1935 #ifndef TARGET_SPARC64
1936 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1938 CPUState
*env
= mon_get_cpu();
1941 return GET_PSR(env
);
1945 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1947 CPUState
*env
= mon_get_cpu();
1950 return env
->regwptr
[val
];
1954 static const MonitorDef monitor_defs
[] = {
1957 #define SEG(name, seg) \
1958 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1959 { name ".base", offsetof(CPUState, segs[seg].base) },\
1960 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1962 { "eax", offsetof(CPUState
, regs
[0]) },
1963 { "ecx", offsetof(CPUState
, regs
[1]) },
1964 { "edx", offsetof(CPUState
, regs
[2]) },
1965 { "ebx", offsetof(CPUState
, regs
[3]) },
1966 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1967 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1968 { "esi", offsetof(CPUState
, regs
[6]) },
1969 { "edi", offsetof(CPUState
, regs
[7]) },
1970 #ifdef TARGET_X86_64
1971 { "r8", offsetof(CPUState
, regs
[8]) },
1972 { "r9", offsetof(CPUState
, regs
[9]) },
1973 { "r10", offsetof(CPUState
, regs
[10]) },
1974 { "r11", offsetof(CPUState
, regs
[11]) },
1975 { "r12", offsetof(CPUState
, regs
[12]) },
1976 { "r13", offsetof(CPUState
, regs
[13]) },
1977 { "r14", offsetof(CPUState
, regs
[14]) },
1978 { "r15", offsetof(CPUState
, regs
[15]) },
1980 { "eflags", offsetof(CPUState
, eflags
) },
1981 { "eip", offsetof(CPUState
, eip
) },
1988 { "pc", 0, monitor_get_pc
, },
1989 #elif defined(TARGET_PPC)
1990 /* General purpose registers */
1991 { "r0", offsetof(CPUState
, gpr
[0]) },
1992 { "r1", offsetof(CPUState
, gpr
[1]) },
1993 { "r2", offsetof(CPUState
, gpr
[2]) },
1994 { "r3", offsetof(CPUState
, gpr
[3]) },
1995 { "r4", offsetof(CPUState
, gpr
[4]) },
1996 { "r5", offsetof(CPUState
, gpr
[5]) },
1997 { "r6", offsetof(CPUState
, gpr
[6]) },
1998 { "r7", offsetof(CPUState
, gpr
[7]) },
1999 { "r8", offsetof(CPUState
, gpr
[8]) },
2000 { "r9", offsetof(CPUState
, gpr
[9]) },
2001 { "r10", offsetof(CPUState
, gpr
[10]) },
2002 { "r11", offsetof(CPUState
, gpr
[11]) },
2003 { "r12", offsetof(CPUState
, gpr
[12]) },
2004 { "r13", offsetof(CPUState
, gpr
[13]) },
2005 { "r14", offsetof(CPUState
, gpr
[14]) },
2006 { "r15", offsetof(CPUState
, gpr
[15]) },
2007 { "r16", offsetof(CPUState
, gpr
[16]) },
2008 { "r17", offsetof(CPUState
, gpr
[17]) },
2009 { "r18", offsetof(CPUState
, gpr
[18]) },
2010 { "r19", offsetof(CPUState
, gpr
[19]) },
2011 { "r20", offsetof(CPUState
, gpr
[20]) },
2012 { "r21", offsetof(CPUState
, gpr
[21]) },
2013 { "r22", offsetof(CPUState
, gpr
[22]) },
2014 { "r23", offsetof(CPUState
, gpr
[23]) },
2015 { "r24", offsetof(CPUState
, gpr
[24]) },
2016 { "r25", offsetof(CPUState
, gpr
[25]) },
2017 { "r26", offsetof(CPUState
, gpr
[26]) },
2018 { "r27", offsetof(CPUState
, gpr
[27]) },
2019 { "r28", offsetof(CPUState
, gpr
[28]) },
2020 { "r29", offsetof(CPUState
, gpr
[29]) },
2021 { "r30", offsetof(CPUState
, gpr
[30]) },
2022 { "r31", offsetof(CPUState
, gpr
[31]) },
2023 /* Floating point registers */
2024 { "f0", offsetof(CPUState
, fpr
[0]) },
2025 { "f1", offsetof(CPUState
, fpr
[1]) },
2026 { "f2", offsetof(CPUState
, fpr
[2]) },
2027 { "f3", offsetof(CPUState
, fpr
[3]) },
2028 { "f4", offsetof(CPUState
, fpr
[4]) },
2029 { "f5", offsetof(CPUState
, fpr
[5]) },
2030 { "f6", offsetof(CPUState
, fpr
[6]) },
2031 { "f7", offsetof(CPUState
, fpr
[7]) },
2032 { "f8", offsetof(CPUState
, fpr
[8]) },
2033 { "f9", offsetof(CPUState
, fpr
[9]) },
2034 { "f10", offsetof(CPUState
, fpr
[10]) },
2035 { "f11", offsetof(CPUState
, fpr
[11]) },
2036 { "f12", offsetof(CPUState
, fpr
[12]) },
2037 { "f13", offsetof(CPUState
, fpr
[13]) },
2038 { "f14", offsetof(CPUState
, fpr
[14]) },
2039 { "f15", offsetof(CPUState
, fpr
[15]) },
2040 { "f16", offsetof(CPUState
, fpr
[16]) },
2041 { "f17", offsetof(CPUState
, fpr
[17]) },
2042 { "f18", offsetof(CPUState
, fpr
[18]) },
2043 { "f19", offsetof(CPUState
, fpr
[19]) },
2044 { "f20", offsetof(CPUState
, fpr
[20]) },
2045 { "f21", offsetof(CPUState
, fpr
[21]) },
2046 { "f22", offsetof(CPUState
, fpr
[22]) },
2047 { "f23", offsetof(CPUState
, fpr
[23]) },
2048 { "f24", offsetof(CPUState
, fpr
[24]) },
2049 { "f25", offsetof(CPUState
, fpr
[25]) },
2050 { "f26", offsetof(CPUState
, fpr
[26]) },
2051 { "f27", offsetof(CPUState
, fpr
[27]) },
2052 { "f28", offsetof(CPUState
, fpr
[28]) },
2053 { "f29", offsetof(CPUState
, fpr
[29]) },
2054 { "f30", offsetof(CPUState
, fpr
[30]) },
2055 { "f31", offsetof(CPUState
, fpr
[31]) },
2056 { "fpscr", offsetof(CPUState
, fpscr
) },
2057 /* Next instruction pointer */
2058 { "nip|pc", offsetof(CPUState
, nip
) },
2059 { "lr", offsetof(CPUState
, lr
) },
2060 { "ctr", offsetof(CPUState
, ctr
) },
2061 { "decr", 0, &monitor_get_decr
, },
2062 { "ccr", 0, &monitor_get_ccr
, },
2063 /* Machine state register */
2064 { "msr", 0, &monitor_get_msr
, },
2065 { "xer", 0, &monitor_get_xer
, },
2066 { "tbu", 0, &monitor_get_tbu
, },
2067 { "tbl", 0, &monitor_get_tbl
, },
2068 #if defined(TARGET_PPC64)
2069 /* Address space register */
2070 { "asr", offsetof(CPUState
, asr
) },
2072 /* Segment registers */
2073 { "sdr1", offsetof(CPUState
, sdr1
) },
2074 { "sr0", offsetof(CPUState
, sr
[0]) },
2075 { "sr1", offsetof(CPUState
, sr
[1]) },
2076 { "sr2", offsetof(CPUState
, sr
[2]) },
2077 { "sr3", offsetof(CPUState
, sr
[3]) },
2078 { "sr4", offsetof(CPUState
, sr
[4]) },
2079 { "sr5", offsetof(CPUState
, sr
[5]) },
2080 { "sr6", offsetof(CPUState
, sr
[6]) },
2081 { "sr7", offsetof(CPUState
, sr
[7]) },
2082 { "sr8", offsetof(CPUState
, sr
[8]) },
2083 { "sr9", offsetof(CPUState
, sr
[9]) },
2084 { "sr10", offsetof(CPUState
, sr
[10]) },
2085 { "sr11", offsetof(CPUState
, sr
[11]) },
2086 { "sr12", offsetof(CPUState
, sr
[12]) },
2087 { "sr13", offsetof(CPUState
, sr
[13]) },
2088 { "sr14", offsetof(CPUState
, sr
[14]) },
2089 { "sr15", offsetof(CPUState
, sr
[15]) },
2090 /* Too lazy to put BATs and SPRs ... */
2091 #elif defined(TARGET_SPARC)
2092 { "g0", offsetof(CPUState
, gregs
[0]) },
2093 { "g1", offsetof(CPUState
, gregs
[1]) },
2094 { "g2", offsetof(CPUState
, gregs
[2]) },
2095 { "g3", offsetof(CPUState
, gregs
[3]) },
2096 { "g4", offsetof(CPUState
, gregs
[4]) },
2097 { "g5", offsetof(CPUState
, gregs
[5]) },
2098 { "g6", offsetof(CPUState
, gregs
[6]) },
2099 { "g7", offsetof(CPUState
, gregs
[7]) },
2100 { "o0", 0, monitor_get_reg
},
2101 { "o1", 1, monitor_get_reg
},
2102 { "o2", 2, monitor_get_reg
},
2103 { "o3", 3, monitor_get_reg
},
2104 { "o4", 4, monitor_get_reg
},
2105 { "o5", 5, monitor_get_reg
},
2106 { "o6", 6, monitor_get_reg
},
2107 { "o7", 7, monitor_get_reg
},
2108 { "l0", 8, monitor_get_reg
},
2109 { "l1", 9, monitor_get_reg
},
2110 { "l2", 10, monitor_get_reg
},
2111 { "l3", 11, monitor_get_reg
},
2112 { "l4", 12, monitor_get_reg
},
2113 { "l5", 13, monitor_get_reg
},
2114 { "l6", 14, monitor_get_reg
},
2115 { "l7", 15, monitor_get_reg
},
2116 { "i0", 16, monitor_get_reg
},
2117 { "i1", 17, monitor_get_reg
},
2118 { "i2", 18, monitor_get_reg
},
2119 { "i3", 19, monitor_get_reg
},
2120 { "i4", 20, monitor_get_reg
},
2121 { "i5", 21, monitor_get_reg
},
2122 { "i6", 22, monitor_get_reg
},
2123 { "i7", 23, monitor_get_reg
},
2124 { "pc", offsetof(CPUState
, pc
) },
2125 { "npc", offsetof(CPUState
, npc
) },
2126 { "y", offsetof(CPUState
, y
) },
2127 #ifndef TARGET_SPARC64
2128 { "psr", 0, &monitor_get_psr
, },
2129 { "wim", offsetof(CPUState
, wim
) },
2131 { "tbr", offsetof(CPUState
, tbr
) },
2132 { "fsr", offsetof(CPUState
, fsr
) },
2133 { "f0", offsetof(CPUState
, fpr
[0]) },
2134 { "f1", offsetof(CPUState
, fpr
[1]) },
2135 { "f2", offsetof(CPUState
, fpr
[2]) },
2136 { "f3", offsetof(CPUState
, fpr
[3]) },
2137 { "f4", offsetof(CPUState
, fpr
[4]) },
2138 { "f5", offsetof(CPUState
, fpr
[5]) },
2139 { "f6", offsetof(CPUState
, fpr
[6]) },
2140 { "f7", offsetof(CPUState
, fpr
[7]) },
2141 { "f8", offsetof(CPUState
, fpr
[8]) },
2142 { "f9", offsetof(CPUState
, fpr
[9]) },
2143 { "f10", offsetof(CPUState
, fpr
[10]) },
2144 { "f11", offsetof(CPUState
, fpr
[11]) },
2145 { "f12", offsetof(CPUState
, fpr
[12]) },
2146 { "f13", offsetof(CPUState
, fpr
[13]) },
2147 { "f14", offsetof(CPUState
, fpr
[14]) },
2148 { "f15", offsetof(CPUState
, fpr
[15]) },
2149 { "f16", offsetof(CPUState
, fpr
[16]) },
2150 { "f17", offsetof(CPUState
, fpr
[17]) },
2151 { "f18", offsetof(CPUState
, fpr
[18]) },
2152 { "f19", offsetof(CPUState
, fpr
[19]) },
2153 { "f20", offsetof(CPUState
, fpr
[20]) },
2154 { "f21", offsetof(CPUState
, fpr
[21]) },
2155 { "f22", offsetof(CPUState
, fpr
[22]) },
2156 { "f23", offsetof(CPUState
, fpr
[23]) },
2157 { "f24", offsetof(CPUState
, fpr
[24]) },
2158 { "f25", offsetof(CPUState
, fpr
[25]) },
2159 { "f26", offsetof(CPUState
, fpr
[26]) },
2160 { "f27", offsetof(CPUState
, fpr
[27]) },
2161 { "f28", offsetof(CPUState
, fpr
[28]) },
2162 { "f29", offsetof(CPUState
, fpr
[29]) },
2163 { "f30", offsetof(CPUState
, fpr
[30]) },
2164 { "f31", offsetof(CPUState
, fpr
[31]) },
2165 #ifdef TARGET_SPARC64
2166 { "f32", offsetof(CPUState
, fpr
[32]) },
2167 { "f34", offsetof(CPUState
, fpr
[34]) },
2168 { "f36", offsetof(CPUState
, fpr
[36]) },
2169 { "f38", offsetof(CPUState
, fpr
[38]) },
2170 { "f40", offsetof(CPUState
, fpr
[40]) },
2171 { "f42", offsetof(CPUState
, fpr
[42]) },
2172 { "f44", offsetof(CPUState
, fpr
[44]) },
2173 { "f46", offsetof(CPUState
, fpr
[46]) },
2174 { "f48", offsetof(CPUState
, fpr
[48]) },
2175 { "f50", offsetof(CPUState
, fpr
[50]) },
2176 { "f52", offsetof(CPUState
, fpr
[52]) },
2177 { "f54", offsetof(CPUState
, fpr
[54]) },
2178 { "f56", offsetof(CPUState
, fpr
[56]) },
2179 { "f58", offsetof(CPUState
, fpr
[58]) },
2180 { "f60", offsetof(CPUState
, fpr
[60]) },
2181 { "f62", offsetof(CPUState
, fpr
[62]) },
2182 { "asi", offsetof(CPUState
, asi
) },
2183 { "pstate", offsetof(CPUState
, pstate
) },
2184 { "cansave", offsetof(CPUState
, cansave
) },
2185 { "canrestore", offsetof(CPUState
, canrestore
) },
2186 { "otherwin", offsetof(CPUState
, otherwin
) },
2187 { "wstate", offsetof(CPUState
, wstate
) },
2188 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2189 { "fprs", offsetof(CPUState
, fprs
) },
2195 static void expr_error(Monitor
*mon
, const char *msg
)
2197 monitor_printf(mon
, "%s\n", msg
);
2198 longjmp(expr_env
, 1);
2201 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2202 static int get_monitor_def(target_long
*pval
, const char *name
)
2204 const MonitorDef
*md
;
2207 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2208 if (compare_cmd(name
, md
->name
)) {
2209 if (md
->get_value
) {
2210 *pval
= md
->get_value(md
, md
->offset
);
2212 CPUState
*env
= mon_get_cpu();
2215 ptr
= (uint8_t *)env
+ md
->offset
;
2218 *pval
= *(int32_t *)ptr
;
2221 *pval
= *(target_long
*)ptr
;
2234 static void next(void)
2238 while (qemu_isspace(*pch
))
2243 static int64_t expr_sum(Monitor
*mon
);
2245 static int64_t expr_unary(Monitor
*mon
)
2254 n
= expr_unary(mon
);
2258 n
= -expr_unary(mon
);
2262 n
= ~expr_unary(mon
);
2268 expr_error(mon
, "')' expected");
2275 expr_error(mon
, "character constant expected");
2279 expr_error(mon
, "missing terminating \' character");
2289 while ((*pch
>= 'a' && *pch
<= 'z') ||
2290 (*pch
>= 'A' && *pch
<= 'Z') ||
2291 (*pch
>= '0' && *pch
<= '9') ||
2292 *pch
== '_' || *pch
== '.') {
2293 if ((q
- buf
) < sizeof(buf
) - 1)
2297 while (qemu_isspace(*pch
))
2300 ret
= get_monitor_def(®
, buf
);
2302 expr_error(mon
, "unknown register");
2304 expr_error(mon
, "no cpu defined");
2309 expr_error(mon
, "unexpected end of expression");
2313 #if TARGET_PHYS_ADDR_BITS > 32
2314 n
= strtoull(pch
, &p
, 0);
2316 n
= strtoul(pch
, &p
, 0);
2319 expr_error(mon
, "invalid char in expression");
2322 while (qemu_isspace(*pch
))
2330 static int64_t expr_prod(Monitor
*mon
)
2335 val
= expr_unary(mon
);
2338 if (op
!= '*' && op
!= '/' && op
!= '%')
2341 val2
= expr_unary(mon
);
2350 expr_error(mon
, "division by zero");
2361 static int64_t expr_logic(Monitor
*mon
)
2366 val
= expr_prod(mon
);
2369 if (op
!= '&' && op
!= '|' && op
!= '^')
2372 val2
= expr_prod(mon
);
2389 static int64_t expr_sum(Monitor
*mon
)
2394 val
= expr_logic(mon
);
2397 if (op
!= '+' && op
!= '-')
2400 val2
= expr_logic(mon
);
2409 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2412 if (setjmp(expr_env
)) {
2416 while (qemu_isspace(*pch
))
2418 *pval
= expr_sum(mon
);
2423 static int get_str(char *buf
, int buf_size
, const char **pp
)
2431 while (qemu_isspace(*p
))
2441 while (*p
!= '\0' && *p
!= '\"') {
2457 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2460 if ((q
- buf
) < buf_size
- 1) {
2464 if ((q
- buf
) < buf_size
- 1) {
2471 qemu_printf("unterminated string\n");
2476 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2477 if ((q
- buf
) < buf_size
- 1) {
2489 * Store the command-name in cmdname, and return a pointer to
2490 * the remaining of the command string.
2492 static const char *get_command_name(const char *cmdline
,
2493 char *cmdname
, size_t nlen
)
2496 const char *p
, *pstart
;
2499 while (qemu_isspace(*p
))
2504 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2509 memcpy(cmdname
, pstart
, len
);
2510 cmdname
[len
] = '\0';
2515 * Read key of 'type' into 'key' and return the current
2518 static char *key_get_info(const char *type
, char **key
)
2526 p
= strchr(type
, ':');
2533 str
= qemu_malloc(len
+ 1);
2534 memcpy(str
, type
, len
);
2541 static int default_fmt_format
= 'x';
2542 static int default_fmt_size
= 4;
2546 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2548 const char *p
, *typestr
;
2549 int c
, nb_args
, i
, has_arg
;
2550 const mon_cmd_t
*cmd
;
2555 void *str_allocated
[MAX_ARGS
];
2556 void *args
[MAX_ARGS
];
2557 void (*handler_0
)(Monitor
*mon
);
2558 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2559 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2560 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2561 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2563 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2564 void *arg3
, void *arg4
);
2565 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2566 void *arg3
, void *arg4
, void *arg5
);
2567 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2568 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2569 void (*handler_8
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2570 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2572 void (*handler_9
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2573 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2574 void *arg7
, void *arg8
);
2575 void (*handler_10
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2576 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2577 void *arg7
, void *arg8
, void *arg9
);
2580 monitor_printf(mon
, "command='%s'\n", cmdline
);
2583 /* extract the command name */
2584 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2588 /* find the command */
2589 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2590 if (compare_cmd(cmdname
, cmd
->name
))
2594 if (cmd
->name
== NULL
) {
2595 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2599 qdict
= qdict_new();
2601 for(i
= 0; i
< MAX_ARGS
; i
++)
2602 str_allocated
[i
] = NULL
;
2604 /* parse the parameters */
2605 typestr
= cmd
->args_type
;
2608 typestr
= key_get_info(typestr
, &key
);
2621 while (qemu_isspace(*p
))
2623 if (*typestr
== '?') {
2626 /* no optional string: NULL argument */
2631 ret
= get_str(buf
, sizeof(buf
), &p
);
2635 monitor_printf(mon
, "%s: filename expected\n",
2639 monitor_printf(mon
, "%s: block device name expected\n",
2643 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2648 str
= qemu_malloc(strlen(buf
) + 1);
2649 pstrcpy(str
, sizeof(buf
), buf
);
2650 str_allocated
[nb_args
] = str
;
2652 if (nb_args
>= MAX_ARGS
) {
2654 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2657 args
[nb_args
++] = str
;
2659 qdict_put(qdict
, key
, qstring_from_str(str
));
2664 int count
, format
, size
;
2666 while (qemu_isspace(*p
))
2672 if (qemu_isdigit(*p
)) {
2674 while (qemu_isdigit(*p
)) {
2675 count
= count
* 10 + (*p
- '0');
2713 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2714 monitor_printf(mon
, "invalid char in format: '%c'\n",
2719 format
= default_fmt_format
;
2720 if (format
!= 'i') {
2721 /* for 'i', not specifying a size gives -1 as size */
2723 size
= default_fmt_size
;
2724 default_fmt_size
= size
;
2726 default_fmt_format
= format
;
2729 format
= default_fmt_format
;
2730 if (format
!= 'i') {
2731 size
= default_fmt_size
;
2736 if (nb_args
+ 3 > MAX_ARGS
)
2738 args
[nb_args
++] = (void*)(long)count
;
2739 args
[nb_args
++] = (void*)(long)format
;
2740 args
[nb_args
++] = (void*)(long)size
;
2741 qdict_put(qdict
, "count", qint_from_int(count
));
2742 qdict_put(qdict
, "format", qint_from_int(format
));
2743 qdict_put(qdict
, "size", qint_from_int(size
));
2752 while (qemu_isspace(*p
))
2754 if (*typestr
== '?' || *typestr
== '.') {
2755 if (*typestr
== '?') {
2763 while (qemu_isspace(*p
))
2771 if (nb_args
>= MAX_ARGS
)
2774 args
[nb_args
++] = (void *)(long)has_arg
;
2776 if (nb_args
>= MAX_ARGS
)
2782 if (get_expr(mon
, &val
, &p
))
2786 if (nb_args
>= MAX_ARGS
)
2788 args
[nb_args
++] = (void *)(long)val
;
2790 qdict_put(qdict
, key
, qint_from_int(val
));
2792 if ((nb_args
+ 1) >= MAX_ARGS
)
2794 #if TARGET_PHYS_ADDR_BITS > 32
2795 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2797 args
[nb_args
++] = (void *)0;
2799 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2800 qdict_put(qdict
, key
, qint_from_int(val
));
2812 while (qemu_isspace(*p
))
2818 monitor_printf(mon
, "%s: unsupported option -%c\n",
2825 if (nb_args
>= MAX_ARGS
)
2827 args
[nb_args
++] = (void *)(long)has_option
;
2828 qdict_put(qdict
, key
, qint_from_int(has_option
));
2833 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2839 /* check that all arguments were parsed */
2840 while (qemu_isspace(*p
))
2843 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2848 qemu_errors_to_mon(mon
);
2851 handler_0
= cmd
->handler
;
2855 handler_1
= cmd
->handler
;
2856 handler_1(mon
, args
[0]);
2859 handler_2
= cmd
->handler
;
2860 handler_2(mon
, args
[0], args
[1]);
2863 handler_3
= cmd
->handler
;
2864 handler_3(mon
, args
[0], args
[1], args
[2]);
2867 handler_4
= cmd
->handler
;
2868 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2871 handler_5
= cmd
->handler
;
2872 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2875 handler_6
= cmd
->handler
;
2876 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2879 handler_7
= cmd
->handler
;
2880 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2884 handler_8
= cmd
->handler
;
2885 handler_8(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2889 handler_9
= cmd
->handler
;
2890 handler_9(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2891 args
[6], args
[7], args
[8]);
2894 handler_10
= cmd
->handler
;
2895 handler_10(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2896 args
[6], args
[7], args
[8], args
[9]);
2899 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2902 qemu_errors_to_previous();
2906 for(i
= 0; i
< MAX_ARGS
; i
++)
2907 qemu_free(str_allocated
[i
]);
2911 static void cmd_completion(const char *name
, const char *list
)
2913 const char *p
, *pstart
;
2922 p
= pstart
+ strlen(pstart
);
2924 if (len
> sizeof(cmd
) - 2)
2925 len
= sizeof(cmd
) - 2;
2926 memcpy(cmd
, pstart
, len
);
2928 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2929 readline_add_completion(cur_mon
->rs
, cmd
);
2937 static void file_completion(const char *input
)
2942 char file
[1024], file_prefix
[1024];
2946 p
= strrchr(input
, '/');
2949 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2950 pstrcpy(path
, sizeof(path
), ".");
2952 input_path_len
= p
- input
+ 1;
2953 memcpy(path
, input
, input_path_len
);
2954 if (input_path_len
> sizeof(path
) - 1)
2955 input_path_len
= sizeof(path
) - 1;
2956 path
[input_path_len
] = '\0';
2957 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2959 #ifdef DEBUG_COMPLETION
2960 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2961 input
, path
, file_prefix
);
2963 ffs
= opendir(path
);
2971 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2972 memcpy(file
, input
, input_path_len
);
2973 if (input_path_len
< sizeof(file
))
2974 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2976 /* stat the file to find out if it's a directory.
2977 * In that case add a slash to speed up typing long paths
2980 if(S_ISDIR(sb
.st_mode
))
2981 pstrcat(file
, sizeof(file
), "/");
2982 readline_add_completion(cur_mon
->rs
, file
);
2988 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2990 const char *name
= bdrv_get_device_name(bs
);
2991 const char *input
= opaque
;
2993 if (input
[0] == '\0' ||
2994 !strncmp(name
, (char *)input
, strlen(input
))) {
2995 readline_add_completion(cur_mon
->rs
, name
);
2999 /* NOTE: this parser is an approximate form of the real command parser */
3000 static void parse_cmdline(const char *cmdline
,
3001 int *pnb_args
, char **args
)
3010 while (qemu_isspace(*p
))
3014 if (nb_args
>= MAX_ARGS
)
3016 ret
= get_str(buf
, sizeof(buf
), &p
);
3017 args
[nb_args
] = qemu_strdup(buf
);
3022 *pnb_args
= nb_args
;
3025 static const char *next_arg_type(const char *typestr
)
3027 const char *p
= strchr(typestr
, ':');
3028 return (p
!= NULL
? ++p
: typestr
);
3031 static void monitor_find_completion(const char *cmdline
)
3033 const char *cmdname
;
3034 char *args
[MAX_ARGS
];
3035 int nb_args
, i
, len
;
3036 const char *ptype
, *str
;
3037 const mon_cmd_t
*cmd
;
3040 parse_cmdline(cmdline
, &nb_args
, args
);
3041 #ifdef DEBUG_COMPLETION
3042 for(i
= 0; i
< nb_args
; i
++) {
3043 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3047 /* if the line ends with a space, it means we want to complete the
3049 len
= strlen(cmdline
);
3050 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3051 if (nb_args
>= MAX_ARGS
)
3053 args
[nb_args
++] = qemu_strdup("");
3056 /* command completion */
3061 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3062 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3063 cmd_completion(cmdname
, cmd
->name
);
3066 /* find the command */
3067 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3068 if (compare_cmd(args
[0], cmd
->name
))
3073 ptype
= next_arg_type(cmd
->args_type
);
3074 for(i
= 0; i
< nb_args
- 2; i
++) {
3075 if (*ptype
!= '\0') {
3076 ptype
= next_arg_type(ptype
);
3077 while (*ptype
== '?')
3078 ptype
= next_arg_type(ptype
);
3081 str
= args
[nb_args
- 1];
3082 if (*ptype
== '-' && ptype
[1] != '\0') {
3087 /* file completion */
3088 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3089 file_completion(str
);
3092 /* block device name completion */
3093 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3094 bdrv_iterate(block_completion_it
, (void *)str
);
3097 /* XXX: more generic ? */
3098 if (!strcmp(cmd
->name
, "info")) {
3099 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3100 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3101 cmd_completion(str
, cmd
->name
);
3103 } else if (!strcmp(cmd
->name
, "sendkey")) {
3104 char *sep
= strrchr(str
, '-');
3107 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3108 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3109 cmd_completion(str
, key
->name
);
3111 } else if (!strcmp(cmd
->name
, "help|?")) {
3112 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3113 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3114 cmd_completion(str
, cmd
->name
);
3122 for(i
= 0; i
< nb_args
; i
++)
3126 static int monitor_can_read(void *opaque
)
3128 Monitor
*mon
= opaque
;
3130 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3133 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3135 Monitor
*old_mon
= cur_mon
;
3141 for (i
= 0; i
< size
; i
++)
3142 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3144 if (size
== 0 || buf
[size
- 1] != 0)
3145 monitor_printf(cur_mon
, "corrupted command\n");
3147 monitor_handle_command(cur_mon
, (char *)buf
);
3153 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3155 monitor_suspend(mon
);
3156 monitor_handle_command(mon
, cmdline
);
3157 monitor_resume(mon
);
3160 int monitor_suspend(Monitor
*mon
)
3168 void monitor_resume(Monitor
*mon
)
3172 if (--mon
->suspend_cnt
== 0)
3173 readline_show_prompt(mon
->rs
);
3176 static void monitor_event(void *opaque
, int event
)
3178 Monitor
*mon
= opaque
;
3181 case CHR_EVENT_MUX_IN
:
3182 readline_restart(mon
->rs
);
3183 monitor_resume(mon
);
3187 case CHR_EVENT_MUX_OUT
:
3188 if (mon
->suspend_cnt
== 0)
3189 monitor_printf(mon
, "\n");
3191 monitor_suspend(mon
);
3194 case CHR_EVENT_RESET
:
3195 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3196 "information\n", QEMU_VERSION
);
3197 if (mon
->chr
->focus
== 0)
3198 readline_show_prompt(mon
->rs
);
3212 void monitor_init(CharDriverState
*chr
, int flags
)
3214 static int is_first_init
= 1;
3217 if (is_first_init
) {
3218 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3222 mon
= qemu_mallocz(sizeof(*mon
));
3226 if (mon
->chr
->focus
!= 0)
3227 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3228 if (flags
& MONITOR_USE_READLINE
) {
3229 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3230 monitor_read_command(mon
, 0);
3233 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3236 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3237 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3241 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3243 BlockDriverState
*bs
= opaque
;
3246 if (bdrv_set_key(bs
, password
) != 0) {
3247 monitor_printf(mon
, "invalid password\n");
3250 if (mon
->password_completion_cb
)
3251 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3253 monitor_read_command(mon
, 1);
3256 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3257 BlockDriverCompletionFunc
*completion_cb
,
3262 if (!bdrv_key_required(bs
)) {
3264 completion_cb(opaque
, 0);
3268 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3269 bdrv_get_encrypted_filename(bs
));
3271 mon
->password_completion_cb
= completion_cb
;
3272 mon
->password_opaque
= opaque
;
3274 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3276 if (err
&& completion_cb
)
3277 completion_cb(opaque
, err
);
3280 typedef struct QemuErrorSink QemuErrorSink
;
3281 struct QemuErrorSink
{
3290 QemuErrorSink
*previous
;
3293 static QemuErrorSink
*qemu_error_sink
;
3295 void qemu_errors_to_file(FILE *fp
)
3297 QemuErrorSink
*sink
;
3299 sink
= qemu_mallocz(sizeof(*sink
));
3300 sink
->dest
= ERR_SINK_FILE
;
3302 sink
->previous
= qemu_error_sink
;
3303 qemu_error_sink
= sink
;
3306 void qemu_errors_to_mon(Monitor
*mon
)
3308 QemuErrorSink
*sink
;
3310 sink
= qemu_mallocz(sizeof(*sink
));
3311 sink
->dest
= ERR_SINK_MONITOR
;
3313 sink
->previous
= qemu_error_sink
;
3314 qemu_error_sink
= sink
;
3317 void qemu_errors_to_previous(void)
3319 QemuErrorSink
*sink
;
3321 assert(qemu_error_sink
!= NULL
);
3322 sink
= qemu_error_sink
;
3323 qemu_error_sink
= sink
->previous
;
3327 void qemu_error(const char *fmt
, ...)
3331 assert(qemu_error_sink
!= NULL
);
3332 switch (qemu_error_sink
->dest
) {
3334 va_start(args
, fmt
);
3335 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3338 case ERR_SINK_MONITOR
:
3339 va_start(args
, fmt
);
3340 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);