17b78dc52046d143bda7028b9e6b129401bd342a
[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     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) {}
49   };
50
51   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
52   ~LLVMSymbolizer() {
53     flush();
54   }
55
56   // Returns the result of symbolization for module name/offset as
57   // a string (possibly containing newlines).
58   std::string
59   symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
60   std::string
61   symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
62   void flush();
63   static std::string DemangleName(const std::string &Name);
64 private:
65   typedef std::pair<Binary*, Binary*> BinaryPair;
66
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);
73
74   std::string printDILineInfo(DILineInfo LineInfo) const;
75
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()));
82   }
83
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 *>
88       ObjectFileForArch;
89
90   Options Opts;
91   static const char kBadString[];
92 };
93
94 class ModuleInfo {
95 public:
96   ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
97
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;
104
105 private:
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);
110   ObjectFile *Module;
111   std::unique_ptr<DIContext> DebugInfoContext;
112
113   struct SymbolDesc {
114     uint64_t Addr;
115     // If size is 0, assume that symbol occupies the whole memory range up to
116     // the following symbol.
117     uint64_t Size;
118     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
119       return s1.Addr < s2.Addr;
120     }
121   };
122   std::map<SymbolDesc, StringRef> Functions;
123   std::map<SymbolDesc, StringRef> Objects;
124 };
125
126 } // namespace symbolize
127 } // namespace llvm
128
129 #endif