230dc759a97e90c9e33feb0c986a2addd5f34aee
[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/ErrorOr.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include <map>
24 #include <memory>
25 #include <string>
26
27 namespace llvm {
28 namespace symbolize {
29
30 using namespace object;
31 using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
32
33 class LLVMSymbolizer {
34 public:
35   struct Options {
36     FunctionNameKind PrintFunctions;
37     bool UseSymbolTable : 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 Demangle = true,
44             bool RelativeAddresses = false, std::string DefaultArch = "")
45         : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
46           Demangle(Demangle), RelativeAddresses(RelativeAddresses),
47           DefaultArch(DefaultArch) {}
48   };
49
50   LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
51   ~LLVMSymbolizer() {
52     flush();
53   }
54
55   ErrorOr<DILineInfo> symbolizeCode(const std::string &ModuleName,
56                                     uint64_t ModuleOffset);
57   ErrorOr<DIInliningInfo> symbolizeInlinedCode(const std::string &ModuleName,
58                                                uint64_t ModuleOffset);
59   ErrorOr<DIGlobal> symbolizeData(const std::string &ModuleName,
60                                   uint64_t ModuleOffset);
61   void flush();
62   static std::string DemangleName(const std::string &Name,
63                                   const SymbolizableModule *ModInfo);
64
65 private:
66   typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
67
68   ErrorOr<SymbolizableModule *>
69   getOrCreateModuleInfo(const std::string &ModuleName);
70   ObjectFile *lookUpDsymFile(const std::string &Path,
71                              const MachOObjectFile *ExeObj,
72                              const std::string &ArchName);
73   ObjectFile *lookUpDebuglinkObject(const std::string &Path,
74                                     const ObjectFile *Obj,
75                                     const std::string &ArchName);
76
77   /// \brief Returns pair of pointers to object and debug object.
78   ErrorOr<ObjectPair> getOrCreateObjects(const std::string &Path,
79                                          const std::string &ArchName);
80   /// \brief Returns a parsed object file for a given architecture in a
81   /// universal binary (or the binary itself if it is an object file).
82   ErrorOr<ObjectFile *> getObjectFileFromBinary(Binary *Bin,
83                                                 const std::string &ArchName);
84
85   // Owns all the parsed binaries and object files.
86   SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
87   SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
88   void addOwningBinary(OwningBinary<Binary> OwningBin) {
89     std::unique_ptr<Binary> Bin;
90     std::unique_ptr<MemoryBuffer> MemBuf;
91     std::tie(Bin, MemBuf) = OwningBin.takeBinary();
92     ParsedBinariesAndObjects.push_back(std::move(Bin));
93     MemoryBuffers.push_back(std::move(MemBuf));
94   }
95
96   std::map<std::string, ErrorOr<std::unique_ptr<SymbolizableModule>>> Modules;
97   std::map<std::pair<MachOUniversalBinary *, std::string>,
98            ErrorOr<ObjectFile *>> ObjectFileForArch;
99   std::map<std::pair<std::string, std::string>, ErrorOr<ObjectPair>>
100       ObjectPairForPathArch;
101
102   Options Opts;
103 };
104
105 } // namespace symbolize
106 } // namespace llvm
107
108 #endif