* configure.in, configure: support for winsock2
[fw/sdcc] / debugger / mcs51 / sdcdb.c
index cb241699d47bf3f729587bfb8ae24698ec9948e2..6c1d610c1c6956b2edc63175f6b656724e669a2a 100644 (file)
@@ -45,17 +45,20 @@ linkrec **linkrecs = NULL; /* all linkage editor records */
 context *currCtxt = NULL;
 short fullname = 0;
 short showfull = 0;
-short userinterrupt = 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
 {
@@ -70,8 +73,14 @@ struct cmdtab
     { "break"    ,  cmdSetUserBp  ,
       "{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"
     },
@@ -80,6 +89,15 @@ 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" },
@@ -127,6 +145,9 @@ struct cmdtab
     { "step"     ,  cmdStep       ,
       "{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"
@@ -377,18 +398,19 @@ srcLine **loadFile (char *name, int *nlines)
 
 
     if (!(mfile = searchDirsFopen(name))) {
-  fprintf(stderr,"sdcdb: cannot open module %s -- use '--directory=<source directory> option\n",name);
-  return NULL;
+        fprintf(stderr,"sdcdb: cannot open module %s -- use '--directory=<source directory> option\n",name);
+        return NULL;
     }
 
     while ((bp = fgets(buffer,sizeof(buffer),mfile))) {
-  (*nlines)++;
+        (*nlines)++;
 
-  slines = (srcLine **)resize((void **)slines,*nlines);
+        slines = (srcLine **)resize((void **)slines,*nlines);
 
-  slines[(*nlines)-1] = Safe_calloc(1,sizeof(srcLine));
-  slines[(*nlines)-1]->src = alloccpy(bp,strlen(bp));
-    }
+        slines[(*nlines)-1] = Safe_calloc(1,sizeof(srcLine));
+        slines[(*nlines)-1]->src = alloccpy(bp,strlen(bp));
+        slines[(*nlines)-1]->addr= INT_MAX;
+            }
 
     fclose(mfile);
     return slines;
@@ -438,7 +460,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:
@@ -472,99 +494,132 @@ static void functionPoints ()
 {
     function *func;
     symbol *sym;
-
-    /* for all functions do */
-    for ( func = setFirstItem(functions); func;
-    func = setNextItem(functions)) {
-  int j ;
-  module *mod;
-
-  sym = func->sym;
-
-  Dprintf(D_sdcdb, ("sdcdb: func '%s' has entry '0x%x' exit '0x%x'\n",
-         func->sym->name,
-         func->sym->addr,
-         func->sym->eaddr));
-
-  if (!func->sym->addr && !func->sym->eaddr)
-      continue ;
-
-  /* for all source lines in the module find
-     the ones with address >= start and <= end
-     and put them in the point */
-  mod = NULL ;
-  if (! applyToSet(modules,moduleWithName,func->modName,&mod))
-      continue ;
-  func->mod = mod ;
-  func->entryline= INT_MAX;
-  func->exitline =  0;
-  func->aentryline = INT_MAX ;
-  func->aexitline = 0;
-
-  /* do it for the C Lines first */
-  for ( j = 0 ; j < mod->ncLines ; j++ ) {
-      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)
-        func->entryline = j;
-
-    if (func->exitline < j)
-        func->exitline = j;
-
-    ep = Safe_calloc(1,sizeof(exePoint));
-    ep->addr =  mod->cLines[j]->addr ;
-    ep->line = j;
-    ep->block= mod->cLines[j]->block;
-    ep->level= mod->cLines[j]->level;
-    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 &&
-    mod->asmLines[j]->addr <= sym->eaddr ) {
+    // add _main dummy for runtime env
+    if ((func = needExtraMainFunction()))
+    {
+        function *func1;
+
+        /* alloc new _main function */
+        func1 = Safe_calloc(1,sizeof(function));
+        *func1 = *func;
+        func1->sym = Safe_calloc(1,sizeof(symbol));
+        *func1->sym =  *func->sym;
+        func1->sym->name  = alloccpy("_main",5);
+        func1->sym->rname = alloccpy("G$_main$0$",10);
+        /* TODO must be set by symbol information */
+        func1->sym->addr  = 0;
+        func1->sym->eaddr = 0x2f;
+        addSet(&functions,func1);
+    }
 
-    exePoint *ep ;
-    /* add it to the execution point */
-    if (func->aentryline > j)
-        func->aentryline = j;
-
-    if (func->aexitline < j)
-        func->aexitline = j;
-
-    /* add it to the execution point */
-    ep = Safe_calloc(1,sizeof(exePoint));
-    ep->addr =  mod->asmLines[j]->addr ;
-    ep->line = j;
-    addSet(&func->afpoints,ep);
-      }
-  }
+    /* for all functions do */
+    for ( func = setFirstItem(functions); func;
+          func = setNextItem(functions)) {
+        int j ;
+        module *mod;
+
+        sym = func->sym;
+
+        Dprintf(D_sdcdb, ("sdcdb: func '%s' has entry '0x%x' exit '0x%x'\n",
+                          func->sym->name,
+                          func->sym->addr,
+                          func->sym->eaddr));
+
+        if (!func->sym->addr && !func->sym->eaddr)
+            continue ;
+
+        /* for all source lines in the module find
+           the ones with address >= start and <= end
+           and put them in the point */
+        mod = NULL ;
+        if (! applyToSet(modules,moduleWithName,func->modName,&mod))
+            continue ;
+        func->mod = mod ;
+        func->entryline= INT_MAX-2;
+        func->exitline =  0;
+        func->aentryline = INT_MAX-2 ;
+        func->aexitline = 0;
+
+        /* do it for the C Lines first */
+        for ( j = 0 ; j < mod->ncLines ; j++ ) {
+            if (mod->cLines[j]->addr < INT_MAX &&
+                mod->cLines[j]->addr >= sym->addr &&
+                mod->cLines[j]->addr <= sym->eaddr ) {
+
+
+                /* add it to the execution point */
+                if (func->entryline > j)
+                    func->entryline = j;
+
+                if (func->exitline < j)
+                    func->exitline = j;
+
+                ep = Safe_calloc(1,sizeof(exePoint));
+                ep->addr =  mod->cLines[j]->addr ;
+                ep->line = j;
+                ep->block= mod->cLines[j]->block;
+                ep->level= mod->cLines[j]->level;
+                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 < INT_MAX &&
+                mod->asmLines[j]->addr >= sym->addr &&
+                mod->asmLines[j]->addr <= sym->eaddr ) {
+
+                exePoint *ep ;
+                /* add it to the execution point */
+                if (func->aentryline > j)
+                    func->aentryline = j;
+
+                if (func->aexitline < j)
+                    func->aexitline = j;
+
+                /* add it to the execution point */
+                ep = Safe_calloc(1,sizeof(exePoint));
+                ep->addr =  mod->asmLines[j]->addr ;
+                ep->line = j;
+                addSet(&func->afpoints,ep);
+            }
+        }
+        if ( func->entryline == INT_MAX-2 )
+            func->entryline = 0;
+        if ( func->aentryline == INT_MAX-2 )
+            func->aentryline = 0;
 
 #ifdef SDCDB_DEBUG
-  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, ("sdcdb: {0x%x,%d} %s",
-         ep->addr,ep->line,mod->cLines[ep->line]->src));
-
-      Dprintf(D_sdcdb, ("sdcdb:  and the following ASM exePoints\n"));
-      for (ep = setFirstItem(func->afpoints); ep;
-           ep = setNextItem(func->afpoints))
-        Dprintf (D_sdcdb, ("sdcdb: {0x%x,%d} %s",
-            ep->addr,ep->line,mod->asmLines[ep->line]->src));
-  }
+        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, ("sdcdb: {0x%x,%d} %s",
+                                  ep->addr,ep->line+1,mod->cLines[ep->line]->src));
+
+            Dprintf(D_sdcdb, ("sdcdb:  and the following ASM exePoints\n"));
+            for (ep = setFirstItem(func->afpoints); ep;
+                 ep = setNextItem(func->afpoints))
+                Dprintf (D_sdcdb, ("sdcdb: {0x%x,%d} %s",
+                                   ep->addr,ep->line+1,mod->asmLines[ep->line]->src));
+        }
 #endif
     }
 }
@@ -649,7 +704,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;
 }
 
@@ -671,7 +750,7 @@ int cmdHelp (char *s, context *cctxt)
     }
     else if (*s)
     {
-        for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++) 
+        for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++)
         {
             if ((cmdTab[i].htxt) && !strcmp(cmdTab[i].cmd,s))
             {
@@ -690,7 +769,7 @@ int cmdHelp (char *s, context *cctxt)
     for (i = 0 ; i < (sizeof(cmdTab)/sizeof(struct cmdtab)) ; i++) {
 
       /* command string matches */
-      
+
       if ((cmdTab[i].htxt) && (i >= startline))
         fprintf(stdout,"%s",cmdTab[i].htxt);
       if (i == endline)
@@ -770,7 +849,7 @@ int interpretCmd (char *s)
           if (srcMode == SRC_CMODE)
             fprintf(stdout,"\032\032%s:%d:1:beg:0x%08x\n",
                     currCtxt->func->mod->cfullname,
-                    currCtxt->cline,currCtxt->addr);
+                    currCtxt->cline+1,currCtxt->addr);
           else
             fprintf(stdout,"\032\032%s:%d:1:beg:0x%08x\n",
                     currCtxt->func->mod->afullname,
@@ -786,37 +865,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;
-    }
-#endif
+        //fprintf(stderr,"commandLoop actualcmdfile=%p cmdfile=%p\n",
+        //        actualcmdfile,cmdfile);
+        if (fgets(cmdbuff,sizeof(cmdbuff),cmdfile) == NULL)
+            break;
 
-    if (interpretCmd(cmdbuff))
-      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;
+            }
+        }
+    }
 }
 
 /*-----------------------------------------------------------------*/
@@ -843,6 +977,7 @@ static void parseCmdLine (int argc, char **argv)
     int passon_args_flag = 0;  /* if true, pass on args to simulator */
 
     Dprintf(D_sdcdb, ("sdcdb: parseCmdLine\n"));
+    contsim=0;
 
     for ( i = 1; i < argc ; i++) {
   //fprintf(stdout,"%s\n",argv[i]);
@@ -889,6 +1024,13 @@ static void parseCmdLine (int argc, char **argv)
           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) {
@@ -921,7 +1063,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 ;
@@ -930,7 +1072,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 ;
       }
@@ -972,9 +1121,11 @@ sigintr(int sig)
 {
     /* may be interrupt from user: stop debugger and also simulator */
     userinterrupt = 1;
-    sendSim("\n");
+    if ( !nointerrupt )
+        sendSim("stop\n");
 }
 
+#ifndef _WIN32
 /* the only child can be the simulator */
 static void sigchld(int sig)
 {
@@ -984,22 +1135,27 @@ static void sigchld(int sig)
     /* if ( retpid == simPid ) */
     simactive = 0;
 }
+#endif
 
 static void
 setsignals()
 {
-    signal(SIGHUP , bad_signal);               
-    signal(SIGINT , sigintr ); 
-    signal(SIGTERM, bad_signal);       
+    signal(SIGINT , sigintr );
+    signal(SIGABRT, bad_signal);
+    signal(SIGTERM, bad_signal);
+
+#ifndef _WIN32
+    signal(SIGHUP , SIG_IGN);
+    signal(SIGCONT, SIG_IGN);
     signal(SIGCHLD, sigchld );
 
-    signal(SIGABRT, bad_signal);
     signal(SIGALRM, bad_signal);
-    signal(SIGFPE,  bad_signal);
-    signal(SIGILL,  bad_signal);
+    //signal(SIGFPE,  bad_signal);
+    //signal(SIGILL,  bad_signal);
     signal(SIGPIPE, bad_signal);
     signal(SIGQUIT, bad_signal);
-    signal(SIGSEGV, bad_signal);
+    //signal(SIGSEGV, bad_signal);
+#endif
 }
 
 /*-----------------------------------------------------------------*/
@@ -1019,7 +1175,7 @@ int main ( int argc, char **argv)
     setsignals();
     parseCmdLine(argc,argv);
 
-    commandLoop();
+    commandLoop(stdin);
 
     return 0;
 }