[LLVMSymbolize] Move printing the description of a global into a separate function...
[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/DebugInfo/Symbolize/SymbolizableModule.h"
19 #include "llvm/Object/MachOUniversal.h"
20 #include "llvm/Object/ObjectFile.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
32 class LLVMSymbolizer {
33 public:
34   struct Options {
35     FunctionNameKind PrintFunctions;
36     bool UseSymbolTable : 1;
37     bool PrintInlining : 1;
38     bool Demangle : 1;
39     bool RelativeAddresses : 1;
40     std::string DefaultArch;
41     std::vector<std::string> DsymHints;
42     Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
43             bool UseSymbolTable = true, bool PrintInlining = true,
44             bool Demangle = true, bool RelativeAddresses = false,
45             std::string DefaultArch = "")
46         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
47           PrintInlining(PrintInlining), Demangle(Demangle),
48           RelativeAddresses(RelativeAddresses), 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                                   const SymbolizableModule *ModInfo);
65
66 private:
67   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
68
69   SymbolizableModule *getOrCreateModuleInfo(const std::string &ModuleName);
70   ObjectFile *lookUpDsymFile(const std::string &Path,
71                              const MachOObjectFile *ExeObj,
72                              const std::string &ArchName);
73
74   /// \brief Returns pair of pointers to object and debug object.
75   ObjectPair getOrCreateObjects(const std::string &Path,
76                                 const std::string &ArchName);
77   /// \brief Returns a parsed object file for a given architecture in a
78   /// universal binary (or the binary itself if it is an object file).
79   ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
80
81   std::string printDILineInfo(DILineInfo LineInfo,
82                               const SymbolizableModule *ModInfo) const;
83   std::string printDIGlobal(DIGlobal Global,
84                             const SymbolizableModule *ModInfo) const;
85
86   // Owns all the parsed binaries and object files.
87   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
88   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
89   void addOwningBinary(OwningBinary<Binary> OwningBin) {
90     std::unique_ptr<Binary> Bin;
91     std::unique_ptr<MemoryBuffer> MemBuf;
92     std::tie(Bin, MemBuf) = OwningBin.takeBinary();
93     ParsedBinariesAndObjects.push_back(std::move(Bin));
94     MemoryBuffers.push_back(std::move(MemBuf));
95   }
96
97   std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
98   std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
99       ObjectFileForArch;
100   std::map<std::pair<std::string, std::string>, ObjectPair>
101       ObjectPairForPathArch;
102
103   Options Opts;
104   static const char kBadString[];
105 };
106
107 } // namespace symbolize
108 } // namespace llvm
109
110 #endif