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 "SyntaxHighlighting.h"
11 #include "llvm/ADT/None.h"
12 #include "llvm/ADT/Optional.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/DebugInfo/DWARF/DWARFAbbreviationDeclaration.h"
15 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
16 #include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
17 #include "llvm/DebugInfo/DWARF/DWARFDie.h"
18 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
19 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
20 #include "llvm/Support/DataExtractor.h"
21 #include "llvm/Support/Dwarf.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) {
73 const char BaseIndent[] = " ";
76 auto attrString = AttributeString(Attr);
77 if (!attrString.empty())
78 WithColor(OS, syntax::Attribute) << attrString;
80 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
82 auto formString = FormEncodingString(Form);
83 if (!formString.empty())
84 OS << " [" << formString << ']';
86 OS << format(" [DW_FORM_Unknown_%x]", Form);
88 DWARFUnit *U = Die.getDwarfUnit();
89 DWARFFormValue formValue(Form);
91 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
98 auto Color = syntax::Enumerator;
99 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
100 Color = syntax::String;
101 if (const auto *LT = U->getContext().getLineTableForUnit(U))
102 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
103 File = '"' + File + '"';
106 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
107 Name = AttributeValueString(Attr, *Val);
110 WithColor(OS, Color) << Name;
111 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
112 OS << *formValue.getAsUnsignedConstant();
116 // We have dumped the attribute raw value. For some attributes
117 // having both the raw value and the pretty-printed value is
118 // interesting. These attributes are handled below.
119 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
120 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
121 OS << " \"" << Name << '\"';
122 } else if (Attr == DW_AT_APPLE_property_attribute) {
123 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
124 dumpApplePropertyAttribute(OS, *OptVal);
125 } else if (Attr == DW_AT_ranges) {
126 dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
127 sizeof(BaseIndent)+Indent+4);
133 bool DWARFDie::isSubprogramDIE() const {
134 return getTag() == DW_TAG_subprogram;
137 bool DWARFDie::isSubroutineDIE() const {
139 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
142 Optional<DWARFFormValue>
143 DWARFDie::find(dwarf::Attribute Attr) const {
146 auto AbbrevDecl = getAbbreviationDeclarationPtr();
148 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
152 Optional<DWARFFormValue>
153 DWARFDie::find(ArrayRef<dwarf::Attribute> Attrs) const {
156 auto AbbrevDecl = getAbbreviationDeclarationPtr();
158 for (auto Attr : Attrs) {
159 if (auto Value = AbbrevDecl->getAttributeValue(getOffset(), Attr, *U))
166 Optional<DWARFFormValue>
167 DWARFDie::findRecursively(ArrayRef<dwarf::Attribute> Attrs) const {
171 if (auto Value = Die.find(Attrs))
173 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_abstract_origin))
175 if (auto Value = Die.find(Attrs))
177 if (auto D = Die.getAttributeValueAsReferencedDie(DW_AT_specification))
179 if (auto Value = Die.find(Attrs))
185 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
186 auto SpecRef = toReference(find(Attr));
188 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
190 return SpecUnit->getDIEForOffset(*SpecRef);
196 DWARFDie::getRangesBaseAttribute() const {
197 return toSectionOffset(find({DW_AT_rnglists_base, DW_AT_GNU_ranges_base}));
200 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
201 if (auto FormValue = find(DW_AT_high_pc)) {
202 if (auto Address = FormValue->getAsAddress()) {
203 // High PC is an address.
206 if (auto Offset = FormValue->getAsUnsignedConstant()) {
207 // High PC is an offset from LowPC.
208 return LowPC + *Offset;
214 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC,
215 uint64_t &SectionIndex) const {
216 auto F = find(DW_AT_low_pc);
217 auto LowPcAddr = toAddress(F);
220 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
222 HighPC = *HighPcAddr;
223 SectionIndex = F->getSectionIndex();
229 DWARFAddressRangesVector
230 DWARFDie::getAddressRanges() const {
232 return DWARFAddressRangesVector();
233 // Single range specified by low/high PC.
234 uint64_t LowPC, HighPC, Index;
235 if (getLowAndHighPC(LowPC, HighPC, Index))
236 return {{LowPC, HighPC, Index}};
238 // Multiple ranges from .debug_ranges section.
239 auto RangesOffset = toSectionOffset(find(DW_AT_ranges));
241 DWARFDebugRangeList RangeList;
242 if (U->extractRangeList(*RangesOffset, RangeList))
243 return RangeList.getAbsoluteRanges(U->getBaseAddress());
245 return DWARFAddressRangesVector();
249 DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
252 if (isSubprogramDIE()) {
253 const auto &DIERanges = getAddressRanges();
254 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
257 for (auto Child: children())
258 Child.collectChildrenAddressRanges(Ranges);
261 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
262 for (const auto& R : getAddressRanges()) {
263 if (R.LowPC <= Address && Address < R.HighPC)
270 DWARFDie::getSubroutineName(DINameKind Kind) const {
271 if (!isSubroutineDIE())
273 return getName(Kind);
277 DWARFDie::getName(DINameKind Kind) const {
278 if (!isValid() || Kind == DINameKind::None)
280 // Try to get mangled name only if it was asked for.
281 if (Kind == DINameKind::LinkageName) {
282 if (auto Name = dwarf::toString(findRecursively({DW_AT_MIPS_linkage_name,
283 DW_AT_linkage_name}), nullptr))
286 if (auto Name = dwarf::toString(findRecursively(DW_AT_name), nullptr))
291 uint64_t DWARFDie::getDeclLine() const {
292 return toUnsigned(findRecursively(DW_AT_decl_line), 0);
295 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
296 uint32_t &CallColumn,
297 uint32_t &CallDiscriminator) const {
298 CallFile = toUnsigned(find(DW_AT_call_file), 0);
299 CallLine = toUnsigned(find(DW_AT_call_line), 0);
300 CallColumn = toUnsigned(find(DW_AT_call_column), 0);
301 CallDiscriminator = toUnsigned(find(DW_AT_GNU_discriminator), 0);
304 void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
305 unsigned Indent) const {
308 DataExtractor debug_info_data = U->getDebugInfoExtractor();
309 const uint32_t Offset = getOffset();
310 uint32_t offset = Offset;
312 if (debug_info_data.isValidOffset(offset)) {
313 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
314 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
317 auto AbbrevDecl = getAbbreviationDeclarationPtr();
319 auto tagString = TagString(getTag());
320 if (!tagString.empty())
321 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
323 WithColor(OS, syntax::Tag).get().indent(Indent)
324 << format("DW_TAG_Unknown_%x", getTag());
326 OS << format(" [%u] %c\n", abbrCode,
327 AbbrevDecl->hasChildren() ? '*' : ' ');
329 // Dump all data in the DIE for the attributes.
330 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
331 if (AttrSpec.Form == DW_FORM_implicit_const) {
332 // We are dumping .debug_info section ,
333 // implicit_const attribute values are not really stored here,
334 // but in .debug_abbrev section. So we just skip such attrs.
337 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
341 DWARFDie child = getFirstChild();
342 if (RecurseDepth > 0 && child) {
344 child.dump(OS, RecurseDepth-1, Indent+2);
345 child = child.getSibling();
349 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
353 OS.indent(Indent) << "NULL\n";
358 DWARFDie DWARFDie::getParent() const {
360 return U->getParent(Die);
364 DWARFDie DWARFDie::getSibling() const {
366 return U->getSibling(Die);
370 iterator_range<DWARFDie::attribute_iterator>
371 DWARFDie::attributes() const {
372 return make_range(attribute_iterator(*this, false),
373 attribute_iterator(*this, true));
376 DWARFDie::attribute_iterator::attribute_iterator(DWARFDie D, bool End) :
377 Die(D), AttrValue(0), Index(0) {
378 auto AbbrDecl = Die.getAbbreviationDeclarationPtr();
379 assert(AbbrDecl && "Must have abbreviation declaration");
381 // This is the end iterator so we set the index to the attribute count.
382 Index = AbbrDecl->getNumAttributes();
384 // This is the begin iterator so we extract the value for this->Index.
385 AttrValue.Offset = D.getOffset() + AbbrDecl->getCodeByteSize();
386 updateForIndex(*AbbrDecl, 0);
390 void DWARFDie::attribute_iterator::updateForIndex(
391 const DWARFAbbreviationDeclaration &AbbrDecl, uint32_t I) {
393 // AbbrDecl must be valid befor calling this function.
394 auto NumAttrs = AbbrDecl.getNumAttributes();
395 if (Index < NumAttrs) {
396 AttrValue.Attr = AbbrDecl.getAttrByIndex(Index);
397 // Add the previous byte size of any previous attribute value.
398 AttrValue.Offset += AttrValue.ByteSize;
399 AttrValue.Value.setForm(AbbrDecl.getFormByIndex(Index));
400 uint32_t ParseOffset = AttrValue.Offset;
401 auto U = Die.getDwarfUnit();
402 assert(U && "Die must have valid DWARF unit");
403 bool b = AttrValue.Value.extractValue(U->getDebugInfoExtractor(),
406 assert(b && "extractValue cannot fail on fully parsed DWARF");
407 AttrValue.ByteSize = ParseOffset - AttrValue.Offset;
409 assert(Index == NumAttrs && "Indexes should be [0, NumAttrs) only");
414 DWARFDie::attribute_iterator &DWARFDie::attribute_iterator::operator++() {
415 if (auto AbbrDecl = Die.getAbbreviationDeclarationPtr())
416 updateForIndex(*AbbrDecl, Index + 1);