Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

159 lines
4.5 KiB

  1. /*
  2. * This code implements the old MapDebugInformation API.
  3. */
  4. #if defined(_WIN64)
  5. #error "This doesn't build in 64 bits!"
  6. #endif
  7. #include <pch.h>
  8. PIMAGE_DEBUG_INFORMATION
  9. IMAGEAPI
  10. MapDebugInformation(
  11. HANDLE FileHandle,
  12. LPSTR FileName,
  13. LPSTR SymbolPath,
  14. ULONG ImageBase
  15. )
  16. // Here's what we're going to try. MapDebugInformation was only
  17. // documented as returning COFF symbolic and every user I can find
  18. // in the tree uses COFF exclusively. Rather than try to make this
  19. // api do everything possible, let's just leave it as a COFF only thing.
  20. // The new debug info api (GetDebugData) will be internal only.
  21. {
  22. PIMAGE_DEBUG_INFORMATION pIDI;
  23. CHAR szName[_MAX_FNAME];
  24. CHAR szExt[_MAX_EXT];
  25. PIMGHLP_DEBUG_DATA pIDD;
  26. PPIDI pPIDI;
  27. DWORD sections;
  28. BOOL SymbolsLoaded;
  29. HANDLE hProcess;
  30. LPSTR sz;
  31. HANDLE hdb;
  32. DWORD dw;
  33. DWORD len;
  34. hProcess = GetCurrentProcess();
  35. pIDD = GetIDD(FileHandle, FileName, SymbolPath, ImageBase, NO_PE64_IMAGES);
  36. if (!pIDD)
  37. return NULL;
  38. pPIDI = (PPIDI)MemAlloc(sizeof(PIDI));
  39. if (!pPIDI)
  40. return NULL;
  41. ZeroMemory(pPIDI, sizeof(PIDI));
  42. pIDI = &pPIDI->idi;
  43. pPIDI->hdr.pIDD = pIDD;
  44. pIDI->ReservedSize = sizeof(IMAGE_DEBUG_INFORMATION);
  45. pIDI->ReservedMachine = pIDD->Machine;
  46. pIDI->ReservedCharacteristics = (USHORT)pIDD->Characteristics;
  47. pIDI->ReservedCheckSum = pIDD->CheckSum;
  48. pIDI->ReservedTimeDateStamp = pIDD->TimeDateStamp;
  49. pIDI->ReservedRomImage = pIDD->fROM;
  50. // read info
  51. InitializeListHead( &pIDI->List );
  52. pIDI->ImageBase = (ULONG)pIDD->ImageBaseFromImage;
  53. len = strlen(pIDD->ImageFilePath) + 1;
  54. pIDI->ImageFilePath = (PSTR)MemAlloc(len);
  55. if (pIDI->ImageFilePath) {
  56. CopyString(pIDI->ImageFilePath, pIDD->ImageFilePath, len);
  57. }
  58. len = strlen(pIDD->OriginalImageFileName) + 1;
  59. pIDI->ImageFileName = (PSTR)MemAlloc(len);
  60. if (pIDI->ImageFileName) {
  61. CopyString(pIDI->ImageFileName, pIDD->OriginalImageFileName, len);
  62. }
  63. if (pIDD->pMappedCoff) {
  64. pIDI->CoffSymbols = (PIMAGE_COFF_SYMBOLS_HEADER)MemAlloc(pIDD->cMappedCoff);
  65. if (pIDI->CoffSymbols) {
  66. memcpy(pIDI->CoffSymbols, pIDD->pMappedCoff, pIDD->cMappedCoff);
  67. }
  68. pIDI->SizeOfCoffSymbols = pIDD->cMappedCoff;
  69. }
  70. if (pIDD->pFpo) {
  71. pIDI->ReservedNumberOfFpoTableEntries = pIDD->cFpo;
  72. pIDI->ReservedFpoTableEntries = (PFPO_DATA)pIDD->pFpo;
  73. }
  74. pIDI->SizeOfImage = pIDD->SizeOfImage;
  75. if (pIDD->DbgFilePath && *pIDD->DbgFilePath) {
  76. len = strlen(pIDD->DbgFilePath) + 1;
  77. pIDI->ReservedDebugFilePath = (PSTR)MemAlloc(len);
  78. if (pIDI->ReservedDebugFilePath) {
  79. CopyString(pIDI->ReservedDebugFilePath, pIDD->DbgFilePath, len);
  80. }
  81. }
  82. if (pIDD->pMappedCv) {
  83. pIDI->ReservedCodeViewSymbols = pIDD->pMappedCv;
  84. pIDI->ReservedSizeOfCodeViewSymbols = pIDD->cMappedCv;
  85. }
  86. // for backwards compatibility
  87. if (pIDD->ImageMap) {
  88. sections = (DWORD)((char *)pIDD->pCurrentSections - (char *)pIDD->ImageMap);
  89. pIDI->ReservedMappedBase = MapItRO(pIDD->ImageFileHandle);
  90. if (pIDI->ReservedMappedBase) {
  91. pIDI->ReservedSections = (PIMAGE_SECTION_HEADER)pIDD->pCurrentSections;
  92. pIDI->ReservedNumberOfSections = pIDD->cCurrentSections;
  93. if (pIDD->ddva) {
  94. pIDI->ReservedDebugDirectory = (PIMAGE_DEBUG_DIRECTORY)((PCHAR)pIDI->ReservedMappedBase + pIDD->ddva);
  95. pIDI->ReservedNumberOfDebugDirectories = pIDD->cdd;
  96. }
  97. }
  98. }
  99. return pIDI;
  100. }
  101. BOOL
  102. UnmapDebugInformation(
  103. PIMAGE_DEBUG_INFORMATION pIDI
  104. )
  105. {
  106. PPIDI pPIDI;
  107. if (!pIDI)
  108. return true;
  109. if (pIDI->ImageFileName){
  110. MemFree(pIDI->ImageFileName);
  111. }
  112. if (pIDI->ImageFilePath) {
  113. MemFree(pIDI->ImageFilePath);
  114. }
  115. if (pIDI->ReservedDebugFilePath) {
  116. MemFree(pIDI->ReservedDebugFilePath);
  117. }
  118. if (pIDI->CoffSymbols) {
  119. MemFree(pIDI->CoffSymbols);
  120. }
  121. if (pIDI->ReservedMappedBase) {
  122. UnmapViewOfFile(pIDI->ReservedMappedBase);
  123. }
  124. pPIDI = (PPIDI)(PCHAR)((PCHAR)pIDI - sizeof(PIDI_HEADER));
  125. ReleaseDebugData(pPIDI->hdr.pIDD, IMGHLP_FREE_ALL);
  126. MemFree(pPIDI);
  127. return true;
  128. }