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 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
94 uint16_t attr, uint16_t form,
95 unsigned indent) const {
98 const char *attrString = AttributeString(attr);
102 OS << format("DW_AT_Unknown_%x", attr);
103 const char *formString = FormEncodingString(form);
105 OS << " [" << formString << ']';
107 OS << format(" [DW_FORM_Unknown_%x]", form);
109 DWARFFormValue formValue(form);
111 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
116 const char *Name = nullptr;
118 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
119 if (const auto *LT = u->getContext().getLineTableForUnit(u))
120 if (LT->getFileNameByIndex(
121 formValue.getAsUnsignedConstant().getValue(),
122 u->getCompilationDir(),
123 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
124 File = '"' + File + '"';
127 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
128 Name = AttributeValueString(attr, *Val);
132 } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
133 OS << *formValue.getAsUnsignedConstant();
135 formValue.dump(OS, u);
138 // We have dumped the attribute raw value. For some attributes
139 // having both the raw value and the pretty-printed value is
140 // interesting. These attributes are handled below.
141 if ((attr == DW_AT_specification || attr == DW_AT_abstract_origin) &&
142 // The signature references aren't handled.
143 formValue.getForm() != DW_FORM_ref_sig8) {
144 uint32_t Ref = formValue.getAsReference(u).getValue();
145 DWARFDebugInfoEntryMinimal DIE;
146 if (const DWARFUnit *RefU = findUnitAndExtractFast(DIE, u, &Ref))
147 if (const char *Ref = DIE.getName(RefU, DINameKind::LinkageName))
148 OS << " \"" << Ref << '\"';
149 } else if (attr == DW_AT_APPLE_property_attribute) {
150 if (Optional<uint64_t> OptVal = formValue.getAsUnsignedConstant())
151 dumpApplePropertyAttribute(OS, *OptVal);
157 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
158 uint32_t *OffsetPtr) {
160 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
161 uint32_t UEndOffset = U->getNextUnitOffset();
162 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
164 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
166 // NULL debug tag entry.
167 AbbrevDecl = nullptr;
170 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
171 if (nullptr == AbbrevDecl) {
172 // Restore the original offset.
176 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
177 U->getAddressByteSize(), U->getVersion());
178 assert(FixedFormSizes.size() > 0);
180 // Skip all data in the .debug_info for the attributes
181 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
182 uint16_t Form = AttrSpec.Form;
184 uint8_t FixedFormSize =
185 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
187 *OffsetPtr += FixedFormSize;
188 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
189 // Restore the original offset.
197 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
198 return getTag() == DW_TAG_subprogram;
201 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
202 uint32_t Tag = getTag();
203 return Tag == DW_TAG_subprogram ||
204 Tag == DW_TAG_inlined_subroutine;
207 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
208 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
212 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
216 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
217 uint32_t DebugInfoOffset = getOffset();
219 // Skip the abbreviation code so we are at the data for the attributes
220 DebugInfoData.getULEB128(&DebugInfoOffset);
222 // Skip preceding attribute values.
223 for (uint32_t i = 0; i < AttrIdx; ++i) {
224 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
225 DebugInfoData, &DebugInfoOffset, U);
228 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
229 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
232 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
233 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
234 DWARFFormValue FormValue;
235 if (!getAttributeValue(U, Attr, FormValue))
237 Optional<const char *> Result = FormValue.getAsCString(U);
238 return Result.hasValue() ? Result.getValue() : FailValue;
241 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
242 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
243 DWARFFormValue FormValue;
244 if (!getAttributeValue(U, Attr, FormValue))
246 Optional<uint64_t> Result = FormValue.getAsAddress(U);
247 return Result.hasValue() ? Result.getValue() : FailValue;
250 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
251 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
252 DWARFFormValue FormValue;
253 if (!getAttributeValue(U, Attr, FormValue))
255 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
256 return Result.hasValue() ? Result.getValue() : FailValue;
259 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
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.getAsReference(U);
265 return Result.hasValue() ? Result.getValue() : FailValue;
268 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
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.getAsSectionOffset();
274 return Result.hasValue() ? Result.getValue() : FailValue;
278 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
279 uint64_t FailValue) const {
281 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
284 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
287 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
289 uint64_t &HighPC) const {
290 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
293 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
294 if (HighPC == -1ULL) {
295 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
296 // it represents function size.
297 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
301 return (HighPC != -1ULL);
304 DWARFAddressRangesVector
305 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
307 return DWARFAddressRangesVector();
308 // Single range specified by low/high PC.
309 uint64_t LowPC, HighPC;
310 if (getLowAndHighPC(U, LowPC, HighPC)) {
311 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
313 // Multiple ranges from .debug_ranges section.
314 uint32_t RangesOffset =
315 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
316 if (RangesOffset != -1U) {
317 DWARFDebugRangeList RangeList;
318 if (U->extractRangeList(RangesOffset, RangeList))
319 return RangeList.getAbsoluteRanges(U->getBaseAddress());
321 return DWARFAddressRangesVector();
324 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
325 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
328 if (isSubprogramDIE()) {
329 const auto &DIERanges = getAddressRanges(U);
330 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
333 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
335 Child->collectChildrenAddressRanges(U, Ranges);
336 Child = Child->getSibling();
340 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
341 const DWARFUnit *U, const uint64_t Address) const {
342 for (const auto& R : getAddressRanges(U)) {
343 if (R.first <= Address && Address < R.second)
350 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
351 DINameKind Kind) const {
352 if (!isSubroutineDIE())
354 return getName(U, Kind);
358 DWARFDebugInfoEntryMinimal::getName(const DWARFUnit *U,
359 DINameKind Kind) const {
360 if (Kind == DINameKind::None)
362 // Try to get mangled name only if it was asked for.
363 if (Kind == DINameKind::LinkageName) {
364 if (const char *name =
365 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
367 if (const char *name =
368 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
371 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
373 // Try to get name from specification DIE.
375 getAttributeValueAsReference(U, DW_AT_specification, -1U);
376 if (spec_ref != -1U) {
377 DWARFDebugInfoEntryMinimal spec_die;
378 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
379 if (const char *name = spec_die.getName(RefU, Kind))
383 // Try to get name from abstract origin DIE.
384 uint32_t abs_origin_ref =
385 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
386 if (abs_origin_ref != -1U) {
387 DWARFDebugInfoEntryMinimal abs_origin_die;
388 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
390 if (const char *name = abs_origin_die.getName(RefU, Kind))
397 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
400 uint32_t &CallColumn) const {
401 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
402 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
403 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
406 DWARFDebugInfoEntryInlinedChain
407 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
408 const DWARFUnit *U, const uint64_t Address) const {
409 DWARFDebugInfoEntryInlinedChain InlinedChain;
413 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
414 // Append current DIE to inlined chain only if it has correct tag
415 // (e.g. it is not a lexical block).
416 if (DIE->isSubroutineDIE()) {
417 InlinedChain.DIEs.push_back(*DIE);
419 // Try to get child which also contains provided address.
420 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
422 if (Child->addressRangeContainsAddress(U, Address)) {
423 // Assume there is only one such child.
426 Child = Child->getSibling();
430 // Reverse the obtained chain to make the root of inlined chain last.
431 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());