[Lldb-commits] [lldb] r356612 - Introduce DWARFContext.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Wed Mar 20 13:49:25 PDT 2019


Author: zturner
Date: Wed Mar 20 13:49:25 2019
New Revision: 356612

URL: http://llvm.org/viewvc/llvm-project?rev=356612&view=rev
Log:
Introduce DWARFContext.

LLVM's DWARF parsing library has a class called DWARFContext which holds
all of the various DWARF data sections and lots of other information.
LLDB's on the other hand stores all of this directly in SymbolFileDWARF
/ SymbolFileDWARFDwo and passes this interface around through the
parsing library. Obviously this is incompatible with a world where the
low level interface does not depend on the high level interface, so we
need to move towards a model similar to LLVM's - i.e. all of the context
needed for low level parsing should be in a single class, and that class
gets passed around.

This patch is a small incremental step towards achieving this. The
interface and internals deviate from LLVM's for technical reasons, but
the high level idea is the same. The goal is, eventually, to remove all
occurrences of SymbolFileDWARF from the low level parsing code.

For now I've chosen a very simple section - the .debug_aranges section
to move into DWARFContext while leaving everything else unchanged. In
the short term this is a bit confusing because now the information you
need might come from either of 2 different locations. But it's a huge
refactor to do this all at once and runs a much higher risk of breaking
things. So I think it would be wise to do this in very small pieces.

TL;DR - No functional change

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

Added:
    lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
    lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h
Modified:
    lldb/trunk/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
    lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
    lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
    lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
    lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
    lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/CMakeLists.txt?rev=356612&r1=356611&r2=356612&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/CMakeLists.txt (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/CMakeLists.txt Wed Mar 20 13:49:25 2019
@@ -7,6 +7,7 @@ add_lldb_library(lldbPluginSymbolFileDWA
   DWARFAttribute.cpp
   DWARFBaseDIE.cpp
   DWARFCompileUnit.cpp
+  DWARFContext.cpp
   DWARFDataExtractor.cpp
   DWARFDebugAbbrev.cpp
   DWARFDebugAranges.cpp

Added: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp?rev=356612&view=auto
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp (added)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.cpp Wed Mar 20 13:49:25 2019
@@ -0,0 +1,43 @@
+//===-- DWARFContext.cpp ----------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "DWARFContext.h"
+
+#include "lldb/Core/Section.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+static const DWARFDataExtractor *
+LoadOrGetSection(Module &module, SectionType section_type,
+                 llvm::Optional<DWARFDataExtractor> &extractor) {
+  if (extractor.hasValue())
+    return extractor->GetByteSize() > 0 ? extractor.getPointer() : nullptr;
+
+  // Initialize to an empty extractor so that we always take the fast path going
+  // forward.
+  extractor.emplace();
+
+  const SectionList *section_list = module.GetSectionList();
+  if (!section_list)
+    return nullptr;
+
+  auto section_sp = section_list->FindSectionByType(section_type, true);
+  if (!section_sp)
+    return nullptr;
+
+  section_sp->GetSectionData(*extractor);
+  return extractor.getPointer();
+}
+
+DWARFContext::DWARFContext(Module &module) : m_module(module) {}
+
+const DWARFDataExtractor *DWARFContext::getOrLoadArangesData() {
+  return LoadOrGetSection(m_module, eSectionTypeDWARFDebugAranges,
+                          m_data_debug_aranges);
+}

Added: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h?rev=356612&view=auto
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h (added)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFContext.h Wed Mar 20 13:49:25 2019
@@ -0,0 +1,30 @@
+//===-- DWARFContext.h ------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_PLUGINS_SYMBOLFILE_DWARF_DWARFCONTEXT_H
+#define LLDB_PLUGINS_SYMBOLFILE_DWARF_DWARFCONTEXT_H
+
+#include "DWARFDataExtractor.h"
+#include "lldb/Core/Module.h"
+#include "llvm/ADT/Optional.h"
+#include <memory>
+
+namespace lldb_private {
+class DWARFContext {
+private:
+  Module &m_module;
+  llvm::Optional<DWARFDataExtractor> m_data_debug_aranges;
+
+public:
+  explicit DWARFContext(Module &module);
+
+  const DWARFDataExtractor *getOrLoadArangesData();
+};
+} // namespace lldb_private
+
+#endif

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp?rev=356612&r1=356611&r2=356612&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.cpp Wed Mar 20 13:49:25 2019
@@ -17,7 +17,7 @@
 #include "lldb/Utility/Stream.h"
 
 #include "DWARFCompileUnit.h"
-#include "DWARFDebugAranges.h"
+#include "DWARFContext.h"
 #include "DWARFDebugAranges.h"
 #include "DWARFDebugInfo.h"
 #include "DWARFDebugInfoEntry.h"
@@ -30,8 +30,9 @@ using namespace std;
 //----------------------------------------------------------------------
 // Constructor
 //----------------------------------------------------------------------
-DWARFDebugInfo::DWARFDebugInfo()
-    : m_dwarf2Data(NULL), m_compile_units(), m_cu_aranges_up() {}
+DWARFDebugInfo::DWARFDebugInfo(lldb_private::DWARFContext &context)
+    : m_dwarf2Data(NULL), m_context(context), m_compile_units(),
+      m_cu_aranges_up() {}
 
 //----------------------------------------------------------------------
 // SetDwarfData
@@ -48,10 +49,10 @@ llvm::Expected<DWARFDebugAranges &> DWAR
   assert(m_dwarf2Data);
 
   m_cu_aranges_up = llvm::make_unique<DWARFDebugAranges>();
-  const DWARFDataExtractor &debug_aranges_data =
-      m_dwarf2Data->get_debug_aranges_data();
-  if (debug_aranges_data.GetByteSize() > 0) {
-    llvm::Error error = m_cu_aranges_up->extract(debug_aranges_data);
+  const DWARFDataExtractor *debug_aranges_data =
+      m_context.getOrLoadArangesData();
+  if (debug_aranges_data) {
+    llvm::Error error = m_cu_aranges_up->extract(*debug_aranges_data);
     if (error)
       return std::move(error);
   }

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h?rev=356612&r1=356611&r2=356612&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/DWARFDebugInfo.h Wed Mar 20 13:49:25 2019
@@ -19,6 +19,10 @@
 #include "lldb/lldb-private.h"
 #include "llvm/Support/Error.h"
 
+namespace lldb_private {
+class DWARFContext;
+}
+
 typedef std::multimap<const char *, dw_offset_t, CStringCompareFunctionObject>
     CStringToDIEMap;
 typedef CStringToDIEMap::iterator CStringToDIEMapIter;
@@ -32,7 +36,7 @@ public:
                                   const dw_offset_t next_offset,
                                   const uint32_t depth, void *userData);
 
-  DWARFDebugInfo();
+  explicit DWARFDebugInfo(lldb_private::DWARFContext &context);
   void SetDwarfData(SymbolFileDWARF *dwarf2Data);
 
   size_t GetNumCompileUnits();
@@ -62,6 +66,7 @@ protected:
   // Member variables
   //----------------------------------------------------------------------
   SymbolFileDWARF *m_dwarf2Data;
+  lldb_private::DWARFContext &m_context;
   CompileUnitColl m_compile_units;
   std::unique_ptr<DWARFDebugAranges>
       m_cu_aranges_up; // A quick address to compile unit table

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp?rev=356612&r1=356611&r2=356612&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.cpp Wed Mar 20 13:49:25 2019
@@ -355,12 +355,13 @@ SymbolFileDWARF::SymbolFileDWARF(ObjectF
                                   << 32), // Used by SymbolFileDWARFDebugMap to
                                           // when this class parses .o files to
                                           // contain the .o file index/ID
-      m_debug_map_module_wp(), m_debug_map_symfile(NULL), m_data_debug_abbrev(),
-      m_data_debug_aranges(), m_data_debug_frame(), m_data_debug_info(),
-      m_data_debug_line(), m_data_debug_macro(), m_data_debug_loc(),
-      m_data_debug_ranges(), m_data_debug_rnglists(), m_data_debug_str(),
-      m_data_apple_names(), m_data_apple_types(), m_data_apple_namespaces(),
-      m_abbr(), m_info(), m_line(), m_fetched_external_modules(false),
+      m_debug_map_module_wp(), m_debug_map_symfile(NULL),
+      m_context(*objfile->GetModule()), m_data_debug_abbrev(),
+      m_data_debug_frame(), m_data_debug_info(), m_data_debug_line(),
+      m_data_debug_macro(), m_data_debug_loc(), m_data_debug_ranges(),
+      m_data_debug_rnglists(), m_data_debug_str(), m_data_apple_names(),
+      m_data_apple_types(), m_data_apple_namespaces(), m_abbr(), m_info(),
+      m_line(), m_fetched_external_modules(false),
       m_supports_DW_AT_APPLE_objc_complete_type(eLazyBoolCalculate), m_ranges(),
       m_unique_ast_type_map() {}
 
@@ -394,15 +395,6 @@ TypeSystem *SymbolFileDWARF::GetTypeSyst
 
 void SymbolFileDWARF::InitializeObject() {
   Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
-  ModuleSP module_sp(m_obj_file->GetModule());
-  if (module_sp) {
-    const SectionList *section_list = module_sp->GetSectionList();
-    Section *section =
-        section_list->FindSectionByName(GetDWARFMachOSegmentName()).get();
-
-    if (section)
-      m_obj_file->ReadSectionData(section, m_dwarf_data);
-  }
 
   if (!GetGlobalPluginProperties()->IgnoreFileIndexes()) {
     DWARFDataExtractor apple_names, apple_namespaces, apple_types, apple_objc;
@@ -549,19 +541,15 @@ void SymbolFileDWARF::LoadSectionData(ll
                                       DWARFDataExtractor &data) {
   ModuleSP module_sp(m_obj_file->GetModule());
   const SectionList *section_list = module_sp->GetSectionList();
-  if (section_list) {
-    SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
-    if (section_sp) {
-      // See if we memory mapped the DWARF segment?
-      if (m_dwarf_data.GetByteSize()) {
-        data.SetData(m_dwarf_data, section_sp->GetOffset(),
-                     section_sp->GetFileSize());
-      } else {
-        if (m_obj_file->ReadSectionData(section_sp.get(), data) == 0)
-          data.Clear();
-      }
-    }
-  }
+  if (!section_list)
+    return;
+
+  SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
+  if (!section_sp)
+    return;
+
+  data.Clear();
+  m_obj_file->ReadSectionData(section_sp.get(), data);
 }
 
 const DWARFDataExtractor &SymbolFileDWARF::get_debug_abbrev_data() {
@@ -573,11 +561,6 @@ const DWARFDataExtractor &SymbolFileDWAR
   return GetCachedSectionData(eSectionTypeDWARFDebugAddr, m_data_debug_addr);
 }
 
-const DWARFDataExtractor &SymbolFileDWARF::get_debug_aranges_data() {
-  return GetCachedSectionData(eSectionTypeDWARFDebugAranges,
-                              m_data_debug_aranges);
-}
-
 const DWARFDataExtractor &SymbolFileDWARF::get_debug_frame_data() {
   return GetCachedSectionData(eSectionTypeDWARFDebugFrame, m_data_debug_frame);
 }
@@ -690,10 +673,8 @@ DWARFDebugInfo *SymbolFileDWARF::DebugIn
     Timer scoped_timer(func_cat, "%s this = %p", LLVM_PRETTY_FUNCTION,
                        static_cast<void *>(this));
     if (get_debug_info_data().GetByteSize() > 0) {
-      m_info.reset(new DWARFDebugInfo());
-      if (m_info) {
-        m_info->SetDwarfData(this);
-      }
+      m_info = llvm::make_unique<DWARFDebugInfo>(m_context);
+      m_info->SetDwarfData(this);
     }
   }
   return m_info.get();

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h?rev=356612&r1=356611&r2=356612&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARF.h Wed Mar 20 13:49:25 2019
@@ -30,6 +30,7 @@
 #include "lldb/Utility/RangeMap.h"
 #include "lldb/lldb-private.h"
 
+#include "DWARFContext.h"
 #include "DWARFDataExtractor.h"
 #include "DWARFDefines.h"
 #include "DWARFIndex.h"
@@ -227,7 +228,6 @@ public:
 
   virtual const lldb_private::DWARFDataExtractor &get_debug_abbrev_data();
   virtual const lldb_private::DWARFDataExtractor &get_debug_addr_data();
-  const lldb_private::DWARFDataExtractor &get_debug_aranges_data();
   const lldb_private::DWARFDataExtractor &get_debug_frame_data();
   virtual const lldb_private::DWARFDataExtractor &get_debug_info_data();
   const lldb_private::DWARFDataExtractor &get_debug_line_data();
@@ -458,11 +458,10 @@ protected:
   llvm::once_flag m_dwp_symfile_once_flag;
   std::unique_ptr<SymbolFileDWARFDwp> m_dwp_symfile;
 
-  lldb_private::DWARFDataExtractor m_dwarf_data;
+  lldb_private::DWARFContext m_context;
 
   DWARFDataSegment m_data_debug_abbrev;
   DWARFDataSegment m_data_debug_addr;
-  DWARFDataSegment m_data_debug_aranges;
   DWARFDataSegment m_data_debug_frame;
   DWARFDataSegment m_data_debug_info;
   DWARFDataSegment m_data_debug_line;

Modified: lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp?rev=356612&r1=356611&r2=356612&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp (original)
+++ lldb/trunk/source/Plugins/SymbolFile/DWARF/SymbolFileDWARFDwo.cpp Wed Mar 20 13:49:25 2019
@@ -33,12 +33,6 @@ void SymbolFileDWARFDwo::LoadSectionData
   if (section_list) {
     SectionSP section_sp(section_list->FindSectionByType(sect_type, true));
     if (section_sp) {
-      // See if we memory mapped the DWARF segment?
-      if (m_dwarf_data.GetByteSize()) {
-        data.SetData(m_dwarf_data, section_sp->GetOffset(),
-                     section_sp->GetFileSize());
-        return;
-      }
 
       if (m_obj_file->ReadSectionData(section_sp.get(), data) != 0)
         return;




More information about the lldb-commits mailing list