PDBImage.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #include "PDBImage.h"
  2. #include <algorithm>
  3. #include "vm/MetadataLock.h"
  4. #include "os/Mutex.h"
  5. #include "Baselib.h"
  6. #include "../interpreter/InterpreterDefs.h"
  7. #include "BlobReader.h"
  8. namespace hybridclr
  9. {
  10. namespace metadata
  11. {
  12. constexpr uint32_t kHiddenLine = 0xfeefee;
  13. baselib::ReentrantLock s_pdbLock;
  14. LoadImageErrorCode PDBImage::LoadCLIHeader(uint32_t& entryPointToken, uint32_t& metadataRva, uint32_t& metadataSize)
  15. {
  16. entryPointToken = 0;
  17. metadataRva = 0;
  18. metadataSize = _imageLength;
  19. _sections.push_back({ 0, _imageLength, 0 });
  20. return LoadImageErrorCode::OK;
  21. }
  22. uint32_t PDBImage::FindILOffsetByIROffset(const il2cpp::utils::dynamic_array<ILMapper>& ilMapper, uint32_t irOffset)
  23. {
  24. auto it = std::upper_bound(ilMapper.begin(), ilMapper.end(), irOffset, [](uint32_t irOffset, const ILMapper& mapper) { return irOffset < mapper.irOffset; });
  25. if (it != ilMapper.begin())
  26. {
  27. auto next = it;
  28. if (next != ilMapper.end())
  29. {
  30. IL2CPP_ASSERT(next->irOffset > irOffset);
  31. }
  32. --it;
  33. IL2CPP_ASSERT(it->irOffset <= irOffset);
  34. return it->ilOffset;
  35. }
  36. return 0;
  37. }
  38. const PDBImage::SymbolSequencePoint* PDBImage::FindSequencePoint(const il2cpp::utils::dynamic_array<SymbolSequencePoint>& sequencePoints, uint32_t ilOffset)
  39. {
  40. auto it = std::upper_bound(sequencePoints.begin(), sequencePoints.end(), ilOffset, [](uint32_t ilOffset, const SymbolSequencePoint& ssp) { return ilOffset < ssp.ilOffset; });
  41. if (it != sequencePoints.begin())
  42. {
  43. auto next = it;
  44. if (next != sequencePoints.end())
  45. {
  46. IL2CPP_ASSERT(next->ilOffset > ilOffset);
  47. }
  48. --it;
  49. IL2CPP_ASSERT(it->ilOffset <= ilOffset);
  50. return &(*it);
  51. }
  52. return nullptr;
  53. }
  54. const PDBImage::SymbolDocumentData* PDBImage::GetDocument(uint32_t documentToken)
  55. {
  56. const Table& tableMeta = GetTable(TableType::DOCUMENT);
  57. uint32_t rowIndex = DecodeTokenRowIndex(documentToken);
  58. if (rowIndex == 0 || rowIndex > tableMeta.rowNum)
  59. {
  60. return nullptr;
  61. }
  62. il2cpp::os::FastAutoLock lock(&s_pdbLock);
  63. auto it = _documents.find(documentToken);
  64. if (it != _documents.end())
  65. {
  66. return it->second;
  67. }
  68. TbSymbolDocument document = ReadSymbolDocument(rowIndex);
  69. SymbolDocumentData* documentData = new (HYBRIDCLR_MALLOC_ZERO(sizeof(SymbolDocumentData))) SymbolDocumentData();
  70. BlobReader reader = GetBlobReaderByRawIndex(document.name);
  71. // FIXME. this is a utf-8 char.
  72. char sep = (char)reader.ReadByte();
  73. std::string sourceFileNames;
  74. bool first = true;
  75. while (reader.NonEmpty())
  76. {
  77. if (sep && !first)
  78. {
  79. sourceFileNames.push_back(sep);
  80. }
  81. uint32_t sourceFileNameIndex = reader.ReadCompressedUint32();
  82. if (sourceFileNameIndex > 0)
  83. {
  84. BlobReader sourceFileNameReader = GetBlobReaderByRawIndex(sourceFileNameIndex);
  85. sourceFileNames.append((const char*)sourceFileNameReader.GetData(), sourceFileNameReader.GetLength());
  86. }
  87. first = false;
  88. }
  89. documentData->sourceFiles = CopyString(sourceFileNames.c_str());
  90. _documents.add(documentToken, documentData);
  91. return documentData;
  92. }
  93. void PDBImage::SetupStackFrameInfo(const MethodInfo* method, const void* ip, Il2CppStackFrameInfo& stackFrame)
  94. {
  95. auto it = _methodInfos.find(method);
  96. if (it == _methodInfos.end())
  97. {
  98. return;
  99. }
  100. const SymbolMethodInfoData* methodInfoData = it->second;
  101. const SymbolMethodDefData* methodData = methodInfoData->methodData;
  102. const hybridclr::interpreter::InterpMethodInfo* imi = (const hybridclr::interpreter::InterpMethodInfo*)method->interpData;
  103. const byte* actualIp;
  104. if (ip >= imi->codes && ip < imi->codes + imi->codeLength)
  105. {
  106. actualIp = (const byte*)ip;
  107. }
  108. else
  109. {
  110. actualIp = *(byte**)ip;
  111. IL2CPP_ASSERT(actualIp >= imi->codes && actualIp < imi->codes + imi->codeLength);
  112. }
  113. uint32_t irOffset = (uint32_t)((uintptr_t)actualIp - (uintptr_t)imi->codes);
  114. uint32_t ilOffset = FindILOffsetByIROffset(methodInfoData->ilMapper, irOffset);
  115. // when call sub interpreter method, ip point to next instruction, so we need to adjust ilOffset.
  116. if (ilOffset > 0)
  117. {
  118. --ilOffset;
  119. }
  120. const SymbolSequencePoint* ssp = FindSequencePoint(methodData->sequencePoints, ilOffset);
  121. if (!ssp)
  122. {
  123. return;
  124. }
  125. stackFrame.ilOffset = ilOffset;
  126. stackFrame.sourceCodeLineNumber = ssp->line;
  127. stackFrame.filePath = GetDocumentName(ssp->document);
  128. }
  129. void PDBImage::SetMethodDebugInfo(const MethodInfo* method, const il2cpp::utils::dynamic_array<ILMapper>& ilMapper)
  130. {
  131. il2cpp::os::FastAutoLock lock(&s_pdbLock);
  132. IL2CPP_ASSERT(_methodInfos.find(method) == _methodInfos.end());
  133. SymbolMethodInfoData* methodInfoData = new (HYBRIDCLR_MALLOC_ZERO(sizeof(SymbolMethodInfoData))) SymbolMethodInfoData();
  134. methodInfoData->methodData = GetMethodDataFromCache(method->token);
  135. IL2CPP_ASSERT(methodInfoData->methodData);
  136. methodInfoData->ilMapper = ilMapper;
  137. _methodInfos.add(method, methodInfoData);
  138. }
  139. PDBImage::SymbolMethodDefData* PDBImage::GetMethodDataFromCache(uint32_t methodToken)
  140. {
  141. const Table& tableMeta = GetTable(TableType::METHODBODY);
  142. uint32_t rowIndex = hybridclr::metadata::DecodeTokenRowIndex(methodToken);
  143. if (rowIndex == 0 || rowIndex > tableMeta.rowNum)
  144. {
  145. return nullptr;
  146. }
  147. il2cpp::os::FastAutoLock lock(&s_pdbLock);
  148. auto it = _methods.find(methodToken);
  149. if (it != _methods.end())
  150. {
  151. return it->second;
  152. }
  153. SymbolMethodDefData* methodData = new (HYBRIDCLR_MALLOC_ZERO(sizeof(SymbolMethodDefData))) SymbolMethodDefData();
  154. // see https://github.com/dotnet/runtime/blob/main/docs/design/specs/PortablePdb-Metadata.md
  155. TbSymbolMethodBody smb = ReadSymbolMethodBody(rowIndex);
  156. methodData->document = smb.document;
  157. if (smb.sequencePoints > 0)
  158. {
  159. auto& sequencePoints = methodData->sequencePoints;
  160. BlobReader reader = GetBlobReaderByRawIndex(smb.sequencePoints);
  161. uint32_t localSignature = reader.ReadCompressedUint32();
  162. uint32_t document = smb.document ? smb.document : reader.ReadCompressedUint32();
  163. int32_t prevStartLine = -1;
  164. int32_t prevStartColumn = -1;
  165. while (reader.NonEmpty())
  166. {
  167. uint32_t deltaIlOffset = reader.ReadCompressedUint32();
  168. // document record
  169. if (deltaIlOffset == 0 && !sequencePoints.empty())
  170. {
  171. document = reader.ReadCompressedUint32();
  172. continue;
  173. }
  174. uint32_t deltaLines = reader.ReadCompressedUint32();
  175. int32_t deltaColumns = deltaLines == 0 ? reader.ReadCompressedUint32() : reader.ReadCompressedInt32();
  176. uint32_t ilOffset = sequencePoints.empty() ? deltaIlOffset : sequencePoints.back().ilOffset + deltaIlOffset;
  177. SymbolSequencePoint ssp = {};
  178. ssp.document = document;
  179. ssp.ilOffset = ilOffset;
  180. // hidden-sequence-point record
  181. if (deltaLines == 0 && deltaColumns == 0)
  182. {
  183. ssp.line = ssp.endLine = kHiddenLine;
  184. ssp.column = ssp.endColumn = 0;
  185. }
  186. else
  187. {
  188. // sequence-point record
  189. if (prevStartColumn < 0)
  190. {
  191. prevStartLine = reader.ReadCompressedUint32();
  192. prevStartColumn = reader.ReadCompressedUint32();
  193. }
  194. else
  195. {
  196. prevStartLine += reader.ReadCompressedInt32();
  197. prevStartColumn += reader.ReadCompressedInt32();
  198. }
  199. ssp.line = prevStartLine;
  200. ssp.endLine = prevStartLine + deltaLines;
  201. ssp.column = prevStartColumn;
  202. ssp.endColumn = prevStartColumn + deltaColumns;
  203. }
  204. sequencePoints.push_back(ssp);
  205. }
  206. }
  207. _methods.add(methodToken, methodData);
  208. return methodData;
  209. }
  210. }
  211. }