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"
34 #include "qemu-char.h"
40 #include "audio/audio.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
52 //#define DEBUG_COMPLETION
58 * 'B' block device name
59 * 's' string (accept optional quote)
61 * 'l' target long (32 or 64 bit)
62 * '/' optional gdb-like print format (like "/10x")
64 * '?' optional type (for 'F', 's' and 'i')
68 typedef struct mon_cmd_t
{
70 const char *args_type
;
76 /* file descriptors passed via SCM_RIGHTS */
77 typedef struct mon_fd_t mon_fd_t
;
81 LIST_ENTRY(mon_fd_t
) next
;
92 BlockDriverCompletionFunc
*password_completion_cb
;
93 void *password_opaque
;
94 LIST_HEAD(,mon_fd_t
) fds
;
95 LIST_ENTRY(Monitor
) entry
;
98 static LIST_HEAD(mon_list
, Monitor
) mon_list
;
100 static const mon_cmd_t mon_cmds
[];
101 static const mon_cmd_t info_cmds
[];
103 Monitor
*cur_mon
= NULL
;
105 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
,
108 static void monitor_read_command(Monitor
*mon
, int show_prompt
)
110 readline_start(mon
->rs
, "(qemu) ", 0, monitor_command_cb
, NULL
);
112 readline_show_prompt(mon
->rs
);
115 static int monitor_read_password(Monitor
*mon
, ReadLineFunc
*readline_func
,
119 readline_start(mon
->rs
, "Password: ", 1, readline_func
, opaque
);
120 /* prompt is printed on return from the command handler */
123 monitor_printf(mon
, "terminal does not support password prompting\n");
128 void monitor_flush(Monitor
*mon
)
130 if (mon
&& mon
->outbuf_index
!= 0 && mon
->chr
->focus
== 0) {
131 qemu_chr_write(mon
->chr
, mon
->outbuf
, mon
->outbuf_index
);
132 mon
->outbuf_index
= 0;
136 /* flush at every end of line or if the buffer is full */
137 static void monitor_puts(Monitor
*mon
, const char *str
)
149 mon
->outbuf
[mon
->outbuf_index
++] = '\r';
150 mon
->outbuf
[mon
->outbuf_index
++] = c
;
151 if (mon
->outbuf_index
>= (sizeof(mon
->outbuf
) - 1)
157 void monitor_vprintf(Monitor
*mon
, const char *fmt
, va_list ap
)
160 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
161 monitor_puts(mon
, buf
);
164 void monitor_printf(Monitor
*mon
, const char *fmt
, ...)
168 monitor_vprintf(mon
, fmt
, ap
);
172 void monitor_print_filename(Monitor
*mon
, const char *filename
)
176 for (i
= 0; filename
[i
]; i
++) {
177 switch (filename
[i
]) {
181 monitor_printf(mon
, "\\%c", filename
[i
]);
184 monitor_printf(mon
, "\\t");
187 monitor_printf(mon
, "\\r");
190 monitor_printf(mon
, "\\n");
193 monitor_printf(mon
, "%c", filename
[i
]);
199 static int monitor_fprintf(FILE *stream
, const char *fmt
, ...)
203 monitor_vprintf((Monitor
*)stream
, fmt
, ap
);
208 static int compare_cmd(const char *name
, const char *list
)
210 const char *p
, *pstart
;
218 p
= pstart
+ strlen(pstart
);
219 if ((p
- pstart
) == len
&& !memcmp(pstart
, name
, len
))
228 static void help_cmd_dump(Monitor
*mon
, const mon_cmd_t
*cmds
,
229 const char *prefix
, const char *name
)
231 const mon_cmd_t
*cmd
;
233 for(cmd
= cmds
; cmd
->name
!= NULL
; cmd
++) {
234 if (!name
|| !strcmp(name
, cmd
->name
))
235 monitor_printf(mon
, "%s%s %s -- %s\n", prefix
, cmd
->name
,
236 cmd
->params
, cmd
->help
);
240 static void help_cmd(Monitor
*mon
, const char *name
)
242 if (name
&& !strcmp(name
, "info")) {
243 help_cmd_dump(mon
, info_cmds
, "info ", NULL
);
245 help_cmd_dump(mon
, mon_cmds
, "", name
);
246 if (name
&& !strcmp(name
, "log")) {
247 const CPULogItem
*item
;
248 monitor_printf(mon
, "Log items (comma separated):\n");
249 monitor_printf(mon
, "%-10s %s\n", "none", "remove all logs");
250 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
251 monitor_printf(mon
, "%-10s %s\n", item
->name
, item
->help
);
257 static void do_help_cmd(Monitor
*mon
, const QDict
*qdict
)
259 help_cmd(mon
, qdict_get_try_str(qdict
, "name"));
262 static void do_commit(Monitor
*mon
, const QDict
*qdict
)
266 const char *device
= qdict_get_str(qdict
, "device");
268 all_devices
= !strcmp(device
, "all");
269 TAILQ_FOREACH(dinfo
, &drives
, next
) {
271 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
273 bdrv_commit(dinfo
->bdrv
);
277 static void do_info(Monitor
*mon
, const QDict
*qdict
)
279 const mon_cmd_t
*cmd
;
280 const char *item
= qdict_get_try_str(qdict
, "item");
281 void (*handler
)(Monitor
*);
285 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
286 if (compare_cmd(item
, cmd
->name
))
290 help_cmd(mon
, "info");
293 handler
= cmd
->handler
;
297 static void do_info_version(Monitor
*mon
)
299 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
302 static void do_info_name(Monitor
*mon
)
305 monitor_printf(mon
, "%s\n", qemu_name
);
308 #if defined(TARGET_I386)
309 static void do_info_hpet(Monitor
*mon
)
311 monitor_printf(mon
, "HPET is %s by QEMU\n",
312 (no_hpet
) ? "disabled" : "enabled");
316 static void do_info_uuid(Monitor
*mon
)
318 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
319 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
320 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
321 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
322 qemu_uuid
[14], qemu_uuid
[15]);
325 /* get the current CPU defined by the user */
326 static int mon_set_cpu(int cpu_index
)
330 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
331 if (env
->cpu_index
== cpu_index
) {
332 cur_mon
->mon_cpu
= env
;
339 static CPUState
*mon_get_cpu(void)
341 if (!cur_mon
->mon_cpu
) {
344 cpu_synchronize_state(cur_mon
->mon_cpu
);
345 return cur_mon
->mon_cpu
;
348 static void do_info_registers(Monitor
*mon
)
355 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
358 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
363 static void do_info_cpus(Monitor
*mon
)
367 /* just to set the default cpu if not already done */
370 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
371 cpu_synchronize_state(env
);
372 monitor_printf(mon
, "%c CPU #%d:",
373 (env
== mon
->mon_cpu
) ? '*' : ' ',
375 #if defined(TARGET_I386)
376 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
377 env
->eip
+ env
->segs
[R_CS
].base
);
378 #elif defined(TARGET_PPC)
379 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
380 #elif defined(TARGET_SPARC)
381 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
383 #elif defined(TARGET_MIPS)
384 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
387 monitor_printf(mon
, " (halted)");
388 monitor_printf(mon
, "\n");
392 static void do_cpu_set(Monitor
*mon
, const QDict
*qdict
)
394 int index
= qdict_get_int(qdict
, "index");
395 if (mon_set_cpu(index
) < 0)
396 monitor_printf(mon
, "Invalid CPU index\n");
399 static void do_info_jit(Monitor
*mon
)
401 dump_exec_info((FILE *)mon
, monitor_fprintf
);
404 static void do_info_history(Monitor
*mon
)
413 str
= readline_get_history(mon
->rs
, i
);
416 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
421 #if defined(TARGET_PPC)
422 /* XXX: not implemented in other targets */
423 static void do_info_cpu_stats(Monitor
*mon
)
428 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
432 static void do_quit(Monitor
*mon
, const QDict
*qdict
)
437 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
439 if (bdrv_is_inserted(bs
)) {
441 if (!bdrv_is_removable(bs
)) {
442 monitor_printf(mon
, "device is not removable\n");
445 if (bdrv_is_locked(bs
)) {
446 monitor_printf(mon
, "device is locked\n");
455 static void do_eject(Monitor
*mon
, const QDict
*qdict
)
457 BlockDriverState
*bs
;
458 int force
= qdict_get_int(qdict
, "force");
459 const char *filename
= qdict_get_str(qdict
, "filename");
461 bs
= bdrv_find(filename
);
463 monitor_printf(mon
, "device not found\n");
466 eject_device(mon
, bs
, force
);
469 static void do_change_block(Monitor
*mon
, const char *device
,
470 const char *filename
, const char *fmt
)
472 BlockDriverState
*bs
;
473 BlockDriver
*drv
= NULL
;
475 bs
= bdrv_find(device
);
477 monitor_printf(mon
, "device not found\n");
481 drv
= bdrv_find_format(fmt
);
483 monitor_printf(mon
, "invalid format %s\n", fmt
);
487 if (eject_device(mon
, bs
, 0) < 0)
489 bdrv_open2(bs
, filename
, 0, drv
);
490 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
493 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
496 if (vnc_display_password(NULL
, password
) < 0)
497 monitor_printf(mon
, "could not set VNC server password\n");
499 monitor_read_command(mon
, 1);
502 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
504 if (strcmp(target
, "passwd") == 0 ||
505 strcmp(target
, "password") == 0) {
508 strncpy(password
, arg
, sizeof(password
));
509 password
[sizeof(password
) - 1] = '\0';
510 change_vnc_password_cb(mon
, password
, NULL
);
512 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
515 if (vnc_display_open(NULL
, target
) < 0)
516 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
520 static void do_change(Monitor
*mon
, const QDict
*qdict
)
522 const char *device
= qdict_get_str(qdict
, "device");
523 const char *target
= qdict_get_str(qdict
, "target");
524 const char *arg
= qdict_get_try_str(qdict
, "arg");
525 if (strcmp(device
, "vnc") == 0) {
526 do_change_vnc(mon
, target
, arg
);
528 do_change_block(mon
, device
, target
, arg
);
532 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
534 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
537 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
539 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
542 static void do_log(Monitor
*mon
, const QDict
*qdict
)
545 const char *items
= qdict_get_str(qdict
, "items");
547 if (!strcmp(items
, "none")) {
550 mask
= cpu_str_to_log_mask(items
);
552 help_cmd(mon
, "log");
559 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
561 const char *option
= qdict_get_try_str(qdict
, "option");
562 if (!option
|| !strcmp(option
, "on")) {
564 } else if (!strcmp(option
, "off")) {
567 monitor_printf(mon
, "unexpected option %s\n", option
);
571 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
573 vm_stop(EXCP_INTERRUPT
);
576 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
578 struct bdrv_iterate_context
{
583 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
585 struct bdrv_iterate_context context
= { mon
, 0 };
587 bdrv_iterate(encrypted_bdrv_it
, &context
);
588 /* only resume the vm if all keys are set and valid */
593 static void bdrv_key_cb(void *opaque
, int err
)
595 Monitor
*mon
= opaque
;
597 /* another key was set successfully, retry to continue */
602 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
604 struct bdrv_iterate_context
*context
= opaque
;
606 if (!context
->err
&& bdrv_key_required(bs
)) {
607 context
->err
= -EBUSY
;
608 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
613 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
615 const char *device
= qdict_get_try_str(qdict
, "device");
617 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
618 if (gdbserver_start(device
) < 0) {
619 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
621 } else if (strcmp(device
, "none") == 0) {
622 monitor_printf(mon
, "Disabled gdbserver\n");
624 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
629 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
631 const char *action
= qdict_get_str(qdict
, "action");
632 if (select_watchdog_action(action
) == -1) {
633 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
637 static void monitor_printc(Monitor
*mon
, int c
)
639 monitor_printf(mon
, "'");
642 monitor_printf(mon
, "\\'");
645 monitor_printf(mon
, "\\\\");
648 monitor_printf(mon
, "\\n");
651 monitor_printf(mon
, "\\r");
654 if (c
>= 32 && c
<= 126) {
655 monitor_printf(mon
, "%c", c
);
657 monitor_printf(mon
, "\\x%02x", c
);
661 monitor_printf(mon
, "'");
664 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
665 target_phys_addr_t addr
, int is_physical
)
668 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
676 if (!env
&& !is_physical
)
681 } else if (wsize
== 4) {
684 /* as default we use the current CS size */
688 if ((env
->efer
& MSR_EFER_LMA
) &&
689 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
693 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
698 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
707 nb_per_line
= line_size
/ wsize
;
712 max_digits
= (wsize
* 8 + 2) / 3;
716 max_digits
= (wsize
* 8) / 4;
720 max_digits
= (wsize
* 8 * 10 + 32) / 33;
729 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
731 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
736 cpu_physical_memory_rw(addr
, buf
, l
, 0);
741 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
742 monitor_printf(mon
, " Cannot access memory\n");
751 v
= ldub_raw(buf
+ i
);
754 v
= lduw_raw(buf
+ i
);
757 v
= (uint32_t)ldl_raw(buf
+ i
);
760 v
= ldq_raw(buf
+ i
);
763 monitor_printf(mon
, " ");
766 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
769 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
772 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
775 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
778 monitor_printc(mon
, v
);
783 monitor_printf(mon
, "\n");
789 #if TARGET_LONG_BITS == 64
790 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
792 #define GET_TLONG(h, l) (l)
795 static void do_memory_dump(Monitor
*mon
, const QDict
*qdict
)
797 int count
= qdict_get_int(qdict
, "count");
798 int format
= qdict_get_int(qdict
, "format");
799 int size
= qdict_get_int(qdict
, "size");
800 target_long addr
= qdict_get_int(qdict
, "addr");
802 memory_dump(mon
, count
, format
, size
, addr
, 0);
805 #if TARGET_PHYS_ADDR_BITS > 32
806 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
808 #define GET_TPHYSADDR(h, l) (l)
811 static void do_physical_memory_dump(Monitor
*mon
, const QDict
*qdict
)
813 int count
= qdict_get_int(qdict
, "count");
814 int format
= qdict_get_int(qdict
, "format");
815 int size
= qdict_get_int(qdict
, "size");
816 target_phys_addr_t addr
= qdict_get_int(qdict
, "addr");
818 memory_dump(mon
, count
, format
, size
, addr
, 1);
821 static void do_print(Monitor
*mon
, const QDict
*qdict
)
823 int format
= qdict_get_int(qdict
, "format");
824 target_phys_addr_t val
= qdict_get_int(qdict
, "val");
826 #if TARGET_PHYS_ADDR_BITS == 32
829 monitor_printf(mon
, "%#o", val
);
832 monitor_printf(mon
, "%#x", val
);
835 monitor_printf(mon
, "%u", val
);
839 monitor_printf(mon
, "%d", val
);
842 monitor_printc(mon
, val
);
848 monitor_printf(mon
, "%#" PRIo64
, val
);
851 monitor_printf(mon
, "%#" PRIx64
, val
);
854 monitor_printf(mon
, "%" PRIu64
, val
);
858 monitor_printf(mon
, "%" PRId64
, val
);
861 monitor_printc(mon
, val
);
865 monitor_printf(mon
, "\n");
868 static void do_memory_save(Monitor
*mon
, const QDict
*qdict
)
871 uint32_t size
= qdict_get_int(qdict
, "size");
872 const char *filename
= qdict_get_str(qdict
, "filename");
873 target_long addr
= qdict_get_int(qdict
, "val");
882 f
= fopen(filename
, "wb");
884 monitor_printf(mon
, "could not open '%s'\n", filename
);
891 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
892 fwrite(buf
, 1, l
, f
);
899 static void do_physical_memory_save(Monitor
*mon
, const QDict
*qdict
)
904 uint32_t size
= qdict_get_int(qdict
, "size");
905 const char *filename
= qdict_get_str(qdict
, "filename");
906 target_phys_addr_t addr
= qdict_get_int(qdict
, "val");
908 f
= fopen(filename
, "wb");
910 monitor_printf(mon
, "could not open '%s'\n", filename
);
917 cpu_physical_memory_rw(addr
, buf
, l
, 0);
918 fwrite(buf
, 1, l
, f
);
926 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
931 uint32_t start
= qdict_get_int(qdict
, "start");
932 uint32_t size
= qdict_get_int(qdict
, "size");
935 for(addr
= start
; addr
< (start
+ size
); addr
++) {
936 cpu_physical_memory_rw(addr
, buf
, 1, 0);
937 /* BSD sum algorithm ('sum' Unix command) */
938 sum
= (sum
>> 1) | (sum
<< 15);
941 monitor_printf(mon
, "%05d\n", sum
);
949 static const KeyDef key_defs
[] = {
976 { 0x0e, "backspace" },
1013 { 0x37, "asterisk" },
1016 { 0x3a, "caps_lock" },
1027 { 0x45, "num_lock" },
1028 { 0x46, "scroll_lock" },
1030 { 0xb5, "kp_divide" },
1031 { 0x37, "kp_multiply" },
1032 { 0x4a, "kp_subtract" },
1034 { 0x9c, "kp_enter" },
1035 { 0x53, "kp_decimal" },
1068 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1083 { 0xfe, "compose" },
1088 static int get_keycode(const char *key
)
1094 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1095 if (!strcmp(key
, p
->name
))
1098 if (strstart(key
, "0x", NULL
)) {
1099 ret
= strtoul(key
, &endp
, 0);
1100 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1106 #define MAX_KEYCODES 16
1107 static uint8_t keycodes
[MAX_KEYCODES
];
1108 static int nb_pending_keycodes
;
1109 static QEMUTimer
*key_timer
;
1111 static void release_keys(void *opaque
)
1115 while (nb_pending_keycodes
> 0) {
1116 nb_pending_keycodes
--;
1117 keycode
= keycodes
[nb_pending_keycodes
];
1119 kbd_put_keycode(0xe0);
1120 kbd_put_keycode(keycode
| 0x80);
1124 static void do_sendkey(Monitor
*mon
, const QDict
*qdict
)
1126 char keyname_buf
[16];
1128 int keyname_len
, keycode
, i
;
1129 const char *string
= qdict_get_str(qdict
, "string");
1130 int has_hold_time
= qdict_haskey(qdict
, "hold_time");
1131 int hold_time
= qdict_get_try_int(qdict
, "hold_time", -1);
1133 if (nb_pending_keycodes
> 0) {
1134 qemu_del_timer(key_timer
);
1141 separator
= strchr(string
, '-');
1142 keyname_len
= separator
? separator
- string
: strlen(string
);
1143 if (keyname_len
> 0) {
1144 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1145 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1146 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1149 if (i
== MAX_KEYCODES
) {
1150 monitor_printf(mon
, "too many keys\n");
1153 keyname_buf
[keyname_len
] = 0;
1154 keycode
= get_keycode(keyname_buf
);
1156 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1159 keycodes
[i
++] = keycode
;
1163 string
= separator
+ 1;
1165 nb_pending_keycodes
= i
;
1166 /* key down events */
1167 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1168 keycode
= keycodes
[i
];
1170 kbd_put_keycode(0xe0);
1171 kbd_put_keycode(keycode
& 0x7f);
1173 /* delayed key up events */
1174 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1175 muldiv64(ticks_per_sec
, hold_time
, 1000));
1178 static int mouse_button_state
;
1180 static void do_mouse_move(Monitor
*mon
, const QDict
*qdict
)
1183 const char *dx_str
= qdict_get_str(qdict
, "dx_str");
1184 const char *dy_str
= qdict_get_str(qdict
, "dy_str");
1185 const char *dz_str
= qdict_get_try_str(qdict
, "dz_str");
1186 dx
= strtol(dx_str
, NULL
, 0);
1187 dy
= strtol(dy_str
, NULL
, 0);
1190 dz
= strtol(dz_str
, NULL
, 0);
1191 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1194 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1196 int button_state
= qdict_get_int(qdict
, "button_state");
1197 mouse_button_state
= button_state
;
1198 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1201 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1202 int addr
, int has_index
, int index
)
1208 cpu_outb(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1216 val
= cpu_inb(NULL
, addr
);
1220 val
= cpu_inw(NULL
, addr
);
1224 val
= cpu_inl(NULL
, addr
);
1228 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1229 suffix
, addr
, size
* 2, val
);
1232 static void do_ioport_write(Monitor
*mon
, const QDict
*qdict
)
1234 int size
= qdict_get_int(qdict
, "size");
1235 int addr
= qdict_get_int(qdict
, "addr");
1236 int val
= qdict_get_int(qdict
, "val");
1238 addr
&= IOPORTS_MASK
;
1243 cpu_outb(NULL
, addr
, val
);
1246 cpu_outw(NULL
, addr
, val
);
1249 cpu_outl(NULL
, addr
, val
);
1254 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1257 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1259 res
= qemu_boot_set(bootdevice
);
1261 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1262 } else if (res
> 0) {
1263 monitor_printf(mon
, "setting boot device list failed\n");
1265 monitor_printf(mon
, "no function defined to set boot device list for "
1266 "this architecture\n");
1270 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1272 qemu_system_reset_request();
1275 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1277 qemu_system_powerdown_request();
1280 #if defined(TARGET_I386)
1281 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1283 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1286 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1287 pte
& PG_PSE_MASK
? 'P' : '-',
1288 pte
& PG_DIRTY_MASK
? 'D' : '-',
1289 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1290 pte
& PG_PCD_MASK
? 'C' : '-',
1291 pte
& PG_PWT_MASK
? 'T' : '-',
1292 pte
& PG_USER_MASK
? 'U' : '-',
1293 pte
& PG_RW_MASK
? 'W' : '-');
1296 static void tlb_info(Monitor
*mon
)
1300 uint32_t pgd
, pde
, pte
;
1302 env
= mon_get_cpu();
1306 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1307 monitor_printf(mon
, "PG disabled\n");
1310 pgd
= env
->cr
[3] & ~0xfff;
1311 for(l1
= 0; l1
< 1024; l1
++) {
1312 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1313 pde
= le32_to_cpu(pde
);
1314 if (pde
& PG_PRESENT_MASK
) {
1315 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1316 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1318 for(l2
= 0; l2
< 1024; l2
++) {
1319 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1320 (uint8_t *)&pte
, 4);
1321 pte
= le32_to_cpu(pte
);
1322 if (pte
& PG_PRESENT_MASK
) {
1323 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1333 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1334 uint32_t end
, int prot
)
1337 prot1
= *plast_prot
;
1338 if (prot
!= prot1
) {
1339 if (*pstart
!= -1) {
1340 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1341 *pstart
, end
, end
- *pstart
,
1342 prot1
& PG_USER_MASK
? 'u' : '-',
1344 prot1
& PG_RW_MASK
? 'w' : '-');
1354 static void mem_info(Monitor
*mon
)
1357 int l1
, l2
, prot
, last_prot
;
1358 uint32_t pgd
, pde
, pte
, start
, end
;
1360 env
= mon_get_cpu();
1364 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1365 monitor_printf(mon
, "PG disabled\n");
1368 pgd
= env
->cr
[3] & ~0xfff;
1371 for(l1
= 0; l1
< 1024; l1
++) {
1372 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1373 pde
= le32_to_cpu(pde
);
1375 if (pde
& PG_PRESENT_MASK
) {
1376 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1377 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1378 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1380 for(l2
= 0; l2
< 1024; l2
++) {
1381 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1382 (uint8_t *)&pte
, 4);
1383 pte
= le32_to_cpu(pte
);
1384 end
= (l1
<< 22) + (l2
<< 12);
1385 if (pte
& PG_PRESENT_MASK
) {
1386 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1390 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1395 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1401 #if defined(TARGET_SH4)
1403 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1405 monitor_printf(mon
, " tlb%i:\t"
1406 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1407 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1408 "dirty=%hhu writethrough=%hhu\n",
1410 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1411 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1415 static void tlb_info(Monitor
*mon
)
1417 CPUState
*env
= mon_get_cpu();
1420 monitor_printf (mon
, "ITLB:\n");
1421 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1422 print_tlb (mon
, i
, &env
->itlb
[i
]);
1423 monitor_printf (mon
, "UTLB:\n");
1424 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1425 print_tlb (mon
, i
, &env
->utlb
[i
]);
1430 static void do_info_kvm(Monitor
*mon
)
1433 monitor_printf(mon
, "kvm support: ");
1435 monitor_printf(mon
, "enabled\n");
1437 monitor_printf(mon
, "disabled\n");
1439 monitor_printf(mon
, "kvm support: not compiled\n");
1443 static void do_info_numa(Monitor
*mon
)
1448 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1449 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1450 monitor_printf(mon
, "node %d cpus:", i
);
1451 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1452 if (env
->numa_node
== i
) {
1453 monitor_printf(mon
, " %d", env
->cpu_index
);
1456 monitor_printf(mon
, "\n");
1457 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1462 #ifdef CONFIG_PROFILER
1464 static void do_info_profile(Monitor
*mon
)
1470 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1471 dev_time
, dev_time
/ (double)ticks_per_sec
);
1472 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1473 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1478 static void do_info_profile(Monitor
*mon
)
1480 monitor_printf(mon
, "Internal profiler not compiled\n");
1484 /* Capture support */
1485 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1487 static void do_info_capture(Monitor
*mon
)
1492 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1493 monitor_printf(mon
, "[%d]: ", i
);
1494 s
->ops
.info (s
->opaque
);
1499 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1502 int n
= qdict_get_int(qdict
, "n");
1505 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1507 s
->ops
.destroy (s
->opaque
);
1508 LIST_REMOVE (s
, entries
);
1515 static void do_wav_capture(Monitor
*mon
, const char *path
,
1516 int has_freq
, int freq
,
1517 int has_bits
, int bits
,
1518 int has_channels
, int nchannels
)
1522 s
= qemu_mallocz (sizeof (*s
));
1524 freq
= has_freq
? freq
: 44100;
1525 bits
= has_bits
? bits
: 16;
1526 nchannels
= has_channels
? nchannels
: 2;
1528 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1529 monitor_printf(mon
, "Faied to add wave capture\n");
1532 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1536 #if defined(TARGET_I386)
1537 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1540 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1542 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1543 if (env
->cpu_index
== cpu_index
) {
1544 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1550 static void do_info_status(Monitor
*mon
)
1554 monitor_printf(mon
, "VM status: running (single step mode)\n");
1556 monitor_printf(mon
, "VM status: running\n");
1559 monitor_printf(mon
, "VM status: paused\n");
1563 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1565 int value
= qdict_get_int(qdict
, "value");
1566 ram_addr_t target
= value
;
1567 qemu_balloon(target
<< 20);
1570 static void do_info_balloon(Monitor
*mon
)
1574 actual
= qemu_balloon_status();
1575 if (kvm_enabled() && !kvm_has_sync_mmu())
1576 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1577 "ballooning disabled\n");
1578 else if (actual
== 0)
1579 monitor_printf(mon
, "Ballooning not activated in VM\n");
1581 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1584 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1586 qemu_acl
*acl
= qemu_acl_find(name
);
1589 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1594 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1596 const char *aclname
= qdict_get_str(qdict
, "aclname");
1597 qemu_acl
*acl
= find_acl(mon
, aclname
);
1598 qemu_acl_entry
*entry
;
1602 monitor_printf(mon
, "policy: %s\n",
1603 acl
->defaultDeny
? "deny" : "allow");
1604 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1606 monitor_printf(mon
, "%d: %s %s\n", i
,
1607 entry
->deny
? "deny" : "allow", entry
->match
);
1612 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1614 const char *aclname
= qdict_get_str(qdict
, "aclname");
1615 qemu_acl
*acl
= find_acl(mon
, aclname
);
1618 qemu_acl_reset(acl
);
1619 monitor_printf(mon
, "acl: removed all rules\n");
1623 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1625 const char *aclname
= qdict_get_str(qdict
, "aclname");
1626 const char *policy
= qdict_get_str(qdict
, "policy");
1627 qemu_acl
*acl
= find_acl(mon
, aclname
);
1630 if (strcmp(policy
, "allow") == 0) {
1631 acl
->defaultDeny
= 0;
1632 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1633 } else if (strcmp(policy
, "deny") == 0) {
1634 acl
->defaultDeny
= 1;
1635 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1637 monitor_printf(mon
, "acl: unknown policy '%s', "
1638 "expected 'deny' or 'allow'\n", policy
);
1643 static void do_acl_add(Monitor
*mon
, const QDict
*qdict
)
1645 const char *aclname
= qdict_get_str(qdict
, "aclname");
1646 const char *match
= qdict_get_str(qdict
, "match");
1647 const char *policy
= qdict_get_str(qdict
, "policy");
1648 int has_index
= qdict_haskey(qdict
, "index");
1649 int index
= qdict_get_try_int(qdict
, "index", -1);
1650 qemu_acl
*acl
= find_acl(mon
, aclname
);
1654 if (strcmp(policy
, "allow") == 0) {
1656 } else if (strcmp(policy
, "deny") == 0) {
1659 monitor_printf(mon
, "acl: unknown policy '%s', "
1660 "expected 'deny' or 'allow'\n", policy
);
1664 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1666 ret
= qemu_acl_append(acl
, deny
, match
);
1668 monitor_printf(mon
, "acl: unable to add acl entry\n");
1670 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1674 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1676 const char *aclname
= qdict_get_str(qdict
, "aclname");
1677 const char *match
= qdict_get_str(qdict
, "match");
1678 qemu_acl
*acl
= find_acl(mon
, aclname
);
1682 ret
= qemu_acl_remove(acl
, match
);
1684 monitor_printf(mon
, "acl: no matching acl entry\n");
1686 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1690 #if defined(TARGET_I386)
1691 static void do_inject_mce(Monitor
*mon
,
1692 int cpu_index
, int bank
,
1693 unsigned status_hi
, unsigned status_lo
,
1694 unsigned mcg_status_hi
, unsigned mcg_status_lo
,
1695 unsigned addr_hi
, unsigned addr_lo
,
1696 unsigned misc_hi
, unsigned misc_lo
)
1699 uint64_t status
= ((uint64_t)status_hi
<< 32) | status_lo
;
1700 uint64_t mcg_status
= ((uint64_t)mcg_status_hi
<< 32) | mcg_status_lo
;
1701 uint64_t addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1702 uint64_t misc
= ((uint64_t)misc_hi
<< 32) | misc_lo
;
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 LIST_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 LIST_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 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1759 if (strcmp(monfd
->name
, fdname
) != 0) {
1763 LIST_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 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1792 if (strcmp(monfd
->name
, fdname
) != 0) {
1798 /* caller takes ownership of fd */
1799 LIST_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" },
1894 /*******************************************************************/
1896 static const char *pch
;
1897 static jmp_buf expr_env
;
1902 typedef struct MonitorDef
{
1905 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1909 #if defined(TARGET_I386)
1910 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1912 CPUState
*env
= mon_get_cpu();
1915 return env
->eip
+ env
->segs
[R_CS
].base
;
1919 #if defined(TARGET_PPC)
1920 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1922 CPUState
*env
= mon_get_cpu();
1930 for (i
= 0; i
< 8; i
++)
1931 u
|= env
->crf
[i
] << (32 - (4 * i
));
1936 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1938 CPUState
*env
= mon_get_cpu();
1944 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1946 CPUState
*env
= mon_get_cpu();
1952 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1954 CPUState
*env
= mon_get_cpu();
1957 return cpu_ppc_load_decr(env
);
1960 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1962 CPUState
*env
= mon_get_cpu();
1965 return cpu_ppc_load_tbu(env
);
1968 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1970 CPUState
*env
= mon_get_cpu();
1973 return cpu_ppc_load_tbl(env
);
1977 #if defined(TARGET_SPARC)
1978 #ifndef TARGET_SPARC64
1979 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1981 CPUState
*env
= mon_get_cpu();
1984 return GET_PSR(env
);
1988 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1990 CPUState
*env
= mon_get_cpu();
1993 return env
->regwptr
[val
];
1997 static const MonitorDef monitor_defs
[] = {
2000 #define SEG(name, seg) \
2001 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2002 { name ".base", offsetof(CPUState, segs[seg].base) },\
2003 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2005 { "eax", offsetof(CPUState
, regs
[0]) },
2006 { "ecx", offsetof(CPUState
, regs
[1]) },
2007 { "edx", offsetof(CPUState
, regs
[2]) },
2008 { "ebx", offsetof(CPUState
, regs
[3]) },
2009 { "esp|sp", offsetof(CPUState
, regs
[4]) },
2010 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
2011 { "esi", offsetof(CPUState
, regs
[6]) },
2012 { "edi", offsetof(CPUState
, regs
[7]) },
2013 #ifdef TARGET_X86_64
2014 { "r8", offsetof(CPUState
, regs
[8]) },
2015 { "r9", offsetof(CPUState
, regs
[9]) },
2016 { "r10", offsetof(CPUState
, regs
[10]) },
2017 { "r11", offsetof(CPUState
, regs
[11]) },
2018 { "r12", offsetof(CPUState
, regs
[12]) },
2019 { "r13", offsetof(CPUState
, regs
[13]) },
2020 { "r14", offsetof(CPUState
, regs
[14]) },
2021 { "r15", offsetof(CPUState
, regs
[15]) },
2023 { "eflags", offsetof(CPUState
, eflags
) },
2024 { "eip", offsetof(CPUState
, eip
) },
2031 { "pc", 0, monitor_get_pc
, },
2032 #elif defined(TARGET_PPC)
2033 /* General purpose registers */
2034 { "r0", offsetof(CPUState
, gpr
[0]) },
2035 { "r1", offsetof(CPUState
, gpr
[1]) },
2036 { "r2", offsetof(CPUState
, gpr
[2]) },
2037 { "r3", offsetof(CPUState
, gpr
[3]) },
2038 { "r4", offsetof(CPUState
, gpr
[4]) },
2039 { "r5", offsetof(CPUState
, gpr
[5]) },
2040 { "r6", offsetof(CPUState
, gpr
[6]) },
2041 { "r7", offsetof(CPUState
, gpr
[7]) },
2042 { "r8", offsetof(CPUState
, gpr
[8]) },
2043 { "r9", offsetof(CPUState
, gpr
[9]) },
2044 { "r10", offsetof(CPUState
, gpr
[10]) },
2045 { "r11", offsetof(CPUState
, gpr
[11]) },
2046 { "r12", offsetof(CPUState
, gpr
[12]) },
2047 { "r13", offsetof(CPUState
, gpr
[13]) },
2048 { "r14", offsetof(CPUState
, gpr
[14]) },
2049 { "r15", offsetof(CPUState
, gpr
[15]) },
2050 { "r16", offsetof(CPUState
, gpr
[16]) },
2051 { "r17", offsetof(CPUState
, gpr
[17]) },
2052 { "r18", offsetof(CPUState
, gpr
[18]) },
2053 { "r19", offsetof(CPUState
, gpr
[19]) },
2054 { "r20", offsetof(CPUState
, gpr
[20]) },
2055 { "r21", offsetof(CPUState
, gpr
[21]) },
2056 { "r22", offsetof(CPUState
, gpr
[22]) },
2057 { "r23", offsetof(CPUState
, gpr
[23]) },
2058 { "r24", offsetof(CPUState
, gpr
[24]) },
2059 { "r25", offsetof(CPUState
, gpr
[25]) },
2060 { "r26", offsetof(CPUState
, gpr
[26]) },
2061 { "r27", offsetof(CPUState
, gpr
[27]) },
2062 { "r28", offsetof(CPUState
, gpr
[28]) },
2063 { "r29", offsetof(CPUState
, gpr
[29]) },
2064 { "r30", offsetof(CPUState
, gpr
[30]) },
2065 { "r31", offsetof(CPUState
, gpr
[31]) },
2066 /* Floating point registers */
2067 { "f0", offsetof(CPUState
, fpr
[0]) },
2068 { "f1", offsetof(CPUState
, fpr
[1]) },
2069 { "f2", offsetof(CPUState
, fpr
[2]) },
2070 { "f3", offsetof(CPUState
, fpr
[3]) },
2071 { "f4", offsetof(CPUState
, fpr
[4]) },
2072 { "f5", offsetof(CPUState
, fpr
[5]) },
2073 { "f6", offsetof(CPUState
, fpr
[6]) },
2074 { "f7", offsetof(CPUState
, fpr
[7]) },
2075 { "f8", offsetof(CPUState
, fpr
[8]) },
2076 { "f9", offsetof(CPUState
, fpr
[9]) },
2077 { "f10", offsetof(CPUState
, fpr
[10]) },
2078 { "f11", offsetof(CPUState
, fpr
[11]) },
2079 { "f12", offsetof(CPUState
, fpr
[12]) },
2080 { "f13", offsetof(CPUState
, fpr
[13]) },
2081 { "f14", offsetof(CPUState
, fpr
[14]) },
2082 { "f15", offsetof(CPUState
, fpr
[15]) },
2083 { "f16", offsetof(CPUState
, fpr
[16]) },
2084 { "f17", offsetof(CPUState
, fpr
[17]) },
2085 { "f18", offsetof(CPUState
, fpr
[18]) },
2086 { "f19", offsetof(CPUState
, fpr
[19]) },
2087 { "f20", offsetof(CPUState
, fpr
[20]) },
2088 { "f21", offsetof(CPUState
, fpr
[21]) },
2089 { "f22", offsetof(CPUState
, fpr
[22]) },
2090 { "f23", offsetof(CPUState
, fpr
[23]) },
2091 { "f24", offsetof(CPUState
, fpr
[24]) },
2092 { "f25", offsetof(CPUState
, fpr
[25]) },
2093 { "f26", offsetof(CPUState
, fpr
[26]) },
2094 { "f27", offsetof(CPUState
, fpr
[27]) },
2095 { "f28", offsetof(CPUState
, fpr
[28]) },
2096 { "f29", offsetof(CPUState
, fpr
[29]) },
2097 { "f30", offsetof(CPUState
, fpr
[30]) },
2098 { "f31", offsetof(CPUState
, fpr
[31]) },
2099 { "fpscr", offsetof(CPUState
, fpscr
) },
2100 /* Next instruction pointer */
2101 { "nip|pc", offsetof(CPUState
, nip
) },
2102 { "lr", offsetof(CPUState
, lr
) },
2103 { "ctr", offsetof(CPUState
, ctr
) },
2104 { "decr", 0, &monitor_get_decr
, },
2105 { "ccr", 0, &monitor_get_ccr
, },
2106 /* Machine state register */
2107 { "msr", 0, &monitor_get_msr
, },
2108 { "xer", 0, &monitor_get_xer
, },
2109 { "tbu", 0, &monitor_get_tbu
, },
2110 { "tbl", 0, &monitor_get_tbl
, },
2111 #if defined(TARGET_PPC64)
2112 /* Address space register */
2113 { "asr", offsetof(CPUState
, asr
) },
2115 /* Segment registers */
2116 { "sdr1", offsetof(CPUState
, sdr1
) },
2117 { "sr0", offsetof(CPUState
, sr
[0]) },
2118 { "sr1", offsetof(CPUState
, sr
[1]) },
2119 { "sr2", offsetof(CPUState
, sr
[2]) },
2120 { "sr3", offsetof(CPUState
, sr
[3]) },
2121 { "sr4", offsetof(CPUState
, sr
[4]) },
2122 { "sr5", offsetof(CPUState
, sr
[5]) },
2123 { "sr6", offsetof(CPUState
, sr
[6]) },
2124 { "sr7", offsetof(CPUState
, sr
[7]) },
2125 { "sr8", offsetof(CPUState
, sr
[8]) },
2126 { "sr9", offsetof(CPUState
, sr
[9]) },
2127 { "sr10", offsetof(CPUState
, sr
[10]) },
2128 { "sr11", offsetof(CPUState
, sr
[11]) },
2129 { "sr12", offsetof(CPUState
, sr
[12]) },
2130 { "sr13", offsetof(CPUState
, sr
[13]) },
2131 { "sr14", offsetof(CPUState
, sr
[14]) },
2132 { "sr15", offsetof(CPUState
, sr
[15]) },
2133 /* Too lazy to put BATs and SPRs ... */
2134 #elif defined(TARGET_SPARC)
2135 { "g0", offsetof(CPUState
, gregs
[0]) },
2136 { "g1", offsetof(CPUState
, gregs
[1]) },
2137 { "g2", offsetof(CPUState
, gregs
[2]) },
2138 { "g3", offsetof(CPUState
, gregs
[3]) },
2139 { "g4", offsetof(CPUState
, gregs
[4]) },
2140 { "g5", offsetof(CPUState
, gregs
[5]) },
2141 { "g6", offsetof(CPUState
, gregs
[6]) },
2142 { "g7", offsetof(CPUState
, gregs
[7]) },
2143 { "o0", 0, monitor_get_reg
},
2144 { "o1", 1, monitor_get_reg
},
2145 { "o2", 2, monitor_get_reg
},
2146 { "o3", 3, monitor_get_reg
},
2147 { "o4", 4, monitor_get_reg
},
2148 { "o5", 5, monitor_get_reg
},
2149 { "o6", 6, monitor_get_reg
},
2150 { "o7", 7, monitor_get_reg
},
2151 { "l0", 8, monitor_get_reg
},
2152 { "l1", 9, monitor_get_reg
},
2153 { "l2", 10, monitor_get_reg
},
2154 { "l3", 11, monitor_get_reg
},
2155 { "l4", 12, monitor_get_reg
},
2156 { "l5", 13, monitor_get_reg
},
2157 { "l6", 14, monitor_get_reg
},
2158 { "l7", 15, monitor_get_reg
},
2159 { "i0", 16, monitor_get_reg
},
2160 { "i1", 17, monitor_get_reg
},
2161 { "i2", 18, monitor_get_reg
},
2162 { "i3", 19, monitor_get_reg
},
2163 { "i4", 20, monitor_get_reg
},
2164 { "i5", 21, monitor_get_reg
},
2165 { "i6", 22, monitor_get_reg
},
2166 { "i7", 23, monitor_get_reg
},
2167 { "pc", offsetof(CPUState
, pc
) },
2168 { "npc", offsetof(CPUState
, npc
) },
2169 { "y", offsetof(CPUState
, y
) },
2170 #ifndef TARGET_SPARC64
2171 { "psr", 0, &monitor_get_psr
, },
2172 { "wim", offsetof(CPUState
, wim
) },
2174 { "tbr", offsetof(CPUState
, tbr
) },
2175 { "fsr", offsetof(CPUState
, fsr
) },
2176 { "f0", offsetof(CPUState
, fpr
[0]) },
2177 { "f1", offsetof(CPUState
, fpr
[1]) },
2178 { "f2", offsetof(CPUState
, fpr
[2]) },
2179 { "f3", offsetof(CPUState
, fpr
[3]) },
2180 { "f4", offsetof(CPUState
, fpr
[4]) },
2181 { "f5", offsetof(CPUState
, fpr
[5]) },
2182 { "f6", offsetof(CPUState
, fpr
[6]) },
2183 { "f7", offsetof(CPUState
, fpr
[7]) },
2184 { "f8", offsetof(CPUState
, fpr
[8]) },
2185 { "f9", offsetof(CPUState
, fpr
[9]) },
2186 { "f10", offsetof(CPUState
, fpr
[10]) },
2187 { "f11", offsetof(CPUState
, fpr
[11]) },
2188 { "f12", offsetof(CPUState
, fpr
[12]) },
2189 { "f13", offsetof(CPUState
, fpr
[13]) },
2190 { "f14", offsetof(CPUState
, fpr
[14]) },
2191 { "f15", offsetof(CPUState
, fpr
[15]) },
2192 { "f16", offsetof(CPUState
, fpr
[16]) },
2193 { "f17", offsetof(CPUState
, fpr
[17]) },
2194 { "f18", offsetof(CPUState
, fpr
[18]) },
2195 { "f19", offsetof(CPUState
, fpr
[19]) },
2196 { "f20", offsetof(CPUState
, fpr
[20]) },
2197 { "f21", offsetof(CPUState
, fpr
[21]) },
2198 { "f22", offsetof(CPUState
, fpr
[22]) },
2199 { "f23", offsetof(CPUState
, fpr
[23]) },
2200 { "f24", offsetof(CPUState
, fpr
[24]) },
2201 { "f25", offsetof(CPUState
, fpr
[25]) },
2202 { "f26", offsetof(CPUState
, fpr
[26]) },
2203 { "f27", offsetof(CPUState
, fpr
[27]) },
2204 { "f28", offsetof(CPUState
, fpr
[28]) },
2205 { "f29", offsetof(CPUState
, fpr
[29]) },
2206 { "f30", offsetof(CPUState
, fpr
[30]) },
2207 { "f31", offsetof(CPUState
, fpr
[31]) },
2208 #ifdef TARGET_SPARC64
2209 { "f32", offsetof(CPUState
, fpr
[32]) },
2210 { "f34", offsetof(CPUState
, fpr
[34]) },
2211 { "f36", offsetof(CPUState
, fpr
[36]) },
2212 { "f38", offsetof(CPUState
, fpr
[38]) },
2213 { "f40", offsetof(CPUState
, fpr
[40]) },
2214 { "f42", offsetof(CPUState
, fpr
[42]) },
2215 { "f44", offsetof(CPUState
, fpr
[44]) },
2216 { "f46", offsetof(CPUState
, fpr
[46]) },
2217 { "f48", offsetof(CPUState
, fpr
[48]) },
2218 { "f50", offsetof(CPUState
, fpr
[50]) },
2219 { "f52", offsetof(CPUState
, fpr
[52]) },
2220 { "f54", offsetof(CPUState
, fpr
[54]) },
2221 { "f56", offsetof(CPUState
, fpr
[56]) },
2222 { "f58", offsetof(CPUState
, fpr
[58]) },
2223 { "f60", offsetof(CPUState
, fpr
[60]) },
2224 { "f62", offsetof(CPUState
, fpr
[62]) },
2225 { "asi", offsetof(CPUState
, asi
) },
2226 { "pstate", offsetof(CPUState
, pstate
) },
2227 { "cansave", offsetof(CPUState
, cansave
) },
2228 { "canrestore", offsetof(CPUState
, canrestore
) },
2229 { "otherwin", offsetof(CPUState
, otherwin
) },
2230 { "wstate", offsetof(CPUState
, wstate
) },
2231 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2232 { "fprs", offsetof(CPUState
, fprs
) },
2238 static void expr_error(Monitor
*mon
, const char *msg
)
2240 monitor_printf(mon
, "%s\n", msg
);
2241 longjmp(expr_env
, 1);
2244 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2245 static int get_monitor_def(target_long
*pval
, const char *name
)
2247 const MonitorDef
*md
;
2250 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2251 if (compare_cmd(name
, md
->name
)) {
2252 if (md
->get_value
) {
2253 *pval
= md
->get_value(md
, md
->offset
);
2255 CPUState
*env
= mon_get_cpu();
2258 ptr
= (uint8_t *)env
+ md
->offset
;
2261 *pval
= *(int32_t *)ptr
;
2264 *pval
= *(target_long
*)ptr
;
2277 static void next(void)
2281 while (qemu_isspace(*pch
))
2286 static int64_t expr_sum(Monitor
*mon
);
2288 static int64_t expr_unary(Monitor
*mon
)
2297 n
= expr_unary(mon
);
2301 n
= -expr_unary(mon
);
2305 n
= ~expr_unary(mon
);
2311 expr_error(mon
, "')' expected");
2318 expr_error(mon
, "character constant expected");
2322 expr_error(mon
, "missing terminating \' character");
2332 while ((*pch
>= 'a' && *pch
<= 'z') ||
2333 (*pch
>= 'A' && *pch
<= 'Z') ||
2334 (*pch
>= '0' && *pch
<= '9') ||
2335 *pch
== '_' || *pch
== '.') {
2336 if ((q
- buf
) < sizeof(buf
) - 1)
2340 while (qemu_isspace(*pch
))
2343 ret
= get_monitor_def(®
, buf
);
2345 expr_error(mon
, "unknown register");
2347 expr_error(mon
, "no cpu defined");
2352 expr_error(mon
, "unexpected end of expression");
2356 #if TARGET_PHYS_ADDR_BITS > 32
2357 n
= strtoull(pch
, &p
, 0);
2359 n
= strtoul(pch
, &p
, 0);
2362 expr_error(mon
, "invalid char in expression");
2365 while (qemu_isspace(*pch
))
2373 static int64_t expr_prod(Monitor
*mon
)
2378 val
= expr_unary(mon
);
2381 if (op
!= '*' && op
!= '/' && op
!= '%')
2384 val2
= expr_unary(mon
);
2393 expr_error(mon
, "division by zero");
2404 static int64_t expr_logic(Monitor
*mon
)
2409 val
= expr_prod(mon
);
2412 if (op
!= '&' && op
!= '|' && op
!= '^')
2415 val2
= expr_prod(mon
);
2432 static int64_t expr_sum(Monitor
*mon
)
2437 val
= expr_logic(mon
);
2440 if (op
!= '+' && op
!= '-')
2443 val2
= expr_logic(mon
);
2452 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2455 if (setjmp(expr_env
)) {
2459 while (qemu_isspace(*pch
))
2461 *pval
= expr_sum(mon
);
2466 static int get_str(char *buf
, int buf_size
, const char **pp
)
2474 while (qemu_isspace(*p
))
2484 while (*p
!= '\0' && *p
!= '\"') {
2500 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2503 if ((q
- buf
) < buf_size
- 1) {
2507 if ((q
- buf
) < buf_size
- 1) {
2514 qemu_printf("unterminated string\n");
2519 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2520 if ((q
- buf
) < buf_size
- 1) {
2532 * Store the command-name in cmdname, and return a pointer to
2533 * the remaining of the command string.
2535 static const char *get_command_name(const char *cmdline
,
2536 char *cmdname
, size_t nlen
)
2539 const char *p
, *pstart
;
2542 while (qemu_isspace(*p
))
2547 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2552 memcpy(cmdname
, pstart
, len
);
2553 cmdname
[len
] = '\0';
2558 * Read key of 'type' into 'key' and return the current
2561 static char *key_get_info(const char *type
, char **key
)
2569 p
= strchr(type
, ':');
2576 str
= qemu_malloc(len
+ 1);
2577 memcpy(str
, type
, len
);
2584 static int default_fmt_format
= 'x';
2585 static int default_fmt_size
= 4;
2589 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2591 const char *p
, *typestr
;
2592 int c
, nb_args
, i
, has_arg
;
2593 const mon_cmd_t
*cmd
;
2598 void *str_allocated
[MAX_ARGS
];
2599 void *args
[MAX_ARGS
];
2600 void (*handler_d
)(Monitor
*mon
, const QDict
*qdict
);
2601 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2602 void *arg3
, void *arg4
, void *arg5
);
2603 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2604 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2605 void (*handler_8
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2606 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2608 void (*handler_9
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2609 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2610 void *arg7
, void *arg8
);
2611 void (*handler_10
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2612 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2613 void *arg7
, void *arg8
, void *arg9
);
2616 monitor_printf(mon
, "command='%s'\n", cmdline
);
2619 /* extract the command name */
2620 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2624 /* find the command */
2625 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2626 if (compare_cmd(cmdname
, cmd
->name
))
2630 if (cmd
->name
== NULL
) {
2631 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2635 qdict
= qdict_new();
2637 for(i
= 0; i
< MAX_ARGS
; i
++)
2638 str_allocated
[i
] = NULL
;
2640 /* parse the parameters */
2641 typestr
= cmd
->args_type
;
2644 typestr
= key_get_info(typestr
, &key
);
2657 while (qemu_isspace(*p
))
2659 if (*typestr
== '?') {
2662 /* no optional string: NULL argument */
2667 ret
= get_str(buf
, sizeof(buf
), &p
);
2671 monitor_printf(mon
, "%s: filename expected\n",
2675 monitor_printf(mon
, "%s: block device name expected\n",
2679 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2684 str
= qemu_malloc(strlen(buf
) + 1);
2685 pstrcpy(str
, sizeof(buf
), buf
);
2686 str_allocated
[nb_args
] = str
;
2688 if (nb_args
>= MAX_ARGS
) {
2690 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2693 args
[nb_args
++] = str
;
2695 qdict_put(qdict
, key
, qstring_from_str(str
));
2700 int count
, format
, size
;
2702 while (qemu_isspace(*p
))
2708 if (qemu_isdigit(*p
)) {
2710 while (qemu_isdigit(*p
)) {
2711 count
= count
* 10 + (*p
- '0');
2749 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2750 monitor_printf(mon
, "invalid char in format: '%c'\n",
2755 format
= default_fmt_format
;
2756 if (format
!= 'i') {
2757 /* for 'i', not specifying a size gives -1 as size */
2759 size
= default_fmt_size
;
2760 default_fmt_size
= size
;
2762 default_fmt_format
= format
;
2765 format
= default_fmt_format
;
2766 if (format
!= 'i') {
2767 size
= default_fmt_size
;
2772 if (nb_args
+ 3 > MAX_ARGS
)
2774 args
[nb_args
++] = (void*)(long)count
;
2775 args
[nb_args
++] = (void*)(long)format
;
2776 args
[nb_args
++] = (void*)(long)size
;
2777 qdict_put(qdict
, "count", qint_from_int(count
));
2778 qdict_put(qdict
, "format", qint_from_int(format
));
2779 qdict_put(qdict
, "size", qint_from_int(size
));
2788 while (qemu_isspace(*p
))
2790 if (*typestr
== '?' || *typestr
== '.') {
2791 if (*typestr
== '?') {
2799 while (qemu_isspace(*p
))
2807 if (nb_args
>= MAX_ARGS
)
2810 args
[nb_args
++] = (void *)(long)has_arg
;
2812 if (nb_args
>= MAX_ARGS
)
2818 if (get_expr(mon
, &val
, &p
))
2822 if (nb_args
>= MAX_ARGS
)
2824 args
[nb_args
++] = (void *)(long)val
;
2826 qdict_put(qdict
, key
, qint_from_int(val
));
2828 if ((nb_args
+ 1) >= MAX_ARGS
)
2830 #if TARGET_PHYS_ADDR_BITS > 32
2831 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2833 args
[nb_args
++] = (void *)0;
2835 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2836 qdict_put(qdict
, key
, qint_from_int(val
));
2848 while (qemu_isspace(*p
))
2854 monitor_printf(mon
, "%s: unsupported option -%c\n",
2861 if (nb_args
>= MAX_ARGS
)
2863 args
[nb_args
++] = (void *)(long)has_option
;
2864 qdict_put(qdict
, key
, qint_from_int(has_option
));
2869 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2875 /* check that all arguments were parsed */
2876 while (qemu_isspace(*p
))
2879 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2884 qemu_errors_to_mon(mon
);
2892 handler_d
= cmd
->handler
;
2893 handler_d(mon
, qdict
);
2896 handler_6
= cmd
->handler
;
2897 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2900 handler_7
= cmd
->handler
;
2901 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2905 handler_8
= cmd
->handler
;
2906 handler_8(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2910 handler_9
= cmd
->handler
;
2911 handler_9(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2912 args
[6], args
[7], args
[8]);
2915 handler_10
= cmd
->handler
;
2916 handler_10(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2917 args
[6], args
[7], args
[8], args
[9]);
2920 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2923 qemu_errors_to_previous();
2927 for(i
= 0; i
< MAX_ARGS
; i
++)
2928 qemu_free(str_allocated
[i
]);
2932 static void cmd_completion(const char *name
, const char *list
)
2934 const char *p
, *pstart
;
2943 p
= pstart
+ strlen(pstart
);
2945 if (len
> sizeof(cmd
) - 2)
2946 len
= sizeof(cmd
) - 2;
2947 memcpy(cmd
, pstart
, len
);
2949 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2950 readline_add_completion(cur_mon
->rs
, cmd
);
2958 static void file_completion(const char *input
)
2963 char file
[1024], file_prefix
[1024];
2967 p
= strrchr(input
, '/');
2970 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2971 pstrcpy(path
, sizeof(path
), ".");
2973 input_path_len
= p
- input
+ 1;
2974 memcpy(path
, input
, input_path_len
);
2975 if (input_path_len
> sizeof(path
) - 1)
2976 input_path_len
= sizeof(path
) - 1;
2977 path
[input_path_len
] = '\0';
2978 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2980 #ifdef DEBUG_COMPLETION
2981 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2982 input
, path
, file_prefix
);
2984 ffs
= opendir(path
);
2992 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2993 memcpy(file
, input
, input_path_len
);
2994 if (input_path_len
< sizeof(file
))
2995 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2997 /* stat the file to find out if it's a directory.
2998 * In that case add a slash to speed up typing long paths
3001 if(S_ISDIR(sb
.st_mode
))
3002 pstrcat(file
, sizeof(file
), "/");
3003 readline_add_completion(cur_mon
->rs
, file
);
3009 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3011 const char *name
= bdrv_get_device_name(bs
);
3012 const char *input
= opaque
;
3014 if (input
[0] == '\0' ||
3015 !strncmp(name
, (char *)input
, strlen(input
))) {
3016 readline_add_completion(cur_mon
->rs
, name
);
3020 /* NOTE: this parser is an approximate form of the real command parser */
3021 static void parse_cmdline(const char *cmdline
,
3022 int *pnb_args
, char **args
)
3031 while (qemu_isspace(*p
))
3035 if (nb_args
>= MAX_ARGS
)
3037 ret
= get_str(buf
, sizeof(buf
), &p
);
3038 args
[nb_args
] = qemu_strdup(buf
);
3043 *pnb_args
= nb_args
;
3046 static const char *next_arg_type(const char *typestr
)
3048 const char *p
= strchr(typestr
, ':');
3049 return (p
!= NULL
? ++p
: typestr
);
3052 static void monitor_find_completion(const char *cmdline
)
3054 const char *cmdname
;
3055 char *args
[MAX_ARGS
];
3056 int nb_args
, i
, len
;
3057 const char *ptype
, *str
;
3058 const mon_cmd_t
*cmd
;
3061 parse_cmdline(cmdline
, &nb_args
, args
);
3062 #ifdef DEBUG_COMPLETION
3063 for(i
= 0; i
< nb_args
; i
++) {
3064 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3068 /* if the line ends with a space, it means we want to complete the
3070 len
= strlen(cmdline
);
3071 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3072 if (nb_args
>= MAX_ARGS
)
3074 args
[nb_args
++] = qemu_strdup("");
3077 /* command completion */
3082 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3083 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3084 cmd_completion(cmdname
, cmd
->name
);
3087 /* find the command */
3088 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3089 if (compare_cmd(args
[0], cmd
->name
))
3094 ptype
= next_arg_type(cmd
->args_type
);
3095 for(i
= 0; i
< nb_args
- 2; i
++) {
3096 if (*ptype
!= '\0') {
3097 ptype
= next_arg_type(ptype
);
3098 while (*ptype
== '?')
3099 ptype
= next_arg_type(ptype
);
3102 str
= args
[nb_args
- 1];
3103 if (*ptype
== '-' && ptype
[1] != '\0') {
3108 /* file completion */
3109 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3110 file_completion(str
);
3113 /* block device name completion */
3114 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3115 bdrv_iterate(block_completion_it
, (void *)str
);
3118 /* XXX: more generic ? */
3119 if (!strcmp(cmd
->name
, "info")) {
3120 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3121 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3122 cmd_completion(str
, cmd
->name
);
3124 } else if (!strcmp(cmd
->name
, "sendkey")) {
3125 char *sep
= strrchr(str
, '-');
3128 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3129 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3130 cmd_completion(str
, key
->name
);
3132 } else if (!strcmp(cmd
->name
, "help|?")) {
3133 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3134 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3135 cmd_completion(str
, cmd
->name
);
3143 for(i
= 0; i
< nb_args
; i
++)
3147 static int monitor_can_read(void *opaque
)
3149 Monitor
*mon
= opaque
;
3151 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3154 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3156 Monitor
*old_mon
= cur_mon
;
3162 for (i
= 0; i
< size
; i
++)
3163 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3165 if (size
== 0 || buf
[size
- 1] != 0)
3166 monitor_printf(cur_mon
, "corrupted command\n");
3168 monitor_handle_command(cur_mon
, (char *)buf
);
3174 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3176 monitor_suspend(mon
);
3177 monitor_handle_command(mon
, cmdline
);
3178 monitor_resume(mon
);
3181 int monitor_suspend(Monitor
*mon
)
3189 void monitor_resume(Monitor
*mon
)
3193 if (--mon
->suspend_cnt
== 0)
3194 readline_show_prompt(mon
->rs
);
3197 static void monitor_event(void *opaque
, int event
)
3199 Monitor
*mon
= opaque
;
3202 case CHR_EVENT_MUX_IN
:
3203 readline_restart(mon
->rs
);
3204 monitor_resume(mon
);
3208 case CHR_EVENT_MUX_OUT
:
3209 if (mon
->suspend_cnt
== 0)
3210 monitor_printf(mon
, "\n");
3212 monitor_suspend(mon
);
3215 case CHR_EVENT_RESET
:
3216 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3217 "information\n", QEMU_VERSION
);
3218 if (mon
->chr
->focus
== 0)
3219 readline_show_prompt(mon
->rs
);
3233 void monitor_init(CharDriverState
*chr
, int flags
)
3235 static int is_first_init
= 1;
3238 if (is_first_init
) {
3239 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3243 mon
= qemu_mallocz(sizeof(*mon
));
3247 if (mon
->chr
->focus
!= 0)
3248 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3249 if (flags
& MONITOR_USE_READLINE
) {
3250 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3251 monitor_read_command(mon
, 0);
3254 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3257 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3258 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3262 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3264 BlockDriverState
*bs
= opaque
;
3267 if (bdrv_set_key(bs
, password
) != 0) {
3268 monitor_printf(mon
, "invalid password\n");
3271 if (mon
->password_completion_cb
)
3272 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3274 monitor_read_command(mon
, 1);
3277 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3278 BlockDriverCompletionFunc
*completion_cb
,
3283 if (!bdrv_key_required(bs
)) {
3285 completion_cb(opaque
, 0);
3289 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3290 bdrv_get_encrypted_filename(bs
));
3292 mon
->password_completion_cb
= completion_cb
;
3293 mon
->password_opaque
= opaque
;
3295 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3297 if (err
&& completion_cb
)
3298 completion_cb(opaque
, err
);
3301 typedef struct QemuErrorSink QemuErrorSink
;
3302 struct QemuErrorSink
{
3311 QemuErrorSink
*previous
;
3314 static QemuErrorSink
*qemu_error_sink
;
3316 void qemu_errors_to_file(FILE *fp
)
3318 QemuErrorSink
*sink
;
3320 sink
= qemu_mallocz(sizeof(*sink
));
3321 sink
->dest
= ERR_SINK_FILE
;
3323 sink
->previous
= qemu_error_sink
;
3324 qemu_error_sink
= sink
;
3327 void qemu_errors_to_mon(Monitor
*mon
)
3329 QemuErrorSink
*sink
;
3331 sink
= qemu_mallocz(sizeof(*sink
));
3332 sink
->dest
= ERR_SINK_MONITOR
;
3334 sink
->previous
= qemu_error_sink
;
3335 qemu_error_sink
= sink
;
3338 void qemu_errors_to_previous(void)
3340 QemuErrorSink
*sink
;
3342 assert(qemu_error_sink
!= NULL
);
3343 sink
= qemu_error_sink
;
3344 qemu_error_sink
= sink
->previous
;
3348 void qemu_error(const char *fmt
, ...)
3352 assert(qemu_error_sink
!= NULL
);
3353 switch (qemu_error_sink
->dest
) {
3355 va_start(args
, fmt
);
3356 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3359 case ERR_SINK_MONITOR
:
3360 va_start(args
, fmt
);
3361 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);