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
27 #include "hw/pcmcia.h"
32 #include "qemu-char.h"
38 #include "audio/audio.h"
41 #include "qemu-timer.h"
42 #include "migration.h"
47 //#define DEBUG_COMPLETION
53 * 'B' block device name
54 * 's' string (accept optional quote)
56 * 'l' target long (32 or 64 bit)
57 * '/' optional gdb-like print format (like "/10x")
59 * '?' optional type (for 'F', 's' and 'i')
63 typedef struct mon_cmd_t {
65 const char *args_type;
79 BlockDriverCompletionFunc *password_completion_cb;
80 void *password_opaque;
81 LIST_ENTRY(Monitor) entry;
84 static LIST_HEAD(mon_list, Monitor) mon_list;
86 static const mon_cmd_t mon_cmds[];
87 static const mon_cmd_t info_cmds[];
89 Monitor *cur_mon = NULL;
91 static void monitor_command_cb(Monitor *mon, const char *cmdline,
94 static void monitor_read_command(Monitor *mon, int show_prompt)
96 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
98 readline_show_prompt(mon->rs);
101 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
105 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
106 /* prompt is printed on return from the command handler */
109 monitor_printf(mon, "terminal does not support password prompting\n");
114 void monitor_flush(Monitor *mon)
116 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
117 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
118 mon->outbuf_index = 0;
122 /* flush at every end of line or if the buffer is full */
123 static void monitor_puts(Monitor *mon, const char *str)
135 mon->outbuf[mon->outbuf_index++] = '\r';
136 mon->outbuf[mon->outbuf_index++] = c;
137 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
143 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
146 vsnprintf(buf, sizeof(buf), fmt, ap);
147 monitor_puts(mon, buf);
150 void monitor_printf(Monitor *mon, const char *fmt, ...)
154 monitor_vprintf(mon, fmt, ap);
158 void monitor_print_filename(Monitor *mon, const char *filename)
162 for (i = 0; filename[i]; i++) {
163 switch (filename[i]) {
167 monitor_printf(mon, "\\%c", filename[i]);
170 monitor_printf(mon, "\\t");
173 monitor_printf(mon, "\\r");
176 monitor_printf(mon, "\\n");
179 monitor_printf(mon, "%c", filename[i]);
185 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
189 monitor_vprintf((Monitor *)stream, fmt, ap);
194 static int compare_cmd(const char *name, const char *list)
196 const char *p, *pstart;
204 p = pstart + strlen(pstart);
205 if ((p - pstart) == len && !memcmp(pstart, name, len))
214 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
215 const char *prefix, const char *name)
217 const mon_cmd_t *cmd;
219 for(cmd = cmds; cmd->name != NULL; cmd++) {
220 if (!name || !strcmp(name, cmd->name))
221 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
222 cmd->params, cmd->help);
226 static void help_cmd(Monitor *mon, const char *name)
228 if (name && !strcmp(name, "info")) {
229 help_cmd_dump(mon, info_cmds, "info ", NULL);
231 help_cmd_dump(mon, mon_cmds, "", name);
232 if (name && !strcmp(name, "log")) {
233 const CPULogItem *item;
234 monitor_printf(mon, "Log items (comma separated):\n");
235 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
236 for(item = cpu_log_items; item->mask != 0; item++) {
237 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
243 static void do_commit(Monitor *mon, const char *device)
247 all_devices = !strcmp(device, "all");
248 for (i = 0; i < nb_drives; i++) {
250 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
251 bdrv_commit(drives_table[i].bdrv);
255 static void do_info(Monitor *mon, const char *item)
257 const mon_cmd_t *cmd;
258 void (*handler)(Monitor *);
262 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
263 if (compare_cmd(item, cmd->name))
267 help_cmd(mon, "info");
270 handler = cmd->handler;
274 static void do_info_version(Monitor *mon)
276 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
279 static void do_info_name(Monitor *mon)
282 monitor_printf(mon, "%s\n", qemu_name);
285 #if defined(TARGET_I386)
286 static void do_info_hpet(Monitor *mon)
288 monitor_printf(mon, "HPET is %s by QEMU\n",
289 (no_hpet) ? "disabled" : "enabled");
293 static void do_info_uuid(Monitor *mon)
295 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
296 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
297 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
298 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
299 qemu_uuid[14], qemu_uuid[15]);
302 /* get the current CPU defined by the user */
303 static int mon_set_cpu(int cpu_index)
307 for(env = first_cpu; env != NULL; env = env->next_cpu) {
308 if (env->cpu_index == cpu_index) {
309 cur_mon->mon_cpu = env;
316 static CPUState *mon_get_cpu(void)
318 if (!cur_mon->mon_cpu) {
321 cpu_synchronize_state(cur_mon->mon_cpu, 0);
322 return cur_mon->mon_cpu;
325 static void do_info_registers(Monitor *mon)
332 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
335 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
340 static void do_info_cpus(Monitor *mon)
344 /* just to set the default cpu if not already done */
347 for(env = first_cpu; env != NULL; env = env->next_cpu) {
348 cpu_synchronize_state(env, 0);
349 monitor_printf(mon, "%c CPU #%d:",
350 (env == mon->mon_cpu) ? '*' : ' ',
352 #if defined(TARGET_I386)
353 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
354 env->eip + env->segs[R_CS].base);
355 #elif defined(TARGET_PPC)
356 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
357 #elif defined(TARGET_SPARC)
358 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
360 #elif defined(TARGET_MIPS)
361 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
364 monitor_printf(mon, " (halted)");
365 monitor_printf(mon, "\n");
369 static void do_cpu_set(Monitor *mon, int index)
371 if (mon_set_cpu(index) < 0)
372 monitor_printf(mon, "Invalid CPU index\n");
375 static void do_info_jit(Monitor *mon)
377 dump_exec_info((FILE *)mon, monitor_fprintf);
380 static void do_info_history(Monitor *mon)
389 str = readline_get_history(mon->rs, i);
392 monitor_printf(mon, "%d: '%s'\n", i, str);
397 #if defined(TARGET_PPC)
398 /* XXX: not implemented in other targets */
399 static void do_info_cpu_stats(Monitor *mon)
404 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
408 static void do_quit(Monitor *mon)
413 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
415 if (bdrv_is_inserted(bs)) {
417 if (!bdrv_is_removable(bs)) {
418 monitor_printf(mon, "device is not removable\n");
421 if (bdrv_is_locked(bs)) {
422 monitor_printf(mon, "device is locked\n");
431 static void do_eject(Monitor *mon, int force, const char *filename)
433 BlockDriverState *bs;
435 bs = bdrv_find(filename);
437 monitor_printf(mon, "device not found\n");
440 eject_device(mon, bs, force);
443 static void do_change_block(Monitor *mon, const char *device,
444 const char *filename, const char *fmt)
446 BlockDriverState *bs;
447 BlockDriver *drv = NULL;
449 bs = bdrv_find(device);
451 monitor_printf(mon, "device not found\n");
455 drv = bdrv_find_format(fmt);
457 monitor_printf(mon, "invalid format %s\n", fmt);
461 if (eject_device(mon, bs, 0) < 0)
463 bdrv_open2(bs, filename, 0, drv);
464 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
467 static void change_vnc_password_cb(Monitor *mon, const char *password,
470 if (vnc_display_password(NULL, password) < 0)
471 monitor_printf(mon, "could not set VNC server password\n");
473 monitor_read_command(mon, 1);
476 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
478 if (strcmp(target, "passwd") == 0 ||
479 strcmp(target, "password") == 0) {
482 strncpy(password, arg, sizeof(password));
483 password[sizeof(password) - 1] = '\0';
484 change_vnc_password_cb(mon, password, NULL);
486 monitor_read_password(mon, change_vnc_password_cb, NULL);
489 if (vnc_display_open(NULL, target) < 0)
490 monitor_printf(mon, "could not start VNC server on %s\n", target);
494 static void do_change(Monitor *mon, const char *device, const char *target,
497 if (strcmp(device, "vnc") == 0) {
498 do_change_vnc(mon, target, arg);
500 do_change_block(mon, device, target, arg);
504 static void do_screen_dump(Monitor *mon, const char *filename)
506 vga_hw_screen_dump(filename);
509 static void do_logfile(Monitor *mon, const char *filename)
511 cpu_set_log_filename(filename);
514 static void do_log(Monitor *mon, const char *items)
518 if (!strcmp(items, "none")) {
521 mask = cpu_str_to_log_mask(items);
523 help_cmd(mon, "log");
530 static void do_singlestep(Monitor *mon, const char *option)
532 if (!option || !strcmp(option, "on")) {
534 } else if (!strcmp(option, "off")) {
537 monitor_printf(mon, "unexpected option %s\n", option);
541 static void do_stop(Monitor *mon)
543 vm_stop(EXCP_INTERRUPT);
546 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
548 struct bdrv_iterate_context {
553 static void do_cont(Monitor *mon)
555 struct bdrv_iterate_context context = { mon, 0 };
557 bdrv_iterate(encrypted_bdrv_it, &context);
558 /* only resume the vm if all keys are set and valid */
563 static void bdrv_key_cb(void *opaque, int err)
565 Monitor *mon = opaque;
567 /* another key was set successfully, retry to continue */
572 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
574 struct bdrv_iterate_context *context = opaque;
576 if (!context->err && bdrv_key_required(bs)) {
577 context->err = -EBUSY;
578 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
583 #ifdef CONFIG_GDBSTUB
584 static void do_gdbserver(Monitor *mon, const char *device)
587 device = "tcp::" DEFAULT_GDBSTUB_PORT;
588 if (gdbserver_start(device) < 0) {
589 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
591 } else if (strcmp(device, "none") == 0) {
592 monitor_printf(mon, "Disabled gdbserver\n");
594 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
600 static void monitor_printc(Monitor *mon, int c)
602 monitor_printf(mon, "'");
605 monitor_printf(mon, "\\'");
608 monitor_printf(mon, "\\\\");
611 monitor_printf(mon, "\\n");
614 monitor_printf(mon, "\\r");
617 if (c >= 32 && c <= 126) {
618 monitor_printf(mon, "%c", c);
620 monitor_printf(mon, "\\x%02x", c);
624 monitor_printf(mon, "'");
627 static void memory_dump(Monitor *mon, int count, int format, int wsize,
628 target_phys_addr_t addr, int is_physical)
631 int nb_per_line, l, line_size, i, max_digits, len;
639 if (!env && !is_physical)
644 } else if (wsize == 4) {
647 /* as default we use the current CS size */
651 if ((env->efer & MSR_EFER_LMA) &&
652 (env->segs[R_CS].flags & DESC_L_MASK))
656 if (!(env->segs[R_CS].flags & DESC_B_MASK))
661 monitor_disas(mon, env, addr, count, is_physical, flags);
670 nb_per_line = line_size / wsize;
675 max_digits = (wsize * 8 + 2) / 3;
679 max_digits = (wsize * 8) / 4;
683 max_digits = (wsize * 8 * 10 + 32) / 33;
692 monitor_printf(mon, TARGET_FMT_plx ":", addr);
694 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
699 cpu_physical_memory_rw(addr, buf, l, 0);
704 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
705 monitor_printf(mon, " Cannot access memory\n");
714 v = ldub_raw(buf + i);
717 v = lduw_raw(buf + i);
720 v = (uint32_t)ldl_raw(buf + i);
723 v = ldq_raw(buf + i);
726 monitor_printf(mon, " ");
729 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
732 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
735 monitor_printf(mon, "%*" PRIu64, max_digits, v);
738 monitor_printf(mon, "%*" PRId64, max_digits, v);
741 monitor_printc(mon, v);
746 monitor_printf(mon, "\n");
752 #if TARGET_LONG_BITS == 64
753 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
755 #define GET_TLONG(h, l) (l)
758 static void do_memory_dump(Monitor *mon, int count, int format, int size,
759 uint32_t addrh, uint32_t addrl)
761 target_long addr = GET_TLONG(addrh, addrl);
762 memory_dump(mon, count, format, size, addr, 0);
765 #if TARGET_PHYS_ADDR_BITS > 32
766 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
768 #define GET_TPHYSADDR(h, l) (l)
771 static void do_physical_memory_dump(Monitor *mon, int count, int format,
772 int size, uint32_t addrh, uint32_t addrl)
775 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
776 memory_dump(mon, count, format, size, addr, 1);
779 static void do_print(Monitor *mon, int count, int format, int size,
780 unsigned int valh, unsigned int vall)
782 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
783 #if TARGET_PHYS_ADDR_BITS == 32
786 monitor_printf(mon, "%#o", val);
789 monitor_printf(mon, "%#x", val);
792 monitor_printf(mon, "%u", val);
796 monitor_printf(mon, "%d", val);
799 monitor_printc(mon, val);
805 monitor_printf(mon, "%#" PRIo64, val);
808 monitor_printf(mon, "%#" PRIx64, val);
811 monitor_printf(mon, "%" PRIu64, val);
815 monitor_printf(mon, "%" PRId64, val);
818 monitor_printc(mon, val);
822 monitor_printf(mon, "\n");
825 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
826 uint32_t size, const char *filename)
829 target_long addr = GET_TLONG(valh, vall);
838 f = fopen(filename, "wb");
840 monitor_printf(mon, "could not open '%s'\n", filename);
847 cpu_memory_rw_debug(env, addr, buf, l, 0);
848 fwrite(buf, 1, l, f);
855 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
856 unsigned int vall, uint32_t size,
857 const char *filename)
862 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
864 f = fopen(filename, "wb");
866 monitor_printf(mon, "could not open '%s'\n", filename);
873 cpu_physical_memory_rw(addr, buf, l, 0);
874 fwrite(buf, 1, l, f);
882 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
889 for(addr = start; addr < (start + size); addr++) {
890 cpu_physical_memory_rw(addr, buf, 1, 0);
891 /* BSD sum algorithm ('sum' Unix command) */
892 sum = (sum >> 1) | (sum << 15);
895 monitor_printf(mon, "%05d\n", sum);
903 static const KeyDef key_defs[] = {
930 { 0x0e, "backspace" },
967 { 0x37, "asterisk" },
970 { 0x3a, "caps_lock" },
981 { 0x45, "num_lock" },
982 { 0x46, "scroll_lock" },
984 { 0xb5, "kp_divide" },
985 { 0x37, "kp_multiply" },
986 { 0x4a, "kp_subtract" },
988 { 0x9c, "kp_enter" },
989 { 0x53, "kp_decimal" },
1022 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1037 { 0xfe, "compose" },
1042 static int get_keycode(const char *key)
1048 for(p = key_defs; p->name != NULL; p++) {
1049 if (!strcmp(key, p->name))
1052 if (strstart(key, "0x", NULL)) {
1053 ret = strtoul(key, &endp, 0);
1054 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1060 #define MAX_KEYCODES 16
1061 static uint8_t keycodes[MAX_KEYCODES];
1062 static int nb_pending_keycodes;
1063 static QEMUTimer *key_timer;
1065 static void release_keys(void *opaque)
1069 while (nb_pending_keycodes > 0) {
1070 nb_pending_keycodes--;
1071 keycode = keycodes[nb_pending_keycodes];
1073 kbd_put_keycode(0xe0);
1074 kbd_put_keycode(keycode | 0x80);
1078 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1081 char keyname_buf[16];
1083 int keyname_len, keycode, i;
1085 if (nb_pending_keycodes > 0) {
1086 qemu_del_timer(key_timer);
1093 separator = strchr(string, '-');
1094 keyname_len = separator ? separator - string : strlen(string);
1095 if (keyname_len > 0) {
1096 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1097 if (keyname_len > sizeof(keyname_buf) - 1) {
1098 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1101 if (i == MAX_KEYCODES) {
1102 monitor_printf(mon, "too many keys\n");
1105 keyname_buf[keyname_len] = 0;
1106 keycode = get_keycode(keyname_buf);
1108 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1111 keycodes[i++] = keycode;
1115 string = separator + 1;
1117 nb_pending_keycodes = i;
1118 /* key down events */
1119 for (i = 0; i < nb_pending_keycodes; i++) {
1120 keycode = keycodes[i];
1122 kbd_put_keycode(0xe0);
1123 kbd_put_keycode(keycode & 0x7f);
1125 /* delayed key up events */
1126 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1127 muldiv64(ticks_per_sec, hold_time, 1000));
1130 static int mouse_button_state;
1132 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1136 dx = strtol(dx_str, NULL, 0);
1137 dy = strtol(dy_str, NULL, 0);
1140 dz = strtol(dz_str, NULL, 0);
1141 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1144 static void do_mouse_button(Monitor *mon, int button_state)
1146 mouse_button_state = button_state;
1147 kbd_mouse_event(0, 0, 0, mouse_button_state);
1150 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1151 int addr, int has_index, int index)
1157 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1165 val = cpu_inb(NULL, addr);
1169 val = cpu_inw(NULL, addr);
1173 val = cpu_inl(NULL, addr);
1177 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1178 suffix, addr, size * 2, val);
1181 /* boot_set handler */
1182 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1183 static void *boot_opaque;
1185 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1187 qemu_boot_set_handler = func;
1188 boot_opaque = opaque;
1191 static void do_boot_set(Monitor *mon, const char *bootdevice)
1195 if (qemu_boot_set_handler) {
1196 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1198 monitor_printf(mon, "boot device list now set to %s\n",
1201 monitor_printf(mon, "setting boot device list failed with "
1204 monitor_printf(mon, "no function defined to set boot device list for "
1205 "this architecture\n");
1209 static void do_system_reset(Monitor *mon)
1211 qemu_system_reset_request();
1214 static void do_system_powerdown(Monitor *mon)
1216 qemu_system_powerdown_request();
1219 #if defined(TARGET_I386)
1220 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1222 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1225 pte & PG_GLOBAL_MASK ? 'G' : '-',
1226 pte & PG_PSE_MASK ? 'P' : '-',
1227 pte & PG_DIRTY_MASK ? 'D' : '-',
1228 pte & PG_ACCESSED_MASK ? 'A' : '-',
1229 pte & PG_PCD_MASK ? 'C' : '-',
1230 pte & PG_PWT_MASK ? 'T' : '-',
1231 pte & PG_USER_MASK ? 'U' : '-',
1232 pte & PG_RW_MASK ? 'W' : '-');
1235 static void tlb_info(Monitor *mon)
1239 uint32_t pgd, pde, pte;
1241 env = mon_get_cpu();
1245 if (!(env->cr[0] & CR0_PG_MASK)) {
1246 monitor_printf(mon, "PG disabled\n");
1249 pgd = env->cr[3] & ~0xfff;
1250 for(l1 = 0; l1 < 1024; l1++) {
1251 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1252 pde = le32_to_cpu(pde);
1253 if (pde & PG_PRESENT_MASK) {
1254 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1255 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1257 for(l2 = 0; l2 < 1024; l2++) {
1258 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1259 (uint8_t *)&pte, 4);
1260 pte = le32_to_cpu(pte);
1261 if (pte & PG_PRESENT_MASK) {
1262 print_pte(mon, (l1 << 22) + (l2 << 12),
1272 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1273 uint32_t end, int prot)
1276 prot1 = *plast_prot;
1277 if (prot != prot1) {
1278 if (*pstart != -1) {
1279 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1280 *pstart, end, end - *pstart,
1281 prot1 & PG_USER_MASK ? 'u' : '-',
1283 prot1 & PG_RW_MASK ? 'w' : '-');
1293 static void mem_info(Monitor *mon)
1296 int l1, l2, prot, last_prot;
1297 uint32_t pgd, pde, pte, start, end;
1299 env = mon_get_cpu();
1303 if (!(env->cr[0] & CR0_PG_MASK)) {
1304 monitor_printf(mon, "PG disabled\n");
1307 pgd = env->cr[3] & ~0xfff;
1310 for(l1 = 0; l1 < 1024; l1++) {
1311 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1312 pde = le32_to_cpu(pde);
1314 if (pde & PG_PRESENT_MASK) {
1315 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1316 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1317 mem_print(mon, &start, &last_prot, end, prot);
1319 for(l2 = 0; l2 < 1024; l2++) {
1320 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1321 (uint8_t *)&pte, 4);
1322 pte = le32_to_cpu(pte);
1323 end = (l1 << 22) + (l2 << 12);
1324 if (pte & PG_PRESENT_MASK) {
1325 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1329 mem_print(mon, &start, &last_prot, end, prot);
1334 mem_print(mon, &start, &last_prot, end, prot);
1340 #if defined(TARGET_SH4)
1342 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1344 monitor_printf(mon, " tlb%i:\t"
1345 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1346 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1347 "dirty=%hhu writethrough=%hhu\n",
1349 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1350 tlb->v, tlb->sh, tlb->c, tlb->pr,
1354 static void tlb_info(Monitor *mon)
1356 CPUState *env = mon_get_cpu();
1359 monitor_printf (mon, "ITLB:\n");
1360 for (i = 0 ; i < ITLB_SIZE ; i++)
1361 print_tlb (mon, i, &env->itlb[i]);
1362 monitor_printf (mon, "UTLB:\n");
1363 for (i = 0 ; i < UTLB_SIZE ; i++)
1364 print_tlb (mon, i, &env->utlb[i]);
1369 static void do_info_kqemu(Monitor *mon)
1375 env = mon_get_cpu();
1377 monitor_printf(mon, "No cpu initialized yet");
1380 val = env->kqemu_enabled;
1381 monitor_printf(mon, "kqemu support: ");
1385 monitor_printf(mon, "disabled\n");
1388 monitor_printf(mon, "enabled for user code\n");
1391 monitor_printf(mon, "enabled for user and kernel code\n");
1395 monitor_printf(mon, "kqemu support: not compiled\n");
1399 static void do_info_kvm(Monitor *mon)
1402 monitor_printf(mon, "kvm support: ");
1404 monitor_printf(mon, "enabled\n");
1406 monitor_printf(mon, "disabled\n");
1408 monitor_printf(mon, "kvm support: not compiled\n");
1412 #ifdef CONFIG_PROFILER
1416 int64_t kqemu_exec_count;
1418 int64_t kqemu_ret_int_count;
1419 int64_t kqemu_ret_excp_count;
1420 int64_t kqemu_ret_intr_count;
1422 static void do_info_profile(Monitor *mon)
1428 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1429 dev_time, dev_time / (double)ticks_per_sec);
1430 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1431 qemu_time, qemu_time / (double)ticks_per_sec);
1432 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1433 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1435 kqemu_time, kqemu_time / (double)ticks_per_sec,
1436 kqemu_time / (double)total * 100.0,
1438 kqemu_ret_int_count,
1439 kqemu_ret_excp_count,
1440 kqemu_ret_intr_count);
1443 kqemu_exec_count = 0;
1445 kqemu_ret_int_count = 0;
1446 kqemu_ret_excp_count = 0;
1447 kqemu_ret_intr_count = 0;
1449 kqemu_record_dump();
1453 static void do_info_profile(Monitor *mon)
1455 monitor_printf(mon, "Internal profiler not compiled\n");
1459 /* Capture support */
1460 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1462 static void do_info_capture(Monitor *mon)
1467 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1468 monitor_printf(mon, "[%d]: ", i);
1469 s->ops.info (s->opaque);
1473 static void do_stop_capture(Monitor *mon, int n)
1478 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1480 s->ops.destroy (s->opaque);
1481 LIST_REMOVE (s, entries);
1489 static void do_wav_capture(Monitor *mon, const char *path,
1490 int has_freq, int freq,
1491 int has_bits, int bits,
1492 int has_channels, int nchannels)
1496 s = qemu_mallocz (sizeof (*s));
1498 freq = has_freq ? freq : 44100;
1499 bits = has_bits ? bits : 16;
1500 nchannels = has_channels ? nchannels : 2;
1502 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1503 monitor_printf(mon, "Faied to add wave capture\n");
1506 LIST_INSERT_HEAD (&capture_head, s, entries);
1510 #if defined(TARGET_I386)
1511 static void do_inject_nmi(Monitor *mon, int cpu_index)
1515 for (env = first_cpu; env != NULL; env = env->next_cpu)
1516 if (env->cpu_index == cpu_index) {
1517 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1523 static void do_info_status(Monitor *mon)
1527 monitor_printf(mon, "VM status: running (single step mode)\n");
1529 monitor_printf(mon, "VM status: running\n");
1532 monitor_printf(mon, "VM status: paused\n");
1536 static void do_balloon(Monitor *mon, int value)
1538 ram_addr_t target = value;
1539 qemu_balloon(target << 20);
1542 static void do_info_balloon(Monitor *mon)
1546 actual = qemu_balloon_status();
1547 if (kvm_enabled() && !kvm_has_sync_mmu())
1548 monitor_printf(mon, "Using KVM without synchronous MMU, "
1549 "ballooning disabled\n");
1550 else if (actual == 0)
1551 monitor_printf(mon, "Ballooning not activated in VM\n");
1553 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1556 static void do_acl(Monitor *mon,
1557 const char *command,
1558 const char *aclname,
1565 acl = qemu_acl_find(aclname);
1567 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1571 if (strcmp(command, "show") == 0) {
1573 qemu_acl_entry *entry;
1574 monitor_printf(mon, "policy: %s\n",
1575 acl->defaultDeny ? "deny" : "allow");
1576 TAILQ_FOREACH(entry, &acl->entries, next) {
1578 monitor_printf(mon, "%d: %s %s\n", i,
1579 entry->deny ? "deny" : "allow",
1582 } else if (strcmp(command, "reset") == 0) {
1583 qemu_acl_reset(acl);
1584 monitor_printf(mon, "acl: removed all rules\n");
1585 } else if (strcmp(command, "policy") == 0) {
1587 monitor_printf(mon, "acl: missing policy parameter\n");
1591 if (strcmp(match, "allow") == 0) {
1592 acl->defaultDeny = 0;
1593 monitor_printf(mon, "acl: policy set to 'allow'\n");
1594 } else if (strcmp(match, "deny") == 0) {
1595 acl->defaultDeny = 1;
1596 monitor_printf(mon, "acl: policy set to 'deny'\n");
1598 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1600 } else if ((strcmp(command, "allow") == 0) ||
1601 (strcmp(command, "deny") == 0)) {
1602 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1606 monitor_printf(mon, "acl: missing match parameter\n");
1611 ret = qemu_acl_insert(acl, deny, match, index);
1613 ret = qemu_acl_append(acl, deny, match);
1615 monitor_printf(mon, "acl: unable to add acl entry\n");
1617 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1618 } else if (strcmp(command, "remove") == 0) {
1622 monitor_printf(mon, "acl: missing match parameter\n");
1626 ret = qemu_acl_remove(acl, match);
1628 monitor_printf(mon, "acl: no matching acl entry\n");
1630 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1632 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1636 /* Please update qemu-doc.texi when adding or changing commands */
1637 static const mon_cmd_t mon_cmds[] = {
1638 { "help|?", "s?", help_cmd,
1639 "[cmd]", "show the help" },
1640 { "commit", "s", do_commit,
1641 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1642 { "info", "s?", do_info,
1643 "[subcommand]", "show various information about the system state" },
1644 { "q|quit", "", do_quit,
1645 "", "quit the emulator" },
1646 { "eject", "-fB", do_eject,
1647 "[-f] device", "eject a removable medium (use -f to force it)" },
1648 { "change", "BFs?", do_change,
1649 "device filename [format]", "change a removable medium, optional format" },
1650 { "screendump", "F", do_screen_dump,
1651 "filename", "save screen into PPM image 'filename'" },
1652 { "logfile", "F", do_logfile,
1653 "filename", "output logs to 'filename'" },
1654 { "log", "s", do_log,
1655 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1656 { "savevm", "s?", do_savevm,
1657 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1658 { "loadvm", "s", do_loadvm,
1659 "tag|id", "restore a VM snapshot from its tag or id" },
1660 { "delvm", "s", do_delvm,
1661 "tag|id", "delete a VM snapshot from its tag or id" },
1662 { "singlestep", "s?", do_singlestep,
1663 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1664 { "stop", "", do_stop,
1665 "", "stop emulation", },
1666 { "c|cont", "", do_cont,
1667 "", "resume emulation", },
1668 #ifdef CONFIG_GDBSTUB
1669 { "gdbserver", "s?", do_gdbserver,
1670 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
1672 { "x", "/l", do_memory_dump,
1673 "/fmt addr", "virtual memory dump starting at 'addr'", },
1674 { "xp", "/l", do_physical_memory_dump,
1675 "/fmt addr", "physical memory dump starting at 'addr'", },
1676 { "p|print", "/l", do_print,
1677 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1678 { "i", "/ii.", do_ioport_read,
1679 "/fmt addr", "I/O port read" },
1681 { "sendkey", "si?", do_sendkey,
1682 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1683 { "system_reset", "", do_system_reset,
1684 "", "reset the system" },
1685 { "system_powerdown", "", do_system_powerdown,
1686 "", "send system power down event" },
1687 { "sum", "ii", do_sum,
1688 "addr size", "compute the checksum of a memory region" },
1689 { "usb_add", "s", do_usb_add,
1690 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1691 { "usb_del", "s", do_usb_del,
1692 "device", "remove USB device 'bus.addr'" },
1693 { "cpu", "i", do_cpu_set,
1694 "index", "set the default CPU" },
1695 { "mouse_move", "sss?", do_mouse_move,
1696 "dx dy [dz]", "send mouse move events" },
1697 { "mouse_button", "i", do_mouse_button,
1698 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1699 { "mouse_set", "i", do_mouse_set,
1700 "index", "set which mouse device receives events" },
1702 { "wavcapture", "si?i?i?", do_wav_capture,
1703 "path [frequency [bits [channels]]]",
1704 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1706 { "stopcapture", "i", do_stop_capture,
1707 "capture index", "stop capture" },
1708 { "memsave", "lis", do_memory_save,
1709 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1710 { "pmemsave", "lis", do_physical_memory_save,
1711 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1712 { "boot_set", "s", do_boot_set,
1713 "bootdevice", "define new values for the boot device list" },
1714 #if defined(TARGET_I386)
1715 { "nmi", "i", do_inject_nmi,
1716 "cpu", "inject an NMI on the given CPU", },
1718 { "migrate", "-ds", do_migrate,
1719 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1720 { "migrate_cancel", "", do_migrate_cancel,
1721 "", "cancel the current VM migration" },
1722 { "migrate_set_speed", "s", do_migrate_set_speed,
1723 "value", "set maximum speed (in bytes) for migrations" },
1724 #if defined(TARGET_I386)
1725 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1726 "[file=file][,if=type][,bus=n]\n"
1727 "[,unit=m][,media=d][index=i]\n"
1728 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1729 "[snapshot=on|off][,cache=on|off]",
1730 "add drive to PCI storage controller" },
1731 { "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" },
1732 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1733 { "host_net_add", "ss", net_host_device_add,
1734 "[tap,user,socket,vde,dump] options", "add host VLAN client" },
1735 { "host_net_remove", "is", net_host_device_remove,
1736 "vlan_id name", "remove host VLAN client" },
1738 { "balloon", "i", do_balloon,
1739 "target", "request VM to change it's memory allocation (in MB)" },
1740 { "set_link", "ss", do_set_link,
1741 "name up|down", "change the link status of a network adapter" },
1742 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
1743 "acl show vnc.username\n"
1744 "acl policy vnc.username deny\n"
1745 "acl allow vnc.username fred\n"
1746 "acl deny vnc.username bob\n"
1747 "acl reset vnc.username\n" },
1751 /* Please update qemu-doc.texi when adding or changing commands */
1752 static const mon_cmd_t info_cmds[] = {
1753 { "version", "", do_info_version,
1754 "", "show the version of QEMU" },
1755 { "network", "", do_info_network,
1756 "", "show the network state" },
1757 { "chardev", "", qemu_chr_info,
1758 "", "show the character devices" },
1759 { "block", "", bdrv_info,
1760 "", "show the block devices" },
1761 { "blockstats", "", bdrv_info_stats,
1762 "", "show block device statistics" },
1763 { "registers", "", do_info_registers,
1764 "", "show the cpu registers" },
1765 { "cpus", "", do_info_cpus,
1766 "", "show infos for each CPU" },
1767 { "history", "", do_info_history,
1768 "", "show the command line history", },
1769 { "irq", "", irq_info,
1770 "", "show the interrupts statistics (if available)", },
1771 { "pic", "", pic_info,
1772 "", "show i8259 (PIC) state", },
1773 { "pci", "", pci_info,
1774 "", "show PCI info", },
1775 #if defined(TARGET_I386) || defined(TARGET_SH4)
1776 { "tlb", "", tlb_info,
1777 "", "show virtual to physical memory mappings", },
1779 #if defined(TARGET_I386)
1780 { "mem", "", mem_info,
1781 "", "show the active virtual memory mappings", },
1782 { "hpet", "", do_info_hpet,
1783 "", "show state of HPET", },
1785 { "jit", "", do_info_jit,
1786 "", "show dynamic compiler info", },
1787 { "kqemu", "", do_info_kqemu,
1788 "", "show KQEMU information", },
1789 { "kvm", "", do_info_kvm,
1790 "", "show KVM information", },
1791 { "usb", "", usb_info,
1792 "", "show guest USB devices", },
1793 { "usbhost", "", usb_host_info,
1794 "", "show host USB devices", },
1795 { "profile", "", do_info_profile,
1796 "", "show profiling information", },
1797 { "capture", "", do_info_capture,
1798 "", "show capture information" },
1799 { "snapshots", "", do_info_snapshots,
1800 "", "show the currently saved VM snapshots" },
1801 { "status", "", do_info_status,
1802 "", "show the current VM status (running|paused)" },
1803 { "pcmcia", "", pcmcia_info,
1804 "", "show guest PCMCIA status" },
1805 { "mice", "", do_info_mice,
1806 "", "show which guest mouse is receiving events" },
1807 { "vnc", "", do_info_vnc,
1808 "", "show the vnc server status"},
1809 { "name", "", do_info_name,
1810 "", "show the current VM name" },
1811 { "uuid", "", do_info_uuid,
1812 "", "show the current VM UUID" },
1813 #if defined(TARGET_PPC)
1814 { "cpustats", "", do_info_cpu_stats,
1815 "", "show CPU statistics", },
1817 #if defined(CONFIG_SLIRP)
1818 { "slirp", "", do_info_slirp,
1819 "", "show SLIRP statistics", },
1821 { "migrate", "", do_info_migrate, "", "show migration status" },
1822 { "balloon", "", do_info_balloon,
1823 "", "show balloon information" },
1827 /*******************************************************************/
1829 static const char *pch;
1830 static jmp_buf expr_env;
1835 typedef struct MonitorDef {
1838 target_long (*get_value)(const struct MonitorDef *md, int val);
1842 #if defined(TARGET_I386)
1843 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1845 CPUState *env = mon_get_cpu();
1848 return env->eip + env->segs[R_CS].base;
1852 #if defined(TARGET_PPC)
1853 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1855 CPUState *env = mon_get_cpu();
1863 for (i = 0; i < 8; i++)
1864 u |= env->crf[i] << (32 - (4 * i));
1869 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1871 CPUState *env = mon_get_cpu();
1877 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1879 CPUState *env = mon_get_cpu();
1885 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1887 CPUState *env = mon_get_cpu();
1890 return cpu_ppc_load_decr(env);
1893 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1895 CPUState *env = mon_get_cpu();
1898 return cpu_ppc_load_tbu(env);
1901 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1903 CPUState *env = mon_get_cpu();
1906 return cpu_ppc_load_tbl(env);
1910 #if defined(TARGET_SPARC)
1911 #ifndef TARGET_SPARC64
1912 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1914 CPUState *env = mon_get_cpu();
1917 return GET_PSR(env);
1921 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1923 CPUState *env = mon_get_cpu();
1926 return env->regwptr[val];
1930 static const MonitorDef monitor_defs[] = {
1933 #define SEG(name, seg) \
1934 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1935 { name ".base", offsetof(CPUState, segs[seg].base) },\
1936 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1938 { "eax", offsetof(CPUState, regs[0]) },
1939 { "ecx", offsetof(CPUState, regs[1]) },
1940 { "edx", offsetof(CPUState, regs[2]) },
1941 { "ebx", offsetof(CPUState, regs[3]) },
1942 { "esp|sp", offsetof(CPUState, regs[4]) },
1943 { "ebp|fp", offsetof(CPUState, regs[5]) },
1944 { "esi", offsetof(CPUState, regs[6]) },
1945 { "edi", offsetof(CPUState, regs[7]) },
1946 #ifdef TARGET_X86_64
1947 { "r8", offsetof(CPUState, regs[8]) },
1948 { "r9", offsetof(CPUState, regs[9]) },
1949 { "r10", offsetof(CPUState, regs[10]) },
1950 { "r11", offsetof(CPUState, regs[11]) },
1951 { "r12", offsetof(CPUState, regs[12]) },
1952 { "r13", offsetof(CPUState, regs[13]) },
1953 { "r14", offsetof(CPUState, regs[14]) },
1954 { "r15", offsetof(CPUState, regs[15]) },
1956 { "eflags", offsetof(CPUState, eflags) },
1957 { "eip", offsetof(CPUState, eip) },
1964 { "pc", 0, monitor_get_pc, },
1965 #elif defined(TARGET_PPC)
1966 /* General purpose registers */
1967 { "r0", offsetof(CPUState, gpr[0]) },
1968 { "r1", offsetof(CPUState, gpr[1]) },
1969 { "r2", offsetof(CPUState, gpr[2]) },
1970 { "r3", offsetof(CPUState, gpr[3]) },
1971 { "r4", offsetof(CPUState, gpr[4]) },
1972 { "r5", offsetof(CPUState, gpr[5]) },
1973 { "r6", offsetof(CPUState, gpr[6]) },
1974 { "r7", offsetof(CPUState, gpr[7]) },
1975 { "r8", offsetof(CPUState, gpr[8]) },
1976 { "r9", offsetof(CPUState, gpr[9]) },
1977 { "r10", offsetof(CPUState, gpr[10]) },
1978 { "r11", offsetof(CPUState, gpr[11]) },
1979 { "r12", offsetof(CPUState, gpr[12]) },
1980 { "r13", offsetof(CPUState, gpr[13]) },
1981 { "r14", offsetof(CPUState, gpr[14]) },
1982 { "r15", offsetof(CPUState, gpr[15]) },
1983 { "r16", offsetof(CPUState, gpr[16]) },
1984 { "r17", offsetof(CPUState, gpr[17]) },
1985 { "r18", offsetof(CPUState, gpr[18]) },
1986 { "r19", offsetof(CPUState, gpr[19]) },
1987 { "r20", offsetof(CPUState, gpr[20]) },
1988 { "r21", offsetof(CPUState, gpr[21]) },
1989 { "r22", offsetof(CPUState, gpr[22]) },
1990 { "r23", offsetof(CPUState, gpr[23]) },
1991 { "r24", offsetof(CPUState, gpr[24]) },
1992 { "r25", offsetof(CPUState, gpr[25]) },
1993 { "r26", offsetof(CPUState, gpr[26]) },
1994 { "r27", offsetof(CPUState, gpr[27]) },
1995 { "r28", offsetof(CPUState, gpr[28]) },
1996 { "r29", offsetof(CPUState, gpr[29]) },
1997 { "r30", offsetof(CPUState, gpr[30]) },
1998 { "r31", offsetof(CPUState, gpr[31]) },
1999 /* Floating point registers */
2000 { "f0", offsetof(CPUState, fpr[0]) },
2001 { "f1", offsetof(CPUState, fpr[1]) },
2002 { "f2", offsetof(CPUState, fpr[2]) },
2003 { "f3", offsetof(CPUState, fpr[3]) },
2004 { "f4", offsetof(CPUState, fpr[4]) },
2005 { "f5", offsetof(CPUState, fpr[5]) },
2006 { "f6", offsetof(CPUState, fpr[6]) },
2007 { "f7", offsetof(CPUState, fpr[7]) },
2008 { "f8", offsetof(CPUState, fpr[8]) },
2009 { "f9", offsetof(CPUState, fpr[9]) },
2010 { "f10", offsetof(CPUState, fpr[10]) },
2011 { "f11", offsetof(CPUState, fpr[11]) },
2012 { "f12", offsetof(CPUState, fpr[12]) },
2013 { "f13", offsetof(CPUState, fpr[13]) },
2014 { "f14", offsetof(CPUState, fpr[14]) },
2015 { "f15", offsetof(CPUState, fpr[15]) },
2016 { "f16", offsetof(CPUState, fpr[16]) },
2017 { "f17", offsetof(CPUState, fpr[17]) },
2018 { "f18", offsetof(CPUState, fpr[18]) },
2019 { "f19", offsetof(CPUState, fpr[19]) },
2020 { "f20", offsetof(CPUState, fpr[20]) },
2021 { "f21", offsetof(CPUState, fpr[21]) },
2022 { "f22", offsetof(CPUState, fpr[22]) },
2023 { "f23", offsetof(CPUState, fpr[23]) },
2024 { "f24", offsetof(CPUState, fpr[24]) },
2025 { "f25", offsetof(CPUState, fpr[25]) },
2026 { "f26", offsetof(CPUState, fpr[26]) },
2027 { "f27", offsetof(CPUState, fpr[27]) },
2028 { "f28", offsetof(CPUState, fpr[28]) },
2029 { "f29", offsetof(CPUState, fpr[29]) },
2030 { "f30", offsetof(CPUState, fpr[30]) },
2031 { "f31", offsetof(CPUState, fpr[31]) },
2032 { "fpscr", offsetof(CPUState, fpscr) },
2033 /* Next instruction pointer */
2034 { "nip|pc", offsetof(CPUState, nip) },
2035 { "lr", offsetof(CPUState, lr) },
2036 { "ctr", offsetof(CPUState, ctr) },
2037 { "decr", 0, &monitor_get_decr, },
2038 { "ccr", 0, &monitor_get_ccr, },
2039 /* Machine state register */
2040 { "msr", 0, &monitor_get_msr, },
2041 { "xer", 0, &monitor_get_xer, },
2042 { "tbu", 0, &monitor_get_tbu, },
2043 { "tbl", 0, &monitor_get_tbl, },
2044 #if defined(TARGET_PPC64)
2045 /* Address space register */
2046 { "asr", offsetof(CPUState, asr) },
2048 /* Segment registers */
2049 { "sdr1", offsetof(CPUState, sdr1) },
2050 { "sr0", offsetof(CPUState, sr[0]) },
2051 { "sr1", offsetof(CPUState, sr[1]) },
2052 { "sr2", offsetof(CPUState, sr[2]) },
2053 { "sr3", offsetof(CPUState, sr[3]) },
2054 { "sr4", offsetof(CPUState, sr[4]) },
2055 { "sr5", offsetof(CPUState, sr[5]) },
2056 { "sr6", offsetof(CPUState, sr[6]) },
2057 { "sr7", offsetof(CPUState, sr[7]) },
2058 { "sr8", offsetof(CPUState, sr[8]) },
2059 { "sr9", offsetof(CPUState, sr[9]) },
2060 { "sr10", offsetof(CPUState, sr[10]) },
2061 { "sr11", offsetof(CPUState, sr[11]) },
2062 { "sr12", offsetof(CPUState, sr[12]) },
2063 { "sr13", offsetof(CPUState, sr[13]) },
2064 { "sr14", offsetof(CPUState, sr[14]) },
2065 { "sr15", offsetof(CPUState, sr[15]) },
2066 /* Too lazy to put BATs and SPRs ... */
2067 #elif defined(TARGET_SPARC)
2068 { "g0", offsetof(CPUState, gregs[0]) },
2069 { "g1", offsetof(CPUState, gregs[1]) },
2070 { "g2", offsetof(CPUState, gregs[2]) },
2071 { "g3", offsetof(CPUState, gregs[3]) },
2072 { "g4", offsetof(CPUState, gregs[4]) },
2073 { "g5", offsetof(CPUState, gregs[5]) },
2074 { "g6", offsetof(CPUState, gregs[6]) },
2075 { "g7", offsetof(CPUState, gregs[7]) },
2076 { "o0", 0, monitor_get_reg },
2077 { "o1", 1, monitor_get_reg },
2078 { "o2", 2, monitor_get_reg },
2079 { "o3", 3, monitor_get_reg },
2080 { "o4", 4, monitor_get_reg },
2081 { "o5", 5, monitor_get_reg },
2082 { "o6", 6, monitor_get_reg },
2083 { "o7", 7, monitor_get_reg },
2084 { "l0", 8, monitor_get_reg },
2085 { "l1", 9, monitor_get_reg },
2086 { "l2", 10, monitor_get_reg },
2087 { "l3", 11, monitor_get_reg },
2088 { "l4", 12, monitor_get_reg },
2089 { "l5", 13, monitor_get_reg },
2090 { "l6", 14, monitor_get_reg },
2091 { "l7", 15, monitor_get_reg },
2092 { "i0", 16, monitor_get_reg },
2093 { "i1", 17, monitor_get_reg },
2094 { "i2", 18, monitor_get_reg },
2095 { "i3", 19, monitor_get_reg },
2096 { "i4", 20, monitor_get_reg },
2097 { "i5", 21, monitor_get_reg },
2098 { "i6", 22, monitor_get_reg },
2099 { "i7", 23, monitor_get_reg },
2100 { "pc", offsetof(CPUState, pc) },
2101 { "npc", offsetof(CPUState, npc) },
2102 { "y", offsetof(CPUState, y) },
2103 #ifndef TARGET_SPARC64
2104 { "psr", 0, &monitor_get_psr, },
2105 { "wim", offsetof(CPUState, wim) },
2107 { "tbr", offsetof(CPUState, tbr) },
2108 { "fsr", offsetof(CPUState, fsr) },
2109 { "f0", offsetof(CPUState, fpr[0]) },
2110 { "f1", offsetof(CPUState, fpr[1]) },
2111 { "f2", offsetof(CPUState, fpr[2]) },
2112 { "f3", offsetof(CPUState, fpr[3]) },
2113 { "f4", offsetof(CPUState, fpr[4]) },
2114 { "f5", offsetof(CPUState, fpr[5]) },
2115 { "f6", offsetof(CPUState, fpr[6]) },
2116 { "f7", offsetof(CPUState, fpr[7]) },
2117 { "f8", offsetof(CPUState, fpr[8]) },
2118 { "f9", offsetof(CPUState, fpr[9]) },
2119 { "f10", offsetof(CPUState, fpr[10]) },
2120 { "f11", offsetof(CPUState, fpr[11]) },
2121 { "f12", offsetof(CPUState, fpr[12]) },
2122 { "f13", offsetof(CPUState, fpr[13]) },
2123 { "f14", offsetof(CPUState, fpr[14]) },
2124 { "f15", offsetof(CPUState, fpr[15]) },
2125 { "f16", offsetof(CPUState, fpr[16]) },
2126 { "f17", offsetof(CPUState, fpr[17]) },
2127 { "f18", offsetof(CPUState, fpr[18]) },
2128 { "f19", offsetof(CPUState, fpr[19]) },
2129 { "f20", offsetof(CPUState, fpr[20]) },
2130 { "f21", offsetof(CPUState, fpr[21]) },
2131 { "f22", offsetof(CPUState, fpr[22]) },
2132 { "f23", offsetof(CPUState, fpr[23]) },
2133 { "f24", offsetof(CPUState, fpr[24]) },
2134 { "f25", offsetof(CPUState, fpr[25]) },
2135 { "f26", offsetof(CPUState, fpr[26]) },
2136 { "f27", offsetof(CPUState, fpr[27]) },
2137 { "f28", offsetof(CPUState, fpr[28]) },
2138 { "f29", offsetof(CPUState, fpr[29]) },
2139 { "f30", offsetof(CPUState, fpr[30]) },
2140 { "f31", offsetof(CPUState, fpr[31]) },
2141 #ifdef TARGET_SPARC64
2142 { "f32", offsetof(CPUState, fpr[32]) },
2143 { "f34", offsetof(CPUState, fpr[34]) },
2144 { "f36", offsetof(CPUState, fpr[36]) },
2145 { "f38", offsetof(CPUState, fpr[38]) },
2146 { "f40", offsetof(CPUState, fpr[40]) },
2147 { "f42", offsetof(CPUState, fpr[42]) },
2148 { "f44", offsetof(CPUState, fpr[44]) },
2149 { "f46", offsetof(CPUState, fpr[46]) },
2150 { "f48", offsetof(CPUState, fpr[48]) },
2151 { "f50", offsetof(CPUState, fpr[50]) },
2152 { "f52", offsetof(CPUState, fpr[52]) },
2153 { "f54", offsetof(CPUState, fpr[54]) },
2154 { "f56", offsetof(CPUState, fpr[56]) },
2155 { "f58", offsetof(CPUState, fpr[58]) },
2156 { "f60", offsetof(CPUState, fpr[60]) },
2157 { "f62", offsetof(CPUState, fpr[62]) },
2158 { "asi", offsetof(CPUState, asi) },
2159 { "pstate", offsetof(CPUState, pstate) },
2160 { "cansave", offsetof(CPUState, cansave) },
2161 { "canrestore", offsetof(CPUState, canrestore) },
2162 { "otherwin", offsetof(CPUState, otherwin) },
2163 { "wstate", offsetof(CPUState, wstate) },
2164 { "cleanwin", offsetof(CPUState, cleanwin) },
2165 { "fprs", offsetof(CPUState, fprs) },
2171 static void expr_error(Monitor *mon, const char *msg)
2173 monitor_printf(mon, "%s\n", msg);
2174 longjmp(expr_env, 1);
2177 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2178 static int get_monitor_def(target_long *pval, const char *name)
2180 const MonitorDef *md;
2183 for(md = monitor_defs; md->name != NULL; md++) {
2184 if (compare_cmd(name, md->name)) {
2185 if (md->get_value) {
2186 *pval = md->get_value(md, md->offset);
2188 CPUState *env = mon_get_cpu();
2191 ptr = (uint8_t *)env + md->offset;
2194 *pval = *(int32_t *)ptr;
2197 *pval = *(target_long *)ptr;
2210 static void next(void)
2214 while (qemu_isspace(*pch))
2219 static int64_t expr_sum(Monitor *mon);
2221 static int64_t expr_unary(Monitor *mon)
2230 n = expr_unary(mon);
2234 n = -expr_unary(mon);
2238 n = ~expr_unary(mon);
2244 expr_error(mon, "')' expected");
2251 expr_error(mon, "character constant expected");
2255 expr_error(mon, "missing terminating \' character");
2265 while ((*pch >= 'a' && *pch <= 'z') ||
2266 (*pch >= 'A' && *pch <= 'Z') ||
2267 (*pch >= '0' && *pch <= '9') ||
2268 *pch == '_' || *pch == '.') {
2269 if ((q - buf) < sizeof(buf) - 1)
2273 while (qemu_isspace(*pch))
2276 ret = get_monitor_def(®, buf);
2278 expr_error(mon, "unknown register");
2280 expr_error(mon, "no cpu defined");
2285 expr_error(mon, "unexpected end of expression");
2289 #if TARGET_PHYS_ADDR_BITS > 32
2290 n = strtoull(pch, &p, 0);
2292 n = strtoul(pch, &p, 0);
2295 expr_error(mon, "invalid char in expression");
2298 while (qemu_isspace(*pch))
2306 static int64_t expr_prod(Monitor *mon)
2311 val = expr_unary(mon);
2314 if (op != '*' && op != '/' && op != '%')
2317 val2 = expr_unary(mon);
2326 expr_error(mon, "division by zero");
2337 static int64_t expr_logic(Monitor *mon)
2342 val = expr_prod(mon);
2345 if (op != '&' && op != '|' && op != '^')
2348 val2 = expr_prod(mon);
2365 static int64_t expr_sum(Monitor *mon)
2370 val = expr_logic(mon);
2373 if (op != '+' && op != '-')
2376 val2 = expr_logic(mon);
2385 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2388 if (setjmp(expr_env)) {
2392 while (qemu_isspace(*pch))
2394 *pval = expr_sum(mon);
2399 static int get_str(char *buf, int buf_size, const char **pp)
2407 while (qemu_isspace(*p))
2417 while (*p != '\0' && *p != '\"') {
2433 qemu_printf("unsupported escape code: '\\%c'\n", c);
2436 if ((q - buf) < buf_size - 1) {
2440 if ((q - buf) < buf_size - 1) {
2447 qemu_printf("unterminated string\n");
2452 while (*p != '\0' && !qemu_isspace(*p)) {
2453 if ((q - buf) < buf_size - 1) {
2464 static int default_fmt_format = 'x';
2465 static int default_fmt_size = 4;
2469 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2471 const char *p, *pstart, *typestr;
2473 int c, nb_args, len, i, has_arg;
2474 const mon_cmd_t *cmd;
2477 void *str_allocated[MAX_ARGS];
2478 void *args[MAX_ARGS];
2479 void (*handler_0)(Monitor *mon);
2480 void (*handler_1)(Monitor *mon, void *arg0);
2481 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2482 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2483 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2485 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2486 void *arg3, void *arg4);
2487 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2488 void *arg3, void *arg4, void *arg5);
2489 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2490 void *arg3, void *arg4, void *arg5, void *arg6);
2493 monitor_printf(mon, "command='%s'\n", cmdline);
2496 /* extract the command name */
2499 while (qemu_isspace(*p))
2504 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2507 if (len > sizeof(cmdname) - 1)
2508 len = sizeof(cmdname) - 1;
2509 memcpy(cmdname, pstart, len);
2510 cmdname[len] = '\0';
2512 /* find the command */
2513 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2514 if (compare_cmd(cmdname, cmd->name))
2517 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2521 for(i = 0; i < MAX_ARGS; i++)
2522 str_allocated[i] = NULL;
2524 /* parse the parameters */
2525 typestr = cmd->args_type;
2540 while (qemu_isspace(*p))
2542 if (*typestr == '?') {
2545 /* no optional string: NULL argument */
2550 ret = get_str(buf, sizeof(buf), &p);
2554 monitor_printf(mon, "%s: filename expected\n",
2558 monitor_printf(mon, "%s: block device name expected\n",
2562 monitor_printf(mon, "%s: string expected\n", cmdname);
2567 str = qemu_malloc(strlen(buf) + 1);
2568 pstrcpy(str, sizeof(buf), buf);
2569 str_allocated[nb_args] = str;
2571 if (nb_args >= MAX_ARGS) {
2573 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2576 args[nb_args++] = str;
2581 int count, format, size;
2583 while (qemu_isspace(*p))
2589 if (qemu_isdigit(*p)) {
2591 while (qemu_isdigit(*p)) {
2592 count = count * 10 + (*p - '0');
2630 if (*p != '\0' && !qemu_isspace(*p)) {
2631 monitor_printf(mon, "invalid char in format: '%c'\n",
2636 format = default_fmt_format;
2637 if (format != 'i') {
2638 /* for 'i', not specifying a size gives -1 as size */
2640 size = default_fmt_size;
2641 default_fmt_size = size;
2643 default_fmt_format = format;
2646 format = default_fmt_format;
2647 if (format != 'i') {
2648 size = default_fmt_size;
2653 if (nb_args + 3 > MAX_ARGS)
2655 args[nb_args++] = (void*)(long)count;
2656 args[nb_args++] = (void*)(long)format;
2657 args[nb_args++] = (void*)(long)size;
2665 while (qemu_isspace(*p))
2667 if (*typestr == '?' || *typestr == '.') {
2668 if (*typestr == '?') {
2676 while (qemu_isspace(*p))
2684 if (nb_args >= MAX_ARGS)
2686 args[nb_args++] = (void *)(long)has_arg;
2688 if (nb_args >= MAX_ARGS)
2694 if (get_expr(mon, &val, &p))
2698 if (nb_args >= MAX_ARGS)
2700 args[nb_args++] = (void *)(long)val;
2702 if ((nb_args + 1) >= MAX_ARGS)
2704 #if TARGET_PHYS_ADDR_BITS > 32
2705 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2707 args[nb_args++] = (void *)0;
2709 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2721 while (qemu_isspace(*p))
2727 monitor_printf(mon, "%s: unsupported option -%c\n",
2734 if (nb_args >= MAX_ARGS)
2736 args[nb_args++] = (void *)(long)has_option;
2741 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2745 /* check that all arguments were parsed */
2746 while (qemu_isspace(*p))
2749 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2756 handler_0 = cmd->handler;
2760 handler_1 = cmd->handler;
2761 handler_1(mon, args[0]);
2764 handler_2 = cmd->handler;
2765 handler_2(mon, args[0], args[1]);
2768 handler_3 = cmd->handler;
2769 handler_3(mon, args[0], args[1], args[2]);
2772 handler_4 = cmd->handler;
2773 handler_4(mon, args[0], args[1], args[2], args[3]);
2776 handler_5 = cmd->handler;
2777 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2780 handler_6 = cmd->handler;
2781 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2784 handler_7 = cmd->handler;
2785 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2789 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2793 for(i = 0; i < MAX_ARGS; i++)
2794 qemu_free(str_allocated[i]);
2798 static void cmd_completion(const char *name, const char *list)
2800 const char *p, *pstart;
2809 p = pstart + strlen(pstart);
2811 if (len > sizeof(cmd) - 2)
2812 len = sizeof(cmd) - 2;
2813 memcpy(cmd, pstart, len);
2815 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2816 readline_add_completion(cur_mon->rs, cmd);
2824 static void file_completion(const char *input)
2829 char file[1024], file_prefix[1024];
2833 p = strrchr(input, '/');
2836 pstrcpy(file_prefix, sizeof(file_prefix), input);
2837 pstrcpy(path, sizeof(path), ".");
2839 input_path_len = p - input + 1;
2840 memcpy(path, input, input_path_len);
2841 if (input_path_len > sizeof(path) - 1)
2842 input_path_len = sizeof(path) - 1;
2843 path[input_path_len] = '\0';
2844 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2846 #ifdef DEBUG_COMPLETION
2847 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2848 input, path, file_prefix);
2850 ffs = opendir(path);
2858 if (strstart(d->d_name, file_prefix, NULL)) {
2859 memcpy(file, input, input_path_len);
2860 if (input_path_len < sizeof(file))
2861 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2863 /* stat the file to find out if it's a directory.
2864 * In that case add a slash to speed up typing long paths
2867 if(S_ISDIR(sb.st_mode))
2868 pstrcat(file, sizeof(file), "/");
2869 readline_add_completion(cur_mon->rs, file);
2875 static void block_completion_it(void *opaque, BlockDriverState *bs)
2877 const char *name = bdrv_get_device_name(bs);
2878 const char *input = opaque;
2880 if (input[0] == '\0' ||
2881 !strncmp(name, (char *)input, strlen(input))) {
2882 readline_add_completion(cur_mon->rs, name);
2886 /* NOTE: this parser is an approximate form of the real command parser */
2887 static void parse_cmdline(const char *cmdline,
2888 int *pnb_args, char **args)
2897 while (qemu_isspace(*p))
2901 if (nb_args >= MAX_ARGS)
2903 ret = get_str(buf, sizeof(buf), &p);
2904 args[nb_args] = qemu_strdup(buf);
2909 *pnb_args = nb_args;
2912 static void monitor_find_completion(const char *cmdline)
2914 const char *cmdname;
2915 char *args[MAX_ARGS];
2916 int nb_args, i, len;
2917 const char *ptype, *str;
2918 const mon_cmd_t *cmd;
2921 parse_cmdline(cmdline, &nb_args, args);
2922 #ifdef DEBUG_COMPLETION
2923 for(i = 0; i < nb_args; i++) {
2924 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2928 /* if the line ends with a space, it means we want to complete the
2930 len = strlen(cmdline);
2931 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2932 if (nb_args >= MAX_ARGS)
2934 args[nb_args++] = qemu_strdup("");
2937 /* command completion */
2942 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2943 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2944 cmd_completion(cmdname, cmd->name);
2947 /* find the command */
2948 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2949 if (compare_cmd(args[0], cmd->name))
2954 ptype = cmd->args_type;
2955 for(i = 0; i < nb_args - 2; i++) {
2956 if (*ptype != '\0') {
2958 while (*ptype == '?')
2962 str = args[nb_args - 1];
2965 /* file completion */
2966 readline_set_completion_index(cur_mon->rs, strlen(str));
2967 file_completion(str);
2970 /* block device name completion */
2971 readline_set_completion_index(cur_mon->rs, strlen(str));
2972 bdrv_iterate(block_completion_it, (void *)str);
2975 /* XXX: more generic ? */
2976 if (!strcmp(cmd->name, "info")) {
2977 readline_set_completion_index(cur_mon->rs, strlen(str));
2978 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2979 cmd_completion(str, cmd->name);
2981 } else if (!strcmp(cmd->name, "sendkey")) {
2982 char *sep = strrchr(str, '-');
2985 readline_set_completion_index(cur_mon->rs, strlen(str));
2986 for(key = key_defs; key->name != NULL; key++) {
2987 cmd_completion(str, key->name);
2995 for(i = 0; i < nb_args; i++)
2999 static int monitor_can_read(void *opaque)
3001 Monitor *mon = opaque;
3003 return (mon->suspend_cnt == 0) ? 128 : 0;
3006 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3008 Monitor *old_mon = cur_mon;
3014 for (i = 0; i < size; i++)
3015 readline_handle_byte(cur_mon->rs, buf[i]);
3017 if (size == 0 || buf[size - 1] != 0)
3018 monitor_printf(cur_mon, "corrupted command\n");
3020 monitor_handle_command(cur_mon, (char *)buf);
3026 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3028 monitor_suspend(mon);
3029 monitor_handle_command(mon, cmdline);
3030 monitor_resume(mon);
3033 int monitor_suspend(Monitor *mon)
3041 void monitor_resume(Monitor *mon)
3045 if (--mon->suspend_cnt == 0)
3046 readline_show_prompt(mon->rs);
3049 static void monitor_event(void *opaque, int event)
3051 Monitor *mon = opaque;
3054 case CHR_EVENT_MUX_IN:
3055 readline_restart(mon->rs);
3056 monitor_resume(mon);
3060 case CHR_EVENT_MUX_OUT:
3061 if (mon->suspend_cnt == 0)
3062 monitor_printf(mon, "\n");
3064 monitor_suspend(mon);
3067 case CHR_EVENT_RESET:
3068 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3069 "information\n", QEMU_VERSION);
3070 if (mon->chr->focus == 0)
3071 readline_show_prompt(mon->rs);
3085 void monitor_init(CharDriverState *chr, int flags)
3087 static int is_first_init = 1;
3090 if (is_first_init) {
3091 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3095 mon = qemu_mallocz(sizeof(*mon));
3099 if (mon->chr->focus != 0)
3100 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3101 if (flags & MONITOR_USE_READLINE) {
3102 mon->rs = readline_init(mon, monitor_find_completion);
3103 monitor_read_command(mon, 0);
3106 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3109 LIST_INSERT_HEAD(&mon_list, mon, entry);
3110 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3114 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3116 BlockDriverState *bs = opaque;
3119 if (bdrv_set_key(bs, password) != 0) {
3120 monitor_printf(mon, "invalid password\n");
3123 if (mon->password_completion_cb)
3124 mon->password_completion_cb(mon->password_opaque, ret);
3126 monitor_read_command(mon, 1);
3129 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3130 BlockDriverCompletionFunc *completion_cb,
3135 if (!bdrv_key_required(bs)) {
3137 completion_cb(opaque, 0);
3141 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3142 bdrv_get_encrypted_filename(bs));
3144 mon->password_completion_cb = completion_cb;
3145 mon->password_opaque = opaque;
3147 err = monitor_read_password(mon, bdrv_password_cb, bs);
3149 if (err && completion_cb)
3150 completion_cb(opaque, err);