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;
353 /* we use the current CS size */
354 if (!(cpu_single_env->segs[R_CS].flags & DESC_B_MASK))
357 monitor_disas(addr, count, is_physical, flags);
366 nb_per_line = line_size / wsize;
371 max_digits = (wsize * 8 + 2) / 3;
375 max_digits = (wsize * 8) / 4;
379 max_digits = (wsize * 8 * 10 + 32) / 33;
387 term_printf("0x%08x:", addr);
392 cpu_physical_memory_rw(addr, buf, l, 0);
394 cpu_memory_rw_debug(cpu_single_env, addr, buf, l, 0);
401 v = ldub_raw(buf + i);
404 v = lduw_raw(buf + i);
407 v = ldl_raw(buf + i);
410 v = ldq_raw(buf + i);
416 term_printf("%#*llo", max_digits, v);
419 term_printf("0x%0*llx", max_digits, v);
422 term_printf("%*llu", max_digits, v);
425 term_printf("%*lld", max_digits, v);
439 static void do_memory_dump(int count, int format, int size, int addr)
441 memory_dump(count, format, size, addr, 0);
444 static void do_physical_memory_dump(int count, int format, int size, int addr)
446 memory_dump(count, format, size, addr, 1);
449 static void do_print(int count, int format, int size, int val)
453 term_printf("%#o", val);
456 term_printf("%#x", val);
459 term_printf("%u", val);
463 term_printf("%d", val);
472 static term_cmd_t term_cmds[] = {
473 { "help|?", "s?", do_help,
474 "[cmd]", "show the help" },
475 { "commit", "", do_commit,
476 "", "commit changes to the disk images (if -snapshot is used)" },
477 { "info", "s?", do_info,
478 "subcommand", "show various information about the system state" },
479 { "q|quit", "", do_quit,
480 "", "quit the emulator" },
481 { "eject", "-fs", do_eject,
482 "[-f] device", "eject a removable media (use -f to force it)" },
483 { "change", "sF", do_change,
484 "device filename", "change a removable media" },
485 { "screendump", "F", do_screen_dump,
486 "filename", "save screen into PPM image 'filename'" },
487 { "log", "s", do_log,
488 "item1[,...]", "activate logging of the specified items to '/tmp/qemu.log'" },
489 { "savevm", "F", do_savevm,
490 "filename", "save the whole virtual machine state to 'filename'" },
491 { "loadvm", "F", do_loadvm,
492 "filename", "restore the whole virtual machine state from 'filename'" },
493 { "stop", "", do_stop,
494 "", "stop emulation", },
495 { "c|cont", "", do_cont,
496 "", "resume emulation", },
497 #ifdef CONFIG_GDBSTUB
498 { "gdbserver", "i?", do_gdbserver,
499 "[port]", "start gdbserver session (default port=1234)", },
501 { "x", "/i", do_memory_dump,
502 "/fmt addr", "virtual memory dump starting at 'addr'", },
503 { "xp", "/i", do_physical_memory_dump,
504 "/fmt addr", "physical memory dump starting at 'addr'", },
505 { "p|print", "/i", do_print,
506 "/fmt expr", "print expression value (use $reg for CPU register access)", },
510 static term_cmd_t info_cmds[] = {
511 { "network", "", do_info_network,
512 "", "show the network state" },
513 { "block", "", do_info_block,
514 "", "show the block devices" },
515 { "registers", "", do_info_registers,
516 "", "show the cpu registers" },
517 { "history", "", do_info_history,
518 "", "show the command line history", },
522 /*******************************************************************/
524 static const char *pch;
525 static jmp_buf expr_env;
527 typedef struct MonitorDef {
530 int (*get_value)(struct MonitorDef *md);
533 static MonitorDef monitor_defs[] = {
535 { "eax", offsetof(CPUState, regs[0]) },
536 { "ecx", offsetof(CPUState, regs[1]) },
537 { "edx", offsetof(CPUState, regs[2]) },
538 { "ebx", offsetof(CPUState, regs[3]) },
539 { "esp|sp", offsetof(CPUState, regs[4]) },
540 { "ebp|fp", offsetof(CPUState, regs[5]) },
541 { "esi", offsetof(CPUState, regs[6]) },
542 { "esi", offsetof(CPUState, regs[7]) },
543 { "eflags", offsetof(CPUState, eflags) },
544 { "eip|pc", offsetof(CPUState, eip) },
549 static void expr_error(const char *fmt)
553 longjmp(expr_env, 1);
556 static int get_monitor_def(int *pval, const char *name)
559 for(md = monitor_defs; md->name != NULL; md++) {
560 if (compare_cmd(name, md->name)) {
562 *pval = md->get_value(md);
564 *pval = *(uint32_t *)((uint8_t *)cpu_single_env + md->offset);
572 static void next(void)
576 while (isspace(*pch))
581 static int expr_sum(void);
583 static int expr_unary(void)
605 expr_error("')' expected");
615 while ((*pch >= 'a' && *pch <= 'z') ||
616 (*pch >= 'A' && *pch <= 'Z') ||
617 (*pch >= '0' && *pch <= '9') ||
619 if ((q - buf) < sizeof(buf) - 1)
623 while (isspace(*pch))
626 if (get_monitor_def(&n, buf))
627 expr_error("unknown register");
631 expr_error("unexpected end of expression");
635 n = strtoul(pch, &p, 0);
637 expr_error("invalid char in expression");
640 while (isspace(*pch))
648 static int expr_prod(void)
655 if (op != '*' && op != '/' && op != '%')
667 expr_error("divison by zero");
678 static int expr_logic(void)
685 if (op != '&' && op != '|' && op != '^')
705 static int expr_sum(void)
712 if (op != '+' && op != '-')
724 static int get_expr(int *pval, const char **pp)
727 if (setjmp(expr_env)) {
731 while (isspace(*pch))
738 static int get_str(char *buf, int buf_size, const char **pp)
755 while (*p != '\0' && *p != '\"') {
771 qemu_printf("unsupported escape code: '\\%c'\n", c);
774 if ((q - buf) < buf_size - 1) {
778 if ((q - buf) < buf_size - 1) {
785 qemu_printf("untermintated string\n");
790 while (*p != '\0' && !isspace(*p)) {
791 if ((q - buf) < buf_size - 1) {
802 static int default_fmt_format = 'x';
803 static int default_fmt_size = 4;
807 static void term_handle_command(const char *cmdline)
809 const char *p, *pstart, *typestr;
811 int c, nb_args, len, i, has_arg;
815 void *str_allocated[MAX_ARGS];
816 void *args[MAX_ARGS];
819 term_printf("command='%s'\n", cmdline);
822 /* extract the command name */
830 while (*p != '\0' && *p != '/' && !isspace(*p))
833 if (len > sizeof(cmdname) - 1)
834 len = sizeof(cmdname) - 1;
835 memcpy(cmdname, pstart, len);
838 /* find the command */
839 for(cmd = term_cmds; cmd->name != NULL; cmd++) {
840 if (compare_cmd(cmdname, cmd->name))
843 term_printf("unknown command: '%s'\n", cmdname);
847 for(i = 0; i < MAX_ARGS; i++)
848 str_allocated[i] = NULL;
850 /* parse the parameters */
851 typestr = cmd->args_type;
867 if (*typestr == '?') {
870 /* no optional string: NULL argument */
875 ret = get_str(buf, sizeof(buf), &p);
878 term_printf("%s: filename expected\n", cmdname);
880 term_printf("%s: string expected\n", cmdname);
883 str = qemu_malloc(strlen(buf) + 1);
885 str_allocated[nb_args] = str;
887 if (nb_args >= MAX_ARGS) {
889 term_printf("%s: too many arguments\n", cmdname);
892 args[nb_args++] = str;
897 int count, format, size;
907 while (isdigit(*p)) {
908 count = count * 10 + (*p - '0');
946 if (*p != '\0' && !isspace(*p)) {
947 term_printf("invalid char in format: '%c'\n", *p);
951 size = default_fmt_size;
953 format = default_fmt_format;
954 default_fmt_size = size;
955 default_fmt_format = format;
958 format = default_fmt_format;
959 size = default_fmt_size;
961 if (nb_args + 3 > MAX_ARGS)
963 args[nb_args++] = (void*)count;
964 args[nb_args++] = (void*)format;
965 args[nb_args++] = (void*)size;
973 if (*typestr == '?') {
979 if (nb_args >= MAX_ARGS)
981 args[nb_args++] = (void *)has_arg;
983 if (nb_args >= MAX_ARGS)
989 if (get_expr(&val, &p))
992 if (nb_args >= MAX_ARGS)
994 args[nb_args++] = (void *)val;
1011 term_printf("%s: unsupported option -%c\n",
1018 if (nb_args >= MAX_ARGS)
1020 args[nb_args++] = (void *)has_option;
1025 term_printf("%s: unknown type '%c'\n", cmdname, c);
1029 /* check that all arguments were parsed */
1033 term_printf("%s: extraneous characters at the end of line\n",
1043 cmd->handler(args[0]);
1046 cmd->handler(args[0], args[1]);
1049 cmd->handler(args[0], args[1], args[2]);
1052 cmd->handler(args[0], args[1], args[2], args[3]);
1055 cmd->handler(args[0], args[1], args[2], args[3], args[4]);
1058 term_printf("unsupported number of arguments: %d\n", nb_args);
1062 for(i = 0; i < MAX_ARGS; i++)
1063 qemu_free(str_allocated[i]);
1067 static void term_show_prompt(void)
1069 term_printf("(qemu) ");
1071 term_cmd_buf_index = 0;
1072 term_cmd_buf_size = 0;
1073 term_esc_state = IS_NORM;
1076 static void term_print_cmdline (const char *cmdline)
1079 term_printf(cmdline);
1083 static void term_insert_char(int ch)
1085 if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
1086 memmove(term_cmd_buf + term_cmd_buf_index + 1,
1087 term_cmd_buf + term_cmd_buf_index,
1088 term_cmd_buf_size - term_cmd_buf_index);
1089 term_cmd_buf[term_cmd_buf_index] = ch;
1090 term_cmd_buf_size++;
1091 term_printf("\033[@%c", ch);
1092 term_cmd_buf_index++;
1097 static void term_backward_char(void)
1099 if (term_cmd_buf_index > 0) {
1100 term_cmd_buf_index--;
1101 term_printf("\033[D");
1106 static void term_forward_char(void)
1108 if (term_cmd_buf_index < term_cmd_buf_size) {
1109 term_cmd_buf_index++;
1110 term_printf("\033[C");
1115 static void term_delete_char(void)
1117 if (term_cmd_buf_index < term_cmd_buf_size) {
1118 memmove(term_cmd_buf + term_cmd_buf_index,
1119 term_cmd_buf + term_cmd_buf_index + 1,
1120 term_cmd_buf_size - term_cmd_buf_index - 1);
1121 term_printf("\033[P");
1122 term_cmd_buf_size--;
1127 static void term_backspace(void)
1129 if (term_cmd_buf_index > 0) {
1130 term_backward_char();
1135 static void term_bol(void)
1137 while (term_cmd_buf_index > 0)
1138 term_backward_char();
1141 static void term_eol(void)
1143 while (term_cmd_buf_index < term_cmd_buf_size)
1144 term_forward_char();
1147 static void term_up_char(void)
1151 if (term_hist_entry == 0)
1153 if (term_hist_entry == -1) {
1154 /* Find latest entry */
1155 for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1156 if (term_history[idx] == NULL)
1159 term_hist_entry = idx;
1162 if (term_hist_entry >= 0) {
1163 strcpy(term_cmd_buf, term_history[term_hist_entry]);
1165 term_print_cmdline(term_cmd_buf);
1166 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1170 static void term_down_char(void)
1172 if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1)
1174 if (term_history[++term_hist_entry] != NULL) {
1175 strcpy(term_cmd_buf, term_history[term_hist_entry]);
1177 term_hist_entry = -1;
1180 term_print_cmdline(term_cmd_buf);
1181 term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf);
1184 static void term_hist_add(const char *cmdline)
1186 char *hist_entry, *new_entry;
1189 if (cmdline[0] == '\0')
1192 if (term_hist_entry != -1) {
1193 /* We were editing an existing history entry: replace it */
1194 hist_entry = term_history[term_hist_entry];
1195 idx = term_hist_entry;
1196 if (strcmp(hist_entry, cmdline) == 0) {
1200 /* Search cmdline in history buffers */
1201 for (idx = 0; idx < TERM_MAX_CMDS; idx++) {
1202 hist_entry = term_history[idx];
1203 if (hist_entry == NULL)
1205 if (strcmp(hist_entry, cmdline) == 0) {
1207 new_entry = hist_entry;
1208 /* Put this entry at the end of history */
1209 memmove(&term_history[idx], &term_history[idx + 1],
1210 &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
1211 term_history[TERM_MAX_CMDS - 1] = NULL;
1212 for (; idx < TERM_MAX_CMDS; idx++) {
1213 if (term_history[idx] == NULL)
1219 if (idx == TERM_MAX_CMDS) {
1220 /* Need to get one free slot */
1221 free(term_history[0]);
1222 memcpy(term_history, &term_history[1],
1223 &term_history[TERM_MAX_CMDS] - &term_history[1]);
1224 term_history[TERM_MAX_CMDS - 1] = NULL;
1225 idx = TERM_MAX_CMDS - 1;
1227 if (new_entry == NULL)
1228 new_entry = strdup(cmdline);
1229 term_history[idx] = new_entry;
1230 term_hist_entry = -1;
1233 /* return true if command handled */
1234 static void term_handle_byte(int ch)
1236 switch(term_esc_state) {
1247 term_cmd_buf[term_cmd_buf_size] = '\0';
1248 term_hist_add(term_cmd_buf);
1250 term_handle_command(term_cmd_buf);
1254 term_esc_state = IS_ESC;
1261 term_esc_state = IS_CSI;
1265 term_insert_char(ch);
1272 term_esc_state = IS_CSI;
1275 term_esc_state = IS_NORM;
1289 term_backward_char();
1292 term_forward_char();
1295 term_esc_param = term_esc_param * 10 + (ch - '0');
1298 switch(term_esc_param) {
1313 term_esc_state = IS_NORM;
1319 /*************************************************************/
1320 /* serial console support */
1322 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1324 static int term_got_escape, term_command;
1326 void term_print_help(void)
1329 "C-a h print this help\n"
1330 "C-a x exit emulatior\n"
1331 "C-a s save disk data back to file (if -snapshot)\n"
1332 "C-a b send break (magic sysrq)\n"
1333 "C-a c switch between console and monitor\n"
1334 "C-a C-a send C-a\n"
1338 /* called when a char is received */
1339 static void term_received_byte(int ch)
1341 if (!serial_console) {
1342 /* if no serial console, handle every command */
1343 term_handle_byte(ch);
1345 if (term_got_escape) {
1346 term_got_escape = 0;
1357 for (i = 0; i < MAX_DISKS; i++) {
1359 bdrv_commit(bs_table[i]);
1365 serial_receive_break(serial_console);
1368 if (!term_command) {
1378 } else if (ch == TERM_ESCAPE) {
1379 term_got_escape = 1;
1383 term_handle_byte(ch);
1386 serial_receive_byte(serial_console, ch);
1392 static int term_can_read(void *opaque)
1394 if (serial_console) {
1395 return serial_can_receive(serial_console);
1401 static void term_read(void *opaque, const uint8_t *buf, int size)
1404 for(i = 0; i < size; i++)
1405 term_received_byte(buf[i]);
1408 void monitor_init(void)
1410 if (!serial_console) {
1411 term_printf("QEMU %s monitor - type 'help' for more information\n",
1415 term_hist_entry = -1;
1416 qemu_add_fd_read_handler(0, term_can_read, term_read, NULL);