mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
simplify the workdir process
This commit is contained in:
parent
0a1f6dc0cd
commit
d156f1b5ad
@ -12,7 +12,6 @@ void c11_debugger_set_step_mode(enum C11_STEP_MODE mode);
|
|||||||
void c11_debugger_on_trace(py_Frame* frame, enum py_TraceEvent event);
|
void c11_debugger_on_trace(py_Frame* frame, enum py_TraceEvent event);
|
||||||
void c11_debugger_frames(c11_sbuf* buffer);
|
void c11_debugger_frames(c11_sbuf* buffer);
|
||||||
void c11_debugger_scopes(int frameid, c11_sbuf* buffer);
|
void c11_debugger_scopes(int frameid, c11_sbuf* buffer);
|
||||||
void c11_debugger_set_work_directort(const char* path);
|
|
||||||
bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer);
|
bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer);
|
||||||
int c11_debugger_setbreakpoint(const char* filename, int lineno);
|
int c11_debugger_setbreakpoint(const char* filename, int lineno);
|
||||||
int c11_debugger_reset_breakpoints_by_source(const char* sourcesname);
|
int c11_debugger_reset_breakpoints_by_source(const char* sourcesname);
|
||||||
|
@ -41,27 +41,28 @@ static struct c11_debugger {
|
|||||||
c11_vector py_frames;
|
c11_vector py_frames;
|
||||||
c11_smallmap_d2index scopes_query_cache;
|
c11_smallmap_d2index scopes_query_cache;
|
||||||
|
|
||||||
const char* work_directory;
|
#define python_vars py_r7()
|
||||||
|
|
||||||
} debugger;
|
} debugger;
|
||||||
|
|
||||||
inline static void init_structures() {
|
inline static void init_structures() {
|
||||||
c11_vector__ctor(&debugger.breakpoints, sizeof(c11_debugger_breakpoint));
|
c11_vector__ctor(&debugger.breakpoints, sizeof(c11_debugger_breakpoint));
|
||||||
c11_vector__ctor(&debugger.py_frames, sizeof(py_Frame*));
|
c11_vector__ctor(&debugger.py_frames, sizeof(py_Frame*));
|
||||||
c11_smallmap_d2index__ctor(&debugger.scopes_query_cache);
|
c11_smallmap_d2index__ctor(&debugger.scopes_query_cache);
|
||||||
py_newlist(py_r7());
|
py_newlist(python_vars);
|
||||||
py_newnil(py_list_emplace(py_r7()));
|
py_newnil(py_list_emplace(python_vars));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static void clear_structures() {
|
inline static void clear_structures() {
|
||||||
c11_vector__clear(&debugger.py_frames);
|
c11_vector__clear(&debugger.py_frames);
|
||||||
c11_smallmap_d2index__clear(&debugger.scopes_query_cache);
|
c11_smallmap_d2index__clear(&debugger.scopes_query_cache);
|
||||||
py_list_clear(py_r7());
|
py_list_clear(python_vars);
|
||||||
py_newnone(py_list_emplace(py_r7()));
|
py_newnone(py_list_emplace(python_vars));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline static py_Ref get_variable(int var_ref) {
|
inline static py_Ref get_variable(int var_ref) {
|
||||||
assert(var_ref < py_list_len(py_r7()) && var_ref > 0);
|
assert(var_ref < py_list_len(python_vars) && var_ref > 0);
|
||||||
return py_list_getitem(py_r7(), var_ref);
|
return py_list_getitem(python_vars, var_ref);
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_debugger_init() {
|
void c11_debugger_init() {
|
||||||
@ -71,7 +72,6 @@ void c11_debugger_init() {
|
|||||||
debugger.step_line = -1;
|
debugger.step_line = -1;
|
||||||
debugger.keep_suspend = false;
|
debugger.keep_suspend = false;
|
||||||
debugger.step_mode = C11_STEP_CONTINUE;
|
debugger.step_mode = C11_STEP_CONTINUE;
|
||||||
debugger.work_directory = NULL;
|
|
||||||
init_structures();
|
init_structures();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,21 +98,8 @@ void c11_debugger_set_step_mode(enum C11_STEP_MODE mode) {
|
|||||||
debugger.keep_suspend = false;
|
debugger.keep_suspend = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* c11_strip_workdir_prefix_dap(const char* full_path, const char* workdir) {
|
|
||||||
if(!full_path || !workdir) return full_path;
|
|
||||||
size_t wd_len = strlen(workdir);
|
|
||||||
const char* suffix = full_path + wd_len;
|
|
||||||
if(*suffix == '\\') {
|
|
||||||
return suffix + 1;
|
|
||||||
} else if(*suffix == '\0') {
|
|
||||||
return suffix;
|
|
||||||
} else {
|
|
||||||
return full_path;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int c11_debugger_setbreakpoint(const char* filename, int lineno) {
|
int c11_debugger_setbreakpoint(const char* filename, int lineno) {
|
||||||
filename = c11_strip_workdir_prefix_dap(filename, debugger.work_directory);
|
|
||||||
c11_debugger_breakpoint breakpoint = {.sourcename = c11_strdup(filename), .lineno = lineno};
|
c11_debugger_breakpoint breakpoint = {.sourcename = c11_strdup(filename), .lineno = lineno};
|
||||||
c11_vector__push(c11_debugger_breakpoint, &debugger.breakpoints, breakpoint);
|
c11_vector__push(c11_debugger_breakpoint, &debugger.breakpoints, breakpoint);
|
||||||
return debugger.breakpoints.length;
|
return debugger.breakpoints.length;
|
||||||
@ -169,13 +156,6 @@ int c11_debugger_should_pause() {
|
|||||||
|
|
||||||
int c11_debugger_should_keep_pause(void) { return debugger.keep_suspend; }
|
int c11_debugger_should_keep_pause(void) { return debugger.keep_suspend; }
|
||||||
|
|
||||||
void c11_debugger_set_work_directort(const char* path) {
|
|
||||||
if(path == NULL) return;
|
|
||||||
char* format_path = c11_strdup(path);
|
|
||||||
format_path[0] = (char)tolower(format_path[0]);
|
|
||||||
debugger.work_directory = format_path;
|
|
||||||
printf("[DEBUGGER INFO] set workdir as %s\n", format_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline static c11_sv sv_from_cstr(const char* str) {
|
inline static c11_sv sv_from_cstr(const char* str) {
|
||||||
c11_sv sv = {.data = str, .size = strlen(str)};
|
c11_sv sv = {.data = str, .size = strlen(str)};
|
||||||
@ -193,13 +173,6 @@ const inline static char* get_basename(const char* path) {
|
|||||||
return last_slash ? last_slash + 1 : path;
|
return last_slash ? last_slash + 1 : path;
|
||||||
}
|
}
|
||||||
|
|
||||||
const inline static char* prepend_workdir(const char* workdir, const char* path) {
|
|
||||||
static char full_path[512];
|
|
||||||
if(!workdir || !path) return path;
|
|
||||||
snprintf(full_path, sizeof(full_path), "%s\\%s", workdir, path);
|
|
||||||
return full_path;
|
|
||||||
}
|
|
||||||
|
|
||||||
void c11_debugger_frames(c11_sbuf* buffer) {
|
void c11_debugger_frames(c11_sbuf* buffer) {
|
||||||
c11_sbuf__write_cstr(buffer, "{\"stackFrames\": [");
|
c11_sbuf__write_cstr(buffer, "{\"stackFrames\": [");
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
@ -208,8 +181,7 @@ void c11_debugger_frames(c11_sbuf* buffer) {
|
|||||||
while(now_frame) {
|
while(now_frame) {
|
||||||
if(idx > 0) c11_sbuf__write_char(buffer, ',');
|
if(idx > 0) c11_sbuf__write_char(buffer, ',');
|
||||||
int line;
|
int line;
|
||||||
const char* raw_filename = py_Frame_sourceloc(now_frame, &line);
|
const char* filename = py_Frame_sourceloc(now_frame, &line);
|
||||||
const char* filename = prepend_workdir(debugger.work_directory, raw_filename);
|
|
||||||
const char* basename = get_basename(filename);
|
const char* basename = get_basename(filename);
|
||||||
const char* modname = now_frame->co->name->data;
|
const char* modname = now_frame->co->name->data;
|
||||||
pk_sprintf(
|
pk_sprintf(
|
||||||
@ -230,9 +202,9 @@ void c11_debugger_frames(c11_sbuf* buffer) {
|
|||||||
inline static c11_debugger_scope_index append_new_scope(int frameid) {
|
inline static c11_debugger_scope_index append_new_scope(int frameid) {
|
||||||
assert(frameid < debugger.py_frames.length);
|
assert(frameid < debugger.py_frames.length);
|
||||||
py_Frame* requested_frame = c11__getitem(py_Frame*, &debugger.py_frames, frameid);
|
py_Frame* requested_frame = c11__getitem(py_Frame*, &debugger.py_frames, frameid);
|
||||||
int base_index = py_list_len(py_r7());
|
int base_index = py_list_len(python_vars);
|
||||||
py_Ref new_locals = py_list_emplace(py_r7());
|
py_Ref new_locals = py_list_emplace(python_vars);
|
||||||
py_Ref new_globals = py_list_emplace(py_r7());
|
py_Ref new_globals = py_list_emplace(python_vars);
|
||||||
py_Frame_newlocals(requested_frame, new_locals);
|
py_Frame_newlocals(requested_frame, new_locals);
|
||||||
py_Frame_newglobals(requested_frame, new_globals);
|
py_Frame_newglobals(requested_frame, new_globals);
|
||||||
c11_debugger_scope_index result = {.locals_ref = base_index, .globals_ref = base_index + 1};
|
c11_debugger_scope_index result = {.locals_ref = base_index, .globals_ref = base_index + 1};
|
||||||
@ -278,7 +250,7 @@ bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer) {
|
|||||||
py_Ref kv_list = py_pushtmp();
|
py_Ref kv_list = py_pushtmp();
|
||||||
py_assign(kv_list, py_retval());
|
py_assign(kv_list, py_retval());
|
||||||
// 2. prepare base_ref
|
// 2. prepare base_ref
|
||||||
int base_index = py_list_len(py_r7());
|
int base_index = py_list_len(python_vars);
|
||||||
py_Ref base_var_ref = py_pushtmp();
|
py_Ref base_var_ref = py_pushtmp();
|
||||||
py_newint(base_var_ref, base_index);
|
py_newint(base_var_ref, base_index);
|
||||||
|
|
||||||
@ -300,8 +272,8 @@ bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer) {
|
|||||||
py_Ref dap_obj = py_pushtmp();
|
py_Ref dap_obj = py_pushtmp();
|
||||||
py_assign(dap_obj, py_retval());
|
py_assign(dap_obj, py_retval());
|
||||||
|
|
||||||
// 4. extend py_r7
|
// 4. extend python_vars
|
||||||
if(!py_smartexec("_0.extend([kv[1] for kv in _1])", NULL, py_r7(), kv_list)) {
|
if(!py_smartexec("_0.extend([kv[1] for kv in _1])", NULL, python_vars, kv_list)) {
|
||||||
py_printexc();
|
py_printexc();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -319,3 +291,4 @@ bool c11_debugger_unfold_var(int var_id, c11_sbuf* buffer) {
|
|||||||
py_pop(); // kv_list
|
py_pop(); // kv_list
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
#undef python_vars
|
||||||
|
@ -58,14 +58,7 @@ void c11_dap_handle_initialize(py_Ref arguments, c11_sbuf* buffer) {
|
|||||||
|
|
||||||
void c11_dap_handle_attach(py_Ref arguments, c11_sbuf* buffer) {
|
void c11_dap_handle_attach(py_Ref arguments, c11_sbuf* buffer) {
|
||||||
server.isatttach = true;
|
server.isatttach = true;
|
||||||
int res = py_dict_getitem_by_str(arguments, "workdir");
|
|
||||||
if(res == -1) {
|
|
||||||
py_printexc();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const char* workdir = NULL;
|
|
||||||
if(res) { workdir = py_tostr(py_retval()); }
|
|
||||||
c11_debugger_set_work_directort(workdir);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_dap_handle_next(py_Ref arguments, c11_sbuf* buffer) {
|
void c11_dap_handle_next(py_Ref arguments, c11_sbuf* buffer) {
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "pocketpy.h"
|
#include "pocketpy.h"
|
||||||
|
#include "pocketpy/debugger/dap.h"
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
@ -82,6 +82,7 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
wait_for_debugger("127.0.0.1", 3939);
|
||||||
char* source = read_file(filename);
|
char* source = read_file(filename);
|
||||||
if(source) {
|
if(source) {
|
||||||
if(!py_exec(source, filename, EXEC_MODE, NULL)) py_printexc();
|
if(!py_exec(source, filename, EXEC_MODE, NULL)) py_printexc();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user