LLVM  7.0.0svn
DWARFFormValue.h
Go to the documentation of this file.
1 //===- DWARFFormValue.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 #ifndef LLVM_DEBUGINFO_DWARFFORMVALUE_H
11 #define LLVM_DEBUGINFO_DWARFFORMVALUE_H
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/None.h"
15 #include "llvm/ADT/Optional.h"
19 #include <cstdint>
20 
21 namespace llvm {
22 
23 class DWARFContext;
24 class DWARFUnit;
25 class raw_ostream;
26 
28 public:
29  enum FormClass {
40  };
41 
42 private:
43  struct ValueType {
44  ValueType() { uval = 0; }
45 
46  union {
47  uint64_t uval;
48  int64_t sval;
49  const char *cstr;
50  };
51  const uint8_t *data = nullptr;
52  uint64_t SectionIndex; /// Section index for reference forms.
53  };
54 
55  dwarf::Form Form; /// Form for this value.
56  ValueType Value; /// Contains all data for the form.
57  const DWARFUnit *U = nullptr; /// Remember the DWARFUnit at extract time.
58  const DWARFContext *C = nullptr; /// Context for extract time.
59 public:
61 
62  dwarf::Form getForm() const { return Form; }
63  uint64_t getRawUValue() const { return Value.uval; }
64  uint64_t getSectionIndex() const { return Value.SectionIndex; }
65  void setForm(dwarf::Form F) { Form = F; }
66  void setUValue(uint64_t V) { Value.uval = V; }
67  void setSValue(int64_t V) { Value.sval = V; }
68  void setPValue(const char *V) { Value.cstr = V; }
69 
71  Value.data = Data.data();
72  setUValue(Data.size());
73  }
74 
75  bool isFormClass(FormClass FC) const;
76  const DWARFUnit *getUnit() const { return U; }
77  void dump(raw_ostream &OS, DIDumpOptions DumpOpts = DIDumpOptions()) const;
78 
79  /// Extracts a value in \p Data at offset \p *OffsetPtr. The information
80  /// in \p FormParams is needed to interpret some forms. The optional
81  /// \p Context and \p Unit allows extracting information if the form refers
82  /// to other sections (e.g., .debug_str).
83  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
84  dwarf::FormParams FormParams,
85  const DWARFContext *Context = nullptr,
86  const DWARFUnit *Unit = nullptr);
87 
88  bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr,
89  dwarf::FormParams FormParams, const DWARFUnit *U) {
90  return extractValue(Data, OffsetPtr, FormParams, nullptr, U);
91  }
92 
93  bool isInlinedCStr() const {
94  return Value.data != nullptr && Value.data == (const uint8_t *)Value.cstr;
95  }
96 
97  /// getAsFoo functions below return the extracted value as Foo if only
98  /// DWARFFormValue has form class is suitable for representing Foo.
108 
109  /// Skip a form's value in \p DebugInfoData at the offset specified by
110  /// \p OffsetPtr.
111  ///
112  /// Skips the bytes for the current form and updates the offset.
113  ///
114  /// \param DebugInfoData The data where we want to skip the value.
115  /// \param OffsetPtr A reference to the offset that will be updated.
116  /// \param Params DWARF parameters to help interpret forms.
117  /// \returns true on success, false if the form was not skipped.
118  bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
119  const dwarf::FormParams Params) const {
120  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
121  }
122 
123  /// Skip a form's value in \p DebugInfoData at the offset specified by
124  /// \p OffsetPtr.
125  ///
126  /// Skips the bytes for the specified form and updates the offset.
127  ///
128  /// \param Form The DW_FORM enumeration that indicates the form to skip.
129  /// \param DebugInfoData The data where we want to skip the value.
130  /// \param OffsetPtr A reference to the offset that will be updated.
131  /// \param FormParams DWARF parameters to help interpret forms.
132  /// \returns true on success, false if the form was not skipped.
133  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
134  uint32_t *OffsetPtr,
135  const dwarf::FormParams FormParams);
136 
137 private:
138  void dumpString(raw_ostream &OS) const;
139 };
140 
141 namespace dwarf {
142 
143 /// Take an optional DWARFFormValue and try to extract a string value from it.
144 ///
145 /// \param V and optional DWARFFormValue to attempt to extract the value from.
146 /// \returns an optional value that contains a value if the form value
147 /// was valid and was a string.
149  if (V)
150  return V->getAsCString();
151  return None;
152 }
153 
154 /// Take an optional DWARFFormValue and extract a string value from it.
155 ///
156 /// \param V and optional DWARFFormValue to attempt to extract the value from.
157 /// \param Default the default value to return in case of failure.
158 /// \returns the string value or Default if the V doesn't have a value or the
159 /// form value's encoding wasn't a string.
160 inline const char *toString(const Optional<DWARFFormValue> &V,
161  const char *Default) {
162  return toString(V).getValueOr(Default);
163 }
164 
165 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
166 ///
167 /// \param V and optional DWARFFormValue to attempt to extract the value from.
168 /// \returns an optional value that contains a value if the form value
169 /// was valid and has a unsigned constant form.
171  if (V)
172  return V->getAsUnsignedConstant();
173  return None;
174 }
175 
176 /// Take an optional DWARFFormValue and extract a unsigned constant.
177 ///
178 /// \param V and optional DWARFFormValue to attempt to extract the value from.
179 /// \param Default the default value to return in case of failure.
180 /// \returns the extracted unsigned value or Default if the V doesn't have a
181 /// value or the form value's encoding wasn't an unsigned constant form.
182 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
183  uint64_t Default) {
184  return toUnsigned(V).getValueOr(Default);
185 }
186 
187 /// Take an optional DWARFFormValue and try to extract an reference.
188 ///
189 /// \param V and optional DWARFFormValue to attempt to extract the value from.
190 /// \returns an optional value that contains a value if the form value
191 /// was valid and has a reference form.
193  if (V)
194  return V->getAsReference();
195  return None;
196 }
197 
198 /// Take an optional DWARFFormValue and extract a reference.
199 ///
200 /// \param V and optional DWARFFormValue to attempt to extract the value from.
201 /// \param Default the default value to return in case of failure.
202 /// \returns the extracted reference value or Default if the V doesn't have a
203 /// value or the form value's encoding wasn't a reference form.
204 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
205  uint64_t Default) {
206  return toReference(V).getValueOr(Default);
207 }
208 
209 /// Take an optional DWARFFormValue and try to extract an signed constant.
210 ///
211 /// \param V and optional DWARFFormValue to attempt to extract the value from.
212 /// \returns an optional value that contains a value if the form value
213 /// was valid and has a signed constant form.
215  if (V)
216  return V->getAsSignedConstant();
217  return None;
218 }
219 
220 /// Take an optional DWARFFormValue and extract a signed integer.
221 ///
222 /// \param V and optional DWARFFormValue to attempt to extract the value from.
223 /// \param Default the default value to return in case of failure.
224 /// \returns the extracted signed integer value or Default if the V doesn't
225 /// have a value or the form value's encoding wasn't a signed integer form.
226 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
227  return toSigned(V).getValueOr(Default);
228 }
229 
230 /// Take an optional DWARFFormValue and try to extract an address.
231 ///
232 /// \param V and optional DWARFFormValue to attempt to extract the value from.
233 /// \returns an optional value that contains a value if the form value
234 /// was valid and has a address form.
236  if (V)
237  return V->getAsAddress();
238  return None;
239 }
240 
241 /// Take an optional DWARFFormValue and extract a address.
242 ///
243 /// \param V and optional DWARFFormValue to attempt to extract the value from.
244 /// \param Default the default value to return in case of failure.
245 /// \returns the extracted address value or Default if the V doesn't have a
246 /// value or the form value's encoding wasn't an address form.
247 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
248  return toAddress(V).getValueOr(Default);
249 }
250 
251 /// Take an optional DWARFFormValue and try to extract an section offset.
252 ///
253 /// \param V and optional DWARFFormValue to attempt to extract the value from.
254 /// \returns an optional value that contains a value if the form value
255 /// was valid and has a section offset form.
257  if (V)
258  return V->getAsSectionOffset();
259  return None;
260 }
261 
262 /// Take an optional DWARFFormValue and extract a section offset.
263 ///
264 /// \param V and optional DWARFFormValue to attempt to extract the value from.
265 /// \param Default the default value to return in case of failure.
266 /// \returns the extracted section offset value or Default if the V doesn't
267 /// have a value or the form value's encoding wasn't a section offset form.
268 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
269  uint64_t Default) {
270  return toSectionOffset(V).getValueOr(Default);
271 }
272 
273 /// Take an optional DWARFFormValue and try to extract block data.
274 ///
275 /// \param V and optional DWARFFormValue to attempt to extract the value from.
276 /// \returns an optional value that contains a value if the form value
277 /// was valid and has a block form.
279  if (V)
280  return V->getAsBlock();
281  return None;
282 }
283 
284 } // end namespace dwarf
285 
286 } // end namespace llvm
287 
288 #endif // LLVM_DEBUGINFO_DWARFFORMVALUE_H
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
void setBlockValue(const ArrayRef< uint8_t > &Data)
A helper struct providing information about the byte size of DW_FORM values that vary in size dependi...
Definition: Dwarf.h:495
PointerUnion< const Value *, const PseudoSourceValue * > ValueType
LLVMContext & Context
Optional< ArrayRef< uint8_t > > toBlock(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract block data.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
uint64_t toUnsigned(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a unsigned constant.
uint64_t getSectionIndex() const
void setUValue(uint64_t V)
F(f)
bool isFormClass(FormClass FC) const
dwarf::Form getForm() const
Optional< uint64_t > getAsReference() const
getAsFoo functions below return the extracted value as Foo if only DWARFFormValue has form class is s...
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:955
uint64_t getRawUValue() const
Optional< ArrayRef< uint8_t > > getAsBlock() const
void setForm(dwarf::Form F)
const DWARFUnit * getUnit() const
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:154
void setPValue(const char *V)
uint64_t toSectionOffset(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a section offset.
int64_t toSigned(const Optional< DWARFFormValue > &V, int64_t Default)
Take an optional DWARFFormValue and extract a signed integer.
bool isInlinedCStr() const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
Optional< uint64_t > getAsAddress() const
Optional< uint64_t > getAsUnsignedConstant() const
bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr, const dwarf::FormParams Params) const
Skip a form&#39;s value in DebugInfoData at the offset specified by OffsetPtr.
Optional< uint64_t > getAsReferenceUVal() const
const T * data() const
Definition: ArrayRef.h:146
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
uint64_t toReference(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a reference.
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:59
Optional< uint64_t > getAsCStringOffset() const
This file contains constants used for implementing Dwarf debug support.
DWARFFormValue(dwarf::Form F=dwarf::Form(0))
Context for extract time.
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFContext *Context=nullptr, const DWARFUnit *Unit=nullptr)
Extracts a value in Data at offset *OffsetPtr.
uint64_t toAddress(const Optional< DWARFFormValue > &V, uint64_t Default)
Take an optional DWARFFormValue and extract a address.
Optional< const char * > getAsCString() const
Optional< int64_t > getAsSignedConstant() const
bool extractValue(const DWARFDataExtractor &Data, uint32_t *OffsetPtr, dwarf::FormParams FormParams, const DWARFUnit *U)
void setSValue(int64_t V)
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
Optional< uint64_t > getAsSectionOffset() const