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"
55 //#define DEBUG_COMPLETION
61 * 'B' block device name
62 * 's' string (accept optional quote)
64 * 'l' target long (32 or 64 bit)
65 * '/' optional gdb-like print format (like "/10x")
67 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
73 typedef struct mon_cmd_t
{
75 const char *args_type
;
78 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
80 void (*info
)(Monitor
*mon
);
81 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
82 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
83 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
87 /* file descriptors passed via SCM_RIGHTS */
88 typedef struct mon_fd_t mon_fd_t
;
92 QLIST_ENTRY(mon_fd_t
) next
;
101 uint8_t outbuf
[1024];
105 BlockDriverCompletionFunc
*password_completion_cb
;
106 void *password_opaque
;
108 QLIST_HEAD(,mon_fd_t
) fds
;
109 QLIST_ENTRY(Monitor
) entry
;
112 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
114 static const mon_cmd_t mon_cmds
[];
115 static const mon_cmd_t info_cmds
[];
117 Monitor
*cur_mon
= NULL
;
119 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
122 /* Return true if in control mode, false otherwise */
123 static inline int monitor_ctrl_mode(const Monitor
*mon
)
125 return (mon
->flags
& MONITOR_USE_CONTROL
);
128 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
130 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
132 readline_show_prompt(mon
->rs
);
135 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
139 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
140 /* prompt is printed on return from the command handler */
143 monitor_printf(mon
, "terminal does not support password prompting\n");
148 void monitor_flush(Monitor
*mon
)
150 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
151 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
152 mon
->outbuf_index
= 0;
156 /* flush at every end of line or if the buffer is full */
157 static void monitor_puts(Monitor
*mon
, const char *str
)
169 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
170 mon
->outbuf
[mon
->outbuf_index
++] = c
;
171 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
177 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
180 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
181 monitor_puts(mon
, buf
);
184 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
188 monitor_vprintf(mon
, fmt
, ap
);
192 void monitor_print_filename(Monitor
*mon
, const char *filename
)
196 for (i
= 0; filename
[i
]; i
++) {
197 switch (filename
[i
]) {
201 monitor_printf(mon
, "\\%c", filename
[i
]);
204 monitor_printf(mon
, "\\t");
207 monitor_printf(mon
, "\\r");
210 monitor_printf(mon
, "\\n");
213 monitor_printf(mon
, "%c", filename
[i
]);
219 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
223 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
228 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
230 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
232 return cmd
->user_print
!= NULL
;
235 static inline int monitor_has_error(const Monitor
*mon
)
237 return mon
->error
!= NULL
;
240 static void monitor_print_qobject(Monitor
*mon
, const QObject
*data
)
242 switch (qobject_type(data
)) {
244 monitor_printf(mon
, "%s",qstring_get_str(qobject_to_qstring(data
)));
247 monitor_printf(mon
, "%" PRId64
,qint_get_int(qobject_to_qint(data
)));
250 monitor_printf(mon
, "ERROR: unsupported type: %d",
255 monitor_puts(mon
, "\n");
258 static int compare_cmd(const char *name
, const char *list
)
260 const char *p
, *pstart
;
268 p
= pstart
+ strlen(pstart
);
269 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
278 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
279 const char *prefix
, const char *name
)
281 const mon_cmd_t
*cmd
;
283 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
284 if (!name
|| !strcmp(name
, cmd
->name
))
285 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
286 cmd
->params
, cmd
->help
);
290 static void help_cmd(Monitor
*mon
, const char *name
)
292 if (name
&& !strcmp(name
, "info")) {
293 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
295 help_cmd_dump(mon
, mon_cmds
, "", name
);
296 if (name
&& !strcmp(name
, "log")) {
297 const CPULogItem
*item
;
298 monitor_printf(mon
, "Log items (comma separated):\n");
299 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
300 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
301 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
307 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
309 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
312 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
316 const char *device
= qdict_get_str(qdict
, "device");
318 all_devices
= !strcmp(device
, "all");
319 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
321 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
323 bdrv_commit(dinfo
->bdrv
);
327 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
329 const mon_cmd_t
*cmd
;
330 const char *item
= qdict_get_try_str(qdict
, "item");
335 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
336 if (compare_cmd(item
, cmd
->name
))
340 if (cmd
->name
== NULL
)
343 if (monitor_handler_ported(cmd
)) {
344 cmd
->mhandler
.info_new(mon
, ret_data
);
346 cmd
->user_print(mon
, *ret_data
);
348 cmd
->mhandler
.info(mon
);
354 help_cmd(mon
, "info");
358 * do_info_version(): Show QEMU version
360 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
362 *ret_data
= QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION
));
365 static void do_info_name(Monitor
*mon
)
368 monitor_printf(mon
, "%s\n", qemu_name
);
371 #if defined(TARGET_I386)
372 static void do_info_hpet(Monitor
*mon
)
374 monitor_printf(mon
, "HPET is %s by QEMU\n",
375 (no_hpet
) ? "disabled" : "enabled");
379 static void do_info_uuid(Monitor
*mon
)
381 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
382 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
383 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
384 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
385 qemu_uuid
[14], qemu_uuid
[15]);
388 /* get the current CPU defined by the user */
389 static int mon_set_cpu(int cpu_index
)
393 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
394 if (env
->cpu_index
== cpu_index
) {
395 cur_mon
->mon_cpu
= env
;
402 static CPUState
*mon_get_cpu(void)
404 if (!cur_mon
->mon_cpu
) {
407 cpu_synchronize_state(cur_mon
->mon_cpu
);
408 return cur_mon
->mon_cpu
;
411 static void do_info_registers(Monitor
*mon
)
418 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
421 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
426 static void print_cpu_iter(QObject
*obj
, void *opaque
)
430 Monitor
*mon
= opaque
;
432 assert(qobject_type(obj
) == QTYPE_QDICT
);
433 cpu
= qobject_to_qdict(obj
);
435 if (strcmp(qdict_get_str(cpu
, "current"), "yes") == 0)
438 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
440 #if defined(TARGET_I386)
441 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
442 (target_ulong
) qdict_get_int(cpu
, "pc"));
443 #elif defined(TARGET_PPC)
444 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
445 (target_long
) qdict_get_int(cpu
, "nip"));
446 #elif defined(TARGET_SPARC)
447 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
448 (target_long
) qdict_get_int(cpu
, "pc"));
449 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
450 (target_long
) qdict_get_int(cpu
, "npc"));
451 #elif defined(TARGET_MIPS)
452 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
453 (target_long
) qdict_get_int(cpu
, "PC"));
456 if (strcmp(qdict_get_str(cpu
, "halted"), "yes") == 0)
457 monitor_printf(mon
, " (halted)");
459 monitor_printf(mon
, "\n");
462 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
466 assert(qobject_type(data
) == QTYPE_QLIST
);
467 cpu_list
= qobject_to_qlist(data
);
468 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
472 * do_info_cpus(): Show CPU information
474 * Return a QList with a QDict for each CPU.
478 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
479 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
481 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
486 cpu_list
= qlist_new();
488 /* just to set the default cpu if not already done */
491 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
493 QDict
*cpu
= qdict_new();
495 cpu_synchronize_state(env
);
497 qdict_put(cpu
, "CPU", qint_from_int(env
->cpu_index
));
498 answer
= (env
== mon
->mon_cpu
) ? "yes" : "no";
499 qdict_put(cpu
, "current", qstring_from_str(answer
));
501 #if defined(TARGET_I386)
502 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
503 #elif defined(TARGET_PPC)
504 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
505 #elif defined(TARGET_SPARC)
506 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
507 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
508 #elif defined(TARGET_MIPS)
509 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
511 answer
= env
->halted
? "yes" : "no";
512 qdict_put(cpu
, "halted", qstring_from_str(answer
));
514 qlist_append(cpu_list
, cpu
);
517 *ret_data
= QOBJECT(cpu_list
);
520 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
522 int index
= qdict_get_int(qdict
, "index");
523 if (mon_set_cpu(index
) < 0)
524 monitor_printf(mon
, "Invalid CPU index\n");
527 static void do_info_jit(Monitor
*mon
)
529 dump_exec_info((FILE *)mon
, monitor_fprintf
);
532 static void do_info_history(Monitor
*mon
)
541 str
= readline_get_history(mon
->rs
, i
);
544 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
549 #if defined(TARGET_PPC)
550 /* XXX: not implemented in other targets */
551 static void do_info_cpu_stats(Monitor
*mon
)
556 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
561 * do_quit(): Quit QEMU execution
563 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
568 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
570 if (bdrv_is_inserted(bs
)) {
572 if (!bdrv_is_removable(bs
)) {
573 monitor_printf(mon
, "device is not removable\n");
576 if (bdrv_is_locked(bs
)) {
577 monitor_printf(mon
, "device is locked\n");
586 static void do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
588 BlockDriverState
*bs
;
589 int force
= qdict_get_int(qdict
, "force");
590 const char *filename
= qdict_get_str(qdict
, "filename");
592 bs
= bdrv_find(filename
);
594 monitor_printf(mon
, "device not found\n");
597 eject_device(mon
, bs
, force
);
600 static void do_change_block(Monitor
*mon
, const char *device
,
601 const char *filename
, const char *fmt
)
603 BlockDriverState
*bs
;
604 BlockDriver
*drv
= NULL
;
606 bs
= bdrv_find(device
);
608 monitor_printf(mon
, "device not found\n");
612 drv
= bdrv_find_whitelisted_format(fmt
);
614 monitor_printf(mon
, "invalid format %s\n", fmt
);
618 if (eject_device(mon
, bs
, 0) < 0)
620 bdrv_open2(bs
, filename
, 0, drv
);
621 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
624 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
627 if (vnc_display_password(NULL
, password
) < 0)
628 monitor_printf(mon
, "could not set VNC server password\n");
630 monitor_read_command(mon
, 1);
633 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
635 if (strcmp(target
, "passwd") == 0 ||
636 strcmp(target
, "password") == 0) {
639 strncpy(password
, arg
, sizeof(password
));
640 password
[sizeof(password
) - 1] = '\0';
641 change_vnc_password_cb(mon
, password
, NULL
);
643 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
646 if (vnc_display_open(NULL
, target
) < 0)
647 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
651 static void do_change(Monitor
*mon
, const QDict
*qdict
)
653 const char *device
= qdict_get_str(qdict
, "device");
654 const char *target
= qdict_get_str(qdict
, "target");
655 const char *arg
= qdict_get_try_str(qdict
, "arg");
656 if (strcmp(device
, "vnc") == 0) {
657 do_change_vnc(mon
, target
, arg
);
659 do_change_block(mon
, device
, target
, arg
);
663 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
665 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
668 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
670 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
673 static void do_log(Monitor
*mon
, const QDict
*qdict
)
676 const char *items
= qdict_get_str(qdict
, "items");
678 if (!strcmp(items
, "none")) {
681 mask
= cpu_str_to_log_mask(items
);
683 help_cmd(mon
, "log");
690 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
692 const char *option
= qdict_get_try_str(qdict
, "option");
693 if (!option
|| !strcmp(option
, "on")) {
695 } else if (!strcmp(option
, "off")) {
698 monitor_printf(mon
, "unexpected option %s\n", option
);
703 * do_stop(): Stop VM execution
705 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
707 vm_stop(EXCP_INTERRUPT
);
710 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
712 struct bdrv_iterate_context
{
718 * do_cont(): Resume emulation.
720 static void do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
722 struct bdrv_iterate_context context
= { mon
, 0 };
724 bdrv_iterate(encrypted_bdrv_it
, &context
);
725 /* only resume the vm if all keys are set and valid */
730 static void bdrv_key_cb(void *opaque
, int err
)
732 Monitor
*mon
= opaque
;
734 /* another key was set successfully, retry to continue */
736 do_cont(mon
, NULL
, NULL
);
739 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
741 struct bdrv_iterate_context
*context
= opaque
;
743 if (!context
->err
&& bdrv_key_required(bs
)) {
744 context
->err
= -EBUSY
;
745 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
750 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
752 const char *device
= qdict_get_try_str(qdict
, "device");
754 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
755 if (gdbserver_start(device
) < 0) {
756 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
758 } else if (strcmp(device
, "none") == 0) {
759 monitor_printf(mon
, "Disabled gdbserver\n");
761 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
766 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
768 const char *action
= qdict_get_str(qdict
, "action");
769 if (select_watchdog_action(action
) == -1) {
770 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
774 static void monitor_printc(Monitor
*mon
, int c
)
776 monitor_printf(mon
, "'");
779 monitor_printf(mon
, "\\'");
782 monitor_printf(mon
, "\\\\");
785 monitor_printf(mon
, "\\n");
788 monitor_printf(mon
, "\\r");
791 if (c
>= 32 && c
<= 126) {
792 monitor_printf(mon
, "%c", c
);
794 monitor_printf(mon
, "\\x%02x", c
);
798 monitor_printf(mon
, "'");
801 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
802 target_phys_addr_t addr
, int is_physical
)
805 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
813 if (!env
&& !is_physical
)
818 } else if (wsize
== 4) {
821 /* as default we use the current CS size */
825 if ((env
->efer
& MSR_EFER_LMA
) &&
826 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
830 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
835 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
844 nb_per_line
= line_size
/ wsize
;
849 max_digits
= (wsize
* 8 + 2) / 3;
853 max_digits
= (wsize
* 8) / 4;
857 max_digits
= (wsize
* 8 * 10 + 32) / 33;
866 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
868 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
873 cpu_physical_memory_rw(addr
, buf
, l
, 0);
878 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
879 monitor_printf(mon
, " Cannot access memory\n");
888 v
= ldub_raw(buf
+ i
);
891 v
= lduw_raw(buf
+ i
);
894 v
= (uint32_t)ldl_raw(buf
+ i
);
897 v
= ldq_raw(buf
+ i
);
900 monitor_printf(mon
, " ");
903 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
906 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
909 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
912 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
915 monitor_printc(mon
, v
);
920 monitor_printf(mon
, "\n");
926 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
928 int count
= qdict_get_int(qdict
, "count");
929 int format
= qdict_get_int(qdict
, "format");
930 int size
= qdict_get_int(qdict
, "size");
931 target_long addr
= qdict_get_int(qdict
, "addr");
933 memory_dump(mon
, count
, format
, size
, addr
, 0);
936 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
938 int count
= qdict_get_int(qdict
, "count");
939 int format
= qdict_get_int(qdict
, "format");
940 int size
= qdict_get_int(qdict
, "size");
941 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
943 memory_dump(mon
, count
, format
, size
, addr
, 1);
946 static void do_print(Monitor
*mon
, const QDict
*qdict
)
948 int format
= qdict_get_int(qdict
, "format");
949 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
951 #if TARGET_PHYS_ADDR_BITS == 32
954 monitor_printf(mon
, "%#o", val
);
957 monitor_printf(mon
, "%#x", val
);
960 monitor_printf(mon
, "%u", val
);
964 monitor_printf(mon
, "%d", val
);
967 monitor_printc(mon
, val
);
973 monitor_printf(mon
, "%#" PRIo64
, val
);
976 monitor_printf(mon
, "%#" PRIx64
, val
);
979 monitor_printf(mon
, "%" PRIu64
, val
);
983 monitor_printf(mon
, "%" PRId64
, val
);
986 monitor_printc(mon
, val
);
990 monitor_printf(mon
, "\n");
993 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
996 uint32_t size
= qdict_get_int(qdict
, "size");
997 const char *filename
= qdict_get_str(qdict
, "filename");
998 target_long addr
= qdict_get_int(qdict
, "val");
1003 env
= mon_get_cpu();
1007 f
= fopen(filename
, "wb");
1009 monitor_printf(mon
, "could not open '%s'\n", filename
);
1016 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1017 fwrite(buf
, 1, l
, f
);
1024 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1030 uint32_t size
= qdict_get_int(qdict
, "size");
1031 const char *filename
= qdict_get_str(qdict
, "filename");
1032 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1034 f
= fopen(filename
, "wb");
1036 monitor_printf(mon
, "could not open '%s'\n", filename
);
1043 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1044 fwrite(buf
, 1, l
, f
);
1052 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1057 uint32_t start
= qdict_get_int(qdict
, "start");
1058 uint32_t size
= qdict_get_int(qdict
, "size");
1061 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1062 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1063 /* BSD sum algorithm ('sum' Unix command) */
1064 sum
= (sum
>> 1) | (sum
<< 15);
1067 monitor_printf(mon
, "%05d\n", sum
);
1075 static const KeyDef key_defs
[] = {
1077 { 0x36, "shift_r" },
1082 { 0xe4, "altgr_r" },
1102 { 0x0e, "backspace" },
1139 { 0x37, "asterisk" },
1142 { 0x3a, "caps_lock" },
1153 { 0x45, "num_lock" },
1154 { 0x46, "scroll_lock" },
1156 { 0xb5, "kp_divide" },
1157 { 0x37, "kp_multiply" },
1158 { 0x4a, "kp_subtract" },
1160 { 0x9c, "kp_enter" },
1161 { 0x53, "kp_decimal" },
1194 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1209 { 0xfe, "compose" },
1214 static int get_keycode(const char *key
)
1220 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1221 if (!strcmp(key
, p
->name
))
1224 if (strstart(key
, "0x", NULL
)) {
1225 ret
= strtoul(key
, &endp
, 0);
1226 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1232 #define MAX_KEYCODES 16
1233 static uint8_t keycodes
[MAX_KEYCODES
];
1234 static int nb_pending_keycodes
;
1235 static QEMUTimer
*key_timer
;
1237 static void release_keys(void *opaque
)
1241 while (nb_pending_keycodes
> 0) {
1242 nb_pending_keycodes
--;
1243 keycode
= keycodes
[nb_pending_keycodes
];
1245 kbd_put_keycode(0xe0);
1246 kbd_put_keycode(keycode
| 0x80);
1250 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1252 char keyname_buf
[16];
1254 int keyname_len
, keycode
, i
;
1255 const char *string
= qdict_get_str(qdict
, "string");
1256 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1257 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1259 if (nb_pending_keycodes
> 0) {
1260 qemu_del_timer(key_timer
);
1267 separator
= strchr(string
, '-');
1268 keyname_len
= separator
? separator
- string
: strlen(string
);
1269 if (keyname_len
> 0) {
1270 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1271 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1272 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1275 if (i
== MAX_KEYCODES
) {
1276 monitor_printf(mon
, "too many keys\n");
1279 keyname_buf
[keyname_len
] = 0;
1280 keycode
= get_keycode(keyname_buf
);
1282 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1285 keycodes
[i
++] = keycode
;
1289 string
= separator
+ 1;
1291 nb_pending_keycodes
= i
;
1292 /* key down events */
1293 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1294 keycode
= keycodes
[i
];
1296 kbd_put_keycode(0xe0);
1297 kbd_put_keycode(keycode
& 0x7f);
1299 /* delayed key up events */
1300 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1301 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1304 static int mouse_button_state
;
1306 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1309 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1310 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1311 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1312 dx
= strtol(dx_str
, NULL
, 0);
1313 dy
= strtol(dy_str
, NULL
, 0);
1316 dz
= strtol(dz_str
, NULL
, 0);
1317 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1320 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1322 int button_state
= qdict_get_int(qdict
, "button_state");
1323 mouse_button_state
= button_state
;
1324 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1327 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1329 int size
= qdict_get_int(qdict
, "size");
1330 int addr
= qdict_get_int(qdict
, "addr");
1331 int has_index
= qdict_haskey(qdict
, "index");
1336 int index
= qdict_get_int(qdict
, "index");
1337 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1345 val
= cpu_inb(addr
);
1349 val
= cpu_inw(addr
);
1353 val
= cpu_inl(addr
);
1357 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1358 suffix
, addr
, size
* 2, val
);
1361 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1363 int size
= qdict_get_int(qdict
, "size");
1364 int addr
= qdict_get_int(qdict
, "addr");
1365 int val
= qdict_get_int(qdict
, "val");
1367 addr
&= IOPORTS_MASK
;
1372 cpu_outb(addr
, val
);
1375 cpu_outw(addr
, val
);
1378 cpu_outl(addr
, val
);
1383 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1386 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1388 res
= qemu_boot_set(bootdevice
);
1390 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1391 } else if (res
> 0) {
1392 monitor_printf(mon
, "setting boot device list failed\n");
1394 monitor_printf(mon
, "no function defined to set boot device list for "
1395 "this architecture\n");
1400 * do_system_reset(): Issue a machine reset
1402 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1405 qemu_system_reset_request();
1409 * do_system_powerdown(): Issue a machine powerdown
1411 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1414 qemu_system_powerdown_request();
1417 #if defined(TARGET_I386)
1418 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1420 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1423 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1424 pte
& PG_PSE_MASK
? 'P' : '-',
1425 pte
& PG_DIRTY_MASK
? 'D' : '-',
1426 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1427 pte
& PG_PCD_MASK
? 'C' : '-',
1428 pte
& PG_PWT_MASK
? 'T' : '-',
1429 pte
& PG_USER_MASK
? 'U' : '-',
1430 pte
& PG_RW_MASK
? 'W' : '-');
1433 static void tlb_info(Monitor
*mon
)
1437 uint32_t pgd
, pde
, pte
;
1439 env
= mon_get_cpu();
1443 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1444 monitor_printf(mon
, "PG disabled\n");
1447 pgd
= env
->cr
[3] & ~0xfff;
1448 for(l1
= 0; l1
< 1024; l1
++) {
1449 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1450 pde
= le32_to_cpu(pde
);
1451 if (pde
& PG_PRESENT_MASK
) {
1452 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1453 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1455 for(l2
= 0; l2
< 1024; l2
++) {
1456 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1457 (uint8_t *)&pte
, 4);
1458 pte
= le32_to_cpu(pte
);
1459 if (pte
& PG_PRESENT_MASK
) {
1460 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1470 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1471 uint32_t end
, int prot
)
1474 prot1
= *plast_prot
;
1475 if (prot
!= prot1
) {
1476 if (*pstart
!= -1) {
1477 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1478 *pstart
, end
, end
- *pstart
,
1479 prot1
& PG_USER_MASK
? 'u' : '-',
1481 prot1
& PG_RW_MASK
? 'w' : '-');
1491 static void mem_info(Monitor
*mon
)
1494 int l1
, l2
, prot
, last_prot
;
1495 uint32_t pgd
, pde
, pte
, start
, end
;
1497 env
= mon_get_cpu();
1501 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1502 monitor_printf(mon
, "PG disabled\n");
1505 pgd
= env
->cr
[3] & ~0xfff;
1508 for(l1
= 0; l1
< 1024; l1
++) {
1509 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1510 pde
= le32_to_cpu(pde
);
1512 if (pde
& PG_PRESENT_MASK
) {
1513 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1514 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1515 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1517 for(l2
= 0; l2
< 1024; l2
++) {
1518 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1519 (uint8_t *)&pte
, 4);
1520 pte
= le32_to_cpu(pte
);
1521 end
= (l1
<< 22) + (l2
<< 12);
1522 if (pte
& PG_PRESENT_MASK
) {
1523 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1527 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1532 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1538 #if defined(TARGET_SH4)
1540 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1542 monitor_printf(mon
, " tlb%i:\t"
1543 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1544 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1545 "dirty=%hhu writethrough=%hhu\n",
1547 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1548 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1552 static void tlb_info(Monitor
*mon
)
1554 CPUState
*env
= mon_get_cpu();
1557 monitor_printf (mon
, "ITLB:\n");
1558 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1559 print_tlb (mon
, i
, &env
->itlb
[i
]);
1560 monitor_printf (mon
, "UTLB:\n");
1561 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1562 print_tlb (mon
, i
, &env
->utlb
[i
]);
1567 static void do_info_kvm(Monitor
*mon
)
1570 monitor_printf(mon
, "kvm support: ");
1572 monitor_printf(mon
, "enabled\n");
1574 monitor_printf(mon
, "disabled\n");
1576 monitor_printf(mon
, "kvm support: not compiled\n");
1580 static void do_info_numa(Monitor
*mon
)
1585 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1586 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1587 monitor_printf(mon
, "node %d cpus:", i
);
1588 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1589 if (env
->numa_node
== i
) {
1590 monitor_printf(mon
, " %d", env
->cpu_index
);
1593 monitor_printf(mon
, "\n");
1594 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1599 #ifdef CONFIG_PROFILER
1604 static void do_info_profile(Monitor
*mon
)
1610 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1611 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1612 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1613 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1618 static void do_info_profile(Monitor
*mon
)
1620 monitor_printf(mon
, "Internal profiler not compiled\n");
1624 /* Capture support */
1625 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1627 static void do_info_capture(Monitor
*mon
)
1632 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1633 monitor_printf(mon
, "[%d]: ", i
);
1634 s
->ops
.info (s
->opaque
);
1639 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1642 int n
= qdict_get_int(qdict
, "n");
1645 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1647 s
->ops
.destroy (s
->opaque
);
1648 QLIST_REMOVE (s
, entries
);
1655 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1657 const char *path
= qdict_get_str(qdict
, "path");
1658 int has_freq
= qdict_haskey(qdict
, "freq");
1659 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1660 int has_bits
= qdict_haskey(qdict
, "bits");
1661 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1662 int has_channels
= qdict_haskey(qdict
, "nchannels");
1663 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1666 s
= qemu_mallocz (sizeof (*s
));
1668 freq
= has_freq
? freq
: 44100;
1669 bits
= has_bits
? bits
: 16;
1670 nchannels
= has_channels
? nchannels
: 2;
1672 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1673 monitor_printf(mon
, "Faied to add wave capture\n");
1676 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1680 #if defined(TARGET_I386)
1681 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1684 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1686 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1687 if (env
->cpu_index
== cpu_index
) {
1688 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1694 static void do_info_status(Monitor
*mon
)
1698 monitor_printf(mon
, "VM status: running (single step mode)\n");
1700 monitor_printf(mon
, "VM status: running\n");
1703 monitor_printf(mon
, "VM status: paused\n");
1707 * do_balloon(): Request VM to change its memory allocation
1709 static void do_balloon(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1711 int value
= qdict_get_int(qdict
, "value");
1712 ram_addr_t target
= value
;
1713 qemu_balloon(target
<< 20);
1716 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
1718 monitor_printf(mon
, "balloon: actual=%d\n",
1719 (int)qint_get_int(qobject_to_qint(data
)));
1723 * do_info_balloon(): Balloon information
1725 static void do_info_balloon(Monitor
*mon
, QObject
**ret_data
)
1729 actual
= qemu_balloon_status();
1730 if (kvm_enabled() && !kvm_has_sync_mmu())
1731 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
1732 else if (actual
== 0)
1733 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
1735 *ret_data
= QOBJECT(qint_from_int((int)(actual
>> 20)));
1738 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1740 qemu_acl
*acl
= qemu_acl_find(name
);
1743 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1748 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1750 const char *aclname
= qdict_get_str(qdict
, "aclname");
1751 qemu_acl
*acl
= find_acl(mon
, aclname
);
1752 qemu_acl_entry
*entry
;
1756 monitor_printf(mon
, "policy: %s\n",
1757 acl
->defaultDeny
? "deny" : "allow");
1758 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1760 monitor_printf(mon
, "%d: %s %s\n", i
,
1761 entry
->deny
? "deny" : "allow", entry
->match
);
1766 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1768 const char *aclname
= qdict_get_str(qdict
, "aclname");
1769 qemu_acl
*acl
= find_acl(mon
, aclname
);
1772 qemu_acl_reset(acl
);
1773 monitor_printf(mon
, "acl: removed all rules\n");
1777 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1779 const char *aclname
= qdict_get_str(qdict
, "aclname");
1780 const char *policy
= qdict_get_str(qdict
, "policy");
1781 qemu_acl
*acl
= find_acl(mon
, aclname
);
1784 if (strcmp(policy
, "allow") == 0) {
1785 acl
->defaultDeny
= 0;
1786 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1787 } else if (strcmp(policy
, "deny") == 0) {
1788 acl
->defaultDeny
= 1;
1789 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1791 monitor_printf(mon
, "acl: unknown policy '%s', "
1792 "expected 'deny' or 'allow'\n", policy
);
1797 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1799 const char *aclname
= qdict_get_str(qdict
, "aclname");
1800 const char *match
= qdict_get_str(qdict
, "match");
1801 const char *policy
= qdict_get_str(qdict
, "policy");
1802 int has_index
= qdict_haskey(qdict
, "index");
1803 int index
= qdict_get_try_int(qdict
, "index", -1);
1804 qemu_acl
*acl
= find_acl(mon
, aclname
);
1808 if (strcmp(policy
, "allow") == 0) {
1810 } else if (strcmp(policy
, "deny") == 0) {
1813 monitor_printf(mon
, "acl: unknown policy '%s', "
1814 "expected 'deny' or 'allow'\n", policy
);
1818 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1820 ret
= qemu_acl_append(acl
, deny
, match
);
1822 monitor_printf(mon
, "acl: unable to add acl entry\n");
1824 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1828 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1830 const char *aclname
= qdict_get_str(qdict
, "aclname");
1831 const char *match
= qdict_get_str(qdict
, "match");
1832 qemu_acl
*acl
= find_acl(mon
, aclname
);
1836 ret
= qemu_acl_remove(acl
, match
);
1838 monitor_printf(mon
, "acl: no matching acl entry\n");
1840 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1844 #if defined(TARGET_I386)
1845 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1848 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1849 int bank
= qdict_get_int(qdict
, "bank");
1850 uint64_t status
= qdict_get_int(qdict
, "status");
1851 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1852 uint64_t addr
= qdict_get_int(qdict
, "addr");
1853 uint64_t misc
= qdict_get_int(qdict
, "misc");
1855 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1856 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1857 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1863 static void do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1865 const char *fdname
= qdict_get_str(qdict
, "fdname");
1869 fd
= qemu_chr_get_msgfd(mon
->chr
);
1871 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1875 if (qemu_isdigit(fdname
[0])) {
1876 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1882 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1887 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1888 if (strcmp(monfd
->name
, fdname
) != 0) {
1897 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1898 monfd
->name
= qemu_strdup(fdname
);
1901 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1904 static void do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1906 const char *fdname
= qdict_get_str(qdict
, "fdname");
1909 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1910 if (strcmp(monfd
->name
, fdname
) != 0) {
1914 QLIST_REMOVE(monfd
, next
);
1916 qemu_free(monfd
->name
);
1921 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1925 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1927 int saved_vm_running
= vm_running
;
1928 const char *name
= qdict_get_str(qdict
, "name");
1932 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1936 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1940 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1943 if (strcmp(monfd
->name
, fdname
) != 0) {
1949 /* caller takes ownership of fd */
1950 QLIST_REMOVE(monfd
, next
);
1951 qemu_free(monfd
->name
);
1960 static const mon_cmd_t mon_cmds
[] = {
1961 #include "qemu-monitor.h"
1965 /* Please update qemu-monitor.hx when adding or changing commands */
1966 static const mon_cmd_t info_cmds
[] = {
1971 .help
= "show the version of QEMU",
1972 .user_print
= monitor_print_qobject
,
1973 .mhandler
.info_new
= do_info_version
,
1979 .help
= "show the network state",
1980 .mhandler
.info
= do_info_network
,
1986 .help
= "show the character devices",
1987 .mhandler
.info
= qemu_chr_info
,
1993 .help
= "show the block devices",
1994 .mhandler
.info
= bdrv_info
,
1997 .name
= "blockstats",
2000 .help
= "show block device statistics",
2001 .mhandler
.info
= bdrv_info_stats
,
2004 .name
= "registers",
2007 .help
= "show the cpu registers",
2008 .mhandler
.info
= do_info_registers
,
2014 .help
= "show infos for each CPU",
2015 .user_print
= monitor_print_cpus
,
2016 .mhandler
.info_new
= do_info_cpus
,
2022 .help
= "show the command line history",
2023 .mhandler
.info
= do_info_history
,
2029 .help
= "show the interrupts statistics (if available)",
2030 .mhandler
.info
= irq_info
,
2036 .help
= "show i8259 (PIC) state",
2037 .mhandler
.info
= pic_info
,
2043 .help
= "show PCI info",
2044 .mhandler
.info
= pci_info
,
2046 #if defined(TARGET_I386) || defined(TARGET_SH4)
2051 .help
= "show virtual to physical memory mappings",
2052 .mhandler
.info
= tlb_info
,
2055 #if defined(TARGET_I386)
2060 .help
= "show the active virtual memory mappings",
2061 .mhandler
.info
= mem_info
,
2067 .help
= "show state of HPET",
2068 .mhandler
.info
= do_info_hpet
,
2075 .help
= "show dynamic compiler info",
2076 .mhandler
.info
= do_info_jit
,
2082 .help
= "show KVM information",
2083 .mhandler
.info
= do_info_kvm
,
2089 .help
= "show NUMA information",
2090 .mhandler
.info
= do_info_numa
,
2096 .help
= "show guest USB devices",
2097 .mhandler
.info
= usb_info
,
2103 .help
= "show host USB devices",
2104 .mhandler
.info
= usb_host_info
,
2110 .help
= "show profiling information",
2111 .mhandler
.info
= do_info_profile
,
2117 .help
= "show capture information",
2118 .mhandler
.info
= do_info_capture
,
2121 .name
= "snapshots",
2124 .help
= "show the currently saved VM snapshots",
2125 .mhandler
.info
= do_info_snapshots
,
2131 .help
= "show the current VM status (running|paused)",
2132 .mhandler
.info
= do_info_status
,
2138 .help
= "show guest PCMCIA status",
2139 .mhandler
.info
= pcmcia_info
,
2145 .help
= "show which guest mouse is receiving events",
2146 .mhandler
.info
= do_info_mice
,
2152 .help
= "show the vnc server status",
2153 .mhandler
.info
= do_info_vnc
,
2159 .help
= "show the current VM name",
2160 .mhandler
.info
= do_info_name
,
2166 .help
= "show the current VM UUID",
2167 .mhandler
.info
= do_info_uuid
,
2169 #if defined(TARGET_PPC)
2174 .help
= "show CPU statistics",
2175 .mhandler
.info
= do_info_cpu_stats
,
2178 #if defined(CONFIG_SLIRP)
2183 .help
= "show user network stack connection states",
2184 .mhandler
.info
= do_info_usernet
,
2191 .help
= "show migration status",
2192 .mhandler
.info
= do_info_migrate
,
2198 .help
= "show balloon information",
2199 .user_print
= monitor_print_balloon
,
2200 .mhandler
.info_new
= do_info_balloon
,
2206 .help
= "show device tree",
2207 .mhandler
.info
= do_info_qtree
,
2213 .help
= "show qdev device model list",
2214 .mhandler
.info
= do_info_qdm
,
2220 .help
= "show roms",
2221 .mhandler
.info
= do_info_roms
,
2228 /*******************************************************************/
2230 static const char *pch
;
2231 static jmp_buf expr_env
;
2236 typedef struct MonitorDef
{
2239 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2243 #if defined(TARGET_I386)
2244 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2246 CPUState
*env
= mon_get_cpu();
2249 return env
->eip
+ env
->segs
[R_CS
].base
;
2253 #if defined(TARGET_PPC)
2254 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2256 CPUState
*env
= mon_get_cpu();
2264 for (i
= 0; i
< 8; i
++)
2265 u
|= env
->crf
[i
] << (32 - (4 * i
));
2270 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2272 CPUState
*env
= mon_get_cpu();
2278 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2280 CPUState
*env
= mon_get_cpu();
2286 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2288 CPUState
*env
= mon_get_cpu();
2291 return cpu_ppc_load_decr(env
);
2294 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2296 CPUState
*env
= mon_get_cpu();
2299 return cpu_ppc_load_tbu(env
);
2302 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2304 CPUState
*env
= mon_get_cpu();
2307 return cpu_ppc_load_tbl(env
);
2311 #if defined(TARGET_SPARC)
2312 #ifndef TARGET_SPARC64
2313 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2315 CPUState
*env
= mon_get_cpu();
2318 return GET_PSR(env
);
2322 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2324 CPUState
*env
= mon_get_cpu();
2327 return env
->regwptr
[val
];
2331 static const MonitorDef monitor_defs
[] = {
2334 #define SEG(name, seg) \
2335 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2336 { name ".base", offsetof(CPUState, segs[seg].base) },\
2337 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2339 { "eax", offsetof(CPUState
, regs
[0]) },
2340 { "ecx", offsetof(CPUState
, regs
[1]) },
2341 { "edx", offsetof(CPUState
, regs
[2]) },
2342 { "ebx", offsetof(CPUState
, regs
[3]) },
2343 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2344 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2345 { "esi", offsetof(CPUState
, regs
[6]) },
2346 { "edi", offsetof(CPUState
, regs
[7]) },
2347 #ifdef TARGET_X86_64
2348 { "r8", offsetof(CPUState
, regs
[8]) },
2349 { "r9", offsetof(CPUState
, regs
[9]) },
2350 { "r10", offsetof(CPUState
, regs
[10]) },
2351 { "r11", offsetof(CPUState
, regs
[11]) },
2352 { "r12", offsetof(CPUState
, regs
[12]) },
2353 { "r13", offsetof(CPUState
, regs
[13]) },
2354 { "r14", offsetof(CPUState
, regs
[14]) },
2355 { "r15", offsetof(CPUState
, regs
[15]) },
2357 { "eflags", offsetof(CPUState
, eflags
) },
2358 { "eip", offsetof(CPUState
, eip
) },
2365 { "pc", 0, monitor_get_pc
, },
2366 #elif defined(TARGET_PPC)
2367 /* General purpose registers */
2368 { "r0", offsetof(CPUState
, gpr
[0]) },
2369 { "r1", offsetof(CPUState
, gpr
[1]) },
2370 { "r2", offsetof(CPUState
, gpr
[2]) },
2371 { "r3", offsetof(CPUState
, gpr
[3]) },
2372 { "r4", offsetof(CPUState
, gpr
[4]) },
2373 { "r5", offsetof(CPUState
, gpr
[5]) },
2374 { "r6", offsetof(CPUState
, gpr
[6]) },
2375 { "r7", offsetof(CPUState
, gpr
[7]) },
2376 { "r8", offsetof(CPUState
, gpr
[8]) },
2377 { "r9", offsetof(CPUState
, gpr
[9]) },
2378 { "r10", offsetof(CPUState
, gpr
[10]) },
2379 { "r11", offsetof(CPUState
, gpr
[11]) },
2380 { "r12", offsetof(CPUState
, gpr
[12]) },
2381 { "r13", offsetof(CPUState
, gpr
[13]) },
2382 { "r14", offsetof(CPUState
, gpr
[14]) },
2383 { "r15", offsetof(CPUState
, gpr
[15]) },
2384 { "r16", offsetof(CPUState
, gpr
[16]) },
2385 { "r17", offsetof(CPUState
, gpr
[17]) },
2386 { "r18", offsetof(CPUState
, gpr
[18]) },
2387 { "r19", offsetof(CPUState
, gpr
[19]) },
2388 { "r20", offsetof(CPUState
, gpr
[20]) },
2389 { "r21", offsetof(CPUState
, gpr
[21]) },
2390 { "r22", offsetof(CPUState
, gpr
[22]) },
2391 { "r23", offsetof(CPUState
, gpr
[23]) },
2392 { "r24", offsetof(CPUState
, gpr
[24]) },
2393 { "r25", offsetof(CPUState
, gpr
[25]) },
2394 { "r26", offsetof(CPUState
, gpr
[26]) },
2395 { "r27", offsetof(CPUState
, gpr
[27]) },
2396 { "r28", offsetof(CPUState
, gpr
[28]) },
2397 { "r29", offsetof(CPUState
, gpr
[29]) },
2398 { "r30", offsetof(CPUState
, gpr
[30]) },
2399 { "r31", offsetof(CPUState
, gpr
[31]) },
2400 /* Floating point registers */
2401 { "f0", offsetof(CPUState
, fpr
[0]) },
2402 { "f1", offsetof(CPUState
, fpr
[1]) },
2403 { "f2", offsetof(CPUState
, fpr
[2]) },
2404 { "f3", offsetof(CPUState
, fpr
[3]) },
2405 { "f4", offsetof(CPUState
, fpr
[4]) },
2406 { "f5", offsetof(CPUState
, fpr
[5]) },
2407 { "f6", offsetof(CPUState
, fpr
[6]) },
2408 { "f7", offsetof(CPUState
, fpr
[7]) },
2409 { "f8", offsetof(CPUState
, fpr
[8]) },
2410 { "f9", offsetof(CPUState
, fpr
[9]) },
2411 { "f10", offsetof(CPUState
, fpr
[10]) },
2412 { "f11", offsetof(CPUState
, fpr
[11]) },
2413 { "f12", offsetof(CPUState
, fpr
[12]) },
2414 { "f13", offsetof(CPUState
, fpr
[13]) },
2415 { "f14", offsetof(CPUState
, fpr
[14]) },
2416 { "f15", offsetof(CPUState
, fpr
[15]) },
2417 { "f16", offsetof(CPUState
, fpr
[16]) },
2418 { "f17", offsetof(CPUState
, fpr
[17]) },
2419 { "f18", offsetof(CPUState
, fpr
[18]) },
2420 { "f19", offsetof(CPUState
, fpr
[19]) },
2421 { "f20", offsetof(CPUState
, fpr
[20]) },
2422 { "f21", offsetof(CPUState
, fpr
[21]) },
2423 { "f22", offsetof(CPUState
, fpr
[22]) },
2424 { "f23", offsetof(CPUState
, fpr
[23]) },
2425 { "f24", offsetof(CPUState
, fpr
[24]) },
2426 { "f25", offsetof(CPUState
, fpr
[25]) },
2427 { "f26", offsetof(CPUState
, fpr
[26]) },
2428 { "f27", offsetof(CPUState
, fpr
[27]) },
2429 { "f28", offsetof(CPUState
, fpr
[28]) },
2430 { "f29", offsetof(CPUState
, fpr
[29]) },
2431 { "f30", offsetof(CPUState
, fpr
[30]) },
2432 { "f31", offsetof(CPUState
, fpr
[31]) },
2433 { "fpscr", offsetof(CPUState
, fpscr
) },
2434 /* Next instruction pointer */
2435 { "nip|pc", offsetof(CPUState
, nip
) },
2436 { "lr", offsetof(CPUState
, lr
) },
2437 { "ctr", offsetof(CPUState
, ctr
) },
2438 { "decr", 0, &monitor_get_decr
, },
2439 { "ccr", 0, &monitor_get_ccr
, },
2440 /* Machine state register */
2441 { "msr", 0, &monitor_get_msr
, },
2442 { "xer", 0, &monitor_get_xer
, },
2443 { "tbu", 0, &monitor_get_tbu
, },
2444 { "tbl", 0, &monitor_get_tbl
, },
2445 #if defined(TARGET_PPC64)
2446 /* Address space register */
2447 { "asr", offsetof(CPUState
, asr
) },
2449 /* Segment registers */
2450 { "sdr1", offsetof(CPUState
, sdr1
) },
2451 { "sr0", offsetof(CPUState
, sr
[0]) },
2452 { "sr1", offsetof(CPUState
, sr
[1]) },
2453 { "sr2", offsetof(CPUState
, sr
[2]) },
2454 { "sr3", offsetof(CPUState
, sr
[3]) },
2455 { "sr4", offsetof(CPUState
, sr
[4]) },
2456 { "sr5", offsetof(CPUState
, sr
[5]) },
2457 { "sr6", offsetof(CPUState
, sr
[6]) },
2458 { "sr7", offsetof(CPUState
, sr
[7]) },
2459 { "sr8", offsetof(CPUState
, sr
[8]) },
2460 { "sr9", offsetof(CPUState
, sr
[9]) },
2461 { "sr10", offsetof(CPUState
, sr
[10]) },
2462 { "sr11", offsetof(CPUState
, sr
[11]) },
2463 { "sr12", offsetof(CPUState
, sr
[12]) },
2464 { "sr13", offsetof(CPUState
, sr
[13]) },
2465 { "sr14", offsetof(CPUState
, sr
[14]) },
2466 { "sr15", offsetof(CPUState
, sr
[15]) },
2467 /* Too lazy to put BATs and SPRs ... */
2468 #elif defined(TARGET_SPARC)
2469 { "g0", offsetof(CPUState
, gregs
[0]) },
2470 { "g1", offsetof(CPUState
, gregs
[1]) },
2471 { "g2", offsetof(CPUState
, gregs
[2]) },
2472 { "g3", offsetof(CPUState
, gregs
[3]) },
2473 { "g4", offsetof(CPUState
, gregs
[4]) },
2474 { "g5", offsetof(CPUState
, gregs
[5]) },
2475 { "g6", offsetof(CPUState
, gregs
[6]) },
2476 { "g7", offsetof(CPUState
, gregs
[7]) },
2477 { "o0", 0, monitor_get_reg
},
2478 { "o1", 1, monitor_get_reg
},
2479 { "o2", 2, monitor_get_reg
},
2480 { "o3", 3, monitor_get_reg
},
2481 { "o4", 4, monitor_get_reg
},
2482 { "o5", 5, monitor_get_reg
},
2483 { "o6", 6, monitor_get_reg
},
2484 { "o7", 7, monitor_get_reg
},
2485 { "l0", 8, monitor_get_reg
},
2486 { "l1", 9, monitor_get_reg
},
2487 { "l2", 10, monitor_get_reg
},
2488 { "l3", 11, monitor_get_reg
},
2489 { "l4", 12, monitor_get_reg
},
2490 { "l5", 13, monitor_get_reg
},
2491 { "l6", 14, monitor_get_reg
},
2492 { "l7", 15, monitor_get_reg
},
2493 { "i0", 16, monitor_get_reg
},
2494 { "i1", 17, monitor_get_reg
},
2495 { "i2", 18, monitor_get_reg
},
2496 { "i3", 19, monitor_get_reg
},
2497 { "i4", 20, monitor_get_reg
},
2498 { "i5", 21, monitor_get_reg
},
2499 { "i6", 22, monitor_get_reg
},
2500 { "i7", 23, monitor_get_reg
},
2501 { "pc", offsetof(CPUState
, pc
) },
2502 { "npc", offsetof(CPUState
, npc
) },
2503 { "y", offsetof(CPUState
, y
) },
2504 #ifndef TARGET_SPARC64
2505 { "psr", 0, &monitor_get_psr
, },
2506 { "wim", offsetof(CPUState
, wim
) },
2508 { "tbr", offsetof(CPUState
, tbr
) },
2509 { "fsr", offsetof(CPUState
, fsr
) },
2510 { "f0", offsetof(CPUState
, fpr
[0]) },
2511 { "f1", offsetof(CPUState
, fpr
[1]) },
2512 { "f2", offsetof(CPUState
, fpr
[2]) },
2513 { "f3", offsetof(CPUState
, fpr
[3]) },
2514 { "f4", offsetof(CPUState
, fpr
[4]) },
2515 { "f5", offsetof(CPUState
, fpr
[5]) },
2516 { "f6", offsetof(CPUState
, fpr
[6]) },
2517 { "f7", offsetof(CPUState
, fpr
[7]) },
2518 { "f8", offsetof(CPUState
, fpr
[8]) },
2519 { "f9", offsetof(CPUState
, fpr
[9]) },
2520 { "f10", offsetof(CPUState
, fpr
[10]) },
2521 { "f11", offsetof(CPUState
, fpr
[11]) },
2522 { "f12", offsetof(CPUState
, fpr
[12]) },
2523 { "f13", offsetof(CPUState
, fpr
[13]) },
2524 { "f14", offsetof(CPUState
, fpr
[14]) },
2525 { "f15", offsetof(CPUState
, fpr
[15]) },
2526 { "f16", offsetof(CPUState
, fpr
[16]) },
2527 { "f17", offsetof(CPUState
, fpr
[17]) },
2528 { "f18", offsetof(CPUState
, fpr
[18]) },
2529 { "f19", offsetof(CPUState
, fpr
[19]) },
2530 { "f20", offsetof(CPUState
, fpr
[20]) },
2531 { "f21", offsetof(CPUState
, fpr
[21]) },
2532 { "f22", offsetof(CPUState
, fpr
[22]) },
2533 { "f23", offsetof(CPUState
, fpr
[23]) },
2534 { "f24", offsetof(CPUState
, fpr
[24]) },
2535 { "f25", offsetof(CPUState
, fpr
[25]) },
2536 { "f26", offsetof(CPUState
, fpr
[26]) },
2537 { "f27", offsetof(CPUState
, fpr
[27]) },
2538 { "f28", offsetof(CPUState
, fpr
[28]) },
2539 { "f29", offsetof(CPUState
, fpr
[29]) },
2540 { "f30", offsetof(CPUState
, fpr
[30]) },
2541 { "f31", offsetof(CPUState
, fpr
[31]) },
2542 #ifdef TARGET_SPARC64
2543 { "f32", offsetof(CPUState
, fpr
[32]) },
2544 { "f34", offsetof(CPUState
, fpr
[34]) },
2545 { "f36", offsetof(CPUState
, fpr
[36]) },
2546 { "f38", offsetof(CPUState
, fpr
[38]) },
2547 { "f40", offsetof(CPUState
, fpr
[40]) },
2548 { "f42", offsetof(CPUState
, fpr
[42]) },
2549 { "f44", offsetof(CPUState
, fpr
[44]) },
2550 { "f46", offsetof(CPUState
, fpr
[46]) },
2551 { "f48", offsetof(CPUState
, fpr
[48]) },
2552 { "f50", offsetof(CPUState
, fpr
[50]) },
2553 { "f52", offsetof(CPUState
, fpr
[52]) },
2554 { "f54", offsetof(CPUState
, fpr
[54]) },
2555 { "f56", offsetof(CPUState
, fpr
[56]) },
2556 { "f58", offsetof(CPUState
, fpr
[58]) },
2557 { "f60", offsetof(CPUState
, fpr
[60]) },
2558 { "f62", offsetof(CPUState
, fpr
[62]) },
2559 { "asi", offsetof(CPUState
, asi
) },
2560 { "pstate", offsetof(CPUState
, pstate
) },
2561 { "cansave", offsetof(CPUState
, cansave
) },
2562 { "canrestore", offsetof(CPUState
, canrestore
) },
2563 { "otherwin", offsetof(CPUState
, otherwin
) },
2564 { "wstate", offsetof(CPUState
, wstate
) },
2565 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2566 { "fprs", offsetof(CPUState
, fprs
) },
2572 static void expr_error(Monitor
*mon
, const char *msg
)
2574 monitor_printf(mon
, "%s\n", msg
);
2575 longjmp(expr_env
, 1);
2578 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2579 static int get_monitor_def(target_long
*pval
, const char *name
)
2581 const MonitorDef
*md
;
2584 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2585 if (compare_cmd(name
, md
->name
)) {
2586 if (md
->get_value
) {
2587 *pval
= md
->get_value(md
, md
->offset
);
2589 CPUState
*env
= mon_get_cpu();
2592 ptr
= (uint8_t *)env
+ md
->offset
;
2595 *pval
= *(int32_t *)ptr
;
2598 *pval
= *(target_long
*)ptr
;
2611 static void next(void)
2615 while (qemu_isspace(*pch
))
2620 static int64_t expr_sum(Monitor
*mon
);
2622 static int64_t expr_unary(Monitor
*mon
)
2631 n
= expr_unary(mon
);
2635 n
= -expr_unary(mon
);
2639 n
= ~expr_unary(mon
);
2645 expr_error(mon
, "')' expected");
2652 expr_error(mon
, "character constant expected");
2656 expr_error(mon
, "missing terminating \' character");
2666 while ((*pch
>= 'a' && *pch
<= 'z') ||
2667 (*pch
>= 'A' && *pch
<= 'Z') ||
2668 (*pch
>= '0' && *pch
<= '9') ||
2669 *pch
== '_' || *pch
== '.') {
2670 if ((q
- buf
) < sizeof(buf
) - 1)
2674 while (qemu_isspace(*pch
))
2677 ret
= get_monitor_def(®
, buf
);
2679 expr_error(mon
, "unknown register");
2681 expr_error(mon
, "no cpu defined");
2686 expr_error(mon
, "unexpected end of expression");
2690 #if TARGET_PHYS_ADDR_BITS > 32
2691 n
= strtoull(pch
, &p
, 0);
2693 n
= strtoul(pch
, &p
, 0);
2696 expr_error(mon
, "invalid char in expression");
2699 while (qemu_isspace(*pch
))
2707 static int64_t expr_prod(Monitor
*mon
)
2712 val
= expr_unary(mon
);
2715 if (op
!= '*' && op
!= '/' && op
!= '%')
2718 val2
= expr_unary(mon
);
2727 expr_error(mon
, "division by zero");
2738 static int64_t expr_logic(Monitor
*mon
)
2743 val
= expr_prod(mon
);
2746 if (op
!= '&' && op
!= '|' && op
!= '^')
2749 val2
= expr_prod(mon
);
2766 static int64_t expr_sum(Monitor
*mon
)
2771 val
= expr_logic(mon
);
2774 if (op
!= '+' && op
!= '-')
2777 val2
= expr_logic(mon
);
2786 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2789 if (setjmp(expr_env
)) {
2793 while (qemu_isspace(*pch
))
2795 *pval
= expr_sum(mon
);
2800 static int get_str(char *buf
, int buf_size
, const char **pp
)
2808 while (qemu_isspace(*p
))
2818 while (*p
!= '\0' && *p
!= '\"') {
2834 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2837 if ((q
- buf
) < buf_size
- 1) {
2841 if ((q
- buf
) < buf_size
- 1) {
2848 qemu_printf("unterminated string\n");
2853 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2854 if ((q
- buf
) < buf_size
- 1) {
2866 * Store the command-name in cmdname, and return a pointer to
2867 * the remaining of the command string.
2869 static const char *get_command_name(const char *cmdline
,
2870 char *cmdname
, size_t nlen
)
2873 const char *p
, *pstart
;
2876 while (qemu_isspace(*p
))
2881 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2886 memcpy(cmdname
, pstart
, len
);
2887 cmdname
[len
] = '\0';
2892 * Read key of 'type' into 'key' and return the current
2895 static char *key_get_info(const char *type
, char **key
)
2903 p
= strchr(type
, ':');
2910 str
= qemu_malloc(len
+ 1);
2911 memcpy(str
, type
, len
);
2918 static int default_fmt_format
= 'x';
2919 static int default_fmt_size
= 4;
2923 static int is_valid_option(const char *c
, const char *typestr
)
2931 typestr
= strstr(typestr
, option
);
2932 return (typestr
!= NULL
);
2935 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2936 const char *cmdline
,
2939 const char *p
, *typestr
;
2941 const mon_cmd_t
*cmd
;
2947 monitor_printf(mon
, "command='%s'\n", cmdline
);
2950 /* extract the command name */
2951 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2955 /* find the command */
2956 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2957 if (compare_cmd(cmdname
, cmd
->name
))
2961 if (cmd
->name
== NULL
) {
2962 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2966 /* parse the parameters */
2967 typestr
= cmd
->args_type
;
2969 typestr
= key_get_info(typestr
, &key
);
2981 while (qemu_isspace(*p
))
2983 if (*typestr
== '?') {
2986 /* no optional string: NULL argument */
2990 ret
= get_str(buf
, sizeof(buf
), &p
);
2994 monitor_printf(mon
, "%s: filename expected\n",
2998 monitor_printf(mon
, "%s: block device name expected\n",
3002 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3007 qdict_put(qdict
, key
, qstring_from_str(buf
));
3012 int count
, format
, size
;
3014 while (qemu_isspace(*p
))
3020 if (qemu_isdigit(*p
)) {
3022 while (qemu_isdigit(*p
)) {
3023 count
= count
* 10 + (*p
- '0');
3061 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3062 monitor_printf(mon
, "invalid char in format: '%c'\n",
3067 format
= default_fmt_format
;
3068 if (format
!= 'i') {
3069 /* for 'i', not specifying a size gives -1 as size */
3071 size
= default_fmt_size
;
3072 default_fmt_size
= size
;
3074 default_fmt_format
= format
;
3077 format
= default_fmt_format
;
3078 if (format
!= 'i') {
3079 size
= default_fmt_size
;
3084 qdict_put(qdict
, "count", qint_from_int(count
));
3085 qdict_put(qdict
, "format", qint_from_int(format
));
3086 qdict_put(qdict
, "size", qint_from_int(size
));
3094 while (qemu_isspace(*p
))
3096 if (*typestr
== '?' || *typestr
== '.') {
3097 if (*typestr
== '?') {
3105 while (qemu_isspace(*p
))
3114 if (get_expr(mon
, &val
, &p
))
3116 /* Check if 'i' is greater than 32-bit */
3117 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3118 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3119 monitor_printf(mon
, "integer is for 32-bit values\n");
3122 qdict_put(qdict
, key
, qint_from_int(val
));
3127 const char *tmp
= p
;
3128 int has_option
, skip_key
= 0;
3134 while (qemu_isspace(*p
))
3140 if(!is_valid_option(p
, typestr
)) {
3142 monitor_printf(mon
, "%s: unsupported option -%c\n",
3156 qdict_put(qdict
, key
, qint_from_int(has_option
));
3161 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3167 /* check that all arguments were parsed */
3168 while (qemu_isspace(*p
))
3171 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3183 static void monitor_print_error(Monitor
*mon
)
3185 qerror_print(mon
->error
);
3186 QDECREF(mon
->error
);
3190 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3193 const mon_cmd_t
*cmd
;
3195 qdict
= qdict_new();
3197 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3201 qemu_errors_to_mon(mon
);
3203 if (monitor_handler_ported(cmd
)) {
3204 QObject
*data
= NULL
;
3206 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3208 cmd
->user_print(mon
, data
);
3210 qobject_decref(data
);
3212 cmd
->mhandler
.cmd(mon
, qdict
);
3215 if (monitor_has_error(mon
))
3216 monitor_print_error(mon
);
3218 qemu_errors_to_previous();
3224 static void cmd_completion(const char *name
, const char *list
)
3226 const char *p
, *pstart
;
3235 p
= pstart
+ strlen(pstart
);
3237 if (len
> sizeof(cmd
) - 2)
3238 len
= sizeof(cmd
) - 2;
3239 memcpy(cmd
, pstart
, len
);
3241 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3242 readline_add_completion(cur_mon
->rs
, cmd
);
3250 static void file_completion(const char *input
)
3255 char file
[1024], file_prefix
[1024];
3259 p
= strrchr(input
, '/');
3262 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3263 pstrcpy(path
, sizeof(path
), ".");
3265 input_path_len
= p
- input
+ 1;
3266 memcpy(path
, input
, input_path_len
);
3267 if (input_path_len
> sizeof(path
) - 1)
3268 input_path_len
= sizeof(path
) - 1;
3269 path
[input_path_len
] = '\0';
3270 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3272 #ifdef DEBUG_COMPLETION
3273 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3274 input
, path
, file_prefix
);
3276 ffs
= opendir(path
);
3284 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3285 memcpy(file
, input
, input_path_len
);
3286 if (input_path_len
< sizeof(file
))
3287 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3289 /* stat the file to find out if it's a directory.
3290 * In that case add a slash to speed up typing long paths
3293 if(S_ISDIR(sb
.st_mode
))
3294 pstrcat(file
, sizeof(file
), "/");
3295 readline_add_completion(cur_mon
->rs
, file
);
3301 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3303 const char *name
= bdrv_get_device_name(bs
);
3304 const char *input
= opaque
;
3306 if (input
[0] == '\0' ||
3307 !strncmp(name
, (char *)input
, strlen(input
))) {
3308 readline_add_completion(cur_mon
->rs
, name
);
3312 /* NOTE: this parser is an approximate form of the real command parser */
3313 static void parse_cmdline(const char *cmdline
,
3314 int *pnb_args
, char **args
)
3323 while (qemu_isspace(*p
))
3327 if (nb_args
>= MAX_ARGS
)
3329 ret
= get_str(buf
, sizeof(buf
), &p
);
3330 args
[nb_args
] = qemu_strdup(buf
);
3335 *pnb_args
= nb_args
;
3338 static const char *next_arg_type(const char *typestr
)
3340 const char *p
= strchr(typestr
, ':');
3341 return (p
!= NULL
? ++p
: typestr
);
3344 static void monitor_find_completion(const char *cmdline
)
3346 const char *cmdname
;
3347 char *args
[MAX_ARGS
];
3348 int nb_args
, i
, len
;
3349 const char *ptype
, *str
;
3350 const mon_cmd_t
*cmd
;
3353 parse_cmdline(cmdline
, &nb_args
, args
);
3354 #ifdef DEBUG_COMPLETION
3355 for(i
= 0; i
< nb_args
; i
++) {
3356 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3360 /* if the line ends with a space, it means we want to complete the
3362 len
= strlen(cmdline
);
3363 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3364 if (nb_args
>= MAX_ARGS
)
3366 args
[nb_args
++] = qemu_strdup("");
3369 /* command completion */
3374 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3375 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3376 cmd_completion(cmdname
, cmd
->name
);
3379 /* find the command */
3380 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3381 if (compare_cmd(args
[0], cmd
->name
))
3386 ptype
= next_arg_type(cmd
->args_type
);
3387 for(i
= 0; i
< nb_args
- 2; i
++) {
3388 if (*ptype
!= '\0') {
3389 ptype
= next_arg_type(ptype
);
3390 while (*ptype
== '?')
3391 ptype
= next_arg_type(ptype
);
3394 str
= args
[nb_args
- 1];
3395 if (*ptype
== '-' && ptype
[1] != '\0') {
3400 /* file completion */
3401 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3402 file_completion(str
);
3405 /* block device name completion */
3406 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3407 bdrv_iterate(block_completion_it
, (void *)str
);
3410 /* XXX: more generic ? */
3411 if (!strcmp(cmd
->name
, "info")) {
3412 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3413 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3414 cmd_completion(str
, cmd
->name
);
3416 } else if (!strcmp(cmd
->name
, "sendkey")) {
3417 char *sep
= strrchr(str
, '-');
3420 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3421 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3422 cmd_completion(str
, key
->name
);
3424 } else if (!strcmp(cmd
->name
, "help|?")) {
3425 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3426 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3427 cmd_completion(str
, cmd
->name
);
3435 for(i
= 0; i
< nb_args
; i
++)
3439 static int monitor_can_read(void *opaque
)
3441 Monitor
*mon
= opaque
;
3443 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3446 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3448 Monitor
*old_mon
= cur_mon
;
3454 for (i
= 0; i
< size
; i
++)
3455 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3457 if (size
== 0 || buf
[size
- 1] != 0)
3458 monitor_printf(cur_mon
, "corrupted command\n");
3460 monitor_handle_command(cur_mon
, (char *)buf
);
3466 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3468 monitor_suspend(mon
);
3469 monitor_handle_command(mon
, cmdline
);
3470 monitor_resume(mon
);
3473 int monitor_suspend(Monitor
*mon
)
3481 void monitor_resume(Monitor
*mon
)
3485 if (--mon
->suspend_cnt
== 0)
3486 readline_show_prompt(mon
->rs
);
3489 static void monitor_event(void *opaque
, int event
)
3491 Monitor
*mon
= opaque
;
3494 case CHR_EVENT_MUX_IN
:
3496 if (mon
->reset_seen
) {
3497 readline_restart(mon
->rs
);
3498 monitor_resume(mon
);
3501 mon
->suspend_cnt
= 0;
3505 case CHR_EVENT_MUX_OUT
:
3506 if (mon
->reset_seen
) {
3507 if (mon
->suspend_cnt
== 0) {
3508 monitor_printf(mon
, "\n");
3511 monitor_suspend(mon
);
3518 case CHR_EVENT_OPENED
:
3519 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3520 "information\n", QEMU_VERSION
);
3521 if (!mon
->mux_out
) {
3522 readline_show_prompt(mon
->rs
);
3524 mon
->reset_seen
= 1;
3538 void monitor_init(CharDriverState
*chr
, int flags
)
3540 static int is_first_init
= 1;
3543 if (is_first_init
) {
3544 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3548 mon
= qemu_mallocz(sizeof(*mon
));
3552 if (flags
& MONITOR_USE_READLINE
) {
3553 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3554 monitor_read_command(mon
, 0);
3557 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3560 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3561 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3565 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3567 BlockDriverState
*bs
= opaque
;
3570 if (bdrv_set_key(bs
, password
) != 0) {
3571 monitor_printf(mon
, "invalid password\n");
3574 if (mon
->password_completion_cb
)
3575 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3577 monitor_read_command(mon
, 1);
3580 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3581 BlockDriverCompletionFunc
*completion_cb
,
3586 if (!bdrv_key_required(bs
)) {
3588 completion_cb(opaque
, 0);
3592 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3593 bdrv_get_encrypted_filename(bs
));
3595 mon
->password_completion_cb
= completion_cb
;
3596 mon
->password_opaque
= opaque
;
3598 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3600 if (err
&& completion_cb
)
3601 completion_cb(opaque
, err
);
3604 typedef struct QemuErrorSink QemuErrorSink
;
3605 struct QemuErrorSink
{
3614 QemuErrorSink
*previous
;
3617 static QemuErrorSink
*qemu_error_sink
;
3619 void qemu_errors_to_file(FILE *fp
)
3621 QemuErrorSink
*sink
;
3623 sink
= qemu_mallocz(sizeof(*sink
));
3624 sink
->dest
= ERR_SINK_FILE
;
3626 sink
->previous
= qemu_error_sink
;
3627 qemu_error_sink
= sink
;
3630 void qemu_errors_to_mon(Monitor
*mon
)
3632 QemuErrorSink
*sink
;
3634 sink
= qemu_mallocz(sizeof(*sink
));
3635 sink
->dest
= ERR_SINK_MONITOR
;
3637 sink
->previous
= qemu_error_sink
;
3638 qemu_error_sink
= sink
;
3641 void qemu_errors_to_previous(void)
3643 QemuErrorSink
*sink
;
3645 assert(qemu_error_sink
!= NULL
);
3646 sink
= qemu_error_sink
;
3647 qemu_error_sink
= sink
->previous
;
3651 void qemu_error(const char *fmt
, ...)
3655 assert(qemu_error_sink
!= NULL
);
3656 switch (qemu_error_sink
->dest
) {
3658 va_start(args
, fmt
);
3659 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3662 case ERR_SINK_MONITOR
:
3663 va_start(args
, fmt
);
3664 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);
3670 void qemu_error_internal(const char *file
, int linenr
, const char *func
,
3671 const char *fmt
, ...)
3676 assert(qemu_error_sink
!= NULL
);
3679 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
3682 switch (qemu_error_sink
->dest
) {
3684 qerror_print(qerror
);
3687 case ERR_SINK_MONITOR
:
3688 assert(qemu_error_sink
->mon
->error
== NULL
);
3689 qemu_error_sink
->mon
->error
= qerror
;