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
;
76 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
78 void (*info
)(Monitor
*mon
);
79 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
80 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
81 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
85 /* file descriptors passed via SCM_RIGHTS */
86 typedef struct mon_fd_t mon_fd_t
;
90 QLIST_ENTRY(mon_fd_t
) next
;
103 BlockDriverCompletionFunc
*password_completion_cb
;
104 void *password_opaque
;
105 QLIST_HEAD(,mon_fd_t
) fds
;
106 QLIST_ENTRY(Monitor
) entry
;
109 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
111 static const mon_cmd_t mon_cmds
[];
112 static const mon_cmd_t info_cmds
[];
114 Monitor
*cur_mon
= NULL
;
116 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
119 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
121 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
123 readline_show_prompt(mon
->rs
);
126 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
130 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
131 /* prompt is printed on return from the command handler */
134 monitor_printf(mon
, "terminal does not support password prompting\n");
139 void monitor_flush(Monitor
*mon
)
141 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
142 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
143 mon
->outbuf_index
= 0;
147 /* flush at every end of line or if the buffer is full */
148 static void monitor_puts(Monitor
*mon
, const char *str
)
160 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
161 mon
->outbuf
[mon
->outbuf_index
++] = c
;
162 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
168 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
171 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
172 monitor_puts(mon
, buf
);
175 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
179 monitor_vprintf(mon
, fmt
, ap
);
183 void monitor_print_filename(Monitor
*mon
, const char *filename
)
187 for (i
= 0; filename
[i
]; i
++) {
188 switch (filename
[i
]) {
192 monitor_printf(mon
, "\\%c", filename
[i
]);
195 monitor_printf(mon
, "\\t");
198 monitor_printf(mon
, "\\r");
201 monitor_printf(mon
, "\\n");
204 monitor_printf(mon
, "%c", filename
[i
]);
210 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
214 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
219 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
221 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
223 return cmd
->user_print
!= NULL
;
226 static int compare_cmd(const char *name
, const char *list
)
228 const char *p
, *pstart
;
236 p
= pstart
+ strlen(pstart
);
237 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
246 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
247 const char *prefix
, const char *name
)
249 const mon_cmd_t
*cmd
;
251 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
252 if (!name
|| !strcmp(name
, cmd
->name
))
253 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
254 cmd
->params
, cmd
->help
);
258 static void help_cmd(Monitor
*mon
, const char *name
)
260 if (name
&& !strcmp(name
, "info")) {
261 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
263 help_cmd_dump(mon
, mon_cmds
, "", name
);
264 if (name
&& !strcmp(name
, "log")) {
265 const CPULogItem
*item
;
266 monitor_printf(mon
, "Log items (comma separated):\n");
267 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
268 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
269 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
275 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
277 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
280 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
284 const char *device
= qdict_get_str(qdict
, "device");
286 all_devices
= !strcmp(device
, "all");
287 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
289 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
291 bdrv_commit(dinfo
->bdrv
);
295 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
297 const mon_cmd_t
*cmd
;
298 const char *item
= qdict_get_try_str(qdict
, "item");
303 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
304 if (compare_cmd(item
, cmd
->name
))
308 if (cmd
->name
== NULL
)
311 if (monitor_handler_ported(cmd
)) {
312 cmd
->mhandler
.info_new(mon
, ret_data
);
314 cmd
->user_print(mon
, *ret_data
);
316 cmd
->mhandler
.info(mon
);
322 help_cmd(mon
, "info");
325 static void do_info_version(Monitor
*mon
)
327 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
330 static void do_info_name(Monitor
*mon
)
333 monitor_printf(mon
, "%s\n", qemu_name
);
336 #if defined(TARGET_I386)
337 static void do_info_hpet(Monitor
*mon
)
339 monitor_printf(mon
, "HPET is %s by QEMU\n",
340 (no_hpet
) ? "disabled" : "enabled");
344 static void do_info_uuid(Monitor
*mon
)
346 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
347 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
348 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
349 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
350 qemu_uuid
[14], qemu_uuid
[15]);
353 /* get the current CPU defined by the user */
354 static int mon_set_cpu(int cpu_index
)
358 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
359 if (env
->cpu_index
== cpu_index
) {
360 cur_mon
->mon_cpu
= env
;
367 static CPUState
*mon_get_cpu(void)
369 if (!cur_mon
->mon_cpu
) {
372 cpu_synchronize_state(cur_mon
->mon_cpu
);
373 return cur_mon
->mon_cpu
;
376 static void do_info_registers(Monitor
*mon
)
383 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
386 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
391 static void do_info_cpus(Monitor
*mon
)
395 /* just to set the default cpu if not already done */
398 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
399 cpu_synchronize_state(env
);
400 monitor_printf(mon
, "%c CPU #%d:",
401 (env
== mon
->mon_cpu
) ? '*' : ' ',
403 #if defined(TARGET_I386)
404 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
405 env
->eip
+ env
->segs
[R_CS
].base
);
406 #elif defined(TARGET_PPC)
407 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
408 #elif defined(TARGET_SPARC)
409 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
411 #elif defined(TARGET_MIPS)
412 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
415 monitor_printf(mon
, " (halted)");
416 monitor_printf(mon
, "\n");
420 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
422 int index
= qdict_get_int(qdict
, "index");
423 if (mon_set_cpu(index
) < 0)
424 monitor_printf(mon
, "Invalid CPU index\n");
427 static void do_info_jit(Monitor
*mon
)
429 dump_exec_info((FILE *)mon
, monitor_fprintf
);
432 static void do_info_history(Monitor
*mon
)
441 str
= readline_get_history(mon
->rs
, i
);
444 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
449 #if defined(TARGET_PPC)
450 /* XXX: not implemented in other targets */
451 static void do_info_cpu_stats(Monitor
*mon
)
456 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
461 * do_quit(): Quit QEMU execution
463 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
468 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
470 if (bdrv_is_inserted(bs
)) {
472 if (!bdrv_is_removable(bs
)) {
473 monitor_printf(mon
, "device is not removable\n");
476 if (bdrv_is_locked(bs
)) {
477 monitor_printf(mon
, "device is locked\n");
486 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
488 BlockDriverState
*bs
;
489 int force
= qdict_get_int(qdict
, "force");
490 const char *filename
= qdict_get_str(qdict
, "filename");
492 bs
= bdrv_find(filename
);
494 monitor_printf(mon
, "device not found\n");
497 eject_device(mon
, bs
, force
);
500 static void do_change_block(Monitor
*mon
, const char *device
,
501 const char *filename
, const char *fmt
)
503 BlockDriverState
*bs
;
504 BlockDriver
*drv
= NULL
;
506 bs
= bdrv_find(device
);
508 monitor_printf(mon
, "device not found\n");
512 drv
= bdrv_find_format(fmt
);
514 monitor_printf(mon
, "invalid format %s\n", fmt
);
518 if (eject_device(mon
, bs
, 0) < 0)
520 bdrv_open2(bs
, filename
, 0, drv
);
521 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
524 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
527 if (vnc_display_password(NULL
, password
) < 0)
528 monitor_printf(mon
, "could not set VNC server password\n");
530 monitor_read_command(mon
, 1);
533 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
535 if (strcmp(target
, "passwd") == 0 ||
536 strcmp(target
, "password") == 0) {
539 strncpy(password
, arg
, sizeof(password
));
540 password
[sizeof(password
) - 1] = '\0';
541 change_vnc_password_cb(mon
, password
, NULL
);
543 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
546 if (vnc_display_open(NULL
, target
) < 0)
547 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
551 static void do_change(Monitor
*mon
, const QDict
*qdict
)
553 const char *device
= qdict_get_str(qdict
, "device");
554 const char *target
= qdict_get_str(qdict
, "target");
555 const char *arg
= qdict_get_try_str(qdict
, "arg");
556 if (strcmp(device
, "vnc") == 0) {
557 do_change_vnc(mon
, target
, arg
);
559 do_change_block(mon
, device
, target
, arg
);
563 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
565 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
568 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
570 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
573 static void do_log(Monitor
*mon
, const QDict
*qdict
)
576 const char *items
= qdict_get_str(qdict
, "items");
578 if (!strcmp(items
, "none")) {
581 mask
= cpu_str_to_log_mask(items
);
583 help_cmd(mon
, "log");
590 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
592 const char *option
= qdict_get_try_str(qdict
, "option");
593 if (!option
|| !strcmp(option
, "on")) {
595 } else if (!strcmp(option
, "off")) {
598 monitor_printf(mon
, "unexpected option %s\n", option
);
603 * do_stop(): Stop VM execution
605 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
607 vm_stop(EXCP_INTERRUPT
);
610 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
612 struct bdrv_iterate_context
{
617 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
619 struct bdrv_iterate_context context
= { mon
, 0 };
621 bdrv_iterate(encrypted_bdrv_it
, &context
);
622 /* only resume the vm if all keys are set and valid */
627 static void bdrv_key_cb(void *opaque
, int err
)
629 Monitor
*mon
= opaque
;
631 /* another key was set successfully, retry to continue */
636 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
638 struct bdrv_iterate_context
*context
= opaque
;
640 if (!context
->err
&& bdrv_key_required(bs
)) {
641 context
->err
= -EBUSY
;
642 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
647 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
649 const char *device
= qdict_get_try_str(qdict
, "device");
651 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
652 if (gdbserver_start(device
) < 0) {
653 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
655 } else if (strcmp(device
, "none") == 0) {
656 monitor_printf(mon
, "Disabled gdbserver\n");
658 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
663 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
665 const char *action
= qdict_get_str(qdict
, "action");
666 if (select_watchdog_action(action
) == -1) {
667 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
671 static void monitor_printc(Monitor
*mon
, int c
)
673 monitor_printf(mon
, "'");
676 monitor_printf(mon
, "\\'");
679 monitor_printf(mon
, "\\\\");
682 monitor_printf(mon
, "\\n");
685 monitor_printf(mon
, "\\r");
688 if (c
>= 32 && c
<= 126) {
689 monitor_printf(mon
, "%c", c
);
691 monitor_printf(mon
, "\\x%02x", c
);
695 monitor_printf(mon
, "'");
698 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
699 target_phys_addr_t addr
, int is_physical
)
702 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
710 if (!env
&& !is_physical
)
715 } else if (wsize
== 4) {
718 /* as default we use the current CS size */
722 if ((env
->efer
& MSR_EFER_LMA
) &&
723 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
727 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
732 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
741 nb_per_line
= line_size
/ wsize
;
746 max_digits
= (wsize
* 8 + 2) / 3;
750 max_digits
= (wsize
* 8) / 4;
754 max_digits
= (wsize
* 8 * 10 + 32) / 33;
763 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
765 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
770 cpu_physical_memory_rw(addr
, buf
, l
, 0);
775 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
776 monitor_printf(mon
, " Cannot access memory\n");
785 v
= ldub_raw(buf
+ i
);
788 v
= lduw_raw(buf
+ i
);
791 v
= (uint32_t)ldl_raw(buf
+ i
);
794 v
= ldq_raw(buf
+ i
);
797 monitor_printf(mon
, " ");
800 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
803 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
806 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
809 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
812 monitor_printc(mon
, v
);
817 monitor_printf(mon
, "\n");
823 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
825 int count
= qdict_get_int(qdict
, "count");
826 int format
= qdict_get_int(qdict
, "format");
827 int size
= qdict_get_int(qdict
, "size");
828 target_long addr
= qdict_get_int(qdict
, "addr");
830 memory_dump(mon
, count
, format
, size
, addr
, 0);
833 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
835 int count
= qdict_get_int(qdict
, "count");
836 int format
= qdict_get_int(qdict
, "format");
837 int size
= qdict_get_int(qdict
, "size");
838 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
840 memory_dump(mon
, count
, format
, size
, addr
, 1);
843 static void do_print(Monitor
*mon
, const QDict
*qdict
)
845 int format
= qdict_get_int(qdict
, "format");
846 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
848 #if TARGET_PHYS_ADDR_BITS == 32
851 monitor_printf(mon
, "%#o", val
);
854 monitor_printf(mon
, "%#x", val
);
857 monitor_printf(mon
, "%u", val
);
861 monitor_printf(mon
, "%d", val
);
864 monitor_printc(mon
, val
);
870 monitor_printf(mon
, "%#" PRIo64
, val
);
873 monitor_printf(mon
, "%#" PRIx64
, val
);
876 monitor_printf(mon
, "%" PRIu64
, val
);
880 monitor_printf(mon
, "%" PRId64
, val
);
883 monitor_printc(mon
, val
);
887 monitor_printf(mon
, "\n");
890 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
893 uint32_t size
= qdict_get_int(qdict
, "size");
894 const char *filename
= qdict_get_str(qdict
, "filename");
895 target_long addr
= qdict_get_int(qdict
, "val");
904 f
= fopen(filename
, "wb");
906 monitor_printf(mon
, "could not open '%s'\n", filename
);
913 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
914 fwrite(buf
, 1, l
, f
);
921 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
926 uint32_t size
= qdict_get_int(qdict
, "size");
927 const char *filename
= qdict_get_str(qdict
, "filename");
928 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
930 f
= fopen(filename
, "wb");
932 monitor_printf(mon
, "could not open '%s'\n", filename
);
939 cpu_physical_memory_rw(addr
, buf
, l
, 0);
940 fwrite(buf
, 1, l
, f
);
948 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
953 uint32_t start
= qdict_get_int(qdict
, "start");
954 uint32_t size
= qdict_get_int(qdict
, "size");
957 for(addr
= start
; addr
< (start
+ size
); addr
++) {
958 cpu_physical_memory_rw(addr
, buf
, 1, 0);
959 /* BSD sum algorithm ('sum' Unix command) */
960 sum
= (sum
>> 1) | (sum
<< 15);
963 monitor_printf(mon
, "%05d\n", sum
);
971 static const KeyDef key_defs
[] = {
998 { 0x0e, "backspace" },
1035 { 0x37, "asterisk" },
1038 { 0x3a, "caps_lock" },
1049 { 0x45, "num_lock" },
1050 { 0x46, "scroll_lock" },
1052 { 0xb5, "kp_divide" },
1053 { 0x37, "kp_multiply" },
1054 { 0x4a, "kp_subtract" },
1056 { 0x9c, "kp_enter" },
1057 { 0x53, "kp_decimal" },
1090 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1105 { 0xfe, "compose" },
1110 static int get_keycode(const char *key
)
1116 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1117 if (!strcmp(key
, p
->name
))
1120 if (strstart(key
, "0x", NULL
)) {
1121 ret
= strtoul(key
, &endp
, 0);
1122 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1128 #define MAX_KEYCODES 16
1129 static uint8_t keycodes
[MAX_KEYCODES
];
1130 static int nb_pending_keycodes
;
1131 static QEMUTimer
*key_timer
;
1133 static void release_keys(void *opaque
)
1137 while (nb_pending_keycodes
> 0) {
1138 nb_pending_keycodes
--;
1139 keycode
= keycodes
[nb_pending_keycodes
];
1141 kbd_put_keycode(0xe0);
1142 kbd_put_keycode(keycode
| 0x80);
1146 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1148 char keyname_buf
[16];
1150 int keyname_len
, keycode
, i
;
1151 const char *string
= qdict_get_str(qdict
, "string");
1152 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1153 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1155 if (nb_pending_keycodes
> 0) {
1156 qemu_del_timer(key_timer
);
1163 separator
= strchr(string
, '-');
1164 keyname_len
= separator
? separator
- string
: strlen(string
);
1165 if (keyname_len
> 0) {
1166 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1167 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1168 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1171 if (i
== MAX_KEYCODES
) {
1172 monitor_printf(mon
, "too many keys\n");
1175 keyname_buf
[keyname_len
] = 0;
1176 keycode
= get_keycode(keyname_buf
);
1178 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1181 keycodes
[i
++] = keycode
;
1185 string
= separator
+ 1;
1187 nb_pending_keycodes
= i
;
1188 /* key down events */
1189 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1190 keycode
= keycodes
[i
];
1192 kbd_put_keycode(0xe0);
1193 kbd_put_keycode(keycode
& 0x7f);
1195 /* delayed key up events */
1196 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1197 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1200 static int mouse_button_state
;
1202 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1205 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1206 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1207 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1208 dx
= strtol(dx_str
, NULL
, 0);
1209 dy
= strtol(dy_str
, NULL
, 0);
1212 dz
= strtol(dz_str
, NULL
, 0);
1213 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1216 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1218 int button_state
= qdict_get_int(qdict
, "button_state");
1219 mouse_button_state
= button_state
;
1220 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1223 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1225 int size
= qdict_get_int(qdict
, "size");
1226 int addr
= qdict_get_int(qdict
, "addr");
1227 int has_index
= qdict_haskey(qdict
, "index");
1232 int index
= qdict_get_int(qdict
, "index");
1233 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1241 val
= cpu_inb(addr
);
1245 val
= cpu_inw(addr
);
1249 val
= cpu_inl(addr
);
1253 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1254 suffix
, addr
, size
* 2, val
);
1257 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1259 int size
= qdict_get_int(qdict
, "size");
1260 int addr
= qdict_get_int(qdict
, "addr");
1261 int val
= qdict_get_int(qdict
, "val");
1263 addr
&= IOPORTS_MASK
;
1268 cpu_outb(addr
, val
);
1271 cpu_outw(addr
, val
);
1274 cpu_outl(addr
, val
);
1279 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1282 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1284 res
= qemu_boot_set(bootdevice
);
1286 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1287 } else if (res
> 0) {
1288 monitor_printf(mon
, "setting boot device list failed\n");
1290 monitor_printf(mon
, "no function defined to set boot device list for "
1291 "this architecture\n");
1295 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1297 qemu_system_reset_request();
1300 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1302 qemu_system_powerdown_request();
1305 #if defined(TARGET_I386)
1306 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1308 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1311 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1312 pte
& PG_PSE_MASK
? 'P' : '-',
1313 pte
& PG_DIRTY_MASK
? 'D' : '-',
1314 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1315 pte
& PG_PCD_MASK
? 'C' : '-',
1316 pte
& PG_PWT_MASK
? 'T' : '-',
1317 pte
& PG_USER_MASK
? 'U' : '-',
1318 pte
& PG_RW_MASK
? 'W' : '-');
1321 static void tlb_info(Monitor
*mon
)
1325 uint32_t pgd
, pde
, pte
;
1327 env
= mon_get_cpu();
1331 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1332 monitor_printf(mon
, "PG disabled\n");
1335 pgd
= env
->cr
[3] & ~0xfff;
1336 for(l1
= 0; l1
< 1024; l1
++) {
1337 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1338 pde
= le32_to_cpu(pde
);
1339 if (pde
& PG_PRESENT_MASK
) {
1340 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1341 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1343 for(l2
= 0; l2
< 1024; l2
++) {
1344 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1345 (uint8_t *)&pte
, 4);
1346 pte
= le32_to_cpu(pte
);
1347 if (pte
& PG_PRESENT_MASK
) {
1348 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1358 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1359 uint32_t end
, int prot
)
1362 prot1
= *plast_prot
;
1363 if (prot
!= prot1
) {
1364 if (*pstart
!= -1) {
1365 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1366 *pstart
, end
, end
- *pstart
,
1367 prot1
& PG_USER_MASK
? 'u' : '-',
1369 prot1
& PG_RW_MASK
? 'w' : '-');
1379 static void mem_info(Monitor
*mon
)
1382 int l1
, l2
, prot
, last_prot
;
1383 uint32_t pgd
, pde
, pte
, start
, end
;
1385 env
= mon_get_cpu();
1389 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1390 monitor_printf(mon
, "PG disabled\n");
1393 pgd
= env
->cr
[3] & ~0xfff;
1396 for(l1
= 0; l1
< 1024; l1
++) {
1397 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1398 pde
= le32_to_cpu(pde
);
1400 if (pde
& PG_PRESENT_MASK
) {
1401 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1402 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1403 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1405 for(l2
= 0; l2
< 1024; l2
++) {
1406 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1407 (uint8_t *)&pte
, 4);
1408 pte
= le32_to_cpu(pte
);
1409 end
= (l1
<< 22) + (l2
<< 12);
1410 if (pte
& PG_PRESENT_MASK
) {
1411 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1415 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1420 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1426 #if defined(TARGET_SH4)
1428 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1430 monitor_printf(mon
, " tlb%i:\t"
1431 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1432 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1433 "dirty=%hhu writethrough=%hhu\n",
1435 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1436 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1440 static void tlb_info(Monitor
*mon
)
1442 CPUState
*env
= mon_get_cpu();
1445 monitor_printf (mon
, "ITLB:\n");
1446 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1447 print_tlb (mon
, i
, &env
->itlb
[i
]);
1448 monitor_printf (mon
, "UTLB:\n");
1449 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1450 print_tlb (mon
, i
, &env
->utlb
[i
]);
1455 static void do_info_kvm(Monitor
*mon
)
1458 monitor_printf(mon
, "kvm support: ");
1460 monitor_printf(mon
, "enabled\n");
1462 monitor_printf(mon
, "disabled\n");
1464 monitor_printf(mon
, "kvm support: not compiled\n");
1468 static void do_info_numa(Monitor
*mon
)
1473 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1474 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1475 monitor_printf(mon
, "node %d cpus:", i
);
1476 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1477 if (env
->numa_node
== i
) {
1478 monitor_printf(mon
, " %d", env
->cpu_index
);
1481 monitor_printf(mon
, "\n");
1482 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1487 #ifdef CONFIG_PROFILER
1492 static void do_info_profile(Monitor
*mon
)
1498 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1499 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1500 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1501 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1506 static void do_info_profile(Monitor
*mon
)
1508 monitor_printf(mon
, "Internal profiler not compiled\n");
1512 /* Capture support */
1513 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1515 static void do_info_capture(Monitor
*mon
)
1520 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1521 monitor_printf(mon
, "[%d]: ", i
);
1522 s
->ops
.info (s
->opaque
);
1527 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1530 int n
= qdict_get_int(qdict
, "n");
1533 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1535 s
->ops
.destroy (s
->opaque
);
1536 QLIST_REMOVE (s
, entries
);
1543 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1545 const char *path
= qdict_get_str(qdict
, "path");
1546 int has_freq
= qdict_haskey(qdict
, "freq");
1547 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1548 int has_bits
= qdict_haskey(qdict
, "bits");
1549 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1550 int has_channels
= qdict_haskey(qdict
, "nchannels");
1551 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1554 s
= qemu_mallocz (sizeof (*s
));
1556 freq
= has_freq
? freq
: 44100;
1557 bits
= has_bits
? bits
: 16;
1558 nchannels
= has_channels
? nchannels
: 2;
1560 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1561 monitor_printf(mon
, "Faied to add wave capture\n");
1564 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1568 #if defined(TARGET_I386)
1569 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1572 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1574 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1575 if (env
->cpu_index
== cpu_index
) {
1576 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1582 static void do_info_status(Monitor
*mon
)
1586 monitor_printf(mon
, "VM status: running (single step mode)\n");
1588 monitor_printf(mon
, "VM status: running\n");
1591 monitor_printf(mon
, "VM status: paused\n");
1595 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1597 int value
= qdict_get_int(qdict
, "value");
1598 ram_addr_t target
= value
;
1599 qemu_balloon(target
<< 20);
1602 static void do_info_balloon(Monitor
*mon
)
1606 actual
= qemu_balloon_status();
1607 if (kvm_enabled() && !kvm_has_sync_mmu())
1608 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1609 "ballooning disabled\n");
1610 else if (actual
== 0)
1611 monitor_printf(mon
, "Ballooning not activated in VM\n");
1613 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1616 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1618 qemu_acl
*acl
= qemu_acl_find(name
);
1621 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1626 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1628 const char *aclname
= qdict_get_str(qdict
, "aclname");
1629 qemu_acl
*acl
= find_acl(mon
, aclname
);
1630 qemu_acl_entry
*entry
;
1634 monitor_printf(mon
, "policy: %s\n",
1635 acl
->defaultDeny
? "deny" : "allow");
1636 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1638 monitor_printf(mon
, "%d: %s %s\n", i
,
1639 entry
->deny
? "deny" : "allow", entry
->match
);
1644 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1646 const char *aclname
= qdict_get_str(qdict
, "aclname");
1647 qemu_acl
*acl
= find_acl(mon
, aclname
);
1650 qemu_acl_reset(acl
);
1651 monitor_printf(mon
, "acl: removed all rules\n");
1655 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1657 const char *aclname
= qdict_get_str(qdict
, "aclname");
1658 const char *policy
= qdict_get_str(qdict
, "policy");
1659 qemu_acl
*acl
= find_acl(mon
, aclname
);
1662 if (strcmp(policy
, "allow") == 0) {
1663 acl
->defaultDeny
= 0;
1664 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1665 } else if (strcmp(policy
, "deny") == 0) {
1666 acl
->defaultDeny
= 1;
1667 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1669 monitor_printf(mon
, "acl: unknown policy '%s', "
1670 "expected 'deny' or 'allow'\n", policy
);
1675 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1677 const char *aclname
= qdict_get_str(qdict
, "aclname");
1678 const char *match
= qdict_get_str(qdict
, "match");
1679 const char *policy
= qdict_get_str(qdict
, "policy");
1680 int has_index
= qdict_haskey(qdict
, "index");
1681 int index
= qdict_get_try_int(qdict
, "index", -1);
1682 qemu_acl
*acl
= find_acl(mon
, aclname
);
1686 if (strcmp(policy
, "allow") == 0) {
1688 } else if (strcmp(policy
, "deny") == 0) {
1691 monitor_printf(mon
, "acl: unknown policy '%s', "
1692 "expected 'deny' or 'allow'\n", policy
);
1696 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1698 ret
= qemu_acl_append(acl
, deny
, match
);
1700 monitor_printf(mon
, "acl: unable to add acl entry\n");
1702 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1706 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1708 const char *aclname
= qdict_get_str(qdict
, "aclname");
1709 const char *match
= qdict_get_str(qdict
, "match");
1710 qemu_acl
*acl
= find_acl(mon
, aclname
);
1714 ret
= qemu_acl_remove(acl
, match
);
1716 monitor_printf(mon
, "acl: no matching acl entry\n");
1718 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1722 #if defined(TARGET_I386)
1723 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1726 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1727 int bank
= qdict_get_int(qdict
, "bank");
1728 uint64_t status
= qdict_get_int(qdict
, "status");
1729 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1730 uint64_t addr
= qdict_get_int(qdict
, "addr");
1731 uint64_t misc
= qdict_get_int(qdict
, "misc");
1733 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1734 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1735 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1741 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1743 const char *fdname
= qdict_get_str(qdict
, "fdname");
1747 fd
= qemu_chr_get_msgfd(mon
->chr
);
1749 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1753 if (qemu_isdigit(fdname
[0])) {
1754 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1760 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1765 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1766 if (strcmp(monfd
->name
, fdname
) != 0) {
1775 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1776 monfd
->name
= qemu_strdup(fdname
);
1779 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1782 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1784 const char *fdname
= qdict_get_str(qdict
, "fdname");
1787 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1788 if (strcmp(monfd
->name
, fdname
) != 0) {
1792 QLIST_REMOVE(monfd
, next
);
1794 qemu_free(monfd
->name
);
1799 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1803 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1805 int saved_vm_running
= vm_running
;
1806 const char *name
= qdict_get_str(qdict
, "name");
1810 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1814 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1818 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1821 if (strcmp(monfd
->name
, fdname
) != 0) {
1827 /* caller takes ownership of fd */
1828 QLIST_REMOVE(monfd
, next
);
1829 qemu_free(monfd
->name
);
1838 static const mon_cmd_t mon_cmds
[] = {
1839 #include "qemu-monitor.h"
1843 /* Please update qemu-monitor.hx when adding or changing commands */
1844 static const mon_cmd_t info_cmds
[] = {
1849 .help
= "show the version of QEMU",
1850 .mhandler
.info
= do_info_version
,
1856 .help
= "show the network state",
1857 .mhandler
.info
= do_info_network
,
1863 .help
= "show the character devices",
1864 .mhandler
.info
= qemu_chr_info
,
1870 .help
= "show the block devices",
1871 .mhandler
.info
= bdrv_info
,
1874 .name
= "blockstats",
1877 .help
= "show block device statistics",
1878 .mhandler
.info
= bdrv_info_stats
,
1881 .name
= "registers",
1884 .help
= "show the cpu registers",
1885 .mhandler
.info
= do_info_registers
,
1891 .help
= "show infos for each CPU",
1892 .mhandler
.info
= do_info_cpus
,
1898 .help
= "show the command line history",
1899 .mhandler
.info
= do_info_history
,
1905 .help
= "show the interrupts statistics (if available)",
1906 .mhandler
.info
= irq_info
,
1912 .help
= "show i8259 (PIC) state",
1913 .mhandler
.info
= pic_info
,
1919 .help
= "show PCI info",
1920 .mhandler
.info
= pci_info
,
1922 #if defined(TARGET_I386) || defined(TARGET_SH4)
1927 .help
= "show virtual to physical memory mappings",
1928 .mhandler
.info
= tlb_info
,
1931 #if defined(TARGET_I386)
1936 .help
= "show the active virtual memory mappings",
1937 .mhandler
.info
= mem_info
,
1943 .help
= "show state of HPET",
1944 .mhandler
.info
= do_info_hpet
,
1951 .help
= "show dynamic compiler info",
1952 .mhandler
.info
= do_info_jit
,
1958 .help
= "show KVM information",
1959 .mhandler
.info
= do_info_kvm
,
1965 .help
= "show NUMA information",
1966 .mhandler
.info
= do_info_numa
,
1972 .help
= "show guest USB devices",
1973 .mhandler
.info
= usb_info
,
1979 .help
= "show host USB devices",
1980 .mhandler
.info
= usb_host_info
,
1986 .help
= "show profiling information",
1987 .mhandler
.info
= do_info_profile
,
1993 .help
= "show capture information",
1994 .mhandler
.info
= do_info_capture
,
1997 .name
= "snapshots",
2000 .help
= "show the currently saved VM snapshots",
2001 .mhandler
.info
= do_info_snapshots
,
2007 .help
= "show the current VM status (running|paused)",
2008 .mhandler
.info
= do_info_status
,
2014 .help
= "show guest PCMCIA status",
2015 .mhandler
.info
= pcmcia_info
,
2021 .help
= "show which guest mouse is receiving events",
2022 .mhandler
.info
= do_info_mice
,
2028 .help
= "show the vnc server status",
2029 .mhandler
.info
= do_info_vnc
,
2035 .help
= "show the current VM name",
2036 .mhandler
.info
= do_info_name
,
2042 .help
= "show the current VM UUID",
2043 .mhandler
.info
= do_info_uuid
,
2045 #if defined(TARGET_PPC)
2050 .help
= "show CPU statistics",
2051 .mhandler
.info
= do_info_cpu_stats
,
2054 #if defined(CONFIG_SLIRP)
2059 .help
= "show user network stack connection states",
2060 .mhandler
.info
= do_info_usernet
,
2067 .help
= "show migration status",
2068 .mhandler
.info
= do_info_migrate
,
2074 .help
= "show balloon information",
2075 .mhandler
.info
= do_info_balloon
,
2081 .help
= "show device tree",
2082 .mhandler
.info
= do_info_qtree
,
2088 .help
= "show qdev device model list",
2089 .mhandler
.info
= do_info_qdm
,
2095 .help
= "show roms",
2096 .mhandler
.info
= do_info_roms
,
2103 /*******************************************************************/
2105 static const char *pch
;
2106 static jmp_buf expr_env
;
2111 typedef struct MonitorDef
{
2114 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2118 #if defined(TARGET_I386)
2119 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2121 CPUState
*env
= mon_get_cpu();
2124 return env
->eip
+ env
->segs
[R_CS
].base
;
2128 #if defined(TARGET_PPC)
2129 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2131 CPUState
*env
= mon_get_cpu();
2139 for (i
= 0; i
< 8; i
++)
2140 u
|= env
->crf
[i
] << (32 - (4 * i
));
2145 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2147 CPUState
*env
= mon_get_cpu();
2153 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2155 CPUState
*env
= mon_get_cpu();
2161 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2163 CPUState
*env
= mon_get_cpu();
2166 return cpu_ppc_load_decr(env
);
2169 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2171 CPUState
*env
= mon_get_cpu();
2174 return cpu_ppc_load_tbu(env
);
2177 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2179 CPUState
*env
= mon_get_cpu();
2182 return cpu_ppc_load_tbl(env
);
2186 #if defined(TARGET_SPARC)
2187 #ifndef TARGET_SPARC64
2188 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2190 CPUState
*env
= mon_get_cpu();
2193 return GET_PSR(env
);
2197 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2199 CPUState
*env
= mon_get_cpu();
2202 return env
->regwptr
[val
];
2206 static const MonitorDef monitor_defs
[] = {
2209 #define SEG(name, seg) \
2210 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2211 { name ".base", offsetof(CPUState, segs[seg].base) },\
2212 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2214 { "eax", offsetof(CPUState
, regs
[0]) },
2215 { "ecx", offsetof(CPUState
, regs
[1]) },
2216 { "edx", offsetof(CPUState
, regs
[2]) },
2217 { "ebx", offsetof(CPUState
, regs
[3]) },
2218 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2219 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2220 { "esi", offsetof(CPUState
, regs
[6]) },
2221 { "edi", offsetof(CPUState
, regs
[7]) },
2222 #ifdef TARGET_X86_64
2223 { "r8", offsetof(CPUState
, regs
[8]) },
2224 { "r9", offsetof(CPUState
, regs
[9]) },
2225 { "r10", offsetof(CPUState
, regs
[10]) },
2226 { "r11", offsetof(CPUState
, regs
[11]) },
2227 { "r12", offsetof(CPUState
, regs
[12]) },
2228 { "r13", offsetof(CPUState
, regs
[13]) },
2229 { "r14", offsetof(CPUState
, regs
[14]) },
2230 { "r15", offsetof(CPUState
, regs
[15]) },
2232 { "eflags", offsetof(CPUState
, eflags
) },
2233 { "eip", offsetof(CPUState
, eip
) },
2240 { "pc", 0, monitor_get_pc
, },
2241 #elif defined(TARGET_PPC)
2242 /* General purpose registers */
2243 { "r0", offsetof(CPUState
, gpr
[0]) },
2244 { "r1", offsetof(CPUState
, gpr
[1]) },
2245 { "r2", offsetof(CPUState
, gpr
[2]) },
2246 { "r3", offsetof(CPUState
, gpr
[3]) },
2247 { "r4", offsetof(CPUState
, gpr
[4]) },
2248 { "r5", offsetof(CPUState
, gpr
[5]) },
2249 { "r6", offsetof(CPUState
, gpr
[6]) },
2250 { "r7", offsetof(CPUState
, gpr
[7]) },
2251 { "r8", offsetof(CPUState
, gpr
[8]) },
2252 { "r9", offsetof(CPUState
, gpr
[9]) },
2253 { "r10", offsetof(CPUState
, gpr
[10]) },
2254 { "r11", offsetof(CPUState
, gpr
[11]) },
2255 { "r12", offsetof(CPUState
, gpr
[12]) },
2256 { "r13", offsetof(CPUState
, gpr
[13]) },
2257 { "r14", offsetof(CPUState
, gpr
[14]) },
2258 { "r15", offsetof(CPUState
, gpr
[15]) },
2259 { "r16", offsetof(CPUState
, gpr
[16]) },
2260 { "r17", offsetof(CPUState
, gpr
[17]) },
2261 { "r18", offsetof(CPUState
, gpr
[18]) },
2262 { "r19", offsetof(CPUState
, gpr
[19]) },
2263 { "r20", offsetof(CPUState
, gpr
[20]) },
2264 { "r21", offsetof(CPUState
, gpr
[21]) },
2265 { "r22", offsetof(CPUState
, gpr
[22]) },
2266 { "r23", offsetof(CPUState
, gpr
[23]) },
2267 { "r24", offsetof(CPUState
, gpr
[24]) },
2268 { "r25", offsetof(CPUState
, gpr
[25]) },
2269 { "r26", offsetof(CPUState
, gpr
[26]) },
2270 { "r27", offsetof(CPUState
, gpr
[27]) },
2271 { "r28", offsetof(CPUState
, gpr
[28]) },
2272 { "r29", offsetof(CPUState
, gpr
[29]) },
2273 { "r30", offsetof(CPUState
, gpr
[30]) },
2274 { "r31", offsetof(CPUState
, gpr
[31]) },
2275 /* Floating point registers */
2276 { "f0", offsetof(CPUState
, fpr
[0]) },
2277 { "f1", offsetof(CPUState
, fpr
[1]) },
2278 { "f2", offsetof(CPUState
, fpr
[2]) },
2279 { "f3", offsetof(CPUState
, fpr
[3]) },
2280 { "f4", offsetof(CPUState
, fpr
[4]) },
2281 { "f5", offsetof(CPUState
, fpr
[5]) },
2282 { "f6", offsetof(CPUState
, fpr
[6]) },
2283 { "f7", offsetof(CPUState
, fpr
[7]) },
2284 { "f8", offsetof(CPUState
, fpr
[8]) },
2285 { "f9", offsetof(CPUState
, fpr
[9]) },
2286 { "f10", offsetof(CPUState
, fpr
[10]) },
2287 { "f11", offsetof(CPUState
, fpr
[11]) },
2288 { "f12", offsetof(CPUState
, fpr
[12]) },
2289 { "f13", offsetof(CPUState
, fpr
[13]) },
2290 { "f14", offsetof(CPUState
, fpr
[14]) },
2291 { "f15", offsetof(CPUState
, fpr
[15]) },
2292 { "f16", offsetof(CPUState
, fpr
[16]) },
2293 { "f17", offsetof(CPUState
, fpr
[17]) },
2294 { "f18", offsetof(CPUState
, fpr
[18]) },
2295 { "f19", offsetof(CPUState
, fpr
[19]) },
2296 { "f20", offsetof(CPUState
, fpr
[20]) },
2297 { "f21", offsetof(CPUState
, fpr
[21]) },
2298 { "f22", offsetof(CPUState
, fpr
[22]) },
2299 { "f23", offsetof(CPUState
, fpr
[23]) },
2300 { "f24", offsetof(CPUState
, fpr
[24]) },
2301 { "f25", offsetof(CPUState
, fpr
[25]) },
2302 { "f26", offsetof(CPUState
, fpr
[26]) },
2303 { "f27", offsetof(CPUState
, fpr
[27]) },
2304 { "f28", offsetof(CPUState
, fpr
[28]) },
2305 { "f29", offsetof(CPUState
, fpr
[29]) },
2306 { "f30", offsetof(CPUState
, fpr
[30]) },
2307 { "f31", offsetof(CPUState
, fpr
[31]) },
2308 { "fpscr", offsetof(CPUState
, fpscr
) },
2309 /* Next instruction pointer */
2310 { "nip|pc", offsetof(CPUState
, nip
) },
2311 { "lr", offsetof(CPUState
, lr
) },
2312 { "ctr", offsetof(CPUState
, ctr
) },
2313 { "decr", 0, &monitor_get_decr
, },
2314 { "ccr", 0, &monitor_get_ccr
, },
2315 /* Machine state register */
2316 { "msr", 0, &monitor_get_msr
, },
2317 { "xer", 0, &monitor_get_xer
, },
2318 { "tbu", 0, &monitor_get_tbu
, },
2319 { "tbl", 0, &monitor_get_tbl
, },
2320 #if defined(TARGET_PPC64)
2321 /* Address space register */
2322 { "asr", offsetof(CPUState
, asr
) },
2324 /* Segment registers */
2325 { "sdr1", offsetof(CPUState
, sdr1
) },
2326 { "sr0", offsetof(CPUState
, sr
[0]) },
2327 { "sr1", offsetof(CPUState
, sr
[1]) },
2328 { "sr2", offsetof(CPUState
, sr
[2]) },
2329 { "sr3", offsetof(CPUState
, sr
[3]) },
2330 { "sr4", offsetof(CPUState
, sr
[4]) },
2331 { "sr5", offsetof(CPUState
, sr
[5]) },
2332 { "sr6", offsetof(CPUState
, sr
[6]) },
2333 { "sr7", offsetof(CPUState
, sr
[7]) },
2334 { "sr8", offsetof(CPUState
, sr
[8]) },
2335 { "sr9", offsetof(CPUState
, sr
[9]) },
2336 { "sr10", offsetof(CPUState
, sr
[10]) },
2337 { "sr11", offsetof(CPUState
, sr
[11]) },
2338 { "sr12", offsetof(CPUState
, sr
[12]) },
2339 { "sr13", offsetof(CPUState
, sr
[13]) },
2340 { "sr14", offsetof(CPUState
, sr
[14]) },
2341 { "sr15", offsetof(CPUState
, sr
[15]) },
2342 /* Too lazy to put BATs and SPRs ... */
2343 #elif defined(TARGET_SPARC)
2344 { "g0", offsetof(CPUState
, gregs
[0]) },
2345 { "g1", offsetof(CPUState
, gregs
[1]) },
2346 { "g2", offsetof(CPUState
, gregs
[2]) },
2347 { "g3", offsetof(CPUState
, gregs
[3]) },
2348 { "g4", offsetof(CPUState
, gregs
[4]) },
2349 { "g5", offsetof(CPUState
, gregs
[5]) },
2350 { "g6", offsetof(CPUState
, gregs
[6]) },
2351 { "g7", offsetof(CPUState
, gregs
[7]) },
2352 { "o0", 0, monitor_get_reg
},
2353 { "o1", 1, monitor_get_reg
},
2354 { "o2", 2, monitor_get_reg
},
2355 { "o3", 3, monitor_get_reg
},
2356 { "o4", 4, monitor_get_reg
},
2357 { "o5", 5, monitor_get_reg
},
2358 { "o6", 6, monitor_get_reg
},
2359 { "o7", 7, monitor_get_reg
},
2360 { "l0", 8, monitor_get_reg
},
2361 { "l1", 9, monitor_get_reg
},
2362 { "l2", 10, monitor_get_reg
},
2363 { "l3", 11, monitor_get_reg
},
2364 { "l4", 12, monitor_get_reg
},
2365 { "l5", 13, monitor_get_reg
},
2366 { "l6", 14, monitor_get_reg
},
2367 { "l7", 15, monitor_get_reg
},
2368 { "i0", 16, monitor_get_reg
},
2369 { "i1", 17, monitor_get_reg
},
2370 { "i2", 18, monitor_get_reg
},
2371 { "i3", 19, monitor_get_reg
},
2372 { "i4", 20, monitor_get_reg
},
2373 { "i5", 21, monitor_get_reg
},
2374 { "i6", 22, monitor_get_reg
},
2375 { "i7", 23, monitor_get_reg
},
2376 { "pc", offsetof(CPUState
, pc
) },
2377 { "npc", offsetof(CPUState
, npc
) },
2378 { "y", offsetof(CPUState
, y
) },
2379 #ifndef TARGET_SPARC64
2380 { "psr", 0, &monitor_get_psr
, },
2381 { "wim", offsetof(CPUState
, wim
) },
2383 { "tbr", offsetof(CPUState
, tbr
) },
2384 { "fsr", offsetof(CPUState
, fsr
) },
2385 { "f0", offsetof(CPUState
, fpr
[0]) },
2386 { "f1", offsetof(CPUState
, fpr
[1]) },
2387 { "f2", offsetof(CPUState
, fpr
[2]) },
2388 { "f3", offsetof(CPUState
, fpr
[3]) },
2389 { "f4", offsetof(CPUState
, fpr
[4]) },
2390 { "f5", offsetof(CPUState
, fpr
[5]) },
2391 { "f6", offsetof(CPUState
, fpr
[6]) },
2392 { "f7", offsetof(CPUState
, fpr
[7]) },
2393 { "f8", offsetof(CPUState
, fpr
[8]) },
2394 { "f9", offsetof(CPUState
, fpr
[9]) },
2395 { "f10", offsetof(CPUState
, fpr
[10]) },
2396 { "f11", offsetof(CPUState
, fpr
[11]) },
2397 { "f12", offsetof(CPUState
, fpr
[12]) },
2398 { "f13", offsetof(CPUState
, fpr
[13]) },
2399 { "f14", offsetof(CPUState
, fpr
[14]) },
2400 { "f15", offsetof(CPUState
, fpr
[15]) },
2401 { "f16", offsetof(CPUState
, fpr
[16]) },
2402 { "f17", offsetof(CPUState
, fpr
[17]) },
2403 { "f18", offsetof(CPUState
, fpr
[18]) },
2404 { "f19", offsetof(CPUState
, fpr
[19]) },
2405 { "f20", offsetof(CPUState
, fpr
[20]) },
2406 { "f21", offsetof(CPUState
, fpr
[21]) },
2407 { "f22", offsetof(CPUState
, fpr
[22]) },
2408 { "f23", offsetof(CPUState
, fpr
[23]) },
2409 { "f24", offsetof(CPUState
, fpr
[24]) },
2410 { "f25", offsetof(CPUState
, fpr
[25]) },
2411 { "f26", offsetof(CPUState
, fpr
[26]) },
2412 { "f27", offsetof(CPUState
, fpr
[27]) },
2413 { "f28", offsetof(CPUState
, fpr
[28]) },
2414 { "f29", offsetof(CPUState
, fpr
[29]) },
2415 { "f30", offsetof(CPUState
, fpr
[30]) },
2416 { "f31", offsetof(CPUState
, fpr
[31]) },
2417 #ifdef TARGET_SPARC64
2418 { "f32", offsetof(CPUState
, fpr
[32]) },
2419 { "f34", offsetof(CPUState
, fpr
[34]) },
2420 { "f36", offsetof(CPUState
, fpr
[36]) },
2421 { "f38", offsetof(CPUState
, fpr
[38]) },
2422 { "f40", offsetof(CPUState
, fpr
[40]) },
2423 { "f42", offsetof(CPUState
, fpr
[42]) },
2424 { "f44", offsetof(CPUState
, fpr
[44]) },
2425 { "f46", offsetof(CPUState
, fpr
[46]) },
2426 { "f48", offsetof(CPUState
, fpr
[48]) },
2427 { "f50", offsetof(CPUState
, fpr
[50]) },
2428 { "f52", offsetof(CPUState
, fpr
[52]) },
2429 { "f54", offsetof(CPUState
, fpr
[54]) },
2430 { "f56", offsetof(CPUState
, fpr
[56]) },
2431 { "f58", offsetof(CPUState
, fpr
[58]) },
2432 { "f60", offsetof(CPUState
, fpr
[60]) },
2433 { "f62", offsetof(CPUState
, fpr
[62]) },
2434 { "asi", offsetof(CPUState
, asi
) },
2435 { "pstate", offsetof(CPUState
, pstate
) },
2436 { "cansave", offsetof(CPUState
, cansave
) },
2437 { "canrestore", offsetof(CPUState
, canrestore
) },
2438 { "otherwin", offsetof(CPUState
, otherwin
) },
2439 { "wstate", offsetof(CPUState
, wstate
) },
2440 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2441 { "fprs", offsetof(CPUState
, fprs
) },
2447 static void expr_error(Monitor
*mon
, const char *msg
)
2449 monitor_printf(mon
, "%s\n", msg
);
2450 longjmp(expr_env
, 1);
2453 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2454 static int get_monitor_def(target_long
*pval
, const char *name
)
2456 const MonitorDef
*md
;
2459 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2460 if (compare_cmd(name
, md
->name
)) {
2461 if (md
->get_value
) {
2462 *pval
= md
->get_value(md
, md
->offset
);
2464 CPUState
*env
= mon_get_cpu();
2467 ptr
= (uint8_t *)env
+ md
->offset
;
2470 *pval
= *(int32_t *)ptr
;
2473 *pval
= *(target_long
*)ptr
;
2486 static void next(void)
2490 while (qemu_isspace(*pch
))
2495 static int64_t expr_sum(Monitor
*mon
);
2497 static int64_t expr_unary(Monitor
*mon
)
2506 n
= expr_unary(mon
);
2510 n
= -expr_unary(mon
);
2514 n
= ~expr_unary(mon
);
2520 expr_error(mon
, "')' expected");
2527 expr_error(mon
, "character constant expected");
2531 expr_error(mon
, "missing terminating \' character");
2541 while ((*pch
>= 'a' && *pch
<= 'z') ||
2542 (*pch
>= 'A' && *pch
<= 'Z') ||
2543 (*pch
>= '0' && *pch
<= '9') ||
2544 *pch
== '_' || *pch
== '.') {
2545 if ((q
- buf
) < sizeof(buf
) - 1)
2549 while (qemu_isspace(*pch
))
2552 ret
= get_monitor_def(®
, buf
);
2554 expr_error(mon
, "unknown register");
2556 expr_error(mon
, "no cpu defined");
2561 expr_error(mon
, "unexpected end of expression");
2565 #if TARGET_PHYS_ADDR_BITS > 32
2566 n
= strtoull(pch
, &p
, 0);
2568 n
= strtoul(pch
, &p
, 0);
2571 expr_error(mon
, "invalid char in expression");
2574 while (qemu_isspace(*pch
))
2582 static int64_t expr_prod(Monitor
*mon
)
2587 val
= expr_unary(mon
);
2590 if (op
!= '*' && op
!= '/' && op
!= '%')
2593 val2
= expr_unary(mon
);
2602 expr_error(mon
, "division by zero");
2613 static int64_t expr_logic(Monitor
*mon
)
2618 val
= expr_prod(mon
);
2621 if (op
!= '&' && op
!= '|' && op
!= '^')
2624 val2
= expr_prod(mon
);
2641 static int64_t expr_sum(Monitor
*mon
)
2646 val
= expr_logic(mon
);
2649 if (op
!= '+' && op
!= '-')
2652 val2
= expr_logic(mon
);
2661 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2664 if (setjmp(expr_env
)) {
2668 while (qemu_isspace(*pch
))
2670 *pval
= expr_sum(mon
);
2675 static int get_str(char *buf
, int buf_size
, const char **pp
)
2683 while (qemu_isspace(*p
))
2693 while (*p
!= '\0' && *p
!= '\"') {
2709 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2712 if ((q
- buf
) < buf_size
- 1) {
2716 if ((q
- buf
) < buf_size
- 1) {
2723 qemu_printf("unterminated string\n");
2728 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2729 if ((q
- buf
) < buf_size
- 1) {
2741 * Store the command-name in cmdname, and return a pointer to
2742 * the remaining of the command string.
2744 static const char *get_command_name(const char *cmdline
,
2745 char *cmdname
, size_t nlen
)
2748 const char *p
, *pstart
;
2751 while (qemu_isspace(*p
))
2756 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2761 memcpy(cmdname
, pstart
, len
);
2762 cmdname
[len
] = '\0';
2767 * Read key of 'type' into 'key' and return the current
2770 static char *key_get_info(const char *type
, char **key
)
2778 p
= strchr(type
, ':');
2785 str
= qemu_malloc(len
+ 1);
2786 memcpy(str
, type
, len
);
2793 static int default_fmt_format
= 'x';
2794 static int default_fmt_size
= 4;
2798 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2799 const char *cmdline
,
2802 const char *p
, *typestr
;
2804 const mon_cmd_t
*cmd
;
2810 monitor_printf(mon
, "command='%s'\n", cmdline
);
2813 /* extract the command name */
2814 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2818 /* find the command */
2819 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2820 if (compare_cmd(cmdname
, cmd
->name
))
2824 if (cmd
->name
== NULL
) {
2825 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2829 /* parse the parameters */
2830 typestr
= cmd
->args_type
;
2832 typestr
= key_get_info(typestr
, &key
);
2844 while (qemu_isspace(*p
))
2846 if (*typestr
== '?') {
2849 /* no optional string: NULL argument */
2853 ret
= get_str(buf
, sizeof(buf
), &p
);
2857 monitor_printf(mon
, "%s: filename expected\n",
2861 monitor_printf(mon
, "%s: block device name expected\n",
2865 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2870 qdict_put(qdict
, key
, qstring_from_str(buf
));
2875 int count
, format
, size
;
2877 while (qemu_isspace(*p
))
2883 if (qemu_isdigit(*p
)) {
2885 while (qemu_isdigit(*p
)) {
2886 count
= count
* 10 + (*p
- '0');
2924 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2925 monitor_printf(mon
, "invalid char in format: '%c'\n",
2930 format
= default_fmt_format
;
2931 if (format
!= 'i') {
2932 /* for 'i', not specifying a size gives -1 as size */
2934 size
= default_fmt_size
;
2935 default_fmt_size
= size
;
2937 default_fmt_format
= format
;
2940 format
= default_fmt_format
;
2941 if (format
!= 'i') {
2942 size
= default_fmt_size
;
2947 qdict_put(qdict
, "count", qint_from_int(count
));
2948 qdict_put(qdict
, "format", qint_from_int(format
));
2949 qdict_put(qdict
, "size", qint_from_int(size
));
2957 while (qemu_isspace(*p
))
2959 if (*typestr
== '?' || *typestr
== '.') {
2960 if (*typestr
== '?') {
2968 while (qemu_isspace(*p
))
2977 if (get_expr(mon
, &val
, &p
))
2979 /* Check if 'i' is greater than 32-bit */
2980 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2981 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2982 monitor_printf(mon
, "integer is for 32-bit values\n");
2985 qdict_put(qdict
, key
, qint_from_int(val
));
2996 while (qemu_isspace(*p
))
3002 monitor_printf(mon
, "%s: unsupported option -%c\n",
3009 qdict_put(qdict
, key
, qint_from_int(has_option
));
3014 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3020 /* check that all arguments were parsed */
3021 while (qemu_isspace(*p
))
3024 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3036 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3039 const mon_cmd_t
*cmd
;
3041 qdict
= qdict_new();
3043 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3047 qemu_errors_to_mon(mon
);
3049 if (monitor_handler_ported(cmd
)) {
3050 QObject
*data
= NULL
;
3052 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3054 cmd
->user_print(mon
, data
);
3056 qobject_decref(data
);
3058 cmd
->mhandler
.cmd(mon
, qdict
);
3061 qemu_errors_to_previous();
3067 static void cmd_completion(const char *name
, const char *list
)
3069 const char *p
, *pstart
;
3078 p
= pstart
+ strlen(pstart
);
3080 if (len
> sizeof(cmd
) - 2)
3081 len
= sizeof(cmd
) - 2;
3082 memcpy(cmd
, pstart
, len
);
3084 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3085 readline_add_completion(cur_mon
->rs
, cmd
);
3093 static void file_completion(const char *input
)
3098 char file
[1024], file_prefix
[1024];
3102 p
= strrchr(input
, '/');
3105 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3106 pstrcpy(path
, sizeof(path
), ".");
3108 input_path_len
= p
- input
+ 1;
3109 memcpy(path
, input
, input_path_len
);
3110 if (input_path_len
> sizeof(path
) - 1)
3111 input_path_len
= sizeof(path
) - 1;
3112 path
[input_path_len
] = '\0';
3113 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3115 #ifdef DEBUG_COMPLETION
3116 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3117 input
, path
, file_prefix
);
3119 ffs
= opendir(path
);
3127 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3128 memcpy(file
, input
, input_path_len
);
3129 if (input_path_len
< sizeof(file
))
3130 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3132 /* stat the file to find out if it's a directory.
3133 * In that case add a slash to speed up typing long paths
3136 if(S_ISDIR(sb
.st_mode
))
3137 pstrcat(file
, sizeof(file
), "/");
3138 readline_add_completion(cur_mon
->rs
, file
);
3144 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3146 const char *name
= bdrv_get_device_name(bs
);
3147 const char *input
= opaque
;
3149 if (input
[0] == '\0' ||
3150 !strncmp(name
, (char *)input
, strlen(input
))) {
3151 readline_add_completion(cur_mon
->rs
, name
);
3155 /* NOTE: this parser is an approximate form of the real command parser */
3156 static void parse_cmdline(const char *cmdline
,
3157 int *pnb_args
, char **args
)
3166 while (qemu_isspace(*p
))
3170 if (nb_args
>= MAX_ARGS
)
3172 ret
= get_str(buf
, sizeof(buf
), &p
);
3173 args
[nb_args
] = qemu_strdup(buf
);
3178 *pnb_args
= nb_args
;
3181 static const char *next_arg_type(const char *typestr
)
3183 const char *p
= strchr(typestr
, ':');
3184 return (p
!= NULL
? ++p
: typestr
);
3187 static void monitor_find_completion(const char *cmdline
)
3189 const char *cmdname
;
3190 char *args
[MAX_ARGS
];
3191 int nb_args
, i
, len
;
3192 const char *ptype
, *str
;
3193 const mon_cmd_t
*cmd
;
3196 parse_cmdline(cmdline
, &nb_args
, args
);
3197 #ifdef DEBUG_COMPLETION
3198 for(i
= 0; i
< nb_args
; i
++) {
3199 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3203 /* if the line ends with a space, it means we want to complete the
3205 len
= strlen(cmdline
);
3206 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3207 if (nb_args
>= MAX_ARGS
)
3209 args
[nb_args
++] = qemu_strdup("");
3212 /* command completion */
3217 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3218 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3219 cmd_completion(cmdname
, cmd
->name
);
3222 /* find the command */
3223 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3224 if (compare_cmd(args
[0], cmd
->name
))
3229 ptype
= next_arg_type(cmd
->args_type
);
3230 for(i
= 0; i
< nb_args
- 2; i
++) {
3231 if (*ptype
!= '\0') {
3232 ptype
= next_arg_type(ptype
);
3233 while (*ptype
== '?')
3234 ptype
= next_arg_type(ptype
);
3237 str
= args
[nb_args
- 1];
3238 if (*ptype
== '-' && ptype
[1] != '\0') {
3243 /* file completion */
3244 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3245 file_completion(str
);
3248 /* block device name completion */
3249 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3250 bdrv_iterate(block_completion_it
, (void *)str
);
3253 /* XXX: more generic ? */
3254 if (!strcmp(cmd
->name
, "info")) {
3255 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3256 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3257 cmd_completion(str
, cmd
->name
);
3259 } else if (!strcmp(cmd
->name
, "sendkey")) {
3260 char *sep
= strrchr(str
, '-');
3263 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3264 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3265 cmd_completion(str
, key
->name
);
3267 } else if (!strcmp(cmd
->name
, "help|?")) {
3268 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3269 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3270 cmd_completion(str
, cmd
->name
);
3278 for(i
= 0; i
< nb_args
; i
++)
3282 static int monitor_can_read(void *opaque
)
3284 Monitor
*mon
= opaque
;
3286 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3289 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3291 Monitor
*old_mon
= cur_mon
;
3297 for (i
= 0; i
< size
; i
++)
3298 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3300 if (size
== 0 || buf
[size
- 1] != 0)
3301 monitor_printf(cur_mon
, "corrupted command\n");
3303 monitor_handle_command(cur_mon
, (char *)buf
);
3309 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3311 monitor_suspend(mon
);
3312 monitor_handle_command(mon
, cmdline
);
3313 monitor_resume(mon
);
3316 int monitor_suspend(Monitor
*mon
)
3324 void monitor_resume(Monitor
*mon
)
3328 if (--mon
->suspend_cnt
== 0)
3329 readline_show_prompt(mon
->rs
);
3332 static void monitor_event(void *opaque
, int event
)
3334 Monitor
*mon
= opaque
;
3337 case CHR_EVENT_MUX_IN
:
3339 if (mon
->reset_seen
) {
3340 readline_restart(mon
->rs
);
3341 monitor_resume(mon
);
3344 mon
->suspend_cnt
= 0;
3348 case CHR_EVENT_MUX_OUT
:
3349 if (mon
->reset_seen
) {
3350 if (mon
->suspend_cnt
== 0) {
3351 monitor_printf(mon
, "\n");
3354 monitor_suspend(mon
);
3361 case CHR_EVENT_RESET
:
3362 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3363 "information\n", QEMU_VERSION
);
3364 if (!mon
->mux_out
) {
3365 readline_show_prompt(mon
->rs
);
3367 mon
->reset_seen
= 1;
3381 void monitor_init(CharDriverState
*chr
, int flags
)
3383 static int is_first_init
= 1;
3386 if (is_first_init
) {
3387 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3391 mon
= qemu_mallocz(sizeof(*mon
));
3395 if (flags
& MONITOR_USE_READLINE
) {
3396 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3397 monitor_read_command(mon
, 0);
3400 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3403 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3404 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3408 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3410 BlockDriverState
*bs
= opaque
;
3413 if (bdrv_set_key(bs
, password
) != 0) {
3414 monitor_printf(mon
, "invalid password\n");
3417 if (mon
->password_completion_cb
)
3418 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3420 monitor_read_command(mon
, 1);
3423 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3424 BlockDriverCompletionFunc
*completion_cb
,
3429 if (!bdrv_key_required(bs
)) {
3431 completion_cb(opaque
, 0);
3435 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3436 bdrv_get_encrypted_filename(bs
));
3438 mon
->password_completion_cb
= completion_cb
;
3439 mon
->password_opaque
= opaque
;
3441 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3443 if (err
&& completion_cb
)
3444 completion_cb(opaque
, err
);
3447 typedef struct QemuErrorSink QemuErrorSink
;
3448 struct QemuErrorSink
{
3457 QemuErrorSink
*previous
;
3460 static QemuErrorSink
*qemu_error_sink
;
3462 void qemu_errors_to_file(FILE *fp
)
3464 QemuErrorSink
*sink
;
3466 sink
= qemu_mallocz(sizeof(*sink
));
3467 sink
->dest
= ERR_SINK_FILE
;
3469 sink
->previous
= qemu_error_sink
;
3470 qemu_error_sink
= sink
;
3473 void qemu_errors_to_mon(Monitor
*mon
)
3475 QemuErrorSink
*sink
;
3477 sink
= qemu_mallocz(sizeof(*sink
));
3478 sink
->dest
= ERR_SINK_MONITOR
;
3480 sink
->previous
= qemu_error_sink
;
3481 qemu_error_sink
= sink
;
3484 void qemu_errors_to_previous(void)
3486 QemuErrorSink
*sink
;
3488 assert(qemu_error_sink
!= NULL
);
3489 sink
= qemu_error_sink
;
3490 qemu_error_sink
= sink
->previous
;
3494 void qemu_error(const char *fmt
, ...)
3498 assert(qemu_error_sink
!= NULL
);
3499 switch (qemu_error_sink
->dest
) {
3501 va_start(args
, fmt
);
3502 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3505 case ERR_SINK_MONITOR
:
3506 va_start(args
, fmt
);
3507 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);