4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
34 #include "qemu-char.h"
40 #include "audio/audio.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
49 //#define DEBUG_COMPLETION
55 * 'B' block device name
56 * 's' string (accept optional quote)
58 * 'l' target long (32 or 64 bit)
59 * '/' optional gdb-like print format (like "/10x")
61 * '?' optional type (for 'F', 's' and 'i')
65 typedef struct mon_cmd_t
{
67 const char *args_type
;
81 BlockDriverCompletionFunc
*password_completion_cb
;
82 void *password_opaque
;
83 LIST_ENTRY(Monitor
) entry
;
86 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
88 static const mon_cmd_t mon_cmds
[];
89 static const mon_cmd_t info_cmds
[];
91 Monitor
*cur_mon
= NULL
;
93 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
96 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
98 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
100 readline_show_prompt(mon
->rs
);
103 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
107 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
108 /* prompt is printed on return from the command handler */
111 monitor_printf(mon
, "terminal does not support password prompting\n");
116 void monitor_flush(Monitor
*mon
)
118 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
119 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
120 mon
->outbuf_index
= 0;
124 /* flush at every end of line or if the buffer is full */
125 static void monitor_puts(Monitor
*mon
, const char *str
)
137 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
138 mon
->outbuf
[mon
->outbuf_index
++] = c
;
139 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
145 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
148 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
149 monitor_puts(mon
, buf
);
152 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
156 monitor_vprintf(mon
, fmt
, ap
);
160 void monitor_print_filename(Monitor
*mon
, const char *filename
)
164 for (i
= 0; filename
[i
]; i
++) {
165 switch (filename
[i
]) {
169 monitor_printf(mon
, "\\%c", filename
[i
]);
172 monitor_printf(mon
, "\\t");
175 monitor_printf(mon
, "\\r");
178 monitor_printf(mon
, "\\n");
181 monitor_printf(mon
, "%c", filename
[i
]);
187 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
191 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
196 static int compare_cmd(const char *name
, const char *list
)
198 const char *p
, *pstart
;
206 p
= pstart
+ strlen(pstart
);
207 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
216 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
217 const char *prefix
, const char *name
)
219 const mon_cmd_t
*cmd
;
221 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
222 if (!name
|| !strcmp(name
, cmd
->name
))
223 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
224 cmd
->params
, cmd
->help
);
228 static void help_cmd(Monitor
*mon
, const char *name
)
230 if (name
&& !strcmp(name
, "info")) {
231 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
233 help_cmd_dump(mon
, mon_cmds
, "", name
);
234 if (name
&& !strcmp(name
, "log")) {
235 const CPULogItem
*item
;
236 monitor_printf(mon
, "Log items (comma separated):\n");
237 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
238 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
239 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
245 static void do_commit(Monitor
*mon
, const char *device
)
249 all_devices
= !strcmp(device
, "all");
250 for (i
= 0; i
< nb_drives
; i
++) {
252 !strcmp(bdrv_get_device_name(drives_table
[i
].bdrv
), device
))
253 bdrv_commit(drives_table
[i
].bdrv
);
257 static void do_info(Monitor
*mon
, const char *item
)
259 const mon_cmd_t
*cmd
;
260 void (*handler
)(Monitor
*);
264 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
265 if (compare_cmd(item
, cmd
->name
))
269 help_cmd(mon
, "info");
272 handler
= cmd
->handler
;
276 static void do_info_version(Monitor
*mon
)
278 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
281 static void do_info_name(Monitor
*mon
)
284 monitor_printf(mon
, "%s\n", qemu_name
);
287 #if defined(TARGET_I386)
288 static void do_info_hpet(Monitor
*mon
)
290 monitor_printf(mon
, "HPET is %s by QEMU\n",
291 (no_hpet
) ? "disabled" : "enabled");
295 static void do_info_uuid(Monitor
*mon
)
297 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
298 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
299 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
300 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
301 qemu_uuid
[14], qemu_uuid
[15]);
304 /* get the current CPU defined by the user */
305 static int mon_set_cpu(int cpu_index
)
309 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
310 if (env
->cpu_index
== cpu_index
) {
311 cur_mon
->mon_cpu
= env
;
318 static CPUState
*mon_get_cpu(void)
320 if (!cur_mon
->mon_cpu
) {
323 cpu_synchronize_state(cur_mon
->mon_cpu
, 0);
324 return cur_mon
->mon_cpu
;
327 static void do_info_registers(Monitor
*mon
)
334 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
337 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
342 static void do_info_cpus(Monitor
*mon
)
346 /* just to set the default cpu if not already done */
349 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
350 cpu_synchronize_state(env
, 0);
351 monitor_printf(mon
, "%c CPU #%d:",
352 (env
== mon
->mon_cpu
) ? '*' : ' ',
354 #if defined(TARGET_I386)
355 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
356 env
->eip
+ env
->segs
[R_CS
].base
);
357 #elif defined(TARGET_PPC)
358 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
359 #elif defined(TARGET_SPARC)
360 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
362 #elif defined(TARGET_MIPS)
363 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
366 monitor_printf(mon
, " (halted)");
367 monitor_printf(mon
, "\n");
371 static void do_cpu_set(Monitor
*mon
, int index
)
373 if (mon_set_cpu(index
) < 0)
374 monitor_printf(mon
, "Invalid CPU index\n");
377 static void do_info_jit(Monitor
*mon
)
379 dump_exec_info((FILE *)mon
, monitor_fprintf
);
382 static void do_info_history(Monitor
*mon
)
391 str
= readline_get_history(mon
->rs
, i
);
394 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
399 #if defined(TARGET_PPC)
400 /* XXX: not implemented in other targets */
401 static void do_info_cpu_stats(Monitor
*mon
)
406 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
410 static void do_quit(Monitor
*mon
)
415 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
417 if (bdrv_is_inserted(bs
)) {
419 if (!bdrv_is_removable(bs
)) {
420 monitor_printf(mon
, "device is not removable\n");
423 if (bdrv_is_locked(bs
)) {
424 monitor_printf(mon
, "device is locked\n");
433 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
435 BlockDriverState
*bs
;
437 bs
= bdrv_find(filename
);
439 monitor_printf(mon
, "device not found\n");
442 eject_device(mon
, bs
, force
);
445 static void do_change_block(Monitor
*mon
, const char *device
,
446 const char *filename
, const char *fmt
)
448 BlockDriverState
*bs
;
449 BlockDriver
*drv
= NULL
;
451 bs
= bdrv_find(device
);
453 monitor_printf(mon
, "device not found\n");
457 drv
= bdrv_find_format(fmt
);
459 monitor_printf(mon
, "invalid format %s\n", fmt
);
463 if (eject_device(mon
, bs
, 0) < 0)
465 bdrv_open2(bs
, filename
, 0, drv
);
466 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
469 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
472 if (vnc_display_password(NULL
, password
) < 0)
473 monitor_printf(mon
, "could not set VNC server password\n");
475 monitor_read_command(mon
, 1);
478 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
480 if (strcmp(target
, "passwd") == 0 ||
481 strcmp(target
, "password") == 0) {
484 strncpy(password
, arg
, sizeof(password
));
485 password
[sizeof(password
) - 1] = '\0';
486 change_vnc_password_cb(mon
, password
, NULL
);
488 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
491 if (vnc_display_open(NULL
, target
) < 0)
492 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
496 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
499 if (strcmp(device
, "vnc") == 0) {
500 do_change_vnc(mon
, target
, arg
);
502 do_change_block(mon
, device
, target
, arg
);
506 static void do_screen_dump(Monitor
*mon
, const char *filename
)
508 vga_hw_screen_dump(filename
);
511 static void do_logfile(Monitor
*mon
, const char *filename
)
513 cpu_set_log_filename(filename
);
516 static void do_log(Monitor
*mon
, const char *items
)
520 if (!strcmp(items
, "none")) {
523 mask
= cpu_str_to_log_mask(items
);
525 help_cmd(mon
, "log");
532 static void do_singlestep(Monitor
*mon
, const char *option
)
534 if (!option
|| !strcmp(option
, "on")) {
536 } else if (!strcmp(option
, "off")) {
539 monitor_printf(mon
, "unexpected option %s\n", option
);
543 static void do_stop(Monitor
*mon
)
545 vm_stop(EXCP_INTERRUPT
);
548 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
550 struct bdrv_iterate_context
{
555 static void do_cont(Monitor
*mon
)
557 struct bdrv_iterate_context context
= { mon
, 0 };
559 bdrv_iterate(encrypted_bdrv_it
, &context
);
560 /* only resume the vm if all keys are set and valid */
565 static void bdrv_key_cb(void *opaque
, int err
)
567 Monitor
*mon
= opaque
;
569 /* another key was set successfully, retry to continue */
574 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
576 struct bdrv_iterate_context
*context
= opaque
;
578 if (!context
->err
&& bdrv_key_required(bs
)) {
579 context
->err
= -EBUSY
;
580 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
585 static void do_gdbserver(Monitor
*mon
, const char *device
)
588 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
589 if (gdbserver_start(device
) < 0) {
590 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
592 } else if (strcmp(device
, "none") == 0) {
593 monitor_printf(mon
, "Disabled gdbserver\n");
595 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
600 static void do_watchdog_action(Monitor
*mon
, const char *action
)
602 if (select_watchdog_action(action
) == -1) {
603 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
607 static void monitor_printc(Monitor
*mon
, int c
)
609 monitor_printf(mon
, "'");
612 monitor_printf(mon
, "\\'");
615 monitor_printf(mon
, "\\\\");
618 monitor_printf(mon
, "\\n");
621 monitor_printf(mon
, "\\r");
624 if (c
>= 32 && c
<= 126) {
625 monitor_printf(mon
, "%c", c
);
627 monitor_printf(mon
, "\\x%02x", c
);
631 monitor_printf(mon
, "'");
634 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
635 target_phys_addr_t addr
, int is_physical
)
638 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
646 if (!env
&& !is_physical
)
651 } else if (wsize
== 4) {
654 /* as default we use the current CS size */
658 if ((env
->efer
& MSR_EFER_LMA
) &&
659 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
663 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
668 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
677 nb_per_line
= line_size
/ wsize
;
682 max_digits
= (wsize
* 8 + 2) / 3;
686 max_digits
= (wsize
* 8) / 4;
690 max_digits
= (wsize
* 8 * 10 + 32) / 33;
699 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
701 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
706 cpu_physical_memory_rw(addr
, buf
, l
, 0);
711 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
712 monitor_printf(mon
, " Cannot access memory\n");
721 v
= ldub_raw(buf
+ i
);
724 v
= lduw_raw(buf
+ i
);
727 v
= (uint32_t)ldl_raw(buf
+ i
);
730 v
= ldq_raw(buf
+ i
);
733 monitor_printf(mon
, " ");
736 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
739 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
742 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
745 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
748 monitor_printc(mon
, v
);
753 monitor_printf(mon
, "\n");
759 #if TARGET_LONG_BITS == 64
760 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
762 #define GET_TLONG(h, l) (l)
765 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
766 uint32_t addrh
, uint32_t addrl
)
768 target_long addr
= GET_TLONG(addrh
, addrl
);
769 memory_dump(mon
, count
, format
, size
, addr
, 0);
772 #if TARGET_PHYS_ADDR_BITS > 32
773 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
775 #define GET_TPHYSADDR(h, l) (l)
778 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
779 int size
, uint32_t addrh
, uint32_t addrl
)
782 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
783 memory_dump(mon
, count
, format
, size
, addr
, 1);
786 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
787 unsigned int valh
, unsigned int vall
)
789 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
790 #if TARGET_PHYS_ADDR_BITS == 32
793 monitor_printf(mon
, "%#o", val
);
796 monitor_printf(mon
, "%#x", val
);
799 monitor_printf(mon
, "%u", val
);
803 monitor_printf(mon
, "%d", val
);
806 monitor_printc(mon
, val
);
812 monitor_printf(mon
, "%#" PRIo64
, val
);
815 monitor_printf(mon
, "%#" PRIx64
, val
);
818 monitor_printf(mon
, "%" PRIu64
, val
);
822 monitor_printf(mon
, "%" PRId64
, val
);
825 monitor_printc(mon
, val
);
829 monitor_printf(mon
, "\n");
832 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
833 uint32_t size
, const char *filename
)
836 target_long addr
= GET_TLONG(valh
, vall
);
845 f
= fopen(filename
, "wb");
847 monitor_printf(mon
, "could not open '%s'\n", filename
);
854 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
855 fwrite(buf
, 1, l
, f
);
862 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
863 unsigned int vall
, uint32_t size
,
864 const char *filename
)
869 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
871 f
= fopen(filename
, "wb");
873 monitor_printf(mon
, "could not open '%s'\n", filename
);
880 cpu_physical_memory_rw(addr
, buf
, l
, 0);
881 fwrite(buf
, 1, l
, f
);
889 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
896 for(addr
= start
; addr
< (start
+ size
); addr
++) {
897 cpu_physical_memory_rw(addr
, buf
, 1, 0);
898 /* BSD sum algorithm ('sum' Unix command) */
899 sum
= (sum
>> 1) | (sum
<< 15);
902 monitor_printf(mon
, "%05d\n", sum
);
910 static const KeyDef key_defs
[] = {
937 { 0x0e, "backspace" },
974 { 0x37, "asterisk" },
977 { 0x3a, "caps_lock" },
988 { 0x45, "num_lock" },
989 { 0x46, "scroll_lock" },
991 { 0xb5, "kp_divide" },
992 { 0x37, "kp_multiply" },
993 { 0x4a, "kp_subtract" },
995 { 0x9c, "kp_enter" },
996 { 0x53, "kp_decimal" },
1029 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1044 { 0xfe, "compose" },
1049 static int get_keycode(const char *key
)
1055 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1056 if (!strcmp(key
, p
->name
))
1059 if (strstart(key
, "0x", NULL
)) {
1060 ret
= strtoul(key
, &endp
, 0);
1061 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1067 #define MAX_KEYCODES 16
1068 static uint8_t keycodes
[MAX_KEYCODES
];
1069 static int nb_pending_keycodes
;
1070 static QEMUTimer
*key_timer
;
1072 static void release_keys(void *opaque
)
1076 while (nb_pending_keycodes
> 0) {
1077 nb_pending_keycodes
--;
1078 keycode
= keycodes
[nb_pending_keycodes
];
1080 kbd_put_keycode(0xe0);
1081 kbd_put_keycode(keycode
| 0x80);
1085 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1088 char keyname_buf
[16];
1090 int keyname_len
, keycode
, i
;
1092 if (nb_pending_keycodes
> 0) {
1093 qemu_del_timer(key_timer
);
1100 separator
= strchr(string
, '-');
1101 keyname_len
= separator
? separator
- string
: strlen(string
);
1102 if (keyname_len
> 0) {
1103 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1104 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1105 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1108 if (i
== MAX_KEYCODES
) {
1109 monitor_printf(mon
, "too many keys\n");
1112 keyname_buf
[keyname_len
] = 0;
1113 keycode
= get_keycode(keyname_buf
);
1115 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1118 keycodes
[i
++] = keycode
;
1122 string
= separator
+ 1;
1124 nb_pending_keycodes
= i
;
1125 /* key down events */
1126 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1127 keycode
= keycodes
[i
];
1129 kbd_put_keycode(0xe0);
1130 kbd_put_keycode(keycode
& 0x7f);
1132 /* delayed key up events */
1133 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1134 muldiv64(ticks_per_sec
, hold_time
, 1000));
1137 static int mouse_button_state
;
1139 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1143 dx
= strtol(dx_str
, NULL
, 0);
1144 dy
= strtol(dy_str
, NULL
, 0);
1147 dz
= strtol(dz_str
, NULL
, 0);
1148 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1151 static void do_mouse_button(Monitor
*mon
, int button_state
)
1153 mouse_button_state
= button_state
;
1154 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1157 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1158 int addr
, int has_index
, int index
)
1164 cpu_outb(NULL
, addr
& 0xffff, index
& 0xff);
1172 val
= cpu_inb(NULL
, addr
);
1176 val
= cpu_inw(NULL
, addr
);
1180 val
= cpu_inl(NULL
, addr
);
1184 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1185 suffix
, addr
, size
* 2, val
);
1188 /* boot_set handler */
1189 static QEMUBootSetHandler
*qemu_boot_set_handler
= NULL
;
1190 static void *boot_opaque
;
1192 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
1194 qemu_boot_set_handler
= func
;
1195 boot_opaque
= opaque
;
1198 static void do_boot_set(Monitor
*mon
, const char *bootdevice
)
1202 if (qemu_boot_set_handler
) {
1203 res
= qemu_boot_set_handler(boot_opaque
, bootdevice
);
1205 monitor_printf(mon
, "boot device list now set to %s\n",
1208 monitor_printf(mon
, "setting boot device list failed with "
1211 monitor_printf(mon
, "no function defined to set boot device list for "
1212 "this architecture\n");
1216 static void do_system_reset(Monitor
*mon
)
1218 qemu_system_reset_request();
1221 static void do_system_powerdown(Monitor
*mon
)
1223 qemu_system_powerdown_request();
1226 #if defined(TARGET_I386)
1227 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1229 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1232 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1233 pte
& PG_PSE_MASK
? 'P' : '-',
1234 pte
& PG_DIRTY_MASK
? 'D' : '-',
1235 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1236 pte
& PG_PCD_MASK
? 'C' : '-',
1237 pte
& PG_PWT_MASK
? 'T' : '-',
1238 pte
& PG_USER_MASK
? 'U' : '-',
1239 pte
& PG_RW_MASK
? 'W' : '-');
1242 static void tlb_info(Monitor
*mon
)
1246 uint32_t pgd
, pde
, pte
;
1248 env
= mon_get_cpu();
1252 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1253 monitor_printf(mon
, "PG disabled\n");
1256 pgd
= env
->cr
[3] & ~0xfff;
1257 for(l1
= 0; l1
< 1024; l1
++) {
1258 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1259 pde
= le32_to_cpu(pde
);
1260 if (pde
& PG_PRESENT_MASK
) {
1261 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1262 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1264 for(l2
= 0; l2
< 1024; l2
++) {
1265 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1266 (uint8_t *)&pte
, 4);
1267 pte
= le32_to_cpu(pte
);
1268 if (pte
& PG_PRESENT_MASK
) {
1269 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1279 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1280 uint32_t end
, int prot
)
1283 prot1
= *plast_prot
;
1284 if (prot
!= prot1
) {
1285 if (*pstart
!= -1) {
1286 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1287 *pstart
, end
, end
- *pstart
,
1288 prot1
& PG_USER_MASK
? 'u' : '-',
1290 prot1
& PG_RW_MASK
? 'w' : '-');
1300 static void mem_info(Monitor
*mon
)
1303 int l1
, l2
, prot
, last_prot
;
1304 uint32_t pgd
, pde
, pte
, start
, end
;
1306 env
= mon_get_cpu();
1310 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1311 monitor_printf(mon
, "PG disabled\n");
1314 pgd
= env
->cr
[3] & ~0xfff;
1317 for(l1
= 0; l1
< 1024; l1
++) {
1318 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1319 pde
= le32_to_cpu(pde
);
1321 if (pde
& PG_PRESENT_MASK
) {
1322 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1323 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1324 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1326 for(l2
= 0; l2
< 1024; l2
++) {
1327 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1328 (uint8_t *)&pte
, 4);
1329 pte
= le32_to_cpu(pte
);
1330 end
= (l1
<< 22) + (l2
<< 12);
1331 if (pte
& PG_PRESENT_MASK
) {
1332 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1336 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1341 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1347 #if defined(TARGET_SH4)
1349 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1351 monitor_printf(mon
, " tlb%i:\t"
1352 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1353 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1354 "dirty=%hhu writethrough=%hhu\n",
1356 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1357 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1361 static void tlb_info(Monitor
*mon
)
1363 CPUState
*env
= mon_get_cpu();
1366 monitor_printf (mon
, "ITLB:\n");
1367 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1368 print_tlb (mon
, i
, &env
->itlb
[i
]);
1369 monitor_printf (mon
, "UTLB:\n");
1370 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1371 print_tlb (mon
, i
, &env
->utlb
[i
]);
1376 static void do_info_kqemu(Monitor
*mon
)
1382 env
= mon_get_cpu();
1384 monitor_printf(mon
, "No cpu initialized yet");
1387 val
= env
->kqemu_enabled
;
1388 monitor_printf(mon
, "kqemu support: ");
1392 monitor_printf(mon
, "disabled\n");
1395 monitor_printf(mon
, "enabled for user code\n");
1398 monitor_printf(mon
, "enabled for user and kernel code\n");
1402 monitor_printf(mon
, "kqemu support: not compiled\n");
1406 static void do_info_kvm(Monitor
*mon
)
1409 monitor_printf(mon
, "kvm support: ");
1411 monitor_printf(mon
, "enabled\n");
1413 monitor_printf(mon
, "disabled\n");
1415 monitor_printf(mon
, "kvm support: not compiled\n");
1419 static void do_info_numa(Monitor
*mon
)
1424 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1425 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1426 monitor_printf(mon
, "node %d cpus:", i
);
1427 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1428 if (env
->numa_node
== i
) {
1429 monitor_printf(mon
, " %d", env
->cpu_index
);
1432 monitor_printf(mon
, "\n");
1433 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1438 #ifdef CONFIG_PROFILER
1442 int64_t kqemu_exec_count
;
1444 int64_t kqemu_ret_int_count
;
1445 int64_t kqemu_ret_excp_count
;
1446 int64_t kqemu_ret_intr_count
;
1448 static void do_info_profile(Monitor
*mon
)
1454 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1455 dev_time
, dev_time
/ (double)ticks_per_sec
);
1456 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1457 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1458 monitor_printf(mon
, "kqemu time %" PRId64
" (%0.3f %0.1f%%) count=%"
1459 PRId64
" int=%" PRId64
" excp=%" PRId64
" intr=%"
1461 kqemu_time
, kqemu_time
/ (double)ticks_per_sec
,
1462 kqemu_time
/ (double)total
* 100.0,
1464 kqemu_ret_int_count
,
1465 kqemu_ret_excp_count
,
1466 kqemu_ret_intr_count
);
1469 kqemu_exec_count
= 0;
1471 kqemu_ret_int_count
= 0;
1472 kqemu_ret_excp_count
= 0;
1473 kqemu_ret_intr_count
= 0;
1475 kqemu_record_dump();
1479 static void do_info_profile(Monitor
*mon
)
1481 monitor_printf(mon
, "Internal profiler not compiled\n");
1485 /* Capture support */
1486 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1488 static void do_info_capture(Monitor
*mon
)
1493 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1494 monitor_printf(mon
, "[%d]: ", i
);
1495 s
->ops
.info (s
->opaque
);
1499 static void do_stop_capture(Monitor
*mon
, int n
)
1504 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1506 s
->ops
.destroy (s
->opaque
);
1507 LIST_REMOVE (s
, entries
);
1515 static void do_wav_capture(Monitor
*mon
, const char *path
,
1516 int has_freq
, int freq
,
1517 int has_bits
, int bits
,
1518 int has_channels
, int nchannels
)
1522 s
= qemu_mallocz (sizeof (*s
));
1524 freq
= has_freq
? freq
: 44100;
1525 bits
= has_bits
? bits
: 16;
1526 nchannels
= has_channels
? nchannels
: 2;
1528 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1529 monitor_printf(mon
, "Faied to add wave capture\n");
1532 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1536 #if defined(TARGET_I386)
1537 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1541 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1542 if (env
->cpu_index
== cpu_index
) {
1543 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1549 static void do_info_status(Monitor
*mon
)
1553 monitor_printf(mon
, "VM status: running (single step mode)\n");
1555 monitor_printf(mon
, "VM status: running\n");
1558 monitor_printf(mon
, "VM status: paused\n");
1562 static void do_balloon(Monitor
*mon
, int value
)
1564 ram_addr_t target
= value
;
1565 qemu_balloon(target
<< 20);
1568 static void do_info_balloon(Monitor
*mon
)
1572 actual
= qemu_balloon_status();
1573 if (kvm_enabled() && !kvm_has_sync_mmu())
1574 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1575 "ballooning disabled\n");
1576 else if (actual
== 0)
1577 monitor_printf(mon
, "Ballooning not activated in VM\n");
1579 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1582 static void do_acl(Monitor
*mon
,
1583 const char *command
,
1584 const char *aclname
,
1591 acl
= qemu_acl_find(aclname
);
1593 monitor_printf(mon
, "acl: unknown list '%s'\n", aclname
);
1597 if (strcmp(command
, "show") == 0) {
1599 qemu_acl_entry
*entry
;
1600 monitor_printf(mon
, "policy: %s\n",
1601 acl
->defaultDeny
? "deny" : "allow");
1602 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1604 monitor_printf(mon
, "%d: %s %s\n", i
,
1605 entry
->deny
? "deny" : "allow",
1608 } else if (strcmp(command
, "reset") == 0) {
1609 qemu_acl_reset(acl
);
1610 monitor_printf(mon
, "acl: removed all rules\n");
1611 } else if (strcmp(command
, "policy") == 0) {
1613 monitor_printf(mon
, "acl: missing policy parameter\n");
1617 if (strcmp(match
, "allow") == 0) {
1618 acl
->defaultDeny
= 0;
1619 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1620 } else if (strcmp(match
, "deny") == 0) {
1621 acl
->defaultDeny
= 1;
1622 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1624 monitor_printf(mon
, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match
);
1626 } else if ((strcmp(command
, "allow") == 0) ||
1627 (strcmp(command
, "deny") == 0)) {
1628 int deny
= strcmp(command
, "deny") == 0 ? 1 : 0;
1632 monitor_printf(mon
, "acl: missing match parameter\n");
1637 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1639 ret
= qemu_acl_append(acl
, deny
, match
);
1641 monitor_printf(mon
, "acl: unable to add acl entry\n");
1643 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1644 } else if (strcmp(command
, "remove") == 0) {
1648 monitor_printf(mon
, "acl: missing match parameter\n");
1652 ret
= qemu_acl_remove(acl
, match
);
1654 monitor_printf(mon
, "acl: no matching acl entry\n");
1656 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1658 monitor_printf(mon
, "acl: unknown command '%s'\n", command
);
1662 /* Please update qemu-doc.texi when adding or changing commands */
1663 static const mon_cmd_t mon_cmds
[] = {
1664 { "help|?", "s?", help_cmd
,
1665 "[cmd]", "show the help" },
1666 { "commit", "s", do_commit
,
1667 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1668 { "info", "s?", do_info
,
1669 "[subcommand]", "show various information about the system state" },
1670 { "q|quit", "", do_quit
,
1671 "", "quit the emulator" },
1672 { "eject", "-fB", do_eject
,
1673 "[-f] device", "eject a removable medium (use -f to force it)" },
1674 { "change", "BFs?", do_change
,
1675 "device filename [format]", "change a removable medium, optional format" },
1676 { "screendump", "F", do_screen_dump
,
1677 "filename", "save screen into PPM image 'filename'" },
1678 { "logfile", "F", do_logfile
,
1679 "filename", "output logs to 'filename'" },
1680 { "log", "s", do_log
,
1681 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1682 { "savevm", "s?", do_savevm
,
1683 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1684 { "loadvm", "s", do_loadvm
,
1685 "tag|id", "restore a VM snapshot from its tag or id" },
1686 { "delvm", "s", do_delvm
,
1687 "tag|id", "delete a VM snapshot from its tag or id" },
1688 { "singlestep", "s?", do_singlestep
,
1689 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1690 { "stop", "", do_stop
,
1691 "", "stop emulation", },
1692 { "c|cont", "", do_cont
,
1693 "", "resume emulation", },
1694 { "gdbserver", "s?", do_gdbserver
,
1695 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
1696 { "x", "/l", do_memory_dump
,
1697 "/fmt addr", "virtual memory dump starting at 'addr'", },
1698 { "xp", "/l", do_physical_memory_dump
,
1699 "/fmt addr", "physical memory dump starting at 'addr'", },
1700 { "p|print", "/l", do_print
,
1701 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1702 { "i", "/ii.", do_ioport_read
,
1703 "/fmt addr", "I/O port read" },
1705 { "sendkey", "si?", do_sendkey
,
1706 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1707 { "system_reset", "", do_system_reset
,
1708 "", "reset the system" },
1709 { "system_powerdown", "", do_system_powerdown
,
1710 "", "send system power down event" },
1711 { "sum", "ii", do_sum
,
1712 "addr size", "compute the checksum of a memory region" },
1713 { "usb_add", "s", do_usb_add
,
1714 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1715 { "usb_del", "s", do_usb_del
,
1716 "device", "remove USB device 'bus.addr'" },
1717 { "cpu", "i", do_cpu_set
,
1718 "index", "set the default CPU" },
1719 { "mouse_move", "sss?", do_mouse_move
,
1720 "dx dy [dz]", "send mouse move events" },
1721 { "mouse_button", "i", do_mouse_button
,
1722 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1723 { "mouse_set", "i", do_mouse_set
,
1724 "index", "set which mouse device receives events" },
1726 { "wavcapture", "si?i?i?", do_wav_capture
,
1727 "path [frequency [bits [channels]]]",
1728 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1730 { "stopcapture", "i", do_stop_capture
,
1731 "capture index", "stop capture" },
1732 { "memsave", "lis", do_memory_save
,
1733 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1734 { "pmemsave", "lis", do_physical_memory_save
,
1735 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1736 { "boot_set", "s", do_boot_set
,
1737 "bootdevice", "define new values for the boot device list" },
1738 #if defined(TARGET_I386)
1739 { "nmi", "i", do_inject_nmi
,
1740 "cpu", "inject an NMI on the given CPU", },
1742 { "migrate", "-ds", do_migrate
,
1743 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1744 { "migrate_cancel", "", do_migrate_cancel
,
1745 "", "cancel the current VM migration" },
1746 { "migrate_set_speed", "s", do_migrate_set_speed
,
1747 "value", "set maximum speed (in bytes) for migrations" },
1748 #if defined(TARGET_I386)
1749 { "drive_add", "ss", drive_hot_add
, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1750 "[file=file][,if=type][,bus=n]\n"
1751 "[,unit=m][,media=d][index=i]\n"
1752 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1753 "[snapshot=on|off][,cache=on|off]",
1754 "add drive to PCI storage controller" },
1755 { "pci_add", "sss", pci_device_hot_add
, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1756 { "pci_del", "s", pci_device_hot_remove
, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1758 { "host_net_add", "ss?", net_host_device_add
,
1759 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1760 { "host_net_remove", "is", net_host_device_remove
,
1761 "vlan_id name", "remove host VLAN client" },
1763 { "host_net_redir", "ss?", net_slirp_redir
,
1764 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)\n"
1765 "host_net_redir remove [tcp:|udp:]host-port -- remove redirection\n"
1766 "host_net_redir list -- show all redirections" },
1768 { "balloon", "i", do_balloon
,
1769 "target", "request VM to change it's memory allocation (in MB)" },
1770 { "set_link", "ss", do_set_link
,
1771 "name up|down", "change the link status of a network adapter" },
1772 { "watchdog_action", "s", do_watchdog_action
,
1773 "[reset|shutdown|poweroff|pause|debug|none]", "change watchdog action" },
1774 { "acl", "sss?i?", do_acl
, "<command> <aclname> [<match> [<index>]]\n",
1775 "acl show vnc.username\n"
1776 "acl policy vnc.username deny\n"
1777 "acl allow vnc.username fred\n"
1778 "acl deny vnc.username bob\n"
1779 "acl reset vnc.username\n" },
1783 /* Please update qemu-doc.texi when adding or changing commands */
1784 static const mon_cmd_t info_cmds
[] = {
1785 { "version", "", do_info_version
,
1786 "", "show the version of QEMU" },
1787 { "network", "", do_info_network
,
1788 "", "show the network state" },
1789 { "chardev", "", qemu_chr_info
,
1790 "", "show the character devices" },
1791 { "block", "", bdrv_info
,
1792 "", "show the block devices" },
1793 { "blockstats", "", bdrv_info_stats
,
1794 "", "show block device statistics" },
1795 { "registers", "", do_info_registers
,
1796 "", "show the cpu registers" },
1797 { "cpus", "", do_info_cpus
,
1798 "", "show infos for each CPU" },
1799 { "history", "", do_info_history
,
1800 "", "show the command line history", },
1801 { "irq", "", irq_info
,
1802 "", "show the interrupts statistics (if available)", },
1803 { "pic", "", pic_info
,
1804 "", "show i8259 (PIC) state", },
1805 { "pci", "", pci_info
,
1806 "", "show PCI info", },
1807 #if defined(TARGET_I386) || defined(TARGET_SH4)
1808 { "tlb", "", tlb_info
,
1809 "", "show virtual to physical memory mappings", },
1811 #if defined(TARGET_I386)
1812 { "mem", "", mem_info
,
1813 "", "show the active virtual memory mappings", },
1814 { "hpet", "", do_info_hpet
,
1815 "", "show state of HPET", },
1817 { "jit", "", do_info_jit
,
1818 "", "show dynamic compiler info", },
1819 { "kqemu", "", do_info_kqemu
,
1820 "", "show KQEMU information", },
1821 { "kvm", "", do_info_kvm
,
1822 "", "show KVM information", },
1823 { "numa", "", do_info_numa
,
1824 "", "show NUMA information", },
1825 { "usb", "", usb_info
,
1826 "", "show guest USB devices", },
1827 { "usbhost", "", usb_host_info
,
1828 "", "show host USB devices", },
1829 { "profile", "", do_info_profile
,
1830 "", "show profiling information", },
1831 { "capture", "", do_info_capture
,
1832 "", "show capture information" },
1833 { "snapshots", "", do_info_snapshots
,
1834 "", "show the currently saved VM snapshots" },
1835 { "status", "", do_info_status
,
1836 "", "show the current VM status (running|paused)" },
1837 { "pcmcia", "", pcmcia_info
,
1838 "", "show guest PCMCIA status" },
1839 { "mice", "", do_info_mice
,
1840 "", "show which guest mouse is receiving events" },
1841 { "vnc", "", do_info_vnc
,
1842 "", "show the vnc server status"},
1843 { "name", "", do_info_name
,
1844 "", "show the current VM name" },
1845 { "uuid", "", do_info_uuid
,
1846 "", "show the current VM UUID" },
1847 #if defined(TARGET_PPC)
1848 { "cpustats", "", do_info_cpu_stats
,
1849 "", "show CPU statistics", },
1851 #if defined(CONFIG_SLIRP)
1852 { "slirp", "", do_info_slirp
,
1853 "", "show SLIRP statistics", },
1855 { "migrate", "", do_info_migrate
, "", "show migration status" },
1856 { "balloon", "", do_info_balloon
,
1857 "", "show balloon information" },
1858 { "qtree", "", do_info_qtree
,
1859 "", "show device tree" },
1863 /*******************************************************************/
1865 static const char *pch
;
1866 static jmp_buf expr_env
;
1871 typedef struct MonitorDef
{
1874 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1878 #if defined(TARGET_I386)
1879 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1881 CPUState
*env
= mon_get_cpu();
1884 return env
->eip
+ env
->segs
[R_CS
].base
;
1888 #if defined(TARGET_PPC)
1889 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1891 CPUState
*env
= mon_get_cpu();
1899 for (i
= 0; i
< 8; i
++)
1900 u
|= env
->crf
[i
] << (32 - (4 * i
));
1905 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1907 CPUState
*env
= mon_get_cpu();
1913 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1915 CPUState
*env
= mon_get_cpu();
1921 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1923 CPUState
*env
= mon_get_cpu();
1926 return cpu_ppc_load_decr(env
);
1929 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1931 CPUState
*env
= mon_get_cpu();
1934 return cpu_ppc_load_tbu(env
);
1937 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1939 CPUState
*env
= mon_get_cpu();
1942 return cpu_ppc_load_tbl(env
);
1946 #if defined(TARGET_SPARC)
1947 #ifndef TARGET_SPARC64
1948 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1950 CPUState
*env
= mon_get_cpu();
1953 return GET_PSR(env
);
1957 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1959 CPUState
*env
= mon_get_cpu();
1962 return env
->regwptr
[val
];
1966 static const MonitorDef monitor_defs
[] = {
1969 #define SEG(name, seg) \
1970 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1971 { name ".base", offsetof(CPUState, segs[seg].base) },\
1972 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1974 { "eax", offsetof(CPUState
, regs
[0]) },
1975 { "ecx", offsetof(CPUState
, regs
[1]) },
1976 { "edx", offsetof(CPUState
, regs
[2]) },
1977 { "ebx", offsetof(CPUState
, regs
[3]) },
1978 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1979 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1980 { "esi", offsetof(CPUState
, regs
[6]) },
1981 { "edi", offsetof(CPUState
, regs
[7]) },
1982 #ifdef TARGET_X86_64
1983 { "r8", offsetof(CPUState
, regs
[8]) },
1984 { "r9", offsetof(CPUState
, regs
[9]) },
1985 { "r10", offsetof(CPUState
, regs
[10]) },
1986 { "r11", offsetof(CPUState
, regs
[11]) },
1987 { "r12", offsetof(CPUState
, regs
[12]) },
1988 { "r13", offsetof(CPUState
, regs
[13]) },
1989 { "r14", offsetof(CPUState
, regs
[14]) },
1990 { "r15", offsetof(CPUState
, regs
[15]) },
1992 { "eflags", offsetof(CPUState
, eflags
) },
1993 { "eip", offsetof(CPUState
, eip
) },
2000 { "pc", 0, monitor_get_pc
, },
2001 #elif defined(TARGET_PPC)
2002 /* General purpose registers */
2003 { "r0", offsetof(CPUState
, gpr
[0]) },
2004 { "r1", offsetof(CPUState
, gpr
[1]) },
2005 { "r2", offsetof(CPUState
, gpr
[2]) },
2006 { "r3", offsetof(CPUState
, gpr
[3]) },
2007 { "r4", offsetof(CPUState
, gpr
[4]) },
2008 { "r5", offsetof(CPUState
, gpr
[5]) },
2009 { "r6", offsetof(CPUState
, gpr
[6]) },
2010 { "r7", offsetof(CPUState
, gpr
[7]) },
2011 { "r8", offsetof(CPUState
, gpr
[8]) },
2012 { "r9", offsetof(CPUState
, gpr
[9]) },
2013 { "r10", offsetof(CPUState
, gpr
[10]) },
2014 { "r11", offsetof(CPUState
, gpr
[11]) },
2015 { "r12", offsetof(CPUState
, gpr
[12]) },
2016 { "r13", offsetof(CPUState
, gpr
[13]) },
2017 { "r14", offsetof(CPUState
, gpr
[14]) },
2018 { "r15", offsetof(CPUState
, gpr
[15]) },
2019 { "r16", offsetof(CPUState
, gpr
[16]) },
2020 { "r17", offsetof(CPUState
, gpr
[17]) },
2021 { "r18", offsetof(CPUState
, gpr
[18]) },
2022 { "r19", offsetof(CPUState
, gpr
[19]) },
2023 { "r20", offsetof(CPUState
, gpr
[20]) },
2024 { "r21", offsetof(CPUState
, gpr
[21]) },
2025 { "r22", offsetof(CPUState
, gpr
[22]) },
2026 { "r23", offsetof(CPUState
, gpr
[23]) },
2027 { "r24", offsetof(CPUState
, gpr
[24]) },
2028 { "r25", offsetof(CPUState
, gpr
[25]) },
2029 { "r26", offsetof(CPUState
, gpr
[26]) },
2030 { "r27", offsetof(CPUState
, gpr
[27]) },
2031 { "r28", offsetof(CPUState
, gpr
[28]) },
2032 { "r29", offsetof(CPUState
, gpr
[29]) },
2033 { "r30", offsetof(CPUState
, gpr
[30]) },
2034 { "r31", offsetof(CPUState
, gpr
[31]) },
2035 /* Floating point registers */
2036 { "f0", offsetof(CPUState
, fpr
[0]) },
2037 { "f1", offsetof(CPUState
, fpr
[1]) },
2038 { "f2", offsetof(CPUState
, fpr
[2]) },
2039 { "f3", offsetof(CPUState
, fpr
[3]) },
2040 { "f4", offsetof(CPUState
, fpr
[4]) },
2041 { "f5", offsetof(CPUState
, fpr
[5]) },
2042 { "f6", offsetof(CPUState
, fpr
[6]) },
2043 { "f7", offsetof(CPUState
, fpr
[7]) },
2044 { "f8", offsetof(CPUState
, fpr
[8]) },
2045 { "f9", offsetof(CPUState
, fpr
[9]) },
2046 { "f10", offsetof(CPUState
, fpr
[10]) },
2047 { "f11", offsetof(CPUState
, fpr
[11]) },
2048 { "f12", offsetof(CPUState
, fpr
[12]) },
2049 { "f13", offsetof(CPUState
, fpr
[13]) },
2050 { "f14", offsetof(CPUState
, fpr
[14]) },
2051 { "f15", offsetof(CPUState
, fpr
[15]) },
2052 { "f16", offsetof(CPUState
, fpr
[16]) },
2053 { "f17", offsetof(CPUState
, fpr
[17]) },
2054 { "f18", offsetof(CPUState
, fpr
[18]) },
2055 { "f19", offsetof(CPUState
, fpr
[19]) },
2056 { "f20", offsetof(CPUState
, fpr
[20]) },
2057 { "f21", offsetof(CPUState
, fpr
[21]) },
2058 { "f22", offsetof(CPUState
, fpr
[22]) },
2059 { "f23", offsetof(CPUState
, fpr
[23]) },
2060 { "f24", offsetof(CPUState
, fpr
[24]) },
2061 { "f25", offsetof(CPUState
, fpr
[25]) },
2062 { "f26", offsetof(CPUState
, fpr
[26]) },
2063 { "f27", offsetof(CPUState
, fpr
[27]) },
2064 { "f28", offsetof(CPUState
, fpr
[28]) },
2065 { "f29", offsetof(CPUState
, fpr
[29]) },
2066 { "f30", offsetof(CPUState
, fpr
[30]) },
2067 { "f31", offsetof(CPUState
, fpr
[31]) },
2068 { "fpscr", offsetof(CPUState
, fpscr
) },
2069 /* Next instruction pointer */
2070 { "nip|pc", offsetof(CPUState
, nip
) },
2071 { "lr", offsetof(CPUState
, lr
) },
2072 { "ctr", offsetof(CPUState
, ctr
) },
2073 { "decr", 0, &monitor_get_decr
, },
2074 { "ccr", 0, &monitor_get_ccr
, },
2075 /* Machine state register */
2076 { "msr", 0, &monitor_get_msr
, },
2077 { "xer", 0, &monitor_get_xer
, },
2078 { "tbu", 0, &monitor_get_tbu
, },
2079 { "tbl", 0, &monitor_get_tbl
, },
2080 #if defined(TARGET_PPC64)
2081 /* Address space register */
2082 { "asr", offsetof(CPUState
, asr
) },
2084 /* Segment registers */
2085 { "sdr1", offsetof(CPUState
, sdr1
) },
2086 { "sr0", offsetof(CPUState
, sr
[0]) },
2087 { "sr1", offsetof(CPUState
, sr
[1]) },
2088 { "sr2", offsetof(CPUState
, sr
[2]) },
2089 { "sr3", offsetof(CPUState
, sr
[3]) },
2090 { "sr4", offsetof(CPUState
, sr
[4]) },
2091 { "sr5", offsetof(CPUState
, sr
[5]) },
2092 { "sr6", offsetof(CPUState
, sr
[6]) },
2093 { "sr7", offsetof(CPUState
, sr
[7]) },
2094 { "sr8", offsetof(CPUState
, sr
[8]) },
2095 { "sr9", offsetof(CPUState
, sr
[9]) },
2096 { "sr10", offsetof(CPUState
, sr
[10]) },
2097 { "sr11", offsetof(CPUState
, sr
[11]) },
2098 { "sr12", offsetof(CPUState
, sr
[12]) },
2099 { "sr13", offsetof(CPUState
, sr
[13]) },
2100 { "sr14", offsetof(CPUState
, sr
[14]) },
2101 { "sr15", offsetof(CPUState
, sr
[15]) },
2102 /* Too lazy to put BATs and SPRs ... */
2103 #elif defined(TARGET_SPARC)
2104 { "g0", offsetof(CPUState
, gregs
[0]) },
2105 { "g1", offsetof(CPUState
, gregs
[1]) },
2106 { "g2", offsetof(CPUState
, gregs
[2]) },
2107 { "g3", offsetof(CPUState
, gregs
[3]) },
2108 { "g4", offsetof(CPUState
, gregs
[4]) },
2109 { "g5", offsetof(CPUState
, gregs
[5]) },
2110 { "g6", offsetof(CPUState
, gregs
[6]) },
2111 { "g7", offsetof(CPUState
, gregs
[7]) },
2112 { "o0", 0, monitor_get_reg
},
2113 { "o1", 1, monitor_get_reg
},
2114 { "o2", 2, monitor_get_reg
},
2115 { "o3", 3, monitor_get_reg
},
2116 { "o4", 4, monitor_get_reg
},
2117 { "o5", 5, monitor_get_reg
},
2118 { "o6", 6, monitor_get_reg
},
2119 { "o7", 7, monitor_get_reg
},
2120 { "l0", 8, monitor_get_reg
},
2121 { "l1", 9, monitor_get_reg
},
2122 { "l2", 10, monitor_get_reg
},
2123 { "l3", 11, monitor_get_reg
},
2124 { "l4", 12, monitor_get_reg
},
2125 { "l5", 13, monitor_get_reg
},
2126 { "l6", 14, monitor_get_reg
},
2127 { "l7", 15, monitor_get_reg
},
2128 { "i0", 16, monitor_get_reg
},
2129 { "i1", 17, monitor_get_reg
},
2130 { "i2", 18, monitor_get_reg
},
2131 { "i3", 19, monitor_get_reg
},
2132 { "i4", 20, monitor_get_reg
},
2133 { "i5", 21, monitor_get_reg
},
2134 { "i6", 22, monitor_get_reg
},
2135 { "i7", 23, monitor_get_reg
},
2136 { "pc", offsetof(CPUState
, pc
) },
2137 { "npc", offsetof(CPUState
, npc
) },
2138 { "y", offsetof(CPUState
, y
) },
2139 #ifndef TARGET_SPARC64
2140 { "psr", 0, &monitor_get_psr
, },
2141 { "wim", offsetof(CPUState
, wim
) },
2143 { "tbr", offsetof(CPUState
, tbr
) },
2144 { "fsr", offsetof(CPUState
, fsr
) },
2145 { "f0", offsetof(CPUState
, fpr
[0]) },
2146 { "f1", offsetof(CPUState
, fpr
[1]) },
2147 { "f2", offsetof(CPUState
, fpr
[2]) },
2148 { "f3", offsetof(CPUState
, fpr
[3]) },
2149 { "f4", offsetof(CPUState
, fpr
[4]) },
2150 { "f5", offsetof(CPUState
, fpr
[5]) },
2151 { "f6", offsetof(CPUState
, fpr
[6]) },
2152 { "f7", offsetof(CPUState
, fpr
[7]) },
2153 { "f8", offsetof(CPUState
, fpr
[8]) },
2154 { "f9", offsetof(CPUState
, fpr
[9]) },
2155 { "f10", offsetof(CPUState
, fpr
[10]) },
2156 { "f11", offsetof(CPUState
, fpr
[11]) },
2157 { "f12", offsetof(CPUState
, fpr
[12]) },
2158 { "f13", offsetof(CPUState
, fpr
[13]) },
2159 { "f14", offsetof(CPUState
, fpr
[14]) },
2160 { "f15", offsetof(CPUState
, fpr
[15]) },
2161 { "f16", offsetof(CPUState
, fpr
[16]) },
2162 { "f17", offsetof(CPUState
, fpr
[17]) },
2163 { "f18", offsetof(CPUState
, fpr
[18]) },
2164 { "f19", offsetof(CPUState
, fpr
[19]) },
2165 { "f20", offsetof(CPUState
, fpr
[20]) },
2166 { "f21", offsetof(CPUState
, fpr
[21]) },
2167 { "f22", offsetof(CPUState
, fpr
[22]) },
2168 { "f23", offsetof(CPUState
, fpr
[23]) },
2169 { "f24", offsetof(CPUState
, fpr
[24]) },
2170 { "f25", offsetof(CPUState
, fpr
[25]) },
2171 { "f26", offsetof(CPUState
, fpr
[26]) },
2172 { "f27", offsetof(CPUState
, fpr
[27]) },
2173 { "f28", offsetof(CPUState
, fpr
[28]) },
2174 { "f29", offsetof(CPUState
, fpr
[29]) },
2175 { "f30", offsetof(CPUState
, fpr
[30]) },
2176 { "f31", offsetof(CPUState
, fpr
[31]) },
2177 #ifdef TARGET_SPARC64
2178 { "f32", offsetof(CPUState
, fpr
[32]) },
2179 { "f34", offsetof(CPUState
, fpr
[34]) },
2180 { "f36", offsetof(CPUState
, fpr
[36]) },
2181 { "f38", offsetof(CPUState
, fpr
[38]) },
2182 { "f40", offsetof(CPUState
, fpr
[40]) },
2183 { "f42", offsetof(CPUState
, fpr
[42]) },
2184 { "f44", offsetof(CPUState
, fpr
[44]) },
2185 { "f46", offsetof(CPUState
, fpr
[46]) },
2186 { "f48", offsetof(CPUState
, fpr
[48]) },
2187 { "f50", offsetof(CPUState
, fpr
[50]) },
2188 { "f52", offsetof(CPUState
, fpr
[52]) },
2189 { "f54", offsetof(CPUState
, fpr
[54]) },
2190 { "f56", offsetof(CPUState
, fpr
[56]) },
2191 { "f58", offsetof(CPUState
, fpr
[58]) },
2192 { "f60", offsetof(CPUState
, fpr
[60]) },
2193 { "f62", offsetof(CPUState
, fpr
[62]) },
2194 { "asi", offsetof(CPUState
, asi
) },
2195 { "pstate", offsetof(CPUState
, pstate
) },
2196 { "cansave", offsetof(CPUState
, cansave
) },
2197 { "canrestore", offsetof(CPUState
, canrestore
) },
2198 { "otherwin", offsetof(CPUState
, otherwin
) },
2199 { "wstate", offsetof(CPUState
, wstate
) },
2200 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2201 { "fprs", offsetof(CPUState
, fprs
) },
2207 static void expr_error(Monitor
*mon
, const char *msg
)
2209 monitor_printf(mon
, "%s\n", msg
);
2210 longjmp(expr_env
, 1);
2213 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2214 static int get_monitor_def(target_long
*pval
, const char *name
)
2216 const MonitorDef
*md
;
2219 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2220 if (compare_cmd(name
, md
->name
)) {
2221 if (md
->get_value
) {
2222 *pval
= md
->get_value(md
, md
->offset
);
2224 CPUState
*env
= mon_get_cpu();
2227 ptr
= (uint8_t *)env
+ md
->offset
;
2230 *pval
= *(int32_t *)ptr
;
2233 *pval
= *(target_long
*)ptr
;
2246 static void next(void)
2250 while (qemu_isspace(*pch
))
2255 static int64_t expr_sum(Monitor
*mon
);
2257 static int64_t expr_unary(Monitor
*mon
)
2266 n
= expr_unary(mon
);
2270 n
= -expr_unary(mon
);
2274 n
= ~expr_unary(mon
);
2280 expr_error(mon
, "')' expected");
2287 expr_error(mon
, "character constant expected");
2291 expr_error(mon
, "missing terminating \' character");
2301 while ((*pch
>= 'a' && *pch
<= 'z') ||
2302 (*pch
>= 'A' && *pch
<= 'Z') ||
2303 (*pch
>= '0' && *pch
<= '9') ||
2304 *pch
== '_' || *pch
== '.') {
2305 if ((q
- buf
) < sizeof(buf
) - 1)
2309 while (qemu_isspace(*pch
))
2312 ret
= get_monitor_def(®
, buf
);
2314 expr_error(mon
, "unknown register");
2316 expr_error(mon
, "no cpu defined");
2321 expr_error(mon
, "unexpected end of expression");
2325 #if TARGET_PHYS_ADDR_BITS > 32
2326 n
= strtoull(pch
, &p
, 0);
2328 n
= strtoul(pch
, &p
, 0);
2331 expr_error(mon
, "invalid char in expression");
2334 while (qemu_isspace(*pch
))
2342 static int64_t expr_prod(Monitor
*mon
)
2347 val
= expr_unary(mon
);
2350 if (op
!= '*' && op
!= '/' && op
!= '%')
2353 val2
= expr_unary(mon
);
2362 expr_error(mon
, "division by zero");
2373 static int64_t expr_logic(Monitor
*mon
)
2378 val
= expr_prod(mon
);
2381 if (op
!= '&' && op
!= '|' && op
!= '^')
2384 val2
= expr_prod(mon
);
2401 static int64_t expr_sum(Monitor
*mon
)
2406 val
= expr_logic(mon
);
2409 if (op
!= '+' && op
!= '-')
2412 val2
= expr_logic(mon
);
2421 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2424 if (setjmp(expr_env
)) {
2428 while (qemu_isspace(*pch
))
2430 *pval
= expr_sum(mon
);
2435 static int get_str(char *buf
, int buf_size
, const char **pp
)
2443 while (qemu_isspace(*p
))
2453 while (*p
!= '\0' && *p
!= '\"') {
2469 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2472 if ((q
- buf
) < buf_size
- 1) {
2476 if ((q
- buf
) < buf_size
- 1) {
2483 qemu_printf("unterminated string\n");
2488 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2489 if ((q
- buf
) < buf_size
- 1) {
2500 static int default_fmt_format
= 'x';
2501 static int default_fmt_size
= 4;
2505 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2507 const char *p
, *pstart
, *typestr
;
2509 int c
, nb_args
, len
, i
, has_arg
;
2510 const mon_cmd_t
*cmd
;
2513 void *str_allocated
[MAX_ARGS
];
2514 void *args
[MAX_ARGS
];
2515 void (*handler_0
)(Monitor
*mon
);
2516 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2517 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2518 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2519 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2521 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2522 void *arg3
, void *arg4
);
2523 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2524 void *arg3
, void *arg4
, void *arg5
);
2525 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2526 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2529 monitor_printf(mon
, "command='%s'\n", cmdline
);
2532 /* extract the command name */
2535 while (qemu_isspace(*p
))
2540 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2543 if (len
> sizeof(cmdname
) - 1)
2544 len
= sizeof(cmdname
) - 1;
2545 memcpy(cmdname
, pstart
, len
);
2546 cmdname
[len
] = '\0';
2548 /* find the command */
2549 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2550 if (compare_cmd(cmdname
, cmd
->name
))
2553 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2557 for(i
= 0; i
< MAX_ARGS
; i
++)
2558 str_allocated
[i
] = NULL
;
2560 /* parse the parameters */
2561 typestr
= cmd
->args_type
;
2576 while (qemu_isspace(*p
))
2578 if (*typestr
== '?') {
2581 /* no optional string: NULL argument */
2586 ret
= get_str(buf
, sizeof(buf
), &p
);
2590 monitor_printf(mon
, "%s: filename expected\n",
2594 monitor_printf(mon
, "%s: block device name expected\n",
2598 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2603 str
= qemu_malloc(strlen(buf
) + 1);
2604 pstrcpy(str
, sizeof(buf
), buf
);
2605 str_allocated
[nb_args
] = str
;
2607 if (nb_args
>= MAX_ARGS
) {
2609 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2612 args
[nb_args
++] = str
;
2617 int count
, format
, size
;
2619 while (qemu_isspace(*p
))
2625 if (qemu_isdigit(*p
)) {
2627 while (qemu_isdigit(*p
)) {
2628 count
= count
* 10 + (*p
- '0');
2666 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2667 monitor_printf(mon
, "invalid char in format: '%c'\n",
2672 format
= default_fmt_format
;
2673 if (format
!= 'i') {
2674 /* for 'i', not specifying a size gives -1 as size */
2676 size
= default_fmt_size
;
2677 default_fmt_size
= size
;
2679 default_fmt_format
= format
;
2682 format
= default_fmt_format
;
2683 if (format
!= 'i') {
2684 size
= default_fmt_size
;
2689 if (nb_args
+ 3 > MAX_ARGS
)
2691 args
[nb_args
++] = (void*)(long)count
;
2692 args
[nb_args
++] = (void*)(long)format
;
2693 args
[nb_args
++] = (void*)(long)size
;
2701 while (qemu_isspace(*p
))
2703 if (*typestr
== '?' || *typestr
== '.') {
2704 if (*typestr
== '?') {
2712 while (qemu_isspace(*p
))
2720 if (nb_args
>= MAX_ARGS
)
2722 args
[nb_args
++] = (void *)(long)has_arg
;
2724 if (nb_args
>= MAX_ARGS
)
2730 if (get_expr(mon
, &val
, &p
))
2734 if (nb_args
>= MAX_ARGS
)
2736 args
[nb_args
++] = (void *)(long)val
;
2738 if ((nb_args
+ 1) >= MAX_ARGS
)
2740 #if TARGET_PHYS_ADDR_BITS > 32
2741 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2743 args
[nb_args
++] = (void *)0;
2745 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2757 while (qemu_isspace(*p
))
2763 monitor_printf(mon
, "%s: unsupported option -%c\n",
2770 if (nb_args
>= MAX_ARGS
)
2772 args
[nb_args
++] = (void *)(long)has_option
;
2777 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2781 /* check that all arguments were parsed */
2782 while (qemu_isspace(*p
))
2785 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2792 handler_0
= cmd
->handler
;
2796 handler_1
= cmd
->handler
;
2797 handler_1(mon
, args
[0]);
2800 handler_2
= cmd
->handler
;
2801 handler_2(mon
, args
[0], args
[1]);
2804 handler_3
= cmd
->handler
;
2805 handler_3(mon
, args
[0], args
[1], args
[2]);
2808 handler_4
= cmd
->handler
;
2809 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2812 handler_5
= cmd
->handler
;
2813 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2816 handler_6
= cmd
->handler
;
2817 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2820 handler_7
= cmd
->handler
;
2821 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2825 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2829 for(i
= 0; i
< MAX_ARGS
; i
++)
2830 qemu_free(str_allocated
[i
]);
2834 static void cmd_completion(const char *name
, const char *list
)
2836 const char *p
, *pstart
;
2845 p
= pstart
+ strlen(pstart
);
2847 if (len
> sizeof(cmd
) - 2)
2848 len
= sizeof(cmd
) - 2;
2849 memcpy(cmd
, pstart
, len
);
2851 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2852 readline_add_completion(cur_mon
->rs
, cmd
);
2860 static void file_completion(const char *input
)
2865 char file
[1024], file_prefix
[1024];
2869 p
= strrchr(input
, '/');
2872 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2873 pstrcpy(path
, sizeof(path
), ".");
2875 input_path_len
= p
- input
+ 1;
2876 memcpy(path
, input
, input_path_len
);
2877 if (input_path_len
> sizeof(path
) - 1)
2878 input_path_len
= sizeof(path
) - 1;
2879 path
[input_path_len
] = '\0';
2880 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2882 #ifdef DEBUG_COMPLETION
2883 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2884 input
, path
, file_prefix
);
2886 ffs
= opendir(path
);
2894 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2895 memcpy(file
, input
, input_path_len
);
2896 if (input_path_len
< sizeof(file
))
2897 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2899 /* stat the file to find out if it's a directory.
2900 * In that case add a slash to speed up typing long paths
2903 if(S_ISDIR(sb
.st_mode
))
2904 pstrcat(file
, sizeof(file
), "/");
2905 readline_add_completion(cur_mon
->rs
, file
);
2911 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2913 const char *name
= bdrv_get_device_name(bs
);
2914 const char *input
= opaque
;
2916 if (input
[0] == '\0' ||
2917 !strncmp(name
, (char *)input
, strlen(input
))) {
2918 readline_add_completion(cur_mon
->rs
, name
);
2922 /* NOTE: this parser is an approximate form of the real command parser */
2923 static void parse_cmdline(const char *cmdline
,
2924 int *pnb_args
, char **args
)
2933 while (qemu_isspace(*p
))
2937 if (nb_args
>= MAX_ARGS
)
2939 ret
= get_str(buf
, sizeof(buf
), &p
);
2940 args
[nb_args
] = qemu_strdup(buf
);
2945 *pnb_args
= nb_args
;
2948 static void monitor_find_completion(const char *cmdline
)
2950 const char *cmdname
;
2951 char *args
[MAX_ARGS
];
2952 int nb_args
, i
, len
;
2953 const char *ptype
, *str
;
2954 const mon_cmd_t
*cmd
;
2957 parse_cmdline(cmdline
, &nb_args
, args
);
2958 #ifdef DEBUG_COMPLETION
2959 for(i
= 0; i
< nb_args
; i
++) {
2960 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2964 /* if the line ends with a space, it means we want to complete the
2966 len
= strlen(cmdline
);
2967 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2968 if (nb_args
>= MAX_ARGS
)
2970 args
[nb_args
++] = qemu_strdup("");
2973 /* command completion */
2978 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
2979 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2980 cmd_completion(cmdname
, cmd
->name
);
2983 /* find the command */
2984 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2985 if (compare_cmd(args
[0], cmd
->name
))
2990 ptype
= cmd
->args_type
;
2991 for(i
= 0; i
< nb_args
- 2; i
++) {
2992 if (*ptype
!= '\0') {
2994 while (*ptype
== '?')
2998 str
= args
[nb_args
- 1];
3001 /* file completion */
3002 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3003 file_completion(str
);
3006 /* block device name completion */
3007 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3008 bdrv_iterate(block_completion_it
, (void *)str
);
3011 /* XXX: more generic ? */
3012 if (!strcmp(cmd
->name
, "info")) {
3013 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3014 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3015 cmd_completion(str
, cmd
->name
);
3017 } else if (!strcmp(cmd
->name
, "sendkey")) {
3018 char *sep
= strrchr(str
, '-');
3021 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3022 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3023 cmd_completion(str
, key
->name
);
3031 for(i
= 0; i
< nb_args
; i
++)
3035 static int monitor_can_read(void *opaque
)
3037 Monitor
*mon
= opaque
;
3039 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3042 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3044 Monitor
*old_mon
= cur_mon
;
3050 for (i
= 0; i
< size
; i
++)
3051 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3053 if (size
== 0 || buf
[size
- 1] != 0)
3054 monitor_printf(cur_mon
, "corrupted command\n");
3056 monitor_handle_command(cur_mon
, (char *)buf
);
3062 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3064 monitor_suspend(mon
);
3065 monitor_handle_command(mon
, cmdline
);
3066 monitor_resume(mon
);
3069 int monitor_suspend(Monitor
*mon
)
3077 void monitor_resume(Monitor
*mon
)
3081 if (--mon
->suspend_cnt
== 0)
3082 readline_show_prompt(mon
->rs
);
3085 static void monitor_event(void *opaque
, int event
)
3087 Monitor
*mon
= opaque
;
3090 case CHR_EVENT_MUX_IN
:
3091 readline_restart(mon
->rs
);
3092 monitor_resume(mon
);
3096 case CHR_EVENT_MUX_OUT
:
3097 if (mon
->suspend_cnt
== 0)
3098 monitor_printf(mon
, "\n");
3100 monitor_suspend(mon
);
3103 case CHR_EVENT_RESET
:
3104 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3105 "information\n", QEMU_VERSION
);
3106 if (mon
->chr
->focus
== 0)
3107 readline_show_prompt(mon
->rs
);
3121 void monitor_init(CharDriverState
*chr
, int flags
)
3123 static int is_first_init
= 1;
3126 if (is_first_init
) {
3127 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3131 mon
= qemu_mallocz(sizeof(*mon
));
3135 if (mon
->chr
->focus
!= 0)
3136 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3137 if (flags
& MONITOR_USE_READLINE
) {
3138 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3139 monitor_read_command(mon
, 0);
3142 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3145 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3146 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3150 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3152 BlockDriverState
*bs
= opaque
;
3155 if (bdrv_set_key(bs
, password
) != 0) {
3156 monitor_printf(mon
, "invalid password\n");
3159 if (mon
->password_completion_cb
)
3160 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3162 monitor_read_command(mon
, 1);
3165 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3166 BlockDriverCompletionFunc
*completion_cb
,
3171 if (!bdrv_key_required(bs
)) {
3173 completion_cb(opaque
, 0);
3177 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3178 bdrv_get_encrypted_filename(bs
));
3180 mon
->password_completion_cb
= completion_cb
;
3181 mon
->password_opaque
= opaque
;
3183 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3185 if (err
&& completion_cb
)
3186 completion_cb(opaque
, err
);