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"
32 #include "hw/loader.h"
35 #include "qemu-char.h"
41 #include "audio/audio.h"
44 #include "qemu-timer.h"
45 #include "migration.h"
54 //#define DEBUG_COMPLETION
60 * 'B' block device name
61 * 's' string (accept optional quote)
63 * 'l' target long (32 or 64 bit)
64 * '/' optional gdb-like print format (like "/10x")
66 * '?' optional type (for all types, except '/')
67 * '.' other form of optional type (for 'i' and 'l')
68 * '-' optional parameter (eg. '-f')
72 typedef struct mon_cmd_t {
74 const char *args_type;
77 void (*user_print)(Monitor *mon, const QObject *data);
79 void (*info)(Monitor *mon);
80 void (*info_new)(Monitor *mon, QObject **ret_data);
81 void (*cmd)(Monitor *mon, const QDict *qdict);
82 void (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
86 /* file descriptors passed via SCM_RIGHTS */
87 typedef struct mon_fd_t mon_fd_t;
91 QLIST_ENTRY(mon_fd_t) next;
100 uint8_t outbuf[1024];
104 BlockDriverCompletionFunc *password_completion_cb;
105 void *password_opaque;
106 QLIST_HEAD(,mon_fd_t) fds;
107 QLIST_ENTRY(Monitor) entry;
110 static QLIST_HEAD(mon_list, Monitor) mon_list;
112 static const mon_cmd_t mon_cmds[];
113 static const mon_cmd_t info_cmds[];
115 Monitor *cur_mon = NULL;
117 static void monitor_command_cb(Monitor *mon, const char *cmdline,
120 static void monitor_read_command(Monitor *mon, int show_prompt)
122 readline_start(mon->rs, "(qemu) ", 0, monitor_command_cb, NULL);
124 readline_show_prompt(mon->rs);
127 static int monitor_read_password(Monitor *mon, ReadLineFunc *readline_func,
131 readline_start(mon->rs, "Password: ", 1, readline_func, opaque);
132 /* prompt is printed on return from the command handler */
135 monitor_printf(mon, "terminal does not support password prompting\n");
140 void monitor_flush(Monitor *mon)
142 if (mon && mon->outbuf_index != 0 && !mon->mux_out) {
143 qemu_chr_write(mon->chr, mon->outbuf, mon->outbuf_index);
144 mon->outbuf_index = 0;
148 /* flush at every end of line or if the buffer is full */
149 static void monitor_puts(Monitor *mon, const char *str)
161 mon->outbuf[mon->outbuf_index++] = '\r';
162 mon->outbuf[mon->outbuf_index++] = c;
163 if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1)
169 void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap)
172 vsnprintf(buf, sizeof(buf), fmt, ap);
173 monitor_puts(mon, buf);
176 void monitor_printf(Monitor *mon, const char *fmt, ...)
180 monitor_vprintf(mon, fmt, ap);
184 void monitor_print_filename(Monitor *mon, const char *filename)
188 for (i = 0; filename[i]; i++) {
189 switch (filename[i]) {
193 monitor_printf(mon, "\\%c", filename[i]);
196 monitor_printf(mon, "\\t");
199 monitor_printf(mon, "\\r");
202 monitor_printf(mon, "\\n");
205 monitor_printf(mon, "%c", filename[i]);
211 static int monitor_fprintf(FILE *stream, const char *fmt, ...)
215 monitor_vprintf((Monitor *)stream, fmt, ap);
220 static void monitor_user_noop(Monitor *mon, const QObject *data) { }
222 static inline int monitor_handler_ported(const mon_cmd_t *cmd)
224 return cmd->user_print != NULL;
227 static void monitor_print_qobject(Monitor *mon, const QObject *data)
229 switch (qobject_type(data)) {
231 monitor_printf(mon, "%s",qstring_get_str(qobject_to_qstring(data)));
234 monitor_printf(mon, "%" PRId64,qint_get_int(qobject_to_qint(data)));
237 monitor_printf(mon, "ERROR: unsupported type: %d",
242 monitor_puts(mon, "\n");
245 static int compare_cmd(const char *name, const char *list)
247 const char *p, *pstart;
255 p = pstart + strlen(pstart);
256 if ((p - pstart) == len && !memcmp(pstart, name, len))
265 static void help_cmd_dump(Monitor *mon, const mon_cmd_t *cmds,
266 const char *prefix, const char *name)
268 const mon_cmd_t *cmd;
270 for(cmd = cmds; cmd->name != NULL; cmd++) {
271 if (!name || !strcmp(name, cmd->name))
272 monitor_printf(mon, "%s%s %s -- %s\n", prefix, cmd->name,
273 cmd->params, cmd->help);
277 static void help_cmd(Monitor *mon, const char *name)
279 if (name && !strcmp(name, "info")) {
280 help_cmd_dump(mon, info_cmds, "info ", NULL);
282 help_cmd_dump(mon, mon_cmds, "", name);
283 if (name && !strcmp(name, "log")) {
284 const CPULogItem *item;
285 monitor_printf(mon, "Log items (comma separated):\n");
286 monitor_printf(mon, "%-10s %s\n", "none", "remove all logs");
287 for(item = cpu_log_items; item->mask != 0; item++) {
288 monitor_printf(mon, "%-10s %s\n", item->name, item->help);
294 static void do_help_cmd(Monitor *mon, const QDict *qdict)
296 help_cmd(mon, qdict_get_try_str(qdict, "name"));
299 static void do_commit(Monitor *mon, const QDict *qdict)
303 const char *device = qdict_get_str(qdict, "device");
305 all_devices = !strcmp(device, "all");
306 QTAILQ_FOREACH(dinfo, &drives, next) {
308 if (strcmp(bdrv_get_device_name(dinfo->bdrv), device))
310 bdrv_commit(dinfo->bdrv);
314 static void do_info(Monitor *mon, const QDict *qdict, QObject **ret_data)
316 const mon_cmd_t *cmd;
317 const char *item = qdict_get_try_str(qdict, "item");
322 for (cmd = info_cmds; cmd->name != NULL; cmd++) {
323 if (compare_cmd(item, cmd->name))
327 if (cmd->name == NULL)
330 if (monitor_handler_ported(cmd)) {
331 cmd->mhandler.info_new(mon, ret_data);
333 cmd->user_print(mon, *ret_data);
335 cmd->mhandler.info(mon);
341 help_cmd(mon, "info");
345 * do_info_version(): Show QEMU version
347 static void do_info_version(Monitor *mon, QObject **ret_data)
349 *ret_data = QOBJECT(qstring_from_str(QEMU_VERSION QEMU_PKGVERSION));
352 static void do_info_name(Monitor *mon)
355 monitor_printf(mon, "%s\n", qemu_name);
358 #if defined(TARGET_I386)
359 static void do_info_hpet(Monitor *mon)
361 monitor_printf(mon, "HPET is %s by QEMU\n",
362 (no_hpet) ? "disabled" : "enabled");
366 static void do_info_uuid(Monitor *mon)
368 monitor_printf(mon, UUID_FMT "\n", qemu_uuid[0], qemu_uuid[1],
369 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
370 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
371 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
372 qemu_uuid[14], qemu_uuid[15]);
375 /* get the current CPU defined by the user */
376 static int mon_set_cpu(int cpu_index)
380 for(env = first_cpu; env != NULL; env = env->next_cpu) {
381 if (env->cpu_index == cpu_index) {
382 cur_mon->mon_cpu = env;
389 static CPUState *mon_get_cpu(void)
391 if (!cur_mon->mon_cpu) {
394 cpu_synchronize_state(cur_mon->mon_cpu);
395 return cur_mon->mon_cpu;
398 static void do_info_registers(Monitor *mon)
405 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
408 cpu_dump_state(env, (FILE *)mon, monitor_fprintf,
413 static void print_cpu_iter(QObject *obj, void *opaque)
417 Monitor *mon = opaque;
419 assert(qobject_type(obj) == QTYPE_QDICT);
420 cpu = qobject_to_qdict(obj);
422 if (strcmp(qdict_get_str(cpu, "current"), "yes") == 0)
425 monitor_printf(mon, "%c CPU #%d: ", active, (int)qdict_get_int(cpu, "CPU"));
427 #if defined(TARGET_I386)
428 monitor_printf(mon, "pc=0x" TARGET_FMT_lx,
429 (target_ulong) qdict_get_int(cpu, "pc"));
430 #elif defined(TARGET_PPC)
431 monitor_printf(mon, "nip=0x" TARGET_FMT_lx,
432 (target_long) qdict_get_int(cpu, "nip"));
433 #elif defined(TARGET_SPARC)
434 monitor_printf(mon, "pc=0x " TARGET_FMT_lx,
435 (target_long) qdict_get_int(cpu, "pc"));
436 monitor_printf(mon, "npc=0x" TARGET_FMT_lx,
437 (target_long) qdict_get_int(cpu, "npc"));
438 #elif defined(TARGET_MIPS)
439 monitor_printf(mon, "PC=0x" TARGET_FMT_lx,
440 (target_long) qdict_get_int(cpu, "PC"));
443 if (strcmp(qdict_get_str(cpu, "halted"), "yes") == 0)
444 monitor_printf(mon, " (halted)");
446 monitor_printf(mon, "\n");
449 static void monitor_print_cpus(Monitor *mon, const QObject *data)
453 assert(qobject_type(data) == QTYPE_QLIST);
454 cpu_list = qobject_to_qlist(data);
455 qlist_iter(cpu_list, print_cpu_iter, mon);
459 * do_info_cpus(): Show CPU information
461 * Return a QList with a QDict for each CPU.
465 * [ { "CPU": 0, "current": "yes", "pc": 0x..., "halted": "no" },
466 * { "CPU": 1, "current": "no", "pc": 0x..., "halted": "yes" } ]
468 static void do_info_cpus(Monitor *mon, QObject **ret_data)
473 cpu_list = qlist_new();
475 /* just to set the default cpu if not already done */
478 for(env = first_cpu; env != NULL; env = env->next_cpu) {
480 QDict *cpu = qdict_new();
482 cpu_synchronize_state(env);
484 qdict_put(cpu, "CPU", qint_from_int(env->cpu_index));
485 answer = (env == mon->mon_cpu) ? "yes" : "no";
486 qdict_put(cpu, "current", qstring_from_str(answer));
488 #if defined(TARGET_I386)
489 qdict_put(cpu, "pc", qint_from_int(env->eip + env->segs[R_CS].base));
490 #elif defined(TARGET_PPC)
491 qdict_put(cpu, "nip", qint_from_int(env->nip));
492 #elif defined(TARGET_SPARC)
493 qdict_put(cpu, "pc", qint_from_int(env->pc));
494 qdict_put(cpu, "npc", qint_from_int(env->npc));
495 #elif defined(TARGET_MIPS)
496 qdict_put(cpu, "PC", qint_from_int(env->active_tc.PC));
498 answer = env->halted ? "yes" : "no";
499 qdict_put(cpu, "halted", qstring_from_str(answer));
501 qlist_append(cpu_list, cpu);
504 *ret_data = QOBJECT(cpu_list);
507 static void do_cpu_set(Monitor *mon, const QDict *qdict)
509 int index = qdict_get_int(qdict, "index");
510 if (mon_set_cpu(index) < 0)
511 monitor_printf(mon, "Invalid CPU index\n");
514 static void do_info_jit(Monitor *mon)
516 dump_exec_info((FILE *)mon, monitor_fprintf);
519 static void do_info_history(Monitor *mon)
528 str = readline_get_history(mon->rs, i);
531 monitor_printf(mon, "%d: '%s'\n", i, str);
536 #if defined(TARGET_PPC)
537 /* XXX: not implemented in other targets */
538 static void do_info_cpu_stats(Monitor *mon)
543 cpu_dump_statistics(env, (FILE *)mon, &monitor_fprintf, 0);
548 * do_quit(): Quit QEMU execution
550 static void do_quit(Monitor *mon, const QDict *qdict, QObject **ret_data)
555 static int eject_device(Monitor *mon, BlockDriverState *bs, int force)
557 if (bdrv_is_inserted(bs)) {
559 if (!bdrv_is_removable(bs)) {
560 monitor_printf(mon, "device is not removable\n");
563 if (bdrv_is_locked(bs)) {
564 monitor_printf(mon, "device is locked\n");
573 static void do_eject(Monitor *mon, const QDict *qdict)
575 BlockDriverState *bs;
576 int force = qdict_get_int(qdict, "force");
577 const char *filename = qdict_get_str(qdict, "filename");
579 bs = bdrv_find(filename);
581 monitor_printf(mon, "device not found\n");
584 eject_device(mon, bs, force);
587 static void do_change_block(Monitor *mon, const char *device,
588 const char *filename, const char *fmt)
590 BlockDriverState *bs;
591 BlockDriver *drv = NULL;
593 bs = bdrv_find(device);
595 monitor_printf(mon, "device not found\n");
599 drv = bdrv_find_format(fmt);
601 monitor_printf(mon, "invalid format %s\n", fmt);
605 if (eject_device(mon, bs, 0) < 0)
607 bdrv_open2(bs, filename, 0, drv);
608 monitor_read_bdrv_key_start(mon, bs, NULL, NULL);
611 static void change_vnc_password_cb(Monitor *mon, const char *password,
614 if (vnc_display_password(NULL, password) < 0)
615 monitor_printf(mon, "could not set VNC server password\n");
617 monitor_read_command(mon, 1);
620 static void do_change_vnc(Monitor *mon, const char *target, const char *arg)
622 if (strcmp(target, "passwd") == 0 ||
623 strcmp(target, "password") == 0) {
626 strncpy(password, arg, sizeof(password));
627 password[sizeof(password) - 1] = '\0';
628 change_vnc_password_cb(mon, password, NULL);
630 monitor_read_password(mon, change_vnc_password_cb, NULL);
633 if (vnc_display_open(NULL, target) < 0)
634 monitor_printf(mon, "could not start VNC server on %s\n", target);
638 static void do_change(Monitor *mon, const QDict *qdict)
640 const char *device = qdict_get_str(qdict, "device");
641 const char *target = qdict_get_str(qdict, "target");
642 const char *arg = qdict_get_try_str(qdict, "arg");
643 if (strcmp(device, "vnc") == 0) {
644 do_change_vnc(mon, target, arg);
646 do_change_block(mon, device, target, arg);
650 static void do_screen_dump(Monitor *mon, const QDict *qdict)
652 vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
655 static void do_logfile(Monitor *mon, const QDict *qdict)
657 cpu_set_log_filename(qdict_get_str(qdict, "filename"));
660 static void do_log(Monitor *mon, const QDict *qdict)
663 const char *items = qdict_get_str(qdict, "items");
665 if (!strcmp(items, "none")) {
668 mask = cpu_str_to_log_mask(items);
670 help_cmd(mon, "log");
677 static void do_singlestep(Monitor *mon, const QDict *qdict)
679 const char *option = qdict_get_try_str(qdict, "option");
680 if (!option || !strcmp(option, "on")) {
682 } else if (!strcmp(option, "off")) {
685 monitor_printf(mon, "unexpected option %s\n", option);
690 * do_stop(): Stop VM execution
692 static void do_stop(Monitor *mon, const QDict *qdict, QObject **ret_data)
694 vm_stop(EXCP_INTERRUPT);
697 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs);
699 struct bdrv_iterate_context {
705 * do_cont(): Resume emulation.
707 static void do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
709 struct bdrv_iterate_context context = { mon, 0 };
711 bdrv_iterate(encrypted_bdrv_it, &context);
712 /* only resume the vm if all keys are set and valid */
717 static void bdrv_key_cb(void *opaque, int err)
719 Monitor *mon = opaque;
721 /* another key was set successfully, retry to continue */
723 do_cont(mon, NULL, NULL);
726 static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
728 struct bdrv_iterate_context *context = opaque;
730 if (!context->err && bdrv_key_required(bs)) {
731 context->err = -EBUSY;
732 monitor_read_bdrv_key_start(context->mon, bs, bdrv_key_cb,
737 static void do_gdbserver(Monitor *mon, const QDict *qdict)
739 const char *device = qdict_get_try_str(qdict, "device");
741 device = "tcp::" DEFAULT_GDBSTUB_PORT;
742 if (gdbserver_start(device) < 0) {
743 monitor_printf(mon, "Could not open gdbserver on device '%s'\n",
745 } else if (strcmp(device, "none") == 0) {
746 monitor_printf(mon, "Disabled gdbserver\n");
748 monitor_printf(mon, "Waiting for gdb connection on device '%s'\n",
753 static void do_watchdog_action(Monitor *mon, const QDict *qdict)
755 const char *action = qdict_get_str(qdict, "action");
756 if (select_watchdog_action(action) == -1) {
757 monitor_printf(mon, "Unknown watchdog action '%s'\n", action);
761 static void monitor_printc(Monitor *mon, int c)
763 monitor_printf(mon, "'");
766 monitor_printf(mon, "\\'");
769 monitor_printf(mon, "\\\\");
772 monitor_printf(mon, "\\n");
775 monitor_printf(mon, "\\r");
778 if (c >= 32 && c <= 126) {
779 monitor_printf(mon, "%c", c);
781 monitor_printf(mon, "\\x%02x", c);
785 monitor_printf(mon, "'");
788 static void memory_dump(Monitor *mon, int count, int format, int wsize,
789 target_phys_addr_t addr, int is_physical)
792 int nb_per_line, l, line_size, i, max_digits, len;
800 if (!env && !is_physical)
805 } else if (wsize == 4) {
808 /* as default we use the current CS size */
812 if ((env->efer & MSR_EFER_LMA) &&
813 (env->segs[R_CS].flags & DESC_L_MASK))
817 if (!(env->segs[R_CS].flags & DESC_B_MASK))
822 monitor_disas(mon, env, addr, count, is_physical, flags);
831 nb_per_line = line_size / wsize;
836 max_digits = (wsize * 8 + 2) / 3;
840 max_digits = (wsize * 8) / 4;
844 max_digits = (wsize * 8 * 10 + 32) / 33;
853 monitor_printf(mon, TARGET_FMT_plx ":", addr);
855 monitor_printf(mon, TARGET_FMT_lx ":", (target_ulong)addr);
860 cpu_physical_memory_rw(addr, buf, l, 0);
865 if (cpu_memory_rw_debug(env, addr, buf, l, 0) < 0) {
866 monitor_printf(mon, " Cannot access memory\n");
875 v = ldub_raw(buf + i);
878 v = lduw_raw(buf + i);
881 v = (uint32_t)ldl_raw(buf + i);
884 v = ldq_raw(buf + i);
887 monitor_printf(mon, " ");
890 monitor_printf(mon, "%#*" PRIo64, max_digits, v);
893 monitor_printf(mon, "0x%0*" PRIx64, max_digits, v);
896 monitor_printf(mon, "%*" PRIu64, max_digits, v);
899 monitor_printf(mon, "%*" PRId64, max_digits, v);
902 monitor_printc(mon, v);
907 monitor_printf(mon, "\n");
913 static void do_memory_dump(Monitor *mon, const QDict *qdict)
915 int count = qdict_get_int(qdict, "count");
916 int format = qdict_get_int(qdict, "format");
917 int size = qdict_get_int(qdict, "size");
918 target_long addr = qdict_get_int(qdict, "addr");
920 memory_dump(mon, count, format, size, addr, 0);
923 static void do_physical_memory_dump(Monitor *mon, const QDict *qdict)
925 int count = qdict_get_int(qdict, "count");
926 int format = qdict_get_int(qdict, "format");
927 int size = qdict_get_int(qdict, "size");
928 target_phys_addr_t addr = qdict_get_int(qdict, "addr");
930 memory_dump(mon, count, format, size, addr, 1);
933 static void do_print(Monitor *mon, const QDict *qdict)
935 int format = qdict_get_int(qdict, "format");
936 target_phys_addr_t val = qdict_get_int(qdict, "val");
938 #if TARGET_PHYS_ADDR_BITS == 32
941 monitor_printf(mon, "%#o", val);
944 monitor_printf(mon, "%#x", val);
947 monitor_printf(mon, "%u", val);
951 monitor_printf(mon, "%d", val);
954 monitor_printc(mon, val);
960 monitor_printf(mon, "%#" PRIo64, val);
963 monitor_printf(mon, "%#" PRIx64, val);
966 monitor_printf(mon, "%" PRIu64, val);
970 monitor_printf(mon, "%" PRId64, val);
973 monitor_printc(mon, val);
977 monitor_printf(mon, "\n");
980 static void do_memory_save(Monitor *mon, const QDict *qdict)
983 uint32_t size = qdict_get_int(qdict, "size");
984 const char *filename = qdict_get_str(qdict, "filename");
985 target_long addr = qdict_get_int(qdict, "val");
994 f = fopen(filename, "wb");
996 monitor_printf(mon, "could not open '%s'\n", filename);
1003 cpu_memory_rw_debug(env, addr, buf, l, 0);
1004 fwrite(buf, 1, l, f);
1011 static void do_physical_memory_save(Monitor *mon, const QDict *qdict)
1016 uint32_t size = qdict_get_int(qdict, "size");
1017 const char *filename = qdict_get_str(qdict, "filename");
1018 target_phys_addr_t addr = qdict_get_int(qdict, "val");
1020 f = fopen(filename, "wb");
1022 monitor_printf(mon, "could not open '%s'\n", filename);
1029 cpu_physical_memory_rw(addr, buf, l, 0);
1030 fwrite(buf, 1, l, f);
1038 static void do_sum(Monitor *mon, const QDict *qdict)
1043 uint32_t start = qdict_get_int(qdict, "start");
1044 uint32_t size = qdict_get_int(qdict, "size");
1047 for(addr = start; addr < (start + size); addr++) {
1048 cpu_physical_memory_rw(addr, buf, 1, 0);
1049 /* BSD sum algorithm ('sum' Unix command) */
1050 sum = (sum >> 1) | (sum << 15);
1053 monitor_printf(mon, "%05d\n", sum);
1061 static const KeyDef key_defs[] = {
1063 { 0x36, "shift_r" },
1068 { 0xe4, "altgr_r" },
1088 { 0x0e, "backspace" },
1125 { 0x37, "asterisk" },
1128 { 0x3a, "caps_lock" },
1139 { 0x45, "num_lock" },
1140 { 0x46, "scroll_lock" },
1142 { 0xb5, "kp_divide" },
1143 { 0x37, "kp_multiply" },
1144 { 0x4a, "kp_subtract" },
1146 { 0x9c, "kp_enter" },
1147 { 0x53, "kp_decimal" },
1180 #if defined(TARGET_SPARC) && !defined(TARGET_SPARC64)
1195 { 0xfe, "compose" },
1200 static int get_keycode(const char *key)
1206 for(p = key_defs; p->name != NULL; p++) {
1207 if (!strcmp(key, p->name))
1210 if (strstart(key, "0x", NULL)) {
1211 ret = strtoul(key, &endp, 0);
1212 if (*endp == '\0' && ret >= 0x01 && ret <= 0xff)
1218 #define MAX_KEYCODES 16
1219 static uint8_t keycodes[MAX_KEYCODES];
1220 static int nb_pending_keycodes;
1221 static QEMUTimer *key_timer;
1223 static void release_keys(void *opaque)
1227 while (nb_pending_keycodes > 0) {
1228 nb_pending_keycodes--;
1229 keycode = keycodes[nb_pending_keycodes];
1231 kbd_put_keycode(0xe0);
1232 kbd_put_keycode(keycode | 0x80);
1236 static void do_sendkey(Monitor *mon, const QDict *qdict)
1238 char keyname_buf[16];
1240 int keyname_len, keycode, i;
1241 const char *string = qdict_get_str(qdict, "string");
1242 int has_hold_time = qdict_haskey(qdict, "hold_time");
1243 int hold_time = qdict_get_try_int(qdict, "hold_time", -1);
1245 if (nb_pending_keycodes > 0) {
1246 qemu_del_timer(key_timer);
1253 separator = strchr(string, '-');
1254 keyname_len = separator ? separator - string : strlen(string);
1255 if (keyname_len > 0) {
1256 pstrcpy(keyname_buf, sizeof(keyname_buf), string);
1257 if (keyname_len > sizeof(keyname_buf) - 1) {
1258 monitor_printf(mon, "invalid key: '%s...'\n", keyname_buf);
1261 if (i == MAX_KEYCODES) {
1262 monitor_printf(mon, "too many keys\n");
1265 keyname_buf[keyname_len] = 0;
1266 keycode = get_keycode(keyname_buf);
1268 monitor_printf(mon, "unknown key: '%s'\n", keyname_buf);
1271 keycodes[i++] = keycode;
1275 string = separator + 1;
1277 nb_pending_keycodes = i;
1278 /* key down events */
1279 for (i = 0; i < nb_pending_keycodes; i++) {
1280 keycode = keycodes[i];
1282 kbd_put_keycode(0xe0);
1283 kbd_put_keycode(keycode & 0x7f);
1285 /* delayed key up events */
1286 qemu_mod_timer(key_timer, qemu_get_clock(vm_clock) +
1287 muldiv64(get_ticks_per_sec(), hold_time, 1000));
1290 static int mouse_button_state;
1292 static void do_mouse_move(Monitor *mon, const QDict *qdict)
1295 const char *dx_str = qdict_get_str(qdict, "dx_str");
1296 const char *dy_str = qdict_get_str(qdict, "dy_str");
1297 const char *dz_str = qdict_get_try_str(qdict, "dz_str");
1298 dx = strtol(dx_str, NULL, 0);
1299 dy = strtol(dy_str, NULL, 0);
1302 dz = strtol(dz_str, NULL, 0);
1303 kbd_mouse_event(dx, dy, dz, mouse_button_state);
1306 static void do_mouse_button(Monitor *mon, const QDict *qdict)
1308 int button_state = qdict_get_int(qdict, "button_state");
1309 mouse_button_state = button_state;
1310 kbd_mouse_event(0, 0, 0, mouse_button_state);
1313 static void do_ioport_read(Monitor *mon, const QDict *qdict)
1315 int size = qdict_get_int(qdict, "size");
1316 int addr = qdict_get_int(qdict, "addr");
1317 int has_index = qdict_haskey(qdict, "index");
1322 int index = qdict_get_int(qdict, "index");
1323 cpu_outb(addr & IOPORTS_MASK, index & 0xff);
1331 val = cpu_inb(addr);
1335 val = cpu_inw(addr);
1339 val = cpu_inl(addr);
1343 monitor_printf(mon, "port%c[0x%04x] = %#0*x\n",
1344 suffix, addr, size * 2, val);
1347 static void do_ioport_write(Monitor *mon, const QDict *qdict)
1349 int size = qdict_get_int(qdict, "size");
1350 int addr = qdict_get_int(qdict, "addr");
1351 int val = qdict_get_int(qdict, "val");
1353 addr &= IOPORTS_MASK;
1358 cpu_outb(addr, val);
1361 cpu_outw(addr, val);
1364 cpu_outl(addr, val);
1369 static void do_boot_set(Monitor *mon, const QDict *qdict)
1372 const char *bootdevice = qdict_get_str(qdict, "bootdevice");
1374 res = qemu_boot_set(bootdevice);
1376 monitor_printf(mon, "boot device list now set to %s\n", bootdevice);
1377 } else if (res > 0) {
1378 monitor_printf(mon, "setting boot device list failed\n");
1380 monitor_printf(mon, "no function defined to set boot device list for "
1381 "this architecture\n");
1386 * do_system_reset(): Issue a machine reset
1388 static void do_system_reset(Monitor *mon, const QDict *qdict,
1391 qemu_system_reset_request();
1395 * do_system_powerdown(): Issue a machine powerdown
1397 static void do_system_powerdown(Monitor *mon, const QDict *qdict,
1400 qemu_system_powerdown_request();
1403 #if defined(TARGET_I386)
1404 static void print_pte(Monitor *mon, uint32_t addr, uint32_t pte, uint32_t mask)
1406 monitor_printf(mon, "%08x: %08x %c%c%c%c%c%c%c%c\n",
1409 pte & PG_GLOBAL_MASK ? 'G' : '-',
1410 pte & PG_PSE_MASK ? 'P' : '-',
1411 pte & PG_DIRTY_MASK ? 'D' : '-',
1412 pte & PG_ACCESSED_MASK ? 'A' : '-',
1413 pte & PG_PCD_MASK ? 'C' : '-',
1414 pte & PG_PWT_MASK ? 'T' : '-',
1415 pte & PG_USER_MASK ? 'U' : '-',
1416 pte & PG_RW_MASK ? 'W' : '-');
1419 static void tlb_info(Monitor *mon)
1423 uint32_t pgd, pde, pte;
1425 env = mon_get_cpu();
1429 if (!(env->cr[0] & CR0_PG_MASK)) {
1430 monitor_printf(mon, "PG disabled\n");
1433 pgd = env->cr[3] & ~0xfff;
1434 for(l1 = 0; l1 < 1024; l1++) {
1435 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1436 pde = le32_to_cpu(pde);
1437 if (pde & PG_PRESENT_MASK) {
1438 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1439 print_pte(mon, (l1 << 22), pde, ~((1 << 20) - 1));
1441 for(l2 = 0; l2 < 1024; l2++) {
1442 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1443 (uint8_t *)&pte, 4);
1444 pte = le32_to_cpu(pte);
1445 if (pte & PG_PRESENT_MASK) {
1446 print_pte(mon, (l1 << 22) + (l2 << 12),
1456 static void mem_print(Monitor *mon, uint32_t *pstart, int *plast_prot,
1457 uint32_t end, int prot)
1460 prot1 = *plast_prot;
1461 if (prot != prot1) {
1462 if (*pstart != -1) {
1463 monitor_printf(mon, "%08x-%08x %08x %c%c%c\n",
1464 *pstart, end, end - *pstart,
1465 prot1 & PG_USER_MASK ? 'u' : '-',
1467 prot1 & PG_RW_MASK ? 'w' : '-');
1477 static void mem_info(Monitor *mon)
1480 int l1, l2, prot, last_prot;
1481 uint32_t pgd, pde, pte, start, end;
1483 env = mon_get_cpu();
1487 if (!(env->cr[0] & CR0_PG_MASK)) {
1488 monitor_printf(mon, "PG disabled\n");
1491 pgd = env->cr[3] & ~0xfff;
1494 for(l1 = 0; l1 < 1024; l1++) {
1495 cpu_physical_memory_read(pgd + l1 * 4, (uint8_t *)&pde, 4);
1496 pde = le32_to_cpu(pde);
1498 if (pde & PG_PRESENT_MASK) {
1499 if ((pde & PG_PSE_MASK) && (env->cr[4] & CR4_PSE_MASK)) {
1500 prot = pde & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1501 mem_print(mon, &start, &last_prot, end, prot);
1503 for(l2 = 0; l2 < 1024; l2++) {
1504 cpu_physical_memory_read((pde & ~0xfff) + l2 * 4,
1505 (uint8_t *)&pte, 4);
1506 pte = le32_to_cpu(pte);
1507 end = (l1 << 22) + (l2 << 12);
1508 if (pte & PG_PRESENT_MASK) {
1509 prot = pte & (PG_USER_MASK | PG_RW_MASK | PG_PRESENT_MASK);
1513 mem_print(mon, &start, &last_prot, end, prot);
1518 mem_print(mon, &start, &last_prot, end, prot);
1524 #if defined(TARGET_SH4)
1526 static void print_tlb(Monitor *mon, int idx, tlb_t *tlb)
1528 monitor_printf(mon, " tlb%i:\t"
1529 "asid=%hhu vpn=%x\tppn=%x\tsz=%hhu size=%u\t"
1530 "v=%hhu shared=%hhu cached=%hhu prot=%hhu "
1531 "dirty=%hhu writethrough=%hhu\n",
1533 tlb->asid, tlb->vpn, tlb->ppn, tlb->sz, tlb->size,
1534 tlb->v, tlb->sh, tlb->c, tlb->pr,
1538 static void tlb_info(Monitor *mon)
1540 CPUState *env = mon_get_cpu();
1543 monitor_printf (mon, "ITLB:\n");
1544 for (i = 0 ; i < ITLB_SIZE ; i++)
1545 print_tlb (mon, i, &env->itlb[i]);
1546 monitor_printf (mon, "UTLB:\n");
1547 for (i = 0 ; i < UTLB_SIZE ; i++)
1548 print_tlb (mon, i, &env->utlb[i]);
1553 static void do_info_kvm(Monitor *mon)
1556 monitor_printf(mon, "kvm support: ");
1558 monitor_printf(mon, "enabled\n");
1560 monitor_printf(mon, "disabled\n");
1562 monitor_printf(mon, "kvm support: not compiled\n");
1566 static void do_info_numa(Monitor *mon)
1571 monitor_printf(mon, "%d nodes\n", nb_numa_nodes);
1572 for (i = 0; i < nb_numa_nodes; i++) {
1573 monitor_printf(mon, "node %d cpus:", i);
1574 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1575 if (env->numa_node == i) {
1576 monitor_printf(mon, " %d", env->cpu_index);
1579 monitor_printf(mon, "\n");
1580 monitor_printf(mon, "node %d size: %" PRId64 " MB\n", i,
1585 #ifdef CONFIG_PROFILER
1590 static void do_info_profile(Monitor *mon)
1596 monitor_printf(mon, "async time %" PRId64 " (%0.3f)\n",
1597 dev_time, dev_time / (double)get_ticks_per_sec());
1598 monitor_printf(mon, "qemu time %" PRId64 " (%0.3f)\n",
1599 qemu_time, qemu_time / (double)get_ticks_per_sec());
1604 static void do_info_profile(Monitor *mon)
1606 monitor_printf(mon, "Internal profiler not compiled\n");
1610 /* Capture support */
1611 static QLIST_HEAD (capture_list_head, CaptureState) capture_head;
1613 static void do_info_capture(Monitor *mon)
1618 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1619 monitor_printf(mon, "[%d]: ", i);
1620 s->ops.info (s->opaque);
1625 static void do_stop_capture(Monitor *mon, const QDict *qdict)
1628 int n = qdict_get_int(qdict, "n");
1631 for (s = capture_head.lh_first, i = 0; s; s = s->entries.le_next, ++i) {
1633 s->ops.destroy (s->opaque);
1634 QLIST_REMOVE (s, entries);
1641 static void do_wav_capture(Monitor *mon, const QDict *qdict)
1643 const char *path = qdict_get_str(qdict, "path");
1644 int has_freq = qdict_haskey(qdict, "freq");
1645 int freq = qdict_get_try_int(qdict, "freq", -1);
1646 int has_bits = qdict_haskey(qdict, "bits");
1647 int bits = qdict_get_try_int(qdict, "bits", -1);
1648 int has_channels = qdict_haskey(qdict, "nchannels");
1649 int nchannels = qdict_get_try_int(qdict, "nchannels", -1);
1652 s = qemu_mallocz (sizeof (*s));
1654 freq = has_freq ? freq : 44100;
1655 bits = has_bits ? bits : 16;
1656 nchannels = has_channels ? nchannels : 2;
1658 if (wav_start_capture (s, path, freq, bits, nchannels)) {
1659 monitor_printf(mon, "Faied to add wave capture\n");
1662 QLIST_INSERT_HEAD (&capture_head, s, entries);
1666 #if defined(TARGET_I386)
1667 static void do_inject_nmi(Monitor *mon, const QDict *qdict)
1670 int cpu_index = qdict_get_int(qdict, "cpu_index");
1672 for (env = first_cpu; env != NULL; env = env->next_cpu)
1673 if (env->cpu_index == cpu_index) {
1674 cpu_interrupt(env, CPU_INTERRUPT_NMI);
1680 static void do_info_status(Monitor *mon)
1684 monitor_printf(mon, "VM status: running (single step mode)\n");
1686 monitor_printf(mon, "VM status: running\n");
1689 monitor_printf(mon, "VM status: paused\n");
1693 * do_balloon(): Request VM to change its memory allocation
1695 static void do_balloon(Monitor *mon, const QDict *qdict, QObject **ret_data)
1697 int value = qdict_get_int(qdict, "value");
1698 ram_addr_t target = value;
1699 qemu_balloon(target << 20);
1702 static void monitor_print_balloon(Monitor *mon, const QObject *data)
1704 monitor_printf(mon, "balloon: actual=%d\n",
1705 (int)qint_get_int(qobject_to_qint(data)));
1709 * do_info_balloon(): Balloon information
1711 static void do_info_balloon(Monitor *mon, QObject **ret_data)
1715 actual = qemu_balloon_status();
1716 if (kvm_enabled() && !kvm_has_sync_mmu())
1717 monitor_printf(mon, "Using KVM without synchronous MMU, "
1718 "ballooning disabled\n");
1719 else if (actual == 0)
1720 monitor_printf(mon, "Ballooning not activated in VM\n");
1722 *ret_data = QOBJECT(qint_from_int((int)(actual >> 20)));
1725 static qemu_acl *find_acl(Monitor *mon, const char *name)
1727 qemu_acl *acl = qemu_acl_find(name);
1730 monitor_printf(mon, "acl: unknown list '%s'\n", name);
1735 static void do_acl_show(Monitor *mon, const QDict *qdict)
1737 const char *aclname = qdict_get_str(qdict, "aclname");
1738 qemu_acl *acl = find_acl(mon, aclname);
1739 qemu_acl_entry *entry;
1743 monitor_printf(mon, "policy: %s\n",
1744 acl->defaultDeny ? "deny" : "allow");
1745 QTAILQ_FOREACH(entry, &acl->entries, next) {
1747 monitor_printf(mon, "%d: %s %s\n", i,
1748 entry->deny ? "deny" : "allow", entry->match);
1753 static void do_acl_reset(Monitor *mon, const QDict *qdict)
1755 const char *aclname = qdict_get_str(qdict, "aclname");
1756 qemu_acl *acl = find_acl(mon, aclname);
1759 qemu_acl_reset(acl);
1760 monitor_printf(mon, "acl: removed all rules\n");
1764 static void do_acl_policy(Monitor *mon, const QDict *qdict)
1766 const char *aclname = qdict_get_str(qdict, "aclname");
1767 const char *policy = qdict_get_str(qdict, "policy");
1768 qemu_acl *acl = find_acl(mon, aclname);
1771 if (strcmp(policy, "allow") == 0) {
1772 acl->defaultDeny = 0;
1773 monitor_printf(mon, "acl: policy set to 'allow'\n");
1774 } else if (strcmp(policy, "deny") == 0) {
1775 acl->defaultDeny = 1;
1776 monitor_printf(mon, "acl: policy set to 'deny'\n");
1778 monitor_printf(mon, "acl: unknown policy '%s', "
1779 "expected 'deny' or 'allow'\n", policy);
1784 static void do_acl_add(Monitor *mon, const QDict *qdict)
1786 const char *aclname = qdict_get_str(qdict, "aclname");
1787 const char *match = qdict_get_str(qdict, "match");
1788 const char *policy = qdict_get_str(qdict, "policy");
1789 int has_index = qdict_haskey(qdict, "index");
1790 int index = qdict_get_try_int(qdict, "index", -1);
1791 qemu_acl *acl = find_acl(mon, aclname);
1795 if (strcmp(policy, "allow") == 0) {
1797 } else if (strcmp(policy, "deny") == 0) {
1800 monitor_printf(mon, "acl: unknown policy '%s', "
1801 "expected 'deny' or 'allow'\n", policy);
1805 ret = qemu_acl_insert(acl, deny, match, index);
1807 ret = qemu_acl_append(acl, deny, match);
1809 monitor_printf(mon, "acl: unable to add acl entry\n");
1811 monitor_printf(mon, "acl: added rule at position %d\n", ret);
1815 static void do_acl_remove(Monitor *mon, const QDict *qdict)
1817 const char *aclname = qdict_get_str(qdict, "aclname");
1818 const char *match = qdict_get_str(qdict, "match");
1819 qemu_acl *acl = find_acl(mon, aclname);
1823 ret = qemu_acl_remove(acl, match);
1825 monitor_printf(mon, "acl: no matching acl entry\n");
1827 monitor_printf(mon, "acl: removed rule at position %d\n", ret);
1831 #if defined(TARGET_I386)
1832 static void do_inject_mce(Monitor *mon, const QDict *qdict)
1835 int cpu_index = qdict_get_int(qdict, "cpu_index");
1836 int bank = qdict_get_int(qdict, "bank");
1837 uint64_t status = qdict_get_int(qdict, "status");
1838 uint64_t mcg_status = qdict_get_int(qdict, "mcg_status");
1839 uint64_t addr = qdict_get_int(qdict, "addr");
1840 uint64_t misc = qdict_get_int(qdict, "misc");
1842 for (cenv = first_cpu; cenv != NULL; cenv = cenv->next_cpu)
1843 if (cenv->cpu_index == cpu_index && cenv->mcg_cap) {
1844 cpu_inject_x86_mce(cenv, bank, status, mcg_status, addr, misc);
1850 static void do_getfd(Monitor *mon, const QDict *qdict)
1852 const char *fdname = qdict_get_str(qdict, "fdname");
1856 fd = qemu_chr_get_msgfd(mon->chr);
1858 monitor_printf(mon, "getfd: no file descriptor supplied via SCM_RIGHTS\n");
1862 if (qemu_isdigit(fdname[0])) {
1863 monitor_printf(mon, "getfd: monitor names may not begin with a number\n");
1869 monitor_printf(mon, "Failed to dup() file descriptor: %s\n",
1874 QLIST_FOREACH(monfd, &mon->fds, next) {
1875 if (strcmp(monfd->name, fdname) != 0) {
1884 monfd = qemu_mallocz(sizeof(mon_fd_t));
1885 monfd->name = qemu_strdup(fdname);
1888 QLIST_INSERT_HEAD(&mon->fds, monfd, next);
1891 static void do_closefd(Monitor *mon, const QDict *qdict)
1893 const char *fdname = qdict_get_str(qdict, "fdname");
1896 QLIST_FOREACH(monfd, &mon->fds, next) {
1897 if (strcmp(monfd->name, fdname) != 0) {
1901 QLIST_REMOVE(monfd, next);
1903 qemu_free(monfd->name);
1908 monitor_printf(mon, "Failed to find file descriptor named %s\n",
1912 static void do_loadvm(Monitor *mon, const QDict *qdict)
1914 int saved_vm_running = vm_running;
1915 const char *name = qdict_get_str(qdict, "name");
1919 if (load_vmstate(mon, name) >= 0 && saved_vm_running)
1923 int monitor_get_fd(Monitor *mon, const char *fdname)
1927 QLIST_FOREACH(monfd, &mon->fds, next) {
1930 if (strcmp(monfd->name, fdname) != 0) {
1936 /* caller takes ownership of fd */
1937 QLIST_REMOVE(monfd, next);
1938 qemu_free(monfd->name);
1947 static const mon_cmd_t mon_cmds[] = {
1948 #include "qemu-monitor.h"
1952 /* Please update qemu-monitor.hx when adding or changing commands */
1953 static const mon_cmd_t info_cmds[] = {
1958 .help = "show the version of QEMU",
1959 .user_print = monitor_print_qobject,
1960 .mhandler.info_new = do_info_version,
1966 .help = "show the network state",
1967 .mhandler.info = do_info_network,
1973 .help = "show the character devices",
1974 .mhandler.info = qemu_chr_info,
1980 .help = "show the block devices",
1981 .mhandler.info = bdrv_info,
1984 .name = "blockstats",
1987 .help = "show block device statistics",
1988 .mhandler.info = bdrv_info_stats,
1991 .name = "registers",
1994 .help = "show the cpu registers",
1995 .mhandler.info = do_info_registers,
2001 .help = "show infos for each CPU",
2002 .user_print = monitor_print_cpus,
2003 .mhandler.info_new = do_info_cpus,
2009 .help = "show the command line history",
2010 .mhandler.info = do_info_history,
2016 .help = "show the interrupts statistics (if available)",
2017 .mhandler.info = irq_info,
2023 .help = "show i8259 (PIC) state",
2024 .mhandler.info = pic_info,
2030 .help = "show PCI info",
2031 .mhandler.info = pci_info,
2033 #if defined(TARGET_I386) || defined(TARGET_SH4)
2038 .help = "show virtual to physical memory mappings",
2039 .mhandler.info = tlb_info,
2042 #if defined(TARGET_I386)
2047 .help = "show the active virtual memory mappings",
2048 .mhandler.info = mem_info,
2054 .help = "show state of HPET",
2055 .mhandler.info = do_info_hpet,
2062 .help = "show dynamic compiler info",
2063 .mhandler.info = do_info_jit,
2069 .help = "show KVM information",
2070 .mhandler.info = do_info_kvm,
2076 .help = "show NUMA information",
2077 .mhandler.info = do_info_numa,
2083 .help = "show guest USB devices",
2084 .mhandler.info = usb_info,
2090 .help = "show host USB devices",
2091 .mhandler.info = usb_host_info,
2097 .help = "show profiling information",
2098 .mhandler.info = do_info_profile,
2104 .help = "show capture information",
2105 .mhandler.info = do_info_capture,
2108 .name = "snapshots",
2111 .help = "show the currently saved VM snapshots",
2112 .mhandler.info = do_info_snapshots,
2118 .help = "show the current VM status (running|paused)",
2119 .mhandler.info = do_info_status,
2125 .help = "show guest PCMCIA status",
2126 .mhandler.info = pcmcia_info,
2132 .help = "show which guest mouse is receiving events",
2133 .mhandler.info = do_info_mice,
2139 .help = "show the vnc server status",
2140 .mhandler.info = do_info_vnc,
2146 .help = "show the current VM name",
2147 .mhandler.info = do_info_name,
2153 .help = "show the current VM UUID",
2154 .mhandler.info = do_info_uuid,
2156 #if defined(TARGET_PPC)
2161 .help = "show CPU statistics",
2162 .mhandler.info = do_info_cpu_stats,
2165 #if defined(CONFIG_SLIRP)
2170 .help = "show user network stack connection states",
2171 .mhandler.info = do_info_usernet,
2178 .help = "show migration status",
2179 .mhandler.info = do_info_migrate,
2185 .help = "show balloon information",
2186 .user_print = monitor_print_balloon,
2187 .mhandler.info_new = do_info_balloon,
2193 .help = "show device tree",
2194 .mhandler.info = do_info_qtree,
2200 .help = "show qdev device model list",
2201 .mhandler.info = do_info_qdm,
2207 .help = "show roms",
2208 .mhandler.info = do_info_roms,
2215 /*******************************************************************/
2217 static const char *pch;
2218 static jmp_buf expr_env;
2223 typedef struct MonitorDef {
2226 target_long (*get_value)(const struct MonitorDef *md, int val);
2230 #if defined(TARGET_I386)
2231 static target_long monitor_get_pc (const struct MonitorDef *md, int val)
2233 CPUState *env = mon_get_cpu();
2236 return env->eip + env->segs[R_CS].base;
2240 #if defined(TARGET_PPC)
2241 static target_long monitor_get_ccr (const struct MonitorDef *md, int val)
2243 CPUState *env = mon_get_cpu();
2251 for (i = 0; i < 8; i++)
2252 u |= env->crf[i] << (32 - (4 * i));
2257 static target_long monitor_get_msr (const struct MonitorDef *md, int val)
2259 CPUState *env = mon_get_cpu();
2265 static target_long monitor_get_xer (const struct MonitorDef *md, int val)
2267 CPUState *env = mon_get_cpu();
2273 static target_long monitor_get_decr (const struct MonitorDef *md, int val)
2275 CPUState *env = mon_get_cpu();
2278 return cpu_ppc_load_decr(env);
2281 static target_long monitor_get_tbu (const struct MonitorDef *md, int val)
2283 CPUState *env = mon_get_cpu();
2286 return cpu_ppc_load_tbu(env);
2289 static target_long monitor_get_tbl (const struct MonitorDef *md, int val)
2291 CPUState *env = mon_get_cpu();
2294 return cpu_ppc_load_tbl(env);
2298 #if defined(TARGET_SPARC)
2299 #ifndef TARGET_SPARC64
2300 static target_long monitor_get_psr (const struct MonitorDef *md, int val)
2302 CPUState *env = mon_get_cpu();
2305 return GET_PSR(env);
2309 static target_long monitor_get_reg(const struct MonitorDef *md, int val)
2311 CPUState *env = mon_get_cpu();
2314 return env->regwptr[val];
2318 static const MonitorDef monitor_defs[] = {
2321 #define SEG(name, seg) \
2322 { name, offsetof(CPUState, segs[seg].selector), NULL, MD_I32 },\
2323 { name ".base", offsetof(CPUState, segs[seg].base) },\
2324 { name ".limit", offsetof(CPUState, segs[seg].limit), NULL, MD_I32 },
2326 { "eax", offsetof(CPUState, regs[0]) },
2327 { "ecx", offsetof(CPUState, regs[1]) },
2328 { "edx", offsetof(CPUState, regs[2]) },
2329 { "ebx", offsetof(CPUState, regs[3]) },
2330 { "esp|sp", offsetof(CPUState, regs[4]) },
2331 { "ebp|fp", offsetof(CPUState, regs[5]) },
2332 { "esi", offsetof(CPUState, regs[6]) },
2333 { "edi", offsetof(CPUState, regs[7]) },
2334 #ifdef TARGET_X86_64
2335 { "r8", offsetof(CPUState, regs[8]) },
2336 { "r9", offsetof(CPUState, regs[9]) },
2337 { "r10", offsetof(CPUState, regs[10]) },
2338 { "r11", offsetof(CPUState, regs[11]) },
2339 { "r12", offsetof(CPUState, regs[12]) },
2340 { "r13", offsetof(CPUState, regs[13]) },
2341 { "r14", offsetof(CPUState, regs[14]) },
2342 { "r15", offsetof(CPUState, regs[15]) },
2344 { "eflags", offsetof(CPUState, eflags) },
2345 { "eip", offsetof(CPUState, eip) },
2352 { "pc", 0, monitor_get_pc, },
2353 #elif defined(TARGET_PPC)
2354 /* General purpose registers */
2355 { "r0", offsetof(CPUState, gpr[0]) },
2356 { "r1", offsetof(CPUState, gpr[1]) },
2357 { "r2", offsetof(CPUState, gpr[2]) },
2358 { "r3", offsetof(CPUState, gpr[3]) },
2359 { "r4", offsetof(CPUState, gpr[4]) },
2360 { "r5", offsetof(CPUState, gpr[5]) },
2361 { "r6", offsetof(CPUState, gpr[6]) },
2362 { "r7", offsetof(CPUState, gpr[7]) },
2363 { "r8", offsetof(CPUState, gpr[8]) },
2364 { "r9", offsetof(CPUState, gpr[9]) },
2365 { "r10", offsetof(CPUState, gpr[10]) },
2366 { "r11", offsetof(CPUState, gpr[11]) },
2367 { "r12", offsetof(CPUState, gpr[12]) },
2368 { "r13", offsetof(CPUState, gpr[13]) },
2369 { "r14", offsetof(CPUState, gpr[14]) },
2370 { "r15", offsetof(CPUState, gpr[15]) },
2371 { "r16", offsetof(CPUState, gpr[16]) },
2372 { "r17", offsetof(CPUState, gpr[17]) },
2373 { "r18", offsetof(CPUState, gpr[18]) },
2374 { "r19", offsetof(CPUState, gpr[19]) },
2375 { "r20", offsetof(CPUState, gpr[20]) },
2376 { "r21", offsetof(CPUState, gpr[21]) },
2377 { "r22", offsetof(CPUState, gpr[22]) },
2378 { "r23", offsetof(CPUState, gpr[23]) },
2379 { "r24", offsetof(CPUState, gpr[24]) },
2380 { "r25", offsetof(CPUState, gpr[25]) },
2381 { "r26", offsetof(CPUState, gpr[26]) },
2382 { "r27", offsetof(CPUState, gpr[27]) },
2383 { "r28", offsetof(CPUState, gpr[28]) },
2384 { "r29", offsetof(CPUState, gpr[29]) },
2385 { "r30", offsetof(CPUState, gpr[30]) },
2386 { "r31", offsetof(CPUState, gpr[31]) },
2387 /* Floating point registers */
2388 { "f0", offsetof(CPUState, fpr[0]) },
2389 { "f1", offsetof(CPUState, fpr[1]) },
2390 { "f2", offsetof(CPUState, fpr[2]) },
2391 { "f3", offsetof(CPUState, fpr[3]) },
2392 { "f4", offsetof(CPUState, fpr[4]) },
2393 { "f5", offsetof(CPUState, fpr[5]) },
2394 { "f6", offsetof(CPUState, fpr[6]) },
2395 { "f7", offsetof(CPUState, fpr[7]) },
2396 { "f8", offsetof(CPUState, fpr[8]) },
2397 { "f9", offsetof(CPUState, fpr[9]) },
2398 { "f10", offsetof(CPUState, fpr[10]) },
2399 { "f11", offsetof(CPUState, fpr[11]) },
2400 { "f12", offsetof(CPUState, fpr[12]) },
2401 { "f13", offsetof(CPUState, fpr[13]) },
2402 { "f14", offsetof(CPUState, fpr[14]) },
2403 { "f15", offsetof(CPUState, fpr[15]) },
2404 { "f16", offsetof(CPUState, fpr[16]) },
2405 { "f17", offsetof(CPUState, fpr[17]) },
2406 { "f18", offsetof(CPUState, fpr[18]) },
2407 { "f19", offsetof(CPUState, fpr[19]) },
2408 { "f20", offsetof(CPUState, fpr[20]) },
2409 { "f21", offsetof(CPUState, fpr[21]) },
2410 { "f22", offsetof(CPUState, fpr[22]) },
2411 { "f23", offsetof(CPUState, fpr[23]) },
2412 { "f24", offsetof(CPUState, fpr[24]) },
2413 { "f25", offsetof(CPUState, fpr[25]) },
2414 { "f26", offsetof(CPUState, fpr[26]) },
2415 { "f27", offsetof(CPUState, fpr[27]) },
2416 { "f28", offsetof(CPUState, fpr[28]) },
2417 { "f29", offsetof(CPUState, fpr[29]) },
2418 { "f30", offsetof(CPUState, fpr[30]) },
2419 { "f31", offsetof(CPUState, fpr[31]) },
2420 { "fpscr", offsetof(CPUState, fpscr) },
2421 /* Next instruction pointer */
2422 { "nip|pc", offsetof(CPUState, nip) },
2423 { "lr", offsetof(CPUState, lr) },
2424 { "ctr", offsetof(CPUState, ctr) },
2425 { "decr", 0, &monitor_get_decr, },
2426 { "ccr", 0, &monitor_get_ccr, },
2427 /* Machine state register */
2428 { "msr", 0, &monitor_get_msr, },
2429 { "xer", 0, &monitor_get_xer, },
2430 { "tbu", 0, &monitor_get_tbu, },
2431 { "tbl", 0, &monitor_get_tbl, },
2432 #if defined(TARGET_PPC64)
2433 /* Address space register */
2434 { "asr", offsetof(CPUState, asr) },
2436 /* Segment registers */
2437 { "sdr1", offsetof(CPUState, sdr1) },
2438 { "sr0", offsetof(CPUState, sr[0]) },
2439 { "sr1", offsetof(CPUState, sr[1]) },
2440 { "sr2", offsetof(CPUState, sr[2]) },
2441 { "sr3", offsetof(CPUState, sr[3]) },
2442 { "sr4", offsetof(CPUState, sr[4]) },
2443 { "sr5", offsetof(CPUState, sr[5]) },
2444 { "sr6", offsetof(CPUState, sr[6]) },
2445 { "sr7", offsetof(CPUState, sr[7]) },
2446 { "sr8", offsetof(CPUState, sr[8]) },
2447 { "sr9", offsetof(CPUState, sr[9]) },
2448 { "sr10", offsetof(CPUState, sr[10]) },
2449 { "sr11", offsetof(CPUState, sr[11]) },
2450 { "sr12", offsetof(CPUState, sr[12]) },
2451 { "sr13", offsetof(CPUState, sr[13]) },
2452 { "sr14", offsetof(CPUState, sr[14]) },
2453 { "sr15", offsetof(CPUState, sr[15]) },
2454 /* Too lazy to put BATs and SPRs ... */
2455 #elif defined(TARGET_SPARC)
2456 { "g0", offsetof(CPUState, gregs[0]) },
2457 { "g1", offsetof(CPUState, gregs[1]) },
2458 { "g2", offsetof(CPUState, gregs[2]) },
2459 { "g3", offsetof(CPUState, gregs[3]) },
2460 { "g4", offsetof(CPUState, gregs[4]) },
2461 { "g5", offsetof(CPUState, gregs[5]) },
2462 { "g6", offsetof(CPUState, gregs[6]) },
2463 { "g7", offsetof(CPUState, gregs[7]) },
2464 { "o0", 0, monitor_get_reg },
2465 { "o1", 1, monitor_get_reg },
2466 { "o2", 2, monitor_get_reg },
2467 { "o3", 3, monitor_get_reg },
2468 { "o4", 4, monitor_get_reg },
2469 { "o5", 5, monitor_get_reg },
2470 { "o6", 6, monitor_get_reg },
2471 { "o7", 7, monitor_get_reg },
2472 { "l0", 8, monitor_get_reg },
2473 { "l1", 9, monitor_get_reg },
2474 { "l2", 10, monitor_get_reg },
2475 { "l3", 11, monitor_get_reg },
2476 { "l4", 12, monitor_get_reg },
2477 { "l5", 13, monitor_get_reg },
2478 { "l6", 14, monitor_get_reg },
2479 { "l7", 15, monitor_get_reg },
2480 { "i0", 16, monitor_get_reg },
2481 { "i1", 17, monitor_get_reg },
2482 { "i2", 18, monitor_get_reg },
2483 { "i3", 19, monitor_get_reg },
2484 { "i4", 20, monitor_get_reg },
2485 { "i5", 21, monitor_get_reg },
2486 { "i6", 22, monitor_get_reg },
2487 { "i7", 23, monitor_get_reg },
2488 { "pc", offsetof(CPUState, pc) },
2489 { "npc", offsetof(CPUState, npc) },
2490 { "y", offsetof(CPUState, y) },
2491 #ifndef TARGET_SPARC64
2492 { "psr", 0, &monitor_get_psr, },
2493 { "wim", offsetof(CPUState, wim) },
2495 { "tbr", offsetof(CPUState, tbr) },
2496 { "fsr", offsetof(CPUState, fsr) },
2497 { "f0", offsetof(CPUState, fpr[0]) },
2498 { "f1", offsetof(CPUState, fpr[1]) },
2499 { "f2", offsetof(CPUState, fpr[2]) },
2500 { "f3", offsetof(CPUState, fpr[3]) },
2501 { "f4", offsetof(CPUState, fpr[4]) },
2502 { "f5", offsetof(CPUState, fpr[5]) },
2503 { "f6", offsetof(CPUState, fpr[6]) },
2504 { "f7", offsetof(CPUState, fpr[7]) },
2505 { "f8", offsetof(CPUState, fpr[8]) },
2506 { "f9", offsetof(CPUState, fpr[9]) },
2507 { "f10", offsetof(CPUState, fpr[10]) },
2508 { "f11", offsetof(CPUState, fpr[11]) },
2509 { "f12", offsetof(CPUState, fpr[12]) },
2510 { "f13", offsetof(CPUState, fpr[13]) },
2511 { "f14", offsetof(CPUState, fpr[14]) },
2512 { "f15", offsetof(CPUState, fpr[15]) },
2513 { "f16", offsetof(CPUState, fpr[16]) },
2514 { "f17", offsetof(CPUState, fpr[17]) },
2515 { "f18", offsetof(CPUState, fpr[18]) },
2516 { "f19", offsetof(CPUState, fpr[19]) },
2517 { "f20", offsetof(CPUState, fpr[20]) },
2518 { "f21", offsetof(CPUState, fpr[21]) },
2519 { "f22", offsetof(CPUState, fpr[22]) },
2520 { "f23", offsetof(CPUState, fpr[23]) },
2521 { "f24", offsetof(CPUState, fpr[24]) },
2522 { "f25", offsetof(CPUState, fpr[25]) },
2523 { "f26", offsetof(CPUState, fpr[26]) },
2524 { "f27", offsetof(CPUState, fpr[27]) },
2525 { "f28", offsetof(CPUState, fpr[28]) },
2526 { "f29", offsetof(CPUState, fpr[29]) },
2527 { "f30", offsetof(CPUState, fpr[30]) },
2528 { "f31", offsetof(CPUState, fpr[31]) },
2529 #ifdef TARGET_SPARC64
2530 { "f32", offsetof(CPUState, fpr[32]) },
2531 { "f34", offsetof(CPUState, fpr[34]) },
2532 { "f36", offsetof(CPUState, fpr[36]) },
2533 { "f38", offsetof(CPUState, fpr[38]) },
2534 { "f40", offsetof(CPUState, fpr[40]) },
2535 { "f42", offsetof(CPUState, fpr[42]) },
2536 { "f44", offsetof(CPUState, fpr[44]) },
2537 { "f46", offsetof(CPUState, fpr[46]) },
2538 { "f48", offsetof(CPUState, fpr[48]) },
2539 { "f50", offsetof(CPUState, fpr[50]) },
2540 { "f52", offsetof(CPUState, fpr[52]) },
2541 { "f54", offsetof(CPUState, fpr[54]) },
2542 { "f56", offsetof(CPUState, fpr[56]) },
2543 { "f58", offsetof(CPUState, fpr[58]) },
2544 { "f60", offsetof(CPUState, fpr[60]) },
2545 { "f62", offsetof(CPUState, fpr[62]) },
2546 { "asi", offsetof(CPUState, asi) },
2547 { "pstate", offsetof(CPUState, pstate) },
2548 { "cansave", offsetof(CPUState, cansave) },
2549 { "canrestore", offsetof(CPUState, canrestore) },
2550 { "otherwin", offsetof(CPUState, otherwin) },
2551 { "wstate", offsetof(CPUState, wstate) },
2552 { "cleanwin", offsetof(CPUState, cleanwin) },
2553 { "fprs", offsetof(CPUState, fprs) },
2559 static void expr_error(Monitor *mon, const char *msg)
2561 monitor_printf(mon, "%s\n", msg);
2562 longjmp(expr_env, 1);
2565 /* return 0 if OK, -1 if not found, -2 if no CPU defined */
2566 static int get_monitor_def(target_long *pval, const char *name)
2568 const MonitorDef *md;
2571 for(md = monitor_defs; md->name != NULL; md++) {
2572 if (compare_cmd(name, md->name)) {
2573 if (md->get_value) {
2574 *pval = md->get_value(md, md->offset);
2576 CPUState *env = mon_get_cpu();
2579 ptr = (uint8_t *)env + md->offset;
2582 *pval = *(int32_t *)ptr;
2585 *pval = *(target_long *)ptr;
2598 static void next(void)
2602 while (qemu_isspace(*pch))
2607 static int64_t expr_sum(Monitor *mon);
2609 static int64_t expr_unary(Monitor *mon)
2618 n = expr_unary(mon);
2622 n = -expr_unary(mon);
2626 n = ~expr_unary(mon);
2632 expr_error(mon, "')' expected");
2639 expr_error(mon, "character constant expected");
2643 expr_error(mon, "missing terminating \' character");
2653 while ((*pch >= 'a' && *pch <= 'z') ||
2654 (*pch >= 'A' && *pch <= 'Z') ||
2655 (*pch >= '0' && *pch <= '9') ||
2656 *pch == '_' || *pch == '.') {
2657 if ((q - buf) < sizeof(buf) - 1)
2661 while (qemu_isspace(*pch))
2664 ret = get_monitor_def(®, buf);
2666 expr_error(mon, "unknown register");
2668 expr_error(mon, "no cpu defined");
2673 expr_error(mon, "unexpected end of expression");
2677 #if TARGET_PHYS_ADDR_BITS > 32
2678 n = strtoull(pch, &p, 0);
2680 n = strtoul(pch, &p, 0);
2683 expr_error(mon, "invalid char in expression");
2686 while (qemu_isspace(*pch))
2694 static int64_t expr_prod(Monitor *mon)
2699 val = expr_unary(mon);
2702 if (op != '*' && op != '/' && op != '%')
2705 val2 = expr_unary(mon);
2714 expr_error(mon, "division by zero");
2725 static int64_t expr_logic(Monitor *mon)
2730 val = expr_prod(mon);
2733 if (op != '&' && op != '|' && op != '^')
2736 val2 = expr_prod(mon);
2753 static int64_t expr_sum(Monitor *mon)
2758 val = expr_logic(mon);
2761 if (op != '+' && op != '-')
2764 val2 = expr_logic(mon);
2773 static int get_expr(Monitor *mon, int64_t *pval, const char **pp)
2776 if (setjmp(expr_env)) {
2780 while (qemu_isspace(*pch))
2782 *pval = expr_sum(mon);
2787 static int get_str(char *buf, int buf_size, const char **pp)
2795 while (qemu_isspace(*p))
2805 while (*p != '\0' && *p != '\"') {
2821 qemu_printf("unsupported escape code: '\\%c'\n", c);
2824 if ((q - buf) < buf_size - 1) {
2828 if ((q - buf) < buf_size - 1) {
2835 qemu_printf("unterminated string\n");
2840 while (*p != '\0' && !qemu_isspace(*p)) {
2841 if ((q - buf) < buf_size - 1) {
2853 * Store the command-name in cmdname, and return a pointer to
2854 * the remaining of the command string.
2856 static const char *get_command_name(const char *cmdline,
2857 char *cmdname, size_t nlen)
2860 const char *p, *pstart;
2863 while (qemu_isspace(*p))
2868 while (*p != '\0' && *p != '/' && !qemu_isspace(*p))
2873 memcpy(cmdname, pstart, len);
2874 cmdname[len] = '\0';
2879 * Read key of 'type' into 'key' and return the current
2882 static char *key_get_info(const char *type, char **key)
2890 p = strchr(type, ':');
2897 str = qemu_malloc(len + 1);
2898 memcpy(str, type, len);
2905 static int default_fmt_format = 'x';
2906 static int default_fmt_size = 4;
2910 static const mon_cmd_t *monitor_parse_command(Monitor *mon,
2911 const char *cmdline,
2914 const char *p, *typestr;
2916 const mon_cmd_t *cmd;
2922 monitor_printf(mon, "command='%s'\n", cmdline);
2925 /* extract the command name */
2926 p = get_command_name(cmdline, cmdname, sizeof(cmdname));
2930 /* find the command */
2931 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
2932 if (compare_cmd(cmdname, cmd->name))
2936 if (cmd->name == NULL) {
2937 monitor_printf(mon, "unknown command: '%s'\n", cmdname);
2941 /* parse the parameters */
2942 typestr = cmd->args_type;
2944 typestr = key_get_info(typestr, &key);
2956 while (qemu_isspace(*p))
2958 if (*typestr == '?') {
2961 /* no optional string: NULL argument */
2965 ret = get_str(buf, sizeof(buf), &p);
2969 monitor_printf(mon, "%s: filename expected\n",
2973 monitor_printf(mon, "%s: block device name expected\n",
2977 monitor_printf(mon, "%s: string expected\n", cmdname);
2982 qdict_put(qdict, key, qstring_from_str(buf));
2987 int count, format, size;
2989 while (qemu_isspace(*p))
2995 if (qemu_isdigit(*p)) {
2997 while (qemu_isdigit(*p)) {
2998 count = count * 10 + (*p - '0');
3036 if (*p != '\0' && !qemu_isspace(*p)) {
3037 monitor_printf(mon, "invalid char in format: '%c'\n",
3042 format = default_fmt_format;
3043 if (format != 'i') {
3044 /* for 'i', not specifying a size gives -1 as size */
3046 size = default_fmt_size;
3047 default_fmt_size = size;
3049 default_fmt_format = format;
3052 format = default_fmt_format;
3053 if (format != 'i') {
3054 size = default_fmt_size;
3059 qdict_put(qdict, "count", qint_from_int(count));
3060 qdict_put(qdict, "format", qint_from_int(format));
3061 qdict_put(qdict, "size", qint_from_int(size));
3069 while (qemu_isspace(*p))
3071 if (*typestr == '?' || *typestr == '.') {
3072 if (*typestr == '?') {
3080 while (qemu_isspace(*p))
3089 if (get_expr(mon, &val, &p))
3091 /* Check if 'i' is greater than 32-bit */
3092 if ((c == 'i') && ((val >> 32) & 0xffffffff)) {
3093 monitor_printf(mon, "\'%s\' has failed: ", cmdname);
3094 monitor_printf(mon, "integer is for 32-bit values\n");
3097 qdict_put(qdict, key, qint_from_int(val));
3108 while (qemu_isspace(*p))
3114 monitor_printf(mon, "%s: unsupported option -%c\n",
3121 qdict_put(qdict, key, qint_from_int(has_option));
3126 monitor_printf(mon, "%s: unknown type '%c'\n", cmdname, c);
3132 /* check that all arguments were parsed */
3133 while (qemu_isspace(*p))
3136 monitor_printf(mon, "%s: extraneous characters at the end of line\n",
3148 static void monitor_handle_command(Monitor *mon, const char *cmdline)
3151 const mon_cmd_t *cmd;
3153 qdict = qdict_new();
3155 cmd = monitor_parse_command(mon, cmdline, qdict);
3159 qemu_errors_to_mon(mon);
3161 if (monitor_handler_ported(cmd)) {
3162 QObject *data = NULL;
3164 cmd->mhandler.cmd_new(mon, qdict, &data);
3166 cmd->user_print(mon, data);
3168 qobject_decref(data);
3170 cmd->mhandler.cmd(mon, qdict);
3173 qemu_errors_to_previous();
3179 static void cmd_completion(const char *name, const char *list)
3181 const char *p, *pstart;
3190 p = pstart + strlen(pstart);
3192 if (len > sizeof(cmd) - 2)
3193 len = sizeof(cmd) - 2;
3194 memcpy(cmd, pstart, len);
3196 if (name[0] == '\0' || !strncmp(name, cmd, strlen(name))) {
3197 readline_add_completion(cur_mon->rs, cmd);
3205 static void file_completion(const char *input)
3210 char file[1024], file_prefix[1024];
3214 p = strrchr(input, '/');
3217 pstrcpy(file_prefix, sizeof(file_prefix), input);
3218 pstrcpy(path, sizeof(path), ".");
3220 input_path_len = p - input + 1;
3221 memcpy(path, input, input_path_len);
3222 if (input_path_len > sizeof(path) - 1)
3223 input_path_len = sizeof(path) - 1;
3224 path[input_path_len] = '\0';
3225 pstrcpy(file_prefix, sizeof(file_prefix), p + 1);
3227 #ifdef DEBUG_COMPLETION
3228 monitor_printf(cur_mon, "input='%s' path='%s' prefix='%s'\n",
3229 input, path, file_prefix);
3231 ffs = opendir(path);
3239 if (strstart(d->d_name, file_prefix, NULL)) {
3240 memcpy(file, input, input_path_len);
3241 if (input_path_len < sizeof(file))
3242 pstrcpy(file + input_path_len, sizeof(file) - input_path_len,
3244 /* stat the file to find out if it's a directory.
3245 * In that case add a slash to speed up typing long paths
3248 if(S_ISDIR(sb.st_mode))
3249 pstrcat(file, sizeof(file), "/");
3250 readline_add_completion(cur_mon->rs, file);
3256 static void block_completion_it(void *opaque, BlockDriverState *bs)
3258 const char *name = bdrv_get_device_name(bs);
3259 const char *input = opaque;
3261 if (input[0] == '\0' ||
3262 !strncmp(name, (char *)input, strlen(input))) {
3263 readline_add_completion(cur_mon->rs, name);
3267 /* NOTE: this parser is an approximate form of the real command parser */
3268 static void parse_cmdline(const char *cmdline,
3269 int *pnb_args, char **args)
3278 while (qemu_isspace(*p))
3282 if (nb_args >= MAX_ARGS)
3284 ret = get_str(buf, sizeof(buf), &p);
3285 args[nb_args] = qemu_strdup(buf);
3290 *pnb_args = nb_args;
3293 static const char *next_arg_type(const char *typestr)
3295 const char *p = strchr(typestr, ':');
3296 return (p != NULL ? ++p : typestr);
3299 static void monitor_find_completion(const char *cmdline)
3301 const char *cmdname;
3302 char *args[MAX_ARGS];
3303 int nb_args, i, len;
3304 const char *ptype, *str;
3305 const mon_cmd_t *cmd;
3308 parse_cmdline(cmdline, &nb_args, args);
3309 #ifdef DEBUG_COMPLETION
3310 for(i = 0; i < nb_args; i++) {
3311 monitor_printf(cur_mon, "arg%d = '%s'\n", i, (char *)args[i]);
3315 /* if the line ends with a space, it means we want to complete the
3317 len = strlen(cmdline);
3318 if (len > 0 && qemu_isspace(cmdline[len - 1])) {
3319 if (nb_args >= MAX_ARGS)
3321 args[nb_args++] = qemu_strdup("");
3324 /* command completion */
3329 readline_set_completion_index(cur_mon->rs, strlen(cmdname));
3330 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3331 cmd_completion(cmdname, cmd->name);
3334 /* find the command */
3335 for(cmd = mon_cmds; cmd->name != NULL; cmd++) {
3336 if (compare_cmd(args[0], cmd->name))
3341 ptype = next_arg_type(cmd->args_type);
3342 for(i = 0; i < nb_args - 2; i++) {
3343 if (*ptype != '\0') {
3344 ptype = next_arg_type(ptype);
3345 while (*ptype == '?')
3346 ptype = next_arg_type(ptype);
3349 str = args[nb_args - 1];
3350 if (*ptype == '-' && ptype[1] != '\0') {
3355 /* file completion */
3356 readline_set_completion_index(cur_mon->rs, strlen(str));
3357 file_completion(str);
3360 /* block device name completion */
3361 readline_set_completion_index(cur_mon->rs, strlen(str));
3362 bdrv_iterate(block_completion_it, (void *)str);
3365 /* XXX: more generic ? */
3366 if (!strcmp(cmd->name, "info")) {
3367 readline_set_completion_index(cur_mon->rs, strlen(str));
3368 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
3369 cmd_completion(str, cmd->name);
3371 } else if (!strcmp(cmd->name, "sendkey")) {
3372 char *sep = strrchr(str, '-');
3375 readline_set_completion_index(cur_mon->rs, strlen(str));
3376 for(key = key_defs; key->name != NULL; key++) {
3377 cmd_completion(str, key->name);
3379 } else if (!strcmp(cmd->name, "help|?")) {
3380 readline_set_completion_index(cur_mon->rs, strlen(str));
3381 for (cmd = mon_cmds; cmd->name != NULL; cmd++) {
3382 cmd_completion(str, cmd->name);
3390 for(i = 0; i < nb_args; i++)
3394 static int monitor_can_read(void *opaque)
3396 Monitor *mon = opaque;
3398 return (mon->suspend_cnt == 0) ? 128 : 0;
3401 static void monitor_read(void *opaque, const uint8_t *buf, int size)
3403 Monitor *old_mon = cur_mon;
3409 for (i = 0; i < size; i++)
3410 readline_handle_byte(cur_mon->rs, buf[i]);
3412 if (size == 0 || buf[size - 1] != 0)
3413 monitor_printf(cur_mon, "corrupted command\n");
3415 monitor_handle_command(cur_mon, (char *)buf);
3421 static void monitor_command_cb(Monitor *mon, const char *cmdline, void *opaque)
3423 monitor_suspend(mon);
3424 monitor_handle_command(mon, cmdline);
3425 monitor_resume(mon);
3428 int monitor_suspend(Monitor *mon)
3436 void monitor_resume(Monitor *mon)
3440 if (--mon->suspend_cnt == 0)
3441 readline_show_prompt(mon->rs);
3444 static void monitor_event(void *opaque, int event)
3446 Monitor *mon = opaque;
3449 case CHR_EVENT_MUX_IN:
3451 if (mon->reset_seen) {
3452 readline_restart(mon->rs);
3453 monitor_resume(mon);
3456 mon->suspend_cnt = 0;
3460 case CHR_EVENT_MUX_OUT:
3461 if (mon->reset_seen) {
3462 if (mon->suspend_cnt == 0) {
3463 monitor_printf(mon, "\n");
3466 monitor_suspend(mon);
3473 case CHR_EVENT_RESET:
3474 monitor_printf(mon, "QEMU %s monitor - type 'help' for more "
3475 "information\n", QEMU_VERSION);
3476 if (!mon->mux_out) {
3477 readline_show_prompt(mon->rs);
3479 mon->reset_seen = 1;
3493 void monitor_init(CharDriverState *chr, int flags)
3495 static int is_first_init = 1;
3498 if (is_first_init) {
3499 key_timer = qemu_new_timer(vm_clock, release_keys, NULL);
3503 mon = qemu_mallocz(sizeof(*mon));
3507 if (flags & MONITOR_USE_READLINE) {
3508 mon->rs = readline_init(mon, monitor_find_completion);
3509 monitor_read_command(mon, 0);
3512 qemu_chr_add_handlers(chr, monitor_can_read, monitor_read, monitor_event,
3515 QLIST_INSERT_HEAD(&mon_list, mon, entry);
3516 if (!cur_mon || (flags & MONITOR_IS_DEFAULT))
3520 static void bdrv_password_cb(Monitor *mon, const char *password, void *opaque)
3522 BlockDriverState *bs = opaque;
3525 if (bdrv_set_key(bs, password) != 0) {
3526 monitor_printf(mon, "invalid password\n");
3529 if (mon->password_completion_cb)
3530 mon->password_completion_cb(mon->password_opaque, ret);
3532 monitor_read_command(mon, 1);
3535 void monitor_read_bdrv_key_start(Monitor *mon, BlockDriverState *bs,
3536 BlockDriverCompletionFunc *completion_cb,
3541 if (!bdrv_key_required(bs)) {
3543 completion_cb(opaque, 0);
3547 monitor_printf(mon, "%s (%s) is encrypted.\n", bdrv_get_device_name(bs),
3548 bdrv_get_encrypted_filename(bs));
3550 mon->password_completion_cb = completion_cb;
3551 mon->password_opaque = opaque;
3553 err = monitor_read_password(mon, bdrv_password_cb, bs);
3555 if (err && completion_cb)
3556 completion_cb(opaque, err);
3559 typedef struct QemuErrorSink QemuErrorSink;
3560 struct QemuErrorSink {
3569 QemuErrorSink *previous;
3572 static QemuErrorSink *qemu_error_sink;
3574 void qemu_errors_to_file(FILE *fp)
3576 QemuErrorSink *sink;
3578 sink = qemu_mallocz(sizeof(*sink));
3579 sink->dest = ERR_SINK_FILE;
3581 sink->previous = qemu_error_sink;
3582 qemu_error_sink = sink;
3585 void qemu_errors_to_mon(Monitor *mon)
3587 QemuErrorSink *sink;
3589 sink = qemu_mallocz(sizeof(*sink));
3590 sink->dest = ERR_SINK_MONITOR;
3592 sink->previous = qemu_error_sink;
3593 qemu_error_sink = sink;
3596 void qemu_errors_to_previous(void)
3598 QemuErrorSink *sink;
3600 assert(qemu_error_sink != NULL);
3601 sink = qemu_error_sink;
3602 qemu_error_sink = sink->previous;
3606 void qemu_error(const char *fmt, ...)
3610 assert(qemu_error_sink != NULL);
3611 switch (qemu_error_sink->dest) {
3613 va_start(args, fmt);
3614 vfprintf(qemu_error_sink->fp, fmt, args);
3617 case ERR_SINK_MONITOR:
3618 va_start(args, fmt);
3619 monitor_vprintf(qemu_error_sink->mon, fmt, args);