Replace OwningPtr<T> with std::unique_ptr<T>.
[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_SYMBOLIZE_H
14 #define LLVM_SYMBOLIZE_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 <string>
23
24 namespace llvm {
25
26 using namespace object;
27
28 namespace symbolize {
29
30 class ModuleInfo;
31
32 class LLVMSymbolizer {
33 public:
34   struct Options {
35     bool UseSymbolTable : 1;
36     bool PrintFunctions : 1;
37     bool PrintInlining : 1;
38     bool Demangle : 1;
39     std::string DefaultArch;
40     Options(bool UseSymbolTable = true, bool PrintFunctions = true,
41             bool PrintInlining = true, bool Demangle = true,
42             std::string DefaultArch = "")
43         : UseSymbolTable(UseSymbolTable), PrintFunctions(PrintFunctions),
44           PrintInlining(PrintInlining), Demangle(Demangle),
45           DefaultArch(DefaultArch) {
46     }
47   };
48
49   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
50   ~LLVMSymbolizer() {
51     flush();
52   }
53
54   // Returns the result of symbolization for module name/offset as
55   // a string (possibly containing newlines).
56   std::string
57   symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
58   std::string
59   symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
60   void flush();
61   static std::string DemangleName(const std::string &Name);
62 private:
63   typedef std::pair<Binary*, Binary*> BinaryPair;
64
65   ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
66   /// \brief Returns pair of pointers to binary and debug binary.
67   BinaryPair getOrCreateBinary(const std::string &Path);
68   /// \brief Returns a parsed object file for a given architecture in a
69   /// universal binary (or the binary itself if it is an object file).
70   ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
71
72   std::string printDILineInfo(DILineInfo LineInfo) const;
73
74   // Owns all the parsed binaries and object files.
75   SmallVector<Binary*, 4> ParsedBinariesAndObjects;
76   // Owns module info objects.
77   typedef std::map<std::string, ModuleInfo *> ModuleMapTy;
78   ModuleMapTy Modules;
79   typedef std::map<std::string, BinaryPair> BinaryMapTy;
80   BinaryMapTy BinaryForPath;
81   typedef std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
82       ObjectFileForArchMapTy;
83   ObjectFileForArchMapTy ObjectFileForArch;
84
85   Options Opts;
86   static const char kBadString[];
87 };
88
89 class ModuleInfo {
90 public:
91   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
92
93   DILineInfo symbolizeCode(uint64_t ModuleOffset,
94                            const LLVMSymbolizer::Options &Opts) const;
95   DIInliningInfo symbolizeInlinedCode(
96       uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
97   bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
98                      uint64_t &Size) const;
99
100 private:
101   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
102                               std::string &Name, uint64_t &Addr,
103                               uint64_t &Size) const;
104   void addSymbol(const symbol_iterator &Sym);
105   ObjectFile *Module;
106   std::unique_ptr<DIContext> DebugInfoContext;
107
108   struct SymbolDesc {
109     uint64_t Addr;
110     // If size is 0, assume that symbol occupies the whole memory range up to
111     // the following symbol.
112     uint64_t Size;
113     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
114       return s1.Addr < s2.Addr;
115     }
116   };
117   typedef std::map<SymbolDesc, StringRef> SymbolMapTy;
118   SymbolMapTy Functions;
119   SymbolMapTy Objects;
120 };
121
122 } // namespace symbolize
123 } // namespace llvm
124
125 #endif // LLVM_SYMBOLIZE_H