update debugger

This commit is contained in:
blueloveTH 2025-08-09 12:47:25 +08:00
parent 36321c501e
commit 527ad85902
4 changed files with 28 additions and 14 deletions

View File

@ -1,4 +1 @@
#pragma once #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);

View File

@ -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; PK_API bool py_pickle_loads(const unsigned char* data, int size) PY_RAISE PY_RETURN;
/************* Profiler *************/ /************* Profiler *************/
PK_API void py_profiler_begin(); PK_API void py_profiler_begin();
PK_API void py_profiler_end(); PK_API void py_profiler_end();
PK_API void py_profiler_reset(); PK_API void py_profiler_reset();
PK_API char* py_profiler_report(); 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 *************/ /************* Unchecked Functions *************/
PK_API py_ObjectRef py_tuple_data(py_Ref self); PK_API py_ObjectRef py_tuple_data(py_Ref self);

View File

@ -1,9 +1,7 @@
#include <ctype.h>
#include <stdbool.h> #include <stdbool.h>
#include "pocketpy/common/socket.h" #include "pocketpy/common/socket.h"
#include "pocketpy/debugger/core.h" #include "pocketpy/debugger/core.h"
#include "pocketpy/objects/base.h" #include "pocketpy/objects/base.h"
#include "pocketpy/debugger/dap.h"
#define DAP_COMMAND_LIST(X) \ #define DAP_COMMAND_LIST(X) \
X(initialize) \ X(initialize) \
@ -235,12 +233,6 @@ void c11_dap_send_stop_event() {
"{\"reason\":\"breakpoint\",\"threadId\":1,\"allThreadsStopped\":true}"); "{\"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", "{}"); } void c11_dap_send_initialized_event() { c11_dap_send_event("initialized", "{}"); }
int c11_dap_read_content_length(const char* buffer, int* header_length) { 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); 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_dap_init_server(hostname, port);
c11_debugger_init(); c11_debugger_init();
c11_dap_configure_debugger(); c11_dap_configure_debugger();
c11_socket_set_block(server.toclient, 0); c11_socket_set_block(server.toclient, 0);
py_sys_settrace(c11_dap_tracefunc, true); 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);
}

View File

@ -34,6 +34,7 @@ int main(int argc, char** argv) {
#endif #endif
bool profile = false; bool profile = false;
bool debug = false;
const char* filename = NULL; const char* filename = NULL;
for(int i = 1; i < argc; i++) { for(int i = 1; i < argc; i++) {
@ -41,11 +42,20 @@ int main(int argc, char** argv) {
profile = true; profile = true;
continue; continue;
} }
if(strcmp(argv[i], "--debug") == 0) {
debug = true;
continue;
}
if(filename == NULL) { if(filename == NULL) {
filename = argv[i]; filename = argv[i];
continue; 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(); py_initialize();
@ -53,6 +63,8 @@ int main(int argc, char** argv) {
if(filename == NULL) { if(filename == NULL) {
if(profile) printf("Warning: --profile is ignored in REPL mode.\n"); 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("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING); printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
#ifndef NDEBUG #ifndef NDEBUG
@ -79,6 +91,8 @@ int main(int argc, char** argv) {
} }
} else { } else {
if(profile) py_profiler_begin(); if(profile) py_profiler_begin();
if(debug) py_debugger_waitforattach("localhost", 3939);
char* source = read_file(filename); char* source = read_file(filename);
if(source) { if(source) {
if(!py_exec(source, filename, EXEC_MODE, NULL)) 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; int code = py_checkexc(false) ? 1 : 0;
py_finalize(); py_finalize();
if(debug) py_debugger_exit(code);
return code; return code;
} }