1 //===-- DWARFCompileUnit.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 "DWARFCompileUnit.h"
12 #include "Plugins/Language/ObjC/ObjCLanguage.h"
13 #include "lldb/Core/DumpDataExtractor.h"
14 #include "lldb/Core/Mangled.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Host/StringConvert.h"
17 #include "lldb/Symbol/CompileUnit.h"
18 #include "lldb/Symbol/LineTable.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Utility/Stream.h"
21 #include "lldb/Utility/StreamString.h"
22 #include "lldb/Utility/Timer.h"
24 #include "DWARFDIECollection.h"
25 #include "DWARFDebugAbbrev.h"
26 #include "DWARFDebugAranges.h"
27 #include "DWARFDebugInfo.h"
28 #include "DWARFFormValue.h"
29 #include "LogChannelDWARF.h"
30 #include "NameToDIE.h"
31 #include "SymbolFileDWARF.h"
32 #include "SymbolFileDWARFDebugMap.h"
33 #include "SymbolFileDWARFDwo.h"
36 using namespace lldb_private;
41 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF *dwarf2Data)
42 : m_dwarf2Data(dwarf2Data) {}
44 DWARFCompileUnit::~DWARFCompileUnit() {}
46 DWARFCompileUnitSP DWARFCompileUnit::Extract(SymbolFileDWARF *dwarf2Data,
47 lldb::offset_t *offset_ptr) {
48 DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(dwarf2Data));
50 if (cu_sp.get() == NULL)
53 const DWARFDataExtractor &debug_info = dwarf2Data->get_debug_info_data();
55 cu_sp->m_offset = *offset_ptr;
57 if (debug_info.ValidOffset(*offset_ptr)) {
58 dw_offset_t abbr_offset;
59 const DWARFDebugAbbrev *abbr = dwarf2Data->DebugAbbrev();
60 cu_sp->m_length = debug_info.GetDWARFInitialLength(offset_ptr);
61 cu_sp->m_is_dwarf64 = debug_info.IsDWARF64();
62 cu_sp->m_version = debug_info.GetU16(offset_ptr);
63 abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
64 cu_sp->m_addr_size = debug_info.GetU8(offset_ptr);
67 debug_info.ValidOffset(cu_sp->GetNextCompileUnitOffset() - 1);
68 bool version_OK = SymbolFileDWARF::SupportedVersion(cu_sp->m_version);
70 dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
71 bool addr_size_OK = (cu_sp->m_addr_size == 4) || (cu_sp->m_addr_size == 8);
73 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK &&
75 cu_sp->m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
79 // reset the offset to where we tried to parse from if anything went wrong
80 *offset_ptr = cu_sp->m_offset;
86 void DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die) {
87 if (m_die_array.size() > 1) {
88 // std::vectors never get any smaller when resized to a smaller size,
89 // or when clear() or erase() are called, the size will report that it
90 // is smaller, but the memory allocated remains intact (call capacity()
91 // to see this). So we need to create a temporary vector and swap the
92 // contents which will cause just the internal pointers to be swapped
93 // so that when "tmp_array" goes out of scope, it will destroy the
96 // Save at least the compile unit DIE
97 DWARFDebugInfoEntry::collection tmp_array;
98 m_die_array.swap(tmp_array);
99 if (keep_compile_unit_die)
100 m_die_array.push_back(tmp_array.front());
103 if (m_dwo_symbol_file)
104 m_dwo_symbol_file->GetCompileUnit()->ClearDIEs(keep_compile_unit_die);
107 //----------------------------------------------------------------------
108 // ParseCompileUnitDIEsIfNeeded
110 // Parses a compile unit and indexes its DIEs if it hasn't already been
112 //----------------------------------------------------------------------
113 size_t DWARFCompileUnit::ExtractDIEsIfNeeded(bool cu_die_only) {
114 const size_t initial_die_array_size = m_die_array.size();
115 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
116 return 0; // Already parsed
118 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
121 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
122 m_offset, cu_die_only);
124 // Set the offset to that of the first DIE and calculate the start of the
125 // next compilation unit header.
126 lldb::offset_t offset = GetFirstDIEOffset();
127 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
129 DWARFDebugInfoEntry die;
130 // Keep a flat array of the DIE for binary lookup by DIE offset
133 LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
135 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
136 log, "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at "
137 ".debug_info[0x%8.8x]",
143 // We are in our compile unit, parse starting at the offset
144 // we were told to parse
145 const DWARFDataExtractor &debug_info_data =
146 m_dwarf2Data->get_debug_info_data();
147 std::vector<uint32_t> die_index_stack;
148 die_index_stack.reserve(32);
149 die_index_stack.push_back(0);
150 bool prev_die_had_children = false;
151 DWARFFormValue::FixedFormSizes fixed_form_sizes =
152 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
154 while (offset < next_cu_offset &&
155 die.FastExtract(debug_info_data, this, fixed_form_sizes, &offset)) {
157 // log->Printf("0x%8.8x: %*.*s%s%s",
159 // depth * 2, depth * 2, "",
160 // DW_TAG_value_to_name (die.Tag()),
161 // die.HasChildren() ? " *" : "");
163 const bool null_die = die.IsNULL();
165 if (initial_die_array_size == 0)
166 AddCompileUnitDIE(die);
167 uint64_t base_addr = die.GetAttributeValueAsAddress(
168 m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
169 if (base_addr == LLDB_INVALID_ADDRESS)
170 base_addr = die.GetAttributeValueAsAddress(m_dwarf2Data, this,
172 SetBaseAddress(base_addr);
177 if (prev_die_had_children) {
178 // This will only happen if a DIE says is has children
179 // but all it contains is a NULL tag. Since we are removing
180 // the NULL DIEs from the list (saves up to 25% in C++ code),
181 // we need a way to let the DIE know that it actually doesn't
183 if (!m_die_array.empty())
184 m_die_array.back().SetEmptyChildren(true);
187 die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
189 if (die_index_stack.back())
190 m_die_array[die_index_stack.back()].SetSiblingIndex(
191 m_die_array.size() - die_index_stack.back());
193 // Only push the DIE if it isn't a NULL DIE
194 m_die_array.push_back(die);
200 if (!die_index_stack.empty())
201 die_index_stack.pop_back();
206 break; // We are done with this compile unit!
208 prev_die_had_children = false;
210 die_index_stack.back() = m_die_array.size() - 1;
212 const bool die_has_children = die.HasChildren();
213 if (die_has_children) {
214 die_index_stack.push_back(0);
217 prev_die_had_children = die_has_children;
221 // Give a little bit of info if we encounter corrupt DWARF (our offset
222 // should always terminate at or before the start of the next compilation
224 if (offset > next_cu_offset) {
225 m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning(
226 "DWARF compile unit extends beyond its bounds cu 0x%8.8x at "
227 "0x%8.8" PRIx64 "\n",
228 GetOffset(), offset);
231 // Since std::vector objects will double their size, we really need to
232 // make a new array with the perfect size so we don't end up wasting
233 // space. So here we copy and swap to make sure we don't have any extra
236 if (m_die_array.size() < m_die_array.capacity()) {
237 DWARFDebugInfoEntry::collection exact_size_die_array(m_die_array.begin(),
239 exact_size_die_array.swap(m_die_array);
241 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
242 if (log && log->GetVerbose()) {
245 if (m_die_array.empty())
246 strm.Printf("error: no DIE for compile unit");
248 m_die_array[0].Dump(m_dwarf2Data, this, strm, UINT32_MAX);
249 log->PutString(strm.GetString());
252 if (!m_dwo_symbol_file)
253 return m_die_array.size();
255 DWARFCompileUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
256 size_t dwo_die_count = dwo_cu->ExtractDIEsIfNeeded(cu_die_only);
257 return m_die_array.size() + dwo_die_count -
258 1; // We have 2 CU die, but we want to count it only as one
261 void DWARFCompileUnit::AddCompileUnitDIE(DWARFDebugInfoEntry &die) {
262 assert(m_die_array.empty() && "Compile unit DIE already added");
265 const DWARFDebugInfoEntry &cu_die = m_die_array.front();
266 std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
267 m_dwarf2Data->GetDwoSymbolFileForCompileUnit(*this, cu_die);
268 if (!dwo_symbol_file)
271 DWARFCompileUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
273 return; // Can't fetch the compile unit from the dwo file.
275 DWARFDIE dwo_cu_die = dwo_cu->GetCompileUnitDIEOnly();
276 if (!dwo_cu_die.IsValid())
277 return; // Can't fetch the compile unit DIE from the dwo file.
279 uint64_t main_dwo_id = cu_die.GetAttributeValueAsUnsigned(
280 m_dwarf2Data, this, DW_AT_GNU_dwo_id, 0);
281 uint64_t sub_dwo_id =
282 dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0);
283 if (main_dwo_id != sub_dwo_id)
284 return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to
285 // a differectn compilation.
287 m_dwo_symbol_file = std::move(dwo_symbol_file);
289 dw_addr_t addr_base = cu_die.GetAttributeValueAsUnsigned(
290 m_dwarf2Data, this, DW_AT_GNU_addr_base, 0);
291 dw_addr_t ranges_base = cu_die.GetAttributeValueAsUnsigned(
292 m_dwarf2Data, this, DW_AT_GNU_ranges_base, 0);
293 dwo_cu->SetAddrBase(addr_base, ranges_base, m_offset);
296 dw_offset_t DWARFCompileUnit::GetAbbrevOffset() const {
297 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
300 bool DWARFCompileUnit::Verify(Stream *s) const {
301 const DWARFDataExtractor &debug_info = m_dwarf2Data->get_debug_info_data();
302 bool valid_offset = debug_info.ValidOffset(m_offset);
303 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset() - 1);
304 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
305 bool abbr_offset_OK =
306 m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
307 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
308 if (valid_offset && length_OK && version_OK && addr_size_OK &&
312 s->Printf(" 0x%8.8x: ", m_offset);
313 DumpDataExtractor(m_dwarf2Data->get_debug_info_data(), s, m_offset,
314 lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0,
319 s->Printf(" The length (0x%8.8x) for this compile unit is too "
320 "large for the .debug_info provided.\n",
323 s->Printf(" The 16 bit compile unit header version is not "
326 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) "
330 s->Printf(" The address size is unsupported: 0x%2.2x\n",
333 s->Printf(" The start offset of the compile unit header in the "
334 ".debug_info is invalid.\n");
339 void DWARFCompileUnit::Dump(Stream *s) const {
340 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, "
341 "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at "
343 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size,
344 GetNextCompileUnitOffset());
347 static uint8_t g_default_addr_size = 4;
349 uint8_t DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit *cu) {
351 return cu->GetAddressByteSize();
352 return DWARFCompileUnit::GetDefaultAddressSize();
355 bool DWARFCompileUnit::IsDWARF64(const DWARFCompileUnit *cu) {
357 return cu->IsDWARF64();
361 uint8_t DWARFCompileUnit::GetDefaultAddressSize() {
362 return g_default_addr_size;
365 void DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size) {
366 g_default_addr_size = addr_size;
369 lldb::user_id_t DWARFCompileUnit::GetID() const {
370 dw_offset_t local_id =
371 m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset;
373 return DIERef(local_id, local_id).GetUID(m_dwarf2Data);
378 void DWARFCompileUnit::BuildAddressRangeTable(
379 SymbolFileDWARF *dwarf2Data, DWARFDebugAranges *debug_aranges) {
380 // This function is usually called if there in no .debug_aranges section
381 // in order to produce a compile unit level set of address ranges that
384 size_t num_debug_aranges = debug_aranges->GetNumRanges();
386 // First get the compile unit DIE only and check if it has a DW_AT_ranges
387 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
389 const dw_offset_t cu_offset = GetOffset();
391 DWARFRangeList ranges;
392 const size_t num_ranges =
393 die->GetAttributeAddressRanges(dwarf2Data, this, ranges, false);
394 if (num_ranges > 0) {
395 // This compile unit has DW_AT_ranges, assume this is correct if it
396 // is present since clang no longer makes .debug_aranges by default
397 // and it emits DW_AT_ranges for DW_TAG_compile_units. GCC also does
398 // this with recent GCC builds.
399 for (size_t i = 0; i < num_ranges; ++i) {
400 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
401 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
402 range.GetRangeEnd());
405 return; // We got all of our ranges from the DW_AT_ranges attribute
408 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF
410 // If the DIEs weren't parsed, then we don't want all dies for all compile
412 // to stay loaded when they weren't needed. So we can end up parsing the DWARF
413 // and then throwing them all away to keep memory usage down.
414 const bool clear_dies = ExtractDIEsIfNeeded(false) > 1;
418 die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges);
420 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
421 // We got nothing from the functions, maybe we have a line tables only
422 // situation. Check the line tables and build the arange table from this.
424 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
426 SymbolFileDWARFDebugMap *debug_map_sym_file =
427 m_dwarf2Data->GetDebugMapSymfile();
428 if (debug_map_sym_file == NULL) {
429 LineTable *line_table = sc.comp_unit->GetLineTable();
432 LineTable::FileAddressRanges file_ranges;
433 const bool append = true;
434 const size_t num_ranges =
435 line_table->GetContiguousFileAddressRanges(file_ranges, append);
436 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
437 const LineTable::FileAddressRanges::Entry &range =
438 file_ranges.GetEntryRef(idx);
439 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
440 range.GetRangeEnd());
444 debug_map_sym_file->AddOSOARanges(dwarf2Data, debug_aranges);
448 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
449 // We got nothing from the functions, maybe we have a line tables only
450 // situation. Check the line tables and build the arange table from this.
452 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
454 LineTable *line_table = sc.comp_unit->GetLineTable();
457 LineTable::FileAddressRanges file_ranges;
458 const bool append = true;
459 const size_t num_ranges =
460 line_table->GetContiguousFileAddressRanges(file_ranges, append);
461 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
462 const LineTable::FileAddressRanges::Entry &range =
463 file_ranges.GetEntryRef(idx);
464 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
465 range.GetRangeEnd());
471 // Keep memory down by clearing DIEs if this generate function
472 // caused them to be parsed
477 const DWARFDebugAranges &DWARFCompileUnit::GetFunctionAranges() {
478 if (m_func_aranges_ap.get() == NULL) {
479 m_func_aranges_ap.reset(new DWARFDebugAranges());
480 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
483 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage(
484 log, "DWARFCompileUnit::GetFunctionAranges() for compile unit at "
485 ".debug_info[0x%8.8x]",
488 const DWARFDebugInfoEntry *die = DIEPtr();
490 die->BuildFunctionAddressRangeTable(m_dwarf2Data, this,
491 m_func_aranges_ap.get());
493 if (m_dwo_symbol_file) {
494 DWARFCompileUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
495 const DWARFDebugInfoEntry *dwo_die = dwo_cu->DIEPtr();
497 dwo_die->BuildFunctionAddressRangeTable(m_dwo_symbol_file.get(), dwo_cu,
498 m_func_aranges_ap.get());
501 const bool minimize = false;
502 m_func_aranges_ap->Sort(minimize);
504 return *m_func_aranges_ap.get();
508 DWARFCompileUnit::LookupAddress(const dw_addr_t address) {
510 const DWARFDebugAranges &func_aranges = GetFunctionAranges();
512 // Re-check the aranges auto pointer contents in case it was created above
513 if (!func_aranges.IsEmpty())
514 return GetDIE(func_aranges.FindAddress(address));
519 //----------------------------------------------------------------------
520 // Compare function DWARFDebugAranges::Range structures
521 //----------------------------------------------------------------------
522 static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
523 const dw_offset_t die_offset) {
524 return die.GetOffset() < die_offset;
527 //----------------------------------------------------------------------
530 // Get the DIE (Debug Information Entry) with the specified offset by
531 // first checking if the DIE is contained within this compile unit and
532 // grabbing the DIE from this compile unit. Otherwise we grab the DIE
533 // from the DWARF file.
534 //----------------------------------------------------------------------
536 DWARFCompileUnit::GetDIE(dw_offset_t die_offset) {
537 if (die_offset != DW_INVALID_OFFSET) {
538 if (m_dwo_symbol_file)
539 return m_dwo_symbol_file->GetCompileUnit()->GetDIE(die_offset);
541 if (ContainsDIEOffset(die_offset)) {
542 ExtractDIEsIfNeeded(false);
543 DWARFDebugInfoEntry::iterator end = m_die_array.end();
544 DWARFDebugInfoEntry::iterator pos =
545 lower_bound(m_die_array.begin(), end, die_offset, CompareDIEOffset);
547 if (die_offset == (*pos).GetOffset())
548 return DWARFDIE(this, &(*pos));
551 // Don't specify the compile unit offset as we don't know it because the
553 // a different compile unit in the same symbol file.
554 return m_dwarf2Data->DebugInfo()->GetDIEForDIEOffset(die_offset);
557 return DWARFDIE(); // Not found
560 size_t DWARFCompileUnit::AppendDIEsWithTag(const dw_tag_t tag,
561 DWARFDIECollection &dies,
562 uint32_t depth) const {
563 size_t old_size = dies.Size();
564 DWARFDebugInfoEntry::const_iterator pos;
565 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
566 for (pos = m_die_array.begin(); pos != end; ++pos) {
567 if (pos->Tag() == tag)
568 dies.Append(DWARFDIE(this, &(*pos)));
571 // Return the number of DIEs added to the collection
572 return dies.Size() - old_size;
576 // DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
578 // m_global_die_indexes.push_back (die_idx);
583 // DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
585 // // Indexes to all file level global and static variables
586 // m_global_die_indexes;
588 // if (m_die_array.empty())
591 // const DWARFDebugInfoEntry* first_die = &m_die_array[0];
592 // const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
593 // if (first_die <= die && die < end)
594 // m_global_die_indexes.push_back (die - first_die);
597 void DWARFCompileUnit::Index(NameToDIE &func_basenames,
598 NameToDIE &func_fullnames, NameToDIE &func_methods,
599 NameToDIE &func_selectors,
600 NameToDIE &objc_class_selectors,
601 NameToDIE &globals, NameToDIE &types,
602 NameToDIE &namespaces) {
603 assert(!m_dwarf2Data->GetBaseCompileUnit() &&
604 "DWARFCompileUnit associated with .dwo or .dwp "
605 "should not be indexed directly");
607 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
610 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage(
612 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]",
616 const LanguageType cu_language = GetLanguageType();
617 DWARFFormValue::FixedFormSizes fixed_form_sizes =
618 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
621 IndexPrivate(this, cu_language, fixed_form_sizes, GetOffset(), func_basenames,
622 func_fullnames, func_methods, func_selectors,
623 objc_class_selectors, globals, types, namespaces);
625 SymbolFileDWARFDwo *dwo_symbol_file = GetDwoSymbolFile();
626 if (dwo_symbol_file) {
627 IndexPrivate(dwo_symbol_file->GetCompileUnit(), cu_language,
628 fixed_form_sizes, GetOffset(), func_basenames, func_fullnames,
629 func_methods, func_selectors, objc_class_selectors, globals,
634 void DWARFCompileUnit::IndexPrivate(
635 DWARFCompileUnit *dwarf_cu, const LanguageType cu_language,
636 const DWARFFormValue::FixedFormSizes &fixed_form_sizes,
637 const dw_offset_t cu_offset, NameToDIE &func_basenames,
638 NameToDIE &func_fullnames, NameToDIE &func_methods,
639 NameToDIE &func_selectors, NameToDIE &objc_class_selectors,
640 NameToDIE &globals, NameToDIE &types, NameToDIE &namespaces) {
641 DWARFDebugInfoEntry::const_iterator pos;
642 DWARFDebugInfoEntry::const_iterator begin = dwarf_cu->m_die_array.begin();
643 DWARFDebugInfoEntry::const_iterator end = dwarf_cu->m_die_array.end();
644 for (pos = begin; pos != end; ++pos) {
645 const DWARFDebugInfoEntry &die = *pos;
647 const dw_tag_t tag = die.Tag();
650 case DW_TAG_array_type:
651 case DW_TAG_base_type:
652 case DW_TAG_class_type:
653 case DW_TAG_constant:
654 case DW_TAG_enumeration_type:
655 case DW_TAG_inlined_subroutine:
656 case DW_TAG_namespace:
657 case DW_TAG_string_type:
658 case DW_TAG_structure_type:
659 case DW_TAG_subprogram:
660 case DW_TAG_subroutine_type:
662 case DW_TAG_union_type:
663 case DW_TAG_unspecified_type:
664 case DW_TAG_variable:
671 DWARFAttributes attributes;
672 const char *name = NULL;
673 const char *mangled_cstr = NULL;
674 bool is_declaration = false;
675 // bool is_artificial = false;
676 bool has_address = false;
677 bool has_location_or_const_value = false;
678 bool is_global_or_static_variable = false;
680 DWARFFormValue specification_die_form;
681 const size_t num_attributes =
682 die.GetAttributes(dwarf_cu, fixed_form_sizes, attributes);
683 if (num_attributes > 0) {
684 for (uint32_t i = 0; i < num_attributes; ++i) {
685 dw_attr_t attr = attributes.AttributeAtIndex(i);
686 DWARFFormValue form_value;
689 if (attributes.ExtractFormValueAtIndex(i, form_value))
690 name = form_value.AsCString();
693 case DW_AT_declaration:
694 if (attributes.ExtractFormValueAtIndex(i, form_value))
695 is_declaration = form_value.Unsigned() != 0;
698 // case DW_AT_artificial:
699 // if (attributes.ExtractFormValueAtIndex(i,
701 // is_artificial = form_value.Unsigned() != 0;
704 case DW_AT_MIPS_linkage_name:
705 case DW_AT_linkage_name:
706 if (attributes.ExtractFormValueAtIndex(i, form_value))
707 mangled_cstr = form_value.AsCString();
721 case DW_AT_const_value:
722 has_location_or_const_value = true;
723 if (tag == DW_TAG_variable) {
724 const DWARFDebugInfoEntry *parent_die = die.GetParent();
725 while (parent_die != NULL) {
726 switch (parent_die->Tag()) {
727 case DW_TAG_subprogram:
728 case DW_TAG_lexical_block:
729 case DW_TAG_inlined_subroutine:
730 // Even if this is a function level static, we don't add it. We
731 // could theoretically
732 // add these if we wanted to by introspecting into the
733 // DW_AT_location and seeing
734 // if the location describes a hard coded address, but we dont
735 // want the performance
736 // penalty of that right now.
737 is_global_or_static_variable = false;
739 // (attributes.ExtractFormValueAtIndex(dwarf2Data,
742 // // If we have valid block
743 // data, then we have location
745 // // that are fixed (not a
747 // const uint8_t *block_data =
748 // form_value.BlockData();
751 // uint32_t block_length =
752 // form_value.Unsigned();
753 // if (block_length == 1 +
754 // attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
756 // if (block_data[0] ==
762 parent_die = NULL; // Terminate the while loop.
765 case DW_TAG_compile_unit:
766 is_global_or_static_variable = true;
767 parent_die = NULL; // Terminate the while loop.
772 parent_die->GetParent(); // Keep going in the while loop.
779 case DW_AT_specification:
780 if (attributes.ExtractFormValueAtIndex(i, form_value))
781 specification_die_form = form_value;
788 case DW_TAG_subprogram:
791 ObjCLanguage::MethodName objc_method(name, true);
792 if (objc_method.IsValid(true)) {
793 ConstString objc_class_name_with_category(
794 objc_method.GetClassNameWithCategory());
795 ConstString objc_selector_name(objc_method.GetSelector());
796 ConstString objc_fullname_no_category_name(
797 objc_method.GetFullNameWithoutCategory(true));
798 ConstString objc_class_name_no_category(objc_method.GetClassName());
799 func_fullnames.Insert(ConstString(name),
800 DIERef(cu_offset, die.GetOffset()));
801 if (objc_class_name_with_category)
802 objc_class_selectors.Insert(objc_class_name_with_category,
803 DIERef(cu_offset, die.GetOffset()));
804 if (objc_class_name_no_category &&
805 objc_class_name_no_category != objc_class_name_with_category)
806 objc_class_selectors.Insert(objc_class_name_no_category,
807 DIERef(cu_offset, die.GetOffset()));
808 if (objc_selector_name)
809 func_selectors.Insert(objc_selector_name,
810 DIERef(cu_offset, die.GetOffset()));
811 if (objc_fullname_no_category_name)
812 func_fullnames.Insert(objc_fullname_no_category_name,
813 DIERef(cu_offset, die.GetOffset()));
815 // If we have a mangled name, then the DW_AT_name attribute
816 // is usually the method name without the class or any parameters
817 const DWARFDebugInfoEntry *parent = die.GetParent();
818 bool is_method = false;
820 dw_tag_t parent_tag = parent->Tag();
821 if (parent_tag == DW_TAG_class_type ||
822 parent_tag == DW_TAG_structure_type) {
825 if (specification_die_form.IsValid()) {
826 DWARFDIE specification_die =
827 dwarf_cu->GetSymbolFileDWARF()->DebugInfo()->GetDIE(
828 DIERef(specification_die_form));
829 if (specification_die.GetParent().IsStructOrClass())
836 func_methods.Insert(ConstString(name),
837 DIERef(cu_offset, die.GetOffset()));
839 func_basenames.Insert(ConstString(name),
840 DIERef(cu_offset, die.GetOffset()));
842 if (!is_method && !mangled_cstr && !objc_method.IsValid(true))
843 func_fullnames.Insert(ConstString(name),
844 DIERef(cu_offset, die.GetOffset()));
847 // Make sure our mangled name isn't the same string table entry
848 // as our name. If it starts with '_', then it is ok, else compare
849 // the string to make sure it isn't the same and we don't end up
850 // with duplicate entries
851 if (name && name != mangled_cstr &&
852 ((mangled_cstr[0] == '_') ||
853 (::strcmp(name, mangled_cstr) != 0))) {
854 Mangled mangled(ConstString(mangled_cstr), true);
855 func_fullnames.Insert(mangled.GetMangledName(),
856 DIERef(cu_offset, die.GetOffset()));
857 ConstString demangled = mangled.GetDemangledName(cu_language);
859 func_fullnames.Insert(demangled,
860 DIERef(cu_offset, die.GetOffset()));
866 case DW_TAG_inlined_subroutine:
869 func_basenames.Insert(ConstString(name),
870 DIERef(cu_offset, die.GetOffset()));
872 // Make sure our mangled name isn't the same string table entry
873 // as our name. If it starts with '_', then it is ok, else compare
874 // the string to make sure it isn't the same and we don't end up
875 // with duplicate entries
876 if (name && name != mangled_cstr &&
877 ((mangled_cstr[0] == '_') ||
878 (::strcmp(name, mangled_cstr) != 0))) {
879 Mangled mangled(ConstString(mangled_cstr), true);
880 func_fullnames.Insert(mangled.GetMangledName(),
881 DIERef(cu_offset, die.GetOffset()));
882 ConstString demangled = mangled.GetDemangledName(cu_language);
884 func_fullnames.Insert(demangled,
885 DIERef(cu_offset, die.GetOffset()));
888 func_fullnames.Insert(ConstString(name),
889 DIERef(cu_offset, die.GetOffset()));
893 case DW_TAG_array_type:
894 case DW_TAG_base_type:
895 case DW_TAG_class_type:
896 case DW_TAG_constant:
897 case DW_TAG_enumeration_type:
898 case DW_TAG_string_type:
899 case DW_TAG_structure_type:
900 case DW_TAG_subroutine_type:
902 case DW_TAG_union_type:
903 case DW_TAG_unspecified_type:
904 if (name && !is_declaration)
905 types.Insert(ConstString(name), DIERef(cu_offset, die.GetOffset()));
906 if (mangled_cstr && !is_declaration)
907 types.Insert(ConstString(mangled_cstr),
908 DIERef(cu_offset, die.GetOffset()));
911 case DW_TAG_namespace:
913 namespaces.Insert(ConstString(name),
914 DIERef(cu_offset, die.GetOffset()));
917 case DW_TAG_variable:
918 if (name && has_location_or_const_value && is_global_or_static_variable) {
919 globals.Insert(ConstString(name), DIERef(cu_offset, die.GetOffset()));
920 // Be sure to include variables by their mangled and demangled
921 // names if they have any since a variable can have a basename
922 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled
923 // mangled name "(anonymous namespace)::i"...
925 // Make sure our mangled name isn't the same string table entry
926 // as our name. If it starts with '_', then it is ok, else compare
927 // the string to make sure it isn't the same and we don't end up
928 // with duplicate entries
929 if (mangled_cstr && name != mangled_cstr &&
930 ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) {
931 Mangled mangled(ConstString(mangled_cstr), true);
932 globals.Insert(mangled.GetMangledName(),
933 DIERef(cu_offset, die.GetOffset()));
934 ConstString demangled = mangled.GetDemangledName(cu_language);
936 globals.Insert(demangled, DIERef(cu_offset, die.GetOffset()));
947 bool DWARFCompileUnit::Supports_unnamed_objc_bitfields() {
948 if (GetProducer() == eProducerClang) {
949 const uint32_t major_version = GetProducerVersionMajor();
950 if (major_version > 425 ||
951 (major_version == 425 && GetProducerVersionUpdate() >= 13))
956 return true; // Assume all other compilers didn't have incorrect ObjC bitfield
960 bool DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type() {
961 if (GetProducer() == eProducerLLVMGCC)
966 bool DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid() {
967 // llvm-gcc makes completely invalid decl file attributes and won't ever
968 // be fixed, so we need to know to ignore these.
969 return GetProducer() == eProducerLLVMGCC;
972 void DWARFCompileUnit::ParseProducerInfo() {
973 m_producer_version_major = UINT32_MAX;
974 m_producer_version_minor = UINT32_MAX;
975 m_producer_version_update = UINT32_MAX;
977 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
980 const char *producer_cstr = die->GetAttributeValueAsString(
981 m_dwarf2Data, this, DW_AT_producer, NULL);
983 RegularExpression llvm_gcc_regex(
984 llvm::StringRef("^4\\.[012]\\.[01] \\(Based on Apple "
985 "Inc\\. build [0-9]+\\) \\(LLVM build "
987 if (llvm_gcc_regex.Execute(llvm::StringRef(producer_cstr))) {
988 m_producer = eProducerLLVMGCC;
989 } else if (strstr(producer_cstr, "clang")) {
990 static RegularExpression g_clang_version_regex(
991 llvm::StringRef("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"));
992 RegularExpression::Match regex_match(3);
993 if (g_clang_version_regex.Execute(llvm::StringRef(producer_cstr),
996 if (regex_match.GetMatchAtIndex(producer_cstr, 1, str))
997 m_producer_version_major =
998 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
999 if (regex_match.GetMatchAtIndex(producer_cstr, 2, str))
1000 m_producer_version_minor =
1001 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1002 if (regex_match.GetMatchAtIndex(producer_cstr, 3, str))
1003 m_producer_version_update =
1004 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1006 m_producer = eProducerClang;
1007 } else if (strstr(producer_cstr, "GNU"))
1008 m_producer = eProducerGCC;
1011 if (m_producer == eProducerInvalid)
1012 m_producer = eProcucerOther;
1015 DWARFCompileUnit::Producer DWARFCompileUnit::GetProducer() {
1016 if (m_producer == eProducerInvalid)
1017 ParseProducerInfo();
1021 uint32_t DWARFCompileUnit::GetProducerVersionMajor() {
1022 if (m_producer_version_major == 0)
1023 ParseProducerInfo();
1024 return m_producer_version_major;
1027 uint32_t DWARFCompileUnit::GetProducerVersionMinor() {
1028 if (m_producer_version_minor == 0)
1029 ParseProducerInfo();
1030 return m_producer_version_minor;
1033 uint32_t DWARFCompileUnit::GetProducerVersionUpdate() {
1034 if (m_producer_version_update == 0)
1035 ParseProducerInfo();
1036 return m_producer_version_update;
1039 LanguageType DWARFCompileUnit::LanguageTypeFromDWARF(uint64_t val) {
1040 // Note: user languages between lo_user and hi_user
1041 // must be handled explicitly here.
1043 case DW_LANG_Mips_Assembler:
1044 return eLanguageTypeMipsAssembler;
1045 case DW_LANG_GOOGLE_RenderScript:
1046 return eLanguageTypeExtRenderScript;
1048 return static_cast<LanguageType>(val);
1052 LanguageType DWARFCompileUnit::GetLanguageType() {
1053 if (m_language_type != eLanguageTypeUnknown)
1054 return m_language_type;
1056 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1058 m_language_type = LanguageTypeFromDWARF(die->GetAttributeValueAsUnsigned(
1059 m_dwarf2Data, this, DW_AT_language, 0));
1060 return m_language_type;
1063 bool DWARFCompileUnit::IsDWARF64() const { return m_is_dwarf64; }
1065 bool DWARFCompileUnit::GetIsOptimized() {
1066 if (m_is_optimized == eLazyBoolCalculate) {
1067 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1069 m_is_optimized = eLazyBoolNo;
1070 if (die->GetAttributeValueAsUnsigned(m_dwarf2Data, this,
1071 DW_AT_APPLE_optimized, 0) == 1) {
1072 m_is_optimized = eLazyBoolYes;
1076 if (m_is_optimized == eLazyBoolYes) {
1083 DWARFFormValue::FixedFormSizes DWARFCompileUnit::GetFixedFormSizes() {
1084 return DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
1088 TypeSystem *DWARFCompileUnit::GetTypeSystem() {
1090 return m_dwarf2Data->GetTypeSystemForLanguage(GetLanguageType());
1095 void DWARFCompileUnit::SetUserData(void *d) {
1097 if (m_dwo_symbol_file)
1098 m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
1101 void DWARFCompileUnit::SetAddrBase(dw_addr_t addr_base,
1102 dw_addr_t ranges_base,
1103 dw_offset_t base_obj_offset) {
1104 m_addr_base = addr_base;
1105 m_ranges_base = ranges_base;
1106 m_base_obj_offset = base_obj_offset;
1109 lldb::ByteOrder DWARFCompileUnit::GetByteOrder() const {
1110 return m_dwarf2Data->GetObjectFile()->GetByteOrder();