[Object] Modify OwningBinary's interface to separate inspection from ownership.
[oota-llvm.git] / tools / llvm-symbolizer / LLVMSymbolize.h
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
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"
21 #include <map>
22 #include <memory>
23 #include <string>
24
25 namespace llvm {
26
27 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
28 using namespace object;
29
30 namespace symbolize {
31
32 class ModuleInfo;
33
34 class LLVMSymbolizer {
35 public:
36   struct Options {
37     bool UseSymbolTable : 1;
38     FunctionNameKind PrintFunctions;
39     bool PrintInlining : 1;
40     bool Demangle : 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) {}
50   };
51
52   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
53   ~LLVMSymbolizer() {
54     flush();
55   }
56
57   // Returns the result of symbolization for module name/offset as
58   // a string (possibly containing newlines).
59   std::string
60   symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
61   std::string
62   symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
63   void flush();
64   static std::string DemangleName(const std::string &Name);
65 private:
66   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
67
68   ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
69   ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
70                              const std::string &ArchName);
71
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);
78
79   std::string printDILineInfo(DILineInfo LineInfo) const;
80
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));
90   }
91
92   // Owns module info objects.
93   std::map<std::string, ModuleInfo *> Modules;
94   std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
95       ObjectFileForArch;
96   std::map<std::pair<std::string, std::string>, ObjectPair>
97       ObjectPairForPathArch;
98
99   Options Opts;
100   static const char kBadString[];
101 };
102
103 class ModuleInfo {
104 public:
105   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
106
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;
113
114 private:
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);
119   ObjectFile *Module;
120   std::unique_ptr<DIContext> DebugInfoContext;
121
122   struct SymbolDesc {
123     uint64_t Addr;
124     // If size is 0, assume that symbol occupies the whole memory range up to
125     // the following symbol.
126     uint64_t Size;
127     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
128       return s1.Addr < s2.Addr;
129     }
130   };
131   std::map<SymbolDesc, StringRef> Functions;
132   std::map<SymbolDesc, StringRef> Objects;
133 };
134
135 } // namespace symbolize
136 } // namespace llvm
137
138 #endif