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/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 "lldb/Utility/Stream.h"
22 #include "lldb/Utility/StreamString.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), m_abbrevs(NULL), m_user_data(NULL),
43 m_die_array(), m_func_aranges_ap(), m_base_addr(0),
44 m_offset(DW_INVALID_OFFSET), m_length(0), m_version(0),
45 m_addr_size(DWARFCompileUnit::GetDefaultAddressSize()),
46 m_producer(eProducerInvalid), m_producer_version_major(0),
47 m_producer_version_minor(0), m_producer_version_update(0),
48 m_language_type(eLanguageTypeUnknown), m_is_dwarf64(false),
49 m_is_optimized(eLazyBoolCalculate), m_addr_base(0),
50 m_ranges_base(0), m_base_obj_offset(DW_INVALID_OFFSET) {}
52 DWARFCompileUnit::~DWARFCompileUnit() {}
54 void DWARFCompileUnit::Clear() {
55 m_offset = DW_INVALID_OFFSET;
59 m_addr_size = DWARFCompileUnit::GetDefaultAddressSize();
62 m_func_aranges_ap.reset();
64 m_producer = eProducerInvalid;
65 m_language_type = eLanguageTypeUnknown;
67 m_is_optimized = eLazyBoolCalculate;
69 m_base_obj_offset = DW_INVALID_OFFSET;
72 bool DWARFCompileUnit::Extract(const DWARFDataExtractor &debug_info,
73 lldb::offset_t *offset_ptr) {
76 m_offset = *offset_ptr;
78 if (debug_info.ValidOffset(*offset_ptr)) {
79 dw_offset_t abbr_offset;
80 const DWARFDebugAbbrev *abbr = m_dwarf2Data->DebugAbbrev();
81 m_length = debug_info.GetDWARFInitialLength(offset_ptr);
82 m_is_dwarf64 = debug_info.IsDWARF64();
83 m_version = debug_info.GetU16(offset_ptr);
84 abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
85 m_addr_size = debug_info.GetU8(offset_ptr);
87 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset() - 1);
88 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
90 m_dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
91 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
93 if (length_OK && version_OK && addr_size_OK && abbr_offset_OK &&
95 m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
99 // reset the offset to where we tried to parse from if anything went wrong
100 *offset_ptr = m_offset;
106 void DWARFCompileUnit::ClearDIEs(bool keep_compile_unit_die) {
107 if (m_die_array.size() > 1) {
108 // std::vectors never get any smaller when resized to a smaller size,
109 // or when clear() or erase() are called, the size will report that it
110 // is smaller, but the memory allocated remains intact (call capacity()
111 // to see this). So we need to create a temporary vector and swap the
112 // contents which will cause just the internal pointers to be swapped
113 // so that when "tmp_array" goes out of scope, it will destroy the
116 // Save at least the compile unit DIE
117 DWARFDebugInfoEntry::collection tmp_array;
118 m_die_array.swap(tmp_array);
119 if (keep_compile_unit_die)
120 m_die_array.push_back(tmp_array.front());
123 if (m_dwo_symbol_file)
124 m_dwo_symbol_file->GetCompileUnit()->ClearDIEs(keep_compile_unit_die);
127 //----------------------------------------------------------------------
128 // ParseCompileUnitDIEsIfNeeded
130 // Parses a compile unit and indexes its DIEs if it hasn't already been
132 //----------------------------------------------------------------------
133 size_t DWARFCompileUnit::ExtractDIEsIfNeeded(bool cu_die_only) {
134 const size_t initial_die_array_size = m_die_array.size();
135 if ((cu_die_only && initial_die_array_size > 0) || initial_die_array_size > 1)
136 return 0; // Already parsed
139 LLVM_PRETTY_FUNCTION,
140 "%8.8x: DWARFCompileUnit::ExtractDIEsIfNeeded( cu_die_only = %i )",
141 m_offset, cu_die_only);
143 // Set the offset to that of the first DIE and calculate the start of the
144 // next compilation unit header.
145 lldb::offset_t offset = GetFirstDIEOffset();
146 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
148 DWARFDebugInfoEntry die;
149 // Keep a flat array of the DIE for binary lookup by DIE offset
152 LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
154 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
155 log, "DWARFCompileUnit::ExtractDIEsIfNeeded () for compile unit at "
156 ".debug_info[0x%8.8x]",
162 // We are in our compile unit, parse starting at the offset
163 // we were told to parse
164 const DWARFDataExtractor &debug_info_data =
165 m_dwarf2Data->get_debug_info_data();
166 std::vector<uint32_t> die_index_stack;
167 die_index_stack.reserve(32);
168 die_index_stack.push_back(0);
169 bool prev_die_had_children = false;
170 DWARFFormValue::FixedFormSizes fixed_form_sizes =
171 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
173 while (offset < next_cu_offset &&
174 die.FastExtract(debug_info_data, this, fixed_form_sizes, &offset)) {
176 // log->Printf("0x%8.8x: %*.*s%s%s",
178 // depth * 2, depth * 2, "",
179 // DW_TAG_value_to_name (die.Tag()),
180 // die.HasChildren() ? " *" : "");
182 const bool null_die = die.IsNULL();
184 if (initial_die_array_size == 0)
185 AddCompileUnitDIE(die);
186 uint64_t base_addr = die.GetAttributeValueAsAddress(
187 m_dwarf2Data, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
188 if (base_addr == LLDB_INVALID_ADDRESS)
189 base_addr = die.GetAttributeValueAsAddress(m_dwarf2Data, this,
191 SetBaseAddress(base_addr);
196 if (prev_die_had_children) {
197 // This will only happen if a DIE says is has children
198 // but all it contains is a NULL tag. Since we are removing
199 // the NULL DIEs from the list (saves up to 25% in C++ code),
200 // we need a way to let the DIE know that it actually doesn't
202 if (!m_die_array.empty())
203 m_die_array.back().SetEmptyChildren(true);
206 die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
208 if (die_index_stack.back())
209 m_die_array[die_index_stack.back()].SetSiblingIndex(
210 m_die_array.size() - die_index_stack.back());
212 // Only push the DIE if it isn't a NULL DIE
213 m_die_array.push_back(die);
219 if (!die_index_stack.empty())
220 die_index_stack.pop_back();
225 break; // We are done with this compile unit!
227 prev_die_had_children = false;
229 die_index_stack.back() = m_die_array.size() - 1;
231 const bool die_has_children = die.HasChildren();
232 if (die_has_children) {
233 die_index_stack.push_back(0);
236 prev_die_had_children = die_has_children;
240 // Give a little bit of info if we encounter corrupt DWARF (our offset
241 // should always terminate at or before the start of the next compilation
243 if (offset > next_cu_offset) {
244 m_dwarf2Data->GetObjectFile()->GetModule()->ReportWarning(
245 "DWARF compile unit extends beyond its bounds cu 0x%8.8x at "
246 "0x%8.8" PRIx64 "\n",
247 GetOffset(), offset);
250 // Since std::vector objects will double their size, we really need to
251 // make a new array with the perfect size so we don't end up wasting
252 // space. So here we copy and swap to make sure we don't have any extra
255 if (m_die_array.size() < m_die_array.capacity()) {
256 DWARFDebugInfoEntry::collection exact_size_die_array(m_die_array.begin(),
258 exact_size_die_array.swap(m_die_array);
260 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
261 if (log && log->GetVerbose()) {
264 if (m_die_array.empty())
265 strm.Printf("error: no DIE for compile unit");
267 m_die_array[0].Dump(m_dwarf2Data, this, strm, UINT32_MAX);
268 log->PutString(strm.GetString());
271 if (!m_dwo_symbol_file)
272 return m_die_array.size();
274 DWARFCompileUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
275 size_t dwo_die_count = dwo_cu->ExtractDIEsIfNeeded(cu_die_only);
276 return m_die_array.size() + dwo_die_count -
277 1; // We have 2 CU die, but we want to count it only as one
280 void DWARFCompileUnit::AddCompileUnitDIE(DWARFDebugInfoEntry &die) {
281 assert(m_die_array.empty() && "Compile unit DIE already added");
284 const DWARFDebugInfoEntry &cu_die = m_die_array.front();
285 std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
286 m_dwarf2Data->GetDwoSymbolFileForCompileUnit(*this, cu_die);
287 if (!dwo_symbol_file)
290 DWARFCompileUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
292 return; // Can't fetch the compile unit from the dwo file.
294 DWARFDIE dwo_cu_die = dwo_cu->GetCompileUnitDIEOnly();
295 if (!dwo_cu_die.IsValid())
296 return; // Can't fetch the compile unit DIE from the dwo file.
298 uint64_t main_dwo_id = cu_die.GetAttributeValueAsUnsigned(
299 m_dwarf2Data, this, DW_AT_GNU_dwo_id, 0);
300 uint64_t sub_dwo_id =
301 dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0);
302 if (main_dwo_id != sub_dwo_id)
303 return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to
304 // a differectn compilation.
306 m_dwo_symbol_file = std::move(dwo_symbol_file);
308 dw_addr_t addr_base = cu_die.GetAttributeValueAsUnsigned(
309 m_dwarf2Data, this, DW_AT_GNU_addr_base, 0);
310 dw_addr_t ranges_base = cu_die.GetAttributeValueAsUnsigned(
311 m_dwarf2Data, this, DW_AT_GNU_ranges_base, 0);
312 dwo_cu->SetAddrBase(addr_base, ranges_base, m_offset);
315 dw_offset_t DWARFCompileUnit::GetAbbrevOffset() const {
316 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
319 bool DWARFCompileUnit::Verify(Stream *s) const {
320 const DWARFDataExtractor &debug_info = m_dwarf2Data->get_debug_info_data();
321 bool valid_offset = debug_info.ValidOffset(m_offset);
322 bool length_OK = debug_info.ValidOffset(GetNextCompileUnitOffset() - 1);
323 bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
324 bool abbr_offset_OK =
325 m_dwarf2Data->get_debug_abbrev_data().ValidOffset(GetAbbrevOffset());
326 bool addr_size_OK = ((m_addr_size == 4) || (m_addr_size == 8));
327 if (valid_offset && length_OK && version_OK && addr_size_OK &&
331 s->Printf(" 0x%8.8x: ", m_offset);
332 DumpDataExtractor(m_dwarf2Data->get_debug_info_data(), s, m_offset,
333 lldb::eFormatHex, 1, Size(), 32, LLDB_INVALID_ADDRESS, 0,
338 s->Printf(" The length (0x%8.8x) for this compile unit is too "
339 "large for the .debug_info provided.\n",
342 s->Printf(" The 16 bit compile unit header version is not "
345 s->Printf(" The offset into the .debug_abbrev section (0x%8.8x) "
349 s->Printf(" The address size is unsupported: 0x%2.2x\n",
352 s->Printf(" The start offset of the compile unit header in the "
353 ".debug_info is invalid.\n");
358 void DWARFCompileUnit::Dump(Stream *s) const {
359 s->Printf("0x%8.8x: Compile Unit: length = 0x%8.8x, version = 0x%4.4x, "
360 "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x (next CU at "
362 m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size,
363 GetNextCompileUnitOffset());
366 static uint8_t g_default_addr_size = 4;
368 uint8_t DWARFCompileUnit::GetAddressByteSize(const DWARFCompileUnit *cu) {
370 return cu->GetAddressByteSize();
371 return DWARFCompileUnit::GetDefaultAddressSize();
374 bool DWARFCompileUnit::IsDWARF64(const DWARFCompileUnit *cu) {
376 return cu->IsDWARF64();
380 uint8_t DWARFCompileUnit::GetDefaultAddressSize() {
381 return g_default_addr_size;
384 void DWARFCompileUnit::SetDefaultAddressSize(uint8_t addr_size) {
385 g_default_addr_size = addr_size;
388 lldb::user_id_t DWARFCompileUnit::GetID() const {
389 dw_offset_t local_id =
390 m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset;
392 return DIERef(local_id, local_id).GetUID(m_dwarf2Data);
397 void DWARFCompileUnit::BuildAddressRangeTable(
398 SymbolFileDWARF *dwarf2Data, DWARFDebugAranges *debug_aranges) {
399 // This function is usually called if there in no .debug_aranges section
400 // in order to produce a compile unit level set of address ranges that
403 size_t num_debug_aranges = debug_aranges->GetNumRanges();
405 // First get the compile unit DIE only and check if it has a DW_AT_ranges
406 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
408 const dw_offset_t cu_offset = GetOffset();
410 DWARFRangeList ranges;
411 const size_t num_ranges =
412 die->GetAttributeAddressRanges(dwarf2Data, this, ranges, false);
413 if (num_ranges > 0) {
414 // This compile unit has DW_AT_ranges, assume this is correct if it
415 // is present since clang no longer makes .debug_aranges by default
416 // and it emits DW_AT_ranges for DW_TAG_compile_units. GCC also does
417 // this with recent GCC builds.
418 for (size_t i = 0; i < num_ranges; ++i) {
419 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
420 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
421 range.GetRangeEnd());
424 return; // We got all of our ranges from the DW_AT_ranges attribute
427 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF
429 // If the DIEs weren't parsed, then we don't want all dies for all compile
431 // to stay loaded when they weren't needed. So we can end up parsing the DWARF
432 // and then throwing them all away to keep memory usage down.
433 const bool clear_dies = ExtractDIEsIfNeeded(false) > 1;
437 die->BuildAddressRangeTable(dwarf2Data, this, debug_aranges);
439 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
440 // We got nothing from the functions, maybe we have a line tables only
441 // situation. Check the line tables and build the arange table from this.
443 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
445 SymbolFileDWARFDebugMap *debug_map_sym_file =
446 m_dwarf2Data->GetDebugMapSymfile();
447 if (debug_map_sym_file == NULL) {
448 LineTable *line_table = sc.comp_unit->GetLineTable();
451 LineTable::FileAddressRanges file_ranges;
452 const bool append = true;
453 const size_t num_ranges =
454 line_table->GetContiguousFileAddressRanges(file_ranges, append);
455 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
456 const LineTable::FileAddressRanges::Entry &range =
457 file_ranges.GetEntryRef(idx);
458 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
459 range.GetRangeEnd());
463 debug_map_sym_file->AddOSOARanges(dwarf2Data, debug_aranges);
467 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
468 // We got nothing from the functions, maybe we have a line tables only
469 // situation. Check the line tables and build the arange table from this.
471 sc.comp_unit = dwarf2Data->GetCompUnitForDWARFCompUnit(this);
473 LineTable *line_table = sc.comp_unit->GetLineTable();
476 LineTable::FileAddressRanges file_ranges;
477 const bool append = true;
478 const size_t num_ranges =
479 line_table->GetContiguousFileAddressRanges(file_ranges, append);
480 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
481 const LineTable::FileAddressRanges::Entry &range =
482 file_ranges.GetEntryRef(idx);
483 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
484 range.GetRangeEnd());
490 // Keep memory down by clearing DIEs if this generate function
491 // caused them to be parsed
496 const DWARFDebugAranges &DWARFCompileUnit::GetFunctionAranges() {
497 if (m_func_aranges_ap.get() == NULL) {
498 m_func_aranges_ap.reset(new DWARFDebugAranges());
499 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
502 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage(
503 log, "DWARFCompileUnit::GetFunctionAranges() for compile unit at "
504 ".debug_info[0x%8.8x]",
507 const DWARFDebugInfoEntry *die = DIEPtr();
509 die->BuildFunctionAddressRangeTable(m_dwarf2Data, this,
510 m_func_aranges_ap.get());
512 if (m_dwo_symbol_file) {
513 DWARFCompileUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
514 const DWARFDebugInfoEntry *dwo_die = dwo_cu->DIEPtr();
516 dwo_die->BuildFunctionAddressRangeTable(m_dwo_symbol_file.get(), dwo_cu,
517 m_func_aranges_ap.get());
520 const bool minimize = false;
521 m_func_aranges_ap->Sort(minimize);
523 return *m_func_aranges_ap.get();
527 DWARFCompileUnit::LookupAddress(const dw_addr_t address) {
529 const DWARFDebugAranges &func_aranges = GetFunctionAranges();
531 // Re-check the aranges auto pointer contents in case it was created above
532 if (!func_aranges.IsEmpty())
533 return GetDIE(func_aranges.FindAddress(address));
538 //----------------------------------------------------------------------
539 // Compare function DWARFDebugAranges::Range structures
540 //----------------------------------------------------------------------
541 static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
542 const dw_offset_t die_offset) {
543 return die.GetOffset() < die_offset;
546 //----------------------------------------------------------------------
549 // Get the DIE (Debug Information Entry) with the specified offset by
550 // first checking if the DIE is contained within this compile unit and
551 // grabbing the DIE from this compile unit. Otherwise we grab the DIE
552 // from the DWARF file.
553 //----------------------------------------------------------------------
555 DWARFCompileUnit::GetDIE(dw_offset_t die_offset) {
556 if (die_offset != DW_INVALID_OFFSET) {
557 if (m_dwo_symbol_file)
558 return m_dwo_symbol_file->GetCompileUnit()->GetDIE(die_offset);
560 if (ContainsDIEOffset(die_offset)) {
561 ExtractDIEsIfNeeded(false);
562 DWARFDebugInfoEntry::iterator end = m_die_array.end();
563 DWARFDebugInfoEntry::iterator pos =
564 lower_bound(m_die_array.begin(), end, die_offset, CompareDIEOffset);
566 if (die_offset == (*pos).GetOffset())
567 return DWARFDIE(this, &(*pos));
570 // Don't specify the compile unit offset as we don't know it because the
572 // a different compile unit in the same symbol file.
573 return m_dwarf2Data->DebugInfo()->GetDIEForDIEOffset(die_offset);
576 return DWARFDIE(); // Not found
579 size_t DWARFCompileUnit::AppendDIEsWithTag(const dw_tag_t tag,
580 DWARFDIECollection &dies,
581 uint32_t depth) const {
582 size_t old_size = dies.Size();
583 DWARFDebugInfoEntry::const_iterator pos;
584 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
585 for (pos = m_die_array.begin(); pos != end; ++pos) {
586 if (pos->Tag() == tag)
587 dies.Append(DWARFDIE(this, &(*pos)));
590 // Return the number of DIEs added to the collection
591 return dies.Size() - old_size;
595 // DWARFCompileUnit::AddGlobalDIEByIndex (uint32_t die_idx)
597 // m_global_die_indexes.push_back (die_idx);
602 // DWARFCompileUnit::AddGlobal (const DWARFDebugInfoEntry* die)
604 // // Indexes to all file level global and static variables
605 // m_global_die_indexes;
607 // if (m_die_array.empty())
610 // const DWARFDebugInfoEntry* first_die = &m_die_array[0];
611 // const DWARFDebugInfoEntry* end = first_die + m_die_array.size();
612 // if (first_die <= die && die < end)
613 // m_global_die_indexes.push_back (die - first_die);
616 void DWARFCompileUnit::Index(NameToDIE &func_basenames,
617 NameToDIE &func_fullnames, NameToDIE &func_methods,
618 NameToDIE &func_selectors,
619 NameToDIE &objc_class_selectors,
620 NameToDIE &globals, NameToDIE &types,
621 NameToDIE &namespaces) {
622 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
625 m_dwarf2Data->GetObjectFile()->GetModule()->LogMessage(
627 "DWARFCompileUnit::Index() for compile unit at .debug_info[0x%8.8x]",
631 const LanguageType cu_language = GetLanguageType();
632 DWARFFormValue::FixedFormSizes fixed_form_sizes =
633 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
636 IndexPrivate(this, cu_language, fixed_form_sizes, GetOffset(), func_basenames,
637 func_fullnames, func_methods, func_selectors,
638 objc_class_selectors, globals, types, namespaces);
640 SymbolFileDWARFDwo *dwo_symbol_file = GetDwoSymbolFile();
641 if (dwo_symbol_file) {
642 IndexPrivate(dwo_symbol_file->GetCompileUnit(), cu_language,
643 fixed_form_sizes, GetOffset(), func_basenames, func_fullnames,
644 func_methods, func_selectors, objc_class_selectors, globals,
649 void DWARFCompileUnit::IndexPrivate(
650 DWARFCompileUnit *dwarf_cu, const LanguageType cu_language,
651 const DWARFFormValue::FixedFormSizes &fixed_form_sizes,
652 const dw_offset_t cu_offset, NameToDIE &func_basenames,
653 NameToDIE &func_fullnames, NameToDIE &func_methods,
654 NameToDIE &func_selectors, NameToDIE &objc_class_selectors,
655 NameToDIE &globals, NameToDIE &types, NameToDIE &namespaces) {
656 DWARFDebugInfoEntry::const_iterator pos;
657 DWARFDebugInfoEntry::const_iterator begin = dwarf_cu->m_die_array.begin();
658 DWARFDebugInfoEntry::const_iterator end = dwarf_cu->m_die_array.end();
659 for (pos = begin; pos != end; ++pos) {
660 const DWARFDebugInfoEntry &die = *pos;
662 const dw_tag_t tag = die.Tag();
665 case DW_TAG_array_type:
666 case DW_TAG_base_type:
667 case DW_TAG_class_type:
668 case DW_TAG_constant:
669 case DW_TAG_enumeration_type:
670 case DW_TAG_inlined_subroutine:
671 case DW_TAG_namespace:
672 case DW_TAG_string_type:
673 case DW_TAG_structure_type:
674 case DW_TAG_subprogram:
675 case DW_TAG_subroutine_type:
677 case DW_TAG_union_type:
678 case DW_TAG_unspecified_type:
679 case DW_TAG_variable:
686 DWARFAttributes attributes;
687 const char *name = NULL;
688 const char *mangled_cstr = NULL;
689 bool is_declaration = false;
690 // bool is_artificial = false;
691 bool has_address = false;
692 bool has_location_or_const_value = false;
693 bool is_global_or_static_variable = false;
695 DWARFFormValue specification_die_form;
696 const size_t num_attributes =
697 die.GetAttributes(dwarf_cu, fixed_form_sizes, attributes);
698 if (num_attributes > 0) {
699 for (uint32_t i = 0; i < num_attributes; ++i) {
700 dw_attr_t attr = attributes.AttributeAtIndex(i);
701 DWARFFormValue form_value;
704 if (attributes.ExtractFormValueAtIndex(i, form_value))
705 name = form_value.AsCString();
708 case DW_AT_declaration:
709 if (attributes.ExtractFormValueAtIndex(i, form_value))
710 is_declaration = form_value.Unsigned() != 0;
713 // case DW_AT_artificial:
714 // if (attributes.ExtractFormValueAtIndex(i,
716 // is_artificial = form_value.Unsigned() != 0;
719 case DW_AT_MIPS_linkage_name:
720 case DW_AT_linkage_name:
721 if (attributes.ExtractFormValueAtIndex(i, form_value))
722 mangled_cstr = form_value.AsCString();
736 case DW_AT_const_value:
737 has_location_or_const_value = true;
738 if (tag == DW_TAG_variable) {
739 const DWARFDebugInfoEntry *parent_die = die.GetParent();
740 while (parent_die != NULL) {
741 switch (parent_die->Tag()) {
742 case DW_TAG_subprogram:
743 case DW_TAG_lexical_block:
744 case DW_TAG_inlined_subroutine:
745 // Even if this is a function level static, we don't add it. We
746 // could theoretically
747 // add these if we wanted to by introspecting into the
748 // DW_AT_location and seeing
749 // if the location describes a hard coded address, but we dont
750 // want the performance
751 // penalty of that right now.
752 is_global_or_static_variable = false;
754 // (attributes.ExtractFormValueAtIndex(dwarf2Data,
757 // // If we have valid block
758 // data, then we have location
760 // // that are fixed (not a
762 // const uint8_t *block_data =
763 // form_value.BlockData();
766 // uint32_t block_length =
767 // form_value.Unsigned();
768 // if (block_length == 1 +
769 // attributes.CompileUnitAtIndex(i)->GetAddressByteSize())
771 // if (block_data[0] ==
777 parent_die = NULL; // Terminate the while loop.
780 case DW_TAG_compile_unit:
781 is_global_or_static_variable = true;
782 parent_die = NULL; // Terminate the while loop.
787 parent_die->GetParent(); // Keep going in the while loop.
794 case DW_AT_specification:
795 if (attributes.ExtractFormValueAtIndex(i, form_value))
796 specification_die_form = form_value;
803 case DW_TAG_subprogram:
806 ObjCLanguage::MethodName objc_method(name, true);
807 if (objc_method.IsValid(true)) {
808 ConstString objc_class_name_with_category(
809 objc_method.GetClassNameWithCategory());
810 ConstString objc_selector_name(objc_method.GetSelector());
811 ConstString objc_fullname_no_category_name(
812 objc_method.GetFullNameWithoutCategory(true));
813 ConstString objc_class_name_no_category(objc_method.GetClassName());
814 func_fullnames.Insert(ConstString(name),
815 DIERef(cu_offset, die.GetOffset()));
816 if (objc_class_name_with_category)
817 objc_class_selectors.Insert(objc_class_name_with_category,
818 DIERef(cu_offset, die.GetOffset()));
819 if (objc_class_name_no_category &&
820 objc_class_name_no_category != objc_class_name_with_category)
821 objc_class_selectors.Insert(objc_class_name_no_category,
822 DIERef(cu_offset, die.GetOffset()));
823 if (objc_selector_name)
824 func_selectors.Insert(objc_selector_name,
825 DIERef(cu_offset, die.GetOffset()));
826 if (objc_fullname_no_category_name)
827 func_fullnames.Insert(objc_fullname_no_category_name,
828 DIERef(cu_offset, die.GetOffset()));
830 // If we have a mangled name, then the DW_AT_name attribute
831 // is usually the method name without the class or any parameters
832 const DWARFDebugInfoEntry *parent = die.GetParent();
833 bool is_method = false;
835 dw_tag_t parent_tag = parent->Tag();
836 if (parent_tag == DW_TAG_class_type ||
837 parent_tag == DW_TAG_structure_type) {
840 if (specification_die_form.IsValid()) {
841 DWARFDIE specification_die =
842 dwarf_cu->GetSymbolFileDWARF()->DebugInfo()->GetDIE(
843 DIERef(specification_die_form));
844 if (specification_die.GetParent().IsStructOrClass())
851 func_methods.Insert(ConstString(name),
852 DIERef(cu_offset, die.GetOffset()));
854 func_basenames.Insert(ConstString(name),
855 DIERef(cu_offset, die.GetOffset()));
857 if (!is_method && !mangled_cstr && !objc_method.IsValid(true))
858 func_fullnames.Insert(ConstString(name),
859 DIERef(cu_offset, die.GetOffset()));
862 // Make sure our mangled name isn't the same string table entry
863 // as our name. If it starts with '_', then it is ok, else compare
864 // the string to make sure it isn't the same and we don't end up
865 // with duplicate entries
866 if (name && name != mangled_cstr &&
867 ((mangled_cstr[0] == '_') ||
868 (::strcmp(name, mangled_cstr) != 0))) {
869 Mangled mangled(ConstString(mangled_cstr), true);
870 func_fullnames.Insert(mangled.GetMangledName(),
871 DIERef(cu_offset, die.GetOffset()));
872 ConstString demangled = mangled.GetDemangledName(cu_language);
874 func_fullnames.Insert(demangled,
875 DIERef(cu_offset, die.GetOffset()));
881 case DW_TAG_inlined_subroutine:
884 func_basenames.Insert(ConstString(name),
885 DIERef(cu_offset, die.GetOffset()));
887 // Make sure our mangled name isn't the same string table entry
888 // as our name. If it starts with '_', then it is ok, else compare
889 // the string to make sure it isn't the same and we don't end up
890 // with duplicate entries
891 if (name && name != mangled_cstr &&
892 ((mangled_cstr[0] == '_') ||
893 (::strcmp(name, mangled_cstr) != 0))) {
894 Mangled mangled(ConstString(mangled_cstr), true);
895 func_fullnames.Insert(mangled.GetMangledName(),
896 DIERef(cu_offset, die.GetOffset()));
897 ConstString demangled = mangled.GetDemangledName(cu_language);
899 func_fullnames.Insert(demangled,
900 DIERef(cu_offset, die.GetOffset()));
903 func_fullnames.Insert(ConstString(name),
904 DIERef(cu_offset, die.GetOffset()));
908 case DW_TAG_array_type:
909 case DW_TAG_base_type:
910 case DW_TAG_class_type:
911 case DW_TAG_constant:
912 case DW_TAG_enumeration_type:
913 case DW_TAG_string_type:
914 case DW_TAG_structure_type:
915 case DW_TAG_subroutine_type:
917 case DW_TAG_union_type:
918 case DW_TAG_unspecified_type:
919 if (name && !is_declaration)
920 types.Insert(ConstString(name), DIERef(cu_offset, die.GetOffset()));
921 if (mangled_cstr && !is_declaration)
922 types.Insert(ConstString(mangled_cstr),
923 DIERef(cu_offset, die.GetOffset()));
926 case DW_TAG_namespace:
928 namespaces.Insert(ConstString(name),
929 DIERef(cu_offset, die.GetOffset()));
932 case DW_TAG_variable:
933 if (name && has_location_or_const_value && is_global_or_static_variable) {
934 globals.Insert(ConstString(name), DIERef(cu_offset, die.GetOffset()));
935 // Be sure to include variables by their mangled and demangled
936 // names if they have any since a variable can have a basename
937 // "i", a mangled named "_ZN12_GLOBAL__N_11iE" and a demangled
938 // mangled name "(anonymous namespace)::i"...
940 // Make sure our mangled name isn't the same string table entry
941 // as our name. If it starts with '_', then it is ok, else compare
942 // the string to make sure it isn't the same and we don't end up
943 // with duplicate entries
944 if (mangled_cstr && name != mangled_cstr &&
945 ((mangled_cstr[0] == '_') || (::strcmp(name, mangled_cstr) != 0))) {
946 Mangled mangled(ConstString(mangled_cstr), true);
947 globals.Insert(mangled.GetMangledName(),
948 DIERef(cu_offset, die.GetOffset()));
949 ConstString demangled = mangled.GetDemangledName(cu_language);
951 globals.Insert(demangled, DIERef(cu_offset, die.GetOffset()));
962 bool DWARFCompileUnit::Supports_unnamed_objc_bitfields() {
963 if (GetProducer() == eProducerClang) {
964 const uint32_t major_version = GetProducerVersionMajor();
965 if (major_version > 425 ||
966 (major_version == 425 && GetProducerVersionUpdate() >= 13))
971 return true; // Assume all other compilers didn't have incorrect ObjC bitfield
975 bool DWARFCompileUnit::Supports_DW_AT_APPLE_objc_complete_type() {
976 if (GetProducer() == eProducerLLVMGCC)
981 bool DWARFCompileUnit::DW_AT_decl_file_attributes_are_invalid() {
982 // llvm-gcc makes completely invalid decl file attributes and won't ever
983 // be fixed, so we need to know to ignore these.
984 return GetProducer() == eProducerLLVMGCC;
987 void DWARFCompileUnit::ParseProducerInfo() {
988 m_producer_version_major = UINT32_MAX;
989 m_producer_version_minor = UINT32_MAX;
990 m_producer_version_update = UINT32_MAX;
992 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
995 const char *producer_cstr = die->GetAttributeValueAsString(
996 m_dwarf2Data, this, DW_AT_producer, NULL);
998 RegularExpression llvm_gcc_regex(
999 llvm::StringRef("^4\\.[012]\\.[01] \\(Based on Apple "
1000 "Inc\\. build [0-9]+\\) \\(LLVM build "
1002 if (llvm_gcc_regex.Execute(llvm::StringRef(producer_cstr))) {
1003 m_producer = eProducerLLVMGCC;
1004 } else if (strstr(producer_cstr, "clang")) {
1005 static RegularExpression g_clang_version_regex(
1006 llvm::StringRef("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"));
1007 RegularExpression::Match regex_match(3);
1008 if (g_clang_version_regex.Execute(llvm::StringRef(producer_cstr),
1011 if (regex_match.GetMatchAtIndex(producer_cstr, 1, str))
1012 m_producer_version_major =
1013 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1014 if (regex_match.GetMatchAtIndex(producer_cstr, 2, str))
1015 m_producer_version_minor =
1016 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1017 if (regex_match.GetMatchAtIndex(producer_cstr, 3, str))
1018 m_producer_version_update =
1019 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
1021 m_producer = eProducerClang;
1022 } else if (strstr(producer_cstr, "GNU"))
1023 m_producer = eProducerGCC;
1026 if (m_producer == eProducerInvalid)
1027 m_producer = eProcucerOther;
1030 DWARFCompileUnit::Producer DWARFCompileUnit::GetProducer() {
1031 if (m_producer == eProducerInvalid)
1032 ParseProducerInfo();
1036 uint32_t DWARFCompileUnit::GetProducerVersionMajor() {
1037 if (m_producer_version_major == 0)
1038 ParseProducerInfo();
1039 return m_producer_version_major;
1042 uint32_t DWARFCompileUnit::GetProducerVersionMinor() {
1043 if (m_producer_version_minor == 0)
1044 ParseProducerInfo();
1045 return m_producer_version_minor;
1048 uint32_t DWARFCompileUnit::GetProducerVersionUpdate() {
1049 if (m_producer_version_update == 0)
1050 ParseProducerInfo();
1051 return m_producer_version_update;
1054 LanguageType DWARFCompileUnit::LanguageTypeFromDWARF(uint64_t val) {
1055 // Note: user languages between lo_user and hi_user
1056 // must be handled explicitly here.
1058 case DW_LANG_Mips_Assembler:
1059 return eLanguageTypeMipsAssembler;
1060 case DW_LANG_GOOGLE_RenderScript:
1061 return eLanguageTypeExtRenderScript;
1063 return static_cast<LanguageType>(val);
1067 LanguageType DWARFCompileUnit::GetLanguageType() {
1068 if (m_language_type != eLanguageTypeUnknown)
1069 return m_language_type;
1071 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1073 m_language_type = LanguageTypeFromDWARF(die->GetAttributeValueAsUnsigned(
1074 m_dwarf2Data, this, DW_AT_language, 0));
1075 return m_language_type;
1078 bool DWARFCompileUnit::IsDWARF64() const { return m_is_dwarf64; }
1080 bool DWARFCompileUnit::GetIsOptimized() {
1081 if (m_is_optimized == eLazyBoolCalculate) {
1082 const DWARFDebugInfoEntry *die = GetCompileUnitDIEPtrOnly();
1084 m_is_optimized = eLazyBoolNo;
1085 if (die->GetAttributeValueAsUnsigned(m_dwarf2Data, this,
1086 DW_AT_APPLE_optimized, 0) == 1) {
1087 m_is_optimized = eLazyBoolYes;
1091 if (m_is_optimized == eLazyBoolYes) {
1098 DWARFFormValue::FixedFormSizes DWARFCompileUnit::GetFixedFormSizes() {
1099 return DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
1103 TypeSystem *DWARFCompileUnit::GetTypeSystem() {
1105 return m_dwarf2Data->GetTypeSystemForLanguage(GetLanguageType());
1110 void DWARFCompileUnit::SetUserData(void *d) {
1112 if (m_dwo_symbol_file)
1113 m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
1116 void DWARFCompileUnit::SetAddrBase(dw_addr_t addr_base,
1117 dw_addr_t ranges_base,
1118 dw_offset_t base_obj_offset) {
1119 m_addr_base = addr_base;
1120 m_ranges_base = ranges_base;
1121 m_base_obj_offset = base_obj_offset;
1124 lldb::ByteOrder DWARFCompileUnit::GetByteOrder() const {
1125 return m_dwarf2Data->GetObjectFile()->GetByteOrder();