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/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 {
74 OS << format("0x%8.8x: ", *offset_ptr);
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 *fixed_form_sizes,
99 uint32_t *offset_ptr) {
100 Offset = *offset_ptr;
102 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
103 uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr);
105 assert(fixed_form_sizes); // For best performance this should be specified!
108 uint32_t offset = *offset_ptr;
110 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
112 // Skip all data in the .debug_info for the attributes
113 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
116 for (i=0; i<numAttributes; ++i) {
118 form = AbbrevDecl->getFormByIndex(i);
120 // FIXME: Currently we're checking if this is less than the last
121 // entry in the fixed_form_sizes table, but this should be changed
122 // to use dynamic dispatch.
123 const uint8_t fixed_skip_size = (form < DW_FORM_ref_sig8) ?
124 fixed_form_sizes[form] : 0;
126 offset += fixed_skip_size;
128 bool form_is_indirect = false;
130 form_is_indirect = false;
131 uint32_t form_size = 0;
133 // Blocks if inlined data that have a length field and the data bytes
134 // inlined in the .debug_info.
135 case DW_FORM_exprloc:
137 form_size = debug_info_data.getULEB128(&offset);
140 form_size = debug_info_data.getU8(&offset);
143 form_size = debug_info_data.getU16(&offset);
146 form_size = debug_info_data.getU32(&offset);
149 // Inlined NULL terminated C-strings
151 debug_info_data.getCStr(&offset);
154 // Compile unit address sized values
156 case DW_FORM_ref_addr:
157 form_size = cu->getAddressByteSize();
161 case DW_FORM_flag_present:
188 case DW_FORM_ref_sig8:
192 // signed or unsigned LEB 128 values
195 case DW_FORM_ref_udata:
196 case DW_FORM_GNU_str_index:
197 case DW_FORM_GNU_addr_index:
198 debug_info_data.getULEB128(&offset);
201 case DW_FORM_indirect:
202 form_is_indirect = true;
203 form = debug_info_data.getULEB128(&offset);
206 case DW_FORM_sec_offset:
207 if (cu->getAddressByteSize() == 4)
208 debug_info_data.getU32(offset_ptr);
210 debug_info_data.getU64(offset_ptr);
214 *offset_ptr = Offset;
218 } while (form_is_indirect);
221 *offset_ptr = offset;
225 return true; // NULL debug tag entry
230 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
231 uint32_t *offset_ptr) {
232 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
233 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
234 const uint8_t cu_addr_size = cu->getAddressByteSize();
235 uint32_t offset = *offset_ptr;
236 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
239 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
242 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
245 uint16_t tag = AbbrevDecl->getTag();
247 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
248 if(cu && isCompileUnitTag)
249 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
251 // Skip all data in the .debug_info for the attributes
252 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
253 for (uint32_t i = 0; i != numAttributes; ++i) {
254 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
255 uint16_t form = AbbrevDecl->getFormByIndex(i);
257 if (isCompileUnitTag &&
258 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
259 DWARFFormValue form_value(form);
260 if (form_value.extractValue(debug_info_data, &offset, cu)) {
261 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
262 const_cast<DWARFCompileUnit*>(cu)
263 ->setBaseAddress(form_value.getUnsigned());
266 bool form_is_indirect = false;
268 form_is_indirect = false;
269 register uint32_t form_size = 0;
271 // Blocks if inlined data that have a length field and the data
272 // bytes // inlined in the .debug_info
273 case DW_FORM_exprloc:
275 form_size = debug_info_data.getULEB128(&offset);
278 form_size = debug_info_data.getU8(&offset);
281 form_size = debug_info_data.getU16(&offset);
284 form_size = debug_info_data.getU32(&offset);
287 // Inlined NULL terminated C-strings
289 debug_info_data.getCStr(&offset);
292 // Compile unit address sized values
294 case DW_FORM_ref_addr:
295 form_size = cu_addr_size;
299 case DW_FORM_flag_present:
329 case DW_FORM_ref_sig8:
333 // signed or unsigned LEB 128 values
336 case DW_FORM_ref_udata:
337 case DW_FORM_GNU_str_index:
338 case DW_FORM_GNU_addr_index:
339 debug_info_data.getULEB128(&offset);
342 case DW_FORM_indirect:
343 form = debug_info_data.getULEB128(&offset);
344 form_is_indirect = true;
347 case DW_FORM_sec_offset:
348 if (cu->getAddressByteSize() == 4)
349 debug_info_data.getU32(offset_ptr);
351 debug_info_data.getU64(offset_ptr);
355 *offset_ptr = offset;
360 } while (form_is_indirect);
363 *offset_ptr = offset;
368 *offset_ptr = offset;
369 return true; // NULL debug tag entry
376 bool DWARFDebugInfoEntryMinimal::isSubprogramDIE() const {
377 return getTag() == DW_TAG_subprogram;
380 bool DWARFDebugInfoEntryMinimal::isSubroutineDIE() const {
381 uint32_t Tag = getTag();
382 return Tag == DW_TAG_subprogram ||
383 Tag == DW_TAG_inlined_subroutine;
387 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
389 DWARFFormValue &form_value,
390 uint32_t *end_attr_offset_ptr)
393 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
395 if (attr_idx != -1U) {
396 uint32_t offset = getOffset();
398 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
400 // Skip the abbreviation code so we are at the data for the attributes
401 debug_info_data.getULEB128(&offset);
404 while (idx < attr_idx)
405 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
406 debug_info_data, &offset, cu);
408 const uint32_t attr_offset = offset;
409 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
410 if (form_value.extractValue(debug_info_data, &offset, cu)) {
411 if (end_attr_offset_ptr)
412 *end_attr_offset_ptr = offset;
422 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
423 const DWARFCompileUnit* cu,
425 const char* fail_value)
427 DWARFFormValue form_value;
428 if (getAttributeValue(cu, attr, form_value)) {
429 DataExtractor stringExtractor(cu->getStringSection(), false, 0);
430 return form_value.getAsCString(&stringExtractor);
436 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
437 const DWARFCompileUnit* cu,
439 uint64_t fail_value) const {
440 DWARFFormValue form_value;
441 if (getAttributeValue(cu, attr, form_value))
442 return form_value.getUnsigned();
447 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
448 const DWARFCompileUnit* cu,
450 int64_t fail_value) const {
451 DWARFFormValue form_value;
452 if (getAttributeValue(cu, attr, form_value))
453 return form_value.getSigned();
458 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(
459 const DWARFCompileUnit* cu,
463 DWARFFormValue form_value;
464 if (getAttributeValue(cu, attr, form_value))
465 return form_value.getReference(cu);
469 bool DWARFDebugInfoEntryMinimal::getLowAndHighPC(const DWARFCompileUnit *CU,
471 uint64_t &HighPC) const {
473 LowPC = getAttributeValueAsUnsigned(CU, DW_AT_low_pc, -1ULL);
475 HighPC = getAttributeValueAsUnsigned(CU, DW_AT_high_pc, -1ULL);
476 return (HighPC != -1ULL);
480 DWARFDebugInfoEntryMinimal::buildAddressRangeTable(const DWARFCompileUnit *CU,
481 DWARFDebugAranges *DebugAranges)
484 if (isSubprogramDIE()) {
485 uint64_t LowPC, HighPC;
486 if (getLowAndHighPC(CU, LowPC, HighPC)) {
487 DebugAranges->appendRange(CU->getOffset(), LowPC, HighPC);
489 // FIXME: try to append ranges from .debug_ranges section.
492 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
494 child->buildAddressRangeTable(CU, DebugAranges);
495 child = child->getSibling();
501 DWARFDebugInfoEntryMinimal::addressRangeContainsAddress(
502 const DWARFCompileUnit *CU,
503 const uint64_t Address)
507 uint64_t LowPC, HighPC;
508 if (getLowAndHighPC(CU, LowPC, HighPC))
509 return (LowPC <= Address && Address <= HighPC);
510 // Try to get address ranges from .debug_ranges section.
511 uint32_t RangesOffset = getAttributeValueAsReference(CU, DW_AT_ranges, -1U);
512 if (RangesOffset != -1U) {
513 DWARFDebugRangeList RangeList;
514 if (CU->extractRangeList(RangesOffset, RangeList))
515 return RangeList.containsAddress(CU->getBaseAddress(), Address);
521 DWARFDebugInfoEntryMinimal::getSubroutineName(const DWARFCompileUnit *CU)
523 if (!isSubroutineDIE())
525 // Try to get mangled name if possible.
526 if (const char *name =
527 getAttributeValueAsString(CU, DW_AT_MIPS_linkage_name, 0))
529 if (const char *name = getAttributeValueAsString(CU, DW_AT_linkage_name, 0))
531 if (const char *name = getAttributeValueAsString(CU, DW_AT_name, 0))
533 // Try to get name from specification DIE.
535 getAttributeValueAsReference(CU, DW_AT_specification, -1U);
536 if (spec_ref != -1U) {
537 DWARFDebugInfoEntryMinimal spec_die;
538 if (spec_die.extract(CU, &spec_ref)) {
539 if (const char *name = spec_die.getSubroutineName(CU))
543 // Try to get name from abstract origin DIE.
544 uint32_t abs_origin_ref =
545 getAttributeValueAsReference(CU, DW_AT_abstract_origin, -1U);
546 if (abs_origin_ref != -1U) {
547 DWARFDebugInfoEntryMinimal abs_origin_die;
548 if (abs_origin_die.extract(CU, &abs_origin_ref)) {
549 if (const char *name = abs_origin_die.getSubroutineName(CU))
556 void DWARFDebugInfoEntryMinimal::getCallerFrame(const DWARFCompileUnit *CU,
559 uint32_t &CallColumn) const {
560 CallFile = getAttributeValueAsUnsigned(CU, DW_AT_call_file, 0);
561 CallLine = getAttributeValueAsUnsigned(CU, DW_AT_call_line, 0);
562 CallColumn = getAttributeValueAsUnsigned(CU, DW_AT_call_column, 0);
565 DWARFDebugInfoEntryMinimal::InlinedChain
566 DWARFDebugInfoEntryMinimal::getInlinedChainForAddress(
567 const DWARFCompileUnit *CU,
568 const uint64_t Address)
570 DWARFDebugInfoEntryMinimal::InlinedChain InlinedChain;
573 for (const DWARFDebugInfoEntryMinimal *DIE = this; DIE; ) {
574 // Append current DIE to inlined chain only if it has correct tag
575 // (e.g. it is not a lexical block).
576 if (DIE->isSubroutineDIE()) {
577 InlinedChain.push_back(*DIE);
579 // Try to get child which also contains provided address.
580 const DWARFDebugInfoEntryMinimal *Child = DIE->getFirstChild();
582 if (Child->addressRangeContainsAddress(CU, Address)) {
583 // Assume there is only one such child.
586 Child = Child->getSibling();
590 // Reverse the obtained chain to make the root of inlined chain last.
591 std::reverse(InlinedChain.begin(), InlinedChain.end());