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 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
24 unsigned recurseDepth,
25 unsigned indent) const {
26 DataExtractor debug_info_data = u->getDebugInfoExtractor();
27 uint32_t offset = Offset;
29 if (debug_info_data.isValidOffset(offset)) {
30 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
32 OS << format("\n0x%8.8x: ", Offset);
35 const char *tagString = TagString(getTag());
37 OS.indent(indent) << tagString;
39 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
40 OS << format(" [%u] %c\n", abbrCode,
41 AbbrevDecl->hasChildren() ? '*' : ' ');
43 // Dump all data in the DIE for the attributes.
44 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
45 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
48 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
49 if (recurseDepth > 0 && child) {
51 child->dump(OS, u, recurseDepth-1, indent+2);
52 child = child->getSibling();
56 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
60 OS.indent(indent) << "NULL\n";
65 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
68 uint16_t attr, uint16_t form,
69 unsigned indent) const {
72 const char *attrString = AttributeString(attr);
76 OS << format("DW_AT_Unknown_%x", attr);
77 const char *formString = FormEncodingString(form);
79 OS << " [" << formString << ']';
81 OS << format(" [DW_FORM_Unknown_%x]", form);
83 DWARFFormValue formValue(form);
85 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
90 const char *Name = nullptr;
91 if (Optional<uint64_t> Val = formValue.getAsUnsignedConstant())
92 Name = AttributeValueString(attr, *Val);
97 formValue.dump(OS, u);
103 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
104 uint32_t *OffsetPtr) {
106 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
107 uint32_t UEndOffset = U->getNextUnitOffset();
108 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
110 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
112 // NULL debug tag entry.
113 AbbrevDecl = nullptr;
116 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
117 if (nullptr == AbbrevDecl) {
118 // Restore the original offset.
122 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
123 U->getAddressByteSize(), U->getVersion());
124 assert(FixedFormSizes.size() > 0);
126 // Skip all data in the .debug_info for the attributes
127 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
128 uint16_t Form = AttrSpec.Form;
130 uint8_t FixedFormSize =
131 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
133 *OffsetPtr += FixedFormSize;
134 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
135 // Restore the original offset.
143 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
144 return getTag() == DW_TAG_subprogram;
147 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
148 uint32_t Tag = getTag();
149 return Tag == DW_TAG_subprogram ||
150 Tag == DW_TAG_inlined_subroutine;
153 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
154 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
158 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
162 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
163 uint32_t DebugInfoOffset = getOffset();
165 // Skip the abbreviation code so we are at the data for the attributes
166 DebugInfoData.getULEB128(&DebugInfoOffset);
168 // Skip preceding attribute values.
169 for (uint32_t i = 0; i < AttrIdx; ++i) {
170 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
171 DebugInfoData, &DebugInfoOffset, U);
174 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
175 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
178 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
179 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
180 DWARFFormValue FormValue;
181 if (!getAttributeValue(U, Attr, FormValue))
183 Optional<const char *> Result = FormValue.getAsCString(U);
184 return Result.hasValue() ? Result.getValue() : FailValue;
187 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
188 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
189 DWARFFormValue FormValue;
190 if (!getAttributeValue(U, Attr, FormValue))
192 Optional<uint64_t> Result = FormValue.getAsAddress(U);
193 return Result.hasValue() ? Result.getValue() : FailValue;
196 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
197 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
198 DWARFFormValue FormValue;
199 if (!getAttributeValue(U, Attr, FormValue))
201 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
202 return Result.hasValue() ? Result.getValue() : FailValue;
205 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
206 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
207 DWARFFormValue FormValue;
208 if (!getAttributeValue(U, Attr, FormValue))
210 Optional<uint64_t> Result = FormValue.getAsReference(U);
211 return Result.hasValue() ? Result.getValue() : FailValue;
214 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
215 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
216 DWARFFormValue FormValue;
217 if (!getAttributeValue(U, Attr, FormValue))
219 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
220 return Result.hasValue() ? Result.getValue() : FailValue;
224 DWARFDebugInfoEntryMinimal::getRangesBaseAttribute(const DWARFUnit *U,
225 uint64_t FailValue) const {
227 getAttributeValueAsSectionOffset(U, DW_AT_ranges_base, -1ULL);
230 return getAttributeValueAsSectionOffset(U, DW_AT_GNU_ranges_base, FailValue);
233 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
235 uint64_t &HighPC) const {
236 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
239 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
240 if (HighPC == -1ULL) {
241 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
242 // it represents function size.
243 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
247 return (HighPC != -1ULL);
250 DWARFAddressRangesVector
251 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
253 return DWARFAddressRangesVector();
254 // Single range specified by low/high PC.
255 uint64_t LowPC, HighPC;
256 if (getLowAndHighPC(U, LowPC, HighPC)) {
257 return DWARFAddressRangesVector(1, std::make_pair(LowPC, HighPC));
259 // Multiple ranges from .debug_ranges section.
260 uint32_t RangesOffset =
261 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
262 if (RangesOffset != -1U) {
263 DWARFDebugRangeList RangeList;
264 if (U->extractRangeList(RangesOffset, RangeList))
265 return RangeList.getAbsoluteRanges(U->getBaseAddress());
267 return DWARFAddressRangesVector();
270 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
271 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
274 if (isSubprogramDIE()) {
275 const auto &DIERanges = getAddressRanges(U);
276 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
279 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
281 Child->collectChildrenAddressRanges(U, Ranges);
282 Child = Child->getSibling();
286 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
287 const DWARFUnit *U, const uint64_t Address) const {
288 for (const auto& R : getAddressRanges(U)) {
289 if (R.first <= Address && Address < R.second)
296 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U,
297 FunctionNameKind Kind) const {
298 if (!isSubroutineDIE() || Kind == FunctionNameKind::None)
300 // Try to get mangled name only if it was asked for.
301 if (Kind == FunctionNameKind::LinkageName) {
302 if (const char *name =
303 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
305 if (const char *name =
306 getAttributeValueAsString(U, DW_AT_linkage_name, nullptr))
309 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
311 // Try to get name from specification DIE.
313 getAttributeValueAsReference(U, DW_AT_specification, -1U);
314 if (spec_ref != -1U) {
315 DWARFDebugInfoEntryMinimal spec_die;
316 if (spec_die.extractFast(U, &spec_ref)) {
317 if (const char *name = spec_die.getSubroutineName(U, Kind))
321 // Try to get name from abstract origin DIE.
322 uint32_t abs_origin_ref =
323 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
324 if (abs_origin_ref != -1U) {
325 DWARFDebugInfoEntryMinimal abs_origin_die;
326 if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
327 if (const char *name = abs_origin_die.getSubroutineName(U, Kind))
334 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
337 uint32_t &CallColumn) const {
338 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
339 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
340 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
343 DWARFDebugInfoEntryInlinedChain
344 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
345 const DWARFUnit *U, const uint64_t Address) const {
346 DWARFDebugInfoEntryInlinedChain InlinedChain;
350 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
351 // Append current DIE to inlined chain only if it has correct tag
352 // (e.g. it is not a lexical block).
353 if (DIE->isSubroutineDIE()) {
354 InlinedChain.DIEs.push_back(*DIE);
356 // Try to get child which also contains provided address.
357 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
359 if (Child->addressRangeContainsAddress(U, Address)) {
360 // Assume there is only one such child.
363 Child = Child->getSibling();
367 // Reverse the obtained chain to make the root of inlined chain last.
368 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());