2018-04-14 00:50:19 +02:00
/*
crashhandler . c - advanced crashhandler
Copyright ( C ) 2016 Mittorn
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
*/
2023-01-04 15:54:45 +01:00
# ifndef _GNU_SOURCE
2018-05-14 09:14:41 +02:00
# define _GNU_SOURCE
2023-01-04 15:54:45 +01:00
# endif
2018-04-14 00:50:19 +02:00
# include "common.h"
/*
= = = = = = = = = = = = = = = =
Sys_Crash
Crash handler , called from system
= = = = = = = = = = = = = = = =
*/
2023-01-09 05:55:54 +01:00
# if XASH_WIN32
2021-12-22 03:02:34 +01:00
2023-01-09 05:55:54 +01:00
# if DBGHELP
2021-12-22 03:02:34 +01:00
2018-04-14 00:50:19 +02:00
# pragma comment( lib, "dbghelp" )
2021-12-22 03:02:34 +01:00
2018-04-14 00:50:19 +02:00
# include <winnt.h>
# include <dbghelp.h>
# include <psapi.h>
2023-03-08 23:25:19 +01:00
# include <time.h>
2018-04-14 00:50:19 +02:00
# ifndef XASH_SDL
typedef ULONG_PTR DWORD_PTR , * PDWORD_PTR ;
# endif
2021-12-22 03:02:34 +01:00
static int Sys_ModuleName ( HANDLE process , char * name , void * address , int len )
2018-04-14 00:50:19 +02:00
{
DWORD_PTR baseAddress = 0 ;
static HMODULE * moduleArray ;
static unsigned int moduleCount ;
LPBYTE moduleArrayBytes ;
DWORD bytesRequired ;
int i ;
2021-12-22 03:02:34 +01:00
if ( len < 3 )
2018-04-14 00:50:19 +02:00
return 0 ;
2021-12-22 03:02:34 +01:00
if ( ! moduleArray & & EnumProcessModules ( process , NULL , 0 , & bytesRequired ) )
2018-04-14 00:50:19 +02:00
{
if ( bytesRequired )
{
moduleArrayBytes = ( LPBYTE ) LocalAlloc ( LPTR , bytesRequired ) ;
2021-12-22 03:02:34 +01:00
if ( moduleArrayBytes & & EnumProcessModules ( process , ( HMODULE * ) moduleArrayBytes , bytesRequired , & bytesRequired ) )
2018-04-14 00:50:19 +02:00
{
2021-12-22 03:02:34 +01:00
moduleCount = bytesRequired / sizeof ( HMODULE ) ;
moduleArray = ( HMODULE * ) moduleArrayBytes ;
2018-04-14 00:50:19 +02:00
}
}
}
2021-12-22 03:02:34 +01:00
for ( i = 0 ; i < moduleCount ; i + + )
2018-04-14 00:50:19 +02:00
{
MODULEINFO info ;
GetModuleInformation ( process , moduleArray [ i ] , & info , sizeof ( MODULEINFO ) ) ;
if ( ( address > info . lpBaseOfDll ) & &
( ( DWORD64 ) address < ( DWORD64 ) info . lpBaseOfDll + ( DWORD64 ) info . SizeOfImage ) )
return GetModuleBaseName ( process , moduleArray [ i ] , name , len ) ;
}
2021-12-22 03:02:34 +01:00
return Q_snprintf ( name , len , " ??? " ) ;
2018-04-14 00:50:19 +02:00
}
2021-12-22 03:02:34 +01:00
static void Sys_StackTrace ( PEXCEPTION_POINTERS pInfo )
2018-04-14 00:50:19 +02:00
{
char message [ 1024 ] ;
int len = 0 ;
size_t i ;
HANDLE process = GetCurrentProcess ( ) ;
HANDLE thread = GetCurrentThread ( ) ;
IMAGEHLP_LINE64 line ;
DWORD dline = 0 ;
DWORD options ;
CONTEXT context ;
STACKFRAME64 stackframe ;
DWORD image ;
2021-12-22 03:02:34 +01:00
memcpy ( & context , pInfo - > ContextRecord , sizeof ( CONTEXT ) ) ;
2018-04-14 00:50:19 +02:00
options = SymGetOptions ( ) ;
options | = SYMOPT_DEBUG ;
options | = SYMOPT_LOAD_LINES ;
SymSetOptions ( options ) ;
SymInitialize ( process , NULL , TRUE ) ;
2021-12-22 03:02:34 +01:00
ZeroMemory ( & stackframe , sizeof ( STACKFRAME64 ) ) ;
2018-04-14 00:50:19 +02:00
# ifdef _M_IX86
image = IMAGE_FILE_MACHINE_I386 ;
stackframe . AddrPC . Offset = context . Eip ;
stackframe . AddrPC . Mode = AddrModeFlat ;
stackframe . AddrFrame . Offset = context . Ebp ;
stackframe . AddrFrame . Mode = AddrModeFlat ;
stackframe . AddrStack . Offset = context . Esp ;
stackframe . AddrStack . Mode = AddrModeFlat ;
# elif _M_X64
image = IMAGE_FILE_MACHINE_AMD64 ;
stackframe . AddrPC . Offset = context . Rip ;
stackframe . AddrPC . Mode = AddrModeFlat ;
stackframe . AddrFrame . Offset = context . Rsp ;
stackframe . AddrFrame . Mode = AddrModeFlat ;
stackframe . AddrStack . Offset = context . Rsp ;
stackframe . AddrStack . Mode = AddrModeFlat ;
# elif _M_IA64
image = IMAGE_FILE_MACHINE_IA64 ;
stackframe . AddrPC . Offset = context . StIIP ;
stackframe . AddrPC . Mode = AddrModeFlat ;
stackframe . AddrFrame . Offset = context . IntSp ;
stackframe . AddrFrame . Mode = AddrModeFlat ;
stackframe . AddrBStore . Offset = context . RsBSP ;
stackframe . AddrBStore . Mode = AddrModeFlat ;
stackframe . AddrStack . Offset = context . IntSp ;
stackframe . AddrStack . Mode = AddrModeFlat ;
2021-12-22 03:02:34 +01:00
# elif
# error
2018-04-14 00:50:19 +02:00
# endif
2021-12-22 03:02:34 +01:00
len + = Q_snprintf ( message + len , 1024 - len , " Sys_Crash: address %p, code %p \n " ,
pInfo - > ExceptionRecord - > ExceptionAddress , ( void * ) pInfo - > ExceptionRecord - > ExceptionCode ) ;
2018-04-14 00:50:19 +02:00
if ( SymGetLineFromAddr64 ( process , ( DWORD64 ) pInfo - > ExceptionRecord - > ExceptionAddress , & dline , & line ) )
{
2021-12-22 03:02:34 +01:00
len + = Q_snprintf ( message + len , 1024 - len , " Exception: %s:%d:%d \n " ,
( char * ) line . FileName , ( int ) line . LineNumber , ( int ) dline ) ;
2018-04-14 00:50:19 +02:00
}
if ( SymGetLineFromAddr64 ( process , stackframe . AddrPC . Offset , & dline , & line ) )
{
2021-12-22 03:02:34 +01:00
len + = Q_snprintf ( message + len , 1024 - len , " PC: %s:%d:%d \n " ,
( char * ) line . FileName , ( int ) line . LineNumber , ( int ) dline ) ;
2018-04-14 00:50:19 +02:00
}
if ( SymGetLineFromAddr64 ( process , stackframe . AddrFrame . Offset , & dline , & line ) )
{
2021-12-22 03:02:34 +01:00
len + = Q_snprintf ( message + len , 1024 - len , " Frame: %s:%d:%d \n " ,
( char * ) line . FileName , ( int ) line . LineNumber , ( int ) dline ) ;
2018-04-14 00:50:19 +02:00
}
for ( i = 0 ; i < 25 ; i + + )
{
char buffer [ sizeof ( SYMBOL_INFO ) + MAX_SYM_NAME * sizeof ( TCHAR ) ] ;
PSYMBOL_INFO symbol = ( PSYMBOL_INFO ) buffer ;
BOOL result = StackWalk64 (
image , process , thread ,
& stackframe , & context , NULL ,
SymFunctionTableAccess64 , SymGetModuleBase64 , NULL ) ;
DWORD64 displacement = 0 ;
2021-12-22 03:02:34 +01:00
2018-04-14 00:50:19 +02:00
if ( ! result )
break ;
symbol - > SizeOfStruct = sizeof ( SYMBOL_INFO ) ;
symbol - > MaxNameLen = MAX_SYM_NAME ;
2021-12-22 03:02:34 +01:00
len + = Q_snprintf ( message + len , 1024 - len , " % 2d %p " ,
i , ( void * ) stackframe . AddrPC . Offset ) ;
2018-04-14 00:50:19 +02:00
if ( SymFromAddr ( process , stackframe . AddrPC . Offset , & displacement , symbol ) )
{
len + = Q_snprintf ( message + len , 1024 - len , " %s " , symbol - > Name ) ;
}
if ( SymGetLineFromAddr64 ( process , stackframe . AddrPC . Offset , & dline , & line ) )
{
2021-12-22 03:02:34 +01:00
len + = Q_snprintf ( message + len , 1024 - len , " (%s:%d:%d) " ,
( char * ) line . FileName , ( int ) line . LineNumber , ( int ) dline ) ;
2018-04-14 00:50:19 +02:00
}
len + = Q_snprintf ( message + len , 1024 - len , " ( " ) ;
2021-12-22 03:02:34 +01:00
len + = Sys_ModuleName ( process , message + len , ( void * ) stackframe . AddrPC . Offset , 1024 - len ) ;
2018-04-14 00:50:19 +02:00
len + = Q_snprintf ( message + len , 1024 - len , " ) \n " ) ;
}
2021-12-22 03:02:34 +01:00
2022-01-04 06:26:41 +01:00
# if XASH_SDL == 2
2018-04-14 00:50:19 +02:00
if ( host . type ! = HOST_DEDICATED ) // let system to restart server automaticly
2021-12-22 03:02:34 +01:00
SDL_ShowSimpleMessageBox ( SDL_MESSAGEBOX_ERROR , " Sys_Crash " , message , host . hWnd ) ;
2018-04-14 00:50:19 +02:00
# endif
2021-12-22 03:02:34 +01:00
Sys_PrintLog ( message ) ;
SymCleanup ( process ) ;
2018-04-14 00:50:19 +02:00
}
2023-03-08 23:25:19 +01:00
static void Sys_GetProcessName ( char * processName , size_t bufferSize )
{
2023-04-26 03:04:48 +02:00
char fullpath [ MAX_PATH ] ;
GetModuleBaseName ( GetCurrentProcess ( ) , NULL , fullpath , sizeof ( fullpath ) - 1 ) ;
COM_FileBase ( fullpath , processName , bufferSize ) ;
2023-03-08 23:25:19 +01:00
}
static void Sys_GetMinidumpFileName ( const char * processName , char * mdmpFileName , size_t bufferSize )
{
time_t currentUtcTime = time ( NULL ) ;
struct tm * currentLocalTime = localtime ( & currentUtcTime ) ;
Q_snprintf ( mdmpFileName , bufferSize , " %s_%s_crash_%d%.2d%.2d_%.2d%.2d%.2d.mdmp " ,
processName ,
Q_buildcommit ( ) ,
currentLocalTime - > tm_year + 1900 ,
currentLocalTime - > tm_mon + 1 ,
currentLocalTime - > tm_mday ,
currentLocalTime - > tm_hour ,
currentLocalTime - > tm_min ,
currentLocalTime - > tm_sec ) ;
}
static qboolean Sys_WriteMinidump ( PEXCEPTION_POINTERS exceptionInfo , MINIDUMP_TYPE minidumpType )
{
HRESULT errorCode ;
string processName ;
string mdmpFileName ;
MINIDUMP_EXCEPTION_INFORMATION minidumpInfo ;
Sys_GetProcessName ( processName , sizeof ( processName ) ) ;
Sys_GetMinidumpFileName ( processName , mdmpFileName , sizeof ( mdmpFileName ) ) ;
SetLastError ( NOERROR ) ;
HANDLE fileHandle = CreateFile (
mdmpFileName , GENERIC_WRITE , FILE_SHARE_WRITE ,
NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL ) ;
errorCode = HRESULT_FROM_WIN32 ( GetLastError ( ) ) ;
if ( ! SUCCEEDED ( errorCode ) ) {
CloseHandle ( fileHandle ) ;
return false ;
}
minidumpInfo . ThreadId = GetCurrentThreadId ( ) ;
minidumpInfo . ExceptionPointers = exceptionInfo ;
minidumpInfo . ClientPointers = FALSE ;
qboolean status = MiniDumpWriteDump (
GetCurrentProcess ( ) , GetCurrentProcessId ( ) , fileHandle ,
minidumpType , & minidumpInfo , NULL , NULL ) ;
CloseHandle ( fileHandle ) ;
return status ;
}
2023-01-09 05:55:54 +01:00
# endif /* DBGHELP */
2021-12-22 03:02:34 +01:00
2018-04-14 00:50:19 +02:00
LPTOP_LEVEL_EXCEPTION_FILTER oldFilter ;
2021-12-22 03:02:34 +01:00
static long _stdcall Sys_Crash ( PEXCEPTION_POINTERS pInfo )
2018-04-14 00:50:19 +02:00
{
// save config
2018-05-14 09:14:41 +02:00
if ( host . status ! = HOST_CRASHED )
2018-04-14 00:50:19 +02:00
{
// check to avoid recursive call
host . crashed = true ;
2023-01-09 05:55:54 +01:00
# if DBGHELP
2021-12-22 03:02:34 +01:00
Sys_StackTrace ( pInfo ) ;
2018-04-14 00:50:19 +02:00
# else
Sys_Warn ( " Sys_Crash: call %p at address %p " , pInfo - > ExceptionRecord - > ExceptionAddress , pInfo - > ExceptionRecord - > ExceptionCode ) ;
# endif
if ( host . type = = HOST_NORMAL )
CL_Crashed ( ) ; // tell client about crash
2018-05-14 09:14:41 +02:00
else host . status = HOST_CRASHED ;
2018-04-14 00:50:19 +02:00
2023-03-08 23:25:19 +01:00
# if DBGHELP
if ( Sys_CheckParm ( " -minidumps " ) )
{
int minidumpFlags = (
MiniDumpWithDataSegs |
MiniDumpWithCodeSegs |
MiniDumpWithHandleData |
MiniDumpWithFullMemory |
MiniDumpWithFullMemoryInfo |
MiniDumpWithIndirectlyReferencedMemory |
MiniDumpWithThreadInfo |
MiniDumpWithModuleHeaders ) ;
if ( ! Sys_WriteMinidump ( pInfo , ( MINIDUMP_TYPE ) minidumpFlags ) ) {
// fallback method, create minidump with minimal info in it
Sys_WriteMinidump ( pInfo , MiniDumpWithDataSegs ) ;
}
}
# endif
2018-10-24 19:18:06 +02:00
if ( host_developer . value < = 0 )
2018-04-14 00:50:19 +02:00
{
// no reason to call debugger in release build - just exit
Sys_Quit ( ) ;
return EXCEPTION_CONTINUE_EXECUTION ;
}
// all other states keep unchanged to let debugger find bug
Sys_DestroyConsole ( ) ;
}
if ( oldFilter )
return oldFilter ( pInfo ) ;
return EXCEPTION_CONTINUE_EXECUTION ;
}
void Sys_SetupCrashHandler ( void )
{
SetErrorMode ( SEM_FAILCRITICALERRORS ) ; // no abort/retry/fail errors
oldFilter = SetUnhandledExceptionFilter ( Sys_Crash ) ;
host . hInst = GetModuleHandle ( NULL ) ;
}
void Sys_RestoreCrashHandler ( void )
{
// restore filter
if ( oldFilter ) SetUnhandledExceptionFilter ( oldFilter ) ;
}
2023-01-09 05:55:54 +01:00
# elif XASH_FREEBSD || XASH_NETBSD || XASH_OPENBSD || XASH_ANDROID || XASH_LINUX
2018-04-14 00:50:19 +02:00
// Posix signal handler
2023-11-21 02:47:30 +01:00
# ifndef XASH_OPENBSD
2023-01-09 05:55:54 +01:00
# include <ucontext.h>
2023-11-21 02:47:30 +01:00
# endif
2023-01-09 05:55:54 +01:00
# include <signal.h>
# include <sys/mman.h>
2018-04-14 00:50:19 +02:00
# include "library.h"
2018-05-14 09:14:41 +02:00
2023-01-09 05:55:54 +01:00
# define STACK_BACKTRACE_STR "Stack backtrace:\n"
# define STACK_DUMP_STR "Stack dump:\n"
2018-04-14 00:50:19 +02:00
2023-01-09 05:55:54 +01:00
# define STACK_BACKTRACE_STR_LEN ( sizeof( STACK_BACKTRACE_STR ) - 1 )
# define STACK_DUMP_STR_LEN ( sizeof( STACK_DUMP_STR ) - 1 )
# define ALIGN( x, y ) (((uintptr_t) ( x ) + (( y ) - 1 )) & ~(( y ) - 1 ))
2021-12-22 03:02:34 +01:00
2023-01-09 05:55:54 +01:00
static struct sigaction oldFilter ;
2018-04-14 00:50:19 +02:00
2021-12-22 03:02:34 +01:00
static int Sys_PrintFrame ( char * buf , int len , int i , void * addr )
2018-04-14 00:50:19 +02:00
{
Dl_info dlinfo ;
2018-05-14 09:14:41 +02:00
if ( len < = 0 )
return 0 ; // overflow
2018-04-14 00:50:19 +02:00
if ( dladdr ( addr , & dlinfo ) )
{
if ( dlinfo . dli_sname )
2018-05-14 09:14:41 +02:00
return Q_snprintf ( buf , len , " %2d: %p <%s+%lu> (%s) \n " , i , addr , dlinfo . dli_sname ,
2021-12-22 03:02:34 +01:00
( unsigned long ) addr - ( unsigned long ) dlinfo . dli_saddr , dlinfo . dli_fname ) ; // print symbol, module and address
2018-04-14 00:50:19 +02:00
2023-01-09 05:55:54 +01:00
return Q_snprintf ( buf , len , " %2d: %p (%s) \ n " , i, addr, dlinfo.dli_fname ) ; // print module and address
}
2021-12-22 03:02:34 +01:00
2023-01-09 05:55:54 +01:00
return Q_snprintf ( buf , len , " %2d: %p \n " , i , addr ) ; // print only address
}
2018-05-14 09:14:41 +02:00
2018-04-14 00:50:19 +02:00
static void Sys_Crash ( int signal , siginfo_t * si , void * context )
{
2020-08-23 18:01:29 +02:00
void * pc = NULL , * * bp = NULL , * * sp = NULL ; // this must be set for every OS!
2018-05-14 09:14:41 +02:00
char message [ 8192 ] ;
int len , logfd , i = 0 ;
2018-04-14 00:50:19 +02:00
2019-11-24 01:52:08 +01:00
# if XASH_OPENBSD
2018-04-14 00:50:19 +02:00
struct sigcontext * ucontext = ( struct sigcontext * ) context ;
# else
ucontext_t * ucontext = ( ucontext_t * ) context ;
# endif
2018-05-14 09:14:41 +02:00
2019-11-24 01:52:08 +01:00
# if XASH_AMD64
# if XASH_FREEBSD
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . mc_rip ;
bp = ( void * * ) ucontext - > uc_mcontext . mc_rbp ;
sp = ( void * * ) ucontext - > uc_mcontext . mc_rsp ;
2019-11-24 01:52:08 +01:00
# elif XASH_NETBSD
2024-01-23 18:25:17 +01:00
pc = ( void * ) ucontext - > uc_mcontext . __gregs [ _REG_RIP ] ;
bp = ( void * * ) ucontext - > uc_mcontext . __gregs [ _REG_RBP ] ;
sp = ( void * * ) ucontext - > uc_mcontext . __gregs [ _REG_RSP ] ;
2019-11-24 01:52:08 +01:00
# elif XASH_OPENBSD
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > sc_rip ;
bp = ( void * * ) ucontext - > sc_rbp ;
sp = ( void * * ) ucontext - > sc_rsp ;
2018-04-14 00:50:19 +02:00
# else
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . gregs [ REG_RIP ] ;
bp = ( void * * ) ucontext - > uc_mcontext . gregs [ REG_RBP ] ;
sp = ( void * * ) ucontext - > uc_mcontext . gregs [ REG_RSP ] ;
2018-04-14 00:50:19 +02:00
# endif
2019-11-24 01:52:08 +01:00
# elif XASH_X86
# if XASH_FREEBSD
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . mc_eip ;
bp = ( void * * ) ucontext - > uc_mcontext . mc_ebp ;
sp = ( void * * ) ucontext - > uc_mcontext . mc_esp ;
2019-11-24 01:52:08 +01:00
# elif XASH_NETBSD
2023-08-30 17:53:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . __gregs [ _REG_EIP ] ;
bp = ( void * * ) ucontext - > uc_mcontext . __gregs [ _REG_EBP ] ;
sp = ( void * * ) ucontext - > uc_mcontext . __gregs [ _REG_ESP ] ;
2019-11-24 01:52:08 +01:00
# elif XASH_OPENBSD
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > sc_eip ;
bp = ( void * * ) ucontext - > sc_ebp ;
sp = ( void * * ) ucontext - > sc_esp ;
2018-04-14 00:50:19 +02:00
# else
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . gregs [ REG_EIP ] ;
bp = ( void * * ) ucontext - > uc_mcontext . gregs [ REG_EBP ] ;
sp = ( void * * ) ucontext - > uc_mcontext . gregs [ REG_ESP ] ;
2018-04-14 00:50:19 +02:00
# endif
2020-09-28 15:17:08 +02:00
# elif XASH_ARM && XASH_64BIT
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . pc ;
bp = ( void * ) ucontext - > uc_mcontext . regs [ 29 ] ;
sp = ( void * ) ucontext - > uc_mcontext . sp ;
2019-11-24 01:52:08 +01:00
# elif XASH_ARM
2018-05-14 09:14:41 +02:00
pc = ( void * ) ucontext - > uc_mcontext . arm_pc ;
bp = ( void * ) ucontext - > uc_mcontext . arm_fp ;
sp = ( void * ) ucontext - > uc_mcontext . arm_sp ;
2018-04-14 00:50:19 +02:00
# endif
2018-05-14 09:14:41 +02:00
// safe actions first, stack and memory may be corrupted
2022-12-04 16:08:28 +01:00
len = Q_snprintf ( message , sizeof ( message ) , " Ver: " XASH_ENGINE_NAME " " XASH_VERSION " (build %i-%s, %s-%s) \n " ,
Q_buildnum ( ) , Q_buildcommit ( ) , Q_buildos ( ) , Q_buildarch ( ) ) ;
2018-05-14 09:14:41 +02:00
2023-01-18 17:45:21 +01:00
# if !XASH_FREEBSD && !XASH_NETBSD && !XASH_OPENBSD
2018-05-14 09:14:41 +02:00
len + = Q_snprintf ( message + len , sizeof ( message ) - len , " Crash: signal %d errno %d with code %d at %p %p \n " , signal , si - > si_errno , si - > si_code , si - > si_addr , si - > si_ptr ) ;
# else
len + = Q_snprintf ( message + len , sizeof ( message ) - len , " Crash: signal %d errno %d with code %d at %p \n " , signal , si - > si_errno , si - > si_code , si - > si_addr ) ;
# endif
2021-12-22 03:02:34 +01:00
write ( STDERR_FILENO , message , len ) ;
2018-05-14 09:14:41 +02:00
// flush buffers before writing directly to descriptors
2018-04-14 00:50:19 +02:00
fflush ( stdout ) ;
fflush ( stderr ) ;
2018-05-14 09:14:41 +02:00
// now get log fd and write trace directly to log
2018-04-14 00:50:19 +02:00
logfd = Sys_LogFileNo ( ) ;
write ( logfd , message , len ) ;
2018-05-14 09:14:41 +02:00
2020-08-23 18:01:29 +02:00
if ( pc & & bp & & sp )
2018-05-14 09:14:41 +02:00
{
2020-08-23 18:01:29 +02:00
size_t pagesize = sysconf ( _SC_PAGESIZE ) ;
2023-01-09 05:55:54 +01:00
2020-08-23 18:01:29 +02:00
// try to print backtrace
2021-12-22 03:02:34 +01:00
write ( STDERR_FILENO , STACK_BACKTRACE_STR , STACK_BACKTRACE_STR_LEN ) ;
2020-08-23 18:01:29 +02:00
write ( logfd , STACK_BACKTRACE_STR , STACK_BACKTRACE_STR_LEN ) ;
2021-12-22 03:02:34 +01:00
Q_strncpy ( message + len , STACK_BACKTRACE_STR , sizeof ( message ) - len ) ;
2020-08-23 18:01:29 +02:00
len + = STACK_BACKTRACE_STR_LEN ;
// false on success, true on failure
# define try_allow_read(pointer, pagesize) \
2020-08-23 19:33:17 +02:00
( ( mprotect ( ( char * ) ALIGN ( ( pointer ) , ( pagesize ) ) , ( pagesize ) , PROT_READ | PROT_WRITE | PROT_EXEC ) = = - 1 ) & & \
2020-08-23 18:01:29 +02:00
( mprotect ( ( char * ) ALIGN ( ( pointer ) , ( pagesize ) ) , ( pagesize ) , PROT_READ | PROT_EXEC ) = = - 1 ) & & \
( mprotect ( ( char * ) ALIGN ( ( pointer ) , ( pagesize ) ) , ( pagesize ) , PROT_READ | PROT_WRITE ) = = - 1 ) & & \
2020-08-23 19:33:17 +02:00
( mprotect ( ( char * ) ALIGN ( ( pointer ) , ( pagesize ) ) , ( pagesize ) , PROT_READ ) = = - 1 ) )
2020-08-23 18:01:29 +02:00
do
2018-04-14 00:50:19 +02:00
{
2021-12-22 03:02:34 +01:00
int line = Sys_PrintFrame ( message + len , sizeof ( message ) - len , + + i , pc ) ;
write ( STDERR_FILENO , message + len , line ) ;
2018-04-14 00:50:19 +02:00
write ( logfd , message + len , line ) ;
len + = line ;
2020-08-23 18:01:29 +02:00
//if( !dladdr(bp,0) ) break; // only when bp is in module
if ( try_allow_read ( bp , pagesize ) )
break ;
if ( try_allow_read ( bp [ 0 ] , pagesize ) )
break ;
pc = bp [ 1 ] ;
bp = ( void * * ) bp [ 0 ] ;
2018-04-14 00:50:19 +02:00
}
2020-08-23 18:01:29 +02:00
while ( bp & & i < 128 ) ;
// try to print stack
2021-12-22 03:02:34 +01:00
write ( STDERR_FILENO , STACK_DUMP_STR , STACK_DUMP_STR_LEN ) ;
2020-08-23 18:01:29 +02:00
write ( logfd , STACK_DUMP_STR , STACK_DUMP_STR_LEN ) ;
2021-12-22 03:02:34 +01:00
Q_strncpy ( message + len , STACK_DUMP_STR , sizeof ( message ) - len ) ;
2020-08-23 18:01:29 +02:00
len + = STACK_DUMP_STR_LEN ;
if ( ! try_allow_read ( sp , pagesize ) )
{
for ( i = 0 ; i < 32 ; i + + )
{
2021-12-22 03:02:34 +01:00
int line = Sys_PrintFrame ( message + len , sizeof ( message ) - len , i , sp [ i ] ) ;
write ( STDERR_FILENO , message + len , line ) ;
2020-08-23 18:01:29 +02:00
write ( logfd , message + len , line ) ;
len + = line ;
}
}
# undef try_allow_read
2018-05-14 09:14:41 +02:00
}
// put MessageBox as Sys_Error
2018-04-14 00:50:19 +02:00
Msg ( " %s \n " , message ) ;
# ifdef XASH_SDL
SDL_SetWindowGrab ( host . hWnd , SDL_FALSE ) ;
# endif
2023-12-01 05:03:23 +01:00
host . crashed = true ;
2023-12-15 04:39:26 +01:00
Platform_MessageBox ( " Xash Error " , message , false ) ;
2018-04-14 00:50:19 +02:00
2018-05-14 09:14:41 +02:00
// log saved, now we can try to save configs and close log correctly, it may crash
2018-04-14 00:50:19 +02:00
if ( host . type = = HOST_NORMAL )
CL_Crashed ( ) ;
2018-05-14 09:14:41 +02:00
host . status = HOST_CRASHED ;
2023-12-01 05:03:23 +01:00
2018-04-14 00:50:19 +02:00
Sys_Quit ( ) ;
}
void Sys_SetupCrashHandler ( void )
{
2023-01-30 22:50:48 +01:00
struct sigaction act = { 0 } ;
2018-04-14 00:50:19 +02:00
act . sa_sigaction = Sys_Crash ;
act . sa_flags = SA_SIGINFO | SA_ONSTACK ;
2018-05-14 09:14:41 +02:00
sigaction ( SIGSEGV , & act , & oldFilter ) ;
sigaction ( SIGABRT , & act , & oldFilter ) ;
sigaction ( SIGBUS , & act , & oldFilter ) ;
sigaction ( SIGILL , & act , & oldFilter ) ;
2018-04-14 00:50:19 +02:00
}
void Sys_RestoreCrashHandler ( void )
{
sigaction ( SIGSEGV , & oldFilter , NULL ) ;
sigaction ( SIGABRT , & oldFilter , NULL ) ;
2018-05-14 09:14:41 +02:00
sigaction ( SIGBUS , & oldFilter , NULL ) ;
sigaction ( SIGILL , & oldFilter , NULL ) ;
2018-04-14 00:50:19 +02:00
}
2023-01-09 05:55:54 +01:00
# else
2018-04-14 00:50:19 +02:00
void Sys_SetupCrashHandler ( void )
{
// stub
}
void Sys_RestoreCrashHandler ( void )
{
// stub
}
# endif