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 "lldb/Core/Mangled.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Stream.h"
15 #include "lldb/Core/StreamString.h"
16 #include "lldb/Core/Timer.h"
17 #include "lldb/Host/StringConvert.h"
18 #include "lldb/Symbol/CompileUnit.h"
19 #include "lldb/Symbol/LineTable.h"
20 #include "lldb/Symbol/ObjectFile.h"
21 #include "Plugins/Language/ObjC/ObjCLanguage.h"
23 #include "DWARFDebugAbbrev.h"
24 #include "DWARFDebugAranges.h"
25 #include "DWARFDebugInfo.h"
26 #include "DWARFDIECollection.h"
27 #include "DWARFFormValue.h"
28 #include "LogChannelDWARF.h"
29 #include "NameToDIE.h"
30 #include "SymbolFileDWARF.h"
31 #include "SymbolFileDWARFDwo.h"
32 #include "SymbolFileDWARFDebugMap.h"
35 using namespace lldb_private;
41 DWARFCompileUnit::DWARFCompileUnit(SymbolFileDWARF* dwarf2Data) :
42 m_dwarf2Data (dwarf2Data),
48 m_offset (DW_INVALID_OFFSET),
51 m_addr_size (DWARFCompileUnit::GetDefaultAddressSize()),
52 m_producer (eProducerInvalid),
53 m_producer_version_major (0),
54 m_producer_version_minor (0),
55 m_producer_version_update (0),
56 m_language_type (eLanguageTypeUnknown),
58 m_is_optimized (eLazyBoolCalculate),
60 m_base_obj_offset (DW_INVALID_OFFSET)
64 DWARFCompileUnit::~DWARFCompileUnit()
68 DWARFCompileUnit::Clear()
70 m_offset = DW_INVALID_OFFSET;
74 m_addr_size = DWARFCompileUnit::GetDefaultAddressSize();
77 m_func_aranges_ap.reset();
79 m_producer = eProducerInvalid;
80 m_language_type = eLanguageTypeUnknown;
82 m_is_optimized = eLazyBoolCalculate;
84 m_base_obj_offset = DW_INVALID_OFFSET;
88 DWARFCompileUnit::Extract(const DWARFDataExtractor &debug_info, lldb::offset_t *offset_ptr)
92 m_offset = *offset_ptr;
94 if (debug_info.ValidOffset(*offset_ptr))
96 dw_offset_t abbr_offset;
97 const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
98 m_length = debug_info.GetDWARFInitialLength(offset_ptr);
99 m_is_dwarf64 = debug_info.IsDWARF64();
100 m_version = debug_info.GetU16(offset_ptr);
101 abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
102 m_addr_size = debug_info.GetU8 (offset_ptr);
104 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
105 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
106 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
107 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
109 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK && abbr != NULL)
111 m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
115 // reset the offset to where we tried to parse from if anything went wrong
116 *offset_ptr = m_offset;
124 DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die)
126 if (m_die_array.size() > 1)
128 // std::vectors never get any smaller when resized to a smaller size,
129 // or when clear() or erase() are called, the size will report that it
130 // is smaller, but the memory allocated remains intact (call capacity()
131 // to see this). So we need to create a temporary vector and swap the
132 // contents which will cause just the internal pointers to be swapped
133 // so that when "tmp_array" goes out of scope, it will destroy the
136 // Save at least the compile unit DIE
137 DWARFDebugInfoEntry::collection tmp_array;
138 m_die_array.swap(tmp_array);
139 if (keep_compile_unit_die)
140 m_die_array.push_back(tmp_array.front());
143 if (m_dwo_symbol_file)
144 m_dwo_symbol_file->GetCompileUnit()->ClearDIEs(keep_compile_unit_die);
147 //----------------------------------------------------------------------
148 // ParseCompileUnitDIEsIfNeeded
150 // Parses a compile unit and indexes its DIEs if it hasn't already been
152 //----------------------------------------------------------------------
154 DWARFCompileUnit::ExtractDIEsIfNeeded (bool cu_die_only)
156 const size_t initial_die_array_size = m_die_array.size();
157 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
158 return 0; // Already parsed
160 Timer scoped_timer (__PRETTY_FUNCTION__,
161 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
165 // Set the offset to that of the first DIE and calculate the start of the
166 // next compilation unit header.
167 lldb::offset_t offset = GetFirstDIEOffset();
168 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
170 DWARFDebugInfoEntry die;
171 // Keep a flat array of the DIE for binary lookup by DIE offset
174 Log *log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
177 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
178 "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at .debug_info[0x%8.8x]",
184 // We are in our compile unit, parse starting at the offset
185 // we were told to parse
186 const DWARFDataExtractor& debug_info_data = m_dwarf2Data->get_debug_info_data();
187 std::vector<uint32_t> die_index_stack;
188 die_index_stack.reserve(32);
189 die_index_stack.push_back(0);
190 bool prev_die_had_children = false;
191 DWARFFormValue::FixedFormSizes fixed_form_sizes =
192 DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), m_is_dwarf64);
193 while (offset < next_cu_offset &&
194 die.FastExtract (debug_info_data, this, fixed_form_sizes, &offset))
197 // log->Printf("0x%8.8x: %*.*s%s%s",
199 // depth * 2, depth * 2, "",
200 // DW_TAG_value_to_name (die.Tag()),
201 // die.HasChildren() ? " *" : "");
203 const bool null_die = die.IsNULL();
206 if (initial_die_array_size == 0)
207 AddCompileUnitDIE(die);
208 uint64_t base_addr = die.GetAttributeValueAsAddress(m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
209 if (base_addr == LLDB_INVALID_ADDRESS)
210 base_addr = die.GetAttributeValueAsAddress(m_dwarf2Data, this, DW_AT_entry_pc, 0);
211 SetBaseAddress (base_addr);
219 if (prev_die_had_children)
221 // This will only happen if a DIE says is has children
222 // but all it contains is a NULL tag. Since we are removing
223 // the NULL DIEs from the list (saves up to 25% in C++ code),
224 // we need a way to let the DIE know that it actually doesn't
226 if (!m_die_array.empty())
227 m_die_array.back().SetEmptyChildren(true);
232 die.SetParentIndex(m_die_array.size() - die_index_stack[depth-1]);
234 if (die_index_stack.back())
235 m_die_array[die_index_stack.back()].SetSiblingIndex(m_die_array.size()-die_index_stack.back());
237 // Only push the DIE if it isn't a NULL DIE
238 m_die_array.push_back(die);
245 if (!die_index_stack.empty())
246 die_index_stack.pop_back();
251 break; // We are done with this compile unit!
253 prev_die_had_children = false;
257 die_index_stack.back() = m_die_array.size() - 1;
259 const bool die_has_children = die.HasChildren();
260 if (die_has_children)
262 die_index_stack.push_back(0);
265 prev_die_had_children = die_has_children;
269 // Give a little bit of info if we encounter corrupt DWARF (our offset
270 // should always terminate at or before the start of the next compilation
272 if (offset > next_cu_offset)
274 m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning ("DWARF compile unit extends beyond its bounds cu 0x%8.8x at 0x%8.8" PRIx64 "\n",
279 // Since std::vector objects will double their size, we really need to
280 // make a new array with the perfect size so we don't end up wasting
281 // space. So here we copy and swap to make sure we don't have any extra
284 if (m_die_array.size () < m_die_array.capacity())
286 DWARFDebugInfoEntry::collection exact_size_die_array (m_die_array.begin(), m_die_array.end());
287 exact_size_die_array.swap (m_die_array);
289 Log *verbose_log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_DEBUG_INFO | DWARF_LOG_VERBOSE));
294 if (m_die_array.empty())
295 strm.Printf("error: no DIE for compile unit");
297 m_die_array[0].Dump(m_dwarf2Data, this, strm, UINT32_MAX);
298 verbose_log->PutCString (strm.GetString().c_str());
301 if (!m_dwo_symbol_file)
302 return m_die_array.size();
304 DWARFCompileUnit* dwo_cu = m_dwo_symbol_file->GetCompileUnit();
305 size_t dwo_die_count = dwo_cu->ExtractDIEsIfNeeded(cu_die_only);
306 return m_die_array.size() + dwo_die_count - 1; // We have 2 CU die, but we waht to count it only as one
310 DWARFCompileUnit::AddCompileUnitDIE(DWARFDebugInfoEntry& die)
312 assert (m_die_array.empty() && "Compile unit DIE already added");
315 const DWARFDebugInfoEntry &cu_die = m_die_array.front();
316 std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file = m_dwarf2Data->GetDwoSymbolFileForCompileUnit(*this, cu_die);
317 if (!dwo_symbol_file)
320 DWARFCompileUnit* dwo_cu = dwo_symbol_file->GetCompileUnit();
322 return; // Can't fetch the compile unit from the dwo file.
324 DWARFDIE dwo_cu_die = dwo_cu->GetCompileUnitDIEOnly();
325 if (!dwo_cu_die.IsValid())
326 return; // Can't fetch the compile unit DIE from the dwo file.
328 uint64_t main_dwo_id = cu_die.GetAttributeValueAsUnsigned(m_dwarf2Data,
332 uint64_t sub_dwo_id = dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0);
333 if (main_dwo_id != sub_dwo_id)
334 return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to a differectn compilation.
336 m_dwo_symbol_file = std::move(dwo_symbol_file);
338 dw_addr_t addr_base = cu_die.GetAttributeValueAsUnsigned(m_dwarf2Data,
342 dwo_cu->SetAddrBase(addr_base, m_offset);
346 DWARFCompileUnit::GetAbbrevOffset() const
348 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
354 DWARFCompileUnit::Verify(Stream *s) const
356 const DWARFDataExtractor& debug_info = m_dwarf2Data->get_debug_info_data();
357 bool valid_offset = debug_info.ValidOffset(m_offset);
358 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset()-1);
359 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
360 bool abbr_offset_OK = m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
361 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
362 bool verbose = s->GetVerbose();
363 if (valid_offset && length_OK && version_OK && addr_size_OK && abbr_offset_OK)
366 s->Printf(" 0x%8.8x: OK\n", m_offset);
371 s->Printf(" 0x%8.8x: ", m_offset);
373 m_dwarf2Data->get_debug_info_data().Dump (s, m_offset, lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0, 0);
378 s->Printf(" The length (0x%8.8x) for this compile unit is too large for the .debug_info provided.\n", m_length);
380 s->Printf(" The 16 bit compile unit header version is not supported.\n");
382 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) is not valid.\n", GetAbbrevOffset());
384 s->Printf(" The address size is unsupported: 0x%2.2x\n", m_addr_size);
387 s->Printf(" The start offset of the compile unit header in the .debug_info is invalid.\n");
394 DWARFCompileUnit::Dump(Stream *s) const
396 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at {0x%8.8x})\n",
397 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size, GetNextCompileUnitOffset());
401 static uint8_t g_default_addr_size = 4;
404 DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit* cu)
407 return cu->GetAddressByteSize();
408 return DWARFCompileUnit::GetDefaultAddressSize();
412 DWARFCompileUnit::IsDWARF64(const DWARFCompileUnit* cu)
415 return cu->IsDWARF64();
420 DWARFCompileUnit::GetDefaultAddressSize()
422 return g_default_addr_size;
426 DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size)
428 g_default_addr_size = addr_size;
432 DWARFCompileUnit::GetID () const
434 dw_offset_t local_id = m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset;
436 return DIERef(local_id, local_id).GetUID(m_dwarf2Data);
442 DWARFCompileUnit::BuildAddressRangeTable (SymbolFileDWARF* dwarf2Data,
443 DWARFDebugAranges* debug_aranges)
445 // This function is usually called if there in no .debug_aranges section
446 // in order to produce a compile unit level set of address ranges that
449 size_t num_debug_aranges = debug_aranges->GetNumRanges();
451 // First get the compile unit DIE only and check if it has a DW_AT_ranges
452 const DWARFDebugInfoEntry* die = GetCompileUnitDIEPtrOnly();
454 const dw_offset_t cu_offset = GetOffset();
457 DWARFRangeList ranges;
458 const size_t num_ranges = die->GetAttributeAddressRanges(dwarf2Data, this, ranges, false);
461 // This compile unit has DW_AT_ranges, assume this is correct if it
462 // is present since clang no longer makes .debug_aranges by default
463 // and it emits DW_AT_ranges for DW_TAG_compile_units. GCC also does
464 // this with recent GCC builds.
465 for (size_t i=0; i<num_ranges; ++i)
467 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
468 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), range.GetRangeEnd());
471 return; // We got all of our ranges from the DW_AT_ranges attribute
474 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF
476 // If the DIEs weren't parsed, then we don't want all dies for all compile units
477 // to stay loaded when they weren't needed. So we can end up parsing the DWARF
478 // and then throwing them all away to keep memory usage down.
479 const bool clear_dies = ExtractDIEsIfNeeded (false) > 1;
483 die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges);
485 if (debug_aranges->GetNumRanges() == num_debug_aranges)
487 // We got nothing from the functions, maybe we have a line tables only
488 // situation. Check the line tables and build the arange table from this.
490 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
493 SymbolFileDWARFDebugMap *debug_map_sym_file = m_dwarf2Data->GetDebugMapSymfile();
494 if (debug_map_sym_file == NULL)
496 LineTable *line_table = sc.comp_unit->GetLineTable();
500 LineTable::FileAddressRanges file_ranges;
501 const bool append = true;
502 const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append);
503 for (uint32_t idx=0; idx<num_ranges; ++idx)
505 const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx);
506 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(), range.GetRangeEnd());
511 debug_map_sym_file->AddOSOARanges(dwarf2Data,debug_aranges);
515 if (debug_aranges->GetNumRanges() == num_debug_aranges)
517 // We got nothing from the functions, maybe we have a line tables only
518 // situation. Check the line tables and build the arange table from this.
520 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
523 LineTable *line_table = sc.comp_unit->GetLineTable();
527 LineTable::FileAddressRanges file_ranges;
528 const bool append = true;
529 const size_t num_ranges = line_table->GetContiguousFileAddressRanges (file_ranges, append);
530 for (uint32_t idx=0; idx<num_ranges; ++idx)
532 const LineTable::FileAddressRanges::Entry &range = file_ranges.GetEntryRef(idx);
533 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(), range.GetRangeEnd());
539 // Keep memory down by clearing DIEs if this generate function
540 // caused them to be parsed
547 const DWARFDebugAranges &
548 DWARFCompileUnit::GetFunctionAranges ()
550 if (m_func_aranges_ap.get() == NULL)
552 m_func_aranges_ap.reset (new DWARFDebugAranges());
553 Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
557 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log,
558 "DWARFCompileUnit::GetFunctionAranges() for compile unit at .debug_info[0x%8.8x]",
561 const DWARFDebugInfoEntry* die = DIEPtr();
563 die->BuildFunctionAddressRangeTable (m_dwarf2Data, this, m_func_aranges_ap.get());
565 if (m_dwo_symbol_file)
567 DWARFCompileUnit* dwo_cu = m_dwo_symbol_file->GetCompileUnit();
568 const DWARFDebugInfoEntry* dwo_die = dwo_cu->DIEPtr();
570 dwo_die->BuildFunctionAddressRangeTable (m_dwo_symbol_file.get(),
572 m_func_aranges_ap.get());
575 const bool minimize = false;
576 m_func_aranges_ap->Sort(minimize);
578 return *m_func_aranges_ap.get();
582 DWARFCompileUnit::LookupAddress (const dw_addr_t address)
586 const DWARFDebugAranges &func_aranges = GetFunctionAranges ();
588 // Re-check the aranges auto pointer contents in case it was created above
589 if (!func_aranges.IsEmpty())
590 return GetDIE(func_aranges.FindAddress(address));
595 //----------------------------------------------------------------------
596 // Compare function DWARFDebugAranges::Range structures
597 //----------------------------------------------------------------------
598 static bool CompareDIEOffset (const DWARFDebugInfoEntry& die, const dw_offset_t die_offset)
600 return die.GetOffset() < die_offset;
603 //----------------------------------------------------------------------
606 // Get the DIE (Debug Information Entry) with the specified offset by
607 // first checking if the DIE is contained within this compile unit and
608 // grabbing the DIE from this compile unit. Otherwise we grab the DIE
609 // from the DWARF file.
610 //----------------------------------------------------------------------
612 DWARFCompileUnit::GetDIE (dw_offset_t die_offset)
614 if (die_offset != DW_INVALID_OFFSET)
616 if (m_dwo_symbol_file)
617 return m_dwo_symbol_file->GetCompileUnit()->GetDIE(die_offset);
619 if (ContainsDIEOffset(die_offset))
621 ExtractDIEsIfNeeded (false);
622 DWARFDebugInfoEntry::iterator end = m_die_array.end();
623 DWARFDebugInfoEntry::iterator pos = lower_bound(m_die_array.begin(), end, die_offset, CompareDIEOffset);
626 if (die_offset == (*pos).GetOffset())
627 return DWARFDIE(this, &(*pos));
632 // Don't specify the compile unit offset as we don't know it because the DIE belongs to
633 // a different compile unit in the same symbol file.
634 return m_dwarf2Data->DebugInfo()->GetDIEForDIEOffset(die_offset);
637 return DWARFDIE(); // Not found
641 DWARFCompileUnit::AppendDIEsWithTag (const dw_tag_t tag, DWARFDIECollection& dies, uint32_t depth) const
643 size_t old_size = dies.Size();
644 DWARFDebugInfoEntry::const_iterator pos;
645 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
646 for (pos = m_die_array.begin(); pos != end; ++pos)
648 if (pos->Tag() == tag)
649 dies.Append (DWARFDIE(this, &(*pos)));
652 // Return the number of DIEs added to the collection
653 return dies.Size() - old_size;
657 //DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
659 // m_global_die_indexes.push_back (die_idx);
664 //DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
666 // // Indexes to all file level global and static variables
667 // m_global_die_indexes;
669 // if (m_die_array.empty())
672 // const DWARFDebugInfoEntry* first_die = &m_die_array[0];
673 // const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
674 // if (first_die <= die && die < end)
675 // m_global_die_indexes.push_back (die - first_die);
680 DWARFCompileUnit::Index (NameToDIE& func_basenames,
681 NameToDIE& func_fullnames,
682 NameToDIE& func_methods,
683 NameToDIE& func_selectors,
684 NameToDIE& objc_class_selectors,
687 NameToDIE& namespaces)
689 Log *log (LogChannelDWARF::GetLogIfAll (DWARF_LOG_LOOKUPS));
693 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage (log,
694 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]",
698 const LanguageType cu_language = GetLanguageType();
699 DWARFFormValue::FixedFormSizes fixed_form_sizes =
700 DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), m_is_dwarf64);
710 objc_class_selectors,
715 SymbolFileDWARFDwo* dwo_symbol_file = GetDwoSymbolFile();
718 IndexPrivate(dwo_symbol_file->GetCompileUnit(),
726 objc_class_selectors,
734 DWARFCompileUnit::IndexPrivate (DWARFCompileUnit* dwarf_cu,
735 const LanguageType cu_language,
736 const DWARFFormValue::FixedFormSizes& fixed_form_sizes,
737 const dw_offset_t cu_offset,
738 NameToDIE& func_basenames,
739 NameToDIE& func_fullnames,
740 NameToDIE& func_methods,
741 NameToDIE& func_selectors,
742 NameToDIE& objc_class_selectors,
745 NameToDIE& namespaces)
747 DWARFDebugInfoEntry::const_iterator pos;
748 DWARFDebugInfoEntry::const_iterator begin = dwarf_cu->m_die_array.begin();
749 DWARFDebugInfoEntry::const_iterator end = dwarf_cu->m_die_array.end();
750 for (pos = begin; pos != end; ++pos)
752 const DWARFDebugInfoEntry &die = *pos;
754 const dw_tag_t tag = die.Tag();
758 case DW_TAG_array_type:
759 case DW_TAG_base_type:
760 case DW_TAG_class_type:
761 case DW_TAG_constant:
762 case DW_TAG_enumeration_type:
763 case DW_TAG_inlined_subroutine:
764 case DW_TAG_namespace:
765 case DW_TAG_string_type:
766 case DW_TAG_structure_type:
767 case DW_TAG_subprogram:
768 case DW_TAG_subroutine_type:
770 case DW_TAG_union_type:
771 case DW_TAG_unspecified_type:
772 case DW_TAG_variable:
779 DWARFAttributes attributes;
780 const char *name = NULL;
781 const char *mangled_cstr = NULL;
782 bool is_declaration = false;
783 //bool is_artificial = false;
784 bool has_address = false;
785 bool has_location_or_const_value = false;
786 bool is_global_or_static_variable = false;
788 DWARFFormValue specification_die_form;
789 const size_t num_attributes = die.GetAttributes(dwarf_cu, fixed_form_sizes, attributes);
790 if (num_attributes > 0)
792 for (uint32_t i=0; i<num_attributes; ++i)
794 dw_attr_t attr = attributes.AttributeAtIndex(i);
795 DWARFFormValue form_value;
799 if (attributes.ExtractFormValueAtIndex(i, form_value))
800 name = form_value.AsCString();
803 case DW_AT_declaration:
804 if (attributes.ExtractFormValueAtIndex(i, form_value))
805 is_declaration = form_value.Unsigned() != 0;
808 // case DW_AT_artificial:
809 // if (attributes.ExtractFormValueAtIndex(i, form_value))
810 // is_artificial = form_value.Unsigned() != 0;
813 case DW_AT_MIPS_linkage_name:
814 case DW_AT_linkage_name:
815 if (attributes.ExtractFormValueAtIndex(i, form_value))
816 mangled_cstr = form_value.AsCString();
830 case DW_AT_const_value:
831 has_location_or_const_value = true;
832 if (tag == DW_TAG_variable)
834 const DWARFDebugInfoEntry* parent_die = die.GetParent();
835 while ( parent_die != NULL )
837 switch (parent_die->Tag())
839 case DW_TAG_subprogram:
840 case DW_TAG_lexical_block:
841 case DW_TAG_inlined_subroutine:
842 // Even if this is a function level static, we don't add it. We could theoretically
843 // add these if we wanted to by introspecting into the DW_AT_location and seeing
844 // if the location describes a hard coded address, but we dont want the performance
845 // penalty of that right now.
846 is_global_or_static_variable = false;
847 // if (attributes.ExtractFormValueAtIndex(dwarf2Data, i, form_value))
849 // // If we have valid block data, then we have location expression bytes
850 // // that are fixed (not a location list).
851 // const uint8_t *block_data = form_value.BlockData();
854 // uint32_t block_length = form_value.Unsigned();
855 // if (block_length == 1 + attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
857 // if (block_data[0] == DW_OP_addr)
862 parent_die = NULL; // Terminate the while loop.
865 case DW_TAG_compile_unit:
866 is_global_or_static_variable = true;
867 parent_die = NULL; // Terminate the while loop.
871 parent_die = parent_die->GetParent(); // Keep going in the while loop.
878 case DW_AT_specification:
879 if (attributes.ExtractFormValueAtIndex(i, form_value))
880 specification_die_form = form_value;
888 case DW_TAG_subprogram:
893 ObjCLanguage::MethodName objc_method(name, true);
894 if (objc_method.IsValid(true))
896 ConstString objc_class_name_with_category (objc_method.GetClassNameWithCategory());
897 ConstString objc_selector_name (objc_method.GetSelector());
898 ConstString objc_fullname_no_category_name (objc_method.GetFullNameWithoutCategory(true));
899 ConstString objc_class_name_no_category (objc_method.GetClassName());
900 func_fullnames.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
901 if (objc_class_name_with_category)
902 objc_class_selectors.Insert(objc_class_name_with_category, DIERef(cu_offset, die.GetOffset()));
903 if (objc_class_name_no_category && objc_class_name_no_category != objc_class_name_with_category)
904 objc_class_selectors.Insert(objc_class_name_no_category, DIERef(cu_offset, die.GetOffset()));
905 if (objc_selector_name)
906 func_selectors.Insert (objc_selector_name, DIERef(cu_offset, die.GetOffset()));
907 if (objc_fullname_no_category_name)
908 func_fullnames.Insert (objc_fullname_no_category_name, DIERef(cu_offset, die.GetOffset()));
910 // If we have a mangled name, then the DW_AT_name attribute
911 // is usually the method name without the class or any parameters
912 const DWARFDebugInfoEntry *parent = die.GetParent();
913 bool is_method = false;
916 dw_tag_t parent_tag = parent->Tag();
917 if (parent_tag == DW_TAG_class_type || parent_tag == DW_TAG_structure_type)
923 if (specification_die_form.IsValid())
925 DWARFDIE specification_die = dwarf_cu->GetSymbolFileDWARF()->DebugInfo()->GetDIE (DIERef(specification_die_form));
926 if (specification_die.GetParent().IsStructOrClass())
934 func_methods.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
936 func_basenames.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
938 if (!is_method && !mangled_cstr && !objc_method.IsValid(true))
939 func_fullnames.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
943 // Make sure our mangled name isn't the same string table entry
944 // as our name. If it starts with '_', then it is ok, else compare
945 // the string to make sure it isn't the same and we don't end up
946 // with duplicate entries
947 if (name && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
949 Mangled mangled (ConstString(mangled_cstr), true);
950 func_fullnames.Insert (mangled.GetMangledName(), DIERef(cu_offset, die.GetOffset()));
951 ConstString demangled = mangled.GetDemangledName(cu_language);
953 func_fullnames.Insert (demangled, DIERef(cu_offset, die.GetOffset()));
959 case DW_TAG_inlined_subroutine:
963 func_basenames.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
966 // Make sure our mangled name isn't the same string table entry
967 // as our name. If it starts with '_', then it is ok, else compare
968 // the string to make sure it isn't the same and we don't end up
969 // with duplicate entries
970 if (name && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
972 Mangled mangled (ConstString(mangled_cstr), true);
973 func_fullnames.Insert (mangled.GetMangledName(), DIERef(cu_offset, die.GetOffset()));
974 ConstString demangled = mangled.GetDemangledName(cu_language);
976 func_fullnames.Insert (demangled, DIERef(cu_offset, die.GetOffset()));
980 func_fullnames.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
984 case DW_TAG_array_type:
985 case DW_TAG_base_type:
986 case DW_TAG_class_type:
987 case DW_TAG_constant:
988 case DW_TAG_enumeration_type:
989 case DW_TAG_string_type:
990 case DW_TAG_structure_type:
991 case DW_TAG_subroutine_type:
993 case DW_TAG_union_type:
994 case DW_TAG_unspecified_type:
995 if (name && !is_declaration)
996 types.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
997 if (mangled_cstr && !is_declaration)
998 types.Insert (ConstString(mangled_cstr), DIERef(cu_offset, die.GetOffset()));
1001 case DW_TAG_namespace:
1003 namespaces.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
1006 case DW_TAG_variable:
1007 if (name && has_location_or_const_value && is_global_or_static_variable)
1009 globals.Insert (ConstString(name), DIERef(cu_offset, die.GetOffset()));
1010 // Be sure to include variables by their mangled and demangled
1011 // names if they have any since a variable can have a basename
1012 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled
1013 // mangled name "(anonymous namespace)::i"...
1015 // Make sure our mangled name isn't the same string table entry
1016 // as our name. If it starts with '_', then it is ok, else compare
1017 // the string to make sure it isn't the same and we don't end up
1018 // with duplicate entries
1019 if (mangled_cstr && name != mangled_cstr && ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0)))
1021 Mangled mangled (ConstString(mangled_cstr), true);
1022 globals.Insert (mangled.GetMangledName(), DIERef(cu_offset, die.GetOffset()));
1023 ConstString demangled = mangled.GetDemangledName(cu_language);
1025 globals.Insert (demangled, DIERef(cu_offset, die.GetOffset()));
1037 DWARFCompileUnit::Supports_unnamed_objc_bitfields ()
1039 if (GetProducer() == eProducerClang)
1041 const uint32_t major_version = GetProducerVersionMajor();
1042 if (major_version > 425 || (major_version == 425 && GetProducerVersionUpdate() >= 13))
1047 return true; // Assume all other compilers didn't have incorrect ObjC bitfield info
1051 DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type ()
1053 if (GetProducer() == eProducerLLVMGCC)
1059 DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid()
1061 // llvm-gcc makes completely invalid decl file attributes and won't ever
1062 // be fixed, so we need to know to ignore these.
1063 return GetProducer() == eProducerLLVMGCC;
1067 DWARFCompileUnit::ParseProducerInfo ()
1069 m_producer_version_major = UINT32_MAX;
1070 m_producer_version_minor = UINT32_MAX;
1071 m_producer_version_update = UINT32_MAX;
1073 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1077 const char *producer_cstr = die->GetAttributeValueAsString(m_dwarf2Data, this, DW_AT_producer, NULL);
1080 RegularExpression llvm_gcc_regex("^4\\.[012]\\.[01] \\(Based on Apple Inc\\. build [0-9]+\\) \\(LLVM build [\\.0-9]+\\)$");
1081 if (llvm_gcc_regex.Execute (producer_cstr))
1083 m_producer = eProducerLLVMGCC;
1085 else if (strstr(producer_cstr, "clang"))
1087 static RegularExpression g_clang_version_regex("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)");
1088 RegularExpression::Match regex_match(3);
1089 if (g_clang_version_regex.Execute (producer_cstr, ®ex_match))
1092 if (regex_match.GetMatchAtIndex (producer_cstr, 1, str))
1093 m_producer_version_major = StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1094 if (regex_match.GetMatchAtIndex (producer_cstr, 2, str))
1095 m_producer_version_minor = StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1096 if (regex_match.GetMatchAtIndex (producer_cstr, 3, str))
1097 m_producer_version_update = StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1099 m_producer = eProducerClang;
1101 else if (strstr(producer_cstr, "GNU"))
1102 m_producer = eProducerGCC;
1105 if (m_producer == eProducerInvalid)
1106 m_producer = eProcucerOther;
1109 DWARFCompileUnit::Producer
1110 DWARFCompileUnit::GetProducer ()
1112 if (m_producer == eProducerInvalid)
1113 ParseProducerInfo ();
1119 DWARFCompileUnit::GetProducerVersionMajor()
1121 if (m_producer_version_major == 0)
1122 ParseProducerInfo ();
1123 return m_producer_version_major;
1127 DWARFCompileUnit::GetProducerVersionMinor()
1129 if (m_producer_version_minor == 0)
1130 ParseProducerInfo ();
1131 return m_producer_version_minor;
1135 DWARFCompileUnit::GetProducerVersionUpdate()
1137 if (m_producer_version_update == 0)
1138 ParseProducerInfo ();
1139 return m_producer_version_update;
1143 DWARFCompileUnit::LanguageTypeFromDWARF(uint64_t val)
1145 // Note: user languages between lo_user and hi_user
1146 // must be handled explicitly here.
1149 case DW_LANG_Mips_Assembler:
1150 return eLanguageTypeMipsAssembler;
1151 case DW_LANG_GOOGLE_RenderScript:
1152 return eLanguageTypeExtRenderScript;
1154 return static_cast<LanguageType>(val);
1159 DWARFCompileUnit::GetLanguageType()
1161 if (m_language_type != eLanguageTypeUnknown)
1162 return m_language_type;
1164 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1166 m_language_type = LanguageTypeFromDWARF(die->GetAttributeValueAsUnsigned(m_dwarf2Data, this, DW_AT_language, 0));
1167 return m_language_type;
1171 DWARFCompileUnit::IsDWARF64() const
1173 return m_is_dwarf64;
1177 DWARFCompileUnit::GetIsOptimized ()
1179 if (m_is_optimized == eLazyBoolCalculate)
1181 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1184 m_is_optimized = eLazyBoolNo;
1185 if (die->GetAttributeValueAsUnsigned (m_dwarf2Data, this, DW_AT_APPLE_optimized, 0) == 1)
1187 m_is_optimized = eLazyBoolYes;
1191 if (m_is_optimized == eLazyBoolYes)
1201 DWARFFormValue::FixedFormSizes
1202 DWARFCompileUnit::GetFixedFormSizes ()
1204 return DWARFFormValue::GetFixedFormSizesForAddressSize (GetAddressByteSize(), IsDWARF64());
1208 DWARFCompileUnit::GetTypeSystem ()
1211 return m_dwarf2Data->GetTypeSystemForLanguage(GetLanguageType());
1217 DWARFCompileUnit::SetUserData(void *d)
1220 if (m_dwo_symbol_file)
1221 m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
1225 DWARFCompileUnit::SetAddrBase(dw_addr_t addr_base, dw_offset_t base_obj_offset)
1227 m_addr_base = addr_base;
1228 m_base_obj_offset = base_obj_offset;
1232 DWARFCompileUnit::GetByteOrder() const
1234 return m_dwarf2Data->GetObjectFile()->GetByteOrder();