diff --git a/src/debugger.c b/src/debugger.c index f8dbfac..14c3b24 100644 --- a/src/debugger.c +++ b/src/debugger.c @@ -325,3 +325,12 @@ void dbg_onsleep(void) { if (dbg_impl) dbg_impl->onsleep(); } + +boolean dbg_console_vprintf(const char *format, va_list ap) { + if (!dbg_impl || !dbg_impl->console_output) + return false; + char buf[1024]; + vsnprintf(buf, sizeof(buf), format, ap); + dbg_impl->console_output(buf); + return true; +} diff --git a/src/debugger.h b/src/debugger.h index 4045cad..8cc3bb2 100644 --- a/src/debugger.h +++ b/src/debugger.h @@ -20,6 +20,7 @@ #ifndef __DEBUGGER_H__ #define __DEBUGGER_H__ +#include #include "portab.h" typedef enum { @@ -44,6 +45,7 @@ void dbg_quit(); void dbg_main(void); void dbg_onsleep(void); BYTE dbg_handle_breakpoint(int page, int addr); +boolean dbg_console_vprintf(const char *format, va_list ap); #else // ENABLE_DEBUGGER @@ -53,6 +55,7 @@ BYTE dbg_handle_breakpoint(int page, int addr); #define dbg_main() #define dbg_onsleep() #define dbg_handle_breakpoint(page, addr) BREAKPOINT +#define dbg_console_vprintf(format, ap) false #endif // ENABLE_DEBUGGER diff --git a/src/debugger_dap.c b/src/debugger_dap.c index 60eab0c..1f5a6b3 100644 --- a/src/debugger_dap.c +++ b/src/debugger_dap.c @@ -68,21 +68,21 @@ static void send_json(cJSON *json) { cJSON_free(json); } -static void emit_initialized_event() { +static void emit_initialized_event(void) { cJSON *event = cJSON_CreateObject(); cJSON_AddStringToObject(event, "type", "event"); cJSON_AddStringToObject(event, "event", "initialized"); send_json(event); } -static void emit_terminated_event() { +static void emit_terminated_event(void) { cJSON *event = cJSON_CreateObject(); cJSON_AddStringToObject(event, "type", "event"); cJSON_AddStringToObject(event, "event", "terminated"); send_json(event); } -static void emit_stop_event() { +static void emit_stop_event(void) { const char *reason; switch (dbg_state) { case DBG_STOPPED_ENTRY: reason = "entry"; break; @@ -102,6 +102,15 @@ static void emit_stop_event() { send_json(event); } +static void emit_output_event(const char *output) { + cJSON *event = cJSON_CreateObject(), *body; + cJSON_AddStringToObject(event, "type", "event"); + cJSON_AddStringToObject(event, "event", "output"); + cJSON_AddItemToObjectCS(event, "body", body = cJSON_CreateObject()); + cJSON_AddStringToObject(body, "output", output); + send_json(event); +} + static void cmd_initialize(cJSON *args, cJSON *resp) { cJSON *body; cJSON_AddBoolToObject(resp, "success", true); @@ -598,4 +607,5 @@ DebuggerImpl dbg_dap_impl = { .quit = dbg_dap_quit, .repl = dbg_dap_repl, .onsleep = dbg_dap_onsleep, + .console_output = emit_output_event, }; diff --git a/src/debugger_private.h b/src/debugger_private.h index c10c90c..3afddc7 100644 --- a/src/debugger_private.h +++ b/src/debugger_private.h @@ -52,6 +52,7 @@ typedef struct { void (*quit)(void); void (*repl)(void); void (*onsleep)(void); + void (*console_output)(const char *output); } DebuggerImpl; extern DebuggerImpl dbg_cui_impl; diff --git a/src/xsystem35.c b/src/xsystem35.c index ba40994..f313748 100644 --- a/src/xsystem35.c +++ b/src/xsystem35.c @@ -176,7 +176,8 @@ void sys_message(int lv, char *format, ...) { int prio = prio_table[min(lv, 5)]; __android_log_vprint(prio, "xsystem35", format, args); #else - vfprintf(stderr, format, args); + if (!dbg_console_vprintf(format, args)) + vfprintf(stderr, format, args); #endif va_end(args); }