Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h +++ lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h @@ -92,6 +92,7 @@ uint64_t GetDWOId(); void ExtractUnitDIEIfNeeded(); + void ExtractUnitDIENoDwoIfNeeded(); void ExtractDIEsIfNeeded(); class ScopedExtractDIEs { @@ -151,7 +152,7 @@ const DWARFAbbreviationDeclarationSet *GetAbbreviations() const; dw_offset_t GetAbbrevOffset() const; uint8_t GetAddressByteSize() const { return m_header.GetAddressByteSize(); } - dw_addr_t GetAddrBase() const { return m_addr_base; } + dw_addr_t GetAddrBase() const { return m_addr_base ? *m_addr_base : 0; } dw_addr_t GetBaseAddress() const { return m_base_addr; } dw_offset_t GetLineTableOffset(); dw_addr_t GetRangesBase() const { return m_ranges_base; } @@ -268,7 +269,7 @@ // Get the DWARF unit DWARF debug information entry. Parse the single DIE // if needed. const DWARFDebugInfoEntry *GetUnitDIEPtrOnly() { - ExtractUnitDIEIfNeeded(); + ExtractUnitDIENoDwoIfNeeded(); // m_first_die_mutex is not required as m_first_die is never cleared. if (!m_first_die) return NULL; @@ -315,9 +316,11 @@ lldb_private::LazyBool m_is_optimized = lldb_private::eLazyBoolCalculate; llvm::Optional m_comp_dir; llvm::Optional m_file_spec; - dw_addr_t m_addr_base = 0; ///< Value of DW_AT_addr_base. - dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base. - dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base. + llvm::Optional m_addr_base; ///< Value of DW_AT_addr_base. + dw_addr_t m_loclists_base = 0; ///< Value of DW_AT_loclists_base. + dw_addr_t m_ranges_base = 0; ///< Value of DW_AT_rnglists_base. + llvm::Optional m_gnu_addr_base; + llvm::Optional m_gnu_ranges_base; /// Value of DW_AT_stmt_list. dw_offset_t m_line_table_offset = DW_INVALID_OFFSET; @@ -330,6 +333,7 @@ const DIERef::Section m_section; bool m_is_dwo; + bool m_has_parsed_non_skeleton_unit; /// Value of DW_AT_GNU_dwo_id (v4) or dwo_id from CU header (v5). uint64_t m_dwo_id; Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp +++ lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp @@ -35,12 +35,12 @@ DIERef::Section section, bool is_dwo) : UserID(uid), m_dwarf(dwarf), m_header(header), m_abbrevs(&abbrevs), m_cancel_scopes(false), m_section(section), m_is_dwo(is_dwo), - m_dwo_id(header.GetDWOId()) {} + m_has_parsed_non_skeleton_unit(false), m_dwo_id(header.GetDWOId()) {} DWARFUnit::~DWARFUnit() = default; -// Parses first DIE of a compile unit. -void DWARFUnit::ExtractUnitDIEIfNeeded() { +// Parses first DIE of a compile unit, excluding DWO. +void DWARFUnit::ExtractUnitDIENoDwoIfNeeded() { { llvm::sys::ScopedReader lock(m_first_die_mutex); if (m_first_die) @@ -66,6 +66,58 @@ } } +// Parses first DIE of a compile unit including DWO. +void DWARFUnit::ExtractUnitDIEIfNeeded() { + ExtractUnitDIENoDwoIfNeeded(); + + if (m_has_parsed_non_skeleton_unit) + return; + + m_has_parsed_non_skeleton_unit = true; + + std::shared_ptr dwo_symbol_file = + m_dwarf.GetDwoSymbolFileForCompileUnit(*this, m_first_die); + if (!dwo_symbol_file) + return; + + DWARFUnit *dwo_cu = dwo_symbol_file->GetDWOCompileUnitForHash(m_dwo_id); + + if (!dwo_cu) + return; // Can't fetch the compile unit from the dwo file. + dwo_cu->SetUserData(this); + + DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly(); + if (!dwo_cu_die.IsValid()) + return; // Can't fetch the compile unit DIE from the dwo file. + + // Here for DWO CU we want to use the address base set in the skeleton unit + // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base + // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_* + // attributes which were applicable to the DWO units. The corresponding + // DW_AT_* attributes standardized in DWARF v5 are also applicable to the + // main unit in contrast. + if (m_addr_base) + dwo_cu->SetAddrBase(*m_addr_base); + else if (m_gnu_addr_base) + dwo_cu->SetAddrBase(*m_gnu_addr_base); + + if (GetVersion() <= 4 && m_gnu_ranges_base) + dwo_cu->SetRangesBase(*m_gnu_ranges_base); + else if (dwo_symbol_file->GetDWARFContext() + .getOrLoadRngListsData() + .GetByteSize() > 0) + dwo_cu->SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32)); + + if (GetVersion() >= 5 && + dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() > + 0) + dwo_cu->SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32)); + + dwo_cu->SetBaseAddress(GetBaseAddress()); + + m_dwo = std::shared_ptr(std::move(dwo_symbol_file), dwo_cu); +} + // Parses a compile unit and indexes its DIEs if it hasn't already been done. // It will leave this compile unit extracted forever. void DWARFUnit::ExtractDIEsIfNeeded() { @@ -291,14 +343,12 @@ } uint64_t DWARFUnit::GetDWOId() { - ExtractUnitDIEIfNeeded(); + ExtractUnitDIENoDwoIfNeeded(); return m_dwo_id; } // m_die_array_mutex must be already held as read/write. void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) { - llvm::Optional addr_base, gnu_addr_base, gnu_ranges_base; - DWARFAttributes attributes; size_t num_attributes = cu_die.GetAttributes(this, attributes); @@ -308,8 +358,7 @@ continue; DWARFFormValue form_value; if (attributes.ExtractFormValueAtIndex(i, form_value)) { - addr_base = form_value.Unsigned(); - SetAddrBase(*addr_base); + SetAddrBase(form_value.Unsigned()); break; } } @@ -341,10 +390,10 @@ m_line_table_offset = form_value.Unsigned(); break; case DW_AT_GNU_addr_base: - gnu_addr_base = form_value.Unsigned(); + m_gnu_addr_base = form_value.Unsigned(); break; case DW_AT_GNU_ranges_base: - gnu_ranges_base = form_value.Unsigned(); + m_gnu_ranges_base = form_value.Unsigned(); break; case DW_AT_GNU_dwo_id: m_dwo_id = form_value.Unsigned(); @@ -353,50 +402,10 @@ } if (m_is_dwo) { + m_has_parsed_non_skeleton_unit = true; SetDwoStrOffsetsBase(); return; } - - std::shared_ptr dwo_symbol_file = - m_dwarf.GetDwoSymbolFileForCompileUnit(*this, cu_die); - if (!dwo_symbol_file) - return; - - DWARFUnit *dwo_cu = dwo_symbol_file->GetDWOCompileUnitForHash(m_dwo_id); - - if (!dwo_cu) - return; // Can't fetch the compile unit from the dwo file. - dwo_cu->SetUserData(this); - - DWARFBaseDIE dwo_cu_die = dwo_cu->GetUnitDIEOnly(); - if (!dwo_cu_die.IsValid()) - return; // Can't fetch the compile unit DIE from the dwo file. - - // Here for DWO CU we want to use the address base set in the skeleton unit - // (DW_AT_addr_base) if it is available and use the DW_AT_GNU_addr_base - // otherwise. We do that because pre-DWARF v5 could use the DW_AT_GNU_* - // attributes which were applicable to the DWO units. The corresponding - // DW_AT_* attributes standardized in DWARF v5 are also applicable to the main - // unit in contrast. - if (addr_base) - dwo_cu->SetAddrBase(*addr_base); - else if (gnu_addr_base) - dwo_cu->SetAddrBase(*gnu_addr_base); - - if (GetVersion() <= 4 && gnu_ranges_base) - dwo_cu->SetRangesBase(*gnu_ranges_base); - else if (dwo_symbol_file->GetDWARFContext() - .getOrLoadRngListsData() - .GetByteSize() > 0) - dwo_cu->SetRangesBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32)); - - if (GetVersion() >= 5 && - dwo_symbol_file->GetDWARFContext().getOrLoadLocListsData().GetByteSize() > - 0) - dwo_cu->SetLoclistsBase(llvm::DWARFListTableHeader::getHeaderSize(DWARF32)); - dwo_cu->SetBaseAddress(GetBaseAddress()); - - m_dwo = std::shared_ptr(std::move(dwo_symbol_file), dwo_cu); } size_t DWARFUnit::GetDebugInfoSize() const { @@ -412,11 +421,13 @@ } dw_offset_t DWARFUnit::GetLineTableOffset() { - ExtractUnitDIEIfNeeded(); + ExtractUnitDIENoDwoIfNeeded(); return m_line_table_offset; } -void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { m_addr_base = addr_base; } +void DWARFUnit::SetAddrBase(dw_addr_t addr_base) { + m_addr_base = addr_base; +} // Parse the rangelist table header, including the optional array of offsets // following it (DWARF v5 and later). Index: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h +++ lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h @@ -368,6 +368,9 @@ lldb::TypeSP ParseType(const lldb_private::SymbolContext &sc, const DWARFDIE &die, bool *type_is_new); + bool ParseSupportFiles(DWARFUnit &dwarf_cu, const lldb::ModuleSP &module, + lldb_private::FileSpecList &support_files); + lldb_private::Type *ResolveTypeUID(const DWARFDIE &die, bool assert_not_being_parsed); Index: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp =================================================================== --- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp +++ lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp @@ -702,25 +702,47 @@ } else { ModuleSP module_sp(m_objfile_sp->GetModule()); if (module_sp) { - const DWARFBaseDIE cu_die = - dwarf_cu.GetNonSkeletonUnit().GetUnitDIEOnly(); - if (cu_die) { - FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle()); - MakeAbsoluteAndRemap(cu_file_spec, dwarf_cu, module_sp); + bool need_non_skeleton = true; + auto InitializeCU = [&](const FileSpec &file_spec, + LazyBool is_optimized) { LanguageType cu_language = SymbolFileDWARF::LanguageTypeFromDWARF( - cu_die.GetAttributeValueAsUnsigned(DW_AT_language, 0)); + dwarf_cu.GetDWARFLanguageType()); - bool is_optimized = dwarf_cu.GetNonSkeletonUnit().GetIsOptimized(); BuildCuTranslationTable(); cu_sp = std::make_shared( - module_sp, &dwarf_cu, cu_file_spec, - *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language, - is_optimized ? eLazyBoolYes : eLazyBoolNo); + module_sp, &dwarf_cu, file_spec, + *GetDWARFUnitIndex(dwarf_cu.GetID()), cu_language, is_optimized); dwarf_cu.SetUserData(cu_sp.get()); SetCompileUnitAtIndex(dwarf_cu.GetID(), cu_sp); + }; + if (dwarf_cu.GetVersion() >= 5) { + // With DWARFv5 we can assume that the first support + // file is also the name of the compile unit. This + // allows us to avoid loading the non-skeleton unit, + // which may be in a separate DWO file. + FileSpecList support_files; + if (ParseSupportFiles(dwarf_cu, module_sp, support_files) && + support_files.GetSize() > 0) { + InitializeCU(support_files.GetFileSpecAtIndex(0), + eLazyBoolCalculate); + cu_sp->SetSupportFiles(std::move(support_files)); + need_non_skeleton = false; + } + } + if (need_non_skeleton) { + const DWARFBaseDIE cu_die = + dwarf_cu.GetNonSkeletonUnit().GetUnitDIEOnly(); + if (cu_die) { + FileSpec cu_file_spec(cu_die.GetName(), dwarf_cu.GetPathStyle()); + MakeAbsoluteAndRemap(cu_file_spec, dwarf_cu, module_sp); + + bool is_optimized = dwarf_cu.GetNonSkeletonUnit().GetIsOptimized(); + InitializeCU(cu_file_spec, + is_optimized ? eLazyBoolYes : eLazyBoolNo); + } } } } @@ -808,7 +830,7 @@ std::lock_guard guard(GetModuleMutex()); DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); if (dwarf_cu) - return GetLanguage(*dwarf_cu); + return GetLanguage(dwarf_cu->GetNonSkeletonUnit()); else return eLanguageTypeUnknown; } @@ -899,18 +921,29 @@ if (!dwarf_cu) return false; - dw_offset_t offset = dwarf_cu->GetLineTableOffset(); + if (!ParseSupportFiles(*dwarf_cu, comp_unit.GetModule(), support_files)) + return false; + + comp_unit.SetSupportFiles(support_files); + return true; +} + +bool SymbolFileDWARF::ParseSupportFiles(DWARFUnit &dwarf_cu, + const ModuleSP &module, + FileSpecList &support_files) { + + dw_offset_t offset = dwarf_cu.GetLineTableOffset(); if (offset == DW_INVALID_OFFSET) return false; llvm::DWARFDebugLine::Prologue prologue; if (!ParseLLVMLineTablePrologue(m_context, prologue, offset, - dwarf_cu->GetOffset())) + dwarf_cu.GetOffset())) return false; - comp_unit.SetSupportFiles(ParseSupportFilesFromPrologue( - comp_unit.GetModule(), prologue, dwarf_cu->GetPathStyle(), - dwarf_cu->GetCompilationDirectory().GetCString())); + support_files = ParseSupportFilesFromPrologue( + module, prologue, dwarf_cu.GetPathStyle(), + dwarf_cu.GetCompilationDirectory().GetCString()); return true; } @@ -966,7 +999,7 @@ std::lock_guard guard(GetModuleMutex()); DWARFUnit *dwarf_cu = GetDWARFCompileUnit(&comp_unit); if (dwarf_cu) - return dwarf_cu->GetIsOptimized(); + return dwarf_cu->GetNonSkeletonUnit().GetIsOptimized(); return false; } Index: lldb/test/Shell/SymbolFile/DWARF/x86/dwarf5-lazy-dwo.cpp =================================================================== --- /dev/null +++ lldb/test/Shell/SymbolFile/DWARF/x86/dwarf5-lazy-dwo.cpp @@ -0,0 +1,31 @@ +// Test that we can jump from a type unit in one dwo file into a type unit in a +// different dwo file. + +// REQUIRES: lld + +// RUN: %clang %s -target x86_64-pc-linux -fno-standalone-debug -g \ +// RUN: -gdwarf-5 -gpubnames -gsplit-dwarf -c -o %t1.o -DONE +// RUN: %clang %s -target x86_64-pc-linux -fno-standalone-debug -g \ +// RUN: -gdwarf-5 -gpubnames -gsplit-dwarf -c -o %t2.o -DTWO +// RUN: ld.lld %t1.o %t2.o -o %t +// RUN: %lldb %t -o "log enable ll""db object" -o "b main" -o "run" -o "image lookup -n main -v" -b | FileCheck %s + +// CHECK: (lldb) b main +// CHECK-NOT: 2.dwo, +// CHECK: 1.dwo, +// CHECK-NOT: 2.dwo, +// CHECK: (lldb) run +// CHECK-NOT: 2.dwo, +// CHECK: stop reason = breakpoint +// CHECK-NOT: 2.dwo, +// CHECK: (lldb) image lookup +// CHECK-NOT: 2.dwo, +// CHECK: CompileUnit: id = {0x00000000}, file = +// CHECK-SAME: language = "c++14" +// CHECK-NOT: 2.dwo, + +#ifdef ONE +int main() { return 0; } +#else +int x; +#endif Index: lldb/test/Shell/SymbolFile/DWARF/x86/split-optimized.cpp =================================================================== --- /dev/null +++ lldb/test/Shell/SymbolFile/DWARF/x86/split-optimized.cpp @@ -0,0 +1,17 @@ +// RUN: %clang %s -target x86_64-pc-linux -fno-standalone-debug -glldb \ +// RUN: -gdwarf-5 -gpubnames -gsplit-dwarf -O3 -c -o %t1.o + +// RUN: llvm-dwarfdump %t1.o | FileCheck %s --check-prefix DWARFDUMP_O +// RUN: llvm-dwarfdump %t1.dwo | FileCheck %s --check-prefix DWARFDUMP_DWO +// RUN: %lldb -b -o 'script lldb.SBDebugger.Create().CreateTarget("%t1.o").FindFunctions("main",lldb.eFunctionNameTypeAuto).GetContextAtIndex(0).GetFunction().GetIsOptimized()' | FileCheck %s + +// DWARFDUMP_O-NOT: DW_AT_APPLE_optimized +// +// DWARFDUMP_DWO: DW_TAG_compile_unit +// DWARFDUMP_DWO-NOT: DW_TAG_ +// DWARFDUMP_DWO: DW_AT_APPLE_optimized (true) + +// CHECK: (lldb) script lldb.SBDebugger.Create() +// CHECK-NEXT: True + +int main(void) { return 0; }