Skip to content

Commit a91f9b8

Browse files
[llvm-c] Expose debug support for LLJIT in Orc bindings
1 parent 1d6a678 commit a91f9b8

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

llvm/include/llvm-c/LLJIT.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,12 @@ LLVMOrcIRTransformLayerRef LLVMOrcLLJITGetIRTransformLayer(LLVMOrcLLJITRef J);
242242
*/
243243
const char *LLVMOrcLLJITGetDataLayoutStr(LLVMOrcLLJITRef J);
244244

245+
/**
246+
* Install the plugin that submits debug objects to the executor. Executors must
247+
* expose the llvm_orc_registerJITLoaderGDBWrapper symbol.
248+
*/
249+
LLVMErrorRef LLVMOrcLLJITEnableDebugSupport(LLVMOrcLLJITRef J);
250+
245251
/**
246252
* @}
247253
*/

llvm/lib/ExecutionEngine/Orc/Debugging/DebuggerSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Error enableDebuggerSupport(LLJIT &J) {
3939
if (!Registrar)
4040
return Registrar.takeError();
4141
ObjLinkingLayer->addPlugin(std::make_unique<DebugObjectManagerPlugin>(
42-
ES, std::move(*Registrar), true, true));
42+
ES, std::move(*Registrar), false, true));
4343
return Error::success();
4444
}
4545
case Triple::MachO: {

llvm/lib/ExecutionEngine/Orc/OrcV2CBindings.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm-c/OrcEE.h"
1212
#include "llvm-c/TargetMachine.h"
1313

14+
#include "llvm/ExecutionEngine/Orc/Debugging/DebuggerSupport.h"
1415
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
1516
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
1617
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
@@ -1179,3 +1180,7 @@ void LLVMOrcDisposeLazyCallThroughManager(
11791180
LLVMOrcLazyCallThroughManagerRef LCM) {
11801181
std::unique_ptr<LazyCallThroughManager> TmpLCM(unwrap(LCM));
11811182
}
1183+
1184+
LLVMErrorRef LLVMOrcLLJITEnableDebugSupport(LLVMOrcLLJITRef J) {
1185+
return wrap(llvm::orc::enableDebuggerSupport(*unwrap(J)));
1186+
}

llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(LLVM_LINK_COMPONENTS
55
IRReader
66
JITLink
77
Object
8+
OrcDebugging
89
OrcJIT
910
OrcShared
1011
OrcTargetProcess

llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,70 @@ TEST_F(OrcCAPITestBase, AddObjectBuffer) {
494494
ASSERT_TRUE(!!SumAddr);
495495
}
496496

497+
// This must be kept in sync with gdb/gdb/jit.h .
498+
extern "C" {
499+
500+
typedef enum {
501+
JIT_NOACTION = 0,
502+
JIT_REGISTER_FN,
503+
JIT_UNREGISTER_FN
504+
} jit_actions_t;
505+
506+
struct jit_code_entry {
507+
struct jit_code_entry *next_entry;
508+
struct jit_code_entry *prev_entry;
509+
const char *symfile_addr;
510+
uint64_t symfile_size;
511+
};
512+
513+
struct jit_descriptor {
514+
uint32_t version;
515+
// This should be jit_actions_t, but we want to be specific about the
516+
// bit-width.
517+
uint32_t action_flag;
518+
struct jit_code_entry *relevant_entry;
519+
struct jit_code_entry *first_entry;
520+
};
521+
522+
// We put information about the JITed function in this global, which the
523+
// debugger reads. Make sure to specify the version statically, because the
524+
// debugger checks the version before we can set it during runtime.
525+
struct jit_descriptor __jit_debug_descriptor;
526+
527+
static void *findLastDebugDescriptorEntryPtr() {
528+
struct jit_code_entry *Last = __jit_debug_descriptor.first_entry;
529+
while (Last && Last->next_entry)
530+
Last = Last->next_entry;
531+
return Last;
532+
}
533+
}
534+
535+
#if defined(_AIX) or (not defined(__ELF__) and not defined(__MACH__))
536+
TEST_F(OrcCAPITestBase, DISABLED_EnableDebugSupport) {
537+
#else
538+
TEST_F(OrcCAPITestBase, EnableDebugSupport) {
539+
#endif
540+
if (LLVMErrorRef E = LLVMOrcLLJITEnableDebugSupport(Jit))
541+
FAIL() << "Error testing LLJIT debug support (triple = " << TargetTriple
542+
<< "): " << toString(E);
543+
544+
void *Before = findLastDebugDescriptorEntryPtr();
545+
LLVMMemoryBufferRef ObjBuffer = createTestObject(SumExample, "sum.ll");
546+
LLVMOrcObjectLayerRef ObjLayer = LLVMOrcLLJITGetObjLinkingLayer(Jit);
547+
if (LLVMErrorRef E =
548+
LLVMOrcObjectLayerAddObjectFile(ObjLayer, MainDylib, ObjBuffer))
549+
FAIL() << "Failed to add object file to ObjLinkingLayer (triple = "
550+
<< TargetTriple << "): " << toString(E);
551+
552+
LLVMOrcJITTargetAddress SumAddr;
553+
if (LLVMErrorRef E = LLVMOrcLLJITLookup(Jit, &SumAddr, "sum"))
554+
FAIL() << "Symbol \"sum\" was not added into JIT (triple = " << TargetTriple
555+
<< "): " << toString(E);
556+
557+
void *After = findLastDebugDescriptorEntryPtr();
558+
ASSERT_NE(Before, After);
559+
}
560+
497561
#if defined(_AIX)
498562
TEST_F(OrcCAPITestBase, DISABLED_ExecutionTest) {
499563
#else

0 commit comments

Comments
 (0)