Reland "[clang-repl] Enable debugging of JIT-ed code."
authorVassil Vassilev <v.g.vassilev@gmail.com>
Tue, 18 Apr 2023 16:22:04 +0000 (16:22 +0000)
committerVassil Vassilev <v.g.vassilev@gmail.com>
Tue, 18 Apr 2023 18:33:52 +0000 (18:33 +0000)
Original commit message: "
[clang-repl] Enable debugging of JIT-ed code.

    This change follows llvm/llvm-project@21b5ebd and makes use of the jitlink
    infrastructure. In order to use this feature inside lldb one needs to run the
    lldb command: settings set plugin.jit-loader.gdb.enable on

    This works currently only on Darwin since jitlink is not a default ELF/x86-64
    backend yet.

    Differential revision: https://reviews.llvm.org/D148481
"

This patch reverts commit e64fbf2cca8c4763a058ba59a48ab8e4b8193028 and adds
the missing library dependencies which caused the initial failure.

clang/lib/Interpreter/CMakeLists.txt
clang/lib/Interpreter/IncrementalExecutor.cpp

index c923624..721864c 100644 (file)
@@ -3,6 +3,8 @@ set(LLVM_LINK_COMPONENTS
    native
    Option
    OrcJit
+   OrcShared
+   OrcTargetProcess
    Support
    Target
    TargetParser
index fdf12dd..3f8d606 100644 (file)
 #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h"
 #include "llvm/ExecutionEngine/Orc/LLJIT.h"
 #include "llvm/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.h"
+#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderGDB.h"
 #include "llvm/ExecutionEngine/SectionMemoryManager.h"
 #include "llvm/IR/Module.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/TargetSelect.h"
 
+// Force linking some of the runtimes that helps attaching to a debugger.
+LLVM_ATTRIBUTE_USED void linkComponents() {
+  llvm::errs() << (void *)&llvm_orc_registerJITLoaderGDBWrapper
+               << (void *)&llvm_orc_registerJITLoaderGDBAllocAction;
+}
+
 namespace clang {
 
 IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
@@ -37,7 +44,12 @@ IncrementalExecutor::IncrementalExecutor(llvm::orc::ThreadSafeContext &TSC,
 
   auto JTMB = JITTargetMachineBuilder(TI.getTriple());
   JTMB.addFeatures(TI.getTargetOpts().Features);
-  if (auto JitOrErr = LLJITBuilder().setJITTargetMachineBuilder(JTMB).create())
+  LLJITBuilder Builder;
+  Builder.setJITTargetMachineBuilder(JTMB);
+  // Enable debugging of JIT'd code (only works on JITLink for ELF and MachO).
+  Builder.setEnableDebuggerSupport(true);
+
+  if (auto JitOrErr = Builder.create())
     Jit = std::move(*JitOrErr);
   else {
     Err = JitOrErr.takeError();