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

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   mte.c                                                                822*/
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*  Handle mte table functions.                                              */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*... 01/29/93 Created.                                                      */
  12. /*... 05/06/93  822   Joe       Add mte table handling.                      */
  13. /*... 09/09/93  900   Joe       Fix for trap when  ...\EXE\.. in path name.  */
  14. /*...                                                                        */
  15. /*****************************************************************************/
  16. #include "all.h"
  17.  
  18. #define NextMod(p)  ((UCHAR*)p+*(UINT*)p + sizeof(UINT) )
  19. #define NextObj(p) ( ((UCHAR*)p+2*sizeof(UINT)) + *((UCHAR*)p+2*sizeof(UINT)) \
  20.                      + sizeof(UINT) )
  21.  
  22. static MTE_TABLE_ENTRY *pMteTable;
  23. static UINT             TableSize;
  24.  
  25. extern PROCESS_NODE *pnode;
  26.  
  27. /*****************************************************************************/
  28. /* UpdateMteTable()                                                          */
  29. /*                                                                           */
  30. /* Description:                                                              */
  31. /*                                                                           */
  32. /*   Add information to the mte table.                                       */
  33. /*                                                                           */
  34. /* Parameters:                                                               */
  35. /*                                                                           */
  36. /*   pModuleTable input - -> to a module table of dll load information.      */
  37. /*                                                                           */
  38. /* Return:                                                                   */
  39. /*                                                                           */
  40. /*   void* pMteTable                                                         */
  41. /*                                                                           */
  42. /* Assumptions:                                                              */
  43. /*                                                                           */
  44. /*****************************************************************************/
  45. void *UpdateMteTable( UINT *pModuleTable )
  46. {
  47.  
  48.  UINT   NumModules;
  49.  
  50.  MODULE_LOAD_ENTRY      *pMteEntry;
  51.  OBJTABLEENTRY  *pObjectTable;
  52.  int             ModIndex;
  53.  
  54.  /****************************************************************************/
  55.  /* First scan the module table to see if there are any module frees. If     */
  56.  /* there have been, then scan the current mte table and mark the freed      */
  57.  /* entries. These entries may be cleaned up later if the table needs to     */
  58.  /* expand. Also, dump the data strucures allocated for this mte.            */
  59.  /****************************************************************************/
  60.  NumModules = *pModuleTable++;
  61.  
  62.  pMteEntry = (MODULE_LOAD_ENTRY*)pModuleTable;
  63.  for(ModIndex=1;ModIndex<=NumModules;ModIndex++,
  64.             pMteEntry=(MODULE_LOAD_ENTRY*)NextMod(pMteEntry) )
  65.  {
  66.   if(strlen(pMteEntry->ModuleName) == 0 )
  67.   {
  68.    MarkTheFreedMtes(pMteEntry->mte);
  69.    FreeMte(pMteEntry->mte);
  70.   }
  71.  }
  72.  
  73.  /****************************************************************************/
  74.  /* Now, scan the module table and update the mte table.                     */
  75.  /****************************************************************************/
  76.  pMteEntry = (MODULE_LOAD_ENTRY*)pModuleTable;
  77.  for(ModIndex=1;ModIndex<=NumModules;ModIndex++ ,
  78.             pMteEntry=(MODULE_LOAD_ENTRY*)NextMod(pMteEntry) )
  79.  {
  80.   if(strlen(pMteEntry->ModuleName) != 0 )
  81.   {
  82.    pObjectTable = (OBJTABLEENTRY *)NextObj(pMteEntry);
  83.    AddObjectsToMteTable( pMteEntry->mte, pObjectTable );
  84.   }
  85.  }
  86. /*DumpMteTable( pMteTable );*/
  87.  return((void*)pMteTable );
  88. }
  89.  
  90. /*****************************************************************************/
  91. /* MarkTheFreedMtes()                                                        */
  92. /*                                                                           */
  93. /* Description:                                                              */
  94. /*                                                                           */
  95. /*   Mark the entries in the module table as freed. We simply replace        */
  96. /*   the mtes with a 0.                                                      */
  97. /*                                                                           */
  98. /* Parameters:                                                               */
  99. /*                                                                           */
  100. /*   mte          input - the target executable module table handle.         */
  101. /*                                                                           */
  102. /* Return:                                                                   */
  103. /*                                                                           */
  104. /*   void                                                                    */
  105. /*                                                                           */
  106. /* Assumptions:                                                              */
  107. /*                                                                           */
  108. /*****************************************************************************/
  109.  
  110. void MarkTheFreedMtes( UINT mte )
  111. {
  112.  MTE_TABLE_ENTRY *pMteTableEntry;
  113.  
  114.  /****************************************************************************/
  115.  /* Scan the mte table and zero out all the mte entries for this mte.        */
  116.  /****************************************************************************/
  117.  for(pMteTableEntry = pMteTable ; pMteTableEntry->mte != ENDOFTABLE;
  118.              pMteTableEntry++ )
  119.   if(pMteTableEntry->mte == mte)
  120.    pMteTableEntry->mte = 0;
  121.  
  122. }
  123.  
  124. /*****************************************************************************/
  125. /* AddMtesToTable()                                                          */
  126. /*                                                                           */
  127. /* Description:                                                              */
  128. /*                                                                           */
  129. /*   Update the mte table with the entries from an object table.             */
  130. /*                                                                           */
  131. /* Parameters:                                                               */
  132. /*                                                                           */
  133. /*   pObjectTable input - -> to a Module  table module table handle.         */
  134. /*                                                                           */
  135. /* Return:                                                                   */
  136. /*                                                                           */
  137. /*   void                                                                    */
  138. /*                                                                           */
  139. /* Assumptions:                                                              */
  140. /*                                                                           */
  141. /*   The mte table pointer, pMteTable, will be NULL on the first             */
  142. /*   entry to this function.                                                 */
  143. /*                                                                           */
  144. /*****************************************************************************/
  145. void AddObjectsToMteTable( UINT mte, OBJTABLEENTRY *pObjectTable )
  146. {
  147.  MTE_TABLE_ENTRY *pMteTableEntry;
  148.  UINT NumObjects;
  149.  int  i;
  150.  
  151.  /****************************************************************************/
  152.  /*  - Get the number of objects to be added to the table.                   */
  153.  /*  - Bump the object table pointer past the NumObjects field.              */
  154.  /****************************************************************************/
  155.  NumObjects = *(UINT*)pObjectTable;
  156.  pObjectTable = (OBJTABLEENTRY*)((UCHAR*)pObjectTable + sizeof(UINT));
  157.  
  158. tryagain:
  159.  /****************************************************************************/
  160.  /* Scan the mte table to see if this mte is already there. If it is         */
  161.  /* then just don't put it in. We will also come here after the mte          */
  162.  /* table has been expanded.                                                 */
  163.  /****************************************************************************/
  164.  for(pMteTableEntry = pMteTable ;
  165.                       pMteTable &&
  166.                       ( pMteTableEntry->mte != ENDOFTABLE);
  167.                       pMteTableEntry++ )
  168.   if(pMteTableEntry->mte == mte)
  169.    return;
  170.  
  171.  /****************************************************************************/
  172.  /* At this point, pMteTableEntry is pointing to the ENDOFTABLE entry.       */
  173.  /*                                                                          */
  174.  /* - If there's room in the table, then add the objects.                    */
  175.  /* - If there isn't then go expand the table and come back.                 */
  176.  /****************************************************************************/
  177.  if( (TableSize - (pMteTableEntry - pMteTable)) > NumObjects )
  178.  {
  179.   for( i=1;i<=NumObjects;pObjectTable++,pMteTableEntry++ , i++ )
  180.   {
  181.    pMteTableEntry->mte      = mte;
  182.    pMteTableEntry->ObjNum   = pObjectTable->ObjNum;
  183.    pMteTableEntry->LoadAddr = pObjectTable->ObjLoadAddr;
  184.   }
  185.   pMteTableEntry->mte = ENDOFTABLE;
  186.  }
  187.  else
  188.  /****************************************************************************/
  189.  /* - Expand the table to hold the growth.                                   */
  190.  /* - If there is an old table ( i.e. not the first time through ) then      */
  191.  /*    - copy the new table to the expanded block.                           */
  192.  /*    - free the old table.                                                 */
  193.  /* - If it's the first time through, then mark the end of the table.        */
  194.  /* - Update the table pointer and go back and try again.                    */
  195.  /****************************************************************************/
  196.  {
  197.   MTE_TABLE_ENTRY *ptr;
  198.   UINT             size;
  199.  
  200.   size = TableSize;
  201.   TableSize += TABLE_INCREMENT;
  202.   ptr = (MTE_TABLE_ENTRY*)Talloc(TableSize*sizeof(MTE_TABLE_ENTRY) );
  203.   if(pMteTable)
  204.   {
  205.    memcpy(ptr, pMteTable, size*sizeof(MTE_TABLE_ENTRY) );
  206.    Tfree(pMteTable);
  207.   }
  208.   if( pMteTable == NULL)
  209.    ptr->mte = ENDOFTABLE;
  210.   pMteTable = ptr;
  211.   goto tryagain;
  212.  }
  213. }
  214.  
  215. /*****************************************************************************/
  216. /* ExeDllInit()                                                              */
  217. /*                                                                           */
  218. /* Description:                                                              */
  219. /*                                                                           */
  220. /*   Initialize the data structures for module loads.                        */
  221. /*                                                                           */
  222. /* Parameters:                                                               */
  223. /*                                                                           */
  224. /*   pModuleTable input - -> to a module table of dll load information.      */
  225. /*                                                                           */
  226. /* Return:                                                                   */
  227. /*                                                                           */
  228. /* Assumptions:                                                              */
  229. /*                                                                           */
  230. /*****************************************************************************/
  231. APIRET ExeDllInit( UINT *pModuleTable )
  232. {
  233.  UINT            NumModules;
  234.  MODULE_LOAD_ENTRY      *pMteEntry;
  235.  OBJTABLEENTRY  *pObjectTable;
  236.  int             ModIndex;
  237.  char           *pModuleName;
  238.  UINT            NumObjects;
  239.  UINT            ObjectTableLength;
  240.  UCHAR          *p;
  241.  APIRET          rc = 0;
  242.  
  243.  NumModules = *pModuleTable++;
  244.  /****************************************************************************/
  245.  /* - Scan the module table.                                                 */
  246.  /* - If we're debugging remote, then we have to find the image of the       */
  247.  /*   executable.                                                            */
  248.  /* - If we can't find the image, the free up allocated storage and go on.   */
  249.  /*                                                                          */
  250.  /****************************************************************************/
  251.  pMteEntry = (MODULE_LOAD_ENTRY*)pModuleTable;
  252.  for(ModIndex=1;ModIndex<=NumModules;ModIndex++ ,
  253.             pMteEntry=(MODULE_LOAD_ENTRY*)NextMod(pMteEntry) )
  254.  {
  255.   if(strlen(pMteEntry->ModuleName) != 0 )
  256.   {
  257.    pModuleName = pMteEntry->ModuleName;
  258.    /**************************************************************************/
  259.    /* Allocate space for the object table. The module table will be          */
  260.    /* freed eventually so we have to get the object table out of it          */
  261.    /* before it goes away.                                                   */
  262.    /**************************************************************************/
  263.    pObjectTable = (OBJTABLEENTRY *)NextObj(pMteEntry);
  264.    NumObjects = *(UINT*)pObjectTable;
  265.    ObjectTableLength = NumObjects*sizeof(OBJTABLEENTRY) + sizeof(UINT);
  266.    p = Talloc(ObjectTableLength);
  267.    memcpy(p,pObjectTable,ObjectTableLength);
  268.    pObjectTable = (OBJTABLEENTRY*)p;
  269.  
  270.    /**************************************************************************/
  271.    /* - the module name will already have been converted to upper case.   900*/
  272.    /**************************************************************************/
  273.    if( strstr(pModuleName,".EXE") != NULL)                              /*900*/
  274.    {
  275.     rc = exeinit(pMteEntry->mte,pModuleName,pObjectTable);
  276.     if( rc != 0 )
  277.      Error(rc,TRUE,1,pModuleName);
  278.    }
  279.    else
  280.    {
  281.     rc = dllinit(pMteEntry->mte,pModuleName,pObjectTable);
  282.     if( (rc == TRAP_ADDR_LOAD) ||
  283.         (rc == TRAP_DLL_LOAD)
  284.       )
  285.      return(rc);
  286.  
  287.     if( rc != 0 )
  288.      Error(rc,TRUE,1,pModuleName);
  289.    }
  290.   }
  291.  }
  292.  return(0);
  293. }
  294. /*****************************************************************************/
  295. /* GetLoadAddr()                                                             */
  296. /*                                                                           */
  297. /* Description:                                                              */
  298. /*                                                                           */
  299. /*   Get the load address of an object number in an mte.                     */
  300. /*                                                                           */
  301. /* Parameters:                                                               */
  302. /*                                                                           */
  303. /*   mte          input - mte of the module the object is in.                */
  304. /*   ObjectNumber input - the object number. duh.                            */
  305. /*                                                                           */
  306. /* Return:                                                                   */
  307. /*                                                                           */
  308. /*   The base address of the Object or 0 if the ObjNum is not in the table.  */
  309. /*                                                                           */
  310. /* Assumptions:                                                              */
  311. /*                                                                           */
  312. /*****************************************************************************/
  313. ULONG GetLoadAddr( ULONG mte, UINT ObjectNumber)
  314. {
  315.  MTE_TABLE_ENTRY *pMteTableEntry;
  316.  ULONG            LoadAddr = 0;
  317.  
  318.  /****************************************************************************/
  319.  /*  - Scan the mte table to find this mte and object number.                */
  320.  /****************************************************************************/
  321.  for(pMteTableEntry = pMteTable ;
  322.                       pMteTable &&
  323.                       ( pMteTableEntry->mte != ENDOFTABLE);
  324.                       pMteTableEntry++ )
  325.  {
  326.   if( (pMteTableEntry->mte == mte) &&
  327.       (pMteTableEntry->ObjNum == ObjectNumber)
  328.     )
  329.   {
  330.    LoadAddr = pMteTableEntry->LoadAddr;
  331.    break;
  332.   }
  333.  }
  334.  /****************************************************************************/
  335.  /* We come here with ObjNum=0. One time this happens is when the FileName   */
  336.  /* record in the debug info has an ObjNum=0. Here's a typical dump:         */
  337.  /*                                                                          */
  338.  /* Absolute       Code          Length    SSTLib   Debug       Module       */
  339.  /* Address     Seg Offset        (0x)     Index    Style    Index  Name     */
  340.  /* ========   =============    ========   ======   =====   ================ */
  341.  /*    .             .             .          .      .              .        */
  342.  /*    .             .             .          .      .              .        */
  343.  /* 00001123     0000:0000        0000        1     None     12 DOSGETDBCSEV */
  344.  /*                                                                          */
  345.  /*                                                                          */
  346.  /****************************************************************************/
  347.  
  348.  return(LoadAddr);
  349. }
  350.  
  351. /*****************************************************************************/
  352. /* FreeMteTable()                                                            */
  353. /*                                                                           */
  354. /* Description:                                                              */
  355. /*                                                                           */
  356. /*   Free the mte table.                                                     */
  357. /*                                                                           */
  358. /* Parameters:                                                               */
  359. /*                                                                           */
  360. /*   none                                                                    */
  361. /*                                                                           */
  362. /* Return:                                                                   */
  363. /*                                                                           */
  364. /*   none                                                                    */
  365. /*                                                                           */
  366. /* Assumptions:                                                              */
  367. /*                                                                           */
  368. /*****************************************************************************/
  369. void FreeMteTable( void )
  370. {
  371.  if( pMteTable )
  372.  {
  373.   Tfree( pMteTable );
  374.   pMteTable = NULL;
  375.   TableSize = 0;
  376.  }
  377. }
  378.  
  379. /*****************************************************************************/
  380. /* CopySharedMteTab()                                                        */
  381. /*                                                                           */
  382. /* Description:                                                              */
  383. /*                                                                           */
  384. /*   Free the mte table.                                                     */
  385. /*                                                                           */
  386. /* Parameters:                                                               */
  387. /*                                                                           */
  388. /*   none                                                                    */
  389. /*                                                                           */
  390. /* Return:                                                                   */
  391. /*                                                                           */
  392. /*   none                                                                    */
  393. /*                                                                           */
  394. /* Assumptions:                                                              */
  395. /*                                                                           */
  396. /*****************************************************************************/
  397. void CopySharedMteTab( ULONG *pMteTabShared )
  398. {
  399.  ULONG len;
  400.  
  401.  len = *pMteTabShared++;
  402.  TableSize = *pMteTabShared++;
  403.  len -= 12;
  404.  
  405.  pMteTable = Talloc(len);
  406.  memcpy( pMteTable, pMteTabShared, len);
  407. }
  408.  
  409. /*****************************************************************************/
  410. /* FreeMte()                                                                 */
  411. /*                                                                           */
  412. /* Description:                                                              */
  413. /*                                                                           */
  414. /*   Free the data structures allocated for this mte and take the            */
  415. /*   structures out of the list.                                             */
  416. /*                                                                           */
  417. /* Parameters:                                                               */
  418. /*                                                                           */
  419. /*   mte   the mte we're freeing up.                                         */
  420. /*                                                                           */
  421. /* Return:                                                                   */
  422. /*                                                                           */
  423. /*   void                                                                    */
  424. /*                                                                           */
  425. /* Assumptions:                                                              */
  426. /*                                                                           */
  427. /*****************************************************************************/
  428. void FreeMte( HMODULE mte )
  429. {
  430.  DEBFILE *pdf;
  431.  DEBFILE *pdfprev;
  432.  BRK     *p;
  433.  BRK     *pnext;
  434.  
  435.  pdf     = pnode->ExeStruct->next;
  436.  pdfprev = pnode->ExeStruct;
  437.  for (;pdf;pdfprev = pdf, pdf = pdf->next )
  438.  {
  439.   if (pdf->mte == mte )
  440.   {
  441.    pdfprev->next = pdf->next;
  442.    freepdf(pdf);
  443.    Tfree((void*)pdf);
  444.    break;
  445.   }
  446.  }
  447.  
  448.  /****************************************************************************/
  449.  /* - now scan the list of breakpoints and remove any breakpoints            */
  450.  /*   defined for this mte.                                                  */
  451.  /****************************************************************************/
  452.  for( p = pnode->allbrks; p ; )
  453.  {
  454.   pnext = p->next;
  455.   if( p->mte == mte )
  456.   {
  457.    if(p->flag.DefineType == BP_LOAD_ADDR )
  458.    {
  459.     p->flag.Reported = 0;
  460.     p->mte           = 0;
  461.    }
  462.    else
  463.     UndBrk(p->brkat, TRUE);
  464.   }
  465.   p = pnext;
  466.  }
  467. }
  468.