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/Debug.h"
16 #include "llvm/Support/Dwarf.h"
17 #include "llvm/Support/Format.h"
18 #include "llvm/Support/raw_ostream.h"
20 using namespace dwarf;
21 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
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 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
79 uint16_t attr, uint16_t form,
80 unsigned indent) const {
83 const char *attrString = AttributeString(attr);
87 OS << format("DW_AT_Unknown_%x", attr);
88 const char *formString = FormEncodingString(form);
90 OS << " [" << formString << ']';
92 OS << format(" [DW_FORM_Unknown_%x]", form);
94 DWARFFormValue formValue(form);
96 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
101 const char *Name = nullptr;
103 if (attr == DW_AT_decl_file || attr == DW_AT_call_file) {
104 if (const auto *LT = u->getContext().getLineTableForUnit(u))
105 if (LT->getFileNameByIndex(
106 formValue.getAsUnsignedConstant().getValue(),
107 u->getCompilationDir(),
108 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, File)) {
109 File = '"' + File + '"';
112 } else if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
113 Name = AttributeValueString(attr, *Val);
117 } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
118 OS << *formValue.getAsUnsignedConstant();
120 formValue.dump(OS, u);
126 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
127 uint32_t *OffsetPtr) {
129 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
130 uint32_t UEndOffset = U->getNextUnitOffset();
131 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
133 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
135 // NULL debug tag entry.
136 AbbrevDecl = nullptr;
139 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
140 if (nullptr == AbbrevDecl) {
141 // Restore the original offset.
145 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
146 U->getAddressByteSize(), U->getVersion());
147 assert(FixedFormSizes.size() > 0);
149 // Skip all data in the .debug_info for the attributes
150 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
151 uint16_t Form = AttrSpec.Form;
153 uint8_t FixedFormSize =
154 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
156 *OffsetPtr += FixedFormSize;
157 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
158 // Restore the original offset.
166 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
167 return getTag() == DW_TAG_subprogram;
170 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
171 uint32_t Tag = getTag();
172 return Tag == DW_TAG_subprogram ||
173 Tag == DW_TAG_inlined_subroutine;
176 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
177 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
181 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
185 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
186 uint32_t DebugInfoOffset = getOffset();
188 // Skip the abbreviation code so we are at the data for the attributes
189 DebugInfoData.getULEB128(&DebugInfoOffset);
191 // Skip preceding attribute values.
192 for (uint32_t i = 0; i < AttrIdx; ++i) {
193 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
194 DebugInfoData, &DebugInfoOffset, U);
197 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
198 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
201 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
202 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
203 DWARFFormValue FormValue;
204 if (!getAttributeValue(U, Attr, FormValue))
206 Optional<const char *> Result = FormValue.getAsCString(U);
207 return Result.hasValue() ? Result.getValue() : FailValue;
210 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
211 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
212 DWARFFormValue FormValue;
213 if (!getAttributeValue(U, Attr, FormValue))
215 Optional<uint64_t> Result = FormValue.getAsAddress(U);
216 return Result.hasValue() ? Result.getValue() : FailValue;
219 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
220 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
221 DWARFFormValue FormValue;
222 if (!getAttributeValue(U, Attr, FormValue))
224 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
225 return Result.hasValue() ? Result.getValue() : FailValue;
228 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
229 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
230 DWARFFormValue FormValue;
231 if (!getAttributeValue(U, Attr, FormValue))
233 Optional<uint64_t> Result = FormValue.getAsReference(U);
234 return Result.hasValue() ? Result.getValue() : FailValue;
237 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
238 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
239 DWARFFormValue FormValue;
240 if (!getAttributeValue(U, Attr, FormValue))
242 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
243 return Result.hasValue() ? Result.getValue() : FailValue;
247 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
248 uint64_t FailValue) const {
250 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
253 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
256 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
258 uint64_t &HighPC) const {
259 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
262 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
263 if (HighPC == -1ULL) {
264 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
265 // it represents function size.
266 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
270 return (HighPC != -1ULL);
273 DWARFAddressRangesVector
274 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
276 return DWARFAddressRangesVector();
277 // Single range specified by low/high PC.
278 uint64_t LowPC, HighPC;
279 if (getLowAndHighPC(U, LowPC, HighPC)) {
280 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
282 // Multiple ranges from .debug_ranges section.
283 uint32_t RangesOffset =
284 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
285 if (RangesOffset != -1U) {
286 DWARFDebugRangeList RangeList;
287 if (U->extractRangeList(RangesOffset, RangeList))
288 return RangeList.getAbsoluteRanges(U->getBaseAddress());
290 return DWARFAddressRangesVector();
293 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
294 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
297 if (isSubprogramDIE()) {
298 const auto &DIERanges = getAddressRanges(U);
299 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
302 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
304 Child->collectChildrenAddressRanges(U, Ranges);
305 Child = Child->getSibling();
309 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
310 const DWARFUnit *U, const uint64_t Address) const {
311 for (const auto& R : getAddressRanges(U)) {
312 if (R.first <= Address && Address < R.second)
319 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
320 FunctionNameKind Kind) const {
321 if (!isSubroutineDIE() || Kind == FunctionNameKind::None)
323 // Try to get mangled name only if it was asked for.
324 if (Kind == FunctionNameKind::LinkageName) {
325 if (const char *name =
326 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
328 if (const char *name =
329 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
332 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
334 // Try to get name from specification DIE.
336 getAttributeValueAsReference(U, DW_AT_specification, -1U);
337 if (spec_ref != -1U) {
338 DWARFDebugInfoEntryMinimal spec_die;
339 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
340 if (const char *name = spec_die.getSubroutineName(RefU, Kind))
344 // Try to get name from abstract origin DIE.
345 uint32_t abs_origin_ref =
346 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
347 if (abs_origin_ref != -1U) {
348 DWARFDebugInfoEntryMinimal abs_origin_die;
349 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
351 if (const char *name = abs_origin_die.getSubroutineName(RefU, Kind))
358 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
361 uint32_t &CallColumn) const {
362 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
363 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
364 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
367 DWARFDebugInfoEntryInlinedChain
368 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
369 const DWARFUnit *U, const uint64_t Address) const {
370 DWARFDebugInfoEntryInlinedChain InlinedChain;
374 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
375 // Append current DIE to inlined chain only if it has correct tag
376 // (e.g. it is not a lexical block).
377 if (DIE->isSubroutineDIE()) {
378 InlinedChain.DIEs.push_back(*DIE);
380 // Try to get child which also contains provided address.
381 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
383 if (Child->addressRangeContainsAddress(U, Address)) {
384 // Assume there is only one such child.
387 Child = Child->getSibling();
391 // Reverse the obtained chain to make the root of inlined chain last.
392 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());