* src/SDCCutil.c (pathEquivalent): defined but not used
[fw/sdcc] / debugger / mcs51 / sdcdb.c
index 020814529a1f0eac5c8110ee514e4a4f96faa5b8..53a1a38cf253044b872a34211ed4e932213a4a65 100644 (file)
@@ -26,6 +26,7 @@
 #include "simi.h"
 #include "break.h"
 #include "cmd.h"
+#include "newalloc.h"
 
 char *currModName = NULL;
 cdbrecs *recsRoot = NULL ;
@@ -38,9 +39,10 @@ int nLinkrecs = 0;
 linkrec **linkrecs = NULL; /* all linkage editor records */
 context *currCtxt = NULL;
 short fullname = 0;
-char *ssdirl = SDCC_LIB_DIR ":" SDCC_LIB_DIR "/small" ;
-char *simArgs[8];
+char *ssdirl = DATADIR LIB_DIR_SUFFIX ":" DATADIR LIB_DIR_SUFFIX "/small" ;
+char *simArgs[40];
 int nsimArgs = 0;
+char model_str[20];
 
 /* fake filename & lineno to make linker */
 char *filename=NULL;
@@ -59,7 +61,7 @@ struct cmdtab
        precede the synonym "b" */
     /* break point */
     { "break"    ,  cmdSetUserBp  ,
-      "{b}reak\t\t\t [LINE |  FILE:LINE | FILE:FUNCTION | FUNCTION]\n",
+      "{b}reak\t\t\t [LINE | FILE:LINE | FILE:FUNCTION | FUNCTION]\n",
     },
     { "b"        ,  cmdSetUserBp  , NULL },
 
@@ -85,27 +87,26 @@ struct cmdtab
     { "h"        ,  cmdHelp       , NULL },
 
     { "info"     ,  cmdInfo       ,
-      "info\n"
-      "\t {break}\t list all break points\n"
-      "\t {stack}\t information about call stack\n"
-      "\t {frame}\t current frame information\n"
-      "\t {registers}\t display value of all registers\n"
+      "info <break stack frame registers>\n"
+      "\t list all break points, call-stack, frame or register information\n"
     },
 
     { "listasm"  ,  cmdListAsm    ,
       "listasm {la}\t\t list assembler code for the current C line\n"
     },
     { "la"       ,  cmdListAsm    , NULL },
+    { "ls"       ,  cmdListSymbols  , "ls,lf,lm\t\t list symbols,functions,modules\n" },
+    { "lf"       ,  cmdListFunctions, NULL },
+    { "lm"       ,  cmdListModules  , NULL },
     { "list"     ,  cmdListSrc    ,
       "{l}ist\t\t\t [LINE | FILE:LINE | FILE:FUNCTION | FUNCTION]\n"
     },
     { "l"        ,  cmdListSrc    , NULL },
     { "show"     ,  cmdShow       ,
-      "show\n"
-      "\t {copying}\t copying & distribution terms\n"
-      "\t {warranty}\t warranty information\n"
+      "show"
+      " <copying warranty>\t copying & distribution terms, warranty\n"
     },
-    { "set"      ,  cmdSetOption  , NULL },
+    { "set"      ,  cmdSetOption  , "set <srcmode>\t\t toggle between c/asm.\n" },
     { "step"     ,  cmdStep       ,
       "{s}tep\t\t\t Step program until it reaches a different source line.\n"
     },
@@ -141,6 +142,9 @@ struct cmdtab
     { "!"        ,  cmdSimulator  ,
       "!<simulator command>\t send a command directly to the simulator\n"
     },
+    { "."        ,  cmdSimulator  ,
+      ".{cmd}\t switch from simulator or debugger command mode\n"
+    },
     { "quit"     ,  cmdQuit       ,
       "{q}uit\t\t\t \"Watch me now. Iam going Down. My name is Bobby Brown\"\n"
     },
@@ -153,7 +157,7 @@ struct cmdtab
 char *gc_strdup(const char *s)
 {
     char *ret;
-    Safe_calloc(1,ret, strlen(s)+1);
+    ret = Safe_malloc(strlen(s)+1);
     strcpy(ret, s);
     return ret;
 }
@@ -168,7 +172,7 @@ char *alloccpy ( char *s, int size)
     if (!size)
   return NULL;
 
-    Safe_calloc(1,d,size+1);
+    d = Safe_malloc(size+1);
     memcpy(d,s,size);
     d[size] = '\0';
 
@@ -209,7 +213,7 @@ static int readCdb (FILE *file)
     if (!(bp = fgets(buffer,sizeof(buffer),file)))
       return 0;
 
-    Safe_calloc(1,currl,sizeof(cdbrecs));
+    currl = Safe_calloc(1,sizeof(cdbrecs));
     recsRoot = currl ;
 
     while (1) {
@@ -237,7 +241,7 @@ static int readCdb (FILE *file)
       }
 
       bp += 2;
-      Safe_calloc(1,currl->line,strlen(bp));
+      currl->line = Safe_malloc(strlen(bp));
       strncpy(currl->line,bp,strlen(bp)-1);
       currl->line[strlen(bp)-1] = '\0';
   }
@@ -248,7 +252,7 @@ static int readCdb (FILE *file)
   if (feof(file))
       break;
 
-  Safe_calloc(1,currl->next,sizeof(cdbrecs));
+  currl->next = Safe_calloc(1,sizeof(cdbrecs));
   currl = currl->next;
     }
 
@@ -351,7 +355,7 @@ srcLine **loadFile (char *name, int *nlines)
 
   slines = (srcLine **)resize((void **)slines,*nlines);
 
-  Safe_calloc(1,slines[(*nlines)-1],sizeof(srcLine));
+  slines[(*nlines)-1] = Safe_calloc(1,sizeof(srcLine));
   slines[(*nlines)-1]->src = alloccpy(bp,strlen(bp));
     }
 
@@ -429,12 +433,11 @@ static void functionPoints ()
 
   sym = func->sym;
 
-#ifdef SDCDB_DEBUG
-  printf("func '%s' has entry '%x' exit '%x'\n",
+  Dprintf(D_sdcdb, ("func '%s' has entry '%x' exit '%x'\n",
          func->sym->name,
          func->sym->addr,
-         func->sym->eaddr);
-#endif
+         func->sym->eaddr));
+
   if (!func->sym->addr && !func->sym->eaddr)
       continue ;
 
@@ -464,7 +467,7 @@ static void functionPoints ()
     if (func->exitline < j)
         func->exitline = j;
 
-    Safe_calloc(1,ep,sizeof(exePoint));
+    ep = Safe_calloc(1,sizeof(exePoint));
     ep->addr =  mod->cLines[j]->addr ;
     ep->line = j;
     ep->block= mod->cLines[j]->block;
@@ -487,7 +490,7 @@ static void functionPoints ()
         func->aexitline = j;
 
     /* add it to the execution point */
-    Safe_calloc(1,ep,sizeof(exePoint));
+    ep = Safe_calloc(1,sizeof(exePoint));
     ep->addr =  mod->asmLines[j]->addr ;
     ep->line = j;
     addSet(&func->afpoints,ep);
@@ -495,20 +498,21 @@ static void functionPoints ()
   }
 
 #ifdef SDCDB_DEBUG
-  printf("function '%s' has the following C exePoints\n",
-         func->sym->name);
+  Dprintf(D_sdcdb, ("function '%s' has the following C exePoints\n",
+         func->sym->name));
   {
       exePoint *ep;
 
       for (ep = setFirstItem(func->cfpoints); ep;
      ep = setNextItem(func->cfpoints))
-    fprintf (stdout,"{%x,%d} %s",ep->addr,ep->line,mod->cLines[ep->line]->src);
+     Dprintf(D_sdcdb, ("{%x,%d} %s",
+         ep->addr,ep->line,mod->cLines[ep->line]->src));
 
-      fprintf(stdout," and the following ASM exePoints\n");
+      Dprintf(D_sdcdb, (" and the following ASM exePoints\n"));
       for (ep = setFirstItem(func->afpoints); ep;
-     ep = setNextItem(func->afpoints))
-    fprintf (stdout,"{%x,%d} %s",ep->addr,ep->line,mod->asmLines[ep->line]->src);
-
+           ep = setNextItem(func->afpoints))
+        Dprintf (D_sdcdb, ("{%x,%d} %s",
+            ep->addr,ep->line,mod->asmLines[ep->line]->src));
   }
 #endif
     }
@@ -554,17 +558,19 @@ int cmdFile (char *s,context *cctxt)
     sprintf(buffer,"%s.cdb",s);
     /* try creating the cdbfile */
     if (!(cdbFile = searchDirsFopen(buffer))) {
-  fprintf(stdout,"Cannot open file\"%s\"",buffer);
-  return 0;
+      fprintf(stdout,"Cannot open file\"%s\", no symbolic information loaded\n",buffer);
+      // return 0;
     }
 
     /* allocate for context */
-    Safe_calloc(1,currCtxt ,sizeof(context));
+    currCtxt = Safe_calloc(1,sizeof(context));
 
-    /* readin the debug information */
-    if (!readCdb (cdbFile)) {
-  fprintf(stdout,"No symbolic information found in file %s.cdb\n",s);
-  return 0;
+    if (cdbFile) {
+      /* readin the debug information */
+      if (!readCdb (cdbFile)) {
+        fprintf(stdout,"No symbolic information found in file %s.cdb\n",s);
+        //return 0;
+      }
     }
 
     /* parse and load the modules required */
@@ -598,67 +604,107 @@ int cmdFile (char *s,context *cctxt)
 int cmdHelp (char *s, context *cctxt)
 {
     int i ;
+    int endline = 999;
+    int startline = 0;
+
+    while (isspace(*s))
+      ++s;
+    if (isdigit(*s)) {
+      endline = ((*s - '0') * 20) + 20;
+      if (endline > 0)
+        startline = endline - 20;
+    }
 
     for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++) {
 
-  /* command string matches */
-  if (cmdTab[i].htxt)
-      fprintf(stdout,"%s",cmdTab[i].htxt);
+      /* command string matches */
+      
+      if ((cmdTab[i].htxt) && (i >= startline))
+        fprintf(stdout,"%s",cmdTab[i].htxt);
+      if (i == endline)
+        break;
     }
 
     return 0;
 }
 
 #define MAX_CMD_LEN 512
-char *prompt = "(sdcdb) ";
-char cmdbuff[MAX_CMD_LEN];
+static char cmdbuff[MAX_CMD_LEN];
+static int sim_cmd_mode = 0;
 
-/*-----------------------------------------------------------------*/
-/* interpretCmd - interpret and do the command                     */
-/*-----------------------------------------------------------------*/
+/*-----------------------------------------------------------------
+ interpretCmd - interpret and do the command.  Return 0 to continue,
+   return 1 to exit program.
+|-----------------------------------------------------------------*/
 int interpretCmd (char *s)
 {
     static char *pcmd = NULL;
     int i ;
     int rv = 0 ;
+
     /* if nothing & previous command exists then
        execute the previous command again */
     if (*s == '\n' && pcmd)
-  strcpy(s,pcmd);
+      strcpy(s,pcmd);
+
     /* if previous command exists & is different
        from the current command then copy it */
     if (pcmd) {
-  if (strcmp(pcmd,s)) {
-      free(pcmd);
-      pcmd = strdup(s);
-  }
+      if (strcmp(pcmd,s)) {
+         free(pcmd);
+         pcmd = strdup(s);
+      }
     } else
-  pcmd = strdup(s);
-    /* lookup the command table and do the
-       task required */
+      pcmd = strdup(s);
+
+    /* lookup the command table and do the task required */
     strtok(s,"\n");
 
+    if (sim_cmd_mode) {
+      if (strcmp(s,".") == 0) {
+        sim_cmd_mode = 0;
+        return 0;
+      }
+      else if (s[0] == '.') {
+        /* kill the preceeding '.' and pass on as SDCDB command */
+        char *s1 = s+1;
+        char *s2 = s;
+        while (*s1 != 0)
+          *s2++ = *s1++;
+        *s2 = 0;
+      } else {
+        cmdSimulator (s, currCtxt);
+        return 0;
+      }
+    } else {
+      if (strcmp(s,".") ==0) {
+        sim_cmd_mode = 1;
+        return 0;
+      }
+    }
+
     for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++) {
 
-  /* command string matches */
-  if (strncmp(s,cmdTab[i].cmd,strlen(cmdTab[i].cmd)) == 0) {
-      if (!cmdTab[i].cmdfunc)
-    return 1;
-      rv = (*cmdTab[i].cmdfunc)(s + strlen(cmdTab[i].cmd),currCtxt);
-
-      /* if full name then give the file name & position */
-      if (fullname && currCtxt && currCtxt->func) {
-    if (srcMode == SRC_CMODE)
-        fprintf(stdout,"\032\032%s:%d:1\n",
-          currCtxt->func->mod->cfullname,
-          currCtxt->cline+1);
-    else
-        fprintf(stdout,"\032\032%s:%d:1\n",
-          currCtxt->func->mod->afullname,
-          currCtxt->asmline+1);
+      /* command string matches */
+      if (strncmp(s,cmdTab[i].cmd,strlen(cmdTab[i].cmd)) == 0) {
+        if (!cmdTab[i].cmdfunc)
+          return 1;
+
+        rv = (*cmdTab[i].cmdfunc)(s + strlen(cmdTab[i].cmd),currCtxt);
+
+        /* if full name then give the file name & position */
+        if (fullname && currCtxt && currCtxt->func) {
+          if (srcMode == SRC_CMODE)
+            fprintf(stdout,"\032\032%s:%d:1\n",
+                    currCtxt->func->mod->cfullname,
+                    currCtxt->cline+1);
+          else
+            fprintf(stdout,"\032\032%s:%d:1\n",
+                    currCtxt->func->mod->afullname,
+                    currCtxt->asmline+1);
+        }
+        goto ret;
       }
-      goto ret;
-  }
     }
 
     fprintf(stdout,"Undefined command: \"%s\".  Try \"help\".\n",s);
@@ -671,18 +717,32 @@ int interpretCmd (char *s)
 /*-----------------------------------------------------------------*/
 void commandLoop()
 {
+ char *prompt = "(sdcdb) ";
+ char *sim_prompt = "(sim) ";
 
-    while (1) {
-  fprintf(stdout,"%s",prompt);
-  fflush(stdout);
+  while (1) {
+    if (sim_cmd_mode)
+      printf("%s",sim_prompt);
+    else
+      fprintf(stdout,"%s",prompt);
 
-  if (fgets(cmdbuff,sizeof(cmdbuff),stdin) == NULL)
-      break;
+    fflush(stdout);
 
-  if (interpretCmd(cmdbuff))
+    if (fgets(cmdbuff,sizeof(cmdbuff),stdin) == NULL)
       break;
 
+#if 0
+    /* make a way to go into "ucSim" mode */
+    if (cmdbuff[0] == '$') {
+      if (sim_cmd_mode) sim_cmd_mode = 0;
+      else sim_cmd_mode = 1;
+      continue;
     }
+#endif
+
+    if (interpretCmd(cmdbuff))
+      break;
+  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -706,9 +766,17 @@ static void parseCmdLine (int argc, char **argv)
 {
     int i ;
     char *filename = NULL;
-    char buffer[100];
+    int passon_args_flag = 0;  /* if true, pass on args to simulator */
+
+    Dprintf(D_sdcdb, ("parseCmdLine\n"));
+
     for ( i = 1; i < argc ; i++) {
-  fprintf(stdout,"%s\n",argv[i]);
+  //fprintf(stdout,"%s\n",argv[i]);
+
+  if (passon_args_flag) { /* if true, pass on args to simulator */
+    simArgs[nsimArgs++] = strdup(argv[i]);
+    continue;
+  }
 
   /* if this is an option */
   if (argv[i][0] == '-') {
@@ -741,6 +809,24 @@ static void parseCmdLine (int argc, char **argv)
     continue;
       }
 
+      /* model string */
+      if (strncmp(argv[i],"-m",2) == 0) {
+        strncpy(model_str, &argv[i][2], 15);
+        if (strcmp(model_str,"avr") == 0)
+          simArgs[0] = "savr";
+        else if (strcmp(model_str,"xa") == 0)
+          simArgs[0] = "sxa";
+        else if (strcmp(model_str,"z80") == 0)
+          simArgs[0] = "sz80";
+        continue ;
+      }
+
+      /* -z all remaining options are for simulator */
+      if (strcmp(argv[i],"-z") == 0) {
+        passon_args_flag = 1;
+        continue ;
+      }
+
       /* the simulator arguments */
 
       /* cpu */
@@ -761,13 +847,8 @@ static void parseCmdLine (int argc, char **argv)
       }
 
       /* serial port */
-      if (strcmp(argv[i],"-s") == 0) {
-        simArgs[nsimArgs++] = "-s";
-        simArgs[nsimArgs++] = strdup(argv[++i]);
-        continue ;
-      }
-
-      if (strcmp(argv[i],"-S") == 0) {
+      if ( (strcmp(argv[i],"-S") == 0) ||
+           (strcmp(argv[i],"-s") == 0)) {
         simArgs[nsimArgs++] = "-s";
         simArgs[nsimArgs++] = strdup(argv[++i]);
         continue ;
@@ -775,6 +856,7 @@ static void parseCmdLine (int argc, char **argv)
 
       fprintf(stderr,"unknown option %s --- ignored\n",
         argv[i]);
+
   } else {
       /* must be file name */
       if (filename) {
@@ -799,7 +881,13 @@ static void parseCmdLine (int argc, char **argv)
 int main ( int argc, char **argv)
 {
     printVersionInfo();
+    printf("WARNING: SDCDB is EXPERIMENTAL and NOT A FULLY FUNCTIONING TOOL.\n");
+
+    simArgs[nsimArgs++] = "s51";
+    simArgs[nsimArgs++] = "-P";
+    simArgs[nsimArgs++] = "-r 9756";
     /* parse command line */
+
     parseCmdLine(argc,argv);
 
     commandLoop();