Skip to content

Commit a6a02dc

Browse files
author
kr-2003
committed
out-of-process JIT execution
1 parent ae46678 commit a6a02dc

File tree

6 files changed

+116
-83
lines changed

6 files changed

+116
-83
lines changed

.vscode/settings.json

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
11
{
22
"files.associations": {
3-
"iostream": "cpp"
3+
"iostream": "cpp",
4+
"fstream": "cpp",
5+
"ios": "cpp",
6+
"locale": "cpp",
7+
"sstream": "cpp",
8+
"__bit_reference": "cpp",
9+
"__hash_table": "cpp",
10+
"__locale": "cpp",
11+
"__node_handle": "cpp",
12+
"__split_buffer": "cpp",
13+
"__tree": "cpp",
14+
"__verbose_abort": "cpp",
15+
"array": "cpp",
16+
"bitset": "cpp",
17+
"cctype": "cpp",
18+
"clocale": "cpp",
19+
"cmath": "cpp",
20+
"cstddef": "cpp",
21+
"cstdint": "cpp",
22+
"cstdio": "cpp",
23+
"cstdlib": "cpp",
24+
"cstring": "cpp",
25+
"ctime": "cpp",
26+
"cwchar": "cpp",
27+
"initializer_list": "cpp",
28+
"iosfwd": "cpp",
29+
"istream": "cpp",
30+
"limits": "cpp",
31+
"map": "cpp",
32+
"mutex": "cpp",
33+
"new": "cpp",
34+
"print": "cpp",
35+
"regex": "cpp",
36+
"stdexcept": "cpp",
37+
"streambuf": "cpp",
38+
"string": "cpp",
39+
"string_view": "cpp",
40+
"typeinfo": "cpp",
41+
"unordered_map": "cpp",
42+
"vector": "cpp",
43+
"cstdarg": "cpp",
44+
"cwctype": "cpp",
45+
"deque": "cpp",
46+
"execution": "cpp",
47+
"iomanip": "cpp",
48+
"optional": "cpp",
49+
"queue": "cpp",
50+
"ratio": "cpp",
51+
"stack": "cpp",
52+
"variant": "cpp"
453
}
554
}

include/xeus-cpp/xdebugger.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ namespace xcpp
6969
bool m_is_running;
7070
int m_tcp_socket;
7171
bool m_tcp_connected;
72+
std::string jit_process_pid;
7273
};
7374

7475
XEUS_CPP_API

include/xeus-cpp/xinterpreter.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ namespace xcpp
4040
void publish_stdout(const std::string&);
4141
void publish_stderr(const std::string&);
4242

43+
static std::string get_current_pid();
44+
4345
private:
4446

4547
void configure_impl() override;

share/jupyter/kernels/xcpp17/kernel.json.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
"-std=c++17"
1414
],
1515
"language": "cpp",
16-
"metadata": {"debugger": true
16+
"metadata": {"debugger": false
1717
}
1818
}

src/xdebugger.cpp

Lines changed: 17 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
#include "xeus-cpp/xdebugger.hpp"
22

3-
#include <arpa/inet.h> // For inet_pton(), htons()
4-
#include <chrono> // For std::chrono (used in sleep_for)
3+
#include <arpa/inet.h>
4+
#include <chrono>
55
#include <cstdlib>
66
#include <fcntl.h>
77
#include <fstream>
88
#include <iostream>
9-
#include <netinet/in.h> // For sockaddr_in, AF_INET
10-
#include <sys/socket.h> // For socket(), connect(), send(), recv()
9+
#include <netinet/in.h>
10+
#include <sys/socket.h>
1111
#include <sys/types.h>
1212
#include <sys/wait.h>
1313
#include <thread>
1414
#include <unistd.h>
1515

1616
#include "nlohmann/json.hpp"
1717
#include "xdebuglldb_client.hpp"
18+
#include "xeus-cpp/xinterpreter.hpp"
1819
#include "xeus-zmq/xmiddleware.hpp"
1920
#include "xeus/xsystem.hpp"
2021
#include "xinternal_utils.hpp"
@@ -56,8 +57,6 @@ namespace xcpp
5657
std::bind(&debugger::configuration_done_request, this, _1),
5758
true
5859
);
59-
60-
std::cout << "Debugger initialized with config: " << m_debugger_config.dump() << std::endl;
6160
}
6261

6362
debugger::~debugger()
@@ -70,6 +69,7 @@ namespace xcpp
7069
bool debugger::start_lldb()
7170
{
7271
std::cout << "debugger::start_lldb" << std::endl;
72+
jit_process_pid = xcpp::interpreter::get_current_pid();
7373

7474
// Find a free port for LLDB-DAP
7575
m_lldb_port = xeus::find_free_port(100, 9999, 10099);
@@ -263,23 +263,6 @@ namespace xcpp
263263
);
264264
client.detach();
265265

266-
// Now test direct TCP communication
267-
nl::json init_request = {
268-
{"seq", 1},
269-
{"type", "request"},
270-
{"command", "initialize"},
271-
{"arguments",
272-
{{"adapterID", "xcpp17"},
273-
{"clientID", "jupyterlab"},
274-
{"clientName", "JupyterLab"},
275-
{"columnsStartAt1", true},
276-
{"linesStartAt1", true},
277-
{"locale", "en"},
278-
{"pathFormat", "path"},
279-
{"supportsRunInTerminalRequest", true},
280-
{"supportsVariablePaging", true},
281-
{"supportsVariableType", true}}}
282-
};
283266
// Also test ZMQ path
284267
send_recv_request("REQ");
285268

@@ -295,42 +278,8 @@ namespace xcpp
295278
// Dummy implementations for other methods
296279
nl::json debugger::inspect_variables_request(const nl::json& message)
297280
{
298-
// Placeholder DAP response
299-
// std::cout << "Sending setBreakpoints request..." << std::endl;
300-
// nl::json breakpoint_request = {
301-
// {"seq", 3},
302-
// {"type", "request"},
303-
// {"command", "setBreakpoints"},
304-
// {"arguments", {
305-
// {"source", {
306-
// {"name", "input_line_1"},
307-
// {"path", "/Users/abhinavkumar/Desktop/Coding/Testing/input_line_1"}
308-
// }},
309-
// {"breakpoints", {{{"line", 8}}}},
310-
// {"lines", {8}},
311-
// {"sourceModified", false}
312-
// }}
313-
// };
314-
// nl::json breakpoint_reply = forward_message(breakpoint_request);
315-
// std::cout << "Breakpoint reply: " << breakpoint_reply.dump() << std::endl;
316-
// nl::json config_done_request = {
317-
// {"seq", 4},
318-
// {"type", "request"},
319-
// {"command", "configurationDone"}
320-
// };
321-
// nl::json config_reply = forward_message(config_done_request);
322-
// std::cout << "Configuration done reply: " << config_reply.dump() << std::endl;
323-
324-
// nl::json run_request = {
325-
// {"seq", 5},
326-
// {"type", "request"},
327-
// {"command", "continue"},
328-
// {"arguments", nl::json::object()}
329-
// };
330-
// nl::json run_reply = forward_message(run_request);
331-
// std::cout << "Continue reply: " << run_reply.dump() << std::endl;
332-
333-
std::cout << "inspect_variables_request not implemented" << std::endl;
281+
std::cout << "[debugger::inspect_variables_request] inspect_variables_request not implemented"
282+
<< std::endl;
334283
std::cout << message.dump() << std::endl;
335284
nl::json reply = {
336285
{"type", "response"},
@@ -339,16 +288,12 @@ namespace xcpp
339288
{"command", message["command"]},
340289
{"body",
341290
{{"variables",
342-
{{{"name", "a"},
343-
{"value", "100"},
344-
{"type", "int"},
345-
{"evaluateName", "a"},
346-
{"variablesReference", 0}},
347-
{{"name", "b"},
348-
{"value", "1000"},
349-
{"type", "int"},
350-
{"evaluateName", "b"},
351-
{"variablesReference", 0}}}}}}
291+
{{{"name", "a"}, {"value", "100"}, {"type", "int"}, {"evaluateName", "a"}, {"variablesReference", 0}},
292+
{{"name", "b"},
293+
{"value", "1000"},
294+
{"type", "int"},
295+
{"evaluateName", "b"},
296+
{"variablesReference", 0}}}}}}
352297
};
353298
return reply;
354299
}
@@ -372,17 +317,16 @@ namespace xcpp
372317
{
373318
// Placeholder DAP response
374319
std::cout << "debugger::attach_request" << std::endl;
320+
std::cout << "Message: " << message.dump() << std::endl;
375321
nl::json attach_request = {
376322
{"seq", 2},
377323
{"type", "request"},
378324
{"command", "attach"},
379325
{"arguments", {
380-
{"pid", message["arguments"].value("pid", 0)},
381-
{"program", message["arguments"].value("program", "")},
382-
{"stopOnEntry", message["arguments"].value("stopOnEntry", false)},
383-
{"initCommands", message["arguments"].value("initCommands", nl::json::array())}
326+
{"pid", jit_process_pid}
384327
}}
385328
};
329+
std::cout << "Sending attach request: " << attach_request.dump() << std::endl;
386330
nl::json reply = forward_message(attach_request);
387331
std::cout << "Attach request sent: " << reply.dump() << std::endl;
388332
return reply;

src/xinterpreter.cpp

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,32 @@ void* createInterpreter(const Args &ExtraArgs = {}) {
4646
ClangArgs.push_back(CxxInclude.c_str());
4747
}
4848
ClangArgs.insert(ClangArgs.end(), ExtraArgs.begin(), ExtraArgs.end());
49+
4950
// FIXME: We should process the kernel input options and conditionally pass
5051
// the gpu args here.
51-
return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/);
52+
if (std::find_if(ExtraArgs.begin(), ExtraArgs.end(), [](const std::string& s) {
53+
return s == "-gdwarf-4";}) == ExtraArgs.end()) {
54+
// If no debugger option, then use the non-OOP JIT execution.
55+
return Cpp::CreateInterpreter(ClangArgs/*, {"-cuda"}*/);
56+
}
57+
58+
// If debugger option is set, then use the OOP JIT execution.
59+
return Cpp::CreateInterpreter(ClangArgs, {}, true);
5260
}
5361

5462
using namespace std::placeholders;
5563

5664
namespace xcpp
5765
{
5866
struct StreamRedirectRAII {
59-
std::string &err;
60-
StreamRedirectRAII(std::string &e) : err(e) {
67+
std::string &out, &err;
68+
StreamRedirectRAII(std::string &o, std::string &e) : out(o), err(e) {
6169
Cpp::BeginStdStreamCapture(Cpp::kStdErr);
6270
Cpp::BeginStdStreamCapture(Cpp::kStdOut);
6371
}
6472
~StreamRedirectRAII() {
65-
std::string out = Cpp::EndStdStreamCapture();
73+
out = Cpp::EndStdStreamCapture();
6674
err = Cpp::EndStdStreamCapture();
67-
std::cout << out;
6875
}
6976
};
7077

@@ -73,6 +80,33 @@ namespace xcpp
7380
xeus::register_interpreter(this);
7481
}
7582

83+
std::string interpreter::get_current_pid()
84+
{
85+
const char* code = R"(
86+
#include <iostream>
87+
#include <unistd.h>
88+
std::cout << getpid() << std::endl;
89+
)";
90+
91+
std::string pid_str, err;
92+
bool compilation_result = false;
93+
{
94+
StreamRedirectRAII R(pid_str, err);
95+
compilation_result = Cpp::Process(code);
96+
}
97+
if (compilation_result) {
98+
std::cerr << "Error getting OOP JIT pid: " << err << std::endl;
99+
return "";
100+
} else {
101+
std::cout << "Current OOP JIT PID: " << pid_str << std::endl;
102+
}
103+
// Remove trailing newline from pid_str if present
104+
if (!pid_str.empty() && pid_str.back() == '\n') {
105+
pid_str.pop_back();
106+
}
107+
return pid_str;
108+
}
109+
76110
static std::string get_stdopt()
77111
{
78112
// We need to find what's the C++ version the interpreter runs with.
@@ -109,7 +143,7 @@ __get_cxx_version ()
109143
{
110144
//NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
111145
createInterpreter(Args(argv ? argv + 1 : argv, argv + argc));
112-
m_version = get_stdopt();
146+
// m_version = get_stdopt();
113147
redirect_output();
114148
init_preamble();
115149
init_magic();
@@ -162,12 +196,12 @@ __get_cxx_version ()
162196
std::cerr.rdbuf(&null);
163197
}
164198

165-
std::string err;
199+
std::string out, err;
166200

167201
// Attempt normal evaluation
168202
try
169203
{
170-
StreamRedirectRAII R(err);
204+
StreamRedirectRAII R(out, err);
171205
compilation_result = Cpp::Process(code.c_str());
172206
}
173207
catch (std::exception& e)
@@ -188,6 +222,9 @@ __get_cxx_version ()
188222
ename = "Error: ";
189223
evalue = "Compilation error! " + err;
190224
std::cerr << err;
225+
} else {
226+
std::cout << "EHLLOE" << std::endl;
227+
std::cout << out;
191228
}
192229

193230
// Flush streams

0 commit comments

Comments
 (0)