Skip to content

Commit 3462b5d

Browse files
[Orc][examples] Check that debug support plugins append jit_code_entry
1 parent 1477b90 commit 3462b5d

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

llvm/examples/OrcV2Examples/LLJITWithRemoteDebugging/RemoteJITUtils.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,15 @@ launchLocalExecutor(StringRef ExecutablePath) {
9595
close(FromExecutor[ReadEnd]);
9696

9797
// Execute the child process.
98-
std::unique_ptr<char[]> ExecPath, FDSpecifier;
98+
std::unique_ptr<char[]> ExecPath, FDSpecifier, TestOutputFlag;
9999
{
100100
ExecPath = std::make_unique<char[]>(ExecutablePath.size() + 1);
101101
strcpy(ExecPath.get(), ExecutablePath.data());
102102

103+
const char *TestOutputFlagStr = "test-jitloadergdb";
104+
TestOutputFlag = std::make_unique<char[]>(strlen(TestOutputFlagStr) + 1);
105+
strcpy(TestOutputFlag.get(), TestOutputFlagStr);
106+
103107
std::string FDSpecifierStr("filedescs=");
104108
FDSpecifierStr += utostr(ToExecutor[ReadEnd]);
105109
FDSpecifierStr += ',';
@@ -108,7 +112,8 @@ launchLocalExecutor(StringRef ExecutablePath) {
108112
strcpy(FDSpecifier.get(), FDSpecifierStr.c_str());
109113
}
110114

111-
char *const Args[] = {ExecPath.get(), FDSpecifier.get(), nullptr};
115+
char *const Args[] = {ExecPath.get(), TestOutputFlag.get(),
116+
FDSpecifier.get(), nullptr};
112117
int RC = execvp(ExecPath.get(), Args);
113118
if (RC != 0)
114119
return make_error<StringError>(
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
# This test makes sure that the example builds and executes as expected.
1+
# Check that the debug support plugin appends a jit_code_entry to the
2+
# jit_descriptor of the child process.
3+
24
# Instructions for debugging can be found in LLJITWithRemoteDebugging.cpp
35

46
# REQUIRES: default_triple
57
# UNSUPPORTED: target=powerpc64{{.*}}
68

7-
# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1_elf.ll | FileCheck --check-prefix=CHECK0 %s
9+
# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1_elf.ll 2>&1 | FileCheck --check-prefix=CHECK0 %s
10+
# CHECK0: __jit_debug_descriptor.last_entry = [[BEFORE0:0x[0-9a-f]+]]
11+
# CHECK0-NOT: __jit_debug_descriptor.last_entry = [[BEFORE0]]
812
# CHECK0: Parsing input IR code from: {{.*}}/Inputs/argc_sub1_elf.ll
913
# CHECK0: Running: main()
1014
# CHECK0: Exit code: 0
1115

12-
# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1_elf.ll --args 2nd 3rd 4th | FileCheck --check-prefix=CHECK3 %s
16+
# RUN: LLJITWithRemoteDebugging %p/Inputs/argc_sub1_elf.ll --args 2nd 3rd 4th 2>&1 | FileCheck --check-prefix=CHECK3 %s
17+
# CHECK3: __jit_debug_descriptor.last_entry = [[BEFORE3:0x[0-9a-f]+]]
18+
# CHECK3-NOT: __jit_debug_descriptor.last_entry = [[BEFORE3]]
1319
# CHECK3: Parsing input IR code from: {{.*}}/Inputs/argc_sub1_elf.ll
1420
# CHECK3: Running: main("2nd", "3rd", "4th")
1521
# CHECK3: Exit code: 3

llvm/tools/llvm-jitlink/llvm-jitlink-executor/llvm-jitlink-executor.cpp

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,44 @@ int openListener(std::string Host, std::string PortStr) {
112112
#endif // LLVM_ON_UNIX
113113
}
114114

115+
// This must be kept in sync with gdb/gdb/jit.h .
116+
extern "C" {
117+
118+
typedef enum {
119+
JIT_NOACTION = 0,
120+
JIT_REGISTER_FN,
121+
JIT_UNREGISTER_FN
122+
} jit_actions_t;
123+
124+
struct jit_code_entry {
125+
struct jit_code_entry *next_entry;
126+
struct jit_code_entry *prev_entry;
127+
const char *symfile_addr;
128+
uint64_t symfile_size;
129+
};
130+
131+
struct jit_descriptor {
132+
uint32_t version;
133+
// This should be jit_actions_t, but we want to be specific about the
134+
// bit-width.
135+
uint32_t action_flag;
136+
struct jit_code_entry *relevant_entry;
137+
struct jit_code_entry *first_entry;
138+
};
139+
140+
// We put information about the JITed function in this global, which the
141+
// debugger reads. Make sure to specify the version statically, because the
142+
// debugger checks the version before we can set it during runtime.
143+
extern struct jit_descriptor __jit_debug_descriptor;
144+
145+
static void *findLastDebugDescriptorEntryPtr() {
146+
struct jit_code_entry *Last = __jit_debug_descriptor.first_entry;
147+
while (Last && Last->next_entry)
148+
Last = Last->next_entry;
149+
return Last;
150+
}
151+
}
152+
115153
int main(int argc, char *argv[]) {
116154
#if LLVM_ENABLE_THREADS
117155

@@ -121,10 +159,11 @@ int main(int argc, char *argv[]) {
121159
int InFD = 0;
122160
int OutFD = 0;
123161

162+
std::vector<StringRef> TestOutputFlags;
163+
124164
if (argc < 2)
125165
printErrorAndExit("insufficient arguments");
126166
else {
127-
128167
StringRef ConnectArg = argv[FirstProgramArg++];
129168
#ifndef NDEBUG
130169
if (ConnectArg == "debug") {
@@ -133,6 +172,11 @@ int main(int argc, char *argv[]) {
133172
}
134173
#endif
135174

175+
while (ConnectArg.starts_with("test-")) {
176+
TestOutputFlags.push_back(ConnectArg);
177+
ConnectArg = argv[FirstProgramArg++];
178+
}
179+
136180
StringRef SpecifierType, Specifier;
137181
std::tie(SpecifierType, Specifier) = ConnectArg.split('=');
138182
if (SpecifierType == "filedescs") {
@@ -156,6 +200,10 @@ int main(int argc, char *argv[]) {
156200
printErrorAndExit("invalid specifier type \"" + SpecifierType + "\"");
157201
}
158202

203+
if (llvm::is_contained(TestOutputFlags, "test-jitloadergdb"))
204+
fprintf(stderr, "__jit_debug_descriptor.last_entry = 0x%016" PRIx64 "\n",
205+
pointerToJITTargetAddress(findLastDebugDescriptorEntryPtr()));
206+
159207
auto Server =
160208
ExitOnErr(SimpleRemoteEPCServer::Create<FDSimpleRemoteEPCTransport>(
161209
[](SimpleRemoteEPCServer::Setup &S) -> Error {
@@ -173,6 +221,11 @@ int main(int argc, char *argv[]) {
173221
InFD, OutFD));
174222

175223
ExitOnErr(Server->waitForDisconnect());
224+
225+
if (llvm::is_contained(TestOutputFlags, "test-jitloadergdb"))
226+
fprintf(stderr, "__jit_debug_descriptor.last_entry = 0x%016" PRIx64 "\n",
227+
pointerToJITTargetAddress(findLastDebugDescriptorEntryPtr()));
228+
176229
return 0;
177230

178231
#else

0 commit comments

Comments
 (0)