[clang] b251897 - Re-apply "[ORC] Add N_SO and N_OSO stabs entries to MachO debug..." with fixes.

Lang Hames via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 28 21:34:22 PDT 2023


Author: Lang Hames
Date: 2023-09-28T21:34:14-07:00
New Revision: b2518971d82331b09d7630ac1aecbb49e876a496

URL: https://github.com/llvm/llvm-project/commit/b2518971d82331b09d7630ac1aecbb49e876a496
DIFF: https://github.com/llvm/llvm-project/commit/b2518971d82331b09d7630ac1aecbb49e876a496.diff

LOG: Re-apply "[ORC] Add N_SO and N_OSO stabs entries to MachO debug..." with fixes.

This re-applies db51e572893, which was reverted in 05b1a2cb3e6 due to bot
failures. The DebuggerSupportPlugin now depends on DWARF, so it has been moved
to the new OrcDebugging library (as has the enableDebuggerSupport API).

Added: 
    llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h
    llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h
    llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupport.cpp
    llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.cpp

Modified: 
    clang/lib/Interpreter/CMakeLists.txt
    clang/lib/Interpreter/IncrementalExecutor.cpp
    llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
    llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt
    llvm/tools/lli/CMakeLists.txt
    llvm/tools/lli/lli.cpp
    llvm/tools/llvm-jitlink/llvm-jitlink.cpp

Removed: 
    llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h
    llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h
    llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp
    llvm/lib/ExecutionEngine/Orc/DebuggerSupportPlugin.cpp


################################################################################
diff  --git a/clang/lib/Interpreter/CMakeLists.txt b/clang/lib/Interpreter/CMakeLists.txt
index 79d2cba67f54c4d..84f6ca5271d2ab0 100644
--- a/clang/lib/Interpreter/CMakeLists.txt
+++ b/clang/lib/Interpreter/CMakeLists.txt
@@ -4,6 +4,7 @@ set(LLVM_LINK_COMPONENTS
    MC
    Option
    OrcJit
+   OrcDebugging
    OrcShared
    OrcTargetProcess
    Support

diff  --git a/clang/lib/Interpreter/IncrementalExecutor.cpp b/clang/lib/Interpreter/IncrementalExecutor.cpp
index 2692d0618b8649e..40bcef94797d43d 100644
--- a/clang/lib/Interpreter/IncrementalExecutor.cpp
+++ b/clang/lib/Interpreter/IncrementalExecutor.cpp
@@ -17,7 +17,7 @@
 #include "clang/Interpreter/PartialTranslationUnit.h"
 #include "llvm/ExecutionEngine/ExecutionEngine.h"
 #include "llvm/ExecutionEngine/Orc/CompileUtils.h"
-#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
+#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
 #include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h b/llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h
similarity index 100%
rename from llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupport.h
rename to llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h b/llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h
similarity index 100%
rename from llvm/include/llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h
rename to llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h

diff  --git a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
index 49f202244f96c17..fdb628ac84d4356 100644
--- a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt
@@ -13,8 +13,6 @@ add_llvm_component_library(LLVMOrcJIT
   CompileUtils.cpp
   Core.cpp
   DebugObjectManagerPlugin.cpp
-  DebuggerSupport.cpp
-  DebuggerSupportPlugin.cpp
   DebugUtils.cpp
   EPCDynamicLibrarySearchGenerator.cpp
   EPCDebugObjectRegistrar.cpp

diff  --git a/llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt
index 216761d8209084c..2c3b2bd877f0f11 100644
--- a/llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt
+++ b/llvm/lib/ExecutionEngine/Orc/Debugging/CMakeLists.txt
@@ -4,6 +4,8 @@ endif()
 
 add_llvm_component_library(LLVMOrcDebugging
   DebugInfoSupport.cpp
+  DebuggerSupport.cpp
+  DebuggerSupportPlugin.cpp
   PerfSupportPlugin.cpp
 
   ADDITIONAL_HEADER_DIRS

diff  --git a/llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp b/llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupport.cpp
similarity index 94%
rename from llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp
rename to llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupport.cpp
index 68d68f07b586203..9ba6dd90f50de51 100644
--- a/llvm/lib/ExecutionEngine/Orc/DebuggerSupport.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupport.cpp
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
+#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
 #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
-#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
+#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 
 #define DEBUG_TYPE "orc"

diff  --git a/llvm/lib/ExecutionEngine/Orc/DebuggerSupportPlugin.cpp b/llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.cpp
similarity index 82%
rename from llvm/lib/ExecutionEngine/Orc/DebuggerSupportPlugin.cpp
rename to llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.cpp
index 560311214bf1859..236ba5114130bc6 100644
--- a/llvm/lib/ExecutionEngine/Orc/DebuggerSupportPlugin.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.cpp
@@ -9,13 +9,17 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
+#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
 #include "llvm/ExecutionEngine/Orc/MachOBuilder.h"
 
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/BinaryFormat/MachO.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
+
+#include <chrono>
 
 #define DEBUG_TYPE "orc"
 
@@ -97,8 +101,6 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
              << "\n";
     });
 
-    auto &SDOSec = G.createSection(SynthDebugSectionName, MemProt::Read);
-
     for (auto &Sec : G.sections()) {
       if (Sec.blocks().empty())
         continue;
@@ -114,6 +116,10 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
         NonDebugSections.push_back({&Sec, nullptr});
     }
 
+    // Bail out early if no debug sections.
+    if (DebugSections.empty())
+      return Error::success();
+
     // Write MachO header and debug section load commands.
     Builder.Header.filetype = MachO::MH_OBJECT;
     switch (G.getTargetTriple().getArch()) {
@@ -131,16 +137,65 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
 
     Seg = &Builder.addSegment("");
 
+    StringMap<std::unique_ptr<MemoryBuffer>> DebugSectionMap;
+    StringRef DebugLineSectionData;
     for (auto &DSec : DebugSections) {
       auto [SegName, SecName] = DSec.GraphSec->getName().split(',');
       DSec.BuilderSec = &Seg->addSection(SecName, SegName);
 
       SectionRange SR(*DSec.GraphSec);
       DSec.BuilderSec->Content.Size = SR.getSize();
-      if (!SR.empty())
+      if (!SR.empty()) {
         DSec.BuilderSec->align = Log2_64(SR.getFirstBlock()->getAlignment());
+        StringRef SectionData(SR.getFirstBlock()->getContent().data(),
+                              SR.getFirstBlock()->getSize());
+        DebugSectionMap[SecName] =
+            MemoryBuffer::getMemBuffer(SectionData, G.getName(), false);
+        if (SecName == "__debug_line")
+          DebugLineSectionData = SectionData;
+      }
     }
 
+    std::optional<std::string> FileName;
+    if (!DebugLineSectionData.empty()) {
+      auto DWARFCtx = DWARFContext::create(DebugSectionMap, G.getPointerSize(),
+                                           G.getEndianness());
+      DWARFDataExtractor DebugLineData(
+          DebugLineSectionData,
+          G.getEndianness() == support::endianness::little, G.getPointerSize());
+      uint64_t Offset = 0;
+      DWARFDebugLine::LineTable LineTable;
+
+      // Try to parse line data. Consume error on failure.
+      if (auto Err = LineTable.parse(DebugLineData, &Offset, *DWARFCtx, nullptr,
+                                     consumeError)) {
+        handleAllErrors(
+          std::move(Err),
+          [&](ErrorInfoBase &EIB) {
+            LLVM_DEBUG({
+              dbgs() << "Cannot parse line table for \"" << G.getName() << "\": ";
+              EIB.log(dbgs());
+              dbgs() << "\n";
+            });
+          });
+      } else {
+        if (!LineTable.Prologue.FileNames.empty())
+          FileName = *dwarf::toString(LineTable.Prologue.FileNames[0].Name);
+      }
+    }
+
+    // If no line table (or unable to use) then use graph name.
+    // FIXME: There are probably other debug sections we should look in first.
+    if (!FileName)
+      FileName = G.getName();
+
+    Builder.addSymbol("", MachO::N_SO, 0, 0, 0);
+    Builder.addSymbol(*FileName, MachO::N_SO, 0, 0, 0);
+    auto TimeStamp = std::chrono::duration_cast<std::chrono::seconds>(
+                         std::chrono::system_clock::now().time_since_epoch())
+                         .count();
+    Builder.addSymbol("", MachO::N_OSO, 3, 1, TimeStamp);
+
     for (auto &NDSP : NonDebugSections) {
       auto [SegName, SecName] = NDSP.GraphSec->getName().split(',');
       NDSP.BuilderSec = &Seg->addSection(SecName, SegName);
@@ -164,8 +219,12 @@ class MachODebugObjectSynthesizer : public MachODebugObjectSynthesizerBase {
       }
     }
 
+    Builder.addSymbol("", MachO::N_SO, 1, 0, 0);
+
+    // Lay out the debug object, create a section and block for it.
     size_t DebugObjectSize = Builder.layout();
 
+    auto &SDOSec = G.createSection(SynthDebugSectionName, MemProt::Read);
     MachOContainerBlock = &G.createMutableContentBlock(
         SDOSec, G.allocateBuffer(DebugObjectSize), orc::ExecutorAddr(), 8, 0);
 

diff  --git a/llvm/tools/lli/CMakeLists.txt b/llvm/tools/lli/CMakeLists.txt
index 3b3cf91b82d01fc..315de28e12b57cc 100644
--- a/llvm/tools/lli/CMakeLists.txt
+++ b/llvm/tools/lli/CMakeLists.txt
@@ -12,8 +12,9 @@ set(LLVM_LINK_COMPONENTS
   MC
   MCJIT
   Object
-  OrcShared
   OrcJIT
+  OrcDebugging
+  OrcShared
   OrcTargetProcess
   Passes
   RuntimeDyld

diff  --git a/llvm/tools/lli/lli.cpp b/llvm/tools/lli/lli.cpp
index abe1c7556699b54..9d95cbcb29c25cd 100644
--- a/llvm/tools/lli/lli.cpp
+++ b/llvm/tools/lli/lli.cpp
@@ -26,7 +26,7 @@
 #include "llvm/ExecutionEngine/MCJIT.h"
 #include "llvm/ExecutionEngine/ObjectCache.h"
 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
-#include "llvm/ExecutionEngine/Orc/DebuggerSupport.h"
+#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
 #include "llvm/ExecutionEngine/Orc/EPCDynamicLibrarySearchGenerator.h"
 #include "llvm/ExecutionEngine/Orc/EPCEHFrameRegistrar.h"
 #include "llvm/ExecutionEngine/Orc/EPCGenericRTDyldMemoryManager.h"

diff  --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
index 17aeb3055953efb..0709f292a492c10 100644
--- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
+++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp
@@ -18,8 +18,8 @@
 #include "llvm/ExecutionEngine/Orc/COFFPlatform.h"
 #include "llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h"
 #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h"
-#include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h"
 #include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"
+#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupportPlugin.h"
 #include "llvm/ExecutionEngine/Orc/Debugging/PerfSupportPlugin.h"
 #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h"
 #include "llvm/ExecutionEngine/Orc/EPCDebugObjectRegistrar.h"


        


More information about the cfe-commits mailing list