DebugSymbolReader.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #include "il2cpp-config.h"
  2. #include "os/File.h"
  3. #include "os/Image.h"
  4. #include "os/Path.h"
  5. #include "utils/Logging.h"
  6. #include "utils/Memory.h"
  7. #include "utils/MemoryMappedFile.h"
  8. #include "utils/PathUtils.h"
  9. #include "utils/StringView.h"
  10. #include "utils/Runtime.h"
  11. #include "vm-utils/DebugSymbolReader.h"
  12. #include "vm/GlobalMetadata.h"
  13. #include "vm/Method.h"
  14. #include "vm/Reflection.h"
  15. #include <string>
  16. #if IL2CPP_TARGET_ARM64E
  17. #include <ptrauth.h>
  18. #endif
  19. namespace il2cpp
  20. {
  21. namespace utils
  22. {
  23. #if !IL2CPP_TINY
  24. struct usymliteHeader
  25. {
  26. uint32_t magic;
  27. uint32_t version;
  28. uint32_t lineCount;
  29. uint32_t id; // executable's id, offset in string table
  30. uint32_t os;
  31. uint32_t arch;
  32. };
  33. struct usymliteLine
  34. {
  35. uint64_t address;
  36. uint32_t methodIndex;
  37. uint32_t fileName; // Reference to the managed source file name in the string table
  38. uint32_t line; // Managed line number
  39. uint32_t parent;
  40. };
  41. struct Reader
  42. {
  43. void* debugSymbolData;
  44. const usymliteLine* lines;
  45. const char* strings;
  46. usymliteHeader header;
  47. std::string uuid;
  48. std::string os;
  49. std::string arch;
  50. uint64_t firstLineAddress;
  51. uint64_t lastLineAddress;
  52. uint32_t maxStringIndex;
  53. };
  54. static Reader s_usym = { 0 };
  55. const int headerSize = 24;
  56. const int lineSize = 24;
  57. const uint32_t magicUsymlite = 0x2D6D7973; // "sym-"
  58. const uint32_t noLine = 0xFFFFFFFF;
  59. // Do a binary search to find the line with the given address
  60. // This is looking for the line with the closest address without going over (price is right style)
  61. usymliteLine FindLine(uint64_t address)
  62. {
  63. uint32_t head = 0;
  64. uint32_t tail = s_usym.header.lineCount - 1;
  65. while (head < tail)
  66. {
  67. uint32_t mid = (head + tail + 1) / 2;
  68. uint64_t midAddr = s_usym.lines[mid].address;
  69. if (address < midAddr)
  70. {
  71. tail = mid - 1;
  72. }
  73. else
  74. {
  75. head = mid;
  76. }
  77. }
  78. uint64_t foundAddr = s_usym.lines[head].address;
  79. // Find the last entry with this address
  80. while (head + 1 < s_usym.header.lineCount && s_usym.lines[head + 1].address == foundAddr)
  81. {
  82. head += 1;
  83. }
  84. return s_usym.lines[head];
  85. }
  86. const char* GetString(uint32_t index)
  87. {
  88. IL2CPP_ASSERT(index < s_usym.maxStringIndex);
  89. return s_usym.strings + index;
  90. }
  91. #define IL2CPP_DEBUG_DUMP_USYM_DATA 0
  92. #if IL2CPP_DEBUG_DUMP_USYM_DATA
  93. static void DumpUsymData()
  94. {
  95. // You may want to change this to be a full path so it is easy to locate.
  96. FILE* dumpFile = fopen("usymData.txt", "w");
  97. uint64_t imageBase = (uint64_t)os::Image::GetImageBase();
  98. for (uint32_t i = 0; i < s_usym.header.lineCount; i++)
  99. {
  100. if (s_usym.lines[i].methodIndex != noLine)
  101. {
  102. uint64_t address = s_usym.lines[i].address;
  103. void* actualAddress = (void*)(s_usym.lines[i].address + imageBase);
  104. const MethodInfo* methodInfo = vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(s_usym.lines[i].methodIndex);
  105. uint32_t methodIndex = s_usym.lines[i].methodIndex;
  106. const char* filePath = GetString(s_usym.lines[i].fileName);
  107. uint32_t sourceCodeLineNumber = s_usym.lines[i].line;
  108. uint32_t parent = s_usym.lines[i].parent;
  109. if (methodInfo != NULL)
  110. fprintf(dumpFile, "%d [%p, %llu] Method Index: %d %s %s(%d) parent: %d\n", i, actualAddress, address, methodIndex, vm::Method::GetFullName(methodInfo).c_str(), filePath, sourceCodeLineNumber, parent);
  111. }
  112. }
  113. fclose(dumpFile);
  114. }
  115. #endif
  116. bool DebugSymbolReader::LoadDebugSymbols()
  117. {
  118. int error = 0;
  119. std::string symbolsPath;
  120. const StringView<char> symbolFileName = "il2cpp.usym";
  121. // First, look for the symbol file next to the executable.
  122. std::string applicationFolder = os::Path::GetApplicationFolder();
  123. if (!applicationFolder.empty())
  124. symbolsPath = PathUtils::Combine(applicationFolder, symbolFileName);
  125. os::FileHandle* symbolsFileHandle = NULL;
  126. if (!symbolsPath.empty())
  127. symbolsFileHandle = os::File::Open(symbolsPath.c_str(), kFileModeOpen, kFileAccessRead, kFileShareRead, kFileOptionsNone, &error);
  128. // If we don't have a symbol path yet or there was some error opening the file next to the executable, try to
  129. // look in the data directory. For some platforms, the packaging won't allow the file to live next to the
  130. // executable.
  131. if (symbolsPath.empty() || error != 0)
  132. {
  133. symbolsPath = PathUtils::Combine(utils::Runtime::GetDataDir(), symbolFileName);
  134. symbolsFileHandle = os::File::Open(symbolsPath.c_str(), kFileModeOpen, kFileAccessRead, kFileShareRead, kFileOptionsNone, &error);
  135. if (error != 0)
  136. return false;
  137. }
  138. s_usym.debugSymbolData = utils::MemoryMappedFile::Map(symbolsFileHandle);
  139. int64_t length = os::File::GetLength(symbolsFileHandle, &error);
  140. os::File::Close(symbolsFileHandle, &error);
  141. if (error != 0)
  142. {
  143. utils::MemoryMappedFile::Unmap(s_usym.debugSymbolData);
  144. s_usym.debugSymbolData = NULL;
  145. return false;
  146. }
  147. s_usym.header = *(usymliteHeader *)((char *)s_usym.debugSymbolData);
  148. if (s_usym.header.magic != magicUsymlite || s_usym.header.lineCount == 0)
  149. {
  150. utils::MemoryMappedFile::Unmap(s_usym.debugSymbolData);
  151. s_usym.debugSymbolData = NULL;
  152. return false;
  153. }
  154. int64_t lineOffset = headerSize;
  155. int64_t stringOffset = lineOffset + (s_usym.header.lineCount * lineSize);
  156. s_usym.maxStringIndex = (uint32_t)(length - stringOffset);
  157. s_usym.lines = (const usymliteLine*)((const char *)s_usym.debugSymbolData + lineOffset);
  158. s_usym.strings = ((const char *)s_usym.debugSymbolData + stringOffset);
  159. #if IL2CPP_ENABLE_NATIVE_INSTRUCTION_POINTER_EMISSION
  160. char* our_uuid = os::Image::GetImageUUID();
  161. s_usym.uuid = std::string(GetString(s_usym.header.id));
  162. if (our_uuid == NULL || s_usym.uuid != our_uuid)
  163. {
  164. // UUID mismatch means this usymfile is not for this program
  165. il2cpp::utils::Logging::Write("Ignoring symbol file due to UUID mismatch. File contains %s but expected %s.", s_usym.uuid.c_str(), our_uuid);
  166. utils::MemoryMappedFile::Unmap(s_usym.debugSymbolData);
  167. s_usym.debugSymbolData = NULL;
  168. s_usym.lines = NULL;
  169. s_usym.strings = NULL;
  170. return false;
  171. }
  172. IL2CPP_FREE(our_uuid, IL2CPP_MEM_IMAGE);
  173. #endif
  174. s_usym.os = std::string(GetString(s_usym.header.os));
  175. s_usym.arch = std::string(GetString(s_usym.header.arch));
  176. s_usym.firstLineAddress = s_usym.lines[0].address;
  177. s_usym.lastLineAddress = s_usym.lines[s_usym.header.lineCount - 1].address;
  178. #if IL2CPP_DEBUG_DUMP_USYM_DATA
  179. DumpUsymData();
  180. #endif
  181. return true;
  182. }
  183. void InsertStackFrame(usymliteLine line, std::vector<Il2CppStackFrameInfo>* stackFrames)
  184. {
  185. if (line.parent != noLine)
  186. {
  187. InsertStackFrame(s_usym.lines[line.parent], stackFrames);
  188. }
  189. const MethodInfo* methodInfo = vm::GlobalMetadata::GetMethodInfoFromMethodDefinitionIndex(line.methodIndex);
  190. Il2CppStackFrameInfo frameInfo = { 0 };
  191. frameInfo.method = methodInfo;
  192. frameInfo.raw_ip = (uintptr_t)line.address;
  193. frameInfo.filePath = GetString(line.fileName);
  194. frameInfo.sourceCodeLineNumber = line.line;
  195. stackFrames->push_back(frameInfo);
  196. }
  197. bool DebugSymbolReader::AddStackFrames(void* nativeInstructionPointer, std::vector<Il2CppStackFrameInfo>* stackFrames)
  198. {
  199. if (s_usym.debugSymbolData == NULL || nativeInstructionPointer == NULL)
  200. {
  201. return false;
  202. }
  203. // The instruction pointer points to the next address, so to get the address we came from, we subtract 1.
  204. // findLine matches the address to the closest address <= the one we give, so it finds the one we need
  205. uint64_t adjustedAddress = ((uint64_t)nativeInstructionPointer) - ((uint64_t)os::Image::GetImageBase()) - 1;
  206. #if IL2CPP_TARGET_ANDROID
  207. // We don't seem to need to subtract by one for Android
  208. // https://github.com/Unity-Technologies/unity-services-crash/commit/50611fcf29a1d876689942ed1f1cdca23e32c522
  209. adjustedAddress += 1;
  210. #endif
  211. #if IL2CPP_TARGET_ARM64E
  212. adjustedAddress = (uint64_t)ptrauth_strip((void*)adjustedAddress, ptrauth_key_return_address);
  213. #endif
  214. // Quick check to remove anything outside the range
  215. if (adjustedAddress < s_usym.firstLineAddress || s_usym.lastLineAddress < adjustedAddress)
  216. {
  217. return false;
  218. }
  219. usymliteLine line = FindLine(adjustedAddress);
  220. // End of symbol entries are placed to indicate that we're past the end of a C# function.
  221. // These EOS entries have their Line and FileName set to 0xFFFFFFFF
  222. if (line.line == noLine)
  223. {
  224. return false;
  225. }
  226. InsertStackFrame(line, stackFrames);
  227. return true;
  228. }
  229. #endif
  230. bool DebugSymbolReader::DebugSymbolsAvailable()
  231. {
  232. /*
  233. #if IL2CPP_TINY
  234. return false;
  235. #elif IL2CPP_MONO_DEBUGGER
  236. return true;
  237. #else
  238. return s_usym.debugSymbolData != NULL;
  239. #endif
  240. */
  241. return true;
  242. }
  243. } /* namespace utils */
  244. } /* namespace il2cpp */