From ceb4f30fec3ae201201f0d1ce129d645d3a2133d Mon Sep 17 00:00:00 2001 From: Devajith Valaparambil Sreeramaswamy Date: Fri, 24 Oct 2025 18:46:05 +0200 Subject: [PATCH] [cling] Enable JITLink debugging/profiling --- .../cling/lib/Interpreter/CMakeLists.txt | 1 + .../cling/lib/Interpreter/IncrementalJIT.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/interpreter/cling/lib/Interpreter/CMakeLists.txt b/interpreter/cling/lib/Interpreter/CMakeLists.txt index a7ae46d881dbb..fb4a3ae7eab84 100644 --- a/interpreter/cling/lib/Interpreter/CMakeLists.txt +++ b/interpreter/cling/lib/Interpreter/CMakeLists.txt @@ -48,6 +48,7 @@ set(LLVM_LINK_COMPONENTS object option orcjit + orcdebugging runtimedyld scalaropts support diff --git a/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp b/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp index 944f68a491a0d..fabb0d5ab1c9e 100644 --- a/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp +++ b/interpreter/cling/lib/Interpreter/IncrementalJIT.cpp @@ -19,6 +19,9 @@ #include #include +#include +#include +#include #include #include #include @@ -491,6 +494,16 @@ IncrementalJIT::IncrementalJIT( Builder.setDataLayout(m_TM->createDataLayout()); Builder.setExecutorProcessControl(std::move(EPC)); + if (m_JITLink && + cling::utils::ConvertEnvValueToBool(std::getenv("CLING_DEBUG"))) { + Builder.setPrePlatformSetup([](llvm::orc::LLJIT& J) { + // Try to enable debugging of JIT'd code (only works with JITLink for + // ELF and MachO). + consumeError(enableDebuggerSupport(J)); + return llvm::Error::success(); + }); + } + // Create ObjectLinkingLayer with our own MemoryManager. Builder.setObjectLinkingLayerCreator([&](ExecutionSession& ES, const Triple& TT) @@ -503,6 +516,36 @@ IncrementalJIT::IncrementalJIT( unsigned PageSize = cantFail(sys::Process::getPageSize()); auto ObjLinkingLayer = std::make_unique( ES, std::make_unique(PageSize)); + +#ifdef __linux__ + if (cling::utils::ConvertEnvValueToBool(std::getenv("CLING_PROFILE")) && + TT.isOSBinFormatELF()) { + auto ProcessSymsJD = ES.getJITDylibByName(""); + if (!ProcessSymsJD) { + Err = make_error( + "MachO debugging requires process symbols", + inconvertibleErrorCode()); + return ObjLinkingLayer; + } + if (Expected> + debugInfoPreservationPlugin = + DebugInfoPreservationPlugin::Create()) { + ObjLinkingLayer->addPlugin( + std::move(debugInfoPreservationPlugin.get())); + } else { + Err = debugInfoPreservationPlugin.takeError(); + return ObjLinkingLayer; + } + if (Expected> perfSupportPlugin = + PerfSupportPlugin::Create(ES.getExecutorProcessControl(), + *ProcessSymsJD, true, true)) { + ObjLinkingLayer->addPlugin(std::move(perfSupportPlugin.get())); + } else { + Err = perfSupportPlugin.takeError(); + return ObjLinkingLayer; + } + } +#endif return ObjLinkingLayer; }