Symbolize.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Symbolize.h ----------------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Header for LLVM symbolization library.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  18. #define LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  19. #include "llvm/ADT/StringMap.h"
  20. #include "llvm/ADT/ilist_node.h"
  21. #include "llvm/ADT/simple_ilist.h"
  22. #include "llvm/DebugInfo/DIContext.h"
  23. #include "llvm/Object/Binary.h"
  24. #include "llvm/Object/BuildID.h"
  25. #include "llvm/Support/Error.h"
  26. #include <algorithm>
  27. #include <cstdint>
  28. #include <map>
  29. #include <memory>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. namespace llvm {
  34. namespace object {
  35. class ELFObjectFileBase;
  36. class MachOObjectFile;
  37. class ObjectFile;
  38. struct SectionedAddress;
  39. } // namespace object
  40. namespace symbolize {
  41. class SymbolizableModule;
  42. using namespace object;
  43. using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
  44. using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
  45. class CachedBinary;
  46. class LLVMSymbolizer {
  47. public:
  48. struct Options {
  49. FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
  50. FileLineInfoKind PathStyle = FileLineInfoKind::AbsoluteFilePath;
  51. bool UseSymbolTable = true;
  52. bool Demangle = true;
  53. bool RelativeAddresses = false;
  54. bool UntagAddresses = false;
  55. bool UseDIA = false;
  56. std::string DefaultArch;
  57. std::vector<std::string> DsymHints;
  58. std::string FallbackDebugPath;
  59. std::string DWPName;
  60. std::vector<std::string> DebugFileDirectory;
  61. size_t MaxCacheSize =
  62. sizeof(size_t) == 4
  63. ? 512 * 1024 * 1024 /* 512 MiB */
  64. : static_cast<size_t>(4ULL * 1024 * 1024 * 1024) /* 4 GiB */;
  65. };
  66. LLVMSymbolizer();
  67. LLVMSymbolizer(const Options &Opts);
  68. ~LLVMSymbolizer();
  69. // Overloads accepting ObjectFile does not support COFF currently
  70. Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
  71. object::SectionedAddress ModuleOffset);
  72. Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
  73. object::SectionedAddress ModuleOffset);
  74. Expected<DILineInfo> symbolizeCode(ArrayRef<uint8_t> BuildID,
  75. object::SectionedAddress ModuleOffset);
  76. Expected<DIInliningInfo>
  77. symbolizeInlinedCode(const ObjectFile &Obj,
  78. object::SectionedAddress ModuleOffset);
  79. Expected<DIInliningInfo>
  80. symbolizeInlinedCode(const std::string &ModuleName,
  81. object::SectionedAddress ModuleOffset);
  82. Expected<DIInliningInfo>
  83. symbolizeInlinedCode(ArrayRef<uint8_t> BuildID,
  84. object::SectionedAddress ModuleOffset);
  85. Expected<DIGlobal> symbolizeData(const ObjectFile &Obj,
  86. object::SectionedAddress ModuleOffset);
  87. Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
  88. object::SectionedAddress ModuleOffset);
  89. Expected<DIGlobal> symbolizeData(ArrayRef<uint8_t> BuildID,
  90. object::SectionedAddress ModuleOffset);
  91. Expected<std::vector<DILocal>>
  92. symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
  93. Expected<std::vector<DILocal>>
  94. symbolizeFrame(const std::string &ModuleName,
  95. object::SectionedAddress ModuleOffset);
  96. Expected<std::vector<DILocal>>
  97. symbolizeFrame(ArrayRef<uint8_t> BuildID,
  98. object::SectionedAddress ModuleOffset);
  99. void flush();
  100. // Evict entries from the binary cache until it is under the maximum size
  101. // given in the options. Calling this invalidates references in the DI...
  102. // objects returned by the methods above.
  103. void pruneCache();
  104. static std::string
  105. DemangleName(const std::string &Name,
  106. const SymbolizableModule *DbiModuleDescriptor);
  107. void setBuildIDFetcher(std::unique_ptr<BuildIDFetcher> Fetcher) {
  108. BIDFetcher = std::move(Fetcher);
  109. }
  110. private:
  111. // Bundles together object file with code/data and object file with
  112. // corresponding debug info. These objects can be the same.
  113. using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
  114. template <typename T>
  115. Expected<DILineInfo>
  116. symbolizeCodeCommon(const T &ModuleSpecifier,
  117. object::SectionedAddress ModuleOffset);
  118. template <typename T>
  119. Expected<DIInliningInfo>
  120. symbolizeInlinedCodeCommon(const T &ModuleSpecifier,
  121. object::SectionedAddress ModuleOffset);
  122. template <typename T>
  123. Expected<DIGlobal> symbolizeDataCommon(const T &ModuleSpecifier,
  124. object::SectionedAddress ModuleOffset);
  125. template <typename T>
  126. Expected<std::vector<DILocal>>
  127. symbolizeFrameCommon(const T &ModuleSpecifier,
  128. object::SectionedAddress ModuleOffset);
  129. /// Returns a SymbolizableModule or an error if loading debug info failed.
  130. /// Only one attempt is made to load a module, and errors during loading are
  131. /// only reported once. Subsequent calls to get module info for a module that
  132. /// failed to load will return nullptr.
  133. Expected<SymbolizableModule *>
  134. getOrCreateModuleInfo(const std::string &ModuleName);
  135. Expected<SymbolizableModule *> getOrCreateModuleInfo(const ObjectFile &Obj);
  136. /// Returns a SymbolizableModule or an error if loading debug info failed.
  137. /// Unlike the above, errors are reported each time, since they are more
  138. /// likely to be transient.
  139. Expected<SymbolizableModule *>
  140. getOrCreateModuleInfo(ArrayRef<uint8_t> BuildID);
  141. Expected<SymbolizableModule *>
  142. createModuleInfo(const ObjectFile *Obj, std::unique_ptr<DIContext> Context,
  143. StringRef ModuleName);
  144. ObjectFile *lookUpDsymFile(const std::string &Path,
  145. const MachOObjectFile *ExeObj,
  146. const std::string &ArchName);
  147. ObjectFile *lookUpDebuglinkObject(const std::string &Path,
  148. const ObjectFile *Obj,
  149. const std::string &ArchName);
  150. ObjectFile *lookUpBuildIDObject(const std::string &Path,
  151. const ELFObjectFileBase *Obj,
  152. const std::string &ArchName);
  153. bool findDebugBinary(const std::string &OrigPath,
  154. const std::string &DebuglinkName, uint32_t CRCHash,
  155. std::string &Result);
  156. bool getOrFindDebugBinary(const ArrayRef<uint8_t> BuildID,
  157. std::string &Result);
  158. /// Returns pair of pointers to object and debug object.
  159. Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
  160. const std::string &ArchName);
  161. /// Return a pointer to object file at specified path, for a specified
  162. /// architecture (e.g. if path refers to a Mach-O universal binary, only one
  163. /// object file from it will be returned).
  164. Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
  165. const std::string &ArchName);
  166. /// Update the LRU cache order when a binary is accessed.
  167. void recordAccess(CachedBinary &Bin);
  168. std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>>
  169. Modules;
  170. StringMap<std::string> BuildIDPaths;
  171. /// Contains cached results of getOrCreateObjectPair().
  172. std::map<std::pair<std::string, std::string>, ObjectPair>
  173. ObjectPairForPathArch;
  174. /// Contains parsed binary for each path, or parsing error.
  175. std::map<std::string, CachedBinary> BinaryForPath;
  176. /// A list of cached binaries in LRU order.
  177. simple_ilist<CachedBinary> LRUBinaries;
  178. /// Sum of the sizes of the cached binaries.
  179. size_t CacheSize = 0;
  180. /// Parsed object file for path/architecture pair, where "path" refers
  181. /// to Mach-O universal binary.
  182. std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
  183. ObjectForUBPathAndArch;
  184. Options Opts;
  185. std::unique_ptr<BuildIDFetcher> BIDFetcher;
  186. };
  187. // A binary intrusively linked into a LRU cache list. If the binary is empty,
  188. // then the entry marks that an error occurred, and it is not part of the LRU
  189. // list.
  190. class CachedBinary : public ilist_node<CachedBinary> {
  191. public:
  192. CachedBinary() = default;
  193. CachedBinary(OwningBinary<Binary> Bin) : Bin(std::move(Bin)) {}
  194. OwningBinary<Binary> &operator*() { return Bin; }
  195. OwningBinary<Binary> *operator->() { return &Bin; }
  196. // Add an action to be performed when the binary is evicted, before all
  197. // previously registered evictors.
  198. void pushEvictor(std::function<void()> Evictor);
  199. // Run all registered evictors in the reverse of the order in which they were
  200. // added.
  201. void evict() {
  202. if (Evictor)
  203. Evictor();
  204. }
  205. size_t size() { return Bin.getBinary()->getData().size(); }
  206. private:
  207. OwningBinary<Binary> Bin;
  208. std::function<void()> Evictor;
  209. };
  210. } // end namespace symbolize
  211. } // end namespace llvm
  212. #endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  213. #ifdef __GNUC__
  214. #pragma GCC diagnostic pop
  215. #endif