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,
23 const DWARFCompileUnit *cu,
24 unsigned recurseDepth,
25 unsigned indent) const {
26 DataExtractor debug_info_data = cu->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 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
45 for (uint32_t i = 0; i != numAttributes; ++i) {
46 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
47 uint16_t form = AbbrevDecl->getFormByIndex(i);
48 dumpAttribute(OS, cu, &offset, attr, form, indent);
51 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
52 if (recurseDepth > 0 && child) {
54 child->dump(OS, cu, recurseDepth-1, indent+2);
55 child = child->getSibling();
59 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
63 OS.indent(indent) << "NULL\n";
68 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
69 const DWARFCompileUnit *cu,
73 unsigned indent) const {
76 const char *attrString = AttributeString(attr);
80 OS << format("DW_AT_Unknown_%x", attr);
81 const char *formString = FormEncodingString(form);
83 OS << " [" << formString << ']';
85 OS << format(" [DW_FORM_Unknown_%x]", form);
87 DWARFFormValue formValue(form);
89 if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu))
93 formValue.dump(OS, cu);
97 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *CU,
98 const uint8_t *FixedFormSizes,
99 uint32_t *OffsetPtr) {
101 DataExtractor DebugInfoData = CU->getDebugInfoExtractor();
102 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
104 // NULL debug tag entry.
108 AbbrevDecl = CU->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
110 assert(FixedFormSizes); // For best performance this should be specified!
112 // Skip all data in the .debug_info for the attributes
113 for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
114 uint16_t Form = AbbrevDecl->getFormByIndex(i);
116 // FIXME: Currently we're checking if this is less than the last
117 // entry in the fixed_form_sizes table, but this should be changed
118 // to use dynamic dispatch.
119 uint8_t FixedFormSize =
120 (Form < DW_FORM_ref_sig8) ? FixedFormSizes[Form] : 0;
122 *OffsetPtr += FixedFormSize;
123 else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr,
125 // Restore the original offset.
134 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *CU,
135 uint32_t *OffsetPtr) {
136 DataExtractor DebugInfoData = CU->getDebugInfoExtractor();
137 const uint32_t CUEndOffset = CU->getNextCompileUnitOffset();
139 if ((Offset >= CUEndOffset) || !DebugInfoData.isValidOffset(Offset))
141 uint64_t AbbrCode = DebugInfoData.getULEB128(OffsetPtr);
143 // NULL debug tag entry.
147 AbbrevDecl = CU->getAbbreviations()->getAbbreviationDeclaration(AbbrCode);
148 if (0 == AbbrevDecl) {
149 // Restore the original offset.
153 bool IsCompileUnitTag = (AbbrevDecl->getTag() == DW_TAG_compile_unit);
154 if (IsCompileUnitTag)
155 const_cast<DWARFCompileUnit*>(CU)->setBaseAddress(0);
157 // Skip all data in the .debug_info for the attributes
158 for (uint32_t i = 0, n = AbbrevDecl->getNumAttributes(); i < n; ++i) {
159 uint16_t Attr = AbbrevDecl->getAttrByIndex(i);
160 uint16_t Form = AbbrevDecl->getFormByIndex(i);
162 if (IsCompileUnitTag &&
163 ((Attr == DW_AT_entry_pc) || (Attr == DW_AT_low_pc))) {
164 DWARFFormValue FormValue(Form);
165 if (FormValue.extractValue(DebugInfoData, OffsetPtr, CU)) {
166 if (Attr == DW_AT_low_pc || Attr == DW_AT_entry_pc)
167 const_cast<DWARFCompileUnit*>(CU)
168 ->setBaseAddress(FormValue.getUnsigned());
170 } else if (!DWARFFormValue::skipValue(Form, DebugInfoData, OffsetPtr,
172 // Restore the original offset.
180 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
181 return getTag() == DW_TAG_subprogram;
184 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
185 uint32_t Tag = getTag();
186 return Tag == DW_TAG_subprogram ||
187 Tag == DW_TAG_inlined_subroutine;
191 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
193 DWARFFormValue &form_value,
194 uint32_t *end_attr_offset_ptr)
197 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
199 if (attr_idx != -1U) {
200 uint32_t offset = getOffset();
202 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
204 // Skip the abbreviation code so we are at the data for the attributes
205 debug_info_data.getULEB128(&offset);
208 while (idx < attr_idx)
209 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
210 debug_info_data, &offset, cu);
212 const uint32_t attr_offset = offset;
213 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
214 if (form_value.extractValue(debug_info_data, &offset, cu)) {
215 if (end_attr_offset_ptr)
216 *end_attr_offset_ptr = offset;
225 const char *DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
226 const DWARFCompileUnit *CU, const uint16_t Attr,
227 const char *FailValue) const {
228 DWARFFormValue FormValue;
229 if (getAttributeValue(CU, Attr, FormValue))
230 return FormValue.getAsCString(CU);
234 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsAddress(
235 const DWARFCompileUnit *CU, const uint16_t Attr, uint64_t FailValue) const {
236 DWARFFormValue FormValue;
237 if (getAttributeValue(CU, Attr, FormValue))
238 return FormValue.getAsAddress(CU);
242 uint64_t DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
243 const DWARFCompileUnit *CU, const uint16_t Attr, uint64_t FailValue) const {
244 DWARFFormValue FormValue;
245 if (getAttributeValue(CU, Attr, FormValue)) {
246 return FormValue.getUnsigned();
252 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
253 const DWARFCompileUnit* cu,
255 int64_t fail_value) const {
256 DWARFFormValue form_value;
257 if (getAttributeValue(cu, attr, form_value))
258 return form_value.getSigned();
263 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
264 const DWARFCompileUnit* cu,
268 DWARFFormValue form_value;
269 if (getAttributeValue(cu, attr, form_value))
270 return form_value.getReference(cu);
274 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFCompileUnit *CU,
276 uint64_t &HighPC) const {
278 LowPC = getAttributeValueAsAddress(CU, DW_AT_low_pc, -1ULL);
280 HighPC = getAttributeValueAsAddress(CU, DW_AT_high_pc, -1ULL);
281 return (HighPC != -1ULL);
285 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *CU,
286 DWARFDebugAranges *DebugAranges,
287 uint32_t CUOffsetInAranges)
290 if (isSubprogramDIE()) {
291 uint64_t LowPC, HighPC;
292 if (getLowAndHighPC(CU, LowPC, HighPC))
293 DebugAranges->appendRange(CUOffsetInAranges, LowPC, HighPC);
294 // FIXME: try to append ranges from .debug_ranges section.
297 const DWARFDebugInfoEntryMinimal *Child = getFirstChild();
299 Child->buildAddressRangeTable(CU, DebugAranges, CUOffsetInAranges);
300 Child = Child->getSibling();
306 DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
307 const DWARFCompileUnit *CU,
308 const uint64_t Address)
312 uint64_t LowPC, HighPC;
313 if (getLowAndHighPC(CU, LowPC, HighPC))
314 return (LowPC <= Address && Address <= HighPC);
315 // Try to get address ranges from .debug_ranges section.
316 uint32_t RangesOffset = getAttributeValueAsReference(CU, DW_AT_ranges, -1U);
317 if (RangesOffset != -1U) {
318 DWARFDebugRangeList RangeList;
319 if (CU->extractRangeList(RangesOffset, RangeList))
320 return RangeList.containsAddress(CU->getBaseAddress(), Address);
326 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFCompileUnit *CU)
328 if (!isSubroutineDIE())
330 // Try to get mangled name if possible.
331 if (const char *name =
332 getAttributeValueAsString(CU, DW_AT_MIPS_linkage_name, 0))
334 if (const char *name = getAttributeValueAsString(CU, DW_AT_linkage_name, 0))
336 if (const char *name = getAttributeValueAsString(CU, DW_AT_name, 0))
338 // Try to get name from specification DIE.
340 getAttributeValueAsReference(CU, DW_AT_specification, -1U);
341 if (spec_ref != -1U) {
342 DWARFDebugInfoEntryMinimal spec_die;
343 if (spec_die.extract(CU, &spec_ref)) {
344 if (const char *name = spec_die.getSubroutineName(CU))
348 // Try to get name from abstract origin DIE.
349 uint32_t abs_origin_ref =
350 getAttributeValueAsReference(CU, DW_AT_abstract_origin, -1U);
351 if (abs_origin_ref != -1U) {
352 DWARFDebugInfoEntryMinimal abs_origin_die;
353 if (abs_origin_die.extract(CU, &abs_origin_ref)) {
354 if (const char *name = abs_origin_die.getSubroutineName(CU))
361 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFCompileUnit *CU,
364 uint32_t &CallColumn) const {
365 CallFile = getAttributeValueAsUnsigned(CU, DW_AT_call_file, 0);
366 CallLine = getAttributeValueAsUnsigned(CU, DW_AT_call_line, 0);
367 CallColumn = getAttributeValueAsUnsigned(CU, DW_AT_call_column, 0);
370 DWARFDebugInfoEntryInlinedChain
371 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
372 const DWARFCompileUnit *CU, const uint64_t Address) const {
373 DWARFDebugInfoEntryInlinedChain InlinedChain;
374 InlinedChain.CU = CU;
377 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
378 // Append current DIE to inlined chain only if it has correct tag
379 // (e.g. it is not a lexical block).
380 if (DIE->isSubroutineDIE()) {
381 InlinedChain.DIEs.push_back(*DIE);
383 // Try to get child which also contains provided address.
384 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
386 if (Child->addressRangeContainsAddress(CU, Address)) {
387 // Assume there is only one such child.
390 Child = Child->getSibling();
394 // Reverse the obtained chain to make the root of inlined chain last.
395 std::reverse(InlinedChain.DIEs.begin(), InlinedChain.DIEs.end());