Symbolize.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/DebugInfo/Symbolize/DIFetcher.h"
  20. #include "llvm/DebugInfo/Symbolize/SymbolizableModule.h"
  21. #include "llvm/Object/Binary.h"
  22. #include "llvm/Object/ELFObjectFile.h"
  23. #include "llvm/Object/ObjectFile.h"
  24. #include "llvm/Support/Error.h"
  25. #include <algorithm>
  26. #include <cstdint>
  27. #include <map>
  28. #include <memory>
  29. #include <string>
  30. #include <utility>
  31. #include <vector>
  32. namespace llvm {
  33. namespace symbolize {
  34. using namespace object;
  35. using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
  36. using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
  37. class LLVMSymbolizer {
  38. public:
  39. struct Options {
  40. FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName;
  41. FileLineInfoKind PathStyle = FileLineInfoKind::AbsoluteFilePath;
  42. bool UseSymbolTable = true;
  43. bool Demangle = true;
  44. bool RelativeAddresses = false;
  45. bool UntagAddresses = false;
  46. bool UseDIA = false;
  47. std::string DefaultArch;
  48. std::vector<std::string> DsymHints;
  49. std::string FallbackDebugPath;
  50. std::string DWPName;
  51. std::vector<std::string> DebugFileDirectory;
  52. };
  53. LLVMSymbolizer() = default;
  54. LLVMSymbolizer(const Options &Opts) : Opts(Opts) {}
  55. ~LLVMSymbolizer() { flush(); }
  56. // Overloads accepting ObjectFile does not support COFF currently
  57. Expected<DILineInfo> symbolizeCode(const ObjectFile &Obj,
  58. object::SectionedAddress ModuleOffset);
  59. Expected<DILineInfo> symbolizeCode(const std::string &ModuleName,
  60. object::SectionedAddress ModuleOffset);
  61. Expected<DIInliningInfo>
  62. symbolizeInlinedCode(const ObjectFile &Obj,
  63. object::SectionedAddress ModuleOffset);
  64. Expected<DIInliningInfo>
  65. symbolizeInlinedCode(const std::string &ModuleName,
  66. object::SectionedAddress ModuleOffset);
  67. Expected<DIGlobal> symbolizeData(const ObjectFile &Obj,
  68. object::SectionedAddress ModuleOffset);
  69. Expected<DIGlobal> symbolizeData(const std::string &ModuleName,
  70. object::SectionedAddress ModuleOffset);
  71. Expected<std::vector<DILocal>>
  72. symbolizeFrame(const ObjectFile &Obj, object::SectionedAddress ModuleOffset);
  73. Expected<std::vector<DILocal>>
  74. symbolizeFrame(const std::string &ModuleName,
  75. object::SectionedAddress ModuleOffset);
  76. void flush();
  77. static std::string
  78. DemangleName(const std::string &Name,
  79. const SymbolizableModule *DbiModuleDescriptor);
  80. void addDIFetcher(std::unique_ptr<DIFetcher> Fetcher) {
  81. DIFetchers.push_back(std::move(Fetcher));
  82. }
  83. private:
  84. // Bundles together object file with code/data and object file with
  85. // corresponding debug info. These objects can be the same.
  86. using ObjectPair = std::pair<const ObjectFile *, const ObjectFile *>;
  87. template <typename T>
  88. Expected<DILineInfo>
  89. symbolizeCodeCommon(const T &ModuleSpecifier,
  90. object::SectionedAddress ModuleOffset);
  91. template <typename T>
  92. Expected<DIInliningInfo>
  93. symbolizeInlinedCodeCommon(const T &ModuleSpecifier,
  94. object::SectionedAddress ModuleOffset);
  95. template <typename T>
  96. Expected<DIGlobal> symbolizeDataCommon(const T &ModuleSpecifier,
  97. object::SectionedAddress ModuleOffset);
  98. template <typename T>
  99. Expected<std::vector<DILocal>>
  100. symbolizeFrameCommon(const T &ModuleSpecifier,
  101. object::SectionedAddress ModuleOffset);
  102. /// Returns a SymbolizableModule or an error if loading debug info failed.
  103. /// Only one attempt is made to load a module, and errors during loading are
  104. /// only reported once. Subsequent calls to get module info for a module that
  105. /// failed to load will return nullptr.
  106. Expected<SymbolizableModule *>
  107. getOrCreateModuleInfo(const std::string &ModuleName);
  108. Expected<SymbolizableModule *> getOrCreateModuleInfo(const ObjectFile &Obj);
  109. Expected<SymbolizableModule *>
  110. createModuleInfo(const ObjectFile *Obj, std::unique_ptr<DIContext> Context,
  111. StringRef ModuleName);
  112. ObjectFile *lookUpDsymFile(const std::string &Path,
  113. const MachOObjectFile *ExeObj,
  114. const std::string &ArchName);
  115. ObjectFile *lookUpDebuglinkObject(const std::string &Path,
  116. const ObjectFile *Obj,
  117. const std::string &ArchName);
  118. ObjectFile *lookUpBuildIDObject(const std::string &Path,
  119. const ELFObjectFileBase *Obj,
  120. const std::string &ArchName);
  121. bool findDebugBinary(const std::string &OrigPath,
  122. const std::string &DebuglinkName, uint32_t CRCHash,
  123. std::string &Result);
  124. bool findDebugBinary(const ArrayRef<uint8_t> BuildID, std::string &Result);
  125. /// Returns pair of pointers to object and debug object.
  126. Expected<ObjectPair> getOrCreateObjectPair(const std::string &Path,
  127. const std::string &ArchName);
  128. /// Return a pointer to object file at specified path, for a specified
  129. /// architecture (e.g. if path refers to a Mach-O universal binary, only one
  130. /// object file from it will be returned).
  131. Expected<ObjectFile *> getOrCreateObject(const std::string &Path,
  132. const std::string &ArchName);
  133. std::map<std::string, std::unique_ptr<SymbolizableModule>, std::less<>>
  134. Modules;
  135. /// Contains cached results of getOrCreateObjectPair().
  136. std::map<std::pair<std::string, std::string>, ObjectPair>
  137. ObjectPairForPathArch;
  138. /// Contains parsed binary for each path, or parsing error.
  139. std::map<std::string, OwningBinary<Binary>> BinaryForPath;
  140. /// Parsed object file for path/architecture pair, where "path" refers
  141. /// to Mach-O universal binary.
  142. std::map<std::pair<std::string, std::string>, std::unique_ptr<ObjectFile>>
  143. ObjectForUBPathAndArch;
  144. Options Opts;
  145. SmallVector<std::unique_ptr<DIFetcher>> DIFetchers;
  146. };
  147. } // end namespace symbolize
  148. } // end namespace llvm
  149. #endif // LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
  150. #ifdef __GNUC__
  151. #pragma GCC diagnostic pop
  152. #endif