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 int compare_cmd(const char *name
, const char *list
)
272 const char *p
, *pstart
;
280 p
= pstart
+ strlen(pstart
);
281 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
290 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
291 const char *prefix
, const char *name
)
293 const mon_cmd_t
*cmd
;
295 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
296 if (!name
|| !strcmp(name
, cmd
->name
))
297 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
298 cmd
->params
, cmd
->help
);
302 static void help_cmd(Monitor
*mon
, const char *name
)
304 if (name
&& !strcmp(name
, "info")) {
305 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
307 help_cmd_dump(mon
, mon_cmds
, "", name
);
308 if (name
&& !strcmp(name
, "log")) {
309 const CPULogItem
*item
;
310 monitor_printf(mon
, "Log items (comma separated):\n");
311 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
312 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
313 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
319 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
321 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
324 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
328 const char *device
= qdict_get_str(qdict
, "device");
330 all_devices
= !strcmp(device
, "all");
331 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
333 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
335 bdrv_commit(dinfo
->bdrv
);
339 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
341 const mon_cmd_t
*cmd
;
342 const char *item
= qdict_get_try_str(qdict
, "item");
347 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
348 if (compare_cmd(item
, cmd
->name
))
352 if (cmd
->name
== NULL
)
355 if (monitor_handler_ported(cmd
)) {
356 cmd
->mhandler
.info_new(mon
, ret_data
);
358 cmd
->user_print(mon
, *ret_data
);
360 cmd
->mhandler
.info(mon
);
366 help_cmd(mon
, "info");
370 * do_info_version(): Show QEMU version
372 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
374 *ret_data
= QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION
));
377 static void do_info_name(Monitor
*mon
)
380 monitor_printf(mon
, "%s\n", qemu_name
);
384 * do_info_commands(): List QMP available commands
386 * Return a QList of QStrings.
388 static void do_info_commands(Monitor
*mon
, QObject
**ret_data
)
391 const mon_cmd_t
*cmd
;
393 cmd_list
= qlist_new();
395 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
396 if (monitor_handler_ported(cmd
) && !compare_cmd(cmd
->name
, "info")) {
397 qlist_append(cmd_list
, qstring_from_str(cmd
->name
));
401 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
402 if (monitor_handler_ported(cmd
)) {
404 snprintf(buf
, sizeof(buf
), "query-%s", cmd
->name
);
405 qlist_append(cmd_list
, qstring_from_str(buf
));
409 *ret_data
= QOBJECT(cmd_list
);
412 #if defined(TARGET_I386)
413 static void do_info_hpet(Monitor
*mon
)
415 monitor_printf(mon
, "HPET is %s by QEMU\n",
416 (no_hpet
) ? "disabled" : "enabled");
420 static void do_info_uuid(Monitor
*mon
)
422 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
423 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
424 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
425 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
426 qemu_uuid
[14], qemu_uuid
[15]);
429 /* get the current CPU defined by the user */
430 static int mon_set_cpu(int cpu_index
)
434 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
435 if (env
->cpu_index
== cpu_index
) {
436 cur_mon
->mon_cpu
= env
;
443 static CPUState
*mon_get_cpu(void)
445 if (!cur_mon
->mon_cpu
) {
448 cpu_synchronize_state(cur_mon
->mon_cpu
);
449 return cur_mon
->mon_cpu
;
452 static void do_info_registers(Monitor
*mon
)
459 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
462 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
467 static void print_cpu_iter(QObject
*obj
, void *opaque
)
471 Monitor
*mon
= opaque
;
473 assert(qobject_type(obj
) == QTYPE_QDICT
);
474 cpu
= qobject_to_qdict(obj
);
476 if (strcmp(qdict_get_str(cpu
, "current"), "yes") == 0)
479 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
481 #if defined(TARGET_I386)
482 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
483 (target_ulong
) qdict_get_int(cpu
, "pc"));
484 #elif defined(TARGET_PPC)
485 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
486 (target_long
) qdict_get_int(cpu
, "nip"));
487 #elif defined(TARGET_SPARC)
488 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
489 (target_long
) qdict_get_int(cpu
, "pc"));
490 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
491 (target_long
) qdict_get_int(cpu
, "npc"));
492 #elif defined(TARGET_MIPS)
493 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
494 (target_long
) qdict_get_int(cpu
, "PC"));
497 if (strcmp(qdict_get_str(cpu
, "halted"), "yes") == 0)
498 monitor_printf(mon
, " (halted)");
500 monitor_printf(mon
, "\n");
503 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
507 assert(qobject_type(data
) == QTYPE_QLIST
);
508 cpu_list
= qobject_to_qlist(data
);
509 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
513 * do_info_cpus(): Show CPU information
515 * Return a QList with a QDict for each CPU.
519 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
520 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
522 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
527 cpu_list
= qlist_new();
529 /* just to set the default cpu if not already done */
532 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
534 QDict
*cpu
= qdict_new();
536 cpu_synchronize_state(env
);
538 qdict_put(cpu
, "CPU", qint_from_int(env
->cpu_index
));
539 answer
= (env
== mon
->mon_cpu
) ? "yes" : "no";
540 qdict_put(cpu
, "current", qstring_from_str(answer
));
542 #if defined(TARGET_I386)
543 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
544 #elif defined(TARGET_PPC)
545 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
546 #elif defined(TARGET_SPARC)
547 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
548 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
549 #elif defined(TARGET_MIPS)
550 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
552 answer
= env
->halted
? "yes" : "no";
553 qdict_put(cpu
, "halted", qstring_from_str(answer
));
555 qlist_append(cpu_list
, cpu
);
558 *ret_data
= QOBJECT(cpu_list
);
561 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
563 int index
= qdict_get_int(qdict
, "index");
564 if (mon_set_cpu(index
) < 0)
565 monitor_printf(mon
, "Invalid CPU index\n");
568 static void do_info_jit(Monitor
*mon
)
570 dump_exec_info((FILE *)mon
, monitor_fprintf
);
573 static void do_info_history(Monitor
*mon
)
582 str
= readline_get_history(mon
->rs
, i
);
585 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
590 #if defined(TARGET_PPC)
591 /* XXX: not implemented in other targets */
592 static void do_info_cpu_stats(Monitor
*mon
)
597 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
602 * do_quit(): Quit QEMU execution
604 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
609 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
611 if (bdrv_is_inserted(bs
)) {
613 if (!bdrv_is_removable(bs
)) {
614 monitor_printf(mon
, "device is not removable\n");
617 if (bdrv_is_locked(bs
)) {
618 monitor_printf(mon
, "device is locked\n");
627 static void do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
629 BlockDriverState
*bs
;
630 int force
= qdict_get_int(qdict
, "force");
631 const char *filename
= qdict_get_str(qdict
, "filename");
633 bs
= bdrv_find(filename
);
635 monitor_printf(mon
, "device not found\n");
638 eject_device(mon
, bs
, force
);
641 static void do_change_block(Monitor
*mon
, const char *device
,
642 const char *filename
, const char *fmt
)
644 BlockDriverState
*bs
;
645 BlockDriver
*drv
= NULL
;
647 bs
= bdrv_find(device
);
649 monitor_printf(mon
, "device not found\n");
653 drv
= bdrv_find_whitelisted_format(fmt
);
655 monitor_printf(mon
, "invalid format %s\n", fmt
);
659 if (eject_device(mon
, bs
, 0) < 0)
661 bdrv_open2(bs
, filename
, 0, drv
);
662 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
665 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
668 if (vnc_display_password(NULL
, password
) < 0)
669 monitor_printf(mon
, "could not set VNC server password\n");
671 monitor_read_command(mon
, 1);
674 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
676 if (strcmp(target
, "passwd") == 0 ||
677 strcmp(target
, "password") == 0) {
680 strncpy(password
, arg
, sizeof(password
));
681 password
[sizeof(password
) - 1] = '\0';
682 change_vnc_password_cb(mon
, password
, NULL
);
684 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
687 if (vnc_display_open(NULL
, target
) < 0)
688 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
692 static void do_change(Monitor
*mon
, const QDict
*qdict
)
694 const char *device
= qdict_get_str(qdict
, "device");
695 const char *target
= qdict_get_str(qdict
, "target");
696 const char *arg
= qdict_get_try_str(qdict
, "arg");
697 if (strcmp(device
, "vnc") == 0) {
698 do_change_vnc(mon
, target
, arg
);
700 do_change_block(mon
, device
, target
, arg
);
704 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
706 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
709 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
711 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
714 static void do_log(Monitor
*mon
, const QDict
*qdict
)
717 const char *items
= qdict_get_str(qdict
, "items");
719 if (!strcmp(items
, "none")) {
722 mask
= cpu_str_to_log_mask(items
);
724 help_cmd(mon
, "log");
731 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
733 const char *option
= qdict_get_try_str(qdict
, "option");
734 if (!option
|| !strcmp(option
, "on")) {
736 } else if (!strcmp(option
, "off")) {
739 monitor_printf(mon
, "unexpected option %s\n", option
);
744 * do_stop(): Stop VM execution
746 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
748 vm_stop(EXCP_INTERRUPT
);
751 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
753 struct bdrv_iterate_context
{
759 * do_cont(): Resume emulation.
761 static void do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
763 struct bdrv_iterate_context context
= { mon
, 0 };
765 bdrv_iterate(encrypted_bdrv_it
, &context
);
766 /* only resume the vm if all keys are set and valid */
771 static void bdrv_key_cb(void *opaque
, int err
)
773 Monitor
*mon
= opaque
;
775 /* another key was set successfully, retry to continue */
777 do_cont(mon
, NULL
, NULL
);
780 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
782 struct bdrv_iterate_context
*context
= opaque
;
784 if (!context
->err
&& bdrv_key_required(bs
)) {
785 context
->err
= -EBUSY
;
786 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
791 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
793 const char *device
= qdict_get_try_str(qdict
, "device");
795 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
796 if (gdbserver_start(device
) < 0) {
797 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
799 } else if (strcmp(device
, "none") == 0) {
800 monitor_printf(mon
, "Disabled gdbserver\n");
802 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
807 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
809 const char *action
= qdict_get_str(qdict
, "action");
810 if (select_watchdog_action(action
) == -1) {
811 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
815 static void monitor_printc(Monitor
*mon
, int c
)
817 monitor_printf(mon
, "'");
820 monitor_printf(mon
, "\\'");
823 monitor_printf(mon
, "\\\\");
826 monitor_printf(mon
, "\\n");
829 monitor_printf(mon
, "\\r");
832 if (c
>= 32 && c
<= 126) {
833 monitor_printf(mon
, "%c", c
);
835 monitor_printf(mon
, "\\x%02x", c
);
839 monitor_printf(mon
, "'");
842 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
843 target_phys_addr_t addr
, int is_physical
)
846 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
854 if (!env
&& !is_physical
)
859 } else if (wsize
== 4) {
862 /* as default we use the current CS size */
866 if ((env
->efer
& MSR_EFER_LMA
) &&
867 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
871 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
876 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
885 nb_per_line
= line_size
/ wsize
;
890 max_digits
= (wsize
* 8 + 2) / 3;
894 max_digits
= (wsize
* 8) / 4;
898 max_digits
= (wsize
* 8 * 10 + 32) / 33;
907 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
909 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
914 cpu_physical_memory_rw(addr
, buf
, l
, 0);
919 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
920 monitor_printf(mon
, " Cannot access memory\n");
929 v
= ldub_raw(buf
+ i
);
932 v
= lduw_raw(buf
+ i
);
935 v
= (uint32_t)ldl_raw(buf
+ i
);
938 v
= ldq_raw(buf
+ i
);
941 monitor_printf(mon
, " ");
944 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
947 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
950 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
953 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
956 monitor_printc(mon
, v
);
961 monitor_printf(mon
, "\n");
967 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
969 int count
= qdict_get_int(qdict
, "count");
970 int format
= qdict_get_int(qdict
, "format");
971 int size
= qdict_get_int(qdict
, "size");
972 target_long addr
= qdict_get_int(qdict
, "addr");
974 memory_dump(mon
, count
, format
, size
, addr
, 0);
977 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
979 int count
= qdict_get_int(qdict
, "count");
980 int format
= qdict_get_int(qdict
, "format");
981 int size
= qdict_get_int(qdict
, "size");
982 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
984 memory_dump(mon
, count
, format
, size
, addr
, 1);
987 static void do_print(Monitor
*mon
, const QDict
*qdict
)
989 int format
= qdict_get_int(qdict
, "format");
990 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
992 #if TARGET_PHYS_ADDR_BITS == 32
995 monitor_printf(mon
, "%#o", val
);
998 monitor_printf(mon
, "%#x", val
);
1001 monitor_printf(mon
, "%u", val
);
1005 monitor_printf(mon
, "%d", val
);
1008 monitor_printc(mon
, val
);
1014 monitor_printf(mon
, "%#" PRIo64
, val
);
1017 monitor_printf(mon
, "%#" PRIx64
, val
);
1020 monitor_printf(mon
, "%" PRIu64
, val
);
1024 monitor_printf(mon
, "%" PRId64
, val
);
1027 monitor_printc(mon
, val
);
1031 monitor_printf(mon
, "\n");
1034 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1037 uint32_t size
= qdict_get_int(qdict
, "size");
1038 const char *filename
= qdict_get_str(qdict
, "filename");
1039 target_long addr
= qdict_get_int(qdict
, "val");
1044 env
= mon_get_cpu();
1048 f
= fopen(filename
, "wb");
1050 monitor_printf(mon
, "could not open '%s'\n", filename
);
1057 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1058 fwrite(buf
, 1, l
, f
);
1065 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1071 uint32_t size
= qdict_get_int(qdict
, "size");
1072 const char *filename
= qdict_get_str(qdict
, "filename");
1073 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1075 f
= fopen(filename
, "wb");
1077 monitor_printf(mon
, "could not open '%s'\n", filename
);
1084 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1085 fwrite(buf
, 1, l
, f
);
1093 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1098 uint32_t start
= qdict_get_int(qdict
, "start");
1099 uint32_t size
= qdict_get_int(qdict
, "size");
1102 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1103 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1104 /* BSD sum algorithm ('sum' Unix command) */
1105 sum
= (sum
>> 1) | (sum
<< 15);
1108 monitor_printf(mon
, "%05d\n", sum
);
1116 static const KeyDef key_defs
[] = {
1118 { 0x36, "shift_r" },
1123 { 0xe4, "altgr_r" },
1143 { 0x0e, "backspace" },
1180 { 0x37, "asterisk" },
1183 { 0x3a, "caps_lock" },
1194 { 0x45, "num_lock" },
1195 { 0x46, "scroll_lock" },
1197 { 0xb5, "kp_divide" },
1198 { 0x37, "kp_multiply" },
1199 { 0x4a, "kp_subtract" },
1201 { 0x9c, "kp_enter" },
1202 { 0x53, "kp_decimal" },
1235 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1250 { 0xfe, "compose" },
1255 static int get_keycode(const char *key
)
1261 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1262 if (!strcmp(key
, p
->name
))
1265 if (strstart(key
, "0x", NULL
)) {
1266 ret
= strtoul(key
, &endp
, 0);
1267 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1273 #define MAX_KEYCODES 16
1274 static uint8_t keycodes
[MAX_KEYCODES
];
1275 static int nb_pending_keycodes
;
1276 static QEMUTimer
*key_timer
;
1278 static void release_keys(void *opaque
)
1282 while (nb_pending_keycodes
> 0) {
1283 nb_pending_keycodes
--;
1284 keycode
= keycodes
[nb_pending_keycodes
];
1286 kbd_put_keycode(0xe0);
1287 kbd_put_keycode(keycode
| 0x80);
1291 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1293 char keyname_buf
[16];
1295 int keyname_len
, keycode
, i
;
1296 const char *string
= qdict_get_str(qdict
, "string");
1297 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1298 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1300 if (nb_pending_keycodes
> 0) {
1301 qemu_del_timer(key_timer
);
1308 separator
= strchr(string
, '-');
1309 keyname_len
= separator
? separator
- string
: strlen(string
);
1310 if (keyname_len
> 0) {
1311 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1312 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1313 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1316 if (i
== MAX_KEYCODES
) {
1317 monitor_printf(mon
, "too many keys\n");
1320 keyname_buf
[keyname_len
] = 0;
1321 keycode
= get_keycode(keyname_buf
);
1323 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1326 keycodes
[i
++] = keycode
;
1330 string
= separator
+ 1;
1332 nb_pending_keycodes
= i
;
1333 /* key down events */
1334 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1335 keycode
= keycodes
[i
];
1337 kbd_put_keycode(0xe0);
1338 kbd_put_keycode(keycode
& 0x7f);
1340 /* delayed key up events */
1341 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1342 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1345 static int mouse_button_state
;
1347 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1350 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1351 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1352 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1353 dx
= strtol(dx_str
, NULL
, 0);
1354 dy
= strtol(dy_str
, NULL
, 0);
1357 dz
= strtol(dz_str
, NULL
, 0);
1358 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1361 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1363 int button_state
= qdict_get_int(qdict
, "button_state");
1364 mouse_button_state
= button_state
;
1365 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1368 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1370 int size
= qdict_get_int(qdict
, "size");
1371 int addr
= qdict_get_int(qdict
, "addr");
1372 int has_index
= qdict_haskey(qdict
, "index");
1377 int index
= qdict_get_int(qdict
, "index");
1378 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1386 val
= cpu_inb(addr
);
1390 val
= cpu_inw(addr
);
1394 val
= cpu_inl(addr
);
1398 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1399 suffix
, addr
, size
* 2, val
);
1402 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1404 int size
= qdict_get_int(qdict
, "size");
1405 int addr
= qdict_get_int(qdict
, "addr");
1406 int val
= qdict_get_int(qdict
, "val");
1408 addr
&= IOPORTS_MASK
;
1413 cpu_outb(addr
, val
);
1416 cpu_outw(addr
, val
);
1419 cpu_outl(addr
, val
);
1424 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1427 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1429 res
= qemu_boot_set(bootdevice
);
1431 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1432 } else if (res
> 0) {
1433 monitor_printf(mon
, "setting boot device list failed\n");
1435 monitor_printf(mon
, "no function defined to set boot device list for "
1436 "this architecture\n");
1441 * do_system_reset(): Issue a machine reset
1443 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1446 qemu_system_reset_request();
1450 * do_system_powerdown(): Issue a machine powerdown
1452 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1455 qemu_system_powerdown_request();
1458 #if defined(TARGET_I386)
1459 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1461 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1464 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1465 pte
& PG_PSE_MASK
? 'P' : '-',
1466 pte
& PG_DIRTY_MASK
? 'D' : '-',
1467 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1468 pte
& PG_PCD_MASK
? 'C' : '-',
1469 pte
& PG_PWT_MASK
? 'T' : '-',
1470 pte
& PG_USER_MASK
? 'U' : '-',
1471 pte
& PG_RW_MASK
? 'W' : '-');
1474 static void tlb_info(Monitor
*mon
)
1478 uint32_t pgd
, pde
, pte
;
1480 env
= mon_get_cpu();
1484 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1485 monitor_printf(mon
, "PG disabled\n");
1488 pgd
= env
->cr
[3] & ~0xfff;
1489 for(l1
= 0; l1
< 1024; l1
++) {
1490 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1491 pde
= le32_to_cpu(pde
);
1492 if (pde
& PG_PRESENT_MASK
) {
1493 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1494 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1496 for(l2
= 0; l2
< 1024; l2
++) {
1497 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1498 (uint8_t *)&pte
, 4);
1499 pte
= le32_to_cpu(pte
);
1500 if (pte
& PG_PRESENT_MASK
) {
1501 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1511 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1512 uint32_t end
, int prot
)
1515 prot1
= *plast_prot
;
1516 if (prot
!= prot1
) {
1517 if (*pstart
!= -1) {
1518 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1519 *pstart
, end
, end
- *pstart
,
1520 prot1
& PG_USER_MASK
? 'u' : '-',
1522 prot1
& PG_RW_MASK
? 'w' : '-');
1532 static void mem_info(Monitor
*mon
)
1535 int l1
, l2
, prot
, last_prot
;
1536 uint32_t pgd
, pde
, pte
, start
, end
;
1538 env
= mon_get_cpu();
1542 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1543 monitor_printf(mon
, "PG disabled\n");
1546 pgd
= env
->cr
[3] & ~0xfff;
1549 for(l1
= 0; l1
< 1024; l1
++) {
1550 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1551 pde
= le32_to_cpu(pde
);
1553 if (pde
& PG_PRESENT_MASK
) {
1554 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1555 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1556 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1558 for(l2
= 0; l2
< 1024; l2
++) {
1559 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1560 (uint8_t *)&pte
, 4);
1561 pte
= le32_to_cpu(pte
);
1562 end
= (l1
<< 22) + (l2
<< 12);
1563 if (pte
& PG_PRESENT_MASK
) {
1564 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1568 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1573 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1579 #if defined(TARGET_SH4)
1581 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1583 monitor_printf(mon
, " tlb%i:\t"
1584 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1585 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1586 "dirty=%hhu writethrough=%hhu\n",
1588 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1589 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1593 static void tlb_info(Monitor
*mon
)
1595 CPUState
*env
= mon_get_cpu();
1598 monitor_printf (mon
, "ITLB:\n");
1599 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1600 print_tlb (mon
, i
, &env
->itlb
[i
]);
1601 monitor_printf (mon
, "UTLB:\n");
1602 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1603 print_tlb (mon
, i
, &env
->utlb
[i
]);
1608 static void do_info_kvm(Monitor
*mon
)
1611 monitor_printf(mon
, "kvm support: ");
1613 monitor_printf(mon
, "enabled\n");
1615 monitor_printf(mon
, "disabled\n");
1617 monitor_printf(mon
, "kvm support: not compiled\n");
1621 static void do_info_numa(Monitor
*mon
)
1626 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1627 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1628 monitor_printf(mon
, "node %d cpus:", i
);
1629 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1630 if (env
->numa_node
== i
) {
1631 monitor_printf(mon
, " %d", env
->cpu_index
);
1634 monitor_printf(mon
, "\n");
1635 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1640 #ifdef CONFIG_PROFILER
1645 static void do_info_profile(Monitor
*mon
)
1651 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1652 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1653 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1654 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1659 static void do_info_profile(Monitor
*mon
)
1661 monitor_printf(mon
, "Internal profiler not compiled\n");
1665 /* Capture support */
1666 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1668 static void do_info_capture(Monitor
*mon
)
1673 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1674 monitor_printf(mon
, "[%d]: ", i
);
1675 s
->ops
.info (s
->opaque
);
1680 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1683 int n
= qdict_get_int(qdict
, "n");
1686 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1688 s
->ops
.destroy (s
->opaque
);
1689 QLIST_REMOVE (s
, entries
);
1696 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1698 const char *path
= qdict_get_str(qdict
, "path");
1699 int has_freq
= qdict_haskey(qdict
, "freq");
1700 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1701 int has_bits
= qdict_haskey(qdict
, "bits");
1702 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1703 int has_channels
= qdict_haskey(qdict
, "nchannels");
1704 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1707 s
= qemu_mallocz (sizeof (*s
));
1709 freq
= has_freq
? freq
: 44100;
1710 bits
= has_bits
? bits
: 16;
1711 nchannels
= has_channels
? nchannels
: 2;
1713 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1714 monitor_printf(mon
, "Faied to add wave capture\n");
1717 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1721 #if defined(TARGET_I386)
1722 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1725 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1727 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1728 if (env
->cpu_index
== cpu_index
) {
1729 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1735 static void do_info_status(Monitor
*mon
)
1739 monitor_printf(mon
, "VM status: running (single step mode)\n");
1741 monitor_printf(mon
, "VM status: running\n");
1744 monitor_printf(mon
, "VM status: paused\n");
1748 * do_balloon(): Request VM to change its memory allocation
1750 static void do_balloon(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1752 int value
= qdict_get_int(qdict
, "value");
1753 ram_addr_t target
= value
;
1754 qemu_balloon(target
<< 20);
1757 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
1759 monitor_printf(mon
, "balloon: actual=%d\n",
1760 (int)qint_get_int(qobject_to_qint(data
)));
1764 * do_info_balloon(): Balloon information
1766 static void do_info_balloon(Monitor
*mon
, QObject
**ret_data
)
1770 actual
= qemu_balloon_status();
1771 if (kvm_enabled() && !kvm_has_sync_mmu())
1772 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
1773 else if (actual
== 0)
1774 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
1776 *ret_data
= QOBJECT(qint_from_int((int)(actual
>> 20)));
1779 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1781 qemu_acl
*acl
= qemu_acl_find(name
);
1784 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1789 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1791 const char *aclname
= qdict_get_str(qdict
, "aclname");
1792 qemu_acl
*acl
= find_acl(mon
, aclname
);
1793 qemu_acl_entry
*entry
;
1797 monitor_printf(mon
, "policy: %s\n",
1798 acl
->defaultDeny
? "deny" : "allow");
1799 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1801 monitor_printf(mon
, "%d: %s %s\n", i
,
1802 entry
->deny
? "deny" : "allow", entry
->match
);
1807 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1809 const char *aclname
= qdict_get_str(qdict
, "aclname");
1810 qemu_acl
*acl
= find_acl(mon
, aclname
);
1813 qemu_acl_reset(acl
);
1814 monitor_printf(mon
, "acl: removed all rules\n");
1818 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1820 const char *aclname
= qdict_get_str(qdict
, "aclname");
1821 const char *policy
= qdict_get_str(qdict
, "policy");
1822 qemu_acl
*acl
= find_acl(mon
, aclname
);
1825 if (strcmp(policy
, "allow") == 0) {
1826 acl
->defaultDeny
= 0;
1827 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1828 } else if (strcmp(policy
, "deny") == 0) {
1829 acl
->defaultDeny
= 1;
1830 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1832 monitor_printf(mon
, "acl: unknown policy '%s', "
1833 "expected 'deny' or 'allow'\n", policy
);
1838 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1840 const char *aclname
= qdict_get_str(qdict
, "aclname");
1841 const char *match
= qdict_get_str(qdict
, "match");
1842 const char *policy
= qdict_get_str(qdict
, "policy");
1843 int has_index
= qdict_haskey(qdict
, "index");
1844 int index
= qdict_get_try_int(qdict
, "index", -1);
1845 qemu_acl
*acl
= find_acl(mon
, aclname
);
1849 if (strcmp(policy
, "allow") == 0) {
1851 } else if (strcmp(policy
, "deny") == 0) {
1854 monitor_printf(mon
, "acl: unknown policy '%s', "
1855 "expected 'deny' or 'allow'\n", policy
);
1859 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1861 ret
= qemu_acl_append(acl
, deny
, match
);
1863 monitor_printf(mon
, "acl: unable to add acl entry\n");
1865 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1869 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1871 const char *aclname
= qdict_get_str(qdict
, "aclname");
1872 const char *match
= qdict_get_str(qdict
, "match");
1873 qemu_acl
*acl
= find_acl(mon
, aclname
);
1877 ret
= qemu_acl_remove(acl
, match
);
1879 monitor_printf(mon
, "acl: no matching acl entry\n");
1881 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1885 #if defined(TARGET_I386)
1886 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1889 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1890 int bank
= qdict_get_int(qdict
, "bank");
1891 uint64_t status
= qdict_get_int(qdict
, "status");
1892 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1893 uint64_t addr
= qdict_get_int(qdict
, "addr");
1894 uint64_t misc
= qdict_get_int(qdict
, "misc");
1896 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1897 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1898 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1904 static void do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1906 const char *fdname
= qdict_get_str(qdict
, "fdname");
1910 fd
= qemu_chr_get_msgfd(mon
->chr
);
1912 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1916 if (qemu_isdigit(fdname
[0])) {
1917 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1923 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1928 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1929 if (strcmp(monfd
->name
, fdname
) != 0) {
1938 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1939 monfd
->name
= qemu_strdup(fdname
);
1942 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1945 static void do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1947 const char *fdname
= qdict_get_str(qdict
, "fdname");
1950 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1951 if (strcmp(monfd
->name
, fdname
) != 0) {
1955 QLIST_REMOVE(monfd
, next
);
1957 qemu_free(monfd
->name
);
1962 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1966 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1968 int saved_vm_running
= vm_running
;
1969 const char *name
= qdict_get_str(qdict
, "name");
1973 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1977 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1981 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1984 if (strcmp(monfd
->name
, fdname
) != 0) {
1990 /* caller takes ownership of fd */
1991 QLIST_REMOVE(monfd
, next
);
1992 qemu_free(monfd
->name
);
2001 static const mon_cmd_t mon_cmds
[] = {
2002 #include "qemu-monitor.h"
2006 /* Please update qemu-monitor.hx when adding or changing commands */
2007 static const mon_cmd_t info_cmds
[] = {
2012 .help
= "show the version of QEMU",
2013 .user_print
= monitor_print_qobject
,
2014 .mhandler
.info_new
= do_info_version
,
2020 .help
= "list QMP available commands",
2021 .user_print
= monitor_user_noop
,
2022 .mhandler
.info_new
= do_info_commands
,
2028 .help
= "show the network state",
2029 .mhandler
.info
= do_info_network
,
2035 .help
= "show the character devices",
2036 .mhandler
.info
= qemu_chr_info
,
2042 .help
= "show the block devices",
2043 .mhandler
.info
= bdrv_info
,
2046 .name
= "blockstats",
2049 .help
= "show block device statistics",
2050 .mhandler
.info
= bdrv_info_stats
,
2053 .name
= "registers",
2056 .help
= "show the cpu registers",
2057 .mhandler
.info
= do_info_registers
,
2063 .help
= "show infos for each CPU",
2064 .user_print
= monitor_print_cpus
,
2065 .mhandler
.info_new
= do_info_cpus
,
2071 .help
= "show the command line history",
2072 .mhandler
.info
= do_info_history
,
2078 .help
= "show the interrupts statistics (if available)",
2079 .mhandler
.info
= irq_info
,
2085 .help
= "show i8259 (PIC) state",
2086 .mhandler
.info
= pic_info
,
2092 .help
= "show PCI info",
2093 .mhandler
.info
= pci_info
,
2095 #if defined(TARGET_I386) || defined(TARGET_SH4)
2100 .help
= "show virtual to physical memory mappings",
2101 .mhandler
.info
= tlb_info
,
2104 #if defined(TARGET_I386)
2109 .help
= "show the active virtual memory mappings",
2110 .mhandler
.info
= mem_info
,
2116 .help
= "show state of HPET",
2117 .mhandler
.info
= do_info_hpet
,
2124 .help
= "show dynamic compiler info",
2125 .mhandler
.info
= do_info_jit
,
2131 .help
= "show KVM information",
2132 .mhandler
.info
= do_info_kvm
,
2138 .help
= "show NUMA information",
2139 .mhandler
.info
= do_info_numa
,
2145 .help
= "show guest USB devices",
2146 .mhandler
.info
= usb_info
,
2152 .help
= "show host USB devices",
2153 .mhandler
.info
= usb_host_info
,
2159 .help
= "show profiling information",
2160 .mhandler
.info
= do_info_profile
,
2166 .help
= "show capture information",
2167 .mhandler
.info
= do_info_capture
,
2170 .name
= "snapshots",
2173 .help
= "show the currently saved VM snapshots",
2174 .mhandler
.info
= do_info_snapshots
,
2180 .help
= "show the current VM status (running|paused)",
2181 .mhandler
.info
= do_info_status
,
2187 .help
= "show guest PCMCIA status",
2188 .mhandler
.info
= pcmcia_info
,
2194 .help
= "show which guest mouse is receiving events",
2195 .mhandler
.info
= do_info_mice
,
2201 .help
= "show the vnc server status",
2202 .mhandler
.info
= do_info_vnc
,
2208 .help
= "show the current VM name",
2209 .mhandler
.info
= do_info_name
,
2215 .help
= "show the current VM UUID",
2216 .mhandler
.info
= do_info_uuid
,
2218 #if defined(TARGET_PPC)
2223 .help
= "show CPU statistics",
2224 .mhandler
.info
= do_info_cpu_stats
,
2227 #if defined(CONFIG_SLIRP)
2232 .help
= "show user network stack connection states",
2233 .mhandler
.info
= do_info_usernet
,
2240 .help
= "show migration status",
2241 .mhandler
.info
= do_info_migrate
,
2247 .help
= "show balloon information",
2248 .user_print
= monitor_print_balloon
,
2249 .mhandler
.info_new
= do_info_balloon
,
2255 .help
= "show device tree",
2256 .mhandler
.info
= do_info_qtree
,
2262 .help
= "show qdev device model list",
2263 .mhandler
.info
= do_info_qdm
,
2269 .help
= "show roms",
2270 .mhandler
.info
= do_info_roms
,
2277 /*******************************************************************/
2279 static const char *pch
;
2280 static jmp_buf expr_env
;
2285 typedef struct MonitorDef
{
2288 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2292 #if defined(TARGET_I386)
2293 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2295 CPUState
*env
= mon_get_cpu();
2298 return env
->eip
+ env
->segs
[R_CS
].base
;
2302 #if defined(TARGET_PPC)
2303 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2305 CPUState
*env
= mon_get_cpu();
2313 for (i
= 0; i
< 8; i
++)
2314 u
|= env
->crf
[i
] << (32 - (4 * i
));
2319 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2321 CPUState
*env
= mon_get_cpu();
2327 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2329 CPUState
*env
= mon_get_cpu();
2335 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2337 CPUState
*env
= mon_get_cpu();
2340 return cpu_ppc_load_decr(env
);
2343 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2345 CPUState
*env
= mon_get_cpu();
2348 return cpu_ppc_load_tbu(env
);
2351 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2353 CPUState
*env
= mon_get_cpu();
2356 return cpu_ppc_load_tbl(env
);
2360 #if defined(TARGET_SPARC)
2361 #ifndef TARGET_SPARC64
2362 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2364 CPUState
*env
= mon_get_cpu();
2367 return GET_PSR(env
);
2371 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2373 CPUState
*env
= mon_get_cpu();
2376 return env
->regwptr
[val
];
2380 static const MonitorDef monitor_defs
[] = {
2383 #define SEG(name, seg) \
2384 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2385 { name ".base", offsetof(CPUState, segs[seg].base) },\
2386 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2388 { "eax", offsetof(CPUState
, regs
[0]) },
2389 { "ecx", offsetof(CPUState
, regs
[1]) },
2390 { "edx", offsetof(CPUState
, regs
[2]) },
2391 { "ebx", offsetof(CPUState
, regs
[3]) },
2392 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2393 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2394 { "esi", offsetof(CPUState
, regs
[6]) },
2395 { "edi", offsetof(CPUState
, regs
[7]) },
2396 #ifdef TARGET_X86_64
2397 { "r8", offsetof(CPUState
, regs
[8]) },
2398 { "r9", offsetof(CPUState
, regs
[9]) },
2399 { "r10", offsetof(CPUState
, regs
[10]) },
2400 { "r11", offsetof(CPUState
, regs
[11]) },
2401 { "r12", offsetof(CPUState
, regs
[12]) },
2402 { "r13", offsetof(CPUState
, regs
[13]) },
2403 { "r14", offsetof(CPUState
, regs
[14]) },
2404 { "r15", offsetof(CPUState
, regs
[15]) },
2406 { "eflags", offsetof(CPUState
, eflags
) },
2407 { "eip", offsetof(CPUState
, eip
) },
2414 { "pc", 0, monitor_get_pc
, },
2415 #elif defined(TARGET_PPC)
2416 /* General purpose registers */
2417 { "r0", offsetof(CPUState
, gpr
[0]) },
2418 { "r1", offsetof(CPUState
, gpr
[1]) },
2419 { "r2", offsetof(CPUState
, gpr
[2]) },
2420 { "r3", offsetof(CPUState
, gpr
[3]) },
2421 { "r4", offsetof(CPUState
, gpr
[4]) },
2422 { "r5", offsetof(CPUState
, gpr
[5]) },
2423 { "r6", offsetof(CPUState
, gpr
[6]) },
2424 { "r7", offsetof(CPUState
, gpr
[7]) },
2425 { "r8", offsetof(CPUState
, gpr
[8]) },
2426 { "r9", offsetof(CPUState
, gpr
[9]) },
2427 { "r10", offsetof(CPUState
, gpr
[10]) },
2428 { "r11", offsetof(CPUState
, gpr
[11]) },
2429 { "r12", offsetof(CPUState
, gpr
[12]) },
2430 { "r13", offsetof(CPUState
, gpr
[13]) },
2431 { "r14", offsetof(CPUState
, gpr
[14]) },
2432 { "r15", offsetof(CPUState
, gpr
[15]) },
2433 { "r16", offsetof(CPUState
, gpr
[16]) },
2434 { "r17", offsetof(CPUState
, gpr
[17]) },
2435 { "r18", offsetof(CPUState
, gpr
[18]) },
2436 { "r19", offsetof(CPUState
, gpr
[19]) },
2437 { "r20", offsetof(CPUState
, gpr
[20]) },
2438 { "r21", offsetof(CPUState
, gpr
[21]) },
2439 { "r22", offsetof(CPUState
, gpr
[22]) },
2440 { "r23", offsetof(CPUState
, gpr
[23]) },
2441 { "r24", offsetof(CPUState
, gpr
[24]) },
2442 { "r25", offsetof(CPUState
, gpr
[25]) },
2443 { "r26", offsetof(CPUState
, gpr
[26]) },
2444 { "r27", offsetof(CPUState
, gpr
[27]) },
2445 { "r28", offsetof(CPUState
, gpr
[28]) },
2446 { "r29", offsetof(CPUState
, gpr
[29]) },
2447 { "r30", offsetof(CPUState
, gpr
[30]) },
2448 { "r31", offsetof(CPUState
, gpr
[31]) },
2449 /* Floating point registers */
2450 { "f0", offsetof(CPUState
, fpr
[0]) },
2451 { "f1", offsetof(CPUState
, fpr
[1]) },
2452 { "f2", offsetof(CPUState
, fpr
[2]) },
2453 { "f3", offsetof(CPUState
, fpr
[3]) },
2454 { "f4", offsetof(CPUState
, fpr
[4]) },
2455 { "f5", offsetof(CPUState
, fpr
[5]) },
2456 { "f6", offsetof(CPUState
, fpr
[6]) },
2457 { "f7", offsetof(CPUState
, fpr
[7]) },
2458 { "f8", offsetof(CPUState
, fpr
[8]) },
2459 { "f9", offsetof(CPUState
, fpr
[9]) },
2460 { "f10", offsetof(CPUState
, fpr
[10]) },
2461 { "f11", offsetof(CPUState
, fpr
[11]) },
2462 { "f12", offsetof(CPUState
, fpr
[12]) },
2463 { "f13", offsetof(CPUState
, fpr
[13]) },
2464 { "f14", offsetof(CPUState
, fpr
[14]) },
2465 { "f15", offsetof(CPUState
, fpr
[15]) },
2466 { "f16", offsetof(CPUState
, fpr
[16]) },
2467 { "f17", offsetof(CPUState
, fpr
[17]) },
2468 { "f18", offsetof(CPUState
, fpr
[18]) },
2469 { "f19", offsetof(CPUState
, fpr
[19]) },
2470 { "f20", offsetof(CPUState
, fpr
[20]) },
2471 { "f21", offsetof(CPUState
, fpr
[21]) },
2472 { "f22", offsetof(CPUState
, fpr
[22]) },
2473 { "f23", offsetof(CPUState
, fpr
[23]) },
2474 { "f24", offsetof(CPUState
, fpr
[24]) },
2475 { "f25", offsetof(CPUState
, fpr
[25]) },
2476 { "f26", offsetof(CPUState
, fpr
[26]) },
2477 { "f27", offsetof(CPUState
, fpr
[27]) },
2478 { "f28", offsetof(CPUState
, fpr
[28]) },
2479 { "f29", offsetof(CPUState
, fpr
[29]) },
2480 { "f30", offsetof(CPUState
, fpr
[30]) },
2481 { "f31", offsetof(CPUState
, fpr
[31]) },
2482 { "fpscr", offsetof(CPUState
, fpscr
) },
2483 /* Next instruction pointer */
2484 { "nip|pc", offsetof(CPUState
, nip
) },
2485 { "lr", offsetof(CPUState
, lr
) },
2486 { "ctr", offsetof(CPUState
, ctr
) },
2487 { "decr", 0, &monitor_get_decr
, },
2488 { "ccr", 0, &monitor_get_ccr
, },
2489 /* Machine state register */
2490 { "msr", 0, &monitor_get_msr
, },
2491 { "xer", 0, &monitor_get_xer
, },
2492 { "tbu", 0, &monitor_get_tbu
, },
2493 { "tbl", 0, &monitor_get_tbl
, },
2494 #if defined(TARGET_PPC64)
2495 /* Address space register */
2496 { "asr", offsetof(CPUState
, asr
) },
2498 /* Segment registers */
2499 { "sdr1", offsetof(CPUState
, sdr1
) },
2500 { "sr0", offsetof(CPUState
, sr
[0]) },
2501 { "sr1", offsetof(CPUState
, sr
[1]) },
2502 { "sr2", offsetof(CPUState
, sr
[2]) },
2503 { "sr3", offsetof(CPUState
, sr
[3]) },
2504 { "sr4", offsetof(CPUState
, sr
[4]) },
2505 { "sr5", offsetof(CPUState
, sr
[5]) },
2506 { "sr6", offsetof(CPUState
, sr
[6]) },
2507 { "sr7", offsetof(CPUState
, sr
[7]) },
2508 { "sr8", offsetof(CPUState
, sr
[8]) },
2509 { "sr9", offsetof(CPUState
, sr
[9]) },
2510 { "sr10", offsetof(CPUState
, sr
[10]) },
2511 { "sr11", offsetof(CPUState
, sr
[11]) },
2512 { "sr12", offsetof(CPUState
, sr
[12]) },
2513 { "sr13", offsetof(CPUState
, sr
[13]) },
2514 { "sr14", offsetof(CPUState
, sr
[14]) },
2515 { "sr15", offsetof(CPUState
, sr
[15]) },
2516 /* Too lazy to put BATs and SPRs ... */
2517 #elif defined(TARGET_SPARC)
2518 { "g0", offsetof(CPUState
, gregs
[0]) },
2519 { "g1", offsetof(CPUState
, gregs
[1]) },
2520 { "g2", offsetof(CPUState
, gregs
[2]) },
2521 { "g3", offsetof(CPUState
, gregs
[3]) },
2522 { "g4", offsetof(CPUState
, gregs
[4]) },
2523 { "g5", offsetof(CPUState
, gregs
[5]) },
2524 { "g6", offsetof(CPUState
, gregs
[6]) },
2525 { "g7", offsetof(CPUState
, gregs
[7]) },
2526 { "o0", 0, monitor_get_reg
},
2527 { "o1", 1, monitor_get_reg
},
2528 { "o2", 2, monitor_get_reg
},
2529 { "o3", 3, monitor_get_reg
},
2530 { "o4", 4, monitor_get_reg
},
2531 { "o5", 5, monitor_get_reg
},
2532 { "o6", 6, monitor_get_reg
},
2533 { "o7", 7, monitor_get_reg
},
2534 { "l0", 8, monitor_get_reg
},
2535 { "l1", 9, monitor_get_reg
},
2536 { "l2", 10, monitor_get_reg
},
2537 { "l3", 11, monitor_get_reg
},
2538 { "l4", 12, monitor_get_reg
},
2539 { "l5", 13, monitor_get_reg
},
2540 { "l6", 14, monitor_get_reg
},
2541 { "l7", 15, monitor_get_reg
},
2542 { "i0", 16, monitor_get_reg
},
2543 { "i1", 17, monitor_get_reg
},
2544 { "i2", 18, monitor_get_reg
},
2545 { "i3", 19, monitor_get_reg
},
2546 { "i4", 20, monitor_get_reg
},
2547 { "i5", 21, monitor_get_reg
},
2548 { "i6", 22, monitor_get_reg
},
2549 { "i7", 23, monitor_get_reg
},
2550 { "pc", offsetof(CPUState
, pc
) },
2551 { "npc", offsetof(CPUState
, npc
) },
2552 { "y", offsetof(CPUState
, y
) },
2553 #ifndef TARGET_SPARC64
2554 { "psr", 0, &monitor_get_psr
, },
2555 { "wim", offsetof(CPUState
, wim
) },
2557 { "tbr", offsetof(CPUState
, tbr
) },
2558 { "fsr", offsetof(CPUState
, fsr
) },
2559 { "f0", offsetof(CPUState
, fpr
[0]) },
2560 { "f1", offsetof(CPUState
, fpr
[1]) },
2561 { "f2", offsetof(CPUState
, fpr
[2]) },
2562 { "f3", offsetof(CPUState
, fpr
[3]) },
2563 { "f4", offsetof(CPUState
, fpr
[4]) },
2564 { "f5", offsetof(CPUState
, fpr
[5]) },
2565 { "f6", offsetof(CPUState
, fpr
[6]) },
2566 { "f7", offsetof(CPUState
, fpr
[7]) },
2567 { "f8", offsetof(CPUState
, fpr
[8]) },
2568 { "f9", offsetof(CPUState
, fpr
[9]) },
2569 { "f10", offsetof(CPUState
, fpr
[10]) },
2570 { "f11", offsetof(CPUState
, fpr
[11]) },
2571 { "f12", offsetof(CPUState
, fpr
[12]) },
2572 { "f13", offsetof(CPUState
, fpr
[13]) },
2573 { "f14", offsetof(CPUState
, fpr
[14]) },
2574 { "f15", offsetof(CPUState
, fpr
[15]) },
2575 { "f16", offsetof(CPUState
, fpr
[16]) },
2576 { "f17", offsetof(CPUState
, fpr
[17]) },
2577 { "f18", offsetof(CPUState
, fpr
[18]) },
2578 { "f19", offsetof(CPUState
, fpr
[19]) },
2579 { "f20", offsetof(CPUState
, fpr
[20]) },
2580 { "f21", offsetof(CPUState
, fpr
[21]) },
2581 { "f22", offsetof(CPUState
, fpr
[22]) },
2582 { "f23", offsetof(CPUState
, fpr
[23]) },
2583 { "f24", offsetof(CPUState
, fpr
[24]) },
2584 { "f25", offsetof(CPUState
, fpr
[25]) },
2585 { "f26", offsetof(CPUState
, fpr
[26]) },
2586 { "f27", offsetof(CPUState
, fpr
[27]) },
2587 { "f28", offsetof(CPUState
, fpr
[28]) },
2588 { "f29", offsetof(CPUState
, fpr
[29]) },
2589 { "f30", offsetof(CPUState
, fpr
[30]) },
2590 { "f31", offsetof(CPUState
, fpr
[31]) },
2591 #ifdef TARGET_SPARC64
2592 { "f32", offsetof(CPUState
, fpr
[32]) },
2593 { "f34", offsetof(CPUState
, fpr
[34]) },
2594 { "f36", offsetof(CPUState
, fpr
[36]) },
2595 { "f38", offsetof(CPUState
, fpr
[38]) },
2596 { "f40", offsetof(CPUState
, fpr
[40]) },
2597 { "f42", offsetof(CPUState
, fpr
[42]) },
2598 { "f44", offsetof(CPUState
, fpr
[44]) },
2599 { "f46", offsetof(CPUState
, fpr
[46]) },
2600 { "f48", offsetof(CPUState
, fpr
[48]) },
2601 { "f50", offsetof(CPUState
, fpr
[50]) },
2602 { "f52", offsetof(CPUState
, fpr
[52]) },
2603 { "f54", offsetof(CPUState
, fpr
[54]) },
2604 { "f56", offsetof(CPUState
, fpr
[56]) },
2605 { "f58", offsetof(CPUState
, fpr
[58]) },
2606 { "f60", offsetof(CPUState
, fpr
[60]) },
2607 { "f62", offsetof(CPUState
, fpr
[62]) },
2608 { "asi", offsetof(CPUState
, asi
) },
2609 { "pstate", offsetof(CPUState
, pstate
) },
2610 { "cansave", offsetof(CPUState
, cansave
) },
2611 { "canrestore", offsetof(CPUState
, canrestore
) },
2612 { "otherwin", offsetof(CPUState
, otherwin
) },
2613 { "wstate", offsetof(CPUState
, wstate
) },
2614 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2615 { "fprs", offsetof(CPUState
, fprs
) },
2621 static void expr_error(Monitor
*mon
, const char *msg
)
2623 monitor_printf(mon
, "%s\n", msg
);
2624 longjmp(expr_env
, 1);
2627 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2628 static int get_monitor_def(target_long
*pval
, const char *name
)
2630 const MonitorDef
*md
;
2633 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2634 if (compare_cmd(name
, md
->name
)) {
2635 if (md
->get_value
) {
2636 *pval
= md
->get_value(md
, md
->offset
);
2638 CPUState
*env
= mon_get_cpu();
2641 ptr
= (uint8_t *)env
+ md
->offset
;
2644 *pval
= *(int32_t *)ptr
;
2647 *pval
= *(target_long
*)ptr
;
2660 static void next(void)
2664 while (qemu_isspace(*pch
))
2669 static int64_t expr_sum(Monitor
*mon
);
2671 static int64_t expr_unary(Monitor
*mon
)
2680 n
= expr_unary(mon
);
2684 n
= -expr_unary(mon
);
2688 n
= ~expr_unary(mon
);
2694 expr_error(mon
, "')' expected");
2701 expr_error(mon
, "character constant expected");
2705 expr_error(mon
, "missing terminating \' character");
2715 while ((*pch
>= 'a' && *pch
<= 'z') ||
2716 (*pch
>= 'A' && *pch
<= 'Z') ||
2717 (*pch
>= '0' && *pch
<= '9') ||
2718 *pch
== '_' || *pch
== '.') {
2719 if ((q
- buf
) < sizeof(buf
) - 1)
2723 while (qemu_isspace(*pch
))
2726 ret
= get_monitor_def(®
, buf
);
2728 expr_error(mon
, "unknown register");
2730 expr_error(mon
, "no cpu defined");
2735 expr_error(mon
, "unexpected end of expression");
2739 #if TARGET_PHYS_ADDR_BITS > 32
2740 n
= strtoull(pch
, &p
, 0);
2742 n
= strtoul(pch
, &p
, 0);
2745 expr_error(mon
, "invalid char in expression");
2748 while (qemu_isspace(*pch
))
2756 static int64_t expr_prod(Monitor
*mon
)
2761 val
= expr_unary(mon
);
2764 if (op
!= '*' && op
!= '/' && op
!= '%')
2767 val2
= expr_unary(mon
);
2776 expr_error(mon
, "division by zero");
2787 static int64_t expr_logic(Monitor
*mon
)
2792 val
= expr_prod(mon
);
2795 if (op
!= '&' && op
!= '|' && op
!= '^')
2798 val2
= expr_prod(mon
);
2815 static int64_t expr_sum(Monitor
*mon
)
2820 val
= expr_logic(mon
);
2823 if (op
!= '+' && op
!= '-')
2826 val2
= expr_logic(mon
);
2835 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2838 if (setjmp(expr_env
)) {
2842 while (qemu_isspace(*pch
))
2844 *pval
= expr_sum(mon
);
2849 static int get_str(char *buf
, int buf_size
, const char **pp
)
2857 while (qemu_isspace(*p
))
2867 while (*p
!= '\0' && *p
!= '\"') {
2883 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2886 if ((q
- buf
) < buf_size
- 1) {
2890 if ((q
- buf
) < buf_size
- 1) {
2897 qemu_printf("unterminated string\n");
2902 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2903 if ((q
- buf
) < buf_size
- 1) {
2915 * Store the command-name in cmdname, and return a pointer to
2916 * the remaining of the command string.
2918 static const char *get_command_name(const char *cmdline
,
2919 char *cmdname
, size_t nlen
)
2922 const char *p
, *pstart
;
2925 while (qemu_isspace(*p
))
2930 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2935 memcpy(cmdname
, pstart
, len
);
2936 cmdname
[len
] = '\0';
2941 * Read key of 'type' into 'key' and return the current
2944 static char *key_get_info(const char *type
, char **key
)
2952 p
= strchr(type
, ':');
2959 str
= qemu_malloc(len
+ 1);
2960 memcpy(str
, type
, len
);
2967 static int default_fmt_format
= 'x';
2968 static int default_fmt_size
= 4;
2972 static int is_valid_option(const char *c
, const char *typestr
)
2980 typestr
= strstr(typestr
, option
);
2981 return (typestr
!= NULL
);
2984 static const mon_cmd_t
*monitor_find_command(const char *cmdname
)
2986 const mon_cmd_t
*cmd
;
2988 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2989 if (compare_cmd(cmdname
, cmd
->name
)) {
2997 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2998 const char *cmdline
,
3001 const char *p
, *typestr
;
3003 const mon_cmd_t
*cmd
;
3009 monitor_printf(mon
, "command='%s'\n", cmdline
);
3012 /* extract the command name */
3013 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
3017 cmd
= monitor_find_command(cmdname
);
3019 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
3023 /* parse the parameters */
3024 typestr
= cmd
->args_type
;
3026 typestr
= key_get_info(typestr
, &key
);
3038 while (qemu_isspace(*p
))
3040 if (*typestr
== '?') {
3043 /* no optional string: NULL argument */
3047 ret
= get_str(buf
, sizeof(buf
), &p
);
3051 monitor_printf(mon
, "%s: filename expected\n",
3055 monitor_printf(mon
, "%s: block device name expected\n",
3059 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3064 qdict_put(qdict
, key
, qstring_from_str(buf
));
3069 int count
, format
, size
;
3071 while (qemu_isspace(*p
))
3077 if (qemu_isdigit(*p
)) {
3079 while (qemu_isdigit(*p
)) {
3080 count
= count
* 10 + (*p
- '0');
3118 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3119 monitor_printf(mon
, "invalid char in format: '%c'\n",
3124 format
= default_fmt_format
;
3125 if (format
!= 'i') {
3126 /* for 'i', not specifying a size gives -1 as size */
3128 size
= default_fmt_size
;
3129 default_fmt_size
= size
;
3131 default_fmt_format
= format
;
3134 format
= default_fmt_format
;
3135 if (format
!= 'i') {
3136 size
= default_fmt_size
;
3141 qdict_put(qdict
, "count", qint_from_int(count
));
3142 qdict_put(qdict
, "format", qint_from_int(format
));
3143 qdict_put(qdict
, "size", qint_from_int(size
));
3151 while (qemu_isspace(*p
))
3153 if (*typestr
== '?' || *typestr
== '.') {
3154 if (*typestr
== '?') {
3162 while (qemu_isspace(*p
))
3171 if (get_expr(mon
, &val
, &p
))
3173 /* Check if 'i' is greater than 32-bit */
3174 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3175 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3176 monitor_printf(mon
, "integer is for 32-bit values\n");
3179 qdict_put(qdict
, key
, qint_from_int(val
));
3184 const char *tmp
= p
;
3185 int has_option
, skip_key
= 0;
3191 while (qemu_isspace(*p
))
3197 if(!is_valid_option(p
, typestr
)) {
3199 monitor_printf(mon
, "%s: unsupported option -%c\n",
3213 qdict_put(qdict
, key
, qint_from_int(has_option
));
3218 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3224 /* check that all arguments were parsed */
3225 while (qemu_isspace(*p
))
3228 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3240 static void monitor_print_error(Monitor
*mon
)
3242 qerror_print(mon
->error
);
3243 QDECREF(mon
->error
);
3247 static void monitor_call_handler(Monitor
*mon
, const mon_cmd_t
*cmd
,
3248 const QDict
*params
)
3250 QObject
*data
= NULL
;
3252 cmd
->mhandler
.cmd_new(mon
, params
, &data
);
3254 cmd
->user_print(mon
, data
);
3256 qobject_decref(data
);
3259 static void handle_user_command(Monitor
*mon
, const char *cmdline
)
3262 const mon_cmd_t
*cmd
;
3264 qdict
= qdict_new();
3266 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3270 qemu_errors_to_mon(mon
);
3272 if (monitor_handler_ported(cmd
)) {
3273 monitor_call_handler(mon
, cmd
, qdict
);
3275 cmd
->mhandler
.cmd(mon
, qdict
);
3278 if (monitor_has_error(mon
))
3279 monitor_print_error(mon
);
3281 qemu_errors_to_previous();
3287 static void cmd_completion(const char *name
, const char *list
)
3289 const char *p
, *pstart
;
3298 p
= pstart
+ strlen(pstart
);
3300 if (len
> sizeof(cmd
) - 2)
3301 len
= sizeof(cmd
) - 2;
3302 memcpy(cmd
, pstart
, len
);
3304 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3305 readline_add_completion(cur_mon
->rs
, cmd
);
3313 static void file_completion(const char *input
)
3318 char file
[1024], file_prefix
[1024];
3322 p
= strrchr(input
, '/');
3325 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3326 pstrcpy(path
, sizeof(path
), ".");
3328 input_path_len
= p
- input
+ 1;
3329 memcpy(path
, input
, input_path_len
);
3330 if (input_path_len
> sizeof(path
) - 1)
3331 input_path_len
= sizeof(path
) - 1;
3332 path
[input_path_len
] = '\0';
3333 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3335 #ifdef DEBUG_COMPLETION
3336 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3337 input
, path
, file_prefix
);
3339 ffs
= opendir(path
);
3347 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3348 memcpy(file
, input
, input_path_len
);
3349 if (input_path_len
< sizeof(file
))
3350 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3352 /* stat the file to find out if it's a directory.
3353 * In that case add a slash to speed up typing long paths
3356 if(S_ISDIR(sb
.st_mode
))
3357 pstrcat(file
, sizeof(file
), "/");
3358 readline_add_completion(cur_mon
->rs
, file
);
3364 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3366 const char *name
= bdrv_get_device_name(bs
);
3367 const char *input
= opaque
;
3369 if (input
[0] == '\0' ||
3370 !strncmp(name
, (char *)input
, strlen(input
))) {
3371 readline_add_completion(cur_mon
->rs
, name
);
3375 /* NOTE: this parser is an approximate form of the real command parser */
3376 static void parse_cmdline(const char *cmdline
,
3377 int *pnb_args
, char **args
)
3386 while (qemu_isspace(*p
))
3390 if (nb_args
>= MAX_ARGS
)
3392 ret
= get_str(buf
, sizeof(buf
), &p
);
3393 args
[nb_args
] = qemu_strdup(buf
);
3398 *pnb_args
= nb_args
;
3401 static const char *next_arg_type(const char *typestr
)
3403 const char *p
= strchr(typestr
, ':');
3404 return (p
!= NULL
? ++p
: typestr
);
3407 static void monitor_find_completion(const char *cmdline
)
3409 const char *cmdname
;
3410 char *args
[MAX_ARGS
];
3411 int nb_args
, i
, len
;
3412 const char *ptype
, *str
;
3413 const mon_cmd_t
*cmd
;
3416 parse_cmdline(cmdline
, &nb_args
, args
);
3417 #ifdef DEBUG_COMPLETION
3418 for(i
= 0; i
< nb_args
; i
++) {
3419 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3423 /* if the line ends with a space, it means we want to complete the
3425 len
= strlen(cmdline
);
3426 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3427 if (nb_args
>= MAX_ARGS
)
3429 args
[nb_args
++] = qemu_strdup("");
3432 /* command completion */
3437 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3438 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3439 cmd_completion(cmdname
, cmd
->name
);
3442 /* find the command */
3443 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3444 if (compare_cmd(args
[0], cmd
->name
))
3449 ptype
= next_arg_type(cmd
->args_type
);
3450 for(i
= 0; i
< nb_args
- 2; i
++) {
3451 if (*ptype
!= '\0') {
3452 ptype
= next_arg_type(ptype
);
3453 while (*ptype
== '?')
3454 ptype
= next_arg_type(ptype
);
3457 str
= args
[nb_args
- 1];
3458 if (*ptype
== '-' && ptype
[1] != '\0') {
3463 /* file completion */
3464 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3465 file_completion(str
);
3468 /* block device name completion */
3469 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3470 bdrv_iterate(block_completion_it
, (void *)str
);
3473 /* XXX: more generic ? */
3474 if (!strcmp(cmd
->name
, "info")) {
3475 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3476 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3477 cmd_completion(str
, cmd
->name
);
3479 } else if (!strcmp(cmd
->name
, "sendkey")) {
3480 char *sep
= strrchr(str
, '-');
3483 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3484 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3485 cmd_completion(str
, key
->name
);
3487 } else if (!strcmp(cmd
->name
, "help|?")) {
3488 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3489 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3490 cmd_completion(str
, cmd
->name
);
3498 for(i
= 0; i
< nb_args
; i
++)
3502 static int monitor_can_read(void *opaque
)
3504 Monitor
*mon
= opaque
;
3506 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3510 * monitor_control_read(): Read and handle QMP input
3512 static void monitor_control_read(void *opaque
, const uint8_t *buf
, int size
)
3514 Monitor
*old_mon
= cur_mon
;
3518 // TODO: read QMP input
3523 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3525 Monitor
*old_mon
= cur_mon
;
3531 for (i
= 0; i
< size
; i
++)
3532 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3534 if (size
== 0 || buf
[size
- 1] != 0)
3535 monitor_printf(cur_mon
, "corrupted command\n");
3537 handle_user_command(cur_mon
, (char *)buf
);
3543 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3545 monitor_suspend(mon
);
3546 handle_user_command(mon
, cmdline
);
3547 monitor_resume(mon
);
3550 int monitor_suspend(Monitor
*mon
)
3558 void monitor_resume(Monitor
*mon
)
3562 if (--mon
->suspend_cnt
== 0)
3563 readline_show_prompt(mon
->rs
);
3567 * monitor_control_event(): Print QMP gretting
3569 static void monitor_control_event(void *opaque
, int event
)
3571 if (event
== CHR_EVENT_OPENED
) {
3573 Monitor
*mon
= opaque
;
3575 data
= qobject_from_jsonf("{ 'QMP': { 'capabilities': [] } }");
3576 assert(data
!= NULL
);
3578 monitor_json_emitter(mon
, data
);
3579 qobject_decref(data
);
3583 static void monitor_event(void *opaque
, int event
)
3585 Monitor
*mon
= opaque
;
3588 case CHR_EVENT_MUX_IN
:
3590 if (mon
->reset_seen
) {
3591 readline_restart(mon
->rs
);
3592 monitor_resume(mon
);
3595 mon
->suspend_cnt
= 0;
3599 case CHR_EVENT_MUX_OUT
:
3600 if (mon
->reset_seen
) {
3601 if (mon
->suspend_cnt
== 0) {
3602 monitor_printf(mon
, "\n");
3605 monitor_suspend(mon
);
3612 case CHR_EVENT_OPENED
:
3613 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3614 "information\n", QEMU_VERSION
);
3615 if (!mon
->mux_out
) {
3616 readline_show_prompt(mon
->rs
);
3618 mon
->reset_seen
= 1;
3632 const char *monitor_cmdline_parse(const char *cmdline
, int *flags
)
3636 if (strstart(cmdline
, "control,", &dev
)) {
3637 if (strstart(dev
, "vc", NULL
)) {
3638 fprintf(stderr
, "qemu: control mode is for low-level interaction ");
3639 fprintf(stderr
, "cannot be used with device 'vc'\n");
3642 *flags
&= ~MONITOR_USE_READLINE
;
3643 *flags
|= MONITOR_USE_CONTROL
;
3650 void monitor_init(CharDriverState
*chr
, int flags
)
3652 static int is_first_init
= 1;
3655 if (is_first_init
) {
3656 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3660 mon
= qemu_mallocz(sizeof(*mon
));
3664 if (flags
& MONITOR_USE_READLINE
) {
3665 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3666 monitor_read_command(mon
, 0);
3669 if (monitor_ctrl_mode(mon
)) {
3670 /* Control mode requires special handlers */
3671 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_control_read
,
3672 monitor_control_event
, mon
);
3674 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
,
3675 monitor_event
, mon
);
3678 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3679 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3683 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3685 BlockDriverState
*bs
= opaque
;
3688 if (bdrv_set_key(bs
, password
) != 0) {
3689 monitor_printf(mon
, "invalid password\n");
3692 if (mon
->password_completion_cb
)
3693 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3695 monitor_read_command(mon
, 1);
3698 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3699 BlockDriverCompletionFunc
*completion_cb
,
3704 if (!bdrv_key_required(bs
)) {
3706 completion_cb(opaque
, 0);
3710 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3711 bdrv_get_encrypted_filename(bs
));
3713 mon
->password_completion_cb
= completion_cb
;
3714 mon
->password_opaque
= opaque
;
3716 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3718 if (err
&& completion_cb
)
3719 completion_cb(opaque
, err
);
3722 typedef struct QemuErrorSink QemuErrorSink
;
3723 struct QemuErrorSink
{
3732 QemuErrorSink
*previous
;
3735 static QemuErrorSink
*qemu_error_sink
;
3737 void qemu_errors_to_file(FILE *fp
)
3739 QemuErrorSink
*sink
;
3741 sink
= qemu_mallocz(sizeof(*sink
));
3742 sink
->dest
= ERR_SINK_FILE
;
3744 sink
->previous
= qemu_error_sink
;
3745 qemu_error_sink
= sink
;
3748 void qemu_errors_to_mon(Monitor
*mon
)
3750 QemuErrorSink
*sink
;
3752 sink
= qemu_mallocz(sizeof(*sink
));
3753 sink
->dest
= ERR_SINK_MONITOR
;
3755 sink
->previous
= qemu_error_sink
;
3756 qemu_error_sink
= sink
;
3759 void qemu_errors_to_previous(void)
3761 QemuErrorSink
*sink
;
3763 assert(qemu_error_sink
!= NULL
);
3764 sink
= qemu_error_sink
;
3765 qemu_error_sink
= sink
->previous
;
3769 void qemu_error(const char *fmt
, ...)
3773 assert(qemu_error_sink
!= NULL
);
3774 switch (qemu_error_sink
->dest
) {
3776 va_start(args
, fmt
);
3777 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3780 case ERR_SINK_MONITOR
:
3781 va_start(args
, fmt
);
3782 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);
3788 void qemu_error_internal(const char *file
, int linenr
, const char *func
,
3789 const char *fmt
, ...)
3794 assert(qemu_error_sink
!= NULL
);
3797 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
3800 switch (qemu_error_sink
->dest
) {
3802 qerror_print(qerror
);
3805 case ERR_SINK_MONITOR
:
3806 assert(qemu_error_sink
->mon
->error
== NULL
);
3807 qemu_error_sink
->mon
->error
= qerror
;