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"
53 //#define DEBUG_COMPLETION
59 * 'B' block device name
60 * 's' string (accept optional quote)
62 * 'l' target long (32 or 64 bit)
63 * '/' optional gdb-like print format (like "/10x")
65 * '?' optional type (for all types, except '/')
66 * '.' other form of optional type (for 'i' and 'l')
67 * '-' optional parameter (eg. '-f')
71 typedef struct mon_cmd_t
{
73 const char *args_type
;
76 void (*user_print
)(Monitor
*mon
, const QObject
*data
);
78 void (*info
)(Monitor
*mon
);
79 void (*cmd
)(Monitor
*mon
, const QDict
*qdict
);
83 /* file descriptors passed via SCM_RIGHTS */
84 typedef struct mon_fd_t mon_fd_t
;
88 QLIST_ENTRY(mon_fd_t
) next
;
101 BlockDriverCompletionFunc
*password_completion_cb
;
102 void *password_opaque
;
103 QLIST_HEAD(,mon_fd_t
) fds
;
104 QLIST_ENTRY(Monitor
) entry
;
107 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
109 static const mon_cmd_t mon_cmds
[];
110 static const mon_cmd_t info_cmds
[];
112 Monitor
*cur_mon
= NULL
;
114 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
117 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
119 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
121 readline_show_prompt(mon
->rs
);
124 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
128 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
129 /* prompt is printed on return from the command handler */
132 monitor_printf(mon
, "terminal does not support password prompting\n");
137 void monitor_flush(Monitor
*mon
)
139 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
140 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
141 mon
->outbuf_index
= 0;
145 /* flush at every end of line or if the buffer is full */
146 static void monitor_puts(Monitor
*mon
, const char *str
)
158 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
159 mon
->outbuf
[mon
->outbuf_index
++] = c
;
160 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
166 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
169 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
170 monitor_puts(mon
, buf
);
173 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
177 monitor_vprintf(mon
, fmt
, ap
);
181 void monitor_print_filename(Monitor
*mon
, const char *filename
)
185 for (i
= 0; filename
[i
]; i
++) {
186 switch (filename
[i
]) {
190 monitor_printf(mon
, "\\%c", filename
[i
]);
193 monitor_printf(mon
, "\\t");
196 monitor_printf(mon
, "\\r");
199 monitor_printf(mon
, "\\n");
202 monitor_printf(mon
, "%c", filename
[i
]);
208 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
212 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
217 static int compare_cmd(const char *name
, const char *list
)
219 const char *p
, *pstart
;
227 p
= pstart
+ strlen(pstart
);
228 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
237 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
238 const char *prefix
, const char *name
)
240 const mon_cmd_t
*cmd
;
242 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
243 if (!name
|| !strcmp(name
, cmd
->name
))
244 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
245 cmd
->params
, cmd
->help
);
249 static void help_cmd(Monitor
*mon
, const char *name
)
251 if (name
&& !strcmp(name
, "info")) {
252 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
254 help_cmd_dump(mon
, mon_cmds
, "", name
);
255 if (name
&& !strcmp(name
, "log")) {
256 const CPULogItem
*item
;
257 monitor_printf(mon
, "Log items (comma separated):\n");
258 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
259 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
260 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
266 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
268 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
271 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
275 const char *device
= qdict_get_str(qdict
, "device");
277 all_devices
= !strcmp(device
, "all");
278 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
280 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
282 bdrv_commit(dinfo
->bdrv
);
286 static void do_info(Monitor
*mon
, const QDict
*qdict
)
288 const mon_cmd_t
*cmd
;
289 const char *item
= qdict_get_try_str(qdict
, "item");
293 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
294 if (compare_cmd(item
, cmd
->name
))
298 help_cmd(mon
, "info");
301 cmd
->mhandler
.info(mon
);
304 static void do_info_version(Monitor
*mon
)
306 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
309 static void do_info_name(Monitor
*mon
)
312 monitor_printf(mon
, "%s\n", qemu_name
);
315 #if defined(TARGET_I386)
316 static void do_info_hpet(Monitor
*mon
)
318 monitor_printf(mon
, "HPET is %s by QEMU\n",
319 (no_hpet
) ? "disabled" : "enabled");
323 static void do_info_uuid(Monitor
*mon
)
325 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
326 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
327 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
328 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
329 qemu_uuid
[14], qemu_uuid
[15]);
332 /* get the current CPU defined by the user */
333 static int mon_set_cpu(int cpu_index
)
337 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
338 if (env
->cpu_index
== cpu_index
) {
339 cur_mon
->mon_cpu
= env
;
346 static CPUState
*mon_get_cpu(void)
348 if (!cur_mon
->mon_cpu
) {
351 cpu_synchronize_state(cur_mon
->mon_cpu
);
352 return cur_mon
->mon_cpu
;
355 static void do_info_registers(Monitor
*mon
)
362 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
365 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
370 static void do_info_cpus(Monitor
*mon
)
374 /* just to set the default cpu if not already done */
377 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
378 cpu_synchronize_state(env
);
379 monitor_printf(mon
, "%c CPU #%d:",
380 (env
== mon
->mon_cpu
) ? '*' : ' ',
382 #if defined(TARGET_I386)
383 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
384 env
->eip
+ env
->segs
[R_CS
].base
);
385 #elif defined(TARGET_PPC)
386 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
387 #elif defined(TARGET_SPARC)
388 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
390 #elif defined(TARGET_MIPS)
391 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
394 monitor_printf(mon
, " (halted)");
395 monitor_printf(mon
, "\n");
399 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
401 int index
= qdict_get_int(qdict
, "index");
402 if (mon_set_cpu(index
) < 0)
403 monitor_printf(mon
, "Invalid CPU index\n");
406 static void do_info_jit(Monitor
*mon
)
408 dump_exec_info((FILE *)mon
, monitor_fprintf
);
411 static void do_info_history(Monitor
*mon
)
420 str
= readline_get_history(mon
->rs
, i
);
423 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
428 #if defined(TARGET_PPC)
429 /* XXX: not implemented in other targets */
430 static void do_info_cpu_stats(Monitor
*mon
)
435 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
439 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
444 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
446 if (bdrv_is_inserted(bs
)) {
448 if (!bdrv_is_removable(bs
)) {
449 monitor_printf(mon
, "device is not removable\n");
452 if (bdrv_is_locked(bs
)) {
453 monitor_printf(mon
, "device is locked\n");
462 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
464 BlockDriverState
*bs
;
465 int force
= qdict_get_int(qdict
, "force");
466 const char *filename
= qdict_get_str(qdict
, "filename");
468 bs
= bdrv_find(filename
);
470 monitor_printf(mon
, "device not found\n");
473 eject_device(mon
, bs
, force
);
476 static void do_change_block(Monitor
*mon
, const char *device
,
477 const char *filename
, const char *fmt
)
479 BlockDriverState
*bs
;
480 BlockDriver
*drv
= NULL
;
482 bs
= bdrv_find(device
);
484 monitor_printf(mon
, "device not found\n");
488 drv
= bdrv_find_format(fmt
);
490 monitor_printf(mon
, "invalid format %s\n", fmt
);
494 if (eject_device(mon
, bs
, 0) < 0)
496 bdrv_open2(bs
, filename
, 0, drv
);
497 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
500 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
503 if (vnc_display_password(NULL
, password
) < 0)
504 monitor_printf(mon
, "could not set VNC server password\n");
506 monitor_read_command(mon
, 1);
509 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
511 if (strcmp(target
, "passwd") == 0 ||
512 strcmp(target
, "password") == 0) {
515 strncpy(password
, arg
, sizeof(password
));
516 password
[sizeof(password
) - 1] = '\0';
517 change_vnc_password_cb(mon
, password
, NULL
);
519 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
522 if (vnc_display_open(NULL
, target
) < 0)
523 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
527 static void do_change(Monitor
*mon
, const QDict
*qdict
)
529 const char *device
= qdict_get_str(qdict
, "device");
530 const char *target
= qdict_get_str(qdict
, "target");
531 const char *arg
= qdict_get_try_str(qdict
, "arg");
532 if (strcmp(device
, "vnc") == 0) {
533 do_change_vnc(mon
, target
, arg
);
535 do_change_block(mon
, device
, target
, arg
);
539 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
541 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
544 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
546 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
549 static void do_log(Monitor
*mon
, const QDict
*qdict
)
552 const char *items
= qdict_get_str(qdict
, "items");
554 if (!strcmp(items
, "none")) {
557 mask
= cpu_str_to_log_mask(items
);
559 help_cmd(mon
, "log");
566 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
568 const char *option
= qdict_get_try_str(qdict
, "option");
569 if (!option
|| !strcmp(option
, "on")) {
571 } else if (!strcmp(option
, "off")) {
574 monitor_printf(mon
, "unexpected option %s\n", option
);
578 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
580 vm_stop(EXCP_INTERRUPT
);
583 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
585 struct bdrv_iterate_context
{
590 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
592 struct bdrv_iterate_context context
= { mon
, 0 };
594 bdrv_iterate(encrypted_bdrv_it
, &context
);
595 /* only resume the vm if all keys are set and valid */
600 static void bdrv_key_cb(void *opaque
, int err
)
602 Monitor
*mon
= opaque
;
604 /* another key was set successfully, retry to continue */
609 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
611 struct bdrv_iterate_context
*context
= opaque
;
613 if (!context
->err
&& bdrv_key_required(bs
)) {
614 context
->err
= -EBUSY
;
615 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
620 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
622 const char *device
= qdict_get_try_str(qdict
, "device");
624 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
625 if (gdbserver_start(device
) < 0) {
626 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
628 } else if (strcmp(device
, "none") == 0) {
629 monitor_printf(mon
, "Disabled gdbserver\n");
631 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
636 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
638 const char *action
= qdict_get_str(qdict
, "action");
639 if (select_watchdog_action(action
) == -1) {
640 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
644 static void monitor_printc(Monitor
*mon
, int c
)
646 monitor_printf(mon
, "'");
649 monitor_printf(mon
, "\\'");
652 monitor_printf(mon
, "\\\\");
655 monitor_printf(mon
, "\\n");
658 monitor_printf(mon
, "\\r");
661 if (c
>= 32 && c
<= 126) {
662 monitor_printf(mon
, "%c", c
);
664 monitor_printf(mon
, "\\x%02x", c
);
668 monitor_printf(mon
, "'");
671 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
672 target_phys_addr_t addr
, int is_physical
)
675 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
683 if (!env
&& !is_physical
)
688 } else if (wsize
== 4) {
691 /* as default we use the current CS size */
695 if ((env
->efer
& MSR_EFER_LMA
) &&
696 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
700 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
705 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
714 nb_per_line
= line_size
/ wsize
;
719 max_digits
= (wsize
* 8 + 2) / 3;
723 max_digits
= (wsize
* 8) / 4;
727 max_digits
= (wsize
* 8 * 10 + 32) / 33;
736 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
738 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
743 cpu_physical_memory_rw(addr
, buf
, l
, 0);
748 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
749 monitor_printf(mon
, " Cannot access memory\n");
758 v
= ldub_raw(buf
+ i
);
761 v
= lduw_raw(buf
+ i
);
764 v
= (uint32_t)ldl_raw(buf
+ i
);
767 v
= ldq_raw(buf
+ i
);
770 monitor_printf(mon
, " ");
773 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
776 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
779 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
782 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
785 monitor_printc(mon
, v
);
790 monitor_printf(mon
, "\n");
796 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
798 int count
= qdict_get_int(qdict
, "count");
799 int format
= qdict_get_int(qdict
, "format");
800 int size
= qdict_get_int(qdict
, "size");
801 target_long addr
= qdict_get_int(qdict
, "addr");
803 memory_dump(mon
, count
, format
, size
, addr
, 0);
806 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
808 int count
= qdict_get_int(qdict
, "count");
809 int format
= qdict_get_int(qdict
, "format");
810 int size
= qdict_get_int(qdict
, "size");
811 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
813 memory_dump(mon
, count
, format
, size
, addr
, 1);
816 static void do_print(Monitor
*mon
, const QDict
*qdict
)
818 int format
= qdict_get_int(qdict
, "format");
819 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
821 #if TARGET_PHYS_ADDR_BITS == 32
824 monitor_printf(mon
, "%#o", val
);
827 monitor_printf(mon
, "%#x", val
);
830 monitor_printf(mon
, "%u", val
);
834 monitor_printf(mon
, "%d", val
);
837 monitor_printc(mon
, val
);
843 monitor_printf(mon
, "%#" PRIo64
, val
);
846 monitor_printf(mon
, "%#" PRIx64
, val
);
849 monitor_printf(mon
, "%" PRIu64
, val
);
853 monitor_printf(mon
, "%" PRId64
, val
);
856 monitor_printc(mon
, val
);
860 monitor_printf(mon
, "\n");
863 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
866 uint32_t size
= qdict_get_int(qdict
, "size");
867 const char *filename
= qdict_get_str(qdict
, "filename");
868 target_long addr
= qdict_get_int(qdict
, "val");
877 f
= fopen(filename
, "wb");
879 monitor_printf(mon
, "could not open '%s'\n", filename
);
886 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
887 fwrite(buf
, 1, l
, f
);
894 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
899 uint32_t size
= qdict_get_int(qdict
, "size");
900 const char *filename
= qdict_get_str(qdict
, "filename");
901 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
903 f
= fopen(filename
, "wb");
905 monitor_printf(mon
, "could not open '%s'\n", filename
);
912 cpu_physical_memory_rw(addr
, buf
, l
, 0);
913 fwrite(buf
, 1, l
, f
);
921 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
926 uint32_t start
= qdict_get_int(qdict
, "start");
927 uint32_t size
= qdict_get_int(qdict
, "size");
930 for(addr
= start
; addr
< (start
+ size
); addr
++) {
931 cpu_physical_memory_rw(addr
, buf
, 1, 0);
932 /* BSD sum algorithm ('sum' Unix command) */
933 sum
= (sum
>> 1) | (sum
<< 15);
936 monitor_printf(mon
, "%05d\n", sum
);
944 static const KeyDef key_defs
[] = {
971 { 0x0e, "backspace" },
1008 { 0x37, "asterisk" },
1011 { 0x3a, "caps_lock" },
1022 { 0x45, "num_lock" },
1023 { 0x46, "scroll_lock" },
1025 { 0xb5, "kp_divide" },
1026 { 0x37, "kp_multiply" },
1027 { 0x4a, "kp_subtract" },
1029 { 0x9c, "kp_enter" },
1030 { 0x53, "kp_decimal" },
1063 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1078 { 0xfe, "compose" },
1083 static int get_keycode(const char *key
)
1089 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1090 if (!strcmp(key
, p
->name
))
1093 if (strstart(key
, "0x", NULL
)) {
1094 ret
= strtoul(key
, &endp
, 0);
1095 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1101 #define MAX_KEYCODES 16
1102 static uint8_t keycodes
[MAX_KEYCODES
];
1103 static int nb_pending_keycodes
;
1104 static QEMUTimer
*key_timer
;
1106 static void release_keys(void *opaque
)
1110 while (nb_pending_keycodes
> 0) {
1111 nb_pending_keycodes
--;
1112 keycode
= keycodes
[nb_pending_keycodes
];
1114 kbd_put_keycode(0xe0);
1115 kbd_put_keycode(keycode
| 0x80);
1119 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1121 char keyname_buf
[16];
1123 int keyname_len
, keycode
, i
;
1124 const char *string
= qdict_get_str(qdict
, "string");
1125 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1126 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1128 if (nb_pending_keycodes
> 0) {
1129 qemu_del_timer(key_timer
);
1136 separator
= strchr(string
, '-');
1137 keyname_len
= separator
? separator
- string
: strlen(string
);
1138 if (keyname_len
> 0) {
1139 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1140 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1141 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1144 if (i
== MAX_KEYCODES
) {
1145 monitor_printf(mon
, "too many keys\n");
1148 keyname_buf
[keyname_len
] = 0;
1149 keycode
= get_keycode(keyname_buf
);
1151 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1154 keycodes
[i
++] = keycode
;
1158 string
= separator
+ 1;
1160 nb_pending_keycodes
= i
;
1161 /* key down events */
1162 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1163 keycode
= keycodes
[i
];
1165 kbd_put_keycode(0xe0);
1166 kbd_put_keycode(keycode
& 0x7f);
1168 /* delayed key up events */
1169 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1170 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1173 static int mouse_button_state
;
1175 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1178 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1179 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1180 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1181 dx
= strtol(dx_str
, NULL
, 0);
1182 dy
= strtol(dy_str
, NULL
, 0);
1185 dz
= strtol(dz_str
, NULL
, 0);
1186 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1189 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1191 int button_state
= qdict_get_int(qdict
, "button_state");
1192 mouse_button_state
= button_state
;
1193 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1196 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1198 int size
= qdict_get_int(qdict
, "size");
1199 int addr
= qdict_get_int(qdict
, "addr");
1200 int has_index
= qdict_haskey(qdict
, "index");
1205 int index
= qdict_get_int(qdict
, "index");
1206 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1214 val
= cpu_inb(addr
);
1218 val
= cpu_inw(addr
);
1222 val
= cpu_inl(addr
);
1226 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1227 suffix
, addr
, size
* 2, val
);
1230 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1232 int size
= qdict_get_int(qdict
, "size");
1233 int addr
= qdict_get_int(qdict
, "addr");
1234 int val
= qdict_get_int(qdict
, "val");
1236 addr
&= IOPORTS_MASK
;
1241 cpu_outb(addr
, val
);
1244 cpu_outw(addr
, val
);
1247 cpu_outl(addr
, val
);
1252 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1255 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1257 res
= qemu_boot_set(bootdevice
);
1259 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1260 } else if (res
> 0) {
1261 monitor_printf(mon
, "setting boot device list failed\n");
1263 monitor_printf(mon
, "no function defined to set boot device list for "
1264 "this architecture\n");
1268 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1270 qemu_system_reset_request();
1273 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1275 qemu_system_powerdown_request();
1278 #if defined(TARGET_I386)
1279 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1281 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1284 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1285 pte
& PG_PSE_MASK
? 'P' : '-',
1286 pte
& PG_DIRTY_MASK
? 'D' : '-',
1287 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1288 pte
& PG_PCD_MASK
? 'C' : '-',
1289 pte
& PG_PWT_MASK
? 'T' : '-',
1290 pte
& PG_USER_MASK
? 'U' : '-',
1291 pte
& PG_RW_MASK
? 'W' : '-');
1294 static void tlb_info(Monitor
*mon
)
1298 uint32_t pgd
, pde
, pte
;
1300 env
= mon_get_cpu();
1304 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1305 monitor_printf(mon
, "PG disabled\n");
1308 pgd
= env
->cr
[3] & ~0xfff;
1309 for(l1
= 0; l1
< 1024; l1
++) {
1310 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1311 pde
= le32_to_cpu(pde
);
1312 if (pde
& PG_PRESENT_MASK
) {
1313 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1314 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1316 for(l2
= 0; l2
< 1024; l2
++) {
1317 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1318 (uint8_t *)&pte
, 4);
1319 pte
= le32_to_cpu(pte
);
1320 if (pte
& PG_PRESENT_MASK
) {
1321 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1331 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1332 uint32_t end
, int prot
)
1335 prot1
= *plast_prot
;
1336 if (prot
!= prot1
) {
1337 if (*pstart
!= -1) {
1338 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1339 *pstart
, end
, end
- *pstart
,
1340 prot1
& PG_USER_MASK
? 'u' : '-',
1342 prot1
& PG_RW_MASK
? 'w' : '-');
1352 static void mem_info(Monitor
*mon
)
1355 int l1
, l2
, prot
, last_prot
;
1356 uint32_t pgd
, pde
, pte
, start
, end
;
1358 env
= mon_get_cpu();
1362 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1363 monitor_printf(mon
, "PG disabled\n");
1366 pgd
= env
->cr
[3] & ~0xfff;
1369 for(l1
= 0; l1
< 1024; l1
++) {
1370 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1371 pde
= le32_to_cpu(pde
);
1373 if (pde
& PG_PRESENT_MASK
) {
1374 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1375 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1376 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1378 for(l2
= 0; l2
< 1024; l2
++) {
1379 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1380 (uint8_t *)&pte
, 4);
1381 pte
= le32_to_cpu(pte
);
1382 end
= (l1
<< 22) + (l2
<< 12);
1383 if (pte
& PG_PRESENT_MASK
) {
1384 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1388 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1393 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1399 #if defined(TARGET_SH4)
1401 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1403 monitor_printf(mon
, " tlb%i:\t"
1404 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1405 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1406 "dirty=%hhu writethrough=%hhu\n",
1408 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1409 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1413 static void tlb_info(Monitor
*mon
)
1415 CPUState
*env
= mon_get_cpu();
1418 monitor_printf (mon
, "ITLB:\n");
1419 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1420 print_tlb (mon
, i
, &env
->itlb
[i
]);
1421 monitor_printf (mon
, "UTLB:\n");
1422 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1423 print_tlb (mon
, i
, &env
->utlb
[i
]);
1428 static void do_info_kvm(Monitor
*mon
)
1431 monitor_printf(mon
, "kvm support: ");
1433 monitor_printf(mon
, "enabled\n");
1435 monitor_printf(mon
, "disabled\n");
1437 monitor_printf(mon
, "kvm support: not compiled\n");
1441 static void do_info_numa(Monitor
*mon
)
1446 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1447 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1448 monitor_printf(mon
, "node %d cpus:", i
);
1449 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1450 if (env
->numa_node
== i
) {
1451 monitor_printf(mon
, " %d", env
->cpu_index
);
1454 monitor_printf(mon
, "\n");
1455 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1460 #ifdef CONFIG_PROFILER
1465 static void do_info_profile(Monitor
*mon
)
1471 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1472 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1473 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1474 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1479 static void do_info_profile(Monitor
*mon
)
1481 monitor_printf(mon
, "Internal profiler not compiled\n");
1485 /* Capture support */
1486 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1488 static void do_info_capture(Monitor
*mon
)
1493 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1494 monitor_printf(mon
, "[%d]: ", i
);
1495 s
->ops
.info (s
->opaque
);
1500 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1503 int n
= qdict_get_int(qdict
, "n");
1506 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1508 s
->ops
.destroy (s
->opaque
);
1509 QLIST_REMOVE (s
, entries
);
1516 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1518 const char *path
= qdict_get_str(qdict
, "path");
1519 int has_freq
= qdict_haskey(qdict
, "freq");
1520 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1521 int has_bits
= qdict_haskey(qdict
, "bits");
1522 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1523 int has_channels
= qdict_haskey(qdict
, "nchannels");
1524 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1527 s
= qemu_mallocz (sizeof (*s
));
1529 freq
= has_freq
? freq
: 44100;
1530 bits
= has_bits
? bits
: 16;
1531 nchannels
= has_channels
? nchannels
: 2;
1533 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1534 monitor_printf(mon
, "Faied to add wave capture\n");
1537 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1541 #if defined(TARGET_I386)
1542 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1545 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1547 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1548 if (env
->cpu_index
== cpu_index
) {
1549 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1555 static void do_info_status(Monitor
*mon
)
1559 monitor_printf(mon
, "VM status: running (single step mode)\n");
1561 monitor_printf(mon
, "VM status: running\n");
1564 monitor_printf(mon
, "VM status: paused\n");
1568 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1570 int value
= qdict_get_int(qdict
, "value");
1571 ram_addr_t target
= value
;
1572 qemu_balloon(target
<< 20);
1575 static void do_info_balloon(Monitor
*mon
)
1579 actual
= qemu_balloon_status();
1580 if (kvm_enabled() && !kvm_has_sync_mmu())
1581 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1582 "ballooning disabled\n");
1583 else if (actual
== 0)
1584 monitor_printf(mon
, "Ballooning not activated in VM\n");
1586 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1589 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1591 qemu_acl
*acl
= qemu_acl_find(name
);
1594 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1599 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1601 const char *aclname
= qdict_get_str(qdict
, "aclname");
1602 qemu_acl
*acl
= find_acl(mon
, aclname
);
1603 qemu_acl_entry
*entry
;
1607 monitor_printf(mon
, "policy: %s\n",
1608 acl
->defaultDeny
? "deny" : "allow");
1609 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1611 monitor_printf(mon
, "%d: %s %s\n", i
,
1612 entry
->deny
? "deny" : "allow", entry
->match
);
1617 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1619 const char *aclname
= qdict_get_str(qdict
, "aclname");
1620 qemu_acl
*acl
= find_acl(mon
, aclname
);
1623 qemu_acl_reset(acl
);
1624 monitor_printf(mon
, "acl: removed all rules\n");
1628 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1630 const char *aclname
= qdict_get_str(qdict
, "aclname");
1631 const char *policy
= qdict_get_str(qdict
, "policy");
1632 qemu_acl
*acl
= find_acl(mon
, aclname
);
1635 if (strcmp(policy
, "allow") == 0) {
1636 acl
->defaultDeny
= 0;
1637 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1638 } else if (strcmp(policy
, "deny") == 0) {
1639 acl
->defaultDeny
= 1;
1640 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1642 monitor_printf(mon
, "acl: unknown policy '%s', "
1643 "expected 'deny' or 'allow'\n", policy
);
1648 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1650 const char *aclname
= qdict_get_str(qdict
, "aclname");
1651 const char *match
= qdict_get_str(qdict
, "match");
1652 const char *policy
= qdict_get_str(qdict
, "policy");
1653 int has_index
= qdict_haskey(qdict
, "index");
1654 int index
= qdict_get_try_int(qdict
, "index", -1);
1655 qemu_acl
*acl
= find_acl(mon
, aclname
);
1659 if (strcmp(policy
, "allow") == 0) {
1661 } else if (strcmp(policy
, "deny") == 0) {
1664 monitor_printf(mon
, "acl: unknown policy '%s', "
1665 "expected 'deny' or 'allow'\n", policy
);
1669 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1671 ret
= qemu_acl_append(acl
, deny
, match
);
1673 monitor_printf(mon
, "acl: unable to add acl entry\n");
1675 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1679 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1681 const char *aclname
= qdict_get_str(qdict
, "aclname");
1682 const char *match
= qdict_get_str(qdict
, "match");
1683 qemu_acl
*acl
= find_acl(mon
, aclname
);
1687 ret
= qemu_acl_remove(acl
, match
);
1689 monitor_printf(mon
, "acl: no matching acl entry\n");
1691 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1695 #if defined(TARGET_I386)
1696 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1699 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1700 int bank
= qdict_get_int(qdict
, "bank");
1701 uint64_t status
= qdict_get_int(qdict
, "status");
1702 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1703 uint64_t addr
= qdict_get_int(qdict
, "addr");
1704 uint64_t misc
= qdict_get_int(qdict
, "misc");
1706 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1707 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1708 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1714 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1716 const char *fdname
= qdict_get_str(qdict
, "fdname");
1720 fd
= qemu_chr_get_msgfd(mon
->chr
);
1722 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1726 if (qemu_isdigit(fdname
[0])) {
1727 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1733 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1738 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1739 if (strcmp(monfd
->name
, fdname
) != 0) {
1748 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1749 monfd
->name
= qemu_strdup(fdname
);
1752 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1755 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1757 const char *fdname
= qdict_get_str(qdict
, "fdname");
1760 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1761 if (strcmp(monfd
->name
, fdname
) != 0) {
1765 QLIST_REMOVE(monfd
, next
);
1767 qemu_free(monfd
->name
);
1772 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1776 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1778 int saved_vm_running
= vm_running
;
1779 const char *name
= qdict_get_str(qdict
, "name");
1783 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1787 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1791 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1794 if (strcmp(monfd
->name
, fdname
) != 0) {
1800 /* caller takes ownership of fd */
1801 QLIST_REMOVE(monfd
, next
);
1802 qemu_free(monfd
->name
);
1811 static const mon_cmd_t mon_cmds
[] = {
1812 #include "qemu-monitor.h"
1816 /* Please update qemu-monitor.hx when adding or changing commands */
1817 static const mon_cmd_t info_cmds
[] = {
1822 .help
= "show the version of QEMU",
1823 .mhandler
.info
= do_info_version
,
1829 .help
= "show the network state",
1830 .mhandler
.info
= do_info_network
,
1836 .help
= "show the character devices",
1837 .mhandler
.info
= qemu_chr_info
,
1843 .help
= "show the block devices",
1844 .mhandler
.info
= bdrv_info
,
1847 .name
= "blockstats",
1850 .help
= "show block device statistics",
1851 .mhandler
.info
= bdrv_info_stats
,
1854 .name
= "registers",
1857 .help
= "show the cpu registers",
1858 .mhandler
.info
= do_info_registers
,
1864 .help
= "show infos for each CPU",
1865 .mhandler
.info
= do_info_cpus
,
1871 .help
= "show the command line history",
1872 .mhandler
.info
= do_info_history
,
1878 .help
= "show the interrupts statistics (if available)",
1879 .mhandler
.info
= irq_info
,
1885 .help
= "show i8259 (PIC) state",
1886 .mhandler
.info
= pic_info
,
1892 .help
= "show PCI info",
1893 .mhandler
.info
= pci_info
,
1895 #if defined(TARGET_I386) || defined(TARGET_SH4)
1900 .help
= "show virtual to physical memory mappings",
1901 .mhandler
.info
= tlb_info
,
1904 #if defined(TARGET_I386)
1909 .help
= "show the active virtual memory mappings",
1910 .mhandler
.info
= mem_info
,
1916 .help
= "show state of HPET",
1917 .mhandler
.info
= do_info_hpet
,
1924 .help
= "show dynamic compiler info",
1925 .mhandler
.info
= do_info_jit
,
1931 .help
= "show KVM information",
1932 .mhandler
.info
= do_info_kvm
,
1938 .help
= "show NUMA information",
1939 .mhandler
.info
= do_info_numa
,
1945 .help
= "show guest USB devices",
1946 .mhandler
.info
= usb_info
,
1952 .help
= "show host USB devices",
1953 .mhandler
.info
= usb_host_info
,
1959 .help
= "show profiling information",
1960 .mhandler
.info
= do_info_profile
,
1966 .help
= "show capture information",
1967 .mhandler
.info
= do_info_capture
,
1970 .name
= "snapshots",
1973 .help
= "show the currently saved VM snapshots",
1974 .mhandler
.info
= do_info_snapshots
,
1980 .help
= "show the current VM status (running|paused)",
1981 .mhandler
.info
= do_info_status
,
1987 .help
= "show guest PCMCIA status",
1988 .mhandler
.info
= pcmcia_info
,
1994 .help
= "show which guest mouse is receiving events",
1995 .mhandler
.info
= do_info_mice
,
2001 .help
= "show the vnc server status",
2002 .mhandler
.info
= do_info_vnc
,
2008 .help
= "show the current VM name",
2009 .mhandler
.info
= do_info_name
,
2015 .help
= "show the current VM UUID",
2016 .mhandler
.info
= do_info_uuid
,
2018 #if defined(TARGET_PPC)
2023 .help
= "show CPU statistics",
2024 .mhandler
.info
= do_info_cpu_stats
,
2027 #if defined(CONFIG_SLIRP)
2032 .help
= "show user network stack connection states",
2033 .mhandler
.info
= do_info_usernet
,
2040 .help
= "show migration status",
2041 .mhandler
.info
= do_info_migrate
,
2047 .help
= "show balloon information",
2048 .mhandler
.info
= do_info_balloon
,
2054 .help
= "show device tree",
2055 .mhandler
.info
= do_info_qtree
,
2061 .help
= "show qdev device model list",
2062 .mhandler
.info
= do_info_qdm
,
2068 .help
= "show roms",
2069 .mhandler
.info
= do_info_roms
,
2076 /*******************************************************************/
2078 static const char *pch
;
2079 static jmp_buf expr_env
;
2084 typedef struct MonitorDef
{
2087 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2091 #if defined(TARGET_I386)
2092 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2094 CPUState
*env
= mon_get_cpu();
2097 return env
->eip
+ env
->segs
[R_CS
].base
;
2101 #if defined(TARGET_PPC)
2102 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2104 CPUState
*env
= mon_get_cpu();
2112 for (i
= 0; i
< 8; i
++)
2113 u
|= env
->crf
[i
] << (32 - (4 * i
));
2118 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2120 CPUState
*env
= mon_get_cpu();
2126 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2128 CPUState
*env
= mon_get_cpu();
2134 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2136 CPUState
*env
= mon_get_cpu();
2139 return cpu_ppc_load_decr(env
);
2142 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2144 CPUState
*env
= mon_get_cpu();
2147 return cpu_ppc_load_tbu(env
);
2150 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2152 CPUState
*env
= mon_get_cpu();
2155 return cpu_ppc_load_tbl(env
);
2159 #if defined(TARGET_SPARC)
2160 #ifndef TARGET_SPARC64
2161 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2163 CPUState
*env
= mon_get_cpu();
2166 return GET_PSR(env
);
2170 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2172 CPUState
*env
= mon_get_cpu();
2175 return env
->regwptr
[val
];
2179 static const MonitorDef monitor_defs
[] = {
2182 #define SEG(name, seg) \
2183 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2184 { name ".base", offsetof(CPUState, segs[seg].base) },\
2185 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2187 { "eax", offsetof(CPUState
, regs
[0]) },
2188 { "ecx", offsetof(CPUState
, regs
[1]) },
2189 { "edx", offsetof(CPUState
, regs
[2]) },
2190 { "ebx", offsetof(CPUState
, regs
[3]) },
2191 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2192 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2193 { "esi", offsetof(CPUState
, regs
[6]) },
2194 { "edi", offsetof(CPUState
, regs
[7]) },
2195 #ifdef TARGET_X86_64
2196 { "r8", offsetof(CPUState
, regs
[8]) },
2197 { "r9", offsetof(CPUState
, regs
[9]) },
2198 { "r10", offsetof(CPUState
, regs
[10]) },
2199 { "r11", offsetof(CPUState
, regs
[11]) },
2200 { "r12", offsetof(CPUState
, regs
[12]) },
2201 { "r13", offsetof(CPUState
, regs
[13]) },
2202 { "r14", offsetof(CPUState
, regs
[14]) },
2203 { "r15", offsetof(CPUState
, regs
[15]) },
2205 { "eflags", offsetof(CPUState
, eflags
) },
2206 { "eip", offsetof(CPUState
, eip
) },
2213 { "pc", 0, monitor_get_pc
, },
2214 #elif defined(TARGET_PPC)
2215 /* General purpose registers */
2216 { "r0", offsetof(CPUState
, gpr
[0]) },
2217 { "r1", offsetof(CPUState
, gpr
[1]) },
2218 { "r2", offsetof(CPUState
, gpr
[2]) },
2219 { "r3", offsetof(CPUState
, gpr
[3]) },
2220 { "r4", offsetof(CPUState
, gpr
[4]) },
2221 { "r5", offsetof(CPUState
, gpr
[5]) },
2222 { "r6", offsetof(CPUState
, gpr
[6]) },
2223 { "r7", offsetof(CPUState
, gpr
[7]) },
2224 { "r8", offsetof(CPUState
, gpr
[8]) },
2225 { "r9", offsetof(CPUState
, gpr
[9]) },
2226 { "r10", offsetof(CPUState
, gpr
[10]) },
2227 { "r11", offsetof(CPUState
, gpr
[11]) },
2228 { "r12", offsetof(CPUState
, gpr
[12]) },
2229 { "r13", offsetof(CPUState
, gpr
[13]) },
2230 { "r14", offsetof(CPUState
, gpr
[14]) },
2231 { "r15", offsetof(CPUState
, gpr
[15]) },
2232 { "r16", offsetof(CPUState
, gpr
[16]) },
2233 { "r17", offsetof(CPUState
, gpr
[17]) },
2234 { "r18", offsetof(CPUState
, gpr
[18]) },
2235 { "r19", offsetof(CPUState
, gpr
[19]) },
2236 { "r20", offsetof(CPUState
, gpr
[20]) },
2237 { "r21", offsetof(CPUState
, gpr
[21]) },
2238 { "r22", offsetof(CPUState
, gpr
[22]) },
2239 { "r23", offsetof(CPUState
, gpr
[23]) },
2240 { "r24", offsetof(CPUState
, gpr
[24]) },
2241 { "r25", offsetof(CPUState
, gpr
[25]) },
2242 { "r26", offsetof(CPUState
, gpr
[26]) },
2243 { "r27", offsetof(CPUState
, gpr
[27]) },
2244 { "r28", offsetof(CPUState
, gpr
[28]) },
2245 { "r29", offsetof(CPUState
, gpr
[29]) },
2246 { "r30", offsetof(CPUState
, gpr
[30]) },
2247 { "r31", offsetof(CPUState
, gpr
[31]) },
2248 /* Floating point registers */
2249 { "f0", offsetof(CPUState
, fpr
[0]) },
2250 { "f1", offsetof(CPUState
, fpr
[1]) },
2251 { "f2", offsetof(CPUState
, fpr
[2]) },
2252 { "f3", offsetof(CPUState
, fpr
[3]) },
2253 { "f4", offsetof(CPUState
, fpr
[4]) },
2254 { "f5", offsetof(CPUState
, fpr
[5]) },
2255 { "f6", offsetof(CPUState
, fpr
[6]) },
2256 { "f7", offsetof(CPUState
, fpr
[7]) },
2257 { "f8", offsetof(CPUState
, fpr
[8]) },
2258 { "f9", offsetof(CPUState
, fpr
[9]) },
2259 { "f10", offsetof(CPUState
, fpr
[10]) },
2260 { "f11", offsetof(CPUState
, fpr
[11]) },
2261 { "f12", offsetof(CPUState
, fpr
[12]) },
2262 { "f13", offsetof(CPUState
, fpr
[13]) },
2263 { "f14", offsetof(CPUState
, fpr
[14]) },
2264 { "f15", offsetof(CPUState
, fpr
[15]) },
2265 { "f16", offsetof(CPUState
, fpr
[16]) },
2266 { "f17", offsetof(CPUState
, fpr
[17]) },
2267 { "f18", offsetof(CPUState
, fpr
[18]) },
2268 { "f19", offsetof(CPUState
, fpr
[19]) },
2269 { "f20", offsetof(CPUState
, fpr
[20]) },
2270 { "f21", offsetof(CPUState
, fpr
[21]) },
2271 { "f22", offsetof(CPUState
, fpr
[22]) },
2272 { "f23", offsetof(CPUState
, fpr
[23]) },
2273 { "f24", offsetof(CPUState
, fpr
[24]) },
2274 { "f25", offsetof(CPUState
, fpr
[25]) },
2275 { "f26", offsetof(CPUState
, fpr
[26]) },
2276 { "f27", offsetof(CPUState
, fpr
[27]) },
2277 { "f28", offsetof(CPUState
, fpr
[28]) },
2278 { "f29", offsetof(CPUState
, fpr
[29]) },
2279 { "f30", offsetof(CPUState
, fpr
[30]) },
2280 { "f31", offsetof(CPUState
, fpr
[31]) },
2281 { "fpscr", offsetof(CPUState
, fpscr
) },
2282 /* Next instruction pointer */
2283 { "nip|pc", offsetof(CPUState
, nip
) },
2284 { "lr", offsetof(CPUState
, lr
) },
2285 { "ctr", offsetof(CPUState
, ctr
) },
2286 { "decr", 0, &monitor_get_decr
, },
2287 { "ccr", 0, &monitor_get_ccr
, },
2288 /* Machine state register */
2289 { "msr", 0, &monitor_get_msr
, },
2290 { "xer", 0, &monitor_get_xer
, },
2291 { "tbu", 0, &monitor_get_tbu
, },
2292 { "tbl", 0, &monitor_get_tbl
, },
2293 #if defined(TARGET_PPC64)
2294 /* Address space register */
2295 { "asr", offsetof(CPUState
, asr
) },
2297 /* Segment registers */
2298 { "sdr1", offsetof(CPUState
, sdr1
) },
2299 { "sr0", offsetof(CPUState
, sr
[0]) },
2300 { "sr1", offsetof(CPUState
, sr
[1]) },
2301 { "sr2", offsetof(CPUState
, sr
[2]) },
2302 { "sr3", offsetof(CPUState
, sr
[3]) },
2303 { "sr4", offsetof(CPUState
, sr
[4]) },
2304 { "sr5", offsetof(CPUState
, sr
[5]) },
2305 { "sr6", offsetof(CPUState
, sr
[6]) },
2306 { "sr7", offsetof(CPUState
, sr
[7]) },
2307 { "sr8", offsetof(CPUState
, sr
[8]) },
2308 { "sr9", offsetof(CPUState
, sr
[9]) },
2309 { "sr10", offsetof(CPUState
, sr
[10]) },
2310 { "sr11", offsetof(CPUState
, sr
[11]) },
2311 { "sr12", offsetof(CPUState
, sr
[12]) },
2312 { "sr13", offsetof(CPUState
, sr
[13]) },
2313 { "sr14", offsetof(CPUState
, sr
[14]) },
2314 { "sr15", offsetof(CPUState
, sr
[15]) },
2315 /* Too lazy to put BATs and SPRs ... */
2316 #elif defined(TARGET_SPARC)
2317 { "g0", offsetof(CPUState
, gregs
[0]) },
2318 { "g1", offsetof(CPUState
, gregs
[1]) },
2319 { "g2", offsetof(CPUState
, gregs
[2]) },
2320 { "g3", offsetof(CPUState
, gregs
[3]) },
2321 { "g4", offsetof(CPUState
, gregs
[4]) },
2322 { "g5", offsetof(CPUState
, gregs
[5]) },
2323 { "g6", offsetof(CPUState
, gregs
[6]) },
2324 { "g7", offsetof(CPUState
, gregs
[7]) },
2325 { "o0", 0, monitor_get_reg
},
2326 { "o1", 1, monitor_get_reg
},
2327 { "o2", 2, monitor_get_reg
},
2328 { "o3", 3, monitor_get_reg
},
2329 { "o4", 4, monitor_get_reg
},
2330 { "o5", 5, monitor_get_reg
},
2331 { "o6", 6, monitor_get_reg
},
2332 { "o7", 7, monitor_get_reg
},
2333 { "l0", 8, monitor_get_reg
},
2334 { "l1", 9, monitor_get_reg
},
2335 { "l2", 10, monitor_get_reg
},
2336 { "l3", 11, monitor_get_reg
},
2337 { "l4", 12, monitor_get_reg
},
2338 { "l5", 13, monitor_get_reg
},
2339 { "l6", 14, monitor_get_reg
},
2340 { "l7", 15, monitor_get_reg
},
2341 { "i0", 16, monitor_get_reg
},
2342 { "i1", 17, monitor_get_reg
},
2343 { "i2", 18, monitor_get_reg
},
2344 { "i3", 19, monitor_get_reg
},
2345 { "i4", 20, monitor_get_reg
},
2346 { "i5", 21, monitor_get_reg
},
2347 { "i6", 22, monitor_get_reg
},
2348 { "i7", 23, monitor_get_reg
},
2349 { "pc", offsetof(CPUState
, pc
) },
2350 { "npc", offsetof(CPUState
, npc
) },
2351 { "y", offsetof(CPUState
, y
) },
2352 #ifndef TARGET_SPARC64
2353 { "psr", 0, &monitor_get_psr
, },
2354 { "wim", offsetof(CPUState
, wim
) },
2356 { "tbr", offsetof(CPUState
, tbr
) },
2357 { "fsr", offsetof(CPUState
, fsr
) },
2358 { "f0", offsetof(CPUState
, fpr
[0]) },
2359 { "f1", offsetof(CPUState
, fpr
[1]) },
2360 { "f2", offsetof(CPUState
, fpr
[2]) },
2361 { "f3", offsetof(CPUState
, fpr
[3]) },
2362 { "f4", offsetof(CPUState
, fpr
[4]) },
2363 { "f5", offsetof(CPUState
, fpr
[5]) },
2364 { "f6", offsetof(CPUState
, fpr
[6]) },
2365 { "f7", offsetof(CPUState
, fpr
[7]) },
2366 { "f8", offsetof(CPUState
, fpr
[8]) },
2367 { "f9", offsetof(CPUState
, fpr
[9]) },
2368 { "f10", offsetof(CPUState
, fpr
[10]) },
2369 { "f11", offsetof(CPUState
, fpr
[11]) },
2370 { "f12", offsetof(CPUState
, fpr
[12]) },
2371 { "f13", offsetof(CPUState
, fpr
[13]) },
2372 { "f14", offsetof(CPUState
, fpr
[14]) },
2373 { "f15", offsetof(CPUState
, fpr
[15]) },
2374 { "f16", offsetof(CPUState
, fpr
[16]) },
2375 { "f17", offsetof(CPUState
, fpr
[17]) },
2376 { "f18", offsetof(CPUState
, fpr
[18]) },
2377 { "f19", offsetof(CPUState
, fpr
[19]) },
2378 { "f20", offsetof(CPUState
, fpr
[20]) },
2379 { "f21", offsetof(CPUState
, fpr
[21]) },
2380 { "f22", offsetof(CPUState
, fpr
[22]) },
2381 { "f23", offsetof(CPUState
, fpr
[23]) },
2382 { "f24", offsetof(CPUState
, fpr
[24]) },
2383 { "f25", offsetof(CPUState
, fpr
[25]) },
2384 { "f26", offsetof(CPUState
, fpr
[26]) },
2385 { "f27", offsetof(CPUState
, fpr
[27]) },
2386 { "f28", offsetof(CPUState
, fpr
[28]) },
2387 { "f29", offsetof(CPUState
, fpr
[29]) },
2388 { "f30", offsetof(CPUState
, fpr
[30]) },
2389 { "f31", offsetof(CPUState
, fpr
[31]) },
2390 #ifdef TARGET_SPARC64
2391 { "f32", offsetof(CPUState
, fpr
[32]) },
2392 { "f34", offsetof(CPUState
, fpr
[34]) },
2393 { "f36", offsetof(CPUState
, fpr
[36]) },
2394 { "f38", offsetof(CPUState
, fpr
[38]) },
2395 { "f40", offsetof(CPUState
, fpr
[40]) },
2396 { "f42", offsetof(CPUState
, fpr
[42]) },
2397 { "f44", offsetof(CPUState
, fpr
[44]) },
2398 { "f46", offsetof(CPUState
, fpr
[46]) },
2399 { "f48", offsetof(CPUState
, fpr
[48]) },
2400 { "f50", offsetof(CPUState
, fpr
[50]) },
2401 { "f52", offsetof(CPUState
, fpr
[52]) },
2402 { "f54", offsetof(CPUState
, fpr
[54]) },
2403 { "f56", offsetof(CPUState
, fpr
[56]) },
2404 { "f58", offsetof(CPUState
, fpr
[58]) },
2405 { "f60", offsetof(CPUState
, fpr
[60]) },
2406 { "f62", offsetof(CPUState
, fpr
[62]) },
2407 { "asi", offsetof(CPUState
, asi
) },
2408 { "pstate", offsetof(CPUState
, pstate
) },
2409 { "cansave", offsetof(CPUState
, cansave
) },
2410 { "canrestore", offsetof(CPUState
, canrestore
) },
2411 { "otherwin", offsetof(CPUState
, otherwin
) },
2412 { "wstate", offsetof(CPUState
, wstate
) },
2413 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2414 { "fprs", offsetof(CPUState
, fprs
) },
2420 static void expr_error(Monitor
*mon
, const char *msg
)
2422 monitor_printf(mon
, "%s\n", msg
);
2423 longjmp(expr_env
, 1);
2426 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2427 static int get_monitor_def(target_long
*pval
, const char *name
)
2429 const MonitorDef
*md
;
2432 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2433 if (compare_cmd(name
, md
->name
)) {
2434 if (md
->get_value
) {
2435 *pval
= md
->get_value(md
, md
->offset
);
2437 CPUState
*env
= mon_get_cpu();
2440 ptr
= (uint8_t *)env
+ md
->offset
;
2443 *pval
= *(int32_t *)ptr
;
2446 *pval
= *(target_long
*)ptr
;
2459 static void next(void)
2463 while (qemu_isspace(*pch
))
2468 static int64_t expr_sum(Monitor
*mon
);
2470 static int64_t expr_unary(Monitor
*mon
)
2479 n
= expr_unary(mon
);
2483 n
= -expr_unary(mon
);
2487 n
= ~expr_unary(mon
);
2493 expr_error(mon
, "')' expected");
2500 expr_error(mon
, "character constant expected");
2504 expr_error(mon
, "missing terminating \' character");
2514 while ((*pch
>= 'a' && *pch
<= 'z') ||
2515 (*pch
>= 'A' && *pch
<= 'Z') ||
2516 (*pch
>= '0' && *pch
<= '9') ||
2517 *pch
== '_' || *pch
== '.') {
2518 if ((q
- buf
) < sizeof(buf
) - 1)
2522 while (qemu_isspace(*pch
))
2525 ret
= get_monitor_def(®
, buf
);
2527 expr_error(mon
, "unknown register");
2529 expr_error(mon
, "no cpu defined");
2534 expr_error(mon
, "unexpected end of expression");
2538 #if TARGET_PHYS_ADDR_BITS > 32
2539 n
= strtoull(pch
, &p
, 0);
2541 n
= strtoul(pch
, &p
, 0);
2544 expr_error(mon
, "invalid char in expression");
2547 while (qemu_isspace(*pch
))
2555 static int64_t expr_prod(Monitor
*mon
)
2560 val
= expr_unary(mon
);
2563 if (op
!= '*' && op
!= '/' && op
!= '%')
2566 val2
= expr_unary(mon
);
2575 expr_error(mon
, "division by zero");
2586 static int64_t expr_logic(Monitor
*mon
)
2591 val
= expr_prod(mon
);
2594 if (op
!= '&' && op
!= '|' && op
!= '^')
2597 val2
= expr_prod(mon
);
2614 static int64_t expr_sum(Monitor
*mon
)
2619 val
= expr_logic(mon
);
2622 if (op
!= '+' && op
!= '-')
2625 val2
= expr_logic(mon
);
2634 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2637 if (setjmp(expr_env
)) {
2641 while (qemu_isspace(*pch
))
2643 *pval
= expr_sum(mon
);
2648 static int get_str(char *buf
, int buf_size
, const char **pp
)
2656 while (qemu_isspace(*p
))
2666 while (*p
!= '\0' && *p
!= '\"') {
2682 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2685 if ((q
- buf
) < buf_size
- 1) {
2689 if ((q
- buf
) < buf_size
- 1) {
2696 qemu_printf("unterminated string\n");
2701 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2702 if ((q
- buf
) < buf_size
- 1) {
2714 * Store the command-name in cmdname, and return a pointer to
2715 * the remaining of the command string.
2717 static const char *get_command_name(const char *cmdline
,
2718 char *cmdname
, size_t nlen
)
2721 const char *p
, *pstart
;
2724 while (qemu_isspace(*p
))
2729 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2734 memcpy(cmdname
, pstart
, len
);
2735 cmdname
[len
] = '\0';
2740 * Read key of 'type' into 'key' and return the current
2743 static char *key_get_info(const char *type
, char **key
)
2751 p
= strchr(type
, ':');
2758 str
= qemu_malloc(len
+ 1);
2759 memcpy(str
, type
, len
);
2766 static int default_fmt_format
= 'x';
2767 static int default_fmt_size
= 4;
2771 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2772 const char *cmdline
,
2775 const char *p
, *typestr
;
2777 const mon_cmd_t
*cmd
;
2783 monitor_printf(mon
, "command='%s'\n", cmdline
);
2786 /* extract the command name */
2787 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2791 /* find the command */
2792 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2793 if (compare_cmd(cmdname
, cmd
->name
))
2797 if (cmd
->name
== NULL
) {
2798 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2802 /* parse the parameters */
2803 typestr
= cmd
->args_type
;
2805 typestr
= key_get_info(typestr
, &key
);
2817 while (qemu_isspace(*p
))
2819 if (*typestr
== '?') {
2822 /* no optional string: NULL argument */
2826 ret
= get_str(buf
, sizeof(buf
), &p
);
2830 monitor_printf(mon
, "%s: filename expected\n",
2834 monitor_printf(mon
, "%s: block device name expected\n",
2838 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2843 qdict_put(qdict
, key
, qstring_from_str(buf
));
2848 int count
, format
, size
;
2850 while (qemu_isspace(*p
))
2856 if (qemu_isdigit(*p
)) {
2858 while (qemu_isdigit(*p
)) {
2859 count
= count
* 10 + (*p
- '0');
2897 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2898 monitor_printf(mon
, "invalid char in format: '%c'\n",
2903 format
= default_fmt_format
;
2904 if (format
!= 'i') {
2905 /* for 'i', not specifying a size gives -1 as size */
2907 size
= default_fmt_size
;
2908 default_fmt_size
= size
;
2910 default_fmt_format
= format
;
2913 format
= default_fmt_format
;
2914 if (format
!= 'i') {
2915 size
= default_fmt_size
;
2920 qdict_put(qdict
, "count", qint_from_int(count
));
2921 qdict_put(qdict
, "format", qint_from_int(format
));
2922 qdict_put(qdict
, "size", qint_from_int(size
));
2930 while (qemu_isspace(*p
))
2932 if (*typestr
== '?' || *typestr
== '.') {
2933 if (*typestr
== '?') {
2941 while (qemu_isspace(*p
))
2950 if (get_expr(mon
, &val
, &p
))
2952 /* Check if 'i' is greater than 32-bit */
2953 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2954 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2955 monitor_printf(mon
, "integer is for 32-bit values\n");
2958 qdict_put(qdict
, key
, qint_from_int(val
));
2969 while (qemu_isspace(*p
))
2975 monitor_printf(mon
, "%s: unsupported option -%c\n",
2982 qdict_put(qdict
, key
, qint_from_int(has_option
));
2987 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2993 /* check that all arguments were parsed */
2994 while (qemu_isspace(*p
))
2997 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3009 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3012 const mon_cmd_t
*cmd
;
3014 qdict
= qdict_new();
3016 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3018 qemu_errors_to_mon(mon
);
3019 cmd
->mhandler
.cmd(mon
, qdict
);
3020 qemu_errors_to_previous();
3026 static void cmd_completion(const char *name
, const char *list
)
3028 const char *p
, *pstart
;
3037 p
= pstart
+ strlen(pstart
);
3039 if (len
> sizeof(cmd
) - 2)
3040 len
= sizeof(cmd
) - 2;
3041 memcpy(cmd
, pstart
, len
);
3043 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3044 readline_add_completion(cur_mon
->rs
, cmd
);
3052 static void file_completion(const char *input
)
3057 char file
[1024], file_prefix
[1024];
3061 p
= strrchr(input
, '/');
3064 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3065 pstrcpy(path
, sizeof(path
), ".");
3067 input_path_len
= p
- input
+ 1;
3068 memcpy(path
, input
, input_path_len
);
3069 if (input_path_len
> sizeof(path
) - 1)
3070 input_path_len
= sizeof(path
) - 1;
3071 path
[input_path_len
] = '\0';
3072 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3074 #ifdef DEBUG_COMPLETION
3075 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3076 input
, path
, file_prefix
);
3078 ffs
= opendir(path
);
3086 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3087 memcpy(file
, input
, input_path_len
);
3088 if (input_path_len
< sizeof(file
))
3089 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3091 /* stat the file to find out if it's a directory.
3092 * In that case add a slash to speed up typing long paths
3095 if(S_ISDIR(sb
.st_mode
))
3096 pstrcat(file
, sizeof(file
), "/");
3097 readline_add_completion(cur_mon
->rs
, file
);
3103 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3105 const char *name
= bdrv_get_device_name(bs
);
3106 const char *input
= opaque
;
3108 if (input
[0] == '\0' ||
3109 !strncmp(name
, (char *)input
, strlen(input
))) {
3110 readline_add_completion(cur_mon
->rs
, name
);
3114 /* NOTE: this parser is an approximate form of the real command parser */
3115 static void parse_cmdline(const char *cmdline
,
3116 int *pnb_args
, char **args
)
3125 while (qemu_isspace(*p
))
3129 if (nb_args
>= MAX_ARGS
)
3131 ret
= get_str(buf
, sizeof(buf
), &p
);
3132 args
[nb_args
] = qemu_strdup(buf
);
3137 *pnb_args
= nb_args
;
3140 static const char *next_arg_type(const char *typestr
)
3142 const char *p
= strchr(typestr
, ':');
3143 return (p
!= NULL
? ++p
: typestr
);
3146 static void monitor_find_completion(const char *cmdline
)
3148 const char *cmdname
;
3149 char *args
[MAX_ARGS
];
3150 int nb_args
, i
, len
;
3151 const char *ptype
, *str
;
3152 const mon_cmd_t
*cmd
;
3155 parse_cmdline(cmdline
, &nb_args
, args
);
3156 #ifdef DEBUG_COMPLETION
3157 for(i
= 0; i
< nb_args
; i
++) {
3158 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3162 /* if the line ends with a space, it means we want to complete the
3164 len
= strlen(cmdline
);
3165 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3166 if (nb_args
>= MAX_ARGS
)
3168 args
[nb_args
++] = qemu_strdup("");
3171 /* command completion */
3176 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3177 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3178 cmd_completion(cmdname
, cmd
->name
);
3181 /* find the command */
3182 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3183 if (compare_cmd(args
[0], cmd
->name
))
3188 ptype
= next_arg_type(cmd
->args_type
);
3189 for(i
= 0; i
< nb_args
- 2; i
++) {
3190 if (*ptype
!= '\0') {
3191 ptype
= next_arg_type(ptype
);
3192 while (*ptype
== '?')
3193 ptype
= next_arg_type(ptype
);
3196 str
= args
[nb_args
- 1];
3197 if (*ptype
== '-' && ptype
[1] != '\0') {
3202 /* file completion */
3203 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3204 file_completion(str
);
3207 /* block device name completion */
3208 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3209 bdrv_iterate(block_completion_it
, (void *)str
);
3212 /* XXX: more generic ? */
3213 if (!strcmp(cmd
->name
, "info")) {
3214 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3215 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3216 cmd_completion(str
, cmd
->name
);
3218 } else if (!strcmp(cmd
->name
, "sendkey")) {
3219 char *sep
= strrchr(str
, '-');
3222 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3223 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3224 cmd_completion(str
, key
->name
);
3226 } else if (!strcmp(cmd
->name
, "help|?")) {
3227 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3228 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3229 cmd_completion(str
, cmd
->name
);
3237 for(i
= 0; i
< nb_args
; i
++)
3241 static int monitor_can_read(void *opaque
)
3243 Monitor
*mon
= opaque
;
3245 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3248 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3250 Monitor
*old_mon
= cur_mon
;
3256 for (i
= 0; i
< size
; i
++)
3257 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3259 if (size
== 0 || buf
[size
- 1] != 0)
3260 monitor_printf(cur_mon
, "corrupted command\n");
3262 monitor_handle_command(cur_mon
, (char *)buf
);
3268 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3270 monitor_suspend(mon
);
3271 monitor_handle_command(mon
, cmdline
);
3272 monitor_resume(mon
);
3275 int monitor_suspend(Monitor
*mon
)
3283 void monitor_resume(Monitor
*mon
)
3287 if (--mon
->suspend_cnt
== 0)
3288 readline_show_prompt(mon
->rs
);
3291 static void monitor_event(void *opaque
, int event
)
3293 Monitor
*mon
= opaque
;
3296 case CHR_EVENT_MUX_IN
:
3298 if (mon
->reset_seen
) {
3299 readline_restart(mon
->rs
);
3300 monitor_resume(mon
);
3303 mon
->suspend_cnt
= 0;
3307 case CHR_EVENT_MUX_OUT
:
3308 if (mon
->reset_seen
) {
3309 if (mon
->suspend_cnt
== 0) {
3310 monitor_printf(mon
, "\n");
3313 monitor_suspend(mon
);
3320 case CHR_EVENT_RESET
:
3321 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3322 "information\n", QEMU_VERSION
);
3323 if (!mon
->mux_out
) {
3324 readline_show_prompt(mon
->rs
);
3326 mon
->reset_seen
= 1;
3340 void monitor_init(CharDriverState
*chr
, int flags
)
3342 static int is_first_init
= 1;
3345 if (is_first_init
) {
3346 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3350 mon
= qemu_mallocz(sizeof(*mon
));
3354 if (flags
& MONITOR_USE_READLINE
) {
3355 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3356 monitor_read_command(mon
, 0);
3359 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3362 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3363 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3367 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3369 BlockDriverState
*bs
= opaque
;
3372 if (bdrv_set_key(bs
, password
) != 0) {
3373 monitor_printf(mon
, "invalid password\n");
3376 if (mon
->password_completion_cb
)
3377 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3379 monitor_read_command(mon
, 1);
3382 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3383 BlockDriverCompletionFunc
*completion_cb
,
3388 if (!bdrv_key_required(bs
)) {
3390 completion_cb(opaque
, 0);
3394 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3395 bdrv_get_encrypted_filename(bs
));
3397 mon
->password_completion_cb
= completion_cb
;
3398 mon
->password_opaque
= opaque
;
3400 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3402 if (err
&& completion_cb
)
3403 completion_cb(opaque
, err
);
3406 typedef struct QemuErrorSink QemuErrorSink
;
3407 struct QemuErrorSink
{
3416 QemuErrorSink
*previous
;
3419 static QemuErrorSink
*qemu_error_sink
;
3421 void qemu_errors_to_file(FILE *fp
)
3423 QemuErrorSink
*sink
;
3425 sink
= qemu_mallocz(sizeof(*sink
));
3426 sink
->dest
= ERR_SINK_FILE
;
3428 sink
->previous
= qemu_error_sink
;
3429 qemu_error_sink
= sink
;
3432 void qemu_errors_to_mon(Monitor
*mon
)
3434 QemuErrorSink
*sink
;
3436 sink
= qemu_mallocz(sizeof(*sink
));
3437 sink
->dest
= ERR_SINK_MONITOR
;
3439 sink
->previous
= qemu_error_sink
;
3440 qemu_error_sink
= sink
;
3443 void qemu_errors_to_previous(void)
3445 QemuErrorSink
*sink
;
3447 assert(qemu_error_sink
!= NULL
);
3448 sink
= qemu_error_sink
;
3449 qemu_error_sink
= sink
->previous
;
3453 void qemu_error(const char *fmt
, ...)
3457 assert(qemu_error_sink
!= NULL
);
3458 switch (qemu_error_sink
->dest
) {
3460 va_start(args
, fmt
);
3461 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3464 case ERR_SINK_MONITOR
:
3465 va_start(args
, fmt
);
3466 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);