Skip to content

[wip] Implementing debugger in xeus-cpp(Prototype) #354

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

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 15 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
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ else()
configure_native_kernel("/share/jupyter/kernels/xcpp17/")
configure_native_kernel("/share/jupyter/kernels/xcpp20/")
configure_native_kernel("/share/jupyter/kernels/xcpp23/")
if(XEUS_CPP_USE_DEBUGGER)
configure_native_kernel("/share/jupyter/kernels/xcpp17-debugger/")
endif()
endif()

# Source files
Expand All @@ -201,6 +204,12 @@ set(XEUS_CPP_HEADERS
#src/xparser.hpp
)

if(XEUS_CPP_USE_DEBUGGER)
list(APPEND XEUS_CPP_HEADERS
include/xeus-cpp/xdebugger.hpp
)
endif()

set(XEUS_CPP_SRC
src/xholder.cpp
src/xinput.cpp
Expand All @@ -218,6 +227,14 @@ if(NOT EMSCRIPTEN)
)
endif()

if(XEUS_CPP_USE_DEBUGGER)
list(APPEND XEUS_CPP_SRC
src/xdebugger.cpp
src/xdebuglldb_client.cpp
src/xinternal_utils.cpp
)
endif()

if(EMSCRIPTEN)
list(APPEND XEUS_CPP_SRC src/xinterpreter_wasm.cpp)
endif()
Expand Down Expand Up @@ -547,3 +564,9 @@ if(EMSCRIPTEN)
"$<TARGET_FILE_DIR:xcpp>/xcpp.data"
DESTINATION ${CMAKE_INSTALL_BINDIR})
endif ()

if(XEUS_CPP_USE_DEBUGGER)
add_definitions(
-DXEUS_CPP_USE_DEBUGGER
)
endif()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a good idea. I need to know the purpose.

87 changes: 87 additions & 0 deletions include/xeus-cpp/xdebugger.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/************************************************************************************
* Copyright (c) 2023, xeus-cpp contributors *
* Copyright (c) 2023, Johan Mabille, Loic Gouarin, Sylvain Corlay, Wolf Vollprecht *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
************************************************************************************/

#ifndef XEUS_CPP_DEBUGGER_HPP
#define XEUS_CPP_DEBUGGER_HPP

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
#endif

#include <map>
#include <mutex>
#include <set>
#include <string>

#include "nlohmann/json.hpp"
#include "xeus-zmq/xdebugger_base.hpp"
#include "xeus_cpp_config.hpp"

namespace xcpp
{
class xdebuglldb_client;

class XEUS_CPP_API debugger : public xeus::xdebugger_base
{
public:

using base_type = xeus::xdebugger_base;

debugger(xeus::xcontext& context,
const xeus::xconfiguration& config,
const std::string& user_name,
const std::string& session_id,
const nl::json& debugger_config);

virtual ~debugger();

private:

nl::json inspect_variables_request(const nl::json& message);
nl::json stack_trace_request(const nl::json& message);
nl::json attach_request(const nl::json& message);
nl::json configuration_done_request(const nl::json& message);

nl::json variables_request_impl(const nl::json& message) override;

bool start_lldb();
bool start() override;
void stop() override;
xeus::xdebugger_info get_debugger_info() const override;
std::string get_cell_temporary_file(const std::string& code) const override;

bool connect_to_lldb_tcp();
std::string send_dap_message(const nl::json& message);
std::string receive_dap_response();

xdebuglldb_client* p_debuglldb_client;
std::string m_lldb_host;
std::string m_lldb_port;
std::string m_lldbdap_port;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between m_lldb_port & m_lldbdap_port?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like m_lldbdap_port is unused?

nl::json m_debugger_config;
bool m_is_running;
int m_tcp_socket;
bool m_tcp_connected;
pid_t jit_process_pid;
};

XEUS_CPP_API
std::unique_ptr<xeus::xdebugger> make_cpp_debugger(xeus::xcontext& context,
const xeus::xconfiguration& config,
const std::string& user_name,
const std::string& session_id,
const nl::json& debugger_config);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: namespace 'xcpp' not terminated with a closing comment [llvm-namespace-comment]

Suggested change
}
} // namespace xcpp
Additional context

include/xeus-cpp/xdebugger.hpp:27: namespace 'xcpp' starts here

namespace xcpp
          ^


#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#endif
4 changes: 4 additions & 0 deletions include/xeus-cpp/xinterpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace xcpp
void publish_stdout(const std::string&);
void publish_stderr(const std::string&);

static pid_t get_current_pid();

private:

void configure_impl() override;
Expand All @@ -62,6 +64,8 @@ namespace xcpp

void shutdown_request_impl() override;

nl::json internal_request_impl(const nl::json& content) override;

nl::json get_error_reply(
const std::string& ename,
const std::string& evalue,
Expand Down
20 changes: 20 additions & 0 deletions share/jupyter/kernels/xcpp17-debugger/kernel.json.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"display_name": "C++17-Debugger",
"env": {
"PATH":"@XEUS_CPP_PATH@",
"LD_LIBRARY_PATH":"@XEUS_CPP_LD_LIBRARY_PATH@"
},
"argv": [
"@XEUS_CPP_KERNELSPEC_PATH@xcpp",
"-f",
"{connection_file}",
"-gdwarf-4",
"-O0",
"-resource-dir", "@XEUS_CPP_RESOURCE_DIR@",
"-I", "@XEUS_CPP_INCLUDE_DIR@",
"-std=c++17"
],
"language": "cpp",
"metadata": {"debugger": true
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions share/jupyter/kernels/xcpp17-debugger/logo-svg.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion share/jupyter/kernels/xcpp17-omp/kernel.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"-std=c++17"@XEUS_CPP_OMP@
],
"language": "cpp",
"metadata": {"debugger": false
"metadata": {"debugger": true
}
}
2 changes: 1 addition & 1 deletion share/jupyter/kernels/xcpp17/wasm_kernel.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
],
"language": "cpp",
"metadata": {
"debugger": false,
"debugger": true,
"shared": {
"libxeus.so": "lib/libxeus.so",
"libclangCppInterOp.so": "lib/libclangCppInterOp.so"
Expand Down
71 changes: 37 additions & 34 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
#include <unistd.h>
#endif

#include "xeus/xhelper.hpp"
#include "nlohmann/json.hpp"
#include <xeus/xhelper.hpp>
#include <xeus/xkernel.hpp>
#include <xeus/xkernel_configuration.hpp>

#include "xeus-zmq/xzmq_context.hpp"
#include <xeus-zmq/xserver_zmq.hpp>

#include "xeus-cpp/xeus_cpp_config.hpp"
#include "xeus-cpp/xinterpreter.hpp"
#include "xeus-cpp/xutils.hpp"
#include "xeus-cpp/xdebugger.hpp"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: #includes are not sorted properly [llvm-include-order]

Suggested change
#include "xeus-cpp/xdebugger.hpp"
#include "xeus-cpp/xdebugger.hpp"
#include "xeus-cpp/xeus_cpp_config.hpp"
#include "xeus-cpp/xinterpreter.hpp"
#include "xeus-cpp/xutils.hpp"
#include "xeus-zmq/xzmq_context.hpp"
#include <xeus-zmq/xserver_zmq.hpp>
#include <xeus/xhelper.hpp>
#include <xeus/xkernel.hpp>
#include <xeus/xkernel_configuration.hpp>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: #includes are not sorted properly [llvm-include-order]

Suggested change
#include "xeus-cpp/xdebugger.hpp"
#include "xeus-cpp/xdebugger.hpp"
#include "xeus-cpp/xeus_cpp_config.hpp"
#include "xeus-cpp/xinterpreter.hpp"
#include "xeus-cpp/xutils.hpp"
#include "xeus-zmq/xserver_zmq_split.hpp"
#include "xeus-zmq/xzmq_context.hpp"
#include <xeus-zmq/xserver_zmq.hpp>
#include <xeus/xhelper.hpp>
#include <xeus/xkernel.hpp>
#include <xeus/xkernel_configuration.hpp>


namespace nl = nlohmann;

int main(int argc, char* argv[])
{
Expand Down Expand Up @@ -58,6 +60,13 @@ int main(int argc, char* argv[])
#endif
signal(SIGINT, xcpp::stop_handler);

#ifdef XEUS_CPP_USE_DEBUGGER
nl::json debugger_config;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "nlohmann::json" is directly included [misc-include-cleaner]

src/main.cpp:9:

- #include <string>
+ #include <nlohmann/json_fwd.hpp>
+ #include <string>

debugger_config["lldb"]["initCommands"] = {
"settings set plugin.jit-loader.gdb.enable on"
};
#endif

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not look correct. The use of ifdef. Let's discuss it in the meeting.
We need to do this based on the metadata of the kernel.json.


std::string file_name = xeus::extract_filename(argc, argv);
auto interpreter = std::make_unique<xcpp::interpreter>(argc, argv);
std::unique_ptr<xeus::xcontext> context = xeus::make_zmq_context();
Expand All @@ -77,13 +86,18 @@ int main(int argc, char* argv[])
xeus::make_console_logger(
xeus::xlogger::msg_type,
xeus::make_file_logger(xeus::xlogger::content, "xeus.log")
#ifdef XEUS_CPP_USE_DEBUGGER
),
xcpp::make_cpp_debugger,
debugger_config
#else
)
#endif
);

std::clog << "Starting xcpp kernel...\n\n"
"If you want to connect to this kernel from an other client, you can use"
" the "
+ file_name + " file."
" the " + file_name + " file."
<< std::endl;

kernel.start();
Expand All @@ -99,7 +113,13 @@ int main(int argc, char* argv[])
xeus::make_console_logger(
xeus::xlogger::msg_type,
xeus::make_file_logger(xeus::xlogger::content, "xeus.log")
#ifdef XEUS_CPP_USE_DEBUGGER
),
xcpp::make_cpp_debugger,
debugger_config
#else
)
#endif
);

std::cout << "Getting config" << std::endl;
Expand All @@ -109,37 +129,20 @@ int main(int argc, char* argv[])
" and paste the following content inside of a `kernel.json` file. And then run for example:\n\n"
"# jupyter console --existing kernel.json\n\n"
"kernel.json\n```\n{\n"
" \"transport\": \""
+ config.m_transport
+ "\",\n"
" \"ip\": \""
+ config.m_ip
+ "\",\n"
" \"control_port\": "
+ config.m_control_port
+ ",\n"
" \"shell_port\": "
+ config.m_shell_port
+ ",\n"
" \"stdin_port\": "
+ config.m_stdin_port
+ ",\n"
" \"iopub_port\": "
+ config.m_iopub_port
+ ",\n"
" \"hb_port\": "
+ config.m_hb_port
+ ",\n"
" \"signature_scheme\": \""
+ config.m_signature_scheme
+ "\",\n"
" \"key\": \""
+ config.m_key
+ "\"\n"
"}\n```\n";
" \"transport\": \"" + config.m_transport + "\",\n"
" \"ip\": \"" + config.m_ip + "\",\n"
" \"control_port\": " + config.m_control_port + ",\n"
" \"shell_port\": " + config.m_shell_port + ",\n"
" \"stdin_port\": " + config.m_stdin_port + ",\n"
" \"iopub_port\": " + config.m_iopub_port + ",\n"
" \"hb_port\": " + config.m_hb_port + ",\n"
" \"signature_scheme\": \"" + config.m_signature_scheme + "\",\n"
" \"key\": \"" + config.m_key + "\"\n"
"}\n```"
<< std::endl;

kernel.start();
}

return 0;
}
}
Loading
Loading