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"
56 //#define DEBUG_COMPLETION
62 * 'B' block device name
63 * 's' string (accept optional quote)
65 * 'l' target long (32 or 64 bit)
66 * '/' optional gdb-like print format (like "/10x")
68 * '?' optional type (for all types, except '/')
69 * '.' other form of optional type (for 'i' and 'l')
70 * '-' optional parameter (eg. '-f')
74 typedef struct mon_cmd_t
{
76 const char *args_type
;
79 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
81 void (*info
)(Monitor
*mon
);
82 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
83 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
84 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
88 /* file descriptors passed via SCM_RIGHTS */
89 typedef struct mon_fd_t mon_fd_t
;
93 QLIST_ENTRY(mon_fd_t
) next
;
102 uint8_t outbuf
[1024];
106 BlockDriverCompletionFunc
*password_completion_cb
;
107 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 /* Return true if in control mode, false otherwise */
124 static inline int monitor_ctrl_mode(const Monitor
*mon
)
126 return (mon
->flags
& MONITOR_USE_CONTROL
);
129 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
131 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
133 readline_show_prompt(mon
->rs
);
136 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
140 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
141 /* prompt is printed on return from the command handler */
144 monitor_printf(mon
, "terminal does not support password prompting\n");
149 void monitor_flush(Monitor
*mon
)
151 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
152 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
153 mon
->outbuf_index
= 0;
157 /* flush at every end of line or if the buffer is full */
158 static void monitor_puts(Monitor
*mon
, const char *str
)
170 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
171 mon
->outbuf
[mon
->outbuf_index
++] = c
;
172 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
178 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
181 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
182 monitor_puts(mon
, buf
);
185 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
189 monitor_vprintf(mon
, fmt
, ap
);
193 void monitor_print_filename(Monitor
*mon
, const char *filename
)
197 for (i
= 0; filename
[i
]; i
++) {
198 switch (filename
[i
]) {
202 monitor_printf(mon
, "\\%c", filename
[i
]);
205 monitor_printf(mon
, "\\t");
208 monitor_printf(mon
, "\\r");
211 monitor_printf(mon
, "\\n");
214 monitor_printf(mon
, "%c", filename
[i
]);
220 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
224 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
229 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
231 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
233 return cmd
->user_print
!= NULL
;
236 static inline int monitor_has_error(const Monitor
*mon
)
238 return mon
->error
!= NULL
;
241 static void monitor_print_qobject(Monitor
*mon
, const QObject
*data
)
243 switch (qobject_type(data
)) {
245 monitor_printf(mon
, "%s",qstring_get_str(qobject_to_qstring(data
)));
248 monitor_printf(mon
, "%" PRId64
,qint_get_int(qobject_to_qint(data
)));
251 monitor_printf(mon
, "ERROR: unsupported type: %d",
256 monitor_puts(mon
, "\n");
259 static void monitor_json_emitter(Monitor
*mon
, const QObject
*data
)
263 json
= qobject_to_json(data
);
264 assert(json
!= NULL
);
266 monitor_printf(mon
, "%s\n", qstring_get_str(json
));
270 static void monitor_protocol_emitter(Monitor
*mon
, QObject
*data
)
276 if (!monitor_has_error(mon
)) {
277 /* success response */
279 qobject_incref(data
);
280 qdict_put_obj(qmp
, "return", data
);
282 qdict_put(qmp
, "return", qstring_from_str("OK"));
286 qdict_put(qmp
, "error", mon
->error
->error
);
287 QINCREF(mon
->error
->error
);
292 monitor_json_emitter(mon
, QOBJECT(qmp
));
296 static int compare_cmd(const char *name
, const char *list
)
298 const char *p
, *pstart
;
306 p
= pstart
+ strlen(pstart
);
307 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
316 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
317 const char *prefix
, const char *name
)
319 const mon_cmd_t
*cmd
;
321 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
322 if (!name
|| !strcmp(name
, cmd
->name
))
323 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
324 cmd
->params
, cmd
->help
);
328 static void help_cmd(Monitor
*mon
, const char *name
)
330 if (name
&& !strcmp(name
, "info")) {
331 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
333 help_cmd_dump(mon
, mon_cmds
, "", name
);
334 if (name
&& !strcmp(name
, "log")) {
335 const CPULogItem
*item
;
336 monitor_printf(mon
, "Log items (comma separated):\n");
337 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
338 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
339 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
345 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
347 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
350 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
354 const char *device
= qdict_get_str(qdict
, "device");
356 all_devices
= !strcmp(device
, "all");
357 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
359 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
361 bdrv_commit(dinfo
->bdrv
);
365 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
367 const mon_cmd_t
*cmd
;
368 const char *item
= qdict_get_try_str(qdict
, "item");
371 assert(monitor_ctrl_mode(mon
) == 0);
375 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
376 if (compare_cmd(item
, cmd
->name
))
380 if (cmd
->name
== NULL
) {
381 if (monitor_ctrl_mode(mon
)) {
382 qemu_error_new(QERR_COMMAND_NOT_FOUND
, item
);
388 if (monitor_handler_ported(cmd
)) {
389 cmd
->mhandler
.info_new(mon
, ret_data
);
391 if (!monitor_ctrl_mode(mon
)) {
393 * User Protocol function is called here, Monitor Protocol is
394 * handled by monitor_call_handler()
397 cmd
->user_print(mon
, *ret_data
);
400 if (monitor_ctrl_mode(mon
)) {
401 /* handler not converted yet */
402 qemu_error_new(QERR_COMMAND_NOT_FOUND
, item
);
404 cmd
->mhandler
.info(mon
);
411 help_cmd(mon
, "info");
415 * do_info_version(): Show QEMU version
417 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
419 *ret_data
= QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION
));
422 static void do_info_name(Monitor
*mon
)
425 monitor_printf(mon
, "%s\n", qemu_name
);
429 * do_info_commands(): List QMP available commands
431 * Return a QList of QStrings.
433 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
436 const mon_cmd_t
*cmd
;
438 cmd_list
= qlist_new();
440 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
441 if (monitor_handler_ported(cmd
) && !compare_cmd(cmd
->name
, "info")) {
442 qlist_append(cmd_list
, qstring_from_str(cmd
->name
));
446 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
447 if (monitor_handler_ported(cmd
)) {
449 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
450 qlist_append(cmd_list
, qstring_from_str(buf
));
454 *ret_data
= QOBJECT(cmd_list
);
457 #if defined(TARGET_I386)
458 static void do_info_hpet(Monitor
*mon
)
460 monitor_printf(mon
, "HPET is %s by QEMU\n",
461 (no_hpet
) ? "disabled" : "enabled");
465 static void do_info_uuid(Monitor
*mon
)
467 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
468 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
469 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
470 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
471 qemu_uuid
[14], qemu_uuid
[15]);
474 /* get the current CPU defined by the user */
475 static int mon_set_cpu(int cpu_index
)
479 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
480 if (env
->cpu_index
== cpu_index
) {
481 cur_mon
->mon_cpu
= env
;
488 static CPUState
*mon_get_cpu(void)
490 if (!cur_mon
->mon_cpu
) {
493 cpu_synchronize_state(cur_mon
->mon_cpu
);
494 return cur_mon
->mon_cpu
;
497 static void do_info_registers(Monitor
*mon
)
504 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
507 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
512 static void print_cpu_iter(QObject
*obj
, void *opaque
)
516 Monitor
*mon
= opaque
;
518 assert(qobject_type(obj
) == QTYPE_QDICT
);
519 cpu
= qobject_to_qdict(obj
);
521 if (strcmp(qdict_get_str(cpu
, "current"), "yes") == 0)
524 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
526 #if defined(TARGET_I386)
527 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
528 (target_ulong
) qdict_get_int(cpu
, "pc"));
529 #elif defined(TARGET_PPC)
530 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
531 (target_long
) qdict_get_int(cpu
, "nip"));
532 #elif defined(TARGET_SPARC)
533 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
534 (target_long
) qdict_get_int(cpu
, "pc"));
535 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
536 (target_long
) qdict_get_int(cpu
, "npc"));
537 #elif defined(TARGET_MIPS)
538 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
539 (target_long
) qdict_get_int(cpu
, "PC"));
542 if (strcmp(qdict_get_str(cpu
, "halted"), "yes") == 0)
543 monitor_printf(mon
, " (halted)");
545 monitor_printf(mon
, "\n");
548 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
552 assert(qobject_type(data
) == QTYPE_QLIST
);
553 cpu_list
= qobject_to_qlist(data
);
554 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
558 * do_info_cpus(): Show CPU information
560 * Return a QList with a QDict for each CPU.
564 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
565 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
567 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
572 cpu_list
= qlist_new();
574 /* just to set the default cpu if not already done */
577 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
579 QDict
*cpu
= qdict_new();
581 cpu_synchronize_state(env
);
583 qdict_put(cpu
, "CPU", qint_from_int(env
->cpu_index
));
584 answer
= (env
== mon
->mon_cpu
) ? "yes" : "no";
585 qdict_put(cpu
, "current", qstring_from_str(answer
));
587 #if defined(TARGET_I386)
588 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
589 #elif defined(TARGET_PPC)
590 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
591 #elif defined(TARGET_SPARC)
592 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
593 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
594 #elif defined(TARGET_MIPS)
595 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
597 answer
= env
->halted
? "yes" : "no";
598 qdict_put(cpu
, "halted", qstring_from_str(answer
));
600 qlist_append(cpu_list
, cpu
);
603 *ret_data
= QOBJECT(cpu_list
);
606 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
608 int index
= qdict_get_int(qdict
, "index");
609 if (mon_set_cpu(index
) < 0)
610 monitor_printf(mon
, "Invalid CPU index\n");
613 static void do_info_jit(Monitor
*mon
)
615 dump_exec_info((FILE *)mon
, monitor_fprintf
);
618 static void do_info_history(Monitor
*mon
)
627 str
= readline_get_history(mon
->rs
, i
);
630 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
635 #if defined(TARGET_PPC)
636 /* XXX: not implemented in other targets */
637 static void do_info_cpu_stats(Monitor
*mon
)
642 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
647 * do_quit(): Quit QEMU execution
649 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
654 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
656 if (bdrv_is_inserted(bs
)) {
658 if (!bdrv_is_removable(bs
)) {
659 monitor_printf(mon
, "device is not removable\n");
662 if (bdrv_is_locked(bs
)) {
663 monitor_printf(mon
, "device is locked\n");
672 static void do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
674 BlockDriverState
*bs
;
675 int force
= qdict_get_int(qdict
, "force");
676 const char *filename
= qdict_get_str(qdict
, "filename");
678 bs
= bdrv_find(filename
);
680 monitor_printf(mon
, "device not found\n");
683 eject_device(mon
, bs
, force
);
686 static void do_change_block(Monitor
*mon
, const char *device
,
687 const char *filename
, const char *fmt
)
689 BlockDriverState
*bs
;
690 BlockDriver
*drv
= NULL
;
692 bs
= bdrv_find(device
);
694 monitor_printf(mon
, "device not found\n");
698 drv
= bdrv_find_whitelisted_format(fmt
);
700 monitor_printf(mon
, "invalid format %s\n", fmt
);
704 if (eject_device(mon
, bs
, 0) < 0)
706 bdrv_open2(bs
, filename
, 0, drv
);
707 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
710 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
713 if (vnc_display_password(NULL
, password
) < 0)
714 monitor_printf(mon
, "could not set VNC server password\n");
716 monitor_read_command(mon
, 1);
719 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
721 if (strcmp(target
, "passwd") == 0 ||
722 strcmp(target
, "password") == 0) {
725 strncpy(password
, arg
, sizeof(password
));
726 password
[sizeof(password
) - 1] = '\0';
727 change_vnc_password_cb(mon
, password
, NULL
);
729 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
732 if (vnc_display_open(NULL
, target
) < 0)
733 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
737 static void do_change(Monitor
*mon
, const QDict
*qdict
)
739 const char *device
= qdict_get_str(qdict
, "device");
740 const char *target
= qdict_get_str(qdict
, "target");
741 const char *arg
= qdict_get_try_str(qdict
, "arg");
742 if (strcmp(device
, "vnc") == 0) {
743 do_change_vnc(mon
, target
, arg
);
745 do_change_block(mon
, device
, target
, arg
);
749 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
751 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
754 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
756 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
759 static void do_log(Monitor
*mon
, const QDict
*qdict
)
762 const char *items
= qdict_get_str(qdict
, "items");
764 if (!strcmp(items
, "none")) {
767 mask
= cpu_str_to_log_mask(items
);
769 help_cmd(mon
, "log");
776 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
778 const char *option
= qdict_get_try_str(qdict
, "option");
779 if (!option
|| !strcmp(option
, "on")) {
781 } else if (!strcmp(option
, "off")) {
784 monitor_printf(mon
, "unexpected option %s\n", option
);
789 * do_stop(): Stop VM execution
791 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
793 vm_stop(EXCP_INTERRUPT
);
796 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
798 struct bdrv_iterate_context
{
804 * do_cont(): Resume emulation.
806 static void do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
808 struct bdrv_iterate_context context
= { mon
, 0 };
810 bdrv_iterate(encrypted_bdrv_it
, &context
);
811 /* only resume the vm if all keys are set and valid */
816 static void bdrv_key_cb(void *opaque
, int err
)
818 Monitor
*mon
= opaque
;
820 /* another key was set successfully, retry to continue */
822 do_cont(mon
, NULL
, NULL
);
825 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
827 struct bdrv_iterate_context
*context
= opaque
;
829 if (!context
->err
&& bdrv_key_required(bs
)) {
830 context
->err
= -EBUSY
;
831 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
836 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
838 const char *device
= qdict_get_try_str(qdict
, "device");
840 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
841 if (gdbserver_start(device
) < 0) {
842 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
844 } else if (strcmp(device
, "none") == 0) {
845 monitor_printf(mon
, "Disabled gdbserver\n");
847 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
852 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
854 const char *action
= qdict_get_str(qdict
, "action");
855 if (select_watchdog_action(action
) == -1) {
856 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
860 static void monitor_printc(Monitor
*mon
, int c
)
862 monitor_printf(mon
, "'");
865 monitor_printf(mon
, "\\'");
868 monitor_printf(mon
, "\\\\");
871 monitor_printf(mon
, "\\n");
874 monitor_printf(mon
, "\\r");
877 if (c
>= 32 && c
<= 126) {
878 monitor_printf(mon
, "%c", c
);
880 monitor_printf(mon
, "\\x%02x", c
);
884 monitor_printf(mon
, "'");
887 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
888 target_phys_addr_t addr
, int is_physical
)
891 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
899 if (!env
&& !is_physical
)
904 } else if (wsize
== 4) {
907 /* as default we use the current CS size */
911 if ((env
->efer
& MSR_EFER_LMA
) &&
912 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
916 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
921 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
930 nb_per_line
= line_size
/ wsize
;
935 max_digits
= (wsize
* 8 + 2) / 3;
939 max_digits
= (wsize
* 8) / 4;
943 max_digits
= (wsize
* 8 * 10 + 32) / 33;
952 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
954 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
959 cpu_physical_memory_rw(addr
, buf
, l
, 0);
964 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
965 monitor_printf(mon
, " Cannot access memory\n");
974 v
= ldub_raw(buf
+ i
);
977 v
= lduw_raw(buf
+ i
);
980 v
= (uint32_t)ldl_raw(buf
+ i
);
983 v
= ldq_raw(buf
+ i
);
986 monitor_printf(mon
, " ");
989 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
992 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
995 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
998 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
1001 monitor_printc(mon
, v
);
1006 monitor_printf(mon
, "\n");
1012 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1014 int count
= qdict_get_int(qdict
, "count");
1015 int format
= qdict_get_int(qdict
, "format");
1016 int size
= qdict_get_int(qdict
, "size");
1017 target_long addr
= qdict_get_int(qdict
, "addr");
1019 memory_dump(mon
, count
, format
, size
, addr
, 0);
1022 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
1024 int count
= qdict_get_int(qdict
, "count");
1025 int format
= qdict_get_int(qdict
, "format");
1026 int size
= qdict_get_int(qdict
, "size");
1027 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
1029 memory_dump(mon
, count
, format
, size
, addr
, 1);
1032 static void do_print(Monitor
*mon
, const QDict
*qdict
)
1034 int format
= qdict_get_int(qdict
, "format");
1035 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
1037 #if TARGET_PHYS_ADDR_BITS == 32
1040 monitor_printf(mon
, "%#o", val
);
1043 monitor_printf(mon
, "%#x", val
);
1046 monitor_printf(mon
, "%u", val
);
1050 monitor_printf(mon
, "%d", val
);
1053 monitor_printc(mon
, val
);
1059 monitor_printf(mon
, "%#" PRIo64
, val
);
1062 monitor_printf(mon
, "%#" PRIx64
, val
);
1065 monitor_printf(mon
, "%" PRIu64
, val
);
1069 monitor_printf(mon
, "%" PRId64
, val
);
1072 monitor_printc(mon
, val
);
1076 monitor_printf(mon
, "\n");
1079 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1082 uint32_t size
= qdict_get_int(qdict
, "size");
1083 const char *filename
= qdict_get_str(qdict
, "filename");
1084 target_long addr
= qdict_get_int(qdict
, "val");
1089 env
= mon_get_cpu();
1093 f
= fopen(filename
, "wb");
1095 monitor_printf(mon
, "could not open '%s'\n", filename
);
1102 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1103 fwrite(buf
, 1, l
, f
);
1110 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1116 uint32_t size
= qdict_get_int(qdict
, "size");
1117 const char *filename
= qdict_get_str(qdict
, "filename");
1118 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1120 f
= fopen(filename
, "wb");
1122 monitor_printf(mon
, "could not open '%s'\n", filename
);
1129 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1130 fwrite(buf
, 1, l
, f
);
1138 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1143 uint32_t start
= qdict_get_int(qdict
, "start");
1144 uint32_t size
= qdict_get_int(qdict
, "size");
1147 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1148 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1149 /* BSD sum algorithm ('sum' Unix command) */
1150 sum
= (sum
>> 1) | (sum
<< 15);
1153 monitor_printf(mon
, "%05d\n", sum
);
1161 static const KeyDef key_defs
[] = {
1163 { 0x36, "shift_r" },
1168 { 0xe4, "altgr_r" },
1188 { 0x0e, "backspace" },
1225 { 0x37, "asterisk" },
1228 { 0x3a, "caps_lock" },
1239 { 0x45, "num_lock" },
1240 { 0x46, "scroll_lock" },
1242 { 0xb5, "kp_divide" },
1243 { 0x37, "kp_multiply" },
1244 { 0x4a, "kp_subtract" },
1246 { 0x9c, "kp_enter" },
1247 { 0x53, "kp_decimal" },
1280 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1295 { 0xfe, "compose" },
1300 static int get_keycode(const char *key
)
1306 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1307 if (!strcmp(key
, p
->name
))
1310 if (strstart(key
, "0x", NULL
)) {
1311 ret
= strtoul(key
, &endp
, 0);
1312 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1318 #define MAX_KEYCODES 16
1319 static uint8_t keycodes
[MAX_KEYCODES
];
1320 static int nb_pending_keycodes
;
1321 static QEMUTimer
*key_timer
;
1323 static void release_keys(void *opaque
)
1327 while (nb_pending_keycodes
> 0) {
1328 nb_pending_keycodes
--;
1329 keycode
= keycodes
[nb_pending_keycodes
];
1331 kbd_put_keycode(0xe0);
1332 kbd_put_keycode(keycode
| 0x80);
1336 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1338 char keyname_buf
[16];
1340 int keyname_len
, keycode
, i
;
1341 const char *string
= qdict_get_str(qdict
, "string");
1342 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1343 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1345 if (nb_pending_keycodes
> 0) {
1346 qemu_del_timer(key_timer
);
1353 separator
= strchr(string
, '-');
1354 keyname_len
= separator
? separator
- string
: strlen(string
);
1355 if (keyname_len
> 0) {
1356 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1357 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1358 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1361 if (i
== MAX_KEYCODES
) {
1362 monitor_printf(mon
, "too many keys\n");
1365 keyname_buf
[keyname_len
] = 0;
1366 keycode
= get_keycode(keyname_buf
);
1368 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1371 keycodes
[i
++] = keycode
;
1375 string
= separator
+ 1;
1377 nb_pending_keycodes
= i
;
1378 /* key down events */
1379 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1380 keycode
= keycodes
[i
];
1382 kbd_put_keycode(0xe0);
1383 kbd_put_keycode(keycode
& 0x7f);
1385 /* delayed key up events */
1386 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1387 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1390 static int mouse_button_state
;
1392 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1395 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1396 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1397 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1398 dx
= strtol(dx_str
, NULL
, 0);
1399 dy
= strtol(dy_str
, NULL
, 0);
1402 dz
= strtol(dz_str
, NULL
, 0);
1403 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1406 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1408 int button_state
= qdict_get_int(qdict
, "button_state");
1409 mouse_button_state
= button_state
;
1410 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1413 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1415 int size
= qdict_get_int(qdict
, "size");
1416 int addr
= qdict_get_int(qdict
, "addr");
1417 int has_index
= qdict_haskey(qdict
, "index");
1422 int index
= qdict_get_int(qdict
, "index");
1423 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1431 val
= cpu_inb(addr
);
1435 val
= cpu_inw(addr
);
1439 val
= cpu_inl(addr
);
1443 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1444 suffix
, addr
, size
* 2, val
);
1447 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1449 int size
= qdict_get_int(qdict
, "size");
1450 int addr
= qdict_get_int(qdict
, "addr");
1451 int val
= qdict_get_int(qdict
, "val");
1453 addr
&= IOPORTS_MASK
;
1458 cpu_outb(addr
, val
);
1461 cpu_outw(addr
, val
);
1464 cpu_outl(addr
, val
);
1469 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1472 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1474 res
= qemu_boot_set(bootdevice
);
1476 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1477 } else if (res
> 0) {
1478 monitor_printf(mon
, "setting boot device list failed\n");
1480 monitor_printf(mon
, "no function defined to set boot device list for "
1481 "this architecture\n");
1486 * do_system_reset(): Issue a machine reset
1488 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1491 qemu_system_reset_request();
1495 * do_system_powerdown(): Issue a machine powerdown
1497 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1500 qemu_system_powerdown_request();
1503 #if defined(TARGET_I386)
1504 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1506 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1509 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1510 pte
& PG_PSE_MASK
? 'P' : '-',
1511 pte
& PG_DIRTY_MASK
? 'D' : '-',
1512 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1513 pte
& PG_PCD_MASK
? 'C' : '-',
1514 pte
& PG_PWT_MASK
? 'T' : '-',
1515 pte
& PG_USER_MASK
? 'U' : '-',
1516 pte
& PG_RW_MASK
? 'W' : '-');
1519 static void tlb_info(Monitor
*mon
)
1523 uint32_t pgd
, pde
, pte
;
1525 env
= mon_get_cpu();
1529 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1530 monitor_printf(mon
, "PG disabled\n");
1533 pgd
= env
->cr
[3] & ~0xfff;
1534 for(l1
= 0; l1
< 1024; l1
++) {
1535 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1536 pde
= le32_to_cpu(pde
);
1537 if (pde
& PG_PRESENT_MASK
) {
1538 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1539 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1541 for(l2
= 0; l2
< 1024; l2
++) {
1542 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1543 (uint8_t *)&pte
, 4);
1544 pte
= le32_to_cpu(pte
);
1545 if (pte
& PG_PRESENT_MASK
) {
1546 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1556 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1557 uint32_t end
, int prot
)
1560 prot1
= *plast_prot
;
1561 if (prot
!= prot1
) {
1562 if (*pstart
!= -1) {
1563 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1564 *pstart
, end
, end
- *pstart
,
1565 prot1
& PG_USER_MASK
? 'u' : '-',
1567 prot1
& PG_RW_MASK
? 'w' : '-');
1577 static void mem_info(Monitor
*mon
)
1580 int l1
, l2
, prot
, last_prot
;
1581 uint32_t pgd
, pde
, pte
, start
, end
;
1583 env
= mon_get_cpu();
1587 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1588 monitor_printf(mon
, "PG disabled\n");
1591 pgd
= env
->cr
[3] & ~0xfff;
1594 for(l1
= 0; l1
< 1024; l1
++) {
1595 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1596 pde
= le32_to_cpu(pde
);
1598 if (pde
& PG_PRESENT_MASK
) {
1599 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1600 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1601 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1603 for(l2
= 0; l2
< 1024; l2
++) {
1604 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1605 (uint8_t *)&pte
, 4);
1606 pte
= le32_to_cpu(pte
);
1607 end
= (l1
<< 22) + (l2
<< 12);
1608 if (pte
& PG_PRESENT_MASK
) {
1609 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1613 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1618 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1624 #if defined(TARGET_SH4)
1626 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1628 monitor_printf(mon
, " tlb%i:\t"
1629 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1630 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1631 "dirty=%hhu writethrough=%hhu\n",
1633 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1634 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1638 static void tlb_info(Monitor
*mon
)
1640 CPUState
*env
= mon_get_cpu();
1643 monitor_printf (mon
, "ITLB:\n");
1644 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1645 print_tlb (mon
, i
, &env
->itlb
[i
]);
1646 monitor_printf (mon
, "UTLB:\n");
1647 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1648 print_tlb (mon
, i
, &env
->utlb
[i
]);
1653 static void do_info_kvm(Monitor
*mon
)
1656 monitor_printf(mon
, "kvm support: ");
1658 monitor_printf(mon
, "enabled\n");
1660 monitor_printf(mon
, "disabled\n");
1662 monitor_printf(mon
, "kvm support: not compiled\n");
1666 static void do_info_numa(Monitor
*mon
)
1671 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1672 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1673 monitor_printf(mon
, "node %d cpus:", i
);
1674 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1675 if (env
->numa_node
== i
) {
1676 monitor_printf(mon
, " %d", env
->cpu_index
);
1679 monitor_printf(mon
, "\n");
1680 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1685 #ifdef CONFIG_PROFILER
1690 static void do_info_profile(Monitor
*mon
)
1696 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1697 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1698 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1699 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1704 static void do_info_profile(Monitor
*mon
)
1706 monitor_printf(mon
, "Internal profiler not compiled\n");
1710 /* Capture support */
1711 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1713 static void do_info_capture(Monitor
*mon
)
1718 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1719 monitor_printf(mon
, "[%d]: ", i
);
1720 s
->ops
.info (s
->opaque
);
1725 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1728 int n
= qdict_get_int(qdict
, "n");
1731 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1733 s
->ops
.destroy (s
->opaque
);
1734 QLIST_REMOVE (s
, entries
);
1741 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1743 const char *path
= qdict_get_str(qdict
, "path");
1744 int has_freq
= qdict_haskey(qdict
, "freq");
1745 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1746 int has_bits
= qdict_haskey(qdict
, "bits");
1747 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1748 int has_channels
= qdict_haskey(qdict
, "nchannels");
1749 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1752 s
= qemu_mallocz (sizeof (*s
));
1754 freq
= has_freq
? freq
: 44100;
1755 bits
= has_bits
? bits
: 16;
1756 nchannels
= has_channels
? nchannels
: 2;
1758 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1759 monitor_printf(mon
, "Faied to add wave capture\n");
1762 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1766 #if defined(TARGET_I386)
1767 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1770 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1772 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1773 if (env
->cpu_index
== cpu_index
) {
1774 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1780 static void do_info_status(Monitor
*mon
)
1784 monitor_printf(mon
, "VM status: running (single step mode)\n");
1786 monitor_printf(mon
, "VM status: running\n");
1789 monitor_printf(mon
, "VM status: paused\n");
1793 * do_balloon(): Request VM to change its memory allocation
1795 static void do_balloon(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1797 int value
= qdict_get_int(qdict
, "value");
1798 ram_addr_t target
= value
;
1799 qemu_balloon(target
<< 20);
1802 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
1804 monitor_printf(mon
, "balloon: actual=%d\n",
1805 (int)qint_get_int(qobject_to_qint(data
)));
1809 * do_info_balloon(): Balloon information
1811 static void do_info_balloon(Monitor
*mon
, QObject
**ret_data
)
1815 actual
= qemu_balloon_status();
1816 if (kvm_enabled() && !kvm_has_sync_mmu())
1817 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
1818 else if (actual
== 0)
1819 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
1821 *ret_data
= QOBJECT(qint_from_int((int)(actual
>> 20)));
1824 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1826 qemu_acl
*acl
= qemu_acl_find(name
);
1829 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1834 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1836 const char *aclname
= qdict_get_str(qdict
, "aclname");
1837 qemu_acl
*acl
= find_acl(mon
, aclname
);
1838 qemu_acl_entry
*entry
;
1842 monitor_printf(mon
, "policy: %s\n",
1843 acl
->defaultDeny
? "deny" : "allow");
1844 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1846 monitor_printf(mon
, "%d: %s %s\n", i
,
1847 entry
->deny
? "deny" : "allow", entry
->match
);
1852 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1854 const char *aclname
= qdict_get_str(qdict
, "aclname");
1855 qemu_acl
*acl
= find_acl(mon
, aclname
);
1858 qemu_acl_reset(acl
);
1859 monitor_printf(mon
, "acl: removed all rules\n");
1863 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1865 const char *aclname
= qdict_get_str(qdict
, "aclname");
1866 const char *policy
= qdict_get_str(qdict
, "policy");
1867 qemu_acl
*acl
= find_acl(mon
, aclname
);
1870 if (strcmp(policy
, "allow") == 0) {
1871 acl
->defaultDeny
= 0;
1872 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1873 } else if (strcmp(policy
, "deny") == 0) {
1874 acl
->defaultDeny
= 1;
1875 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1877 monitor_printf(mon
, "acl: unknown policy '%s', "
1878 "expected 'deny' or 'allow'\n", policy
);
1883 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1885 const char *aclname
= qdict_get_str(qdict
, "aclname");
1886 const char *match
= qdict_get_str(qdict
, "match");
1887 const char *policy
= qdict_get_str(qdict
, "policy");
1888 int has_index
= qdict_haskey(qdict
, "index");
1889 int index
= qdict_get_try_int(qdict
, "index", -1);
1890 qemu_acl
*acl
= find_acl(mon
, aclname
);
1894 if (strcmp(policy
, "allow") == 0) {
1896 } else if (strcmp(policy
, "deny") == 0) {
1899 monitor_printf(mon
, "acl: unknown policy '%s', "
1900 "expected 'deny' or 'allow'\n", policy
);
1904 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1906 ret
= qemu_acl_append(acl
, deny
, match
);
1908 monitor_printf(mon
, "acl: unable to add acl entry\n");
1910 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1914 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1916 const char *aclname
= qdict_get_str(qdict
, "aclname");
1917 const char *match
= qdict_get_str(qdict
, "match");
1918 qemu_acl
*acl
= find_acl(mon
, aclname
);
1922 ret
= qemu_acl_remove(acl
, match
);
1924 monitor_printf(mon
, "acl: no matching acl entry\n");
1926 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1930 #if defined(TARGET_I386)
1931 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1934 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1935 int bank
= qdict_get_int(qdict
, "bank");
1936 uint64_t status
= qdict_get_int(qdict
, "status");
1937 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1938 uint64_t addr
= qdict_get_int(qdict
, "addr");
1939 uint64_t misc
= qdict_get_int(qdict
, "misc");
1941 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1942 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1943 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1949 static void do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1951 const char *fdname
= qdict_get_str(qdict
, "fdname");
1955 fd
= qemu_chr_get_msgfd(mon
->chr
);
1957 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1961 if (qemu_isdigit(fdname
[0])) {
1962 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1968 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1973 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1974 if (strcmp(monfd
->name
, fdname
) != 0) {
1983 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1984 monfd
->name
= qemu_strdup(fdname
);
1987 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1990 static void do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1992 const char *fdname
= qdict_get_str(qdict
, "fdname");
1995 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1996 if (strcmp(monfd
->name
, fdname
) != 0) {
2000 QLIST_REMOVE(monfd
, next
);
2002 qemu_free(monfd
->name
);
2007 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
2011 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
2013 int saved_vm_running
= vm_running
;
2014 const char *name
= qdict_get_str(qdict
, "name");
2018 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
2022 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
2026 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
2029 if (strcmp(monfd
->name
, fdname
) != 0) {
2035 /* caller takes ownership of fd */
2036 QLIST_REMOVE(monfd
, next
);
2037 qemu_free(monfd
->name
);
2046 static const mon_cmd_t mon_cmds
[] = {
2047 #include "qemu-monitor.h"
2051 /* Please update qemu-monitor.hx when adding or changing commands */
2052 static const mon_cmd_t info_cmds
[] = {
2057 .help
= "show the version of QEMU",
2058 .user_print
= monitor_print_qobject
,
2059 .mhandler
.info_new
= do_info_version
,
2065 .help
= "list QMP available commands",
2066 .user_print
= monitor_user_noop
,
2067 .mhandler
.info_new
= do_info_commands
,
2073 .help
= "show the network state",
2074 .mhandler
.info
= do_info_network
,
2080 .help
= "show the character devices",
2081 .mhandler
.info
= qemu_chr_info
,
2087 .help
= "show the block devices",
2088 .mhandler
.info
= bdrv_info
,
2091 .name
= "blockstats",
2094 .help
= "show block device statistics",
2095 .mhandler
.info
= bdrv_info_stats
,
2098 .name
= "registers",
2101 .help
= "show the cpu registers",
2102 .mhandler
.info
= do_info_registers
,
2108 .help
= "show infos for each CPU",
2109 .user_print
= monitor_print_cpus
,
2110 .mhandler
.info_new
= do_info_cpus
,
2116 .help
= "show the command line history",
2117 .mhandler
.info
= do_info_history
,
2123 .help
= "show the interrupts statistics (if available)",
2124 .mhandler
.info
= irq_info
,
2130 .help
= "show i8259 (PIC) state",
2131 .mhandler
.info
= pic_info
,
2137 .help
= "show PCI info",
2138 .mhandler
.info
= pci_info
,
2140 #if defined(TARGET_I386) || defined(TARGET_SH4)
2145 .help
= "show virtual to physical memory mappings",
2146 .mhandler
.info
= tlb_info
,
2149 #if defined(TARGET_I386)
2154 .help
= "show the active virtual memory mappings",
2155 .mhandler
.info
= mem_info
,
2161 .help
= "show state of HPET",
2162 .mhandler
.info
= do_info_hpet
,
2169 .help
= "show dynamic compiler info",
2170 .mhandler
.info
= do_info_jit
,
2176 .help
= "show KVM information",
2177 .mhandler
.info
= do_info_kvm
,
2183 .help
= "show NUMA information",
2184 .mhandler
.info
= do_info_numa
,
2190 .help
= "show guest USB devices",
2191 .mhandler
.info
= usb_info
,
2197 .help
= "show host USB devices",
2198 .mhandler
.info
= usb_host_info
,
2204 .help
= "show profiling information",
2205 .mhandler
.info
= do_info_profile
,
2211 .help
= "show capture information",
2212 .mhandler
.info
= do_info_capture
,
2215 .name
= "snapshots",
2218 .help
= "show the currently saved VM snapshots",
2219 .mhandler
.info
= do_info_snapshots
,
2225 .help
= "show the current VM status (running|paused)",
2226 .mhandler
.info
= do_info_status
,
2232 .help
= "show guest PCMCIA status",
2233 .mhandler
.info
= pcmcia_info
,
2239 .help
= "show which guest mouse is receiving events",
2240 .mhandler
.info
= do_info_mice
,
2246 .help
= "show the vnc server status",
2247 .mhandler
.info
= do_info_vnc
,
2253 .help
= "show the current VM name",
2254 .mhandler
.info
= do_info_name
,
2260 .help
= "show the current VM UUID",
2261 .mhandler
.info
= do_info_uuid
,
2263 #if defined(TARGET_PPC)
2268 .help
= "show CPU statistics",
2269 .mhandler
.info
= do_info_cpu_stats
,
2272 #if defined(CONFIG_SLIRP)
2277 .help
= "show user network stack connection states",
2278 .mhandler
.info
= do_info_usernet
,
2285 .help
= "show migration status",
2286 .mhandler
.info
= do_info_migrate
,
2292 .help
= "show balloon information",
2293 .user_print
= monitor_print_balloon
,
2294 .mhandler
.info_new
= do_info_balloon
,
2300 .help
= "show device tree",
2301 .mhandler
.info
= do_info_qtree
,
2307 .help
= "show qdev device model list",
2308 .mhandler
.info
= do_info_qdm
,
2314 .help
= "show roms",
2315 .mhandler
.info
= do_info_roms
,
2322 /*******************************************************************/
2324 static const char *pch
;
2325 static jmp_buf expr_env
;
2330 typedef struct MonitorDef
{
2333 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2337 #if defined(TARGET_I386)
2338 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2340 CPUState
*env
= mon_get_cpu();
2343 return env
->eip
+ env
->segs
[R_CS
].base
;
2347 #if defined(TARGET_PPC)
2348 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2350 CPUState
*env
= mon_get_cpu();
2358 for (i
= 0; i
< 8; i
++)
2359 u
|= env
->crf
[i
] << (32 - (4 * i
));
2364 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2366 CPUState
*env
= mon_get_cpu();
2372 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2374 CPUState
*env
= mon_get_cpu();
2380 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2382 CPUState
*env
= mon_get_cpu();
2385 return cpu_ppc_load_decr(env
);
2388 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2390 CPUState
*env
= mon_get_cpu();
2393 return cpu_ppc_load_tbu(env
);
2396 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2398 CPUState
*env
= mon_get_cpu();
2401 return cpu_ppc_load_tbl(env
);
2405 #if defined(TARGET_SPARC)
2406 #ifndef TARGET_SPARC64
2407 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2409 CPUState
*env
= mon_get_cpu();
2412 return GET_PSR(env
);
2416 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2418 CPUState
*env
= mon_get_cpu();
2421 return env
->regwptr
[val
];
2425 static const MonitorDef monitor_defs
[] = {
2428 #define SEG(name, seg) \
2429 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2430 { name ".base", offsetof(CPUState, segs[seg].base) },\
2431 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2433 { "eax", offsetof(CPUState
, regs
[0]) },
2434 { "ecx", offsetof(CPUState
, regs
[1]) },
2435 { "edx", offsetof(CPUState
, regs
[2]) },
2436 { "ebx", offsetof(CPUState
, regs
[3]) },
2437 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2438 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2439 { "esi", offsetof(CPUState
, regs
[6]) },
2440 { "edi", offsetof(CPUState
, regs
[7]) },
2441 #ifdef TARGET_X86_64
2442 { "r8", offsetof(CPUState
, regs
[8]) },
2443 { "r9", offsetof(CPUState
, regs
[9]) },
2444 { "r10", offsetof(CPUState
, regs
[10]) },
2445 { "r11", offsetof(CPUState
, regs
[11]) },
2446 { "r12", offsetof(CPUState
, regs
[12]) },
2447 { "r13", offsetof(CPUState
, regs
[13]) },
2448 { "r14", offsetof(CPUState
, regs
[14]) },
2449 { "r15", offsetof(CPUState
, regs
[15]) },
2451 { "eflags", offsetof(CPUState
, eflags
) },
2452 { "eip", offsetof(CPUState
, eip
) },
2459 { "pc", 0, monitor_get_pc
, },
2460 #elif defined(TARGET_PPC)
2461 /* General purpose registers */
2462 { "r0", offsetof(CPUState
, gpr
[0]) },
2463 { "r1", offsetof(CPUState
, gpr
[1]) },
2464 { "r2", offsetof(CPUState
, gpr
[2]) },
2465 { "r3", offsetof(CPUState
, gpr
[3]) },
2466 { "r4", offsetof(CPUState
, gpr
[4]) },
2467 { "r5", offsetof(CPUState
, gpr
[5]) },
2468 { "r6", offsetof(CPUState
, gpr
[6]) },
2469 { "r7", offsetof(CPUState
, gpr
[7]) },
2470 { "r8", offsetof(CPUState
, gpr
[8]) },
2471 { "r9", offsetof(CPUState
, gpr
[9]) },
2472 { "r10", offsetof(CPUState
, gpr
[10]) },
2473 { "r11", offsetof(CPUState
, gpr
[11]) },
2474 { "r12", offsetof(CPUState
, gpr
[12]) },
2475 { "r13", offsetof(CPUState
, gpr
[13]) },
2476 { "r14", offsetof(CPUState
, gpr
[14]) },
2477 { "r15", offsetof(CPUState
, gpr
[15]) },
2478 { "r16", offsetof(CPUState
, gpr
[16]) },
2479 { "r17", offsetof(CPUState
, gpr
[17]) },
2480 { "r18", offsetof(CPUState
, gpr
[18]) },
2481 { "r19", offsetof(CPUState
, gpr
[19]) },
2482 { "r20", offsetof(CPUState
, gpr
[20]) },
2483 { "r21", offsetof(CPUState
, gpr
[21]) },
2484 { "r22", offsetof(CPUState
, gpr
[22]) },
2485 { "r23", offsetof(CPUState
, gpr
[23]) },
2486 { "r24", offsetof(CPUState
, gpr
[24]) },
2487 { "r25", offsetof(CPUState
, gpr
[25]) },
2488 { "r26", offsetof(CPUState
, gpr
[26]) },
2489 { "r27", offsetof(CPUState
, gpr
[27]) },
2490 { "r28", offsetof(CPUState
, gpr
[28]) },
2491 { "r29", offsetof(CPUState
, gpr
[29]) },
2492 { "r30", offsetof(CPUState
, gpr
[30]) },
2493 { "r31", offsetof(CPUState
, gpr
[31]) },
2494 /* Floating point registers */
2495 { "f0", offsetof(CPUState
, fpr
[0]) },
2496 { "f1", offsetof(CPUState
, fpr
[1]) },
2497 { "f2", offsetof(CPUState
, fpr
[2]) },
2498 { "f3", offsetof(CPUState
, fpr
[3]) },
2499 { "f4", offsetof(CPUState
, fpr
[4]) },
2500 { "f5", offsetof(CPUState
, fpr
[5]) },
2501 { "f6", offsetof(CPUState
, fpr
[6]) },
2502 { "f7", offsetof(CPUState
, fpr
[7]) },
2503 { "f8", offsetof(CPUState
, fpr
[8]) },
2504 { "f9", offsetof(CPUState
, fpr
[9]) },
2505 { "f10", offsetof(CPUState
, fpr
[10]) },
2506 { "f11", offsetof(CPUState
, fpr
[11]) },
2507 { "f12", offsetof(CPUState
, fpr
[12]) },
2508 { "f13", offsetof(CPUState
, fpr
[13]) },
2509 { "f14", offsetof(CPUState
, fpr
[14]) },
2510 { "f15", offsetof(CPUState
, fpr
[15]) },
2511 { "f16", offsetof(CPUState
, fpr
[16]) },
2512 { "f17", offsetof(CPUState
, fpr
[17]) },
2513 { "f18", offsetof(CPUState
, fpr
[18]) },
2514 { "f19", offsetof(CPUState
, fpr
[19]) },
2515 { "f20", offsetof(CPUState
, fpr
[20]) },
2516 { "f21", offsetof(CPUState
, fpr
[21]) },
2517 { "f22", offsetof(CPUState
, fpr
[22]) },
2518 { "f23", offsetof(CPUState
, fpr
[23]) },
2519 { "f24", offsetof(CPUState
, fpr
[24]) },
2520 { "f25", offsetof(CPUState
, fpr
[25]) },
2521 { "f26", offsetof(CPUState
, fpr
[26]) },
2522 { "f27", offsetof(CPUState
, fpr
[27]) },
2523 { "f28", offsetof(CPUState
, fpr
[28]) },
2524 { "f29", offsetof(CPUState
, fpr
[29]) },
2525 { "f30", offsetof(CPUState
, fpr
[30]) },
2526 { "f31", offsetof(CPUState
, fpr
[31]) },
2527 { "fpscr", offsetof(CPUState
, fpscr
) },
2528 /* Next instruction pointer */
2529 { "nip|pc", offsetof(CPUState
, nip
) },
2530 { "lr", offsetof(CPUState
, lr
) },
2531 { "ctr", offsetof(CPUState
, ctr
) },
2532 { "decr", 0, &monitor_get_decr
, },
2533 { "ccr", 0, &monitor_get_ccr
, },
2534 /* Machine state register */
2535 { "msr", 0, &monitor_get_msr
, },
2536 { "xer", 0, &monitor_get_xer
, },
2537 { "tbu", 0, &monitor_get_tbu
, },
2538 { "tbl", 0, &monitor_get_tbl
, },
2539 #if defined(TARGET_PPC64)
2540 /* Address space register */
2541 { "asr", offsetof(CPUState
, asr
) },
2543 /* Segment registers */
2544 { "sdr1", offsetof(CPUState
, sdr1
) },
2545 { "sr0", offsetof(CPUState
, sr
[0]) },
2546 { "sr1", offsetof(CPUState
, sr
[1]) },
2547 { "sr2", offsetof(CPUState
, sr
[2]) },
2548 { "sr3", offsetof(CPUState
, sr
[3]) },
2549 { "sr4", offsetof(CPUState
, sr
[4]) },
2550 { "sr5", offsetof(CPUState
, sr
[5]) },
2551 { "sr6", offsetof(CPUState
, sr
[6]) },
2552 { "sr7", offsetof(CPUState
, sr
[7]) },
2553 { "sr8", offsetof(CPUState
, sr
[8]) },
2554 { "sr9", offsetof(CPUState
, sr
[9]) },
2555 { "sr10", offsetof(CPUState
, sr
[10]) },
2556 { "sr11", offsetof(CPUState
, sr
[11]) },
2557 { "sr12", offsetof(CPUState
, sr
[12]) },
2558 { "sr13", offsetof(CPUState
, sr
[13]) },
2559 { "sr14", offsetof(CPUState
, sr
[14]) },
2560 { "sr15", offsetof(CPUState
, sr
[15]) },
2561 /* Too lazy to put BATs and SPRs ... */
2562 #elif defined(TARGET_SPARC)
2563 { "g0", offsetof(CPUState
, gregs
[0]) },
2564 { "g1", offsetof(CPUState
, gregs
[1]) },
2565 { "g2", offsetof(CPUState
, gregs
[2]) },
2566 { "g3", offsetof(CPUState
, gregs
[3]) },
2567 { "g4", offsetof(CPUState
, gregs
[4]) },
2568 { "g5", offsetof(CPUState
, gregs
[5]) },
2569 { "g6", offsetof(CPUState
, gregs
[6]) },
2570 { "g7", offsetof(CPUState
, gregs
[7]) },
2571 { "o0", 0, monitor_get_reg
},
2572 { "o1", 1, monitor_get_reg
},
2573 { "o2", 2, monitor_get_reg
},
2574 { "o3", 3, monitor_get_reg
},
2575 { "o4", 4, monitor_get_reg
},
2576 { "o5", 5, monitor_get_reg
},
2577 { "o6", 6, monitor_get_reg
},
2578 { "o7", 7, monitor_get_reg
},
2579 { "l0", 8, monitor_get_reg
},
2580 { "l1", 9, monitor_get_reg
},
2581 { "l2", 10, monitor_get_reg
},
2582 { "l3", 11, monitor_get_reg
},
2583 { "l4", 12, monitor_get_reg
},
2584 { "l5", 13, monitor_get_reg
},
2585 { "l6", 14, monitor_get_reg
},
2586 { "l7", 15, monitor_get_reg
},
2587 { "i0", 16, monitor_get_reg
},
2588 { "i1", 17, monitor_get_reg
},
2589 { "i2", 18, monitor_get_reg
},
2590 { "i3", 19, monitor_get_reg
},
2591 { "i4", 20, monitor_get_reg
},
2592 { "i5", 21, monitor_get_reg
},
2593 { "i6", 22, monitor_get_reg
},
2594 { "i7", 23, monitor_get_reg
},
2595 { "pc", offsetof(CPUState
, pc
) },
2596 { "npc", offsetof(CPUState
, npc
) },
2597 { "y", offsetof(CPUState
, y
) },
2598 #ifndef TARGET_SPARC64
2599 { "psr", 0, &monitor_get_psr
, },
2600 { "wim", offsetof(CPUState
, wim
) },
2602 { "tbr", offsetof(CPUState
, tbr
) },
2603 { "fsr", offsetof(CPUState
, fsr
) },
2604 { "f0", offsetof(CPUState
, fpr
[0]) },
2605 { "f1", offsetof(CPUState
, fpr
[1]) },
2606 { "f2", offsetof(CPUState
, fpr
[2]) },
2607 { "f3", offsetof(CPUState
, fpr
[3]) },
2608 { "f4", offsetof(CPUState
, fpr
[4]) },
2609 { "f5", offsetof(CPUState
, fpr
[5]) },
2610 { "f6", offsetof(CPUState
, fpr
[6]) },
2611 { "f7", offsetof(CPUState
, fpr
[7]) },
2612 { "f8", offsetof(CPUState
, fpr
[8]) },
2613 { "f9", offsetof(CPUState
, fpr
[9]) },
2614 { "f10", offsetof(CPUState
, fpr
[10]) },
2615 { "f11", offsetof(CPUState
, fpr
[11]) },
2616 { "f12", offsetof(CPUState
, fpr
[12]) },
2617 { "f13", offsetof(CPUState
, fpr
[13]) },
2618 { "f14", offsetof(CPUState
, fpr
[14]) },
2619 { "f15", offsetof(CPUState
, fpr
[15]) },
2620 { "f16", offsetof(CPUState
, fpr
[16]) },
2621 { "f17", offsetof(CPUState
, fpr
[17]) },
2622 { "f18", offsetof(CPUState
, fpr
[18]) },
2623 { "f19", offsetof(CPUState
, fpr
[19]) },
2624 { "f20", offsetof(CPUState
, fpr
[20]) },
2625 { "f21", offsetof(CPUState
, fpr
[21]) },
2626 { "f22", offsetof(CPUState
, fpr
[22]) },
2627 { "f23", offsetof(CPUState
, fpr
[23]) },
2628 { "f24", offsetof(CPUState
, fpr
[24]) },
2629 { "f25", offsetof(CPUState
, fpr
[25]) },
2630 { "f26", offsetof(CPUState
, fpr
[26]) },
2631 { "f27", offsetof(CPUState
, fpr
[27]) },
2632 { "f28", offsetof(CPUState
, fpr
[28]) },
2633 { "f29", offsetof(CPUState
, fpr
[29]) },
2634 { "f30", offsetof(CPUState
, fpr
[30]) },
2635 { "f31", offsetof(CPUState
, fpr
[31]) },
2636 #ifdef TARGET_SPARC64
2637 { "f32", offsetof(CPUState
, fpr
[32]) },
2638 { "f34", offsetof(CPUState
, fpr
[34]) },
2639 { "f36", offsetof(CPUState
, fpr
[36]) },
2640 { "f38", offsetof(CPUState
, fpr
[38]) },
2641 { "f40", offsetof(CPUState
, fpr
[40]) },
2642 { "f42", offsetof(CPUState
, fpr
[42]) },
2643 { "f44", offsetof(CPUState
, fpr
[44]) },
2644 { "f46", offsetof(CPUState
, fpr
[46]) },
2645 { "f48", offsetof(CPUState
, fpr
[48]) },
2646 { "f50", offsetof(CPUState
, fpr
[50]) },
2647 { "f52", offsetof(CPUState
, fpr
[52]) },
2648 { "f54", offsetof(CPUState
, fpr
[54]) },
2649 { "f56", offsetof(CPUState
, fpr
[56]) },
2650 { "f58", offsetof(CPUState
, fpr
[58]) },
2651 { "f60", offsetof(CPUState
, fpr
[60]) },
2652 { "f62", offsetof(CPUState
, fpr
[62]) },
2653 { "asi", offsetof(CPUState
, asi
) },
2654 { "pstate", offsetof(CPUState
, pstate
) },
2655 { "cansave", offsetof(CPUState
, cansave
) },
2656 { "canrestore", offsetof(CPUState
, canrestore
) },
2657 { "otherwin", offsetof(CPUState
, otherwin
) },
2658 { "wstate", offsetof(CPUState
, wstate
) },
2659 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2660 { "fprs", offsetof(CPUState
, fprs
) },
2666 static void expr_error(Monitor
*mon
, const char *msg
)
2668 monitor_printf(mon
, "%s\n", msg
);
2669 longjmp(expr_env
, 1);
2672 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2673 static int get_monitor_def(target_long
*pval
, const char *name
)
2675 const MonitorDef
*md
;
2678 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2679 if (compare_cmd(name
, md
->name
)) {
2680 if (md
->get_value
) {
2681 *pval
= md
->get_value(md
, md
->offset
);
2683 CPUState
*env
= mon_get_cpu();
2686 ptr
= (uint8_t *)env
+ md
->offset
;
2689 *pval
= *(int32_t *)ptr
;
2692 *pval
= *(target_long
*)ptr
;
2705 static void next(void)
2709 while (qemu_isspace(*pch
))
2714 static int64_t expr_sum(Monitor
*mon
);
2716 static int64_t expr_unary(Monitor
*mon
)
2725 n
= expr_unary(mon
);
2729 n
= -expr_unary(mon
);
2733 n
= ~expr_unary(mon
);
2739 expr_error(mon
, "')' expected");
2746 expr_error(mon
, "character constant expected");
2750 expr_error(mon
, "missing terminating \' character");
2760 while ((*pch
>= 'a' && *pch
<= 'z') ||
2761 (*pch
>= 'A' && *pch
<= 'Z') ||
2762 (*pch
>= '0' && *pch
<= '9') ||
2763 *pch
== '_' || *pch
== '.') {
2764 if ((q
- buf
) < sizeof(buf
) - 1)
2768 while (qemu_isspace(*pch
))
2771 ret
= get_monitor_def(®
, buf
);
2773 expr_error(mon
, "unknown register");
2775 expr_error(mon
, "no cpu defined");
2780 expr_error(mon
, "unexpected end of expression");
2784 #if TARGET_PHYS_ADDR_BITS > 32
2785 n
= strtoull(pch
, &p
, 0);
2787 n
= strtoul(pch
, &p
, 0);
2790 expr_error(mon
, "invalid char in expression");
2793 while (qemu_isspace(*pch
))
2801 static int64_t expr_prod(Monitor
*mon
)
2806 val
= expr_unary(mon
);
2809 if (op
!= '*' && op
!= '/' && op
!= '%')
2812 val2
= expr_unary(mon
);
2821 expr_error(mon
, "division by zero");
2832 static int64_t expr_logic(Monitor
*mon
)
2837 val
= expr_prod(mon
);
2840 if (op
!= '&' && op
!= '|' && op
!= '^')
2843 val2
= expr_prod(mon
);
2860 static int64_t expr_sum(Monitor
*mon
)
2865 val
= expr_logic(mon
);
2868 if (op
!= '+' && op
!= '-')
2871 val2
= expr_logic(mon
);
2880 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2883 if (setjmp(expr_env
)) {
2887 while (qemu_isspace(*pch
))
2889 *pval
= expr_sum(mon
);
2894 static int get_str(char *buf
, int buf_size
, const char **pp
)
2902 while (qemu_isspace(*p
))
2912 while (*p
!= '\0' && *p
!= '\"') {
2928 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2931 if ((q
- buf
) < buf_size
- 1) {
2935 if ((q
- buf
) < buf_size
- 1) {
2942 qemu_printf("unterminated string\n");
2947 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2948 if ((q
- buf
) < buf_size
- 1) {
2960 * Store the command-name in cmdname, and return a pointer to
2961 * the remaining of the command string.
2963 static const char *get_command_name(const char *cmdline
,
2964 char *cmdname
, size_t nlen
)
2967 const char *p
, *pstart
;
2970 while (qemu_isspace(*p
))
2975 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2980 memcpy(cmdname
, pstart
, len
);
2981 cmdname
[len
] = '\0';
2986 * Read key of 'type' into 'key' and return the current
2989 static char *key_get_info(const char *type
, char **key
)
2997 p
= strchr(type
, ':');
3004 str
= qemu_malloc(len
+ 1);
3005 memcpy(str
, type
, len
);
3012 static int default_fmt_format
= 'x';
3013 static int default_fmt_size
= 4;
3017 static int is_valid_option(const char *c
, const char *typestr
)
3025 typestr
= strstr(typestr
, option
);
3026 return (typestr
!= NULL
);
3029 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
3031 const mon_cmd_t
*cmd
;
3033 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3034 if (compare_cmd(cmdname
, cmd
->name
)) {
3042 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
3043 const char *cmdline
,
3046 const char *p
, *typestr
;
3048 const mon_cmd_t
*cmd
;
3054 monitor_printf(mon
, "command='%s'\n", cmdline
);
3057 /* extract the command name */
3058 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3062 cmd
= monitor_find_command(cmdname
);
3064 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3068 /* parse the parameters */
3069 typestr
= cmd
->args_type
;
3071 typestr
= key_get_info(typestr
, &key
);
3083 while (qemu_isspace(*p
))
3085 if (*typestr
== '?') {
3088 /* no optional string: NULL argument */
3092 ret
= get_str(buf
, sizeof(buf
), &p
);
3096 monitor_printf(mon
, "%s: filename expected\n",
3100 monitor_printf(mon
, "%s: block device name expected\n",
3104 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3109 qdict_put(qdict
, key
, qstring_from_str(buf
));
3114 int count
, format
, size
;
3116 while (qemu_isspace(*p
))
3122 if (qemu_isdigit(*p
)) {
3124 while (qemu_isdigit(*p
)) {
3125 count
= count
* 10 + (*p
- '0');
3163 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3164 monitor_printf(mon
, "invalid char in format: '%c'\n",
3169 format
= default_fmt_format
;
3170 if (format
!= 'i') {
3171 /* for 'i', not specifying a size gives -1 as size */
3173 size
= default_fmt_size
;
3174 default_fmt_size
= size
;
3176 default_fmt_format
= format
;
3179 format
= default_fmt_format
;
3180 if (format
!= 'i') {
3181 size
= default_fmt_size
;
3186 qdict_put(qdict
, "count", qint_from_int(count
));
3187 qdict_put(qdict
, "format", qint_from_int(format
));
3188 qdict_put(qdict
, "size", qint_from_int(size
));
3196 while (qemu_isspace(*p
))
3198 if (*typestr
== '?' || *typestr
== '.') {
3199 if (*typestr
== '?') {
3207 while (qemu_isspace(*p
))
3216 if (get_expr(mon
, &val
, &p
))
3218 /* Check if 'i' is greater than 32-bit */
3219 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3220 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3221 monitor_printf(mon
, "integer is for 32-bit values\n");
3224 qdict_put(qdict
, key
, qint_from_int(val
));
3229 const char *tmp
= p
;
3230 int has_option
, skip_key
= 0;
3236 while (qemu_isspace(*p
))
3242 if(!is_valid_option(p
, typestr
)) {
3244 monitor_printf(mon
, "%s: unsupported option -%c\n",
3258 qdict_put(qdict
, key
, qint_from_int(has_option
));
3263 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3269 /* check that all arguments were parsed */
3270 while (qemu_isspace(*p
))
3273 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3285 static void monitor_print_error(Monitor
*mon
)
3287 qerror_print(mon
->error
);
3288 QDECREF(mon
->error
);
3292 static void monitor_call_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
3293 const QDict
*params
)
3295 QObject
*data
= NULL
;
3297 cmd
->mhandler
.cmd_new(mon
, params
, &data
);
3299 if (monitor_ctrl_mode(mon
)) {
3300 /* Monitor Protocol */
3301 monitor_protocol_emitter(mon
, data
);
3305 cmd
->user_print(mon
, data
);
3308 qobject_decref(data
);
3311 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
3314 const mon_cmd_t
*cmd
;
3316 qdict
= qdict_new();
3318 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3322 qemu_errors_to_mon(mon
);
3324 if (monitor_handler_ported(cmd
)) {
3325 monitor_call_handler(mon
, cmd
, qdict
);
3327 cmd
->mhandler
.cmd(mon
, qdict
);
3330 if (monitor_has_error(mon
))
3331 monitor_print_error(mon
);
3333 qemu_errors_to_previous();
3339 static void cmd_completion(const char *name
, const char *list
)
3341 const char *p
, *pstart
;
3350 p
= pstart
+ strlen(pstart
);
3352 if (len
> sizeof(cmd
) - 2)
3353 len
= sizeof(cmd
) - 2;
3354 memcpy(cmd
, pstart
, len
);
3356 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3357 readline_add_completion(cur_mon
->rs
, cmd
);
3365 static void file_completion(const char *input
)
3370 char file
[1024], file_prefix
[1024];
3374 p
= strrchr(input
, '/');
3377 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3378 pstrcpy(path
, sizeof(path
), ".");
3380 input_path_len
= p
- input
+ 1;
3381 memcpy(path
, input
, input_path_len
);
3382 if (input_path_len
> sizeof(path
) - 1)
3383 input_path_len
= sizeof(path
) - 1;
3384 path
[input_path_len
] = '\0';
3385 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3387 #ifdef DEBUG_COMPLETION
3388 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3389 input
, path
, file_prefix
);
3391 ffs
= opendir(path
);
3399 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3400 memcpy(file
, input
, input_path_len
);
3401 if (input_path_len
< sizeof(file
))
3402 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3404 /* stat the file to find out if it's a directory.
3405 * In that case add a slash to speed up typing long paths
3408 if(S_ISDIR(sb
.st_mode
))
3409 pstrcat(file
, sizeof(file
), "/");
3410 readline_add_completion(cur_mon
->rs
, file
);
3416 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3418 const char *name
= bdrv_get_device_name(bs
);
3419 const char *input
= opaque
;
3421 if (input
[0] == '\0' ||
3422 !strncmp(name
, (char *)input
, strlen(input
))) {
3423 readline_add_completion(cur_mon
->rs
, name
);
3427 /* NOTE: this parser is an approximate form of the real command parser */
3428 static void parse_cmdline(const char *cmdline
,
3429 int *pnb_args
, char **args
)
3438 while (qemu_isspace(*p
))
3442 if (nb_args
>= MAX_ARGS
)
3444 ret
= get_str(buf
, sizeof(buf
), &p
);
3445 args
[nb_args
] = qemu_strdup(buf
);
3450 *pnb_args
= nb_args
;
3453 static const char *next_arg_type(const char *typestr
)
3455 const char *p
= strchr(typestr
, ':');
3456 return (p
!= NULL
? ++p
: typestr
);
3459 static void monitor_find_completion(const char *cmdline
)
3461 const char *cmdname
;
3462 char *args
[MAX_ARGS
];
3463 int nb_args
, i
, len
;
3464 const char *ptype
, *str
;
3465 const mon_cmd_t
*cmd
;
3468 parse_cmdline(cmdline
, &nb_args
, args
);
3469 #ifdef DEBUG_COMPLETION
3470 for(i
= 0; i
< nb_args
; i
++) {
3471 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3475 /* if the line ends with a space, it means we want to complete the
3477 len
= strlen(cmdline
);
3478 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3479 if (nb_args
>= MAX_ARGS
)
3481 args
[nb_args
++] = qemu_strdup("");
3484 /* command completion */
3489 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3490 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3491 cmd_completion(cmdname
, cmd
->name
);
3494 /* find the command */
3495 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3496 if (compare_cmd(args
[0], cmd
->name
))
3501 ptype
= next_arg_type(cmd
->args_type
);
3502 for(i
= 0; i
< nb_args
- 2; i
++) {
3503 if (*ptype
!= '\0') {
3504 ptype
= next_arg_type(ptype
);
3505 while (*ptype
== '?')
3506 ptype
= next_arg_type(ptype
);
3509 str
= args
[nb_args
- 1];
3510 if (*ptype
== '-' && ptype
[1] != '\0') {
3515 /* file completion */
3516 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3517 file_completion(str
);
3520 /* block device name completion */
3521 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3522 bdrv_iterate(block_completion_it
, (void *)str
);
3525 /* XXX: more generic ? */
3526 if (!strcmp(cmd
->name
, "info")) {
3527 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3528 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3529 cmd_completion(str
, cmd
->name
);
3531 } else if (!strcmp(cmd
->name
, "sendkey")) {
3532 char *sep
= strrchr(str
, '-');
3535 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3536 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3537 cmd_completion(str
, key
->name
);
3539 } else if (!strcmp(cmd
->name
, "help|?")) {
3540 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3541 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3542 cmd_completion(str
, cmd
->name
);
3550 for(i
= 0; i
< nb_args
; i
++)
3554 static int monitor_can_read(void *opaque
)
3556 Monitor
*mon
= opaque
;
3558 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3562 * monitor_control_read(): Read and handle QMP input
3564 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
3566 Monitor
*old_mon
= cur_mon
;
3570 // TODO: read QMP input
3575 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3577 Monitor
*old_mon
= cur_mon
;
3583 for (i
= 0; i
< size
; i
++)
3584 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3586 if (size
== 0 || buf
[size
- 1] != 0)
3587 monitor_printf(cur_mon
, "corrupted command\n");
3589 handle_user_command(cur_mon
, (char *)buf
);
3595 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3597 monitor_suspend(mon
);
3598 handle_user_command(mon
, cmdline
);
3599 monitor_resume(mon
);
3602 int monitor_suspend(Monitor
*mon
)
3610 void monitor_resume(Monitor
*mon
)
3614 if (--mon
->suspend_cnt
== 0)
3615 readline_show_prompt(mon
->rs
);
3619 * monitor_control_event(): Print QMP gretting
3621 static void monitor_control_event(void *opaque
, int event
)
3623 if (event
== CHR_EVENT_OPENED
) {
3625 Monitor
*mon
= opaque
;
3627 data
= qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
3628 assert(data
!= NULL
);
3630 monitor_json_emitter(mon
, data
);
3631 qobject_decref(data
);
3635 static void monitor_event(void *opaque
, int event
)
3637 Monitor
*mon
= opaque
;
3640 case CHR_EVENT_MUX_IN
:
3642 if (mon
->reset_seen
) {
3643 readline_restart(mon
->rs
);
3644 monitor_resume(mon
);
3647 mon
->suspend_cnt
= 0;
3651 case CHR_EVENT_MUX_OUT
:
3652 if (mon
->reset_seen
) {
3653 if (mon
->suspend_cnt
== 0) {
3654 monitor_printf(mon
, "\n");
3657 monitor_suspend(mon
);
3664 case CHR_EVENT_OPENED
:
3665 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3666 "information\n", QEMU_VERSION
);
3667 if (!mon
->mux_out
) {
3668 readline_show_prompt(mon
->rs
);
3670 mon
->reset_seen
= 1;
3684 const char *monitor_cmdline_parse(const char *cmdline
, int *flags
)
3688 if (strstart(cmdline
, "control,", &dev
)) {
3689 if (strstart(dev
, "vc", NULL
)) {
3690 fprintf(stderr
, "qemu: control mode is for low-level interaction ");
3691 fprintf(stderr
, "cannot be used with device 'vc'\n");
3694 *flags
&= ~MONITOR_USE_READLINE
;
3695 *flags
|= MONITOR_USE_CONTROL
;
3702 void monitor_init(CharDriverState
*chr
, int flags
)
3704 static int is_first_init
= 1;
3707 if (is_first_init
) {
3708 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3712 mon
= qemu_mallocz(sizeof(*mon
));
3716 if (flags
& MONITOR_USE_READLINE
) {
3717 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3718 monitor_read_command(mon
, 0);
3721 if (monitor_ctrl_mode(mon
)) {
3722 /* Control mode requires special handlers */
3723 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
3724 monitor_control_event
, mon
);
3726 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
3727 monitor_event
, mon
);
3730 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3731 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3735 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3737 BlockDriverState
*bs
= opaque
;
3740 if (bdrv_set_key(bs
, password
) != 0) {
3741 monitor_printf(mon
, "invalid password\n");
3744 if (mon
->password_completion_cb
)
3745 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3747 monitor_read_command(mon
, 1);
3750 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3751 BlockDriverCompletionFunc
*completion_cb
,
3756 if (!bdrv_key_required(bs
)) {
3758 completion_cb(opaque
, 0);
3762 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3763 bdrv_get_encrypted_filename(bs
));
3765 mon
->password_completion_cb
= completion_cb
;
3766 mon
->password_opaque
= opaque
;
3768 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3770 if (err
&& completion_cb
)
3771 completion_cb(opaque
, err
);
3774 typedef struct QemuErrorSink QemuErrorSink
;
3775 struct QemuErrorSink
{
3784 QemuErrorSink
*previous
;
3787 static QemuErrorSink
*qemu_error_sink
;
3789 void qemu_errors_to_file(FILE *fp
)
3791 QemuErrorSink
*sink
;
3793 sink
= qemu_mallocz(sizeof(*sink
));
3794 sink
->dest
= ERR_SINK_FILE
;
3796 sink
->previous
= qemu_error_sink
;
3797 qemu_error_sink
= sink
;
3800 void qemu_errors_to_mon(Monitor
*mon
)
3802 QemuErrorSink
*sink
;
3804 sink
= qemu_mallocz(sizeof(*sink
));
3805 sink
->dest
= ERR_SINK_MONITOR
;
3807 sink
->previous
= qemu_error_sink
;
3808 qemu_error_sink
= sink
;
3811 void qemu_errors_to_previous(void)
3813 QemuErrorSink
*sink
;
3815 assert(qemu_error_sink
!= NULL
);
3816 sink
= qemu_error_sink
;
3817 qemu_error_sink
= sink
->previous
;
3821 void qemu_error(const char *fmt
, ...)
3825 assert(qemu_error_sink
!= NULL
);
3826 switch (qemu_error_sink
->dest
) {
3828 va_start(args
, fmt
);
3829 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3832 case ERR_SINK_MONITOR
:
3833 va_start(args
, fmt
);
3834 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);
3840 void qemu_error_internal(const char *file
, int linenr
, const char *func
,
3841 const char *fmt
, ...)
3846 assert(qemu_error_sink
!= NULL
);
3849 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
3852 switch (qemu_error_sink
->dest
) {
3854 qerror_print(qerror
);
3857 case ERR_SINK_MONITOR
:
3858 assert(qemu_error_sink
->mon
->error
== NULL
);
3859 qemu_error_sink
->mon
->error
= qerror
;