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
28 #include "hw/pcmcia.h"
31 #include "hw/watchdog.h"
34 #include "qemu-char.h"
40 #include "audio/audio.h"
43 #include "qemu-timer.h"
44 #include "migration.h"
52 //#define DEBUG_COMPLETION
58 * 'B' block device name
59 * 's' string (accept optional quote)
61 * 'l' target long (32 or 64 bit)
62 * '/' optional gdb-like print format (like "/10x")
64 * '?' optional type (for all types, except '/')
65 * '.' other form of optional type (for 'i' and 'l')
66 * '-' optional parameter (eg. '-f')
70 typedef struct mon_cmd_t {
72 const char *args_type;
78 /* file descriptors passed via SCM_RIGHTS */
79 typedef struct mon_fd_t mon_fd_t;
83 LIST_ENTRY(mon_fd_t) next;
94 BlockDriverCompletionFunc *password_completion_cb;
95 void *password_opaque;
96 LIST_HEAD(,mon_fd_t) fds;
97 LIST_ENTRY(Monitor) entry;
100 static LIST_HEAD(mon_list, Monitor) mon_list;
102 static const mon_cmd_t mon_cmds[];
103 static const mon_cmd_t info_cmds[];
105 Monitor *cur_mon = NULL;
107 static void monitor_command_cb(Monitor *mon, const char *cmdline,
110 static void monitor_read_command(Monitor *mon, int show_prompt)
112 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
114 readline_show_prompt(mon->rs);
117 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
121 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
122 /* prompt is printed on return from the command handler */
125 monitor_printf(mon, "terminal does not support password prompting\n");
130 void monitor_flush(Monitor *mon)
132 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
133 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
134 mon->outbuf_index = 0;
138 /* flush at every end of line or if the buffer is full */
139 static void monitor_puts(Monitor *mon, const char *str)
151 mon->outbuf[mon->outbuf_index++] = '\r';
152 mon->outbuf[mon->outbuf_index++] = c;
153 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
159 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
162 vsnprintf(buf, sizeof(buf), fmt, ap);
163 monitor_puts(mon, buf);
166 void monitor_printf(Monitor *mon, const char *fmt, ...)
170 monitor_vprintf(mon, fmt, ap);
174 void monitor_print_filename(Monitor *mon, const char *filename)
178 for (i = 0; filename[i]; i++) {
179 switch (filename[i]) {
183 monitor_printf(mon, "\\%c", filename[i]);
186 monitor_printf(mon, "\\t");
189 monitor_printf(mon, "\\r");
192 monitor_printf(mon, "\\n");
195 monitor_printf(mon, "%c", filename[i]);
201 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
205 monitor_vprintf((Monitor *)stream, fmt, ap);
210 static int compare_cmd(const char *name, const char *list)
212 const char *p, *pstart;
220 p = pstart + strlen(pstart);
221 if ((p - pstart) == len && !memcmp(pstart, name, len))
230 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
231 const char *prefix, const char *name)
233 const mon_cmd_t *cmd;
235 for(cmd = cmds; cmd->name != NULL; cmd++) {
236 if (!name || !strcmp(name, cmd->name))
237 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
238 cmd->params, cmd->help);
242 static void help_cmd(Monitor *mon, const char *name)
244 if (name && !strcmp(name, "info")) {
245 help_cmd_dump(mon, info_cmds, "info ", NULL);
247 help_cmd_dump(mon, mon_cmds, "", name);
248 if (name && !strcmp(name, "log")) {
249 const CPULogItem *item;
250 monitor_printf(mon, "Log items (comma separated):\n");
251 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
252 for(item = cpu_log_items; item->mask != 0; item++) {
253 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
259 static void do_help_cmd(Monitor *mon, const QDict *qdict)
261 help_cmd(mon, qdict_get_try_str(qdict, "name"));
264 static void do_commit(Monitor *mon, const QDict *qdict)
268 const char *device = qdict_get_str(qdict, "device");
270 all_devices = !strcmp(device, "all");
271 TAILQ_FOREACH(dinfo, &drives, next) {
273 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
275 bdrv_commit(dinfo->bdrv);
279 static void do_info(Monitor *mon, const QDict *qdict)
281 const mon_cmd_t *cmd;
282 const char *item = qdict_get_try_str(qdict, "item");
283 void (*handler)(Monitor *);
287 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
288 if (compare_cmd(item, cmd->name))
292 help_cmd(mon, "info");
295 handler = cmd->handler;
299 static void do_info_version(Monitor *mon)
301 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
304 static void do_info_name(Monitor *mon)
307 monitor_printf(mon, "%s\n", qemu_name);
310 #if defined(TARGET_I386)
311 static void do_info_hpet(Monitor *mon)
313 monitor_printf(mon, "HPET is %s by QEMU\n",
314 (no_hpet) ? "disabled" : "enabled");
318 static void do_info_uuid(Monitor *mon)
320 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
321 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
322 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
323 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
324 qemu_uuid[14], qemu_uuid[15]);
327 /* get the current CPU defined by the user */
328 static int mon_set_cpu(int cpu_index)
332 for(env = first_cpu; env != NULL; env = env->next_cpu) {
333 if (env->cpu_index == cpu_index) {
334 cur_mon->mon_cpu = env;
341 static CPUState *mon_get_cpu(void)
343 if (!cur_mon->mon_cpu) {
346 cpu_synchronize_state(cur_mon->mon_cpu);
347 return cur_mon->mon_cpu;
350 static void do_info_registers(Monitor *mon)
357 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
360 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
365 static void do_info_cpus(Monitor *mon)
369 /* just to set the default cpu if not already done */
372 for(env = first_cpu; env != NULL; env = env->next_cpu) {
373 cpu_synchronize_state(env);
374 monitor_printf(mon, "%c CPU #%d:",
375 (env == mon->mon_cpu) ? '*' : ' ',
377 #if defined(TARGET_I386)
378 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
379 env->eip + env->segs[R_CS].base);
380 #elif defined(TARGET_PPC)
381 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
382 #elif defined(TARGET_SPARC)
383 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
385 #elif defined(TARGET_MIPS)
386 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
389 monitor_printf(mon, " (halted)");
390 monitor_printf(mon, "\n");
394 static void do_cpu_set(Monitor *mon, const QDict *qdict)
396 int index = qdict_get_int(qdict, "index");
397 if (mon_set_cpu(index) < 0)
398 monitor_printf(mon, "Invalid CPU index\n");
401 static void do_info_jit(Monitor *mon)
403 dump_exec_info((FILE *)mon, monitor_fprintf);
406 static void do_info_history(Monitor *mon)
415 str = readline_get_history(mon->rs, i);
418 monitor_printf(mon, "%d: '%s'\n", i, str);
423 #if defined(TARGET_PPC)
424 /* XXX: not implemented in other targets */
425 static void do_info_cpu_stats(Monitor *mon)
430 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
434 static void do_quit(Monitor *mon, const QDict *qdict)
439 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
441 if (bdrv_is_inserted(bs)) {
443 if (!bdrv_is_removable(bs)) {
444 monitor_printf(mon, "device is not removable\n");
447 if (bdrv_is_locked(bs)) {
448 monitor_printf(mon, "device is locked\n");
457 static void do_eject(Monitor *mon, const QDict *qdict)
459 BlockDriverState *bs;
460 int force = qdict_get_int(qdict, "force");
461 const char *filename = qdict_get_str(qdict, "filename");
463 bs = bdrv_find(filename);
465 monitor_printf(mon, "device not found\n");
468 eject_device(mon, bs, force);
471 static void do_change_block(Monitor *mon, const char *device,
472 const char *filename, const char *fmt)
474 BlockDriverState *bs;
475 BlockDriver *drv = NULL;
477 bs = bdrv_find(device);
479 monitor_printf(mon, "device not found\n");
483 drv = bdrv_find_format(fmt);
485 monitor_printf(mon, "invalid format %s\n", fmt);
489 if (eject_device(mon, bs, 0) < 0)
491 bdrv_open2(bs, filename, 0, drv);
492 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
495 static void change_vnc_password_cb(Monitor *mon, const char *password,
498 if (vnc_display_password(NULL, password) < 0)
499 monitor_printf(mon, "could not set VNC server password\n");
501 monitor_read_command(mon, 1);
504 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
506 if (strcmp(target, "passwd") == 0 ||
507 strcmp(target, "password") == 0) {
510 strncpy(password, arg, sizeof(password));
511 password[sizeof(password) - 1] = '\0';
512 change_vnc_password_cb(mon, password, NULL);
514 monitor_read_password(mon, change_vnc_password_cb, NULL);
517 if (vnc_display_open(NULL, target) < 0)
518 monitor_printf(mon, "could not start VNC server on %s\n", target);
522 static void do_change(Monitor *mon, const QDict *qdict)
524 const char *device = qdict_get_str(qdict, "device");
525 const char *target = qdict_get_str(qdict, "target");
526 const char *arg = qdict_get_try_str(qdict, "arg");
527 if (strcmp(device, "vnc") == 0) {
528 do_change_vnc(mon, target, arg);
530 do_change_block(mon, device, target, arg);
534 static void do_screen_dump(Monitor *mon, const QDict *qdict)
536 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
539 static void do_logfile(Monitor *mon, const QDict *qdict)
541 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
544 static void do_log(Monitor *mon, const QDict *qdict)
547 const char *items = qdict_get_str(qdict, "items");
549 if (!strcmp(items, "none")) {
552 mask = cpu_str_to_log_mask(items);
554 help_cmd(mon, "log");
561 static void do_singlestep(Monitor *mon, const QDict *qdict)
563 const char *option = qdict_get_try_str(qdict, "option");
564 if (!option || !strcmp(option, "on")) {
566 } else if (!strcmp(option, "off")) {
569 monitor_printf(mon, "unexpected option %s\n", option);
573 static void do_stop(Monitor *mon, const QDict *qdict)
575 vm_stop(EXCP_INTERRUPT);
578 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
580 struct bdrv_iterate_context {
585 static void do_cont(Monitor *mon, const QDict *qdict)
587 struct bdrv_iterate_context context = { mon, 0 };
589 bdrv_iterate(encrypted_bdrv_it, &context);
590 /* only resume the vm if all keys are set and valid */
595 static void bdrv_key_cb(void *opaque, int err)
597 Monitor *mon = opaque;
599 /* another key was set successfully, retry to continue */
604 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
606 struct bdrv_iterate_context *context = opaque;
608 if (!context->err && bdrv_key_required(bs)) {
609 context->err = -EBUSY;
610 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
615 static void do_gdbserver(Monitor *mon, const QDict *qdict)
617 const char *device = qdict_get_try_str(qdict, "device");
619 device = "tcp::" DEFAULT_GDBSTUB_PORT;
620 if (gdbserver_start(device) < 0) {
621 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
623 } else if (strcmp(device, "none") == 0) {
624 monitor_printf(mon, "Disabled gdbserver\n");
626 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
631 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
633 const char *action = qdict_get_str(qdict, "action");
634 if (select_watchdog_action(action) == -1) {
635 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
639 static void monitor_printc(Monitor *mon, int c)
641 monitor_printf(mon, "'");
644 monitor_printf(mon, "\\'");
647 monitor_printf(mon, "\\\\");
650 monitor_printf(mon, "\\n");
653 monitor_printf(mon, "\\r");
656 if (c >= 32 && c <= 126) {
657 monitor_printf(mon, "%c", c);
659 monitor_printf(mon, "\\x%02x", c);
663 monitor_printf(mon, "'");
666 static void memory_dump(Monitor *mon, int count, int format, int wsize,
667 target_phys_addr_t addr, int is_physical)
670 int nb_per_line, l, line_size, i, max_digits, len;
678 if (!env && !is_physical)
683 } else if (wsize == 4) {
686 /* as default we use the current CS size */
690 if ((env->efer & MSR_EFER_LMA) &&
691 (env->segs[R_CS].flags & DESC_L_MASK))
695 if (!(env->segs[R_CS].flags & DESC_B_MASK))
700 monitor_disas(mon, env, addr, count, is_physical, flags);
709 nb_per_line = line_size / wsize;
714 max_digits = (wsize * 8 + 2) / 3;
718 max_digits = (wsize * 8) / 4;
722 max_digits = (wsize * 8 * 10 + 32) / 33;
731 monitor_printf(mon, TARGET_FMT_plx ":", addr);
733 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
738 cpu_physical_memory_rw(addr, buf, l, 0);
743 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
744 monitor_printf(mon, " Cannot access memory\n");
753 v = ldub_raw(buf + i);
756 v = lduw_raw(buf + i);
759 v = (uint32_t)ldl_raw(buf + i);
762 v = ldq_raw(buf + i);
765 monitor_printf(mon, " ");
768 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
771 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
774 monitor_printf(mon, "%*" PRIu64, max_digits, v);
777 monitor_printf(mon, "%*" PRId64, max_digits, v);
780 monitor_printc(mon, v);
785 monitor_printf(mon, "\n");
791 static void do_memory_dump(Monitor *mon, const QDict *qdict)
793 int count = qdict_get_int(qdict, "count");
794 int format = qdict_get_int(qdict, "format");
795 int size = qdict_get_int(qdict, "size");
796 target_long addr = qdict_get_int(qdict, "addr");
798 memory_dump(mon, count, format, size, addr, 0);
801 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
803 int count = qdict_get_int(qdict, "count");
804 int format = qdict_get_int(qdict, "format");
805 int size = qdict_get_int(qdict, "size");
806 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
808 memory_dump(mon, count, format, size, addr, 1);
811 static void do_print(Monitor *mon, const QDict *qdict)
813 int format = qdict_get_int(qdict, "format");
814 target_phys_addr_t val = qdict_get_int(qdict, "val");
816 #if TARGET_PHYS_ADDR_BITS == 32
819 monitor_printf(mon, "%#o", val);
822 monitor_printf(mon, "%#x", val);
825 monitor_printf(mon, "%u", val);
829 monitor_printf(mon, "%d", val);
832 monitor_printc(mon, val);
838 monitor_printf(mon, "%#" PRIo64, val);
841 monitor_printf(mon, "%#" PRIx64, val);
844 monitor_printf(mon, "%" PRIu64, val);
848 monitor_printf(mon, "%" PRId64, val);
851 monitor_printc(mon, val);
855 monitor_printf(mon, "\n");
858 static void do_memory_save(Monitor *mon, const QDict *qdict)
861 uint32_t size = qdict_get_int(qdict, "size");
862 const char *filename = qdict_get_str(qdict, "filename");
863 target_long addr = qdict_get_int(qdict, "val");
872 f = fopen(filename, "wb");
874 monitor_printf(mon, "could not open '%s'\n", filename);
881 cpu_memory_rw_debug(env, addr, buf, l, 0);
882 fwrite(buf, 1, l, f);
889 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
894 uint32_t size = qdict_get_int(qdict, "size");
895 const char *filename = qdict_get_str(qdict, "filename");
896 target_phys_addr_t addr = qdict_get_int(qdict, "val");
898 f = fopen(filename, "wb");
900 monitor_printf(mon, "could not open '%s'\n", filename);
907 cpu_physical_memory_rw(addr, buf, l, 0);
908 fwrite(buf, 1, l, f);
916 static void do_sum(Monitor *mon, const QDict *qdict)
921 uint32_t start = qdict_get_int(qdict, "start");
922 uint32_t size = qdict_get_int(qdict, "size");
925 for(addr = start; addr < (start + size); addr++) {
926 cpu_physical_memory_rw(addr, buf, 1, 0);
927 /* BSD sum algorithm ('sum' Unix command) */
928 sum = (sum >> 1) | (sum << 15);
931 monitor_printf(mon, "%05d\n", sum);
939 static const KeyDef key_defs[] = {
966 { 0x0e, "backspace" },
1003 { 0x37, "asterisk" },
1006 { 0x3a, "caps_lock" },
1017 { 0x45, "num_lock" },
1018 { 0x46, "scroll_lock" },
1020 { 0xb5, "kp_divide" },
1021 { 0x37, "kp_multiply" },
1022 { 0x4a, "kp_subtract" },
1024 { 0x9c, "kp_enter" },
1025 { 0x53, "kp_decimal" },
1058 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1073 { 0xfe, "compose" },
1078 static int get_keycode(const char *key)
1084 for(p = key_defs; p->name != NULL; p++) {
1085 if (!strcmp(key, p->name))
1088 if (strstart(key, "0x", NULL)) {
1089 ret = strtoul(key, &endp, 0);
1090 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1096 #define MAX_KEYCODES 16
1097 static uint8_t keycodes[MAX_KEYCODES];
1098 static int nb_pending_keycodes;
1099 static QEMUTimer *key_timer;
1101 static void release_keys(void *opaque)
1105 while (nb_pending_keycodes > 0) {
1106 nb_pending_keycodes--;
1107 keycode = keycodes[nb_pending_keycodes];
1109 kbd_put_keycode(0xe0);
1110 kbd_put_keycode(keycode | 0x80);
1114 static void do_sendkey(Monitor *mon, const QDict *qdict)
1116 char keyname_buf[16];
1118 int keyname_len, keycode, i;
1119 const char *string = qdict_get_str(qdict, "string");
1120 int has_hold_time = qdict_haskey(qdict, "hold_time");
1121 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1123 if (nb_pending_keycodes > 0) {
1124 qemu_del_timer(key_timer);
1131 separator = strchr(string, '-');
1132 keyname_len = separator ? separator - string : strlen(string);
1133 if (keyname_len > 0) {
1134 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1135 if (keyname_len > sizeof(keyname_buf) - 1) {
1136 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1139 if (i == MAX_KEYCODES) {
1140 monitor_printf(mon, "too many keys\n");
1143 keyname_buf[keyname_len] = 0;
1144 keycode = get_keycode(keyname_buf);
1146 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1149 keycodes[i++] = keycode;
1153 string = separator + 1;
1155 nb_pending_keycodes = i;
1156 /* key down events */
1157 for (i = 0; i < nb_pending_keycodes; i++) {
1158 keycode = keycodes[i];
1160 kbd_put_keycode(0xe0);
1161 kbd_put_keycode(keycode & 0x7f);
1163 /* delayed key up events */
1164 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1165 muldiv64(ticks_per_sec, hold_time, 1000));
1168 static int mouse_button_state;
1170 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1173 const char *dx_str = qdict_get_str(qdict, "dx_str");
1174 const char *dy_str = qdict_get_str(qdict, "dy_str");
1175 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1176 dx = strtol(dx_str, NULL, 0);
1177 dy = strtol(dy_str, NULL, 0);
1180 dz = strtol(dz_str, NULL, 0);
1181 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1184 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1186 int button_state = qdict_get_int(qdict, "button_state");
1187 mouse_button_state = button_state;
1188 kbd_mouse_event(0, 0, 0, mouse_button_state);
1191 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1193 int size = qdict_get_int(qdict, "size");
1194 int addr = qdict_get_int(qdict, "addr");
1195 int has_index = qdict_haskey(qdict, "index");
1200 int index = qdict_get_int(qdict, "index");
1201 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1209 val = cpu_inb(NULL, addr);
1213 val = cpu_inw(NULL, addr);
1217 val = cpu_inl(NULL, addr);
1221 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1222 suffix, addr, size * 2, val);
1225 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1227 int size = qdict_get_int(qdict, "size");
1228 int addr = qdict_get_int(qdict, "addr");
1229 int val = qdict_get_int(qdict, "val");
1231 addr &= IOPORTS_MASK;
1236 cpu_outb(NULL, addr, val);
1239 cpu_outw(NULL, addr, val);
1242 cpu_outl(NULL, addr, val);
1247 static void do_boot_set(Monitor *mon, const QDict *qdict)
1250 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1252 res = qemu_boot_set(bootdevice);
1254 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1255 } else if (res > 0) {
1256 monitor_printf(mon, "setting boot device list failed\n");
1258 monitor_printf(mon, "no function defined to set boot device list for "
1259 "this architecture\n");
1263 static void do_system_reset(Monitor *mon, const QDict *qdict)
1265 qemu_system_reset_request();
1268 static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1270 qemu_system_powerdown_request();
1273 #if defined(TARGET_I386)
1274 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1276 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1279 pte & PG_GLOBAL_MASK ? 'G' : '-',
1280 pte & PG_PSE_MASK ? 'P' : '-',
1281 pte & PG_DIRTY_MASK ? 'D' : '-',
1282 pte & PG_ACCESSED_MASK ? 'A' : '-',
1283 pte & PG_PCD_MASK ? 'C' : '-',
1284 pte & PG_PWT_MASK ? 'T' : '-',
1285 pte & PG_USER_MASK ? 'U' : '-',
1286 pte & PG_RW_MASK ? 'W' : '-');
1289 static void tlb_info(Monitor *mon)
1293 uint32_t pgd, pde, pte;
1295 env = mon_get_cpu();
1299 if (!(env->cr[0] & CR0_PG_MASK)) {
1300 monitor_printf(mon, "PG disabled\n");
1303 pgd = env->cr[3] & ~0xfff;
1304 for(l1 = 0; l1 < 1024; l1++) {
1305 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1306 pde = le32_to_cpu(pde);
1307 if (pde & PG_PRESENT_MASK) {
1308 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1309 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1311 for(l2 = 0; l2 < 1024; l2++) {
1312 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1313 (uint8_t *)&pte, 4);
1314 pte = le32_to_cpu(pte);
1315 if (pte & PG_PRESENT_MASK) {
1316 print_pte(mon, (l1 << 22) + (l2 << 12),
1326 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1327 uint32_t end, int prot)
1330 prot1 = *plast_prot;
1331 if (prot != prot1) {
1332 if (*pstart != -1) {
1333 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1334 *pstart, end, end - *pstart,
1335 prot1 & PG_USER_MASK ? 'u' : '-',
1337 prot1 & PG_RW_MASK ? 'w' : '-');
1347 static void mem_info(Monitor *mon)
1350 int l1, l2, prot, last_prot;
1351 uint32_t pgd, pde, pte, start, end;
1353 env = mon_get_cpu();
1357 if (!(env->cr[0] & CR0_PG_MASK)) {
1358 monitor_printf(mon, "PG disabled\n");
1361 pgd = env->cr[3] & ~0xfff;
1364 for(l1 = 0; l1 < 1024; l1++) {
1365 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1366 pde = le32_to_cpu(pde);
1368 if (pde & PG_PRESENT_MASK) {
1369 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1370 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1371 mem_print(mon, &start, &last_prot, end, prot);
1373 for(l2 = 0; l2 < 1024; l2++) {
1374 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1375 (uint8_t *)&pte, 4);
1376 pte = le32_to_cpu(pte);
1377 end = (l1 << 22) + (l2 << 12);
1378 if (pte & PG_PRESENT_MASK) {
1379 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1383 mem_print(mon, &start, &last_prot, end, prot);
1388 mem_print(mon, &start, &last_prot, end, prot);
1394 #if defined(TARGET_SH4)
1396 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1398 monitor_printf(mon, " tlb%i:\t"
1399 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1400 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1401 "dirty=%hhu writethrough=%hhu\n",
1403 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1404 tlb->v, tlb->sh, tlb->c, tlb->pr,
1408 static void tlb_info(Monitor *mon)
1410 CPUState *env = mon_get_cpu();
1413 monitor_printf (mon, "ITLB:\n");
1414 for (i = 0 ; i < ITLB_SIZE ; i++)
1415 print_tlb (mon, i, &env->itlb[i]);
1416 monitor_printf (mon, "UTLB:\n");
1417 for (i = 0 ; i < UTLB_SIZE ; i++)
1418 print_tlb (mon, i, &env->utlb[i]);
1423 static void do_info_kvm(Monitor *mon)
1426 monitor_printf(mon, "kvm support: ");
1428 monitor_printf(mon, "enabled\n");
1430 monitor_printf(mon, "disabled\n");
1432 monitor_printf(mon, "kvm support: not compiled\n");
1436 static void do_info_numa(Monitor *mon)
1441 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1442 for (i = 0; i < nb_numa_nodes; i++) {
1443 monitor_printf(mon, "node %d cpus:", i);
1444 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1445 if (env->numa_node == i) {
1446 monitor_printf(mon, " %d", env->cpu_index);
1449 monitor_printf(mon, "\n");
1450 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1455 #ifdef CONFIG_PROFILER
1457 static void do_info_profile(Monitor *mon)
1463 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1464 dev_time, dev_time / (double)ticks_per_sec);
1465 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1466 qemu_time, qemu_time / (double)ticks_per_sec);
1471 static void do_info_profile(Monitor *mon)
1473 monitor_printf(mon, "Internal profiler not compiled\n");
1477 /* Capture support */
1478 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1480 static void do_info_capture(Monitor *mon)
1485 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1486 monitor_printf(mon, "[%d]: ", i);
1487 s->ops.info (s->opaque);
1492 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1495 int n = qdict_get_int(qdict, "n");
1498 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1500 s->ops.destroy (s->opaque);
1501 LIST_REMOVE (s, entries);
1508 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1510 const char *path = qdict_get_str(qdict, "path");
1511 int has_freq = qdict_haskey(qdict, "freq");
1512 int freq = qdict_get_try_int(qdict, "freq", -1);
1513 int has_bits = qdict_haskey(qdict, "bits");
1514 int bits = qdict_get_try_int(qdict, "bits", -1);
1515 int has_channels = qdict_haskey(qdict, "nchannels");
1516 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1519 s = qemu_mallocz (sizeof (*s));
1521 freq = has_freq ? freq : 44100;
1522 bits = has_bits ? bits : 16;
1523 nchannels = has_channels ? nchannels : 2;
1525 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1526 monitor_printf(mon, "Faied to add wave capture\n");
1529 LIST_INSERT_HEAD (&capture_head, s, entries);
1533 #if defined(TARGET_I386)
1534 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1537 int cpu_index = qdict_get_int(qdict, "cpu_index");
1539 for (env = first_cpu; env != NULL; env = env->next_cpu)
1540 if (env->cpu_index == cpu_index) {
1541 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1547 static void do_info_status(Monitor *mon)
1551 monitor_printf(mon, "VM status: running (single step mode)\n");
1553 monitor_printf(mon, "VM status: running\n");
1556 monitor_printf(mon, "VM status: paused\n");
1560 static void do_balloon(Monitor *mon, const QDict *qdict)
1562 int value = qdict_get_int(qdict, "value");
1563 ram_addr_t target = value;
1564 qemu_balloon(target << 20);
1567 static void do_info_balloon(Monitor *mon)
1571 actual = qemu_balloon_status();
1572 if (kvm_enabled() && !kvm_has_sync_mmu())
1573 monitor_printf(mon, "Using KVM without synchronous MMU, "
1574 "ballooning disabled\n");
1575 else if (actual == 0)
1576 monitor_printf(mon, "Ballooning not activated in VM\n");
1578 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1581 static qemu_acl *find_acl(Monitor *mon, const char *name)
1583 qemu_acl *acl = qemu_acl_find(name);
1586 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1591 static void do_acl_show(Monitor *mon, const QDict *qdict)
1593 const char *aclname = qdict_get_str(qdict, "aclname");
1594 qemu_acl *acl = find_acl(mon, aclname);
1595 qemu_acl_entry *entry;
1599 monitor_printf(mon, "policy: %s\n",
1600 acl->defaultDeny ? "deny" : "allow");
1601 TAILQ_FOREACH(entry, &acl->entries, next) {
1603 monitor_printf(mon, "%d: %s %s\n", i,
1604 entry->deny ? "deny" : "allow", entry->match);
1609 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1611 const char *aclname = qdict_get_str(qdict, "aclname");
1612 qemu_acl *acl = find_acl(mon, aclname);
1615 qemu_acl_reset(acl);
1616 monitor_printf(mon, "acl: removed all rules\n");
1620 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1622 const char *aclname = qdict_get_str(qdict, "aclname");
1623 const char *policy = qdict_get_str(qdict, "policy");
1624 qemu_acl *acl = find_acl(mon, aclname);
1627 if (strcmp(policy, "allow") == 0) {
1628 acl->defaultDeny = 0;
1629 monitor_printf(mon, "acl: policy set to 'allow'\n");
1630 } else if (strcmp(policy, "deny") == 0) {
1631 acl->defaultDeny = 1;
1632 monitor_printf(mon, "acl: policy set to 'deny'\n");
1634 monitor_printf(mon, "acl: unknown policy '%s', "
1635 "expected 'deny' or 'allow'\n", policy);
1640 static void do_acl_add(Monitor *mon, const QDict *qdict)
1642 const char *aclname = qdict_get_str(qdict, "aclname");
1643 const char *match = qdict_get_str(qdict, "match");
1644 const char *policy = qdict_get_str(qdict, "policy");
1645 int has_index = qdict_haskey(qdict, "index");
1646 int index = qdict_get_try_int(qdict, "index", -1);
1647 qemu_acl *acl = find_acl(mon, aclname);
1651 if (strcmp(policy, "allow") == 0) {
1653 } else if (strcmp(policy, "deny") == 0) {
1656 monitor_printf(mon, "acl: unknown policy '%s', "
1657 "expected 'deny' or 'allow'\n", policy);
1661 ret = qemu_acl_insert(acl, deny, match, index);
1663 ret = qemu_acl_append(acl, deny, match);
1665 monitor_printf(mon, "acl: unable to add acl entry\n");
1667 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1671 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1673 const char *aclname = qdict_get_str(qdict, "aclname");
1674 const char *match = qdict_get_str(qdict, "match");
1675 qemu_acl *acl = find_acl(mon, aclname);
1679 ret = qemu_acl_remove(acl, match);
1681 monitor_printf(mon, "acl: no matching acl entry\n");
1683 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1687 #if defined(TARGET_I386)
1688 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1691 int cpu_index = qdict_get_int(qdict, "cpu_index");
1692 int bank = qdict_get_int(qdict, "bank");
1693 uint64_t status = qdict_get_int(qdict, "status");
1694 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1695 uint64_t addr = qdict_get_int(qdict, "addr");
1696 uint64_t misc = qdict_get_int(qdict, "misc");
1698 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1699 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1700 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1706 static void do_getfd(Monitor *mon, const QDict *qdict)
1708 const char *fdname = qdict_get_str(qdict, "fdname");
1712 fd = qemu_chr_get_msgfd(mon->chr);
1714 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1718 if (qemu_isdigit(fdname[0])) {
1719 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1725 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1730 LIST_FOREACH(monfd, &mon->fds, next) {
1731 if (strcmp(monfd->name, fdname) != 0) {
1740 monfd = qemu_mallocz(sizeof(mon_fd_t));
1741 monfd->name = qemu_strdup(fdname);
1744 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1747 static void do_closefd(Monitor *mon, const QDict *qdict)
1749 const char *fdname = qdict_get_str(qdict, "fdname");
1752 LIST_FOREACH(monfd, &mon->fds, next) {
1753 if (strcmp(monfd->name, fdname) != 0) {
1757 LIST_REMOVE(monfd, next);
1759 qemu_free(monfd->name);
1764 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1768 static void do_loadvm(Monitor *mon, const QDict *qdict)
1770 int saved_vm_running = vm_running;
1771 const char *name = qdict_get_str(qdict, "name");
1775 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1779 int monitor_get_fd(Monitor *mon, const char *fdname)
1783 LIST_FOREACH(monfd, &mon->fds, next) {
1786 if (strcmp(monfd->name, fdname) != 0) {
1792 /* caller takes ownership of fd */
1793 LIST_REMOVE(monfd, next);
1794 qemu_free(monfd->name);
1803 static const mon_cmd_t mon_cmds[] = {
1804 #include "qemu-monitor.h"
1808 /* Please update qemu-monitor.hx when adding or changing commands */
1809 static const mon_cmd_t info_cmds[] = {
1810 { "version", "", do_info_version,
1811 "", "show the version of QEMU" },
1812 { "network", "", do_info_network,
1813 "", "show the network state" },
1814 { "chardev", "", qemu_chr_info,
1815 "", "show the character devices" },
1816 { "block", "", bdrv_info,
1817 "", "show the block devices" },
1818 { "blockstats", "", bdrv_info_stats,
1819 "", "show block device statistics" },
1820 { "registers", "", do_info_registers,
1821 "", "show the cpu registers" },
1822 { "cpus", "", do_info_cpus,
1823 "", "show infos for each CPU" },
1824 { "history", "", do_info_history,
1825 "", "show the command line history", },
1826 { "irq", "", irq_info,
1827 "", "show the interrupts statistics (if available)", },
1828 { "pic", "", pic_info,
1829 "", "show i8259 (PIC) state", },
1830 { "pci", "", pci_info,
1831 "", "show PCI info", },
1832 #if defined(TARGET_I386) || defined(TARGET_SH4)
1833 { "tlb", "", tlb_info,
1834 "", "show virtual to physical memory mappings", },
1836 #if defined(TARGET_I386)
1837 { "mem", "", mem_info,
1838 "", "show the active virtual memory mappings", },
1839 { "hpet", "", do_info_hpet,
1840 "", "show state of HPET", },
1842 { "jit", "", do_info_jit,
1843 "", "show dynamic compiler info", },
1844 { "kvm", "", do_info_kvm,
1845 "", "show KVM information", },
1846 { "numa", "", do_info_numa,
1847 "", "show NUMA information", },
1848 { "usb", "", usb_info,
1849 "", "show guest USB devices", },
1850 { "usbhost", "", usb_host_info,
1851 "", "show host USB devices", },
1852 { "profile", "", do_info_profile,
1853 "", "show profiling information", },
1854 { "capture", "", do_info_capture,
1855 "", "show capture information" },
1856 { "snapshots", "", do_info_snapshots,
1857 "", "show the currently saved VM snapshots" },
1858 { "status", "", do_info_status,
1859 "", "show the current VM status (running|paused)" },
1860 { "pcmcia", "", pcmcia_info,
1861 "", "show guest PCMCIA status" },
1862 { "mice", "", do_info_mice,
1863 "", "show which guest mouse is receiving events" },
1864 { "vnc", "", do_info_vnc,
1865 "", "show the vnc server status"},
1866 { "name", "", do_info_name,
1867 "", "show the current VM name" },
1868 { "uuid", "", do_info_uuid,
1869 "", "show the current VM UUID" },
1870 #if defined(TARGET_PPC)
1871 { "cpustats", "", do_info_cpu_stats,
1872 "", "show CPU statistics", },
1874 #if defined(CONFIG_SLIRP)
1875 { "usernet", "", do_info_usernet,
1876 "", "show user network stack connection states", },
1878 { "migrate", "", do_info_migrate, "", "show migration status" },
1879 { "balloon", "", do_info_balloon,
1880 "", "show balloon information" },
1881 { "qtree", "", do_info_qtree,
1882 "", "show device tree" },
1883 { "qdm", "", do_info_qdm,
1884 "", "show qdev device model list" },
1888 /*******************************************************************/
1890 static const char *pch;
1891 static jmp_buf expr_env;
1896 typedef struct MonitorDef {
1899 target_long (*get_value)(const struct MonitorDef *md, int val);
1903 #if defined(TARGET_I386)
1904 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1906 CPUState *env = mon_get_cpu();
1909 return env->eip + env->segs[R_CS].base;
1913 #if defined(TARGET_PPC)
1914 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1916 CPUState *env = mon_get_cpu();
1924 for (i = 0; i < 8; i++)
1925 u |= env->crf[i] << (32 - (4 * i));
1930 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1932 CPUState *env = mon_get_cpu();
1938 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1940 CPUState *env = mon_get_cpu();
1946 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1948 CPUState *env = mon_get_cpu();
1951 return cpu_ppc_load_decr(env);
1954 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1956 CPUState *env = mon_get_cpu();
1959 return cpu_ppc_load_tbu(env);
1962 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1964 CPUState *env = mon_get_cpu();
1967 return cpu_ppc_load_tbl(env);
1971 #if defined(TARGET_SPARC)
1972 #ifndef TARGET_SPARC64
1973 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1975 CPUState *env = mon_get_cpu();
1978 return GET_PSR(env);
1982 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1984 CPUState *env = mon_get_cpu();
1987 return env->regwptr[val];
1991 static const MonitorDef monitor_defs[] = {
1994 #define SEG(name, seg) \
1995 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1996 { name ".base", offsetof(CPUState, segs[seg].base) },\
1997 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1999 { "eax", offsetof(CPUState, regs[0]) },
2000 { "ecx", offsetof(CPUState, regs[1]) },
2001 { "edx", offsetof(CPUState, regs[2]) },
2002 { "ebx", offsetof(CPUState, regs[3]) },
2003 { "esp|sp", offsetof(CPUState, regs[4]) },
2004 { "ebp|fp", offsetof(CPUState, regs[5]) },
2005 { "esi", offsetof(CPUState, regs[6]) },
2006 { "edi", offsetof(CPUState, regs[7]) },
2007 #ifdef TARGET_X86_64
2008 { "r8", offsetof(CPUState, regs[8]) },
2009 { "r9", offsetof(CPUState, regs[9]) },
2010 { "r10", offsetof(CPUState, regs[10]) },
2011 { "r11", offsetof(CPUState, regs[11]) },
2012 { "r12", offsetof(CPUState, regs[12]) },
2013 { "r13", offsetof(CPUState, regs[13]) },
2014 { "r14", offsetof(CPUState, regs[14]) },
2015 { "r15", offsetof(CPUState, regs[15]) },
2017 { "eflags", offsetof(CPUState, eflags) },
2018 { "eip", offsetof(CPUState, eip) },
2025 { "pc", 0, monitor_get_pc, },
2026 #elif defined(TARGET_PPC)
2027 /* General purpose registers */
2028 { "r0", offsetof(CPUState, gpr[0]) },
2029 { "r1", offsetof(CPUState, gpr[1]) },
2030 { "r2", offsetof(CPUState, gpr[2]) },
2031 { "r3", offsetof(CPUState, gpr[3]) },
2032 { "r4", offsetof(CPUState, gpr[4]) },
2033 { "r5", offsetof(CPUState, gpr[5]) },
2034 { "r6", offsetof(CPUState, gpr[6]) },
2035 { "r7", offsetof(CPUState, gpr[7]) },
2036 { "r8", offsetof(CPUState, gpr[8]) },
2037 { "r9", offsetof(CPUState, gpr[9]) },
2038 { "r10", offsetof(CPUState, gpr[10]) },
2039 { "r11", offsetof(CPUState, gpr[11]) },
2040 { "r12", offsetof(CPUState, gpr[12]) },
2041 { "r13", offsetof(CPUState, gpr[13]) },
2042 { "r14", offsetof(CPUState, gpr[14]) },
2043 { "r15", offsetof(CPUState, gpr[15]) },
2044 { "r16", offsetof(CPUState, gpr[16]) },
2045 { "r17", offsetof(CPUState, gpr[17]) },
2046 { "r18", offsetof(CPUState, gpr[18]) },
2047 { "r19", offsetof(CPUState, gpr[19]) },
2048 { "r20", offsetof(CPUState, gpr[20]) },
2049 { "r21", offsetof(CPUState, gpr[21]) },
2050 { "r22", offsetof(CPUState, gpr[22]) },
2051 { "r23", offsetof(CPUState, gpr[23]) },
2052 { "r24", offsetof(CPUState, gpr[24]) },
2053 { "r25", offsetof(CPUState, gpr[25]) },
2054 { "r26", offsetof(CPUState, gpr[26]) },
2055 { "r27", offsetof(CPUState, gpr[27]) },
2056 { "r28", offsetof(CPUState, gpr[28]) },
2057 { "r29", offsetof(CPUState, gpr[29]) },
2058 { "r30", offsetof(CPUState, gpr[30]) },
2059 { "r31", offsetof(CPUState, gpr[31]) },
2060 /* Floating point registers */
2061 { "f0", offsetof(CPUState, fpr[0]) },
2062 { "f1", offsetof(CPUState, fpr[1]) },
2063 { "f2", offsetof(CPUState, fpr[2]) },
2064 { "f3", offsetof(CPUState, fpr[3]) },
2065 { "f4", offsetof(CPUState, fpr[4]) },
2066 { "f5", offsetof(CPUState, fpr[5]) },
2067 { "f6", offsetof(CPUState, fpr[6]) },
2068 { "f7", offsetof(CPUState, fpr[7]) },
2069 { "f8", offsetof(CPUState, fpr[8]) },
2070 { "f9", offsetof(CPUState, fpr[9]) },
2071 { "f10", offsetof(CPUState, fpr[10]) },
2072 { "f11", offsetof(CPUState, fpr[11]) },
2073 { "f12", offsetof(CPUState, fpr[12]) },
2074 { "f13", offsetof(CPUState, fpr[13]) },
2075 { "f14", offsetof(CPUState, fpr[14]) },
2076 { "f15", offsetof(CPUState, fpr[15]) },
2077 { "f16", offsetof(CPUState, fpr[16]) },
2078 { "f17", offsetof(CPUState, fpr[17]) },
2079 { "f18", offsetof(CPUState, fpr[18]) },
2080 { "f19", offsetof(CPUState, fpr[19]) },
2081 { "f20", offsetof(CPUState, fpr[20]) },
2082 { "f21", offsetof(CPUState, fpr[21]) },
2083 { "f22", offsetof(CPUState, fpr[22]) },
2084 { "f23", offsetof(CPUState, fpr[23]) },
2085 { "f24", offsetof(CPUState, fpr[24]) },
2086 { "f25", offsetof(CPUState, fpr[25]) },
2087 { "f26", offsetof(CPUState, fpr[26]) },
2088 { "f27", offsetof(CPUState, fpr[27]) },
2089 { "f28", offsetof(CPUState, fpr[28]) },
2090 { "f29", offsetof(CPUState, fpr[29]) },
2091 { "f30", offsetof(CPUState, fpr[30]) },
2092 { "f31", offsetof(CPUState, fpr[31]) },
2093 { "fpscr", offsetof(CPUState, fpscr) },
2094 /* Next instruction pointer */
2095 { "nip|pc", offsetof(CPUState, nip) },
2096 { "lr", offsetof(CPUState, lr) },
2097 { "ctr", offsetof(CPUState, ctr) },
2098 { "decr", 0, &monitor_get_decr, },
2099 { "ccr", 0, &monitor_get_ccr, },
2100 /* Machine state register */
2101 { "msr", 0, &monitor_get_msr, },
2102 { "xer", 0, &monitor_get_xer, },
2103 { "tbu", 0, &monitor_get_tbu, },
2104 { "tbl", 0, &monitor_get_tbl, },
2105 #if defined(TARGET_PPC64)
2106 /* Address space register */
2107 { "asr", offsetof(CPUState, asr) },
2109 /* Segment registers */
2110 { "sdr1", offsetof(CPUState, sdr1) },
2111 { "sr0", offsetof(CPUState, sr[0]) },
2112 { "sr1", offsetof(CPUState, sr[1]) },
2113 { "sr2", offsetof(CPUState, sr[2]) },
2114 { "sr3", offsetof(CPUState, sr[3]) },
2115 { "sr4", offsetof(CPUState, sr[4]) },
2116 { "sr5", offsetof(CPUState, sr[5]) },
2117 { "sr6", offsetof(CPUState, sr[6]) },
2118 { "sr7", offsetof(CPUState, sr[7]) },
2119 { "sr8", offsetof(CPUState, sr[8]) },
2120 { "sr9", offsetof(CPUState, sr[9]) },
2121 { "sr10", offsetof(CPUState, sr[10]) },
2122 { "sr11", offsetof(CPUState, sr[11]) },
2123 { "sr12", offsetof(CPUState, sr[12]) },
2124 { "sr13", offsetof(CPUState, sr[13]) },
2125 { "sr14", offsetof(CPUState, sr[14]) },
2126 { "sr15", offsetof(CPUState, sr[15]) },
2127 /* Too lazy to put BATs and SPRs ... */
2128 #elif defined(TARGET_SPARC)
2129 { "g0", offsetof(CPUState, gregs[0]) },
2130 { "g1", offsetof(CPUState, gregs[1]) },
2131 { "g2", offsetof(CPUState, gregs[2]) },
2132 { "g3", offsetof(CPUState, gregs[3]) },
2133 { "g4", offsetof(CPUState, gregs[4]) },
2134 { "g5", offsetof(CPUState, gregs[5]) },
2135 { "g6", offsetof(CPUState, gregs[6]) },
2136 { "g7", offsetof(CPUState, gregs[7]) },
2137 { "o0", 0, monitor_get_reg },
2138 { "o1", 1, monitor_get_reg },
2139 { "o2", 2, monitor_get_reg },
2140 { "o3", 3, monitor_get_reg },
2141 { "o4", 4, monitor_get_reg },
2142 { "o5", 5, monitor_get_reg },
2143 { "o6", 6, monitor_get_reg },
2144 { "o7", 7, monitor_get_reg },
2145 { "l0", 8, monitor_get_reg },
2146 { "l1", 9, monitor_get_reg },
2147 { "l2", 10, monitor_get_reg },
2148 { "l3", 11, monitor_get_reg },
2149 { "l4", 12, monitor_get_reg },
2150 { "l5", 13, monitor_get_reg },
2151 { "l6", 14, monitor_get_reg },
2152 { "l7", 15, monitor_get_reg },
2153 { "i0", 16, monitor_get_reg },
2154 { "i1", 17, monitor_get_reg },
2155 { "i2", 18, monitor_get_reg },
2156 { "i3", 19, monitor_get_reg },
2157 { "i4", 20, monitor_get_reg },
2158 { "i5", 21, monitor_get_reg },
2159 { "i6", 22, monitor_get_reg },
2160 { "i7", 23, monitor_get_reg },
2161 { "pc", offsetof(CPUState, pc) },
2162 { "npc", offsetof(CPUState, npc) },
2163 { "y", offsetof(CPUState, y) },
2164 #ifndef TARGET_SPARC64
2165 { "psr", 0, &monitor_get_psr, },
2166 { "wim", offsetof(CPUState, wim) },
2168 { "tbr", offsetof(CPUState, tbr) },
2169 { "fsr", offsetof(CPUState, fsr) },
2170 { "f0", offsetof(CPUState, fpr[0]) },
2171 { "f1", offsetof(CPUState, fpr[1]) },
2172 { "f2", offsetof(CPUState, fpr[2]) },
2173 { "f3", offsetof(CPUState, fpr[3]) },
2174 { "f4", offsetof(CPUState, fpr[4]) },
2175 { "f5", offsetof(CPUState, fpr[5]) },
2176 { "f6", offsetof(CPUState, fpr[6]) },
2177 { "f7", offsetof(CPUState, fpr[7]) },
2178 { "f8", offsetof(CPUState, fpr[8]) },
2179 { "f9", offsetof(CPUState, fpr[9]) },
2180 { "f10", offsetof(CPUState, fpr[10]) },
2181 { "f11", offsetof(CPUState, fpr[11]) },
2182 { "f12", offsetof(CPUState, fpr[12]) },
2183 { "f13", offsetof(CPUState, fpr[13]) },
2184 { "f14", offsetof(CPUState, fpr[14]) },
2185 { "f15", offsetof(CPUState, fpr[15]) },
2186 { "f16", offsetof(CPUState, fpr[16]) },
2187 { "f17", offsetof(CPUState, fpr[17]) },
2188 { "f18", offsetof(CPUState, fpr[18]) },
2189 { "f19", offsetof(CPUState, fpr[19]) },
2190 { "f20", offsetof(CPUState, fpr[20]) },
2191 { "f21", offsetof(CPUState, fpr[21]) },
2192 { "f22", offsetof(CPUState, fpr[22]) },
2193 { "f23", offsetof(CPUState, fpr[23]) },
2194 { "f24", offsetof(CPUState, fpr[24]) },
2195 { "f25", offsetof(CPUState, fpr[25]) },
2196 { "f26", offsetof(CPUState, fpr[26]) },
2197 { "f27", offsetof(CPUState, fpr[27]) },
2198 { "f28", offsetof(CPUState, fpr[28]) },
2199 { "f29", offsetof(CPUState, fpr[29]) },
2200 { "f30", offsetof(CPUState, fpr[30]) },
2201 { "f31", offsetof(CPUState, fpr[31]) },
2202 #ifdef TARGET_SPARC64
2203 { "f32", offsetof(CPUState, fpr[32]) },
2204 { "f34", offsetof(CPUState, fpr[34]) },
2205 { "f36", offsetof(CPUState, fpr[36]) },
2206 { "f38", offsetof(CPUState, fpr[38]) },
2207 { "f40", offsetof(CPUState, fpr[40]) },
2208 { "f42", offsetof(CPUState, fpr[42]) },
2209 { "f44", offsetof(CPUState, fpr[44]) },
2210 { "f46", offsetof(CPUState, fpr[46]) },
2211 { "f48", offsetof(CPUState, fpr[48]) },
2212 { "f50", offsetof(CPUState, fpr[50]) },
2213 { "f52", offsetof(CPUState, fpr[52]) },
2214 { "f54", offsetof(CPUState, fpr[54]) },
2215 { "f56", offsetof(CPUState, fpr[56]) },
2216 { "f58", offsetof(CPUState, fpr[58]) },
2217 { "f60", offsetof(CPUState, fpr[60]) },
2218 { "f62", offsetof(CPUState, fpr[62]) },
2219 { "asi", offsetof(CPUState, asi) },
2220 { "pstate", offsetof(CPUState, pstate) },
2221 { "cansave", offsetof(CPUState, cansave) },
2222 { "canrestore", offsetof(CPUState, canrestore) },
2223 { "otherwin", offsetof(CPUState, otherwin) },
2224 { "wstate", offsetof(CPUState, wstate) },
2225 { "cleanwin", offsetof(CPUState, cleanwin) },
2226 { "fprs", offsetof(CPUState, fprs) },
2232 static void expr_error(Monitor *mon, const char *msg)
2234 monitor_printf(mon, "%s\n", msg);
2235 longjmp(expr_env, 1);
2238 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2239 static int get_monitor_def(target_long *pval, const char *name)
2241 const MonitorDef *md;
2244 for(md = monitor_defs; md->name != NULL; md++) {
2245 if (compare_cmd(name, md->name)) {
2246 if (md->get_value) {
2247 *pval = md->get_value(md, md->offset);
2249 CPUState *env = mon_get_cpu();
2252 ptr = (uint8_t *)env + md->offset;
2255 *pval = *(int32_t *)ptr;
2258 *pval = *(target_long *)ptr;
2271 static void next(void)
2275 while (qemu_isspace(*pch))
2280 static int64_t expr_sum(Monitor *mon);
2282 static int64_t expr_unary(Monitor *mon)
2291 n = expr_unary(mon);
2295 n = -expr_unary(mon);
2299 n = ~expr_unary(mon);
2305 expr_error(mon, "')' expected");
2312 expr_error(mon, "character constant expected");
2316 expr_error(mon, "missing terminating \' character");
2326 while ((*pch >= 'a' && *pch <= 'z') ||
2327 (*pch >= 'A' && *pch <= 'Z') ||
2328 (*pch >= '0' && *pch <= '9') ||
2329 *pch == '_' || *pch == '.') {
2330 if ((q - buf) < sizeof(buf) - 1)
2334 while (qemu_isspace(*pch))
2337 ret = get_monitor_def(®, buf);
2339 expr_error(mon, "unknown register");
2341 expr_error(mon, "no cpu defined");
2346 expr_error(mon, "unexpected end of expression");
2350 #if TARGET_PHYS_ADDR_BITS > 32
2351 n = strtoull(pch, &p, 0);
2353 n = strtoul(pch, &p, 0);
2356 expr_error(mon, "invalid char in expression");
2359 while (qemu_isspace(*pch))
2367 static int64_t expr_prod(Monitor *mon)
2372 val = expr_unary(mon);
2375 if (op != '*' && op != '/' && op != '%')
2378 val2 = expr_unary(mon);
2387 expr_error(mon, "division by zero");
2398 static int64_t expr_logic(Monitor *mon)
2403 val = expr_prod(mon);
2406 if (op != '&' && op != '|' && op != '^')
2409 val2 = expr_prod(mon);
2426 static int64_t expr_sum(Monitor *mon)
2431 val = expr_logic(mon);
2434 if (op != '+' && op != '-')
2437 val2 = expr_logic(mon);
2446 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2449 if (setjmp(expr_env)) {
2453 while (qemu_isspace(*pch))
2455 *pval = expr_sum(mon);
2460 static int get_str(char *buf, int buf_size, const char **pp)
2468 while (qemu_isspace(*p))
2478 while (*p != '\0' && *p != '\"') {
2494 qemu_printf("unsupported escape code: '\\%c'\n", c);
2497 if ((q - buf) < buf_size - 1) {
2501 if ((q - buf) < buf_size - 1) {
2508 qemu_printf("unterminated string\n");
2513 while (*p != '\0' && !qemu_isspace(*p)) {
2514 if ((q - buf) < buf_size - 1) {
2526 * Store the command-name in cmdname, and return a pointer to
2527 * the remaining of the command string.
2529 static const char *get_command_name(const char *cmdline,
2530 char *cmdname, size_t nlen)
2533 const char *p, *pstart;
2536 while (qemu_isspace(*p))
2541 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2546 memcpy(cmdname, pstart, len);
2547 cmdname[len] = '\0';
2552 * Read key of 'type' into 'key' and return the current
2555 static char *key_get_info(const char *type, char **key)
2563 p = strchr(type, ':');
2570 str = qemu_malloc(len + 1);
2571 memcpy(str, type, len);
2578 static int default_fmt_format = 'x';
2579 static int default_fmt_size = 4;
2583 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2584 const char *cmdline,
2587 const char *p, *typestr;
2589 const mon_cmd_t *cmd;
2595 monitor_printf(mon, "command='%s'\n", cmdline);
2598 /* extract the command name */
2599 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2603 /* find the command */
2604 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2605 if (compare_cmd(cmdname, cmd->name))
2609 if (cmd->name == NULL) {
2610 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2614 /* parse the parameters */
2615 typestr = cmd->args_type;
2617 typestr = key_get_info(typestr, &key);
2629 while (qemu_isspace(*p))
2631 if (*typestr == '?') {
2634 /* no optional string: NULL argument */
2638 ret = get_str(buf, sizeof(buf), &p);
2642 monitor_printf(mon, "%s: filename expected\n",
2646 monitor_printf(mon, "%s: block device name expected\n",
2650 monitor_printf(mon, "%s: string expected\n", cmdname);
2655 qdict_put(qdict, key, qstring_from_str(buf));
2660 int count, format, size;
2662 while (qemu_isspace(*p))
2668 if (qemu_isdigit(*p)) {
2670 while (qemu_isdigit(*p)) {
2671 count = count * 10 + (*p - '0');
2709 if (*p != '\0' && !qemu_isspace(*p)) {
2710 monitor_printf(mon, "invalid char in format: '%c'\n",
2715 format = default_fmt_format;
2716 if (format != 'i') {
2717 /* for 'i', not specifying a size gives -1 as size */
2719 size = default_fmt_size;
2720 default_fmt_size = size;
2722 default_fmt_format = format;
2725 format = default_fmt_format;
2726 if (format != 'i') {
2727 size = default_fmt_size;
2732 qdict_put(qdict, "count", qint_from_int(count));
2733 qdict_put(qdict, "format", qint_from_int(format));
2734 qdict_put(qdict, "size", qint_from_int(size));
2742 while (qemu_isspace(*p))
2744 if (*typestr == '?' || *typestr == '.') {
2745 if (*typestr == '?') {
2753 while (qemu_isspace(*p))
2762 if (get_expr(mon, &val, &p))
2764 /* Check if 'i' is greater than 32-bit */
2765 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
2766 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
2767 monitor_printf(mon, "integer is for 32-bit values\n");
2770 qdict_put(qdict, key, qint_from_int(val));
2781 while (qemu_isspace(*p))
2787 monitor_printf(mon, "%s: unsupported option -%c\n",
2794 qdict_put(qdict, key, qint_from_int(has_option));
2799 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2805 /* check that all arguments were parsed */
2806 while (qemu_isspace(*p))
2809 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2821 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2824 const mon_cmd_t *cmd;
2826 qdict = qdict_new();
2828 cmd = monitor_parse_command(mon, cmdline, qdict);
2830 void (*handler)(Monitor *mon, const QDict *qdict);
2832 qemu_errors_to_mon(mon);
2834 handler = cmd->handler;
2835 handler(mon, qdict);
2837 qemu_errors_to_previous();
2843 static void cmd_completion(const char *name, const char *list)
2845 const char *p, *pstart;
2854 p = pstart + strlen(pstart);
2856 if (len > sizeof(cmd) - 2)
2857 len = sizeof(cmd) - 2;
2858 memcpy(cmd, pstart, len);
2860 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2861 readline_add_completion(cur_mon->rs, cmd);
2869 static void file_completion(const char *input)
2874 char file[1024], file_prefix[1024];
2878 p = strrchr(input, '/');
2881 pstrcpy(file_prefix, sizeof(file_prefix), input);
2882 pstrcpy(path, sizeof(path), ".");
2884 input_path_len = p - input + 1;
2885 memcpy(path, input, input_path_len);
2886 if (input_path_len > sizeof(path) - 1)
2887 input_path_len = sizeof(path) - 1;
2888 path[input_path_len] = '\0';
2889 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2891 #ifdef DEBUG_COMPLETION
2892 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2893 input, path, file_prefix);
2895 ffs = opendir(path);
2903 if (strstart(d->d_name, file_prefix, NULL)) {
2904 memcpy(file, input, input_path_len);
2905 if (input_path_len < sizeof(file))
2906 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2908 /* stat the file to find out if it's a directory.
2909 * In that case add a slash to speed up typing long paths
2912 if(S_ISDIR(sb.st_mode))
2913 pstrcat(file, sizeof(file), "/");
2914 readline_add_completion(cur_mon->rs, file);
2920 static void block_completion_it(void *opaque, BlockDriverState *bs)
2922 const char *name = bdrv_get_device_name(bs);
2923 const char *input = opaque;
2925 if (input[0] == '\0' ||
2926 !strncmp(name, (char *)input, strlen(input))) {
2927 readline_add_completion(cur_mon->rs, name);
2931 /* NOTE: this parser is an approximate form of the real command parser */
2932 static void parse_cmdline(const char *cmdline,
2933 int *pnb_args, char **args)
2942 while (qemu_isspace(*p))
2946 if (nb_args >= MAX_ARGS)
2948 ret = get_str(buf, sizeof(buf), &p);
2949 args[nb_args] = qemu_strdup(buf);
2954 *pnb_args = nb_args;
2957 static const char *next_arg_type(const char *typestr)
2959 const char *p = strchr(typestr, ':');
2960 return (p != NULL ? ++p : typestr);
2963 static void monitor_find_completion(const char *cmdline)
2965 const char *cmdname;
2966 char *args[MAX_ARGS];
2967 int nb_args, i, len;
2968 const char *ptype, *str;
2969 const mon_cmd_t *cmd;
2972 parse_cmdline(cmdline, &nb_args, args);
2973 #ifdef DEBUG_COMPLETION
2974 for(i = 0; i < nb_args; i++) {
2975 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2979 /* if the line ends with a space, it means we want to complete the
2981 len = strlen(cmdline);
2982 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2983 if (nb_args >= MAX_ARGS)
2985 args[nb_args++] = qemu_strdup("");
2988 /* command completion */
2993 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2994 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2995 cmd_completion(cmdname, cmd->name);
2998 /* find the command */
2999 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3000 if (compare_cmd(args[0], cmd->name))
3005 ptype = next_arg_type(cmd->args_type);
3006 for(i = 0; i < nb_args - 2; i++) {
3007 if (*ptype != '\0') {
3008 ptype = next_arg_type(ptype);
3009 while (*ptype == '?')
3010 ptype = next_arg_type(ptype);
3013 str = args[nb_args - 1];
3014 if (*ptype == '-' && ptype[1] != '\0') {
3019 /* file completion */
3020 readline_set_completion_index(cur_mon->rs, strlen(str));
3021 file_completion(str);
3024 /* block device name completion */
3025 readline_set_completion_index(cur_mon->rs, strlen(str));
3026 bdrv_iterate(block_completion_it, (void *)str);
3029 /* XXX: more generic ? */
3030 if (!strcmp(cmd->name, "info")) {
3031 readline_set_completion_index(cur_mon->rs, strlen(str));
3032 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3033 cmd_completion(str, cmd->name);
3035 } else if (!strcmp(cmd->name, "sendkey")) {
3036 char *sep = strrchr(str, '-');
3039 readline_set_completion_index(cur_mon->rs, strlen(str));
3040 for(key = key_defs; key->name != NULL; key++) {
3041 cmd_completion(str, key->name);
3043 } else if (!strcmp(cmd->name, "help|?")) {
3044 readline_set_completion_index(cur_mon->rs, strlen(str));
3045 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3046 cmd_completion(str, cmd->name);
3054 for(i = 0; i < nb_args; i++)
3058 static int monitor_can_read(void *opaque)
3060 Monitor *mon = opaque;
3062 return (mon->suspend_cnt == 0) ? 128 : 0;
3065 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3067 Monitor *old_mon = cur_mon;
3073 for (i = 0; i < size; i++)
3074 readline_handle_byte(cur_mon->rs, buf[i]);
3076 if (size == 0 || buf[size - 1] != 0)
3077 monitor_printf(cur_mon, "corrupted command\n");
3079 monitor_handle_command(cur_mon, (char *)buf);
3085 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3087 monitor_suspend(mon);
3088 monitor_handle_command(mon, cmdline);
3089 monitor_resume(mon);
3092 int monitor_suspend(Monitor *mon)
3100 void monitor_resume(Monitor *mon)
3104 if (--mon->suspend_cnt == 0)
3105 readline_show_prompt(mon->rs);
3108 static void monitor_event(void *opaque, int event)
3110 Monitor *mon = opaque;
3113 case CHR_EVENT_MUX_IN:
3114 readline_restart(mon->rs);
3115 monitor_resume(mon);
3119 case CHR_EVENT_MUX_OUT:
3120 if (mon->suspend_cnt == 0)
3121 monitor_printf(mon, "\n");
3123 monitor_suspend(mon);
3126 case CHR_EVENT_RESET:
3127 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3128 "information\n", QEMU_VERSION);
3129 if (mon->chr->focus == 0)
3130 readline_show_prompt(mon->rs);
3144 void monitor_init(CharDriverState *chr, int flags)
3146 static int is_first_init = 1;
3149 if (is_first_init) {
3150 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3154 mon = qemu_mallocz(sizeof(*mon));
3158 if (mon->chr->focus != 0)
3159 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3160 if (flags & MONITOR_USE_READLINE) {
3161 mon->rs = readline_init(mon, monitor_find_completion);
3162 monitor_read_command(mon, 0);
3165 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3168 LIST_INSERT_HEAD(&mon_list, mon, entry);
3169 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3173 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3175 BlockDriverState *bs = opaque;
3178 if (bdrv_set_key(bs, password) != 0) {
3179 monitor_printf(mon, "invalid password\n");
3182 if (mon->password_completion_cb)
3183 mon->password_completion_cb(mon->password_opaque, ret);
3185 monitor_read_command(mon, 1);
3188 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3189 BlockDriverCompletionFunc *completion_cb,
3194 if (!bdrv_key_required(bs)) {
3196 completion_cb(opaque, 0);
3200 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3201 bdrv_get_encrypted_filename(bs));
3203 mon->password_completion_cb = completion_cb;
3204 mon->password_opaque = opaque;
3206 err = monitor_read_password(mon, bdrv_password_cb, bs);
3208 if (err && completion_cb)
3209 completion_cb(opaque, err);
3212 typedef struct QemuErrorSink QemuErrorSink;
3213 struct QemuErrorSink {
3222 QemuErrorSink *previous;
3225 static QemuErrorSink *qemu_error_sink;
3227 void qemu_errors_to_file(FILE *fp)
3229 QemuErrorSink *sink;
3231 sink = qemu_mallocz(sizeof(*sink));
3232 sink->dest = ERR_SINK_FILE;
3234 sink->previous = qemu_error_sink;
3235 qemu_error_sink = sink;
3238 void qemu_errors_to_mon(Monitor *mon)
3240 QemuErrorSink *sink;
3242 sink = qemu_mallocz(sizeof(*sink));
3243 sink->dest = ERR_SINK_MONITOR;
3245 sink->previous = qemu_error_sink;
3246 qemu_error_sink = sink;
3249 void qemu_errors_to_previous(void)
3251 QemuErrorSink *sink;
3253 assert(qemu_error_sink != NULL);
3254 sink = qemu_error_sink;
3255 qemu_error_sink = sink->previous;
3259 void qemu_error(const char *fmt, ...)
3263 assert(qemu_error_sink != NULL);
3264 switch (qemu_error_sink->dest) {
3266 va_start(args, fmt);
3267 vfprintf(qemu_error_sink->fp, fmt, args);
3270 case ERR_SINK_MONITOR:
3271 va_start(args, fmt);
3272 monitor_vprintf(qemu_error_sink->mon, fmt, args);