implement exit event

This commit is contained in:
lightovernight 2025-08-09 14:30:37 +08:00
parent 4d1d2a7581
commit a539ba8978
5 changed files with 20 additions and 54 deletions

View File

@ -7,11 +7,16 @@
typedef enum { C11_STEP_IN, C11_STEP_OVER, C11_STEP_OUT, C11_STEP_CONTINUE } C11_STEP_MODE;
typedef enum { C11_DEBUGGER_SUCCESS, C11_DEBUGGER_UNKNOW_ERROR, C11_DEBUGGER_FILEPATH_ERROR } C11_DEBUGGER_FAIL_REASON;
typedef enum {
C11_DEBUGGER_SUCCESS = 0,
C11_DEBUGGER_EXIT = 1,
C11_DEBUGGER_UNKNOW_ERROR = 3,
C11_DEBUGGER_FILEPATH_ERROR = 7
} C11_DEBUGGER_STATUS;
void c11_debugger_init(void);
void c11_debugger_set_step_mode(C11_STEP_MODE mode);
C11_DEBUGGER_FAIL_REASON c11_debugger_on_trace(py_Frame* frame, enum py_TraceEvent event);
C11_DEBUGGER_STATUS c11_debugger_on_trace(py_Frame* frame, enum py_TraceEvent event);
void c11_debugger_frames(c11_sbuf* buffer);
void c11_debugger_scopes(int frameid, c11_sbuf* buffer);
bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer);

View File

@ -1 +0,0 @@
#pragma once

View File

@ -41,11 +41,7 @@ static struct c11_debugger {
c11_vector py_frames;
c11_smallmap_d2index scopes_query_cache;
<<<<<<< HEAD
#define python_vars py_r7()
=======
#define python_vars py_r7()
>>>>>>> 429f2e78 (simplify the workdir process)
} debugger;
@ -69,21 +65,13 @@ inline static py_Ref get_variable(int var_ref) {
return py_list_getitem(python_vars, var_ref);
}
static inline const char* format_filepath(const char* path) {
if (strstr(path, "..")) {
return NULL;
}
if (strstr(path + 1, "./") || strstr(path + 1, ".\\")) {
return NULL;
}
if (path[0] == '.' && (path[1] == '/' || path[1] == '\\')) {
return path + 2;
}
const inline static char* format_filepath(const char* path) {
if(strstr(path, "..")) { return NULL; }
if(strstr(path + 1, "./") || strstr(path + 1, ".\\")) { return NULL; }
if(path[0] == '.' && (path[1] == '/' || path[1] == '\\')) { return path + 2; }
return path;
}
void c11_debugger_init() {
debugger.curr_stack_depth = 0;
debugger.current_line = -1;
@ -94,7 +82,7 @@ void c11_debugger_init() {
init_structures();
}
C11_DEBUGGER_FAIL_REASON c11_debugger_on_trace(py_Frame* frame, enum py_TraceEvent event) {
C11_DEBUGGER_STATUS c11_debugger_on_trace(py_Frame* frame, enum py_TraceEvent event) {
debugger.current_frame = frame;
debugger.current_event = event;
const char* source_name = py_Frame_sourceloc(debugger.current_frame, &debugger.current_line);
@ -106,6 +94,7 @@ C11_DEBUGGER_FAIL_REASON c11_debugger_on_trace(py_Frame* frame, enum py_TraceEve
case TRACE_EVENT_POP: debugger.curr_stack_depth--; break;
default: break;
}
if(debugger.curr_stack_depth == 0) return C11_DEBUGGER_EXIT;
return C11_DEBUGGER_SUCCESS;
}
@ -123,10 +112,6 @@ void c11_debugger_set_step_mode(C11_STEP_MODE mode) {
debugger.keep_suspend = false;
}
<<<<<<< HEAD
=======
>>>>>>> 429f2e78 (simplify the workdir process)
int c11_debugger_setbreakpoint(const char* filename, int lineno) {
c11_debugger_breakpoint breakpoint = {.sourcename = c11_strdup(filename), .lineno = lineno};
c11_vector__push(c11_debugger_breakpoint, &debugger.breakpoints, breakpoint);
@ -184,10 +169,6 @@ int c11_debugger_should_pause() {
int c11_debugger_should_keep_pause(void) { return debugger.keep_suspend; }
<<<<<<< HEAD
=======
>>>>>>> 429f2e78 (simplify the workdir process)
inline static c11_sv sv_from_cstr(const char* str) {
c11_sv sv = {.data = str, .size = strlen(str)};
return sv;
@ -287,12 +268,6 @@ bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer) {
// 3. construct DAP JSON and extend python_vars
py_Ref dap_obj = py_pushtmp();
<<<<<<< HEAD
py_assign(dap_obj, py_retval());
// 4. extend python_vars
if(!py_smartexec("_0.extend([kv[1] for kv in _1])", NULL, python_vars, kv_list)) {
=======
py_newdict(dap_obj); // 先创建字典
const char* dap_code =
"_2['variables'] = []\n"
@ -307,7 +282,6 @@ bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer) {
" })\n"
" if has_children: var_ref += 1\n";
if(!py_smartexec(dap_code, NULL, kv_list, base_var_ref, dap_obj)) {
>>>>>>> 429f2e78 (simplify the workdir process)
py_printexc();
return false;
}
@ -337,8 +311,5 @@ bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer) {
py_pop(); // kv_list
return true;
}
<<<<<<< HEAD
=======
>>>>>>> 429f2e78 (simplify the workdir process)
#undef python_vars

View File

@ -1,4 +1,5 @@
#include <stdbool.h>
#include <stdio.h>
#include "pocketpy/common/socket.h"
#include "pocketpy/debugger/core.h"
#include "pocketpy/objects/base.h"
@ -54,14 +55,7 @@ void c11_dap_handle_initialize(py_Ref arguments, c11_sbuf* buffer) {
c11_sbuf__write_char(buffer, ',');
}
<<<<<<< HEAD
void c11_dap_handle_attach(py_Ref arguments, c11_sbuf* buffer) {
server.isatttach = true;
}
=======
void c11_dap_handle_attach(py_Ref arguments, c11_sbuf* buffer) { server.isatttach = true; }
>>>>>>> 429f2e78 (simplify the workdir process)
void c11_dap_handle_next(py_Ref arguments, c11_sbuf* buffer) {
c11_debugger_set_step_mode(C11_STEP_OVER);
@ -237,8 +231,6 @@ void c11_dap_send_stop_event() {
"{\"threadId\":1,\"allThreadsStopped\":true}");
}
<<<<<<< HEAD
=======
void c11_dap_send_exited_event(int exitCode) {
char body[64];
snprintf(body, sizeof(body), "{\"exitCode\":%d}", exitCode);
@ -251,7 +243,6 @@ void c11_dap_send_fatal_event(const char* message) {
c11_dap_send_event("pkpy/fatalError", body);
}
>>>>>>> 429f2e78 (simplify the workdir process)
void c11_dap_send_initialized_event() { c11_dap_send_event("initialized", "{}"); }
int c11_dap_read_content_length(const char* buffer, int* header_length) {
@ -365,7 +356,12 @@ void c11_dap_configure_debugger() {
void c11_dap_tracefunc(py_Frame* frame, enum py_TraceEvent event) {
py_sys_settrace(NULL, false);
C11_DEBUGGER_FAIL_REASON result = c11_debugger_on_trace(frame, event);
C11_DEBUGGER_STATUS result = c11_debugger_on_trace(frame, event);
if(result == C11_DEBUGGER_EXIT) {
c11_dap_send_exited_event(0);
printf("[DEBUGGER INFO] : program exit\n");
exit(0);
}
if(result != C11_DEBUGGER_SUCCESS) {
const char* message = NULL;
switch(result) {

View File

@ -4,7 +4,6 @@
#include <string.h>
#include "pocketpy.h"
#include "pocketpy/debugger/dap.h"
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
@ -90,13 +89,9 @@ int main(int argc, char** argv) {
}
}
} else {
<<<<<<< HEAD
if(profile) py_profiler_begin();
if(debug) py_debugger_waitforattach("127.0.0.1", 6110);
=======
wait_for_debugger("127.0.0.1", 3939);
>>>>>>> 429f2e78 (simplify the workdir process)
char* source = read_file(filename);
if(source) {
if(!py_exec(source, filename, EXEC_MODE, NULL))