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 void monitor_print_qobject(Monitor
*mon
, const QObject
*data
)
228 switch (qobject_type(data
)) {
230 monitor_printf(mon
, "%s",qstring_get_str(qobject_to_qstring(data
)));
233 monitor_printf(mon
, "%" PRId64
,qint_get_int(qobject_to_qint(data
)));
236 monitor_printf(mon
, "ERROR: unsupported type: %d",
241 monitor_puts(mon
, "\n");
244 static int compare_cmd(const char *name
, const char *list
)
246 const char *p
, *pstart
;
254 p
= pstart
+ strlen(pstart
);
255 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
264 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
265 const char *prefix
, const char *name
)
267 const mon_cmd_t
*cmd
;
269 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
270 if (!name
|| !strcmp(name
, cmd
->name
))
271 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
272 cmd
->params
, cmd
->help
);
276 static void help_cmd(Monitor
*mon
, const char *name
)
278 if (name
&& !strcmp(name
, "info")) {
279 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
281 help_cmd_dump(mon
, mon_cmds
, "", name
);
282 if (name
&& !strcmp(name
, "log")) {
283 const CPULogItem
*item
;
284 monitor_printf(mon
, "Log items (comma separated):\n");
285 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
286 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
287 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
293 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
295 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
298 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
302 const char *device
= qdict_get_str(qdict
, "device");
304 all_devices
= !strcmp(device
, "all");
305 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
307 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
309 bdrv_commit(dinfo
->bdrv
);
313 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
315 const mon_cmd_t
*cmd
;
316 const char *item
= qdict_get_try_str(qdict
, "item");
321 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
322 if (compare_cmd(item
, cmd
->name
))
326 if (cmd
->name
== NULL
)
329 if (monitor_handler_ported(cmd
)) {
330 cmd
->mhandler
.info_new(mon
, ret_data
);
332 cmd
->user_print(mon
, *ret_data
);
334 cmd
->mhandler
.info(mon
);
340 help_cmd(mon
, "info");
344 * do_info_version(): Show QEMU version
346 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
348 *ret_data
= QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION
));
351 static void do_info_name(Monitor
*mon
)
354 monitor_printf(mon
, "%s\n", qemu_name
);
357 #if defined(TARGET_I386)
358 static void do_info_hpet(Monitor
*mon
)
360 monitor_printf(mon
, "HPET is %s by QEMU\n",
361 (no_hpet
) ? "disabled" : "enabled");
365 static void do_info_uuid(Monitor
*mon
)
367 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
368 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
369 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
370 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
371 qemu_uuid
[14], qemu_uuid
[15]);
374 /* get the current CPU defined by the user */
375 static int mon_set_cpu(int cpu_index
)
379 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
380 if (env
->cpu_index
== cpu_index
) {
381 cur_mon
->mon_cpu
= env
;
388 static CPUState
*mon_get_cpu(void)
390 if (!cur_mon
->mon_cpu
) {
393 cpu_synchronize_state(cur_mon
->mon_cpu
);
394 return cur_mon
->mon_cpu
;
397 static void do_info_registers(Monitor
*mon
)
404 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
407 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
412 static void do_info_cpus(Monitor
*mon
)
416 /* just to set the default cpu if not already done */
419 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
420 cpu_synchronize_state(env
);
421 monitor_printf(mon
, "%c CPU #%d:",
422 (env
== mon
->mon_cpu
) ? '*' : ' ',
424 #if defined(TARGET_I386)
425 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
426 env
->eip
+ env
->segs
[R_CS
].base
);
427 #elif defined(TARGET_PPC)
428 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
429 #elif defined(TARGET_SPARC)
430 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
432 #elif defined(TARGET_MIPS)
433 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
436 monitor_printf(mon
, " (halted)");
437 monitor_printf(mon
, "\n");
441 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
443 int index
= qdict_get_int(qdict
, "index");
444 if (mon_set_cpu(index
) < 0)
445 monitor_printf(mon
, "Invalid CPU index\n");
448 static void do_info_jit(Monitor
*mon
)
450 dump_exec_info((FILE *)mon
, monitor_fprintf
);
453 static void do_info_history(Monitor
*mon
)
462 str
= readline_get_history(mon
->rs
, i
);
465 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
470 #if defined(TARGET_PPC)
471 /* XXX: not implemented in other targets */
472 static void do_info_cpu_stats(Monitor
*mon
)
477 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
482 * do_quit(): Quit QEMU execution
484 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
489 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
491 if (bdrv_is_inserted(bs
)) {
493 if (!bdrv_is_removable(bs
)) {
494 monitor_printf(mon
, "device is not removable\n");
497 if (bdrv_is_locked(bs
)) {
498 monitor_printf(mon
, "device is locked\n");
507 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
509 BlockDriverState
*bs
;
510 int force
= qdict_get_int(qdict
, "force");
511 const char *filename
= qdict_get_str(qdict
, "filename");
513 bs
= bdrv_find(filename
);
515 monitor_printf(mon
, "device not found\n");
518 eject_device(mon
, bs
, force
);
521 static void do_change_block(Monitor
*mon
, const char *device
,
522 const char *filename
, const char *fmt
)
524 BlockDriverState
*bs
;
525 BlockDriver
*drv
= NULL
;
527 bs
= bdrv_find(device
);
529 monitor_printf(mon
, "device not found\n");
533 drv
= bdrv_find_format(fmt
);
535 monitor_printf(mon
, "invalid format %s\n", fmt
);
539 if (eject_device(mon
, bs
, 0) < 0)
541 bdrv_open2(bs
, filename
, 0, drv
);
542 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
545 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
548 if (vnc_display_password(NULL
, password
) < 0)
549 monitor_printf(mon
, "could not set VNC server password\n");
551 monitor_read_command(mon
, 1);
554 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
556 if (strcmp(target
, "passwd") == 0 ||
557 strcmp(target
, "password") == 0) {
560 strncpy(password
, arg
, sizeof(password
));
561 password
[sizeof(password
) - 1] = '\0';
562 change_vnc_password_cb(mon
, password
, NULL
);
564 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
567 if (vnc_display_open(NULL
, target
) < 0)
568 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
572 static void do_change(Monitor
*mon
, const QDict
*qdict
)
574 const char *device
= qdict_get_str(qdict
, "device");
575 const char *target
= qdict_get_str(qdict
, "target");
576 const char *arg
= qdict_get_try_str(qdict
, "arg");
577 if (strcmp(device
, "vnc") == 0) {
578 do_change_vnc(mon
, target
, arg
);
580 do_change_block(mon
, device
, target
, arg
);
584 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
586 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
589 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
591 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
594 static void do_log(Monitor
*mon
, const QDict
*qdict
)
597 const char *items
= qdict_get_str(qdict
, "items");
599 if (!strcmp(items
, "none")) {
602 mask
= cpu_str_to_log_mask(items
);
604 help_cmd(mon
, "log");
611 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
613 const char *option
= qdict_get_try_str(qdict
, "option");
614 if (!option
|| !strcmp(option
, "on")) {
616 } else if (!strcmp(option
, "off")) {
619 monitor_printf(mon
, "unexpected option %s\n", option
);
624 * do_stop(): Stop VM execution
626 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
628 vm_stop(EXCP_INTERRUPT
);
631 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
633 struct bdrv_iterate_context
{
639 * do_cont(): Resume emulation.
641 static void do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
643 struct bdrv_iterate_context context
= { mon
, 0 };
645 bdrv_iterate(encrypted_bdrv_it
, &context
);
646 /* only resume the vm if all keys are set and valid */
651 static void bdrv_key_cb(void *opaque
, int err
)
653 Monitor
*mon
= opaque
;
655 /* another key was set successfully, retry to continue */
657 do_cont(mon
, NULL
, NULL
);
660 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
662 struct bdrv_iterate_context
*context
= opaque
;
664 if (!context
->err
&& bdrv_key_required(bs
)) {
665 context
->err
= -EBUSY
;
666 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
671 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
673 const char *device
= qdict_get_try_str(qdict
, "device");
675 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
676 if (gdbserver_start(device
) < 0) {
677 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
679 } else if (strcmp(device
, "none") == 0) {
680 monitor_printf(mon
, "Disabled gdbserver\n");
682 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
687 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
689 const char *action
= qdict_get_str(qdict
, "action");
690 if (select_watchdog_action(action
) == -1) {
691 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
695 static void monitor_printc(Monitor
*mon
, int c
)
697 monitor_printf(mon
, "'");
700 monitor_printf(mon
, "\\'");
703 monitor_printf(mon
, "\\\\");
706 monitor_printf(mon
, "\\n");
709 monitor_printf(mon
, "\\r");
712 if (c
>= 32 && c
<= 126) {
713 monitor_printf(mon
, "%c", c
);
715 monitor_printf(mon
, "\\x%02x", c
);
719 monitor_printf(mon
, "'");
722 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
723 target_phys_addr_t addr
, int is_physical
)
726 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
734 if (!env
&& !is_physical
)
739 } else if (wsize
== 4) {
742 /* as default we use the current CS size */
746 if ((env
->efer
& MSR_EFER_LMA
) &&
747 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
751 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
756 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
765 nb_per_line
= line_size
/ wsize
;
770 max_digits
= (wsize
* 8 + 2) / 3;
774 max_digits
= (wsize
* 8) / 4;
778 max_digits
= (wsize
* 8 * 10 + 32) / 33;
787 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
789 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
794 cpu_physical_memory_rw(addr
, buf
, l
, 0);
799 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
800 monitor_printf(mon
, " Cannot access memory\n");
809 v
= ldub_raw(buf
+ i
);
812 v
= lduw_raw(buf
+ i
);
815 v
= (uint32_t)ldl_raw(buf
+ i
);
818 v
= ldq_raw(buf
+ i
);
821 monitor_printf(mon
, " ");
824 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
827 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
830 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
833 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
836 monitor_printc(mon
, v
);
841 monitor_printf(mon
, "\n");
847 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
849 int count
= qdict_get_int(qdict
, "count");
850 int format
= qdict_get_int(qdict
, "format");
851 int size
= qdict_get_int(qdict
, "size");
852 target_long addr
= qdict_get_int(qdict
, "addr");
854 memory_dump(mon
, count
, format
, size
, addr
, 0);
857 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
859 int count
= qdict_get_int(qdict
, "count");
860 int format
= qdict_get_int(qdict
, "format");
861 int size
= qdict_get_int(qdict
, "size");
862 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
864 memory_dump(mon
, count
, format
, size
, addr
, 1);
867 static void do_print(Monitor
*mon
, const QDict
*qdict
)
869 int format
= qdict_get_int(qdict
, "format");
870 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
872 #if TARGET_PHYS_ADDR_BITS == 32
875 monitor_printf(mon
, "%#o", val
);
878 monitor_printf(mon
, "%#x", val
);
881 monitor_printf(mon
, "%u", val
);
885 monitor_printf(mon
, "%d", val
);
888 monitor_printc(mon
, val
);
894 monitor_printf(mon
, "%#" PRIo64
, val
);
897 monitor_printf(mon
, "%#" PRIx64
, val
);
900 monitor_printf(mon
, "%" PRIu64
, val
);
904 monitor_printf(mon
, "%" PRId64
, val
);
907 monitor_printc(mon
, val
);
911 monitor_printf(mon
, "\n");
914 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
917 uint32_t size
= qdict_get_int(qdict
, "size");
918 const char *filename
= qdict_get_str(qdict
, "filename");
919 target_long addr
= qdict_get_int(qdict
, "val");
928 f
= fopen(filename
, "wb");
930 monitor_printf(mon
, "could not open '%s'\n", filename
);
937 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
938 fwrite(buf
, 1, l
, f
);
945 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
950 uint32_t size
= qdict_get_int(qdict
, "size");
951 const char *filename
= qdict_get_str(qdict
, "filename");
952 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
954 f
= fopen(filename
, "wb");
956 monitor_printf(mon
, "could not open '%s'\n", filename
);
963 cpu_physical_memory_rw(addr
, buf
, l
, 0);
964 fwrite(buf
, 1, l
, f
);
972 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
977 uint32_t start
= qdict_get_int(qdict
, "start");
978 uint32_t size
= qdict_get_int(qdict
, "size");
981 for(addr
= start
; addr
< (start
+ size
); addr
++) {
982 cpu_physical_memory_rw(addr
, buf
, 1, 0);
983 /* BSD sum algorithm ('sum' Unix command) */
984 sum
= (sum
>> 1) | (sum
<< 15);
987 monitor_printf(mon
, "%05d\n", sum
);
995 static const KeyDef key_defs
[] = {
1002 { 0xe4, "altgr_r" },
1022 { 0x0e, "backspace" },
1059 { 0x37, "asterisk" },
1062 { 0x3a, "caps_lock" },
1073 { 0x45, "num_lock" },
1074 { 0x46, "scroll_lock" },
1076 { 0xb5, "kp_divide" },
1077 { 0x37, "kp_multiply" },
1078 { 0x4a, "kp_subtract" },
1080 { 0x9c, "kp_enter" },
1081 { 0x53, "kp_decimal" },
1114 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1129 { 0xfe, "compose" },
1134 static int get_keycode(const char *key
)
1140 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1141 if (!strcmp(key
, p
->name
))
1144 if (strstart(key
, "0x", NULL
)) {
1145 ret
= strtoul(key
, &endp
, 0);
1146 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1152 #define MAX_KEYCODES 16
1153 static uint8_t keycodes
[MAX_KEYCODES
];
1154 static int nb_pending_keycodes
;
1155 static QEMUTimer
*key_timer
;
1157 static void release_keys(void *opaque
)
1161 while (nb_pending_keycodes
> 0) {
1162 nb_pending_keycodes
--;
1163 keycode
= keycodes
[nb_pending_keycodes
];
1165 kbd_put_keycode(0xe0);
1166 kbd_put_keycode(keycode
| 0x80);
1170 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1172 char keyname_buf
[16];
1174 int keyname_len
, keycode
, i
;
1175 const char *string
= qdict_get_str(qdict
, "string");
1176 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1177 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1179 if (nb_pending_keycodes
> 0) {
1180 qemu_del_timer(key_timer
);
1187 separator
= strchr(string
, '-');
1188 keyname_len
= separator
? separator
- string
: strlen(string
);
1189 if (keyname_len
> 0) {
1190 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1191 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1192 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1195 if (i
== MAX_KEYCODES
) {
1196 monitor_printf(mon
, "too many keys\n");
1199 keyname_buf
[keyname_len
] = 0;
1200 keycode
= get_keycode(keyname_buf
);
1202 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1205 keycodes
[i
++] = keycode
;
1209 string
= separator
+ 1;
1211 nb_pending_keycodes
= i
;
1212 /* key down events */
1213 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1214 keycode
= keycodes
[i
];
1216 kbd_put_keycode(0xe0);
1217 kbd_put_keycode(keycode
& 0x7f);
1219 /* delayed key up events */
1220 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1221 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1224 static int mouse_button_state
;
1226 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1229 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1230 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1231 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1232 dx
= strtol(dx_str
, NULL
, 0);
1233 dy
= strtol(dy_str
, NULL
, 0);
1236 dz
= strtol(dz_str
, NULL
, 0);
1237 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1240 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1242 int button_state
= qdict_get_int(qdict
, "button_state");
1243 mouse_button_state
= button_state
;
1244 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1247 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1249 int size
= qdict_get_int(qdict
, "size");
1250 int addr
= qdict_get_int(qdict
, "addr");
1251 int has_index
= qdict_haskey(qdict
, "index");
1256 int index
= qdict_get_int(qdict
, "index");
1257 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1265 val
= cpu_inb(addr
);
1269 val
= cpu_inw(addr
);
1273 val
= cpu_inl(addr
);
1277 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1278 suffix
, addr
, size
* 2, val
);
1281 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1283 int size
= qdict_get_int(qdict
, "size");
1284 int addr
= qdict_get_int(qdict
, "addr");
1285 int val
= qdict_get_int(qdict
, "val");
1287 addr
&= IOPORTS_MASK
;
1292 cpu_outb(addr
, val
);
1295 cpu_outw(addr
, val
);
1298 cpu_outl(addr
, val
);
1303 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1306 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1308 res
= qemu_boot_set(bootdevice
);
1310 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1311 } else if (res
> 0) {
1312 monitor_printf(mon
, "setting boot device list failed\n");
1314 monitor_printf(mon
, "no function defined to set boot device list for "
1315 "this architecture\n");
1320 * do_system_reset(): Issue a machine reset
1322 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1325 qemu_system_reset_request();
1329 * do_system_powerdown(): Issue a machine powerdown
1331 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1334 qemu_system_powerdown_request();
1337 #if defined(TARGET_I386)
1338 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1340 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1343 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1344 pte
& PG_PSE_MASK
? 'P' : '-',
1345 pte
& PG_DIRTY_MASK
? 'D' : '-',
1346 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1347 pte
& PG_PCD_MASK
? 'C' : '-',
1348 pte
& PG_PWT_MASK
? 'T' : '-',
1349 pte
& PG_USER_MASK
? 'U' : '-',
1350 pte
& PG_RW_MASK
? 'W' : '-');
1353 static void tlb_info(Monitor
*mon
)
1357 uint32_t pgd
, pde
, pte
;
1359 env
= mon_get_cpu();
1363 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1364 monitor_printf(mon
, "PG disabled\n");
1367 pgd
= env
->cr
[3] & ~0xfff;
1368 for(l1
= 0; l1
< 1024; l1
++) {
1369 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1370 pde
= le32_to_cpu(pde
);
1371 if (pde
& PG_PRESENT_MASK
) {
1372 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1373 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1375 for(l2
= 0; l2
< 1024; l2
++) {
1376 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1377 (uint8_t *)&pte
, 4);
1378 pte
= le32_to_cpu(pte
);
1379 if (pte
& PG_PRESENT_MASK
) {
1380 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1390 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1391 uint32_t end
, int prot
)
1394 prot1
= *plast_prot
;
1395 if (prot
!= prot1
) {
1396 if (*pstart
!= -1) {
1397 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1398 *pstart
, end
, end
- *pstart
,
1399 prot1
& PG_USER_MASK
? 'u' : '-',
1401 prot1
& PG_RW_MASK
? 'w' : '-');
1411 static void mem_info(Monitor
*mon
)
1414 int l1
, l2
, prot
, last_prot
;
1415 uint32_t pgd
, pde
, pte
, start
, end
;
1417 env
= mon_get_cpu();
1421 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1422 monitor_printf(mon
, "PG disabled\n");
1425 pgd
= env
->cr
[3] & ~0xfff;
1428 for(l1
= 0; l1
< 1024; l1
++) {
1429 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1430 pde
= le32_to_cpu(pde
);
1432 if (pde
& PG_PRESENT_MASK
) {
1433 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1434 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1435 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1437 for(l2
= 0; l2
< 1024; l2
++) {
1438 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1439 (uint8_t *)&pte
, 4);
1440 pte
= le32_to_cpu(pte
);
1441 end
= (l1
<< 22) + (l2
<< 12);
1442 if (pte
& PG_PRESENT_MASK
) {
1443 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1447 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1452 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1458 #if defined(TARGET_SH4)
1460 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1462 monitor_printf(mon
, " tlb%i:\t"
1463 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1464 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1465 "dirty=%hhu writethrough=%hhu\n",
1467 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1468 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1472 static void tlb_info(Monitor
*mon
)
1474 CPUState
*env
= mon_get_cpu();
1477 monitor_printf (mon
, "ITLB:\n");
1478 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1479 print_tlb (mon
, i
, &env
->itlb
[i
]);
1480 monitor_printf (mon
, "UTLB:\n");
1481 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1482 print_tlb (mon
, i
, &env
->utlb
[i
]);
1487 static void do_info_kvm(Monitor
*mon
)
1490 monitor_printf(mon
, "kvm support: ");
1492 monitor_printf(mon
, "enabled\n");
1494 monitor_printf(mon
, "disabled\n");
1496 monitor_printf(mon
, "kvm support: not compiled\n");
1500 static void do_info_numa(Monitor
*mon
)
1505 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1506 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1507 monitor_printf(mon
, "node %d cpus:", i
);
1508 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1509 if (env
->numa_node
== i
) {
1510 monitor_printf(mon
, " %d", env
->cpu_index
);
1513 monitor_printf(mon
, "\n");
1514 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1519 #ifdef CONFIG_PROFILER
1524 static void do_info_profile(Monitor
*mon
)
1530 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1531 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1532 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1533 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1538 static void do_info_profile(Monitor
*mon
)
1540 monitor_printf(mon
, "Internal profiler not compiled\n");
1544 /* Capture support */
1545 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1547 static void do_info_capture(Monitor
*mon
)
1552 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1553 monitor_printf(mon
, "[%d]: ", i
);
1554 s
->ops
.info (s
->opaque
);
1559 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1562 int n
= qdict_get_int(qdict
, "n");
1565 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1567 s
->ops
.destroy (s
->opaque
);
1568 QLIST_REMOVE (s
, entries
);
1575 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1577 const char *path
= qdict_get_str(qdict
, "path");
1578 int has_freq
= qdict_haskey(qdict
, "freq");
1579 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1580 int has_bits
= qdict_haskey(qdict
, "bits");
1581 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1582 int has_channels
= qdict_haskey(qdict
, "nchannels");
1583 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1586 s
= qemu_mallocz (sizeof (*s
));
1588 freq
= has_freq
? freq
: 44100;
1589 bits
= has_bits
? bits
: 16;
1590 nchannels
= has_channels
? nchannels
: 2;
1592 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1593 monitor_printf(mon
, "Faied to add wave capture\n");
1596 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1600 #if defined(TARGET_I386)
1601 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1604 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1606 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1607 if (env
->cpu_index
== cpu_index
) {
1608 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1614 static void do_info_status(Monitor
*mon
)
1618 monitor_printf(mon
, "VM status: running (single step mode)\n");
1620 monitor_printf(mon
, "VM status: running\n");
1623 monitor_printf(mon
, "VM status: paused\n");
1627 * do_balloon(): Request VM to change its memory allocation
1629 static void do_balloon(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1631 int value
= qdict_get_int(qdict
, "value");
1632 ram_addr_t target
= value
;
1633 qemu_balloon(target
<< 20);
1636 static void do_info_balloon(Monitor
*mon
)
1640 actual
= qemu_balloon_status();
1641 if (kvm_enabled() && !kvm_has_sync_mmu())
1642 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1643 "ballooning disabled\n");
1644 else if (actual
== 0)
1645 monitor_printf(mon
, "Ballooning not activated in VM\n");
1647 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1650 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1652 qemu_acl
*acl
= qemu_acl_find(name
);
1655 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1660 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1662 const char *aclname
= qdict_get_str(qdict
, "aclname");
1663 qemu_acl
*acl
= find_acl(mon
, aclname
);
1664 qemu_acl_entry
*entry
;
1668 monitor_printf(mon
, "policy: %s\n",
1669 acl
->defaultDeny
? "deny" : "allow");
1670 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1672 monitor_printf(mon
, "%d: %s %s\n", i
,
1673 entry
->deny
? "deny" : "allow", entry
->match
);
1678 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1680 const char *aclname
= qdict_get_str(qdict
, "aclname");
1681 qemu_acl
*acl
= find_acl(mon
, aclname
);
1684 qemu_acl_reset(acl
);
1685 monitor_printf(mon
, "acl: removed all rules\n");
1689 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1691 const char *aclname
= qdict_get_str(qdict
, "aclname");
1692 const char *policy
= qdict_get_str(qdict
, "policy");
1693 qemu_acl
*acl
= find_acl(mon
, aclname
);
1696 if (strcmp(policy
, "allow") == 0) {
1697 acl
->defaultDeny
= 0;
1698 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1699 } else if (strcmp(policy
, "deny") == 0) {
1700 acl
->defaultDeny
= 1;
1701 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1703 monitor_printf(mon
, "acl: unknown policy '%s', "
1704 "expected 'deny' or 'allow'\n", policy
);
1709 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1711 const char *aclname
= qdict_get_str(qdict
, "aclname");
1712 const char *match
= qdict_get_str(qdict
, "match");
1713 const char *policy
= qdict_get_str(qdict
, "policy");
1714 int has_index
= qdict_haskey(qdict
, "index");
1715 int index
= qdict_get_try_int(qdict
, "index", -1);
1716 qemu_acl
*acl
= find_acl(mon
, aclname
);
1720 if (strcmp(policy
, "allow") == 0) {
1722 } else if (strcmp(policy
, "deny") == 0) {
1725 monitor_printf(mon
, "acl: unknown policy '%s', "
1726 "expected 'deny' or 'allow'\n", policy
);
1730 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1732 ret
= qemu_acl_append(acl
, deny
, match
);
1734 monitor_printf(mon
, "acl: unable to add acl entry\n");
1736 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1740 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1742 const char *aclname
= qdict_get_str(qdict
, "aclname");
1743 const char *match
= qdict_get_str(qdict
, "match");
1744 qemu_acl
*acl
= find_acl(mon
, aclname
);
1748 ret
= qemu_acl_remove(acl
, match
);
1750 monitor_printf(mon
, "acl: no matching acl entry\n");
1752 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1756 #if defined(TARGET_I386)
1757 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1760 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1761 int bank
= qdict_get_int(qdict
, "bank");
1762 uint64_t status
= qdict_get_int(qdict
, "status");
1763 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1764 uint64_t addr
= qdict_get_int(qdict
, "addr");
1765 uint64_t misc
= qdict_get_int(qdict
, "misc");
1767 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1768 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1769 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1775 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1777 const char *fdname
= qdict_get_str(qdict
, "fdname");
1781 fd
= qemu_chr_get_msgfd(mon
->chr
);
1783 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1787 if (qemu_isdigit(fdname
[0])) {
1788 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1794 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1799 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1800 if (strcmp(monfd
->name
, fdname
) != 0) {
1809 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1810 monfd
->name
= qemu_strdup(fdname
);
1813 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1816 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1818 const char *fdname
= qdict_get_str(qdict
, "fdname");
1821 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1822 if (strcmp(monfd
->name
, fdname
) != 0) {
1826 QLIST_REMOVE(monfd
, next
);
1828 qemu_free(monfd
->name
);
1833 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1837 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1839 int saved_vm_running
= vm_running
;
1840 const char *name
= qdict_get_str(qdict
, "name");
1844 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1848 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1852 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1855 if (strcmp(monfd
->name
, fdname
) != 0) {
1861 /* caller takes ownership of fd */
1862 QLIST_REMOVE(monfd
, next
);
1863 qemu_free(monfd
->name
);
1872 static const mon_cmd_t mon_cmds
[] = {
1873 #include "qemu-monitor.h"
1877 /* Please update qemu-monitor.hx when adding or changing commands */
1878 static const mon_cmd_t info_cmds
[] = {
1883 .help
= "show the version of QEMU",
1884 .user_print
= monitor_print_qobject
,
1885 .mhandler
.info_new
= do_info_version
,
1891 .help
= "show the network state",
1892 .mhandler
.info
= do_info_network
,
1898 .help
= "show the character devices",
1899 .mhandler
.info
= qemu_chr_info
,
1905 .help
= "show the block devices",
1906 .mhandler
.info
= bdrv_info
,
1909 .name
= "blockstats",
1912 .help
= "show block device statistics",
1913 .mhandler
.info
= bdrv_info_stats
,
1916 .name
= "registers",
1919 .help
= "show the cpu registers",
1920 .mhandler
.info
= do_info_registers
,
1926 .help
= "show infos for each CPU",
1927 .mhandler
.info
= do_info_cpus
,
1933 .help
= "show the command line history",
1934 .mhandler
.info
= do_info_history
,
1940 .help
= "show the interrupts statistics (if available)",
1941 .mhandler
.info
= irq_info
,
1947 .help
= "show i8259 (PIC) state",
1948 .mhandler
.info
= pic_info
,
1954 .help
= "show PCI info",
1955 .mhandler
.info
= pci_info
,
1957 #if defined(TARGET_I386) || defined(TARGET_SH4)
1962 .help
= "show virtual to physical memory mappings",
1963 .mhandler
.info
= tlb_info
,
1966 #if defined(TARGET_I386)
1971 .help
= "show the active virtual memory mappings",
1972 .mhandler
.info
= mem_info
,
1978 .help
= "show state of HPET",
1979 .mhandler
.info
= do_info_hpet
,
1986 .help
= "show dynamic compiler info",
1987 .mhandler
.info
= do_info_jit
,
1993 .help
= "show KVM information",
1994 .mhandler
.info
= do_info_kvm
,
2000 .help
= "show NUMA information",
2001 .mhandler
.info
= do_info_numa
,
2007 .help
= "show guest USB devices",
2008 .mhandler
.info
= usb_info
,
2014 .help
= "show host USB devices",
2015 .mhandler
.info
= usb_host_info
,
2021 .help
= "show profiling information",
2022 .mhandler
.info
= do_info_profile
,
2028 .help
= "show capture information",
2029 .mhandler
.info
= do_info_capture
,
2032 .name
= "snapshots",
2035 .help
= "show the currently saved VM snapshots",
2036 .mhandler
.info
= do_info_snapshots
,
2042 .help
= "show the current VM status (running|paused)",
2043 .mhandler
.info
= do_info_status
,
2049 .help
= "show guest PCMCIA status",
2050 .mhandler
.info
= pcmcia_info
,
2056 .help
= "show which guest mouse is receiving events",
2057 .mhandler
.info
= do_info_mice
,
2063 .help
= "show the vnc server status",
2064 .mhandler
.info
= do_info_vnc
,
2070 .help
= "show the current VM name",
2071 .mhandler
.info
= do_info_name
,
2077 .help
= "show the current VM UUID",
2078 .mhandler
.info
= do_info_uuid
,
2080 #if defined(TARGET_PPC)
2085 .help
= "show CPU statistics",
2086 .mhandler
.info
= do_info_cpu_stats
,
2089 #if defined(CONFIG_SLIRP)
2094 .help
= "show user network stack connection states",
2095 .mhandler
.info
= do_info_usernet
,
2102 .help
= "show migration status",
2103 .mhandler
.info
= do_info_migrate
,
2109 .help
= "show balloon information",
2110 .mhandler
.info
= do_info_balloon
,
2116 .help
= "show device tree",
2117 .mhandler
.info
= do_info_qtree
,
2123 .help
= "show qdev device model list",
2124 .mhandler
.info
= do_info_qdm
,
2130 .help
= "show roms",
2131 .mhandler
.info
= do_info_roms
,
2138 /*******************************************************************/
2140 static const char *pch
;
2141 static jmp_buf expr_env
;
2146 typedef struct MonitorDef
{
2149 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2153 #if defined(TARGET_I386)
2154 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2156 CPUState
*env
= mon_get_cpu();
2159 return env
->eip
+ env
->segs
[R_CS
].base
;
2163 #if defined(TARGET_PPC)
2164 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2166 CPUState
*env
= mon_get_cpu();
2174 for (i
= 0; i
< 8; i
++)
2175 u
|= env
->crf
[i
] << (32 - (4 * i
));
2180 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2182 CPUState
*env
= mon_get_cpu();
2188 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2190 CPUState
*env
= mon_get_cpu();
2196 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2198 CPUState
*env
= mon_get_cpu();
2201 return cpu_ppc_load_decr(env
);
2204 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2206 CPUState
*env
= mon_get_cpu();
2209 return cpu_ppc_load_tbu(env
);
2212 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2214 CPUState
*env
= mon_get_cpu();
2217 return cpu_ppc_load_tbl(env
);
2221 #if defined(TARGET_SPARC)
2222 #ifndef TARGET_SPARC64
2223 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2225 CPUState
*env
= mon_get_cpu();
2228 return GET_PSR(env
);
2232 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2234 CPUState
*env
= mon_get_cpu();
2237 return env
->regwptr
[val
];
2241 static const MonitorDef monitor_defs
[] = {
2244 #define SEG(name, seg) \
2245 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2246 { name ".base", offsetof(CPUState, segs[seg].base) },\
2247 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2249 { "eax", offsetof(CPUState
, regs
[0]) },
2250 { "ecx", offsetof(CPUState
, regs
[1]) },
2251 { "edx", offsetof(CPUState
, regs
[2]) },
2252 { "ebx", offsetof(CPUState
, regs
[3]) },
2253 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2254 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2255 { "esi", offsetof(CPUState
, regs
[6]) },
2256 { "edi", offsetof(CPUState
, regs
[7]) },
2257 #ifdef TARGET_X86_64
2258 { "r8", offsetof(CPUState
, regs
[8]) },
2259 { "r9", offsetof(CPUState
, regs
[9]) },
2260 { "r10", offsetof(CPUState
, regs
[10]) },
2261 { "r11", offsetof(CPUState
, regs
[11]) },
2262 { "r12", offsetof(CPUState
, regs
[12]) },
2263 { "r13", offsetof(CPUState
, regs
[13]) },
2264 { "r14", offsetof(CPUState
, regs
[14]) },
2265 { "r15", offsetof(CPUState
, regs
[15]) },
2267 { "eflags", offsetof(CPUState
, eflags
) },
2268 { "eip", offsetof(CPUState
, eip
) },
2275 { "pc", 0, monitor_get_pc
, },
2276 #elif defined(TARGET_PPC)
2277 /* General purpose registers */
2278 { "r0", offsetof(CPUState
, gpr
[0]) },
2279 { "r1", offsetof(CPUState
, gpr
[1]) },
2280 { "r2", offsetof(CPUState
, gpr
[2]) },
2281 { "r3", offsetof(CPUState
, gpr
[3]) },
2282 { "r4", offsetof(CPUState
, gpr
[4]) },
2283 { "r5", offsetof(CPUState
, gpr
[5]) },
2284 { "r6", offsetof(CPUState
, gpr
[6]) },
2285 { "r7", offsetof(CPUState
, gpr
[7]) },
2286 { "r8", offsetof(CPUState
, gpr
[8]) },
2287 { "r9", offsetof(CPUState
, gpr
[9]) },
2288 { "r10", offsetof(CPUState
, gpr
[10]) },
2289 { "r11", offsetof(CPUState
, gpr
[11]) },
2290 { "r12", offsetof(CPUState
, gpr
[12]) },
2291 { "r13", offsetof(CPUState
, gpr
[13]) },
2292 { "r14", offsetof(CPUState
, gpr
[14]) },
2293 { "r15", offsetof(CPUState
, gpr
[15]) },
2294 { "r16", offsetof(CPUState
, gpr
[16]) },
2295 { "r17", offsetof(CPUState
, gpr
[17]) },
2296 { "r18", offsetof(CPUState
, gpr
[18]) },
2297 { "r19", offsetof(CPUState
, gpr
[19]) },
2298 { "r20", offsetof(CPUState
, gpr
[20]) },
2299 { "r21", offsetof(CPUState
, gpr
[21]) },
2300 { "r22", offsetof(CPUState
, gpr
[22]) },
2301 { "r23", offsetof(CPUState
, gpr
[23]) },
2302 { "r24", offsetof(CPUState
, gpr
[24]) },
2303 { "r25", offsetof(CPUState
, gpr
[25]) },
2304 { "r26", offsetof(CPUState
, gpr
[26]) },
2305 { "r27", offsetof(CPUState
, gpr
[27]) },
2306 { "r28", offsetof(CPUState
, gpr
[28]) },
2307 { "r29", offsetof(CPUState
, gpr
[29]) },
2308 { "r30", offsetof(CPUState
, gpr
[30]) },
2309 { "r31", offsetof(CPUState
, gpr
[31]) },
2310 /* Floating point registers */
2311 { "f0", offsetof(CPUState
, fpr
[0]) },
2312 { "f1", offsetof(CPUState
, fpr
[1]) },
2313 { "f2", offsetof(CPUState
, fpr
[2]) },
2314 { "f3", offsetof(CPUState
, fpr
[3]) },
2315 { "f4", offsetof(CPUState
, fpr
[4]) },
2316 { "f5", offsetof(CPUState
, fpr
[5]) },
2317 { "f6", offsetof(CPUState
, fpr
[6]) },
2318 { "f7", offsetof(CPUState
, fpr
[7]) },
2319 { "f8", offsetof(CPUState
, fpr
[8]) },
2320 { "f9", offsetof(CPUState
, fpr
[9]) },
2321 { "f10", offsetof(CPUState
, fpr
[10]) },
2322 { "f11", offsetof(CPUState
, fpr
[11]) },
2323 { "f12", offsetof(CPUState
, fpr
[12]) },
2324 { "f13", offsetof(CPUState
, fpr
[13]) },
2325 { "f14", offsetof(CPUState
, fpr
[14]) },
2326 { "f15", offsetof(CPUState
, fpr
[15]) },
2327 { "f16", offsetof(CPUState
, fpr
[16]) },
2328 { "f17", offsetof(CPUState
, fpr
[17]) },
2329 { "f18", offsetof(CPUState
, fpr
[18]) },
2330 { "f19", offsetof(CPUState
, fpr
[19]) },
2331 { "f20", offsetof(CPUState
, fpr
[20]) },
2332 { "f21", offsetof(CPUState
, fpr
[21]) },
2333 { "f22", offsetof(CPUState
, fpr
[22]) },
2334 { "f23", offsetof(CPUState
, fpr
[23]) },
2335 { "f24", offsetof(CPUState
, fpr
[24]) },
2336 { "f25", offsetof(CPUState
, fpr
[25]) },
2337 { "f26", offsetof(CPUState
, fpr
[26]) },
2338 { "f27", offsetof(CPUState
, fpr
[27]) },
2339 { "f28", offsetof(CPUState
, fpr
[28]) },
2340 { "f29", offsetof(CPUState
, fpr
[29]) },
2341 { "f30", offsetof(CPUState
, fpr
[30]) },
2342 { "f31", offsetof(CPUState
, fpr
[31]) },
2343 { "fpscr", offsetof(CPUState
, fpscr
) },
2344 /* Next instruction pointer */
2345 { "nip|pc", offsetof(CPUState
, nip
) },
2346 { "lr", offsetof(CPUState
, lr
) },
2347 { "ctr", offsetof(CPUState
, ctr
) },
2348 { "decr", 0, &monitor_get_decr
, },
2349 { "ccr", 0, &monitor_get_ccr
, },
2350 /* Machine state register */
2351 { "msr", 0, &monitor_get_msr
, },
2352 { "xer", 0, &monitor_get_xer
, },
2353 { "tbu", 0, &monitor_get_tbu
, },
2354 { "tbl", 0, &monitor_get_tbl
, },
2355 #if defined(TARGET_PPC64)
2356 /* Address space register */
2357 { "asr", offsetof(CPUState
, asr
) },
2359 /* Segment registers */
2360 { "sdr1", offsetof(CPUState
, sdr1
) },
2361 { "sr0", offsetof(CPUState
, sr
[0]) },
2362 { "sr1", offsetof(CPUState
, sr
[1]) },
2363 { "sr2", offsetof(CPUState
, sr
[2]) },
2364 { "sr3", offsetof(CPUState
, sr
[3]) },
2365 { "sr4", offsetof(CPUState
, sr
[4]) },
2366 { "sr5", offsetof(CPUState
, sr
[5]) },
2367 { "sr6", offsetof(CPUState
, sr
[6]) },
2368 { "sr7", offsetof(CPUState
, sr
[7]) },
2369 { "sr8", offsetof(CPUState
, sr
[8]) },
2370 { "sr9", offsetof(CPUState
, sr
[9]) },
2371 { "sr10", offsetof(CPUState
, sr
[10]) },
2372 { "sr11", offsetof(CPUState
, sr
[11]) },
2373 { "sr12", offsetof(CPUState
, sr
[12]) },
2374 { "sr13", offsetof(CPUState
, sr
[13]) },
2375 { "sr14", offsetof(CPUState
, sr
[14]) },
2376 { "sr15", offsetof(CPUState
, sr
[15]) },
2377 /* Too lazy to put BATs and SPRs ... */
2378 #elif defined(TARGET_SPARC)
2379 { "g0", offsetof(CPUState
, gregs
[0]) },
2380 { "g1", offsetof(CPUState
, gregs
[1]) },
2381 { "g2", offsetof(CPUState
, gregs
[2]) },
2382 { "g3", offsetof(CPUState
, gregs
[3]) },
2383 { "g4", offsetof(CPUState
, gregs
[4]) },
2384 { "g5", offsetof(CPUState
, gregs
[5]) },
2385 { "g6", offsetof(CPUState
, gregs
[6]) },
2386 { "g7", offsetof(CPUState
, gregs
[7]) },
2387 { "o0", 0, monitor_get_reg
},
2388 { "o1", 1, monitor_get_reg
},
2389 { "o2", 2, monitor_get_reg
},
2390 { "o3", 3, monitor_get_reg
},
2391 { "o4", 4, monitor_get_reg
},
2392 { "o5", 5, monitor_get_reg
},
2393 { "o6", 6, monitor_get_reg
},
2394 { "o7", 7, monitor_get_reg
},
2395 { "l0", 8, monitor_get_reg
},
2396 { "l1", 9, monitor_get_reg
},
2397 { "l2", 10, monitor_get_reg
},
2398 { "l3", 11, monitor_get_reg
},
2399 { "l4", 12, monitor_get_reg
},
2400 { "l5", 13, monitor_get_reg
},
2401 { "l6", 14, monitor_get_reg
},
2402 { "l7", 15, monitor_get_reg
},
2403 { "i0", 16, monitor_get_reg
},
2404 { "i1", 17, monitor_get_reg
},
2405 { "i2", 18, monitor_get_reg
},
2406 { "i3", 19, monitor_get_reg
},
2407 { "i4", 20, monitor_get_reg
},
2408 { "i5", 21, monitor_get_reg
},
2409 { "i6", 22, monitor_get_reg
},
2410 { "i7", 23, monitor_get_reg
},
2411 { "pc", offsetof(CPUState
, pc
) },
2412 { "npc", offsetof(CPUState
, npc
) },
2413 { "y", offsetof(CPUState
, y
) },
2414 #ifndef TARGET_SPARC64
2415 { "psr", 0, &monitor_get_psr
, },
2416 { "wim", offsetof(CPUState
, wim
) },
2418 { "tbr", offsetof(CPUState
, tbr
) },
2419 { "fsr", offsetof(CPUState
, fsr
) },
2420 { "f0", offsetof(CPUState
, fpr
[0]) },
2421 { "f1", offsetof(CPUState
, fpr
[1]) },
2422 { "f2", offsetof(CPUState
, fpr
[2]) },
2423 { "f3", offsetof(CPUState
, fpr
[3]) },
2424 { "f4", offsetof(CPUState
, fpr
[4]) },
2425 { "f5", offsetof(CPUState
, fpr
[5]) },
2426 { "f6", offsetof(CPUState
, fpr
[6]) },
2427 { "f7", offsetof(CPUState
, fpr
[7]) },
2428 { "f8", offsetof(CPUState
, fpr
[8]) },
2429 { "f9", offsetof(CPUState
, fpr
[9]) },
2430 { "f10", offsetof(CPUState
, fpr
[10]) },
2431 { "f11", offsetof(CPUState
, fpr
[11]) },
2432 { "f12", offsetof(CPUState
, fpr
[12]) },
2433 { "f13", offsetof(CPUState
, fpr
[13]) },
2434 { "f14", offsetof(CPUState
, fpr
[14]) },
2435 { "f15", offsetof(CPUState
, fpr
[15]) },
2436 { "f16", offsetof(CPUState
, fpr
[16]) },
2437 { "f17", offsetof(CPUState
, fpr
[17]) },
2438 { "f18", offsetof(CPUState
, fpr
[18]) },
2439 { "f19", offsetof(CPUState
, fpr
[19]) },
2440 { "f20", offsetof(CPUState
, fpr
[20]) },
2441 { "f21", offsetof(CPUState
, fpr
[21]) },
2442 { "f22", offsetof(CPUState
, fpr
[22]) },
2443 { "f23", offsetof(CPUState
, fpr
[23]) },
2444 { "f24", offsetof(CPUState
, fpr
[24]) },
2445 { "f25", offsetof(CPUState
, fpr
[25]) },
2446 { "f26", offsetof(CPUState
, fpr
[26]) },
2447 { "f27", offsetof(CPUState
, fpr
[27]) },
2448 { "f28", offsetof(CPUState
, fpr
[28]) },
2449 { "f29", offsetof(CPUState
, fpr
[29]) },
2450 { "f30", offsetof(CPUState
, fpr
[30]) },
2451 { "f31", offsetof(CPUState
, fpr
[31]) },
2452 #ifdef TARGET_SPARC64
2453 { "f32", offsetof(CPUState
, fpr
[32]) },
2454 { "f34", offsetof(CPUState
, fpr
[34]) },
2455 { "f36", offsetof(CPUState
, fpr
[36]) },
2456 { "f38", offsetof(CPUState
, fpr
[38]) },
2457 { "f40", offsetof(CPUState
, fpr
[40]) },
2458 { "f42", offsetof(CPUState
, fpr
[42]) },
2459 { "f44", offsetof(CPUState
, fpr
[44]) },
2460 { "f46", offsetof(CPUState
, fpr
[46]) },
2461 { "f48", offsetof(CPUState
, fpr
[48]) },
2462 { "f50", offsetof(CPUState
, fpr
[50]) },
2463 { "f52", offsetof(CPUState
, fpr
[52]) },
2464 { "f54", offsetof(CPUState
, fpr
[54]) },
2465 { "f56", offsetof(CPUState
, fpr
[56]) },
2466 { "f58", offsetof(CPUState
, fpr
[58]) },
2467 { "f60", offsetof(CPUState
, fpr
[60]) },
2468 { "f62", offsetof(CPUState
, fpr
[62]) },
2469 { "asi", offsetof(CPUState
, asi
) },
2470 { "pstate", offsetof(CPUState
, pstate
) },
2471 { "cansave", offsetof(CPUState
, cansave
) },
2472 { "canrestore", offsetof(CPUState
, canrestore
) },
2473 { "otherwin", offsetof(CPUState
, otherwin
) },
2474 { "wstate", offsetof(CPUState
, wstate
) },
2475 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2476 { "fprs", offsetof(CPUState
, fprs
) },
2482 static void expr_error(Monitor
*mon
, const char *msg
)
2484 monitor_printf(mon
, "%s\n", msg
);
2485 longjmp(expr_env
, 1);
2488 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2489 static int get_monitor_def(target_long
*pval
, const char *name
)
2491 const MonitorDef
*md
;
2494 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2495 if (compare_cmd(name
, md
->name
)) {
2496 if (md
->get_value
) {
2497 *pval
= md
->get_value(md
, md
->offset
);
2499 CPUState
*env
= mon_get_cpu();
2502 ptr
= (uint8_t *)env
+ md
->offset
;
2505 *pval
= *(int32_t *)ptr
;
2508 *pval
= *(target_long
*)ptr
;
2521 static void next(void)
2525 while (qemu_isspace(*pch
))
2530 static int64_t expr_sum(Monitor
*mon
);
2532 static int64_t expr_unary(Monitor
*mon
)
2541 n
= expr_unary(mon
);
2545 n
= -expr_unary(mon
);
2549 n
= ~expr_unary(mon
);
2555 expr_error(mon
, "')' expected");
2562 expr_error(mon
, "character constant expected");
2566 expr_error(mon
, "missing terminating \' character");
2576 while ((*pch
>= 'a' && *pch
<= 'z') ||
2577 (*pch
>= 'A' && *pch
<= 'Z') ||
2578 (*pch
>= '0' && *pch
<= '9') ||
2579 *pch
== '_' || *pch
== '.') {
2580 if ((q
- buf
) < sizeof(buf
) - 1)
2584 while (qemu_isspace(*pch
))
2587 ret
= get_monitor_def(®
, buf
);
2589 expr_error(mon
, "unknown register");
2591 expr_error(mon
, "no cpu defined");
2596 expr_error(mon
, "unexpected end of expression");
2600 #if TARGET_PHYS_ADDR_BITS > 32
2601 n
= strtoull(pch
, &p
, 0);
2603 n
= strtoul(pch
, &p
, 0);
2606 expr_error(mon
, "invalid char in expression");
2609 while (qemu_isspace(*pch
))
2617 static int64_t expr_prod(Monitor
*mon
)
2622 val
= expr_unary(mon
);
2625 if (op
!= '*' && op
!= '/' && op
!= '%')
2628 val2
= expr_unary(mon
);
2637 expr_error(mon
, "division by zero");
2648 static int64_t expr_logic(Monitor
*mon
)
2653 val
= expr_prod(mon
);
2656 if (op
!= '&' && op
!= '|' && op
!= '^')
2659 val2
= expr_prod(mon
);
2676 static int64_t expr_sum(Monitor
*mon
)
2681 val
= expr_logic(mon
);
2684 if (op
!= '+' && op
!= '-')
2687 val2
= expr_logic(mon
);
2696 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2699 if (setjmp(expr_env
)) {
2703 while (qemu_isspace(*pch
))
2705 *pval
= expr_sum(mon
);
2710 static int get_str(char *buf
, int buf_size
, const char **pp
)
2718 while (qemu_isspace(*p
))
2728 while (*p
!= '\0' && *p
!= '\"') {
2744 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2747 if ((q
- buf
) < buf_size
- 1) {
2751 if ((q
- buf
) < buf_size
- 1) {
2758 qemu_printf("unterminated string\n");
2763 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2764 if ((q
- buf
) < buf_size
- 1) {
2776 * Store the command-name in cmdname, and return a pointer to
2777 * the remaining of the command string.
2779 static const char *get_command_name(const char *cmdline
,
2780 char *cmdname
, size_t nlen
)
2783 const char *p
, *pstart
;
2786 while (qemu_isspace(*p
))
2791 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2796 memcpy(cmdname
, pstart
, len
);
2797 cmdname
[len
] = '\0';
2802 * Read key of 'type' into 'key' and return the current
2805 static char *key_get_info(const char *type
, char **key
)
2813 p
= strchr(type
, ':');
2820 str
= qemu_malloc(len
+ 1);
2821 memcpy(str
, type
, len
);
2828 static int default_fmt_format
= 'x';
2829 static int default_fmt_size
= 4;
2833 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2834 const char *cmdline
,
2837 const char *p
, *typestr
;
2839 const mon_cmd_t
*cmd
;
2845 monitor_printf(mon
, "command='%s'\n", cmdline
);
2848 /* extract the command name */
2849 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2853 /* find the command */
2854 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2855 if (compare_cmd(cmdname
, cmd
->name
))
2859 if (cmd
->name
== NULL
) {
2860 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2864 /* parse the parameters */
2865 typestr
= cmd
->args_type
;
2867 typestr
= key_get_info(typestr
, &key
);
2879 while (qemu_isspace(*p
))
2881 if (*typestr
== '?') {
2884 /* no optional string: NULL argument */
2888 ret
= get_str(buf
, sizeof(buf
), &p
);
2892 monitor_printf(mon
, "%s: filename expected\n",
2896 monitor_printf(mon
, "%s: block device name expected\n",
2900 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2905 qdict_put(qdict
, key
, qstring_from_str(buf
));
2910 int count
, format
, size
;
2912 while (qemu_isspace(*p
))
2918 if (qemu_isdigit(*p
)) {
2920 while (qemu_isdigit(*p
)) {
2921 count
= count
* 10 + (*p
- '0');
2959 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2960 monitor_printf(mon
, "invalid char in format: '%c'\n",
2965 format
= default_fmt_format
;
2966 if (format
!= 'i') {
2967 /* for 'i', not specifying a size gives -1 as size */
2969 size
= default_fmt_size
;
2970 default_fmt_size
= size
;
2972 default_fmt_format
= format
;
2975 format
= default_fmt_format
;
2976 if (format
!= 'i') {
2977 size
= default_fmt_size
;
2982 qdict_put(qdict
, "count", qint_from_int(count
));
2983 qdict_put(qdict
, "format", qint_from_int(format
));
2984 qdict_put(qdict
, "size", qint_from_int(size
));
2992 while (qemu_isspace(*p
))
2994 if (*typestr
== '?' || *typestr
== '.') {
2995 if (*typestr
== '?') {
3003 while (qemu_isspace(*p
))
3012 if (get_expr(mon
, &val
, &p
))
3014 /* Check if 'i' is greater than 32-bit */
3015 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3016 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3017 monitor_printf(mon
, "integer is for 32-bit values\n");
3020 qdict_put(qdict
, key
, qint_from_int(val
));
3031 while (qemu_isspace(*p
))
3037 monitor_printf(mon
, "%s: unsupported option -%c\n",
3044 qdict_put(qdict
, key
, qint_from_int(has_option
));
3049 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3055 /* check that all arguments were parsed */
3056 while (qemu_isspace(*p
))
3059 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3071 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3074 const mon_cmd_t
*cmd
;
3076 qdict
= qdict_new();
3078 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3082 qemu_errors_to_mon(mon
);
3084 if (monitor_handler_ported(cmd
)) {
3085 QObject
*data
= NULL
;
3087 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3089 cmd
->user_print(mon
, data
);
3091 qobject_decref(data
);
3093 cmd
->mhandler
.cmd(mon
, qdict
);
3096 qemu_errors_to_previous();
3102 static void cmd_completion(const char *name
, const char *list
)
3104 const char *p
, *pstart
;
3113 p
= pstart
+ strlen(pstart
);
3115 if (len
> sizeof(cmd
) - 2)
3116 len
= sizeof(cmd
) - 2;
3117 memcpy(cmd
, pstart
, len
);
3119 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3120 readline_add_completion(cur_mon
->rs
, cmd
);
3128 static void file_completion(const char *input
)
3133 char file
[1024], file_prefix
[1024];
3137 p
= strrchr(input
, '/');
3140 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3141 pstrcpy(path
, sizeof(path
), ".");
3143 input_path_len
= p
- input
+ 1;
3144 memcpy(path
, input
, input_path_len
);
3145 if (input_path_len
> sizeof(path
) - 1)
3146 input_path_len
= sizeof(path
) - 1;
3147 path
[input_path_len
] = '\0';
3148 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3150 #ifdef DEBUG_COMPLETION
3151 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3152 input
, path
, file_prefix
);
3154 ffs
= opendir(path
);
3162 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3163 memcpy(file
, input
, input_path_len
);
3164 if (input_path_len
< sizeof(file
))
3165 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3167 /* stat the file to find out if it's a directory.
3168 * In that case add a slash to speed up typing long paths
3171 if(S_ISDIR(sb
.st_mode
))
3172 pstrcat(file
, sizeof(file
), "/");
3173 readline_add_completion(cur_mon
->rs
, file
);
3179 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3181 const char *name
= bdrv_get_device_name(bs
);
3182 const char *input
= opaque
;
3184 if (input
[0] == '\0' ||
3185 !strncmp(name
, (char *)input
, strlen(input
))) {
3186 readline_add_completion(cur_mon
->rs
, name
);
3190 /* NOTE: this parser is an approximate form of the real command parser */
3191 static void parse_cmdline(const char *cmdline
,
3192 int *pnb_args
, char **args
)
3201 while (qemu_isspace(*p
))
3205 if (nb_args
>= MAX_ARGS
)
3207 ret
= get_str(buf
, sizeof(buf
), &p
);
3208 args
[nb_args
] = qemu_strdup(buf
);
3213 *pnb_args
= nb_args
;
3216 static const char *next_arg_type(const char *typestr
)
3218 const char *p
= strchr(typestr
, ':');
3219 return (p
!= NULL
? ++p
: typestr
);
3222 static void monitor_find_completion(const char *cmdline
)
3224 const char *cmdname
;
3225 char *args
[MAX_ARGS
];
3226 int nb_args
, i
, len
;
3227 const char *ptype
, *str
;
3228 const mon_cmd_t
*cmd
;
3231 parse_cmdline(cmdline
, &nb_args
, args
);
3232 #ifdef DEBUG_COMPLETION
3233 for(i
= 0; i
< nb_args
; i
++) {
3234 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3238 /* if the line ends with a space, it means we want to complete the
3240 len
= strlen(cmdline
);
3241 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3242 if (nb_args
>= MAX_ARGS
)
3244 args
[nb_args
++] = qemu_strdup("");
3247 /* command completion */
3252 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3253 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3254 cmd_completion(cmdname
, cmd
->name
);
3257 /* find the command */
3258 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3259 if (compare_cmd(args
[0], cmd
->name
))
3264 ptype
= next_arg_type(cmd
->args_type
);
3265 for(i
= 0; i
< nb_args
- 2; i
++) {
3266 if (*ptype
!= '\0') {
3267 ptype
= next_arg_type(ptype
);
3268 while (*ptype
== '?')
3269 ptype
= next_arg_type(ptype
);
3272 str
= args
[nb_args
- 1];
3273 if (*ptype
== '-' && ptype
[1] != '\0') {
3278 /* file completion */
3279 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3280 file_completion(str
);
3283 /* block device name completion */
3284 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3285 bdrv_iterate(block_completion_it
, (void *)str
);
3288 /* XXX: more generic ? */
3289 if (!strcmp(cmd
->name
, "info")) {
3290 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3291 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3292 cmd_completion(str
, cmd
->name
);
3294 } else if (!strcmp(cmd
->name
, "sendkey")) {
3295 char *sep
= strrchr(str
, '-');
3298 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3299 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3300 cmd_completion(str
, key
->name
);
3302 } else if (!strcmp(cmd
->name
, "help|?")) {
3303 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3304 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3305 cmd_completion(str
, cmd
->name
);
3313 for(i
= 0; i
< nb_args
; i
++)
3317 static int monitor_can_read(void *opaque
)
3319 Monitor
*mon
= opaque
;
3321 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3324 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3326 Monitor
*old_mon
= cur_mon
;
3332 for (i
= 0; i
< size
; i
++)
3333 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3335 if (size
== 0 || buf
[size
- 1] != 0)
3336 monitor_printf(cur_mon
, "corrupted command\n");
3338 monitor_handle_command(cur_mon
, (char *)buf
);
3344 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3346 monitor_suspend(mon
);
3347 monitor_handle_command(mon
, cmdline
);
3348 monitor_resume(mon
);
3351 int monitor_suspend(Monitor
*mon
)
3359 void monitor_resume(Monitor
*mon
)
3363 if (--mon
->suspend_cnt
== 0)
3364 readline_show_prompt(mon
->rs
);
3367 static void monitor_event(void *opaque
, int event
)
3369 Monitor
*mon
= opaque
;
3372 case CHR_EVENT_MUX_IN
:
3374 if (mon
->reset_seen
) {
3375 readline_restart(mon
->rs
);
3376 monitor_resume(mon
);
3379 mon
->suspend_cnt
= 0;
3383 case CHR_EVENT_MUX_OUT
:
3384 if (mon
->reset_seen
) {
3385 if (mon
->suspend_cnt
== 0) {
3386 monitor_printf(mon
, "\n");
3389 monitor_suspend(mon
);
3396 case CHR_EVENT_RESET
:
3397 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3398 "information\n", QEMU_VERSION
);
3399 if (!mon
->mux_out
) {
3400 readline_show_prompt(mon
->rs
);
3402 mon
->reset_seen
= 1;
3416 void monitor_init(CharDriverState
*chr
, int flags
)
3418 static int is_first_init
= 1;
3421 if (is_first_init
) {
3422 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3426 mon
= qemu_mallocz(sizeof(*mon
));
3430 if (flags
& MONITOR_USE_READLINE
) {
3431 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3432 monitor_read_command(mon
, 0);
3435 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3438 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3439 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3443 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3445 BlockDriverState
*bs
= opaque
;
3448 if (bdrv_set_key(bs
, password
) != 0) {
3449 monitor_printf(mon
, "invalid password\n");
3452 if (mon
->password_completion_cb
)
3453 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3455 monitor_read_command(mon
, 1);
3458 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3459 BlockDriverCompletionFunc
*completion_cb
,
3464 if (!bdrv_key_required(bs
)) {
3466 completion_cb(opaque
, 0);
3470 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3471 bdrv_get_encrypted_filename(bs
));
3473 mon
->password_completion_cb
= completion_cb
;
3474 mon
->password_opaque
= opaque
;
3476 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3478 if (err
&& completion_cb
)
3479 completion_cb(opaque
, err
);
3482 typedef struct QemuErrorSink QemuErrorSink
;
3483 struct QemuErrorSink
{
3492 QemuErrorSink
*previous
;
3495 static QemuErrorSink
*qemu_error_sink
;
3497 void qemu_errors_to_file(FILE *fp
)
3499 QemuErrorSink
*sink
;
3501 sink
= qemu_mallocz(sizeof(*sink
));
3502 sink
->dest
= ERR_SINK_FILE
;
3504 sink
->previous
= qemu_error_sink
;
3505 qemu_error_sink
= sink
;
3508 void qemu_errors_to_mon(Monitor
*mon
)
3510 QemuErrorSink
*sink
;
3512 sink
= qemu_mallocz(sizeof(*sink
));
3513 sink
->dest
= ERR_SINK_MONITOR
;
3515 sink
->previous
= qemu_error_sink
;
3516 qemu_error_sink
= sink
;
3519 void qemu_errors_to_previous(void)
3521 QemuErrorSink
*sink
;
3523 assert(qemu_error_sink
!= NULL
);
3524 sink
= qemu_error_sink
;
3525 qemu_error_sink
= sink
->previous
;
3529 void qemu_error(const char *fmt
, ...)
3533 assert(qemu_error_sink
!= NULL
);
3534 switch (qemu_error_sink
->dest
) {
3536 va_start(args
, fmt
);
3537 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3540 case ERR_SINK_MONITOR
:
3541 va_start(args
, fmt
);
3542 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);