LLVM  8.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.
109 
110  /// Skip a form's value in \p DebugInfoData at the offset specified by
111  /// \p OffsetPtr.
112  ///
113  /// Skips the bytes for the current form and updates the offset.
114  ///
115  /// \param DebugInfoData The data where we want to skip the value.
116  /// \param OffsetPtr A reference to the offset that will be updated.
117  /// \param Params DWARF parameters to help interpret forms.
118  /// \returns true on success, false if the form was not skipped.
119  bool skipValue(DataExtractor DebugInfoData, uint32_t *OffsetPtr,
120  const dwarf::FormParams Params) const {
121  return DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, Params);
122  }
123 
124  /// Skip a form's value in \p DebugInfoData at the offset specified by
125  /// \p OffsetPtr.
126  ///
127  /// Skips the bytes for the specified form and updates the offset.
128  ///
129  /// \param Form The DW_FORM enumeration that indicates the form to skip.
130  /// \param DebugInfoData The data where we want to skip the value.
131  /// \param OffsetPtr A reference to the offset that will be updated.
132  /// \param FormParams DWARF parameters to help interpret forms.
133  /// \returns true on success, false if the form was not skipped.
134  static bool skipValue(dwarf::Form Form, DataExtractor DebugInfoData,
135  uint32_t *OffsetPtr,
136  const dwarf::FormParams FormParams);
137 
138 private:
139  void dumpString(raw_ostream &OS) const;
140 };
141 
142 namespace dwarf {
143 
144 /// Take an optional DWARFFormValue and try to extract a string value from it.
145 ///
146 /// \param V and optional DWARFFormValue to attempt to extract the value from.
147 /// \returns an optional value that contains a value if the form value
148 /// was valid and was a string.
150  if (V)
151  return V->getAsCString();
152  return None;
153 }
154 
155 /// Take an optional DWARFFormValue and extract a string value from it.
156 ///
157 /// \param V and optional DWARFFormValue to attempt to extract the value from.
158 /// \param Default the default value to return in case of failure.
159 /// \returns the string value or Default if the V doesn't have a value or the
160 /// form value's encoding wasn't a string.
161 inline const char *toString(const Optional<DWARFFormValue> &V,
162  const char *Default) {
163  return toString(V).getValueOr(Default);
164 }
165 
166 /// Take an optional DWARFFormValue and try to extract an unsigned constant.
167 ///
168 /// \param V and optional DWARFFormValue to attempt to extract the value from.
169 /// \returns an optional value that contains a value if the form value
170 /// was valid and has a unsigned constant form.
172  if (V)
173  return V->getAsUnsignedConstant();
174  return None;
175 }
176 
177 /// Take an optional DWARFFormValue and extract a unsigned constant.
178 ///
179 /// \param V and optional DWARFFormValue to attempt to extract the value from.
180 /// \param Default the default value to return in case of failure.
181 /// \returns the extracted unsigned value or Default if the V doesn't have a
182 /// value or the form value's encoding wasn't an unsigned constant form.
183 inline uint64_t toUnsigned(const Optional<DWARFFormValue> &V,
184  uint64_t Default) {
185  return toUnsigned(V).getValueOr(Default);
186 }
187 
188 /// Take an optional DWARFFormValue and try to extract an reference.
189 ///
190 /// \param V and optional DWARFFormValue to attempt to extract the value from.
191 /// \returns an optional value that contains a value if the form value
192 /// was valid and has a reference form.
194  if (V)
195  return V->getAsReference();
196  return None;
197 }
198 
199 /// Take an optional DWARFFormValue and extract a reference.
200 ///
201 /// \param V and optional DWARFFormValue to attempt to extract the value from.
202 /// \param Default the default value to return in case of failure.
203 /// \returns the extracted reference value or Default if the V doesn't have a
204 /// value or the form value's encoding wasn't a reference form.
205 inline uint64_t toReference(const Optional<DWARFFormValue> &V,
206  uint64_t Default) {
207  return toReference(V).getValueOr(Default);
208 }
209 
210 /// Take an optional DWARFFormValue and try to extract an signed constant.
211 ///
212 /// \param V and optional DWARFFormValue to attempt to extract the value from.
213 /// \returns an optional value that contains a value if the form value
214 /// was valid and has a signed constant form.
216  if (V)
217  return V->getAsSignedConstant();
218  return None;
219 }
220 
221 /// Take an optional DWARFFormValue and extract a signed integer.
222 ///
223 /// \param V and optional DWARFFormValue to attempt to extract the value from.
224 /// \param Default the default value to return in case of failure.
225 /// \returns the extracted signed integer value or Default if the V doesn't
226 /// have a value or the form value's encoding wasn't a signed integer form.
227 inline int64_t toSigned(const Optional<DWARFFormValue> &V, int64_t Default) {
228  return toSigned(V).getValueOr(Default);
229 }
230 
231 /// Take an optional DWARFFormValue and try to extract an address.
232 ///
233 /// \param V and optional DWARFFormValue to attempt to extract the value from.
234 /// \returns an optional value that contains a value if the form value
235 /// was valid and has a address form.
237  if (V)
238  return V->getAsAddress();
239  return None;
240 }
241 
244  if (V)
245  return V->getAsSectionedAddress();
246  return None;
247 }
248 
249 /// Take an optional DWARFFormValue and extract a address.
250 ///
251 /// \param V and optional DWARFFormValue to attempt to extract the value from.
252 /// \param Default the default value to return in case of failure.
253 /// \returns the extracted address value or Default if the V doesn't have a
254 /// value or the form value's encoding wasn't an address form.
255 inline uint64_t toAddress(const Optional<DWARFFormValue> &V, uint64_t Default) {
256  return toAddress(V).getValueOr(Default);
257 }
258 
259 /// Take an optional DWARFFormValue and try to extract an section offset.
260 ///
261 /// \param V and optional DWARFFormValue to attempt to extract the value from.
262 /// \returns an optional value that contains a value if the form value
263 /// was valid and has a section offset form.
265  if (V)
266  return V->getAsSectionOffset();
267  return None;
268 }
269 
270 /// Take an optional DWARFFormValue and extract a section offset.
271 ///
272 /// \param V and optional DWARFFormValue to attempt to extract the value from.
273 /// \param Default the default value to return in case of failure.
274 /// \returns the extracted section offset value or Default if the V doesn't
275 /// have a value or the form value's encoding wasn't a section offset form.
276 inline uint64_t toSectionOffset(const Optional<DWARFFormValue> &V,
277  uint64_t Default) {
278  return toSectionOffset(V).getValueOr(Default);
279 }
280 
281 /// Take an optional DWARFFormValue and try to extract block data.
282 ///
283 /// \param V and optional DWARFFormValue to attempt to extract the value from.
284 /// \returns an optional value that contains a value if the form value
285 /// was valid and has a block form.
287  if (V)
288  return V->getAsBlock();
289  return None;
290 }
291 
292 } // end namespace dwarf
293 
294 } // end namespace llvm
295 
296 #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:494
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
Optional< SectionedAddress > getAsSectionedAddress() 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:963
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
Optional< SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)