Skip to content

Commit 94c38d9

Browse files
Add Squirrel debugger
1 parent 82bc30c commit 94c38d9

File tree

15 files changed

+23884
-12
lines changed

15 files changed

+23884
-12
lines changed

sp/src/game/client/vscript_client.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
extern IScriptManager *scriptmanager;
2929
extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * );
3030

31+
#ifdef MAPBASE_VSCRIPT
32+
extern int vscript_debugger_port;
33+
#endif
34+
3135
// #define VMPROFILE 1
3236

3337
#ifdef VMPROFILE
@@ -682,6 +686,14 @@ bool VScriptClientInit()
682686
//g_pScriptVM->RegisterInstance( &g_ScriptEntityIterator, "Entities" );
683687
#endif
684688

689+
#ifdef MAPBASE_VSCRIPT
690+
if ( vscript_debugger_port )
691+
{
692+
g_pScriptVM->ConnectDebugger( vscript_debugger_port );
693+
vscript_debugger_port = 0;
694+
}
695+
#endif
696+
685697
if (scriptLanguage == SL_SQUIRREL)
686698
{
687699
g_pScriptVM->Run( g_Script_vscript_client );
@@ -768,11 +780,19 @@ class CVScriptGameSystem : public CAutoGameSystemPerFrame
768780
VScriptClientTerm();
769781
}
770782

771-
virtual void FrameUpdatePostEntityThink()
783+
#ifdef MAPBASE_VSCRIPT
784+
virtual void Update( float frametime )
785+
{
786+
if ( g_pScriptVM )
787+
g_pScriptVM->Frame( frametime );
788+
}
789+
#else
790+
virtual void FrameUpdatePostEntityThink()
772791
{
773792
if ( g_pScriptVM )
774793
g_pScriptVM->Frame( gpGlobals->frametime );
775794
}
795+
#endif
776796

777797
bool m_bAllowEntityCreationInScripts;
778798
};

sp/src/game/server/vscript_server.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * );
2424

25+
#ifdef MAPBASE_VSCRIPT
26+
extern int vscript_debugger_port;
27+
#endif
28+
2529
// #define VMPROFILE 1
2630

2731
#ifdef VMPROFILE
@@ -663,6 +667,14 @@ bool VScriptServerInit()
663667
RegisterSharedScriptFunctions();
664668
#endif
665669

670+
#ifdef MAPBASE_VSCRIPT
671+
if ( vscript_debugger_port )
672+
{
673+
g_pScriptVM->ConnectDebugger( vscript_debugger_port );
674+
vscript_debugger_port = 0;
675+
}
676+
#endif
677+
666678
if (scriptLanguage == SL_SQUIRREL)
667679
{
668680
g_pScriptVM->Run( g_Script_vscript_server );

sp/src/game/shared/vscript_shared.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ extern ScriptClassDesc_t * GetScriptDesc( CBaseEntity * );
3737
#ifdef MAPBASE_VSCRIPT
3838
// This is to ensure a dependency exists between the vscript library and the game DLLs
3939
extern int vscript_token;
40+
extern int vscript_debugger_port;
4041
int vscript_token_hack = vscript_token;
4142
#endif
4243

@@ -390,12 +391,30 @@ CON_COMMAND_F( script_debug, "Connect the vscript VM to the script debugger", FC
390391
if ( !IsCommandIssuedByServerAdmin() )
391392
return;
392393

394+
#ifdef MAPBASE_VSCRIPT
395+
#ifdef GAME_DLL
396+
int port = 1212;
397+
#else
398+
int port = 1213;
399+
#endif
400+
#endif
401+
393402
if ( !g_pScriptVM )
394403
{
404+
#ifdef MAPBASE_VSCRIPT
405+
vscript_debugger_port = port;
406+
CGMsg( 0, CON_GROUP_VSCRIPT, "VScript VM is not running, waiting for it to attach the debugger to port %d...\n", port );
407+
#else
395408
CGWarning( 0, CON_GROUP_VSCRIPT, "Scripting disabled or no server running\n" );
409+
#endif
396410
return;
397411
}
412+
413+
#ifdef MAPBASE_VSCRIPT
414+
g_pScriptVM->ConnectDebugger( port );
415+
#else
398416
g_pScriptVM->ConnectDebugger();
417+
#endif
399418
}
400419

401420
#ifdef CLIENT_DLL

sp/src/public/vscript/ivscript.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,11 @@ class IScriptVM
838838
virtual bool Init() = 0;
839839
virtual void Shutdown() = 0;
840840

841+
#ifdef MAPBASE_VSCRIPT
842+
virtual bool ConnectDebugger( int port = 0 ) = 0;
843+
#else
841844
virtual bool ConnectDebugger() = 0;
845+
#endif
842846
virtual void DisconnectDebugger() = 0;
843847

844848
virtual ScriptLanguage_t GetLanguage() = 0;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//-----------------------------------------------------------------------
2+
// github.com/samisalreadytaken/sqdbg
3+
//-----------------------------------------------------------------------
4+
//
5+
// Squirrel Debugger
6+
//
7+
8+
#ifndef SQDBG_H
9+
#define SQDBG_H
10+
11+
#include <squirrel.h>
12+
13+
#define SQDBG_SV_API_VER 1
14+
15+
#ifndef SQDBG_API
16+
#ifdef SQDBG_DLL
17+
#if defined(_WIN32)
18+
#ifdef SQDBG_DLL_EXPORT
19+
#define SQDBG_API __declspec(dllexport)
20+
#else
21+
#define SQDBG_API __declspec(dllimport)
22+
#endif
23+
#elif defined(__GNUC__)
24+
#ifdef SQDBG_DLL_EXPORT
25+
#define SQDBG_API __attribute__((visibility("default")))
26+
#else
27+
#define SQDBG_API extern
28+
#endif
29+
#else
30+
#define SQDBG_API extern
31+
#endif
32+
#else
33+
#define SQDBG_API extern
34+
#endif
35+
#endif
36+
37+
struct SQDebugServer;
38+
typedef SQDebugServer* HSQDEBUGSERVER;
39+
40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
44+
// Create and attach a new debugger
45+
// Memory is owned by the VM, it is freed when the VM dies or
46+
// the debugger is disconnected via sqdbg_destroy_debugger()
47+
SQDBG_API HSQDEBUGSERVER sqdbg_attach_debugger( HSQUIRRELVM vm );
48+
49+
// Detach and destroy the debugger attached to this VM
50+
// Invalidates the handle returned from sqdbg_attach_debugger()
51+
SQDBG_API void sqdbg_destroy_debugger( HSQUIRRELVM vm );
52+
53+
// Open specified port and allow client connections
54+
// If port is 0, the system will choose a unique available port
55+
// Returns 0 on success
56+
SQDBG_API int sqdbg_listen_socket( HSQDEBUGSERVER dbg, unsigned short port );
57+
58+
// Process client connections and incoming messages
59+
// Blocks on script breakpoints while a client is connected
60+
SQDBG_API void sqdbg_frame( HSQDEBUGSERVER dbg );
61+
62+
// Copies the script to be able to source it to debugger clients
63+
SQDBG_API void sqdbg_on_script_compile( HSQDEBUGSERVER dbg, const SQChar *script, SQInteger size,
64+
const SQChar *sourcename, SQInteger sourcenamelen );
65+
66+
#ifdef __cplusplus
67+
}
68+
#endif
69+
70+
#endif // SQDBG_H

sp/src/vscript/sqdbg/sqdbg/debug.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//-----------------------------------------------------------------------
2+
// github.com/samisalreadytaken/sqdbg
3+
//-----------------------------------------------------------------------
4+
//
5+
6+
#ifndef SQDBG_DEBUG_H
7+
#define SQDBG_DEBUG_H
8+
9+
#if 0
10+
11+
#ifdef _DEBUG
12+
#ifdef _WIN32
13+
#include <crtdbg.h>
14+
15+
bool __IsDebuggerPresent();
16+
const char *GetModuleBaseName();
17+
18+
#define DebuggerBreak() do { if ( __IsDebuggerPresent() ) __debugbreak(); } while(0)
19+
20+
#define Assert( x ) \
21+
do { \
22+
__CAT( L, __LINE__ ): \
23+
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), #x)) ) \
24+
{ \
25+
if ( !__IsDebuggerPresent() ) \
26+
goto __CAT( L, __LINE__ ); \
27+
__debugbreak(); \
28+
} \
29+
} while(0)
30+
31+
#define AssertMsg( x, msg ) \
32+
do { \
33+
__CAT( L, __LINE__ ): \
34+
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg)) ) \
35+
{ \
36+
if ( !__IsDebuggerPresent() ) \
37+
goto __CAT( L, __LINE__ ); \
38+
__debugbreak(); \
39+
} \
40+
} while(0)
41+
42+
#define AssertMsg1( x, msg, a1 ) \
43+
do { \
44+
__CAT( L, __LINE__ ): \
45+
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1)) ) \
46+
{ \
47+
if ( !__IsDebuggerPresent() ) \
48+
goto __CAT( L, __LINE__ ); \
49+
__debugbreak(); \
50+
} \
51+
} while(0)
52+
53+
#define AssertMsg2( x, msg, a1, a2 ) \
54+
do { \
55+
__CAT( L, __LINE__ ): \
56+
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1, a2)) ) \
57+
{ \
58+
if ( !__IsDebuggerPresent() ) \
59+
goto __CAT( L, __LINE__ ); \
60+
__debugbreak(); \
61+
} \
62+
} while(0)
63+
#else
64+
extern "C" int printf(const char *, ...);
65+
66+
#define DebuggerBreak() asm("int3")
67+
68+
#define Assert( x ) \
69+
do { \
70+
if ( !(x) ) \
71+
{ \
72+
::printf("Assertion failed %s:%d: %s\n", __FILE__, __LINE__, #x); \
73+
DebuggerBreak(); \
74+
} \
75+
} while(0)
76+
77+
#define AssertMsg( x, msg ) \
78+
do { \
79+
if ( !(x) ) \
80+
{ \
81+
::printf("Assertion failed %s:%d: %s\n", __FILE__, __LINE__, msg); \
82+
DebuggerBreak(); \
83+
} \
84+
} while(0)
85+
86+
#define AssertMsg1( x, msg, a1 ) \
87+
do { \
88+
if ( !(x) ) \
89+
{ \
90+
::printf("Assertion failed %s:%d: ", __FILE__, __LINE__); \
91+
::printf(msg, a1); \
92+
::printf("\n"); \
93+
DebuggerBreak(); \
94+
} \
95+
} while(0)
96+
97+
#define AssertMsg2( x, msg, a1, a2 ) \
98+
do { \
99+
if ( !(x) ) \
100+
{ \
101+
::printf("Assertion failed %s:%d: ", __FILE__, __LINE__); \
102+
::printf(msg, a1, a2); \
103+
::printf("\n"); \
104+
DebuggerBreak(); \
105+
} \
106+
} while(0)
107+
#endif
108+
#define Verify( x ) Assert(x)
109+
#else
110+
#define DebuggerBreak() ((void)0)
111+
#define Assert( x ) ((void)0)
112+
#define AssertMsg( x, msg ) ((void)0)
113+
#define AssertMsg1( x, msg, a1 ) ((void)0)
114+
#define AssertMsg2( x, msg, a1, a2 ) ((void)0)
115+
#define Verify( x ) x
116+
#endif // _DEBUG
117+
118+
#endif
119+
120+
#include <tier0/dbg.h>
121+
122+
// Misdefined for GCC in platform.h
123+
#undef UNREACHABLE
124+
125+
#ifdef _WIN32
126+
#define UNREACHABLE() do { Assert(!"UNREACHABLE"); __assume(0); } while(0)
127+
#else
128+
#define UNREACHABLE() do { Assert(!"UNREACHABLE"); __builtin_unreachable(); } while(0)
129+
#endif
130+
131+
#endif // SQDBG_DEBUG_H

0 commit comments

Comments
 (0)