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 "DWARFFormValue.h"
15 #include "llvm/Support/Dwarf.h"
16 #include "llvm/Support/Format.h"
17 #include "llvm/Support/raw_ostream.h"
19 using namespace dwarf;
21 void DWARFDebugInfoEntryMinimal::dump(raw_ostream &OS,
22 const DWARFCompileUnit *cu,
23 unsigned recurseDepth,
24 unsigned indent) const {
25 DataExtractor debug_info_data = cu->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, cu, &offset, attr, form, indent);
50 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
51 if (recurseDepth > 0 && child) {
53 child->dump(OS, cu, 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,
68 const DWARFCompileUnit *cu,
72 unsigned indent) const {
73 OS << format("0x%8.8x: ", *offset_ptr);
75 const char *attrString = AttributeString(attr);
79 OS << format("DW_AT_Unknown_%x", attr);
80 const char *formString = FormEncodingString(form);
82 OS << " [" << formString << ']';
84 OS << format(" [DW_FORM_Unknown_%x]", form);
86 DWARFFormValue formValue(form);
88 if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu))
92 formValue.dump(OS, cu);
96 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *cu,
97 const uint8_t *fixed_form_sizes,
98 uint32_t *offset_ptr) {
101 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
102 uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr);
104 assert(fixed_form_sizes); // For best performance this should be specified!
107 uint32_t offset = *offset_ptr;
109 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
111 // Skip all data in the .debug_info for the attributes
112 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
115 for (i=0; i<numAttributes; ++i) {
116 form = AbbrevDecl->getFormByIndex(i);
118 const uint8_t fixed_skip_size = fixed_form_sizes[form];
120 offset += fixed_skip_size;
122 bool form_is_indirect = false;
124 form_is_indirect = false;
125 uint32_t form_size = 0;
127 // Blocks if inlined data that have a length field and the data bytes
128 // inlined in the .debug_info.
129 case DW_FORM_exprloc:
131 form_size = debug_info_data.getULEB128(&offset);
134 form_size = debug_info_data.getU8(&offset);
137 form_size = debug_info_data.getU16(&offset);
140 form_size = debug_info_data.getU32(&offset);
143 // Inlined NULL terminated C-strings
145 debug_info_data.getCStr(&offset);
148 // Compile unit address sized values
150 case DW_FORM_ref_addr:
151 form_size = cu->getAddressByteSize();
155 case DW_FORM_flag_present:
182 case DW_FORM_ref_sig8:
186 // signed or unsigned LEB 128 values
189 case DW_FORM_ref_udata:
190 debug_info_data.getULEB128(&offset);
193 case DW_FORM_indirect:
194 form_is_indirect = true;
195 form = debug_info_data.getULEB128(&offset);
198 case DW_FORM_sec_offset:
199 if (cu->getAddressByteSize() == 4)
200 debug_info_data.getU32(offset_ptr);
202 debug_info_data.getU64(offset_ptr);
206 *offset_ptr = Offset;
211 } while (form_is_indirect);
214 *offset_ptr = offset;
218 return true; // NULL debug tag entry
223 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
224 uint32_t *offset_ptr) {
225 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
226 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
227 const uint8_t cu_addr_size = cu->getAddressByteSize();
228 uint32_t offset = *offset_ptr;
229 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
232 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
235 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
238 uint16_t tag = AbbrevDecl->getTag();
240 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
241 if(cu && isCompileUnitTag)
242 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
244 // Skip all data in the .debug_info for the attributes
245 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
246 for (uint32_t i = 0; i != numAttributes; ++i) {
247 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
248 uint16_t form = AbbrevDecl->getFormByIndex(i);
250 if (isCompileUnitTag &&
251 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
252 DWARFFormValue form_value(form);
253 if (form_value.extractValue(debug_info_data, &offset, cu)) {
254 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
255 const_cast<DWARFCompileUnit*>(cu)
256 ->setBaseAddress(form_value.getUnsigned());
259 bool form_is_indirect = false;
261 form_is_indirect = false;
262 register uint32_t form_size = 0;
264 // Blocks if inlined data that have a length field and the data
265 // bytes // inlined in the .debug_info
266 case DW_FORM_exprloc:
268 form_size = debug_info_data.getULEB128(&offset);
271 form_size = debug_info_data.getU8(&offset);
274 form_size = debug_info_data.getU16(&offset);
277 form_size = debug_info_data.getU32(&offset);
280 // Inlined NULL terminated C-strings
282 debug_info_data.getCStr(&offset);
285 // Compile unit address sized values
287 case DW_FORM_ref_addr:
288 form_size = cu_addr_size;
292 case DW_FORM_flag_present:
322 case DW_FORM_ref_sig8:
326 // signed or unsigned LEB 128 values
329 case DW_FORM_ref_udata:
330 debug_info_data.getULEB128(&offset);
333 case DW_FORM_indirect:
334 form = debug_info_data.getULEB128(&offset);
335 form_is_indirect = true;
338 case DW_FORM_sec_offset:
339 if (cu->getAddressByteSize() == 4)
340 debug_info_data.getU32(offset_ptr);
342 debug_info_data.getU64(offset_ptr);
346 *offset_ptr = offset;
351 } while (form_is_indirect);
354 *offset_ptr = offset;
359 *offset_ptr = offset;
360 return true; // NULL debug tag entry
367 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
368 return getTag() == DW_TAG_subprogram;
371 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
372 uint32_t Tag = getTag();
373 return Tag == DW_TAG_subprogram ||
374 Tag == DW_TAG_inlined_subroutine;
378 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
380 DWARFFormValue &form_value,
381 uint32_t *end_attr_offset_ptr)
384 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
386 if (attr_idx != -1U) {
387 uint32_t offset = getOffset();
389 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
391 // Skip the abbreviation code so we are at the data for the attributes
392 debug_info_data.getULEB128(&offset);
395 while (idx < attr_idx)
396 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
397 debug_info_data, &offset, cu);
399 const uint32_t attr_offset = offset;
400 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
401 if (form_value.extractValue(debug_info_data, &offset, cu)) {
402 if (end_attr_offset_ptr)
403 *end_attr_offset_ptr = offset;
413 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
414 const DWARFCompileUnit* cu,
416 const char* fail_value)
418 DWARFFormValue form_value;
419 if (getAttributeValue(cu, attr, form_value)) {
420 DataExtractor stringExtractor(cu->getStringSection(), false, 0);
421 return form_value.getAsCString(&stringExtractor);
427 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
428 const DWARFCompileUnit* cu,
430 uint64_t fail_value) const {
431 DWARFFormValue form_value;
432 if (getAttributeValue(cu, attr, form_value))
433 return form_value.getUnsigned();
438 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
439 const DWARFCompileUnit* cu,
441 int64_t fail_value) const {
442 DWARFFormValue form_value;
443 if (getAttributeValue(cu, attr, form_value))
444 return form_value.getSigned();
449 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
450 const DWARFCompileUnit* cu,
454 DWARFFormValue form_value;
455 if (getAttributeValue(cu, attr, form_value))
456 return form_value.getReference(cu);
460 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFCompileUnit *CU,
462 uint64_t &HighPC) const {
464 LowPC = getAttributeValueAsUnsigned(CU, DW_AT_low_pc, -1ULL);
466 HighPC = getAttributeValueAsUnsigned(CU, DW_AT_high_pc, -1ULL);
467 return (HighPC != -1ULL);
471 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *CU,
472 DWARFDebugAranges *DebugAranges)
475 if (isSubprogramDIE()) {
476 uint64_t LowPC, HighPC;
477 if (getLowAndHighPC(CU, LowPC, HighPC)) {
478 DebugAranges->appendRange(CU->getOffset(), LowPC, HighPC);
480 // FIXME: try to append ranges from .debug_ranges section.
483 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
485 child->buildAddressRangeTable(CU, DebugAranges);
486 child = child->getSibling();
492 DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
493 const DWARFCompileUnit *CU,
494 const uint64_t Address)
498 uint64_t LowPC, HighPC;
499 if (getLowAndHighPC(CU, LowPC, HighPC))
500 return (LowPC <= Address && Address <= HighPC);
501 // Try to get address ranges from .debug_ranges section.
502 uint32_t RangesOffset = getAttributeValueAsReference(CU, DW_AT_ranges, -1U);
503 if (RangesOffset != -1U) {
504 DWARFDebugRangeList RangeList;
505 if (CU->extractRangeList(RangesOffset, RangeList))
506 return RangeList.containsAddress(CU->getBaseAddress(), Address);
512 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFCompileUnit *CU)
514 if (!isSubroutineDIE())
516 // Try to get mangled name if possible.
517 if (const char *name =
518 getAttributeValueAsString(CU, DW_AT_MIPS_linkage_name, 0))
520 if (const char *name = getAttributeValueAsString(CU, DW_AT_linkage_name, 0))
522 if (const char *name = getAttributeValueAsString(CU, DW_AT_name, 0))
524 // Try to get name from specification DIE.
526 getAttributeValueAsReference(CU, DW_AT_specification, -1U);
527 if (spec_ref != -1U) {
528 DWARFDebugInfoEntryMinimal spec_die;
529 if (spec_die.extract(CU, &spec_ref)) {
530 if (const char *name = spec_die.getSubroutineName(CU))
534 // Try to get name from abstract origin DIE.
535 uint32_t abs_origin_ref =
536 getAttributeValueAsReference(CU, DW_AT_abstract_origin, -1U);
537 if (abs_origin_ref != -1U) {
538 DWARFDebugInfoEntryMinimal abs_origin_die;
539 if (abs_origin_die.extract(CU, &abs_origin_ref)) {
540 if (const char *name = abs_origin_die.getSubroutineName(CU))
547 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFCompileUnit *CU,
550 uint32_t &CallColumn) const {
551 CallFile = getAttributeValueAsUnsigned(CU, DW_AT_call_file, 0);
552 CallLine = getAttributeValueAsUnsigned(CU, DW_AT_call_line, 0);
553 CallColumn = getAttributeValueAsUnsigned(CU, DW_AT_call_column, 0);
556 DWARFDebugInfoEntryMinimal::InlinedChain
557 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
558 const DWARFCompileUnit *CU,
559 const uint64_t Address)
561 DWARFDebugInfoEntryMinimal::InlinedChain InlinedChain;
564 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
565 // Append current DIE to inlined chain only if it has correct tag
566 // (e.g. it is not a lexical block).
567 if (DIE->isSubroutineDIE()) {
568 InlinedChain.push_back(*DIE);
570 // Try to get child which also contains provided address.
571 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
573 if (Child->addressRangeContainsAddress(CU, Address)) {
574 // Assume there is only one such child.
577 Child = Child->getSibling();
581 // Reverse the obtained chain to make the root of inlined chain last.
582 std::reverse(InlinedChain.begin(), InlinedChain.end());