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/DebugInfo/DWARF/DWARFCompileUnit.h"
13 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
14 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
15 #include "llvm/DebugInfo/DWARF/DWARFDebugInfoEntry.h"
16 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
17 #include "llvm/Support/DataTypes.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Dwarf.h"
20 #include "llvm/Support/Format.h"
21 #include "llvm/Support/raw_ostream.h"
24 using namespace dwarf;
25 using namespace syntax;
28 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
31 uint64_t Shift = countTrailingZeros(Val);
32 assert(Shift < 64 && "undefined behavior");
33 uint64_t Bit = 1ULL << Shift;
34 auto PropName = ApplePropertyString(Bit);
35 if (!PropName.empty())
38 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
46 static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
47 unsigned AddressSize, unsigned Indent) {
51 for (const auto &Range: Ranges) {
54 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
55 AddressSize*2, Range.first,
56 AddressSize*2, Range.second);
60 static void dumpAttribute(raw_ostream &OS, const DWARFDie &Die,
61 uint32_t *OffsetPtr, dwarf::Attribute Attr,
62 dwarf::Form Form, unsigned Indent) {
65 const char BaseIndent[] = " ";
68 auto attrString = AttributeString(Attr);
69 if (!attrString.empty())
70 WithColor(OS, syntax::Attribute) << attrString;
72 WithColor(OS, syntax::Attribute).get() << format("DW_AT_Unknown_%x", Attr);
74 auto formString = FormEncodingString(Form);
75 if (!formString.empty())
76 OS << " [" << formString << ']';
78 OS << format(" [DW_FORM_Unknown_%x]", Form);
80 DWARFUnit *U = Die.getDwarfUnit();
81 DWARFFormValue formValue(Form);
83 if (!formValue.extractValue(U->getDebugInfoExtractor(), OffsetPtr, U))
90 auto Color = syntax::Enumerator;
91 if (Attr == DW_AT_decl_file || Attr == DW_AT_call_file) {
92 Color = syntax::String;
93 if (const auto *LT = U->getContext().getLineTableForUnit(U))
94 if (LT->getFileNameByIndex(formValue.getAsUnsignedConstant().getValue(), U->getCompilationDir(), DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
95 File = '"' + File + '"';
98 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
99 Name = AttributeValueString(Attr, *Val);
102 WithColor(OS, Color) << Name;
103 else if (Attr == DW_AT_decl_line || Attr == DW_AT_call_line)
104 OS << *formValue.getAsUnsignedConstant();
108 // We have dumped the attribute raw value. For some attributes
109 // having both the raw value and the pretty-printed value is
110 // interesting. These attributes are handled below.
111 if (Attr == DW_AT_specification || Attr == DW_AT_abstract_origin) {
112 if (const char *Name = Die.getAttributeValueAsReferencedDie(Attr).getName(DINameKind::LinkageName))
113 OS << " \"" << Name << '\"';
114 } else if (Attr == DW_AT_APPLE_property_attribute) {
115 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
116 dumpApplePropertyAttribute(OS, *OptVal);
117 } else if (Attr == DW_AT_ranges) {
118 dumpRanges(OS, Die.getAddressRanges(), U->getAddressByteSize(),
119 sizeof(BaseIndent)+Indent+4);
125 } // end anonymous namespace
127 bool DWARFDie::isSubprogramDIE() const {
128 return getTag() == DW_TAG_subprogram;
131 bool DWARFDie::isSubroutineDIE() const {
133 return Tag == DW_TAG_subprogram || Tag == DW_TAG_inlined_subroutine;
136 Optional<DWARFFormValue>
137 DWARFDie::getAttributeValue(dwarf::Attribute Attr) const {
140 auto AbbrevDecl = getAbbreviationDeclarationPtr();
142 return AbbrevDecl->getAttributeValue(getOffset(), Attr, *U);
146 const char *DWARFDie::getAttributeValueAsString(dwarf::Attribute Attr,
147 const char *FailValue) const {
148 auto FormValue = getAttributeValue(Attr);
151 Optional<const char *> Result = FormValue->getAsCString();
152 return Result.hasValue() ? Result.getValue() : FailValue;
155 uint64_t DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr,
156 uint64_t FailValue) const {
157 if (auto Value = getAttributeValueAsAddress(Attr))
163 DWARFDie::getAttributeValueAsAddress(dwarf::Attribute Attr) const {
164 if (auto FormValue = getAttributeValue(Attr))
165 return FormValue->getAsAddress();
169 int64_t DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr,
170 int64_t FailValue) const {
171 if (auto Value = getAttributeValueAsSignedConstant(Attr))
177 DWARFDie::getAttributeValueAsSignedConstant(dwarf::Attribute Attr) const {
178 if (auto FormValue = getAttributeValue(Attr))
179 return FormValue->getAsSignedConstant();
184 DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr,
185 uint64_t FailValue) const {
186 if (auto Value = getAttributeValueAsUnsignedConstant(Attr))
193 DWARFDie::getAttributeValueAsUnsignedConstant(dwarf::Attribute Attr) const {
194 if (auto FormValue = getAttributeValue(Attr))
195 return FormValue->getAsUnsignedConstant();
199 uint64_t DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr,
200 uint64_t FailValue) const {
201 if (auto Value = getAttributeValueAsReference(Attr))
208 DWARFDie::getAttributeValueAsReference(dwarf::Attribute Attr) const {
209 if (auto FormValue = getAttributeValue(Attr))
210 return FormValue->getAsReference();
214 uint64_t DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr,
215 uint64_t FailValue) const {
216 if (auto Value = getAttributeValueAsSectionOffset(Attr))
222 DWARFDie::getAttributeValueAsSectionOffset(dwarf::Attribute Attr) const {
223 if (auto FormValue = getAttributeValue(Attr))
224 return FormValue->getAsSectionOffset();
230 DWARFDie::getAttributeValueAsReferencedDie(dwarf::Attribute Attr) const {
231 auto SpecRef = getAttributeValueAsReference(Attr);
233 auto SpecUnit = U->getUnitSection().getUnitForOffset(*SpecRef);
235 return SpecUnit->getDIEForOffset(*SpecRef);
241 DWARFDie::getRangesBaseAttribute() const {
242 auto Result = getAttributeValueAsSectionOffset(DW_AT_rnglists_base);
245 return getAttributeValueAsSectionOffset(DW_AT_GNU_ranges_base);
248 Optional<uint64_t> DWARFDie::getHighPC(uint64_t LowPC) const {
249 if (auto FormValue = getAttributeValue(DW_AT_high_pc)) {
250 if (auto Address = FormValue->getAsAddress()) {
251 // High PC is an address.
254 if (auto Offset = FormValue->getAsUnsignedConstant()) {
255 // High PC is an offset from LowPC.
256 return LowPC + *Offset;
262 bool DWARFDie::getLowAndHighPC(uint64_t &LowPC, uint64_t &HighPC) const {
263 auto LowPcAddr = getAttributeValueAsAddress(DW_AT_low_pc);
266 if (auto HighPcAddr = getHighPC(*LowPcAddr)) {
268 HighPC = *HighPcAddr;
274 DWARFAddressRangesVector
275 DWARFDie::getAddressRanges() const {
277 return DWARFAddressRangesVector();
278 // Single range specified by low/high PC.
279 uint64_t LowPC, HighPC;
280 if (getLowAndHighPC(LowPC, HighPC)) {
281 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
283 // Multiple ranges from .debug_ranges section.
284 auto RangesOffset = getAttributeValueAsSectionOffset(DW_AT_ranges);
286 DWARFDebugRangeList RangeList;
287 if (U->extractRangeList(*RangesOffset, RangeList))
288 return RangeList.getAbsoluteRanges(U->getBaseAddress());
290 return DWARFAddressRangesVector();
294 DWARFDie::collectChildrenAddressRanges(DWARFAddressRangesVector& Ranges) const {
297 if (isSubprogramDIE()) {
298 const auto &DIERanges = getAddressRanges();
299 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
302 for (auto Child: children())
303 Child.collectChildrenAddressRanges(Ranges);
306 bool DWARFDie::addressRangeContainsAddress(const uint64_t Address) const {
307 for (const auto& R : getAddressRanges()) {
308 if (R.first <= Address && Address < R.second)
315 DWARFDie::getSubroutineName(DINameKind Kind) const {
316 if (!isSubroutineDIE())
318 return getName(Kind);
322 DWARFDie::getName(DINameKind Kind) const {
323 if (!isValid() || Kind == DINameKind::None)
325 const char *name = nullptr;
326 // Try to get mangled name only if it was asked for.
327 if (Kind == DINameKind::LinkageName) {
328 if ((name = getAttributeValueAsString(DW_AT_MIPS_linkage_name, nullptr)))
330 if ((name = getAttributeValueAsString(DW_AT_linkage_name, nullptr)))
333 if ((name = getAttributeValueAsString(DW_AT_name, nullptr)))
335 // Try to get name from specification DIE.
336 DWARFDie SpecDie = getAttributeValueAsReferencedDie(DW_AT_specification);
337 if (SpecDie && (name = SpecDie.getName(Kind)))
339 // Try to get name from abstract origin DIE.
340 DWARFDie AbsDie = getAttributeValueAsReferencedDie(DW_AT_abstract_origin);
341 if (AbsDie && (name = AbsDie.getName(Kind)))
346 void DWARFDie::getCallerFrame(uint32_t &CallFile, uint32_t &CallLine,
347 uint32_t &CallColumn) const {
348 CallFile = getAttributeValueAsUnsignedConstant(DW_AT_call_file, 0);
349 CallLine = getAttributeValueAsUnsignedConstant(DW_AT_call_line, 0);
350 CallColumn = getAttributeValueAsUnsignedConstant(DW_AT_call_column, 0);
353 void DWARFDie::dump(raw_ostream &OS, unsigned RecurseDepth,
354 unsigned Indent) const {
357 DataExtractor debug_info_data = U->getDebugInfoExtractor();
358 const uint32_t Offset = getOffset();
359 uint32_t offset = Offset;
361 if (debug_info_data.isValidOffset(offset)) {
362 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
363 WithColor(OS, syntax::Address).get() << format("\n0x%8.8x: ", Offset);
366 auto AbbrevDecl = getAbbreviationDeclarationPtr();
368 auto tagString = TagString(getTag());
369 if (!tagString.empty())
370 WithColor(OS, syntax::Tag).get().indent(Indent) << tagString;
372 WithColor(OS, syntax::Tag).get().indent(Indent)
373 << format("DW_TAG_Unknown_%x", getTag());
375 OS << format(" [%u] %c\n", abbrCode,
376 AbbrevDecl->hasChildren() ? '*' : ' ');
378 // Dump all data in the DIE for the attributes.
379 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
380 dumpAttribute(OS, *this, &offset, AttrSpec.Attr, AttrSpec.Form,
384 DWARFDie child = getFirstChild();
385 if (RecurseDepth > 0 && child) {
387 child.dump(OS, RecurseDepth-1, Indent+2);
388 child = child.getSibling();
392 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
396 OS.indent(Indent) << "NULL\n";
402 void DWARFDie::getInlinedChainForAddress(
403 const uint64_t Address, SmallVectorImpl<DWARFDie> &InlinedChain) const {
408 // Append current DIE to inlined chain only if it has correct tag
409 // (e.g. it is not a lexical block).
410 if (DIE.isSubroutineDIE())
411 InlinedChain.push_back(DIE);
413 // Try to get child which also contains provided address.
414 DWARFDie Child = DIE.getFirstChild();
416 if (Child.addressRangeContainsAddress(Address)) {
417 // Assume there is only one such child.
420 Child = Child.getSibling();
424 // Reverse the obtained chain to make the root of inlined chain last.
425 std::reverse(InlinedChain.begin(), InlinedChain.end());
428 DWARFDie DWARFDie::getParent() const {
430 return U->getParent(Die);
434 DWARFDie DWARFDie::getSibling() const {
436 return U->getSibling(Die);