Browse Source

improve dap

blueloveTH 13 hours ago
parent
commit
cca40f8978
4 changed files with 46 additions and 17 deletions
  1. 12 0
      include/pocketpy/debugger/dap.h
  2. 18 11
      include/pocketpy/pocketpy.h
  3. 7 6
      src/debugger/dap.c
  4. 9 0
      src/public/GlobalSetup.c

+ 12 - 0
include/pocketpy/debugger/dap.h

@@ -0,0 +1,12 @@
+#pragma once
+
+#include "pocketpy/pocketpy.h"
+
+#if PK_ENABLE_OS
+
+void dap_waitforattach(const char* hostname, unsigned short port);
+int dap_status();
+void dap_exceptionbreakpoint(py_Ref exc);
+void dap_exit(int code);
+
+#endif  // PK_ENABLE_OS

+ 18 - 11
include/pocketpy/pocketpy.h

@@ -84,6 +84,12 @@ typedef struct py_Callbacks {
 typedef struct py_AppCallbacks {
     void (*on_vm_ctor)(int index);
     void (*on_vm_dtor)(int index);
+    /// Debugger callbacks default to DAP when PK_ENABLE_OS is enabled, otherwise NULL.
+    PY_MAYBENULL void (*debugger_waitforattach)(const char* hostname, unsigned short port);
+    /// 0: detached, 1: attached and running user code, 2: attached and running debugger code.
+    PY_MAYBENULL int (*debugger_status)();
+    PY_MAYBENULL void (*debugger_exceptionbreakpoint)(py_Ref exc);
+    PY_MAYBENULL void (*debugger_exit)(int code);
 } py_AppCallbacks;
 
 /// Native function signature.
@@ -666,17 +672,18 @@ PK_API bool StopIteration() PY_RAISE;
 
 /************* Debugger *************/
 
-#if PK_ENABLE_OS
-PK_API void py_debugger_waitforattach(const char* hostname, unsigned short port);
-PK_API int py_debugger_status();
-PK_API void py_debugger_exceptionbreakpoint(py_Ref exc);
-PK_API void py_debugger_exit(int code);
-#else
-#define py_debugger_waitforattach(hostname, port)
-#define py_debugger_status() 0
-#define py_debugger_exceptionbreakpoint(exc)
-#define py_debugger_exit(code)
-#endif
+#define py_debugger_waitforattach(hostname, port) \
+    (py_appcallbacks()->debugger_waitforattach \
+         ? py_appcallbacks()->debugger_waitforattach((hostname), (port)) \
+         : (void)0)
+#define py_debugger_status() \
+    (py_appcallbacks()->debugger_status ? py_appcallbacks()->debugger_status() : 0)
+#define py_debugger_exceptionbreakpoint(exc) \
+    (py_appcallbacks()->debugger_exceptionbreakpoint \
+         ? py_appcallbacks()->debugger_exceptionbreakpoint((exc)) \
+         : (void)0)
+#define py_debugger_exit(code) \
+    (py_appcallbacks()->debugger_exit ? py_appcallbacks()->debugger_exit((code)) : (void)0)
 
 /************* PyTuple *************/
 

+ 7 - 6
src/debugger/dap.c

@@ -2,6 +2,7 @@
 #include <stdio.h>
 #include "pocketpy/common/socket.h"
 #include "pocketpy/debugger/core.h"
+#include "pocketpy/debugger/dap.h"
 #include "pocketpy/objects/base.h"
 #include "pocketpy/objects/exception.h"
 #include "pocketpy/pocketpy.h"
@@ -531,7 +532,7 @@ void c11_dap_tracefunc(py_Frame* frame, enum py_TraceEvent event) {
     py_sys_settrace(c11_dap_tracefunc, false);
 }
 
-void py_debugger_waitforattach(const char* hostname, unsigned short port) {
+void dap_waitforattach(const char* hostname, unsigned short port) {
     c11_debugger_init();
     c11_dap_init_server(hostname, port);
     while(!server.isconfiguredone) {
@@ -546,16 +547,16 @@ void py_debugger_waitforattach(const char* hostname, unsigned short port) {
     py_sys_settrace(c11_dap_tracefunc, true);
 }
 
-void py_debugger_exit(int exitCode) { c11_dap_send_exited_event(exitCode); }
+void dap_exit(int exitCode) { c11_dap_send_exited_event(exitCode); }
 
-int py_debugger_status() { 
+int dap_status() {
     if(!server.isAttached) {
         return 0;
     }
     return server.isUserCode ? 1 : 2;
- }
+}
 
-void py_debugger_exceptionbreakpoint(py_Ref exc) {
+void dap_exceptionbreakpoint(py_Ref exc) {
     assert(py_isinstance(exc, tp_BaseException));
 
     py_sys_settrace(NULL, true);
@@ -571,4 +572,4 @@ void py_debugger_exceptionbreakpoint(py_Ref exc) {
     }
 }
 
-#endif // PK_ENABLE_OS
+#endif // PK_ENABLE_OS

+ 9 - 0
src/public/GlobalSetup.c

@@ -3,6 +3,7 @@
 
 #include "pocketpy/common/utils.h"
 #include "pocketpy/common/name.h"
+#include "pocketpy/debugger/dap.h"
 #include "pocketpy/interpreter/vm.h"
 
 PK_THREAD_LOCAL VM* pk_current_vm;
@@ -22,6 +23,14 @@ void py_initialize() {
         return;
     }
 
+#if PK_ENABLE_OS
+    py_AppCallbacks* callbacks = py_appcallbacks();
+    callbacks->debugger_waitforattach = dap_waitforattach;
+    callbacks->debugger_status = dap_status;
+    callbacks->debugger_exceptionbreakpoint = dap_exceptionbreakpoint;
+    callbacks->debugger_exit = dap_exit;
+#endif
+
     pk_names_initialize();
 
     // check endianness