Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed artificial steps due to interactive mode #169

Merged
merged 1 commit into from
Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 135 additions & 16 deletions src/xptvsd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace xpyt

if(items[2].revents & ZMQ_POLLIN)
{
handle_ptvsd_socket();
handle_ptvsd_socket(m_message_queue);
}

process_message_queue();
Expand All @@ -108,18 +108,7 @@ namespace xpyt
// message is either an event or a response
if(message["type"] == "event")
{
m_event_callback(message);
zmq::multipart_t wire_msg;
nl::json header = xeus::make_header("debug_event", m_user_name, m_session_id);
nl::json parent_header = m_parent_header.empty() ? nl::json::object() : nl::json::parse(m_parent_header);
xeus::xpub_message msg("debug_event",
std::move(header),
std::move(parent_header),
nl::json::object(),
std::move(message),
xeus::buffer_sequence());
std::move(msg).serialize(wire_msg, *p_auth);
wire_msg.send(m_publisher);
handle_event(std::move(message));
}
else
{
Expand All @@ -142,7 +131,7 @@ namespace xpyt
m_controller_header.send(zmq::message_t("ACK", 3));
}

void xptvsd_client::handle_ptvsd_socket()
void xptvsd_client::handle_ptvsd_socket(queue_type& message_queue)
{
using size_type = std::string::size_type;

Expand Down Expand Up @@ -182,7 +171,7 @@ namespace xpyt
// The end of the buffer contains a full message
if(buffer.size() - msg_pos == msg_size)
{
m_message_queue.push(buffer.substr(msg_pos));
message_queue.push(buffer.substr(msg_pos));
messages_received = true;
}
else
Expand All @@ -191,7 +180,7 @@ namespace xpyt
// and the beginning of a new one. We push the first
// one in the queue, and loop again to get the next
// one.
m_message_queue.push(buffer.substr(msg_pos, msg_size));
message_queue.push(buffer.substr(msg_pos, msg_size));
hint = msg_pos + msg_size;
header_pos = buffer.find(HEADER, hint);
separator_pos = std::string::npos;
Expand Down Expand Up @@ -221,5 +210,135 @@ namespace xpyt

buffer += std::string(content.data<const char>(), content.size());
}

void xptvsd_client::handle_event(nl::json message)
{
bool wait_cond = message["event"] == "stopped" && message["body"]["reason"] == "step";
while(wait_cond)
{
int thread_id = message["body"]["threadId"];
int seq = message["seq"];
nl::json frames = get_stack_frames(thread_id, seq);
if(frames.size() == 1 && frames[0]["source"]["path"]=="<string>")
{
message = wait_next(thread_id, seq);
}
else
{
wait_cond = false;
}
}
forward_event(std::move(message));
}

void xptvsd_client::forward_event(nl::json message)
{
m_event_callback(message);
zmq::multipart_t wire_msg;
nl::json header = xeus::make_header("debug_event", m_user_name, m_session_id);
nl::json parent_header = m_parent_header.empty() ? nl::json::object() : nl::json::parse(m_parent_header);
xeus::xpub_message msg("debug_event",
std::move(header),
std::move(parent_header),
nl::json::object(),
std::move(message),
xeus::buffer_sequence());
std::move(msg).serialize(wire_msg, *p_auth);
wire_msg.send(m_publisher);
}

nl::json xptvsd_client::get_stack_frames(int thread_id, int seq)
{
nl::json request = {
{"type", "request"},
{"seq", seq},
{"command", "stackTrace"},
{"arguments", {
{"threadId", thread_id}
}}
};

send_ptvsd_request(std::move(request));

bool wait_for_stack_frame = true;
nl::json reply;
while(wait_for_stack_frame)
{
handle_ptvsd_socket(m_stopped_queue);
while(!m_stopped_queue.empty())
{
const std::string& raw_message = m_stopped_queue.front();
nl::json message = nl::json::parse(raw_message);
if(message["type"] == "response" && message["command"] == "stackTrace")
{
reply = std::move(message);
wait_for_stack_frame = false;
}
else
{
m_message_queue.push(raw_message);
}
m_stopped_queue.pop();
}
}
return reply["body"]["stackFrames"];
}

nl::json xptvsd_client::wait_next(int thread_id, int seq)
{
nl::json request = {
{"type", "request"},
{"seq", seq},
{"command", "next"},
{"arguments", {
{"threadId", thread_id}
}}
};

send_ptvsd_request(std::move(request));

nl::json reply;
bool wait_cond = true;
while(wait_cond)
{
handle_ptvsd_socket(m_stopped_queue);

while(!m_stopped_queue.empty())
{
const std::string& raw_message = m_stopped_queue.front();
nl::json message = nl::json::parse(raw_message);
std::string msg_type = message["type"];
if(msg_type == "event")
{
std::string evt_type = message["event"];
if((evt_type == "continued" || evt_type == "stopped") && message["body"]["threadId"] != thread_id)
{
m_message_queue.push(raw_message);
}
else if(evt_type == "stopped" && message["body"]["threadId"] == thread_id)
{
wait_cond = false;
reply = std::move(message);
}
}
m_stopped_queue.pop();
}
}
return reply;
}

void xptvsd_client::send_ptvsd_request(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(zmq::message_t(m_socket_id, m_id_size), ZMQ_SNDMORE);
m_ptvsd_socket.send(raw_message);
}
}

12 changes: 10 additions & 2 deletions src/xptvsd_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,18 @@ namespace xpyt

private:

using queue_type = std::queue<std::string>;

void process_message_queue();
void handle_header_socket();
void handle_ptvsd_socket();
void handle_ptvsd_socket(queue_type& message_queue);
void handle_control_socket();
void append_tcp_message(std::string& buffer);
void handle_event(nl::json message);
void forward_event(nl::json message);
nl::json get_stack_frames(int thread_id, int seq);
nl::json wait_next(int thread_id, int seq);
void send_ptvsd_request(nl::json message);

zmq::socket_t m_ptvsd_socket;
std::size_t m_id_size;
Expand All @@ -77,7 +84,8 @@ namespace xpyt

bool m_request_stop;

std::queue<std::string> m_message_queue;
queue_type m_message_queue;
queue_type m_stopped_queue;
};
}

Expand Down
57 changes: 56 additions & 1 deletion test/test_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ class debugger_client
bool test_step_in();
bool test_debug_info();
bool test_inspect_variables();
bool test_next();
void shutdown();

private:
Expand Down Expand Up @@ -578,6 +579,47 @@ bool debugger_client::test_inspect_variables()
return res;
}

bool debugger_client::test_next()
{
attach();
{
m_client.send_on_control("debug_request", make_dump_cell_request(4, make_code()));
nl::json res = m_client.receive_on_control();
std::string path = res["content"]["body"]["sourcePath"].get<std::string>();
m_client.send_on_control("debug_request", make_breakpoint_request(4, path, 2, 3));
m_client.receive_on_control();
}

m_client.send_on_control("debug_request", make_configuration_done_request(5));
m_client.receive_on_control();

for(int j = 0; j < 3; ++j)
{
m_client.send_on_shell("execute_request", make_execute_request(make_code()));

int seq = 6;
int nb_next = 3;

for(int i = 0; i < nb_next; ++i)
{
nl::json ev = m_client.wait_for_debug_event("stopped");
m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
nl::json json = m_client.receive_on_control();
next(seq);
}

m_client.send_on_control("debug_request", make_stacktrace_request(seq, 1));
++seq;
nl::json json = m_client.receive_on_control();

nl::json rep = m_client.receive_on_shell();
bool res = rep["content"]["status"] == "ok";
}

return true;
}

void debugger_client::shutdown()
{
m_client.send_on_control("shutdown_request", make_shutdown_request());
Expand Down Expand Up @@ -628,7 +670,7 @@ void debugger_client::dump_external_file()

std::string debugger_client::make_code() const
{
return "i=4\ni+=4\ni+=3\ni";
return "i=4\ni+=4\ni+=3\ni-=1";
}

std::string debugger_client::make_external_code() const
Expand Down Expand Up @@ -838,5 +880,18 @@ TEST(debugger, inspect_variables)
}
}

TEST(debugger, next)
{
start_kernel();
zmq::context_t context;
{
debugger_client deb(context, KERNEL_JSON, "debugger_debug_info.log");
bool res = deb.test_next();
deb.shutdown();
std::this_thread::sleep_for(2s);
EXPECT_TRUE(res);
}
}

#endif