LLVM  10.0.0svn
DWARFDie.cpp
Go to the documentation of this file.
1 //===- DWARFDie.cpp -------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "llvm/ADT/None.h"
11 #include "llvm/ADT/Optional.h"
12 #include "llvm/ADT/SmallSet.h"
13 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Format.h"
27 #include "llvm/Support/WithColor.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cinttypes>
32 #include <cstdint>
33 #include <string>
34 #include <utility>
35 
36 using namespace llvm;
37 using namespace dwarf;
38 using namespace object;
39 
40 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
41  OS << " (";
42  do {
43  uint64_t Shift = countTrailingZeros(Val);
44  assert(Shift < 64 && "undefined behavior");
45  uint64_t Bit = 1ULL << Shift;
46  auto PropName = ApplePropertyString(Bit);
47  if (!PropName.empty())
48  OS << PropName;
49  else
50  OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
51  if (!(Val ^= Bit))
52  break;
53  OS << ", ";
54  } while (true);
55  OS << ")";
56 }
57 
58 static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS,
59  const DWARFAddressRangesVector &Ranges,
60  unsigned AddressSize, unsigned Indent,
61  const DIDumpOptions &DumpOpts) {
62  if (!DumpOpts.ShowAddresses)
63  return;
64 
65  ArrayRef<SectionName> SectionNames;
66  if (DumpOpts.Verbose)
67  SectionNames = Obj.getSectionNames();
68 
69  for (const DWARFAddressRange &R : Ranges) {
70  OS << '\n';
71  OS.indent(Indent);
72  R.dump(OS, AddressSize);
73 
74  DWARFFormValue::dumpAddressSection(Obj, OS, DumpOpts, R.SectionIndex);
75  }
76 }
77 
78 static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
79  DWARFUnit *U, unsigned Indent,
80  DIDumpOptions DumpOpts) {
81  DWARFContext &Ctx = U->getContext();
82  const DWARFObject &Obj = Ctx.getDWARFObj();
83  const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
84  if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
86  ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
87  DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
88  Ctx.isLittleEndian(), 0);
90  .print(OS, MRI, U);
91  return;
92  }
93 
94  FormValue.dump(OS, DumpOpts);
95  const auto &DumpLL = [&](auto ExpectedLL) {
96  if (ExpectedLL) {
97  uint64_t BaseAddr = 0;
99  BaseAddr = BA->Address;
100  auto LLDumpOpts = DumpOpts;
101  LLDumpOpts.Verbose = false;
102  ExpectedLL->dump(OS, BaseAddr, Ctx.isLittleEndian(), Obj.getAddressSize(),
103  MRI, U, LLDumpOpts, Indent);
104  } else {
105  OS << '\n';
106  OS.indent(Indent);
107  OS << formatv("error extracting location list: {0}",
108  fmt_consume(ExpectedLL.takeError()));
109  }
110  };
112  uint64_t Offset = *FormValue.getAsSectionOffset();
113  if (!U->isDWOUnit() && !U->getLocSection()->Data.empty()) {
116  Obj.getAddressSize());
117  DumpLL(DebugLoc.parseOneLocationList(Data, &Offset));
118  return;
119  }
120 
121  bool UseLocLists = !U->isDWOUnit();
122  StringRef LoclistsSectionData =
123  UseLocLists ? Obj.getLoclistsSection().Data : U->getLocSectionData();
124 
125  if (!LoclistsSectionData.empty()) {
126  DataExtractor Data(LoclistsSectionData, Ctx.isLittleEndian(),
127  Obj.getAddressSize());
128 
129  // Old-style location list were used in DWARF v4 (.debug_loc.dwo section).
130  // Modern locations list (.debug_loclists) are used starting from v5.
131  // Ideally we should take the version from the .debug_loclists section
132  // header, but using CU's version for simplicity.
134  Data, &Offset, UseLocLists ? U->getVersion() : 4));
135  }
136  }
137 }
138 
139 /// Dump the name encoded in the type tag.
141  StringRef TagStr = TagString(T);
142  if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
143  return;
144  OS << TagStr.substr(7, TagStr.size() - 12) << " ";
145 }
146 
147 static void dumpArrayType(raw_ostream &OS, const DWARFDie &D) {
148  Optional<uint64_t> Bound;
149  for (const DWARFDie &C : D.children())
150  if (C.getTag() == DW_TAG_subrange_type) {
152  Optional<uint64_t> Count;
154  Optional<unsigned> DefaultLB;
155  if (Optional<DWARFFormValue> L = C.find(DW_AT_lower_bound))
156  LB = L->getAsUnsignedConstant();
157  if (Optional<DWARFFormValue> CountV = C.find(DW_AT_count))
158  Count = CountV->getAsUnsignedConstant();
159  if (Optional<DWARFFormValue> UpperV = C.find(DW_AT_upper_bound))
160  UB = UpperV->getAsUnsignedConstant();
161  if (Optional<DWARFFormValue> LV =
162  D.getDwarfUnit()->getUnitDIE().find(DW_AT_language))
163  if (Optional<uint64_t> LC = LV->getAsUnsignedConstant())
164  if ((DefaultLB =
165  LanguageLowerBound(static_cast<dwarf::SourceLanguage>(*LC))))
166  if (LB && *LB == *DefaultLB)
167  LB = None;
168  if (!LB && !Count && !UB)
169  OS << "[]";
170  else if (!LB && (Count || UB) && DefaultLB)
171  OS << '[' << (Count ? *Count : *UB - *DefaultLB + 1) << ']';
172  else {
173  OS << "[[";
174  if (LB)
175  OS << *LB;
176  else
177  OS << '?';
178  OS << ", ";
179  if (Count)
180  if (LB)
181  OS << *LB + *Count;
182  else
183  OS << "? + " << *Count;
184  else if (UB)
185  OS << *UB + 1;
186  else
187  OS << '?';
188  OS << ")]";
189  }
190  }
191 }
192 
193 /// Recursively dump the DIE type name when applicable.
194 static void dumpTypeName(raw_ostream &OS, const DWARFDie &D) {
195  if (!D.isValid())
196  return;
197 
198  if (const char *Name = D.getName(DINameKind::LinkageName)) {
199  OS << Name;
200  return;
201  }
202 
203  // FIXME: We should have pretty printers per language. Currently we print
204  // everything as if it was C++ and fall back to the TAG type name.
205  const dwarf::Tag T = D.getTag();
206  switch (T) {
207  case DW_TAG_array_type:
208  case DW_TAG_pointer_type:
209  case DW_TAG_ptr_to_member_type:
210  case DW_TAG_reference_type:
211  case DW_TAG_rvalue_reference_type:
212  case DW_TAG_subroutine_type:
213  break;
214  default:
215  dumpTypeTagName(OS, T);
216  }
217 
218  // Follow the DW_AT_type if possible.
219  DWARFDie TypeDie = D.getAttributeValueAsReferencedDie(DW_AT_type);
220  dumpTypeName(OS, TypeDie);
221 
222  switch (T) {
223  case DW_TAG_subroutine_type: {
224  if (!TypeDie)
225  OS << "void";
226  OS << '(';
227  bool First = true;
228  for (const DWARFDie &C : D.children()) {
229  if (C.getTag() == DW_TAG_formal_parameter) {
230  if (!First)
231  OS << ", ";
232  First = false;
233  dumpTypeName(OS, C.getAttributeValueAsReferencedDie(DW_AT_type));
234  }
235  }
236  OS << ')';
237  break;
238  }
239  case DW_TAG_array_type: {
240  dumpArrayType(OS, D);
241  break;
242  }
243  case DW_TAG_pointer_type:
244  OS << '*';
245  break;
246  case DW_TAG_ptr_to_member_type:
247  if (DWARFDie Cont =
248  D.getAttributeValueAsReferencedDie(DW_AT_containing_type)) {
249  dumpTypeName(OS << ' ', Cont);
250  OS << "::";
251  }
252  OS << '*';
253  break;
254  case DW_TAG_reference_type:
255  OS << '&';
256  break;
257  case DW_TAG_rvalue_reference_type:
258  OS << "&&";
259  break;
260  default:
261  break;
262  }
263 }
264 
265 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
266  uint64_t *OffsetPtr, dwarf::Attribute Attr,
267  dwarf::Form Form, unsigned Indent,
268  DIDumpOptions DumpOpts) {
269  if (!Die.isValid())
270  return;
271  const char BaseIndent[] = " ";
272  OS << BaseIndent;
273  OS.indent(Indent + 2);
274  WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
275 
276  if (DumpOpts.Verbose || DumpOpts.ShowForm)
277  OS << formatv(" [{0}]", Form);
278 
279  DWARFUnit *U = Die.getDwarfUnit();
280  DWARFFormValue FormValue = DWARFFormValue::createFromUnit(Form, U, OffsetPtr);
281 
282  OS << "\t(";
283 
284  StringRef Name;
285  std::string File;
287  if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
289  if (const auto *LT = U->getContext().getLineTableForUnit(U))
290  if (LT->getFileNameByIndex(
291  FormValue.getAsUnsignedConstant().getValue(),
292  U->getCompilationDir(),
294  File = '"' + File + '"';
295  Name = File;
296  }
297  } else if (Optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
298  Name = AttributeValueString(Attr, *Val);
299 
300  if (!Name.empty())
301  WithColor(OS, Color) << Name;
302  else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
303  OS << *FormValue.getAsUnsignedConstant();
304  else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
305  FormValue.getAsUnsignedConstant()) {
306  if (DumpOpts.ShowAddresses) {
307  // Print the actual address rather than the offset.
308  uint64_t LowPC, HighPC, Index;
309  if (Die.getLowAndHighPC(LowPC, HighPC, Index))
310  OS << format("0x%016" PRIx64, HighPC);
311  else
312  FormValue.dump(OS, DumpOpts);
313  }
315  dumpLocation(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
316  else
317  FormValue.dump(OS, DumpOpts);
318 
319  std::string Space = DumpOpts.ShowAddresses ? " " : "";
320 
321  // We have dumped the attribute raw value. For some attributes
322  // having both the raw value and the pretty-printed value is
323  // interesting. These attributes are handled below.
324  if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
325  if (const char *Name =
328  OS << Space << "\"" << Name << '\"';
329  } else if (Attr == DW_AT_type) {
330  OS << Space << "\"";
332  OS << '"';
333  } else if (Attr == DW_AT_APPLE_property_attribute) {
334  if (Optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
335  dumpApplePropertyAttribute(OS, *OptVal);
336  } else if (Attr == DW_AT_ranges) {
337  const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
338  // For DW_FORM_rnglistx we need to dump the offset separately, since
339  // we have only dumped the index so far.
340  if (FormValue.getForm() == DW_FORM_rnglistx)
341  if (auto RangeListOffset =
342  U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
344  dwarf::DW_FORM_sec_offset, *RangeListOffset);
345  FV.dump(OS, DumpOpts);
346  }
347  if (auto RangesOrError = Die.getAddressRanges())
348  dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
349  sizeof(BaseIndent) + Indent + 4, DumpOpts);
350  else
351  WithColor::error() << "decoding address ranges: "
352  << toString(RangesOrError.takeError()) << '\n';
353  }
354 
355  OS << ")\n";
356 }
357 
358 bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
359 
361  auto Tag = getTag();
362  return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
363 }
364 
366  if (!isValid())
367  return None;
368  auto AbbrevDecl = getAbbreviationDeclarationPtr();
369  if (AbbrevDecl)
370  return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
371  return None;
372 }
373 
376  if (!isValid())
377  return None;
378  auto AbbrevDecl = getAbbreviationDeclarationPtr();
379  if (AbbrevDecl) {
380  for (auto Attr : Attrs) {
381  if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
382  return Value;
383  }
384  }
385  return None;
386 }
387 
390  std::vector<DWARFDie> Worklist;
391  Worklist.push_back(*this);
392 
393  // Keep track if DIEs already seen to prevent infinite recursion.
394  // Empirically we rarely see a depth of more than 3 when dealing with valid
395  // DWARF. This corresponds to following the DW_AT_abstract_origin and
396  // DW_AT_specification just once.
398  Seen.insert(*this);
399 
400  while (!Worklist.empty()) {
401  DWARFDie Die = Worklist.back();
402  Worklist.pop_back();
403 
404  if (!Die.isValid())
405  continue;
406 
407  if (auto Value = Die.find(Attrs))
408  return Value;
409 
410  if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
411  if (Seen.insert(D).second)
412  Worklist.push_back(D);
413 
414  if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
415  if (Seen.insert(D).second)
416  Worklist.push_back(D);
417  }
418 
419  return None;
420 }
421 
422 DWARFDie
424  if (Optional<DWARFFormValue> F = find(Attr))
425  return getAttributeValueAsReferencedDie(*F);
426  return DWARFDie();
427 }
428 
429 DWARFDie
431  if (auto SpecRef = V.getAsRelativeReference()) {
432  if (SpecRef->Unit)
433  return SpecRef->Unit->getDIEForOffset(SpecRef->Unit->getOffset() + SpecRef->Offset);
434  if (auto SpecUnit = U->getUnitVector().getUnitForOffset(SpecRef->Offset))
435  return SpecUnit->getDIEForOffset(SpecRef->Offset);
436  }
437  return DWARFDie();
438 }
439 
441  return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
442 }
443 
445  if (auto FormValue = find(DW_AT_high_pc)) {
446  if (auto Address = FormValue->getAsAddress()) {
447  // High PC is an address.
448  return Address;
449  }
450  if (auto Offset = FormValue->getAsUnsignedConstant()) {
451  // High PC is an offset from LowPC.
452  return LowPC + *Offset;
453  }
454  }
455  return None;
456 }
457 
458 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
459  uint64_t &SectionIndex) const {
460  auto F = find(DW_AT_low_pc);
461  auto LowPcAddr = toSectionedAddress(F);
462  if (!LowPcAddr)
463  return false;
464  if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
465  LowPC = LowPcAddr->Address;
466  HighPC = *HighPcAddr;
467  SectionIndex = LowPcAddr->SectionIndex;
468  return true;
469  }
470  return false;
471 }
472 
474  if (isNULL())
475  return DWARFAddressRangesVector();
476  // Single range specified by low/high PC.
477  uint64_t LowPC, HighPC, Index;
478  if (getLowAndHighPC(LowPC, HighPC, Index))
479  return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
480 
481  Optional<DWARFFormValue> Value = find(DW_AT_ranges);
482  if (Value) {
483  if (Value->getForm() == DW_FORM_rnglistx)
484  return U->findRnglistFromIndex(*Value->getAsSectionOffset());
485  return U->findRnglistFromOffset(*Value->getAsSectionOffset());
486  }
487  return DWARFAddressRangesVector();
488 }
489 
491  DWARFAddressRangesVector &Ranges) const {
492  if (isNULL())
493  return;
494  if (isSubprogramDIE()) {
495  if (auto DIERangesOrError = getAddressRanges())
496  Ranges.insert(Ranges.end(), DIERangesOrError.get().begin(),
497  DIERangesOrError.get().end());
498  else
499  llvm::consumeError(DIERangesOrError.takeError());
500  }
501 
502  for (auto Child : children())
503  Child.collectChildrenAddressRanges(Ranges);
504 }
505 
506 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
507  auto RangesOrError = getAddressRanges();
508  if (!RangesOrError) {
509  llvm::consumeError(RangesOrError.takeError());
510  return false;
511  }
512 
513  for (const auto &R : RangesOrError.get())
514  if (R.LowPC <= Address && Address < R.HighPC)
515  return true;
516  return false;
517 }
518 
520  if (!isSubroutineDIE())
521  return nullptr;
522  return getName(Kind);
523 }
524 
525 const char *DWARFDie::getName(DINameKind Kind) const {
526  if (!isValid() || Kind == DINameKind::None)
527  return nullptr;
528  // Try to get mangled name only if it was asked for.
529  if (Kind == DINameKind::LinkageName) {
530  if (auto Name = dwarf::toString(
531  findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
532  nullptr))
533  return Name;
534  }
535  if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
536  return Name;
537  return nullptr;
538 }
539 
540 uint64_t DWARFDie::getDeclLine() const {
541  return toUnsigned(findRecursively(DW_AT_decl_line), 0);
542 }
543 
544 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
545  uint32_t &CallColumn,
546  uint32_t &CallDiscriminator) const {
547  CallFile = toUnsigned(find(DW_AT_call_file), 0);
548  CallLine = toUnsigned(find(DW_AT_call_line), 0);
549  CallColumn = toUnsigned(find(DW_AT_call_column), 0);
550  CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
551 }
552 
553 /// Helper to dump a DIE with all of its parents, but no siblings.
554 static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
555  DIDumpOptions DumpOpts, unsigned Depth = 0) {
556  if (!Die)
557  return Indent;
558  if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
559  return Indent;
560  Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
561  Die.dump(OS, Indent, DumpOpts);
562  return Indent + 2;
563 }
564 
565 void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
566  DIDumpOptions DumpOpts) const {
567  if (!isValid())
568  return;
569  DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
570  const uint64_t Offset = getOffset();
571  uint64_t offset = Offset;
572  if (DumpOpts.ShowParents) {
573  DIDumpOptions ParentDumpOpts = DumpOpts;
574  ParentDumpOpts.ShowParents = false;
575  ParentDumpOpts.ShowChildren = false;
576  Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
577  }
578 
579  if (debug_info_data.isValidOffset(offset)) {
580  uint32_t abbrCode = debug_info_data.getULEB128(&offset);
581  if (DumpOpts.ShowAddresses)
583  << format("\n0x%8.8" PRIx64 ": ", Offset);
584 
585  if (abbrCode) {
586  auto AbbrevDecl = getAbbreviationDeclarationPtr();
587  if (AbbrevDecl) {
588  WithColor(OS, HighlightColor::Tag).get().indent(Indent)
589  << formatv("{0}", getTag());
590  if (DumpOpts.Verbose)
591  OS << format(" [%u] %c", abbrCode,
592  AbbrevDecl->hasChildren() ? '*' : ' ');
593  OS << '\n';
594 
595  // Dump all data in the DIE for the attributes.
596  for (const auto &AttrSpec : AbbrevDecl->attributes()) {
597  if (AttrSpec.Form == DW_FORM_implicit_const) {
598  // We are dumping .debug_info section ,
599  // implicit_const attribute values are not really stored here,
600  // but in .debug_abbrev section. So we just skip such attrs.
601  continue;
602  }
603  dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
604  Indent, DumpOpts);
605  }
606 
607  DWARFDie child = getFirstChild();
608  if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0 && child) {
609  DumpOpts.ChildRecurseDepth--;
610  DIDumpOptions ChildDumpOpts = DumpOpts;
611  ChildDumpOpts.ShowParents = false;
612  while (child) {
613  child.dump(OS, Indent + 2, ChildDumpOpts);
614  child = child.getSibling();
615  }
616  }
617  } else {
618  OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
619  << abbrCode << '\n';
620  }
621  } else {
622  OS.indent(Indent) << "NULL\n";
623  }
624  }
625 }
626 
628 
630  if (isValid())
631  return U->getParent(Die);
632  return DWARFDie();
633 }
634 
636  if (isValid())
637  return U->getSibling(Die);
638  return DWARFDie();
639 }
640 
642  if (isValid())
643  return U->getPreviousSibling(Die);
644  return DWARFDie();
645 }
646 
648  if (isValid())
649  return U->getFirstChild(Die);
650  return DWARFDie();
651 }
652 
654  if (isValid())
655  return U->getLastChild(Die);
656  return DWARFDie();
657 }
658 
660  return make_range(attribute_iterator(*this, false),
661  attribute_iterator(*this, true));
662 }
663 
665  : Die(D), Index(0) {
666  auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
667  assert(AbbrDecl && "Must have abbreviation declaration");
668  if (End) {
669  // This is the end iterator so we set the index to the attribute count.
670  Index = AbbrDecl->getNumAttributes();
671  } else {
672  // This is the begin iterator so we extract the value for this->Index.
673  AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
674  updateForIndex(*AbbrDecl, 0);
675  }
676 }
677 
678 void DWARFDie::attribute_iterator::updateForIndex(
679  const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
680  Index = I;
681  // AbbrDecl must be valid before calling this function.
682  auto NumAttrs = AbbrDecl.getNumAttributes();
683  if (Index < NumAttrs) {
684  AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
685  // Add the previous byte size of any previous attribute value.
686  AttrValue.Offset += AttrValue.ByteSize;
687  uint64_t ParseOffset = AttrValue.Offset;
688  auto U = Die.getDwarfUnit();
689  assert(U && "Die must have valid DWARF unit");
691  AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
692  AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
693  } else {
694  assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
695  AttrValue = {};
696  }
697 }
698 
700  if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
701  updateForIndex(*AbbrDecl, Index + 1);
702  return *this;
703 }
704 
706  switch (Attr) {
707  // From the DWARF v5 specification.
708  case DW_AT_location:
709  case DW_AT_byte_size:
710  case DW_AT_bit_size:
711  case DW_AT_string_length:
712  case DW_AT_lower_bound:
713  case DW_AT_return_addr:
714  case DW_AT_bit_stride:
715  case DW_AT_upper_bound:
716  case DW_AT_count:
717  case DW_AT_data_member_location:
718  case DW_AT_frame_base:
719  case DW_AT_segment:
720  case DW_AT_static_link:
721  case DW_AT_use_location:
722  case DW_AT_vtable_elem_location:
723  case DW_AT_allocated:
724  case DW_AT_associated:
725  case DW_AT_byte_stride:
726  case DW_AT_rank:
727  case DW_AT_call_value:
728  case DW_AT_call_origin:
729  case DW_AT_call_target:
730  case DW_AT_call_target_clobbered:
731  case DW_AT_call_data_location:
732  case DW_AT_call_data_value:
733  // Extensions.
734  case DW_AT_GNU_call_site_value:
735  case DW_AT_GNU_call_site_target:
736  return true;
737  default:
738  return false;
739  }
740 }
void dump(raw_ostream &OS, DIDumpOptions DumpOpts=DIDumpOptions()) const
const NoneType None
Definition: None.h:23
uint64_t CallInst * C
iterator_range< typename GraphTraits< GraphType >::ChildIteratorType > children(const typename GraphTraits< GraphType >::NodeRef &G)
Definition: GraphTraits.h:121
StringRef ApplePropertyString(unsigned)
Definition: Dwarf.cpp:515
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:53
bool isValid() const
Definition: DWARFDie.h:50
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:281
Optional< UnitOffset > getAsRelativeReference() const
dwarf::Attribute Attr
The attribute enumeration of this attribute.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:484
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:358
An RAII object that temporarily switches an output stream to a specific color.
Definition: WithColor.h:37
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool addressRangeContainsAddress(const uint64_t Address) const
Definition: DWARFDie.cpp:506
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
Attribute
Attributes.
Definition: Dwarf.h:102
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:60
attribute_iterator & operator++()
Definition: DWARFDie.cpp:699
uint32_t ByteSize
The debug info/types section byte size of the data for this attribute.
void collectChildrenAddressRanges(DWARFAddressRangesVector &Ranges) const
Get all address ranges for any DW_TAG_subprogram DIEs in this DIE or any of its children.
Definition: DWARFDie.cpp:490
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
const DWARFSection * getLocSection() const
Definition: DWARFUnit.h:277
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition: DWARFDie.h:58
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
auto formatv(const char *Fmt, Ts &&... Vals) -> formatv_object< decltype(std::make_tuple(detail::build_format_adapter(std::forward< Ts >(Vals))...))>
const DWARFDebugLine::LineTable * getLineTableForUnit(DWARFUnit *U)
Get a pointer to a parsed line table corresponding to a compile unit.
DWARFDie getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE as the referenced DIE.
Definition: DWARFDie.cpp:423
A debug info location.
Definition: DebugLoc.h:33
F(f)
DINameKind
A DINameKind is passed to name search methods to specify a preference regarding the type of name reso...
Definition: DIContext.h:131
Optional< unsigned > LanguageLowerBound(SourceLanguage L)
Definition: Dwarf.cpp:346
bool isFormClass(FormClass FC) const
void dump(raw_ostream &OS, unsigned indent=0, DIDumpOptions DumpOpts=DIDumpOptions()) const
Dump the DIE and all of its attributes to the supplied stream.
Definition: DWARFDie.cpp:565
static DWARFFormValue createFromUnit(dwarf::Form F, const DWARFUnit *Unit, uint64_t *OffsetPtr)
dwarf::Form getForm() const
DWARFFormValue Value
The form and value for this attribute.
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0&#39;s from the least significant bit to the most stopping at the first 1...
Definition: MathExtras.h:156
iterator_range< iterator > children() const
Definition: DWARFDie.h:382
const char * getCompilationDir()
Definition: DWARFUnit.cpp:347
Optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition: DWARFDie.cpp:440
Optional< ArrayRef< uint8_t > > getAsBlock() const
static StringRef getName(Value *V)
void getCallerFrame(uint32_t &CallFile, uint32_t &CallLine, uint32_t &CallColumn, uint32_t &CallDiscriminator) const
Retrieves values of DW_AT_call_file, DW_AT_call_line and DW_AT_call_column from DIE (or zeroes if the...
Definition: DWARFDie.cpp:544
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:592
const MCRegisterInfo * getRegisterInfo() const
Definition: DWARFContext.h:344
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:284
Optional< object::SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)
bool isDWOUnit() const
Definition: DWARFUnit.h:274
static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val)
Definition: DWARFDie.cpp:40
dwarf::Form getFormByIndex(uint32_t idx) const
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
llvm::Optional< object::SectionedAddress > getBaseAddress()
Definition: DWARFUnit.cpp:767
static void dumpRanges(const DWARFObject &Obj, raw_ostream &OS, const DWARFAddressRangesVector &Ranges, unsigned AddressSize, unsigned Indent, const DIDumpOptions &DumpOpts)
Definition: DWARFDie.cpp:58
static Error getOffset(const SymbolRef &Sym, SectionRef Sec, uint64_t &Result)
bool getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC, uint64_t &SectionIndex) const
Retrieves DW_AT_low_pc and DW_AT_high_pc from CU.
Definition: DWARFDie.cpp:458
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
uint16_t getVersion() const
Definition: DWARFUnit.h:283
DWARFDie getSibling() const
Get the sibling of this DIE object.
Definition: DWARFDie.cpp:635
StringRef AttributeValueString(uint16_t Attr, unsigned Val)
Returns the symbolic string representing Val when used as a value for attribute Attr.
Definition: Dwarf.cpp:589
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:540
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
unsigned ParentRecurseDepth
Definition: DIContext.h:174
Container for dump options that control which debug information will be dumped.
Definition: DIContext.h:171
raw_ostream & get()
Definition: WithColor.h:64
Optional< uint64_t > toSectionOffset(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an section offset.
Utility class that carries the DWARF compile/type unit and the debug info entry in an object...
Definition: DWARFDie.h:42
DWARFDie getLastChild() const
Get the last child of this DIE object.
Definition: DWARFDie.cpp:653
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
uint64_t Offset
The debug info/types offset for this attribute.
virtual uint8_t getAddressSize() const
Definition: DWARFObject.h:34
Instrumentation for Order File
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
bool isSubroutineDIE() const
Returns true if DIE represents a subprogram or an inlined subroutine.
Definition: DWARFDie.cpp:360
static void dumpAddressSection(const DWARFObject &Obj, raw_ostream &OS, DIDumpOptions DumpOpts, uint64_t SectionIndex)
Optional< DWARFFormValue > findRecursively(ArrayRef< dwarf::Attribute > Attrs) const
Extract the first value of any attribute in Attrs from this DIE and recurse into any DW_AT_specificat...
Definition: DWARFDie.cpp:389
Expected< LocationList > parseOneLocationList(const DWARFDataExtractor &Data, uint64_t *Offset)
detail::ErrorAdapter fmt_consume(Error &&Item)
Optional< uint64_t > getAsUnsignedConstant() const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
DWARFContext & getContext() const
Definition: DWARFUnit.h:275
static DWARFFormValue createFromUValue(dwarf::Form F, uint64_t V)
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn&#39;t already there.
Definition: SmallSet.h:180
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1186
static void dumpArrayType(raw_ostream &OS, const DWARFDie &D)
Definition: DWARFDie.cpp:147
const T * data() const
Definition: ArrayRef.h:145
unsigned getTag(StringRef TagString)
Definition: Dwarf.cpp:32
A DataExtractor (typically for an in-memory copy of an object-file section) plus a relocation map for...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
DWARFDie getUnitDIE(bool ExtractUnitDIEOnly=true)
Definition: DWARFUnit.h:386
static Expected< LocationList > parseOneLocationList(const DataExtractor &Data, uint64_t *Offset, unsigned Version)
const char * getName(DINameKind Kind) const
Return the DIE name resolving DW_AT_sepcification or DW_AT_abstract_origin references if necessary...
Definition: DWARFDie.cpp:525
Color
A "color", which is either even or odd.
DWARFDie getPreviousSibling() const
Get the previous sibling of this DIE object.
Definition: DWARFDie.cpp:641
DWARFContext This data structure is the top level entity that deals with dwarf debug information pars...
Definition: DWARFContext.h:58
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:659
Optional< const char * > toString(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract a string value from it.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
StringRef getLocSectionData() const
Definition: DWARFUnit.h:278
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
LLVM_DUMP_METHOD void dump() const
Convenience zero-argument overload for debugging.
Definition: DWARFDie.cpp:627
dwarf::Tag getTag() const
Definition: DWARFDie.h:71
#define I(x, y, z)
Definition: MD5.cpp:58
virtual ArrayRef< SectionName > getSectionNames() const
Definition: DWARFObject.h:32
static bool mayHaveLocationDescription(dwarf::Attribute Attr)
Identifies DWARF attributes that may contain a reference to a DWARF expression.
Definition: DWARFDie.cpp:705
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:473
unsigned ChildRecurseDepth
Definition: DIContext.h:173
Optional< uint64_t > getRnglistOffset(uint32_t Index)
Return a rangelist&#39;s offset based on an index.
Definition: DWARFUnit.h:419
bool isLittleEndian() const
Definition: DWARFContext.h:337
const DWARFObject & getDWARFObj() const
Definition: DWARFContext.h:116
Optional< uint64_t > getHighPC(uint64_t LowPC) const
Get the DW_AT_high_pc attribute value as an address.
Definition: DWARFDie.cpp:444
static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent, DIDumpOptions DumpOpts, unsigned Depth=0)
Helper to dump a DIE with all of its parents, but no siblings.
Definition: DWARFDie.cpp:554
dwarf::Attribute getAttrByIndex(uint32_t idx) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Optional< DWARFFormValue > find(dwarf::Attribute Attr) const
Extract the specified attribute from this DIE.
Definition: DWARFDie.cpp:365
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
LLVM Value Representation.
Definition: Value.h:73
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.cpp:647
static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue, DWARFUnit *U, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:78
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:66
static const Function * getParent(const Value *V)
static void dumpTypeName(raw_ostream &OS, const DWARFDie &D)
Recursively dump the DIE type name when applicable.
Definition: DWARFDie.cpp:194
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die, uint64_t *OffsetPtr, dwarf::Attribute Attr, dwarf::Form Form, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:265
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Optional< uint64_t > getAsSectionOffset() const
virtual const DWARFSection & getLoclistsSection() const
Definition: DWARFObject.h:41
const char * getSubroutineName(DINameKind Kind) const
If a DIE represents a subprogram (or inlined subroutine), returns its mangled name (or short name...
Definition: DWARFDie.cpp:519
Optional< uint64_t > toUnsigned(const Optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
static void dumpTypeTagName(raw_ostream &OS, dwarf::Tag T)
Dump the name encoded in the type tag.
Definition: DWARFDie.cpp:140
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:629