[llvm-symbolizer] Introduce the -dsym-hint option.
[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> Bin) {
85     ParsedBinariesAndObjects.push_back(std::move(Bin.getBinary()));
86     MemoryBuffers.push_back(std::move(Bin.getBuffer()));
87   }
88
89   // Owns module info objects.
90   std::map<std::string, ModuleInfo *> Modules;
91   std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
92       ObjectFileForArch;
93   std::map<std::pair<std::string, std::string>, ObjectPair>
94       ObjectPairForPathArch;
95
96   Options Opts;
97   static const char kBadString[];
98 };
99
100 class ModuleInfo {
101 public:
102   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
103
104   DILineInfo symbolizeCode(uint64_t ModuleOffset,
105                            const LLVMSymbolizer::Options &Opts) const;
106   DIInliningInfo symbolizeInlinedCode(
107       uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
108   bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
109                      uint64_t &Size) const;
110
111 private:
112   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
113                               std::string &Name, uint64_t &Addr,
114                               uint64_t &Size) const;
115   void addSymbol(const SymbolRef &Symbol);
116   ObjectFile *Module;
117   std::unique_ptr<DIContext> DebugInfoContext;
118
119   struct SymbolDesc {
120     uint64_t Addr;
121     // If size is 0, assume that symbol occupies the whole memory range up to
122     // the following symbol.
123     uint64_t Size;
124     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
125       return s1.Addr < s2.Addr;
126     }
127   };
128   std::map<SymbolDesc, StringRef> Functions;
129   std::map<SymbolDesc, StringRef> Objects;
130 };
131
132 } // namespace symbolize
133 } // namespace llvm
134
135 #endif