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;
22 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS, const DWARFUnit *u,
23 unsigned recurseDepth,
24 unsigned indent) const {
25 DataExtractor debug_info_data = u->getDebugInfoExtractor();
26 uint32_t offset = Offset;
28 if (debug_info_data.isValidOffset(offset)) {
29 uint32_t abbrCode = debug_info_data.getULEB128(&offset);
31 OS << format("\n0x%8.8x: ", Offset);
34 const char *tagString = TagString(getTag());
36 OS.indent(indent) << tagString;
38 OS.indent(indent) << format("DW_TAG_Unknown_%x", getTag());
39 OS << format(" [%u] %c\n", abbrCode,
40 AbbrevDecl->hasChildren() ? '*' : ' ');
42 // Dump all data in the DIE for the attributes.
43 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
44 dumpAttribute(OS, u, &offset, AttrSpec.Attr, AttrSpec.Form, indent);
47 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
48 if (recurseDepth > 0 && child) {
50 child->dump(OS, u, recurseDepth-1, indent+2);
51 child = child->getSibling();
55 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
59 OS.indent(indent) << "NULL\n";
64 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
67 uint16_t attr, uint16_t form,
68 unsigned indent) const {
71 const char *attrString = AttributeString(attr);
75 OS << format("DW_AT_Unknown_%x", attr);
76 const char *formString = FormEncodingString(form);
78 OS << " [" << formString << ']';
80 OS << format(" [DW_FORM_Unknown_%x]", form);
82 DWARFFormValue formValue(form);
84 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
88 formValue.dump(OS, u);
92 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
93 uint32_t *OffsetPtr) {
95 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
96 uint32_t UEndOffset = U->getNextUnitOffset();
97 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
99 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
101 // NULL debug tag entry.
102 AbbrevDecl = nullptr;
105 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
106 if (nullptr == AbbrevDecl) {
107 // Restore the original offset.
111 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
112 U->getAddressByteSize(), U->getVersion());
113 assert(FixedFormSizes.size() > 0);
115 // Skip all data in the .debug_info for the attributes
116 for (const auto &AttrSpec : AbbrevDecl->attributes()) {
117 uint16_t Form = AttrSpec.Form;
119 uint8_t FixedFormSize =
120 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
122 *OffsetPtr += FixedFormSize;
123 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
124 // Restore the original offset.
132 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
133 return getTag() == DW_TAG_subprogram;
136 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
137 uint32_t Tag = getTag();
138 return Tag == DW_TAG_subprogram ||
139 Tag == DW_TAG_inlined_subroutine;
142 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
143 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
147 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
151 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
152 uint32_t DebugInfoOffset = getOffset();
154 // Skip the abbreviation code so we are at the data for the attributes
155 DebugInfoData.getULEB128(&DebugInfoOffset);
157 // Skip preceding attribute values.
158 for (uint32_t i = 0; i < AttrIdx; ++i) {
159 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
160 DebugInfoData, &DebugInfoOffset, U);
163 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
164 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
167 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
168 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
169 DWARFFormValue FormValue;
170 if (!getAttributeValue(U, Attr, FormValue))
172 Optional<const char *> Result = FormValue.getAsCString(U);
173 return Result.hasValue() ? Result.getValue() : FailValue;
176 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
177 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
178 DWARFFormValue FormValue;
179 if (!getAttributeValue(U, Attr, FormValue))
181 Optional<uint64_t> Result = FormValue.getAsAddress(U);
182 return Result.hasValue() ? Result.getValue() : FailValue;
185 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
186 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
187 DWARFFormValue FormValue;
188 if (!getAttributeValue(U, Attr, FormValue))
190 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
191 return Result.hasValue() ? Result.getValue() : FailValue;
194 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
195 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
196 DWARFFormValue FormValue;
197 if (!getAttributeValue(U, Attr, FormValue))
199 Optional<uint64_t> Result = FormValue.getAsReference(U);
200 return Result.hasValue() ? Result.getValue() : FailValue;
203 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
204 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
205 DWARFFormValue FormValue;
206 if (!getAttributeValue(U, Attr, FormValue))
208 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
209 return Result.hasValue() ? Result.getValue() : FailValue;
212 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
214 uint64_t &HighPC) const {
215 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
218 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
219 if (HighPC == -1ULL) {
220 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
221 // it represents function size.
222 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
226 return (HighPC != -1ULL);
229 DWARFAddressRangesVector
230 DWARFDebugInfoEntryMinimal::getAddressRanges(const DWARFUnit *U) const {
232 return DWARFAddressRangesVector{};
233 // Single range specified by low/high PC.
234 uint64_t LowPC, HighPC;
235 if (getLowAndHighPC(U, LowPC, HighPC)) {
236 return DWARFAddressRangesVector{std::make_pair(LowPC, HighPC)};
238 // Multiple ranges from .debug_ranges section.
239 uint32_t RangesOffset =
240 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
241 if (RangesOffset != -1U) {
242 DWARFDebugRangeList RangeList;
243 if (U->extractRangeList(RangesOffset, RangeList))
244 return RangeList.getAbsoluteRanges(U->getBaseAddress());
246 return DWARFAddressRangesVector{};
249 void DWARFDebugInfoEntryMinimal::collectChildrenAddressRanges(
250 const DWARFUnit *U, DWARFAddressRangesVector& Ranges) const {
253 if (isSubprogramDIE()) {
254 const auto &DIERanges = getAddressRanges(U);
255 Ranges.insert(Ranges.end(), DIERanges.begin(), DIERanges.end());
258 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
260 Child->collectChildrenAddressRanges(U, Ranges);
261 Child = Child->getSibling();
265 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
266 const DWARFUnit *U, const uint64_t Address) const {
267 for (const auto& R : getAddressRanges(U)) {
268 if (R.first <= Address && Address < R.second)
275 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U) const {
276 if (!isSubroutineDIE())
278 // Try to get mangled name if possible.
279 if (const char *name =
280 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, nullptr))
282 if (const char *name = getAttributeValueAsString(U, DW_AT_linkage_name,
285 if (const char *name = getAttributeValueAsString(U, DW_AT_name, nullptr))
287 // Try to get name from specification DIE.
289 getAttributeValueAsReference(U, DW_AT_specification, -1U);
290 if (spec_ref != -1U) {
291 DWARFDebugInfoEntryMinimal spec_die;
292 if (spec_die.extractFast(U, &spec_ref)) {
293 if (const char *name = spec_die.getSubroutineName(U))
297 // Try to get name from abstract origin DIE.
298 uint32_t abs_origin_ref =
299 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
300 if (abs_origin_ref != -1U) {
301 DWARFDebugInfoEntryMinimal abs_origin_die;
302 if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
303 if (const char *name = abs_origin_die.getSubroutineName(U))
310 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
313 uint32_t &CallColumn) const {
314 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
315 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
316 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
319 DWARFDebugInfoEntryInlinedChain
320 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
321 const DWARFUnit *U, const uint64_t Address) const {
322 DWARFDebugInfoEntryInlinedChain InlinedChain;
326 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
327 // Append current DIE to inlined chain only if it has correct tag
328 // (e.g. it is not a lexical block).
329 if (DIE->isSubroutineDIE()) {
330 InlinedChain.DIEs.push_back(*DIE);
332 // Try to get child which also contains provided address.
333 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
335 if (Child->addressRangeContainsAddress(U, Address)) {
336 // Assume there is only one such child.
339 Child = Child->getSibling();
343 // Reverse the obtained chain to make the root of inlined chain last.
344 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());