set PC if a symbol at pc reg is set
[fw/sdcc] / debugger / mcs51 / sdcdb.c
index 267dc7f201201a1e3a7a88c835bbb3ea185bf965..1cc14082404448aece81fd3514c134efe379ed36 100644 (file)
 #include "cmd.h"
 #include "newalloc.h"
 
+#ifdef SDCDB_DEBUG
+int   sdcdbDebug = 0;
+#endif
+
 char *currModName = NULL;
 cdbrecs *recsRoot = NULL ;
 set  *modules = NULL;    /* set of all modules */
 set  *functions = NULL ; /* set of functions */
 set  *symbols = NULL   ; /* set of symbols */
+set  *sfrsymbols= NULL ; /* set of symbols of sfr or sbit */
 int nStructs = 0 ;
 structdef **structs = NULL ; /* all structures */
 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" ;
+short showfull = 0;
+char userinterrupt = 0;
+char nointerrupt = 0;
+char contsim = 0;
+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;
 int lineno = 0;
 int fatalError = 0;
 
+static void commandLoop(FILE *cmdfile);
+
 /* command table */
 struct cmdtab
 {
@@ -61,10 +71,16 @@ 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 | *<address>]\n",
+    },
+    { "tbreak"   ,  cmdSetTmpUserBp ,
+      "tbreak\t\t\t [LINE | FILE:LINE | FILE:FUNCTION | FUNCTION | *<address>]\n",
     },
     { "b"        ,  cmdSetUserBp  , NULL },
 
+    { "jump"   ,  cmdJump ,
+      "jump\t\t\tContinue program being debugged at specified line or address\n [LINE | FILE:LINE | *<address>]\n",
+    },
     { "clear"    ,  cmdClrUserBp  ,
       "{cl}ear\t\t\t [LINE | FILE:LINE | FILE:FUNCTION | FUNCTION]\n"
     },
@@ -73,21 +89,37 @@ struct cmdtab
     { "continue" ,  cmdContinue   ,
       "{c}ontinue\t\t Continue program being debugged, after breakpoint.\n"
     },
+    { "condition" ,  cmdCondition   ,
+      "condition brkpoint_number expr\t\tSet condition for breakpoint.\n"
+    },
+    { "ignore" ,  cmdIgnore  ,
+      "brkpoint_number count\t\tSet ignore count for breakpoint.\n"
+    },
+    { "commands" ,  cmdCommands  ,
+      "commands [brkpoint_number]\t\tSetting commands for breakpoint.\n"
+    },
     { "c"        ,  cmdContinue   , NULL },
 
+    { "disassemble",cmdDisasmF    , "disassemble [startaddr [endaddress]]\tdisassemble asm commands\n" },
     { "delete" ,  cmdDelUserBp  ,
       "{d}elete n\t\t clears break point number n\n"
     },
-    { "d"        ,  cmdDelUserBp  , NULL },
-
-    { "help"     ,  cmdHelp       ,
-      "{h|?}elp\t\t this message\n"
+    { "display"    ,  cmdDisplay     ,
+      "display [/<fmt>] [<variable>]\t print value of given variable each time the program stops\n"
     },
-    { "?"        ,  cmdHelp       , NULL },
-    { "h"        ,  cmdHelp       , NULL },
+    { "undisplay"  ,  cmdUnDisplay   ,
+      "undisplay [<variable>]\t dont display this variable or all\n"
+    },
+    { "down"     ,  cmdDown      ,
+      "down\t\tSelect and print stack frame called by this one.\nAn argument says how many frames down to go.\n"
+    },
+    { "up"       ,  cmdUp      ,
+      "up\t\tSelect and print stack frame that called this one.\nAn argument says how many frames up to go.\n"
+    },
+    { "d"        ,  cmdDelUserBp  , NULL },
 
     { "info"     ,  cmdInfo       ,
-      "info <break stack frame registers>\n"
+      "info <break stack frame registers all-registers>\n"
       "\t list all break points, call-stack, frame or register information\n"
     },
 
@@ -106,45 +138,65 @@ struct cmdtab
       "show"
       " <copying warranty>\t copying & distribution terms, warranty\n"
     },
-    { "set"      ,  cmdSetOption  , "set <srcmode>\t\t toggle between c/asm.\n" },
+    { "set"      ,  cmdSetOption  , "set <srcmode>\t\t toggle between c/asm.\nset variable <var> = >value\t\tset variable to new value\n" },
+    { "stepi"    ,  cmdStepi      ,
+      "stepi\t\t\tStep one instruction exactly.\n"
+    },
     { "step"     ,  cmdStep       ,
-      "{s}tep\t\t\t Step program until it reaches a different source line.\n"
+      "{s}tep\t\t\tStep program until it reaches a different source line.\n"
+    },
+    { "source"   ,  cmdSource      ,
+      "source <FILE>\t\t\tRead commands from a file named FILE.\n"
     },
     { "s"        ,  cmdStep       , NULL },
+    { "nexti"    ,  cmdNexti      ,
+      "nexti\t\t\tStep one instruction, but proceed through subroutine calls.\n"
+    },
     { "next"     ,  cmdNext       ,
-      "{n}ext\t\t\t Step program, proceeding through subroutine calls.\n"
+      "{n}ext\t\t\tStep program, proceeding through subroutine calls.\n"
     },
     { "n"        ,  cmdNext       , NULL },
     { "run"      ,  cmdRun        ,
-      "{r}un\t\t\t Start debugged program. \n"
+      "{r}un\t\t\tStart debugged program. \n"
     },
     { "r"        ,  cmdRun        , NULL },
     { "ptype"    ,  cmdPrintType  ,
-      "{pt}ype <variable>\t print type information of a variable\n"
+      "{pt}ype <variable>\tprint type information of a variable\n"
     },
     { "pt"       ,  cmdPrintType  , NULL },
     { "print"    ,  cmdPrint      ,
       "{p}rint <variable>\t print value of given variable\n"
     },
+    { "output"   ,  cmdOutput      ,
+      "output <variable>\t print value of given variable without $ and newline \n"
+    },
     { "p"        ,  cmdPrint      , NULL },
     { "file"     ,  cmdFile       ,
       "file <filename>\t\t load symbolic information from <filename>\n"
     },
     { "frame"    ,  cmdFrame      ,
-      "{fr}ame\t\t\t print information about the current Stack\n"
+      "{fr}ame\t\t print information about the current Stack\n"
     },
     { "finish"   ,  cmdFinish     ,
       "{fi}nish\t\t execute till return of current function\n"
     },
     { "fi"       ,  cmdFinish     , NULL },
+    { "where"    ,  cmdWhere      , "where\t\t print stack\n" },
     { "fr"       ,  cmdFrame      , NULL },
     { "f"        ,  cmdFrame      , NULL },
+    { "x /i"     ,  cmdDisasm1    , "x\t\t disassemble one asm command\n" },
     { "!"        ,  cmdSimulator  ,
       "!<simulator command>\t send a command directly to the simulator\n"
     },
     { "."        ,  cmdSimulator  ,
       ".{cmd}\t switch from simulator or debugger command mode\n"
     },
+    { "help"     ,  cmdHelp       ,
+      "{h|?}elp\t [CMD_NAME | 0,1,2,3(help page)] (general help or specific help)\n"
+    },
+    { "?"        ,  cmdHelp       , NULL },
+    { "h"        ,  cmdHelp       , NULL },
+
     { "quit"     ,  cmdQuit       ,
       "{q}uit\t\t\t \"Watch me now. Iam going Down. My name is Bobby Brown\"\n"
     },
@@ -407,7 +459,7 @@ static void loadModules ()
 
   /* if symbol then parse the symbol */
   case  SYM_REC:
-      parseSymbol(loop->line,&rs);
+      parseSymbol(loop->line,&rs,2);
       break;
 
   case LNK_REC:
@@ -417,6 +469,23 @@ static void loadModules ()
     }
 }
 
+/*-----------------------------------------------------------------*/
+/* generate extra sets of sfr and sbit symbols                     */
+/*-----------------------------------------------------------------*/
+static void specialFunctionRegs ()
+{
+    symbol *sym;
+    for (sym = setFirstItem(symbols);
+         sym ;
+         sym = setNextItem(symbols))
+    {
+        if ( sym->addrspace == 'I' ||
+             sym->addrspace == 'J')
+        {
+            addSet(&sfrsymbols,sym);
+        }
+    }
+}
 /*-----------------------------------------------------------------*/
 /* functionPoints - determine the execution points within a func   */
 /*-----------------------------------------------------------------*/
@@ -424,6 +493,7 @@ static void functionPoints ()
 {
     function *func;
     symbol *sym;
+    exePoint *ep ;
 
     /* for all functions do */
     for ( func = setFirstItem(functions); func;
@@ -433,7 +503,7 @@ static void functionPoints ()
 
   sym = func->sym;
 
-  Dprintf(D_sdcdb, ("func '%s' has entry '%x' exit '%x'\n",
+  Dprintf(D_sdcdb, ("sdcdb: func '%s' has entry '0x%x' exit '0x%x'\n",
          func->sym->name,
          func->sym->addr,
          func->sym->eaddr));
@@ -458,7 +528,6 @@ static void functionPoints ()
       if (mod->cLines[j]->addr >= sym->addr &&
     mod->cLines[j]->addr <= sym->eaddr ) {
 
-    exePoint *ep ;
 
     /* add it to the execution point */
     if (func->entryline > j)
@@ -475,7 +544,16 @@ static void functionPoints ()
     addSet(&func->cfpoints,ep);
       }
   }
-
+  /* check double line execution points of module */
+  for (ep = setFirstItem(mod->cfpoints); ep;
+       ep = setNextItem(mod->cfpoints))
+  {
+      if (ep->addr >= sym->addr &&
+          ep->addr <= sym->eaddr ) 
+      {
+          addSet(&func->cfpoints,ep);
+      }
+  }
   /* do the same for asm execution points */
   for ( j = 0 ; j < mod->nasmLines ; j++ ) {
       if (mod->asmLines[j]->addr >= sym->addr &&
@@ -498,21 +576,24 @@ static void functionPoints ()
   }
 
 #ifdef SDCDB_DEBUG
-  Dprintf(D_sdcdb, ("function '%s' has the following C exePoints\n",
+  if (!( D_sdcdb & sdcdbDebug))
+      continue;
+
+  Dprintf(D_sdcdb, ("sdcdb: function '%s' has the following C exePoints\n",
          func->sym->name));
   {
       exePoint *ep;
 
       for (ep = setFirstItem(func->cfpoints); ep;
      ep = setNextItem(func->cfpoints))
-     Dprintf(D_sdcdb, ("{%x,%d} %s",
-         ep->addr,ep->line,mod->cLines[ep->line]->src));
+     Dprintf(D_sdcdb, ("sdcdb: {0x%x,%d} %s",
+         ep->addr,ep->line+1,mod->cLines[ep->line]->src));
 
-      Dprintf(D_sdcdb, (" and the following ASM exePoints\n"));
+      Dprintf(D_sdcdb, ("sdcdb:  and the following ASM exePoints\n"));
       for (ep = setFirstItem(func->afpoints); ep;
            ep = setNextItem(func->afpoints))
-        Dprintf (D_sdcdb, ("{%x,%d} %s",
-            ep->addr,ep->line,mod->asmLines[ep->line]->src));
+        Dprintf (D_sdcdb, ("sdcdb: {0x%x,%d} %s",
+            ep->addr,ep->line+1,mod->asmLines[ep->line]->src));
   }
 #endif
     }
@@ -580,8 +661,12 @@ int cmdFile (char *s,context *cctxt)
        module */
     functionPoints();
 
+    /* extract known special function registers */
+    specialFunctionRegs();
+
     /* start the simulator & setup connection to it */
-    openSimulator((char **)simArgs,nsimArgs);
+    if ( sock == -1 )
+        openSimulator((char **)simArgs,nsimArgs);
     fprintf(stdout,"%s",simResponse());
     /* now send the filename to be loaded to the simulator */
     sprintf(buffer,"%s.ihx",s);
@@ -594,7 +679,31 @@ int cmdFile (char *s,context *cctxt)
        and function exit break points */
     applyToSet(functions,setEntryExitBP);
 
-    /* ad we are done */
+    setMainContext();
+    return 0;
+}
+
+/*-----------------------------------------------------------------*/
+/* cmdSource - read commands from file                             */
+/*-----------------------------------------------------------------*/
+int cmdSource (char *s, context *cctxt)
+{
+    FILE *cmdfile;
+    char *bp = s+strlen(s) -1;
+
+    while (isspace(*s))
+      ++s;
+
+    while (isspace(*bp)) bp--;
+    *++bp = '\0';
+
+    if (!( cmdfile = searchDirsFopen(s)))
+    {
+        fprintf(stderr,"commandfile '%s' not found\n",s);
+        return 0;
+    }
+    commandLoop( cmdfile );
+    fclose( cmdfile );
     return 0;
 }
 
@@ -614,6 +723,23 @@ int cmdHelp (char *s, context *cctxt)
       if (endline > 0)
         startline = endline - 20;
     }
+    else if (*s)
+    {
+        for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++) 
+        {
+            if ((cmdTab[i].htxt) && !strcmp(cmdTab[i].cmd,s))
+            {
+                s = strrchr(cmdTab[i].htxt,'\t');
+                if ( !s )
+                    s = cmdTab[i].htxt;
+                else
+                    s++;
+                fprintf(stdout,"%s",s);
+                break;
+            }
+        }
+        return 0;
+    }
 
     for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++) {
 
@@ -693,15 +819,17 @@ int interpretCmd (char *s)
         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 (fullname && showfull && currCtxt && currCtxt->func) {
+          showfull = 0;
           if (srcMode == SRC_CMODE)
-            fprintf(stdout,"\032\032%s:%d:1\n",
+            fprintf(stdout,"\032\032%s:%d:1:beg:0x%08x\n",
                     currCtxt->func->mod->cfullname,
-                    currCtxt->cline+1);
+                    currCtxt->cline+1,currCtxt->addr);
           else
-            fprintf(stdout,"\032\032%s:%d:1\n",
+            fprintf(stdout,"\032\032%s:%d:1:beg:0x%08x\n",
                     currCtxt->func->mod->afullname,
-                    currCtxt->asmline+1);
+                    currCtxt->asmline,currCtxt->addr);
+          displayAll(currCtxt);
         }
         goto ret;
       }
@@ -712,37 +840,92 @@ int interpretCmd (char *s)
     return rv;
 }
 
+static FILE *actualcmdfile=NULL ;
+static char *actualcmds=NULL;
+static int   stopcmdlist;
 /*-----------------------------------------------------------------*/
-/* commandLoop - the main command loop                             */
+/* getNextCmdLine get additional lines used by special commands    */
 /*-----------------------------------------------------------------*/
-void commandLoop()
+char *getNextCmdLine()
 {
- char *prompt = "(sdcdb) ";
- char *sim_prompt = "(sim) ";
+    //fprintf(stderr,"getNextCmdLine() actualcmdfile=%p\n",actualcmdfile);
+    if (!actualcmdfile)
+        return NULL;
+    fprintf(stdout,">");
+    fflush(stdout);
+    if (fgets(cmdbuff,sizeof(cmdbuff),actualcmdfile) == NULL)
+    {
+        // fprintf(stderr,"getNextCmdLine() returns null\n");
+        return NULL;
+    }
+    //fprintf(stderr,"getNextCmdLine() returns: %s",cmdbuff);
+    return cmdbuff;
+}
 
-  while (1) {
-    if (sim_cmd_mode)
-      printf("%s",sim_prompt);
-    else
-      fprintf(stdout,"%s",prompt);
+void setCmdLine( char *cmds )
+{
+    actualcmds = cmds;
+}
 
-    fflush(stdout);
+void stopCommandList()
+{
+    stopcmdlist = 1;
+}
 
-    if (fgets(cmdbuff,sizeof(cmdbuff),stdin) == NULL)
-      break;
+/*-----------------------------------------------------------------*/
+/* commandLoop - the main command loop or loop over command file   */
+/*-----------------------------------------------------------------*/
+static void commandLoop(FILE *cmdfile)
+{
+    char *line, save_ch, *s;
+    actualcmdfile = cmdfile;
+    while (1) 
+    {
+        if ( cmdfile == stdin )
+        {
+            if (sim_cmd_mode)
+                printf("(sim) ");
+            else
+                fprintf(stdout,"(sdcdb) ");        
+            fflush(stdout);
+        }
 
-#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;
+        //fprintf(stderr,"commandLoop actualcmdfile=%p cmdfile=%p\n",
+        //        actualcmdfile,cmdfile);
+        if (fgets(cmdbuff,sizeof(cmdbuff),cmdfile) == NULL)
+            break;
+
+        if (interpretCmd(cmdbuff))
+            break;
+
+        while ( actualcmds )
+        {
+            strcpy(cmdbuff,actualcmds);
+            actualcmds = NULL;
+            stopcmdlist= 0;
+            for ( line = cmdbuff; *line ; line = s )
+            {
+                if ( (s=strchr(line ,'\n')))
+                {
+                    save_ch = *++s;
+                    *s = '\0';
+                }
+                else
+                {
+                    s += strlen( line );
+                    save_ch = '\0';
+                }
+                if (interpretCmd( line ))
+                {
+                    *s = save_ch;
+                    break;
+                }
+                *s = save_ch;
+                if ( stopcmdlist )
+                    break;
+            }
+        }
     }
-#endif
-
-    if (interpretCmd(cmdbuff))
-      break;
-  }
 }
 
 /*-----------------------------------------------------------------*/
@@ -768,7 +951,8 @@ static void parseCmdLine (int argc, char **argv)
     char *filename = NULL;
     int passon_args_flag = 0;  /* if true, pass on args to simulator */
 
-    Dprintf(D_sdcdb, ("parseCmdLine\n"));
+    Dprintf(D_sdcdb, ("sdcdb: parseCmdLine\n"));
+    contsim=0;
 
     for ( i = 1; i < argc ; i++) {
   //fprintf(stdout,"%s\n",argv[i]);
@@ -809,6 +993,20 @@ static void parseCmdLine (int argc, char **argv)
     continue;
       }
 
+#ifdef SDCDB_DEBUG
+      if (strncmp(argv[i],"-d=",3) == 0) {
+          sdcdbDebug = strtol(&argv[i][3],0,0);
+          continue;
+      }
+#endif
+      if (strncmp(argv[i],"-contsim",8) == 0) {
+          contsim=1;
+          continue;
+      }
+      if (strncmp(argv[i],"-q",2) == 0) {
+          continue;
+      }
+
       /* model string */
       if (strncmp(argv[i],"-m",2) == 0) {
         strncpy(model_str, &argv[i][2], 15);
@@ -840,7 +1038,7 @@ static void parseCmdLine (int argc, char **argv)
 
       /* XTAL Frequency */
       if (strcmp(argv[i],"-X") == 0 ||
-    strcmp(argv[i],"-frequency") == 0) {
+          strcmp(argv[i],"-frequency") == 0) {
         simArgs[nsimArgs++] = "-X";
         simArgs[nsimArgs++] = strdup(argv[++i]);
         continue ;
@@ -849,7 +1047,14 @@ static void parseCmdLine (int argc, char **argv)
       /* serial port */
       if ( (strcmp(argv[i],"-S") == 0) ||
            (strcmp(argv[i],"-s") == 0)) {
-        simArgs[nsimArgs++] = "-s";
+        simArgs[nsimArgs++] = strdup(argv[i]);
+        simArgs[nsimArgs++] = strdup(argv[++i]);
+        continue ;
+      }
+
+      /* network serial port */
+      if ( (strcmp(argv[i],"-k") == 0)) {
+        simArgs[nsimArgs++] = strdup(argv[i]);
         simArgs[nsimArgs++] = strdup(argv[++i]);
         continue ;
       }
@@ -874,6 +1079,55 @@ static void parseCmdLine (int argc, char **argv)
   cmdFile(filename,NULL);
 }
 
+/*-----------------------------------------------------------------*/
+/* setsignals -  catch some signals                                */
+/*-----------------------------------------------------------------*/
+#include <signal.h>
+static void
+bad_signal(int sig)
+{
+    if ( simactive )
+        closeSimulator();
+    exit(1);
+}
+
+static void
+sigintr(int sig)
+{
+    /* may be interrupt from user: stop debugger and also simulator */
+    userinterrupt = 1;
+    if ( !nointerrupt )
+        sendSim("stop\n");
+}
+
+/* the only child can be the simulator */
+static void sigchld(int sig)
+{
+    /* the only child can be the simulator */
+    int status, retpid;
+    retpid = wait ( &status );
+    /* if ( retpid == simPid ) */
+    simactive = 0;
+}
+
+static void
+setsignals()
+{
+    signal(SIGHUP , SIG_IGN);          
+    signal(SIGCONT, SIG_IGN);          
+    signal(SIGINT , sigintr ); 
+    signal(SIGTERM, bad_signal);       
+    signal(SIGCHLD, sigchld );
+
+    signal(SIGABRT, bad_signal);
+    signal(SIGALRM, bad_signal);
+    //signal(SIGFPE,  bad_signal);
+    //signal(SIGILL,  bad_signal);
+    signal(SIGPIPE, bad_signal);
+    signal(SIGQUIT, bad_signal);
+    //signal(SIGSEGV, bad_signal);
+}
+
 /*-----------------------------------------------------------------*/
 /* main -                                                          */
 /*-----------------------------------------------------------------*/
@@ -881,16 +1135,17 @@ 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");
+    printf("WARNING: SDCDB is EXPERIMENTAL.\n");
 
     simArgs[nsimArgs++] = "s51";
     simArgs[nsimArgs++] = "-P";
     simArgs[nsimArgs++] = "-r 9756";
     /* parse command line */
 
+    setsignals();
     parseCmdLine(argc,argv);
 
-    commandLoop();
+    commandLoop(stdin);
 
     return 0;
 }