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);
460 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
465 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
467 if (bdrv_is_inserted(bs
)) {
469 if (!bdrv_is_removable(bs
)) {
470 monitor_printf(mon
, "device is not removable\n");
473 if (bdrv_is_locked(bs
)) {
474 monitor_printf(mon
, "device is locked\n");
483 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
485 BlockDriverState
*bs
;
486 int force
= qdict_get_int(qdict
, "force");
487 const char *filename
= qdict_get_str(qdict
, "filename");
489 bs
= bdrv_find(filename
);
491 monitor_printf(mon
, "device not found\n");
494 eject_device(mon
, bs
, force
);
497 static void do_change_block(Monitor
*mon
, const char *device
,
498 const char *filename
, const char *fmt
)
500 BlockDriverState
*bs
;
501 BlockDriver
*drv
= NULL
;
503 bs
= bdrv_find(device
);
505 monitor_printf(mon
, "device not found\n");
509 drv
= bdrv_find_format(fmt
);
511 monitor_printf(mon
, "invalid format %s\n", fmt
);
515 if (eject_device(mon
, bs
, 0) < 0)
517 bdrv_open2(bs
, filename
, 0, drv
);
518 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
521 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
524 if (vnc_display_password(NULL
, password
) < 0)
525 monitor_printf(mon
, "could not set VNC server password\n");
527 monitor_read_command(mon
, 1);
530 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
532 if (strcmp(target
, "passwd") == 0 ||
533 strcmp(target
, "password") == 0) {
536 strncpy(password
, arg
, sizeof(password
));
537 password
[sizeof(password
) - 1] = '\0';
538 change_vnc_password_cb(mon
, password
, NULL
);
540 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
543 if (vnc_display_open(NULL
, target
) < 0)
544 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
548 static void do_change(Monitor
*mon
, const QDict
*qdict
)
550 const char *device
= qdict_get_str(qdict
, "device");
551 const char *target
= qdict_get_str(qdict
, "target");
552 const char *arg
= qdict_get_try_str(qdict
, "arg");
553 if (strcmp(device
, "vnc") == 0) {
554 do_change_vnc(mon
, target
, arg
);
556 do_change_block(mon
, device
, target
, arg
);
560 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
562 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
565 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
567 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
570 static void do_log(Monitor
*mon
, const QDict
*qdict
)
573 const char *items
= qdict_get_str(qdict
, "items");
575 if (!strcmp(items
, "none")) {
578 mask
= cpu_str_to_log_mask(items
);
580 help_cmd(mon
, "log");
587 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
589 const char *option
= qdict_get_try_str(qdict
, "option");
590 if (!option
|| !strcmp(option
, "on")) {
592 } else if (!strcmp(option
, "off")) {
595 monitor_printf(mon
, "unexpected option %s\n", option
);
599 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
601 vm_stop(EXCP_INTERRUPT
);
604 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
606 struct bdrv_iterate_context
{
611 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
613 struct bdrv_iterate_context context
= { mon
, 0 };
615 bdrv_iterate(encrypted_bdrv_it
, &context
);
616 /* only resume the vm if all keys are set and valid */
621 static void bdrv_key_cb(void *opaque
, int err
)
623 Monitor
*mon
= opaque
;
625 /* another key was set successfully, retry to continue */
630 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
632 struct bdrv_iterate_context
*context
= opaque
;
634 if (!context
->err
&& bdrv_key_required(bs
)) {
635 context
->err
= -EBUSY
;
636 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
641 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
643 const char *device
= qdict_get_try_str(qdict
, "device");
645 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
646 if (gdbserver_start(device
) < 0) {
647 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
649 } else if (strcmp(device
, "none") == 0) {
650 monitor_printf(mon
, "Disabled gdbserver\n");
652 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
657 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
659 const char *action
= qdict_get_str(qdict
, "action");
660 if (select_watchdog_action(action
) == -1) {
661 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
665 static void monitor_printc(Monitor
*mon
, int c
)
667 monitor_printf(mon
, "'");
670 monitor_printf(mon
, "\\'");
673 monitor_printf(mon
, "\\\\");
676 monitor_printf(mon
, "\\n");
679 monitor_printf(mon
, "\\r");
682 if (c
>= 32 && c
<= 126) {
683 monitor_printf(mon
, "%c", c
);
685 monitor_printf(mon
, "\\x%02x", c
);
689 monitor_printf(mon
, "'");
692 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
693 target_phys_addr_t addr
, int is_physical
)
696 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
704 if (!env
&& !is_physical
)
709 } else if (wsize
== 4) {
712 /* as default we use the current CS size */
716 if ((env
->efer
& MSR_EFER_LMA
) &&
717 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
721 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
726 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
735 nb_per_line
= line_size
/ wsize
;
740 max_digits
= (wsize
* 8 + 2) / 3;
744 max_digits
= (wsize
* 8) / 4;
748 max_digits
= (wsize
* 8 * 10 + 32) / 33;
757 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
759 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
764 cpu_physical_memory_rw(addr
, buf
, l
, 0);
769 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
770 monitor_printf(mon
, " Cannot access memory\n");
779 v
= ldub_raw(buf
+ i
);
782 v
= lduw_raw(buf
+ i
);
785 v
= (uint32_t)ldl_raw(buf
+ i
);
788 v
= ldq_raw(buf
+ i
);
791 monitor_printf(mon
, " ");
794 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
797 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
800 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
803 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
806 monitor_printc(mon
, v
);
811 monitor_printf(mon
, "\n");
817 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
819 int count
= qdict_get_int(qdict
, "count");
820 int format
= qdict_get_int(qdict
, "format");
821 int size
= qdict_get_int(qdict
, "size");
822 target_long addr
= qdict_get_int(qdict
, "addr");
824 memory_dump(mon
, count
, format
, size
, addr
, 0);
827 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
829 int count
= qdict_get_int(qdict
, "count");
830 int format
= qdict_get_int(qdict
, "format");
831 int size
= qdict_get_int(qdict
, "size");
832 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
834 memory_dump(mon
, count
, format
, size
, addr
, 1);
837 static void do_print(Monitor
*mon
, const QDict
*qdict
)
839 int format
= qdict_get_int(qdict
, "format");
840 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
842 #if TARGET_PHYS_ADDR_BITS == 32
845 monitor_printf(mon
, "%#o", val
);
848 monitor_printf(mon
, "%#x", val
);
851 monitor_printf(mon
, "%u", val
);
855 monitor_printf(mon
, "%d", val
);
858 monitor_printc(mon
, val
);
864 monitor_printf(mon
, "%#" PRIo64
, val
);
867 monitor_printf(mon
, "%#" PRIx64
, val
);
870 monitor_printf(mon
, "%" PRIu64
, val
);
874 monitor_printf(mon
, "%" PRId64
, val
);
877 monitor_printc(mon
, val
);
881 monitor_printf(mon
, "\n");
884 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
887 uint32_t size
= qdict_get_int(qdict
, "size");
888 const char *filename
= qdict_get_str(qdict
, "filename");
889 target_long addr
= qdict_get_int(qdict
, "val");
898 f
= fopen(filename
, "wb");
900 monitor_printf(mon
, "could not open '%s'\n", filename
);
907 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
908 fwrite(buf
, 1, l
, f
);
915 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
920 uint32_t size
= qdict_get_int(qdict
, "size");
921 const char *filename
= qdict_get_str(qdict
, "filename");
922 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
924 f
= fopen(filename
, "wb");
926 monitor_printf(mon
, "could not open '%s'\n", filename
);
933 cpu_physical_memory_rw(addr
, buf
, l
, 0);
934 fwrite(buf
, 1, l
, f
);
942 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
947 uint32_t start
= qdict_get_int(qdict
, "start");
948 uint32_t size
= qdict_get_int(qdict
, "size");
951 for(addr
= start
; addr
< (start
+ size
); addr
++) {
952 cpu_physical_memory_rw(addr
, buf
, 1, 0);
953 /* BSD sum algorithm ('sum' Unix command) */
954 sum
= (sum
>> 1) | (sum
<< 15);
957 monitor_printf(mon
, "%05d\n", sum
);
965 static const KeyDef key_defs
[] = {
992 { 0x0e, "backspace" },
1029 { 0x37, "asterisk" },
1032 { 0x3a, "caps_lock" },
1043 { 0x45, "num_lock" },
1044 { 0x46, "scroll_lock" },
1046 { 0xb5, "kp_divide" },
1047 { 0x37, "kp_multiply" },
1048 { 0x4a, "kp_subtract" },
1050 { 0x9c, "kp_enter" },
1051 { 0x53, "kp_decimal" },
1084 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1099 { 0xfe, "compose" },
1104 static int get_keycode(const char *key
)
1110 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1111 if (!strcmp(key
, p
->name
))
1114 if (strstart(key
, "0x", NULL
)) {
1115 ret
= strtoul(key
, &endp
, 0);
1116 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1122 #define MAX_KEYCODES 16
1123 static uint8_t keycodes
[MAX_KEYCODES
];
1124 static int nb_pending_keycodes
;
1125 static QEMUTimer
*key_timer
;
1127 static void release_keys(void *opaque
)
1131 while (nb_pending_keycodes
> 0) {
1132 nb_pending_keycodes
--;
1133 keycode
= keycodes
[nb_pending_keycodes
];
1135 kbd_put_keycode(0xe0);
1136 kbd_put_keycode(keycode
| 0x80);
1140 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1142 char keyname_buf
[16];
1144 int keyname_len
, keycode
, i
;
1145 const char *string
= qdict_get_str(qdict
, "string");
1146 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1147 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1149 if (nb_pending_keycodes
> 0) {
1150 qemu_del_timer(key_timer
);
1157 separator
= strchr(string
, '-');
1158 keyname_len
= separator
? separator
- string
: strlen(string
);
1159 if (keyname_len
> 0) {
1160 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1161 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1162 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1165 if (i
== MAX_KEYCODES
) {
1166 monitor_printf(mon
, "too many keys\n");
1169 keyname_buf
[keyname_len
] = 0;
1170 keycode
= get_keycode(keyname_buf
);
1172 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1175 keycodes
[i
++] = keycode
;
1179 string
= separator
+ 1;
1181 nb_pending_keycodes
= i
;
1182 /* key down events */
1183 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1184 keycode
= keycodes
[i
];
1186 kbd_put_keycode(0xe0);
1187 kbd_put_keycode(keycode
& 0x7f);
1189 /* delayed key up events */
1190 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1191 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1194 static int mouse_button_state
;
1196 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1199 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1200 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1201 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1202 dx
= strtol(dx_str
, NULL
, 0);
1203 dy
= strtol(dy_str
, NULL
, 0);
1206 dz
= strtol(dz_str
, NULL
, 0);
1207 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1210 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1212 int button_state
= qdict_get_int(qdict
, "button_state");
1213 mouse_button_state
= button_state
;
1214 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1217 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1219 int size
= qdict_get_int(qdict
, "size");
1220 int addr
= qdict_get_int(qdict
, "addr");
1221 int has_index
= qdict_haskey(qdict
, "index");
1226 int index
= qdict_get_int(qdict
, "index");
1227 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1235 val
= cpu_inb(addr
);
1239 val
= cpu_inw(addr
);
1243 val
= cpu_inl(addr
);
1247 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1248 suffix
, addr
, size
* 2, val
);
1251 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1253 int size
= qdict_get_int(qdict
, "size");
1254 int addr
= qdict_get_int(qdict
, "addr");
1255 int val
= qdict_get_int(qdict
, "val");
1257 addr
&= IOPORTS_MASK
;
1262 cpu_outb(addr
, val
);
1265 cpu_outw(addr
, val
);
1268 cpu_outl(addr
, val
);
1273 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1276 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1278 res
= qemu_boot_set(bootdevice
);
1280 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1281 } else if (res
> 0) {
1282 monitor_printf(mon
, "setting boot device list failed\n");
1284 monitor_printf(mon
, "no function defined to set boot device list for "
1285 "this architecture\n");
1289 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1291 qemu_system_reset_request();
1294 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1296 qemu_system_powerdown_request();
1299 #if defined(TARGET_I386)
1300 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1302 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1305 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1306 pte
& PG_PSE_MASK
? 'P' : '-',
1307 pte
& PG_DIRTY_MASK
? 'D' : '-',
1308 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1309 pte
& PG_PCD_MASK
? 'C' : '-',
1310 pte
& PG_PWT_MASK
? 'T' : '-',
1311 pte
& PG_USER_MASK
? 'U' : '-',
1312 pte
& PG_RW_MASK
? 'W' : '-');
1315 static void tlb_info(Monitor
*mon
)
1319 uint32_t pgd
, pde
, pte
;
1321 env
= mon_get_cpu();
1325 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1326 monitor_printf(mon
, "PG disabled\n");
1329 pgd
= env
->cr
[3] & ~0xfff;
1330 for(l1
= 0; l1
< 1024; l1
++) {
1331 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1332 pde
= le32_to_cpu(pde
);
1333 if (pde
& PG_PRESENT_MASK
) {
1334 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1335 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1337 for(l2
= 0; l2
< 1024; l2
++) {
1338 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1339 (uint8_t *)&pte
, 4);
1340 pte
= le32_to_cpu(pte
);
1341 if (pte
& PG_PRESENT_MASK
) {
1342 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1352 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1353 uint32_t end
, int prot
)
1356 prot1
= *plast_prot
;
1357 if (prot
!= prot1
) {
1358 if (*pstart
!= -1) {
1359 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1360 *pstart
, end
, end
- *pstart
,
1361 prot1
& PG_USER_MASK
? 'u' : '-',
1363 prot1
& PG_RW_MASK
? 'w' : '-');
1373 static void mem_info(Monitor
*mon
)
1376 int l1
, l2
, prot
, last_prot
;
1377 uint32_t pgd
, pde
, pte
, start
, end
;
1379 env
= mon_get_cpu();
1383 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1384 monitor_printf(mon
, "PG disabled\n");
1387 pgd
= env
->cr
[3] & ~0xfff;
1390 for(l1
= 0; l1
< 1024; l1
++) {
1391 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1392 pde
= le32_to_cpu(pde
);
1394 if (pde
& PG_PRESENT_MASK
) {
1395 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1396 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1397 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1399 for(l2
= 0; l2
< 1024; l2
++) {
1400 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1401 (uint8_t *)&pte
, 4);
1402 pte
= le32_to_cpu(pte
);
1403 end
= (l1
<< 22) + (l2
<< 12);
1404 if (pte
& PG_PRESENT_MASK
) {
1405 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1409 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1414 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1420 #if defined(TARGET_SH4)
1422 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1424 monitor_printf(mon
, " tlb%i:\t"
1425 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1426 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1427 "dirty=%hhu writethrough=%hhu\n",
1429 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1430 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1434 static void tlb_info(Monitor
*mon
)
1436 CPUState
*env
= mon_get_cpu();
1439 monitor_printf (mon
, "ITLB:\n");
1440 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1441 print_tlb (mon
, i
, &env
->itlb
[i
]);
1442 monitor_printf (mon
, "UTLB:\n");
1443 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1444 print_tlb (mon
, i
, &env
->utlb
[i
]);
1449 static void do_info_kvm(Monitor
*mon
)
1452 monitor_printf(mon
, "kvm support: ");
1454 monitor_printf(mon
, "enabled\n");
1456 monitor_printf(mon
, "disabled\n");
1458 monitor_printf(mon
, "kvm support: not compiled\n");
1462 static void do_info_numa(Monitor
*mon
)
1467 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1468 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1469 monitor_printf(mon
, "node %d cpus:", i
);
1470 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1471 if (env
->numa_node
== i
) {
1472 monitor_printf(mon
, " %d", env
->cpu_index
);
1475 monitor_printf(mon
, "\n");
1476 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1481 #ifdef CONFIG_PROFILER
1486 static void do_info_profile(Monitor
*mon
)
1492 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1493 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1494 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1495 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1500 static void do_info_profile(Monitor
*mon
)
1502 monitor_printf(mon
, "Internal profiler not compiled\n");
1506 /* Capture support */
1507 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1509 static void do_info_capture(Monitor
*mon
)
1514 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1515 monitor_printf(mon
, "[%d]: ", i
);
1516 s
->ops
.info (s
->opaque
);
1521 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1524 int n
= qdict_get_int(qdict
, "n");
1527 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1529 s
->ops
.destroy (s
->opaque
);
1530 QLIST_REMOVE (s
, entries
);
1537 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1539 const char *path
= qdict_get_str(qdict
, "path");
1540 int has_freq
= qdict_haskey(qdict
, "freq");
1541 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1542 int has_bits
= qdict_haskey(qdict
, "bits");
1543 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1544 int has_channels
= qdict_haskey(qdict
, "nchannels");
1545 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1548 s
= qemu_mallocz (sizeof (*s
));
1550 freq
= has_freq
? freq
: 44100;
1551 bits
= has_bits
? bits
: 16;
1552 nchannels
= has_channels
? nchannels
: 2;
1554 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1555 monitor_printf(mon
, "Faied to add wave capture\n");
1558 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1562 #if defined(TARGET_I386)
1563 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1566 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1568 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1569 if (env
->cpu_index
== cpu_index
) {
1570 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1576 static void do_info_status(Monitor
*mon
)
1580 monitor_printf(mon
, "VM status: running (single step mode)\n");
1582 monitor_printf(mon
, "VM status: running\n");
1585 monitor_printf(mon
, "VM status: paused\n");
1589 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1591 int value
= qdict_get_int(qdict
, "value");
1592 ram_addr_t target
= value
;
1593 qemu_balloon(target
<< 20);
1596 static void do_info_balloon(Monitor
*mon
)
1600 actual
= qemu_balloon_status();
1601 if (kvm_enabled() && !kvm_has_sync_mmu())
1602 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1603 "ballooning disabled\n");
1604 else if (actual
== 0)
1605 monitor_printf(mon
, "Ballooning not activated in VM\n");
1607 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1610 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1612 qemu_acl
*acl
= qemu_acl_find(name
);
1615 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1620 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1622 const char *aclname
= qdict_get_str(qdict
, "aclname");
1623 qemu_acl
*acl
= find_acl(mon
, aclname
);
1624 qemu_acl_entry
*entry
;
1628 monitor_printf(mon
, "policy: %s\n",
1629 acl
->defaultDeny
? "deny" : "allow");
1630 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1632 monitor_printf(mon
, "%d: %s %s\n", i
,
1633 entry
->deny
? "deny" : "allow", entry
->match
);
1638 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1640 const char *aclname
= qdict_get_str(qdict
, "aclname");
1641 qemu_acl
*acl
= find_acl(mon
, aclname
);
1644 qemu_acl_reset(acl
);
1645 monitor_printf(mon
, "acl: removed all rules\n");
1649 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1651 const char *aclname
= qdict_get_str(qdict
, "aclname");
1652 const char *policy
= qdict_get_str(qdict
, "policy");
1653 qemu_acl
*acl
= find_acl(mon
, aclname
);
1656 if (strcmp(policy
, "allow") == 0) {
1657 acl
->defaultDeny
= 0;
1658 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1659 } else if (strcmp(policy
, "deny") == 0) {
1660 acl
->defaultDeny
= 1;
1661 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1663 monitor_printf(mon
, "acl: unknown policy '%s', "
1664 "expected 'deny' or 'allow'\n", policy
);
1669 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1671 const char *aclname
= qdict_get_str(qdict
, "aclname");
1672 const char *match
= qdict_get_str(qdict
, "match");
1673 const char *policy
= qdict_get_str(qdict
, "policy");
1674 int has_index
= qdict_haskey(qdict
, "index");
1675 int index
= qdict_get_try_int(qdict
, "index", -1);
1676 qemu_acl
*acl
= find_acl(mon
, aclname
);
1680 if (strcmp(policy
, "allow") == 0) {
1682 } else if (strcmp(policy
, "deny") == 0) {
1685 monitor_printf(mon
, "acl: unknown policy '%s', "
1686 "expected 'deny' or 'allow'\n", policy
);
1690 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1692 ret
= qemu_acl_append(acl
, deny
, match
);
1694 monitor_printf(mon
, "acl: unable to add acl entry\n");
1696 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1700 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1702 const char *aclname
= qdict_get_str(qdict
, "aclname");
1703 const char *match
= qdict_get_str(qdict
, "match");
1704 qemu_acl
*acl
= find_acl(mon
, aclname
);
1708 ret
= qemu_acl_remove(acl
, match
);
1710 monitor_printf(mon
, "acl: no matching acl entry\n");
1712 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1716 #if defined(TARGET_I386)
1717 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1720 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1721 int bank
= qdict_get_int(qdict
, "bank");
1722 uint64_t status
= qdict_get_int(qdict
, "status");
1723 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1724 uint64_t addr
= qdict_get_int(qdict
, "addr");
1725 uint64_t misc
= qdict_get_int(qdict
, "misc");
1727 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1728 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1729 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1735 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1737 const char *fdname
= qdict_get_str(qdict
, "fdname");
1741 fd
= qemu_chr_get_msgfd(mon
->chr
);
1743 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1747 if (qemu_isdigit(fdname
[0])) {
1748 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1754 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1759 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1760 if (strcmp(monfd
->name
, fdname
) != 0) {
1769 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1770 monfd
->name
= qemu_strdup(fdname
);
1773 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1776 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1778 const char *fdname
= qdict_get_str(qdict
, "fdname");
1781 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1782 if (strcmp(monfd
->name
, fdname
) != 0) {
1786 QLIST_REMOVE(monfd
, next
);
1788 qemu_free(monfd
->name
);
1793 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1797 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1799 int saved_vm_running
= vm_running
;
1800 const char *name
= qdict_get_str(qdict
, "name");
1804 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1808 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1812 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1815 if (strcmp(monfd
->name
, fdname
) != 0) {
1821 /* caller takes ownership of fd */
1822 QLIST_REMOVE(monfd
, next
);
1823 qemu_free(monfd
->name
);
1832 static const mon_cmd_t mon_cmds
[] = {
1833 #include "qemu-monitor.h"
1837 /* Please update qemu-monitor.hx when adding or changing commands */
1838 static const mon_cmd_t info_cmds
[] = {
1843 .help
= "show the version of QEMU",
1844 .mhandler
.info
= do_info_version
,
1850 .help
= "show the network state",
1851 .mhandler
.info
= do_info_network
,
1857 .help
= "show the character devices",
1858 .mhandler
.info
= qemu_chr_info
,
1864 .help
= "show the block devices",
1865 .mhandler
.info
= bdrv_info
,
1868 .name
= "blockstats",
1871 .help
= "show block device statistics",
1872 .mhandler
.info
= bdrv_info_stats
,
1875 .name
= "registers",
1878 .help
= "show the cpu registers",
1879 .mhandler
.info
= do_info_registers
,
1885 .help
= "show infos for each CPU",
1886 .mhandler
.info
= do_info_cpus
,
1892 .help
= "show the command line history",
1893 .mhandler
.info
= do_info_history
,
1899 .help
= "show the interrupts statistics (if available)",
1900 .mhandler
.info
= irq_info
,
1906 .help
= "show i8259 (PIC) state",
1907 .mhandler
.info
= pic_info
,
1913 .help
= "show PCI info",
1914 .mhandler
.info
= pci_info
,
1916 #if defined(TARGET_I386) || defined(TARGET_SH4)
1921 .help
= "show virtual to physical memory mappings",
1922 .mhandler
.info
= tlb_info
,
1925 #if defined(TARGET_I386)
1930 .help
= "show the active virtual memory mappings",
1931 .mhandler
.info
= mem_info
,
1937 .help
= "show state of HPET",
1938 .mhandler
.info
= do_info_hpet
,
1945 .help
= "show dynamic compiler info",
1946 .mhandler
.info
= do_info_jit
,
1952 .help
= "show KVM information",
1953 .mhandler
.info
= do_info_kvm
,
1959 .help
= "show NUMA information",
1960 .mhandler
.info
= do_info_numa
,
1966 .help
= "show guest USB devices",
1967 .mhandler
.info
= usb_info
,
1973 .help
= "show host USB devices",
1974 .mhandler
.info
= usb_host_info
,
1980 .help
= "show profiling information",
1981 .mhandler
.info
= do_info_profile
,
1987 .help
= "show capture information",
1988 .mhandler
.info
= do_info_capture
,
1991 .name
= "snapshots",
1994 .help
= "show the currently saved VM snapshots",
1995 .mhandler
.info
= do_info_snapshots
,
2001 .help
= "show the current VM status (running|paused)",
2002 .mhandler
.info
= do_info_status
,
2008 .help
= "show guest PCMCIA status",
2009 .mhandler
.info
= pcmcia_info
,
2015 .help
= "show which guest mouse is receiving events",
2016 .mhandler
.info
= do_info_mice
,
2022 .help
= "show the vnc server status",
2023 .mhandler
.info
= do_info_vnc
,
2029 .help
= "show the current VM name",
2030 .mhandler
.info
= do_info_name
,
2036 .help
= "show the current VM UUID",
2037 .mhandler
.info
= do_info_uuid
,
2039 #if defined(TARGET_PPC)
2044 .help
= "show CPU statistics",
2045 .mhandler
.info
= do_info_cpu_stats
,
2048 #if defined(CONFIG_SLIRP)
2053 .help
= "show user network stack connection states",
2054 .mhandler
.info
= do_info_usernet
,
2061 .help
= "show migration status",
2062 .mhandler
.info
= do_info_migrate
,
2068 .help
= "show balloon information",
2069 .mhandler
.info
= do_info_balloon
,
2075 .help
= "show device tree",
2076 .mhandler
.info
= do_info_qtree
,
2082 .help
= "show qdev device model list",
2083 .mhandler
.info
= do_info_qdm
,
2089 .help
= "show roms",
2090 .mhandler
.info
= do_info_roms
,
2097 /*******************************************************************/
2099 static const char *pch
;
2100 static jmp_buf expr_env
;
2105 typedef struct MonitorDef
{
2108 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2112 #if defined(TARGET_I386)
2113 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2115 CPUState
*env
= mon_get_cpu();
2118 return env
->eip
+ env
->segs
[R_CS
].base
;
2122 #if defined(TARGET_PPC)
2123 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2125 CPUState
*env
= mon_get_cpu();
2133 for (i
= 0; i
< 8; i
++)
2134 u
|= env
->crf
[i
] << (32 - (4 * i
));
2139 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2141 CPUState
*env
= mon_get_cpu();
2147 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2149 CPUState
*env
= mon_get_cpu();
2155 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2157 CPUState
*env
= mon_get_cpu();
2160 return cpu_ppc_load_decr(env
);
2163 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2165 CPUState
*env
= mon_get_cpu();
2168 return cpu_ppc_load_tbu(env
);
2171 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2173 CPUState
*env
= mon_get_cpu();
2176 return cpu_ppc_load_tbl(env
);
2180 #if defined(TARGET_SPARC)
2181 #ifndef TARGET_SPARC64
2182 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2184 CPUState
*env
= mon_get_cpu();
2187 return GET_PSR(env
);
2191 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2193 CPUState
*env
= mon_get_cpu();
2196 return env
->regwptr
[val
];
2200 static const MonitorDef monitor_defs
[] = {
2203 #define SEG(name, seg) \
2204 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2205 { name ".base", offsetof(CPUState, segs[seg].base) },\
2206 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2208 { "eax", offsetof(CPUState
, regs
[0]) },
2209 { "ecx", offsetof(CPUState
, regs
[1]) },
2210 { "edx", offsetof(CPUState
, regs
[2]) },
2211 { "ebx", offsetof(CPUState
, regs
[3]) },
2212 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2213 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2214 { "esi", offsetof(CPUState
, regs
[6]) },
2215 { "edi", offsetof(CPUState
, regs
[7]) },
2216 #ifdef TARGET_X86_64
2217 { "r8", offsetof(CPUState
, regs
[8]) },
2218 { "r9", offsetof(CPUState
, regs
[9]) },
2219 { "r10", offsetof(CPUState
, regs
[10]) },
2220 { "r11", offsetof(CPUState
, regs
[11]) },
2221 { "r12", offsetof(CPUState
, regs
[12]) },
2222 { "r13", offsetof(CPUState
, regs
[13]) },
2223 { "r14", offsetof(CPUState
, regs
[14]) },
2224 { "r15", offsetof(CPUState
, regs
[15]) },
2226 { "eflags", offsetof(CPUState
, eflags
) },
2227 { "eip", offsetof(CPUState
, eip
) },
2234 { "pc", 0, monitor_get_pc
, },
2235 #elif defined(TARGET_PPC)
2236 /* General purpose registers */
2237 { "r0", offsetof(CPUState
, gpr
[0]) },
2238 { "r1", offsetof(CPUState
, gpr
[1]) },
2239 { "r2", offsetof(CPUState
, gpr
[2]) },
2240 { "r3", offsetof(CPUState
, gpr
[3]) },
2241 { "r4", offsetof(CPUState
, gpr
[4]) },
2242 { "r5", offsetof(CPUState
, gpr
[5]) },
2243 { "r6", offsetof(CPUState
, gpr
[6]) },
2244 { "r7", offsetof(CPUState
, gpr
[7]) },
2245 { "r8", offsetof(CPUState
, gpr
[8]) },
2246 { "r9", offsetof(CPUState
, gpr
[9]) },
2247 { "r10", offsetof(CPUState
, gpr
[10]) },
2248 { "r11", offsetof(CPUState
, gpr
[11]) },
2249 { "r12", offsetof(CPUState
, gpr
[12]) },
2250 { "r13", offsetof(CPUState
, gpr
[13]) },
2251 { "r14", offsetof(CPUState
, gpr
[14]) },
2252 { "r15", offsetof(CPUState
, gpr
[15]) },
2253 { "r16", offsetof(CPUState
, gpr
[16]) },
2254 { "r17", offsetof(CPUState
, gpr
[17]) },
2255 { "r18", offsetof(CPUState
, gpr
[18]) },
2256 { "r19", offsetof(CPUState
, gpr
[19]) },
2257 { "r20", offsetof(CPUState
, gpr
[20]) },
2258 { "r21", offsetof(CPUState
, gpr
[21]) },
2259 { "r22", offsetof(CPUState
, gpr
[22]) },
2260 { "r23", offsetof(CPUState
, gpr
[23]) },
2261 { "r24", offsetof(CPUState
, gpr
[24]) },
2262 { "r25", offsetof(CPUState
, gpr
[25]) },
2263 { "r26", offsetof(CPUState
, gpr
[26]) },
2264 { "r27", offsetof(CPUState
, gpr
[27]) },
2265 { "r28", offsetof(CPUState
, gpr
[28]) },
2266 { "r29", offsetof(CPUState
, gpr
[29]) },
2267 { "r30", offsetof(CPUState
, gpr
[30]) },
2268 { "r31", offsetof(CPUState
, gpr
[31]) },
2269 /* Floating point registers */
2270 { "f0", offsetof(CPUState
, fpr
[0]) },
2271 { "f1", offsetof(CPUState
, fpr
[1]) },
2272 { "f2", offsetof(CPUState
, fpr
[2]) },
2273 { "f3", offsetof(CPUState
, fpr
[3]) },
2274 { "f4", offsetof(CPUState
, fpr
[4]) },
2275 { "f5", offsetof(CPUState
, fpr
[5]) },
2276 { "f6", offsetof(CPUState
, fpr
[6]) },
2277 { "f7", offsetof(CPUState
, fpr
[7]) },
2278 { "f8", offsetof(CPUState
, fpr
[8]) },
2279 { "f9", offsetof(CPUState
, fpr
[9]) },
2280 { "f10", offsetof(CPUState
, fpr
[10]) },
2281 { "f11", offsetof(CPUState
, fpr
[11]) },
2282 { "f12", offsetof(CPUState
, fpr
[12]) },
2283 { "f13", offsetof(CPUState
, fpr
[13]) },
2284 { "f14", offsetof(CPUState
, fpr
[14]) },
2285 { "f15", offsetof(CPUState
, fpr
[15]) },
2286 { "f16", offsetof(CPUState
, fpr
[16]) },
2287 { "f17", offsetof(CPUState
, fpr
[17]) },
2288 { "f18", offsetof(CPUState
, fpr
[18]) },
2289 { "f19", offsetof(CPUState
, fpr
[19]) },
2290 { "f20", offsetof(CPUState
, fpr
[20]) },
2291 { "f21", offsetof(CPUState
, fpr
[21]) },
2292 { "f22", offsetof(CPUState
, fpr
[22]) },
2293 { "f23", offsetof(CPUState
, fpr
[23]) },
2294 { "f24", offsetof(CPUState
, fpr
[24]) },
2295 { "f25", offsetof(CPUState
, fpr
[25]) },
2296 { "f26", offsetof(CPUState
, fpr
[26]) },
2297 { "f27", offsetof(CPUState
, fpr
[27]) },
2298 { "f28", offsetof(CPUState
, fpr
[28]) },
2299 { "f29", offsetof(CPUState
, fpr
[29]) },
2300 { "f30", offsetof(CPUState
, fpr
[30]) },
2301 { "f31", offsetof(CPUState
, fpr
[31]) },
2302 { "fpscr", offsetof(CPUState
, fpscr
) },
2303 /* Next instruction pointer */
2304 { "nip|pc", offsetof(CPUState
, nip
) },
2305 { "lr", offsetof(CPUState
, lr
) },
2306 { "ctr", offsetof(CPUState
, ctr
) },
2307 { "decr", 0, &monitor_get_decr
, },
2308 { "ccr", 0, &monitor_get_ccr
, },
2309 /* Machine state register */
2310 { "msr", 0, &monitor_get_msr
, },
2311 { "xer", 0, &monitor_get_xer
, },
2312 { "tbu", 0, &monitor_get_tbu
, },
2313 { "tbl", 0, &monitor_get_tbl
, },
2314 #if defined(TARGET_PPC64)
2315 /* Address space register */
2316 { "asr", offsetof(CPUState
, asr
) },
2318 /* Segment registers */
2319 { "sdr1", offsetof(CPUState
, sdr1
) },
2320 { "sr0", offsetof(CPUState
, sr
[0]) },
2321 { "sr1", offsetof(CPUState
, sr
[1]) },
2322 { "sr2", offsetof(CPUState
, sr
[2]) },
2323 { "sr3", offsetof(CPUState
, sr
[3]) },
2324 { "sr4", offsetof(CPUState
, sr
[4]) },
2325 { "sr5", offsetof(CPUState
, sr
[5]) },
2326 { "sr6", offsetof(CPUState
, sr
[6]) },
2327 { "sr7", offsetof(CPUState
, sr
[7]) },
2328 { "sr8", offsetof(CPUState
, sr
[8]) },
2329 { "sr9", offsetof(CPUState
, sr
[9]) },
2330 { "sr10", offsetof(CPUState
, sr
[10]) },
2331 { "sr11", offsetof(CPUState
, sr
[11]) },
2332 { "sr12", offsetof(CPUState
, sr
[12]) },
2333 { "sr13", offsetof(CPUState
, sr
[13]) },
2334 { "sr14", offsetof(CPUState
, sr
[14]) },
2335 { "sr15", offsetof(CPUState
, sr
[15]) },
2336 /* Too lazy to put BATs and SPRs ... */
2337 #elif defined(TARGET_SPARC)
2338 { "g0", offsetof(CPUState
, gregs
[0]) },
2339 { "g1", offsetof(CPUState
, gregs
[1]) },
2340 { "g2", offsetof(CPUState
, gregs
[2]) },
2341 { "g3", offsetof(CPUState
, gregs
[3]) },
2342 { "g4", offsetof(CPUState
, gregs
[4]) },
2343 { "g5", offsetof(CPUState
, gregs
[5]) },
2344 { "g6", offsetof(CPUState
, gregs
[6]) },
2345 { "g7", offsetof(CPUState
, gregs
[7]) },
2346 { "o0", 0, monitor_get_reg
},
2347 { "o1", 1, monitor_get_reg
},
2348 { "o2", 2, monitor_get_reg
},
2349 { "o3", 3, monitor_get_reg
},
2350 { "o4", 4, monitor_get_reg
},
2351 { "o5", 5, monitor_get_reg
},
2352 { "o6", 6, monitor_get_reg
},
2353 { "o7", 7, monitor_get_reg
},
2354 { "l0", 8, monitor_get_reg
},
2355 { "l1", 9, monitor_get_reg
},
2356 { "l2", 10, monitor_get_reg
},
2357 { "l3", 11, monitor_get_reg
},
2358 { "l4", 12, monitor_get_reg
},
2359 { "l5", 13, monitor_get_reg
},
2360 { "l6", 14, monitor_get_reg
},
2361 { "l7", 15, monitor_get_reg
},
2362 { "i0", 16, monitor_get_reg
},
2363 { "i1", 17, monitor_get_reg
},
2364 { "i2", 18, monitor_get_reg
},
2365 { "i3", 19, monitor_get_reg
},
2366 { "i4", 20, monitor_get_reg
},
2367 { "i5", 21, monitor_get_reg
},
2368 { "i6", 22, monitor_get_reg
},
2369 { "i7", 23, monitor_get_reg
},
2370 { "pc", offsetof(CPUState
, pc
) },
2371 { "npc", offsetof(CPUState
, npc
) },
2372 { "y", offsetof(CPUState
, y
) },
2373 #ifndef TARGET_SPARC64
2374 { "psr", 0, &monitor_get_psr
, },
2375 { "wim", offsetof(CPUState
, wim
) },
2377 { "tbr", offsetof(CPUState
, tbr
) },
2378 { "fsr", offsetof(CPUState
, fsr
) },
2379 { "f0", offsetof(CPUState
, fpr
[0]) },
2380 { "f1", offsetof(CPUState
, fpr
[1]) },
2381 { "f2", offsetof(CPUState
, fpr
[2]) },
2382 { "f3", offsetof(CPUState
, fpr
[3]) },
2383 { "f4", offsetof(CPUState
, fpr
[4]) },
2384 { "f5", offsetof(CPUState
, fpr
[5]) },
2385 { "f6", offsetof(CPUState
, fpr
[6]) },
2386 { "f7", offsetof(CPUState
, fpr
[7]) },
2387 { "f8", offsetof(CPUState
, fpr
[8]) },
2388 { "f9", offsetof(CPUState
, fpr
[9]) },
2389 { "f10", offsetof(CPUState
, fpr
[10]) },
2390 { "f11", offsetof(CPUState
, fpr
[11]) },
2391 { "f12", offsetof(CPUState
, fpr
[12]) },
2392 { "f13", offsetof(CPUState
, fpr
[13]) },
2393 { "f14", offsetof(CPUState
, fpr
[14]) },
2394 { "f15", offsetof(CPUState
, fpr
[15]) },
2395 { "f16", offsetof(CPUState
, fpr
[16]) },
2396 { "f17", offsetof(CPUState
, fpr
[17]) },
2397 { "f18", offsetof(CPUState
, fpr
[18]) },
2398 { "f19", offsetof(CPUState
, fpr
[19]) },
2399 { "f20", offsetof(CPUState
, fpr
[20]) },
2400 { "f21", offsetof(CPUState
, fpr
[21]) },
2401 { "f22", offsetof(CPUState
, fpr
[22]) },
2402 { "f23", offsetof(CPUState
, fpr
[23]) },
2403 { "f24", offsetof(CPUState
, fpr
[24]) },
2404 { "f25", offsetof(CPUState
, fpr
[25]) },
2405 { "f26", offsetof(CPUState
, fpr
[26]) },
2406 { "f27", offsetof(CPUState
, fpr
[27]) },
2407 { "f28", offsetof(CPUState
, fpr
[28]) },
2408 { "f29", offsetof(CPUState
, fpr
[29]) },
2409 { "f30", offsetof(CPUState
, fpr
[30]) },
2410 { "f31", offsetof(CPUState
, fpr
[31]) },
2411 #ifdef TARGET_SPARC64
2412 { "f32", offsetof(CPUState
, fpr
[32]) },
2413 { "f34", offsetof(CPUState
, fpr
[34]) },
2414 { "f36", offsetof(CPUState
, fpr
[36]) },
2415 { "f38", offsetof(CPUState
, fpr
[38]) },
2416 { "f40", offsetof(CPUState
, fpr
[40]) },
2417 { "f42", offsetof(CPUState
, fpr
[42]) },
2418 { "f44", offsetof(CPUState
, fpr
[44]) },
2419 { "f46", offsetof(CPUState
, fpr
[46]) },
2420 { "f48", offsetof(CPUState
, fpr
[48]) },
2421 { "f50", offsetof(CPUState
, fpr
[50]) },
2422 { "f52", offsetof(CPUState
, fpr
[52]) },
2423 { "f54", offsetof(CPUState
, fpr
[54]) },
2424 { "f56", offsetof(CPUState
, fpr
[56]) },
2425 { "f58", offsetof(CPUState
, fpr
[58]) },
2426 { "f60", offsetof(CPUState
, fpr
[60]) },
2427 { "f62", offsetof(CPUState
, fpr
[62]) },
2428 { "asi", offsetof(CPUState
, asi
) },
2429 { "pstate", offsetof(CPUState
, pstate
) },
2430 { "cansave", offsetof(CPUState
, cansave
) },
2431 { "canrestore", offsetof(CPUState
, canrestore
) },
2432 { "otherwin", offsetof(CPUState
, otherwin
) },
2433 { "wstate", offsetof(CPUState
, wstate
) },
2434 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2435 { "fprs", offsetof(CPUState
, fprs
) },
2441 static void expr_error(Monitor
*mon
, const char *msg
)
2443 monitor_printf(mon
, "%s\n", msg
);
2444 longjmp(expr_env
, 1);
2447 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2448 static int get_monitor_def(target_long
*pval
, const char *name
)
2450 const MonitorDef
*md
;
2453 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2454 if (compare_cmd(name
, md
->name
)) {
2455 if (md
->get_value
) {
2456 *pval
= md
->get_value(md
, md
->offset
);
2458 CPUState
*env
= mon_get_cpu();
2461 ptr
= (uint8_t *)env
+ md
->offset
;
2464 *pval
= *(int32_t *)ptr
;
2467 *pval
= *(target_long
*)ptr
;
2480 static void next(void)
2484 while (qemu_isspace(*pch
))
2489 static int64_t expr_sum(Monitor
*mon
);
2491 static int64_t expr_unary(Monitor
*mon
)
2500 n
= expr_unary(mon
);
2504 n
= -expr_unary(mon
);
2508 n
= ~expr_unary(mon
);
2514 expr_error(mon
, "')' expected");
2521 expr_error(mon
, "character constant expected");
2525 expr_error(mon
, "missing terminating \' character");
2535 while ((*pch
>= 'a' && *pch
<= 'z') ||
2536 (*pch
>= 'A' && *pch
<= 'Z') ||
2537 (*pch
>= '0' && *pch
<= '9') ||
2538 *pch
== '_' || *pch
== '.') {
2539 if ((q
- buf
) < sizeof(buf
) - 1)
2543 while (qemu_isspace(*pch
))
2546 ret
= get_monitor_def(®
, buf
);
2548 expr_error(mon
, "unknown register");
2550 expr_error(mon
, "no cpu defined");
2555 expr_error(mon
, "unexpected end of expression");
2559 #if TARGET_PHYS_ADDR_BITS > 32
2560 n
= strtoull(pch
, &p
, 0);
2562 n
= strtoul(pch
, &p
, 0);
2565 expr_error(mon
, "invalid char in expression");
2568 while (qemu_isspace(*pch
))
2576 static int64_t expr_prod(Monitor
*mon
)
2581 val
= expr_unary(mon
);
2584 if (op
!= '*' && op
!= '/' && op
!= '%')
2587 val2
= expr_unary(mon
);
2596 expr_error(mon
, "division by zero");
2607 static int64_t expr_logic(Monitor
*mon
)
2612 val
= expr_prod(mon
);
2615 if (op
!= '&' && op
!= '|' && op
!= '^')
2618 val2
= expr_prod(mon
);
2635 static int64_t expr_sum(Monitor
*mon
)
2640 val
= expr_logic(mon
);
2643 if (op
!= '+' && op
!= '-')
2646 val2
= expr_logic(mon
);
2655 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2658 if (setjmp(expr_env
)) {
2662 while (qemu_isspace(*pch
))
2664 *pval
= expr_sum(mon
);
2669 static int get_str(char *buf
, int buf_size
, const char **pp
)
2677 while (qemu_isspace(*p
))
2687 while (*p
!= '\0' && *p
!= '\"') {
2703 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2706 if ((q
- buf
) < buf_size
- 1) {
2710 if ((q
- buf
) < buf_size
- 1) {
2717 qemu_printf("unterminated string\n");
2722 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2723 if ((q
- buf
) < buf_size
- 1) {
2735 * Store the command-name in cmdname, and return a pointer to
2736 * the remaining of the command string.
2738 static const char *get_command_name(const char *cmdline
,
2739 char *cmdname
, size_t nlen
)
2742 const char *p
, *pstart
;
2745 while (qemu_isspace(*p
))
2750 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2755 memcpy(cmdname
, pstart
, len
);
2756 cmdname
[len
] = '\0';
2761 * Read key of 'type' into 'key' and return the current
2764 static char *key_get_info(const char *type
, char **key
)
2772 p
= strchr(type
, ':');
2779 str
= qemu_malloc(len
+ 1);
2780 memcpy(str
, type
, len
);
2787 static int default_fmt_format
= 'x';
2788 static int default_fmt_size
= 4;
2792 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2793 const char *cmdline
,
2796 const char *p
, *typestr
;
2798 const mon_cmd_t
*cmd
;
2804 monitor_printf(mon
, "command='%s'\n", cmdline
);
2807 /* extract the command name */
2808 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2812 /* find the command */
2813 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2814 if (compare_cmd(cmdname
, cmd
->name
))
2818 if (cmd
->name
== NULL
) {
2819 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2823 /* parse the parameters */
2824 typestr
= cmd
->args_type
;
2826 typestr
= key_get_info(typestr
, &key
);
2838 while (qemu_isspace(*p
))
2840 if (*typestr
== '?') {
2843 /* no optional string: NULL argument */
2847 ret
= get_str(buf
, sizeof(buf
), &p
);
2851 monitor_printf(mon
, "%s: filename expected\n",
2855 monitor_printf(mon
, "%s: block device name expected\n",
2859 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2864 qdict_put(qdict
, key
, qstring_from_str(buf
));
2869 int count
, format
, size
;
2871 while (qemu_isspace(*p
))
2877 if (qemu_isdigit(*p
)) {
2879 while (qemu_isdigit(*p
)) {
2880 count
= count
* 10 + (*p
- '0');
2918 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2919 monitor_printf(mon
, "invalid char in format: '%c'\n",
2924 format
= default_fmt_format
;
2925 if (format
!= 'i') {
2926 /* for 'i', not specifying a size gives -1 as size */
2928 size
= default_fmt_size
;
2929 default_fmt_size
= size
;
2931 default_fmt_format
= format
;
2934 format
= default_fmt_format
;
2935 if (format
!= 'i') {
2936 size
= default_fmt_size
;
2941 qdict_put(qdict
, "count", qint_from_int(count
));
2942 qdict_put(qdict
, "format", qint_from_int(format
));
2943 qdict_put(qdict
, "size", qint_from_int(size
));
2951 while (qemu_isspace(*p
))
2953 if (*typestr
== '?' || *typestr
== '.') {
2954 if (*typestr
== '?') {
2962 while (qemu_isspace(*p
))
2971 if (get_expr(mon
, &val
, &p
))
2973 /* Check if 'i' is greater than 32-bit */
2974 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2975 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2976 monitor_printf(mon
, "integer is for 32-bit values\n");
2979 qdict_put(qdict
, key
, qint_from_int(val
));
2990 while (qemu_isspace(*p
))
2996 monitor_printf(mon
, "%s: unsupported option -%c\n",
3003 qdict_put(qdict
, key
, qint_from_int(has_option
));
3008 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3014 /* check that all arguments were parsed */
3015 while (qemu_isspace(*p
))
3018 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3030 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3033 const mon_cmd_t
*cmd
;
3035 qdict
= qdict_new();
3037 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3041 qemu_errors_to_mon(mon
);
3043 if (monitor_handler_ported(cmd
)) {
3044 QObject
*data
= NULL
;
3046 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3048 cmd
->user_print(mon
, data
);
3050 qobject_decref(data
);
3052 cmd
->mhandler
.cmd(mon
, qdict
);
3055 qemu_errors_to_previous();
3061 static void cmd_completion(const char *name
, const char *list
)
3063 const char *p
, *pstart
;
3072 p
= pstart
+ strlen(pstart
);
3074 if (len
> sizeof(cmd
) - 2)
3075 len
= sizeof(cmd
) - 2;
3076 memcpy(cmd
, pstart
, len
);
3078 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3079 readline_add_completion(cur_mon
->rs
, cmd
);
3087 static void file_completion(const char *input
)
3092 char file
[1024], file_prefix
[1024];
3096 p
= strrchr(input
, '/');
3099 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3100 pstrcpy(path
, sizeof(path
), ".");
3102 input_path_len
= p
- input
+ 1;
3103 memcpy(path
, input
, input_path_len
);
3104 if (input_path_len
> sizeof(path
) - 1)
3105 input_path_len
= sizeof(path
) - 1;
3106 path
[input_path_len
] = '\0';
3107 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3109 #ifdef DEBUG_COMPLETION
3110 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3111 input
, path
, file_prefix
);
3113 ffs
= opendir(path
);
3121 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3122 memcpy(file
, input
, input_path_len
);
3123 if (input_path_len
< sizeof(file
))
3124 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3126 /* stat the file to find out if it's a directory.
3127 * In that case add a slash to speed up typing long paths
3130 if(S_ISDIR(sb
.st_mode
))
3131 pstrcat(file
, sizeof(file
), "/");
3132 readline_add_completion(cur_mon
->rs
, file
);
3138 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3140 const char *name
= bdrv_get_device_name(bs
);
3141 const char *input
= opaque
;
3143 if (input
[0] == '\0' ||
3144 !strncmp(name
, (char *)input
, strlen(input
))) {
3145 readline_add_completion(cur_mon
->rs
, name
);
3149 /* NOTE: this parser is an approximate form of the real command parser */
3150 static void parse_cmdline(const char *cmdline
,
3151 int *pnb_args
, char **args
)
3160 while (qemu_isspace(*p
))
3164 if (nb_args
>= MAX_ARGS
)
3166 ret
= get_str(buf
, sizeof(buf
), &p
);
3167 args
[nb_args
] = qemu_strdup(buf
);
3172 *pnb_args
= nb_args
;
3175 static const char *next_arg_type(const char *typestr
)
3177 const char *p
= strchr(typestr
, ':');
3178 return (p
!= NULL
? ++p
: typestr
);
3181 static void monitor_find_completion(const char *cmdline
)
3183 const char *cmdname
;
3184 char *args
[MAX_ARGS
];
3185 int nb_args
, i
, len
;
3186 const char *ptype
, *str
;
3187 const mon_cmd_t
*cmd
;
3190 parse_cmdline(cmdline
, &nb_args
, args
);
3191 #ifdef DEBUG_COMPLETION
3192 for(i
= 0; i
< nb_args
; i
++) {
3193 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3197 /* if the line ends with a space, it means we want to complete the
3199 len
= strlen(cmdline
);
3200 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3201 if (nb_args
>= MAX_ARGS
)
3203 args
[nb_args
++] = qemu_strdup("");
3206 /* command completion */
3211 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3212 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3213 cmd_completion(cmdname
, cmd
->name
);
3216 /* find the command */
3217 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3218 if (compare_cmd(args
[0], cmd
->name
))
3223 ptype
= next_arg_type(cmd
->args_type
);
3224 for(i
= 0; i
< nb_args
- 2; i
++) {
3225 if (*ptype
!= '\0') {
3226 ptype
= next_arg_type(ptype
);
3227 while (*ptype
== '?')
3228 ptype
= next_arg_type(ptype
);
3231 str
= args
[nb_args
- 1];
3232 if (*ptype
== '-' && ptype
[1] != '\0') {
3237 /* file completion */
3238 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3239 file_completion(str
);
3242 /* block device name completion */
3243 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3244 bdrv_iterate(block_completion_it
, (void *)str
);
3247 /* XXX: more generic ? */
3248 if (!strcmp(cmd
->name
, "info")) {
3249 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3250 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3251 cmd_completion(str
, cmd
->name
);
3253 } else if (!strcmp(cmd
->name
, "sendkey")) {
3254 char *sep
= strrchr(str
, '-');
3257 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3258 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3259 cmd_completion(str
, key
->name
);
3261 } else if (!strcmp(cmd
->name
, "help|?")) {
3262 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3263 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3264 cmd_completion(str
, cmd
->name
);
3272 for(i
= 0; i
< nb_args
; i
++)
3276 static int monitor_can_read(void *opaque
)
3278 Monitor
*mon
= opaque
;
3280 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3283 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3285 Monitor
*old_mon
= cur_mon
;
3291 for (i
= 0; i
< size
; i
++)
3292 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3294 if (size
== 0 || buf
[size
- 1] != 0)
3295 monitor_printf(cur_mon
, "corrupted command\n");
3297 monitor_handle_command(cur_mon
, (char *)buf
);
3303 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3305 monitor_suspend(mon
);
3306 monitor_handle_command(mon
, cmdline
);
3307 monitor_resume(mon
);
3310 int monitor_suspend(Monitor
*mon
)
3318 void monitor_resume(Monitor
*mon
)
3322 if (--mon
->suspend_cnt
== 0)
3323 readline_show_prompt(mon
->rs
);
3326 static void monitor_event(void *opaque
, int event
)
3328 Monitor
*mon
= opaque
;
3331 case CHR_EVENT_MUX_IN
:
3333 if (mon
->reset_seen
) {
3334 readline_restart(mon
->rs
);
3335 monitor_resume(mon
);
3338 mon
->suspend_cnt
= 0;
3342 case CHR_EVENT_MUX_OUT
:
3343 if (mon
->reset_seen
) {
3344 if (mon
->suspend_cnt
== 0) {
3345 monitor_printf(mon
, "\n");
3348 monitor_suspend(mon
);
3355 case CHR_EVENT_RESET
:
3356 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3357 "information\n", QEMU_VERSION
);
3358 if (!mon
->mux_out
) {
3359 readline_show_prompt(mon
->rs
);
3361 mon
->reset_seen
= 1;
3375 void monitor_init(CharDriverState
*chr
, int flags
)
3377 static int is_first_init
= 1;
3380 if (is_first_init
) {
3381 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3385 mon
= qemu_mallocz(sizeof(*mon
));
3389 if (flags
& MONITOR_USE_READLINE
) {
3390 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3391 monitor_read_command(mon
, 0);
3394 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3397 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3398 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3402 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3404 BlockDriverState
*bs
= opaque
;
3407 if (bdrv_set_key(bs
, password
) != 0) {
3408 monitor_printf(mon
, "invalid password\n");
3411 if (mon
->password_completion_cb
)
3412 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3414 monitor_read_command(mon
, 1);
3417 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3418 BlockDriverCompletionFunc
*completion_cb
,
3423 if (!bdrv_key_required(bs
)) {
3425 completion_cb(opaque
, 0);
3429 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3430 bdrv_get_encrypted_filename(bs
));
3432 mon
->password_completion_cb
= completion_cb
;
3433 mon
->password_opaque
= opaque
;
3435 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3437 if (err
&& completion_cb
)
3438 completion_cb(opaque
, err
);
3441 typedef struct QemuErrorSink QemuErrorSink
;
3442 struct QemuErrorSink
{
3451 QemuErrorSink
*previous
;
3454 static QemuErrorSink
*qemu_error_sink
;
3456 void qemu_errors_to_file(FILE *fp
)
3458 QemuErrorSink
*sink
;
3460 sink
= qemu_mallocz(sizeof(*sink
));
3461 sink
->dest
= ERR_SINK_FILE
;
3463 sink
->previous
= qemu_error_sink
;
3464 qemu_error_sink
= sink
;
3467 void qemu_errors_to_mon(Monitor
*mon
)
3469 QemuErrorSink
*sink
;
3471 sink
= qemu_mallocz(sizeof(*sink
));
3472 sink
->dest
= ERR_SINK_MONITOR
;
3474 sink
->previous
= qemu_error_sink
;
3475 qemu_error_sink
= sink
;
3478 void qemu_errors_to_previous(void)
3480 QemuErrorSink
*sink
;
3482 assert(qemu_error_sink
!= NULL
);
3483 sink
= qemu_error_sink
;
3484 qemu_error_sink
= sink
->previous
;
3488 void qemu_error(const char *fmt
, ...)
3492 assert(qemu_error_sink
!= NULL
);
3493 switch (qemu_error_sink
->dest
) {
3495 va_start(args
, fmt
);
3496 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3499 case ERR_SINK_MONITOR
:
3500 va_start(args
, fmt
);
3501 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);