-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathxdebugger.cpp
409 lines (360 loc) · 13.6 KB
/
xdebugger.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <cstdlib>
#include <fstream>
#include <string>
#include <thread>
// This must be included BEFORE pybind
// otherwise it fails to build on Windows
// because of the redefinition of snprintf
#include "nlohmann/json.hpp"
#include "pybind11_json/pybind11_json.hpp"
#include "pybind11/pybind11.h"
#include "pybind11/stl.h"
#include "xeus/xinterpreter.hpp"
#include "xeus/xmiddleware.hpp"
#include "xeus/xsystem.hpp"
#include "xeus-python/xdebugger.hpp"
#include "xptvsd_client.hpp"
#include "xutils.hpp"
namespace nl = nlohmann;
namespace py = pybind11;
using namespace pybind11::literals;
using namespace std::placeholders;
namespace xpyt
{
debugger::debugger(zmq::context_t& context,
const xeus::xconfiguration& config,
const std::string& user_name,
const std::string& session_id)
: p_ptvsd_client(new xptvsd_client(context, config, xeus::get_socket_linger(), user_name, session_id,
std::bind(&debugger::handle_event, this, _1)))
, m_ptvsd_socket(context, zmq::socket_type::req)
, m_ptvsd_header(context, zmq::socket_type::req)
, m_ptvsd_port("")
, m_is_started(false)
{
m_ptvsd_socket.setsockopt(ZMQ_LINGER, xeus::get_socket_linger());
m_ptvsd_header.setsockopt(ZMQ_LINGER, xeus::get_socket_linger());
m_ptvsd_port = xeus::find_free_port(100, 5678, 5900);
}
debugger::~debugger()
{
delete p_ptvsd_client;
p_ptvsd_client = nullptr;
}
nl::json debugger::process_request_impl(const nl::json& header,
const nl::json& message)
{
nl::json reply = nl::json::object();
if(message["command"] == "initialize")
{
if(m_is_started)
{
std::clog << "XEUS-PYTHON: the debugger has already started" << std::endl;
}
else
{
start();
std::clog << "XEUS-PYTHON: the debugger has started" << std::endl;
}
}
if(m_is_started)
{
std::string header_buffer = header.dump();
zmq::message_t raw_header(header_buffer.c_str(), header_buffer.length());
m_ptvsd_header.send(raw_header);
// client responds with ACK message
m_ptvsd_header.recv(&raw_header);
if(message["command"] == "dumpCell")
{
reply = dump_cell_request(message);
}
else if(message["command"] == "setBreakpoints")
{
reply = set_breakpoints_request(message);
}
else if(message["command"] == "source")
{
reply = source_request(message);
}
else if(message["command"] == "stackTrace")
{
reply = stack_trace_request(message);
}
else
{
reply = forward_message(message);
}
}
if(message["command"] == "debugInfo")
{
reply = debug_info_request(message);
}
else if(message["command"] == "inspectVariables")
{
reply = inspect_variables_request(message);
}
else if(message["command"] == "disconnect")
{
stop();
std::clog << "XEUS-PYTHON: the debugger has stopped" << std::endl;
}
return reply;
}
nl::json debugger::forward_message(const nl::json& message)
{
std::string content = message.dump();
size_t content_length = content.length();
std::string buffer = xptvsd_client::HEADER
+ std::to_string(content_length)
+ xptvsd_client::SEPARATOR
+ content;
zmq::message_t raw_message(buffer.c_str(), buffer.length());
m_ptvsd_socket.send(raw_message);
zmq::message_t raw_reply;
m_ptvsd_socket.recv(&raw_reply);
return nl::json::parse(std::string(raw_reply.data<const char>(), raw_reply.size()));
}
nl::json debugger::dump_cell_request(const nl::json& message)
{
std::string code;
try
{
code = message["arguments"]["code"];
}
catch(nl::json::type_error& e)
{
std::clog << e.what() << std::endl;
}
catch(...)
{
std::clog << "XEUS-PYTHON: Unknown issue" << std::endl;
}
std::string next_file_name = get_cell_tmp_file(code);
std::clog << "XEUS-PYTHON: dumped " << next_file_name << std::endl;
std::fstream fs(next_file_name, std::ios::in);
if(!fs.is_open())
{
fs.clear();
fs.open(next_file_name, std::ios::out);
fs << code;
}
nl::json reply = {
{"type", "response"},
{"request_seq", message["seq"]},
{"success", true},
{"command", message["command"]},
{"body", {
{"sourcePath", next_file_name}
}}
};
return reply;
}
nl::json debugger::set_breakpoints_request(const nl::json& message)
{
std::string source = message["arguments"]["source"]["path"];
m_breakpoint_list.erase(source);
nl::json breakpoint_reply = forward_message(message);
nl::json bp_json = breakpoint_reply["body"]["breakpoints"];
std::vector<nl::json> bp_list(bp_json.begin(), bp_json.end());
m_breakpoint_list.insert(std::make_pair(std::move(source), std::move(bp_list)));
return breakpoint_reply;
}
nl::json debugger::source_request(const nl::json& message)
{
std::string sourcePath;
try
{
sourcePath = message["arguments"]["source"]["path"];
}
catch(nl::json::type_error& e)
{
std::clog << e.what() << std::endl;
}
catch(...)
{
std::clog << "XEUS-PYTHON: Unknown issue" << std::endl;
}
std::ifstream ifs(sourcePath, std::ios::in);
if(!ifs.is_open())
{
nl::json reply = {
{"type", "response"},
{"request_seq", message["seq"]},
{"success", false},
{"command", message["command"]},
{"message", "source unavailable"},
{"body", {{}}}
};
return reply;
}
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
nl::json reply = {
{"type", "response"},
{"request_seq", message["seq"]},
{"success", true},
{"command", message["command"]},
{"body", {
{"content", content}
}}
};
return reply;
}
nl::json debugger::stack_trace_request(const nl::json& message)
{
nl::json reply = forward_message(message);
size_t size = reply["body"]["stackFrames"].size();
for(size_t i = 0; i < size; ++i)
{
if(reply["body"]["stackFrames"][i]["source"]["path"] == "<string>")
{
reply["body"]["stackFrames"].erase(i);
break;
}
}
return reply;
}
nl::json debugger::debug_info_request(const nl::json& message)
{
nl::json breakpoint_list = nl::json::array();
if(m_is_started)
{
for(auto it = m_breakpoint_list.cbegin(); it != m_breakpoint_list.cend(); ++it)
{
breakpoint_list.push_back({{"source", it->first},
{"breakpoints", it->second}});
}
}
std::lock_guard<std::mutex> lock(m_stopped_mutex);
nl::json reply = {
{"type", "response"},
{"request_seq", message["seq"]},
{"success", true},
{"command", message["command"]},
{"body", {
{"isStarted", m_is_started},
{"hashMethod", "Murmur2"},
{"hashSeed", get_hash_seed()},
{"tmpFilePrefix", get_tmp_prefix()},
{"tmpFileSuffix", get_tmp_suffix()},
{"breakpoints", breakpoint_list},
{"stoppedThreads", m_stopped_threads}
}}
};
return reply;
}
nl::json debugger::inspect_variables_request(const nl::json& message)
{
py::gil_scoped_acquire acquire;
py::object variables = py::globals();
nl::json json_var = nl::json::object();
for (const py::handle& key : variables)
{
std::string k = py::str(key).cast<std::string>();
try
{
json_var[k] = nl::detail::to_json_impl(variables[key]);
}
catch(std::exception&)
{
json_var[k] = nl::detail::to_json_impl(py::repr(variables[key]));
}
}
nl::json reply = {
{"type", "response"},
{"request_seq", message["seq"]},
{"success", true},
{"command", message["command"]},
{"body", {
{"variables", json_var}
}}
};
return reply;
}
void debugger::start()
{
std::string host = "127.0.0.1";
std::string temp_dir = xeus::get_temp_directory_path();
std::string log_dir = temp_dir + "/" + "xpython_debug_logs_" + std::to_string(xeus::get_current_pid());
xeus::create_directory(log_dir);
// PTVSD has to be started in the main thread
std::string code = "import ptvsd\nptvsd.enable_attach((\'" + host + "\'," + m_ptvsd_port
+ "), log_dir=\'" + log_dir + "\')";
nl::json json_code;
json_code["code"] = code;
nl::json rep = xdebugger::get_control_messenger().send_to_shell(json_code);
std::string status = rep["status"].get<std::string>();
if(status != "ok")
{
std::string ename = rep["ename"].get<std::string>();
std::string evalue = rep["evalue"].get<std::string>();
std::vector<std::string> traceback = rep["traceback"].get<std::vector<std::string>>();
std::clog << "Exception raised when trying to import ptvsd" << std::endl;
for(std::size_t i = 0; i < traceback.size(); ++i)
{
std::clog << traceback[i] << std::endl;
}
std::clog << ename << " - " << evalue << std::endl;
}
std::string controller_end_point = xeus::get_controller_end_point("debugger");
std::string controller_header_end_point = xeus::get_controller_end_point("debugger_header");
std::string publisher_end_point = xeus::get_publisher_end_point();
m_ptvsd_socket.bind(controller_end_point);
m_ptvsd_header.bind(controller_header_end_point);
std::string ptvsd_end_point = "tcp://" + host + ':' + m_ptvsd_port;
std::thread client(&xptvsd_client::start_debugger,
p_ptvsd_client,
ptvsd_end_point,
publisher_end_point,
controller_end_point,
controller_header_end_point);
client.detach();
m_ptvsd_socket.send(zmq::message_t("REQ", 3));
zmq::message_t ack;
m_ptvsd_socket.recv(&ack);
m_is_started = true;
std::string tmp_folder = get_tmp_prefix();
xeus::create_directory(tmp_folder);
}
void debugger::stop()
{
std::string controller_end_point = xeus::get_controller_end_point("debugger");
std::string controller_header_end_point = xeus::get_controller_end_point("debugger_header");
m_ptvsd_socket.unbind(controller_end_point);
m_ptvsd_header.unbind(controller_header_end_point);
m_breakpoint_list.clear();
m_stopped_threads.clear();
m_is_started = false;
}
void debugger::handle_event(const nl::json& message)
{
std::string event = message["event"];
if(event == "stopped")
{
std::lock_guard<std::mutex> lock(m_stopped_mutex);
int id = message["body"]["threadId"];
m_stopped_threads.insert(id);
}
else if(event == "continued")
{
std::lock_guard<std::mutex> lock(m_stopped_mutex);
int id = message["body"]["threadId"];
m_stopped_threads.erase(id);
}
}
std::unique_ptr<xeus::xdebugger> make_python_debugger(zmq::context_t& context,
const xeus::xconfiguration& config,
const std::string& user_name,
const std::string& session_id)
{
return std::unique_ptr<xeus::xdebugger>(new debugger(context, config, user_name, session_id));
}
}