1 //===- DWARFDie.cpp -------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
11 #include "SyntaxHighlighting.h"
12 #include "llvm/ADT/None.h"
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
17 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
18 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
20 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
21 #include "llvm/Support/DataExtractor.h"
22 #include "llvm/Support/Format.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/raw_ostream.h"
33 using namespace dwarf;
34 using namespace syntax;
36 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
39 uint64_t Shift = countTrailingZeros(Val);
40 assert(Shift < 64 && "undefined behavior");
41 uint64_t Bit = 1ULL << Shift;
42 auto PropName = ApplePropertyString(Bit);
43 if (!PropName.empty())
46 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
54 static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
55 unsigned AddressSize, unsigned Indent) {
59 for (const auto &Range: Ranges) {
62 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
63 AddressSize*2, Range.LowPC,
64 AddressSize*2, Range.HighPC);
68 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
69 uint32_t *OffsetPtr, dwarf::Attribute Attr,
70 dwarf::Form Form, unsigned Indent,
71 DIDumpOptions DumpOpts) {
74 const char BaseIndent[] = " ";
77 auto attrString = AttributeString(Attr);
78 if (!attrString.empty())
79 WithColor(OS, syntax::Attribute) << attrString;
81 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
83 if (!DumpOpts.Brief) {
84 auto formString = FormEncodingString(Form);
85 if (!formString.empty())
86 OS << " [" << formString << ']';
88 OS << format(" [DW_FORM_Unknown_%x]", Form);
91 DWARFUnit *U = Die.getDwarfUnit();
92 DWARFFormValue formValue(Form);
94 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
101 auto Color = syntax::Enumerator;
102 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
103 Color = syntax::String;
104 if (const auto *LT = U->getContext().getLineTableForUnit(U))
105 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
106 File = '"' + File + '"';
109 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
110 Name = AttributeValueString(Attr, *Val);
113 WithColor(OS, Color) << Name;
114 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
115 OS << *formValue.getAsUnsignedConstant();
119 // We have dumped the attribute raw value. For some attributes
120 // having both the raw value and the pretty-printed value is
121 // interesting. These attributes are handled below.
122 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
123 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
124 OS << " \"" << Name << '\"';
125 } else if (Attr == DW_AT_APPLE_property_attribute) {
126 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
127 dumpApplePropertyAttribute(OS, *OptVal);
128 } else if (Attr == DW_AT_ranges) {
129 dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
130 sizeof(BaseIndent)+Indent+4);
136 bool DWARFDie::isSubprogramDIE() const {
137 return getTag() == DW_TAG_subprogram;
140 bool DWARFDie::isSubroutineDIE() const {
142 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
145 Optional<DWARFFormValue>
146 DWARFDie::find(dwarf::Attribute Attr) const {
149 auto AbbrevDecl = getAbbreviationDeclarationPtr();
151 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
155 Optional<DWARFFormValue>
156 DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
159 auto AbbrevDecl = getAbbreviationDeclarationPtr();
161 for (auto Attr : Attrs) {
162 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
169 Optional<DWARFFormValue>
170 DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
174 if (auto Value = Die.find(Attrs))
176 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
178 if (auto Value = Die.find(Attrs))
180 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
182 if (auto Value = Die.find(Attrs))
188 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
189 auto SpecRef = toReference(find(Attr));
191 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
193 return SpecUnit->getDIEForOffset(*SpecRef);
199 DWARFDie::getRangesBaseAttribute() const {
200 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
203 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
204 if (auto FormValue = find(DW_AT_high_pc)) {
205 if (auto Address = FormValue->getAsAddress()) {
206 // High PC is an address.
209 if (auto Offset = FormValue->getAsUnsignedConstant()) {
210 // High PC is an offset from LowPC.
211 return LowPC + *Offset;
217 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
218 uint64_t &SectionIndex) const {
219 auto F = find(DW_AT_low_pc);
220 auto LowPcAddr = toAddress(F);
223 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
225 HighPC = *HighPcAddr;
226 SectionIndex = F->getSectionIndex();
232 DWARFAddressRangesVector
233 DWARFDie::getAddressRanges() const {
235 return DWARFAddressRangesVector();
236 // Single range specified by low/high PC.
237 uint64_t LowPC, HighPC, Index;
238 if (getLowAndHighPC(LowPC, HighPC, Index))
239 return {{LowPC, HighPC, Index}};
241 // Multiple ranges from .debug_ranges section.
242 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
244 DWARFDebugRangeList RangeList;
245 if (U->extractRangeList(*RangesOffset, RangeList))
246 return RangeList.getAbsoluteRanges(U->getBaseAddress());
248 return DWARFAddressRangesVector();
252 DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
255 if (isSubprogramDIE()) {
256 const auto &DIERanges = getAddressRanges();
257 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
260 for (auto Child: children())
261 Child.collectChildrenAddressRanges(Ranges);
264 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
265 for (const auto& R : getAddressRanges()) {
266 if (R.LowPC <= Address && Address < R.HighPC)
273 DWARFDie::getSubroutineName(DINameKind Kind) const {
274 if (!isSubroutineDIE())
276 return getName(Kind);
280 DWARFDie::getName(DINameKind Kind) const {
281 if (!isValid() || Kind == DINameKind::None)
283 // Try to get mangled name only if it was asked for.
284 if (Kind == DINameKind::LinkageName) {
285 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
286 DW_AT_linkage_name}), nullptr))
289 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
294 uint64_t DWARFDie::getDeclLine() const {
295 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
298 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
299 uint32_t &CallColumn,
300 uint32_t &CallDiscriminator) const {
301 CallFile = toUnsigned(find(DW_AT_call_file), 0);
302 CallLine = toUnsigned(find(DW_AT_call_line), 0);
303 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
304 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
307 void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth, unsigned Indent,
308 DIDumpOptions DumpOpts) const {
311 DWARFDataExtractor debug_info_data = U->getDebugInfoExtractor();
312 const uint32_t Offset = getOffset();
313 uint32_t offset = Offset;
315 if (debug_info_data.isValidOffset(offset)) {
316 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
317 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
320 auto AbbrevDecl = getAbbreviationDeclarationPtr();
322 auto tagString = TagString(getTag());
323 if (!tagString.empty())
324 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
326 WithColor(OS, syntax::Tag).get().indent(Indent)
327 << format("DW_TAG_Unknown_%x", getTag());
330 OS << format(" [%u] %c", abbrCode,
331 AbbrevDecl->hasChildren() ? '*' : ' ');
334 // Dump all data in the DIE for the attributes.
335 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
336 if (AttrSpec.Form == DW_FORM_implicit_const) {
337 // We are dumping .debug_info section ,
338 // implicit_const attribute values are not really stored here,
339 // but in .debug_abbrev section. So we just skip such attrs.
342 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
346 DWARFDie child = getFirstChild();
347 if (RecurseDepth > 0 && child) {
349 child.dump(OS, RecurseDepth-1, Indent+2, DumpOpts);
350 child = child.getSibling();
354 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
358 OS.indent(Indent) << "NULL\n";
363 DWARFDie DWARFDie::getParent() const {
365 return U->getParent(Die);
369 DWARFDie DWARFDie::getSibling() const {
371 return U->getSibling(Die);
375 iterator_range<DWARFDie::attribute_iterator>
376 DWARFDie::attributes() const {
377 return make_range(attribute_iterator(*this, false),
378 attribute_iterator(*this, true));
381 DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
382 Die(D), AttrValue(0), Index(0) {
383 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
384 assert(AbbrDecl && "Must have abbreviation declaration");
386 // This is the end iterator so we set the index to the attribute count.
387 Index = AbbrDecl->getNumAttributes();
389 // This is the begin iterator so we extract the value for this->Index.
390 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
391 updateForIndex(*AbbrDecl, 0);
395 void DWARFDie::attribute_iterator::updateForIndex(
396 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
398 // AbbrDecl must be valid before calling this function.
399 auto NumAttrs = AbbrDecl.getNumAttributes();
400 if (Index < NumAttrs) {
401 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
402 // Add the previous byte size of any previous attribute value.
403 AttrValue.Offset += AttrValue.ByteSize;
404 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
405 uint32_t ParseOffset = AttrValue.Offset;
406 auto U = Die.getDwarfUnit();
407 assert(U && "Die must have valid DWARF unit");
408 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
411 assert(b && "extractValue cannot fail on fully parsed DWARF");
412 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
414 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
419 DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
420 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
421 updateForIndex(*AbbrDecl, Index + 1);