[LLVMSymbolize] Factor out the logic for printing structs from DIContext. 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/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 Demangle : 1;
38     bool RelativeAddresses : 1;
39     std::string DefaultArch;
40     std::vector<std::string> DsymHints;
41     Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
42             bool UseSymbolTable = true, bool Demangle = true,
43             bool RelativeAddresses = false, std::string DefaultArch = "")
44         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
45           Demangle(Demangle), RelativeAddresses(RelativeAddresses),
46           DefaultArch(DefaultArch) {}
47   };
48
49   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
50   ~LLVMSymbolizer() {
51     flush();
52   }
53
54   DILineInfo symbolizeCode(const std::string &ModuleName,
55                            uint64_t ModuleOffset);
56   DIInliningInfo symbolizeInlinedCode(const std::string &ModuleName,
57                                       uint64_t ModuleOffset);
58   DIGlobal symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
59   void flush();
60   static std::string DemangleName(const std::string &Name,
61                                   const SymbolizableModule *ModInfo);
62
63 private:
64   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
65
66   SymbolizableModule *getOrCreateModuleInfo(const std::string &ModuleName);
67   ObjectFile *lookUpDsymFile(const std::string &Path,
68                              const MachOObjectFile *ExeObj,
69                              const std::string &ArchName);
70
71   /// \brief Returns pair of pointers to object and debug object.
72   ObjectPair getOrCreateObjects(const std::string &Path,
73                                 const std::string &ArchName);
74   /// \brief Returns a parsed object file for a given architecture in a
75   /// universal binary (or the binary itself if it is an object file).
76   ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
77
78   // Owns all the parsed binaries and object files.
79   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
80   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
81   void addOwningBinary(OwningBinary<Binary> OwningBin) {
82     std::unique_ptr<Binary> Bin;
83     std::unique_ptr<MemoryBuffer> MemBuf;
84     std::tie(Bin, MemBuf) = OwningBin.takeBinary();
85     ParsedBinariesAndObjects.push_back(std::move(Bin));
86     MemoryBuffers.push_back(std::move(MemBuf));
87   }
88
89   std::map<std::string, std::unique_ptr<SymbolizableModule>> Modules;
90   std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
91       ObjectFileForArch;
92   std::map<std::pair<std::string, std::string>, ObjectPair>
93       ObjectPairForPathArch;
94
95   Options Opts;
96 };
97
98 } // namespace symbolize
99 } // namespace llvm
100
101 #endif