Changeset 3530 for trunk/kDbg/kDbgModWinDbgHelp.cpp
- Timestamp:
- Aug 20, 2007, 5:42:03 AM (18 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/kDbg/kDbgModWinDbgHelp.cpp
r3529 r3530 63 63 * A dbghelp based PE debug reader. 64 64 */ 65 typedef struct KDBGMOD PE65 typedef struct KDBGMODDBGHELP 66 66 { 67 67 /** The common module core. */ 68 KDBGMOD Core;68 KDBGMOD Core; 69 69 /** The image base. */ 70 70 DWORD64 ImageBase; … … 78 78 * implicit header section.*/ 79 79 IMAGE_SECTION_HEADER aSections[1]; 80 } KDBGMOD PE, *PKDBGMODPE;80 } KDBGMODDBGHELP, *PKDBGMODDBGHELP; 81 81 82 82 … … 102 102 * @returns IPRT status code. 103 103 * 104 * @param pMod PeThe PE debug module instance.104 * @param pModDH The PE debug module instance. 105 105 * @param iSegment The segment number. Special segments are dealt with as well. 106 106 * @param off The segment offset. 107 107 * @param puRVA Where to store the RVA on success. 108 108 */ 109 static int kDbgModPeSegOffToRVA(PKDBGMOD PE pModPe, int32_t iSegment, KDBGADDR off, uint32_t *puRVA)109 static int kDbgModPeSegOffToRVA(PKDBGMODDBGHELP pModDH, int32_t iSegment, KDBGADDR off, uint32_t *puRVA) 110 110 { 111 111 if (iSegment >= 0) 112 112 { 113 kDbgAssertMsgReturn(iSegment < pMod Pe->cSections, ("iSegment=%x cSections=%x\n", iSegment, pModPe->cSections),113 kDbgAssertMsgReturn(iSegment < pModDH->cSections, ("iSegment=%x cSections=%x\n", iSegment, pModDH->cSections), 114 114 KDBG_ERR_INVALID_ADDRESS); 115 kDbgAssertMsgReturn(off < pMod Pe->aSections[iSegment].Misc.VirtualSize,116 ("off=" PRI_KDBGADDR " VirtualSize=%x\n", off, pMod Pe->aSections[iSegment].Misc.VirtualSize),115 kDbgAssertMsgReturn(off < pModDH->aSections[iSegment].Misc.VirtualSize, 116 ("off=" PRI_KDBGADDR " VirtualSize=%x\n", off, pModDH->aSections[iSegment].Misc.VirtualSize), 117 117 KDBG_ERR_INVALID_ADDRESS); 118 *puRVA = pMod Pe->aSections[iSegment].VirtualAddress + (uint32_t)off;118 *puRVA = pModDH->aSections[iSegment].VirtualAddress + (uint32_t)off; 119 119 return 0; 120 120 } … … 122 122 if (iSegment == KDBGSEG_RVA) 123 123 { 124 kDbgAssertMsgReturn(off < pMod Pe->cbImage, ("off=" PRI_KDBGADDR ", cbImage=%x\n", off, pModPe->cbImage),124 kDbgAssertMsgReturn(off < pModDH->cbImage, ("off=" PRI_KDBGADDR ", cbImage=%x\n", off, pModDH->cbImage), 125 125 KDBG_ERR_INVALID_ADDRESS); 126 126 *puRVA = (uint32_t)off; … … 136 136 * @returns IPRT status code. 137 137 * 138 * @param pMod PeThe PE debug module instance.138 * @param pModDH The PE debug module instance. 139 139 * @param uRVA The RVA. 140 140 * @param piSegment Where to store the segment number. 141 141 * @param poff Where to store the segment offset. 142 142 */ 143 static int kDbgModPeRVAToSegOff(PKDBGMOD PE pModPe, uint32_t uRVA, int32_t *piSegment, KDBGADDR *poff)144 { 145 kDbgAssertMsgReturn(uRVA < pMod Pe->cbImage, ("uRVA=%x, cbImage=%x\n", uRVA, pModPe->cbImage),146 KDBG_ERR_INVALID_ADDRESS);147 for (int32_t iSegment = 0; iSegment < pMod Pe->cSections; iSegment++)143 static int kDbgModPeRVAToSegOff(PKDBGMODDBGHELP pModDH, uint32_t uRVA, int32_t *piSegment, KDBGADDR *poff) 144 { 145 kDbgAssertMsgReturn(uRVA < pModDH->cbImage, ("uRVA=%x, cbImage=%x\n", uRVA, pModDH->cbImage), 146 KDBG_ERR_INVALID_ADDRESS); 147 for (int32_t iSegment = 0; iSegment < pModDH->cSections; iSegment++) 148 148 { 149 149 /** @todo should probably be less strict about address in the alignment gaps. */ 150 uint32_t off = uRVA - pMod Pe->aSections[iSegment].VirtualAddress;151 if (off < pMod Pe->aSections[iSegment].Misc.VirtualSize)150 uint32_t off = uRVA - pModDH->aSections[iSegment].VirtualAddress; 151 if (off < pModDH->aSections[iSegment].Misc.VirtualSize) 152 152 { 153 153 *poff = off; … … 163 163 * @copydoc KDBGMODOPS::pfnQueryLine 164 164 */ 165 static int rtDbgModPEQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine)166 { 167 PKDBGMOD PE pModPe = (PKDBGMODPE)pMod;165 static int kdbgModDHQueryLine(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGLINE pLine) 166 { 167 PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)pMod; 168 168 169 169 /* … … 171 171 */ 172 172 uint32_t uRVA; 173 int rc = kDbgModPeSegOffToRVA(pMod Pe, iSegment, off, &uRVA);173 int rc = kDbgModPeSegOffToRVA(pModDH, iSegment, off, &uRVA); 174 174 if (!rc) 175 175 { … … 177 177 IMAGEHLP_LINE64 Line; 178 178 Line.SizeOfStruct = sizeof(Line); 179 if (g_pfnSymGetLineFromAddr64(pMod Pe->hSymInst, pModPe->ImageBase + uRVA, &off, &Line))180 { 181 pLine->RVA = (KDBGADDR)(Line.Address - pMod Pe->ImageBase);182 rc = kDbgModPeRVAToSegOff(pMod Pe, (uint32_t)pLine->RVA, &pLine->iSegment, &pLine->offSegment);179 if (g_pfnSymGetLineFromAddr64(pModDH->hSymInst, pModDH->ImageBase + uRVA, &off, &Line)) 180 { 181 pLine->RVA = (KDBGADDR)(Line.Address - pModDH->ImageBase); 182 rc = kDbgModPeRVAToSegOff(pModDH, (uint32_t)pLine->RVA, &pLine->iSegment, &pLine->offSegment); 183 183 pLine->iLine = Line.LineNumber; 184 184 size_t cchFile = strlen(Line.FileName); … … 202 202 * @copydoc KDBGMODOPS::pfnQuerySymbol 203 203 */ 204 static int rtDbgModPEQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym)205 { 206 PKDBGMOD PE pModPe = (PKDBGMODPE)pMod;204 static int kdbgModDHQuerySymbol(PKDBGMOD pMod, int32_t iSegment, KDBGADDR off, PKDBGSYMBOL pSym) 205 { 206 PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)pMod; 207 207 208 208 /* … … 210 210 */ 211 211 uint32_t uRVA; 212 int rc = kDbgModPeSegOffToRVA(pMod Pe, iSegment, off, &uRVA);212 int rc = kDbgModPeSegOffToRVA(pModDH, iSegment, off, &uRVA); 213 213 if (!rc) 214 214 { … … 221 221 Buf.Sym.SizeOfStruct = sizeof(SYMBOL_INFO); 222 222 Buf.Sym.MaxNameLen = KDBG_SYMBOL_MAX; 223 if (g_pfnSymFromAddr(pMod Pe->hSymInst, pModPe->ImageBase + uRVA, &off, &Buf.Sym))223 if (g_pfnSymFromAddr(pModDH->hSymInst, pModDH->ImageBase + uRVA, &off, &Buf.Sym)) 224 224 { 225 225 pSym->cb = Buf.Sym.Size; … … 241 241 else 242 242 { 243 pSym->RVA = (KDBGADDR)(Buf.Sym.Address - pMod Pe->ImageBase);244 rc = kDbgModPeRVAToSegOff(pMod Pe, (uint32_t)pSym->RVA, &pSym->iSegment, &pSym->offSegment);243 pSym->RVA = (KDBGADDR)(Buf.Sym.Address - pModDH->ImageBase); 244 rc = kDbgModPeRVAToSegOff(pModDH, (uint32_t)pSym->RVA, &pSym->iSegment, &pSym->offSegment); 245 245 } 246 246 pSym->cchName = (uint16_t)Buf.Sym.NameLen; … … 263 263 * @copydoc KDBGMODOPS::pfnClose 264 264 */ 265 static int rtDbgModPEClose(PKDBGMOD pMod)266 { 267 PKDBGMOD PE pModPe = (PKDBGMODPE)pMod;268 269 if (g_pfnSymCleanup(pMod Pe->hSymInst))265 static int kdbgModDHClose(PKDBGMOD pMod) 266 { 267 PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)pMod; 268 269 if (g_pfnSymCleanup(pModDH->hSymInst)) 270 270 return 0; 271 271 … … 280 280 * Methods for a PE module. 281 281 */ 282 static const KDBGMODOPS g_ rtDbgModPEOps =282 static const KDBGMODOPS g_kdbgModDHOps = 283 283 { 284 284 "PE (dbghelp)", 285 rtDbgModPEClose,286 rtDbgModPEQuerySymbol,287 rtDbgModPEQueryLine285 kdbgModDHClose, 286 kdbgModDHQuerySymbol, 287 kdbgModDHQueryLine 288 288 }; 289 289 … … 296 296 * @param pszPath the path to the dbghelp.dll. 297 297 */ 298 static int rtDbgModPETryDbgHelp(const char *pszPath, uint32_t *pu32FileVersionMS, uint32_t *pu32FileVersionLS)298 static int kdbgModDHTryDbgHelp(const char *pszPath, uint32_t *pu32FileVersionMS, uint32_t *pu32FileVersionLS) 299 299 { 300 300 int rc; … … 337 337 * Find the dbghelp.dll 338 338 */ 339 static int rtDbgModPEFindDbgHelp(char *pszPath, size_t cchPath)339 static int kdbgModDHFindDbgHelp(char *pszPath, size_t cchPath) 340 340 { 341 341 /* … … 349 349 { 350 350 strcat(pszPath, s_szDbgHelp); 351 int rc2 = rtDbgModPETryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);351 int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS); 352 352 if (!rc2) 353 353 return rc2; … … 362 362 { 363 363 strcat(strrchr(pszPath, '\\'), s_szDbgHelp); 364 int rc2 = rtDbgModPETryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);364 int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS); 365 365 if (!rc) 366 366 return rc2; … … 375 375 { 376 376 strcat(pszPath, s_szDbgHelp); 377 int rc2 = rtDbgModPETryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);377 int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS); 378 378 if (!rc2) 379 379 return rc2; … … 388 388 { 389 389 strcat(pszPath, s_szDbgHelp); 390 int rc2 = rtDbgModPETryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);390 int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS); 391 391 if (!rc2) 392 392 return rc2; … … 415 415 memcpy(pszPath, psz, pszEnd - psz); 416 416 memcpy(&pszPath[pszEnd - psz], s_szDbgHelp, sizeof(s_szDbgHelp)); 417 int rc2 = rtDbgModPETryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS);417 int rc2 = kdbgModDHTryDbgHelp(pszPath, &FileVersionMS, &FileVersionLS); 418 418 if (!rc2) 419 419 return rc2; … … 447 447 * @returns IPRT status code. 448 448 */ 449 static int rtDbgModPELoadDbgHelp(void)449 static int kdbgModDHLoadDbgHelp(void) 450 450 { 451 451 if (g_hDbgHelp) … … 467 467 */ 468 468 char szPath[260]; 469 int rc = rtDbgModPEFindDbgHelp(szPath, sizeof(szPath));469 int rc = kdbgModDHFindDbgHelp(szPath, sizeof(szPath)); 470 470 if (rc) 471 471 { … … 566 566 * 567 567 */ 568 int kdbgMod PEOpen(PKDBGHLPFILE pFile, int64_t offHdr, const char *pszModulePath, PKDBGMOD *ppDbgMod)568 int kdbgModWinDbgHelpOpen(PKDBGHLPFILE pFile, int64_t offHdr, const char *pszModulePath, PKDBGMOD *ppDbgMod) 569 569 { 570 570 /* … … 589 589 * Load dbghelp.dll. 590 590 */ 591 rc = rtDbgModPELoadDbgHelp();591 rc = kdbgModDHLoadDbgHelp(); 592 592 if (rc) 593 593 return rc; … … 596 596 * Allocate the module and read/construct the section headers. 597 597 */ 598 PKDBGMOD PE pModPe = (PKDBGMODPE)kDbgHlpAlloc(KDBG_OFFSETOF(KDBGMODPE, aSections[FHdr.NumberOfSections + 2]));599 kDbgAssertReturn(pMod Pe, KDBG_ERR_NO_MEMORY);600 pMod Pe->Core.u32Magic = KDBGMOD_MAGIC;601 pMod Pe->Core.pOps = &g_rtDbgModPEOps;602 pMod Pe->Core.pFile = pFile;603 pMod Pe->cbImage = cbImage;604 pMod Pe->cSections = 1 + FHdr.NumberOfSections;598 PKDBGMODDBGHELP pModDH = (PKDBGMODDBGHELP)kDbgHlpAlloc(KDBG_OFFSETOF(KDBGMODDBGHELP, aSections[FHdr.NumberOfSections + 2])); 599 kDbgAssertReturn(pModDH, KDBG_ERR_NO_MEMORY); 600 pModDH->Core.u32Magic = KDBGMOD_MAGIC; 601 pModDH->Core.pOps = &g_kdbgModDHOps; 602 pModDH->Core.pFile = pFile; 603 pModDH->cbImage = cbImage; 604 pModDH->cSections = 1 + FHdr.NumberOfSections; 605 605 rc = kDbgHlpReadAt(pFile, offHdr + KDBG_OFFSETOF(IMAGE_NT_HEADERS32, OptionalHeader) + FHdr.SizeOfOptionalHeader, 606 &pMod Pe->aSections[1], sizeof(pModPe->aSections[0]) * FHdr.NumberOfSections);606 &pModDH->aSections[1], sizeof(pModDH->aSections[0]) * FHdr.NumberOfSections); 607 607 if (!rc) 608 608 { 609 PIMAGE_SECTION_HEADER pSH = &pMod Pe->aSections[0];609 PIMAGE_SECTION_HEADER pSH = &pModDH->aSections[0]; 610 610 memcpy(pSH->Name, "headers", sizeof(pSH->Name)); 611 pSH->Misc.VirtualSize = pMod Pe->aSections[1].VirtualAddress;611 pSH->Misc.VirtualSize = pModDH->aSections[1].VirtualAddress; 612 612 pSH->VirtualAddress = 0; 613 613 pSH->SizeOfRawData = pSH->Misc.VirtualSize; … … 619 619 pSH->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_ALIGN_4BYTES | IMAGE_SCN_MEM_READ; 620 620 621 uint32_t uTheEnd = pMod Pe->aSections[FHdr.NumberOfSections].VirtualAddress622 + pMod Pe->aSections[FHdr.NumberOfSections].Misc.VirtualSize;621 uint32_t uTheEnd = pModDH->aSections[FHdr.NumberOfSections].VirtualAddress 622 + pModDH->aSections[FHdr.NumberOfSections].Misc.VirtualSize; 623 623 if (uTheEnd < cbImage) 624 624 { 625 pSH = &pMod Pe->aSections[pModPe->cSections++];625 pSH = &pModDH->aSections[pModDH->cSections++]; 626 626 memcpy(pSH->Name, "tail\0\0\0", sizeof(pSH->Name)); 627 627 pSH->Misc.VirtualSize = cbImage - uTheEnd; … … 661 661 if (ImageBase) 662 662 { 663 pMod Pe->hSymInst = hSymInst;664 pMod Pe->ImageBase = ImageBase;665 *ppDbgMod = &pMod Pe->Core;663 pModDH->hSymInst = hSymInst; 664 pModDH->ImageBase = ImageBase; 665 *ppDbgMod = &pModDH->Core; 666 666 return rc; 667 667 } … … 682 682 kDbgAssertRC(rc); 683 683 684 kDbgHlpFree(pMod Pe);684 kDbgHlpFree(pModDH); 685 685 return rc; 686 686 }
Note:
See TracChangeset
for help on using the changeset viewer.