]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/DebugInfo/DWARF/DWARFDie.cpp
Merge ^/head r311684 through r311691.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / DebugInfo / DWARF / DWARFDie.cpp
1 //===-- DWARFDie.cpp ------------------------------------------------------===//
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 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
11 #include "SyntaxHighlighting.h"
12 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
13 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
16 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 using namespace llvm;
24 using namespace dwarf;
25 using namespace syntax;
26
27 namespace {
28  static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
29   OS << " (";
30   do {
31     uint64_t Shift = countTrailingZeros(Val);
32     assert(Shift < 64 && "undefined behavior");
33     uint64_t Bit = 1ULL << Shift;
34     auto PropName = ApplePropertyString(Bit);
35     if (!PropName.empty())
36       OS << PropName;
37     else
38       OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
39     if (!(Val ^= Bit))
40       break;
41     OS << ", ";
42   } while (true);
43   OS << ")";
44 }
45
46 static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
47                        unsigned AddressSize, unsigned Indent) {
48   if (Ranges.empty())
49     return;
50   
51   for (const auto &Range: Ranges) {
52     OS << '\n';
53     OS.indent(Indent);
54     OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
55                  AddressSize*2, Range.first,
56                  AddressSize*2, Range.second);
57   }
58 }
59
60 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
61                           uint32_t *OffsetPtr, dwarf::Attribute Attr,
62                           dwarf::Form Form, unsigned Indent) {
63   if (!Die.isValid())
64     return;
65   const char BaseIndent[] = "            ";
66   OS << BaseIndent;
67   OS.indent(Indent+2);
68   auto attrString = AttributeString(Attr);
69   if (!attrString.empty())
70     WithColor(OS, syntax::Attribute) << attrString;
71   else
72     WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
73   
74   auto formString = FormEncodingString(Form);
75   if (!formString.empty())
76     OS << " [" << formString << ']';
77   else
78     OS << format(" [DW_FORM_Unknown_%x]", Form);
79   
80   DWARFUnit *U = Die.getDwarfUnit();
81   DWARFFormValue formValue(Form);
82   
83   if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
84     return;
85   
86   OS << "\t(";
87   
88   StringRef Name;
89   std::string File;
90   auto Color = syntax::Enumerator;
91   if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
92     Color = syntax::String;
93     if (const auto *LT = U->getContext().getLineTableForUnit(U))
94       if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
95         File = '"' + File + '"';
96         Name = File;
97       }
98   } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
99     Name = AttributeValueString(Attr, *Val);
100   
101   if (!Name.empty())
102     WithColor(OS, Color) << Name;
103   else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
104     OS << *formValue.getAsUnsignedConstant();
105   else
106     formValue.dump(OS);
107   
108   // We have dumped the attribute raw value. For some attributes
109   // having both the raw value and the pretty-printed value is
110   // interesting. These attributes are handled below.
111   if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
112     if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
113         OS << " \"" << Name << '\"';
114   } else if (Attr == DW_AT_APPLE_property_attribute) {
115     if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
116       dumpApplePropertyAttribute(OS, *OptVal);
117   } else if (Attr == DW_AT_ranges) {
118     dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
119                sizeof(BaseIndent)+Indent+4);
120   }
121   
122   OS << ")\n";
123 }
124
125 } // end anonymous namespace
126
127 bool DWARFDie::isSubprogramDIE() const {
128   return getTag() == DW_TAG_subprogram;
129 }
130
131 bool DWARFDie::isSubroutineDIE() const {
132   auto Tag = getTag();
133   return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
134 }
135
136 Optional<DWARFFormValue>
137 DWARFDie::getAttributeValue(dwarf::Attribute Attr) const {
138   if (!isValid())
139     return None;
140   auto AbbrevDecl = getAbbreviationDeclarationPtr();
141   if (AbbrevDecl)
142     return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
143   return None;
144 }
145
146 const char *DWARFDie::getAttributeValueAsString(dwarf::Attribute Attr,
147                                                 const char *FailValue) const {
148   auto FormValue = getAttributeValue(Attr);
149   if (!FormValue)
150     return FailValue;
151   Optional<const char *> Result = FormValue->getAsCString();
152   return Result.hasValue() ? Result.getValue() : FailValue;
153 }
154
155 uint64_t DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr,
156                                               uint64_t FailValue) const {
157   if (auto Value = getAttributeValueAsAddress(Attr))
158     return *Value;
159   return FailValue;
160 }
161
162 Optional<uint64_t>
163 DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr) const {
164   if (auto FormValue = getAttributeValue(Attr))
165     return FormValue->getAsAddress();
166   return None;
167 }
168
169 int64_t DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr,
170                                                     int64_t FailValue) const {
171   if (auto Value = getAttributeValueAsSignedConstant(Attr))
172     return *Value;
173   return FailValue;
174 }
175
176 Optional<int64_t>
177 DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr) const {
178   if (auto FormValue = getAttributeValue(Attr))
179     return FormValue->getAsSignedConstant();
180   return None;
181 }
182
183 uint64_t
184 DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr,
185                                               uint64_t FailValue) const {
186   if (auto Value = getAttributeValueAsUnsignedConstant(Attr))
187     return *Value;
188   return FailValue;
189 }
190
191
192 Optional<uint64_t>
193 DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr) const {
194   if (auto FormValue = getAttributeValue(Attr))
195     return FormValue->getAsUnsignedConstant();
196   return None;
197 }
198
199 uint64_t DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr,
200                                                 uint64_t FailValue) const {
201   if (auto Value = getAttributeValueAsReference(Attr))
202     return *Value;
203   return FailValue;
204 }
205
206
207 Optional<uint64_t>
208 DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr) const {
209   if (auto FormValue = getAttributeValue(Attr))
210     return FormValue->getAsReference();
211   return None;
212 }
213
214 uint64_t DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr,
215                                                     uint64_t FailValue) const {
216   if (auto Value = getAttributeValueAsSectionOffset(Attr))
217     return *Value;
218   return FailValue;
219 }
220
221 Optional<uint64_t>
222 DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr) const {
223   if (auto FormValue = getAttributeValue(Attr))
224     return FormValue->getAsSectionOffset();
225   return None;
226 }
227
228
229 DWARFDie
230 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
231   auto SpecRef = getAttributeValueAsReference(Attr);
232   if (SpecRef) {
233     auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
234     if (SpecUnit)
235       return SpecUnit->getDIEForOffset(*SpecRef);
236   }
237   return DWARFDie();
238 }
239
240 Optional<uint64_t>
241 DWARFDie::getRangesBaseAttribute() const {
242   auto Result = getAttributeValueAsSectionOffset(DW_AT_rnglists_base);
243   if (Result)
244     return Result;
245   return getAttributeValueAsSectionOffset(DW_AT_GNU_ranges_base);
246 }
247
248 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
249   if (auto FormValue = getAttributeValue(DW_AT_high_pc)) {
250     if (auto Address = FormValue->getAsAddress()) {
251       // High PC is an address.
252       return Address;
253     }
254     if (auto Offset = FormValue->getAsUnsignedConstant()) {
255       // High PC is an offset from LowPC.
256       return LowPC + *Offset;
257     }
258   }
259   return None;
260 }
261
262 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const {
263   auto LowPcAddr = getAttributeValueAsAddress(DW_AT_low_pc);
264   if (!LowPcAddr)
265     return false;
266   if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
267     LowPC = *LowPcAddr;
268     HighPC = *HighPcAddr;
269     return true;
270   }
271   return false;
272 }
273
274 DWARFAddressRangesVector
275 DWARFDie::getAddressRanges() const {
276   if (isNULL())
277     return DWARFAddressRangesVector();
278   // Single range specified by low/high PC.
279   uint64_t LowPC, HighPC;
280   if (getLowAndHighPC(LowPC, HighPC)) {
281     return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
282   }
283   // Multiple ranges from .debug_ranges section.
284   auto RangesOffset = getAttributeValueAsSectionOffset(DW_AT_ranges);
285   if (RangesOffset) {
286     DWARFDebugRangeList RangeList;
287     if (U->extractRangeList(*RangesOffset, RangeList))
288       return RangeList.getAbsoluteRanges(U->getBaseAddress());
289   }
290   return DWARFAddressRangesVector();
291 }
292
293 void
294 DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
295   if (isNULL())
296     return;
297   if (isSubprogramDIE()) {
298     const auto &DIERanges = getAddressRanges();
299     Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
300   }
301
302   for (auto Child: children())
303     Child.collectChildrenAddressRanges(Ranges);
304 }
305
306 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
307   for (const auto& R : getAddressRanges()) {
308     if (R.first <= Address && Address < R.second)
309       return true;
310   }
311   return false;
312 }
313
314 const char *
315 DWARFDie::getSubroutineName(DINameKind Kind) const {
316   if (!isSubroutineDIE())
317     return nullptr;
318   return getName(Kind);
319 }
320
321 const char *
322 DWARFDie::getName(DINameKind Kind) const {
323   if (!isValid() || Kind == DINameKind::None)
324     return nullptr;
325   const char *name = nullptr;
326   // Try to get mangled name only if it was asked for.
327   if (Kind == DINameKind::LinkageName) {
328     if ((name = getAttributeValueAsString(DW_AT_MIPS_linkage_name, nullptr)))
329       return name;
330     if ((name = getAttributeValueAsString(DW_AT_linkage_name, nullptr)))
331       return name;
332   }
333   if ((name = getAttributeValueAsString(DW_AT_name, nullptr)))
334     return name;
335   // Try to get name from specification DIE.
336   DWARFDie SpecDie = getAttributeValueAsReferencedDie(DW_AT_specification);
337   if (SpecDie && (name = SpecDie.getName(Kind)))
338     return name;
339   // Try to get name from abstract origin DIE.
340   DWARFDie AbsDie = getAttributeValueAsReferencedDie(DW_AT_abstract_origin);
341   if (AbsDie && (name = AbsDie.getName(Kind)))
342     return name;
343   return nullptr;
344 }
345
346 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
347                               uint32_t &CallColumn) const {
348   CallFile = getAttributeValueAsUnsignedConstant(DW_AT_call_file, 0);
349   CallLine = getAttributeValueAsUnsignedConstant(DW_AT_call_line, 0);
350   CallColumn = getAttributeValueAsUnsignedConstant(DW_AT_call_column, 0);
351 }
352
353 void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
354                     unsigned Indent) const {
355   if (!isValid())
356     return;
357   DataExtractor debug_info_data = U->getDebugInfoExtractor();
358   const uint32_t Offset = getOffset();
359   uint32_t offset = Offset;
360   
361   if (debug_info_data.isValidOffset(offset)) {
362     uint32_t abbrCode = debug_info_data.getULEB128(&offset);
363     WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
364     
365     if (abbrCode) {
366       auto AbbrevDecl = getAbbreviationDeclarationPtr();
367       if (AbbrevDecl) {
368         auto tagString = TagString(getTag());
369         if (!tagString.empty())
370           WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
371         else
372           WithColor(OS, syntax::Tag).get().indent(Indent)
373           << format("DW_TAG_Unknown_%x", getTag());
374         
375         OS << format(" [%u] %c\n", abbrCode,
376                      AbbrevDecl->hasChildren() ? '*' : ' ');
377         
378         // Dump all data in the DIE for the attributes.
379         for (const auto &AttrSpec : AbbrevDecl->attributes()) {
380           dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
381                         Indent);
382         }
383         
384         DWARFDie child = getFirstChild();
385         if (RecurseDepth > 0 && child) {
386           while (child) {
387             child.dump(OS, RecurseDepth-1, Indent+2);
388             child = child.getSibling();
389           }
390         }
391       } else {
392         OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
393         << abbrCode << '\n';
394       }
395     } else {
396       OS.indent(Indent) << "NULL\n";
397     }
398   }
399 }
400
401
402 void DWARFDie::getInlinedChainForAddress(
403     const uint64_t Address, SmallVectorImpl<DWARFDie> &InlinedChain) const {
404   if (isNULL())
405     return;
406   DWARFDie DIE(*this);
407   while (DIE) {
408     // Append current DIE to inlined chain only if it has correct tag
409     // (e.g. it is not a lexical block).
410     if (DIE.isSubroutineDIE())
411       InlinedChain.push_back(DIE);
412
413     // Try to get child which also contains provided address.
414     DWARFDie Child = DIE.getFirstChild();
415     while (Child) {
416       if (Child.addressRangeContainsAddress(Address)) {
417         // Assume there is only one such child.
418         break;
419       }
420       Child = Child.getSibling();
421     }
422     DIE = Child;
423   }
424   // Reverse the obtained chain to make the root of inlined chain last.
425   std::reverse(InlinedChain.begin(), InlinedChain.end());
426 }
427
428 DWARFDie DWARFDie::getParent() const {
429   if (isValid())
430     return U->getParent(Die);
431   return DWARFDie();
432 }
433
434 DWARFDie DWARFDie::getSibling() const {
435   if (isValid())
436     return U->getSibling(Die);
437   return DWARFDie();
438 }