home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / XSRVBRK.C < prev    next >
Text File  |  1995-11-02  |  23KB  |  442 lines

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   srvrbrk.c                                                            827*/
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Handle breakpoints.                                                      */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   06/04/93 Created.                                                       */
  12. /*                                                                           */
  13. /*... 06/04/93  827   Joe       Add remote debug support.                    */
  14. /*...                                                                        */
  15. /*****************************************************************************/
  16. #include "all.h"
  17.  
  18. static BRK *pAllBrks;
  19. /*****************************************************************************/
  20. /* XSrvDefBrk                                                                */
  21. /*                                                                           */
  22. /* Description:                                                              */
  23. /*                                                                           */
  24. /*   Add a breakpoint to the server breakpoint list.                         */
  25. /*                                                                           */
  26. /* Parameters:                                                               */
  27. /*                                                                           */
  28. /*   where      address where the breakpoint is to be set.                   */
  29. /*                                                                           */
  30. /* Return:                                                                   */
  31. /*                                                                           */
  32. /*   p         -> to the BRK structure.                                      */
  33. /*                                                                           */
  34. /* Assumptions:                                                              */
  35. /*                                                                           */
  36. /*   The returned BRK* is only used within the x-server.                     */
  37. /*                                                                           */
  38. /*****************************************************************************/
  39. BRK *XSrvDefBrk( ULONG where )
  40. {
  41.  BRK *p;
  42.  
  43.  p = (BRK *)Talloc(sizeof(BRK));
  44.  p->brkat = where;
  45.  p->byte = BREAKPT8086OPCODE;
  46.  p->next = pAllBrks;
  47.  pAllBrks = p ;
  48.  return(p);
  49. }
  50.  
  51. /*****************************************************************************/
  52. /* XSrvUndBrk()                                                              */
  53. /*                                                                           */
  54. /* Description:                                                              */
  55. /*                                                                           */
  56. /*  Undefine a break point.                                                  */
  57. /*                                                                           */
  58. /* Parameters:                                                               */
  59. /*                                                                           */
  60. /*    brkat   address we're undefining.                                      */
  61. /*                                                                           */
  62. /* Return:                                                                   */
  63. /*                                                                           */
  64. /* Assumptions:                                                              */
  65. /*                                                                           */
  66. /*   brk != NULL &&                                                          */
  67. /*   brk is a pointer to a brk* structure in the list of breakpoints.        */
  68. /*   This guarantees that we will always be able to find it.                 */
  69. /*                                                                           */
  70. /*****************************************************************************/
  71. void XSrvUndBrk( ULONG brkat )
  72. {
  73.  BRK *p, *pprev;
  74.  
  75.  /****************************************************************************/
  76.  /* - scan the break point list and remove this break point.                 */
  77.  /****************************************************************************/
  78.  pprev = (BRK*)&pAllBrks;
  79.  p = pprev->next;
  80.  for( ; p ; )
  81.  {
  82.   if( p->brkat == brkat )
  83.   {
  84.    pprev->next = p->next;
  85.    if( p->cond )
  86.      Tfree( (void*)p->cond );
  87.    Tfree((void*)p);
  88.    return;
  89.   }
  90.   pprev = p;
  91.   p = pprev->next;
  92.  }
  93. }
  94. /*****************************************************************************/
  95. /* XSrvPutInBrk()                                                            */
  96. /*                                                                           */
  97. /* Description:                                                              */
  98. /*                                                                           */
  99. /*   Stuff in a break.                                                       */
  100. /*                                                                           */
  101. /* Parameters:                                                               */
  102. /*                                                                           */
  103. /*   brk       structure containing the breakpoint information.              */
  104. /*                                                                           */
  105. /* Return:                                                                   */
  106. /*                                                                           */
  107. /* Assumptions:                                                              */
  108. /*                                                                           */
  109. /*****************************************************************************/
  110. void XSrvPutInBrk( ULONG where )
  111. {
  112.  UCHAR opcode;
  113.  UINT  bytesread;
  114.  UCHAR *p;
  115.  BRK   *pBrk;
  116.  
  117. #if 0
  118.  /****************************************************************************/
  119.  /* - deferred breakpoints do not have addresses yet, so we can't            */
  120.  /*   "insert" them.                                                         */
  121.  /****************************************************************************/
  122.  if((brk->flag & BP_DEFR_ONCE) ||
  123.    (brk->flag & BP_DEFR_ALL))
  124.   return;
  125. #endif
  126.  
  127.  /****************************************************************************/
  128.  /* - get a pointer to the BRK structure for this address.                   */
  129.  /* - extract the opcode from the address where the breakpoint will go.      */
  130.  /* - if read fails then drop into error.                                    */
  131.  /****************************************************************************/
  132.  pBrk = GetBrk( where );
  133.  if( pBrk == NULL )
  134.   return;
  135.  p=Getnbytes(pBrk->brkat,1,&bytesread);
  136.  if( bytesread )
  137.   opcode = *p;
  138.  else
  139.   goto error;
  140.  
  141.  /****************************************************************************/
  142.  /* - if there's not a break point already there then put one in.            */
  143.  /****************************************************************************/
  144.  if( opcode != BREAKPT8086OPCODE)
  145.  {
  146.   pBrk->byte = opcode;
  147.   opcode = BREAKPT8086OPCODE;
  148.   if( Putnbytes(pBrk->brkat,1,&opcode) )
  149.    goto error;
  150.  }
  151.  return;
  152.  
  153. error:
  154.  /****************************************************************************/
  155.  /* - toss the breakpoint on an error.                                       */
  156.  /****************************************************************************/
  157.  XSrvUndBrk( where );
  158. }
  159. /*****************************************************************************/
  160. /* XSrvPullOutBrk()                                                          */
  161. /*                                                                           */
  162. /* Description:                                                              */
  163. /*                                                                           */
  164. /*   Replaces an INT3 with the saved away opcode.                            */
  165. /*                                                                           */
  166. /* Parameters:                                                               */
  167. /*                                                                           */
  168. /*   brk        -> to structure containing address where we want to lift     */
  169. /*                 the break.                                                */
  170. /*                                                                           */
  171. /* Return:                                                                   */
  172. /*                                                                           */
  173. /*  rc          0=>breakpoint removed.                                       */
  174. /*              1=>unable to remove the breakpoint.                          */
  175. /*                                                                           */
  176. /* Assumptions:                                                              */
  177. /*                                                                           */
  178. /*****************************************************************************/
  179. int XSrvPullOutBrk( ULONG where )
  180. {
  181.  UCHAR  opcode;
  182.  UCHAR *p;
  183.  UINT   bytesread;
  184.  BRK   *pBrk;
  185.  
  186.  /****************************************************************************/
  187.  /* - get a pointer to the BRK structure for this address.                   */
  188.  /* - extract the INT3 from the address.                                     */
  189.  /* - if read fails then drop into error.                                    */
  190.  /****************************************************************************/
  191.  pBrk = GetBrk( where );
  192.  if( pBrk == NULL )
  193.   return(0);
  194.  p=Getnbytes(pBrk->brkat,1,&bytesread);
  195.  if( bytesread )
  196.   opcode = *p;
  197.  else
  198.   goto error;
  199.  
  200.  /****************************************************************************/
  201.  /* - put the original opcode back in.                                       */
  202.  /****************************************************************************/
  203.  if( opcode == BREAKPT8086OPCODE )
  204.  {
  205.   opcode = pBrk->byte;
  206.   if( Putnbytes(pBrk->brkat,1,&opcode ) )
  207.    goto error;
  208.  }
  209.  return(0);
  210.  
  211. error:
  212.  /****************************************************************************/
  213.  /* If we can't peek or poke the breakpoint, then return unsuccessful.       */
  214.  /****************************************************************************/
  215.  return(1);
  216. }
  217.  
  218. /*****************************************************************************/
  219. /* XSrvInsertAllBrk()                                                        */
  220. /*                                                                           */
  221. /* Description:                                                              */
  222. /*                                                                           */
  223. /*   Stuff in all breaks.                                                    */
  224. /*                                                                           */
  225. /* Parameters:                                                               */
  226. /*                                                                           */
  227. /* Return:                                                                   */
  228. /*                                                                           */
  229. /* Assumptions:                                                              */
  230. /*                                                                           */
  231. /*****************************************************************************/
  232. void XSrvInsertAllBrk( void )
  233. {
  234.  BRK *p;
  235.  
  236.  for( p = pAllBrks; p ; p = p->next )
  237.   XSrvPutInBrk( p->brkat );
  238.  return;
  239. }
  240.  
  241. /*****************************************************************************/
  242. /* XSrvRemoveAllBrk()                                                        */
  243. /*                                                                           */
  244. /* Description:                                                              */
  245. /*                                                                           */
  246. /*   Remove INT3s and put back opcodes.                                      */
  247. /*                                                                           */
  248. /* Parameters:                                                               */
  249. /*   void                                                                    */
  250. /*                                                                           */
  251. /* Return:                                                                   */
  252. /*                                                                           */
  253. /*   void                                                                    */
  254. /*                                                                           */
  255. /*****************************************************************************/
  256. void XSrvRemoveAllBrk( void )
  257. {
  258.  BRK *p;
  259.  BRK *plast;
  260.  
  261.  p = pAllBrks;
  262.  plast = (BRK*)&pAllBrks;
  263.  for( ; p ; plast = p, p = p->next )
  264.  {
  265.   if( XSrvPullOutBrk( p->brkat ) == 1 )
  266.   {
  267.    XSrvUndBrk( p->brkat );
  268.    p = plast;
  269.   }
  270.  }
  271.  return;
  272. }
  273.  
  274. /*****************************************************************************/
  275. /* XSrvInsertOneBrk                                                              */
  276. /*                                                                           */
  277. /* Description:                                                              */
  278. /*                                                                           */
  279. /*   Put in a single breakpoint from the list. If not in the list then       */
  280. /*   add it to the list.                                                     */
  281. /*                                                                           */
  282. /* Parameters:                                                               */
  283. /*                                                                           */
  284. /*   where      address where the breakpoint goes.                           */
  285. /*                                                                           */
  286. /* Return:                                                                   */
  287. /*                                                                           */
  288. /*   TRUE       if the breakpoint had to be defined then inform the          */
  289. /*              caller.                                                      */
  290. /*   FALSE      if the breakpoint was already in the list.                   */
  291. /*                                                                           */
  292. /*                                                                           */
  293. /* Assumptions:                                                              */
  294. /*                                                                           */
  295. /*  There exists a BRK * defining the breakpoint.                            */
  296. /*                                                                           */
  297. /*****************************************************************************/
  298. int XSrvInsertOneBrk( ULONG where )
  299. {
  300.  BRK   *p;
  301.  
  302.  for( p = pAllBrks; p ; p = p->next )
  303.  {
  304.   if( p->brkat == where )
  305.   {
  306.     XSrvPutInBrk( p->brkat );
  307.     return(FALSE);
  308.   }
  309.  }
  310.  p = XSrvDefBrk( where );
  311.  XSrvPutInBrk( p->brkat );
  312.  return(TRUE);
  313. }
  314.  
  315. /*****************************************************************************/
  316. /* XSrvRemoveOneBrk()                                                        */
  317. /*                                                                           */
  318. /* Description:                                                              */
  319. /*                                                                           */
  320. /*   Remove a breakpoint.                                                    */
  321. /*                                                                           */
  322. /* Parameters:                                                               */
  323. /*                                                                           */
  324. /*   where      address of breakpoint to remove.                             */
  325. /*                                                                           */
  326. /* Return:                                                                   */
  327. /*                                                                           */
  328. /* Assumptions:                                                              */
  329. /*                                                                           */
  330. /*****************************************************************************/
  331. void XSrvRemoveOneBrk( ULONG where)
  332. {
  333.  BRK *p;
  334.  
  335.  for( p = pAllBrks; p ; p = p->next )
  336.  {
  337.   if( p->brkat == where )
  338.   {
  339.    XSrvPullOutBrk( p->brkat );
  340.    return;
  341.   }
  342.  }
  343. }
  344.  
  345. /*****************************************************************************/
  346. /* GetBreak()                                                                    */
  347. /*                                                                           */
  348. /* Description:                                                              */
  349. /*                                                                           */
  350. /*   Get a pointer to the breakpoint structure for the parameter address.    */
  351. /*                                                                           */
  352. /* Parameters:                                                               */
  353. /*                                                                           */
  354. /*   where      address we want.                                             */
  355. /*                                                                           */
  356. /* Return:                                                                   */
  357. /*                                                                           */
  358. /*  pBrk        -> to BRK structure defining where.                          */
  359. /*              NULL if not found.                                           */
  360. /*                                                                           */
  361. /* Assumptions:                                                              */
  362. /*                                                                           */
  363. /*****************************************************************************/
  364. BRK *GetBrk( ULONG where )
  365. {
  366.  BRK *pBrk;
  367.  
  368.  for( pBrk = pAllBrks; pBrk ; pBrk = pBrk->next )
  369.  {
  370.   if( pBrk->brkat == where )
  371.    break;
  372.  }
  373.  return(pBrk);
  374. }
  375.  
  376. /*****************************************************************************/
  377. /* ClearBrks()                                                               */
  378. /*                                                                           */
  379. /* Description:                                                              */
  380. /*   free all break points in the xserver.                                   */
  381. /*                                                                           */
  382. /* Parameters:                                                               */
  383. /*                                                                           */
  384. /* Return:                                                                   */
  385. /*                                                                           */
  386. /* Assumptions:                                                              */
  387. /*                                                                           */
  388. /*                                                                           */
  389. /*****************************************************************************/
  390. void ClearBrks( )
  391. {
  392.   BRK   *bp;
  393.   BRK   *bpnext;
  394.   BRK   *bplast;
  395.  
  396.   bplast=(BRK*)&pAllBrks;
  397.   bp=pAllBrks;
  398.   while(  bp )
  399.   {
  400.    bpnext= bp->next;
  401.    if(bp->funcname)
  402.     Tfree((void*)bp->funcname);
  403.    if(bp->dllname)
  404.     Tfree((void*)bp->dllname);
  405.    Tfree((void*)bp);
  406.    bplast->next=bpnext;
  407.    bp=bpnext;
  408.   }
  409.   pAllBrks = NULL;
  410. }
  411.  
  412. /*****************************************************************************/
  413. /* _IfBrkOnAddr()                                                            */
  414. /*                                                                           */
  415. /* Description:                                                              */
  416. /*                                                                           */
  417. /*   Is there a break defined at this address.                               */
  418. /*                                                                           */
  419. /* Parameters:                                                               */
  420. /*                                                                           */
  421. /*   address    breakpoint address we're testing for.                        */
  422. /*                                                                           */
  423. /* Return Value:                                                             */
  424. /*                                                                           */
  425. /*   TRUE or FALSE                                                           */
  426. /*                                                                           */
  427. /* Assumptions:                                                              */
  428. /*                                                                           */
  429. /*****************************************************************************/
  430. BRK *_IfBrkOnAddr( UINT address )
  431. {
  432.  BRK *p = NULL;
  433.  
  434.  for( p = pAllBrks; p ; p = p->next )
  435.  {
  436.   if( p->brkat == address ){
  437.    return( p );
  438.   }
  439.  }
  440.  return( NULL );
  441. }
  442.