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 char *device
, const char *target
,
523 if (strcmp(device
, "vnc") == 0) {
524 do_change_vnc(mon
, target
, arg
);
526 do_change_block(mon
, device
, target
, arg
);
530 static void do_screen_dump(Monitor
*mon
, const QDict
*qdict
)
532 vga_hw_screen_dump(qdict_get_str(qdict
, "filename"));
535 static void do_logfile(Monitor
*mon
, const QDict
*qdict
)
537 cpu_set_log_filename(qdict_get_str(qdict
, "filename"));
540 static void do_log(Monitor
*mon
, const QDict
*qdict
)
543 const char *items
= qdict_get_str(qdict
, "items");
545 if (!strcmp(items
, "none")) {
548 mask
= cpu_str_to_log_mask(items
);
550 help_cmd(mon
, "log");
557 static void do_singlestep(Monitor
*mon
, const QDict
*qdict
)
559 const char *option
= qdict_get_try_str(qdict
, "option");
560 if (!option
|| !strcmp(option
, "on")) {
562 } else if (!strcmp(option
, "off")) {
565 monitor_printf(mon
, "unexpected option %s\n", option
);
569 static void do_stop(Monitor
*mon
, const QDict
*qdict
)
571 vm_stop(EXCP_INTERRUPT
);
574 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
576 struct bdrv_iterate_context
{
581 static void do_cont(Monitor
*mon
, const QDict
*qdict
)
583 struct bdrv_iterate_context context
= { mon
, 0 };
585 bdrv_iterate(encrypted_bdrv_it
, &context
);
586 /* only resume the vm if all keys are set and valid */
591 static void bdrv_key_cb(void *opaque
, int err
)
593 Monitor
*mon
= opaque
;
595 /* another key was set successfully, retry to continue */
600 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
602 struct bdrv_iterate_context
*context
= opaque
;
604 if (!context
->err
&& bdrv_key_required(bs
)) {
605 context
->err
= -EBUSY
;
606 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
611 static void do_gdbserver(Monitor
*mon
, const QDict
*qdict
)
613 const char *device
= qdict_get_try_str(qdict
, "device");
615 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
616 if (gdbserver_start(device
) < 0) {
617 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
619 } else if (strcmp(device
, "none") == 0) {
620 monitor_printf(mon
, "Disabled gdbserver\n");
622 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
627 static void do_watchdog_action(Monitor
*mon
, const QDict
*qdict
)
629 const char *action
= qdict_get_str(qdict
, "action");
630 if (select_watchdog_action(action
) == -1) {
631 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
635 static void monitor_printc(Monitor
*mon
, int c
)
637 monitor_printf(mon
, "'");
640 monitor_printf(mon
, "\\'");
643 monitor_printf(mon
, "\\\\");
646 monitor_printf(mon
, "\\n");
649 monitor_printf(mon
, "\\r");
652 if (c
>= 32 && c
<= 126) {
653 monitor_printf(mon
, "%c", c
);
655 monitor_printf(mon
, "\\x%02x", c
);
659 monitor_printf(mon
, "'");
662 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
663 target_phys_addr_t addr
, int is_physical
)
666 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
674 if (!env
&& !is_physical
)
679 } else if (wsize
== 4) {
682 /* as default we use the current CS size */
686 if ((env
->efer
& MSR_EFER_LMA
) &&
687 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
691 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
696 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
705 nb_per_line
= line_size
/ wsize
;
710 max_digits
= (wsize
* 8 + 2) / 3;
714 max_digits
= (wsize
* 8) / 4;
718 max_digits
= (wsize
* 8 * 10 + 32) / 33;
727 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
729 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
734 cpu_physical_memory_rw(addr
, buf
, l
, 0);
739 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
740 monitor_printf(mon
, " Cannot access memory\n");
749 v
= ldub_raw(buf
+ i
);
752 v
= lduw_raw(buf
+ i
);
755 v
= (uint32_t)ldl_raw(buf
+ i
);
758 v
= ldq_raw(buf
+ i
);
761 monitor_printf(mon
, " ");
764 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
767 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
770 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
773 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
776 monitor_printc(mon
, v
);
781 monitor_printf(mon
, "\n");
787 #if TARGET_LONG_BITS == 64
788 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
790 #define GET_TLONG(h, l) (l)
793 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
794 uint32_t addrh
, uint32_t addrl
)
796 target_long addr
= GET_TLONG(addrh
, addrl
);
797 memory_dump(mon
, count
, format
, size
, addr
, 0);
800 #if TARGET_PHYS_ADDR_BITS > 32
801 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
803 #define GET_TPHYSADDR(h, l) (l)
806 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
807 int size
, uint32_t addrh
, uint32_t addrl
)
810 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
811 memory_dump(mon
, count
, format
, size
, addr
, 1);
814 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
815 unsigned int valh
, unsigned int vall
)
817 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
818 #if TARGET_PHYS_ADDR_BITS == 32
821 monitor_printf(mon
, "%#o", val
);
824 monitor_printf(mon
, "%#x", val
);
827 monitor_printf(mon
, "%u", val
);
831 monitor_printf(mon
, "%d", val
);
834 monitor_printc(mon
, val
);
840 monitor_printf(mon
, "%#" PRIo64
, val
);
843 monitor_printf(mon
, "%#" PRIx64
, val
);
846 monitor_printf(mon
, "%" PRIu64
, val
);
850 monitor_printf(mon
, "%" PRId64
, val
);
853 monitor_printc(mon
, val
);
857 monitor_printf(mon
, "\n");
860 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
861 uint32_t size
, const char *filename
)
864 target_long addr
= GET_TLONG(valh
, vall
);
873 f
= fopen(filename
, "wb");
875 monitor_printf(mon
, "could not open '%s'\n", filename
);
882 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
883 fwrite(buf
, 1, l
, f
);
890 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
891 unsigned int vall
, uint32_t size
,
892 const char *filename
)
897 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
899 f
= fopen(filename
, "wb");
901 monitor_printf(mon
, "could not open '%s'\n", filename
);
908 cpu_physical_memory_rw(addr
, buf
, l
, 0);
909 fwrite(buf
, 1, l
, f
);
917 static void do_sum(Monitor
*mon
, const QDict
*qdict
)
922 uint32_t start
= qdict_get_int(qdict
, "start");
923 uint32_t size
= qdict_get_int(qdict
, "size");
926 for(addr
= start
; addr
< (start
+ size
); addr
++) {
927 cpu_physical_memory_rw(addr
, buf
, 1, 0);
928 /* BSD sum algorithm ('sum' Unix command) */
929 sum
= (sum
>> 1) | (sum
<< 15);
932 monitor_printf(mon
, "%05d\n", sum
);
940 static const KeyDef key_defs
[] = {
967 { 0x0e, "backspace" },
1004 { 0x37, "asterisk" },
1007 { 0x3a, "caps_lock" },
1018 { 0x45, "num_lock" },
1019 { 0x46, "scroll_lock" },
1021 { 0xb5, "kp_divide" },
1022 { 0x37, "kp_multiply" },
1023 { 0x4a, "kp_subtract" },
1025 { 0x9c, "kp_enter" },
1026 { 0x53, "kp_decimal" },
1059 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1074 { 0xfe, "compose" },
1079 static int get_keycode(const char *key
)
1085 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1086 if (!strcmp(key
, p
->name
))
1089 if (strstart(key
, "0x", NULL
)) {
1090 ret
= strtoul(key
, &endp
, 0);
1091 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1097 #define MAX_KEYCODES 16
1098 static uint8_t keycodes
[MAX_KEYCODES
];
1099 static int nb_pending_keycodes
;
1100 static QEMUTimer
*key_timer
;
1102 static void release_keys(void *opaque
)
1106 while (nb_pending_keycodes
> 0) {
1107 nb_pending_keycodes
--;
1108 keycode
= keycodes
[nb_pending_keycodes
];
1110 kbd_put_keycode(0xe0);
1111 kbd_put_keycode(keycode
| 0x80);
1115 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1118 char keyname_buf
[16];
1120 int keyname_len
, keycode
, i
;
1122 if (nb_pending_keycodes
> 0) {
1123 qemu_del_timer(key_timer
);
1130 separator
= strchr(string
, '-');
1131 keyname_len
= separator
? separator
- string
: strlen(string
);
1132 if (keyname_len
> 0) {
1133 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1134 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1135 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1138 if (i
== MAX_KEYCODES
) {
1139 monitor_printf(mon
, "too many keys\n");
1142 keyname_buf
[keyname_len
] = 0;
1143 keycode
= get_keycode(keyname_buf
);
1145 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1148 keycodes
[i
++] = keycode
;
1152 string
= separator
+ 1;
1154 nb_pending_keycodes
= i
;
1155 /* key down events */
1156 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1157 keycode
= keycodes
[i
];
1159 kbd_put_keycode(0xe0);
1160 kbd_put_keycode(keycode
& 0x7f);
1162 /* delayed key up events */
1163 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1164 muldiv64(ticks_per_sec
, hold_time
, 1000));
1167 static int mouse_button_state
;
1169 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1173 dx
= strtol(dx_str
, NULL
, 0);
1174 dy
= strtol(dy_str
, NULL
, 0);
1177 dz
= strtol(dz_str
, NULL
, 0);
1178 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1181 static void do_mouse_button(Monitor
*mon
, const QDict
*qdict
)
1183 int button_state
= qdict_get_int(qdict
, "button_state");
1184 mouse_button_state
= button_state
;
1185 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1188 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1189 int addr
, int has_index
, int index
)
1195 cpu_outb(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1203 val
= cpu_inb(NULL
, addr
);
1207 val
= cpu_inw(NULL
, addr
);
1211 val
= cpu_inl(NULL
, addr
);
1215 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1216 suffix
, addr
, size
* 2, val
);
1219 static void do_ioport_write(Monitor
*mon
, int count
, int format
, int size
,
1222 addr
&= IOPORTS_MASK
;
1227 cpu_outb(NULL
, addr
, val
);
1230 cpu_outw(NULL
, addr
, val
);
1233 cpu_outl(NULL
, addr
, val
);
1238 static void do_boot_set(Monitor
*mon
, const QDict
*qdict
)
1241 const char *bootdevice
= qdict_get_str(qdict
, "bootdevice");
1243 res
= qemu_boot_set(bootdevice
);
1245 monitor_printf(mon
, "boot device list now set to %s\n", bootdevice
);
1246 } else if (res
> 0) {
1247 monitor_printf(mon
, "setting boot device list failed\n");
1249 monitor_printf(mon
, "no function defined to set boot device list for "
1250 "this architecture\n");
1254 static void do_system_reset(Monitor
*mon
, const QDict
*qdict
)
1256 qemu_system_reset_request();
1259 static void do_system_powerdown(Monitor
*mon
, const QDict
*qdict
)
1261 qemu_system_powerdown_request();
1264 #if defined(TARGET_I386)
1265 static void print_pte(Monitor
*mon
, uint32_t addr
, uint32_t pte
, uint32_t mask
)
1267 monitor_printf(mon
, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1270 pte
& PG_GLOBAL_MASK
? 'G' : '-',
1271 pte
& PG_PSE_MASK
? 'P' : '-',
1272 pte
& PG_DIRTY_MASK
? 'D' : '-',
1273 pte
& PG_ACCESSED_MASK
? 'A' : '-',
1274 pte
& PG_PCD_MASK
? 'C' : '-',
1275 pte
& PG_PWT_MASK
? 'T' : '-',
1276 pte
& PG_USER_MASK
? 'U' : '-',
1277 pte
& PG_RW_MASK
? 'W' : '-');
1280 static void tlb_info(Monitor
*mon
)
1284 uint32_t pgd
, pde
, pte
;
1286 env
= mon_get_cpu();
1290 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1291 monitor_printf(mon
, "PG disabled\n");
1294 pgd
= env
->cr
[3] & ~0xfff;
1295 for(l1
= 0; l1
< 1024; l1
++) {
1296 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1297 pde
= le32_to_cpu(pde
);
1298 if (pde
& PG_PRESENT_MASK
) {
1299 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1300 print_pte(mon
, (l1
<< 22), pde
, ~((1 << 20) - 1));
1302 for(l2
= 0; l2
< 1024; l2
++) {
1303 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1304 (uint8_t *)&pte
, 4);
1305 pte
= le32_to_cpu(pte
);
1306 if (pte
& PG_PRESENT_MASK
) {
1307 print_pte(mon
, (l1
<< 22) + (l2
<< 12),
1317 static void mem_print(Monitor
*mon
, uint32_t *pstart
, int *plast_prot
,
1318 uint32_t end
, int prot
)
1321 prot1
= *plast_prot
;
1322 if (prot
!= prot1
) {
1323 if (*pstart
!= -1) {
1324 monitor_printf(mon
, "%08x-%08x %08x %c%c%c\n",
1325 *pstart
, end
, end
- *pstart
,
1326 prot1
& PG_USER_MASK
? 'u' : '-',
1328 prot1
& PG_RW_MASK
? 'w' : '-');
1338 static void mem_info(Monitor
*mon
)
1341 int l1
, l2
, prot
, last_prot
;
1342 uint32_t pgd
, pde
, pte
, start
, end
;
1344 env
= mon_get_cpu();
1348 if (!(env
->cr
[0] & CR0_PG_MASK
)) {
1349 monitor_printf(mon
, "PG disabled\n");
1352 pgd
= env
->cr
[3] & ~0xfff;
1355 for(l1
= 0; l1
< 1024; l1
++) {
1356 cpu_physical_memory_read(pgd
+ l1
* 4, (uint8_t *)&pde
, 4);
1357 pde
= le32_to_cpu(pde
);
1359 if (pde
& PG_PRESENT_MASK
) {
1360 if ((pde
& PG_PSE_MASK
) && (env
->cr
[4] & CR4_PSE_MASK
)) {
1361 prot
= pde
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1362 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1364 for(l2
= 0; l2
< 1024; l2
++) {
1365 cpu_physical_memory_read((pde
& ~0xfff) + l2
* 4,
1366 (uint8_t *)&pte
, 4);
1367 pte
= le32_to_cpu(pte
);
1368 end
= (l1
<< 22) + (l2
<< 12);
1369 if (pte
& PG_PRESENT_MASK
) {
1370 prot
= pte
& (PG_USER_MASK
| PG_RW_MASK
| PG_PRESENT_MASK
);
1374 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1379 mem_print(mon
, &start
, &last_prot
, end
, prot
);
1385 #if defined(TARGET_SH4)
1387 static void print_tlb(Monitor
*mon
, int idx
, tlb_t
*tlb
)
1389 monitor_printf(mon
, " tlb%i:\t"
1390 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1391 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1392 "dirty=%hhu writethrough=%hhu\n",
1394 tlb
->asid
, tlb
->vpn
, tlb
->ppn
, tlb
->sz
, tlb
->size
,
1395 tlb
->v
, tlb
->sh
, tlb
->c
, tlb
->pr
,
1399 static void tlb_info(Monitor
*mon
)
1401 CPUState
*env
= mon_get_cpu();
1404 monitor_printf (mon
, "ITLB:\n");
1405 for (i
= 0 ; i
< ITLB_SIZE
; i
++)
1406 print_tlb (mon
, i
, &env
->itlb
[i
]);
1407 monitor_printf (mon
, "UTLB:\n");
1408 for (i
= 0 ; i
< UTLB_SIZE
; i
++)
1409 print_tlb (mon
, i
, &env
->utlb
[i
]);
1414 static void do_info_kvm(Monitor
*mon
)
1417 monitor_printf(mon
, "kvm support: ");
1419 monitor_printf(mon
, "enabled\n");
1421 monitor_printf(mon
, "disabled\n");
1423 monitor_printf(mon
, "kvm support: not compiled\n");
1427 static void do_info_numa(Monitor
*mon
)
1432 monitor_printf(mon
, "%d nodes\n", nb_numa_nodes
);
1433 for (i
= 0; i
< nb_numa_nodes
; i
++) {
1434 monitor_printf(mon
, "node %d cpus:", i
);
1435 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1436 if (env
->numa_node
== i
) {
1437 monitor_printf(mon
, " %d", env
->cpu_index
);
1440 monitor_printf(mon
, "\n");
1441 monitor_printf(mon
, "node %d size: %" PRId64
" MB\n", i
,
1446 #ifdef CONFIG_PROFILER
1448 static void do_info_profile(Monitor
*mon
)
1454 monitor_printf(mon
, "async time %" PRId64
" (%0.3f)\n",
1455 dev_time
, dev_time
/ (double)ticks_per_sec
);
1456 monitor_printf(mon
, "qemu time %" PRId64
" (%0.3f)\n",
1457 qemu_time
, qemu_time
/ (double)ticks_per_sec
);
1462 static void do_info_profile(Monitor
*mon
)
1464 monitor_printf(mon
, "Internal profiler not compiled\n");
1468 /* Capture support */
1469 static LIST_HEAD (capture_list_head
, CaptureState
) capture_head
;
1471 static void do_info_capture(Monitor
*mon
)
1476 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1477 monitor_printf(mon
, "[%d]: ", i
);
1478 s
->ops
.info (s
->opaque
);
1483 static void do_stop_capture(Monitor
*mon
, const QDict
*qdict
)
1486 int n
= qdict_get_int(qdict
, "n");
1489 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1491 s
->ops
.destroy (s
->opaque
);
1492 LIST_REMOVE (s
, entries
);
1499 static void do_wav_capture(Monitor
*mon
, const char *path
,
1500 int has_freq
, int freq
,
1501 int has_bits
, int bits
,
1502 int has_channels
, int nchannels
)
1506 s
= qemu_mallocz (sizeof (*s
));
1508 freq
= has_freq
? freq
: 44100;
1509 bits
= has_bits
? bits
: 16;
1510 nchannels
= has_channels
? nchannels
: 2;
1512 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1513 monitor_printf(mon
, "Faied to add wave capture\n");
1516 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1520 #if defined(TARGET_I386)
1521 static void do_inject_nmi(Monitor
*mon
, const QDict
*qdict
)
1524 int cpu_index
= qdict_get_int(qdict
, "cpu_index");
1526 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1527 if (env
->cpu_index
== cpu_index
) {
1528 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1534 static void do_info_status(Monitor
*mon
)
1538 monitor_printf(mon
, "VM status: running (single step mode)\n");
1540 monitor_printf(mon
, "VM status: running\n");
1543 monitor_printf(mon
, "VM status: paused\n");
1547 static void do_balloon(Monitor
*mon
, const QDict
*qdict
)
1549 int value
= qdict_get_int(qdict
, "value");
1550 ram_addr_t target
= value
;
1551 qemu_balloon(target
<< 20);
1554 static void do_info_balloon(Monitor
*mon
)
1558 actual
= qemu_balloon_status();
1559 if (kvm_enabled() && !kvm_has_sync_mmu())
1560 monitor_printf(mon
, "Using KVM without synchronous MMU, "
1561 "ballooning disabled\n");
1562 else if (actual
== 0)
1563 monitor_printf(mon
, "Ballooning not activated in VM\n");
1565 monitor_printf(mon
, "balloon: actual=%d\n", (int)(actual
>> 20));
1568 static qemu_acl
*find_acl(Monitor
*mon
, const char *name
)
1570 qemu_acl
*acl
= qemu_acl_find(name
);
1573 monitor_printf(mon
, "acl: unknown list '%s'\n", name
);
1578 static void do_acl_show(Monitor
*mon
, const QDict
*qdict
)
1580 const char *aclname
= qdict_get_str(qdict
, "aclname");
1581 qemu_acl
*acl
= find_acl(mon
, aclname
);
1582 qemu_acl_entry
*entry
;
1586 monitor_printf(mon
, "policy: %s\n",
1587 acl
->defaultDeny
? "deny" : "allow");
1588 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1590 monitor_printf(mon
, "%d: %s %s\n", i
,
1591 entry
->deny
? "deny" : "allow", entry
->match
);
1596 static void do_acl_reset(Monitor
*mon
, const QDict
*qdict
)
1598 const char *aclname
= qdict_get_str(qdict
, "aclname");
1599 qemu_acl
*acl
= find_acl(mon
, aclname
);
1602 qemu_acl_reset(acl
);
1603 monitor_printf(mon
, "acl: removed all rules\n");
1607 static void do_acl_policy(Monitor
*mon
, const QDict
*qdict
)
1609 const char *aclname
= qdict_get_str(qdict
, "aclname");
1610 const char *policy
= qdict_get_str(qdict
, "policy");
1611 qemu_acl
*acl
= find_acl(mon
, aclname
);
1614 if (strcmp(policy
, "allow") == 0) {
1615 acl
->defaultDeny
= 0;
1616 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1617 } else if (strcmp(policy
, "deny") == 0) {
1618 acl
->defaultDeny
= 1;
1619 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1621 monitor_printf(mon
, "acl: unknown policy '%s', "
1622 "expected 'deny' or 'allow'\n", policy
);
1627 static void do_acl_add(Monitor
*mon
, const char *aclname
,
1628 const char *match
, const char *policy
,
1629 int has_index
, int index
)
1631 qemu_acl
*acl
= find_acl(mon
, aclname
);
1635 if (strcmp(policy
, "allow") == 0) {
1637 } else if (strcmp(policy
, "deny") == 0) {
1640 monitor_printf(mon
, "acl: unknown policy '%s', "
1641 "expected 'deny' or 'allow'\n", policy
);
1645 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1647 ret
= qemu_acl_append(acl
, deny
, match
);
1649 monitor_printf(mon
, "acl: unable to add acl entry\n");
1651 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1655 static void do_acl_remove(Monitor
*mon
, const QDict
*qdict
)
1657 const char *aclname
= qdict_get_str(qdict
, "aclname");
1658 const char *match
= qdict_get_str(qdict
, "match");
1659 qemu_acl
*acl
= find_acl(mon
, aclname
);
1663 ret
= qemu_acl_remove(acl
, match
);
1665 monitor_printf(mon
, "acl: no matching acl entry\n");
1667 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1671 #if defined(TARGET_I386)
1672 static void do_inject_mce(Monitor
*mon
,
1673 int cpu_index
, int bank
,
1674 unsigned status_hi
, unsigned status_lo
,
1675 unsigned mcg_status_hi
, unsigned mcg_status_lo
,
1676 unsigned addr_hi
, unsigned addr_lo
,
1677 unsigned misc_hi
, unsigned misc_lo
)
1680 uint64_t status
= ((uint64_t)status_hi
<< 32) | status_lo
;
1681 uint64_t mcg_status
= ((uint64_t)mcg_status_hi
<< 32) | mcg_status_lo
;
1682 uint64_t addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1683 uint64_t misc
= ((uint64_t)misc_hi
<< 32) | misc_lo
;
1685 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1686 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1687 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1693 static void do_getfd(Monitor
*mon
, const QDict
*qdict
)
1695 const char *fdname
= qdict_get_str(qdict
, "fdname");
1699 fd
= qemu_chr_get_msgfd(mon
->chr
);
1701 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1705 if (qemu_isdigit(fdname
[0])) {
1706 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1712 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1717 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1718 if (strcmp(monfd
->name
, fdname
) != 0) {
1727 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1728 monfd
->name
= qemu_strdup(fdname
);
1731 LIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1734 static void do_closefd(Monitor
*mon
, const QDict
*qdict
)
1736 const char *fdname
= qdict_get_str(qdict
, "fdname");
1739 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1740 if (strcmp(monfd
->name
, fdname
) != 0) {
1744 LIST_REMOVE(monfd
, next
);
1746 qemu_free(monfd
->name
);
1751 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1755 static void do_loadvm(Monitor
*mon
, const QDict
*qdict
)
1757 int saved_vm_running
= vm_running
;
1758 const char *name
= qdict_get_str(qdict
, "name");
1762 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1766 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1770 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1773 if (strcmp(monfd
->name
, fdname
) != 0) {
1779 /* caller takes ownership of fd */
1780 LIST_REMOVE(monfd
, next
);
1781 qemu_free(monfd
->name
);
1790 static const mon_cmd_t mon_cmds
[] = {
1791 #include "qemu-monitor.h"
1795 /* Please update qemu-monitor.hx when adding or changing commands */
1796 static const mon_cmd_t info_cmds
[] = {
1797 { "version", "", do_info_version
,
1798 "", "show the version of QEMU" },
1799 { "network", "", do_info_network
,
1800 "", "show the network state" },
1801 { "chardev", "", qemu_chr_info
,
1802 "", "show the character devices" },
1803 { "block", "", bdrv_info
,
1804 "", "show the block devices" },
1805 { "blockstats", "", bdrv_info_stats
,
1806 "", "show block device statistics" },
1807 { "registers", "", do_info_registers
,
1808 "", "show the cpu registers" },
1809 { "cpus", "", do_info_cpus
,
1810 "", "show infos for each CPU" },
1811 { "history", "", do_info_history
,
1812 "", "show the command line history", },
1813 { "irq", "", irq_info
,
1814 "", "show the interrupts statistics (if available)", },
1815 { "pic", "", pic_info
,
1816 "", "show i8259 (PIC) state", },
1817 { "pci", "", pci_info
,
1818 "", "show PCI info", },
1819 #if defined(TARGET_I386) || defined(TARGET_SH4)
1820 { "tlb", "", tlb_info
,
1821 "", "show virtual to physical memory mappings", },
1823 #if defined(TARGET_I386)
1824 { "mem", "", mem_info
,
1825 "", "show the active virtual memory mappings", },
1826 { "hpet", "", do_info_hpet
,
1827 "", "show state of HPET", },
1829 { "jit", "", do_info_jit
,
1830 "", "show dynamic compiler info", },
1831 { "kvm", "", do_info_kvm
,
1832 "", "show KVM information", },
1833 { "numa", "", do_info_numa
,
1834 "", "show NUMA information", },
1835 { "usb", "", usb_info
,
1836 "", "show guest USB devices", },
1837 { "usbhost", "", usb_host_info
,
1838 "", "show host USB devices", },
1839 { "profile", "", do_info_profile
,
1840 "", "show profiling information", },
1841 { "capture", "", do_info_capture
,
1842 "", "show capture information" },
1843 { "snapshots", "", do_info_snapshots
,
1844 "", "show the currently saved VM snapshots" },
1845 { "status", "", do_info_status
,
1846 "", "show the current VM status (running|paused)" },
1847 { "pcmcia", "", pcmcia_info
,
1848 "", "show guest PCMCIA status" },
1849 { "mice", "", do_info_mice
,
1850 "", "show which guest mouse is receiving events" },
1851 { "vnc", "", do_info_vnc
,
1852 "", "show the vnc server status"},
1853 { "name", "", do_info_name
,
1854 "", "show the current VM name" },
1855 { "uuid", "", do_info_uuid
,
1856 "", "show the current VM UUID" },
1857 #if defined(TARGET_PPC)
1858 { "cpustats", "", do_info_cpu_stats
,
1859 "", "show CPU statistics", },
1861 #if defined(CONFIG_SLIRP)
1862 { "usernet", "", do_info_usernet
,
1863 "", "show user network stack connection states", },
1865 { "migrate", "", do_info_migrate
, "", "show migration status" },
1866 { "balloon", "", do_info_balloon
,
1867 "", "show balloon information" },
1868 { "qtree", "", do_info_qtree
,
1869 "", "show device tree" },
1870 { "qdm", "", do_info_qdm
,
1871 "", "show qdev device model list" },
1875 /*******************************************************************/
1877 static const char *pch
;
1878 static jmp_buf expr_env
;
1883 typedef struct MonitorDef
{
1886 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1890 #if defined(TARGET_I386)
1891 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1893 CPUState
*env
= mon_get_cpu();
1896 return env
->eip
+ env
->segs
[R_CS
].base
;
1900 #if defined(TARGET_PPC)
1901 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1903 CPUState
*env
= mon_get_cpu();
1911 for (i
= 0; i
< 8; i
++)
1912 u
|= env
->crf
[i
] << (32 - (4 * i
));
1917 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1919 CPUState
*env
= mon_get_cpu();
1925 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1927 CPUState
*env
= mon_get_cpu();
1933 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1935 CPUState
*env
= mon_get_cpu();
1938 return cpu_ppc_load_decr(env
);
1941 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1943 CPUState
*env
= mon_get_cpu();
1946 return cpu_ppc_load_tbu(env
);
1949 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1951 CPUState
*env
= mon_get_cpu();
1954 return cpu_ppc_load_tbl(env
);
1958 #if defined(TARGET_SPARC)
1959 #ifndef TARGET_SPARC64
1960 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1962 CPUState
*env
= mon_get_cpu();
1965 return GET_PSR(env
);
1969 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1971 CPUState
*env
= mon_get_cpu();
1974 return env
->regwptr
[val
];
1978 static const MonitorDef monitor_defs
[] = {
1981 #define SEG(name, seg) \
1982 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1983 { name ".base", offsetof(CPUState, segs[seg].base) },\
1984 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1986 { "eax", offsetof(CPUState
, regs
[0]) },
1987 { "ecx", offsetof(CPUState
, regs
[1]) },
1988 { "edx", offsetof(CPUState
, regs
[2]) },
1989 { "ebx", offsetof(CPUState
, regs
[3]) },
1990 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1991 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1992 { "esi", offsetof(CPUState
, regs
[6]) },
1993 { "edi", offsetof(CPUState
, regs
[7]) },
1994 #ifdef TARGET_X86_64
1995 { "r8", offsetof(CPUState
, regs
[8]) },
1996 { "r9", offsetof(CPUState
, regs
[9]) },
1997 { "r10", offsetof(CPUState
, regs
[10]) },
1998 { "r11", offsetof(CPUState
, regs
[11]) },
1999 { "r12", offsetof(CPUState
, regs
[12]) },
2000 { "r13", offsetof(CPUState
, regs
[13]) },
2001 { "r14", offsetof(CPUState
, regs
[14]) },
2002 { "r15", offsetof(CPUState
, regs
[15]) },
2004 { "eflags", offsetof(CPUState
, eflags
) },
2005 { "eip", offsetof(CPUState
, eip
) },
2012 { "pc", 0, monitor_get_pc
, },
2013 #elif defined(TARGET_PPC)
2014 /* General purpose registers */
2015 { "r0", offsetof(CPUState
, gpr
[0]) },
2016 { "r1", offsetof(CPUState
, gpr
[1]) },
2017 { "r2", offsetof(CPUState
, gpr
[2]) },
2018 { "r3", offsetof(CPUState
, gpr
[3]) },
2019 { "r4", offsetof(CPUState
, gpr
[4]) },
2020 { "r5", offsetof(CPUState
, gpr
[5]) },
2021 { "r6", offsetof(CPUState
, gpr
[6]) },
2022 { "r7", offsetof(CPUState
, gpr
[7]) },
2023 { "r8", offsetof(CPUState
, gpr
[8]) },
2024 { "r9", offsetof(CPUState
, gpr
[9]) },
2025 { "r10", offsetof(CPUState
, gpr
[10]) },
2026 { "r11", offsetof(CPUState
, gpr
[11]) },
2027 { "r12", offsetof(CPUState
, gpr
[12]) },
2028 { "r13", offsetof(CPUState
, gpr
[13]) },
2029 { "r14", offsetof(CPUState
, gpr
[14]) },
2030 { "r15", offsetof(CPUState
, gpr
[15]) },
2031 { "r16", offsetof(CPUState
, gpr
[16]) },
2032 { "r17", offsetof(CPUState
, gpr
[17]) },
2033 { "r18", offsetof(CPUState
, gpr
[18]) },
2034 { "r19", offsetof(CPUState
, gpr
[19]) },
2035 { "r20", offsetof(CPUState
, gpr
[20]) },
2036 { "r21", offsetof(CPUState
, gpr
[21]) },
2037 { "r22", offsetof(CPUState
, gpr
[22]) },
2038 { "r23", offsetof(CPUState
, gpr
[23]) },
2039 { "r24", offsetof(CPUState
, gpr
[24]) },
2040 { "r25", offsetof(CPUState
, gpr
[25]) },
2041 { "r26", offsetof(CPUState
, gpr
[26]) },
2042 { "r27", offsetof(CPUState
, gpr
[27]) },
2043 { "r28", offsetof(CPUState
, gpr
[28]) },
2044 { "r29", offsetof(CPUState
, gpr
[29]) },
2045 { "r30", offsetof(CPUState
, gpr
[30]) },
2046 { "r31", offsetof(CPUState
, gpr
[31]) },
2047 /* Floating point registers */
2048 { "f0", offsetof(CPUState
, fpr
[0]) },
2049 { "f1", offsetof(CPUState
, fpr
[1]) },
2050 { "f2", offsetof(CPUState
, fpr
[2]) },
2051 { "f3", offsetof(CPUState
, fpr
[3]) },
2052 { "f4", offsetof(CPUState
, fpr
[4]) },
2053 { "f5", offsetof(CPUState
, fpr
[5]) },
2054 { "f6", offsetof(CPUState
, fpr
[6]) },
2055 { "f7", offsetof(CPUState
, fpr
[7]) },
2056 { "f8", offsetof(CPUState
, fpr
[8]) },
2057 { "f9", offsetof(CPUState
, fpr
[9]) },
2058 { "f10", offsetof(CPUState
, fpr
[10]) },
2059 { "f11", offsetof(CPUState
, fpr
[11]) },
2060 { "f12", offsetof(CPUState
, fpr
[12]) },
2061 { "f13", offsetof(CPUState
, fpr
[13]) },
2062 { "f14", offsetof(CPUState
, fpr
[14]) },
2063 { "f15", offsetof(CPUState
, fpr
[15]) },
2064 { "f16", offsetof(CPUState
, fpr
[16]) },
2065 { "f17", offsetof(CPUState
, fpr
[17]) },
2066 { "f18", offsetof(CPUState
, fpr
[18]) },
2067 { "f19", offsetof(CPUState
, fpr
[19]) },
2068 { "f20", offsetof(CPUState
, fpr
[20]) },
2069 { "f21", offsetof(CPUState
, fpr
[21]) },
2070 { "f22", offsetof(CPUState
, fpr
[22]) },
2071 { "f23", offsetof(CPUState
, fpr
[23]) },
2072 { "f24", offsetof(CPUState
, fpr
[24]) },
2073 { "f25", offsetof(CPUState
, fpr
[25]) },
2074 { "f26", offsetof(CPUState
, fpr
[26]) },
2075 { "f27", offsetof(CPUState
, fpr
[27]) },
2076 { "f28", offsetof(CPUState
, fpr
[28]) },
2077 { "f29", offsetof(CPUState
, fpr
[29]) },
2078 { "f30", offsetof(CPUState
, fpr
[30]) },
2079 { "f31", offsetof(CPUState
, fpr
[31]) },
2080 { "fpscr", offsetof(CPUState
, fpscr
) },
2081 /* Next instruction pointer */
2082 { "nip|pc", offsetof(CPUState
, nip
) },
2083 { "lr", offsetof(CPUState
, lr
) },
2084 { "ctr", offsetof(CPUState
, ctr
) },
2085 { "decr", 0, &monitor_get_decr
, },
2086 { "ccr", 0, &monitor_get_ccr
, },
2087 /* Machine state register */
2088 { "msr", 0, &monitor_get_msr
, },
2089 { "xer", 0, &monitor_get_xer
, },
2090 { "tbu", 0, &monitor_get_tbu
, },
2091 { "tbl", 0, &monitor_get_tbl
, },
2092 #if defined(TARGET_PPC64)
2093 /* Address space register */
2094 { "asr", offsetof(CPUState
, asr
) },
2096 /* Segment registers */
2097 { "sdr1", offsetof(CPUState
, sdr1
) },
2098 { "sr0", offsetof(CPUState
, sr
[0]) },
2099 { "sr1", offsetof(CPUState
, sr
[1]) },
2100 { "sr2", offsetof(CPUState
, sr
[2]) },
2101 { "sr3", offsetof(CPUState
, sr
[3]) },
2102 { "sr4", offsetof(CPUState
, sr
[4]) },
2103 { "sr5", offsetof(CPUState
, sr
[5]) },
2104 { "sr6", offsetof(CPUState
, sr
[6]) },
2105 { "sr7", offsetof(CPUState
, sr
[7]) },
2106 { "sr8", offsetof(CPUState
, sr
[8]) },
2107 { "sr9", offsetof(CPUState
, sr
[9]) },
2108 { "sr10", offsetof(CPUState
, sr
[10]) },
2109 { "sr11", offsetof(CPUState
, sr
[11]) },
2110 { "sr12", offsetof(CPUState
, sr
[12]) },
2111 { "sr13", offsetof(CPUState
, sr
[13]) },
2112 { "sr14", offsetof(CPUState
, sr
[14]) },
2113 { "sr15", offsetof(CPUState
, sr
[15]) },
2114 /* Too lazy to put BATs and SPRs ... */
2115 #elif defined(TARGET_SPARC)
2116 { "g0", offsetof(CPUState
, gregs
[0]) },
2117 { "g1", offsetof(CPUState
, gregs
[1]) },
2118 { "g2", offsetof(CPUState
, gregs
[2]) },
2119 { "g3", offsetof(CPUState
, gregs
[3]) },
2120 { "g4", offsetof(CPUState
, gregs
[4]) },
2121 { "g5", offsetof(CPUState
, gregs
[5]) },
2122 { "g6", offsetof(CPUState
, gregs
[6]) },
2123 { "g7", offsetof(CPUState
, gregs
[7]) },
2124 { "o0", 0, monitor_get_reg
},
2125 { "o1", 1, monitor_get_reg
},
2126 { "o2", 2, monitor_get_reg
},
2127 { "o3", 3, monitor_get_reg
},
2128 { "o4", 4, monitor_get_reg
},
2129 { "o5", 5, monitor_get_reg
},
2130 { "o6", 6, monitor_get_reg
},
2131 { "o7", 7, monitor_get_reg
},
2132 { "l0", 8, monitor_get_reg
},
2133 { "l1", 9, monitor_get_reg
},
2134 { "l2", 10, monitor_get_reg
},
2135 { "l3", 11, monitor_get_reg
},
2136 { "l4", 12, monitor_get_reg
},
2137 { "l5", 13, monitor_get_reg
},
2138 { "l6", 14, monitor_get_reg
},
2139 { "l7", 15, monitor_get_reg
},
2140 { "i0", 16, monitor_get_reg
},
2141 { "i1", 17, monitor_get_reg
},
2142 { "i2", 18, monitor_get_reg
},
2143 { "i3", 19, monitor_get_reg
},
2144 { "i4", 20, monitor_get_reg
},
2145 { "i5", 21, monitor_get_reg
},
2146 { "i6", 22, monitor_get_reg
},
2147 { "i7", 23, monitor_get_reg
},
2148 { "pc", offsetof(CPUState
, pc
) },
2149 { "npc", offsetof(CPUState
, npc
) },
2150 { "y", offsetof(CPUState
, y
) },
2151 #ifndef TARGET_SPARC64
2152 { "psr", 0, &monitor_get_psr
, },
2153 { "wim", offsetof(CPUState
, wim
) },
2155 { "tbr", offsetof(CPUState
, tbr
) },
2156 { "fsr", offsetof(CPUState
, fsr
) },
2157 { "f0", offsetof(CPUState
, fpr
[0]) },
2158 { "f1", offsetof(CPUState
, fpr
[1]) },
2159 { "f2", offsetof(CPUState
, fpr
[2]) },
2160 { "f3", offsetof(CPUState
, fpr
[3]) },
2161 { "f4", offsetof(CPUState
, fpr
[4]) },
2162 { "f5", offsetof(CPUState
, fpr
[5]) },
2163 { "f6", offsetof(CPUState
, fpr
[6]) },
2164 { "f7", offsetof(CPUState
, fpr
[7]) },
2165 { "f8", offsetof(CPUState
, fpr
[8]) },
2166 { "f9", offsetof(CPUState
, fpr
[9]) },
2167 { "f10", offsetof(CPUState
, fpr
[10]) },
2168 { "f11", offsetof(CPUState
, fpr
[11]) },
2169 { "f12", offsetof(CPUState
, fpr
[12]) },
2170 { "f13", offsetof(CPUState
, fpr
[13]) },
2171 { "f14", offsetof(CPUState
, fpr
[14]) },
2172 { "f15", offsetof(CPUState
, fpr
[15]) },
2173 { "f16", offsetof(CPUState
, fpr
[16]) },
2174 { "f17", offsetof(CPUState
, fpr
[17]) },
2175 { "f18", offsetof(CPUState
, fpr
[18]) },
2176 { "f19", offsetof(CPUState
, fpr
[19]) },
2177 { "f20", offsetof(CPUState
, fpr
[20]) },
2178 { "f21", offsetof(CPUState
, fpr
[21]) },
2179 { "f22", offsetof(CPUState
, fpr
[22]) },
2180 { "f23", offsetof(CPUState
, fpr
[23]) },
2181 { "f24", offsetof(CPUState
, fpr
[24]) },
2182 { "f25", offsetof(CPUState
, fpr
[25]) },
2183 { "f26", offsetof(CPUState
, fpr
[26]) },
2184 { "f27", offsetof(CPUState
, fpr
[27]) },
2185 { "f28", offsetof(CPUState
, fpr
[28]) },
2186 { "f29", offsetof(CPUState
, fpr
[29]) },
2187 { "f30", offsetof(CPUState
, fpr
[30]) },
2188 { "f31", offsetof(CPUState
, fpr
[31]) },
2189 #ifdef TARGET_SPARC64
2190 { "f32", offsetof(CPUState
, fpr
[32]) },
2191 { "f34", offsetof(CPUState
, fpr
[34]) },
2192 { "f36", offsetof(CPUState
, fpr
[36]) },
2193 { "f38", offsetof(CPUState
, fpr
[38]) },
2194 { "f40", offsetof(CPUState
, fpr
[40]) },
2195 { "f42", offsetof(CPUState
, fpr
[42]) },
2196 { "f44", offsetof(CPUState
, fpr
[44]) },
2197 { "f46", offsetof(CPUState
, fpr
[46]) },
2198 { "f48", offsetof(CPUState
, fpr
[48]) },
2199 { "f50", offsetof(CPUState
, fpr
[50]) },
2200 { "f52", offsetof(CPUState
, fpr
[52]) },
2201 { "f54", offsetof(CPUState
, fpr
[54]) },
2202 { "f56", offsetof(CPUState
, fpr
[56]) },
2203 { "f58", offsetof(CPUState
, fpr
[58]) },
2204 { "f60", offsetof(CPUState
, fpr
[60]) },
2205 { "f62", offsetof(CPUState
, fpr
[62]) },
2206 { "asi", offsetof(CPUState
, asi
) },
2207 { "pstate", offsetof(CPUState
, pstate
) },
2208 { "cansave", offsetof(CPUState
, cansave
) },
2209 { "canrestore", offsetof(CPUState
, canrestore
) },
2210 { "otherwin", offsetof(CPUState
, otherwin
) },
2211 { "wstate", offsetof(CPUState
, wstate
) },
2212 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2213 { "fprs", offsetof(CPUState
, fprs
) },
2219 static void expr_error(Monitor
*mon
, const char *msg
)
2221 monitor_printf(mon
, "%s\n", msg
);
2222 longjmp(expr_env
, 1);
2225 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2226 static int get_monitor_def(target_long
*pval
, const char *name
)
2228 const MonitorDef
*md
;
2231 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2232 if (compare_cmd(name
, md
->name
)) {
2233 if (md
->get_value
) {
2234 *pval
= md
->get_value(md
, md
->offset
);
2236 CPUState
*env
= mon_get_cpu();
2239 ptr
= (uint8_t *)env
+ md
->offset
;
2242 *pval
= *(int32_t *)ptr
;
2245 *pval
= *(target_long
*)ptr
;
2258 static void next(void)
2262 while (qemu_isspace(*pch
))
2267 static int64_t expr_sum(Monitor
*mon
);
2269 static int64_t expr_unary(Monitor
*mon
)
2278 n
= expr_unary(mon
);
2282 n
= -expr_unary(mon
);
2286 n
= ~expr_unary(mon
);
2292 expr_error(mon
, "')' expected");
2299 expr_error(mon
, "character constant expected");
2303 expr_error(mon
, "missing terminating \' character");
2313 while ((*pch
>= 'a' && *pch
<= 'z') ||
2314 (*pch
>= 'A' && *pch
<= 'Z') ||
2315 (*pch
>= '0' && *pch
<= '9') ||
2316 *pch
== '_' || *pch
== '.') {
2317 if ((q
- buf
) < sizeof(buf
) - 1)
2321 while (qemu_isspace(*pch
))
2324 ret
= get_monitor_def(®
, buf
);
2326 expr_error(mon
, "unknown register");
2328 expr_error(mon
, "no cpu defined");
2333 expr_error(mon
, "unexpected end of expression");
2337 #if TARGET_PHYS_ADDR_BITS > 32
2338 n
= strtoull(pch
, &p
, 0);
2340 n
= strtoul(pch
, &p
, 0);
2343 expr_error(mon
, "invalid char in expression");
2346 while (qemu_isspace(*pch
))
2354 static int64_t expr_prod(Monitor
*mon
)
2359 val
= expr_unary(mon
);
2362 if (op
!= '*' && op
!= '/' && op
!= '%')
2365 val2
= expr_unary(mon
);
2374 expr_error(mon
, "division by zero");
2385 static int64_t expr_logic(Monitor
*mon
)
2390 val
= expr_prod(mon
);
2393 if (op
!= '&' && op
!= '|' && op
!= '^')
2396 val2
= expr_prod(mon
);
2413 static int64_t expr_sum(Monitor
*mon
)
2418 val
= expr_logic(mon
);
2421 if (op
!= '+' && op
!= '-')
2424 val2
= expr_logic(mon
);
2433 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2436 if (setjmp(expr_env
)) {
2440 while (qemu_isspace(*pch
))
2442 *pval
= expr_sum(mon
);
2447 static int get_str(char *buf
, int buf_size
, const char **pp
)
2455 while (qemu_isspace(*p
))
2465 while (*p
!= '\0' && *p
!= '\"') {
2481 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2484 if ((q
- buf
) < buf_size
- 1) {
2488 if ((q
- buf
) < buf_size
- 1) {
2495 qemu_printf("unterminated string\n");
2500 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2501 if ((q
- buf
) < buf_size
- 1) {
2513 * Store the command-name in cmdname, and return a pointer to
2514 * the remaining of the command string.
2516 static const char *get_command_name(const char *cmdline
,
2517 char *cmdname
, size_t nlen
)
2520 const char *p
, *pstart
;
2523 while (qemu_isspace(*p
))
2528 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2533 memcpy(cmdname
, pstart
, len
);
2534 cmdname
[len
] = '\0';
2539 * Read key of 'type' into 'key' and return the current
2542 static char *key_get_info(const char *type
, char **key
)
2550 p
= strchr(type
, ':');
2557 str
= qemu_malloc(len
+ 1);
2558 memcpy(str
, type
, len
);
2565 static int default_fmt_format
= 'x';
2566 static int default_fmt_size
= 4;
2570 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2572 const char *p
, *typestr
;
2573 int c
, nb_args
, i
, has_arg
;
2574 const mon_cmd_t
*cmd
;
2579 void *str_allocated
[MAX_ARGS
];
2580 void *args
[MAX_ARGS
];
2581 void (*handler_d
)(Monitor
*mon
, const QDict
*qdict
);
2582 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2583 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2585 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2586 void *arg3
, void *arg4
);
2587 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2588 void *arg3
, void *arg4
, void *arg5
);
2589 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2590 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2591 void (*handler_8
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2592 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2594 void (*handler_9
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2595 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2596 void *arg7
, void *arg8
);
2597 void (*handler_10
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2598 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2599 void *arg7
, void *arg8
, void *arg9
);
2602 monitor_printf(mon
, "command='%s'\n", cmdline
);
2605 /* extract the command name */
2606 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2610 /* find the command */
2611 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2612 if (compare_cmd(cmdname
, cmd
->name
))
2616 if (cmd
->name
== NULL
) {
2617 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2621 qdict
= qdict_new();
2623 for(i
= 0; i
< MAX_ARGS
; i
++)
2624 str_allocated
[i
] = NULL
;
2626 /* parse the parameters */
2627 typestr
= cmd
->args_type
;
2630 typestr
= key_get_info(typestr
, &key
);
2643 while (qemu_isspace(*p
))
2645 if (*typestr
== '?') {
2648 /* no optional string: NULL argument */
2653 ret
= get_str(buf
, sizeof(buf
), &p
);
2657 monitor_printf(mon
, "%s: filename expected\n",
2661 monitor_printf(mon
, "%s: block device name expected\n",
2665 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2670 str
= qemu_malloc(strlen(buf
) + 1);
2671 pstrcpy(str
, sizeof(buf
), buf
);
2672 str_allocated
[nb_args
] = str
;
2674 if (nb_args
>= MAX_ARGS
) {
2676 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2679 args
[nb_args
++] = str
;
2681 qdict_put(qdict
, key
, qstring_from_str(str
));
2686 int count
, format
, size
;
2688 while (qemu_isspace(*p
))
2694 if (qemu_isdigit(*p
)) {
2696 while (qemu_isdigit(*p
)) {
2697 count
= count
* 10 + (*p
- '0');
2735 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2736 monitor_printf(mon
, "invalid char in format: '%c'\n",
2741 format
= default_fmt_format
;
2742 if (format
!= 'i') {
2743 /* for 'i', not specifying a size gives -1 as size */
2745 size
= default_fmt_size
;
2746 default_fmt_size
= size
;
2748 default_fmt_format
= format
;
2751 format
= default_fmt_format
;
2752 if (format
!= 'i') {
2753 size
= default_fmt_size
;
2758 if (nb_args
+ 3 > MAX_ARGS
)
2760 args
[nb_args
++] = (void*)(long)count
;
2761 args
[nb_args
++] = (void*)(long)format
;
2762 args
[nb_args
++] = (void*)(long)size
;
2763 qdict_put(qdict
, "count", qint_from_int(count
));
2764 qdict_put(qdict
, "format", qint_from_int(format
));
2765 qdict_put(qdict
, "size", qint_from_int(size
));
2774 while (qemu_isspace(*p
))
2776 if (*typestr
== '?' || *typestr
== '.') {
2777 if (*typestr
== '?') {
2785 while (qemu_isspace(*p
))
2793 if (nb_args
>= MAX_ARGS
)
2796 args
[nb_args
++] = (void *)(long)has_arg
;
2798 if (nb_args
>= MAX_ARGS
)
2804 if (get_expr(mon
, &val
, &p
))
2808 if (nb_args
>= MAX_ARGS
)
2810 args
[nb_args
++] = (void *)(long)val
;
2812 qdict_put(qdict
, key
, qint_from_int(val
));
2814 if ((nb_args
+ 1) >= MAX_ARGS
)
2816 #if TARGET_PHYS_ADDR_BITS > 32
2817 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2819 args
[nb_args
++] = (void *)0;
2821 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2822 qdict_put(qdict
, key
, qint_from_int(val
));
2834 while (qemu_isspace(*p
))
2840 monitor_printf(mon
, "%s: unsupported option -%c\n",
2847 if (nb_args
>= MAX_ARGS
)
2849 args
[nb_args
++] = (void *)(long)has_option
;
2850 qdict_put(qdict
, key
, qint_from_int(has_option
));
2855 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2861 /* check that all arguments were parsed */
2862 while (qemu_isspace(*p
))
2865 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2870 qemu_errors_to_mon(mon
);
2875 handler_d
= cmd
->handler
;
2876 handler_d(mon
, qdict
);
2879 handler_3
= cmd
->handler
;
2880 handler_3(mon
, args
[0], args
[1], args
[2]);
2883 handler_4
= cmd
->handler
;
2884 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2887 handler_5
= cmd
->handler
;
2888 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2891 handler_6
= cmd
->handler
;
2892 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2895 handler_7
= cmd
->handler
;
2896 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2900 handler_8
= cmd
->handler
;
2901 handler_8(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2905 handler_9
= cmd
->handler
;
2906 handler_9(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2907 args
[6], args
[7], args
[8]);
2910 handler_10
= cmd
->handler
;
2911 handler_10(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2912 args
[6], args
[7], args
[8], args
[9]);
2915 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2918 qemu_errors_to_previous();
2922 for(i
= 0; i
< MAX_ARGS
; i
++)
2923 qemu_free(str_allocated
[i
]);
2927 static void cmd_completion(const char *name
, const char *list
)
2929 const char *p
, *pstart
;
2938 p
= pstart
+ strlen(pstart
);
2940 if (len
> sizeof(cmd
) - 2)
2941 len
= sizeof(cmd
) - 2;
2942 memcpy(cmd
, pstart
, len
);
2944 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2945 readline_add_completion(cur_mon
->rs
, cmd
);
2953 static void file_completion(const char *input
)
2958 char file
[1024], file_prefix
[1024];
2962 p
= strrchr(input
, '/');
2965 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2966 pstrcpy(path
, sizeof(path
), ".");
2968 input_path_len
= p
- input
+ 1;
2969 memcpy(path
, input
, input_path_len
);
2970 if (input_path_len
> sizeof(path
) - 1)
2971 input_path_len
= sizeof(path
) - 1;
2972 path
[input_path_len
] = '\0';
2973 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2975 #ifdef DEBUG_COMPLETION
2976 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2977 input
, path
, file_prefix
);
2979 ffs
= opendir(path
);
2987 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2988 memcpy(file
, input
, input_path_len
);
2989 if (input_path_len
< sizeof(file
))
2990 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2992 /* stat the file to find out if it's a directory.
2993 * In that case add a slash to speed up typing long paths
2996 if(S_ISDIR(sb
.st_mode
))
2997 pstrcat(file
, sizeof(file
), "/");
2998 readline_add_completion(cur_mon
->rs
, file
);
3004 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
3006 const char *name
= bdrv_get_device_name(bs
);
3007 const char *input
= opaque
;
3009 if (input
[0] == '\0' ||
3010 !strncmp(name
, (char *)input
, strlen(input
))) {
3011 readline_add_completion(cur_mon
->rs
, name
);
3015 /* NOTE: this parser is an approximate form of the real command parser */
3016 static void parse_cmdline(const char *cmdline
,
3017 int *pnb_args
, char **args
)
3026 while (qemu_isspace(*p
))
3030 if (nb_args
>= MAX_ARGS
)
3032 ret
= get_str(buf
, sizeof(buf
), &p
);
3033 args
[nb_args
] = qemu_strdup(buf
);
3038 *pnb_args
= nb_args
;
3041 static const char *next_arg_type(const char *typestr
)
3043 const char *p
= strchr(typestr
, ':');
3044 return (p
!= NULL
? ++p
: typestr
);
3047 static void monitor_find_completion(const char *cmdline
)
3049 const char *cmdname
;
3050 char *args
[MAX_ARGS
];
3051 int nb_args
, i
, len
;
3052 const char *ptype
, *str
;
3053 const mon_cmd_t
*cmd
;
3056 parse_cmdline(cmdline
, &nb_args
, args
);
3057 #ifdef DEBUG_COMPLETION
3058 for(i
= 0; i
< nb_args
; i
++) {
3059 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3063 /* if the line ends with a space, it means we want to complete the
3065 len
= strlen(cmdline
);
3066 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3067 if (nb_args
>= MAX_ARGS
)
3069 args
[nb_args
++] = qemu_strdup("");
3072 /* command completion */
3077 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3078 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3079 cmd_completion(cmdname
, cmd
->name
);
3082 /* find the command */
3083 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3084 if (compare_cmd(args
[0], cmd
->name
))
3089 ptype
= next_arg_type(cmd
->args_type
);
3090 for(i
= 0; i
< nb_args
- 2; i
++) {
3091 if (*ptype
!= '\0') {
3092 ptype
= next_arg_type(ptype
);
3093 while (*ptype
== '?')
3094 ptype
= next_arg_type(ptype
);
3097 str
= args
[nb_args
- 1];
3098 if (*ptype
== '-' && ptype
[1] != '\0') {
3103 /* file completion */
3104 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3105 file_completion(str
);
3108 /* block device name completion */
3109 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3110 bdrv_iterate(block_completion_it
, (void *)str
);
3113 /* XXX: more generic ? */
3114 if (!strcmp(cmd
->name
, "info")) {
3115 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3116 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3117 cmd_completion(str
, cmd
->name
);
3119 } else if (!strcmp(cmd
->name
, "sendkey")) {
3120 char *sep
= strrchr(str
, '-');
3123 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3124 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3125 cmd_completion(str
, key
->name
);
3127 } else if (!strcmp(cmd
->name
, "help|?")) {
3128 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3129 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3130 cmd_completion(str
, cmd
->name
);
3138 for(i
= 0; i
< nb_args
; i
++)
3142 static int monitor_can_read(void *opaque
)
3144 Monitor
*mon
= opaque
;
3146 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3149 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3151 Monitor
*old_mon
= cur_mon
;
3157 for (i
= 0; i
< size
; i
++)
3158 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3160 if (size
== 0 || buf
[size
- 1] != 0)
3161 monitor_printf(cur_mon
, "corrupted command\n");
3163 monitor_handle_command(cur_mon
, (char *)buf
);
3169 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3171 monitor_suspend(mon
);
3172 monitor_handle_command(mon
, cmdline
);
3173 monitor_resume(mon
);
3176 int monitor_suspend(Monitor
*mon
)
3184 void monitor_resume(Monitor
*mon
)
3188 if (--mon
->suspend_cnt
== 0)
3189 readline_show_prompt(mon
->rs
);
3192 static void monitor_event(void *opaque
, int event
)
3194 Monitor
*mon
= opaque
;
3197 case CHR_EVENT_MUX_IN
:
3198 readline_restart(mon
->rs
);
3199 monitor_resume(mon
);
3203 case CHR_EVENT_MUX_OUT
:
3204 if (mon
->suspend_cnt
== 0)
3205 monitor_printf(mon
, "\n");
3207 monitor_suspend(mon
);
3210 case CHR_EVENT_RESET
:
3211 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3212 "information\n", QEMU_VERSION
);
3213 if (mon
->chr
->focus
== 0)
3214 readline_show_prompt(mon
->rs
);
3228 void monitor_init(CharDriverState
*chr
, int flags
)
3230 static int is_first_init
= 1;
3233 if (is_first_init
) {
3234 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3238 mon
= qemu_mallocz(sizeof(*mon
));
3242 if (mon
->chr
->focus
!= 0)
3243 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3244 if (flags
& MONITOR_USE_READLINE
) {
3245 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3246 monitor_read_command(mon
, 0);
3249 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3252 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3253 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3257 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3259 BlockDriverState
*bs
= opaque
;
3262 if (bdrv_set_key(bs
, password
) != 0) {
3263 monitor_printf(mon
, "invalid password\n");
3266 if (mon
->password_completion_cb
)
3267 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3269 monitor_read_command(mon
, 1);
3272 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3273 BlockDriverCompletionFunc
*completion_cb
,
3278 if (!bdrv_key_required(bs
)) {
3280 completion_cb(opaque
, 0);
3284 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3285 bdrv_get_encrypted_filename(bs
));
3287 mon
->password_completion_cb
= completion_cb
;
3288 mon
->password_opaque
= opaque
;
3290 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3292 if (err
&& completion_cb
)
3293 completion_cb(opaque
, err
);
3296 typedef struct QemuErrorSink QemuErrorSink
;
3297 struct QemuErrorSink
{
3306 QemuErrorSink
*previous
;
3309 static QemuErrorSink
*qemu_error_sink
;
3311 void qemu_errors_to_file(FILE *fp
)
3313 QemuErrorSink
*sink
;
3315 sink
= qemu_mallocz(sizeof(*sink
));
3316 sink
->dest
= ERR_SINK_FILE
;
3318 sink
->previous
= qemu_error_sink
;
3319 qemu_error_sink
= sink
;
3322 void qemu_errors_to_mon(Monitor
*mon
)
3324 QemuErrorSink
*sink
;
3326 sink
= qemu_mallocz(sizeof(*sink
));
3327 sink
->dest
= ERR_SINK_MONITOR
;
3329 sink
->previous
= qemu_error_sink
;
3330 qemu_error_sink
= sink
;
3333 void qemu_errors_to_previous(void)
3335 QemuErrorSink
*sink
;
3337 assert(qemu_error_sink
!= NULL
);
3338 sink
= qemu_error_sink
;
3339 qemu_error_sink
= sink
->previous
;
3343 void qemu_error(const char *fmt
, ...)
3347 assert(qemu_error_sink
!= NULL
);
3348 switch (qemu_error_sink
->dest
) {
3350 va_start(args
, fmt
);
3351 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3354 case ERR_SINK_MONITOR
:
3355 va_start(args
, fmt
);
3356 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);