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 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
31 OS.indent(indent) << format("\n0x%8.8x: ", Offset);
34 OS << TagString(AbbrevDecl->getTag())
35 << format(" [%u] %c\n", abbrCode,
36 AbbrevDecl->hasChildren() ? '*': ' ');
38 // Dump all data in the .debug_info for the attributes
39 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
40 for (uint32_t i = 0; i != numAttributes; ++i) {
41 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
42 uint16_t form = AbbrevDecl->getFormByIndex(i);
43 dumpAttribute(OS, cu, &offset, attr, form, indent);
46 const DWARFDebugInfoEntryMinimal *child = getFirstChild();
47 if (recurseDepth > 0 && child) {
49 child->dump(OS, cu, recurseDepth-1, indent+2);
50 child = child->getSibling();
54 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
63 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
64 const DWARFCompileUnit *cu,
68 unsigned indent) const {
69 OS.indent(indent) << format("0x%8.8x: ", *offset_ptr)
70 << AttributeString(attr)
71 << " [" << FormEncodingString(form) << ']';
73 DWARFFormValue formValue(form);
75 if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu))
79 formValue.dump(OS, 0, cu);
83 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *cu,
84 const uint8_t *fixed_form_sizes,
85 uint32_t *offset_ptr) {
88 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
89 uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr);
91 assert (fixed_form_sizes); // For best performance this should be specified!
94 uint32_t offset = *offset_ptr;
96 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
98 // Skip all data in the .debug_info for the attributes
99 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
102 for (i=0; i<numAttributes; ++i) {
103 form = AbbrevDecl->getFormByIndex(i);
105 const uint8_t fixed_skip_size = fixed_form_sizes[form];
107 offset += fixed_skip_size;
109 bool form_is_indirect = false;
111 form_is_indirect = false;
112 uint32_t form_size = 0;
114 // Blocks if inlined data that have a length field and the data bytes
115 // inlined in the .debug_info.
117 form_size = debug_info_data.getULEB128(&offset);
120 form_size = debug_info_data.getU8(&offset);
123 form_size = debug_info_data.getU16(&offset);
126 form_size = debug_info_data.getU32(&offset);
129 // Inlined NULL terminated C-strings
131 debug_info_data.getCStr(&offset);
134 // Compile unit address sized values
136 case DW_FORM_ref_addr:
137 form_size = cu->getAddressByteSize();
166 // signed or unsigned LEB 128 values
169 case DW_FORM_ref_udata:
170 debug_info_data.getULEB128(&offset);
173 case DW_FORM_indirect:
174 form_is_indirect = true;
175 form = debug_info_data.getULEB128(&offset);
179 *offset_ptr = Offset;
184 } while (form_is_indirect);
187 *offset_ptr = offset;
191 return true; // NULL debug tag entry
198 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
199 uint32_t *offset_ptr) {
200 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
201 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
202 const uint8_t cu_addr_size = cu->getAddressByteSize();
203 uint32_t offset = *offset_ptr;
204 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
207 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
210 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
213 uint16_t tag = AbbrevDecl->getTag();
215 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
216 if(cu && isCompileUnitTag)
217 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
219 // Skip all data in the .debug_info for the attributes
220 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
221 for (uint32_t i = 0; i != numAttributes; ++i) {
222 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
223 uint16_t form = AbbrevDecl->getFormByIndex(i);
225 if (isCompileUnitTag &&
226 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
227 DWARFFormValue form_value(form);
228 if (form_value.extractValue(debug_info_data, &offset, cu)) {
229 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
230 const_cast<DWARFCompileUnit*>(cu)
231 ->setBaseAddress(form_value.getUnsigned());
234 bool form_is_indirect = false;
236 form_is_indirect = false;
237 register uint32_t form_size = 0;
239 // Blocks if inlined data that have a length field and the data
240 // bytes // inlined in the .debug_info
242 form_size = debug_info_data.getULEB128(&offset);
245 form_size = debug_info_data.getU8(&offset);
248 form_size = debug_info_data.getU16(&offset);
251 form_size = debug_info_data.getU32(&offset);
254 // Inlined NULL terminated C-strings
256 debug_info_data.getCStr(&offset);
259 // Compile unit address sized values
261 case DW_FORM_ref_addr:
262 form_size = cu_addr_size;
294 // signed or unsigned LEB 128 values
297 case DW_FORM_ref_udata:
298 debug_info_data.getULEB128(&offset);
301 case DW_FORM_indirect:
302 form = debug_info_data.getULEB128(&offset);
303 form_is_indirect = true;
307 *offset_ptr = offset;
312 } while (form_is_indirect);
315 *offset_ptr = offset;
320 *offset_ptr = offset;
321 return true; // NULL debug tag entry
329 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
331 DWARFFormValue &form_value,
332 uint32_t *end_attr_offset_ptr)
335 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
337 if (attr_idx != -1U) {
338 uint32_t offset = getOffset();
340 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
342 // Skip the abbreviation code so we are at the data for the attributes
343 debug_info_data.getULEB128(&offset);
346 while (idx < attr_idx)
347 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
348 debug_info_data, &offset, cu);
350 const uint32_t attr_offset = offset;
351 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
352 if (form_value.extractValue(debug_info_data, &offset, cu)) {
353 if (end_attr_offset_ptr)
354 *end_attr_offset_ptr = offset;
364 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
365 const DWARFCompileUnit* cu,
367 const char* fail_value) const {
368 DWARFFormValue form_value;
369 if (getAttributeValue(cu, attr, form_value)) {
370 DataExtractor stringExtractor(cu->getContext().getStringSection(),
372 return form_value.getAsCString(&stringExtractor);
378 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
379 const DWARFCompileUnit* cu,
381 uint64_t fail_value) const {
382 DWARFFormValue form_value;
383 if (getAttributeValue(cu, attr, form_value))
384 return form_value.getUnsigned();
389 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
390 const DWARFCompileUnit* cu,
392 int64_t fail_value) const {
393 DWARFFormValue form_value;
394 if (getAttributeValue(cu, attr, form_value))
395 return form_value.getSigned();
400 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(const DWARFCompileUnit* cu,
402 uint64_t fail_value) const {
403 DWARFFormValue form_value;
404 if (getAttributeValue(cu, attr, form_value))
405 return form_value.getReference(cu);