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