Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sp/src/vscript/sqdbg/include/sqdbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#endif

struct SQDebugServer;
typedef SQDebugServer* HSQDEBUGSERVER;
typedef struct SQDebugServer *HSQDEBUGSERVER;

#ifdef __cplusplus
extern "C" {
Expand Down
49 changes: 30 additions & 19 deletions sp/src/vscript/sqdbg/sqdbg/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,29 @@

#if 0

#if defined(_WIN32) && !defined(__MINGW32__)
#define DebuggerBreak() __debugbreak()
#else
#define DebuggerBreak() asm("int3")
#endif

#ifdef _WIN32
#define __IsDebuggerPresent() IsDebuggerPresent()
#else
#define __IsDebuggerPresent() 0
#endif

#ifdef _DEBUG
#ifdef _WIN32
#if defined(_WIN32) && !defined(__MINGW32__)
#include <crtdbg.h>

bool __IsDebuggerPresent();
const char *GetModuleBaseName();

#define DebuggerBreak() do { if ( __IsDebuggerPresent() ) __debugbreak(); } while(0)

#define Assert( x ) \
do { \
__CAT( L, __LINE__ ): \
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), #x)) ) \
if ( !(x) && \
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), "%s", #x)) ) \
{ \
if ( !__IsDebuggerPresent() ) \
goto __CAT( L, __LINE__ ); \
Expand All @@ -31,7 +41,8 @@
#define AssertMsg( x, msg ) \
do { \
__CAT( L, __LINE__ ): \
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg)) ) \
if ( !(x) && \
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg)) ) \
{ \
if ( !__IsDebuggerPresent() ) \
goto __CAT( L, __LINE__ ); \
Expand All @@ -42,7 +53,8 @@
#define AssertMsg1( x, msg, a1 ) \
do { \
__CAT( L, __LINE__ ): \
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1)) ) \
if ( !(x) && \
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1)) ) \
{ \
if ( !__IsDebuggerPresent() ) \
goto __CAT( L, __LINE__ ); \
Expand All @@ -53,17 +65,19 @@
#define AssertMsg2( x, msg, a1, a2 ) \
do { \
__CAT( L, __LINE__ ): \
if ( !(x) && (1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1, a2)) ) \
if ( !(x) && \
(1 == _CrtDbgReport(_CRT_ASSERT, __FILE__, __LINE__, GetModuleBaseName(), msg, a1, a2)) ) \
{ \
if ( !__IsDebuggerPresent() ) \
goto __CAT( L, __LINE__ ); \
__debugbreak(); \
} \
} while(0)
#else
extern "C" int printf(const char *, ...);

#define DebuggerBreak() asm("int3")
#ifndef __MINGW32__
extern "C"
#endif
int printf(const char *, ...);

#define Assert( x ) \
do { \
Expand All @@ -88,8 +102,7 @@
if ( !(x) ) \
{ \
::printf("Assertion failed %s:%d: ", __FILE__, __LINE__); \
::printf(msg, a1); \
::printf("\n"); \
::printf(msg "\n", a1); \
DebuggerBreak(); \
} \
} while(0)
Expand All @@ -99,24 +112,22 @@
if ( !(x) ) \
{ \
::printf("Assertion failed %s:%d: ", __FILE__, __LINE__); \
::printf(msg, a1, a2); \
::printf("\n"); \
::printf(msg "\n", a1, a2); \
DebuggerBreak(); \
} \
} while(0)
#endif
#define Verify( x ) Assert(x)
#define STATIC_ASSERT( x ) static_assert( x, #x )
#else
#define DebuggerBreak() ((void)0)
#define Assert( x ) ((void)0)
#define AssertMsg( x, msg ) ((void)0)
#define AssertMsg1( x, msg, a1 ) ((void)0)
#define AssertMsg2( x, msg, a1, a2 ) ((void)0)
#define Verify( x ) x
#define STATIC_ASSERT( x )
#endif // _DEBUG

#define STATIC_ASSERT( x ) static_assert( x, #x )

#endif

#include <tier0/dbg.h>
Expand All @@ -126,7 +137,7 @@
// Misdefined for GCC in platform.h
#undef UNREACHABLE

#ifdef _WIN32
#ifdef _MSC_VER
#define UNREACHABLE() do { Assert(!"UNREACHABLE"); __assume(0); } while(0)
#else
#define UNREACHABLE() do { Assert(!"UNREACHABLE"); __builtin_unreachable(); } while(0)
Expand Down
91 changes: 47 additions & 44 deletions sp/src/vscript/sqdbg/sqdbg/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
#ifndef SQDBG_JSON_H
#define SQDBG_JSON_H

// Most messages are going to require less than 256 bytes,
// only approaching 1024 on large breakpoint requests
#define JSON_SCRATCH_CHUNK_SIZE 1024

typedef enum
{
JSON_NULL = 0x0000,
Expand Down Expand Up @@ -62,12 +58,12 @@ class json_array_t
{
public:
const char *m_pBase;
CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *m_Allocator;
int *m_Elements;
CScratch< true > *m_Allocator;
scratchindex_t *m_Elements;
unsigned short m_nElementCount;
unsigned short m_nElementsSize;

void Init( const char *base, CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *allocator )
void Init( const char *base, CScratch< true > *allocator )
{
m_pBase = base;
m_Allocator = allocator;
Expand All @@ -81,16 +77,16 @@ class json_array_t
{
// doesn't free old ptr, this is an uncommon operation and extra allocation is fine
int oldsize = m_nElementsSize;
int *oldptr = m_Elements;
scratchindex_t *oldptr = m_Elements;

m_nElementsSize = !m_nElementsSize ? 8 : ( m_nElementsSize << 1 );
m_Elements = (int*)m_Allocator->Alloc( m_nElementsSize * sizeof(int) );
m_Elements = (scratchindex_t*)m_Allocator->Alloc( m_nElementsSize * sizeof(*m_Elements) );

if ( oldsize )
memcpy( m_Elements, oldptr, oldsize * sizeof(int) );
memcpy( m_Elements, oldptr, oldsize * sizeof(*m_Elements) );
}

int index;
scratchindex_t index;
json_value_t *ret = (json_value_t*)m_Allocator->Alloc( sizeof(json_value_t), &index );
m_Elements[ m_nElementCount++ ] = index;
return ret;
Expand Down Expand Up @@ -138,12 +134,12 @@ class json_table_t
{
public:
const char *m_pBase;
CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *m_Allocator;
int *m_Elements;
CScratch< true > *m_Allocator;
scratchindex_t *m_Elements;
unsigned short m_nElementCount;
unsigned short m_nElementsSize;

void Init( const char *base, CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *allocator )
void Init( const char *base, CScratch< true > *allocator )
{
m_pBase = base;
m_Allocator = allocator;
Expand Down Expand Up @@ -174,16 +170,16 @@ class json_table_t
if ( m_nElementCount == m_nElementsSize )
{
int oldsize = m_nElementsSize;
int *oldptr = m_Elements;
scratchindex_t *oldptr = m_Elements;

m_nElementsSize = !m_nElementsSize ? 8 : ( m_nElementsSize << 1 );
m_Elements = (int*)m_Allocator->Alloc( m_nElementsSize * sizeof(int) );
m_Elements = (scratchindex_t*)m_Allocator->Alloc( m_nElementsSize * sizeof(*m_Elements) );

if ( oldsize )
memcpy( m_Elements, oldptr, oldsize * sizeof(int) );
memcpy( m_Elements, oldptr, oldsize * sizeof(*m_Elements) );
}

int index;
scratchindex_t index;
json_field_t *ret = (json_field_t*)m_Allocator->Alloc( sizeof(json_field_t), &index );
m_Elements[ m_nElementCount++ ] = index;
return ret;
Expand Down Expand Up @@ -275,7 +271,12 @@ static inline void PutStr( CBuffer *buffer, const string_t &str )
#ifdef SQDBG_VALIDATE_SENT_MSG
for ( unsigned int i = 0; i < str.len; i++ )
{
if ( str.ptr[i] == '\\' && ( str.ptr[i+1] == '\\' || str.ptr[i+1] == '\"' ) )
if ( str.ptr[i] == '\\' &&
( str.ptr[i+1] == '\\' ||
str.ptr[i+1] == '\"' ||
str.ptr[i+1] == 'n' ||
str.ptr[i+1] == 'r' ||
str.ptr[i+1] == 't' ) )
{
i++;
continue;
Expand Down Expand Up @@ -314,7 +315,7 @@ static inline void PutStr( CBuffer *buffer, const string_t &str, bool quote )
default:
if ( !IN_RANGE_CHAR( *c, 0x20, 0x7E ) )
{
int ret = IsValidUTF8( (unsigned char*)c, i + 1 );
int ret = IsValidUTF8( c, i + 1 );
if ( ret != 0 )
{
i -= ret - 1;
Expand Down Expand Up @@ -410,7 +411,7 @@ static inline void PutStr( CBuffer *buffer, const string_t &str, bool quote )
default:
if ( !IN_RANGE_CHAR( *c, 0x20, 0x7E ) )
{
int ret = IsValidUTF8( (unsigned char*)c, i + 1 );
int ret = IsValidUTF8( c, i + 1 );
if ( ret != 0 )
{
memcpy( mem + idx, c + 1, ret - 1 );
Expand All @@ -425,19 +426,26 @@ static inline void PutStr( CBuffer *buffer, const string_t &str, bool quote )
if ( !quote )
{
mem[idx++] = 'u';
idx += printhex< true, false >(
uint16_t val = (uint16_t)*(unsigned char*)c;
idx += printhex< false >(
mem + idx,
buffer->Capacity() - idx,
(uint16_t)*(unsigned char*)c );
val );
}
else
{
mem[idx++] = '\\';
#ifdef SQUNICODE
mem[idx++] = 'u';
uint16_t val = (uint16_t)*(unsigned char*)c;
#else
mem[idx++] = 'x';
idx += printhex< true, false >(
unsigned char val = *(unsigned char*)c;
#endif
idx += printhex< false >(
mem + idx,
buffer->Capacity() - idx,
(SQUnsignedChar)*(unsigned char*)c );
val );
}
}
}
Expand Down Expand Up @@ -504,12 +512,12 @@ static inline void PutInt( CBuffer *buffer, I val )
buffer->size += len;
}

template < bool padding, typename I >
static inline void PutHex( CBuffer *buffer, I val )
template < typename I >
static inline void PutHex( CBuffer *buffer, I val, bool padding )
{
STATIC_ASSERT( IS_UNSIGNED( I ) );
buffer->base.Ensure( buffer->Size() + countdigits<16>( val ) + 1 );
int len = printhex< padding >( buffer->Base() + buffer->Size(), buffer->Capacity() - buffer->Size(), val );
buffer->base.Ensure( buffer->Size() + ( padding ? sizeof(I) * 2 : countdigits<16>( val ) ) + 2 );
int len = printhex( buffer->Base() + buffer->Size(), buffer->Capacity() - buffer->Size(), val, -(int)padding );
buffer->size += len;
}

Expand Down Expand Up @@ -571,14 +579,7 @@ struct jstringbuf_t
template < typename I >
void PutHex( I val, bool padding = true )
{
if ( padding )
{
::PutHex< true >( m_pBuffer, val );
}
else
{
::PutHex< false >( m_pBuffer, val );
}
::PutHex( m_pBuffer, val, padding );
}
};

Expand Down Expand Up @@ -704,7 +705,7 @@ class wjson_table_t : public wjson_t
}
else
{
PutHex< false >( m_pBuffer, cast_unsigned( I, val ) );
PutHex( m_pBuffer, cast_unsigned( val ), false );
}
PutChar( m_pBuffer, ']' );
PutChar( m_pBuffer, '\"' );
Expand Down Expand Up @@ -788,7 +789,7 @@ class JSONParser
char *m_cur;
char *m_end;
char *m_start;
CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *m_Allocator;
CScratch< true > *m_Allocator;
char *m_error;

enum
Expand All @@ -805,7 +806,7 @@ class JSONParser
};

public:
JSONParser( CScratch< true, JSON_SCRATCH_CHUNK_SIZE > *allocator, char *ptr, int len, json_table_t *pTable ) :
JSONParser( CScratch< true > *allocator, char *ptr, int len, json_table_t *pTable ) :
m_cur( ptr ),
m_end( ptr + len + 1 ),
m_start( ptr ),
Expand Down Expand Up @@ -855,7 +856,7 @@ class JSONParser
else
{
buf = m_Allocator->Alloc(5);
int i = printhex< true, true, false >( buf, 5, (unsigned char)token );
int i = printhex< true, false >( buf, 5, (unsigned char)token );
Assert( i == 4 );
buf[i] = 0;
}
Expand Down Expand Up @@ -1041,10 +1042,12 @@ class JSONParser
}

#define _shift( bytesWritten, bytesRead ) \
do { \
Assert( (bytesWritten) < (bytesRead) ); \
memmove( cur + (bytesWritten), cur + (bytesRead), end - ( cur + (bytesRead) ) ); \
cur += (bytesWritten); \
end -= (bytesRead) - (bytesWritten);
end -= (bytesRead) - (bytesWritten); \
} while (0)

switch ( cur[1] )
{
Expand Down Expand Up @@ -1222,7 +1225,7 @@ class JSONParser

json_field_t *kv = pTable->NewElement();

Assert( token.ptr - m_start < (ostr_t::index_t)-1 );
Assert( (ostr_t::index_t)( token.ptr - m_start ) < (ostr_t::index_t)-1 );
kv->key.ofs = token.ptr - m_start;
kv->key.len = (ostr_t::index_t)token.len;

Expand Down Expand Up @@ -1315,7 +1318,7 @@ class JSONParser
return type;
case Token_String:
value->type = JSON_STRING;
Assert( token.ptr - m_start < (ostr_t::index_t)-1 );
Assert( (ostr_t::index_t)( token.ptr - m_start ) < (ostr_t::index_t)-1 );
value->_string.ofs = token.ptr - m_start;
value->_string.len = (ostr_t::index_t)token.len;
return type;
Expand Down
Loading
Loading