--- trunk/src/debugger.c 2007/10/08 16:18:22 9 +++ trunk/src/debugger.c 2007/10/08 16:18:27 10 @@ -25,7 +25,7 @@ * SUCH DAMAGE. * * - * $Id: debugger.c,v 1.103 2005/05/13 14:26:29 debug Exp $ + * $Id: debugger.c,v 1.106 2005/06/26 11:36:27 debug Exp $ * * Single-step debugger. * @@ -101,12 +101,12 @@ static int exit_debugger; static int n_steps_left_before_interaction = 0; -#define MAX_CMD_LEN 70 +#define MAX_CMD_BUFLEN 72 #define N_PREVIOUS_CMDS 150 static char *last_cmd[N_PREVIOUS_CMDS]; static int last_cmd_index; -static char repeat_cmd[MAX_CMD_LEN + 1]; +static char repeat_cmd[MAX_CMD_BUFLEN]; #define MAGIC_UNTOUCHED 0x98ca76c2ffcc0011ULL @@ -128,7 +128,7 @@ if (single_step) { /* Already in the debugger. Do nothing. */ int i; - for (i=0; in_breakpoints >= MAX_BREAKPOINTS) { printf("Too many breakpoints. (You need to recompile" @@ -358,12 +359,14 @@ return; } - m->breakpoint_string[i] = malloc(strlen(cmd_line+4) + 1); + breakpoint_buf_len = strlen(cmd_line+4) + 1; + m->breakpoint_string[i] = malloc(breakpoint_buf_len); if (m->breakpoint_string[i] == NULL) { printf("out of memory in debugger_cmd_breakpoint()\n"); exit(1); } - strcpy(m->breakpoint_string[i], cmd_line+4); + strlcpy(m->breakpoint_string[i], cmd_line+4, + breakpoint_buf_len); m->breakpoint_addr[i] = tmp; m->breakpoint_flags[i] = 0; @@ -658,7 +661,7 @@ last_dump_addr = addr_end; - strcpy(repeat_cmd, "dump"); + strlcpy(repeat_cmd, "dump", MAX_CMD_BUFLEN); } @@ -1209,7 +1212,7 @@ /* Special hack, see debugger() for more info. */ exit_debugger = -1; - strcpy(repeat_cmd, "step"); + strlcpy(repeat_cmd, "step", MAX_CMD_BUFLEN); } @@ -1381,7 +1384,7 @@ last_unasm_addr = addr; - strcpy(repeat_cmd, "unassemble"); + strlcpy(repeat_cmd, "unassemble", MAX_CMD_BUFLEN); } @@ -1498,10 +1501,16 @@ * * NOTE: This is placed after the cmds[] array, because it needs to * access it. + * + * TODO: Command completion (ie just type "help s" for "help step"). */ static void debugger_cmd_help(struct machine *m, char *cmd_line) { - int i, j, max_name_len = 0; + int i, j, max_name_len = 0, only_one = 0, only_one_match = 0; + + if (cmd_line[0] != '\0') { + only_one = 1; + } i = 0; while (cmds[i].name != NULL) { @@ -1513,12 +1522,22 @@ i++; } - printf("Available commands:\n"); + if (!only_one) + printf("Available commands:\n"); i = 0; while (cmds[i].name != NULL) { char buf[100]; snprintf(buf, sizeof(buf), "%s", cmds[i].name); + + if (only_one) { + if (strcmp(cmds[i].name, cmd_line) != 0) { + i++; + continue; + } + only_one_match = 1; + } + if (cmds[i].args != NULL) snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " %s", cmds[i].args); @@ -1534,6 +1553,12 @@ i++; } + if (only_one) { + if (!only_one_match) + printf("%s: no such command\n", cmd_line); + return; + } + printf("Generic assignments: x = expr\n"); printf("where x must be a register, and expr can be a register, a " "numeric value, or\na symbol name (+ an optional numeric offset)." @@ -1558,12 +1583,12 @@ int res_left, res_right; uint64_t tmp; - left = malloc(strlen(cmd) + 1); + left = malloc(MAX_CMD_BUFLEN); if (left == NULL) { fprintf(stderr, "out of memory in debugger_assignment()\n"); exit(1); } - strcpy(left, cmd); + strlcpy(left, cmd, MAX_CMD_BUFLEN); right = strchr(left, '='); if (right == NULL) { fprintf(stderr, "internal error in the debugger\n"); @@ -1692,7 +1717,7 @@ } } else if (ch == 11) { /* CTRL-K: Kill to end of line. */ - for (i=0; i=0; i--) printf("\b \b"); - strcpy(cmd, - last_cmd[read_from_index]); + strlcpy(cmd, + last_cmd[read_from_index], + MAX_CMD_BUFLEN); cmd_len = strlen(cmd); printf("%s", cmd); cursor_pos = cmd_len; } } while (0); - } else if (ch >= ' ' && cmd_len < MAX_CMD_LEN) { + } else if (ch >= ' ' && cmd_len < MAX_CMD_BUFLEN-1) { /* Visible character: */ memmove(cmd + cursor_pos + 1, cmd + cursor_pos, cmd_len - cursor_pos); @@ -1908,7 +1934,7 @@ if (cmd_len == 0) { /* Special case for repeated commands: */ if (repeat_cmd[0] != '\0') - strcpy(cmd, repeat_cmd); + strlcpy(cmd, repeat_cmd, MAX_CMD_BUFLEN); else continue; } else { @@ -2005,6 +2031,9 @@ return; } + gettimeofday(&debugger_machine->starttime, NULL); + debugger_machine->ncycles_since_gettimeofday = 0; + single_step = 0; debugger_machine->instruction_trace = old_instruction_trace; debugger_machine->show_trace_tree = old_show_trace_tree; @@ -2053,7 +2082,7 @@ debugger_machine = emuls[0]->machines[0]; for (i=0; i