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_commit(Monitor
*mon
, const char *device
)
261 all_devices
= !strcmp(device
, "all");
262 for (i
= 0; i
< nb_drives
; i
++) {
264 if (strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
266 bdrv_commit(drives_table
[i
].bdrv
);
270 static void do_info(Monitor
*mon
, const char *item
)
272 const mon_cmd_t
*cmd
;
273 void (*handler
)(Monitor
*);
277 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
278 if (compare_cmd(item
, cmd
->name
))
282 help_cmd(mon
, "info");
285 handler
= cmd
->handler
;
289 static void do_info_version(Monitor
*mon
)
291 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
294 static void do_info_name(Monitor
*mon
)
297 monitor_printf(mon
, "%s\n", qemu_name
);
300 #if defined(TARGET_I386)
301 static void do_info_hpet(Monitor
*mon
)
303 monitor_printf(mon
, "HPET is %s by QEMU\n",
304 (no_hpet
) ? "disabled" : "enabled");
308 static void do_info_uuid(Monitor
*mon
)
310 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
311 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
312 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
313 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
314 qemu_uuid
[14], qemu_uuid
[15]);
317 /* get the current CPU defined by the user */
318 static int mon_set_cpu(int cpu_index
)
322 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
323 if (env
->cpu_index
== cpu_index
) {
324 cur_mon
->mon_cpu
= env
;
331 static CPUState
*mon_get_cpu(void)
333 if (!cur_mon
->mon_cpu
) {
336 cpu_synchronize_state(cur_mon
->mon_cpu
, 0);
337 return cur_mon
->mon_cpu
;
340 static void do_info_registers(Monitor
*mon
)
347 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
350 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
355 static void do_info_cpus(Monitor
*mon
)
359 /* just to set the default cpu if not already done */
362 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
363 cpu_synchronize_state(env
, 0);
364 monitor_printf(mon
, "%c CPU #%d:",
365 (env
== mon
->mon_cpu
) ? '*' : ' ',
367 #if defined(TARGET_I386)
368 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
369 env
->eip
+ env
->segs
[R_CS
].base
);
370 #elif defined(TARGET_PPC)
371 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
372 #elif defined(TARGET_SPARC)
373 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
375 #elif defined(TARGET_MIPS)
376 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
379 monitor_printf(mon
, " (halted)");
380 monitor_printf(mon
," thread_id=%d", env
->thread_id
);
381 monitor_printf(mon
, "\n");
385 static void do_cpu_set(Monitor
*mon
, int index
)
387 if (mon_set_cpu(index
) < 0)
388 monitor_printf(mon
, "Invalid CPU index\n");
391 static void do_cpu_set_nr(Monitor
*mon
, int value
, const char *status
)
395 if (!strcmp(status
, "online"))
397 else if (!strcmp(status
, "offline"))
400 monitor_printf(mon
, "invalid status: %s\n", status
);
403 #if defined(TARGET_I386) || defined(TARGET_X86_64)
404 qemu_system_cpu_hot_add(value
, state
);
408 static void do_info_jit(Monitor
*mon
)
410 dump_exec_info((FILE *)mon
, monitor_fprintf
);
413 static void do_info_history(Monitor
*mon
)
422 str
= readline_get_history(mon
->rs
, i
);
425 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
430 #if defined(TARGET_PPC)
431 /* XXX: not implemented in other targets */
432 static void do_info_cpu_stats(Monitor
*mon
)
437 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
441 static void do_quit(Monitor
*mon
)
446 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
448 if (bdrv_is_inserted(bs
)) {
450 if (!bdrv_is_removable(bs
)) {
451 monitor_printf(mon
, "device is not removable\n");
454 if (bdrv_is_locked(bs
)) {
455 monitor_printf(mon
, "device is locked\n");
464 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
466 BlockDriverState
*bs
;
468 bs
= bdrv_find(filename
);
470 monitor_printf(mon
, "device not found\n");
473 eject_device(mon
, bs
, force
);
476 static void do_change_block(Monitor
*mon
, const char *device
,
477 const char *filename
, const char *fmt
)
479 BlockDriverState
*bs
;
480 BlockDriver
*drv
= NULL
;
482 bs
= bdrv_find(device
);
484 monitor_printf(mon
, "device not found\n");
488 drv
= bdrv_find_format(fmt
);
490 monitor_printf(mon
, "invalid format %s\n", fmt
);
494 if (eject_device(mon
, bs
, 0) < 0)
496 bdrv_open2(bs
, filename
, 0, drv
);
497 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
500 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
503 if (vnc_display_password(NULL
, password
) < 0)
504 monitor_printf(mon
, "could not set VNC server password\n");
506 monitor_read_command(mon
, 1);
509 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
511 if (strcmp(target
, "passwd") == 0 ||
512 strcmp(target
, "password") == 0) {
515 strncpy(password
, arg
, sizeof(password
));
516 password
[sizeof(password
) - 1] = '\0';
517 change_vnc_password_cb(mon
, password
, NULL
);
519 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
522 if (vnc_display_open(NULL
, target
) < 0)
523 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
527 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
530 if (strcmp(device
, "vnc") == 0) {
531 do_change_vnc(mon
, target
, arg
);
533 do_change_block(mon
, device
, target
, arg
);
537 static void do_screen_dump(Monitor
*mon
, const char *filename
)
539 vga_hw_screen_dump(filename
);
542 static void do_logfile(Monitor
*mon
, const char *filename
)
544 cpu_set_log_filename(filename
);
547 static void do_log(Monitor
*mon
, const char *items
)
551 if (!strcmp(items
, "none")) {
554 mask
= cpu_str_to_log_mask(items
);
556 help_cmd(mon
, "log");
563 static void do_singlestep(Monitor
*mon
, const char *option
)
565 if (!option
|| !strcmp(option
, "on")) {
567 } else if (!strcmp(option
, "off")) {
570 monitor_printf(mon
, "unexpected option %s\n", option
);
574 static void do_stop(Monitor
*mon
)
576 vm_stop(EXCP_INTERRUPT
);
579 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
581 struct bdrv_iterate_context
{
586 static void do_cont(Monitor
*mon
)
588 struct bdrv_iterate_context context
= { mon
, 0 };
590 bdrv_iterate(encrypted_bdrv_it
, &context
);
591 /* only resume the vm if all keys are set and valid */
596 static void bdrv_key_cb(void *opaque
, int err
)
598 Monitor
*mon
= opaque
;
600 /* another key was set successfully, retry to continue */
605 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
607 struct bdrv_iterate_context
*context
= opaque
;
609 if (!context
->err
&& bdrv_key_required(bs
)) {
610 context
->err
= -EBUSY
;
611 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
616 static void do_gdbserver(Monitor
*mon
, const char *device
)
619 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
620 if (gdbserver_start(device
) < 0) {
621 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
623 } else if (strcmp(device
, "none") == 0) {
624 monitor_printf(mon
, "Disabled gdbserver\n");
626 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
631 static void do_watchdog_action(Monitor
*mon
, const char *action
)
633 if (select_watchdog_action(action
) == -1) {
634 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
638 static void monitor_printc(Monitor
*mon
, int c
)
640 monitor_printf(mon
, "'");
643 monitor_printf(mon
, "\\'");
646 monitor_printf(mon
, "\\\\");
649 monitor_printf(mon
, "\\n");
652 monitor_printf(mon
, "\\r");
655 if (c
>= 32 && c
<= 126) {
656 monitor_printf(mon
, "%c", c
);
658 monitor_printf(mon
, "\\x%02x", c
);
662 monitor_printf(mon
, "'");
665 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
666 target_phys_addr_t addr
, int is_physical
)
669 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
677 if (!env
&& !is_physical
)
682 } else if (wsize
== 4) {
685 /* as default we use the current CS size */
689 if ((env
->efer
& MSR_EFER_LMA
) &&
690 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
694 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
699 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
708 nb_per_line
= line_size
/ wsize
;
713 max_digits
= (wsize
* 8 + 2) / 3;
717 max_digits
= (wsize
* 8) / 4;
721 max_digits
= (wsize
* 8 * 10 + 32) / 33;
730 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
732 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
737 cpu_physical_memory_rw(addr
, buf
, l
, 0);
742 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
743 monitor_printf(mon
, " Cannot access memory\n");
752 v
= ldub_raw(buf
+ i
);
755 v
= lduw_raw(buf
+ i
);
758 v
= (uint32_t)ldl_raw(buf
+ i
);
761 v
= ldq_raw(buf
+ i
);
764 monitor_printf(mon
, " ");
767 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
770 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
773 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
776 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
779 monitor_printc(mon
, v
);
784 monitor_printf(mon
, "\n");
790 #if TARGET_LONG_BITS == 64
791 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
793 #define GET_TLONG(h, l) (l)
796 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
797 uint32_t addrh
, uint32_t addrl
)
799 target_long addr
= GET_TLONG(addrh
, addrl
);
800 memory_dump(mon
, count
, format
, size
, addr
, 0);
803 #if TARGET_PHYS_ADDR_BITS > 32
804 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
806 #define GET_TPHYSADDR(h, l) (l)
809 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
810 int size
, uint32_t addrh
, uint32_t addrl
)
813 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
814 memory_dump(mon
, count
, format
, size
, addr
, 1);
817 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
818 unsigned int valh
, unsigned int vall
)
820 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
821 #if TARGET_PHYS_ADDR_BITS == 32
824 monitor_printf(mon
, "%#o", val
);
827 monitor_printf(mon
, "%#x", val
);
830 monitor_printf(mon
, "%u", val
);
834 monitor_printf(mon
, "%d", val
);
837 monitor_printc(mon
, val
);
843 monitor_printf(mon
, "%#" PRIo64
, val
);
846 monitor_printf(mon
, "%#" PRIx64
, val
);
849 monitor_printf(mon
, "%" PRIu64
, val
);
853 monitor_printf(mon
, "%" PRId64
, val
);
856 monitor_printc(mon
, val
);
860 monitor_printf(mon
, "\n");
863 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
864 uint32_t size
, const char *filename
)
867 target_long addr
= GET_TLONG(valh
, vall
);
876 f
= fopen(filename
, "wb");
878 monitor_printf(mon
, "could not open '%s'\n", filename
);
885 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
886 fwrite(buf
, 1, l
, f
);
893 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
894 unsigned int vall
, uint32_t size
,
895 const char *filename
)
900 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
902 f
= fopen(filename
, "wb");
904 monitor_printf(mon
, "could not open '%s'\n", filename
);
911 cpu_physical_memory_rw(addr
, buf
, l
, 0);
912 fwrite(buf
, 1, l
, f
);
920 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
927 for(addr
= start
; addr
< (start
+ size
); addr
++) {
928 cpu_physical_memory_rw(addr
, buf
, 1, 0);
929 /* BSD sum algorithm ('sum' Unix command) */
930 sum
= (sum
>> 1) | (sum
<< 15);
933 monitor_printf(mon
, "%05d\n", sum
);
941 static const KeyDef key_defs
[] = {
968 { 0x0e, "backspace" },
1005 { 0x37, "asterisk" },
1008 { 0x3a, "caps_lock" },
1019 { 0x45, "num_lock" },
1020 { 0x46, "scroll_lock" },
1022 { 0xb5, "kp_divide" },
1023 { 0x37, "kp_multiply" },
1024 { 0x4a, "kp_subtract" },
1026 { 0x9c, "kp_enter" },
1027 { 0x53, "kp_decimal" },
1060 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1075 { 0xfe, "compose" },
1080 static int get_keycode(const char *key
)
1086 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1087 if (!strcmp(key
, p
->name
))
1090 if (strstart(key
, "0x", NULL
)) {
1091 ret
= strtoul(key
, &endp
, 0);
1092 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1098 #define MAX_KEYCODES 16
1099 static uint8_t keycodes
[MAX_KEYCODES
];
1100 static int nb_pending_keycodes
;
1101 static QEMUTimer
*key_timer
;
1103 static void release_keys(void *opaque
)
1107 while (nb_pending_keycodes
> 0) {
1108 nb_pending_keycodes
--;
1109 keycode
= keycodes
[nb_pending_keycodes
];
1111 kbd_put_keycode(0xe0);
1112 kbd_put_keycode(keycode
| 0x80);
1116 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1119 char keyname_buf
[16];
1121 int keyname_len
, keycode
, i
;
1123 if (nb_pending_keycodes
> 0) {
1124 qemu_del_timer(key_timer
);
1131 separator
= strchr(string
, '-');
1132 keyname_len
= separator
? separator
- string
: strlen(string
);
1133 if (keyname_len
> 0) {
1134 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1135 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1136 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1139 if (i
== MAX_KEYCODES
) {
1140 monitor_printf(mon
, "too many keys\n");
1143 keyname_buf
[keyname_len
] = 0;
1144 keycode
= get_keycode(keyname_buf
);
1146 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1149 keycodes
[i
++] = keycode
;
1153 string
= separator
+ 1;
1155 nb_pending_keycodes
= i
;
1156 /* key down events */
1157 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1158 keycode
= keycodes
[i
];
1160 kbd_put_keycode(0xe0);
1161 kbd_put_keycode(keycode
& 0x7f);
1163 /* delayed key up events */
1164 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1165 muldiv64(ticks_per_sec
, hold_time
, 1000));
1168 static int mouse_button_state
;
1170 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1174 dx
= strtol(dx_str
, NULL
, 0);
1175 dy
= strtol(dy_str
, NULL
, 0);
1178 dz
= strtol(dz_str
, NULL
, 0);
1179 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1182 static void do_mouse_button(Monitor
*mon
, int button_state
)
1184 mouse_button_state
= button_state
;
1185 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1188 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1189 int addr
, int has_index
, int index
)
1195 cpu_outb(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1203 val
= cpu_inb(NULL
, addr
);
1207 val
= cpu_inw(NULL
, addr
);
1211 val
= cpu_inl(NULL
, addr
);
1215 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1216 suffix
, addr
, size
* 2, val
);
1219 static void do_ioport_write(Monitor
*mon
, int count
, int format
, int size
,
1222 addr
&= IOPORTS_MASK
;
1227 cpu_outb(NULL
, addr
, val
);
1230 cpu_outw(NULL
, addr
, val
);
1233 cpu_outl(NULL
, addr
, val
);
1238 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1242 res
= qemu_boot_set(bootdevice
);
1244 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1245 } else if (res
> 0) {
1246 monitor_printf(mon
, "setting boot device list failed\n");
1248 monitor_printf(mon
, "no function defined to set boot device list for "
1249 "this architecture\n");
1253 static void do_system_reset(Monitor
*mon
)
1255 qemu_system_reset_request();
1258 static void do_system_powerdown(Monitor
*mon
)
1260 qemu_system_powerdown_request();
1263 #if defined(TARGET_I386)
1264 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1266 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1269 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1270 pte
& PG_PSE_MASK
? 'P' : '-',
1271 pte
& PG_DIRTY_MASK
? 'D' : '-',
1272 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1273 pte
& PG_PCD_MASK
? 'C' : '-',
1274 pte
& PG_PWT_MASK
? 'T' : '-',
1275 pte
& PG_USER_MASK
? 'U' : '-',
1276 pte
& PG_RW_MASK
? 'W' : '-');
1279 static void tlb_info(Monitor
*mon
)
1283 uint32_t pgd
, pde
, pte
;
1285 env
= mon_get_cpu();
1289 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1290 monitor_printf(mon
, "PG disabled\n");
1293 pgd
= env
->cr
[3] & ~0xfff;
1294 for(l1
= 0; l1
< 1024; l1
++) {
1295 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1296 pde
= le32_to_cpu(pde
);
1297 if (pde
& PG_PRESENT_MASK
) {
1298 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1299 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1301 for(l2
= 0; l2
< 1024; l2
++) {
1302 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1303 (uint8_t *)&pte
, 4);
1304 pte
= le32_to_cpu(pte
);
1305 if (pte
& PG_PRESENT_MASK
) {
1306 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1316 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1317 uint32_t end
, int prot
)
1320 prot1
= *plast_prot
;
1321 if (prot
!= prot1
) {
1322 if (*pstart
!= -1) {
1323 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1324 *pstart
, end
, end
- *pstart
,
1325 prot1
& PG_USER_MASK
? 'u' : '-',
1327 prot1
& PG_RW_MASK
? 'w' : '-');
1337 static void mem_info(Monitor
*mon
)
1340 int l1
, l2
, prot
, last_prot
;
1341 uint32_t pgd
, pde
, pte
, start
, end
;
1343 env
= mon_get_cpu();
1347 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1348 monitor_printf(mon
, "PG disabled\n");
1351 pgd
= env
->cr
[3] & ~0xfff;
1354 for(l1
= 0; l1
< 1024; l1
++) {
1355 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1356 pde
= le32_to_cpu(pde
);
1358 if (pde
& PG_PRESENT_MASK
) {
1359 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1360 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1361 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1363 for(l2
= 0; l2
< 1024; l2
++) {
1364 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1365 (uint8_t *)&pte
, 4);
1366 pte
= le32_to_cpu(pte
);
1367 end
= (l1
<< 22) + (l2
<< 12);
1368 if (pte
& PG_PRESENT_MASK
) {
1369 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1373 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1378 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1384 #if defined(TARGET_SH4)
1386 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1388 monitor_printf(mon
, " tlb%i:\t"
1389 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1390 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1391 "dirty=%hhu writethrough=%hhu\n",
1393 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1394 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1398 static void tlb_info(Monitor
*mon
)
1400 CPUState
*env
= mon_get_cpu();
1403 monitor_printf (mon
, "ITLB:\n");
1404 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1405 print_tlb (mon
, i
, &env
->itlb
[i
]);
1406 monitor_printf (mon
, "UTLB:\n");
1407 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1408 print_tlb (mon
, i
, &env
->utlb
[i
]);
1413 static void do_info_kqemu(Monitor
*mon
)
1419 env
= mon_get_cpu();
1421 monitor_printf(mon
, "No cpu initialized yet");
1424 val
= env
->kqemu_enabled
;
1425 monitor_printf(mon
, "kqemu support: ");
1429 monitor_printf(mon
, "disabled\n");
1432 monitor_printf(mon
, "enabled for user code\n");
1435 monitor_printf(mon
, "enabled for user and kernel code\n");
1439 monitor_printf(mon
, "kqemu support: not compiled\n");
1443 static void do_info_kvm(Monitor
*mon
)
1445 #if defined(USE_KVM) || defined(CONFIG_KVM)
1446 monitor_printf(mon
, "kvm support: ");
1448 monitor_printf(mon
, "enabled\n");
1450 monitor_printf(mon
, "disabled\n");
1452 monitor_printf(mon
, "kvm support: not compiled\n");
1456 static void do_info_numa(Monitor
*mon
)
1461 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1462 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1463 monitor_printf(mon
, "node %d cpus:", i
);
1464 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1465 if (env
->numa_node
== i
) {
1466 monitor_printf(mon
, " %d", env
->cpu_index
);
1469 monitor_printf(mon
, "\n");
1470 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1475 #ifdef CONFIG_PROFILER
1479 int64_t kqemu_exec_count
;
1481 int64_t kqemu_ret_int_count
;
1482 int64_t kqemu_ret_excp_count
;
1483 int64_t kqemu_ret_intr_count
;
1485 static void do_info_profile(Monitor
*mon
)
1491 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1492 dev_time
, dev_time
/ (double)ticks_per_sec
);
1493 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1494 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1495 monitor_printf(mon
, "kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%"
1496 PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%"
1498 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1499 kqemu_time
/ (double)total
* 100.0,
1501 kqemu_ret_int_count
,
1502 kqemu_ret_excp_count
,
1503 kqemu_ret_intr_count
);
1506 kqemu_exec_count
= 0;
1508 kqemu_ret_int_count
= 0;
1509 kqemu_ret_excp_count
= 0;
1510 kqemu_ret_intr_count
= 0;
1512 kqemu_record_dump();
1516 static void do_info_profile(Monitor
*mon
)
1518 monitor_printf(mon
, "Internal profiler not compiled\n");
1522 /* Capture support */
1523 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1525 static void do_info_capture(Monitor
*mon
)
1530 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1531 monitor_printf(mon
, "[%d]: ", i
);
1532 s
->ops
.info (s
->opaque
);
1537 static void do_stop_capture(Monitor
*mon
, int n
)
1542 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1544 s
->ops
.destroy (s
->opaque
);
1545 LIST_REMOVE (s
, entries
);
1552 static void do_wav_capture(Monitor
*mon
, const char *path
,
1553 int has_freq
, int freq
,
1554 int has_bits
, int bits
,
1555 int has_channels
, int nchannels
)
1559 s
= qemu_mallocz (sizeof (*s
));
1561 freq
= has_freq
? freq
: 44100;
1562 bits
= has_bits
? bits
: 16;
1563 nchannels
= has_channels
? nchannels
: 2;
1565 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1566 monitor_printf(mon
, "Faied to add wave capture\n");
1569 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1573 #if defined(TARGET_I386)
1574 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1578 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1579 if (env
->cpu_index
== cpu_index
) {
1581 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1583 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1589 static void do_info_status(Monitor
*mon
)
1593 monitor_printf(mon
, "VM status: running (single step mode)\n");
1595 monitor_printf(mon
, "VM status: running\n");
1598 monitor_printf(mon
, "VM status: paused\n");
1602 static void do_balloon(Monitor
*mon
, int value
)
1604 ram_addr_t target
= value
;
1605 qemu_balloon(target
<< 20);
1608 static void do_info_balloon(Monitor
*mon
)
1612 actual
= qemu_balloon_status();
1613 if (kvm_enabled() && !kvm_has_sync_mmu())
1614 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1615 "ballooning disabled\n");
1616 else if (actual
== 0)
1617 monitor_printf(mon
, "Ballooning not activated in VM\n");
1619 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1622 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1624 qemu_acl
*acl
= qemu_acl_find(name
);
1627 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1632 static void do_acl_show(Monitor
*mon
, const char *aclname
)
1634 qemu_acl
*acl
= find_acl(mon
, aclname
);
1635 qemu_acl_entry
*entry
;
1639 monitor_printf(mon
, "policy: %s\n",
1640 acl
->defaultDeny
? "deny" : "allow");
1641 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1643 monitor_printf(mon
, "%d: %s %s\n", i
,
1644 entry
->deny
? "deny" : "allow", entry
->match
);
1649 static void do_acl_reset(Monitor
*mon
, const char *aclname
)
1651 qemu_acl
*acl
= find_acl(mon
, aclname
);
1654 qemu_acl_reset(acl
);
1655 monitor_printf(mon
, "acl: removed all rules\n");
1659 static void do_acl_policy(Monitor
*mon
, const char *aclname
,
1662 qemu_acl
*acl
= find_acl(mon
, aclname
);
1665 if (strcmp(policy
, "allow") == 0) {
1666 acl
->defaultDeny
= 0;
1667 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1668 } else if (strcmp(policy
, "deny") == 0) {
1669 acl
->defaultDeny
= 1;
1670 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1672 monitor_printf(mon
, "acl: unknown policy '%s', "
1673 "expected 'deny' or 'allow'\n", policy
);
1678 static void do_acl_add(Monitor
*mon
, const char *aclname
,
1679 const char *match
, const char *policy
,
1680 int has_index
, int index
)
1682 qemu_acl
*acl
= find_acl(mon
, aclname
);
1686 if (strcmp(policy
, "allow") == 0) {
1688 } else if (strcmp(policy
, "deny") == 0) {
1691 monitor_printf(mon
, "acl: unknown policy '%s', "
1692 "expected 'deny' or 'allow'\n", policy
);
1696 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1698 ret
= qemu_acl_append(acl
, deny
, match
);
1700 monitor_printf(mon
, "acl: unable to add acl entry\n");
1702 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1706 static void do_acl_remove(Monitor
*mon
, const char *aclname
, const char *match
)
1708 qemu_acl
*acl
= find_acl(mon
, aclname
);
1712 ret
= qemu_acl_remove(acl
, match
);
1714 monitor_printf(mon
, "acl: no matching acl entry\n");
1716 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1720 #if defined(TARGET_I386)
1721 static void do_inject_mce(Monitor
*mon
,
1722 int cpu_index
, int bank
,
1723 unsigned status_hi
, unsigned status_lo
,
1724 unsigned mcg_status_hi
, unsigned mcg_status_lo
,
1725 unsigned addr_hi
, unsigned addr_lo
,
1726 unsigned misc_hi
, unsigned misc_lo
)
1729 uint64_t status
= ((uint64_t)status_hi
<< 32) | status_lo
;
1730 uint64_t mcg_status
= ((uint64_t)mcg_status_hi
<< 32) | mcg_status_lo
;
1731 uint64_t addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1732 uint64_t misc
= ((uint64_t)misc_hi
<< 32) | misc_lo
;
1734 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1735 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1736 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1742 static void do_getfd(Monitor
*mon
, const char *fdname
)
1747 fd
= qemu_chr_get_msgfd(mon
->chr
);
1749 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1753 if (qemu_isdigit(fdname
[0])) {
1754 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1760 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1765 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1766 if (strcmp(monfd
->name
, fdname
) != 0) {
1775 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1776 monfd
->name
= qemu_strdup(fdname
);
1779 LIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1782 static void do_closefd(Monitor
*mon
, const char *fdname
)
1786 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1787 if (strcmp(monfd
->name
, fdname
) != 0) {
1791 LIST_REMOVE(monfd
, next
);
1793 qemu_free(monfd
->name
);
1798 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1802 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1806 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1809 if (strcmp(monfd
->name
, fdname
) != 0) {
1815 /* caller takes ownership of fd */
1816 LIST_REMOVE(monfd
, next
);
1817 qemu_free(monfd
->name
);
1826 static const mon_cmd_t mon_cmds
[] = {
1827 #include "qemu-monitor.h"
1831 /* Please update qemu-monitor.hx when adding or changing commands */
1832 static const mon_cmd_t info_cmds
[] = {
1833 { "version", "", do_info_version
,
1834 "", "show the version of QEMU" },
1835 { "network", "", do_info_network
,
1836 "", "show the network state" },
1837 { "chardev", "", qemu_chr_info
,
1838 "", "show the character devices" },
1839 { "block", "", bdrv_info
,
1840 "", "show the block devices" },
1841 { "blockstats", "", bdrv_info_stats
,
1842 "", "show block device statistics" },
1843 { "registers", "", do_info_registers
,
1844 "", "show the cpu registers" },
1845 { "cpus", "", do_info_cpus
,
1846 "", "show infos for each CPU" },
1847 { "history", "", do_info_history
,
1848 "", "show the command line history", },
1849 { "irq", "", irq_info
,
1850 "", "show the interrupts statistics (if available)", },
1851 { "pic", "", pic_info
,
1852 "", "show i8259 (PIC) state", },
1853 { "pci", "", pci_info
,
1854 "", "show PCI info", },
1855 #if defined(TARGET_I386) || defined(TARGET_SH4)
1856 { "tlb", "", tlb_info
,
1857 "", "show virtual to physical memory mappings", },
1859 #if defined(TARGET_I386)
1860 { "mem", "", mem_info
,
1861 "", "show the active virtual memory mappings", },
1862 { "hpet", "", do_info_hpet
,
1863 "", "show state of HPET", },
1865 { "jit", "", do_info_jit
,
1866 "", "show dynamic compiler info", },
1867 { "kqemu", "", do_info_kqemu
,
1868 "", "show KQEMU information", },
1869 { "kvm", "", do_info_kvm
,
1870 "", "show KVM information", },
1871 { "numa", "", do_info_numa
,
1872 "", "show NUMA information", },
1873 { "usb", "", usb_info
,
1874 "", "show guest USB devices", },
1875 { "usbhost", "", usb_host_info
,
1876 "", "show host USB devices", },
1877 { "profile", "", do_info_profile
,
1878 "", "show profiling information", },
1879 { "capture", "", do_info_capture
,
1880 "", "show capture information" },
1881 { "snapshots", "", do_info_snapshots
,
1882 "", "show the currently saved VM snapshots" },
1883 { "status", "", do_info_status
,
1884 "", "show the current VM status (running|paused)" },
1885 { "pcmcia", "", pcmcia_info
,
1886 "", "show guest PCMCIA status" },
1887 { "mice", "", do_info_mice
,
1888 "", "show which guest mouse is receiving events" },
1889 { "vnc", "", do_info_vnc
,
1890 "", "show the vnc server status"},
1891 { "name", "", do_info_name
,
1892 "", "show the current VM name" },
1893 { "uuid", "", do_info_uuid
,
1894 "", "show the current VM UUID" },
1895 #if defined(TARGET_PPC)
1896 { "cpustats", "", do_info_cpu_stats
,
1897 "", "show CPU statistics", },
1899 #if defined(CONFIG_SLIRP)
1900 { "usernet", "", do_info_usernet
,
1901 "", "show user network stack connection states", },
1903 { "migrate", "", do_info_migrate
, "", "show migration status" },
1904 { "balloon", "", do_info_balloon
,
1905 "", "show balloon information" },
1906 { "qtree", "", do_info_qtree
,
1907 "", "show device tree" },
1911 /*******************************************************************/
1913 static const char *pch
;
1914 static jmp_buf expr_env
;
1919 typedef struct MonitorDef
{
1922 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1926 #if defined(TARGET_I386)
1927 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1929 CPUState
*env
= mon_get_cpu();
1932 return env
->eip
+ env
->segs
[R_CS
].base
;
1936 #if defined(TARGET_PPC)
1937 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1939 CPUState
*env
= mon_get_cpu();
1947 for (i
= 0; i
< 8; i
++)
1948 u
|= env
->crf
[i
] << (32 - (4 * i
));
1953 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1955 CPUState
*env
= mon_get_cpu();
1961 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1963 CPUState
*env
= mon_get_cpu();
1969 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1971 CPUState
*env
= mon_get_cpu();
1974 return cpu_ppc_load_decr(env
);
1977 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1979 CPUState
*env
= mon_get_cpu();
1982 return cpu_ppc_load_tbu(env
);
1985 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1987 CPUState
*env
= mon_get_cpu();
1990 return cpu_ppc_load_tbl(env
);
1994 #if defined(TARGET_SPARC)
1995 #ifndef TARGET_SPARC64
1996 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1998 CPUState
*env
= mon_get_cpu();
2001 return GET_PSR(env
);
2005 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2007 CPUState
*env
= mon_get_cpu();
2010 return env
->regwptr
[val
];
2014 static const MonitorDef monitor_defs
[] = {
2017 #define SEG(name, seg) \
2018 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2019 { name ".base", offsetof(CPUState, segs[seg].base) },\
2020 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2022 { "eax", offsetof(CPUState
, regs
[0]) },
2023 { "ecx", offsetof(CPUState
, regs
[1]) },
2024 { "edx", offsetof(CPUState
, regs
[2]) },
2025 { "ebx", offsetof(CPUState
, regs
[3]) },
2026 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2027 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2028 { "esi", offsetof(CPUState
, regs
[6]) },
2029 { "edi", offsetof(CPUState
, regs
[7]) },
2030 #ifdef TARGET_X86_64
2031 { "r8", offsetof(CPUState
, regs
[8]) },
2032 { "r9", offsetof(CPUState
, regs
[9]) },
2033 { "r10", offsetof(CPUState
, regs
[10]) },
2034 { "r11", offsetof(CPUState
, regs
[11]) },
2035 { "r12", offsetof(CPUState
, regs
[12]) },
2036 { "r13", offsetof(CPUState
, regs
[13]) },
2037 { "r14", offsetof(CPUState
, regs
[14]) },
2038 { "r15", offsetof(CPUState
, regs
[15]) },
2040 { "eflags", offsetof(CPUState
, eflags
) },
2041 { "eip", offsetof(CPUState
, eip
) },
2048 { "pc", 0, monitor_get_pc
, },
2049 #elif defined(TARGET_PPC)
2050 /* General purpose registers */
2051 { "r0", offsetof(CPUState
, gpr
[0]) },
2052 { "r1", offsetof(CPUState
, gpr
[1]) },
2053 { "r2", offsetof(CPUState
, gpr
[2]) },
2054 { "r3", offsetof(CPUState
, gpr
[3]) },
2055 { "r4", offsetof(CPUState
, gpr
[4]) },
2056 { "r5", offsetof(CPUState
, gpr
[5]) },
2057 { "r6", offsetof(CPUState
, gpr
[6]) },
2058 { "r7", offsetof(CPUState
, gpr
[7]) },
2059 { "r8", offsetof(CPUState
, gpr
[8]) },
2060 { "r9", offsetof(CPUState
, gpr
[9]) },
2061 { "r10", offsetof(CPUState
, gpr
[10]) },
2062 { "r11", offsetof(CPUState
, gpr
[11]) },
2063 { "r12", offsetof(CPUState
, gpr
[12]) },
2064 { "r13", offsetof(CPUState
, gpr
[13]) },
2065 { "r14", offsetof(CPUState
, gpr
[14]) },
2066 { "r15", offsetof(CPUState
, gpr
[15]) },
2067 { "r16", offsetof(CPUState
, gpr
[16]) },
2068 { "r17", offsetof(CPUState
, gpr
[17]) },
2069 { "r18", offsetof(CPUState
, gpr
[18]) },
2070 { "r19", offsetof(CPUState
, gpr
[19]) },
2071 { "r20", offsetof(CPUState
, gpr
[20]) },
2072 { "r21", offsetof(CPUState
, gpr
[21]) },
2073 { "r22", offsetof(CPUState
, gpr
[22]) },
2074 { "r23", offsetof(CPUState
, gpr
[23]) },
2075 { "r24", offsetof(CPUState
, gpr
[24]) },
2076 { "r25", offsetof(CPUState
, gpr
[25]) },
2077 { "r26", offsetof(CPUState
, gpr
[26]) },
2078 { "r27", offsetof(CPUState
, gpr
[27]) },
2079 { "r28", offsetof(CPUState
, gpr
[28]) },
2080 { "r29", offsetof(CPUState
, gpr
[29]) },
2081 { "r30", offsetof(CPUState
, gpr
[30]) },
2082 { "r31", offsetof(CPUState
, gpr
[31]) },
2083 /* Floating point registers */
2084 { "f0", offsetof(CPUState
, fpr
[0]) },
2085 { "f1", offsetof(CPUState
, fpr
[1]) },
2086 { "f2", offsetof(CPUState
, fpr
[2]) },
2087 { "f3", offsetof(CPUState
, fpr
[3]) },
2088 { "f4", offsetof(CPUState
, fpr
[4]) },
2089 { "f5", offsetof(CPUState
, fpr
[5]) },
2090 { "f6", offsetof(CPUState
, fpr
[6]) },
2091 { "f7", offsetof(CPUState
, fpr
[7]) },
2092 { "f8", offsetof(CPUState
, fpr
[8]) },
2093 { "f9", offsetof(CPUState
, fpr
[9]) },
2094 { "f10", offsetof(CPUState
, fpr
[10]) },
2095 { "f11", offsetof(CPUState
, fpr
[11]) },
2096 { "f12", offsetof(CPUState
, fpr
[12]) },
2097 { "f13", offsetof(CPUState
, fpr
[13]) },
2098 { "f14", offsetof(CPUState
, fpr
[14]) },
2099 { "f15", offsetof(CPUState
, fpr
[15]) },
2100 { "f16", offsetof(CPUState
, fpr
[16]) },
2101 { "f17", offsetof(CPUState
, fpr
[17]) },
2102 { "f18", offsetof(CPUState
, fpr
[18]) },
2103 { "f19", offsetof(CPUState
, fpr
[19]) },
2104 { "f20", offsetof(CPUState
, fpr
[20]) },
2105 { "f21", offsetof(CPUState
, fpr
[21]) },
2106 { "f22", offsetof(CPUState
, fpr
[22]) },
2107 { "f23", offsetof(CPUState
, fpr
[23]) },
2108 { "f24", offsetof(CPUState
, fpr
[24]) },
2109 { "f25", offsetof(CPUState
, fpr
[25]) },
2110 { "f26", offsetof(CPUState
, fpr
[26]) },
2111 { "f27", offsetof(CPUState
, fpr
[27]) },
2112 { "f28", offsetof(CPUState
, fpr
[28]) },
2113 { "f29", offsetof(CPUState
, fpr
[29]) },
2114 { "f30", offsetof(CPUState
, fpr
[30]) },
2115 { "f31", offsetof(CPUState
, fpr
[31]) },
2116 { "fpscr", offsetof(CPUState
, fpscr
) },
2117 /* Next instruction pointer */
2118 { "nip|pc", offsetof(CPUState
, nip
) },
2119 { "lr", offsetof(CPUState
, lr
) },
2120 { "ctr", offsetof(CPUState
, ctr
) },
2121 { "decr", 0, &monitor_get_decr
, },
2122 { "ccr", 0, &monitor_get_ccr
, },
2123 /* Machine state register */
2124 { "msr", 0, &monitor_get_msr
, },
2125 { "xer", 0, &monitor_get_xer
, },
2126 { "tbu", 0, &monitor_get_tbu
, },
2127 { "tbl", 0, &monitor_get_tbl
, },
2128 #if defined(TARGET_PPC64)
2129 /* Address space register */
2130 { "asr", offsetof(CPUState
, asr
) },
2132 /* Segment registers */
2133 { "sdr1", offsetof(CPUState
, sdr1
) },
2134 { "sr0", offsetof(CPUState
, sr
[0]) },
2135 { "sr1", offsetof(CPUState
, sr
[1]) },
2136 { "sr2", offsetof(CPUState
, sr
[2]) },
2137 { "sr3", offsetof(CPUState
, sr
[3]) },
2138 { "sr4", offsetof(CPUState
, sr
[4]) },
2139 { "sr5", offsetof(CPUState
, sr
[5]) },
2140 { "sr6", offsetof(CPUState
, sr
[6]) },
2141 { "sr7", offsetof(CPUState
, sr
[7]) },
2142 { "sr8", offsetof(CPUState
, sr
[8]) },
2143 { "sr9", offsetof(CPUState
, sr
[9]) },
2144 { "sr10", offsetof(CPUState
, sr
[10]) },
2145 { "sr11", offsetof(CPUState
, sr
[11]) },
2146 { "sr12", offsetof(CPUState
, sr
[12]) },
2147 { "sr13", offsetof(CPUState
, sr
[13]) },
2148 { "sr14", offsetof(CPUState
, sr
[14]) },
2149 { "sr15", offsetof(CPUState
, sr
[15]) },
2150 /* Too lazy to put BATs and SPRs ... */
2151 #elif defined(TARGET_SPARC)
2152 { "g0", offsetof(CPUState
, gregs
[0]) },
2153 { "g1", offsetof(CPUState
, gregs
[1]) },
2154 { "g2", offsetof(CPUState
, gregs
[2]) },
2155 { "g3", offsetof(CPUState
, gregs
[3]) },
2156 { "g4", offsetof(CPUState
, gregs
[4]) },
2157 { "g5", offsetof(CPUState
, gregs
[5]) },
2158 { "g6", offsetof(CPUState
, gregs
[6]) },
2159 { "g7", offsetof(CPUState
, gregs
[7]) },
2160 { "o0", 0, monitor_get_reg
},
2161 { "o1", 1, monitor_get_reg
},
2162 { "o2", 2, monitor_get_reg
},
2163 { "o3", 3, monitor_get_reg
},
2164 { "o4", 4, monitor_get_reg
},
2165 { "o5", 5, monitor_get_reg
},
2166 { "o6", 6, monitor_get_reg
},
2167 { "o7", 7, monitor_get_reg
},
2168 { "l0", 8, monitor_get_reg
},
2169 { "l1", 9, monitor_get_reg
},
2170 { "l2", 10, monitor_get_reg
},
2171 { "l3", 11, monitor_get_reg
},
2172 { "l4", 12, monitor_get_reg
},
2173 { "l5", 13, monitor_get_reg
},
2174 { "l6", 14, monitor_get_reg
},
2175 { "l7", 15, monitor_get_reg
},
2176 { "i0", 16, monitor_get_reg
},
2177 { "i1", 17, monitor_get_reg
},
2178 { "i2", 18, monitor_get_reg
},
2179 { "i3", 19, monitor_get_reg
},
2180 { "i4", 20, monitor_get_reg
},
2181 { "i5", 21, monitor_get_reg
},
2182 { "i6", 22, monitor_get_reg
},
2183 { "i7", 23, monitor_get_reg
},
2184 { "pc", offsetof(CPUState
, pc
) },
2185 { "npc", offsetof(CPUState
, npc
) },
2186 { "y", offsetof(CPUState
, y
) },
2187 #ifndef TARGET_SPARC64
2188 { "psr", 0, &monitor_get_psr
, },
2189 { "wim", offsetof(CPUState
, wim
) },
2191 { "tbr", offsetof(CPUState
, tbr
) },
2192 { "fsr", offsetof(CPUState
, fsr
) },
2193 { "f0", offsetof(CPUState
, fpr
[0]) },
2194 { "f1", offsetof(CPUState
, fpr
[1]) },
2195 { "f2", offsetof(CPUState
, fpr
[2]) },
2196 { "f3", offsetof(CPUState
, fpr
[3]) },
2197 { "f4", offsetof(CPUState
, fpr
[4]) },
2198 { "f5", offsetof(CPUState
, fpr
[5]) },
2199 { "f6", offsetof(CPUState
, fpr
[6]) },
2200 { "f7", offsetof(CPUState
, fpr
[7]) },
2201 { "f8", offsetof(CPUState
, fpr
[8]) },
2202 { "f9", offsetof(CPUState
, fpr
[9]) },
2203 { "f10", offsetof(CPUState
, fpr
[10]) },
2204 { "f11", offsetof(CPUState
, fpr
[11]) },
2205 { "f12", offsetof(CPUState
, fpr
[12]) },
2206 { "f13", offsetof(CPUState
, fpr
[13]) },
2207 { "f14", offsetof(CPUState
, fpr
[14]) },
2208 { "f15", offsetof(CPUState
, fpr
[15]) },
2209 { "f16", offsetof(CPUState
, fpr
[16]) },
2210 { "f17", offsetof(CPUState
, fpr
[17]) },
2211 { "f18", offsetof(CPUState
, fpr
[18]) },
2212 { "f19", offsetof(CPUState
, fpr
[19]) },
2213 { "f20", offsetof(CPUState
, fpr
[20]) },
2214 { "f21", offsetof(CPUState
, fpr
[21]) },
2215 { "f22", offsetof(CPUState
, fpr
[22]) },
2216 { "f23", offsetof(CPUState
, fpr
[23]) },
2217 { "f24", offsetof(CPUState
, fpr
[24]) },
2218 { "f25", offsetof(CPUState
, fpr
[25]) },
2219 { "f26", offsetof(CPUState
, fpr
[26]) },
2220 { "f27", offsetof(CPUState
, fpr
[27]) },
2221 { "f28", offsetof(CPUState
, fpr
[28]) },
2222 { "f29", offsetof(CPUState
, fpr
[29]) },
2223 { "f30", offsetof(CPUState
, fpr
[30]) },
2224 { "f31", offsetof(CPUState
, fpr
[31]) },
2225 #ifdef TARGET_SPARC64
2226 { "f32", offsetof(CPUState
, fpr
[32]) },
2227 { "f34", offsetof(CPUState
, fpr
[34]) },
2228 { "f36", offsetof(CPUState
, fpr
[36]) },
2229 { "f38", offsetof(CPUState
, fpr
[38]) },
2230 { "f40", offsetof(CPUState
, fpr
[40]) },
2231 { "f42", offsetof(CPUState
, fpr
[42]) },
2232 { "f44", offsetof(CPUState
, fpr
[44]) },
2233 { "f46", offsetof(CPUState
, fpr
[46]) },
2234 { "f48", offsetof(CPUState
, fpr
[48]) },
2235 { "f50", offsetof(CPUState
, fpr
[50]) },
2236 { "f52", offsetof(CPUState
, fpr
[52]) },
2237 { "f54", offsetof(CPUState
, fpr
[54]) },
2238 { "f56", offsetof(CPUState
, fpr
[56]) },
2239 { "f58", offsetof(CPUState
, fpr
[58]) },
2240 { "f60", offsetof(CPUState
, fpr
[60]) },
2241 { "f62", offsetof(CPUState
, fpr
[62]) },
2242 { "asi", offsetof(CPUState
, asi
) },
2243 { "pstate", offsetof(CPUState
, pstate
) },
2244 { "cansave", offsetof(CPUState
, cansave
) },
2245 { "canrestore", offsetof(CPUState
, canrestore
) },
2246 { "otherwin", offsetof(CPUState
, otherwin
) },
2247 { "wstate", offsetof(CPUState
, wstate
) },
2248 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2249 { "fprs", offsetof(CPUState
, fprs
) },
2255 static void expr_error(Monitor
*mon
, const char *msg
)
2257 monitor_printf(mon
, "%s\n", msg
);
2258 longjmp(expr_env
, 1);
2261 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2262 static int get_monitor_def(target_long
*pval
, const char *name
)
2264 const MonitorDef
*md
;
2267 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2268 if (compare_cmd(name
, md
->name
)) {
2269 if (md
->get_value
) {
2270 *pval
= md
->get_value(md
, md
->offset
);
2272 CPUState
*env
= mon_get_cpu();
2275 ptr
= (uint8_t *)env
+ md
->offset
;
2278 *pval
= *(int32_t *)ptr
;
2281 *pval
= *(target_long
*)ptr
;
2294 static void next(void)
2298 while (qemu_isspace(*pch
))
2303 static int64_t expr_sum(Monitor
*mon
);
2305 static int64_t expr_unary(Monitor
*mon
)
2314 n
= expr_unary(mon
);
2318 n
= -expr_unary(mon
);
2322 n
= ~expr_unary(mon
);
2328 expr_error(mon
, "')' expected");
2335 expr_error(mon
, "character constant expected");
2339 expr_error(mon
, "missing terminating \' character");
2349 while ((*pch
>= 'a' && *pch
<= 'z') ||
2350 (*pch
>= 'A' && *pch
<= 'Z') ||
2351 (*pch
>= '0' && *pch
<= '9') ||
2352 *pch
== '_' || *pch
== '.') {
2353 if ((q
- buf
) < sizeof(buf
) - 1)
2357 while (qemu_isspace(*pch
))
2360 ret
= get_monitor_def(®
, buf
);
2362 expr_error(mon
, "unknown register");
2364 expr_error(mon
, "no cpu defined");
2369 expr_error(mon
, "unexpected end of expression");
2373 #if TARGET_PHYS_ADDR_BITS > 32
2374 n
= strtoull(pch
, &p
, 0);
2376 n
= strtoul(pch
, &p
, 0);
2379 expr_error(mon
, "invalid char in expression");
2382 while (qemu_isspace(*pch
))
2390 static int64_t expr_prod(Monitor
*mon
)
2395 val
= expr_unary(mon
);
2398 if (op
!= '*' && op
!= '/' && op
!= '%')
2401 val2
= expr_unary(mon
);
2410 expr_error(mon
, "division by zero");
2421 static int64_t expr_logic(Monitor
*mon
)
2426 val
= expr_prod(mon
);
2429 if (op
!= '&' && op
!= '|' && op
!= '^')
2432 val2
= expr_prod(mon
);
2449 static int64_t expr_sum(Monitor
*mon
)
2454 val
= expr_logic(mon
);
2457 if (op
!= '+' && op
!= '-')
2460 val2
= expr_logic(mon
);
2469 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2472 if (setjmp(expr_env
)) {
2476 while (qemu_isspace(*pch
))
2478 *pval
= expr_sum(mon
);
2483 static int get_str(char *buf
, int buf_size
, const char **pp
)
2491 while (qemu_isspace(*p
))
2501 while (*p
!= '\0' && *p
!= '\"') {
2517 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2520 if ((q
- buf
) < buf_size
- 1) {
2524 if ((q
- buf
) < buf_size
- 1) {
2531 qemu_printf("unterminated string\n");
2536 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2537 if ((q
- buf
) < buf_size
- 1) {
2549 * Store the command-name in cmdname, and return a pointer to
2550 * the remaining of the command string.
2552 static const char *get_command_name(const char *cmdline
,
2553 char *cmdname
, size_t nlen
)
2556 const char *p
, *pstart
;
2559 while (qemu_isspace(*p
))
2564 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2569 memcpy(cmdname
, pstart
, len
);
2570 cmdname
[len
] = '\0';
2574 static int default_fmt_format
= 'x';
2575 static int default_fmt_size
= 4;
2579 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2581 const char *p
, *typestr
;
2582 int c
, nb_args
, i
, has_arg
;
2583 const mon_cmd_t
*cmd
;
2586 void *str_allocated
[MAX_ARGS
];
2587 void *args
[MAX_ARGS
];
2588 void (*handler_0
)(Monitor
*mon
);
2589 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2590 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2591 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2592 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2594 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2595 void *arg3
, void *arg4
);
2596 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2597 void *arg3
, void *arg4
, void *arg5
);
2598 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2599 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2600 void (*handler_8
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2601 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2603 void (*handler_9
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2604 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2605 void *arg7
, void *arg8
);
2606 void (*handler_10
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2607 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2608 void *arg7
, void *arg8
, void *arg9
);
2611 monitor_printf(mon
, "command='%s'\n", cmdline
);
2614 /* extract the command name */
2615 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2619 /* find the command */
2620 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2621 if (compare_cmd(cmdname
, cmd
->name
))
2625 if (cmd
->name
== NULL
) {
2626 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2630 for(i
= 0; i
< MAX_ARGS
; i
++)
2631 str_allocated
[i
] = NULL
;
2633 /* parse the parameters */
2634 typestr
= cmd
->args_type
;
2649 while (qemu_isspace(*p
))
2651 if (*typestr
== '?') {
2654 /* no optional string: NULL argument */
2659 ret
= get_str(buf
, sizeof(buf
), &p
);
2663 monitor_printf(mon
, "%s: filename expected\n",
2667 monitor_printf(mon
, "%s: block device name expected\n",
2671 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2676 str
= qemu_malloc(strlen(buf
) + 1);
2677 pstrcpy(str
, sizeof(buf
), buf
);
2678 str_allocated
[nb_args
] = str
;
2680 if (nb_args
>= MAX_ARGS
) {
2682 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2685 args
[nb_args
++] = str
;
2690 int count
, format
, size
;
2692 while (qemu_isspace(*p
))
2698 if (qemu_isdigit(*p
)) {
2700 while (qemu_isdigit(*p
)) {
2701 count
= count
* 10 + (*p
- '0');
2739 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2740 monitor_printf(mon
, "invalid char in format: '%c'\n",
2745 format
= default_fmt_format
;
2746 if (format
!= 'i') {
2747 /* for 'i', not specifying a size gives -1 as size */
2749 size
= default_fmt_size
;
2750 default_fmt_size
= size
;
2752 default_fmt_format
= format
;
2755 format
= default_fmt_format
;
2756 if (format
!= 'i') {
2757 size
= default_fmt_size
;
2762 if (nb_args
+ 3 > MAX_ARGS
)
2764 args
[nb_args
++] = (void*)(long)count
;
2765 args
[nb_args
++] = (void*)(long)format
;
2766 args
[nb_args
++] = (void*)(long)size
;
2774 while (qemu_isspace(*p
))
2776 if (*typestr
== '?' || *typestr
== '.') {
2777 if (*typestr
== '?') {
2785 while (qemu_isspace(*p
))
2793 if (nb_args
>= MAX_ARGS
)
2795 args
[nb_args
++] = (void *)(long)has_arg
;
2797 if (nb_args
>= MAX_ARGS
)
2803 if (get_expr(mon
, &val
, &p
))
2807 if (nb_args
>= MAX_ARGS
)
2809 args
[nb_args
++] = (void *)(long)val
;
2811 if ((nb_args
+ 1) >= MAX_ARGS
)
2813 #if TARGET_PHYS_ADDR_BITS > 32
2814 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2816 args
[nb_args
++] = (void *)0;
2818 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2830 while (qemu_isspace(*p
))
2836 monitor_printf(mon
, "%s: unsupported option -%c\n",
2843 if (nb_args
>= MAX_ARGS
)
2845 args
[nb_args
++] = (void *)(long)has_option
;
2850 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2854 /* check that all arguments were parsed */
2855 while (qemu_isspace(*p
))
2858 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2865 handler_0
= cmd
->handler
;
2869 handler_1
= cmd
->handler
;
2870 handler_1(mon
, args
[0]);
2873 handler_2
= cmd
->handler
;
2874 handler_2(mon
, args
[0], args
[1]);
2877 handler_3
= cmd
->handler
;
2878 handler_3(mon
, args
[0], args
[1], args
[2]);
2881 handler_4
= cmd
->handler
;
2882 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2885 handler_5
= cmd
->handler
;
2886 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2889 handler_6
= cmd
->handler
;
2890 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2893 handler_7
= cmd
->handler
;
2894 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2898 handler_8
= cmd
->handler
;
2899 handler_8(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2903 handler_9
= cmd
->handler
;
2904 handler_9(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2905 args
[6], args
[7], args
[8]);
2908 handler_10
= cmd
->handler
;
2909 handler_10(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2910 args
[6], args
[7], args
[8], args
[9]);
2913 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2917 for(i
= 0; i
< MAX_ARGS
; i
++)
2918 qemu_free(str_allocated
[i
]);
2921 static void cmd_completion(const char *name
, const char *list
)
2923 const char *p
, *pstart
;
2932 p
= pstart
+ strlen(pstart
);
2934 if (len
> sizeof(cmd
) - 2)
2935 len
= sizeof(cmd
) - 2;
2936 memcpy(cmd
, pstart
, len
);
2938 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2939 readline_add_completion(cur_mon
->rs
, cmd
);
2947 static void file_completion(const char *input
)
2952 char file
[1024], file_prefix
[1024];
2956 p
= strrchr(input
, '/');
2959 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2960 pstrcpy(path
, sizeof(path
), ".");
2962 input_path_len
= p
- input
+ 1;
2963 memcpy(path
, input
, input_path_len
);
2964 if (input_path_len
> sizeof(path
) - 1)
2965 input_path_len
= sizeof(path
) - 1;
2966 path
[input_path_len
] = '\0';
2967 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2969 #ifdef DEBUG_COMPLETION
2970 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2971 input
, path
, file_prefix
);
2973 ffs
= opendir(path
);
2981 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2982 memcpy(file
, input
, input_path_len
);
2983 if (input_path_len
< sizeof(file
))
2984 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2986 /* stat the file to find out if it's a directory.
2987 * In that case add a slash to speed up typing long paths
2990 if(S_ISDIR(sb
.st_mode
))
2991 pstrcat(file
, sizeof(file
), "/");
2992 readline_add_completion(cur_mon
->rs
, file
);
2998 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3000 const char *name
= bdrv_get_device_name(bs
);
3001 const char *input
= opaque
;
3003 if (input
[0] == '\0' ||
3004 !strncmp(name
, (char *)input
, strlen(input
))) {
3005 readline_add_completion(cur_mon
->rs
, name
);
3009 /* NOTE: this parser is an approximate form of the real command parser */
3010 static void parse_cmdline(const char *cmdline
,
3011 int *pnb_args
, char **args
)
3020 while (qemu_isspace(*p
))
3024 if (nb_args
>= MAX_ARGS
)
3026 ret
= get_str(buf
, sizeof(buf
), &p
);
3027 args
[nb_args
] = qemu_strdup(buf
);
3032 *pnb_args
= nb_args
;
3035 static void monitor_find_completion(const char *cmdline
)
3037 const char *cmdname
;
3038 char *args
[MAX_ARGS
];
3039 int nb_args
, i
, len
;
3040 const char *ptype
, *str
;
3041 const mon_cmd_t
*cmd
;
3044 parse_cmdline(cmdline
, &nb_args
, args
);
3045 #ifdef DEBUG_COMPLETION
3046 for(i
= 0; i
< nb_args
; i
++) {
3047 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3051 /* if the line ends with a space, it means we want to complete the
3053 len
= strlen(cmdline
);
3054 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3055 if (nb_args
>= MAX_ARGS
)
3057 args
[nb_args
++] = qemu_strdup("");
3060 /* command completion */
3065 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3066 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3067 cmd_completion(cmdname
, cmd
->name
);
3070 /* find the command */
3071 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3072 if (compare_cmd(args
[0], cmd
->name
))
3077 ptype
= cmd
->args_type
;
3078 for(i
= 0; i
< nb_args
- 2; i
++) {
3079 if (*ptype
!= '\0') {
3081 while (*ptype
== '?')
3085 str
= args
[nb_args
- 1];
3086 if (*ptype
== '-' && ptype
[1] != '\0') {
3091 /* file completion */
3092 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3093 file_completion(str
);
3096 /* block device name completion */
3097 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3098 bdrv_iterate(block_completion_it
, (void *)str
);
3101 /* XXX: more generic ? */
3102 if (!strcmp(cmd
->name
, "info")) {
3103 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3104 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3105 cmd_completion(str
, cmd
->name
);
3107 } else if (!strcmp(cmd
->name
, "sendkey")) {
3108 char *sep
= strrchr(str
, '-');
3111 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3112 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3113 cmd_completion(str
, key
->name
);
3115 } else if (!strcmp(cmd
->name
, "help|?")) {
3116 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3117 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3118 cmd_completion(str
, cmd
->name
);
3126 for(i
= 0; i
< nb_args
; i
++)
3130 static int monitor_can_read(void *opaque
)
3132 Monitor
*mon
= opaque
;
3134 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3137 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3139 Monitor
*old_mon
= cur_mon
;
3145 for (i
= 0; i
< size
; i
++)
3146 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3148 if (size
== 0 || buf
[size
- 1] != 0)
3149 monitor_printf(cur_mon
, "corrupted command\n");
3151 monitor_handle_command(cur_mon
, (char *)buf
);
3157 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3159 monitor_suspend(mon
);
3160 monitor_handle_command(mon
, cmdline
);
3161 monitor_resume(mon
);
3164 int monitor_suspend(Monitor
*mon
)
3172 void monitor_resume(Monitor
*mon
)
3176 if (--mon
->suspend_cnt
== 0)
3177 readline_show_prompt(mon
->rs
);
3180 static void monitor_event(void *opaque
, int event
)
3182 Monitor
*mon
= opaque
;
3185 case CHR_EVENT_MUX_IN
:
3186 readline_restart(mon
->rs
);
3187 monitor_resume(mon
);
3191 case CHR_EVENT_MUX_OUT
:
3192 if (mon
->suspend_cnt
== 0)
3193 monitor_printf(mon
, "\n");
3195 monitor_suspend(mon
);
3198 case CHR_EVENT_RESET
:
3199 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3200 "information\n", QEMU_VERSION
);
3201 if (mon
->chr
->focus
== 0)
3202 readline_show_prompt(mon
->rs
);
3216 void monitor_init(CharDriverState
*chr
, int flags
)
3218 static int is_first_init
= 1;
3221 if (is_first_init
) {
3222 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3226 mon
= qemu_mallocz(sizeof(*mon
));
3230 if (mon
->chr
->focus
!= 0)
3231 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3232 if (flags
& MONITOR_USE_READLINE
) {
3233 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3234 monitor_read_command(mon
, 0);
3237 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3240 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3241 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3245 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3247 BlockDriverState
*bs
= opaque
;
3250 if (bdrv_set_key(bs
, password
) != 0) {
3251 monitor_printf(mon
, "invalid password\n");
3254 if (mon
->password_completion_cb
)
3255 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3257 monitor_read_command(mon
, 1);
3260 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3261 BlockDriverCompletionFunc
*completion_cb
,
3266 if (!bdrv_key_required(bs
)) {
3268 completion_cb(opaque
, 0);
3272 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3273 bdrv_get_encrypted_filename(bs
));
3275 mon
->password_completion_cb
= completion_cb
;
3276 mon
->password_opaque
= opaque
;
3278 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3280 if (err
&& completion_cb
)
3281 completion_cb(opaque
, err
);