jankratochvil created this revision.
jankratochvil added reviewers: clayborg, labath, JDevlieghere.
jankratochvil added a project: LLDB.
Herald added subscribers: teemperor, mgorny.
jankratochvil added a parent revision: D61504: 05/06: Track CUs and TUs 
separately.

It is a rebased patch D32167 <https://reviews.llvm.org/D32167> by @clayborg.

DWO still does work (no regression), .debug_types also work now but 
DWO+.debug_types does not work together (while it does work for example in 
GDB). I have a work in progress as an additional patch for that, not yet 
finished. DWO+.debug_types has no testcase now so there is no new FAIL testcase.

The whole patchset available for testing as: `git clone -b debugtypes 
git://git.jankratochvil.net/lldb`


Repository:
  rLLDB LLDB

https://reviews.llvm.org/D61505

Files:
  lldb/packages/Python/lldbsuite/test/lldbtest.py
  lldb/packages/Python/lldbsuite/test/make/Makefile.rules
  lldb/packages/Python/lldbsuite/test/plugins/builder_base.py
  lldb/packages/Python/lldbsuite/test/test_categories.py
  lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
  lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
  lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.h
  lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.h
  lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
  lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
  lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp

Index: lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
@@ -440,20 +440,6 @@
     if (section_list == NULL)
       return 0;
 
-    // On non Apple platforms we might have .debug_types debug info that is
-    // created by using "-fdebug-types-section". LLDB currently will try to
-    // load this debug info, but it causes crashes during debugging when types
-    // are missing since it doesn't know how to parse the info in the
-    // .debug_types type units. This causes all complex debug info types to be
-    // unresolved. Because this causes LLDB to crash and since it really
-    // doesn't provide a solid debuggiung experience, we should disable trying
-    // to debug this kind of DWARF until support gets added or deprecated.
-    if (section_list->FindSectionByName(ConstString(".debug_types"))) {
-      m_obj_file->GetModule()->ReportWarning(
-        "lldb doesn’t support .debug_types debug info");
-      return 0;
-    }
-
     uint64_t debug_abbrev_file_size = 0;
     uint64_t debug_info_file_size = 0;
     uint64_t debug_line_file_size = 0;
@@ -766,7 +752,7 @@
             cu_sp = std::make_shared<CompileUnit>(
                 module_sp, dwarf_cu, cu_file_spec, dwarf_cu->GetID(),
                 cu_language, is_optimized ? eLazyBoolYes : eLazyBoolNo);
-            if (cu_sp) {
+            if (dwarf_cu->GetAsCompileUnit() && cu_sp) {
               // If we just created a compile unit with an invalid file spec,
               // try and get the first entry in the supports files from the
               // line table as that should be the compile unit.
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.h
@@ -16,12 +16,15 @@
 #include <atomic>
 
 class DWARFUnit;
+class DWARFTypeUnit;
 class DWARFCompileUnit;
 class NameToDIE;
 class SymbolFileDWARF;
 class SymbolFileDWARFDwo;
 
 typedef std::shared_ptr<DWARFUnit> DWARFUnitSP;
+typedef std::shared_ptr<DWARFTypeUnit> DWARFTypeUnitSP;
+typedef std::shared_ptr<DWARFCompileUnit> DWARFCompileUnitSP;
 
 enum DWARFProducer {
   eProducerInvalid = 0,
@@ -38,6 +41,12 @@
 public:
   virtual ~DWARFUnit();
 
+  llvm::Error ExtractHeader(SymbolFileDWARF *dwarf2Data,
+                            const lldb_private::DWARFDataExtractor &debug_info,
+                            lldb::offset_t *offset_ptr);
+
+  DWARFTypeUnit *GetAsTypeUnit();
+  DWARFCompileUnit *GetAsCompileUnit();
   virtual bool IsTypesSection() const = 0;
   void ExtractUnitDIEIfNeeded();
   void ExtractDIEsIfNeeded();
@@ -169,6 +178,8 @@
     return die_iterator_range(m_die_array.begin(), m_die_array.end());
   }
 
+  DWARFDIE FindTypeSignatureDIE(uint64_t type_sig) const;
+
 protected:
   DWARFUnit(SymbolFileDWARF *dwarf);
 
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFUnit.cpp
@@ -16,9 +16,12 @@
 #include "lldb/Utility/LLDBAssert.h"
 #include "lldb/Utility/StreamString.h"
 #include "lldb/Utility/Timer.h"
+#include "llvm/Object/Error.h"
 
+#include "DWARFCompileUnit.h"
 #include "DWARFDebugAranges.h"
 #include "DWARFDebugInfo.h"
+#include "DWARFTypeUnit.h"
 #include "LogChannelDWARF.h"
 #include "SymbolFileDWARFDebugMap.h"
 #include "SymbolFileDWARFDwo.h"
@@ -418,6 +421,10 @@
 
 void DWARFUnit::BuildAddressRangeTable(SymbolFileDWARF *dwarf,
                                        DWARFDebugAranges *debug_aranges) {
+  // No addresses in a type unit
+  if (GetAsTypeUnit())
+    return;
+
   // This function is usually called if there in no .debug_aranges section in
   // order to produce a compile unit level set of address ranges that is
   // accurate.
@@ -801,3 +808,74 @@
   return *m_func_aranges_up;
 }
 
+DWARFDIE DWARFUnit::FindTypeSignatureDIE(uint64_t type_sig) const {
+  if (auto cu = m_dwarf->DebugInfo()->GetTypeUnitForSignature(type_sig))
+    return cu->GetTypeUnitDIE();
+  return DWARFDIE();
+}
+
+DWARFTypeUnit *DWARFUnit::GetAsTypeUnit() {
+  if (GetUnitDIEOnly().Tag() == DW_TAG_type_unit)
+    return static_cast<DWARFTypeUnit *>(this);
+  return nullptr;
+}
+
+DWARFCompileUnit *DWARFUnit::GetAsCompileUnit() {
+  if (GetUnitDIEOnly().Tag() == DW_TAG_compile_unit)
+    return static_cast<DWARFCompileUnit *>(this);
+  return nullptr;
+}
+
+llvm::Error
+DWARFUnit::ExtractHeader(SymbolFileDWARF *dwarf2Data,
+                         const lldb_private::DWARFDataExtractor &debug_info,
+                         lldb::offset_t *offset_ptr) {
+  m_offset = *offset_ptr;
+
+  dw_offset_t abbr_offset;
+  const DWARFDebugAbbrev *abbr = dwarf2Data->DebugAbbrev();
+  if (!abbr)
+    return llvm::make_error<llvm::object::GenericBinaryError>(
+        "No debug_abbrev data");
+
+  m_length = debug_info.GetDWARFInitialLength(offset_ptr);
+  m_version = debug_info.GetU16(offset_ptr);
+
+  if (m_version == 5) {
+    m_unit_type = debug_info.GetU8(offset_ptr);
+    m_addr_size = debug_info.GetU8(offset_ptr);
+    abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
+
+    if (m_unit_type == llvm::dwarf::DW_UT_skeleton)
+      m_dwo_id = debug_info.GetU64(offset_ptr);
+  } else {
+    abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
+    m_addr_size = debug_info.GetU8(offset_ptr);
+  }
+
+  bool length_OK = debug_info.ValidOffset(GetNextUnitOffset() - 1);
+  bool version_OK = SymbolFileDWARF::SupportedVersion(m_version);
+  bool abbr_offset_OK =
+      dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
+  bool addr_size_OK = (m_addr_size == 4) || (m_addr_size == 8);
+
+  if (!length_OK)
+    return llvm::make_error<llvm::object::GenericBinaryError>(
+        "Invalid compile unit length");
+  if (!version_OK)
+    return llvm::make_error<llvm::object::GenericBinaryError>(
+        "Unsupported compile unit version");
+  if (!abbr_offset_OK)
+    return llvm::make_error<llvm::object::GenericBinaryError>(
+        "Abbreviation offset for compile unit is not valid");
+  if (!addr_size_OK)
+    return llvm::make_error<llvm::object::GenericBinaryError>(
+        "Invalid compile unit address size");
+
+  m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
+  if (!m_abbrevs)
+    return llvm::make_error<llvm::object::GenericBinaryError>(
+        "No abbrev exists at the specified offset.");
+
+  return llvm::Error::success();
+}
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.h
===================================================================
--- /dev/null
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.h
@@ -0,0 +1,95 @@
+//===-- DWARFTypeUnit.h --------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SymbolFileDWARF_DWARFTypeUnit_h_
+#define SymbolFileDWARF_DWARFTypeUnit_h_
+
+#include "DWARFUnit.h"
+
+class DWARFTypeUnit : public DWARFUnit {
+  friend class DWARFUnit;
+
+public:
+  static llvm::Expected<DWARFUnitSP>
+  extract(SymbolFileDWARF *dwarf2Data,
+          const lldb_private::DWARFDataExtractor &debug_info,
+          lldb::offset_t *offset_ptr);
+  void Dump(lldb_private::Stream *s) const override;
+
+  //------------------------------------------------------------------
+  /// Get the size in bytes of the header.
+  ///
+  /// @return
+  ///     Byte size of the type unit header
+  //------------------------------------------------------------------
+  uint32_t GetHeaderByteSize() const override {
+    return 23;
+  }
+
+  //------------------------------------------------------------------
+  /// Get the type DIE offset if this is a type unit.
+  ///
+  /// If this unit is a type unit, return type DIE offset for the type
+  /// in this unit.
+  ///
+  /// @return
+  ///   The DIE offset that points to the type unit type if this is
+  ///   a type unit, otherwise return DW_INVALID_OFFSET.
+  //------------------------------------------------------------------
+  dw_offset_t GetTypeUnitDIEOffset() {
+    return m_offset + m_type_offset;
+  }
+  
+  //------------------------------------------------------------------
+  /// Get the type signature for the type contained in this unit.
+  ///
+  /// @return
+  ///   The 64 bit type signature for the type contained in this type
+  ///   unit. This value will not be valid if this compile unit is not
+  ///   a type unit.
+  //------------------------------------------------------------------
+  uint64_t GetTypeSignature() const { return m_type_signature; }
+
+  //------------------------------------------------------------------
+  /// Get the type DIE if this is a type unit.
+  ///
+  /// If this unit is a type unit, return type contained within it as
+  /// a DWARFDIE object.
+  ///
+  /// @return
+  ///   The DIE representing the type if this is a type unit, or an
+  ///   invalid DWARFDIE if this is not a type unit.
+  //------------------------------------------------------------------
+  DWARFDIE GetTypeUnitDIE();
+
+  //------------------------------------------------------------------
+  /// Get the data that contains the DIE information for this unit.
+  ///
+  /// \return
+  ///   The correct data (.debug_types for DWARF 4 and earlier, and
+  ///   .debug_info for DWARF 5 and later) for the DIE information in
+  ///   this unit.
+  //------------------------------------------------------------------
+  const lldb_private::DWARFDataExtractor &GetData() const override;
+
+  bool IsTypesSection() const override { return GetVersion() < 5; }
+
+protected:
+  // Type signature contained in a type unit which will be valid (non-zero)
+  // for type units only.
+  uint64_t m_type_signature = 0;
+  // Compile unit relative type offset for type units only.
+  dw_offset_t m_type_offset = DW_INVALID_OFFSET;
+
+private:
+  DWARFTypeUnit(SymbolFileDWARF *dwarf2Data);
+  DISALLOW_COPY_AND_ASSIGN(DWARFTypeUnit);
+};
+
+#endif // SymbolFileDWARF_DWARFTypeUnit_h_
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.cpp
===================================================================
--- /dev/null
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFTypeUnit.cpp
@@ -0,0 +1,52 @@
+//===-- DWARFTypeUnit.cpp ---------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "DWARFTypeUnit.h"
+#include "DWARFDataExtractor.h"
+#include "SymbolFileDWARF.h"
+#include "lldb/Utility/Stream.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+DWARFTypeUnit::DWARFTypeUnit(SymbolFileDWARF *dwarf2Data)
+    : DWARFUnit(dwarf2Data) {}
+
+void DWARFTypeUnit::Dump(Stream *s) const {
+  s->Printf("0x%8.8x: Type Unit: length = 0x%8.8x, version = 0x%4.4x, "
+            "abbr_offset = 0x%8.8x, addr_size = 0x%2.2x, "
+            "type_signature = 0x%16.16" PRIx64 ", type_offset = 0x%8.8x "
+            "(next CU at {0x%8.8x})\n",
+            m_offset, m_length, m_version, GetAbbrevOffset(), m_addr_size,
+            m_type_signature, m_type_offset, GetNextUnitOffset());
+}
+
+llvm::Expected<DWARFUnitSP>
+DWARFTypeUnit::extract(SymbolFileDWARF *dwarf2Data,
+                       const DWARFDataExtractor &debug_info,
+                       lldb::offset_t *offset_ptr) {
+  // std::make_shared would require the ctor to be public.
+  DWARFTypeUnitSP cu_sp(new DWARFTypeUnit(dwarf2Data));
+  auto err = cu_sp->ExtractHeader(dwarf2Data, debug_info, offset_ptr);
+  if (err)
+    return std::move(err);
+  cu_sp->m_type_signature = debug_info.GetU64(offset_ptr);
+  cu_sp->m_type_offset = debug_info.GetDWARFOffset(offset_ptr);
+  return cu_sp;
+}
+
+DWARFDIE DWARFTypeUnit::GetTypeUnitDIE() {
+  // The type offset is compile unit relative, so we need to add the compile
+  // unit offset to ensure we get the correct DIE.
+  return GetDIE(GetTypeUnitDIEOffset());
+}
+
+const DWARFDataExtractor &DWARFTypeUnit::GetData() const {
+  return m_dwarf->get_debug_types_data();
+}
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.h
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.h
@@ -88,6 +88,7 @@
   const uint8_t *BlockData() const;
   CUDIERef Reference() const;
   uint64_t Reference(dw_offset_t offset) const;
+  DWARFDIE GetTypeSignatureDIE() const;
   bool Boolean() const { return m_value.value.uval != 0; }
   uint64_t Unsigned() const { return m_value.value.uval; }
   void SetUnsigned(uint64_t uval) { m_value.value.uval = uval; }
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFFormValue.cpp
@@ -14,11 +14,10 @@
 #include "lldb/Utility/Stream.h"
 
 #include "DWARFDebugInfo.h"
+#include "DWARFTypeUnit.h"
 #include "DWARFUnit.h"
 #include "DWARFFormValue.h"
 
-class DWARFUnit;
-
 using namespace lldb_private;
 
 static uint8_t g_form_sizes_addr4[] = {
@@ -588,6 +587,19 @@
     return CUDIERef(ref_cu, value);
     }
 
+  case DW_FORM_ref_sig8: {
+    assert(m_cu);
+    DWARFDebugInfo *debug_info = m_cu->GetSymbolFileDWARF()->DebugInfo();
+    DWARFTypeUnit *ref_tu = debug_info->GetTypeUnitForSignature(value);
+    if (!ref_tu) {
+      m_cu->GetSymbolFileDWARF()->GetObjectFile()->GetModule()->ReportError(
+          "DW_FORM_ref_sig8 DIE reference 0x%" PRIx64 " has no matching TU",
+          value);
+      return CUDIERef();
+    }
+    return CUDIERef(ref_tu, ref_tu->GetTypeUnitDIEOffset());
+    }
+
   default:
     return CUDIERef();
   }
@@ -613,6 +625,17 @@
   }
 }
 
+DWARFDIE DWARFFormValue::GetTypeSignatureDIE() const {
+  if (m_form == DW_FORM_ref_sig8) {
+    // CU must be valid since we will need to back up into the debug info and
+    // find the DIE offset of the type in the type unit.
+    assert(m_cu);
+    return m_cu->FindTypeSignatureDIE(m_value.value.uval);
+  }
+
+  return DWARFDIE();
+}
+
 const uint8_t *DWARFFormValue::BlockData() const { return m_value.data; }
 
 bool DWARFFormValue::IsBlockForm(const dw_form_t form) {
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
@@ -83,11 +83,13 @@
                                        const DWARFUnitSP &cu_sp);
 
   typedef std::vector<DWARFUnitSP> UnitColl;
+  typedef llvm::DenseMap<uint64_t, uint32_t> TypeSignatureMap;
 
   // Member variables
   SymbolFileDWARF *m_dwarf2Data;
   lldb_private::DWARFContext &m_context;
   UnitColl m_compile_units, m_type_units;
+  TypeSignatureMap m_type_sig_to_cu_index;
   std::unique_ptr<DWARFDebugAranges>
       m_cu_aranges_up; // A quick address to compile unit table
 
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
@@ -22,6 +22,7 @@
 #include "DWARFDebugInfo.h"
 #include "DWARFDebugInfoEntry.h"
 #include "DWARFFormValue.h"
+#include "DWARFTypeUnit.h"
 
 using namespace lldb;
 using namespace lldb_private;
@@ -106,6 +107,30 @@
       offset = (*cu_sp)->GetNextUnitOffset();
     }
   }
+
+  {
+    lldb::offset_t offset = 0;
+    const auto &debug_types_data = m_dwarf2Data->get_debug_types_data();
+    while (debug_types_data.ValidOffset(offset)) {
+      llvm::Expected<DWARFUnitSP> tu_sp =
+          DWARFTypeUnit::extract(m_dwarf2Data, debug_types_data, &offset);
+
+      if (!tu_sp) {
+        // FIXME: Propagate this error up.
+        llvm::consumeError(tu_sp.takeError());
+        return;
+      }
+
+      // If it didn't return an error, then it should be returning a valid Unit.
+      assert(*tu_sp);
+
+      m_type_sig_to_cu_index[(*tu_sp)->GetAsTypeUnit()->GetTypeSignature()] =
+          m_type_units.size();
+      m_type_units.push_back(*tu_sp);
+
+      offset = (*tu_sp)->GetNextUnitOffset();
+    }
+  }
 }
 
 size_t DWARFDebugInfo::GetNumUnits() {
@@ -172,3 +197,10 @@
   return DWARFDIE(); // Not found
 }
 
+DWARFTypeUnit *DWARFDebugInfo::GetTypeUnitForSignature(uint64_t type_sig) {
+  auto pos = m_type_sig_to_cu_index.find(type_sig);
+  if (pos != m_type_sig_to_cu_index.end())
+    return GetUnitAtIndex(GetNumOnlyCompileUnits() + pos->second)
+        ->GetAsTypeUnit();
+  return nullptr;
+}
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFCompileUnit.cpp
@@ -25,55 +25,10 @@
   assert(debug_info.ValidOffset(*offset_ptr));
 
   // std::make_shared would require the ctor to be public.
-  std::shared_ptr<DWARFCompileUnit> cu_sp(new DWARFCompileUnit(dwarf2Data));
-
-  cu_sp->m_offset = *offset_ptr;
-
-  dw_offset_t abbr_offset;
-  const DWARFDebugAbbrev *abbr = dwarf2Data->DebugAbbrev();
-  if (!abbr)
-    return llvm::make_error<llvm::object::GenericBinaryError>(
-        "No debug_abbrev data");
-
-  cu_sp->m_length = debug_info.GetDWARFInitialLength(offset_ptr);
-  cu_sp->m_version = debug_info.GetU16(offset_ptr);
-
-  if (cu_sp->m_version == 5) {
-    cu_sp->m_unit_type = debug_info.GetU8(offset_ptr);
-    cu_sp->m_addr_size = debug_info.GetU8(offset_ptr);
-    abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
-
-    if (cu_sp->m_unit_type == llvm::dwarf::DW_UT_skeleton)
-      cu_sp->m_dwo_id = debug_info.GetU64(offset_ptr);
-  } else {
-    abbr_offset = debug_info.GetDWARFOffset(offset_ptr);
-    cu_sp->m_addr_size = debug_info.GetU8(offset_ptr);
-  }
-
-  bool length_OK = debug_info.ValidOffset(cu_sp->GetNextUnitOffset() - 1);
-  bool version_OK = SymbolFileDWARF::SupportedVersion(cu_sp->m_version);
-  bool abbr_offset_OK =
-      dwarf2Data->get_debug_abbrev_data().ValidOffset(abbr_offset);
-  bool addr_size_OK = (cu_sp->m_addr_size == 4) || (cu_sp->m_addr_size == 8);
-
-  if (!length_OK)
-    return llvm::make_error<llvm::object::GenericBinaryError>(
-        "Invalid compile unit length");
-  if (!version_OK)
-    return llvm::make_error<llvm::object::GenericBinaryError>(
-        "Unsupported compile unit version");
-  if (!abbr_offset_OK)
-    return llvm::make_error<llvm::object::GenericBinaryError>(
-        "Abbreviation offset for compile unit is not valid");
-  if (!addr_size_OK)
-    return llvm::make_error<llvm::object::GenericBinaryError>(
-        "Invalid compile unit address size");
-
-  cu_sp->m_abbrevs = abbr->GetAbbreviationDeclarationSet(abbr_offset);
-  if (!cu_sp->m_abbrevs)
-    return llvm::make_error<llvm::object::GenericBinaryError>(
-        "No abbrev exists at the specified offset.");
-
+  DWARFCompileUnitSP cu_sp(new DWARFCompileUnit(dwarf2Data));
+  auto err = cu_sp->ExtractHeader(dwarf2Data, debug_info, offset_ptr);
+  if (err)
+    return std::move(err);
   return cu_sp;
 }
 
Index: lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
+++ lldb/source/Plugins/SymbolFile/DWARF/DWARFASTParserClang.cpp
@@ -251,8 +251,25 @@
           context_die.GetOffset(), die.GetTagAsCString(), die.GetName());
     }
     Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
-    TypeList *type_list = dwarf->GetTypeList();
     if (type_ptr == NULL) {
+
+      // If we have .debug_types defer to the complete version in the
+      // .debug_types section, not the broken incomplete definition in real
+      // compile units that are only there so addresses can be assigned to
+      // static values.
+      auto signature_die = die.GetAttributeValueAsReferenceDIE(DW_AT_signature);
+      if (signature_die) {
+        type_sp = ParseTypeFromDWARF(sc, signature_die, log, type_is_new_ptr);
+        if (type_sp) {
+          dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
+          clang::DeclContext *decl_ctx =
+              GetCachedClangDeclContextForDIE(signature_die);
+          if (decl_ctx)
+            LinkDeclContextToDIE(decl_ctx, die);
+          return type_sp;
+        }
+      }
+
       if (type_is_new_ptr)
         *type_is_new_ptr = true;
 
@@ -1905,7 +1922,7 @@
 
         // We are ready to put this type into the uniqued list up at the module
         // level
-        type_list->Insert(type_sp);
+        dwarf->GetTypeList()->Insert(type_sp);
 
         dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
       }
Index: lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
===================================================================
--- lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
+++ lldb/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
@@ -23,6 +23,7 @@
   DWARFFormValue.cpp
   DWARFIndex.cpp
   DWARFUnit.cpp
+  DWARFTypeUnit.cpp
   HashedNameToDIE.cpp
   LogChannelDWARF.cpp
   ManualDWARFIndex.cpp
Index: lldb/packages/Python/lldbsuite/test/test_categories.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/test_categories.py
+++ lldb/packages/Python/lldbsuite/test/test_categories.py
@@ -15,7 +15,7 @@
 
 
 debug_info_categories = [
-    'dwarf', 'dwo', 'dsym', 'gmodules'
+    'dwarf', 'dwo', 'dsym', 'gmodules', 'dwarf_type_units'
 ]
 
 all_categories = {
@@ -24,6 +24,7 @@
     'dwo': 'Tests that can be run with DWO debug information',
     'dsym': 'Tests that can be run with DSYM debug information',
     'gmodules': 'Tests that can be run with -gmodules debug information',
+    'dwarf_type_units' : 'Tests that can be run with DWARF type units',
     'expression': 'Tests related to the expression parser',
     'libc++': 'Test for libc++ data formatters',
     'libstdcxx': 'Test for libstdcxx data formatters',
@@ -62,6 +63,8 @@
         if platform not in ["freebsd", "darwin", "macosx", "ios", "watchos", "tvos", "bridgeos"]:
             return False
         return gmodules.is_compiler_clang_with_gmodules(compiler_path)
+    elif category == "dwarf_type_units":
+        return platform in ["linux"]
     return True
 
 
Index: lldb/packages/Python/lldbsuite/test/plugins/builder_base.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/plugins/builder_base.py
+++ lldb/packages/Python/lldbsuite/test/plugins/builder_base.py
@@ -212,6 +212,27 @@
     return True
 
 
+def buildDwarfTypeUnits(
+        sender=None,
+        architecture=None,
+        compiler=None,
+        dictionary=None,
+        testdir=None,
+        testname=None):
+    """Build the binaries with type units (type in  a .debug_types section)."""
+    commands = []
+    commands.append(getMake(testdir, testname) +
+                    ["MAKE_DSYM=NO",
+                     "DWARF_TYPE_UNITS=YES",
+                     getArchSpec(architecture),
+                     getCCSpec(compiler),
+                     getCmdLine(dictionary)])
+
+    lldbtest.system(commands, sender=sender)
+    # True signifies that we can handle building with type units.
+    return True
+
+
 def cleanup(sender=None, dictionary=None):
     """Perform a platform-specific cleanup after the test."""
     return True
Index: lldb/packages/Python/lldbsuite/test/make/Makefile.rules
===================================================================
--- lldb/packages/Python/lldbsuite/test/make/Makefile.rules
+++ lldb/packages/Python/lldbsuite/test/make/Makefile.rules
@@ -320,6 +320,12 @@
 	CXXFLAGS += $(MANDATORY_MODULE_BUILD_CFLAGS)
 endif
 
+MANDATORY_DWARF_TYPE_UNITS_CFLAGS := -fdebug-types-section
+
+ifeq "$(DWARF_TYPE_UNITS)" "YES"
+	CFLAGS += $(MANDATORY_DWARF_TYPE_UNITS_CFLAGS)
+endif
+
 CXXFLAGS += -std=c++11 $(CFLAGS) $(ARCH_CXXFLAGS)
 LD = $(CC)
 LDFLAGS ?= $(CFLAGS)
Index: lldb/packages/Python/lldbsuite/test/lldbtest.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/lldbtest.py
+++ lldb/packages/Python/lldbsuite/test/lldbtest.py
@@ -1576,6 +1576,23 @@
                                     dictionary, testdir, testname):
             raise Exception("Don't know how to build binary with gmodules")
 
+    def buildDwarfTypeUnits(
+            self,
+            architecture=None,
+            compiler=None,
+            dictionary=None):
+        """Platform specific way to build binaries with DWARF type units."""
+        testdir = self.mydir
+        testname = self.getBuildDirBasename()
+        if self.getDebugInfo() != "dwarf_type_units":
+            raise Exception("NO_DEBUG_INFO_TESTCASE must build with buildDefault")
+
+        module = builder_module()
+        dictionary = lldbplatformutil.finalize_build_dictionary(dictionary)
+        if not module.buildDwarfTypeUnits(self, architecture, compiler,
+                                          dictionary, testdir, testname):
+            raise Exception("Don't know how to build binary with DWARF type units")
+
     def signBinary(self, binary_path):
         if sys.platform.startswith("darwin"):
             codesign_cmd = "codesign --force --sign \"%s\" %s" % (
@@ -2402,6 +2419,8 @@
             return self.buildDwo(architecture, compiler, dictionary)
         elif self.getDebugInfo() == "gmodules":
             return self.buildGModules(architecture, compiler, dictionary)
+        elif self.getDebugInfo() == "dwarf_type_units":
+            return self.buildDwarfTypeUnits(architecture, compiler, dictionary)
         else:
             self.fail("Can't build for debug info: %s" % self.getDebugInfo())
 
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH] D61... Jan Kratochvil via Phabricator via lldb-commits

Reply via email to