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

  1. /*
  2. **  Sherlock - Copyright 1992, 1993, 1994
  3. **    Harfmann Software
  4. **    Compuserve: 73147,213
  5. **    All rights reserved
  6. */
  7. /*
  8. ** HLL 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    "HLL.h"
  21.  
  22. /*
  23. ** Find the offset of a line given a file name.
  24. */
  25. ULONG HLLFindSourceLine(DebugModule *module, int line, char *fileName)
  26. {
  27. HLLModule  *hllMod = ((HLLAuxData *) module->AuxData)->moduleData;
  28. USHORT        i;
  29. SrcLine    *lineData;
  30.  
  31.     /*
  32.     ** Find the source module.
  33.     */
  34.     for(; hllMod; hllMod=hllMod->next) {
  35.  
  36.     if(hllMod->newLineData == NULL)
  37.         continue;
  38.     if(hllMod->newLineData->entryData.srcLines == NULL)
  39.             continue;
  40.  
  41.         /*
  42.         ** Check for the correct file name.
  43.         */
  44.     if(stricmp(hllMod->newLineData->fileNames[0], fileName) != 0)
  45.             continue;
  46.  
  47.         /*
  48.         ** Read the line/offset pairs.
  49.     */
  50.     lineData = hllMod->newLineData->entryData.srcLines;
  51.     for(i=0; i<hllMod->newLineData->numEntries; i++) {
  52.         USHORT  lineNum;
  53.         ULONG   offset;
  54.  
  55.         lineNum = lineData[i].lineNum;
  56.         if(line == (int) lineNum) {
  57.  
  58.         offset    = lineData[i].offset;
  59.  
  60.         /*
  61.                 ** Find the adjustment for the object.
  62.                 */
  63.         debugBuffer->Value = hllMod->module->segment;
  64.         debugBuffer->MTE   = module->MTE;
  65.         if(DispatchCommand(DBG_C_NumToAddr))
  66.             return 0;
  67.         return offset + debugBuffer->Addr;
  68.             }
  69.         if(line < (int) lineNum) {
  70.                 return 0;
  71.             }
  72.         }
  73.     }
  74.  
  75.     return 0;
  76. }
  77.  
  78. /*
  79. ** Find the source file associated with a given EIP for the module.
  80. */
  81. int HLLFindSource(DebugModule *module, ULONG eipOffset,
  82.             char *funcName, char *sourceName, ULONG *lineNum)
  83. {
  84. HLLModule  *hllMod = ((HLLAuxData *) module->AuxData)->moduleData;
  85. SrcLine    *lineData;
  86. ULONG        relOffset;
  87. ULONG        baseOffset;
  88. USHORT        i;
  89. USHORT        objectNum;
  90.  
  91.     *sourceName = '\0';
  92.     *funcName = '\0';
  93.     *lineNum = 0;
  94.  
  95.     /*
  96.     ** Find out which object and relative offset the EIP is assocated with.
  97.     */
  98.     debugBuffer->Addr = eipOffset;
  99.     debugBuffer->MTE  = module->MTE;
  100.     if(DispatchCommand(DBG_C_AddrToObject))
  101.     return 0;
  102.     baseOffset = debugBuffer->Buffer;
  103.     relOffset  = eipOffset - baseOffset;
  104.  
  105.     /*
  106.     ** Find the object number associated with the address.
  107.     */
  108.     for(objectNum=1;;objectNum++) {
  109.     debugBuffer->Value = (ULONG) objectNum;
  110.     debugBuffer->MTE   = module->MTE;
  111.     if(DispatchCommand(DBG_C_NumToAddr) != DBG_N_Success)
  112.         break;
  113.     if(debugBuffer->Addr == baseOffset)
  114.         break;
  115.     }
  116.  
  117.     /*
  118.     ** Find the source module.
  119.     */
  120.     for(; hllMod; hllMod=hllMod->next) {
  121.     USHORT     totalBytes;
  122.  
  123.     /*
  124.         ** Is this module a possiblity
  125.         */
  126.     if((hllMod->module->offset > relOffset) ||
  127.        ((hllMod->module->offset+hllMod->module->cbSeg) < relOffset))
  128.             continue;
  129.  
  130.     /*
  131.     ** Search the symbol records until we find the function we are looking for.
  132.     */
  133.     totalBytes = hllMod->symbolSize;
  134.     if(hllMod->symbols != NULL) {
  135.         unsigned char    *tmp;
  136.  
  137.         tmp = hllMod->symbols;
  138.         while(totalBytes > 0) {
  139.                 totalBytes -= tmp[0] + 1;
  140.         if(tmp[1] == 0x01 || tmp[1] == 0x0f) {
  141.             BeginBlock *tp;
  142.  
  143.             tp = (BeginBlock *) &tmp[2];
  144.             if((relOffset >= tp->offset) &&
  145.                (relOffset < tp->offset + tp->procLength)) {
  146.             if(tp->name[0] == '_') {
  147.                 strncpy(funcName, &tp->name[1], tp->cbName-1);
  148.                 funcName[tp->cbName-1] = '\0';
  149.             } else {
  150.                 strncpy(funcName, tp->name, tp->cbName);
  151.                 funcName[tp->cbName] = '\0';
  152.             }
  153.             }
  154.         }
  155.                 tmp += tmp[0] + 1;
  156.             }
  157.     }
  158.  
  159.     /*
  160.     ** If there are no symbol records, then check the publics.
  161.     */
  162.     if(hllMod->public != NULL) {
  163.         HLLPublic *public, *best;
  164.  
  165.         best = NULL;
  166.         for(public = hllMod->public; public; public=public->next) {
  167.         if(public->data.segment != objectNum)
  168.             continue;
  169.  
  170.         if(best == NULL)
  171.             best = public;
  172.         if((relOffset >= public->data.offset) &&
  173.             (public->data.offset < best->data.offset)) {
  174.             best = public;
  175.         }
  176.         }
  177.         strcpy(sourceName, "UNKNOWN");
  178.         if(best == NULL) {
  179.         strcpy(funcName, "UNKNOWN");
  180.         } else {
  181.         if(best->data.name[0] == '_')
  182.             strcpy(funcName, &best->data.name[1]);
  183.         else
  184.             strcpy(funcName, best->data.name);
  185.         }
  186.     }
  187.  
  188.     /*
  189.     ** Find the line for the offset.
  190.     */
  191.     if(hllMod->newLineData) {
  192.         lineData = hllMod->newLineData->entryData.srcLines;
  193.         if(lineData && (hllMod->newLineData->entryType == 0)) {
  194.         for(i=1; i<hllMod->newLineData->numEntries; i++) {
  195.             if(relOffset < lineData[i].offset) {
  196.             break;
  197.             }
  198.         }
  199.         *lineNum = lineData[i-1].lineNum;
  200.         strcpy(sourceName, hllMod->newLineData->
  201.             fileNames[lineData[i-1].srcFileIndex-1]);
  202.         } else {
  203.         sourceName[0] = 0;
  204.         }
  205.     }
  206.     return 0;
  207.     }
  208.     return 1;
  209. }
  210.  
  211. /*
  212. ** Find the address of a function given the name.
  213. */
  214. ULONG HLLFindFuncAddr(DebugModule *module, char *funcName)
  215. {
  216. HLLModule  *hllMod = ((HLLAuxData *) module->AuxData)->moduleData;
  217. UCHAR       cbName = (unsigned char) strlen(funcName);
  218.  
  219.     /*
  220.     ** Find the source module.
  221.     */
  222.     for(;hllMod; hllMod=hllMod->next) {
  223.     USHORT        totalBytes;
  224.     unsigned char  *tmp;
  225.  
  226.     if(hllMod->symbols != NULL) {
  227.         tmp = hllMod->symbols;
  228.         totalBytes = hllMod->symbolSize;
  229.         while(totalBytes > 0) {
  230.         totalBytes -= tmp[0] + 1;
  231.  
  232.         /*
  233.         ** 32 Bit procedure.
  234.         */
  235.         if(tmp[1] == 0x01 || tmp[1] == 0x0f) {
  236.             BeginBlock *tp;
  237.  
  238.             tp = (BeginBlock *) &tmp[2];
  239.             if((UCHAR) (cbName+1) == tp->cbName) {
  240.             if(strncmp(funcName, &tp->name[1], cbName-1) == 0) {
  241.                 debugBuffer->MTE = module->MTE;
  242.                 debugBuffer->Value = hllMod->module->segment;
  243.                 if(DispatchCommand(DBG_C_NumToAddr))
  244.                 return 0;
  245.                 return debugBuffer->Addr + tp->offset;
  246.             }
  247.             }
  248.             if(cbName = tp->cbName) {
  249.             if(strncmp(funcName, tp->name, cbName) == 0) {
  250.                 debugBuffer->MTE = module->MTE;
  251.                 debugBuffer->Value = hllMod->module->segment;
  252.                 if(DispatchCommand(DBG_C_NumToAddr))
  253.                 return 0;
  254.                 return debugBuffer->Addr + tp->offset;
  255.             }
  256.             }
  257.         }
  258.  
  259.         /*
  260.         ** 32 Bit code label.
  261.         */
  262.         if(tmp[1] == 0x0b) {
  263.             CodeLabel *cl;
  264.  
  265.             cl = (CodeLabel *) &tmp[2];
  266.             if((UCHAR) (cbName+1) == cl->cbName) {
  267.             if(strncmp(funcName, &cl->name[1], cbName-1) == 0) {
  268.                 debugBuffer->MTE = module->MTE;
  269.                 debugBuffer->Value = hllMod->module->segment;
  270.                 if(DispatchCommand(DBG_C_NumToAddr))
  271.                 return 0;
  272.                 return debugBuffer->Addr + cl->offset;
  273.             }
  274.             }
  275.             if(cbName == cl->cbName) {
  276.             if(strncmp(funcName, cl->name, cbName) == 0) {
  277.                 debugBuffer->MTE = module->MTE;
  278.                 debugBuffer->Value = hllMod->module->segment;
  279.                 if(DispatchCommand(DBG_C_NumToAddr))
  280.                 return 0;
  281.                 return debugBuffer->Addr + cl->offset;
  282.             }
  283.             }
  284.         }
  285.         tmp += tmp[0] + 1;
  286.         }
  287.     }
  288.  
  289.     /*
  290.     ** If there are no symbol records, then check the publics.
  291.     */
  292.     if(hllMod->public != NULL) {
  293.         HLLPublic *public;
  294.  
  295.         for(public = hllMod->public; public; public=public->next) {
  296.         if((strcmp(funcName,  public->data.name) == 0) ||
  297.            (strcmp(funcName, &public->data.name[1]) == 0)) {
  298.             debugBuffer->MTE = module->MTE;
  299.             debugBuffer->Value = public->data.segment;
  300.             if(DispatchCommand(DBG_C_NumToAddr))
  301.             return 0;
  302.             return debugBuffer->Addr + public->data.offset;
  303.         }
  304.         }
  305.     }
  306.     }
  307.     return 0;
  308. }
  309.