home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / RUN.C < prev    next >
Text File  |  1996-02-05  |  18KB  |  444 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   run.c                                                                   */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*   execution driver function(s).                                           */
  7. /*                                                                           */
  8. /* History:                                                                  */
  9. /*                                                                           */
  10. /*...                                                                        */
  11. /*...Rewritten  11/21/93                                                     */
  12. /*... 03/29/94  917   Joe       Ctrl-Break handling for single process.      */
  13. /*...                                                                        */
  14. /**Includes*******************************************************************/
  15.  
  16. #include "all.h"
  17.  
  18. uchar msgbuf[80];
  19. int   AppTerminated;
  20.  
  21. /**Externs********************************************************************/
  22.  
  23. extern PtraceBuffer  AppPTB;
  24. extern CmdParms      cmd;
  25. extern PROCESS_NODE *pnode;
  26. extern int           WatchpointFlag;
  27.  
  28. AFILE  *fp_focus;
  29. AFILE  *Getfp_focus(void) { return(fp_focus); }
  30.  
  31. USHORT  DgroupDS;
  32. /*****************************************************************************/
  33. /* Run()                                                                     */
  34. /*                                                                           */
  35. /* Description:                                                              */
  36. /*                                                                           */
  37. /*  Run the application.                                                     */
  38. /*                                                                           */
  39. /* Parameters:                                                               */
  40. /*                                                                           */
  41. /*    MainEntryPt  address of "main"                                         */
  42. /*    ExeEntryPt   where the loader starts the EXE.                          */
  43. /*                                                                           */
  44. /* Return:                                                                   */
  45. /*                                                                           */
  46. /* Assumptions:                                                              */
  47. /*                                                                           */
  48. /*****************************************************************************/
  49. void Run( ULONG MainEntryPt , ULONG ExeEntryPt )
  50. {
  51.  uint     stopcode;
  52.  int      how;
  53.  BRK     *RestartBrks;
  54.  DEBFILE *pdf;
  55.  BRK     *pBrk;
  56.  BRK     *plast;
  57.  
  58.  /****************************************************************************/
  59.  /* - Save the current breakpoint ring.                                      */
  60.  /* - Init a ring for startup.                                               */
  61.  /****************************************************************************/
  62.  RestartBrks     = pnode->allbrks;
  63.  pnode->allbrks  = NULL;
  64.  
  65.  /****************************************************************************/
  66.  /* - Blow by main if that's the user's choice.                              */
  67.  /* - If the /i option was specified, then set breaks on the entry           */
  68.  /*   points of all currently attached executables.                          */
  69.  /* - Else, set a breakpoint on main.                                        */
  70.  /* - And if there is no "main", then stop at initial cs:eip.                */
  71.  /****************************************************************************/
  72.  if( cmd.NoStopAtMain == TRUE )
  73.  {;}
  74.  else if( cmd.ShowInit == TRUE )
  75.  {
  76.   for( pdf=pnode->ExeStruct; pdf!=NULL; pdf=pdf->next )
  77.   {
  78.    if( pdf->EntryExitPt != NULL )
  79.    {
  80.     pBrk                  = DefBrk( pdf->EntryExitPt, TRUE);
  81.     pBrk->flag.DorI       = BP_IMMEDIATE;
  82.     pBrk->flag.DefineType = BP_INTERNAL;
  83.    }
  84.   }
  85.  }
  86.  else if( MainEntryPt != NULL )
  87.  {
  88.   pBrk                    = DefBrk(MainEntryPt,TRUE);
  89.   pBrk->flag.DorI       = BP_IMMEDIATE;
  90.   pBrk->flag.DefineType = BP_INTERNAL;
  91.  }
  92.  else
  93.  {
  94.   pBrk                  = DefBrk(ExeEntryPt,TRUE);
  95.   pBrk->flag.DorI       = BP_IMMEDIATE;
  96.   pBrk->flag.DefineType = BP_INTERNAL;
  97.  }
  98.  
  99.  /****************************************************************************/
  100.  /* - Merge breaks from file.                                                */
  101.  /* - Turn off any watch points.                                             */
  102.  /* - Run to the initial control point.                                      */
  103.  /* - Free all the internal breakpoints.                                     */
  104.  /* - Turn watch points back on.                                             */
  105.  /****************************************************************************/
  106.  MergeBreakpoints();
  107.  WatchpointFlag = FALSE;
  108.  stopcode = GoEntry();
  109.  
  110.  for( pBrk = pnode->allbrks; pBrk ; )
  111.  {
  112.   if( pBrk->flag.DefineType != BP_INTERNAL )
  113.    pBrk  = pBrk->next;
  114.   else
  115.   {
  116.    ULONG where;
  117.  
  118.    where = pBrk->brkat;
  119.    pBrk  = pBrk->next;
  120.    UndBrk( where, TRUE );
  121.   }
  122.  }
  123.  
  124.  /****************************************************************************/
  125.  /* - Now attach the restart breakpoints to the front of the chain.          */
  126.  /****************************************************************************/
  127.  if( RestartBrks )
  128.  {
  129.   for( pBrk = RestartBrks; pBrk ; plast = pBrk, pBrk = pBrk->next ){;}
  130.  
  131.   plast->next    = pnode->allbrks;
  132.   pnode->allbrks = RestartBrks;
  133.  }
  134.  
  135.  WatchpointFlag = TRUE;
  136.  
  137.  /****************************************************************************/
  138.  /* - We need to get a DS to use in conjunction with 16 bit near pointers.   */
  139.  /****************************************************************************/
  140.  DgroupDS = AppPTB.DS;
  141.  
  142.  /****************************************************************************/
  143.  /* - Build the initial control point message.                               */
  144.  /****************************************************************************/
  145.  TransLateRcToMessage(stopcode,msgbuf);
  146.  
  147.  
  148.  /****************************************************************************/
  149.  /* - We need to make sure that we have managed to build an initial view.    */
  150.  /****************************************************************************/
  151.  fp_focus = SetExecfp();
  152.  if( (fp_focus == NULL) )
  153.   Error(ERR_VIEW_CANT_BUILD, TRUE,0, NULL);
  154.  
  155.  /****************************************************************************/
  156.  /* - Create/Start sema4s and polling threads for serial/multiple.           */
  157.  /****************************************************************************/
  158.  if( (SerialParallel()==SERIAL) && (SingleMultiple()==MULTIPLE) )
  159.  {
  160.   ALLPIDS  *p;
  161.  
  162.   /***************************************************************************/
  163.   /* - Set a flag to indicate that this debugger is finished initializing.   */
  164.   /***************************************************************************/
  165.   p = GetPid( cmd.ProcessID );
  166.  
  167.   p->PidFlags.Initializing = FALSE;
  168.   ReleaseSerialMutex();
  169.  
  170.   /***************************************************************************/
  171.   /* - Start a thread to check for notifications when the debugger is not    */
  172.   /*   connected.                                                            */
  173.   /***************************************************************************/
  174.   _beginthread( Check4ConnectRequest, NULL, 0x8000, NULL );
  175.  
  176.   /***************************************************************************/
  177.   /* - Start a thread to detect disconnects.                                 */
  178.   /***************************************************************************/
  179.    SetKillChk4Disconnect( FALSE );
  180.    _beginthread(CheckForDisConnect, NULL, 0x8000, NULL );
  181.  
  182.   if( IsParent() )
  183.   {
  184.    /**************************************************************************/
  185.    /* - The parent process creates the connect sema4, the mutex sema4,       */
  186.    /*   and starts the polling thread.                                       */
  187.    /**************************************************************************/
  188.    USHORT    DbgPid;
  189.  
  190.    p->Connect = CONNECTED;
  191.    DbgPid     = p->DbgPid;
  192.  
  193.    CreateConnectSema4( DbgPid, _DBG );
  194.    CreateSerialMutex();
  195.    _beginthread( PollForMsgFromEsp, NULL, 0x8000, NULL );
  196.   }
  197.  }
  198.  
  199.  /****************************************************************************/
  200.  /* - We may have brought the user's session to the foreground so that       */
  201.  /*   he can do whatever he needs to do to hit the main entry point          */
  202.  /*   of the child. Now bring the debug session back to the foreground.      */
  203.  /****************************************************************************/
  204.  DosSelectSession(0);
  205.  
  206.  for( ;; )
  207.  {
  208.   /***************************************************************************/
  209.   /* - Build a view for the current ExecAddr.                                */
  210.   /* - Set the flag for showing the currently executing line.                */
  211.   /***************************************************************************/
  212.   fp_focus = SetExecfp();
  213.   fp_focus->flags |= AF_ZOOM;
  214.  
  215.   /***************************************************************************/
  216.   /* - Toss any "one time" break points such as those hit by the             */
  217.   /*   RunToCursor() function.                                               */
  218.   /***************************************************************************/
  219.   DropOnceBrks( GetExecAddr() );
  220.  
  221.   how = -1;
  222.   while( how == -1 )
  223.    how = (int)(* fp_focus->shower)(fp_focus, msgbuf);
  224.  
  225.   /***************************************************************************/
  226.   /* - If we're just changing thread focus, then merely go build             */
  227.   /*   the thread view.                                                      */
  228.   /***************************************************************************/
  229.   if ( how == GOTID )
  230.    continue;
  231.  
  232.   /***************************************************************************/
  233.   /* - Now run the application.                                              */
  234.   /* - Then, build a message for the reason we stopped.                      */
  235.   /***************************************************************************/
  236.   SayStatusMsg("Running...Ctrl-Break to interrupt.");
  237.   SetKillChk4Disconnect( TRUE );
  238.   SetExecuteFlag( TRUE );
  239.   stopcode = Go(how);
  240.   SetExecuteFlag( FALSE );
  241.   TransLateRcToMessage(stopcode,msgbuf);
  242.  
  243.   /***************************************************************************/
  244.   /* - If we're debugging multiple processes over a serial connection, then  */
  245.   /*   we need a thread that will check to see if this debugger gets         */
  246.   /*   disconnected when the user clicks on another window.                  */
  247.   /***************************************************************************/
  248.   if( (SerialParallel() == SERIAL) && (SingleMultiple() == MULTIPLE) )
  249.   {
  250.    SetKillChk4Disconnect( FALSE );
  251.    _beginthread(CheckForDisConnect, NULL, 0x8000, NULL );
  252.   }
  253.  }
  254. }
  255.  
  256. /*****************************************************************************/
  257. /* TransLateRcToMessage()                                                 509*/
  258. /*                                                                           */
  259. /* Description:                                                              */
  260. /*      Formats a Return code from Dosdebug into message with address.       */
  261. /* Parameters:                                                               */
  262. /*      RetCode   -  Return Code from DosDebug.                              */
  263. /*      msg       -  -> to buffer where the message needs to be put.         */
  264. /* Return:                                                                   */
  265. /*                                                                           */
  266. /* Assumptions:                                                              */
  267. /*      none                                                                 */
  268. /*****************************************************************************/
  269. extern DEBUG_REGISTER  Debug_Regs[];
  270.  
  271. static uchar IstepMsg[] = "Instruction Step";
  272. static uchar BrkMsg[] = "Stopped at Breakpoint";
  273.  
  274. void TransLateRcToMessage(uint RetCode, char *msg)
  275. {
  276.  int      i;
  277.  USHORT   Sel;                                                          /*607*/
  278.  USHORT   Off;                                                          /*607*/
  279.  
  280.  switch (RetCode)
  281.  {
  282.    case TRAP_ADDR_LOAD:
  283.    {
  284.     BRK *pbrk;
  285.  
  286.     /*************************************************************************/
  287.     /* - find the unreported breakpoint, report it, then mark it.            */
  288.     /*************************************************************************/
  289.     pbrk = pnode->allbrks;
  290.     for(;pbrk; pbrk=pbrk->next )
  291.     {
  292.      if( pbrk->flag.Reported == 1 )
  293.      {
  294.       sprintf(msg, "Address %08X Loaded.", pbrk->brkat);
  295.      }
  296.     }
  297.    }
  298.    break;
  299.  
  300.    case TRAP_DLL_LOAD:
  301.    {
  302.     BRK *pbrk;
  303.  
  304.     /*************************************************************************/
  305.     /* - find the unreported breakpoint, report it, then mark it.            */
  306.     /*************************************************************************/
  307.     pbrk = pnode->allbrks;
  308.     for(;pbrk; pbrk=pbrk->next )
  309.     {
  310.      if( pbrk->flag.Reported == 1 )
  311.      {
  312.       pbrk->flag.Reported = 0;
  313.       sprintf(msg, "%s Loaded.", pbrk->dllname);
  314.      }
  315.     }
  316.    }
  317.    break;
  318.  
  319.    case TRAP_SS:
  320.      memcpy( msg, IstepMsg, sizeof(IstepMsg) );
  321.      break;
  322.  
  323.    case TRAP_BPT:
  324.      memcpy( msg, BrkMsg, sizeof(BrkMsg) );
  325.      break;
  326.  
  327.    case TRAP_ASYNC:
  328.      sprintf( msg, "Asynchronous stop" );
  329.      break;
  330.  
  331.    case TRAP_EXIT:
  332.    {
  333.     UCHAR bitness;
  334.  
  335.      bitness = GetBitness(GetExecAddr());
  336.      if(bitness == BIT32)
  337.        sprintf(msg, "Exit at %08X", GetExecAddr());
  338.      else
  339.      {
  340.        Code_Flat2SelOff(GetExecAddr(),&Sel,&Off);
  341.        sprintf(msg, "Exit at %04X:%04X",Sel,Off);
  342.      }
  343.    }
  344.    AppTerminated = TRUE;
  345.    break;
  346.  
  347.    case TRAP_DATA:
  348.      sprintf( msg, "Data breakpoint" );                                 /*521*/
  349.      break;
  350.  
  351.    case TRAP_ABEND:
  352.      sprintf( msg, "DosDebug Anomaly");                                 /*521*/
  353.      break;
  354.  
  355.    case TRAP_INTERLOCK:
  356.      beep();                                                            /*920*/
  357.      Error(ERR_TRAP_INTERLOCK,TRUE,0);                                  /*920*/
  358.      break;
  359.  
  360.    case TRAP_SS_ERROR:
  361.      beep();                                                            /*901*/
  362.      sprintf( msg, "Single Step Error");                                /*901*/
  363.      break;
  364.  
  365.    case XCPT_GUARD_PAGE_VIOLATION:
  366.    case XCPT_UNABLE_TO_GROW_STACK:
  367.    case XCPT_DATATYPE_MISALIGNMENT:
  368.    case XCPT_ACCESS_VIOLATION:
  369.    case XCPT_ILLEGAL_INSTRUCTION:
  370.    case XCPT_FLOAT_DENORMAL_OPERAND:
  371.    case XCPT_FLOAT_DIVIDE_BY_ZERO:
  372.    case XCPT_FLOAT_INEXACT_RESULT:
  373.    case XCPT_FLOAT_INVALID_OPERATION:
  374.    case XCPT_FLOAT_OVERFLOW:
  375.    case XCPT_FLOAT_STACK_CHECK:
  376.    case XCPT_FLOAT_UNDERFLOW:
  377.    case XCPT_INTEGER_DIVIDE_BY_ZERO:
  378.    case XCPT_INTEGER_OVERFLOW:
  379.    case XCPT_PRIVILEGED_INSTRUCTION:
  380.    case XCPT_IN_PAGE_ERROR:
  381.    case XCPT_PROCESS_TERMINATE:
  382.    case XCPT_ASYNC_PROCESS_TERMINATE:
  383.    case XCPT_NONCONTINUABLE_EXCEPTION:
  384.    case XCPT_INVALID_DISPOSITION:
  385.    case XCPT_INVALID_LOCK_SEQUENCE:
  386.    case XCPT_ARRAY_BOUNDS_EXCEEDED:
  387.    case XCPT_UNWIND:
  388.    case XCPT_BAD_STACK:
  389.    case XCPT_INVALID_UNWIND_TARGET:
  390.    case XCPT_SIGNAL:
  391.    case XCPT_PROGRAM:
  392.    {
  393.      int n;
  394.      beep();
  395.  
  396.      n = GetExceptionIndex( RetCode);
  397.  
  398.      if( AppPTB.CSAtr )
  399.       sprintf( msg, "Exception-%s At %08X", GetExceptionType(n), AppPTB.EIP );
  400.      else
  401.      {
  402.       Sel = AppPTB.CS;
  403.       Off = LoFlat(AppPTB.EIP);
  404.       sprintf( msg, "Exception-%s At %04X:%04X", GetExceptionType(n), Sel, Off );
  405.      }
  406.      break;
  407.    }
  408.  
  409.    case TRAP_PROC:
  410.      sprintf(msg,"Unable to debug new process");                        /*521*/
  411.      break;
  412.  
  413.    case TRAP_NMI:
  414.    case TRAP_FP:
  415.      break;
  416.  
  417.    case TRAP_WATCH:
  418.      /************************************************************************/
  419.      /* Scan the hardware debug registers for the watch point index hit,  602*/
  420.      /* and display the register number and address.                      602*/
  421.      /************************************************************************/
  422.      for (i = 0 ; i < NODEBUGREGS ; i++ )                               /*602*/
  423.      {                                                                  /*602*/
  424.          if( Debug_Regs[i].Wpindex == AppPTB.Index )                    /*602*/
  425.          {                                                              /*602*/
  426.             Debug_Regs[i].Status = ENABLED;                             /*602*/
  427.             Debug_Regs[i].Wpindex = 0;                                  /*602*/
  428.             sprintf(msg,"Watch Point %1d hit for Address %08X",         /*602*/
  429.                         i+1,Debug_Regs[i].Address);                     /*602*/
  430.             break;                                                      /*602*/
  431.          }                                                              /*602*/
  432.      }                                                                  /*602*/
  433.      break;                                                             /*602*/
  434.  
  435.    default:
  436.      printf( "\nSD386 System Error %u\n", RetCode);                     /*521*/
  437.      panic(OOstop);
  438.  }
  439. }
  440.  
  441. static BOOL ExecuteFlag;
  442. void SetExecuteFlag( BOOL TorF ) { ExecuteFlag = TorF; }
  443. BOOL IsExecuteFlag( void ) { return(ExecuteFlag); }
  444.