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_commit(Monitor
*mon
, const char *device
)
262 all_devices
= !strcmp(device
, "all");
263 TAILQ_FOREACH(dinfo
, &drives
, next
) {
265 if (strcmp(bdrv_get_device_name(dinfo
->bdrv
), device
))
267 bdrv_commit(dinfo
->bdrv
);
271 static void do_info(Monitor
*mon
, const char *item
)
273 const mon_cmd_t
*cmd
;
274 void (*handler
)(Monitor
*);
278 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
279 if (compare_cmd(item
, cmd
->name
))
283 help_cmd(mon
, "info");
286 handler
= cmd
->handler
;
290 static void do_info_version(Monitor
*mon
)
292 monitor_printf(mon
, "%s\n", QEMU_VERSION QEMU_PKGVERSION
);
295 static void do_info_name(Monitor
*mon
)
298 monitor_printf(mon
, "%s\n", qemu_name
);
301 #if defined(TARGET_I386)
302 static void do_info_hpet(Monitor
*mon
)
304 monitor_printf(mon
, "HPET is %s by QEMU\n",
305 (no_hpet
) ? "disabled" : "enabled");
309 static void do_info_uuid(Monitor
*mon
)
311 monitor_printf(mon
, UUID_FMT
"\n", qemu_uuid
[0], qemu_uuid
[1],
312 qemu_uuid
[2], qemu_uuid
[3], qemu_uuid
[4], qemu_uuid
[5],
313 qemu_uuid
[6], qemu_uuid
[7], qemu_uuid
[8], qemu_uuid
[9],
314 qemu_uuid
[10], qemu_uuid
[11], qemu_uuid
[12], qemu_uuid
[13],
315 qemu_uuid
[14], qemu_uuid
[15]);
318 /* get the current CPU defined by the user */
319 static int mon_set_cpu(int cpu_index
)
323 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
324 if (env
->cpu_index
== cpu_index
) {
325 cur_mon
->mon_cpu
= env
;
332 static CPUState
*mon_get_cpu(void)
334 if (!cur_mon
->mon_cpu
) {
337 cpu_synchronize_state(cur_mon
->mon_cpu
);
338 return cur_mon
->mon_cpu
;
341 static void do_info_registers(Monitor
*mon
)
348 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
351 cpu_dump_state(env
, (FILE *)mon
, monitor_fprintf
,
356 static void do_info_cpus(Monitor
*mon
)
360 /* just to set the default cpu if not already done */
363 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
364 cpu_synchronize_state(env
);
365 monitor_printf(mon
, "%c CPU #%d:",
366 (env
== mon
->mon_cpu
) ? '*' : ' ',
368 #if defined(TARGET_I386)
369 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
,
370 env
->eip
+ env
->segs
[R_CS
].base
);
371 #elif defined(TARGET_PPC)
372 monitor_printf(mon
, " nip=0x" TARGET_FMT_lx
, env
->nip
);
373 #elif defined(TARGET_SPARC)
374 monitor_printf(mon
, " pc=0x" TARGET_FMT_lx
" npc=0x" TARGET_FMT_lx
,
376 #elif defined(TARGET_MIPS)
377 monitor_printf(mon
, " PC=0x" TARGET_FMT_lx
, env
->active_tc
.PC
);
380 monitor_printf(mon
, " (halted)");
381 monitor_printf(mon
," thread_id=%d", env
->thread_id
);
382 monitor_printf(mon
, "\n");
386 static void do_cpu_set(Monitor
*mon
, int index
)
388 if (mon_set_cpu(index
) < 0)
389 monitor_printf(mon
, "Invalid CPU index\n");
392 static void do_cpu_set_nr(Monitor
*mon
, int value
, const char *status
)
396 if (!strcmp(status
, "online"))
398 else if (!strcmp(status
, "offline"))
401 monitor_printf(mon
, "invalid status: %s\n", status
);
404 #if defined(TARGET_I386) || defined(TARGET_X86_64)
405 qemu_system_cpu_hot_add(value
, state
);
409 static void do_info_jit(Monitor
*mon
)
411 dump_exec_info((FILE *)mon
, monitor_fprintf
);
414 static void do_info_history(Monitor
*mon
)
423 str
= readline_get_history(mon
->rs
, i
);
426 monitor_printf(mon
, "%d: '%s'\n", i
, str
);
431 #if defined(TARGET_PPC)
432 /* XXX: not implemented in other targets */
433 static void do_info_cpu_stats(Monitor
*mon
)
438 cpu_dump_statistics(env
, (FILE *)mon
, &monitor_fprintf
, 0);
442 static void do_quit(Monitor
*mon
)
447 static int eject_device(Monitor
*mon
, BlockDriverState
*bs
, int force
)
449 if (bdrv_is_inserted(bs
)) {
451 if (!bdrv_is_removable(bs
)) {
452 monitor_printf(mon
, "device is not removable\n");
455 if (bdrv_is_locked(bs
)) {
456 monitor_printf(mon
, "device is locked\n");
465 static void do_eject(Monitor
*mon
, int force
, const char *filename
)
467 BlockDriverState
*bs
;
469 bs
= bdrv_find(filename
);
471 monitor_printf(mon
, "device not found\n");
474 eject_device(mon
, bs
, force
);
477 static void do_change_block(Monitor
*mon
, const char *device
,
478 const char *filename
, const char *fmt
)
480 BlockDriverState
*bs
;
481 BlockDriver
*drv
= NULL
;
483 bs
= bdrv_find(device
);
485 monitor_printf(mon
, "device not found\n");
489 drv
= bdrv_find_format(fmt
);
491 monitor_printf(mon
, "invalid format %s\n", fmt
);
495 if (eject_device(mon
, bs
, 0) < 0)
497 bdrv_open2(bs
, filename
, 0, drv
);
498 monitor_read_bdrv_key_start(mon
, bs
, NULL
, NULL
);
501 static void change_vnc_password_cb(Monitor
*mon
, const char *password
,
504 if (vnc_display_password(NULL
, password
) < 0)
505 monitor_printf(mon
, "could not set VNC server password\n");
507 monitor_read_command(mon
, 1);
510 static void do_change_vnc(Monitor
*mon
, const char *target
, const char *arg
)
512 if (strcmp(target
, "passwd") == 0 ||
513 strcmp(target
, "password") == 0) {
516 strncpy(password
, arg
, sizeof(password
));
517 password
[sizeof(password
) - 1] = '\0';
518 change_vnc_password_cb(mon
, password
, NULL
);
520 monitor_read_password(mon
, change_vnc_password_cb
, NULL
);
523 if (vnc_display_open(NULL
, target
) < 0)
524 monitor_printf(mon
, "could not start VNC server on %s\n", target
);
528 static void do_change(Monitor
*mon
, const char *device
, const char *target
,
531 if (strcmp(device
, "vnc") == 0) {
532 do_change_vnc(mon
, target
, arg
);
534 do_change_block(mon
, device
, target
, arg
);
538 static void do_screen_dump(Monitor
*mon
, const char *filename
)
540 vga_hw_screen_dump(filename
);
543 static void do_logfile(Monitor
*mon
, const char *filename
)
545 cpu_set_log_filename(filename
);
548 static void do_log(Monitor
*mon
, const char *items
)
552 if (!strcmp(items
, "none")) {
555 mask
= cpu_str_to_log_mask(items
);
557 help_cmd(mon
, "log");
564 static void do_singlestep(Monitor
*mon
, const char *option
)
566 if (!option
|| !strcmp(option
, "on")) {
568 } else if (!strcmp(option
, "off")) {
571 monitor_printf(mon
, "unexpected option %s\n", option
);
575 static void do_stop(Monitor
*mon
)
577 vm_stop(EXCP_INTERRUPT
);
580 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
);
582 struct bdrv_iterate_context
{
587 static void do_cont(Monitor
*mon
)
589 struct bdrv_iterate_context context
= { mon
, 0 };
591 bdrv_iterate(encrypted_bdrv_it
, &context
);
592 /* only resume the vm if all keys are set and valid */
597 static void bdrv_key_cb(void *opaque
, int err
)
599 Monitor
*mon
= opaque
;
601 /* another key was set successfully, retry to continue */
606 static void encrypted_bdrv_it(void *opaque
, BlockDriverState
*bs
)
608 struct bdrv_iterate_context
*context
= opaque
;
610 if (!context
->err
&& bdrv_key_required(bs
)) {
611 context
->err
= -EBUSY
;
612 monitor_read_bdrv_key_start(context
->mon
, bs
, bdrv_key_cb
,
617 static void do_gdbserver(Monitor
*mon
, const char *device
)
620 device
= "tcp::" DEFAULT_GDBSTUB_PORT
;
621 if (gdbserver_start(device
) < 0) {
622 monitor_printf(mon
, "Could not open gdbserver on device '%s'\n",
624 } else if (strcmp(device
, "none") == 0) {
625 monitor_printf(mon
, "Disabled gdbserver\n");
627 monitor_printf(mon
, "Waiting for gdb connection on device '%s'\n",
632 static void do_watchdog_action(Monitor
*mon
, const char *action
)
634 if (select_watchdog_action(action
) == -1) {
635 monitor_printf(mon
, "Unknown watchdog action '%s'\n", action
);
639 static void monitor_printc(Monitor
*mon
, int c
)
641 monitor_printf(mon
, "'");
644 monitor_printf(mon
, "\\'");
647 monitor_printf(mon
, "\\\\");
650 monitor_printf(mon
, "\\n");
653 monitor_printf(mon
, "\\r");
656 if (c
>= 32 && c
<= 126) {
657 monitor_printf(mon
, "%c", c
);
659 monitor_printf(mon
, "\\x%02x", c
);
663 monitor_printf(mon
, "'");
666 static void memory_dump(Monitor
*mon
, int count
, int format
, int wsize
,
667 target_phys_addr_t addr
, int is_physical
)
670 int nb_per_line
, l
, line_size
, i
, max_digits
, len
;
678 if (!env
&& !is_physical
)
683 } else if (wsize
== 4) {
686 /* as default we use the current CS size */
690 if ((env
->efer
& MSR_EFER_LMA
) &&
691 (env
->segs
[R_CS
].flags
& DESC_L_MASK
))
695 if (!(env
->segs
[R_CS
].flags
& DESC_B_MASK
))
700 monitor_disas(mon
, env
, addr
, count
, is_physical
, flags
);
709 nb_per_line
= line_size
/ wsize
;
714 max_digits
= (wsize
* 8 + 2) / 3;
718 max_digits
= (wsize
* 8) / 4;
722 max_digits
= (wsize
* 8 * 10 + 32) / 33;
731 monitor_printf(mon
, TARGET_FMT_plx
":", addr
);
733 monitor_printf(mon
, TARGET_FMT_lx
":", (target_ulong
)addr
);
738 cpu_physical_memory_rw(addr
, buf
, l
, 0);
743 if (cpu_memory_rw_debug(env
, addr
, buf
, l
, 0) < 0) {
744 monitor_printf(mon
, " Cannot access memory\n");
753 v
= ldub_raw(buf
+ i
);
756 v
= lduw_raw(buf
+ i
);
759 v
= (uint32_t)ldl_raw(buf
+ i
);
762 v
= ldq_raw(buf
+ i
);
765 monitor_printf(mon
, " ");
768 monitor_printf(mon
, "%#*" PRIo64
, max_digits
, v
);
771 monitor_printf(mon
, "0x%0*" PRIx64
, max_digits
, v
);
774 monitor_printf(mon
, "%*" PRIu64
, max_digits
, v
);
777 monitor_printf(mon
, "%*" PRId64
, max_digits
, v
);
780 monitor_printc(mon
, v
);
785 monitor_printf(mon
, "\n");
791 #if TARGET_LONG_BITS == 64
792 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
794 #define GET_TLONG(h, l) (l)
797 static void do_memory_dump(Monitor
*mon
, int count
, int format
, int size
,
798 uint32_t addrh
, uint32_t addrl
)
800 target_long addr
= GET_TLONG(addrh
, addrl
);
801 memory_dump(mon
, count
, format
, size
, addr
, 0);
804 #if TARGET_PHYS_ADDR_BITS > 32
805 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
807 #define GET_TPHYSADDR(h, l) (l)
810 static void do_physical_memory_dump(Monitor
*mon
, int count
, int format
,
811 int size
, uint32_t addrh
, uint32_t addrl
)
814 target_phys_addr_t addr
= GET_TPHYSADDR(addrh
, addrl
);
815 memory_dump(mon
, count
, format
, size
, addr
, 1);
818 static void do_print(Monitor
*mon
, int count
, int format
, int size
,
819 unsigned int valh
, unsigned int vall
)
821 target_phys_addr_t val
= GET_TPHYSADDR(valh
, vall
);
822 #if TARGET_PHYS_ADDR_BITS == 32
825 monitor_printf(mon
, "%#o", val
);
828 monitor_printf(mon
, "%#x", val
);
831 monitor_printf(mon
, "%u", val
);
835 monitor_printf(mon
, "%d", val
);
838 monitor_printc(mon
, val
);
844 monitor_printf(mon
, "%#" PRIo64
, val
);
847 monitor_printf(mon
, "%#" PRIx64
, val
);
850 monitor_printf(mon
, "%" PRIu64
, val
);
854 monitor_printf(mon
, "%" PRId64
, val
);
857 monitor_printc(mon
, val
);
861 monitor_printf(mon
, "\n");
864 static void do_memory_save(Monitor
*mon
, unsigned int valh
, unsigned int vall
,
865 uint32_t size
, const char *filename
)
868 target_long addr
= GET_TLONG(valh
, vall
);
877 f
= fopen(filename
, "wb");
879 monitor_printf(mon
, "could not open '%s'\n", filename
);
886 cpu_memory_rw_debug(env
, addr
, buf
, l
, 0);
887 fwrite(buf
, 1, l
, f
);
894 static void do_physical_memory_save(Monitor
*mon
, unsigned int valh
,
895 unsigned int vall
, uint32_t size
,
896 const char *filename
)
901 target_phys_addr_t addr
= GET_TPHYSADDR(valh
, vall
);
903 f
= fopen(filename
, "wb");
905 monitor_printf(mon
, "could not open '%s'\n", filename
);
912 cpu_physical_memory_rw(addr
, buf
, l
, 0);
913 fwrite(buf
, 1, l
, f
);
921 static void do_sum(Monitor
*mon
, uint32_t start
, uint32_t size
)
928 for(addr
= start
; addr
< (start
+ size
); addr
++) {
929 cpu_physical_memory_rw(addr
, buf
, 1, 0);
930 /* BSD sum algorithm ('sum' Unix command) */
931 sum
= (sum
>> 1) | (sum
<< 15);
934 monitor_printf(mon
, "%05d\n", sum
);
942 static const KeyDef key_defs
[] = {
969 { 0x0e, "backspace" },
1006 { 0x37, "asterisk" },
1009 { 0x3a, "caps_lock" },
1020 { 0x45, "num_lock" },
1021 { 0x46, "scroll_lock" },
1023 { 0xb5, "kp_divide" },
1024 { 0x37, "kp_multiply" },
1025 { 0x4a, "kp_subtract" },
1027 { 0x9c, "kp_enter" },
1028 { 0x53, "kp_decimal" },
1061 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1076 { 0xfe, "compose" },
1081 static int get_keycode(const char *key
)
1087 for(p
= key_defs
; p
->name
!= NULL
; p
++) {
1088 if (!strcmp(key
, p
->name
))
1091 if (strstart(key
, "0x", NULL
)) {
1092 ret
= strtoul(key
, &endp
, 0);
1093 if (*endp
== '\0' && ret
>= 0x01 && ret
<= 0xff)
1099 #define MAX_KEYCODES 16
1100 static uint8_t keycodes
[MAX_KEYCODES
];
1101 static int nb_pending_keycodes
;
1102 static QEMUTimer
*key_timer
;
1104 static void release_keys(void *opaque
)
1108 while (nb_pending_keycodes
> 0) {
1109 nb_pending_keycodes
--;
1110 keycode
= keycodes
[nb_pending_keycodes
];
1112 kbd_put_keycode(0xe0);
1113 kbd_put_keycode(keycode
| 0x80);
1117 static void do_sendkey(Monitor
*mon
, const char *string
, int has_hold_time
,
1120 char keyname_buf
[16];
1122 int keyname_len
, keycode
, i
;
1124 if (nb_pending_keycodes
> 0) {
1125 qemu_del_timer(key_timer
);
1132 separator
= strchr(string
, '-');
1133 keyname_len
= separator
? separator
- string
: strlen(string
);
1134 if (keyname_len
> 0) {
1135 pstrcpy(keyname_buf
, sizeof(keyname_buf
), string
);
1136 if (keyname_len
> sizeof(keyname_buf
) - 1) {
1137 monitor_printf(mon
, "invalid key: '%s...'\n", keyname_buf
);
1140 if (i
== MAX_KEYCODES
) {
1141 monitor_printf(mon
, "too many keys\n");
1144 keyname_buf
[keyname_len
] = 0;
1145 keycode
= get_keycode(keyname_buf
);
1147 monitor_printf(mon
, "unknown key: '%s'\n", keyname_buf
);
1150 keycodes
[i
++] = keycode
;
1154 string
= separator
+ 1;
1156 nb_pending_keycodes
= i
;
1157 /* key down events */
1158 for (i
= 0; i
< nb_pending_keycodes
; i
++) {
1159 keycode
= keycodes
[i
];
1161 kbd_put_keycode(0xe0);
1162 kbd_put_keycode(keycode
& 0x7f);
1164 /* delayed key up events */
1165 qemu_mod_timer(key_timer
, qemu_get_clock(vm_clock
) +
1166 muldiv64(ticks_per_sec
, hold_time
, 1000));
1169 static int mouse_button_state
;
1171 static void do_mouse_move(Monitor
*mon
, const char *dx_str
, const char *dy_str
,
1175 dx
= strtol(dx_str
, NULL
, 0);
1176 dy
= strtol(dy_str
, NULL
, 0);
1179 dz
= strtol(dz_str
, NULL
, 0);
1180 kbd_mouse_event(dx
, dy
, dz
, mouse_button_state
);
1183 static void do_mouse_button(Monitor
*mon
, int button_state
)
1185 mouse_button_state
= button_state
;
1186 kbd_mouse_event(0, 0, 0, mouse_button_state
);
1189 static void do_ioport_read(Monitor
*mon
, int count
, int format
, int size
,
1190 int addr
, int has_index
, int index
)
1196 cpu_outb(NULL
, addr
& IOPORTS_MASK
, index
& 0xff);
1204 val
= cpu_inb(NULL
, addr
);
1208 val
= cpu_inw(NULL
, addr
);
1212 val
= cpu_inl(NULL
, addr
);
1216 monitor_printf(mon
, "port%c[0x%04x] = %#0*x\n",
1217 suffix
, addr
, size
* 2, val
);
1220 static void do_ioport_write(Monitor
*mon
, int count
, int format
, int size
,
1223 addr
&= IOPORTS_MASK
;
1228 cpu_outb(NULL
, addr
, val
);
1231 cpu_outw(NULL
, addr
, val
);
1234 cpu_outl(NULL
, addr
, val
);
1239 static void do_boot_set(Monitor
*mon
, const char *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
)
1256 qemu_system_reset_request();
1259 static void do_system_powerdown(Monitor
*mon
)
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
)
1416 #if defined(USE_KVM) || defined(CONFIG_KVM)
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
, int n
)
1488 for (s
= capture_head
.lh_first
, i
= 0; s
; s
= s
->entries
.le_next
, ++i
) {
1490 s
->ops
.destroy (s
->opaque
);
1491 LIST_REMOVE (s
, entries
);
1498 static void do_wav_capture(Monitor
*mon
, const char *path
,
1499 int has_freq
, int freq
,
1500 int has_bits
, int bits
,
1501 int has_channels
, int nchannels
)
1505 s
= qemu_mallocz (sizeof (*s
));
1507 freq
= has_freq
? freq
: 44100;
1508 bits
= has_bits
? bits
: 16;
1509 nchannels
= has_channels
? nchannels
: 2;
1511 if (wav_start_capture (s
, path
, freq
, bits
, nchannels
)) {
1512 monitor_printf(mon
, "Faied to add wave capture\n");
1515 LIST_INSERT_HEAD (&capture_head
, s
, entries
);
1519 #if defined(TARGET_I386)
1520 static void do_inject_nmi(Monitor
*mon
, int cpu_index
)
1524 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1525 if (env
->cpu_index
== cpu_index
) {
1527 kvm_inject_interrupt(env
, CPU_INTERRUPT_NMI
);
1529 cpu_interrupt(env
, CPU_INTERRUPT_NMI
);
1535 static void do_info_status(Monitor
*mon
)
1539 monitor_printf(mon
, "VM status: running (single step mode)\n");
1541 monitor_printf(mon
, "VM status: running\n");
1544 monitor_printf(mon
, "VM status: paused\n");
1548 static void do_balloon(Monitor
*mon
, int 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 char *aclname
)
1580 qemu_acl
*acl
= find_acl(mon
, aclname
);
1581 qemu_acl_entry
*entry
;
1585 monitor_printf(mon
, "policy: %s\n",
1586 acl
->defaultDeny
? "deny" : "allow");
1587 TAILQ_FOREACH(entry
, &acl
->entries
, next
) {
1589 monitor_printf(mon
, "%d: %s %s\n", i
,
1590 entry
->deny
? "deny" : "allow", entry
->match
);
1595 static void do_acl_reset(Monitor
*mon
, const char *aclname
)
1597 qemu_acl
*acl
= find_acl(mon
, aclname
);
1600 qemu_acl_reset(acl
);
1601 monitor_printf(mon
, "acl: removed all rules\n");
1605 static void do_acl_policy(Monitor
*mon
, const char *aclname
,
1608 qemu_acl
*acl
= find_acl(mon
, aclname
);
1611 if (strcmp(policy
, "allow") == 0) {
1612 acl
->defaultDeny
= 0;
1613 monitor_printf(mon
, "acl: policy set to 'allow'\n");
1614 } else if (strcmp(policy
, "deny") == 0) {
1615 acl
->defaultDeny
= 1;
1616 monitor_printf(mon
, "acl: policy set to 'deny'\n");
1618 monitor_printf(mon
, "acl: unknown policy '%s', "
1619 "expected 'deny' or 'allow'\n", policy
);
1624 static void do_acl_add(Monitor
*mon
, const char *aclname
,
1625 const char *match
, const char *policy
,
1626 int has_index
, int index
)
1628 qemu_acl
*acl
= find_acl(mon
, aclname
);
1632 if (strcmp(policy
, "allow") == 0) {
1634 } else if (strcmp(policy
, "deny") == 0) {
1637 monitor_printf(mon
, "acl: unknown policy '%s', "
1638 "expected 'deny' or 'allow'\n", policy
);
1642 ret
= qemu_acl_insert(acl
, deny
, match
, index
);
1644 ret
= qemu_acl_append(acl
, deny
, match
);
1646 monitor_printf(mon
, "acl: unable to add acl entry\n");
1648 monitor_printf(mon
, "acl: added rule at position %d\n", ret
);
1652 static void do_acl_remove(Monitor
*mon
, const char *aclname
, const char *match
)
1654 qemu_acl
*acl
= find_acl(mon
, aclname
);
1658 ret
= qemu_acl_remove(acl
, match
);
1660 monitor_printf(mon
, "acl: no matching acl entry\n");
1662 monitor_printf(mon
, "acl: removed rule at position %d\n", ret
);
1666 #if defined(TARGET_I386)
1667 static void do_inject_mce(Monitor
*mon
,
1668 int cpu_index
, int bank
,
1669 unsigned status_hi
, unsigned status_lo
,
1670 unsigned mcg_status_hi
, unsigned mcg_status_lo
,
1671 unsigned addr_hi
, unsigned addr_lo
,
1672 unsigned misc_hi
, unsigned misc_lo
)
1675 uint64_t status
= ((uint64_t)status_hi
<< 32) | status_lo
;
1676 uint64_t mcg_status
= ((uint64_t)mcg_status_hi
<< 32) | mcg_status_lo
;
1677 uint64_t addr
= ((uint64_t)addr_hi
<< 32) | addr_lo
;
1678 uint64_t misc
= ((uint64_t)misc_hi
<< 32) | misc_lo
;
1680 for (cenv
= first_cpu
; cenv
!= NULL
; cenv
= cenv
->next_cpu
)
1681 if (cenv
->cpu_index
== cpu_index
&& cenv
->mcg_cap
) {
1682 cpu_inject_x86_mce(cenv
, bank
, status
, mcg_status
, addr
, misc
);
1688 static void do_getfd(Monitor
*mon
, const char *fdname
)
1693 fd
= qemu_chr_get_msgfd(mon
->chr
);
1695 monitor_printf(mon
, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1699 if (qemu_isdigit(fdname
[0])) {
1700 monitor_printf(mon
, "getfd: monitor names may not begin with a number\n");
1706 monitor_printf(mon
, "Failed to dup() file descriptor: %s\n",
1711 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1712 if (strcmp(monfd
->name
, fdname
) != 0) {
1721 monfd
= qemu_mallocz(sizeof(mon_fd_t
));
1722 monfd
->name
= qemu_strdup(fdname
);
1725 LIST_INSERT_HEAD(&mon
->fds
, monfd
, next
);
1728 static void do_closefd(Monitor
*mon
, const char *fdname
)
1732 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1733 if (strcmp(monfd
->name
, fdname
) != 0) {
1737 LIST_REMOVE(monfd
, next
);
1739 qemu_free(monfd
->name
);
1744 monitor_printf(mon
, "Failed to find file descriptor named %s\n",
1748 static void do_loadvm(Monitor
*mon
, const char *name
)
1750 int saved_vm_running
= vm_running
;
1754 if (load_vmstate(mon
, name
) >= 0 && saved_vm_running
)
1758 int monitor_get_fd(Monitor
*mon
, const char *fdname
)
1762 LIST_FOREACH(monfd
, &mon
->fds
, next
) {
1765 if (strcmp(monfd
->name
, fdname
) != 0) {
1771 /* caller takes ownership of fd */
1772 LIST_REMOVE(monfd
, next
);
1773 qemu_free(monfd
->name
);
1782 static const mon_cmd_t mon_cmds
[] = {
1783 #include "qemu-monitor.h"
1787 /* Please update qemu-monitor.hx when adding or changing commands */
1788 static const mon_cmd_t info_cmds
[] = {
1789 { "version", "", do_info_version
,
1790 "", "show the version of QEMU" },
1791 { "network", "", do_info_network
,
1792 "", "show the network state" },
1793 { "chardev", "", qemu_chr_info
,
1794 "", "show the character devices" },
1795 { "block", "", bdrv_info
,
1796 "", "show the block devices" },
1797 { "blockstats", "", bdrv_info_stats
,
1798 "", "show block device statistics" },
1799 { "registers", "", do_info_registers
,
1800 "", "show the cpu registers" },
1801 { "cpus", "", do_info_cpus
,
1802 "", "show infos for each CPU" },
1803 { "history", "", do_info_history
,
1804 "", "show the command line history", },
1805 { "irq", "", irq_info
,
1806 "", "show the interrupts statistics (if available)", },
1807 { "pic", "", pic_info
,
1808 "", "show i8259 (PIC) state", },
1809 { "pci", "", pci_info
,
1810 "", "show PCI info", },
1811 #if defined(TARGET_I386) || defined(TARGET_SH4)
1812 { "tlb", "", tlb_info
,
1813 "", "show virtual to physical memory mappings", },
1815 #if defined(TARGET_I386)
1816 { "mem", "", mem_info
,
1817 "", "show the active virtual memory mappings", },
1818 { "hpet", "", do_info_hpet
,
1819 "", "show state of HPET", },
1821 { "jit", "", do_info_jit
,
1822 "", "show dynamic compiler info", },
1823 { "kvm", "", do_info_kvm
,
1824 "", "show KVM information", },
1825 { "numa", "", do_info_numa
,
1826 "", "show NUMA information", },
1827 { "usb", "", usb_info
,
1828 "", "show guest USB devices", },
1829 { "usbhost", "", usb_host_info
,
1830 "", "show host USB devices", },
1831 { "profile", "", do_info_profile
,
1832 "", "show profiling information", },
1833 { "capture", "", do_info_capture
,
1834 "", "show capture information" },
1835 { "snapshots", "", do_info_snapshots
,
1836 "", "show the currently saved VM snapshots" },
1837 { "status", "", do_info_status
,
1838 "", "show the current VM status (running|paused)" },
1839 { "pcmcia", "", pcmcia_info
,
1840 "", "show guest PCMCIA status" },
1841 { "mice", "", do_info_mice
,
1842 "", "show which guest mouse is receiving events" },
1843 { "vnc", "", do_info_vnc
,
1844 "", "show the vnc server status"},
1845 { "name", "", do_info_name
,
1846 "", "show the current VM name" },
1847 { "uuid", "", do_info_uuid
,
1848 "", "show the current VM UUID" },
1849 #if defined(TARGET_PPC)
1850 { "cpustats", "", do_info_cpu_stats
,
1851 "", "show CPU statistics", },
1853 #if defined(CONFIG_SLIRP)
1854 { "usernet", "", do_info_usernet
,
1855 "", "show user network stack connection states", },
1857 { "migrate", "", do_info_migrate
, "", "show migration status" },
1858 { "balloon", "", do_info_balloon
,
1859 "", "show balloon information" },
1860 { "qtree", "", do_info_qtree
,
1861 "", "show device tree" },
1862 { "qdm", "", do_info_qdm
,
1863 "", "show qdev device model list" },
1867 /*******************************************************************/
1869 static const char *pch
;
1870 static jmp_buf expr_env
;
1875 typedef struct MonitorDef
{
1878 target_long (*get_value
)(const struct MonitorDef
*md
, int val
);
1882 #if defined(TARGET_I386)
1883 static target_long
monitor_get_pc (const struct MonitorDef
*md
, int val
)
1885 CPUState
*env
= mon_get_cpu();
1888 return env
->eip
+ env
->segs
[R_CS
].base
;
1892 #if defined(TARGET_PPC)
1893 static target_long
monitor_get_ccr (const struct MonitorDef
*md
, int val
)
1895 CPUState
*env
= mon_get_cpu();
1903 for (i
= 0; i
< 8; i
++)
1904 u
|= env
->crf
[i
] << (32 - (4 * i
));
1909 static target_long
monitor_get_msr (const struct MonitorDef
*md
, int val
)
1911 CPUState
*env
= mon_get_cpu();
1917 static target_long
monitor_get_xer (const struct MonitorDef
*md
, int val
)
1919 CPUState
*env
= mon_get_cpu();
1925 static target_long
monitor_get_decr (const struct MonitorDef
*md
, int val
)
1927 CPUState
*env
= mon_get_cpu();
1930 return cpu_ppc_load_decr(env
);
1933 static target_long
monitor_get_tbu (const struct MonitorDef
*md
, int val
)
1935 CPUState
*env
= mon_get_cpu();
1938 return cpu_ppc_load_tbu(env
);
1941 static target_long
monitor_get_tbl (const struct MonitorDef
*md
, int val
)
1943 CPUState
*env
= mon_get_cpu();
1946 return cpu_ppc_load_tbl(env
);
1950 #if defined(TARGET_SPARC)
1951 #ifndef TARGET_SPARC64
1952 static target_long
monitor_get_psr (const struct MonitorDef
*md
, int val
)
1954 CPUState
*env
= mon_get_cpu();
1957 return GET_PSR(env
);
1961 static target_long
monitor_get_reg(const struct MonitorDef
*md
, int val
)
1963 CPUState
*env
= mon_get_cpu();
1966 return env
->regwptr
[val
];
1970 static const MonitorDef monitor_defs
[] = {
1973 #define SEG(name, seg) \
1974 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1975 { name ".base", offsetof(CPUState, segs[seg].base) },\
1976 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1978 { "eax", offsetof(CPUState
, regs
[0]) },
1979 { "ecx", offsetof(CPUState
, regs
[1]) },
1980 { "edx", offsetof(CPUState
, regs
[2]) },
1981 { "ebx", offsetof(CPUState
, regs
[3]) },
1982 { "esp|sp", offsetof(CPUState
, regs
[4]) },
1983 { "ebp|fp", offsetof(CPUState
, regs
[5]) },
1984 { "esi", offsetof(CPUState
, regs
[6]) },
1985 { "edi", offsetof(CPUState
, regs
[7]) },
1986 #ifdef TARGET_X86_64
1987 { "r8", offsetof(CPUState
, regs
[8]) },
1988 { "r9", offsetof(CPUState
, regs
[9]) },
1989 { "r10", offsetof(CPUState
, regs
[10]) },
1990 { "r11", offsetof(CPUState
, regs
[11]) },
1991 { "r12", offsetof(CPUState
, regs
[12]) },
1992 { "r13", offsetof(CPUState
, regs
[13]) },
1993 { "r14", offsetof(CPUState
, regs
[14]) },
1994 { "r15", offsetof(CPUState
, regs
[15]) },
1996 { "eflags", offsetof(CPUState
, eflags
) },
1997 { "eip", offsetof(CPUState
, eip
) },
2004 { "pc", 0, monitor_get_pc
, },
2005 #elif defined(TARGET_PPC)
2006 /* General purpose registers */
2007 { "r0", offsetof(CPUState
, gpr
[0]) },
2008 { "r1", offsetof(CPUState
, gpr
[1]) },
2009 { "r2", offsetof(CPUState
, gpr
[2]) },
2010 { "r3", offsetof(CPUState
, gpr
[3]) },
2011 { "r4", offsetof(CPUState
, gpr
[4]) },
2012 { "r5", offsetof(CPUState
, gpr
[5]) },
2013 { "r6", offsetof(CPUState
, gpr
[6]) },
2014 { "r7", offsetof(CPUState
, gpr
[7]) },
2015 { "r8", offsetof(CPUState
, gpr
[8]) },
2016 { "r9", offsetof(CPUState
, gpr
[9]) },
2017 { "r10", offsetof(CPUState
, gpr
[10]) },
2018 { "r11", offsetof(CPUState
, gpr
[11]) },
2019 { "r12", offsetof(CPUState
, gpr
[12]) },
2020 { "r13", offsetof(CPUState
, gpr
[13]) },
2021 { "r14", offsetof(CPUState
, gpr
[14]) },
2022 { "r15", offsetof(CPUState
, gpr
[15]) },
2023 { "r16", offsetof(CPUState
, gpr
[16]) },
2024 { "r17", offsetof(CPUState
, gpr
[17]) },
2025 { "r18", offsetof(CPUState
, gpr
[18]) },
2026 { "r19", offsetof(CPUState
, gpr
[19]) },
2027 { "r20", offsetof(CPUState
, gpr
[20]) },
2028 { "r21", offsetof(CPUState
, gpr
[21]) },
2029 { "r22", offsetof(CPUState
, gpr
[22]) },
2030 { "r23", offsetof(CPUState
, gpr
[23]) },
2031 { "r24", offsetof(CPUState
, gpr
[24]) },
2032 { "r25", offsetof(CPUState
, gpr
[25]) },
2033 { "r26", offsetof(CPUState
, gpr
[26]) },
2034 { "r27", offsetof(CPUState
, gpr
[27]) },
2035 { "r28", offsetof(CPUState
, gpr
[28]) },
2036 { "r29", offsetof(CPUState
, gpr
[29]) },
2037 { "r30", offsetof(CPUState
, gpr
[30]) },
2038 { "r31", offsetof(CPUState
, gpr
[31]) },
2039 /* Floating point registers */
2040 { "f0", offsetof(CPUState
, fpr
[0]) },
2041 { "f1", offsetof(CPUState
, fpr
[1]) },
2042 { "f2", offsetof(CPUState
, fpr
[2]) },
2043 { "f3", offsetof(CPUState
, fpr
[3]) },
2044 { "f4", offsetof(CPUState
, fpr
[4]) },
2045 { "f5", offsetof(CPUState
, fpr
[5]) },
2046 { "f6", offsetof(CPUState
, fpr
[6]) },
2047 { "f7", offsetof(CPUState
, fpr
[7]) },
2048 { "f8", offsetof(CPUState
, fpr
[8]) },
2049 { "f9", offsetof(CPUState
, fpr
[9]) },
2050 { "f10", offsetof(CPUState
, fpr
[10]) },
2051 { "f11", offsetof(CPUState
, fpr
[11]) },
2052 { "f12", offsetof(CPUState
, fpr
[12]) },
2053 { "f13", offsetof(CPUState
, fpr
[13]) },
2054 { "f14", offsetof(CPUState
, fpr
[14]) },
2055 { "f15", offsetof(CPUState
, fpr
[15]) },
2056 { "f16", offsetof(CPUState
, fpr
[16]) },
2057 { "f17", offsetof(CPUState
, fpr
[17]) },
2058 { "f18", offsetof(CPUState
, fpr
[18]) },
2059 { "f19", offsetof(CPUState
, fpr
[19]) },
2060 { "f20", offsetof(CPUState
, fpr
[20]) },
2061 { "f21", offsetof(CPUState
, fpr
[21]) },
2062 { "f22", offsetof(CPUState
, fpr
[22]) },
2063 { "f23", offsetof(CPUState
, fpr
[23]) },
2064 { "f24", offsetof(CPUState
, fpr
[24]) },
2065 { "f25", offsetof(CPUState
, fpr
[25]) },
2066 { "f26", offsetof(CPUState
, fpr
[26]) },
2067 { "f27", offsetof(CPUState
, fpr
[27]) },
2068 { "f28", offsetof(CPUState
, fpr
[28]) },
2069 { "f29", offsetof(CPUState
, fpr
[29]) },
2070 { "f30", offsetof(CPUState
, fpr
[30]) },
2071 { "f31", offsetof(CPUState
, fpr
[31]) },
2072 { "fpscr", offsetof(CPUState
, fpscr
) },
2073 /* Next instruction pointer */
2074 { "nip|pc", offsetof(CPUState
, nip
) },
2075 { "lr", offsetof(CPUState
, lr
) },
2076 { "ctr", offsetof(CPUState
, ctr
) },
2077 { "decr", 0, &monitor_get_decr
, },
2078 { "ccr", 0, &monitor_get_ccr
, },
2079 /* Machine state register */
2080 { "msr", 0, &monitor_get_msr
, },
2081 { "xer", 0, &monitor_get_xer
, },
2082 { "tbu", 0, &monitor_get_tbu
, },
2083 { "tbl", 0, &monitor_get_tbl
, },
2084 #if defined(TARGET_PPC64)
2085 /* Address space register */
2086 { "asr", offsetof(CPUState
, asr
) },
2088 /* Segment registers */
2089 { "sdr1", offsetof(CPUState
, sdr1
) },
2090 { "sr0", offsetof(CPUState
, sr
[0]) },
2091 { "sr1", offsetof(CPUState
, sr
[1]) },
2092 { "sr2", offsetof(CPUState
, sr
[2]) },
2093 { "sr3", offsetof(CPUState
, sr
[3]) },
2094 { "sr4", offsetof(CPUState
, sr
[4]) },
2095 { "sr5", offsetof(CPUState
, sr
[5]) },
2096 { "sr6", offsetof(CPUState
, sr
[6]) },
2097 { "sr7", offsetof(CPUState
, sr
[7]) },
2098 { "sr8", offsetof(CPUState
, sr
[8]) },
2099 { "sr9", offsetof(CPUState
, sr
[9]) },
2100 { "sr10", offsetof(CPUState
, sr
[10]) },
2101 { "sr11", offsetof(CPUState
, sr
[11]) },
2102 { "sr12", offsetof(CPUState
, sr
[12]) },
2103 { "sr13", offsetof(CPUState
, sr
[13]) },
2104 { "sr14", offsetof(CPUState
, sr
[14]) },
2105 { "sr15", offsetof(CPUState
, sr
[15]) },
2106 /* Too lazy to put BATs and SPRs ... */
2107 #elif defined(TARGET_SPARC)
2108 { "g0", offsetof(CPUState
, gregs
[0]) },
2109 { "g1", offsetof(CPUState
, gregs
[1]) },
2110 { "g2", offsetof(CPUState
, gregs
[2]) },
2111 { "g3", offsetof(CPUState
, gregs
[3]) },
2112 { "g4", offsetof(CPUState
, gregs
[4]) },
2113 { "g5", offsetof(CPUState
, gregs
[5]) },
2114 { "g6", offsetof(CPUState
, gregs
[6]) },
2115 { "g7", offsetof(CPUState
, gregs
[7]) },
2116 { "o0", 0, monitor_get_reg
},
2117 { "o1", 1, monitor_get_reg
},
2118 { "o2", 2, monitor_get_reg
},
2119 { "o3", 3, monitor_get_reg
},
2120 { "o4", 4, monitor_get_reg
},
2121 { "o5", 5, monitor_get_reg
},
2122 { "o6", 6, monitor_get_reg
},
2123 { "o7", 7, monitor_get_reg
},
2124 { "l0", 8, monitor_get_reg
},
2125 { "l1", 9, monitor_get_reg
},
2126 { "l2", 10, monitor_get_reg
},
2127 { "l3", 11, monitor_get_reg
},
2128 { "l4", 12, monitor_get_reg
},
2129 { "l5", 13, monitor_get_reg
},
2130 { "l6", 14, monitor_get_reg
},
2131 { "l7", 15, monitor_get_reg
},
2132 { "i0", 16, monitor_get_reg
},
2133 { "i1", 17, monitor_get_reg
},
2134 { "i2", 18, monitor_get_reg
},
2135 { "i3", 19, monitor_get_reg
},
2136 { "i4", 20, monitor_get_reg
},
2137 { "i5", 21, monitor_get_reg
},
2138 { "i6", 22, monitor_get_reg
},
2139 { "i7", 23, monitor_get_reg
},
2140 { "pc", offsetof(CPUState
, pc
) },
2141 { "npc", offsetof(CPUState
, npc
) },
2142 { "y", offsetof(CPUState
, y
) },
2143 #ifndef TARGET_SPARC64
2144 { "psr", 0, &monitor_get_psr
, },
2145 { "wim", offsetof(CPUState
, wim
) },
2147 { "tbr", offsetof(CPUState
, tbr
) },
2148 { "fsr", offsetof(CPUState
, fsr
) },
2149 { "f0", offsetof(CPUState
, fpr
[0]) },
2150 { "f1", offsetof(CPUState
, fpr
[1]) },
2151 { "f2", offsetof(CPUState
, fpr
[2]) },
2152 { "f3", offsetof(CPUState
, fpr
[3]) },
2153 { "f4", offsetof(CPUState
, fpr
[4]) },
2154 { "f5", offsetof(CPUState
, fpr
[5]) },
2155 { "f6", offsetof(CPUState
, fpr
[6]) },
2156 { "f7", offsetof(CPUState
, fpr
[7]) },
2157 { "f8", offsetof(CPUState
, fpr
[8]) },
2158 { "f9", offsetof(CPUState
, fpr
[9]) },
2159 { "f10", offsetof(CPUState
, fpr
[10]) },
2160 { "f11", offsetof(CPUState
, fpr
[11]) },
2161 { "f12", offsetof(CPUState
, fpr
[12]) },
2162 { "f13", offsetof(CPUState
, fpr
[13]) },
2163 { "f14", offsetof(CPUState
, fpr
[14]) },
2164 { "f15", offsetof(CPUState
, fpr
[15]) },
2165 { "f16", offsetof(CPUState
, fpr
[16]) },
2166 { "f17", offsetof(CPUState
, fpr
[17]) },
2167 { "f18", offsetof(CPUState
, fpr
[18]) },
2168 { "f19", offsetof(CPUState
, fpr
[19]) },
2169 { "f20", offsetof(CPUState
, fpr
[20]) },
2170 { "f21", offsetof(CPUState
, fpr
[21]) },
2171 { "f22", offsetof(CPUState
, fpr
[22]) },
2172 { "f23", offsetof(CPUState
, fpr
[23]) },
2173 { "f24", offsetof(CPUState
, fpr
[24]) },
2174 { "f25", offsetof(CPUState
, fpr
[25]) },
2175 { "f26", offsetof(CPUState
, fpr
[26]) },
2176 { "f27", offsetof(CPUState
, fpr
[27]) },
2177 { "f28", offsetof(CPUState
, fpr
[28]) },
2178 { "f29", offsetof(CPUState
, fpr
[29]) },
2179 { "f30", offsetof(CPUState
, fpr
[30]) },
2180 { "f31", offsetof(CPUState
, fpr
[31]) },
2181 #ifdef TARGET_SPARC64
2182 { "f32", offsetof(CPUState
, fpr
[32]) },
2183 { "f34", offsetof(CPUState
, fpr
[34]) },
2184 { "f36", offsetof(CPUState
, fpr
[36]) },
2185 { "f38", offsetof(CPUState
, fpr
[38]) },
2186 { "f40", offsetof(CPUState
, fpr
[40]) },
2187 { "f42", offsetof(CPUState
, fpr
[42]) },
2188 { "f44", offsetof(CPUState
, fpr
[44]) },
2189 { "f46", offsetof(CPUState
, fpr
[46]) },
2190 { "f48", offsetof(CPUState
, fpr
[48]) },
2191 { "f50", offsetof(CPUState
, fpr
[50]) },
2192 { "f52", offsetof(CPUState
, fpr
[52]) },
2193 { "f54", offsetof(CPUState
, fpr
[54]) },
2194 { "f56", offsetof(CPUState
, fpr
[56]) },
2195 { "f58", offsetof(CPUState
, fpr
[58]) },
2196 { "f60", offsetof(CPUState
, fpr
[60]) },
2197 { "f62", offsetof(CPUState
, fpr
[62]) },
2198 { "asi", offsetof(CPUState
, asi
) },
2199 { "pstate", offsetof(CPUState
, pstate
) },
2200 { "cansave", offsetof(CPUState
, cansave
) },
2201 { "canrestore", offsetof(CPUState
, canrestore
) },
2202 { "otherwin", offsetof(CPUState
, otherwin
) },
2203 { "wstate", offsetof(CPUState
, wstate
) },
2204 { "cleanwin", offsetof(CPUState
, cleanwin
) },
2205 { "fprs", offsetof(CPUState
, fprs
) },
2211 static void expr_error(Monitor
*mon
, const char *msg
)
2213 monitor_printf(mon
, "%s\n", msg
);
2214 longjmp(expr_env
, 1);
2217 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2218 static int get_monitor_def(target_long
*pval
, const char *name
)
2220 const MonitorDef
*md
;
2223 for(md
= monitor_defs
; md
->name
!= NULL
; md
++) {
2224 if (compare_cmd(name
, md
->name
)) {
2225 if (md
->get_value
) {
2226 *pval
= md
->get_value(md
, md
->offset
);
2228 CPUState
*env
= mon_get_cpu();
2231 ptr
= (uint8_t *)env
+ md
->offset
;
2234 *pval
= *(int32_t *)ptr
;
2237 *pval
= *(target_long
*)ptr
;
2250 static void next(void)
2254 while (qemu_isspace(*pch
))
2259 static int64_t expr_sum(Monitor
*mon
);
2261 static int64_t expr_unary(Monitor
*mon
)
2270 n
= expr_unary(mon
);
2274 n
= -expr_unary(mon
);
2278 n
= ~expr_unary(mon
);
2284 expr_error(mon
, "')' expected");
2291 expr_error(mon
, "character constant expected");
2295 expr_error(mon
, "missing terminating \' character");
2305 while ((*pch
>= 'a' && *pch
<= 'z') ||
2306 (*pch
>= 'A' && *pch
<= 'Z') ||
2307 (*pch
>= '0' && *pch
<= '9') ||
2308 *pch
== '_' || *pch
== '.') {
2309 if ((q
- buf
) < sizeof(buf
) - 1)
2313 while (qemu_isspace(*pch
))
2316 ret
= get_monitor_def(®
, buf
);
2318 expr_error(mon
, "unknown register");
2320 expr_error(mon
, "no cpu defined");
2325 expr_error(mon
, "unexpected end of expression");
2329 #if TARGET_PHYS_ADDR_BITS > 32
2330 n
= strtoull(pch
, &p
, 0);
2332 n
= strtoul(pch
, &p
, 0);
2335 expr_error(mon
, "invalid char in expression");
2338 while (qemu_isspace(*pch
))
2346 static int64_t expr_prod(Monitor
*mon
)
2351 val
= expr_unary(mon
);
2354 if (op
!= '*' && op
!= '/' && op
!= '%')
2357 val2
= expr_unary(mon
);
2366 expr_error(mon
, "division by zero");
2377 static int64_t expr_logic(Monitor
*mon
)
2382 val
= expr_prod(mon
);
2385 if (op
!= '&' && op
!= '|' && op
!= '^')
2388 val2
= expr_prod(mon
);
2405 static int64_t expr_sum(Monitor
*mon
)
2410 val
= expr_logic(mon
);
2413 if (op
!= '+' && op
!= '-')
2416 val2
= expr_logic(mon
);
2425 static int get_expr(Monitor
*mon
, int64_t *pval
, const char **pp
)
2428 if (setjmp(expr_env
)) {
2432 while (qemu_isspace(*pch
))
2434 *pval
= expr_sum(mon
);
2439 static int get_str(char *buf
, int buf_size
, const char **pp
)
2447 while (qemu_isspace(*p
))
2457 while (*p
!= '\0' && *p
!= '\"') {
2473 qemu_printf("unsupported escape code: '\\%c'\n", c
);
2476 if ((q
- buf
) < buf_size
- 1) {
2480 if ((q
- buf
) < buf_size
- 1) {
2487 qemu_printf("unterminated string\n");
2492 while (*p
!= '\0' && !qemu_isspace(*p
)) {
2493 if ((q
- buf
) < buf_size
- 1) {
2505 * Store the command-name in cmdname, and return a pointer to
2506 * the remaining of the command string.
2508 static const char *get_command_name(const char *cmdline
,
2509 char *cmdname
, size_t nlen
)
2512 const char *p
, *pstart
;
2515 while (qemu_isspace(*p
))
2520 while (*p
!= '\0' && *p
!= '/' && !qemu_isspace(*p
))
2525 memcpy(cmdname
, pstart
, len
);
2526 cmdname
[len
] = '\0';
2530 static int default_fmt_format
= 'x';
2531 static int default_fmt_size
= 4;
2535 static void monitor_handle_command(Monitor
*mon
, const char *cmdline
)
2537 const char *p
, *typestr
;
2538 int c
, nb_args
, i
, has_arg
;
2539 const mon_cmd_t
*cmd
;
2542 void *str_allocated
[MAX_ARGS
];
2543 void *args
[MAX_ARGS
];
2544 void (*handler_0
)(Monitor
*mon
);
2545 void (*handler_1
)(Monitor
*mon
, void *arg0
);
2546 void (*handler_2
)(Monitor
*mon
, void *arg0
, void *arg1
);
2547 void (*handler_3
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
);
2548 void (*handler_4
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2550 void (*handler_5
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2551 void *arg3
, void *arg4
);
2552 void (*handler_6
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2553 void *arg3
, void *arg4
, void *arg5
);
2554 void (*handler_7
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2555 void *arg3
, void *arg4
, void *arg5
, void *arg6
);
2556 void (*handler_8
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2557 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2559 void (*handler_9
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2560 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2561 void *arg7
, void *arg8
);
2562 void (*handler_10
)(Monitor
*mon
, void *arg0
, void *arg1
, void *arg2
,
2563 void *arg3
, void *arg4
, void *arg5
, void *arg6
,
2564 void *arg7
, void *arg8
, void *arg9
);
2567 monitor_printf(mon
, "command='%s'\n", cmdline
);
2570 /* extract the command name */
2571 p
= get_command_name(cmdline
, cmdname
, sizeof(cmdname
));
2575 /* find the command */
2576 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
2577 if (compare_cmd(cmdname
, cmd
->name
))
2581 if (cmd
->name
== NULL
) {
2582 monitor_printf(mon
, "unknown command: '%s'\n", cmdname
);
2586 for(i
= 0; i
< MAX_ARGS
; i
++)
2587 str_allocated
[i
] = NULL
;
2589 /* parse the parameters */
2590 typestr
= cmd
->args_type
;
2605 while (qemu_isspace(*p
))
2607 if (*typestr
== '?') {
2610 /* no optional string: NULL argument */
2615 ret
= get_str(buf
, sizeof(buf
), &p
);
2619 monitor_printf(mon
, "%s: filename expected\n",
2623 monitor_printf(mon
, "%s: block device name expected\n",
2627 monitor_printf(mon
, "%s: string expected\n", cmdname
);
2632 str
= qemu_malloc(strlen(buf
) + 1);
2633 pstrcpy(str
, sizeof(buf
), buf
);
2634 str_allocated
[nb_args
] = str
;
2636 if (nb_args
>= MAX_ARGS
) {
2638 monitor_printf(mon
, "%s: too many arguments\n", cmdname
);
2641 args
[nb_args
++] = str
;
2646 int count
, format
, size
;
2648 while (qemu_isspace(*p
))
2654 if (qemu_isdigit(*p
)) {
2656 while (qemu_isdigit(*p
)) {
2657 count
= count
* 10 + (*p
- '0');
2695 if (*p
!= '\0' && !qemu_isspace(*p
)) {
2696 monitor_printf(mon
, "invalid char in format: '%c'\n",
2701 format
= default_fmt_format
;
2702 if (format
!= 'i') {
2703 /* for 'i', not specifying a size gives -1 as size */
2705 size
= default_fmt_size
;
2706 default_fmt_size
= size
;
2708 default_fmt_format
= format
;
2711 format
= default_fmt_format
;
2712 if (format
!= 'i') {
2713 size
= default_fmt_size
;
2718 if (nb_args
+ 3 > MAX_ARGS
)
2720 args
[nb_args
++] = (void*)(long)count
;
2721 args
[nb_args
++] = (void*)(long)format
;
2722 args
[nb_args
++] = (void*)(long)size
;
2730 while (qemu_isspace(*p
))
2732 if (*typestr
== '?' || *typestr
== '.') {
2733 if (*typestr
== '?') {
2741 while (qemu_isspace(*p
))
2749 if (nb_args
>= MAX_ARGS
)
2751 args
[nb_args
++] = (void *)(long)has_arg
;
2753 if (nb_args
>= MAX_ARGS
)
2759 if (get_expr(mon
, &val
, &p
))
2763 if (nb_args
>= MAX_ARGS
)
2765 args
[nb_args
++] = (void *)(long)val
;
2767 if ((nb_args
+ 1) >= MAX_ARGS
)
2769 #if TARGET_PHYS_ADDR_BITS > 32
2770 args
[nb_args
++] = (void *)(long)((val
>> 32) & 0xffffffff);
2772 args
[nb_args
++] = (void *)0;
2774 args
[nb_args
++] = (void *)(long)(val
& 0xffffffff);
2786 while (qemu_isspace(*p
))
2792 monitor_printf(mon
, "%s: unsupported option -%c\n",
2799 if (nb_args
>= MAX_ARGS
)
2801 args
[nb_args
++] = (void *)(long)has_option
;
2806 monitor_printf(mon
, "%s: unknown type '%c'\n", cmdname
, c
);
2810 /* check that all arguments were parsed */
2811 while (qemu_isspace(*p
))
2814 monitor_printf(mon
, "%s: extraneous characters at the end of line\n",
2819 qemu_errors_to_mon(mon
);
2822 handler_0
= cmd
->handler
;
2826 handler_1
= cmd
->handler
;
2827 handler_1(mon
, args
[0]);
2830 handler_2
= cmd
->handler
;
2831 handler_2(mon
, args
[0], args
[1]);
2834 handler_3
= cmd
->handler
;
2835 handler_3(mon
, args
[0], args
[1], args
[2]);
2838 handler_4
= cmd
->handler
;
2839 handler_4(mon
, args
[0], args
[1], args
[2], args
[3]);
2842 handler_5
= cmd
->handler
;
2843 handler_5(mon
, args
[0], args
[1], args
[2], args
[3], args
[4]);
2846 handler_6
= cmd
->handler
;
2847 handler_6(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5]);
2850 handler_7
= cmd
->handler
;
2851 handler_7(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2855 handler_8
= cmd
->handler
;
2856 handler_8(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2860 handler_9
= cmd
->handler
;
2861 handler_9(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2862 args
[6], args
[7], args
[8]);
2865 handler_10
= cmd
->handler
;
2866 handler_10(mon
, args
[0], args
[1], args
[2], args
[3], args
[4], args
[5],
2867 args
[6], args
[7], args
[8], args
[9]);
2870 monitor_printf(mon
, "unsupported number of arguments: %d\n", nb_args
);
2873 qemu_errors_to_previous();
2876 for(i
= 0; i
< MAX_ARGS
; i
++)
2877 qemu_free(str_allocated
[i
]);
2880 static void cmd_completion(const char *name
, const char *list
)
2882 const char *p
, *pstart
;
2891 p
= pstart
+ strlen(pstart
);
2893 if (len
> sizeof(cmd
) - 2)
2894 len
= sizeof(cmd
) - 2;
2895 memcpy(cmd
, pstart
, len
);
2897 if (name
[0] == '\0' || !strncmp(name
, cmd
, strlen(name
))) {
2898 readline_add_completion(cur_mon
->rs
, cmd
);
2906 static void file_completion(const char *input
)
2911 char file
[1024], file_prefix
[1024];
2915 p
= strrchr(input
, '/');
2918 pstrcpy(file_prefix
, sizeof(file_prefix
), input
);
2919 pstrcpy(path
, sizeof(path
), ".");
2921 input_path_len
= p
- input
+ 1;
2922 memcpy(path
, input
, input_path_len
);
2923 if (input_path_len
> sizeof(path
) - 1)
2924 input_path_len
= sizeof(path
) - 1;
2925 path
[input_path_len
] = '\0';
2926 pstrcpy(file_prefix
, sizeof(file_prefix
), p
+ 1);
2928 #ifdef DEBUG_COMPLETION
2929 monitor_printf(cur_mon
, "input='%s' path='%s' prefix='%s'\n",
2930 input
, path
, file_prefix
);
2932 ffs
= opendir(path
);
2940 if (strstart(d
->d_name
, file_prefix
, NULL
)) {
2941 memcpy(file
, input
, input_path_len
);
2942 if (input_path_len
< sizeof(file
))
2943 pstrcpy(file
+ input_path_len
, sizeof(file
) - input_path_len
,
2945 /* stat the file to find out if it's a directory.
2946 * In that case add a slash to speed up typing long paths
2949 if(S_ISDIR(sb
.st_mode
))
2950 pstrcat(file
, sizeof(file
), "/");
2951 readline_add_completion(cur_mon
->rs
, file
);
2957 static void block_completion_it(void *opaque
, BlockDriverState
*bs
)
2959 const char *name
= bdrv_get_device_name(bs
);
2960 const char *input
= opaque
;
2962 if (input
[0] == '\0' ||
2963 !strncmp(name
, (char *)input
, strlen(input
))) {
2964 readline_add_completion(cur_mon
->rs
, name
);
2968 /* NOTE: this parser is an approximate form of the real command parser */
2969 static void parse_cmdline(const char *cmdline
,
2970 int *pnb_args
, char **args
)
2979 while (qemu_isspace(*p
))
2983 if (nb_args
>= MAX_ARGS
)
2985 ret
= get_str(buf
, sizeof(buf
), &p
);
2986 args
[nb_args
] = qemu_strdup(buf
);
2991 *pnb_args
= nb_args
;
2994 static void monitor_find_completion(const char *cmdline
)
2996 const char *cmdname
;
2997 char *args
[MAX_ARGS
];
2998 int nb_args
, i
, len
;
2999 const char *ptype
, *str
;
3000 const mon_cmd_t
*cmd
;
3003 parse_cmdline(cmdline
, &nb_args
, args
);
3004 #ifdef DEBUG_COMPLETION
3005 for(i
= 0; i
< nb_args
; i
++) {
3006 monitor_printf(cur_mon
, "arg%d = '%s'\n", i
, (char *)args
[i
]);
3010 /* if the line ends with a space, it means we want to complete the
3012 len
= strlen(cmdline
);
3013 if (len
> 0 && qemu_isspace(cmdline
[len
- 1])) {
3014 if (nb_args
>= MAX_ARGS
)
3016 args
[nb_args
++] = qemu_strdup("");
3019 /* command completion */
3024 readline_set_completion_index(cur_mon
->rs
, strlen(cmdname
));
3025 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3026 cmd_completion(cmdname
, cmd
->name
);
3029 /* find the command */
3030 for(cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3031 if (compare_cmd(args
[0], cmd
->name
))
3036 ptype
= cmd
->args_type
;
3037 for(i
= 0; i
< nb_args
- 2; i
++) {
3038 if (*ptype
!= '\0') {
3040 while (*ptype
== '?')
3044 str
= args
[nb_args
- 1];
3045 if (*ptype
== '-' && ptype
[1] != '\0') {
3050 /* file completion */
3051 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3052 file_completion(str
);
3055 /* block device name completion */
3056 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3057 bdrv_iterate(block_completion_it
, (void *)str
);
3060 /* XXX: more generic ? */
3061 if (!strcmp(cmd
->name
, "info")) {
3062 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3063 for(cmd
= info_cmds
; cmd
->name
!= NULL
; cmd
++) {
3064 cmd_completion(str
, cmd
->name
);
3066 } else if (!strcmp(cmd
->name
, "sendkey")) {
3067 char *sep
= strrchr(str
, '-');
3070 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3071 for(key
= key_defs
; key
->name
!= NULL
; key
++) {
3072 cmd_completion(str
, key
->name
);
3074 } else if (!strcmp(cmd
->name
, "help|?")) {
3075 readline_set_completion_index(cur_mon
->rs
, strlen(str
));
3076 for (cmd
= mon_cmds
; cmd
->name
!= NULL
; cmd
++) {
3077 cmd_completion(str
, cmd
->name
);
3085 for(i
= 0; i
< nb_args
; i
++)
3089 static int monitor_can_read(void *opaque
)
3091 Monitor
*mon
= opaque
;
3093 return (mon
->suspend_cnt
== 0) ? 128 : 0;
3096 static void monitor_read(void *opaque
, const uint8_t *buf
, int size
)
3098 Monitor
*old_mon
= cur_mon
;
3104 for (i
= 0; i
< size
; i
++)
3105 readline_handle_byte(cur_mon
->rs
, buf
[i
]);
3107 if (size
== 0 || buf
[size
- 1] != 0)
3108 monitor_printf(cur_mon
, "corrupted command\n");
3110 monitor_handle_command(cur_mon
, (char *)buf
);
3116 static void monitor_command_cb(Monitor
*mon
, const char *cmdline
, void *opaque
)
3118 monitor_suspend(mon
);
3119 monitor_handle_command(mon
, cmdline
);
3120 monitor_resume(mon
);
3123 int monitor_suspend(Monitor
*mon
)
3131 void monitor_resume(Monitor
*mon
)
3135 if (--mon
->suspend_cnt
== 0)
3136 readline_show_prompt(mon
->rs
);
3139 static void monitor_event(void *opaque
, int event
)
3141 Monitor
*mon
= opaque
;
3144 case CHR_EVENT_MUX_IN
:
3145 readline_restart(mon
->rs
);
3146 monitor_resume(mon
);
3150 case CHR_EVENT_MUX_OUT
:
3151 if (mon
->suspend_cnt
== 0)
3152 monitor_printf(mon
, "\n");
3154 monitor_suspend(mon
);
3157 case CHR_EVENT_RESET
:
3158 monitor_printf(mon
, "QEMU %s monitor - type 'help' for more "
3159 "information\n", QEMU_VERSION
);
3160 if (mon
->chr
->focus
== 0)
3161 readline_show_prompt(mon
->rs
);
3175 void monitor_init(CharDriverState
*chr
, int flags
)
3177 static int is_first_init
= 1;
3180 if (is_first_init
) {
3181 key_timer
= qemu_new_timer(vm_clock
, release_keys
, NULL
);
3185 mon
= qemu_mallocz(sizeof(*mon
));
3189 if (mon
->chr
->focus
!= 0)
3190 mon
->suspend_cnt
= 1; /* mux'ed monitors start suspended */
3191 if (flags
& MONITOR_USE_READLINE
) {
3192 mon
->rs
= readline_init(mon
, monitor_find_completion
);
3193 monitor_read_command(mon
, 0);
3196 qemu_chr_add_handlers(chr
, monitor_can_read
, monitor_read
, monitor_event
,
3199 LIST_INSERT_HEAD(&mon_list
, mon
, entry
);
3200 if (!cur_mon
|| (flags
& MONITOR_IS_DEFAULT
))
3204 static void bdrv_password_cb(Monitor
*mon
, const char *password
, void *opaque
)
3206 BlockDriverState
*bs
= opaque
;
3209 if (bdrv_set_key(bs
, password
) != 0) {
3210 monitor_printf(mon
, "invalid password\n");
3213 if (mon
->password_completion_cb
)
3214 mon
->password_completion_cb(mon
->password_opaque
, ret
);
3216 monitor_read_command(mon
, 1);
3219 void monitor_read_bdrv_key_start(Monitor
*mon
, BlockDriverState
*bs
,
3220 BlockDriverCompletionFunc
*completion_cb
,
3225 if (!bdrv_key_required(bs
)) {
3227 completion_cb(opaque
, 0);
3231 monitor_printf(mon
, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs
),
3232 bdrv_get_encrypted_filename(bs
));
3234 mon
->password_completion_cb
= completion_cb
;
3235 mon
->password_opaque
= opaque
;
3237 err
= monitor_read_password(mon
, bdrv_password_cb
, bs
);
3239 if (err
&& completion_cb
)
3240 completion_cb(opaque
, err
);
3243 typedef struct QemuErrorSink QemuErrorSink
;
3244 struct QemuErrorSink
{
3253 QemuErrorSink
*previous
;
3256 static QemuErrorSink
*qemu_error_sink
;
3258 void qemu_errors_to_file(FILE *fp
)
3260 QemuErrorSink
*sink
;
3262 sink
= qemu_mallocz(sizeof(*sink
));
3263 sink
->dest
= ERR_SINK_FILE
;
3265 sink
->previous
= qemu_error_sink
;
3266 qemu_error_sink
= sink
;
3269 void qemu_errors_to_mon(Monitor
*mon
)
3271 QemuErrorSink
*sink
;
3273 sink
= qemu_mallocz(sizeof(*sink
));
3274 sink
->dest
= ERR_SINK_MONITOR
;
3276 sink
->previous
= qemu_error_sink
;
3277 qemu_error_sink
= sink
;
3280 void qemu_errors_to_previous(void)
3282 QemuErrorSink
*sink
;
3284 assert(qemu_error_sink
!= NULL
);
3285 sink
= qemu_error_sink
;
3286 qemu_error_sink
= sink
->previous
;
3290 void qemu_error(const char *fmt
, ...)
3294 assert(qemu_error_sink
!= NULL
);
3295 switch (qemu_error_sink
->dest
) {
3297 va_start(args
, fmt
);
3298 vfprintf(qemu_error_sink
->fp
, fmt
, args
);
3301 case ERR_SINK_MONITOR
:
3302 va_start(args
, fmt
);
3303 monitor_vprintf(qemu_error_sink
->mon
, fmt
, args
);