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"
57 //#define DEBUG_COMPLETION
63 * 'B' block device name
64 * 's' string (accept optional quote)
66 * 'l' target long (32 or 64 bit)
67 * '/' optional gdb-like print format (like "/10x")
69 * '?' optional type (for all types, except '/')
70 * '.' other form of optional type (for 'i' and 'l')
71 * '-' optional parameter (eg. '-f')
75 typedef struct mon_cmd_t
{
77 const char *args_type
;
80 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
82 void (*info
)(Monitor
*mon
);
83 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
84 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
85 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
89 /* file descriptors passed via SCM_RIGHTS */
90 typedef struct mon_fd_t mon_fd_t
;
94 QLIST_ENTRY(mon_fd_t
) next
;
103 uint8_t outbuf
[1024];
107 BlockDriverCompletionFunc
*password_completion_cb
;
108 void *password_opaque
;
109 QLIST_HEAD(,mon_fd_t
) fds
;
110 QLIST_ENTRY(Monitor
) entry
;
113 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
115 static const mon_cmd_t mon_cmds
[];
116 static const mon_cmd_t info_cmds
[];
118 Monitor
*cur_mon
= NULL
;
120 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
123 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
125 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
127 readline_show_prompt(mon
->rs
);
130 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
134 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
135 /* prompt is printed on return from the command handler */
138 monitor_printf(mon
, "terminal does not support password prompting\n");
143 void monitor_flush(Monitor
*mon
)
145 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
146 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
147 mon
->outbuf_index
= 0;
151 /* flush at every end of line or if the buffer is full */
152 static void monitor_puts(Monitor
*mon
, const char *str
)
164 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
165 mon
->outbuf
[mon
->outbuf_index
++] = c
;
166 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
172 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
175 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
176 monitor_puts(mon
, buf
);
179 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
183 monitor_vprintf(mon
, fmt
, ap
);
187 void monitor_print_filename(Monitor
*mon
, const char *filename
)
191 for (i
= 0; filename
[i
]; i
++) {
192 switch (filename
[i
]) {
196 monitor_printf(mon
, "\\%c", filename
[i
]);
199 monitor_printf(mon
, "\\t");
202 monitor_printf(mon
, "\\r");
205 monitor_printf(mon
, "\\n");
208 monitor_printf(mon
, "%c", filename
[i
]);
214 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
218 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
223 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
225 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
227 return cmd
->user_print
!= NULL
;
230 static void monitor_print_qobject(Monitor
*mon
, const QObject
*data
)
232 switch (qobject_type(data
)) {
234 monitor_printf(mon
, "%s",qstring_get_str(qobject_to_qstring(data
)));
237 monitor_printf(mon
, "%" PRId64
,qint_get_int(qobject_to_qint(data
)));
240 monitor_printf(mon
, "ERROR: unsupported type: %d",
245 monitor_puts(mon
, "\n");
248 static int compare_cmd(const char *name
, const char *list
)
250 const char *p
, *pstart
;
258 p
= pstart
+ strlen(pstart
);
259 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
268 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
269 const char *prefix
, const char *name
)
271 const mon_cmd_t
*cmd
;
273 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
274 if (!name
|| !strcmp(name
, cmd
->name
))
275 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
276 cmd
->params
, cmd
->help
);
280 static void help_cmd(Monitor
*mon
, const char *name
)
282 if (name
&& !strcmp(name
, "info")) {
283 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
285 help_cmd_dump(mon
, mon_cmds
, "", name
);
286 if (name
&& !strcmp(name
, "log")) {
287 const CPULogItem
*item
;
288 monitor_printf(mon
, "Log items (comma separated):\n");
289 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
290 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
291 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
297 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
299 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
302 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
306 const char *device
= qdict_get_str(qdict
, "device");
308 all_devices
= !strcmp(device
, "all");
309 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
311 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
313 bdrv_commit(dinfo
->bdrv
);
317 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
319 const mon_cmd_t
*cmd
;
320 const char *item
= qdict_get_try_str(qdict
, "item");
325 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
326 if (compare_cmd(item
, cmd
->name
))
330 if (cmd
->name
== NULL
)
333 if (monitor_handler_ported(cmd
)) {
334 cmd
->mhandler
.info_new(mon
, ret_data
);
336 cmd
->user_print(mon
, *ret_data
);
338 cmd
->mhandler
.info(mon
);
344 help_cmd(mon
, "info");
348 * do_info_version(): Show QEMU version
350 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
352 *ret_data
= QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION
));
355 static void do_info_name(Monitor
*mon
)
358 monitor_printf(mon
, "%s\n", qemu_name
);
361 #if defined(TARGET_I386)
362 static void do_info_hpet(Monitor
*mon
)
364 monitor_printf(mon
, "HPET is %s by QEMU\n",
365 (no_hpet
) ? "disabled" : "enabled");
369 static void do_info_uuid(Monitor
*mon
)
371 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
372 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
373 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
374 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
375 qemu_uuid
[14], qemu_uuid
[15]);
378 /* get the current CPU defined by the user */
379 static int mon_set_cpu(int cpu_index
)
383 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
384 if (env
->cpu_index
== cpu_index
) {
385 cur_mon
->mon_cpu
= env
;
392 static CPUState
*mon_get_cpu(void)
394 if (!cur_mon
->mon_cpu
) {
397 cpu_synchronize_state(cur_mon
->mon_cpu
);
398 kvm_save_mpstate(cur_mon
->mon_cpu
);
399 return cur_mon
->mon_cpu
;
402 static void do_info_registers(Monitor
*mon
)
409 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
412 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
417 static void print_cpu_iter(QObject
*obj
, void *opaque
)
421 Monitor
*mon
= opaque
;
423 assert(qobject_type(obj
) == QTYPE_QDICT
);
424 cpu
= qobject_to_qdict(obj
);
426 if (strcmp(qdict_get_str(cpu
, "current"), "yes") == 0)
429 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
431 #if defined(TARGET_I386)
432 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
433 (target_ulong
) qdict_get_int(cpu
, "pc"));
434 #elif defined(TARGET_PPC)
435 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
436 (target_long
) qdict_get_int(cpu
, "nip"));
437 #elif defined(TARGET_SPARC)
438 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
439 (target_long
) qdict_get_int(cpu
, "pc"));
440 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
441 (target_long
) qdict_get_int(cpu
, "npc"));
442 #elif defined(TARGET_MIPS)
443 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
444 (target_long
) qdict_get_int(cpu
, "PC"));
447 if (strcmp(qdict_get_str(cpu
, "halted"), "yes") == 0)
448 monitor_printf(mon
, " (halted)");
450 monitor_printf(mon
, " thread_id=%" PRId64
" ",
451 qdict_get_int(cpu
, "thread_id"));
453 monitor_printf(mon
, "\n");
456 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
460 assert(qobject_type(data
) == QTYPE_QLIST
);
461 cpu_list
= qobject_to_qlist(data
);
462 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
466 * do_info_cpus(): Show CPU information
468 * Return a QList with a QDict for each CPU.
472 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
473 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
475 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
480 cpu_list
= qlist_new();
482 /* just to set the default cpu if not already done */
485 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
487 QDict
*cpu
= qdict_new();
489 cpu_synchronize_state(env
);
490 kvm_save_mpstate(env
);
492 qdict_put(cpu
, "CPU", qint_from_int(env
->cpu_index
));
493 answer
= (env
== mon
->mon_cpu
) ? "yes" : "no";
494 qdict_put(cpu
, "current", qstring_from_str(answer
));
495 #if defined(TARGET_I386)
496 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
497 #elif defined(TARGET_PPC)
498 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
499 #elif defined(TARGET_SPARC)
500 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
501 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
502 #elif defined(TARGET_MIPS)
503 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
505 answer
= env
->halted
? "yes" : "no";
506 qdict_put(cpu
, "halted", qstring_from_str(answer
));
507 qdict_put(cpu
, "thread_id", qint_from_int(env
->thread_id
));
509 qlist_append(cpu_list
, cpu
);
512 *ret_data
= QOBJECT(cpu_list
);
515 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
517 int index
= qdict_get_int(qdict
, "index");
518 if (mon_set_cpu(index
) < 0)
519 monitor_printf(mon
, "Invalid CPU index\n");
522 static void do_cpu_set_nr(Monitor
*mon
, const QDict
*qdict
)
527 status
= qdict_get_str(qdict
, "state");
528 value
= qdict_get_int(qdict
, "cpu");
530 if (!strcmp(status
, "online"))
532 else if (!strcmp(status
, "offline"))
535 monitor_printf(mon
, "invalid status: %s\n", status
);
538 #if defined(TARGET_I386) || defined(TARGET_X86_64)
539 qemu_system_cpu_hot_add(value
, state
);
543 static void do_info_jit(Monitor
*mon
)
545 dump_exec_info((FILE *)mon
, monitor_fprintf
);
548 static void do_info_history(Monitor
*mon
)
557 str
= readline_get_history(mon
->rs
, i
);
560 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
565 #if defined(TARGET_PPC)
566 /* XXX: not implemented in other targets */
567 static void do_info_cpu_stats(Monitor
*mon
)
572 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
577 * do_quit(): Quit QEMU execution
579 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
584 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
586 if (bdrv_is_inserted(bs
)) {
588 if (!bdrv_is_removable(bs
)) {
589 monitor_printf(mon
, "device is not removable\n");
592 if (bdrv_is_locked(bs
)) {
593 monitor_printf(mon
, "device is locked\n");
602 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
604 BlockDriverState
*bs
;
605 int force
= qdict_get_int(qdict
, "force");
606 const char *filename
= qdict_get_str(qdict
, "filename");
608 bs
= bdrv_find(filename
);
610 monitor_printf(mon
, "device not found\n");
613 eject_device(mon
, bs
, force
);
616 static void do_change_block(Monitor
*mon
, const char *device
,
617 const char *filename
, const char *fmt
)
619 BlockDriverState
*bs
;
620 BlockDriver
*drv
= NULL
;
622 bs
= bdrv_find(device
);
624 monitor_printf(mon
, "device not found\n");
628 drv
= bdrv_find_format(fmt
);
630 monitor_printf(mon
, "invalid format %s\n", fmt
);
634 if (eject_device(mon
, bs
, 0) < 0)
636 bdrv_open2(bs
, filename
, 0, drv
);
637 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
640 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
643 if (vnc_display_password(NULL
, password
) < 0)
644 monitor_printf(mon
, "could not set VNC server password\n");
646 monitor_read_command(mon
, 1);
649 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
651 if (strcmp(target
, "passwd") == 0 ||
652 strcmp(target
, "password") == 0) {
655 strncpy(password
, arg
, sizeof(password
));
656 password
[sizeof(password
) - 1] = '\0';
657 change_vnc_password_cb(mon
, password
, NULL
);
659 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
662 if (vnc_display_open(NULL
, target
) < 0)
663 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
667 static void do_change(Monitor
*mon
, const QDict
*qdict
)
669 const char *device
= qdict_get_str(qdict
, "device");
670 const char *target
= qdict_get_str(qdict
, "target");
671 const char *arg
= qdict_get_try_str(qdict
, "arg");
672 if (strcmp(device
, "vnc") == 0) {
673 do_change_vnc(mon
, target
, arg
);
675 do_change_block(mon
, device
, target
, arg
);
679 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
681 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
684 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
686 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
689 static void do_log(Monitor
*mon
, const QDict
*qdict
)
692 const char *items
= qdict_get_str(qdict
, "items");
694 if (!strcmp(items
, "none")) {
697 mask
= cpu_str_to_log_mask(items
);
699 help_cmd(mon
, "log");
706 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
708 const char *option
= qdict_get_try_str(qdict
, "option");
709 if (!option
|| !strcmp(option
, "on")) {
711 } else if (!strcmp(option
, "off")) {
714 monitor_printf(mon
, "unexpected option %s\n", option
);
719 * do_stop(): Stop VM execution
721 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
723 vm_stop(EXCP_INTERRUPT
);
726 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
728 struct bdrv_iterate_context
{
734 * do_cont(): Resume emulation.
736 static void do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
738 struct bdrv_iterate_context context
= { mon
, 0 };
740 bdrv_iterate(encrypted_bdrv_it
, &context
);
741 /* only resume the vm if all keys are set and valid */
746 static void bdrv_key_cb(void *opaque
, int err
)
748 Monitor
*mon
= opaque
;
750 /* another key was set successfully, retry to continue */
752 do_cont(mon
, NULL
, NULL
);
755 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
757 struct bdrv_iterate_context
*context
= opaque
;
759 if (!context
->err
&& bdrv_key_required(bs
)) {
760 context
->err
= -EBUSY
;
761 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
766 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
768 const char *device
= qdict_get_try_str(qdict
, "device");
770 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
771 if (gdbserver_start(device
) < 0) {
772 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
774 } else if (strcmp(device
, "none") == 0) {
775 monitor_printf(mon
, "Disabled gdbserver\n");
777 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
782 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
784 const char *action
= qdict_get_str(qdict
, "action");
785 if (select_watchdog_action(action
) == -1) {
786 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
790 static void monitor_printc(Monitor
*mon
, int c
)
792 monitor_printf(mon
, "'");
795 monitor_printf(mon
, "\\'");
798 monitor_printf(mon
, "\\\\");
801 monitor_printf(mon
, "\\n");
804 monitor_printf(mon
, "\\r");
807 if (c
>= 32 && c
<= 126) {
808 monitor_printf(mon
, "%c", c
);
810 monitor_printf(mon
, "\\x%02x", c
);
814 monitor_printf(mon
, "'");
817 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
818 target_phys_addr_t addr
, int is_physical
)
821 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
829 if (!env
&& !is_physical
)
834 } else if (wsize
== 4) {
837 /* as default we use the current CS size */
841 if ((env
->efer
& MSR_EFER_LMA
) &&
842 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
846 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
851 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
860 nb_per_line
= line_size
/ wsize
;
865 max_digits
= (wsize
* 8 + 2) / 3;
869 max_digits
= (wsize
* 8) / 4;
873 max_digits
= (wsize
* 8 * 10 + 32) / 33;
882 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
884 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
889 cpu_physical_memory_rw(addr
, buf
, l
, 0);
894 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
895 monitor_printf(mon
, " Cannot access memory\n");
904 v
= ldub_raw(buf
+ i
);
907 v
= lduw_raw(buf
+ i
);
910 v
= (uint32_t)ldl_raw(buf
+ i
);
913 v
= ldq_raw(buf
+ i
);
916 monitor_printf(mon
, " ");
919 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
922 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
925 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
928 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
931 monitor_printc(mon
, v
);
936 monitor_printf(mon
, "\n");
942 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
944 int count
= qdict_get_int(qdict
, "count");
945 int format
= qdict_get_int(qdict
, "format");
946 int size
= qdict_get_int(qdict
, "size");
947 target_long addr
= qdict_get_int(qdict
, "addr");
949 memory_dump(mon
, count
, format
, size
, addr
, 0);
952 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
954 int count
= qdict_get_int(qdict
, "count");
955 int format
= qdict_get_int(qdict
, "format");
956 int size
= qdict_get_int(qdict
, "size");
957 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
959 memory_dump(mon
, count
, format
, size
, addr
, 1);
962 static void do_print(Monitor
*mon
, const QDict
*qdict
)
964 int format
= qdict_get_int(qdict
, "format");
965 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
967 #if TARGET_PHYS_ADDR_BITS == 32
970 monitor_printf(mon
, "%#o", val
);
973 monitor_printf(mon
, "%#x", val
);
976 monitor_printf(mon
, "%u", val
);
980 monitor_printf(mon
, "%d", val
);
983 monitor_printc(mon
, val
);
989 monitor_printf(mon
, "%#" PRIo64
, val
);
992 monitor_printf(mon
, "%#" PRIx64
, val
);
995 monitor_printf(mon
, "%" PRIu64
, val
);
999 monitor_printf(mon
, "%" PRId64
, val
);
1002 monitor_printc(mon
, val
);
1006 monitor_printf(mon
, "\n");
1009 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
1012 uint32_t size
= qdict_get_int(qdict
, "size");
1013 const char *filename
= qdict_get_str(qdict
, "filename");
1014 target_long addr
= qdict_get_int(qdict
, "val");
1019 env
= mon_get_cpu();
1023 f
= fopen(filename
, "wb");
1025 monitor_printf(mon
, "could not open '%s'\n", filename
);
1032 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1033 fwrite(buf
, 1, l
, f
);
1040 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
1045 uint32_t size
= qdict_get_int(qdict
, "size");
1046 const char *filename
= qdict_get_str(qdict
, "filename");
1047 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1049 f
= fopen(filename
, "wb");
1051 monitor_printf(mon
, "could not open '%s'\n", filename
);
1058 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1059 fwrite(buf
, 1, l
, f
);
1067 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1072 uint32_t start
= qdict_get_int(qdict
, "start");
1073 uint32_t size
= qdict_get_int(qdict
, "size");
1076 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1077 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1078 /* BSD sum algorithm ('sum' Unix command) */
1079 sum
= (sum
>> 1) | (sum
<< 15);
1082 monitor_printf(mon
, "%05d\n", sum
);
1090 static const KeyDef key_defs
[] = {
1092 { 0x36, "shift_r" },
1097 { 0xe4, "altgr_r" },
1117 { 0x0e, "backspace" },
1154 { 0x37, "asterisk" },
1157 { 0x3a, "caps_lock" },
1168 { 0x45, "num_lock" },
1169 { 0x46, "scroll_lock" },
1171 { 0xb5, "kp_divide" },
1172 { 0x37, "kp_multiply" },
1173 { 0x4a, "kp_subtract" },
1175 { 0x9c, "kp_enter" },
1176 { 0x53, "kp_decimal" },
1209 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1224 { 0xfe, "compose" },
1229 static int get_keycode(const char *key
)
1235 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1236 if (!strcmp(key
, p
->name
))
1239 if (strstart(key
, "0x", NULL
)) {
1240 ret
= strtoul(key
, &endp
, 0);
1241 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1247 #define MAX_KEYCODES 16
1248 static uint8_t keycodes
[MAX_KEYCODES
];
1249 static int nb_pending_keycodes
;
1250 static QEMUTimer
*key_timer
;
1252 static void release_keys(void *opaque
)
1256 while (nb_pending_keycodes
> 0) {
1257 nb_pending_keycodes
--;
1258 keycode
= keycodes
[nb_pending_keycodes
];
1260 kbd_put_keycode(0xe0);
1261 kbd_put_keycode(keycode
| 0x80);
1265 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1267 char keyname_buf
[16];
1269 int keyname_len
, keycode
, i
;
1270 const char *string
= qdict_get_str(qdict
, "string");
1271 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1272 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1274 if (nb_pending_keycodes
> 0) {
1275 qemu_del_timer(key_timer
);
1282 separator
= strchr(string
, '-');
1283 keyname_len
= separator
? separator
- string
: strlen(string
);
1284 if (keyname_len
> 0) {
1285 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1286 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1287 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1290 if (i
== MAX_KEYCODES
) {
1291 monitor_printf(mon
, "too many keys\n");
1294 keyname_buf
[keyname_len
] = 0;
1295 keycode
= get_keycode(keyname_buf
);
1297 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1300 keycodes
[i
++] = keycode
;
1304 string
= separator
+ 1;
1306 nb_pending_keycodes
= i
;
1307 /* key down events */
1308 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1309 keycode
= keycodes
[i
];
1311 kbd_put_keycode(0xe0);
1312 kbd_put_keycode(keycode
& 0x7f);
1314 /* delayed key up events */
1315 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1316 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1319 static int mouse_button_state
;
1321 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1324 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1325 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1326 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1327 dx
= strtol(dx_str
, NULL
, 0);
1328 dy
= strtol(dy_str
, NULL
, 0);
1331 dz
= strtol(dz_str
, NULL
, 0);
1332 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1335 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1337 int button_state
= qdict_get_int(qdict
, "button_state");
1338 mouse_button_state
= button_state
;
1339 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1342 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1344 int size
= qdict_get_int(qdict
, "size");
1345 int addr
= qdict_get_int(qdict
, "addr");
1346 int has_index
= qdict_haskey(qdict
, "index");
1351 int index
= qdict_get_int(qdict
, "index");
1352 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1360 val
= cpu_inb(addr
);
1364 val
= cpu_inw(addr
);
1368 val
= cpu_inl(addr
);
1372 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1373 suffix
, addr
, size
* 2, val
);
1376 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1378 int size
= qdict_get_int(qdict
, "size");
1379 int addr
= qdict_get_int(qdict
, "addr");
1380 int val
= qdict_get_int(qdict
, "val");
1382 addr
&= IOPORTS_MASK
;
1387 cpu_outb(addr
, val
);
1390 cpu_outw(addr
, val
);
1393 cpu_outl(addr
, val
);
1398 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1401 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1403 res
= qemu_boot_set(bootdevice
);
1405 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1406 } else if (res
> 0) {
1407 monitor_printf(mon
, "setting boot device list failed\n");
1409 monitor_printf(mon
, "no function defined to set boot device list for "
1410 "this architecture\n");
1415 * do_system_reset(): Issue a machine reset
1417 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1420 qemu_system_reset_request();
1424 * do_system_powerdown(): Issue a machine powerdown
1426 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1429 qemu_system_powerdown_request();
1432 #if defined(TARGET_I386)
1433 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1435 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1438 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1439 pte
& PG_PSE_MASK
? 'P' : '-',
1440 pte
& PG_DIRTY_MASK
? 'D' : '-',
1441 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1442 pte
& PG_PCD_MASK
? 'C' : '-',
1443 pte
& PG_PWT_MASK
? 'T' : '-',
1444 pte
& PG_USER_MASK
? 'U' : '-',
1445 pte
& PG_RW_MASK
? 'W' : '-');
1448 static void tlb_info(Monitor
*mon
)
1452 uint32_t pgd
, pde
, pte
;
1454 env
= mon_get_cpu();
1458 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1459 monitor_printf(mon
, "PG disabled\n");
1462 pgd
= env
->cr
[3] & ~0xfff;
1463 for(l1
= 0; l1
< 1024; l1
++) {
1464 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1465 pde
= le32_to_cpu(pde
);
1466 if (pde
& PG_PRESENT_MASK
) {
1467 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1468 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1470 for(l2
= 0; l2
< 1024; l2
++) {
1471 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1472 (uint8_t *)&pte
, 4);
1473 pte
= le32_to_cpu(pte
);
1474 if (pte
& PG_PRESENT_MASK
) {
1475 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1485 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1486 uint32_t end
, int prot
)
1489 prot1
= *plast_prot
;
1490 if (prot
!= prot1
) {
1491 if (*pstart
!= -1) {
1492 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1493 *pstart
, end
, end
- *pstart
,
1494 prot1
& PG_USER_MASK
? 'u' : '-',
1496 prot1
& PG_RW_MASK
? 'w' : '-');
1506 static void mem_info(Monitor
*mon
)
1509 int l1
, l2
, prot
, last_prot
;
1510 uint32_t pgd
, pde
, pte
, start
, end
;
1512 env
= mon_get_cpu();
1516 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1517 monitor_printf(mon
, "PG disabled\n");
1520 pgd
= env
->cr
[3] & ~0xfff;
1523 for(l1
= 0; l1
< 1024; l1
++) {
1524 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1525 pde
= le32_to_cpu(pde
);
1527 if (pde
& PG_PRESENT_MASK
) {
1528 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1529 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1530 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1532 for(l2
= 0; l2
< 1024; l2
++) {
1533 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1534 (uint8_t *)&pte
, 4);
1535 pte
= le32_to_cpu(pte
);
1536 end
= (l1
<< 22) + (l2
<< 12);
1537 if (pte
& PG_PRESENT_MASK
) {
1538 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1542 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1547 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1553 #if defined(TARGET_SH4)
1555 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1557 monitor_printf(mon
, " tlb%i:\t"
1558 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1559 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1560 "dirty=%hhu writethrough=%hhu\n",
1562 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1563 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1567 static void tlb_info(Monitor
*mon
)
1569 CPUState
*env
= mon_get_cpu();
1572 monitor_printf (mon
, "ITLB:\n");
1573 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1574 print_tlb (mon
, i
, &env
->itlb
[i
]);
1575 monitor_printf (mon
, "UTLB:\n");
1576 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1577 print_tlb (mon
, i
, &env
->utlb
[i
]);
1582 static void do_info_kvm(Monitor
*mon
)
1584 #if defined(USE_KVM) || defined(CONFIG_KVM)
1585 monitor_printf(mon
, "kvm support: ");
1587 monitor_printf(mon
, "enabled\n");
1589 monitor_printf(mon
, "disabled\n");
1591 monitor_printf(mon
, "kvm support: not compiled\n");
1595 static void do_info_numa(Monitor
*mon
)
1600 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1601 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1602 monitor_printf(mon
, "node %d cpus:", i
);
1603 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1604 if (env
->numa_node
== i
) {
1605 monitor_printf(mon
, " %d", env
->cpu_index
);
1608 monitor_printf(mon
, "\n");
1609 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1614 #ifdef CONFIG_PROFILER
1619 static void do_info_profile(Monitor
*mon
)
1625 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1626 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1627 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1628 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1633 static void do_info_profile(Monitor
*mon
)
1635 monitor_printf(mon
, "Internal profiler not compiled\n");
1639 /* Capture support */
1640 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1642 static void do_info_capture(Monitor
*mon
)
1647 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1648 monitor_printf(mon
, "[%d]: ", i
);
1649 s
->ops
.info (s
->opaque
);
1654 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1657 int n
= qdict_get_int(qdict
, "n");
1660 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1662 s
->ops
.destroy (s
->opaque
);
1663 QLIST_REMOVE (s
, entries
);
1670 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1672 const char *path
= qdict_get_str(qdict
, "path");
1673 int has_freq
= qdict_haskey(qdict
, "freq");
1674 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1675 int has_bits
= qdict_haskey(qdict
, "bits");
1676 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1677 int has_channels
= qdict_haskey(qdict
, "nchannels");
1678 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1681 s
= qemu_mallocz (sizeof (*s
));
1683 freq
= has_freq
? freq
: 44100;
1684 bits
= has_bits
? bits
: 16;
1685 nchannels
= has_channels
? nchannels
: 2;
1687 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1688 monitor_printf(mon
, "Faied to add wave capture\n");
1691 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1695 #if defined(TARGET_I386)
1696 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1699 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1701 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1702 if (env
->cpu_index
== cpu_index
) {
1704 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1706 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1712 static void do_info_status(Monitor
*mon
)
1716 monitor_printf(mon
, "VM status: running (single step mode)\n");
1718 monitor_printf(mon
, "VM status: running\n");
1721 monitor_printf(mon
, "VM status: paused\n");
1725 * do_balloon(): Request VM to change its memory allocation
1727 static void do_balloon(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1729 int value
= qdict_get_int(qdict
, "value");
1730 ram_addr_t target
= value
;
1731 qemu_balloon(target
<< 20);
1734 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
1736 monitor_printf(mon
, "balloon: actual=%d\n",
1737 (int)qint_get_int(qobject_to_qint(data
)));
1741 * do_info_balloon(): Balloon information
1743 static void do_info_balloon(Monitor
*mon
, QObject
**ret_data
)
1747 actual
= qemu_balloon_status();
1748 if (kvm_enabled() && !kvm_has_sync_mmu())
1749 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1750 "ballooning disabled\n");
1751 else if (actual
== 0)
1752 monitor_printf(mon
, "Ballooning not activated in VM\n");
1754 *ret_data
= QOBJECT(qint_from_int((int)(actual
>> 20)));
1757 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1759 qemu_acl
*acl
= qemu_acl_find(name
);
1762 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1767 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1769 const char *aclname
= qdict_get_str(qdict
, "aclname");
1770 qemu_acl
*acl
= find_acl(mon
, aclname
);
1771 qemu_acl_entry
*entry
;
1775 monitor_printf(mon
, "policy: %s\n",
1776 acl
->defaultDeny
? "deny" : "allow");
1777 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1779 monitor_printf(mon
, "%d: %s %s\n", i
,
1780 entry
->deny
? "deny" : "allow", entry
->match
);
1785 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1787 const char *aclname
= qdict_get_str(qdict
, "aclname");
1788 qemu_acl
*acl
= find_acl(mon
, aclname
);
1791 qemu_acl_reset(acl
);
1792 monitor_printf(mon
, "acl: removed all rules\n");
1796 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1798 const char *aclname
= qdict_get_str(qdict
, "aclname");
1799 const char *policy
= qdict_get_str(qdict
, "policy");
1800 qemu_acl
*acl
= find_acl(mon
, aclname
);
1803 if (strcmp(policy
, "allow") == 0) {
1804 acl
->defaultDeny
= 0;
1805 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1806 } else if (strcmp(policy
, "deny") == 0) {
1807 acl
->defaultDeny
= 1;
1808 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1810 monitor_printf(mon
, "acl: unknown policy '%s', "
1811 "expected 'deny' or 'allow'\n", policy
);
1816 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1818 const char *aclname
= qdict_get_str(qdict
, "aclname");
1819 const char *match
= qdict_get_str(qdict
, "match");
1820 const char *policy
= qdict_get_str(qdict
, "policy");
1821 int has_index
= qdict_haskey(qdict
, "index");
1822 int index
= qdict_get_try_int(qdict
, "index", -1);
1823 qemu_acl
*acl
= find_acl(mon
, aclname
);
1827 if (strcmp(policy
, "allow") == 0) {
1829 } else if (strcmp(policy
, "deny") == 0) {
1832 monitor_printf(mon
, "acl: unknown policy '%s', "
1833 "expected 'deny' or 'allow'\n", policy
);
1837 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1839 ret
= qemu_acl_append(acl
, deny
, match
);
1841 monitor_printf(mon
, "acl: unable to add acl entry\n");
1843 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1847 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1849 const char *aclname
= qdict_get_str(qdict
, "aclname");
1850 const char *match
= qdict_get_str(qdict
, "match");
1851 qemu_acl
*acl
= find_acl(mon
, aclname
);
1855 ret
= qemu_acl_remove(acl
, match
);
1857 monitor_printf(mon
, "acl: no matching acl entry\n");
1859 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1863 #if defined(TARGET_I386)
1864 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1867 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1868 int bank
= qdict_get_int(qdict
, "bank");
1869 uint64_t status
= qdict_get_int(qdict
, "status");
1870 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1871 uint64_t addr
= qdict_get_int(qdict
, "addr");
1872 uint64_t misc
= qdict_get_int(qdict
, "misc");
1874 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1875 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1876 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1882 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1884 const char *fdname
= qdict_get_str(qdict
, "fdname");
1888 fd
= qemu_chr_get_msgfd(mon
->chr
);
1890 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1894 if (qemu_isdigit(fdname
[0])) {
1895 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1901 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1906 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1907 if (strcmp(monfd
->name
, fdname
) != 0) {
1916 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1917 monfd
->name
= qemu_strdup(fdname
);
1920 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1923 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1925 const char *fdname
= qdict_get_str(qdict
, "fdname");
1928 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1929 if (strcmp(monfd
->name
, fdname
) != 0) {
1933 QLIST_REMOVE(monfd
, next
);
1935 qemu_free(monfd
->name
);
1940 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1944 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1946 int saved_vm_running
= vm_running
;
1947 const char *name
= qdict_get_str(qdict
, "name");
1951 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1955 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1959 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1962 if (strcmp(monfd
->name
, fdname
) != 0) {
1968 /* caller takes ownership of fd */
1969 QLIST_REMOVE(monfd
, next
);
1970 qemu_free(monfd
->name
);
1979 static const mon_cmd_t mon_cmds
[] = {
1980 #include "qemu-monitor.h"
1984 /* Please update qemu-monitor.hx when adding or changing commands */
1985 static const mon_cmd_t info_cmds
[] = {
1990 .help
= "show the version of QEMU",
1991 .user_print
= monitor_print_qobject
,
1992 .mhandler
.info_new
= do_info_version
,
1998 .help
= "show the network state",
1999 .mhandler
.info
= do_info_network
,
2005 .help
= "show the character devices",
2006 .mhandler
.info
= qemu_chr_info
,
2012 .help
= "show the block devices",
2013 .mhandler
.info
= bdrv_info
,
2016 .name
= "blockstats",
2019 .help
= "show block device statistics",
2020 .mhandler
.info
= bdrv_info_stats
,
2023 .name
= "registers",
2026 .help
= "show the cpu registers",
2027 .mhandler
.info
= do_info_registers
,
2033 .help
= "show infos for each CPU",
2034 .user_print
= monitor_print_cpus
,
2035 .mhandler
.info_new
= do_info_cpus
,
2041 .help
= "show the command line history",
2042 .mhandler
.info
= do_info_history
,
2048 .help
= "show the interrupts statistics (if available)",
2049 .mhandler
.info
= irq_info
,
2055 .help
= "show i8259 (PIC) state",
2056 .mhandler
.info
= pic_info
,
2062 .help
= "show PCI info",
2063 .mhandler
.info
= pci_info
,
2065 #if defined(TARGET_I386) || defined(TARGET_SH4)
2070 .help
= "show virtual to physical memory mappings",
2071 .mhandler
.info
= tlb_info
,
2074 #if defined(TARGET_I386)
2079 .help
= "show the active virtual memory mappings",
2080 .mhandler
.info
= mem_info
,
2086 .help
= "show state of HPET",
2087 .mhandler
.info
= do_info_hpet
,
2094 .help
= "show dynamic compiler info",
2095 .mhandler
.info
= do_info_jit
,
2101 .help
= "show KVM information",
2102 .mhandler
.info
= do_info_kvm
,
2108 .help
= "show NUMA information",
2109 .mhandler
.info
= do_info_numa
,
2115 .help
= "show guest USB devices",
2116 .mhandler
.info
= usb_info
,
2122 .help
= "show host USB devices",
2123 .mhandler
.info
= usb_host_info
,
2129 .help
= "show profiling information",
2130 .mhandler
.info
= do_info_profile
,
2136 .help
= "show capture information",
2137 .mhandler
.info
= do_info_capture
,
2140 .name
= "snapshots",
2143 .help
= "show the currently saved VM snapshots",
2144 .mhandler
.info
= do_info_snapshots
,
2150 .help
= "show the current VM status (running|paused)",
2151 .mhandler
.info
= do_info_status
,
2157 .help
= "show guest PCMCIA status",
2158 .mhandler
.info
= pcmcia_info
,
2164 .help
= "show which guest mouse is receiving events",
2165 .mhandler
.info
= do_info_mice
,
2171 .help
= "show the vnc server status",
2172 .mhandler
.info
= do_info_vnc
,
2178 .help
= "show the current VM name",
2179 .mhandler
.info
= do_info_name
,
2185 .help
= "show the current VM UUID",
2186 .mhandler
.info
= do_info_uuid
,
2188 #if defined(TARGET_PPC)
2193 .help
= "show CPU statistics",
2194 .mhandler
.info
= do_info_cpu_stats
,
2197 #if defined(CONFIG_SLIRP)
2202 .help
= "show user network stack connection states",
2203 .mhandler
.info
= do_info_usernet
,
2210 .help
= "show migration status",
2211 .mhandler
.info
= do_info_migrate
,
2217 .help
= "show balloon information",
2218 .user_print
= monitor_print_balloon
,
2219 .mhandler
.info_new
= do_info_balloon
,
2225 .help
= "show device tree",
2226 .mhandler
.info
= do_info_qtree
,
2232 .help
= "show qdev device model list",
2233 .mhandler
.info
= do_info_qdm
,
2239 .help
= "show roms",
2240 .mhandler
.info
= do_info_roms
,
2247 /*******************************************************************/
2249 static const char *pch
;
2250 static jmp_buf expr_env
;
2255 typedef struct MonitorDef
{
2258 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2262 #if defined(TARGET_I386)
2263 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2265 CPUState
*env
= mon_get_cpu();
2268 return env
->eip
+ env
->segs
[R_CS
].base
;
2272 #if defined(TARGET_PPC)
2273 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2275 CPUState
*env
= mon_get_cpu();
2283 for (i
= 0; i
< 8; i
++)
2284 u
|= env
->crf
[i
] << (32 - (4 * i
));
2289 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2291 CPUState
*env
= mon_get_cpu();
2297 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2299 CPUState
*env
= mon_get_cpu();
2305 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2307 CPUState
*env
= mon_get_cpu();
2310 return cpu_ppc_load_decr(env
);
2313 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2315 CPUState
*env
= mon_get_cpu();
2318 return cpu_ppc_load_tbu(env
);
2321 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2323 CPUState
*env
= mon_get_cpu();
2326 return cpu_ppc_load_tbl(env
);
2330 #if defined(TARGET_SPARC)
2331 #ifndef TARGET_SPARC64
2332 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2334 CPUState
*env
= mon_get_cpu();
2337 return GET_PSR(env
);
2341 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2343 CPUState
*env
= mon_get_cpu();
2346 return env
->regwptr
[val
];
2350 static const MonitorDef monitor_defs
[] = {
2353 #define SEG(name, seg) \
2354 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2355 { name ".base", offsetof(CPUState, segs[seg].base) },\
2356 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2358 { "eax", offsetof(CPUState
, regs
[0]) },
2359 { "ecx", offsetof(CPUState
, regs
[1]) },
2360 { "edx", offsetof(CPUState
, regs
[2]) },
2361 { "ebx", offsetof(CPUState
, regs
[3]) },
2362 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2363 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2364 { "esi", offsetof(CPUState
, regs
[6]) },
2365 { "edi", offsetof(CPUState
, regs
[7]) },
2366 #ifdef TARGET_X86_64
2367 { "r8", offsetof(CPUState
, regs
[8]) },
2368 { "r9", offsetof(CPUState
, regs
[9]) },
2369 { "r10", offsetof(CPUState
, regs
[10]) },
2370 { "r11", offsetof(CPUState
, regs
[11]) },
2371 { "r12", offsetof(CPUState
, regs
[12]) },
2372 { "r13", offsetof(CPUState
, regs
[13]) },
2373 { "r14", offsetof(CPUState
, regs
[14]) },
2374 { "r15", offsetof(CPUState
, regs
[15]) },
2376 { "eflags", offsetof(CPUState
, eflags
) },
2377 { "eip", offsetof(CPUState
, eip
) },
2384 { "pc", 0, monitor_get_pc
, },
2385 #elif defined(TARGET_PPC)
2386 /* General purpose registers */
2387 { "r0", offsetof(CPUState
, gpr
[0]) },
2388 { "r1", offsetof(CPUState
, gpr
[1]) },
2389 { "r2", offsetof(CPUState
, gpr
[2]) },
2390 { "r3", offsetof(CPUState
, gpr
[3]) },
2391 { "r4", offsetof(CPUState
, gpr
[4]) },
2392 { "r5", offsetof(CPUState
, gpr
[5]) },
2393 { "r6", offsetof(CPUState
, gpr
[6]) },
2394 { "r7", offsetof(CPUState
, gpr
[7]) },
2395 { "r8", offsetof(CPUState
, gpr
[8]) },
2396 { "r9", offsetof(CPUState
, gpr
[9]) },
2397 { "r10", offsetof(CPUState
, gpr
[10]) },
2398 { "r11", offsetof(CPUState
, gpr
[11]) },
2399 { "r12", offsetof(CPUState
, gpr
[12]) },
2400 { "r13", offsetof(CPUState
, gpr
[13]) },
2401 { "r14", offsetof(CPUState
, gpr
[14]) },
2402 { "r15", offsetof(CPUState
, gpr
[15]) },
2403 { "r16", offsetof(CPUState
, gpr
[16]) },
2404 { "r17", offsetof(CPUState
, gpr
[17]) },
2405 { "r18", offsetof(CPUState
, gpr
[18]) },
2406 { "r19", offsetof(CPUState
, gpr
[19]) },
2407 { "r20", offsetof(CPUState
, gpr
[20]) },
2408 { "r21", offsetof(CPUState
, gpr
[21]) },
2409 { "r22", offsetof(CPUState
, gpr
[22]) },
2410 { "r23", offsetof(CPUState
, gpr
[23]) },
2411 { "r24", offsetof(CPUState
, gpr
[24]) },
2412 { "r25", offsetof(CPUState
, gpr
[25]) },
2413 { "r26", offsetof(CPUState
, gpr
[26]) },
2414 { "r27", offsetof(CPUState
, gpr
[27]) },
2415 { "r28", offsetof(CPUState
, gpr
[28]) },
2416 { "r29", offsetof(CPUState
, gpr
[29]) },
2417 { "r30", offsetof(CPUState
, gpr
[30]) },
2418 { "r31", offsetof(CPUState
, gpr
[31]) },
2419 /* Floating point registers */
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 { "fpscr", offsetof(CPUState
, fpscr
) },
2453 /* Next instruction pointer */
2454 { "nip|pc", offsetof(CPUState
, nip
) },
2455 { "lr", offsetof(CPUState
, lr
) },
2456 { "ctr", offsetof(CPUState
, ctr
) },
2457 { "decr", 0, &monitor_get_decr
, },
2458 { "ccr", 0, &monitor_get_ccr
, },
2459 /* Machine state register */
2460 { "msr", 0, &monitor_get_msr
, },
2461 { "xer", 0, &monitor_get_xer
, },
2462 { "tbu", 0, &monitor_get_tbu
, },
2463 { "tbl", 0, &monitor_get_tbl
, },
2464 #if defined(TARGET_PPC64)
2465 /* Address space register */
2466 { "asr", offsetof(CPUState
, asr
) },
2468 /* Segment registers */
2469 { "sdr1", offsetof(CPUState
, sdr1
) },
2470 { "sr0", offsetof(CPUState
, sr
[0]) },
2471 { "sr1", offsetof(CPUState
, sr
[1]) },
2472 { "sr2", offsetof(CPUState
, sr
[2]) },
2473 { "sr3", offsetof(CPUState
, sr
[3]) },
2474 { "sr4", offsetof(CPUState
, sr
[4]) },
2475 { "sr5", offsetof(CPUState
, sr
[5]) },
2476 { "sr6", offsetof(CPUState
, sr
[6]) },
2477 { "sr7", offsetof(CPUState
, sr
[7]) },
2478 { "sr8", offsetof(CPUState
, sr
[8]) },
2479 { "sr9", offsetof(CPUState
, sr
[9]) },
2480 { "sr10", offsetof(CPUState
, sr
[10]) },
2481 { "sr11", offsetof(CPUState
, sr
[11]) },
2482 { "sr12", offsetof(CPUState
, sr
[12]) },
2483 { "sr13", offsetof(CPUState
, sr
[13]) },
2484 { "sr14", offsetof(CPUState
, sr
[14]) },
2485 { "sr15", offsetof(CPUState
, sr
[15]) },
2486 /* Too lazy to put BATs and SPRs ... */
2487 #elif defined(TARGET_SPARC)
2488 { "g0", offsetof(CPUState
, gregs
[0]) },
2489 { "g1", offsetof(CPUState
, gregs
[1]) },
2490 { "g2", offsetof(CPUState
, gregs
[2]) },
2491 { "g3", offsetof(CPUState
, gregs
[3]) },
2492 { "g4", offsetof(CPUState
, gregs
[4]) },
2493 { "g5", offsetof(CPUState
, gregs
[5]) },
2494 { "g6", offsetof(CPUState
, gregs
[6]) },
2495 { "g7", offsetof(CPUState
, gregs
[7]) },
2496 { "o0", 0, monitor_get_reg
},
2497 { "o1", 1, monitor_get_reg
},
2498 { "o2", 2, monitor_get_reg
},
2499 { "o3", 3, monitor_get_reg
},
2500 { "o4", 4, monitor_get_reg
},
2501 { "o5", 5, monitor_get_reg
},
2502 { "o6", 6, monitor_get_reg
},
2503 { "o7", 7, monitor_get_reg
},
2504 { "l0", 8, monitor_get_reg
},
2505 { "l1", 9, monitor_get_reg
},
2506 { "l2", 10, monitor_get_reg
},
2507 { "l3", 11, monitor_get_reg
},
2508 { "l4", 12, monitor_get_reg
},
2509 { "l5", 13, monitor_get_reg
},
2510 { "l6", 14, monitor_get_reg
},
2511 { "l7", 15, monitor_get_reg
},
2512 { "i0", 16, monitor_get_reg
},
2513 { "i1", 17, monitor_get_reg
},
2514 { "i2", 18, monitor_get_reg
},
2515 { "i3", 19, monitor_get_reg
},
2516 { "i4", 20, monitor_get_reg
},
2517 { "i5", 21, monitor_get_reg
},
2518 { "i6", 22, monitor_get_reg
},
2519 { "i7", 23, monitor_get_reg
},
2520 { "pc", offsetof(CPUState
, pc
) },
2521 { "npc", offsetof(CPUState
, npc
) },
2522 { "y", offsetof(CPUState
, y
) },
2523 #ifndef TARGET_SPARC64
2524 { "psr", 0, &monitor_get_psr
, },
2525 { "wim", offsetof(CPUState
, wim
) },
2527 { "tbr", offsetof(CPUState
, tbr
) },
2528 { "fsr", offsetof(CPUState
, fsr
) },
2529 { "f0", offsetof(CPUState
, fpr
[0]) },
2530 { "f1", offsetof(CPUState
, fpr
[1]) },
2531 { "f2", offsetof(CPUState
, fpr
[2]) },
2532 { "f3", offsetof(CPUState
, fpr
[3]) },
2533 { "f4", offsetof(CPUState
, fpr
[4]) },
2534 { "f5", offsetof(CPUState
, fpr
[5]) },
2535 { "f6", offsetof(CPUState
, fpr
[6]) },
2536 { "f7", offsetof(CPUState
, fpr
[7]) },
2537 { "f8", offsetof(CPUState
, fpr
[8]) },
2538 { "f9", offsetof(CPUState
, fpr
[9]) },
2539 { "f10", offsetof(CPUState
, fpr
[10]) },
2540 { "f11", offsetof(CPUState
, fpr
[11]) },
2541 { "f12", offsetof(CPUState
, fpr
[12]) },
2542 { "f13", offsetof(CPUState
, fpr
[13]) },
2543 { "f14", offsetof(CPUState
, fpr
[14]) },
2544 { "f15", offsetof(CPUState
, fpr
[15]) },
2545 { "f16", offsetof(CPUState
, fpr
[16]) },
2546 { "f17", offsetof(CPUState
, fpr
[17]) },
2547 { "f18", offsetof(CPUState
, fpr
[18]) },
2548 { "f19", offsetof(CPUState
, fpr
[19]) },
2549 { "f20", offsetof(CPUState
, fpr
[20]) },
2550 { "f21", offsetof(CPUState
, fpr
[21]) },
2551 { "f22", offsetof(CPUState
, fpr
[22]) },
2552 { "f23", offsetof(CPUState
, fpr
[23]) },
2553 { "f24", offsetof(CPUState
, fpr
[24]) },
2554 { "f25", offsetof(CPUState
, fpr
[25]) },
2555 { "f26", offsetof(CPUState
, fpr
[26]) },
2556 { "f27", offsetof(CPUState
, fpr
[27]) },
2557 { "f28", offsetof(CPUState
, fpr
[28]) },
2558 { "f29", offsetof(CPUState
, fpr
[29]) },
2559 { "f30", offsetof(CPUState
, fpr
[30]) },
2560 { "f31", offsetof(CPUState
, fpr
[31]) },
2561 #ifdef TARGET_SPARC64
2562 { "f32", offsetof(CPUState
, fpr
[32]) },
2563 { "f34", offsetof(CPUState
, fpr
[34]) },
2564 { "f36", offsetof(CPUState
, fpr
[36]) },
2565 { "f38", offsetof(CPUState
, fpr
[38]) },
2566 { "f40", offsetof(CPUState
, fpr
[40]) },
2567 { "f42", offsetof(CPUState
, fpr
[42]) },
2568 { "f44", offsetof(CPUState
, fpr
[44]) },
2569 { "f46", offsetof(CPUState
, fpr
[46]) },
2570 { "f48", offsetof(CPUState
, fpr
[48]) },
2571 { "f50", offsetof(CPUState
, fpr
[50]) },
2572 { "f52", offsetof(CPUState
, fpr
[52]) },
2573 { "f54", offsetof(CPUState
, fpr
[54]) },
2574 { "f56", offsetof(CPUState
, fpr
[56]) },
2575 { "f58", offsetof(CPUState
, fpr
[58]) },
2576 { "f60", offsetof(CPUState
, fpr
[60]) },
2577 { "f62", offsetof(CPUState
, fpr
[62]) },
2578 { "asi", offsetof(CPUState
, asi
) },
2579 { "pstate", offsetof(CPUState
, pstate
) },
2580 { "cansave", offsetof(CPUState
, cansave
) },
2581 { "canrestore", offsetof(CPUState
, canrestore
) },
2582 { "otherwin", offsetof(CPUState
, otherwin
) },
2583 { "wstate", offsetof(CPUState
, wstate
) },
2584 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2585 { "fprs", offsetof(CPUState
, fprs
) },
2591 static void expr_error(Monitor
*mon
, const char *msg
)
2593 monitor_printf(mon
, "%s\n", msg
);
2594 longjmp(expr_env
, 1);
2597 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2598 static int get_monitor_def(target_long
*pval
, const char *name
)
2600 const MonitorDef
*md
;
2603 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2604 if (compare_cmd(name
, md
->name
)) {
2605 if (md
->get_value
) {
2606 *pval
= md
->get_value(md
, md
->offset
);
2608 CPUState
*env
= mon_get_cpu();
2611 ptr
= (uint8_t *)env
+ md
->offset
;
2614 *pval
= *(int32_t *)ptr
;
2617 *pval
= *(target_long
*)ptr
;
2630 static void next(void)
2634 while (qemu_isspace(*pch
))
2639 static int64_t expr_sum(Monitor
*mon
);
2641 static int64_t expr_unary(Monitor
*mon
)
2650 n
= expr_unary(mon
);
2654 n
= -expr_unary(mon
);
2658 n
= ~expr_unary(mon
);
2664 expr_error(mon
, "')' expected");
2671 expr_error(mon
, "character constant expected");
2675 expr_error(mon
, "missing terminating \' character");
2685 while ((*pch
>= 'a' && *pch
<= 'z') ||
2686 (*pch
>= 'A' && *pch
<= 'Z') ||
2687 (*pch
>= '0' && *pch
<= '9') ||
2688 *pch
== '_' || *pch
== '.') {
2689 if ((q
- buf
) < sizeof(buf
) - 1)
2693 while (qemu_isspace(*pch
))
2696 ret
= get_monitor_def(®
, buf
);
2698 expr_error(mon
, "unknown register");
2700 expr_error(mon
, "no cpu defined");
2705 expr_error(mon
, "unexpected end of expression");
2709 #if TARGET_PHYS_ADDR_BITS > 32
2710 n
= strtoull(pch
, &p
, 0);
2712 n
= strtoul(pch
, &p
, 0);
2715 expr_error(mon
, "invalid char in expression");
2718 while (qemu_isspace(*pch
))
2726 static int64_t expr_prod(Monitor
*mon
)
2731 val
= expr_unary(mon
);
2734 if (op
!= '*' && op
!= '/' && op
!= '%')
2737 val2
= expr_unary(mon
);
2746 expr_error(mon
, "division by zero");
2757 static int64_t expr_logic(Monitor
*mon
)
2762 val
= expr_prod(mon
);
2765 if (op
!= '&' && op
!= '|' && op
!= '^')
2768 val2
= expr_prod(mon
);
2785 static int64_t expr_sum(Monitor
*mon
)
2790 val
= expr_logic(mon
);
2793 if (op
!= '+' && op
!= '-')
2796 val2
= expr_logic(mon
);
2805 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2808 if (setjmp(expr_env
)) {
2812 while (qemu_isspace(*pch
))
2814 *pval
= expr_sum(mon
);
2819 static int get_str(char *buf
, int buf_size
, const char **pp
)
2827 while (qemu_isspace(*p
))
2837 while (*p
!= '\0' && *p
!= '\"') {
2853 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2856 if ((q
- buf
) < buf_size
- 1) {
2860 if ((q
- buf
) < buf_size
- 1) {
2867 qemu_printf("unterminated string\n");
2872 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2873 if ((q
- buf
) < buf_size
- 1) {
2885 * Store the command-name in cmdname, and return a pointer to
2886 * the remaining of the command string.
2888 static const char *get_command_name(const char *cmdline
,
2889 char *cmdname
, size_t nlen
)
2892 const char *p
, *pstart
;
2895 while (qemu_isspace(*p
))
2900 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2905 memcpy(cmdname
, pstart
, len
);
2906 cmdname
[len
] = '\0';
2911 * Read key of 'type' into 'key' and return the current
2914 static char *key_get_info(const char *type
, char **key
)
2922 p
= strchr(type
, ':');
2929 str
= qemu_malloc(len
+ 1);
2930 memcpy(str
, type
, len
);
2937 static int default_fmt_format
= 'x';
2938 static int default_fmt_size
= 4;
2942 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2943 const char *cmdline
,
2946 const char *p
, *typestr
;
2948 const mon_cmd_t
*cmd
;
2954 monitor_printf(mon
, "command='%s'\n", cmdline
);
2957 /* extract the command name */
2958 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2962 /* find the command */
2963 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2964 if (compare_cmd(cmdname
, cmd
->name
))
2968 if (cmd
->name
== NULL
) {
2969 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2973 /* parse the parameters */
2974 typestr
= cmd
->args_type
;
2976 typestr
= key_get_info(typestr
, &key
);
2988 while (qemu_isspace(*p
))
2990 if (*typestr
== '?') {
2993 /* no optional string: NULL argument */
2997 ret
= get_str(buf
, sizeof(buf
), &p
);
3001 monitor_printf(mon
, "%s: filename expected\n",
3005 monitor_printf(mon
, "%s: block device name expected\n",
3009 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3014 qdict_put(qdict
, key
, qstring_from_str(buf
));
3019 int count
, format
, size
;
3021 while (qemu_isspace(*p
))
3027 if (qemu_isdigit(*p
)) {
3029 while (qemu_isdigit(*p
)) {
3030 count
= count
* 10 + (*p
- '0');
3068 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3069 monitor_printf(mon
, "invalid char in format: '%c'\n",
3074 format
= default_fmt_format
;
3075 if (format
!= 'i') {
3076 /* for 'i', not specifying a size gives -1 as size */
3078 size
= default_fmt_size
;
3079 default_fmt_size
= size
;
3081 default_fmt_format
= format
;
3084 format
= default_fmt_format
;
3085 if (format
!= 'i') {
3086 size
= default_fmt_size
;
3091 qdict_put(qdict
, "count", qint_from_int(count
));
3092 qdict_put(qdict
, "format", qint_from_int(format
));
3093 qdict_put(qdict
, "size", qint_from_int(size
));
3101 while (qemu_isspace(*p
))
3103 if (*typestr
== '?' || *typestr
== '.') {
3104 if (*typestr
== '?') {
3112 while (qemu_isspace(*p
))
3121 if (get_expr(mon
, &val
, &p
))
3123 /* Check if 'i' is greater than 32-bit */
3124 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3125 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3126 monitor_printf(mon
, "integer is for 32-bit values\n");
3129 qdict_put(qdict
, key
, qint_from_int(val
));
3140 while (qemu_isspace(*p
))
3146 monitor_printf(mon
, "%s: unsupported option -%c\n",
3153 qdict_put(qdict
, key
, qint_from_int(has_option
));
3158 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3164 /* check that all arguments were parsed */
3165 while (qemu_isspace(*p
))
3168 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3180 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3183 const mon_cmd_t
*cmd
;
3185 qdict
= qdict_new();
3187 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3191 qemu_errors_to_mon(mon
);
3193 if (monitor_handler_ported(cmd
)) {
3194 QObject
*data
= NULL
;
3196 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3198 cmd
->user_print(mon
, data
);
3200 qobject_decref(data
);
3202 cmd
->mhandler
.cmd(mon
, qdict
);
3205 qemu_errors_to_previous();
3211 static void cmd_completion(const char *name
, const char *list
)
3213 const char *p
, *pstart
;
3222 p
= pstart
+ strlen(pstart
);
3224 if (len
> sizeof(cmd
) - 2)
3225 len
= sizeof(cmd
) - 2;
3226 memcpy(cmd
, pstart
, len
);
3228 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3229 readline_add_completion(cur_mon
->rs
, cmd
);
3237 static void file_completion(const char *input
)
3242 char file
[1024], file_prefix
[1024];
3246 p
= strrchr(input
, '/');
3249 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3250 pstrcpy(path
, sizeof(path
), ".");
3252 input_path_len
= p
- input
+ 1;
3253 memcpy(path
, input
, input_path_len
);
3254 if (input_path_len
> sizeof(path
) - 1)
3255 input_path_len
= sizeof(path
) - 1;
3256 path
[input_path_len
] = '\0';
3257 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3259 #ifdef DEBUG_COMPLETION
3260 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3261 input
, path
, file_prefix
);
3263 ffs
= opendir(path
);
3271 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3272 memcpy(file
, input
, input_path_len
);
3273 if (input_path_len
< sizeof(file
))
3274 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3276 /* stat the file to find out if it's a directory.
3277 * In that case add a slash to speed up typing long paths
3280 if(S_ISDIR(sb
.st_mode
))
3281 pstrcat(file
, sizeof(file
), "/");
3282 readline_add_completion(cur_mon
->rs
, file
);
3288 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3290 const char *name
= bdrv_get_device_name(bs
);
3291 const char *input
= opaque
;
3293 if (input
[0] == '\0' ||
3294 !strncmp(name
, (char *)input
, strlen(input
))) {
3295 readline_add_completion(cur_mon
->rs
, name
);
3299 /* NOTE: this parser is an approximate form of the real command parser */
3300 static void parse_cmdline(const char *cmdline
,
3301 int *pnb_args
, char **args
)
3310 while (qemu_isspace(*p
))
3314 if (nb_args
>= MAX_ARGS
)
3316 ret
= get_str(buf
, sizeof(buf
), &p
);
3317 args
[nb_args
] = qemu_strdup(buf
);
3322 *pnb_args
= nb_args
;
3325 static const char *next_arg_type(const char *typestr
)
3327 const char *p
= strchr(typestr
, ':');
3328 return (p
!= NULL
? ++p
: typestr
);
3331 static void monitor_find_completion(const char *cmdline
)
3333 const char *cmdname
;
3334 char *args
[MAX_ARGS
];
3335 int nb_args
, i
, len
;
3336 const char *ptype
, *str
;
3337 const mon_cmd_t
*cmd
;
3340 parse_cmdline(cmdline
, &nb_args
, args
);
3341 #ifdef DEBUG_COMPLETION
3342 for(i
= 0; i
< nb_args
; i
++) {
3343 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3347 /* if the line ends with a space, it means we want to complete the
3349 len
= strlen(cmdline
);
3350 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3351 if (nb_args
>= MAX_ARGS
)
3353 args
[nb_args
++] = qemu_strdup("");
3356 /* command completion */
3361 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3362 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3363 cmd_completion(cmdname
, cmd
->name
);
3366 /* find the command */
3367 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3368 if (compare_cmd(args
[0], cmd
->name
))
3373 ptype
= next_arg_type(cmd
->args_type
);
3374 for(i
= 0; i
< nb_args
- 2; i
++) {
3375 if (*ptype
!= '\0') {
3376 ptype
= next_arg_type(ptype
);
3377 while (*ptype
== '?')
3378 ptype
= next_arg_type(ptype
);
3381 str
= args
[nb_args
- 1];
3382 if (*ptype
== '-' && ptype
[1] != '\0') {
3387 /* file completion */
3388 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3389 file_completion(str
);
3392 /* block device name completion */
3393 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3394 bdrv_iterate(block_completion_it
, (void *)str
);
3397 /* XXX: more generic ? */
3398 if (!strcmp(cmd
->name
, "info")) {
3399 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3400 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3401 cmd_completion(str
, cmd
->name
);
3403 } else if (!strcmp(cmd
->name
, "sendkey")) {
3404 char *sep
= strrchr(str
, '-');
3407 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3408 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3409 cmd_completion(str
, key
->name
);
3411 } else if (!strcmp(cmd
->name
, "help|?")) {
3412 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3413 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3414 cmd_completion(str
, cmd
->name
);
3422 for(i
= 0; i
< nb_args
; i
++)
3426 static int monitor_can_read(void *opaque
)
3428 Monitor
*mon
= opaque
;
3430 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3433 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3435 Monitor
*old_mon
= cur_mon
;
3441 for (i
= 0; i
< size
; i
++)
3442 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3444 if (size
== 0 || buf
[size
- 1] != 0)
3445 monitor_printf(cur_mon
, "corrupted command\n");
3447 monitor_handle_command(cur_mon
, (char *)buf
);
3453 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3455 monitor_suspend(mon
);
3456 monitor_handle_command(mon
, cmdline
);
3457 monitor_resume(mon
);
3460 int monitor_suspend(Monitor
*mon
)
3468 void monitor_resume(Monitor
*mon
)
3472 if (--mon
->suspend_cnt
== 0)
3473 readline_show_prompt(mon
->rs
);
3476 static void monitor_event(void *opaque
, int event
)
3478 Monitor
*mon
= opaque
;
3481 case CHR_EVENT_MUX_IN
:
3483 if (mon
->reset_seen
) {
3484 readline_restart(mon
->rs
);
3485 monitor_resume(mon
);
3488 mon
->suspend_cnt
= 0;
3492 case CHR_EVENT_MUX_OUT
:
3493 if (mon
->reset_seen
) {
3494 if (mon
->suspend_cnt
== 0) {
3495 monitor_printf(mon
, "\n");
3498 monitor_suspend(mon
);
3505 case CHR_EVENT_OPENED
:
3506 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3507 "information\n", QEMU_VERSION
);
3508 if (!mon
->mux_out
) {
3509 readline_show_prompt(mon
->rs
);
3511 mon
->reset_seen
= 1;
3525 void monitor_init(CharDriverState
*chr
, int flags
)
3527 static int is_first_init
= 1;
3530 if (is_first_init
) {
3531 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3535 mon
= qemu_mallocz(sizeof(*mon
));
3539 if (flags
& MONITOR_USE_READLINE
) {
3540 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3541 monitor_read_command(mon
, 0);
3544 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3547 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3548 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3552 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3554 BlockDriverState
*bs
= opaque
;
3557 if (bdrv_set_key(bs
, password
) != 0) {
3558 monitor_printf(mon
, "invalid password\n");
3561 if (mon
->password_completion_cb
)
3562 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3564 monitor_read_command(mon
, 1);
3567 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3568 BlockDriverCompletionFunc
*completion_cb
,
3573 if (!bdrv_key_required(bs
)) {
3575 completion_cb(opaque
, 0);
3579 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3580 bdrv_get_encrypted_filename(bs
));
3582 mon
->password_completion_cb
= completion_cb
;
3583 mon
->password_opaque
= opaque
;
3585 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3587 if (err
&& completion_cb
)
3588 completion_cb(opaque
, err
);
3591 typedef struct QemuErrorSink QemuErrorSink
;
3592 struct QemuErrorSink
{
3601 QemuErrorSink
*previous
;
3604 static QemuErrorSink
*qemu_error_sink
;
3606 void qemu_errors_to_file(FILE *fp
)
3608 QemuErrorSink
*sink
;
3610 sink
= qemu_mallocz(sizeof(*sink
));
3611 sink
->dest
= ERR_SINK_FILE
;
3613 sink
->previous
= qemu_error_sink
;
3614 qemu_error_sink
= sink
;
3617 void qemu_errors_to_mon(Monitor
*mon
)
3619 QemuErrorSink
*sink
;
3621 sink
= qemu_mallocz(sizeof(*sink
));
3622 sink
->dest
= ERR_SINK_MONITOR
;
3624 sink
->previous
= qemu_error_sink
;
3625 qemu_error_sink
= sink
;
3628 void qemu_errors_to_previous(void)
3630 QemuErrorSink
*sink
;
3632 assert(qemu_error_sink
!= NULL
);
3633 sink
= qemu_error_sink
;
3634 qemu_error_sink
= sink
->previous
;
3638 void qemu_error(const char *fmt
, ...)
3642 assert(qemu_error_sink
!= NULL
);
3643 switch (qemu_error_sink
->dest
) {
3645 va_start(args
, fmt
);
3646 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3649 case ERR_SINK_MONITOR
:
3650 va_start(args
, fmt
);
3651 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);