2021-12-05 17:54:36 +00:00
# include "Diagnostics.h"
2022-11-27 21:47:14 +00:00
# include "Game.h"
# include "dLogger.h"
2021-12-05 17:54:36 +00:00
// If we're on Win32, we'll include our minidump writer
# ifdef _WIN32
2021-12-05 23:59:50 +00:00
2021-12-05 17:54:36 +00:00
# include <Windows.h>
2021-12-05 23:59:50 +00:00
# include <Dbghelp.h>
2021-12-05 17:54:36 +00:00
# include "Game.h"
# include "dLogger.h"
void make_minidump ( EXCEPTION_POINTERS * e ) {
2022-07-28 13:39:57 +00:00
auto hDbgHelp = LoadLibraryA ( " dbghelp " ) ;
if ( hDbgHelp = = nullptr )
return ;
auto pMiniDumpWriteDump = ( decltype ( & MiniDumpWriteDump ) ) GetProcAddress ( hDbgHelp , " MiniDumpWriteDump " ) ;
if ( pMiniDumpWriteDump = = nullptr )
return ;
char name [ MAX_PATH ] ;
{
auto nameEnd = name + GetModuleFileNameA ( GetModuleHandleA ( 0 ) , name , MAX_PATH ) ;
SYSTEMTIME t ;
GetSystemTime ( & t ) ;
wsprintfA ( nameEnd - strlen ( " .exe " ) ,
" _%4d%02d%02d_%02d%02d%02d.dmp " ,
t . wYear , t . wMonth , t . wDay , t . wHour , t . wMinute , t . wSecond ) ;
}
2022-11-27 21:47:14 +00:00
Game : : logger - > Log ( " Diagnostics " , " Creating crash dump %s " , name ) ;
2022-07-28 13:39:57 +00:00
auto hFile = CreateFileA ( name , GENERIC_WRITE , FILE_SHARE_READ , 0 , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , 0 ) ;
if ( hFile = = INVALID_HANDLE_VALUE )
return ;
MINIDUMP_EXCEPTION_INFORMATION exceptionInfo ;
exceptionInfo . ThreadId = GetCurrentThreadId ( ) ;
exceptionInfo . ExceptionPointers = e ;
exceptionInfo . ClientPointers = FALSE ;
auto dumped = pMiniDumpWriteDump (
GetCurrentProcess ( ) ,
GetCurrentProcessId ( ) ,
hFile ,
MINIDUMP_TYPE ( MiniDumpWithIndirectlyReferencedMemory | MiniDumpScanMemory ) ,
e ? & exceptionInfo : nullptr ,
nullptr ,
nullptr ) ;
CloseHandle ( hFile ) ;
return ;
2021-12-05 17:54:36 +00:00
}
LONG CALLBACK unhandled_handler ( EXCEPTION_POINTERS * e ) {
2022-07-28 13:39:57 +00:00
make_minidump ( e ) ;
if ( Game : : logger )
Game : : logger - > Flush ( ) ; // Flush our log if we have one, before exiting.
return EXCEPTION_CONTINUE_SEARCH ;
2021-12-05 17:54:36 +00:00
}
# endif
# if defined(__linux__) //&& !defined(__clang__) // backtrace is a gcc exclusive system library
# include <execinfo.h>
# include <ucontext.h>
# include <unistd.h>
# include <csignal>
# include <cstdio>
# include <cstdlib>
# include <cstring>
# include <exception>
# if defined(__include_backtrace__)
# include <backtrace.h>
# include <backtrace-supported.h>
struct bt_ctx {
2022-07-28 13:39:57 +00:00
struct backtrace_state * state ;
int error ;
2021-12-05 17:54:36 +00:00
} ;
static inline void Bt ( struct backtrace_state * state ) {
2022-07-28 13:39:57 +00:00
std : : string fileName = Diagnostics : : GetOutDirectory ( ) + " crash_ " + Diagnostics : : GetProcessName ( ) + " _ " + std : : to_string ( getpid ( ) ) + " .log " ;
2022-11-27 21:47:14 +00:00
Game : : logger - > Log ( " Diagnostics " , " backtrace is enabled, crash dump located at %s " , fileName . c_str ( ) ) ;
2022-07-28 13:39:57 +00:00
FILE * file = fopen ( fileName . c_str ( ) , " w+ " ) ;
if ( file ! = nullptr ) {
backtrace_print ( state , 2 , file ) ;
fclose ( file ) ;
}
backtrace_print ( state , 2 , stdout ) ;
2021-12-05 17:54:36 +00:00
}
static void ErrorCallback ( void * data , const char * msg , int errnum ) {
2022-07-28 13:39:57 +00:00
auto * ctx = ( struct bt_ctx * ) data ;
fprintf ( stderr , " ERROR: %s (%d) " , msg , errnum ) ;
ctx - > error = 1 ;
std : : string fileName = Diagnostics : : GetOutDirectory ( ) + " crash_ " + Diagnostics : : GetProcessName ( ) + " _ " + std : : to_string ( getpid ( ) ) + " .log " ;
FILE * file = fopen ( fileName . c_str ( ) , " w+ " ) ;
if ( file ! = nullptr ) {
fprintf ( file , " ERROR: %s (%d) " , msg , errnum ) ;
fclose ( file ) ;
}
2021-12-05 17:54:36 +00:00
}
# endif
# include "Type.h"
void GenerateDump ( ) {
2022-07-28 13:39:57 +00:00
std : : string cmd = " sudo gcore " + std : : to_string ( getpid ( ) ) ;
system ( cmd . c_str ( ) ) ;
2021-12-05 17:54:36 +00:00
}
void CatchUnhandled ( int sig ) {
# ifndef __include_backtrace__
2022-11-27 21:47:14 +00:00
std : : string fileName = Diagnostics : : GetOutDirectory ( ) + " crash_ " + Diagnostics : : GetProcessName ( ) + " _ " + std : : to_string ( getpid ( ) ) + " .log " ;
Game : : logger - > Log ( " Diagnostics " , " Encountered signal %i, creating crash dump %s " , sig , fileName . c_str ( ) ) ;
2022-07-28 13:39:57 +00:00
if ( Diagnostics : : GetProduceMemoryDump ( ) ) {
GenerateDump ( ) ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
void * array [ 10 ] ;
size_t size ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
// get void*'s for all entries on the stack
size = backtrace ( array , 10 ) ;
2021-12-05 17:54:36 +00:00
# if defined(__GNUG__) and defined(__dynamic)
2022-07-28 13:39:57 +00:00
// Loop through the returned addresses, and get the symbols to be demangled
char * * strings = backtrace_symbols ( array , size ) ;
// Print the stack trace
for ( size_t i = 0 ; i < size ; i + + ) {
// Take a string like './WorldServer(_ZN19SlashCommandHandler17HandleChatCommandERKSbIDsSt11char_traitsIDsESaIDsEEP6EntityRK13SystemAddress+0x6187) [0x55869c44ecf7]' and extract the function name
std : : string functionName = strings [ i ] ;
std : : string : : size_type start = functionName . find ( ' ( ' ) ;
std : : string : : size_type end = functionName . find ( ' + ' ) ;
if ( start ! = std : : string : : npos & & end ! = std : : string : : npos ) {
std : : string demangled = functionName . substr ( start + 1 , end - start - 1 ) ;
demangled = demangle ( functionName . c_str ( ) ) ;
if ( demangled . empty ( ) ) {
2022-11-27 21:47:14 +00:00
Game : : logger - > Log ( " Diagnostics " , " [%02zu] %s " , i , demangled . c_str ( ) ) ;
2022-07-28 13:39:57 +00:00
} else {
2022-11-27 21:47:14 +00:00
Game : : logger - > Log ( " Diagnostics " , " [%02zu] %s " , i , functionName . c_str ( ) ) ;
2022-07-28 13:39:57 +00:00
}
} else {
2022-11-27 21:47:14 +00:00
Game : : logger - > Log ( " Diagnostics " , " [%02zu] %s " , i , functionName . c_str ( ) ) ;
2022-07-28 13:39:57 +00:00
}
}
2021-12-05 17:54:36 +00:00
# else
2022-07-28 13:39:57 +00:00
backtrace_symbols_fd ( array , size , STDOUT_FILENO ) ;
2021-12-05 17:54:36 +00:00
# endif
2022-07-28 13:39:57 +00:00
FILE * file = fopen ( fileName . c_str ( ) , " w+ " ) ;
if ( file ! = NULL ) {
// print out all the frames to stderr
fprintf ( file , " Error: signal %d: \n " , sig ) ;
backtrace_symbols_fd ( array , size , fileno ( file ) ) ;
fclose ( file ) ;
}
2021-12-05 17:54:36 +00:00
# else
2022-07-28 13:39:57 +00:00
struct backtrace_state * state = backtrace_create_state (
Diagnostics : : GetProcessFileName ( ) . c_str ( ) ,
BACKTRACE_SUPPORTS_THREADS ,
ErrorCallback ,
nullptr ) ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
struct bt_ctx ctx = { state , 0 } ;
Bt ( state ) ;
2021-12-05 17:54:36 +00:00
# endif
2022-07-28 13:39:57 +00:00
exit ( EXIT_FAILURE ) ;
2021-12-05 17:54:36 +00:00
}
void CritErrHdlr ( int sig_num , siginfo_t * info , void * ucontext ) {
2022-07-28 13:39:57 +00:00
CatchUnhandled ( sig_num ) ;
2021-12-05 17:54:36 +00:00
}
void OnTerminate ( ) {
2022-07-28 13:39:57 +00:00
CatchUnhandled ( - 1 ) ;
2021-12-05 17:54:36 +00:00
}
void MakeBacktrace ( ) {
2022-07-28 13:39:57 +00:00
struct sigaction sigact ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
sigact . sa_sigaction = CritErrHdlr ;
sigact . sa_flags = SA_RESTART | SA_SIGINFO ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
if ( sigaction ( SIGSEGV , & sigact , ( struct sigaction * ) nullptr ) ! = 0 | |
sigaction ( SIGFPE , & sigact , ( struct sigaction * ) nullptr ) ! = 0 | |
sigaction ( SIGABRT , & sigact , ( struct sigaction * ) nullptr ) ! = 0 | |
sigaction ( SIGILL , & sigact , ( struct sigaction * ) nullptr ) ! = 0 ) {
fprintf ( stderr , " error setting signal handler for %d (%s) \n " ,
SIGSEGV ,
strsignal ( SIGSEGV ) ) ;
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
exit ( EXIT_FAILURE ) ;
}
2021-12-05 17:54:36 +00:00
2022-07-28 13:39:57 +00:00
std : : set_terminate ( OnTerminate ) ;
2021-12-05 17:54:36 +00:00
}
# endif
void Diagnostics : : Initialize ( ) {
# ifdef _WIN32
2022-07-28 13:39:57 +00:00
SetUnhandledExceptionFilter ( unhandled_handler ) ;
2021-12-05 17:54:36 +00:00
# elif defined(__linux__) //&& !defined(__clang__)
2022-07-28 13:39:57 +00:00
MakeBacktrace ( ) ;
2021-12-05 17:54:36 +00:00
# else
2022-07-28 13:39:57 +00:00
fprintf ( stderr , " Diagnostics not supported on this platform. \n " ) ;
2021-12-05 17:54:36 +00:00
# endif
}
std : : string Diagnostics : : m_ProcessName { } ;
std : : string Diagnostics : : m_ProcessFileName { } ;
std : : string Diagnostics : : m_OutDirectory { } ;
bool Diagnostics : : m_ProduceMemoryDump { } ;
void Diagnostics : : SetProcessName ( const std : : string & name ) {
2022-07-28 13:39:57 +00:00
m_ProcessName = name ;
2021-12-05 17:54:36 +00:00
}
void Diagnostics : : SetProcessFileName ( const std : : string & name ) {
2022-07-28 13:39:57 +00:00
m_ProcessFileName = name ;
2021-12-05 17:54:36 +00:00
}
void Diagnostics : : SetOutDirectory ( const std : : string & path ) {
2022-07-28 13:39:57 +00:00
m_OutDirectory = path ;
2021-12-05 17:54:36 +00:00
}
void Diagnostics : : SetProduceMemoryDump ( bool value ) {
2022-07-28 13:39:57 +00:00
m_ProduceMemoryDump = value ;
2021-12-05 17:54:36 +00:00
}
const std : : string & Diagnostics : : GetProcessName ( ) {
2022-07-28 13:39:57 +00:00
return m_ProcessName ;
2021-12-05 17:54:36 +00:00
}
const std : : string & Diagnostics : : GetProcessFileName ( ) {
2022-07-28 13:39:57 +00:00
return m_ProcessFileName ;
2021-12-05 17:54:36 +00:00
}
const std : : string & Diagnostics : : GetOutDirectory ( ) {
2022-07-28 13:39:57 +00:00
return m_OutDirectory ;
2021-12-05 17:54:36 +00:00
}
bool Diagnostics : : GetProduceMemoryDump ( ) {
2022-07-28 13:39:57 +00:00
return m_ProduceMemoryDump ;
2021-12-05 17:54:36 +00:00
}