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 .debug_info 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->getContext().getStringSection(),
422 return form_value.getAsCString(&stringExtractor);
428 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
429 const DWARFCompileUnit* cu,
431 uint64_t fail_value) const {
432 DWARFFormValue form_value;
433 if (getAttributeValue(cu, attr, form_value))
434 return form_value.getUnsigned();
439 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
440 const DWARFCompileUnit* cu,
442 int64_t fail_value) const {
443 DWARFFormValue form_value;
444 if (getAttributeValue(cu, attr, form_value))
445 return form_value.getSigned();
450 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
451 const DWARFCompileUnit* cu,
455 DWARFFormValue form_value;
456 if (getAttributeValue(cu, attr, form_value))
457 return form_value.getReference(cu);
461 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFCompileUnit *CU,
463 uint64_t &HighPC) const {
465 LowPC = getAttributeValueAsUnsigned(CU, DW_AT_low_pc, -1ULL);
467 HighPC = getAttributeValueAsUnsigned(CU, DW_AT_high_pc, -1ULL);
468 return (HighPC != -1ULL);
472 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *CU,
473 DWARFDebugAranges *DebugAranges)
476 if (isSubprogramDIE()) {
477 uint64_t LowPC, HighPC;
478 if (getLowAndHighPC(CU, LowPC, HighPC)) {
479 DebugAranges->appendRange(CU->getOffset(), LowPC, HighPC);
481 // FIXME: try to append ranges from .debug_ranges section.
484 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
486 child->buildAddressRangeTable(CU, DebugAranges);
487 child = child->getSibling();
493 DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
494 const DWARFCompileUnit *CU,
495 const uint64_t Address)
499 uint64_t LowPC, HighPC;
500 if (getLowAndHighPC(CU, LowPC, HighPC))
501 return (LowPC <= Address && Address <= HighPC);
502 // Try to get address ranges from .debug_ranges section.
503 uint32_t RangesOffset = getAttributeValueAsReference(CU, DW_AT_ranges, -1U);
504 if (RangesOffset != -1U) {
505 DWARFDebugRangeList RangeList;
506 if (CU->extractRangeList(RangesOffset, RangeList))
507 return RangeList.containsAddress(CU->getBaseAddress(), Address);
513 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFCompileUnit *CU)
515 if (!isSubroutineDIE())
517 // Try to get mangled name if possible.
518 if (const char *name =
519 getAttributeValueAsString(CU, DW_AT_MIPS_linkage_name, 0))
521 if (const char *name = getAttributeValueAsString(CU, DW_AT_linkage_name, 0))
523 if (const char *name = getAttributeValueAsString(CU, DW_AT_name, 0))
525 // Try to get name from specification DIE.
527 getAttributeValueAsReference(CU, DW_AT_specification, -1U);
528 if (spec_ref != -1U) {
529 DWARFDebugInfoEntryMinimal spec_die;
530 if (spec_die.extract(CU, &spec_ref)) {
531 if (const char *name = spec_die.getSubroutineName(CU))
535 // Try to get name from abstract origin DIE.
536 uint32_t abs_origin_ref =
537 getAttributeValueAsReference(CU, DW_AT_abstract_origin, -1U);
538 if (abs_origin_ref != -1U) {
539 DWARFDebugInfoEntryMinimal abs_origin_die;
540 if (abs_origin_die.extract(CU, &abs_origin_ref)) {
541 if (const char *name = abs_origin_die.getSubroutineName(CU))
548 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFCompileUnit *CU,
551 uint32_t &CallColumn) const {
552 CallFile = getAttributeValueAsUnsigned(CU, DW_AT_call_file, 0);
553 CallLine = getAttributeValueAsUnsigned(CU, DW_AT_call_line, 0);
554 CallColumn = getAttributeValueAsUnsigned(CU, DW_AT_call_column, 0);
557 DWARFDebugInfoEntryMinimal::InlinedChain
558 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
559 const DWARFCompileUnit *CU,
560 const uint64_t Address)
562 DWARFDebugInfoEntryMinimal::InlinedChain InlinedChain;
565 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
566 // Append current DIE to inlined chain only if it has correct tag
567 // (e.g. it is not a lexical block).
568 if (DIE->isSubroutineDIE()) {
569 InlinedChain.push_back(*DIE);
571 // Try to get child which also contains provided address.
572 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
574 if (Child->addressRangeContainsAddress(CU, Address)) {
575 // Assume there is only one such child.
578 Child = Child->getSibling();
582 // Reverse the obtained chain to make the root of inlined chain last.
583 std::reverse(InlinedChain.begin(), InlinedChain.end());