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 Options(bool UseSymbolTable = true,
43 FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
44 bool PrintInlining = true, bool Demangle = true,
45 std::string DefaultArch = "")
46 : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
47 PrintInlining(PrintInlining), Demangle(Demangle),
48 DefaultArch(DefaultArch) {}
51 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
56 // Returns the result of symbolization for module name/offset as
57 // a string (possibly containing newlines).
59 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
61 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
63 static std::string DemangleName(const std::string &Name);
65 typedef std::pair<Binary*, Binary*> BinaryPair;
67 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
68 /// \brief Returns pair of pointers to binary and debug binary.
69 BinaryPair getOrCreateBinary(const std::string &Path);
70 /// \brief Returns a parsed object file for a given architecture in a
71 /// universal binary (or the binary itself if it is an object file).
72 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
74 std::string printDILineInfo(DILineInfo LineInfo) const;
76 // Owns all the parsed binaries and object files.
77 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
78 SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
79 void addOwningBinary(OwningBinary<Binary> Bin) {
80 ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary()));
81 MemoryBuffers.push_back(std::move(Bin.getBuffer()));
84 // Owns module info objects.
85 std::map<std::string, ModuleInfo *> Modules;
86 std::map<std::string, BinaryPair> BinaryForPath;
87 std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
91 static const char kBadString[];
96 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
98 DILineInfo symbolizeCode(uint64_t ModuleOffset,
99 const LLVMSymbolizer::Options &Opts) const;
100 DIInliningInfo symbolizeInlinedCode(
101 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
102 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
103 uint64_t &Size) const;
106 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
107 std::string &Name, uint64_t &Addr,
108 uint64_t &Size) const;
109 void addSymbol(const SymbolRef &Symbol);
111 std::unique_ptr<DIContext> DebugInfoContext;
115 // If size is 0, assume that symbol occupies the whole memory range up to
116 // the following symbol.
118 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
119 return s1.Addr < s2.Addr;
122 std::map<SymbolDesc, StringRef> Functions;
123 std::map<SymbolDesc, StringRef> Objects;
126 } // namespace symbolize