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, const 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;
102 if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
103 Name = AttributeValueString(attr, *Val);
107 } else if (attr == DW_AT_decl_line || attr == DW_AT_call_line) {
108 OS << *formValue.getAsUnsignedConstant();
110 formValue.dump(OS, u);
116 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
117 uint32_t *OffsetPtr) {
119 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
120 uint32_t UEndOffset = U->getNextUnitOffset();
121 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
123 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
125 // NULL debug tag entry.
126 AbbrevDecl = nullptr;
129 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
130 if (nullptr == AbbrevDecl) {
131 // Restore the original offset.
135 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
136 U->getAddressByteSize(), U->getVersion());
137 assert(FixedFormSizes.size() > 0);
139 // Skip all data in the .debug_info for the attributes
140 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
141 uint16_t Form = AttrSpec.Form;
143 uint8_t FixedFormSize =
144 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
146 *OffsetPtr += FixedFormSize;
147 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
148 // Restore the original offset.
156 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
157 return getTag() == DW_TAG_subprogram;
160 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
161 uint32_t Tag = getTag();
162 return Tag == DW_TAG_subprogram ||
163 Tag == DW_TAG_inlined_subroutine;
166 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
167 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
171 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
175 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
176 uint32_t DebugInfoOffset = getOffset();
178 // Skip the abbreviation code so we are at the data for the attributes
179 DebugInfoData.getULEB128(&DebugInfoOffset);
181 // Skip preceding attribute values.
182 for (uint32_t i = 0; i < AttrIdx; ++i) {
183 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
184 DebugInfoData, &DebugInfoOffset, U);
187 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
188 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
191 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
192 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
193 DWARFFormValue FormValue;
194 if (!getAttributeValue(U, Attr, FormValue))
196 Optional<const char *> Result = FormValue.getAsCString(U);
197 return Result.hasValue() ? Result.getValue() : FailValue;
200 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
201 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
202 DWARFFormValue FormValue;
203 if (!getAttributeValue(U, Attr, FormValue))
205 Optional<uint64_t> Result = FormValue.getAsAddress(U);
206 return Result.hasValue() ? Result.getValue() : FailValue;
209 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
210 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
211 DWARFFormValue FormValue;
212 if (!getAttributeValue(U, Attr, FormValue))
214 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
215 return Result.hasValue() ? Result.getValue() : FailValue;
218 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
219 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
220 DWARFFormValue FormValue;
221 if (!getAttributeValue(U, Attr, FormValue))
223 Optional<uint64_t> Result = FormValue.getAsReference(U);
224 return Result.hasValue() ? Result.getValue() : FailValue;
227 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
228 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
229 DWARFFormValue FormValue;
230 if (!getAttributeValue(U, Attr, FormValue))
232 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
233 return Result.hasValue() ? Result.getValue() : FailValue;
237 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
238 uint64_t FailValue) const {
240 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
243 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
246 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
248 uint64_t &HighPC) const {
249 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
252 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
253 if (HighPC == -1ULL) {
254 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
255 // it represents function size.
256 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
260 return (HighPC != -1ULL);
263 DWARFAddressRangesVector
264 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
266 return DWARFAddressRangesVector();
267 // Single range specified by low/high PC.
268 uint64_t LowPC, HighPC;
269 if (getLowAndHighPC(U, LowPC, HighPC)) {
270 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
272 // Multiple ranges from .debug_ranges section.
273 uint32_t RangesOffset =
274 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
275 if (RangesOffset != -1U) {
276 DWARFDebugRangeList RangeList;
277 if (U->extractRangeList(RangesOffset, RangeList))
278 return RangeList.getAbsoluteRanges(U->getBaseAddress());
280 return DWARFAddressRangesVector();
283 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
284 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
287 if (isSubprogramDIE()) {
288 const auto &DIERanges = getAddressRanges(U);
289 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
292 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
294 Child->collectChildrenAddressRanges(U, Ranges);
295 Child = Child->getSibling();
299 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
300 const DWARFUnit *U, const uint64_t Address) const {
301 for (const auto& R : getAddressRanges(U)) {
302 if (R.first <= Address && Address < R.second)
309 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
310 FunctionNameKind Kind) const {
311 if (!isSubroutineDIE() || Kind == FunctionNameKind::None)
313 // Try to get mangled name only if it was asked for.
314 if (Kind == FunctionNameKind::LinkageName) {
315 if (const char *name =
316 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
318 if (const char *name =
319 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
322 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
324 // Try to get name from specification DIE.
326 getAttributeValueAsReference(U, DW_AT_specification, -1U);
327 if (spec_ref != -1U) {
328 DWARFDebugInfoEntryMinimal spec_die;
329 if (const DWARFUnit *RefU = findUnitAndExtractFast(spec_die, U, &spec_ref)) {
330 if (const char *name = spec_die.getSubroutineName(RefU, Kind))
334 // Try to get name from abstract origin DIE.
335 uint32_t abs_origin_ref =
336 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
337 if (abs_origin_ref != -1U) {
338 DWARFDebugInfoEntryMinimal abs_origin_die;
339 if (const DWARFUnit *RefU = findUnitAndExtractFast(abs_origin_die, U,
341 if (const char *name = abs_origin_die.getSubroutineName(RefU, Kind))
348 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
351 uint32_t &CallColumn) const {
352 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
353 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
354 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
357 DWARFDebugInfoEntryInlinedChain
358 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
359 const DWARFUnit *U, const uint64_t Address) const {
360 DWARFDebugInfoEntryInlinedChain InlinedChain;
364 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
365 // Append current DIE to inlined chain only if it has correct tag
366 // (e.g. it is not a lexical block).
367 if (DIE->isSubroutineDIE()) {
368 InlinedChain.DIEs.push_back(*DIE);
370 // Try to get child which also contains provided address.
371 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
373 if (Child->addressRangeContainsAddress(U, Address)) {
374 // Assume there is only one such child.
377 Child = Child->getSibling();
381 // Reverse the obtained chain to make the root of inlined chain last.
382 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());