1
0
Fork 0

Added a basic stacktracing for assert and signal failures.

This commit is contained in:
Mattes D 2014-11-29 23:06:10 +01:00
parent 883230abbc
commit 201313a9f8
5 changed files with 73 additions and 4 deletions

View File

@ -264,6 +264,7 @@ template class SizeChecker<UInt16, 2>;
#include "OSSupport/Thread.h"
#include "OSSupport/File.h"
#include "Logger.h"
#include "OSSupport/StackTrace.h"
#else
// Logging functions
void inline LOGERROR(const char* a_Format, ...) FORMATSTRING(1, 2);
@ -349,14 +350,14 @@ void inline LOGD(const char* a_Format, ...)
#else
#ifdef _DEBUG
#define ASSERT( x) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__), assert(0), 0))
#define ASSERT( x) ( !!(x) || ( LOGERROR("Assertion failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), assert(0), 0))
#else
#define ASSERT(x) ((void)(x))
#endif
#endif
// Pretty much the same as ASSERT() but stays in Release builds
#define VERIFY( x) ( !!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), exit(1), 0))
#define VERIFY( x) ( !!(x) || ( LOGERROR("Verification failed: %s, file %s, line %i", #x, __FILE__, __LINE__), PrintStackTrace(), exit(1), 0))
// Same as assert but in all Self test builds
#ifdef SELF_TEST

View File

@ -16,8 +16,10 @@ SET (SRCS
Sleep.cpp
Socket.cpp
SocketThreads.cpp
StackTrace.cpp
Thread.cpp
Timer.cpp)
Timer.cpp
)
SET (HDRS
CriticalSection.h
@ -32,8 +34,10 @@ SET (HDRS
Sleep.h
Socket.h
SocketThreads.h
StackTrace.h
Thread.h
Timer.h)
Timer.h
)
if(NOT MSVC)
add_library(OSSupport ${SRCS} ${HDRS})

View File

@ -0,0 +1,43 @@
// StackTrace.cpp
// Implements the functions to print current stack traces
#include "Globals.h"
#include "StackTrace.h"
#ifdef _WIN32
#include "../StackWalker.h"
#else
#include <execinfo.h>
#endif
void PrintStackTrace(void)
{
#ifdef _WIN32
// Reuse the StackWalker from the LeakFinder project already bound to MCS
// Define a subclass of the StackWalker that outputs everything to stdout
class PrintingStackWalker :
public StackWalker
{
virtual void OnOutput(LPCSTR szText) override
{
puts(szText);
}
} sw;
sw.ShowCallstack();
#else
// Use the backtrace() function to get and output the stackTrace:
// Code adapted from http://stackoverflow.com/questions/77005/how-to-generate-a-stacktrace-when-my-gcc-c-app-crashes
void * stackTrace[30];
size_t numItems = backtrace(stackTrace, ARRAYCOUNT(stackTrace));
backtrace_symbols_fd(stackTrace, numItems, STDERR_FILENO);
#endif
}

View File

@ -0,0 +1,15 @@
// StackTrace.h
// Declares the functions to print current stack trace
/** Prints the stacktrace for the current thread. */
extern void PrintStackTrace(void);

View File

@ -10,6 +10,7 @@
#ifdef _MSC_VER
#include <dbghelp.h>
#endif // _MSC_VER
#include "OSSupport/StackTrace.h"
bool cRoot::m_TerminateEventRaised = false; // If something has told the server to stop; checked periodically in cRoot
@ -61,6 +62,7 @@ void NonCtrlHandler(int a_Signal)
std::signal(SIGSEGV, SIG_DFL);
LOGERROR(" D: | MCServer has encountered an error and needs to close");
LOGERROR("Details | SIGSEGV: Segmentation fault");
PrintStackTrace();
abort();
}
case SIGABRT:
@ -71,6 +73,7 @@ void NonCtrlHandler(int a_Signal)
std::signal(a_Signal, SIG_DFL);
LOGERROR(" D: | MCServer has encountered an error and needs to close");
LOGERROR("Details | SIGABRT: Server self-terminated due to an internal fault");
PrintStackTrace();
abort();
}
case SIGINT:
@ -137,6 +140,9 @@ LONG WINAPI LastChanceExceptionFilter(__in struct _EXCEPTION_POINTERS * a_Except
g_WriteMiniDump(GetCurrentProcess(), GetCurrentProcessId(), dumpFile, g_DumpFlags, (a_ExceptionInfo) ? &ExcInformation : nullptr, nullptr, nullptr);
CloseHandle(dumpFile);
// Print the stack trace for the basic debugging:
PrintStackTrace();
// Revert to old stack:
_asm
{