From 0cdd7e9e2e8bbb873fdde835681339daf6a32bcb Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Sun, 2 Mar 2025 15:55:20 -0800 Subject: [PATCH] start debug server in d_main, add `vm_debug` cvars and `-debug` CLI arg --- src/d_main.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/d_main.cpp b/src/d_main.cpp index 875f892bf9..1efe65b2ba 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -118,6 +118,7 @@ #include "screenjob.h" #include "startscreen.h" #include "shiftstate.h" +#include "common/scripting/dap/DebugServer.h" #ifdef __unix__ #include "i_system.h" // for SHARE_DIR @@ -130,6 +131,8 @@ EXTERN_CVAR(Int, vr_mode) EXTERN_CVAR(Bool, cl_customizeinvulmap) EXTERN_CVAR(Bool, log_vgafont) EXTERN_CVAR(Bool, dlg_vgafont) +EXTERN_CVAR(Bool, vm_jit) +EXTERN_CVAR(Bool, vm_jit_aot) CVAR(Int, vid_renderer, 1, 0) // for some stupid mods which threw caution out of the window... void DrawHUD(); @@ -334,6 +337,7 @@ extern bool AppActive; bool playedtitlemusic; FStartScreen* StartScreen; +std::unique_ptr debugServer; cycle_t FrameCycles; @@ -2651,6 +2655,23 @@ CUSTOM_CVAR(Int, mouse_capturemode, 1, CVAR_GLOBALCONFIG | CVAR_ARCHIVE) } } +CUSTOM_CVAR(Bool, vm_debug, false, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) +{ + if (vm_debug == false){ + if (debugServer){ + debugServer->Stop(); + debugServer = nullptr; + } + } else { + // TODO: we wouldn't need to do this if we were able to recompile everything when it's enabled? + Printf("You must restart " GAMENAME " for this change to take effect.\n"); + Printf("Note that enabling the debug server will disable JIT compilation.\n"); + } + // TODO: save this to the config file? + +} + +CVAR(Int, vm_debug_port, 19021, CVAR_ARCHIVE | CVAR_GLOBALCONFIG) void Mlook_ReleaseHandler() { @@ -3400,9 +3421,14 @@ static int D_InitGame(const FIWADInfo* iwad_info, std::vector& allw M_Init(); M_CreateGameMenus(); + bool should_debug = vm_debug.get(); + if (Args->CheckValue("-debug")) { + should_debug = true; + } // clean up the compiler symbols which are not needed any longer. - RemoveUnusedSymbols(); + if (!should_debug) + RemoveUnusedSymbols(); InitActorNumsFromMapinfo(); InitSpawnablesFromMapinfo(); @@ -3702,6 +3728,12 @@ static int D_DoomMain_Internal (void) // Now that we have the IWADINFO, initialize the autoload ini sections. GameConfig->DoAutoloadSetup(iwad_man); + bool should_debug = vm_debug.get(); + const char * debug_port_arg = Args->CheckValue("-debug"); + if (debug_port_arg) { + should_debug = true; + } + // reinit from here do @@ -3755,6 +3787,25 @@ static int D_DoomMain_Internal (void) D_DoAnonStats(); I_UpdateWindowTitle(); + + // Launch debug server if enabled + if (should_debug) { + debugServer = std::make_unique(); + int debug_port = vm_debug_port.get()->ToInt(); + if (should_debug) { + if (debug_port_arg) { + debug_port = atoi(debug_port_arg); + } + } + if (debug_port > 65535 || debug_port < 0) { + I_FatalError("Invalid debug port %d (must be between 0 and 65535)", debug_port); + } + debugServer->Listen(debug_port); + // disable vm_jit and vm_jit_aot when debugging + vm_jit = false; + vm_jit_aot = false; + } + D_DoomLoop (); // this only returns if a 'restart' CCMD is given. // // Clean up after a restart