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"
55 //#define DEBUG_COMPLETION
61 * 'B' block device name
62 * 's' string (accept optional quote)
64 * 'l' target long (32 or 64 bit)
65 * '/' optional gdb-like print format (like "/10x")
67 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
73 typedef struct mon_cmd_t
{
75 const char *args_type
;
81 /* file descriptors passed via SCM_RIGHTS */
82 typedef struct mon_fd_t mon_fd_t
;
86 QLIST_ENTRY(mon_fd_t
) next
;
99 BlockDriverCompletionFunc
*password_completion_cb
;
100 void *password_opaque
;
101 QLIST_HEAD(,mon_fd_t
) fds
;
102 QLIST_ENTRY(Monitor
) entry
;
105 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
107 static const mon_cmd_t mon_cmds
[];
108 static const mon_cmd_t info_cmds
[];
110 Monitor
*cur_mon
= NULL
;
112 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
115 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
117 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
119 readline_show_prompt(mon
->rs
);
122 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
126 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
127 /* prompt is printed on return from the command handler */
130 monitor_printf(mon
, "terminal does not support password prompting\n");
135 void monitor_flush(Monitor
*mon
)
137 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
138 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
139 mon
->outbuf_index
= 0;
143 /* flush at every end of line or if the buffer is full */
144 static void monitor_puts(Monitor
*mon
, const char *str
)
156 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
157 mon
->outbuf
[mon
->outbuf_index
++] = c
;
158 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
164 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
167 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
168 monitor_puts(mon
, buf
);
171 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
175 monitor_vprintf(mon
, fmt
, ap
);
179 void monitor_print_filename(Monitor
*mon
, const char *filename
)
183 for (i
= 0; filename
[i
]; i
++) {
184 switch (filename
[i
]) {
188 monitor_printf(mon
, "\\%c", filename
[i
]);
191 monitor_printf(mon
, "\\t");
194 monitor_printf(mon
, "\\r");
197 monitor_printf(mon
, "\\n");
200 monitor_printf(mon
, "%c", filename
[i
]);
206 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
210 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
215 static int compare_cmd(const char *name
, const char *list
)
217 const char *p
, *pstart
;
225 p
= pstart
+ strlen(pstart
);
226 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
235 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
236 const char *prefix
, const char *name
)
238 const mon_cmd_t
*cmd
;
240 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
241 if (!name
|| !strcmp(name
, cmd
->name
))
242 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
243 cmd
->params
, cmd
->help
);
247 static void help_cmd(Monitor
*mon
, const char *name
)
249 if (name
&& !strcmp(name
, "info")) {
250 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
252 help_cmd_dump(mon
, mon_cmds
, "", name
);
253 if (name
&& !strcmp(name
, "log")) {
254 const CPULogItem
*item
;
255 monitor_printf(mon
, "Log items (comma separated):\n");
256 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
257 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
258 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
264 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
266 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
269 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
273 const char *device
= qdict_get_str(qdict
, "device");
275 all_devices
= !strcmp(device
, "all");
276 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
278 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
280 bdrv_commit(dinfo
->bdrv
);
284 static void do_info(Monitor
*mon
, const QDict
*qdict
)
286 const mon_cmd_t
*cmd
;
287 const char *item
= qdict_get_try_str(qdict
, "item");
288 void (*handler
)(Monitor
*);
292 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
293 if (compare_cmd(item
, cmd
->name
))
297 help_cmd(mon
, "info");
300 handler
= cmd
->handler
;
304 static void do_info_version(Monitor
*mon
)
306 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
309 static void do_info_name(Monitor
*mon
)
312 monitor_printf(mon
, "%s\n", qemu_name
);
315 #if defined(TARGET_I386)
316 static void do_info_hpet(Monitor
*mon
)
318 monitor_printf(mon
, "HPET is %s by QEMU\n",
319 (no_hpet
) ? "disabled" : "enabled");
323 static void do_info_uuid(Monitor
*mon
)
325 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
326 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
327 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
328 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
329 qemu_uuid
[14], qemu_uuid
[15]);
332 /* get the current CPU defined by the user */
333 static int mon_set_cpu(int cpu_index
)
337 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
338 if (env
->cpu_index
== cpu_index
) {
339 cur_mon
->mon_cpu
= env
;
346 static CPUState
*mon_get_cpu(void)
348 if (!cur_mon
->mon_cpu
) {
351 cpu_synchronize_state(cur_mon
->mon_cpu
);
352 return cur_mon
->mon_cpu
;
355 static void do_info_registers(Monitor
*mon
)
362 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
365 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
370 static void do_info_cpus(Monitor
*mon
)
374 /* just to set the default cpu if not already done */
377 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
378 cpu_synchronize_state(env
);
379 monitor_printf(mon
, "%c CPU #%d:",
380 (env
== mon
->mon_cpu
) ? '*' : ' ',
382 #if defined(TARGET_I386)
383 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
384 env
->eip
+ env
->segs
[R_CS
].base
);
385 #elif defined(TARGET_PPC)
386 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
387 #elif defined(TARGET_SPARC)
388 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
390 #elif defined(TARGET_MIPS)
391 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
394 monitor_printf(mon
, " (halted)");
395 monitor_printf(mon
," thread_id=%d", env
->thread_id
);
396 monitor_printf(mon
, "\n");
400 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
402 int index
= qdict_get_int(qdict
, "index");
403 if (mon_set_cpu(index
) < 0)
404 monitor_printf(mon
, "Invalid CPU index\n");
407 static void do_cpu_set_nr(Monitor
*mon
, int value
, const char *status
)
411 if (!strcmp(status
, "online"))
413 else if (!strcmp(status
, "offline"))
416 monitor_printf(mon
, "invalid status: %s\n", status
);
419 #if defined(TARGET_I386) || defined(TARGET_X86_64)
420 qemu_system_cpu_hot_add(value
, state
);
424 static void do_info_jit(Monitor
*mon
)
426 dump_exec_info((FILE *)mon
, monitor_fprintf
);
429 static void do_info_history(Monitor
*mon
)
438 str
= readline_get_history(mon
->rs
, i
);
441 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
446 #if defined(TARGET_PPC)
447 /* XXX: not implemented in other targets */
448 static void do_info_cpu_stats(Monitor
*mon
)
453 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
457 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
462 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
464 if (bdrv_is_inserted(bs
)) {
466 if (!bdrv_is_removable(bs
)) {
467 monitor_printf(mon
, "device is not removable\n");
470 if (bdrv_is_locked(bs
)) {
471 monitor_printf(mon
, "device is locked\n");
480 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
482 BlockDriverState
*bs
;
483 int force
= qdict_get_int(qdict
, "force");
484 const char *filename
= qdict_get_str(qdict
, "filename");
486 bs
= bdrv_find(filename
);
488 monitor_printf(mon
, "device not found\n");
491 eject_device(mon
, bs
, force
);
494 static void do_change_block(Monitor
*mon
, const char *device
,
495 const char *filename
, const char *fmt
)
497 BlockDriverState
*bs
;
498 BlockDriver
*drv
= NULL
;
500 bs
= bdrv_find(device
);
502 monitor_printf(mon
, "device not found\n");
506 drv
= bdrv_find_format(fmt
);
508 monitor_printf(mon
, "invalid format %s\n", fmt
);
512 if (eject_device(mon
, bs
, 0) < 0)
514 bdrv_open2(bs
, filename
, 0, drv
);
515 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
518 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
521 if (vnc_display_password(NULL
, password
) < 0)
522 monitor_printf(mon
, "could not set VNC server password\n");
524 monitor_read_command(mon
, 1);
527 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
529 if (strcmp(target
, "passwd") == 0 ||
530 strcmp(target
, "password") == 0) {
533 strncpy(password
, arg
, sizeof(password
));
534 password
[sizeof(password
) - 1] = '\0';
535 change_vnc_password_cb(mon
, password
, NULL
);
537 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
540 if (vnc_display_open(NULL
, target
) < 0)
541 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
545 static void do_change(Monitor
*mon
, const QDict
*qdict
)
547 const char *device
= qdict_get_str(qdict
, "device");
548 const char *target
= qdict_get_str(qdict
, "target");
549 const char *arg
= qdict_get_try_str(qdict
, "arg");
550 if (strcmp(device
, "vnc") == 0) {
551 do_change_vnc(mon
, target
, arg
);
553 do_change_block(mon
, device
, target
, arg
);
557 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
559 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
562 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
564 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
567 static void do_log(Monitor
*mon
, const QDict
*qdict
)
570 const char *items
= qdict_get_str(qdict
, "items");
572 if (!strcmp(items
, "none")) {
575 mask
= cpu_str_to_log_mask(items
);
577 help_cmd(mon
, "log");
584 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
586 const char *option
= qdict_get_try_str(qdict
, "option");
587 if (!option
|| !strcmp(option
, "on")) {
589 } else if (!strcmp(option
, "off")) {
592 monitor_printf(mon
, "unexpected option %s\n", option
);
596 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
598 vm_stop(EXCP_INTERRUPT
);
601 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
603 struct bdrv_iterate_context
{
608 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
610 struct bdrv_iterate_context context
= { mon
, 0 };
612 bdrv_iterate(encrypted_bdrv_it
, &context
);
613 /* only resume the vm if all keys are set and valid */
618 static void bdrv_key_cb(void *opaque
, int err
)
620 Monitor
*mon
= opaque
;
622 /* another key was set successfully, retry to continue */
627 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
629 struct bdrv_iterate_context
*context
= opaque
;
631 if (!context
->err
&& bdrv_key_required(bs
)) {
632 context
->err
= -EBUSY
;
633 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
638 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
640 const char *device
= qdict_get_try_str(qdict
, "device");
642 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
643 if (gdbserver_start(device
) < 0) {
644 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
646 } else if (strcmp(device
, "none") == 0) {
647 monitor_printf(mon
, "Disabled gdbserver\n");
649 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
654 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
656 const char *action
= qdict_get_str(qdict
, "action");
657 if (select_watchdog_action(action
) == -1) {
658 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
662 static void monitor_printc(Monitor
*mon
, int c
)
664 monitor_printf(mon
, "'");
667 monitor_printf(mon
, "\\'");
670 monitor_printf(mon
, "\\\\");
673 monitor_printf(mon
, "\\n");
676 monitor_printf(mon
, "\\r");
679 if (c
>= 32 && c
<= 126) {
680 monitor_printf(mon
, "%c", c
);
682 monitor_printf(mon
, "\\x%02x", c
);
686 monitor_printf(mon
, "'");
689 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
690 target_phys_addr_t addr
, int is_physical
)
693 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
701 if (!env
&& !is_physical
)
706 } else if (wsize
== 4) {
709 /* as default we use the current CS size */
713 if ((env
->efer
& MSR_EFER_LMA
) &&
714 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
718 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
723 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
732 nb_per_line
= line_size
/ wsize
;
737 max_digits
= (wsize
* 8 + 2) / 3;
741 max_digits
= (wsize
* 8) / 4;
745 max_digits
= (wsize
* 8 * 10 + 32) / 33;
754 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
756 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
761 cpu_physical_memory_rw(addr
, buf
, l
, 0);
766 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
767 monitor_printf(mon
, " Cannot access memory\n");
776 v
= ldub_raw(buf
+ i
);
779 v
= lduw_raw(buf
+ i
);
782 v
= (uint32_t)ldl_raw(buf
+ i
);
785 v
= ldq_raw(buf
+ i
);
788 monitor_printf(mon
, " ");
791 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
794 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
797 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
800 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
803 monitor_printc(mon
, v
);
808 monitor_printf(mon
, "\n");
814 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
816 int count
= qdict_get_int(qdict
, "count");
817 int format
= qdict_get_int(qdict
, "format");
818 int size
= qdict_get_int(qdict
, "size");
819 target_long addr
= qdict_get_int(qdict
, "addr");
821 memory_dump(mon
, count
, format
, size
, addr
, 0);
824 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
826 int count
= qdict_get_int(qdict
, "count");
827 int format
= qdict_get_int(qdict
, "format");
828 int size
= qdict_get_int(qdict
, "size");
829 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
831 memory_dump(mon
, count
, format
, size
, addr
, 1);
834 static void do_print(Monitor
*mon
, const QDict
*qdict
)
836 int format
= qdict_get_int(qdict
, "format");
837 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
839 #if TARGET_PHYS_ADDR_BITS == 32
842 monitor_printf(mon
, "%#o", val
);
845 monitor_printf(mon
, "%#x", val
);
848 monitor_printf(mon
, "%u", val
);
852 monitor_printf(mon
, "%d", val
);
855 monitor_printc(mon
, val
);
861 monitor_printf(mon
, "%#" PRIo64
, val
);
864 monitor_printf(mon
, "%#" PRIx64
, val
);
867 monitor_printf(mon
, "%" PRIu64
, val
);
871 monitor_printf(mon
, "%" PRId64
, val
);
874 monitor_printc(mon
, val
);
878 monitor_printf(mon
, "\n");
881 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
884 uint32_t size
= qdict_get_int(qdict
, "size");
885 const char *filename
= qdict_get_str(qdict
, "filename");
886 target_long addr
= qdict_get_int(qdict
, "val");
895 f
= fopen(filename
, "wb");
897 monitor_printf(mon
, "could not open '%s'\n", filename
);
904 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
905 fwrite(buf
, 1, l
, f
);
912 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
917 uint32_t size
= qdict_get_int(qdict
, "size");
918 const char *filename
= qdict_get_str(qdict
, "filename");
919 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
921 f
= fopen(filename
, "wb");
923 monitor_printf(mon
, "could not open '%s'\n", filename
);
930 cpu_physical_memory_rw(addr
, buf
, l
, 0);
931 fwrite(buf
, 1, l
, f
);
939 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
944 uint32_t start
= qdict_get_int(qdict
, "start");
945 uint32_t size
= qdict_get_int(qdict
, "size");
948 for(addr
= start
; addr
< (start
+ size
); addr
++) {
949 cpu_physical_memory_rw(addr
, buf
, 1, 0);
950 /* BSD sum algorithm ('sum' Unix command) */
951 sum
= (sum
>> 1) | (sum
<< 15);
954 monitor_printf(mon
, "%05d\n", sum
);
962 static const KeyDef key_defs
[] = {
989 { 0x0e, "backspace" },
1026 { 0x37, "asterisk" },
1029 { 0x3a, "caps_lock" },
1040 { 0x45, "num_lock" },
1041 { 0x46, "scroll_lock" },
1043 { 0xb5, "kp_divide" },
1044 { 0x37, "kp_multiply" },
1045 { 0x4a, "kp_subtract" },
1047 { 0x9c, "kp_enter" },
1048 { 0x53, "kp_decimal" },
1081 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1096 { 0xfe, "compose" },
1101 static int get_keycode(const char *key
)
1107 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1108 if (!strcmp(key
, p
->name
))
1111 if (strstart(key
, "0x", NULL
)) {
1112 ret
= strtoul(key
, &endp
, 0);
1113 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1119 #define MAX_KEYCODES 16
1120 static uint8_t keycodes
[MAX_KEYCODES
];
1121 static int nb_pending_keycodes
;
1122 static QEMUTimer
*key_timer
;
1124 static void release_keys(void *opaque
)
1128 while (nb_pending_keycodes
> 0) {
1129 nb_pending_keycodes
--;
1130 keycode
= keycodes
[nb_pending_keycodes
];
1132 kbd_put_keycode(0xe0);
1133 kbd_put_keycode(keycode
| 0x80);
1137 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1139 char keyname_buf
[16];
1141 int keyname_len
, keycode
, i
;
1142 const char *string
= qdict_get_str(qdict
, "string");
1143 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1144 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1146 if (nb_pending_keycodes
> 0) {
1147 qemu_del_timer(key_timer
);
1154 separator
= strchr(string
, '-');
1155 keyname_len
= separator
? separator
- string
: strlen(string
);
1156 if (keyname_len
> 0) {
1157 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1158 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1159 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1162 if (i
== MAX_KEYCODES
) {
1163 monitor_printf(mon
, "too many keys\n");
1166 keyname_buf
[keyname_len
] = 0;
1167 keycode
= get_keycode(keyname_buf
);
1169 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1172 keycodes
[i
++] = keycode
;
1176 string
= separator
+ 1;
1178 nb_pending_keycodes
= i
;
1179 /* key down events */
1180 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1181 keycode
= keycodes
[i
];
1183 kbd_put_keycode(0xe0);
1184 kbd_put_keycode(keycode
& 0x7f);
1186 /* delayed key up events */
1187 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1188 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1191 static int mouse_button_state
;
1193 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1196 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1197 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1198 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1199 dx
= strtol(dx_str
, NULL
, 0);
1200 dy
= strtol(dy_str
, NULL
, 0);
1203 dz
= strtol(dz_str
, NULL
, 0);
1204 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1207 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1209 int button_state
= qdict_get_int(qdict
, "button_state");
1210 mouse_button_state
= button_state
;
1211 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1214 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1216 int size
= qdict_get_int(qdict
, "size");
1217 int addr
= qdict_get_int(qdict
, "addr");
1218 int has_index
= qdict_haskey(qdict
, "index");
1223 int index
= qdict_get_int(qdict
, "index");
1224 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1232 val
= cpu_inb(addr
);
1236 val
= cpu_inw(addr
);
1240 val
= cpu_inl(addr
);
1244 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1245 suffix
, addr
, size
* 2, val
);
1248 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1250 int size
= qdict_get_int(qdict
, "size");
1251 int addr
= qdict_get_int(qdict
, "addr");
1252 int val
= qdict_get_int(qdict
, "val");
1254 addr
&= IOPORTS_MASK
;
1259 cpu_outb(addr
, val
);
1262 cpu_outw(addr
, val
);
1265 cpu_outl(addr
, val
);
1270 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1273 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1275 res
= qemu_boot_set(bootdevice
);
1277 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1278 } else if (res
> 0) {
1279 monitor_printf(mon
, "setting boot device list failed\n");
1281 monitor_printf(mon
, "no function defined to set boot device list for "
1282 "this architecture\n");
1286 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1288 qemu_system_reset_request();
1291 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1293 qemu_system_powerdown_request();
1296 #if defined(TARGET_I386)
1297 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1299 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1302 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1303 pte
& PG_PSE_MASK
? 'P' : '-',
1304 pte
& PG_DIRTY_MASK
? 'D' : '-',
1305 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1306 pte
& PG_PCD_MASK
? 'C' : '-',
1307 pte
& PG_PWT_MASK
? 'T' : '-',
1308 pte
& PG_USER_MASK
? 'U' : '-',
1309 pte
& PG_RW_MASK
? 'W' : '-');
1312 static void tlb_info(Monitor
*mon
)
1316 uint32_t pgd
, pde
, pte
;
1318 env
= mon_get_cpu();
1322 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1323 monitor_printf(mon
, "PG disabled\n");
1326 pgd
= env
->cr
[3] & ~0xfff;
1327 for(l1
= 0; l1
< 1024; l1
++) {
1328 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1329 pde
= le32_to_cpu(pde
);
1330 if (pde
& PG_PRESENT_MASK
) {
1331 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1332 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1334 for(l2
= 0; l2
< 1024; l2
++) {
1335 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1336 (uint8_t *)&pte
, 4);
1337 pte
= le32_to_cpu(pte
);
1338 if (pte
& PG_PRESENT_MASK
) {
1339 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1349 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1350 uint32_t end
, int prot
)
1353 prot1
= *plast_prot
;
1354 if (prot
!= prot1
) {
1355 if (*pstart
!= -1) {
1356 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1357 *pstart
, end
, end
- *pstart
,
1358 prot1
& PG_USER_MASK
? 'u' : '-',
1360 prot1
& PG_RW_MASK
? 'w' : '-');
1370 static void mem_info(Monitor
*mon
)
1373 int l1
, l2
, prot
, last_prot
;
1374 uint32_t pgd
, pde
, pte
, start
, end
;
1376 env
= mon_get_cpu();
1380 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1381 monitor_printf(mon
, "PG disabled\n");
1384 pgd
= env
->cr
[3] & ~0xfff;
1387 for(l1
= 0; l1
< 1024; l1
++) {
1388 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1389 pde
= le32_to_cpu(pde
);
1391 if (pde
& PG_PRESENT_MASK
) {
1392 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1393 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1394 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1396 for(l2
= 0; l2
< 1024; l2
++) {
1397 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1398 (uint8_t *)&pte
, 4);
1399 pte
= le32_to_cpu(pte
);
1400 end
= (l1
<< 22) + (l2
<< 12);
1401 if (pte
& PG_PRESENT_MASK
) {
1402 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1406 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1411 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1417 #if defined(TARGET_SH4)
1419 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1421 monitor_printf(mon
, " tlb%i:\t"
1422 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1423 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1424 "dirty=%hhu writethrough=%hhu\n",
1426 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1427 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1431 static void tlb_info(Monitor
*mon
)
1433 CPUState
*env
= mon_get_cpu();
1436 monitor_printf (mon
, "ITLB:\n");
1437 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1438 print_tlb (mon
, i
, &env
->itlb
[i
]);
1439 monitor_printf (mon
, "UTLB:\n");
1440 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1441 print_tlb (mon
, i
, &env
->utlb
[i
]);
1446 static void do_info_kvm(Monitor
*mon
)
1448 #if defined(USE_KVM) || defined(CONFIG_KVM)
1449 monitor_printf(mon
, "kvm support: ");
1451 monitor_printf(mon
, "enabled\n");
1453 monitor_printf(mon
, "disabled\n");
1455 monitor_printf(mon
, "kvm support: not compiled\n");
1459 static void do_info_numa(Monitor
*mon
)
1464 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1465 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1466 monitor_printf(mon
, "node %d cpus:", i
);
1467 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1468 if (env
->numa_node
== i
) {
1469 monitor_printf(mon
, " %d", env
->cpu_index
);
1472 monitor_printf(mon
, "\n");
1473 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1478 #ifdef CONFIG_PROFILER
1483 static void do_info_profile(Monitor
*mon
)
1489 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1490 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1491 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1492 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1497 static void do_info_profile(Monitor
*mon
)
1499 monitor_printf(mon
, "Internal profiler not compiled\n");
1503 /* Capture support */
1504 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1506 static void do_info_capture(Monitor
*mon
)
1511 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1512 monitor_printf(mon
, "[%d]: ", i
);
1513 s
->ops
.info (s
->opaque
);
1518 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1521 int n
= qdict_get_int(qdict
, "n");
1524 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1526 s
->ops
.destroy (s
->opaque
);
1527 QLIST_REMOVE (s
, entries
);
1534 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1536 const char *path
= qdict_get_str(qdict
, "path");
1537 int has_freq
= qdict_haskey(qdict
, "freq");
1538 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1539 int has_bits
= qdict_haskey(qdict
, "bits");
1540 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1541 int has_channels
= qdict_haskey(qdict
, "nchannels");
1542 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1545 s
= qemu_mallocz (sizeof (*s
));
1547 freq
= has_freq
? freq
: 44100;
1548 bits
= has_bits
? bits
: 16;
1549 nchannels
= has_channels
? nchannels
: 2;
1551 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1552 monitor_printf(mon
, "Faied to add wave capture\n");
1555 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1559 #if defined(TARGET_I386)
1560 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1563 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1565 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1566 if (env
->cpu_index
== cpu_index
) {
1568 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1570 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1576 static void do_info_status(Monitor
*mon
)
1580 monitor_printf(mon
, "VM status: running (single step mode)\n");
1582 monitor_printf(mon
, "VM status: running\n");
1585 monitor_printf(mon
, "VM status: paused\n");
1589 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1591 int value
= qdict_get_int(qdict
, "value");
1592 ram_addr_t target
= value
;
1593 qemu_balloon(target
<< 20);
1596 static void do_info_balloon(Monitor
*mon
)
1600 actual
= qemu_balloon_status();
1601 if (kvm_enabled() && !kvm_has_sync_mmu())
1602 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1603 "ballooning disabled\n");
1604 else if (actual
== 0)
1605 monitor_printf(mon
, "Ballooning not activated in VM\n");
1607 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1610 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1612 qemu_acl
*acl
= qemu_acl_find(name
);
1615 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1620 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1622 const char *aclname
= qdict_get_str(qdict
, "aclname");
1623 qemu_acl
*acl
= find_acl(mon
, aclname
);
1624 qemu_acl_entry
*entry
;
1628 monitor_printf(mon
, "policy: %s\n",
1629 acl
->defaultDeny
? "deny" : "allow");
1630 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1632 monitor_printf(mon
, "%d: %s %s\n", i
,
1633 entry
->deny
? "deny" : "allow", entry
->match
);
1638 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1640 const char *aclname
= qdict_get_str(qdict
, "aclname");
1641 qemu_acl
*acl
= find_acl(mon
, aclname
);
1644 qemu_acl_reset(acl
);
1645 monitor_printf(mon
, "acl: removed all rules\n");
1649 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1651 const char *aclname
= qdict_get_str(qdict
, "aclname");
1652 const char *policy
= qdict_get_str(qdict
, "policy");
1653 qemu_acl
*acl
= find_acl(mon
, aclname
);
1656 if (strcmp(policy
, "allow") == 0) {
1657 acl
->defaultDeny
= 0;
1658 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1659 } else if (strcmp(policy
, "deny") == 0) {
1660 acl
->defaultDeny
= 1;
1661 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1663 monitor_printf(mon
, "acl: unknown policy '%s', "
1664 "expected 'deny' or 'allow'\n", policy
);
1669 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1671 const char *aclname
= qdict_get_str(qdict
, "aclname");
1672 const char *match
= qdict_get_str(qdict
, "match");
1673 const char *policy
= qdict_get_str(qdict
, "policy");
1674 int has_index
= qdict_haskey(qdict
, "index");
1675 int index
= qdict_get_try_int(qdict
, "index", -1);
1676 qemu_acl
*acl
= find_acl(mon
, aclname
);
1680 if (strcmp(policy
, "allow") == 0) {
1682 } else if (strcmp(policy
, "deny") == 0) {
1685 monitor_printf(mon
, "acl: unknown policy '%s', "
1686 "expected 'deny' or 'allow'\n", policy
);
1690 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1692 ret
= qemu_acl_append(acl
, deny
, match
);
1694 monitor_printf(mon
, "acl: unable to add acl entry\n");
1696 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1700 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1702 const char *aclname
= qdict_get_str(qdict
, "aclname");
1703 const char *match
= qdict_get_str(qdict
, "match");
1704 qemu_acl
*acl
= find_acl(mon
, aclname
);
1708 ret
= qemu_acl_remove(acl
, match
);
1710 monitor_printf(mon
, "acl: no matching acl entry\n");
1712 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1716 #if defined(TARGET_I386)
1717 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1720 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1721 int bank
= qdict_get_int(qdict
, "bank");
1722 uint64_t status
= qdict_get_int(qdict
, "status");
1723 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1724 uint64_t addr
= qdict_get_int(qdict
, "addr");
1725 uint64_t misc
= qdict_get_int(qdict
, "misc");
1727 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1728 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1729 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1735 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1737 const char *fdname
= qdict_get_str(qdict
, "fdname");
1741 fd
= qemu_chr_get_msgfd(mon
->chr
);
1743 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1747 if (qemu_isdigit(fdname
[0])) {
1748 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1754 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1759 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1760 if (strcmp(monfd
->name
, fdname
) != 0) {
1769 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1770 monfd
->name
= qemu_strdup(fdname
);
1773 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1776 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1778 const char *fdname
= qdict_get_str(qdict
, "fdname");
1781 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1782 if (strcmp(monfd
->name
, fdname
) != 0) {
1786 QLIST_REMOVE(monfd
, next
);
1788 qemu_free(monfd
->name
);
1793 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1797 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1799 int saved_vm_running
= vm_running
;
1800 const char *name
= qdict_get_str(qdict
, "name");
1804 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1808 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1812 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1815 if (strcmp(monfd
->name
, fdname
) != 0) {
1821 /* caller takes ownership of fd */
1822 QLIST_REMOVE(monfd
, next
);
1823 qemu_free(monfd
->name
);
1832 static const mon_cmd_t mon_cmds
[] = {
1833 #include "qemu-monitor.h"
1837 /* Please update qemu-monitor.hx when adding or changing commands */
1838 static const mon_cmd_t info_cmds
[] = {
1839 { "version", "", do_info_version
,
1840 "", "show the version of QEMU" },
1841 { "network", "", do_info_network
,
1842 "", "show the network state" },
1843 { "chardev", "", qemu_chr_info
,
1844 "", "show the character devices" },
1845 { "block", "", bdrv_info
,
1846 "", "show the block devices" },
1847 { "blockstats", "", bdrv_info_stats
,
1848 "", "show block device statistics" },
1849 { "registers", "", do_info_registers
,
1850 "", "show the cpu registers" },
1851 { "cpus", "", do_info_cpus
,
1852 "", "show infos for each CPU" },
1853 { "history", "", do_info_history
,
1854 "", "show the command line history", },
1855 { "irq", "", irq_info
,
1856 "", "show the interrupts statistics (if available)", },
1857 { "pic", "", pic_info
,
1858 "", "show i8259 (PIC) state", },
1859 { "pci", "", pci_info
,
1860 "", "show PCI info", },
1861 #if defined(TARGET_I386) || defined(TARGET_SH4)
1862 { "tlb", "", tlb_info
,
1863 "", "show virtual to physical memory mappings", },
1865 #if defined(TARGET_I386)
1866 { "mem", "", mem_info
,
1867 "", "show the active virtual memory mappings", },
1868 { "hpet", "", do_info_hpet
,
1869 "", "show state of HPET", },
1871 { "jit", "", do_info_jit
,
1872 "", "show dynamic compiler info", },
1873 { "kvm", "", do_info_kvm
,
1874 "", "show KVM information", },
1875 { "numa", "", do_info_numa
,
1876 "", "show NUMA information", },
1877 { "usb", "", usb_info
,
1878 "", "show guest USB devices", },
1879 { "usbhost", "", usb_host_info
,
1880 "", "show host USB devices", },
1881 { "profile", "", do_info_profile
,
1882 "", "show profiling information", },
1883 { "capture", "", do_info_capture
,
1884 "", "show capture information" },
1885 { "snapshots", "", do_info_snapshots
,
1886 "", "show the currently saved VM snapshots" },
1887 { "status", "", do_info_status
,
1888 "", "show the current VM status (running|paused)" },
1889 { "pcmcia", "", pcmcia_info
,
1890 "", "show guest PCMCIA status" },
1891 { "mice", "", do_info_mice
,
1892 "", "show which guest mouse is receiving events" },
1893 { "vnc", "", do_info_vnc
,
1894 "", "show the vnc server status"},
1895 { "name", "", do_info_name
,
1896 "", "show the current VM name" },
1897 { "uuid", "", do_info_uuid
,
1898 "", "show the current VM UUID" },
1899 #if defined(TARGET_PPC)
1900 { "cpustats", "", do_info_cpu_stats
,
1901 "", "show CPU statistics", },
1903 #if defined(CONFIG_SLIRP)
1904 { "usernet", "", do_info_usernet
,
1905 "", "show user network stack connection states", },
1907 { "migrate", "", do_info_migrate
, "", "show migration status" },
1908 { "balloon", "", do_info_balloon
,
1909 "", "show balloon information" },
1910 { "qtree", "", do_info_qtree
,
1911 "", "show device tree" },
1912 { "qdm", "", do_info_qdm
,
1913 "", "show qdev device model list" },
1917 /*******************************************************************/
1919 static const char *pch
;
1920 static jmp_buf expr_env
;
1925 typedef struct MonitorDef
{
1928 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1932 #if defined(TARGET_I386)
1933 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1935 CPUState
*env
= mon_get_cpu();
1938 return env
->eip
+ env
->segs
[R_CS
].base
;
1942 #if defined(TARGET_PPC)
1943 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1945 CPUState
*env
= mon_get_cpu();
1953 for (i
= 0; i
< 8; i
++)
1954 u
|= env
->crf
[i
] << (32 - (4 * i
));
1959 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1961 CPUState
*env
= mon_get_cpu();
1967 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1969 CPUState
*env
= mon_get_cpu();
1975 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1977 CPUState
*env
= mon_get_cpu();
1980 return cpu_ppc_load_decr(env
);
1983 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1985 CPUState
*env
= mon_get_cpu();
1988 return cpu_ppc_load_tbu(env
);
1991 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1993 CPUState
*env
= mon_get_cpu();
1996 return cpu_ppc_load_tbl(env
);
2000 #if defined(TARGET_SPARC)
2001 #ifndef TARGET_SPARC64
2002 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2004 CPUState
*env
= mon_get_cpu();
2007 return GET_PSR(env
);
2011 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2013 CPUState
*env
= mon_get_cpu();
2016 return env
->regwptr
[val
];
2020 static const MonitorDef monitor_defs
[] = {
2023 #define SEG(name, seg) \
2024 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2025 { name ".base", offsetof(CPUState, segs[seg].base) },\
2026 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2028 { "eax", offsetof(CPUState
, regs
[0]) },
2029 { "ecx", offsetof(CPUState
, regs
[1]) },
2030 { "edx", offsetof(CPUState
, regs
[2]) },
2031 { "ebx", offsetof(CPUState
, regs
[3]) },
2032 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2033 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2034 { "esi", offsetof(CPUState
, regs
[6]) },
2035 { "edi", offsetof(CPUState
, regs
[7]) },
2036 #ifdef TARGET_X86_64
2037 { "r8", offsetof(CPUState
, regs
[8]) },
2038 { "r9", offsetof(CPUState
, regs
[9]) },
2039 { "r10", offsetof(CPUState
, regs
[10]) },
2040 { "r11", offsetof(CPUState
, regs
[11]) },
2041 { "r12", offsetof(CPUState
, regs
[12]) },
2042 { "r13", offsetof(CPUState
, regs
[13]) },
2043 { "r14", offsetof(CPUState
, regs
[14]) },
2044 { "r15", offsetof(CPUState
, regs
[15]) },
2046 { "eflags", offsetof(CPUState
, eflags
) },
2047 { "eip", offsetof(CPUState
, eip
) },
2054 { "pc", 0, monitor_get_pc
, },
2055 #elif defined(TARGET_PPC)
2056 /* General purpose registers */
2057 { "r0", offsetof(CPUState
, gpr
[0]) },
2058 { "r1", offsetof(CPUState
, gpr
[1]) },
2059 { "r2", offsetof(CPUState
, gpr
[2]) },
2060 { "r3", offsetof(CPUState
, gpr
[3]) },
2061 { "r4", offsetof(CPUState
, gpr
[4]) },
2062 { "r5", offsetof(CPUState
, gpr
[5]) },
2063 { "r6", offsetof(CPUState
, gpr
[6]) },
2064 { "r7", offsetof(CPUState
, gpr
[7]) },
2065 { "r8", offsetof(CPUState
, gpr
[8]) },
2066 { "r9", offsetof(CPUState
, gpr
[9]) },
2067 { "r10", offsetof(CPUState
, gpr
[10]) },
2068 { "r11", offsetof(CPUState
, gpr
[11]) },
2069 { "r12", offsetof(CPUState
, gpr
[12]) },
2070 { "r13", offsetof(CPUState
, gpr
[13]) },
2071 { "r14", offsetof(CPUState
, gpr
[14]) },
2072 { "r15", offsetof(CPUState
, gpr
[15]) },
2073 { "r16", offsetof(CPUState
, gpr
[16]) },
2074 { "r17", offsetof(CPUState
, gpr
[17]) },
2075 { "r18", offsetof(CPUState
, gpr
[18]) },
2076 { "r19", offsetof(CPUState
, gpr
[19]) },
2077 { "r20", offsetof(CPUState
, gpr
[20]) },
2078 { "r21", offsetof(CPUState
, gpr
[21]) },
2079 { "r22", offsetof(CPUState
, gpr
[22]) },
2080 { "r23", offsetof(CPUState
, gpr
[23]) },
2081 { "r24", offsetof(CPUState
, gpr
[24]) },
2082 { "r25", offsetof(CPUState
, gpr
[25]) },
2083 { "r26", offsetof(CPUState
, gpr
[26]) },
2084 { "r27", offsetof(CPUState
, gpr
[27]) },
2085 { "r28", offsetof(CPUState
, gpr
[28]) },
2086 { "r29", offsetof(CPUState
, gpr
[29]) },
2087 { "r30", offsetof(CPUState
, gpr
[30]) },
2088 { "r31", offsetof(CPUState
, gpr
[31]) },
2089 /* Floating point registers */
2090 { "f0", offsetof(CPUState
, fpr
[0]) },
2091 { "f1", offsetof(CPUState
, fpr
[1]) },
2092 { "f2", offsetof(CPUState
, fpr
[2]) },
2093 { "f3", offsetof(CPUState
, fpr
[3]) },
2094 { "f4", offsetof(CPUState
, fpr
[4]) },
2095 { "f5", offsetof(CPUState
, fpr
[5]) },
2096 { "f6", offsetof(CPUState
, fpr
[6]) },
2097 { "f7", offsetof(CPUState
, fpr
[7]) },
2098 { "f8", offsetof(CPUState
, fpr
[8]) },
2099 { "f9", offsetof(CPUState
, fpr
[9]) },
2100 { "f10", offsetof(CPUState
, fpr
[10]) },
2101 { "f11", offsetof(CPUState
, fpr
[11]) },
2102 { "f12", offsetof(CPUState
, fpr
[12]) },
2103 { "f13", offsetof(CPUState
, fpr
[13]) },
2104 { "f14", offsetof(CPUState
, fpr
[14]) },
2105 { "f15", offsetof(CPUState
, fpr
[15]) },
2106 { "f16", offsetof(CPUState
, fpr
[16]) },
2107 { "f17", offsetof(CPUState
, fpr
[17]) },
2108 { "f18", offsetof(CPUState
, fpr
[18]) },
2109 { "f19", offsetof(CPUState
, fpr
[19]) },
2110 { "f20", offsetof(CPUState
, fpr
[20]) },
2111 { "f21", offsetof(CPUState
, fpr
[21]) },
2112 { "f22", offsetof(CPUState
, fpr
[22]) },
2113 { "f23", offsetof(CPUState
, fpr
[23]) },
2114 { "f24", offsetof(CPUState
, fpr
[24]) },
2115 { "f25", offsetof(CPUState
, fpr
[25]) },
2116 { "f26", offsetof(CPUState
, fpr
[26]) },
2117 { "f27", offsetof(CPUState
, fpr
[27]) },
2118 { "f28", offsetof(CPUState
, fpr
[28]) },
2119 { "f29", offsetof(CPUState
, fpr
[29]) },
2120 { "f30", offsetof(CPUState
, fpr
[30]) },
2121 { "f31", offsetof(CPUState
, fpr
[31]) },
2122 { "fpscr", offsetof(CPUState
, fpscr
) },
2123 /* Next instruction pointer */
2124 { "nip|pc", offsetof(CPUState
, nip
) },
2125 { "lr", offsetof(CPUState
, lr
) },
2126 { "ctr", offsetof(CPUState
, ctr
) },
2127 { "decr", 0, &monitor_get_decr
, },
2128 { "ccr", 0, &monitor_get_ccr
, },
2129 /* Machine state register */
2130 { "msr", 0, &monitor_get_msr
, },
2131 { "xer", 0, &monitor_get_xer
, },
2132 { "tbu", 0, &monitor_get_tbu
, },
2133 { "tbl", 0, &monitor_get_tbl
, },
2134 #if defined(TARGET_PPC64)
2135 /* Address space register */
2136 { "asr", offsetof(CPUState
, asr
) },
2138 /* Segment registers */
2139 { "sdr1", offsetof(CPUState
, sdr1
) },
2140 { "sr0", offsetof(CPUState
, sr
[0]) },
2141 { "sr1", offsetof(CPUState
, sr
[1]) },
2142 { "sr2", offsetof(CPUState
, sr
[2]) },
2143 { "sr3", offsetof(CPUState
, sr
[3]) },
2144 { "sr4", offsetof(CPUState
, sr
[4]) },
2145 { "sr5", offsetof(CPUState
, sr
[5]) },
2146 { "sr6", offsetof(CPUState
, sr
[6]) },
2147 { "sr7", offsetof(CPUState
, sr
[7]) },
2148 { "sr8", offsetof(CPUState
, sr
[8]) },
2149 { "sr9", offsetof(CPUState
, sr
[9]) },
2150 { "sr10", offsetof(CPUState
, sr
[10]) },
2151 { "sr11", offsetof(CPUState
, sr
[11]) },
2152 { "sr12", offsetof(CPUState
, sr
[12]) },
2153 { "sr13", offsetof(CPUState
, sr
[13]) },
2154 { "sr14", offsetof(CPUState
, sr
[14]) },
2155 { "sr15", offsetof(CPUState
, sr
[15]) },
2156 /* Too lazy to put BATs and SPRs ... */
2157 #elif defined(TARGET_SPARC)
2158 { "g0", offsetof(CPUState
, gregs
[0]) },
2159 { "g1", offsetof(CPUState
, gregs
[1]) },
2160 { "g2", offsetof(CPUState
, gregs
[2]) },
2161 { "g3", offsetof(CPUState
, gregs
[3]) },
2162 { "g4", offsetof(CPUState
, gregs
[4]) },
2163 { "g5", offsetof(CPUState
, gregs
[5]) },
2164 { "g6", offsetof(CPUState
, gregs
[6]) },
2165 { "g7", offsetof(CPUState
, gregs
[7]) },
2166 { "o0", 0, monitor_get_reg
},
2167 { "o1", 1, monitor_get_reg
},
2168 { "o2", 2, monitor_get_reg
},
2169 { "o3", 3, monitor_get_reg
},
2170 { "o4", 4, monitor_get_reg
},
2171 { "o5", 5, monitor_get_reg
},
2172 { "o6", 6, monitor_get_reg
},
2173 { "o7", 7, monitor_get_reg
},
2174 { "l0", 8, monitor_get_reg
},
2175 { "l1", 9, monitor_get_reg
},
2176 { "l2", 10, monitor_get_reg
},
2177 { "l3", 11, monitor_get_reg
},
2178 { "l4", 12, monitor_get_reg
},
2179 { "l5", 13, monitor_get_reg
},
2180 { "l6", 14, monitor_get_reg
},
2181 { "l7", 15, monitor_get_reg
},
2182 { "i0", 16, monitor_get_reg
},
2183 { "i1", 17, monitor_get_reg
},
2184 { "i2", 18, monitor_get_reg
},
2185 { "i3", 19, monitor_get_reg
},
2186 { "i4", 20, monitor_get_reg
},
2187 { "i5", 21, monitor_get_reg
},
2188 { "i6", 22, monitor_get_reg
},
2189 { "i7", 23, monitor_get_reg
},
2190 { "pc", offsetof(CPUState
, pc
) },
2191 { "npc", offsetof(CPUState
, npc
) },
2192 { "y", offsetof(CPUState
, y
) },
2193 #ifndef TARGET_SPARC64
2194 { "psr", 0, &monitor_get_psr
, },
2195 { "wim", offsetof(CPUState
, wim
) },
2197 { "tbr", offsetof(CPUState
, tbr
) },
2198 { "fsr", offsetof(CPUState
, fsr
) },
2199 { "f0", offsetof(CPUState
, fpr
[0]) },
2200 { "f1", offsetof(CPUState
, fpr
[1]) },
2201 { "f2", offsetof(CPUState
, fpr
[2]) },
2202 { "f3", offsetof(CPUState
, fpr
[3]) },
2203 { "f4", offsetof(CPUState
, fpr
[4]) },
2204 { "f5", offsetof(CPUState
, fpr
[5]) },
2205 { "f6", offsetof(CPUState
, fpr
[6]) },
2206 { "f7", offsetof(CPUState
, fpr
[7]) },
2207 { "f8", offsetof(CPUState
, fpr
[8]) },
2208 { "f9", offsetof(CPUState
, fpr
[9]) },
2209 { "f10", offsetof(CPUState
, fpr
[10]) },
2210 { "f11", offsetof(CPUState
, fpr
[11]) },
2211 { "f12", offsetof(CPUState
, fpr
[12]) },
2212 { "f13", offsetof(CPUState
, fpr
[13]) },
2213 { "f14", offsetof(CPUState
, fpr
[14]) },
2214 { "f15", offsetof(CPUState
, fpr
[15]) },
2215 { "f16", offsetof(CPUState
, fpr
[16]) },
2216 { "f17", offsetof(CPUState
, fpr
[17]) },
2217 { "f18", offsetof(CPUState
, fpr
[18]) },
2218 { "f19", offsetof(CPUState
, fpr
[19]) },
2219 { "f20", offsetof(CPUState
, fpr
[20]) },
2220 { "f21", offsetof(CPUState
, fpr
[21]) },
2221 { "f22", offsetof(CPUState
, fpr
[22]) },
2222 { "f23", offsetof(CPUState
, fpr
[23]) },
2223 { "f24", offsetof(CPUState
, fpr
[24]) },
2224 { "f25", offsetof(CPUState
, fpr
[25]) },
2225 { "f26", offsetof(CPUState
, fpr
[26]) },
2226 { "f27", offsetof(CPUState
, fpr
[27]) },
2227 { "f28", offsetof(CPUState
, fpr
[28]) },
2228 { "f29", offsetof(CPUState
, fpr
[29]) },
2229 { "f30", offsetof(CPUState
, fpr
[30]) },
2230 { "f31", offsetof(CPUState
, fpr
[31]) },
2231 #ifdef TARGET_SPARC64
2232 { "f32", offsetof(CPUState
, fpr
[32]) },
2233 { "f34", offsetof(CPUState
, fpr
[34]) },
2234 { "f36", offsetof(CPUState
, fpr
[36]) },
2235 { "f38", offsetof(CPUState
, fpr
[38]) },
2236 { "f40", offsetof(CPUState
, fpr
[40]) },
2237 { "f42", offsetof(CPUState
, fpr
[42]) },
2238 { "f44", offsetof(CPUState
, fpr
[44]) },
2239 { "f46", offsetof(CPUState
, fpr
[46]) },
2240 { "f48", offsetof(CPUState
, fpr
[48]) },
2241 { "f50", offsetof(CPUState
, fpr
[50]) },
2242 { "f52", offsetof(CPUState
, fpr
[52]) },
2243 { "f54", offsetof(CPUState
, fpr
[54]) },
2244 { "f56", offsetof(CPUState
, fpr
[56]) },
2245 { "f58", offsetof(CPUState
, fpr
[58]) },
2246 { "f60", offsetof(CPUState
, fpr
[60]) },
2247 { "f62", offsetof(CPUState
, fpr
[62]) },
2248 { "asi", offsetof(CPUState
, asi
) },
2249 { "pstate", offsetof(CPUState
, pstate
) },
2250 { "cansave", offsetof(CPUState
, cansave
) },
2251 { "canrestore", offsetof(CPUState
, canrestore
) },
2252 { "otherwin", offsetof(CPUState
, otherwin
) },
2253 { "wstate", offsetof(CPUState
, wstate
) },
2254 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2255 { "fprs", offsetof(CPUState
, fprs
) },
2261 static void expr_error(Monitor
*mon
, const char *msg
)
2263 monitor_printf(mon
, "%s\n", msg
);
2264 longjmp(expr_env
, 1);
2267 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2268 static int get_monitor_def(target_long
*pval
, const char *name
)
2270 const MonitorDef
*md
;
2273 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2274 if (compare_cmd(name
, md
->name
)) {
2275 if (md
->get_value
) {
2276 *pval
= md
->get_value(md
, md
->offset
);
2278 CPUState
*env
= mon_get_cpu();
2281 ptr
= (uint8_t *)env
+ md
->offset
;
2284 *pval
= *(int32_t *)ptr
;
2287 *pval
= *(target_long
*)ptr
;
2300 static void next(void)
2304 while (qemu_isspace(*pch
))
2309 static int64_t expr_sum(Monitor
*mon
);
2311 static int64_t expr_unary(Monitor
*mon
)
2320 n
= expr_unary(mon
);
2324 n
= -expr_unary(mon
);
2328 n
= ~expr_unary(mon
);
2334 expr_error(mon
, "')' expected");
2341 expr_error(mon
, "character constant expected");
2345 expr_error(mon
, "missing terminating \' character");
2355 while ((*pch
>= 'a' && *pch
<= 'z') ||
2356 (*pch
>= 'A' && *pch
<= 'Z') ||
2357 (*pch
>= '0' && *pch
<= '9') ||
2358 *pch
== '_' || *pch
== '.') {
2359 if ((q
- buf
) < sizeof(buf
) - 1)
2363 while (qemu_isspace(*pch
))
2366 ret
= get_monitor_def(®
, buf
);
2368 expr_error(mon
, "unknown register");
2370 expr_error(mon
, "no cpu defined");
2375 expr_error(mon
, "unexpected end of expression");
2379 #if TARGET_PHYS_ADDR_BITS > 32
2380 n
= strtoull(pch
, &p
, 0);
2382 n
= strtoul(pch
, &p
, 0);
2385 expr_error(mon
, "invalid char in expression");
2388 while (qemu_isspace(*pch
))
2396 static int64_t expr_prod(Monitor
*mon
)
2401 val
= expr_unary(mon
);
2404 if (op
!= '*' && op
!= '/' && op
!= '%')
2407 val2
= expr_unary(mon
);
2416 expr_error(mon
, "division by zero");
2427 static int64_t expr_logic(Monitor
*mon
)
2432 val
= expr_prod(mon
);
2435 if (op
!= '&' && op
!= '|' && op
!= '^')
2438 val2
= expr_prod(mon
);
2455 static int64_t expr_sum(Monitor
*mon
)
2460 val
= expr_logic(mon
);
2463 if (op
!= '+' && op
!= '-')
2466 val2
= expr_logic(mon
);
2475 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2478 if (setjmp(expr_env
)) {
2482 while (qemu_isspace(*pch
))
2484 *pval
= expr_sum(mon
);
2489 static int get_str(char *buf
, int buf_size
, const char **pp
)
2497 while (qemu_isspace(*p
))
2507 while (*p
!= '\0' && *p
!= '\"') {
2523 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2526 if ((q
- buf
) < buf_size
- 1) {
2530 if ((q
- buf
) < buf_size
- 1) {
2537 qemu_printf("unterminated string\n");
2542 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2543 if ((q
- buf
) < buf_size
- 1) {
2555 * Store the command-name in cmdname, and return a pointer to
2556 * the remaining of the command string.
2558 static const char *get_command_name(const char *cmdline
,
2559 char *cmdname
, size_t nlen
)
2562 const char *p
, *pstart
;
2565 while (qemu_isspace(*p
))
2570 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2575 memcpy(cmdname
, pstart
, len
);
2576 cmdname
[len
] = '\0';
2581 * Read key of 'type' into 'key' and return the current
2584 static char *key_get_info(const char *type
, char **key
)
2592 p
= strchr(type
, ':');
2599 str
= qemu_malloc(len
+ 1);
2600 memcpy(str
, type
, len
);
2607 static int default_fmt_format
= 'x';
2608 static int default_fmt_size
= 4;
2612 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2613 const char *cmdline
,
2616 const char *p
, *typestr
;
2618 const mon_cmd_t
*cmd
;
2624 monitor_printf(mon
, "command='%s'\n", cmdline
);
2627 /* extract the command name */
2628 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2632 /* find the command */
2633 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2634 if (compare_cmd(cmdname
, cmd
->name
))
2638 if (cmd
->name
== NULL
) {
2639 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2643 /* parse the parameters */
2644 typestr
= cmd
->args_type
;
2646 typestr
= key_get_info(typestr
, &key
);
2658 while (qemu_isspace(*p
))
2660 if (*typestr
== '?') {
2663 /* no optional string: NULL argument */
2667 ret
= get_str(buf
, sizeof(buf
), &p
);
2671 monitor_printf(mon
, "%s: filename expected\n",
2675 monitor_printf(mon
, "%s: block device name expected\n",
2679 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2684 qdict_put(qdict
, key
, qstring_from_str(buf
));
2689 int count
, format
, size
;
2691 while (qemu_isspace(*p
))
2697 if (qemu_isdigit(*p
)) {
2699 while (qemu_isdigit(*p
)) {
2700 count
= count
* 10 + (*p
- '0');
2738 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2739 monitor_printf(mon
, "invalid char in format: '%c'\n",
2744 format
= default_fmt_format
;
2745 if (format
!= 'i') {
2746 /* for 'i', not specifying a size gives -1 as size */
2748 size
= default_fmt_size
;
2749 default_fmt_size
= size
;
2751 default_fmt_format
= format
;
2754 format
= default_fmt_format
;
2755 if (format
!= 'i') {
2756 size
= default_fmt_size
;
2761 qdict_put(qdict
, "count", qint_from_int(count
));
2762 qdict_put(qdict
, "format", qint_from_int(format
));
2763 qdict_put(qdict
, "size", qint_from_int(size
));
2771 while (qemu_isspace(*p
))
2773 if (*typestr
== '?' || *typestr
== '.') {
2774 if (*typestr
== '?') {
2782 while (qemu_isspace(*p
))
2791 if (get_expr(mon
, &val
, &p
))
2793 /* Check if 'i' is greater than 32-bit */
2794 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2795 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2796 monitor_printf(mon
, "integer is for 32-bit values\n");
2799 qdict_put(qdict
, key
, qint_from_int(val
));
2810 while (qemu_isspace(*p
))
2816 monitor_printf(mon
, "%s: unsupported option -%c\n",
2823 qdict_put(qdict
, key
, qint_from_int(has_option
));
2828 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2834 /* check that all arguments were parsed */
2835 while (qemu_isspace(*p
))
2838 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2850 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2853 const mon_cmd_t
*cmd
;
2855 qdict
= qdict_new();
2857 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
2859 void (*handler
)(Monitor
*mon
, const QDict
*qdict
);
2861 qemu_errors_to_mon(mon
);
2863 handler
= cmd
->handler
;
2864 handler(mon
, qdict
);
2866 qemu_errors_to_previous();
2872 static void cmd_completion(const char *name
, const char *list
)
2874 const char *p
, *pstart
;
2883 p
= pstart
+ strlen(pstart
);
2885 if (len
> sizeof(cmd
) - 2)
2886 len
= sizeof(cmd
) - 2;
2887 memcpy(cmd
, pstart
, len
);
2889 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2890 readline_add_completion(cur_mon
->rs
, cmd
);
2898 static void file_completion(const char *input
)
2903 char file
[1024], file_prefix
[1024];
2907 p
= strrchr(input
, '/');
2910 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2911 pstrcpy(path
, sizeof(path
), ".");
2913 input_path_len
= p
- input
+ 1;
2914 memcpy(path
, input
, input_path_len
);
2915 if (input_path_len
> sizeof(path
) - 1)
2916 input_path_len
= sizeof(path
) - 1;
2917 path
[input_path_len
] = '\0';
2918 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2920 #ifdef DEBUG_COMPLETION
2921 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2922 input
, path
, file_prefix
);
2924 ffs
= opendir(path
);
2932 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2933 memcpy(file
, input
, input_path_len
);
2934 if (input_path_len
< sizeof(file
))
2935 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2937 /* stat the file to find out if it's a directory.
2938 * In that case add a slash to speed up typing long paths
2941 if(S_ISDIR(sb
.st_mode
))
2942 pstrcat(file
, sizeof(file
), "/");
2943 readline_add_completion(cur_mon
->rs
, file
);
2949 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2951 const char *name
= bdrv_get_device_name(bs
);
2952 const char *input
= opaque
;
2954 if (input
[0] == '\0' ||
2955 !strncmp(name
, (char *)input
, strlen(input
))) {
2956 readline_add_completion(cur_mon
->rs
, name
);
2960 /* NOTE: this parser is an approximate form of the real command parser */
2961 static void parse_cmdline(const char *cmdline
,
2962 int *pnb_args
, char **args
)
2971 while (qemu_isspace(*p
))
2975 if (nb_args
>= MAX_ARGS
)
2977 ret
= get_str(buf
, sizeof(buf
), &p
);
2978 args
[nb_args
] = qemu_strdup(buf
);
2983 *pnb_args
= nb_args
;
2986 static const char *next_arg_type(const char *typestr
)
2988 const char *p
= strchr(typestr
, ':');
2989 return (p
!= NULL
? ++p
: typestr
);
2992 static void monitor_find_completion(const char *cmdline
)
2994 const char *cmdname
;
2995 char *args
[MAX_ARGS
];
2996 int nb_args
, i
, len
;
2997 const char *ptype
, *str
;
2998 const mon_cmd_t
*cmd
;
3001 parse_cmdline(cmdline
, &nb_args
, args
);
3002 #ifdef DEBUG_COMPLETION
3003 for(i
= 0; i
< nb_args
; i
++) {
3004 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3008 /* if the line ends with a space, it means we want to complete the
3010 len
= strlen(cmdline
);
3011 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3012 if (nb_args
>= MAX_ARGS
)
3014 args
[nb_args
++] = qemu_strdup("");
3017 /* command completion */
3022 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3023 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3024 cmd_completion(cmdname
, cmd
->name
);
3027 /* find the command */
3028 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3029 if (compare_cmd(args
[0], cmd
->name
))
3034 ptype
= next_arg_type(cmd
->args_type
);
3035 for(i
= 0; i
< nb_args
- 2; i
++) {
3036 if (*ptype
!= '\0') {
3037 ptype
= next_arg_type(ptype
);
3038 while (*ptype
== '?')
3039 ptype
= next_arg_type(ptype
);
3042 str
= args
[nb_args
- 1];
3043 if (*ptype
== '-' && ptype
[1] != '\0') {
3048 /* file completion */
3049 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3050 file_completion(str
);
3053 /* block device name completion */
3054 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3055 bdrv_iterate(block_completion_it
, (void *)str
);
3058 /* XXX: more generic ? */
3059 if (!strcmp(cmd
->name
, "info")) {
3060 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3061 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3062 cmd_completion(str
, cmd
->name
);
3064 } else if (!strcmp(cmd
->name
, "sendkey")) {
3065 char *sep
= strrchr(str
, '-');
3068 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3069 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3070 cmd_completion(str
, key
->name
);
3072 } else if (!strcmp(cmd
->name
, "help|?")) {
3073 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3074 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3075 cmd_completion(str
, cmd
->name
);
3083 for(i
= 0; i
< nb_args
; i
++)
3087 static int monitor_can_read(void *opaque
)
3089 Monitor
*mon
= opaque
;
3091 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3094 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3096 Monitor
*old_mon
= cur_mon
;
3102 for (i
= 0; i
< size
; i
++)
3103 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3105 if (size
== 0 || buf
[size
- 1] != 0)
3106 monitor_printf(cur_mon
, "corrupted command\n");
3108 monitor_handle_command(cur_mon
, (char *)buf
);
3114 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3116 monitor_suspend(mon
);
3117 monitor_handle_command(mon
, cmdline
);
3118 monitor_resume(mon
);
3121 int monitor_suspend(Monitor
*mon
)
3129 void monitor_resume(Monitor
*mon
)
3133 if (--mon
->suspend_cnt
== 0)
3134 readline_show_prompt(mon
->rs
);
3137 static void monitor_event(void *opaque
, int event
)
3139 Monitor
*mon
= opaque
;
3142 case CHR_EVENT_MUX_IN
:
3144 if (mon
->reset_seen
) {
3145 readline_restart(mon
->rs
);
3146 monitor_resume(mon
);
3149 mon
->suspend_cnt
= 0;
3153 case CHR_EVENT_MUX_OUT
:
3154 if (mon
->reset_seen
) {
3155 if (mon
->suspend_cnt
== 0) {
3156 monitor_printf(mon
, "\n");
3159 monitor_suspend(mon
);
3166 case CHR_EVENT_RESET
:
3167 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3168 "information\n", QEMU_VERSION
);
3169 if (!mon
->mux_out
) {
3170 readline_show_prompt(mon
->rs
);
3172 mon
->reset_seen
= 1;
3186 void monitor_init(CharDriverState
*chr
, int flags
)
3188 static int is_first_init
= 1;
3191 if (is_first_init
) {
3192 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3196 mon
= qemu_mallocz(sizeof(*mon
));
3200 if (flags
& MONITOR_USE_READLINE
) {
3201 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3202 monitor_read_command(mon
, 0);
3205 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3208 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3209 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3213 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3215 BlockDriverState
*bs
= opaque
;
3218 if (bdrv_set_key(bs
, password
) != 0) {
3219 monitor_printf(mon
, "invalid password\n");
3222 if (mon
->password_completion_cb
)
3223 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3225 monitor_read_command(mon
, 1);
3228 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3229 BlockDriverCompletionFunc
*completion_cb
,
3234 if (!bdrv_key_required(bs
)) {
3236 completion_cb(opaque
, 0);
3240 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3241 bdrv_get_encrypted_filename(bs
));
3243 mon
->password_completion_cb
= completion_cb
;
3244 mon
->password_opaque
= opaque
;
3246 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3248 if (err
&& completion_cb
)
3249 completion_cb(opaque
, err
);
3252 typedef struct QemuErrorSink QemuErrorSink
;
3253 struct QemuErrorSink
{
3262 QemuErrorSink
*previous
;
3265 static QemuErrorSink
*qemu_error_sink
;
3267 void qemu_errors_to_file(FILE *fp
)
3269 QemuErrorSink
*sink
;
3271 sink
= qemu_mallocz(sizeof(*sink
));
3272 sink
->dest
= ERR_SINK_FILE
;
3274 sink
->previous
= qemu_error_sink
;
3275 qemu_error_sink
= sink
;
3278 void qemu_errors_to_mon(Monitor
*mon
)
3280 QemuErrorSink
*sink
;
3282 sink
= qemu_mallocz(sizeof(*sink
));
3283 sink
->dest
= ERR_SINK_MONITOR
;
3285 sink
->previous
= qemu_error_sink
;
3286 qemu_error_sink
= sink
;
3289 void qemu_errors_to_previous(void)
3291 QemuErrorSink
*sink
;
3293 assert(qemu_error_sink
!= NULL
);
3294 sink
= qemu_error_sink
;
3295 qemu_error_sink
= sink
->previous
;
3299 void qemu_error(const char *fmt
, ...)
3303 assert(qemu_error_sink
!= NULL
);
3304 switch (qemu_error_sink
->dest
) {
3306 va_start(args
, fmt
);
3307 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3310 case ERR_SINK_MONITOR
:
3311 va_start(args
, fmt
);
3312 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);