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 void monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
103 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
104 /* prompt is printed on return from the command handler */
107 void monitor_flush(Monitor *mon)
109 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
110 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
111 mon->outbuf_index = 0;
115 /* flush at every end of line or if the buffer is full */
116 static void monitor_puts(Monitor *mon, const char *str)
128 mon->outbuf[mon->outbuf_index++] = '\r';
129 mon->outbuf[mon->outbuf_index++] = c;
130 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
136 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
139 vsnprintf(buf, sizeof(buf), fmt, ap);
140 monitor_puts(mon, buf);
143 void monitor_printf(Monitor *mon, const char *fmt, ...)
147 monitor_vprintf(mon, fmt, ap);
151 void monitor_print_filename(Monitor *mon, const char *filename)
155 for (i = 0; filename[i]; i++) {
156 switch (filename[i]) {
160 monitor_printf(mon, "\\%c", filename[i]);
163 monitor_printf(mon, "\\t");
166 monitor_printf(mon, "\\r");
169 monitor_printf(mon, "\\n");
172 monitor_printf(mon, "%c", filename[i]);
178 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
182 monitor_vprintf((Monitor *)stream, fmt, ap);
187 static int compare_cmd(const char *name, const char *list)
189 const char *p, *pstart;
197 p = pstart + strlen(pstart);
198 if ((p - pstart) == len && !memcmp(pstart, name, len))
207 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
208 const char *prefix, const char *name)
210 const mon_cmd_t *cmd;
212 for(cmd = cmds; cmd->name != NULL; cmd++) {
213 if (!name || !strcmp(name, cmd->name))
214 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
215 cmd->params, cmd->help);
219 static void help_cmd(Monitor *mon, const char *name)
221 if (name && !strcmp(name, "info")) {
222 help_cmd_dump(mon, info_cmds, "info ", NULL);
224 help_cmd_dump(mon, mon_cmds, "", name);
225 if (name && !strcmp(name, "log")) {
226 const CPULogItem *item;
227 monitor_printf(mon, "Log items (comma separated):\n");
228 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
229 for(item = cpu_log_items; item->mask != 0; item++) {
230 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
236 static void do_commit(Monitor *mon, const char *device)
240 all_devices = !strcmp(device, "all");
241 for (i = 0; i < nb_drives; i++) {
243 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
244 bdrv_commit(drives_table[i].bdrv);
248 static void do_info(Monitor *mon, const char *item)
250 const mon_cmd_t *cmd;
251 void (*handler)(Monitor *);
255 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
256 if (compare_cmd(item, cmd->name))
260 help_cmd(mon, "info");
263 handler = cmd->handler;
267 static void do_info_version(Monitor *mon)
269 monitor_printf(mon, "%s\n", QEMU_VERSION);
272 static void do_info_name(Monitor *mon)
275 monitor_printf(mon, "%s\n", qemu_name);
278 #if defined(TARGET_I386)
279 static void do_info_hpet(Monitor *mon)
281 monitor_printf(mon, "HPET is %s by QEMU\n",
282 (no_hpet) ? "disabled" : "enabled");
286 static void do_info_uuid(Monitor *mon)
288 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
289 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
290 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
291 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
292 qemu_uuid[14], qemu_uuid[15]);
295 /* get the current CPU defined by the user */
296 static int mon_set_cpu(int cpu_index)
300 for(env = first_cpu; env != NULL; env = env->next_cpu) {
301 if (env->cpu_index == cpu_index) {
302 cur_mon->mon_cpu = env;
309 static CPUState *mon_get_cpu(void)
311 if (!cur_mon->mon_cpu) {
314 return cur_mon->mon_cpu;
317 static void do_info_registers(Monitor *mon)
324 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
327 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
332 static void do_info_cpus(Monitor *mon)
336 /* just to set the default cpu if not already done */
339 for(env = first_cpu; env != NULL; env = env->next_cpu) {
340 monitor_printf(mon, "%c CPU #%d:",
341 (env == mon->mon_cpu) ? '*' : ' ',
343 #if defined(TARGET_I386)
344 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
345 env->eip + env->segs[R_CS].base);
346 #elif defined(TARGET_PPC)
347 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
348 #elif defined(TARGET_SPARC)
349 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
351 #elif defined(TARGET_MIPS)
352 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
355 monitor_printf(mon, " (halted)");
356 monitor_printf(mon, "\n");
360 static void do_cpu_set(Monitor *mon, int index)
362 if (mon_set_cpu(index) < 0)
363 monitor_printf(mon, "Invalid CPU index\n");
366 static void do_info_jit(Monitor *mon)
368 dump_exec_info((FILE *)mon, monitor_fprintf);
371 static void do_info_history(Monitor *mon)
378 str = readline_get_history(mon->rs, i);
381 monitor_printf(mon, "%d: '%s'\n", i, str);
386 #if defined(TARGET_PPC)
387 /* XXX: not implemented in other targets */
388 static void do_info_cpu_stats(Monitor *mon)
393 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
397 static void do_quit(Monitor *mon)
402 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
404 if (bdrv_is_inserted(bs)) {
406 if (!bdrv_is_removable(bs)) {
407 monitor_printf(mon, "device is not removable\n");
410 if (bdrv_is_locked(bs)) {
411 monitor_printf(mon, "device is locked\n");
420 static void do_eject(Monitor *mon, int force, const char *filename)
422 BlockDriverState *bs;
424 bs = bdrv_find(filename);
426 monitor_printf(mon, "device not found\n");
429 eject_device(mon, bs, force);
432 static void do_change_block(Monitor *mon, const char *device,
433 const char *filename, const char *fmt)
435 BlockDriverState *bs;
436 BlockDriver *drv = NULL;
438 bs = bdrv_find(device);
440 monitor_printf(mon, "device not found\n");
444 drv = bdrv_find_format(fmt);
446 monitor_printf(mon, "invalid format %s\n", fmt);
450 if (eject_device(mon, bs, 0) < 0)
452 bdrv_open2(bs, filename, 0, drv);
453 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
456 static void change_vnc_password_cb(Monitor *mon, const char *password,
459 if (vnc_display_password(NULL, password) < 0)
460 monitor_printf(mon, "could not set VNC server password\n");
462 monitor_read_command(mon, 1);
465 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
467 if (strcmp(target, "passwd") == 0 ||
468 strcmp(target, "password") == 0) {
471 strncpy(password, arg, sizeof(password));
472 password[sizeof(password) - 1] = '\0';
473 change_vnc_password_cb(mon, password, NULL);
475 monitor_read_password(mon, change_vnc_password_cb, NULL);
478 if (vnc_display_open(NULL, target) < 0)
479 monitor_printf(mon, "could not start VNC server on %s\n", target);
483 static void do_change(Monitor *mon, const char *device, const char *target,
486 if (strcmp(device, "vnc") == 0) {
487 do_change_vnc(mon, target, arg);
489 do_change_block(mon, device, target, arg);
493 static void do_screen_dump(Monitor *mon, const char *filename)
495 vga_hw_screen_dump(filename);
498 static void do_logfile(Monitor *mon, const char *filename)
500 cpu_set_log_filename(filename);
503 static void do_log(Monitor *mon, const char *items)
507 if (!strcmp(items, "none")) {
510 mask = cpu_str_to_log_mask(items);
512 help_cmd(mon, "log");
519 static void do_stop(Monitor *mon)
521 vm_stop(EXCP_INTERRUPT);
524 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
526 struct bdrv_iterate_context {
531 static void do_cont(Monitor *mon)
533 struct bdrv_iterate_context context = { mon, 0 };
535 bdrv_iterate(encrypted_bdrv_it, &context);
536 /* only resume the vm if all keys are set and valid */
541 static void bdrv_key_cb(void *opaque, int err)
543 Monitor *mon = opaque;
545 /* another key was set successfully, retry to continue */
550 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
552 struct bdrv_iterate_context *context = opaque;
554 if (!context->err && bdrv_key_required(bs)) {
555 context->err = -EBUSY;
556 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
561 #ifdef CONFIG_GDBSTUB
562 static void do_gdbserver(Monitor *mon, const char *port)
565 port = DEFAULT_GDBSTUB_PORT;
566 if (gdbserver_start(port) < 0) {
567 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
570 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
575 static void monitor_printc(Monitor *mon, int c)
577 monitor_printf(mon, "'");
580 monitor_printf(mon, "\\'");
583 monitor_printf(mon, "\\\\");
586 monitor_printf(mon, "\\n");
589 monitor_printf(mon, "\\r");
592 if (c >= 32 && c <= 126) {
593 monitor_printf(mon, "%c", c);
595 monitor_printf(mon, "\\x%02x", c);
599 monitor_printf(mon, "'");
602 static void memory_dump(Monitor *mon, int count, int format, int wsize,
603 target_phys_addr_t addr, int is_physical)
606 int nb_per_line, l, line_size, i, max_digits, len;
614 if (!env && !is_physical)
619 } else if (wsize == 4) {
622 /* as default we use the current CS size */
626 if ((env->efer & MSR_EFER_LMA) &&
627 (env->segs[R_CS].flags & DESC_L_MASK))
631 if (!(env->segs[R_CS].flags & DESC_B_MASK))
636 monitor_disas(mon, env, addr, count, is_physical, flags);
645 nb_per_line = line_size / wsize;
650 max_digits = (wsize * 8 + 2) / 3;
654 max_digits = (wsize * 8) / 4;
658 max_digits = (wsize * 8 * 10 + 32) / 33;
667 monitor_printf(mon, TARGET_FMT_plx ":", addr);
669 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
674 cpu_physical_memory_rw(addr, buf, l, 0);
679 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
680 monitor_printf(mon, " Cannot access memory\n");
689 v = ldub_raw(buf + i);
692 v = lduw_raw(buf + i);
695 v = (uint32_t)ldl_raw(buf + i);
698 v = ldq_raw(buf + i);
701 monitor_printf(mon, " ");
704 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
707 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
710 monitor_printf(mon, "%*" PRIu64, max_digits, v);
713 monitor_printf(mon, "%*" PRId64, max_digits, v);
716 monitor_printc(mon, v);
721 monitor_printf(mon, "\n");
727 #if TARGET_LONG_BITS == 64
728 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
730 #define GET_TLONG(h, l) (l)
733 static void do_memory_dump(Monitor *mon, int count, int format, int size,
734 uint32_t addrh, uint32_t addrl)
736 target_long addr = GET_TLONG(addrh, addrl);
737 memory_dump(mon, count, format, size, addr, 0);
740 #if TARGET_PHYS_ADDR_BITS > 32
741 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
743 #define GET_TPHYSADDR(h, l) (l)
746 static void do_physical_memory_dump(Monitor *mon, int count, int format,
747 int size, uint32_t addrh, uint32_t addrl)
750 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
751 memory_dump(mon, count, format, size, addr, 1);
754 static void do_print(Monitor *mon, int count, int format, int size,
755 unsigned int valh, unsigned int vall)
757 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
758 #if TARGET_PHYS_ADDR_BITS == 32
761 monitor_printf(mon, "%#o", val);
764 monitor_printf(mon, "%#x", val);
767 monitor_printf(mon, "%u", val);
771 monitor_printf(mon, "%d", val);
774 monitor_printc(mon, val);
780 monitor_printf(mon, "%#" PRIo64, val);
783 monitor_printf(mon, "%#" PRIx64, val);
786 monitor_printf(mon, "%" PRIu64, val);
790 monitor_printf(mon, "%" PRId64, val);
793 monitor_printc(mon, val);
797 monitor_printf(mon, "\n");
800 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
801 uint32_t size, const char *filename)
804 target_long addr = GET_TLONG(valh, vall);
813 f = fopen(filename, "wb");
815 monitor_printf(mon, "could not open '%s'\n", filename);
822 cpu_memory_rw_debug(env, addr, buf, l, 0);
823 fwrite(buf, 1, l, f);
830 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
831 unsigned int vall, uint32_t size,
832 const char *filename)
837 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
839 f = fopen(filename, "wb");
841 monitor_printf(mon, "could not open '%s'\n", filename);
848 cpu_physical_memory_rw(addr, buf, l, 0);
849 fwrite(buf, 1, l, f);
857 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
864 for(addr = start; addr < (start + size); addr++) {
865 cpu_physical_memory_rw(addr, buf, 1, 0);
866 /* BSD sum algorithm ('sum' Unix command) */
867 sum = (sum >> 1) | (sum << 15);
870 monitor_printf(mon, "%05d\n", sum);
878 static const KeyDef key_defs[] = {
905 { 0x0e, "backspace" },
942 { 0x37, "asterisk" },
945 { 0x3a, "caps_lock" },
956 { 0x45, "num_lock" },
957 { 0x46, "scroll_lock" },
959 { 0xb5, "kp_divide" },
960 { 0x37, "kp_multiply" },
961 { 0x4a, "kp_subtract" },
963 { 0x9c, "kp_enter" },
964 { 0x53, "kp_decimal" },
997 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1012 { 0xfe, "compose" },
1017 static int get_keycode(const char *key)
1023 for(p = key_defs; p->name != NULL; p++) {
1024 if (!strcmp(key, p->name))
1027 if (strstart(key, "0x", NULL)) {
1028 ret = strtoul(key, &endp, 0);
1029 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1035 #define MAX_KEYCODES 16
1036 static uint8_t keycodes[MAX_KEYCODES];
1037 static int nb_pending_keycodes;
1038 static QEMUTimer *key_timer;
1040 static void release_keys(void *opaque)
1044 while (nb_pending_keycodes > 0) {
1045 nb_pending_keycodes--;
1046 keycode = keycodes[nb_pending_keycodes];
1048 kbd_put_keycode(0xe0);
1049 kbd_put_keycode(keycode | 0x80);
1053 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1056 char keyname_buf[16];
1058 int keyname_len, keycode, i;
1060 if (nb_pending_keycodes > 0) {
1061 qemu_del_timer(key_timer);
1068 separator = strchr(string, '-');
1069 keyname_len = separator ? separator - string : strlen(string);
1070 if (keyname_len > 0) {
1071 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1072 if (keyname_len > sizeof(keyname_buf) - 1) {
1073 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1076 if (i == MAX_KEYCODES) {
1077 monitor_printf(mon, "too many keys\n");
1080 keyname_buf[keyname_len] = 0;
1081 keycode = get_keycode(keyname_buf);
1083 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1086 keycodes[i++] = keycode;
1090 string = separator + 1;
1092 nb_pending_keycodes = i;
1093 /* key down events */
1094 for (i = 0; i < nb_pending_keycodes; i++) {
1095 keycode = keycodes[i];
1097 kbd_put_keycode(0xe0);
1098 kbd_put_keycode(keycode & 0x7f);
1100 /* delayed key up events */
1101 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1102 muldiv64(ticks_per_sec, hold_time, 1000));
1105 static int mouse_button_state;
1107 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1111 dx = strtol(dx_str, NULL, 0);
1112 dy = strtol(dy_str, NULL, 0);
1115 dz = strtol(dz_str, NULL, 0);
1116 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1119 static void do_mouse_button(Monitor *mon, int button_state)
1121 mouse_button_state = button_state;
1122 kbd_mouse_event(0, 0, 0, mouse_button_state);
1125 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1126 int addr, int has_index, int index)
1132 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1140 val = cpu_inb(NULL, addr);
1144 val = cpu_inw(NULL, addr);
1148 val = cpu_inl(NULL, addr);
1152 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1153 suffix, addr, size * 2, val);
1156 /* boot_set handler */
1157 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1158 static void *boot_opaque;
1160 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1162 qemu_boot_set_handler = func;
1163 boot_opaque = opaque;
1166 static void do_boot_set(Monitor *mon, const char *bootdevice)
1170 if (qemu_boot_set_handler) {
1171 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1173 monitor_printf(mon, "boot device list now set to %s\n",
1176 monitor_printf(mon, "setting boot device list failed with "
1179 monitor_printf(mon, "no function defined to set boot device list for "
1180 "this architecture\n");
1184 static void do_system_reset(Monitor *mon)
1186 qemu_system_reset_request();
1189 static void do_system_powerdown(Monitor *mon)
1191 qemu_system_powerdown_request();
1194 #if defined(TARGET_I386)
1195 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1197 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1200 pte & PG_GLOBAL_MASK ? 'G' : '-',
1201 pte & PG_PSE_MASK ? 'P' : '-',
1202 pte & PG_DIRTY_MASK ? 'D' : '-',
1203 pte & PG_ACCESSED_MASK ? 'A' : '-',
1204 pte & PG_PCD_MASK ? 'C' : '-',
1205 pte & PG_PWT_MASK ? 'T' : '-',
1206 pte & PG_USER_MASK ? 'U' : '-',
1207 pte & PG_RW_MASK ? 'W' : '-');
1210 static void tlb_info(Monitor *mon)
1214 uint32_t pgd, pde, pte;
1216 env = mon_get_cpu();
1220 if (!(env->cr[0] & CR0_PG_MASK)) {
1221 monitor_printf(mon, "PG disabled\n");
1224 pgd = env->cr[3] & ~0xfff;
1225 for(l1 = 0; l1 < 1024; l1++) {
1226 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1227 pde = le32_to_cpu(pde);
1228 if (pde & PG_PRESENT_MASK) {
1229 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1230 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1232 for(l2 = 0; l2 < 1024; l2++) {
1233 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1234 (uint8_t *)&pte, 4);
1235 pte = le32_to_cpu(pte);
1236 if (pte & PG_PRESENT_MASK) {
1237 print_pte(mon, (l1 << 22) + (l2 << 12),
1247 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1248 uint32_t end, int prot)
1251 prot1 = *plast_prot;
1252 if (prot != prot1) {
1253 if (*pstart != -1) {
1254 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1255 *pstart, end, end - *pstart,
1256 prot1 & PG_USER_MASK ? 'u' : '-',
1258 prot1 & PG_RW_MASK ? 'w' : '-');
1268 static void mem_info(Monitor *mon)
1271 int l1, l2, prot, last_prot;
1272 uint32_t pgd, pde, pte, start, end;
1274 env = mon_get_cpu();
1278 if (!(env->cr[0] & CR0_PG_MASK)) {
1279 monitor_printf(mon, "PG disabled\n");
1282 pgd = env->cr[3] & ~0xfff;
1285 for(l1 = 0; l1 < 1024; l1++) {
1286 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1287 pde = le32_to_cpu(pde);
1289 if (pde & PG_PRESENT_MASK) {
1290 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1291 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1292 mem_print(mon, &start, &last_prot, end, prot);
1294 for(l2 = 0; l2 < 1024; l2++) {
1295 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1296 (uint8_t *)&pte, 4);
1297 pte = le32_to_cpu(pte);
1298 end = (l1 << 22) + (l2 << 12);
1299 if (pte & PG_PRESENT_MASK) {
1300 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1304 mem_print(mon, &start, &last_prot, end, prot);
1309 mem_print(mon, &start, &last_prot, end, prot);
1315 #if defined(TARGET_SH4)
1317 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1319 monitor_printf(mon, " tlb%i:\t"
1320 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1321 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1322 "dirty=%hhu writethrough=%hhu\n",
1324 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1325 tlb->v, tlb->sh, tlb->c, tlb->pr,
1329 static void tlb_info(Monitor *mon)
1331 CPUState *env = mon_get_cpu();
1334 monitor_printf (mon, "ITLB:\n");
1335 for (i = 0 ; i < ITLB_SIZE ; i++)
1336 print_tlb (mon, i, &env->itlb[i]);
1337 monitor_printf (mon, "UTLB:\n");
1338 for (i = 0 ; i < UTLB_SIZE ; i++)
1339 print_tlb (mon, i, &env->utlb[i]);
1344 static void do_info_kqemu(Monitor *mon)
1350 env = mon_get_cpu();
1352 monitor_printf(mon, "No cpu initialized yet");
1355 val = env->kqemu_enabled;
1356 monitor_printf(mon, "kqemu support: ");
1360 monitor_printf(mon, "disabled\n");
1363 monitor_printf(mon, "enabled for user code\n");
1366 monitor_printf(mon, "enabled for user and kernel code\n");
1370 monitor_printf(mon, "kqemu support: not compiled\n");
1374 static void do_info_kvm(Monitor *mon)
1377 monitor_printf(mon, "kvm support: ");
1379 monitor_printf(mon, "enabled\n");
1381 monitor_printf(mon, "disabled\n");
1383 monitor_printf(mon, "kvm support: not compiled\n");
1387 #ifdef CONFIG_PROFILER
1391 int64_t kqemu_exec_count;
1393 int64_t kqemu_ret_int_count;
1394 int64_t kqemu_ret_excp_count;
1395 int64_t kqemu_ret_intr_count;
1397 static void do_info_profile(Monitor *mon)
1403 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1404 dev_time, dev_time / (double)ticks_per_sec);
1405 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1406 qemu_time, qemu_time / (double)ticks_per_sec);
1407 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1408 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1410 kqemu_time, kqemu_time / (double)ticks_per_sec,
1411 kqemu_time / (double)total * 100.0,
1413 kqemu_ret_int_count,
1414 kqemu_ret_excp_count,
1415 kqemu_ret_intr_count);
1418 kqemu_exec_count = 0;
1420 kqemu_ret_int_count = 0;
1421 kqemu_ret_excp_count = 0;
1422 kqemu_ret_intr_count = 0;
1424 kqemu_record_dump();
1428 static void do_info_profile(Monitor *mon)
1430 monitor_printf(mon, "Internal profiler not compiled\n");
1434 /* Capture support */
1435 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1437 static void do_info_capture(Monitor *mon)
1442 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1443 monitor_printf(mon, "[%d]: ", i);
1444 s->ops.info (s->opaque);
1448 static void do_stop_capture(Monitor *mon, int n)
1453 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1455 s->ops.destroy (s->opaque);
1456 LIST_REMOVE (s, entries);
1464 static void do_wav_capture(Monitor *mon, const char *path,
1465 int has_freq, int freq,
1466 int has_bits, int bits,
1467 int has_channels, int nchannels)
1471 s = qemu_mallocz (sizeof (*s));
1473 freq = has_freq ? freq : 44100;
1474 bits = has_bits ? bits : 16;
1475 nchannels = has_channels ? nchannels : 2;
1477 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1478 monitor_printf(mon, "Faied to add wave capture\n");
1481 LIST_INSERT_HEAD (&capture_head, s, entries);
1485 #if defined(TARGET_I386)
1486 static void do_inject_nmi(Monitor *mon, int cpu_index)
1490 for (env = first_cpu; env != NULL; env = env->next_cpu)
1491 if (env->cpu_index == cpu_index) {
1492 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1498 static void do_info_status(Monitor *mon)
1501 monitor_printf(mon, "VM status: running\n");
1503 monitor_printf(mon, "VM status: paused\n");
1507 static void do_balloon(Monitor *mon, int value)
1509 ram_addr_t target = value;
1510 qemu_balloon(target << 20);
1513 static void do_info_balloon(Monitor *mon)
1517 actual = qemu_balloon_status();
1518 if (kvm_enabled() && !kvm_has_sync_mmu())
1519 monitor_printf(mon, "Using KVM without synchronous MMU, "
1520 "ballooning disabled\n");
1521 else if (actual == 0)
1522 monitor_printf(mon, "Ballooning not activated in VM\n");
1524 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1527 /* Please update qemu-doc.texi when adding or changing commands */
1528 static const mon_cmd_t mon_cmds[] = {
1529 { "help|?", "s?", help_cmd,
1530 "[cmd]", "show the help" },
1531 { "commit", "s", do_commit,
1532 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1533 { "info", "s?", do_info,
1534 "subcommand", "show various information about the system state" },
1535 { "q|quit", "", do_quit,
1536 "", "quit the emulator" },
1537 { "eject", "-fB", do_eject,
1538 "[-f] device", "eject a removable medium (use -f to force it)" },
1539 { "change", "BFs?", do_change,
1540 "device filename [format]", "change a removable medium, optional format" },
1541 { "screendump", "F", do_screen_dump,
1542 "filename", "save screen into PPM image 'filename'" },
1543 { "logfile", "F", do_logfile,
1544 "filename", "output logs to 'filename'" },
1545 { "log", "s", do_log,
1546 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1547 { "savevm", "s?", do_savevm,
1548 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1549 { "loadvm", "s", do_loadvm,
1550 "tag|id", "restore a VM snapshot from its tag or id" },
1551 { "delvm", "s", do_delvm,
1552 "tag|id", "delete a VM snapshot from its tag or id" },
1553 { "stop", "", do_stop,
1554 "", "stop emulation", },
1555 { "c|cont", "", do_cont,
1556 "", "resume emulation", },
1557 #ifdef CONFIG_GDBSTUB
1558 { "gdbserver", "s?", do_gdbserver,
1559 "[port]", "start gdbserver session (default port=1234)", },
1561 { "x", "/l", do_memory_dump,
1562 "/fmt addr", "virtual memory dump starting at 'addr'", },
1563 { "xp", "/l", do_physical_memory_dump,
1564 "/fmt addr", "physical memory dump starting at 'addr'", },
1565 { "p|print", "/l", do_print,
1566 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1567 { "i", "/ii.", do_ioport_read,
1568 "/fmt addr", "I/O port read" },
1570 { "sendkey", "si?", do_sendkey,
1571 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1572 { "system_reset", "", do_system_reset,
1573 "", "reset the system" },
1574 { "system_powerdown", "", do_system_powerdown,
1575 "", "send system power down event" },
1576 { "sum", "ii", do_sum,
1577 "addr size", "compute the checksum of a memory region" },
1578 { "usb_add", "s", do_usb_add,
1579 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1580 { "usb_del", "s", do_usb_del,
1581 "device", "remove USB device 'bus.addr'" },
1582 { "cpu", "i", do_cpu_set,
1583 "index", "set the default CPU" },
1584 { "mouse_move", "sss?", do_mouse_move,
1585 "dx dy [dz]", "send mouse move events" },
1586 { "mouse_button", "i", do_mouse_button,
1587 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1588 { "mouse_set", "i", do_mouse_set,
1589 "index", "set which mouse device receives events" },
1591 { "wavcapture", "si?i?i?", do_wav_capture,
1592 "path [frequency bits channels]",
1593 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1595 { "stopcapture", "i", do_stop_capture,
1596 "capture index", "stop capture" },
1597 { "memsave", "lis", do_memory_save,
1598 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1599 { "pmemsave", "lis", do_physical_memory_save,
1600 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1601 { "boot_set", "s", do_boot_set,
1602 "bootdevice", "define new values for the boot device list" },
1603 #if defined(TARGET_I386)
1604 { "nmi", "i", do_inject_nmi,
1605 "cpu", "inject an NMI on the given CPU", },
1607 { "migrate", "-ds", do_migrate,
1608 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1609 { "migrate_cancel", "", do_migrate_cancel,
1610 "", "cancel the current VM migration" },
1611 { "migrate_set_speed", "s", do_migrate_set_speed,
1612 "value", "set maximum speed (in bytes) for migrations" },
1613 #if defined(TARGET_I386)
1614 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1615 "[file=file][,if=type][,bus=n]\n"
1616 "[,unit=m][,media=d][index=i]\n"
1617 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1618 "[snapshot=on|off][,cache=on|off]",
1619 "add drive to PCI storage controller" },
1620 { "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" },
1621 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1622 { "host_net_add", "ss", net_host_device_add,
1623 "[tap,user,socket,vde] options", "add host VLAN client" },
1624 { "host_net_remove", "is", net_host_device_remove,
1625 "vlan_id name", "remove host VLAN client" },
1627 { "balloon", "i", do_balloon,
1628 "target", "request VM to change it's memory allocation (in MB)" },
1629 { "set_link", "ss", do_set_link,
1630 "name [up|down]", "change the link status of a network adapter" },
1634 /* Please update qemu-doc.texi when adding or changing commands */
1635 static const mon_cmd_t info_cmds[] = {
1636 { "version", "", do_info_version,
1637 "", "show the version of QEMU" },
1638 { "network", "", do_info_network,
1639 "", "show the network state" },
1640 { "chardev", "", qemu_chr_info,
1641 "", "show the character devices" },
1642 { "block", "", bdrv_info,
1643 "", "show the block devices" },
1644 { "blockstats", "", bdrv_info_stats,
1645 "", "show block device statistics" },
1646 { "registers", "", do_info_registers,
1647 "", "show the cpu registers" },
1648 { "cpus", "", do_info_cpus,
1649 "", "show infos for each CPU" },
1650 { "history", "", do_info_history,
1651 "", "show the command line history", },
1652 { "irq", "", irq_info,
1653 "", "show the interrupts statistics (if available)", },
1654 { "pic", "", pic_info,
1655 "", "show i8259 (PIC) state", },
1656 { "pci", "", pci_info,
1657 "", "show PCI info", },
1658 #if defined(TARGET_I386) || defined(TARGET_SH4)
1659 { "tlb", "", tlb_info,
1660 "", "show virtual to physical memory mappings", },
1662 #if defined(TARGET_I386)
1663 { "mem", "", mem_info,
1664 "", "show the active virtual memory mappings", },
1665 { "hpet", "", do_info_hpet,
1666 "", "show state of HPET", },
1668 { "jit", "", do_info_jit,
1669 "", "show dynamic compiler info", },
1670 { "kqemu", "", do_info_kqemu,
1671 "", "show KQEMU information", },
1672 { "kvm", "", do_info_kvm,
1673 "", "show KVM information", },
1674 { "usb", "", usb_info,
1675 "", "show guest USB devices", },
1676 { "usbhost", "", usb_host_info,
1677 "", "show host USB devices", },
1678 { "profile", "", do_info_profile,
1679 "", "show profiling information", },
1680 { "capture", "", do_info_capture,
1681 "", "show capture information" },
1682 { "snapshots", "", do_info_snapshots,
1683 "", "show the currently saved VM snapshots" },
1684 { "status", "", do_info_status,
1685 "", "show the current VM status (running|paused)" },
1686 { "pcmcia", "", pcmcia_info,
1687 "", "show guest PCMCIA status" },
1688 { "mice", "", do_info_mice,
1689 "", "show which guest mouse is receiving events" },
1690 { "vnc", "", do_info_vnc,
1691 "", "show the vnc server status"},
1692 { "name", "", do_info_name,
1693 "", "show the current VM name" },
1694 { "uuid", "", do_info_uuid,
1695 "", "show the current VM UUID" },
1696 #if defined(TARGET_PPC)
1697 { "cpustats", "", do_info_cpu_stats,
1698 "", "show CPU statistics", },
1700 #if defined(CONFIG_SLIRP)
1701 { "slirp", "", do_info_slirp,
1702 "", "show SLIRP statistics", },
1704 { "migrate", "", do_info_migrate, "", "show migration status" },
1705 { "balloon", "", do_info_balloon,
1706 "", "show balloon information" },
1710 /*******************************************************************/
1712 static const char *pch;
1713 static jmp_buf expr_env;
1718 typedef struct MonitorDef {
1721 target_long (*get_value)(const struct MonitorDef *md, int val);
1725 #if defined(TARGET_I386)
1726 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1728 CPUState *env = mon_get_cpu();
1731 return env->eip + env->segs[R_CS].base;
1735 #if defined(TARGET_PPC)
1736 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1738 CPUState *env = mon_get_cpu();
1746 for (i = 0; i < 8; i++)
1747 u |= env->crf[i] << (32 - (4 * i));
1752 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1754 CPUState *env = mon_get_cpu();
1760 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1762 CPUState *env = mon_get_cpu();
1768 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1770 CPUState *env = mon_get_cpu();
1773 return cpu_ppc_load_decr(env);
1776 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1778 CPUState *env = mon_get_cpu();
1781 return cpu_ppc_load_tbu(env);
1784 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1786 CPUState *env = mon_get_cpu();
1789 return cpu_ppc_load_tbl(env);
1793 #if defined(TARGET_SPARC)
1794 #ifndef TARGET_SPARC64
1795 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1797 CPUState *env = mon_get_cpu();
1800 return GET_PSR(env);
1804 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1806 CPUState *env = mon_get_cpu();
1809 return env->regwptr[val];
1813 static const MonitorDef monitor_defs[] = {
1816 #define SEG(name, seg) \
1817 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1818 { name ".base", offsetof(CPUState, segs[seg].base) },\
1819 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1821 { "eax", offsetof(CPUState, regs[0]) },
1822 { "ecx", offsetof(CPUState, regs[1]) },
1823 { "edx", offsetof(CPUState, regs[2]) },
1824 { "ebx", offsetof(CPUState, regs[3]) },
1825 { "esp|sp", offsetof(CPUState, regs[4]) },
1826 { "ebp|fp", offsetof(CPUState, regs[5]) },
1827 { "esi", offsetof(CPUState, regs[6]) },
1828 { "edi", offsetof(CPUState, regs[7]) },
1829 #ifdef TARGET_X86_64
1830 { "r8", offsetof(CPUState, regs[8]) },
1831 { "r9", offsetof(CPUState, regs[9]) },
1832 { "r10", offsetof(CPUState, regs[10]) },
1833 { "r11", offsetof(CPUState, regs[11]) },
1834 { "r12", offsetof(CPUState, regs[12]) },
1835 { "r13", offsetof(CPUState, regs[13]) },
1836 { "r14", offsetof(CPUState, regs[14]) },
1837 { "r15", offsetof(CPUState, regs[15]) },
1839 { "eflags", offsetof(CPUState, eflags) },
1840 { "eip", offsetof(CPUState, eip) },
1847 { "pc", 0, monitor_get_pc, },
1848 #elif defined(TARGET_PPC)
1849 /* General purpose registers */
1850 { "r0", offsetof(CPUState, gpr[0]) },
1851 { "r1", offsetof(CPUState, gpr[1]) },
1852 { "r2", offsetof(CPUState, gpr[2]) },
1853 { "r3", offsetof(CPUState, gpr[3]) },
1854 { "r4", offsetof(CPUState, gpr[4]) },
1855 { "r5", offsetof(CPUState, gpr[5]) },
1856 { "r6", offsetof(CPUState, gpr[6]) },
1857 { "r7", offsetof(CPUState, gpr[7]) },
1858 { "r8", offsetof(CPUState, gpr[8]) },
1859 { "r9", offsetof(CPUState, gpr[9]) },
1860 { "r10", offsetof(CPUState, gpr[10]) },
1861 { "r11", offsetof(CPUState, gpr[11]) },
1862 { "r12", offsetof(CPUState, gpr[12]) },
1863 { "r13", offsetof(CPUState, gpr[13]) },
1864 { "r14", offsetof(CPUState, gpr[14]) },
1865 { "r15", offsetof(CPUState, gpr[15]) },
1866 { "r16", offsetof(CPUState, gpr[16]) },
1867 { "r17", offsetof(CPUState, gpr[17]) },
1868 { "r18", offsetof(CPUState, gpr[18]) },
1869 { "r19", offsetof(CPUState, gpr[19]) },
1870 { "r20", offsetof(CPUState, gpr[20]) },
1871 { "r21", offsetof(CPUState, gpr[21]) },
1872 { "r22", offsetof(CPUState, gpr[22]) },
1873 { "r23", offsetof(CPUState, gpr[23]) },
1874 { "r24", offsetof(CPUState, gpr[24]) },
1875 { "r25", offsetof(CPUState, gpr[25]) },
1876 { "r26", offsetof(CPUState, gpr[26]) },
1877 { "r27", offsetof(CPUState, gpr[27]) },
1878 { "r28", offsetof(CPUState, gpr[28]) },
1879 { "r29", offsetof(CPUState, gpr[29]) },
1880 { "r30", offsetof(CPUState, gpr[30]) },
1881 { "r31", offsetof(CPUState, gpr[31]) },
1882 /* Floating point registers */
1883 { "f0", offsetof(CPUState, fpr[0]) },
1884 { "f1", offsetof(CPUState, fpr[1]) },
1885 { "f2", offsetof(CPUState, fpr[2]) },
1886 { "f3", offsetof(CPUState, fpr[3]) },
1887 { "f4", offsetof(CPUState, fpr[4]) },
1888 { "f5", offsetof(CPUState, fpr[5]) },
1889 { "f6", offsetof(CPUState, fpr[6]) },
1890 { "f7", offsetof(CPUState, fpr[7]) },
1891 { "f8", offsetof(CPUState, fpr[8]) },
1892 { "f9", offsetof(CPUState, fpr[9]) },
1893 { "f10", offsetof(CPUState, fpr[10]) },
1894 { "f11", offsetof(CPUState, fpr[11]) },
1895 { "f12", offsetof(CPUState, fpr[12]) },
1896 { "f13", offsetof(CPUState, fpr[13]) },
1897 { "f14", offsetof(CPUState, fpr[14]) },
1898 { "f15", offsetof(CPUState, fpr[15]) },
1899 { "f16", offsetof(CPUState, fpr[16]) },
1900 { "f17", offsetof(CPUState, fpr[17]) },
1901 { "f18", offsetof(CPUState, fpr[18]) },
1902 { "f19", offsetof(CPUState, fpr[19]) },
1903 { "f20", offsetof(CPUState, fpr[20]) },
1904 { "f21", offsetof(CPUState, fpr[21]) },
1905 { "f22", offsetof(CPUState, fpr[22]) },
1906 { "f23", offsetof(CPUState, fpr[23]) },
1907 { "f24", offsetof(CPUState, fpr[24]) },
1908 { "f25", offsetof(CPUState, fpr[25]) },
1909 { "f26", offsetof(CPUState, fpr[26]) },
1910 { "f27", offsetof(CPUState, fpr[27]) },
1911 { "f28", offsetof(CPUState, fpr[28]) },
1912 { "f29", offsetof(CPUState, fpr[29]) },
1913 { "f30", offsetof(CPUState, fpr[30]) },
1914 { "f31", offsetof(CPUState, fpr[31]) },
1915 { "fpscr", offsetof(CPUState, fpscr) },
1916 /* Next instruction pointer */
1917 { "nip|pc", offsetof(CPUState, nip) },
1918 { "lr", offsetof(CPUState, lr) },
1919 { "ctr", offsetof(CPUState, ctr) },
1920 { "decr", 0, &monitor_get_decr, },
1921 { "ccr", 0, &monitor_get_ccr, },
1922 /* Machine state register */
1923 { "msr", 0, &monitor_get_msr, },
1924 { "xer", 0, &monitor_get_xer, },
1925 { "tbu", 0, &monitor_get_tbu, },
1926 { "tbl", 0, &monitor_get_tbl, },
1927 #if defined(TARGET_PPC64)
1928 /* Address space register */
1929 { "asr", offsetof(CPUState, asr) },
1931 /* Segment registers */
1932 { "sdr1", offsetof(CPUState, sdr1) },
1933 { "sr0", offsetof(CPUState, sr[0]) },
1934 { "sr1", offsetof(CPUState, sr[1]) },
1935 { "sr2", offsetof(CPUState, sr[2]) },
1936 { "sr3", offsetof(CPUState, sr[3]) },
1937 { "sr4", offsetof(CPUState, sr[4]) },
1938 { "sr5", offsetof(CPUState, sr[5]) },
1939 { "sr6", offsetof(CPUState, sr[6]) },
1940 { "sr7", offsetof(CPUState, sr[7]) },
1941 { "sr8", offsetof(CPUState, sr[8]) },
1942 { "sr9", offsetof(CPUState, sr[9]) },
1943 { "sr10", offsetof(CPUState, sr[10]) },
1944 { "sr11", offsetof(CPUState, sr[11]) },
1945 { "sr12", offsetof(CPUState, sr[12]) },
1946 { "sr13", offsetof(CPUState, sr[13]) },
1947 { "sr14", offsetof(CPUState, sr[14]) },
1948 { "sr15", offsetof(CPUState, sr[15]) },
1949 /* Too lazy to put BATs and SPRs ... */
1950 #elif defined(TARGET_SPARC)
1951 { "g0", offsetof(CPUState, gregs[0]) },
1952 { "g1", offsetof(CPUState, gregs[1]) },
1953 { "g2", offsetof(CPUState, gregs[2]) },
1954 { "g3", offsetof(CPUState, gregs[3]) },
1955 { "g4", offsetof(CPUState, gregs[4]) },
1956 { "g5", offsetof(CPUState, gregs[5]) },
1957 { "g6", offsetof(CPUState, gregs[6]) },
1958 { "g7", offsetof(CPUState, gregs[7]) },
1959 { "o0", 0, monitor_get_reg },
1960 { "o1", 1, monitor_get_reg },
1961 { "o2", 2, monitor_get_reg },
1962 { "o3", 3, monitor_get_reg },
1963 { "o4", 4, monitor_get_reg },
1964 { "o5", 5, monitor_get_reg },
1965 { "o6", 6, monitor_get_reg },
1966 { "o7", 7, monitor_get_reg },
1967 { "l0", 8, monitor_get_reg },
1968 { "l1", 9, monitor_get_reg },
1969 { "l2", 10, monitor_get_reg },
1970 { "l3", 11, monitor_get_reg },
1971 { "l4", 12, monitor_get_reg },
1972 { "l5", 13, monitor_get_reg },
1973 { "l6", 14, monitor_get_reg },
1974 { "l7", 15, monitor_get_reg },
1975 { "i0", 16, monitor_get_reg },
1976 { "i1", 17, monitor_get_reg },
1977 { "i2", 18, monitor_get_reg },
1978 { "i3", 19, monitor_get_reg },
1979 { "i4", 20, monitor_get_reg },
1980 { "i5", 21, monitor_get_reg },
1981 { "i6", 22, monitor_get_reg },
1982 { "i7", 23, monitor_get_reg },
1983 { "pc", offsetof(CPUState, pc) },
1984 { "npc", offsetof(CPUState, npc) },
1985 { "y", offsetof(CPUState, y) },
1986 #ifndef TARGET_SPARC64
1987 { "psr", 0, &monitor_get_psr, },
1988 { "wim", offsetof(CPUState, wim) },
1990 { "tbr", offsetof(CPUState, tbr) },
1991 { "fsr", offsetof(CPUState, fsr) },
1992 { "f0", offsetof(CPUState, fpr[0]) },
1993 { "f1", offsetof(CPUState, fpr[1]) },
1994 { "f2", offsetof(CPUState, fpr[2]) },
1995 { "f3", offsetof(CPUState, fpr[3]) },
1996 { "f4", offsetof(CPUState, fpr[4]) },
1997 { "f5", offsetof(CPUState, fpr[5]) },
1998 { "f6", offsetof(CPUState, fpr[6]) },
1999 { "f7", offsetof(CPUState, fpr[7]) },
2000 { "f8", offsetof(CPUState, fpr[8]) },
2001 { "f9", offsetof(CPUState, fpr[9]) },
2002 { "f10", offsetof(CPUState, fpr[10]) },
2003 { "f11", offsetof(CPUState, fpr[11]) },
2004 { "f12", offsetof(CPUState, fpr[12]) },
2005 { "f13", offsetof(CPUState, fpr[13]) },
2006 { "f14", offsetof(CPUState, fpr[14]) },
2007 { "f15", offsetof(CPUState, fpr[15]) },
2008 { "f16", offsetof(CPUState, fpr[16]) },
2009 { "f17", offsetof(CPUState, fpr[17]) },
2010 { "f18", offsetof(CPUState, fpr[18]) },
2011 { "f19", offsetof(CPUState, fpr[19]) },
2012 { "f20", offsetof(CPUState, fpr[20]) },
2013 { "f21", offsetof(CPUState, fpr[21]) },
2014 { "f22", offsetof(CPUState, fpr[22]) },
2015 { "f23", offsetof(CPUState, fpr[23]) },
2016 { "f24", offsetof(CPUState, fpr[24]) },
2017 { "f25", offsetof(CPUState, fpr[25]) },
2018 { "f26", offsetof(CPUState, fpr[26]) },
2019 { "f27", offsetof(CPUState, fpr[27]) },
2020 { "f28", offsetof(CPUState, fpr[28]) },
2021 { "f29", offsetof(CPUState, fpr[29]) },
2022 { "f30", offsetof(CPUState, fpr[30]) },
2023 { "f31", offsetof(CPUState, fpr[31]) },
2024 #ifdef TARGET_SPARC64
2025 { "f32", offsetof(CPUState, fpr[32]) },
2026 { "f34", offsetof(CPUState, fpr[34]) },
2027 { "f36", offsetof(CPUState, fpr[36]) },
2028 { "f38", offsetof(CPUState, fpr[38]) },
2029 { "f40", offsetof(CPUState, fpr[40]) },
2030 { "f42", offsetof(CPUState, fpr[42]) },
2031 { "f44", offsetof(CPUState, fpr[44]) },
2032 { "f46", offsetof(CPUState, fpr[46]) },
2033 { "f48", offsetof(CPUState, fpr[48]) },
2034 { "f50", offsetof(CPUState, fpr[50]) },
2035 { "f52", offsetof(CPUState, fpr[52]) },
2036 { "f54", offsetof(CPUState, fpr[54]) },
2037 { "f56", offsetof(CPUState, fpr[56]) },
2038 { "f58", offsetof(CPUState, fpr[58]) },
2039 { "f60", offsetof(CPUState, fpr[60]) },
2040 { "f62", offsetof(CPUState, fpr[62]) },
2041 { "asi", offsetof(CPUState, asi) },
2042 { "pstate", offsetof(CPUState, pstate) },
2043 { "cansave", offsetof(CPUState, cansave) },
2044 { "canrestore", offsetof(CPUState, canrestore) },
2045 { "otherwin", offsetof(CPUState, otherwin) },
2046 { "wstate", offsetof(CPUState, wstate) },
2047 { "cleanwin", offsetof(CPUState, cleanwin) },
2048 { "fprs", offsetof(CPUState, fprs) },
2054 static void expr_error(Monitor *mon, const char *msg)
2056 monitor_printf(mon, "%s\n", msg);
2057 longjmp(expr_env, 1);
2060 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2061 static int get_monitor_def(target_long *pval, const char *name)
2063 const MonitorDef *md;
2066 for(md = monitor_defs; md->name != NULL; md++) {
2067 if (compare_cmd(name, md->name)) {
2068 if (md->get_value) {
2069 *pval = md->get_value(md, md->offset);
2071 CPUState *env = mon_get_cpu();
2074 ptr = (uint8_t *)env + md->offset;
2077 *pval = *(int32_t *)ptr;
2080 *pval = *(target_long *)ptr;
2093 static void next(void)
2097 while (qemu_isspace(*pch))
2102 static int64_t expr_sum(Monitor *mon);
2104 static int64_t expr_unary(Monitor *mon)
2113 n = expr_unary(mon);
2117 n = -expr_unary(mon);
2121 n = ~expr_unary(mon);
2127 expr_error(mon, "')' expected");
2134 expr_error(mon, "character constant expected");
2138 expr_error(mon, "missing terminating \' character");
2148 while ((*pch >= 'a' && *pch <= 'z') ||
2149 (*pch >= 'A' && *pch <= 'Z') ||
2150 (*pch >= '0' && *pch <= '9') ||
2151 *pch == '_' || *pch == '.') {
2152 if ((q - buf) < sizeof(buf) - 1)
2156 while (qemu_isspace(*pch))
2159 ret = get_monitor_def(®, buf);
2161 expr_error(mon, "unknown register");
2163 expr_error(mon, "no cpu defined");
2168 expr_error(mon, "unexpected end of expression");
2172 #if TARGET_PHYS_ADDR_BITS > 32
2173 n = strtoull(pch, &p, 0);
2175 n = strtoul(pch, &p, 0);
2178 expr_error(mon, "invalid char in expression");
2181 while (qemu_isspace(*pch))
2189 static int64_t expr_prod(Monitor *mon)
2194 val = expr_unary(mon);
2197 if (op != '*' && op != '/' && op != '%')
2200 val2 = expr_unary(mon);
2209 expr_error(mon, "division by zero");
2220 static int64_t expr_logic(Monitor *mon)
2225 val = expr_prod(mon);
2228 if (op != '&' && op != '|' && op != '^')
2231 val2 = expr_prod(mon);
2248 static int64_t expr_sum(Monitor *mon)
2253 val = expr_logic(mon);
2256 if (op != '+' && op != '-')
2259 val2 = expr_logic(mon);
2268 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2271 if (setjmp(expr_env)) {
2275 while (qemu_isspace(*pch))
2277 *pval = expr_sum(mon);
2282 static int get_str(char *buf, int buf_size, const char **pp)
2290 while (qemu_isspace(*p))
2300 while (*p != '\0' && *p != '\"') {
2316 qemu_printf("unsupported escape code: '\\%c'\n", c);
2319 if ((q - buf) < buf_size - 1) {
2323 if ((q - buf) < buf_size - 1) {
2330 qemu_printf("unterminated string\n");
2335 while (*p != '\0' && !qemu_isspace(*p)) {
2336 if ((q - buf) < buf_size - 1) {
2347 static int default_fmt_format = 'x';
2348 static int default_fmt_size = 4;
2352 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2354 const char *p, *pstart, *typestr;
2356 int c, nb_args, len, i, has_arg;
2357 const mon_cmd_t *cmd;
2360 void *str_allocated[MAX_ARGS];
2361 void *args[MAX_ARGS];
2362 void (*handler_0)(Monitor *mon);
2363 void (*handler_1)(Monitor *mon, void *arg0);
2364 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2365 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2366 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2368 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2369 void *arg3, void *arg4);
2370 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2371 void *arg3, void *arg4, void *arg5);
2372 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2373 void *arg3, void *arg4, void *arg5, void *arg6);
2376 monitor_printf(mon, "command='%s'\n", cmdline);
2379 /* extract the command name */
2382 while (qemu_isspace(*p))
2387 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2390 if (len > sizeof(cmdname) - 1)
2391 len = sizeof(cmdname) - 1;
2392 memcpy(cmdname, pstart, len);
2393 cmdname[len] = '\0';
2395 /* find the command */
2396 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2397 if (compare_cmd(cmdname, cmd->name))
2400 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2404 for(i = 0; i < MAX_ARGS; i++)
2405 str_allocated[i] = NULL;
2407 /* parse the parameters */
2408 typestr = cmd->args_type;
2423 while (qemu_isspace(*p))
2425 if (*typestr == '?') {
2428 /* no optional string: NULL argument */
2433 ret = get_str(buf, sizeof(buf), &p);
2437 monitor_printf(mon, "%s: filename expected\n",
2441 monitor_printf(mon, "%s: block device name expected\n",
2445 monitor_printf(mon, "%s: string expected\n", cmdname);
2450 str = qemu_malloc(strlen(buf) + 1);
2451 pstrcpy(str, sizeof(buf), buf);
2452 str_allocated[nb_args] = str;
2454 if (nb_args >= MAX_ARGS) {
2456 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2459 args[nb_args++] = str;
2464 int count, format, size;
2466 while (qemu_isspace(*p))
2472 if (qemu_isdigit(*p)) {
2474 while (qemu_isdigit(*p)) {
2475 count = count * 10 + (*p - '0');
2513 if (*p != '\0' && !qemu_isspace(*p)) {
2514 monitor_printf(mon, "invalid char in format: '%c'\n",
2519 format = default_fmt_format;
2520 if (format != 'i') {
2521 /* for 'i', not specifying a size gives -1 as size */
2523 size = default_fmt_size;
2524 default_fmt_size = size;
2526 default_fmt_format = format;
2529 format = default_fmt_format;
2530 if (format != 'i') {
2531 size = default_fmt_size;
2536 if (nb_args + 3 > MAX_ARGS)
2538 args[nb_args++] = (void*)(long)count;
2539 args[nb_args++] = (void*)(long)format;
2540 args[nb_args++] = (void*)(long)size;
2548 while (qemu_isspace(*p))
2550 if (*typestr == '?' || *typestr == '.') {
2551 if (*typestr == '?') {
2559 while (qemu_isspace(*p))
2567 if (nb_args >= MAX_ARGS)
2569 args[nb_args++] = (void *)(long)has_arg;
2571 if (nb_args >= MAX_ARGS)
2577 if (get_expr(mon, &val, &p))
2581 if (nb_args >= MAX_ARGS)
2583 args[nb_args++] = (void *)(long)val;
2585 if ((nb_args + 1) >= MAX_ARGS)
2587 #if TARGET_PHYS_ADDR_BITS > 32
2588 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2590 args[nb_args++] = (void *)0;
2592 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2604 while (qemu_isspace(*p))
2610 monitor_printf(mon, "%s: unsupported option -%c\n",
2617 if (nb_args >= MAX_ARGS)
2619 args[nb_args++] = (void *)(long)has_option;
2624 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2628 /* check that all arguments were parsed */
2629 while (qemu_isspace(*p))
2632 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2639 handler_0 = cmd->handler;
2643 handler_1 = cmd->handler;
2644 handler_1(mon, args[0]);
2647 handler_2 = cmd->handler;
2648 handler_2(mon, args[0], args[1]);
2651 handler_3 = cmd->handler;
2652 handler_3(mon, args[0], args[1], args[2]);
2655 handler_4 = cmd->handler;
2656 handler_4(mon, args[0], args[1], args[2], args[3]);
2659 handler_5 = cmd->handler;
2660 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2663 handler_6 = cmd->handler;
2664 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2667 handler_7 = cmd->handler;
2668 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2672 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2676 for(i = 0; i < MAX_ARGS; i++)
2677 qemu_free(str_allocated[i]);
2681 static void cmd_completion(const char *name, const char *list)
2683 const char *p, *pstart;
2692 p = pstart + strlen(pstart);
2694 if (len > sizeof(cmd) - 2)
2695 len = sizeof(cmd) - 2;
2696 memcpy(cmd, pstart, len);
2698 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2699 readline_add_completion(cur_mon->rs, cmd);
2707 static void file_completion(const char *input)
2712 char file[1024], file_prefix[1024];
2716 p = strrchr(input, '/');
2719 pstrcpy(file_prefix, sizeof(file_prefix), input);
2720 pstrcpy(path, sizeof(path), ".");
2722 input_path_len = p - input + 1;
2723 memcpy(path, input, input_path_len);
2724 if (input_path_len > sizeof(path) - 1)
2725 input_path_len = sizeof(path) - 1;
2726 path[input_path_len] = '\0';
2727 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2729 #ifdef DEBUG_COMPLETION
2730 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2731 input, path, file_prefix);
2733 ffs = opendir(path);
2741 if (strstart(d->d_name, file_prefix, NULL)) {
2742 memcpy(file, input, input_path_len);
2743 if (input_path_len < sizeof(file))
2744 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2746 /* stat the file to find out if it's a directory.
2747 * In that case add a slash to speed up typing long paths
2750 if(S_ISDIR(sb.st_mode))
2751 pstrcat(file, sizeof(file), "/");
2752 readline_add_completion(cur_mon->rs, file);
2758 static void block_completion_it(void *opaque, BlockDriverState *bs)
2760 const char *name = bdrv_get_device_name(bs);
2761 const char *input = opaque;
2763 if (input[0] == '\0' ||
2764 !strncmp(name, (char *)input, strlen(input))) {
2765 readline_add_completion(cur_mon->rs, name);
2769 /* NOTE: this parser is an approximate form of the real command parser */
2770 static void parse_cmdline(const char *cmdline,
2771 int *pnb_args, char **args)
2780 while (qemu_isspace(*p))
2784 if (nb_args >= MAX_ARGS)
2786 ret = get_str(buf, sizeof(buf), &p);
2787 args[nb_args] = qemu_strdup(buf);
2792 *pnb_args = nb_args;
2795 static void monitor_find_completion(const char *cmdline)
2797 const char *cmdname;
2798 char *args[MAX_ARGS];
2799 int nb_args, i, len;
2800 const char *ptype, *str;
2801 const mon_cmd_t *cmd;
2804 parse_cmdline(cmdline, &nb_args, args);
2805 #ifdef DEBUG_COMPLETION
2806 for(i = 0; i < nb_args; i++) {
2807 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2811 /* if the line ends with a space, it means we want to complete the
2813 len = strlen(cmdline);
2814 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2815 if (nb_args >= MAX_ARGS)
2817 args[nb_args++] = qemu_strdup("");
2820 /* command completion */
2825 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2826 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2827 cmd_completion(cmdname, cmd->name);
2830 /* find the command */
2831 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2832 if (compare_cmd(args[0], cmd->name))
2837 ptype = cmd->args_type;
2838 for(i = 0; i < nb_args - 2; i++) {
2839 if (*ptype != '\0') {
2841 while (*ptype == '?')
2845 str = args[nb_args - 1];
2848 /* file completion */
2849 readline_set_completion_index(cur_mon->rs, strlen(str));
2850 file_completion(str);
2853 /* block device name completion */
2854 readline_set_completion_index(cur_mon->rs, strlen(str));
2855 bdrv_iterate(block_completion_it, (void *)str);
2858 /* XXX: more generic ? */
2859 if (!strcmp(cmd->name, "info")) {
2860 readline_set_completion_index(cur_mon->rs, strlen(str));
2861 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2862 cmd_completion(str, cmd->name);
2864 } else if (!strcmp(cmd->name, "sendkey")) {
2865 readline_set_completion_index(cur_mon->rs, strlen(str));
2866 for(key = key_defs; key->name != NULL; key++) {
2867 cmd_completion(str, key->name);
2875 for(i = 0; i < nb_args; i++)
2879 static int monitor_can_read(void *opaque)
2881 Monitor *mon = opaque;
2883 return (mon->suspend_cnt == 0) ? 128 : 0;
2886 static void monitor_read(void *opaque, const uint8_t *buf, int size)
2888 Monitor *old_mon = cur_mon;
2893 for (i = 0; i < size; i++)
2894 readline_handle_byte(cur_mon->rs, buf[i]);
2899 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
2901 monitor_suspend(mon);
2902 monitor_handle_command(mon, cmdline);
2903 monitor_resume(mon);
2906 void monitor_suspend(Monitor *mon)
2911 void monitor_resume(Monitor *mon)
2913 if (--mon->suspend_cnt == 0)
2914 readline_show_prompt(mon->rs);
2917 static void monitor_event(void *opaque, int event)
2919 Monitor *mon = opaque;
2921 if (event != CHR_EVENT_RESET)
2924 monitor_printf(mon, "QEMU %s monitor - type 'help' for more information\n",
2926 readline_show_prompt(mon->rs);
2929 void monitor_init(CharDriverState *chr, int flags)
2931 static int is_first_init = 1;
2934 if (is_first_init) {
2935 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
2939 mon = qemu_mallocz(sizeof(*mon));
2943 mon->rs = readline_init(mon, monitor_find_completion);
2944 monitor_read_command(mon, 0);
2946 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
2949 LIST_INSERT_HEAD(&mon_list, mon, entry);
2950 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
2954 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
2956 BlockDriverState *bs = opaque;
2959 if (bdrv_set_key(bs, password) != 0) {
2960 monitor_printf(mon, "invalid password\n");
2963 if (mon->password_completion_cb)
2964 mon->password_completion_cb(mon->password_opaque, ret);
2966 monitor_read_command(mon, 1);
2969 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
2970 BlockDriverCompletionFunc *completion_cb,
2973 if (!bdrv_key_required(bs)) {
2975 completion_cb(opaque, 0);
2979 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
2980 bdrv_get_encrypted_filename(bs));
2982 mon->password_completion_cb = completion_cb;
2983 mon->password_opaque = opaque;
2985 monitor_read_password(mon, bdrv_password_cb, bs);