home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sherlock.zip / SYM / SYMSRC.C < prev    next >
C/C++ Source or Header  |  1994-06-29  |  4KB  |  176 lines

  1. /*
  2. **  Sherlock - Copyright 1992, 1993, 1994
  3. **    Harfmann Software
  4. **    Compuserve: 73147,213
  5. **    All rights reserved
  6. */
  7. /*
  8. ** SYM file interface functions to extract symbolic information
  9. ** given a state to extract information from.
  10. */
  11. #include    <stdio.h>
  12. #include    <string.h>
  13. #include    <sys\stat.h>
  14.  
  15. #define     INCL_DOSPROCESS
  16. #include    <os2.h>
  17. #include    "..\Debug.h"
  18.  
  19. #include    "..\SrcInter.h"
  20. #include    "MapSym.h"
  21.  
  22. /*
  23. ** Find the offset of a line given a file name.
  24. */
  25. ULONG SymFindSourceLine(DebugModule *module, int line, char *fileName)
  26. {
  27.     return 0;
  28. }
  29.  
  30. /*
  31. ** Find the source file associated with a given EIP for the module.
  32. */
  33. int SymFindSource(DebugModule *module, ULONG eipOffset,
  34.             char *funcName, char *sourceName, ULONG *lineNum)
  35. {
  36. SymSegmentData *symData = (SymSegmentData *) module->AuxData;
  37. SymData16  *sym16;
  38. SymData32  *sym32;
  39. ULONG        relOffset;
  40. ULONG        baseOffset;
  41. USHORT        objectNum;
  42.  
  43.     *sourceName = '\0';
  44.     *funcName = '\0';
  45.     *lineNum = 0;
  46.  
  47.     /*
  48.     ** Find out which object and relative offset the EIP is assocated with.
  49.     */
  50.     debugBuffer->Addr = eipOffset;
  51.     debugBuffer->MTE  = module->MTE;
  52.     if(DispatchCommand(DBG_C_AddrToObject))
  53.     return 0;
  54.     baseOffset = debugBuffer->Buffer;
  55.     relOffset  = eipOffset - baseOffset;
  56.  
  57.     /*
  58.     ** Find the object number associated with the address.
  59.     */
  60.     for(objectNum=1;;objectNum++) {
  61.     debugBuffer->Value = (ULONG) objectNum;
  62.     debugBuffer->MTE   = module->MTE;
  63.     if(DispatchCommand(DBG_C_NumToAddr) != DBG_N_Success)
  64.         break;
  65.     if(debugBuffer->Addr == baseOffset)
  66.         break;
  67.     symData = symData->next;
  68.     }
  69.  
  70.     /*
  71.     ** Set the Source file name to the segment name.
  72.     */
  73.     *lineNum = relOffset;
  74.  
  75.     /*
  76.     ** Try to load the 16 byte symbols.
  77.     */
  78.     sym16 = symData->first16Symbol;
  79.     if(sym16) {
  80.     while(sym16->next) {
  81.         if(sym16->next->wSymVal > relOffset) {
  82.         break;
  83.         }
  84.         sym16 = sym16->next;
  85.     }
  86.     if(sym16 == NULL)
  87.         return 0;
  88.     *lineNum = relOffset - ((ULONG) sym16->wSymVal);
  89.     strcpy(funcName, sym16->SymName);
  90.     return 1;
  91.     }
  92.  
  93.     /*
  94.     ** Try the 32 bit symbols.
  95.     */
  96.     sym32 = symData->first32Symbol;
  97.     while(sym32->next) {
  98.     if(sym32->next->lSymVal > relOffset) {
  99.         break;
  100.     }
  101.     sym32 = sym32->next;
  102.     }
  103.     if(sym32 == NULL)
  104.     return 0;
  105.  
  106.     *lineNum = relOffset - ((ULONG) sym32->lSymVal);
  107.     strcpy(funcName, sym32->SymName);
  108.     return 1;
  109. }
  110.  
  111. /*
  112. ** Find the address of a function given the name.
  113. */
  114. ULONG SymFindFuncAddr(DebugModule *module, char *funcName)
  115. {
  116. SymSegmentData *symData = (SymSegmentData *) module->AuxData;
  117. SymData16      *sym16;
  118. SymData32      *sym32;
  119. int        objectNum = 1;
  120. int        flag = 0;
  121. ULONG        relOffset;
  122.  
  123.     /*
  124.     ** Iterate through all segments.
  125.     */
  126.     while(symData) {
  127.  
  128.     /*
  129.     ** Iterate through all symbols for the segment
  130.     */
  131.     sym16 = symData->first16Symbol;
  132.     sym32 = symData->first32Symbol;
  133.     if(sym16) {
  134.         while(sym16) {
  135.         if((strcmp(funcName,  sym16->SymName)     == 0) ||
  136.            (strcmp(funcName, &sym16->SymName[1]) == 0)) {
  137.             relOffset = (ULONG) sym16->wSymVal;
  138.             flag      = 1;
  139.             break;
  140.         }
  141.         sym16 = sym16->next;
  142.         }
  143.     }
  144.  
  145.     if(sym32) {
  146.         while(sym32) {
  147.         if((strcmp(funcName,  sym32->SymName)     == 0) ||
  148.            (strcmp(funcName, &sym32->SymName[1]) == 0)) {
  149.             relOffset = (ULONG) sym32->lSymVal;
  150.             flag      = 1;
  151.             break;
  152.         }
  153.         sym32 = sym32->next;
  154.         }
  155.     }
  156.  
  157.     /*
  158.     ** If we found the function, return
  159.     */
  160.     if(flag) {
  161.         debugBuffer->MTE = module->MTE;
  162.         debugBuffer->Value = objectNum;
  163.         if(DispatchCommand(DBG_C_NumToAddr))
  164.             return 0;
  165.         return debugBuffer->Addr + relOffset;
  166.     }
  167.  
  168.     /*
  169.     ** NEXT!
  170.     */
  171.     symData = symData->next;
  172.     objectNum++;
  173.     }
  174.     return 0;
  175. }
  176.