1 //===-- DWARFUnit.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 "DWARFUnit.h"
12 #include "lldb/Core/Module.h"
13 #include "lldb/Host/StringConvert.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/LineTable.h"
16 #include "lldb/Symbol/ObjectFile.h"
17 #include "lldb/Utility/LLDBAssert.h"
18 #include "lldb/Utility/StreamString.h"
19 #include "lldb/Utility/Timer.h"
21 #include "DWARFDIECollection.h"
22 #include "DWARFDebugAranges.h"
23 #include "DWARFDebugInfo.h"
24 #include "LogChannelDWARF.h"
25 #include "SymbolFileDWARFDebugMap.h"
26 #include "SymbolFileDWARFDwo.h"
29 using namespace lldb_private;
34 DWARFUnit::DWARFUnit(SymbolFileDWARF *dwarf)
35 : m_dwarf(dwarf), m_cancel_scopes(false) {}
37 DWARFUnit::~DWARFUnit() {}
39 //----------------------------------------------------------------------
40 // Parses first DIE of a compile unit.
41 //----------------------------------------------------------------------
42 void DWARFUnit::ExtractUnitDIEIfNeeded() {
44 llvm::sys::ScopedReader lock(m_first_die_mutex);
46 return; // Already parsed
48 llvm::sys::ScopedWriter lock(m_first_die_mutex);
50 return; // Already parsed
52 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
54 func_cat, "%8.8x: DWARFUnit::ExtractUnitDIEIfNeeded()", m_offset);
56 // Set the offset to that of the first DIE and calculate the start of the
57 // next compilation unit header.
58 lldb::offset_t offset = GetFirstDIEOffset();
60 // We are in our compile unit, parse starting at the offset we were told to
62 const DWARFDataExtractor &data = GetData();
63 DWARFFormValue::FixedFormSizes fixed_form_sizes =
64 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
66 if (offset < GetNextCompileUnitOffset() &&
67 m_first_die.FastExtract(data, this, fixed_form_sizes, &offset)) {
68 AddUnitDIE(m_first_die);
72 ExtractDIEsEndCheck(offset);
75 //----------------------------------------------------------------------
76 // Parses a compile unit and indexes its DIEs if it hasn't already been done.
77 // It will leave this compile unit extracted forever.
78 //----------------------------------------------------------------------
79 void DWARFUnit::ExtractDIEsIfNeeded() {
80 m_cancel_scopes = true;
83 llvm::sys::ScopedReader lock(m_die_array_mutex);
84 if (!m_die_array.empty())
85 return; // Already parsed
87 llvm::sys::ScopedWriter lock(m_die_array_mutex);
88 if (!m_die_array.empty())
89 return; // Already parsed
91 ExtractDIEsRWLocked();
94 //----------------------------------------------------------------------
95 // Parses a compile unit and indexes its DIEs if it hasn't already been done.
96 // It will clear this compile unit after returned instance gets out of scope,
97 // no other ScopedExtractDIEs instance is running for this compile unit
98 // and no ExtractDIEsIfNeeded() has been executed during this ScopedExtractDIEs
100 //----------------------------------------------------------------------
101 DWARFUnit::ScopedExtractDIEs DWARFUnit::ExtractDIEsScoped() {
102 ScopedExtractDIEs scoped(this);
105 llvm::sys::ScopedReader lock(m_die_array_mutex);
106 if (!m_die_array.empty())
107 return scoped; // Already parsed
109 llvm::sys::ScopedWriter lock(m_die_array_mutex);
110 if (!m_die_array.empty())
111 return scoped; // Already parsed
113 // Otherwise m_die_array would be already populated.
114 lldbassert(!m_cancel_scopes);
116 ExtractDIEsRWLocked();
117 scoped.m_clear_dies = true;
121 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(DWARFUnit *cu) : m_cu(cu) {
123 m_cu->m_die_array_scoped_mutex.lock_shared();
126 DWARFUnit::ScopedExtractDIEs::~ScopedExtractDIEs() {
129 m_cu->m_die_array_scoped_mutex.unlock_shared();
130 if (!m_clear_dies || m_cu->m_cancel_scopes)
132 // Be sure no other ScopedExtractDIEs is running anymore.
133 llvm::sys::ScopedWriter lock_scoped(m_cu->m_die_array_scoped_mutex);
134 llvm::sys::ScopedWriter lock(m_cu->m_die_array_mutex);
135 if (m_cu->m_cancel_scopes)
137 m_cu->ClearDIEsRWLocked();
140 DWARFUnit::ScopedExtractDIEs::ScopedExtractDIEs(ScopedExtractDIEs &&rhs)
141 : m_cu(rhs.m_cu), m_clear_dies(rhs.m_clear_dies) {
145 DWARFUnit::ScopedExtractDIEs &DWARFUnit::ScopedExtractDIEs::operator=(
146 DWARFUnit::ScopedExtractDIEs &&rhs) {
149 m_clear_dies = rhs.m_clear_dies;
153 //----------------------------------------------------------------------
154 // Parses a compile unit and indexes its DIEs, m_die_array_mutex must be
155 // held R/W and m_die_array must be empty.
156 //----------------------------------------------------------------------
157 void DWARFUnit::ExtractDIEsRWLocked() {
158 llvm::sys::ScopedWriter first_die_lock(m_first_die_mutex);
160 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
162 func_cat, "%8.8x: DWARFUnit::ExtractDIEsIfNeeded()", m_offset);
164 // Set the offset to that of the first DIE and calculate the start of the
165 // next compilation unit header.
166 lldb::offset_t offset = GetFirstDIEOffset();
167 lldb::offset_t next_cu_offset = GetNextCompileUnitOffset();
169 DWARFDebugInfoEntry die;
170 // Keep a flat array of the DIE for binary lookup by DIE offset
172 LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_LOOKUPS));
174 m_dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace(
176 "DWARFUnit::ExtractDIEsIfNeeded () for compile unit at "
177 ".debug_info[0x%8.8x]",
182 // We are in our compile unit, parse starting at the offset we were told to
184 const DWARFDataExtractor &data = GetData();
185 std::vector<uint32_t> die_index_stack;
186 die_index_stack.reserve(32);
187 die_index_stack.push_back(0);
188 bool prev_die_had_children = false;
189 DWARFFormValue::FixedFormSizes fixed_form_sizes =
190 DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
192 while (offset < next_cu_offset &&
193 die.FastExtract(data, this, fixed_form_sizes, &offset)) {
194 const bool null_die = die.IsNULL();
196 assert(m_die_array.empty() && "Compile unit DIE already added");
198 // The average bytes per DIE entry has been seen to be around 14-20 so
199 // lets pre-reserve half of that since we are now stripping the NULL
202 // Only reserve the memory if we are adding children of the main
203 // compile unit DIE. The compile unit DIE is always the first entry, so
204 // if our size is 1, then we are adding the first compile unit child
205 // DIE and should reserve the memory.
206 m_die_array.reserve(GetDebugInfoSize() / 24);
207 m_die_array.push_back(die);
210 AddUnitDIE(m_die_array.front());
213 if (prev_die_had_children) {
214 // This will only happen if a DIE says is has children but all it
215 // contains is a NULL tag. Since we are removing the NULL DIEs from
216 // the list (saves up to 25% in C++ code), we need a way to let the
217 // DIE know that it actually doesn't have children.
218 if (!m_die_array.empty())
219 m_die_array.back().SetHasChildren(false);
222 die.SetParentIndex(m_die_array.size() - die_index_stack[depth - 1]);
224 if (die_index_stack.back())
225 m_die_array[die_index_stack.back()].SetSiblingIndex(
226 m_die_array.size() - die_index_stack.back());
228 // Only push the DIE if it isn't a NULL DIE
229 m_die_array.push_back(die);
235 if (!die_index_stack.empty())
236 die_index_stack.pop_back();
240 prev_die_had_children = false;
242 die_index_stack.back() = m_die_array.size() - 1;
244 const bool die_has_children = die.HasChildren();
245 if (die_has_children) {
246 die_index_stack.push_back(0);
249 prev_die_had_children = die_has_children;
253 break; // We are done with this compile unit!
256 if (!m_die_array.empty()) {
258 // Only needed for the assertion.
259 m_first_die.SetHasChildren(m_die_array.front().HasChildren());
260 lldbassert(m_first_die == m_die_array.front());
262 m_first_die = m_die_array.front();
265 m_die_array.shrink_to_fit();
267 ExtractDIEsEndCheck(offset);
269 if (m_dwo_symbol_file) {
270 DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
271 dwo_cu->ExtractDIEsIfNeeded();
275 //--------------------------------------------------------------------------
276 // Final checks for both ExtractUnitDIEIfNeeded() and ExtractDIEsIfNeeded().
277 //--------------------------------------------------------------------------
278 void DWARFUnit::ExtractDIEsEndCheck(lldb::offset_t offset) const {
279 // Give a little bit of info if we encounter corrupt DWARF (our offset should
280 // always terminate at or before the start of the next compilation unit
282 if (offset > GetNextCompileUnitOffset()) {
283 m_dwarf->GetObjectFile()->GetModule()->ReportWarning(
284 "DWARF compile unit extends beyond its bounds cu 0x%8.8x at "
285 "0x%8.8" PRIx64 "\n",
286 GetOffset(), offset);
289 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
290 if (log && log->GetVerbose()) {
293 if (m_die_array.empty())
294 strm.Printf("error: no DIE for compile unit");
296 m_die_array[0].Dump(m_dwarf, this, strm, UINT32_MAX);
297 log->PutString(strm.GetString());
301 // This is used when a split dwarf is enabled.
302 // A skeleton compilation unit may contain the DW_AT_str_offsets_base attribute
303 // that points to the first string offset of the CU contribution to the
304 // .debug_str_offsets. At the same time, the corresponding split debug unit also
305 // may use DW_FORM_strx* forms pointing to its own .debug_str_offsets.dwo and
306 // for that case, we should find the offset (skip the section header).
307 static void SetDwoStrOffsetsBase(DWARFUnit *dwo_cu) {
308 lldb::offset_t baseOffset = 0;
310 const DWARFDataExtractor &strOffsets =
311 dwo_cu->GetSymbolFileDWARF()->get_debug_str_offsets_data();
312 uint64_t length = strOffsets.GetU32(&baseOffset);
313 if (length == 0xffffffff)
314 length = strOffsets.GetU64(&baseOffset);
317 if (strOffsets.GetU16(&baseOffset) < 5)
323 dwo_cu->SetStrOffsetsBase(baseOffset);
326 // m_die_array_mutex must be already held as read/write.
327 void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) {
328 dw_addr_t addr_base = cu_die.GetAttributeValueAsUnsigned(
329 m_dwarf, this, DW_AT_addr_base, LLDB_INVALID_ADDRESS);
330 if (addr_base != LLDB_INVALID_ADDRESS)
331 SetAddrBase(addr_base);
333 dw_addr_t ranges_base = cu_die.GetAttributeValueAsUnsigned(
334 m_dwarf, this, DW_AT_rnglists_base, LLDB_INVALID_ADDRESS);
335 if (ranges_base != LLDB_INVALID_ADDRESS)
336 SetRangesBase(ranges_base);
338 SetStrOffsetsBase(cu_die.GetAttributeValueAsUnsigned(
339 m_dwarf, this, DW_AT_str_offsets_base, 0));
341 uint64_t base_addr = cu_die.GetAttributeValueAsAddress(
342 m_dwarf, this, DW_AT_low_pc, LLDB_INVALID_ADDRESS);
343 if (base_addr == LLDB_INVALID_ADDRESS)
344 base_addr = cu_die.GetAttributeValueAsAddress(
345 m_dwarf, this, DW_AT_entry_pc, 0);
346 SetBaseAddress(base_addr);
348 std::unique_ptr<SymbolFileDWARFDwo> dwo_symbol_file =
349 m_dwarf->GetDwoSymbolFileForCompileUnit(*this, cu_die);
350 if (!dwo_symbol_file)
353 DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
355 return; // Can't fetch the compile unit from the dwo file.
357 DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly();
358 if (!dwo_cu_die.IsValid())
359 return; // Can't fetch the compile unit DIE from the dwo file.
361 uint64_t main_dwo_id =
362 cu_die.GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_GNU_dwo_id, 0);
363 uint64_t sub_dwo_id =
364 dwo_cu_die.GetAttributeValueAsUnsigned(DW_AT_GNU_dwo_id, 0);
365 if (main_dwo_id != sub_dwo_id)
366 return; // The 2 dwo ID isn't match. Don't use the dwo file as it belongs to
367 // a differectn compilation.
369 m_dwo_symbol_file = std::move(dwo_symbol_file);
371 // Here for DWO CU we want to use the address base set in the skeleton unit
372 // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base
373 // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_*
374 // attributes which were applicable to the DWO units. The corresponding
375 // DW_AT_* attributes standardized in DWARF v5 are also applicable to the main
377 if (addr_base == LLDB_INVALID_ADDRESS)
378 addr_base = cu_die.GetAttributeValueAsUnsigned(m_dwarf, this,
379 DW_AT_GNU_addr_base, 0);
380 dwo_cu->SetAddrBase(addr_base);
382 if (ranges_base == LLDB_INVALID_ADDRESS)
383 ranges_base = cu_die.GetAttributeValueAsUnsigned(m_dwarf, this,
384 DW_AT_GNU_ranges_base, 0);
385 dwo_cu->SetRangesBase(ranges_base);
387 dwo_cu->SetBaseObjOffset(m_offset);
389 SetDwoStrOffsetsBase(dwo_cu);
392 DWARFDIE DWARFUnit::LookupAddress(const dw_addr_t address) {
394 const DWARFDebugAranges &func_aranges = GetFunctionAranges();
396 // Re-check the aranges auto pointer contents in case it was created above
397 if (!func_aranges.IsEmpty())
398 return GetDIE(func_aranges.FindAddress(address));
403 size_t DWARFUnit::AppendDIEsWithTag(const dw_tag_t tag,
404 DWARFDIECollection &dies,
405 uint32_t depth) const {
406 size_t old_size = dies.Size();
408 llvm::sys::ScopedReader lock(m_die_array_mutex);
409 DWARFDebugInfoEntry::const_iterator pos;
410 DWARFDebugInfoEntry::const_iterator end = m_die_array.end();
411 for (pos = m_die_array.begin(); pos != end; ++pos) {
412 if (pos->Tag() == tag)
413 dies.Append(DWARFDIE(this, &(*pos)));
417 // Return the number of DIEs added to the collection
418 return dies.Size() - old_size;
422 lldb::user_id_t DWARFUnit::GetID() const {
423 dw_offset_t local_id =
424 m_base_obj_offset != DW_INVALID_OFFSET ? m_base_obj_offset : m_offset;
426 return DIERef(local_id, local_id).GetUID(m_dwarf);
431 dw_offset_t DWARFUnit::GetNextCompileUnitOffset() const {
432 return m_offset + GetLengthByteSize() + GetLength();
435 size_t DWARFUnit::GetDebugInfoSize() const {
436 return GetLengthByteSize() + GetLength() - GetHeaderByteSize();
439 const DWARFAbbreviationDeclarationSet *DWARFUnit::GetAbbreviations() const {
443 dw_offset_t DWARFUnit::GetAbbrevOffset() const {
444 return m_abbrevs ? m_abbrevs->GetOffset() : DW_INVALID_OFFSET;
447 void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; }
449 void DWARFUnit::SetRangesBase(dw_addr_t ranges_base) {
450 m_ranges_base = ranges_base;
453 void DWARFUnit::SetBaseObjOffset(dw_offset_t base_obj_offset) {
454 m_base_obj_offset = base_obj_offset;
457 void DWARFUnit::SetStrOffsetsBase(dw_offset_t str_offsets_base) {
458 m_str_offsets_base = str_offsets_base;
461 // It may be called only with m_die_array_mutex held R/W.
462 void DWARFUnit::ClearDIEsRWLocked() {
464 m_die_array.shrink_to_fit();
466 if (m_dwo_symbol_file)
467 m_dwo_symbol_file->GetCompileUnit()->ClearDIEsRWLocked();
470 void DWARFUnit::BuildAddressRangeTable(SymbolFileDWARF *dwarf,
471 DWARFDebugAranges *debug_aranges) {
472 // This function is usually called if there in no .debug_aranges section in
473 // order to produce a compile unit level set of address ranges that is
476 size_t num_debug_aranges = debug_aranges->GetNumRanges();
478 // First get the compile unit DIE only and check if it has a DW_AT_ranges
479 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
481 const dw_offset_t cu_offset = GetOffset();
483 DWARFRangeList ranges;
484 const size_t num_ranges =
485 die->GetAttributeAddressRanges(dwarf, this, ranges, false);
486 if (num_ranges > 0) {
487 // This compile unit has DW_AT_ranges, assume this is correct if it is
488 // present since clang no longer makes .debug_aranges by default and it
489 // emits DW_AT_ranges for DW_TAG_compile_units. GCC also does this with
490 // recent GCC builds.
491 for (size_t i = 0; i < num_ranges; ++i) {
492 const DWARFRangeList::Entry &range = ranges.GetEntryRef(i);
493 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
494 range.GetRangeEnd());
497 return; // We got all of our ranges from the DW_AT_ranges attribute
500 // We don't have a DW_AT_ranges attribute, so we need to parse the DWARF
502 // If the DIEs weren't parsed, then we don't want all dies for all compile
503 // units to stay loaded when they weren't needed. So we can end up parsing
504 // the DWARF and then throwing them all away to keep memory usage down.
505 ScopedExtractDIEs clear_dies(ExtractDIEsScoped());
509 die->BuildAddressRangeTable(dwarf, this, debug_aranges);
511 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
512 // We got nothing from the functions, maybe we have a line tables only
513 // situation. Check the line tables and build the arange table from this.
515 sc.comp_unit = dwarf->GetCompUnitForDWARFCompUnit(this);
517 SymbolFileDWARFDebugMap *debug_map_sym_file =
518 m_dwarf->GetDebugMapSymfile();
519 if (debug_map_sym_file == NULL) {
520 LineTable *line_table = sc.comp_unit->GetLineTable();
523 LineTable::FileAddressRanges file_ranges;
524 const bool append = true;
525 const size_t num_ranges =
526 line_table->GetContiguousFileAddressRanges(file_ranges, append);
527 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
528 const LineTable::FileAddressRanges::Entry &range =
529 file_ranges.GetEntryRef(idx);
530 debug_aranges->AppendRange(cu_offset, range.GetRangeBase(),
531 range.GetRangeEnd());
535 debug_map_sym_file->AddOSOARanges(dwarf, debug_aranges);
539 if (debug_aranges->GetNumRanges() == num_debug_aranges) {
540 // We got nothing from the functions, maybe we have a line tables only
541 // situation. Check the line tables and build the arange table from this.
543 sc.comp_unit = dwarf->GetCompUnitForDWARFCompUnit(this);
545 LineTable *line_table = sc.comp_unit->GetLineTable();
548 LineTable::FileAddressRanges file_ranges;
549 const bool append = true;
550 const size_t num_ranges =
551 line_table->GetContiguousFileAddressRanges(file_ranges, append);
552 for (uint32_t idx = 0; idx < num_ranges; ++idx) {
553 const LineTable::FileAddressRanges::Entry &range =
554 file_ranges.GetEntryRef(idx);
555 debug_aranges->AppendRange(GetOffset(), range.GetRangeBase(),
556 range.GetRangeEnd());
563 lldb::ByteOrder DWARFUnit::GetByteOrder() const {
564 return m_dwarf->GetObjectFile()->GetByteOrder();
567 TypeSystem *DWARFUnit::GetTypeSystem() {
569 return m_dwarf->GetTypeSystemForLanguage(GetLanguageType());
574 DWARFFormValue::FixedFormSizes DWARFUnit::GetFixedFormSizes() {
575 return DWARFFormValue::GetFixedFormSizesForAddressSize(GetAddressByteSize(),
579 void DWARFUnit::SetBaseAddress(dw_addr_t base_addr) { m_base_addr = base_addr; }
581 //----------------------------------------------------------------------
582 // Compare function DWARFDebugAranges::Range structures
583 //----------------------------------------------------------------------
584 static bool CompareDIEOffset(const DWARFDebugInfoEntry &die,
585 const dw_offset_t die_offset) {
586 return die.GetOffset() < die_offset;
589 //----------------------------------------------------------------------
592 // Get the DIE (Debug Information Entry) with the specified offset by first
593 // checking if the DIE is contained within this compile unit and grabbing the
594 // DIE from this compile unit. Otherwise we grab the DIE from the DWARF file.
595 //----------------------------------------------------------------------
597 DWARFUnit::GetDIE(dw_offset_t die_offset) {
598 if (die_offset != DW_INVALID_OFFSET) {
599 if (GetDwoSymbolFile())
600 return GetDwoSymbolFile()->GetCompileUnit()->GetDIE(die_offset);
602 if (ContainsDIEOffset(die_offset)) {
603 ExtractDIEsIfNeeded();
604 DWARFDebugInfoEntry::const_iterator end = m_die_array.cend();
605 DWARFDebugInfoEntry::const_iterator pos =
606 lower_bound(m_die_array.cbegin(), end, die_offset, CompareDIEOffset);
608 if (die_offset == (*pos).GetOffset())
609 return DWARFDIE(this, &(*pos));
612 // Don't specify the compile unit offset as we don't know it because the
614 // a different compile unit in the same symbol file.
615 return m_dwarf->DebugInfo()->GetDIEForDIEOffset(die_offset);
618 return DWARFDIE(); // Not found
621 uint8_t DWARFUnit::GetAddressByteSize(const DWARFUnit *cu) {
623 return cu->GetAddressByteSize();
624 return DWARFUnit::GetDefaultAddressSize();
627 bool DWARFUnit::IsDWARF64(const DWARFUnit *cu) {
629 return cu->IsDWARF64();
633 uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
635 void *DWARFUnit::GetUserData() const { return m_user_data; }
637 void DWARFUnit::SetUserData(void *d) {
639 if (m_dwo_symbol_file)
640 m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
643 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
644 return GetProducer() != eProducerLLVMGCC;
647 bool DWARFUnit::DW_AT_decl_file_attributes_are_invalid() {
648 // llvm-gcc makes completely invalid decl file attributes and won't ever be
649 // fixed, so we need to know to ignore these.
650 return GetProducer() == eProducerLLVMGCC;
653 bool DWARFUnit::Supports_unnamed_objc_bitfields() {
654 if (GetProducer() == eProducerClang) {
655 const uint32_t major_version = GetProducerVersionMajor();
656 return major_version > 425 ||
657 (major_version == 425 && GetProducerVersionUpdate() >= 13);
659 return true; // Assume all other compilers didn't have incorrect ObjC bitfield
663 SymbolFileDWARF *DWARFUnit::GetSymbolFileDWARF() const { return m_dwarf; }
665 void DWARFUnit::ParseProducerInfo() {
666 m_producer_version_major = UINT32_MAX;
667 m_producer_version_minor = UINT32_MAX;
668 m_producer_version_update = UINT32_MAX;
670 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
673 const char *producer_cstr =
674 die->GetAttributeValueAsString(m_dwarf, this, DW_AT_producer, NULL);
676 RegularExpression llvm_gcc_regex(
677 llvm::StringRef("^4\\.[012]\\.[01] \\(Based on Apple "
678 "Inc\\. build [0-9]+\\) \\(LLVM build "
680 if (llvm_gcc_regex.Execute(llvm::StringRef(producer_cstr))) {
681 m_producer = eProducerLLVMGCC;
682 } else if (strstr(producer_cstr, "clang")) {
683 static RegularExpression g_clang_version_regex(
684 llvm::StringRef("clang-([0-9]+)\\.([0-9]+)\\.([0-9]+)"));
685 RegularExpression::Match regex_match(3);
686 if (g_clang_version_regex.Execute(llvm::StringRef(producer_cstr),
689 if (regex_match.GetMatchAtIndex(producer_cstr, 1, str))
690 m_producer_version_major =
691 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
692 if (regex_match.GetMatchAtIndex(producer_cstr, 2, str))
693 m_producer_version_minor =
694 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
695 if (regex_match.GetMatchAtIndex(producer_cstr, 3, str))
696 m_producer_version_update =
697 StringConvert::ToUInt32(str.c_str(), UINT32_MAX, 10);
699 m_producer = eProducerClang;
700 } else if (strstr(producer_cstr, "GNU"))
701 m_producer = eProducerGCC;
704 if (m_producer == eProducerInvalid)
705 m_producer = eProcucerOther;
708 DWARFProducer DWARFUnit::GetProducer() {
709 if (m_producer == eProducerInvalid)
714 uint32_t DWARFUnit::GetProducerVersionMajor() {
715 if (m_producer_version_major == 0)
717 return m_producer_version_major;
720 uint32_t DWARFUnit::GetProducerVersionMinor() {
721 if (m_producer_version_minor == 0)
723 return m_producer_version_minor;
726 uint32_t DWARFUnit::GetProducerVersionUpdate() {
727 if (m_producer_version_update == 0)
729 return m_producer_version_update;
731 LanguageType DWARFUnit::LanguageTypeFromDWARF(uint64_t val) {
732 // Note: user languages between lo_user and hi_user must be handled
735 case DW_LANG_Mips_Assembler:
736 return eLanguageTypeMipsAssembler;
737 case DW_LANG_GOOGLE_RenderScript:
738 return eLanguageTypeExtRenderScript;
740 return static_cast<LanguageType>(val);
744 LanguageType DWARFUnit::GetLanguageType() {
745 if (m_language_type != eLanguageTypeUnknown)
746 return m_language_type;
748 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
750 m_language_type = LanguageTypeFromDWARF(
751 die->GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_language, 0));
752 return m_language_type;
755 bool DWARFUnit::GetIsOptimized() {
756 if (m_is_optimized == eLazyBoolCalculate) {
757 const DWARFDebugInfoEntry *die = GetUnitDIEPtrOnly();
759 m_is_optimized = eLazyBoolNo;
760 if (die->GetAttributeValueAsUnsigned(m_dwarf, this, DW_AT_APPLE_optimized,
762 m_is_optimized = eLazyBoolYes;
766 return m_is_optimized == eLazyBoolYes;
769 SymbolFileDWARFDwo *DWARFUnit::GetDwoSymbolFile() const {
770 return m_dwo_symbol_file.get();
773 dw_offset_t DWARFUnit::GetBaseObjOffset() const { return m_base_obj_offset; }
775 const DWARFDebugAranges &DWARFUnit::GetFunctionAranges() {
776 if (m_func_aranges_ap.get() == NULL) {
777 m_func_aranges_ap.reset(new DWARFDebugAranges());
778 Log *log(LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_ARANGES));
781 m_dwarf->GetObjectFile()->GetModule()->LogMessage(
783 "DWARFUnit::GetFunctionAranges() for compile unit at "
784 ".debug_info[0x%8.8x]",
787 const DWARFDebugInfoEntry *die = DIEPtr();
789 die->BuildFunctionAddressRangeTable(m_dwarf, this,
790 m_func_aranges_ap.get());
792 if (m_dwo_symbol_file) {
793 DWARFUnit *dwo_cu = m_dwo_symbol_file->GetCompileUnit();
794 const DWARFDebugInfoEntry *dwo_die = dwo_cu->DIEPtr();
796 dwo_die->BuildFunctionAddressRangeTable(m_dwo_symbol_file.get(), dwo_cu,
797 m_func_aranges_ap.get());
800 const bool minimize = false;
801 m_func_aranges_ap->Sort(minimize);
803 return *m_func_aranges_ap.get();