[lldb/DWARF] Don't assume that a SymbolFileDWARFDwo contains one compile unit
authorPavel Labath <pavel@labath.sk>
Thu, 6 Feb 2020 03:45:22 +0000 (19:45 -0800)
committerPavel Labath <labath@google.com>
Thu, 6 Feb 2020 04:37:56 +0000 (20:37 -0800)
Summary:
This is a preparatory patch to re-enable DWP support in lldb (we already
have code claiming to do that, but it has been completely broken for a
while now).

The idea of the new approach is to make the SymbolFileDWARFDwo class
handle both dwo and dwo files, similar to how llvm uses one DWARFContext
to handle the two.

The first step is to remove the assumption that a SymbolFileDWARFDwo
holds just a single compile unit, i.e. the GetBaseCompileUnit method.
This requires changing the way how we reach the skeleton compile unit
(and the lldb_private::CompileUnit) from a dwo unit, which was
previously done via GetSymbolFile()->GetBaseCompileUnit() (and some
virtual dispatch).

The new approach reuses the "user data" mechanism of DWARFUnits, which
was used to link dwarf units (both skeleton and split) to their
lldb_private counterparts. Now, this is done only for non-dwo units, and
instead of that, the dwo units holds a pointer to the relevant skeleton
unit.

Reviewers: JDevlieghere, aprantl, clayborg

Reviewed By: JDevlieghere, clayborg

Subscribers: arphaman, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D73781

lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
lldb/source/Plugins/SymbolFile/DWARF/ManualDWARFIndex.cpp
lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp
lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.h
lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwoDwp.cpp

index 1c96968..8944078 100644 (file)
@@ -347,6 +347,7 @@ void DWARFUnit::AddUnitDIE(const DWARFDebugInfoEntry &cu_die) {
   DWARFUnit *dwo_cu = dwo_symbol_file->GetCompileUnit();
   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())
@@ -563,11 +564,7 @@ uint8_t DWARFUnit::GetDefaultAddressSize() { return 4; }
 
 void *DWARFUnit::GetUserData() const { return m_user_data; }
 
-void DWARFUnit::SetUserData(void *d) {
-  m_user_data = d;
-  if (m_dwo_symbol_file)
-    m_dwo_symbol_file->GetCompileUnit()->SetUserData(d);
-}
+void DWARFUnit::SetUserData(void *d) { m_user_data = d; }
 
 bool DWARFUnit::Supports_DW_AT_APPLE_objc_complete_type() {
   return GetProducer() != eProducerLLVMGCC;
index 78b1e3b..73edf62 100644 (file)
@@ -79,6 +79,8 @@ public:
           DIERef::Section section, lldb::offset_t *offset_ptr);
   virtual ~DWARFUnit();
 
+  bool IsDWOUnit() { return m_is_dwo; }
+
   void ExtractUnitDIEIfNeeded();
   void ExtractDIEsIfNeeded();
 
index 0d0857e..c86149d 100644 (file)
@@ -89,7 +89,7 @@ void ManualDWARFIndex::Index() {
 
 void ManualDWARFIndex::IndexUnit(DWARFUnit &unit, IndexSet &set) {
   assert(
-      !unit.GetSymbolFileDWARF().GetBaseCompileUnit() &&
+      !unit.IsDWOUnit() &&
       "DWARFUnit associated with .dwo or .dwp should not be indexed directly");
 
   Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS);
index 2d7ffde..02fb37f 100644 (file)
@@ -645,9 +645,7 @@ lldb::CompUnitSP SymbolFileDWARF::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
     // We already parsed this compile unit, had out a shared pointer to it
     cu_sp = comp_unit->shared_from_this();
   } else {
-    if (&dwarf_cu.GetSymbolFileDWARF() != this) {
-      return dwarf_cu.GetSymbolFileDWARF().ParseCompileUnit(dwarf_cu);
-    } else if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
+    if (dwarf_cu.GetOffset() == 0 && GetDebugMapSymfile()) {
       // Let the debug map create the compile unit
       cu_sp = m_debug_map_symfile->GetCompileUnit(this);
       dwarf_cu.SetUserData(cu_sp.get());
@@ -1454,13 +1452,17 @@ Type *SymbolFileDWARF::ResolveType(const DWARFDIE &die,
 
 CompileUnit *
 SymbolFileDWARF::GetCompUnitForDWARFCompUnit(DWARFCompileUnit &dwarf_cu) {
+  DWARFCompileUnit *non_dwo_cu =
+      dwarf_cu.IsDWOUnit()
+          ? static_cast<DWARFCompileUnit *>(dwarf_cu.GetUserData())
+          : &dwarf_cu;
   // Check if the symbol vendor already knows about this compile unit?
-  if (dwarf_cu.GetUserData() == nullptr) {
+  if (non_dwo_cu->GetUserData() == nullptr) {
     // The symbol vendor doesn't know about this compile unit, we need to parse
     // and add it to the symbol vendor object.
-    return ParseCompileUnit(dwarf_cu).get();
+    return ParseCompileUnit(*non_dwo_cu).get();
   }
-  return (CompileUnit *)dwarf_cu.GetUserData();
+  return static_cast<CompileUnit *>(non_dwo_cu->GetUserData());
 }
 
 size_t SymbolFileDWARF::GetObjCMethodDIEOffsets(ConstString class_name,
@@ -1605,7 +1607,8 @@ SymbolFileDWARF::GetDwoSymbolFileForCompileUnit(
   if (dwo_obj_file == nullptr)
     return nullptr;
 
-  return std::make_unique<SymbolFileDWARFDwo>(dwo_obj_file, *dwarf_cu);
+  return std::make_unique<SymbolFileDWARFDwo>(*this, dwo_obj_file,
+                                              dwarf_cu->GetID());
 }
 
 void SymbolFileDWARF::UpdateExternalModuleListIfNeeded() {
index 4510efc..9e53382 100644 (file)
@@ -246,8 +246,6 @@ public:
 
   static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die);
 
-  virtual lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu);
-
   lldb::ModuleSP GetExternalModule(lldb_private::ConstString name);
 
   typedef std::map<lldb_private::ConstString, lldb::ModuleSP>
@@ -276,11 +274,6 @@ public:
   GetDwoSymbolFileForCompileUnit(DWARFUnit &dwarf_cu,
                                  const DWARFDebugInfoEntry &cu_die);
 
-  // For regular SymbolFileDWARF instances the method returns nullptr,
-  // for the instances of the subclass SymbolFileDWARFDwo
-  // the method returns a pointer to the base compile unit.
-  virtual DWARFCompileUnit *GetBaseCompileUnit() { return nullptr; }
-
   virtual llvm::Optional<uint32_t> GetDwoNum() { return llvm::None; }
 
   /// If this is a DWARF object with a single CU, return its DW_AT_dwo_id.
@@ -347,6 +340,8 @@ protected:
 
   lldb_private::TypeList &GetTypeList() override;
 
+  lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu);
+
   virtual DWARFUnit *
   GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit);
 
index 1886412..d7d1b03 100644 (file)
@@ -23,12 +23,12 @@ using namespace lldb_private;
 
 char SymbolFileDWARFDwo::ID;
 
-SymbolFileDWARFDwo::SymbolFileDWARFDwo(ObjectFileSP objfile,
-                                       DWARFCompileUnit &dwarf_cu)
+SymbolFileDWARFDwo::SymbolFileDWARFDwo(SymbolFileDWARF &base_symbol_file,
+                                       ObjectFileSP objfile, uint32_t id)
     : SymbolFileDWARF(objfile, objfile->GetSectionList(
                                    /*update_module_section_list*/ false)),
-      m_base_dwarf_cu(dwarf_cu) {
-  SetID(((lldb::user_id_t)dwarf_cu.GetID()) << 32);
+      m_base_symbol_file(base_symbol_file) {
+  SetID(user_id_t(id) << 32);
 }
 
 void SymbolFileDWARFDwo::LoadSectionData(lldb::SectionType sect_type,
@@ -49,14 +49,6 @@ void SymbolFileDWARFDwo::LoadSectionData(lldb::SectionType sect_type,
   SymbolFileDWARF::LoadSectionData(sect_type, data);
 }
 
-lldb::CompUnitSP
-SymbolFileDWARFDwo::ParseCompileUnit(DWARFCompileUnit &dwarf_cu) {
-  assert(GetCompileUnit() == &dwarf_cu &&
-         "SymbolFileDWARFDwo::ParseCompileUnit called with incompatible "
-         "compile unit");
-  return GetBaseSymbolFile().ParseCompileUnit(m_base_dwarf_cu);
-}
-
 DWARFCompileUnit *SymbolFileDWARFDwo::GetCompileUnit() {
   if (!m_cu)
     m_cu = ComputeCompileUnit();
@@ -133,10 +125,6 @@ lldb::TypeSP SymbolFileDWARFDwo::FindCompleteObjCDefinitionTypeForDIE(
       die, type_name, must_be_implementation);
 }
 
-SymbolFileDWARF &SymbolFileDWARFDwo::GetBaseSymbolFile() {
-  return m_base_dwarf_cu.GetSymbolFileDWARF();
-}
-
 llvm::Expected<TypeSystem &>
 SymbolFileDWARFDwo::GetTypeSystemForLanguage(LanguageType language) {
   return GetBaseSymbolFile().GetTypeSystemForLanguage(language);
index 0855dba..1002d0b 100644 (file)
@@ -24,12 +24,11 @@ public:
   static bool classof(const SymbolFile *obj) { return obj->isA(&ID); }
   /// \}
 
-  SymbolFileDWARFDwo(lldb::ObjectFileSP objfile, DWARFCompileUnit &dwarf_cu);
+  SymbolFileDWARFDwo(SymbolFileDWARF &m_base_symbol_file,
+                     lldb::ObjectFileSP objfile, uint32_t id);
 
   ~SymbolFileDWARFDwo() override = default;
 
-  lldb::CompUnitSP ParseCompileUnit(DWARFCompileUnit &dwarf_cu) override;
-
   DWARFCompileUnit *GetCompileUnit();
 
   DWARFUnit *
@@ -44,8 +43,6 @@ public:
   DWARFDIE
   GetDIE(const DIERef &die_ref) override;
 
-  DWARFCompileUnit *GetBaseCompileUnit() override { return &m_base_dwarf_cu; }
-
   llvm::Optional<uint32_t> GetDwoNum() override { return GetID() >> 32; }
 
 protected:
@@ -69,11 +66,11 @@ protected:
       const DWARFDIE &die, lldb_private::ConstString type_name,
       bool must_be_implementation) override;
 
-  SymbolFileDWARF &GetBaseSymbolFile();
+  SymbolFileDWARF &GetBaseSymbolFile() { return m_base_symbol_file; }
 
   DWARFCompileUnit *ComputeCompileUnit();
 
-  DWARFCompileUnit &m_base_dwarf_cu;
+  SymbolFileDWARF &m_base_symbol_file;
   DWARFCompileUnit *m_cu = nullptr;
 };
 
index 61faead..05c7e5c 100644 (file)
@@ -13,7 +13,7 @@
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Utility/LLDBAssert.h"
 
-#include "DWARFUnit.h"
+#include "DWARFCompileUnit.h"
 #include "DWARFDebugInfo.h"
 
 using namespace lldb;
@@ -25,8 +25,9 @@ SymbolFileDWARFDwoDwp::SymbolFileDWARFDwoDwp(SymbolFileDWARFDwp *dwp_symfile,
                                              ObjectFileSP objfile,
                                              DWARFCompileUnit &dwarf_cu,
                                              uint64_t dwo_id)
-    : SymbolFileDWARFDwo(objfile, dwarf_cu), m_dwp_symfile(dwp_symfile),
-      m_dwo_id(dwo_id) {}
+    : SymbolFileDWARFDwo(dwarf_cu.GetSymbolFileDWARF(), objfile,
+                         dwarf_cu.GetID()),
+      m_dwp_symfile(dwp_symfile), m_dwo_id(dwo_id) {}
 
 void SymbolFileDWARFDwoDwp::LoadSectionData(lldb::SectionType sect_type,
                                             DWARFDataExtractor &data) {