1 //===-- DWARFDebugPubnames.cpp ----------------------------------*- C++ -*-===//
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 "DWARFDebugPubnames.h"
12 #include "lldb/Core/Stream.h"
13 #include "lldb/Core/Timer.h"
15 #include "DWARFDebugInfo.h"
16 #include "DWARFDIECollection.h"
17 #include "DWARFFormValue.h"
18 #include "DWARFCompileUnit.h"
19 #include "LogChannelDWARF.h"
20 #include "SymbolFileDWARF.h"
24 using namespace lldb_private;
26 DWARFDebugPubnames::DWARFDebugPubnames() :
32 DWARFDebugPubnames::Extract(const DWARFDataExtractor& data)
34 Timer scoped_timer (__PRETTY_FUNCTION__,
35 "DWARFDebugPubnames::Extract (byte_size = %" PRIu64 ")",
36 (uint64_t)data.GetByteSize());
37 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_PUBNAMES));
39 log->Printf("DWARFDebugPubnames::Extract (byte_size = %" PRIu64 ")", (uint64_t)data.GetByteSize());
41 if (data.ValidOffset(0))
43 lldb::offset_t offset = 0;
45 DWARFDebugPubnamesSet set;
46 while (data.ValidOffset(offset))
48 if (set.Extract(data, &offset))
50 m_sets.push_back(set);
51 offset = set.GetOffsetOfNextEntry();
65 DWARFDebugPubnames::GeneratePubnames(SymbolFileDWARF* dwarf2Data)
67 Timer scoped_timer (__PRETTY_FUNCTION__,
68 "DWARFDebugPubnames::GeneratePubnames (data = %p)",
69 static_cast<void*>(dwarf2Data));
71 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_PUBNAMES));
73 log->Printf("DWARFDebugPubnames::GeneratePubnames (data = %p)",
74 static_cast<void*>(dwarf2Data));
77 DWARFDebugInfo* debug_info = dwarf2Data->DebugInfo();
81 const DWARFDataExtractor* debug_str = &dwarf2Data->get_debug_str_data();
84 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
85 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
88 DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx);
90 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (cu->GetAddressByteSize());
92 bool clear_dies = cu->ExtractDIEsIfNeeded (false) > 1;
94 DWARFDIECollection dies;
95 const size_t die_count = cu->AppendDIEsWithTag (DW_TAG_subprogram, dies) +
96 cu->AppendDIEsWithTag (DW_TAG_variable, dies);
98 dw_offset_t cu_offset = cu->GetOffset();
99 DWARFDebugPubnamesSet pubnames_set(DW_INVALID_OFFSET, cu_offset, cu->GetNextCompileUnitOffset() - cu_offset);
102 for (die_idx = 0; die_idx < die_count; ++die_idx)
104 const DWARFDebugInfoEntry *die = dies.GetDIEPtrAtIndex(die_idx);
105 DWARFDebugInfoEntry::Attributes attributes;
106 const char *name = NULL;
107 const char *mangled = NULL;
108 bool add_die = false;
109 const size_t num_attributes = die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes);
110 if (num_attributes > 0)
114 dw_tag_t tag = die->Tag();
116 for (i=0; i<num_attributes; ++i)
118 dw_attr_t attr = attributes.AttributeAtIndex(i);
119 DWARFFormValue form_value;
123 if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
124 name = form_value.AsCString(debug_str);
127 case DW_AT_MIPS_linkage_name:
128 case DW_AT_linkage_name:
129 if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
130 mangled = form_value.AsCString(debug_str);
136 if (tag == DW_TAG_subprogram)
141 if (tag == DW_TAG_variable)
143 const DWARFDebugInfoEntry* parent_die = die->GetParent();
144 while ( parent_die != NULL )
146 switch (parent_die->Tag())
148 case DW_TAG_subprogram:
149 case DW_TAG_lexical_block:
150 case DW_TAG_inlined_subroutine:
151 // Even if this is a function level static, we don't add it. We could theoretically
152 // add these if we wanted to by introspecting into the DW_AT_location and seeing
153 // if the location describes a hard coded address, but we don't want the performance
154 // penalty of that right now.
156 // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
158 // // If we have valid block data, then we have location expression bytes
159 // // that are fixed (not a location list).
160 // const uint8_t *block_data = form_value.BlockData();
163 // uint32_t block_length = form_value.Unsigned();
164 // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
166 // if (block_data[0] == DW_OP_addr)
171 parent_die = NULL; // Terminate the while loop.
174 case DW_TAG_compile_unit:
176 parent_die = NULL; // Terminate the while loop.
180 parent_die = parent_die->GetParent(); // Keep going in the while loop.
190 if (add_die && (name || mangled))
192 pubnames_set.AddDescriptor(die->GetOffset() - cu_offset, mangled ? mangled : name);
196 if (pubnames_set.NumDescriptors() > 0)
198 m_sets.push_back(pubnames_set);
201 // Keep memory down by clearing DIEs if this generate function
202 // caused them to be parsed
204 cu->ClearDIEs (true);
215 DWARFDebugPubnames::GeneratePubBaseTypes(SymbolFileDWARF* dwarf2Data)
218 DWARFDebugInfo* debug_info = dwarf2Data->DebugInfo();
222 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
223 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
225 DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx);
226 DWARFDIECollection dies;
227 const size_t die_count = cu->AppendDIEsWithTag (DW_TAG_base_type, dies);
228 dw_offset_t cu_offset = cu->GetOffset();
229 DWARFDebugPubnamesSet pubnames_set(DW_INVALID_OFFSET, cu_offset, cu->GetNextCompileUnitOffset() - cu_offset);
232 for (die_idx = 0; die_idx < die_count; ++die_idx)
234 const DWARFDebugInfoEntry *die = dies.GetDIEPtrAtIndex(die_idx);
235 const char *name = die->GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, NULL);
239 pubnames_set.AddDescriptor(die->GetOffset() - cu_offset, name);
243 if (pubnames_set.NumDescriptors() > 0)
245 m_sets.push_back(pubnames_set);
249 return !m_sets.empty();
253 DWARFDebugPubnames::Dump(Log *s) const
256 s->PutCString("< EMPTY >\n");
260 const_iterator end = m_sets.end();
262 for (pos = m_sets.begin(); pos != end; ++pos)
268 DWARFDebugPubnames::Find(const char* name, bool ignore_case, std::vector<dw_offset_t>& die_offsets) const
271 const_iterator end = m_sets.end();
275 for (pos = m_sets.begin(); pos != end; ++pos)
277 (*pos).Find(name, ignore_case, die_offsets);
280 return !die_offsets.empty();
284 DWARFDebugPubnames::Find(const RegularExpression& regex, std::vector<dw_offset_t>& die_offsets) const
287 const_iterator end = m_sets.end();
291 for (pos = m_sets.begin(); pos != end; ++pos)
293 (*pos).Find(regex, die_offsets);
296 return !die_offsets.empty();