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"
32 #include "hw/loader.h"
35 #include "qemu-char.h"
41 #include "audio/audio.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
53 //#define DEBUG_COMPLETION
59 * 'B' block device name
60 * 's' string (accept optional quote)
62 * 'l' target long (32 or 64 bit)
63 * '/' optional gdb-like print format (like "/10x")
65 * '?' optional type (for all types, except '/')
66 * '.' other form of optional type (for 'i' and 'l')
67 * '-' optional parameter (eg. '-f')
71 typedef struct mon_cmd_t
{
73 const char *args_type
;
78 void (*info
)(Monitor
*mon
);
82 /* file descriptors passed via SCM_RIGHTS */
83 typedef struct mon_fd_t mon_fd_t
;
87 QLIST_ENTRY(mon_fd_t
) next
;
100 BlockDriverCompletionFunc
*password_completion_cb
;
101 void *password_opaque
;
102 QLIST_HEAD(,mon_fd_t
) fds
;
103 QLIST_ENTRY(Monitor
) entry
;
106 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
108 static const mon_cmd_t mon_cmds
[];
109 static const mon_cmd_t info_cmds
[];
111 Monitor
*cur_mon
= NULL
;
113 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
116 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
118 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
120 readline_show_prompt(mon
->rs
);
123 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
127 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
128 /* prompt is printed on return from the command handler */
131 monitor_printf(mon
, "terminal does not support password prompting\n");
136 void monitor_flush(Monitor
*mon
)
138 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
139 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
140 mon
->outbuf_index
= 0;
144 /* flush at every end of line or if the buffer is full */
145 static void monitor_puts(Monitor
*mon
, const char *str
)
157 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
158 mon
->outbuf
[mon
->outbuf_index
++] = c
;
159 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
165 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
168 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
169 monitor_puts(mon
, buf
);
172 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
176 monitor_vprintf(mon
, fmt
, ap
);
180 void monitor_print_filename(Monitor
*mon
, const char *filename
)
184 for (i
= 0; filename
[i
]; i
++) {
185 switch (filename
[i
]) {
189 monitor_printf(mon
, "\\%c", filename
[i
]);
192 monitor_printf(mon
, "\\t");
195 monitor_printf(mon
, "\\r");
198 monitor_printf(mon
, "\\n");
201 monitor_printf(mon
, "%c", filename
[i
]);
207 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
211 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
216 static int compare_cmd(const char *name
, const char *list
)
218 const char *p
, *pstart
;
226 p
= pstart
+ strlen(pstart
);
227 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
236 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
237 const char *prefix
, const char *name
)
239 const mon_cmd_t
*cmd
;
241 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
242 if (!name
|| !strcmp(name
, cmd
->name
))
243 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
244 cmd
->params
, cmd
->help
);
248 static void help_cmd(Monitor
*mon
, const char *name
)
250 if (name
&& !strcmp(name
, "info")) {
251 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
253 help_cmd_dump(mon
, mon_cmds
, "", name
);
254 if (name
&& !strcmp(name
, "log")) {
255 const CPULogItem
*item
;
256 monitor_printf(mon
, "Log items (comma separated):\n");
257 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
258 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
259 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
265 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
267 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
270 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
274 const char *device
= qdict_get_str(qdict
, "device");
276 all_devices
= !strcmp(device
, "all");
277 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
279 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
281 bdrv_commit(dinfo
->bdrv
);
285 static void do_info(Monitor
*mon
, const QDict
*qdict
)
287 const mon_cmd_t
*cmd
;
288 const char *item
= qdict_get_try_str(qdict
, "item");
292 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
293 if (compare_cmd(item
, cmd
->name
))
297 help_cmd(mon
, "info");
300 cmd
->mhandler
.info(mon
);
303 static void do_info_version(Monitor
*mon
)
305 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
308 static void do_info_name(Monitor
*mon
)
311 monitor_printf(mon
, "%s\n", qemu_name
);
314 #if defined(TARGET_I386)
315 static void do_info_hpet(Monitor
*mon
)
317 monitor_printf(mon
, "HPET is %s by QEMU\n",
318 (no_hpet
) ? "disabled" : "enabled");
322 static void do_info_uuid(Monitor
*mon
)
324 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
325 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
326 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
327 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
328 qemu_uuid
[14], qemu_uuid
[15]);
331 /* get the current CPU defined by the user */
332 static int mon_set_cpu(int cpu_index
)
336 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
337 if (env
->cpu_index
== cpu_index
) {
338 cur_mon
->mon_cpu
= env
;
345 static CPUState
*mon_get_cpu(void)
347 if (!cur_mon
->mon_cpu
) {
350 cpu_synchronize_state(cur_mon
->mon_cpu
);
351 return cur_mon
->mon_cpu
;
354 static void do_info_registers(Monitor
*mon
)
361 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
364 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
369 static void do_info_cpus(Monitor
*mon
)
373 /* just to set the default cpu if not already done */
376 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
377 cpu_synchronize_state(env
);
378 monitor_printf(mon
, "%c CPU #%d:",
379 (env
== mon
->mon_cpu
) ? '*' : ' ',
381 #if defined(TARGET_I386)
382 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
383 env
->eip
+ env
->segs
[R_CS
].base
);
384 #elif defined(TARGET_PPC)
385 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
386 #elif defined(TARGET_SPARC)
387 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
389 #elif defined(TARGET_MIPS)
390 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
393 monitor_printf(mon
, " (halted)");
394 monitor_printf(mon
, "\n");
398 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
400 int index
= qdict_get_int(qdict
, "index");
401 if (mon_set_cpu(index
) < 0)
402 monitor_printf(mon
, "Invalid CPU index\n");
405 static void do_info_jit(Monitor
*mon
)
407 dump_exec_info((FILE *)mon
, monitor_fprintf
);
410 static void do_info_history(Monitor
*mon
)
419 str
= readline_get_history(mon
->rs
, i
);
422 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
427 #if defined(TARGET_PPC)
428 /* XXX: not implemented in other targets */
429 static void do_info_cpu_stats(Monitor
*mon
)
434 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
438 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
443 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
445 if (bdrv_is_inserted(bs
)) {
447 if (!bdrv_is_removable(bs
)) {
448 monitor_printf(mon
, "device is not removable\n");
451 if (bdrv_is_locked(bs
)) {
452 monitor_printf(mon
, "device is locked\n");
461 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
463 BlockDriverState
*bs
;
464 int force
= qdict_get_int(qdict
, "force");
465 const char *filename
= qdict_get_str(qdict
, "filename");
467 bs
= bdrv_find(filename
);
469 monitor_printf(mon
, "device not found\n");
472 eject_device(mon
, bs
, force
);
475 static void do_change_block(Monitor
*mon
, const char *device
,
476 const char *filename
, const char *fmt
)
478 BlockDriverState
*bs
;
479 BlockDriver
*drv
= NULL
;
481 bs
= bdrv_find(device
);
483 monitor_printf(mon
, "device not found\n");
487 drv
= bdrv_find_format(fmt
);
489 monitor_printf(mon
, "invalid format %s\n", fmt
);
493 if (eject_device(mon
, bs
, 0) < 0)
495 bdrv_open2(bs
, filename
, 0, drv
);
496 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
499 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
502 if (vnc_display_password(NULL
, password
) < 0)
503 monitor_printf(mon
, "could not set VNC server password\n");
505 monitor_read_command(mon
, 1);
508 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
510 if (strcmp(target
, "passwd") == 0 ||
511 strcmp(target
, "password") == 0) {
514 strncpy(password
, arg
, sizeof(password
));
515 password
[sizeof(password
) - 1] = '\0';
516 change_vnc_password_cb(mon
, password
, NULL
);
518 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
521 if (vnc_display_open(NULL
, target
) < 0)
522 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
526 static void do_change(Monitor
*mon
, const QDict
*qdict
)
528 const char *device
= qdict_get_str(qdict
, "device");
529 const char *target
= qdict_get_str(qdict
, "target");
530 const char *arg
= qdict_get_try_str(qdict
, "arg");
531 if (strcmp(device
, "vnc") == 0) {
532 do_change_vnc(mon
, target
, arg
);
534 do_change_block(mon
, device
, target
, arg
);
538 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
540 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
543 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
545 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
548 static void do_log(Monitor
*mon
, const QDict
*qdict
)
551 const char *items
= qdict_get_str(qdict
, "items");
553 if (!strcmp(items
, "none")) {
556 mask
= cpu_str_to_log_mask(items
);
558 help_cmd(mon
, "log");
565 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
567 const char *option
= qdict_get_try_str(qdict
, "option");
568 if (!option
|| !strcmp(option
, "on")) {
570 } else if (!strcmp(option
, "off")) {
573 monitor_printf(mon
, "unexpected option %s\n", option
);
577 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
579 vm_stop(EXCP_INTERRUPT
);
582 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
584 struct bdrv_iterate_context
{
589 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
591 struct bdrv_iterate_context context
= { mon
, 0 };
593 bdrv_iterate(encrypted_bdrv_it
, &context
);
594 /* only resume the vm if all keys are set and valid */
599 static void bdrv_key_cb(void *opaque
, int err
)
601 Monitor
*mon
= opaque
;
603 /* another key was set successfully, retry to continue */
608 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
610 struct bdrv_iterate_context
*context
= opaque
;
612 if (!context
->err
&& bdrv_key_required(bs
)) {
613 context
->err
= -EBUSY
;
614 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
619 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
621 const char *device
= qdict_get_try_str(qdict
, "device");
623 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
624 if (gdbserver_start(device
) < 0) {
625 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
627 } else if (strcmp(device
, "none") == 0) {
628 monitor_printf(mon
, "Disabled gdbserver\n");
630 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
635 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
637 const char *action
= qdict_get_str(qdict
, "action");
638 if (select_watchdog_action(action
) == -1) {
639 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
643 static void monitor_printc(Monitor
*mon
, int c
)
645 monitor_printf(mon
, "'");
648 monitor_printf(mon
, "\\'");
651 monitor_printf(mon
, "\\\\");
654 monitor_printf(mon
, "\\n");
657 monitor_printf(mon
, "\\r");
660 if (c
>= 32 && c
<= 126) {
661 monitor_printf(mon
, "%c", c
);
663 monitor_printf(mon
, "\\x%02x", c
);
667 monitor_printf(mon
, "'");
670 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
671 target_phys_addr_t addr
, int is_physical
)
674 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
682 if (!env
&& !is_physical
)
687 } else if (wsize
== 4) {
690 /* as default we use the current CS size */
694 if ((env
->efer
& MSR_EFER_LMA
) &&
695 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
699 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
704 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
713 nb_per_line
= line_size
/ wsize
;
718 max_digits
= (wsize
* 8 + 2) / 3;
722 max_digits
= (wsize
* 8) / 4;
726 max_digits
= (wsize
* 8 * 10 + 32) / 33;
735 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
737 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
742 cpu_physical_memory_rw(addr
, buf
, l
, 0);
747 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
748 monitor_printf(mon
, " Cannot access memory\n");
757 v
= ldub_raw(buf
+ i
);
760 v
= lduw_raw(buf
+ i
);
763 v
= (uint32_t)ldl_raw(buf
+ i
);
766 v
= ldq_raw(buf
+ i
);
769 monitor_printf(mon
, " ");
772 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
775 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
778 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
781 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
784 monitor_printc(mon
, v
);
789 monitor_printf(mon
, "\n");
795 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
797 int count
= qdict_get_int(qdict
, "count");
798 int format
= qdict_get_int(qdict
, "format");
799 int size
= qdict_get_int(qdict
, "size");
800 target_long addr
= qdict_get_int(qdict
, "addr");
802 memory_dump(mon
, count
, format
, size
, addr
, 0);
805 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
807 int count
= qdict_get_int(qdict
, "count");
808 int format
= qdict_get_int(qdict
, "format");
809 int size
= qdict_get_int(qdict
, "size");
810 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
812 memory_dump(mon
, count
, format
, size
, addr
, 1);
815 static void do_print(Monitor
*mon
, const QDict
*qdict
)
817 int format
= qdict_get_int(qdict
, "format");
818 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
820 #if TARGET_PHYS_ADDR_BITS == 32
823 monitor_printf(mon
, "%#o", val
);
826 monitor_printf(mon
, "%#x", val
);
829 monitor_printf(mon
, "%u", val
);
833 monitor_printf(mon
, "%d", val
);
836 monitor_printc(mon
, val
);
842 monitor_printf(mon
, "%#" PRIo64
, val
);
845 monitor_printf(mon
, "%#" PRIx64
, val
);
848 monitor_printf(mon
, "%" PRIu64
, val
);
852 monitor_printf(mon
, "%" PRId64
, val
);
855 monitor_printc(mon
, val
);
859 monitor_printf(mon
, "\n");
862 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
865 uint32_t size
= qdict_get_int(qdict
, "size");
866 const char *filename
= qdict_get_str(qdict
, "filename");
867 target_long addr
= qdict_get_int(qdict
, "val");
876 f
= fopen(filename
, "wb");
878 monitor_printf(mon
, "could not open '%s'\n", filename
);
885 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
886 fwrite(buf
, 1, l
, f
);
893 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
898 uint32_t size
= qdict_get_int(qdict
, "size");
899 const char *filename
= qdict_get_str(qdict
, "filename");
900 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
902 f
= fopen(filename
, "wb");
904 monitor_printf(mon
, "could not open '%s'\n", filename
);
911 cpu_physical_memory_rw(addr
, buf
, l
, 0);
912 fwrite(buf
, 1, l
, f
);
920 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
925 uint32_t start
= qdict_get_int(qdict
, "start");
926 uint32_t size
= qdict_get_int(qdict
, "size");
929 for(addr
= start
; addr
< (start
+ size
); addr
++) {
930 cpu_physical_memory_rw(addr
, buf
, 1, 0);
931 /* BSD sum algorithm ('sum' Unix command) */
932 sum
= (sum
>> 1) | (sum
<< 15);
935 monitor_printf(mon
, "%05d\n", sum
);
943 static const KeyDef key_defs
[] = {
970 { 0x0e, "backspace" },
1007 { 0x37, "asterisk" },
1010 { 0x3a, "caps_lock" },
1021 { 0x45, "num_lock" },
1022 { 0x46, "scroll_lock" },
1024 { 0xb5, "kp_divide" },
1025 { 0x37, "kp_multiply" },
1026 { 0x4a, "kp_subtract" },
1028 { 0x9c, "kp_enter" },
1029 { 0x53, "kp_decimal" },
1062 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1077 { 0xfe, "compose" },
1082 static int get_keycode(const char *key
)
1088 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1089 if (!strcmp(key
, p
->name
))
1092 if (strstart(key
, "0x", NULL
)) {
1093 ret
= strtoul(key
, &endp
, 0);
1094 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1100 #define MAX_KEYCODES 16
1101 static uint8_t keycodes
[MAX_KEYCODES
];
1102 static int nb_pending_keycodes
;
1103 static QEMUTimer
*key_timer
;
1105 static void release_keys(void *opaque
)
1109 while (nb_pending_keycodes
> 0) {
1110 nb_pending_keycodes
--;
1111 keycode
= keycodes
[nb_pending_keycodes
];
1113 kbd_put_keycode(0xe0);
1114 kbd_put_keycode(keycode
| 0x80);
1118 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1120 char keyname_buf
[16];
1122 int keyname_len
, keycode
, i
;
1123 const char *string
= qdict_get_str(qdict
, "string");
1124 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1125 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1127 if (nb_pending_keycodes
> 0) {
1128 qemu_del_timer(key_timer
);
1135 separator
= strchr(string
, '-');
1136 keyname_len
= separator
? separator
- string
: strlen(string
);
1137 if (keyname_len
> 0) {
1138 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1139 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1140 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1143 if (i
== MAX_KEYCODES
) {
1144 monitor_printf(mon
, "too many keys\n");
1147 keyname_buf
[keyname_len
] = 0;
1148 keycode
= get_keycode(keyname_buf
);
1150 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1153 keycodes
[i
++] = keycode
;
1157 string
= separator
+ 1;
1159 nb_pending_keycodes
= i
;
1160 /* key down events */
1161 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1162 keycode
= keycodes
[i
];
1164 kbd_put_keycode(0xe0);
1165 kbd_put_keycode(keycode
& 0x7f);
1167 /* delayed key up events */
1168 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1169 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1172 static int mouse_button_state
;
1174 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1177 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1178 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1179 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1180 dx
= strtol(dx_str
, NULL
, 0);
1181 dy
= strtol(dy_str
, NULL
, 0);
1184 dz
= strtol(dz_str
, NULL
, 0);
1185 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1188 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1190 int button_state
= qdict_get_int(qdict
, "button_state");
1191 mouse_button_state
= button_state
;
1192 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1195 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1197 int size
= qdict_get_int(qdict
, "size");
1198 int addr
= qdict_get_int(qdict
, "addr");
1199 int has_index
= qdict_haskey(qdict
, "index");
1204 int index
= qdict_get_int(qdict
, "index");
1205 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1213 val
= cpu_inb(addr
);
1217 val
= cpu_inw(addr
);
1221 val
= cpu_inl(addr
);
1225 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1226 suffix
, addr
, size
* 2, val
);
1229 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1231 int size
= qdict_get_int(qdict
, "size");
1232 int addr
= qdict_get_int(qdict
, "addr");
1233 int val
= qdict_get_int(qdict
, "val");
1235 addr
&= IOPORTS_MASK
;
1240 cpu_outb(addr
, val
);
1243 cpu_outw(addr
, val
);
1246 cpu_outl(addr
, val
);
1251 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1254 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1256 res
= qemu_boot_set(bootdevice
);
1258 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1259 } else if (res
> 0) {
1260 monitor_printf(mon
, "setting boot device list failed\n");
1262 monitor_printf(mon
, "no function defined to set boot device list for "
1263 "this architecture\n");
1267 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1269 qemu_system_reset_request();
1272 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1274 qemu_system_powerdown_request();
1277 #if defined(TARGET_I386)
1278 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1280 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1283 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1284 pte
& PG_PSE_MASK
? 'P' : '-',
1285 pte
& PG_DIRTY_MASK
? 'D' : '-',
1286 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1287 pte
& PG_PCD_MASK
? 'C' : '-',
1288 pte
& PG_PWT_MASK
? 'T' : '-',
1289 pte
& PG_USER_MASK
? 'U' : '-',
1290 pte
& PG_RW_MASK
? 'W' : '-');
1293 static void tlb_info(Monitor
*mon
)
1297 uint32_t pgd
, pde
, pte
;
1299 env
= mon_get_cpu();
1303 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1304 monitor_printf(mon
, "PG disabled\n");
1307 pgd
= env
->cr
[3] & ~0xfff;
1308 for(l1
= 0; l1
< 1024; l1
++) {
1309 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1310 pde
= le32_to_cpu(pde
);
1311 if (pde
& PG_PRESENT_MASK
) {
1312 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1313 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1315 for(l2
= 0; l2
< 1024; l2
++) {
1316 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1317 (uint8_t *)&pte
, 4);
1318 pte
= le32_to_cpu(pte
);
1319 if (pte
& PG_PRESENT_MASK
) {
1320 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1330 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1331 uint32_t end
, int prot
)
1334 prot1
= *plast_prot
;
1335 if (prot
!= prot1
) {
1336 if (*pstart
!= -1) {
1337 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1338 *pstart
, end
, end
- *pstart
,
1339 prot1
& PG_USER_MASK
? 'u' : '-',
1341 prot1
& PG_RW_MASK
? 'w' : '-');
1351 static void mem_info(Monitor
*mon
)
1354 int l1
, l2
, prot
, last_prot
;
1355 uint32_t pgd
, pde
, pte
, start
, end
;
1357 env
= mon_get_cpu();
1361 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1362 monitor_printf(mon
, "PG disabled\n");
1365 pgd
= env
->cr
[3] & ~0xfff;
1368 for(l1
= 0; l1
< 1024; l1
++) {
1369 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1370 pde
= le32_to_cpu(pde
);
1372 if (pde
& PG_PRESENT_MASK
) {
1373 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1374 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1375 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1377 for(l2
= 0; l2
< 1024; l2
++) {
1378 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1379 (uint8_t *)&pte
, 4);
1380 pte
= le32_to_cpu(pte
);
1381 end
= (l1
<< 22) + (l2
<< 12);
1382 if (pte
& PG_PRESENT_MASK
) {
1383 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1387 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1392 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1398 #if defined(TARGET_SH4)
1400 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1402 monitor_printf(mon
, " tlb%i:\t"
1403 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1404 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1405 "dirty=%hhu writethrough=%hhu\n",
1407 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1408 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1412 static void tlb_info(Monitor
*mon
)
1414 CPUState
*env
= mon_get_cpu();
1417 monitor_printf (mon
, "ITLB:\n");
1418 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1419 print_tlb (mon
, i
, &env
->itlb
[i
]);
1420 monitor_printf (mon
, "UTLB:\n");
1421 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1422 print_tlb (mon
, i
, &env
->utlb
[i
]);
1427 static void do_info_kvm(Monitor
*mon
)
1430 monitor_printf(mon
, "kvm support: ");
1432 monitor_printf(mon
, "enabled\n");
1434 monitor_printf(mon
, "disabled\n");
1436 monitor_printf(mon
, "kvm support: not compiled\n");
1440 static void do_info_numa(Monitor
*mon
)
1445 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1446 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1447 monitor_printf(mon
, "node %d cpus:", i
);
1448 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1449 if (env
->numa_node
== i
) {
1450 monitor_printf(mon
, " %d", env
->cpu_index
);
1453 monitor_printf(mon
, "\n");
1454 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1459 #ifdef CONFIG_PROFILER
1464 static void do_info_profile(Monitor
*mon
)
1470 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1471 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1472 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1473 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1478 static void do_info_profile(Monitor
*mon
)
1480 monitor_printf(mon
, "Internal profiler not compiled\n");
1484 /* Capture support */
1485 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1487 static void do_info_capture(Monitor
*mon
)
1492 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1493 monitor_printf(mon
, "[%d]: ", i
);
1494 s
->ops
.info (s
->opaque
);
1499 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1502 int n
= qdict_get_int(qdict
, "n");
1505 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1507 s
->ops
.destroy (s
->opaque
);
1508 QLIST_REMOVE (s
, entries
);
1515 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1517 const char *path
= qdict_get_str(qdict
, "path");
1518 int has_freq
= qdict_haskey(qdict
, "freq");
1519 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1520 int has_bits
= qdict_haskey(qdict
, "bits");
1521 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1522 int has_channels
= qdict_haskey(qdict
, "nchannels");
1523 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1526 s
= qemu_mallocz (sizeof (*s
));
1528 freq
= has_freq
? freq
: 44100;
1529 bits
= has_bits
? bits
: 16;
1530 nchannels
= has_channels
? nchannels
: 2;
1532 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1533 monitor_printf(mon
, "Faied to add wave capture\n");
1536 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1540 #if defined(TARGET_I386)
1541 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1544 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1546 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1547 if (env
->cpu_index
== cpu_index
) {
1548 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1554 static void do_info_status(Monitor
*mon
)
1558 monitor_printf(mon
, "VM status: running (single step mode)\n");
1560 monitor_printf(mon
, "VM status: running\n");
1563 monitor_printf(mon
, "VM status: paused\n");
1567 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1569 int value
= qdict_get_int(qdict
, "value");
1570 ram_addr_t target
= value
;
1571 qemu_balloon(target
<< 20);
1574 static void do_info_balloon(Monitor
*mon
)
1578 actual
= qemu_balloon_status();
1579 if (kvm_enabled() && !kvm_has_sync_mmu())
1580 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1581 "ballooning disabled\n");
1582 else if (actual
== 0)
1583 monitor_printf(mon
, "Ballooning not activated in VM\n");
1585 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1588 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1590 qemu_acl
*acl
= qemu_acl_find(name
);
1593 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1598 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1600 const char *aclname
= qdict_get_str(qdict
, "aclname");
1601 qemu_acl
*acl
= find_acl(mon
, aclname
);
1602 qemu_acl_entry
*entry
;
1606 monitor_printf(mon
, "policy: %s\n",
1607 acl
->defaultDeny
? "deny" : "allow");
1608 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1610 monitor_printf(mon
, "%d: %s %s\n", i
,
1611 entry
->deny
? "deny" : "allow", entry
->match
);
1616 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1618 const char *aclname
= qdict_get_str(qdict
, "aclname");
1619 qemu_acl
*acl
= find_acl(mon
, aclname
);
1622 qemu_acl_reset(acl
);
1623 monitor_printf(mon
, "acl: removed all rules\n");
1627 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1629 const char *aclname
= qdict_get_str(qdict
, "aclname");
1630 const char *policy
= qdict_get_str(qdict
, "policy");
1631 qemu_acl
*acl
= find_acl(mon
, aclname
);
1634 if (strcmp(policy
, "allow") == 0) {
1635 acl
->defaultDeny
= 0;
1636 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1637 } else if (strcmp(policy
, "deny") == 0) {
1638 acl
->defaultDeny
= 1;
1639 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1641 monitor_printf(mon
, "acl: unknown policy '%s', "
1642 "expected 'deny' or 'allow'\n", policy
);
1647 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1649 const char *aclname
= qdict_get_str(qdict
, "aclname");
1650 const char *match
= qdict_get_str(qdict
, "match");
1651 const char *policy
= qdict_get_str(qdict
, "policy");
1652 int has_index
= qdict_haskey(qdict
, "index");
1653 int index
= qdict_get_try_int(qdict
, "index", -1);
1654 qemu_acl
*acl
= find_acl(mon
, aclname
);
1658 if (strcmp(policy
, "allow") == 0) {
1660 } else if (strcmp(policy
, "deny") == 0) {
1663 monitor_printf(mon
, "acl: unknown policy '%s', "
1664 "expected 'deny' or 'allow'\n", policy
);
1668 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1670 ret
= qemu_acl_append(acl
, deny
, match
);
1672 monitor_printf(mon
, "acl: unable to add acl entry\n");
1674 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1678 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1680 const char *aclname
= qdict_get_str(qdict
, "aclname");
1681 const char *match
= qdict_get_str(qdict
, "match");
1682 qemu_acl
*acl
= find_acl(mon
, aclname
);
1686 ret
= qemu_acl_remove(acl
, match
);
1688 monitor_printf(mon
, "acl: no matching acl entry\n");
1690 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1694 #if defined(TARGET_I386)
1695 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1698 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1699 int bank
= qdict_get_int(qdict
, "bank");
1700 uint64_t status
= qdict_get_int(qdict
, "status");
1701 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1702 uint64_t addr
= qdict_get_int(qdict
, "addr");
1703 uint64_t misc
= qdict_get_int(qdict
, "misc");
1705 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1706 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1707 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1713 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1715 const char *fdname
= qdict_get_str(qdict
, "fdname");
1719 fd
= qemu_chr_get_msgfd(mon
->chr
);
1721 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1725 if (qemu_isdigit(fdname
[0])) {
1726 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1732 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1737 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1738 if (strcmp(monfd
->name
, fdname
) != 0) {
1747 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1748 monfd
->name
= qemu_strdup(fdname
);
1751 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1754 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1756 const char *fdname
= qdict_get_str(qdict
, "fdname");
1759 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1760 if (strcmp(monfd
->name
, fdname
) != 0) {
1764 QLIST_REMOVE(monfd
, next
);
1766 qemu_free(monfd
->name
);
1771 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1775 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1777 int saved_vm_running
= vm_running
;
1778 const char *name
= qdict_get_str(qdict
, "name");
1782 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1786 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1790 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1793 if (strcmp(monfd
->name
, fdname
) != 0) {
1799 /* caller takes ownership of fd */
1800 QLIST_REMOVE(monfd
, next
);
1801 qemu_free(monfd
->name
);
1810 static const mon_cmd_t mon_cmds
[] = {
1811 #include "qemu-monitor.h"
1815 /* Please update qemu-monitor.hx when adding or changing commands */
1816 static const mon_cmd_t info_cmds
[] = {
1821 .help
= "show the version of QEMU",
1822 .mhandler
.info
= do_info_version
,
1828 .help
= "show the network state",
1829 .mhandler
.info
= do_info_network
,
1835 .help
= "show the character devices",
1836 .mhandler
.info
= qemu_chr_info
,
1842 .help
= "show the block devices",
1843 .mhandler
.info
= bdrv_info
,
1846 .name
= "blockstats",
1849 .help
= "show block device statistics",
1850 .mhandler
.info
= bdrv_info_stats
,
1853 .name
= "registers",
1856 .help
= "show the cpu registers",
1857 .mhandler
.info
= do_info_registers
,
1863 .help
= "show infos for each CPU",
1864 .mhandler
.info
= do_info_cpus
,
1870 .help
= "show the command line history",
1871 .mhandler
.info
= do_info_history
,
1877 .help
= "show the interrupts statistics (if available)",
1878 .mhandler
.info
= irq_info
,
1884 .help
= "show i8259 (PIC) state",
1885 .mhandler
.info
= pic_info
,
1891 .help
= "show PCI info",
1892 .mhandler
.info
= pci_info
,
1894 #if defined(TARGET_I386) || defined(TARGET_SH4)
1899 .help
= "show virtual to physical memory mappings",
1900 .mhandler
.info
= tlb_info
,
1903 #if defined(TARGET_I386)
1908 .help
= "show the active virtual memory mappings",
1909 .mhandler
.info
= mem_info
,
1915 .help
= "show state of HPET",
1916 .mhandler
.info
= do_info_hpet
,
1923 .help
= "show dynamic compiler info",
1924 .mhandler
.info
= do_info_jit
,
1930 .help
= "show KVM information",
1931 .mhandler
.info
= do_info_kvm
,
1937 .help
= "show NUMA information",
1938 .mhandler
.info
= do_info_numa
,
1944 .help
= "show guest USB devices",
1945 .mhandler
.info
= usb_info
,
1951 .help
= "show host USB devices",
1952 .mhandler
.info
= usb_host_info
,
1958 .help
= "show profiling information",
1959 .mhandler
.info
= do_info_profile
,
1965 .help
= "show capture information",
1966 .mhandler
.info
= do_info_capture
,
1969 .name
= "snapshots",
1972 .help
= "show the currently saved VM snapshots",
1973 .mhandler
.info
= do_info_snapshots
,
1979 .help
= "show the current VM status (running|paused)",
1980 .mhandler
.info
= do_info_status
,
1986 .help
= "show guest PCMCIA status",
1987 .mhandler
.info
= pcmcia_info
,
1993 .help
= "show which guest mouse is receiving events",
1994 .mhandler
.info
= do_info_mice
,
2000 .help
= "show the vnc server status",
2001 .mhandler
.info
= do_info_vnc
,
2007 .help
= "show the current VM name",
2008 .mhandler
.info
= do_info_name
,
2014 .help
= "show the current VM UUID",
2015 .mhandler
.info
= do_info_uuid
,
2017 #if defined(TARGET_PPC)
2022 .help
= "show CPU statistics",
2023 .mhandler
.info
= do_info_cpu_stats
,
2026 #if defined(CONFIG_SLIRP)
2031 .help
= "show user network stack connection states",
2032 .mhandler
.info
= do_info_usernet
,
2039 .help
= "show migration status",
2040 .mhandler
.info
= do_info_migrate
,
2046 .help
= "show balloon information",
2047 .mhandler
.info
= do_info_balloon
,
2053 .help
= "show device tree",
2054 .mhandler
.info
= do_info_qtree
,
2060 .help
= "show qdev device model list",
2061 .mhandler
.info
= do_info_qdm
,
2067 .help
= "show roms",
2068 .mhandler
.info
= do_info_roms
,
2075 /*******************************************************************/
2077 static const char *pch
;
2078 static jmp_buf expr_env
;
2083 typedef struct MonitorDef
{
2086 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2090 #if defined(TARGET_I386)
2091 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2093 CPUState
*env
= mon_get_cpu();
2096 return env
->eip
+ env
->segs
[R_CS
].base
;
2100 #if defined(TARGET_PPC)
2101 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2103 CPUState
*env
= mon_get_cpu();
2111 for (i
= 0; i
< 8; i
++)
2112 u
|= env
->crf
[i
] << (32 - (4 * i
));
2117 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2119 CPUState
*env
= mon_get_cpu();
2125 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2127 CPUState
*env
= mon_get_cpu();
2133 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2135 CPUState
*env
= mon_get_cpu();
2138 return cpu_ppc_load_decr(env
);
2141 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2143 CPUState
*env
= mon_get_cpu();
2146 return cpu_ppc_load_tbu(env
);
2149 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2151 CPUState
*env
= mon_get_cpu();
2154 return cpu_ppc_load_tbl(env
);
2158 #if defined(TARGET_SPARC)
2159 #ifndef TARGET_SPARC64
2160 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2162 CPUState
*env
= mon_get_cpu();
2165 return GET_PSR(env
);
2169 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2171 CPUState
*env
= mon_get_cpu();
2174 return env
->regwptr
[val
];
2178 static const MonitorDef monitor_defs
[] = {
2181 #define SEG(name, seg) \
2182 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2183 { name ".base", offsetof(CPUState, segs[seg].base) },\
2184 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2186 { "eax", offsetof(CPUState
, regs
[0]) },
2187 { "ecx", offsetof(CPUState
, regs
[1]) },
2188 { "edx", offsetof(CPUState
, regs
[2]) },
2189 { "ebx", offsetof(CPUState
, regs
[3]) },
2190 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2191 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2192 { "esi", offsetof(CPUState
, regs
[6]) },
2193 { "edi", offsetof(CPUState
, regs
[7]) },
2194 #ifdef TARGET_X86_64
2195 { "r8", offsetof(CPUState
, regs
[8]) },
2196 { "r9", offsetof(CPUState
, regs
[9]) },
2197 { "r10", offsetof(CPUState
, regs
[10]) },
2198 { "r11", offsetof(CPUState
, regs
[11]) },
2199 { "r12", offsetof(CPUState
, regs
[12]) },
2200 { "r13", offsetof(CPUState
, regs
[13]) },
2201 { "r14", offsetof(CPUState
, regs
[14]) },
2202 { "r15", offsetof(CPUState
, regs
[15]) },
2204 { "eflags", offsetof(CPUState
, eflags
) },
2205 { "eip", offsetof(CPUState
, eip
) },
2212 { "pc", 0, monitor_get_pc
, },
2213 #elif defined(TARGET_PPC)
2214 /* General purpose registers */
2215 { "r0", offsetof(CPUState
, gpr
[0]) },
2216 { "r1", offsetof(CPUState
, gpr
[1]) },
2217 { "r2", offsetof(CPUState
, gpr
[2]) },
2218 { "r3", offsetof(CPUState
, gpr
[3]) },
2219 { "r4", offsetof(CPUState
, gpr
[4]) },
2220 { "r5", offsetof(CPUState
, gpr
[5]) },
2221 { "r6", offsetof(CPUState
, gpr
[6]) },
2222 { "r7", offsetof(CPUState
, gpr
[7]) },
2223 { "r8", offsetof(CPUState
, gpr
[8]) },
2224 { "r9", offsetof(CPUState
, gpr
[9]) },
2225 { "r10", offsetof(CPUState
, gpr
[10]) },
2226 { "r11", offsetof(CPUState
, gpr
[11]) },
2227 { "r12", offsetof(CPUState
, gpr
[12]) },
2228 { "r13", offsetof(CPUState
, gpr
[13]) },
2229 { "r14", offsetof(CPUState
, gpr
[14]) },
2230 { "r15", offsetof(CPUState
, gpr
[15]) },
2231 { "r16", offsetof(CPUState
, gpr
[16]) },
2232 { "r17", offsetof(CPUState
, gpr
[17]) },
2233 { "r18", offsetof(CPUState
, gpr
[18]) },
2234 { "r19", offsetof(CPUState
, gpr
[19]) },
2235 { "r20", offsetof(CPUState
, gpr
[20]) },
2236 { "r21", offsetof(CPUState
, gpr
[21]) },
2237 { "r22", offsetof(CPUState
, gpr
[22]) },
2238 { "r23", offsetof(CPUState
, gpr
[23]) },
2239 { "r24", offsetof(CPUState
, gpr
[24]) },
2240 { "r25", offsetof(CPUState
, gpr
[25]) },
2241 { "r26", offsetof(CPUState
, gpr
[26]) },
2242 { "r27", offsetof(CPUState
, gpr
[27]) },
2243 { "r28", offsetof(CPUState
, gpr
[28]) },
2244 { "r29", offsetof(CPUState
, gpr
[29]) },
2245 { "r30", offsetof(CPUState
, gpr
[30]) },
2246 { "r31", offsetof(CPUState
, gpr
[31]) },
2247 /* Floating point registers */
2248 { "f0", offsetof(CPUState
, fpr
[0]) },
2249 { "f1", offsetof(CPUState
, fpr
[1]) },
2250 { "f2", offsetof(CPUState
, fpr
[2]) },
2251 { "f3", offsetof(CPUState
, fpr
[3]) },
2252 { "f4", offsetof(CPUState
, fpr
[4]) },
2253 { "f5", offsetof(CPUState
, fpr
[5]) },
2254 { "f6", offsetof(CPUState
, fpr
[6]) },
2255 { "f7", offsetof(CPUState
, fpr
[7]) },
2256 { "f8", offsetof(CPUState
, fpr
[8]) },
2257 { "f9", offsetof(CPUState
, fpr
[9]) },
2258 { "f10", offsetof(CPUState
, fpr
[10]) },
2259 { "f11", offsetof(CPUState
, fpr
[11]) },
2260 { "f12", offsetof(CPUState
, fpr
[12]) },
2261 { "f13", offsetof(CPUState
, fpr
[13]) },
2262 { "f14", offsetof(CPUState
, fpr
[14]) },
2263 { "f15", offsetof(CPUState
, fpr
[15]) },
2264 { "f16", offsetof(CPUState
, fpr
[16]) },
2265 { "f17", offsetof(CPUState
, fpr
[17]) },
2266 { "f18", offsetof(CPUState
, fpr
[18]) },
2267 { "f19", offsetof(CPUState
, fpr
[19]) },
2268 { "f20", offsetof(CPUState
, fpr
[20]) },
2269 { "f21", offsetof(CPUState
, fpr
[21]) },
2270 { "f22", offsetof(CPUState
, fpr
[22]) },
2271 { "f23", offsetof(CPUState
, fpr
[23]) },
2272 { "f24", offsetof(CPUState
, fpr
[24]) },
2273 { "f25", offsetof(CPUState
, fpr
[25]) },
2274 { "f26", offsetof(CPUState
, fpr
[26]) },
2275 { "f27", offsetof(CPUState
, fpr
[27]) },
2276 { "f28", offsetof(CPUState
, fpr
[28]) },
2277 { "f29", offsetof(CPUState
, fpr
[29]) },
2278 { "f30", offsetof(CPUState
, fpr
[30]) },
2279 { "f31", offsetof(CPUState
, fpr
[31]) },
2280 { "fpscr", offsetof(CPUState
, fpscr
) },
2281 /* Next instruction pointer */
2282 { "nip|pc", offsetof(CPUState
, nip
) },
2283 { "lr", offsetof(CPUState
, lr
) },
2284 { "ctr", offsetof(CPUState
, ctr
) },
2285 { "decr", 0, &monitor_get_decr
, },
2286 { "ccr", 0, &monitor_get_ccr
, },
2287 /* Machine state register */
2288 { "msr", 0, &monitor_get_msr
, },
2289 { "xer", 0, &monitor_get_xer
, },
2290 { "tbu", 0, &monitor_get_tbu
, },
2291 { "tbl", 0, &monitor_get_tbl
, },
2292 #if defined(TARGET_PPC64)
2293 /* Address space register */
2294 { "asr", offsetof(CPUState
, asr
) },
2296 /* Segment registers */
2297 { "sdr1", offsetof(CPUState
, sdr1
) },
2298 { "sr0", offsetof(CPUState
, sr
[0]) },
2299 { "sr1", offsetof(CPUState
, sr
[1]) },
2300 { "sr2", offsetof(CPUState
, sr
[2]) },
2301 { "sr3", offsetof(CPUState
, sr
[3]) },
2302 { "sr4", offsetof(CPUState
, sr
[4]) },
2303 { "sr5", offsetof(CPUState
, sr
[5]) },
2304 { "sr6", offsetof(CPUState
, sr
[6]) },
2305 { "sr7", offsetof(CPUState
, sr
[7]) },
2306 { "sr8", offsetof(CPUState
, sr
[8]) },
2307 { "sr9", offsetof(CPUState
, sr
[9]) },
2308 { "sr10", offsetof(CPUState
, sr
[10]) },
2309 { "sr11", offsetof(CPUState
, sr
[11]) },
2310 { "sr12", offsetof(CPUState
, sr
[12]) },
2311 { "sr13", offsetof(CPUState
, sr
[13]) },
2312 { "sr14", offsetof(CPUState
, sr
[14]) },
2313 { "sr15", offsetof(CPUState
, sr
[15]) },
2314 /* Too lazy to put BATs and SPRs ... */
2315 #elif defined(TARGET_SPARC)
2316 { "g0", offsetof(CPUState
, gregs
[0]) },
2317 { "g1", offsetof(CPUState
, gregs
[1]) },
2318 { "g2", offsetof(CPUState
, gregs
[2]) },
2319 { "g3", offsetof(CPUState
, gregs
[3]) },
2320 { "g4", offsetof(CPUState
, gregs
[4]) },
2321 { "g5", offsetof(CPUState
, gregs
[5]) },
2322 { "g6", offsetof(CPUState
, gregs
[6]) },
2323 { "g7", offsetof(CPUState
, gregs
[7]) },
2324 { "o0", 0, monitor_get_reg
},
2325 { "o1", 1, monitor_get_reg
},
2326 { "o2", 2, monitor_get_reg
},
2327 { "o3", 3, monitor_get_reg
},
2328 { "o4", 4, monitor_get_reg
},
2329 { "o5", 5, monitor_get_reg
},
2330 { "o6", 6, monitor_get_reg
},
2331 { "o7", 7, monitor_get_reg
},
2332 { "l0", 8, monitor_get_reg
},
2333 { "l1", 9, monitor_get_reg
},
2334 { "l2", 10, monitor_get_reg
},
2335 { "l3", 11, monitor_get_reg
},
2336 { "l4", 12, monitor_get_reg
},
2337 { "l5", 13, monitor_get_reg
},
2338 { "l6", 14, monitor_get_reg
},
2339 { "l7", 15, monitor_get_reg
},
2340 { "i0", 16, monitor_get_reg
},
2341 { "i1", 17, monitor_get_reg
},
2342 { "i2", 18, monitor_get_reg
},
2343 { "i3", 19, monitor_get_reg
},
2344 { "i4", 20, monitor_get_reg
},
2345 { "i5", 21, monitor_get_reg
},
2346 { "i6", 22, monitor_get_reg
},
2347 { "i7", 23, monitor_get_reg
},
2348 { "pc", offsetof(CPUState
, pc
) },
2349 { "npc", offsetof(CPUState
, npc
) },
2350 { "y", offsetof(CPUState
, y
) },
2351 #ifndef TARGET_SPARC64
2352 { "psr", 0, &monitor_get_psr
, },
2353 { "wim", offsetof(CPUState
, wim
) },
2355 { "tbr", offsetof(CPUState
, tbr
) },
2356 { "fsr", offsetof(CPUState
, fsr
) },
2357 { "f0", offsetof(CPUState
, fpr
[0]) },
2358 { "f1", offsetof(CPUState
, fpr
[1]) },
2359 { "f2", offsetof(CPUState
, fpr
[2]) },
2360 { "f3", offsetof(CPUState
, fpr
[3]) },
2361 { "f4", offsetof(CPUState
, fpr
[4]) },
2362 { "f5", offsetof(CPUState
, fpr
[5]) },
2363 { "f6", offsetof(CPUState
, fpr
[6]) },
2364 { "f7", offsetof(CPUState
, fpr
[7]) },
2365 { "f8", offsetof(CPUState
, fpr
[8]) },
2366 { "f9", offsetof(CPUState
, fpr
[9]) },
2367 { "f10", offsetof(CPUState
, fpr
[10]) },
2368 { "f11", offsetof(CPUState
, fpr
[11]) },
2369 { "f12", offsetof(CPUState
, fpr
[12]) },
2370 { "f13", offsetof(CPUState
, fpr
[13]) },
2371 { "f14", offsetof(CPUState
, fpr
[14]) },
2372 { "f15", offsetof(CPUState
, fpr
[15]) },
2373 { "f16", offsetof(CPUState
, fpr
[16]) },
2374 { "f17", offsetof(CPUState
, fpr
[17]) },
2375 { "f18", offsetof(CPUState
, fpr
[18]) },
2376 { "f19", offsetof(CPUState
, fpr
[19]) },
2377 { "f20", offsetof(CPUState
, fpr
[20]) },
2378 { "f21", offsetof(CPUState
, fpr
[21]) },
2379 { "f22", offsetof(CPUState
, fpr
[22]) },
2380 { "f23", offsetof(CPUState
, fpr
[23]) },
2381 { "f24", offsetof(CPUState
, fpr
[24]) },
2382 { "f25", offsetof(CPUState
, fpr
[25]) },
2383 { "f26", offsetof(CPUState
, fpr
[26]) },
2384 { "f27", offsetof(CPUState
, fpr
[27]) },
2385 { "f28", offsetof(CPUState
, fpr
[28]) },
2386 { "f29", offsetof(CPUState
, fpr
[29]) },
2387 { "f30", offsetof(CPUState
, fpr
[30]) },
2388 { "f31", offsetof(CPUState
, fpr
[31]) },
2389 #ifdef TARGET_SPARC64
2390 { "f32", offsetof(CPUState
, fpr
[32]) },
2391 { "f34", offsetof(CPUState
, fpr
[34]) },
2392 { "f36", offsetof(CPUState
, fpr
[36]) },
2393 { "f38", offsetof(CPUState
, fpr
[38]) },
2394 { "f40", offsetof(CPUState
, fpr
[40]) },
2395 { "f42", offsetof(CPUState
, fpr
[42]) },
2396 { "f44", offsetof(CPUState
, fpr
[44]) },
2397 { "f46", offsetof(CPUState
, fpr
[46]) },
2398 { "f48", offsetof(CPUState
, fpr
[48]) },
2399 { "f50", offsetof(CPUState
, fpr
[50]) },
2400 { "f52", offsetof(CPUState
, fpr
[52]) },
2401 { "f54", offsetof(CPUState
, fpr
[54]) },
2402 { "f56", offsetof(CPUState
, fpr
[56]) },
2403 { "f58", offsetof(CPUState
, fpr
[58]) },
2404 { "f60", offsetof(CPUState
, fpr
[60]) },
2405 { "f62", offsetof(CPUState
, fpr
[62]) },
2406 { "asi", offsetof(CPUState
, asi
) },
2407 { "pstate", offsetof(CPUState
, pstate
) },
2408 { "cansave", offsetof(CPUState
, cansave
) },
2409 { "canrestore", offsetof(CPUState
, canrestore
) },
2410 { "otherwin", offsetof(CPUState
, otherwin
) },
2411 { "wstate", offsetof(CPUState
, wstate
) },
2412 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2413 { "fprs", offsetof(CPUState
, fprs
) },
2419 static void expr_error(Monitor
*mon
, const char *msg
)
2421 monitor_printf(mon
, "%s\n", msg
);
2422 longjmp(expr_env
, 1);
2425 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2426 static int get_monitor_def(target_long
*pval
, const char *name
)
2428 const MonitorDef
*md
;
2431 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2432 if (compare_cmd(name
, md
->name
)) {
2433 if (md
->get_value
) {
2434 *pval
= md
->get_value(md
, md
->offset
);
2436 CPUState
*env
= mon_get_cpu();
2439 ptr
= (uint8_t *)env
+ md
->offset
;
2442 *pval
= *(int32_t *)ptr
;
2445 *pval
= *(target_long
*)ptr
;
2458 static void next(void)
2462 while (qemu_isspace(*pch
))
2467 static int64_t expr_sum(Monitor
*mon
);
2469 static int64_t expr_unary(Monitor
*mon
)
2478 n
= expr_unary(mon
);
2482 n
= -expr_unary(mon
);
2486 n
= ~expr_unary(mon
);
2492 expr_error(mon
, "')' expected");
2499 expr_error(mon
, "character constant expected");
2503 expr_error(mon
, "missing terminating \' character");
2513 while ((*pch
>= 'a' && *pch
<= 'z') ||
2514 (*pch
>= 'A' && *pch
<= 'Z') ||
2515 (*pch
>= '0' && *pch
<= '9') ||
2516 *pch
== '_' || *pch
== '.') {
2517 if ((q
- buf
) < sizeof(buf
) - 1)
2521 while (qemu_isspace(*pch
))
2524 ret
= get_monitor_def(®
, buf
);
2526 expr_error(mon
, "unknown register");
2528 expr_error(mon
, "no cpu defined");
2533 expr_error(mon
, "unexpected end of expression");
2537 #if TARGET_PHYS_ADDR_BITS > 32
2538 n
= strtoull(pch
, &p
, 0);
2540 n
= strtoul(pch
, &p
, 0);
2543 expr_error(mon
, "invalid char in expression");
2546 while (qemu_isspace(*pch
))
2554 static int64_t expr_prod(Monitor
*mon
)
2559 val
= expr_unary(mon
);
2562 if (op
!= '*' && op
!= '/' && op
!= '%')
2565 val2
= expr_unary(mon
);
2574 expr_error(mon
, "division by zero");
2585 static int64_t expr_logic(Monitor
*mon
)
2590 val
= expr_prod(mon
);
2593 if (op
!= '&' && op
!= '|' && op
!= '^')
2596 val2
= expr_prod(mon
);
2613 static int64_t expr_sum(Monitor
*mon
)
2618 val
= expr_logic(mon
);
2621 if (op
!= '+' && op
!= '-')
2624 val2
= expr_logic(mon
);
2633 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2636 if (setjmp(expr_env
)) {
2640 while (qemu_isspace(*pch
))
2642 *pval
= expr_sum(mon
);
2647 static int get_str(char *buf
, int buf_size
, const char **pp
)
2655 while (qemu_isspace(*p
))
2665 while (*p
!= '\0' && *p
!= '\"') {
2681 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2684 if ((q
- buf
) < buf_size
- 1) {
2688 if ((q
- buf
) < buf_size
- 1) {
2695 qemu_printf("unterminated string\n");
2700 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2701 if ((q
- buf
) < buf_size
- 1) {
2713 * Store the command-name in cmdname, and return a pointer to
2714 * the remaining of the command string.
2716 static const char *get_command_name(const char *cmdline
,
2717 char *cmdname
, size_t nlen
)
2720 const char *p
, *pstart
;
2723 while (qemu_isspace(*p
))
2728 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2733 memcpy(cmdname
, pstart
, len
);
2734 cmdname
[len
] = '\0';
2739 * Read key of 'type' into 'key' and return the current
2742 static char *key_get_info(const char *type
, char **key
)
2750 p
= strchr(type
, ':');
2757 str
= qemu_malloc(len
+ 1);
2758 memcpy(str
, type
, len
);
2765 static int default_fmt_format
= 'x';
2766 static int default_fmt_size
= 4;
2770 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2771 const char *cmdline
,
2774 const char *p
, *typestr
;
2776 const mon_cmd_t
*cmd
;
2782 monitor_printf(mon
, "command='%s'\n", cmdline
);
2785 /* extract the command name */
2786 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2790 /* find the command */
2791 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2792 if (compare_cmd(cmdname
, cmd
->name
))
2796 if (cmd
->name
== NULL
) {
2797 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2801 /* parse the parameters */
2802 typestr
= cmd
->args_type
;
2804 typestr
= key_get_info(typestr
, &key
);
2816 while (qemu_isspace(*p
))
2818 if (*typestr
== '?') {
2821 /* no optional string: NULL argument */
2825 ret
= get_str(buf
, sizeof(buf
), &p
);
2829 monitor_printf(mon
, "%s: filename expected\n",
2833 monitor_printf(mon
, "%s: block device name expected\n",
2837 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2842 qdict_put(qdict
, key
, qstring_from_str(buf
));
2847 int count
, format
, size
;
2849 while (qemu_isspace(*p
))
2855 if (qemu_isdigit(*p
)) {
2857 while (qemu_isdigit(*p
)) {
2858 count
= count
* 10 + (*p
- '0');
2896 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2897 monitor_printf(mon
, "invalid char in format: '%c'\n",
2902 format
= default_fmt_format
;
2903 if (format
!= 'i') {
2904 /* for 'i', not specifying a size gives -1 as size */
2906 size
= default_fmt_size
;
2907 default_fmt_size
= size
;
2909 default_fmt_format
= format
;
2912 format
= default_fmt_format
;
2913 if (format
!= 'i') {
2914 size
= default_fmt_size
;
2919 qdict_put(qdict
, "count", qint_from_int(count
));
2920 qdict_put(qdict
, "format", qint_from_int(format
));
2921 qdict_put(qdict
, "size", qint_from_int(size
));
2929 while (qemu_isspace(*p
))
2931 if (*typestr
== '?' || *typestr
== '.') {
2932 if (*typestr
== '?') {
2940 while (qemu_isspace(*p
))
2949 if (get_expr(mon
, &val
, &p
))
2951 /* Check if 'i' is greater than 32-bit */
2952 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2953 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2954 monitor_printf(mon
, "integer is for 32-bit values\n");
2957 qdict_put(qdict
, key
, qint_from_int(val
));
2968 while (qemu_isspace(*p
))
2974 monitor_printf(mon
, "%s: unsupported option -%c\n",
2981 qdict_put(qdict
, key
, qint_from_int(has_option
));
2986 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2992 /* check that all arguments were parsed */
2993 while (qemu_isspace(*p
))
2996 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3008 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3011 const mon_cmd_t
*cmd
;
3013 qdict
= qdict_new();
3015 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3017 void (*handler
)(Monitor
*mon
, const QDict
*qdict
);
3019 qemu_errors_to_mon(mon
);
3021 handler
= cmd
->handler
;
3022 handler(mon
, qdict
);
3024 qemu_errors_to_previous();
3030 static void cmd_completion(const char *name
, const char *list
)
3032 const char *p
, *pstart
;
3041 p
= pstart
+ strlen(pstart
);
3043 if (len
> sizeof(cmd
) - 2)
3044 len
= sizeof(cmd
) - 2;
3045 memcpy(cmd
, pstart
, len
);
3047 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3048 readline_add_completion(cur_mon
->rs
, cmd
);
3056 static void file_completion(const char *input
)
3061 char file
[1024], file_prefix
[1024];
3065 p
= strrchr(input
, '/');
3068 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3069 pstrcpy(path
, sizeof(path
), ".");
3071 input_path_len
= p
- input
+ 1;
3072 memcpy(path
, input
, input_path_len
);
3073 if (input_path_len
> sizeof(path
) - 1)
3074 input_path_len
= sizeof(path
) - 1;
3075 path
[input_path_len
] = '\0';
3076 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3078 #ifdef DEBUG_COMPLETION
3079 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3080 input
, path
, file_prefix
);
3082 ffs
= opendir(path
);
3090 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3091 memcpy(file
, input
, input_path_len
);
3092 if (input_path_len
< sizeof(file
))
3093 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3095 /* stat the file to find out if it's a directory.
3096 * In that case add a slash to speed up typing long paths
3099 if(S_ISDIR(sb
.st_mode
))
3100 pstrcat(file
, sizeof(file
), "/");
3101 readline_add_completion(cur_mon
->rs
, file
);
3107 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3109 const char *name
= bdrv_get_device_name(bs
);
3110 const char *input
= opaque
;
3112 if (input
[0] == '\0' ||
3113 !strncmp(name
, (char *)input
, strlen(input
))) {
3114 readline_add_completion(cur_mon
->rs
, name
);
3118 /* NOTE: this parser is an approximate form of the real command parser */
3119 static void parse_cmdline(const char *cmdline
,
3120 int *pnb_args
, char **args
)
3129 while (qemu_isspace(*p
))
3133 if (nb_args
>= MAX_ARGS
)
3135 ret
= get_str(buf
, sizeof(buf
), &p
);
3136 args
[nb_args
] = qemu_strdup(buf
);
3141 *pnb_args
= nb_args
;
3144 static const char *next_arg_type(const char *typestr
)
3146 const char *p
= strchr(typestr
, ':');
3147 return (p
!= NULL
? ++p
: typestr
);
3150 static void monitor_find_completion(const char *cmdline
)
3152 const char *cmdname
;
3153 char *args
[MAX_ARGS
];
3154 int nb_args
, i
, len
;
3155 const char *ptype
, *str
;
3156 const mon_cmd_t
*cmd
;
3159 parse_cmdline(cmdline
, &nb_args
, args
);
3160 #ifdef DEBUG_COMPLETION
3161 for(i
= 0; i
< nb_args
; i
++) {
3162 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3166 /* if the line ends with a space, it means we want to complete the
3168 len
= strlen(cmdline
);
3169 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3170 if (nb_args
>= MAX_ARGS
)
3172 args
[nb_args
++] = qemu_strdup("");
3175 /* command completion */
3180 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3181 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3182 cmd_completion(cmdname
, cmd
->name
);
3185 /* find the command */
3186 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3187 if (compare_cmd(args
[0], cmd
->name
))
3192 ptype
= next_arg_type(cmd
->args_type
);
3193 for(i
= 0; i
< nb_args
- 2; i
++) {
3194 if (*ptype
!= '\0') {
3195 ptype
= next_arg_type(ptype
);
3196 while (*ptype
== '?')
3197 ptype
= next_arg_type(ptype
);
3200 str
= args
[nb_args
- 1];
3201 if (*ptype
== '-' && ptype
[1] != '\0') {
3206 /* file completion */
3207 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3208 file_completion(str
);
3211 /* block device name completion */
3212 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3213 bdrv_iterate(block_completion_it
, (void *)str
);
3216 /* XXX: more generic ? */
3217 if (!strcmp(cmd
->name
, "info")) {
3218 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3219 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3220 cmd_completion(str
, cmd
->name
);
3222 } else if (!strcmp(cmd
->name
, "sendkey")) {
3223 char *sep
= strrchr(str
, '-');
3226 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3227 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3228 cmd_completion(str
, key
->name
);
3230 } else if (!strcmp(cmd
->name
, "help|?")) {
3231 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3232 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3233 cmd_completion(str
, cmd
->name
);
3241 for(i
= 0; i
< nb_args
; i
++)
3245 static int monitor_can_read(void *opaque
)
3247 Monitor
*mon
= opaque
;
3249 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3252 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3254 Monitor
*old_mon
= cur_mon
;
3260 for (i
= 0; i
< size
; i
++)
3261 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3263 if (size
== 0 || buf
[size
- 1] != 0)
3264 monitor_printf(cur_mon
, "corrupted command\n");
3266 monitor_handle_command(cur_mon
, (char *)buf
);
3272 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3274 monitor_suspend(mon
);
3275 monitor_handle_command(mon
, cmdline
);
3276 monitor_resume(mon
);
3279 int monitor_suspend(Monitor
*mon
)
3287 void monitor_resume(Monitor
*mon
)
3291 if (--mon
->suspend_cnt
== 0)
3292 readline_show_prompt(mon
->rs
);
3295 static void monitor_event(void *opaque
, int event
)
3297 Monitor
*mon
= opaque
;
3300 case CHR_EVENT_MUX_IN
:
3302 if (mon
->reset_seen
) {
3303 readline_restart(mon
->rs
);
3304 monitor_resume(mon
);
3307 mon
->suspend_cnt
= 0;
3311 case CHR_EVENT_MUX_OUT
:
3312 if (mon
->reset_seen
) {
3313 if (mon
->suspend_cnt
== 0) {
3314 monitor_printf(mon
, "\n");
3317 monitor_suspend(mon
);
3324 case CHR_EVENT_RESET
:
3325 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3326 "information\n", QEMU_VERSION
);
3327 if (!mon
->mux_out
) {
3328 readline_show_prompt(mon
->rs
);
3330 mon
->reset_seen
= 1;
3344 void monitor_init(CharDriverState
*chr
, int flags
)
3346 static int is_first_init
= 1;
3349 if (is_first_init
) {
3350 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3354 mon
= qemu_mallocz(sizeof(*mon
));
3358 if (flags
& MONITOR_USE_READLINE
) {
3359 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3360 monitor_read_command(mon
, 0);
3363 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3366 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3367 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3371 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3373 BlockDriverState
*bs
= opaque
;
3376 if (bdrv_set_key(bs
, password
) != 0) {
3377 monitor_printf(mon
, "invalid password\n");
3380 if (mon
->password_completion_cb
)
3381 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3383 monitor_read_command(mon
, 1);
3386 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3387 BlockDriverCompletionFunc
*completion_cb
,
3392 if (!bdrv_key_required(bs
)) {
3394 completion_cb(opaque
, 0);
3398 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3399 bdrv_get_encrypted_filename(bs
));
3401 mon
->password_completion_cb
= completion_cb
;
3402 mon
->password_opaque
= opaque
;
3404 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3406 if (err
&& completion_cb
)
3407 completion_cb(opaque
, err
);
3410 typedef struct QemuErrorSink QemuErrorSink
;
3411 struct QemuErrorSink
{
3420 QemuErrorSink
*previous
;
3423 static QemuErrorSink
*qemu_error_sink
;
3425 void qemu_errors_to_file(FILE *fp
)
3427 QemuErrorSink
*sink
;
3429 sink
= qemu_mallocz(sizeof(*sink
));
3430 sink
->dest
= ERR_SINK_FILE
;
3432 sink
->previous
= qemu_error_sink
;
3433 qemu_error_sink
= sink
;
3436 void qemu_errors_to_mon(Monitor
*mon
)
3438 QemuErrorSink
*sink
;
3440 sink
= qemu_mallocz(sizeof(*sink
));
3441 sink
->dest
= ERR_SINK_MONITOR
;
3443 sink
->previous
= qemu_error_sink
;
3444 qemu_error_sink
= sink
;
3447 void qemu_errors_to_previous(void)
3449 QemuErrorSink
*sink
;
3451 assert(qemu_error_sink
!= NULL
);
3452 sink
= qemu_error_sink
;
3453 qemu_error_sink
= sink
->previous
;
3457 void qemu_error(const char *fmt
, ...)
3461 assert(qemu_error_sink
!= NULL
);
3462 switch (qemu_error_sink
->dest
) {
3464 va_start(args
, fmt
);
3465 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3468 case ERR_SINK_MONITOR
:
3469 va_start(args
, fmt
);
3470 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);