Skip to content
Merged
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
35 changes: 32 additions & 3 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ void MSG_WriteChar (sizebuf_t *sb, int c)
{
byte *buf;

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (c < -128 || c > 127)
Host_Error ("MSG_WriteChar: range error = %i not in -128..127", c);
#endif
Expand All @@ -787,7 +787,7 @@ void MSG_WriteByte (sizebuf_t *sb, int c)
{
byte *buf;

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (c < 0 || c > 255)
Host_Error ("MSG_WriteByte: range error = %i not in 0..255", c);
#endif
Expand All @@ -800,7 +800,7 @@ void MSG_WriteShort (sizebuf_t *sb, int c)
{
byte *buf;

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// it is apparently used to encode signed OR unsigned shorts...
if (c < INT16_MIN || c > UINT16_MAX)
Host_Error ("MSG_WriteShort: range error = %i not in -32768..65535", c);
Expand Down Expand Up @@ -3435,3 +3435,32 @@ int32_t COM_Rand ()

return (int32_t)(result & COM_RAND_MAX);
}

void COM_Assert_Failed (const char *expr, const char *file_path, int line)
{
// only keep the simple file name, strip the directory part
// we only want the short file name, not the full path:
char *last_sep = strrchr (file_path, '\\');

if (!last_sep)
last_sep = strrchr (file_path, '/');

const char *filename = (last_sep ? last_sep + 1 : file_path);

if (Tasks_IsWorker ())
{
Sys_DebugBreak ();

if (!Sys_IsInDebugger ())
{
char msg[4096];
q_snprintf (msg, 4096, "%s:%d Assertion: '%s' failed\n", filename, line, expr);
q_snprintf (msg + strnlen (msg, 4096), 4096, "STACK TRACE:\n");
q_snprintf (msg + strnlen (msg, 4096), 4096, "%s", Sys_StackTrace ());
PL_ErrorDialog (msg);
}
exit (1);
}
else // We are in the main thread, console is accessible, do Host_Error and we can recover.
Host_Error ("%s:%d Assertion: '%s' failed", filename, line, expr);
}
3 changes: 3 additions & 0 deletions Quake/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@ int32_t COM_Rand (void);
// Limit to 24 bits so values fit in float mantissa & don't get negative when casting to ints
#define COM_RAND_MAX 0xFFFFFF

// Utility for assert() redefinition
void COM_Assert_Failed (const char *expr, const char *file_path, int line);

//============================================================================

// QUAKEFS
Expand Down
4 changes: 2 additions & 2 deletions Quake/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -1275,8 +1275,8 @@ void LOG_Init (quakeparms_t *parms)
time_t inittime;
char session[24];

// always activate the console log in PARANOID mode
#if !defined(PARANOID)
// always activate the console log in Debug mode
#if !defined(DEBUG) && !defined(_DEBUG)
if (!COM_CheckParm ("-condebug"))
return;
#endif
Expand Down
28 changes: 17 additions & 11 deletions Quake/host.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,23 @@ void Host_Error (const char *error, ...)
Sys_Error ("Host_Error: recursively entered");
inerror = true;

Con_Printf ("================ STACK TRACE ================\n");
Con_Printf ("%s", Sys_StackTrace ());
Con_Printf ("=============================================\n");
va_start (argptr, error);
q_vsnprintf (string, sizeof (string), error, argptr);
va_end (argptr);

Sys_DebugBreak ();

if (!Sys_IsInDebugger ())
{
Con_Printf ("================ STACK TRACE ================\n");
Con_Printf ("%s", Sys_StackTrace ());
Con_Printf ("=============================================\n");
}

PR_SwitchQCVM (NULL);

SCR_EndLoadingPlaque (); // reenable screen updates

va_start (argptr, error);
q_vsnprintf (string, sizeof (string), error, argptr);
va_end (argptr);
Con_Printf ("Host_Error: %s\n", string);

if (cl.qcvm.extfuncs.CSQC_DrawHud && in_update_screen)
Expand Down Expand Up @@ -308,8 +314,8 @@ void Host_Version_f (void)
Con_Printf ("QuakeSpasm Version " QUAKESPASM_VER_STRING "\n");
Con_Printf ("vkQuake Version " ENGINE_NAME_AND_VER "\n");

#ifdef PARANOID
const char *build_str_suffix = "(PARANOID build)";
#if defined(DEBUG) || defined(_DEBUG)
const char *build_str_suffix = "(DEBUG build)";
#else
const char *build_str_suffix = "";
#endif
Expand Down Expand Up @@ -815,7 +821,7 @@ static void CL_LoadCSProgs (void)
qcvm->max_edicts = CLAMP (MIN_EDICTS, (int)max_edicts.value, MAX_EDICTS);
qcvm->edicts = (edict_t *)Mem_Alloc (qcvm->max_edicts * qcvm->edict_size);
qcvm->num_edicts = qcvm->reserved_edicts = 1;
#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// set debug fiels for all max_edicts
for (int i = 0; i < qcvm->max_edicts; i++)
{
Expand Down Expand Up @@ -1102,8 +1108,8 @@ void Host_Init (void)
NET_Init ();
SV_Init ();

#ifdef PARANOID
const char *build_str_suffix = "(PARANOID build)";
#if defined(DEBUG) || defined(_DEBUG)
const char *build_str_suffix = "(DEBUG build)";
#else
const char *build_str_suffix = "";
#endif
Expand Down
2 changes: 1 addition & 1 deletion Quake/host_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1607,7 +1607,7 @@ static void Host_Loadgame_f (void)
assert (!ent->free);

ent->baseline = nullentitystate;
#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// fill debug fields, they were overwriten above:
ent->qcvm_owner = qcvm;
ent->edict_ptr = ent;
Expand Down
12 changes: 5 additions & 7 deletions Quake/mathlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, mplane_t *p)
dist1 = p->normal[0] * (xneg ? emins : emaxs)[0] + p->normal[1] * (yneg ? emins : emaxs)[1] + p->normal[2] * (zneg ? emins : emaxs)[2];
dist2 = p->normal[0] * (xneg ? emaxs : emins)[0] + p->normal[1] * (yneg ? emaxs : emins)[1] + p->normal[2] * (zneg ? emaxs : emins)[2];

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (p->signbits & ~7)
Sys_Error ("BoxOnPlaneSide: Bad signbits");
#endif
Expand Down Expand Up @@ -226,7 +226,7 @@ int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, mplane_t *p)
if (dist2 < p->dist)
sides |= 2;

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (sides == 0)
Sys_Error ("BoxOnPlaneSide: sides==0");
#endif
Expand Down Expand Up @@ -445,14 +445,12 @@ void FloorDivMod (double numer, double denom, int *quotient, int *rem)
int q, r;
double x;

#ifndef PARANOID
if (denom <= 0.0)
Sys_Error ("FloorDivMod: bad denominator %f\n", denom);

// if ((floor(numer) != numer) || (floor(denom) != denom))
// Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
// numer, denom);
#endif
// if ((floor(numer) != numer) || (floor(denom) != denom))
// Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
// numer, denom);

if (numer >= 0.0)
{
Expand Down
2 changes: 1 addition & 1 deletion Quake/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#undef vsnprintf
#define vsnprintf q_vsnprintf

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// activate mimalloc debug and guarded pages:
#define MI_DEBUG 3
#define MI_GUARDED 1
Expand Down
16 changes: 8 additions & 8 deletions Quake/pr_edict.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ edict_t *ED_Alloc (void)

assert (!e->free);

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// fill debug fields, they were overwriten above:
e->qcvm_owner = qcvm;
e->edict_ptr = e;
Expand All @@ -109,7 +109,7 @@ ED_AddToFreeList
*/
static void ED_AddToFreeList (edict_t *ed)
{
#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (qcvm->free_list.size >= MAX_EDICTS)
Host_Error ("ED_AddToFreeList : is full (qcvm 0x%p)", qcvm);
if (qcvm->free_list.size >= qcvm->max_edicts)
Expand Down Expand Up @@ -1352,7 +1352,7 @@ qboolean ED_ParseEpair (void *base, ddef_t *key, const char *s, qboolean zoned)
// proceed to the same init as new edicts in ED_Alloc: wipe all out, then deallocate it
// right away
memset (new_edict, 0, qcvm->edict_size);
#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// fill debug fields, they were overwriten above:
new_edict->qcvm_owner = qcvm;
new_edict->edict_ptr = new_edict;
Expand Down Expand Up @@ -2067,7 +2067,7 @@ edict_t *EDICT_NUM (int n)

edict_t *found_edict = EDICT_NUM_NO_CHECK (n);

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (found_edict->edict_num != n)
Host_Error ("EDICT_NUM(%i): inconsistent number vs. edict_num=%i", n, (int)found_edict->edict_num);

Expand All @@ -2079,7 +2079,7 @@ edict_t *EDICT_NUM (int n)

int NUM_FOR_EDICT (edict_t *e)
{
#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (e->qcvm_owner != qcvm)
Host_Error ("NUM_FOR_EDICT inconsistent qcvm 0x%p, expected 0x%p", qcvm, e->qcvm_owner);

Expand All @@ -2095,15 +2095,15 @@ int NUM_FOR_EDICT (edict_t *e)
if (b < 0 || b >= qcvm->num_edicts)
Host_Error ("NUM_FOR_EDICT: bad pointer");

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
if (e->edict_num != b)
Host_Error ("NUM_FOR_EDICT: inconsistent number %i vs. e.edict_num %i", b, (int)e->edict_num);
#endif

return b;
}

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
edict_t *NEXT_EDICT (edict_t *e)
{
int current_num = NUM_FOR_EDICT (e);
Expand All @@ -2113,7 +2113,7 @@ edict_t *NEXT_EDICT (edict_t *e)
// in for loops but this is normally fine because the returned value is not used.
// here test for last element and return a NULL edict_t* if we go beyond the last element,
// and we coredump if that element get used. This NULL checks is only for
// PARANOID mode because it has a noticable performance impact on big edict-heavy levels.
// Debug builds because it has a noticable performance impact on big edict-heavy levels.
if (current_num == qcvm->num_edicts - 1)
return NULL;

Expand Down
4 changes: 2 additions & 2 deletions Quake/progs.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ typedef struct qcvm_s qcvm_t;

typedef struct edict_s
{
#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
// by construction, an edict do not move after its creation
edict_t *edict_ptr;
// edicts are allocated and owned by one qcvm:
Expand Down Expand Up @@ -138,7 +138,7 @@ void ED_LoadFromFile (const char *data);
edict_t *EDICT_NUM (int n);
int NUM_FOR_EDICT (edict_t *e);

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
edict_t *NEXT_EDICT (edict_t *e);
int EDICT_TO_PROG (edict_t *e);
edict_t *PROG_TO_EDICT (int n);
Expand Down
1 change: 0 additions & 1 deletion Quake/q_stdinc.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
#include <stdarg.h>
#include <string.h>
#include <float.h>
#include <assert.h>

/*==========================================================================*/

Expand Down
18 changes: 10 additions & 8 deletions Quake/quakedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define CONFIG_NAME "vkQuake.cfg"
#define SCREENSHOT_PREFIX "vkQuake"

// Instrumented build with extra checks
#if !defined(PARANOID)
// TODO: always valid for MSVC, what about others ?
#if defined(_DEBUG) || defined(DEBUG)
#define PARANOID
#endif
#endif

#define GAMENAME "id1" // directory to look in by default

#define PSET_SCRIPT // enable the scriptable particle system (poorly ported from FTE)
Expand Down Expand Up @@ -356,6 +348,16 @@ static inline int FindLastBitNonZero64 (const uint64_t mask)

#include "sys.h"
#include "common.h"

// Our custom assert() output stack traces either on the console with Host_Error
// if we are on the main thread, else print on stdout and abort()
// TBC : performance impact ? reserved for Debug builds ?
#ifndef assert
#define assert(e) ((e) ? (void)0 : COM_Assert_Failed (#e, __FILE__, __LINE__))
#else
#include <assert.h>
#endif

#include "mem.h"
#include "bspfile.h"
#include "mathlib.h"
Expand Down
9 changes: 9 additions & 0 deletions Quake/r_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ void R_SetupWorldCBXTexRanges (qboolean use_tasks)
texture_t *t = cl.worldmodel->textures[i];
if (!t || !t->texturechains[chain_world] || t->texturechains[chain_world]->flags & (SURF_DRAWTURB | SURF_DRAWTILED))
continue;

assert (current_cbx < NUM_WORLD_CBX);
// TODO : quick hack to shutup MSYS2 error:
// error: array subscript 6 is above array bounds of 'int[6]' [-Werror=array-bounds=]
// world_texend[current_cbx] = i + 1;
// ==> potential bug or zealous compiler triggered by assert (current_cbx < NUM_WORLD_CBX); ?
if (current_cbx >= NUM_WORLD_CBX)
break;

world_texend[current_cbx] = i + 1;
num_assigned_to_cbx += t->chain_size[chain_world];
if (num_assigned_to_cbx >= num_surfs_per_cbx)
Expand All @@ -188,6 +196,7 @@ void R_SetupWorldCBXTexRanges (qboolean use_tasks)
{
world_texstart[current_cbx] = i + 1;
}

num_assigned_to_cbx = 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Quake/sv_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3192,7 +3192,7 @@ void SV_SpawnServer (const char *server)
qcvm->max_edicts = CLAMP (MIN_EDICTS, (int)max_edicts.value, MAX_EDICTS); // johnfitz -- max_edicts cvar
qcvm->edicts = (edict_t *)Mem_Alloc (qcvm->max_edicts * qcvm->edict_size); // ericw -- sv.edicts switched to use malloc()

#ifdef PARANOID
#if defined(DEBUG) || defined(_DEBUG)
for (int j = 0; j < qcvm->max_edicts; j++)
{
// set debug fiels for all max_edicts
Expand Down
6 changes: 6 additions & 0 deletions Quake/sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ bool Sys_Pin_Current_Thread (int core_index);
// The returned string is a per-thread static buffer (no deallocation needed)
const char *Sys_StackTrace (void);

// Return true if we are running in a debugger
bool Sys_IsInDebugger (void);

// Break in a debugger if we are running in one, else do nothing
void Sys_DebugBreak (void);

#endif /* _QUAKE_SYS_H */
Loading