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
;
79 /* file descriptors passed via SCM_RIGHTS */
80 typedef struct mon_fd_t mon_fd_t
;
84 QLIST_ENTRY(mon_fd_t
) next
;
97 BlockDriverCompletionFunc
*password_completion_cb
;
98 void *password_opaque
;
99 QLIST_HEAD(,mon_fd_t
) fds
;
100 QLIST_ENTRY(Monitor
) entry
;
103 static QLIST_HEAD(mon_list
, Monitor
) mon_list
;
105 static const mon_cmd_t mon_cmds
[];
106 static const mon_cmd_t info_cmds
[];
108 Monitor
*cur_mon
= NULL
;
110 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
113 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
115 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
117 readline_show_prompt(mon
->rs
);
120 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
124 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
125 /* prompt is printed on return from the command handler */
128 monitor_printf(mon
, "terminal does not support password prompting\n");
133 void monitor_flush(Monitor
*mon
)
135 if (mon
&& mon
->outbuf_index
!= 0 && !mon
->mux_out
) {
136 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
137 mon
->outbuf_index
= 0;
141 /* flush at every end of line or if the buffer is full */
142 static void monitor_puts(Monitor
*mon
, const char *str
)
154 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
155 mon
->outbuf
[mon
->outbuf_index
++] = c
;
156 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
162 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
165 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
166 monitor_puts(mon
, buf
);
169 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
173 monitor_vprintf(mon
, fmt
, ap
);
177 void monitor_print_filename(Monitor
*mon
, const char *filename
)
181 for (i
= 0; filename
[i
]; i
++) {
182 switch (filename
[i
]) {
186 monitor_printf(mon
, "\\%c", filename
[i
]);
189 monitor_printf(mon
, "\\t");
192 monitor_printf(mon
, "\\r");
195 monitor_printf(mon
, "\\n");
198 monitor_printf(mon
, "%c", filename
[i
]);
204 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
208 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
213 static int compare_cmd(const char *name
, const char *list
)
215 const char *p
, *pstart
;
223 p
= pstart
+ strlen(pstart
);
224 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
233 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
234 const char *prefix
, const char *name
)
236 const mon_cmd_t
*cmd
;
238 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
239 if (!name
|| !strcmp(name
, cmd
->name
))
240 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
241 cmd
->params
, cmd
->help
);
245 static void help_cmd(Monitor
*mon
, const char *name
)
247 if (name
&& !strcmp(name
, "info")) {
248 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
250 help_cmd_dump(mon
, mon_cmds
, "", name
);
251 if (name
&& !strcmp(name
, "log")) {
252 const CPULogItem
*item
;
253 monitor_printf(mon
, "Log items (comma separated):\n");
254 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
255 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
256 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
262 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
264 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
267 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
271 const char *device
= qdict_get_str(qdict
, "device");
273 all_devices
= !strcmp(device
, "all");
274 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
276 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
278 bdrv_commit(dinfo
->bdrv
);
282 static void do_info(Monitor
*mon
, const QDict
*qdict
)
284 const mon_cmd_t
*cmd
;
285 const char *item
= qdict_get_try_str(qdict
, "item");
286 void (*handler
)(Monitor
*);
290 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
291 if (compare_cmd(item
, cmd
->name
))
295 help_cmd(mon
, "info");
298 handler
= cmd
->handler
;
302 static void do_info_version(Monitor
*mon
)
304 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
307 static void do_info_name(Monitor
*mon
)
310 monitor_printf(mon
, "%s\n", qemu_name
);
313 #if defined(TARGET_I386)
314 static void do_info_hpet(Monitor
*mon
)
316 monitor_printf(mon
, "HPET is %s by QEMU\n",
317 (no_hpet
) ? "disabled" : "enabled");
321 static void do_info_uuid(Monitor
*mon
)
323 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
324 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
325 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
326 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
327 qemu_uuid
[14], qemu_uuid
[15]);
330 /* get the current CPU defined by the user */
331 static int mon_set_cpu(int cpu_index
)
335 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
336 if (env
->cpu_index
== cpu_index
) {
337 cur_mon
->mon_cpu
= env
;
344 static CPUState
*mon_get_cpu(void)
346 if (!cur_mon
->mon_cpu
) {
349 cpu_synchronize_state(cur_mon
->mon_cpu
);
350 return cur_mon
->mon_cpu
;
353 static void do_info_registers(Monitor
*mon
)
360 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
363 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
368 static void do_info_cpus(Monitor
*mon
)
372 /* just to set the default cpu if not already done */
375 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
376 cpu_synchronize_state(env
);
377 monitor_printf(mon
, "%c CPU #%d:",
378 (env
== mon
->mon_cpu
) ? '*' : ' ',
380 #if defined(TARGET_I386)
381 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
382 env
->eip
+ env
->segs
[R_CS
].base
);
383 #elif defined(TARGET_PPC)
384 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
385 #elif defined(TARGET_SPARC)
386 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
388 #elif defined(TARGET_MIPS)
389 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
392 monitor_printf(mon
, " (halted)");
393 monitor_printf(mon
, "\n");
397 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
399 int index
= qdict_get_int(qdict
, "index");
400 if (mon_set_cpu(index
) < 0)
401 monitor_printf(mon
, "Invalid CPU index\n");
404 static void do_info_jit(Monitor
*mon
)
406 dump_exec_info((FILE *)mon
, monitor_fprintf
);
409 static void do_info_history(Monitor
*mon
)
418 str
= readline_get_history(mon
->rs
, i
);
421 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
426 #if defined(TARGET_PPC)
427 /* XXX: not implemented in other targets */
428 static void do_info_cpu_stats(Monitor
*mon
)
433 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
437 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
442 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
444 if (bdrv_is_inserted(bs
)) {
446 if (!bdrv_is_removable(bs
)) {
447 monitor_printf(mon
, "device is not removable\n");
450 if (bdrv_is_locked(bs
)) {
451 monitor_printf(mon
, "device is locked\n");
460 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
462 BlockDriverState
*bs
;
463 int force
= qdict_get_int(qdict
, "force");
464 const char *filename
= qdict_get_str(qdict
, "filename");
466 bs
= bdrv_find(filename
);
468 monitor_printf(mon
, "device not found\n");
471 eject_device(mon
, bs
, force
);
474 static void do_change_block(Monitor
*mon
, const char *device
,
475 const char *filename
, const char *fmt
)
477 BlockDriverState
*bs
;
478 BlockDriver
*drv
= NULL
;
480 bs
= bdrv_find(device
);
482 monitor_printf(mon
, "device not found\n");
486 drv
= bdrv_find_format(fmt
);
488 monitor_printf(mon
, "invalid format %s\n", fmt
);
492 if (eject_device(mon
, bs
, 0) < 0)
494 bdrv_open2(bs
, filename
, 0, drv
);
495 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
498 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
501 if (vnc_display_password(NULL
, password
) < 0)
502 monitor_printf(mon
, "could not set VNC server password\n");
504 monitor_read_command(mon
, 1);
507 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
509 if (strcmp(target
, "passwd") == 0 ||
510 strcmp(target
, "password") == 0) {
513 strncpy(password
, arg
, sizeof(password
));
514 password
[sizeof(password
) - 1] = '\0';
515 change_vnc_password_cb(mon
, password
, NULL
);
517 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
520 if (vnc_display_open(NULL
, target
) < 0)
521 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
525 static void do_change(Monitor
*mon
, const QDict
*qdict
)
527 const char *device
= qdict_get_str(qdict
, "device");
528 const char *target
= qdict_get_str(qdict
, "target");
529 const char *arg
= qdict_get_try_str(qdict
, "arg");
530 if (strcmp(device
, "vnc") == 0) {
531 do_change_vnc(mon
, target
, arg
);
533 do_change_block(mon
, device
, target
, arg
);
537 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
539 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
542 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
544 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
547 static void do_log(Monitor
*mon
, const QDict
*qdict
)
550 const char *items
= qdict_get_str(qdict
, "items");
552 if (!strcmp(items
, "none")) {
555 mask
= cpu_str_to_log_mask(items
);
557 help_cmd(mon
, "log");
564 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
566 const char *option
= qdict_get_try_str(qdict
, "option");
567 if (!option
|| !strcmp(option
, "on")) {
569 } else if (!strcmp(option
, "off")) {
572 monitor_printf(mon
, "unexpected option %s\n", option
);
576 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
578 vm_stop(EXCP_INTERRUPT
);
581 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
583 struct bdrv_iterate_context
{
588 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
590 struct bdrv_iterate_context context
= { mon
, 0 };
592 bdrv_iterate(encrypted_bdrv_it
, &context
);
593 /* only resume the vm if all keys are set and valid */
598 static void bdrv_key_cb(void *opaque
, int err
)
600 Monitor
*mon
= opaque
;
602 /* another key was set successfully, retry to continue */
607 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
609 struct bdrv_iterate_context
*context
= opaque
;
611 if (!context
->err
&& bdrv_key_required(bs
)) {
612 context
->err
= -EBUSY
;
613 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
618 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
620 const char *device
= qdict_get_try_str(qdict
, "device");
622 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
623 if (gdbserver_start(device
) < 0) {
624 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
626 } else if (strcmp(device
, "none") == 0) {
627 monitor_printf(mon
, "Disabled gdbserver\n");
629 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
634 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
636 const char *action
= qdict_get_str(qdict
, "action");
637 if (select_watchdog_action(action
) == -1) {
638 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
642 static void monitor_printc(Monitor
*mon
, int c
)
644 monitor_printf(mon
, "'");
647 monitor_printf(mon
, "\\'");
650 monitor_printf(mon
, "\\\\");
653 monitor_printf(mon
, "\\n");
656 monitor_printf(mon
, "\\r");
659 if (c
>= 32 && c
<= 126) {
660 monitor_printf(mon
, "%c", c
);
662 monitor_printf(mon
, "\\x%02x", c
);
666 monitor_printf(mon
, "'");
669 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
670 target_phys_addr_t addr
, int is_physical
)
673 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
681 if (!env
&& !is_physical
)
686 } else if (wsize
== 4) {
689 /* as default we use the current CS size */
693 if ((env
->efer
& MSR_EFER_LMA
) &&
694 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
698 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
703 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
712 nb_per_line
= line_size
/ wsize
;
717 max_digits
= (wsize
* 8 + 2) / 3;
721 max_digits
= (wsize
* 8) / 4;
725 max_digits
= (wsize
* 8 * 10 + 32) / 33;
734 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
736 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
741 cpu_physical_memory_rw(addr
, buf
, l
, 0);
746 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
747 monitor_printf(mon
, " Cannot access memory\n");
756 v
= ldub_raw(buf
+ i
);
759 v
= lduw_raw(buf
+ i
);
762 v
= (uint32_t)ldl_raw(buf
+ i
);
765 v
= ldq_raw(buf
+ i
);
768 monitor_printf(mon
, " ");
771 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
774 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
777 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
780 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
783 monitor_printc(mon
, v
);
788 monitor_printf(mon
, "\n");
794 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
796 int count
= qdict_get_int(qdict
, "count");
797 int format
= qdict_get_int(qdict
, "format");
798 int size
= qdict_get_int(qdict
, "size");
799 target_long addr
= qdict_get_int(qdict
, "addr");
801 memory_dump(mon
, count
, format
, size
, addr
, 0);
804 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
806 int count
= qdict_get_int(qdict
, "count");
807 int format
= qdict_get_int(qdict
, "format");
808 int size
= qdict_get_int(qdict
, "size");
809 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
811 memory_dump(mon
, count
, format
, size
, addr
, 1);
814 static void do_print(Monitor
*mon
, const QDict
*qdict
)
816 int format
= qdict_get_int(qdict
, "format");
817 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
819 #if TARGET_PHYS_ADDR_BITS == 32
822 monitor_printf(mon
, "%#o", val
);
825 monitor_printf(mon
, "%#x", val
);
828 monitor_printf(mon
, "%u", val
);
832 monitor_printf(mon
, "%d", val
);
835 monitor_printc(mon
, val
);
841 monitor_printf(mon
, "%#" PRIo64
, val
);
844 monitor_printf(mon
, "%#" PRIx64
, val
);
847 monitor_printf(mon
, "%" PRIu64
, val
);
851 monitor_printf(mon
, "%" PRId64
, val
);
854 monitor_printc(mon
, val
);
858 monitor_printf(mon
, "\n");
861 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
864 uint32_t size
= qdict_get_int(qdict
, "size");
865 const char *filename
= qdict_get_str(qdict
, "filename");
866 target_long addr
= qdict_get_int(qdict
, "val");
875 f
= fopen(filename
, "wb");
877 monitor_printf(mon
, "could not open '%s'\n", filename
);
884 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
885 fwrite(buf
, 1, l
, f
);
892 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
897 uint32_t size
= qdict_get_int(qdict
, "size");
898 const char *filename
= qdict_get_str(qdict
, "filename");
899 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
901 f
= fopen(filename
, "wb");
903 monitor_printf(mon
, "could not open '%s'\n", filename
);
910 cpu_physical_memory_rw(addr
, buf
, l
, 0);
911 fwrite(buf
, 1, l
, f
);
919 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
924 uint32_t start
= qdict_get_int(qdict
, "start");
925 uint32_t size
= qdict_get_int(qdict
, "size");
928 for(addr
= start
; addr
< (start
+ size
); addr
++) {
929 cpu_physical_memory_rw(addr
, buf
, 1, 0);
930 /* BSD sum algorithm ('sum' Unix command) */
931 sum
= (sum
>> 1) | (sum
<< 15);
934 monitor_printf(mon
, "%05d\n", sum
);
942 static const KeyDef key_defs
[] = {
969 { 0x0e, "backspace" },
1006 { 0x37, "asterisk" },
1009 { 0x3a, "caps_lock" },
1020 { 0x45, "num_lock" },
1021 { 0x46, "scroll_lock" },
1023 { 0xb5, "kp_divide" },
1024 { 0x37, "kp_multiply" },
1025 { 0x4a, "kp_subtract" },
1027 { 0x9c, "kp_enter" },
1028 { 0x53, "kp_decimal" },
1061 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1076 { 0xfe, "compose" },
1081 static int get_keycode(const char *key
)
1087 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1088 if (!strcmp(key
, p
->name
))
1091 if (strstart(key
, "0x", NULL
)) {
1092 ret
= strtoul(key
, &endp
, 0);
1093 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1099 #define MAX_KEYCODES 16
1100 static uint8_t keycodes
[MAX_KEYCODES
];
1101 static int nb_pending_keycodes
;
1102 static QEMUTimer
*key_timer
;
1104 static void release_keys(void *opaque
)
1108 while (nb_pending_keycodes
> 0) {
1109 nb_pending_keycodes
--;
1110 keycode
= keycodes
[nb_pending_keycodes
];
1112 kbd_put_keycode(0xe0);
1113 kbd_put_keycode(keycode
| 0x80);
1117 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1119 char keyname_buf
[16];
1121 int keyname_len
, keycode
, i
;
1122 const char *string
= qdict_get_str(qdict
, "string");
1123 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1124 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1126 if (nb_pending_keycodes
> 0) {
1127 qemu_del_timer(key_timer
);
1134 separator
= strchr(string
, '-');
1135 keyname_len
= separator
? separator
- string
: strlen(string
);
1136 if (keyname_len
> 0) {
1137 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1138 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1139 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1142 if (i
== MAX_KEYCODES
) {
1143 monitor_printf(mon
, "too many keys\n");
1146 keyname_buf
[keyname_len
] = 0;
1147 keycode
= get_keycode(keyname_buf
);
1149 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1152 keycodes
[i
++] = keycode
;
1156 string
= separator
+ 1;
1158 nb_pending_keycodes
= i
;
1159 /* key down events */
1160 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1161 keycode
= keycodes
[i
];
1163 kbd_put_keycode(0xe0);
1164 kbd_put_keycode(keycode
& 0x7f);
1166 /* delayed key up events */
1167 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1168 muldiv64(get_ticks_per_sec(), hold_time
, 1000));
1171 static int mouse_button_state
;
1173 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1176 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1177 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1178 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1179 dx
= strtol(dx_str
, NULL
, 0);
1180 dy
= strtol(dy_str
, NULL
, 0);
1183 dz
= strtol(dz_str
, NULL
, 0);
1184 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1187 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1189 int button_state
= qdict_get_int(qdict
, "button_state");
1190 mouse_button_state
= button_state
;
1191 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1194 static void do_ioport_read(Monitor
*mon
, const QDict
*qdict
)
1196 int size
= qdict_get_int(qdict
, "size");
1197 int addr
= qdict_get_int(qdict
, "addr");
1198 int has_index
= qdict_haskey(qdict
, "index");
1203 int index
= qdict_get_int(qdict
, "index");
1204 cpu_outb(addr
& IOPORTS_MASK
, index
& 0xff);
1212 val
= cpu_inb(addr
);
1216 val
= cpu_inw(addr
);
1220 val
= cpu_inl(addr
);
1224 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1225 suffix
, addr
, size
* 2, val
);
1228 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1230 int size
= qdict_get_int(qdict
, "size");
1231 int addr
= qdict_get_int(qdict
, "addr");
1232 int val
= qdict_get_int(qdict
, "val");
1234 addr
&= IOPORTS_MASK
;
1239 cpu_outb(addr
, val
);
1242 cpu_outw(addr
, val
);
1245 cpu_outl(addr
, val
);
1250 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1253 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1255 res
= qemu_boot_set(bootdevice
);
1257 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1258 } else if (res
> 0) {
1259 monitor_printf(mon
, "setting boot device list failed\n");
1261 monitor_printf(mon
, "no function defined to set boot device list for "
1262 "this architecture\n");
1266 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1268 qemu_system_reset_request();
1271 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1273 qemu_system_powerdown_request();
1276 #if defined(TARGET_I386)
1277 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1279 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1282 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1283 pte
& PG_PSE_MASK
? 'P' : '-',
1284 pte
& PG_DIRTY_MASK
? 'D' : '-',
1285 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1286 pte
& PG_PCD_MASK
? 'C' : '-',
1287 pte
& PG_PWT_MASK
? 'T' : '-',
1288 pte
& PG_USER_MASK
? 'U' : '-',
1289 pte
& PG_RW_MASK
? 'W' : '-');
1292 static void tlb_info(Monitor
*mon
)
1296 uint32_t pgd
, pde
, pte
;
1298 env
= mon_get_cpu();
1302 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1303 monitor_printf(mon
, "PG disabled\n");
1306 pgd
= env
->cr
[3] & ~0xfff;
1307 for(l1
= 0; l1
< 1024; l1
++) {
1308 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1309 pde
= le32_to_cpu(pde
);
1310 if (pde
& PG_PRESENT_MASK
) {
1311 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1312 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1314 for(l2
= 0; l2
< 1024; l2
++) {
1315 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1316 (uint8_t *)&pte
, 4);
1317 pte
= le32_to_cpu(pte
);
1318 if (pte
& PG_PRESENT_MASK
) {
1319 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1329 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1330 uint32_t end
, int prot
)
1333 prot1
= *plast_prot
;
1334 if (prot
!= prot1
) {
1335 if (*pstart
!= -1) {
1336 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1337 *pstart
, end
, end
- *pstart
,
1338 prot1
& PG_USER_MASK
? 'u' : '-',
1340 prot1
& PG_RW_MASK
? 'w' : '-');
1350 static void mem_info(Monitor
*mon
)
1353 int l1
, l2
, prot
, last_prot
;
1354 uint32_t pgd
, pde
, pte
, start
, end
;
1356 env
= mon_get_cpu();
1360 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1361 monitor_printf(mon
, "PG disabled\n");
1364 pgd
= env
->cr
[3] & ~0xfff;
1367 for(l1
= 0; l1
< 1024; l1
++) {
1368 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1369 pde
= le32_to_cpu(pde
);
1371 if (pde
& PG_PRESENT_MASK
) {
1372 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1373 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1374 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1376 for(l2
= 0; l2
< 1024; l2
++) {
1377 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1378 (uint8_t *)&pte
, 4);
1379 pte
= le32_to_cpu(pte
);
1380 end
= (l1
<< 22) + (l2
<< 12);
1381 if (pte
& PG_PRESENT_MASK
) {
1382 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1386 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1391 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1397 #if defined(TARGET_SH4)
1399 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1401 monitor_printf(mon
, " tlb%i:\t"
1402 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1403 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1404 "dirty=%hhu writethrough=%hhu\n",
1406 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1407 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1411 static void tlb_info(Monitor
*mon
)
1413 CPUState
*env
= mon_get_cpu();
1416 monitor_printf (mon
, "ITLB:\n");
1417 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1418 print_tlb (mon
, i
, &env
->itlb
[i
]);
1419 monitor_printf (mon
, "UTLB:\n");
1420 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1421 print_tlb (mon
, i
, &env
->utlb
[i
]);
1426 static void do_info_kvm(Monitor
*mon
)
1429 monitor_printf(mon
, "kvm support: ");
1431 monitor_printf(mon
, "enabled\n");
1433 monitor_printf(mon
, "disabled\n");
1435 monitor_printf(mon
, "kvm support: not compiled\n");
1439 static void do_info_numa(Monitor
*mon
)
1444 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1445 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1446 monitor_printf(mon
, "node %d cpus:", i
);
1447 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1448 if (env
->numa_node
== i
) {
1449 monitor_printf(mon
, " %d", env
->cpu_index
);
1452 monitor_printf(mon
, "\n");
1453 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1458 #ifdef CONFIG_PROFILER
1463 static void do_info_profile(Monitor
*mon
)
1469 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1470 dev_time
, dev_time
/ (double)get_ticks_per_sec());
1471 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1472 qemu_time
, qemu_time
/ (double)get_ticks_per_sec());
1477 static void do_info_profile(Monitor
*mon
)
1479 monitor_printf(mon
, "Internal profiler not compiled\n");
1483 /* Capture support */
1484 static QLIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1486 static void do_info_capture(Monitor
*mon
)
1491 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1492 monitor_printf(mon
, "[%d]: ", i
);
1493 s
->ops
.info (s
->opaque
);
1498 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1501 int n
= qdict_get_int(qdict
, "n");
1504 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1506 s
->ops
.destroy (s
->opaque
);
1507 QLIST_REMOVE (s
, entries
);
1514 static void do_wav_capture(Monitor
*mon
, const QDict
*qdict
)
1516 const char *path
= qdict_get_str(qdict
, "path");
1517 int has_freq
= qdict_haskey(qdict
, "freq");
1518 int freq
= qdict_get_try_int(qdict
, "freq", -1);
1519 int has_bits
= qdict_haskey(qdict
, "bits");
1520 int bits
= qdict_get_try_int(qdict
, "bits", -1);
1521 int has_channels
= qdict_haskey(qdict
, "nchannels");
1522 int nchannels
= qdict_get_try_int(qdict
, "nchannels", -1);
1525 s
= qemu_mallocz (sizeof (*s
));
1527 freq
= has_freq
? freq
: 44100;
1528 bits
= has_bits
? bits
: 16;
1529 nchannels
= has_channels
? nchannels
: 2;
1531 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1532 monitor_printf(mon
, "Faied to add wave capture\n");
1535 QLIST_INSERT_HEAD (&capture_head
, s
, entries
);
1539 #if defined(TARGET_I386)
1540 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1543 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1545 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1546 if (env
->cpu_index
== cpu_index
) {
1547 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1553 static void do_info_status(Monitor
*mon
)
1557 monitor_printf(mon
, "VM status: running (single step mode)\n");
1559 monitor_printf(mon
, "VM status: running\n");
1562 monitor_printf(mon
, "VM status: paused\n");
1566 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1568 int value
= qdict_get_int(qdict
, "value");
1569 ram_addr_t target
= value
;
1570 qemu_balloon(target
<< 20);
1573 static void do_info_balloon(Monitor
*mon
)
1577 actual
= qemu_balloon_status();
1578 if (kvm_enabled() && !kvm_has_sync_mmu())
1579 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1580 "ballooning disabled\n");
1581 else if (actual
== 0)
1582 monitor_printf(mon
, "Ballooning not activated in VM\n");
1584 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1587 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1589 qemu_acl
*acl
= qemu_acl_find(name
);
1592 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1597 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1599 const char *aclname
= qdict_get_str(qdict
, "aclname");
1600 qemu_acl
*acl
= find_acl(mon
, aclname
);
1601 qemu_acl_entry
*entry
;
1605 monitor_printf(mon
, "policy: %s\n",
1606 acl
->defaultDeny
? "deny" : "allow");
1607 QTAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1609 monitor_printf(mon
, "%d: %s %s\n", i
,
1610 entry
->deny
? "deny" : "allow", entry
->match
);
1615 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1617 const char *aclname
= qdict_get_str(qdict
, "aclname");
1618 qemu_acl
*acl
= find_acl(mon
, aclname
);
1621 qemu_acl_reset(acl
);
1622 monitor_printf(mon
, "acl: removed all rules\n");
1626 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1628 const char *aclname
= qdict_get_str(qdict
, "aclname");
1629 const char *policy
= qdict_get_str(qdict
, "policy");
1630 qemu_acl
*acl
= find_acl(mon
, aclname
);
1633 if (strcmp(policy
, "allow") == 0) {
1634 acl
->defaultDeny
= 0;
1635 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1636 } else if (strcmp(policy
, "deny") == 0) {
1637 acl
->defaultDeny
= 1;
1638 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1640 monitor_printf(mon
, "acl: unknown policy '%s', "
1641 "expected 'deny' or 'allow'\n", policy
);
1646 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1648 const char *aclname
= qdict_get_str(qdict
, "aclname");
1649 const char *match
= qdict_get_str(qdict
, "match");
1650 const char *policy
= qdict_get_str(qdict
, "policy");
1651 int has_index
= qdict_haskey(qdict
, "index");
1652 int index
= qdict_get_try_int(qdict
, "index", -1);
1653 qemu_acl
*acl
= find_acl(mon
, aclname
);
1657 if (strcmp(policy
, "allow") == 0) {
1659 } else if (strcmp(policy
, "deny") == 0) {
1662 monitor_printf(mon
, "acl: unknown policy '%s', "
1663 "expected 'deny' or 'allow'\n", policy
);
1667 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1669 ret
= qemu_acl_append(acl
, deny
, match
);
1671 monitor_printf(mon
, "acl: unable to add acl entry\n");
1673 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1677 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1679 const char *aclname
= qdict_get_str(qdict
, "aclname");
1680 const char *match
= qdict_get_str(qdict
, "match");
1681 qemu_acl
*acl
= find_acl(mon
, aclname
);
1685 ret
= qemu_acl_remove(acl
, match
);
1687 monitor_printf(mon
, "acl: no matching acl entry\n");
1689 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1693 #if defined(TARGET_I386)
1694 static void do_inject_mce(Monitor
*mon
, const QDict
*qdict
)
1697 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1698 int bank
= qdict_get_int(qdict
, "bank");
1699 uint64_t status
= qdict_get_int(qdict
, "status");
1700 uint64_t mcg_status
= qdict_get_int(qdict
, "mcg_status");
1701 uint64_t addr
= qdict_get_int(qdict
, "addr");
1702 uint64_t misc
= qdict_get_int(qdict
, "misc");
1704 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1705 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1706 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1712 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1714 const char *fdname
= qdict_get_str(qdict
, "fdname");
1718 fd
= qemu_chr_get_msgfd(mon
->chr
);
1720 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1724 if (qemu_isdigit(fdname
[0])) {
1725 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1731 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1736 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1737 if (strcmp(monfd
->name
, fdname
) != 0) {
1746 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1747 monfd
->name
= qemu_strdup(fdname
);
1750 QLIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1753 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1755 const char *fdname
= qdict_get_str(qdict
, "fdname");
1758 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1759 if (strcmp(monfd
->name
, fdname
) != 0) {
1763 QLIST_REMOVE(monfd
, next
);
1765 qemu_free(monfd
->name
);
1770 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1774 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1776 int saved_vm_running
= vm_running
;
1777 const char *name
= qdict_get_str(qdict
, "name");
1781 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1785 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1789 QLIST_FOREACH(monfd
, &mon
->fds
, next
) {
1792 if (strcmp(monfd
->name
, fdname
) != 0) {
1798 /* caller takes ownership of fd */
1799 QLIST_REMOVE(monfd
, next
);
1800 qemu_free(monfd
->name
);
1809 static const mon_cmd_t mon_cmds
[] = {
1810 #include "qemu-monitor.h"
1814 /* Please update qemu-monitor.hx when adding or changing commands */
1815 static const mon_cmd_t info_cmds
[] = {
1816 { "version", "", do_info_version
,
1817 "", "show the version of QEMU" },
1818 { "network", "", do_info_network
,
1819 "", "show the network state" },
1820 { "chardev", "", qemu_chr_info
,
1821 "", "show the character devices" },
1822 { "block", "", bdrv_info
,
1823 "", "show the block devices" },
1824 { "blockstats", "", bdrv_info_stats
,
1825 "", "show block device statistics" },
1826 { "registers", "", do_info_registers
,
1827 "", "show the cpu registers" },
1828 { "cpus", "", do_info_cpus
,
1829 "", "show infos for each CPU" },
1830 { "history", "", do_info_history
,
1831 "", "show the command line history", },
1832 { "irq", "", irq_info
,
1833 "", "show the interrupts statistics (if available)", },
1834 { "pic", "", pic_info
,
1835 "", "show i8259 (PIC) state", },
1836 { "pci", "", pci_info
,
1837 "", "show PCI info", },
1838 #if defined(TARGET_I386) || defined(TARGET_SH4)
1839 { "tlb", "", tlb_info
,
1840 "", "show virtual to physical memory mappings", },
1842 #if defined(TARGET_I386)
1843 { "mem", "", mem_info
,
1844 "", "show the active virtual memory mappings", },
1845 { "hpet", "", do_info_hpet
,
1846 "", "show state of HPET", },
1848 { "jit", "", do_info_jit
,
1849 "", "show dynamic compiler info", },
1850 { "kvm", "", do_info_kvm
,
1851 "", "show KVM information", },
1852 { "numa", "", do_info_numa
,
1853 "", "show NUMA information", },
1854 { "usb", "", usb_info
,
1855 "", "show guest USB devices", },
1856 { "usbhost", "", usb_host_info
,
1857 "", "show host USB devices", },
1858 { "profile", "", do_info_profile
,
1859 "", "show profiling information", },
1860 { "capture", "", do_info_capture
,
1861 "", "show capture information" },
1862 { "snapshots", "", do_info_snapshots
,
1863 "", "show the currently saved VM snapshots" },
1864 { "status", "", do_info_status
,
1865 "", "show the current VM status (running|paused)" },
1866 { "pcmcia", "", pcmcia_info
,
1867 "", "show guest PCMCIA status" },
1868 { "mice", "", do_info_mice
,
1869 "", "show which guest mouse is receiving events" },
1870 { "vnc", "", do_info_vnc
,
1871 "", "show the vnc server status"},
1872 { "name", "", do_info_name
,
1873 "", "show the current VM name" },
1874 { "uuid", "", do_info_uuid
,
1875 "", "show the current VM UUID" },
1876 #if defined(TARGET_PPC)
1877 { "cpustats", "", do_info_cpu_stats
,
1878 "", "show CPU statistics", },
1880 #if defined(CONFIG_SLIRP)
1881 { "usernet", "", do_info_usernet
,
1882 "", "show user network stack connection states", },
1884 { "migrate", "", do_info_migrate
, "", "show migration status" },
1885 { "balloon", "", do_info_balloon
,
1886 "", "show balloon information" },
1887 { "qtree", "", do_info_qtree
,
1888 "", "show device tree" },
1889 { "qdm", "", do_info_qdm
,
1890 "", "show qdev device model list" },
1891 { "roms", "", do_info_roms
,
1896 /*******************************************************************/
1898 static const char *pch
;
1899 static jmp_buf expr_env
;
1904 typedef struct MonitorDef
{
1907 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1911 #if defined(TARGET_I386)
1912 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1914 CPUState
*env
= mon_get_cpu();
1917 return env
->eip
+ env
->segs
[R_CS
].base
;
1921 #if defined(TARGET_PPC)
1922 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1924 CPUState
*env
= mon_get_cpu();
1932 for (i
= 0; i
< 8; i
++)
1933 u
|= env
->crf
[i
] << (32 - (4 * i
));
1938 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1940 CPUState
*env
= mon_get_cpu();
1946 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1948 CPUState
*env
= mon_get_cpu();
1954 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1956 CPUState
*env
= mon_get_cpu();
1959 return cpu_ppc_load_decr(env
);
1962 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1964 CPUState
*env
= mon_get_cpu();
1967 return cpu_ppc_load_tbu(env
);
1970 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1972 CPUState
*env
= mon_get_cpu();
1975 return cpu_ppc_load_tbl(env
);
1979 #if defined(TARGET_SPARC)
1980 #ifndef TARGET_SPARC64
1981 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1983 CPUState
*env
= mon_get_cpu();
1986 return GET_PSR(env
);
1990 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1992 CPUState
*env
= mon_get_cpu();
1995 return env
->regwptr
[val
];
1999 static const MonitorDef monitor_defs
[] = {
2002 #define SEG(name, seg) \
2003 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2004 { name ".base", offsetof(CPUState, segs[seg].base) },\
2005 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2007 { "eax", offsetof(CPUState
, regs
[0]) },
2008 { "ecx", offsetof(CPUState
, regs
[1]) },
2009 { "edx", offsetof(CPUState
, regs
[2]) },
2010 { "ebx", offsetof(CPUState
, regs
[3]) },
2011 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2012 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2013 { "esi", offsetof(CPUState
, regs
[6]) },
2014 { "edi", offsetof(CPUState
, regs
[7]) },
2015 #ifdef TARGET_X86_64
2016 { "r8", offsetof(CPUState
, regs
[8]) },
2017 { "r9", offsetof(CPUState
, regs
[9]) },
2018 { "r10", offsetof(CPUState
, regs
[10]) },
2019 { "r11", offsetof(CPUState
, regs
[11]) },
2020 { "r12", offsetof(CPUState
, regs
[12]) },
2021 { "r13", offsetof(CPUState
, regs
[13]) },
2022 { "r14", offsetof(CPUState
, regs
[14]) },
2023 { "r15", offsetof(CPUState
, regs
[15]) },
2025 { "eflags", offsetof(CPUState
, eflags
) },
2026 { "eip", offsetof(CPUState
, eip
) },
2033 { "pc", 0, monitor_get_pc
, },
2034 #elif defined(TARGET_PPC)
2035 /* General purpose registers */
2036 { "r0", offsetof(CPUState
, gpr
[0]) },
2037 { "r1", offsetof(CPUState
, gpr
[1]) },
2038 { "r2", offsetof(CPUState
, gpr
[2]) },
2039 { "r3", offsetof(CPUState
, gpr
[3]) },
2040 { "r4", offsetof(CPUState
, gpr
[4]) },
2041 { "r5", offsetof(CPUState
, gpr
[5]) },
2042 { "r6", offsetof(CPUState
, gpr
[6]) },
2043 { "r7", offsetof(CPUState
, gpr
[7]) },
2044 { "r8", offsetof(CPUState
, gpr
[8]) },
2045 { "r9", offsetof(CPUState
, gpr
[9]) },
2046 { "r10", offsetof(CPUState
, gpr
[10]) },
2047 { "r11", offsetof(CPUState
, gpr
[11]) },
2048 { "r12", offsetof(CPUState
, gpr
[12]) },
2049 { "r13", offsetof(CPUState
, gpr
[13]) },
2050 { "r14", offsetof(CPUState
, gpr
[14]) },
2051 { "r15", offsetof(CPUState
, gpr
[15]) },
2052 { "r16", offsetof(CPUState
, gpr
[16]) },
2053 { "r17", offsetof(CPUState
, gpr
[17]) },
2054 { "r18", offsetof(CPUState
, gpr
[18]) },
2055 { "r19", offsetof(CPUState
, gpr
[19]) },
2056 { "r20", offsetof(CPUState
, gpr
[20]) },
2057 { "r21", offsetof(CPUState
, gpr
[21]) },
2058 { "r22", offsetof(CPUState
, gpr
[22]) },
2059 { "r23", offsetof(CPUState
, gpr
[23]) },
2060 { "r24", offsetof(CPUState
, gpr
[24]) },
2061 { "r25", offsetof(CPUState
, gpr
[25]) },
2062 { "r26", offsetof(CPUState
, gpr
[26]) },
2063 { "r27", offsetof(CPUState
, gpr
[27]) },
2064 { "r28", offsetof(CPUState
, gpr
[28]) },
2065 { "r29", offsetof(CPUState
, gpr
[29]) },
2066 { "r30", offsetof(CPUState
, gpr
[30]) },
2067 { "r31", offsetof(CPUState
, gpr
[31]) },
2068 /* Floating point registers */
2069 { "f0", offsetof(CPUState
, fpr
[0]) },
2070 { "f1", offsetof(CPUState
, fpr
[1]) },
2071 { "f2", offsetof(CPUState
, fpr
[2]) },
2072 { "f3", offsetof(CPUState
, fpr
[3]) },
2073 { "f4", offsetof(CPUState
, fpr
[4]) },
2074 { "f5", offsetof(CPUState
, fpr
[5]) },
2075 { "f6", offsetof(CPUState
, fpr
[6]) },
2076 { "f7", offsetof(CPUState
, fpr
[7]) },
2077 { "f8", offsetof(CPUState
, fpr
[8]) },
2078 { "f9", offsetof(CPUState
, fpr
[9]) },
2079 { "f10", offsetof(CPUState
, fpr
[10]) },
2080 { "f11", offsetof(CPUState
, fpr
[11]) },
2081 { "f12", offsetof(CPUState
, fpr
[12]) },
2082 { "f13", offsetof(CPUState
, fpr
[13]) },
2083 { "f14", offsetof(CPUState
, fpr
[14]) },
2084 { "f15", offsetof(CPUState
, fpr
[15]) },
2085 { "f16", offsetof(CPUState
, fpr
[16]) },
2086 { "f17", offsetof(CPUState
, fpr
[17]) },
2087 { "f18", offsetof(CPUState
, fpr
[18]) },
2088 { "f19", offsetof(CPUState
, fpr
[19]) },
2089 { "f20", offsetof(CPUState
, fpr
[20]) },
2090 { "f21", offsetof(CPUState
, fpr
[21]) },
2091 { "f22", offsetof(CPUState
, fpr
[22]) },
2092 { "f23", offsetof(CPUState
, fpr
[23]) },
2093 { "f24", offsetof(CPUState
, fpr
[24]) },
2094 { "f25", offsetof(CPUState
, fpr
[25]) },
2095 { "f26", offsetof(CPUState
, fpr
[26]) },
2096 { "f27", offsetof(CPUState
, fpr
[27]) },
2097 { "f28", offsetof(CPUState
, fpr
[28]) },
2098 { "f29", offsetof(CPUState
, fpr
[29]) },
2099 { "f30", offsetof(CPUState
, fpr
[30]) },
2100 { "f31", offsetof(CPUState
, fpr
[31]) },
2101 { "fpscr", offsetof(CPUState
, fpscr
) },
2102 /* Next instruction pointer */
2103 { "nip|pc", offsetof(CPUState
, nip
) },
2104 { "lr", offsetof(CPUState
, lr
) },
2105 { "ctr", offsetof(CPUState
, ctr
) },
2106 { "decr", 0, &monitor_get_decr
, },
2107 { "ccr", 0, &monitor_get_ccr
, },
2108 /* Machine state register */
2109 { "msr", 0, &monitor_get_msr
, },
2110 { "xer", 0, &monitor_get_xer
, },
2111 { "tbu", 0, &monitor_get_tbu
, },
2112 { "tbl", 0, &monitor_get_tbl
, },
2113 #if defined(TARGET_PPC64)
2114 /* Address space register */
2115 { "asr", offsetof(CPUState
, asr
) },
2117 /* Segment registers */
2118 { "sdr1", offsetof(CPUState
, sdr1
) },
2119 { "sr0", offsetof(CPUState
, sr
[0]) },
2120 { "sr1", offsetof(CPUState
, sr
[1]) },
2121 { "sr2", offsetof(CPUState
, sr
[2]) },
2122 { "sr3", offsetof(CPUState
, sr
[3]) },
2123 { "sr4", offsetof(CPUState
, sr
[4]) },
2124 { "sr5", offsetof(CPUState
, sr
[5]) },
2125 { "sr6", offsetof(CPUState
, sr
[6]) },
2126 { "sr7", offsetof(CPUState
, sr
[7]) },
2127 { "sr8", offsetof(CPUState
, sr
[8]) },
2128 { "sr9", offsetof(CPUState
, sr
[9]) },
2129 { "sr10", offsetof(CPUState
, sr
[10]) },
2130 { "sr11", offsetof(CPUState
, sr
[11]) },
2131 { "sr12", offsetof(CPUState
, sr
[12]) },
2132 { "sr13", offsetof(CPUState
, sr
[13]) },
2133 { "sr14", offsetof(CPUState
, sr
[14]) },
2134 { "sr15", offsetof(CPUState
, sr
[15]) },
2135 /* Too lazy to put BATs and SPRs ... */
2136 #elif defined(TARGET_SPARC)
2137 { "g0", offsetof(CPUState
, gregs
[0]) },
2138 { "g1", offsetof(CPUState
, gregs
[1]) },
2139 { "g2", offsetof(CPUState
, gregs
[2]) },
2140 { "g3", offsetof(CPUState
, gregs
[3]) },
2141 { "g4", offsetof(CPUState
, gregs
[4]) },
2142 { "g5", offsetof(CPUState
, gregs
[5]) },
2143 { "g6", offsetof(CPUState
, gregs
[6]) },
2144 { "g7", offsetof(CPUState
, gregs
[7]) },
2145 { "o0", 0, monitor_get_reg
},
2146 { "o1", 1, monitor_get_reg
},
2147 { "o2", 2, monitor_get_reg
},
2148 { "o3", 3, monitor_get_reg
},
2149 { "o4", 4, monitor_get_reg
},
2150 { "o5", 5, monitor_get_reg
},
2151 { "o6", 6, monitor_get_reg
},
2152 { "o7", 7, monitor_get_reg
},
2153 { "l0", 8, monitor_get_reg
},
2154 { "l1", 9, monitor_get_reg
},
2155 { "l2", 10, monitor_get_reg
},
2156 { "l3", 11, monitor_get_reg
},
2157 { "l4", 12, monitor_get_reg
},
2158 { "l5", 13, monitor_get_reg
},
2159 { "l6", 14, monitor_get_reg
},
2160 { "l7", 15, monitor_get_reg
},
2161 { "i0", 16, monitor_get_reg
},
2162 { "i1", 17, monitor_get_reg
},
2163 { "i2", 18, monitor_get_reg
},
2164 { "i3", 19, monitor_get_reg
},
2165 { "i4", 20, monitor_get_reg
},
2166 { "i5", 21, monitor_get_reg
},
2167 { "i6", 22, monitor_get_reg
},
2168 { "i7", 23, monitor_get_reg
},
2169 { "pc", offsetof(CPUState
, pc
) },
2170 { "npc", offsetof(CPUState
, npc
) },
2171 { "y", offsetof(CPUState
, y
) },
2172 #ifndef TARGET_SPARC64
2173 { "psr", 0, &monitor_get_psr
, },
2174 { "wim", offsetof(CPUState
, wim
) },
2176 { "tbr", offsetof(CPUState
, tbr
) },
2177 { "fsr", offsetof(CPUState
, fsr
) },
2178 { "f0", offsetof(CPUState
, fpr
[0]) },
2179 { "f1", offsetof(CPUState
, fpr
[1]) },
2180 { "f2", offsetof(CPUState
, fpr
[2]) },
2181 { "f3", offsetof(CPUState
, fpr
[3]) },
2182 { "f4", offsetof(CPUState
, fpr
[4]) },
2183 { "f5", offsetof(CPUState
, fpr
[5]) },
2184 { "f6", offsetof(CPUState
, fpr
[6]) },
2185 { "f7", offsetof(CPUState
, fpr
[7]) },
2186 { "f8", offsetof(CPUState
, fpr
[8]) },
2187 { "f9", offsetof(CPUState
, fpr
[9]) },
2188 { "f10", offsetof(CPUState
, fpr
[10]) },
2189 { "f11", offsetof(CPUState
, fpr
[11]) },
2190 { "f12", offsetof(CPUState
, fpr
[12]) },
2191 { "f13", offsetof(CPUState
, fpr
[13]) },
2192 { "f14", offsetof(CPUState
, fpr
[14]) },
2193 { "f15", offsetof(CPUState
, fpr
[15]) },
2194 { "f16", offsetof(CPUState
, fpr
[16]) },
2195 { "f17", offsetof(CPUState
, fpr
[17]) },
2196 { "f18", offsetof(CPUState
, fpr
[18]) },
2197 { "f19", offsetof(CPUState
, fpr
[19]) },
2198 { "f20", offsetof(CPUState
, fpr
[20]) },
2199 { "f21", offsetof(CPUState
, fpr
[21]) },
2200 { "f22", offsetof(CPUState
, fpr
[22]) },
2201 { "f23", offsetof(CPUState
, fpr
[23]) },
2202 { "f24", offsetof(CPUState
, fpr
[24]) },
2203 { "f25", offsetof(CPUState
, fpr
[25]) },
2204 { "f26", offsetof(CPUState
, fpr
[26]) },
2205 { "f27", offsetof(CPUState
, fpr
[27]) },
2206 { "f28", offsetof(CPUState
, fpr
[28]) },
2207 { "f29", offsetof(CPUState
, fpr
[29]) },
2208 { "f30", offsetof(CPUState
, fpr
[30]) },
2209 { "f31", offsetof(CPUState
, fpr
[31]) },
2210 #ifdef TARGET_SPARC64
2211 { "f32", offsetof(CPUState
, fpr
[32]) },
2212 { "f34", offsetof(CPUState
, fpr
[34]) },
2213 { "f36", offsetof(CPUState
, fpr
[36]) },
2214 { "f38", offsetof(CPUState
, fpr
[38]) },
2215 { "f40", offsetof(CPUState
, fpr
[40]) },
2216 { "f42", offsetof(CPUState
, fpr
[42]) },
2217 { "f44", offsetof(CPUState
, fpr
[44]) },
2218 { "f46", offsetof(CPUState
, fpr
[46]) },
2219 { "f48", offsetof(CPUState
, fpr
[48]) },
2220 { "f50", offsetof(CPUState
, fpr
[50]) },
2221 { "f52", offsetof(CPUState
, fpr
[52]) },
2222 { "f54", offsetof(CPUState
, fpr
[54]) },
2223 { "f56", offsetof(CPUState
, fpr
[56]) },
2224 { "f58", offsetof(CPUState
, fpr
[58]) },
2225 { "f60", offsetof(CPUState
, fpr
[60]) },
2226 { "f62", offsetof(CPUState
, fpr
[62]) },
2227 { "asi", offsetof(CPUState
, asi
) },
2228 { "pstate", offsetof(CPUState
, pstate
) },
2229 { "cansave", offsetof(CPUState
, cansave
) },
2230 { "canrestore", offsetof(CPUState
, canrestore
) },
2231 { "otherwin", offsetof(CPUState
, otherwin
) },
2232 { "wstate", offsetof(CPUState
, wstate
) },
2233 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2234 { "fprs", offsetof(CPUState
, fprs
) },
2240 static void expr_error(Monitor
*mon
, const char *msg
)
2242 monitor_printf(mon
, "%s\n", msg
);
2243 longjmp(expr_env
, 1);
2246 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2247 static int get_monitor_def(target_long
*pval
, const char *name
)
2249 const MonitorDef
*md
;
2252 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2253 if (compare_cmd(name
, md
->name
)) {
2254 if (md
->get_value
) {
2255 *pval
= md
->get_value(md
, md
->offset
);
2257 CPUState
*env
= mon_get_cpu();
2260 ptr
= (uint8_t *)env
+ md
->offset
;
2263 *pval
= *(int32_t *)ptr
;
2266 *pval
= *(target_long
*)ptr
;
2279 static void next(void)
2283 while (qemu_isspace(*pch
))
2288 static int64_t expr_sum(Monitor
*mon
);
2290 static int64_t expr_unary(Monitor
*mon
)
2299 n
= expr_unary(mon
);
2303 n
= -expr_unary(mon
);
2307 n
= ~expr_unary(mon
);
2313 expr_error(mon
, "')' expected");
2320 expr_error(mon
, "character constant expected");
2324 expr_error(mon
, "missing terminating \' character");
2334 while ((*pch
>= 'a' && *pch
<= 'z') ||
2335 (*pch
>= 'A' && *pch
<= 'Z') ||
2336 (*pch
>= '0' && *pch
<= '9') ||
2337 *pch
== '_' || *pch
== '.') {
2338 if ((q
- buf
) < sizeof(buf
) - 1)
2342 while (qemu_isspace(*pch
))
2345 ret
= get_monitor_def(®
, buf
);
2347 expr_error(mon
, "unknown register");
2349 expr_error(mon
, "no cpu defined");
2354 expr_error(mon
, "unexpected end of expression");
2358 #if TARGET_PHYS_ADDR_BITS > 32
2359 n
= strtoull(pch
, &p
, 0);
2361 n
= strtoul(pch
, &p
, 0);
2364 expr_error(mon
, "invalid char in expression");
2367 while (qemu_isspace(*pch
))
2375 static int64_t expr_prod(Monitor
*mon
)
2380 val
= expr_unary(mon
);
2383 if (op
!= '*' && op
!= '/' && op
!= '%')
2386 val2
= expr_unary(mon
);
2395 expr_error(mon
, "division by zero");
2406 static int64_t expr_logic(Monitor
*mon
)
2411 val
= expr_prod(mon
);
2414 if (op
!= '&' && op
!= '|' && op
!= '^')
2417 val2
= expr_prod(mon
);
2434 static int64_t expr_sum(Monitor
*mon
)
2439 val
= expr_logic(mon
);
2442 if (op
!= '+' && op
!= '-')
2445 val2
= expr_logic(mon
);
2454 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2457 if (setjmp(expr_env
)) {
2461 while (qemu_isspace(*pch
))
2463 *pval
= expr_sum(mon
);
2468 static int get_str(char *buf
, int buf_size
, const char **pp
)
2476 while (qemu_isspace(*p
))
2486 while (*p
!= '\0' && *p
!= '\"') {
2502 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2505 if ((q
- buf
) < buf_size
- 1) {
2509 if ((q
- buf
) < buf_size
- 1) {
2516 qemu_printf("unterminated string\n");
2521 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2522 if ((q
- buf
) < buf_size
- 1) {
2534 * Store the command-name in cmdname, and return a pointer to
2535 * the remaining of the command string.
2537 static const char *get_command_name(const char *cmdline
,
2538 char *cmdname
, size_t nlen
)
2541 const char *p
, *pstart
;
2544 while (qemu_isspace(*p
))
2549 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2554 memcpy(cmdname
, pstart
, len
);
2555 cmdname
[len
] = '\0';
2560 * Read key of 'type' into 'key' and return the current
2563 static char *key_get_info(const char *type
, char **key
)
2571 p
= strchr(type
, ':');
2578 str
= qemu_malloc(len
+ 1);
2579 memcpy(str
, type
, len
);
2586 static int default_fmt_format
= 'x';
2587 static int default_fmt_size
= 4;
2591 static const mon_cmd_t
*monitor_parse_command(Monitor
*mon
,
2592 const char *cmdline
,
2595 const char *p
, *typestr
;
2597 const mon_cmd_t
*cmd
;
2603 monitor_printf(mon
, "command='%s'\n", cmdline
);
2606 /* extract the command name */
2607 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2611 /* find the command */
2612 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2613 if (compare_cmd(cmdname
, cmd
->name
))
2617 if (cmd
->name
== NULL
) {
2618 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2622 /* parse the parameters */
2623 typestr
= cmd
->args_type
;
2625 typestr
= key_get_info(typestr
, &key
);
2637 while (qemu_isspace(*p
))
2639 if (*typestr
== '?') {
2642 /* no optional string: NULL argument */
2646 ret
= get_str(buf
, sizeof(buf
), &p
);
2650 monitor_printf(mon
, "%s: filename expected\n",
2654 monitor_printf(mon
, "%s: block device name expected\n",
2658 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2663 qdict_put(qdict
, key
, qstring_from_str(buf
));
2668 int count
, format
, size
;
2670 while (qemu_isspace(*p
))
2676 if (qemu_isdigit(*p
)) {
2678 while (qemu_isdigit(*p
)) {
2679 count
= count
* 10 + (*p
- '0');
2717 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2718 monitor_printf(mon
, "invalid char in format: '%c'\n",
2723 format
= default_fmt_format
;
2724 if (format
!= 'i') {
2725 /* for 'i', not specifying a size gives -1 as size */
2727 size
= default_fmt_size
;
2728 default_fmt_size
= size
;
2730 default_fmt_format
= format
;
2733 format
= default_fmt_format
;
2734 if (format
!= 'i') {
2735 size
= default_fmt_size
;
2740 qdict_put(qdict
, "count", qint_from_int(count
));
2741 qdict_put(qdict
, "format", qint_from_int(format
));
2742 qdict_put(qdict
, "size", qint_from_int(size
));
2750 while (qemu_isspace(*p
))
2752 if (*typestr
== '?' || *typestr
== '.') {
2753 if (*typestr
== '?') {
2761 while (qemu_isspace(*p
))
2770 if (get_expr(mon
, &val
, &p
))
2772 /* Check if 'i' is greater than 32-bit */
2773 if ((c
== 'i') && ((val
>> 32) & 0xffffffff)) {
2774 monitor_printf(mon
, "\'%s\' has failed: ", cmdname
);
2775 monitor_printf(mon
, "integer is for 32-bit values\n");
2778 qdict_put(qdict
, key
, qint_from_int(val
));
2789 while (qemu_isspace(*p
))
2795 monitor_printf(mon
, "%s: unsupported option -%c\n",
2802 qdict_put(qdict
, key
, qint_from_int(has_option
));
2807 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2813 /* check that all arguments were parsed */
2814 while (qemu_isspace(*p
))
2817 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2829 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2832 const mon_cmd_t
*cmd
;
2834 qdict
= qdict_new();
2836 cmd
= monitor_parse_command(mon
, cmdline
, qdict
);
2838 void (*handler
)(Monitor
*mon
, const QDict
*qdict
);
2840 qemu_errors_to_mon(mon
);
2842 handler
= cmd
->handler
;
2843 handler(mon
, qdict
);
2845 qemu_errors_to_previous();
2851 static void cmd_completion(const char *name
, const char *list
)
2853 const char *p
, *pstart
;
2862 p
= pstart
+ strlen(pstart
);
2864 if (len
> sizeof(cmd
) - 2)
2865 len
= sizeof(cmd
) - 2;
2866 memcpy(cmd
, pstart
, len
);
2868 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2869 readline_add_completion(cur_mon
->rs
, cmd
);
2877 static void file_completion(const char *input
)
2882 char file
[1024], file_prefix
[1024];
2886 p
= strrchr(input
, '/');
2889 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2890 pstrcpy(path
, sizeof(path
), ".");
2892 input_path_len
= p
- input
+ 1;
2893 memcpy(path
, input
, input_path_len
);
2894 if (input_path_len
> sizeof(path
) - 1)
2895 input_path_len
= sizeof(path
) - 1;
2896 path
[input_path_len
] = '\0';
2897 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2899 #ifdef DEBUG_COMPLETION
2900 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2901 input
, path
, file_prefix
);
2903 ffs
= opendir(path
);
2911 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2912 memcpy(file
, input
, input_path_len
);
2913 if (input_path_len
< sizeof(file
))
2914 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2916 /* stat the file to find out if it's a directory.
2917 * In that case add a slash to speed up typing long paths
2920 if(S_ISDIR(sb
.st_mode
))
2921 pstrcat(file
, sizeof(file
), "/");
2922 readline_add_completion(cur_mon
->rs
, file
);
2928 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2930 const char *name
= bdrv_get_device_name(bs
);
2931 const char *input
= opaque
;
2933 if (input
[0] == '\0' ||
2934 !strncmp(name
, (char *)input
, strlen(input
))) {
2935 readline_add_completion(cur_mon
->rs
, name
);
2939 /* NOTE: this parser is an approximate form of the real command parser */
2940 static void parse_cmdline(const char *cmdline
,
2941 int *pnb_args
, char **args
)
2950 while (qemu_isspace(*p
))
2954 if (nb_args
>= MAX_ARGS
)
2956 ret
= get_str(buf
, sizeof(buf
), &p
);
2957 args
[nb_args
] = qemu_strdup(buf
);
2962 *pnb_args
= nb_args
;
2965 static const char *next_arg_type(const char *typestr
)
2967 const char *p
= strchr(typestr
, ':');
2968 return (p
!= NULL
? ++p
: typestr
);
2971 static void monitor_find_completion(const char *cmdline
)
2973 const char *cmdname
;
2974 char *args
[MAX_ARGS
];
2975 int nb_args
, i
, len
;
2976 const char *ptype
, *str
;
2977 const mon_cmd_t
*cmd
;
2980 parse_cmdline(cmdline
, &nb_args
, args
);
2981 #ifdef DEBUG_COMPLETION
2982 for(i
= 0; i
< nb_args
; i
++) {
2983 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
2987 /* if the line ends with a space, it means we want to complete the
2989 len
= strlen(cmdline
);
2990 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
2991 if (nb_args
>= MAX_ARGS
)
2993 args
[nb_args
++] = qemu_strdup("");
2996 /* command completion */
3001 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3002 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3003 cmd_completion(cmdname
, cmd
->name
);
3006 /* find the command */
3007 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3008 if (compare_cmd(args
[0], cmd
->name
))
3013 ptype
= next_arg_type(cmd
->args_type
);
3014 for(i
= 0; i
< nb_args
- 2; i
++) {
3015 if (*ptype
!= '\0') {
3016 ptype
= next_arg_type(ptype
);
3017 while (*ptype
== '?')
3018 ptype
= next_arg_type(ptype
);
3021 str
= args
[nb_args
- 1];
3022 if (*ptype
== '-' && ptype
[1] != '\0') {
3027 /* file completion */
3028 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3029 file_completion(str
);
3032 /* block device name completion */
3033 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3034 bdrv_iterate(block_completion_it
, (void *)str
);
3037 /* XXX: more generic ? */
3038 if (!strcmp(cmd
->name
, "info")) {
3039 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3040 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3041 cmd_completion(str
, cmd
->name
);
3043 } else if (!strcmp(cmd
->name
, "sendkey")) {
3044 char *sep
= strrchr(str
, '-');
3047 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3048 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3049 cmd_completion(str
, key
->name
);
3051 } else if (!strcmp(cmd
->name
, "help|?")) {
3052 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3053 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3054 cmd_completion(str
, cmd
->name
);
3062 for(i
= 0; i
< nb_args
; i
++)
3066 static int monitor_can_read(void *opaque
)
3068 Monitor
*mon
= opaque
;
3070 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3073 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3075 Monitor
*old_mon
= cur_mon
;
3081 for (i
= 0; i
< size
; i
++)
3082 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3084 if (size
== 0 || buf
[size
- 1] != 0)
3085 monitor_printf(cur_mon
, "corrupted command\n");
3087 monitor_handle_command(cur_mon
, (char *)buf
);
3093 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3095 monitor_suspend(mon
);
3096 monitor_handle_command(mon
, cmdline
);
3097 monitor_resume(mon
);
3100 int monitor_suspend(Monitor
*mon
)
3108 void monitor_resume(Monitor
*mon
)
3112 if (--mon
->suspend_cnt
== 0)
3113 readline_show_prompt(mon
->rs
);
3116 static void monitor_event(void *opaque
, int event
)
3118 Monitor
*mon
= opaque
;
3121 case CHR_EVENT_MUX_IN
:
3123 if (mon
->reset_seen
) {
3124 readline_restart(mon
->rs
);
3125 monitor_resume(mon
);
3128 mon
->suspend_cnt
= 0;
3132 case CHR_EVENT_MUX_OUT
:
3133 if (mon
->reset_seen
) {
3134 if (mon
->suspend_cnt
== 0) {
3135 monitor_printf(mon
, "\n");
3138 monitor_suspend(mon
);
3145 case CHR_EVENT_RESET
:
3146 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3147 "information\n", QEMU_VERSION
);
3148 if (!mon
->mux_out
) {
3149 readline_show_prompt(mon
->rs
);
3151 mon
->reset_seen
= 1;
3165 void monitor_init(CharDriverState
*chr
, int flags
)
3167 static int is_first_init
= 1;
3170 if (is_first_init
) {
3171 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3175 mon
= qemu_mallocz(sizeof(*mon
));
3179 if (flags
& MONITOR_USE_READLINE
) {
3180 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3181 monitor_read_command(mon
, 0);
3184 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3187 QLIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3188 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3192 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3194 BlockDriverState
*bs
= opaque
;
3197 if (bdrv_set_key(bs
, password
) != 0) {
3198 monitor_printf(mon
, "invalid password\n");
3201 if (mon
->password_completion_cb
)
3202 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3204 monitor_read_command(mon
, 1);
3207 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3208 BlockDriverCompletionFunc
*completion_cb
,
3213 if (!bdrv_key_required(bs
)) {
3215 completion_cb(opaque
, 0);
3219 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3220 bdrv_get_encrypted_filename(bs
));
3222 mon
->password_completion_cb
= completion_cb
;
3223 mon
->password_opaque
= opaque
;
3225 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3227 if (err
&& completion_cb
)
3228 completion_cb(opaque
, err
);
3231 typedef struct QemuErrorSink QemuErrorSink
;
3232 struct QemuErrorSink
{
3241 QemuErrorSink
*previous
;
3244 static QemuErrorSink
*qemu_error_sink
;
3246 void qemu_errors_to_file(FILE *fp
)
3248 QemuErrorSink
*sink
;
3250 sink
= qemu_mallocz(sizeof(*sink
));
3251 sink
->dest
= ERR_SINK_FILE
;
3253 sink
->previous
= qemu_error_sink
;
3254 qemu_error_sink
= sink
;
3257 void qemu_errors_to_mon(Monitor
*mon
)
3259 QemuErrorSink
*sink
;
3261 sink
= qemu_mallocz(sizeof(*sink
));
3262 sink
->dest
= ERR_SINK_MONITOR
;
3264 sink
->previous
= qemu_error_sink
;
3265 qemu_error_sink
= sink
;
3268 void qemu_errors_to_previous(void)
3270 QemuErrorSink
*sink
;
3272 assert(qemu_error_sink
!= NULL
);
3273 sink
= qemu_error_sink
;
3274 qemu_error_sink
= sink
->previous
;
3278 void qemu_error(const char *fmt
, ...)
3282 assert(qemu_error_sink
!= NULL
);
3283 switch (qemu_error_sink
->dest
) {
3285 va_start(args
, fmt
);
3286 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3289 case ERR_SINK_MONITOR
:
3290 va_start(args
, fmt
);
3291 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);