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) {
50 child->dump(OS, cu, recurseDepth-1, indent);
51 child = child->getSibling();
56 OS << "Abbreviation code not found in 'debug_abbrev' class for code: "
65 void DWARFDebugInfoEntryMinimal::dumpAttribute(raw_ostream &OS,
66 const DWARFCompileUnit *cu,
70 unsigned indent) const {
71 OS.indent(indent) << format("0x%8.8x: ", *offset_ptr)
72 << AttributeString(attr)
73 << " [" << FormEncodingString(form) << ']';
75 DWARFFormValue formValue(form);
77 if (!formValue.extractValue(cu->getDebugInfoExtractor(), offset_ptr, cu))
81 formValue.dump(OS, 0, cu);
85 bool DWARFDebugInfoEntryMinimal::extractFast(const DWARFCompileUnit *cu,
86 const uint8_t *fixed_form_sizes,
87 uint32_t *offset_ptr) {
90 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
91 uint64_t abbrCode = debug_info_data.getULEB128(offset_ptr);
93 assert (fixed_form_sizes); // For best performance this should be specified!
96 uint32_t offset = *offset_ptr;
98 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
100 // Skip all data in the .debug_info for the attributes
101 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
104 for (i=0; i<numAttributes; ++i) {
105 form = AbbrevDecl->getFormByIndex(i);
107 const uint8_t fixed_skip_size = fixed_form_sizes[form];
109 offset += fixed_skip_size;
111 bool form_is_indirect = false;
113 form_is_indirect = false;
114 uint32_t form_size = 0;
116 // Blocks if inlined data that have a length field and the data bytes
117 // inlined in the .debug_info.
119 form_size = debug_info_data.getULEB128(&offset);
122 form_size = debug_info_data.getU8(&offset);
125 form_size = debug_info_data.getU16(&offset);
128 form_size = debug_info_data.getU32(&offset);
131 // Inlined NULL terminated C-strings
133 debug_info_data.getCStr(&offset);
136 // Compile unit address sized values
138 case DW_FORM_ref_addr:
139 form_size = cu->getAddressByteSize();
168 // signed or unsigned LEB 128 values
171 case DW_FORM_ref_udata:
172 debug_info_data.getULEB128(&offset);
175 case DW_FORM_indirect:
176 form_is_indirect = true;
177 form = debug_info_data.getULEB128(&offset);
181 *offset_ptr = Offset;
186 } while (form_is_indirect);
189 *offset_ptr = offset;
193 return true; // NULL debug tag entry
200 DWARFDebugInfoEntryMinimal::extract(const DWARFCompileUnit *cu,
201 uint32_t *offset_ptr) {
202 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
203 const uint32_t cu_end_offset = cu->getNextCompileUnitOffset();
204 const uint8_t cu_addr_size = cu->getAddressByteSize();
205 uint32_t offset = *offset_ptr;
206 if ((offset < cu_end_offset) && debug_info_data.isValidOffset(offset)) {
209 uint64_t abbrCode = debug_info_data.getULEB128(&offset);
212 AbbrevDecl = cu->getAbbreviations()->getAbbreviationDeclaration(abbrCode);
215 uint16_t tag = AbbrevDecl->getTag();
217 bool isCompileUnitTag = tag == DW_TAG_compile_unit;
218 if(cu && isCompileUnitTag)
219 const_cast<DWARFCompileUnit*>(cu)->setBaseAddress(0);
221 // Skip all data in the .debug_info for the attributes
222 const uint32_t numAttributes = AbbrevDecl->getNumAttributes();
223 for (uint32_t i = 0; i != numAttributes; ++i) {
224 uint16_t attr = AbbrevDecl->getAttrByIndex(i);
225 uint16_t form = AbbrevDecl->getFormByIndex(i);
227 if (isCompileUnitTag &&
228 ((attr == DW_AT_entry_pc) || (attr == DW_AT_low_pc))) {
229 DWARFFormValue form_value(form);
230 if (form_value.extractValue(debug_info_data, &offset, cu)) {
231 if (attr == DW_AT_low_pc || attr == DW_AT_entry_pc)
232 const_cast<DWARFCompileUnit*>(cu)
233 ->setBaseAddress(form_value.getUnsigned());
236 bool form_is_indirect = false;
238 form_is_indirect = false;
239 register uint32_t form_size = 0;
241 // Blocks if inlined data that have a length field and the data
242 // bytes // inlined in the .debug_info
244 form_size = debug_info_data.getULEB128(&offset);
247 form_size = debug_info_data.getU8(&offset);
250 form_size = debug_info_data.getU16(&offset);
253 form_size = debug_info_data.getU32(&offset);
256 // Inlined NULL terminated C-strings
258 debug_info_data.getCStr(&offset);
261 // Compile unit address sized values
263 case DW_FORM_ref_addr:
264 form_size = cu_addr_size;
296 // signed or unsigned LEB 128 values
299 case DW_FORM_ref_udata:
300 debug_info_data.getULEB128(&offset);
303 case DW_FORM_indirect:
304 form = debug_info_data.getULEB128(&offset);
305 form_is_indirect = true;
309 *offset_ptr = offset;
314 } while (form_is_indirect);
317 *offset_ptr = offset;
322 *offset_ptr = offset;
323 return true; // NULL debug tag entry
331 DWARFDebugInfoEntryMinimal::getAttributeValue(const DWARFCompileUnit *cu,
333 DWARFFormValue &form_value,
334 uint32_t *end_attr_offset_ptr)
337 uint32_t attr_idx = AbbrevDecl->findAttributeIndex(attr);
339 if (attr_idx != -1U) {
340 uint32_t offset = getOffset();
342 DataExtractor debug_info_data = cu->getDebugInfoExtractor();
344 // Skip the abbreviation code so we are at the data for the attributes
345 debug_info_data.getULEB128(&offset);
348 while (idx < attr_idx)
349 DWARFFormValue::skipValue(AbbrevDecl->getFormByIndex(idx++),
350 debug_info_data, &offset, cu);
352 const uint32_t attr_offset = offset;
353 form_value = DWARFFormValue(AbbrevDecl->getFormByIndex(idx));
354 if (form_value.extractValue(debug_info_data, &offset, cu)) {
355 if (end_attr_offset_ptr)
356 *end_attr_offset_ptr = offset;
366 DWARFDebugInfoEntryMinimal::getAttributeValueAsString(
367 const DWARFCompileUnit* cu,
369 const char* fail_value) const {
370 DWARFFormValue form_value;
371 if (getAttributeValue(cu, attr, form_value)) {
372 DataExtractor stringExtractor(cu->getContext().getStringSection(),
374 return form_value.getAsCString(&stringExtractor);
380 DWARFDebugInfoEntryMinimal::getAttributeValueAsUnsigned(
381 const DWARFCompileUnit* cu,
383 uint64_t fail_value) const {
384 DWARFFormValue form_value;
385 if (getAttributeValue(cu, attr, form_value))
386 return form_value.getUnsigned();
391 DWARFDebugInfoEntryMinimal::getAttributeValueAsSigned(
392 const DWARFCompileUnit* cu,
394 int64_t fail_value) const {
395 DWARFFormValue form_value;
396 if (getAttributeValue(cu, attr, form_value))
397 return form_value.getSigned();
402 DWARFDebugInfoEntryMinimal::getAttributeValueAsReference(const DWARFCompileUnit* cu,
404 uint64_t fail_value) const {
405 DWARFFormValue form_value;
406 if (getAttributeValue(cu, attr, form_value))
407 return form_value.getReference(cu);