4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
32 #include "hw/loader.h"
35 #include "qemu-char.h"
41 #include "audio/audio.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
55 //#define DEBUG_COMPLETION
61 * 'B' block device name
62 * 's' string (accept optional quote)
64 * 'l' target long (32 or 64 bit)
65 * '/' optional gdb-like print format (like "/10x")
67 * '?' optional type (for all types, except '/')
68 * '.' other form of optional type (for 'i' and 'l')
69 * '-' optional parameter (eg. '-f')
73 typedef struct mon_cmd_t
{
75 const char *args_type
;
78 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
80 void (*info
)(Monitor
*mon
);
81 void (*info_new
)(Monitor
*mon
, QObject
**ret_data
);
82 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
83 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
87 /* file descriptors passed via SCM_RIGHTS */
88 typedef struct mon_fd_t mon_fd_t
;
92 QLIST_ENTRY(mon_fd_t
) next
;
101 uint8_t outbuf
[1024];
105 BlockDriverCompletionFunc
*password_completion_cb
;
106 void *password_opaque
;
108 QLIST_HEAD(,mon_fd_t
) fds
;
109 QLIST_ENTRY(Monitor
) entry
;
112 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
114 static const mon_cmd_t mon_cmds
[];
115 static const mon_cmd_t info_cmds
[];
117 Monitor
*cur_mon
= NULL
;
119 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
122 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
124 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
126 readline_show_prompt(mon
->rs
);
129 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
133 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
134 /* prompt is printed on return from the command handler */
137 monitor_printf(mon
, "terminal does not support password prompting\n");
142 void monitor_flush(Monitor
*mon
)
144 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
145 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
146 mon
->outbuf_index
= 0;
150 /* flush at every end of line or if the buffer is full */
151 static void monitor_puts(Monitor
*mon
, const char *str
)
163 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
164 mon
->outbuf
[mon
->outbuf_index
++] = c
;
165 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
171 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
174 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
175 monitor_puts(mon
, buf
);
178 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
182 monitor_vprintf(mon
, fmt
, ap
);
186 void monitor_print_filename(Monitor
*mon
, const char *filename
)
190 for (i
= 0; filename
[i
]; i
++) {
191 switch (filename
[i
]) {
195 monitor_printf(mon
, "\\%c", filename
[i
]);
198 monitor_printf(mon
, "\\t");
201 monitor_printf(mon
, "\\r");
204 monitor_printf(mon
, "\\n");
207 monitor_printf(mon
, "%c", filename
[i
]);
213 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
217 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
222 static void monitor_user_noop(Monitor
*mon
, const QObject
*data
) { }
224 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
226 return cmd
->user_print
!= NULL
;
229 static inline int monitor_has_error(const Monitor
*mon
)
231 return mon
->error
!= NULL
;
234 static void monitor_print_qobject(Monitor
*mon
, const QObject
*data
)
236 switch (qobject_type(data
)) {
238 monitor_printf(mon
, "%s",qstring_get_str(qobject_to_qstring(data
)));
241 monitor_printf(mon
, "%" PRId64
,qint_get_int(qobject_to_qint(data
)));
244 monitor_printf(mon
, "ERROR: unsupported type: %d",
249 monitor_puts(mon
, "\n");
252 static int compare_cmd(const char *name
, const char *list
)
254 const char *p
, *pstart
;
262 p
= pstart
+ strlen(pstart
);
263 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
272 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
273 const char *prefix
, const char *name
)
275 const mon_cmd_t
*cmd
;
277 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
278 if (!name
|| !strcmp(name
, cmd
->name
))
279 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
280 cmd
->params
, cmd
->help
);
284 static void help_cmd(Monitor
*mon
, const char *name
)
286 if (name
&& !strcmp(name
, "info")) {
287 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
289 help_cmd_dump(mon
, mon_cmds
, "", name
);
290 if (name
&& !strcmp(name
, "log")) {
291 const CPULogItem
*item
;
292 monitor_printf(mon
, "Log items (comma separated):\n");
293 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
294 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
295 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
301 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
303 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
306 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
310 const char *device
= qdict_get_str(qdict
, "device");
312 all_devices
= !strcmp(device
, "all");
313 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
315 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
317 bdrv_commit(dinfo
->bdrv
);
321 static void do_info(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
323 const mon_cmd_t
*cmd
;
324 const char *item
= qdict_get_try_str(qdict
, "item");
329 for (cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
330 if (compare_cmd(item
, cmd
->name
))
334 if (cmd
->name
== NULL
)
337 if (monitor_handler_ported(cmd
)) {
338 cmd
->mhandler
.info_new(mon
, ret_data
);
340 cmd
->user_print(mon
, *ret_data
);
342 cmd
->mhandler
.info(mon
);
348 help_cmd(mon
, "info");
352 * do_info_version(): Show QEMU version
354 static void do_info_version(Monitor
*mon
, QObject
**ret_data
)
356 *ret_data
= QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION
));
359 static void do_info_name(Monitor
*mon
)
362 monitor_printf(mon
, "%s\n", qemu_name
);
365 #if defined(TARGET_I386)
366 static void do_info_hpet(Monitor
*mon
)
368 monitor_printf(mon
, "HPET is %s by QEMU\n",
369 (no_hpet
) ? "disabled" : "enabled");
373 static void do_info_uuid(Monitor
*mon
)
375 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
376 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
377 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
378 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
379 qemu_uuid
[14], qemu_uuid
[15]);
382 /* get the current CPU defined by the user */
383 static int mon_set_cpu(int cpu_index
)
387 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
388 if (env
->cpu_index
== cpu_index
) {
389 cur_mon
->mon_cpu
= env
;
396 static CPUState
*mon_get_cpu(void)
398 if (!cur_mon
->mon_cpu
) {
401 cpu_synchronize_state(cur_mon
->mon_cpu
);
402 return cur_mon
->mon_cpu
;
405 static void do_info_registers(Monitor
*mon
)
412 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
415 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
420 static void print_cpu_iter(QObject
*obj
, void *opaque
)
424 Monitor
*mon
= opaque
;
426 assert(qobject_type(obj
) == QTYPE_QDICT
);
427 cpu
= qobject_to_qdict(obj
);
429 if (strcmp(qdict_get_str(cpu
, "current"), "yes") == 0)
432 monitor_printf(mon
, "%c CPU #%d: ", active
, (int)qdict_get_int(cpu
, "CPU"));
434 #if defined(TARGET_I386)
435 monitor_printf(mon
, "pc=0x" TARGET_FMT_lx
,
436 (target_ulong
) qdict_get_int(cpu
, "pc"));
437 #elif defined(TARGET_PPC)
438 monitor_printf(mon
, "nip=0x" TARGET_FMT_lx
,
439 (target_long
) qdict_get_int(cpu
, "nip"));
440 #elif defined(TARGET_SPARC)
441 monitor_printf(mon
, "pc=0x " TARGET_FMT_lx
,
442 (target_long
) qdict_get_int(cpu
, "pc"));
443 monitor_printf(mon
, "npc=0x" TARGET_FMT_lx
,
444 (target_long
) qdict_get_int(cpu
, "npc"));
445 #elif defined(TARGET_MIPS)
446 monitor_printf(mon
, "PC=0x" TARGET_FMT_lx
,
447 (target_long
) qdict_get_int(cpu
, "PC"));
450 if (strcmp(qdict_get_str(cpu
, "halted"), "yes") == 0)
451 monitor_printf(mon
, " (halted)");
453 monitor_printf(mon
, "\n");
456 static void monitor_print_cpus(Monitor
*mon
, const QObject
*data
)
460 assert(qobject_type(data
) == QTYPE_QLIST
);
461 cpu_list
= qobject_to_qlist(data
);
462 qlist_iter(cpu_list
, print_cpu_iter
, mon
);
466 * do_info_cpus(): Show CPU information
468 * Return a QList with a QDict for each CPU.
472 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
473 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
475 static void do_info_cpus(Monitor
*mon
, QObject
**ret_data
)
480 cpu_list
= qlist_new();
482 /* just to set the default cpu if not already done */
485 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
487 QDict
*cpu
= qdict_new();
489 cpu_synchronize_state(env
);
491 qdict_put(cpu
, "CPU", qint_from_int(env
->cpu_index
));
492 answer
= (env
== mon
->mon_cpu
) ? "yes" : "no";
493 qdict_put(cpu
, "current", qstring_from_str(answer
));
495 #if defined(TARGET_I386)
496 qdict_put(cpu
, "pc", qint_from_int(env
->eip
+ env
->segs
[R_CS
].base
));
497 #elif defined(TARGET_PPC)
498 qdict_put(cpu
, "nip", qint_from_int(env
->nip
));
499 #elif defined(TARGET_SPARC)
500 qdict_put(cpu
, "pc", qint_from_int(env
->pc
));
501 qdict_put(cpu
, "npc", qint_from_int(env
->npc
));
502 #elif defined(TARGET_MIPS)
503 qdict_put(cpu
, "PC", qint_from_int(env
->active_tc
.PC
));
505 answer
= env
->halted
? "yes" : "no";
506 qdict_put(cpu
, "halted", qstring_from_str(answer
));
508 qlist_append(cpu_list
, cpu
);
511 *ret_data
= QOBJECT(cpu_list
);
514 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
516 int index
= qdict_get_int(qdict
, "index");
517 if (mon_set_cpu(index
) < 0)
518 monitor_printf(mon
, "Invalid CPU index\n");
521 static void do_info_jit(Monitor
*mon
)
523 dump_exec_info((FILE *)mon
, monitor_fprintf
);
526 static void do_info_history(Monitor
*mon
)
535 str
= readline_get_history(mon
->rs
, i
);
538 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
543 #if defined(TARGET_PPC)
544 /* XXX: not implemented in other targets */
545 static void do_info_cpu_stats(Monitor
*mon
)
550 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
555 * do_quit(): Quit QEMU execution
557 static void do_quit(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
562 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
564 if (bdrv_is_inserted(bs
)) {
566 if (!bdrv_is_removable(bs
)) {
567 monitor_printf(mon
, "device is not removable\n");
570 if (bdrv_is_locked(bs
)) {
571 monitor_printf(mon
, "device is locked\n");
580 static void do_eject(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
582 BlockDriverState
*bs
;
583 int force
= qdict_get_int(qdict
, "force");
584 const char *filename
= qdict_get_str(qdict
, "filename");
586 bs
= bdrv_find(filename
);
588 monitor_printf(mon
, "device not found\n");
591 eject_device(mon
, bs
, force
);
594 static void do_change_block(Monitor
*mon
, const char *device
,
595 const char *filename
, const char *fmt
)
597 BlockDriverState
*bs
;
598 BlockDriver
*drv
= NULL
;
600 bs
= bdrv_find(device
);
602 monitor_printf(mon
, "device not found\n");
606 drv
= bdrv_find_whitelisted_format(fmt
);
608 monitor_printf(mon
, "invalid format %s\n", fmt
);
612 if (eject_device(mon
, bs
, 0) < 0)
614 bdrv_open2(bs
, filename
, 0, drv
);
615 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
618 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
621 if (vnc_display_password(NULL
, password
) < 0)
622 monitor_printf(mon
, "could not set VNC server password\n");
624 monitor_read_command(mon
, 1);
627 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
629 if (strcmp(target
, "passwd") == 0 ||
630 strcmp(target
, "password") == 0) {
633 strncpy(password
, arg
, sizeof(password
));
634 password
[sizeof(password
) - 1] = '\0';
635 change_vnc_password_cb(mon
, password
, NULL
);
637 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
640 if (vnc_display_open(NULL
, target
) < 0)
641 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
645 static void do_change(Monitor
*mon
, const QDict
*qdict
)
647 const char *device
= qdict_get_str(qdict
, "device");
648 const char *target
= qdict_get_str(qdict
, "target");
649 const char *arg
= qdict_get_try_str(qdict
, "arg");
650 if (strcmp(device
, "vnc") == 0) {
651 do_change_vnc(mon
, target
, arg
);
653 do_change_block(mon
, device
, target
, arg
);
657 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
659 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
662 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
664 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
667 static void do_log(Monitor
*mon
, const QDict
*qdict
)
670 const char *items
= qdict_get_str(qdict
, "items");
672 if (!strcmp(items
, "none")) {
675 mask
= cpu_str_to_log_mask(items
);
677 help_cmd(mon
, "log");
684 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
686 const char *option
= qdict_get_try_str(qdict
, "option");
687 if (!option
|| !strcmp(option
, "on")) {
689 } else if (!strcmp(option
, "off")) {
692 monitor_printf(mon
, "unexpected option %s\n", option
);
697 * do_stop(): Stop VM execution
699 static void do_stop(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
701 vm_stop(EXCP_INTERRUPT
);
704 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
706 struct bdrv_iterate_context
{
712 * do_cont(): Resume emulation.
714 static void do_cont(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
716 struct bdrv_iterate_context context
= { mon
, 0 };
718 bdrv_iterate(encrypted_bdrv_it
, &context
);
719 /* only resume the vm if all keys are set and valid */
724 static void bdrv_key_cb(void *opaque
, int err
)
726 Monitor
*mon
= opaque
;
728 /* another key was set successfully, retry to continue */
730 do_cont(mon
, NULL
, NULL
);
733 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
735 struct bdrv_iterate_context
*context
= opaque
;
737 if (!context
->err
&& bdrv_key_required(bs
)) {
738 context
->err
= -EBUSY
;
739 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
744 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
746 const char *device
= qdict_get_try_str(qdict
, "device");
748 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
749 if (gdbserver_start(device
) < 0) {
750 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
752 } else if (strcmp(device
, "none") == 0) {
753 monitor_printf(mon
, "Disabled gdbserver\n");
755 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
760 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
762 const char *action
= qdict_get_str(qdict
, "action");
763 if (select_watchdog_action(action
) == -1) {
764 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
768 static void monitor_printc(Monitor
*mon
, int c
)
770 monitor_printf(mon
, "'");
773 monitor_printf(mon
, "\\'");
776 monitor_printf(mon
, "\\\\");
779 monitor_printf(mon
, "\\n");
782 monitor_printf(mon
, "\\r");
785 if (c
>= 32 && c
<= 126) {
786 monitor_printf(mon
, "%c", c
);
788 monitor_printf(mon
, "\\x%02x", c
);
792 monitor_printf(mon
, "'");
795 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
796 target_phys_addr_t addr
, int is_physical
)
799 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
807 if (!env
&& !is_physical
)
812 } else if (wsize
== 4) {
815 /* as default we use the current CS size */
819 if ((env
->efer
& MSR_EFER_LMA
) &&
820 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
824 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
829 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
838 nb_per_line
= line_size
/ wsize
;
843 max_digits
= (wsize
* 8 + 2) / 3;
847 max_digits
= (wsize
* 8) / 4;
851 max_digits
= (wsize
* 8 * 10 + 32) / 33;
860 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
862 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
867 cpu_physical_memory_rw(addr
, buf
, l
, 0);
872 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
873 monitor_printf(mon
, " Cannot access memory\n");
882 v
= ldub_raw(buf
+ i
);
885 v
= lduw_raw(buf
+ i
);
888 v
= (uint32_t)ldl_raw(buf
+ i
);
891 v
= ldq_raw(buf
+ i
);
894 monitor_printf(mon
, " ");
897 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
900 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
903 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
906 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
909 monitor_printc(mon
, v
);
914 monitor_printf(mon
, "\n");
920 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
922 int count
= qdict_get_int(qdict
, "count");
923 int format
= qdict_get_int(qdict
, "format");
924 int size
= qdict_get_int(qdict
, "size");
925 target_long addr
= qdict_get_int(qdict
, "addr");
927 memory_dump(mon
, count
, format
, size
, addr
, 0);
930 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
932 int count
= qdict_get_int(qdict
, "count");
933 int format
= qdict_get_int(qdict
, "format");
934 int size
= qdict_get_int(qdict
, "size");
935 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
937 memory_dump(mon
, count
, format
, size
, addr
, 1);
940 static void do_print(Monitor
*mon
, const QDict
*qdict
)
942 int format
= qdict_get_int(qdict
, "format");
943 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
945 #if TARGET_PHYS_ADDR_BITS == 32
948 monitor_printf(mon
, "%#o", val
);
951 monitor_printf(mon
, "%#x", val
);
954 monitor_printf(mon
, "%u", val
);
958 monitor_printf(mon
, "%d", val
);
961 monitor_printc(mon
, val
);
967 monitor_printf(mon
, "%#" PRIo64
, val
);
970 monitor_printf(mon
, "%#" PRIx64
, val
);
973 monitor_printf(mon
, "%" PRIu64
, val
);
977 monitor_printf(mon
, "%" PRId64
, val
);
980 monitor_printc(mon
, val
);
984 monitor_printf(mon
, "\n");
987 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
990 uint32_t size
= qdict_get_int(qdict
, "size");
991 const char *filename
= qdict_get_str(qdict
, "filename");
992 target_long addr
= qdict_get_int(qdict
, "val");
1001 f
= fopen(filename
, "wb");
1003 monitor_printf(mon
, "could not open '%s'\n", filename
);
1010 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
1011 fwrite(buf
, 1, l
, f
);
1018 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
,
1024 uint32_t size
= qdict_get_int(qdict
, "size");
1025 const char *filename
= qdict_get_str(qdict
, "filename");
1026 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
1028 f
= fopen(filename
, "wb");
1030 monitor_printf(mon
, "could not open '%s'\n", filename
);
1037 cpu_physical_memory_rw(addr
, buf
, l
, 0);
1038 fwrite(buf
, 1, l
, f
);
1046 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
1051 uint32_t start
= qdict_get_int(qdict
, "start");
1052 uint32_t size
= qdict_get_int(qdict
, "size");
1055 for(addr
= start
; addr
< (start
+ size
); addr
++) {
1056 cpu_physical_memory_rw(addr
, buf
, 1, 0);
1057 /* BSD sum algorithm ('sum' Unix command) */
1058 sum
= (sum
>> 1) | (sum
<< 15);
1061 monitor_printf(mon
, "%05d\n", sum
);
1069 static const KeyDef key_defs
[] = {
1071 { 0x36, "shift_r" },
1076 { 0xe4, "altgr_r" },
1096 { 0x0e, "backspace" },
1133 { 0x37, "asterisk" },
1136 { 0x3a, "caps_lock" },
1147 { 0x45, "num_lock" },
1148 { 0x46, "scroll_lock" },
1150 { 0xb5, "kp_divide" },
1151 { 0x37, "kp_multiply" },
1152 { 0x4a, "kp_subtract" },
1154 { 0x9c, "kp_enter" },
1155 { 0x53, "kp_decimal" },
1188 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1203 { 0xfe, "compose" },
1208 static int get_keycode(const char *key
)
1214 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1215 if (!strcmp(key
, p
->name
))
1218 if (strstart(key
, "0x", NULL
)) {
1219 ret
= strtoul(key
, &endp
, 0);
1220 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1226 #define MAX_KEYCODES 16
1227 static uint8_t keycodes
[MAX_KEYCODES
];
1228 static int nb_pending_keycodes
;
1229 static QEMUTimer
*key_timer
;
1231 static void release_keys(void *opaque
)
1235 while (nb_pending_keycodes
> 0) {
1236 nb_pending_keycodes
--;
1237 keycode
= keycodes
[nb_pending_keycodes
];
1239 kbd_put_keycode(0xe0);
1240 kbd_put_keycode(keycode
| 0x80);
1244 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1246 char keyname_buf
[16];
1248 int keyname_len
, keycode
, i
;
1249 const char *string
= qdict_get_str(qdict
, "string");
1250 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1251 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1253 if (nb_pending_keycodes
> 0) {
1254 qemu_del_timer(key_timer
);
1261 separator
= strchr(string
, '-');
1262 keyname_len
= separator
? separator
- string
: strlen(string
);
1263 if (keyname_len
> 0) {
1264 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1265 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1266 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1269 if (i
== MAX_KEYCODES
) {
1270 monitor_printf(mon
, "too many keys\n");
1273 keyname_buf
[keyname_len
] = 0;
1274 keycode
= get_keycode(keyname_buf
);
1276 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1279 keycodes
[i
++] = keycode
;
1283 string
= separator
+ 1;
1285 nb_pending_keycodes
= i
;
1286 /* key down events */
1287 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1288 keycode
= keycodes
[i
];
1290 kbd_put_keycode(0xe0);
1291 kbd_put_keycode(keycode
& 0x7f);
1293 /* delayed key up events */
1294 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1295 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1298 static int mouse_button_state
;
1300 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1303 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1304 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1305 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1306 dx
= strtol(dx_str
, NULL
, 0);
1307 dy
= strtol(dy_str
, NULL
, 0);
1310 dz
= strtol(dz_str
, NULL
, 0);
1311 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1314 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1316 int button_state
= qdict_get_int(qdict
, "button_state");
1317 mouse_button_state
= button_state
;
1318 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1321 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1323 int size
= qdict_get_int(qdict
, "size");
1324 int addr
= qdict_get_int(qdict
, "addr");
1325 int has_index
= qdict_haskey(qdict
, "index");
1330 int index
= qdict_get_int(qdict
, "index");
1331 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1339 val
= cpu_inb(addr
);
1343 val
= cpu_inw(addr
);
1347 val
= cpu_inl(addr
);
1351 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1352 suffix
, addr
, size
* 2, val
);
1355 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1357 int size
= qdict_get_int(qdict
, "size");
1358 int addr
= qdict_get_int(qdict
, "addr");
1359 int val
= qdict_get_int(qdict
, "val");
1361 addr
&= IOPORTS_MASK
;
1366 cpu_outb(addr
, val
);
1369 cpu_outw(addr
, val
);
1372 cpu_outl(addr
, val
);
1377 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1380 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1382 res
= qemu_boot_set(bootdevice
);
1384 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1385 } else if (res
> 0) {
1386 monitor_printf(mon
, "setting boot device list failed\n");
1388 monitor_printf(mon
, "no function defined to set boot device list for "
1389 "this architecture\n");
1394 * do_system_reset(): Issue a machine reset
1396 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
,
1399 qemu_system_reset_request();
1403 * do_system_powerdown(): Issue a machine powerdown
1405 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
,
1408 qemu_system_powerdown_request();
1411 #if defined(TARGET_I386)
1412 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1414 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1417 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1418 pte
& PG_PSE_MASK
? 'P' : '-',
1419 pte
& PG_DIRTY_MASK
? 'D' : '-',
1420 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1421 pte
& PG_PCD_MASK
? 'C' : '-',
1422 pte
& PG_PWT_MASK
? 'T' : '-',
1423 pte
& PG_USER_MASK
? 'U' : '-',
1424 pte
& PG_RW_MASK
? 'W' : '-');
1427 static void tlb_info(Monitor
*mon
)
1431 uint32_t pgd
, pde
, pte
;
1433 env
= mon_get_cpu();
1437 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1438 monitor_printf(mon
, "PG disabled\n");
1441 pgd
= env
->cr
[3] & ~0xfff;
1442 for(l1
= 0; l1
< 1024; l1
++) {
1443 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1444 pde
= le32_to_cpu(pde
);
1445 if (pde
& PG_PRESENT_MASK
) {
1446 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1447 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1449 for(l2
= 0; l2
< 1024; l2
++) {
1450 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1451 (uint8_t *)&pte
, 4);
1452 pte
= le32_to_cpu(pte
);
1453 if (pte
& PG_PRESENT_MASK
) {
1454 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1464 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1465 uint32_t end
, int prot
)
1468 prot1
= *plast_prot
;
1469 if (prot
!= prot1
) {
1470 if (*pstart
!= -1) {
1471 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1472 *pstart
, end
, end
- *pstart
,
1473 prot1
& PG_USER_MASK
? 'u' : '-',
1475 prot1
& PG_RW_MASK
? 'w' : '-');
1485 static void mem_info(Monitor
*mon
)
1488 int l1
, l2
, prot
, last_prot
;
1489 uint32_t pgd
, pde
, pte
, start
, end
;
1491 env
= mon_get_cpu();
1495 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1496 monitor_printf(mon
, "PG disabled\n");
1499 pgd
= env
->cr
[3] & ~0xfff;
1502 for(l1
= 0; l1
< 1024; l1
++) {
1503 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1504 pde
= le32_to_cpu(pde
);
1506 if (pde
& PG_PRESENT_MASK
) {
1507 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1508 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1509 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1511 for(l2
= 0; l2
< 1024; l2
++) {
1512 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1513 (uint8_t *)&pte
, 4);
1514 pte
= le32_to_cpu(pte
);
1515 end
= (l1
<< 22) + (l2
<< 12);
1516 if (pte
& PG_PRESENT_MASK
) {
1517 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1521 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1526 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1532 #if defined(TARGET_SH4)
1534 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1536 monitor_printf(mon
, " tlb%i:\t"
1537 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1538 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1539 "dirty=%hhu writethrough=%hhu\n",
1541 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1542 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1546 static void tlb_info(Monitor
*mon
)
1548 CPUState
*env
= mon_get_cpu();
1551 monitor_printf (mon
, "ITLB:\n");
1552 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1553 print_tlb (mon
, i
, &env
->itlb
[i
]);
1554 monitor_printf (mon
, "UTLB:\n");
1555 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1556 print_tlb (mon
, i
, &env
->utlb
[i
]);
1561 static void do_info_kvm(Monitor
*mon
)
1564 monitor_printf(mon
, "kvm support: ");
1566 monitor_printf(mon
, "enabled\n");
1568 monitor_printf(mon
, "disabled\n");
1570 monitor_printf(mon
, "kvm support: not compiled\n");
1574 static void do_info_numa(Monitor
*mon
)
1579 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1580 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1581 monitor_printf(mon
, "node %d cpus:", i
);
1582 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1583 if (env
->numa_node
== i
) {
1584 monitor_printf(mon
, " %d", env
->cpu_index
);
1587 monitor_printf(mon
, "\n");
1588 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1593 #ifdef CONFIG_PROFILER
1598 static void do_info_profile(Monitor
*mon
)
1604 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1605 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1606 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1607 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1612 static void do_info_profile(Monitor
*mon
)
1614 monitor_printf(mon
, "Internal profiler not compiled\n");
1618 /* Capture support */
1619 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1621 static void do_info_capture(Monitor
*mon
)
1626 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1627 monitor_printf(mon
, "[%d]: ", i
);
1628 s
->ops
.info (s
->opaque
);
1633 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1636 int n
= qdict_get_int(qdict
, "n");
1639 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1641 s
->ops
.destroy (s
->opaque
);
1642 QLIST_REMOVE (s
, entries
);
1649 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1651 const char *path
= qdict_get_str(qdict
, "path");
1652 int has_freq
= qdict_haskey(qdict
, "freq");
1653 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1654 int has_bits
= qdict_haskey(qdict
, "bits");
1655 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1656 int has_channels
= qdict_haskey(qdict
, "nchannels");
1657 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1660 s
= qemu_mallocz (sizeof (*s
));
1662 freq
= has_freq
? freq
: 44100;
1663 bits
= has_bits
? bits
: 16;
1664 nchannels
= has_channels
? nchannels
: 2;
1666 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1667 monitor_printf(mon
, "Faied to add wave capture\n");
1670 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1674 #if defined(TARGET_I386)
1675 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1678 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1680 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1681 if (env
->cpu_index
== cpu_index
) {
1682 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1688 static void do_info_status(Monitor
*mon
)
1692 monitor_printf(mon
, "VM status: running (single step mode)\n");
1694 monitor_printf(mon
, "VM status: running\n");
1697 monitor_printf(mon
, "VM status: paused\n");
1701 * do_balloon(): Request VM to change its memory allocation
1703 static void do_balloon(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1705 int value
= qdict_get_int(qdict
, "value");
1706 ram_addr_t target
= value
;
1707 qemu_balloon(target
<< 20);
1710 static void monitor_print_balloon(Monitor
*mon
, const QObject
*data
)
1712 monitor_printf(mon
, "balloon: actual=%d\n",
1713 (int)qint_get_int(qobject_to_qint(data
)));
1717 * do_info_balloon(): Balloon information
1719 static void do_info_balloon(Monitor
*mon
, QObject
**ret_data
)
1723 actual
= qemu_balloon_status();
1724 if (kvm_enabled() && !kvm_has_sync_mmu())
1725 qemu_error_new(QERR_KVM_MISSING_CAP
, "synchronous MMU", "balloon");
1726 else if (actual
== 0)
1727 qemu_error_new(QERR_DEVICE_NOT_ACTIVE
, "balloon");
1729 *ret_data
= QOBJECT(qint_from_int((int)(actual
>> 20)));
1732 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1734 qemu_acl
*acl
= qemu_acl_find(name
);
1737 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1742 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1744 const char *aclname
= qdict_get_str(qdict
, "aclname");
1745 qemu_acl
*acl
= find_acl(mon
, aclname
);
1746 qemu_acl_entry
*entry
;
1750 monitor_printf(mon
, "policy: %s\n",
1751 acl
->defaultDeny
? "deny" : "allow");
1752 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1754 monitor_printf(mon
, "%d: %s %s\n", i
,
1755 entry
->deny
? "deny" : "allow", entry
->match
);
1760 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1762 const char *aclname
= qdict_get_str(qdict
, "aclname");
1763 qemu_acl
*acl
= find_acl(mon
, aclname
);
1766 qemu_acl_reset(acl
);
1767 monitor_printf(mon
, "acl: removed all rules\n");
1771 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1773 const char *aclname
= qdict_get_str(qdict
, "aclname");
1774 const char *policy
= qdict_get_str(qdict
, "policy");
1775 qemu_acl
*acl
= find_acl(mon
, aclname
);
1778 if (strcmp(policy
, "allow") == 0) {
1779 acl
->defaultDeny
= 0;
1780 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1781 } else if (strcmp(policy
, "deny") == 0) {
1782 acl
->defaultDeny
= 1;
1783 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1785 monitor_printf(mon
, "acl: unknown policy '%s', "
1786 "expected 'deny' or 'allow'\n", policy
);
1791 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1793 const char *aclname
= qdict_get_str(qdict
, "aclname");
1794 const char *match
= qdict_get_str(qdict
, "match");
1795 const char *policy
= qdict_get_str(qdict
, "policy");
1796 int has_index
= qdict_haskey(qdict
, "index");
1797 int index
= qdict_get_try_int(qdict
, "index", -1);
1798 qemu_acl
*acl
= find_acl(mon
, aclname
);
1802 if (strcmp(policy
, "allow") == 0) {
1804 } else if (strcmp(policy
, "deny") == 0) {
1807 monitor_printf(mon
, "acl: unknown policy '%s', "
1808 "expected 'deny' or 'allow'\n", policy
);
1812 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1814 ret
= qemu_acl_append(acl
, deny
, match
);
1816 monitor_printf(mon
, "acl: unable to add acl entry\n");
1818 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1822 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1824 const char *aclname
= qdict_get_str(qdict
, "aclname");
1825 const char *match
= qdict_get_str(qdict
, "match");
1826 qemu_acl
*acl
= find_acl(mon
, aclname
);
1830 ret
= qemu_acl_remove(acl
, match
);
1832 monitor_printf(mon
, "acl: no matching acl entry\n");
1834 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1838 #if defined(TARGET_I386)
1839 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1842 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1843 int bank
= qdict_get_int(qdict
, "bank");
1844 uint64_t status
= qdict_get_int(qdict
, "status");
1845 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1846 uint64_t addr
= qdict_get_int(qdict
, "addr");
1847 uint64_t misc
= qdict_get_int(qdict
, "misc");
1849 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1850 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1851 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1857 static void do_getfd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1859 const char *fdname
= qdict_get_str(qdict
, "fdname");
1863 fd
= qemu_chr_get_msgfd(mon
->chr
);
1865 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1869 if (qemu_isdigit(fdname
[0])) {
1870 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1876 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1881 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1882 if (strcmp(monfd
->name
, fdname
) != 0) {
1891 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1892 monfd
->name
= qemu_strdup(fdname
);
1895 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1898 static void do_closefd(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
1900 const char *fdname
= qdict_get_str(qdict
, "fdname");
1903 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1904 if (strcmp(monfd
->name
, fdname
) != 0) {
1908 QLIST_REMOVE(monfd
, next
);
1910 qemu_free(monfd
->name
);
1915 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1919 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1921 int saved_vm_running
= vm_running
;
1922 const char *name
= qdict_get_str(qdict
, "name");
1926 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1930 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1934 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1937 if (strcmp(monfd
->name
, fdname
) != 0) {
1943 /* caller takes ownership of fd */
1944 QLIST_REMOVE(monfd
, next
);
1945 qemu_free(monfd
->name
);
1954 static const mon_cmd_t mon_cmds
[] = {
1955 #include "qemu-monitor.h"
1959 /* Please update qemu-monitor.hx when adding or changing commands */
1960 static const mon_cmd_t info_cmds
[] = {
1965 .help
= "show the version of QEMU",
1966 .user_print
= monitor_print_qobject
,
1967 .mhandler
.info_new
= do_info_version
,
1973 .help
= "show the network state",
1974 .mhandler
.info
= do_info_network
,
1980 .help
= "show the character devices",
1981 .mhandler
.info
= qemu_chr_info
,
1987 .help
= "show the block devices",
1988 .mhandler
.info
= bdrv_info
,
1991 .name
= "blockstats",
1994 .help
= "show block device statistics",
1995 .mhandler
.info
= bdrv_info_stats
,
1998 .name
= "registers",
2001 .help
= "show the cpu registers",
2002 .mhandler
.info
= do_info_registers
,
2008 .help
= "show infos for each CPU",
2009 .user_print
= monitor_print_cpus
,
2010 .mhandler
.info_new
= do_info_cpus
,
2016 .help
= "show the command line history",
2017 .mhandler
.info
= do_info_history
,
2023 .help
= "show the interrupts statistics (if available)",
2024 .mhandler
.info
= irq_info
,
2030 .help
= "show i8259 (PIC) state",
2031 .mhandler
.info
= pic_info
,
2037 .help
= "show PCI info",
2038 .mhandler
.info
= pci_info
,
2040 #if defined(TARGET_I386) || defined(TARGET_SH4)
2045 .help
= "show virtual to physical memory mappings",
2046 .mhandler
.info
= tlb_info
,
2049 #if defined(TARGET_I386)
2054 .help
= "show the active virtual memory mappings",
2055 .mhandler
.info
= mem_info
,
2061 .help
= "show state of HPET",
2062 .mhandler
.info
= do_info_hpet
,
2069 .help
= "show dynamic compiler info",
2070 .mhandler
.info
= do_info_jit
,
2076 .help
= "show KVM information",
2077 .mhandler
.info
= do_info_kvm
,
2083 .help
= "show NUMA information",
2084 .mhandler
.info
= do_info_numa
,
2090 .help
= "show guest USB devices",
2091 .mhandler
.info
= usb_info
,
2097 .help
= "show host USB devices",
2098 .mhandler
.info
= usb_host_info
,
2104 .help
= "show profiling information",
2105 .mhandler
.info
= do_info_profile
,
2111 .help
= "show capture information",
2112 .mhandler
.info
= do_info_capture
,
2115 .name
= "snapshots",
2118 .help
= "show the currently saved VM snapshots",
2119 .mhandler
.info
= do_info_snapshots
,
2125 .help
= "show the current VM status (running|paused)",
2126 .mhandler
.info
= do_info_status
,
2132 .help
= "show guest PCMCIA status",
2133 .mhandler
.info
= pcmcia_info
,
2139 .help
= "show which guest mouse is receiving events",
2140 .mhandler
.info
= do_info_mice
,
2146 .help
= "show the vnc server status",
2147 .mhandler
.info
= do_info_vnc
,
2153 .help
= "show the current VM name",
2154 .mhandler
.info
= do_info_name
,
2160 .help
= "show the current VM UUID",
2161 .mhandler
.info
= do_info_uuid
,
2163 #if defined(TARGET_PPC)
2168 .help
= "show CPU statistics",
2169 .mhandler
.info
= do_info_cpu_stats
,
2172 #if defined(CONFIG_SLIRP)
2177 .help
= "show user network stack connection states",
2178 .mhandler
.info
= do_info_usernet
,
2185 .help
= "show migration status",
2186 .mhandler
.info
= do_info_migrate
,
2192 .help
= "show balloon information",
2193 .user_print
= monitor_print_balloon
,
2194 .mhandler
.info_new
= do_info_balloon
,
2200 .help
= "show device tree",
2201 .mhandler
.info
= do_info_qtree
,
2207 .help
= "show qdev device model list",
2208 .mhandler
.info
= do_info_qdm
,
2214 .help
= "show roms",
2215 .mhandler
.info
= do_info_roms
,
2222 /*******************************************************************/
2224 static const char *pch
;
2225 static jmp_buf expr_env
;
2230 typedef struct MonitorDef
{
2233 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2237 #if defined(TARGET_I386)
2238 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2240 CPUState
*env
= mon_get_cpu();
2243 return env
->eip
+ env
->segs
[R_CS
].base
;
2247 #if defined(TARGET_PPC)
2248 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2250 CPUState
*env
= mon_get_cpu();
2258 for (i
= 0; i
< 8; i
++)
2259 u
|= env
->crf
[i
] << (32 - (4 * i
));
2264 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2266 CPUState
*env
= mon_get_cpu();
2272 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2274 CPUState
*env
= mon_get_cpu();
2280 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2282 CPUState
*env
= mon_get_cpu();
2285 return cpu_ppc_load_decr(env
);
2288 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2290 CPUState
*env
= mon_get_cpu();
2293 return cpu_ppc_load_tbu(env
);
2296 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2298 CPUState
*env
= mon_get_cpu();
2301 return cpu_ppc_load_tbl(env
);
2305 #if defined(TARGET_SPARC)
2306 #ifndef TARGET_SPARC64
2307 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2309 CPUState
*env
= mon_get_cpu();
2312 return GET_PSR(env
);
2316 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2318 CPUState
*env
= mon_get_cpu();
2321 return env
->regwptr
[val
];
2325 static const MonitorDef monitor_defs
[] = {
2328 #define SEG(name, seg) \
2329 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2330 { name ".base", offsetof(CPUState, segs[seg].base) },\
2331 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2333 { "eax", offsetof(CPUState
, regs
[0]) },
2334 { "ecx", offsetof(CPUState
, regs
[1]) },
2335 { "edx", offsetof(CPUState
, regs
[2]) },
2336 { "ebx", offsetof(CPUState
, regs
[3]) },
2337 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2338 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2339 { "esi", offsetof(CPUState
, regs
[6]) },
2340 { "edi", offsetof(CPUState
, regs
[7]) },
2341 #ifdef TARGET_X86_64
2342 { "r8", offsetof(CPUState
, regs
[8]) },
2343 { "r9", offsetof(CPUState
, regs
[9]) },
2344 { "r10", offsetof(CPUState
, regs
[10]) },
2345 { "r11", offsetof(CPUState
, regs
[11]) },
2346 { "r12", offsetof(CPUState
, regs
[12]) },
2347 { "r13", offsetof(CPUState
, regs
[13]) },
2348 { "r14", offsetof(CPUState
, regs
[14]) },
2349 { "r15", offsetof(CPUState
, regs
[15]) },
2351 { "eflags", offsetof(CPUState
, eflags
) },
2352 { "eip", offsetof(CPUState
, eip
) },
2359 { "pc", 0, monitor_get_pc
, },
2360 #elif defined(TARGET_PPC)
2361 /* General purpose registers */
2362 { "r0", offsetof(CPUState
, gpr
[0]) },
2363 { "r1", offsetof(CPUState
, gpr
[1]) },
2364 { "r2", offsetof(CPUState
, gpr
[2]) },
2365 { "r3", offsetof(CPUState
, gpr
[3]) },
2366 { "r4", offsetof(CPUState
, gpr
[4]) },
2367 { "r5", offsetof(CPUState
, gpr
[5]) },
2368 { "r6", offsetof(CPUState
, gpr
[6]) },
2369 { "r7", offsetof(CPUState
, gpr
[7]) },
2370 { "r8", offsetof(CPUState
, gpr
[8]) },
2371 { "r9", offsetof(CPUState
, gpr
[9]) },
2372 { "r10", offsetof(CPUState
, gpr
[10]) },
2373 { "r11", offsetof(CPUState
, gpr
[11]) },
2374 { "r12", offsetof(CPUState
, gpr
[12]) },
2375 { "r13", offsetof(CPUState
, gpr
[13]) },
2376 { "r14", offsetof(CPUState
, gpr
[14]) },
2377 { "r15", offsetof(CPUState
, gpr
[15]) },
2378 { "r16", offsetof(CPUState
, gpr
[16]) },
2379 { "r17", offsetof(CPUState
, gpr
[17]) },
2380 { "r18", offsetof(CPUState
, gpr
[18]) },
2381 { "r19", offsetof(CPUState
, gpr
[19]) },
2382 { "r20", offsetof(CPUState
, gpr
[20]) },
2383 { "r21", offsetof(CPUState
, gpr
[21]) },
2384 { "r22", offsetof(CPUState
, gpr
[22]) },
2385 { "r23", offsetof(CPUState
, gpr
[23]) },
2386 { "r24", offsetof(CPUState
, gpr
[24]) },
2387 { "r25", offsetof(CPUState
, gpr
[25]) },
2388 { "r26", offsetof(CPUState
, gpr
[26]) },
2389 { "r27", offsetof(CPUState
, gpr
[27]) },
2390 { "r28", offsetof(CPUState
, gpr
[28]) },
2391 { "r29", offsetof(CPUState
, gpr
[29]) },
2392 { "r30", offsetof(CPUState
, gpr
[30]) },
2393 { "r31", offsetof(CPUState
, gpr
[31]) },
2394 /* Floating point registers */
2395 { "f0", offsetof(CPUState
, fpr
[0]) },
2396 { "f1", offsetof(CPUState
, fpr
[1]) },
2397 { "f2", offsetof(CPUState
, fpr
[2]) },
2398 { "f3", offsetof(CPUState
, fpr
[3]) },
2399 { "f4", offsetof(CPUState
, fpr
[4]) },
2400 { "f5", offsetof(CPUState
, fpr
[5]) },
2401 { "f6", offsetof(CPUState
, fpr
[6]) },
2402 { "f7", offsetof(CPUState
, fpr
[7]) },
2403 { "f8", offsetof(CPUState
, fpr
[8]) },
2404 { "f9", offsetof(CPUState
, fpr
[9]) },
2405 { "f10", offsetof(CPUState
, fpr
[10]) },
2406 { "f11", offsetof(CPUState
, fpr
[11]) },
2407 { "f12", offsetof(CPUState
, fpr
[12]) },
2408 { "f13", offsetof(CPUState
, fpr
[13]) },
2409 { "f14", offsetof(CPUState
, fpr
[14]) },
2410 { "f15", offsetof(CPUState
, fpr
[15]) },
2411 { "f16", offsetof(CPUState
, fpr
[16]) },
2412 { "f17", offsetof(CPUState
, fpr
[17]) },
2413 { "f18", offsetof(CPUState
, fpr
[18]) },
2414 { "f19", offsetof(CPUState
, fpr
[19]) },
2415 { "f20", offsetof(CPUState
, fpr
[20]) },
2416 { "f21", offsetof(CPUState
, fpr
[21]) },
2417 { "f22", offsetof(CPUState
, fpr
[22]) },
2418 { "f23", offsetof(CPUState
, fpr
[23]) },
2419 { "f24", offsetof(CPUState
, fpr
[24]) },
2420 { "f25", offsetof(CPUState
, fpr
[25]) },
2421 { "f26", offsetof(CPUState
, fpr
[26]) },
2422 { "f27", offsetof(CPUState
, fpr
[27]) },
2423 { "f28", offsetof(CPUState
, fpr
[28]) },
2424 { "f29", offsetof(CPUState
, fpr
[29]) },
2425 { "f30", offsetof(CPUState
, fpr
[30]) },
2426 { "f31", offsetof(CPUState
, fpr
[31]) },
2427 { "fpscr", offsetof(CPUState
, fpscr
) },
2428 /* Next instruction pointer */
2429 { "nip|pc", offsetof(CPUState
, nip
) },
2430 { "lr", offsetof(CPUState
, lr
) },
2431 { "ctr", offsetof(CPUState
, ctr
) },
2432 { "decr", 0, &monitor_get_decr
, },
2433 { "ccr", 0, &monitor_get_ccr
, },
2434 /* Machine state register */
2435 { "msr", 0, &monitor_get_msr
, },
2436 { "xer", 0, &monitor_get_xer
, },
2437 { "tbu", 0, &monitor_get_tbu
, },
2438 { "tbl", 0, &monitor_get_tbl
, },
2439 #if defined(TARGET_PPC64)
2440 /* Address space register */
2441 { "asr", offsetof(CPUState
, asr
) },
2443 /* Segment registers */
2444 { "sdr1", offsetof(CPUState
, sdr1
) },
2445 { "sr0", offsetof(CPUState
, sr
[0]) },
2446 { "sr1", offsetof(CPUState
, sr
[1]) },
2447 { "sr2", offsetof(CPUState
, sr
[2]) },
2448 { "sr3", offsetof(CPUState
, sr
[3]) },
2449 { "sr4", offsetof(CPUState
, sr
[4]) },
2450 { "sr5", offsetof(CPUState
, sr
[5]) },
2451 { "sr6", offsetof(CPUState
, sr
[6]) },
2452 { "sr7", offsetof(CPUState
, sr
[7]) },
2453 { "sr8", offsetof(CPUState
, sr
[8]) },
2454 { "sr9", offsetof(CPUState
, sr
[9]) },
2455 { "sr10", offsetof(CPUState
, sr
[10]) },
2456 { "sr11", offsetof(CPUState
, sr
[11]) },
2457 { "sr12", offsetof(CPUState
, sr
[12]) },
2458 { "sr13", offsetof(CPUState
, sr
[13]) },
2459 { "sr14", offsetof(CPUState
, sr
[14]) },
2460 { "sr15", offsetof(CPUState
, sr
[15]) },
2461 /* Too lazy to put BATs and SPRs ... */
2462 #elif defined(TARGET_SPARC)
2463 { "g0", offsetof(CPUState
, gregs
[0]) },
2464 { "g1", offsetof(CPUState
, gregs
[1]) },
2465 { "g2", offsetof(CPUState
, gregs
[2]) },
2466 { "g3", offsetof(CPUState
, gregs
[3]) },
2467 { "g4", offsetof(CPUState
, gregs
[4]) },
2468 { "g5", offsetof(CPUState
, gregs
[5]) },
2469 { "g6", offsetof(CPUState
, gregs
[6]) },
2470 { "g7", offsetof(CPUState
, gregs
[7]) },
2471 { "o0", 0, monitor_get_reg
},
2472 { "o1", 1, monitor_get_reg
},
2473 { "o2", 2, monitor_get_reg
},
2474 { "o3", 3, monitor_get_reg
},
2475 { "o4", 4, monitor_get_reg
},
2476 { "o5", 5, monitor_get_reg
},
2477 { "o6", 6, monitor_get_reg
},
2478 { "o7", 7, monitor_get_reg
},
2479 { "l0", 8, monitor_get_reg
},
2480 { "l1", 9, monitor_get_reg
},
2481 { "l2", 10, monitor_get_reg
},
2482 { "l3", 11, monitor_get_reg
},
2483 { "l4", 12, monitor_get_reg
},
2484 { "l5", 13, monitor_get_reg
},
2485 { "l6", 14, monitor_get_reg
},
2486 { "l7", 15, monitor_get_reg
},
2487 { "i0", 16, monitor_get_reg
},
2488 { "i1", 17, monitor_get_reg
},
2489 { "i2", 18, monitor_get_reg
},
2490 { "i3", 19, monitor_get_reg
},
2491 { "i4", 20, monitor_get_reg
},
2492 { "i5", 21, monitor_get_reg
},
2493 { "i6", 22, monitor_get_reg
},
2494 { "i7", 23, monitor_get_reg
},
2495 { "pc", offsetof(CPUState
, pc
) },
2496 { "npc", offsetof(CPUState
, npc
) },
2497 { "y", offsetof(CPUState
, y
) },
2498 #ifndef TARGET_SPARC64
2499 { "psr", 0, &monitor_get_psr
, },
2500 { "wim", offsetof(CPUState
, wim
) },
2502 { "tbr", offsetof(CPUState
, tbr
) },
2503 { "fsr", offsetof(CPUState
, fsr
) },
2504 { "f0", offsetof(CPUState
, fpr
[0]) },
2505 { "f1", offsetof(CPUState
, fpr
[1]) },
2506 { "f2", offsetof(CPUState
, fpr
[2]) },
2507 { "f3", offsetof(CPUState
, fpr
[3]) },
2508 { "f4", offsetof(CPUState
, fpr
[4]) },
2509 { "f5", offsetof(CPUState
, fpr
[5]) },
2510 { "f6", offsetof(CPUState
, fpr
[6]) },
2511 { "f7", offsetof(CPUState
, fpr
[7]) },
2512 { "f8", offsetof(CPUState
, fpr
[8]) },
2513 { "f9", offsetof(CPUState
, fpr
[9]) },
2514 { "f10", offsetof(CPUState
, fpr
[10]) },
2515 { "f11", offsetof(CPUState
, fpr
[11]) },
2516 { "f12", offsetof(CPUState
, fpr
[12]) },
2517 { "f13", offsetof(CPUState
, fpr
[13]) },
2518 { "f14", offsetof(CPUState
, fpr
[14]) },
2519 { "f15", offsetof(CPUState
, fpr
[15]) },
2520 { "f16", offsetof(CPUState
, fpr
[16]) },
2521 { "f17", offsetof(CPUState
, fpr
[17]) },
2522 { "f18", offsetof(CPUState
, fpr
[18]) },
2523 { "f19", offsetof(CPUState
, fpr
[19]) },
2524 { "f20", offsetof(CPUState
, fpr
[20]) },
2525 { "f21", offsetof(CPUState
, fpr
[21]) },
2526 { "f22", offsetof(CPUState
, fpr
[22]) },
2527 { "f23", offsetof(CPUState
, fpr
[23]) },
2528 { "f24", offsetof(CPUState
, fpr
[24]) },
2529 { "f25", offsetof(CPUState
, fpr
[25]) },
2530 { "f26", offsetof(CPUState
, fpr
[26]) },
2531 { "f27", offsetof(CPUState
, fpr
[27]) },
2532 { "f28", offsetof(CPUState
, fpr
[28]) },
2533 { "f29", offsetof(CPUState
, fpr
[29]) },
2534 { "f30", offsetof(CPUState
, fpr
[30]) },
2535 { "f31", offsetof(CPUState
, fpr
[31]) },
2536 #ifdef TARGET_SPARC64
2537 { "f32", offsetof(CPUState
, fpr
[32]) },
2538 { "f34", offsetof(CPUState
, fpr
[34]) },
2539 { "f36", offsetof(CPUState
, fpr
[36]) },
2540 { "f38", offsetof(CPUState
, fpr
[38]) },
2541 { "f40", offsetof(CPUState
, fpr
[40]) },
2542 { "f42", offsetof(CPUState
, fpr
[42]) },
2543 { "f44", offsetof(CPUState
, fpr
[44]) },
2544 { "f46", offsetof(CPUState
, fpr
[46]) },
2545 { "f48", offsetof(CPUState
, fpr
[48]) },
2546 { "f50", offsetof(CPUState
, fpr
[50]) },
2547 { "f52", offsetof(CPUState
, fpr
[52]) },
2548 { "f54", offsetof(CPUState
, fpr
[54]) },
2549 { "f56", offsetof(CPUState
, fpr
[56]) },
2550 { "f58", offsetof(CPUState
, fpr
[58]) },
2551 { "f60", offsetof(CPUState
, fpr
[60]) },
2552 { "f62", offsetof(CPUState
, fpr
[62]) },
2553 { "asi", offsetof(CPUState
, asi
) },
2554 { "pstate", offsetof(CPUState
, pstate
) },
2555 { "cansave", offsetof(CPUState
, cansave
) },
2556 { "canrestore", offsetof(CPUState
, canrestore
) },
2557 { "otherwin", offsetof(CPUState
, otherwin
) },
2558 { "wstate", offsetof(CPUState
, wstate
) },
2559 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2560 { "fprs", offsetof(CPUState
, fprs
) },
2566 static void expr_error(Monitor
*mon
, const char *msg
)
2568 monitor_printf(mon
, "%s\n", msg
);
2569 longjmp(expr_env
, 1);
2572 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2573 static int get_monitor_def(target_long
*pval
, const char *name
)
2575 const MonitorDef
*md
;
2578 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2579 if (compare_cmd(name
, md
->name
)) {
2580 if (md
->get_value
) {
2581 *pval
= md
->get_value(md
, md
->offset
);
2583 CPUState
*env
= mon_get_cpu();
2586 ptr
= (uint8_t *)env
+ md
->offset
;
2589 *pval
= *(int32_t *)ptr
;
2592 *pval
= *(target_long
*)ptr
;
2605 static void next(void)
2609 while (qemu_isspace(*pch
))
2614 static int64_t expr_sum(Monitor
*mon
);
2616 static int64_t expr_unary(Monitor
*mon
)
2625 n
= expr_unary(mon
);
2629 n
= -expr_unary(mon
);
2633 n
= ~expr_unary(mon
);
2639 expr_error(mon
, "')' expected");
2646 expr_error(mon
, "character constant expected");
2650 expr_error(mon
, "missing terminating \' character");
2660 while ((*pch
>= 'a' && *pch
<= 'z') ||
2661 (*pch
>= 'A' && *pch
<= 'Z') ||
2662 (*pch
>= '0' && *pch
<= '9') ||
2663 *pch
== '_' || *pch
== '.') {
2664 if ((q
- buf
) < sizeof(buf
) - 1)
2668 while (qemu_isspace(*pch
))
2671 ret
= get_monitor_def(®
, buf
);
2673 expr_error(mon
, "unknown register");
2675 expr_error(mon
, "no cpu defined");
2680 expr_error(mon
, "unexpected end of expression");
2684 #if TARGET_PHYS_ADDR_BITS > 32
2685 n
= strtoull(pch
, &p
, 0);
2687 n
= strtoul(pch
, &p
, 0);
2690 expr_error(mon
, "invalid char in expression");
2693 while (qemu_isspace(*pch
))
2701 static int64_t expr_prod(Monitor
*mon
)
2706 val
= expr_unary(mon
);
2709 if (op
!= '*' && op
!= '/' && op
!= '%')
2712 val2
= expr_unary(mon
);
2721 expr_error(mon
, "division by zero");
2732 static int64_t expr_logic(Monitor
*mon
)
2737 val
= expr_prod(mon
);
2740 if (op
!= '&' && op
!= '|' && op
!= '^')
2743 val2
= expr_prod(mon
);
2760 static int64_t expr_sum(Monitor
*mon
)
2765 val
= expr_logic(mon
);
2768 if (op
!= '+' && op
!= '-')
2771 val2
= expr_logic(mon
);
2780 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2783 if (setjmp(expr_env
)) {
2787 while (qemu_isspace(*pch
))
2789 *pval
= expr_sum(mon
);
2794 static int get_str(char *buf
, int buf_size
, const char **pp
)
2802 while (qemu_isspace(*p
))
2812 while (*p
!= '\0' && *p
!= '\"') {
2828 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2831 if ((q
- buf
) < buf_size
- 1) {
2835 if ((q
- buf
) < buf_size
- 1) {
2842 qemu_printf("unterminated string\n");
2847 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2848 if ((q
- buf
) < buf_size
- 1) {
2860 * Store the command-name in cmdname, and return a pointer to
2861 * the remaining of the command string.
2863 static const char *get_command_name(const char *cmdline
,
2864 char *cmdname
, size_t nlen
)
2867 const char *p
, *pstart
;
2870 while (qemu_isspace(*p
))
2875 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2880 memcpy(cmdname
, pstart
, len
);
2881 cmdname
[len
] = '\0';
2886 * Read key of 'type' into 'key' and return the current
2889 static char *key_get_info(const char *type
, char **key
)
2897 p
= strchr(type
, ':');
2904 str
= qemu_malloc(len
+ 1);
2905 memcpy(str
, type
, len
);
2912 static int default_fmt_format
= 'x';
2913 static int default_fmt_size
= 4;
2917 static int is_valid_option(const char *c
, const char *typestr
)
2925 typestr
= strstr(typestr
, option
);
2926 return (typestr
!= NULL
);
2929 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2930 const char *cmdline
,
2933 const char *p
, *typestr
;
2935 const mon_cmd_t
*cmd
;
2941 monitor_printf(mon
, "command='%s'\n", cmdline
);
2944 /* extract the command name */
2945 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2949 /* find the command */
2950 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2951 if (compare_cmd(cmdname
, cmd
->name
))
2955 if (cmd
->name
== NULL
) {
2956 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2960 /* parse the parameters */
2961 typestr
= cmd
->args_type
;
2963 typestr
= key_get_info(typestr
, &key
);
2975 while (qemu_isspace(*p
))
2977 if (*typestr
== '?') {
2980 /* no optional string: NULL argument */
2984 ret
= get_str(buf
, sizeof(buf
), &p
);
2988 monitor_printf(mon
, "%s: filename expected\n",
2992 monitor_printf(mon
, "%s: block device name expected\n",
2996 monitor_printf(mon
, "%s: string expected\n", cmdname
);
3001 qdict_put(qdict
, key
, qstring_from_str(buf
));
3006 int count
, format
, size
;
3008 while (qemu_isspace(*p
))
3014 if (qemu_isdigit(*p
)) {
3016 while (qemu_isdigit(*p
)) {
3017 count
= count
* 10 + (*p
- '0');
3055 if (*p
!= '\0' && !qemu_isspace(*p
)) {
3056 monitor_printf(mon
, "invalid char in format: '%c'\n",
3061 format
= default_fmt_format
;
3062 if (format
!= 'i') {
3063 /* for 'i', not specifying a size gives -1 as size */
3065 size
= default_fmt_size
;
3066 default_fmt_size
= size
;
3068 default_fmt_format
= format
;
3071 format
= default_fmt_format
;
3072 if (format
!= 'i') {
3073 size
= default_fmt_size
;
3078 qdict_put(qdict
, "count", qint_from_int(count
));
3079 qdict_put(qdict
, "format", qint_from_int(format
));
3080 qdict_put(qdict
, "size", qint_from_int(size
));
3088 while (qemu_isspace(*p
))
3090 if (*typestr
== '?' || *typestr
== '.') {
3091 if (*typestr
== '?') {
3099 while (qemu_isspace(*p
))
3108 if (get_expr(mon
, &val
, &p
))
3110 /* Check if 'i' is greater than 32-bit */
3111 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
3112 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
3113 monitor_printf(mon
, "integer is for 32-bit values\n");
3116 qdict_put(qdict
, key
, qint_from_int(val
));
3121 const char *tmp
= p
;
3122 int has_option
, skip_key
= 0;
3128 while (qemu_isspace(*p
))
3134 if(!is_valid_option(p
, typestr
)) {
3136 monitor_printf(mon
, "%s: unsupported option -%c\n",
3150 qdict_put(qdict
, key
, qint_from_int(has_option
));
3155 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
3161 /* check that all arguments were parsed */
3162 while (qemu_isspace(*p
))
3165 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3177 static void monitor_print_error(Monitor
*mon
)
3179 qerror_print(mon
->error
);
3180 QDECREF(mon
->error
);
3184 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3187 const mon_cmd_t
*cmd
;
3189 qdict
= qdict_new();
3191 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3195 qemu_errors_to_mon(mon
);
3197 if (monitor_handler_ported(cmd
)) {
3198 QObject
*data
= NULL
;
3200 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3202 cmd
->user_print(mon
, data
);
3204 qobject_decref(data
);
3206 cmd
->mhandler
.cmd(mon
, qdict
);
3209 if (monitor_has_error(mon
))
3210 monitor_print_error(mon
);
3212 qemu_errors_to_previous();
3218 static void cmd_completion(const char *name
, const char *list
)
3220 const char *p
, *pstart
;
3229 p
= pstart
+ strlen(pstart
);
3231 if (len
> sizeof(cmd
) - 2)
3232 len
= sizeof(cmd
) - 2;
3233 memcpy(cmd
, pstart
, len
);
3235 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3236 readline_add_completion(cur_mon
->rs
, cmd
);
3244 static void file_completion(const char *input
)
3249 char file
[1024], file_prefix
[1024];
3253 p
= strrchr(input
, '/');
3256 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3257 pstrcpy(path
, sizeof(path
), ".");
3259 input_path_len
= p
- input
+ 1;
3260 memcpy(path
, input
, input_path_len
);
3261 if (input_path_len
> sizeof(path
) - 1)
3262 input_path_len
= sizeof(path
) - 1;
3263 path
[input_path_len
] = '\0';
3264 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3266 #ifdef DEBUG_COMPLETION
3267 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3268 input
, path
, file_prefix
);
3270 ffs
= opendir(path
);
3278 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3279 memcpy(file
, input
, input_path_len
);
3280 if (input_path_len
< sizeof(file
))
3281 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3283 /* stat the file to find out if it's a directory.
3284 * In that case add a slash to speed up typing long paths
3287 if(S_ISDIR(sb
.st_mode
))
3288 pstrcat(file
, sizeof(file
), "/");
3289 readline_add_completion(cur_mon
->rs
, file
);
3295 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3297 const char *name
= bdrv_get_device_name(bs
);
3298 const char *input
= opaque
;
3300 if (input
[0] == '\0' ||
3301 !strncmp(name
, (char *)input
, strlen(input
))) {
3302 readline_add_completion(cur_mon
->rs
, name
);
3306 /* NOTE: this parser is an approximate form of the real command parser */
3307 static void parse_cmdline(const char *cmdline
,
3308 int *pnb_args
, char **args
)
3317 while (qemu_isspace(*p
))
3321 if (nb_args
>= MAX_ARGS
)
3323 ret
= get_str(buf
, sizeof(buf
), &p
);
3324 args
[nb_args
] = qemu_strdup(buf
);
3329 *pnb_args
= nb_args
;
3332 static const char *next_arg_type(const char *typestr
)
3334 const char *p
= strchr(typestr
, ':');
3335 return (p
!= NULL
? ++p
: typestr
);
3338 static void monitor_find_completion(const char *cmdline
)
3340 const char *cmdname
;
3341 char *args
[MAX_ARGS
];
3342 int nb_args
, i
, len
;
3343 const char *ptype
, *str
;
3344 const mon_cmd_t
*cmd
;
3347 parse_cmdline(cmdline
, &nb_args
, args
);
3348 #ifdef DEBUG_COMPLETION
3349 for(i
= 0; i
< nb_args
; i
++) {
3350 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3354 /* if the line ends with a space, it means we want to complete the
3356 len
= strlen(cmdline
);
3357 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3358 if (nb_args
>= MAX_ARGS
)
3360 args
[nb_args
++] = qemu_strdup("");
3363 /* command completion */
3368 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3369 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3370 cmd_completion(cmdname
, cmd
->name
);
3373 /* find the command */
3374 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3375 if (compare_cmd(args
[0], cmd
->name
))
3380 ptype
= next_arg_type(cmd
->args_type
);
3381 for(i
= 0; i
< nb_args
- 2; i
++) {
3382 if (*ptype
!= '\0') {
3383 ptype
= next_arg_type(ptype
);
3384 while (*ptype
== '?')
3385 ptype
= next_arg_type(ptype
);
3388 str
= args
[nb_args
- 1];
3389 if (*ptype
== '-' && ptype
[1] != '\0') {
3394 /* file completion */
3395 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3396 file_completion(str
);
3399 /* block device name completion */
3400 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3401 bdrv_iterate(block_completion_it
, (void *)str
);
3404 /* XXX: more generic ? */
3405 if (!strcmp(cmd
->name
, "info")) {
3406 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3407 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3408 cmd_completion(str
, cmd
->name
);
3410 } else if (!strcmp(cmd
->name
, "sendkey")) {
3411 char *sep
= strrchr(str
, '-');
3414 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3415 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3416 cmd_completion(str
, key
->name
);
3418 } else if (!strcmp(cmd
->name
, "help|?")) {
3419 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3420 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3421 cmd_completion(str
, cmd
->name
);
3429 for(i
= 0; i
< nb_args
; i
++)
3433 static int monitor_can_read(void *opaque
)
3435 Monitor
*mon
= opaque
;
3437 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3440 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3442 Monitor
*old_mon
= cur_mon
;
3448 for (i
= 0; i
< size
; i
++)
3449 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3451 if (size
== 0 || buf
[size
- 1] != 0)
3452 monitor_printf(cur_mon
, "corrupted command\n");
3454 monitor_handle_command(cur_mon
, (char *)buf
);
3460 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3462 monitor_suspend(mon
);
3463 monitor_handle_command(mon
, cmdline
);
3464 monitor_resume(mon
);
3467 int monitor_suspend(Monitor
*mon
)
3475 void monitor_resume(Monitor
*mon
)
3479 if (--mon
->suspend_cnt
== 0)
3480 readline_show_prompt(mon
->rs
);
3483 static void monitor_event(void *opaque
, int event
)
3485 Monitor
*mon
= opaque
;
3488 case CHR_EVENT_MUX_IN
:
3490 if (mon
->reset_seen
) {
3491 readline_restart(mon
->rs
);
3492 monitor_resume(mon
);
3495 mon
->suspend_cnt
= 0;
3499 case CHR_EVENT_MUX_OUT
:
3500 if (mon
->reset_seen
) {
3501 if (mon
->suspend_cnt
== 0) {
3502 monitor_printf(mon
, "\n");
3505 monitor_suspend(mon
);
3512 case CHR_EVENT_OPENED
:
3513 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3514 "information\n", QEMU_VERSION
);
3515 if (!mon
->mux_out
) {
3516 readline_show_prompt(mon
->rs
);
3518 mon
->reset_seen
= 1;
3532 void monitor_init(CharDriverState
*chr
, int flags
)
3534 static int is_first_init
= 1;
3537 if (is_first_init
) {
3538 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3542 mon
= qemu_mallocz(sizeof(*mon
));
3546 if (flags
& MONITOR_USE_READLINE
) {
3547 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3548 monitor_read_command(mon
, 0);
3551 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3554 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3555 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3559 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3561 BlockDriverState
*bs
= opaque
;
3564 if (bdrv_set_key(bs
, password
) != 0) {
3565 monitor_printf(mon
, "invalid password\n");
3568 if (mon
->password_completion_cb
)
3569 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3571 monitor_read_command(mon
, 1);
3574 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3575 BlockDriverCompletionFunc
*completion_cb
,
3580 if (!bdrv_key_required(bs
)) {
3582 completion_cb(opaque
, 0);
3586 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3587 bdrv_get_encrypted_filename(bs
));
3589 mon
->password_completion_cb
= completion_cb
;
3590 mon
->password_opaque
= opaque
;
3592 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3594 if (err
&& completion_cb
)
3595 completion_cb(opaque
, err
);
3598 typedef struct QemuErrorSink QemuErrorSink
;
3599 struct QemuErrorSink
{
3608 QemuErrorSink
*previous
;
3611 static QemuErrorSink
*qemu_error_sink
;
3613 void qemu_errors_to_file(FILE *fp
)
3615 QemuErrorSink
*sink
;
3617 sink
= qemu_mallocz(sizeof(*sink
));
3618 sink
->dest
= ERR_SINK_FILE
;
3620 sink
->previous
= qemu_error_sink
;
3621 qemu_error_sink
= sink
;
3624 void qemu_errors_to_mon(Monitor
*mon
)
3626 QemuErrorSink
*sink
;
3628 sink
= qemu_mallocz(sizeof(*sink
));
3629 sink
->dest
= ERR_SINK_MONITOR
;
3631 sink
->previous
= qemu_error_sink
;
3632 qemu_error_sink
= sink
;
3635 void qemu_errors_to_previous(void)
3637 QemuErrorSink
*sink
;
3639 assert(qemu_error_sink
!= NULL
);
3640 sink
= qemu_error_sink
;
3641 qemu_error_sink
= sink
->previous
;
3645 void qemu_error(const char *fmt
, ...)
3649 assert(qemu_error_sink
!= NULL
);
3650 switch (qemu_error_sink
->dest
) {
3652 va_start(args
, fmt
);
3653 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3656 case ERR_SINK_MONITOR
:
3657 va_start(args
, fmt
);
3658 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);
3664 void qemu_error_internal(const char *file
, int linenr
, const char *func
,
3665 const char *fmt
, ...)
3670 assert(qemu_error_sink
!= NULL
);
3673 qerror
= qerror_from_info(file
, linenr
, func
, fmt
, &va
);
3676 switch (qemu_error_sink
->dest
) {
3678 qerror_print(qerror
);
3681 case ERR_SINK_MONITOR
:
3682 assert(qemu_error_sink
->mon
->error
== NULL
);
3683 qemu_error_sink
->mon
->error
= qerror
;