LLVM  10.0.0
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  for (const DWARFAddressRange &R : Ranges) {
66  OS << '\n';
67  OS.indent(Indent);
68  R.dump(OS, AddressSize, DumpOpts, &Obj);
69  }
70 }
71 
72 static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue,
73  DWARFUnit *U, unsigned Indent,
74  DIDumpOptions DumpOpts) {
75  DWARFContext &Ctx = U->getContext();
76  const MCRegisterInfo *MRI = Ctx.getRegisterInfo();
77  if (FormValue.isFormClass(DWARFFormValue::FC_Block) ||
79  ArrayRef<uint8_t> Expr = *FormValue.getAsBlock();
80  DataExtractor Data(StringRef((const char *)Expr.data(), Expr.size()),
81  Ctx.isLittleEndian(), 0);
83  .print(OS, MRI, U);
84  return;
85  }
86 
88  uint64_t Offset = *FormValue.getAsSectionOffset();
89 
90  if (FormValue.getForm() == DW_FORM_loclistx) {
91  FormValue.dump(OS, DumpOpts);
92 
93  if (auto LoclistOffset = U->getLoclistOffset(Offset))
94  Offset = *LoclistOffset;
95  else
96  return;
97  }
98  U->getLocationTable().dumpLocationList(&Offset, OS, U->getBaseAddress(),
99  MRI, Ctx.getDWARFObj(), U, DumpOpts,
100  Indent);
101  return;
102  }
103 
104  FormValue.dump(OS, DumpOpts);
105 }
106 
107 /// Dump the name encoded in the type tag.
109  StringRef TagStr = TagString(T);
110  if (!TagStr.startswith("DW_TAG_") || !TagStr.endswith("_type"))
111  return;
112  OS << TagStr.substr(7, TagStr.size() - 12) << " ";
113 }
114 
115 static void dumpArrayType(raw_ostream &OS, const DWARFDie &D) {
116  Optional<uint64_t> Bound;
117  for (const DWARFDie &C : D.children())
118  if (C.getTag() == DW_TAG_subrange_type) {
120  Optional<uint64_t> Count;
122  Optional<unsigned> DefaultLB;
123  if (Optional<DWARFFormValue> L = C.find(DW_AT_lower_bound))
124  LB = L->getAsUnsignedConstant();
125  if (Optional<DWARFFormValue> CountV = C.find(DW_AT_count))
126  Count = CountV->getAsUnsignedConstant();
127  if (Optional<DWARFFormValue> UpperV = C.find(DW_AT_upper_bound))
128  UB = UpperV->getAsUnsignedConstant();
129  if (Optional<DWARFFormValue> LV =
130  D.getDwarfUnit()->getUnitDIE().find(DW_AT_language))
131  if (Optional<uint64_t> LC = LV->getAsUnsignedConstant())
132  if ((DefaultLB =
133  LanguageLowerBound(static_cast<dwarf::SourceLanguage>(*LC))))
134  if (LB && *LB == *DefaultLB)
135  LB = None;
136  if (!LB && !Count && !UB)
137  OS << "[]";
138  else if (!LB && (Count || UB) && DefaultLB)
139  OS << '[' << (Count ? *Count : *UB - *DefaultLB + 1) << ']';
140  else {
141  OS << "[[";
142  if (LB)
143  OS << *LB;
144  else
145  OS << '?';
146  OS << ", ";
147  if (Count)
148  if (LB)
149  OS << *LB + *Count;
150  else
151  OS << "? + " << *Count;
152  else if (UB)
153  OS << *UB + 1;
154  else
155  OS << '?';
156  OS << ")]";
157  }
158  }
159 }
160 
161 /// Recursively dump the DIE type name when applicable.
162 static void dumpTypeName(raw_ostream &OS, const DWARFDie &D) {
163  if (!D.isValid())
164  return;
165 
166  if (const char *Name = D.getName(DINameKind::LinkageName)) {
167  OS << Name;
168  return;
169  }
170 
171  // FIXME: We should have pretty printers per language. Currently we print
172  // everything as if it was C++ and fall back to the TAG type name.
173  const dwarf::Tag T = D.getTag();
174  switch (T) {
175  case DW_TAG_array_type:
176  case DW_TAG_pointer_type:
177  case DW_TAG_ptr_to_member_type:
178  case DW_TAG_reference_type:
179  case DW_TAG_rvalue_reference_type:
180  case DW_TAG_subroutine_type:
181  break;
182  default:
183  dumpTypeTagName(OS, T);
184  }
185 
186  // Follow the DW_AT_type if possible.
187  DWARFDie TypeDie = D.getAttributeValueAsReferencedDie(DW_AT_type);
188  dumpTypeName(OS, TypeDie);
189 
190  switch (T) {
191  case DW_TAG_subroutine_type: {
192  if (!TypeDie)
193  OS << "void";
194  OS << '(';
195  bool First = true;
196  for (const DWARFDie &C : D.children()) {
197  if (C.getTag() == DW_TAG_formal_parameter) {
198  if (!First)
199  OS << ", ";
200  First = false;
201  dumpTypeName(OS, C.getAttributeValueAsReferencedDie(DW_AT_type));
202  }
203  }
204  OS << ')';
205  break;
206  }
207  case DW_TAG_array_type: {
208  dumpArrayType(OS, D);
209  break;
210  }
211  case DW_TAG_pointer_type:
212  OS << '*';
213  break;
214  case DW_TAG_ptr_to_member_type:
215  if (DWARFDie Cont =
216  D.getAttributeValueAsReferencedDie(DW_AT_containing_type)) {
217  dumpTypeName(OS << ' ', Cont);
218  OS << "::";
219  }
220  OS << '*';
221  break;
222  case DW_TAG_reference_type:
223  OS << '&';
224  break;
225  case DW_TAG_rvalue_reference_type:
226  OS << "&&";
227  break;
228  default:
229  break;
230  }
231 }
232 
233 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
234  uint64_t *OffsetPtr, dwarf::Attribute Attr,
235  dwarf::Form Form, unsigned Indent,
236  DIDumpOptions DumpOpts) {
237  if (!Die.isValid())
238  return;
239  const char BaseIndent[] = " ";
240  OS << BaseIndent;
241  OS.indent(Indent + 2);
242  WithColor(OS, HighlightColor::Attribute) << formatv("{0}", Attr);
243 
244  if (DumpOpts.Verbose || DumpOpts.ShowForm)
245  OS << formatv(" [{0}]", Form);
246 
247  DWARFUnit *U = Die.getDwarfUnit();
248  DWARFFormValue FormValue = DWARFFormValue::createFromUnit(Form, U, OffsetPtr);
249 
250  OS << "\t(";
251 
252  StringRef Name;
253  std::string File;
255  if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
257  if (const auto *LT = U->getContext().getLineTableForUnit(U))
258  if (LT->getFileNameByIndex(
259  FormValue.getAsUnsignedConstant().getValue(),
260  U->getCompilationDir(),
262  File = '"' + File + '"';
263  Name = File;
264  }
265  } else if (Optional<uint64_t> Val = FormValue.getAsUnsignedConstant())
266  Name = AttributeValueString(Attr, *Val);
267 
268  if (!Name.empty())
269  WithColor(OS, Color) << Name;
270  else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
271  OS << *FormValue.getAsUnsignedConstant();
272  else if (Attr == DW_AT_high_pc && !DumpOpts.ShowForm && !DumpOpts.Verbose &&
273  FormValue.getAsUnsignedConstant()) {
274  if (DumpOpts.ShowAddresses) {
275  // Print the actual address rather than the offset.
276  uint64_t LowPC, HighPC, Index;
277  if (Die.getLowAndHighPC(LowPC, HighPC, Index))
278  OS << format("0x%016" PRIx64, HighPC);
279  else
280  FormValue.dump(OS, DumpOpts);
281  }
282  } else if (Form == dwarf::Form::DW_FORM_exprloc ||
284  dumpLocation(OS, FormValue, U, sizeof(BaseIndent) + Indent + 4, DumpOpts);
285  else
286  FormValue.dump(OS, DumpOpts);
287 
288  std::string Space = DumpOpts.ShowAddresses ? " " : "";
289 
290  // We have dumped the attribute raw value. For some attributes
291  // having both the raw value and the pretty-printed value is
292  // interesting. These attributes are handled below.
293  if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
294  if (const char *Name =
297  OS << Space << "\"" << Name << '\"';
298  } else if (Attr == DW_AT_type) {
299  OS << Space << "\"";
301  OS << '"';
302  } else if (Attr == DW_AT_APPLE_property_attribute) {
303  if (Optional<uint64_t> OptVal = FormValue.getAsUnsignedConstant())
304  dumpApplePropertyAttribute(OS, *OptVal);
305  } else if (Attr == DW_AT_ranges) {
306  const DWARFObject &Obj = Die.getDwarfUnit()->getContext().getDWARFObj();
307  // For DW_FORM_rnglistx we need to dump the offset separately, since
308  // we have only dumped the index so far.
309  if (FormValue.getForm() == DW_FORM_rnglistx)
310  if (auto RangeListOffset =
311  U->getRnglistOffset(*FormValue.getAsSectionOffset())) {
313  dwarf::DW_FORM_sec_offset, *RangeListOffset);
314  FV.dump(OS, DumpOpts);
315  }
316  if (auto RangesOrError = Die.getAddressRanges())
317  dumpRanges(Obj, OS, RangesOrError.get(), U->getAddressByteSize(),
318  sizeof(BaseIndent) + Indent + 4, DumpOpts);
319  else
320  WithColor::error() << "decoding address ranges: "
321  << toString(RangesOrError.takeError()) << '\n';
322  }
323 
324  OS << ")\n";
325 }
326 
327 bool DWARFDie::isSubprogramDIE() const { return getTag() == DW_TAG_subprogram; }
328 
330  auto Tag = getTag();
331  return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
332 }
333 
335  if (!isValid())
336  return None;
337  auto AbbrevDecl = getAbbreviationDeclarationPtr();
338  if (AbbrevDecl)
339  return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
340  return None;
341 }
342 
345  if (!isValid())
346  return None;
347  auto AbbrevDecl = getAbbreviationDeclarationPtr();
348  if (AbbrevDecl) {
349  for (auto Attr : Attrs) {
350  if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
351  return Value;
352  }
353  }
354  return None;
355 }
356 
359  std::vector<DWARFDie> Worklist;
360  Worklist.push_back(*this);
361 
362  // Keep track if DIEs already seen to prevent infinite recursion.
363  // Empirically we rarely see a depth of more than 3 when dealing with valid
364  // DWARF. This corresponds to following the DW_AT_abstract_origin and
365  // DW_AT_specification just once.
367  Seen.insert(*this);
368 
369  while (!Worklist.empty()) {
370  DWARFDie Die = Worklist.back();
371  Worklist.pop_back();
372 
373  if (!Die.isValid())
374  continue;
375 
376  if (auto Value = Die.find(Attrs))
377  return Value;
378 
379  if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
380  if (Seen.insert(D).second)
381  Worklist.push_back(D);
382 
383  if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
384  if (Seen.insert(D).second)
385  Worklist.push_back(D);
386  }
387 
388  return None;
389 }
390 
391 DWARFDie
393  if (Optional<DWARFFormValue> F = find(Attr))
394  return getAttributeValueAsReferencedDie(*F);
395  return DWARFDie();
396 }
397 
398 DWARFDie
400  if (auto SpecRef = V.getAsRelativeReference()) {
401  if (SpecRef->Unit)
402  return SpecRef->Unit->getDIEForOffset(SpecRef->Unit->getOffset() + SpecRef->Offset);
403  if (auto SpecUnit = U->getUnitVector().getUnitForOffset(SpecRef->Offset))
404  return SpecUnit->getDIEForOffset(SpecRef->Offset);
405  }
406  return DWARFDie();
407 }
408 
410  return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
411 }
412 
414  return toSectionOffset(find(DW_AT_loclists_base));
415 }
416 
418  if (auto FormValue = find(DW_AT_high_pc)) {
419  if (auto Address = FormValue->getAsAddress()) {
420  // High PC is an address.
421  return Address;
422  }
423  if (auto Offset = FormValue->getAsUnsignedConstant()) {
424  // High PC is an offset from LowPC.
425  return LowPC + *Offset;
426  }
427  }
428  return None;
429 }
430 
431 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
432  uint64_t &SectionIndex) const {
433  auto F = find(DW_AT_low_pc);
434  auto LowPcAddr = toSectionedAddress(F);
435  if (!LowPcAddr)
436  return false;
437  if (auto HighPcAddr = getHighPC(LowPcAddr->Address)) {
438  LowPC = LowPcAddr->Address;
439  HighPC = *HighPcAddr;
440  SectionIndex = LowPcAddr->SectionIndex;
441  return true;
442  }
443  return false;
444 }
445 
447  if (isNULL())
448  return DWARFAddressRangesVector();
449  // Single range specified by low/high PC.
450  uint64_t LowPC, HighPC, Index;
451  if (getLowAndHighPC(LowPC, HighPC, Index))
452  return DWARFAddressRangesVector{{LowPC, HighPC, Index}};
453 
454  Optional<DWARFFormValue> Value = find(DW_AT_ranges);
455  if (Value) {
456  if (Value->getForm() == DW_FORM_rnglistx)
457  return U->findRnglistFromIndex(*Value->getAsSectionOffset());
458  return U->findRnglistFromOffset(*Value->getAsSectionOffset());
459  }
460  return DWARFAddressRangesVector();
461 }
462 
464  DWARFAddressRangesVector &Ranges) const {
465  if (isNULL())
466  return;
467  if (isSubprogramDIE()) {
468  if (auto DIERangesOrError = getAddressRanges())
469  Ranges.insert(Ranges.end(), DIERangesOrError.get().begin(),
470  DIERangesOrError.get().end());
471  else
472  llvm::consumeError(DIERangesOrError.takeError());
473  }
474 
475  for (auto Child : children())
476  Child.collectChildrenAddressRanges(Ranges);
477 }
478 
479 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
480  auto RangesOrError = getAddressRanges();
481  if (!RangesOrError) {
482  llvm::consumeError(RangesOrError.takeError());
483  return false;
484  }
485 
486  for (const auto &R : RangesOrError.get())
487  if (R.LowPC <= Address && Address < R.HighPC)
488  return true;
489  return false;
490 }
491 
494  Optional<DWARFFormValue> Location = find(Attr);
495  if (!Location)
496  return createStringError(inconvertibleErrorCode(), "No %s",
497  dwarf::AttributeString(Attr).data());
498 
499  if (Optional<uint64_t> Off = Location->getAsSectionOffset()) {
500  uint64_t Offset = *Off;
501 
502  if (Location->getForm() == DW_FORM_loclistx) {
503  if (auto LoclistOffset = U->getLoclistOffset(Offset))
504  Offset = *LoclistOffset;
505  else
507  "Loclist table not found");
508  }
509  return U->findLoclistFromOffset(Offset);
510  }
511 
512  if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
514  DWARFLocationExpression{None, to_vector<4>(*Expr)}};
515  }
516 
517  return createStringError(
518  inconvertibleErrorCode(), "Unsupported %s encoding: %s",
519  dwarf::AttributeString(Attr).data(),
520  dwarf::FormEncodingString(Location->getForm()).data());
521 }
522 
524  if (!isSubroutineDIE())
525  return nullptr;
526  return getName(Kind);
527 }
528 
529 const char *DWARFDie::getName(DINameKind Kind) const {
530  if (!isValid() || Kind == DINameKind::None)
531  return nullptr;
532  // Try to get mangled name only if it was asked for.
533  if (Kind == DINameKind::LinkageName) {
534  if (auto Name = dwarf::toString(
535  findRecursively({DW_AT_MIPS_linkage_name, DW_AT_linkage_name}),
536  nullptr))
537  return Name;
538  }
539  if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
540  return Name;
541  return nullptr;
542 }
543 
544 uint64_t DWARFDie::getDeclLine() const {
545  return toUnsigned(findRecursively(DW_AT_decl_line), 0);
546 }
547 
548 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
549  uint32_t &CallColumn,
550  uint32_t &CallDiscriminator) const {
551  CallFile = toUnsigned(find(DW_AT_call_file), 0);
552  CallLine = toUnsigned(find(DW_AT_call_line), 0);
553  CallColumn = toUnsigned(find(DW_AT_call_column), 0);
554  CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
555 }
556 
557 /// Helper to dump a DIE with all of its parents, but no siblings.
558 static unsigned dumpParentChain(DWARFDie Die, raw_ostream &OS, unsigned Indent,
559  DIDumpOptions DumpOpts, unsigned Depth = 0) {
560  if (!Die)
561  return Indent;
562  if (DumpOpts.ParentRecurseDepth > 0 && Depth >= DumpOpts.ParentRecurseDepth)
563  return Indent;
564  Indent = dumpParentChain(Die.getParent(), OS, Indent, DumpOpts, Depth + 1);
565  Die.dump(OS, Indent, DumpOpts);
566  return Indent + 2;
567 }
568 
569 void DWARFDie::dump(raw_ostream &OS, unsigned Indent,
570  DIDumpOptions DumpOpts) const {
571  if (!isValid())
572  return;
573  DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
574  const uint64_t Offset = getOffset();
575  uint64_t offset = Offset;
576  if (DumpOpts.ShowParents) {
577  DIDumpOptions ParentDumpOpts = DumpOpts;
578  ParentDumpOpts.ShowParents = false;
579  ParentDumpOpts.ShowChildren = false;
580  Indent = dumpParentChain(getParent(), OS, Indent, ParentDumpOpts);
581  }
582 
583  if (debug_info_data.isValidOffset(offset)) {
584  uint32_t abbrCode = debug_info_data.getULEB128(&offset);
585  if (DumpOpts.ShowAddresses)
587  << format("\n0x%8.8" PRIx64 ": ", Offset);
588 
589  if (abbrCode) {
590  auto AbbrevDecl = getAbbreviationDeclarationPtr();
591  if (AbbrevDecl) {
592  WithColor(OS, HighlightColor::Tag).get().indent(Indent)
593  << formatv("{0}", getTag());
594  if (DumpOpts.Verbose)
595  OS << format(" [%u] %c", abbrCode,
596  AbbrevDecl->hasChildren() ? '*' : ' ');
597  OS << '\n';
598 
599  // Dump all data in the DIE for the attributes.
600  for (const auto &AttrSpec : AbbrevDecl->attributes()) {
601  if (AttrSpec.Form == DW_FORM_implicit_const) {
602  // We are dumping .debug_info section ,
603  // implicit_const attribute values are not really stored here,
604  // but in .debug_abbrev section. So we just skip such attrs.
605  continue;
606  }
607  dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
608  Indent, DumpOpts);
609  }
610 
611  DWARFDie child = getFirstChild();
612  if (DumpOpts.ShowChildren && DumpOpts.ChildRecurseDepth > 0 && child) {
613  DumpOpts.ChildRecurseDepth--;
614  DIDumpOptions ChildDumpOpts = DumpOpts;
615  ChildDumpOpts.ShowParents = false;
616  while (child) {
617  child.dump(OS, Indent + 2, ChildDumpOpts);
618  child = child.getSibling();
619  }
620  }
621  } else {
622  OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
623  << abbrCode << '\n';
624  }
625  } else {
626  OS.indent(Indent) << "NULL\n";
627  }
628  }
629 }
630 
632 
634  if (isValid())
635  return U->getParent(Die);
636  return DWARFDie();
637 }
638 
640  if (isValid())
641  return U->getSibling(Die);
642  return DWARFDie();
643 }
644 
646  if (isValid())
647  return U->getPreviousSibling(Die);
648  return DWARFDie();
649 }
650 
652  if (isValid())
653  return U->getFirstChild(Die);
654  return DWARFDie();
655 }
656 
658  if (isValid())
659  return U->getLastChild(Die);
660  return DWARFDie();
661 }
662 
664  return make_range(attribute_iterator(*this, false),
665  attribute_iterator(*this, true));
666 }
667 
669  : Die(D), Index(0) {
670  auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
671  assert(AbbrDecl && "Must have abbreviation declaration");
672  if (End) {
673  // This is the end iterator so we set the index to the attribute count.
674  Index = AbbrDecl->getNumAttributes();
675  } else {
676  // This is the begin iterator so we extract the value for this->Index.
677  AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
678  updateForIndex(*AbbrDecl, 0);
679  }
680 }
681 
682 void DWARFDie::attribute_iterator::updateForIndex(
683  const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
684  Index = I;
685  // AbbrDecl must be valid before calling this function.
686  auto NumAttrs = AbbrDecl.getNumAttributes();
687  if (Index < NumAttrs) {
688  AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
689  // Add the previous byte size of any previous attribute value.
690  AttrValue.Offset += AttrValue.ByteSize;
691  uint64_t ParseOffset = AttrValue.Offset;
692  auto U = Die.getDwarfUnit();
693  assert(U && "Die must have valid DWARF unit");
695  AbbrDecl.getFormByIndex(Index), U, &ParseOffset);
696  AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
697  } else {
698  assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
699  AttrValue = {};
700  }
701 }
702 
704  if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
705  updateForIndex(*AbbrDecl, Index + 1);
706  return *this;
707 }
708 
710  switch (Attr) {
711  // From the DWARF v5 specification.
712  case DW_AT_location:
713  case DW_AT_byte_size:
714  case DW_AT_bit_size:
715  case DW_AT_string_length:
716  case DW_AT_lower_bound:
717  case DW_AT_return_addr:
718  case DW_AT_bit_stride:
719  case DW_AT_upper_bound:
720  case DW_AT_count:
721  case DW_AT_data_member_location:
722  case DW_AT_frame_base:
723  case DW_AT_segment:
724  case DW_AT_static_link:
725  case DW_AT_use_location:
726  case DW_AT_vtable_elem_location:
727  case DW_AT_allocated:
728  case DW_AT_associated:
729  case DW_AT_byte_stride:
730  case DW_AT_rank:
731  case DW_AT_call_value:
732  case DW_AT_call_origin:
733  case DW_AT_call_target:
734  case DW_AT_call_target_clobbered:
735  case DW_AT_call_data_location:
736  case DW_AT_call_data_value:
737  // Extensions.
738  case DW_AT_GNU_call_site_value:
739  case DW_AT_GNU_call_site_target:
740  return true;
741  default:
742  return false;
743  }
744 }
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:531
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFUnit * getDwarfUnit() const
Definition: DWARFDie.h:54
bool isValid() const
Definition: DWARFDie.h:51
LLVM_NODISCARD bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:288
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:492
bool isSubprogramDIE() const
Returns true if DIE represents a subprogram (not inlined).
Definition: DWARFDie.cpp:327
bool dumpLocationList(uint64_t *Offset, raw_ostream &OS, Optional< object::SectionedAddress > BaseAddr, const MCRegisterInfo *MRI, const DWARFObject &Obj, DWARFUnit *U, DIDumpOptions DumpOpts, unsigned Indent) const
Dump the location list at the given Offset.
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:479
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:277
Attribute
Attributes.
Definition: Dwarf.h:103
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:60
attribute_iterator & operator++()
Definition: DWARFDie.cpp:703
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:463
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Get the abbreviation declaration for this DIE.
Definition: DWARFDie.h:59
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:392
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:362
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:569
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:387
const char * getCompilationDir()
Definition: DWARFUnit.cpp:376
Optional< uint64_t > getRangesBaseAttribute() const
Extract the range base attribute from this DIE as absolute section offset.
Definition: DWARFDie.cpp:409
Optional< ArrayRef< uint8_t > > getAsBlock() const
StringRef FormEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:105
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:548
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
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:600
const MCRegisterInfo * getRegisterInfo() const
Definition: DWARFContext.h:344
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:147
uint8_t getAddressByteSize() const
Definition: DWARFUnit.h:284
Optional< object::SectionedAddress > toSectionedAddress(const Optional< DWARFFormValue > &V)
static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val)
Definition: DWARFDie.cpp:40
dwarf::Form getFormByIndex(uint32_t idx) const
StringRef AttributeString(unsigned Attribute)
Definition: Dwarf.cpp:72
const T & getValue() const LLVM_LVALUE_FUNCTION
Definition: Optional.h:255
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:151
llvm::Optional< object::SectionedAddress > getBaseAddress()
Definition: DWARFUnit.cpp:856
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:431
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:639
StringRef AttributeValueString(uint16_t Attr, unsigned Val)
Returns the symbolic string representing Val when used as a value for attribute Attr.
Definition: Dwarf.cpp:605
uint64_t getDeclLine() const
Returns the declaration line (start line) for a DIE, assuming it specifies a subprogram.
Definition: DWARFDie.cpp:544
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:43
DWARFDie getLastChild() const
Get the last child of this DIE object.
Definition: DWARFDie.cpp:657
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
uint64_t Offset
The debug info/types offset for this attribute.
Instrumentation for Order File
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:158
Expected< DWARFLocationExpressionsVector > getLocations(dwarf::Attribute Attr) const
Definition: DWARFDie.cpp:493
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:329
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:358
Optional< uint64_t > getAsUnsignedConstant() const
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
DWARFContext & getContext() const
Definition: DWARFUnit.h:277
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:1001
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:1203
static void dumpArrayType(raw_ostream &OS, const DWARFDie &D)
Definition: DWARFDie.cpp:115
const T * data() const
Definition: ArrayRef.h:155
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:396
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:529
Color
A "color", which is either even or odd.
Represents a single DWARF expression, whose value is location-dependent.
DWARFDie getPreviousSibling() const
Get the previous sibling of this DIE object.
Definition: DWARFDie.cpp:645
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")
Optional< uint64_t > getLocBaseAttribute() const
Definition: DWARFDie.cpp:413
iterator_range< attribute_iterator > attributes() const
Get an iterator range to all attributes in the current DIE only.
Definition: DWARFDie.cpp:663
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.
A range adaptor for a pair of iterators.
This file contains constants used for implementing Dwarf debug support.
std::vector< DWARFLocationExpression > DWARFLocationExpressionsVector
Represents a set of absolute location expressions.
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
const DWARFLocationTable & getLocationTable()
Definition: DWARFUnit.h:330
LLVM_DUMP_METHOD void dump() const
Convenience zero-argument overload for debugging.
Definition: DWARFDie.cpp:631
dwarf::Tag getTag() const
Definition: DWARFDie.h:72
#define I(x, y, z)
Definition: MD5.cpp:58
static bool mayHaveLocationDescription(dwarf::Attribute Attr)
Identifies DWARF attributes that may contain a reference to a DWARF expression.
Definition: DWARFDie.cpp:709
Expected< DWARFAddressRangesVector > getAddressRanges() const
Get the address ranges for this DIE.
Definition: DWARFDie.cpp:446
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:429
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:417
Optional< uint64_t > getLoclistOffset(uint32_t Index)
Definition: DWARFUnit.h:437
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:558
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:334
std::vector< DWARFAddressRange > DWARFAddressRangesVector
DWARFAddressRangesVector - represents a set of absolute address ranges.
LLVM Value Representation.
Definition: Value.h:74
DWARFDie getFirstChild() const
Get the first child of this DIE object.
Definition: DWARFDie.cpp:651
static void dumpLocation(raw_ostream &OS, DWARFFormValue &FormValue, DWARFUnit *U, unsigned Indent, DIDumpOptions DumpOpts)
Definition: DWARFDie.cpp:72
uint64_t getOffset() const
Get the absolute offset into the debug info or types section.
Definition: DWARFDie.h:67
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:162
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
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:233
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Optional< uint64_t > getAsSectionOffset() const
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:523
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:108
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1197
DWARFDie getParent() const
Get the parent of this DIE object.
Definition: DWARFDie.cpp:633
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77