[LLVMSymbolize] Don't use LLVMSymbolizer::Options in ModuleInfo. NFC.
[oota-llvm.git] / include / llvm / DebugInfo / Symbolize / Symbolize.h
1 //===-- Symbolize.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_DEBUGINFO_SYMBOLIZE_SYMBOLIZE_H
14 #define LLVM_DEBUGINFO_SYMBOLIZE_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/DataExtractor.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include <map>
23 #include <memory>
24 #include <string>
25
26 namespace llvm {
27 namespace symbolize {
28
29 using namespace object;
30 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
31 class ModuleInfo;
32
33 class LLVMSymbolizer {
34 public:
35   struct Options {
36     FunctionNameKind PrintFunctions;
37     bool UseSymbolTable : 1;
38     bool PrintInlining : 1;
39     bool Demangle : 1;
40     bool RelativeAddresses : 1;
41     std::string DefaultArch;
42     std::vector<std::string> DsymHints;
43     Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
44             bool UseSymbolTable = true, bool PrintInlining = true,
45             bool Demangle = true, bool RelativeAddresses = false,
46             std::string DefaultArch = "")
47         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
48           PrintInlining(PrintInlining), Demangle(Demangle),
49           RelativeAddresses(RelativeAddresses), 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, ModuleInfo *ModInfo);
65
66 private:
67   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
68
69   ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
70   ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
71                              const std::string &ArchName);
72
73   /// \brief Returns pair of pointers to object and debug object.
74   ObjectPair getOrCreateObjects(const std::string &Path,
75                                 const std::string &ArchName);
76   /// \brief Returns a parsed object file for a given architecture in a
77   /// universal binary (or the binary itself if it is an object file).
78   ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
79
80   std::string printDILineInfo(DILineInfo LineInfo, ModuleInfo *ModInfo) const;
81
82   // Owns all the parsed binaries and object files.
83   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
84   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
85   void addOwningBinary(OwningBinary<Binary> OwningBin) {
86     std::unique_ptr<Binary> Bin;
87     std::unique_ptr<MemoryBuffer> MemBuf;
88     std::tie(Bin, MemBuf) = OwningBin.takeBinary();
89     ParsedBinariesAndObjects.push_back(std::move(Bin));
90     MemoryBuffers.push_back(std::move(MemBuf));
91   }
92
93   std::map<std::string, std::unique_ptr<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, std::unique_ptr<DIContext> DICtx);
106
107   DILineInfo symbolizeCode(uint64_t ModuleOffset, FunctionNameKind FNKind,
108                            bool UseSymbolTable) const;
109   DIInliningInfo symbolizeInlinedCode(uint64_t ModuleOffset,
110                                       FunctionNameKind FNKind,
111                                       bool UseSymbolTable) const;
112   bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
113                      uint64_t &Size) const;
114
115   // Return true if this is a 32-bit x86 PE COFF module.
116   bool isWin32Module() const;
117
118   // Returns the preferred base of the module, i.e. where the loader would place
119   // it in memory assuming there were no conflicts.
120   uint64_t getModulePreferredBase() const;
121
122 private:
123   bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
124                               std::string &Name, uint64_t &Addr,
125                               uint64_t &Size) const;
126   // For big-endian PowerPC64 ELF, OpdAddress is the address of the .opd
127   // (function descriptor) section and OpdExtractor refers to its contents.
128   void addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
129                  DataExtractor *OpdExtractor = nullptr,
130                  uint64_t OpdAddress = 0);
131   void addCoffExportSymbols(const COFFObjectFile *CoffObj);
132   ObjectFile *Module;
133   std::unique_ptr<DIContext> DebugInfoContext;
134
135   struct SymbolDesc {
136     uint64_t Addr;
137     // If size is 0, assume that symbol occupies the whole memory range up to
138     // the following symbol.
139     uint64_t Size;
140     friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
141       return s1.Addr < s2.Addr;
142     }
143   };
144   std::map<SymbolDesc, StringRef> Functions;
145   std::map<SymbolDesc, StringRef> Objects;
146 };
147
148 } // namespace symbolize
149 } // namespace llvm
150
151 #endif