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;
71 static CharDriverState *monitor_hd[MAX_MON];
72 static int hide_banner;
74 static const mon_cmd_t mon_cmds[];
75 static const mon_cmd_t info_cmds[];
77 static uint8_t term_outbuf[1024];
78 static int term_outbuf_index;
79 static BlockDriverCompletionFunc *password_completion_cb;
80 static void *password_opaque;
84 static void monitor_start_input(void);
86 static CPUState *mon_cpu = NULL;
88 static void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
91 readline_start("Password: ", 1, readline_func, opaque);
94 void monitor_flush(Monitor *mon)
97 if (term_outbuf_index > 0) {
98 for (i = 0; i < MAX_MON; i++)
99 if (monitor_hd[i] && monitor_hd[i]->focus == 0)
100 qemu_chr_write(monitor_hd[i], term_outbuf, term_outbuf_index);
101 term_outbuf_index = 0;
105 /* flush at every end of line or if the buffer is full */
106 static void monitor_puts(Monitor *mon, const char *str)
114 term_outbuf[term_outbuf_index++] = '\r';
115 term_outbuf[term_outbuf_index++] = c;
116 if (term_outbuf_index >= (sizeof(term_outbuf) - 1) ||
122 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
125 vsnprintf(buf, sizeof(buf), fmt, ap);
126 monitor_puts(mon, buf);
129 void monitor_printf(Monitor *mon, const char *fmt, ...)
133 monitor_vprintf(mon, fmt, ap);
137 void monitor_print_filename(Monitor *mon, const char *filename)
141 for (i = 0; filename[i]; i++) {
142 switch (filename[i]) {
146 monitor_printf(mon, "\\%c", filename[i]);
149 monitor_printf(mon, "\\t");
152 monitor_printf(mon, "\\r");
155 monitor_printf(mon, "\\n");
158 monitor_printf(mon, "%c", filename[i]);
164 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
168 monitor_vprintf((Monitor *)stream, fmt, ap);
173 static int compare_cmd(const char *name, const char *list)
175 const char *p, *pstart;
183 p = pstart + strlen(pstart);
184 if ((p - pstart) == len && !memcmp(pstart, name, len))
193 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
194 const char *prefix, const char *name)
196 const mon_cmd_t *cmd;
198 for(cmd = cmds; cmd->name != NULL; cmd++) {
199 if (!name || !strcmp(name, cmd->name))
200 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
201 cmd->params, cmd->help);
205 static void help_cmd(Monitor *mon, const char *name)
207 if (name && !strcmp(name, "info")) {
208 help_cmd_dump(mon, info_cmds, "info ", NULL);
210 help_cmd_dump(mon, mon_cmds, "", name);
211 if (name && !strcmp(name, "log")) {
212 const CPULogItem *item;
213 monitor_printf(mon, "Log items (comma separated):\n");
214 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
215 for(item = cpu_log_items; item->mask != 0; item++) {
216 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
222 static void do_commit(Monitor *mon, const char *device)
226 all_devices = !strcmp(device, "all");
227 for (i = 0; i < nb_drives; i++) {
229 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
230 bdrv_commit(drives_table[i].bdrv);
234 static void do_info(Monitor *mon, const char *item)
236 const mon_cmd_t *cmd;
237 void (*handler)(Monitor *);
241 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
242 if (compare_cmd(item, cmd->name))
246 help_cmd(mon, "info");
249 handler = cmd->handler;
253 static void do_info_version(Monitor *mon)
255 monitor_printf(mon, "%s\n", QEMU_VERSION);
258 static void do_info_name(Monitor *mon)
261 monitor_printf(mon, "%s\n", qemu_name);
264 #if defined(TARGET_I386)
265 static void do_info_hpet(Monitor *mon)
267 monitor_printf(mon, "HPET is %s by QEMU\n",
268 (no_hpet) ? "disabled" : "enabled");
272 static void do_info_uuid(Monitor *mon)
274 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
275 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
276 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
277 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
278 qemu_uuid[14], qemu_uuid[15]);
281 /* get the current CPU defined by the user */
282 static int mon_set_cpu(int cpu_index)
286 for(env = first_cpu; env != NULL; env = env->next_cpu) {
287 if (env->cpu_index == cpu_index) {
295 static CPUState *mon_get_cpu(void)
303 static void do_info_registers(Monitor *mon)
310 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
313 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
318 static void do_info_cpus(Monitor *mon)
322 /* just to set the default cpu if not already done */
325 for(env = first_cpu; env != NULL; env = env->next_cpu) {
326 monitor_printf(mon, "%c CPU #%d:",
327 (env == mon_cpu) ? '*' : ' ',
329 #if defined(TARGET_I386)
330 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
331 env->eip + env->segs[R_CS].base);
332 #elif defined(TARGET_PPC)
333 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
334 #elif defined(TARGET_SPARC)
335 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
337 #elif defined(TARGET_MIPS)
338 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
341 monitor_printf(mon, " (halted)");
342 monitor_printf(mon, "\n");
346 static void do_cpu_set(Monitor *mon, int index)
348 if (mon_set_cpu(index) < 0)
349 monitor_printf(mon, "Invalid CPU index\n");
352 static void do_info_jit(Monitor *mon)
354 dump_exec_info((FILE *)mon, monitor_fprintf);
357 static void do_info_history(Monitor *mon)
364 str = readline_get_history(i);
367 monitor_printf(mon, "%d: '%s'\n", i, str);
372 #if defined(TARGET_PPC)
373 /* XXX: not implemented in other targets */
374 static void do_info_cpu_stats(Monitor *mon)
379 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
383 static void do_quit(Monitor *mon)
388 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
390 if (bdrv_is_inserted(bs)) {
392 if (!bdrv_is_removable(bs)) {
393 monitor_printf(mon, "device is not removable\n");
396 if (bdrv_is_locked(bs)) {
397 monitor_printf(mon, "device is locked\n");
406 static void do_eject(Monitor *mon, int force, const char *filename)
408 BlockDriverState *bs;
410 bs = bdrv_find(filename);
412 monitor_printf(mon, "device not found\n");
415 eject_device(mon, bs, force);
418 static void do_change_block(Monitor *mon, const char *device,
419 const char *filename, const char *fmt)
421 BlockDriverState *bs;
422 BlockDriver *drv = NULL;
424 bs = bdrv_find(device);
426 monitor_printf(mon, "device not found\n");
430 drv = bdrv_find_format(fmt);
432 monitor_printf(mon, "invalid format %s\n", fmt);
436 if (eject_device(mon, bs, 0) < 0)
438 bdrv_open2(bs, filename, 0, drv);
439 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
442 static void change_vnc_password_cb(Monitor *mon, const char *password,
445 if (vnc_display_password(NULL, password) < 0)
446 monitor_printf(mon, "could not set VNC server password\n");
448 monitor_start_input();
451 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
453 if (strcmp(target, "passwd") == 0 ||
454 strcmp(target, "password") == 0) {
457 strncpy(password, arg, sizeof(password));
458 password[sizeof(password) - 1] = '\0';
459 change_vnc_password_cb(mon, password, NULL);
461 monitor_read_password(mon, change_vnc_password_cb, NULL);
464 if (vnc_display_open(NULL, target) < 0)
465 monitor_printf(mon, "could not start VNC server on %s\n", target);
469 static void do_change(Monitor *mon, const char *device, const char *target,
472 if (strcmp(device, "vnc") == 0) {
473 do_change_vnc(mon, target, arg);
475 do_change_block(mon, device, target, arg);
479 static void do_screen_dump(Monitor *mon, const char *filename)
481 vga_hw_screen_dump(filename);
484 static void do_logfile(Monitor *mon, const char *filename)
486 cpu_set_log_filename(filename);
489 static void do_log(Monitor *mon, const char *items)
493 if (!strcmp(items, "none")) {
496 mask = cpu_str_to_log_mask(items);
498 help_cmd(mon, "log");
505 static void do_stop(Monitor *mon)
507 vm_stop(EXCP_INTERRUPT);
510 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
512 struct bdrv_iterate_context {
517 static void do_cont(Monitor *mon)
519 struct bdrv_iterate_context context = { mon, 0 };
521 bdrv_iterate(encrypted_bdrv_it, &context);
522 /* only resume the vm if all keys are set and valid */
527 static void bdrv_key_cb(void *opaque, int err)
529 Monitor *mon = opaque;
531 /* another key was set successfully, retry to continue */
536 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
538 struct bdrv_iterate_context *context = opaque;
540 if (!context->err && bdrv_key_required(bs)) {
541 context->err = -EBUSY;
542 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
547 #ifdef CONFIG_GDBSTUB
548 static void do_gdbserver(Monitor *mon, const char *port)
551 port = DEFAULT_GDBSTUB_PORT;
552 if (gdbserver_start(port) < 0) {
553 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
556 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
561 static void monitor_printc(Monitor *mon, int c)
563 monitor_printf(mon, "'");
566 monitor_printf(mon, "\\'");
569 monitor_printf(mon, "\\\\");
572 monitor_printf(mon, "\\n");
575 monitor_printf(mon, "\\r");
578 if (c >= 32 && c <= 126) {
579 monitor_printf(mon, "%c", c);
581 monitor_printf(mon, "\\x%02x", c);
585 monitor_printf(mon, "'");
588 static void memory_dump(Monitor *mon, int count, int format, int wsize,
589 target_phys_addr_t addr, int is_physical)
592 int nb_per_line, l, line_size, i, max_digits, len;
600 if (!env && !is_physical)
605 } else if (wsize == 4) {
608 /* as default we use the current CS size */
612 if ((env->efer & MSR_EFER_LMA) &&
613 (env->segs[R_CS].flags & DESC_L_MASK))
617 if (!(env->segs[R_CS].flags & DESC_B_MASK))
622 monitor_disas(mon, env, addr, count, is_physical, flags);
631 nb_per_line = line_size / wsize;
636 max_digits = (wsize * 8 + 2) / 3;
640 max_digits = (wsize * 8) / 4;
644 max_digits = (wsize * 8 * 10 + 32) / 33;
653 monitor_printf(mon, TARGET_FMT_plx ":", addr);
655 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
660 cpu_physical_memory_rw(addr, buf, l, 0);
665 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
666 monitor_printf(mon, " Cannot access memory\n");
675 v = ldub_raw(buf + i);
678 v = lduw_raw(buf + i);
681 v = (uint32_t)ldl_raw(buf + i);
684 v = ldq_raw(buf + i);
687 monitor_printf(mon, " ");
690 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
693 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
696 monitor_printf(mon, "%*" PRIu64, max_digits, v);
699 monitor_printf(mon, "%*" PRId64, max_digits, v);
702 monitor_printc(mon, v);
707 monitor_printf(mon, "\n");
713 #if TARGET_LONG_BITS == 64
714 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
716 #define GET_TLONG(h, l) (l)
719 static void do_memory_dump(Monitor *mon, int count, int format, int size,
720 uint32_t addrh, uint32_t addrl)
722 target_long addr = GET_TLONG(addrh, addrl);
723 memory_dump(mon, count, format, size, addr, 0);
726 #if TARGET_PHYS_ADDR_BITS > 32
727 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
729 #define GET_TPHYSADDR(h, l) (l)
732 static void do_physical_memory_dump(Monitor *mon, int count, int format,
733 int size, uint32_t addrh, uint32_t addrl)
736 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
737 memory_dump(mon, count, format, size, addr, 1);
740 static void do_print(Monitor *mon, int count, int format, int size,
741 unsigned int valh, unsigned int vall)
743 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
744 #if TARGET_PHYS_ADDR_BITS == 32
747 monitor_printf(mon, "%#o", val);
750 monitor_printf(mon, "%#x", val);
753 monitor_printf(mon, "%u", val);
757 monitor_printf(mon, "%d", val);
760 monitor_printc(mon, val);
766 monitor_printf(mon, "%#" PRIo64, val);
769 monitor_printf(mon, "%#" PRIx64, val);
772 monitor_printf(mon, "%" PRIu64, val);
776 monitor_printf(mon, "%" PRId64, val);
779 monitor_printc(mon, val);
783 monitor_printf(mon, "\n");
786 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
787 uint32_t size, const char *filename)
790 target_long addr = GET_TLONG(valh, vall);
799 f = fopen(filename, "wb");
801 monitor_printf(mon, "could not open '%s'\n", filename);
808 cpu_memory_rw_debug(env, addr, buf, l, 0);
809 fwrite(buf, 1, l, f);
816 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
817 unsigned int vall, uint32_t size,
818 const char *filename)
823 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
825 f = fopen(filename, "wb");
827 monitor_printf(mon, "could not open '%s'\n", filename);
834 cpu_physical_memory_rw(addr, buf, l, 0);
835 fwrite(buf, 1, l, f);
843 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
850 for(addr = start; addr < (start + size); addr++) {
851 cpu_physical_memory_rw(addr, buf, 1, 0);
852 /* BSD sum algorithm ('sum' Unix command) */
853 sum = (sum >> 1) | (sum << 15);
856 monitor_printf(mon, "%05d\n", sum);
864 static const KeyDef key_defs[] = {
891 { 0x0e, "backspace" },
928 { 0x37, "asterisk" },
931 { 0x3a, "caps_lock" },
942 { 0x45, "num_lock" },
943 { 0x46, "scroll_lock" },
945 { 0xb5, "kp_divide" },
946 { 0x37, "kp_multiply" },
947 { 0x4a, "kp_subtract" },
949 { 0x9c, "kp_enter" },
950 { 0x53, "kp_decimal" },
983 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1003 static int get_keycode(const char *key)
1009 for(p = key_defs; p->name != NULL; p++) {
1010 if (!strcmp(key, p->name))
1013 if (strstart(key, "0x", NULL)) {
1014 ret = strtoul(key, &endp, 0);
1015 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1021 #define MAX_KEYCODES 16
1022 static uint8_t keycodes[MAX_KEYCODES];
1023 static int nb_pending_keycodes;
1024 static QEMUTimer *key_timer;
1026 static void release_keys(void *opaque)
1030 while (nb_pending_keycodes > 0) {
1031 nb_pending_keycodes--;
1032 keycode = keycodes[nb_pending_keycodes];
1034 kbd_put_keycode(0xe0);
1035 kbd_put_keycode(keycode | 0x80);
1039 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1042 char keyname_buf[16];
1044 int keyname_len, keycode, i;
1046 if (nb_pending_keycodes > 0) {
1047 qemu_del_timer(key_timer);
1054 separator = strchr(string, '-');
1055 keyname_len = separator ? separator - string : strlen(string);
1056 if (keyname_len > 0) {
1057 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1058 if (keyname_len > sizeof(keyname_buf) - 1) {
1059 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1062 if (i == MAX_KEYCODES) {
1063 monitor_printf(mon, "too many keys\n");
1066 keyname_buf[keyname_len] = 0;
1067 keycode = get_keycode(keyname_buf);
1069 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1072 keycodes[i++] = keycode;
1076 string = separator + 1;
1078 nb_pending_keycodes = i;
1079 /* key down events */
1080 for (i = 0; i < nb_pending_keycodes; i++) {
1081 keycode = keycodes[i];
1083 kbd_put_keycode(0xe0);
1084 kbd_put_keycode(keycode & 0x7f);
1086 /* delayed key up events */
1087 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1088 muldiv64(ticks_per_sec, hold_time, 1000));
1091 static int mouse_button_state;
1093 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1097 dx = strtol(dx_str, NULL, 0);
1098 dy = strtol(dy_str, NULL, 0);
1101 dz = strtol(dz_str, NULL, 0);
1102 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1105 static void do_mouse_button(Monitor *mon, int button_state)
1107 mouse_button_state = button_state;
1108 kbd_mouse_event(0, 0, 0, mouse_button_state);
1111 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1112 int addr, int has_index, int index)
1118 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1126 val = cpu_inb(NULL, addr);
1130 val = cpu_inw(NULL, addr);
1134 val = cpu_inl(NULL, addr);
1138 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1139 suffix, addr, size * 2, val);
1142 /* boot_set handler */
1143 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1144 static void *boot_opaque;
1146 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1148 qemu_boot_set_handler = func;
1149 boot_opaque = opaque;
1152 static void do_boot_set(Monitor *mon, const char *bootdevice)
1156 if (qemu_boot_set_handler) {
1157 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1159 monitor_printf(mon, "boot device list now set to %s\n",
1162 monitor_printf(mon, "setting boot device list failed with "
1165 monitor_printf(mon, "no function defined to set boot device list for "
1166 "this architecture\n");
1170 static void do_system_reset(Monitor *mon)
1172 qemu_system_reset_request();
1175 static void do_system_powerdown(Monitor *mon)
1177 qemu_system_powerdown_request();
1180 #if defined(TARGET_I386)
1181 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1183 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1186 pte & PG_GLOBAL_MASK ? 'G' : '-',
1187 pte & PG_PSE_MASK ? 'P' : '-',
1188 pte & PG_DIRTY_MASK ? 'D' : '-',
1189 pte & PG_ACCESSED_MASK ? 'A' : '-',
1190 pte & PG_PCD_MASK ? 'C' : '-',
1191 pte & PG_PWT_MASK ? 'T' : '-',
1192 pte & PG_USER_MASK ? 'U' : '-',
1193 pte & PG_RW_MASK ? 'W' : '-');
1196 static void tlb_info(Monitor *mon)
1200 uint32_t pgd, pde, pte;
1202 env = mon_get_cpu();
1206 if (!(env->cr[0] & CR0_PG_MASK)) {
1207 monitor_printf(mon, "PG disabled\n");
1210 pgd = env->cr[3] & ~0xfff;
1211 for(l1 = 0; l1 < 1024; l1++) {
1212 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1213 pde = le32_to_cpu(pde);
1214 if (pde & PG_PRESENT_MASK) {
1215 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1216 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1218 for(l2 = 0; l2 < 1024; l2++) {
1219 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1220 (uint8_t *)&pte, 4);
1221 pte = le32_to_cpu(pte);
1222 if (pte & PG_PRESENT_MASK) {
1223 print_pte(mon, (l1 << 22) + (l2 << 12),
1233 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1234 uint32_t end, int prot)
1237 prot1 = *plast_prot;
1238 if (prot != prot1) {
1239 if (*pstart != -1) {
1240 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1241 *pstart, end, end - *pstart,
1242 prot1 & PG_USER_MASK ? 'u' : '-',
1244 prot1 & PG_RW_MASK ? 'w' : '-');
1254 static void mem_info(Monitor *mon)
1257 int l1, l2, prot, last_prot;
1258 uint32_t pgd, pde, pte, start, end;
1260 env = mon_get_cpu();
1264 if (!(env->cr[0] & CR0_PG_MASK)) {
1265 monitor_printf(mon, "PG disabled\n");
1268 pgd = env->cr[3] & ~0xfff;
1271 for(l1 = 0; l1 < 1024; l1++) {
1272 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1273 pde = le32_to_cpu(pde);
1275 if (pde & PG_PRESENT_MASK) {
1276 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1277 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1278 mem_print(mon, &start, &last_prot, end, prot);
1280 for(l2 = 0; l2 < 1024; l2++) {
1281 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1282 (uint8_t *)&pte, 4);
1283 pte = le32_to_cpu(pte);
1284 end = (l1 << 22) + (l2 << 12);
1285 if (pte & PG_PRESENT_MASK) {
1286 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1290 mem_print(mon, &start, &last_prot, end, prot);
1295 mem_print(mon, &start, &last_prot, end, prot);
1301 #if defined(TARGET_SH4)
1303 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1305 monitor_printf(mon, " tlb%i:\t"
1306 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1307 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1308 "dirty=%hhu writethrough=%hhu\n",
1310 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1311 tlb->v, tlb->sh, tlb->c, tlb->pr,
1315 static void tlb_info(Monitor *mon)
1317 CPUState *env = mon_get_cpu();
1320 monitor_printf (mon, "ITLB:\n");
1321 for (i = 0 ; i < ITLB_SIZE ; i++)
1322 print_tlb (mon, i, &env->itlb[i]);
1323 monitor_printf (mon, "UTLB:\n");
1324 for (i = 0 ; i < UTLB_SIZE ; i++)
1325 print_tlb (mon, i, &env->utlb[i]);
1330 static void do_info_kqemu(Monitor *mon)
1336 env = mon_get_cpu();
1338 monitor_printf(mon, "No cpu initialized yet");
1341 val = env->kqemu_enabled;
1342 monitor_printf(mon, "kqemu support: ");
1346 monitor_printf(mon, "disabled\n");
1349 monitor_printf(mon, "enabled for user code\n");
1352 monitor_printf(mon, "enabled for user and kernel code\n");
1356 monitor_printf(mon, "kqemu support: not compiled\n");
1360 static void do_info_kvm(Monitor *mon)
1363 monitor_printf(mon, "kvm support: ");
1365 monitor_printf(mon, "enabled\n");
1367 monitor_printf(mon, "disabled\n");
1369 monitor_printf(mon, "kvm support: not compiled\n");
1373 #ifdef CONFIG_PROFILER
1377 int64_t kqemu_exec_count;
1379 int64_t kqemu_ret_int_count;
1380 int64_t kqemu_ret_excp_count;
1381 int64_t kqemu_ret_intr_count;
1383 static void do_info_profile(Monitor *mon)
1389 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1390 dev_time, dev_time / (double)ticks_per_sec);
1391 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1392 qemu_time, qemu_time / (double)ticks_per_sec);
1393 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1394 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1396 kqemu_time, kqemu_time / (double)ticks_per_sec,
1397 kqemu_time / (double)total * 100.0,
1399 kqemu_ret_int_count,
1400 kqemu_ret_excp_count,
1401 kqemu_ret_intr_count);
1404 kqemu_exec_count = 0;
1406 kqemu_ret_int_count = 0;
1407 kqemu_ret_excp_count = 0;
1408 kqemu_ret_intr_count = 0;
1410 kqemu_record_dump();
1414 static void do_info_profile(Monitor *mon)
1416 monitor_printf(mon, "Internal profiler not compiled\n");
1420 /* Capture support */
1421 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1423 static void do_info_capture(Monitor *mon)
1428 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1429 monitor_printf(mon, "[%d]: ", i);
1430 s->ops.info (s->opaque);
1434 static void do_stop_capture(Monitor *mon, int n)
1439 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1441 s->ops.destroy (s->opaque);
1442 LIST_REMOVE (s, entries);
1450 static void do_wav_capture(Monitor *mon, const char *path,
1451 int has_freq, int freq,
1452 int has_bits, int bits,
1453 int has_channels, int nchannels)
1457 s = qemu_mallocz (sizeof (*s));
1459 freq = has_freq ? freq : 44100;
1460 bits = has_bits ? bits : 16;
1461 nchannels = has_channels ? nchannels : 2;
1463 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1464 monitor_printf(mon, "Faied to add wave capture\n");
1467 LIST_INSERT_HEAD (&capture_head, s, entries);
1471 #if defined(TARGET_I386)
1472 static void do_inject_nmi(Monitor *mon, int cpu_index)
1476 for (env = first_cpu; env != NULL; env = env->next_cpu)
1477 if (env->cpu_index == cpu_index) {
1478 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1484 static void do_info_status(Monitor *mon)
1487 monitor_printf(mon, "VM status: running\n");
1489 monitor_printf(mon, "VM status: paused\n");
1493 static void do_balloon(Monitor *mon, int value)
1495 ram_addr_t target = value;
1496 qemu_balloon(target << 20);
1499 static void do_info_balloon(Monitor *mon)
1503 actual = qemu_balloon_status();
1504 if (kvm_enabled() && !kvm_has_sync_mmu())
1505 monitor_printf(mon, "Using KVM without synchronous MMU, "
1506 "ballooning disabled\n");
1507 else if (actual == 0)
1508 monitor_printf(mon, "Ballooning not activated in VM\n");
1510 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1513 /* Please update qemu-doc.texi when adding or changing commands */
1514 static const mon_cmd_t mon_cmds[] = {
1515 { "help|?", "s?", help_cmd,
1516 "[cmd]", "show the help" },
1517 { "commit", "s", do_commit,
1518 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1519 { "info", "s?", do_info,
1520 "subcommand", "show various information about the system state" },
1521 { "q|quit", "", do_quit,
1522 "", "quit the emulator" },
1523 { "eject", "-fB", do_eject,
1524 "[-f] device", "eject a removable medium (use -f to force it)" },
1525 { "change", "BFs?", do_change,
1526 "device filename [format]", "change a removable medium, optional format" },
1527 { "screendump", "F", do_screen_dump,
1528 "filename", "save screen into PPM image 'filename'" },
1529 { "logfile", "F", do_logfile,
1530 "filename", "output logs to 'filename'" },
1531 { "log", "s", do_log,
1532 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1533 { "savevm", "s?", do_savevm,
1534 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1535 { "loadvm", "s", do_loadvm,
1536 "tag|id", "restore a VM snapshot from its tag or id" },
1537 { "delvm", "s", do_delvm,
1538 "tag|id", "delete a VM snapshot from its tag or id" },
1539 { "stop", "", do_stop,
1540 "", "stop emulation", },
1541 { "c|cont", "", do_cont,
1542 "", "resume emulation", },
1543 #ifdef CONFIG_GDBSTUB
1544 { "gdbserver", "s?", do_gdbserver,
1545 "[port]", "start gdbserver session (default port=1234)", },
1547 { "x", "/l", do_memory_dump,
1548 "/fmt addr", "virtual memory dump starting at 'addr'", },
1549 { "xp", "/l", do_physical_memory_dump,
1550 "/fmt addr", "physical memory dump starting at 'addr'", },
1551 { "p|print", "/l", do_print,
1552 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1553 { "i", "/ii.", do_ioport_read,
1554 "/fmt addr", "I/O port read" },
1556 { "sendkey", "si?", do_sendkey,
1557 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1558 { "system_reset", "", do_system_reset,
1559 "", "reset the system" },
1560 { "system_powerdown", "", do_system_powerdown,
1561 "", "send system power down event" },
1562 { "sum", "ii", do_sum,
1563 "addr size", "compute the checksum of a memory region" },
1564 { "usb_add", "s", do_usb_add,
1565 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1566 { "usb_del", "s", do_usb_del,
1567 "device", "remove USB device 'bus.addr'" },
1568 { "cpu", "i", do_cpu_set,
1569 "index", "set the default CPU" },
1570 { "mouse_move", "sss?", do_mouse_move,
1571 "dx dy [dz]", "send mouse move events" },
1572 { "mouse_button", "i", do_mouse_button,
1573 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1574 { "mouse_set", "i", do_mouse_set,
1575 "index", "set which mouse device receives events" },
1577 { "wavcapture", "si?i?i?", do_wav_capture,
1578 "path [frequency bits channels]",
1579 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1581 { "stopcapture", "i", do_stop_capture,
1582 "capture index", "stop capture" },
1583 { "memsave", "lis", do_memory_save,
1584 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1585 { "pmemsave", "lis", do_physical_memory_save,
1586 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1587 { "boot_set", "s", do_boot_set,
1588 "bootdevice", "define new values for the boot device list" },
1589 #if defined(TARGET_I386)
1590 { "nmi", "i", do_inject_nmi,
1591 "cpu", "inject an NMI on the given CPU", },
1593 { "migrate", "-ds", do_migrate,
1594 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1595 { "migrate_cancel", "", do_migrate_cancel,
1596 "", "cancel the current VM migration" },
1597 { "migrate_set_speed", "s", do_migrate_set_speed,
1598 "value", "set maximum speed (in bytes) for migrations" },
1599 #if defined(TARGET_I386)
1600 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1601 "[file=file][,if=type][,bus=n]\n"
1602 "[,unit=m][,media=d][index=i]\n"
1603 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1604 "[snapshot=on|off][,cache=on|off]",
1605 "add drive to PCI storage controller" },
1606 { "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" },
1607 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1608 { "host_net_add", "ss", net_host_device_add,
1609 "[tap,user,socket,vde] options", "add host VLAN client" },
1610 { "host_net_remove", "is", net_host_device_remove,
1611 "vlan_id name", "remove host VLAN client" },
1613 { "balloon", "i", do_balloon,
1614 "target", "request VM to change it's memory allocation (in MB)" },
1615 { "set_link", "ss", do_set_link,
1616 "name [up|down]", "change the link status of a network adapter" },
1620 /* Please update qemu-doc.texi when adding or changing commands */
1621 static const mon_cmd_t info_cmds[] = {
1622 { "version", "", do_info_version,
1623 "", "show the version of QEMU" },
1624 { "network", "", do_info_network,
1625 "", "show the network state" },
1626 { "chardev", "", qemu_chr_info,
1627 "", "show the character devices" },
1628 { "block", "", bdrv_info,
1629 "", "show the block devices" },
1630 { "blockstats", "", bdrv_info_stats,
1631 "", "show block device statistics" },
1632 { "registers", "", do_info_registers,
1633 "", "show the cpu registers" },
1634 { "cpus", "", do_info_cpus,
1635 "", "show infos for each CPU" },
1636 { "history", "", do_info_history,
1637 "", "show the command line history", },
1638 { "irq", "", irq_info,
1639 "", "show the interrupts statistics (if available)", },
1640 { "pic", "", pic_info,
1641 "", "show i8259 (PIC) state", },
1642 { "pci", "", pci_info,
1643 "", "show PCI info", },
1644 #if defined(TARGET_I386) || defined(TARGET_SH4)
1645 { "tlb", "", tlb_info,
1646 "", "show virtual to physical memory mappings", },
1648 #if defined(TARGET_I386)
1649 { "mem", "", mem_info,
1650 "", "show the active virtual memory mappings", },
1651 { "hpet", "", do_info_hpet,
1652 "", "show state of HPET", },
1654 { "jit", "", do_info_jit,
1655 "", "show dynamic compiler info", },
1656 { "kqemu", "", do_info_kqemu,
1657 "", "show KQEMU information", },
1658 { "kvm", "", do_info_kvm,
1659 "", "show KVM information", },
1660 { "usb", "", usb_info,
1661 "", "show guest USB devices", },
1662 { "usbhost", "", usb_host_info,
1663 "", "show host USB devices", },
1664 { "profile", "", do_info_profile,
1665 "", "show profiling information", },
1666 { "capture", "", do_info_capture,
1667 "", "show capture information" },
1668 { "snapshots", "", do_info_snapshots,
1669 "", "show the currently saved VM snapshots" },
1670 { "status", "", do_info_status,
1671 "", "show the current VM status (running|paused)" },
1672 { "pcmcia", "", pcmcia_info,
1673 "", "show guest PCMCIA status" },
1674 { "mice", "", do_info_mice,
1675 "", "show which guest mouse is receiving events" },
1676 { "vnc", "", do_info_vnc,
1677 "", "show the vnc server status"},
1678 { "name", "", do_info_name,
1679 "", "show the current VM name" },
1680 { "uuid", "", do_info_uuid,
1681 "", "show the current VM UUID" },
1682 #if defined(TARGET_PPC)
1683 { "cpustats", "", do_info_cpu_stats,
1684 "", "show CPU statistics", },
1686 #if defined(CONFIG_SLIRP)
1687 { "slirp", "", do_info_slirp,
1688 "", "show SLIRP statistics", },
1690 { "migrate", "", do_info_migrate, "", "show migration status" },
1691 { "balloon", "", do_info_balloon,
1692 "", "show balloon information" },
1696 /*******************************************************************/
1698 static const char *pch;
1699 static jmp_buf expr_env;
1704 typedef struct MonitorDef {
1707 target_long (*get_value)(const struct MonitorDef *md, int val);
1711 #if defined(TARGET_I386)
1712 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1714 CPUState *env = mon_get_cpu();
1717 return env->eip + env->segs[R_CS].base;
1721 #if defined(TARGET_PPC)
1722 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1724 CPUState *env = mon_get_cpu();
1732 for (i = 0; i < 8; i++)
1733 u |= env->crf[i] << (32 - (4 * i));
1738 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1740 CPUState *env = mon_get_cpu();
1746 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1748 CPUState *env = mon_get_cpu();
1754 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1756 CPUState *env = mon_get_cpu();
1759 return cpu_ppc_load_decr(env);
1762 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1764 CPUState *env = mon_get_cpu();
1767 return cpu_ppc_load_tbu(env);
1770 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1772 CPUState *env = mon_get_cpu();
1775 return cpu_ppc_load_tbl(env);
1779 #if defined(TARGET_SPARC)
1780 #ifndef TARGET_SPARC64
1781 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1783 CPUState *env = mon_get_cpu();
1786 return GET_PSR(env);
1790 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1792 CPUState *env = mon_get_cpu();
1795 return env->regwptr[val];
1799 static const MonitorDef monitor_defs[] = {
1802 #define SEG(name, seg) \
1803 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1804 { name ".base", offsetof(CPUState, segs[seg].base) },\
1805 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1807 { "eax", offsetof(CPUState, regs[0]) },
1808 { "ecx", offsetof(CPUState, regs[1]) },
1809 { "edx", offsetof(CPUState, regs[2]) },
1810 { "ebx", offsetof(CPUState, regs[3]) },
1811 { "esp|sp", offsetof(CPUState, regs[4]) },
1812 { "ebp|fp", offsetof(CPUState, regs[5]) },
1813 { "esi", offsetof(CPUState, regs[6]) },
1814 { "edi", offsetof(CPUState, regs[7]) },
1815 #ifdef TARGET_X86_64
1816 { "r8", offsetof(CPUState, regs[8]) },
1817 { "r9", offsetof(CPUState, regs[9]) },
1818 { "r10", offsetof(CPUState, regs[10]) },
1819 { "r11", offsetof(CPUState, regs[11]) },
1820 { "r12", offsetof(CPUState, regs[12]) },
1821 { "r13", offsetof(CPUState, regs[13]) },
1822 { "r14", offsetof(CPUState, regs[14]) },
1823 { "r15", offsetof(CPUState, regs[15]) },
1825 { "eflags", offsetof(CPUState, eflags) },
1826 { "eip", offsetof(CPUState, eip) },
1833 { "pc", 0, monitor_get_pc, },
1834 #elif defined(TARGET_PPC)
1835 /* General purpose registers */
1836 { "r0", offsetof(CPUState, gpr[0]) },
1837 { "r1", offsetof(CPUState, gpr[1]) },
1838 { "r2", offsetof(CPUState, gpr[2]) },
1839 { "r3", offsetof(CPUState, gpr[3]) },
1840 { "r4", offsetof(CPUState, gpr[4]) },
1841 { "r5", offsetof(CPUState, gpr[5]) },
1842 { "r6", offsetof(CPUState, gpr[6]) },
1843 { "r7", offsetof(CPUState, gpr[7]) },
1844 { "r8", offsetof(CPUState, gpr[8]) },
1845 { "r9", offsetof(CPUState, gpr[9]) },
1846 { "r10", offsetof(CPUState, gpr[10]) },
1847 { "r11", offsetof(CPUState, gpr[11]) },
1848 { "r12", offsetof(CPUState, gpr[12]) },
1849 { "r13", offsetof(CPUState, gpr[13]) },
1850 { "r14", offsetof(CPUState, gpr[14]) },
1851 { "r15", offsetof(CPUState, gpr[15]) },
1852 { "r16", offsetof(CPUState, gpr[16]) },
1853 { "r17", offsetof(CPUState, gpr[17]) },
1854 { "r18", offsetof(CPUState, gpr[18]) },
1855 { "r19", offsetof(CPUState, gpr[19]) },
1856 { "r20", offsetof(CPUState, gpr[20]) },
1857 { "r21", offsetof(CPUState, gpr[21]) },
1858 { "r22", offsetof(CPUState, gpr[22]) },
1859 { "r23", offsetof(CPUState, gpr[23]) },
1860 { "r24", offsetof(CPUState, gpr[24]) },
1861 { "r25", offsetof(CPUState, gpr[25]) },
1862 { "r26", offsetof(CPUState, gpr[26]) },
1863 { "r27", offsetof(CPUState, gpr[27]) },
1864 { "r28", offsetof(CPUState, gpr[28]) },
1865 { "r29", offsetof(CPUState, gpr[29]) },
1866 { "r30", offsetof(CPUState, gpr[30]) },
1867 { "r31", offsetof(CPUState, gpr[31]) },
1868 /* Floating point registers */
1869 { "f0", offsetof(CPUState, fpr[0]) },
1870 { "f1", offsetof(CPUState, fpr[1]) },
1871 { "f2", offsetof(CPUState, fpr[2]) },
1872 { "f3", offsetof(CPUState, fpr[3]) },
1873 { "f4", offsetof(CPUState, fpr[4]) },
1874 { "f5", offsetof(CPUState, fpr[5]) },
1875 { "f6", offsetof(CPUState, fpr[6]) },
1876 { "f7", offsetof(CPUState, fpr[7]) },
1877 { "f8", offsetof(CPUState, fpr[8]) },
1878 { "f9", offsetof(CPUState, fpr[9]) },
1879 { "f10", offsetof(CPUState, fpr[10]) },
1880 { "f11", offsetof(CPUState, fpr[11]) },
1881 { "f12", offsetof(CPUState, fpr[12]) },
1882 { "f13", offsetof(CPUState, fpr[13]) },
1883 { "f14", offsetof(CPUState, fpr[14]) },
1884 { "f15", offsetof(CPUState, fpr[15]) },
1885 { "f16", offsetof(CPUState, fpr[16]) },
1886 { "f17", offsetof(CPUState, fpr[17]) },
1887 { "f18", offsetof(CPUState, fpr[18]) },
1888 { "f19", offsetof(CPUState, fpr[19]) },
1889 { "f20", offsetof(CPUState, fpr[20]) },
1890 { "f21", offsetof(CPUState, fpr[21]) },
1891 { "f22", offsetof(CPUState, fpr[22]) },
1892 { "f23", offsetof(CPUState, fpr[23]) },
1893 { "f24", offsetof(CPUState, fpr[24]) },
1894 { "f25", offsetof(CPUState, fpr[25]) },
1895 { "f26", offsetof(CPUState, fpr[26]) },
1896 { "f27", offsetof(CPUState, fpr[27]) },
1897 { "f28", offsetof(CPUState, fpr[28]) },
1898 { "f29", offsetof(CPUState, fpr[29]) },
1899 { "f30", offsetof(CPUState, fpr[30]) },
1900 { "f31", offsetof(CPUState, fpr[31]) },
1901 { "fpscr", offsetof(CPUState, fpscr) },
1902 /* Next instruction pointer */
1903 { "nip|pc", offsetof(CPUState, nip) },
1904 { "lr", offsetof(CPUState, lr) },
1905 { "ctr", offsetof(CPUState, ctr) },
1906 { "decr", 0, &monitor_get_decr, },
1907 { "ccr", 0, &monitor_get_ccr, },
1908 /* Machine state register */
1909 { "msr", 0, &monitor_get_msr, },
1910 { "xer", 0, &monitor_get_xer, },
1911 { "tbu", 0, &monitor_get_tbu, },
1912 { "tbl", 0, &monitor_get_tbl, },
1913 #if defined(TARGET_PPC64)
1914 /* Address space register */
1915 { "asr", offsetof(CPUState, asr) },
1917 /* Segment registers */
1918 { "sdr1", offsetof(CPUState, sdr1) },
1919 { "sr0", offsetof(CPUState, sr[0]) },
1920 { "sr1", offsetof(CPUState, sr[1]) },
1921 { "sr2", offsetof(CPUState, sr[2]) },
1922 { "sr3", offsetof(CPUState, sr[3]) },
1923 { "sr4", offsetof(CPUState, sr[4]) },
1924 { "sr5", offsetof(CPUState, sr[5]) },
1925 { "sr6", offsetof(CPUState, sr[6]) },
1926 { "sr7", offsetof(CPUState, sr[7]) },
1927 { "sr8", offsetof(CPUState, sr[8]) },
1928 { "sr9", offsetof(CPUState, sr[9]) },
1929 { "sr10", offsetof(CPUState, sr[10]) },
1930 { "sr11", offsetof(CPUState, sr[11]) },
1931 { "sr12", offsetof(CPUState, sr[12]) },
1932 { "sr13", offsetof(CPUState, sr[13]) },
1933 { "sr14", offsetof(CPUState, sr[14]) },
1934 { "sr15", offsetof(CPUState, sr[15]) },
1935 /* Too lazy to put BATs and SPRs ... */
1936 #elif defined(TARGET_SPARC)
1937 { "g0", offsetof(CPUState, gregs[0]) },
1938 { "g1", offsetof(CPUState, gregs[1]) },
1939 { "g2", offsetof(CPUState, gregs[2]) },
1940 { "g3", offsetof(CPUState, gregs[3]) },
1941 { "g4", offsetof(CPUState, gregs[4]) },
1942 { "g5", offsetof(CPUState, gregs[5]) },
1943 { "g6", offsetof(CPUState, gregs[6]) },
1944 { "g7", offsetof(CPUState, gregs[7]) },
1945 { "o0", 0, monitor_get_reg },
1946 { "o1", 1, monitor_get_reg },
1947 { "o2", 2, monitor_get_reg },
1948 { "o3", 3, monitor_get_reg },
1949 { "o4", 4, monitor_get_reg },
1950 { "o5", 5, monitor_get_reg },
1951 { "o6", 6, monitor_get_reg },
1952 { "o7", 7, monitor_get_reg },
1953 { "l0", 8, monitor_get_reg },
1954 { "l1", 9, monitor_get_reg },
1955 { "l2", 10, monitor_get_reg },
1956 { "l3", 11, monitor_get_reg },
1957 { "l4", 12, monitor_get_reg },
1958 { "l5", 13, monitor_get_reg },
1959 { "l6", 14, monitor_get_reg },
1960 { "l7", 15, monitor_get_reg },
1961 { "i0", 16, monitor_get_reg },
1962 { "i1", 17, monitor_get_reg },
1963 { "i2", 18, monitor_get_reg },
1964 { "i3", 19, monitor_get_reg },
1965 { "i4", 20, monitor_get_reg },
1966 { "i5", 21, monitor_get_reg },
1967 { "i6", 22, monitor_get_reg },
1968 { "i7", 23, monitor_get_reg },
1969 { "pc", offsetof(CPUState, pc) },
1970 { "npc", offsetof(CPUState, npc) },
1971 { "y", offsetof(CPUState, y) },
1972 #ifndef TARGET_SPARC64
1973 { "psr", 0, &monitor_get_psr, },
1974 { "wim", offsetof(CPUState, wim) },
1976 { "tbr", offsetof(CPUState, tbr) },
1977 { "fsr", offsetof(CPUState, fsr) },
1978 { "f0", offsetof(CPUState, fpr[0]) },
1979 { "f1", offsetof(CPUState, fpr[1]) },
1980 { "f2", offsetof(CPUState, fpr[2]) },
1981 { "f3", offsetof(CPUState, fpr[3]) },
1982 { "f4", offsetof(CPUState, fpr[4]) },
1983 { "f5", offsetof(CPUState, fpr[5]) },
1984 { "f6", offsetof(CPUState, fpr[6]) },
1985 { "f7", offsetof(CPUState, fpr[7]) },
1986 { "f8", offsetof(CPUState, fpr[8]) },
1987 { "f9", offsetof(CPUState, fpr[9]) },
1988 { "f10", offsetof(CPUState, fpr[10]) },
1989 { "f11", offsetof(CPUState, fpr[11]) },
1990 { "f12", offsetof(CPUState, fpr[12]) },
1991 { "f13", offsetof(CPUState, fpr[13]) },
1992 { "f14", offsetof(CPUState, fpr[14]) },
1993 { "f15", offsetof(CPUState, fpr[15]) },
1994 { "f16", offsetof(CPUState, fpr[16]) },
1995 { "f17", offsetof(CPUState, fpr[17]) },
1996 { "f18", offsetof(CPUState, fpr[18]) },
1997 { "f19", offsetof(CPUState, fpr[19]) },
1998 { "f20", offsetof(CPUState, fpr[20]) },
1999 { "f21", offsetof(CPUState, fpr[21]) },
2000 { "f22", offsetof(CPUState, fpr[22]) },
2001 { "f23", offsetof(CPUState, fpr[23]) },
2002 { "f24", offsetof(CPUState, fpr[24]) },
2003 { "f25", offsetof(CPUState, fpr[25]) },
2004 { "f26", offsetof(CPUState, fpr[26]) },
2005 { "f27", offsetof(CPUState, fpr[27]) },
2006 { "f28", offsetof(CPUState, fpr[28]) },
2007 { "f29", offsetof(CPUState, fpr[29]) },
2008 { "f30", offsetof(CPUState, fpr[30]) },
2009 { "f31", offsetof(CPUState, fpr[31]) },
2010 #ifdef TARGET_SPARC64
2011 { "f32", offsetof(CPUState, fpr[32]) },
2012 { "f34", offsetof(CPUState, fpr[34]) },
2013 { "f36", offsetof(CPUState, fpr[36]) },
2014 { "f38", offsetof(CPUState, fpr[38]) },
2015 { "f40", offsetof(CPUState, fpr[40]) },
2016 { "f42", offsetof(CPUState, fpr[42]) },
2017 { "f44", offsetof(CPUState, fpr[44]) },
2018 { "f46", offsetof(CPUState, fpr[46]) },
2019 { "f48", offsetof(CPUState, fpr[48]) },
2020 { "f50", offsetof(CPUState, fpr[50]) },
2021 { "f52", offsetof(CPUState, fpr[52]) },
2022 { "f54", offsetof(CPUState, fpr[54]) },
2023 { "f56", offsetof(CPUState, fpr[56]) },
2024 { "f58", offsetof(CPUState, fpr[58]) },
2025 { "f60", offsetof(CPUState, fpr[60]) },
2026 { "f62", offsetof(CPUState, fpr[62]) },
2027 { "asi", offsetof(CPUState, asi) },
2028 { "pstate", offsetof(CPUState, pstate) },
2029 { "cansave", offsetof(CPUState, cansave) },
2030 { "canrestore", offsetof(CPUState, canrestore) },
2031 { "otherwin", offsetof(CPUState, otherwin) },
2032 { "wstate", offsetof(CPUState, wstate) },
2033 { "cleanwin", offsetof(CPUState, cleanwin) },
2034 { "fprs", offsetof(CPUState, fprs) },
2040 static void expr_error(Monitor *mon, const char *msg)
2042 monitor_printf(mon, "%s\n", msg);
2043 longjmp(expr_env, 1);
2046 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2047 static int get_monitor_def(target_long *pval, const char *name)
2049 const MonitorDef *md;
2052 for(md = monitor_defs; md->name != NULL; md++) {
2053 if (compare_cmd(name, md->name)) {
2054 if (md->get_value) {
2055 *pval = md->get_value(md, md->offset);
2057 CPUState *env = mon_get_cpu();
2060 ptr = (uint8_t *)env + md->offset;
2063 *pval = *(int32_t *)ptr;
2066 *pval = *(target_long *)ptr;
2079 static void next(void)
2083 while (qemu_isspace(*pch))
2088 static int64_t expr_sum(Monitor *mon);
2090 static int64_t expr_unary(Monitor *mon)
2099 n = expr_unary(mon);
2103 n = -expr_unary(mon);
2107 n = ~expr_unary(mon);
2113 expr_error(mon, "')' expected");
2120 expr_error(mon, "character constant expected");
2124 expr_error(mon, "missing terminating \' character");
2134 while ((*pch >= 'a' && *pch <= 'z') ||
2135 (*pch >= 'A' && *pch <= 'Z') ||
2136 (*pch >= '0' && *pch <= '9') ||
2137 *pch == '_' || *pch == '.') {
2138 if ((q - buf) < sizeof(buf) - 1)
2142 while (qemu_isspace(*pch))
2145 ret = get_monitor_def(®, buf);
2147 expr_error(mon, "unknown register");
2149 expr_error(mon, "no cpu defined");
2154 expr_error(mon, "unexpected end of expression");
2158 #if TARGET_PHYS_ADDR_BITS > 32
2159 n = strtoull(pch, &p, 0);
2161 n = strtoul(pch, &p, 0);
2164 expr_error(mon, "invalid char in expression");
2167 while (qemu_isspace(*pch))
2175 static int64_t expr_prod(Monitor *mon)
2180 val = expr_unary(mon);
2183 if (op != '*' && op != '/' && op != '%')
2186 val2 = expr_unary(mon);
2195 expr_error(mon, "division by zero");
2206 static int64_t expr_logic(Monitor *mon)
2211 val = expr_prod(mon);
2214 if (op != '&' && op != '|' && op != '^')
2217 val2 = expr_prod(mon);
2234 static int64_t expr_sum(Monitor *mon)
2239 val = expr_logic(mon);
2242 if (op != '+' && op != '-')
2245 val2 = expr_logic(mon);
2254 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2257 if (setjmp(expr_env)) {
2261 while (qemu_isspace(*pch))
2263 *pval = expr_sum(mon);
2268 static int get_str(char *buf, int buf_size, const char **pp)
2276 while (qemu_isspace(*p))
2286 while (*p != '\0' && *p != '\"') {
2302 qemu_printf("unsupported escape code: '\\%c'\n", c);
2305 if ((q - buf) < buf_size - 1) {
2309 if ((q - buf) < buf_size - 1) {
2316 qemu_printf("unterminated string\n");
2321 while (*p != '\0' && !qemu_isspace(*p)) {
2322 if ((q - buf) < buf_size - 1) {
2333 static int default_fmt_format = 'x';
2334 static int default_fmt_size = 4;
2338 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2340 const char *p, *pstart, *typestr;
2342 int c, nb_args, len, i, has_arg;
2343 const mon_cmd_t *cmd;
2346 void *str_allocated[MAX_ARGS];
2347 void *args[MAX_ARGS];
2348 void (*handler_0)(Monitor *mon);
2349 void (*handler_1)(Monitor *mon, void *arg0);
2350 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2351 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2352 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2354 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2355 void *arg3, void *arg4);
2356 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2357 void *arg3, void *arg4, void *arg5);
2358 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2359 void *arg3, void *arg4, void *arg5, void *arg6);
2362 monitor_printf(mon, "command='%s'\n", cmdline);
2365 /* extract the command name */
2368 while (qemu_isspace(*p))
2373 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2376 if (len > sizeof(cmdname) - 1)
2377 len = sizeof(cmdname) - 1;
2378 memcpy(cmdname, pstart, len);
2379 cmdname[len] = '\0';
2381 /* find the command */
2382 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2383 if (compare_cmd(cmdname, cmd->name))
2386 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2390 for(i = 0; i < MAX_ARGS; i++)
2391 str_allocated[i] = NULL;
2393 /* parse the parameters */
2394 typestr = cmd->args_type;
2409 while (qemu_isspace(*p))
2411 if (*typestr == '?') {
2414 /* no optional string: NULL argument */
2419 ret = get_str(buf, sizeof(buf), &p);
2423 monitor_printf(mon, "%s: filename expected\n",
2427 monitor_printf(mon, "%s: block device name expected\n",
2431 monitor_printf(mon, "%s: string expected\n", cmdname);
2436 str = qemu_malloc(strlen(buf) + 1);
2437 pstrcpy(str, sizeof(buf), buf);
2438 str_allocated[nb_args] = str;
2440 if (nb_args >= MAX_ARGS) {
2442 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2445 args[nb_args++] = str;
2450 int count, format, size;
2452 while (qemu_isspace(*p))
2458 if (qemu_isdigit(*p)) {
2460 while (qemu_isdigit(*p)) {
2461 count = count * 10 + (*p - '0');
2499 if (*p != '\0' && !qemu_isspace(*p)) {
2500 monitor_printf(mon, "invalid char in format: '%c'\n",
2505 format = default_fmt_format;
2506 if (format != 'i') {
2507 /* for 'i', not specifying a size gives -1 as size */
2509 size = default_fmt_size;
2510 default_fmt_size = size;
2512 default_fmt_format = format;
2515 format = default_fmt_format;
2516 if (format != 'i') {
2517 size = default_fmt_size;
2522 if (nb_args + 3 > MAX_ARGS)
2524 args[nb_args++] = (void*)(long)count;
2525 args[nb_args++] = (void*)(long)format;
2526 args[nb_args++] = (void*)(long)size;
2534 while (qemu_isspace(*p))
2536 if (*typestr == '?' || *typestr == '.') {
2537 if (*typestr == '?') {
2545 while (qemu_isspace(*p))
2553 if (nb_args >= MAX_ARGS)
2555 args[nb_args++] = (void *)(long)has_arg;
2557 if (nb_args >= MAX_ARGS)
2563 if (get_expr(mon, &val, &p))
2567 if (nb_args >= MAX_ARGS)
2569 args[nb_args++] = (void *)(long)val;
2571 if ((nb_args + 1) >= MAX_ARGS)
2573 #if TARGET_PHYS_ADDR_BITS > 32
2574 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2576 args[nb_args++] = (void *)0;
2578 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2590 while (qemu_isspace(*p))
2596 monitor_printf(mon, "%s: unsupported option -%c\n",
2603 if (nb_args >= MAX_ARGS)
2605 args[nb_args++] = (void *)(long)has_option;
2610 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2614 /* check that all arguments were parsed */
2615 while (qemu_isspace(*p))
2618 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2625 handler_0 = cmd->handler;
2629 handler_1 = cmd->handler;
2630 handler_1(mon, args[0]);
2633 handler_2 = cmd->handler;
2634 handler_2(mon, args[0], args[1]);
2637 handler_3 = cmd->handler;
2638 handler_3(mon, args[0], args[1], args[2]);
2641 handler_4 = cmd->handler;
2642 handler_4(mon, args[0], args[1], args[2], args[3]);
2645 handler_5 = cmd->handler;
2646 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2649 handler_6 = cmd->handler;
2650 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2653 handler_7 = cmd->handler;
2654 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2658 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2662 for(i = 0; i < MAX_ARGS; i++)
2663 qemu_free(str_allocated[i]);
2667 static void cmd_completion(const char *name, const char *list)
2669 const char *p, *pstart;
2678 p = pstart + strlen(pstart);
2680 if (len > sizeof(cmd) - 2)
2681 len = sizeof(cmd) - 2;
2682 memcpy(cmd, pstart, len);
2684 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2685 readline_add_completion(cmd);
2693 static void file_completion(const char *input)
2698 char file[1024], file_prefix[1024];
2702 p = strrchr(input, '/');
2705 pstrcpy(file_prefix, sizeof(file_prefix), input);
2706 pstrcpy(path, sizeof(path), ".");
2708 input_path_len = p - input + 1;
2709 memcpy(path, input, input_path_len);
2710 if (input_path_len > sizeof(path) - 1)
2711 input_path_len = sizeof(path) - 1;
2712 path[input_path_len] = '\0';
2713 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2715 #ifdef DEBUG_COMPLETION
2716 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2717 input, path, file_prefix);
2719 ffs = opendir(path);
2727 if (strstart(d->d_name, file_prefix, NULL)) {
2728 memcpy(file, input, input_path_len);
2729 if (input_path_len < sizeof(file))
2730 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2732 /* stat the file to find out if it's a directory.
2733 * In that case add a slash to speed up typing long paths
2736 if(S_ISDIR(sb.st_mode))
2737 pstrcat(file, sizeof(file), "/");
2738 readline_add_completion(file);
2744 static void block_completion_it(void *opaque, BlockDriverState *bs)
2746 const char *name = bdrv_get_device_name(bs);
2747 const char *input = opaque;
2749 if (input[0] == '\0' ||
2750 !strncmp(name, (char *)input, strlen(input))) {
2751 readline_add_completion(name);
2755 /* NOTE: this parser is an approximate form of the real command parser */
2756 static void parse_cmdline(const char *cmdline,
2757 int *pnb_args, char **args)
2766 while (qemu_isspace(*p))
2770 if (nb_args >= MAX_ARGS)
2772 ret = get_str(buf, sizeof(buf), &p);
2773 args[nb_args] = qemu_strdup(buf);
2778 *pnb_args = nb_args;
2781 void readline_find_completion(const char *cmdline)
2783 const char *cmdname;
2784 char *args[MAX_ARGS];
2785 int nb_args, i, len;
2786 const char *ptype, *str;
2787 const mon_cmd_t *cmd;
2790 parse_cmdline(cmdline, &nb_args, args);
2791 #ifdef DEBUG_COMPLETION
2792 for(i = 0; i < nb_args; i++) {
2793 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2797 /* if the line ends with a space, it means we want to complete the
2799 len = strlen(cmdline);
2800 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2801 if (nb_args >= MAX_ARGS)
2803 args[nb_args++] = qemu_strdup("");
2806 /* command completion */
2811 readline_set_completion_index(strlen(cmdname));
2812 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2813 cmd_completion(cmdname, cmd->name);
2816 /* find the command */
2817 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2818 if (compare_cmd(args[0], cmd->name))
2823 ptype = cmd->args_type;
2824 for(i = 0; i < nb_args - 2; i++) {
2825 if (*ptype != '\0') {
2827 while (*ptype == '?')
2831 str = args[nb_args - 1];
2834 /* file completion */
2835 readline_set_completion_index(strlen(str));
2836 file_completion(str);
2839 /* block device name completion */
2840 readline_set_completion_index(strlen(str));
2841 bdrv_iterate(block_completion_it, (void *)str);
2844 /* XXX: more generic ? */
2845 if (!strcmp(cmd->name, "info")) {
2846 readline_set_completion_index(strlen(str));
2847 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2848 cmd_completion(str, cmd->name);
2850 } else if (!strcmp(cmd->name, "sendkey")) {
2851 readline_set_completion_index(strlen(str));
2852 for(key = key_defs; key->name != NULL; key++) {
2853 cmd_completion(str, key->name);
2861 for(i = 0; i < nb_args; i++)
2865 static int term_can_read(void *opaque)
2870 static void term_read(void *opaque, const uint8_t *buf, int size)
2874 for (i = 0; i < size; i++)
2875 readline_handle_byte(buf[i]);
2878 static int monitor_suspended;
2880 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
2882 monitor_handle_command(mon, cmdline);
2883 if (!monitor_suspended)
2884 readline_show_prompt();
2886 monitor_suspended = 2;
2889 void monitor_suspend(Monitor *mon)
2891 monitor_suspended = 1;
2894 void monitor_resume(Monitor *mon)
2896 if (monitor_suspended == 2)
2897 monitor_start_input();
2898 monitor_suspended = 0;
2901 static void monitor_start_input(void)
2903 readline_start("(qemu) ", 0, monitor_command_cb, NULL);
2904 readline_show_prompt();
2907 static void term_event(void *opaque, int event)
2909 Monitor *mon = opaque;
2911 if (event != CHR_EVENT_RESET)
2915 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
2916 "information\n", QEMU_VERSION);
2917 monitor_start_input();
2920 static int is_first_init = 1;
2922 void monitor_init(CharDriverState *chr, int show_banner)
2926 if (is_first_init) {
2927 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2930 for (i = 0; i < MAX_MON; i++) {
2931 monitor_hd[i] = NULL;
2935 for (i = 0; i < MAX_MON; i++) {
2936 if (monitor_hd[i] == NULL) {
2937 monitor_hd[i] = chr;
2942 hide_banner = !show_banner;
2944 qemu_chr_add_handlers(chr, term_can_read, term_read, term_event, cur_mon);
2946 readline_start("", 0, monitor_command_cb, NULL);
2949 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
2951 BlockDriverState *bs = opaque;
2954 if (bdrv_set_key(bs, password) != 0) {
2955 monitor_printf(mon, "invalid password\n");
2958 if (password_completion_cb)
2959 password_completion_cb(password_opaque, ret);
2961 monitor_start_input();
2964 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
2965 BlockDriverCompletionFunc *completion_cb,
2968 if (!bdrv_key_required(bs)) {
2970 completion_cb(opaque, 0);
2974 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2975 bdrv_get_encrypted_filename(bs));
2977 password_completion_cb = completion_cb;
2978 password_opaque = opaque;
2980 monitor_read_password(mon, bdrv_password_cb, bs);