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 all types, except '/')
65 * '.' other form of optional type (for 'i' and 'l')
66 * '-' optional parameter (eg. '-f')
70 typedef struct mon_cmd_t
{
72 const char *args_type
;
78 /* file descriptors passed via SCM_RIGHTS */
79 typedef struct mon_fd_t mon_fd_t
;
83 QLIST_ENTRY(mon_fd_t
) next
;
96 BlockDriverCompletionFunc
*password_completion_cb
;
97 void *password_opaque
;
98 QLIST_HEAD(,mon_fd_t
) fds
;
99 QLIST_ENTRY(Monitor
) entry
;
102 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
104 static const mon_cmd_t mon_cmds
[];
105 static const mon_cmd_t info_cmds
[];
107 Monitor
*cur_mon
= NULL
;
109 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
112 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
114 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
116 readline_show_prompt(mon
->rs
);
119 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
123 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
124 /* prompt is printed on return from the command handler */
127 monitor_printf(mon
, "terminal does not support password prompting\n");
132 void monitor_flush(Monitor
*mon
)
134 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
135 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
136 mon
->outbuf_index
= 0;
140 /* flush at every end of line or if the buffer is full */
141 static void monitor_puts(Monitor
*mon
, const char *str
)
153 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
154 mon
->outbuf
[mon
->outbuf_index
++] = c
;
155 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
161 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
164 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
165 monitor_puts(mon
, buf
);
168 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
172 monitor_vprintf(mon
, fmt
, ap
);
176 void monitor_print_filename(Monitor
*mon
, const char *filename
)
180 for (i
= 0; filename
[i
]; i
++) {
181 switch (filename
[i
]) {
185 monitor_printf(mon
, "\\%c", filename
[i
]);
188 monitor_printf(mon
, "\\t");
191 monitor_printf(mon
, "\\r");
194 monitor_printf(mon
, "\\n");
197 monitor_printf(mon
, "%c", filename
[i
]);
203 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
207 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
212 static int compare_cmd(const char *name
, const char *list
)
214 const char *p
, *pstart
;
222 p
= pstart
+ strlen(pstart
);
223 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
232 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
233 const char *prefix
, const char *name
)
235 const mon_cmd_t
*cmd
;
237 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
238 if (!name
|| !strcmp(name
, cmd
->name
))
239 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
240 cmd
->params
, cmd
->help
);
244 static void help_cmd(Monitor
*mon
, const char *name
)
246 if (name
&& !strcmp(name
, "info")) {
247 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
249 help_cmd_dump(mon
, mon_cmds
, "", name
);
250 if (name
&& !strcmp(name
, "log")) {
251 const CPULogItem
*item
;
252 monitor_printf(mon
, "Log items (comma separated):\n");
253 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
254 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
255 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
261 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
263 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
266 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
270 const char *device
= qdict_get_str(qdict
, "device");
272 all_devices
= !strcmp(device
, "all");
273 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
275 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
277 bdrv_commit(dinfo
->bdrv
);
281 static void do_info(Monitor
*mon
, const QDict
*qdict
)
283 const mon_cmd_t
*cmd
;
284 const char *item
= qdict_get_try_str(qdict
, "item");
285 void (*handler
)(Monitor
*);
289 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
290 if (compare_cmd(item
, cmd
->name
))
294 help_cmd(mon
, "info");
297 handler
= cmd
->handler
;
301 static void do_info_version(Monitor
*mon
)
303 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
306 static void do_info_name(Monitor
*mon
)
309 monitor_printf(mon
, "%s\n", qemu_name
);
312 #if defined(TARGET_I386)
313 static void do_info_hpet(Monitor
*mon
)
315 monitor_printf(mon
, "HPET is %s by QEMU\n",
316 (no_hpet
) ? "disabled" : "enabled");
320 static void do_info_uuid(Monitor
*mon
)
322 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
323 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
324 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
325 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
326 qemu_uuid
[14], qemu_uuid
[15]);
329 /* get the current CPU defined by the user */
330 static int mon_set_cpu(int cpu_index
)
334 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
335 if (env
->cpu_index
== cpu_index
) {
336 cur_mon
->mon_cpu
= env
;
343 static CPUState
*mon_get_cpu(void)
345 if (!cur_mon
->mon_cpu
) {
348 cpu_synchronize_state(cur_mon
->mon_cpu
);
349 return cur_mon
->mon_cpu
;
352 static void do_info_registers(Monitor
*mon
)
359 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
362 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
367 static void do_info_cpus(Monitor
*mon
)
371 /* just to set the default cpu if not already done */
374 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
375 cpu_synchronize_state(env
);
376 monitor_printf(mon
, "%c CPU #%d:",
377 (env
== mon
->mon_cpu
) ? '*' : ' ',
379 #if defined(TARGET_I386)
380 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
381 env
->eip
+ env
->segs
[R_CS
].base
);
382 #elif defined(TARGET_PPC)
383 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
384 #elif defined(TARGET_SPARC)
385 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
387 #elif defined(TARGET_MIPS)
388 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
391 monitor_printf(mon
, " (halted)");
392 monitor_printf(mon
, "\n");
396 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
398 int index
= qdict_get_int(qdict
, "index");
399 if (mon_set_cpu(index
) < 0)
400 monitor_printf(mon
, "Invalid CPU index\n");
403 static void do_info_jit(Monitor
*mon
)
405 dump_exec_info((FILE *)mon
, monitor_fprintf
);
408 static void do_info_history(Monitor
*mon
)
417 str
= readline_get_history(mon
->rs
, i
);
420 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
425 #if defined(TARGET_PPC)
426 /* XXX: not implemented in other targets */
427 static void do_info_cpu_stats(Monitor
*mon
)
432 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
436 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
441 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
443 if (bdrv_is_inserted(bs
)) {
445 if (!bdrv_is_removable(bs
)) {
446 monitor_printf(mon
, "device is not removable\n");
449 if (bdrv_is_locked(bs
)) {
450 monitor_printf(mon
, "device is locked\n");
459 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
461 BlockDriverState
*bs
;
462 int force
= qdict_get_int(qdict
, "force");
463 const char *filename
= qdict_get_str(qdict
, "filename");
465 bs
= bdrv_find(filename
);
467 monitor_printf(mon
, "device not found\n");
470 eject_device(mon
, bs
, force
);
473 static void do_change_block(Monitor
*mon
, const char *device
,
474 const char *filename
, const char *fmt
)
476 BlockDriverState
*bs
;
477 BlockDriver
*drv
= NULL
;
479 bs
= bdrv_find(device
);
481 monitor_printf(mon
, "device not found\n");
485 drv
= bdrv_find_format(fmt
);
487 monitor_printf(mon
, "invalid format %s\n", fmt
);
491 if (eject_device(mon
, bs
, 0) < 0)
493 bdrv_open2(bs
, filename
, 0, drv
);
494 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
497 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
500 if (vnc_display_password(NULL
, password
) < 0)
501 monitor_printf(mon
, "could not set VNC server password\n");
503 monitor_read_command(mon
, 1);
506 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
508 if (strcmp(target
, "passwd") == 0 ||
509 strcmp(target
, "password") == 0) {
512 strncpy(password
, arg
, sizeof(password
));
513 password
[sizeof(password
) - 1] = '\0';
514 change_vnc_password_cb(mon
, password
, NULL
);
516 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
519 if (vnc_display_open(NULL
, target
) < 0)
520 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
524 static void do_change(Monitor
*mon
, const QDict
*qdict
)
526 const char *device
= qdict_get_str(qdict
, "device");
527 const char *target
= qdict_get_str(qdict
, "target");
528 const char *arg
= qdict_get_try_str(qdict
, "arg");
529 if (strcmp(device
, "vnc") == 0) {
530 do_change_vnc(mon
, target
, arg
);
532 do_change_block(mon
, device
, target
, arg
);
536 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
538 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
541 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
543 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
546 static void do_log(Monitor
*mon
, const QDict
*qdict
)
549 const char *items
= qdict_get_str(qdict
, "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 QDict
*qdict
)
565 const char *option
= qdict_get_try_str(qdict
, "option");
566 if (!option
|| !strcmp(option
, "on")) {
568 } else if (!strcmp(option
, "off")) {
571 monitor_printf(mon
, "unexpected option %s\n", option
);
575 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
577 vm_stop(EXCP_INTERRUPT
);
580 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
582 struct bdrv_iterate_context
{
587 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
589 struct bdrv_iterate_context context
= { mon
, 0 };
591 bdrv_iterate(encrypted_bdrv_it
, &context
);
592 /* only resume the vm if all keys are set and valid */
597 static void bdrv_key_cb(void *opaque
, int err
)
599 Monitor
*mon
= opaque
;
601 /* another key was set successfully, retry to continue */
606 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
608 struct bdrv_iterate_context
*context
= opaque
;
610 if (!context
->err
&& bdrv_key_required(bs
)) {
611 context
->err
= -EBUSY
;
612 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
617 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
619 const char *device
= qdict_get_try_str(qdict
, "device");
621 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
622 if (gdbserver_start(device
) < 0) {
623 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
625 } else if (strcmp(device
, "none") == 0) {
626 monitor_printf(mon
, "Disabled gdbserver\n");
628 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
633 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
635 const char *action
= qdict_get_str(qdict
, "action");
636 if (select_watchdog_action(action
) == -1) {
637 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
641 static void monitor_printc(Monitor
*mon
, int c
)
643 monitor_printf(mon
, "'");
646 monitor_printf(mon
, "\\'");
649 monitor_printf(mon
, "\\\\");
652 monitor_printf(mon
, "\\n");
655 monitor_printf(mon
, "\\r");
658 if (c
>= 32 && c
<= 126) {
659 monitor_printf(mon
, "%c", c
);
661 monitor_printf(mon
, "\\x%02x", c
);
665 monitor_printf(mon
, "'");
668 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
669 target_phys_addr_t addr
, int is_physical
)
672 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
680 if (!env
&& !is_physical
)
685 } else if (wsize
== 4) {
688 /* as default we use the current CS size */
692 if ((env
->efer
& MSR_EFER_LMA
) &&
693 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
697 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
702 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
711 nb_per_line
= line_size
/ wsize
;
716 max_digits
= (wsize
* 8 + 2) / 3;
720 max_digits
= (wsize
* 8) / 4;
724 max_digits
= (wsize
* 8 * 10 + 32) / 33;
733 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
735 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
740 cpu_physical_memory_rw(addr
, buf
, l
, 0);
745 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
746 monitor_printf(mon
, " Cannot access memory\n");
755 v
= ldub_raw(buf
+ i
);
758 v
= lduw_raw(buf
+ i
);
761 v
= (uint32_t)ldl_raw(buf
+ i
);
764 v
= ldq_raw(buf
+ i
);
767 monitor_printf(mon
, " ");
770 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
773 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
776 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
779 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
782 monitor_printc(mon
, v
);
787 monitor_printf(mon
, "\n");
793 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
795 int count
= qdict_get_int(qdict
, "count");
796 int format
= qdict_get_int(qdict
, "format");
797 int size
= qdict_get_int(qdict
, "size");
798 target_long addr
= qdict_get_int(qdict
, "addr");
800 memory_dump(mon
, count
, format
, size
, addr
, 0);
803 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
805 int count
= qdict_get_int(qdict
, "count");
806 int format
= qdict_get_int(qdict
, "format");
807 int size
= qdict_get_int(qdict
, "size");
808 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
810 memory_dump(mon
, count
, format
, size
, addr
, 1);
813 static void do_print(Monitor
*mon
, const QDict
*qdict
)
815 int format
= qdict_get_int(qdict
, "format");
816 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
818 #if TARGET_PHYS_ADDR_BITS == 32
821 monitor_printf(mon
, "%#o", val
);
824 monitor_printf(mon
, "%#x", val
);
827 monitor_printf(mon
, "%u", val
);
831 monitor_printf(mon
, "%d", val
);
834 monitor_printc(mon
, val
);
840 monitor_printf(mon
, "%#" PRIo64
, val
);
843 monitor_printf(mon
, "%#" PRIx64
, val
);
846 monitor_printf(mon
, "%" PRIu64
, val
);
850 monitor_printf(mon
, "%" PRId64
, val
);
853 monitor_printc(mon
, val
);
857 monitor_printf(mon
, "\n");
860 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
863 uint32_t size
= qdict_get_int(qdict
, "size");
864 const char *filename
= qdict_get_str(qdict
, "filename");
865 target_long addr
= qdict_get_int(qdict
, "val");
874 f
= fopen(filename
, "wb");
876 monitor_printf(mon
, "could not open '%s'\n", filename
);
883 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
884 fwrite(buf
, 1, l
, f
);
891 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
896 uint32_t size
= qdict_get_int(qdict
, "size");
897 const char *filename
= qdict_get_str(qdict
, "filename");
898 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
900 f
= fopen(filename
, "wb");
902 monitor_printf(mon
, "could not open '%s'\n", filename
);
909 cpu_physical_memory_rw(addr
, buf
, l
, 0);
910 fwrite(buf
, 1, l
, f
);
918 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
923 uint32_t start
= qdict_get_int(qdict
, "start");
924 uint32_t size
= qdict_get_int(qdict
, "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 QDict
*qdict
)
1118 char keyname_buf
[16];
1120 int keyname_len
, keycode
, i
;
1121 const char *string
= qdict_get_str(qdict
, "string");
1122 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1123 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1125 if (nb_pending_keycodes
> 0) {
1126 qemu_del_timer(key_timer
);
1133 separator
= strchr(string
, '-');
1134 keyname_len
= separator
? separator
- string
: strlen(string
);
1135 if (keyname_len
> 0) {
1136 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1137 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1138 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1141 if (i
== MAX_KEYCODES
) {
1142 monitor_printf(mon
, "too many keys\n");
1145 keyname_buf
[keyname_len
] = 0;
1146 keycode
= get_keycode(keyname_buf
);
1148 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1151 keycodes
[i
++] = keycode
;
1155 string
= separator
+ 1;
1157 nb_pending_keycodes
= i
;
1158 /* key down events */
1159 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1160 keycode
= keycodes
[i
];
1162 kbd_put_keycode(0xe0);
1163 kbd_put_keycode(keycode
& 0x7f);
1165 /* delayed key up events */
1166 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1167 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1170 static int mouse_button_state
;
1172 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1175 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1176 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1177 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1178 dx
= strtol(dx_str
, NULL
, 0);
1179 dy
= strtol(dy_str
, NULL
, 0);
1182 dz
= strtol(dz_str
, NULL
, 0);
1183 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1186 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1188 int button_state
= qdict_get_int(qdict
, "button_state");
1189 mouse_button_state
= button_state
;
1190 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1193 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1195 int size
= qdict_get_int(qdict
, "size");
1196 int addr
= qdict_get_int(qdict
, "addr");
1197 int has_index
= qdict_haskey(qdict
, "index");
1202 int index
= qdict_get_int(qdict
, "index");
1203 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1211 val
= cpu_inb(addr
);
1215 val
= cpu_inw(addr
);
1219 val
= cpu_inl(addr
);
1223 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1224 suffix
, addr
, size
* 2, val
);
1227 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1229 int size
= qdict_get_int(qdict
, "size");
1230 int addr
= qdict_get_int(qdict
, "addr");
1231 int val
= qdict_get_int(qdict
, "val");
1233 addr
&= IOPORTS_MASK
;
1238 cpu_outb(addr
, val
);
1241 cpu_outw(addr
, val
);
1244 cpu_outl(addr
, val
);
1249 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1252 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1254 res
= qemu_boot_set(bootdevice
);
1256 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1257 } else if (res
> 0) {
1258 monitor_printf(mon
, "setting boot device list failed\n");
1260 monitor_printf(mon
, "no function defined to set boot device list for "
1261 "this architecture\n");
1265 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1267 qemu_system_reset_request();
1270 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1272 qemu_system_powerdown_request();
1275 #if defined(TARGET_I386)
1276 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1278 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1281 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1282 pte
& PG_PSE_MASK
? 'P' : '-',
1283 pte
& PG_DIRTY_MASK
? 'D' : '-',
1284 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1285 pte
& PG_PCD_MASK
? 'C' : '-',
1286 pte
& PG_PWT_MASK
? 'T' : '-',
1287 pte
& PG_USER_MASK
? 'U' : '-',
1288 pte
& PG_RW_MASK
? 'W' : '-');
1291 static void tlb_info(Monitor
*mon
)
1295 uint32_t pgd
, pde
, pte
;
1297 env
= mon_get_cpu();
1301 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1302 monitor_printf(mon
, "PG disabled\n");
1305 pgd
= env
->cr
[3] & ~0xfff;
1306 for(l1
= 0; l1
< 1024; l1
++) {
1307 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1308 pde
= le32_to_cpu(pde
);
1309 if (pde
& PG_PRESENT_MASK
) {
1310 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1311 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1313 for(l2
= 0; l2
< 1024; l2
++) {
1314 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1315 (uint8_t *)&pte
, 4);
1316 pte
= le32_to_cpu(pte
);
1317 if (pte
& PG_PRESENT_MASK
) {
1318 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1328 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1329 uint32_t end
, int prot
)
1332 prot1
= *plast_prot
;
1333 if (prot
!= prot1
) {
1334 if (*pstart
!= -1) {
1335 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1336 *pstart
, end
, end
- *pstart
,
1337 prot1
& PG_USER_MASK
? 'u' : '-',
1339 prot1
& PG_RW_MASK
? 'w' : '-');
1349 static void mem_info(Monitor
*mon
)
1352 int l1
, l2
, prot
, last_prot
;
1353 uint32_t pgd
, pde
, pte
, start
, end
;
1355 env
= mon_get_cpu();
1359 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1360 monitor_printf(mon
, "PG disabled\n");
1363 pgd
= env
->cr
[3] & ~0xfff;
1366 for(l1
= 0; l1
< 1024; l1
++) {
1367 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1368 pde
= le32_to_cpu(pde
);
1370 if (pde
& PG_PRESENT_MASK
) {
1371 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1372 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1373 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1375 for(l2
= 0; l2
< 1024; l2
++) {
1376 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1377 (uint8_t *)&pte
, 4);
1378 pte
= le32_to_cpu(pte
);
1379 end
= (l1
<< 22) + (l2
<< 12);
1380 if (pte
& PG_PRESENT_MASK
) {
1381 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1385 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1390 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1396 #if defined(TARGET_SH4)
1398 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1400 monitor_printf(mon
, " tlb%i:\t"
1401 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1402 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1403 "dirty=%hhu writethrough=%hhu\n",
1405 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1406 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1410 static void tlb_info(Monitor
*mon
)
1412 CPUState
*env
= mon_get_cpu();
1415 monitor_printf (mon
, "ITLB:\n");
1416 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1417 print_tlb (mon
, i
, &env
->itlb
[i
]);
1418 monitor_printf (mon
, "UTLB:\n");
1419 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1420 print_tlb (mon
, i
, &env
->utlb
[i
]);
1425 static void do_info_kvm(Monitor
*mon
)
1428 monitor_printf(mon
, "kvm support: ");
1430 monitor_printf(mon
, "enabled\n");
1432 monitor_printf(mon
, "disabled\n");
1434 monitor_printf(mon
, "kvm support: not compiled\n");
1438 static void do_info_numa(Monitor
*mon
)
1443 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1444 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1445 monitor_printf(mon
, "node %d cpus:", i
);
1446 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1447 if (env
->numa_node
== i
) {
1448 monitor_printf(mon
, " %d", env
->cpu_index
);
1451 monitor_printf(mon
, "\n");
1452 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1457 #ifdef CONFIG_PROFILER
1462 static void do_info_profile(Monitor
*mon
)
1468 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1469 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1470 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1471 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1476 static void do_info_profile(Monitor
*mon
)
1478 monitor_printf(mon
, "Internal profiler not compiled\n");
1482 /* Capture support */
1483 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1485 static void do_info_capture(Monitor
*mon
)
1490 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1491 monitor_printf(mon
, "[%d]: ", i
);
1492 s
->ops
.info (s
->opaque
);
1497 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1500 int n
= qdict_get_int(qdict
, "n");
1503 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1505 s
->ops
.destroy (s
->opaque
);
1506 QLIST_REMOVE (s
, entries
);
1513 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1515 const char *path
= qdict_get_str(qdict
, "path");
1516 int has_freq
= qdict_haskey(qdict
, "freq");
1517 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1518 int has_bits
= qdict_haskey(qdict
, "bits");
1519 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1520 int has_channels
= qdict_haskey(qdict
, "nchannels");
1521 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1524 s
= qemu_mallocz (sizeof (*s
));
1526 freq
= has_freq
? freq
: 44100;
1527 bits
= has_bits
? bits
: 16;
1528 nchannels
= has_channels
? nchannels
: 2;
1530 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1531 monitor_printf(mon
, "Faied to add wave capture\n");
1534 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1538 #if defined(TARGET_I386)
1539 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1542 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1544 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1545 if (env
->cpu_index
== cpu_index
) {
1546 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1552 static void do_info_status(Monitor
*mon
)
1556 monitor_printf(mon
, "VM status: running (single step mode)\n");
1558 monitor_printf(mon
, "VM status: running\n");
1561 monitor_printf(mon
, "VM status: paused\n");
1565 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1567 int value
= qdict_get_int(qdict
, "value");
1568 ram_addr_t target
= value
;
1569 qemu_balloon(target
<< 20);
1572 static void do_info_balloon(Monitor
*mon
)
1576 actual
= qemu_balloon_status();
1577 if (kvm_enabled() && !kvm_has_sync_mmu())
1578 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1579 "ballooning disabled\n");
1580 else if (actual
== 0)
1581 monitor_printf(mon
, "Ballooning not activated in VM\n");
1583 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1586 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1588 qemu_acl
*acl
= qemu_acl_find(name
);
1591 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1596 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1598 const char *aclname
= qdict_get_str(qdict
, "aclname");
1599 qemu_acl
*acl
= find_acl(mon
, aclname
);
1600 qemu_acl_entry
*entry
;
1604 monitor_printf(mon
, "policy: %s\n",
1605 acl
->defaultDeny
? "deny" : "allow");
1606 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1608 monitor_printf(mon
, "%d: %s %s\n", i
,
1609 entry
->deny
? "deny" : "allow", entry
->match
);
1614 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1616 const char *aclname
= qdict_get_str(qdict
, "aclname");
1617 qemu_acl
*acl
= find_acl(mon
, aclname
);
1620 qemu_acl_reset(acl
);
1621 monitor_printf(mon
, "acl: removed all rules\n");
1625 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1627 const char *aclname
= qdict_get_str(qdict
, "aclname");
1628 const char *policy
= qdict_get_str(qdict
, "policy");
1629 qemu_acl
*acl
= find_acl(mon
, aclname
);
1632 if (strcmp(policy
, "allow") == 0) {
1633 acl
->defaultDeny
= 0;
1634 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1635 } else if (strcmp(policy
, "deny") == 0) {
1636 acl
->defaultDeny
= 1;
1637 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1639 monitor_printf(mon
, "acl: unknown policy '%s', "
1640 "expected 'deny' or 'allow'\n", policy
);
1645 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1647 const char *aclname
= qdict_get_str(qdict
, "aclname");
1648 const char *match
= qdict_get_str(qdict
, "match");
1649 const char *policy
= qdict_get_str(qdict
, "policy");
1650 int has_index
= qdict_haskey(qdict
, "index");
1651 int index
= qdict_get_try_int(qdict
, "index", -1);
1652 qemu_acl
*acl
= find_acl(mon
, aclname
);
1656 if (strcmp(policy
, "allow") == 0) {
1658 } else if (strcmp(policy
, "deny") == 0) {
1661 monitor_printf(mon
, "acl: unknown policy '%s', "
1662 "expected 'deny' or 'allow'\n", policy
);
1666 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1668 ret
= qemu_acl_append(acl
, deny
, match
);
1670 monitor_printf(mon
, "acl: unable to add acl entry\n");
1672 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1676 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1678 const char *aclname
= qdict_get_str(qdict
, "aclname");
1679 const char *match
= qdict_get_str(qdict
, "match");
1680 qemu_acl
*acl
= find_acl(mon
, aclname
);
1684 ret
= qemu_acl_remove(acl
, match
);
1686 monitor_printf(mon
, "acl: no matching acl entry\n");
1688 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1692 #if defined(TARGET_I386)
1693 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1696 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1697 int bank
= qdict_get_int(qdict
, "bank");
1698 uint64_t status
= qdict_get_int(qdict
, "status");
1699 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1700 uint64_t addr
= qdict_get_int(qdict
, "addr");
1701 uint64_t misc
= qdict_get_int(qdict
, "misc");
1703 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1704 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1705 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1711 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1713 const char *fdname
= qdict_get_str(qdict
, "fdname");
1717 fd
= qemu_chr_get_msgfd(mon
->chr
);
1719 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1723 if (qemu_isdigit(fdname
[0])) {
1724 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1730 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1735 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1736 if (strcmp(monfd
->name
, fdname
) != 0) {
1745 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1746 monfd
->name
= qemu_strdup(fdname
);
1749 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1752 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1754 const char *fdname
= qdict_get_str(qdict
, "fdname");
1757 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1758 if (strcmp(monfd
->name
, fdname
) != 0) {
1762 QLIST_REMOVE(monfd
, next
);
1764 qemu_free(monfd
->name
);
1769 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1773 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1775 int saved_vm_running
= vm_running
;
1776 const char *name
= qdict_get_str(qdict
, "name");
1780 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1784 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1788 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1791 if (strcmp(monfd
->name
, fdname
) != 0) {
1797 /* caller takes ownership of fd */
1798 QLIST_REMOVE(monfd
, next
);
1799 qemu_free(monfd
->name
);
1808 static const mon_cmd_t mon_cmds
[] = {
1809 #include "qemu-monitor.h"
1813 /* Please update qemu-monitor.hx when adding or changing commands */
1814 static const mon_cmd_t info_cmds
[] = {
1815 { "version", "", do_info_version
,
1816 "", "show the version of QEMU" },
1817 { "network", "", do_info_network
,
1818 "", "show the network state" },
1819 { "chardev", "", qemu_chr_info
,
1820 "", "show the character devices" },
1821 { "block", "", bdrv_info
,
1822 "", "show the block devices" },
1823 { "blockstats", "", bdrv_info_stats
,
1824 "", "show block device statistics" },
1825 { "registers", "", do_info_registers
,
1826 "", "show the cpu registers" },
1827 { "cpus", "", do_info_cpus
,
1828 "", "show infos for each CPU" },
1829 { "history", "", do_info_history
,
1830 "", "show the command line history", },
1831 { "irq", "", irq_info
,
1832 "", "show the interrupts statistics (if available)", },
1833 { "pic", "", pic_info
,
1834 "", "show i8259 (PIC) state", },
1835 { "pci", "", pci_info
,
1836 "", "show PCI info", },
1837 #if defined(TARGET_I386) || defined(TARGET_SH4)
1838 { "tlb", "", tlb_info
,
1839 "", "show virtual to physical memory mappings", },
1841 #if defined(TARGET_I386)
1842 { "mem", "", mem_info
,
1843 "", "show the active virtual memory mappings", },
1844 { "hpet", "", do_info_hpet
,
1845 "", "show state of HPET", },
1847 { "jit", "", do_info_jit
,
1848 "", "show dynamic compiler info", },
1849 { "kvm", "", do_info_kvm
,
1850 "", "show KVM information", },
1851 { "numa", "", do_info_numa
,
1852 "", "show NUMA information", },
1853 { "usb", "", usb_info
,
1854 "", "show guest USB devices", },
1855 { "usbhost", "", usb_host_info
,
1856 "", "show host USB devices", },
1857 { "profile", "", do_info_profile
,
1858 "", "show profiling information", },
1859 { "capture", "", do_info_capture
,
1860 "", "show capture information" },
1861 { "snapshots", "", do_info_snapshots
,
1862 "", "show the currently saved VM snapshots" },
1863 { "status", "", do_info_status
,
1864 "", "show the current VM status (running|paused)" },
1865 { "pcmcia", "", pcmcia_info
,
1866 "", "show guest PCMCIA status" },
1867 { "mice", "", do_info_mice
,
1868 "", "show which guest mouse is receiving events" },
1869 { "vnc", "", do_info_vnc
,
1870 "", "show the vnc server status"},
1871 { "name", "", do_info_name
,
1872 "", "show the current VM name" },
1873 { "uuid", "", do_info_uuid
,
1874 "", "show the current VM UUID" },
1875 #if defined(TARGET_PPC)
1876 { "cpustats", "", do_info_cpu_stats
,
1877 "", "show CPU statistics", },
1879 #if defined(CONFIG_SLIRP)
1880 { "usernet", "", do_info_usernet
,
1881 "", "show user network stack connection states", },
1883 { "migrate", "", do_info_migrate
, "", "show migration status" },
1884 { "balloon", "", do_info_balloon
,
1885 "", "show balloon information" },
1886 { "qtree", "", do_info_qtree
,
1887 "", "show device tree" },
1888 { "qdm", "", do_info_qdm
,
1889 "", "show qdev device model list" },
1893 /*******************************************************************/
1895 static const char *pch
;
1896 static jmp_buf expr_env
;
1901 typedef struct MonitorDef
{
1904 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1908 #if defined(TARGET_I386)
1909 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1911 CPUState
*env
= mon_get_cpu();
1914 return env
->eip
+ env
->segs
[R_CS
].base
;
1918 #if defined(TARGET_PPC)
1919 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1921 CPUState
*env
= mon_get_cpu();
1929 for (i
= 0; i
< 8; i
++)
1930 u
|= env
->crf
[i
] << (32 - (4 * i
));
1935 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1937 CPUState
*env
= mon_get_cpu();
1943 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1945 CPUState
*env
= mon_get_cpu();
1951 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1953 CPUState
*env
= mon_get_cpu();
1956 return cpu_ppc_load_decr(env
);
1959 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1961 CPUState
*env
= mon_get_cpu();
1964 return cpu_ppc_load_tbu(env
);
1967 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1969 CPUState
*env
= mon_get_cpu();
1972 return cpu_ppc_load_tbl(env
);
1976 #if defined(TARGET_SPARC)
1977 #ifndef TARGET_SPARC64
1978 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1980 CPUState
*env
= mon_get_cpu();
1983 return GET_PSR(env
);
1987 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1989 CPUState
*env
= mon_get_cpu();
1992 return env
->regwptr
[val
];
1996 static const MonitorDef monitor_defs
[] = {
1999 #define SEG(name, seg) \
2000 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2001 { name ".base", offsetof(CPUState, segs[seg].base) },\
2002 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2004 { "eax", offsetof(CPUState
, regs
[0]) },
2005 { "ecx", offsetof(CPUState
, regs
[1]) },
2006 { "edx", offsetof(CPUState
, regs
[2]) },
2007 { "ebx", offsetof(CPUState
, regs
[3]) },
2008 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2009 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2010 { "esi", offsetof(CPUState
, regs
[6]) },
2011 { "edi", offsetof(CPUState
, regs
[7]) },
2012 #ifdef TARGET_X86_64
2013 { "r8", offsetof(CPUState
, regs
[8]) },
2014 { "r9", offsetof(CPUState
, regs
[9]) },
2015 { "r10", offsetof(CPUState
, regs
[10]) },
2016 { "r11", offsetof(CPUState
, regs
[11]) },
2017 { "r12", offsetof(CPUState
, regs
[12]) },
2018 { "r13", offsetof(CPUState
, regs
[13]) },
2019 { "r14", offsetof(CPUState
, regs
[14]) },
2020 { "r15", offsetof(CPUState
, regs
[15]) },
2022 { "eflags", offsetof(CPUState
, eflags
) },
2023 { "eip", offsetof(CPUState
, eip
) },
2030 { "pc", 0, monitor_get_pc
, },
2031 #elif defined(TARGET_PPC)
2032 /* General purpose registers */
2033 { "r0", offsetof(CPUState
, gpr
[0]) },
2034 { "r1", offsetof(CPUState
, gpr
[1]) },
2035 { "r2", offsetof(CPUState
, gpr
[2]) },
2036 { "r3", offsetof(CPUState
, gpr
[3]) },
2037 { "r4", offsetof(CPUState
, gpr
[4]) },
2038 { "r5", offsetof(CPUState
, gpr
[5]) },
2039 { "r6", offsetof(CPUState
, gpr
[6]) },
2040 { "r7", offsetof(CPUState
, gpr
[7]) },
2041 { "r8", offsetof(CPUState
, gpr
[8]) },
2042 { "r9", offsetof(CPUState
, gpr
[9]) },
2043 { "r10", offsetof(CPUState
, gpr
[10]) },
2044 { "r11", offsetof(CPUState
, gpr
[11]) },
2045 { "r12", offsetof(CPUState
, gpr
[12]) },
2046 { "r13", offsetof(CPUState
, gpr
[13]) },
2047 { "r14", offsetof(CPUState
, gpr
[14]) },
2048 { "r15", offsetof(CPUState
, gpr
[15]) },
2049 { "r16", offsetof(CPUState
, gpr
[16]) },
2050 { "r17", offsetof(CPUState
, gpr
[17]) },
2051 { "r18", offsetof(CPUState
, gpr
[18]) },
2052 { "r19", offsetof(CPUState
, gpr
[19]) },
2053 { "r20", offsetof(CPUState
, gpr
[20]) },
2054 { "r21", offsetof(CPUState
, gpr
[21]) },
2055 { "r22", offsetof(CPUState
, gpr
[22]) },
2056 { "r23", offsetof(CPUState
, gpr
[23]) },
2057 { "r24", offsetof(CPUState
, gpr
[24]) },
2058 { "r25", offsetof(CPUState
, gpr
[25]) },
2059 { "r26", offsetof(CPUState
, gpr
[26]) },
2060 { "r27", offsetof(CPUState
, gpr
[27]) },
2061 { "r28", offsetof(CPUState
, gpr
[28]) },
2062 { "r29", offsetof(CPUState
, gpr
[29]) },
2063 { "r30", offsetof(CPUState
, gpr
[30]) },
2064 { "r31", offsetof(CPUState
, gpr
[31]) },
2065 /* Floating point registers */
2066 { "f0", offsetof(CPUState
, fpr
[0]) },
2067 { "f1", offsetof(CPUState
, fpr
[1]) },
2068 { "f2", offsetof(CPUState
, fpr
[2]) },
2069 { "f3", offsetof(CPUState
, fpr
[3]) },
2070 { "f4", offsetof(CPUState
, fpr
[4]) },
2071 { "f5", offsetof(CPUState
, fpr
[5]) },
2072 { "f6", offsetof(CPUState
, fpr
[6]) },
2073 { "f7", offsetof(CPUState
, fpr
[7]) },
2074 { "f8", offsetof(CPUState
, fpr
[8]) },
2075 { "f9", offsetof(CPUState
, fpr
[9]) },
2076 { "f10", offsetof(CPUState
, fpr
[10]) },
2077 { "f11", offsetof(CPUState
, fpr
[11]) },
2078 { "f12", offsetof(CPUState
, fpr
[12]) },
2079 { "f13", offsetof(CPUState
, fpr
[13]) },
2080 { "f14", offsetof(CPUState
, fpr
[14]) },
2081 { "f15", offsetof(CPUState
, fpr
[15]) },
2082 { "f16", offsetof(CPUState
, fpr
[16]) },
2083 { "f17", offsetof(CPUState
, fpr
[17]) },
2084 { "f18", offsetof(CPUState
, fpr
[18]) },
2085 { "f19", offsetof(CPUState
, fpr
[19]) },
2086 { "f20", offsetof(CPUState
, fpr
[20]) },
2087 { "f21", offsetof(CPUState
, fpr
[21]) },
2088 { "f22", offsetof(CPUState
, fpr
[22]) },
2089 { "f23", offsetof(CPUState
, fpr
[23]) },
2090 { "f24", offsetof(CPUState
, fpr
[24]) },
2091 { "f25", offsetof(CPUState
, fpr
[25]) },
2092 { "f26", offsetof(CPUState
, fpr
[26]) },
2093 { "f27", offsetof(CPUState
, fpr
[27]) },
2094 { "f28", offsetof(CPUState
, fpr
[28]) },
2095 { "f29", offsetof(CPUState
, fpr
[29]) },
2096 { "f30", offsetof(CPUState
, fpr
[30]) },
2097 { "f31", offsetof(CPUState
, fpr
[31]) },
2098 { "fpscr", offsetof(CPUState
, fpscr
) },
2099 /* Next instruction pointer */
2100 { "nip|pc", offsetof(CPUState
, nip
) },
2101 { "lr", offsetof(CPUState
, lr
) },
2102 { "ctr", offsetof(CPUState
, ctr
) },
2103 { "decr", 0, &monitor_get_decr
, },
2104 { "ccr", 0, &monitor_get_ccr
, },
2105 /* Machine state register */
2106 { "msr", 0, &monitor_get_msr
, },
2107 { "xer", 0, &monitor_get_xer
, },
2108 { "tbu", 0, &monitor_get_tbu
, },
2109 { "tbl", 0, &monitor_get_tbl
, },
2110 #if defined(TARGET_PPC64)
2111 /* Address space register */
2112 { "asr", offsetof(CPUState
, asr
) },
2114 /* Segment registers */
2115 { "sdr1", offsetof(CPUState
, sdr1
) },
2116 { "sr0", offsetof(CPUState
, sr
[0]) },
2117 { "sr1", offsetof(CPUState
, sr
[1]) },
2118 { "sr2", offsetof(CPUState
, sr
[2]) },
2119 { "sr3", offsetof(CPUState
, sr
[3]) },
2120 { "sr4", offsetof(CPUState
, sr
[4]) },
2121 { "sr5", offsetof(CPUState
, sr
[5]) },
2122 { "sr6", offsetof(CPUState
, sr
[6]) },
2123 { "sr7", offsetof(CPUState
, sr
[7]) },
2124 { "sr8", offsetof(CPUState
, sr
[8]) },
2125 { "sr9", offsetof(CPUState
, sr
[9]) },
2126 { "sr10", offsetof(CPUState
, sr
[10]) },
2127 { "sr11", offsetof(CPUState
, sr
[11]) },
2128 { "sr12", offsetof(CPUState
, sr
[12]) },
2129 { "sr13", offsetof(CPUState
, sr
[13]) },
2130 { "sr14", offsetof(CPUState
, sr
[14]) },
2131 { "sr15", offsetof(CPUState
, sr
[15]) },
2132 /* Too lazy to put BATs and SPRs ... */
2133 #elif defined(TARGET_SPARC)
2134 { "g0", offsetof(CPUState
, gregs
[0]) },
2135 { "g1", offsetof(CPUState
, gregs
[1]) },
2136 { "g2", offsetof(CPUState
, gregs
[2]) },
2137 { "g3", offsetof(CPUState
, gregs
[3]) },
2138 { "g4", offsetof(CPUState
, gregs
[4]) },
2139 { "g5", offsetof(CPUState
, gregs
[5]) },
2140 { "g6", offsetof(CPUState
, gregs
[6]) },
2141 { "g7", offsetof(CPUState
, gregs
[7]) },
2142 { "o0", 0, monitor_get_reg
},
2143 { "o1", 1, monitor_get_reg
},
2144 { "o2", 2, monitor_get_reg
},
2145 { "o3", 3, monitor_get_reg
},
2146 { "o4", 4, monitor_get_reg
},
2147 { "o5", 5, monitor_get_reg
},
2148 { "o6", 6, monitor_get_reg
},
2149 { "o7", 7, monitor_get_reg
},
2150 { "l0", 8, monitor_get_reg
},
2151 { "l1", 9, monitor_get_reg
},
2152 { "l2", 10, monitor_get_reg
},
2153 { "l3", 11, monitor_get_reg
},
2154 { "l4", 12, monitor_get_reg
},
2155 { "l5", 13, monitor_get_reg
},
2156 { "l6", 14, monitor_get_reg
},
2157 { "l7", 15, monitor_get_reg
},
2158 { "i0", 16, monitor_get_reg
},
2159 { "i1", 17, monitor_get_reg
},
2160 { "i2", 18, monitor_get_reg
},
2161 { "i3", 19, monitor_get_reg
},
2162 { "i4", 20, monitor_get_reg
},
2163 { "i5", 21, monitor_get_reg
},
2164 { "i6", 22, monitor_get_reg
},
2165 { "i7", 23, monitor_get_reg
},
2166 { "pc", offsetof(CPUState
, pc
) },
2167 { "npc", offsetof(CPUState
, npc
) },
2168 { "y", offsetof(CPUState
, y
) },
2169 #ifndef TARGET_SPARC64
2170 { "psr", 0, &monitor_get_psr
, },
2171 { "wim", offsetof(CPUState
, wim
) },
2173 { "tbr", offsetof(CPUState
, tbr
) },
2174 { "fsr", offsetof(CPUState
, fsr
) },
2175 { "f0", offsetof(CPUState
, fpr
[0]) },
2176 { "f1", offsetof(CPUState
, fpr
[1]) },
2177 { "f2", offsetof(CPUState
, fpr
[2]) },
2178 { "f3", offsetof(CPUState
, fpr
[3]) },
2179 { "f4", offsetof(CPUState
, fpr
[4]) },
2180 { "f5", offsetof(CPUState
, fpr
[5]) },
2181 { "f6", offsetof(CPUState
, fpr
[6]) },
2182 { "f7", offsetof(CPUState
, fpr
[7]) },
2183 { "f8", offsetof(CPUState
, fpr
[8]) },
2184 { "f9", offsetof(CPUState
, fpr
[9]) },
2185 { "f10", offsetof(CPUState
, fpr
[10]) },
2186 { "f11", offsetof(CPUState
, fpr
[11]) },
2187 { "f12", offsetof(CPUState
, fpr
[12]) },
2188 { "f13", offsetof(CPUState
, fpr
[13]) },
2189 { "f14", offsetof(CPUState
, fpr
[14]) },
2190 { "f15", offsetof(CPUState
, fpr
[15]) },
2191 { "f16", offsetof(CPUState
, fpr
[16]) },
2192 { "f17", offsetof(CPUState
, fpr
[17]) },
2193 { "f18", offsetof(CPUState
, fpr
[18]) },
2194 { "f19", offsetof(CPUState
, fpr
[19]) },
2195 { "f20", offsetof(CPUState
, fpr
[20]) },
2196 { "f21", offsetof(CPUState
, fpr
[21]) },
2197 { "f22", offsetof(CPUState
, fpr
[22]) },
2198 { "f23", offsetof(CPUState
, fpr
[23]) },
2199 { "f24", offsetof(CPUState
, fpr
[24]) },
2200 { "f25", offsetof(CPUState
, fpr
[25]) },
2201 { "f26", offsetof(CPUState
, fpr
[26]) },
2202 { "f27", offsetof(CPUState
, fpr
[27]) },
2203 { "f28", offsetof(CPUState
, fpr
[28]) },
2204 { "f29", offsetof(CPUState
, fpr
[29]) },
2205 { "f30", offsetof(CPUState
, fpr
[30]) },
2206 { "f31", offsetof(CPUState
, fpr
[31]) },
2207 #ifdef TARGET_SPARC64
2208 { "f32", offsetof(CPUState
, fpr
[32]) },
2209 { "f34", offsetof(CPUState
, fpr
[34]) },
2210 { "f36", offsetof(CPUState
, fpr
[36]) },
2211 { "f38", offsetof(CPUState
, fpr
[38]) },
2212 { "f40", offsetof(CPUState
, fpr
[40]) },
2213 { "f42", offsetof(CPUState
, fpr
[42]) },
2214 { "f44", offsetof(CPUState
, fpr
[44]) },
2215 { "f46", offsetof(CPUState
, fpr
[46]) },
2216 { "f48", offsetof(CPUState
, fpr
[48]) },
2217 { "f50", offsetof(CPUState
, fpr
[50]) },
2218 { "f52", offsetof(CPUState
, fpr
[52]) },
2219 { "f54", offsetof(CPUState
, fpr
[54]) },
2220 { "f56", offsetof(CPUState
, fpr
[56]) },
2221 { "f58", offsetof(CPUState
, fpr
[58]) },
2222 { "f60", offsetof(CPUState
, fpr
[60]) },
2223 { "f62", offsetof(CPUState
, fpr
[62]) },
2224 { "asi", offsetof(CPUState
, asi
) },
2225 { "pstate", offsetof(CPUState
, pstate
) },
2226 { "cansave", offsetof(CPUState
, cansave
) },
2227 { "canrestore", offsetof(CPUState
, canrestore
) },
2228 { "otherwin", offsetof(CPUState
, otherwin
) },
2229 { "wstate", offsetof(CPUState
, wstate
) },
2230 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2231 { "fprs", offsetof(CPUState
, fprs
) },
2237 static void expr_error(Monitor
*mon
, const char *msg
)
2239 monitor_printf(mon
, "%s\n", msg
);
2240 longjmp(expr_env
, 1);
2243 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2244 static int get_monitor_def(target_long
*pval
, const char *name
)
2246 const MonitorDef
*md
;
2249 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2250 if (compare_cmd(name
, md
->name
)) {
2251 if (md
->get_value
) {
2252 *pval
= md
->get_value(md
, md
->offset
);
2254 CPUState
*env
= mon_get_cpu();
2257 ptr
= (uint8_t *)env
+ md
->offset
;
2260 *pval
= *(int32_t *)ptr
;
2263 *pval
= *(target_long
*)ptr
;
2276 static void next(void)
2280 while (qemu_isspace(*pch
))
2285 static int64_t expr_sum(Monitor
*mon
);
2287 static int64_t expr_unary(Monitor
*mon
)
2296 n
= expr_unary(mon
);
2300 n
= -expr_unary(mon
);
2304 n
= ~expr_unary(mon
);
2310 expr_error(mon
, "')' expected");
2317 expr_error(mon
, "character constant expected");
2321 expr_error(mon
, "missing terminating \' character");
2331 while ((*pch
>= 'a' && *pch
<= 'z') ||
2332 (*pch
>= 'A' && *pch
<= 'Z') ||
2333 (*pch
>= '0' && *pch
<= '9') ||
2334 *pch
== '_' || *pch
== '.') {
2335 if ((q
- buf
) < sizeof(buf
) - 1)
2339 while (qemu_isspace(*pch
))
2342 ret
= get_monitor_def(®
, buf
);
2344 expr_error(mon
, "unknown register");
2346 expr_error(mon
, "no cpu defined");
2351 expr_error(mon
, "unexpected end of expression");
2355 #if TARGET_PHYS_ADDR_BITS > 32
2356 n
= strtoull(pch
, &p
, 0);
2358 n
= strtoul(pch
, &p
, 0);
2361 expr_error(mon
, "invalid char in expression");
2364 while (qemu_isspace(*pch
))
2372 static int64_t expr_prod(Monitor
*mon
)
2377 val
= expr_unary(mon
);
2380 if (op
!= '*' && op
!= '/' && op
!= '%')
2383 val2
= expr_unary(mon
);
2392 expr_error(mon
, "division by zero");
2403 static int64_t expr_logic(Monitor
*mon
)
2408 val
= expr_prod(mon
);
2411 if (op
!= '&' && op
!= '|' && op
!= '^')
2414 val2
= expr_prod(mon
);
2431 static int64_t expr_sum(Monitor
*mon
)
2436 val
= expr_logic(mon
);
2439 if (op
!= '+' && op
!= '-')
2442 val2
= expr_logic(mon
);
2451 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2454 if (setjmp(expr_env
)) {
2458 while (qemu_isspace(*pch
))
2460 *pval
= expr_sum(mon
);
2465 static int get_str(char *buf
, int buf_size
, const char **pp
)
2473 while (qemu_isspace(*p
))
2483 while (*p
!= '\0' && *p
!= '\"') {
2499 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2502 if ((q
- buf
) < buf_size
- 1) {
2506 if ((q
- buf
) < buf_size
- 1) {
2513 qemu_printf("unterminated string\n");
2518 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2519 if ((q
- buf
) < buf_size
- 1) {
2531 * Store the command-name in cmdname, and return a pointer to
2532 * the remaining of the command string.
2534 static const char *get_command_name(const char *cmdline
,
2535 char *cmdname
, size_t nlen
)
2538 const char *p
, *pstart
;
2541 while (qemu_isspace(*p
))
2546 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2551 memcpy(cmdname
, pstart
, len
);
2552 cmdname
[len
] = '\0';
2557 * Read key of 'type' into 'key' and return the current
2560 static char *key_get_info(const char *type
, char **key
)
2568 p
= strchr(type
, ':');
2575 str
= qemu_malloc(len
+ 1);
2576 memcpy(str
, type
, len
);
2583 static int default_fmt_format
= 'x';
2584 static int default_fmt_size
= 4;
2588 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2589 const char *cmdline
,
2592 const char *p
, *typestr
;
2594 const mon_cmd_t
*cmd
;
2600 monitor_printf(mon
, "command='%s'\n", cmdline
);
2603 /* extract the command name */
2604 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2608 /* find the command */
2609 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2610 if (compare_cmd(cmdname
, cmd
->name
))
2614 if (cmd
->name
== NULL
) {
2615 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2619 /* parse the parameters */
2620 typestr
= cmd
->args_type
;
2622 typestr
= key_get_info(typestr
, &key
);
2634 while (qemu_isspace(*p
))
2636 if (*typestr
== '?') {
2639 /* no optional string: NULL argument */
2643 ret
= get_str(buf
, sizeof(buf
), &p
);
2647 monitor_printf(mon
, "%s: filename expected\n",
2651 monitor_printf(mon
, "%s: block device name expected\n",
2655 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2660 qdict_put(qdict
, key
, qstring_from_str(buf
));
2665 int count
, format
, size
;
2667 while (qemu_isspace(*p
))
2673 if (qemu_isdigit(*p
)) {
2675 while (qemu_isdigit(*p
)) {
2676 count
= count
* 10 + (*p
- '0');
2714 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2715 monitor_printf(mon
, "invalid char in format: '%c'\n",
2720 format
= default_fmt_format
;
2721 if (format
!= 'i') {
2722 /* for 'i', not specifying a size gives -1 as size */
2724 size
= default_fmt_size
;
2725 default_fmt_size
= size
;
2727 default_fmt_format
= format
;
2730 format
= default_fmt_format
;
2731 if (format
!= 'i') {
2732 size
= default_fmt_size
;
2737 qdict_put(qdict
, "count", qint_from_int(count
));
2738 qdict_put(qdict
, "format", qint_from_int(format
));
2739 qdict_put(qdict
, "size", qint_from_int(size
));
2747 while (qemu_isspace(*p
))
2749 if (*typestr
== '?' || *typestr
== '.') {
2750 if (*typestr
== '?') {
2758 while (qemu_isspace(*p
))
2767 if (get_expr(mon
, &val
, &p
))
2769 /* Check if 'i' is greater than 32-bit */
2770 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2771 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2772 monitor_printf(mon
, "integer is for 32-bit values\n");
2775 qdict_put(qdict
, key
, qint_from_int(val
));
2786 while (qemu_isspace(*p
))
2792 monitor_printf(mon
, "%s: unsupported option -%c\n",
2799 qdict_put(qdict
, key
, qint_from_int(has_option
));
2804 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2810 /* check that all arguments were parsed */
2811 while (qemu_isspace(*p
))
2814 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2826 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2829 const mon_cmd_t
*cmd
;
2831 qdict
= qdict_new();
2833 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
2835 void (*handler
)(Monitor
*mon
, const QDict
*qdict
);
2837 qemu_errors_to_mon(mon
);
2839 handler
= cmd
->handler
;
2840 handler(mon
, qdict
);
2842 qemu_errors_to_previous();
2848 static void cmd_completion(const char *name
, const char *list
)
2850 const char *p
, *pstart
;
2859 p
= pstart
+ strlen(pstart
);
2861 if (len
> sizeof(cmd
) - 2)
2862 len
= sizeof(cmd
) - 2;
2863 memcpy(cmd
, pstart
, len
);
2865 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2866 readline_add_completion(cur_mon
->rs
, cmd
);
2874 static void file_completion(const char *input
)
2879 char file
[1024], file_prefix
[1024];
2883 p
= strrchr(input
, '/');
2886 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2887 pstrcpy(path
, sizeof(path
), ".");
2889 input_path_len
= p
- input
+ 1;
2890 memcpy(path
, input
, input_path_len
);
2891 if (input_path_len
> sizeof(path
) - 1)
2892 input_path_len
= sizeof(path
) - 1;
2893 path
[input_path_len
] = '\0';
2894 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2896 #ifdef DEBUG_COMPLETION
2897 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2898 input
, path
, file_prefix
);
2900 ffs
= opendir(path
);
2908 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2909 memcpy(file
, input
, input_path_len
);
2910 if (input_path_len
< sizeof(file
))
2911 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2913 /* stat the file to find out if it's a directory.
2914 * In that case add a slash to speed up typing long paths
2917 if(S_ISDIR(sb
.st_mode
))
2918 pstrcat(file
, sizeof(file
), "/");
2919 readline_add_completion(cur_mon
->rs
, file
);
2925 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2927 const char *name
= bdrv_get_device_name(bs
);
2928 const char *input
= opaque
;
2930 if (input
[0] == '\0' ||
2931 !strncmp(name
, (char *)input
, strlen(input
))) {
2932 readline_add_completion(cur_mon
->rs
, name
);
2936 /* NOTE: this parser is an approximate form of the real command parser */
2937 static void parse_cmdline(const char *cmdline
,
2938 int *pnb_args
, char **args
)
2947 while (qemu_isspace(*p
))
2951 if (nb_args
>= MAX_ARGS
)
2953 ret
= get_str(buf
, sizeof(buf
), &p
);
2954 args
[nb_args
] = qemu_strdup(buf
);
2959 *pnb_args
= nb_args
;
2962 static const char *next_arg_type(const char *typestr
)
2964 const char *p
= strchr(typestr
, ':');
2965 return (p
!= NULL
? ++p
: typestr
);
2968 static void monitor_find_completion(const char *cmdline
)
2970 const char *cmdname
;
2971 char *args
[MAX_ARGS
];
2972 int nb_args
, i
, len
;
2973 const char *ptype
, *str
;
2974 const mon_cmd_t
*cmd
;
2977 parse_cmdline(cmdline
, &nb_args
, args
);
2978 #ifdef DEBUG_COMPLETION
2979 for(i
= 0; i
< nb_args
; i
++) {
2980 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2984 /* if the line ends with a space, it means we want to complete the
2986 len
= strlen(cmdline
);
2987 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2988 if (nb_args
>= MAX_ARGS
)
2990 args
[nb_args
++] = qemu_strdup("");
2993 /* command completion */
2998 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
2999 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3000 cmd_completion(cmdname
, cmd
->name
);
3003 /* find the command */
3004 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3005 if (compare_cmd(args
[0], cmd
->name
))
3010 ptype
= next_arg_type(cmd
->args_type
);
3011 for(i
= 0; i
< nb_args
- 2; i
++) {
3012 if (*ptype
!= '\0') {
3013 ptype
= next_arg_type(ptype
);
3014 while (*ptype
== '?')
3015 ptype
= next_arg_type(ptype
);
3018 str
= args
[nb_args
- 1];
3019 if (*ptype
== '-' && ptype
[1] != '\0') {
3024 /* file completion */
3025 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3026 file_completion(str
);
3029 /* block device name completion */
3030 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3031 bdrv_iterate(block_completion_it
, (void *)str
);
3034 /* XXX: more generic ? */
3035 if (!strcmp(cmd
->name
, "info")) {
3036 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3037 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3038 cmd_completion(str
, cmd
->name
);
3040 } else if (!strcmp(cmd
->name
, "sendkey")) {
3041 char *sep
= strrchr(str
, '-');
3044 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3045 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3046 cmd_completion(str
, key
->name
);
3048 } else if (!strcmp(cmd
->name
, "help|?")) {
3049 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3050 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3051 cmd_completion(str
, cmd
->name
);
3059 for(i
= 0; i
< nb_args
; i
++)
3063 static int monitor_can_read(void *opaque
)
3065 Monitor
*mon
= opaque
;
3067 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3070 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3072 Monitor
*old_mon
= cur_mon
;
3078 for (i
= 0; i
< size
; i
++)
3079 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3081 if (size
== 0 || buf
[size
- 1] != 0)
3082 monitor_printf(cur_mon
, "corrupted command\n");
3084 monitor_handle_command(cur_mon
, (char *)buf
);
3090 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3092 monitor_suspend(mon
);
3093 monitor_handle_command(mon
, cmdline
);
3094 monitor_resume(mon
);
3097 int monitor_suspend(Monitor
*mon
)
3105 void monitor_resume(Monitor
*mon
)
3109 if (--mon
->suspend_cnt
== 0)
3110 readline_show_prompt(mon
->rs
);
3113 static void monitor_event(void *opaque
, int event
)
3115 Monitor
*mon
= opaque
;
3118 case CHR_EVENT_MUX_IN
:
3120 if (mon
->reset_seen
) {
3121 readline_restart(mon
->rs
);
3122 monitor_resume(mon
);
3125 mon
->suspend_cnt
= 0;
3129 case CHR_EVENT_MUX_OUT
:
3130 if (mon
->reset_seen
) {
3131 if (mon
->suspend_cnt
== 0) {
3132 monitor_printf(mon
, "\n");
3135 monitor_suspend(mon
);
3142 case CHR_EVENT_RESET
:
3143 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3144 "information\n", QEMU_VERSION
);
3145 if (!mon
->mux_out
) {
3146 readline_show_prompt(mon
->rs
);
3148 mon
->reset_seen
= 1;
3162 void monitor_init(CharDriverState
*chr
, int flags
)
3164 static int is_first_init
= 1;
3167 if (is_first_init
) {
3168 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3172 mon
= qemu_mallocz(sizeof(*mon
));
3176 if (flags
& MONITOR_USE_READLINE
) {
3177 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3178 monitor_read_command(mon
, 0);
3181 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3184 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3185 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3189 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3191 BlockDriverState
*bs
= opaque
;
3194 if (bdrv_set_key(bs
, password
) != 0) {
3195 monitor_printf(mon
, "invalid password\n");
3198 if (mon
->password_completion_cb
)
3199 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3201 monitor_read_command(mon
, 1);
3204 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3205 BlockDriverCompletionFunc
*completion_cb
,
3210 if (!bdrv_key_required(bs
)) {
3212 completion_cb(opaque
, 0);
3216 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3217 bdrv_get_encrypted_filename(bs
));
3219 mon
->password_completion_cb
= completion_cb
;
3220 mon
->password_opaque
= opaque
;
3222 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3224 if (err
&& completion_cb
)
3225 completion_cb(opaque
, err
);
3228 typedef struct QemuErrorSink QemuErrorSink
;
3229 struct QemuErrorSink
{
3238 QemuErrorSink
*previous
;
3241 static QemuErrorSink
*qemu_error_sink
;
3243 void qemu_errors_to_file(FILE *fp
)
3245 QemuErrorSink
*sink
;
3247 sink
= qemu_mallocz(sizeof(*sink
));
3248 sink
->dest
= ERR_SINK_FILE
;
3250 sink
->previous
= qemu_error_sink
;
3251 qemu_error_sink
= sink
;
3254 void qemu_errors_to_mon(Monitor
*mon
)
3256 QemuErrorSink
*sink
;
3258 sink
= qemu_mallocz(sizeof(*sink
));
3259 sink
->dest
= ERR_SINK_MONITOR
;
3261 sink
->previous
= qemu_error_sink
;
3262 qemu_error_sink
= sink
;
3265 void qemu_errors_to_previous(void)
3267 QemuErrorSink
*sink
;
3269 assert(qemu_error_sink
!= NULL
);
3270 sink
= qemu_error_sink
;
3271 qemu_error_sink
= sink
->previous
;
3275 void qemu_error(const char *fmt
, ...)
3279 assert(qemu_error_sink
!= NULL
);
3280 switch (qemu_error_sink
->dest
) {
3282 va_start(args
, fmt
);
3283 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3286 case ERR_SINK_MONITOR
:
3287 va_start(args
, fmt
);
3288 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);