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
30 #define offsetof(type, field) ((size_t) &((type *)0)->field)
33 #define TERM_CMD_BUF_SIZE 4095
34 #define TERM_MAX_CMDS 64
40 #define printf do_not_use_printf
42 static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1];
43 static int term_cmd_buf_index;
44 static int term_cmd_buf_size;
45 static int term_esc_state;
46 static int term_esc_param;
48 static char *term_history[TERM_MAX_CMDS];
49 static int term_hist_entry;
55 * 's' string (accept optional quote)
57 * '/' optional gdb-like print format (like "/10x")
59 * '?' optional type (for 'F', 's' and 'i')
63 typedef struct term_cmd_t {
65 const char *args_type;
71 static term_cmd_t term_cmds[];
72 static term_cmd_t info_cmds[];
74 void term_printf(const char *fmt, ...)
87 static int compare_cmd(const char *name, const char *list)
89 const char *p, *pstart;
97 p = pstart + strlen(pstart);
98 if ((p - pstart) == len && !memcmp(pstart, name, len))
107 static void help_cmd1(term_cmd_t *cmds, const char *prefix, const char *name)
111 for(cmd = cmds; cmd->name != NULL; cmd++) {
112 if (!name || !strcmp(name, cmd->name))
113 term_printf("%s%s %s -- %s\n", prefix, cmd->name, cmd->params, cmd->help);
117 static void help_cmd(const char *name)
119 if (name && !strcmp(name, "info")) {
120 help_cmd1(info_cmds, "info ", NULL);
122 help_cmd1(term_cmds, "", name);
123 if (name && !strcmp(name, "log")) {
125 term_printf("Log items (comma separated):\n");
126 term_printf("%-10s %s\n", "none", "remove all logs");
127 for(item = cpu_log_items; item->mask != 0; item++) {
128 term_printf("%-10s %s\n", item->name, item->help);
134 static void do_help(const char *name)
139 static void do_commit(void)
143 for (i = 0; i < MAX_DISKS; i++) {
145 bdrv_commit(bs_table[i]);
149 static void do_info(const char *item)
155 for(cmd = info_cmds; cmd->name != NULL; cmd++) {
156 if (compare_cmd(item, cmd->name))
166 static void do_info_network(void)
171 for(i = 0; i < nb_nics; i++) {
173 term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
174 for(j = 0; j < 6; j++) {
177 term_printf("%02x", nd->macaddr[j]);
183 static void do_info_block(void)
188 static void do_info_registers(void)
191 cpu_dump_state(cpu_single_env, stdout, X86_DUMP_FPU | X86_DUMP_CCOP);
193 cpu_dump_state(cpu_single_env, stdout, 0);
197 static void do_info_history (void)
201 for (i = 0; i < TERM_MAX_CMDS; i++) {
202 if (term_history[i] == NULL)
204 term_printf("%d: '%s'\n", i, term_history[i]);
208 static void do_quit(void)
213 static int eject_device(BlockDriverState *bs, int force)
215 if (bdrv_is_inserted(bs)) {
217 if (!bdrv_is_removable(bs)) {
218 term_printf("device is not removable\n");
221 if (bdrv_is_locked(bs)) {
222 term_printf("device is locked\n");
231 static void do_eject(int force, const char *filename)
233 BlockDriverState *bs;
235 term_printf("%d %s\n", force, filename);
237 bs = bdrv_find(filename);
239 term_printf("device not found\n");
242 eject_device(bs, force);
245 static void do_change(const char *device, const char *filename)
247 BlockDriverState *bs;
249 bs = bdrv_find(device);
251 term_printf("device not found\n");
254 if (eject_device(bs, 0) < 0)
256 bdrv_open(bs, filename, 0);
259 static void do_screen_dump(const char *filename)
261 vga_screen_dump(filename);
264 static void do_log(const char *items)
268 if (!strcmp(items, "none")) {
271 mask = cpu_str_to_log_mask(items);
280 static void do_savevm(const char *filename)
282 if (qemu_savevm(filename) < 0)
283 term_printf("I/O error when saving VM to '%s'\n", filename);
286 static void do_loadvm(const char *filename)
288 if (qemu_loadvm(filename) < 0)
289 term_printf("I/O error when loading VM from '%s'\n", filename);
292 static void do_stop(void)
294 vm_stop(EXCP_INTERRUPT);
297 static void do_cont(void)
302 #ifdef CONFIG_GDBSTUB
303 static void do_gdbserver(int has_port, int port)
306 port = DEFAULT_GDBSTUB_PORT;
307 if (gdbserver_start(port) < 0) {
308 qemu_printf("Could not open gdbserver socket on port %d\n", port);
310 qemu_printf("Waiting gdb connection on port %d\n", port);
315 static void term_printc(int c)
332 if (c >= 32 && c <= 126) {
333 term_printf("%c", c);
335 term_printf("\\x%02x", c);
342 static void memory_dump(int count, int format, int wsize,
343 target_ulong addr, int is_physical)
345 int nb_per_line, l, line_size, i, max_digits, len;
355 } else if (wsize == 4) {
358 /* as default we use the current CS size */
360 if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
364 monitor_disas(addr, count, is_physical, flags);
373 nb_per_line = line_size / wsize;
378 max_digits = (wsize * 8 + 2) / 3;
382 max_digits = (wsize * 8) / 4;
386 max_digits = (wsize * 8 * 10 + 32) / 33;
394 term_printf("0x%08x:", addr);
399 cpu_physical_memory_rw(addr, buf, l, 0);
401 cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
408 v = ldub_raw(buf + i);
411 v = lduw_raw(buf + i);
414 v = ldl_raw(buf + i);
417 v = ldq_raw(buf + i);
423 term_printf("%#*llo", max_digits, v);
426 term_printf("0x%0*llx", max_digits, v);
429 term_printf("%*llu", max_digits, v);
432 term_printf("%*lld", max_digits, v);
446 static void do_memory_dump(int count, int format, int size, int addr)
448 memory_dump(count, format, size, addr, 0);
451 static void do_physical_memory_dump(int count, int format, int size, int addr)
453 memory_dump(count, format, size, addr, 1);
456 static void do_print(int count, int format, int size, int val)
460 term_printf("%#o", val);
463 term_printf("%#x", val);
466 term_printf("%u", val);
470 term_printf("%d", val);
484 static const KeyDef key_defs[] = {
507 { 0x0e, "backspace" },
542 { 0x3a, "caps_lock" },
553 { 0x45, "num_lock" },
554 { 0x46, "scroll_lock" },
578 static int get_keycode(const char *key)
582 for(p = key_defs; p->name != NULL; p++) {
583 if (!strcmp(key, p->name))
589 static void do_send_key(const char *string)
592 uint8_t keycodes[16];
594 int nb_keycodes, keycode, i;
600 while (*p != '\0' && *p != '-') {
601 if ((q - keybuf) < sizeof(keybuf) - 1) {
607 keycode = get_keycode(keybuf);
609 term_printf("unknown key: '%s'\n", keybuf);
612 keycodes[nb_keycodes++] = keycode;
617 /* key down events */
618 for(i = 0; i < nb_keycodes; i++) {
619 keycode = keycodes[i];
621 kbd_put_keycode(0xe0);
622 kbd_put_keycode(keycode & 0x7f);
625 for(i = nb_keycodes - 1; i >= 0; i--) {
626 keycode = keycodes[i];
628 kbd_put_keycode(0xe0);
629 kbd_put_keycode(keycode | 0x80);
634 static term_cmd_t term_cmds[] = {
635 { "help|?", "s?", do_help,
636 "[cmd]", "show the help" },
637 { "commit", "", do_commit,
638 "", "commit changes to the disk images (if -snapshot is used)" },
639 { "info", "s?", do_info,
640 "subcommand", "show various information about the system state" },
641 { "q|quit", "", do_quit,
642 "", "quit the emulator" },
643 { "eject", "-fs", do_eject,
644 "[-f] device", "eject a removable media (use -f to force it)" },
645 { "change", "sF", do_change,
646 "device filename", "change a removable media" },
647 { "screendump", "F", do_screen_dump,
648 "filename", "save screen into PPM image 'filename'" },
649 { "log", "s", do_log,
650 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
651 { "savevm", "F", do_savevm,
652 "filename", "save the whole virtual machine state to 'filename'" },
653 { "loadvm", "F", do_loadvm,
654 "filename", "restore the whole virtual machine state from 'filename'" },
655 { "stop", "", do_stop,
656 "", "stop emulation", },
657 { "c|cont", "", do_cont,
658 "", "resume emulation", },
659 #ifdef CONFIG_GDBSTUB
660 { "gdbserver", "i?", do_gdbserver,
661 "[port]", "start gdbserver session (default port=1234)", },
663 { "x", "/i", do_memory_dump,
664 "/fmt addr", "virtual memory dump starting at 'addr'", },
665 { "xp", "/i", do_physical_memory_dump,
666 "/fmt addr", "physical memory dump starting at 'addr'", },
667 { "p|print", "/i", do_print,
668 "/fmt expr", "print expression value (use $reg for CPU register access)", },
669 { "sendkey", "s", do_send_key,
670 "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" },
674 static term_cmd_t info_cmds[] = {
675 { "network", "", do_info_network,
676 "", "show the network state" },
677 { "block", "", do_info_block,
678 "", "show the block devices" },
679 { "registers", "", do_info_registers,
680 "", "show the cpu registers" },
681 { "history", "", do_info_history,
682 "", "show the command line history", },
683 { "irq", "", irq_info,
684 "", "show the interrupts statistics (if available)", },
685 { "pic", "", pic_info,
686 "", "show i8259 (PIC) state", },
687 { "pci", "", pci_info,
688 "", "show PCI info", },
692 /*******************************************************************/
694 static const char *pch;
695 static jmp_buf expr_env;
697 typedef struct MonitorDef {
700 int (*get_value)(struct MonitorDef *md);
703 #if defined(TARGET_I386)
704 static int monitor_get_pc (struct MonitorDef *md)
706 return cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base;
710 #if defined(TARGET_PPC)
711 static int monitor_get_ccr (struct MonitorDef *md)
717 for (i = 0; i < 8; i++)
718 u |= cpu_single_env->crf[i] << (32 - (4 * i));
723 static int monitor_get_msr (struct MonitorDef *md)
725 return (cpu_single_env->msr[MSR_POW] << MSR_POW) |
726 (cpu_single_env->msr[MSR_ILE] << MSR_ILE) |
727 (cpu_single_env->msr[MSR_EE] << MSR_EE) |
728 (cpu_single_env->msr[MSR_PR] << MSR_PR) |
729 (cpu_single_env->msr[MSR_FP] << MSR_FP) |
730 (cpu_single_env->msr[MSR_ME] << MSR_ME) |
731 (cpu_single_env->msr[MSR_FE0] << MSR_FE0) |
732 (cpu_single_env->msr[MSR_SE] << MSR_SE) |
733 (cpu_single_env->msr[MSR_BE] << MSR_BE) |
734 (cpu_single_env->msr[MSR_FE1] << MSR_FE1) |
735 (cpu_single_env->msr[MSR_IP] << MSR_IP) |
736 (cpu_single_env->msr[MSR_IR] << MSR_IR) |
737 (cpu_single_env->msr[MSR_DR] << MSR_DR) |
738 (cpu_single_env->msr[MSR_RI] << MSR_RI) |
739 (cpu_single_env->msr[MSR_LE] << MSR_LE);
742 static int monitor_get_xer (struct MonitorDef *md)
744 return (cpu_single_env->xer[XER_SO] << XER_SO) |
745 (cpu_single_env->xer[XER_OV] << XER_OV) |
746 (cpu_single_env->xer[XER_CA] << XER_CA) |
747 (cpu_single_env->xer[XER_BC] << XER_BC);
750 uint32_t cpu_ppc_load_decr (CPUState *env);
751 static int monitor_get_decr (struct MonitorDef *md)
753 return cpu_ppc_load_decr(cpu_single_env);
756 uint32_t cpu_ppc_load_tbu (CPUState *env);
757 static int monitor_get_tbu (struct MonitorDef *md)
759 return cpu_ppc_load_tbu(cpu_single_env);
762 uint32_t cpu_ppc_load_tbl (CPUState *env);
763 static int monitor_get_tbl (struct MonitorDef *md)
765 return cpu_ppc_load_tbl(cpu_single_env);
769 static MonitorDef monitor_defs[] = {
772 #define SEG(name, seg) \
773 { name, offsetof(CPUState, segs[seg].selector) },\
774 { name ".base", offsetof(CPUState, segs[seg].base) },\
775 { name ".limit", offsetof(CPUState, segs[seg].limit) },
777 { "eax", offsetof(CPUState, regs[0]) },
778 { "ecx", offsetof(CPUState, regs[1]) },
779 { "edx", offsetof(CPUState, regs[2]) },
780 { "ebx", offsetof(CPUState, regs[3]) },
781 { "esp|sp", offsetof(CPUState, regs[4]) },
782 { "ebp|fp", offsetof(CPUState, regs[5]) },
783 { "esi", offsetof(CPUState, regs[6]) },
784 { "esi", offsetof(CPUState, regs[7]) },
785 { "eflags", offsetof(CPUState, eflags) },
786 { "eip", offsetof(CPUState, eip) },
792 { "pc", 0, monitor_get_pc, },
793 #elif defined(TARGET_PPC)
794 { "r0", offsetof(CPUState, gpr[0]) },
795 { "r1", offsetof(CPUState, gpr[1]) },
796 { "r2", offsetof(CPUState, gpr[2]) },
797 { "r3", offsetof(CPUState, gpr[3]) },
798 { "r4", offsetof(CPUState, gpr[4]) },
799 { "r5", offsetof(CPUState, gpr[5]) },
800 { "r6", offsetof(CPUState, gpr[6]) },
801 { "r7", offsetof(CPUState, gpr[7]) },
802 { "r8", offsetof(CPUState, gpr[8]) },
803 { "r9", offsetof(CPUState, gpr[9]) },
804 { "r10", offsetof(CPUState, gpr[10]) },
805 { "r11", offsetof(CPUState, gpr[11]) },
806 { "r12", offsetof(CPUState, gpr[12]) },
807 { "r13", offsetof(CPUState, gpr[13]) },
808 { "r14", offsetof(CPUState, gpr[14]) },
809 { "r15", offsetof(CPUState, gpr[15]) },
810 { "r16", offsetof(CPUState, gpr[16]) },
811 { "r17", offsetof(CPUState, gpr[17]) },
812 { "r18", offsetof(CPUState, gpr[18]) },
813 { "r19", offsetof(CPUState, gpr[19]) },
814 { "r20", offsetof(CPUState, gpr[20]) },
815 { "r21", offsetof(CPUState, gpr[21]) },
816 { "r22", offsetof(CPUState, gpr[22]) },
817 { "r23", offsetof(CPUState, gpr[23]) },
818 { "r24", offsetof(CPUState, gpr[24]) },
819 { "r25", offsetof(CPUState, gpr[25]) },
820 { "r26", offsetof(CPUState, gpr[26]) },
821 { "r27", offsetof(CPUState, gpr[27]) },
822 { "r28", offsetof(CPUState, gpr[28]) },
823 { "r29", offsetof(CPUState, gpr[29]) },
824 { "r30", offsetof(CPUState, gpr[30]) },
825 { "r31", offsetof(CPUState, gpr[31]) },
826 { "nip|pc", offsetof(CPUState, nip) },
827 { "lr", offsetof(CPUState, lr) },
828 { "ctr", offsetof(CPUState, ctr) },
829 { "decr", 0, &monitor_get_decr, },
830 { "ccr", 0, &monitor_get_ccr, },
831 { "msr", 0, &monitor_get_msr, },
832 { "xer", 0, &monitor_get_xer, },
833 { "tbu", 0, &monitor_get_tbu, },
834 { "tbl", 0, &monitor_get_tbl, },
835 { "sdr1", offsetof(CPUState, sdr1) },
836 { "sr0", offsetof(CPUState, sr[0]) },
837 { "sr1", offsetof(CPUState, sr[1]) },
838 { "sr2", offsetof(CPUState, sr[2]) },
839 { "sr3", offsetof(CPUState, sr[3]) },
840 { "sr4", offsetof(CPUState, sr[4]) },
841 { "sr5", offsetof(CPUState, sr[5]) },
842 { "sr6", offsetof(CPUState, sr[6]) },
843 { "sr7", offsetof(CPUState, sr[7]) },
844 { "sr8", offsetof(CPUState, sr[8]) },
845 { "sr9", offsetof(CPUState, sr[9]) },
846 { "sr10", offsetof(CPUState, sr[10]) },
847 { "sr11", offsetof(CPUState, sr[11]) },
848 { "sr12", offsetof(CPUState, sr[12]) },
849 { "sr13", offsetof(CPUState, sr[13]) },
850 { "sr14", offsetof(CPUState, sr[14]) },
851 { "sr15", offsetof(CPUState, sr[15]) },
852 /* Too lazy to put BATs and SPRs ... */
857 static void expr_error(const char *fmt)
861 longjmp(expr_env, 1);
864 static int get_monitor_def(int *pval, const char *name)
867 for(md = monitor_defs; md->name != NULL; md++) {
868 if (compare_cmd(name, md->name)) {
870 *pval = md->get_value(md);
872 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
880 static void next(void)
884 while (isspace(*pch))
889 static int expr_sum(void);
891 static int expr_unary(void)
913 expr_error("')' expected");
923 while ((*pch >= 'a' && *pch <= 'z') ||
924 (*pch >= 'A' && *pch <= 'Z') ||
925 (*pch >= '0' && *pch <= '9') ||
926 *pch == '_' || *pch == '.') {
927 if ((q - buf) < sizeof(buf) - 1)
931 while (isspace(*pch))
934 if (get_monitor_def(&n, buf))
935 expr_error("unknown register");
939 expr_error("unexpected end of expression");
943 n = strtoul(pch, &p, 0);
945 expr_error("invalid char in expression");
948 while (isspace(*pch))
956 static int expr_prod(void)
963 if (op != '*' && op != '/' && op != '%')
975 expr_error("division by zero");
986 static int expr_logic(void)
993 if (op != '&' && op != '|' && op != '^')
1013 static int expr_sum(void)
1020 if (op != '+' && op != '-')
1023 val2 = expr_logic();
1032 static int get_expr(int *pval, const char **pp)
1035 if (setjmp(expr_env)) {
1039 while (isspace(*pch))
1046 static int get_str(char *buf, int buf_size, const char **pp)
1063 while (*p != '\0' && *p != '\"') {
1079 qemu_printf("unsupported escape code: '\\%c'\n", c);
1082 if ((q - buf) < buf_size - 1) {
1086 if ((q - buf) < buf_size - 1) {
1093 qemu_printf("unterminated string\n");
1098 while (*p != '\0' && !isspace(*p)) {
1099 if ((q - buf) < buf_size - 1) {
1110 static int default_fmt_format = 'x';
1111 static int default_fmt_size = 4;
1115 static void term_handle_command(const char *cmdline)
1117 const char *p, *pstart, *typestr;
1119 int c, nb_args, len, i, has_arg;
1123 void *str_allocated[MAX_ARGS];
1124 void *args[MAX_ARGS];
1127 term_printf("command='%s'\n", cmdline);
1130 /* extract the command name */
1138 while (*p != '\0' && *p != '/' && !isspace(*p))
1141 if (len > sizeof(cmdname) - 1)
1142 len = sizeof(cmdname) - 1;
1143 memcpy(cmdname, pstart, len);
1144 cmdname[len] = '\0';
1146 /* find the command */
1147 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
1148 if (compare_cmd(cmdname, cmd->name))
1151 term_printf("unknown command: '%s'\n", cmdname);
1155 for(i = 0; i < MAX_ARGS; i++)
1156 str_allocated[i] = NULL;
1158 /* parse the parameters */
1159 typestr = cmd->args_type;
1175 if (*typestr == '?') {
1178 /* no optional string: NULL argument */
1183 ret = get_str(buf, sizeof(buf), &p);
1186 term_printf("%s: filename expected\n", cmdname);
1188 term_printf("%s: string expected\n", cmdname);
1191 str = qemu_malloc(strlen(buf) + 1);
1193 str_allocated[nb_args] = str;
1195 if (nb_args >= MAX_ARGS) {
1197 term_printf("%s: too many arguments\n", cmdname);
1200 args[nb_args++] = str;
1205 int count, format, size;
1215 while (isdigit(*p)) {
1216 count = count * 10 + (*p - '0');
1254 if (*p != '\0' && !isspace(*p)) {
1255 term_printf("invalid char in format: '%c'\n", *p);
1259 format = default_fmt_format;
1260 if (format != 'i') {
1261 /* for 'i', not specifying a size gives -1 as size */
1263 size = default_fmt_size;
1265 default_fmt_size = size;
1266 default_fmt_format = format;
1269 format = default_fmt_format;
1270 if (format != 'i') {
1271 size = default_fmt_size;
1276 if (nb_args + 3 > MAX_ARGS)
1278 args[nb_args++] = (void*)count;
1279 args[nb_args++] = (void*)format;
1280 args[nb_args++] = (void*)size;
1288 if (*typestr == '?') {
1294 if (nb_args >= MAX_ARGS)
1296 args[nb_args++] = (void *)has_arg;
1298 if (nb_args >= MAX_ARGS)
1304 if (get_expr(&val, &p))
1307 if (nb_args >= MAX_ARGS)
1309 args[nb_args++] = (void *)val;
1326 term_printf("%s: unsupported option -%c\n",
1333 if (nb_args >= MAX_ARGS)
1335 args[nb_args++] = (void *)has_option;
1340 term_printf("%s: unknown type '%c'\n", cmdname, c);
1344 /* check that all arguments were parsed */
1348 term_printf("%s: extraneous characters at the end of line\n",
1358 cmd->handler(args[0]);
1361 cmd->handler(args[0], args[1]);
1364 cmd->handler(args[0], args[1], args[2]);
1367 cmd->handler(args[0], args[1], args[2], args[3]);
1370 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1373 term_printf("unsupported number of arguments: %d\n", nb_args);
1377 for(i = 0; i < MAX_ARGS; i++)
1378 qemu_free(str_allocated[i]);
1382 static void term_show_prompt(void)
1384 term_printf("(qemu) ");
1386 term_cmd_buf_index = 0;
1387 term_cmd_buf_size = 0;
1388 term_esc_state = IS_NORM;
1391 static void term_print_cmdline (const char *cmdline)
1394 term_printf(cmdline);
1398 static void term_insert_char(int ch)
1400 if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
1401 memmove(term_cmd_buf + term_cmd_buf_index + 1,
1402 term_cmd_buf + term_cmd_buf_index,
1403 term_cmd_buf_size - term_cmd_buf_index);
1404 term_cmd_buf[term_cmd_buf_index] = ch;
1405 term_cmd_buf_size++;
1406 term_printf("\033[@%c", ch);
1407 term_cmd_buf_index++;
1412 static void term_backward_char(void)
1414 if (term_cmd_buf_index > 0) {
1415 term_cmd_buf_index--;
1416 term_printf("\033[D");
1421 static void term_forward_char(void)
1423 if (term_cmd_buf_index < term_cmd_buf_size) {
1424 term_cmd_buf_index++;
1425 term_printf("\033[C");
1430 static void term_delete_char(void)
1432 if (term_cmd_buf_index < term_cmd_buf_size) {
1433 memmove(term_cmd_buf + term_cmd_buf_index,
1434 term_cmd_buf + term_cmd_buf_index + 1,
1435 term_cmd_buf_size - term_cmd_buf_index - 1);
1436 term_printf("\033[P");
1437 term_cmd_buf_size--;
1442 static void term_backspace(void)
1444 if (term_cmd_buf_index > 0) {
1445 term_backward_char();
1450 static void term_bol(void)
1452 while (term_cmd_buf_index > 0)
1453 term_backward_char();
1456 static void term_eol(void)
1458 while (term_cmd_buf_index < term_cmd_buf_size)
1459 term_forward_char();
1462 static void term_up_char(void)
1466 if (term_hist_entry == 0)
1468 if (term_hist_entry == -1) {
1469 /* Find latest entry */
1470 for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1471 if (term_history[idx] == NULL)
1474 term_hist_entry = idx;
1477 if (term_hist_entry >= 0) {
1478 strcpy(term_cmd_buf, term_history[term_hist_entry]);
1480 term_print_cmdline(term_cmd_buf);
1481 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1485 static void term_down_char(void)
1487 if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1)
1489 if (term_history[++term_hist_entry] != NULL) {
1490 strcpy(term_cmd_buf, term_history[term_hist_entry]);
1492 term_hist_entry = -1;
1495 term_print_cmdline(term_cmd_buf);
1496 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1499 static void term_hist_add(const char *cmdline)
1501 char *hist_entry, *new_entry;
1504 if (cmdline[0] == '\0')
1507 if (term_hist_entry != -1) {
1508 /* We were editing an existing history entry: replace it */
1509 hist_entry = term_history[term_hist_entry];
1510 idx = term_hist_entry;
1511 if (strcmp(hist_entry, cmdline) == 0) {
1515 /* Search cmdline in history buffers */
1516 for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1517 hist_entry = term_history[idx];
1518 if (hist_entry == NULL)
1520 if (strcmp(hist_entry, cmdline) == 0) {
1522 new_entry = hist_entry;
1523 /* Put this entry at the end of history */
1524 memmove(&term_history[idx], &term_history[idx + 1],
1525 &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
1526 term_history[TERM_MAX_CMDS - 1] = NULL;
1527 for (; idx < TERM_MAX_CMDS; idx++) {
1528 if (term_history[idx] == NULL)
1534 if (idx == TERM_MAX_CMDS) {
1535 /* Need to get one free slot */
1536 free(term_history[0]);
1537 memcpy(term_history, &term_history[1],
1538 &term_history[TERM_MAX_CMDS] - &term_history[1]);
1539 term_history[TERM_MAX_CMDS - 1] = NULL;
1540 idx = TERM_MAX_CMDS - 1;
1542 if (new_entry == NULL)
1543 new_entry = strdup(cmdline);
1544 term_history[idx] = new_entry;
1545 term_hist_entry = -1;
1548 /* return true if command handled */
1549 static void term_handle_byte(int ch)
1551 switch(term_esc_state) {
1562 term_cmd_buf[term_cmd_buf_size] = '\0';
1563 term_hist_add(term_cmd_buf);
1565 term_handle_command(term_cmd_buf);
1569 term_esc_state = IS_ESC;
1576 term_esc_state = IS_CSI;
1580 term_insert_char(ch);
1587 term_esc_state = IS_CSI;
1590 term_esc_state = IS_NORM;
1604 term_backward_char();
1607 term_forward_char();
1610 term_esc_param = term_esc_param * 10 + (ch - '0');
1613 switch(term_esc_param) {
1628 term_esc_state = IS_NORM;
1634 /*************************************************************/
1635 /* serial console support */
1637 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1639 static int term_got_escape, term_command;
1641 void term_print_help(void)
1644 "C-a h print this help\n"
1645 "C-a x exit emulatior\n"
1646 "C-a s save disk data back to file (if -snapshot)\n"
1647 "C-a b send break (magic sysrq)\n"
1648 "C-a c switch between console and monitor\n"
1649 "C-a C-a send C-a\n"
1653 /* called when a char is received */
1654 static void term_received_byte(int ch)
1656 if (!serial_console) {
1657 /* if no serial console, handle every command */
1658 term_handle_byte(ch);
1660 if (term_got_escape) {
1661 term_got_escape = 0;
1672 for (i = 0; i < MAX_DISKS; i++) {
1674 bdrv_commit(bs_table[i]);
1680 serial_receive_break(serial_console);
1683 if (!term_command) {
1693 } else if (ch == TERM_ESCAPE) {
1694 term_got_escape = 1;
1698 term_handle_byte(ch);
1701 serial_receive_byte(serial_console, ch);
1707 static int term_can_read(void *opaque)
1709 if (serial_console) {
1710 return serial_can_receive(serial_console);
1716 static void term_read(void *opaque, const uint8_t *buf, int size)
1719 for(i = 0; i < size; i++)
1720 term_received_byte(buf[i]);
1723 void monitor_init(void)
1725 if (!serial_console) {
1726 term_printf("QEMU %s monitor - type 'help' for more information\n",
1730 term_hist_entry = -1;
1731 qemu_add_fd_read_handler(0, term_can_read, term_read, NULL);