/[gxemul]/trunk/src/debugger.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/src/debugger.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2 by dpavlin, Mon Oct 8 16:17:48 2007 UTC revision 10 by dpavlin, Mon Oct 8 16:18:27 2007 UTC
# Line 25  Line 25 
25   *  SUCH DAMAGE.   *  SUCH DAMAGE.
26   *   *
27   *   *
28   *  $Id: debugger.c,v 1.98 2005/03/20 20:27:26 debug Exp $   *  $Id: debugger.c,v 1.106 2005/06/26 11:36:27 debug Exp $
29   *   *
30   *  Single-step debugger.   *  Single-step debugger.
31   *   *
# Line 54  Line 54 
54  #include <string.h>  #include <string.h>
55  #include <unistd.h>  #include <unistd.h>
56    
57    #include "bintrans.h"
58  #include "console.h"  #include "console.h"
59  #include "cpu.h"  #include "cpu.h"
60  #include "device.h"  #include "device.h"
# Line 100  static struct machine *debugger_machine; Line 101  static struct machine *debugger_machine;
101  static int exit_debugger;  static int exit_debugger;
102  static int n_steps_left_before_interaction = 0;  static int n_steps_left_before_interaction = 0;
103    
104  #define MAX_CMD_LEN             70  #define MAX_CMD_BUFLEN          72
105  #define N_PREVIOUS_CMDS         150  #define N_PREVIOUS_CMDS         150
106  static char *last_cmd[N_PREVIOUS_CMDS];  static char *last_cmd[N_PREVIOUS_CMDS];
107  static int last_cmd_index;  static int last_cmd_index;
108    
109  static char repeat_cmd[MAX_CMD_LEN + 1];  static char repeat_cmd[MAX_CMD_BUFLEN];
110    
111  #define MAGIC_UNTOUCHED         0x98ca76c2ffcc0011ULL  #define MAGIC_UNTOUCHED         0x98ca76c2ffcc0011ULL
112    
# Line 127  void debugger_activate(int x) Line 128  void debugger_activate(int x)
128          if (single_step) {          if (single_step) {
129                  /*  Already in the debugger. Do nothing.  */                  /*  Already in the debugger. Do nothing.  */
130                  int i;                  int i;
131                  for (i=0; i<MAX_CMD_LEN+1; i++)                  for (i=0; i<MAX_CMD_BUFLEN; i++)
132                          console_makeavail(MAIN_CONSOLE, '\b');                          console_makeavail(MAIN_CONSOLE, '\b');
133                  console_makeavail(MAIN_CONSOLE, ' ');                  console_makeavail(MAIN_CONSOLE, ' ');
134                  console_makeavail(MAIN_CONSOLE, '\n');                  console_makeavail(MAIN_CONSOLE, '\n');
# Line 233  static int debugger_parse_name(struct ma Line 234  static int debugger_parse_name(struct ma
234                          fprintf(stderr, "out of memory in debugger\n");                          fprintf(stderr, "out of memory in debugger\n");
235                          exit(1);                          exit(1);
236                  }                  }
237                  strcpy(sn, name);                  strlcpy(sn, name, strlen(name)+1);
238    
239                  /*  Is there a '+' in there? Then treat that as an offset:  */                  /*  Is there a '+' in there? Then treat that as an offset:  */
240                  p = strchr(sn, '+');                  p = strchr(sn, '+');
# Line 341  static void debugger_cmd_breakpoint(stru Line 342  static void debugger_cmd_breakpoint(stru
342    
343          if (strncmp(cmd_line, "add ", 4) == 0) {          if (strncmp(cmd_line, "add ", 4) == 0) {
344                  uint64_t tmp;                  uint64_t tmp;
345                    size_t breakpoint_buf_len;
346    
347                  if (m->n_breakpoints >= MAX_BREAKPOINTS) {                  if (m->n_breakpoints >= MAX_BREAKPOINTS) {
348                          printf("Too many breakpoints. (You need to recompile"                          printf("Too many breakpoints. (You need to recompile"
# Line 357  static void debugger_cmd_breakpoint(stru Line 359  static void debugger_cmd_breakpoint(stru
359                          return;                          return;
360                  }                  }
361    
362                  m->breakpoint_string[i] = malloc(strlen(cmd_line+4) + 1);                  breakpoint_buf_len = strlen(cmd_line+4) + 1;
363                    m->breakpoint_string[i] = malloc(breakpoint_buf_len);
364                  if (m->breakpoint_string[i] == NULL) {                  if (m->breakpoint_string[i] == NULL) {
365                          printf("out of memory in debugger_cmd_breakpoint()\n");                          printf("out of memory in debugger_cmd_breakpoint()\n");
366                          exit(1);                          exit(1);
367                  }                  }
368                  strcpy(m->breakpoint_string[i], cmd_line+4);                  strlcpy(m->breakpoint_string[i], cmd_line+4,
369                        breakpoint_buf_len);
370                  m->breakpoint_addr[i] = tmp;                  m->breakpoint_addr[i] = tmp;
371                  m->breakpoint_flags[i] = 0;                  m->breakpoint_flags[i] = 0;
372    
# Line 380  static void debugger_cmd_breakpoint(stru Line 384  static void debugger_cmd_breakpoint(stru
384   */   */
385  static void debugger_cmd_bintrans(struct machine *m, char *cmd_line)  static void debugger_cmd_bintrans(struct machine *m, char *cmd_line)
386  {  {
387            int i;
388    
389          if (*cmd_line == '\0')          if (*cmd_line == '\0')
390                  goto printstate;                  goto printstate;
391    
# Line 393  static void debugger_cmd_bintrans(struct Line 399  static void debugger_cmd_bintrans(struct
399                  cmd_line++;                  cmd_line++;
400    
401          /*  Note: len 3 and 4, to include the NUL char.  */          /*  Note: len 3 and 4, to include the NUL char.  */
402          if (strncasecmp(cmd_line, "on", 3) == 0)          if (strncasecmp(cmd_line, "on", 3) == 0) {
403                  m->bintrans_enable = 1;                  m->bintrans_enable = 1;
404          else if (strncasecmp(cmd_line, "off", 4) == 0)                  for (i=0; i<m->ncpus; i++)
405                            bintrans_restart(m->cpus[i]);
406            } else if (strncasecmp(cmd_line, "off", 4) == 0)
407                  m->bintrans_enable = 0;                  m->bintrans_enable = 0;
408          else          else
409                  printf("syntax: bintrans [on|off]\n");                  printf("syntax: bintrans [on|off]\n");
# Line 653  static void debugger_cmd_dump(struct mac Line 661  static void debugger_cmd_dump(struct mac
661    
662          last_dump_addr = addr_end;          last_dump_addr = addr_end;
663    
664          strcpy(repeat_cmd, "dump");          strlcpy(repeat_cmd, "dump", MAX_CMD_BUFLEN);
665  }  }
666    
667    
# Line 899  static void debugger_cmd_print(struct ma Line 907  static void debugger_cmd_print(struct ma
907                  printf("Multiple matches. Try prefixing with %%, $, or @.\n");                  printf("Multiple matches. Try prefixing with %%, $, or @.\n");
908                  break;                  break;
909          case NAME_PARSE_REGISTER:          case NAME_PARSE_REGISTER:
910                    printf("%s = 0x%llx\n", cmd_line, (long long)tmp);
911                    break;
912          case NAME_PARSE_SYMBOL:          case NAME_PARSE_SYMBOL:
913                  printf("%s = 0x%016llx\n", cmd_line, (long long)tmp);                  printf("%s = 0x%016llx\n", cmd_line, (long long)tmp);
914                  break;                  break;
915          case NAME_PARSE_NUMBER:          case NAME_PARSE_NUMBER:
916                  printf("0x%016llx\n", (long long)tmp);                  printf("0x%llx\n", (long long)tmp);
917                  break;                  break;
918          }          }
919  }  }
# Line 1202  static void debugger_cmd_step(struct mac Line 1212  static void debugger_cmd_step(struct mac
1212          /*  Special hack, see debugger() for more info.  */          /*  Special hack, see debugger() for more info.  */
1213          exit_debugger = -1;          exit_debugger = -1;
1214    
1215          strcpy(repeat_cmd, "step");          strlcpy(repeat_cmd, "step", MAX_CMD_BUFLEN);
1216  }  }
1217    
1218    
# Line 1344  static void debugger_cmd_unassemble(stru Line 1354  static void debugger_cmd_unassemble(stru
1354    
1355          addr = addr_start;          addr = addr_start;
1356    
         if ((addr & 3) != 0)  
                 printf("WARNING! You entered an unaligned address.\n");  
   
1357          ctrl_c = 0;          ctrl_c = 0;
1358    
1359          while (addr < addr_end) {          while (addr < addr_end) {
1360                  int i, len;                  int i, len;
1361                  unsigned char buf[25];  /*  TODO: How long can an                  unsigned char buf[32];  /*  TODO: How long can an
1362                                              instruction be, on weird archs?  */                                              instruction be, on weird archs?  */
1363                  memset(buf, 0, sizeof(buf));                  memset(buf, 0, sizeof(buf));
1364    
# Line 1377  static void debugger_cmd_unassemble(stru Line 1384  static void debugger_cmd_unassemble(stru
1384    
1385          last_unasm_addr = addr;          last_unasm_addr = addr;
1386    
1387          strcpy(repeat_cmd, "unassemble");          strlcpy(repeat_cmd, "unassemble", MAX_CMD_BUFLEN);
1388  }  }
1389    
1390    
# Line 1494  static struct cmd cmds[] = { Line 1501  static struct cmd cmds[] = {
1501   *   *
1502   *  NOTE: This is placed after the cmds[] array, because it needs to   *  NOTE: This is placed after the cmds[] array, because it needs to
1503   *  access it.   *  access it.
1504     *
1505     *  TODO: Command completion (ie just type "help s" for "help step").
1506   */   */
1507  static void debugger_cmd_help(struct machine *m, char *cmd_line)  static void debugger_cmd_help(struct machine *m, char *cmd_line)
1508  {  {
1509          int i, j, max_name_len = 0;          int i, j, max_name_len = 0, only_one = 0, only_one_match = 0;
1510    
1511            if (cmd_line[0] != '\0') {
1512                    only_one = 1;
1513            }
1514    
1515          i = 0;          i = 0;
1516          while (cmds[i].name != NULL) {          while (cmds[i].name != NULL) {
# Line 1509  static void debugger_cmd_help(struct mac Line 1522  static void debugger_cmd_help(struct mac
1522                  i++;                  i++;
1523          }          }
1524    
1525          printf("Available commands:\n");          if (!only_one)
1526                    printf("Available commands:\n");
1527    
1528          i = 0;          i = 0;
1529          while (cmds[i].name != NULL) {          while (cmds[i].name != NULL) {
1530                  char buf[100];                  char buf[100];
1531                  snprintf(buf, sizeof(buf), "%s", cmds[i].name);                  snprintf(buf, sizeof(buf), "%s", cmds[i].name);
1532    
1533                    if (only_one) {
1534                            if (strcmp(cmds[i].name, cmd_line) != 0) {
1535                                    i++;
1536                                    continue;
1537                            }
1538                            only_one_match = 1;
1539                    }
1540    
1541                  if (cmds[i].args != NULL)                  if (cmds[i].args != NULL)
1542                          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),                          snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
1543                              " %s", cmds[i].args);                              " %s", cmds[i].args);
# Line 1530  static void debugger_cmd_help(struct mac Line 1553  static void debugger_cmd_help(struct mac
1553                  i++;                  i++;
1554          }          }
1555    
1556            if (only_one) {
1557                    if (!only_one_match)
1558                            printf("%s: no such command\n", cmd_line);
1559                    return;
1560            }
1561    
1562          printf("Generic assignments:   x = expr\n");          printf("Generic assignments:   x = expr\n");
1563          printf("where x must be a register, and expr can be a register, a "          printf("where x must be a register, and expr can be a register, a "
1564              "numeric value, or\na symbol name (+ an optional numeric offset)."              "numeric value, or\na symbol name (+ an optional numeric offset)."
# Line 1554  void debugger_assignment(struct machine Line 1583  void debugger_assignment(struct machine
1583          int res_left, res_right;          int res_left, res_right;
1584          uint64_t tmp;          uint64_t tmp;
1585    
1586          left  = malloc(strlen(cmd) + 1);          left  = malloc(MAX_CMD_BUFLEN);
1587          if (left == NULL) {          if (left == NULL) {
1588                  fprintf(stderr, "out of memory in debugger_assignment()\n");                  fprintf(stderr, "out of memory in debugger_assignment()\n");
1589                  exit(1);                  exit(1);
1590          }          }
1591          strcpy(left, cmd);          strlcpy(left, cmd, MAX_CMD_BUFLEN);
1592          right = strchr(left, '=');          right = strchr(left, '=');
1593          if (right == NULL) {          if (right == NULL) {
1594                  fprintf(stderr, "internal error in the debugger\n");                  fprintf(stderr, "internal error in the debugger\n");
# Line 1688  static char *debugger_readline(void) Line 1717  static char *debugger_readline(void)
1717                          }                          }
1718                  } else if (ch == 11) {                  } else if (ch == 11) {
1719                          /*  CTRL-K: Kill to end of line.  */                          /*  CTRL-K: Kill to end of line.  */
1720                          for (i=0; i<MAX_CMD_LEN; i++)                          for (i=0; i<MAX_CMD_BUFLEN; i++)
1721                                  console_makeavail(MAIN_CONSOLE, 4); /*  :-)  */                                  console_makeavail(MAIN_CONSOLE, 4); /*  :-)  */
1722                  } else if (ch == 14 || ch == 16) {                  } else if (ch == 14 || ch == 16) {
1723                          /*  CTRL-P: Previous line in the command history,                          /*  CTRL-P: Previous line in the command history,
# Line 1726  static char *debugger_readline(void) Line 1755  static char *debugger_readline(void)
1755                                                  printf(" ");                                                  printf(" ");
1756                                          for (i=cmd_len-1; i>=0; i--)                                          for (i=cmd_len-1; i>=0; i--)
1757                                                  printf("\b \b");                                                  printf("\b \b");
1758                                          strcpy(cmd,                                          strlcpy(cmd,
1759                                              last_cmd[read_from_index]);                                              last_cmd[read_from_index],
1760                                                MAX_CMD_BUFLEN);
1761                                          cmd_len = strlen(cmd);                                          cmd_len = strlen(cmd);
1762                                          printf("%s", cmd);                                          printf("%s", cmd);
1763                                          cursor_pos = cmd_len;                                          cursor_pos = cmd_len;
1764                                  }                                  }
1765                          } while (0);                          } while (0);
1766                  } else if (ch >= ' ' && cmd_len < MAX_CMD_LEN) {                  } else if (ch >= ' ' && cmd_len < MAX_CMD_BUFLEN-1) {
1767                          /*  Visible character:  */                          /*  Visible character:  */
1768                          memmove(cmd + cursor_pos + 1, cmd + cursor_pos,                          memmove(cmd + cursor_pos + 1, cmd + cursor_pos,
1769                              cmd_len - cursor_pos);                              cmd_len - cursor_pos);
# Line 1904  void debugger(void) Line 1934  void debugger(void)
1934                  if (cmd_len == 0) {                  if (cmd_len == 0) {
1935                          /*  Special case for repeated commands:  */                          /*  Special case for repeated commands:  */
1936                          if (repeat_cmd[0] != '\0')                          if (repeat_cmd[0] != '\0')
1937                                  strcpy(cmd, repeat_cmd);                                  strlcpy(cmd, repeat_cmd, MAX_CMD_BUFLEN);
1938                          else                          else
1939                                  continue;                                  continue;
1940                  } else {                  } else {
# Line 2001  void debugger(void) Line 2031  void debugger(void)
2031                          return;                          return;
2032          }          }
2033    
2034            gettimeofday(&debugger_machine->starttime, NULL);
2035            debugger_machine->ncycles_since_gettimeofday = 0;
2036    
2037          single_step = 0;          single_step = 0;
2038          debugger_machine->instruction_trace = old_instruction_trace;          debugger_machine->instruction_trace = old_instruction_trace;
2039          debugger_machine->show_trace_tree = old_show_trace_tree;          debugger_machine->show_trace_tree = old_show_trace_tree;
# Line 2049  void debugger_init(struct emul **emuls, Line 2082  void debugger_init(struct emul **emuls,
2082          debugger_machine = emuls[0]->machines[0];          debugger_machine = emuls[0]->machines[0];
2083    
2084          for (i=0; i<N_PREVIOUS_CMDS; i++) {          for (i=0; i<N_PREVIOUS_CMDS; i++) {
2085                  last_cmd[i] = malloc(MAX_CMD_LEN + 1);                  last_cmd[i] = malloc(MAX_CMD_BUFLEN);
2086                  if (last_cmd[i] == NULL) {                  if (last_cmd[i] == NULL) {
2087                          fprintf(stderr, "debugger_init(): out of memory\n");                          fprintf(stderr, "debugger_init(): out of memory\n");
2088                          exit(1);                          exit(1);

Legend:
Removed from v.2  
changed lines
  Added in v.10

  ViewVC Help
Powered by ViewVC 1.1.26