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)",
71 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_PUBNAMES));
73 log->Printf("DWARFDebugPubnames::GeneratePubnames (data = %p)", dwarf2Data);
76 DWARFDebugInfo* debug_info = dwarf2Data->DebugInfo();
80 const DWARFDataExtractor* debug_str = &dwarf2Data->get_debug_str_data();
83 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
84 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
87 DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx);
89 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (cu->GetAddressByteSize());
91 bool clear_dies = cu->ExtractDIEsIfNeeded (false) > 1;
93 DWARFDIECollection dies;
94 const size_t die_count = cu->AppendDIEsWithTag (DW_TAG_subprogram, dies) +
95 cu->AppendDIEsWithTag (DW_TAG_variable, dies);
97 dw_offset_t cu_offset = cu->GetOffset();
98 DWARFDebugPubnamesSet pubnames_set(DW_INVALID_OFFSET, cu_offset, cu->GetNextCompileUnitOffset() - cu_offset);
101 for (die_idx = 0; die_idx < die_count; ++die_idx)
103 const DWARFDebugInfoEntry *die = dies.GetDIEPtrAtIndex(die_idx);
104 DWARFDebugInfoEntry::Attributes attributes;
105 const char *name = NULL;
106 const char *mangled = NULL;
107 bool add_die = false;
108 const size_t num_attributes = die->GetAttributes(dwarf2Data, cu, fixed_form_sizes, attributes);
109 if (num_attributes > 0)
113 dw_tag_t tag = die->Tag();
115 for (i=0; i<num_attributes; ++i)
117 dw_attr_t attr = attributes.AttributeAtIndex(i);
118 DWARFFormValue form_value;
122 if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
123 name = form_value.AsCString(debug_str);
126 case DW_AT_MIPS_linkage_name:
127 case DW_AT_linkage_name:
128 if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
129 mangled = form_value.AsCString(debug_str);
135 if (tag == DW_TAG_subprogram)
140 if (tag == DW_TAG_variable)
142 const DWARFDebugInfoEntry* parent_die = die->GetParent();
143 while ( parent_die != NULL )
145 switch (parent_die->Tag())
147 case DW_TAG_subprogram:
148 case DW_TAG_lexical_block:
149 case DW_TAG_inlined_subroutine:
150 // Even if this is a function level static, we don't add it. We could theoretically
151 // add these if we wanted to by introspecting into the DW_AT_location and seeing
152 // if the location describes a hard coded address, but we don't want the performance
153 // penalty of that right now.
155 // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
157 // // If we have valid block data, then we have location expression bytes
158 // // that are fixed (not a location list).
159 // const uint8_t *block_data = form_value.BlockData();
162 // uint32_t block_length = form_value.Unsigned();
163 // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
165 // if (block_data[0] == DW_OP_addr)
170 parent_die = NULL; // Terminate the while loop.
173 case DW_TAG_compile_unit:
175 parent_die = NULL; // Terminate the while loop.
179 parent_die = parent_die->GetParent(); // Keep going in the while loop.
189 if (add_die && (name || mangled))
191 pubnames_set.AddDescriptor(die->GetOffset() - cu_offset, mangled ? mangled : name);
195 if (pubnames_set.NumDescriptors() > 0)
197 m_sets.push_back(pubnames_set);
200 // Keep memory down by clearing DIEs if this generate function
201 // caused them to be parsed
203 cu->ClearDIEs (true);
214 DWARFDebugPubnames::GeneratePubBaseTypes(SymbolFileDWARF* dwarf2Data)
217 DWARFDebugInfo* debug_info = dwarf2Data->DebugInfo();
221 const uint32_t num_compile_units = dwarf2Data->GetNumCompileUnits();
222 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
224 DWARFCompileUnit* cu = debug_info->GetCompileUnitAtIndex(cu_idx);
225 DWARFDIECollection dies;
226 const size_t die_count = cu->AppendDIEsWithTag (DW_TAG_base_type, dies);
227 dw_offset_t cu_offset = cu->GetOffset();
228 DWARFDebugPubnamesSet pubnames_set(DW_INVALID_OFFSET, cu_offset, cu->GetNextCompileUnitOffset() - cu_offset);
231 for (die_idx = 0; die_idx < die_count; ++die_idx)
233 const DWARFDebugInfoEntry *die = dies.GetDIEPtrAtIndex(die_idx);
234 const char *name = die->GetAttributeValueAsString(dwarf2Data, cu, DW_AT_name, NULL);
238 pubnames_set.AddDescriptor(die->GetOffset() - cu_offset, name);
242 if (pubnames_set.NumDescriptors() > 0)
244 m_sets.push_back(pubnames_set);
248 return !m_sets.empty();
252 DWARFDebugPubnames::Dump(Log *s) const
255 s->PutCString("< EMPTY >\n");
259 const_iterator end = m_sets.end();
261 for (pos = m_sets.begin(); pos != end; ++pos)
267 DWARFDebugPubnames::Find(const char* name, bool ignore_case, std::vector<dw_offset_t>& die_offsets) const
270 const_iterator end = m_sets.end();
274 for (pos = m_sets.begin(); pos != end; ++pos)
276 (*pos).Find(name, ignore_case, die_offsets);
279 return !die_offsets.empty();
283 DWARFDebugPubnames::Find(const RegularExpression& regex, std::vector<dw_offset_t>& die_offsets) const
286 const_iterator end = m_sets.end();
290 for (pos = m_sets.begin(); pos != end; ++pos)
292 (*pos).Find(regex, die_offsets);
295 return !die_offsets.empty();