1 //===-- DWARFDebugInfoEntry.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 "DWARFDebugInfoEntry.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFContext.h"
13 #include "DWARFDebugAbbrev.h"
14 #include "llvm/DebugInfo/DWARFFormValue.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Dwarf.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/raw_ostream.h"
21 using namespace dwarf;
23 // Small helper to extract a DIE pointed by a reference
24 // attribute. It looks up the Unit containing the DIE and calls
25 // DIE.extractFast with the right unit. Returns new unit on success,
27 static const DWARFUnit *findUnitAndExtractFast(DWARFDebugInfoEntryMinimal &DIE,
28 const DWARFUnit *Unit,
30 Unit = Unit->getUnitSection().getUnitForOffset(*Offset);
31 return (Unit && DIE.extractFast(Unit, Offset)) ? Unit : nullptr;
34 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, DWARFUnit *u,
35 unsigned recurseDepth,
36 unsigned indent) const {
37 DataExtractor debug_info_data = u->getDebugInfoExtractor();
38 uint32_t offset = Offset;
40 if (debug_info_data.isValidOffset(offset)) {
41 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
43 OS << format("\n0x%8.8x: ", Offset);
46 const char *tagString = TagString(getTag());
48 OS.indent(indent) << tagString;
50 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
51 OS << format(" [%u] %c\n", abbrCode,
52 AbbrevDecl->hasChildren() ? '*' : ' ');
54 // Dump all data in the DIE for the attributes.
55 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
56 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
59 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
60 if (recurseDepth > 0 && child) {
62 child->dump(OS, u, recurseDepth-1, indent+2);
63 child = child->getSibling();
67 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
71 OS.indent(indent) << "NULL\n";
76 static void dumpApplePropertyAttribute(raw_ostream &OS, uint64_t Val) {
79 uint64_t Bit = 1ULL << countTrailingZeros(Val);
80 if (const char *PropName = ApplePropertyString(Bit))
83 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
91 static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
92 unsigned AddressSize, unsigned Indent) {
96 for (const auto &Range: Ranges) {
99 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
100 AddressSize*2, Range.first,
101 AddressSize*2, Range.second);
105 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
107 uint32_t *offset_ptr,
108 uint16_t attr, uint16_t form,
109 unsigned indent) const {
110 const char BaseIndent[] = " ";
113 const char *attrString = AttributeString(attr);
117 OS << format("DW_AT_Unknown_%x", attr);
118 const char *formString = FormEncodingString(form);
120 OS << " [" << formString << ']';
122 OS << format(" [DW_FORM_Unknown_%x]", form);
124 DWARFFormValue formValue(form);
126 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
131 const char *Name = nullptr;
133 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
134 if (const auto *LT = u->getContext().getLineTableForUnit(u))
135 if (LT->getFileNameByIndex(
136 formValue.getAsUnsignedConstant().getValue(),
137 u->getCompilationDir(),
138 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
139 File = '"' + File + '"';
142 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
143 Name = AttributeValueString(attr, *Val);
147 } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
148 OS << *formValue.getAsUnsignedConstant();
150 formValue.dump(OS, u);
153 // We have dumped the attribute raw value. For some attributes
154 // having both the raw value and the pretty-printed value is
155 // interesting. These attributes are handled below.
156 if ((attr == DW_AT_specification || attr == DW_AT_abstract_origin) &&
157 // The signature references aren't handled.
158 formValue.getForm() != DW_FORM_ref_sig8) {
159 uint32_t Ref = formValue.getAsReference(u).getValue();
160 DWARFDebugInfoEntryMinimal DIE;
161 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &Ref))
162 if (const char *Ref = DIE.getName(RefU, DINameKind::LinkageName))
163 OS << " \"" << Ref << '\"';
164 } else if (attr == DW_AT_APPLE_property_attribute) {
165 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
166 dumpApplePropertyAttribute(OS, *OptVal);
167 } else if (attr == DW_AT_ranges) {
168 dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
169 sizeof(BaseIndent)+indent+4);
175 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
176 uint32_t *OffsetPtr) {
178 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
179 uint32_t UEndOffset = U->getNextUnitOffset();
180 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
182 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
184 // NULL debug tag entry.
185 AbbrevDecl = nullptr;
188 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
189 if (nullptr == AbbrevDecl) {
190 // Restore the original offset.
194 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
195 U->getAddressByteSize(), U->getVersion());
196 assert(FixedFormSizes.size() > 0);
198 // Skip all data in the .debug_info for the attributes
199 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
200 uint16_t Form = AttrSpec.Form;
202 uint8_t FixedFormSize =
203 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
205 *OffsetPtr += FixedFormSize;
206 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
207 // Restore the original offset.
215 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
216 return getTag() == DW_TAG_subprogram;
219 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
220 uint32_t Tag = getTag();
221 return Tag == DW_TAG_subprogram ||
222 Tag == DW_TAG_inlined_subroutine;
225 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
226 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
230 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
234 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
235 uint32_t DebugInfoOffset = getOffset();
237 // Skip the abbreviation code so we are at the data for the attributes
238 DebugInfoData.getULEB128(&DebugInfoOffset);
240 // Skip preceding attribute values.
241 for (uint32_t i = 0; i < AttrIdx; ++i) {
242 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
243 DebugInfoData, &DebugInfoOffset, U);
246 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
247 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
250 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
251 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
252 DWARFFormValue FormValue;
253 if (!getAttributeValue(U, Attr, FormValue))
255 Optional<const char *> Result = FormValue.getAsCString(U);
256 return Result.hasValue() ? Result.getValue() : FailValue;
259 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
260 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
261 DWARFFormValue FormValue;
262 if (!getAttributeValue(U, Attr, FormValue))
264 Optional<uint64_t> Result = FormValue.getAsAddress(U);
265 return Result.hasValue() ? Result.getValue() : FailValue;
268 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
269 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
270 DWARFFormValue FormValue;
271 if (!getAttributeValue(U, Attr, FormValue))
273 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
274 return Result.hasValue() ? Result.getValue() : FailValue;
277 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
278 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
279 DWARFFormValue FormValue;
280 if (!getAttributeValue(U, Attr, FormValue))
282 Optional<uint64_t> Result = FormValue.getAsReference(U);
283 return Result.hasValue() ? Result.getValue() : FailValue;
286 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
287 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
288 DWARFFormValue FormValue;
289 if (!getAttributeValue(U, Attr, FormValue))
291 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
292 return Result.hasValue() ? Result.getValue() : FailValue;
296 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
297 uint64_t FailValue) const {
299 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
302 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
305 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
307 uint64_t &HighPC) const {
308 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
311 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
312 if (HighPC == -1ULL) {
313 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
314 // it represents function size.
315 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
319 return (HighPC != -1ULL);
322 DWARFAddressRangesVector
323 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
325 return DWARFAddressRangesVector();
326 // Single range specified by low/high PC.
327 uint64_t LowPC, HighPC;
328 if (getLowAndHighPC(U, LowPC, HighPC)) {
329 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
331 // Multiple ranges from .debug_ranges section.
332 uint32_t RangesOffset =
333 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
334 if (RangesOffset != -1U) {
335 DWARFDebugRangeList RangeList;
336 if (U->extractRangeList(RangesOffset, RangeList))
337 return RangeList.getAbsoluteRanges(U->getBaseAddress());
339 return DWARFAddressRangesVector();
342 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
343 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
346 if (isSubprogramDIE()) {
347 const auto &DIERanges = getAddressRanges(U);
348 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
351 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
353 Child->collectChildrenAddressRanges(U, Ranges);
354 Child = Child->getSibling();
358 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
359 const DWARFUnit *U, const uint64_t Address) const {
360 for (const auto& R : getAddressRanges(U)) {
361 if (R.first <= Address && Address < R.second)
368 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
369 DINameKind Kind) const {
370 if (!isSubroutineDIE())
372 return getName(U, Kind);
376 DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
377 DINameKind Kind) const {
378 if (Kind == DINameKind::None)
380 // Try to get mangled name only if it was asked for.
381 if (Kind == DINameKind::LinkageName) {
382 if (const char *name =
383 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
385 if (const char *name =
386 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
389 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
391 // Try to get name from specification DIE.
393 getAttributeValueAsReference(U, DW_AT_specification, -1U);
394 if (spec_ref != -1U) {
395 DWARFDebugInfoEntryMinimal spec_die;
396 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
397 if (const char *name = spec_die.getName(RefU, Kind))
401 // Try to get name from abstract origin DIE.
402 uint32_t abs_origin_ref =
403 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
404 if (abs_origin_ref != -1U) {
405 DWARFDebugInfoEntryMinimal abs_origin_die;
406 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
408 if (const char *name = abs_origin_die.getName(RefU, Kind))
415 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
418 uint32_t &CallColumn) const {
419 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
420 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
421 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
424 DWARFDebugInfoEntryInlinedChain
425 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
426 const DWARFUnit *U, const uint64_t Address) const {
427 DWARFDebugInfoEntryInlinedChain InlinedChain;
431 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
432 // Append current DIE to inlined chain only if it has correct tag
433 // (e.g. it is not a lexical block).
434 if (DIE->isSubroutineDIE()) {
435 InlinedChain.DIEs.push_back(*DIE);
437 // Try to get child which also contains provided address.
438 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
440 if (Child->addressRangeContainsAddress(U, Address)) {
441 // Assume there is only one such child.
444 Child = Child->getSibling();
448 // Reverse the obtained chain to make the root of inlined chain last.
449 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());