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 const uint8_t *FixedFormSizes,
97 uint32_t *OffsetPtr) {
99 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
100 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
102 // NULL debug tag entry.
106 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
108 assert(FixedFormSizes); // For best performance this should be specified!
110 // Skip all data in the .debug_info for the attributes
111 for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
112 uint16_t Form = AbbrevDecl->getFormByIndex(i);
114 // FIXME: Currently we're checking if this is less than the last
115 // entry in the fixed_form_sizes table, but this should be changed
116 // to use dynamic dispatch.
117 uint8_t FixedFormSize =
118 (Form < DW_FORM_ref_sig8) ? FixedFormSizes[Form] : 0;
120 *OffsetPtr += FixedFormSize;
121 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr,
123 // Restore the original offset.
131 bool DWARFDebugInfoEntryMinimal::extract(const DWARFUnit *U,
132 uint32_t *OffsetPtr) {
133 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
134 const uint32_t UEndOffset = U->getNextUnitOffset();
136 if ((Offset >= UEndOffset) || !DebugInfoData.isValidOffset(Offset))
138 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
140 // NULL debug tag entry.
144 AbbrevDecl = U->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
145 if (0 == AbbrevDecl) {
146 // Restore the original offset.
150 bool IsCompileUnitTag = (AbbrevDecl->getTag() == DW_TAG_compile_unit);
151 if (IsCompileUnitTag)
152 const_cast<DWARFUnit *>(U)->setBaseAddress(0);
154 // Skip all data in the .debug_info for the attributes
155 for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
156 uint16_t Attr = AbbrevDecl->getAttrByIndex(i);
157 uint16_t Form = AbbrevDecl->getFormByIndex(i);
159 if (IsCompileUnitTag &&
160 ((Attr == DW_AT_entry_pc) || (Attr == DW_AT_low_pc))) {
161 DWARFFormValue FormValue(Form);
162 if (FormValue.extractValue(DebugInfoData, OffsetPtr, U)) {
163 if (Attr == DW_AT_low_pc || Attr == DW_AT_entry_pc) {
164 Optional<uint64_t> BaseAddr = FormValue.getAsAddress(U);
165 if (BaseAddr.hasValue())
166 const_cast<DWARFUnit *>(U)->setBaseAddress(BaseAddr.getValue());
169 } else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr, U)) {
170 // Restore the original offset.
178 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
179 return getTag() == DW_TAG_subprogram;
182 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
183 uint32_t Tag = getTag();
184 return Tag == DW_TAG_subprogram ||
185 Tag == DW_TAG_inlined_subroutine;
188 bool DWARFDebugInfoEntryMinimal::getAttributeValue(
189 const DWARFUnit *U, const uint16_t Attr, DWARFFormValue &FormValue) const {
193 uint32_t AttrIdx = AbbrevDecl->findAttributeIndex(Attr);
197 DataExtractor DebugInfoData = U->getDebugInfoExtractor();
198 uint32_t DebugInfoOffset = getOffset();
200 // Skip the abbreviation code so we are at the data for the attributes
201 DebugInfoData.getULEB128(&DebugInfoOffset);
203 // Skip preceding attribute values.
204 for (uint32_t i = 0; i < AttrIdx; ++i) {
205 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(i),
206 DebugInfoData, &DebugInfoOffset, U);
209 FormValue = DWARFFormValue(AbbrevDecl->getFormByIndex(AttrIdx));
210 return FormValue.extractValue(DebugInfoData, &DebugInfoOffset, U);
213 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
214 const DWARFUnit *U, const uint16_t Attr, const char *FailValue) const {
215 DWARFFormValue FormValue;
216 if (!getAttributeValue(U, Attr, FormValue))
218 Optional<const char *> Result = FormValue.getAsCString(U);
219 return Result.hasValue() ? Result.getValue() : FailValue;
222 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
223 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
224 DWARFFormValue FormValue;
225 if (!getAttributeValue(U, Attr, FormValue))
227 Optional<uint64_t> Result = FormValue.getAsAddress(U);
228 return Result.hasValue() ? Result.getValue() : FailValue;
231 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsignedConstant(
232 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
233 DWARFFormValue FormValue;
234 if (!getAttributeValue(U, Attr, FormValue))
236 Optional<uint64_t> Result = FormValue.getAsUnsignedConstant();
237 return Result.hasValue() ? Result.getValue() : FailValue;
240 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
241 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
242 DWARFFormValue FormValue;
243 if (!getAttributeValue(U, Attr, FormValue))
245 Optional<uint64_t> Result = FormValue.getAsReference(U);
246 return Result.hasValue() ? Result.getValue() : FailValue;
249 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsSectionOffset(
250 const DWARFUnit *U, const uint16_t Attr, uint64_t FailValue) const {
251 DWARFFormValue FormValue;
252 if (!getAttributeValue(U, Attr, FormValue))
254 Optional<uint64_t> Result = FormValue.getAsSectionOffset();
255 return Result.hasValue() ? Result.getValue() : FailValue;
258 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFUnit *U,
260 uint64_t &HighPC) const {
261 LowPC = getAttributeValueAsAddress(U, DW_AT_low_pc, -1ULL);
264 HighPC = getAttributeValueAsAddress(U, DW_AT_high_pc, -1ULL);
265 if (HighPC == -1ULL) {
266 // Since DWARF4, DW_AT_high_pc may also be of class constant, in which case
267 // it represents function size.
268 HighPC = getAttributeValueAsUnsignedConstant(U, DW_AT_high_pc, -1ULL);
272 return (HighPC != -1ULL);
275 void DWARFDebugInfoEntryMinimal::buildAddressRangeTable(
276 const DWARFUnit *U, DWARFDebugAranges *DebugAranges,
277 uint32_t UOffsetInAranges) const {
279 if (isSubprogramDIE()) {
280 uint64_t LowPC, HighPC;
281 if (getLowAndHighPC(U, LowPC, HighPC))
282 DebugAranges->appendRange(UOffsetInAranges, LowPC, HighPC);
283 // FIXME: try to append ranges from .debug_ranges section.
286 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
288 Child->buildAddressRangeTable(U, DebugAranges, UOffsetInAranges);
289 Child = Child->getSibling();
294 bool DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
295 const DWARFUnit *U, const uint64_t Address) const {
298 uint64_t LowPC, HighPC;
299 if (getLowAndHighPC(U, LowPC, HighPC))
300 return (LowPC <= Address && Address <= HighPC);
301 // Try to get address ranges from .debug_ranges section.
302 uint32_t RangesOffset =
303 getAttributeValueAsSectionOffset(U, DW_AT_ranges, -1U);
304 if (RangesOffset != -1U) {
305 DWARFDebugRangeList RangeList;
306 if (U->extractRangeList(RangesOffset, RangeList))
307 return RangeList.containsAddress(U->getBaseAddress(), Address);
313 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFUnit *U) const {
314 if (!isSubroutineDIE())
316 // Try to get mangled name if possible.
317 if (const char *name =
318 getAttributeValueAsString(U, DW_AT_MIPS_linkage_name, 0))
320 if (const char *name = getAttributeValueAsString(U, DW_AT_linkage_name, 0))
322 if (const char *name = getAttributeValueAsString(U, DW_AT_name, 0))
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 (spec_die.extract(U, &spec_ref)) {
330 if (const char *name = spec_die.getSubroutineName(U))
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 (abs_origin_die.extract(U, &abs_origin_ref)) {
340 if (const char *name = abs_origin_die.getSubroutineName(U))
347 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFUnit *U,
350 uint32_t &CallColumn) const {
351 CallFile = getAttributeValueAsUnsignedConstant(U, DW_AT_call_file, 0);
352 CallLine = getAttributeValueAsUnsignedConstant(U, DW_AT_call_line, 0);
353 CallColumn = getAttributeValueAsUnsignedConstant(U, DW_AT_call_column, 0);
356 DWARFDebugInfoEntryInlinedChain
357 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
358 const DWARFUnit *U, const uint64_t Address) const {
359 DWARFDebugInfoEntryInlinedChain InlinedChain;
363 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
364 // Append current DIE to inlined chain only if it has correct tag
365 // (e.g. it is not a lexical block).
366 if (DIE->isSubroutineDIE()) {
367 InlinedChain.DIEs.push_back(*DIE);
369 // Try to get child which also contains provided address.
370 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
372 if (Child->addressRangeContainsAddress(U, Address)) {
373 // Assume there is only one such child.
376 Child = Child->getSibling();
380 // Reverse the obtained chain to make the root of inlined chain last.
381 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());