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