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);
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 return cur_mon->mon_cpu;
324 static void do_info_registers(Monitor *mon)
331 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
334 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
339 static void do_info_cpus(Monitor *mon)
343 /* just to set the default cpu if not already done */
346 for(env = first_cpu; env != NULL; env = env->next_cpu) {
347 monitor_printf(mon, "%c CPU #%d:",
348 (env == mon->mon_cpu) ? '*' : ' ',
350 #if defined(TARGET_I386)
351 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
352 env->eip + env->segs[R_CS].base);
353 #elif defined(TARGET_PPC)
354 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
355 #elif defined(TARGET_SPARC)
356 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
358 #elif defined(TARGET_MIPS)
359 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
362 monitor_printf(mon, " (halted)");
363 monitor_printf(mon, "\n");
367 static void do_cpu_set(Monitor *mon, int index)
369 if (mon_set_cpu(index) < 0)
370 monitor_printf(mon, "Invalid CPU index\n");
373 static void do_info_jit(Monitor *mon)
375 dump_exec_info((FILE *)mon, monitor_fprintf);
378 static void do_info_history(Monitor *mon)
387 str = readline_get_history(mon->rs, i);
390 monitor_printf(mon, "%d: '%s'\n", i, str);
395 #if defined(TARGET_PPC)
396 /* XXX: not implemented in other targets */
397 static void do_info_cpu_stats(Monitor *mon)
402 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
406 static void do_quit(Monitor *mon)
411 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
413 if (bdrv_is_inserted(bs)) {
415 if (!bdrv_is_removable(bs)) {
416 monitor_printf(mon, "device is not removable\n");
419 if (bdrv_is_locked(bs)) {
420 monitor_printf(mon, "device is locked\n");
429 static void do_eject(Monitor *mon, int force, const char *filename)
431 BlockDriverState *bs;
433 bs = bdrv_find(filename);
435 monitor_printf(mon, "device not found\n");
438 eject_device(mon, bs, force);
441 static void do_change_block(Monitor *mon, const char *device,
442 const char *filename, const char *fmt)
444 BlockDriverState *bs;
445 BlockDriver *drv = NULL;
447 bs = bdrv_find(device);
449 monitor_printf(mon, "device not found\n");
453 drv = bdrv_find_format(fmt);
455 monitor_printf(mon, "invalid format %s\n", fmt);
459 if (eject_device(mon, bs, 0) < 0)
461 bdrv_open2(bs, filename, 0, drv);
462 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
465 static void change_vnc_password_cb(Monitor *mon, const char *password,
468 if (vnc_display_password(NULL, password) < 0)
469 monitor_printf(mon, "could not set VNC server password\n");
471 monitor_read_command(mon, 1);
474 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
476 if (strcmp(target, "passwd") == 0 ||
477 strcmp(target, "password") == 0) {
480 strncpy(password, arg, sizeof(password));
481 password[sizeof(password) - 1] = '\0';
482 change_vnc_password_cb(mon, password, NULL);
484 monitor_read_password(mon, change_vnc_password_cb, NULL);
487 if (vnc_display_open(NULL, target) < 0)
488 monitor_printf(mon, "could not start VNC server on %s\n", target);
492 static void do_change(Monitor *mon, const char *device, const char *target,
495 if (strcmp(device, "vnc") == 0) {
496 do_change_vnc(mon, target, arg);
498 do_change_block(mon, device, target, arg);
502 static void do_screen_dump(Monitor *mon, const char *filename)
504 vga_hw_screen_dump(filename);
507 static void do_logfile(Monitor *mon, const char *filename)
509 cpu_set_log_filename(filename);
512 static void do_log(Monitor *mon, const char *items)
516 if (!strcmp(items, "none")) {
519 mask = cpu_str_to_log_mask(items);
521 help_cmd(mon, "log");
528 static void do_stop(Monitor *mon)
530 vm_stop(EXCP_INTERRUPT);
533 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
535 struct bdrv_iterate_context {
540 static void do_cont(Monitor *mon)
542 struct bdrv_iterate_context context = { mon, 0 };
544 bdrv_iterate(encrypted_bdrv_it, &context);
545 /* only resume the vm if all keys are set and valid */
550 static void bdrv_key_cb(void *opaque, int err)
552 Monitor *mon = opaque;
554 /* another key was set successfully, retry to continue */
559 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
561 struct bdrv_iterate_context *context = opaque;
563 if (!context->err && bdrv_key_required(bs)) {
564 context->err = -EBUSY;
565 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
570 #ifdef CONFIG_GDBSTUB
571 static void do_gdbserver(Monitor *mon, const char *port)
574 port = DEFAULT_GDBSTUB_PORT;
575 if (gdbserver_start(port) < 0) {
576 monitor_printf(mon, "Could not open gdbserver socket on port '%s'\n",
579 monitor_printf(mon, "Waiting gdb connection on port '%s'\n", port);
584 static void monitor_printc(Monitor *mon, int c)
586 monitor_printf(mon, "'");
589 monitor_printf(mon, "\\'");
592 monitor_printf(mon, "\\\\");
595 monitor_printf(mon, "\\n");
598 monitor_printf(mon, "\\r");
601 if (c >= 32 && c <= 126) {
602 monitor_printf(mon, "%c", c);
604 monitor_printf(mon, "\\x%02x", c);
608 monitor_printf(mon, "'");
611 static void memory_dump(Monitor *mon, int count, int format, int wsize,
612 target_phys_addr_t addr, int is_physical)
615 int nb_per_line, l, line_size, i, max_digits, len;
623 if (!env && !is_physical)
628 } else if (wsize == 4) {
631 /* as default we use the current CS size */
635 if ((env->efer & MSR_EFER_LMA) &&
636 (env->segs[R_CS].flags & DESC_L_MASK))
640 if (!(env->segs[R_CS].flags & DESC_B_MASK))
645 monitor_disas(mon, env, addr, count, is_physical, flags);
654 nb_per_line = line_size / wsize;
659 max_digits = (wsize * 8 + 2) / 3;
663 max_digits = (wsize * 8) / 4;
667 max_digits = (wsize * 8 * 10 + 32) / 33;
676 monitor_printf(mon, TARGET_FMT_plx ":", addr);
678 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
683 cpu_physical_memory_rw(addr, buf, l, 0);
688 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
689 monitor_printf(mon, " Cannot access memory\n");
698 v = ldub_raw(buf + i);
701 v = lduw_raw(buf + i);
704 v = (uint32_t)ldl_raw(buf + i);
707 v = ldq_raw(buf + i);
710 monitor_printf(mon, " ");
713 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
716 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
719 monitor_printf(mon, "%*" PRIu64, max_digits, v);
722 monitor_printf(mon, "%*" PRId64, max_digits, v);
725 monitor_printc(mon, v);
730 monitor_printf(mon, "\n");
736 #if TARGET_LONG_BITS == 64
737 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
739 #define GET_TLONG(h, l) (l)
742 static void do_memory_dump(Monitor *mon, int count, int format, int size,
743 uint32_t addrh, uint32_t addrl)
745 target_long addr = GET_TLONG(addrh, addrl);
746 memory_dump(mon, count, format, size, addr, 0);
749 #if TARGET_PHYS_ADDR_BITS > 32
750 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
752 #define GET_TPHYSADDR(h, l) (l)
755 static void do_physical_memory_dump(Monitor *mon, int count, int format,
756 int size, uint32_t addrh, uint32_t addrl)
759 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
760 memory_dump(mon, count, format, size, addr, 1);
763 static void do_print(Monitor *mon, int count, int format, int size,
764 unsigned int valh, unsigned int vall)
766 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
767 #if TARGET_PHYS_ADDR_BITS == 32
770 monitor_printf(mon, "%#o", val);
773 monitor_printf(mon, "%#x", val);
776 monitor_printf(mon, "%u", val);
780 monitor_printf(mon, "%d", val);
783 monitor_printc(mon, val);
789 monitor_printf(mon, "%#" PRIo64, val);
792 monitor_printf(mon, "%#" PRIx64, val);
795 monitor_printf(mon, "%" PRIu64, val);
799 monitor_printf(mon, "%" PRId64, val);
802 monitor_printc(mon, val);
806 monitor_printf(mon, "\n");
809 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
810 uint32_t size, const char *filename)
813 target_long addr = GET_TLONG(valh, vall);
822 f = fopen(filename, "wb");
824 monitor_printf(mon, "could not open '%s'\n", filename);
831 cpu_memory_rw_debug(env, addr, buf, l, 0);
832 fwrite(buf, 1, l, f);
839 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
840 unsigned int vall, uint32_t size,
841 const char *filename)
846 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
848 f = fopen(filename, "wb");
850 monitor_printf(mon, "could not open '%s'\n", filename);
857 cpu_physical_memory_rw(addr, buf, l, 0);
858 fwrite(buf, 1, l, f);
866 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
873 for(addr = start; addr < (start + size); addr++) {
874 cpu_physical_memory_rw(addr, buf, 1, 0);
875 /* BSD sum algorithm ('sum' Unix command) */
876 sum = (sum >> 1) | (sum << 15);
879 monitor_printf(mon, "%05d\n", sum);
887 static const KeyDef key_defs[] = {
914 { 0x0e, "backspace" },
951 { 0x37, "asterisk" },
954 { 0x3a, "caps_lock" },
965 { 0x45, "num_lock" },
966 { 0x46, "scroll_lock" },
968 { 0xb5, "kp_divide" },
969 { 0x37, "kp_multiply" },
970 { 0x4a, "kp_subtract" },
972 { 0x9c, "kp_enter" },
973 { 0x53, "kp_decimal" },
1006 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1021 { 0xfe, "compose" },
1026 static int get_keycode(const char *key)
1032 for(p = key_defs; p->name != NULL; p++) {
1033 if (!strcmp(key, p->name))
1036 if (strstart(key, "0x", NULL)) {
1037 ret = strtoul(key, &endp, 0);
1038 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1044 #define MAX_KEYCODES 16
1045 static uint8_t keycodes[MAX_KEYCODES];
1046 static int nb_pending_keycodes;
1047 static QEMUTimer *key_timer;
1049 static void release_keys(void *opaque)
1053 while (nb_pending_keycodes > 0) {
1054 nb_pending_keycodes--;
1055 keycode = keycodes[nb_pending_keycodes];
1057 kbd_put_keycode(0xe0);
1058 kbd_put_keycode(keycode | 0x80);
1062 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1065 char keyname_buf[16];
1067 int keyname_len, keycode, i;
1069 if (nb_pending_keycodes > 0) {
1070 qemu_del_timer(key_timer);
1077 separator = strchr(string, '-');
1078 keyname_len = separator ? separator - string : strlen(string);
1079 if (keyname_len > 0) {
1080 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1081 if (keyname_len > sizeof(keyname_buf) - 1) {
1082 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1085 if (i == MAX_KEYCODES) {
1086 monitor_printf(mon, "too many keys\n");
1089 keyname_buf[keyname_len] = 0;
1090 keycode = get_keycode(keyname_buf);
1092 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1095 keycodes[i++] = keycode;
1099 string = separator + 1;
1101 nb_pending_keycodes = i;
1102 /* key down events */
1103 for (i = 0; i < nb_pending_keycodes; i++) {
1104 keycode = keycodes[i];
1106 kbd_put_keycode(0xe0);
1107 kbd_put_keycode(keycode & 0x7f);
1109 /* delayed key up events */
1110 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1111 muldiv64(ticks_per_sec, hold_time, 1000));
1114 static int mouse_button_state;
1116 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1120 dx = strtol(dx_str, NULL, 0);
1121 dy = strtol(dy_str, NULL, 0);
1124 dz = strtol(dz_str, NULL, 0);
1125 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1128 static void do_mouse_button(Monitor *mon, int button_state)
1130 mouse_button_state = button_state;
1131 kbd_mouse_event(0, 0, 0, mouse_button_state);
1134 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1135 int addr, int has_index, int index)
1141 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1149 val = cpu_inb(NULL, addr);
1153 val = cpu_inw(NULL, addr);
1157 val = cpu_inl(NULL, addr);
1161 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1162 suffix, addr, size * 2, val);
1165 /* boot_set handler */
1166 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1167 static void *boot_opaque;
1169 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1171 qemu_boot_set_handler = func;
1172 boot_opaque = opaque;
1175 static void do_boot_set(Monitor *mon, const char *bootdevice)
1179 if (qemu_boot_set_handler) {
1180 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1182 monitor_printf(mon, "boot device list now set to %s\n",
1185 monitor_printf(mon, "setting boot device list failed with "
1188 monitor_printf(mon, "no function defined to set boot device list for "
1189 "this architecture\n");
1193 static void do_system_reset(Monitor *mon)
1195 qemu_system_reset_request();
1198 static void do_system_powerdown(Monitor *mon)
1200 qemu_system_powerdown_request();
1203 #if defined(TARGET_I386)
1204 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1206 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1209 pte & PG_GLOBAL_MASK ? 'G' : '-',
1210 pte & PG_PSE_MASK ? 'P' : '-',
1211 pte & PG_DIRTY_MASK ? 'D' : '-',
1212 pte & PG_ACCESSED_MASK ? 'A' : '-',
1213 pte & PG_PCD_MASK ? 'C' : '-',
1214 pte & PG_PWT_MASK ? 'T' : '-',
1215 pte & PG_USER_MASK ? 'U' : '-',
1216 pte & PG_RW_MASK ? 'W' : '-');
1219 static void tlb_info(Monitor *mon)
1223 uint32_t pgd, pde, pte;
1225 env = mon_get_cpu();
1229 if (!(env->cr[0] & CR0_PG_MASK)) {
1230 monitor_printf(mon, "PG disabled\n");
1233 pgd = env->cr[3] & ~0xfff;
1234 for(l1 = 0; l1 < 1024; l1++) {
1235 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1236 pde = le32_to_cpu(pde);
1237 if (pde & PG_PRESENT_MASK) {
1238 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1239 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1241 for(l2 = 0; l2 < 1024; l2++) {
1242 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1243 (uint8_t *)&pte, 4);
1244 pte = le32_to_cpu(pte);
1245 if (pte & PG_PRESENT_MASK) {
1246 print_pte(mon, (l1 << 22) + (l2 << 12),
1256 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1257 uint32_t end, int prot)
1260 prot1 = *plast_prot;
1261 if (prot != prot1) {
1262 if (*pstart != -1) {
1263 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1264 *pstart, end, end - *pstart,
1265 prot1 & PG_USER_MASK ? 'u' : '-',
1267 prot1 & PG_RW_MASK ? 'w' : '-');
1277 static void mem_info(Monitor *mon)
1280 int l1, l2, prot, last_prot;
1281 uint32_t pgd, pde, pte, start, end;
1283 env = mon_get_cpu();
1287 if (!(env->cr[0] & CR0_PG_MASK)) {
1288 monitor_printf(mon, "PG disabled\n");
1291 pgd = env->cr[3] & ~0xfff;
1294 for(l1 = 0; l1 < 1024; l1++) {
1295 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1296 pde = le32_to_cpu(pde);
1298 if (pde & PG_PRESENT_MASK) {
1299 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1300 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1301 mem_print(mon, &start, &last_prot, end, prot);
1303 for(l2 = 0; l2 < 1024; l2++) {
1304 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1305 (uint8_t *)&pte, 4);
1306 pte = le32_to_cpu(pte);
1307 end = (l1 << 22) + (l2 << 12);
1308 if (pte & PG_PRESENT_MASK) {
1309 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1313 mem_print(mon, &start, &last_prot, end, prot);
1318 mem_print(mon, &start, &last_prot, end, prot);
1324 #if defined(TARGET_SH4)
1326 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1328 monitor_printf(mon, " tlb%i:\t"
1329 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1330 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1331 "dirty=%hhu writethrough=%hhu\n",
1333 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1334 tlb->v, tlb->sh, tlb->c, tlb->pr,
1338 static void tlb_info(Monitor *mon)
1340 CPUState *env = mon_get_cpu();
1343 monitor_printf (mon, "ITLB:\n");
1344 for (i = 0 ; i < ITLB_SIZE ; i++)
1345 print_tlb (mon, i, &env->itlb[i]);
1346 monitor_printf (mon, "UTLB:\n");
1347 for (i = 0 ; i < UTLB_SIZE ; i++)
1348 print_tlb (mon, i, &env->utlb[i]);
1353 static void do_info_kqemu(Monitor *mon)
1359 env = mon_get_cpu();
1361 monitor_printf(mon, "No cpu initialized yet");
1364 val = env->kqemu_enabled;
1365 monitor_printf(mon, "kqemu support: ");
1369 monitor_printf(mon, "disabled\n");
1372 monitor_printf(mon, "enabled for user code\n");
1375 monitor_printf(mon, "enabled for user and kernel code\n");
1379 monitor_printf(mon, "kqemu support: not compiled\n");
1383 static void do_info_kvm(Monitor *mon)
1386 monitor_printf(mon, "kvm support: ");
1388 monitor_printf(mon, "enabled\n");
1390 monitor_printf(mon, "disabled\n");
1392 monitor_printf(mon, "kvm support: not compiled\n");
1396 #ifdef CONFIG_PROFILER
1400 int64_t kqemu_exec_count;
1402 int64_t kqemu_ret_int_count;
1403 int64_t kqemu_ret_excp_count;
1404 int64_t kqemu_ret_intr_count;
1406 static void do_info_profile(Monitor *mon)
1412 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1413 dev_time, dev_time / (double)ticks_per_sec);
1414 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1415 qemu_time, qemu_time / (double)ticks_per_sec);
1416 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1417 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1419 kqemu_time, kqemu_time / (double)ticks_per_sec,
1420 kqemu_time / (double)total * 100.0,
1422 kqemu_ret_int_count,
1423 kqemu_ret_excp_count,
1424 kqemu_ret_intr_count);
1427 kqemu_exec_count = 0;
1429 kqemu_ret_int_count = 0;
1430 kqemu_ret_excp_count = 0;
1431 kqemu_ret_intr_count = 0;
1433 kqemu_record_dump();
1437 static void do_info_profile(Monitor *mon)
1439 monitor_printf(mon, "Internal profiler not compiled\n");
1443 /* Capture support */
1444 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1446 static void do_info_capture(Monitor *mon)
1451 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1452 monitor_printf(mon, "[%d]: ", i);
1453 s->ops.info (s->opaque);
1457 static void do_stop_capture(Monitor *mon, int n)
1462 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1464 s->ops.destroy (s->opaque);
1465 LIST_REMOVE (s, entries);
1473 static void do_wav_capture(Monitor *mon, const char *path,
1474 int has_freq, int freq,
1475 int has_bits, int bits,
1476 int has_channels, int nchannels)
1480 s = qemu_mallocz (sizeof (*s));
1482 freq = has_freq ? freq : 44100;
1483 bits = has_bits ? bits : 16;
1484 nchannels = has_channels ? nchannels : 2;
1486 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1487 monitor_printf(mon, "Faied to add wave capture\n");
1490 LIST_INSERT_HEAD (&capture_head, s, entries);
1494 #if defined(TARGET_I386)
1495 static void do_inject_nmi(Monitor *mon, int cpu_index)
1499 for (env = first_cpu; env != NULL; env = env->next_cpu)
1500 if (env->cpu_index == cpu_index) {
1501 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1507 static void do_info_status(Monitor *mon)
1510 monitor_printf(mon, "VM status: running\n");
1512 monitor_printf(mon, "VM status: paused\n");
1516 static void do_balloon(Monitor *mon, int value)
1518 ram_addr_t target = value;
1519 qemu_balloon(target << 20);
1522 static void do_info_balloon(Monitor *mon)
1526 actual = qemu_balloon_status();
1527 if (kvm_enabled() && !kvm_has_sync_mmu())
1528 monitor_printf(mon, "Using KVM without synchronous MMU, "
1529 "ballooning disabled\n");
1530 else if (actual == 0)
1531 monitor_printf(mon, "Ballooning not activated in VM\n");
1533 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1536 static void do_acl(Monitor *mon,
1537 const char *command,
1538 const char *aclname,
1545 acl = qemu_acl_find(aclname);
1547 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1551 if (strcmp(command, "show") == 0) {
1553 qemu_acl_entry *entry;
1554 monitor_printf(mon, "policy: %s\n",
1555 acl->defaultDeny ? "deny" : "allow");
1556 TAILQ_FOREACH(entry, &acl->entries, next) {
1558 monitor_printf(mon, "%d: %s %s\n", i,
1559 entry->deny ? "deny" : "allow",
1562 } else if (strcmp(command, "reset") == 0) {
1563 qemu_acl_reset(acl);
1564 monitor_printf(mon, "acl: removed all rules\n");
1565 } else if (strcmp(command, "policy") == 0) {
1567 monitor_printf(mon, "acl: missing policy parameter\n");
1571 if (strcmp(match, "allow") == 0) {
1572 acl->defaultDeny = 0;
1573 monitor_printf(mon, "acl: policy set to 'allow'\n");
1574 } else if (strcmp(match, "deny") == 0) {
1575 acl->defaultDeny = 1;
1576 monitor_printf(mon, "acl: policy set to 'deny'\n");
1578 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1580 } else if ((strcmp(command, "allow") == 0) ||
1581 (strcmp(command, "deny") == 0)) {
1582 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1586 monitor_printf(mon, "acl: missing match parameter\n");
1591 ret = qemu_acl_insert(acl, deny, match, index);
1593 ret = qemu_acl_append(acl, deny, match);
1595 monitor_printf(mon, "acl: unable to add acl entry\n");
1597 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1598 } else if (strcmp(command, "remove") == 0) {
1602 monitor_printf(mon, "acl: missing match parameter\n");
1606 ret = qemu_acl_remove(acl, match);
1608 monitor_printf(mon, "acl: no matching acl entry\n");
1610 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1612 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1616 /* Please update qemu-doc.texi when adding or changing commands */
1617 static const mon_cmd_t mon_cmds[] = {
1618 { "help|?", "s?", help_cmd,
1619 "[cmd]", "show the help" },
1620 { "commit", "s", do_commit,
1621 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1622 { "info", "s?", do_info,
1623 "subcommand", "show various information about the system state" },
1624 { "q|quit", "", do_quit,
1625 "", "quit the emulator" },
1626 { "eject", "-fB", do_eject,
1627 "[-f] device", "eject a removable medium (use -f to force it)" },
1628 { "change", "BFs?", do_change,
1629 "device filename [format]", "change a removable medium, optional format" },
1630 { "screendump", "F", do_screen_dump,
1631 "filename", "save screen into PPM image 'filename'" },
1632 { "logfile", "F", do_logfile,
1633 "filename", "output logs to 'filename'" },
1634 { "log", "s", do_log,
1635 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1636 { "savevm", "s?", do_savevm,
1637 "tag|id", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1638 { "loadvm", "s", do_loadvm,
1639 "tag|id", "restore a VM snapshot from its tag or id" },
1640 { "delvm", "s", do_delvm,
1641 "tag|id", "delete a VM snapshot from its tag or id" },
1642 { "stop", "", do_stop,
1643 "", "stop emulation", },
1644 { "c|cont", "", do_cont,
1645 "", "resume emulation", },
1646 #ifdef CONFIG_GDBSTUB
1647 { "gdbserver", "s?", do_gdbserver,
1648 "[port]", "start gdbserver session (default port=1234)", },
1650 { "x", "/l", do_memory_dump,
1651 "/fmt addr", "virtual memory dump starting at 'addr'", },
1652 { "xp", "/l", do_physical_memory_dump,
1653 "/fmt addr", "physical memory dump starting at 'addr'", },
1654 { "p|print", "/l", do_print,
1655 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1656 { "i", "/ii.", do_ioport_read,
1657 "/fmt addr", "I/O port read" },
1659 { "sendkey", "si?", do_sendkey,
1660 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1661 { "system_reset", "", do_system_reset,
1662 "", "reset the system" },
1663 { "system_powerdown", "", do_system_powerdown,
1664 "", "send system power down event" },
1665 { "sum", "ii", do_sum,
1666 "addr size", "compute the checksum of a memory region" },
1667 { "usb_add", "s", do_usb_add,
1668 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1669 { "usb_del", "s", do_usb_del,
1670 "device", "remove USB device 'bus.addr'" },
1671 { "cpu", "i", do_cpu_set,
1672 "index", "set the default CPU" },
1673 { "mouse_move", "sss?", do_mouse_move,
1674 "dx dy [dz]", "send mouse move events" },
1675 { "mouse_button", "i", do_mouse_button,
1676 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1677 { "mouse_set", "i", do_mouse_set,
1678 "index", "set which mouse device receives events" },
1680 { "wavcapture", "si?i?i?", do_wav_capture,
1681 "path [frequency bits channels]",
1682 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1684 { "stopcapture", "i", do_stop_capture,
1685 "capture index", "stop capture" },
1686 { "memsave", "lis", do_memory_save,
1687 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1688 { "pmemsave", "lis", do_physical_memory_save,
1689 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1690 { "boot_set", "s", do_boot_set,
1691 "bootdevice", "define new values for the boot device list" },
1692 #if defined(TARGET_I386)
1693 { "nmi", "i", do_inject_nmi,
1694 "cpu", "inject an NMI on the given CPU", },
1696 { "migrate", "-ds", do_migrate,
1697 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1698 { "migrate_cancel", "", do_migrate_cancel,
1699 "", "cancel the current VM migration" },
1700 { "migrate_set_speed", "s", do_migrate_set_speed,
1701 "value", "set maximum speed (in bytes) for migrations" },
1702 #if defined(TARGET_I386)
1703 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1704 "[file=file][,if=type][,bus=n]\n"
1705 "[,unit=m][,media=d][index=i]\n"
1706 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1707 "[snapshot=on|off][,cache=on|off]",
1708 "add drive to PCI storage controller" },
1709 { "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" },
1710 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1711 { "host_net_add", "ss", net_host_device_add,
1712 "[tap,user,socket,vde] options", "add host VLAN client" },
1713 { "host_net_remove", "is", net_host_device_remove,
1714 "vlan_id name", "remove host VLAN client" },
1716 { "balloon", "i", do_balloon,
1717 "target", "request VM to change it's memory allocation (in MB)" },
1718 { "set_link", "ss", do_set_link,
1719 "name [up|down]", "change the link status of a network adapter" },
1720 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match>] [<index>]\n",
1721 "acl show vnc.username\n"
1722 "acl policy vnc.username deny\n"
1723 "acl allow vnc.username fred\n"
1724 "acl deny vnc.username bob\n"
1725 "acl reset vnc.username\n" },
1729 /* Please update qemu-doc.texi when adding or changing commands */
1730 static const mon_cmd_t info_cmds[] = {
1731 { "version", "", do_info_version,
1732 "", "show the version of QEMU" },
1733 { "network", "", do_info_network,
1734 "", "show the network state" },
1735 { "chardev", "", qemu_chr_info,
1736 "", "show the character devices" },
1737 { "block", "", bdrv_info,
1738 "", "show the block devices" },
1739 { "blockstats", "", bdrv_info_stats,
1740 "", "show block device statistics" },
1741 { "registers", "", do_info_registers,
1742 "", "show the cpu registers" },
1743 { "cpus", "", do_info_cpus,
1744 "", "show infos for each CPU" },
1745 { "history", "", do_info_history,
1746 "", "show the command line history", },
1747 { "irq", "", irq_info,
1748 "", "show the interrupts statistics (if available)", },
1749 { "pic", "", pic_info,
1750 "", "show i8259 (PIC) state", },
1751 { "pci", "", pci_info,
1752 "", "show PCI info", },
1753 #if defined(TARGET_I386) || defined(TARGET_SH4)
1754 { "tlb", "", tlb_info,
1755 "", "show virtual to physical memory mappings", },
1757 #if defined(TARGET_I386)
1758 { "mem", "", mem_info,
1759 "", "show the active virtual memory mappings", },
1760 { "hpet", "", do_info_hpet,
1761 "", "show state of HPET", },
1763 { "jit", "", do_info_jit,
1764 "", "show dynamic compiler info", },
1765 { "kqemu", "", do_info_kqemu,
1766 "", "show KQEMU information", },
1767 { "kvm", "", do_info_kvm,
1768 "", "show KVM information", },
1769 { "usb", "", usb_info,
1770 "", "show guest USB devices", },
1771 { "usbhost", "", usb_host_info,
1772 "", "show host USB devices", },
1773 { "profile", "", do_info_profile,
1774 "", "show profiling information", },
1775 { "capture", "", do_info_capture,
1776 "", "show capture information" },
1777 { "snapshots", "", do_info_snapshots,
1778 "", "show the currently saved VM snapshots" },
1779 { "status", "", do_info_status,
1780 "", "show the current VM status (running|paused)" },
1781 { "pcmcia", "", pcmcia_info,
1782 "", "show guest PCMCIA status" },
1783 { "mice", "", do_info_mice,
1784 "", "show which guest mouse is receiving events" },
1785 { "vnc", "", do_info_vnc,
1786 "", "show the vnc server status"},
1787 { "name", "", do_info_name,
1788 "", "show the current VM name" },
1789 { "uuid", "", do_info_uuid,
1790 "", "show the current VM UUID" },
1791 #if defined(TARGET_PPC)
1792 { "cpustats", "", do_info_cpu_stats,
1793 "", "show CPU statistics", },
1795 #if defined(CONFIG_SLIRP)
1796 { "slirp", "", do_info_slirp,
1797 "", "show SLIRP statistics", },
1799 { "migrate", "", do_info_migrate, "", "show migration status" },
1800 { "balloon", "", do_info_balloon,
1801 "", "show balloon information" },
1805 /*******************************************************************/
1807 static const char *pch;
1808 static jmp_buf expr_env;
1813 typedef struct MonitorDef {
1816 target_long (*get_value)(const struct MonitorDef *md, int val);
1820 #if defined(TARGET_I386)
1821 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1823 CPUState *env = mon_get_cpu();
1826 return env->eip + env->segs[R_CS].base;
1830 #if defined(TARGET_PPC)
1831 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1833 CPUState *env = mon_get_cpu();
1841 for (i = 0; i < 8; i++)
1842 u |= env->crf[i] << (32 - (4 * i));
1847 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1849 CPUState *env = mon_get_cpu();
1855 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1857 CPUState *env = mon_get_cpu();
1863 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1865 CPUState *env = mon_get_cpu();
1868 return cpu_ppc_load_decr(env);
1871 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1873 CPUState *env = mon_get_cpu();
1876 return cpu_ppc_load_tbu(env);
1879 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1881 CPUState *env = mon_get_cpu();
1884 return cpu_ppc_load_tbl(env);
1888 #if defined(TARGET_SPARC)
1889 #ifndef TARGET_SPARC64
1890 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1892 CPUState *env = mon_get_cpu();
1895 return GET_PSR(env);
1899 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1901 CPUState *env = mon_get_cpu();
1904 return env->regwptr[val];
1908 static const MonitorDef monitor_defs[] = {
1911 #define SEG(name, seg) \
1912 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1913 { name ".base", offsetof(CPUState, segs[seg].base) },\
1914 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1916 { "eax", offsetof(CPUState, regs[0]) },
1917 { "ecx", offsetof(CPUState, regs[1]) },
1918 { "edx", offsetof(CPUState, regs[2]) },
1919 { "ebx", offsetof(CPUState, regs[3]) },
1920 { "esp|sp", offsetof(CPUState, regs[4]) },
1921 { "ebp|fp", offsetof(CPUState, regs[5]) },
1922 { "esi", offsetof(CPUState, regs[6]) },
1923 { "edi", offsetof(CPUState, regs[7]) },
1924 #ifdef TARGET_X86_64
1925 { "r8", offsetof(CPUState, regs[8]) },
1926 { "r9", offsetof(CPUState, regs[9]) },
1927 { "r10", offsetof(CPUState, regs[10]) },
1928 { "r11", offsetof(CPUState, regs[11]) },
1929 { "r12", offsetof(CPUState, regs[12]) },
1930 { "r13", offsetof(CPUState, regs[13]) },
1931 { "r14", offsetof(CPUState, regs[14]) },
1932 { "r15", offsetof(CPUState, regs[15]) },
1934 { "eflags", offsetof(CPUState, eflags) },
1935 { "eip", offsetof(CPUState, eip) },
1942 { "pc", 0, monitor_get_pc, },
1943 #elif defined(TARGET_PPC)
1944 /* General purpose registers */
1945 { "r0", offsetof(CPUState, gpr[0]) },
1946 { "r1", offsetof(CPUState, gpr[1]) },
1947 { "r2", offsetof(CPUState, gpr[2]) },
1948 { "r3", offsetof(CPUState, gpr[3]) },
1949 { "r4", offsetof(CPUState, gpr[4]) },
1950 { "r5", offsetof(CPUState, gpr[5]) },
1951 { "r6", offsetof(CPUState, gpr[6]) },
1952 { "r7", offsetof(CPUState, gpr[7]) },
1953 { "r8", offsetof(CPUState, gpr[8]) },
1954 { "r9", offsetof(CPUState, gpr[9]) },
1955 { "r10", offsetof(CPUState, gpr[10]) },
1956 { "r11", offsetof(CPUState, gpr[11]) },
1957 { "r12", offsetof(CPUState, gpr[12]) },
1958 { "r13", offsetof(CPUState, gpr[13]) },
1959 { "r14", offsetof(CPUState, gpr[14]) },
1960 { "r15", offsetof(CPUState, gpr[15]) },
1961 { "r16", offsetof(CPUState, gpr[16]) },
1962 { "r17", offsetof(CPUState, gpr[17]) },
1963 { "r18", offsetof(CPUState, gpr[18]) },
1964 { "r19", offsetof(CPUState, gpr[19]) },
1965 { "r20", offsetof(CPUState, gpr[20]) },
1966 { "r21", offsetof(CPUState, gpr[21]) },
1967 { "r22", offsetof(CPUState, gpr[22]) },
1968 { "r23", offsetof(CPUState, gpr[23]) },
1969 { "r24", offsetof(CPUState, gpr[24]) },
1970 { "r25", offsetof(CPUState, gpr[25]) },
1971 { "r26", offsetof(CPUState, gpr[26]) },
1972 { "r27", offsetof(CPUState, gpr[27]) },
1973 { "r28", offsetof(CPUState, gpr[28]) },
1974 { "r29", offsetof(CPUState, gpr[29]) },
1975 { "r30", offsetof(CPUState, gpr[30]) },
1976 { "r31", offsetof(CPUState, gpr[31]) },
1977 /* Floating point registers */
1978 { "f0", offsetof(CPUState, fpr[0]) },
1979 { "f1", offsetof(CPUState, fpr[1]) },
1980 { "f2", offsetof(CPUState, fpr[2]) },
1981 { "f3", offsetof(CPUState, fpr[3]) },
1982 { "f4", offsetof(CPUState, fpr[4]) },
1983 { "f5", offsetof(CPUState, fpr[5]) },
1984 { "f6", offsetof(CPUState, fpr[6]) },
1985 { "f7", offsetof(CPUState, fpr[7]) },
1986 { "f8", offsetof(CPUState, fpr[8]) },
1987 { "f9", offsetof(CPUState, fpr[9]) },
1988 { "f10", offsetof(CPUState, fpr[10]) },
1989 { "f11", offsetof(CPUState, fpr[11]) },
1990 { "f12", offsetof(CPUState, fpr[12]) },
1991 { "f13", offsetof(CPUState, fpr[13]) },
1992 { "f14", offsetof(CPUState, fpr[14]) },
1993 { "f15", offsetof(CPUState, fpr[15]) },
1994 { "f16", offsetof(CPUState, fpr[16]) },
1995 { "f17", offsetof(CPUState, fpr[17]) },
1996 { "f18", offsetof(CPUState, fpr[18]) },
1997 { "f19", offsetof(CPUState, fpr[19]) },
1998 { "f20", offsetof(CPUState, fpr[20]) },
1999 { "f21", offsetof(CPUState, fpr[21]) },
2000 { "f22", offsetof(CPUState, fpr[22]) },
2001 { "f23", offsetof(CPUState, fpr[23]) },
2002 { "f24", offsetof(CPUState, fpr[24]) },
2003 { "f25", offsetof(CPUState, fpr[25]) },
2004 { "f26", offsetof(CPUState, fpr[26]) },
2005 { "f27", offsetof(CPUState, fpr[27]) },
2006 { "f28", offsetof(CPUState, fpr[28]) },
2007 { "f29", offsetof(CPUState, fpr[29]) },
2008 { "f30", offsetof(CPUState, fpr[30]) },
2009 { "f31", offsetof(CPUState, fpr[31]) },
2010 { "fpscr", offsetof(CPUState, fpscr) },
2011 /* Next instruction pointer */
2012 { "nip|pc", offsetof(CPUState, nip) },
2013 { "lr", offsetof(CPUState, lr) },
2014 { "ctr", offsetof(CPUState, ctr) },
2015 { "decr", 0, &monitor_get_decr, },
2016 { "ccr", 0, &monitor_get_ccr, },
2017 /* Machine state register */
2018 { "msr", 0, &monitor_get_msr, },
2019 { "xer", 0, &monitor_get_xer, },
2020 { "tbu", 0, &monitor_get_tbu, },
2021 { "tbl", 0, &monitor_get_tbl, },
2022 #if defined(TARGET_PPC64)
2023 /* Address space register */
2024 { "asr", offsetof(CPUState, asr) },
2026 /* Segment registers */
2027 { "sdr1", offsetof(CPUState, sdr1) },
2028 { "sr0", offsetof(CPUState, sr[0]) },
2029 { "sr1", offsetof(CPUState, sr[1]) },
2030 { "sr2", offsetof(CPUState, sr[2]) },
2031 { "sr3", offsetof(CPUState, sr[3]) },
2032 { "sr4", offsetof(CPUState, sr[4]) },
2033 { "sr5", offsetof(CPUState, sr[5]) },
2034 { "sr6", offsetof(CPUState, sr[6]) },
2035 { "sr7", offsetof(CPUState, sr[7]) },
2036 { "sr8", offsetof(CPUState, sr[8]) },
2037 { "sr9", offsetof(CPUState, sr[9]) },
2038 { "sr10", offsetof(CPUState, sr[10]) },
2039 { "sr11", offsetof(CPUState, sr[11]) },
2040 { "sr12", offsetof(CPUState, sr[12]) },
2041 { "sr13", offsetof(CPUState, sr[13]) },
2042 { "sr14", offsetof(CPUState, sr[14]) },
2043 { "sr15", offsetof(CPUState, sr[15]) },
2044 /* Too lazy to put BATs and SPRs ... */
2045 #elif defined(TARGET_SPARC)
2046 { "g0", offsetof(CPUState, gregs[0]) },
2047 { "g1", offsetof(CPUState, gregs[1]) },
2048 { "g2", offsetof(CPUState, gregs[2]) },
2049 { "g3", offsetof(CPUState, gregs[3]) },
2050 { "g4", offsetof(CPUState, gregs[4]) },
2051 { "g5", offsetof(CPUState, gregs[5]) },
2052 { "g6", offsetof(CPUState, gregs[6]) },
2053 { "g7", offsetof(CPUState, gregs[7]) },
2054 { "o0", 0, monitor_get_reg },
2055 { "o1", 1, monitor_get_reg },
2056 { "o2", 2, monitor_get_reg },
2057 { "o3", 3, monitor_get_reg },
2058 { "o4", 4, monitor_get_reg },
2059 { "o5", 5, monitor_get_reg },
2060 { "o6", 6, monitor_get_reg },
2061 { "o7", 7, monitor_get_reg },
2062 { "l0", 8, monitor_get_reg },
2063 { "l1", 9, monitor_get_reg },
2064 { "l2", 10, monitor_get_reg },
2065 { "l3", 11, monitor_get_reg },
2066 { "l4", 12, monitor_get_reg },
2067 { "l5", 13, monitor_get_reg },
2068 { "l6", 14, monitor_get_reg },
2069 { "l7", 15, monitor_get_reg },
2070 { "i0", 16, monitor_get_reg },
2071 { "i1", 17, monitor_get_reg },
2072 { "i2", 18, monitor_get_reg },
2073 { "i3", 19, monitor_get_reg },
2074 { "i4", 20, monitor_get_reg },
2075 { "i5", 21, monitor_get_reg },
2076 { "i6", 22, monitor_get_reg },
2077 { "i7", 23, monitor_get_reg },
2078 { "pc", offsetof(CPUState, pc) },
2079 { "npc", offsetof(CPUState, npc) },
2080 { "y", offsetof(CPUState, y) },
2081 #ifndef TARGET_SPARC64
2082 { "psr", 0, &monitor_get_psr, },
2083 { "wim", offsetof(CPUState, wim) },
2085 { "tbr", offsetof(CPUState, tbr) },
2086 { "fsr", offsetof(CPUState, fsr) },
2087 { "f0", offsetof(CPUState, fpr[0]) },
2088 { "f1", offsetof(CPUState, fpr[1]) },
2089 { "f2", offsetof(CPUState, fpr[2]) },
2090 { "f3", offsetof(CPUState, fpr[3]) },
2091 { "f4", offsetof(CPUState, fpr[4]) },
2092 { "f5", offsetof(CPUState, fpr[5]) },
2093 { "f6", offsetof(CPUState, fpr[6]) },
2094 { "f7", offsetof(CPUState, fpr[7]) },
2095 { "f8", offsetof(CPUState, fpr[8]) },
2096 { "f9", offsetof(CPUState, fpr[9]) },
2097 { "f10", offsetof(CPUState, fpr[10]) },
2098 { "f11", offsetof(CPUState, fpr[11]) },
2099 { "f12", offsetof(CPUState, fpr[12]) },
2100 { "f13", offsetof(CPUState, fpr[13]) },
2101 { "f14", offsetof(CPUState, fpr[14]) },
2102 { "f15", offsetof(CPUState, fpr[15]) },
2103 { "f16", offsetof(CPUState, fpr[16]) },
2104 { "f17", offsetof(CPUState, fpr[17]) },
2105 { "f18", offsetof(CPUState, fpr[18]) },
2106 { "f19", offsetof(CPUState, fpr[19]) },
2107 { "f20", offsetof(CPUState, fpr[20]) },
2108 { "f21", offsetof(CPUState, fpr[21]) },
2109 { "f22", offsetof(CPUState, fpr[22]) },
2110 { "f23", offsetof(CPUState, fpr[23]) },
2111 { "f24", offsetof(CPUState, fpr[24]) },
2112 { "f25", offsetof(CPUState, fpr[25]) },
2113 { "f26", offsetof(CPUState, fpr[26]) },
2114 { "f27", offsetof(CPUState, fpr[27]) },
2115 { "f28", offsetof(CPUState, fpr[28]) },
2116 { "f29", offsetof(CPUState, fpr[29]) },
2117 { "f30", offsetof(CPUState, fpr[30]) },
2118 { "f31", offsetof(CPUState, fpr[31]) },
2119 #ifdef TARGET_SPARC64
2120 { "f32", offsetof(CPUState, fpr[32]) },
2121 { "f34", offsetof(CPUState, fpr[34]) },
2122 { "f36", offsetof(CPUState, fpr[36]) },
2123 { "f38", offsetof(CPUState, fpr[38]) },
2124 { "f40", offsetof(CPUState, fpr[40]) },
2125 { "f42", offsetof(CPUState, fpr[42]) },
2126 { "f44", offsetof(CPUState, fpr[44]) },
2127 { "f46", offsetof(CPUState, fpr[46]) },
2128 { "f48", offsetof(CPUState, fpr[48]) },
2129 { "f50", offsetof(CPUState, fpr[50]) },
2130 { "f52", offsetof(CPUState, fpr[52]) },
2131 { "f54", offsetof(CPUState, fpr[54]) },
2132 { "f56", offsetof(CPUState, fpr[56]) },
2133 { "f58", offsetof(CPUState, fpr[58]) },
2134 { "f60", offsetof(CPUState, fpr[60]) },
2135 { "f62", offsetof(CPUState, fpr[62]) },
2136 { "asi", offsetof(CPUState, asi) },
2137 { "pstate", offsetof(CPUState, pstate) },
2138 { "cansave", offsetof(CPUState, cansave) },
2139 { "canrestore", offsetof(CPUState, canrestore) },
2140 { "otherwin", offsetof(CPUState, otherwin) },
2141 { "wstate", offsetof(CPUState, wstate) },
2142 { "cleanwin", offsetof(CPUState, cleanwin) },
2143 { "fprs", offsetof(CPUState, fprs) },
2149 static void expr_error(Monitor *mon, const char *msg)
2151 monitor_printf(mon, "%s\n", msg);
2152 longjmp(expr_env, 1);
2155 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2156 static int get_monitor_def(target_long *pval, const char *name)
2158 const MonitorDef *md;
2161 for(md = monitor_defs; md->name != NULL; md++) {
2162 if (compare_cmd(name, md->name)) {
2163 if (md->get_value) {
2164 *pval = md->get_value(md, md->offset);
2166 CPUState *env = mon_get_cpu();
2169 ptr = (uint8_t *)env + md->offset;
2172 *pval = *(int32_t *)ptr;
2175 *pval = *(target_long *)ptr;
2188 static void next(void)
2192 while (qemu_isspace(*pch))
2197 static int64_t expr_sum(Monitor *mon);
2199 static int64_t expr_unary(Monitor *mon)
2208 n = expr_unary(mon);
2212 n = -expr_unary(mon);
2216 n = ~expr_unary(mon);
2222 expr_error(mon, "')' expected");
2229 expr_error(mon, "character constant expected");
2233 expr_error(mon, "missing terminating \' character");
2243 while ((*pch >= 'a' && *pch <= 'z') ||
2244 (*pch >= 'A' && *pch <= 'Z') ||
2245 (*pch >= '0' && *pch <= '9') ||
2246 *pch == '_' || *pch == '.') {
2247 if ((q - buf) < sizeof(buf) - 1)
2251 while (qemu_isspace(*pch))
2254 ret = get_monitor_def(®, buf);
2256 expr_error(mon, "unknown register");
2258 expr_error(mon, "no cpu defined");
2263 expr_error(mon, "unexpected end of expression");
2267 #if TARGET_PHYS_ADDR_BITS > 32
2268 n = strtoull(pch, &p, 0);
2270 n = strtoul(pch, &p, 0);
2273 expr_error(mon, "invalid char in expression");
2276 while (qemu_isspace(*pch))
2284 static int64_t expr_prod(Monitor *mon)
2289 val = expr_unary(mon);
2292 if (op != '*' && op != '/' && op != '%')
2295 val2 = expr_unary(mon);
2304 expr_error(mon, "division by zero");
2315 static int64_t expr_logic(Monitor *mon)
2320 val = expr_prod(mon);
2323 if (op != '&' && op != '|' && op != '^')
2326 val2 = expr_prod(mon);
2343 static int64_t expr_sum(Monitor *mon)
2348 val = expr_logic(mon);
2351 if (op != '+' && op != '-')
2354 val2 = expr_logic(mon);
2363 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2366 if (setjmp(expr_env)) {
2370 while (qemu_isspace(*pch))
2372 *pval = expr_sum(mon);
2377 static int get_str(char *buf, int buf_size, const char **pp)
2385 while (qemu_isspace(*p))
2395 while (*p != '\0' && *p != '\"') {
2411 qemu_printf("unsupported escape code: '\\%c'\n", c);
2414 if ((q - buf) < buf_size - 1) {
2418 if ((q - buf) < buf_size - 1) {
2425 qemu_printf("unterminated string\n");
2430 while (*p != '\0' && !qemu_isspace(*p)) {
2431 if ((q - buf) < buf_size - 1) {
2442 static int default_fmt_format = 'x';
2443 static int default_fmt_size = 4;
2447 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2449 const char *p, *pstart, *typestr;
2451 int c, nb_args, len, i, has_arg;
2452 const mon_cmd_t *cmd;
2455 void *str_allocated[MAX_ARGS];
2456 void *args[MAX_ARGS];
2457 void (*handler_0)(Monitor *mon);
2458 void (*handler_1)(Monitor *mon, void *arg0);
2459 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2460 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2461 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2463 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2464 void *arg3, void *arg4);
2465 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2466 void *arg3, void *arg4, void *arg5);
2467 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2468 void *arg3, void *arg4, void *arg5, void *arg6);
2471 monitor_printf(mon, "command='%s'\n", cmdline);
2474 /* extract the command name */
2477 while (qemu_isspace(*p))
2482 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2485 if (len > sizeof(cmdname) - 1)
2486 len = sizeof(cmdname) - 1;
2487 memcpy(cmdname, pstart, len);
2488 cmdname[len] = '\0';
2490 /* find the command */
2491 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2492 if (compare_cmd(cmdname, cmd->name))
2495 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2499 for(i = 0; i < MAX_ARGS; i++)
2500 str_allocated[i] = NULL;
2502 /* parse the parameters */
2503 typestr = cmd->args_type;
2518 while (qemu_isspace(*p))
2520 if (*typestr == '?') {
2523 /* no optional string: NULL argument */
2528 ret = get_str(buf, sizeof(buf), &p);
2532 monitor_printf(mon, "%s: filename expected\n",
2536 monitor_printf(mon, "%s: block device name expected\n",
2540 monitor_printf(mon, "%s: string expected\n", cmdname);
2545 str = qemu_malloc(strlen(buf) + 1);
2546 pstrcpy(str, sizeof(buf), buf);
2547 str_allocated[nb_args] = str;
2549 if (nb_args >= MAX_ARGS) {
2551 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2554 args[nb_args++] = str;
2559 int count, format, size;
2561 while (qemu_isspace(*p))
2567 if (qemu_isdigit(*p)) {
2569 while (qemu_isdigit(*p)) {
2570 count = count * 10 + (*p - '0');
2608 if (*p != '\0' && !qemu_isspace(*p)) {
2609 monitor_printf(mon, "invalid char in format: '%c'\n",
2614 format = default_fmt_format;
2615 if (format != 'i') {
2616 /* for 'i', not specifying a size gives -1 as size */
2618 size = default_fmt_size;
2619 default_fmt_size = size;
2621 default_fmt_format = format;
2624 format = default_fmt_format;
2625 if (format != 'i') {
2626 size = default_fmt_size;
2631 if (nb_args + 3 > MAX_ARGS)
2633 args[nb_args++] = (void*)(long)count;
2634 args[nb_args++] = (void*)(long)format;
2635 args[nb_args++] = (void*)(long)size;
2643 while (qemu_isspace(*p))
2645 if (*typestr == '?' || *typestr == '.') {
2646 if (*typestr == '?') {
2654 while (qemu_isspace(*p))
2662 if (nb_args >= MAX_ARGS)
2664 args[nb_args++] = (void *)(long)has_arg;
2666 if (nb_args >= MAX_ARGS)
2672 if (get_expr(mon, &val, &p))
2676 if (nb_args >= MAX_ARGS)
2678 args[nb_args++] = (void *)(long)val;
2680 if ((nb_args + 1) >= MAX_ARGS)
2682 #if TARGET_PHYS_ADDR_BITS > 32
2683 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2685 args[nb_args++] = (void *)0;
2687 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2699 while (qemu_isspace(*p))
2705 monitor_printf(mon, "%s: unsupported option -%c\n",
2712 if (nb_args >= MAX_ARGS)
2714 args[nb_args++] = (void *)(long)has_option;
2719 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2723 /* check that all arguments were parsed */
2724 while (qemu_isspace(*p))
2727 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2734 handler_0 = cmd->handler;
2738 handler_1 = cmd->handler;
2739 handler_1(mon, args[0]);
2742 handler_2 = cmd->handler;
2743 handler_2(mon, args[0], args[1]);
2746 handler_3 = cmd->handler;
2747 handler_3(mon, args[0], args[1], args[2]);
2750 handler_4 = cmd->handler;
2751 handler_4(mon, args[0], args[1], args[2], args[3]);
2754 handler_5 = cmd->handler;
2755 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2758 handler_6 = cmd->handler;
2759 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2762 handler_7 = cmd->handler;
2763 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2767 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2771 for(i = 0; i < MAX_ARGS; i++)
2772 qemu_free(str_allocated[i]);
2776 static void cmd_completion(const char *name, const char *list)
2778 const char *p, *pstart;
2787 p = pstart + strlen(pstart);
2789 if (len > sizeof(cmd) - 2)
2790 len = sizeof(cmd) - 2;
2791 memcpy(cmd, pstart, len);
2793 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2794 readline_add_completion(cur_mon->rs, cmd);
2802 static void file_completion(const char *input)
2807 char file[1024], file_prefix[1024];
2811 p = strrchr(input, '/');
2814 pstrcpy(file_prefix, sizeof(file_prefix), input);
2815 pstrcpy(path, sizeof(path), ".");
2817 input_path_len = p - input + 1;
2818 memcpy(path, input, input_path_len);
2819 if (input_path_len > sizeof(path) - 1)
2820 input_path_len = sizeof(path) - 1;
2821 path[input_path_len] = '\0';
2822 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2824 #ifdef DEBUG_COMPLETION
2825 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2826 input, path, file_prefix);
2828 ffs = opendir(path);
2836 if (strstart(d->d_name, file_prefix, NULL)) {
2837 memcpy(file, input, input_path_len);
2838 if (input_path_len < sizeof(file))
2839 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2841 /* stat the file to find out if it's a directory.
2842 * In that case add a slash to speed up typing long paths
2845 if(S_ISDIR(sb.st_mode))
2846 pstrcat(file, sizeof(file), "/");
2847 readline_add_completion(cur_mon->rs, file);
2853 static void block_completion_it(void *opaque, BlockDriverState *bs)
2855 const char *name = bdrv_get_device_name(bs);
2856 const char *input = opaque;
2858 if (input[0] == '\0' ||
2859 !strncmp(name, (char *)input, strlen(input))) {
2860 readline_add_completion(cur_mon->rs, name);
2864 /* NOTE: this parser is an approximate form of the real command parser */
2865 static void parse_cmdline(const char *cmdline,
2866 int *pnb_args, char **args)
2875 while (qemu_isspace(*p))
2879 if (nb_args >= MAX_ARGS)
2881 ret = get_str(buf, sizeof(buf), &p);
2882 args[nb_args] = qemu_strdup(buf);
2887 *pnb_args = nb_args;
2890 static void monitor_find_completion(const char *cmdline)
2892 const char *cmdname;
2893 char *args[MAX_ARGS];
2894 int nb_args, i, len;
2895 const char *ptype, *str;
2896 const mon_cmd_t *cmd;
2899 parse_cmdline(cmdline, &nb_args, args);
2900 #ifdef DEBUG_COMPLETION
2901 for(i = 0; i < nb_args; i++) {
2902 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2906 /* if the line ends with a space, it means we want to complete the
2908 len = strlen(cmdline);
2909 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2910 if (nb_args >= MAX_ARGS)
2912 args[nb_args++] = qemu_strdup("");
2915 /* command completion */
2920 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2921 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2922 cmd_completion(cmdname, cmd->name);
2925 /* find the command */
2926 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2927 if (compare_cmd(args[0], cmd->name))
2932 ptype = cmd->args_type;
2933 for(i = 0; i < nb_args - 2; i++) {
2934 if (*ptype != '\0') {
2936 while (*ptype == '?')
2940 str = args[nb_args - 1];
2943 /* file completion */
2944 readline_set_completion_index(cur_mon->rs, strlen(str));
2945 file_completion(str);
2948 /* block device name completion */
2949 readline_set_completion_index(cur_mon->rs, strlen(str));
2950 bdrv_iterate(block_completion_it, (void *)str);
2953 /* XXX: more generic ? */
2954 if (!strcmp(cmd->name, "info")) {
2955 readline_set_completion_index(cur_mon->rs, strlen(str));
2956 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
2957 cmd_completion(str, cmd->name);
2959 } else if (!strcmp(cmd->name, "sendkey")) {
2960 readline_set_completion_index(cur_mon->rs, strlen(str));
2961 for(key = key_defs; key->name != NULL; key++) {
2962 cmd_completion(str, key->name);
2970 for(i = 0; i < nb_args; i++)
2974 static int monitor_can_read(void *opaque)
2976 Monitor *mon = opaque;
2978 return (mon->suspend_cnt == 0) ? 128 : 0;
2981 static void monitor_read(void *opaque, const uint8_t *buf, int size)
2983 Monitor *old_mon = cur_mon;
2989 for (i = 0; i < size; i++)
2990 readline_handle_byte(cur_mon->rs, buf[i]);
2992 if (size == 0 || buf[size - 1] != 0)
2993 monitor_printf(cur_mon, "corrupted command\n");
2995 monitor_handle_command(cur_mon, (char *)buf);
3001 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3003 monitor_suspend(mon);
3004 monitor_handle_command(mon, cmdline);
3005 monitor_resume(mon);
3008 int monitor_suspend(Monitor *mon)
3016 void monitor_resume(Monitor *mon)
3020 if (--mon->suspend_cnt == 0)
3021 readline_show_prompt(mon->rs);
3024 static void monitor_event(void *opaque, int event)
3026 Monitor *mon = opaque;
3029 case CHR_EVENT_MUX_IN:
3030 readline_restart(mon->rs);
3031 monitor_resume(mon);
3035 case CHR_EVENT_MUX_OUT:
3036 if (mon->suspend_cnt == 0)
3037 monitor_printf(mon, "\n");
3039 monitor_suspend(mon);
3042 case CHR_EVENT_RESET:
3043 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3044 "information\n", QEMU_VERSION);
3045 if (mon->chr->focus == 0)
3046 readline_show_prompt(mon->rs);
3060 void monitor_init(CharDriverState *chr, int flags)
3062 static int is_first_init = 1;
3065 if (is_first_init) {
3066 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3070 mon = qemu_mallocz(sizeof(*mon));
3074 if (mon->chr->focus != 0)
3075 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3076 if (flags & MONITOR_USE_READLINE) {
3077 mon->rs = readline_init(mon, monitor_find_completion);
3078 monitor_read_command(mon, 0);
3081 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3084 LIST_INSERT_HEAD(&mon_list, mon, entry);
3085 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3089 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3091 BlockDriverState *bs = opaque;
3094 if (bdrv_set_key(bs, password) != 0) {
3095 monitor_printf(mon, "invalid password\n");
3098 if (mon->password_completion_cb)
3099 mon->password_completion_cb(mon->password_opaque, ret);
3101 monitor_read_command(mon, 1);
3104 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3105 BlockDriverCompletionFunc *completion_cb,
3110 if (!bdrv_key_required(bs)) {
3112 completion_cb(opaque, 0);
3116 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3117 bdrv_get_encrypted_filename(bs));
3119 mon->password_completion_cb = completion_cb;
3120 mon->password_opaque = opaque;
3122 err = monitor_read_password(mon, bdrv_password_cb, bs);
3124 if (err && completion_cb)
3125 completion_cb(opaque, err);