diff --git a/include/pocketpy/debugger/dap.h b/include/pocketpy/debugger/dap.h index 8588f73f..6f70f09b 100644 --- a/include/pocketpy/debugger/dap.h +++ b/include/pocketpy/debugger/dap.h @@ -1,4 +1 @@ #pragma once -#include "pocketpy/export.h" -PK_API void wait_for_debugger(const char* hostname, unsigned short port); -PK_API void c11_dap_send_exited_event(int exitCode); \ No newline at end of file diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index ecd00b59..764bfb50 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -703,12 +703,15 @@ PK_API bool py_pickle_dumps(py_Ref val) PY_RAISE PY_RETURN; PK_API bool py_pickle_loads(const unsigned char* data, int size) PY_RAISE PY_RETURN; /************* Profiler *************/ - PK_API void py_profiler_begin(); PK_API void py_profiler_end(); PK_API void py_profiler_reset(); PK_API char* py_profiler_report(); +/************* DAP *************/ +PK_API void py_debugger_waitforattach(const char* hostname, unsigned short port); +PK_API void py_debugger_exit(int exitCode); + /************* Unchecked Functions *************/ PK_API py_ObjectRef py_tuple_data(py_Ref self); diff --git a/src/debugger/dap.c b/src/debugger/dap.c index 1a22451d..7e210e27 100644 --- a/src/debugger/dap.c +++ b/src/debugger/dap.c @@ -1,9 +1,7 @@ -#include #include #include "pocketpy/common/socket.h" #include "pocketpy/debugger/core.h" #include "pocketpy/objects/base.h" -#include "pocketpy/debugger/dap.h" #define DAP_COMMAND_LIST(X) \ X(initialize) \ @@ -235,12 +233,6 @@ void c11_dap_send_stop_event() { "{\"reason\":\"breakpoint\",\"threadId\":1,\"allThreadsStopped\":true}"); } -void c11_dap_send_exited_event(int exitCode) { - char body[64]; - snprintf(body, sizeof(body), "{\"exitCode\":%d}", exitCode); - c11_dap_send_event("exited", body); -} - void c11_dap_send_initialized_event() { c11_dap_send_event("initialized", "{}"); } int c11_dap_read_content_length(const char* buffer, int* header_length) { @@ -367,10 +359,16 @@ void c11_dap_tracefunc(py_Frame* frame, enum py_TraceEvent event) { py_sys_settrace(c11_dap_tracefunc, false); } -void wait_for_debugger(const char* hostname, unsigned short port) { +void py_debugger_waitforattach(const char* hostname, unsigned short port) { c11_dap_init_server(hostname, port); c11_debugger_init(); c11_dap_configure_debugger(); c11_socket_set_block(server.toclient, 0); py_sys_settrace(c11_dap_tracefunc, true); } + +void py_debugger_exit(int exitCode) { + char body[64]; + snprintf(body, sizeof(body), "{\"exitCode\":%d}", exitCode); + c11_dap_send_event("exited", body); +} \ No newline at end of file diff --git a/src2/main.c b/src2/main.c index 3ee066ad..6b925a43 100644 --- a/src2/main.c +++ b/src2/main.c @@ -34,6 +34,7 @@ int main(int argc, char** argv) { #endif bool profile = false; + bool debug = false; const char* filename = NULL; for(int i = 1; i < argc; i++) { @@ -41,11 +42,20 @@ int main(int argc, char** argv) { profile = true; continue; } + if(strcmp(argv[i], "--debug") == 0) { + debug = true; + continue; + } if(filename == NULL) { filename = argv[i]; continue; } - printf("Usage: pocketpy [--profile] filename\n"); + printf("Usage: pocketpy [--profile] [--debug] filename\n"); + } + + if(debug && profile) { + printf("Error: --debug and --profile cannot be used together.\n"); + return 1; } py_initialize(); @@ -53,6 +63,8 @@ int main(int argc, char** argv) { if(filename == NULL) { if(profile) printf("Warning: --profile is ignored in REPL mode.\n"); + if(debug) printf("Warning: --debug is ignored in REPL mode.\n"); + printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); #ifndef NDEBUG @@ -79,6 +91,8 @@ int main(int argc, char** argv) { } } else { if(profile) py_profiler_begin(); + if(debug) py_debugger_waitforattach("localhost", 3939); + char* source = read_file(filename); if(source) { if(!py_exec(source, filename, EXEC_MODE, NULL)) @@ -101,5 +115,7 @@ int main(int argc, char** argv) { int code = py_checkexc(false) ? 1 : 0; py_finalize(); + + if(debug) py_debugger_exit(code); return code; }