diff options
author | Shauren <shauren.trinity@gmail.com> | 2018-08-29 23:59:24 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2018-08-29 23:59:24 +0200 |
commit | 62db1fb683c13b8dde0cc359564e090e0594658f (patch) | |
tree | 7259cff0f62d61d590e4e1b17bce3751ef3821e4 /src/common/Debugging/Errors.cpp | |
parent | 704870496f672f628635e45a1cec98a485d86c28 (diff) |
Core/CrashHandler: Include assertion messages in crash reports
Diffstat (limited to 'src/common/Debugging/Errors.cpp')
-rw-r--r-- | src/common/Debugging/Errors.cpp | 89 |
1 files changed, 61 insertions, 28 deletions
diff --git a/src/common/Debugging/Errors.cpp b/src/common/Debugging/Errors.cpp index f6794a7358f..97d8d3ff0fe 100644 --- a/src/common/Debugging/Errors.cpp +++ b/src/common/Debugging/Errors.cpp @@ -17,7 +17,7 @@ */ #include "Errors.h" - +#include "StringFormat.h" #include <cstdio> #include <cstdlib> #include <thread> @@ -34,14 +34,46 @@ terminates the application. */ -namespace Trinity { +#if TRINITY_PLATFORM == TRINITY_PLATFORM_WINDOWS +#define Crash(message) \ + ULONG_PTR execeptionArgs[] = { reinterpret_cast<ULONG_PTR>(strdup(message)), reinterpret_cast<ULONG_PTR>(_ReturnAddress()) }; \ + RaiseException(EXCEPTION_ASSERTION_FAILURE, 0, 2, execeptionArgs); +#else +// should be easily accessible in gdb +extern "C" TC_COMMON_API char const* TrinityAssertionFailedMessage = nullptr; +#define Crash(message) \ + TrinityAssertionFailedMessage = strdup(message); \ + *((volatile int*)nullptr) = 0; \ + exit(1); +#endif + +namespace +{ + std::string FormatAssertionMessage(char const* format, va_list args) + { + std::string formatted; + va_list len; + + va_copy(len, args); + int32 length = vsnprintf(nullptr, 0, format, len); + va_end(len); + + formatted.resize(length + 1); + vsnprintf(&formatted[0], length + 1, format, args); + + return formatted; + } +} + +namespace Trinity +{ void Assert(char const* file, int line, char const* function, char const* message) { - fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s\n", - file, line, function, message); - *((volatile int*)nullptr) = 0; - exit(1); + std::string formattedMessage = StringFormat("\n%s:%i in %s ASSERTION FAILED:\n %s\n", file, line, function, message); + fprintf(stderr, "%s", formattedMessage.c_str()); + fflush(stderr); + Crash(formattedMessage.c_str()); } void Assert(char const* file, int line, char const* function, char const* message, char const* format, ...) @@ -49,14 +81,13 @@ void Assert(char const* file, int line, char const* function, char const* messag va_list args; va_start(args, format); - fprintf(stderr, "\n%s:%i in %s ASSERTION FAILED:\n %s ", file, line, function, message); - vfprintf(stderr, format, args); - fprintf(stderr, "\n"); + std::string formattedMessage = StringFormat("\n%s:%i in %s ASSERTION FAILED:\n %s\n", file, line, function, message) + FormatAssertionMessage(format, args) + '\n'; + va_end(args); + + fprintf(stderr, "%s", formattedMessage.c_str()); fflush(stderr); - va_end(args); - *((volatile int*)nullptr) = 0; - exit(1); + Crash(formattedMessage.c_str()); } void Fatal(char const* file, int line, char const* function, char const* message, ...) @@ -64,22 +95,22 @@ void Fatal(char const* file, int line, char const* function, char const* message va_list args; va_start(args, message); - fprintf(stderr, "\n%s:%i in %s FATAL ERROR:\n ", file, line, function); - vfprintf(stderr, message, args); - fprintf(stderr, "\n"); + std::string formattedMessage = StringFormat("\n%s:%i in %s FATAL ERROR:\n %s\n", file, line, function) + FormatAssertionMessage(message, args) + '\n'; + va_end(args); + + fprintf(stderr, "%s", formattedMessage.c_str()); fflush(stderr); std::this_thread::sleep_for(std::chrono::seconds(10)); - *((volatile int*)nullptr) = 0; - exit(1); + Crash(formattedMessage.c_str()); } void Error(char const* file, int line, char const* function, char const* message) { - fprintf(stderr, "\n%s:%i in %s ERROR:\n %s\n", - file, line, function, message); - *((volatile int*)nullptr) = 0; - exit(1); + std::string formattedMessage = StringFormat("\n%s:%i in %s ERROR:\n %s\n", file, line, function, message); + fprintf(stderr, "%s", formattedMessage.c_str()); + fflush(stderr); + Crash(formattedMessage.c_str()); } void Warning(char const* file, int line, char const* function, char const* message) @@ -90,17 +121,19 @@ void Warning(char const* file, int line, char const* function, char const* messa void Abort(char const* file, int line, char const* function) { - fprintf(stderr, "\n%s:%i in %s ABORTED.\n", - file, line, function); - *((volatile int*)nullptr) = 0; - exit(1); + std::string formattedMessage = StringFormat("\n%s:%i in %s ABORTED.\n", file, line, function); + fprintf(stderr, "%s", formattedMessage.c_str()); + fflush(stderr); + Crash(formattedMessage.c_str()); } -void AbortHandler(int /*sigval*/) +void AbortHandler(int sigval) { // nothing useful to log here, no way to pass args - *((volatile int*)nullptr) = 0; - exit(1); + std::string formattedMessage = StringFormat("Caught signal %i\n", sigval); + fprintf(stderr, "%s", formattedMessage.c_str()); + fflush(stderr); + Crash(formattedMessage.c_str()); } } // namespace Trinity |