LLVMSymbolize.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //===-- LLVMSymbolize.h ----------------------------------------- C++ -----===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Header for LLVM symbolization library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
  14. #define LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/DebugInfo/DIContext.h"
  17. #include "llvm/Object/MachOUniversal.h"
  18. #include "llvm/Object/ObjectFile.h"
  19. #include "llvm/Support/DataExtractor.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include <map>
  22. #include <memory>
  23. #include <string>
  24. namespace llvm {
  25. typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
  26. using namespace object;
  27. namespace symbolize {
  28. class ModuleInfo;
  29. class LLVMSymbolizer {
  30. public:
  31. struct Options {
  32. FunctionNameKind PrintFunctions;
  33. bool UseSymbolTable : 1;
  34. bool PrintInlining : 1;
  35. bool Demangle : 1;
  36. bool RelativeAddresses : 1;
  37. std::string DefaultArch;
  38. std::vector<std::string> DsymHints;
  39. Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
  40. bool UseSymbolTable = true, bool PrintInlining = true,
  41. bool Demangle = true, bool RelativeAddresses = false,
  42. std::string DefaultArch = "")
  43. : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
  44. PrintInlining(PrintInlining), Demangle(Demangle),
  45. RelativeAddresses(RelativeAddresses), DefaultArch(DefaultArch) {}
  46. };
  47. LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
  48. ~LLVMSymbolizer() {
  49. flush();
  50. }
  51. // Returns the result of symbolization for module name/offset as
  52. // a string (possibly containing newlines).
  53. std::string
  54. symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
  55. std::string
  56. symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
  57. void flush();
  58. static std::string DemangleName(const std::string &Name);
  59. private:
  60. typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
  61. ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
  62. ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
  63. const std::string &ArchName);
  64. /// \brief Returns pair of pointers to object and debug object.
  65. ObjectPair getOrCreateObjects(const std::string &Path,
  66. const std::string &ArchName);
  67. /// \brief Returns a parsed object file for a given architecture in a
  68. /// universal binary (or the binary itself if it is an object file).
  69. ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
  70. std::string printDILineInfo(DILineInfo LineInfo) const;
  71. // Owns all the parsed binaries and object files.
  72. SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
  73. SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
  74. void addOwningBinary(OwningBinary<Binary> OwningBin) {
  75. std::unique_ptr<Binary> Bin;
  76. std::unique_ptr<MemoryBuffer> MemBuf;
  77. std::tie(Bin, MemBuf) = OwningBin.takeBinary();
  78. ParsedBinariesAndObjects.push_back(std::move(Bin));
  79. MemoryBuffers.push_back(std::move(MemBuf));
  80. }
  81. // Owns module info objects.
  82. std::map<std::string, ModuleInfo *> Modules;
  83. std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
  84. ObjectFileForArch;
  85. std::map<std::pair<std::string, std::string>, ObjectPair>
  86. ObjectPairForPathArch;
  87. Options Opts;
  88. static const char kBadString[];
  89. };
  90. class ModuleInfo {
  91. public:
  92. ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
  93. DILineInfo symbolizeCode(uint64_t ModuleOffset,
  94. const LLVMSymbolizer::Options &Opts) const;
  95. DIInliningInfo symbolizeInlinedCode(
  96. uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
  97. bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
  98. uint64_t &Size) const;
  99. private:
  100. bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
  101. std::string &Name, uint64_t &Addr,
  102. uint64_t &Size) const;
  103. // For big-endian PowerPC64 ELF, OpdAddress is the address of the .opd
  104. // (function descriptor) section and OpdExtractor refers to its contents.
  105. void addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
  106. DataExtractor *OpdExtractor = nullptr,
  107. uint64_t OpdAddress = 0);
  108. ObjectFile *Module;
  109. std::unique_ptr<DIContext> DebugInfoContext;
  110. struct SymbolDesc {
  111. uint64_t Addr;
  112. // If size is 0, assume that symbol occupies the whole memory range up to
  113. // the following symbol.
  114. uint64_t Size;
  115. friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
  116. return s1.Addr < s2.Addr;
  117. }
  118. };
  119. std::map<SymbolDesc, StringRef> Functions;
  120. std::map<SymbolDesc, StringRef> Objects;
  121. };
  122. } // namespace symbolize
  123. } // namespace llvm
  124. #endif