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
);
80 void (*cmd_new
)(Monitor
*mon
, const QDict
*params
, QObject
**ret_data
);
84 /* file descriptors passed via SCM_RIGHTS */
85 typedef struct mon_fd_t mon_fd_t
;
89 QLIST_ENTRY(mon_fd_t
) next
;
102 BlockDriverCompletionFunc
*password_completion_cb
;
103 void *password_opaque
;
104 QLIST_HEAD(,mon_fd_t
) fds
;
105 QLIST_ENTRY(Monitor
) entry
;
108 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
110 static const mon_cmd_t mon_cmds
[];
111 static const mon_cmd_t info_cmds
[];
113 Monitor
*cur_mon
= NULL
;
115 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
118 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
120 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
122 readline_show_prompt(mon
->rs
);
125 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
129 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
130 /* prompt is printed on return from the command handler */
133 monitor_printf(mon
, "terminal does not support password prompting\n");
138 void monitor_flush(Monitor
*mon
)
140 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
141 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
142 mon
->outbuf_index
= 0;
146 /* flush at every end of line or if the buffer is full */
147 static void monitor_puts(Monitor
*mon
, const char *str
)
159 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
160 mon
->outbuf
[mon
->outbuf_index
++] = c
;
161 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
167 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
170 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
171 monitor_puts(mon
, buf
);
174 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
178 monitor_vprintf(mon
, fmt
, ap
);
182 void monitor_print_filename(Monitor
*mon
, const char *filename
)
186 for (i
= 0; filename
[i
]; i
++) {
187 switch (filename
[i
]) {
191 monitor_printf(mon
, "\\%c", filename
[i
]);
194 monitor_printf(mon
, "\\t");
197 monitor_printf(mon
, "\\r");
200 monitor_printf(mon
, "\\n");
203 monitor_printf(mon
, "%c", filename
[i
]);
209 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
213 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
218 static inline int monitor_handler_ported(const mon_cmd_t
*cmd
)
220 return cmd
->user_print
!= NULL
;
223 static int compare_cmd(const char *name
, const char *list
)
225 const char *p
, *pstart
;
233 p
= pstart
+ strlen(pstart
);
234 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
243 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
244 const char *prefix
, const char *name
)
246 const mon_cmd_t
*cmd
;
248 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
249 if (!name
|| !strcmp(name
, cmd
->name
))
250 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
251 cmd
->params
, cmd
->help
);
255 static void help_cmd(Monitor
*mon
, const char *name
)
257 if (name
&& !strcmp(name
, "info")) {
258 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
260 help_cmd_dump(mon
, mon_cmds
, "", name
);
261 if (name
&& !strcmp(name
, "log")) {
262 const CPULogItem
*item
;
263 monitor_printf(mon
, "Log items (comma separated):\n");
264 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
265 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
266 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
272 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
274 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
277 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
281 const char *device
= qdict_get_str(qdict
, "device");
283 all_devices
= !strcmp(device
, "all");
284 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
286 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
288 bdrv_commit(dinfo
->bdrv
);
292 static void do_info(Monitor
*mon
, const QDict
*qdict
)
294 const mon_cmd_t
*cmd
;
295 const char *item
= qdict_get_try_str(qdict
, "item");
299 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
300 if (compare_cmd(item
, cmd
->name
))
304 help_cmd(mon
, "info");
307 cmd
->mhandler
.info(mon
);
310 static void do_info_version(Monitor
*mon
)
312 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
315 static void do_info_name(Monitor
*mon
)
318 monitor_printf(mon
, "%s\n", qemu_name
);
321 #if defined(TARGET_I386)
322 static void do_info_hpet(Monitor
*mon
)
324 monitor_printf(mon
, "HPET is %s by QEMU\n",
325 (no_hpet
) ? "disabled" : "enabled");
329 static void do_info_uuid(Monitor
*mon
)
331 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
332 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
333 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
334 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
335 qemu_uuid
[14], qemu_uuid
[15]);
338 /* get the current CPU defined by the user */
339 static int mon_set_cpu(int cpu_index
)
343 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
344 if (env
->cpu_index
== cpu_index
) {
345 cur_mon
->mon_cpu
= env
;
352 static CPUState
*mon_get_cpu(void)
354 if (!cur_mon
->mon_cpu
) {
357 cpu_synchronize_state(cur_mon
->mon_cpu
);
358 return cur_mon
->mon_cpu
;
361 static void do_info_registers(Monitor
*mon
)
368 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
371 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
376 static void do_info_cpus(Monitor
*mon
)
380 /* just to set the default cpu if not already done */
383 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
384 cpu_synchronize_state(env
);
385 monitor_printf(mon
, "%c CPU #%d:",
386 (env
== mon
->mon_cpu
) ? '*' : ' ',
388 #if defined(TARGET_I386)
389 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
390 env
->eip
+ env
->segs
[R_CS
].base
);
391 #elif defined(TARGET_PPC)
392 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
393 #elif defined(TARGET_SPARC)
394 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
396 #elif defined(TARGET_MIPS)
397 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
400 monitor_printf(mon
, " (halted)");
401 monitor_printf(mon
, "\n");
405 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
407 int index
= qdict_get_int(qdict
, "index");
408 if (mon_set_cpu(index
) < 0)
409 monitor_printf(mon
, "Invalid CPU index\n");
412 static void do_info_jit(Monitor
*mon
)
414 dump_exec_info((FILE *)mon
, monitor_fprintf
);
417 static void do_info_history(Monitor
*mon
)
426 str
= readline_get_history(mon
->rs
, i
);
429 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
434 #if defined(TARGET_PPC)
435 /* XXX: not implemented in other targets */
436 static void do_info_cpu_stats(Monitor
*mon
)
441 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
445 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
450 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
452 if (bdrv_is_inserted(bs
)) {
454 if (!bdrv_is_removable(bs
)) {
455 monitor_printf(mon
, "device is not removable\n");
458 if (bdrv_is_locked(bs
)) {
459 monitor_printf(mon
, "device is locked\n");
468 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
470 BlockDriverState
*bs
;
471 int force
= qdict_get_int(qdict
, "force");
472 const char *filename
= qdict_get_str(qdict
, "filename");
474 bs
= bdrv_find(filename
);
476 monitor_printf(mon
, "device not found\n");
479 eject_device(mon
, bs
, force
);
482 static void do_change_block(Monitor
*mon
, const char *device
,
483 const char *filename
, const char *fmt
)
485 BlockDriverState
*bs
;
486 BlockDriver
*drv
= NULL
;
488 bs
= bdrv_find(device
);
490 monitor_printf(mon
, "device not found\n");
494 drv
= bdrv_find_format(fmt
);
496 monitor_printf(mon
, "invalid format %s\n", fmt
);
500 if (eject_device(mon
, bs
, 0) < 0)
502 bdrv_open2(bs
, filename
, 0, drv
);
503 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
506 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
509 if (vnc_display_password(NULL
, password
) < 0)
510 monitor_printf(mon
, "could not set VNC server password\n");
512 monitor_read_command(mon
, 1);
515 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
517 if (strcmp(target
, "passwd") == 0 ||
518 strcmp(target
, "password") == 0) {
521 strncpy(password
, arg
, sizeof(password
));
522 password
[sizeof(password
) - 1] = '\0';
523 change_vnc_password_cb(mon
, password
, NULL
);
525 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
528 if (vnc_display_open(NULL
, target
) < 0)
529 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
533 static void do_change(Monitor
*mon
, const QDict
*qdict
)
535 const char *device
= qdict_get_str(qdict
, "device");
536 const char *target
= qdict_get_str(qdict
, "target");
537 const char *arg
= qdict_get_try_str(qdict
, "arg");
538 if (strcmp(device
, "vnc") == 0) {
539 do_change_vnc(mon
, target
, arg
);
541 do_change_block(mon
, device
, target
, arg
);
545 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
547 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
550 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
552 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
555 static void do_log(Monitor
*mon
, const QDict
*qdict
)
558 const char *items
= qdict_get_str(qdict
, "items");
560 if (!strcmp(items
, "none")) {
563 mask
= cpu_str_to_log_mask(items
);
565 help_cmd(mon
, "log");
572 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
574 const char *option
= qdict_get_try_str(qdict
, "option");
575 if (!option
|| !strcmp(option
, "on")) {
577 } else if (!strcmp(option
, "off")) {
580 monitor_printf(mon
, "unexpected option %s\n", option
);
584 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
586 vm_stop(EXCP_INTERRUPT
);
589 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
591 struct bdrv_iterate_context
{
596 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
598 struct bdrv_iterate_context context
= { mon
, 0 };
600 bdrv_iterate(encrypted_bdrv_it
, &context
);
601 /* only resume the vm if all keys are set and valid */
606 static void bdrv_key_cb(void *opaque
, int err
)
608 Monitor
*mon
= opaque
;
610 /* another key was set successfully, retry to continue */
615 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
617 struct bdrv_iterate_context
*context
= opaque
;
619 if (!context
->err
&& bdrv_key_required(bs
)) {
620 context
->err
= -EBUSY
;
621 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
626 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
628 const char *device
= qdict_get_try_str(qdict
, "device");
630 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
631 if (gdbserver_start(device
) < 0) {
632 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
634 } else if (strcmp(device
, "none") == 0) {
635 monitor_printf(mon
, "Disabled gdbserver\n");
637 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
642 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
644 const char *action
= qdict_get_str(qdict
, "action");
645 if (select_watchdog_action(action
) == -1) {
646 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
650 static void monitor_printc(Monitor
*mon
, int c
)
652 monitor_printf(mon
, "'");
655 monitor_printf(mon
, "\\'");
658 monitor_printf(mon
, "\\\\");
661 monitor_printf(mon
, "\\n");
664 monitor_printf(mon
, "\\r");
667 if (c
>= 32 && c
<= 126) {
668 monitor_printf(mon
, "%c", c
);
670 monitor_printf(mon
, "\\x%02x", c
);
674 monitor_printf(mon
, "'");
677 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
678 target_phys_addr_t addr
, int is_physical
)
681 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
689 if (!env
&& !is_physical
)
694 } else if (wsize
== 4) {
697 /* as default we use the current CS size */
701 if ((env
->efer
& MSR_EFER_LMA
) &&
702 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
706 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
711 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
720 nb_per_line
= line_size
/ wsize
;
725 max_digits
= (wsize
* 8 + 2) / 3;
729 max_digits
= (wsize
* 8) / 4;
733 max_digits
= (wsize
* 8 * 10 + 32) / 33;
742 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
744 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
749 cpu_physical_memory_rw(addr
, buf
, l
, 0);
754 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
755 monitor_printf(mon
, " Cannot access memory\n");
764 v
= ldub_raw(buf
+ i
);
767 v
= lduw_raw(buf
+ i
);
770 v
= (uint32_t)ldl_raw(buf
+ i
);
773 v
= ldq_raw(buf
+ i
);
776 monitor_printf(mon
, " ");
779 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
782 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
785 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
788 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
791 monitor_printc(mon
, v
);
796 monitor_printf(mon
, "\n");
802 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
804 int count
= qdict_get_int(qdict
, "count");
805 int format
= qdict_get_int(qdict
, "format");
806 int size
= qdict_get_int(qdict
, "size");
807 target_long addr
= qdict_get_int(qdict
, "addr");
809 memory_dump(mon
, count
, format
, size
, addr
, 0);
812 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
814 int count
= qdict_get_int(qdict
, "count");
815 int format
= qdict_get_int(qdict
, "format");
816 int size
= qdict_get_int(qdict
, "size");
817 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
819 memory_dump(mon
, count
, format
, size
, addr
, 1);
822 static void do_print(Monitor
*mon
, const QDict
*qdict
)
824 int format
= qdict_get_int(qdict
, "format");
825 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
827 #if TARGET_PHYS_ADDR_BITS == 32
830 monitor_printf(mon
, "%#o", val
);
833 monitor_printf(mon
, "%#x", val
);
836 monitor_printf(mon
, "%u", val
);
840 monitor_printf(mon
, "%d", val
);
843 monitor_printc(mon
, val
);
849 monitor_printf(mon
, "%#" PRIo64
, val
);
852 monitor_printf(mon
, "%#" PRIx64
, val
);
855 monitor_printf(mon
, "%" PRIu64
, val
);
859 monitor_printf(mon
, "%" PRId64
, val
);
862 monitor_printc(mon
, val
);
866 monitor_printf(mon
, "\n");
869 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
872 uint32_t size
= qdict_get_int(qdict
, "size");
873 const char *filename
= qdict_get_str(qdict
, "filename");
874 target_long addr
= qdict_get_int(qdict
, "val");
883 f
= fopen(filename
, "wb");
885 monitor_printf(mon
, "could not open '%s'\n", filename
);
892 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
893 fwrite(buf
, 1, l
, f
);
900 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
905 uint32_t size
= qdict_get_int(qdict
, "size");
906 const char *filename
= qdict_get_str(qdict
, "filename");
907 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
909 f
= fopen(filename
, "wb");
911 monitor_printf(mon
, "could not open '%s'\n", filename
);
918 cpu_physical_memory_rw(addr
, buf
, l
, 0);
919 fwrite(buf
, 1, l
, f
);
927 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
932 uint32_t start
= qdict_get_int(qdict
, "start");
933 uint32_t size
= qdict_get_int(qdict
, "size");
936 for(addr
= start
; addr
< (start
+ size
); addr
++) {
937 cpu_physical_memory_rw(addr
, buf
, 1, 0);
938 /* BSD sum algorithm ('sum' Unix command) */
939 sum
= (sum
>> 1) | (sum
<< 15);
942 monitor_printf(mon
, "%05d\n", sum
);
950 static const KeyDef key_defs
[] = {
977 { 0x0e, "backspace" },
1014 { 0x37, "asterisk" },
1017 { 0x3a, "caps_lock" },
1028 { 0x45, "num_lock" },
1029 { 0x46, "scroll_lock" },
1031 { 0xb5, "kp_divide" },
1032 { 0x37, "kp_multiply" },
1033 { 0x4a, "kp_subtract" },
1035 { 0x9c, "kp_enter" },
1036 { 0x53, "kp_decimal" },
1069 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1084 { 0xfe, "compose" },
1089 static int get_keycode(const char *key
)
1095 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1096 if (!strcmp(key
, p
->name
))
1099 if (strstart(key
, "0x", NULL
)) {
1100 ret
= strtoul(key
, &endp
, 0);
1101 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1107 #define MAX_KEYCODES 16
1108 static uint8_t keycodes
[MAX_KEYCODES
];
1109 static int nb_pending_keycodes
;
1110 static QEMUTimer
*key_timer
;
1112 static void release_keys(void *opaque
)
1116 while (nb_pending_keycodes
> 0) {
1117 nb_pending_keycodes
--;
1118 keycode
= keycodes
[nb_pending_keycodes
];
1120 kbd_put_keycode(0xe0);
1121 kbd_put_keycode(keycode
| 0x80);
1125 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1127 char keyname_buf
[16];
1129 int keyname_len
, keycode
, i
;
1130 const char *string
= qdict_get_str(qdict
, "string");
1131 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1132 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1134 if (nb_pending_keycodes
> 0) {
1135 qemu_del_timer(key_timer
);
1142 separator
= strchr(string
, '-');
1143 keyname_len
= separator
? separator
- string
: strlen(string
);
1144 if (keyname_len
> 0) {
1145 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1146 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1147 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1150 if (i
== MAX_KEYCODES
) {
1151 monitor_printf(mon
, "too many keys\n");
1154 keyname_buf
[keyname_len
] = 0;
1155 keycode
= get_keycode(keyname_buf
);
1157 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1160 keycodes
[i
++] = keycode
;
1164 string
= separator
+ 1;
1166 nb_pending_keycodes
= i
;
1167 /* key down events */
1168 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1169 keycode
= keycodes
[i
];
1171 kbd_put_keycode(0xe0);
1172 kbd_put_keycode(keycode
& 0x7f);
1174 /* delayed key up events */
1175 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1176 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1179 static int mouse_button_state
;
1181 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1184 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1185 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1186 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1187 dx
= strtol(dx_str
, NULL
, 0);
1188 dy
= strtol(dy_str
, NULL
, 0);
1191 dz
= strtol(dz_str
, NULL
, 0);
1192 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1195 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1197 int button_state
= qdict_get_int(qdict
, "button_state");
1198 mouse_button_state
= button_state
;
1199 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1202 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1204 int size
= qdict_get_int(qdict
, "size");
1205 int addr
= qdict_get_int(qdict
, "addr");
1206 int has_index
= qdict_haskey(qdict
, "index");
1211 int index
= qdict_get_int(qdict
, "index");
1212 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1220 val
= cpu_inb(addr
);
1224 val
= cpu_inw(addr
);
1228 val
= cpu_inl(addr
);
1232 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1233 suffix
, addr
, size
* 2, val
);
1236 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1238 int size
= qdict_get_int(qdict
, "size");
1239 int addr
= qdict_get_int(qdict
, "addr");
1240 int val
= qdict_get_int(qdict
, "val");
1242 addr
&= IOPORTS_MASK
;
1247 cpu_outb(addr
, val
);
1250 cpu_outw(addr
, val
);
1253 cpu_outl(addr
, val
);
1258 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1261 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1263 res
= qemu_boot_set(bootdevice
);
1265 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1266 } else if (res
> 0) {
1267 monitor_printf(mon
, "setting boot device list failed\n");
1269 monitor_printf(mon
, "no function defined to set boot device list for "
1270 "this architecture\n");
1274 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1276 qemu_system_reset_request();
1279 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1281 qemu_system_powerdown_request();
1284 #if defined(TARGET_I386)
1285 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1287 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1290 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1291 pte
& PG_PSE_MASK
? 'P' : '-',
1292 pte
& PG_DIRTY_MASK
? 'D' : '-',
1293 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1294 pte
& PG_PCD_MASK
? 'C' : '-',
1295 pte
& PG_PWT_MASK
? 'T' : '-',
1296 pte
& PG_USER_MASK
? 'U' : '-',
1297 pte
& PG_RW_MASK
? 'W' : '-');
1300 static void tlb_info(Monitor
*mon
)
1304 uint32_t pgd
, pde
, pte
;
1306 env
= mon_get_cpu();
1310 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1311 monitor_printf(mon
, "PG disabled\n");
1314 pgd
= env
->cr
[3] & ~0xfff;
1315 for(l1
= 0; l1
< 1024; l1
++) {
1316 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1317 pde
= le32_to_cpu(pde
);
1318 if (pde
& PG_PRESENT_MASK
) {
1319 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1320 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1322 for(l2
= 0; l2
< 1024; l2
++) {
1323 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1324 (uint8_t *)&pte
, 4);
1325 pte
= le32_to_cpu(pte
);
1326 if (pte
& PG_PRESENT_MASK
) {
1327 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1337 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1338 uint32_t end
, int prot
)
1341 prot1
= *plast_prot
;
1342 if (prot
!= prot1
) {
1343 if (*pstart
!= -1) {
1344 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1345 *pstart
, end
, end
- *pstart
,
1346 prot1
& PG_USER_MASK
? 'u' : '-',
1348 prot1
& PG_RW_MASK
? 'w' : '-');
1358 static void mem_info(Monitor
*mon
)
1361 int l1
, l2
, prot
, last_prot
;
1362 uint32_t pgd
, pde
, pte
, start
, end
;
1364 env
= mon_get_cpu();
1368 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1369 monitor_printf(mon
, "PG disabled\n");
1372 pgd
= env
->cr
[3] & ~0xfff;
1375 for(l1
= 0; l1
< 1024; l1
++) {
1376 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1377 pde
= le32_to_cpu(pde
);
1379 if (pde
& PG_PRESENT_MASK
) {
1380 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1381 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1382 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1384 for(l2
= 0; l2
< 1024; l2
++) {
1385 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1386 (uint8_t *)&pte
, 4);
1387 pte
= le32_to_cpu(pte
);
1388 end
= (l1
<< 22) + (l2
<< 12);
1389 if (pte
& PG_PRESENT_MASK
) {
1390 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1394 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1399 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1405 #if defined(TARGET_SH4)
1407 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1409 monitor_printf(mon
, " tlb%i:\t"
1410 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1411 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1412 "dirty=%hhu writethrough=%hhu\n",
1414 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1415 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1419 static void tlb_info(Monitor
*mon
)
1421 CPUState
*env
= mon_get_cpu();
1424 monitor_printf (mon
, "ITLB:\n");
1425 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1426 print_tlb (mon
, i
, &env
->itlb
[i
]);
1427 monitor_printf (mon
, "UTLB:\n");
1428 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1429 print_tlb (mon
, i
, &env
->utlb
[i
]);
1434 static void do_info_kvm(Monitor
*mon
)
1437 monitor_printf(mon
, "kvm support: ");
1439 monitor_printf(mon
, "enabled\n");
1441 monitor_printf(mon
, "disabled\n");
1443 monitor_printf(mon
, "kvm support: not compiled\n");
1447 static void do_info_numa(Monitor
*mon
)
1452 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1453 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1454 monitor_printf(mon
, "node %d cpus:", i
);
1455 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1456 if (env
->numa_node
== i
) {
1457 monitor_printf(mon
, " %d", env
->cpu_index
);
1460 monitor_printf(mon
, "\n");
1461 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1466 #ifdef CONFIG_PROFILER
1471 static void do_info_profile(Monitor
*mon
)
1477 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1478 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1479 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1480 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1485 static void do_info_profile(Monitor
*mon
)
1487 monitor_printf(mon
, "Internal profiler not compiled\n");
1491 /* Capture support */
1492 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1494 static void do_info_capture(Monitor
*mon
)
1499 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1500 monitor_printf(mon
, "[%d]: ", i
);
1501 s
->ops
.info (s
->opaque
);
1506 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1509 int n
= qdict_get_int(qdict
, "n");
1512 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1514 s
->ops
.destroy (s
->opaque
);
1515 QLIST_REMOVE (s
, entries
);
1522 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1524 const char *path
= qdict_get_str(qdict
, "path");
1525 int has_freq
= qdict_haskey(qdict
, "freq");
1526 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1527 int has_bits
= qdict_haskey(qdict
, "bits");
1528 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1529 int has_channels
= qdict_haskey(qdict
, "nchannels");
1530 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1533 s
= qemu_mallocz (sizeof (*s
));
1535 freq
= has_freq
? freq
: 44100;
1536 bits
= has_bits
? bits
: 16;
1537 nchannels
= has_channels
? nchannels
: 2;
1539 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1540 monitor_printf(mon
, "Faied to add wave capture\n");
1543 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1547 #if defined(TARGET_I386)
1548 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1551 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1553 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1554 if (env
->cpu_index
== cpu_index
) {
1555 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1561 static void do_info_status(Monitor
*mon
)
1565 monitor_printf(mon
, "VM status: running (single step mode)\n");
1567 monitor_printf(mon
, "VM status: running\n");
1570 monitor_printf(mon
, "VM status: paused\n");
1574 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1576 int value
= qdict_get_int(qdict
, "value");
1577 ram_addr_t target
= value
;
1578 qemu_balloon(target
<< 20);
1581 static void do_info_balloon(Monitor
*mon
)
1585 actual
= qemu_balloon_status();
1586 if (kvm_enabled() && !kvm_has_sync_mmu())
1587 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1588 "ballooning disabled\n");
1589 else if (actual
== 0)
1590 monitor_printf(mon
, "Ballooning not activated in VM\n");
1592 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1595 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1597 qemu_acl
*acl
= qemu_acl_find(name
);
1600 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1605 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1607 const char *aclname
= qdict_get_str(qdict
, "aclname");
1608 qemu_acl
*acl
= find_acl(mon
, aclname
);
1609 qemu_acl_entry
*entry
;
1613 monitor_printf(mon
, "policy: %s\n",
1614 acl
->defaultDeny
? "deny" : "allow");
1615 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1617 monitor_printf(mon
, "%d: %s %s\n", i
,
1618 entry
->deny
? "deny" : "allow", entry
->match
);
1623 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1625 const char *aclname
= qdict_get_str(qdict
, "aclname");
1626 qemu_acl
*acl
= find_acl(mon
, aclname
);
1629 qemu_acl_reset(acl
);
1630 monitor_printf(mon
, "acl: removed all rules\n");
1634 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1636 const char *aclname
= qdict_get_str(qdict
, "aclname");
1637 const char *policy
= qdict_get_str(qdict
, "policy");
1638 qemu_acl
*acl
= find_acl(mon
, aclname
);
1641 if (strcmp(policy
, "allow") == 0) {
1642 acl
->defaultDeny
= 0;
1643 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1644 } else if (strcmp(policy
, "deny") == 0) {
1645 acl
->defaultDeny
= 1;
1646 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1648 monitor_printf(mon
, "acl: unknown policy '%s', "
1649 "expected 'deny' or 'allow'\n", policy
);
1654 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1656 const char *aclname
= qdict_get_str(qdict
, "aclname");
1657 const char *match
= qdict_get_str(qdict
, "match");
1658 const char *policy
= qdict_get_str(qdict
, "policy");
1659 int has_index
= qdict_haskey(qdict
, "index");
1660 int index
= qdict_get_try_int(qdict
, "index", -1);
1661 qemu_acl
*acl
= find_acl(mon
, aclname
);
1665 if (strcmp(policy
, "allow") == 0) {
1667 } else if (strcmp(policy
, "deny") == 0) {
1670 monitor_printf(mon
, "acl: unknown policy '%s', "
1671 "expected 'deny' or 'allow'\n", policy
);
1675 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1677 ret
= qemu_acl_append(acl
, deny
, match
);
1679 monitor_printf(mon
, "acl: unable to add acl entry\n");
1681 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1685 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1687 const char *aclname
= qdict_get_str(qdict
, "aclname");
1688 const char *match
= qdict_get_str(qdict
, "match");
1689 qemu_acl
*acl
= find_acl(mon
, aclname
);
1693 ret
= qemu_acl_remove(acl
, match
);
1695 monitor_printf(mon
, "acl: no matching acl entry\n");
1697 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1701 #if defined(TARGET_I386)
1702 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1705 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1706 int bank
= qdict_get_int(qdict
, "bank");
1707 uint64_t status
= qdict_get_int(qdict
, "status");
1708 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1709 uint64_t addr
= qdict_get_int(qdict
, "addr");
1710 uint64_t misc
= qdict_get_int(qdict
, "misc");
1712 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1713 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1714 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1720 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1722 const char *fdname
= qdict_get_str(qdict
, "fdname");
1726 fd
= qemu_chr_get_msgfd(mon
->chr
);
1728 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1732 if (qemu_isdigit(fdname
[0])) {
1733 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1739 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1744 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1745 if (strcmp(monfd
->name
, fdname
) != 0) {
1754 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1755 monfd
->name
= qemu_strdup(fdname
);
1758 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1761 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1763 const char *fdname
= qdict_get_str(qdict
, "fdname");
1766 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1767 if (strcmp(monfd
->name
, fdname
) != 0) {
1771 QLIST_REMOVE(monfd
, next
);
1773 qemu_free(monfd
->name
);
1778 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1782 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1784 int saved_vm_running
= vm_running
;
1785 const char *name
= qdict_get_str(qdict
, "name");
1789 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1793 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1797 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1800 if (strcmp(monfd
->name
, fdname
) != 0) {
1806 /* caller takes ownership of fd */
1807 QLIST_REMOVE(monfd
, next
);
1808 qemu_free(monfd
->name
);
1817 static const mon_cmd_t mon_cmds
[] = {
1818 #include "qemu-monitor.h"
1822 /* Please update qemu-monitor.hx when adding or changing commands */
1823 static const mon_cmd_t info_cmds
[] = {
1828 .help
= "show the version of QEMU",
1829 .mhandler
.info
= do_info_version
,
1835 .help
= "show the network state",
1836 .mhandler
.info
= do_info_network
,
1842 .help
= "show the character devices",
1843 .mhandler
.info
= qemu_chr_info
,
1849 .help
= "show the block devices",
1850 .mhandler
.info
= bdrv_info
,
1853 .name
= "blockstats",
1856 .help
= "show block device statistics",
1857 .mhandler
.info
= bdrv_info_stats
,
1860 .name
= "registers",
1863 .help
= "show the cpu registers",
1864 .mhandler
.info
= do_info_registers
,
1870 .help
= "show infos for each CPU",
1871 .mhandler
.info
= do_info_cpus
,
1877 .help
= "show the command line history",
1878 .mhandler
.info
= do_info_history
,
1884 .help
= "show the interrupts statistics (if available)",
1885 .mhandler
.info
= irq_info
,
1891 .help
= "show i8259 (PIC) state",
1892 .mhandler
.info
= pic_info
,
1898 .help
= "show PCI info",
1899 .mhandler
.info
= pci_info
,
1901 #if defined(TARGET_I386) || defined(TARGET_SH4)
1906 .help
= "show virtual to physical memory mappings",
1907 .mhandler
.info
= tlb_info
,
1910 #if defined(TARGET_I386)
1915 .help
= "show the active virtual memory mappings",
1916 .mhandler
.info
= mem_info
,
1922 .help
= "show state of HPET",
1923 .mhandler
.info
= do_info_hpet
,
1930 .help
= "show dynamic compiler info",
1931 .mhandler
.info
= do_info_jit
,
1937 .help
= "show KVM information",
1938 .mhandler
.info
= do_info_kvm
,
1944 .help
= "show NUMA information",
1945 .mhandler
.info
= do_info_numa
,
1951 .help
= "show guest USB devices",
1952 .mhandler
.info
= usb_info
,
1958 .help
= "show host USB devices",
1959 .mhandler
.info
= usb_host_info
,
1965 .help
= "show profiling information",
1966 .mhandler
.info
= do_info_profile
,
1972 .help
= "show capture information",
1973 .mhandler
.info
= do_info_capture
,
1976 .name
= "snapshots",
1979 .help
= "show the currently saved VM snapshots",
1980 .mhandler
.info
= do_info_snapshots
,
1986 .help
= "show the current VM status (running|paused)",
1987 .mhandler
.info
= do_info_status
,
1993 .help
= "show guest PCMCIA status",
1994 .mhandler
.info
= pcmcia_info
,
2000 .help
= "show which guest mouse is receiving events",
2001 .mhandler
.info
= do_info_mice
,
2007 .help
= "show the vnc server status",
2008 .mhandler
.info
= do_info_vnc
,
2014 .help
= "show the current VM name",
2015 .mhandler
.info
= do_info_name
,
2021 .help
= "show the current VM UUID",
2022 .mhandler
.info
= do_info_uuid
,
2024 #if defined(TARGET_PPC)
2029 .help
= "show CPU statistics",
2030 .mhandler
.info
= do_info_cpu_stats
,
2033 #if defined(CONFIG_SLIRP)
2038 .help
= "show user network stack connection states",
2039 .mhandler
.info
= do_info_usernet
,
2046 .help
= "show migration status",
2047 .mhandler
.info
= do_info_migrate
,
2053 .help
= "show balloon information",
2054 .mhandler
.info
= do_info_balloon
,
2060 .help
= "show device tree",
2061 .mhandler
.info
= do_info_qtree
,
2067 .help
= "show qdev device model list",
2068 .mhandler
.info
= do_info_qdm
,
2074 .help
= "show roms",
2075 .mhandler
.info
= do_info_roms
,
2082 /*******************************************************************/
2084 static const char *pch
;
2085 static jmp_buf expr_env
;
2090 typedef struct MonitorDef
{
2093 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
2097 #if defined(TARGET_I386)
2098 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
2100 CPUState
*env
= mon_get_cpu();
2103 return env
->eip
+ env
->segs
[R_CS
].base
;
2107 #if defined(TARGET_PPC)
2108 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
2110 CPUState
*env
= mon_get_cpu();
2118 for (i
= 0; i
< 8; i
++)
2119 u
|= env
->crf
[i
] << (32 - (4 * i
));
2124 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
2126 CPUState
*env
= mon_get_cpu();
2132 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
2134 CPUState
*env
= mon_get_cpu();
2140 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
2142 CPUState
*env
= mon_get_cpu();
2145 return cpu_ppc_load_decr(env
);
2148 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
2150 CPUState
*env
= mon_get_cpu();
2153 return cpu_ppc_load_tbu(env
);
2156 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
2158 CPUState
*env
= mon_get_cpu();
2161 return cpu_ppc_load_tbl(env
);
2165 #if defined(TARGET_SPARC)
2166 #ifndef TARGET_SPARC64
2167 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
2169 CPUState
*env
= mon_get_cpu();
2172 return GET_PSR(env
);
2176 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
2178 CPUState
*env
= mon_get_cpu();
2181 return env
->regwptr
[val
];
2185 static const MonitorDef monitor_defs
[] = {
2188 #define SEG(name, seg) \
2189 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2190 { name ".base", offsetof(CPUState, segs[seg].base) },\
2191 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2193 { "eax", offsetof(CPUState
, regs
[0]) },
2194 { "ecx", offsetof(CPUState
, regs
[1]) },
2195 { "edx", offsetof(CPUState
, regs
[2]) },
2196 { "ebx", offsetof(CPUState
, regs
[3]) },
2197 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2198 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2199 { "esi", offsetof(CPUState
, regs
[6]) },
2200 { "edi", offsetof(CPUState
, regs
[7]) },
2201 #ifdef TARGET_X86_64
2202 { "r8", offsetof(CPUState
, regs
[8]) },
2203 { "r9", offsetof(CPUState
, regs
[9]) },
2204 { "r10", offsetof(CPUState
, regs
[10]) },
2205 { "r11", offsetof(CPUState
, regs
[11]) },
2206 { "r12", offsetof(CPUState
, regs
[12]) },
2207 { "r13", offsetof(CPUState
, regs
[13]) },
2208 { "r14", offsetof(CPUState
, regs
[14]) },
2209 { "r15", offsetof(CPUState
, regs
[15]) },
2211 { "eflags", offsetof(CPUState
, eflags
) },
2212 { "eip", offsetof(CPUState
, eip
) },
2219 { "pc", 0, monitor_get_pc
, },
2220 #elif defined(TARGET_PPC)
2221 /* General purpose registers */
2222 { "r0", offsetof(CPUState
, gpr
[0]) },
2223 { "r1", offsetof(CPUState
, gpr
[1]) },
2224 { "r2", offsetof(CPUState
, gpr
[2]) },
2225 { "r3", offsetof(CPUState
, gpr
[3]) },
2226 { "r4", offsetof(CPUState
, gpr
[4]) },
2227 { "r5", offsetof(CPUState
, gpr
[5]) },
2228 { "r6", offsetof(CPUState
, gpr
[6]) },
2229 { "r7", offsetof(CPUState
, gpr
[7]) },
2230 { "r8", offsetof(CPUState
, gpr
[8]) },
2231 { "r9", offsetof(CPUState
, gpr
[9]) },
2232 { "r10", offsetof(CPUState
, gpr
[10]) },
2233 { "r11", offsetof(CPUState
, gpr
[11]) },
2234 { "r12", offsetof(CPUState
, gpr
[12]) },
2235 { "r13", offsetof(CPUState
, gpr
[13]) },
2236 { "r14", offsetof(CPUState
, gpr
[14]) },
2237 { "r15", offsetof(CPUState
, gpr
[15]) },
2238 { "r16", offsetof(CPUState
, gpr
[16]) },
2239 { "r17", offsetof(CPUState
, gpr
[17]) },
2240 { "r18", offsetof(CPUState
, gpr
[18]) },
2241 { "r19", offsetof(CPUState
, gpr
[19]) },
2242 { "r20", offsetof(CPUState
, gpr
[20]) },
2243 { "r21", offsetof(CPUState
, gpr
[21]) },
2244 { "r22", offsetof(CPUState
, gpr
[22]) },
2245 { "r23", offsetof(CPUState
, gpr
[23]) },
2246 { "r24", offsetof(CPUState
, gpr
[24]) },
2247 { "r25", offsetof(CPUState
, gpr
[25]) },
2248 { "r26", offsetof(CPUState
, gpr
[26]) },
2249 { "r27", offsetof(CPUState
, gpr
[27]) },
2250 { "r28", offsetof(CPUState
, gpr
[28]) },
2251 { "r29", offsetof(CPUState
, gpr
[29]) },
2252 { "r30", offsetof(CPUState
, gpr
[30]) },
2253 { "r31", offsetof(CPUState
, gpr
[31]) },
2254 /* Floating point registers */
2255 { "f0", offsetof(CPUState
, fpr
[0]) },
2256 { "f1", offsetof(CPUState
, fpr
[1]) },
2257 { "f2", offsetof(CPUState
, fpr
[2]) },
2258 { "f3", offsetof(CPUState
, fpr
[3]) },
2259 { "f4", offsetof(CPUState
, fpr
[4]) },
2260 { "f5", offsetof(CPUState
, fpr
[5]) },
2261 { "f6", offsetof(CPUState
, fpr
[6]) },
2262 { "f7", offsetof(CPUState
, fpr
[7]) },
2263 { "f8", offsetof(CPUState
, fpr
[8]) },
2264 { "f9", offsetof(CPUState
, fpr
[9]) },
2265 { "f10", offsetof(CPUState
, fpr
[10]) },
2266 { "f11", offsetof(CPUState
, fpr
[11]) },
2267 { "f12", offsetof(CPUState
, fpr
[12]) },
2268 { "f13", offsetof(CPUState
, fpr
[13]) },
2269 { "f14", offsetof(CPUState
, fpr
[14]) },
2270 { "f15", offsetof(CPUState
, fpr
[15]) },
2271 { "f16", offsetof(CPUState
, fpr
[16]) },
2272 { "f17", offsetof(CPUState
, fpr
[17]) },
2273 { "f18", offsetof(CPUState
, fpr
[18]) },
2274 { "f19", offsetof(CPUState
, fpr
[19]) },
2275 { "f20", offsetof(CPUState
, fpr
[20]) },
2276 { "f21", offsetof(CPUState
, fpr
[21]) },
2277 { "f22", offsetof(CPUState
, fpr
[22]) },
2278 { "f23", offsetof(CPUState
, fpr
[23]) },
2279 { "f24", offsetof(CPUState
, fpr
[24]) },
2280 { "f25", offsetof(CPUState
, fpr
[25]) },
2281 { "f26", offsetof(CPUState
, fpr
[26]) },
2282 { "f27", offsetof(CPUState
, fpr
[27]) },
2283 { "f28", offsetof(CPUState
, fpr
[28]) },
2284 { "f29", offsetof(CPUState
, fpr
[29]) },
2285 { "f30", offsetof(CPUState
, fpr
[30]) },
2286 { "f31", offsetof(CPUState
, fpr
[31]) },
2287 { "fpscr", offsetof(CPUState
, fpscr
) },
2288 /* Next instruction pointer */
2289 { "nip|pc", offsetof(CPUState
, nip
) },
2290 { "lr", offsetof(CPUState
, lr
) },
2291 { "ctr", offsetof(CPUState
, ctr
) },
2292 { "decr", 0, &monitor_get_decr
, },
2293 { "ccr", 0, &monitor_get_ccr
, },
2294 /* Machine state register */
2295 { "msr", 0, &monitor_get_msr
, },
2296 { "xer", 0, &monitor_get_xer
, },
2297 { "tbu", 0, &monitor_get_tbu
, },
2298 { "tbl", 0, &monitor_get_tbl
, },
2299 #if defined(TARGET_PPC64)
2300 /* Address space register */
2301 { "asr", offsetof(CPUState
, asr
) },
2303 /* Segment registers */
2304 { "sdr1", offsetof(CPUState
, sdr1
) },
2305 { "sr0", offsetof(CPUState
, sr
[0]) },
2306 { "sr1", offsetof(CPUState
, sr
[1]) },
2307 { "sr2", offsetof(CPUState
, sr
[2]) },
2308 { "sr3", offsetof(CPUState
, sr
[3]) },
2309 { "sr4", offsetof(CPUState
, sr
[4]) },
2310 { "sr5", offsetof(CPUState
, sr
[5]) },
2311 { "sr6", offsetof(CPUState
, sr
[6]) },
2312 { "sr7", offsetof(CPUState
, sr
[7]) },
2313 { "sr8", offsetof(CPUState
, sr
[8]) },
2314 { "sr9", offsetof(CPUState
, sr
[9]) },
2315 { "sr10", offsetof(CPUState
, sr
[10]) },
2316 { "sr11", offsetof(CPUState
, sr
[11]) },
2317 { "sr12", offsetof(CPUState
, sr
[12]) },
2318 { "sr13", offsetof(CPUState
, sr
[13]) },
2319 { "sr14", offsetof(CPUState
, sr
[14]) },
2320 { "sr15", offsetof(CPUState
, sr
[15]) },
2321 /* Too lazy to put BATs and SPRs ... */
2322 #elif defined(TARGET_SPARC)
2323 { "g0", offsetof(CPUState
, gregs
[0]) },
2324 { "g1", offsetof(CPUState
, gregs
[1]) },
2325 { "g2", offsetof(CPUState
, gregs
[2]) },
2326 { "g3", offsetof(CPUState
, gregs
[3]) },
2327 { "g4", offsetof(CPUState
, gregs
[4]) },
2328 { "g5", offsetof(CPUState
, gregs
[5]) },
2329 { "g6", offsetof(CPUState
, gregs
[6]) },
2330 { "g7", offsetof(CPUState
, gregs
[7]) },
2331 { "o0", 0, monitor_get_reg
},
2332 { "o1", 1, monitor_get_reg
},
2333 { "o2", 2, monitor_get_reg
},
2334 { "o3", 3, monitor_get_reg
},
2335 { "o4", 4, monitor_get_reg
},
2336 { "o5", 5, monitor_get_reg
},
2337 { "o6", 6, monitor_get_reg
},
2338 { "o7", 7, monitor_get_reg
},
2339 { "l0", 8, monitor_get_reg
},
2340 { "l1", 9, monitor_get_reg
},
2341 { "l2", 10, monitor_get_reg
},
2342 { "l3", 11, monitor_get_reg
},
2343 { "l4", 12, monitor_get_reg
},
2344 { "l5", 13, monitor_get_reg
},
2345 { "l6", 14, monitor_get_reg
},
2346 { "l7", 15, monitor_get_reg
},
2347 { "i0", 16, monitor_get_reg
},
2348 { "i1", 17, monitor_get_reg
},
2349 { "i2", 18, monitor_get_reg
},
2350 { "i3", 19, monitor_get_reg
},
2351 { "i4", 20, monitor_get_reg
},
2352 { "i5", 21, monitor_get_reg
},
2353 { "i6", 22, monitor_get_reg
},
2354 { "i7", 23, monitor_get_reg
},
2355 { "pc", offsetof(CPUState
, pc
) },
2356 { "npc", offsetof(CPUState
, npc
) },
2357 { "y", offsetof(CPUState
, y
) },
2358 #ifndef TARGET_SPARC64
2359 { "psr", 0, &monitor_get_psr
, },
2360 { "wim", offsetof(CPUState
, wim
) },
2362 { "tbr", offsetof(CPUState
, tbr
) },
2363 { "fsr", offsetof(CPUState
, fsr
) },
2364 { "f0", offsetof(CPUState
, fpr
[0]) },
2365 { "f1", offsetof(CPUState
, fpr
[1]) },
2366 { "f2", offsetof(CPUState
, fpr
[2]) },
2367 { "f3", offsetof(CPUState
, fpr
[3]) },
2368 { "f4", offsetof(CPUState
, fpr
[4]) },
2369 { "f5", offsetof(CPUState
, fpr
[5]) },
2370 { "f6", offsetof(CPUState
, fpr
[6]) },
2371 { "f7", offsetof(CPUState
, fpr
[7]) },
2372 { "f8", offsetof(CPUState
, fpr
[8]) },
2373 { "f9", offsetof(CPUState
, fpr
[9]) },
2374 { "f10", offsetof(CPUState
, fpr
[10]) },
2375 { "f11", offsetof(CPUState
, fpr
[11]) },
2376 { "f12", offsetof(CPUState
, fpr
[12]) },
2377 { "f13", offsetof(CPUState
, fpr
[13]) },
2378 { "f14", offsetof(CPUState
, fpr
[14]) },
2379 { "f15", offsetof(CPUState
, fpr
[15]) },
2380 { "f16", offsetof(CPUState
, fpr
[16]) },
2381 { "f17", offsetof(CPUState
, fpr
[17]) },
2382 { "f18", offsetof(CPUState
, fpr
[18]) },
2383 { "f19", offsetof(CPUState
, fpr
[19]) },
2384 { "f20", offsetof(CPUState
, fpr
[20]) },
2385 { "f21", offsetof(CPUState
, fpr
[21]) },
2386 { "f22", offsetof(CPUState
, fpr
[22]) },
2387 { "f23", offsetof(CPUState
, fpr
[23]) },
2388 { "f24", offsetof(CPUState
, fpr
[24]) },
2389 { "f25", offsetof(CPUState
, fpr
[25]) },
2390 { "f26", offsetof(CPUState
, fpr
[26]) },
2391 { "f27", offsetof(CPUState
, fpr
[27]) },
2392 { "f28", offsetof(CPUState
, fpr
[28]) },
2393 { "f29", offsetof(CPUState
, fpr
[29]) },
2394 { "f30", offsetof(CPUState
, fpr
[30]) },
2395 { "f31", offsetof(CPUState
, fpr
[31]) },
2396 #ifdef TARGET_SPARC64
2397 { "f32", offsetof(CPUState
, fpr
[32]) },
2398 { "f34", offsetof(CPUState
, fpr
[34]) },
2399 { "f36", offsetof(CPUState
, fpr
[36]) },
2400 { "f38", offsetof(CPUState
, fpr
[38]) },
2401 { "f40", offsetof(CPUState
, fpr
[40]) },
2402 { "f42", offsetof(CPUState
, fpr
[42]) },
2403 { "f44", offsetof(CPUState
, fpr
[44]) },
2404 { "f46", offsetof(CPUState
, fpr
[46]) },
2405 { "f48", offsetof(CPUState
, fpr
[48]) },
2406 { "f50", offsetof(CPUState
, fpr
[50]) },
2407 { "f52", offsetof(CPUState
, fpr
[52]) },
2408 { "f54", offsetof(CPUState
, fpr
[54]) },
2409 { "f56", offsetof(CPUState
, fpr
[56]) },
2410 { "f58", offsetof(CPUState
, fpr
[58]) },
2411 { "f60", offsetof(CPUState
, fpr
[60]) },
2412 { "f62", offsetof(CPUState
, fpr
[62]) },
2413 { "asi", offsetof(CPUState
, asi
) },
2414 { "pstate", offsetof(CPUState
, pstate
) },
2415 { "cansave", offsetof(CPUState
, cansave
) },
2416 { "canrestore", offsetof(CPUState
, canrestore
) },
2417 { "otherwin", offsetof(CPUState
, otherwin
) },
2418 { "wstate", offsetof(CPUState
, wstate
) },
2419 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2420 { "fprs", offsetof(CPUState
, fprs
) },
2426 static void expr_error(Monitor
*mon
, const char *msg
)
2428 monitor_printf(mon
, "%s\n", msg
);
2429 longjmp(expr_env
, 1);
2432 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2433 static int get_monitor_def(target_long
*pval
, const char *name
)
2435 const MonitorDef
*md
;
2438 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2439 if (compare_cmd(name
, md
->name
)) {
2440 if (md
->get_value
) {
2441 *pval
= md
->get_value(md
, md
->offset
);
2443 CPUState
*env
= mon_get_cpu();
2446 ptr
= (uint8_t *)env
+ md
->offset
;
2449 *pval
= *(int32_t *)ptr
;
2452 *pval
= *(target_long
*)ptr
;
2465 static void next(void)
2469 while (qemu_isspace(*pch
))
2474 static int64_t expr_sum(Monitor
*mon
);
2476 static int64_t expr_unary(Monitor
*mon
)
2485 n
= expr_unary(mon
);
2489 n
= -expr_unary(mon
);
2493 n
= ~expr_unary(mon
);
2499 expr_error(mon
, "')' expected");
2506 expr_error(mon
, "character constant expected");
2510 expr_error(mon
, "missing terminating \' character");
2520 while ((*pch
>= 'a' && *pch
<= 'z') ||
2521 (*pch
>= 'A' && *pch
<= 'Z') ||
2522 (*pch
>= '0' && *pch
<= '9') ||
2523 *pch
== '_' || *pch
== '.') {
2524 if ((q
- buf
) < sizeof(buf
) - 1)
2528 while (qemu_isspace(*pch
))
2531 ret
= get_monitor_def(®
, buf
);
2533 expr_error(mon
, "unknown register");
2535 expr_error(mon
, "no cpu defined");
2540 expr_error(mon
, "unexpected end of expression");
2544 #if TARGET_PHYS_ADDR_BITS > 32
2545 n
= strtoull(pch
, &p
, 0);
2547 n
= strtoul(pch
, &p
, 0);
2550 expr_error(mon
, "invalid char in expression");
2553 while (qemu_isspace(*pch
))
2561 static int64_t expr_prod(Monitor
*mon
)
2566 val
= expr_unary(mon
);
2569 if (op
!= '*' && op
!= '/' && op
!= '%')
2572 val2
= expr_unary(mon
);
2581 expr_error(mon
, "division by zero");
2592 static int64_t expr_logic(Monitor
*mon
)
2597 val
= expr_prod(mon
);
2600 if (op
!= '&' && op
!= '|' && op
!= '^')
2603 val2
= expr_prod(mon
);
2620 static int64_t expr_sum(Monitor
*mon
)
2625 val
= expr_logic(mon
);
2628 if (op
!= '+' && op
!= '-')
2631 val2
= expr_logic(mon
);
2640 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2643 if (setjmp(expr_env
)) {
2647 while (qemu_isspace(*pch
))
2649 *pval
= expr_sum(mon
);
2654 static int get_str(char *buf
, int buf_size
, const char **pp
)
2662 while (qemu_isspace(*p
))
2672 while (*p
!= '\0' && *p
!= '\"') {
2688 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2691 if ((q
- buf
) < buf_size
- 1) {
2695 if ((q
- buf
) < buf_size
- 1) {
2702 qemu_printf("unterminated string\n");
2707 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2708 if ((q
- buf
) < buf_size
- 1) {
2720 * Store the command-name in cmdname, and return a pointer to
2721 * the remaining of the command string.
2723 static const char *get_command_name(const char *cmdline
,
2724 char *cmdname
, size_t nlen
)
2727 const char *p
, *pstart
;
2730 while (qemu_isspace(*p
))
2735 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2740 memcpy(cmdname
, pstart
, len
);
2741 cmdname
[len
] = '\0';
2746 * Read key of 'type' into 'key' and return the current
2749 static char *key_get_info(const char *type
, char **key
)
2757 p
= strchr(type
, ':');
2764 str
= qemu_malloc(len
+ 1);
2765 memcpy(str
, type
, len
);
2772 static int default_fmt_format
= 'x';
2773 static int default_fmt_size
= 4;
2777 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2778 const char *cmdline
,
2781 const char *p
, *typestr
;
2783 const mon_cmd_t
*cmd
;
2789 monitor_printf(mon
, "command='%s'\n", cmdline
);
2792 /* extract the command name */
2793 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2797 /* find the command */
2798 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2799 if (compare_cmd(cmdname
, cmd
->name
))
2803 if (cmd
->name
== NULL
) {
2804 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2808 /* parse the parameters */
2809 typestr
= cmd
->args_type
;
2811 typestr
= key_get_info(typestr
, &key
);
2823 while (qemu_isspace(*p
))
2825 if (*typestr
== '?') {
2828 /* no optional string: NULL argument */
2832 ret
= get_str(buf
, sizeof(buf
), &p
);
2836 monitor_printf(mon
, "%s: filename expected\n",
2840 monitor_printf(mon
, "%s: block device name expected\n",
2844 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2849 qdict_put(qdict
, key
, qstring_from_str(buf
));
2854 int count
, format
, size
;
2856 while (qemu_isspace(*p
))
2862 if (qemu_isdigit(*p
)) {
2864 while (qemu_isdigit(*p
)) {
2865 count
= count
* 10 + (*p
- '0');
2903 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2904 monitor_printf(mon
, "invalid char in format: '%c'\n",
2909 format
= default_fmt_format
;
2910 if (format
!= 'i') {
2911 /* for 'i', not specifying a size gives -1 as size */
2913 size
= default_fmt_size
;
2914 default_fmt_size
= size
;
2916 default_fmt_format
= format
;
2919 format
= default_fmt_format
;
2920 if (format
!= 'i') {
2921 size
= default_fmt_size
;
2926 qdict_put(qdict
, "count", qint_from_int(count
));
2927 qdict_put(qdict
, "format", qint_from_int(format
));
2928 qdict_put(qdict
, "size", qint_from_int(size
));
2936 while (qemu_isspace(*p
))
2938 if (*typestr
== '?' || *typestr
== '.') {
2939 if (*typestr
== '?') {
2947 while (qemu_isspace(*p
))
2956 if (get_expr(mon
, &val
, &p
))
2958 /* Check if 'i' is greater than 32-bit */
2959 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2960 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2961 monitor_printf(mon
, "integer is for 32-bit values\n");
2964 qdict_put(qdict
, key
, qint_from_int(val
));
2975 while (qemu_isspace(*p
))
2981 monitor_printf(mon
, "%s: unsupported option -%c\n",
2988 qdict_put(qdict
, key
, qint_from_int(has_option
));
2993 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2999 /* check that all arguments were parsed */
3000 while (qemu_isspace(*p
))
3003 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
3015 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
3018 const mon_cmd_t
*cmd
;
3020 qdict
= qdict_new();
3022 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
3026 qemu_errors_to_mon(mon
);
3028 if (monitor_handler_ported(cmd
)) {
3029 QObject
*data
= NULL
;
3031 cmd
->mhandler
.cmd_new(mon
, qdict
, &data
);
3033 cmd
->user_print(mon
, data
);
3035 qobject_decref(data
);
3037 cmd
->mhandler
.cmd(mon
, qdict
);
3040 qemu_errors_to_previous();
3046 static void cmd_completion(const char *name
, const char *list
)
3048 const char *p
, *pstart
;
3057 p
= pstart
+ strlen(pstart
);
3059 if (len
> sizeof(cmd
) - 2)
3060 len
= sizeof(cmd
) - 2;
3061 memcpy(cmd
, pstart
, len
);
3063 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
3064 readline_add_completion(cur_mon
->rs
, cmd
);
3072 static void file_completion(const char *input
)
3077 char file
[1024], file_prefix
[1024];
3081 p
= strrchr(input
, '/');
3084 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
3085 pstrcpy(path
, sizeof(path
), ".");
3087 input_path_len
= p
- input
+ 1;
3088 memcpy(path
, input
, input_path_len
);
3089 if (input_path_len
> sizeof(path
) - 1)
3090 input_path_len
= sizeof(path
) - 1;
3091 path
[input_path_len
] = '\0';
3092 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
3094 #ifdef DEBUG_COMPLETION
3095 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
3096 input
, path
, file_prefix
);
3098 ffs
= opendir(path
);
3106 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
3107 memcpy(file
, input
, input_path_len
);
3108 if (input_path_len
< sizeof(file
))
3109 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
3111 /* stat the file to find out if it's a directory.
3112 * In that case add a slash to speed up typing long paths
3115 if(S_ISDIR(sb
.st_mode
))
3116 pstrcat(file
, sizeof(file
), "/");
3117 readline_add_completion(cur_mon
->rs
, file
);
3123 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3125 const char *name
= bdrv_get_device_name(bs
);
3126 const char *input
= opaque
;
3128 if (input
[0] == '\0' ||
3129 !strncmp(name
, (char *)input
, strlen(input
))) {
3130 readline_add_completion(cur_mon
->rs
, name
);
3134 /* NOTE: this parser is an approximate form of the real command parser */
3135 static void parse_cmdline(const char *cmdline
,
3136 int *pnb_args
, char **args
)
3145 while (qemu_isspace(*p
))
3149 if (nb_args
>= MAX_ARGS
)
3151 ret
= get_str(buf
, sizeof(buf
), &p
);
3152 args
[nb_args
] = qemu_strdup(buf
);
3157 *pnb_args
= nb_args
;
3160 static const char *next_arg_type(const char *typestr
)
3162 const char *p
= strchr(typestr
, ':');
3163 return (p
!= NULL
? ++p
: typestr
);
3166 static void monitor_find_completion(const char *cmdline
)
3168 const char *cmdname
;
3169 char *args
[MAX_ARGS
];
3170 int nb_args
, i
, len
;
3171 const char *ptype
, *str
;
3172 const mon_cmd_t
*cmd
;
3175 parse_cmdline(cmdline
, &nb_args
, args
);
3176 #ifdef DEBUG_COMPLETION
3177 for(i
= 0; i
< nb_args
; i
++) {
3178 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3182 /* if the line ends with a space, it means we want to complete the
3184 len
= strlen(cmdline
);
3185 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3186 if (nb_args
>= MAX_ARGS
)
3188 args
[nb_args
++] = qemu_strdup("");
3191 /* command completion */
3196 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3197 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3198 cmd_completion(cmdname
, cmd
->name
);
3201 /* find the command */
3202 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3203 if (compare_cmd(args
[0], cmd
->name
))
3208 ptype
= next_arg_type(cmd
->args_type
);
3209 for(i
= 0; i
< nb_args
- 2; i
++) {
3210 if (*ptype
!= '\0') {
3211 ptype
= next_arg_type(ptype
);
3212 while (*ptype
== '?')
3213 ptype
= next_arg_type(ptype
);
3216 str
= args
[nb_args
- 1];
3217 if (*ptype
== '-' && ptype
[1] != '\0') {
3222 /* file completion */
3223 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3224 file_completion(str
);
3227 /* block device name completion */
3228 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3229 bdrv_iterate(block_completion_it
, (void *)str
);
3232 /* XXX: more generic ? */
3233 if (!strcmp(cmd
->name
, "info")) {
3234 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3235 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3236 cmd_completion(str
, cmd
->name
);
3238 } else if (!strcmp(cmd
->name
, "sendkey")) {
3239 char *sep
= strrchr(str
, '-');
3242 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3243 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3244 cmd_completion(str
, key
->name
);
3246 } else if (!strcmp(cmd
->name
, "help|?")) {
3247 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3248 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3249 cmd_completion(str
, cmd
->name
);
3257 for(i
= 0; i
< nb_args
; i
++)
3261 static int monitor_can_read(void *opaque
)
3263 Monitor
*mon
= opaque
;
3265 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3268 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3270 Monitor
*old_mon
= cur_mon
;
3276 for (i
= 0; i
< size
; i
++)
3277 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3279 if (size
== 0 || buf
[size
- 1] != 0)
3280 monitor_printf(cur_mon
, "corrupted command\n");
3282 monitor_handle_command(cur_mon
, (char *)buf
);
3288 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3290 monitor_suspend(mon
);
3291 monitor_handle_command(mon
, cmdline
);
3292 monitor_resume(mon
);
3295 int monitor_suspend(Monitor
*mon
)
3303 void monitor_resume(Monitor
*mon
)
3307 if (--mon
->suspend_cnt
== 0)
3308 readline_show_prompt(mon
->rs
);
3311 static void monitor_event(void *opaque
, int event
)
3313 Monitor
*mon
= opaque
;
3316 case CHR_EVENT_MUX_IN
:
3318 if (mon
->reset_seen
) {
3319 readline_restart(mon
->rs
);
3320 monitor_resume(mon
);
3323 mon
->suspend_cnt
= 0;
3327 case CHR_EVENT_MUX_OUT
:
3328 if (mon
->reset_seen
) {
3329 if (mon
->suspend_cnt
== 0) {
3330 monitor_printf(mon
, "\n");
3333 monitor_suspend(mon
);
3340 case CHR_EVENT_RESET
:
3341 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3342 "information\n", QEMU_VERSION
);
3343 if (!mon
->mux_out
) {
3344 readline_show_prompt(mon
->rs
);
3346 mon
->reset_seen
= 1;
3360 void monitor_init(CharDriverState
*chr
, int flags
)
3362 static int is_first_init
= 1;
3365 if (is_first_init
) {
3366 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3370 mon
= qemu_mallocz(sizeof(*mon
));
3374 if (flags
& MONITOR_USE_READLINE
) {
3375 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3376 monitor_read_command(mon
, 0);
3379 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3382 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3383 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3387 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3389 BlockDriverState
*bs
= opaque
;
3392 if (bdrv_set_key(bs
, password
) != 0) {
3393 monitor_printf(mon
, "invalid password\n");
3396 if (mon
->password_completion_cb
)
3397 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3399 monitor_read_command(mon
, 1);
3402 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3403 BlockDriverCompletionFunc
*completion_cb
,
3408 if (!bdrv_key_required(bs
)) {
3410 completion_cb(opaque
, 0);
3414 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3415 bdrv_get_encrypted_filename(bs
));
3417 mon
->password_completion_cb
= completion_cb
;
3418 mon
->password_opaque
= opaque
;
3420 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3422 if (err
&& completion_cb
)
3423 completion_cb(opaque
, err
);
3426 typedef struct QemuErrorSink QemuErrorSink
;
3427 struct QemuErrorSink
{
3436 QemuErrorSink
*previous
;
3439 static QemuErrorSink
*qemu_error_sink
;
3441 void qemu_errors_to_file(FILE *fp
)
3443 QemuErrorSink
*sink
;
3445 sink
= qemu_mallocz(sizeof(*sink
));
3446 sink
->dest
= ERR_SINK_FILE
;
3448 sink
->previous
= qemu_error_sink
;
3449 qemu_error_sink
= sink
;
3452 void qemu_errors_to_mon(Monitor
*mon
)
3454 QemuErrorSink
*sink
;
3456 sink
= qemu_mallocz(sizeof(*sink
));
3457 sink
->dest
= ERR_SINK_MONITOR
;
3459 sink
->previous
= qemu_error_sink
;
3460 qemu_error_sink
= sink
;
3463 void qemu_errors_to_previous(void)
3465 QemuErrorSink
*sink
;
3467 assert(qemu_error_sink
!= NULL
);
3468 sink
= qemu_error_sink
;
3469 qemu_error_sink
= sink
->previous
;
3473 void qemu_error(const char *fmt
, ...)
3477 assert(qemu_error_sink
!= NULL
);
3478 switch (qemu_error_sink
->dest
) {
3480 va_start(args
, fmt
);
3481 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3484 case ERR_SINK_MONITOR
:
3485 va_start(args
, fmt
);
3486 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);