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 'F', 's' and 'i')
68 typedef struct mon_cmd_t {
70 const char *args_type;
76 /* file descriptors passed via SCM_RIGHTS */
77 typedef struct mon_fd_t mon_fd_t;
81 LIST_ENTRY(mon_fd_t) next;
92 BlockDriverCompletionFunc *password_completion_cb;
93 void *password_opaque;
94 LIST_HEAD(,mon_fd_t) fds;
95 LIST_ENTRY(Monitor) entry;
98 static LIST_HEAD(mon_list, Monitor) mon_list;
100 static const mon_cmd_t mon_cmds[];
101 static const mon_cmd_t info_cmds[];
103 Monitor *cur_mon = NULL;
105 static void monitor_command_cb(Monitor *mon, const char *cmdline,
108 static void monitor_read_command(Monitor *mon, int show_prompt)
110 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
112 readline_show_prompt(mon->rs);
115 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
119 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
120 /* prompt is printed on return from the command handler */
123 monitor_printf(mon, "terminal does not support password prompting\n");
128 void monitor_flush(Monitor *mon)
130 if (mon && mon->outbuf_index != 0 && mon->chr->focus == 0) {
131 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
132 mon->outbuf_index = 0;
136 /* flush at every end of line or if the buffer is full */
137 static void monitor_puts(Monitor *mon, const char *str)
149 mon->outbuf[mon->outbuf_index++] = '\r';
150 mon->outbuf[mon->outbuf_index++] = c;
151 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
157 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
160 vsnprintf(buf, sizeof(buf), fmt, ap);
161 monitor_puts(mon, buf);
164 void monitor_printf(Monitor *mon, const char *fmt, ...)
168 monitor_vprintf(mon, fmt, ap);
172 void monitor_print_filename(Monitor *mon, const char *filename)
176 for (i = 0; filename[i]; i++) {
177 switch (filename[i]) {
181 monitor_printf(mon, "\\%c", filename[i]);
184 monitor_printf(mon, "\\t");
187 monitor_printf(mon, "\\r");
190 monitor_printf(mon, "\\n");
193 monitor_printf(mon, "%c", filename[i]);
199 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
203 monitor_vprintf((Monitor *)stream, fmt, ap);
208 static int compare_cmd(const char *name, const char *list)
210 const char *p, *pstart;
218 p = pstart + strlen(pstart);
219 if ((p - pstart) == len && !memcmp(pstart, name, len))
228 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
229 const char *prefix, const char *name)
231 const mon_cmd_t *cmd;
233 for(cmd = cmds; cmd->name != NULL; cmd++) {
234 if (!name || !strcmp(name, cmd->name))
235 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
236 cmd->params, cmd->help);
240 static void help_cmd(Monitor *mon, const char *name)
242 if (name && !strcmp(name, "info")) {
243 help_cmd_dump(mon, info_cmds, "info ", NULL);
245 help_cmd_dump(mon, mon_cmds, "", name);
246 if (name && !strcmp(name, "log")) {
247 const CPULogItem *item;
248 monitor_printf(mon, "Log items (comma separated):\n");
249 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
250 for(item = cpu_log_items; item->mask != 0; item++) {
251 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
257 static void do_help_cmd(Monitor *mon, const QDict *qdict)
259 help_cmd(mon, qdict_get_try_str(qdict, "name"));
262 static void do_commit(Monitor *mon, const QDict *qdict)
266 const char *device = qdict_get_str(qdict, "device");
268 all_devices = !strcmp(device, "all");
269 TAILQ_FOREACH(dinfo, &drives, next) {
271 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
273 bdrv_commit(dinfo->bdrv);
277 static void do_info(Monitor *mon, const QDict *qdict)
279 const mon_cmd_t *cmd;
280 const char *item = qdict_get_try_str(qdict, "item");
281 void (*handler)(Monitor *);
285 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
286 if (compare_cmd(item, cmd->name))
290 help_cmd(mon, "info");
293 handler = cmd->handler;
297 static void do_info_version(Monitor *mon)
299 monitor_printf(mon, "%s\n", QEMU_VERSION QEMU_PKGVERSION);
302 static void do_info_name(Monitor *mon)
305 monitor_printf(mon, "%s\n", qemu_name);
308 #if defined(TARGET_I386)
309 static void do_info_hpet(Monitor *mon)
311 monitor_printf(mon, "HPET is %s by QEMU\n",
312 (no_hpet) ? "disabled" : "enabled");
316 static void do_info_uuid(Monitor *mon)
318 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
319 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
320 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
321 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
322 qemu_uuid[14], qemu_uuid[15]);
325 /* get the current CPU defined by the user */
326 static int mon_set_cpu(int cpu_index)
330 for(env = first_cpu; env != NULL; env = env->next_cpu) {
331 if (env->cpu_index == cpu_index) {
332 cur_mon->mon_cpu = env;
339 static CPUState *mon_get_cpu(void)
341 if (!cur_mon->mon_cpu) {
344 cpu_synchronize_state(cur_mon->mon_cpu);
345 return cur_mon->mon_cpu;
348 static void do_info_registers(Monitor *mon)
355 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
358 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
363 static void do_info_cpus(Monitor *mon)
367 /* just to set the default cpu if not already done */
370 for(env = first_cpu; env != NULL; env = env->next_cpu) {
371 cpu_synchronize_state(env);
372 monitor_printf(mon, "%c CPU #%d:",
373 (env == mon->mon_cpu) ? '*' : ' ',
375 #if defined(TARGET_I386)
376 monitor_printf(mon, " pc=0x" TARGET_FMT_lx,
377 env->eip + env->segs[R_CS].base);
378 #elif defined(TARGET_PPC)
379 monitor_printf(mon, " nip=0x" TARGET_FMT_lx, env->nip);
380 #elif defined(TARGET_SPARC)
381 monitor_printf(mon, " pc=0x" TARGET_FMT_lx " npc=0x" TARGET_FMT_lx,
383 #elif defined(TARGET_MIPS)
384 monitor_printf(mon, " PC=0x" TARGET_FMT_lx, env->active_tc.PC);
387 monitor_printf(mon, " (halted)");
388 monitor_printf(mon, "\n");
392 static void do_cpu_set(Monitor *mon, const QDict *qdict)
394 int index = qdict_get_int(qdict, "index");
395 if (mon_set_cpu(index) < 0)
396 monitor_printf(mon, "Invalid CPU index\n");
399 static void do_info_jit(Monitor *mon)
401 dump_exec_info((FILE *)mon, monitor_fprintf);
404 static void do_info_history(Monitor *mon)
413 str = readline_get_history(mon->rs, i);
416 monitor_printf(mon, "%d: '%s'\n", i, str);
421 #if defined(TARGET_PPC)
422 /* XXX: not implemented in other targets */
423 static void do_info_cpu_stats(Monitor *mon)
428 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
432 static void do_quit(Monitor *mon, const QDict *qdict)
437 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
439 if (bdrv_is_inserted(bs)) {
441 if (!bdrv_is_removable(bs)) {
442 monitor_printf(mon, "device is not removable\n");
445 if (bdrv_is_locked(bs)) {
446 monitor_printf(mon, "device is locked\n");
455 static void do_eject(Monitor *mon, const QDict *qdict)
457 BlockDriverState *bs;
458 int force = qdict_get_int(qdict, "force");
459 const char *filename = qdict_get_str(qdict, "filename");
461 bs = bdrv_find(filename);
463 monitor_printf(mon, "device not found\n");
466 eject_device(mon, bs, force);
469 static void do_change_block(Monitor *mon, const char *device,
470 const char *filename, const char *fmt)
472 BlockDriverState *bs;
473 BlockDriver *drv = NULL;
475 bs = bdrv_find(device);
477 monitor_printf(mon, "device not found\n");
481 drv = bdrv_find_format(fmt);
483 monitor_printf(mon, "invalid format %s\n", fmt);
487 if (eject_device(mon, bs, 0) < 0)
489 bdrv_open2(bs, filename, 0, drv);
490 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
493 static void change_vnc_password_cb(Monitor *mon, const char *password,
496 if (vnc_display_password(NULL, password) < 0)
497 monitor_printf(mon, "could not set VNC server password\n");
499 monitor_read_command(mon, 1);
502 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
504 if (strcmp(target, "passwd") == 0 ||
505 strcmp(target, "password") == 0) {
508 strncpy(password, arg, sizeof(password));
509 password[sizeof(password) - 1] = '\0';
510 change_vnc_password_cb(mon, password, NULL);
512 monitor_read_password(mon, change_vnc_password_cb, NULL);
515 if (vnc_display_open(NULL, target) < 0)
516 monitor_printf(mon, "could not start VNC server on %s\n", target);
520 static void do_change(Monitor *mon, const QDict *qdict)
522 const char *device = qdict_get_str(qdict, "device");
523 const char *target = qdict_get_str(qdict, "target");
524 const char *arg = qdict_get_try_str(qdict, "arg");
525 if (strcmp(device, "vnc") == 0) {
526 do_change_vnc(mon, target, arg);
528 do_change_block(mon, device, target, arg);
532 static void do_screen_dump(Monitor *mon, const QDict *qdict)
534 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
537 static void do_logfile(Monitor *mon, const QDict *qdict)
539 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
542 static void do_log(Monitor *mon, const QDict *qdict)
545 const char *items = qdict_get_str(qdict, "items");
547 if (!strcmp(items, "none")) {
550 mask = cpu_str_to_log_mask(items);
552 help_cmd(mon, "log");
559 static void do_singlestep(Monitor *mon, const QDict *qdict)
561 const char *option = qdict_get_try_str(qdict, "option");
562 if (!option || !strcmp(option, "on")) {
564 } else if (!strcmp(option, "off")) {
567 monitor_printf(mon, "unexpected option %s\n", option);
571 static void do_stop(Monitor *mon, const QDict *qdict)
573 vm_stop(EXCP_INTERRUPT);
576 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
578 struct bdrv_iterate_context {
583 static void do_cont(Monitor *mon, const QDict *qdict)
585 struct bdrv_iterate_context context = { mon, 0 };
587 bdrv_iterate(encrypted_bdrv_it, &context);
588 /* only resume the vm if all keys are set and valid */
593 static void bdrv_key_cb(void *opaque, int err)
595 Monitor *mon = opaque;
597 /* another key was set successfully, retry to continue */
602 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
604 struct bdrv_iterate_context *context = opaque;
606 if (!context->err && bdrv_key_required(bs)) {
607 context->err = -EBUSY;
608 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
613 static void do_gdbserver(Monitor *mon, const QDict *qdict)
615 const char *device = qdict_get_try_str(qdict, "device");
617 device = "tcp::" DEFAULT_GDBSTUB_PORT;
618 if (gdbserver_start(device) < 0) {
619 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
621 } else if (strcmp(device, "none") == 0) {
622 monitor_printf(mon, "Disabled gdbserver\n");
624 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
629 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
631 const char *action = qdict_get_str(qdict, "action");
632 if (select_watchdog_action(action) == -1) {
633 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
637 static void monitor_printc(Monitor *mon, int c)
639 monitor_printf(mon, "'");
642 monitor_printf(mon, "\\'");
645 monitor_printf(mon, "\\\\");
648 monitor_printf(mon, "\\n");
651 monitor_printf(mon, "\\r");
654 if (c >= 32 && c <= 126) {
655 monitor_printf(mon, "%c", c);
657 monitor_printf(mon, "\\x%02x", c);
661 monitor_printf(mon, "'");
664 static void memory_dump(Monitor *mon, int count, int format, int wsize,
665 target_phys_addr_t addr, int is_physical)
668 int nb_per_line, l, line_size, i, max_digits, len;
676 if (!env && !is_physical)
681 } else if (wsize == 4) {
684 /* as default we use the current CS size */
688 if ((env->efer & MSR_EFER_LMA) &&
689 (env->segs[R_CS].flags & DESC_L_MASK))
693 if (!(env->segs[R_CS].flags & DESC_B_MASK))
698 monitor_disas(mon, env, addr, count, is_physical, flags);
707 nb_per_line = line_size / wsize;
712 max_digits = (wsize * 8 + 2) / 3;
716 max_digits = (wsize * 8) / 4;
720 max_digits = (wsize * 8 * 10 + 32) / 33;
729 monitor_printf(mon, TARGET_FMT_plx ":", addr);
731 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
736 cpu_physical_memory_rw(addr, buf, l, 0);
741 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
742 monitor_printf(mon, " Cannot access memory\n");
751 v = ldub_raw(buf + i);
754 v = lduw_raw(buf + i);
757 v = (uint32_t)ldl_raw(buf + i);
760 v = ldq_raw(buf + i);
763 monitor_printf(mon, " ");
766 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
769 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
772 monitor_printf(mon, "%*" PRIu64, max_digits, v);
775 monitor_printf(mon, "%*" PRId64, max_digits, v);
778 monitor_printc(mon, v);
783 monitor_printf(mon, "\n");
789 static void do_memory_dump(Monitor *mon, const QDict *qdict)
791 int count = qdict_get_int(qdict, "count");
792 int format = qdict_get_int(qdict, "format");
793 int size = qdict_get_int(qdict, "size");
794 target_long addr = qdict_get_int(qdict, "addr");
796 memory_dump(mon, count, format, size, addr, 0);
799 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
801 int count = qdict_get_int(qdict, "count");
802 int format = qdict_get_int(qdict, "format");
803 int size = qdict_get_int(qdict, "size");
804 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
806 memory_dump(mon, count, format, size, addr, 1);
809 static void do_print(Monitor *mon, const QDict *qdict)
811 int format = qdict_get_int(qdict, "format");
812 target_phys_addr_t val = qdict_get_int(qdict, "val");
814 #if TARGET_PHYS_ADDR_BITS == 32
817 monitor_printf(mon, "%#o", val);
820 monitor_printf(mon, "%#x", val);
823 monitor_printf(mon, "%u", val);
827 monitor_printf(mon, "%d", val);
830 monitor_printc(mon, val);
836 monitor_printf(mon, "%#" PRIo64, val);
839 monitor_printf(mon, "%#" PRIx64, val);
842 monitor_printf(mon, "%" PRIu64, val);
846 monitor_printf(mon, "%" PRId64, val);
849 monitor_printc(mon, val);
853 monitor_printf(mon, "\n");
856 static void do_memory_save(Monitor *mon, const QDict *qdict)
859 uint32_t size = qdict_get_int(qdict, "size");
860 const char *filename = qdict_get_str(qdict, "filename");
861 target_long addr = qdict_get_int(qdict, "val");
870 f = fopen(filename, "wb");
872 monitor_printf(mon, "could not open '%s'\n", filename);
879 cpu_memory_rw_debug(env, addr, buf, l, 0);
880 fwrite(buf, 1, l, f);
887 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
892 uint32_t size = qdict_get_int(qdict, "size");
893 const char *filename = qdict_get_str(qdict, "filename");
894 target_phys_addr_t addr = qdict_get_int(qdict, "val");
896 f = fopen(filename, "wb");
898 monitor_printf(mon, "could not open '%s'\n", filename);
905 cpu_physical_memory_rw(addr, buf, l, 0);
906 fwrite(buf, 1, l, f);
914 static void do_sum(Monitor *mon, const QDict *qdict)
919 uint32_t start = qdict_get_int(qdict, "start");
920 uint32_t size = qdict_get_int(qdict, "size");
923 for(addr = start; addr < (start + size); addr++) {
924 cpu_physical_memory_rw(addr, buf, 1, 0);
925 /* BSD sum algorithm ('sum' Unix command) */
926 sum = (sum >> 1) | (sum << 15);
929 monitor_printf(mon, "%05d\n", sum);
937 static const KeyDef key_defs[] = {
964 { 0x0e, "backspace" },
1001 { 0x37, "asterisk" },
1004 { 0x3a, "caps_lock" },
1015 { 0x45, "num_lock" },
1016 { 0x46, "scroll_lock" },
1018 { 0xb5, "kp_divide" },
1019 { 0x37, "kp_multiply" },
1020 { 0x4a, "kp_subtract" },
1022 { 0x9c, "kp_enter" },
1023 { 0x53, "kp_decimal" },
1056 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1071 { 0xfe, "compose" },
1076 static int get_keycode(const char *key)
1082 for(p = key_defs; p->name != NULL; p++) {
1083 if (!strcmp(key, p->name))
1086 if (strstart(key, "0x", NULL)) {
1087 ret = strtoul(key, &endp, 0);
1088 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1094 #define MAX_KEYCODES 16
1095 static uint8_t keycodes[MAX_KEYCODES];
1096 static int nb_pending_keycodes;
1097 static QEMUTimer *key_timer;
1099 static void release_keys(void *opaque)
1103 while (nb_pending_keycodes > 0) {
1104 nb_pending_keycodes--;
1105 keycode = keycodes[nb_pending_keycodes];
1107 kbd_put_keycode(0xe0);
1108 kbd_put_keycode(keycode | 0x80);
1112 static void do_sendkey(Monitor *mon, const QDict *qdict)
1114 char keyname_buf[16];
1116 int keyname_len, keycode, i;
1117 const char *string = qdict_get_str(qdict, "string");
1118 int has_hold_time = qdict_haskey(qdict, "hold_time");
1119 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1121 if (nb_pending_keycodes > 0) {
1122 qemu_del_timer(key_timer);
1129 separator = strchr(string, '-');
1130 keyname_len = separator ? separator - string : strlen(string);
1131 if (keyname_len > 0) {
1132 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1133 if (keyname_len > sizeof(keyname_buf) - 1) {
1134 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1137 if (i == MAX_KEYCODES) {
1138 monitor_printf(mon, "too many keys\n");
1141 keyname_buf[keyname_len] = 0;
1142 keycode = get_keycode(keyname_buf);
1144 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1147 keycodes[i++] = keycode;
1151 string = separator + 1;
1153 nb_pending_keycodes = i;
1154 /* key down events */
1155 for (i = 0; i < nb_pending_keycodes; i++) {
1156 keycode = keycodes[i];
1158 kbd_put_keycode(0xe0);
1159 kbd_put_keycode(keycode & 0x7f);
1161 /* delayed key up events */
1162 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1163 muldiv64(ticks_per_sec, hold_time, 1000));
1166 static int mouse_button_state;
1168 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1171 const char *dx_str = qdict_get_str(qdict, "dx_str");
1172 const char *dy_str = qdict_get_str(qdict, "dy_str");
1173 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1174 dx = strtol(dx_str, NULL, 0);
1175 dy = strtol(dy_str, NULL, 0);
1178 dz = strtol(dz_str, NULL, 0);
1179 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1182 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1184 int button_state = qdict_get_int(qdict, "button_state");
1185 mouse_button_state = button_state;
1186 kbd_mouse_event(0, 0, 0, mouse_button_state);
1189 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1191 int size = qdict_get_int(qdict, "size");
1192 int addr = qdict_get_int(qdict, "addr");
1193 int has_index = qdict_haskey(qdict, "index");
1198 int index = qdict_get_int(qdict, "index");
1199 cpu_outb(NULL, addr & IOPORTS_MASK, index & 0xff);
1207 val = cpu_inb(NULL, addr);
1211 val = cpu_inw(NULL, addr);
1215 val = cpu_inl(NULL, addr);
1219 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1220 suffix, addr, size * 2, val);
1223 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1225 int size = qdict_get_int(qdict, "size");
1226 int addr = qdict_get_int(qdict, "addr");
1227 int val = qdict_get_int(qdict, "val");
1229 addr &= IOPORTS_MASK;
1234 cpu_outb(NULL, addr, val);
1237 cpu_outw(NULL, addr, val);
1240 cpu_outl(NULL, addr, val);
1245 static void do_boot_set(Monitor *mon, const QDict *qdict)
1248 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1250 res = qemu_boot_set(bootdevice);
1252 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1253 } else if (res > 0) {
1254 monitor_printf(mon, "setting boot device list failed\n");
1256 monitor_printf(mon, "no function defined to set boot device list for "
1257 "this architecture\n");
1261 static void do_system_reset(Monitor *mon, const QDict *qdict)
1263 qemu_system_reset_request();
1266 static void do_system_powerdown(Monitor *mon, const QDict *qdict)
1268 qemu_system_powerdown_request();
1271 #if defined(TARGET_I386)
1272 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1274 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1277 pte & PG_GLOBAL_MASK ? 'G' : '-',
1278 pte & PG_PSE_MASK ? 'P' : '-',
1279 pte & PG_DIRTY_MASK ? 'D' : '-',
1280 pte & PG_ACCESSED_MASK ? 'A' : '-',
1281 pte & PG_PCD_MASK ? 'C' : '-',
1282 pte & PG_PWT_MASK ? 'T' : '-',
1283 pte & PG_USER_MASK ? 'U' : '-',
1284 pte & PG_RW_MASK ? 'W' : '-');
1287 static void tlb_info(Monitor *mon)
1291 uint32_t pgd, pde, pte;
1293 env = mon_get_cpu();
1297 if (!(env->cr[0] & CR0_PG_MASK)) {
1298 monitor_printf(mon, "PG disabled\n");
1301 pgd = env->cr[3] & ~0xfff;
1302 for(l1 = 0; l1 < 1024; l1++) {
1303 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1304 pde = le32_to_cpu(pde);
1305 if (pde & PG_PRESENT_MASK) {
1306 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1307 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1309 for(l2 = 0; l2 < 1024; l2++) {
1310 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1311 (uint8_t *)&pte, 4);
1312 pte = le32_to_cpu(pte);
1313 if (pte & PG_PRESENT_MASK) {
1314 print_pte(mon, (l1 << 22) + (l2 << 12),
1324 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1325 uint32_t end, int prot)
1328 prot1 = *plast_prot;
1329 if (prot != prot1) {
1330 if (*pstart != -1) {
1331 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1332 *pstart, end, end - *pstart,
1333 prot1 & PG_USER_MASK ? 'u' : '-',
1335 prot1 & PG_RW_MASK ? 'w' : '-');
1345 static void mem_info(Monitor *mon)
1348 int l1, l2, prot, last_prot;
1349 uint32_t pgd, pde, pte, start, end;
1351 env = mon_get_cpu();
1355 if (!(env->cr[0] & CR0_PG_MASK)) {
1356 monitor_printf(mon, "PG disabled\n");
1359 pgd = env->cr[3] & ~0xfff;
1362 for(l1 = 0; l1 < 1024; l1++) {
1363 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1364 pde = le32_to_cpu(pde);
1366 if (pde & PG_PRESENT_MASK) {
1367 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1368 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1369 mem_print(mon, &start, &last_prot, end, prot);
1371 for(l2 = 0; l2 < 1024; l2++) {
1372 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1373 (uint8_t *)&pte, 4);
1374 pte = le32_to_cpu(pte);
1375 end = (l1 << 22) + (l2 << 12);
1376 if (pte & PG_PRESENT_MASK) {
1377 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1381 mem_print(mon, &start, &last_prot, end, prot);
1386 mem_print(mon, &start, &last_prot, end, prot);
1392 #if defined(TARGET_SH4)
1394 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1396 monitor_printf(mon, " tlb%i:\t"
1397 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1398 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1399 "dirty=%hhu writethrough=%hhu\n",
1401 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1402 tlb->v, tlb->sh, tlb->c, tlb->pr,
1406 static void tlb_info(Monitor *mon)
1408 CPUState *env = mon_get_cpu();
1411 monitor_printf (mon, "ITLB:\n");
1412 for (i = 0 ; i < ITLB_SIZE ; i++)
1413 print_tlb (mon, i, &env->itlb[i]);
1414 monitor_printf (mon, "UTLB:\n");
1415 for (i = 0 ; i < UTLB_SIZE ; i++)
1416 print_tlb (mon, i, &env->utlb[i]);
1421 static void do_info_kvm(Monitor *mon)
1424 monitor_printf(mon, "kvm support: ");
1426 monitor_printf(mon, "enabled\n");
1428 monitor_printf(mon, "disabled\n");
1430 monitor_printf(mon, "kvm support: not compiled\n");
1434 static void do_info_numa(Monitor *mon)
1439 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1440 for (i = 0; i < nb_numa_nodes; i++) {
1441 monitor_printf(mon, "node %d cpus:", i);
1442 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1443 if (env->numa_node == i) {
1444 monitor_printf(mon, " %d", env->cpu_index);
1447 monitor_printf(mon, "\n");
1448 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1453 #ifdef CONFIG_PROFILER
1455 static void do_info_profile(Monitor *mon)
1461 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1462 dev_time, dev_time / (double)ticks_per_sec);
1463 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1464 qemu_time, qemu_time / (double)ticks_per_sec);
1469 static void do_info_profile(Monitor *mon)
1471 monitor_printf(mon, "Internal profiler not compiled\n");
1475 /* Capture support */
1476 static LIST_HEAD (capture_list_head, CaptureState) capture_head;
1478 static void do_info_capture(Monitor *mon)
1483 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1484 monitor_printf(mon, "[%d]: ", i);
1485 s->ops.info (s->opaque);
1490 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1493 int n = qdict_get_int(qdict, "n");
1496 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1498 s->ops.destroy (s->opaque);
1499 LIST_REMOVE (s, entries);
1506 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1508 const char *path = qdict_get_str(qdict, "path");
1509 int has_freq = qdict_haskey(qdict, "freq");
1510 int freq = qdict_get_try_int(qdict, "freq", -1);
1511 int has_bits = qdict_haskey(qdict, "bits");
1512 int bits = qdict_get_try_int(qdict, "bits", -1);
1513 int has_channels = qdict_haskey(qdict, "nchannels");
1514 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1517 s = qemu_mallocz (sizeof (*s));
1519 freq = has_freq ? freq : 44100;
1520 bits = has_bits ? bits : 16;
1521 nchannels = has_channels ? nchannels : 2;
1523 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1524 monitor_printf(mon, "Faied to add wave capture\n");
1527 LIST_INSERT_HEAD (&capture_head, s, entries);
1531 #if defined(TARGET_I386)
1532 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1535 int cpu_index = qdict_get_int(qdict, "cpu_index");
1537 for (env = first_cpu; env != NULL; env = env->next_cpu)
1538 if (env->cpu_index == cpu_index) {
1539 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1545 static void do_info_status(Monitor *mon)
1549 monitor_printf(mon, "VM status: running (single step mode)\n");
1551 monitor_printf(mon, "VM status: running\n");
1554 monitor_printf(mon, "VM status: paused\n");
1558 static void do_balloon(Monitor *mon, const QDict *qdict)
1560 int value = qdict_get_int(qdict, "value");
1561 ram_addr_t target = value;
1562 qemu_balloon(target << 20);
1565 static void do_info_balloon(Monitor *mon)
1569 actual = qemu_balloon_status();
1570 if (kvm_enabled() && !kvm_has_sync_mmu())
1571 monitor_printf(mon, "Using KVM without synchronous MMU, "
1572 "ballooning disabled\n");
1573 else if (actual == 0)
1574 monitor_printf(mon, "Ballooning not activated in VM\n");
1576 monitor_printf(mon, "balloon: actual=%d\n", (int)(actual >> 20));
1579 static qemu_acl *find_acl(Monitor *mon, const char *name)
1581 qemu_acl *acl = qemu_acl_find(name);
1584 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1589 static void do_acl_show(Monitor *mon, const QDict *qdict)
1591 const char *aclname = qdict_get_str(qdict, "aclname");
1592 qemu_acl *acl = find_acl(mon, aclname);
1593 qemu_acl_entry *entry;
1597 monitor_printf(mon, "policy: %s\n",
1598 acl->defaultDeny ? "deny" : "allow");
1599 TAILQ_FOREACH(entry, &acl->entries, next) {
1601 monitor_printf(mon, "%d: %s %s\n", i,
1602 entry->deny ? "deny" : "allow", entry->match);
1607 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1609 const char *aclname = qdict_get_str(qdict, "aclname");
1610 qemu_acl *acl = find_acl(mon, aclname);
1613 qemu_acl_reset(acl);
1614 monitor_printf(mon, "acl: removed all rules\n");
1618 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1620 const char *aclname = qdict_get_str(qdict, "aclname");
1621 const char *policy = qdict_get_str(qdict, "policy");
1622 qemu_acl *acl = find_acl(mon, aclname);
1625 if (strcmp(policy, "allow") == 0) {
1626 acl->defaultDeny = 0;
1627 monitor_printf(mon, "acl: policy set to 'allow'\n");
1628 } else if (strcmp(policy, "deny") == 0) {
1629 acl->defaultDeny = 1;
1630 monitor_printf(mon, "acl: policy set to 'deny'\n");
1632 monitor_printf(mon, "acl: unknown policy '%s', "
1633 "expected 'deny' or 'allow'\n", policy);
1638 static void do_acl_add(Monitor *mon, const QDict *qdict)
1640 const char *aclname = qdict_get_str(qdict, "aclname");
1641 const char *match = qdict_get_str(qdict, "match");
1642 const char *policy = qdict_get_str(qdict, "policy");
1643 int has_index = qdict_haskey(qdict, "index");
1644 int index = qdict_get_try_int(qdict, "index", -1);
1645 qemu_acl *acl = find_acl(mon, aclname);
1649 if (strcmp(policy, "allow") == 0) {
1651 } else if (strcmp(policy, "deny") == 0) {
1654 monitor_printf(mon, "acl: unknown policy '%s', "
1655 "expected 'deny' or 'allow'\n", policy);
1659 ret = qemu_acl_insert(acl, deny, match, index);
1661 ret = qemu_acl_append(acl, deny, match);
1663 monitor_printf(mon, "acl: unable to add acl entry\n");
1665 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1669 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1671 const char *aclname = qdict_get_str(qdict, "aclname");
1672 const char *match = qdict_get_str(qdict, "match");
1673 qemu_acl *acl = find_acl(mon, aclname);
1677 ret = qemu_acl_remove(acl, match);
1679 monitor_printf(mon, "acl: no matching acl entry\n");
1681 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1685 #if defined(TARGET_I386)
1686 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1689 int cpu_index = qdict_get_int(qdict, "cpu_index");
1690 int bank = qdict_get_int(qdict, "bank");
1691 uint64_t status = qdict_get_int(qdict, "status");
1692 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1693 uint64_t addr = qdict_get_int(qdict, "addr");
1694 uint64_t misc = qdict_get_int(qdict, "misc");
1696 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1697 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1698 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1704 static void do_getfd(Monitor *mon, const QDict *qdict)
1706 const char *fdname = qdict_get_str(qdict, "fdname");
1710 fd = qemu_chr_get_msgfd(mon->chr);
1712 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1716 if (qemu_isdigit(fdname[0])) {
1717 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1723 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1728 LIST_FOREACH(monfd, &mon->fds, next) {
1729 if (strcmp(monfd->name, fdname) != 0) {
1738 monfd = qemu_mallocz(sizeof(mon_fd_t));
1739 monfd->name = qemu_strdup(fdname);
1742 LIST_INSERT_HEAD(&mon->fds, monfd, next);
1745 static void do_closefd(Monitor *mon, const QDict *qdict)
1747 const char *fdname = qdict_get_str(qdict, "fdname");
1750 LIST_FOREACH(monfd, &mon->fds, next) {
1751 if (strcmp(monfd->name, fdname) != 0) {
1755 LIST_REMOVE(monfd, next);
1757 qemu_free(monfd->name);
1762 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1766 static void do_loadvm(Monitor *mon, const QDict *qdict)
1768 int saved_vm_running = vm_running;
1769 const char *name = qdict_get_str(qdict, "name");
1773 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1777 int monitor_get_fd(Monitor *mon, const char *fdname)
1781 LIST_FOREACH(monfd, &mon->fds, next) {
1784 if (strcmp(monfd->name, fdname) != 0) {
1790 /* caller takes ownership of fd */
1791 LIST_REMOVE(monfd, next);
1792 qemu_free(monfd->name);
1801 static const mon_cmd_t mon_cmds[] = {
1802 #include "qemu-monitor.h"
1806 /* Please update qemu-monitor.hx when adding or changing commands */
1807 static const mon_cmd_t info_cmds[] = {
1808 { "version", "", do_info_version,
1809 "", "show the version of QEMU" },
1810 { "network", "", do_info_network,
1811 "", "show the network state" },
1812 { "chardev", "", qemu_chr_info,
1813 "", "show the character devices" },
1814 { "block", "", bdrv_info,
1815 "", "show the block devices" },
1816 { "blockstats", "", bdrv_info_stats,
1817 "", "show block device statistics" },
1818 { "registers", "", do_info_registers,
1819 "", "show the cpu registers" },
1820 { "cpus", "", do_info_cpus,
1821 "", "show infos for each CPU" },
1822 { "history", "", do_info_history,
1823 "", "show the command line history", },
1824 { "irq", "", irq_info,
1825 "", "show the interrupts statistics (if available)", },
1826 { "pic", "", pic_info,
1827 "", "show i8259 (PIC) state", },
1828 { "pci", "", pci_info,
1829 "", "show PCI info", },
1830 #if defined(TARGET_I386) || defined(TARGET_SH4)
1831 { "tlb", "", tlb_info,
1832 "", "show virtual to physical memory mappings", },
1834 #if defined(TARGET_I386)
1835 { "mem", "", mem_info,
1836 "", "show the active virtual memory mappings", },
1837 { "hpet", "", do_info_hpet,
1838 "", "show state of HPET", },
1840 { "jit", "", do_info_jit,
1841 "", "show dynamic compiler info", },
1842 { "kvm", "", do_info_kvm,
1843 "", "show KVM information", },
1844 { "numa", "", do_info_numa,
1845 "", "show NUMA information", },
1846 { "usb", "", usb_info,
1847 "", "show guest USB devices", },
1848 { "usbhost", "", usb_host_info,
1849 "", "show host USB devices", },
1850 { "profile", "", do_info_profile,
1851 "", "show profiling information", },
1852 { "capture", "", do_info_capture,
1853 "", "show capture information" },
1854 { "snapshots", "", do_info_snapshots,
1855 "", "show the currently saved VM snapshots" },
1856 { "status", "", do_info_status,
1857 "", "show the current VM status (running|paused)" },
1858 { "pcmcia", "", pcmcia_info,
1859 "", "show guest PCMCIA status" },
1860 { "mice", "", do_info_mice,
1861 "", "show which guest mouse is receiving events" },
1862 { "vnc", "", do_info_vnc,
1863 "", "show the vnc server status"},
1864 { "name", "", do_info_name,
1865 "", "show the current VM name" },
1866 { "uuid", "", do_info_uuid,
1867 "", "show the current VM UUID" },
1868 #if defined(TARGET_PPC)
1869 { "cpustats", "", do_info_cpu_stats,
1870 "", "show CPU statistics", },
1872 #if defined(CONFIG_SLIRP)
1873 { "usernet", "", do_info_usernet,
1874 "", "show user network stack connection states", },
1876 { "migrate", "", do_info_migrate, "", "show migration status" },
1877 { "balloon", "", do_info_balloon,
1878 "", "show balloon information" },
1879 { "qtree", "", do_info_qtree,
1880 "", "show device tree" },
1881 { "qdm", "", do_info_qdm,
1882 "", "show qdev device model list" },
1886 /*******************************************************************/
1888 static const char *pch;
1889 static jmp_buf expr_env;
1894 typedef struct MonitorDef {
1897 target_long (*get_value)(const struct MonitorDef *md, int val);
1901 #if defined(TARGET_I386)
1902 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
1904 CPUState *env = mon_get_cpu();
1907 return env->eip + env->segs[R_CS].base;
1911 #if defined(TARGET_PPC)
1912 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
1914 CPUState *env = mon_get_cpu();
1922 for (i = 0; i < 8; i++)
1923 u |= env->crf[i] << (32 - (4 * i));
1928 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
1930 CPUState *env = mon_get_cpu();
1936 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
1938 CPUState *env = mon_get_cpu();
1944 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
1946 CPUState *env = mon_get_cpu();
1949 return cpu_ppc_load_decr(env);
1952 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
1954 CPUState *env = mon_get_cpu();
1957 return cpu_ppc_load_tbu(env);
1960 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
1962 CPUState *env = mon_get_cpu();
1965 return cpu_ppc_load_tbl(env);
1969 #if defined(TARGET_SPARC)
1970 #ifndef TARGET_SPARC64
1971 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
1973 CPUState *env = mon_get_cpu();
1976 return GET_PSR(env);
1980 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
1982 CPUState *env = mon_get_cpu();
1985 return env->regwptr[val];
1989 static const MonitorDef monitor_defs[] = {
1992 #define SEG(name, seg) \
1993 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
1994 { name ".base", offsetof(CPUState, segs[seg].base) },\
1995 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
1997 { "eax", offsetof(CPUState, regs[0]) },
1998 { "ecx", offsetof(CPUState, regs[1]) },
1999 { "edx", offsetof(CPUState, regs[2]) },
2000 { "ebx", offsetof(CPUState, regs[3]) },
2001 { "esp|sp", offsetof(CPUState, regs[4]) },
2002 { "ebp|fp", offsetof(CPUState, regs[5]) },
2003 { "esi", offsetof(CPUState, regs[6]) },
2004 { "edi", offsetof(CPUState, regs[7]) },
2005 #ifdef TARGET_X86_64
2006 { "r8", offsetof(CPUState, regs[8]) },
2007 { "r9", offsetof(CPUState, regs[9]) },
2008 { "r10", offsetof(CPUState, regs[10]) },
2009 { "r11", offsetof(CPUState, regs[11]) },
2010 { "r12", offsetof(CPUState, regs[12]) },
2011 { "r13", offsetof(CPUState, regs[13]) },
2012 { "r14", offsetof(CPUState, regs[14]) },
2013 { "r15", offsetof(CPUState, regs[15]) },
2015 { "eflags", offsetof(CPUState, eflags) },
2016 { "eip", offsetof(CPUState, eip) },
2023 { "pc", 0, monitor_get_pc, },
2024 #elif defined(TARGET_PPC)
2025 /* General purpose registers */
2026 { "r0", offsetof(CPUState, gpr[0]) },
2027 { "r1", offsetof(CPUState, gpr[1]) },
2028 { "r2", offsetof(CPUState, gpr[2]) },
2029 { "r3", offsetof(CPUState, gpr[3]) },
2030 { "r4", offsetof(CPUState, gpr[4]) },
2031 { "r5", offsetof(CPUState, gpr[5]) },
2032 { "r6", offsetof(CPUState, gpr[6]) },
2033 { "r7", offsetof(CPUState, gpr[7]) },
2034 { "r8", offsetof(CPUState, gpr[8]) },
2035 { "r9", offsetof(CPUState, gpr[9]) },
2036 { "r10", offsetof(CPUState, gpr[10]) },
2037 { "r11", offsetof(CPUState, gpr[11]) },
2038 { "r12", offsetof(CPUState, gpr[12]) },
2039 { "r13", offsetof(CPUState, gpr[13]) },
2040 { "r14", offsetof(CPUState, gpr[14]) },
2041 { "r15", offsetof(CPUState, gpr[15]) },
2042 { "r16", offsetof(CPUState, gpr[16]) },
2043 { "r17", offsetof(CPUState, gpr[17]) },
2044 { "r18", offsetof(CPUState, gpr[18]) },
2045 { "r19", offsetof(CPUState, gpr[19]) },
2046 { "r20", offsetof(CPUState, gpr[20]) },
2047 { "r21", offsetof(CPUState, gpr[21]) },
2048 { "r22", offsetof(CPUState, gpr[22]) },
2049 { "r23", offsetof(CPUState, gpr[23]) },
2050 { "r24", offsetof(CPUState, gpr[24]) },
2051 { "r25", offsetof(CPUState, gpr[25]) },
2052 { "r26", offsetof(CPUState, gpr[26]) },
2053 { "r27", offsetof(CPUState, gpr[27]) },
2054 { "r28", offsetof(CPUState, gpr[28]) },
2055 { "r29", offsetof(CPUState, gpr[29]) },
2056 { "r30", offsetof(CPUState, gpr[30]) },
2057 { "r31", offsetof(CPUState, gpr[31]) },
2058 /* Floating point registers */
2059 { "f0", offsetof(CPUState, fpr[0]) },
2060 { "f1", offsetof(CPUState, fpr[1]) },
2061 { "f2", offsetof(CPUState, fpr[2]) },
2062 { "f3", offsetof(CPUState, fpr[3]) },
2063 { "f4", offsetof(CPUState, fpr[4]) },
2064 { "f5", offsetof(CPUState, fpr[5]) },
2065 { "f6", offsetof(CPUState, fpr[6]) },
2066 { "f7", offsetof(CPUState, fpr[7]) },
2067 { "f8", offsetof(CPUState, fpr[8]) },
2068 { "f9", offsetof(CPUState, fpr[9]) },
2069 { "f10", offsetof(CPUState, fpr[10]) },
2070 { "f11", offsetof(CPUState, fpr[11]) },
2071 { "f12", offsetof(CPUState, fpr[12]) },
2072 { "f13", offsetof(CPUState, fpr[13]) },
2073 { "f14", offsetof(CPUState, fpr[14]) },
2074 { "f15", offsetof(CPUState, fpr[15]) },
2075 { "f16", offsetof(CPUState, fpr[16]) },
2076 { "f17", offsetof(CPUState, fpr[17]) },
2077 { "f18", offsetof(CPUState, fpr[18]) },
2078 { "f19", offsetof(CPUState, fpr[19]) },
2079 { "f20", offsetof(CPUState, fpr[20]) },
2080 { "f21", offsetof(CPUState, fpr[21]) },
2081 { "f22", offsetof(CPUState, fpr[22]) },
2082 { "f23", offsetof(CPUState, fpr[23]) },
2083 { "f24", offsetof(CPUState, fpr[24]) },
2084 { "f25", offsetof(CPUState, fpr[25]) },
2085 { "f26", offsetof(CPUState, fpr[26]) },
2086 { "f27", offsetof(CPUState, fpr[27]) },
2087 { "f28", offsetof(CPUState, fpr[28]) },
2088 { "f29", offsetof(CPUState, fpr[29]) },
2089 { "f30", offsetof(CPUState, fpr[30]) },
2090 { "f31", offsetof(CPUState, fpr[31]) },
2091 { "fpscr", offsetof(CPUState, fpscr) },
2092 /* Next instruction pointer */
2093 { "nip|pc", offsetof(CPUState, nip) },
2094 { "lr", offsetof(CPUState, lr) },
2095 { "ctr", offsetof(CPUState, ctr) },
2096 { "decr", 0, &monitor_get_decr, },
2097 { "ccr", 0, &monitor_get_ccr, },
2098 /* Machine state register */
2099 { "msr", 0, &monitor_get_msr, },
2100 { "xer", 0, &monitor_get_xer, },
2101 { "tbu", 0, &monitor_get_tbu, },
2102 { "tbl", 0, &monitor_get_tbl, },
2103 #if defined(TARGET_PPC64)
2104 /* Address space register */
2105 { "asr", offsetof(CPUState, asr) },
2107 /* Segment registers */
2108 { "sdr1", offsetof(CPUState, sdr1) },
2109 { "sr0", offsetof(CPUState, sr[0]) },
2110 { "sr1", offsetof(CPUState, sr[1]) },
2111 { "sr2", offsetof(CPUState, sr[2]) },
2112 { "sr3", offsetof(CPUState, sr[3]) },
2113 { "sr4", offsetof(CPUState, sr[4]) },
2114 { "sr5", offsetof(CPUState, sr[5]) },
2115 { "sr6", offsetof(CPUState, sr[6]) },
2116 { "sr7", offsetof(CPUState, sr[7]) },
2117 { "sr8", offsetof(CPUState, sr[8]) },
2118 { "sr9", offsetof(CPUState, sr[9]) },
2119 { "sr10", offsetof(CPUState, sr[10]) },
2120 { "sr11", offsetof(CPUState, sr[11]) },
2121 { "sr12", offsetof(CPUState, sr[12]) },
2122 { "sr13", offsetof(CPUState, sr[13]) },
2123 { "sr14", offsetof(CPUState, sr[14]) },
2124 { "sr15", offsetof(CPUState, sr[15]) },
2125 /* Too lazy to put BATs and SPRs ... */
2126 #elif defined(TARGET_SPARC)
2127 { "g0", offsetof(CPUState, gregs[0]) },
2128 { "g1", offsetof(CPUState, gregs[1]) },
2129 { "g2", offsetof(CPUState, gregs[2]) },
2130 { "g3", offsetof(CPUState, gregs[3]) },
2131 { "g4", offsetof(CPUState, gregs[4]) },
2132 { "g5", offsetof(CPUState, gregs[5]) },
2133 { "g6", offsetof(CPUState, gregs[6]) },
2134 { "g7", offsetof(CPUState, gregs[7]) },
2135 { "o0", 0, monitor_get_reg },
2136 { "o1", 1, monitor_get_reg },
2137 { "o2", 2, monitor_get_reg },
2138 { "o3", 3, monitor_get_reg },
2139 { "o4", 4, monitor_get_reg },
2140 { "o5", 5, monitor_get_reg },
2141 { "o6", 6, monitor_get_reg },
2142 { "o7", 7, monitor_get_reg },
2143 { "l0", 8, monitor_get_reg },
2144 { "l1", 9, monitor_get_reg },
2145 { "l2", 10, monitor_get_reg },
2146 { "l3", 11, monitor_get_reg },
2147 { "l4", 12, monitor_get_reg },
2148 { "l5", 13, monitor_get_reg },
2149 { "l6", 14, monitor_get_reg },
2150 { "l7", 15, monitor_get_reg },
2151 { "i0", 16, monitor_get_reg },
2152 { "i1", 17, monitor_get_reg },
2153 { "i2", 18, monitor_get_reg },
2154 { "i3", 19, monitor_get_reg },
2155 { "i4", 20, monitor_get_reg },
2156 { "i5", 21, monitor_get_reg },
2157 { "i6", 22, monitor_get_reg },
2158 { "i7", 23, monitor_get_reg },
2159 { "pc", offsetof(CPUState, pc) },
2160 { "npc", offsetof(CPUState, npc) },
2161 { "y", offsetof(CPUState, y) },
2162 #ifndef TARGET_SPARC64
2163 { "psr", 0, &monitor_get_psr, },
2164 { "wim", offsetof(CPUState, wim) },
2166 { "tbr", offsetof(CPUState, tbr) },
2167 { "fsr", offsetof(CPUState, fsr) },
2168 { "f0", offsetof(CPUState, fpr[0]) },
2169 { "f1", offsetof(CPUState, fpr[1]) },
2170 { "f2", offsetof(CPUState, fpr[2]) },
2171 { "f3", offsetof(CPUState, fpr[3]) },
2172 { "f4", offsetof(CPUState, fpr[4]) },
2173 { "f5", offsetof(CPUState, fpr[5]) },
2174 { "f6", offsetof(CPUState, fpr[6]) },
2175 { "f7", offsetof(CPUState, fpr[7]) },
2176 { "f8", offsetof(CPUState, fpr[8]) },
2177 { "f9", offsetof(CPUState, fpr[9]) },
2178 { "f10", offsetof(CPUState, fpr[10]) },
2179 { "f11", offsetof(CPUState, fpr[11]) },
2180 { "f12", offsetof(CPUState, fpr[12]) },
2181 { "f13", offsetof(CPUState, fpr[13]) },
2182 { "f14", offsetof(CPUState, fpr[14]) },
2183 { "f15", offsetof(CPUState, fpr[15]) },
2184 { "f16", offsetof(CPUState, fpr[16]) },
2185 { "f17", offsetof(CPUState, fpr[17]) },
2186 { "f18", offsetof(CPUState, fpr[18]) },
2187 { "f19", offsetof(CPUState, fpr[19]) },
2188 { "f20", offsetof(CPUState, fpr[20]) },
2189 { "f21", offsetof(CPUState, fpr[21]) },
2190 { "f22", offsetof(CPUState, fpr[22]) },
2191 { "f23", offsetof(CPUState, fpr[23]) },
2192 { "f24", offsetof(CPUState, fpr[24]) },
2193 { "f25", offsetof(CPUState, fpr[25]) },
2194 { "f26", offsetof(CPUState, fpr[26]) },
2195 { "f27", offsetof(CPUState, fpr[27]) },
2196 { "f28", offsetof(CPUState, fpr[28]) },
2197 { "f29", offsetof(CPUState, fpr[29]) },
2198 { "f30", offsetof(CPUState, fpr[30]) },
2199 { "f31", offsetof(CPUState, fpr[31]) },
2200 #ifdef TARGET_SPARC64
2201 { "f32", offsetof(CPUState, fpr[32]) },
2202 { "f34", offsetof(CPUState, fpr[34]) },
2203 { "f36", offsetof(CPUState, fpr[36]) },
2204 { "f38", offsetof(CPUState, fpr[38]) },
2205 { "f40", offsetof(CPUState, fpr[40]) },
2206 { "f42", offsetof(CPUState, fpr[42]) },
2207 { "f44", offsetof(CPUState, fpr[44]) },
2208 { "f46", offsetof(CPUState, fpr[46]) },
2209 { "f48", offsetof(CPUState, fpr[48]) },
2210 { "f50", offsetof(CPUState, fpr[50]) },
2211 { "f52", offsetof(CPUState, fpr[52]) },
2212 { "f54", offsetof(CPUState, fpr[54]) },
2213 { "f56", offsetof(CPUState, fpr[56]) },
2214 { "f58", offsetof(CPUState, fpr[58]) },
2215 { "f60", offsetof(CPUState, fpr[60]) },
2216 { "f62", offsetof(CPUState, fpr[62]) },
2217 { "asi", offsetof(CPUState, asi) },
2218 { "pstate", offsetof(CPUState, pstate) },
2219 { "cansave", offsetof(CPUState, cansave) },
2220 { "canrestore", offsetof(CPUState, canrestore) },
2221 { "otherwin", offsetof(CPUState, otherwin) },
2222 { "wstate", offsetof(CPUState, wstate) },
2223 { "cleanwin", offsetof(CPUState, cleanwin) },
2224 { "fprs", offsetof(CPUState, fprs) },
2230 static void expr_error(Monitor *mon, const char *msg)
2232 monitor_printf(mon, "%s\n", msg);
2233 longjmp(expr_env, 1);
2236 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2237 static int get_monitor_def(target_long *pval, const char *name)
2239 const MonitorDef *md;
2242 for(md = monitor_defs; md->name != NULL; md++) {
2243 if (compare_cmd(name, md->name)) {
2244 if (md->get_value) {
2245 *pval = md->get_value(md, md->offset);
2247 CPUState *env = mon_get_cpu();
2250 ptr = (uint8_t *)env + md->offset;
2253 *pval = *(int32_t *)ptr;
2256 *pval = *(target_long *)ptr;
2269 static void next(void)
2273 while (qemu_isspace(*pch))
2278 static int64_t expr_sum(Monitor *mon);
2280 static int64_t expr_unary(Monitor *mon)
2289 n = expr_unary(mon);
2293 n = -expr_unary(mon);
2297 n = ~expr_unary(mon);
2303 expr_error(mon, "')' expected");
2310 expr_error(mon, "character constant expected");
2314 expr_error(mon, "missing terminating \' character");
2324 while ((*pch >= 'a' && *pch <= 'z') ||
2325 (*pch >= 'A' && *pch <= 'Z') ||
2326 (*pch >= '0' && *pch <= '9') ||
2327 *pch == '_' || *pch == '.') {
2328 if ((q - buf) < sizeof(buf) - 1)
2332 while (qemu_isspace(*pch))
2335 ret = get_monitor_def(®, buf);
2337 expr_error(mon, "unknown register");
2339 expr_error(mon, "no cpu defined");
2344 expr_error(mon, "unexpected end of expression");
2348 #if TARGET_PHYS_ADDR_BITS > 32
2349 n = strtoull(pch, &p, 0);
2351 n = strtoul(pch, &p, 0);
2354 expr_error(mon, "invalid char in expression");
2357 while (qemu_isspace(*pch))
2365 static int64_t expr_prod(Monitor *mon)
2370 val = expr_unary(mon);
2373 if (op != '*' && op != '/' && op != '%')
2376 val2 = expr_unary(mon);
2385 expr_error(mon, "division by zero");
2396 static int64_t expr_logic(Monitor *mon)
2401 val = expr_prod(mon);
2404 if (op != '&' && op != '|' && op != '^')
2407 val2 = expr_prod(mon);
2424 static int64_t expr_sum(Monitor *mon)
2429 val = expr_logic(mon);
2432 if (op != '+' && op != '-')
2435 val2 = expr_logic(mon);
2444 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2447 if (setjmp(expr_env)) {
2451 while (qemu_isspace(*pch))
2453 *pval = expr_sum(mon);
2458 static int get_str(char *buf, int buf_size, const char **pp)
2466 while (qemu_isspace(*p))
2476 while (*p != '\0' && *p != '\"') {
2492 qemu_printf("unsupported escape code: '\\%c'\n", c);
2495 if ((q - buf) < buf_size - 1) {
2499 if ((q - buf) < buf_size - 1) {
2506 qemu_printf("unterminated string\n");
2511 while (*p != '\0' && !qemu_isspace(*p)) {
2512 if ((q - buf) < buf_size - 1) {
2524 * Store the command-name in cmdname, and return a pointer to
2525 * the remaining of the command string.
2527 static const char *get_command_name(const char *cmdline,
2528 char *cmdname, size_t nlen)
2531 const char *p, *pstart;
2534 while (qemu_isspace(*p))
2539 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2544 memcpy(cmdname, pstart, len);
2545 cmdname[len] = '\0';
2550 * Read key of 'type' into 'key' and return the current
2553 static char *key_get_info(const char *type, char **key)
2561 p = strchr(type, ':');
2568 str = qemu_malloc(len + 1);
2569 memcpy(str, type, len);
2576 static int default_fmt_format = 'x';
2577 static int default_fmt_size = 4;
2581 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2582 const char *cmdline,
2585 const char *p, *typestr;
2587 const mon_cmd_t *cmd;
2593 monitor_printf(mon, "command='%s'\n", cmdline);
2596 /* extract the command name */
2597 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2601 /* find the command */
2602 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2603 if (compare_cmd(cmdname, cmd->name))
2607 if (cmd->name == NULL) {
2608 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2612 /* parse the parameters */
2613 typestr = cmd->args_type;
2615 typestr = key_get_info(typestr, &key);
2627 while (qemu_isspace(*p))
2629 if (*typestr == '?') {
2632 /* no optional string: NULL argument */
2636 ret = get_str(buf, sizeof(buf), &p);
2640 monitor_printf(mon, "%s: filename expected\n",
2644 monitor_printf(mon, "%s: block device name expected\n",
2648 monitor_printf(mon, "%s: string expected\n", cmdname);
2653 qdict_put(qdict, key, qstring_from_str(buf));
2658 int count, format, size;
2660 while (qemu_isspace(*p))
2666 if (qemu_isdigit(*p)) {
2668 while (qemu_isdigit(*p)) {
2669 count = count * 10 + (*p - '0');
2707 if (*p != '\0' && !qemu_isspace(*p)) {
2708 monitor_printf(mon, "invalid char in format: '%c'\n",
2713 format = default_fmt_format;
2714 if (format != 'i') {
2715 /* for 'i', not specifying a size gives -1 as size */
2717 size = default_fmt_size;
2718 default_fmt_size = size;
2720 default_fmt_format = format;
2723 format = default_fmt_format;
2724 if (format != 'i') {
2725 size = default_fmt_size;
2730 qdict_put(qdict, "count", qint_from_int(count));
2731 qdict_put(qdict, "format", qint_from_int(format));
2732 qdict_put(qdict, "size", qint_from_int(size));
2740 while (qemu_isspace(*p))
2742 if (*typestr == '?' || *typestr == '.') {
2743 if (*typestr == '?') {
2751 while (qemu_isspace(*p))
2760 if (get_expr(mon, &val, &p))
2762 qdict_put(qdict, key, qint_from_int(val));
2773 while (qemu_isspace(*p))
2779 monitor_printf(mon, "%s: unsupported option -%c\n",
2786 qdict_put(qdict, key, qint_from_int(has_option));
2791 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
2797 /* check that all arguments were parsed */
2798 while (qemu_isspace(*p))
2801 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
2813 static void monitor_handle_command(Monitor *mon, const char *cmdline)
2816 const mon_cmd_t *cmd;
2818 qdict = qdict_new();
2820 cmd = monitor_parse_command(mon, cmdline, qdict);
2822 void (*handler)(Monitor *mon, const QDict *qdict);
2824 qemu_errors_to_mon(mon);
2826 handler = cmd->handler;
2827 handler(mon, qdict);
2829 qemu_errors_to_previous();
2835 static void cmd_completion(const char *name, const char *list)
2837 const char *p, *pstart;
2846 p = pstart + strlen(pstart);
2848 if (len > sizeof(cmd) - 2)
2849 len = sizeof(cmd) - 2;
2850 memcpy(cmd, pstart, len);
2852 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
2853 readline_add_completion(cur_mon->rs, cmd);
2861 static void file_completion(const char *input)
2866 char file[1024], file_prefix[1024];
2870 p = strrchr(input, '/');
2873 pstrcpy(file_prefix, sizeof(file_prefix), input);
2874 pstrcpy(path, sizeof(path), ".");
2876 input_path_len = p - input + 1;
2877 memcpy(path, input, input_path_len);
2878 if (input_path_len > sizeof(path) - 1)
2879 input_path_len = sizeof(path) - 1;
2880 path[input_path_len] = '\0';
2881 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
2883 #ifdef DEBUG_COMPLETION
2884 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
2885 input, path, file_prefix);
2887 ffs = opendir(path);
2895 if (strstart(d->d_name, file_prefix, NULL)) {
2896 memcpy(file, input, input_path_len);
2897 if (input_path_len < sizeof(file))
2898 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
2900 /* stat the file to find out if it's a directory.
2901 * In that case add a slash to speed up typing long paths
2904 if(S_ISDIR(sb.st_mode))
2905 pstrcat(file, sizeof(file), "/");
2906 readline_add_completion(cur_mon->rs, file);
2912 static void block_completion_it(void *opaque, BlockDriverState *bs)
2914 const char *name = bdrv_get_device_name(bs);
2915 const char *input = opaque;
2917 if (input[0] == '\0' ||
2918 !strncmp(name, (char *)input, strlen(input))) {
2919 readline_add_completion(cur_mon->rs, name);
2923 /* NOTE: this parser is an approximate form of the real command parser */
2924 static void parse_cmdline(const char *cmdline,
2925 int *pnb_args, char **args)
2934 while (qemu_isspace(*p))
2938 if (nb_args >= MAX_ARGS)
2940 ret = get_str(buf, sizeof(buf), &p);
2941 args[nb_args] = qemu_strdup(buf);
2946 *pnb_args = nb_args;
2949 static const char *next_arg_type(const char *typestr)
2951 const char *p = strchr(typestr, ':');
2952 return (p != NULL ? ++p : typestr);
2955 static void monitor_find_completion(const char *cmdline)
2957 const char *cmdname;
2958 char *args[MAX_ARGS];
2959 int nb_args, i, len;
2960 const char *ptype, *str;
2961 const mon_cmd_t *cmd;
2964 parse_cmdline(cmdline, &nb_args, args);
2965 #ifdef DEBUG_COMPLETION
2966 for(i = 0; i < nb_args; i++) {
2967 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
2971 /* if the line ends with a space, it means we want to complete the
2973 len = strlen(cmdline);
2974 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
2975 if (nb_args >= MAX_ARGS)
2977 args[nb_args++] = qemu_strdup("");
2980 /* command completion */
2985 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
2986 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2987 cmd_completion(cmdname, cmd->name);
2990 /* find the command */
2991 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2992 if (compare_cmd(args[0], cmd->name))
2997 ptype = next_arg_type(cmd->args_type);
2998 for(i = 0; i < nb_args - 2; i++) {
2999 if (*ptype != '\0') {
3000 ptype = next_arg_type(ptype);
3001 while (*ptype == '?')
3002 ptype = next_arg_type(ptype);
3005 str = args[nb_args - 1];
3006 if (*ptype == '-' && ptype[1] != '\0') {
3011 /* file completion */
3012 readline_set_completion_index(cur_mon->rs, strlen(str));
3013 file_completion(str);
3016 /* block device name completion */
3017 readline_set_completion_index(cur_mon->rs, strlen(str));
3018 bdrv_iterate(block_completion_it, (void *)str);
3021 /* XXX: more generic ? */
3022 if (!strcmp(cmd->name, "info")) {
3023 readline_set_completion_index(cur_mon->rs, strlen(str));
3024 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3025 cmd_completion(str, cmd->name);
3027 } else if (!strcmp(cmd->name, "sendkey")) {
3028 char *sep = strrchr(str, '-');
3031 readline_set_completion_index(cur_mon->rs, strlen(str));
3032 for(key = key_defs; key->name != NULL; key++) {
3033 cmd_completion(str, key->name);
3035 } else if (!strcmp(cmd->name, "help|?")) {
3036 readline_set_completion_index(cur_mon->rs, strlen(str));
3037 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3038 cmd_completion(str, cmd->name);
3046 for(i = 0; i < nb_args; i++)
3050 static int monitor_can_read(void *opaque)
3052 Monitor *mon = opaque;
3054 return (mon->suspend_cnt == 0) ? 128 : 0;
3057 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3059 Monitor *old_mon = cur_mon;
3065 for (i = 0; i < size; i++)
3066 readline_handle_byte(cur_mon->rs, buf[i]);
3068 if (size == 0 || buf[size - 1] != 0)
3069 monitor_printf(cur_mon, "corrupted command\n");
3071 monitor_handle_command(cur_mon, (char *)buf);
3077 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3079 monitor_suspend(mon);
3080 monitor_handle_command(mon, cmdline);
3081 monitor_resume(mon);
3084 int monitor_suspend(Monitor *mon)
3092 void monitor_resume(Monitor *mon)
3096 if (--mon->suspend_cnt == 0)
3097 readline_show_prompt(mon->rs);
3100 static void monitor_event(void *opaque, int event)
3102 Monitor *mon = opaque;
3105 case CHR_EVENT_MUX_IN:
3106 readline_restart(mon->rs);
3107 monitor_resume(mon);
3111 case CHR_EVENT_MUX_OUT:
3112 if (mon->suspend_cnt == 0)
3113 monitor_printf(mon, "\n");
3115 monitor_suspend(mon);
3118 case CHR_EVENT_RESET:
3119 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3120 "information\n", QEMU_VERSION);
3121 if (mon->chr->focus == 0)
3122 readline_show_prompt(mon->rs);
3136 void monitor_init(CharDriverState *chr, int flags)
3138 static int is_first_init = 1;
3141 if (is_first_init) {
3142 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3146 mon = qemu_mallocz(sizeof(*mon));
3150 if (mon->chr->focus != 0)
3151 mon->suspend_cnt = 1; /* mux'ed monitors start suspended */
3152 if (flags & MONITOR_USE_READLINE) {
3153 mon->rs = readline_init(mon, monitor_find_completion);
3154 monitor_read_command(mon, 0);
3157 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3160 LIST_INSERT_HEAD(&mon_list, mon, entry);
3161 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3165 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3167 BlockDriverState *bs = opaque;
3170 if (bdrv_set_key(bs, password) != 0) {
3171 monitor_printf(mon, "invalid password\n");
3174 if (mon->password_completion_cb)
3175 mon->password_completion_cb(mon->password_opaque, ret);
3177 monitor_read_command(mon, 1);
3180 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3181 BlockDriverCompletionFunc *completion_cb,
3186 if (!bdrv_key_required(bs)) {
3188 completion_cb(opaque, 0);
3192 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3193 bdrv_get_encrypted_filename(bs));
3195 mon->password_completion_cb = completion_cb;
3196 mon->password_opaque = opaque;
3198 err = monitor_read_password(mon, bdrv_password_cb, bs);
3200 if (err && completion_cb)
3201 completion_cb(opaque, err);
3204 typedef struct QemuErrorSink QemuErrorSink;
3205 struct QemuErrorSink {
3214 QemuErrorSink *previous;
3217 static QemuErrorSink *qemu_error_sink;
3219 void qemu_errors_to_file(FILE *fp)
3221 QemuErrorSink *sink;
3223 sink = qemu_mallocz(sizeof(*sink));
3224 sink->dest = ERR_SINK_FILE;
3226 sink->previous = qemu_error_sink;
3227 qemu_error_sink = sink;
3230 void qemu_errors_to_mon(Monitor *mon)
3232 QemuErrorSink *sink;
3234 sink = qemu_mallocz(sizeof(*sink));
3235 sink->dest = ERR_SINK_MONITOR;
3237 sink->previous = qemu_error_sink;
3238 qemu_error_sink = sink;
3241 void qemu_errors_to_previous(void)
3243 QemuErrorSink *sink;
3245 assert(qemu_error_sink != NULL);
3246 sink = qemu_error_sink;
3247 qemu_error_sink = sink->previous;
3251 void qemu_error(const char *fmt, ...)
3255 assert(qemu_error_sink != NULL);
3256 switch (qemu_error_sink->dest) {
3258 va_start(args, fmt);
3259 vfprintf(qemu_error_sink->fp, fmt, args);
3262 case ERR_SINK_MONITOR:
3263 va_start(args, fmt);
3264 monitor_vprintf(qemu_error_sink->mon, fmt, args);