unique_ptrify LoadedObjectInfo::clone
[oota-llvm.git] / include / llvm / DebugInfo / DIContext.h
1 //===-- DIContext.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 // This file defines DIContext, an abstract data structure that holds
11 // debug information data.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_DEBUGINFO_DICONTEXT_H
16 #define LLVM_DEBUGINFO_DICONTEXT_H
17
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/Object/RelocVisitor.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/DataTypes.h"
24 #include <string>
25 #include <memory>
26
27 namespace llvm {
28
29 class raw_ostream;
30
31 /// DILineInfo - a format-neutral container for source line information.
32 struct DILineInfo {
33   std::string FileName;
34   std::string FunctionName;
35   uint32_t Line;
36   uint32_t Column;
37
38   DILineInfo()
39       : FileName("<invalid>"), FunctionName("<invalid>"), Line(0), Column(0) {}
40
41   bool operator==(const DILineInfo &RHS) const {
42     return Line == RHS.Line && Column == RHS.Column &&
43            FileName == RHS.FileName && FunctionName == RHS.FunctionName;
44   }
45   bool operator!=(const DILineInfo &RHS) const {
46     return !(*this == RHS);
47   }
48 };
49
50 typedef SmallVector<std::pair<uint64_t, DILineInfo>, 16> DILineInfoTable;
51
52 /// DIInliningInfo - a format-neutral container for inlined code description.
53 class DIInliningInfo {
54   SmallVector<DILineInfo, 4> Frames;
55  public:
56   DIInliningInfo() {}
57   DILineInfo getFrame(unsigned Index) const {
58     assert(Index < Frames.size());
59     return Frames[Index];
60   }
61   uint32_t getNumberOfFrames() const {
62     return Frames.size();
63   }
64   void addFrame(const DILineInfo &Frame) {
65     Frames.push_back(Frame);
66   }
67 };
68
69 /// A DINameKind is passed to name search methods to specify a
70 /// preference regarding the type of name resolution the caller wants.
71 enum class DINameKind { None, ShortName, LinkageName };
72
73 /// DILineInfoSpecifier - controls which fields of DILineInfo container
74 /// should be filled with data.
75 struct DILineInfoSpecifier {
76   enum class FileLineInfoKind { None, Default, AbsoluteFilePath };
77   typedef DINameKind FunctionNameKind;
78
79   FileLineInfoKind FLIKind;
80   FunctionNameKind FNKind;
81
82   DILineInfoSpecifier(FileLineInfoKind FLIKind = FileLineInfoKind::Default,
83                       FunctionNameKind FNKind = FunctionNameKind::None)
84       : FLIKind(FLIKind), FNKind(FNKind) {}
85 };
86
87 /// Selects which debug sections get dumped.
88 enum DIDumpType {
89   DIDT_Null,
90   DIDT_All,
91   DIDT_Abbrev,
92   DIDT_AbbrevDwo,
93   DIDT_Aranges,
94   DIDT_Frames,
95   DIDT_Info,
96   DIDT_InfoDwo,
97   DIDT_Types,
98   DIDT_TypesDwo,
99   DIDT_Line,
100   DIDT_LineDwo,
101   DIDT_Loc,
102   DIDT_LocDwo,
103   DIDT_Ranges,
104   DIDT_Pubnames,
105   DIDT_Pubtypes,
106   DIDT_GnuPubnames,
107   DIDT_GnuPubtypes,
108   DIDT_Str,
109   DIDT_StrDwo,
110   DIDT_StrOffsetsDwo,
111   DIDT_AppleNames,
112   DIDT_AppleTypes,
113   DIDT_AppleNamespaces,
114   DIDT_AppleObjC
115 };
116
117 class DIContext {
118 public:
119   enum DIContextKind {
120     CK_DWARF,
121     CK_PDB
122   };
123   DIContextKind getKind() const { return Kind; }
124
125   DIContext(DIContextKind K) : Kind(K) {}
126   virtual ~DIContext() {}
127
128   virtual void dump(raw_ostream &OS, DIDumpType DumpType = DIDT_All) = 0;
129
130   virtual DILineInfo getLineInfoForAddress(uint64_t Address,
131       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
132   virtual DILineInfoTable getLineInfoForAddressRange(uint64_t Address,
133       uint64_t Size, DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
134   virtual DIInliningInfo getInliningInfoForAddress(uint64_t Address,
135       DILineInfoSpecifier Specifier = DILineInfoSpecifier()) = 0;
136 private:
137   const DIContextKind Kind;
138 };
139
140 /// An inferface for inquiring the load address of a loaded object file
141 /// to be used by the DIContext implementations when applying relocations
142 /// on the fly.
143 class LoadedObjectInfo {
144 public:
145   virtual ~LoadedObjectInfo() = default;
146
147   /// Obtain the Load Address of a section by Name.
148   ///
149   /// Calculate the address of the section identified by the passed in Name.
150   /// The section need not be present in the local address space. The addresses
151   /// need to be consistent with the addresses used to query the DIContext and
152   /// the output of this function should be deterministic, i.e. repeated calls with
153   /// the same Name should give the same address.
154   virtual uint64_t getSectionLoadAddress(StringRef Name) const = 0;
155
156   /// If conveniently available, return the content of the given Section.
157   ///
158   /// When the section is available in the local address space, in relocated (loaded)
159   /// form, e.g. because it was relocated by a JIT for execution, this function
160   /// should provide the contents of said section in `Data`. If the loaded section
161   /// is not available, or the cost of retrieving it would be prohibitive, this
162   /// function should return false. In that case, relocations will be read from the
163   /// local (unrelocated) object file and applied on the fly. Note that this method
164   /// is used purely for optimzation purposes in the common case of JITting in the
165   /// local address space, so returning false should always be correct.
166   virtual bool getLoadedSectionContents(StringRef Name, StringRef &Data) const {
167     return false;
168   }
169   virtual std::unique_ptr<LoadedObjectInfo> clone() const = 0;
170 };
171
172 }
173
174 #endif