home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / THREADS.C < prev    next >
Text File  |  1996-03-07  |  22KB  |  334 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   threads.c                                                               */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Thread handling routines.                                                */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*...16->32 port.                                                            */
  12. /*...                                                                        */
  13. /*... 02/08/91  100   made changes for 32-bit compilation.                   */
  14. /*... 02/08/91  101   made changes for 32-bit compilation. ( by Joe C. )     */
  15. /*    04/04/91  107   Change calls to PeekData/PokeData to DBGet/DBPut.   107*/
  16. /*                                                                           */
  17. /*...Release 1.00 (Pre-release 1)                                            */
  18. /*...                                                                        */
  19. /*... 07/18/91  216   srinivas  Better processing for FAKE MID problems.     */
  20. /*...                                                                        */
  21. /*...Release 1.08 (Pre-release 108 )       )                                 */
  22. /*...                                                                        */
  23. /*... 01/17/92  504   Joe       Can't step into 16 bit dll on 6.177H.        */
  24. /*... 02/12/92  521   Joe       Port to C-Set/2.                             */
  25. /*...                                                                        */
  26. /*...Release 1.00 (03/03/92)                                                 */
  27. /*...                                                                        */
  28. /*... 03/20/92  607   Srinivas  CRMA fixes.                                  */
  29. /*...                                                                        */
  30. /*...Release 1.01 (04/03/92)                                                 */
  31. /*...                                                                        */
  32. /*... 10/06/92  709   Joe       Access violation at unknown addr causes trap.*/
  33. /*... 09/16/93  901   Joe       Add code to handle resource interlock error. */
  34. /*... 12/06/93  907   Joe       Fix for not updating thread list.            */
  35. /**Includes*******************************************************************/
  36.  
  37. #include "all.h"                        /* SD86 include files                */
  38.  
  39. /**External declararions******************************************************/
  40.  
  41. extern uint         ProcessID;
  42. extern PtraceBuffer AppPTB;
  43. extern AFILE    *fp_focus;              /* a holder for an afile node.       */
  44. extern PROCESS_NODE *pnode;                                             /*827*/
  45.  
  46. TSTATE        *thead = {NULL};          /* thread list head pointer          */
  47. TSTATE        *tail = {(TSTATE *)(void*)&thead};       /* last node in list  */
  48. /*****************************************************************************/
  49. /* InitThreads()                                                             */
  50. /*                                                                           */
  51. /* Description:                                                              */
  52. /*                                                                           */
  53. /*   Allocate memory for thread 1 and initialize the linked list of threads. */
  54. /*   Establish a pointer to the executing thread node which is thread one.   */
  55. /*                                                                           */
  56. /* Parameters:                                                               */
  57. /*                                                                           */
  58. /*   void                                                                    */
  59. /*                                                                           */
  60. /* Return:                                                                   */
  61. /*                                                                           */
  62. /*   void                                                                    */
  63. /*                                                                           */
  64. /*****************************************************************************/
  65.  void
  66. InitThreadList()                        /*                                   */
  67. {                                       /*                                   */
  68.  TSTATE  *np;                           /* pointer to thread structure node  */
  69.                                         /*                                   */
  70.  thead = NULL;                          /* thread list head pointer          */
  71.  tail = (TSTATE *)&thead;               /* last node in list                 */
  72.  np=(TSTATE *)Talloc(sizeof(TSTATE));   /* allocate one node              521*/
  73.  np->prev   = NULL;                     /* connect back pointer              */
  74.  tail->next = np;                       /* add to end of the list            */
  75.  np->next   = NULL;                     /* ground this node                  */
  76.  tail = np;                             /* establish tail pointer            */
  77.  np->tid = 1;                           /* this is thread 1                  */
  78. }                                       /* end InitThreadList()              */
  79. /*****************************************************************************/
  80. /* FreeThreadList()                                                          */
  81. /*                                                                           */
  82. /* Description:                                                              */
  83. /*                                                                           */
  84. /*   Deallocate the thread list memory for reinitialization.                 */
  85. /*                                                                           */
  86. /* Parameters:                                                               */
  87. /*                                                                           */
  88. /*   void                                                                    */
  89. /*                                                                           */
  90. /* Return:                                                                   */
  91. /*                                                                           */
  92. /*   void                                                                    */
  93. /*                                                                           */
  94. /*****************************************************************************/
  95.  void
  96. FreeThreadList()                        /*                                   */
  97. {                                       /*                                   */
  98.  TSTATE  *np;                           /* pointer to thread structure node  */
  99.  TSTATE  *npnext;                       /* -> to thread structure node    521*/
  100.  np = thead ;                           /* scan the list of threads       521*/
  101.  while (np)                             /* while not grounded node        521*/
  102.  {                                      /*                                521*/
  103.    npnext = np->next;                   /* -> to next thread node         521*/
  104.    Tfree( (void*)np );                   /* free the memory                521*/
  105.    np = npnext;                         /* set the pointer to cp          521*/
  106.  }                                      /*                                521*/
  107.  thead = NULL;                          /* thread list head pointer       822*/
  108.  tail = (TSTATE *)&thead;               /* last node in list              822*/
  109. }                                       /* end FreeThreadList()              */
  110.  
  111. /*****************************************************************************/
  112. /* GetThdDbgState()                                                          */
  113. /*                                                                           */
  114. /* Description:                                                              */
  115. /*                                                                           */
  116. /*   Get the Debug State of a thread.                                        */
  117. /*    0 = Freeze                                                             */
  118. /*    1 = Thaw                                                               */
  119. /*    2 = Terminating ( A result of DBG_N_ThreadTerm from DosDebug )         */
  120. /*                                                                           */
  121. /* Parameters:                                                               */
  122. /*                                                                           */
  123. /*   tid      thread id.                                                     */
  124. /*                                                                           */
  125. /* Return:                                                                   */
  126. /*                                                                           */
  127. /*   state    debug state of the thread.                                     */
  128. /*                                                                           */
  129. /*                                                                           */
  130. /*****************************************************************************/
  131.  uchar
  132. GetThdDbgState( uint tid )              /*                                   */
  133. {                                       /*                                   */
  134.  TSTATE      *np;                       /* thread state node pointer         */
  135.                                         /*                                   */
  136.  for ( np = thead;                      /* scan the list of threads          */
  137.        np->tid != tid;                  /*                                   */
  138.        np = np->next                    /*                                   */
  139.      ){;}                               /*                                   */
  140.  return(np->ts.DebugState);             /* return the debug state            */
  141. }                                       /* end of GetThdDbgState()           */
  142.  
  143. /*****************************************************************************/
  144. /* GetThreadState()                                                          */
  145. /*                                                                           */
  146. /* Description:                                                              */
  147. /*                                                                           */
  148. /*   Get the Thread State of a thread.                                       */
  149. /*    0 = Runnable                                                           */
  150. /*    1 = Suspended                                                          */
  151. /*    2 = Blocked                                                            */
  152. /*    3 = Critical                                                           */
  153. /*                                                                           */
  154. /* Parameters:                                                               */
  155. /*                                                                           */
  156. /*   tid      thread id.                                                     */
  157. /*                                                                           */
  158. /* Return:                                                                   */
  159. /*                                                                           */
  160. /*   state    thread state of the thread.                                    */
  161. /*                                                                           */
  162. /*                                                                           */
  163. /*****************************************************************************/
  164.  uchar
  165. GetThreadState( uint tid )              /*                                   */
  166. {                                       /*                                   */
  167.  TSTATE      *np;                       /* thread state node pointer         */
  168.                                         /*                                   */
  169.  if(thead) {
  170.  for ( np = thead;                      /* scan the list of threads          */
  171.        np->tid != tid;                  /*                                   */
  172.        np = np->next                    /*                                   */
  173.      ){;}                               /*                                   */
  174.  if(np) return(np->ts.ThreadState);            /* return the debug state            */
  175.  }
  176.  return(TRC_C_ENDED); /*Error condition. No threads or thread not found.*/
  177. }                                       /* end of GetThreadState()           */
  178.  
  179. /*****************************************************************************/
  180. /* GetThreadTSTATE()                                                         */
  181. /*                                                                           */
  182. /* Description:                                                              */
  183. /*                                                                           */
  184. /*   Get the TSTATE structure for the given tid.                             */
  185. /*                                                                           */
  186. /* Parameters:                                                               */
  187. /*                                                                           */
  188. /*   tid      thread id.                                                     */
  189. /*                                                                           */
  190. /* Return:                                                                   */
  191. /*                                                                           */
  192. /*   np       -> to TSTATE structure.                                        */
  193. /*                                                                           */
  194. /*                                                                           */
  195. /*****************************************************************************/
  196. TSTATE * GetThreadTSTATE( uint tid )
  197. {
  198.  TSTATE      *np;
  199.  
  200.  for ( np = thead;
  201.        np->tid != tid;
  202.        np = np->next
  203.      ){;}
  204.  return(np);
  205. }
  206.  
  207. /*****************************************************************************/
  208. /* BuildThreadList()                                                         */
  209. /*                                                                           */
  210. /* Description:                                                              */
  211. /*                                                                           */
  212. /*   Build a double linked list of  thread info.                             */
  213. /*                                                                           */
  214. /* Parameters:                                                               */
  215. /*                                                                           */
  216. /*   void                                                                    */
  217. /*                                                                           */
  218. /* Return:                                                                   */
  219. /*                                                                           */
  220. /*   void                                                                    */
  221. /*                                                                           */
  222. /* Notes:                                                                    */
  223. /*                                                                           */
  224. /*   We assume that once a thread id is established it is valid for the      */
  225. /*   duration of the process. It may reach a terminating condition but we    */
  226. /*   let the operating system and DosDebug manage that.                      */
  227. /*                                                                           */
  228. /*****************************************************************************/
  229.  void
  230. BuildThreadList()
  231. {
  232.  uint         tid;                      /* thread id                         */
  233.  uint         mid;                      /* module id                         */
  234.  LNOTAB      *pLnoTabEntry;
  235.  uint         eip;                      /* flat address in thread.           */
  236.  TSTATE      *np;                       /* thread state node pointer         */
  237.  DEBFILE     *pdf;                      /* -> to debug file structure        */
  238.  THREADINFO   buffer[100];                                              /*827*/
  239.  ULONG        ntids;                                                    /*827*/
  240.  
  241.  /****************************************************************************/
  242.  /* - Get the thread information and free up the old list.                   */
  243.  /****************************************************************************/
  244.  ntids = xGetThreadInfo( buffer );
  245.  FreeThreadList();                                                      /*907*/
  246.  for( tid = 1; tid<=ntids; tid++ )
  247.  {
  248.   /***************************************************************************/
  249.   /* - allocate.                                                             */
  250.   /* - fill in the info.                                                     */
  251.   /***************************************************************************/
  252.   np=(TSTATE *)Talloc(sizeof(TSTATE));
  253.   np->prev   = tail;
  254.   tail->next = np;
  255.   np->next   = NULL;
  256.   tail = np;
  257.  
  258.   np->ts  = buffer[tid-1].ts;
  259.   np->ip  = buffer[tid-1].eip;
  260.   np->tid = buffer[tid-1].tid;                                          /*906*/
  261.  }
  262. /*****************************************************************************/
  263. /*                                                                           */
  264. /* Now we want to scan the list we just built and add additional structure   */
  265. /* info.                                                                     */
  266. /*                                                                           */
  267. /*****************************************************************************/
  268.  pLnoTabEntry = NULL;
  269.  for ( np = thead;                      /* scan the list we just built       */
  270.        np != NULL;
  271.        np = np->next
  272.      )
  273.  {
  274.   eip = np->ip;
  275.   mid = 0;
  276.  
  277.   /***************************************************************************/
  278.   /* - scan the exe/dlls to find the mid, lno, and sfi for this eip.         */
  279.   /***************************************************************************/
  280.   for (pdf=pnode->ExeStruct; (pdf != NULL) && (mid == 0); pdf = pdf->next )                                 /*                                   */
  281.   {
  282.    mid=DBMapInstAddr(eip, &pLnoTabEntry, pdf);
  283.    np->pdf = pdf;
  284.   }
  285.  
  286.   np->mid = mid;
  287.   if( pLnoTabEntry != NULL )
  288.   {
  289.    np->lno = pLnoTabEntry->lno;
  290.    np->sfi = pLnoTabEntry->sfi;
  291.   }
  292.  
  293.   if (mid==0)                           /* if we can't locate ourselves      */
  294.   {                                     /* then fall into the fake mid       */
  295.    np->mid = FAKEMID;                   /* this is the fake mid              */
  296.    np->pdf = pnode->ExeStruct;          /* then assume the EXE file          */
  297.   }
  298.  }                                      /*                                   */
  299. }                                       /* end of BuildThreadList()          */
  300.  
  301. /*****************************************************************************/
  302. /* RunThread()                                                               */
  303. /*                                                                           */
  304. /* Description:                                                              */
  305. /*                                                                           */
  306. /*   set user selection to the executing thread                              */
  307. /*                                                                           */
  308. /* Parameters:                                                               */
  309. /*                                                                           */
  310. /*   threadID    input - thread id selected to be executed                   */
  311. /*                                                                           */
  312. /* Return:                                                                   */
  313. /*                                                                           */
  314. /*   0           not used.                                                   */
  315. /*                                                                           */
  316. /*****************************************************************************/
  317.  uint
  318. RunThread( uint threadID )
  319. {
  320.  uchar StatusMsg[20];
  321.  
  322.  SetExecValues( threadID, TRUE);
  323.  SetActFrames();
  324.  fp_focus = SetExecfp() ;
  325.  
  326.  sprintf(StatusMsg, "Selecting Tid %-3d", threadID);
  327.  SayMsgBox2( StatusMsg, 450UL );
  328.  
  329.  if( fp_focus )
  330.   fp_focus->flags |= AF_ZOOM;
  331.  
  332.  return( 0 );
  333. }
  334.