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 Shift = countTrailingZeros(Val);
80 assert(Shift < 64 && "undefined behavior");
81 uint64_t Bit = 1ULL << Shift;
82 if (const char *PropName = ApplePropertyString(Bit))
85 OS << format("DW_APPLE_PROPERTY_0x%" PRIx64, Bit);
93 static void dumpRanges(raw_ostream &OS, const DWARFAddressRangesVector& Ranges,
94 unsigned AddressSize, unsigned Indent) {
98 for (const auto &Range: Ranges) {
101 OS << format("[0x%0*" PRIx64 " - 0x%0*" PRIx64 ")",
102 AddressSize*2, Range.first,
103 AddressSize*2, Range.second);
107 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
109 uint32_t *offset_ptr,
110 uint16_t attr, uint16_t form,
111 unsigned indent) const {
112 const char BaseIndent[] = " ";
115 const char *attrString = AttributeString(attr);
119 OS << format("DW_AT_Unknown_%x", attr);
120 const char *formString = FormEncodingString(form);
122 OS << " [" << formString << ']';
124 OS << format(" [DW_FORM_Unknown_%x]", form);
126 DWARFFormValue formValue(form);
128 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
133 const char *Name = nullptr;
135 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
136 if (const auto *LT = u->getContext().getLineTableForUnit(u))
137 if (LT->getFileNameByIndex(
138 formValue.getAsUnsignedConstant().getValue(),
139 u->getCompilationDir(),
140 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
141 File = '"' + File + '"';
144 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
145 Name = AttributeValueString(attr, *Val);
149 } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
150 OS << *formValue.getAsUnsignedConstant();
152 formValue.dump(OS, u);
155 // We have dumped the attribute raw value. For some attributes
156 // having both the raw value and the pretty-printed value is
157 // interesting. These attributes are handled below.
158 if ((attr == DW_AT_specification || attr == DW_AT_abstract_origin) &&
159 // The signature references aren't handled.
160 formValue.getForm() != DW_FORM_ref_sig8) {
161 uint32_t Ref = formValue.getAsReference(u).getValue();
162 DWARFDebugInfoEntryMinimal DIE;
163 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &Ref))
164 if (const char *Ref = DIE.getName(RefU, DINameKind::LinkageName))
165 OS << " \"" << Ref << '\"';
166 } else if (attr == DW_AT_APPLE_property_attribute) {
167 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
168 dumpApplePropertyAttribute(OS, *OptVal);
169 } else if (attr == DW_AT_ranges) {
170 dumpRanges(OS, getAddressRanges(u), u->getAddressByteSize(),
171 sizeof(BaseIndent)+indent+4);
177 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
178 uint32_t *OffsetPtr) {
180 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
181 uint32_t UEndOffset = U->getNextUnitOffset();
182 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
184 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
186 // NULL debug tag entry.
187 AbbrevDecl = nullptr;
190 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
191 if (nullptr == AbbrevDecl) {
192 // Restore the original offset.
196 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
197 U->getAddressByteSize(), U->getVersion());
198 assert(FixedFormSizes.size() > 0);
200 // Skip all data in the .debug_info for the attributes
201 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
202 uint16_t Form = AttrSpec.Form;
204 uint8_t FixedFormSize =
205 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
207 *OffsetPtr += FixedFormSize;
208 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
209 // Restore the original offset.
217 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
218 return getTag() == DW_TAG_subprogram;
221 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
222 uint32_t Tag = getTag();
223 return Tag == DW_TAG_subprogram ||
224 Tag == DW_TAG_inlined_subroutine;
227 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
228 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
232 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
236 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
237 uint32_t DebugInfoOffset = getOffset();
239 // Skip the abbreviation code so we are at the data for the attributes
240 DebugInfoData.getULEB128(&DebugInfoOffset);
242 // Skip preceding attribute values.
243 for (uint32_t i = 0; i < AttrIdx; ++i) {
244 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
245 DebugInfoData, &DebugInfoOffset, U);
248 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
249 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
252 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
253 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
254 DWARFFormValue FormValue;
255 if (!getAttributeValue(U, Attr, FormValue))
257 Optional<const char *> Result = FormValue.getAsCString(U);
258 return Result.hasValue() ? Result.getValue() : FailValue;
261 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
262 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
263 DWARFFormValue FormValue;
264 if (!getAttributeValue(U, Attr, FormValue))
266 Optional<uint64_t> Result = FormValue.getAsAddress(U);
267 return Result.hasValue() ? Result.getValue() : FailValue;
270 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
271 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
272 DWARFFormValue FormValue;
273 if (!getAttributeValue(U, Attr, FormValue))
275 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
276 return Result.hasValue() ? Result.getValue() : FailValue;
279 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
280 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
281 DWARFFormValue FormValue;
282 if (!getAttributeValue(U, Attr, FormValue))
284 Optional<uint64_t> Result = FormValue.getAsReference(U);
285 return Result.hasValue() ? Result.getValue() : FailValue;
288 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
289 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
290 DWARFFormValue FormValue;
291 if (!getAttributeValue(U, Attr, FormValue))
293 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
294 return Result.hasValue() ? Result.getValue() : FailValue;
298 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
299 uint64_t FailValue) const {
301 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
304 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
307 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
309 uint64_t &HighPC) const {
310 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
313 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
314 if (HighPC == -1ULL) {
315 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
316 // it represents function size.
317 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
321 return (HighPC != -1ULL);
324 DWARFAddressRangesVector
325 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
327 return DWARFAddressRangesVector();
328 // Single range specified by low/high PC.
329 uint64_t LowPC, HighPC;
330 if (getLowAndHighPC(U, LowPC, HighPC)) {
331 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
333 // Multiple ranges from .debug_ranges section.
334 uint32_t RangesOffset =
335 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
336 if (RangesOffset != -1U) {
337 DWARFDebugRangeList RangeList;
338 if (U->extractRangeList(RangesOffset, RangeList))
339 return RangeList.getAbsoluteRanges(U->getBaseAddress());
341 return DWARFAddressRangesVector();
344 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
345 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
348 if (isSubprogramDIE()) {
349 const auto &DIERanges = getAddressRanges(U);
350 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
353 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
355 Child->collectChildrenAddressRanges(U, Ranges);
356 Child = Child->getSibling();
360 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
361 const DWARFUnit *U, const uint64_t Address) const {
362 for (const auto& R : getAddressRanges(U)) {
363 if (R.first <= Address && Address < R.second)
370 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
371 DINameKind Kind) const {
372 if (!isSubroutineDIE())
374 return getName(U, Kind);
378 DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
379 DINameKind Kind) const {
380 if (Kind == DINameKind::None)
382 // Try to get mangled name only if it was asked for.
383 if (Kind == DINameKind::LinkageName) {
384 if (const char *name =
385 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
387 if (const char *name =
388 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
391 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
393 // Try to get name from specification DIE.
395 getAttributeValueAsReference(U, DW_AT_specification, -1U);
396 if (spec_ref != -1U) {
397 DWARFDebugInfoEntryMinimal spec_die;
398 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
399 if (const char *name = spec_die.getName(RefU, Kind))
403 // Try to get name from abstract origin DIE.
404 uint32_t abs_origin_ref =
405 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
406 if (abs_origin_ref != -1U) {
407 DWARFDebugInfoEntryMinimal abs_origin_die;
408 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
410 if (const char *name = abs_origin_die.getName(RefU, Kind))
417 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
420 uint32_t &CallColumn) const {
421 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
422 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
423 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
426 DWARFDebugInfoEntryInlinedChain
427 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
428 const DWARFUnit *U, const uint64_t Address) const {
429 DWARFDebugInfoEntryInlinedChain InlinedChain;
433 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
434 // Append current DIE to inlined chain only if it has correct tag
435 // (e.g. it is not a lexical block).
436 if (DIE->isSubroutineDIE()) {
437 InlinedChain.DIEs.push_back(*DIE);
439 // Try to get child which also contains provided address.
440 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
442 if (Child->addressRangeContainsAddress(U, Address)) {
443 // Assume there is only one such child.
446 Child = Child->getSibling();
450 // Reverse the obtained chain to make the root of inlined chain last.
451 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());