From 201313a9f84192ad7f2fcd7e4ab2cc793a85b96f Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sat, 29 Nov 2014 23:06:10 +0100 Subject: Added a basic stacktracing for assert and signal failures. --- src/OSSupport/CMakeLists.txt | 8 ++++++-- src/OSSupport/StackTrace.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/OSSupport/StackTrace.h | 15 +++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/OSSupport/StackTrace.cpp create mode 100644 src/OSSupport/StackTrace.h (limited to 'src/OSSupport') diff --git a/src/OSSupport/CMakeLists.txt b/src/OSSupport/CMakeLists.txt index c3eabeef6..592525941 100644 --- a/src/OSSupport/CMakeLists.txt +++ b/src/OSSupport/CMakeLists.txt @@ -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}) diff --git a/src/OSSupport/StackTrace.cpp b/src/OSSupport/StackTrace.cpp new file mode 100644 index 000000000..57c547fa1 --- /dev/null +++ b/src/OSSupport/StackTrace.cpp @@ -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 +#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 +} + + + + diff --git a/src/OSSupport/StackTrace.h b/src/OSSupport/StackTrace.h new file mode 100644 index 000000000..228a00077 --- /dev/null +++ b/src/OSSupport/StackTrace.h @@ -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); + + + + -- cgit v1.2.3 From 7049db5bf88e6a75f108fa0640dfb9ada85fadff Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 30 Nov 2014 14:23:51 +0100 Subject: Fixed compiling on linux. --- src/OSSupport/StackTrace.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/OSSupport') diff --git a/src/OSSupport/StackTrace.cpp b/src/OSSupport/StackTrace.cpp index 57c547fa1..a56568457 100644 --- a/src/OSSupport/StackTrace.cpp +++ b/src/OSSupport/StackTrace.cpp @@ -9,6 +9,7 @@ #include "../StackWalker.h" #else #include + #include #endif -- cgit v1.2.3