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(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1232 val
= cpu_inb(NULL
, addr
);
1236 val
= cpu_inw(NULL
, addr
);
1240 val
= cpu_inl(NULL
, 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(NULL
, addr
, val
);
1262 cpu_outw(NULL
, addr
, val
);
1265 cpu_outl(NULL
, 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
1480 static void do_info_profile(Monitor
*mon
)
1486 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1487 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1488 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1489 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1494 static void do_info_profile(Monitor
*mon
)
1496 monitor_printf(mon
, "Internal profiler not compiled\n");
1500 /* Capture support */
1501 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1503 static void do_info_capture(Monitor
*mon
)
1508 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1509 monitor_printf(mon
, "[%d]: ", i
);
1510 s
->ops
.info (s
->opaque
);
1515 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1518 int n
= qdict_get_int(qdict
, "n");
1521 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1523 s
->ops
.destroy (s
->opaque
);
1524 QLIST_REMOVE (s
, entries
);
1531 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1533 const char *path
= qdict_get_str(qdict
, "path");
1534 int has_freq
= qdict_haskey(qdict
, "freq");
1535 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1536 int has_bits
= qdict_haskey(qdict
, "bits");
1537 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1538 int has_channels
= qdict_haskey(qdict
, "nchannels");
1539 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1542 s
= qemu_mallocz (sizeof (*s
));
1544 freq
= has_freq
? freq
: 44100;
1545 bits
= has_bits
? bits
: 16;
1546 nchannels
= has_channels
? nchannels
: 2;
1548 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1549 monitor_printf(mon
, "Faied to add wave capture\n");
1552 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1556 #if defined(TARGET_I386)
1557 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1560 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1562 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1563 if (env
->cpu_index
== cpu_index
) {
1565 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1567 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1573 static void do_info_status(Monitor
*mon
)
1577 monitor_printf(mon
, "VM status: running (single step mode)\n");
1579 monitor_printf(mon
, "VM status: running\n");
1582 monitor_printf(mon
, "VM status: paused\n");
1586 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1588 int value
= qdict_get_int(qdict
, "value");
1589 ram_addr_t target
= value
;
1590 qemu_balloon(target
<< 20);
1593 static void do_info_balloon(Monitor
*mon
)
1597 actual
= qemu_balloon_status();
1598 if (kvm_enabled() && !kvm_has_sync_mmu())
1599 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1600 "ballooning disabled\n");
1601 else if (actual
== 0)
1602 monitor_printf(mon
, "Ballooning not activated in VM\n");
1604 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1607 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1609 qemu_acl
*acl
= qemu_acl_find(name
);
1612 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1617 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1619 const char *aclname
= qdict_get_str(qdict
, "aclname");
1620 qemu_acl
*acl
= find_acl(mon
, aclname
);
1621 qemu_acl_entry
*entry
;
1625 monitor_printf(mon
, "policy: %s\n",
1626 acl
->defaultDeny
? "deny" : "allow");
1627 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1629 monitor_printf(mon
, "%d: %s %s\n", i
,
1630 entry
->deny
? "deny" : "allow", entry
->match
);
1635 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1637 const char *aclname
= qdict_get_str(qdict
, "aclname");
1638 qemu_acl
*acl
= find_acl(mon
, aclname
);
1641 qemu_acl_reset(acl
);
1642 monitor_printf(mon
, "acl: removed all rules\n");
1646 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1648 const char *aclname
= qdict_get_str(qdict
, "aclname");
1649 const char *policy
= qdict_get_str(qdict
, "policy");
1650 qemu_acl
*acl
= find_acl(mon
, aclname
);
1653 if (strcmp(policy
, "allow") == 0) {
1654 acl
->defaultDeny
= 0;
1655 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1656 } else if (strcmp(policy
, "deny") == 0) {
1657 acl
->defaultDeny
= 1;
1658 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1660 monitor_printf(mon
, "acl: unknown policy '%s', "
1661 "expected 'deny' or 'allow'\n", policy
);
1666 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1668 const char *aclname
= qdict_get_str(qdict
, "aclname");
1669 const char *match
= qdict_get_str(qdict
, "match");
1670 const char *policy
= qdict_get_str(qdict
, "policy");
1671 int has_index
= qdict_haskey(qdict
, "index");
1672 int index
= qdict_get_try_int(qdict
, "index", -1);
1673 qemu_acl
*acl
= find_acl(mon
, aclname
);
1677 if (strcmp(policy
, "allow") == 0) {
1679 } else if (strcmp(policy
, "deny") == 0) {
1682 monitor_printf(mon
, "acl: unknown policy '%s', "
1683 "expected 'deny' or 'allow'\n", policy
);
1687 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1689 ret
= qemu_acl_append(acl
, deny
, match
);
1691 monitor_printf(mon
, "acl: unable to add acl entry\n");
1693 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1697 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1699 const char *aclname
= qdict_get_str(qdict
, "aclname");
1700 const char *match
= qdict_get_str(qdict
, "match");
1701 qemu_acl
*acl
= find_acl(mon
, aclname
);
1705 ret
= qemu_acl_remove(acl
, match
);
1707 monitor_printf(mon
, "acl: no matching acl entry\n");
1709 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1713 #if defined(TARGET_I386)
1714 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1717 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1718 int bank
= qdict_get_int(qdict
, "bank");
1719 uint64_t status
= qdict_get_int(qdict
, "status");
1720 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1721 uint64_t addr
= qdict_get_int(qdict
, "addr");
1722 uint64_t misc
= qdict_get_int(qdict
, "misc");
1724 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1725 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1726 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1732 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1734 const char *fdname
= qdict_get_str(qdict
, "fdname");
1738 fd
= qemu_chr_get_msgfd(mon
->chr
);
1740 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1744 if (qemu_isdigit(fdname
[0])) {
1745 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1751 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1756 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1757 if (strcmp(monfd
->name
, fdname
) != 0) {
1766 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1767 monfd
->name
= qemu_strdup(fdname
);
1770 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1773 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1775 const char *fdname
= qdict_get_str(qdict
, "fdname");
1778 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1779 if (strcmp(monfd
->name
, fdname
) != 0) {
1783 QLIST_REMOVE(monfd
, next
);
1785 qemu_free(monfd
->name
);
1790 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1794 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1796 int saved_vm_running
= vm_running
;
1797 const char *name
= qdict_get_str(qdict
, "name");
1801 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1805 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1809 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1812 if (strcmp(monfd
->name
, fdname
) != 0) {
1818 /* caller takes ownership of fd */
1819 QLIST_REMOVE(monfd
, next
);
1820 qemu_free(monfd
->name
);
1829 static const mon_cmd_t mon_cmds
[] = {
1830 #include "qemu-monitor.h"
1834 /* Please update qemu-monitor.hx when adding or changing commands */
1835 static const mon_cmd_t info_cmds
[] = {
1836 { "version", "", do_info_version
,
1837 "", "show the version of QEMU" },
1838 { "network", "", do_info_network
,
1839 "", "show the network state" },
1840 { "chardev", "", qemu_chr_info
,
1841 "", "show the character devices" },
1842 { "block", "", bdrv_info
,
1843 "", "show the block devices" },
1844 { "blockstats", "", bdrv_info_stats
,
1845 "", "show block device statistics" },
1846 { "registers", "", do_info_registers
,
1847 "", "show the cpu registers" },
1848 { "cpus", "", do_info_cpus
,
1849 "", "show infos for each CPU" },
1850 { "history", "", do_info_history
,
1851 "", "show the command line history", },
1852 { "irq", "", irq_info
,
1853 "", "show the interrupts statistics (if available)", },
1854 { "pic", "", pic_info
,
1855 "", "show i8259 (PIC) state", },
1856 { "pci", "", pci_info
,
1857 "", "show PCI info", },
1858 #if defined(TARGET_I386) || defined(TARGET_SH4)
1859 { "tlb", "", tlb_info
,
1860 "", "show virtual to physical memory mappings", },
1862 #if defined(TARGET_I386)
1863 { "mem", "", mem_info
,
1864 "", "show the active virtual memory mappings", },
1865 { "hpet", "", do_info_hpet
,
1866 "", "show state of HPET", },
1868 { "jit", "", do_info_jit
,
1869 "", "show dynamic compiler info", },
1870 { "kvm", "", do_info_kvm
,
1871 "", "show KVM information", },
1872 { "numa", "", do_info_numa
,
1873 "", "show NUMA information", },
1874 { "usb", "", usb_info
,
1875 "", "show guest USB devices", },
1876 { "usbhost", "", usb_host_info
,
1877 "", "show host USB devices", },
1878 { "profile", "", do_info_profile
,
1879 "", "show profiling information", },
1880 { "capture", "", do_info_capture
,
1881 "", "show capture information" },
1882 { "snapshots", "", do_info_snapshots
,
1883 "", "show the currently saved VM snapshots" },
1884 { "status", "", do_info_status
,
1885 "", "show the current VM status (running|paused)" },
1886 { "pcmcia", "", pcmcia_info
,
1887 "", "show guest PCMCIA status" },
1888 { "mice", "", do_info_mice
,
1889 "", "show which guest mouse is receiving events" },
1890 { "vnc", "", do_info_vnc
,
1891 "", "show the vnc server status"},
1892 { "name", "", do_info_name
,
1893 "", "show the current VM name" },
1894 { "uuid", "", do_info_uuid
,
1895 "", "show the current VM UUID" },
1896 #if defined(TARGET_PPC)
1897 { "cpustats", "", do_info_cpu_stats
,
1898 "", "show CPU statistics", },
1900 #if defined(CONFIG_SLIRP)
1901 { "usernet", "", do_info_usernet
,
1902 "", "show user network stack connection states", },
1904 { "migrate", "", do_info_migrate
, "", "show migration status" },
1905 { "balloon", "", do_info_balloon
,
1906 "", "show balloon information" },
1907 { "qtree", "", do_info_qtree
,
1908 "", "show device tree" },
1909 { "qdm", "", do_info_qdm
,
1910 "", "show qdev device model list" },
1914 /*******************************************************************/
1916 static const char *pch
;
1917 static jmp_buf expr_env
;
1922 typedef struct MonitorDef
{
1925 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1929 #if defined(TARGET_I386)
1930 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1932 CPUState
*env
= mon_get_cpu();
1935 return env
->eip
+ env
->segs
[R_CS
].base
;
1939 #if defined(TARGET_PPC)
1940 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1942 CPUState
*env
= mon_get_cpu();
1950 for (i
= 0; i
< 8; i
++)
1951 u
|= env
->crf
[i
] << (32 - (4 * i
));
1956 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1958 CPUState
*env
= mon_get_cpu();
1964 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1966 CPUState
*env
= mon_get_cpu();
1972 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1974 CPUState
*env
= mon_get_cpu();
1977 return cpu_ppc_load_decr(env
);
1980 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1982 CPUState
*env
= mon_get_cpu();
1985 return cpu_ppc_load_tbu(env
);
1988 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1990 CPUState
*env
= mon_get_cpu();
1993 return cpu_ppc_load_tbl(env
);
1997 #if defined(TARGET_SPARC)
1998 #ifndef TARGET_SPARC64
1999 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2001 CPUState
*env
= mon_get_cpu();
2004 return GET_PSR(env
);
2008 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2010 CPUState
*env
= mon_get_cpu();
2013 return env
->regwptr
[val
];
2017 static const MonitorDef monitor_defs
[] = {
2020 #define SEG(name, seg) \
2021 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2022 { name ".base", offsetof(CPUState, segs[seg].base) },\
2023 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2025 { "eax", offsetof(CPUState
, regs
[0]) },
2026 { "ecx", offsetof(CPUState
, regs
[1]) },
2027 { "edx", offsetof(CPUState
, regs
[2]) },
2028 { "ebx", offsetof(CPUState
, regs
[3]) },
2029 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2030 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2031 { "esi", offsetof(CPUState
, regs
[6]) },
2032 { "edi", offsetof(CPUState
, regs
[7]) },
2033 #ifdef TARGET_X86_64
2034 { "r8", offsetof(CPUState
, regs
[8]) },
2035 { "r9", offsetof(CPUState
, regs
[9]) },
2036 { "r10", offsetof(CPUState
, regs
[10]) },
2037 { "r11", offsetof(CPUState
, regs
[11]) },
2038 { "r12", offsetof(CPUState
, regs
[12]) },
2039 { "r13", offsetof(CPUState
, regs
[13]) },
2040 { "r14", offsetof(CPUState
, regs
[14]) },
2041 { "r15", offsetof(CPUState
, regs
[15]) },
2043 { "eflags", offsetof(CPUState
, eflags
) },
2044 { "eip", offsetof(CPUState
, eip
) },
2051 { "pc", 0, monitor_get_pc
, },
2052 #elif defined(TARGET_PPC)
2053 /* General purpose registers */
2054 { "r0", offsetof(CPUState
, gpr
[0]) },
2055 { "r1", offsetof(CPUState
, gpr
[1]) },
2056 { "r2", offsetof(CPUState
, gpr
[2]) },
2057 { "r3", offsetof(CPUState
, gpr
[3]) },
2058 { "r4", offsetof(CPUState
, gpr
[4]) },
2059 { "r5", offsetof(CPUState
, gpr
[5]) },
2060 { "r6", offsetof(CPUState
, gpr
[6]) },
2061 { "r7", offsetof(CPUState
, gpr
[7]) },
2062 { "r8", offsetof(CPUState
, gpr
[8]) },
2063 { "r9", offsetof(CPUState
, gpr
[9]) },
2064 { "r10", offsetof(CPUState
, gpr
[10]) },
2065 { "r11", offsetof(CPUState
, gpr
[11]) },
2066 { "r12", offsetof(CPUState
, gpr
[12]) },
2067 { "r13", offsetof(CPUState
, gpr
[13]) },
2068 { "r14", offsetof(CPUState
, gpr
[14]) },
2069 { "r15", offsetof(CPUState
, gpr
[15]) },
2070 { "r16", offsetof(CPUState
, gpr
[16]) },
2071 { "r17", offsetof(CPUState
, gpr
[17]) },
2072 { "r18", offsetof(CPUState
, gpr
[18]) },
2073 { "r19", offsetof(CPUState
, gpr
[19]) },
2074 { "r20", offsetof(CPUState
, gpr
[20]) },
2075 { "r21", offsetof(CPUState
, gpr
[21]) },
2076 { "r22", offsetof(CPUState
, gpr
[22]) },
2077 { "r23", offsetof(CPUState
, gpr
[23]) },
2078 { "r24", offsetof(CPUState
, gpr
[24]) },
2079 { "r25", offsetof(CPUState
, gpr
[25]) },
2080 { "r26", offsetof(CPUState
, gpr
[26]) },
2081 { "r27", offsetof(CPUState
, gpr
[27]) },
2082 { "r28", offsetof(CPUState
, gpr
[28]) },
2083 { "r29", offsetof(CPUState
, gpr
[29]) },
2084 { "r30", offsetof(CPUState
, gpr
[30]) },
2085 { "r31", offsetof(CPUState
, gpr
[31]) },
2086 /* Floating point registers */
2087 { "f0", offsetof(CPUState
, fpr
[0]) },
2088 { "f1", offsetof(CPUState
, fpr
[1]) },
2089 { "f2", offsetof(CPUState
, fpr
[2]) },
2090 { "f3", offsetof(CPUState
, fpr
[3]) },
2091 { "f4", offsetof(CPUState
, fpr
[4]) },
2092 { "f5", offsetof(CPUState
, fpr
[5]) },
2093 { "f6", offsetof(CPUState
, fpr
[6]) },
2094 { "f7", offsetof(CPUState
, fpr
[7]) },
2095 { "f8", offsetof(CPUState
, fpr
[8]) },
2096 { "f9", offsetof(CPUState
, fpr
[9]) },
2097 { "f10", offsetof(CPUState
, fpr
[10]) },
2098 { "f11", offsetof(CPUState
, fpr
[11]) },
2099 { "f12", offsetof(CPUState
, fpr
[12]) },
2100 { "f13", offsetof(CPUState
, fpr
[13]) },
2101 { "f14", offsetof(CPUState
, fpr
[14]) },
2102 { "f15", offsetof(CPUState
, fpr
[15]) },
2103 { "f16", offsetof(CPUState
, fpr
[16]) },
2104 { "f17", offsetof(CPUState
, fpr
[17]) },
2105 { "f18", offsetof(CPUState
, fpr
[18]) },
2106 { "f19", offsetof(CPUState
, fpr
[19]) },
2107 { "f20", offsetof(CPUState
, fpr
[20]) },
2108 { "f21", offsetof(CPUState
, fpr
[21]) },
2109 { "f22", offsetof(CPUState
, fpr
[22]) },
2110 { "f23", offsetof(CPUState
, fpr
[23]) },
2111 { "f24", offsetof(CPUState
, fpr
[24]) },
2112 { "f25", offsetof(CPUState
, fpr
[25]) },
2113 { "f26", offsetof(CPUState
, fpr
[26]) },
2114 { "f27", offsetof(CPUState
, fpr
[27]) },
2115 { "f28", offsetof(CPUState
, fpr
[28]) },
2116 { "f29", offsetof(CPUState
, fpr
[29]) },
2117 { "f30", offsetof(CPUState
, fpr
[30]) },
2118 { "f31", offsetof(CPUState
, fpr
[31]) },
2119 { "fpscr", offsetof(CPUState
, fpscr
) },
2120 /* Next instruction pointer */
2121 { "nip|pc", offsetof(CPUState
, nip
) },
2122 { "lr", offsetof(CPUState
, lr
) },
2123 { "ctr", offsetof(CPUState
, ctr
) },
2124 { "decr", 0, &monitor_get_decr
, },
2125 { "ccr", 0, &monitor_get_ccr
, },
2126 /* Machine state register */
2127 { "msr", 0, &monitor_get_msr
, },
2128 { "xer", 0, &monitor_get_xer
, },
2129 { "tbu", 0, &monitor_get_tbu
, },
2130 { "tbl", 0, &monitor_get_tbl
, },
2131 #if defined(TARGET_PPC64)
2132 /* Address space register */
2133 { "asr", offsetof(CPUState
, asr
) },
2135 /* Segment registers */
2136 { "sdr1", offsetof(CPUState
, sdr1
) },
2137 { "sr0", offsetof(CPUState
, sr
[0]) },
2138 { "sr1", offsetof(CPUState
, sr
[1]) },
2139 { "sr2", offsetof(CPUState
, sr
[2]) },
2140 { "sr3", offsetof(CPUState
, sr
[3]) },
2141 { "sr4", offsetof(CPUState
, sr
[4]) },
2142 { "sr5", offsetof(CPUState
, sr
[5]) },
2143 { "sr6", offsetof(CPUState
, sr
[6]) },
2144 { "sr7", offsetof(CPUState
, sr
[7]) },
2145 { "sr8", offsetof(CPUState
, sr
[8]) },
2146 { "sr9", offsetof(CPUState
, sr
[9]) },
2147 { "sr10", offsetof(CPUState
, sr
[10]) },
2148 { "sr11", offsetof(CPUState
, sr
[11]) },
2149 { "sr12", offsetof(CPUState
, sr
[12]) },
2150 { "sr13", offsetof(CPUState
, sr
[13]) },
2151 { "sr14", offsetof(CPUState
, sr
[14]) },
2152 { "sr15", offsetof(CPUState
, sr
[15]) },
2153 /* Too lazy to put BATs and SPRs ... */
2154 #elif defined(TARGET_SPARC)
2155 { "g0", offsetof(CPUState
, gregs
[0]) },
2156 { "g1", offsetof(CPUState
, gregs
[1]) },
2157 { "g2", offsetof(CPUState
, gregs
[2]) },
2158 { "g3", offsetof(CPUState
, gregs
[3]) },
2159 { "g4", offsetof(CPUState
, gregs
[4]) },
2160 { "g5", offsetof(CPUState
, gregs
[5]) },
2161 { "g6", offsetof(CPUState
, gregs
[6]) },
2162 { "g7", offsetof(CPUState
, gregs
[7]) },
2163 { "o0", 0, monitor_get_reg
},
2164 { "o1", 1, monitor_get_reg
},
2165 { "o2", 2, monitor_get_reg
},
2166 { "o3", 3, monitor_get_reg
},
2167 { "o4", 4, monitor_get_reg
},
2168 { "o5", 5, monitor_get_reg
},
2169 { "o6", 6, monitor_get_reg
},
2170 { "o7", 7, monitor_get_reg
},
2171 { "l0", 8, monitor_get_reg
},
2172 { "l1", 9, monitor_get_reg
},
2173 { "l2", 10, monitor_get_reg
},
2174 { "l3", 11, monitor_get_reg
},
2175 { "l4", 12, monitor_get_reg
},
2176 { "l5", 13, monitor_get_reg
},
2177 { "l6", 14, monitor_get_reg
},
2178 { "l7", 15, monitor_get_reg
},
2179 { "i0", 16, monitor_get_reg
},
2180 { "i1", 17, monitor_get_reg
},
2181 { "i2", 18, monitor_get_reg
},
2182 { "i3", 19, monitor_get_reg
},
2183 { "i4", 20, monitor_get_reg
},
2184 { "i5", 21, monitor_get_reg
},
2185 { "i6", 22, monitor_get_reg
},
2186 { "i7", 23, monitor_get_reg
},
2187 { "pc", offsetof(CPUState
, pc
) },
2188 { "npc", offsetof(CPUState
, npc
) },
2189 { "y", offsetof(CPUState
, y
) },
2190 #ifndef TARGET_SPARC64
2191 { "psr", 0, &monitor_get_psr
, },
2192 { "wim", offsetof(CPUState
, wim
) },
2194 { "tbr", offsetof(CPUState
, tbr
) },
2195 { "fsr", offsetof(CPUState
, fsr
) },
2196 { "f0", offsetof(CPUState
, fpr
[0]) },
2197 { "f1", offsetof(CPUState
, fpr
[1]) },
2198 { "f2", offsetof(CPUState
, fpr
[2]) },
2199 { "f3", offsetof(CPUState
, fpr
[3]) },
2200 { "f4", offsetof(CPUState
, fpr
[4]) },
2201 { "f5", offsetof(CPUState
, fpr
[5]) },
2202 { "f6", offsetof(CPUState
, fpr
[6]) },
2203 { "f7", offsetof(CPUState
, fpr
[7]) },
2204 { "f8", offsetof(CPUState
, fpr
[8]) },
2205 { "f9", offsetof(CPUState
, fpr
[9]) },
2206 { "f10", offsetof(CPUState
, fpr
[10]) },
2207 { "f11", offsetof(CPUState
, fpr
[11]) },
2208 { "f12", offsetof(CPUState
, fpr
[12]) },
2209 { "f13", offsetof(CPUState
, fpr
[13]) },
2210 { "f14", offsetof(CPUState
, fpr
[14]) },
2211 { "f15", offsetof(CPUState
, fpr
[15]) },
2212 { "f16", offsetof(CPUState
, fpr
[16]) },
2213 { "f17", offsetof(CPUState
, fpr
[17]) },
2214 { "f18", offsetof(CPUState
, fpr
[18]) },
2215 { "f19", offsetof(CPUState
, fpr
[19]) },
2216 { "f20", offsetof(CPUState
, fpr
[20]) },
2217 { "f21", offsetof(CPUState
, fpr
[21]) },
2218 { "f22", offsetof(CPUState
, fpr
[22]) },
2219 { "f23", offsetof(CPUState
, fpr
[23]) },
2220 { "f24", offsetof(CPUState
, fpr
[24]) },
2221 { "f25", offsetof(CPUState
, fpr
[25]) },
2222 { "f26", offsetof(CPUState
, fpr
[26]) },
2223 { "f27", offsetof(CPUState
, fpr
[27]) },
2224 { "f28", offsetof(CPUState
, fpr
[28]) },
2225 { "f29", offsetof(CPUState
, fpr
[29]) },
2226 { "f30", offsetof(CPUState
, fpr
[30]) },
2227 { "f31", offsetof(CPUState
, fpr
[31]) },
2228 #ifdef TARGET_SPARC64
2229 { "f32", offsetof(CPUState
, fpr
[32]) },
2230 { "f34", offsetof(CPUState
, fpr
[34]) },
2231 { "f36", offsetof(CPUState
, fpr
[36]) },
2232 { "f38", offsetof(CPUState
, fpr
[38]) },
2233 { "f40", offsetof(CPUState
, fpr
[40]) },
2234 { "f42", offsetof(CPUState
, fpr
[42]) },
2235 { "f44", offsetof(CPUState
, fpr
[44]) },
2236 { "f46", offsetof(CPUState
, fpr
[46]) },
2237 { "f48", offsetof(CPUState
, fpr
[48]) },
2238 { "f50", offsetof(CPUState
, fpr
[50]) },
2239 { "f52", offsetof(CPUState
, fpr
[52]) },
2240 { "f54", offsetof(CPUState
, fpr
[54]) },
2241 { "f56", offsetof(CPUState
, fpr
[56]) },
2242 { "f58", offsetof(CPUState
, fpr
[58]) },
2243 { "f60", offsetof(CPUState
, fpr
[60]) },
2244 { "f62", offsetof(CPUState
, fpr
[62]) },
2245 { "asi", offsetof(CPUState
, asi
) },
2246 { "pstate", offsetof(CPUState
, pstate
) },
2247 { "cansave", offsetof(CPUState
, cansave
) },
2248 { "canrestore", offsetof(CPUState
, canrestore
) },
2249 { "otherwin", offsetof(CPUState
, otherwin
) },
2250 { "wstate", offsetof(CPUState
, wstate
) },
2251 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2252 { "fprs", offsetof(CPUState
, fprs
) },
2258 static void expr_error(Monitor
*mon
, const char *msg
)
2260 monitor_printf(mon
, "%s\n", msg
);
2261 longjmp(expr_env
, 1);
2264 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2265 static int get_monitor_def(target_long
*pval
, const char *name
)
2267 const MonitorDef
*md
;
2270 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2271 if (compare_cmd(name
, md
->name
)) {
2272 if (md
->get_value
) {
2273 *pval
= md
->get_value(md
, md
->offset
);
2275 CPUState
*env
= mon_get_cpu();
2278 ptr
= (uint8_t *)env
+ md
->offset
;
2281 *pval
= *(int32_t *)ptr
;
2284 *pval
= *(target_long
*)ptr
;
2297 static void next(void)
2301 while (qemu_isspace(*pch
))
2306 static int64_t expr_sum(Monitor
*mon
);
2308 static int64_t expr_unary(Monitor
*mon
)
2317 n
= expr_unary(mon
);
2321 n
= -expr_unary(mon
);
2325 n
= ~expr_unary(mon
);
2331 expr_error(mon
, "')' expected");
2338 expr_error(mon
, "character constant expected");
2342 expr_error(mon
, "missing terminating \' character");
2352 while ((*pch
>= 'a' && *pch
<= 'z') ||
2353 (*pch
>= 'A' && *pch
<= 'Z') ||
2354 (*pch
>= '0' && *pch
<= '9') ||
2355 *pch
== '_' || *pch
== '.') {
2356 if ((q
- buf
) < sizeof(buf
) - 1)
2360 while (qemu_isspace(*pch
))
2363 ret
= get_monitor_def(®
, buf
);
2365 expr_error(mon
, "unknown register");
2367 expr_error(mon
, "no cpu defined");
2372 expr_error(mon
, "unexpected end of expression");
2376 #if TARGET_PHYS_ADDR_BITS > 32
2377 n
= strtoull(pch
, &p
, 0);
2379 n
= strtoul(pch
, &p
, 0);
2382 expr_error(mon
, "invalid char in expression");
2385 while (qemu_isspace(*pch
))
2393 static int64_t expr_prod(Monitor
*mon
)
2398 val
= expr_unary(mon
);
2401 if (op
!= '*' && op
!= '/' && op
!= '%')
2404 val2
= expr_unary(mon
);
2413 expr_error(mon
, "division by zero");
2424 static int64_t expr_logic(Monitor
*mon
)
2429 val
= expr_prod(mon
);
2432 if (op
!= '&' && op
!= '|' && op
!= '^')
2435 val2
= expr_prod(mon
);
2452 static int64_t expr_sum(Monitor
*mon
)
2457 val
= expr_logic(mon
);
2460 if (op
!= '+' && op
!= '-')
2463 val2
= expr_logic(mon
);
2472 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2475 if (setjmp(expr_env
)) {
2479 while (qemu_isspace(*pch
))
2481 *pval
= expr_sum(mon
);
2486 static int get_str(char *buf
, int buf_size
, const char **pp
)
2494 while (qemu_isspace(*p
))
2504 while (*p
!= '\0' && *p
!= '\"') {
2520 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2523 if ((q
- buf
) < buf_size
- 1) {
2527 if ((q
- buf
) < buf_size
- 1) {
2534 qemu_printf("unterminated string\n");
2539 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2540 if ((q
- buf
) < buf_size
- 1) {
2552 * Store the command-name in cmdname, and return a pointer to
2553 * the remaining of the command string.
2555 static const char *get_command_name(const char *cmdline
,
2556 char *cmdname
, size_t nlen
)
2559 const char *p
, *pstart
;
2562 while (qemu_isspace(*p
))
2567 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2572 memcpy(cmdname
, pstart
, len
);
2573 cmdname
[len
] = '\0';
2578 * Read key of 'type' into 'key' and return the current
2581 static char *key_get_info(const char *type
, char **key
)
2589 p
= strchr(type
, ':');
2596 str
= qemu_malloc(len
+ 1);
2597 memcpy(str
, type
, len
);
2604 static int default_fmt_format
= 'x';
2605 static int default_fmt_size
= 4;
2609 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2610 const char *cmdline
,
2613 const char *p
, *typestr
;
2615 const mon_cmd_t
*cmd
;
2621 monitor_printf(mon
, "command='%s'\n", cmdline
);
2624 /* extract the command name */
2625 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2629 /* find the command */
2630 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2631 if (compare_cmd(cmdname
, cmd
->name
))
2635 if (cmd
->name
== NULL
) {
2636 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2640 /* parse the parameters */
2641 typestr
= cmd
->args_type
;
2643 typestr
= key_get_info(typestr
, &key
);
2655 while (qemu_isspace(*p
))
2657 if (*typestr
== '?') {
2660 /* no optional string: NULL argument */
2664 ret
= get_str(buf
, sizeof(buf
), &p
);
2668 monitor_printf(mon
, "%s: filename expected\n",
2672 monitor_printf(mon
, "%s: block device name expected\n",
2676 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2681 qdict_put(qdict
, key
, qstring_from_str(buf
));
2686 int count
, format
, size
;
2688 while (qemu_isspace(*p
))
2694 if (qemu_isdigit(*p
)) {
2696 while (qemu_isdigit(*p
)) {
2697 count
= count
* 10 + (*p
- '0');
2735 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2736 monitor_printf(mon
, "invalid char in format: '%c'\n",
2741 format
= default_fmt_format
;
2742 if (format
!= 'i') {
2743 /* for 'i', not specifying a size gives -1 as size */
2745 size
= default_fmt_size
;
2746 default_fmt_size
= size
;
2748 default_fmt_format
= format
;
2751 format
= default_fmt_format
;
2752 if (format
!= 'i') {
2753 size
= default_fmt_size
;
2758 qdict_put(qdict
, "count", qint_from_int(count
));
2759 qdict_put(qdict
, "format", qint_from_int(format
));
2760 qdict_put(qdict
, "size", qint_from_int(size
));
2768 while (qemu_isspace(*p
))
2770 if (*typestr
== '?' || *typestr
== '.') {
2771 if (*typestr
== '?') {
2779 while (qemu_isspace(*p
))
2788 if (get_expr(mon
, &val
, &p
))
2790 /* Check if 'i' is greater than 32-bit */
2791 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2792 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2793 monitor_printf(mon
, "integer is for 32-bit values\n");
2796 qdict_put(qdict
, key
, qint_from_int(val
));
2807 while (qemu_isspace(*p
))
2813 monitor_printf(mon
, "%s: unsupported option -%c\n",
2820 qdict_put(qdict
, key
, qint_from_int(has_option
));
2825 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2831 /* check that all arguments were parsed */
2832 while (qemu_isspace(*p
))
2835 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2847 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2850 const mon_cmd_t
*cmd
;
2852 qdict
= qdict_new();
2854 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
2856 void (*handler
)(Monitor
*mon
, const QDict
*qdict
);
2858 qemu_errors_to_mon(mon
);
2860 handler
= cmd
->handler
;
2861 handler(mon
, qdict
);
2863 qemu_errors_to_previous();
2869 static void cmd_completion(const char *name
, const char *list
)
2871 const char *p
, *pstart
;
2880 p
= pstart
+ strlen(pstart
);
2882 if (len
> sizeof(cmd
) - 2)
2883 len
= sizeof(cmd
) - 2;
2884 memcpy(cmd
, pstart
, len
);
2886 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2887 readline_add_completion(cur_mon
->rs
, cmd
);
2895 static void file_completion(const char *input
)
2900 char file
[1024], file_prefix
[1024];
2904 p
= strrchr(input
, '/');
2907 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2908 pstrcpy(path
, sizeof(path
), ".");
2910 input_path_len
= p
- input
+ 1;
2911 memcpy(path
, input
, input_path_len
);
2912 if (input_path_len
> sizeof(path
) - 1)
2913 input_path_len
= sizeof(path
) - 1;
2914 path
[input_path_len
] = '\0';
2915 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2917 #ifdef DEBUG_COMPLETION
2918 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2919 input
, path
, file_prefix
);
2921 ffs
= opendir(path
);
2929 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2930 memcpy(file
, input
, input_path_len
);
2931 if (input_path_len
< sizeof(file
))
2932 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2934 /* stat the file to find out if it's a directory.
2935 * In that case add a slash to speed up typing long paths
2938 if(S_ISDIR(sb
.st_mode
))
2939 pstrcat(file
, sizeof(file
), "/");
2940 readline_add_completion(cur_mon
->rs
, file
);
2946 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2948 const char *name
= bdrv_get_device_name(bs
);
2949 const char *input
= opaque
;
2951 if (input
[0] == '\0' ||
2952 !strncmp(name
, (char *)input
, strlen(input
))) {
2953 readline_add_completion(cur_mon
->rs
, name
);
2957 /* NOTE: this parser is an approximate form of the real command parser */
2958 static void parse_cmdline(const char *cmdline
,
2959 int *pnb_args
, char **args
)
2968 while (qemu_isspace(*p
))
2972 if (nb_args
>= MAX_ARGS
)
2974 ret
= get_str(buf
, sizeof(buf
), &p
);
2975 args
[nb_args
] = qemu_strdup(buf
);
2980 *pnb_args
= nb_args
;
2983 static const char *next_arg_type(const char *typestr
)
2985 const char *p
= strchr(typestr
, ':');
2986 return (p
!= NULL
? ++p
: typestr
);
2989 static void monitor_find_completion(const char *cmdline
)
2991 const char *cmdname
;
2992 char *args
[MAX_ARGS
];
2993 int nb_args
, i
, len
;
2994 const char *ptype
, *str
;
2995 const mon_cmd_t
*cmd
;
2998 parse_cmdline(cmdline
, &nb_args
, args
);
2999 #ifdef DEBUG_COMPLETION
3000 for(i
= 0; i
< nb_args
; i
++) {
3001 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3005 /* if the line ends with a space, it means we want to complete the
3007 len
= strlen(cmdline
);
3008 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3009 if (nb_args
>= MAX_ARGS
)
3011 args
[nb_args
++] = qemu_strdup("");
3014 /* command completion */
3019 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3020 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3021 cmd_completion(cmdname
, cmd
->name
);
3024 /* find the command */
3025 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3026 if (compare_cmd(args
[0], cmd
->name
))
3031 ptype
= next_arg_type(cmd
->args_type
);
3032 for(i
= 0; i
< nb_args
- 2; i
++) {
3033 if (*ptype
!= '\0') {
3034 ptype
= next_arg_type(ptype
);
3035 while (*ptype
== '?')
3036 ptype
= next_arg_type(ptype
);
3039 str
= args
[nb_args
- 1];
3040 if (*ptype
== '-' && ptype
[1] != '\0') {
3045 /* file completion */
3046 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3047 file_completion(str
);
3050 /* block device name completion */
3051 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3052 bdrv_iterate(block_completion_it
, (void *)str
);
3055 /* XXX: more generic ? */
3056 if (!strcmp(cmd
->name
, "info")) {
3057 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3058 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3059 cmd_completion(str
, cmd
->name
);
3061 } else if (!strcmp(cmd
->name
, "sendkey")) {
3062 char *sep
= strrchr(str
, '-');
3065 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3066 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3067 cmd_completion(str
, key
->name
);
3069 } else if (!strcmp(cmd
->name
, "help|?")) {
3070 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3071 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3072 cmd_completion(str
, cmd
->name
);
3080 for(i
= 0; i
< nb_args
; i
++)
3084 static int monitor_can_read(void *opaque
)
3086 Monitor
*mon
= opaque
;
3088 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3091 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3093 Monitor
*old_mon
= cur_mon
;
3099 for (i
= 0; i
< size
; i
++)
3100 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3102 if (size
== 0 || buf
[size
- 1] != 0)
3103 monitor_printf(cur_mon
, "corrupted command\n");
3105 monitor_handle_command(cur_mon
, (char *)buf
);
3111 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3113 monitor_suspend(mon
);
3114 monitor_handle_command(mon
, cmdline
);
3115 monitor_resume(mon
);
3118 int monitor_suspend(Monitor
*mon
)
3126 void monitor_resume(Monitor
*mon
)
3130 if (--mon
->suspend_cnt
== 0)
3131 readline_show_prompt(mon
->rs
);
3134 static void monitor_event(void *opaque
, int event
)
3136 Monitor
*mon
= opaque
;
3139 case CHR_EVENT_MUX_IN
:
3141 if (mon
->reset_seen
) {
3142 readline_restart(mon
->rs
);
3143 monitor_resume(mon
);
3146 mon
->suspend_cnt
= 0;
3150 case CHR_EVENT_MUX_OUT
:
3151 if (mon
->reset_seen
) {
3152 if (mon
->suspend_cnt
== 0) {
3153 monitor_printf(mon
, "\n");
3156 monitor_suspend(mon
);
3163 case CHR_EVENT_RESET
:
3164 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3165 "information\n", QEMU_VERSION
);
3166 if (!mon
->mux_out
) {
3167 readline_show_prompt(mon
->rs
);
3169 mon
->reset_seen
= 1;
3183 void monitor_init(CharDriverState
*chr
, int flags
)
3185 static int is_first_init
= 1;
3188 if (is_first_init
) {
3189 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3193 mon
= qemu_mallocz(sizeof(*mon
));
3197 if (flags
& MONITOR_USE_READLINE
) {
3198 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3199 monitor_read_command(mon
, 0);
3202 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3205 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3206 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3210 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3212 BlockDriverState
*bs
= opaque
;
3215 if (bdrv_set_key(bs
, password
) != 0) {
3216 monitor_printf(mon
, "invalid password\n");
3219 if (mon
->password_completion_cb
)
3220 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3222 monitor_read_command(mon
, 1);
3225 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3226 BlockDriverCompletionFunc
*completion_cb
,
3231 if (!bdrv_key_required(bs
)) {
3233 completion_cb(opaque
, 0);
3237 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3238 bdrv_get_encrypted_filename(bs
));
3240 mon
->password_completion_cb
= completion_cb
;
3241 mon
->password_opaque
= opaque
;
3243 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3245 if (err
&& completion_cb
)
3246 completion_cb(opaque
, err
);
3249 typedef struct QemuErrorSink QemuErrorSink
;
3250 struct QemuErrorSink
{
3259 QemuErrorSink
*previous
;
3262 static QemuErrorSink
*qemu_error_sink
;
3264 void qemu_errors_to_file(FILE *fp
)
3266 QemuErrorSink
*sink
;
3268 sink
= qemu_mallocz(sizeof(*sink
));
3269 sink
->dest
= ERR_SINK_FILE
;
3271 sink
->previous
= qemu_error_sink
;
3272 qemu_error_sink
= sink
;
3275 void qemu_errors_to_mon(Monitor
*mon
)
3277 QemuErrorSink
*sink
;
3279 sink
= qemu_mallocz(sizeof(*sink
));
3280 sink
->dest
= ERR_SINK_MONITOR
;
3282 sink
->previous
= qemu_error_sink
;
3283 qemu_error_sink
= sink
;
3286 void qemu_errors_to_previous(void)
3288 QemuErrorSink
*sink
;
3290 assert(qemu_error_sink
!= NULL
);
3291 sink
= qemu_error_sink
;
3292 qemu_error_sink
= sink
->previous
;
3296 void qemu_error(const char *fmt
, ...)
3300 assert(qemu_error_sink
!= NULL
);
3301 switch (qemu_error_sink
->dest
) {
3303 va_start(args
, fmt
);
3304 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3307 case ERR_SINK_MONITOR
:
3308 va_start(args
, fmt
);
3309 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);