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