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"
30 #include "hw/watchdog.h"
33 #include "qemu-char.h"
39 #include "audio/audio.h"
42 #include "qemu-timer.h"
43 #include "migration.h"
48 //#define DEBUG_COMPLETION
54 * 'B' block device name
55 * 's' string (accept optional quote)
57 * 'l' target long (32 or 64 bit)
58 * '/' optional gdb-like print format (like "/10x")
60 * '?' optional type (for 'F', 's' and 'i')
64 typedef struct mon_cmd_t {
66 const char *args_type;
80 BlockDriverCompletionFunc *password_completion_cb;
81 void *password_opaque;
82 LIST_ENTRY(Monitor) entry;
85 static LIST_HEAD(mon_list, Monitor) mon_list;
87 static const mon_cmd_t mon_cmds[];
88 static const mon_cmd_t info_cmds[];
90 Monitor *cur_mon = NULL;
92 static void monitor_command_cb(Monitor *mon, const char *cmdline,
95 static void monitor_read_command(Monitor *mon, int show_prompt)
97 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
99 readline_show_prompt(mon->rs);
102 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
106 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
107 /* prompt is printed on return from the command handler */
110 monitor_printf(mon, "terminal does not support password prompting\n");
115 void monitor_flush(Monitor *mon)
117 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
118 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
119 mon->outbuf_index = 0;
123 /* flush at every end of line or if the buffer is full */
124 static void monitor_puts(Monitor *mon, const char *str)
136 mon->outbuf[mon->outbuf_index++] = '\r';
137 mon->outbuf[mon->outbuf_index++] = c;
138 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
144 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
147 vsnprintf(buf, sizeof(buf), fmt, ap);
148 monitor_puts(mon, buf);
151 void monitor_printf(Monitor *mon, const char *fmt, ...)
155 monitor_vprintf(mon, fmt, ap);
159 void monitor_print_filename(Monitor *mon, const char *filename)
163 for (i = 0; filename[i]; i++) {
164 switch (filename[i]) {
168 monitor_printf(mon, "\\%c", filename[i]);
171 monitor_printf(mon, "\\t");
174 monitor_printf(mon, "\\r");
177 monitor_printf(mon, "\\n");
180 monitor_printf(mon, "%c", filename[i]);
186 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
190 monitor_vprintf((Monitor *)stream, fmt, ap);
195 static int compare_cmd(const char *name, const char *list)
197 const char *p, *pstart;
205 p = pstart + strlen(pstart);
206 if ((p - pstart) == len && !memcmp(pstart, name, len))
215 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
216 const char *prefix, const char *name)
218 const mon_cmd_t *cmd;
220 for(cmd = cmds; cmd->name != NULL; cmd++) {
221 if (!name || !strcmp(name, cmd->name))
222 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
223 cmd->params, cmd->help);
227 static void help_cmd(Monitor *mon, const char *name)
229 if (name && !strcmp(name, "info")) {
230 help_cmd_dump(mon, info_cmds, "info ", NULL);
232 help_cmd_dump(mon, mon_cmds, "", name);
233 if (name && !strcmp(name, "log")) {
234 const CPULogItem *item;
235 monitor_printf(mon, "Log items (comma separated):\n");
236 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
237 for(item = cpu_log_items; item->mask != 0; item++) {
238 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
244 static void do_commit(Monitor *mon, const char *device)
248 all_devices = !strcmp(device, "all");
249 for (i = 0; i < nb_drives; i++) {
251 !strcmp(bdrv_get_device_name(drives_table[i].bdrv), device))
252 bdrv_commit(drives_table[i].bdrv);
256 static void do_info(Monitor *mon, const char *item)
258 const mon_cmd_t *cmd;
259 void (*handler)(Monitor *);
263 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
264 if (compare_cmd(item, cmd->name))
268 help_cmd(mon, "info");
271 handler = cmd->handler;
275 static void do_info_version(Monitor *mon)
277 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
280 static void do_info_name(Monitor *mon)
283 monitor_printf(mon, "%s\n", qemu_name);
286 #if defined(TARGET_I386)
287 static void do_info_hpet(Monitor *mon)
289 monitor_printf(mon, "HPET is %s by QEMU\n",
290 (no_hpet) ? "disabled" : "enabled");
294 static void do_info_uuid(Monitor *mon)
296 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
297 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
298 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
299 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
300 qemu_uuid[14], qemu_uuid[15]);
303 /* get the current CPU defined by the user */
304 static int mon_set_cpu(int cpu_index)
308 for(env = first_cpu; env != NULL; env = env->next_cpu) {
309 if (env->cpu_index == cpu_index) {
310 cur_mon->mon_cpu = env;
317 static CPUState *mon_get_cpu(void)
319 if (!cur_mon->mon_cpu) {
322 cpu_synchronize_state(cur_mon->mon_cpu, 0);
323 return cur_mon->mon_cpu;
326 static void do_info_registers(Monitor *mon)
333 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
336 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
341 static void do_info_cpus(Monitor *mon)
345 /* just to set the default cpu if not already done */
348 for(env = first_cpu; env != NULL; env = env->next_cpu) {
349 cpu_synchronize_state(env, 0);
350 monitor_printf(mon, "%c CPU #%d:",
351 (env == mon->mon_cpu) ? '*' : ' ',
353 #if defined(TARGET_I386)
354 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
355 env->eip + env->segs[R_CS].base);
356 #elif defined(TARGET_PPC)
357 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
358 #elif defined(TARGET_SPARC)
359 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
361 #elif defined(TARGET_MIPS)
362 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
365 monitor_printf(mon, " (halted)");
366 monitor_printf(mon, "\n");
370 static void do_cpu_set(Monitor *mon, int index)
372 if (mon_set_cpu(index) < 0)
373 monitor_printf(mon, "Invalid CPU index\n");
376 static void do_info_jit(Monitor *mon)
378 dump_exec_info((FILE *)mon, monitor_fprintf);
381 static void do_info_history(Monitor *mon)
390 str = readline_get_history(mon->rs, i);
393 monitor_printf(mon, "%d: '%s'\n", i, str);
398 #if defined(TARGET_PPC)
399 /* XXX: not implemented in other targets */
400 static void do_info_cpu_stats(Monitor *mon)
405 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
409 static void do_quit(Monitor *mon)
414 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
416 if (bdrv_is_inserted(bs)) {
418 if (!bdrv_is_removable(bs)) {
419 monitor_printf(mon, "device is not removable\n");
422 if (bdrv_is_locked(bs)) {
423 monitor_printf(mon, "device is locked\n");
432 static void do_eject(Monitor *mon, int force, const char *filename)
434 BlockDriverState *bs;
436 bs = bdrv_find(filename);
438 monitor_printf(mon, "device not found\n");
441 eject_device(mon, bs, force);
444 static void do_change_block(Monitor *mon, const char *device,
445 const char *filename, const char *fmt)
447 BlockDriverState *bs;
448 BlockDriver *drv = NULL;
450 bs = bdrv_find(device);
452 monitor_printf(mon, "device not found\n");
456 drv = bdrv_find_format(fmt);
458 monitor_printf(mon, "invalid format %s\n", fmt);
462 if (eject_device(mon, bs, 0) < 0)
464 bdrv_open2(bs, filename, 0, drv);
465 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
468 static void change_vnc_password_cb(Monitor *mon, const char *password,
471 if (vnc_display_password(NULL, password) < 0)
472 monitor_printf(mon, "could not set VNC server password\n");
474 monitor_read_command(mon, 1);
477 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
479 if (strcmp(target, "passwd") == 0 ||
480 strcmp(target, "password") == 0) {
483 strncpy(password, arg, sizeof(password));
484 password[sizeof(password) - 1] = '\0';
485 change_vnc_password_cb(mon, password, NULL);
487 monitor_read_password(mon, change_vnc_password_cb, NULL);
490 if (vnc_display_open(NULL, target) < 0)
491 monitor_printf(mon, "could not start VNC server on %s\n", target);
495 static void do_change(Monitor *mon, const char *device, const char *target,
498 if (strcmp(device, "vnc") == 0) {
499 do_change_vnc(mon, target, arg);
501 do_change_block(mon, device, target, arg);
505 static void do_screen_dump(Monitor *mon, const char *filename)
507 vga_hw_screen_dump(filename);
510 static void do_logfile(Monitor *mon, const char *filename)
512 cpu_set_log_filename(filename);
515 static void do_log(Monitor *mon, const char *items)
519 if (!strcmp(items, "none")) {
522 mask = cpu_str_to_log_mask(items);
524 help_cmd(mon, "log");
531 static void do_singlestep(Monitor *mon, const char *option)
533 if (!option || !strcmp(option, "on")) {
535 } else if (!strcmp(option, "off")) {
538 monitor_printf(mon, "unexpected option %s\n", option);
542 static void do_stop(Monitor *mon)
544 vm_stop(EXCP_INTERRUPT);
547 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
549 struct bdrv_iterate_context {
554 static void do_cont(Monitor *mon)
556 struct bdrv_iterate_context context = { mon, 0 };
558 bdrv_iterate(encrypted_bdrv_it, &context);
559 /* only resume the vm if all keys are set and valid */
564 static void bdrv_key_cb(void *opaque, int err)
566 Monitor *mon = opaque;
568 /* another key was set successfully, retry to continue */
573 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
575 struct bdrv_iterate_context *context = opaque;
577 if (!context->err && bdrv_key_required(bs)) {
578 context->err = -EBUSY;
579 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
584 #ifdef CONFIG_GDBSTUB
585 static void do_gdbserver(Monitor *mon, const char *device)
588 device = "tcp::" DEFAULT_GDBSTUB_PORT;
589 if (gdbserver_start(device) < 0) {
590 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
592 } else if (strcmp(device, "none") == 0) {
593 monitor_printf(mon, "Disabled gdbserver\n");
595 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
601 static void do_watchdog_action(Monitor *mon, const char *action)
603 if (select_watchdog_action(action) == -1) {
604 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
608 static void monitor_printc(Monitor *mon, int c)
610 monitor_printf(mon, "'");
613 monitor_printf(mon, "\\'");
616 monitor_printf(mon, "\\\\");
619 monitor_printf(mon, "\\n");
622 monitor_printf(mon, "\\r");
625 if (c >= 32 && c <= 126) {
626 monitor_printf(mon, "%c", c);
628 monitor_printf(mon, "\\x%02x", c);
632 monitor_printf(mon, "'");
635 static void memory_dump(Monitor *mon, int count, int format, int wsize,
636 target_phys_addr_t addr, int is_physical)
639 int nb_per_line, l, line_size, i, max_digits, len;
647 if (!env && !is_physical)
652 } else if (wsize == 4) {
655 /* as default we use the current CS size */
659 if ((env->efer & MSR_EFER_LMA) &&
660 (env->segs[R_CS].flags & DESC_L_MASK))
664 if (!(env->segs[R_CS].flags & DESC_B_MASK))
669 monitor_disas(mon, env, addr, count, is_physical, flags);
678 nb_per_line = line_size / wsize;
683 max_digits = (wsize * 8 + 2) / 3;
687 max_digits = (wsize * 8) / 4;
691 max_digits = (wsize * 8 * 10 + 32) / 33;
700 monitor_printf(mon, TARGET_FMT_plx ":", addr);
702 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
707 cpu_physical_memory_rw(addr, buf, l, 0);
712 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
713 monitor_printf(mon, " Cannot access memory\n");
722 v = ldub_raw(buf + i);
725 v = lduw_raw(buf + i);
728 v = (uint32_t)ldl_raw(buf + i);
731 v = ldq_raw(buf + i);
734 monitor_printf(mon, " ");
737 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
740 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
743 monitor_printf(mon, "%*" PRIu64, max_digits, v);
746 monitor_printf(mon, "%*" PRId64, max_digits, v);
749 monitor_printc(mon, v);
754 monitor_printf(mon, "\n");
760 #if TARGET_LONG_BITS == 64
761 #define GET_TLONG(h, l) (((uint64_t)(h) << 32) | (l))
763 #define GET_TLONG(h, l) (l)
766 static void do_memory_dump(Monitor *mon, int count, int format, int size,
767 uint32_t addrh, uint32_t addrl)
769 target_long addr = GET_TLONG(addrh, addrl);
770 memory_dump(mon, count, format, size, addr, 0);
773 #if TARGET_PHYS_ADDR_BITS > 32
774 #define GET_TPHYSADDR(h, l) (((uint64_t)(h) << 32) | (l))
776 #define GET_TPHYSADDR(h, l) (l)
779 static void do_physical_memory_dump(Monitor *mon, int count, int format,
780 int size, uint32_t addrh, uint32_t addrl)
783 target_phys_addr_t addr = GET_TPHYSADDR(addrh, addrl);
784 memory_dump(mon, count, format, size, addr, 1);
787 static void do_print(Monitor *mon, int count, int format, int size,
788 unsigned int valh, unsigned int vall)
790 target_phys_addr_t val = GET_TPHYSADDR(valh, vall);
791 #if TARGET_PHYS_ADDR_BITS == 32
794 monitor_printf(mon, "%#o", val);
797 monitor_printf(mon, "%#x", val);
800 monitor_printf(mon, "%u", val);
804 monitor_printf(mon, "%d", val);
807 monitor_printc(mon, val);
813 monitor_printf(mon, "%#" PRIo64, val);
816 monitor_printf(mon, "%#" PRIx64, val);
819 monitor_printf(mon, "%" PRIu64, val);
823 monitor_printf(mon, "%" PRId64, val);
826 monitor_printc(mon, val);
830 monitor_printf(mon, "\n");
833 static void do_memory_save(Monitor *mon, unsigned int valh, unsigned int vall,
834 uint32_t size, const char *filename)
837 target_long addr = GET_TLONG(valh, vall);
846 f = fopen(filename, "wb");
848 monitor_printf(mon, "could not open '%s'\n", filename);
855 cpu_memory_rw_debug(env, addr, buf, l, 0);
856 fwrite(buf, 1, l, f);
863 static void do_physical_memory_save(Monitor *mon, unsigned int valh,
864 unsigned int vall, uint32_t size,
865 const char *filename)
870 target_phys_addr_t addr = GET_TPHYSADDR(valh, vall);
872 f = fopen(filename, "wb");
874 monitor_printf(mon, "could not open '%s'\n", filename);
881 cpu_physical_memory_rw(addr, buf, l, 0);
882 fwrite(buf, 1, l, f);
890 static void do_sum(Monitor *mon, uint32_t start, uint32_t size)
897 for(addr = start; addr < (start + size); addr++) {
898 cpu_physical_memory_rw(addr, buf, 1, 0);
899 /* BSD sum algorithm ('sum' Unix command) */
900 sum = (sum >> 1) | (sum << 15);
903 monitor_printf(mon, "%05d\n", sum);
911 static const KeyDef key_defs[] = {
938 { 0x0e, "backspace" },
975 { 0x37, "asterisk" },
978 { 0x3a, "caps_lock" },
989 { 0x45, "num_lock" },
990 { 0x46, "scroll_lock" },
992 { 0xb5, "kp_divide" },
993 { 0x37, "kp_multiply" },
994 { 0x4a, "kp_subtract" },
996 { 0x9c, "kp_enter" },
997 { 0x53, "kp_decimal" },
1030 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1045 { 0xfe, "compose" },
1050 static int get_keycode(const char *key)
1056 for(p = key_defs; p->name != NULL; p++) {
1057 if (!strcmp(key, p->name))
1060 if (strstart(key, "0x", NULL)) {
1061 ret = strtoul(key, &endp, 0);
1062 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1068 #define MAX_KEYCODES 16
1069 static uint8_t keycodes[MAX_KEYCODES];
1070 static int nb_pending_keycodes;
1071 static QEMUTimer *key_timer;
1073 static void release_keys(void *opaque)
1077 while (nb_pending_keycodes > 0) {
1078 nb_pending_keycodes--;
1079 keycode = keycodes[nb_pending_keycodes];
1081 kbd_put_keycode(0xe0);
1082 kbd_put_keycode(keycode | 0x80);
1086 static void do_sendkey(Monitor *mon, const char *string, int has_hold_time,
1089 char keyname_buf[16];
1091 int keyname_len, keycode, i;
1093 if (nb_pending_keycodes > 0) {
1094 qemu_del_timer(key_timer);
1101 separator = strchr(string, '-');
1102 keyname_len = separator ? separator - string : strlen(string);
1103 if (keyname_len > 0) {
1104 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1105 if (keyname_len > sizeof(keyname_buf) - 1) {
1106 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1109 if (i == MAX_KEYCODES) {
1110 monitor_printf(mon, "too many keys\n");
1113 keyname_buf[keyname_len] = 0;
1114 keycode = get_keycode(keyname_buf);
1116 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1119 keycodes[i++] = keycode;
1123 string = separator + 1;
1125 nb_pending_keycodes = i;
1126 /* key down events */
1127 for (i = 0; i < nb_pending_keycodes; i++) {
1128 keycode = keycodes[i];
1130 kbd_put_keycode(0xe0);
1131 kbd_put_keycode(keycode & 0x7f);
1133 /* delayed key up events */
1134 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1135 muldiv64(ticks_per_sec, hold_time, 1000));
1138 static int mouse_button_state;
1140 static void do_mouse_move(Monitor *mon, const char *dx_str, const char *dy_str,
1144 dx = strtol(dx_str, NULL, 0);
1145 dy = strtol(dy_str, NULL, 0);
1148 dz = strtol(dz_str, NULL, 0);
1149 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1152 static void do_mouse_button(Monitor *mon, int button_state)
1154 mouse_button_state = button_state;
1155 kbd_mouse_event(0, 0, 0, mouse_button_state);
1158 static void do_ioport_read(Monitor *mon, int count, int format, int size,
1159 int addr, int has_index, int index)
1165 cpu_outb(NULL, addr & 0xffff, index & 0xff);
1173 val = cpu_inb(NULL, addr);
1177 val = cpu_inw(NULL, addr);
1181 val = cpu_inl(NULL, addr);
1185 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1186 suffix, addr, size * 2, val);
1189 /* boot_set handler */
1190 static QEMUBootSetHandler *qemu_boot_set_handler = NULL;
1191 static void *boot_opaque;
1193 void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1195 qemu_boot_set_handler = func;
1196 boot_opaque = opaque;
1199 static void do_boot_set(Monitor *mon, const char *bootdevice)
1203 if (qemu_boot_set_handler) {
1204 res = qemu_boot_set_handler(boot_opaque, bootdevice);
1206 monitor_printf(mon, "boot device list now set to %s\n",
1209 monitor_printf(mon, "setting boot device list failed with "
1212 monitor_printf(mon, "no function defined to set boot device list for "
1213 "this architecture\n");
1217 static void do_system_reset(Monitor *mon)
1219 qemu_system_reset_request();
1222 static void do_system_powerdown(Monitor *mon)
1224 qemu_system_powerdown_request();
1227 #if defined(TARGET_I386)
1228 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1230 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1233 pte & PG_GLOBAL_MASK ? 'G' : '-',
1234 pte & PG_PSE_MASK ? 'P' : '-',
1235 pte & PG_DIRTY_MASK ? 'D' : '-',
1236 pte & PG_ACCESSED_MASK ? 'A' : '-',
1237 pte & PG_PCD_MASK ? 'C' : '-',
1238 pte & PG_PWT_MASK ? 'T' : '-',
1239 pte & PG_USER_MASK ? 'U' : '-',
1240 pte & PG_RW_MASK ? 'W' : '-');
1243 static void tlb_info(Monitor *mon)
1247 uint32_t pgd, pde, pte;
1249 env = mon_get_cpu();
1253 if (!(env->cr[0] & CR0_PG_MASK)) {
1254 monitor_printf(mon, "PG disabled\n");
1257 pgd = env->cr[3] & ~0xfff;
1258 for(l1 = 0; l1 < 1024; l1++) {
1259 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1260 pde = le32_to_cpu(pde);
1261 if (pde & PG_PRESENT_MASK) {
1262 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1263 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1265 for(l2 = 0; l2 < 1024; l2++) {
1266 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1267 (uint8_t *)&pte, 4);
1268 pte = le32_to_cpu(pte);
1269 if (pte & PG_PRESENT_MASK) {
1270 print_pte(mon, (l1 << 22) + (l2 << 12),
1280 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1281 uint32_t end, int prot)
1284 prot1 = *plast_prot;
1285 if (prot != prot1) {
1286 if (*pstart != -1) {
1287 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1288 *pstart, end, end - *pstart,
1289 prot1 & PG_USER_MASK ? 'u' : '-',
1291 prot1 & PG_RW_MASK ? 'w' : '-');
1301 static void mem_info(Monitor *mon)
1304 int l1, l2, prot, last_prot;
1305 uint32_t pgd, pde, pte, start, end;
1307 env = mon_get_cpu();
1311 if (!(env->cr[0] & CR0_PG_MASK)) {
1312 monitor_printf(mon, "PG disabled\n");
1315 pgd = env->cr[3] & ~0xfff;
1318 for(l1 = 0; l1 < 1024; l1++) {
1319 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1320 pde = le32_to_cpu(pde);
1322 if (pde & PG_PRESENT_MASK) {
1323 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1324 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1325 mem_print(mon, &start, &last_prot, end, prot);
1327 for(l2 = 0; l2 < 1024; l2++) {
1328 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1329 (uint8_t *)&pte, 4);
1330 pte = le32_to_cpu(pte);
1331 end = (l1 << 22) + (l2 << 12);
1332 if (pte & PG_PRESENT_MASK) {
1333 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1337 mem_print(mon, &start, &last_prot, end, prot);
1342 mem_print(mon, &start, &last_prot, end, prot);
1348 #if defined(TARGET_SH4)
1350 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1352 monitor_printf(mon, " tlb%i:\t"
1353 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1354 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1355 "dirty=%hhu writethrough=%hhu\n",
1357 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1358 tlb->v, tlb->sh, tlb->c, tlb->pr,
1362 static void tlb_info(Monitor *mon)
1364 CPUState *env = mon_get_cpu();
1367 monitor_printf (mon, "ITLB:\n");
1368 for (i = 0 ; i < ITLB_SIZE ; i++)
1369 print_tlb (mon, i, &env->itlb[i]);
1370 monitor_printf (mon, "UTLB:\n");
1371 for (i = 0 ; i < UTLB_SIZE ; i++)
1372 print_tlb (mon, i, &env->utlb[i]);
1377 static void do_info_kqemu(Monitor *mon)
1383 env = mon_get_cpu();
1385 monitor_printf(mon, "No cpu initialized yet");
1388 val = env->kqemu_enabled;
1389 monitor_printf(mon, "kqemu support: ");
1393 monitor_printf(mon, "disabled\n");
1396 monitor_printf(mon, "enabled for user code\n");
1399 monitor_printf(mon, "enabled for user and kernel code\n");
1403 monitor_printf(mon, "kqemu support: not compiled\n");
1407 static void do_info_kvm(Monitor *mon)
1410 monitor_printf(mon, "kvm support: ");
1412 monitor_printf(mon, "enabled\n");
1414 monitor_printf(mon, "disabled\n");
1416 monitor_printf(mon, "kvm support: not compiled\n");
1420 static void do_info_numa(Monitor *mon)
1425 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1426 for (i = 0; i < nb_numa_nodes; i++) {
1427 monitor_printf(mon, "node %d cpus:", i);
1428 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1429 if (env->numa_node == i) {
1430 monitor_printf(mon, " %d", env->cpu_index);
1433 monitor_printf(mon, "\n");
1434 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1439 #ifdef CONFIG_PROFILER
1443 int64_t kqemu_exec_count;
1445 int64_t kqemu_ret_int_count;
1446 int64_t kqemu_ret_excp_count;
1447 int64_t kqemu_ret_intr_count;
1449 static void do_info_profile(Monitor *mon)
1455 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1456 dev_time, dev_time / (double)ticks_per_sec);
1457 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1458 qemu_time, qemu_time / (double)ticks_per_sec);
1459 monitor_printf(mon, "kqemu time %" PRId64 " (%0.3f %0.1f%%) count=%"
1460 PRId64 " int=%" PRId64 " excp=%" PRId64 " intr=%"
1462 kqemu_time, kqemu_time / (double)ticks_per_sec,
1463 kqemu_time / (double)total * 100.0,
1465 kqemu_ret_int_count,
1466 kqemu_ret_excp_count,
1467 kqemu_ret_intr_count);
1470 kqemu_exec_count = 0;
1472 kqemu_ret_int_count = 0;
1473 kqemu_ret_excp_count = 0;
1474 kqemu_ret_intr_count = 0;
1476 kqemu_record_dump();
1480 static void do_info_profile(Monitor *mon)
1482 monitor_printf(mon, "Internal profiler not compiled\n");
1486 /* Capture support */
1487 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1489 static void do_info_capture(Monitor *mon)
1494 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1495 monitor_printf(mon, "[%d]: ", i);
1496 s->ops.info (s->opaque);
1500 static void do_stop_capture(Monitor *mon, int n)
1505 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1507 s->ops.destroy (s->opaque);
1508 LIST_REMOVE (s, entries);
1516 static void do_wav_capture(Monitor *mon, const char *path,
1517 int has_freq, int freq,
1518 int has_bits, int bits,
1519 int has_channels, int nchannels)
1523 s = qemu_mallocz (sizeof (*s));
1525 freq = has_freq ? freq : 44100;
1526 bits = has_bits ? bits : 16;
1527 nchannels = has_channels ? nchannels : 2;
1529 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1530 monitor_printf(mon, "Faied to add wave capture\n");
1533 LIST_INSERT_HEAD (&capture_head, s, entries);
1537 #if defined(TARGET_I386)
1538 static void do_inject_nmi(Monitor *mon, int cpu_index)
1542 for (env = first_cpu; env != NULL; env = env->next_cpu)
1543 if (env->cpu_index == cpu_index) {
1544 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1550 static void do_info_status(Monitor *mon)
1554 monitor_printf(mon, "VM status: running (single step mode)\n");
1556 monitor_printf(mon, "VM status: running\n");
1559 monitor_printf(mon, "VM status: paused\n");
1563 static void do_balloon(Monitor *mon, int value)
1565 ram_addr_t target = value;
1566 qemu_balloon(target << 20);
1569 static void do_info_balloon(Monitor *mon)
1573 actual = qemu_balloon_status();
1574 if (kvm_enabled() && !kvm_has_sync_mmu())
1575 monitor_printf(mon, "Using KVM without synchronous MMU, "
1576 "ballooning disabled\n");
1577 else if (actual == 0)
1578 monitor_printf(mon, "Ballooning not activated in VM\n");
1580 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1583 static void do_acl(Monitor *mon,
1584 const char *command,
1585 const char *aclname,
1592 acl = qemu_acl_find(aclname);
1594 monitor_printf(mon, "acl: unknown list '%s'\n", aclname);
1598 if (strcmp(command, "show") == 0) {
1600 qemu_acl_entry *entry;
1601 monitor_printf(mon, "policy: %s\n",
1602 acl->defaultDeny ? "deny" : "allow");
1603 TAILQ_FOREACH(entry, &acl->entries, next) {
1605 monitor_printf(mon, "%d: %s %s\n", i,
1606 entry->deny ? "deny" : "allow",
1609 } else if (strcmp(command, "reset") == 0) {
1610 qemu_acl_reset(acl);
1611 monitor_printf(mon, "acl: removed all rules\n");
1612 } else if (strcmp(command, "policy") == 0) {
1614 monitor_printf(mon, "acl: missing policy parameter\n");
1618 if (strcmp(match, "allow") == 0) {
1619 acl->defaultDeny = 0;
1620 monitor_printf(mon, "acl: policy set to 'allow'\n");
1621 } else if (strcmp(match, "deny") == 0) {
1622 acl->defaultDeny = 1;
1623 monitor_printf(mon, "acl: policy set to 'deny'\n");
1625 monitor_printf(mon, "acl: unknown policy '%s', expected 'deny' or 'allow'\n", match);
1627 } else if ((strcmp(command, "allow") == 0) ||
1628 (strcmp(command, "deny") == 0)) {
1629 int deny = strcmp(command, "deny") == 0 ? 1 : 0;
1633 monitor_printf(mon, "acl: missing match parameter\n");
1638 ret = qemu_acl_insert(acl, deny, match, index);
1640 ret = qemu_acl_append(acl, deny, match);
1642 monitor_printf(mon, "acl: unable to add acl entry\n");
1644 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1645 } else if (strcmp(command, "remove") == 0) {
1649 monitor_printf(mon, "acl: missing match parameter\n");
1653 ret = qemu_acl_remove(acl, match);
1655 monitor_printf(mon, "acl: no matching acl entry\n");
1657 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1659 monitor_printf(mon, "acl: unknown command '%s'\n", command);
1663 /* Please update qemu-doc.texi when adding or changing commands */
1664 static const mon_cmd_t mon_cmds[] = {
1665 { "help|?", "s?", help_cmd,
1666 "[cmd]", "show the help" },
1667 { "commit", "s", do_commit,
1668 "device|all", "commit changes to the disk images (if -snapshot is used) or backing files" },
1669 { "info", "s?", do_info,
1670 "[subcommand]", "show various information about the system state" },
1671 { "q|quit", "", do_quit,
1672 "", "quit the emulator" },
1673 { "eject", "-fB", do_eject,
1674 "[-f] device", "eject a removable medium (use -f to force it)" },
1675 { "change", "BFs?", do_change,
1676 "device filename [format]", "change a removable medium, optional format" },
1677 { "screendump", "F", do_screen_dump,
1678 "filename", "save screen into PPM image 'filename'" },
1679 { "logfile", "F", do_logfile,
1680 "filename", "output logs to 'filename'" },
1681 { "log", "s", do_log,
1682 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
1683 { "savevm", "s?", do_savevm,
1684 "[tag|id]", "save a VM snapshot. If no tag or id are provided, a new snapshot is created" },
1685 { "loadvm", "s", do_loadvm,
1686 "tag|id", "restore a VM snapshot from its tag or id" },
1687 { "delvm", "s", do_delvm,
1688 "tag|id", "delete a VM snapshot from its tag or id" },
1689 { "singlestep", "s?", do_singlestep,
1690 "[on|off]", "run emulation in singlestep mode or switch to normal mode", },
1691 { "stop", "", do_stop,
1692 "", "stop emulation", },
1693 { "c|cont", "", do_cont,
1694 "", "resume emulation", },
1695 #ifdef CONFIG_GDBSTUB
1696 { "gdbserver", "s?", do_gdbserver,
1697 "[device]", "start gdbserver on given device (default 'tcp::1234'), stop with 'none'", },
1699 { "x", "/l", do_memory_dump,
1700 "/fmt addr", "virtual memory dump starting at 'addr'", },
1701 { "xp", "/l", do_physical_memory_dump,
1702 "/fmt addr", "physical memory dump starting at 'addr'", },
1703 { "p|print", "/l", do_print,
1704 "/fmt expr", "print expression value (use $reg for CPU register access)", },
1705 { "i", "/ii.", do_ioport_read,
1706 "/fmt addr", "I/O port read" },
1708 { "sendkey", "si?", do_sendkey,
1709 "keys [hold_ms]", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1', default hold time=100 ms)" },
1710 { "system_reset", "", do_system_reset,
1711 "", "reset the system" },
1712 { "system_powerdown", "", do_system_powerdown,
1713 "", "send system power down event" },
1714 { "sum", "ii", do_sum,
1715 "addr size", "compute the checksum of a memory region" },
1716 { "usb_add", "s", do_usb_add,
1717 "device", "add USB device (e.g. 'host:bus.addr' or 'host:vendor_id:product_id')" },
1718 { "usb_del", "s", do_usb_del,
1719 "device", "remove USB device 'bus.addr'" },
1720 { "cpu", "i", do_cpu_set,
1721 "index", "set the default CPU" },
1722 { "mouse_move", "sss?", do_mouse_move,
1723 "dx dy [dz]", "send mouse move events" },
1724 { "mouse_button", "i", do_mouse_button,
1725 "state", "change mouse button state (1=L, 2=M, 4=R)" },
1726 { "mouse_set", "i", do_mouse_set,
1727 "index", "set which mouse device receives events" },
1729 { "wavcapture", "si?i?i?", do_wav_capture,
1730 "path [frequency [bits [channels]]]",
1731 "capture audio to a wave file (default frequency=44100 bits=16 channels=2)" },
1733 { "stopcapture", "i", do_stop_capture,
1734 "capture index", "stop capture" },
1735 { "memsave", "lis", do_memory_save,
1736 "addr size file", "save to disk virtual memory dump starting at 'addr' of size 'size'", },
1737 { "pmemsave", "lis", do_physical_memory_save,
1738 "addr size file", "save to disk physical memory dump starting at 'addr' of size 'size'", },
1739 { "boot_set", "s", do_boot_set,
1740 "bootdevice", "define new values for the boot device list" },
1741 #if defined(TARGET_I386)
1742 { "nmi", "i", do_inject_nmi,
1743 "cpu", "inject an NMI on the given CPU", },
1745 { "migrate", "-ds", do_migrate,
1746 "[-d] uri", "migrate to URI (using -d to not wait for completion)" },
1747 { "migrate_cancel", "", do_migrate_cancel,
1748 "", "cancel the current VM migration" },
1749 { "migrate_set_speed", "s", do_migrate_set_speed,
1750 "value", "set maximum speed (in bytes) for migrations" },
1751 #if defined(TARGET_I386)
1752 { "drive_add", "ss", drive_hot_add, "pci_addr=[[<domain>:]<bus>:]<slot>\n"
1753 "[file=file][,if=type][,bus=n]\n"
1754 "[,unit=m][,media=d][index=i]\n"
1755 "[,cyls=c,heads=h,secs=s[,trans=t]]\n"
1756 "[snapshot=on|off][,cache=on|off]",
1757 "add drive to PCI storage controller" },
1758 { "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" },
1759 { "pci_del", "s", pci_device_hot_remove, "pci_addr=[[<domain>:]<bus>:]<slot>", "hot remove PCI device" },
1761 { "host_net_add", "ss?", net_host_device_add,
1762 "tap|user|socket|vde|dump [options]", "add host VLAN client" },
1763 { "host_net_remove", "is", net_host_device_remove,
1764 "vlan_id name", "remove host VLAN client" },
1766 { "host_net_redir", "s", net_slirp_redir,
1767 "[tcp|udp]:host-port:[guest-host]:guest-port", "redirect TCP or UDP connections from host to guest (requires -net user)" },
1769 { "balloon", "i", do_balloon,
1770 "target", "request VM to change it's memory allocation (in MB)" },
1771 { "set_link", "ss", do_set_link,
1772 "name up|down", "change the link status of a network adapter" },
1773 { "watchdog_action", "s", do_watchdog_action,
1774 "[reset|shutdown|poweroff|pause|debug|none]", "change watchdog action" },
1775 { "acl", "sss?i?", do_acl, "<command> <aclname> [<match> [<index>]]\n",
1776 "acl show vnc.username\n"
1777 "acl policy vnc.username deny\n"
1778 "acl allow vnc.username fred\n"
1779 "acl deny vnc.username bob\n"
1780 "acl reset vnc.username\n" },
1784 /* Please update qemu-doc.texi when adding or changing commands */
1785 static const mon_cmd_t info_cmds[] = {
1786 { "version", "", do_info_version,
1787 "", "show the version of QEMU" },
1788 { "network", "", do_info_network,
1789 "", "show the network state" },
1790 { "chardev", "", qemu_chr_info,
1791 "", "show the character devices" },
1792 { "block", "", bdrv_info,
1793 "", "show the block devices" },
1794 { "blockstats", "", bdrv_info_stats,
1795 "", "show block device statistics" },
1796 { "registers", "", do_info_registers,
1797 "", "show the cpu registers" },
1798 { "cpus", "", do_info_cpus,
1799 "", "show infos for each CPU" },
1800 { "history", "", do_info_history,
1801 "", "show the command line history", },
1802 { "irq", "", irq_info,
1803 "", "show the interrupts statistics (if available)", },
1804 { "pic", "", pic_info,
1805 "", "show i8259 (PIC) state", },
1806 { "pci", "", pci_info,
1807 "", "show PCI info", },
1808 #if defined(TARGET_I386) || defined(TARGET_SH4)
1809 { "tlb", "", tlb_info,
1810 "", "show virtual to physical memory mappings", },
1812 #if defined(TARGET_I386)
1813 { "mem", "", mem_info,
1814 "", "show the active virtual memory mappings", },
1815 { "hpet", "", do_info_hpet,
1816 "", "show state of HPET", },
1818 { "jit", "", do_info_jit,
1819 "", "show dynamic compiler info", },
1820 { "kqemu", "", do_info_kqemu,
1821 "", "show KQEMU information", },
1822 { "kvm", "", do_info_kvm,
1823 "", "show KVM information", },
1824 { "numa", "", do_info_numa,
1825 "", "show NUMA information", },
1826 { "usb", "", usb_info,
1827 "", "show guest USB devices", },
1828 { "usbhost", "", usb_host_info,
1829 "", "show host USB devices", },
1830 { "profile", "", do_info_profile,
1831 "", "show profiling information", },
1832 { "capture", "", do_info_capture,
1833 "", "show capture information" },
1834 { "snapshots", "", do_info_snapshots,
1835 "", "show the currently saved VM snapshots" },
1836 { "status", "", do_info_status,
1837 "", "show the current VM status (running|paused)" },
1838 { "pcmcia", "", pcmcia_info,
1839 "", "show guest PCMCIA status" },
1840 { "mice", "", do_info_mice,
1841 "", "show which guest mouse is receiving events" },
1842 { "vnc", "", do_info_vnc,
1843 "", "show the vnc server status"},
1844 { "name", "", do_info_name,
1845 "", "show the current VM name" },
1846 { "uuid", "", do_info_uuid,
1847 "", "show the current VM UUID" },
1848 #if defined(TARGET_PPC)
1849 { "cpustats", "", do_info_cpu_stats,
1850 "", "show CPU statistics", },
1852 #if defined(CONFIG_SLIRP)
1853 { "slirp", "", do_info_slirp,
1854 "", "show SLIRP statistics", },
1856 { "migrate", "", do_info_migrate, "", "show migration status" },
1857 { "balloon", "", do_info_balloon,
1858 "", "show balloon information" },
1862 /*******************************************************************/
1864 static const char *pch;
1865 static jmp_buf expr_env;
1870 typedef struct MonitorDef {
1873 target_long (*get_value)(const struct MonitorDef *md, int val);
1877 #if defined(TARGET_I386)
1878 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1880 CPUState *env = mon_get_cpu();
1883 return env->eip + env->segs[R_CS].base;
1887 #if defined(TARGET_PPC)
1888 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1890 CPUState *env = mon_get_cpu();
1898 for (i = 0; i < 8; i++)
1899 u |= env->crf[i] << (32 - (4 * i));
1904 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1906 CPUState *env = mon_get_cpu();
1912 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1914 CPUState *env = mon_get_cpu();
1920 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1922 CPUState *env = mon_get_cpu();
1925 return cpu_ppc_load_decr(env);
1928 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1930 CPUState *env = mon_get_cpu();
1933 return cpu_ppc_load_tbu(env);
1936 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1938 CPUState *env = mon_get_cpu();
1941 return cpu_ppc_load_tbl(env);
1945 #if defined(TARGET_SPARC)
1946 #ifndef TARGET_SPARC64
1947 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1949 CPUState *env = mon_get_cpu();
1952 return GET_PSR(env);
1956 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1958 CPUState *env = mon_get_cpu();
1961 return env->regwptr[val];
1965 static const MonitorDef monitor_defs[] = {
1968 #define SEG(name, seg) \
1969 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1970 { name ".base", offsetof(CPUState, segs[seg].base) },\
1971 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1973 { "eax", offsetof(CPUState, regs[0]) },
1974 { "ecx", offsetof(CPUState, regs[1]) },
1975 { "edx", offsetof(CPUState, regs[2]) },
1976 { "ebx", offsetof(CPUState, regs[3]) },
1977 { "esp|sp", offsetof(CPUState, regs[4]) },
1978 { "ebp|fp", offsetof(CPUState, regs[5]) },
1979 { "esi", offsetof(CPUState, regs[6]) },
1980 { "edi", offsetof(CPUState, regs[7]) },
1981 #ifdef TARGET_X86_64
1982 { "r8", offsetof(CPUState, regs[8]) },
1983 { "r9", offsetof(CPUState, regs[9]) },
1984 { "r10", offsetof(CPUState, regs[10]) },
1985 { "r11", offsetof(CPUState, regs[11]) },
1986 { "r12", offsetof(CPUState, regs[12]) },
1987 { "r13", offsetof(CPUState, regs[13]) },
1988 { "r14", offsetof(CPUState, regs[14]) },
1989 { "r15", offsetof(CPUState, regs[15]) },
1991 { "eflags", offsetof(CPUState, eflags) },
1992 { "eip", offsetof(CPUState, eip) },
1999 { "pc", 0, monitor_get_pc, },
2000 #elif defined(TARGET_PPC)
2001 /* General purpose registers */
2002 { "r0", offsetof(CPUState, gpr[0]) },
2003 { "r1", offsetof(CPUState, gpr[1]) },
2004 { "r2", offsetof(CPUState, gpr[2]) },
2005 { "r3", offsetof(CPUState, gpr[3]) },
2006 { "r4", offsetof(CPUState, gpr[4]) },
2007 { "r5", offsetof(CPUState, gpr[5]) },
2008 { "r6", offsetof(CPUState, gpr[6]) },
2009 { "r7", offsetof(CPUState, gpr[7]) },
2010 { "r8", offsetof(CPUState, gpr[8]) },
2011 { "r9", offsetof(CPUState, gpr[9]) },
2012 { "r10", offsetof(CPUState, gpr[10]) },
2013 { "r11", offsetof(CPUState, gpr[11]) },
2014 { "r12", offsetof(CPUState, gpr[12]) },
2015 { "r13", offsetof(CPUState, gpr[13]) },
2016 { "r14", offsetof(CPUState, gpr[14]) },
2017 { "r15", offsetof(CPUState, gpr[15]) },
2018 { "r16", offsetof(CPUState, gpr[16]) },
2019 { "r17", offsetof(CPUState, gpr[17]) },
2020 { "r18", offsetof(CPUState, gpr[18]) },
2021 { "r19", offsetof(CPUState, gpr[19]) },
2022 { "r20", offsetof(CPUState, gpr[20]) },
2023 { "r21", offsetof(CPUState, gpr[21]) },
2024 { "r22", offsetof(CPUState, gpr[22]) },
2025 { "r23", offsetof(CPUState, gpr[23]) },
2026 { "r24", offsetof(CPUState, gpr[24]) },
2027 { "r25", offsetof(CPUState, gpr[25]) },
2028 { "r26", offsetof(CPUState, gpr[26]) },
2029 { "r27", offsetof(CPUState, gpr[27]) },
2030 { "r28", offsetof(CPUState, gpr[28]) },
2031 { "r29", offsetof(CPUState, gpr[29]) },
2032 { "r30", offsetof(CPUState, gpr[30]) },
2033 { "r31", offsetof(CPUState, gpr[31]) },
2034 /* Floating point registers */
2035 { "f0", offsetof(CPUState, fpr[0]) },
2036 { "f1", offsetof(CPUState, fpr[1]) },
2037 { "f2", offsetof(CPUState, fpr[2]) },
2038 { "f3", offsetof(CPUState, fpr[3]) },
2039 { "f4", offsetof(CPUState, fpr[4]) },
2040 { "f5", offsetof(CPUState, fpr[5]) },
2041 { "f6", offsetof(CPUState, fpr[6]) },
2042 { "f7", offsetof(CPUState, fpr[7]) },
2043 { "f8", offsetof(CPUState, fpr[8]) },
2044 { "f9", offsetof(CPUState, fpr[9]) },
2045 { "f10", offsetof(CPUState, fpr[10]) },
2046 { "f11", offsetof(CPUState, fpr[11]) },
2047 { "f12", offsetof(CPUState, fpr[12]) },
2048 { "f13", offsetof(CPUState, fpr[13]) },
2049 { "f14", offsetof(CPUState, fpr[14]) },
2050 { "f15", offsetof(CPUState, fpr[15]) },
2051 { "f16", offsetof(CPUState, fpr[16]) },
2052 { "f17", offsetof(CPUState, fpr[17]) },
2053 { "f18", offsetof(CPUState, fpr[18]) },
2054 { "f19", offsetof(CPUState, fpr[19]) },
2055 { "f20", offsetof(CPUState, fpr[20]) },
2056 { "f21", offsetof(CPUState, fpr[21]) },
2057 { "f22", offsetof(CPUState, fpr[22]) },
2058 { "f23", offsetof(CPUState, fpr[23]) },
2059 { "f24", offsetof(CPUState, fpr[24]) },
2060 { "f25", offsetof(CPUState, fpr[25]) },
2061 { "f26", offsetof(CPUState, fpr[26]) },
2062 { "f27", offsetof(CPUState, fpr[27]) },
2063 { "f28", offsetof(CPUState, fpr[28]) },
2064 { "f29", offsetof(CPUState, fpr[29]) },
2065 { "f30", offsetof(CPUState, fpr[30]) },
2066 { "f31", offsetof(CPUState, fpr[31]) },
2067 { "fpscr", offsetof(CPUState, fpscr) },
2068 /* Next instruction pointer */
2069 { "nip|pc", offsetof(CPUState, nip) },
2070 { "lr", offsetof(CPUState, lr) },
2071 { "ctr", offsetof(CPUState, ctr) },
2072 { "decr", 0, &monitor_get_decr, },
2073 { "ccr", 0, &monitor_get_ccr, },
2074 /* Machine state register */
2075 { "msr", 0, &monitor_get_msr, },
2076 { "xer", 0, &monitor_get_xer, },
2077 { "tbu", 0, &monitor_get_tbu, },
2078 { "tbl", 0, &monitor_get_tbl, },
2079 #if defined(TARGET_PPC64)
2080 /* Address space register */
2081 { "asr", offsetof(CPUState, asr) },
2083 /* Segment registers */
2084 { "sdr1", offsetof(CPUState, sdr1) },
2085 { "sr0", offsetof(CPUState, sr[0]) },
2086 { "sr1", offsetof(CPUState, sr[1]) },
2087 { "sr2", offsetof(CPUState, sr[2]) },
2088 { "sr3", offsetof(CPUState, sr[3]) },
2089 { "sr4", offsetof(CPUState, sr[4]) },
2090 { "sr5", offsetof(CPUState, sr[5]) },
2091 { "sr6", offsetof(CPUState, sr[6]) },
2092 { "sr7", offsetof(CPUState, sr[7]) },
2093 { "sr8", offsetof(CPUState, sr[8]) },
2094 { "sr9", offsetof(CPUState, sr[9]) },
2095 { "sr10", offsetof(CPUState, sr[10]) },
2096 { "sr11", offsetof(CPUState, sr[11]) },
2097 { "sr12", offsetof(CPUState, sr[12]) },
2098 { "sr13", offsetof(CPUState, sr[13]) },
2099 { "sr14", offsetof(CPUState, sr[14]) },
2100 { "sr15", offsetof(CPUState, sr[15]) },
2101 /* Too lazy to put BATs and SPRs ... */
2102 #elif defined(TARGET_SPARC)
2103 { "g0", offsetof(CPUState, gregs[0]) },
2104 { "g1", offsetof(CPUState, gregs[1]) },
2105 { "g2", offsetof(CPUState, gregs[2]) },
2106 { "g3", offsetof(CPUState, gregs[3]) },
2107 { "g4", offsetof(CPUState, gregs[4]) },
2108 { "g5", offsetof(CPUState, gregs[5]) },
2109 { "g6", offsetof(CPUState, gregs[6]) },
2110 { "g7", offsetof(CPUState, gregs[7]) },
2111 { "o0", 0, monitor_get_reg },
2112 { "o1", 1, monitor_get_reg },
2113 { "o2", 2, monitor_get_reg },
2114 { "o3", 3, monitor_get_reg },
2115 { "o4", 4, monitor_get_reg },
2116 { "o5", 5, monitor_get_reg },
2117 { "o6", 6, monitor_get_reg },
2118 { "o7", 7, monitor_get_reg },
2119 { "l0", 8, monitor_get_reg },
2120 { "l1", 9, monitor_get_reg },
2121 { "l2", 10, monitor_get_reg },
2122 { "l3", 11, monitor_get_reg },
2123 { "l4", 12, monitor_get_reg },
2124 { "l5", 13, monitor_get_reg },
2125 { "l6", 14, monitor_get_reg },
2126 { "l7", 15, monitor_get_reg },
2127 { "i0", 16, monitor_get_reg },
2128 { "i1", 17, monitor_get_reg },
2129 { "i2", 18, monitor_get_reg },
2130 { "i3", 19, monitor_get_reg },
2131 { "i4", 20, monitor_get_reg },
2132 { "i5", 21, monitor_get_reg },
2133 { "i6", 22, monitor_get_reg },
2134 { "i7", 23, monitor_get_reg },
2135 { "pc", offsetof(CPUState, pc) },
2136 { "npc", offsetof(CPUState, npc) },
2137 { "y", offsetof(CPUState, y) },
2138 #ifndef TARGET_SPARC64
2139 { "psr", 0, &monitor_get_psr, },
2140 { "wim", offsetof(CPUState, wim) },
2142 { "tbr", offsetof(CPUState, tbr) },
2143 { "fsr", offsetof(CPUState, fsr) },
2144 { "f0", offsetof(CPUState, fpr[0]) },
2145 { "f1", offsetof(CPUState, fpr[1]) },
2146 { "f2", offsetof(CPUState, fpr[2]) },
2147 { "f3", offsetof(CPUState, fpr[3]) },
2148 { "f4", offsetof(CPUState, fpr[4]) },
2149 { "f5", offsetof(CPUState, fpr[5]) },
2150 { "f6", offsetof(CPUState, fpr[6]) },
2151 { "f7", offsetof(CPUState, fpr[7]) },
2152 { "f8", offsetof(CPUState, fpr[8]) },
2153 { "f9", offsetof(CPUState, fpr[9]) },
2154 { "f10", offsetof(CPUState, fpr[10]) },
2155 { "f11", offsetof(CPUState, fpr[11]) },
2156 { "f12", offsetof(CPUState, fpr[12]) },
2157 { "f13", offsetof(CPUState, fpr[13]) },
2158 { "f14", offsetof(CPUState, fpr[14]) },
2159 { "f15", offsetof(CPUState, fpr[15]) },
2160 { "f16", offsetof(CPUState, fpr[16]) },
2161 { "f17", offsetof(CPUState, fpr[17]) },
2162 { "f18", offsetof(CPUState, fpr[18]) },
2163 { "f19", offsetof(CPUState, fpr[19]) },
2164 { "f20", offsetof(CPUState, fpr[20]) },
2165 { "f21", offsetof(CPUState, fpr[21]) },
2166 { "f22", offsetof(CPUState, fpr[22]) },
2167 { "f23", offsetof(CPUState, fpr[23]) },
2168 { "f24", offsetof(CPUState, fpr[24]) },
2169 { "f25", offsetof(CPUState, fpr[25]) },
2170 { "f26", offsetof(CPUState, fpr[26]) },
2171 { "f27", offsetof(CPUState, fpr[27]) },
2172 { "f28", offsetof(CPUState, fpr[28]) },
2173 { "f29", offsetof(CPUState, fpr[29]) },
2174 { "f30", offsetof(CPUState, fpr[30]) },
2175 { "f31", offsetof(CPUState, fpr[31]) },
2176 #ifdef TARGET_SPARC64
2177 { "f32", offsetof(CPUState, fpr[32]) },
2178 { "f34", offsetof(CPUState, fpr[34]) },
2179 { "f36", offsetof(CPUState, fpr[36]) },
2180 { "f38", offsetof(CPUState, fpr[38]) },
2181 { "f40", offsetof(CPUState, fpr[40]) },
2182 { "f42", offsetof(CPUState, fpr[42]) },
2183 { "f44", offsetof(CPUState, fpr[44]) },
2184 { "f46", offsetof(CPUState, fpr[46]) },
2185 { "f48", offsetof(CPUState, fpr[48]) },
2186 { "f50", offsetof(CPUState, fpr[50]) },
2187 { "f52", offsetof(CPUState, fpr[52]) },
2188 { "f54", offsetof(CPUState, fpr[54]) },
2189 { "f56", offsetof(CPUState, fpr[56]) },
2190 { "f58", offsetof(CPUState, fpr[58]) },
2191 { "f60", offsetof(CPUState, fpr[60]) },
2192 { "f62", offsetof(CPUState, fpr[62]) },
2193 { "asi", offsetof(CPUState, asi) },
2194 { "pstate", offsetof(CPUState, pstate) },
2195 { "cansave", offsetof(CPUState, cansave) },
2196 { "canrestore", offsetof(CPUState, canrestore) },
2197 { "otherwin", offsetof(CPUState, otherwin) },
2198 { "wstate", offsetof(CPUState, wstate) },
2199 { "cleanwin", offsetof(CPUState, cleanwin) },
2200 { "fprs", offsetof(CPUState, fprs) },
2206 static void expr_error(Monitor *mon, const char *msg)
2208 monitor_printf(mon, "%s\n", msg);
2209 longjmp(expr_env, 1);
2212 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2213 static int get_monitor_def(target_long *pval, const char *name)
2215 const MonitorDef *md;
2218 for(md = monitor_defs; md->name != NULL; md++) {
2219 if (compare_cmd(name, md->name)) {
2220 if (md->get_value) {
2221 *pval = md->get_value(md, md->offset);
2223 CPUState *env = mon_get_cpu();
2226 ptr = (uint8_t *)env + md->offset;
2229 *pval = *(int32_t *)ptr;
2232 *pval = *(target_long *)ptr;
2245 static void next(void)
2249 while (qemu_isspace(*pch))
2254 static int64_t expr_sum(Monitor *mon);
2256 static int64_t expr_unary(Monitor *mon)
2265 n = expr_unary(mon);
2269 n = -expr_unary(mon);
2273 n = ~expr_unary(mon);
2279 expr_error(mon, "')' expected");
2286 expr_error(mon, "character constant expected");
2290 expr_error(mon, "missing terminating \' character");
2300 while ((*pch >= 'a' && *pch <= 'z') ||
2301 (*pch >= 'A' && *pch <= 'Z') ||
2302 (*pch >= '0' && *pch <= '9') ||
2303 *pch == '_' || *pch == '.') {
2304 if ((q - buf) < sizeof(buf) - 1)
2308 while (qemu_isspace(*pch))
2311 ret = get_monitor_def(®, buf);
2313 expr_error(mon, "unknown register");
2315 expr_error(mon, "no cpu defined");
2320 expr_error(mon, "unexpected end of expression");
2324 #if TARGET_PHYS_ADDR_BITS > 32
2325 n = strtoull(pch, &p, 0);
2327 n = strtoul(pch, &p, 0);
2330 expr_error(mon, "invalid char in expression");
2333 while (qemu_isspace(*pch))
2341 static int64_t expr_prod(Monitor *mon)
2346 val = expr_unary(mon);
2349 if (op != '*' && op != '/' && op != '%')
2352 val2 = expr_unary(mon);
2361 expr_error(mon, "division by zero");
2372 static int64_t expr_logic(Monitor *mon)
2377 val = expr_prod(mon);
2380 if (op != '&' && op != '|' && op != '^')
2383 val2 = expr_prod(mon);
2400 static int64_t expr_sum(Monitor *mon)
2405 val = expr_logic(mon);
2408 if (op != '+' && op != '-')
2411 val2 = expr_logic(mon);
2420 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2423 if (setjmp(expr_env)) {
2427 while (qemu_isspace(*pch))
2429 *pval = expr_sum(mon);
2434 static int get_str(char *buf, int buf_size, const char **pp)
2442 while (qemu_isspace(*p))
2452 while (*p != '\0' && *p != '\"') {
2468 qemu_printf("unsupported escape code: '\\%c'\n", c);
2471 if ((q - buf) < buf_size - 1) {
2475 if ((q - buf) < buf_size - 1) {
2482 qemu_printf("unterminated string\n");
2487 while (*p != '\0' && !qemu_isspace(*p)) {
2488 if ((q - buf) < buf_size - 1) {
2499 static int default_fmt_format = 'x';
2500 static int default_fmt_size = 4;
2504 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2506 const char *p, *pstart, *typestr;
2508 int c, nb_args, len, i, has_arg;
2509 const mon_cmd_t *cmd;
2512 void *str_allocated[MAX_ARGS];
2513 void *args[MAX_ARGS];
2514 void (*handler_0)(Monitor *mon);
2515 void (*handler_1)(Monitor *mon, void *arg0);
2516 void (*handler_2)(Monitor *mon, void *arg0, void *arg1);
2517 void (*handler_3)(Monitor *mon, void *arg0, void *arg1, void *arg2);
2518 void (*handler_4)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2520 void (*handler_5)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2521 void *arg3, void *arg4);
2522 void (*handler_6)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2523 void *arg3, void *arg4, void *arg5);
2524 void (*handler_7)(Monitor *mon, void *arg0, void *arg1, void *arg2,
2525 void *arg3, void *arg4, void *arg5, void *arg6);
2528 monitor_printf(mon, "command='%s'\n", cmdline);
2531 /* extract the command name */
2534 while (qemu_isspace(*p))
2539 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2542 if (len > sizeof(cmdname) - 1)
2543 len = sizeof(cmdname) - 1;
2544 memcpy(cmdname, pstart, len);
2545 cmdname[len] = '\0';
2547 /* find the command */
2548 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2549 if (compare_cmd(cmdname, cmd->name))
2552 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2556 for(i = 0; i < MAX_ARGS; i++)
2557 str_allocated[i] = NULL;
2559 /* parse the parameters */
2560 typestr = cmd->args_type;
2575 while (qemu_isspace(*p))
2577 if (*typestr == '?') {
2580 /* no optional string: NULL argument */
2585 ret = get_str(buf, sizeof(buf), &p);
2589 monitor_printf(mon, "%s: filename expected\n",
2593 monitor_printf(mon, "%s: block device name expected\n",
2597 monitor_printf(mon, "%s: string expected\n", cmdname);
2602 str = qemu_malloc(strlen(buf) + 1);
2603 pstrcpy(str, sizeof(buf), buf);
2604 str_allocated[nb_args] = str;
2606 if (nb_args >= MAX_ARGS) {
2608 monitor_printf(mon, "%s: too many arguments\n", cmdname);
2611 args[nb_args++] = str;
2616 int count, format, size;
2618 while (qemu_isspace(*p))
2624 if (qemu_isdigit(*p)) {
2626 while (qemu_isdigit(*p)) {
2627 count = count * 10 + (*p - '0');
2665 if (*p != '\0' && !qemu_isspace(*p)) {
2666 monitor_printf(mon, "invalid char in format: '%c'\n",
2671 format = default_fmt_format;
2672 if (format != 'i') {
2673 /* for 'i', not specifying a size gives -1 as size */
2675 size = default_fmt_size;
2676 default_fmt_size = size;
2678 default_fmt_format = format;
2681 format = default_fmt_format;
2682 if (format != 'i') {
2683 size = default_fmt_size;
2688 if (nb_args + 3 > MAX_ARGS)
2690 args[nb_args++] = (void*)(long)count;
2691 args[nb_args++] = (void*)(long)format;
2692 args[nb_args++] = (void*)(long)size;
2700 while (qemu_isspace(*p))
2702 if (*typestr == '?' || *typestr == '.') {
2703 if (*typestr == '?') {
2711 while (qemu_isspace(*p))
2719 if (nb_args >= MAX_ARGS)
2721 args[nb_args++] = (void *)(long)has_arg;
2723 if (nb_args >= MAX_ARGS)
2729 if (get_expr(mon, &val, &p))
2733 if (nb_args >= MAX_ARGS)
2735 args[nb_args++] = (void *)(long)val;
2737 if ((nb_args + 1) >= MAX_ARGS)
2739 #if TARGET_PHYS_ADDR_BITS > 32
2740 args[nb_args++] = (void *)(long)((val >> 32) & 0xffffffff);
2742 args[nb_args++] = (void *)0;
2744 args[nb_args++] = (void *)(long)(val & 0xffffffff);
2756 while (qemu_isspace(*p))
2762 monitor_printf(mon, "%s: unsupported option -%c\n",
2769 if (nb_args >= MAX_ARGS)
2771 args[nb_args++] = (void *)(long)has_option;
2776 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2780 /* check that all arguments were parsed */
2781 while (qemu_isspace(*p))
2784 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2791 handler_0 = cmd->handler;
2795 handler_1 = cmd->handler;
2796 handler_1(mon, args[0]);
2799 handler_2 = cmd->handler;
2800 handler_2(mon, args[0], args[1]);
2803 handler_3 = cmd->handler;
2804 handler_3(mon, args[0], args[1], args[2]);
2807 handler_4 = cmd->handler;
2808 handler_4(mon, args[0], args[1], args[2], args[3]);
2811 handler_5 = cmd->handler;
2812 handler_5(mon, args[0], args[1], args[2], args[3], args[4]);
2815 handler_6 = cmd->handler;
2816 handler_6(mon, args[0], args[1], args[2], args[3], args[4], args[5]);
2819 handler_7 = cmd->handler;
2820 handler_7(mon, args[0], args[1], args[2], args[3], args[4], args[5],
2824 monitor_printf(mon, "unsupported number of arguments: %d\n", nb_args);
2828 for(i = 0; i < MAX_ARGS; i++)
2829 qemu_free(str_allocated[i]);
2833 static void cmd_completion(const char *name, const char *list)
2835 const char *p, *pstart;
2844 p = pstart + strlen(pstart);
2846 if (len > sizeof(cmd) - 2)
2847 len = sizeof(cmd) - 2;
2848 memcpy(cmd, pstart, len);
2850 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2851 readline_add_completion(cur_mon->rs, cmd);
2859 static void file_completion(const char *input)
2864 char file[1024], file_prefix[1024];
2868 p = strrchr(input, '/');
2871 pstrcpy(file_prefix, sizeof(file_prefix), input);
2872 pstrcpy(path, sizeof(path), ".");
2874 input_path_len = p - input + 1;
2875 memcpy(path, input, input_path_len);
2876 if (input_path_len > sizeof(path) - 1)
2877 input_path_len = sizeof(path) - 1;
2878 path[input_path_len] = '\0';
2879 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2881 #ifdef DEBUG_COMPLETION
2882 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2883 input, path, file_prefix);
2885 ffs = opendir(path);
2893 if (strstart(d->d_name, file_prefix, NULL)) {
2894 memcpy(file, input, input_path_len);
2895 if (input_path_len < sizeof(file))
2896 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2898 /* stat the file to find out if it's a directory.
2899 * In that case add a slash to speed up typing long paths
2902 if(S_ISDIR(sb.st_mode))
2903 pstrcat(file, sizeof(file), "/");
2904 readline_add_completion(cur_mon->rs, file);
2910 static void block_completion_it(void *opaque, BlockDriverState *bs)
2912 const char *name = bdrv_get_device_name(bs);
2913 const char *input = opaque;
2915 if (input[0] == '\0' ||
2916 !strncmp(name, (char *)input, strlen(input))) {
2917 readline_add_completion(cur_mon->rs, name);
2921 /* NOTE: this parser is an approximate form of the real command parser */
2922 static void parse_cmdline(const char *cmdline,
2923 int *pnb_args, char **args)
2932 while (qemu_isspace(*p))
2936 if (nb_args >= MAX_ARGS)
2938 ret = get_str(buf, sizeof(buf), &p);
2939 args[nb_args] = qemu_strdup(buf);
2944 *pnb_args = nb_args;
2947 static void monitor_find_completion(const char *cmdline)
2949 const char *cmdname;
2950 char *args[MAX_ARGS];
2951 int nb_args, i, len;
2952 const char *ptype, *str;
2953 const mon_cmd_t *cmd;
2956 parse_cmdline(cmdline, &nb_args, args);
2957 #ifdef DEBUG_COMPLETION
2958 for(i = 0; i < nb_args; i++) {
2959 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2963 /* if the line ends with a space, it means we want to complete the
2965 len = strlen(cmdline);
2966 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2967 if (nb_args >= MAX_ARGS)
2969 args[nb_args++] = qemu_strdup("");
2972 /* command completion */
2977 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2978 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2979 cmd_completion(cmdname, cmd->name);
2982 /* find the command */
2983 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2984 if (compare_cmd(args[0], cmd->name))
2989 ptype = cmd->args_type;
2990 for(i = 0; i < nb_args - 2; i++) {
2991 if (*ptype != '\0') {
2993 while (*ptype == '?')
2997 str = args[nb_args - 1];
3000 /* file completion */
3001 readline_set_completion_index(cur_mon->rs, strlen(str));
3002 file_completion(str);
3005 /* block device name completion */
3006 readline_set_completion_index(cur_mon->rs, strlen(str));
3007 bdrv_iterate(block_completion_it, (void *)str);
3010 /* XXX: more generic ? */
3011 if (!strcmp(cmd->name, "info")) {
3012 readline_set_completion_index(cur_mon->rs, strlen(str));
3013 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3014 cmd_completion(str, cmd->name);
3016 } else if (!strcmp(cmd->name, "sendkey")) {
3017 char *sep = strrchr(str, '-');
3020 readline_set_completion_index(cur_mon->rs, strlen(str));
3021 for(key = key_defs; key->name != NULL; key++) {
3022 cmd_completion(str, key->name);
3030 for(i = 0; i < nb_args; i++)
3034 static int monitor_can_read(void *opaque)
3036 Monitor *mon = opaque;
3038 return (mon->suspend_cnt == 0) ? 128 : 0;
3041 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3043 Monitor *old_mon = cur_mon;
3049 for (i = 0; i < size; i++)
3050 readline_handle_byte(cur_mon->rs, buf[i]);
3052 if (size == 0 || buf[size - 1] != 0)
3053 monitor_printf(cur_mon, "corrupted command\n");
3055 monitor_handle_command(cur_mon, (char *)buf);
3061 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3063 monitor_suspend(mon);
3064 monitor_handle_command(mon, cmdline);
3065 monitor_resume(mon);
3068 int monitor_suspend(Monitor *mon)
3076 void monitor_resume(Monitor *mon)
3080 if (--mon->suspend_cnt == 0)
3081 readline_show_prompt(mon->rs);
3084 static void monitor_event(void *opaque, int event)
3086 Monitor *mon = opaque;
3089 case CHR_EVENT_MUX_IN:
3090 readline_restart(mon->rs);
3091 monitor_resume(mon);
3095 case CHR_EVENT_MUX_OUT:
3096 if (mon->suspend_cnt == 0)
3097 monitor_printf(mon, "\n");
3099 monitor_suspend(mon);
3102 case CHR_EVENT_RESET:
3103 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3104 "information\n", QEMU_VERSION);
3105 if (mon->chr->focus == 0)
3106 readline_show_prompt(mon->rs);
3120 void monitor_init(CharDriverState *chr, int flags)
3122 static int is_first_init = 1;
3125 if (is_first_init) {
3126 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3130 mon = qemu_mallocz(sizeof(*mon));
3134 if (mon->chr->focus != 0)
3135 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3136 if (flags & MONITOR_USE_READLINE) {
3137 mon->rs = readline_init(mon, monitor_find_completion);
3138 monitor_read_command(mon, 0);
3141 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3144 LIST_INSERT_HEAD(&mon_list, mon, entry);
3145 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3149 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3151 BlockDriverState *bs = opaque;
3154 if (bdrv_set_key(bs, password) != 0) {
3155 monitor_printf(mon, "invalid password\n");
3158 if (mon->password_completion_cb)
3159 mon->password_completion_cb(mon->password_opaque, ret);
3161 monitor_read_command(mon, 1);
3164 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3165 BlockDriverCompletionFunc *completion_cb,
3170 if (!bdrv_key_required(bs)) {
3172 completion_cb(opaque, 0);
3176 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3177 bdrv_get_encrypted_filename(bs));
3179 mon->password_completion_cb = completion_cb;
3180 mon->password_opaque = opaque;
3182 err = monitor_read_password(mon, bdrv_password_cb, bs);
3184 if (err && completion_cb)
3185 completion_cb(opaque, err);