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

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   xsrvthds.c                                                              */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*   Start the debuggee.                                                     */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   06/30/93 Created.                                                       */
  12. /*                                                                           */
  13. /*...                                                                        */
  14. /*... 06/04/93  827   Joe       Add remote debug support.                    */
  15. /*... 09/16/93  901   Joe       Add code to handle resource interlock error. */
  16. /*... 12/06/93  907   Joe       Fix for not updating thread list.            */
  17. /*...                                                                        */
  18. /*****************************************************************************/
  19. #include "all.h"
  20.  
  21. /*****************************************************************************/
  22. /* XSrvGetThreadInfo()                                                       */
  23. /*                                                                           */
  24. /* Description:                                                              */
  25. /*                                                                           */
  26. /*   Get thread info.                                                        */
  27. /*                                                                           */
  28. /* Parameters:                                                               */
  29. /*                                                                           */
  30. /*   pBuffer        -> to receiver of thread info.                           */
  31. /*                                                                           */
  32. /* Return:                                                                   */
  33. /*                                                                           */
  34. /*   n          number of threads.                                           */
  35. /*                                                                           */
  36. /* Assumptions:                                                              */
  37. /*                                                                           */
  38. /*   We will not hang while traversing the circular list of thread           */
  39. /*   info from DosDebug.                                                     */
  40. /*                                                                           */
  41. /*****************************************************************************/
  42. ULONG XSrvGetThreadInfo( THREADINFO *pBuffer )
  43. {
  44.  ULONG         tid;
  45.  ULONG         nexttid;
  46.  ULONG         n;
  47.  PtraceBuffer  ptb;
  48.  APIRET        rc;
  49.  ULONG         eip;
  50.  
  51.  memset(&ptb,0,sizeof(ptb));
  52.  /****************************************************************************/
  53.  /* - Threads are kept in a circular list, so break on a repeat of           */
  54.  /*   thread 1.                                                              */
  55.  /****************************************************************************/
  56.  nexttid = 0;
  57.  for( n = 0 , tid = 1 ; nexttid != 1; tid = nexttid )
  58.  {
  59.   /***************************************************************************/
  60.   /* - get the thread status.                                                */
  61.   /***************************************************************************/
  62.   ptb.Pid = GetEspProcessID();
  63.   ptb.Cmd = DBG_C_ThrdStat;
  64.   ptb.Tid = tid;
  65.   ptb.Buffer = (ULONG)pBuffer;
  66.   ptb.Len = sizeof( ULONG );   /* thread stat buffer is 4 bytes */
  67.   rc = DosDebug( &ptb);
  68.  
  69.   /***************************************************************************/
  70.   /* - In order to keep from hanging in this loop on a resource interlock,901*/
  71.   /*   if we get an error on thread 1, then the debuggee has been severed.901*/
  72.   /*   In this case, simply return.                                       901*/
  73.   /***************************************************************************/
  74.   if( ptb.Cmd == DBG_N_Error && tid == 1 )                              /*901*/
  75.    return(0);                                                           /*901*/
  76.  
  77.   /***************************************************************************/
  78.   /* This test blows by gaps in the list of threads.                         */
  79.   /***************************************************************************/
  80.   if ( rc != 0 || ptb.Cmd != DBG_N_Success )
  81.    continue;
  82.  
  83.   pBuffer->tid = tid;                                                   /*907*/
  84.   nexttid = ptb.Value;
  85.   /***************************************************************************/
  86.   /* - get the thread eip.                                                   */
  87.   /***************************************************************************/
  88.   memset(&ptb,0,sizeof(ptb));
  89.   ptb.Pid = GetEspProcessID();
  90.   ptb.Cmd = DBG_C_ReadReg;
  91.   ptb.Tid = tid;
  92.   rc = DosDebug( &ptb);
  93.  
  94.   if ( rc != 0 || ptb.Cmd != DBG_N_Success )
  95.    continue;
  96.  
  97.   if( ptb.CSAtr )
  98.    eip = ptb.EIP;
  99.   else
  100.    eip = Sys_SelOff2Flat(ptb.CS, LoFlat(ptb.EIP));
  101.  
  102.   pBuffer->eip = eip;
  103.  
  104.   /***************************************************************************/
  105.   /* - bump counter/pointer.                                                 */
  106.   /***************************************************************************/
  107.   n++;
  108.   pBuffer++;
  109.  }
  110.  return(n);
  111. }
  112.  
  113. /*****************************************************************************/
  114. /* XSrvFreezeThread()                                                        */
  115. /*                                                                           */
  116. /* Description:                                                              */
  117. /*                                                                           */
  118. /*   Freeze single or all threads.                                           */
  119. /*                                                                           */
  120. /* Parameters:                                                               */
  121. /*                                                                           */
  122. /*   tid      thread id. If tid = 0 then all threads will be frozen.         */
  123. /*                                                                           */
  124. /* Return:                                                                   */
  125. /*                                                                           */
  126. /*   void                                                                    */
  127. /*                                                                           */
  128. /* Note:                                                                     */
  129. /*                                                                           */
  130. /*   Thread list must have been updated using BuildThreadList().             */
  131. /*                                                                           */
  132. /*****************************************************************************/
  133. void XSrvFreezeThread ( ULONG tid )
  134. {
  135.  PtraceBuffer ptb;
  136.  
  137.  memset(&ptb,0,sizeof(ptb));
  138.  ptb.Cmd = DBG_C_Freeze;
  139.  ptb.Pid = GetEspProcessID();
  140.  ptb.Tid = tid;
  141.  DosDebug(&ptb);
  142. }
  143.  
  144. /*****************************************************************************/
  145. /* XSrvThawThread()                                                          */
  146. /*                                                                           */
  147. /* Description:                                                              */
  148. /*                                                                           */
  149. /*   Thaw a thread or thaw all threads.                                      */
  150. /*                                                                           */
  151. /* Parameters:                                                               */
  152. /*                                                                           */
  153. /*   tid      thread to thaw or 0.                                           */
  154. /*                                                                           */
  155. /* Return:                                                                   */
  156. /*                                                                           */
  157. /*   void                                                                    */
  158. /*                                                                           */
  159. /* Assumption:                                                               */
  160. /*                                                                           */
  161. /*   Thread list must have been updated using BuildThreadList().             */
  162. /*                                                                           */
  163. /*****************************************************************************/
  164. void XSrvThawThread( ULONG tid )
  165. {
  166.  PtraceBuffer ptb;
  167.  
  168.  memset(&ptb,0,sizeof(ptb));
  169.  ptb.Cmd = DBG_C_Resume  ;
  170.  ptb.Pid = GetEspProcessID();
  171.  ptb.Tid = tid;
  172.  DosDebug(&ptb);
  173. }
  174.  
  175. /*****************************************************************************/
  176. /* XSrvSetExecThread()                                                       */
  177. /*                                                                           */
  178. /* Description:                                                              */
  179. /*                                                                           */
  180. /*   Set the execution context to another thread.                            */
  181. /*                                                                           */
  182. /* Parameters:                                                               */
  183. /*                                                                           */
  184. /*   pExecAddr -> to address that will receive the flat execution address.   */
  185. /*   pptb      -> to the ptrace buffer that we will fill in for the caller.  */
  186. /*   tid          the thread we're establishing context in.                  */
  187. /*                                                                           */
  188. /* Return:                                                                   */
  189. /*                                                                           */
  190. /*   rc        0=>success                                                    */
  191. /*             1=>failure                                                    */
  192. /*                                                                           */
  193. /* Assumption:                                                               */
  194. /*                                                                           */
  195. /*****************************************************************************/
  196. APIRET XSrvSetExecThread(ULONG *pExecAddr,PtraceBuffer *pptb, UINT tid)
  197. {
  198.  APIRET rc;
  199.  
  200.  memset(pptb,0,sizeof(PtraceBuffer));
  201.  
  202.  pptb->Pid = GetEspProcessID();
  203.  pptb->Cmd = DBG_C_ReadReg;
  204.  pptb->Tid = tid;
  205.  rc = DosDebug( pptb);
  206.  if( (rc != 0) || (pptb->Cmd != 0) )
  207.   return(1);
  208.  
  209.  if( pptb->CSAtr )
  210.   *pExecAddr = pptb->EIP;
  211.  else
  212.   *pExecAddr = Sys_SelOff2Flat(pptb->CS,LoFlat(pptb->EIP));
  213.  return(0);
  214. }
  215.