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
26 #include "hw/pcmcia.h"
31 #include "qemu-char.h"
37 #include "audio/audio.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
46 //#define DEBUG_COMPLETION
52 * 'B' block device name
53 * 's' string (accept optional quote)
55 * 'l' target long (32 or 64 bit)
56 * '/' optional gdb-like print format (like "/10x")
58 * '?' optional type (for 'F', 's' and 'i')
62 typedef struct mon_cmd_t {
64 const char *args_type;
78 BlockDriverCompletionFunc *password_completion_cb;
79 void *password_opaque;
80 LIST_ENTRY(Monitor) entry;
83 static LIST_HEAD(mon_list, Monitor) mon_list;
85 static const mon_cmd_t mon_cmds[];
86 static const mon_cmd_t info_cmds[];
88 Monitor *cur_mon = NULL;
90 static void monitor_command_cb(Monitor *mon, const char *cmdline,
93 static void monitor_read_command(Monitor *mon, int show_prompt)
95 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
97 readline_show_prompt(mon->rs);
100 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
104 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
105 /* prompt is printed on return from the command handler */
108 monitor_printf(mon, "terminal does not support password prompting\n");
113 void monitor_flush(Monitor *mon)
115 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
116 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
117 mon->outbuf_index = 0;
121 /* flush at every end of line or if the buffer is full */
122 static void monitor_puts(Monitor *mon, const char *str)
134 mon->outbuf[mon->outbuf_index++] = '\r';
135 mon->outbuf[mon->outbuf_index++] = c;
136 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
142 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
145 vsnprintf(buf, sizeof(buf), fmt, ap);
146 monitor_puts(mon, buf);
149 void monitor_printf(Monitor *mon, const char *fmt, ...)
153 monitor_vprintf(mon, fmt, ap);
157 void monitor_print_filename(Monitor *mon, const char *filename)
161 for (i = 0; filename[i]; i++) {
162 switch (filename[i]) {
166 monitor_printf(mon, "\\%c", filename[i]);
169 monitor_printf(mon, "\\t");
172 monitor_printf(mon, "\\r");
175 monitor_printf(mon, "\\n");
178 monitor_printf(mon, "%c", filename[i]);
184 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
188 monitor_vprintf((Monitor *)stream, fmt, ap);
193 static int compare_cmd(const char *name, const char *list)
195 const char *p, *pstart;
203 p = pstart + strlen(pstart);
204 if ((p - pstart) == len && !memcmp(pstart, name, len))
213 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
214 const char *prefix, const char *name)
216 const mon_cmd_t *cmd;
218 for(cmd = cmds; cmd->name != NULL; cmd++) {
219 if (!name || !strcmp(name, cmd->name))
220 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
221 cmd->params, cmd->help);
225 static void help_cmd(Monitor *mon, const char *name)
227 if (name && !strcmp(name, "info")) {
228 help_cmd_dump(mon, info_cmds, "info ", NULL);
230 help_cmd_dump(mon, mon_cmds, "", name);
231 if (name && !strcmp(name, "log")) {
232 const CPULogItem *item;
233 monitor_printf(mon, "Log items (comma separated):\n");
234 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
235 for(item = cpu_log_items; item->mask != 0; item++) {
236 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
242 static void do_commit(Monitor *mon, const char *device)
246 all_devices = !strcmp(device, "all");
247 for (i = 0; i < nb_drives; i++) {
249 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
250 bdrv_commit(drives_table[i].bdrv);
254 static void do_info(Monitor *mon, const char *item)
256 const mon_cmd_t *cmd;
257 void (*handler)(Monitor *);
261 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
262 if (compare_cmd(item, cmd->name))
266 help_cmd(mon, "info");
269 handler = cmd->handler;
273 static void do_info_version(Monitor *mon)
275 monitor_printf(mon, "%s\n", QEMU_VERSION);
278 static void do_info_name(Monitor *mon)
281 monitor_printf(mon, "%s\n", qemu_name);
284 #if defined(TARGET_I386)
285 static void do_info_hpet(Monitor *mon)
287 monitor_printf(mon, "HPET is %s by QEMU\n",
288 (no_hpet) ? "disabled" : "enabled");
292 static void do_info_uuid(Monitor *mon)
294 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
295 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
296 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
297 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
298 qemu_uuid[14], qemu_uuid[15]);
301 /* get the current CPU defined by the user */
302 static int mon_set_cpu(int cpu_index)
306 for(env = first_cpu; env != NULL; env = env->next_cpu) {
307 if (env->cpu_index == cpu_index) {
308 cur_mon->mon_cpu = env;
315 static CPUState *mon_get_cpu(void)
317 if (!cur_mon->mon_cpu) {
320 return cur_mon->mon_cpu;
323 static void do_info_registers(Monitor *mon)
330 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
333 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
338 static void do_info_cpus(Monitor *mon)
342 /* just to set the default cpu if not already done */
345 for(env = first_cpu; env != NULL; env = env->next_cpu) {
346 monitor_printf(mon, "%c CPU #%d:",
347 (env == mon->mon_cpu) ? '*' : ' ',
349 #if defined(TARGET_I386)
350 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
351 env->eip + env->segs[R_CS].base);
352 #elif defined(TARGET_PPC)
353 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
354 #elif defined(TARGET_SPARC)
355 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
357 #elif defined(TARGET_MIPS)
358 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
361 monitor_printf(mon, " (halted)");
362 monitor_printf(mon, "\n");
366 static void do_cpu_set(Monitor *mon, int index)
368 if (mon_set_cpu(index) < 0)
369 monitor_printf(mon, "Invalid CPU index\n");
372 static void do_info_jit(Monitor *mon)
374 dump_exec_info((FILE *)mon, monitor_fprintf);
377 static void do_info_history(Monitor *mon)
386 str = readline_get_history(mon->rs, i);
389 monitor_printf(mon, "%d: '%s'\n", i, str);
394 #if defined(TARGET_PPC)
395 /* XXX: not implemented in other targets */
396 static void do_info_cpu_stats(Monitor *mon)
401 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
405 static void do_quit(Monitor *mon)
410 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
412 if (bdrv_is_inserted(bs)) {
414 if (!bdrv_is_removable(bs)) {
415 monitor_printf(mon, "device is not removable\n");
418 if (bdrv_is_locked(bs)) {
419 monitor_printf(mon, "device is locked\n");
428 static void do_eject(Monitor *mon, int force, const char *filename)
430 BlockDriverState *bs;
432 bs = bdrv_find(filename);
434 monitor_printf(mon, "device not found\n");
437 eject_device(mon, bs, force);
440 static void do_change_block(Monitor *mon, const char *device,
441 const char *filename, const char *fmt)
443 BlockDriverState *bs;
444 BlockDriver *drv = NULL;
446 bs = bdrv_find(device);
448 monitor_printf(mon, "device not found\n");
452 drv = bdrv_find_format(fmt);
454 monitor_printf(mon, "invalid format %s\n", fmt);
458 if (eject_device(mon, bs, 0) < 0)
460 bdrv_open2(bs, filename, 0, drv);
461 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
464 static void change_vnc_password_cb(Monitor *mon, const char *password,
467 if (vnc_display_password(NULL, password) < 0)
468 monitor_printf(mon, "could not set VNC server password\n");
470 monitor_read_command(mon, 1);
473 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
475 if (strcmp(target, "passwd") == 0 ||
476 strcmp(target, "password") == 0) {
479 strncpy(password, arg, sizeof(password));
480 password[sizeof(password) - 1] = '\0';
481 change_vnc_password_cb(mon, password, NULL);
483 monitor_read_password(mon, change_vnc_password_cb, NULL);
486 if (vnc_display_open(NULL, target) < 0)
487 monitor_printf(mon, "could not start VNC server on %s\n", target);
491 static void do_change(Monitor *mon, const char *device, const char *target,
494 if (strcmp(device, "vnc") == 0) {
495 do_change_vnc(mon, target, arg);
497 do_change_block(mon, device, target, arg);
501 static void do_screen_dump(Monitor *mon, const char *filename)
503 vga_hw_screen_dump(filename);
506 static void do_logfile(Monitor *mon, const char *filename)
508 cpu_set_log_filename(filename);
511 static void do_log(Monitor *mon, const char *items)
515 if (!strcmp(items, "none")) {
518 mask = cpu_str_to_log_mask(items);
520 help_cmd(mon, "log");
527 static void do_stop(Monitor *mon)
529 vm_stop(EXCP_INTERRUPT);
532 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
534 struct bdrv_iterate_context {
539 static void do_cont(Monitor *mon)
541 struct bdrv_iterate_context context = { mon, 0 };
543 bdrv_iterate(encrypted_bdrv_it, &context);
544 /* only resume the vm if all keys are set and valid */
549 static void bdrv_key_cb(void *opaque, int err)
551 Monitor *mon = opaque;
553 /* another key was set successfully, retry to continue */
558 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
560 struct bdrv_iterate_context *context = opaque;
562 if (!context->err && bdrv_key_required(bs)) {
563 context->err = -EBUSY;
564 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
569 #ifdef CONFIG_GDBSTUB
570 static void do_gdbserver(Monitor *mon, const char *port)
573 port = DEFAULT_GDBSTUB_PORT;
574 if (gdbserver_start(port) < 0) {
575 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
578 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
583 static void monitor_printc(Monitor *mon, int c)
585 monitor_printf(mon, "'");
588 monitor_printf(mon, "\\'");
591 monitor_printf(mon, "\\\\");
594 monitor_printf(mon, "\\n");
597 monitor_printf(mon, "\\r");
600 if (c >= 32 && c <= 126) {
601 monitor_printf(mon, "%c", c);
603 monitor_printf(mon, "\\x%02x", c);
607 monitor_printf(mon, "'");
610 static void memory_dump(Monitor *mon, int count, int format, int wsize,
611 target_phys_addr_t addr, int is_physical)
614 int nb_per_line, l, line_size, i, max_digits, len;
622 if (!env && !is_physical)
627 } else if (wsize == 4) {
630 /* as default we use the current CS size */
634 if ((env->efer & MSR_EFER_LMA) &&
635 (env->segs[R_CS].flags & DESC_L_MASK))
639 if (!(env->segs[R_CS].flags & DESC_B_MASK))
644 monitor_disas(mon, env, addr, count, is_physical, flags);
653 nb_per_line = line_size / wsize;
658 max_digits = (wsize * 8 + 2) / 3;
662 max_digits = (wsize * 8) / 4;
666 max_digits = (wsize * 8 * 10 + 32) / 33;
675 monitor_printf(mon, TARGET_FMT_plx ":", addr);
677 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
682 cpu_physical_memory_rw(addr, buf, l, 0);
687 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
688 monitor_printf(mon, " Cannot access memory\n");
697 v = ldub_raw(buf + i);
700 v = lduw_raw(buf + i);
703 v = (uint32_t)ldl_raw(buf + i);
706 v = ldq_raw(buf + i);
709 monitor_printf(mon, " ");
712 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
715 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
718 monitor_printf(mon, "%*" PRIu64, max_digits, v);
721 monitor_printf(mon, "%*" PRId64, max_digits, v);
724 monitor_printc(mon, v);
729 monitor_printf(mon, "\n");
735 #if TARGET_LONG_BITS == 64
736 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
738 #define GET_TLONG(h, l) (l)
741 static void do_memory_dump(Monitor *mon, int count, int format, int size,
742 uint32_t addrh, uint32_t addrl)
744 target_long addr = GET_TLONG(addrh, addrl);
745 memory_dump(mon, count, format, size, addr, 0);
748 #if TARGET_PHYS_ADDR_BITS > 32
749 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
751 #define GET_TPHYSADDR(h, l) (l)
754 static void do_physical_memory_dump(Monitor *mon, int count, int format,
755 int size, uint32_t addrh, uint32_t addrl)
758 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
759 memory_dump(mon, count, format, size, addr, 1);
762 static void do_print(Monitor *mon, int count, int format, int size,
763 unsigned int valh, unsigned int vall)
765 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
766 #if TARGET_PHYS_ADDR_BITS == 32
769 monitor_printf(mon, "%#o", val);
772 monitor_printf(mon, "%#x", val);
775 monitor_printf(mon, "%u", val);
779 monitor_printf(mon, "%d", val);
782 monitor_printc(mon, val);
788 monitor_printf(mon, "%#" PRIo64, val);
791 monitor_printf(mon, "%#" PRIx64, val);
794 monitor_printf(mon, "%" PRIu64, val);
798 monitor_printf(mon, "%" PRId64, val);
801 monitor_printc(mon, val);
805 monitor_printf(mon, "\n");
808 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
809 uint32_t size, const char *filename)
812 target_long addr = GET_TLONG(valh, vall);
821 f = fopen(filename, "wb");
823 monitor_printf(mon, "could not open '%s'\n", filename);
830 cpu_memory_rw_debug(env, addr, buf, l, 0);
831 fwrite(buf, 1, l, f);
838 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
839 unsigned int vall, uint32_t size,
840 const char *filename)
845 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
847 f = fopen(filename, "wb");
849 monitor_printf(mon, "could not open '%s'\n", filename);
856 cpu_physical_memory_rw(addr, buf, l, 0);
857 fwrite(buf, 1, l, f);
865 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
872 for(addr = start; addr < (start + size); addr++) {
873 cpu_physical_memory_rw(addr, buf, 1, 0);
874 /* BSD sum algorithm ('sum' Unix command) */
875 sum = (sum >> 1) | (sum << 15);
878 monitor_printf(mon, "%05d\n", sum);
886 static const KeyDef key_defs[] = {
913 { 0x0e, "backspace" },
950 { 0x37, "asterisk" },
953 { 0x3a, "caps_lock" },
964 { 0x45, "num_lock" },
965 { 0x46, "scroll_lock" },
967 { 0xb5, "kp_divide" },
968 { 0x37, "kp_multiply" },
969 { 0x4a, "kp_subtract" },
971 { 0x9c, "kp_enter" },
972 { 0x53, "kp_decimal" },
1005 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1020 { 0xfe, "compose" },
1025 static int get_keycode(const char *key)
1031 for(p = key_defs; p->name != NULL; p++) {
1032 if (!strcmp(key, p->name))
1035 if (strstart(key, "0x", NULL)) {
1036 ret = strtoul(key, &endp, 0);
1037 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1043 #define MAX_KEYCODES 16
1044 static uint8_t keycodes[MAX_KEYCODES];
1045 static int nb_pending_keycodes;
1046 static QEMUTimer *key_timer;
1048 static void release_keys(void *opaque)
1052 while (nb_pending_keycodes > 0) {
1053 nb_pending_keycodes--;
1054 keycode = keycodes[nb_pending_keycodes];
1056 kbd_put_keycode(0xe0);
1057 kbd_put_keycode(keycode | 0x80);
1061 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1064 char keyname_buf[16];
1066 int keyname_len, keycode, i;
1068 if (nb_pending_keycodes > 0) {
1069 qemu_del_timer(key_timer);
1076 separator = strchr(string, '-');
1077 keyname_len = separator ? separator - string : strlen(string);
1078 if (keyname_len > 0) {
1079 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1080 if (keyname_len > sizeof(keyname_buf) - 1) {
1081 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1084 if (i == MAX_KEYCODES) {
1085 monitor_printf(mon, "too many keys\n");
1088 keyname_buf[keyname_len] = 0;
1089 keycode = get_keycode(keyname_buf);
1091 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1094 keycodes[i++] = keycode;
1098 string = separator + 1;
1100 nb_pending_keycodes = i;
1101 /* key down events */
1102 for (i = 0; i < nb_pending_keycodes; i++) {
1103 keycode = keycodes[i];
1105 kbd_put_keycode(0xe0);
1106 kbd_put_keycode(keycode & 0x7f);
1108 /* delayed key up events */
1109 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1110 muldiv64(ticks_per_sec, hold_time, 1000));
1113 static int mouse_button_state;
1115 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1119 dx = strtol(dx_str, NULL, 0);
1120 dy = strtol(dy_str, NULL, 0);
1123 dz = strtol(dz_str, NULL, 0);
1124 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1127 static void do_mouse_button(Monitor *mon, int button_state)
1129 mouse_button_state = button_state;
1130 kbd_mouse_event(0, 0, 0, mouse_button_state);
1133 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1134 int addr, int has_index, int index)
1140 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1148 val = cpu_inb(NULL, addr);
1152 val = cpu_inw(NULL, addr);
1156 val = cpu_inl(NULL, addr);
1160 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1161 suffix, addr, size * 2, val);
1164 /* boot_set handler */
1165 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1166 static void *boot_opaque;
1168 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1170 qemu_boot_set_handler = func;
1171 boot_opaque = opaque;
1174 static void do_boot_set(Monitor *mon, const char *bootdevice)
1178 if (qemu_boot_set_handler) {
1179 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1181 monitor_printf(mon, "boot device list now set to %s\n",
1184 monitor_printf(mon, "setting boot device list failed with "
1187 monitor_printf(mon, "no function defined to set boot device list for "
1188 "this architecture\n");
1192 static void do_system_reset(Monitor *mon)
1194 qemu_system_reset_request();
1197 static void do_system_powerdown(Monitor *mon)
1199 qemu_system_powerdown_request();
1202 #if defined(TARGET_I386)
1203 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1205 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1208 pte & PG_GLOBAL_MASK ? 'G' : '-',
1209 pte & PG_PSE_MASK ? 'P' : '-',
1210 pte & PG_DIRTY_MASK ? 'D' : '-',
1211 pte & PG_ACCESSED_MASK ? 'A' : '-',
1212 pte & PG_PCD_MASK ? 'C' : '-',
1213 pte & PG_PWT_MASK ? 'T' : '-',
1214 pte & PG_USER_MASK ? 'U' : '-',
1215 pte & PG_RW_MASK ? 'W' : '-');
1218 static void tlb_info(Monitor *mon)
1222 uint32_t pgd, pde, pte;
1224 env = mon_get_cpu();
1228 if (!(env->cr[0] & CR0_PG_MASK)) {
1229 monitor_printf(mon, "PG disabled\n");
1232 pgd = env->cr[3] & ~0xfff;
1233 for(l1 = 0; l1 < 1024; l1++) {
1234 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1235 pde = le32_to_cpu(pde);
1236 if (pde & PG_PRESENT_MASK) {
1237 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1238 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1240 for(l2 = 0; l2 < 1024; l2++) {
1241 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1242 (uint8_t *)&pte, 4);
1243 pte = le32_to_cpu(pte);
1244 if (pte & PG_PRESENT_MASK) {
1245 print_pte(mon, (l1 << 22) + (l2 << 12),
1255 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1256 uint32_t end, int prot)
1259 prot1 = *plast_prot;
1260 if (prot != prot1) {
1261 if (*pstart != -1) {
1262 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1263 *pstart, end, end - *pstart,
1264 prot1 & PG_USER_MASK ? 'u' : '-',
1266 prot1 & PG_RW_MASK ? 'w' : '-');
1276 static void mem_info(Monitor *mon)
1279 int l1, l2, prot, last_prot;
1280 uint32_t pgd, pde, pte, start, end;
1282 env = mon_get_cpu();
1286 if (!(env->cr[0] & CR0_PG_MASK)) {
1287 monitor_printf(mon, "PG disabled\n");
1290 pgd = env->cr[3] & ~0xfff;
1293 for(l1 = 0; l1 < 1024; l1++) {
1294 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1295 pde = le32_to_cpu(pde);
1297 if (pde & PG_PRESENT_MASK) {
1298 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1299 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1300 mem_print(mon, &start, &last_prot, end, prot);
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 end = (l1 << 22) + (l2 << 12);
1307 if (pte & PG_PRESENT_MASK) {
1308 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1312 mem_print(mon, &start, &last_prot, end, prot);
1317 mem_print(mon, &start, &last_prot, end, prot);
1323 #if defined(TARGET_SH4)
1325 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1327 monitor_printf(mon, " tlb%i:\t"
1328 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1329 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1330 "dirty=%hhu writethrough=%hhu\n",
1332 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1333 tlb->v, tlb->sh, tlb->c, tlb->pr,
1337 static void tlb_info(Monitor *mon)
1339 CPUState *env = mon_get_cpu();
1342 monitor_printf (mon, "ITLB:\n");
1343 for (i = 0 ; i < ITLB_SIZE ; i++)
1344 print_tlb (mon, i, &env->itlb[i]);
1345 monitor_printf (mon, "UTLB:\n");
1346 for (i = 0 ; i < UTLB_SIZE ; i++)
1347 print_tlb (mon, i, &env->utlb[i]);
1352 static void do_info_kqemu(Monitor *mon)
1358 env = mon_get_cpu();
1360 monitor_printf(mon, "No cpu initialized yet");
1363 val = env->kqemu_enabled;
1364 monitor_printf(mon, "kqemu support: ");
1368 monitor_printf(mon, "disabled\n");
1371 monitor_printf(mon, "enabled for user code\n");
1374 monitor_printf(mon, "enabled for user and kernel code\n");
1378 monitor_printf(mon, "kqemu support: not compiled\n");
1382 static void do_info_kvm(Monitor *mon)
1385 monitor_printf(mon, "kvm support: ");
1387 monitor_printf(mon, "enabled\n");
1389 monitor_printf(mon, "disabled\n");
1391 monitor_printf(mon, "kvm support: not compiled\n");
1395 #ifdef CONFIG_PROFILER
1399 int64_t kqemu_exec_count;
1401 int64_t kqemu_ret_int_count;
1402 int64_t kqemu_ret_excp_count;
1403 int64_t kqemu_ret_intr_count;
1405 static void do_info_profile(Monitor *mon)
1411 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1412 dev_time, dev_time / (double)ticks_per_sec);
1413 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1414 qemu_time, qemu_time / (double)ticks_per_sec);
1415 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1416 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1418 kqemu_time, kqemu_time / (double)ticks_per_sec,
1419 kqemu_time / (double)total * 100.0,
1421 kqemu_ret_int_count,
1422 kqemu_ret_excp_count,
1423 kqemu_ret_intr_count);
1426 kqemu_exec_count = 0;
1428 kqemu_ret_int_count = 0;
1429 kqemu_ret_excp_count = 0;
1430 kqemu_ret_intr_count = 0;
1432 kqemu_record_dump();
1436 static void do_info_profile(Monitor *mon)
1438 monitor_printf(mon, "Internal profiler not compiled\n");
1442 /* Capture support */
1443 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1445 static void do_info_capture(Monitor *mon)
1450 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1451 monitor_printf(mon, "[%d]: ", i);
1452 s->ops.info (s->opaque);
1456 static void do_stop_capture(Monitor *mon, int n)
1461 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1463 s->ops.destroy (s->opaque);
1464 LIST_REMOVE (s, entries);
1472 static void do_wav_capture(Monitor *mon, const char *path,
1473 int has_freq, int freq,
1474 int has_bits, int bits,
1475 int has_channels, int nchannels)
1479 s = qemu_mallocz (sizeof (*s));
1481 freq = has_freq ? freq : 44100;
1482 bits = has_bits ? bits : 16;
1483 nchannels = has_channels ? nchannels : 2;
1485 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1486 monitor_printf(mon, "Faied to add wave capture\n");
1489 LIST_INSERT_HEAD (&capture_head, s, entries);
1493 #if defined(TARGET_I386)
1494 static void do_inject_nmi(Monitor *mon, int cpu_index)
1498 for (env = first_cpu; env != NULL; env = env->next_cpu)
1499 if (env->cpu_index == cpu_index) {
1500 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1506 static void do_info_status(Monitor *mon)
1509 monitor_printf(mon, "VM status: running\n");
1511 monitor_printf(mon, "VM status: paused\n");
1515 static void do_balloon(Monitor *mon, int value)
1517 ram_addr_t target = value;
1518 qemu_balloon(target << 20);
1521 static void do_info_balloon(Monitor *mon)
1525 actual = qemu_balloon_status();
1526 if (kvm_enabled() && !kvm_has_sync_mmu())
1527 monitor_printf(mon, "Using KVM without synchronous MMU, "
1528 "ballooning disabled\n");
1529 else if (actual == 0)
1530 monitor_printf(mon, "Ballooning not activated in VM\n");
1532 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1535 /* Please update qemu-doc.texi when adding or changing commands */
1536 static const mon_cmd_t mon_cmds[] = {
1537 { "help|?", "s?", help_cmd,
1538 "[cmd]", "show the help" },
1539 { "commit", "s", do_commit,
1540 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1541 { "info", "s?", do_info,
1542 "subcommand", "show various information about the system state" },
1543 { "q|quit", "", do_quit,
1544 "", "quit the emulator" },
1545 { "eject", "-fB", do_eject,
1546 "[-f] device", "eject a removable medium (use -f to force it)" },
1547 { "change", "BFs?", do_change,
1548 "device filename [format]", "change a removable medium, optional format" },
1549 { "screendump", "F", do_screen_dump,
1550 "filename", "save screen into PPM image 'filename'" },
1551 { "logfile", "F", do_logfile,
1552 "filename", "output logs to 'filename'" },
1553 { "log", "s", do_log,
1554 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1555 { "savevm", "s?", do_savevm,
1556 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1557 { "loadvm", "s", do_loadvm,
1558 "tag|id", "restore a VM snapshot from its tag or id" },
1559 { "delvm", "s", do_delvm,
1560 "tag|id", "delete a VM snapshot from its tag or id" },
1561 { "stop", "", do_stop,
1562 "", "stop emulation", },
1563 { "c|cont", "", do_cont,
1564 "", "resume emulation", },
1565 #ifdef CONFIG_GDBSTUB
1566 { "gdbserver", "s?", do_gdbserver,
1567 "[port]", "start gdbserver session (default port=1234)", },
1569 { "x", "/l", do_memory_dump,
1570 "/fmt addr", "virtual memory dump starting at 'addr'", },
1571 { "xp", "/l", do_physical_memory_dump,
1572 "/fmt addr", "physical memory dump starting at 'addr'", },
1573 { "p|print", "/l", do_print,
1574 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1575 { "i", "/ii.", do_ioport_read,
1576 "/fmt addr", "I/O port read" },
1578 { "sendkey", "si?", do_sendkey,
1579 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1580 { "system_reset", "", do_system_reset,
1581 "", "reset the system" },
1582 { "system_powerdown", "", do_system_powerdown,
1583 "", "send system power down event" },
1584 { "sum", "ii", do_sum,
1585 "addr size", "compute the checksum of a memory region" },
1586 { "usb_add", "s", do_usb_add,
1587 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1588 { "usb_del", "s", do_usb_del,
1589 "device", "remove USB device 'bus.addr'" },
1590 { "cpu", "i", do_cpu_set,
1591 "index", "set the default CPU" },
1592 { "mouse_move", "sss?", do_mouse_move,
1593 "dx dy [dz]", "send mouse move events" },
1594 { "mouse_button", "i", do_mouse_button,
1595 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1596 { "mouse_set", "i", do_mouse_set,
1597 "index", "set which mouse device receives events" },
1599 { "wavcapture", "si?i?i?", do_wav_capture,
1600 "path [frequency bits channels]",
1601 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1603 { "stopcapture", "i", do_stop_capture,
1604 "capture index", "stop capture" },
1605 { "memsave", "lis", do_memory_save,
1606 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1607 { "pmemsave", "lis", do_physical_memory_save,
1608 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1609 { "boot_set", "s", do_boot_set,
1610 "bootdevice", "define new values for the boot device list" },
1611 #if defined(TARGET_I386)
1612 { "nmi", "i", do_inject_nmi,
1613 "cpu", "inject an NMI on the given CPU", },
1615 { "migrate", "-ds", do_migrate,
1616 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1617 { "migrate_cancel", "", do_migrate_cancel,
1618 "", "cancel the current VM migration" },
1619 { "migrate_set_speed", "s", do_migrate_set_speed,
1620 "value", "set maximum speed (in bytes) for migrations" },
1621 #if defined(TARGET_I386)
1622 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1623 "[file=file][,if=type][,bus=n]\n"
1624 "[,unit=m][,media=d][index=i]\n"
1625 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1626 "[snapshot=on|off][,cache=on|off]",
1627 "add drive to PCI storage controller" },
1628 { "pci_add", "sss", pci_device_hot_add, "pci_addr=auto|[[<domain>:]<bus>:]<slot> nic|storage [[vlan=n][,macaddr=addr][,model=type]] [file=file][,if=type][,bus=nr]...", "hot-add PCI device" },
1629 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1630 { "host_net_add", "ss", net_host_device_add,
1631 "[tap,user,socket,vde] options", "add host VLAN client" },
1632 { "host_net_remove", "is", net_host_device_remove,
1633 "vlan_id name", "remove host VLAN client" },
1635 { "balloon", "i", do_balloon,
1636 "target", "request VM to change it's memory allocation (in MB)" },
1637 { "set_link", "ss", do_set_link,
1638 "name [up|down]", "change the link status of a network adapter" },
1642 /* Please update qemu-doc.texi when adding or changing commands */
1643 static const mon_cmd_t info_cmds[] = {
1644 { "version", "", do_info_version,
1645 "", "show the version of QEMU" },
1646 { "network", "", do_info_network,
1647 "", "show the network state" },
1648 { "chardev", "", qemu_chr_info,
1649 "", "show the character devices" },
1650 { "block", "", bdrv_info,
1651 "", "show the block devices" },
1652 { "blockstats", "", bdrv_info_stats,
1653 "", "show block device statistics" },
1654 { "registers", "", do_info_registers,
1655 "", "show the cpu registers" },
1656 { "cpus", "", do_info_cpus,
1657 "", "show infos for each CPU" },
1658 { "history", "", do_info_history,
1659 "", "show the command line history", },
1660 { "irq", "", irq_info,
1661 "", "show the interrupts statistics (if available)", },
1662 { "pic", "", pic_info,
1663 "", "show i8259 (PIC) state", },
1664 { "pci", "", pci_info,
1665 "", "show PCI info", },
1666 #if defined(TARGET_I386) || defined(TARGET_SH4)
1667 { "tlb", "", tlb_info,
1668 "", "show virtual to physical memory mappings", },
1670 #if defined(TARGET_I386)
1671 { "mem", "", mem_info,
1672 "", "show the active virtual memory mappings", },
1673 { "hpet", "", do_info_hpet,
1674 "", "show state of HPET", },
1676 { "jit", "", do_info_jit,
1677 "", "show dynamic compiler info", },
1678 { "kqemu", "", do_info_kqemu,
1679 "", "show KQEMU information", },
1680 { "kvm", "", do_info_kvm,
1681 "", "show KVM information", },
1682 { "usb", "", usb_info,
1683 "", "show guest USB devices", },
1684 { "usbhost", "", usb_host_info,
1685 "", "show host USB devices", },
1686 { "profile", "", do_info_profile,
1687 "", "show profiling information", },
1688 { "capture", "", do_info_capture,
1689 "", "show capture information" },
1690 { "snapshots", "", do_info_snapshots,
1691 "", "show the currently saved VM snapshots" },
1692 { "status", "", do_info_status,
1693 "", "show the current VM status (running|paused)" },
1694 { "pcmcia", "", pcmcia_info,
1695 "", "show guest PCMCIA status" },
1696 { "mice", "", do_info_mice,
1697 "", "show which guest mouse is receiving events" },
1698 { "vnc", "", do_info_vnc,
1699 "", "show the vnc server status"},
1700 { "name", "", do_info_name,
1701 "", "show the current VM name" },
1702 { "uuid", "", do_info_uuid,
1703 "", "show the current VM UUID" },
1704 #if defined(TARGET_PPC)
1705 { "cpustats", "", do_info_cpu_stats,
1706 "", "show CPU statistics", },
1708 #if defined(CONFIG_SLIRP)
1709 { "slirp", "", do_info_slirp,
1710 "", "show SLIRP statistics", },
1712 { "migrate", "", do_info_migrate, "", "show migration status" },
1713 { "balloon", "", do_info_balloon,
1714 "", "show balloon information" },
1718 /*******************************************************************/
1720 static const char *pch;
1721 static jmp_buf expr_env;
1726 typedef struct MonitorDef {
1729 target_long (*get_value)(const struct MonitorDef *md, int val);
1733 #if defined(TARGET_I386)
1734 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1736 CPUState *env = mon_get_cpu();
1739 return env->eip + env->segs[R_CS].base;
1743 #if defined(TARGET_PPC)
1744 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1746 CPUState *env = mon_get_cpu();
1754 for (i = 0; i < 8; i++)
1755 u |= env->crf[i] << (32 - (4 * i));
1760 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1762 CPUState *env = mon_get_cpu();
1768 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1770 CPUState *env = mon_get_cpu();
1776 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1778 CPUState *env = mon_get_cpu();
1781 return cpu_ppc_load_decr(env);
1784 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1786 CPUState *env = mon_get_cpu();
1789 return cpu_ppc_load_tbu(env);
1792 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1794 CPUState *env = mon_get_cpu();
1797 return cpu_ppc_load_tbl(env);
1801 #if defined(TARGET_SPARC)
1802 #ifndef TARGET_SPARC64
1803 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1805 CPUState *env = mon_get_cpu();
1808 return GET_PSR(env);
1812 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1814 CPUState *env = mon_get_cpu();
1817 return env->regwptr[val];
1821 static const MonitorDef monitor_defs[] = {
1824 #define SEG(name, seg) \
1825 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1826 { name ".base", offsetof(CPUState, segs[seg].base) },\
1827 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1829 { "eax", offsetof(CPUState, regs[0]) },
1830 { "ecx", offsetof(CPUState, regs[1]) },
1831 { "edx", offsetof(CPUState, regs[2]) },
1832 { "ebx", offsetof(CPUState, regs[3]) },
1833 { "esp|sp", offsetof(CPUState, regs[4]) },
1834 { "ebp|fp", offsetof(CPUState, regs[5]) },
1835 { "esi", offsetof(CPUState, regs[6]) },
1836 { "edi", offsetof(CPUState, regs[7]) },
1837 #ifdef TARGET_X86_64
1838 { "r8", offsetof(CPUState, regs[8]) },
1839 { "r9", offsetof(CPUState, regs[9]) },
1840 { "r10", offsetof(CPUState, regs[10]) },
1841 { "r11", offsetof(CPUState, regs[11]) },
1842 { "r12", offsetof(CPUState, regs[12]) },
1843 { "r13", offsetof(CPUState, regs[13]) },
1844 { "r14", offsetof(CPUState, regs[14]) },
1845 { "r15", offsetof(CPUState, regs[15]) },
1847 { "eflags", offsetof(CPUState, eflags) },
1848 { "eip", offsetof(CPUState, eip) },
1855 { "pc", 0, monitor_get_pc, },
1856 #elif defined(TARGET_PPC)
1857 /* General purpose registers */
1858 { "r0", offsetof(CPUState, gpr[0]) },
1859 { "r1", offsetof(CPUState, gpr[1]) },
1860 { "r2", offsetof(CPUState, gpr[2]) },
1861 { "r3", offsetof(CPUState, gpr[3]) },
1862 { "r4", offsetof(CPUState, gpr[4]) },
1863 { "r5", offsetof(CPUState, gpr[5]) },
1864 { "r6", offsetof(CPUState, gpr[6]) },
1865 { "r7", offsetof(CPUState, gpr[7]) },
1866 { "r8", offsetof(CPUState, gpr[8]) },
1867 { "r9", offsetof(CPUState, gpr[9]) },
1868 { "r10", offsetof(CPUState, gpr[10]) },
1869 { "r11", offsetof(CPUState, gpr[11]) },
1870 { "r12", offsetof(CPUState, gpr[12]) },
1871 { "r13", offsetof(CPUState, gpr[13]) },
1872 { "r14", offsetof(CPUState, gpr[14]) },
1873 { "r15", offsetof(CPUState, gpr[15]) },
1874 { "r16", offsetof(CPUState, gpr[16]) },
1875 { "r17", offsetof(CPUState, gpr[17]) },
1876 { "r18", offsetof(CPUState, gpr[18]) },
1877 { "r19", offsetof(CPUState, gpr[19]) },
1878 { "r20", offsetof(CPUState, gpr[20]) },
1879 { "r21", offsetof(CPUState, gpr[21]) },
1880 { "r22", offsetof(CPUState, gpr[22]) },
1881 { "r23", offsetof(CPUState, gpr[23]) },
1882 { "r24", offsetof(CPUState, gpr[24]) },
1883 { "r25", offsetof(CPUState, gpr[25]) },
1884 { "r26", offsetof(CPUState, gpr[26]) },
1885 { "r27", offsetof(CPUState, gpr[27]) },
1886 { "r28", offsetof(CPUState, gpr[28]) },
1887 { "r29", offsetof(CPUState, gpr[29]) },
1888 { "r30", offsetof(CPUState, gpr[30]) },
1889 { "r31", offsetof(CPUState, gpr[31]) },
1890 /* Floating point registers */
1891 { "f0", offsetof(CPUState, fpr[0]) },
1892 { "f1", offsetof(CPUState, fpr[1]) },
1893 { "f2", offsetof(CPUState, fpr[2]) },
1894 { "f3", offsetof(CPUState, fpr[3]) },
1895 { "f4", offsetof(CPUState, fpr[4]) },
1896 { "f5", offsetof(CPUState, fpr[5]) },
1897 { "f6", offsetof(CPUState, fpr[6]) },
1898 { "f7", offsetof(CPUState, fpr[7]) },
1899 { "f8", offsetof(CPUState, fpr[8]) },
1900 { "f9", offsetof(CPUState, fpr[9]) },
1901 { "f10", offsetof(CPUState, fpr[10]) },
1902 { "f11", offsetof(CPUState, fpr[11]) },
1903 { "f12", offsetof(CPUState, fpr[12]) },
1904 { "f13", offsetof(CPUState, fpr[13]) },
1905 { "f14", offsetof(CPUState, fpr[14]) },
1906 { "f15", offsetof(CPUState, fpr[15]) },
1907 { "f16", offsetof(CPUState, fpr[16]) },
1908 { "f17", offsetof(CPUState, fpr[17]) },
1909 { "f18", offsetof(CPUState, fpr[18]) },
1910 { "f19", offsetof(CPUState, fpr[19]) },
1911 { "f20", offsetof(CPUState, fpr[20]) },
1912 { "f21", offsetof(CPUState, fpr[21]) },
1913 { "f22", offsetof(CPUState, fpr[22]) },
1914 { "f23", offsetof(CPUState, fpr[23]) },
1915 { "f24", offsetof(CPUState, fpr[24]) },
1916 { "f25", offsetof(CPUState, fpr[25]) },
1917 { "f26", offsetof(CPUState, fpr[26]) },
1918 { "f27", offsetof(CPUState, fpr[27]) },
1919 { "f28", offsetof(CPUState, fpr[28]) },
1920 { "f29", offsetof(CPUState, fpr[29]) },
1921 { "f30", offsetof(CPUState, fpr[30]) },
1922 { "f31", offsetof(CPUState, fpr[31]) },
1923 { "fpscr", offsetof(CPUState, fpscr) },
1924 /* Next instruction pointer */
1925 { "nip|pc", offsetof(CPUState, nip) },
1926 { "lr", offsetof(CPUState, lr) },
1927 { "ctr", offsetof(CPUState, ctr) },
1928 { "decr", 0, &monitor_get_decr, },
1929 { "ccr", 0, &monitor_get_ccr, },
1930 /* Machine state register */
1931 { "msr", 0, &monitor_get_msr, },
1932 { "xer", 0, &monitor_get_xer, },
1933 { "tbu", 0, &monitor_get_tbu, },
1934 { "tbl", 0, &monitor_get_tbl, },
1935 #if defined(TARGET_PPC64)
1936 /* Address space register */
1937 { "asr", offsetof(CPUState, asr) },
1939 /* Segment registers */
1940 { "sdr1", offsetof(CPUState, sdr1) },
1941 { "sr0", offsetof(CPUState, sr[0]) },
1942 { "sr1", offsetof(CPUState, sr[1]) },
1943 { "sr2", offsetof(CPUState, sr[2]) },
1944 { "sr3", offsetof(CPUState, sr[3]) },
1945 { "sr4", offsetof(CPUState, sr[4]) },
1946 { "sr5", offsetof(CPUState, sr[5]) },
1947 { "sr6", offsetof(CPUState, sr[6]) },
1948 { "sr7", offsetof(CPUState, sr[7]) },
1949 { "sr8", offsetof(CPUState, sr[8]) },
1950 { "sr9", offsetof(CPUState, sr[9]) },
1951 { "sr10", offsetof(CPUState, sr[10]) },
1952 { "sr11", offsetof(CPUState, sr[11]) },
1953 { "sr12", offsetof(CPUState, sr[12]) },
1954 { "sr13", offsetof(CPUState, sr[13]) },
1955 { "sr14", offsetof(CPUState, sr[14]) },
1956 { "sr15", offsetof(CPUState, sr[15]) },
1957 /* Too lazy to put BATs and SPRs ... */
1958 #elif defined(TARGET_SPARC)
1959 { "g0", offsetof(CPUState, gregs[0]) },
1960 { "g1", offsetof(CPUState, gregs[1]) },
1961 { "g2", offsetof(CPUState, gregs[2]) },
1962 { "g3", offsetof(CPUState, gregs[3]) },
1963 { "g4", offsetof(CPUState, gregs[4]) },
1964 { "g5", offsetof(CPUState, gregs[5]) },
1965 { "g6", offsetof(CPUState, gregs[6]) },
1966 { "g7", offsetof(CPUState, gregs[7]) },
1967 { "o0", 0, monitor_get_reg },
1968 { "o1", 1, monitor_get_reg },
1969 { "o2", 2, monitor_get_reg },
1970 { "o3", 3, monitor_get_reg },
1971 { "o4", 4, monitor_get_reg },
1972 { "o5", 5, monitor_get_reg },
1973 { "o6", 6, monitor_get_reg },
1974 { "o7", 7, monitor_get_reg },
1975 { "l0", 8, monitor_get_reg },
1976 { "l1", 9, monitor_get_reg },
1977 { "l2", 10, monitor_get_reg },
1978 { "l3", 11, monitor_get_reg },
1979 { "l4", 12, monitor_get_reg },
1980 { "l5", 13, monitor_get_reg },
1981 { "l6", 14, monitor_get_reg },
1982 { "l7", 15, monitor_get_reg },
1983 { "i0", 16, monitor_get_reg },
1984 { "i1", 17, monitor_get_reg },
1985 { "i2", 18, monitor_get_reg },
1986 { "i3", 19, monitor_get_reg },
1987 { "i4", 20, monitor_get_reg },
1988 { "i5", 21, monitor_get_reg },
1989 { "i6", 22, monitor_get_reg },
1990 { "i7", 23, monitor_get_reg },
1991 { "pc", offsetof(CPUState, pc) },
1992 { "npc", offsetof(CPUState, npc) },
1993 { "y", offsetof(CPUState, y) },
1994 #ifndef TARGET_SPARC64
1995 { "psr", 0, &monitor_get_psr, },
1996 { "wim", offsetof(CPUState, wim) },
1998 { "tbr", offsetof(CPUState, tbr) },
1999 { "fsr", offsetof(CPUState, fsr) },
2000 { "f0", offsetof(CPUState, fpr[0]) },
2001 { "f1", offsetof(CPUState, fpr[1]) },
2002 { "f2", offsetof(CPUState, fpr[2]) },
2003 { "f3", offsetof(CPUState, fpr[3]) },
2004 { "f4", offsetof(CPUState, fpr[4]) },
2005 { "f5", offsetof(CPUState, fpr[5]) },
2006 { "f6", offsetof(CPUState, fpr[6]) },
2007 { "f7", offsetof(CPUState, fpr[7]) },
2008 { "f8", offsetof(CPUState, fpr[8]) },
2009 { "f9", offsetof(CPUState, fpr[9]) },
2010 { "f10", offsetof(CPUState, fpr[10]) },
2011 { "f11", offsetof(CPUState, fpr[11]) },
2012 { "f12", offsetof(CPUState, fpr[12]) },
2013 { "f13", offsetof(CPUState, fpr[13]) },
2014 { "f14", offsetof(CPUState, fpr[14]) },
2015 { "f15", offsetof(CPUState, fpr[15]) },
2016 { "f16", offsetof(CPUState, fpr[16]) },
2017 { "f17", offsetof(CPUState, fpr[17]) },
2018 { "f18", offsetof(CPUState, fpr[18]) },
2019 { "f19", offsetof(CPUState, fpr[19]) },
2020 { "f20", offsetof(CPUState, fpr[20]) },
2021 { "f21", offsetof(CPUState, fpr[21]) },
2022 { "f22", offsetof(CPUState, fpr[22]) },
2023 { "f23", offsetof(CPUState, fpr[23]) },
2024 { "f24", offsetof(CPUState, fpr[24]) },
2025 { "f25", offsetof(CPUState, fpr[25]) },
2026 { "f26", offsetof(CPUState, fpr[26]) },
2027 { "f27", offsetof(CPUState, fpr[27]) },
2028 { "f28", offsetof(CPUState, fpr[28]) },
2029 { "f29", offsetof(CPUState, fpr[29]) },
2030 { "f30", offsetof(CPUState, fpr[30]) },
2031 { "f31", offsetof(CPUState, fpr[31]) },
2032 #ifdef TARGET_SPARC64
2033 { "f32", offsetof(CPUState, fpr[32]) },
2034 { "f34", offsetof(CPUState, fpr[34]) },
2035 { "f36", offsetof(CPUState, fpr[36]) },
2036 { "f38", offsetof(CPUState, fpr[38]) },
2037 { "f40", offsetof(CPUState, fpr[40]) },
2038 { "f42", offsetof(CPUState, fpr[42]) },
2039 { "f44", offsetof(CPUState, fpr[44]) },
2040 { "f46", offsetof(CPUState, fpr[46]) },
2041 { "f48", offsetof(CPUState, fpr[48]) },
2042 { "f50", offsetof(CPUState, fpr[50]) },
2043 { "f52", offsetof(CPUState, fpr[52]) },
2044 { "f54", offsetof(CPUState, fpr[54]) },
2045 { "f56", offsetof(CPUState, fpr[56]) },
2046 { "f58", offsetof(CPUState, fpr[58]) },
2047 { "f60", offsetof(CPUState, fpr[60]) },
2048 { "f62", offsetof(CPUState, fpr[62]) },
2049 { "asi", offsetof(CPUState, asi) },
2050 { "pstate", offsetof(CPUState, pstate) },
2051 { "cansave", offsetof(CPUState, cansave) },
2052 { "canrestore", offsetof(CPUState, canrestore) },
2053 { "otherwin", offsetof(CPUState, otherwin) },
2054 { "wstate", offsetof(CPUState, wstate) },
2055 { "cleanwin", offsetof(CPUState, cleanwin) },
2056 { "fprs", offsetof(CPUState, fprs) },
2062 static void expr_error(Monitor *mon, const char *msg)
2064 monitor_printf(mon, "%s\n", msg);
2065 longjmp(expr_env, 1);
2068 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2069 static int get_monitor_def(target_long *pval, const char *name)
2071 const MonitorDef *md;
2074 for(md = monitor_defs; md->name != NULL; md++) {
2075 if (compare_cmd(name, md->name)) {
2076 if (md->get_value) {
2077 *pval = md->get_value(md, md->offset);
2079 CPUState *env = mon_get_cpu();
2082 ptr = (uint8_t *)env + md->offset;
2085 *pval = *(int32_t *)ptr;
2088 *pval = *(target_long *)ptr;
2101 static void next(void)
2105 while (qemu_isspace(*pch))
2110 static int64_t expr_sum(Monitor *mon);
2112 static int64_t expr_unary(Monitor *mon)
2121 n = expr_unary(mon);
2125 n = -expr_unary(mon);
2129 n = ~expr_unary(mon);
2135 expr_error(mon, "')' expected");
2142 expr_error(mon, "character constant expected");
2146 expr_error(mon, "missing terminating \' character");
2156 while ((*pch >= 'a' && *pch <= 'z') ||
2157 (*pch >= 'A' && *pch <= 'Z') ||
2158 (*pch >= '0' && *pch <= '9') ||
2159 *pch == '_' || *pch == '.') {
2160 if ((q - buf) < sizeof(buf) - 1)
2164 while (qemu_isspace(*pch))
2167 ret = get_monitor_def(®, buf);
2169 expr_error(mon, "unknown register");
2171 expr_error(mon, "no cpu defined");
2176 expr_error(mon, "unexpected end of expression");
2180 #if TARGET_PHYS_ADDR_BITS > 32
2181 n = strtoull(pch, &p, 0);
2183 n = strtoul(pch, &p, 0);
2186 expr_error(mon, "invalid char in expression");
2189 while (qemu_isspace(*pch))
2197 static int64_t expr_prod(Monitor *mon)
2202 val = expr_unary(mon);
2205 if (op != '*' && op != '/' && op != '%')
2208 val2 = expr_unary(mon);
2217 expr_error(mon, "division by zero");
2228 static int64_t expr_logic(Monitor *mon)
2233 val = expr_prod(mon);
2236 if (op != '&' && op != '|' && op != '^')
2239 val2 = expr_prod(mon);
2256 static int64_t expr_sum(Monitor *mon)
2261 val = expr_logic(mon);
2264 if (op != '+' && op != '-')
2267 val2 = expr_logic(mon);
2276 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2279 if (setjmp(expr_env)) {
2283 while (qemu_isspace(*pch))
2285 *pval = expr_sum(mon);
2290 static int get_str(char *buf, int buf_size, const char **pp)
2298 while (qemu_isspace(*p))
2308 while (*p != '\0' && *p != '\"') {
2324 qemu_printf("unsupported escape code: '\\%c'\n", c);
2327 if ((q - buf) < buf_size - 1) {
2331 if ((q - buf) < buf_size - 1) {
2338 qemu_printf("unterminated string\n");
2343 while (*p != '\0' && !qemu_isspace(*p)) {
2344 if ((q - buf) < buf_size - 1) {
2355 static int default_fmt_format = 'x';
2356 static int default_fmt_size = 4;
2360 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2362 const char *p, *pstart, *typestr;
2364 int c, nb_args, len, i, has_arg;
2365 const mon_cmd_t *cmd;
2368 void *str_allocated[MAX_ARGS];
2369 void *args[MAX_ARGS];
2370 void (*handler_0)(Monitor *mon);
2371 void (*handler_1)(Monitor *mon, void *arg0);
2372 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2373 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2374 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2376 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2377 void *arg3, void *arg4);
2378 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2379 void *arg3, void *arg4, void *arg5);
2380 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2381 void *arg3, void *arg4, void *arg5, void *arg6);
2384 monitor_printf(mon, "command='%s'\n", cmdline);
2387 /* extract the command name */
2390 while (qemu_isspace(*p))
2395 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2398 if (len > sizeof(cmdname) - 1)
2399 len = sizeof(cmdname) - 1;
2400 memcpy(cmdname, pstart, len);
2401 cmdname[len] = '\0';
2403 /* find the command */
2404 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2405 if (compare_cmd(cmdname, cmd->name))
2408 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2412 for(i = 0; i < MAX_ARGS; i++)
2413 str_allocated[i] = NULL;
2415 /* parse the parameters */
2416 typestr = cmd->args_type;
2431 while (qemu_isspace(*p))
2433 if (*typestr == '?') {
2436 /* no optional string: NULL argument */
2441 ret = get_str(buf, sizeof(buf), &p);
2445 monitor_printf(mon, "%s: filename expected\n",
2449 monitor_printf(mon, "%s: block device name expected\n",
2453 monitor_printf(mon, "%s: string expected\n", cmdname);
2458 str = qemu_malloc(strlen(buf) + 1);
2459 pstrcpy(str, sizeof(buf), buf);
2460 str_allocated[nb_args] = str;
2462 if (nb_args >= MAX_ARGS) {
2464 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2467 args[nb_args++] = str;
2472 int count, format, size;
2474 while (qemu_isspace(*p))
2480 if (qemu_isdigit(*p)) {
2482 while (qemu_isdigit(*p)) {
2483 count = count * 10 + (*p - '0');
2521 if (*p != '\0' && !qemu_isspace(*p)) {
2522 monitor_printf(mon, "invalid char in format: '%c'\n",
2527 format = default_fmt_format;
2528 if (format != 'i') {
2529 /* for 'i', not specifying a size gives -1 as size */
2531 size = default_fmt_size;
2532 default_fmt_size = size;
2534 default_fmt_format = format;
2537 format = default_fmt_format;
2538 if (format != 'i') {
2539 size = default_fmt_size;
2544 if (nb_args + 3 > MAX_ARGS)
2546 args[nb_args++] = (void*)(long)count;
2547 args[nb_args++] = (void*)(long)format;
2548 args[nb_args++] = (void*)(long)size;
2556 while (qemu_isspace(*p))
2558 if (*typestr == '?' || *typestr == '.') {
2559 if (*typestr == '?') {
2567 while (qemu_isspace(*p))
2575 if (nb_args >= MAX_ARGS)
2577 args[nb_args++] = (void *)(long)has_arg;
2579 if (nb_args >= MAX_ARGS)
2585 if (get_expr(mon, &val, &p))
2589 if (nb_args >= MAX_ARGS)
2591 args[nb_args++] = (void *)(long)val;
2593 if ((nb_args + 1) >= MAX_ARGS)
2595 #if TARGET_PHYS_ADDR_BITS > 32
2596 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2598 args[nb_args++] = (void *)0;
2600 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2612 while (qemu_isspace(*p))
2618 monitor_printf(mon, "%s: unsupported option -%c\n",
2625 if (nb_args >= MAX_ARGS)
2627 args[nb_args++] = (void *)(long)has_option;
2632 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2636 /* check that all arguments were parsed */
2637 while (qemu_isspace(*p))
2640 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2647 handler_0 = cmd->handler;
2651 handler_1 = cmd->handler;
2652 handler_1(mon, args[0]);
2655 handler_2 = cmd->handler;
2656 handler_2(mon, args[0], args[1]);
2659 handler_3 = cmd->handler;
2660 handler_3(mon, args[0], args[1], args[2]);
2663 handler_4 = cmd->handler;
2664 handler_4(mon, args[0], args[1], args[2], args[3]);
2667 handler_5 = cmd->handler;
2668 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2671 handler_6 = cmd->handler;
2672 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2675 handler_7 = cmd->handler;
2676 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2680 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2684 for(i = 0; i < MAX_ARGS; i++)
2685 qemu_free(str_allocated[i]);
2689 static void cmd_completion(const char *name, const char *list)
2691 const char *p, *pstart;
2700 p = pstart + strlen(pstart);
2702 if (len > sizeof(cmd) - 2)
2703 len = sizeof(cmd) - 2;
2704 memcpy(cmd, pstart, len);
2706 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2707 readline_add_completion(cur_mon->rs, cmd);
2715 static void file_completion(const char *input)
2720 char file[1024], file_prefix[1024];
2724 p = strrchr(input, '/');
2727 pstrcpy(file_prefix, sizeof(file_prefix), input);
2728 pstrcpy(path, sizeof(path), ".");
2730 input_path_len = p - input + 1;
2731 memcpy(path, input, input_path_len);
2732 if (input_path_len > sizeof(path) - 1)
2733 input_path_len = sizeof(path) - 1;
2734 path[input_path_len] = '\0';
2735 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2737 #ifdef DEBUG_COMPLETION
2738 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2739 input, path, file_prefix);
2741 ffs = opendir(path);
2749 if (strstart(d->d_name, file_prefix, NULL)) {
2750 memcpy(file, input, input_path_len);
2751 if (input_path_len < sizeof(file))
2752 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2754 /* stat the file to find out if it's a directory.
2755 * In that case add a slash to speed up typing long paths
2758 if(S_ISDIR(sb.st_mode))
2759 pstrcat(file, sizeof(file), "/");
2760 readline_add_completion(cur_mon->rs, file);
2766 static void block_completion_it(void *opaque, BlockDriverState *bs)
2768 const char *name = bdrv_get_device_name(bs);
2769 const char *input = opaque;
2771 if (input[0] == '\0' ||
2772 !strncmp(name, (char *)input, strlen(input))) {
2773 readline_add_completion(cur_mon->rs, name);
2777 /* NOTE: this parser is an approximate form of the real command parser */
2778 static void parse_cmdline(const char *cmdline,
2779 int *pnb_args, char **args)
2788 while (qemu_isspace(*p))
2792 if (nb_args >= MAX_ARGS)
2794 ret = get_str(buf, sizeof(buf), &p);
2795 args[nb_args] = qemu_strdup(buf);
2800 *pnb_args = nb_args;
2803 static void monitor_find_completion(const char *cmdline)
2805 const char *cmdname;
2806 char *args[MAX_ARGS];
2807 int nb_args, i, len;
2808 const char *ptype, *str;
2809 const mon_cmd_t *cmd;
2812 parse_cmdline(cmdline, &nb_args, args);
2813 #ifdef DEBUG_COMPLETION
2814 for(i = 0; i < nb_args; i++) {
2815 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2819 /* if the line ends with a space, it means we want to complete the
2821 len = strlen(cmdline);
2822 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2823 if (nb_args >= MAX_ARGS)
2825 args[nb_args++] = qemu_strdup("");
2828 /* command completion */
2833 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2834 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2835 cmd_completion(cmdname, cmd->name);
2838 /* find the command */
2839 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2840 if (compare_cmd(args[0], cmd->name))
2845 ptype = cmd->args_type;
2846 for(i = 0; i < nb_args - 2; i++) {
2847 if (*ptype != '\0') {
2849 while (*ptype == '?')
2853 str = args[nb_args - 1];
2856 /* file completion */
2857 readline_set_completion_index(cur_mon->rs, strlen(str));
2858 file_completion(str);
2861 /* block device name completion */
2862 readline_set_completion_index(cur_mon->rs, strlen(str));
2863 bdrv_iterate(block_completion_it, (void *)str);
2866 /* XXX: more generic ? */
2867 if (!strcmp(cmd->name, "info")) {
2868 readline_set_completion_index(cur_mon->rs, strlen(str));
2869 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2870 cmd_completion(str, cmd->name);
2872 } else if (!strcmp(cmd->name, "sendkey")) {
2873 readline_set_completion_index(cur_mon->rs, strlen(str));
2874 for(key = key_defs; key->name != NULL; key++) {
2875 cmd_completion(str, key->name);
2883 for(i = 0; i < nb_args; i++)
2887 static int monitor_can_read(void *opaque)
2889 Monitor *mon = opaque;
2891 return (mon->suspend_cnt == 0) ? 128 : 0;
2894 static void monitor_read(void *opaque, const uint8_t *buf, int size)
2896 Monitor *old_mon = cur_mon;
2902 for (i = 0; i < size; i++)
2903 readline_handle_byte(cur_mon->rs, buf[i]);
2905 if (size == 0 || buf[size - 1] != 0)
2906 monitor_printf(cur_mon, "corrupted command\n");
2908 monitor_handle_command(cur_mon, (char *)buf);
2914 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
2916 monitor_suspend(mon);
2917 monitor_handle_command(mon, cmdline);
2918 monitor_resume(mon);
2921 int monitor_suspend(Monitor *mon)
2929 void monitor_resume(Monitor *mon)
2933 if (--mon->suspend_cnt == 0)
2934 readline_show_prompt(mon->rs);
2937 static void monitor_event(void *opaque, int event)
2939 Monitor *mon = opaque;
2942 case CHR_EVENT_MUX_IN:
2943 readline_restart(mon->rs);
2944 monitor_resume(mon);
2948 case CHR_EVENT_MUX_OUT:
2949 if (mon->suspend_cnt == 0)
2950 monitor_printf(mon, "\n");
2952 monitor_suspend(mon);
2955 case CHR_EVENT_RESET:
2956 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
2957 "information\n", QEMU_VERSION);
2958 if (mon->chr->focus == 0)
2959 readline_show_prompt(mon->rs);
2964 void monitor_init(CharDriverState *chr, int flags)
2966 static int is_first_init = 1;
2969 if (is_first_init) {
2970 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2974 mon = qemu_mallocz(sizeof(*mon));
2978 if (mon->chr->focus != 0)
2979 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
2980 if (flags & MONITOR_USE_READLINE) {
2981 mon->rs = readline_init(mon, monitor_find_completion);
2982 monitor_read_command(mon, 0);
2985 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
2988 LIST_INSERT_HEAD(&mon_list, mon, entry);
2989 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
2993 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
2995 BlockDriverState *bs = opaque;
2998 if (bdrv_set_key(bs, password) != 0) {
2999 monitor_printf(mon, "invalid password\n");
3002 if (mon->password_completion_cb)
3003 mon->password_completion_cb(mon->password_opaque, ret);
3005 monitor_read_command(mon, 1);
3008 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3009 BlockDriverCompletionFunc *completion_cb,
3014 if (!bdrv_key_required(bs)) {
3016 completion_cb(opaque, 0);
3020 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3021 bdrv_get_encrypted_filename(bs));
3023 mon->password_completion_cb = completion_cb;
3024 mon->password_opaque = opaque;
3026 err = monitor_read_password(mon, bdrv_password_cb, bs);
3028 if (err && completion_cb)
3029 completion_cb(opaque, err);