1 //===-- Symbolize.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_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
14 #define LLVM_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_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"
29 using namespace object;
30 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
33 class LLVMSymbolizer {
36 FunctionNameKind PrintFunctions;
37 bool UseSymbolTable : 1;
38 bool PrintInlining : 1;
40 bool RelativeAddresses : 1;
41 std::string DefaultArch;
42 std::vector<std::string> DsymHints;
43 Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
44 bool UseSymbolTable = true, bool PrintInlining = true,
45 bool Demangle = true, bool RelativeAddresses = false,
46 std::string DefaultArch = "")
47 : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
48 PrintInlining(PrintInlining), Demangle(Demangle),
49 RelativeAddresses(RelativeAddresses), DefaultArch(DefaultArch) {}
52 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
57 // Returns the result of symbolization for module name/offset as
58 // a string (possibly containing newlines).
60 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
62 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
64 static std::string DemangleName(const std::string &Name, ModuleInfo *ModInfo);
67 typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
69 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
70 ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
71 const std::string &ArchName);
73 /// \brief Returns pair of pointers to object and debug object.
74 ObjectPair getOrCreateObjects(const std::string &Path,
75 const std::string &ArchName);
76 /// \brief Returns a parsed object file for a given architecture in a
77 /// universal binary (or the binary itself if it is an object file).
78 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
80 std::string printDILineInfo(DILineInfo LineInfo, ModuleInfo *ModInfo) const;
82 // Owns all the parsed binaries and object files.
83 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
84 SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
85 void addOwningBinary(OwningBinary<Binary> OwningBin) {
86 std::unique_ptr<Binary> Bin;
87 std::unique_ptr<MemoryBuffer> MemBuf;
88 std::tie(Bin, MemBuf) = OwningBin.takeBinary();
89 ParsedBinariesAndObjects.push_back(std::move(Bin));
90 MemoryBuffers.push_back(std::move(MemBuf));
93 // Owns module info objects.
94 std::map<std::string, ModuleInfo *> Modules;
95 std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
97 std::map<std::pair<std::string, std::string>, ObjectPair>
98 ObjectPairForPathArch;
101 static const char kBadString[];
106 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
108 DILineInfo symbolizeCode(uint64_t ModuleOffset,
109 const LLVMSymbolizer::Options &Opts) const;
110 DIInliningInfo symbolizeInlinedCode(
111 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
112 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
113 uint64_t &Size) const;
115 // Return true if this is a 32-bit x86 PE COFF module.
116 bool isWin32Module() const;
118 // Returns the preferred base of the module, i.e. where the loader would place
119 // it in memory assuming there were no conflicts.
120 uint64_t getModulePreferredBase() const;
123 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
124 std::string &Name, uint64_t &Addr,
125 uint64_t &Size) const;
126 // For big-endian PowerPC64 ELF, OpdAddress is the address of the .opd
127 // (function descriptor) section and OpdExtractor refers to its contents.
128 void addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
129 DataExtractor *OpdExtractor = nullptr,
130 uint64_t OpdAddress = 0);
131 void addCoffExportSymbols(const COFFObjectFile *CoffObj);
133 std::unique_ptr<DIContext> DebugInfoContext;
137 // If size is 0, assume that symbol occupies the whole memory range up to
138 // the following symbol.
140 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
141 return s1.Addr < s2.Addr;
144 std::map<SymbolDesc, StringRef> Functions;
145 std::map<SymbolDesc, StringRef> Objects;
148 } // namespace symbolize