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 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
44 for (uint32_t i = 0; i != numAttributes; ++i) {
45 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
46 uint16_t form = AbbrevDecl->getFormByIndex(i);
47 dumpAttribute(OS, u, &offset, attr, form, indent);
50 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
51 if (recurseDepth > 0 && child) {
53 child->dump(OS, u, recurseDepth-1, indent+2);
54 child = child->getSibling();
58 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
62 OS.indent(indent) << "NULL\n";
67 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
70 uint16_t attr, uint16_t form,
71 unsigned indent) const {
74 const char *attrString = AttributeString(attr);
78 OS << format("DW_AT_Unknown_%x", attr);
79 const char *formString = FormEncodingString(form);
81 OS << " [" << formString << ']';
83 OS << format(" [DW_FORM_Unknown_%x]", form);
85 DWARFFormValue formValue(form);
87 if (!formValue.extractValue(u->getDebugInfoExtractor(), offset_ptr, u))
91 formValue.dump(OS, u);
95 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFUnit *U,
96 uint32_t *OffsetPtr) {
98 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
99 uint32_t UEndOffset = U->getNextUnitOffset();
100 if (Offset >= UEndOffset || !DebugInfoData.isValidOffset(Offset))
102 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
104 // NULL debug tag entry.
108 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
109 if (0 == AbbrevDecl) {
110 // Restore the original offset.
114 ArrayRef<uint8_t> FixedFormSizes = DWARFFormValue::getFixedFormSizes(
115 U->getAddressByteSize(), U->getVersion());
116 assert(FixedFormSizes.size() > 0);
118 // Skip all data in the .debug_info for the attributes
119 for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
120 uint16_t Form = AbbrevDecl->getFormByIndex(i);
122 uint8_t FixedFormSize =
123 (Form < FixedFormSizes.size()) ? FixedFormSizes[Form] : 0;
125 *OffsetPtr += FixedFormSize;
126 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
127 // Restore the original offset.
135 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
136 return getTag() == DW_TAG_subprogram;
139 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
140 uint32_t Tag = getTag();
141 return Tag == DW_TAG_subprogram ||
142 Tag == DW_TAG_inlined_subroutine;
145 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
146 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
150 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
154 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
155 uint32_t DebugInfoOffset = getOffset();
157 // Skip the abbreviation code so we are at the data for the attributes
158 DebugInfoData.getULEB128(&DebugInfoOffset);
160 // Skip preceding attribute values.
161 for (uint32_t i = 0; i < AttrIdx; ++i) {
162 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
163 DebugInfoData, &DebugInfoOffset, U);
166 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
167 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
170 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
171 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
172 DWARFFormValue FormValue;
173 if (!getAttributeValue(U, Attr, FormValue))
175 Optional<const char *> Result = FormValue.getAsCString(U);
176 return Result.hasValue() ? Result.getValue() : FailValue;
179 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
180 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
181 DWARFFormValue FormValue;
182 if (!getAttributeValue(U, Attr, FormValue))
184 Optional<uint64_t> Result = FormValue.getAsAddress(U);
185 return Result.hasValue() ? Result.getValue() : FailValue;
188 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
189 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
190 DWARFFormValue FormValue;
191 if (!getAttributeValue(U, Attr, FormValue))
193 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
194 return Result.hasValue() ? Result.getValue() : FailValue;
197 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
198 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
199 DWARFFormValue FormValue;
200 if (!getAttributeValue(U, Attr, FormValue))
202 Optional<uint64_t> Result = FormValue.getAsReference(U);
203 return Result.hasValue() ? Result.getValue() : FailValue;
206 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
207 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
208 DWARFFormValue FormValue;
209 if (!getAttributeValue(U, Attr, FormValue))
211 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
212 return Result.hasValue() ? Result.getValue() : FailValue;
215 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
217 uint64_t &HighPC) const {
218 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
221 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
222 if (HighPC == -1ULL) {
223 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
224 // it represents function size.
225 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
229 return (HighPC != -1ULL);
232 void DWARFDebugInfoEntryMinimal::buildAddressRangeTable(
233 const DWARFUnit *U, DWARFDebugAranges *DebugAranges,
234 uint32_t UOffsetInAranges) const {
236 if (isSubprogramDIE()) {
237 uint64_t LowPC, HighPC;
238 if (getLowAndHighPC(U, LowPC, HighPC))
239 DebugAranges->appendRange(UOffsetInAranges, LowPC, HighPC);
240 // FIXME: try to append ranges from .debug_ranges section.
243 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
245 Child->buildAddressRangeTable(U, DebugAranges, UOffsetInAranges);
246 Child = Child->getSibling();
251 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
252 const DWARFUnit *U, const uint64_t Address) const {
255 uint64_t LowPC, HighPC;
256 if (getLowAndHighPC(U, LowPC, HighPC))
257 return (LowPC <= Address && Address <= HighPC);
258 // Try to get address ranges from .debug_ranges section.
259 uint32_t RangesOffset =
260 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
261 if (RangesOffset != -1U) {
262 DWARFDebugRangeList RangeList;
263 if (U->extractRangeList(RangesOffset, RangeList))
264 return RangeList.containsAddress(U->getBaseAddress(), Address);
270 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U) const {
271 if (!isSubroutineDIE())
273 // Try to get mangled name if possible.
274 if (const char *name =
275 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, 0))
277 if (const char *name = getAttributeValueAsString(U, DW_AT_linkage_name, 0))
279 if (const char *name = getAttributeValueAsString(U, DW_AT_name, 0))
281 // Try to get name from specification DIE.
283 getAttributeValueAsReference(U, DW_AT_specification, -1U);
284 if (spec_ref != -1U) {
285 DWARFDebugInfoEntryMinimal spec_die;
286 if (spec_die.extractFast(U, &spec_ref)) {
287 if (const char *name = spec_die.getSubroutineName(U))
291 // Try to get name from abstract origin DIE.
292 uint32_t abs_origin_ref =
293 getAttributeValueAsReference(U, DW_AT_abstract_origin, -1U);
294 if (abs_origin_ref != -1U) {
295 DWARFDebugInfoEntryMinimal abs_origin_die;
296 if (abs_origin_die.extractFast(U, &abs_origin_ref)) {
297 if (const char *name = abs_origin_die.getSubroutineName(U))
304 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
307 uint32_t &CallColumn) const {
308 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
309 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
310 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
313 DWARFDebugInfoEntryInlinedChain
314 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
315 const DWARFUnit *U, const uint64_t Address) const {
316 DWARFDebugInfoEntryInlinedChain InlinedChain;
320 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
321 // Append current DIE to inlined chain only if it has correct tag
322 // (e.g. it is not a lexical block).
323 if (DIE->isSubroutineDIE()) {
324 InlinedChain.DIEs.push_back(*DIE);
326 // Try to get child which also contains provided address.
327 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
329 if (Child->addressRangeContainsAddress(U, Address)) {
330 // Assume there is only one such child.
333 Child = Child->getSibling();
337 // Reverse the obtained chain to make the root of inlined chain last.
338 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());