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/MemoryBuffer.h"
27 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
28 using namespace object;
34 class LLVMSymbolizer {
37 bool UseSymbolTable : 1;
38 FunctionNameKind PrintFunctions;
39 bool PrintInlining : 1;
41 std::string DefaultArch;
42 std::vector<std::string> DsymHints;
43 Options(bool UseSymbolTable = true,
44 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
45 bool PrintInlining = true, bool Demangle = true,
46 std::string DefaultArch = "")
47 : UseSymbolTable(UseSymbolTable),
48 PrintFunctions(PrintFunctions), PrintInlining(PrintInlining),
49 Demangle(Demangle), 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);
66 typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
68 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
69 ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
70 const std::string &ArchName);
72 /// \brief Returns pair of pointers to object and debug object.
73 ObjectPair getOrCreateObjects(const std::string &Path,
74 const std::string &ArchName);
75 /// \brief Returns a parsed object file for a given architecture in a
76 /// universal binary (or the binary itself if it is an object file).
77 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
79 std::string printDILineInfo(DILineInfo LineInfo) const;
81 // Owns all the parsed binaries and object files.
82 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
83 SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
84 void addOwningBinary(OwningBinary<Binary> OwningBin) {
85 std::unique_ptr<Binary> Bin;
86 std::unique_ptr<MemoryBuffer> MemBuf;
87 std::tie(Bin, MemBuf) = OwningBin.takeBinary();
88 ParsedBinariesAndObjects.push_back(std::move(Bin));
89 MemoryBuffers.push_back(std::move(MemBuf));
92 // Owns module info objects.
93 std::map<std::string, ModuleInfo *> Modules;
94 std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
96 std::map<std::pair<std::string, std::string>, ObjectPair>
97 ObjectPairForPathArch;
100 static const char kBadString[];
105 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
107 DILineInfo symbolizeCode(uint64_t ModuleOffset,
108 const LLVMSymbolizer::Options &Opts) const;
109 DIInliningInfo symbolizeInlinedCode(
110 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
111 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
112 uint64_t &Size) const;
115 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
116 std::string &Name, uint64_t &Addr,
117 uint64_t &Size) const;
118 void addSymbol(const SymbolRef &Symbol);
120 std::unique_ptr<DIContext> DebugInfoContext;
124 // If size is 0, assume that symbol occupies the whole memory range up to
125 // the following symbol.
127 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
128 return s1.Addr < s2.Addr;
131 std::map<SymbolDesc, StringRef> Functions;
132 std::map<SymbolDesc, StringRef> Objects;
135 } // namespace symbolize